diff options
author | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2019-04-30 10:50:13 +0200 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2019-05-02 12:40:06 +0200 |
commit | a85c6025903a2b6e97e1938b201b3361a0cbed66 (patch) | |
tree | cf92df4ba782c4b9c4a49dcf1cc3e25f849723a9 | |
parent | fa7f378bf627ddcfd7a93a000149e4d8c3810bf5 (diff) |
Add compressor library
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
-rw-r--r-- | include/compress.h | 25 | ||||
-rw-r--r-- | lib/Makemodule.am | 20 | ||||
-rw-r--r-- | lib/comp/compressor.c | 35 | ||||
-rw-r--r-- | lib/comp/internal.h | 11 | ||||
-rw-r--r-- | lib/comp/lzma.c | 100 | ||||
-rw-r--r-- | lib/comp/zlib.c | 108 | ||||
-rw-r--r-- | mkfs/Makemodule.am | 10 | ||||
-rw-r--r-- | mkfs/options.c | 23 |
8 files changed, 326 insertions, 6 deletions
diff --git a/include/compress.h b/include/compress.h new file mode 100644 index 0000000..d527307 --- /dev/null +++ b/include/compress.h @@ -0,0 +1,25 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +#ifndef COMPRESS_H +#define COMPRESS_H + +#include <sys/types.h> +#include <stdbool.h> +#include <stddef.h> +#include <stdint.h> + +#include "squashfs.h" + +typedef struct compressor_t compressor_t; + +struct compressor_t { + ssize_t (*do_block)(compressor_t *cmp, uint8_t *block, size_t outsize); + + void (*destroy)(compressor_t *stream); +}; + +bool compressor_exists(E_SQFS_COMPRESSOR id); + +compressor_t *compressor_create(E_SQFS_COMPRESSOR id, bool compress, + size_t block_size); + +#endif /* COMPRESS_H */ diff --git a/lib/Makemodule.am b/lib/Makemodule.am index 18ec8c4..89ec538 100644 --- a/lib/Makemodule.am +++ b/lib/Makemodule.am @@ -2,4 +2,22 @@ libfstree_a_SOURCES = lib/fstree/fstree.c lib/fstree/fstree_from_file.c libfstree_a_SOURCES += lib/fstree/fstree_sort.c libfstree_a_SOURCES += include/fstree.h -noinst_LIBRARIES += libfstree.a +libcompress_a_SOURCES = lib/comp/compressor.c lib/comp/internal.h +libcompress_a_CFLAGS = $(AM_CFLAGS) +libcompress_a_CPPFLAGS = $(AM_CPPFLAGS) + +if WITH_ZLIB +libcompress_a_SOURCES += lib/comp/zlib.c + +libcompress_a_CFLAGS += $(ZLIB_CFLAGS) +libcompress_a_CPPFLAGS += -DWITH_ZLIB +endif + +if WITH_LZMA +libcompress_a_SOURCES += lib/comp/lzma.c + +libcompress_a_CFLAGS += $(XZ_CFLAGS) +libcompress_a_CPPFLAGS += -DWITH_LZMA +endif + +noinst_LIBRARIES += libfstree.a libcompress.a diff --git a/lib/comp/compressor.c b/lib/comp/compressor.c new file mode 100644 index 0000000..3bc2b4d --- /dev/null +++ b/lib/comp/compressor.c @@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +#include <string.h> + +#include "internal.h" + +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, +#endif +#ifdef WITH_LZMA + [SQFS_COMP_XZ] = create_lzma_compressor, +#endif +}; + +bool compressor_exists(E_SQFS_COMPRESSOR id) +{ + if (id < SQFS_COMP_MIN || id > SQFS_COMP_MAX) + return false; + + return (compressors[id] != NULL); +} + +compressor_t *compressor_create(E_SQFS_COMPRESSOR id, bool compress, + size_t block_size) +{ + if (id < SQFS_COMP_MIN || id > SQFS_COMP_MAX) + return NULL; + + if (compressors[id] == NULL) + return NULL; + + return compressors[id](compress, block_size); +} diff --git a/lib/comp/internal.h b/lib/comp/internal.h new file mode 100644 index 0000000..17fffe1 --- /dev/null +++ b/lib/comp/internal.h @@ -0,0 +1,11 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +#ifndef INTERNAL_H +#define INTERNAL_H + +#include "compress.h" + +compressor_t *create_lzma_compressor(bool compress, size_t block_size); + +compressor_t *create_zlib_compressor(bool compress, size_t block_size); + +#endif /* INTERNAL_H */ diff --git a/lib/comp/lzma.c b/lib/comp/lzma.c new file mode 100644 index 0000000..fd2a6c7 --- /dev/null +++ b/lib/comp/lzma.c @@ -0,0 +1,100 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +#include <stdbool.h> +#include <stdlib.h> +#include <string.h> +#include <stdio.h> +#include <lzma.h> + +#include "internal.h" + +typedef struct { + compressor_t base; + + size_t block_size; + uint8_t buffer[]; +} lzma_compressor_t; + +static ssize_t lzma_comp_block(compressor_t *base, uint8_t *block, size_t size) +{ + lzma_compressor_t *lzma = (lzma_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); + return -1; + } + + opt.dict_size = lzma->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, + block, size, lzma->buffer, + &written, lzma->block_size); + + if (ret == LZMA_OK) { + if (written >= size) + return 0; + + memcpy(block, lzma->buffer, size); + return written; + } + + if (ret != LZMA_BUF_ERROR) { + fputs("lzma block compress failed\n", stderr); + return -1; + } + + return 0; +} + +static ssize_t lzma_uncomp_block(compressor_t *base, uint8_t *block, + size_t size) +{ + lzma_compressor_t *lzma = (lzma_compressor_t *)base; + uint64_t memlimit = 32 * 1024 * 1024; + size_t dest_pos = 0; + size_t src_pos = 0; + lzma_ret ret; + + ret = lzma_stream_buffer_decode(&memlimit, 0, NULL, + block, &src_pos, size, + lzma->buffer, &dest_pos, + lzma->block_size); + + if (ret == LZMA_OK && size == src_pos) { + memcpy(block, lzma->buffer, dest_pos); + return (ssize_t)dest_pos; + } + + fputs("lzma block extract failed\n", stderr); + return -1; +} + +static void lzma_destroy(compressor_t *base) +{ + free(base); +} + +compressor_t *create_lzma_compressor(bool compress, size_t block_size) +{ + lzma_compressor_t *lzma = calloc(1, sizeof(*lzma) + block_size); + compressor_t *base = (compressor_t *)lzma; + + if (lzma == NULL) { + perror("creating lzma stream"); + return NULL; + } + + lzma->block_size = block_size; + base->destroy = lzma_destroy; + base->do_block = compress ? lzma_comp_block : lzma_uncomp_block; + return base; +} diff --git a/lib/comp/zlib.c b/lib/comp/zlib.c new file mode 100644 index 0000000..d3bfe70 --- /dev/null +++ b/lib/comp/zlib.c @@ -0,0 +1,108 @@ +/* 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; + uint8_t buffer[]; +} 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, uint8_t *block, size_t size) +{ + 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 *)block; + zlib->strm.avail_in = size; + zlib->strm.next_out = zlib->buffer; + zlib->strm.avail_out = zlib->block_size; + + 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; + + memcpy(block, zlib->buffer, written); + 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) + block_size); + 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; +} diff --git a/mkfs/Makemodule.am b/mkfs/Makemodule.am index 1d062b9..ad3f166 100644 --- a/mkfs/Makemodule.am +++ b/mkfs/Makemodule.am @@ -1,5 +1,13 @@ mksquashfs_SOURCES = mkfs/mksquashfs.c mkfs/options.c mkfs/options.h mksquashfs_SOURCES += include/squashfs.h -mksquashfs_LDADD = libfstree.a +mksquashfs_LDADD = libfstree.a libcompress.a + +if WITH_LZMA +mksquashfs_LDADD += $(XZ_LIBS) +endif + +if WITH_ZLIB +mksquashfs_LDADD += $(ZLIB_LIBS) +endif bin_PROGRAMS += mksquashfs diff --git a/mkfs/options.c b/mkfs/options.c index 513e76a..e3371aa 100644 --- a/mkfs/options.c +++ b/mkfs/options.c @@ -1,4 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later */ +#include "compress.h" #include "squashfs.h" #include "options.h" #include "config.h" @@ -221,6 +222,7 @@ static void process_defaults(options_t *opt, char *subopts) void process_command_line(options_t *opt, int argc, char **argv) { + bool have_compressor; int i; opt->def_uid = 0; @@ -241,12 +243,23 @@ void process_command_line(options_t *opt, int argc, char **argv) switch (i) { case 'c': + have_compressor = false; + for (i = SQFS_COMP_MIN; i <= SQFS_COMP_MAX; ++i) { if (strcmp(compressors[i], optarg) == 0) { - opt->compressor = i; - break; + if (compressor_exists(i)) { + have_compressor = true; + opt->compressor = i; + break; + } } } + + if (!have_compressor) { + fprintf(stderr, "Unsupported compressor '%s'\n", + optarg); + exit(EXIT_FAILURE); + } break; case 'b': opt->blksz = read_number("Block size", optarg, @@ -268,8 +281,10 @@ void process_command_line(options_t *opt, int argc, char **argv) fputs("Available compressors:\n", stdout); - for (i = SQFS_COMP_MIN; i <= SQFS_COMP_MAX; ++i) - printf("\t%s\n", compressors[i]); + for (i = SQFS_COMP_MIN; i <= SQFS_COMP_MAX; ++i) { + if (compressor_exists(i)) + printf("\t%s\n", compressors[i]); + } exit(EXIT_SUCCESS); case 'V': |