From 33974fccc8722a9b4702b2423b510eaa6a56bde5 Mon Sep 17 00:00:00 2001 From: Riccardo Schirone Date: Thu, 14 Jul 2022 15:20:33 +0200 Subject: [PATCH] util: make zlib dependency optional --- librz/include/rz_userconf.h.in | 5 +- librz/util/compression.c | 111 +++++++++++++++++++-------------- librz/util/meson.build | 8 ++- meson.build | 21 ++++--- meson_options.txt | 1 + 5 files changed, 89 insertions(+), 57 deletions(-) diff --git a/librz/include/rz_userconf.h.in b/librz/include/rz_userconf.h.in index 86221c576e..ae1c164c26 100644 --- a/librz/include/rz_userconf.h.in +++ b/librz/include/rz_userconf.h.in @@ -9,6 +9,7 @@ #define HAVE_THREADS @HAVE_THREADS@ #define HAVE_PTHREAD @HAVE_PTHREAD@ #define HAVE_LZMA @HAVE_LZMA@ +#define HAVE_ZLIB @HAVE_ZLIB@ #define HAVE_DECL_ADDR_NO_RANDOMIZE @HAVE_DECL_ADDR_NO_RANDOMIZE@ #define HAVE_DECL_PROCCTL_ASLR_CTL @HAVE_DECL_PROCCTL_ASLR_CTL@ #define HAVE_ARC4RANDOM_UNIFORM @HAVE_ARC4RANDOM_UNIFORM@ @@ -57,6 +58,8 @@ #define HAVE_HEADER_INTTYPES_H @HAVE_HEADER_INTTYPES_H@ #define RZ_IS_PORTABLE @IS_PORTABLE@ + +#define RZ_BINDIR_DEPTH @BINDIR_DEPTH@ // clang-format on #define RZ_PREFIX "@PREFIX@" @@ -66,8 +69,6 @@ #define RZ_DATDIR "@DATADIR@" #define RZ_WWWROOT "@WWWROOT@" -#define RZ_BINDIR_DEPTH @BINDIR_DEPTH@ - #define RZ_PLUGINS "@PLUGINS@" #define RZ_DATADIR "@DATADIR_RZ@" #define RZ_SDB "@SDB@" diff --git a/librz/util/compression.c b/librz/util/compression.c index 4b993831e6..b5b06d0240 100644 --- a/librz/util/compression.c +++ b/librz/util/compression.c @@ -8,22 +8,6 @@ // set a maximum output buffer of 50MB #define MAXOUT 50000000 -static const char *gzerr(int n) { - const char *errors[] = { - "", - "file error", /* Z_ERRNO (-1) */ - "stream error", /* Z_STREAM_ERROR (-2) */ - "data error", /* Z_DATA_ERROR (-3) */ - "insufficient memory", /* Z_MEM_ERROR (-4) */ - "buffer error", /* Z_BUF_ERROR (-5) */ - "incompatible version", /* Z_VERSION_ERROR (-6) */ - }; - if (n < 1 || n > 6) { - return "unknown"; - } - return errors[n]; -} - /** * \brief inflate zlib compressed or gzipped, automatically accepts either the zlib or gzip format, and use MAX_WBITS as the window size logarithm. * \see rz_inflatew() @@ -44,6 +28,23 @@ RZ_API ut8 *rz_inflate_ignore_header(RZ_NONNULL const ut8 *src, int srcLen, int return rz_inflatew(src, srcLen, srcConsumed, dstLen, -MAX_WBITS); } +#if HAVE_ZLIB +static const char *gzerr(int n) { + const char *errors[] = { + "", + "file error", /* Z_ERRNO (-1) */ + "stream error", /* Z_STREAM_ERROR (-2) */ + "data error", /* Z_DATA_ERROR (-3) */ + "insufficient memory", /* Z_MEM_ERROR (-4) */ + "buffer error", /* Z_BUF_ERROR (-5) */ + "incompatible version", /* Z_VERSION_ERROR (-6) */ + }; + if (n < 1 || n > 6) { + return "unknown"; + } + return errors[n]; +} + /** * \brief inflate zlib compressed or gzipped. * \param src source compressed bytes @@ -112,16 +113,6 @@ err_exit: return NULL; } -/** - * \brief deflate uncompressed data to zlib or gzipped, use MAX_WBITS as the window size logarithm. - * \see rz_deflatew() - */ -RZ_API ut8 *rz_deflate(RZ_NONNULL const ut8 *src, int srcLen, int *srcConsumed, int *dstLen) { - rz_return_val_if_fail(src, NULL); - rz_return_val_if_fail(srcLen > 0, NULL); - return rz_deflatew(src, srcLen, srcConsumed, dstLen, MAX_WBITS + 16); -} - /** * \brief compress/deflate data to zlib or gzip * \param src source uncompressed bytes @@ -190,16 +181,6 @@ err_exit: return NULL; } -/** - * \brief deflate uncompressed data in RzBbuffer to zlib or gzipped, use MAX_WBITS as the window size logarithm. - * \see rz_deflatew_buf() - */ -RZ_API bool rz_deflate_buf(RZ_NONNULL RzBuffer *src, RZ_NONNULL RzBuffer *dst, ut64 block_size, ut8 *src_consumed) { - rz_return_val_if_fail(src && dst, false); - rz_return_val_if_fail(block_size > 0, false); - return rz_deflatew_buf(src, dst, block_size, src_consumed, MAX_WBITS + 16); -} - /** * \brief deflate data contained in a RzBuffer using zlib * \param src source buffer @@ -266,16 +247,6 @@ return_goto: return ret; } -/** - * \brief inflate compressed data in RzBbuffer, use MAX_WBITS as the window size logarithm. - * \see rz_inflatew_buf() - */ -RZ_API bool rz_inflate_buf(RZ_NONNULL RzBuffer *src, RZ_NONNULL RzBuffer *dst, ut64 block_size, ut8 *src_consumed) { - rz_return_val_if_fail(src && dst, false); - rz_return_val_if_fail(block_size > 0, false); - return rz_inflatew_buf(src, dst, block_size, src_consumed, MAX_WBITS + 32); -} - /** * \brief inflate data contained in a RzBuffer using zlib * \param src source buffer @@ -341,6 +312,54 @@ return_goto: return ret; } +#else +RZ_API ut8 *rz_inflatew(RZ_NONNULL const ut8 *src, int srcLen, int *srcConsumed, int *dstLen, int wbits) { + return NULL; +} + +RZ_API ut8 *rz_deflatew(RZ_NONNULL const ut8 *src, int srcLen, int *srcConsumed, int *dstLen, int wbits) { + return NULL; +} + +RZ_API bool rz_deflatew_buf(RZ_NONNULL RzBuffer *src, RZ_NONNULL RzBuffer *dst, ut64 block_size, ut8 *src_consumed, int wbits) { + return false; +} + +RZ_API bool rz_inflatew_buf(RZ_NONNULL RzBuffer *src, RZ_NONNULL RzBuffer *dst, ut64 block_size, ut8 *src_consumed, int wbits) { + return false; +} +#endif + +/** + * \brief deflate uncompressed data to zlib or gzipped, use MAX_WBITS as the window size logarithm. + * \see rz_deflatew() + */ +RZ_API ut8 *rz_deflate(RZ_NONNULL const ut8 *src, int srcLen, int *srcConsumed, int *dstLen) { + rz_return_val_if_fail(src, NULL); + rz_return_val_if_fail(srcLen > 0, NULL); + return rz_deflatew(src, srcLen, srcConsumed, dstLen, MAX_WBITS + 16); +} + +/** + * \brief deflate uncompressed data in RzBbuffer to zlib or gzipped, use MAX_WBITS as the window size logarithm. + * \see rz_deflatew_buf() + */ +RZ_API bool rz_deflate_buf(RZ_NONNULL RzBuffer *src, RZ_NONNULL RzBuffer *dst, ut64 block_size, ut8 *src_consumed) { + rz_return_val_if_fail(src && dst, false); + rz_return_val_if_fail(block_size > 0, false); + return rz_deflatew_buf(src, dst, block_size, src_consumed, MAX_WBITS + 16); +} + +/** + * \brief inflate compressed data in RzBbuffer, use MAX_WBITS as the window size logarithm. + * \see rz_inflatew_buf() + */ +RZ_API bool rz_inflate_buf(RZ_NONNULL RzBuffer *src, RZ_NONNULL RzBuffer *dst, ut64 block_size, ut8 *src_consumed) { + rz_return_val_if_fail(src && dst, false); + rz_return_val_if_fail(block_size > 0, false); + return rz_inflatew_buf(src, dst, block_size, src_consumed, MAX_WBITS + 32); +} + #if HAVE_LZMA static bool lzma_action_buf(RZ_NONNULL RzBuffer *src, RZ_NONNULL RzBuffer *dst, ut64 block_size, ut8 *src_consumed, bool encode) { bool res = true; diff --git a/librz/util/meson.build b/librz/util/meson.build index e74697272a..8714d8bfca 100644 --- a/librz/util/meson.build +++ b/librz/util/meson.build @@ -81,7 +81,13 @@ rz_util_sources = [ 'x509.c', ] -rz_util_deps = [ldl, lrt, mth, th, utl, sdb_dep, zlib_dep, liblzma_dep] + platform_deps +rz_util_deps = [ldl, lrt, mth, th, utl, sdb_dep] + platform_deps +if zlib_dep.found() + rz_util_deps += [zlib_dep] +endif +if liblzma_dep.found() + rz_util_deps += [liblzma_dep] +endif if ['freebsd', 'netbsd', 'haiku', 'dragonfly'].contains(host_machine.system()) # backtrace_symbols_fd requires -lexecinfo rz_util_deps += [cc.find_library('execinfo', static: is_static_build)] diff --git a/meson.build b/meson.build index 2f553dc749..68f5cd4d98 100644 --- a/meson.build +++ b/meson.build @@ -408,6 +408,7 @@ userconf.set10('HAVE_DECL_PROCCTL_ASLR_CTL', ok) userconf.set10('HAVE_THREADS', th.found()) userconf.set10('HAVE_PTHREAD', have_pthread) userconf.set10('HAVE_LZMA', get_option('use_lzma')) +userconf.set10('HAVE_ZLIB', get_option('use_zlib')) if host_machine.system() == 'freebsd' or host_machine.system() == 'dragonfly' add_project_link_arguments('-Wl,--unresolved-symbols,ignore-in-object-files', language: 'c') @@ -528,15 +529,19 @@ rz_version_h = configure_file( ) # handle zlib dependency -r = run_command(py3_exe, check_meson_subproject_py, 'zlib', check: false) -if r.returncode() == 1 and get_option('subprojects_check') - error('Subprojects are not updated. Please run `git clean -dxff subprojects/` to delete all local subprojects directories. If you want to compile against current subprojects then set option `subprojects_check=false`.') -endif +zlib_dep = disabler() +if get_option('use_zlib') + r = run_command(py3_exe, check_meson_subproject_py, 'zlib', check: false) + if r.returncode() == 1 and get_option('subprojects_check') + error('Subprojects are not updated. Please run `git clean -dxff subprojects/` to delete all local subprojects directories. If you want to compile against current subprojects then set option `subprojects_check=false`.') + endif -zlib_dep = dependency('zlib', required: get_option('use_sys_zlib'), static: is_static_build) -if not zlib_dep.found() - zlib_proj = subproject('zlib', default_options: ['default_library=static']) - zlib_dep = zlib_proj.get_variable('zlib_dep') + zlib_dep = dependency('zlib', required: get_option('use_sys_zlib'), static: is_static_build) + if not zlib_dep.found() + zlib_proj = subproject('zlib', default_options: ['default_library=static']) + zlib_dep = zlib_proj.get_variable('zlib_dep') + meson.override_dependency('zlib', zlib_dep) + endif endif # handle lz4 dependency diff --git a/meson_options.txt b/meson_options.txt index c11bf37ad4..4c565ccb3c 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -27,6 +27,7 @@ option('use_sys_libzip_openssl', type: 'boolean', value: false, description: 'Wh option('use_sys_zlib', type: 'feature', value: 'disabled') option('use_sys_lz4', type: 'feature', value: 'disabled') option('use_lzma', type: 'boolean', value: true, description: 'If true, liblzma is used to provide extra features (e.g. .gnu_debugdata parser)') +option('use_zlib', type: 'boolean', value: true, description: 'If true, zlib is used to provide extra features (e.g. project compression/decompression, etc.)') option('use_sys_lzma', type: 'feature', value: 'disabled') option('use_sys_xxhash', type: 'feature', value: 'disabled') option('use_sys_openssl', type: 'feature', value: 'disabled')