aboutsummaryrefslogtreecommitdiff
path: root/lib/fstream/compress
diff options
context:
space:
mode:
Diffstat (limited to 'lib/fstream/compress')
-rw-r--r--lib/fstream/compress/ostream_compressor.c5
-rw-r--r--lib/fstream/compress/zstd.c92
2 files changed, 97 insertions, 0 deletions
diff --git a/lib/fstream/compress/ostream_compressor.c b/lib/fstream/compress/ostream_compressor.c
index 5137f1d..d1d55e1 100644
--- a/lib/fstream/compress/ostream_compressor.c
+++ b/lib/fstream/compress/ostream_compressor.c
@@ -77,6 +77,11 @@ ostream_t *ostream_compressor_create(ostream_t *strm, int comp_id)
comp = ostream_xz_create(strm->get_filename(strm));
#endif
break;
+ case FSTREAM_COMPRESSOR_ZSTD:
+#ifdef WITH_ZSTD
+ comp = ostream_zstd_create(strm->get_filename(strm));
+#endif
+ break;
default:
break;
}
diff --git a/lib/fstream/compress/zstd.c b/lib/fstream/compress/zstd.c
new file mode 100644
index 0000000..f4f7f86
--- /dev/null
+++ b/lib/fstream/compress/zstd.c
@@ -0,0 +1,92 @@
+/* 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 {
+ ostream_comp_t base;
+
+ ZSTD_CStream *strm;
+} ostream_zstd_t;
+
+static int flush_inbuf(ostream_comp_t *base, bool finish)
+{
+ ostream_zstd_t *zstd = (ostream_zstd_t *)base;
+ ZSTD_EndDirective op;
+ ZSTD_outBuffer out;
+ ZSTD_inBuffer in;
+ size_t ret;
+
+ op = finish ? ZSTD_e_end : ZSTD_e_continue;
+
+ do {
+ memset(&in, 0, sizeof(in));
+ memset(&out, 0, sizeof(out));
+
+ in.src = base->inbuf;
+ in.size = base->inbuf_used;
+
+ out.dst = base->outbuf;
+ out.size = BUFSZ;
+
+ ret = ZSTD_compressStream2(zstd->strm, &out, &in, op);
+
+ if (ZSTD_isError(ret)) {
+ fprintf(stderr, "%s: error in zstd compressor.\n",
+ base->wrapped->get_filename(base->wrapped));
+ return -1;
+ }
+
+ if (base->wrapped->append(base->wrapped, base->outbuf,
+ out.pos)) {
+ return -1;
+ }
+
+ if (in.pos < in.size) {
+ base->inbuf_used = in.size - in.pos;
+
+ memmove(base->inbuf, base->inbuf + in.pos,
+ base->inbuf_used);
+ } else {
+ base->inbuf_used = 0;
+ }
+ } while (finish && ret != 0);
+
+ return 0;
+}
+
+static void cleanup(ostream_comp_t *base)
+{
+ ostream_zstd_t *zstd = (ostream_zstd_t *)base;
+
+ ZSTD_freeCStream(zstd->strm);
+}
+
+ostream_comp_t *ostream_zstd_create(const char *filename)
+{
+ ostream_zstd_t *zstd = calloc(1, sizeof(*zstd));
+ ostream_comp_t *base = (ostream_comp_t *)zstd;
+
+ if (zstd == NULL) {
+ fprintf(stderr, "%s: creating zstd wrapper: %s.\n",
+ filename, strerror(errno));
+ return NULL;
+ }
+
+ zstd->strm = ZSTD_createCStream();
+ if (zstd->strm == NULL) {
+ fprintf(stderr, "%s: error creating zstd decoder.\n",
+ filename);
+ free(zstd);
+ return NULL;
+ }
+
+ base->flush_inbuf = flush_inbuf;
+ base->cleanup = cleanup;
+ return base;
+}