diff options
author | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2022-06-26 22:51:27 +0200 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2022-07-08 19:17:35 +0200 |
commit | 359d71e90050a8b83f7bc7d2ecd4ff29c477cc22 (patch) | |
tree | df2b7a585b78e6776446d4ca9be62a22db27f0a7 /lib/io/uncompress/zstd.c | |
parent | 2087cc237cd0fe1ed29ebf891648bacb46f4833b (diff) |
Cleanup: rename libfstream to libio, split headers
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/io/uncompress/zstd.c')
-rw-r--r-- | lib/io/uncompress/zstd.c | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/lib/io/uncompress/zstd.c b/lib/io/uncompress/zstd.c new file mode 100644 index 0000000..fd22cbf --- /dev/null +++ b/lib/io/uncompress/zstd.c @@ -0,0 +1,81 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * zstd.c + * + * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at> + */ +#include "../internal.h" + +#include <zstd.h> + +#ifdef HAVE_ZSTD_STREAM +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; +} +#endif /* HAVE_ZSTD_STREAM */ |