diff options
Diffstat (limited to 'lib/io')
-rw-r--r-- | lib/io/Makemodule.am | 23 | ||||
-rw-r--r-- | lib/io/src/internal.h | 1 | ||||
-rw-r--r-- | lib/io/src/mem.c | 110 | ||||
-rw-r--r-- | lib/io/src/std.c | 35 | ||||
-rw-r--r-- | lib/io/test/istream_mem.c | 74 | ||||
-rw-r--r-- | lib/io/test/xfrm.c | 2 |
6 files changed, 10 insertions, 235 deletions
diff --git a/lib/io/Makemodule.am b/lib/io/Makemodule.am index d342063..2f6fb1c 100644 --- a/lib/io/Makemodule.am +++ b/lib/io/Makemodule.am @@ -1,20 +1,14 @@ -libio_a_SOURCES = include/io/xfrm.h include/io/std.h \ - include/io/dir_iterator.h include/io/mem.h lib/io/src/internal.h \ - lib/io/src/xfrm/ostream.c \ - lib/io/src/xfrm/istream.c lib/io/src/dir_tree_iterator.c \ - lib/io/src/mem.c lib/io/src/std.c +libio_a_SOURCES = include/io/xfrm.h include/io/dir_iterator.h \ + lib/io/src/internal.h lib/io/src/xfrm/ostream.c \ + lib/io/src/xfrm/istream.c lib/io/src/dir_tree_iterator.c libio_a_CFLAGS = $(AM_CFLAGS) $(ZLIB_CFLAGS) $(XZ_CFLAGS) libio_a_CFLAGS += $(ZSTD_CFLAGS) $(BZIP2_CFLAGS) noinst_LIBRARIES += libio.a -LIBIO_TESTS = test_istream_mem test_dir_iterator \ +LIBIO_TESTS = 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 -test_istream_mem_LDADD = libio.a libcompat.a -test_istream_mem_CPPFLAGS = $(AM_CPPFLAGS) - test_dir_iterator_SOURCES = lib/io/test/dir_iterator.c test_dir_iterator_LDADD = libio.a libsquashfs.la libutil.a libcompat.a test_dir_iterator_CPPFLAGS = $(AM_CPPFLAGS) @@ -37,7 +31,8 @@ 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 = libsquashfs.la libio.a libxfrm.a libcompat.a $(XZ_LIBS) +test_io_xfrm_xz_LDADD = libcommon.a 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 @@ -45,7 +40,7 @@ endif if WITH_BZIP2 test_io_xfrm_bzip2_SOURCES = lib/io/test/xfrm.c -test_io_xfrm_bzip2_LDADD = libsquashfs.la libio.a libxfrm.a \ +test_io_xfrm_bzip2_LDADD = libcommon.a libsquashfs.la libio.a libxfrm.a \ libcompat.a $(BZIP2_LIBS) test_io_xfrm_bzip2_CPPFLAGS = $(AM_CPPFLAGS) -DDO_BZIP2=1 @@ -54,7 +49,7 @@ endif if WITH_GZIP test_io_xfrm_gzip_SOURCES = lib/io/test/xfrm.c -test_io_xfrm_gzip_LDADD = libsquashfs.la libio.a libxfrm.a \ +test_io_xfrm_gzip_LDADD = libcommon.a libsquashfs.la libio.a libxfrm.a \ libcompat.a $(ZLIB_LIBS) test_io_xfrm_gzip_CPPFLAGS = $(AM_CPPFLAGS) -DDO_GZIP=1 @@ -64,7 +59,7 @@ endif if WITH_ZSTD if HAVE_ZSTD_STREAM test_io_xfrm_zstd_SOURCES = lib/io/test/xfrm.c -test_io_xfrm_zstd_LDADD = libsquashfs.la libio.a libxfrm.a \ +test_io_xfrm_zstd_LDADD = libcommon.a libsquashfs.la libio.a libxfrm.a \ libcompat.a $(ZSTD_LIBS) test_io_xfrm_zstd_CPPFLAGS = $(AM_CPPFLAGS) -DDO_ZSTD=1 diff --git a/lib/io/src/internal.h b/lib/io/src/internal.h index e9804b9..7c2b1df 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/xfrm.h" -#include "io/std.h" #include "xfrm/compress.h" #include <string.h> diff --git a/lib/io/src/mem.c b/lib/io/src/mem.c deleted file mode 100644 index 7150d1f..0000000 --- a/lib/io/src/mem.c +++ /dev/null @@ -1,110 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -/* - * mem.c - * - * Copyright (C) 2023 David Oberhollenzer <goliath@infraroot.at> - */ -#include "config.h" -#include "io/mem.h" -#include "compat.h" -#include "sqfs/io.h" - -#include <stdlib.h> -#include <string.h> -#include <assert.h> - -typedef struct { - sqfs_istream_t base; - - sqfs_u8 *buffer; - size_t bufsz; - - const void *data; - size_t size; - size_t offset; - size_t visible; - - char *name; -} mem_istream_t; - -static int mem_get_buffered_data(sqfs_istream_t *strm, const sqfs_u8 **out, - size_t *size, size_t want) -{ - mem_istream_t *mem = (mem_istream_t *)strm; - size_t have = mem->size - mem->offset; - - if (have > mem->bufsz) - have = mem->bufsz; - - if (want > have) - want = have; - - if (mem->visible == 0 || mem->visible < want) { - memcpy(mem->buffer + mem->visible, - (const char *)mem->data + mem->offset + mem->visible, - have - mem->visible); - mem->visible = have; - } - - *out = mem->buffer; - *size = mem->visible; - return (mem->visible == 0) ? 1 : 0; -} - -static void mem_advance_buffer(sqfs_istream_t *strm, size_t count) -{ - mem_istream_t *mem = (mem_istream_t *)strm; - - assert(count <= mem->visible); - - if (count > 0 && count < mem->visible) - memmove(mem->buffer, mem->buffer + count, mem->visible - count); - - mem->offset += count; - mem->visible -= count; - - if (mem->visible < mem->bufsz) { - memset(mem->buffer + mem->visible, 0, - mem->bufsz - mem->visible); - } -} - -static const char *mem_in_get_filename(sqfs_istream_t *strm) -{ - return ((mem_istream_t *)strm)->name; -} - -static void mem_in_destroy(sqfs_object_t *obj) -{ - free(((mem_istream_t *)obj)->buffer); - free(((mem_istream_t *)obj)->name); - free(obj); -} - -sqfs_istream_t *istream_memory_create(const char *name, size_t bufsz, - const void *data, size_t size) -{ - mem_istream_t *mem = calloc(1, sizeof(*mem)); - sqfs_istream_t *strm = (sqfs_istream_t *)mem; - - if (mem == NULL) - return NULL; - - sqfs_object_init(mem, mem_in_destroy, NULL); - - mem->name = strdup(name); - if (mem->name == NULL) - return sqfs_drop(mem); - - mem->buffer = malloc(bufsz); - if (mem->buffer == NULL) - return sqfs_drop(mem); - - mem->data = data; - mem->size = size; - mem->bufsz = bufsz; - strm->get_buffered_data = mem_get_buffered_data; - strm->advance_buffer = mem_advance_buffer; - strm->get_filename = mem_in_get_filename; - return strm; -} diff --git a/lib/io/src/std.c b/lib/io/src/std.c deleted file mode 100644 index 62635ec..0000000 --- a/lib/io/src/std.c +++ /dev/null @@ -1,35 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -/* - * std.c - * - * Copyright (C) 2023 David Oberhollenzer <goliath@infraroot.at> - */ -#include "config.h" -#include "compat.h" -#include "io/std.h" -#include "sqfs/io.h" - -#if defined(_WIN32) || defined(__WINDOWS__) -#define WIN32_LEAN_AND_MEAN -#include <windows.h> -#else -#include <unistd.h> -#define STD_INPUT_HANDLE STDIN_FILENO -#define STD_OUTPUT_HANDLE STDOUT_FILENO -#define GetStdHandle(hnd) hnd -#endif - -int istream_open_stdin(sqfs_istream_t **out) -{ - sqfs_file_handle_t hnd = GetStdHandle(STD_INPUT_HANDLE); - - return sqfs_istream_open_handle(out, "stdin", hnd, 0); -} - -int ostream_open_stdout(sqfs_ostream_t **out) -{ - sqfs_file_handle_t hnd = GetStdHandle(STD_OUTPUT_HANDLE); - - return sqfs_ostream_open_handle(out, "stdout", hnd, - SQFS_FILE_OPEN_NO_SPARSE); -} diff --git a/lib/io/test/istream_mem.c b/lib/io/test/istream_mem.c deleted file mode 100644 index d5e0c2c..0000000 --- a/lib/io/test/istream_mem.c +++ /dev/null @@ -1,74 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -/* - * istream_mem.c - * - * Copyright (C) 2023 David Oberhollenzer <goliath@infraroot.at> - */ -#include "config.h" - -#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' */ -static const size_t end2 = 941; /* region 3: filled with 'C' */ - -static unsigned char data[941]; - -static unsigned char byte_at_offset(size_t off) -{ - if (off < end0) - return 'A'; - if (off < end1) - return 'B'; - return 'C'; -} - -static void init_buffer(void) -{ - for (size_t i = 0; i < end2; ++i) - data[i] = byte_at_offset(i); -} - -int main(int argc, char **argv) -{ - size_t i, diff, size; - bool eat_all = true; - const sqfs_u8 *ptr; - sqfs_istream_t *in; - int ret; - (void)argc; (void)argv; - - init_buffer(); - - in = istream_memory_create("memstream.txt", 61, data, sizeof(data)); - TEST_NOT_NULL(in); - TEST_EQUAL_UI(((sqfs_object_t *)in)->refcount, 1); - - TEST_STR_EQUAL(in->get_filename(in), "memstream.txt"); - - for (i = 0; i < end2; i += diff) { - ret = in->get_buffered_data(in, &ptr, &size, 61); - TEST_EQUAL_I(ret, 0); - - if ((end2 - i) >= 61) { - TEST_NOT_NULL(ptr); - TEST_EQUAL_UI(size, 61); - } else { - TEST_NOT_NULL(ptr); - TEST_EQUAL_UI(size, (end2 - i)); - } - - for (size_t j = 0; j < size; ++j) { - TEST_EQUAL_UI(ptr[j], byte_at_offset(i + j)); - } - - diff = eat_all ? size : (size / 2); - eat_all = !eat_all; - in->advance_buffer(in, diff); - } - - sqfs_drop(in); - return EXIT_SUCCESS; -} diff --git a/lib/io/test/xfrm.c b/lib/io/test/xfrm.c index b871610..26eee87 100644 --- a/lib/io/test/xfrm.c +++ b/lib/io/test/xfrm.c @@ -9,7 +9,7 @@ #include "util/test.h" #include "sqfs/io.h" #include "io/xfrm.h" -#include "io/mem.h" +#include "common.h" static const sqfs_u8 blob_in[] = { #if defined(DO_XZ) |