From 60064dd0412a149fe00cfc4e2f2361c22656db57 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Sat, 7 Sep 2019 20:19:05 +0200 Subject: Remove printing to stderr in libsquashfs with returning error numbers Signed-off-by: David Oberhollenzer --- lib/sqfs/comp/compressor.c | 12 +++---- lib/sqfs/comp/gzip.c | 84 +++++++++++++--------------------------------- lib/sqfs/comp/internal.h | 1 + lib/sqfs/comp/lz4.c | 36 +++++++------------- lib/sqfs/comp/lzo.c | 53 +++++++++-------------------- lib/sqfs/comp/xz.c | 52 ++++++++++------------------ lib/sqfs/comp/zstd.c | 45 +++++++------------------ 7 files changed, 87 insertions(+), 196 deletions(-) (limited to 'lib/sqfs/comp') diff --git a/lib/sqfs/comp/compressor.c b/lib/sqfs/comp/compressor.c index 005dbd4..4d073f2 100644 --- a/lib/sqfs/comp/compressor.c +++ b/lib/sqfs/comp/compressor.c @@ -9,7 +9,6 @@ #include #include -#include #include "internal.h" #include "util.h" @@ -53,7 +52,7 @@ int sqfs_generic_write_options(int fd, const void *data, size_t size) if (write_data("writing compressor options", fd, buffer, sizeof(buffer))) { - return -1; + return SQFS_ERROR_IO; } return sizeof(buffer); @@ -65,14 +64,11 @@ int sqfs_generic_read_options(int fd, void *data, size_t size) if (read_data_at("reading compressor options", sizeof(sqfs_super_t), fd, buffer, sizeof(buffer))) { - return -1; + return SQFS_ERROR_IO; } - if (le16toh(*((uint16_t *)buffer)) != (0x8000 | size)) { - fputs("reading compressor options: invalid meta data header\n", - stderr); - return -1; - } + if (le16toh(*((uint16_t *)buffer)) != (0x8000 | size)) + return SQFS_ERROR_CORRUPTED; memcpy(data, buffer + 2, size); return 0; diff --git a/lib/sqfs/comp/gzip.c b/lib/sqfs/comp/gzip.c index e80073d..484901a 100644 --- a/lib/sqfs/comp/gzip.c +++ b/lib/sqfs/comp/gzip.c @@ -10,7 +10,6 @@ #include #include #include -#include #include #include @@ -67,30 +66,24 @@ static int gzip_read_options(sqfs_compressor_t *base, int fd) { gzip_compressor_t *gzip = (gzip_compressor_t *)base; gzip_options_t opt; + int ret; - if (sqfs_generic_read_options(fd, &opt, sizeof(opt))) - return -1; + ret = sqfs_generic_read_options(fd, &opt, sizeof(opt)); + if (ret) + return ret; gzip->opt.level = le32toh(opt.level); gzip->opt.window = le16toh(opt.window); gzip->opt.strategies = le16toh(opt.strategies); - if (gzip->opt.level < 1 || gzip->opt.level > 9) { - fprintf(stderr, "Invalid gzip compression level '%d'.\n", - gzip->opt.level); - return -1; - } + if (gzip->opt.level < 1 || gzip->opt.level > 9) + return SQFS_ERROR_UNSUPPORTED; - if (gzip->opt.window < 8 || gzip->opt.window > 15) { - fprintf(stderr, "Invalid gzip window size '%d'.\n", - gzip->opt.window); - return -1; - } + if (gzip->opt.window < 8 || gzip->opt.window > 15) + return SQFS_ERROR_UNSUPPORTED; - if (gzip->opt.strategies & ~SQFS_COMP_FLAG_GZIP_ALL) { - fputs("Unknown gzip strategies selected.\n", stderr); - return -1; - } + if (gzip->opt.strategies & ~SQFS_COMP_FLAG_GZIP_ALL) + return SQFS_ERROR_UNSUPPORTED; return 0; } @@ -124,11 +117,8 @@ static int find_strategy(gzip_compressor_t *gzip, const uint8_t *in, continue; ret = deflateReset(&gzip->strm); - if (ret != Z_OK) { - fputs("resetting zlib stream failed\n", - stderr); - return -1; - } + if (ret != Z_OK) + return SQFS_ERROR_COMRPESSOR; strategy = flag_to_zlib_strategy(i); @@ -138,11 +128,8 @@ static int find_strategy(gzip_compressor_t *gzip, const uint8_t *in, gzip->strm.avail_out = outsize; ret = deflateParams(&gzip->strm, gzip->opt.level, strategy); - if (ret != Z_OK) { - fputs("setting deflate parameters failed\n", - stderr); - return -1; - } + if (ret != Z_OK) + return SQFS_ERROR_COMRPESSOR; ret = deflate(&gzip->strm, Z_FINISH); @@ -154,8 +141,7 @@ static int find_strategy(gzip_compressor_t *gzip, const uint8_t *in, selected = strategy; } } else if (ret != Z_OK && ret != Z_BUF_ERROR) { - fputs("gzip block processing failed\n", stderr); - return -1; + return SQFS_ERROR_COMRPESSOR; } } @@ -172,7 +158,7 @@ static ssize_t gzip_do_block(sqfs_compressor_t *base, const uint8_t *in, if (gzip->compress && gzip->opt.strategies != 0) { strategy = find_strategy(gzip, in, size, out, outsize); if (strategy < 0) - return -1; + return strategy; } if (gzip->compress) { @@ -181,10 +167,8 @@ static ssize_t gzip_do_block(sqfs_compressor_t *base, const uint8_t *in, ret = inflateReset(&gzip->strm); } - if (ret != Z_OK) { - fputs("resetting zlib stream failed\n", stderr); - return -1; - } + if (ret != Z_OK) + return SQFS_ERROR_COMRPESSOR; gzip->strm.next_in = (void *)in; gzip->strm.avail_in = size; @@ -193,11 +177,8 @@ static ssize_t gzip_do_block(sqfs_compressor_t *base, const uint8_t *in, if (gzip->compress && gzip->opt.strategies != 0) { ret = deflateParams(&gzip->strm, gzip->opt.level, strategy); - if (ret != Z_OK) { - fputs("setting selcted deflate parameters failed\n", - stderr); - return -1; - } + if (ret != Z_OK) + return SQFS_ERROR_COMRPESSOR; } if (gzip->compress) { @@ -215,10 +196,8 @@ static ssize_t gzip_do_block(sqfs_compressor_t *base, const uint8_t *in, return (ssize_t)written; } - if (ret != Z_OK && ret != Z_BUF_ERROR) { - fputs("gzip block processing failed\n", stderr); - return -1; - } + if (ret != Z_OK && ret != Z_BUF_ERROR) + return SQFS_ERROR_COMRPESSOR; return 0; } @@ -228,10 +207,8 @@ static sqfs_compressor_t *gzip_create_copy(sqfs_compressor_t *cmp) gzip_compressor_t *gzip = malloc(sizeof(*gzip)); int ret; - if (gzip == NULL) { - perror("creating additional gzip compressor"); + if (gzip == NULL) return NULL; - } memcpy(gzip, cmp, sizeof(*gzip)); memset(&gzip->strm, 0, sizeof(gzip->strm)); @@ -244,8 +221,6 @@ static sqfs_compressor_t *gzip_create_copy(sqfs_compressor_t *cmp) } if (ret != Z_OK) { - fputs("internal error creating additional zlib stream\n", - stderr); free(gzip); return NULL; } @@ -261,34 +236,24 @@ sqfs_compressor_t *gzip_compressor_create(const sqfs_compressor_config_t *cfg) if (cfg->flags & ~(SQFS_COMP_FLAG_GZIP_ALL | SQFS_COMP_FLAG_GENERIC_ALL)) { - fputs("creating gzip compressor: unknown compressor flags\n", - stderr); return NULL; } if (cfg->opt.gzip.level < SQFS_GZIP_MIN_LEVEL || cfg->opt.gzip.level > SQFS_GZIP_MAX_LEVEL) { - fprintf(stderr, "creating gzip compressor: compression level" - "must be between %d and %d inclusive\n", - SQFS_GZIP_MIN_LEVEL, SQFS_GZIP_MAX_LEVEL); return NULL; } if (cfg->opt.gzip.window_size < SQFS_GZIP_MIN_WINDOW || cfg->opt.gzip.window_size > SQFS_GZIP_MAX_WINDOW) { - fprintf(stderr, "creating gzip compressor: window size" - "must be between %d and %d inclusive\n", - SQFS_GZIP_MIN_WINDOW, SQFS_GZIP_MAX_WINDOW); return NULL; } gzip = calloc(1, sizeof(*gzip)); base = (sqfs_compressor_t *)gzip; - if (gzip == NULL) { - perror("creating gzip compressor"); + if (gzip == NULL) return NULL; - } gzip->opt.level = cfg->opt.gzip.level; gzip->opt.window = cfg->opt.gzip.window_size; @@ -310,7 +275,6 @@ sqfs_compressor_t *gzip_compressor_create(const sqfs_compressor_config_t *cfg) } if (ret != Z_OK) { - fputs("internal error creating zlib stream\n", stderr); free(gzip); return NULL; } diff --git a/lib/sqfs/comp/internal.h b/lib/sqfs/comp/internal.h index 117bb82..d2410fc 100644 --- a/lib/sqfs/comp/internal.h +++ b/lib/sqfs/comp/internal.h @@ -11,6 +11,7 @@ #include "sqfs/predef.h" #include "sqfs/compress.h" +#include "sqfs/error.h" #include "util.h" SQFS_INTERNAL diff --git a/lib/sqfs/comp/lz4.c b/lib/sqfs/comp/lz4.c index 0893b34..446b83e 100644 --- a/lib/sqfs/comp/lz4.c +++ b/lib/sqfs/comp/lz4.c @@ -10,7 +10,6 @@ #include #include #include -#include #include #include @@ -44,18 +43,18 @@ static int lz4_write_options(sqfs_compressor_t *base, int fd) static int lz4_read_options(sqfs_compressor_t *base, int fd) { lz4_options opt; + int ret; (void)base; - if (sqfs_generic_read_options(fd, &opt, sizeof(opt))) - return -1; + ret = sqfs_generic_read_options(fd, &opt, sizeof(opt)); + if (ret) + return ret; opt.version = le32toh(opt.version); opt.flags = le32toh(opt.flags); - if (opt.version != LZ4LEGACY) { - fprintf(stderr, "unsupported lz4 version '%d'\n", opt.version); - return -1; - } + if (opt.version != LZ4LEGACY) + return SQFS_ERROR_UNSUPPORTED; return 0; } @@ -74,10 +73,8 @@ static ssize_t lz4_comp_block(sqfs_compressor_t *base, const uint8_t *in, size, outsize); } - if (ret < 0) { - fputs("internal error in lz4 compressor\n", stderr); - return -1; - } + if (ret < 0) + return SQFS_ERROR_COMRPESSOR; return ret; } @@ -90,10 +87,8 @@ static ssize_t lz4_uncomp_block(sqfs_compressor_t *base, const uint8_t *in, ret = LZ4_decompress_safe((void *)in, (void *)out, size, outsize); - if (ret < 0) { - fputs("internal error in lz4 decompressor\n", stderr); - return -1; - } + if (ret < 0) + return SQFS_ERROR_COMRPESSOR; return ret; } @@ -102,10 +97,8 @@ static sqfs_compressor_t *lz4_create_copy(sqfs_compressor_t *cmp) { lz4_compressor_t *lz4 = malloc(sizeof(*lz4)); - if (lz4 == NULL) { - perror("creating additional lz4 compressor"); + if (lz4 == NULL) return NULL; - } memcpy(lz4, cmp, sizeof(*lz4)); return (sqfs_compressor_t *)lz4; @@ -123,16 +116,13 @@ sqfs_compressor_t *lz4_compressor_create(const sqfs_compressor_config_t *cfg) if (cfg->flags & ~(SQFS_COMP_FLAG_LZ4_ALL | SQFS_COMP_FLAG_GENERIC_ALL)) { - fputs("creating lz4 compressor: unknown compressor flags\n", - stderr); + return NULL; } lz4 = calloc(1, sizeof(*lz4)); base = (sqfs_compressor_t *)lz4; - if (lz4 == NULL) { - perror("creating lz4 compressor"); + if (lz4 == NULL) return NULL; - } lz4->high_compression = (cfg->flags & SQFS_COMP_FLAG_LZ4_HC) != 0; diff --git a/lib/sqfs/comp/lzo.c b/lib/sqfs/comp/lzo.c index b01bf20..b4cfebf 100644 --- a/lib/sqfs/comp/lzo.c +++ b/lib/sqfs/comp/lzo.c @@ -10,8 +10,6 @@ #include #include #include -#include -#include #include @@ -86,9 +84,11 @@ static int lzo_read_options(sqfs_compressor_t *base, int fd) { lzo_compressor_t *lzo = (lzo_compressor_t *)base; lzo_options_t opt; + int ret; - if (sqfs_generic_read_options(fd, &opt, sizeof(opt))) - return -1; + ret = sqfs_generic_read_options(fd, &opt, sizeof(opt)); + if (ret) + return ret; lzo->algorithm = le32toh(opt.algorithm); lzo->level = le32toh(opt.level); @@ -99,21 +99,17 @@ static int lzo_read_options(sqfs_compressor_t *base, int fd) case SQFS_LZO1X_1_12: case SQFS_LZO1X_1_15: if (lzo->level != 0) - goto fail_level; + return SQFS_ERROR_UNSUPPORTED; break; case SQFS_LZO1X_999: if (lzo->level < 1 || lzo->level > 9) - goto fail_level; + return SQFS_ERROR_UNSUPPORTED; break; default: - fputs("Unsupported LZO variant specified.\n", stderr); - return -1; + return SQFS_ERROR_UNSUPPORTED; } return 0; -fail_level: - fputs("Unsupported LZO compression level specified.\n", stderr); - return -1; } static ssize_t lzo_comp_block(sqfs_compressor_t *base, const uint8_t *in, @@ -133,10 +129,8 @@ static ssize_t lzo_comp_block(sqfs_compressor_t *base, const uint8_t *in, &len, lzo->buffer); } - if (ret != LZO_E_OK) { - fputs("LZO compression failed.\n", stderr); - return -1; - } + if (ret != LZO_E_OK) + return SQFS_ERROR_COMRPESSOR; if (len < size) return len; @@ -153,10 +147,8 @@ static ssize_t lzo_uncomp_block(sqfs_compressor_t *base, const uint8_t *in, ret = lzo1x_decompress_safe(in, size, out, &len, lzo->buffer); - if (ret != LZO_E_OK) { - fputs("lzo decompress: input data is corrupted\n", stderr); - return -1; - } + if (ret != LZO_E_OK) + return SQFS_ERROR_COMRPESSOR; return len; } @@ -167,11 +159,8 @@ static sqfs_compressor_t *lzo_create_copy(sqfs_compressor_t *cmp) lzo_compressor_t *lzo; lzo = alloc_flex(sizeof(*lzo), 1, lzo_algs[other->algorithm].bufsize); - - if (lzo == NULL) { - perror("creating additional lzo compressor"); + if (lzo == NULL) return NULL; - } memcpy(lzo, other, sizeof(*lzo)); return (sqfs_compressor_t *)lzo; @@ -187,28 +176,18 @@ sqfs_compressor_t *lzo_compressor_create(const sqfs_compressor_config_t *cfg) sqfs_compressor_t *base; lzo_compressor_t *lzo; - if (cfg->flags & ~SQFS_COMP_FLAG_GENERIC_ALL) { - fputs("creating lzo compressor: unknown compressor flags\n", - stderr); + if (cfg->flags & ~SQFS_COMP_FLAG_GENERIC_ALL) return NULL; - } 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; } 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); + if (cfg->opt.lzo.level > SQFS_LZO_MAX_LEVEL) return NULL; - } } else if (cfg->opt.lzo.level != 0) { - fputs("creating lzo compressor: level argument " - "only supported by lzo1x 999\n", stderr); return NULL; } @@ -216,10 +195,8 @@ sqfs_compressor_t *lzo_compressor_create(const sqfs_compressor_config_t *cfg) lzo_algs[cfg->opt.lzo.algorithm].bufsize); base = (sqfs_compressor_t *)lzo; - if (lzo == NULL) { - perror("creating lzo compressor"); + if (lzo == NULL) return NULL; - } lzo->algorithm = cfg->opt.lzo.algorithm; lzo->level = cfg->opt.lzo.level; diff --git a/lib/sqfs/comp/xz.c b/lib/sqfs/comp/xz.c index 1f98a25..c626f92 100644 --- a/lib/sqfs/comp/xz.c +++ b/lib/sqfs/comp/xz.c @@ -10,8 +10,6 @@ #include #include #include -#include -#include #include #include "internal.h" @@ -56,22 +54,20 @@ static int xz_read_options(sqfs_compressor_t *base, int fd) { xz_compressor_t *xz = (xz_compressor_t *)base; xz_options_t opt; + int ret; - if (sqfs_generic_read_options(fd, &opt, sizeof(opt))) - return -1; + ret = sqfs_generic_read_options(fd, &opt, sizeof(opt)); + if (ret) + return ret; opt.dict_size = le32toh(opt.dict_size); opt.flags = le32toh(opt.flags); - if (!is_dict_size_valid(opt.dict_size)) { - fputs("Invalid lzma dictionary size.\n", stderr); - return -1; - } + if (!is_dict_size_valid(opt.dict_size)) + return SQFS_ERROR_CORRUPTED; - if (opt.flags & ~SQFS_COMP_FLAG_XZ_ALL) { - fputs("Unknown BCJ filter used.\n", stderr); - return -1; - } + if (opt.flags & ~SQFS_COMP_FLAG_XZ_ALL) + return SQFS_ERROR_UNSUPPORTED; xz->flags = opt.flags; xz->dict_size = opt.dict_size; @@ -88,10 +84,8 @@ static ssize_t compress(xz_compressor_t *xz, lzma_vli filter, lzma_ret ret; int i = 0; - if (lzma_lzma_preset(&opt, LZMA_PRESET_DEFAULT)) { - fputs("error initializing xz options\n", stderr); - return -1; - } + if (lzma_lzma_preset(&opt, LZMA_PRESET_DEFAULT)) + return SQFS_ERROR_COMRPESSOR; opt.dict_size = xz->dict_size; @@ -115,10 +109,8 @@ static ssize_t compress(xz_compressor_t *xz, lzma_vli filter, if (ret == LZMA_OK) return (written >= size) ? 0 : written; - if (ret != LZMA_BUF_ERROR) { - fputs("xz block compress failed\n", stderr); - return -1; - } + if (ret != LZMA_BUF_ERROR) + return SQFS_ERROR_COMRPESSOR; return 0; } @@ -165,7 +157,7 @@ static ssize_t xz_comp_block(sqfs_compressor_t *base, const uint8_t *in, ret = compress(xz, filter, in, size, out, outsize); if (ret < 0) - return -1; + return ret; if (ret > 0 && (smallest == 0 || (size_t)ret < smallest)) { smallest = ret; @@ -195,18 +187,15 @@ static ssize_t xz_uncomp_block(sqfs_compressor_t *base, const uint8_t *in, if (ret == LZMA_OK && size == src_pos) return (ssize_t)dest_pos; - fputs("xz block extract failed\n", stderr); - return -1; + return SQFS_ERROR_COMRPESSOR; } static sqfs_compressor_t *xz_create_copy(sqfs_compressor_t *cmp) { xz_compressor_t *xz = malloc(sizeof(*xz)); - if (xz == NULL) { - perror("creating additional xz compressor"); + if (xz == NULL) return NULL; - } memcpy(xz, cmp, sizeof(*xz)); return (sqfs_compressor_t *)xz; @@ -224,23 +213,16 @@ sqfs_compressor_t *xz_compressor_create(const sqfs_compressor_config_t *cfg) if (cfg->flags & ~(SQFS_COMP_FLAG_GENERIC_ALL | SQFS_COMP_FLAG_XZ_ALL)) { - fputs("creating xz compressor: unknown compressor flags\n", - stderr); return NULL; } - if (!is_dict_size_valid(cfg->opt.xz.dict_size)) { - fputs("creating xz compressor: invalid dictionary size\n", - stderr); + if (!is_dict_size_valid(cfg->opt.xz.dict_size)) return NULL; - } xz = calloc(1, sizeof(*xz)); base = (sqfs_compressor_t *)xz; - if (xz == NULL) { - perror("creating xz compressor"); + if (xz == NULL) return NULL; - } xz->flags = cfg->flags; xz->dict_size = cfg->opt.xz.dict_size; diff --git a/lib/sqfs/comp/zstd.c b/lib/sqfs/comp/zstd.c index 8a1dbf3..dc2387d 100644 --- a/lib/sqfs/comp/zstd.c +++ b/lib/sqfs/comp/zstd.c @@ -10,8 +10,6 @@ #include #include #include -#include -#include #include @@ -43,10 +41,12 @@ static int zstd_write_options(sqfs_compressor_t *base, int fd) static int zstd_read_options(sqfs_compressor_t *base, int fd) { zstd_options_t opt; + int ret; (void)base; - if (sqfs_generic_read_options(fd, &opt, sizeof(opt))) - return -1; + ret = sqfs_generic_read_options(fd, &opt, sizeof(opt)); + if (ret) + return ret; opt.level = le32toh(opt.level); return 0; @@ -61,11 +61,8 @@ static ssize_t zstd_comp_block(sqfs_compressor_t *base, const uint8_t *in, ret = ZSTD_compressCCtx(zstd->zctx, out, outsize, in, size, zstd->level); - if (ZSTD_isError(ret)) { - fprintf(stderr, "internal error in ZSTD compressor: %s\n", - ZSTD_getErrorName(ret)); - return -1; - } + if (ZSTD_isError(ret)) + return SQFS_ERROR_COMRPESSOR; return ret < size ? ret : 0; } @@ -78,11 +75,8 @@ static ssize_t zstd_uncomp_block(sqfs_compressor_t *base, const uint8_t *in, ret = ZSTD_decompress(out, outsize, in, size); - if (ZSTD_isError(ret)) { - fprintf(stderr, "error uncompressing ZSTD compressed data: %s", - ZSTD_getErrorName(ret)); - return -1; - } + if (ZSTD_isError(ret)) + return SQFS_ERROR_COMRPESSOR; return ret; } @@ -91,18 +85,14 @@ static sqfs_compressor_t *zstd_create_copy(sqfs_compressor_t *cmp) { zstd_compressor_t *zstd = malloc(sizeof(*zstd)); - if (zstd == NULL) { - perror("creating additional zstd compressor"); + if (zstd == NULL) return NULL; - } memcpy(zstd, cmp, sizeof(*zstd)); zstd->zctx = ZSTD_createCCtx(); if (zstd->zctx == NULL) { - fputs("error creating addtional zstd compression context\n", - stderr); free(zstd); return NULL; } @@ -123,26 +113,21 @@ sqfs_compressor_t *zstd_compressor_create(const sqfs_compressor_config_t *cfg) zstd_compressor_t *zstd; sqfs_compressor_t *base; - if (cfg->flags & ~SQFS_COMP_FLAG_GENERIC_ALL) { - fputs("creating zstd compressor: unknown compressor flags\n", - stderr); - } + if (cfg->flags & ~SQFS_COMP_FLAG_GENERIC_ALL) + return NULL; if (cfg->opt.zstd.level < 1 || cfg->opt.zstd.level > ZSTD_maxCLevel()) { - goto fail_level; + return NULL; } zstd = calloc(1, sizeof(*zstd)); base = (sqfs_compressor_t *)zstd; - if (zstd == NULL) { - perror("creating zstd compressor"); + if (zstd == NULL) return NULL; - } zstd->zctx = ZSTD_createCCtx(); if (zstd->zctx == NULL) { - fputs("error creating zstd compression context\n", stderr); free(zstd); return NULL; } @@ -154,8 +139,4 @@ sqfs_compressor_t *zstd_compressor_create(const sqfs_compressor_config_t *cfg) base->read_options = zstd_read_options; base->create_copy = zstd_create_copy; return base; -fail_level: - fprintf(stderr, "zstd compression level must be a number in the range " - "1...%d\n", ZSTD_maxCLevel()); - return NULL; } -- cgit v1.2.3