Skip to content

Commit

Permalink
Fixed issues with XZ compression when using libcompression.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmoinvaz committed Oct 25, 2020
1 parent 9667101 commit 1a7e7e4
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 47 deletions.
74 changes: 37 additions & 37 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions minizip.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion mz_os.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion mz_strm_libcomp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
17 changes: 11 additions & 6 deletions mz_zip.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1706,17 +1708,20 @@ 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) {
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);
}
#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)
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 1a7e7e4

Please sign in to comment.