summaryrefslogtreecommitdiff
path: root/lib/fstream/uncompress/zstd.c
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2020-09-14 20:25:10 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2020-09-16 09:34:35 +0200
commit15431d4fd8de67a03af4ee9cc8e774e1750e9da2 (patch)
treef5386db24f3901e86d3aaae10042d4f9181a6b32 /lib/fstream/uncompress/zstd.c
parent76b06f71078e2eea7def3d1c15cdb103dec785ed (diff)
Add zstd stream compressor implementation to libfstream
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/fstream/uncompress/zstd.c')
-rw-r--r--lib/fstream/uncompress/zstd.c79
1 files changed, 79 insertions, 0 deletions
diff --git a/lib/fstream/uncompress/zstd.c b/lib/fstream/uncompress/zstd.c
new file mode 100644
index 0000000..1838af5
--- /dev/null
+++ b/lib/fstream/uncompress/zstd.c
@@ -0,0 +1,79 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+/*
+ * zstd.c
+ *
+ * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
+ */
+#include "../internal.h"
+
+#include <zstd.h>
+
+typedef struct {
+ istream_comp_t base;
+
+ ZSTD_DStream* strm;
+} istream_zstd_t;
+
+static int precache(istream_t *base)
+{
+ istream_zstd_t *zstd = (istream_zstd_t *)base;
+ istream_t *wrapped = ((istream_comp_t *)base)->wrapped;
+ ZSTD_outBuffer out;
+ ZSTD_inBuffer in;
+ size_t ret;
+
+ if (istream_precache(wrapped))
+ return -1;
+
+ memset(&in, 0, sizeof(in));
+ memset(&out, 0, sizeof(out));
+
+ in.src = wrapped->buffer;
+ in.size = wrapped->buffer_used;
+
+ out.dst = ((istream_comp_t *)base)->uncompressed + base->buffer_used;
+ out.size = BUFSZ - base->buffer_used;
+
+ ret = ZSTD_decompressStream(zstd->strm, &out, &in);
+
+ if (ZSTD_isError(ret)) {
+ fprintf(stderr, "%s: error in zstd decoder.\n",
+ wrapped->get_filename(wrapped));
+ return -1;
+ }
+
+ wrapped->buffer_offset = in.pos;
+ base->buffer_used += out.pos;
+ return 0;
+}
+
+static void cleanup(istream_comp_t *base)
+{
+ istream_zstd_t *zstd = (istream_zstd_t *)base;
+
+ ZSTD_freeDStream(zstd->strm);
+}
+
+istream_comp_t *istream_zstd_create(const char *filename)
+{
+ istream_zstd_t *zstd = calloc(1, sizeof(*zstd));
+ istream_comp_t *base = (istream_comp_t *)zstd;
+
+ if (zstd == NULL) {
+ fprintf(stderr, "%s: creating zstd decoder: %s.\n",
+ filename, strerror(errno));
+ return NULL;
+ }
+
+ zstd->strm = ZSTD_createDStream();
+ if (zstd->strm == NULL) {
+ fprintf(stderr, "%s: error creating zstd decoder.\n",
+ filename);
+ free(zstd);
+ return NULL;
+ }
+
+ ((istream_t *)base)->precache = precache;
+ base->cleanup = cleanup;
+ return base;
+}