diff options
Diffstat (limited to 'lib/comp')
-rw-r--r-- | lib/comp/compressor.c | 8 | ||||
-rw-r--r-- | lib/comp/gzip.c | 107 | ||||
-rw-r--r-- | lib/comp/internal.h | 4 | ||||
-rw-r--r-- | lib/comp/xz.c (renamed from lib/comp/lzma.c) | 36 | ||||
-rw-r--r-- | lib/comp/zlib.c | 107 |
5 files changed, 131 insertions, 131 deletions
diff --git a/lib/comp/compressor.c b/lib/comp/compressor.c index 3bc2b4d..1ede111 100644 --- a/lib/comp/compressor.c +++ b/lib/comp/compressor.c @@ -6,11 +6,11 @@ typedef compressor_t *(*compressor_fun_t)(bool compress, size_t block_size); static compressor_fun_t compressors[SQFS_COMP_MAX + 1] = { -#ifdef WITH_ZLIB - [SQFS_COMP_GZIP] = create_zlib_compressor, +#ifdef WITH_GZIP + [SQFS_COMP_GZIP] = create_gzip_compressor, #endif -#ifdef WITH_LZMA - [SQFS_COMP_XZ] = create_lzma_compressor, +#ifdef WITH_XZ + [SQFS_COMP_XZ] = create_xz_compressor, #endif }; diff --git a/lib/comp/gzip.c b/lib/comp/gzip.c new file mode 100644 index 0000000..ea634b8 --- /dev/null +++ b/lib/comp/gzip.c @@ -0,0 +1,107 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +#include <stdbool.h> +#include <stdlib.h> +#include <string.h> +#include <stdio.h> +#include <zlib.h> + +#include "internal.h" + +typedef struct { + compressor_t base; + + z_stream strm; + bool compress; + + size_t block_size; +} gzip_compressor_t; + +static void gzip_destroy(compressor_t *base) +{ + gzip_compressor_t *gzip = (gzip_compressor_t *)base; + + if (gzip->compress) { + deflateEnd(&gzip->strm); + } else { + inflateEnd(&gzip->strm); + } + + free(gzip); +} + +static ssize_t gzip_do_block(compressor_t *base, const uint8_t *in, + size_t size, uint8_t *out, size_t outsize) +{ + gzip_compressor_t *gzip = (gzip_compressor_t *)base; + size_t written; + int ret; + + if (gzip->compress) { + ret = deflateReset(&gzip->strm); + } else { + ret = inflateReset(&gzip->strm); + } + + if (ret != Z_OK) { + fputs("resetting zlib stream failed\n", stderr); + return -1; + } + + gzip->strm.next_in = (void *)in; + gzip->strm.avail_in = size; + gzip->strm.next_out = out; + gzip->strm.avail_out = outsize; + + if (gzip->compress) { + ret = deflate(&gzip->strm, Z_FINISH); + } else { + ret = inflate(&gzip->strm, Z_FINISH); + } + + if (ret == Z_STREAM_END) { + written = gzip->strm.total_out; + + if (gzip->compress && written >= size) + return 0; + + return (ssize_t)written; + } + + if (ret != Z_OK) { + fputs("gzip block processing failed\n", stderr); + return -1; + } + + return 0; +} + +compressor_t *create_gzip_compressor(bool compress, size_t block_size) +{ + gzip_compressor_t *gzip = calloc(1, sizeof(*gzip)); + compressor_t *base = (compressor_t *)gzip; + int ret; + + if (gzip == NULL) { + perror("creating gzip compressor"); + return NULL; + } + + gzip->compress = compress; + gzip->block_size = block_size; + base->do_block = gzip_do_block; + base->destroy = gzip_destroy; + + if (compress) { + ret = deflateInit(&gzip->strm, Z_BEST_COMPRESSION); + } else { + ret = inflateInit(&gzip->strm); + } + + if (ret != Z_OK) { + fputs("internal error creating zlib stream\n", stderr); + free(gzip); + return NULL; + } + + return base; +} diff --git a/lib/comp/internal.h b/lib/comp/internal.h index 17fffe1..d1cf1f2 100644 --- a/lib/comp/internal.h +++ b/lib/comp/internal.h @@ -4,8 +4,8 @@ #include "compress.h" -compressor_t *create_lzma_compressor(bool compress, size_t block_size); +compressor_t *create_xz_compressor(bool compress, size_t block_size); -compressor_t *create_zlib_compressor(bool compress, size_t block_size); +compressor_t *create_gzip_compressor(bool compress, size_t block_size); #endif /* INTERNAL_H */ diff --git a/lib/comp/lzma.c b/lib/comp/xz.c index 3169c33..98d740c 100644 --- a/lib/comp/lzma.c +++ b/lib/comp/xz.c @@ -10,23 +10,23 @@ typedef struct { compressor_t base; size_t block_size; -} lzma_compressor_t; +} xz_compressor_t; -static ssize_t lzma_comp_block(compressor_t *base, const uint8_t *in, +static ssize_t xz_comp_block(compressor_t *base, const uint8_t *in, size_t size, uint8_t *out, size_t outsize) { - lzma_compressor_t *lzma = (lzma_compressor_t *)base; + 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 LZMA options\n", stderr); + fputs("error initializing xz options\n", stderr); return -1; } - opt.dict_size = lzma->block_size; + opt.dict_size = xz->block_size; filters[0].id = LZMA_FILTER_LZMA2; filters[0].options = &opt; @@ -41,15 +41,15 @@ static ssize_t lzma_comp_block(compressor_t *base, const uint8_t *in, return (written >= size) ? 0 : written; if (ret != LZMA_BUF_ERROR) { - fputs("lzma block compress failed\n", stderr); + fputs("xz block compress failed\n", stderr); return -1; } return 0; } -static ssize_t lzma_uncomp_block(compressor_t *base, const uint8_t *in, - size_t size, uint8_t *out, size_t outsize) +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; @@ -64,27 +64,27 @@ static ssize_t lzma_uncomp_block(compressor_t *base, const uint8_t *in, if (ret == LZMA_OK && size == src_pos) return (ssize_t)dest_pos; - fputs("lzma block extract failed\n", stderr); + fputs("xz block extract failed\n", stderr); return -1; } -static void lzma_destroy(compressor_t *base) +static void xz_destroy(compressor_t *base) { free(base); } -compressor_t *create_lzma_compressor(bool compress, size_t block_size) +compressor_t *create_xz_compressor(bool compress, size_t block_size) { - lzma_compressor_t *lzma = calloc(1, sizeof(*lzma)); - compressor_t *base = (compressor_t *)lzma; + xz_compressor_t *xz = calloc(1, sizeof(*xz)); + compressor_t *base = (compressor_t *)xz; - if (lzma == NULL) { - perror("creating lzma stream"); + if (xz == NULL) { + perror("creating xz compressor"); return NULL; } - lzma->block_size = block_size; - base->destroy = lzma_destroy; - base->do_block = compress ? lzma_comp_block : lzma_uncomp_block; + xz->block_size = block_size; + base->destroy = xz_destroy; + base->do_block = compress ? xz_comp_block : xz_uncomp_block; return base; } diff --git a/lib/comp/zlib.c b/lib/comp/zlib.c deleted file mode 100644 index f151132..0000000 --- a/lib/comp/zlib.c +++ /dev/null @@ -1,107 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -#include <stdbool.h> -#include <stdlib.h> -#include <string.h> -#include <stdio.h> -#include <zlib.h> - -#include "internal.h" - -typedef struct { - compressor_t base; - - z_stream strm; - bool compress; - - size_t block_size; -} zlib_compressor_t; - -static void zlib_destroy(compressor_t *base) -{ - zlib_compressor_t *zlib = (zlib_compressor_t *)base; - - if (zlib->compress) { - deflateEnd(&zlib->strm); - } else { - inflateEnd(&zlib->strm); - } - - free(zlib); -} - -static ssize_t zlib_do_block(compressor_t *base, const uint8_t *in, - size_t size, uint8_t *out, size_t outsize) -{ - zlib_compressor_t *zlib = (zlib_compressor_t *)base; - size_t written; - int ret; - - if (zlib->compress) { - ret = deflateReset(&zlib->strm); - } else { - ret = inflateReset(&zlib->strm); - } - - if (ret != Z_OK) { - fputs("resetting zlib stream failed\n", stderr); - return -1; - } - - zlib->strm.next_in = (void *)in; - zlib->strm.avail_in = size; - zlib->strm.next_out = out; - zlib->strm.avail_out = outsize; - - if (zlib->compress) { - ret = deflate(&zlib->strm, Z_FINISH); - } else { - ret = inflate(&zlib->strm, Z_FINISH); - } - - if (ret == Z_STREAM_END) { - written = zlib->strm.total_out; - - if (zlib->compress && written >= size) - return 0; - - return (ssize_t)written; - } - - if (ret != Z_OK) { - fputs("zlib block processing failed\n", stderr); - return -1; - } - - return 0; -} - -compressor_t *create_zlib_compressor(bool compress, size_t block_size) -{ - zlib_compressor_t *zlib = calloc(1, sizeof(*zlib)); - compressor_t *base = (compressor_t *)zlib; - int ret; - - if (zlib == NULL) { - perror("creating zlib stream"); - return NULL; - } - - zlib->compress = compress; - zlib->block_size = block_size; - base->do_block = zlib_do_block; - base->destroy = zlib_destroy; - - if (compress) { - ret = deflateInit(&zlib->strm, Z_BEST_COMPRESSION); - } else { - ret = inflateInit(&zlib->strm); - } - - if (ret != Z_OK) { - fputs("internal error creating zlib stream\n", stderr); - free(zlib); - return NULL; - } - - return base; -} |