--- a/ucx/buffer.c Sat Dec 01 20:34:55 2012 +0100 +++ b/ucx/buffer.c Mon Aug 12 14:40:19 2013 +0200 @@ -1,3 +1,31 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2013 Olaf Wintermann. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. 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. + * + * 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 HOLDER 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 "buffer.h" #include <stdarg.h> #include <stdlib.h> @@ -8,7 +36,7 @@ if (buffer) { buffer->flags = flags; if (!space) { - buffer->space = malloc(size); + buffer->space = (char*)malloc(size); if (!buffer->space) { free(buffer); return NULL; @@ -16,7 +44,7 @@ memset(buffer->space, 0, size); buffer->flags |= UCX_BUFFER_AUTOFREE; } else { - buffer->space = space; + buffer->space = (char*)space; } buffer->capacity = size; buffer->size = 0; @@ -48,7 +76,7 @@ UcxBuffer *dst = (UcxBuffer*) malloc(sizeof(UcxBuffer)); if (dst) { - dst->space = malloc(length); + dst->space = (char*)malloc(length); if (!dst->space) { free(dst); return NULL; @@ -63,7 +91,7 @@ } int ucx_buffer_seek(UcxBuffer *buffer, off_t offset, int whence) { - off_t npos; + size_t npos = 0; switch (whence) { case SEEK_SET: npos = 0; @@ -78,7 +106,7 @@ npos += offset; - if (npos < 0 || npos > buffer->size) { + if (npos > buffer->size) { return -1; } else { buffer->pos = npos; @@ -95,7 +123,7 @@ size_t newcap = buffer->capacity; while (buffer->pos + len > newcap) newcap <<= 1; - char *newspace = realloc(buffer->space, newcap); + char *newspace = (char*)realloc(buffer->space, newcap); if (newspace) { memset(newspace+buffer->size, 0, newcap-buffer->size); buffer->space = newspace; @@ -185,7 +213,7 @@ size_t ucx_buffer_generic_copy(void *s1, void *s2, read_func readfnc, write_func writefnc, size_t bufsize) { size_t ncp = 0; - char *buf = malloc(bufsize); + char *buf = (char*)malloc(bufsize); if(buf == NULL) { return 0; } @@ -202,3 +230,31 @@ free(buf); return ncp; } + +size_t ucx_buffer_generic_ncopy(void *s1, void *s2, + read_func readfnc, write_func writefnc, size_t bufsize, size_t n) { + if(n == 0) { + return 0; + } + + size_t ncp = 0; + char *buf = (char*)malloc(bufsize); + if(buf == NULL) { + return 0; + } + + size_t r; + size_t rn = bufsize > n ? n : bufsize; + while((r = readfnc(buf, 1, rn, s1)) != 0) { + r = writefnc(buf, 1, r, s2); + ncp += r; + n -= r; + rn = bufsize > n ? n : bufsize; + if(r == 0 || n == 0) { + break; + } + } + + free(buf); + return ncp; +}