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 ++++++++++++++++++++++++++++++++ lib/io/src/ostream.c | 33 --------------------------------- lib/io/test/stream_splice.c | 2 +- 3 files changed, 33 insertions(+), 34 deletions(-) (limited to 'lib') 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; +} diff --git a/lib/io/src/ostream.c b/lib/io/src/ostream.c index da0b7b3..35f98d4 100644 --- a/lib/io/src/ostream.c +++ b/lib/io/src/ostream.c @@ -33,36 +33,3 @@ int ostream_append_sparse(ostream_t *strm, size_t size) return strm->append_sparse(strm, size); } - -sqfs_s32 ostream_append_from_istream(ostream_t *out, istream_t *in, - 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 (out->append(out, in->buffer + in->buffer_offset, diff)) - return -1; - - in->buffer_offset += diff; - size -= diff; - total += diff; - } - - return total; -} diff --git a/lib/io/test/stream_splice.c b/lib/io/test/stream_splice.c index 5379b39..9008d83 100644 --- a/lib/io/test/stream_splice.c +++ b/lib/io/test/stream_splice.c @@ -79,7 +79,7 @@ int main(int argc, char **argv) TEST_NOT_NULL(in); for (;;) { - ret = ostream_append_from_istream(&out, in, 211); + ret = istream_splice(in, &out, 211); TEST_ASSERT(ret >= 0); if (ret == 0) -- cgit v1.2.3