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/include/libg1m/buffer.h

93 lines
3.8 KiB
C

/* *****************************************************************************
* libg1m/buffer.h -- the libg1m buffer interface.
* 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/>.
*
* This interface is there so you can use a custom buffer for the library
* to read. There are two use cases:
* - you want to read data: the `size` of the element to read should be set,
* and the `read` callback will be used;
* - you want to receive data: if the `announce` callback is set, it will be
* called with the size of the file to write, in order to prepare space for
* it. If the `announce` callback returns an error, then the file will not
* be written. If the announcement is successful, then the `write` callback
* is used.
*
* In each case, the `cookie` is sent as the first argument to your callbacks.
* ************************************************************************** */
#ifndef LIBG1M_BUFFER_H
# define LIBG1M_BUFFER_H
# include <stdlib.h>
# include <stdint.h>
/* ************************************************************************** */
/* General definition */
/* ************************************************************************** */
/* The callbacks types... */
typedef int (*g1m_buffer_read_t)(void*, unsigned char*, size_t);
typedef int (*g1m_buffer_write_t)(void*, const unsigned char*, size_t);
typedef int (*g1m_buffer_announce_t)(void*, uint_fast32_t size);
/* ... and the structure of a buffer. */
typedef struct {
void *cookie;
size_t _offset;
g1m_buffer_read_t read;
g1m_buffer_write_t write;
g1m_buffer_announce_t announce;
} g1m_buffer_t;
/* ************************************************************************** */
/* Memory buffer definition */
/* ************************************************************************** */
/* Cookie structure */
typedef struct {
const unsigned char *p;
size_t left;
} g1m_cursor_t;
/* Related callbacks and functions */
int g1m_membuffer_read(void *cookie, unsigned char *dest, size_t size);
/* ************************************************************************** */
/* Limited buffer definition */
/* ************************************************************************** */
/* Cookie structure */
typedef struct {
g1m_buffer_t *buffer;
size_t left;
} g1m_limited_t;
/* Related functions and callbacks */
int g1m_limbuffer_read(void *cookie, unsigned char *dest, size_t size);
int g1m_empty_limbuffer(g1m_buffer_t *limbuffer);
/* ************************************************************************** */
/* Buffer macros */
/* ************************************************************************** */
/* Initialize a memory buffer */
# define g1m_membuffer(P, SZ) (g1m_buffer_t){ \
.cookie = (g1m_cursor_t[]){{.p = (P), .left = (SZ)}}, \
.read = g1m_membuffer_read}
/* Initialize a limited buffer */
# define g1m_limbuffer(BUFFER, SZ) (g1m_buffer_t){ \
.cookie = (g1m_limited_t[]){{.buffer = (BUFFER), .left = (SZ)}}, \
.read = g1m_limbuffer_read}
#endif /* LIBG1M_BUFFER_H */