universe@483: /* universe@483: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@483: * universe@483: * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. universe@483: * universe@483: * Redistribution and use in source and binary forms, with or without universe@483: * modification, are permitted provided that the following conditions are met: universe@483: * universe@483: * 1. Redistributions of source code must retain the above copyright universe@483: * notice, this list of conditions and the following disclaimer. universe@483: * universe@483: * 2. Redistributions in binary form must reproduce the above copyright universe@483: * notice, this list of conditions and the following disclaimer in the universe@483: * documentation and/or other materials provided with the distribution. universe@483: * universe@483: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@483: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@483: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@483: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@483: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@483: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@483: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@483: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@483: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@483: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@483: * POSSIBILITY OF SUCH DAMAGE. universe@483: */ universe@483: universe@483: #include "cx/utils.h" universe@483: universe@736: #ifndef CX_STREAM_BCOPY_BUF_SIZE universe@674: #define CX_STREAM_BCOPY_BUF_SIZE 8192 universe@736: #endif universe@736: universe@736: #ifndef CX_STREAM_COPY_BUF_SIZE universe@674: #define CX_STREAM_COPY_BUF_SIZE 1024 universe@736: #endif universe@674: universe@674: size_t cx_stream_bncopy( universe@674: void *src, universe@674: void *dest, universe@674: cx_read_func rfnc, universe@674: cx_write_func wfnc, universe@674: char *buf, universe@674: size_t bufsize, universe@674: size_t n universe@674: ) { universe@674: if (n == 0) { universe@483: return 0; universe@483: } universe@674: universe@674: char *lbuf; universe@674: size_t ncp = 0; universe@674: universe@674: if (buf) { universe@674: if (bufsize == 0) return 0; universe@674: lbuf = buf; universe@674: } else { universe@674: if (bufsize == 0) bufsize = CX_STREAM_BCOPY_BUF_SIZE; universe@674: lbuf = malloc(bufsize); universe@674: if (lbuf == NULL) { universe@674: return 0; universe@674: } universe@674: } universe@674: universe@674: size_t r; universe@674: size_t rn = bufsize > n ? n : bufsize; universe@674: while ((r = rfnc(lbuf, 1, rn, src)) != 0) { universe@674: r = wfnc(lbuf, 1, r, dest); universe@674: ncp += r; universe@674: n -= r; universe@674: rn = bufsize > n ? n : bufsize; universe@674: if (r == 0 || n == 0) { universe@674: break; universe@674: } universe@674: } universe@674: universe@674: if (lbuf != buf) { universe@674: free(lbuf); universe@674: } universe@674: universe@674: return ncp; universe@674: } universe@674: universe@674: size_t cx_stream_ncopy( universe@674: void *src, universe@674: void *dest, universe@674: cx_read_func rfnc, universe@674: cx_write_func wfnc, universe@674: size_t n universe@674: ) { universe@675: char buf[CX_STREAM_COPY_BUF_SIZE]; universe@675: return cx_stream_bncopy(src, dest, rfnc, wfnc, universe@675: buf, CX_STREAM_COPY_BUF_SIZE, n); universe@483: } universe@674: universe@674: #ifndef CX_SZMUL_BUILTIN universe@674: #include "szmul.c" universe@483: #endif