summaryrefslogtreecommitdiff
path: root/lib/sqfs/comp/lzo.c
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-09-01 22:42:49 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-09-01 22:42:49 +0200
commit307107ecd2fc3ffbf6fe91497daf767700f3572f (patch)
tree87c2c5993ab10cd4aa791a4e6d34f251db208ed2 /lib/sqfs/comp/lzo.c
parent2e28c45601a57b1d23e9cad33d2bdcc59e8a3f4f (diff)
Move command line processing stuff out of compressor code
This commit moves stuff like printing help text, command line option processing and enumerating available processors on stdout out of the generic compressor code. The option string is replaced with a structure that directly exposese the tweakable parameters for all compressors. A function for parsing the command line arguments into this structure is added in sqfshelper. Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/sqfs/comp/lzo.c')
-rw-r--r--lib/sqfs/comp/lzo.c176
1 files changed, 44 insertions, 132 deletions
diff --git a/lib/sqfs/comp/lzo.c b/lib/sqfs/comp/lzo.c
index 09ef75c..3293af1 100644
--- a/lib/sqfs/comp/lzo.c
+++ b/lib/sqfs/comp/lzo.c
@@ -16,49 +16,32 @@
#include "internal.h"
-
-typedef enum {
- LZO_ALGORITHM_LZO1X_1 = 0,
- LZO_ALGORITHM_LZO1X_1_11 = 1,
- LZO_ALGORITHM_LZO1X_1_12 = 2,
- LZO_ALGORITHM_LZO1X_1_15 = 3,
- LZO_ALGORITHM_LZO1X_999 = 4,
-} LZO_ALGORITHM;
-
-#define LZO_DEFAULT_ALG LZO_ALGORITHM_LZO1X_999
-#define LZO_DEFAULT_LEVEL 8
#define LZO_NUM_ALGS (sizeof(lzo_algs) / sizeof(lzo_algs[0]))
typedef int (*lzo_cb_t)(const lzo_bytep src, lzo_uint src_len, lzo_bytep dst,
lzo_uintp dst_len, lzo_voidp wrkmem);
static const struct {
- const char *name;
lzo_cb_t compress;
size_t bufsize;
} lzo_algs[] = {
- [LZO_ALGORITHM_LZO1X_1] = {
- .name = "lzo1x_1",
+ [SQFS_LZO1X_1] = {
.compress = lzo1x_1_compress,
.bufsize = LZO1X_1_MEM_COMPRESS,
},
- [LZO_ALGORITHM_LZO1X_1_11] = {
- .name = "lzo1x_1_11",
+ [SQFS_LZO1X_1_11] = {
.compress = lzo1x_1_11_compress,
.bufsize = LZO1X_1_11_MEM_COMPRESS,
},
- [LZO_ALGORITHM_LZO1X_1_12] = {
- .name = "lzo1x_1_12",
+ [SQFS_LZO1X_1_12] = {
.compress = lzo1x_1_12_compress,
.bufsize = LZO1X_1_12_MEM_COMPRESS,
},
- [LZO_ALGORITHM_LZO1X_1_15] = {
- .name = "lzo1x_1_15",
+ [SQFS_LZO1X_1_15] = {
.compress = lzo1x_1_15_compress,
.bufsize = LZO1X_1_15_MEM_COMPRESS,
},
- [LZO_ALGORITHM_LZO1X_999] = {
- .name = "lzo1x_999",
+ [SQFS_LZO1X_999] = {
.compress = lzo1x_999_compress,
.bufsize = LZO1X_999_MEM_COMPRESS,
},
@@ -82,14 +65,14 @@ static int lzo_write_options(compressor_t *base, int fd)
lzo_compressor_t *lzo = (lzo_compressor_t *)base;
lzo_options_t opt;
- if (lzo->algorithm == LZO_DEFAULT_ALG &&
- lzo->level == LZO_DEFAULT_LEVEL) {
+ if (lzo->algorithm == SQFS_LZO_DEFAULT_ALG &&
+ lzo->level == SQFS_LZO_DEFAULT_LEVEL) {
return 0;
}
opt.algorithm = htole32(lzo->algorithm);
- if (lzo->algorithm == LZO_ALGORITHM_LZO1X_999) {
+ if (lzo->algorithm == SQFS_LZO1X_999) {
opt.level = htole32(lzo->level);
} else {
opt.level = 0;
@@ -110,14 +93,14 @@ static int lzo_read_options(compressor_t *base, int fd)
lzo->level = le32toh(opt.level);
switch(lzo->algorithm) {
- case LZO_ALGORITHM_LZO1X_1:
- case LZO_ALGORITHM_LZO1X_1_11:
- case LZO_ALGORITHM_LZO1X_1_12:
- case LZO_ALGORITHM_LZO1X_1_15:
+ case SQFS_LZO1X_1:
+ case SQFS_LZO1X_1_11:
+ case SQFS_LZO1X_1_12:
+ case SQFS_LZO1X_1_15:
if (lzo->level != 0)
goto fail_level;
break;
- case LZO_ALGORITHM_LZO1X_999:
+ case SQFS_LZO1X_999:
if (lzo->level < 1 || lzo->level > 9)
goto fail_level;
break;
@@ -139,8 +122,8 @@ static ssize_t lzo_comp_block(compressor_t *base, const uint8_t *in,
lzo_uint len = outsize;
int ret;
- if (lzo->algorithm == LZO_ALGORITHM_LZO1X_999 &&
- lzo->level != LZO_DEFAULT_LEVEL) {
+ if (lzo->algorithm == SQFS_LZO1X_999 &&
+ lzo->level != SQFS_LZO_DEFAULT_LEVEL) {
ret = lzo1x_999_compress_level(in, size, out, &len,
lzo->buffer, NULL, 0, 0,
lzo->level);
@@ -198,91 +181,38 @@ static void lzo_destroy(compressor_t *base)
free(base);
}
-static int process_options(char *options, int *algorithm, int *level)
-{
- enum {
- OPT_ALG = 0,
- OPT_LEVEL,
- };
- char *const token[] = {
- [OPT_ALG] = (char *)"algorithm",
- [OPT_LEVEL] = (char *)"level",
- NULL
- };
- char *subopts, *value;
- size_t i;
- int opt;
-
- subopts = options;
-
- while (*subopts != '\0') {
- opt = getsubopt(&subopts, token, &value);
-
- switch (opt) {
- case OPT_ALG:
- if (value == NULL)
- goto fail_value;
-
- for (i = 0; i < LZO_NUM_ALGS; ++i) {
- if (strcmp(lzo_algs[i].name, value) == 0) {
- *algorithm = i;
- break;
- }
- }
-
- if (i == LZO_NUM_ALGS) {
- fprintf(stderr, "Unknown lzo variant '%s'.\n",
- value);
- return -1;
- }
- break;
- case OPT_LEVEL:
- if (value == NULL)
- goto fail_value;
-
- for (i = 0; isdigit(value[i]); ++i)
- ;
-
- if (i < 1 || i > 3 || value[i] != '\0')
- goto fail_level;
-
- *level = atoi(value);
-
- if (*level < 1 || *level > 9)
- goto fail_level;
- break;
- default:
- goto fail_opt;
- }
- }
-
- return 0;
-fail_level:
- fputs("Compression level must be a number between 1 and 9.\n", stderr);
- return -1;
-fail_opt:
- fprintf(stderr, "Unknown option '%s'.\n", value);
- return -1;
-fail_value:
- fprintf(stderr, "Missing value for '%s'.\n", token[opt]);
- return -1;
-}
-
-compressor_t *create_lzo_compressor(bool compress, size_t block_size,
- char *options)
+compressor_t *create_lzo_compressor(const compressor_config_t *cfg)
{
lzo_compressor_t *lzo;
compressor_t *base;
- int level, alg;
- (void)block_size;
- alg = LZO_DEFAULT_ALG;
- level = LZO_DEFAULT_LEVEL;
+ if (cfg->flags & ~SQFS_COMP_FLAG_GENERIC_ALL) {
+ fputs("creating lzo compressor: unknown compressor flags\n",
+ stderr);
+ return NULL;
+ }
- if (options != NULL && process_options(options, &alg, &level) != 0)
+ if (cfg->opt.lzo.algorithm > LZO_NUM_ALGS ||
+ lzo_algs[cfg->opt.lzo.algorithm].compress == NULL) {
+ fputs("creating lzo compressor: unknown LZO variant\n",
+ stderr);
return NULL;
+ }
- lzo = alloc_flex(sizeof(*lzo), 1, lzo_algs[alg].bufsize);
+ if (cfg->opt.lzo.algorithm == SQFS_LZO1X_999) {
+ if (cfg->opt.lzo.level > SQFS_LZO_MAX_LEVEL) {
+ fputs("creating lzo compressor: compression level "
+ "must be between 0 and 9 inclusive\n", stderr);
+ return NULL;
+ }
+ } else if (cfg->opt.lzo.level != 0) {
+ fputs("creating lzo compressor: level argument "
+ "only supported by lzo1x 999\n", stderr);
+ return NULL;
+ }
+
+ lzo = alloc_flex(sizeof(*lzo), 1,
+ lzo_algs[cfg->opt.lzo.algorithm].bufsize);
base = (compressor_t *)lzo;
if (lzo == NULL) {
@@ -290,32 +220,14 @@ compressor_t *create_lzo_compressor(bool compress, size_t block_size,
return NULL;
}
- lzo->algorithm = alg;
- lzo->level = level;
+ lzo->algorithm = cfg->opt.lzo.algorithm;
+ lzo->level = cfg->opt.lzo.level;
base->destroy = lzo_destroy;
- base->do_block = compress ? lzo_comp_block : lzo_uncomp_block;
+ base->do_block = (cfg->flags & SQFS_COMP_FLAG_UNCOMPRESS) ?
+ lzo_uncomp_block : lzo_comp_block;
base->write_options = lzo_write_options;
base->read_options = lzo_read_options;
base->create_copy = lzo_create_copy;
return base;
}
-
-void compressor_lzo_print_help(void)
-{
- size_t i;
-
- fputs("Available options for lzo compressor:\n"
- "\n"
- " algorithm=<name> Specify the variant of lzo to use.\n"
- " Defaults to 'lzo1x_999'.\n"
- " level=<value> For lzo1x_999, the compression level.\n"
- " Value from 1 to 9. Defaults to 8.\n"
- " Ignored if algorithm is not lzo1x_999.\n"
- "\n"
- "Available algorithms:\n",
- stdout);
-
- for (i = 0; i < LZO_NUM_ALGS; ++i)
- printf("\t%s\n", lzo_algs[i].name);
-}