#include "cx/utils.h"
#define CX_STREAM_BCOPY_BUF_SIZE 8192
#define CX_STREAM_COPY_BUF_SIZE 1024
size_t cx_stream_bncopy(
void *src,
void *dest,
cx_read_func rfnc,
cx_write_func wfnc,
char *buf,
size_t bufsize,
size_t n
) {
if (n ==
0) {
return 0;
}
char *lbuf;
size_t ncp =
0;
if (buf) {
if (bufsize ==
0)
return 0;
lbuf = buf;
}
else {
if (bufsize ==
0) bufsize =
CX_STREAM_BCOPY_BUF_SIZE;
lbuf = malloc(bufsize);
if (lbuf ==
NULL) {
return 0;
}
}
size_t r;
size_t rn = bufsize > n ? n : bufsize;
while ((r = rfnc(lbuf,
1, rn, src)) !=
0) {
r = wfnc(lbuf,
1, r, dest);
ncp += r;
n -= r;
rn = bufsize > n ? n : bufsize;
if (r ==
0 || n ==
0) {
break;
}
}
if (lbuf != buf) {
free(lbuf);
}
return ncp;
}
size_t cx_stream_ncopy(
void *src,
void *dest,
cx_read_func rfnc,
cx_write_func wfnc,
size_t n
) {
char buf[
CX_STREAM_COPY_BUF_SIZE];
return cx_stream_bncopy(src, dest, rfnc, wfnc,
buf,
CX_STREAM_COPY_BUF_SIZE, n);
}
#ifndef CX_SZMUL_BUILTIN
#include "szmul.c"
#endif