/* ***************************************************************************** * utils/filebuffer.c -- FILE to libg1m buffer utilities. * Copyright (C) 2017 Thomas "Cakeisalie5" Touhey * * This file is part of libg1m. * libg1m is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 3.0 of the License, * or (at your option) any later version. * * libg1m is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with libg1m; if not, see . * ************************************************************************** */ #include #ifndef G1M_DISABLED_FILE /** * g1m_filebuffer_read: * Read from a filebuffer. * * @arg vcookie the FILE* (uncasted) * @arg buf the buffer to fill. * @arg size the size to read. * @return the error (if any). */ int g1m_filebuffer_read(void *vcookie, unsigned char *buf, size_t size) { FILE *file = (void*)vcookie; size_t orig = size; while (size) { size_t read = fread(buf, 1, size, file); size -= read; if (!read || read == (size_t)-1) { log_error("READING failed: read %" PRIuSIZE "/%" PRIuSIZE " bytes, %" PRIuSIZE " missing.", orig - size, orig, size); return (g1m_error_eof); } } /* no error */ return (0); } #endif