[minizip-ng] Update: v3.0.4 -> v3.0.6

Among other things, zip.h and unzip.h are now generated in the binary
directory, not the source directory.
This commit is contained in:
David Korth 2022-05-21 14:20:39 -04:00
parent 9762f592b5
commit 3b3d5d8b69
20 changed files with 541 additions and 163 deletions

View File

@ -1,3 +0,0 @@
# Generated files.
zip.h
unzip.h

View File

@ -7,6 +7,8 @@
#cmake_minimum_required(VERSION 3.13)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
message(STATUS "Using CMake version ${CMAKE_VERSION}")
# rom-properties: Disabled these options.
@ -39,7 +41,11 @@ option(MZ_BUILD_UNIT_TESTS "Builds minizip unit test project" OFF)
option(MZ_BUILD_FUZZ_TESTS "Builds minizip fuzzer executables" OFF)
option(MZ_CODE_COVERAGE "Builds with code coverage flags" OFF)
# Package management options
set(MZ_PROJECT_SUFFIX "" CACHE STRING "Project name suffix for package managers")
if(NOT MZ_COMPAT AND NOT DEFINED MZ_PROJECT_SUFFIX)
set(MZ_PROJECT_SUFFIX "-ng" CACHE STRING "Project name suffix for package managers")
else()
set(MZ_PROJECT_SUFFIX "" CACHE STRING "Project name suffix for package managers")
endif()
mark_as_advanced(MZ_FILE32_API MZ_PROJECT_SUFFIX)
ENDIF(0)
@ -62,9 +68,6 @@ if(DEFINED MZ_BUILD_FUZZ_TEST)
set(MZ_BUILD_FUZZ_TESTS ${MZ_BUILD_FUZZ_TEST})
endif()
if(POLICY CMP0074)
cmake_policy(SET CMP0074 OLD)
endif()
if(POLICY CMP0054)
cmake_policy(SET CMP0054 NEW)
endif()
@ -76,7 +79,7 @@ endif()
enable_language(C)
# Library version
set(VERSION "3.0.4")
set(VERSION "3.0.6")
# API version
set(SOVERSION "3")
@ -90,6 +93,8 @@ include(GNUInstallDirs)
# rom-properties: Disabled FeatureSummary.
#include(FeatureSummary)
include(cmake/clone-repo.cmake)
set(INSTALL_BIN_DIR ${CMAKE_INSTALL_BINDIR} CACHE PATH "Installation directory for executables")
set(INSTALL_LIB_DIR ${CMAKE_INSTALL_LIBDIR} CACHE PATH "Installation directory for libraries")
set(INSTALL_INC_DIR ${CMAKE_INSTALL_INCLUDEDIR} CACHE PATH "Installation directory for headers")
@ -159,42 +164,6 @@ if(MSVC)
unset(OLD_CMAKE_REQUIRED_FLAGS)
endif(MSVC)
# Checkout remote repository
macro(clone_repo name url)
if(NOT ${name}_REPOSITORY)
set(${name}_REPOSITORY ${url})
endif()
if(NOT ${name}_TAG)
set(${name}_TAG master)
endif()
message(STATUS "Fetching ${name} ${${name}_REPOSITORY} ${${name}_TAG}")
# Check for FetchContent cmake support
if(${CMAKE_VERSION} VERSION_LESS "3.11")
message(FATAL_ERROR "CMake 3.11 required to fetch ${name}")
else()
include(FetchContent)
string(TOLOWER ${name} name_lower)
string(TOUPPER ${name} name_upper)
FetchContent_Declare(${name}
GIT_REPOSITORY ${${name}_REPOSITORY}
GIT_TAG ${${name}_TAG}
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/lib/${name_lower})
FetchContent_GetProperties(${name} POPULATED ${name_lower}_POPULATED)
if(NOT ${name_lower}_POPULATED)
FetchContent_Populate(${name})
endif()
set(${name_upper}_SOURCE_DIR ${${name_lower}_SOURCE_DIR})
set(${name_upper}_BINARY_DIR ${${name_lower}_BINARY_DIR})
endif()
endmacro()
if(MZ_LIBCOMP)
if(APPLE)
# Use Apple libcompression
@ -215,11 +184,21 @@ 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})
@ -227,6 +206,7 @@ if(MZ_ZLIB)
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)
@ -237,11 +217,17 @@ if(MZ_ZLIB)
list(APPEND MINIZIP_INC ${ZLIB_BINARY_DIR})
# Have to add zlib to install targets
if(NOT DEFINED BUILD_SHARED_LIBS OR NOT ${BUILD_SHARED_LIBS})
if(NOT DEFINED BUILD_SHARED_LIBS OR NOT BUILD_SHARED_LIBS)
list(APPEND MINIZIP_DEP zlibstatic)
else()
list(APPEND MINIZIP_DEP zlib)
endif()
if(EXISTS "${ZLIB_BINARY_DIR}/zlib-ng.h")
message(STATUS "ZLIB repository detected as ZLIBNG")
set(ZLIB_COMPAT OFF)
else()
set(ZLIB_COMPAT ON)
endif()
else()
message(STATUS "ZLIB library not found")
@ -249,11 +235,15 @@ if(MZ_ZLIB)
endif()
if(MZ_ZLIB)
list(APPEND MINIZIP_DEP_PKG ZLIB)
list(APPEND MINIZIP_DEF -DHAVE_ZLIB)
if(ZLIB_COMPAT)
list(APPEND MINIZIP_DEF -DZLIB_COMPAT)
endif()
if(ZLIBNG_FOUND OR NOT ZLIB_COMPAT)
list(APPEND MINIZIP_DEP_PKG ZLIBNG)
elseif(ZLIB_FOUND)
list(APPEND MINIZIP_DEP_PKG ZLIB)
endif()
list(APPEND MINIZIP_SRC mz_strm_zlib.c)
list(APPEND MINIZIP_HDR mz_strm_zlib.h)
endif()
@ -434,11 +424,13 @@ if(MZ_OPENSSL)
list(APPEND MINIZIP_SRC mz_crypt_openssl.c)
list(APPEND MINIZIP_LIB ${OPENSSL_LIBRARIES})
list(APPEND MINIZIP_LBD ${OPENSSL_LIBRARY_DIRS})
list(APPEND MINIZIP_INC ${OPENSSL_INCLUDE_DIR})
if(OPENSSL_INCLUDE_DIRS)
list(APPEND MINIZIP_INC ${OPENSSL_INCLUDE_DIRS})
endif()
if(OPENSSL_INCLUDE_DIR)
list(APPEND MINIZIP_INC ${OPENSSL_INCLUDE_DIR})
endif()
else()
message(STATUS "OpenSSL library not found")
@ -642,30 +634,19 @@ endif()
# Include compatibility layer
if(MZ_COMPAT)
set(COMPAT_HEADER "\
/* file.h -- Compatibility layer shim\n\
part of the minizip-ng project\n\n\
This program is distributed under the terms of the same license as zlib.\n\
See the accompanying LICENSE file for the full text of the license.\n\
*/\n\n\
#ifndef MZ_COMPAT_FILE\n\
#define MZ_COMPAT_FILE\n\n\
#include \"mz_compat.h\"\n\n\
#endif\n")
set(FILE_H "zip.h")
set(MZ_COMPAT_FILE "MZ_COMPAT_ZIP")
configure_file(mz_compat_shim.h.in zip.h)
string(REPLACE "file.h" "zip.h" ZIP_COMPAT_HEADER ${COMPAT_HEADER})
string(REPLACE "MZ_COMPAT_FILE" "MZ_COMPAT_ZIP" ZIP_COMPAT_HEADER ${ZIP_COMPAT_HEADER})
file(WRITE "zip.h" ${ZIP_COMPAT_HEADER})
string(REPLACE "file.h" "unzip.h" UNZIP_COMPAT_HEADER ${COMPAT_HEADER})
string(REPLACE "MZ_COMPAT_FILE" "MZ_COMPAT_UNZIP" UNZIP_COMPAT_HEADER ${UNZIP_COMPAT_HEADER})
file(WRITE "unzip.h" ${UNZIP_COMPAT_HEADER})
set(FILE_H "unzip.h")
set(MZ_COMPAT_FILE "MZ_COMPAT_UNZIP")
configure_file(mz_compat_shim.h.in unzip.h)
if(MZ_COMPAT_VERSION)
list(APPEND MINIZIP_DEF -DMZ_COMPAT_VERSION=${MZ_COMPAT_VERSION})
endif()
list(APPEND MINIZIP_SRC mz_compat.c)
list(APPEND MINIZIP_HDR mz_compat.h zip.h unzip.h)
list(APPEND MINIZIP_HDR mz_compat.h ${CMAKE_CURRENT_BINARY_DIR}/zip.h ${CMAKE_CURRENT_BINARY_DIR}/unzip.h)
endif()
# Set compiler options
@ -703,7 +684,7 @@ endif()
list(APPEND MINIZIP_INC ${CMAKE_CURRENT_SOURCE_DIR})
# Create minizip library
project(minizip${MZ_PROJECT_SUFFIX} VERSION ${VERSION})
project(minizip${MZ_PROJECT_SUFFIX} LANGUAGES C VERSION ${VERSION})
if(NOT ${MZ_PROJECT_SUFFIX} STREQUAL "")
message(STATUS "Project configured as ${PROJECT_NAME}")
@ -732,7 +713,7 @@ if(MSVC)
# VS debugger has problems when executable and static library are named the same
set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME lib${PROJECT_NAME})
endif()
if(NOT RISCOS)
if(NOT RISCOS AND NOT PSP)
set_target_properties(${PROJECT_NAME} PROPERTIES POSITION_INDEPENDENT_CODE 1)
endif()
if(MZ_LZMA)
@ -744,14 +725,20 @@ target_link_libraries(${PROJECT_NAME} PUBLIC ${MINIZIP_LIB} ${MINIZIP_DEP})
target_compile_definitions(${PROJECT_NAME} PRIVATE ${STDLIB_DEF} ${MINIZIP_DEF})
target_include_directories(${PROJECT_NAME} PRIVATE ${MINIZIP_INC})
target_include_directories(${PROJECT_NAME} PUBLIC
$<INSTALL_INTERFACE:${INSTALL_INC_DIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
if(MZ_COMPAT)
target_include_directories(${PROJECT_NAME} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>)
endif()
# Create minizip alias
add_library(MINIZIP::${PROJECT_NAME} ALIAS ${PROJECT_NAME})
add_library(MINIZIP::minizip ALIAS ${PROJECT_NAME})
# Install files
if(NOT SKIP_INSTALL_LIBRARIES AND NOT SKIP_INSTALL_ALL)
target_include_directories(${PROJECT_NAME} PUBLIC
$<INSTALL_INTERFACE:${INSTALL_INC_DIR}>)
install(TARGETS ${PROJECT_NAME} ${MINIZIP_DEP}
EXPORT ${PROJECT_NAME}
INCLUDES DESTINATION "${INSTALL_INC_DIR}"

View File

@ -1,4 +1,4 @@
# minizip-ng 3.0.4
# minizip-ng 3.0.6
minizip-ng is a zip manipulation library written in C that is supported on Windows, macOS, and Linux.
@ -6,7 +6,7 @@ minizip-ng is a zip manipulation library written in C that is supported on Windo
[![Fuzzing Status](https://oss-fuzz-build-logs.storage.googleapis.com/badges/minizip.svg)](https://bugs.chromium.org/p/oss-fuzz/issues/list?sort=-opened&can=1&q=proj:minizip)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/53d48ca8fec549f4a8b39cf95cba6ad6)](https://www.codacy.com/manual/nmoinvaz/minizip?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=nmoinvaz/minizip&amp;utm_campaign=Badge_Grade)
[![License: Zlib](https://img.shields.io/badge/license-zlib-lightgrey.svg)](https://github.com/zlib-ng/minizip-ng/blob/master/LICENSE)
[![codecov.io](https://codecov.io/github/nmoinvaz/minizip/coverage.svg?branch=dev)](https://codecov.io/github/nmoinvaz/minizip/)
[![codecov.io](https://codecov.io/github/nmoinvaz/minizip/coverage.svg?branch=develop)](https://codecov.io/github/nmoinvaz/minizip/)
Developed and maintained by Nathan Moinvaziri.
@ -15,7 +15,7 @@ Developed and maintained by Nathan Moinvaziri.
|Name|Description|
|:-|:-|
|[master](https://github.com/zlib-ng/minizip-ng/tree/master)|Most recent release.|
|[dev](https://github.com/zlib-ng/minizip-ng/tree/dev)|Latest development code.|
|[develop](https://github.com/zlib-ng/minizip-ng/tree/develop)|Latest development code.|
|[1.2](https://github.com/zlib-ng/minizip-ng/tree/1.2)|Old changes to original minizip that includes WinZip AES encryption, disk splitting, I/O buffering and some additional fixes. Not ABI compatible with original minizip.|
|[1.1](https://github.com/zlib-ng/minizip-ng/tree/1.1)|Original minizip as of zlib 1.2.11.|
@ -97,7 +97,7 @@ installed then it will be used, otherwise CMake will retrieve the source code fo
|Project|License|CMake Option|Comments|
|-|-|-|-|
[bzip2](https://www.sourceware.org/bzip2/)|[license](https://github.com/zlib-ng/minizip-ng/blob/dev/lib/bzip2/LICENSE)|`MZ_BZIP2`|Written by Julian Seward.|
[bzip2](https://www.sourceware.org/bzip2/)|[license](https://github.com/zlib-ng/minizip-ng/blob/develop/lib/bzip2/LICENSE)|`MZ_BZIP2`|Written by Julian Seward.|
|[liblzma](https://tukaani.org/xz/)|Public domain|`MZ_LZMA`|Written by Igor Pavlov and Lasse Collin.|
|[zlib](https://zlib.net/)|zlib|`MZ_ZLIB`|Written by Mark Adler and Jean-loup Gailly. Or alternatively, [zlib-ng](https://github.com/zlib-ng/zlib-ng) by Hans Kristian Rosbach.|
|[zstd](https://github.com/facebook/zstd)|[BSD](https://github.com/facebook/zstd/blob/dev/LICENSE)|`MZ_ZSTD`|Written by Facebook.|

View File

@ -1,9 +1,9 @@
This copy of minizip-ng 3.0.4 is a modified version of the original.
This copy of minizip-ng 3.0.6 is a modified version of the original.
commit 95987e98b4862c055b8cf91d6e7ce5f9153ddc24
Version 3.0.4.
commit 47b8449fec2c7a575a506ecbe6399ad0603b5992
Version 3.0.6.
Tag: 3.0.4
Tag: 3.0.6
The following changes have been made to the original:
@ -20,7 +20,7 @@ The following changes have been made to the original:
- Some other functionality not needed by rom-properties has been disabled.
To obtain the original minizip-ng 3.0.4, visit:
To obtain the original minizip-ng 3.0.6, visit:
https://github.com/zlib-ng/minizip-ng
To obtain the original minizip-1.1, visit:

View File

@ -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 ON)
else(ZLIBNG_INCLUDE_DIRS AND ZLIBNG_LIBRARIES)
set(ZLIBNG_FOUND OFF)
endif()
if(ZLIBNG_FOUND)
message(STATUS "Found zlib-ng: ${ZLIBNG_LIBRARIES}, ${ZLIBNG_INCLUDE_DIRS}")
endif()

View File

@ -0,0 +1,35 @@
# Checkout remote repository
macro(clone_repo name url)
string(TOLOWER ${name} name_lower)
string(TOUPPER ${name} name_upper)
if(NOT ${name_upper}_REPOSITORY)
set(${name_upper}_REPOSITORY ${url})
endif()
if(NOT ${name_upper}_TAG)
set(${name_upper}_TAG master)
endif()
message(STATUS "Fetching ${name} ${${name_upper}_REPOSITORY} ${${name_upper}_TAG}")
# Check for FetchContent cmake support
if(${CMAKE_VERSION} VERSION_LESS "3.11")
message(FATAL_ERROR "CMake 3.11 required to fetch ${name}")
else()
include(FetchContent)
FetchContent_Declare(${name}
GIT_REPOSITORY ${${name_upper}_REPOSITORY}
GIT_TAG ${${name_upper}_TAG}
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/lib/${name_lower})
FetchContent_GetProperties(${name} POPULATED ${name_lower}_POPULATED)
if(NOT ${name_lower}_POPULATED)
FetchContent_Populate(${name})
endif()
set(${name_upper}_SOURCE_DIR ${${name_lower}_SOURCE_DIR})
set(${name_upper}_BINARY_DIR ${${name_lower}_BINARY_DIR})
endif()
endmacro()

View File

@ -29,6 +29,7 @@ The _mz_zip_ object allows for the reading and writing of the a zip file and its
- [mz_zip_entry_write](#mz_zip_entry_write)
- [mz_zip_entry_write_close](#mz_zip_entry_write_close)
- [mz_zip_entry_seek_local_header](#mz_zip_entry_seek_local_header)
- [mz_zip_entry_get_compress_stream](#mz_zip_entry_get_compress_stream)
- [mz_zip_entry_close_raw](#mz_zip_entry_close_raw)
- [mz_zip_entry_close](#mz_zip_entry_close)
- [Entry Enumeration](#entry-enumeration)
@ -709,6 +710,20 @@ Seeks to the local header for the entry.
|-|-|
|int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful.|
### mz_zip_entry_get_compress_stream
Gets a pointer to the compression stream for the current entry. Can be used to retrieve the compressed bytes read during an operation.
**Arguments**
|Type|Name|Description|
|-|-|-|
|void *|handle|_mz_zip_ instance|
|void **|compress_stream|Pointer to _mz_stream_ instance|
**Return**
|Type|Description|
|-|-|
|int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful.|
**Example**
```
int32_t err = mz_zip_goto_first_entry(zip_handle);

View File

@ -1,8 +1,8 @@
prefix=@CMAKE_INSTALL_PREFIX@
exec_prefix=@CMAKE_INSTALL_PREFIX@
libdir=@INSTALL_LIB_DIR@
sharedlibdir=@INSTALL_LIB_DIR@
includedir=@INSTALL_INC_DIR@
libdir=@CMAKE_INSTALL_FULL_LIBDIR@
sharedlibdir=@CMAKE_INSTALL_FULL_LIBDIR@
includedir=@CMAKE_INSTALL_FULL_INCLUDEDIR@
Name: @PROJECT_NAME@
Description: Minizip zip file manipulation library

View File

@ -14,8 +14,8 @@
/***************************************************************************/
/* MZ_VERSION */
#define MZ_VERSION ("3.0.4")
#define MZ_VERSION_BUILD (030004)
#define MZ_VERSION ("3.0.6")
#define MZ_VERSION_BUILD (030006)
/* MZ_ERROR */
#define MZ_OK (0) /* zlib */
@ -126,8 +126,14 @@
#define MZ_HASH_MD5_SIZE (16)
#define MZ_HASH_SHA1 (20)
#define MZ_HASH_SHA1_SIZE (20)
#define MZ_HASH_SHA224 (22)
#define MZ_HASH_SHA224_SIZE (28)
#define MZ_HASH_SHA256 (23)
#define MZ_HASH_SHA256_SIZE (32)
#define MZ_HASH_SHA384 (24)
#define MZ_HASH_SHA384_SIZE (48)
#define MZ_HASH_SHA512 (25)
#define MZ_HASH_SHA512_SIZE (64)
#define MZ_HASH_MAX_SIZE (256)
/* MZ_ENCODING */

12
extlib/minizip-ng/mz_compat_shim.h.in vendored Normal file
View File

@ -0,0 +1,12 @@
/* @FILE_H@ -- Compatibility layer shim
part of the minizip-ng project
This program is distributed under the terms of the same license as zlib.
See the accompanying LICENSE file for the full text of the license.
*/
#ifndef @MZ_COMPAT_FILE@
#define @MZ_COMPAT_FILE@
#include "mz_compat.h"
#endif

View File

@ -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)
@ -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);

View File

@ -29,15 +29,25 @@ int32_t mz_crypt_rand(uint8_t *buf, int32_t size) {
/***************************************************************************/
typedef struct mz_crypt_sha_s {
CC_SHA1_CTX ctx1;
CC_SHA256_CTX ctx256;
int32_t error;
int32_t initialized;
uint16_t algorithm;
union {
CC_SHA1_CTX ctx1;
CC_SHA256_CTX ctx256;
CC_SHA512_CTX ctx512;
};
int32_t error;
int32_t initialized;
uint16_t algorithm;
} mz_crypt_sha;
/***************************************************************************/
static const uint8_t mz_crypt_sha_digest_size[] = {
MZ_HASH_SHA1_SIZE, 0, MZ_HASH_SHA224_SIZE,
MZ_HASH_SHA256_SIZE, MZ_HASH_SHA384_SIZE, MZ_HASH_SHA512_SIZE
};
/***************************************************************************/
void mz_crypt_sha_reset(void *handle) {
mz_crypt_sha *sha = (mz_crypt_sha *)handle;
@ -53,12 +63,25 @@ int32_t mz_crypt_sha_begin(void *handle) {
mz_crypt_sha_reset(handle);
if (sha->algorithm == MZ_HASH_SHA1)
switch (sha->algorithm) {
case MZ_HASH_SHA1:
sha->error = CC_SHA1_Init(&sha->ctx1);
else if (sha->algorithm == MZ_HASH_SHA256)
break;
case MZ_HASH_SHA224:
sha->error = CC_SHA224_Init(&sha->ctx256);
break;
case MZ_HASH_SHA256:
sha->error = CC_SHA256_Init(&sha->ctx256);
else
break;
case MZ_HASH_SHA384:
sha->error = CC_SHA384_Init(&sha->ctx512);
break;
case MZ_HASH_SHA512:
sha->error = CC_SHA512_Init(&sha->ctx512);
break;
default:
return MZ_PARAM_ERROR;
}
if (!sha->error)
return MZ_HASH_ERROR;
@ -73,10 +96,23 @@ int32_t mz_crypt_sha_update(void *handle, const void *buf, int32_t size) {
if (sha == NULL || buf == NULL || !sha->initialized)
return MZ_PARAM_ERROR;
if (sha->algorithm == MZ_HASH_SHA1)
switch (sha->algorithm) {
case MZ_HASH_SHA1:
sha->error = CC_SHA1_Update(&sha->ctx1, buf, size);
else
break;
case MZ_HASH_SHA224:
sha->error = CC_SHA224_Update(&sha->ctx256, buf, size);
break;
case MZ_HASH_SHA256:
sha->error = CC_SHA256_Update(&sha->ctx256, buf, size);
break;
case MZ_HASH_SHA384:
sha->error = CC_SHA384_Update(&sha->ctx512, buf, size);
break;
case MZ_HASH_SHA512:
sha->error = CC_SHA512_Update(&sha->ctx512, buf, size);
break;
}
if (!sha->error)
return MZ_HASH_ERROR;
@ -89,15 +125,25 @@ int32_t mz_crypt_sha_end(void *handle, uint8_t *digest, int32_t digest_size) {
if (sha == NULL || digest == NULL || !sha->initialized)
return MZ_PARAM_ERROR;
if (digest_size < mz_crypt_sha_digest_size[sha->algorithm - MZ_HASH_SHA1])
return MZ_PARAM_ERROR;
if (sha->algorithm == MZ_HASH_SHA1) {
if (digest_size < MZ_HASH_SHA1_SIZE)
return MZ_BUF_ERROR;
switch (sha->algorithm) {
case MZ_HASH_SHA1:
sha->error = CC_SHA1_Final(digest, &sha->ctx1);
} else {
if (digest_size < MZ_HASH_SHA256_SIZE)
return MZ_BUF_ERROR;
break;
case MZ_HASH_SHA224:
sha->error = CC_SHA224_Final(digest, &sha->ctx256);
break;
case MZ_HASH_SHA256:
sha->error = CC_SHA256_Final(digest, &sha->ctx256);
break;
case MZ_HASH_SHA384:
sha->error = CC_SHA384_Final(digest, &sha->ctx512);
break;
case MZ_HASH_SHA512:
sha->error = CC_SHA512_Final(digest, &sha->ctx512);
break;
}
if (!sha->error)
@ -108,7 +154,8 @@ int32_t mz_crypt_sha_end(void *handle, uint8_t *digest, int32_t digest_size) {
void mz_crypt_sha_set_algorithm(void *handle, uint16_t algorithm) {
mz_crypt_sha *sha = (mz_crypt_sha *)handle;
sha->algorithm = algorithm;
if (MZ_HASH_SHA1 <= algorithm && algorithm <= MZ_HASH_SHA512)
sha->algorithm = algorithm;
}
void *mz_crypt_sha_create(void **handle) {

View File

@ -61,15 +61,25 @@ int32_t mz_crypt_rand(uint8_t *buf, int32_t size) {
/***************************************************************************/
typedef struct mz_crypt_sha_s {
SHA256_CTX ctx256;
SHA_CTX ctx1;
int32_t initialized;
int32_t error;
uint16_t algorithm;
union {
SHA512_CTX ctx512;
SHA256_CTX ctx256;
SHA_CTX ctx1;
};
int32_t initialized;
int32_t error;
uint16_t algorithm;
} mz_crypt_sha;
/***************************************************************************/
static const uint8_t mz_crypt_sha_digest_size[] = {
MZ_HASH_SHA1_SIZE, 0, MZ_HASH_SHA224_SIZE,
MZ_HASH_SHA256_SIZE, MZ_HASH_SHA384_SIZE, MZ_HASH_SHA512_SIZE
};
/***************************************************************************/
void mz_crypt_sha_reset(void *handle) {
mz_crypt_sha *sha = (mz_crypt_sha *)handle;
@ -83,16 +93,28 @@ int32_t mz_crypt_sha_begin(void *handle) {
mz_crypt_sha *sha = (mz_crypt_sha *)handle;
int32_t result = 0;
if (sha == NULL)
return MZ_PARAM_ERROR;
mz_crypt_sha_reset(handle);
if (sha->algorithm == MZ_HASH_SHA1)
switch (sha->algorithm) {
case MZ_HASH_SHA1:
result = SHA1_Init(&sha->ctx1);
else
break;
case MZ_HASH_SHA224:
result = SHA224_Init(&sha->ctx256);
break;
case MZ_HASH_SHA256:
result = SHA256_Init(&sha->ctx256);
break;
case MZ_HASH_SHA384:
result = SHA384_Init(&sha->ctx512);
break;
case MZ_HASH_SHA512:
result = SHA512_Init(&sha->ctx512);
break;
}
if (!result) {
sha->error = ERR_get_error();
@ -110,10 +132,23 @@ int32_t mz_crypt_sha_update(void *handle, const void *buf, int32_t size) {
if (sha == NULL || buf == NULL || !sha->initialized)
return MZ_PARAM_ERROR;
if (sha->algorithm == MZ_HASH_SHA1)
switch (sha->algorithm) {
case MZ_HASH_SHA1:
result = SHA1_Update(&sha->ctx1, buf, size);
else
break;
case MZ_HASH_SHA224:
result = SHA224_Update(&sha->ctx256, buf, size);
break;
case MZ_HASH_SHA256:
result = SHA256_Update(&sha->ctx256, buf, size);
break;
case MZ_HASH_SHA384:
result = SHA384_Update(&sha->ctx512, buf, size);
break;
case MZ_HASH_SHA512:
result = SHA512_Update(&sha->ctx512, buf, size);
break;
}
if (!result) {
sha->error = ERR_get_error();
@ -129,15 +164,25 @@ int32_t mz_crypt_sha_end(void *handle, uint8_t *digest, int32_t digest_size) {
if (sha == NULL || digest == NULL || !sha->initialized)
return MZ_PARAM_ERROR;
if (digest_size < mz_crypt_sha_digest_size[sha->algorithm - MZ_HASH_SHA1])
return MZ_PARAM_ERROR;
if (sha->algorithm == MZ_HASH_SHA1) {
if (digest_size < MZ_HASH_SHA1_SIZE)
return MZ_BUF_ERROR;
switch (sha->algorithm) {
case MZ_HASH_SHA1:
result = SHA1_Final(digest, &sha->ctx1);
} else {
if (digest_size < MZ_HASH_SHA256_SIZE)
return MZ_BUF_ERROR;
break;
case MZ_HASH_SHA224:
result = SHA224_Final(digest, &sha->ctx256);
break;
case MZ_HASH_SHA256:
result = SHA256_Final(digest, &sha->ctx256);
break;
case MZ_HASH_SHA384:
result = SHA384_Final(digest, &sha->ctx512);
break;
case MZ_HASH_SHA512:
result = SHA512_Final(digest, &sha->ctx512);
break;
}
if (!result) {
@ -150,7 +195,8 @@ int32_t mz_crypt_sha_end(void *handle, uint8_t *digest, int32_t digest_size) {
void mz_crypt_sha_set_algorithm(void *handle, uint16_t algorithm) {
mz_crypt_sha *sha = (mz_crypt_sha *)handle;
sha->algorithm = algorithm;
if (MZ_HASH_SHA1 <= algorithm && algorithm <= MZ_HASH_SHA512)
sha->algorithm = algorithm;
}
void *mz_crypt_sha_create(void **handle) {

View File

@ -35,23 +35,173 @@ int32_t mz_crypt_rand(uint8_t *buf, int32_t size) {
/***************************************************************************/
/* Adapted from RFC4634 &Igor Pavlov's 2010 public domain implementation */
typedef struct mz_crypt_sha224_s {
uint8_t buffer[64];
uint32_t state[8];
uint64_t count;
} mz_crypt_sha224;
/***************************************************************************/
#define rotl(x, n) (((x) << (n)) | ((x) >> ((8 * sizeof(x)) - (n))))
#define rotr(x, n) (((x) >> (n)) | ((x) << ((8 * sizeof(x)) - (n))))
#define Ch(x,y,z) (z ^ (x & (y ^ z)))
#define Maj(x,y,z) ((x & y) | (z & (x | y)))
#define S0_256(x) (rotr(x, 2) ^ rotr(x,13) ^ rotr(x, 22))
#define S1_256(x) (rotr(x, 6) ^ rotr(x,11) ^ rotr(x, 25))
#define s0_256(x) (rotr(x, 7) ^ rotr(x,18) ^ (x >> 3))
#define s1_256(x) (rotr(x,17) ^ rotr(x,19) ^ (x >> 10))
#define blk0(i) (w[i] = buffer[i])
#define blk2(i) (w[i&15] += s1_256(w[(i-2)&15]) + w[(i-7)&15] + s0_256(w[(i-15)&15]))
#define R(a,b,c,d,e,f,g,h,i) \
h += S1_256(e) + Ch(e,f,g) + k256[i+j] + (j?blk2(i):blk0(i)); \
d += h; h += S0_256(a) + Maj(a, b, c)
#define RX_8(i) \
R(a,b,c,d,e,f,g,h, (i)); \
R(h,a,b,c,d,e,f,g, (i+1)); \
R(g,h,a,b,c,d,e,f, (i+2)); \
R(f,g,h,a,b,c,d,e, (i+3)); \
R(e,f,g,h,a,b,c,d, (i+4)); \
R(d,e,f,g,h,a,b,c, (i+5)); \
R(c,d,e,f,g,h,a,b, (i+6)); \
R(b,c,d,e,f,g,h,a, (i+7))
static const uint32_t k256[64] = {
0x428a2f98u, 0x71374491u, 0xb5c0fbcfu, 0xe9b5dba5u,
0x3956c25bu, 0x59f111f1u, 0x923f82a4u, 0xab1c5ed5u,
0xd807aa98u, 0x12835b01u, 0x243185beu, 0x550c7dc3u,
0x72be5d74u, 0x80deb1feu, 0x9bdc06a7u, 0xc19bf174u,
0xe49b69c1u, 0xefbe4786u, 0x0fc19dc6u, 0x240ca1ccu,
0x2de92c6fu, 0x4a7484aau, 0x5cb0a9dcu, 0x76f988dau,
0x983e5152u, 0xa831c66du, 0xb00327c8u, 0xbf597fc7u,
0xc6e00bf3u, 0xd5a79147u, 0x06ca6351u, 0x14292967u,
0x27b70a85u, 0x2e1b2138u, 0x4d2c6dfcu, 0x53380d13u,
0x650a7354u, 0x766a0abbu, 0x81c2c92eu, 0x92722c85u,
0xa2bfe8a1u, 0xa81a664bu, 0xc24b8b70u, 0xc76c51a3u,
0xd192e819u, 0xd6990624u, 0xf40e3585u, 0x106aa070u,
0x19a4c116u, 0x1e376c08u, 0x2748774cu, 0x34b0bcb5u,
0x391c0cb3u, 0x4ed8aa4au, 0x5b9cca4fu, 0x682e6ff3u,
0x748f82eeu, 0x78a5636fu, 0x84c87814u, 0x8cc70208u,
0x90befffau, 0xa4506cebu, 0xbef9a3f7u, 0xc67178f2u
};
/***************************************************************************/
static void mz_crypt_sha224_init(mz_crypt_sha224 *sha) {
sha->state[0] = 0xc1059ed8u;
sha->state[1] = 0x367cd507u;
sha->state[2] = 0x3070dd17u;
sha->state[3] = 0xf70e5939u;
sha->state[4] = 0xffc00b31u;
sha->state[5] = 0x68581511u;
sha->state[6] = 0x64f98fa7u;
sha->state[7] = 0xbefa4fa4u;
sha->count = 0;
}
static void mz_crypt_sha224_transform(uint32_t *state, const uint32_t *buffer) {
uint32_t w[16];
int32_t j = 0;
uint32_t a = state[0], b = state[1], c = state[2], d = state[3];
uint32_t e = state[4], f = state[5], g = state[6], h = state[7];
for (j = 0; j < 64; j += 16) {
RX_8(0);
RX_8(8);
}
state[0] += a; state[1] += b; state[2] += c; state[3] += d;
state[4] += e; state[5] += f; state[6] += g; state[7] += h;
}
static void mz_crypt_sha224_write_byte_block(mz_crypt_sha224 *sha) {
uint32_t data32[16];
int32_t i = 0;
for (i = 0; i < 16; i++) {
data32[i] = ((uint32_t)(sha->buffer[i * 4 + 0]) << 24) +
((uint32_t)(sha->buffer[i * 4 + 1]) << 16) +
((uint32_t)(sha->buffer[i * 4 + 2]) << 8 ) +
((uint32_t)(sha->buffer[i * 4 + 3]));
}
mz_crypt_sha224_transform(sha->state, data32);
}
static void mz_crypt_sha224_update(mz_crypt_sha224 *sha, const uint8_t *data, size_t size) {
uint32_t pos = (uint32_t)sha->count & 0x3F;
while (size > 0) {
sha->buffer[pos++] = *data++;
sha->count++;
size--;
if (pos == 64) {
pos = 0;
mz_crypt_sha224_write_byte_block(sha);
}
}
}
static void mz_crypt_sha224_end(mz_crypt_sha224 *sha, uint8_t *digest) {
uint64_t bits = (sha->count << 3);
uint32_t pos = (uint32_t)sha->count & 0x3F;
int32_t i = 0;
sha->buffer[pos++] = 0x80;
while (pos != (64 - 8)) {
pos &= 0x3F;
if (pos == 0)
mz_crypt_sha224_write_byte_block(sha);
sha->buffer[pos++] = 0;
}
for (i = 0; i < 8; i++) {
sha->buffer[pos++] = (uint8_t)(bits >> 56);
bits <<= 8;
}
mz_crypt_sha224_write_byte_block(sha);
for (i = 0; i < 7; i++) {
*digest++ = (uint8_t)(sha->state[i] >> 24);
*digest++ = (uint8_t)(sha->state[i] >> 16);
*digest++ = (uint8_t)(sha->state[i] >> 8 );
*digest++ = (uint8_t)(sha->state[i]);
}
mz_crypt_sha224_init(sha);
}
/***************************************************************************/
typedef struct mz_crypt_sha_s {
HCRYPTPROV provider;
HCRYPTHASH hash;
int32_t error;
uint16_t algorithm;
union {
struct {
HCRYPTPROV provider;
HCRYPTHASH hash;
};
mz_crypt_sha224 *sha224;
};
int32_t error;
uint16_t algorithm;
} mz_crypt_sha;
/***************************************************************************/
void mz_crypt_sha_reset(void *handle) {
mz_crypt_sha *sha = (mz_crypt_sha *)handle;
if (sha->hash)
CryptDestroyHash(sha->hash);
sha->hash = 0;
if (sha->provider)
CryptReleaseContext(sha->provider, 0);
sha->provider = 0;
if (sha->algorithm == MZ_HASH_SHA224) {
MZ_FREE(sha->sha224);
sha->sha224 = NULL;
}
else {
if (sha->hash)
CryptDestroyHash(sha->hash);
sha->hash = 0;
if (sha->provider)
CryptReleaseContext(sha->provider, 0);
sha->provider = 0;
}
sha->error = 0;
}
@ -65,10 +215,29 @@ int32_t mz_crypt_sha_begin(void *handle) {
if (sha == NULL)
return MZ_PARAM_ERROR;
if (sha->algorithm == MZ_HASH_SHA1)
if (sha->algorithm == MZ_HASH_SHA224) {
sha->sha224 = MZ_ALLOC(sizeof(mz_crypt_sha224));
if (sha->sha224 == NULL)
return MZ_MEM_ERROR;
mz_crypt_sha224_init(sha->sha224);
return MZ_OK;
}
switch (sha->algorithm)
{
case MZ_HASH_SHA1:
alg_id = CALG_SHA1;
else
break;
case MZ_HASH_SHA256:
alg_id = CALG_SHA_256;
break;
case MZ_HASH_SHA384:
alg_id = CALG_SHA_384;
break;
case MZ_HASH_SHA512:
alg_id = CALG_SHA_512;
break;
}
result = CryptAcquireContext(&sha->provider, NULL, NULL, PROV_RSA_AES, CRYPT_VERIFYCONTEXT | CRYPT_SILENT);
if (!result) {
@ -91,8 +260,19 @@ int32_t mz_crypt_sha_update(void *handle, const void *buf, int32_t size) {
mz_crypt_sha *sha = (mz_crypt_sha *)handle;
int32_t result = 0;
if (sha == NULL || buf == NULL || sha->hash == 0)
if (sha == NULL || buf == NULL || size < 0)
return MZ_PARAM_ERROR;
if (sha->algorithm == MZ_HASH_SHA224) {
if (sha->sha224 == NULL)
return MZ_PARAM_ERROR;
mz_crypt_sha224_update(sha->sha224, buf, size);
return size;
}
if (sha->hash == 0)
return MZ_PARAM_ERROR;
result = CryptHashData(sha->hash, buf, size, 0);
if (!result) {
sha->error = GetLastError();
@ -106,8 +286,19 @@ int32_t mz_crypt_sha_end(void *handle, uint8_t *digest, int32_t digest_size) {
int32_t result = 0;
int32_t expected_size = 0;
if (sha == NULL || digest == NULL || sha->hash == 0)
if (sha == NULL || digest == NULL)
return MZ_PARAM_ERROR;
if (sha->algorithm == MZ_HASH_SHA224) {
if (sha->sha224 == NULL || digest_size < 28)
return MZ_PARAM_ERROR;
mz_crypt_sha224_end(sha->sha224, digest);
return MZ_OK;
}
if (sha->hash == 0)
return MZ_PARAM_ERROR;
result = CryptGetHashParam(sha->hash, HP_HASHVAL, NULL, (DWORD *)&expected_size, 0);
if (expected_size > digest_size)
return MZ_BUF_ERROR;

View File

@ -33,7 +33,9 @@ int32_t mz_path_combine(char *path, const char *join, int32_t max_path) {
path[max_path - 1] = 0;
} else {
mz_path_append_slash(path, max_path, MZ_PATH_SLASH_PLATFORM);
strncat(path, join, max_path - path_len);
path_len = (int32_t)strlen(path);
if (max_path > path_len)
strncat(path, join, max_path - path_len - 1);
}
return MZ_OK;
@ -277,17 +279,17 @@ int32_t mz_path_get_filename(const char *path, const char **filename) {
int32_t mz_dir_make(const char *path) {
int32_t err = MZ_OK;
int16_t len = 0;
size_t len = 0;
char *current_dir = NULL;
char *match = NULL;
char hold = 0;
len = (int16_t)strlen(path);
if (len <= 0)
len = strlen(path);
if (len <= 0 || len > INT16_MAX)
return 0;
current_dir = (char *)MZ_ALLOC((uint16_t)len + 1);
current_dir = (char *)MZ_ALLOC(len + 1);
if (current_dir == NULL)
return MZ_MEM_ERROR;

View File

@ -222,7 +222,8 @@ int32_t mz_os_get_file_date(const char *path, time_t *modified_date, time_t *acc
/* Not all systems allow stat'ing a file with / appended */
len = strlen(path);
name = (char *)malloc(len + 1);
strncpy(name, path, len + 1);
strncpy(name, path, len);
name[len - 1] = 0;
mz_path_remove_slash(name);
if (stat(name, &path_stat) == 0) {

View File

@ -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

View File

@ -2231,6 +2231,16 @@ int32_t mz_zip_entry_seek_local_header(void *handle) {
return mz_stream_seek(zip->stream, zip->file_info.disk_offset + zip->disk_offset_shift, MZ_SEEK_SET);
}
int32_t mz_zip_entry_get_compress_stream(void *handle, void **compress_stream) {
mz_zip *zip = (mz_zip *)handle;
if (zip == NULL || compress_stream == NULL)
return MZ_PARAM_ERROR;
*compress_stream = zip->compress_stream;
if (*compress_stream == NULL)
return MZ_EXIST_ERROR;
return MZ_OK;
}
int32_t mz_zip_entry_close(void *handle) {
return mz_zip_entry_close_raw(handle, UINT64_MAX, 0);
}

View File

@ -139,6 +139,9 @@ int32_t mz_zip_entry_write_close(void *handle, uint32_t crc32, int64_t compresse
int32_t mz_zip_entry_seek_local_header(void *handle);
/* Seeks to the local header for the entry */
int32_t mz_zip_entry_get_compress_stream(void *handle, void **compress_stream);
/* Get a pointer to the compression stream used for the current entry */
int32_t mz_zip_entry_close_raw(void *handle, int64_t uncompressed_size, uint32_t crc32);
/* Close the current file in the zip file where raw is compressed data */

View File

@ -211,8 +211,8 @@ int32_t mz_zip_reader_close(void *handle) {
mz_stream_os_delete(&reader->file_stream);
if (reader->mem_stream != NULL) {
mz_stream_mem_close(reader->mem_stream);
mz_stream_mem_delete(&reader->mem_stream);
mz_stream_close(reader->mem_stream);
mz_stream_delete(&reader->mem_stream);
}
return err;
@ -1143,7 +1143,8 @@ int32_t mz_zip_writer_open_file(void *handle, const char *path, int64_t disk_siz
/* Create destination directory if it doesn't already exist */
if (strchr(path, '/') != NULL || strrchr(path, '\\') != NULL) {
strncpy(directory, path, sizeof(directory));
strncpy(directory, path, sizeof(directory) - 1);
directory[sizeof(directory) - 1] = 0;
mz_path_remove_filename(directory);
if (mz_os_file_exists(directory) != MZ_OK)
mz_dir_make(directory);