paulo@0: /* paulo@0: * $Id: io_buf.c,v 1.2 2003/09/17 17:44:11 hipnod Exp $ paulo@0: * paulo@0: * Copyright (C) 2003 giFT project (gift.sourceforge.net) paulo@0: * paulo@0: * This program is free software; you can redistribute it and/or modify it paulo@0: * under the terms of the GNU General Public License as published by the paulo@0: * Free Software Foundation; either version 2, or (at your option) any paulo@0: * later version. paulo@0: * paulo@0: * This program is distributed in the hope that it will be useful, but paulo@0: * WITHOUT ANY WARRANTY; without even the implied warranty of paulo@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU paulo@0: * General Public License for more details. paulo@0: */ paulo@0: paulo@0: #include "gt_gnutella.h" paulo@0: paulo@0: #include "io_buf.h" paulo@0: paulo@0: /*****************************************************************************/ paulo@0: paulo@0: struct io_buf *io_buf_new (size_t size) paulo@0: { paulo@0: struct io_buf *buf; paulo@0: uint8_t *data; paulo@0: paulo@0: if (!(data = gift_malloc (size + 1))) paulo@0: return NULL; paulo@0: paulo@0: if (!(buf = gift_malloc (sizeof (struct io_buf)))) paulo@0: { paulo@0: free (data); paulo@0: return NULL; paulo@0: } paulo@0: paulo@0: buf->data = data; paulo@0: buf->size = size; paulo@0: buf->r_offs = 0; paulo@0: buf->w_offs = 0; paulo@0: paulo@0: /* null terminate the buffer */ paulo@0: buf->data[size] = 0; paulo@0: paulo@0: return buf; paulo@0: } paulo@0: paulo@0: void io_buf_free (struct io_buf *buf) paulo@0: { paulo@0: uint8_t *data; paulo@0: paulo@0: if (!buf) paulo@0: return; paulo@0: paulo@0: data = io_buf_free_keep (buf); paulo@0: free (data); paulo@0: } paulo@0: paulo@0: uint8_t *io_buf_free_keep (struct io_buf *buf) paulo@0: { paulo@0: uint8_t *data; paulo@0: paulo@0: data = buf->data; paulo@0: free (buf); paulo@0: paulo@0: return data; paulo@0: } paulo@0: paulo@0: /* paulo@0: * Resize the underlying buffer. This includes +1 for the null terminator. paulo@0: */ paulo@0: BOOL io_buf_resize (struct io_buf *buf, size_t len) paulo@0: { paulo@0: uint8_t *resized; paulo@0: paulo@0: if (buf->size >= len) paulo@0: return TRUE; paulo@0: paulo@0: if (!(resized = gift_realloc (buf->data, len + 1))) paulo@0: return FALSE; paulo@0: paulo@0: buf->data = resized; paulo@0: buf->size = len; paulo@0: paulo@0: /* ensure null-termination */ paulo@0: buf->data[len] = 0; paulo@0: paulo@0: return TRUE; paulo@0: } paulo@0: paulo@0: void io_buf_reset (struct io_buf *buf) paulo@0: { paulo@0: buf->w_offs = 0; paulo@0: buf->r_offs = 0; paulo@0: } paulo@0: paulo@0: void io_buf_push (struct io_buf *buf, size_t len) paulo@0: { paulo@0: assert (len + buf->w_offs <= buf->size); paulo@0: buf->w_offs += len; paulo@0: } paulo@0: paulo@0: void io_buf_pop (struct io_buf *buf, size_t len) paulo@0: { paulo@0: assert (len + buf->r_offs <= buf->w_offs); paulo@0: buf->r_offs += len; paulo@0: } paulo@0: paulo@0: size_t io_buf_copy (struct io_buf *dst, struct io_buf *src, size_t len) paulo@0: { paulo@0: size_t src_avail = io_buf_read_avail (src); paulo@0: size_t dst_avail = io_buf_write_avail (dst); paulo@0: paulo@0: if (len > src_avail) paulo@0: len = src_avail; paulo@0: paulo@0: if (len > dst_avail) paulo@0: len = dst_avail; paulo@0: paulo@0: memcpy (dst->data + dst->w_offs, src->data + src->r_offs, len); paulo@0: paulo@0: dst->w_offs += len; paulo@0: src->r_offs += len; paulo@0: paulo@0: return len; paulo@0: }