cake
/
libg1m
Archived
1
0
Fork 0
This repository has been archived on 2024-03-16. You can view files and clone it, but cannot push or open issues or pull requests.
libg1m/src/utils/buffer.c

173 lines
4.9 KiB
C

/* *****************************************************************************
* utils/buffer.c -- libg1m custom buffers utilities.
* Copyright (C) 2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
*
* 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 <http://www.gnu.org/licenses/>.
* ************************************************************************** */
#include <libg1m/internals.h>
#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;
# if LOGLEVEL <= ll_error
size_t orig = size;
# endif
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 (feof(file) ? g1m_error_eof : g1m_error_read);
}
}
/* no error */
return (0);
}
/**
* g1m_filebuffer_write:
* Write to a filebuffer.
*
* @arg vcookie the FILE* (uncasted)
* @arg buf the buffer to write.
* @arg size the size to write.
* @return the error (if any).
*/
int g1m_filebuffer_write(void *vcookie, const unsigned char *buf, size_t size)
{
FILE *file = (void*)vcookie;
# if LOGLEVEL <= ll_error
size_t orig = size;
# endif
while (size) {
size_t written = fwrite(buf, 1, size, file);
buf += written;
size -= written;
if (!written) {
log_error("WRITING failed: wrote %" PRIuSIZE "/%" PRIuSIZE
" bytes, %" PRIuSIZE " missing.", orig - size, orig, size);
return (g1m_error_write);
}
}
/* 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->g1m_cursor_left)
return (g1m_error_eof);
memcpy(dest, cursor->g1m_cursor_p, size);
cursor->g1m_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->g1m_limited_left) return (g1m_error_eof);
g1m_buffer_t *buffer = lim->g1m_limited_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->g1m_buffer_cookie;
g1m_buffer_t *buffer = lim->g1m_limited_buffer;
SKIP(lim->g1m_limited_left)
return (0);
}
/* ************************************************************************** */
/* The announce callback */
/* ************************************************************************** */
/**
* g1m_announce_callback:
* Main announce callback, that just contributes to a size.
*
* @arg vpsize the size_t pointer.
* @arg size the size to add.
* @return 0 because OK.
*/
int g1m_announce_callback(void *vpsize, size_t size)
{
size_t *psize = (void*)vpsize;
*psize += size;
return (0);
}