2007-05-17 14:56:12 +00:00
|
|
|
/*
|
2014-02-05 21:02:28 +00:00
|
|
|
* Copyright (c) 2007-2014, Anthony Minessale II
|
2007-05-17 14:56:12 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* * Neither the name of the original author; nor the names of any contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
|
|
|
|
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
|
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "openzap.h"
|
|
|
|
#include "zap_buffer.h"
|
|
|
|
|
|
|
|
static unsigned buffer_id = 0;
|
|
|
|
|
|
|
|
struct zap_buffer {
|
|
|
|
unsigned char *data;
|
|
|
|
unsigned char *head;
|
|
|
|
zap_size_t used;
|
|
|
|
zap_size_t actually_used;
|
|
|
|
zap_size_t datalen;
|
|
|
|
zap_size_t max_len;
|
|
|
|
zap_size_t blocksize;
|
|
|
|
unsigned id;
|
|
|
|
int loops;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-03-10 15:49:27 +00:00
|
|
|
OZ_DECLARE(zap_status_t) zap_buffer_create(zap_buffer_t **buffer, zap_size_t blocksize, zap_size_t start_len, zap_size_t max_len)
|
2007-05-17 14:56:12 +00:00
|
|
|
{
|
|
|
|
zap_buffer_t *new_buffer;
|
|
|
|
|
2007-05-17 15:08:37 +00:00
|
|
|
new_buffer = malloc(sizeof(*new_buffer));
|
|
|
|
if (new_buffer) {
|
2007-05-17 14:56:12 +00:00
|
|
|
memset(new_buffer, 0, sizeof(*new_buffer));
|
|
|
|
|
|
|
|
if (start_len) {
|
2007-05-17 15:08:37 +00:00
|
|
|
new_buffer->data = malloc(start_len);
|
|
|
|
if (!new_buffer->data) {
|
2007-05-17 14:56:12 +00:00
|
|
|
free(new_buffer);
|
|
|
|
return ZAP_MEMERR;
|
|
|
|
}
|
|
|
|
memset(new_buffer->data, 0, start_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
new_buffer->max_len = max_len;
|
|
|
|
new_buffer->datalen = start_len;
|
|
|
|
new_buffer->id = buffer_id++;
|
|
|
|
new_buffer->blocksize = blocksize;
|
|
|
|
new_buffer->head = new_buffer->data;
|
|
|
|
|
|
|
|
*buffer = new_buffer;
|
|
|
|
return ZAP_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ZAP_MEMERR;
|
|
|
|
}
|
|
|
|
|
2009-03-10 15:49:27 +00:00
|
|
|
OZ_DECLARE(zap_size_t) zap_buffer_len(zap_buffer_t *buffer)
|
2007-05-17 14:56:12 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
assert(buffer != NULL);
|
|
|
|
|
|
|
|
return buffer->datalen;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-10 15:49:27 +00:00
|
|
|
OZ_DECLARE(zap_size_t) zap_buffer_freespace(zap_buffer_t *buffer)
|
2007-05-17 14:56:12 +00:00
|
|
|
{
|
|
|
|
assert(buffer != NULL);
|
|
|
|
|
|
|
|
|
|
|
|
if (buffer->max_len) {
|
|
|
|
return (zap_size_t) (buffer->max_len - buffer->used);
|
|
|
|
}
|
|
|
|
return 1000000;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-03-10 15:49:27 +00:00
|
|
|
OZ_DECLARE(zap_size_t) zap_buffer_inuse(zap_buffer_t *buffer)
|
2007-05-17 14:56:12 +00:00
|
|
|
{
|
|
|
|
assert(buffer != NULL);
|
|
|
|
|
|
|
|
return buffer->used;
|
|
|
|
}
|
|
|
|
|
2009-03-10 15:49:27 +00:00
|
|
|
OZ_DECLARE(zap_size_t) zap_buffer_seek(zap_buffer_t *buffer, zap_size_t datalen)
|
2008-09-06 20:41:50 +00:00
|
|
|
{
|
|
|
|
zap_size_t reading = 0;
|
|
|
|
|
|
|
|
assert(buffer != NULL);
|
|
|
|
|
|
|
|
if (buffer->used < 1) {
|
|
|
|
buffer->used = 0;
|
|
|
|
return 0;
|
|
|
|
} else if (buffer->used >= datalen) {
|
|
|
|
reading = datalen;
|
|
|
|
} else {
|
|
|
|
reading = buffer->used;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer->used = buffer->actually_used - reading;
|
|
|
|
buffer->head = buffer->data + reading;
|
|
|
|
|
|
|
|
return reading;
|
|
|
|
}
|
|
|
|
|
2009-03-10 15:49:27 +00:00
|
|
|
OZ_DECLARE(zap_size_t) zap_buffer_toss(zap_buffer_t *buffer, zap_size_t datalen)
|
2007-05-17 14:56:12 +00:00
|
|
|
{
|
|
|
|
zap_size_t reading = 0;
|
|
|
|
|
|
|
|
assert(buffer != NULL);
|
|
|
|
|
|
|
|
if (buffer->used < 1) {
|
|
|
|
buffer->used = 0;
|
|
|
|
return 0;
|
|
|
|
} else if (buffer->used >= datalen) {
|
|
|
|
reading = datalen;
|
|
|
|
} else {
|
|
|
|
reading = buffer->used;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer->used -= reading;
|
|
|
|
buffer->head += reading;
|
|
|
|
|
|
|
|
return buffer->used;
|
|
|
|
}
|
|
|
|
|
2009-03-10 15:49:27 +00:00
|
|
|
OZ_DECLARE(void) zap_buffer_set_loops(zap_buffer_t *buffer, int loops)
|
2007-05-17 14:56:12 +00:00
|
|
|
{
|
|
|
|
buffer->loops = loops;
|
|
|
|
}
|
|
|
|
|
2009-03-10 15:49:27 +00:00
|
|
|
OZ_DECLARE(zap_size_t) zap_buffer_read_loop(zap_buffer_t *buffer, void *data, zap_size_t datalen)
|
2007-05-17 14:56:12 +00:00
|
|
|
{
|
|
|
|
zap_size_t len;
|
2008-09-06 20:41:50 +00:00
|
|
|
if ((len = zap_buffer_read(buffer, data, datalen)) < datalen) {
|
2007-05-17 14:56:12 +00:00
|
|
|
if (buffer->loops == 0) {
|
2008-09-06 20:41:50 +00:00
|
|
|
return len;
|
2007-05-17 14:56:12 +00:00
|
|
|
}
|
|
|
|
buffer->head = buffer->data;
|
|
|
|
buffer->used = buffer->actually_used;
|
2009-03-06 18:17:15 +00:00
|
|
|
len = zap_buffer_read(buffer, (char*)data + len, datalen - len);
|
2007-05-17 14:56:12 +00:00
|
|
|
buffer->loops--;
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2009-03-10 15:49:27 +00:00
|
|
|
OZ_DECLARE(zap_size_t) zap_buffer_read(zap_buffer_t *buffer, void *data, zap_size_t datalen)
|
2007-05-17 14:56:12 +00:00
|
|
|
{
|
|
|
|
zap_size_t reading = 0;
|
|
|
|
|
|
|
|
assert(buffer != NULL);
|
|
|
|
assert(data != NULL);
|
|
|
|
|
|
|
|
|
|
|
|
if (buffer->used < 1) {
|
|
|
|
buffer->used = 0;
|
|
|
|
return 0;
|
|
|
|
} else if (buffer->used >= datalen) {
|
|
|
|
reading = datalen;
|
|
|
|
} else {
|
|
|
|
reading = buffer->used;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(data, buffer->head, reading);
|
|
|
|
buffer->used -= reading;
|
|
|
|
buffer->head += reading;
|
|
|
|
|
|
|
|
/* if (buffer->id == 4) printf("%u o %d = %d\n", buffer->id, (unsigned)reading, (unsigned)buffer->used); */
|
|
|
|
return reading;
|
|
|
|
}
|
|
|
|
|
2009-03-10 15:49:27 +00:00
|
|
|
OZ_DECLARE(zap_size_t) zap_buffer_write(zap_buffer_t *buffer, const void *data, zap_size_t datalen)
|
2007-05-17 14:56:12 +00:00
|
|
|
{
|
|
|
|
zap_size_t freespace, actual_freespace;
|
|
|
|
|
|
|
|
assert(buffer != NULL);
|
|
|
|
assert(data != NULL);
|
|
|
|
assert(buffer->data != NULL);
|
|
|
|
|
|
|
|
if (!datalen) {
|
|
|
|
return buffer->used;
|
|
|
|
}
|
|
|
|
|
|
|
|
actual_freespace = buffer->datalen - buffer->actually_used;
|
2008-01-14 19:10:12 +00:00
|
|
|
if (actual_freespace < datalen && (!buffer->max_len || (buffer->used + datalen <= buffer->max_len))) {
|
2007-05-17 14:56:12 +00:00
|
|
|
memmove(buffer->data, buffer->head, buffer->used);
|
|
|
|
buffer->head = buffer->data;
|
|
|
|
buffer->actually_used = buffer->used;
|
|
|
|
}
|
|
|
|
|
|
|
|
freespace = buffer->datalen - buffer->used;
|
|
|
|
|
|
|
|
/*
|
2007-06-04 14:53:12 +00:00
|
|
|
if (buffer->data != buffer->head) {
|
|
|
|
memmove(buffer->data, buffer->head, buffer->used);
|
|
|
|
buffer->head = buffer->data;
|
|
|
|
}
|
|
|
|
*/
|
2007-05-17 14:56:12 +00:00
|
|
|
|
|
|
|
if (freespace < datalen) {
|
|
|
|
zap_size_t new_size, new_block_size;
|
2008-07-30 15:05:07 +00:00
|
|
|
void *data;
|
2007-05-17 14:56:12 +00:00
|
|
|
|
|
|
|
new_size = buffer->datalen + datalen;
|
|
|
|
new_block_size = buffer->datalen + buffer->blocksize;
|
|
|
|
|
|
|
|
if (new_block_size > new_size) {
|
|
|
|
new_size = new_block_size;
|
|
|
|
}
|
|
|
|
buffer->head = buffer->data;
|
2008-07-30 15:05:07 +00:00
|
|
|
data = realloc(buffer->data, new_size);
|
|
|
|
if (!data) {
|
2007-05-17 14:56:12 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2008-07-30 15:05:07 +00:00
|
|
|
buffer->data = data;
|
2007-05-17 14:56:12 +00:00
|
|
|
buffer->head = buffer->data;
|
|
|
|
buffer->datalen = new_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
freespace = buffer->datalen - buffer->used;
|
|
|
|
|
|
|
|
if (freespace < datalen) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
memcpy(buffer->head + buffer->used, data, datalen);
|
|
|
|
buffer->used += datalen;
|
|
|
|
buffer->actually_used += datalen;
|
|
|
|
}
|
|
|
|
/* if (buffer->id == 4) printf("%u i %d = %d\n", buffer->id, (unsigned)datalen, (unsigned)buffer->used); */
|
|
|
|
|
|
|
|
return buffer->used;
|
|
|
|
}
|
|
|
|
|
2009-03-10 15:49:27 +00:00
|
|
|
OZ_DECLARE(void) zap_buffer_zero(zap_buffer_t *buffer)
|
2007-05-17 14:56:12 +00:00
|
|
|
{
|
|
|
|
assert(buffer != NULL);
|
|
|
|
assert(buffer->data != NULL);
|
|
|
|
|
|
|
|
buffer->used = 0;
|
2007-05-26 03:27:31 +00:00
|
|
|
buffer->actually_used = 0;
|
2007-05-17 14:56:12 +00:00
|
|
|
buffer->head = buffer->data;
|
|
|
|
}
|
|
|
|
|
2009-03-10 15:49:27 +00:00
|
|
|
OZ_DECLARE(zap_size_t) zap_buffer_zwrite(zap_buffer_t *buffer, const void *data, zap_size_t datalen)
|
2007-12-02 16:05:19 +00:00
|
|
|
{
|
|
|
|
zap_size_t w;
|
|
|
|
|
|
|
|
if (!(w = zap_buffer_write(buffer, data, datalen))) {
|
|
|
|
zap_buffer_zero(buffer);
|
|
|
|
return zap_buffer_write(buffer, data, datalen);
|
|
|
|
}
|
|
|
|
|
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
2009-03-10 15:49:27 +00:00
|
|
|
OZ_DECLARE(void) zap_buffer_destroy(zap_buffer_t **buffer)
|
2007-05-17 14:56:12 +00:00
|
|
|
{
|
|
|
|
if (*buffer) {
|
|
|
|
free((*buffer)->data);
|
|
|
|
free(*buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
*buffer = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* For Emacs:
|
|
|
|
* Local Variables:
|
|
|
|
* mode:c
|
|
|
|
* indent-tabs-mode:t
|
|
|
|
* tab-width:4
|
|
|
|
* c-basic-offset:4
|
|
|
|
* End:
|
|
|
|
* For VIM:
|
2013-06-25 16:50:17 +00:00
|
|
|
* vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
|
2007-05-17 14:56:12 +00:00
|
|
|
*/
|