From ba99ef34e7b073c03519ef74f017091de6c9bee8 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Mon, 12 Jun 2023 21:21:40 +0200 Subject: Move sqfs_istream_t & sqfs_ostream_t into libsquashfs For now, only the interfaces and helper functions are moved, the concrete implementations remain in libio. Signed-off-by: David Oberhollenzer --- bin/tar2sqfs/src/process_tarball.c | 2 +- include/common.h | 2 +- include/io/file.h | 1 - include/io/istream.h | 102 ------------------------ include/io/ostream.h | 60 --------------- include/io/std.h | 1 - include/io/xfrm.h | 1 - include/sqfs/io.h | 154 ++++++++++++++++++++++++++++++++++++- include/sqfs/predef.h | 2 + include/tar/tar.h | 2 +- lib/io/Makemodule.am | 27 +++---- lib/io/src/get_line.c | 1 + lib/io/src/internal.h | 1 - lib/io/src/istream.c | 97 ----------------------- lib/io/src/mem.c | 1 + lib/io/src/unix/istream.c | 1 + lib/io/src/win32/istream.c | 1 + lib/io/src/xfrm/istream.c | 1 + lib/io/src/xfrm/ostream.c | 1 + lib/io/test/istream_mem.c | 1 + lib/io/test/istream_read.c | 105 ------------------------- lib/io/test/istream_skip.c | 94 ---------------------- lib/io/test/stream_splice.c | 98 ----------------------- lib/io/test/xfrm.c | 7 +- lib/sqfs/Makemodule.am | 15 +++- lib/sqfs/src/istream.c | 100 ++++++++++++++++++++++++ lib/sqfs/test/istream_read.c | 105 +++++++++++++++++++++++++ lib/sqfs/test/istream_skip.c | 94 ++++++++++++++++++++++ lib/sqfs/test/stream_splice.c | 96 +++++++++++++++++++++++ lib/tar/src/iterator.c | 4 +- lib/tar/src/read_header.c | 4 +- lib/tar/src/read_sparse_map_new.c | 4 +- lib/tar/src/read_sparse_map_old.c | 2 +- lib/tar/src/record_to_memory.c | 4 +- lib/tar/test/tar_fuzz.c | 2 +- lib/tar/test/tar_iterator.c | 4 +- lib/tar/test/tar_iterator2.c | 2 +- lib/tar/test/tar_iterator3.c | 8 +- lib/tar/test/tar_simple.c | 2 +- lib/tar/test/tar_target_filled.c | 8 +- lib/tar/test/tar_write_simple.c | 6 +- lib/tar/test/tar_xattr.c | 2 +- lib/tar/test/tar_xattr_bin.c | 2 +- 43 files changed, 613 insertions(+), 614 deletions(-) delete mode 100644 include/io/ostream.h delete mode 100644 lib/io/src/istream.c delete mode 100644 lib/io/test/istream_read.c delete mode 100644 lib/io/test/istream_skip.c delete mode 100644 lib/io/test/stream_splice.c create mode 100644 lib/sqfs/src/istream.c create mode 100644 lib/sqfs/test/istream_read.c create mode 100644 lib/sqfs/test/istream_skip.c create mode 100644 lib/sqfs/test/stream_splice.c diff --git a/bin/tar2sqfs/src/process_tarball.c b/bin/tar2sqfs/src/process_tarball.c index 5e7f030..639adf8 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 = istream_splice(in, out, cfg.block_size); + ret = sqfs_istream_splice(in, out, cfg.block_size); } while (ret > 0); out->flush(out); diff --git a/include/common.h b/include/common.h index c289458..d2e3f73 100644 --- a/include/common.h +++ b/include/common.h @@ -17,10 +17,10 @@ #include "sqfs/block.h" #include "sqfs/xattr.h" #include "sqfs/dir.h" +#include "sqfs/io.h" #include "simple_writer.h" #include "compress_cli.h" -#include "io/ostream.h" #include "io/file.h" #include "io/std.h" #include "compat.h" diff --git a/include/io/file.h b/include/io/file.h index 1072b6a..fa0abc0 100644 --- a/include/io/file.h +++ b/include/io/file.h @@ -8,7 +8,6 @@ #define IO_FILE_H #include "io/istream.h" -#include "io/ostream.h" #if defined(_WIN32) || defined(__WINDOWS__) #include diff --git a/include/io/istream.h b/include/io/istream.h index f4ffcb2..f41be2e 100644 --- a/include/io/istream.h +++ b/include/io/istream.h @@ -8,66 +8,6 @@ #define IO_ISTREAM_H #include "sqfs/predef.h" -#include "io/ostream.h" - -typedef struct sqfs_istream_t sqfs_istream_t; - -/** - * @interface sqfs_istream_t - * - * @extends sqfs_object_t - * - * @brief A sequential, read-only data stream. - */ -struct sqfs_istream_t { - sqfs_object_t base; - - /** - * @brief Peek into the data buffered in an istream - * - * If the internal buffer is empty, the function tries to fetch more, - * which can block. It returns a positive return code if there is no - * more data to be read, a negative error code if reading failed. Since - * this and other functions can alter the buffer pointer and contents, - * do not store the pointers returned here across function calls. - * - * Higher level functions like @ref istream_read (providing a - * Unix read() style API) are built on top of this primitive. - * - * @param strm A pointer to an sqfs_istream_t implementation. - * @param out Returns a pointer into an internal buffer on success. - * @param size Returns the number of bytes available in the buffer. - * @param want A number of bytes that the reader would like to have. - * If there is less than this available, the implementation - * can choose to do a blocking precache. - * - * @return Zero on success, a negative error code on failure, - * a postive number on EOF. - */ - int (*get_buffered_data)(sqfs_istream_t *strm, const sqfs_u8 **out, - size_t *size, size_t want); - - /** - * @brief Mark a section of the internal buffer of an istream as used - * - * This marks the first `count` bytes of the internal buffer as used, - * forcing get_buffered_data to return data afterwards and potentially - * try to load more data. - * - * @param strm A pointer to an sqfs_istream_t implementation. - * @param count The number of bytes used up. - */ - void (*advance_buffer)(sqfs_istream_t *strm, size_t count); - - /** - * @brief Get the underlying filename of an input stream. - * - * @param strm The input stream to get the filename from. - * - * @return A string holding the underlying filename. - */ - const char *(*get_filename)(sqfs_istream_t *strm); -}; enum { ISTREAM_LINE_LTRIM = 0x01, @@ -108,48 +48,6 @@ extern "C" { SQFS_INTERNAL int istream_get_line(sqfs_istream_t *strm, char **out, size_t *line_num, int flags); -/** - * @brief Read data from an input stream - * - * @memberof sqfs_istream_t - * - * @param strm A pointer to an input stream. - * @param data A buffer to read into. - * @param size The number of bytes to read into the buffer. - * - * @return The number of bytes actually read on success, -1 on failure, - * 0 on end-of-file. - */ -SQFS_INTERNAL sqfs_s32 istream_read(sqfs_istream_t *strm, - void *data, size_t size); - -/** - * @brief Skip over a number of bytes in an input stream. - * - * @memberof sqfs_istream_t - * - * @param strm A pointer to an input stream. - * @param size The number of bytes to seek forward. - * - * @return Zero on success, -1 on failure. - */ -SQFS_INTERNAL int istream_skip(sqfs_istream_t *strm, sqfs_u64 size); - -/** - * @brief Dump data from an input stream to an output stream - * - * @memberof sqfs_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(sqfs_istream_t *in, sqfs_ostream_t *out, - sqfs_u32 size); - #ifdef __cplusplus } #endif diff --git a/include/io/ostream.h b/include/io/ostream.h deleted file mode 100644 index d1781d2..0000000 --- a/include/io/ostream.h +++ /dev/null @@ -1,60 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -/* - * ostream.h - * - * Copyright (C) 2019 David Oberhollenzer - */ -#ifndef IO_OSTREAM_H -#define IO_OSTREAM_H - -#include "sqfs/predef.h" - -typedef struct sqfs_ostream_t sqfs_ostream_t; - -/** - * @interface sqfs_ostream_t - * - * @extends sqfs_object_t - * - * @brief An append-only data stream. - */ -struct sqfs_ostream_t { - sqfs_object_t base; - - /** - * @brief Append a block of data to an output stream. - * - * @param strm A pointer to an output stream. - * @param data A pointer to the data block to append. If NULL, - * synthesize a chunk of zero bytes. - * @param size The number of bytes to append. - * - * @return Zero on success, -1 on failure. - */ - int (*append)(sqfs_ostream_t *strm, const void *data, size_t size); - - /** - * @brief Process all pending, buffered data and flush it to disk. - * - * If the stream performs some kind of transformation (e.g. transparent - * data compression), flushing caues the wrapped format to insert a - * termination token. Only call this function when you are absolutely - * DONE appending data, shortly before destroying the stream. - * - * @param strm A pointer to an output stream. - * - * @return Zero on success, -1 on failure. - */ - int (*flush)(sqfs_ostream_t *strm); - - /** - * @brief Get the underlying filename of a output stream. - * - * @param strm The output stream to get the filename from. - * - * @return A string holding the underlying filename. - */ - const char *(*get_filename)(sqfs_ostream_t *strm); -}; - -#endif /* IO_OSTREAM_H */ diff --git a/include/io/std.h b/include/io/std.h index 3a8bbf8..f622491 100644 --- a/include/io/std.h +++ b/include/io/std.h @@ -8,7 +8,6 @@ #define IO_STD_H #include "io/istream.h" -#include "io/ostream.h" #ifdef __cplusplus extern "C" { diff --git a/include/io/xfrm.h b/include/io/xfrm.h index c54bc9f..f0ff1ce 100644 --- a/include/io/xfrm.h +++ b/include/io/xfrm.h @@ -8,7 +8,6 @@ #define IO_XFRM_H #include "io/istream.h" -#include "io/ostream.h" #include "xfrm/stream.h" #ifdef __cplusplus diff --git a/include/sqfs/io.h b/include/sqfs/io.h index a1fb169..402b6de 100644 --- a/include/sqfs/io.h +++ b/include/sqfs/io.h @@ -25,7 +25,12 @@ /** * @file io.h * - * @brief Contains the @ref sqfs_file_t interface for abstracting file I/O + * @brief Contains low-level interfaces for abstracting file I/O + * + * The @ref sqfs_file_t interface abstracts I/O on a random-acces sread/write + * file, @ref sqfs_ostream_t represents a buffered, sequential, append only + * data stream, @ref sqfs_istream_t represents a buffered, sequential, read only + * data stream. */ /** @@ -151,6 +156,109 @@ struct sqfs_file_t { int (*truncate)(sqfs_file_t *file, sqfs_u64 size); }; +/** + * @interface sqfs_istream_t + * + * @extends sqfs_object_t + * + * @brief A sequential, read-only data stream. + */ +struct sqfs_istream_t { + sqfs_object_t base; + + /** + * @brief Peek into the data buffered in an istream + * + * If the internal buffer is empty, the function tries to fetch more, + * which can block. It returns a positive return code if there is no + * more data to be read, a negative error code if reading failed. Since + * this and other functions can alter the buffer pointer and contents, + * do not store the pointers returned here across function calls. + * + * Higher level functions like @ref sqfs_istream_read (providing a + * Unix read() style API) are built on top of this primitive. + * + * @param strm A pointer to an sqfs_istream_t implementation. + * @param out Returns a pointer into an internal buffer on success. + * @param size Returns the number of bytes available in the buffer. + * @param want A number of bytes that the reader would like to have. + * If there is less than this available, the implementation + * can choose to do a blocking precache. + * + * @return Zero on success, a negative error code on failure, + * a postive number on EOF. + */ + int (*get_buffered_data)(sqfs_istream_t *strm, const sqfs_u8 **out, + size_t *size, size_t want); + + /** + * @brief Mark a section of the internal buffer of an istream as used + * + * This marks the first `count` bytes of the internal buffer as used, + * forcing get_buffered_data to return data afterwards and potentially + * try to load more data. + * + * @param strm A pointer to an sqfs_istream_t implementation. + * @param count The number of bytes used up. + */ + void (*advance_buffer)(sqfs_istream_t *strm, size_t count); + + /** + * @brief Get the underlying filename of an input stream. + * + * @param strm The input stream to get the filename from. + * + * @return A string holding the underlying filename. + */ + const char *(*get_filename)(sqfs_istream_t *strm); +}; + +/** + * @interface sqfs_ostream_t + * + * @extends sqfs_object_t + * + * @brief An append-only data stream. + */ +struct sqfs_ostream_t { + sqfs_object_t base; + + /** + * @brief Append a block of data to an output stream. + * + * @param strm A pointer to an output stream. + * @param data A pointer to the data block to append. If NULL, + * synthesize a chunk of zero bytes. + * @param size The number of bytes to append. + * + * @return Zero on success, -1 on failure. + */ + int (*append)(sqfs_ostream_t *strm, const void *data, size_t size); + + /** + * @brief Process all pending, buffered data and flush it to disk. + * + * If the stream performs some kind of transformation (e.g. transparent + * data compression), flushing caues the wrapped format to insert a + * termination token. Only call this function when you are absolutely + * DONE appending data, shortly before destroying the stream. + * + * @param strm A pointer to an output stream. + * + * @return Zero on success, -1 on failure. + */ + int (*flush)(sqfs_ostream_t *strm); + + /** + * @brief Get the underlying filename of a output stream. + * + * @param strm The output stream to get the filename from. + * + * @return A string holding the underlying filename. + */ + const char *(*get_filename)(sqfs_ostream_t *strm); +}; + #ifdef __cplusplus extern "C" { #endif @@ -174,6 +282,50 @@ extern "C" { */ SQFS_API sqfs_file_t *sqfs_open_file(const char *filename, sqfs_u32 flags); +/** + * @brief Read data from an input stream + * + * @memberof sqfs_istream_t + * + * This function implements a Unix-style read() function on top of + * an @ref sqfs_istream_t, taking care of buffer management internally. + * + * @param strm A pointer to an input stream. + * @param data A buffer to read into. + * @param size The number of bytes to read into the buffer. + * + * @return The number of bytes actually read on success, a + * negative @ref SQFS_ERROR code on failure, 0 on end-of-file. + */ +SQFS_API sqfs_s32 sqfs_istream_read(sqfs_istream_t *strm, + void *data, size_t size); + +/** + * @brief Skip over a number of bytes in an input stream. + * + * @memberof sqfs_istream_t + * + * @param strm A pointer to an input stream. + * @param size The number of bytes to seek forward. + * + * @return Zero on success, a negative @ref SQFS_ERROR code on failure. + */ +SQFS_API int sqfs_istream_skip(sqfs_istream_t *strm, sqfs_u64 size); + +/** + * @brief Dump data from an input stream to an output stream + * + * @memberof sqfs_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 Zero on success, a negative @ref SQFS_ERROR code on failure. + */ +SQFS_API sqfs_s32 sqfs_istream_splice(sqfs_istream_t *in, sqfs_ostream_t *out, + sqfs_u32 size); + #ifdef __cplusplus } #endif diff --git a/include/sqfs/predef.h b/include/sqfs/predef.h index b0baf92..aae95e4 100644 --- a/include/sqfs/predef.h +++ b/include/sqfs/predef.h @@ -96,6 +96,8 @@ typedef struct sqfs_block_processor_stats_t sqfs_block_processor_stats_t; typedef struct sqfs_block_processor_desc_t sqfs_block_processor_desc_t; typedef struct sqfs_readdir_state_t sqfs_readdir_state_t; typedef struct sqfs_xattr_t sqfs_xattr_t; +typedef struct sqfs_istream_t sqfs_istream_t; +typedef struct sqfs_ostream_t sqfs_ostream_t; typedef struct sqfs_fragment_t sqfs_fragment_t; typedef struct sqfs_dir_header_t sqfs_dir_header_t; diff --git a/include/tar/tar.h b/include/tar/tar.h index 60c2a3d..1f1b1b4 100644 --- a/include/tar/tar.h +++ b/include/tar/tar.h @@ -10,8 +10,8 @@ #include "config.h" #include "compat.h" #include "io/istream.h" -#include "io/ostream.h" #include "io/dir_iterator.h" +#include "sqfs/io.h" #include #include diff --git a/lib/io/Makemodule.am b/lib/io/Makemodule.am index badaa99..9e442be 100644 --- a/lib/io/Makemodule.am +++ b/lib/io/Makemodule.am @@ -1,7 +1,7 @@ -libio_a_SOURCES = include/io/istream.h include/io/ostream.h include/io/xfrm.h \ +libio_a_SOURCES = include/io/istream.h include/io/xfrm.h \ include/io/file.h include/io/std.h include/io/dir_entry.h \ include/io/dir_iterator.h include/io/mem.h lib/io/src/internal.h \ - lib/io/src/istream.c lib/io/src/get_line.c lib/io/src/xfrm/ostream.c \ + lib/io/src/get_line.c lib/io/src/xfrm/ostream.c \ lib/io/src/xfrm/istream.c lib/io/src/dir_tree_iterator.c \ lib/io/src/dir_entry.c lib/io/src/mem.c libio_a_CFLAGS = $(AM_CFLAGS) $(ZLIB_CFLAGS) $(XZ_CFLAGS) @@ -18,8 +18,7 @@ endif noinst_LIBRARIES += libio.a -LIBIO_TESTS = test_istream_mem test_get_line test_istream_read \ - test_istream_skip test_stream_splice test_dir_iterator \ +LIBIO_TESTS = test_istream_mem test_get_line test_dir_iterator \ test_dir_tree_iterator test_dir_tree_iterator2 test_dir_tree_iterator3 test_istream_mem_SOURCES = lib/io/test/istream_mem.c @@ -30,15 +29,6 @@ test_get_line_SOURCES = lib/io/test/get_line.c test_get_line_LDADD = libio.a libcompat.a test_get_line_CPPFLAGS = $(AM_CPPFLAGS) -test_istream_read_SOURCES = lib/io/test/istream_read.c -test_istream_read_LDADD = libio.a libutil.a libcompat.a - -test_istream_skip_SOURCES = lib/io/test/istream_skip.c -test_istream_skip_LDADD = libio.a libutil.a libcompat.a - -test_stream_splice_SOURCES = lib/io/test/stream_splice.c -test_stream_splice_LDADD = libio.a libutil.a libcompat.a - test_dir_iterator_SOURCES = lib/io/test/dir_iterator.c test_dir_iterator_LDADD = libio.a libutil.a libcompat.a test_dir_iterator_CPPFLAGS = $(AM_CPPFLAGS) @@ -61,7 +51,7 @@ test_dir_tree_iterator3_CPPFLAGS += -DTESTPATH=$(top_srcdir)/lib/io/test/testdir if WITH_XZ test_io_xfrm_xz_SOURCES = lib/io/test/xfrm.c -test_io_xfrm_xz_LDADD = libio.a libxfrm.a libcompat.a $(XZ_LIBS) +test_io_xfrm_xz_LDADD = libsquashfs.la libio.a libxfrm.a libcompat.a $(XZ_LIBS) test_io_xfrm_xz_CPPFLAGS = $(AM_CPPFLAGS) -DDO_XZ=1 LIBIO_TESTS += test_io_xfrm_xz @@ -69,7 +59,8 @@ endif if WITH_BZIP2 test_io_xfrm_bzip2_SOURCES = lib/io/test/xfrm.c -test_io_xfrm_bzip2_LDADD = libio.a libxfrm.a libcompat.a $(BZIP2_LIBS) +test_io_xfrm_bzip2_LDADD = libsquashfs.la libio.a libxfrm.a \ + libcompat.a $(BZIP2_LIBS) test_io_xfrm_bzip2_CPPFLAGS = $(AM_CPPFLAGS) -DDO_BZIP2=1 LIBIO_TESTS += test_io_xfrm_bzip2 @@ -77,7 +68,8 @@ endif if WITH_GZIP test_io_xfrm_gzip_SOURCES = lib/io/test/xfrm.c -test_io_xfrm_gzip_LDADD = libio.a libxfrm.a libcompat.a $(ZLIB_LIBS) +test_io_xfrm_gzip_LDADD = libsquashfs.la libio.a libxfrm.a \ + libcompat.a $(ZLIB_LIBS) test_io_xfrm_gzip_CPPFLAGS = $(AM_CPPFLAGS) -DDO_GZIP=1 LIBIO_TESTS += test_io_xfrm_gzip @@ -86,7 +78,8 @@ endif if WITH_ZSTD if HAVE_ZSTD_STREAM test_io_xfrm_zstd_SOURCES = lib/io/test/xfrm.c -test_io_xfrm_zstd_LDADD = libio.a libxfrm.a libcompat.a $(ZSTD_LIBS) +test_io_xfrm_zstd_LDADD = libsquashfs.la libio.a libxfrm.a \ + libcompat.a $(ZSTD_LIBS) test_io_xfrm_zstd_CPPFLAGS = $(AM_CPPFLAGS) -DDO_ZSTD=1 LIBIO_TESTS += test_io_xfrm_zstd diff --git a/lib/io/src/get_line.c b/lib/io/src/get_line.c index 9ab4928..ad37be6 100644 --- a/lib/io/src/get_line.c +++ b/lib/io/src/get_line.c @@ -5,6 +5,7 @@ * Copyright (C) 2019 David Oberhollenzer */ #include "internal.h" +#include "sqfs/io.h" static void ltrim(char *buffer) { diff --git a/lib/io/src/internal.h b/lib/io/src/internal.h index 1e51012..e010727 100644 --- a/lib/io/src/internal.h +++ b/lib/io/src/internal.h @@ -10,7 +10,6 @@ #include "config.h" #include "compat.h" #include "io/istream.h" -#include "io/ostream.h" #include "io/file.h" #include "io/xfrm.h" #include "io/std.h" diff --git a/lib/io/src/istream.c b/lib/io/src/istream.c deleted file mode 100644 index 6fbc67b..0000000 --- a/lib/io/src/istream.c +++ /dev/null @@ -1,97 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -/* - * istream.c - * - * Copyright (C) 2019 David Oberhollenzer - */ -#include "internal.h" - - -sqfs_s32 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 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) { - fprintf(stderr, "%s: unexpected end-of-file\n", - strm->get_filename(strm)); - return -1; - } - - if ((sqfs_u64)diff > size) - diff = size; - - size -= diff; - strm->advance_buffer(strm, diff); - } - - return 0; -} - -sqfs_s32 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; - - if (out->append(out, ptr, diff)) - return -1; - - total += diff; - size -= diff; - in->advance_buffer(in, diff); - } - - return total; -} diff --git a/lib/io/src/mem.c b/lib/io/src/mem.c index 435a169..7150d1f 100644 --- a/lib/io/src/mem.c +++ b/lib/io/src/mem.c @@ -7,6 +7,7 @@ #include "config.h" #include "io/mem.h" #include "compat.h" +#include "sqfs/io.h" #include #include diff --git a/lib/io/src/unix/istream.c b/lib/io/src/unix/istream.c index fc76bab..39d570f 100644 --- a/lib/io/src/unix/istream.c +++ b/lib/io/src/unix/istream.c @@ -5,6 +5,7 @@ * Copyright (C) 2019 David Oberhollenzer */ #include "../internal.h" +#include "sqfs/io.h" typedef struct { sqfs_istream_t base; diff --git a/lib/io/src/win32/istream.c b/lib/io/src/win32/istream.c index 223b20d..e6cb266 100644 --- a/lib/io/src/win32/istream.c +++ b/lib/io/src/win32/istream.c @@ -5,6 +5,7 @@ * Copyright (C) 2019 David Oberhollenzer */ #include "../internal.h" +#include "sqfs/io.h" #define WIN32_LEAN_AND_MEAN #include diff --git a/lib/io/src/xfrm/istream.c b/lib/io/src/xfrm/istream.c index 5c23d28..c499f6c 100644 --- a/lib/io/src/xfrm/istream.c +++ b/lib/io/src/xfrm/istream.c @@ -5,6 +5,7 @@ * Copyright (C) 2019 David Oberhollenzer */ #include "../internal.h" +#include "sqfs/io.h" typedef struct istream_xfrm_t { sqfs_istream_t base; diff --git a/lib/io/src/xfrm/ostream.c b/lib/io/src/xfrm/ostream.c index 81c19dd..4c77f42 100644 --- a/lib/io/src/xfrm/ostream.c +++ b/lib/io/src/xfrm/ostream.c @@ -5,6 +5,7 @@ * Copyright (C) 2019 David Oberhollenzer */ #include "../internal.h" +#include "sqfs/io.h" typedef struct ostream_xfrm_t { sqfs_ostream_t base; diff --git a/lib/io/test/istream_mem.c b/lib/io/test/istream_mem.c index f364fd3..d5e0c2c 100644 --- a/lib/io/test/istream_mem.c +++ b/lib/io/test/istream_mem.c @@ -8,6 +8,7 @@ #include "util/test.h" #include "io/mem.h" +#include "sqfs/io.h" static const size_t end0 = 449; /* region 1: filled with 'A' */ static const size_t end1 = 521; /* region 2: filled with 'B' */ diff --git a/lib/io/test/istream_read.c b/lib/io/test/istream_read.c deleted file mode 100644 index 66fec4b..0000000 --- a/lib/io/test/istream_read.c +++ /dev/null @@ -1,105 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -/* - * istream_read.c - * - * Copyright (C) 2023 David Oberhollenzer - */ -#include "config.h" - -#include "io/istream.h" -#include "util/test.h" -#include "io/mem.h" - -static const sqfs_u64 end0 = 449; /* region 1: filled with 'A' */ -static const sqfs_u64 end1 = 521; /* region 2: filled with 'B' */ -static const sqfs_u64 end2 = 941; /* region 3: filled with 'C' */ - -static sqfs_u8 rd_buffer[941]; - -static sqfs_u8 byte_at_offset(sqfs_u64 off) -{ - if (off < end0) - return 'A'; - if (off < end1) - return 'B'; - return 'C'; -} - -static void init_rd_buffer(void) -{ - for (size_t i = 0; i < end2; ++i) - rd_buffer[i] = byte_at_offset(i); -} - -int main(int argc, char **argv) -{ - sqfs_u8 read_buffer[61]; - sqfs_u64 read_off = 0; - sqfs_istream_t *dummy; - (void)argc; (void)argv; - - init_rd_buffer(); - dummy = istream_memory_create("dummy file", 103, - rd_buffer, sizeof(rd_buffer)); - TEST_NOT_NULL(dummy); - - /* region 1 */ - while (read_off < end0) { - size_t read_diff = end0 - read_off; - - if (read_diff > sizeof(read_buffer)) - read_diff = sizeof(read_buffer); - - int ret = istream_read(dummy, read_buffer, read_diff); - TEST_ASSERT(ret > 0); - TEST_ASSERT((size_t)ret <= read_diff); - - for (int i = 0; i < ret; ++i) { - TEST_EQUAL_UI(read_buffer[i], 'A'); - } - - read_off += ret; - } - - /* region 2 */ - while (read_off < end1) { - size_t read_diff = end1 - read_off; - - if (read_diff > sizeof(read_buffer)) - read_diff = sizeof(read_buffer); - - int ret = istream_read(dummy, read_buffer, read_diff); - TEST_ASSERT(ret > 0); - TEST_ASSERT((size_t)ret <= read_diff); - - for (int i = 0; i < ret; ++i) { - TEST_EQUAL_UI(read_buffer[i], 'B'); - } - - read_off += ret; - } - - /* region 3 */ - for (;;) { - size_t read_diff = sizeof(read_buffer); - - int ret = istream_read(dummy, read_buffer, read_diff); - TEST_ASSERT(ret >= 0); - TEST_ASSERT((size_t)ret <= read_diff); - - if (ret == 0) { - TEST_EQUAL_UI(read_off, end2); - break; - } - - for (int i = 0; i < ret; ++i) { - TEST_EQUAL_UI(read_buffer[i], 'C'); - } - - read_off += ret; - TEST_ASSERT(read_off <= end2); - } - - sqfs_drop(dummy); - return EXIT_SUCCESS; -} diff --git a/lib/io/test/istream_skip.c b/lib/io/test/istream_skip.c deleted file mode 100644 index 0fc3e11..0000000 --- a/lib/io/test/istream_skip.c +++ /dev/null @@ -1,94 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -/* - * istream_skip.c - * - * Copyright (C) 2023 David Oberhollenzer - */ -#include "config.h" - -#include "io/istream.h" -#include "util/test.h" -#include "io/mem.h" - -static const sqfs_u64 end0 = 449; /* region 1: filled with 'A' */ -static const sqfs_u64 end1 = 521; /* region 2: filled with 'B' */ -static const sqfs_u64 end2 = 941; /* region 3: filled with 'C' */ - -static sqfs_u8 rd_buffer[941]; - -static sqfs_u8 byte_at_offset(sqfs_u64 off) -{ - if (off < end0) - return 'A'; - if (off < end1) - return 'B'; - return 'C'; -} - -static void init_rd_buffer(void) -{ - for (size_t i = 0; i < end2; ++i) - rd_buffer[i] = byte_at_offset(i); -} - -int main(int argc, char **argv) -{ - sqfs_u8 read_buffer[61]; - sqfs_u64 read_off = 0; - sqfs_istream_t *dummy; - (void)argc; (void)argv; - - init_rd_buffer(); - dummy = istream_memory_create("dummy file", 103, - rd_buffer, sizeof(rd_buffer)); - TEST_NOT_NULL(dummy); - - /* region 1 */ - while (read_off < end0) { - size_t read_diff = end0 - read_off; - - if (read_diff > sizeof(read_buffer)) - read_diff = sizeof(read_buffer); - - int ret = istream_read(dummy, read_buffer, read_diff); - TEST_ASSERT(ret > 0); - TEST_ASSERT((size_t)ret <= read_diff); - - for (int i = 0; i < ret; ++i) { - TEST_EQUAL_UI(read_buffer[i], 'A'); - } - - read_off += ret; - } - - /* region 2 */ - { - int ret = istream_skip(dummy, end2 - end1); - TEST_EQUAL_I(ret, 0); - read_off += (end2 - end1); - } - - /* region 3 */ - for (;;) { - size_t read_diff = sizeof(read_buffer); - - int ret = istream_read(dummy, read_buffer, read_diff); - TEST_ASSERT(ret >= 0); - TEST_ASSERT((size_t)ret <= read_diff); - - if (ret == 0) { - TEST_EQUAL_UI(read_off, end2); - break; - } - - for (int i = 0; i < ret; ++i) { - TEST_EQUAL_UI(read_buffer[i], 'C'); - } - - read_off += ret; - TEST_ASSERT(read_off <= end2); - } - - sqfs_drop(dummy); - return EXIT_SUCCESS; -} diff --git a/lib/io/test/stream_splice.c b/lib/io/test/stream_splice.c deleted file mode 100644 index a9edd76..0000000 --- a/lib/io/test/stream_splice.c +++ /dev/null @@ -1,98 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -/* - * stream_splice.c - * - * Copyright (C) 2023 David Oberhollenzer - */ -#include "config.h" - -#include "io/istream.h" -#include "io/ostream.h" -#include "io/mem.h" -#include "util/test.h" - -static const sqfs_u64 end0 = 449; /* region 1: filled with 'A' */ -static const sqfs_u64 end1 = 521; /* region 2: filled with 'B' */ -static const sqfs_u64 end2 = 941; /* region 3: filled with 'C' */ - -static sqfs_u8 rd_buffer[941]; - -static sqfs_u8 byte_at_offset(sqfs_u64 off) -{ - if (off < end0) - return 'A'; - if (off < end1) - return 'B'; - return 'C'; -} - -static void init_rd_buffer(void) -{ - for (size_t i = 0; i < end2; ++i) - rd_buffer[i] = byte_at_offset(i); -} - -/*****************************************************************************/ - -static int out_append(sqfs_ostream_t *strm, const void *data, size_t size); - -static sqfs_u64 out_offset = 0; - -static sqfs_ostream_t out = { - { 1, NULL, NULL }, - out_append, - NULL, - NULL, - NULL, -}; - -static int out_append(sqfs_ostream_t *strm, const void *data, size_t size) -{ - const sqfs_u8 *ptr = data; - - TEST_ASSERT(strm == &out); - TEST_ASSERT(size > 0); - - while (size--) { - sqfs_u8 x = *(ptr++); - sqfs_u8 y = byte_at_offset(out_offset++); - - TEST_EQUAL_UI(x, y); - TEST_ASSERT(out_offset <= end2); - } - - return 0; -} - -/*****************************************************************************/ - -int main(int argc, char **argv) -{ - sqfs_u64 total = 0; - sqfs_istream_t *in; - sqfs_s32 ret; - (void)argc; (void)argv; - - init_rd_buffer(); - in = istream_memory_create("memory_in", 109, - rd_buffer, sizeof(rd_buffer)); - TEST_NOT_NULL(in); - - for (;;) { - ret = istream_splice(in, &out, 211); - TEST_ASSERT(ret >= 0); - - if (ret == 0) - break; - - total += ret; - TEST_ASSERT(total <= end2); - TEST_ASSERT(out_offset <= end2); - TEST_EQUAL_UI(total, out_offset); - } - - TEST_EQUAL_UI(total, end2); - TEST_EQUAL_UI(out_offset, end2); - sqfs_drop(in); - return EXIT_SUCCESS; -} diff --git a/lib/io/test/xfrm.c b/lib/io/test/xfrm.c index 1b48e6f..b871610 100644 --- a/lib/io/test/xfrm.c +++ b/lib/io/test/xfrm.c @@ -7,6 +7,7 @@ #include "xfrm/compress.h" #include "xfrm/stream.h" #include "util/test.h" +#include "sqfs/io.h" #include "io/xfrm.h" #include "io/mem.h" @@ -430,15 +431,15 @@ static void run_unpack_test(const void *blob, size_t size) TEST_EQUAL_UI(((sqfs_object_t *)mem_istream)->refcount, 2); for (i = 0; i < (sizeof(orig) - 1); ++i) { - ret = istream_read(istream, &c, 1); + ret = sqfs_istream_read(istream, &c, 1); TEST_EQUAL_I(ret, 1); TEST_EQUAL_I(c, orig[i]); } - ret = istream_read(istream, &c, 1); + ret = sqfs_istream_read(istream, &c, 1); TEST_EQUAL_I(ret, 0); - ret = istream_read(istream, &c, 1); + ret = sqfs_istream_read(istream, &c, 1); TEST_EQUAL_I(ret, 0); sqfs_drop(istream); diff --git a/lib/sqfs/Makemodule.am b/lib/sqfs/Makemodule.am index d54134c..2727b11 100644 --- a/lib/sqfs/Makemodule.am +++ b/lib/sqfs/Makemodule.am @@ -30,7 +30,7 @@ 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/misc.c lib/sqfs/src/istream.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) @@ -122,9 +122,18 @@ xattr_benchmark_LDADD = libcommon.a libsquashfs.la libcompat.a test_get_node_path_SOURCES = lib/sqfs/test/get_node_path.c test_get_node_path_LDADD = libcommon.a libsquashfs.la libcompat.a -LIBSQFS_TESTS = \ - test_abi test_xattr test_table test_xattr_writer test_get_node_path +test_istream_read_SOURCES = lib/sqfs/test/istream_read.c +test_istream_read_LDADD = libio.a libsquashfs.la libutil.a libcompat.a + +test_istream_skip_SOURCES = lib/sqfs/test/istream_skip.c +test_istream_skip_LDADD = libsquashfs.la libio.a libutil.a libcompat.a +test_stream_splice_SOURCES = lib/sqfs/test/stream_splice.c +test_stream_splice_LDADD = libsquashfs.la libio.a libutil.a libcompat.a + +LIBSQFS_TESTS = \ + test_abi test_xattr test_table test_xattr_writer test_get_node_path \ + test_istream_read test_istream_skip test_stream_splice noinst_PROGRAMS += xattr_benchmark check_PROGRAMS += $(LIBSQFS_TESTS) diff --git a/lib/sqfs/src/istream.c b/lib/sqfs/src/istream.c new file mode 100644 index 0000000..3d89461 --- /dev/null +++ b/lib/sqfs/src/istream.c @@ -0,0 +1,100 @@ +/* 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; +} diff --git a/lib/sqfs/test/istream_read.c b/lib/sqfs/test/istream_read.c new file mode 100644 index 0000000..f8facea --- /dev/null +++ b/lib/sqfs/test/istream_read.c @@ -0,0 +1,105 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * istream_read.c + * + * Copyright (C) 2023 David Oberhollenzer + */ +#include "config.h" + +#include "util/test.h" +#include "sqfs/io.h" +#include "io/mem.h" + +static const sqfs_u64 end0 = 449; /* region 1: filled with 'A' */ +static const sqfs_u64 end1 = 521; /* region 2: filled with 'B' */ +static const sqfs_u64 end2 = 941; /* region 3: filled with 'C' */ + +static sqfs_u8 rd_buffer[941]; + +static sqfs_u8 byte_at_offset(sqfs_u64 off) +{ + if (off < end0) + return 'A'; + if (off < end1) + return 'B'; + return 'C'; +} + +static void init_rd_buffer(void) +{ + for (size_t i = 0; i < end2; ++i) + rd_buffer[i] = byte_at_offset(i); +} + +int main(int argc, char **argv) +{ + sqfs_u8 read_buffer[61]; + sqfs_u64 read_off = 0; + sqfs_istream_t *dummy; + (void)argc; (void)argv; + + init_rd_buffer(); + dummy = istream_memory_create("dummy file", 103, + rd_buffer, sizeof(rd_buffer)); + TEST_NOT_NULL(dummy); + + /* region 1 */ + while (read_off < end0) { + size_t read_diff = end0 - read_off; + + if (read_diff > sizeof(read_buffer)) + read_diff = sizeof(read_buffer); + + int ret = sqfs_istream_read(dummy, read_buffer, read_diff); + TEST_ASSERT(ret > 0); + TEST_ASSERT((size_t)ret <= read_diff); + + for (int i = 0; i < ret; ++i) { + TEST_EQUAL_UI(read_buffer[i], 'A'); + } + + read_off += ret; + } + + /* region 2 */ + while (read_off < end1) { + size_t read_diff = end1 - read_off; + + if (read_diff > sizeof(read_buffer)) + read_diff = sizeof(read_buffer); + + int ret = sqfs_istream_read(dummy, read_buffer, read_diff); + TEST_ASSERT(ret > 0); + TEST_ASSERT((size_t)ret <= read_diff); + + for (int i = 0; i < ret; ++i) { + TEST_EQUAL_UI(read_buffer[i], 'B'); + } + + read_off += ret; + } + + /* region 3 */ + for (;;) { + size_t read_diff = sizeof(read_buffer); + + int ret = sqfs_istream_read(dummy, read_buffer, read_diff); + TEST_ASSERT(ret >= 0); + TEST_ASSERT((size_t)ret <= read_diff); + + if (ret == 0) { + TEST_EQUAL_UI(read_off, end2); + break; + } + + for (int i = 0; i < ret; ++i) { + TEST_EQUAL_UI(read_buffer[i], 'C'); + } + + read_off += ret; + TEST_ASSERT(read_off <= end2); + } + + sqfs_drop(dummy); + return EXIT_SUCCESS; +} diff --git a/lib/sqfs/test/istream_skip.c b/lib/sqfs/test/istream_skip.c new file mode 100644 index 0000000..d8a81f2 --- /dev/null +++ b/lib/sqfs/test/istream_skip.c @@ -0,0 +1,94 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * istream_skip.c + * + * Copyright (C) 2023 David Oberhollenzer + */ +#include "config.h" + +#include "sqfs/io.h" +#include "util/test.h" +#include "io/mem.h" + +static const sqfs_u64 end0 = 449; /* region 1: filled with 'A' */ +static const sqfs_u64 end1 = 521; /* region 2: filled with 'B' */ +static const sqfs_u64 end2 = 941; /* region 3: filled with 'C' */ + +static sqfs_u8 rd_buffer[941]; + +static sqfs_u8 byte_at_offset(sqfs_u64 off) +{ + if (off < end0) + return 'A'; + if (off < end1) + return 'B'; + return 'C'; +} + +static void init_rd_buffer(void) +{ + for (size_t i = 0; i < end2; ++i) + rd_buffer[i] = byte_at_offset(i); +} + +int main(int argc, char **argv) +{ + sqfs_u8 read_buffer[61]; + sqfs_u64 read_off = 0; + sqfs_istream_t *dummy; + (void)argc; (void)argv; + + init_rd_buffer(); + dummy = istream_memory_create("dummy file", 103, + rd_buffer, sizeof(rd_buffer)); + TEST_NOT_NULL(dummy); + + /* region 1 */ + while (read_off < end0) { + size_t read_diff = end0 - read_off; + + if (read_diff > sizeof(read_buffer)) + read_diff = sizeof(read_buffer); + + int ret = sqfs_istream_read(dummy, read_buffer, read_diff); + TEST_ASSERT(ret > 0); + TEST_ASSERT((size_t)ret <= read_diff); + + for (int i = 0; i < ret; ++i) { + TEST_EQUAL_UI(read_buffer[i], 'A'); + } + + read_off += ret; + } + + /* region 2 */ + { + int ret = sqfs_istream_skip(dummy, end2 - end1); + TEST_EQUAL_I(ret, 0); + read_off += (end2 - end1); + } + + /* region 3 */ + for (;;) { + size_t read_diff = sizeof(read_buffer); + + int ret = sqfs_istream_read(dummy, read_buffer, read_diff); + TEST_ASSERT(ret >= 0); + TEST_ASSERT((size_t)ret <= read_diff); + + if (ret == 0) { + TEST_EQUAL_UI(read_off, end2); + break; + } + + for (int i = 0; i < ret; ++i) { + TEST_EQUAL_UI(read_buffer[i], 'C'); + } + + read_off += ret; + TEST_ASSERT(read_off <= end2); + } + + sqfs_drop(dummy); + return EXIT_SUCCESS; +} diff --git a/lib/sqfs/test/stream_splice.c b/lib/sqfs/test/stream_splice.c new file mode 100644 index 0000000..6f40d1f --- /dev/null +++ b/lib/sqfs/test/stream_splice.c @@ -0,0 +1,96 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * stream_splice.c + * + * Copyright (C) 2023 David Oberhollenzer + */ +#include "config.h" + +#include "io/mem.h" +#include "util/test.h" +#include "sqfs/io.h" + +static const sqfs_u64 end0 = 449; /* region 1: filled with 'A' */ +static const sqfs_u64 end1 = 521; /* region 2: filled with 'B' */ +static const sqfs_u64 end2 = 941; /* region 3: filled with 'C' */ + +static sqfs_u8 rd_buffer[941]; + +static sqfs_u8 byte_at_offset(sqfs_u64 off) +{ + if (off < end0) + return 'A'; + if (off < end1) + return 'B'; + return 'C'; +} + +static void init_rd_buffer(void) +{ + for (size_t i = 0; i < end2; ++i) + rd_buffer[i] = byte_at_offset(i); +} + +/*****************************************************************************/ + +static int out_append(sqfs_ostream_t *strm, const void *data, size_t size); + +static sqfs_u64 out_offset = 0; + +static sqfs_ostream_t out = { + { 1, NULL, NULL }, + out_append, + NULL, + NULL, +}; + +static int out_append(sqfs_ostream_t *strm, const void *data, size_t size) +{ + const sqfs_u8 *ptr = data; + + TEST_ASSERT(strm == &out); + TEST_ASSERT(size > 0); + + while (size--) { + sqfs_u8 x = *(ptr++); + sqfs_u8 y = byte_at_offset(out_offset++); + + TEST_EQUAL_UI(x, y); + TEST_ASSERT(out_offset <= end2); + } + + return 0; +} + +/*****************************************************************************/ + +int main(int argc, char **argv) +{ + sqfs_u64 total = 0; + sqfs_istream_t *in; + sqfs_s32 ret; + (void)argc; (void)argv; + + init_rd_buffer(); + in = istream_memory_create("memory_in", 109, + rd_buffer, sizeof(rd_buffer)); + TEST_NOT_NULL(in); + + for (;;) { + ret = sqfs_istream_splice(in, &out, 211); + TEST_ASSERT(ret >= 0); + + if (ret == 0) + break; + + total += ret; + TEST_ASSERT(total <= end2); + TEST_ASSERT(out_offset <= end2); + TEST_EQUAL_UI(total, out_offset); + } + + TEST_EQUAL_UI(total, end2); + TEST_EQUAL_UI(out_offset, end2); + sqfs_drop(in); + return EXIT_SUCCESS; +} diff --git a/lib/tar/src/iterator.c b/lib/tar/src/iterator.c index 5920b46..93c931c 100644 --- a/lib/tar/src/iterator.c +++ b/lib/tar/src/iterator.c @@ -177,13 +177,13 @@ static int it_next(dir_iterator_t *it, dir_entry_t **out) return tar->state; retry: if (tar->record_size > 0) { - ret = istream_skip(tar->stream, tar->record_size); + ret = sqfs_istream_skip(tar->stream, tar->record_size); if (ret) goto fail; } if (tar->padding > 0) { - ret = istream_skip(tar->stream, tar->padding); + ret = sqfs_istream_skip(tar->stream, tar->padding); if (ret) goto fail; } diff --git a/lib/tar/src/read_header.c b/lib/tar/src/read_header.c index 3117d8a..16fc9d7 100644 --- a/lib/tar/src/read_header.c +++ b/lib/tar/src/read_header.c @@ -175,7 +175,7 @@ int read_header(sqfs_istream_t *fp, tar_header_decoded_t *out) memset(out, 0, sizeof(*out)); for (;;) { - ret = istream_read(fp, &hdr, sizeof(hdr)); + ret = sqfs_istream_read(fp, &hdr, sizeof(hdr)); if (ret < 0) goto fail; @@ -226,7 +226,7 @@ int read_header(sqfs_istream_t *fp, tar_header_decoded_t *out) goto fail; if (pax_size % 512) pax_size += 512 - (pax_size % 512); - istream_skip(fp, pax_size); + sqfs_istream_skip(fp, pax_size); continue; case TAR_TYPE_PAX: clear_header(out); diff --git a/lib/tar/src/read_sparse_map_new.c b/lib/tar/src/read_sparse_map_new.c index a1f37fd..4e317a8 100644 --- a/lib/tar/src/read_sparse_map_new.c +++ b/lib/tar/src/read_sparse_map_new.c @@ -41,7 +41,7 @@ sparse_map_t *read_gnu_new_sparse(sqfs_istream_t *fp, tar_header_decoded_t *out) if (out->record_size < 512) goto fail_format; - ret = istream_read(fp, buffer, 512); + ret = sqfs_istream_read(fp, buffer, 512); if (ret < 0) goto fail; @@ -68,7 +68,7 @@ sparse_map_t *read_gnu_new_sparse(sqfs_istream_t *fp, tar_header_decoded_t *out) if (out->record_size < 512) goto fail_format; - ret = istream_read(fp, buffer + 512, 512); + ret = sqfs_istream_read(fp, buffer + 512, 512); if (ret < 0) goto fail; diff --git a/lib/tar/src/read_sparse_map_old.c b/lib/tar/src/read_sparse_map_old.c index 832329b..1794073 100644 --- a/lib/tar/src/read_sparse_map_old.c +++ b/lib/tar/src/read_sparse_map_old.c @@ -59,7 +59,7 @@ sparse_map_t *read_gnu_old_sparse(sqfs_istream_t *fp, tar_header_t *hdr) return list; do { - ret = istream_read(fp, &sph, sizeof(sph)); + ret = sqfs_istream_read(fp, &sph, sizeof(sph)); if (ret < 0) goto fail; diff --git a/lib/tar/src/record_to_memory.c b/lib/tar/src/record_to_memory.c index 1bd31aa..597d6f8 100644 --- a/lib/tar/src/record_to_memory.c +++ b/lib/tar/src/record_to_memory.c @@ -18,7 +18,7 @@ char *record_to_memory(sqfs_istream_t *fp, size_t size) if (buffer == NULL) goto fail_errno; - ret = istream_read(fp, buffer, size); + ret = sqfs_istream_read(fp, buffer, size); if (ret < 0) goto fail; @@ -28,7 +28,7 @@ char *record_to_memory(sqfs_istream_t *fp, size_t size) } if (size % 512) { - if (istream_skip(fp, 512 - (size % 512))) + if (sqfs_istream_skip(fp, 512 - (size % 512))) goto fail; } diff --git a/lib/tar/test/tar_fuzz.c b/lib/tar/test/tar_fuzz.c index bdea98e..92c0952 100644 --- a/lib/tar/test/tar_fuzz.c +++ b/lib/tar/test/tar_fuzz.c @@ -34,7 +34,7 @@ int main(int argc, char **argv) if (ret < 0) goto fail; - ret = istream_skip(fp, hdr.record_size); + ret = sqfs_istream_skip(fp, hdr.record_size); clear_header(&hdr); if (ret < 0) diff --git a/lib/tar/test/tar_iterator.c b/lib/tar/test/tar_iterator.c index 25e1389..f51ecd6 100644 --- a/lib/tar/test/tar_iterator.c +++ b/lib/tar/test/tar_iterator.c @@ -83,12 +83,12 @@ int main(int argc, char **argv) TEST_EQUAL_UI(((sqfs_object_t *)ti)->refcount, 1); /* read the data from the stream */ - ret = istream_read(ti, buffer, sizeof(buffer)); + ret = sqfs_istream_read(ti, buffer, sizeof(buffer)); TEST_EQUAL_I(ret, 5); buffer[5] = '\0'; TEST_STR_EQUAL(buffer, "test\n"); - ret = istream_read(ti, buffer, sizeof(buffer)); + ret = sqfs_istream_read(ti, buffer, sizeof(buffer)); TEST_EQUAL_I(ret, 0); sqfs_drop(ti); diff --git a/lib/tar/test/tar_iterator2.c b/lib/tar/test/tar_iterator2.c index 6f472ae..7483e81 100644 --- a/lib/tar/test/tar_iterator2.c +++ b/lib/tar/test/tar_iterator2.c @@ -76,7 +76,7 @@ int main(int argc, char **argv) offset = 0; for (;;) { - ret = istream_read(ti, buffer, sizeof(buffer)); + ret = sqfs_istream_read(ti, buffer, sizeof(buffer)); TEST_ASSERT(ret >= 0); if (ret == 0) diff --git a/lib/tar/test/tar_iterator3.c b/lib/tar/test/tar_iterator3.c index 39e7b42..11c2fd2 100644 --- a/lib/tar/test/tar_iterator3.c +++ b/lib/tar/test/tar_iterator3.c @@ -86,10 +86,10 @@ int main(int argc, char **argv) TEST_EQUAL_I(ret, 0); TEST_NOT_NULL(ti); - TEST_ASSERT(istream_read(ti, buffer, sizeof(buffer)) == 5); + TEST_ASSERT(sqfs_istream_read(ti, buffer, sizeof(buffer)) == 5); buffer[5] = '\0'; TEST_STR_EQUAL(buffer, "test\n"); - TEST_ASSERT(istream_read(ti, buffer, sizeof(buffer)) == 0); + TEST_ASSERT(sqfs_istream_read(ti, buffer, sizeof(buffer)) == 0); ti = sqfs_drop(ti); ret = it->next(it, &ent); @@ -105,10 +105,10 @@ int main(int argc, char **argv) TEST_EQUAL_I(ret, 0); TEST_NOT_NULL(ti); - TEST_ASSERT(istream_read(ti, buffer, sizeof(buffer)) == 5); + TEST_ASSERT(sqfs_istream_read(ti, buffer, sizeof(buffer)) == 5); buffer[5] = '\0'; TEST_STR_EQUAL(buffer, "test\n"); - TEST_ASSERT(istream_read(ti, buffer, sizeof(buffer)) == 0); + TEST_ASSERT(sqfs_istream_read(ti, buffer, sizeof(buffer)) == 0); ti = sqfs_drop(ti); /* "deep" directory hierarchy containg a hard link */ diff --git a/lib/tar/test/tar_simple.c b/lib/tar/test/tar_simple.c index 656e59b..a666eb5 100644 --- a/lib/tar/test/tar_simple.c +++ b/lib/tar/test/tar_simple.c @@ -55,7 +55,7 @@ int main(int argc, char **argv) TEST_STR_EQUAL(hdr.name, fname); TEST_ASSERT(!hdr.unknown_record); - TEST_ASSERT(istream_read(fp, buffer, 5) == 5); + TEST_ASSERT(sqfs_istream_read(fp, buffer, 5) == 5); buffer[5] = '\0'; TEST_STR_EQUAL(buffer, "test\n"); clear_header(&hdr); diff --git a/lib/tar/test/tar_target_filled.c b/lib/tar/test/tar_target_filled.c index 815728a..4f061c0 100644 --- a/lib/tar/test/tar_target_filled.c +++ b/lib/tar/test/tar_target_filled.c @@ -50,10 +50,10 @@ int main(int argc, char **argv) "20_characters_here03/20_characters_here04/" "errored_file_tst"); TEST_EQUAL_UI(hdr.actual_size, 5); - TEST_ASSERT(istream_read(fp, buffer, 5) == 5); + TEST_ASSERT(sqfs_istream_read(fp, buffer, 5) == 5); buffer[5] = '\0'; TEST_STR_EQUAL(buffer, "test\n"); - TEST_ASSERT(istream_skip(fp, 512 - 5) == 0); + TEST_ASSERT(sqfs_istream_skip(fp, 512 - 5) == 0); clear_header(&hdr); TEST_ASSERT(read_header(fp, &hdr) == 0); @@ -62,10 +62,10 @@ int main(int argc, char **argv) "20_characters_here03/20_characters_here04/" "some_test_file"); TEST_EQUAL_UI(hdr.actual_size, 5); - TEST_ASSERT(istream_read(fp, buffer, 5) == 5); + TEST_ASSERT(sqfs_istream_read(fp, buffer, 5) == 5); buffer[5] = '\0'; TEST_STR_EQUAL(buffer, "test\n"); - TEST_ASSERT(istream_skip(fp, 512 - 5) == 0); + TEST_ASSERT(sqfs_istream_skip(fp, 512 - 5) == 0); clear_header(&hdr); /* "deep" directory hierarchy containg a hard link */ diff --git a/lib/tar/test/tar_write_simple.c b/lib/tar/test/tar_write_simple.c index 98ac25b..ca8d1e5 100644 --- a/lib/tar/test/tar_write_simple.c +++ b/lib/tar/test/tar_write_simple.c @@ -6,7 +6,7 @@ */ #include "config.h" #include "tar/tar.h" -#include "io/ostream.h" +#include "sqfs/io.h" #include "io/file.h" #include "util/test.h" #include "sqfs/xattr.h" @@ -191,11 +191,11 @@ int main(int argc, char **argv) fp = istream_open_file(STRVALUE(TESTPATH) "/" STRVALUE(TESTFILE)); TEST_NOT_NULL(fp); - ret = istream_read(fp, rd_buffer, sizeof(rd_buffer)); + ret = sqfs_istream_read(fp, rd_buffer, sizeof(rd_buffer)); TEST_ASSERT(ret > 0); TEST_EQUAL_UI(ret, sizeof(rd_buffer)); - ret = istream_read(fp, rd_buffer, sizeof(rd_buffer)); + ret = sqfs_istream_read(fp, rd_buffer, sizeof(rd_buffer)); TEST_EQUAL_I(ret, 0); sqfs_drop(fp); diff --git a/lib/tar/test/tar_xattr.c b/lib/tar/test/tar_xattr.c index 657b73d..26a2cd0 100644 --- a/lib/tar/test/tar_xattr.c +++ b/lib/tar/test/tar_xattr.c @@ -27,7 +27,7 @@ int main(int argc, char **argv) TEST_EQUAL_UI(hdr.mtime, 1543094477); TEST_STR_EQUAL(hdr.name, "input.txt"); TEST_ASSERT(!hdr.unknown_record); - TEST_ASSERT(istream_read(fp, buffer, 5) == 5); + TEST_ASSERT(sqfs_istream_read(fp, buffer, 5) == 5); buffer[5] = '\0'; TEST_STR_EQUAL(buffer, "test\n"); diff --git a/lib/tar/test/tar_xattr_bin.c b/lib/tar/test/tar_xattr_bin.c index f897bce..94c8d76 100644 --- a/lib/tar/test/tar_xattr_bin.c +++ b/lib/tar/test/tar_xattr_bin.c @@ -35,7 +35,7 @@ int main(int argc, char **argv) TEST_EQUAL_UI(hdr.mtime, 1543094477); TEST_STR_EQUAL(hdr.name, "input.txt"); TEST_ASSERT(!hdr.unknown_record); - TEST_ASSERT(istream_read(fp, buffer, 5) == 5); + TEST_ASSERT(sqfs_istream_read(fp, buffer, 5) == 5); buffer[5] = '\0'; TEST_STR_EQUAL(buffer, "test\n"); -- cgit v1.2.3