From 7070857b47373dafc1ab93fadec79243da589a1a Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Sat, 4 May 2019 20:47:39 +0200 Subject: Rename lzma compressor to xz, zlib compressor to gzip Stick to the terminology that SquashFS uses. SquashFS uses "lzma" to refere to lzma1, "xz" to refere to lzma2 and confusingly "gzip" to refere to raw zlib deflate. We can avoid a lot of confusion by _consistently_ using the same terminology. Signed-off-by: David Oberhollenzer --- lib/comp/xz.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 lib/comp/xz.c (limited to 'lib/comp/xz.c') diff --git a/lib/comp/xz.c b/lib/comp/xz.c new file mode 100644 index 0000000..98d740c --- /dev/null +++ b/lib/comp/xz.c @@ -0,0 +1,90 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +#include +#include +#include +#include +#include + +#include "internal.h" + +typedef struct { + compressor_t base; + size_t block_size; +} xz_compressor_t; + +static ssize_t xz_comp_block(compressor_t *base, const uint8_t *in, + size_t size, uint8_t *out, size_t outsize) +{ + xz_compressor_t *xz = (xz_compressor_t *)base; + lzma_filter filters[5]; + lzma_options_lzma opt; + size_t written = 0; + lzma_ret ret; + + if (lzma_lzma_preset(&opt, LZMA_PRESET_DEFAULT)) { + fputs("error initializing xz options\n", stderr); + return -1; + } + + opt.dict_size = xz->block_size; + + filters[0].id = LZMA_FILTER_LZMA2; + filters[0].options = &opt; + + filters[1].id = LZMA_VLI_UNKNOWN; + filters[1].options = NULL; + + ret = lzma_stream_buffer_encode(filters, LZMA_CHECK_CRC32, NULL, + in, size, out, &written, outsize); + + if (ret == LZMA_OK) + return (written >= size) ? 0 : written; + + if (ret != LZMA_BUF_ERROR) { + fputs("xz block compress failed\n", stderr); + return -1; + } + + return 0; +} + +static ssize_t xz_uncomp_block(compressor_t *base, const uint8_t *in, + size_t size, uint8_t *out, size_t outsize) +{ + uint64_t memlimit = 32 * 1024 * 1024; + size_t dest_pos = 0; + size_t src_pos = 0; + lzma_ret ret; + (void)base; + + ret = lzma_stream_buffer_decode(&memlimit, 0, NULL, + in, &src_pos, size, + out, &dest_pos, outsize); + + if (ret == LZMA_OK && size == src_pos) + return (ssize_t)dest_pos; + + fputs("xz block extract failed\n", stderr); + return -1; +} + +static void xz_destroy(compressor_t *base) +{ + free(base); +} + +compressor_t *create_xz_compressor(bool compress, size_t block_size) +{ + xz_compressor_t *xz = calloc(1, sizeof(*xz)); + compressor_t *base = (compressor_t *)xz; + + if (xz == NULL) { + perror("creating xz compressor"); + return NULL; + } + + xz->block_size = block_size; + base->destroy = xz_destroy; + base->do_block = compress ? xz_comp_block : xz_uncomp_block; + return base; +} -- cgit v1.2.3