/* ***************************************************************************** * utils/buffer.c -- libg1m custom buffers 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 /* ************************************************************************** */ /* FILE buffer */ /* ************************************************************************** */ /** * 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); # if LOGLEVEL <= ll_error if (size) { log_info("Got the following:"); logm_info(buf, orig - size); } # endif return (g1m_error_eof); } } /* no error */ return (0); } #endif /* ************************************************************************** */ /* Memory buffer */ /* ************************************************************************** */ /** * g1m_membuffer_read: * Read from a memory buffer. * * @arg vcookie the cursor (uncasted) * @arg dest the destination buffer. * @arg size the size to read. * @return the error, if any. */ int g1m_membuffer_read(void *vcookie, unsigned char *dest, size_t size) { g1m_cursor_t *cursor = (void*)vcookie; if (size > cursor->left) return (g1m_error_eof); memcpy(dest, cursor->p, size); cursor->p += size; return (0); } /* ************************************************************************** */ /* Limited buffer */ /* ************************************************************************** */ /** * g1m_limbuffer_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_limbuffer_read(void *vcookie, unsigned char *buf, size_t size) { /* check if the size is okay */ g1m_limited_t *lim = (void*)vcookie; if (size > lim->left) return (g1m_error_eof); g1m_buffer_t *buffer = lim->buffer; /* read */ READ(buf, size) /* no error */ return (0); } /** * g1m_empty_limbuffer: * Empty the limit buffer. * * @arg limbuf the limbuffer. */ int g1m_empty_limbuffer(g1m_buffer_t *limbuffer) { g1m_limited_t *lim = (void*)limbuffer->cookie; g1m_buffer_t *buffer = lim->buffer; SKIP(lim->left) return (0); }