summaryrefslogtreecommitdiff
path: root/lib/fstream/uncompress
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2020-09-14 14:59:31 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2020-09-16 09:34:35 +0200
commitc9f5cfd3df2706da940a39d6d816ad084ba80fbd (patch)
tree3aac86238ce840c3c25ff89892f6109c081f60d8 /lib/fstream/uncompress
parentf757737060d4daebb24a32e90d912661428708a8 (diff)
Implement istream decompression support
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/fstream/uncompress')
-rw-r--r--lib/fstream/uncompress/autodetect.c53
-rw-r--r--lib/fstream/uncompress/gzip.c91
-rw-r--r--lib/fstream/uncompress/istream_compressor.c59
-rw-r--r--lib/fstream/uncompress/xz.c93
4 files changed, 296 insertions, 0 deletions
diff --git a/lib/fstream/uncompress/autodetect.c b/lib/fstream/uncompress/autodetect.c
new file mode 100644
index 0000000..4ffe078
--- /dev/null
+++ b/lib/fstream/uncompress/autodetect.c
@@ -0,0 +1,53 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+/*
+ * autodetect.c
+ *
+ * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
+ */
+#include "../internal.h"
+
+static const struct {
+ int id;
+ const sqfs_u8 *value;
+ size_t len;
+} magic[] = {
+ { FSTREAM_COMPRESSOR_GZIP, (const sqfs_u8 *)"\x1F\x8B\x08", 3 },
+ { FSTREAM_COMPRESSOR_XZ, (const sqfs_u8 *)("\xFD" "7zXZ"), 6 },
+};
+
+int istream_detect_compressor(istream_t *strm,
+ int (*probe)(const sqfs_u8 *data, size_t size))
+{
+ size_t i;
+ int ret;
+
+ ret = istream_precache(strm);
+ if (ret != 0)
+ return ret;
+
+ if (probe != NULL) {
+ ret = probe(strm->buffer + strm->buffer_offset,
+ strm->buffer_used - strm->buffer_offset);
+ if (ret < 0)
+ return ret;
+
+ /* XXX: this means the data is uncompressed. We do this check
+ first since it might be perfectly OK for the uncompressed
+ data to contain a magic number from the table. */
+ if (ret > 0)
+ return 0;
+ }
+
+ for (i = 0; i < sizeof(magic) / sizeof(magic[0]); ++i) {
+ if ((strm->buffer_used - strm->buffer_offset) < magic[i].len)
+ continue;
+
+ ret = memcmp(strm->buffer + strm->buffer_offset,
+ magic[i].value, magic[i].len);
+
+ if (ret == 0)
+ return magic[i].id;
+ }
+
+ return 0;
+}
diff --git a/lib/fstream/uncompress/gzip.c b/lib/fstream/uncompress/gzip.c
new file mode 100644
index 0000000..c2003db
--- /dev/null
+++ b/lib/fstream/uncompress/gzip.c
@@ -0,0 +1,91 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+/*
+ * gzip.c
+ *
+ * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
+ */
+#include "../internal.h"
+
+#include <zlib.h>
+
+typedef struct {
+ istream_comp_t base;
+
+ z_stream strm;
+} istream_gzip_t;
+
+static int precache(istream_t *base)
+{
+ istream_t *wrapped = ((istream_comp_t *)base)->wrapped;
+ istream_gzip_t *gzip = (istream_gzip_t *)base;
+ int ret;
+
+ for (;;) {
+ ret = istream_precache(wrapped);
+ if (ret != 0)
+ return ret;
+
+ gzip->strm.avail_in = wrapped->buffer_used;
+ gzip->strm.next_in = wrapped->buffer;
+
+ gzip->strm.avail_out = BUFSZ - base->buffer_used;
+ gzip->strm.next_out = base->buffer + base->buffer_used;
+
+ ret = inflate(&gzip->strm, Z_NO_FLUSH);
+
+ wrapped->buffer_offset = wrapped->buffer_used -
+ gzip->strm.avail_in;
+
+ base->buffer_used = BUFSZ - gzip->strm.avail_out;
+
+ if (ret == Z_BUF_ERROR)
+ break;
+
+ if (ret == Z_STREAM_END) {
+ base->eof = true;
+ break;
+ }
+
+ if (ret != Z_OK) {
+ fprintf(stderr,
+ "%s: internal error in gzip decoder.\n",
+ wrapped->get_filename(wrapped));
+ return -1;
+ }
+ }
+
+ return 0;
+}
+
+static void cleanup(istream_comp_t *base)
+{
+ istream_gzip_t *gzip = (istream_gzip_t *)base;
+
+ inflateEnd(&gzip->strm);
+}
+
+istream_comp_t *istream_gzip_create(const char *filename)
+{
+ istream_gzip_t *gzip = calloc(1, sizeof(*gzip));
+ istream_comp_t *base = (istream_comp_t *)gzip;
+ int ret;
+
+ if (gzip == NULL) {
+ fprintf(stderr, "%s: creating gzip decoder: %s.\n",
+ filename, strerror(errno));
+ return NULL;
+ }
+
+ ret = inflateInit2(&gzip->strm, 16 + 15);
+ if (ret != Z_OK) {
+ fprintf(stderr,
+ "%s: internal error creating gzip reader.\n",
+ filename);
+ free(gzip);
+ return NULL;
+ }
+
+ ((istream_t *)base)->precache = precache;
+ base->cleanup = cleanup;
+ return base;
+}
diff --git a/lib/fstream/uncompress/istream_compressor.c b/lib/fstream/uncompress/istream_compressor.c
new file mode 100644
index 0000000..924f309
--- /dev/null
+++ b/lib/fstream/uncompress/istream_compressor.c
@@ -0,0 +1,59 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+/*
+ * istream_compressor.c
+ *
+ * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
+ */
+#include "../internal.h"
+
+static const char *comp_get_filename(istream_t *strm)
+{
+ istream_comp_t *comp = (istream_comp_t *)strm;
+
+ return comp->wrapped->get_filename(comp->wrapped);
+}
+
+static void comp_destroy(sqfs_object_t *obj)
+{
+ istream_comp_t *comp = (istream_comp_t *)obj;
+
+ comp->cleanup(comp);
+ sqfs_destroy(comp->wrapped);
+ free(comp);
+}
+
+istream_t *istream_compressor_create(istream_t *strm, int comp_id)
+{
+ istream_comp_t *comp = NULL;
+ sqfs_object_t *obj;
+ istream_t *base;
+
+ switch (comp_id) {
+ case FSTREAM_COMPRESSOR_GZIP:
+#ifdef WITH_GZIP
+ comp = istream_gzip_create(strm->get_filename(strm));
+#endif
+ break;
+ case FSTREAM_COMPRESSOR_XZ:
+#ifdef WITH_XZ
+ comp = istream_xz_create(strm->get_filename(strm));
+#endif
+ break;
+ default:
+ break;
+ }
+
+ if (comp == NULL)
+ return NULL;
+
+ comp->wrapped = strm;
+
+ base = (istream_t *)comp;
+ base->get_filename = comp_get_filename;
+ base->buffer = comp->uncompressed;
+ base->eof = false;
+
+ obj = (sqfs_object_t *)comp;
+ obj->destroy = comp_destroy;
+ return base;
+}
diff --git a/lib/fstream/uncompress/xz.c b/lib/fstream/uncompress/xz.c
new file mode 100644
index 0000000..0e48468
--- /dev/null
+++ b/lib/fstream/uncompress/xz.c
@@ -0,0 +1,93 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+/*
+ * xz.c
+ *
+ * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
+ */
+#include "../internal.h"
+
+#include <lzma.h>
+
+typedef struct {
+ istream_comp_t base;
+
+ lzma_stream strm;
+} istream_xz_t;
+
+static int precache(istream_t *base)
+{
+ istream_xz_t *xz = (istream_xz_t *)base;
+ istream_t *wrapped = ((istream_comp_t *)base)->wrapped;
+ lzma_ret ret_xz;
+ int ret;
+
+ for (;;) {
+ ret = istream_precache(wrapped);
+ if (ret != 0)
+ return ret;
+
+ xz->strm.avail_in = wrapped->buffer_used;
+ xz->strm.next_in = wrapped->buffer;
+
+ xz->strm.avail_out = BUFSZ - base->buffer_used;
+ xz->strm.next_out = base->buffer + base->buffer_used;
+
+ ret_xz = lzma_code(&xz->strm, LZMA_RUN);
+
+ base->buffer_used = BUFSZ - xz->strm.avail_out;
+ wrapped->buffer_offset = wrapped->buffer_used -
+ xz->strm.avail_in;
+
+ if (ret_xz == LZMA_BUF_ERROR)
+ break;
+
+ if (ret_xz == LZMA_STREAM_END) {
+ base->eof = true;
+ break;
+ }
+
+ if (ret_xz != LZMA_OK) {
+ fprintf(stderr,
+ "%s: internal error in xz decoder.\n",
+ wrapped->get_filename(wrapped));
+ return -1;
+ }
+ }
+
+ return 0;
+}
+
+static void cleanup(istream_comp_t *base)
+{
+ istream_xz_t *xz = (istream_xz_t *)base;
+
+ lzma_end(&xz->strm);
+}
+
+istream_comp_t *istream_xz_create(const char *filename)
+{
+ istream_xz_t *xz = calloc(1, sizeof(*xz));
+ istream_comp_t *base = (istream_comp_t *)xz;
+ sqfs_u64 memlimit = 65 * 1024 * 1024;
+ lzma_ret ret_xz;
+
+ if (xz == NULL) {
+ fprintf(stderr, "%s: creating xz decoder: %s.\n",
+ filename, strerror(errno));
+ return NULL;
+ }
+
+ ret_xz = lzma_stream_decoder(&xz->strm, memlimit, 0);
+
+ if (ret_xz != LZMA_OK) {
+ fprintf(stderr,
+ "%s: error initializing xz decoder.\n",
+ filename);
+ free(xz);
+ return NULL;
+ }
+
+ ((istream_t *)base)->precache = precache;
+ base->cleanup = cleanup;
+ return base;
+}