Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Link zlib-ng native if available #574

Merged
merged 4 commits into from Mar 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 19 additions & 2 deletions CMakeLists.txt
Expand Up @@ -6,6 +6,7 @@
#***************************************************************************

cmake_minimum_required(VERSION 3.13)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")

message(STATUS "Using CMake version ${CMAKE_VERSION}")

Expand Down Expand Up @@ -198,18 +199,29 @@ endif()
if(MZ_ZLIB)
# Check if zlib is present
if(NOT MZ_FORCE_FETCH_LIBS)
find_package(ZLIBNG QUIET)
find_package(ZLIB QUIET)
set(ZLIB_VERSION ${ZLIB_VERSION_STRING})
endif()

if(ZLIB_FOUND AND NOT MZ_FORCE_FETCH_LIBS)
if(ZLIBNG_FOUND AND NOT MZ_FORCE_FETCH_LIBS)
message(STATUS "Using ZLIBNG")

list(APPEND MINIZIP_INC ${ZLIBNG_INCLUDE_DIRS})
list(APPEND MINIZIP_LIB ${ZLIBNG_LIBRARIES})
list(APPEND MINIZIP_LBD ${ZLIBNG_LIBRARY_DIRS})

set(PC_PRIVATE_LIBS " -lz-ng")
set(ZLIB_COMPAT OFF)
elseif(ZLIB_FOUND AND NOT MZ_FORCE_FETCH_LIBS)
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})

set(PC_PRIVATE_LIBS " -lz")
set(ZLIB_COMPAT ON)
elseif(MZ_FETCH_LIBS)
clone_repo(zlib https://github.com/madler/zlib)

Expand All @@ -225,14 +237,19 @@ if(MZ_ZLIB)
else()
list(APPEND MINIZIP_DEP zlib)
endif()
set(ZLIB_COMPAT ON)
else()
message(STATUS "ZLIB library not found")

set(MZ_ZLIB OFF)
endif()

if(MZ_ZLIB)
list(APPEND MINIZIP_DEP_PKG ZLIB)
if(ZLIBNG_FOUND)
list(APPEND MINIZIP_DEP_PKG ZLIBNG)
elseif(ZLIB_FOUND)
list(APPEND MINIZIP_DEP_PKG ZLIB)
endif()
list(APPEND MINIZIP_DEF -DHAVE_ZLIB)
if(ZLIB_COMPAT)
list(APPEND MINIZIP_DEF -DZLIB_COMPAT)
Expand Down
32 changes: 32 additions & 0 deletions cmake/FindZLIBNG.cmake
@@ -0,0 +1,32 @@
find_path(ZLIBNG_INCLUDE_DIRS NAMES zlib-ng.h)

if(ZLIB_INCLUDE_DIRS)
set(ZLIBNG_LIBRARY_DIRS ${ZLIBNG_INCLUDE_DIRS})

if("${ZLIBNG_LIBRARY_DIRS}" MATCHES "/include$")
# Strip off the trailing "/include" in the path.
get_filename_component(ZLIBNG_LIBRARY_DIRS ${ZLIBNG_LIBRARY_DIRS} PATH)
endif()

if(EXISTS "${ZLIBNG_LIBRARY_DIRS}/lib")
set(ZLIBNG_LIBRARY_DIRS ${ZLIBNG_LIBRARY_DIRS}/lib)
endif()
endif()

find_library(ZLIBNG_LIBRARY NAMES z-ng libz-ng libz-ng.a)

set(ZLIBNG_LIBRARIES ${ZLIBNG_LIBRARY})
set(ZLIBNG_INCLUDE_DIRS ${ZLIBNG_INCLUDE_DIRS})

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(ZLIBNG DEFAULT_MSG ZLIBNG_LIBRARY ZLIBNG_INCLUDE_DIRS)

if(ZLIBNG_INCLUDE_DIRS AND ZLIBNG_LIBRARIES)
set(ZLIBNG_FOUND TRUE)
else(ZLIBNG_INCLUDE_DIRS AND ZLIBNG_LIBRARIES)
set(ZLIBNG_FOUND FALSE)
endif()

if(ZLIBNG_FOUND)
message(STATUS "Found zlib-ng: ${ZLIBNG_LIBRARIES}, ${ZLIBNG_INCLUDE_DIRS}")
endif()
30 changes: 11 additions & 19 deletions mz_crypt.c
Expand Up @@ -14,31 +14,17 @@
#include "mz_crypt.h"

#if defined(HAVE_ZLIB)
# include "zlib.h"
# if defined(ZLIBNG_VERNUM) && !defined(ZLIB_COMPAT)
# if !defined(ZLIB_COMPAT)
# include "zlib-ng.h"
# define ZLIB_PREFIX(x) zng_ ## x
# else
# include "zlib.h"
# define ZLIB_PREFIX(x) x
# endif
#elif defined(HAVE_LZMA)
# include "lzma.h"
#endif

/***************************************************************************/
/* Define z_crc_t in zlib 1.2.5 and less or if using zlib-ng */

#if defined(HAVE_ZLIB) && defined(ZLIBNG_VERNUM)
# if defined(ZLIB_COMPAT)
# define ZLIB_PREFIX(x) x
# else
# define ZLIB_PREFIX(x) zng_ ## x
# endif
typedef uint32_t z_crc_t;
#elif defined(HAVE_ZLIB)
# define ZLIB_PREFIX(x) x
# if (ZLIB_VERNUM < 0x1270)
typedef unsigned long z_crc_t;
# endif
#endif

/***************************************************************************/

#if defined(MZ_ZIP_NO_CRYPTO)
Expand All @@ -49,6 +35,12 @@ int32_t mz_crypt_rand(uint8_t *buf, int32_t size) {

uint32_t mz_crypt_crc32_update(uint32_t value, const uint8_t *buf, int32_t size) {
#if defined(HAVE_ZLIB)
/* Define z_crc_t in zlib 1.2.5 and less or if using zlib-ng */
# if (ZLIB_VERNUM < 0x1270)
typedef unsigned long z_crc_t;
# else
typedef uint32_t z_crc_t;
# endif
return (uint32_t)ZLIB_PREFIX(crc32)((z_crc_t)value, buf, (uInt)size);
#elif defined(HAVE_LZMA)
return (uint32_t)lzma_crc32(buf, (size_t)size, (uint32_t)value);
Expand Down
7 changes: 4 additions & 3 deletions mz_strm_zlib.c
Expand Up @@ -13,14 +13,15 @@
#include "mz_strm.h"
#include "mz_strm_zlib.h"

#include "zlib.h"
#if defined(ZLIBNG_VERNUM) && !defined(ZLIB_COMPAT)
#if !defined(ZLIB_COMPAT)
# include "zlib-ng.h"
#else
# include "zlib.h"
#endif

/***************************************************************************/

#if defined(ZLIBNG_VERNUM) && !defined(ZLIB_COMPAT)
#if !defined(ZLIB_COMPAT)
# define ZLIB_PREFIX(x) zng_ ## x
typedef zng_stream zlib_stream;
#else
Expand Down