X-Git-Url: http://tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Fbuffer.c;fp=src%2Fbuffer.c;h=fc4fac0f3629570f3097d65c54337db857b56f8e;hb=f431fcb35f400be388a905ae0f7f50c1f5c4cd5d;hp=0000000000000000000000000000000000000000;hpb=3794e551c7db9aa81405f65f7b04a9951c4120b2;p=tinc diff --git a/src/buffer.c b/src/buffer.c new file mode 100644 index 00000000..fc4fac0f --- /dev/null +++ b/src/buffer.c @@ -0,0 +1,99 @@ +/* + buffer.c -- buffer management + Copyright (C) 2011 Guus Sliepen , + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include "system.h" + +#include "buffer.h" +#include "xalloc.h" + +// Make sure we can add size bytes to the buffer, and return a pointer to the start of those bytes. + +char *buffer_prepare(buffer_t *buffer, int size) { + if(!buffer->data) { + buffer->maxlen = size; + buffer->data = xmalloc(size); + } else { + if(buffer->offset && buffer->len + size > buffer->maxlen) { + memmove(buffer->data, buffer->data + buffer->offset, buffer->len - buffer->offset); + buffer->len -= buffer->offset; + buffer->offset = 0; + } + + if(buffer->len + size > buffer->maxlen) { + buffer->maxlen = buffer->len + size; + buffer->data = xrealloc(buffer->data, buffer->maxlen); + } + } + + buffer->len += size; + return buffer->data + buffer->offset; +} + +// Copy data into the buffer. + +char *buffer_add(buffer_t *buffer, const char *data, int size) { + memcpy(buffer_prepare(buffer, size), data, size); +} + +// Remove given number of bytes from the buffer, return a pointer to the start of them. + +static char *buffer_consume(buffer_t *buffer, int size) { + char *start = buffer->data + buffer->offset; + + buffer->offset += size; + + if(buffer->offset >= buffer->len) { + buffer->offset = 0; + buffer->len = 0; + } else { + buffer->offset += size; + } + + return start; +} + +// Check if there is a complete line in the buffer, and if so, return it NULL-terminated. + +char *buffer_readline(buffer_t *buffer) { + char *newline = memchr(buffer->data + buffer->offset, '\n', buffer->len - buffer->offset); + + if(!newline) + return NULL; + + int len = newline + 1 - buffer->data + buffer->offset; + *newline = 0; + return buffer_consume(buffer, len); +} + +// Check if we have enough bytes in the buffer, and if so, return a pointer to the start of them. + +char *buffer_read(buffer_t *buffer, int size) { + if(buffer->len - buffer->offset < size) + return NULL; + + return buffer_consume(buffer, size); +} + +void buffer_clear(buffer_t *buffer) { + free(buffer->data); + buffer->data = 0; + buffer->maxlen = 0; + buffer->len = 0; + buffer->offset = 0; +}