From 8af4ee6f415fe316894e4423235dfc4ee70d8cbb Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Sun, 4 Jun 2023 15:40:29 +0200 Subject: libio: move splice function from ostream to istream It touches internals of the istream, particularly the buffer, but not of the ostream. It really belongs to istream_t. Signed-off-by: David Oberhollenzer --- lib/io/src/istream.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'lib/io/src/istream.c') diff --git a/lib/io/src/istream.c b/lib/io/src/istream.c index 051fac8..2b89a00 100644 --- a/lib/io/src/istream.c +++ b/lib/io/src/istream.c @@ -64,3 +64,35 @@ int istream_skip(istream_t *strm, sqfs_u64 size) return 0; } + +sqfs_s32 istream_splice(istream_t *in, ostream_t *out, sqfs_u32 size) +{ + sqfs_s32 total = 0; + size_t diff; + + if (size > 0x7FFFFFFF) + size = 0x7FFFFFFF; + + while (size > 0) { + if (in->buffer_offset >= in->buffer_used) { + if (istream_precache(in)) + return -1; + + if (in->buffer_used == 0) + break; + } + + diff = in->buffer_used - in->buffer_offset; + if (diff > size) + diff = size; + + if (ostream_append(out, in->buffer + in->buffer_offset, diff)) + return -1; + + in->buffer_offset += diff; + size -= diff; + total += diff; + } + + return total; +} -- cgit v1.2.3