From 1a7e7e4a4c6f6065fa47b360077eebdab02c195c Mon Sep 17 00:00:00 2001 From: Nathan Moinvaziri Date: Sun, 25 Oct 2020 14:50:53 -0700 Subject: [PATCH] Fixed issues with XZ compression when using libcompression. --- CMakeLists.txt | 74 +++++++++++++++++++++++------------------------ minizip.c | 3 +- mz_os.h | 2 +- mz_strm_libcomp.c | 4 ++- mz_zip.c | 17 +++++++---- 5 files changed, 53 insertions(+), 47 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f16fa07f..4a936e32 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -153,49 +153,47 @@ macro(clone_repo name url) endif() endmacro() -if(MZ_ZLIB) - if(MZ_LIBCOMP) - # Use Apple libcompression - list(APPEND MINIZIP_DEF -DHAVE_LIBCOMP) - list(APPEND MINIZIP_SRC mz_strm_libcomp.c) - list(APPEND MINIZIP_HDR mz_strm_libcomp.h) - list(APPEND MINIZIP_LIB compression) - else() - # Check if zlib is present - if(NOT ZLIB_FORCE_FETCH) - find_package(ZLIB QUIET) - set(ZLIB_VERSION ${ZLIB_VERSION_STRING}) - endif() - if(ZLIB_FOUND AND NOT ZLIB_FORCE_FETCH) - message(STATUS "Using ZLIB ${ZLIB_VERSION}") +if(MZ_LIBCOMP) + # Use Apple libcompression + list(APPEND MINIZIP_DEF -DHAVE_LIBCOMP) + list(APPEND MINIZIP_SRC mz_strm_libcomp.c) + list(APPEND MINIZIP_HDR mz_strm_libcomp.h) + list(APPEND MINIZIP_LIB compression) +elseif(MZ_ZLIB) + # Check if zlib is present + if(NOT ZLIB_FORCE_FETCH) + find_package(ZLIB QUIET) + set(ZLIB_VERSION ${ZLIB_VERSION_STRING}) + endif() + if(ZLIB_FOUND AND NOT ZLIB_FORCE_FETCH) + message(STATUS "Using ZLIB ${ZLIB_VERSION}") - list(APPEND MINIZIP_INC ${ZLIB_INCLUDE_DIRS}) - list(APPEND MINIZIP_LIB ${ZLIB_LIBRARIES}) - list(APPEND MINIZIP_LBD ${ZLIB_LIBRARY_DIRS}) + list(APPEND MINIZIP_INC ${ZLIB_INCLUDE_DIRS}) + list(APPEND MINIZIP_LIB ${ZLIB_LIBRARIES}) + list(APPEND MINIZIP_LBD ${ZLIB_LIBRARY_DIRS}) - set(PC_PRIVATE_LIBS " -lz") - else() - clone_repo(ZLIB https://github.com/madler/zlib) + set(PC_PRIVATE_LIBS " -lz") + else() + clone_repo(ZLIB https://github.com/madler/zlib) - # Don't automatically add all targets to the solution - add_subdirectory(${ZLIB_SOURCE_DIR} ${ZLIB_BINARY_DIR} EXCLUDE_FROM_ALL) + # Don't automatically add all targets to the solution + add_subdirectory(${ZLIB_SOURCE_DIR} ${ZLIB_BINARY_DIR} EXCLUDE_FROM_ALL) - list(APPEND MINIZIP_INC ${ZLIB_SOURCE_DIR}) - # Have to add zlib to install targets - if(NOT DEFINED BUILD_SHARED_LIBS OR NOT ${BUILD_SHARED_LIBS}) - list(APPEND MINIZIP_DEP zlibstatic) - else() - list(APPEND MINIZIP_DEP zlib) - endif() + list(APPEND MINIZIP_INC ${ZLIB_SOURCE_DIR}) + # Have to add zlib to install targets + if(NOT DEFINED BUILD_SHARED_LIBS OR NOT ${BUILD_SHARED_LIBS}) + list(APPEND MINIZIP_DEP zlibstatic) + else() + list(APPEND MINIZIP_DEP zlib) endif() + endif() - list(APPEND MINIZIP_DEF -DHAVE_ZLIB) - if(ZLIB_COMPAT) - list(APPEND MINIZIP_DEF -DZLIB_COMPAT) - endif() - list(APPEND MINIZIP_SRC mz_strm_zlib.c) - list(APPEND MINIZIP_HDR mz_strm_zlib.h) + list(APPEND MINIZIP_DEF -DHAVE_ZLIB) + if(ZLIB_COMPAT) + list(APPEND MINIZIP_DEF -DZLIB_COMPAT) endif() + list(APPEND MINIZIP_SRC mz_strm_zlib.c) + list(APPEND MINIZIP_HDR mz_strm_zlib.h) endif() if(MZ_BZIP2) @@ -737,6 +735,8 @@ if(MZ_BUILD_TEST AND MZ_BUILD_UNIT_TEST) if(MZ_LZMA) list(APPEND COMPRESS_METHOD_NAMES "lzma") list(APPEND COMPRESS_METHOD_ARGS "-m") + endif() + if(MZ_LZMA OR MZ_LIBCOMP) list(APPEND COMPRESS_METHOD_NAMES "xz") list(APPEND COMPRESS_METHOD_ARGS "-n") endif() @@ -798,7 +798,7 @@ if(MZ_BUILD_TEST AND MZ_BUILD_UNIT_TEST) # Perform tests on others if(NOT MZ_COMPRESS_ONLY) - if(MZ_ZLIB) + if(MZ_ZLIB OR MZ_LIBCOMP) add_test(NAME unzip-tiny COMMAND minizip_cmd -x -o ${EXTRA_ARGS} -d out fuzz/unzip_fuzzer_seed_corpus/tiny.zip diff --git a/minizip.c b/minizip.c index 2f02d5c8..38819302 100644 --- a/minizip.c +++ b/minizip.c @@ -101,7 +101,6 @@ int32_t minizip_help(void) { int32_t minizip_list(const char *path) { mz_zip_file *file_info = NULL; uint32_t ratio = 0; - int16_t level = 0; int32_t err = MZ_OK; struct tm tmu_date; const char *method = NULL; @@ -580,7 +579,7 @@ int main(int argc, const char *argv[]) { err = MZ_SUPPORT_ERROR; #endif else if ((c == 'n') || (c == 'N')) -#ifdef HAVE_LZMA +#if defined(HAVE_LZMA) || defined(HAVE_LIBCOMP) options.compress_method = MZ_COMPRESS_METHOD_XZ; #else err = MZ_SUPPORT_ERROR; diff --git a/mz_os.h b/mz_os.h index 69627080..f0c6f328 100644 --- a/mz_os.h +++ b/mz_os.h @@ -27,7 +27,7 @@ extern "C" { # define MZ_VERSION_MADEBY_HOST_SYSTEM (MZ_HOST_SYSTEM_WINDOWS_NTFS) #endif -#if defined(HAVE_LZMA) +#if defined(HAVE_LZMA) || defined(HAVE_LIBCOMP) # define MZ_VERSION_MADEBY_ZIP_VERSION (63) #elif defined(HAVE_WZAES) # define MZ_VERSION_MADEBY_ZIP_VERSION (51) diff --git a/mz_strm_libcomp.c b/mz_strm_libcomp.c index c62f7c09..4d115746 100644 --- a/mz_strm_libcomp.c +++ b/mz_strm_libcomp.c @@ -83,7 +83,9 @@ int32_t mz_stream_libcomp_open(void *stream, const char *path, int32_t mode) { algorithm = COMPRESSION_ZLIB; else if (libcomp->method == MZ_COMPRESS_METHOD_XZ) algorithm = COMPRESSION_LZMA; - + else + return MZ_SUPPORT_ERROR; + err = compression_stream_init(&libcomp->cstream, (compression_stream_operation)operation, algorithm); if (err == COMPRESSION_STATUS_ERROR) { diff --git a/mz_zip.c b/mz_zip.c index c70bf988..f22aa3f4 100644 --- a/mz_zip.c +++ b/mz_zip.c @@ -658,7 +658,7 @@ static int32_t mz_zip_entry_write_header(void *stream, uint8_t local, mz_zip_fil if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version)) version_needed = 51; #endif -#ifdef HAVE_LZMA +#if defined(HAVE_LZMA) || defined(HAVE_LIBCOMP) if ((file_info->compression_method == MZ_COMPRESS_METHOD_LZMA) || (file_info->compression_method == MZ_COMPRESS_METHOD_XZ)) version_needed = 63; @@ -1621,6 +1621,8 @@ static int32_t mz_zip_entry_open_int(void *handle, uint8_t raw, int16_t compress #endif #ifdef HAVE_LZMA case MZ_COMPRESS_METHOD_LZMA: +#endif +#if defined(HAVE_LZMA) || defined(HAVE_LIBCOMP) case MZ_COMPRESS_METHOD_XZ: #endif #ifdef HAVE_ZSTD @@ -1706,6 +1708,13 @@ static int32_t mz_zip_entry_open_int(void *handle, uint8_t raw, int16_t compress else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_BZIP2) mz_stream_bzip_create(&zip->compress_stream); #endif +#ifdef HAVE_LIBCOMP + else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_XZ) { + mz_stream_lzma_create(&zip->compress_stream); + mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_COMPRESS_METHOD, + zip->file_info.compression_method); + } +#endif #ifdef HAVE_LZMA else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_LZMA || zip->file_info.compression_method == MZ_COMPRESS_METHOD_XZ) { @@ -1713,10 +1722,6 @@ static int32_t mz_zip_entry_open_int(void *handle, uint8_t raw, int16_t compress mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_COMPRESS_METHOD, zip->file_info.compression_method); } -#elif defined(HAVE_LIBCOMP) - else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_XZ) { - mz_stream_lzma_create(&zip->compress_stream); - } #endif #ifdef HAVE_ZSTD else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_ZSTD) @@ -1921,7 +1926,7 @@ int32_t mz_zip_entry_write_open(void *handle, const mz_zip_file *file_info, int1 if (compress_level == 1) zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_SUPER_FAST; } -#ifdef HAVE_LZMA +#if defined(HAVE_LZMA) || defined(HAVE_LIBCOMP) else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_LZMA || zip->file_info.compression_method == MZ_COMPRESS_METHOD_XZ) zip->file_info.flag |= MZ_ZIP_FLAG_LZMA_EOS_MARKER;