Don't include libbsd if we don't have to. #283

This commit is contained in:
Nathan Moinvaziri 2018-07-22 11:12:40 -07:00
parent a6d1f6692e
commit 4042763729
4 changed files with 37 additions and 13 deletions

View File

@ -81,6 +81,9 @@ endif()
if(DECOMPRESS_ONLY)
add_definitions(-DMZ_ZIP_NO_COMPRESSION)
endif()
if(NOT USE_PKCRYPT AND NOT USE_AES)
add_definitions(-DMZ_ZIP_NO_ENCRYPTION)
endif()
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
@ -121,10 +124,12 @@ if(UNIX)
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
find_package(PkgConfig REQUIRED)
pkg_check_modules(LIBBSD libbsd REQUIRED)
include_directories(${LIBBSD_INCLUDE_DIRS})
link_directories(${LIBBSD_LIBRARY_DIRS})
pkg_check_modules(LIBBSD libbsd)
if (LIBBSD_FOUND)
add_definitions(-DHAVE_LIBBSD)
include_directories(${LIBBSD_INCLUDE_DIRS})
link_directories(${LIBBSD_LIBRARY_DIRS})
endif()
endif()
endif()

View File

@ -16,14 +16,17 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/random.h>
#if defined unix || defined __APPLE__
# include <unistd.h>
# include <utime.h>
# define HAVE_ARC4RANDOM_BUF
#endif
#if defined __linux__
# if !defined(MZ_ZIP_NO_COMPRESSION) && \
(defined(HAVE_PKCRYPT) || defined(HAVE_AES))
!defined(MZ_ZIP_NO_ENCRYPTION) && \
defined(HAVE_LIBBSD)
# include <bsd/stdlib.h> // arc4random_buf
# endif
#else
@ -37,13 +40,31 @@
/***************************************************************************/
#if !defined(MZ_ZIP_NO_COMPRESSION) && \
(defined(HAVE_PKCRYPT) || defined(HAVE_AES))
#if !defined(MZ_ZIP_NO_COMPRESSION) && !defined(MZ_ZIP_NO_ENCRYPTION)
#if defined(HAVE_LIBBSD) || defined(HAVE_ARC4RANDOM_BUF)
int32_t mz_posix_rand(uint8_t *buf, int32_t size)
{
arc4random_buf(buf, size);
return size;
}
#else
int32_t mz_posix_rand(uint8_t *buf, int32_t size)
{
int32_t left = size;
int32_t written = 0;
while (left > 0)
{
written = getrandom(buf, left, 0);
if (written < 0)
return MZ_INTERNAL_ERROR;
buf += written;
left -= written;
}
return size - left;
}
#endif
#endif
int32_t mz_posix_file_exists(const char *path)

View File

@ -17,8 +17,7 @@
#include <errno.h>
#include <windows.h>
#if !defined(MZ_ZIP_NO_COMPRESSION) && \
(defined(HAVE_PKCRYPT) || defined(HAVE_AES))
#if !defined(MZ_ZIP_NO_COMPRESSION) && !defined(MZ_ZIP_NO_ENCRYPTION)
# include <wincrypt.h>
#endif
@ -46,8 +45,7 @@ typedef struct DIR_int_s {
/***************************************************************************/
#if !defined(MZ_ZIP_NO_COMPRESSION) && \
(defined(HAVE_PKCRYPT) || defined(HAVE_AES))
#if !defined(MZ_ZIP_NO_COMPRESSION) && !defined(MZ_ZIP_NO_ENCRYPTION)
int32_t mz_win32_rand(uint8_t *buf, int32_t size)
{
HCRYPTPROV provider;

View File

@ -1261,7 +1261,7 @@ extern int32_t mz_zip_entry_read_open(void *handle, int16_t raw, const char *pas
int16_t compression_method = 0;
int32_t err = MZ_OK;
#if !defined(HAVE_PKCRYPT) && !defined(HAVE_AES)
#if defined(MZ_ZIP_NO_ENCRYPTION)
if (password != NULL)
return MZ_PARAM_ERROR;
#endif
@ -1305,7 +1305,7 @@ extern int32_t mz_zip_entry_write_open(void *handle, const mz_zip_file *file_inf
int16_t compression_method = 0;
#if !defined(HAVE_PKCRYPT) && !defined(HAVE_AES)
#if defined(MZ_ZIP_NO_ENCRYPTION)
if (password != NULL)
return MZ_PARAM_ERROR;
#endif