diff options
author | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2023-09-23 10:37:27 +0200 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2023-10-24 15:57:18 +0200 |
commit | 6e7b20a41c8a7f10392884e1741b031b579a93fa (patch) | |
tree | 1ed2c8c7d8db94da6fc8dbc73ad54d13e535344a /lib/common | |
parent | b3c5de63c132f74f4e3592f2ff07e5172c5e295f (diff) |
Cleanup: move memory/stdio streams to libcommon
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/common')
-rw-r--r-- | lib/common/Makemodule.am | 11 | ||||
-rw-r--r-- | lib/common/src/stream.c | 135 | ||||
-rw-r--r-- | lib/common/test/istream_mem.c | 74 |
3 files changed, 217 insertions, 3 deletions
diff --git a/lib/common/Makemodule.am b/lib/common/Makemodule.am index 7c07d48..9438d3a 100644 --- a/lib/common/Makemodule.am +++ b/lib/common/Makemodule.am @@ -6,7 +6,8 @@ libcommon_a_SOURCES = include/common.h include/simple_writer.h \ lib/common/src/writer/init.c lib/common/src/writer/cleanup.c \ lib/common/src/writer/serialize_fstree.c lib/common/src/writer/finish.c\ lib/common/src/fstree_cli.c lib/common/src/perror.c \ - lib/common/src/dir_tree.c lib/common/src/read_tree.c + lib/common/src/dir_tree.c lib/common/src/read_tree.c \ + lib/common/src/stream.c libcommon_a_CFLAGS = $(AM_CFLAGS) $(LZO_CFLAGS) if WITH_LZO @@ -15,14 +16,18 @@ endif noinst_LIBRARIES += libcommon.a +test_istream_mem_SOURCES = lib/common/test/istream_mem.c +test_istream_mem_LDADD = libcommon.a libsquashfs.la libcompat.a +test_istream_mem_CPPFLAGS = $(AM_CPPFLAGS) + test_fstree_cli_SOURCES = lib/common/test/fstree_cli.c -test_fstree_cli_LDADD = libcommon.a libio.a libutil.a libcompat.a +test_fstree_cli_LDADD = libcommon.a libutil.a libcompat.a test_get_node_path_SOURCES = lib/common/test/get_node_path.c test_get_node_path_LDADD = libcommon.a libsquashfs.la libcompat.a LIBCOMMON_TESTS = \ - test_fstree_cli test_get_node_path + test_istream_mem test_fstree_cli test_get_node_path check_PROGRAMS += $(LIBCOMMON_TESTS) TESTS += $(LIBCOMMON_TESTS) diff --git a/lib/common/src/stream.c b/lib/common/src/stream.c new file mode 100644 index 0000000..a557360 --- /dev/null +++ b/lib/common/src/stream.c @@ -0,0 +1,135 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * mem.c + * + * Copyright (C) 2023 David Oberhollenzer <goliath@infraroot.at> + */ +#include "config.h" +#include "compat.h" +#include "common.h" +#include "sqfs/io.h" + +#include <stdlib.h> +#include <string.h> +#include <assert.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 + +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; +} + +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/common/test/istream_mem.c b/lib/common/test/istream_mem.c new file mode 100644 index 0000000..2213168 --- /dev/null +++ b/lib/common/test/istream_mem.c @@ -0,0 +1,74 @@ +/* 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 "sqfs/io.h" +#include "common.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; +} |