aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2020-06-04 07:24:52 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2020-06-04 07:24:52 +0200
commitb6d0bcbf1fa9c8088578c959f442e219f590ebf9 (patch)
tree2aaffddd7c46e1850b651796797c8df18622b14e
parent30597cc7c5d566f671c84c025de97cb1fc2d242e (diff)
Cleanup: Pull compression level parameter out into compressor config
Every compressor (except LC4) has a compression level parameter. This commit pulls the compression level field out into the generic configuration structure and applies some code clean ups as a result from this. Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
-rw-r--r--include/sqfs/compressor.h52
-rw-r--r--lib/common/comp_lzo.c8
-rw-r--r--lib/common/comp_opt.c21
-rw-r--r--lib/sqfs/comp/compressor.c14
-rw-r--r--lib/sqfs/comp/gzip.c10
-rw-r--r--lib/sqfs/comp/lz4.c3
-rw-r--r--lib/sqfs/comp/lzma.c6
-rw-r--r--lib/sqfs/comp/xz.c6
-rw-r--r--lib/sqfs/comp/zstd.c8
9 files changed, 37 insertions, 91 deletions
diff --git a/include/sqfs/compressor.h b/include/sqfs/compressor.h
index 7e687f6..87ecc9d 100644
--- a/include/sqfs/compressor.h
+++ b/include/sqfs/compressor.h
@@ -120,6 +120,13 @@ struct sqfs_compressor_config_t {
sqfs_u32 block_size;
/**
+ * @brief Compression level.
+ *
+ * Valid range and default value depend on the selected compressor.
+ */
+ sqfs_u32 level;
+
+ /**
* @brief Backend specific options for fine tuing.
*/
union {
@@ -128,37 +135,16 @@ struct sqfs_compressor_config_t {
*/
struct {
/**
- * @brief Compression level. Value between 1 and 9.
- *
- * Default is 9, i.e. best compression.
- */
- sqfs_u16 level;
-
- /**
* @brief Deflate window size. Value between 8 and 15.
*
* Default is 15, i.e. 32k window.
*/
sqfs_u16 window_size;
- sqfs_u32 padd0[3];
+ sqfs_u8 padd0[14];
} gzip;
/**
- * @brief Options for the zstd compressor.
- */
- struct {
- /**
- * @brief Compression level. Value between 1 and 22.
- *
- * Default is 15.
- */
- sqfs_u16 level;
-
- sqfs_u16 padd0[7];
- } zstd;
-
- /**
* @brief Options for the lzo compressor.
*/
struct {
@@ -170,18 +156,7 @@ struct sqfs_compressor_config_t {
*/
sqfs_u16 algorithm;
- /**
- * @brief Compression level for @ref SQFS_LZO1X_999.
- *
- * If the selected algorithm is @ref SQFS_LZO1X_999,
- * this can be a value between 0 and 9. For all other
- * algorithms it has to be 0.
- *
- * Defaults to 9, i.e. best compression.
- */
- sqfs_u16 level;
-
- sqfs_u32 padd0[3];
+ sqfs_u8 padd0[14];
} lzo;
/**
@@ -200,13 +175,6 @@ struct sqfs_compressor_config_t {
sqfs_u32 dict_size;
/**
- * @brief Compression level. Maximum 9.
- *
- * For LZMA, the default is 5, for XZ the default is 6.
- */
- sqfs_u8 level;
-
- /**
* @brief Number of literal context bits.
*
* How many of the highest bits of the previous
@@ -244,7 +212,7 @@ struct sqfs_compressor_config_t {
*/
sqfs_u8 pb;
- sqfs_u32 padd0[2];
+ sqfs_u8 padd0[9];
} xz, lzma;
sqfs_u64 padd0[2];
diff --git a/lib/common/comp_lzo.c b/lib/common/comp_lzo.c
index 0956e0e..3452647 100644
--- a/lib/common/comp_lzo.c
+++ b/lib/common/comp_lzo.c
@@ -193,7 +193,7 @@ static void lzo_get_configuration(const sqfs_compressor_t *base,
cfg->block_size = lzo->block_size;
cfg->opt.lzo.algorithm = lzo->algorithm;
- cfg->opt.lzo.level = lzo->level;
+ cfg->level = lzo->level;
if (base->do_block == lzo_uncomp_block)
cfg->flags |= SQFS_COMP_FLAG_UNCOMPRESS;
@@ -233,9 +233,9 @@ int lzo_compressor_create(const sqfs_compressor_config_t *cfg,
}
if (cfg->opt.lzo.algorithm == SQFS_LZO1X_999) {
- if (cfg->opt.lzo.level > SQFS_LZO_MAX_LEVEL)
+ if (cfg->level > SQFS_LZO_MAX_LEVEL)
return SQFS_ERROR_UNSUPPORTED;
- } else if (cfg->opt.lzo.level != 0) {
+ } else if (cfg->level != 0) {
return SQFS_ERROR_UNSUPPORTED;
}
@@ -262,7 +262,7 @@ int lzo_compressor_create(const sqfs_compressor_config_t *cfg,
lzo->block_size = cfg->block_size;
lzo->algorithm = cfg->opt.lzo.algorithm;
- lzo->level = cfg->opt.lzo.level;
+ lzo->level = cfg->level;
lzo->buf_size = scratch_size;
lzo->work_size = lzo_algs[cfg->opt.lzo.algorithm].bufsize;
diff --git a/lib/common/comp_opt.c b/lib/common/comp_opt.c
index 8864a0e..5be4466 100644
--- a/lib/common/comp_opt.c
+++ b/lib/common/comp_opt.c
@@ -185,29 +185,10 @@ int compressor_cfg_init_options(sqfs_compressor_config_t *cfg,
goto fail_level;
level = atoi(value);
-
if (level < min_level || level > max_level)
goto fail_level;
- switch (cfg->id) {
- case SQFS_COMP_GZIP:
- cfg->opt.gzip.level = level;
- break;
- case SQFS_COMP_LZO:
- cfg->opt.lzo.level = level;
- break;
- case SQFS_COMP_ZSTD:
- cfg->opt.zstd.level = level;
- break;
- case SQFS_COMP_XZ:
- cfg->opt.xz.level = level;
- break;
- case SQFS_COMP_LZMA:
- cfg->opt.lzma.level = level;
- break;
- default:
- goto fail_opt;
- }
+ cfg->level = level;
break;
case OPT_ALG:
if (cfg->id != SQFS_COMP_LZO)
diff --git a/lib/sqfs/comp/compressor.c b/lib/sqfs/comp/compressor.c
index ff7ce0c..42ee436 100644
--- a/lib/sqfs/comp/compressor.c
+++ b/lib/sqfs/comp/compressor.c
@@ -106,10 +106,6 @@ int sqfs_compressor_create(const sqfs_compressor_config_t *cfg,
ret = memcmp(cfg->opt.lzo.padd0, padd0,
sizeof(cfg->opt.lzo.padd0));
break;
- case SQFS_COMP_ZSTD:
- ret = memcmp(cfg->opt.zstd.padd0, padd0,
- sizeof(cfg->opt.zstd.padd0));
- break;
case SQFS_COMP_GZIP:
ret = memcmp(cfg->opt.gzip.padd0, padd0,
sizeof(cfg->opt.gzip.padd0));
@@ -156,20 +152,20 @@ int sqfs_compressor_config_init(sqfs_compressor_config_t *cfg,
switch (id) {
case SQFS_COMP_GZIP:
flag_mask |= SQFS_COMP_FLAG_GZIP_ALL;
- cfg->opt.gzip.level = SQFS_GZIP_DEFAULT_LEVEL;
+ cfg->level = SQFS_GZIP_DEFAULT_LEVEL;
cfg->opt.gzip.window_size = SQFS_GZIP_DEFAULT_WINDOW;
break;
case SQFS_COMP_LZO:
cfg->opt.lzo.algorithm = SQFS_LZO_DEFAULT_ALG;
- cfg->opt.lzo.level = SQFS_LZO_DEFAULT_LEVEL;
+ cfg->level = SQFS_LZO_DEFAULT_LEVEL;
break;
case SQFS_COMP_ZSTD:
- cfg->opt.zstd.level = SQFS_ZSTD_DEFAULT_LEVEL;
+ cfg->level = SQFS_ZSTD_DEFAULT_LEVEL;
break;
case SQFS_COMP_XZ:
flag_mask |= SQFS_COMP_FLAG_XZ_ALL;
+ cfg->level = SQFS_XZ_DEFAULT_LEVEL;
cfg->opt.xz.dict_size = block_size;
- cfg->opt.xz.level = SQFS_XZ_DEFAULT_LEVEL;
cfg->opt.xz.lc = SQFS_XZ_DEFAULT_LC;
cfg->opt.xz.lp = SQFS_XZ_DEFAULT_LP;
cfg->opt.xz.pb = SQFS_XZ_DEFAULT_PB;
@@ -179,8 +175,8 @@ int sqfs_compressor_config_init(sqfs_compressor_config_t *cfg,
break;
case SQFS_COMP_LZMA:
flag_mask |= SQFS_COMP_FLAG_LZMA_ALL;
+ cfg->level = SQFS_LZMA_DEFAULT_LEVEL;
cfg->opt.lzma.dict_size = block_size;
- cfg->opt.lzma.level = SQFS_LZMA_DEFAULT_LEVEL;
cfg->opt.lzma.lc = SQFS_LZMA_DEFAULT_LC;
cfg->opt.lzma.lp = SQFS_LZMA_DEFAULT_LP;
cfg->opt.lzma.pb = SQFS_LZMA_DEFAULT_PB;
diff --git a/lib/sqfs/comp/gzip.c b/lib/sqfs/comp/gzip.c
index d702da1..9be84db 100644
--- a/lib/sqfs/comp/gzip.c
+++ b/lib/sqfs/comp/gzip.c
@@ -53,7 +53,7 @@ static void gzip_get_configuration(const sqfs_compressor_t *base,
cfg->id = SQFS_COMP_GZIP;
cfg->flags = gzip->opt.strategies;
cfg->block_size = gzip->block_size;
- cfg->opt.gzip.level = gzip->opt.level;
+ cfg->level = gzip->opt.level;
cfg->opt.gzip.window_size = gzip->opt.window;
if (!gzip->compress)
@@ -259,8 +259,8 @@ int gzip_compressor_create(const sqfs_compressor_config_t *cfg,
return SQFS_ERROR_UNSUPPORTED;
}
- if (cfg->opt.gzip.level < SQFS_GZIP_MIN_LEVEL ||
- cfg->opt.gzip.level > SQFS_GZIP_MAX_LEVEL) {
+ if (cfg->level < SQFS_GZIP_MIN_LEVEL ||
+ cfg->level > SQFS_GZIP_MAX_LEVEL) {
return SQFS_ERROR_UNSUPPORTED;
}
@@ -275,7 +275,7 @@ int gzip_compressor_create(const sqfs_compressor_config_t *cfg,
if (gzip == NULL)
return SQFS_ERROR_ALLOC;
- gzip->opt.level = cfg->opt.gzip.level;
+ gzip->opt.level = cfg->level;
gzip->opt.window = cfg->opt.gzip.window_size;
gzip->opt.strategies = cfg->flags & SQFS_COMP_FLAG_GZIP_ALL;
gzip->compress = (cfg->flags & SQFS_COMP_FLAG_UNCOMPRESS) == 0;
@@ -288,7 +288,7 @@ int gzip_compressor_create(const sqfs_compressor_config_t *cfg,
((sqfs_object_t *)base)->destroy = gzip_destroy;
if (gzip->compress) {
- ret = deflateInit2(&gzip->strm, cfg->opt.gzip.level,
+ ret = deflateInit2(&gzip->strm, cfg->level,
Z_DEFLATED, cfg->opt.gzip.window_size, 8,
Z_DEFAULT_STRATEGY);
} else {
diff --git a/lib/sqfs/comp/lz4.c b/lib/sqfs/comp/lz4.c
index 090ec50..f326870 100644
--- a/lib/sqfs/comp/lz4.c
+++ b/lib/sqfs/comp/lz4.c
@@ -148,6 +148,9 @@ int lz4_compressor_create(const sqfs_compressor_config_t *cfg,
return SQFS_ERROR_UNSUPPORTED;
}
+ if (cfg->level != 0)
+ return SQFS_ERROR_UNSUPPORTED;
+
lz4 = calloc(1, sizeof(*lz4));
base = (sqfs_compressor_t *)lz4;
if (lz4 == NULL)
diff --git a/lib/sqfs/comp/lzma.c b/lib/sqfs/comp/lzma.c
index b674af9..01e6042 100644
--- a/lib/sqfs/comp/lzma.c
+++ b/lib/sqfs/comp/lzma.c
@@ -185,8 +185,8 @@ static void lzma_get_configuration(const sqfs_compressor_t *base,
cfg->id = SQFS_COMP_LZMA;
cfg->block_size = lzma->block_size;
cfg->flags = lzma->flags;
+ cfg->level = lzma->level;
cfg->opt.lzma.dict_size = lzma->dict_size;
- cfg->opt.lzma.level = lzma->level;
cfg->opt.lzma.lc = lzma->lc;
cfg->opt.lzma.lp = lzma->lp;
cfg->opt.lzma.pb = lzma->pb;
@@ -220,7 +220,7 @@ int lzma_compressor_create(const sqfs_compressor_config_t *cfg,
return SQFS_ERROR_UNSUPPORTED;
/* XXX: values are unsigned and minimum is 0 */
- if (cfg->opt.lzma.level > SQFS_LZMA_MAX_LEVEL)
+ if (cfg->level > SQFS_LZMA_MAX_LEVEL)
return SQFS_ERROR_UNSUPPORTED;
if (cfg->opt.lzma.lc > SQFS_LZMA_MAX_LC)
@@ -262,8 +262,8 @@ int lzma_compressor_create(const sqfs_compressor_config_t *cfg,
lzma->block_size = cfg->block_size;
lzma->flags = cfg->flags;
+ lzma->level = cfg->level;
lzma->dict_size = cfg->opt.lzma.dict_size;
- lzma->level = cfg->opt.lzma.level;
lzma->lc = cfg->opt.lzma.lc;
lzma->lp = cfg->opt.lzma.lp;
lzma->pb = cfg->opt.lzma.pb;
diff --git a/lib/sqfs/comp/xz.c b/lib/sqfs/comp/xz.c
index 662a49f..7d7991d 100644
--- a/lib/sqfs/comp/xz.c
+++ b/lib/sqfs/comp/xz.c
@@ -242,8 +242,8 @@ static void xz_get_configuration(const sqfs_compressor_t *base,
cfg->id = SQFS_COMP_XZ;
cfg->flags = xz->flags;
cfg->block_size = xz->block_size;
+ cfg->level = xz->level;
cfg->opt.xz.dict_size = xz->dict_size;
- cfg->opt.xz.level = xz->level;
cfg->opt.xz.lc = xz->lc;
cfg->opt.xz.lp = xz->lp;
cfg->opt.xz.pb = xz->pb;
@@ -288,7 +288,7 @@ int xz_compressor_create(const sqfs_compressor_config_t *cfg,
if (cfg->opt.xz.pb > SQFS_XZ_MAX_PB)
return SQFS_ERROR_UNSUPPORTED;
- if (cfg->opt.xz.level > SQFS_XZ_MAX_LEVEL)
+ if (cfg->level > SQFS_XZ_MAX_LEVEL)
return SQFS_ERROR_UNSUPPORTED;
if (cfg->opt.xz.dict_size < SQFS_XZ_MIN_DICT_SIZE)
@@ -308,7 +308,7 @@ int xz_compressor_create(const sqfs_compressor_config_t *cfg,
xz->lc = cfg->opt.xz.lc;
xz->lp = cfg->opt.xz.lp;
xz->pb = cfg->opt.xz.pb;
- xz->level = cfg->opt.xz.level;
+ xz->level = cfg->level;
base->get_configuration = xz_get_configuration;
base->do_block = (cfg->flags & SQFS_COMP_FLAG_UNCOMPRESS) ?
xz_uncomp_block : xz_comp_block;
diff --git a/lib/sqfs/comp/zstd.c b/lib/sqfs/comp/zstd.c
index 18289fe..af5bd84 100644
--- a/lib/sqfs/comp/zstd.c
+++ b/lib/sqfs/comp/zstd.c
@@ -101,7 +101,7 @@ static void zstd_get_configuration(const sqfs_compressor_t *base,
cfg->id = SQFS_COMP_ZSTD;
cfg->block_size = zstd->block_size;
- cfg->opt.zstd.level = zstd->level;
+ cfg->level = zstd->level;
if (base->do_block == zstd_uncomp_block)
cfg->flags |= SQFS_COMP_FLAG_UNCOMPRESS;
@@ -143,10 +143,8 @@ int zstd_compressor_create(const sqfs_compressor_config_t *cfg,
if (cfg->flags & ~SQFS_COMP_FLAG_GENERIC_ALL)
return SQFS_ERROR_UNSUPPORTED;
- if (cfg->opt.zstd.level < 1 ||
- cfg->opt.zstd.level > ZSTD_maxCLevel()) {
+ if (cfg->level < 1 || cfg->level > (unsigned int)ZSTD_maxCLevel())
return SQFS_ERROR_UNSUPPORTED;
- }
zstd = calloc(1, sizeof(*zstd));
base = (sqfs_compressor_t *)zstd;
@@ -154,7 +152,7 @@ int zstd_compressor_create(const sqfs_compressor_config_t *cfg,
return SQFS_ERROR_ALLOC;
zstd->block_size = cfg->block_size;
- zstd->level = cfg->opt.zstd.level;
+ zstd->level = cfg->level;
zstd->zctx = ZSTD_createCCtx();
if (zstd->zctx == NULL) {
free(zstd);