2017-02-24 02:18:10 +01:00
|
|
|
/* *****************************************************************************
|
|
|
|
* 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
|
2017-04-13 00:18:10 +02:00
|
|
|
* to read.
|
|
|
|
* ************************************************************************** */
|
|
|
|
#ifndef LIBG1M_BUFFER_H
|
|
|
|
# define LIBG1M_BUFFER_H
|
|
|
|
# include <stdlib.h>
|
|
|
|
# include <stdint.h>
|
|
|
|
|
|
|
|
/* ************************************************************************** */
|
|
|
|
/* General definition */
|
|
|
|
/* ************************************************************************** */
|
|
|
|
/* There are two use cases:
|
2017-02-24 02:18:10 +01:00
|
|
|
* - 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
|
2017-03-19 23:27:21 +01:00
|
|
|
* is used. If there is an error and the `unannounce` callback is set, it
|
|
|
|
* will be called in order to free any allocated buffer.
|
2017-02-24 02:18:10 +01:00
|
|
|
*
|
|
|
|
* In each case, the `cookie` is sent as the first argument to your callbacks.
|
2017-04-13 00:18:10 +02:00
|
|
|
* Here are their types: */
|
2017-02-19 23:31:46 +01:00
|
|
|
|
2017-03-23 16:41:27 +01:00
|
|
|
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*, size_t);
|
2017-03-19 23:27:21 +01:00
|
|
|
typedef void (*g1m_buffer_unannounce_t)(void*);
|
2017-02-19 23:31:46 +01:00
|
|
|
|
2017-04-13 00:18:10 +02:00
|
|
|
/* ... and the structure of a buffer: */
|
|
|
|
|
2017-02-19 23:31:46 +01:00
|
|
|
typedef struct {
|
2017-04-13 00:18:10 +02:00
|
|
|
void *g1m_buffer_cookie;
|
|
|
|
size_t g1m_buffer_offset;
|
2017-02-19 23:31:46 +01:00
|
|
|
|
2017-04-13 00:18:10 +02:00
|
|
|
/* callbacks */
|
|
|
|
g1m_buffer_read_t g1m_buffer_read;
|
|
|
|
g1m_buffer_write_t g1m_buffer_write;
|
|
|
|
g1m_buffer_announce_t g1m_buffer_announce;
|
|
|
|
g1m_buffer_unannounce_t g1m_buffer_unannounce;
|
2017-02-19 23:31:46 +01:00
|
|
|
} g1m_buffer_t;
|
2017-03-19 23:27:21 +01:00
|
|
|
/* ************************************************************************** */
|
|
|
|
/* File buffer definition */
|
|
|
|
/* ************************************************************************** */
|
|
|
|
/* Callbacks (the cookie is the FILE* pointer) */
|
2017-04-13 00:18:10 +02:00
|
|
|
extern int g1m_filebuffer_read(void *g1m_arg_cookie,
|
|
|
|
unsigned char *g1m_arg_buf, size_t g1m_arg_size);
|
|
|
|
extern int g1m_filebuffer_write(void *g1m_arg_cookie,
|
|
|
|
const unsigned char *g1m_arg_dest, size_t g1m_arg_size);
|
2017-03-06 12:10:35 +01:00
|
|
|
/* ************************************************************************** */
|
|
|
|
/* Memory buffer definition */
|
|
|
|
/* ************************************************************************** */
|
|
|
|
/* Cookie structure */
|
2017-04-13 00:18:10 +02:00
|
|
|
typedef struct g1m_cursor_s {
|
|
|
|
const unsigned char *g1m_cursor_p;
|
|
|
|
size_t g1m_cursor_left;
|
2017-03-06 12:10:35 +01:00
|
|
|
} g1m_cursor_t;
|
|
|
|
|
|
|
|
/* Related callbacks and functions */
|
2017-04-13 00:18:10 +02:00
|
|
|
extern int g1m_membuffer_read(void *g1m_arg_cookie,
|
|
|
|
unsigned char *g1m_arg_dest, size_t g1m_arg_size);
|
2017-03-06 12:10:35 +01:00
|
|
|
/* ************************************************************************** */
|
|
|
|
/* Limited buffer definition */
|
|
|
|
/* ************************************************************************** */
|
|
|
|
/* Cookie structure */
|
2017-04-13 00:18:10 +02:00
|
|
|
typedef struct g1m_limited_s {
|
|
|
|
g1m_buffer_t *g1m_limited_buffer;
|
|
|
|
size_t g1m_limited_left;
|
2017-03-06 12:10:35 +01:00
|
|
|
} g1m_limited_t;
|
|
|
|
|
|
|
|
/* Related functions and callbacks */
|
2017-04-13 00:18:10 +02:00
|
|
|
int g1m_limbuffer_read(void *g1m_arg_cookie,
|
|
|
|
unsigned char *g1m_arg_dest, size_t g1m_arg_size);
|
|
|
|
int g1m_empty_limbuffer(g1m_buffer_t *g1m_arg_limbuffer);
|
2017-03-06 12:10:35 +01:00
|
|
|
/* ************************************************************************** */
|
|
|
|
/* Buffer macros */
|
|
|
|
/* ************************************************************************** */
|
2017-03-19 23:27:21 +01:00
|
|
|
/* Initialize a file buffer */
|
|
|
|
# define g1m_filebuffer(F) (g1m_filebuffer_t){ \
|
2017-04-13 00:18:10 +02:00
|
|
|
.g1m_buffer_cookie = (void*)(F), \
|
|
|
|
.g1m_buffer_read = g1m_filebuffer_read, \
|
|
|
|
.g1m_buffer_write = g1m_filebuffer_write}
|
2017-03-19 23:27:21 +01:00
|
|
|
|
2017-03-06 12:10:35 +01:00
|
|
|
/* Initialize a memory buffer */
|
|
|
|
# define g1m_membuffer(P, SZ) (g1m_buffer_t){ \
|
2017-04-13 00:18:10 +02:00
|
|
|
.g1m_buffer_cookie = (g1m_cursor_t[]){{ \
|
|
|
|
.g1m_cursor_p = (P), .g1m_cursor_left = (SZ)}}, \
|
|
|
|
.g1m_buffer_read = g1m_membuffer_read}
|
2017-03-06 12:10:35 +01:00
|
|
|
|
|
|
|
/* Initialize a limited buffer */
|
|
|
|
# define g1m_limbuffer(BUFFER, SZ) (g1m_buffer_t){ \
|
2017-04-13 00:18:10 +02:00
|
|
|
.g1m_buffer_cookie = (g1m_limited_t[]){{ \
|
|
|
|
.g1m_limited_buffer = (BUFFER), .g1m_limited_left = (SZ)}}, \
|
|
|
|
.g1m_buffer_read = g1m_limbuffer_read}
|
2017-03-06 12:10:35 +01:00
|
|
|
|
2017-02-19 23:31:46 +01:00
|
|
|
#endif /* LIBG1M_BUFFER_H */
|