From 230453fc840c112bbe2c18708338fd897f1174ec Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Fri, 16 Jun 2023 15:39:36 +0200 Subject: Cleanup: libsqfs: move the generic stream API to the rest of the I/O code Signed-off-by: David Oberhollenzer --- lib/sqfs/Makemodule.am | 5 ++- lib/sqfs/src/io/stream_api.c | 100 +++++++++++++++++++++++++++++++++++++++++++ lib/sqfs/src/istream.c | 100 ------------------------------------------- 3 files changed, 103 insertions(+), 102 deletions(-) create mode 100644 lib/sqfs/src/io/stream_api.c delete mode 100644 lib/sqfs/src/istream.c diff --git a/lib/sqfs/Makemodule.am b/lib/sqfs/Makemodule.am index c722f16..170cf6b 100644 --- a/lib/sqfs/Makemodule.am +++ b/lib/sqfs/Makemodule.am @@ -30,8 +30,9 @@ libsquashfs_la_SOURCES = $(LIBSQFS_HEARDS) lib/sqfs/src/id_table.c \ lib/sqfs/src/block_processor/block_processor.c \ lib/sqfs/src/block_processor/backend.c \ lib/sqfs/src/frag_table.c lib/sqfs/src/block_writer.c \ - lib/sqfs/src/misc.c lib/sqfs/src/istream.c lib/sqfs/src/io/istream.c \ - lib/sqfs/src/io/ostream.c lib/sqfs/src/io/file.c + lib/sqfs/src/misc.c lib/sqfs/src/io/istream.c \ + lib/sqfs/src/io/ostream.c lib/sqfs/src/io/file.c \ + lib/sqfs/src/io/stream_api.c libsquashfs_la_CPPFLAGS = $(AM_CPPFLAGS) libsquashfs_la_LDFLAGS = $(AM_LDFLAGS) -version-info $(LIBSQUASHFS_SO_VERSION) libsquashfs_la_CFLAGS = $(AM_CFLAGS) $(PTHREAD_CFLAGS) $(ZLIB_CFLAGS) diff --git a/lib/sqfs/src/io/stream_api.c b/lib/sqfs/src/io/stream_api.c new file mode 100644 index 0000000..60e8472 --- /dev/null +++ b/lib/sqfs/src/io/stream_api.c @@ -0,0 +1,100 @@ +/* SPDX-License-Identifier: LGPL-3.0-or-later */ +/* + * stream_api.c + * + * Copyright (C) 2019 David Oberhollenzer + */ +#define SQFS_BUILDING_DLL +#include "config.h" + +#include "sqfs/io.h" + +#include + +sqfs_s32 sqfs_istream_read(sqfs_istream_t *strm, void *data, size_t size) +{ + sqfs_s32 total = 0; + + if (size > 0x7FFFFFFF) + size = 0x7FFFFFFF; + + while (size > 0) { + const sqfs_u8 *ptr; + size_t diff; + int ret; + + ret = strm->get_buffered_data(strm, &ptr, &diff, size); + if (ret > 0) + break; + if (ret < 0) + return ret; + + if (diff > size) + diff = size; + + memcpy(data, ptr, diff); + strm->advance_buffer(strm, diff); + data = (char *)data + diff; + size -= diff; + total += diff; + } + + return total; +} + +int sqfs_istream_skip(sqfs_istream_t *strm, sqfs_u64 size) +{ + while (size > 0) { + const sqfs_u8 *ptr; + size_t diff; + int ret; + + ret = strm->get_buffered_data(strm, &ptr, &diff, size); + if (ret < 0) + return ret; + if (ret > 0) + break; + + if ((sqfs_u64)diff > size) + diff = size; + + size -= diff; + strm->advance_buffer(strm, diff); + } + + return 0; +} + +sqfs_s32 sqfs_istream_splice(sqfs_istream_t *in, sqfs_ostream_t *out, + sqfs_u32 size) +{ + sqfs_s32 total = 0; + + if (size > 0x7FFFFFFF) + size = 0x7FFFFFFF; + + while (size > 0) { + const sqfs_u8 *ptr; + size_t diff; + int ret; + + ret = in->get_buffered_data(in, &ptr, &diff, size); + if (ret < 0) + return ret; + if (ret > 0) + break; + + if (diff > size) + diff = size; + + ret = out->append(out, ptr, diff); + if (ret) + return ret; + + total += diff; + size -= diff; + in->advance_buffer(in, diff); + } + + return total; +} diff --git a/lib/sqfs/src/istream.c b/lib/sqfs/src/istream.c deleted file mode 100644 index 3d89461..0000000 --- a/lib/sqfs/src/istream.c +++ /dev/null @@ -1,100 +0,0 @@ -/* SPDX-License-Identifier: LGPL-3.0-or-later */ -/* - * istream.c - * - * Copyright (C) 2019 David Oberhollenzer - */ -#define SQFS_BUILDING_DLL -#include "config.h" - -#include "sqfs/io.h" - -#include - -sqfs_s32 sqfs_istream_read(sqfs_istream_t *strm, void *data, size_t size) -{ - sqfs_s32 total = 0; - - if (size > 0x7FFFFFFF) - size = 0x7FFFFFFF; - - while (size > 0) { - const sqfs_u8 *ptr; - size_t diff; - int ret; - - ret = strm->get_buffered_data(strm, &ptr, &diff, size); - if (ret > 0) - break; - if (ret < 0) - return ret; - - if (diff > size) - diff = size; - - memcpy(data, ptr, diff); - strm->advance_buffer(strm, diff); - data = (char *)data + diff; - size -= diff; - total += diff; - } - - return total; -} - -int sqfs_istream_skip(sqfs_istream_t *strm, sqfs_u64 size) -{ - while (size > 0) { - const sqfs_u8 *ptr; - size_t diff; - int ret; - - ret = strm->get_buffered_data(strm, &ptr, &diff, size); - if (ret < 0) - return ret; - if (ret > 0) - break; - - if ((sqfs_u64)diff > size) - diff = size; - - size -= diff; - strm->advance_buffer(strm, diff); - } - - return 0; -} - -sqfs_s32 sqfs_istream_splice(sqfs_istream_t *in, sqfs_ostream_t *out, - sqfs_u32 size) -{ - sqfs_s32 total = 0; - - if (size > 0x7FFFFFFF) - size = 0x7FFFFFFF; - - while (size > 0) { - const sqfs_u8 *ptr; - size_t diff; - int ret; - - ret = in->get_buffered_data(in, &ptr, &diff, size); - if (ret < 0) - return ret; - if (ret > 0) - break; - - if (diff > size) - diff = size; - - ret = out->append(out, ptr, diff); - if (ret) - return ret; - - total += diff; - size -= diff; - in->advance_buffer(in, diff); - } - - return total; -} -- cgit v1.2.3