diff options
author | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2020-03-05 22:38:15 +0100 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2020-03-05 22:41:04 +0100 |
commit | 85e36aab1258d8d9ec7b61ce013f167ef8e03ae0 (patch) | |
tree | b1d59cce7894520b53c6b1fa86666d91587aaf82 /lib/sqfs/comp/compressor.c | |
parent | 5acb22a6a7168f8b961777482f39a125158def50 (diff) |
Change the signature of sqfs_compressor_create to return an error code
Make sure the function has a way of telling the caller *why* it failed.
This way, the function can convey whether it had an internal error, an
allocation failure, whether the arguments are totaly nonsensical, or
simply that the compressor *or specific configuration* is not supported.
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/sqfs/comp/compressor.c')
-rw-r--r-- | lib/sqfs/comp/compressor.c | 23 |
1 files changed, 15 insertions, 8 deletions
diff --git a/lib/sqfs/comp/compressor.c b/lib/sqfs/comp/compressor.c index 28de2c5..3b48030 100644 --- a/lib/sqfs/comp/compressor.c +++ b/lib/sqfs/comp/compressor.c @@ -12,8 +12,8 @@ #include "internal.h" -typedef sqfs_compressor_t *(*compressor_fun_t) - (const sqfs_compressor_config_t *cfg); +typedef int (*compressor_fun_t)(const sqfs_compressor_config_t *cfg, + sqfs_compressor_t **out); static compressor_fun_t compressors[SQFS_COMP_MAX + 1] = { #ifdef WITH_GZIP @@ -81,17 +81,24 @@ bool sqfs_compressor_exists(SQFS_COMPRESSOR id) return (compressors[id] != NULL); } -sqfs_compressor_t *sqfs_compressor_create(const sqfs_compressor_config_t *cfg) +int sqfs_compressor_create(const sqfs_compressor_config_t *cfg, + sqfs_compressor_t **out) { sqfs_u8 padd0[sizeof(cfg->opt)]; int ret; - if (cfg == NULL || cfg->id < SQFS_COMP_MIN || cfg->id > SQFS_COMP_MAX) - return NULL; + /* check compressor ID */ + if (cfg == NULL) + return SQFS_ERROR_ARG_INVALID; + + if (cfg->id < SQFS_COMP_MIN || cfg->id > SQFS_COMP_MAX) + return SQFS_ERROR_UNSUPPORTED; if (compressors[cfg->id] == NULL) - return NULL; + return SQFS_ERROR_UNSUPPORTED; + /* make sure the padding bytes are cleared, so we could theoretically + turn them into option fields in the future and remain compatible */ memset(padd0, 0, sizeof(padd0)); switch (cfg->id) { @@ -117,9 +124,9 @@ sqfs_compressor_t *sqfs_compressor_create(const sqfs_compressor_config_t *cfg) } if (ret != 0) - return NULL; + return SQFS_ERROR_ARG_INVALID; - return compressors[cfg->id](cfg); + return compressors[cfg->id](cfg, out); } const char *sqfs_compressor_name_from_id(SQFS_COMPRESSOR id) |