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 --- bin/tar2sqfs/src/process_tarball.c | 2 +- include/io/istream.h | 16 ++++++++++++++++ include/io/ostream.h | 17 ----------------- lib/io/src/istream.c | 32 ++++++++++++++++++++++++++++++++ lib/io/src/ostream.c | 33 --------------------------------- lib/io/test/stream_splice.c | 2 +- 6 files changed, 50 insertions(+), 52 deletions(-) diff --git a/bin/tar2sqfs/src/process_tarball.c b/bin/tar2sqfs/src/process_tarball.c index ef49d20..76c7dc1 100644 --- a/bin/tar2sqfs/src/process_tarball.c +++ b/bin/tar2sqfs/src/process_tarball.c @@ -28,7 +28,7 @@ static int write_file(sqfs_writer_t *sqfs, dir_iterator_t *it, } do { - ret = ostream_append_from_istream(out, in, cfg.block_size); + ret = istream_splice(in, out, cfg.block_size); } while (ret > 0); ostream_flush(out); diff --git a/include/io/istream.h b/include/io/istream.h index 9c34678..7adb204 100644 --- a/include/io/istream.h +++ b/include/io/istream.h @@ -8,6 +8,7 @@ #define IO_ISTREAM_H #include "sqfs/predef.h" +#include "io/ostream.h" /** * @struct istream_t @@ -127,6 +128,21 @@ SQFS_INLINE const char *istream_get_filename(istream_t *strm) */ SQFS_INTERNAL int istream_skip(istream_t *strm, sqfs_u64 size); +/** + * @brief Dump data from an input stream to an output stream + * + * @memberof istream_t + * + * @param in A pointer to an input stream to read from. + * @param out A pointer to an output stream to append to. + * @param size The number of bytes to copy over. + * + * @return The number of bytes copied on success, -1 on failure, + * 0 on end-of-file. + */ +SQFS_INTERNAL sqfs_s32 istream_splice(istream_t *in, ostream_t *out, + sqfs_u32 size); + #ifdef __cplusplus } #endif diff --git a/include/io/ostream.h b/include/io/ostream.h index c1602ab..9cc37ad 100644 --- a/include/io/ostream.h +++ b/include/io/ostream.h @@ -8,7 +8,6 @@ #define IO_OSTREAM_H #include "sqfs/predef.h" -#include "io/istream.h" /** * @struct ostream_t @@ -104,22 +103,6 @@ SQFS_INLINE const char *ostream_get_filename(ostream_t *strm) return strm->get_filename(strm); } -/** - * @brief Read data from an input stream and append it to an output stream - * - * @memberof ostream_t - * - * @param out A pointer to an output stream to append to. - * @param in A pointer to an input stream to read from. - * @param size The number of bytes to copy over. - * - * @return The number of bytes copied on success, -1 on failure, - * 0 on end-of-file. - */ -SQFS_INTERNAL sqfs_s32 ostream_append_from_istream(ostream_t *out, - istream_t *in, - sqfs_u32 size); - #ifdef __cplusplus } #endif 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