mirror of
https://github.com/zlib-ng/minizip-ng
synced 2025-03-28 21:13:18 +00:00
Compare commits
99 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
4f571d6e38 | ||
![]() |
1529686f37 | ||
![]() |
5e99d52a00 | ||
![]() |
e6eeb4ea52 | ||
![]() |
dd91a4db20 | ||
![]() |
55db144e03 | ||
![]() |
888d4713f6 | ||
![]() |
4a1d10faa2 | ||
![]() |
cf5404bb71 | ||
![]() |
c808a65a0e | ||
![]() |
6d45beb98d | ||
![]() |
d7f5fdd19a | ||
![]() |
00d695db9b | ||
![]() |
32b66976a7 | ||
![]() |
34111e29e9 | ||
![]() |
3bced613bc | ||
![]() |
522196762d | ||
![]() |
6a1a1eed0b | ||
![]() |
3438b8832d | ||
![]() |
91182928a4 | ||
![]() |
91112baa26 | ||
![]() |
53e63823f5 | ||
![]() |
2c51fa719e | ||
![]() |
483950d09d | ||
![]() |
39255ca709 | ||
![]() |
c460967d7a | ||
![]() |
20c6f562d1 | ||
![]() |
8339456a44 | ||
![]() |
7df56f7cc8 | ||
![]() |
cc14474b37 | ||
![]() |
0bf8db3cff | ||
![]() |
8950c248ae | ||
![]() |
6946ba67d0 | ||
![]() |
b2691a0c96 | ||
![]() |
95fdb49672 | ||
![]() |
9aeff7bc29 | ||
![]() |
40416c35c3 | ||
![]() |
23bf392d1b | ||
![]() |
0ee98b6909 | ||
![]() |
fe5fedc365 | ||
![]() |
bfc8f6c9e1 | ||
![]() |
b887f8104b | ||
![]() |
1ddb23dfc0 | ||
![]() |
a64aa8bb4b | ||
![]() |
51ab7c599d | ||
![]() |
2f380cbbbf | ||
![]() |
71334463b2 | ||
![]() |
437d2f0e4d | ||
![]() |
8a00c0e9fb | ||
![]() |
d3bc79cd0d | ||
![]() |
f1e9f85777 | ||
![]() |
db940ff659 | ||
![]() |
1e402d2c2a | ||
![]() |
4cec116210 | ||
![]() |
07db361a6e | ||
![]() |
288b2e249e | ||
![]() |
c7ed3c8e4c | ||
![]() |
0725402ff1 | ||
![]() |
5aec551bf7 | ||
![]() |
990f512941 | ||
![]() |
3f9bb5ac2f | ||
![]() |
a8aa7542fb | ||
![]() |
0dbe04ceca | ||
![]() |
ab65b0b19b | ||
![]() |
d26500513c | ||
![]() |
59a32bd65b | ||
![]() |
3eed562ef0 | ||
![]() |
552b4831da | ||
![]() |
168f9ffee7 | ||
![]() |
0c3a741b36 | ||
![]() |
4bd8ebc090 | ||
![]() |
32f91ade14 | ||
![]() |
e91cdbc485 | ||
![]() |
a7d612adf2 | ||
![]() |
13b01a74a4 | ||
![]() |
d1f4d18899 | ||
![]() |
0211461993 | ||
![]() |
782b139ee7 | ||
![]() |
487259b09a | ||
![]() |
bacf5122ac | ||
![]() |
6c5f265a55 | ||
![]() |
fbd8443122 | ||
![]() |
2c2d6e5940 | ||
![]() |
ef3ef9a4b8 | ||
![]() |
933cedf2bb | ||
![]() |
3417608874 | ||
![]() |
42b6b8997c | ||
![]() |
3920698d13 | ||
![]() |
97d8e65dc8 | ||
![]() |
1e9c6d8361 | ||
![]() |
2a5543a2b0 | ||
![]() |
4f670cc7d7 | ||
![]() |
145754983c | ||
![]() |
26c484d7ce | ||
![]() |
0d81d2eaae | ||
![]() |
aa4f87309d | ||
![]() |
2bfdd6e6a7 | ||
![]() |
7cb316f28b | ||
![]() |
21606891ab |
22
.clang-format
Normal file
22
.clang-format
Normal file
@ -0,0 +1,22 @@
|
||||
---
|
||||
BasedOnStyle: Google
|
||||
|
||||
AccessModifierOffset: -2
|
||||
AlignArrayOfStructures: Right
|
||||
AlignConsecutiveBitFields: AcrossEmptyLines
|
||||
AlignConsecutiveMacros: AcrossEmptyLines
|
||||
AlignEscapedNewlines: Right
|
||||
AlignOperands: DontAlign
|
||||
AllowShortFunctionsOnASingleLine: Empty
|
||||
AllowShortIfStatementsOnASingleLine: Never
|
||||
AllowShortLambdasOnASingleLine: Empty
|
||||
AllowShortLoopsOnASingleLine: false
|
||||
ColumnLimit: 120
|
||||
DerivePointerAlignment: false
|
||||
IndentCaseLabels: false
|
||||
IndentPPDirectives: AfterHash
|
||||
IndentWidth: 4
|
||||
InsertNewlineAtEOF: true
|
||||
PointerAlignment: Right
|
||||
PPIndentWidth: 2
|
||||
SortIncludes: Never
|
7
.github/dependabot.yml
vendored
Normal file
7
.github/dependabot.yml
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
version: 2
|
||||
updates:
|
||||
- package-ecosystem: "github-actions"
|
||||
directory: "/"
|
||||
schedule:
|
||||
interval: "monthly"
|
||||
target-branch: "develop"
|
22
.github/workflows/build.yml
vendored
22
.github/workflows/build.yml
vendored
@ -1,5 +1,8 @@
|
||||
name: Build
|
||||
on: [push, pull_request]
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
|
||||
cancel-in-progress: true
|
||||
jobs:
|
||||
ci:
|
||||
name: ${{ matrix.name }}
|
||||
@ -206,12 +209,12 @@ jobs:
|
||||
|
||||
- name: macOS Xcode OpenSSL
|
||||
os: macos-latest
|
||||
cmake-args: -D MZ_CODE_COVERAGE=ON -D MZ_OPENSSL=ON -D OPENSSL_ROOT_DIR=/usr/local/opt/openssl -D OPENSSL_INCLUDE_DIRS=/usr/local/opt/openssl/include -D OPENSSL_CRYPTO_LIBRARY=/usr/local/opt/openssl/lib/libcrypto.dylib -D OPENSSL_SSL_LIBRARY=/usr/local/opt/openssl/lib/libssl.dylib
|
||||
cmake-args: -D MZ_CODE_COVERAGE=ON -D MZ_OPENSSL=ON
|
||||
codecov: macos_xcode_openssl
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v3
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Install packages (macOS)
|
||||
if: runner.os == 'macOS'
|
||||
@ -236,6 +239,7 @@ jobs:
|
||||
cmake -S ${{ matrix.build-src-dir || '.' }} -B ${{ matrix.build-dir || '.' }} ${{ matrix.cmake-args }} \
|
||||
-D MZ_BUILD_TESTS=ON \
|
||||
-D MZ_BUILD_UNIT_TESTS=ON \
|
||||
-D BUILD_SHARED_LIBS=OFF \
|
||||
-D CMAKE_BUILD_TYPE=Release
|
||||
env:
|
||||
CC: ${{ matrix.compiler }}
|
||||
@ -249,12 +253,18 @@ jobs:
|
||||
- name: Run test cases
|
||||
run: ctest --output-on-failure -C ${{ matrix.build-config || 'Release' }}
|
||||
working-directory: ${{ matrix.build-dir }}
|
||||
|
||||
- name: Setup python
|
||||
uses: actions/setup-python@v5
|
||||
if: always() && matrix.codecov
|
||||
with:
|
||||
python-version: '3.x'
|
||||
|
||||
- name: Generate coverage report
|
||||
shell: bash
|
||||
if: always() && matrix.codecov
|
||||
run: |
|
||||
python3 -u -m pip install --user gcovr==5.0 --ignore-installed
|
||||
python3 -u -m pip install gcovr
|
||||
python3 -m gcovr \
|
||||
--exclude-unreachable-branches \
|
||||
--gcov-ignore-parse-errors \
|
||||
@ -265,7 +275,7 @@ jobs:
|
||||
--verbose
|
||||
|
||||
- name: Upload coverage report
|
||||
uses: codecov/codecov-action@v3
|
||||
uses: codecov/codecov-action@v5
|
||||
if: always() && matrix.codecov && env.CODECOV_TOKEN != ''
|
||||
with:
|
||||
token: ${{ secrets.CODECOV_TOKEN }}
|
||||
@ -282,7 +292,7 @@ jobs:
|
||||
run: ls -R mini*zip | tar -czvf minizip-ng-${{ matrix.deploy-name }}.tar.gz -T -
|
||||
|
||||
- name: Upload release (Ubuntu/macOS)
|
||||
uses: svenstaro/upload-release-action@v1-release
|
||||
uses: svenstaro/upload-release-action@2.9.0
|
||||
if: (runner.os == 'Linux' || runner.os == 'macOS') && matrix.deploy && startsWith(github.ref, 'refs/tags/') && env.GITHUB_TOKEN != ''
|
||||
with:
|
||||
asset_name: minizip-ng-${{ matrix.deploy-name }}.tar.gz
|
||||
@ -298,7 +308,7 @@ jobs:
|
||||
run: 7z a -tzip minizip-ng-${{ matrix.deploy-name }}.zip ./Release/mini*zip.exe
|
||||
|
||||
- name: Upload release (Windows)
|
||||
uses: svenstaro/upload-release-action@v1-release
|
||||
uses: svenstaro/upload-release-action@2.9.0
|
||||
if: runner.os == 'Windows' && matrix.deploy && startsWith(github.ref, 'refs/tags/') && env.GITHUB_TOKEN != ''
|
||||
with:
|
||||
asset_name: minizip-ng-${{ matrix.deploy-name }}.zip
|
||||
|
16
.github/workflows/codeql.yml
vendored
16
.github/workflows/codeql.yml
vendored
@ -4,14 +4,20 @@ on:
|
||||
pull_request:
|
||||
schedule:
|
||||
- cron: '0 1 * * 4'
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
|
||||
cancel-in-progress: true
|
||||
jobs:
|
||||
analyze:
|
||||
name: Analyze
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
permissions:
|
||||
actions: read
|
||||
contents: read
|
||||
security-events: write
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v3
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
# We must fetch at least the immediate parents so that if this is
|
||||
# a pull request then we can checkout the head.
|
||||
@ -19,12 +25,12 @@ jobs:
|
||||
|
||||
# Initializes the CodeQL tools for scanning.
|
||||
- name: Initialize CodeQL
|
||||
uses: github/codeql-action/init@v2
|
||||
uses: github/codeql-action/init@v3
|
||||
|
||||
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
||||
# If this step fails, then you should remove it and run the build manually (see below)
|
||||
- name: Autobuild
|
||||
uses: github/codeql-action/autobuild@v2
|
||||
uses: github/codeql-action/autobuild@v3
|
||||
|
||||
- name: Perform CodeQL Analysis
|
||||
uses: github/codeql-action/analyze@v2
|
||||
uses: github/codeql-action/analyze@v3
|
||||
|
5
.github/workflows/fuzz.yml
vendored
5
.github/workflows/fuzz.yml
vendored
@ -1,5 +1,8 @@
|
||||
name: OSS-Fuzz
|
||||
on: [pull_request]
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
|
||||
cancel-in-progress: true
|
||||
jobs:
|
||||
Fuzzing:
|
||||
runs-on: ubuntu-latest
|
||||
@ -18,7 +21,7 @@ jobs:
|
||||
dry-run: true
|
||||
|
||||
- name: Upload Crash
|
||||
uses: actions/upload-artifact@v3
|
||||
uses: actions/upload-artifact@v4
|
||||
if: failure()
|
||||
with:
|
||||
name: artifacts
|
||||
|
32
.github/workflows/lint.yml
vendored
Normal file
32
.github/workflows/lint.yml
vendored
Normal file
@ -0,0 +1,32 @@
|
||||
name: Lint
|
||||
on: [workflow_dispatch, pull_request]
|
||||
env:
|
||||
TERM: xterm-256color
|
||||
CLANG_VERSION: 19
|
||||
|
||||
jobs:
|
||||
clang-format:
|
||||
name: Clang-Format
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Install clang-format
|
||||
run: |
|
||||
wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
|
||||
UBCN=$(lsb_release --short --codename)
|
||||
sudo add-apt-repository "deb https://apt.llvm.org/${UBCN}/ llvm-toolchain-${UBCN}-${CLANG_VERSION} main"
|
||||
sudo apt update || true
|
||||
sudo apt install -y clang-format-${CLANG_VERSION}
|
||||
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
lfs: true
|
||||
|
||||
- name: Run clang-format
|
||||
run: |
|
||||
if ! git clang-format-${CLANG_VERSION} --binary=clang-format-${CLANG_VERSION} --diff --commit=origin/dev
|
||||
then
|
||||
echo -e "\033[1;31mCode style does not match clang-format"
|
||||
exit 1
|
||||
fi
|
110
CMakeLists.txt
110
CMakeLists.txt
@ -8,7 +8,7 @@
|
||||
cmake_minimum_required(VERSION 3.13)
|
||||
|
||||
# Library version
|
||||
set(VERSION "4.0.1")
|
||||
set(VERSION "4.0.8")
|
||||
# API version
|
||||
set(SOVERSION "4")
|
||||
|
||||
@ -128,6 +128,7 @@ set(MINIZIP_HDR
|
||||
mz_zip_rw.h)
|
||||
|
||||
set(PC_PRIVATE_LIBS)
|
||||
set(PC_PRIVATE_DEPS)
|
||||
|
||||
# Check for system includes
|
||||
check_include_file(stdint.h HAVE_STDINT_H)
|
||||
@ -184,7 +185,7 @@ if(MZ_ZLIB)
|
||||
list(APPEND MINIZIP_LIB ${ZLIBNG_LIBRARIES})
|
||||
list(APPEND MINIZIP_LBD ${ZLIBNG_LIBRARY_DIRS})
|
||||
|
||||
set(PC_PRIVATE_LIBS " -lz-ng")
|
||||
set(PC_PRIVATE_DEPS "zlib-ng")
|
||||
set(ZLIB_COMPAT OFF)
|
||||
elseif(ZLIB_FOUND AND NOT MZ_FORCE_FETCH_LIBS)
|
||||
message(STATUS "Using ZLIB ${ZLIB_VERSION}")
|
||||
@ -193,10 +194,12 @@ if(MZ_ZLIB)
|
||||
list(APPEND MINIZIP_LIB ${ZLIB_LIBRARIES})
|
||||
list(APPEND MINIZIP_LBD ${ZLIB_LIBRARY_DIRS})
|
||||
|
||||
set(PC_PRIVATE_LIBS " -lz")
|
||||
set(PC_PRIVATE_DEPS "zlib")
|
||||
set(ZLIB_COMPAT ON)
|
||||
elseif(MZ_FETCH_LIBS)
|
||||
clone_repo(zlib https://github.com/madler/zlib)
|
||||
set(ZLIB_ENABLE_TESTS OFF CACHE BOOL "Build zlib-ng tests" FORCE)
|
||||
|
||||
clone_repo(zlib https://github.com/zlib-ng/zlib-ng stable)
|
||||
|
||||
# Don't automatically add all targets to the solution
|
||||
add_subdirectory(${ZLIB_SOURCE_DIR} ${ZLIB_BINARY_DIR} EXCLUDE_FROM_ALL)
|
||||
@ -205,7 +208,7 @@ 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(ZLIB_COMPAT AND (NOT DEFINED BUILD_SHARED_LIBS OR NOT BUILD_SHARED_LIBS))
|
||||
list(APPEND MINIZIP_DEP zlibstatic)
|
||||
else()
|
||||
list(APPEND MINIZIP_DEP zlib)
|
||||
@ -251,9 +254,9 @@ if(MZ_BZIP2)
|
||||
list(APPEND MINIZIP_LIB ${BZIP2_LIBRARIES})
|
||||
list(APPEND MINIZIP_LBD ${BZIP2_LIBRARY_DIRS})
|
||||
|
||||
set(PC_PRIVATE_LIBS "${PC_PRIVATE_LIBS} -lbzip2")
|
||||
set(PC_PRIVATE_LIBS "${PC_PRIVATE_LIBS} -lbz2")
|
||||
elseif(MZ_FETCH_LIBS)
|
||||
clone_repo(bzip2 https://sourceware.org/git/bzip2.git)
|
||||
clone_repo(bzip2 https://sourceware.org/git/bzip2.git master)
|
||||
|
||||
# BZip2 repository does not support cmake so we have to create
|
||||
# the bzip2 library ourselves
|
||||
@ -314,7 +317,7 @@ if(MZ_LZMA)
|
||||
elseif(MZ_FETCH_LIBS)
|
||||
set(BUILD_TESTING OFF CACHE BOOL "Build lzma tests" FORCE)
|
||||
|
||||
clone_repo(liblzma https://git.tukaani.org/xz.git)
|
||||
clone_repo(liblzma https://github.com/tukaani-project/xz master)
|
||||
|
||||
# Don't automatically add all targets to the solution
|
||||
add_subdirectory(${LIBLZMA_SOURCE_DIR} ${LIBLZMA_BINARY_DIR} EXCLUDE_FROM_ALL)
|
||||
@ -366,7 +369,7 @@ if(MZ_ZSTD)
|
||||
elseif(MZ_FETCH_LIBS)
|
||||
set(ZSTD_BUILD_PROGRAMS OFF CACHE BOOL "Build zstd programs")
|
||||
|
||||
clone_repo(zstd https://github.com/facebook/zstd)
|
||||
clone_repo(zstd https://github.com/facebook/zstd release)
|
||||
|
||||
# Don't automatically add all targets to the solution
|
||||
add_subdirectory(${ZSTD_SOURCE_DIR}/build/cmake ${ZSTD_BINARY_DIR} EXCLUDE_FROM_ALL)
|
||||
@ -460,7 +463,7 @@ if(WIN32)
|
||||
set(MZ_LIBBSD OFF)
|
||||
set(MZ_ICONV OFF)
|
||||
else()
|
||||
list(APPEND STDLIB_DEF -D_POSIX_C_SOURCE=200809L -D_BSD_SOURCE)
|
||||
list(APPEND STDLIB_DEF -D_POSIX_C_SOURCE=200809L -D_BSD_SOURCE -D_DEFAULT_SOURCE)
|
||||
list(APPEND MINIZIP_SRC mz_os_posix.c mz_strm_os_posix.c)
|
||||
|
||||
if(MZ_PKCRYPT OR MZ_WZAES)
|
||||
@ -614,19 +617,18 @@ endif()
|
||||
if(MZ_COMPAT)
|
||||
set(SOVERSION "1")
|
||||
|
||||
set(FILE_H "zip.h")
|
||||
set(MZ_COMPAT_FILE "MZ_COMPAT_ZIP")
|
||||
configure_file(mz_compat_shim.h.in zip.h)
|
||||
|
||||
set(FILE_H "unzip.h")
|
||||
set(MZ_COMPAT_FILE "MZ_COMPAT_UNZIP")
|
||||
configure_file(mz_compat_shim.h.in unzip.h)
|
||||
list(APPEND MINIZIP_SRC
|
||||
compat/ioapi.c
|
||||
compat/unzip.c
|
||||
compat/zip.c)
|
||||
list(APPEND MINIZIP_HDR
|
||||
compat/ioapi.h
|
||||
compat/unzip.h
|
||||
compat/zip.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 ${CMAKE_CURRENT_BINARY_DIR}/zip.h ${CMAKE_CURRENT_BINARY_DIR}/unzip.h)
|
||||
endif()
|
||||
|
||||
# Detect available sanitizers
|
||||
@ -686,10 +688,6 @@ set_target_properties(${MINIZIP_TARGET} PROPERTIES
|
||||
if(MINIZIP_LFG)
|
||||
set_target_properties(${MINIZIP_TARGET} PROPERTIES LINK_FLAGS ${MINIZIP_LFG})
|
||||
endif()
|
||||
if(MSVC)
|
||||
# VS debugger has problems when executable and static library are named the same
|
||||
set_target_properties(${MINIZIP_TARGET} PROPERTIES OUTPUT_NAME lib${MINIZIP_TARGET})
|
||||
endif()
|
||||
if(MZ_LZMA)
|
||||
set_target_properties(${MINIZIP_TARGET} PROPERTIES C_STANDARD 99)
|
||||
endif()
|
||||
@ -702,6 +700,7 @@ target_include_directories(${MINIZIP_TARGET} PUBLIC
|
||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
|
||||
if(MZ_COMPAT)
|
||||
target_include_directories(${MINIZIP_TARGET} PUBLIC
|
||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/compat/>
|
||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>)
|
||||
endif()
|
||||
|
||||
@ -727,8 +726,8 @@ if(NOT SKIP_INSTALL_LIBRARIES AND NOT SKIP_INSTALL_ALL)
|
||||
set(MINIZIP_CONFIG_CONTENT "@PACKAGE_INIT@\n")
|
||||
if(MINIZIP_DEP_PKG)
|
||||
string(APPEND MINIZIP_CONFIG_CONTENT "include(CMakeFindDependencyMacro)\n")
|
||||
foreach(PKG_NAME ${MINIZIP_DEP_PKG})
|
||||
string(APPEND MINIZIP_CONFIG_CONTENT "find_dependency(${PKG_NAME} REQUIRED)\n")
|
||||
foreach(pkg_name ${MINIZIP_DEP_PKG})
|
||||
string(APPEND MINIZIP_CONFIG_CONTENT "find_dependency(${pkg_name})\n")
|
||||
endforeach()
|
||||
endif()
|
||||
string(APPEND MINIZIP_CONFIG_CONTENT "include(\"\${CMAKE_CURRENT_LIST_DIR}/${MINIZIP_TARGET}.cmake\")")
|
||||
@ -754,28 +753,35 @@ endif()
|
||||
# Build test executables
|
||||
if(MZ_BUILD_TESTS)
|
||||
if(MZ_ZLIB AND NOT MZ_LIBCOMP)
|
||||
add_executable(minigzip_cmd minigzip.c)
|
||||
set_target_properties(minigzip_cmd PROPERTIES OUTPUT_NAME minigzip)
|
||||
target_compile_definitions(minigzip_cmd PRIVATE ${STDLIB_DEF} ${MINIZIP_DEF})
|
||||
target_include_directories(minigzip_cmd PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
target_link_libraries(minigzip_cmd ${MINIZIP_TARGET})
|
||||
add_executable(minigzip_cli minigzip.c)
|
||||
set_target_properties(minigzip_cli PROPERTIES OUTPUT_NAME minigzip)
|
||||
target_compile_definitions(minigzip_cli PRIVATE ${STDLIB_DEF} ${MINIZIP_DEF})
|
||||
target_include_directories(minigzip_cli PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
target_link_libraries(minigzip_cli ${MINIZIP_TARGET})
|
||||
|
||||
if(NOT SKIP_INSTALL_BINARIES AND NOT SKIP_INSTALL_ALL)
|
||||
install(TARGETS minigzip_cmd RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
|
||||
install(TARGETS minigzip_cli RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
add_executable(minizip_cmd minizip.c)
|
||||
set_target_properties(minizip_cmd PROPERTIES OUTPUT_NAME ${MINIZIP_TARGET})
|
||||
target_compile_definitions(minizip_cmd PRIVATE ${STDLIB_DEF} ${MINIZIP_DEF})
|
||||
target_include_directories(minizip_cmd PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
target_link_libraries(minizip_cmd ${MINIZIP_TARGET})
|
||||
add_executable(minizip_cli minizip.c)
|
||||
|
||||
if(MSVC AND MZ_COMPAT AND CMAKE_BUILD_TYPE MATCHES "Debug|RelWithDebInfo")
|
||||
# VS debugger has problems when executable and static library are named the same
|
||||
set_target_properties(minizip_cli PROPERTIES OUTPUT_NAME minizip_cli)
|
||||
else()
|
||||
set_target_properties(minizip_cli PROPERTIES OUTPUT_NAME minizip)
|
||||
endif()
|
||||
|
||||
target_compile_definitions(minizip_cli PRIVATE ${STDLIB_DEF} ${MINIZIP_DEF})
|
||||
target_include_directories(minizip_cli PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
target_link_libraries(minizip_cli ${MINIZIP_TARGET})
|
||||
if(WIN32)
|
||||
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT minizip_cmd)
|
||||
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT minizip_cli)
|
||||
endif()
|
||||
|
||||
if(NOT SKIP_INSTALL_BINARIES AND NOT SKIP_INSTALL_ALL)
|
||||
install(TARGETS minizip_cmd RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
|
||||
install(TARGETS minizip_cli RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
@ -839,35 +845,35 @@ if(MZ_BUILD_TESTS AND MZ_BUILD_UNIT_TESTS)
|
||||
${TEST_TEMP_DIR}/${COMPRESS_METHOD_NAME}-${EXTRA_NAME}.zip)
|
||||
|
||||
add_test(NAME ${COMPRESS_METHOD_NAME}-zip-${EXTRA_NAME}
|
||||
COMMAND minizip_cmd ${COMPRESS_METHOD_ARG} -o ${EXTRA_ARGS}
|
||||
COMMAND minizip_cli ${COMPRESS_METHOD_ARG} -o ${EXTRA_ARGS}
|
||||
${COMPRESS_METHOD_PATH}
|
||||
test.c test.h empty.txt random.bin uniform.bin fuzz
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test)
|
||||
add_test(NAME ${COMPRESS_METHOD_NAME}-list-${EXTRA_NAME}
|
||||
COMMAND minizip_cmd -l ${EXTRA_ARGS} ${COMPRESS_METHOD_PATH}
|
||||
COMMAND minizip_cli -l ${EXTRA_ARGS} ${COMPRESS_METHOD_PATH}
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test)
|
||||
if(NOT MZ_COMPRESS_ONLY)
|
||||
add_test(NAME ${COMPRESS_METHOD_NAME}-unzip-${EXTRA_NAME}
|
||||
COMMAND minizip_cmd -x -o ${EXTRA_ARGS}
|
||||
COMMAND minizip_cli -x -o ${EXTRA_ARGS}
|
||||
-d ${COMPRESS_METHOD_DEST_DIR} ${COMPRESS_METHOD_PATH}
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test)
|
||||
endif()
|
||||
add_test(NAME ${COMPRESS_METHOD_NAME}-append-${EXTRA_NAME}
|
||||
COMMAND minizip_cmd ${COMPRESS_METHOD_ARG} -a ${EXTRA_ARGS}
|
||||
COMMAND minizip_cli ${COMPRESS_METHOD_ARG} -a ${EXTRA_ARGS}
|
||||
${COMPRESS_METHOD_PATH} single.txt
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test)
|
||||
if(NOT MZ_COMPRESS_ONLY)
|
||||
add_test(NAME ${COMPRESS_METHOD_NAME}-append-unzip-${EXTRA_NAME}
|
||||
COMMAND minizip_cmd -x -o ${EXTRA_ARGS}
|
||||
COMMAND minizip_cli -x -o ${EXTRA_ARGS}
|
||||
-d ${COMPRESS_METHOD_DEST_DIR} ${COMPRESS_METHOD_PATH}
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test)
|
||||
endif()
|
||||
add_test(NAME ${COMPRESS_METHOD_NAME}-erase-${EXTRA_NAME}
|
||||
COMMAND minizip_cmd -o -e ${COMPRESS_METHOD_PATH} test.c test.h
|
||||
COMMAND minizip_cli -o -e ${COMPRESS_METHOD_PATH} test.c test.h
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test)
|
||||
if(NOT MZ_COMPRESS_ONLY)
|
||||
add_test(NAME ${COMPRESS_METHOD_NAME}-erase-unzip-${EXTRA_NAME}
|
||||
COMMAND minizip_cmd -x -o ${EXTRA_ARGS}
|
||||
COMMAND minizip_cli -x -o ${EXTRA_ARGS}
|
||||
-d ${COMPRESS_METHOD_DEST_DIR} ${COMPRESS_METHOD_PATH}
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test)
|
||||
endif()
|
||||
@ -889,35 +895,35 @@ if(MZ_BUILD_TESTS AND MZ_BUILD_UNIT_TESTS)
|
||||
if(NOT MZ_COMPRESS_ONLY)
|
||||
if(MZ_ZLIB OR MZ_LIBCOMP)
|
||||
add_test(NAME unzip-tiny
|
||||
COMMAND minizip_cmd -x -o ${EXTRA_ARGS}
|
||||
COMMAND minizip_cli -x -o ${EXTRA_ARGS}
|
||||
-d ${TEST_TEMP_DIR}/unzip-tiny
|
||||
fuzz/unzip_fuzzer_seed_corpus/tiny.zip
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test)
|
||||
endif()
|
||||
if(MZ_BZIP2)
|
||||
add_test(NAME unzip-bzip2
|
||||
COMMAND minizip_cmd -x -o ${EXTRA_ARGS}
|
||||
COMMAND minizip_cli -x -o ${EXTRA_ARGS}
|
||||
-d ${TEST_TEMP_DIR}/unzip-bzip2
|
||||
fuzz/unzip_fuzzer_seed_corpus/bzip2.zip
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test)
|
||||
endif()
|
||||
if(MZ_LZMA)
|
||||
add_test(NAME unzip-lzma
|
||||
COMMAND minizip_cmd -x -o ${EXTRA_ARGS}
|
||||
COMMAND minizip_cli -x -o ${EXTRA_ARGS}
|
||||
-d ${TEST_TEMP_DIR}/unzip-lzma
|
||||
fuzz/unzip_fuzzer_seed_corpus/lzma.zip
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test)
|
||||
endif()
|
||||
if(MZ_PKCRYPT)
|
||||
add_test(NAME unzip-pkcrypt
|
||||
COMMAND minizip_cmd -x -o ${EXTRA_ARGS}
|
||||
COMMAND minizip_cli -x -o ${EXTRA_ARGS}
|
||||
-d ${TEST_TEMP_DIR}/unzip-pkcrypt -p test123
|
||||
fuzz/unzip_fuzzer_seed_corpus/encrypted_pkcrypt.zip
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test)
|
||||
endif()
|
||||
if(MZ_WZAES)
|
||||
add_test(NAME unzip-wzaes
|
||||
COMMAND minizip_cmd -x -o ${EXTRA_ARGS}
|
||||
COMMAND minizip_cli -x -o ${EXTRA_ARGS}
|
||||
-d ${TEST_TEMP_DIR}/unzip-wzaes -p test123
|
||||
fuzz/unzip_fuzzer_seed_corpus/encrypted_wzaes.zip
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test)
|
||||
@ -927,10 +933,10 @@ if(MZ_BUILD_TESTS AND MZ_BUILD_UNIT_TESTS)
|
||||
if(MZ_ZLIB AND NOT MZ_LIBCOMP)
|
||||
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/test/random.bin DESTINATION ${TEST_TEMP_DIR})
|
||||
add_test(NAME gz
|
||||
COMMAND minigzip_cmd random.bin
|
||||
COMMAND minigzip_cli random.bin
|
||||
WORKING_DIRECTORY ${TEST_TEMP_DIR})
|
||||
add_test(NAME ungz
|
||||
COMMAND minigzip_cmd -x -d ${TEST_TEMP_DIR} random.bin.gz
|
||||
COMMAND minigzip_cli -x -d ${TEST_TEMP_DIR} random.bin.gz
|
||||
WORKING_DIRECTORY ${TEST_TEMP_DIR})
|
||||
endif()
|
||||
endif()
|
||||
|
@ -6,6 +6,7 @@ minizip-ng is a zip manipulation library written in C that is supported on Windo
|
||||
[](https://bugs.chromium.org/p/oss-fuzz/issues/list?sort=-opened&can=1&q=proj:minizip)
|
||||
[](https://github.com/zlib-ng/minizip-ng/blob/master/LICENSE)
|
||||
[](https://codecov.io/github/zlib-ng/minizip-ng/)
|
||||
[](https://repology.org/project/minizip-ng/versions)
|
||||
|
||||
Developed and maintained by Nathan Moinvaziri.
|
||||
|
||||
@ -13,8 +14,8 @@ Developed and maintained by Nathan Moinvaziri.
|
||||
|
||||
| Name | Description |
|
||||
|:--------------------------------------------------------------|:------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
||||
| [master](https://github.com/zlib-ng/minizip-ng/tree/master) | Most recent release. |
|
||||
| [develop](https://github.com/zlib-ng/minizip-ng/tree/develop) | Latest development code. |
|
||||
| [master](https://github.com/zlib-ng/minizip-ng/tree/master) | Most recent stable release. |
|
||||
| [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. |
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
# Checkout remote repository
|
||||
macro(clone_repo name url)
|
||||
macro(clone_repo name url tag)
|
||||
string(TOLOWER ${name} name_lower)
|
||||
string(TOUPPER ${name} name_upper)
|
||||
|
||||
@ -7,7 +7,7 @@ macro(clone_repo name url)
|
||||
set(${name_upper}_REPOSITORY ${url})
|
||||
endif()
|
||||
if(NOT ${name_upper}_TAG)
|
||||
set(${name_upper}_TAG master)
|
||||
set(${name_upper}_TAG ${tag})
|
||||
endif()
|
||||
|
||||
message(STATUS "Fetching ${name} ${${name_upper}_REPOSITORY} ${${name_upper}_TAG}")
|
||||
|
84
compat/crypt.h
Normal file
84
compat/crypt.h
Normal file
@ -0,0 +1,84 @@
|
||||
/* crypt.h -- base code for crypt/uncrypt ZIPfile
|
||||
Version 1.01e, February 12th, 2005
|
||||
|
||||
Copyright (C) 1998-2005 Gilles Vollant
|
||||
|
||||
This code is a modified version of crypting code in Infozip distribution
|
||||
|
||||
The encryption/decryption parts of this source code (as opposed to the
|
||||
non-echoing password parts) were originally written in Europe. The
|
||||
whole source package can be freely distributed, including from the USA.
|
||||
(Prior to January 2000, re-export from the US was a violation of US law.)
|
||||
|
||||
This encryption code is a direct transcription of the algorithm from
|
||||
Roger Schlafly, described by Phil Katz in the file appnote.txt. This
|
||||
file (appnote.txt) is distributed with the PKZIP program (even in the
|
||||
version without encryption capabilities).
|
||||
|
||||
If you don't need crypting in your application, just define symbols
|
||||
NOCRYPT and NOUNCRYPT.
|
||||
|
||||
This code support the "Traditional PKWARE Encryption".
|
||||
|
||||
The new AES encryption added on Zip format by Winzip (see the page
|
||||
http://www.winzip.com/aes_info.htm ) and PKWare PKZip 5.x Strong
|
||||
Encryption is not supported.
|
||||
*/
|
||||
|
||||
#ifndef ZLIB_VERNUM
|
||||
/* No zlib */
|
||||
typedef uint32_t z_crc_t;
|
||||
#elif (ZLIB_VERNUM & 0xf != 0xf) && (ZLIB_VERNUM < 0x1270)
|
||||
/* Define z_crc_t in zlib 1.2.6 and less */
|
||||
typedef unsigned long z_crc_t;
|
||||
#elif (ZLIB_VERNUM & 0xf == 0xf) && (ZLIB_VERNUM < 0x12df)
|
||||
/* Define z_crc_t in zlib-ng 2.0.7 and less */
|
||||
typedef unsigned int z_crc_t;
|
||||
#endif
|
||||
|
||||
#define CRC32(c, b) ((*(pcrc_32_tab + (((int)(c) ^ (b)) & 0xff))) ^ ((c) >> 8))
|
||||
|
||||
/***************************************************************************/
|
||||
/* Return the next byte in the pseudo-random sequence */
|
||||
|
||||
static int decrypt_byte(unsigned long *pkeys, const z_crc_t *pcrc_32_tab) {
|
||||
unsigned temp; /* POTENTIAL BUG: temp*(temp^1) may overflow in an
|
||||
* unpredictable manner on 16-bit systems; not a problem
|
||||
* with any known compiler so far, though */
|
||||
|
||||
(void)pcrc_32_tab;
|
||||
temp = ((unsigned)(*(pkeys + 2)) & 0xffff) | 2;
|
||||
return (int)(((temp * (temp ^ 1)) >> 8) & 0xff);
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
||||
/* Update the encryption keys with the next byte of plain text */
|
||||
|
||||
static int update_keys(unsigned long *pkeys, const z_crc_t *pcrc_32_tab, int c) {
|
||||
(*(pkeys + 0)) = CRC32((*(pkeys + 0)), c);
|
||||
(*(pkeys + 1)) += (*(pkeys + 0)) & 0xff;
|
||||
(*(pkeys + 1)) = (*(pkeys + 1)) * 134775813L + 1;
|
||||
{
|
||||
register int keyshift = (int)((*(pkeys + 1)) >> 24);
|
||||
(*(pkeys + 2)) = CRC32((*(pkeys + 2)), keyshift);
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
||||
/* Initialize the encryption keys and the random header according to the password. */
|
||||
|
||||
static void init_keys(const char *passwd, unsigned long *pkeys, const z_crc_t *pcrc_32_tab) {
|
||||
*(pkeys + 0) = 305419896L;
|
||||
*(pkeys + 1) = 591751049L;
|
||||
*(pkeys + 2) = 878082192L;
|
||||
while (*passwd != '\0') {
|
||||
update_keys(pkeys, pcrc_32_tab, (int)*passwd);
|
||||
passwd++;
|
||||
}
|
||||
}
|
||||
|
||||
#define zdecode(pkeys, pcrc_32_tab, c) (update_keys(pkeys, pcrc_32_tab, c ^= decrypt_byte(pkeys, pcrc_32_tab)))
|
||||
|
||||
#define zencode(pkeys, pcrc_32_tab, c, t) \
|
||||
(t = decrypt_byte(pkeys, pcrc_32_tab), update_keys(pkeys, pcrc_32_tab, c), (Byte)t ^ (c))
|
260
compat/ioapi.c
Normal file
260
compat/ioapi.c
Normal file
@ -0,0 +1,260 @@
|
||||
#include "mz.h"
|
||||
#include "mz_strm.h"
|
||||
#include "mz_strm_mem.h"
|
||||
|
||||
#include "ioapi.h"
|
||||
|
||||
typedef struct mz_stream_ioapi_s {
|
||||
mz_stream stream;
|
||||
void *handle;
|
||||
zlib_filefunc_def filefunc;
|
||||
zlib_filefunc64_def filefunc64;
|
||||
} mz_stream_ioapi;
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
static int32_t mz_stream_ioapi_open(void *stream, const char *path, int32_t mode);
|
||||
static int32_t mz_stream_ioapi_is_open(void *stream);
|
||||
static int32_t mz_stream_ioapi_read(void *stream, void *buf, int32_t size);
|
||||
static int32_t mz_stream_ioapi_write(void *stream, const void *buf, int32_t size);
|
||||
static int64_t mz_stream_ioapi_tell(void *stream);
|
||||
static int32_t mz_stream_ioapi_seek(void *stream, int64_t offset, int32_t origin);
|
||||
static int32_t mz_stream_ioapi_close(void *stream);
|
||||
static int32_t mz_stream_ioapi_error(void *stream);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
static mz_stream_vtbl mz_stream_ioapi_vtbl = {mz_stream_ioapi_open,
|
||||
mz_stream_ioapi_is_open,
|
||||
mz_stream_ioapi_read,
|
||||
mz_stream_ioapi_write,
|
||||
mz_stream_ioapi_tell,
|
||||
mz_stream_ioapi_seek,
|
||||
mz_stream_ioapi_close,
|
||||
mz_stream_ioapi_error,
|
||||
mz_stream_ioapi_create,
|
||||
mz_stream_ioapi_delete,
|
||||
NULL,
|
||||
NULL};
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
static int32_t mz_stream_ioapi_open(void *stream, const char *path, int32_t mode) {
|
||||
mz_stream_ioapi *ioapi = (mz_stream_ioapi *)stream;
|
||||
int32_t ioapi_mode = 0;
|
||||
|
||||
if ((mode & MZ_OPEN_MODE_READWRITE) == MZ_OPEN_MODE_READ)
|
||||
ioapi_mode = ZLIB_FILEFUNC_MODE_READ;
|
||||
else if (mode & MZ_OPEN_MODE_APPEND)
|
||||
ioapi_mode = ZLIB_FILEFUNC_MODE_EXISTING;
|
||||
else if (mode & MZ_OPEN_MODE_CREATE)
|
||||
ioapi_mode = ZLIB_FILEFUNC_MODE_CREATE;
|
||||
else
|
||||
return MZ_OPEN_ERROR;
|
||||
|
||||
if (ioapi->filefunc64.zopen64_file)
|
||||
ioapi->handle = ioapi->filefunc64.zopen64_file(ioapi->filefunc64.opaque, path, ioapi_mode);
|
||||
else if (ioapi->filefunc.zopen_file)
|
||||
ioapi->handle = ioapi->filefunc.zopen_file(ioapi->filefunc.opaque, path, ioapi_mode);
|
||||
|
||||
if (!ioapi->handle)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
static int32_t mz_stream_ioapi_is_open(void *stream) {
|
||||
mz_stream_ioapi *ioapi = (mz_stream_ioapi *)stream;
|
||||
if (!ioapi->handle)
|
||||
return MZ_OPEN_ERROR;
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
static int32_t mz_stream_ioapi_read(void *stream, void *buf, int32_t size) {
|
||||
mz_stream_ioapi *ioapi = (mz_stream_ioapi *)stream;
|
||||
read_file_func zread = NULL;
|
||||
void *opaque = NULL;
|
||||
|
||||
if (mz_stream_ioapi_is_open(stream) != MZ_OK)
|
||||
return MZ_OPEN_ERROR;
|
||||
|
||||
if (ioapi->filefunc64.zread_file) {
|
||||
zread = ioapi->filefunc64.zread_file;
|
||||
opaque = ioapi->filefunc64.opaque;
|
||||
} else if (ioapi->filefunc.zread_file) {
|
||||
zread = ioapi->filefunc.zread_file;
|
||||
opaque = ioapi->filefunc.opaque;
|
||||
} else
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
return (int32_t)zread(opaque, ioapi->handle, buf, size);
|
||||
}
|
||||
|
||||
static int32_t mz_stream_ioapi_write(void *stream, const void *buf, int32_t size) {
|
||||
mz_stream_ioapi *ioapi = (mz_stream_ioapi *)stream;
|
||||
write_file_func zwrite = NULL;
|
||||
int32_t written = 0;
|
||||
void *opaque = NULL;
|
||||
|
||||
if (mz_stream_ioapi_is_open(stream) != MZ_OK)
|
||||
return MZ_OPEN_ERROR;
|
||||
|
||||
if (ioapi->filefunc64.zwrite_file) {
|
||||
zwrite = ioapi->filefunc64.zwrite_file;
|
||||
opaque = ioapi->filefunc64.opaque;
|
||||
} else if (ioapi->filefunc.zwrite_file) {
|
||||
zwrite = ioapi->filefunc.zwrite_file;
|
||||
opaque = ioapi->filefunc.opaque;
|
||||
} else
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
written = (int32_t)zwrite(opaque, ioapi->handle, buf, size);
|
||||
return written;
|
||||
}
|
||||
|
||||
static int64_t mz_stream_ioapi_tell(void *stream) {
|
||||
mz_stream_ioapi *ioapi = (mz_stream_ioapi *)stream;
|
||||
|
||||
if (mz_stream_ioapi_is_open(stream) != MZ_OK)
|
||||
return MZ_OPEN_ERROR;
|
||||
|
||||
if (ioapi->filefunc64.ztell64_file)
|
||||
return ioapi->filefunc64.ztell64_file(ioapi->filefunc64.opaque, ioapi->handle);
|
||||
else if (ioapi->filefunc.ztell_file)
|
||||
return ioapi->filefunc.ztell_file(ioapi->filefunc.opaque, ioapi->handle);
|
||||
|
||||
return MZ_INTERNAL_ERROR;
|
||||
}
|
||||
|
||||
static int32_t mz_stream_ioapi_seek(void *stream, int64_t offset, int32_t origin) {
|
||||
mz_stream_ioapi *ioapi = (mz_stream_ioapi *)stream;
|
||||
|
||||
if (mz_stream_ioapi_is_open(stream) != MZ_OK)
|
||||
return MZ_OPEN_ERROR;
|
||||
|
||||
if (ioapi->filefunc64.zseek64_file) {
|
||||
if (ioapi->filefunc64.zseek64_file(ioapi->filefunc64.opaque, ioapi->handle, offset, origin) != 0)
|
||||
return MZ_INTERNAL_ERROR;
|
||||
} else if (ioapi->filefunc.zseek_file) {
|
||||
if (ioapi->filefunc.zseek_file(ioapi->filefunc.opaque, ioapi->handle, (int32_t)offset, origin) != 0)
|
||||
return MZ_INTERNAL_ERROR;
|
||||
} else
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
static int32_t mz_stream_ioapi_close(void *stream) {
|
||||
mz_stream_ioapi *ioapi = (mz_stream_ioapi *)stream;
|
||||
close_file_func zclose = NULL;
|
||||
void *opaque = NULL;
|
||||
|
||||
if (mz_stream_ioapi_is_open(stream) != MZ_OK)
|
||||
return MZ_OPEN_ERROR;
|
||||
|
||||
if (ioapi->filefunc.zclose_file) {
|
||||
zclose = ioapi->filefunc.zclose_file;
|
||||
opaque = ioapi->filefunc.opaque;
|
||||
} else if (ioapi->filefunc64.zclose_file) {
|
||||
zclose = ioapi->filefunc64.zclose_file;
|
||||
opaque = ioapi->filefunc64.opaque;
|
||||
} else
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
if (zclose(opaque, ioapi->handle) != 0)
|
||||
return MZ_CLOSE_ERROR;
|
||||
ioapi->handle = NULL;
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
static int32_t mz_stream_ioapi_error(void *stream) {
|
||||
mz_stream_ioapi *ioapi = (mz_stream_ioapi *)stream;
|
||||
testerror_file_func zerror = NULL;
|
||||
void *opaque = NULL;
|
||||
|
||||
if (mz_stream_ioapi_is_open(stream) != MZ_OK)
|
||||
return MZ_OPEN_ERROR;
|
||||
|
||||
if (ioapi->filefunc.zerror_file) {
|
||||
zerror = ioapi->filefunc.zerror_file;
|
||||
opaque = ioapi->filefunc.opaque;
|
||||
} else if (ioapi->filefunc64.zerror_file) {
|
||||
zerror = ioapi->filefunc64.zerror_file;
|
||||
opaque = ioapi->filefunc64.opaque;
|
||||
} else
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
return zerror(opaque, ioapi->handle);
|
||||
}
|
||||
|
||||
int32_t mz_stream_ioapi_set_filefunc(void *stream, zlib_filefunc_def *filefunc) {
|
||||
mz_stream_ioapi *ioapi = (mz_stream_ioapi *)stream;
|
||||
memcpy(&ioapi->filefunc, filefunc, sizeof(zlib_filefunc_def));
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_stream_ioapi_set_filefunc64(void *stream, zlib_filefunc64_def *filefunc) {
|
||||
mz_stream_ioapi *ioapi = (mz_stream_ioapi *)stream;
|
||||
memcpy(&ioapi->filefunc64, filefunc, sizeof(zlib_filefunc64_def));
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
void *mz_stream_ioapi_create(void) {
|
||||
mz_stream_ioapi *ioapi = (mz_stream_ioapi *)calloc(1, sizeof(mz_stream_ioapi));
|
||||
if (ioapi)
|
||||
ioapi->stream.vtbl = &mz_stream_ioapi_vtbl;
|
||||
return ioapi;
|
||||
}
|
||||
|
||||
void mz_stream_ioapi_delete(void **stream) {
|
||||
mz_stream_ioapi *ioapi = NULL;
|
||||
if (!stream)
|
||||
return;
|
||||
ioapi = (mz_stream_ioapi *)*stream;
|
||||
free(ioapi);
|
||||
*stream = NULL;
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
void fill_fopen_filefunc(zlib_filefunc_def *pzlib_filefunc_def) {
|
||||
/* For 32-bit file support only, compile with MZ_FILE32_API */
|
||||
if (pzlib_filefunc_def)
|
||||
memset(pzlib_filefunc_def, 0, sizeof(zlib_filefunc_def));
|
||||
}
|
||||
|
||||
void fill_fopen64_filefunc(zlib_filefunc64_def *pzlib_filefunc_def) {
|
||||
/* All mz_stream_os_* support large files if compilation supports it */
|
||||
if (pzlib_filefunc_def)
|
||||
memset(pzlib_filefunc_def, 0, sizeof(zlib_filefunc64_def));
|
||||
}
|
||||
|
||||
void fill_win32_filefunc(zlib_filefunc_def *pzlib_filefunc_def) {
|
||||
/* Handled by mz_stream_os_win32 */
|
||||
if (pzlib_filefunc_def)
|
||||
memset(pzlib_filefunc_def, 0, sizeof(zlib_filefunc_def));
|
||||
}
|
||||
|
||||
void fill_win32_filefunc64(zlib_filefunc64_def *pzlib_filefunc_def) {
|
||||
/* Automatically supported in mz_stream_os_win32 */
|
||||
if (pzlib_filefunc_def)
|
||||
memset(pzlib_filefunc_def, 0, sizeof(zlib_filefunc64_def));
|
||||
}
|
||||
|
||||
void fill_win32_filefunc64A(zlib_filefunc64_def *pzlib_filefunc_def) {
|
||||
/* Automatically supported in mz_stream_os_win32 */
|
||||
if (pzlib_filefunc_def)
|
||||
memset(pzlib_filefunc_def, 0, sizeof(zlib_filefunc64_def));
|
||||
}
|
||||
|
||||
/* NOTE: fill_win32_filefunc64W is no longer necessary since wide-character
|
||||
support is automatically handled by the underlying os stream. Do not
|
||||
pass wide-characters to zipOpen or unzOpen. */
|
||||
|
||||
void fill_memory_filefunc(zlib_filefunc_def *pzlib_filefunc_def) {
|
||||
/* Use opaque to indicate which stream interface to create */
|
||||
if (pzlib_filefunc_def) {
|
||||
memset(pzlib_filefunc_def, 0, sizeof(zlib_filefunc_def));
|
||||
pzlib_filefunc_def->opaque = mz_stream_mem_get_interface();
|
||||
}
|
||||
}
|
97
compat/ioapi.h
Normal file
97
compat/ioapi.h
Normal file
@ -0,0 +1,97 @@
|
||||
|
||||
#ifndef ZLIBIOAPI64_H
|
||||
#define ZLIBIOAPI64_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef uint64_t ZPOS64_T;
|
||||
|
||||
#ifndef ZEXPORT
|
||||
# define ZEXPORT
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#define ZLIB_FILEFUNC_SEEK_SET (0)
|
||||
#define ZLIB_FILEFUNC_SEEK_CUR (1)
|
||||
#define ZLIB_FILEFUNC_SEEK_END (2)
|
||||
|
||||
#define ZLIB_FILEFUNC_MODE_READ (1)
|
||||
#define ZLIB_FILEFUNC_MODE_WRITE (2)
|
||||
#define ZLIB_FILEFUNC_MODE_READWRITEFILTER (3)
|
||||
|
||||
#define ZLIB_FILEFUNC_MODE_EXISTING (4)
|
||||
#define ZLIB_FILEFUNC_MODE_CREATE (8)
|
||||
|
||||
#ifndef ZCALLBACK
|
||||
# define ZCALLBACK
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef void *(ZCALLBACK *open_file_func)(void *opaque, const char *filename, int mode);
|
||||
typedef void *(ZCALLBACK *open64_file_func)(void *opaque, const void *filename, int mode);
|
||||
typedef unsigned long(ZCALLBACK *read_file_func)(void *opaque, void *stream, void *buf, unsigned long size);
|
||||
typedef unsigned long(ZCALLBACK *write_file_func)(void *opaque, void *stream, const void *buf, unsigned long size);
|
||||
typedef int(ZCALLBACK *close_file_func)(void *opaque, void *stream);
|
||||
typedef int(ZCALLBACK *testerror_file_func)(void *opaque, void *stream);
|
||||
typedef long(ZCALLBACK *tell_file_func)(void *opaque, void *stream);
|
||||
typedef ZPOS64_T(ZCALLBACK *tell64_file_func)(void *opaque, void *stream);
|
||||
typedef long(ZCALLBACK *seek_file_func)(void *opaque, void *stream, unsigned long offset, int origin);
|
||||
typedef long(ZCALLBACK *seek64_file_func)(void *opaque, void *stream, ZPOS64_T offset, int origin);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct zlib_filefunc_def_s {
|
||||
open_file_func zopen_file;
|
||||
read_file_func zread_file;
|
||||
write_file_func zwrite_file;
|
||||
tell_file_func ztell_file;
|
||||
seek_file_func zseek_file;
|
||||
close_file_func zclose_file;
|
||||
testerror_file_func zerror_file;
|
||||
void *opaque;
|
||||
} zlib_filefunc_def;
|
||||
|
||||
typedef struct zlib_filefunc64_def_s {
|
||||
open64_file_func zopen64_file;
|
||||
read_file_func zread_file;
|
||||
write_file_func zwrite_file;
|
||||
tell64_file_func ztell64_file;
|
||||
seek64_file_func zseek64_file;
|
||||
close_file_func zclose_file;
|
||||
testerror_file_func zerror_file;
|
||||
void *opaque;
|
||||
} zlib_filefunc64_def;
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
/* Compatibility layer with the original minizip library (ioapi.h and iowin32.h). */
|
||||
ZEXPORT void fill_fopen_filefunc(zlib_filefunc_def *pzlib_filefunc_def);
|
||||
ZEXPORT void fill_fopen64_filefunc(zlib_filefunc64_def *pzlib_filefunc_def);
|
||||
ZEXPORT void fill_win32_filefunc(zlib_filefunc_def *pzlib_filefunc_def);
|
||||
ZEXPORT void fill_win32_filefunc64(zlib_filefunc64_def *pzlib_filefunc_def);
|
||||
ZEXPORT void fill_win32_filefunc64A(zlib_filefunc64_def *pzlib_filefunc_def);
|
||||
|
||||
/* Compatibility layer with older minizip-ng (ioapi_mem.h). */
|
||||
ZEXPORT void fill_memory_filefunc(zlib_filefunc_def *pzlib_filefunc_def);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t mz_stream_ioapi_set_filefunc(void *stream, zlib_filefunc_def *filefunc);
|
||||
int32_t mz_stream_ioapi_set_filefunc64(void *stream, zlib_filefunc64_def *filefunc);
|
||||
|
||||
void *mz_stream_ioapi_create(void);
|
||||
void mz_stream_ioapi_delete(void **stream);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
737
compat/unzip.c
Normal file
737
compat/unzip.c
Normal file
@ -0,0 +1,737 @@
|
||||
/* zip.c -- Backwards compatible unzip interface
|
||||
part of the minizip-ng project
|
||||
|
||||
Copyright (C) Nathan Moinvaziri
|
||||
https://github.com/zlib-ng/minizip-ng
|
||||
Copyright (C) 1998-2010 Gilles Vollant
|
||||
https://www.winimage.com/zLibDll/minizip.html
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
#include "mz.h"
|
||||
#include "mz_os.h"
|
||||
#include "mz_strm.h"
|
||||
#include "mz_strm_os.h"
|
||||
#include "mz_zip.h"
|
||||
|
||||
#include <stdio.h> /* SEEK */
|
||||
|
||||
#include "unzip.h"
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct mz_unzip_compat_s {
|
||||
void *stream;
|
||||
void *handle;
|
||||
uint64_t entry_index;
|
||||
int64_t entry_pos;
|
||||
int64_t total_out;
|
||||
} mz_unzip_compat;
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
unzFile unzOpen(const char *path) {
|
||||
return unzOpen64(path);
|
||||
}
|
||||
|
||||
unzFile unzOpen64(const void *path) {
|
||||
return unzOpen2(path, NULL);
|
||||
}
|
||||
|
||||
unzFile unzOpen2(const char *path, zlib_filefunc_def *pzlib_filefunc_def) {
|
||||
unzFile unz = NULL;
|
||||
void *stream = NULL;
|
||||
|
||||
if (pzlib_filefunc_def) {
|
||||
if (pzlib_filefunc_def->zopen_file) {
|
||||
stream = mz_stream_ioapi_create();
|
||||
if (!stream)
|
||||
return NULL;
|
||||
mz_stream_ioapi_set_filefunc(stream, pzlib_filefunc_def);
|
||||
} else if (pzlib_filefunc_def->opaque) {
|
||||
stream = mz_stream_create((mz_stream_vtbl *)pzlib_filefunc_def->opaque);
|
||||
if (!stream)
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if (!stream) {
|
||||
stream = mz_stream_os_create();
|
||||
if (!stream)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (mz_stream_open(stream, path, MZ_OPEN_MODE_READ) != MZ_OK) {
|
||||
mz_stream_delete(&stream);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
unz = unzOpen_MZ(stream);
|
||||
if (!unz) {
|
||||
mz_stream_close(stream);
|
||||
mz_stream_delete(&stream);
|
||||
return NULL;
|
||||
}
|
||||
return unz;
|
||||
}
|
||||
|
||||
unzFile unzOpen2_64(const void *path, zlib_filefunc64_def *pzlib_filefunc_def) {
|
||||
unzFile unz = NULL;
|
||||
void *stream = NULL;
|
||||
|
||||
if (pzlib_filefunc_def) {
|
||||
if (pzlib_filefunc_def->zopen64_file) {
|
||||
stream = mz_stream_ioapi_create();
|
||||
if (!stream)
|
||||
return NULL;
|
||||
mz_stream_ioapi_set_filefunc64(stream, pzlib_filefunc_def);
|
||||
} else if (pzlib_filefunc_def->opaque) {
|
||||
stream = mz_stream_create((mz_stream_vtbl *)pzlib_filefunc_def->opaque);
|
||||
if (!stream)
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if (!stream) {
|
||||
stream = mz_stream_os_create();
|
||||
if (!stream)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (mz_stream_open(stream, path, MZ_OPEN_MODE_READ) != MZ_OK) {
|
||||
mz_stream_delete(&stream);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
unz = unzOpen_MZ(stream);
|
||||
if (!unz) {
|
||||
mz_stream_close(stream);
|
||||
mz_stream_delete(&stream);
|
||||
return NULL;
|
||||
}
|
||||
return unz;
|
||||
}
|
||||
|
||||
void *unzGetHandle_MZ(unzFile file) {
|
||||
mz_unzip_compat *compat = (mz_unzip_compat *)file;
|
||||
if (!compat)
|
||||
return NULL;
|
||||
return compat->handle;
|
||||
}
|
||||
|
||||
void *unzGetStream_MZ(unzFile file) {
|
||||
mz_unzip_compat *compat = (mz_unzip_compat *)file;
|
||||
if (!compat)
|
||||
return NULL;
|
||||
return compat->stream;
|
||||
}
|
||||
|
||||
unzFile unzOpen_MZ(void *stream) {
|
||||
mz_unzip_compat *compat = NULL;
|
||||
int32_t err = MZ_OK;
|
||||
void *handle = NULL;
|
||||
|
||||
handle = mz_zip_create();
|
||||
if (!handle)
|
||||
return NULL;
|
||||
|
||||
mz_zip_set_recover(handle, 1);
|
||||
|
||||
err = mz_zip_open(handle, stream, MZ_OPEN_MODE_READ);
|
||||
if (err != MZ_OK) {
|
||||
mz_zip_delete(&handle);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
compat = (mz_unzip_compat *)calloc(1, sizeof(mz_unzip_compat));
|
||||
if (compat) {
|
||||
compat->handle = handle;
|
||||
compat->stream = stream;
|
||||
|
||||
mz_zip_goto_first_entry(compat->handle);
|
||||
} else {
|
||||
mz_zip_delete(&handle);
|
||||
}
|
||||
|
||||
return (unzFile)compat;
|
||||
}
|
||||
|
||||
int unzClose(unzFile file) {
|
||||
mz_unzip_compat *compat = (mz_unzip_compat *)file;
|
||||
int32_t err = MZ_OK;
|
||||
|
||||
if (!compat)
|
||||
return UNZ_PARAMERROR;
|
||||
|
||||
if (compat->handle)
|
||||
err = unzClose_MZ(file);
|
||||
|
||||
if (compat->stream) {
|
||||
mz_stream_close(compat->stream);
|
||||
mz_stream_delete(&compat->stream);
|
||||
}
|
||||
|
||||
free(compat);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
/* Only closes the zip handle, does not close the stream */
|
||||
int unzClose_MZ(unzFile file) {
|
||||
mz_unzip_compat *compat = (mz_unzip_compat *)file;
|
||||
int32_t err = MZ_OK;
|
||||
|
||||
if (!compat)
|
||||
return UNZ_PARAMERROR;
|
||||
|
||||
err = mz_zip_close(compat->handle);
|
||||
mz_zip_delete(&compat->handle);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
int unzGetGlobalInfo(unzFile file, unz_global_info *pglobal_info32) {
|
||||
mz_unzip_compat *compat = (mz_unzip_compat *)file;
|
||||
unz_global_info64 global_info64;
|
||||
int32_t err = MZ_OK;
|
||||
|
||||
memset(pglobal_info32, 0, sizeof(unz_global_info));
|
||||
if (!compat)
|
||||
return UNZ_PARAMERROR;
|
||||
|
||||
err = unzGetGlobalInfo64(file, &global_info64);
|
||||
if (err == MZ_OK) {
|
||||
pglobal_info32->number_entry = (uint32_t)global_info64.number_entry;
|
||||
pglobal_info32->size_comment = global_info64.size_comment;
|
||||
pglobal_info32->number_disk_with_CD = global_info64.number_disk_with_CD;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
int unzGetGlobalInfo64(unzFile file, unz_global_info64 *pglobal_info) {
|
||||
mz_unzip_compat *compat = (mz_unzip_compat *)file;
|
||||
const char *comment_ptr = NULL;
|
||||
int32_t err = MZ_OK;
|
||||
|
||||
memset(pglobal_info, 0, sizeof(unz_global_info64));
|
||||
if (!compat)
|
||||
return UNZ_PARAMERROR;
|
||||
err = mz_zip_get_comment(compat->handle, &comment_ptr);
|
||||
if (err == MZ_OK)
|
||||
pglobal_info->size_comment = (uint16_t)strlen(comment_ptr);
|
||||
if ((err == MZ_OK) || (err == MZ_EXIST_ERROR))
|
||||
err = mz_zip_get_number_entry(compat->handle, &pglobal_info->number_entry);
|
||||
if (err == MZ_OK)
|
||||
err = mz_zip_get_disk_number_with_cd(compat->handle, &pglobal_info->number_disk_with_CD);
|
||||
return err;
|
||||
}
|
||||
|
||||
int unzGetGlobalComment(unzFile file, char *comment, unsigned long comment_size) {
|
||||
mz_unzip_compat *compat = (mz_unzip_compat *)file;
|
||||
const char *comment_ptr = NULL;
|
||||
int32_t err = MZ_OK;
|
||||
|
||||
if (!comment || !comment_size)
|
||||
return UNZ_PARAMERROR;
|
||||
err = mz_zip_get_comment(compat->handle, &comment_ptr);
|
||||
if (err == MZ_OK) {
|
||||
strncpy(comment, comment_ptr, comment_size - 1);
|
||||
comment[comment_size - 1] = 0;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
int unzOpenCurrentFile3(unzFile file, int *method, int *level, int raw, const char *password) {
|
||||
mz_unzip_compat *compat = (mz_unzip_compat *)file;
|
||||
mz_zip_file *file_info = NULL;
|
||||
int32_t err = MZ_OK;
|
||||
void *stream = NULL;
|
||||
|
||||
if (!compat)
|
||||
return UNZ_PARAMERROR;
|
||||
if (method)
|
||||
*method = 0;
|
||||
if (level)
|
||||
*level = 0;
|
||||
|
||||
if (mz_zip_entry_is_open(compat->handle) == MZ_OK) {
|
||||
/* zlib minizip does not error out here if close returns errors */
|
||||
unzCloseCurrentFile(file);
|
||||
}
|
||||
|
||||
compat->total_out = 0;
|
||||
err = mz_zip_entry_read_open(compat->handle, (uint8_t)raw, password);
|
||||
if (err == MZ_OK)
|
||||
err = mz_zip_entry_get_info(compat->handle, &file_info);
|
||||
if (err == MZ_OK) {
|
||||
if (method) {
|
||||
*method = file_info->compression_method;
|
||||
}
|
||||
|
||||
if (level) {
|
||||
*level = 6;
|
||||
switch (file_info->flag & 0x06) {
|
||||
case MZ_ZIP_FLAG_DEFLATE_SUPER_FAST:
|
||||
*level = 1;
|
||||
break;
|
||||
case MZ_ZIP_FLAG_DEFLATE_FAST:
|
||||
*level = 2;
|
||||
break;
|
||||
case MZ_ZIP_FLAG_DEFLATE_MAX:
|
||||
*level = 9;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (err == MZ_OK)
|
||||
err = mz_zip_get_stream(compat->handle, &stream);
|
||||
if (err == MZ_OK)
|
||||
compat->entry_pos = mz_stream_tell(stream);
|
||||
return err;
|
||||
}
|
||||
|
||||
int unzOpenCurrentFile(unzFile file) {
|
||||
return unzOpenCurrentFile3(file, NULL, NULL, 0, NULL);
|
||||
}
|
||||
|
||||
int unzOpenCurrentFilePassword(unzFile file, const char *password) {
|
||||
return unzOpenCurrentFile3(file, NULL, NULL, 0, password);
|
||||
}
|
||||
|
||||
int unzOpenCurrentFile2(unzFile file, int *method, int *level, int raw) {
|
||||
return unzOpenCurrentFile3(file, method, level, raw, NULL);
|
||||
}
|
||||
|
||||
int unzReadCurrentFile(unzFile file, void *buf, uint32_t len) {
|
||||
mz_unzip_compat *compat = (mz_unzip_compat *)file;
|
||||
int32_t err = MZ_OK;
|
||||
if (!compat || len >= INT32_MAX)
|
||||
return UNZ_PARAMERROR;
|
||||
err = mz_zip_entry_read(compat->handle, buf, (int32_t)len);
|
||||
if (err > 0)
|
||||
compat->total_out += (uint32_t)err;
|
||||
return err;
|
||||
}
|
||||
|
||||
int unzCloseCurrentFile(unzFile file) {
|
||||
mz_unzip_compat *compat = (mz_unzip_compat *)file;
|
||||
int32_t err = MZ_OK;
|
||||
if (!compat)
|
||||
return UNZ_PARAMERROR;
|
||||
err = mz_zip_entry_close(compat->handle);
|
||||
return err;
|
||||
}
|
||||
|
||||
static void unzConvertTimeToUnzTime(time_t time, tm_unz *tmu_date) {
|
||||
struct tm tm_date;
|
||||
memset(&tm_date, 0, sizeof(struct tm));
|
||||
mz_zip_time_t_to_tm(time, &tm_date);
|
||||
memcpy(tmu_date, &tm_date, sizeof(tm_unz));
|
||||
tmu_date->tm_year += 1900;
|
||||
}
|
||||
|
||||
int unzGetCurrentFileInfo(unzFile file, unz_file_info *pfile_info, char *filename, unsigned long filename_size,
|
||||
void *extrafield, unsigned long extrafield_size, char *comment, unsigned long comment_size) {
|
||||
mz_unzip_compat *compat = (mz_unzip_compat *)file;
|
||||
mz_zip_file *file_info = NULL;
|
||||
uint16_t bytes_to_copy = 0;
|
||||
int32_t err = MZ_OK;
|
||||
|
||||
if (!compat)
|
||||
return UNZ_PARAMERROR;
|
||||
|
||||
err = mz_zip_entry_get_info(compat->handle, &file_info);
|
||||
if (err != MZ_OK)
|
||||
return err;
|
||||
|
||||
if (pfile_info) {
|
||||
pfile_info->version = file_info->version_madeby;
|
||||
pfile_info->version_needed = file_info->version_needed;
|
||||
pfile_info->flag = file_info->flag;
|
||||
pfile_info->compression_method = file_info->compression_method;
|
||||
pfile_info->mz_dos_date = mz_zip_time_t_to_dos_date(file_info->modified_date);
|
||||
unzConvertTimeToUnzTime(file_info->modified_date, &pfile_info->tmu_date);
|
||||
pfile_info->crc = file_info->crc;
|
||||
|
||||
pfile_info->size_filename = file_info->filename_size;
|
||||
pfile_info->size_file_extra = file_info->extrafield_size;
|
||||
pfile_info->size_file_comment = file_info->comment_size;
|
||||
|
||||
pfile_info->disk_num_start = (uint16_t)file_info->disk_number;
|
||||
pfile_info->internal_fa = file_info->internal_fa;
|
||||
pfile_info->external_fa = file_info->external_fa;
|
||||
|
||||
pfile_info->compressed_size = (uint32_t)file_info->compressed_size;
|
||||
pfile_info->uncompressed_size = (uint32_t)file_info->uncompressed_size;
|
||||
}
|
||||
if (filename_size > 0 && filename && file_info->filename) {
|
||||
bytes_to_copy = (uint16_t)filename_size;
|
||||
if (bytes_to_copy > file_info->filename_size)
|
||||
bytes_to_copy = file_info->filename_size;
|
||||
memcpy(filename, file_info->filename, bytes_to_copy);
|
||||
if (bytes_to_copy < filename_size)
|
||||
filename[bytes_to_copy] = 0;
|
||||
}
|
||||
if (extrafield_size > 0 && extrafield) {
|
||||
bytes_to_copy = (uint16_t)extrafield_size;
|
||||
if (bytes_to_copy > file_info->extrafield_size)
|
||||
bytes_to_copy = file_info->extrafield_size;
|
||||
memcpy(extrafield, file_info->extrafield, bytes_to_copy);
|
||||
}
|
||||
if (comment_size > 0 && comment && file_info->comment) {
|
||||
bytes_to_copy = (uint16_t)comment_size;
|
||||
if (bytes_to_copy > file_info->comment_size)
|
||||
bytes_to_copy = file_info->comment_size;
|
||||
memcpy(comment, file_info->comment, bytes_to_copy);
|
||||
if (bytes_to_copy < comment_size)
|
||||
comment[bytes_to_copy] = 0;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
int unzGetCurrentFileInfo64(unzFile file, unz_file_info64 *pfile_info, char *filename, unsigned long filename_size,
|
||||
void *extrafield, unsigned long extrafield_size, char *comment,
|
||||
unsigned long comment_size) {
|
||||
mz_unzip_compat *compat = (mz_unzip_compat *)file;
|
||||
mz_zip_file *file_info = NULL;
|
||||
uint16_t bytes_to_copy = 0;
|
||||
int32_t err = MZ_OK;
|
||||
|
||||
if (!compat)
|
||||
return UNZ_PARAMERROR;
|
||||
|
||||
err = mz_zip_entry_get_info(compat->handle, &file_info);
|
||||
if (err != MZ_OK)
|
||||
return err;
|
||||
|
||||
if (pfile_info) {
|
||||
pfile_info->version = file_info->version_madeby;
|
||||
pfile_info->version_needed = file_info->version_needed;
|
||||
pfile_info->flag = file_info->flag;
|
||||
pfile_info->compression_method = file_info->compression_method;
|
||||
pfile_info->mz_dos_date = mz_zip_time_t_to_dos_date(file_info->modified_date);
|
||||
unzConvertTimeToUnzTime(file_info->modified_date, &pfile_info->tmu_date);
|
||||
pfile_info->crc = file_info->crc;
|
||||
|
||||
pfile_info->size_filename = file_info->filename_size;
|
||||
pfile_info->size_file_extra = file_info->extrafield_size;
|
||||
pfile_info->size_file_comment = file_info->comment_size;
|
||||
|
||||
pfile_info->disk_num_start = file_info->disk_number;
|
||||
pfile_info->internal_fa = file_info->internal_fa;
|
||||
pfile_info->external_fa = file_info->external_fa;
|
||||
|
||||
pfile_info->compressed_size = (uint64_t)file_info->compressed_size;
|
||||
pfile_info->uncompressed_size = (uint64_t)file_info->uncompressed_size;
|
||||
}
|
||||
if (filename_size > 0 && filename && file_info->filename) {
|
||||
bytes_to_copy = (uint16_t)filename_size;
|
||||
if (bytes_to_copy > file_info->filename_size)
|
||||
bytes_to_copy = file_info->filename_size;
|
||||
memcpy(filename, file_info->filename, bytes_to_copy);
|
||||
if (bytes_to_copy < filename_size)
|
||||
filename[bytes_to_copy] = 0;
|
||||
}
|
||||
if (extrafield_size > 0 && extrafield) {
|
||||
bytes_to_copy = (uint16_t)extrafield_size;
|
||||
if (bytes_to_copy > file_info->extrafield_size)
|
||||
bytes_to_copy = file_info->extrafield_size;
|
||||
memcpy(extrafield, file_info->extrafield, bytes_to_copy);
|
||||
}
|
||||
if (comment_size > 0 && comment && file_info->comment) {
|
||||
bytes_to_copy = (uint16_t)comment_size;
|
||||
if (bytes_to_copy > file_info->comment_size)
|
||||
bytes_to_copy = file_info->comment_size;
|
||||
memcpy(comment, file_info->comment, bytes_to_copy);
|
||||
if (bytes_to_copy < comment_size)
|
||||
comment[bytes_to_copy] = 0;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
int unzGoToFirstFile(unzFile file) {
|
||||
mz_unzip_compat *compat = (mz_unzip_compat *)file;
|
||||
if (!compat)
|
||||
return UNZ_PARAMERROR;
|
||||
compat->entry_index = 0;
|
||||
return mz_zip_goto_first_entry(compat->handle);
|
||||
}
|
||||
|
||||
int unzGoToNextFile(unzFile file) {
|
||||
mz_unzip_compat *compat = (mz_unzip_compat *)file;
|
||||
int32_t err = MZ_OK;
|
||||
if (!compat)
|
||||
return UNZ_PARAMERROR;
|
||||
err = mz_zip_goto_next_entry(compat->handle);
|
||||
if (err != MZ_END_OF_LIST)
|
||||
compat->entry_index += 1;
|
||||
return err;
|
||||
}
|
||||
|
||||
#if !defined(MZ_COMPAT_VERSION) || MZ_COMPAT_VERSION < 110
|
||||
# ifdef WIN32
|
||||
# define UNZ_DEFAULT_IGNORE_CASE 1
|
||||
# else
|
||||
# define UNZ_DEFAULT_IGNORE_CASE 0
|
||||
# endif
|
||||
|
||||
int unzLocateFile(unzFile file, const char *filename, unzFileNameCase filename_case) {
|
||||
mz_unzip_compat *compat = (mz_unzip_compat *)file;
|
||||
mz_zip_file *file_info = NULL;
|
||||
uint64_t preserve_index = 0;
|
||||
int32_t err = MZ_OK;
|
||||
int32_t result = 0;
|
||||
uint8_t ignore_case = UNZ_DEFAULT_IGNORE_CASE;
|
||||
|
||||
if (!compat)
|
||||
return UNZ_PARAMERROR;
|
||||
|
||||
if (filename_case == 1) {
|
||||
ignore_case = 0;
|
||||
} else if (filename_case > 1) {
|
||||
ignore_case = 1;
|
||||
}
|
||||
|
||||
preserve_index = compat->entry_index;
|
||||
|
||||
err = mz_zip_goto_first_entry(compat->handle);
|
||||
while (err == MZ_OK) {
|
||||
err = mz_zip_entry_get_info(compat->handle, &file_info);
|
||||
if (err != MZ_OK)
|
||||
break;
|
||||
|
||||
result = mz_path_compare_wc(filename, file_info->filename, !ignore_case);
|
||||
|
||||
if (result == 0)
|
||||
return MZ_OK;
|
||||
|
||||
err = mz_zip_goto_next_entry(compat->handle);
|
||||
}
|
||||
|
||||
compat->entry_index = preserve_index;
|
||||
return err;
|
||||
}
|
||||
#else
|
||||
int unzLocateFile(unzFile file, const char *filename, unzFileNameComparer filename_compare_func) {
|
||||
mz_unzip_compat *compat = (mz_unzip_compat *)file;
|
||||
mz_zip_file *file_info = NULL;
|
||||
uint64_t preserve_index = 0;
|
||||
int32_t err = MZ_OK;
|
||||
int32_t result = 0;
|
||||
|
||||
if (!compat)
|
||||
return UNZ_PARAMERROR;
|
||||
|
||||
preserve_index = compat->entry_index;
|
||||
|
||||
err = mz_zip_goto_first_entry(compat->handle);
|
||||
while (err == MZ_OK) {
|
||||
err = mz_zip_entry_get_info(compat->handle, &file_info);
|
||||
if (err != MZ_OK)
|
||||
break;
|
||||
|
||||
if ((intptr_t)filename_compare_func > 2) {
|
||||
result = filename_compare_func(file, filename, file_info->filename);
|
||||
} else {
|
||||
int32_t case_sensitive = (int32_t)(intptr_t)filename_compare_func;
|
||||
result = mz_path_compare_wc(filename, file_info->filename, !case_sensitive);
|
||||
}
|
||||
|
||||
if (result == 0)
|
||||
return MZ_OK;
|
||||
|
||||
err = mz_zip_goto_next_entry(compat->handle);
|
||||
}
|
||||
|
||||
compat->entry_index = preserve_index;
|
||||
return err;
|
||||
}
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int unzGetFilePos(unzFile file, unz_file_pos *file_pos) {
|
||||
unz64_file_pos file_pos64;
|
||||
int32_t err = 0;
|
||||
|
||||
err = unzGetFilePos64(file, &file_pos64);
|
||||
if (err < 0)
|
||||
return err;
|
||||
|
||||
file_pos->pos_in_zip_directory = (uint32_t)file_pos64.pos_in_zip_directory;
|
||||
file_pos->num_of_file = (uint32_t)file_pos64.num_of_file;
|
||||
return err;
|
||||
}
|
||||
|
||||
int unzGoToFilePos(unzFile file, unz_file_pos *file_pos) {
|
||||
mz_unzip_compat *compat = (mz_unzip_compat *)file;
|
||||
unz64_file_pos file_pos64;
|
||||
|
||||
if (!compat || !file_pos)
|
||||
return UNZ_PARAMERROR;
|
||||
|
||||
file_pos64.pos_in_zip_directory = file_pos->pos_in_zip_directory;
|
||||
file_pos64.num_of_file = file_pos->num_of_file;
|
||||
|
||||
return unzGoToFilePos64(file, &file_pos64);
|
||||
}
|
||||
|
||||
int unzGetFilePos64(unzFile file, unz64_file_pos *file_pos) {
|
||||
mz_unzip_compat *compat = (mz_unzip_compat *)file;
|
||||
int64_t offset = 0;
|
||||
|
||||
if (!compat || !file_pos)
|
||||
return UNZ_PARAMERROR;
|
||||
|
||||
offset = unzGetOffset64(file);
|
||||
if (offset < 0)
|
||||
return (int)offset;
|
||||
|
||||
file_pos->pos_in_zip_directory = offset;
|
||||
file_pos->num_of_file = compat->entry_index;
|
||||
return UNZ_OK;
|
||||
}
|
||||
|
||||
int unzGoToFilePos64(unzFile file, const unz64_file_pos *file_pos) {
|
||||
mz_unzip_compat *compat = (mz_unzip_compat *)file;
|
||||
int32_t err = MZ_OK;
|
||||
|
||||
if (!compat || !file_pos)
|
||||
return UNZ_PARAMERROR;
|
||||
|
||||
err = mz_zip_goto_entry(compat->handle, file_pos->pos_in_zip_directory);
|
||||
if (err == MZ_OK)
|
||||
compat->entry_index = file_pos->num_of_file;
|
||||
return err;
|
||||
}
|
||||
|
||||
unsigned long unzGetOffset(unzFile file) {
|
||||
return (uint32_t)unzGetOffset64(file);
|
||||
}
|
||||
|
||||
int64_t unzGetOffset64(unzFile file) {
|
||||
mz_unzip_compat *compat = (mz_unzip_compat *)file;
|
||||
if (!compat)
|
||||
return UNZ_PARAMERROR;
|
||||
return mz_zip_get_entry(compat->handle);
|
||||
}
|
||||
|
||||
int unzSetOffset(unzFile file, unsigned long pos) {
|
||||
return unzSetOffset64(file, pos);
|
||||
}
|
||||
|
||||
int unzSetOffset64(unzFile file, int64_t pos) {
|
||||
mz_unzip_compat *compat = (mz_unzip_compat *)file;
|
||||
if (!compat)
|
||||
return UNZ_PARAMERROR;
|
||||
return (int)mz_zip_goto_entry(compat->handle, pos);
|
||||
}
|
||||
|
||||
int unzGetLocalExtrafield(unzFile file, void *buf, unsigned int len) {
|
||||
mz_unzip_compat *compat = (mz_unzip_compat *)file;
|
||||
mz_zip_file *file_info = NULL;
|
||||
int32_t err = MZ_OK;
|
||||
int32_t bytes_to_copy = 0;
|
||||
|
||||
if (!compat || !buf || len >= INT32_MAX)
|
||||
return UNZ_PARAMERROR;
|
||||
|
||||
err = mz_zip_entry_get_local_info(compat->handle, &file_info);
|
||||
if (err != MZ_OK)
|
||||
return err;
|
||||
|
||||
bytes_to_copy = (int32_t)len;
|
||||
if (bytes_to_copy > file_info->extrafield_size)
|
||||
bytes_to_copy = file_info->extrafield_size;
|
||||
|
||||
memcpy(buf, file_info->extrafield, bytes_to_copy);
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t unzTell(unzFile file) {
|
||||
return unztell(file);
|
||||
}
|
||||
|
||||
int32_t unztell(unzFile file) {
|
||||
return (int32_t)unztell64(file);
|
||||
}
|
||||
|
||||
uint64_t unzTell64(unzFile file) {
|
||||
return unztell64(file);
|
||||
}
|
||||
|
||||
uint64_t unztell64(unzFile file) {
|
||||
mz_unzip_compat *compat = (mz_unzip_compat *)file;
|
||||
if (!compat)
|
||||
return UNZ_PARAMERROR;
|
||||
return compat->total_out;
|
||||
}
|
||||
|
||||
int unzSeek(unzFile file, int32_t offset, int origin) {
|
||||
return unzSeek64(file, offset, origin);
|
||||
}
|
||||
|
||||
int unzSeek64(unzFile file, int64_t offset, int origin) {
|
||||
mz_unzip_compat *compat = (mz_unzip_compat *)file;
|
||||
mz_zip_file *file_info = NULL;
|
||||
int64_t position = 0;
|
||||
int32_t err = MZ_OK;
|
||||
void *stream = NULL;
|
||||
|
||||
if (!compat)
|
||||
return UNZ_PARAMERROR;
|
||||
err = mz_zip_entry_get_info(compat->handle, &file_info);
|
||||
if (err != MZ_OK)
|
||||
return err;
|
||||
if (file_info->compression_method != MZ_COMPRESS_METHOD_STORE)
|
||||
return UNZ_ERRNO;
|
||||
|
||||
if (origin == SEEK_SET)
|
||||
position = offset;
|
||||
else if (origin == SEEK_CUR)
|
||||
position = compat->total_out + offset;
|
||||
else if (origin == SEEK_END)
|
||||
position = (int64_t)file_info->compressed_size + offset;
|
||||
else
|
||||
return UNZ_PARAMERROR;
|
||||
|
||||
if (position > (int64_t)file_info->compressed_size)
|
||||
return UNZ_PARAMERROR;
|
||||
|
||||
err = mz_zip_get_stream(compat->handle, &stream);
|
||||
if (err == MZ_OK)
|
||||
err = mz_stream_seek(stream, compat->entry_pos + position, MZ_SEEK_SET);
|
||||
if (err == MZ_OK)
|
||||
compat->total_out = position;
|
||||
return err;
|
||||
}
|
||||
|
||||
int unzEndOfFile(unzFile file) {
|
||||
return unzeof(file);
|
||||
}
|
||||
|
||||
int unzeof(unzFile file) {
|
||||
mz_unzip_compat *compat = (mz_unzip_compat *)file;
|
||||
mz_zip_file *file_info = NULL;
|
||||
int32_t err = MZ_OK;
|
||||
|
||||
if (!compat)
|
||||
return UNZ_PARAMERROR;
|
||||
err = mz_zip_entry_get_info(compat->handle, &file_info);
|
||||
if (err != MZ_OK)
|
||||
return err;
|
||||
if (compat->total_out == (int64_t)file_info->uncompressed_size)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void *unzGetStream(unzFile file) {
|
||||
mz_unzip_compat *compat = (mz_unzip_compat *)file;
|
||||
if (!compat)
|
||||
return NULL;
|
||||
return (void *)compat->stream;
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
238
compat/unzip.h
Normal file
238
compat/unzip.h
Normal file
@ -0,0 +1,238 @@
|
||||
/* unzip.h -- Backwards compatible unzip interface
|
||||
part of the minizip-ng project
|
||||
|
||||
Copyright (C) Nathan Moinvaziri
|
||||
https://github.com/zlib-ng/minizip-ng
|
||||
Copyright (C) 1998-2010 Gilles Vollant
|
||||
https://www.winimage.com/zLibDll/minizip.html
|
||||
|
||||
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 _unz64_H
|
||||
#define _unz64_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#if !defined(_ZLIB_H) && !defined(ZLIB_H) && !defined(ZLIB_H_)
|
||||
# if __has_include(<zlib-ng.h>)
|
||||
# include <zlib-ng.h>
|
||||
# elif __has_include(<zlib.h>)
|
||||
# include <zlib.h>
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#ifndef _ZLIBIOAPI_H
|
||||
# include "ioapi.h"
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#if defined(STRICTUNZIP) || defined(STRICTZIPUNZIP)
|
||||
/* like the STRICT of WIN32, we define a pointer that cannot be converted
|
||||
from (void*) without cast */
|
||||
typedef struct TagunzFile__ {
|
||||
int unused;
|
||||
} unzFile__;
|
||||
typedef unzFile__ *unzFile;
|
||||
#else
|
||||
typedef void *unzFile;
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#define UNZ_OK (0)
|
||||
#define UNZ_END_OF_LIST_OF_FILE (-100)
|
||||
#define UNZ_ERRNO (Z_ERRNO)
|
||||
#define UNZ_EOF (0)
|
||||
#define UNZ_PARAMERROR (-102)
|
||||
#define UNZ_BADZIPFILE (-103)
|
||||
#define UNZ_INTERNALERROR (-104)
|
||||
#define UNZ_CRCERROR (-105)
|
||||
#define UNZ_BADPASSWORD (-106) /* minizip-ng */
|
||||
|
||||
/***************************************************************************/
|
||||
/* tm_unz contain date/time info */
|
||||
typedef struct tm_unz_s {
|
||||
int tm_sec; /* seconds after the minute - [0,59] */
|
||||
int tm_min; /* minutes after the hour - [0,59] */
|
||||
int tm_hour; /* hours since midnight - [0,23] */
|
||||
int tm_mday; /* day of the month - [1,31] */
|
||||
int tm_mon; /* months since January - [0,11] */
|
||||
int tm_year; /* years - [1980..2044] */
|
||||
} tm_unz;
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#if !defined(MZ_COMPAT_VERSION) || MZ_COMPAT_VERSION <= 110
|
||||
# define mz_dos_date dosDate
|
||||
#else
|
||||
# define mz_dos_date dos_date
|
||||
#endif
|
||||
|
||||
/* Global data about the zip from end of central dir */
|
||||
typedef struct unz_global_info64_s {
|
||||
uint64_t number_entry; /* total number of entries in the central dir on this disk */
|
||||
unsigned long size_comment; /* size of the global comment of the zipfile */
|
||||
/* minizip-ng fields */
|
||||
uint32_t number_disk_with_CD; /* number the the disk with central dir, used for spanning ZIP */
|
||||
} unz_global_info64;
|
||||
|
||||
typedef struct unz_global_info_s {
|
||||
unsigned long number_entry; /* total number of entries in the central dir on this disk */
|
||||
unsigned long size_comment; /* size of the global comment of the zipfile */
|
||||
/* minizip-ng fields */
|
||||
uint32_t number_disk_with_CD; /* number the the disk with central dir, used for spanning ZIP */
|
||||
} unz_global_info;
|
||||
|
||||
/* Information about a file in the zip */
|
||||
typedef struct unz_file_info64_s {
|
||||
unsigned long version; /* version made by 2 bytes */
|
||||
unsigned long version_needed; /* version needed to extract 2 bytes */
|
||||
unsigned long flag; /* general purpose bit flag 2 bytes */
|
||||
unsigned long compression_method; /* compression method 2 bytes */
|
||||
unsigned long mz_dos_date; /* last mod file date in Dos fmt 4 bytes */
|
||||
unsigned long crc; /* crc-32 4 bytes */
|
||||
ZPOS64_T compressed_size; /* compressed size 8 bytes */
|
||||
ZPOS64_T uncompressed_size; /* uncompressed size 8 bytes */
|
||||
unsigned long size_filename; /* filename length 2 bytes */
|
||||
unsigned long size_file_extra; /* extra field length 2 bytes */
|
||||
unsigned long size_file_comment; /* file comment length 2 bytes */
|
||||
|
||||
unsigned long disk_num_start; /* disk number start 4 bytes */
|
||||
unsigned long internal_fa; /* internal file attributes 2 bytes */
|
||||
unsigned long external_fa; /* external file attributes 4 bytes */
|
||||
|
||||
tm_unz tmu_date;
|
||||
|
||||
/* minizip-ng fields */
|
||||
ZPOS64_T disk_offset;
|
||||
uint16_t size_file_extra_internal;
|
||||
} unz_file_info64;
|
||||
|
||||
typedef struct unz_file_info_s {
|
||||
unsigned long version; /* version made by 2 bytes */
|
||||
unsigned long version_needed; /* version needed to extract 2 bytes */
|
||||
unsigned long flag; /* general purpose bit flag 2 bytes */
|
||||
unsigned long compression_method; /* compression method 2 bytes */
|
||||
unsigned long mz_dos_date; /* last mod file date in Dos fmt 4 bytes */
|
||||
unsigned long crc; /* crc-32 4 bytes */
|
||||
ZPOS64_T compressed_size; /* compressed size 4 bytes */
|
||||
ZPOS64_T uncompressed_size; /* uncompressed size 4 bytes */
|
||||
unsigned long size_filename; /* filename length 2 bytes */
|
||||
unsigned long size_file_extra; /* extra field length 2 bytes */
|
||||
unsigned long size_file_comment; /* file comment length 2 bytes */
|
||||
|
||||
unsigned long disk_num_start; /* disk number start 2 bytes */
|
||||
unsigned long internal_fa; /* internal file attributes 2 bytes */
|
||||
unsigned long external_fa; /* external file attributes 4 bytes */
|
||||
|
||||
tm_unz tmu_date;
|
||||
|
||||
/* minizip-ng fields */
|
||||
ZPOS64_T disk_offset;
|
||||
} unz_file_info;
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#if !defined(MZ_COMPAT_VERSION) || MZ_COMPAT_VERSION < 110
|
||||
/* Possible values:
|
||||
0 - Uses OS default, e.g. Windows ignores case.
|
||||
1 - Is case sensitive.
|
||||
>= 2 - Ignore case.
|
||||
*/
|
||||
typedef int unzFileNameCase;
|
||||
#else
|
||||
typedef int (*unzFileNameComparer)(unzFile file, const char *filename1, const char *filename2);
|
||||
#endif
|
||||
typedef int (*unzIteratorFunction)(unzFile file);
|
||||
typedef int (*unzIteratorFunction2)(unzFile file, unz_file_info64 *pfile_info, char *filename, uint16_t filename_size,
|
||||
void *extrafield, uint16_t extrafield_size, char *comment, uint16_t comment_size);
|
||||
|
||||
/***************************************************************************/
|
||||
/* Reading a zip file */
|
||||
|
||||
/* Compatibility layer with the original minizip library (unzip.h). */
|
||||
ZEXPORT unzFile unzOpen(const char *path);
|
||||
ZEXPORT unzFile unzOpen64(const void *path);
|
||||
ZEXPORT unzFile unzOpen2(const char *path, zlib_filefunc_def *pzlib_filefunc_def);
|
||||
ZEXPORT unzFile unzOpen2_64(const void *path, zlib_filefunc64_def *pzlib_filefunc_def);
|
||||
ZEXPORT int unzClose(unzFile file);
|
||||
|
||||
ZEXPORT int unzGetGlobalInfo(unzFile file, unz_global_info *pglobal_info32);
|
||||
ZEXPORT int unzGetGlobalInfo64(unzFile file, unz_global_info64 *pglobal_info);
|
||||
ZEXPORT int unzGetGlobalComment(unzFile file, char *comment, unsigned long comment_size);
|
||||
ZEXPORT int unzOpenCurrentFile(unzFile file);
|
||||
ZEXPORT int unzOpenCurrentFilePassword(unzFile file, const char *password);
|
||||
ZEXPORT int unzOpenCurrentFile2(unzFile file, int *method, int *level, int raw);
|
||||
ZEXPORT int unzOpenCurrentFile3(unzFile file, int *method, int *level, int raw, const char *password);
|
||||
ZEXPORT int unzReadCurrentFile(unzFile file, void *buf, uint32_t len);
|
||||
ZEXPORT int unzCloseCurrentFile(unzFile file);
|
||||
ZEXPORT int unzGetCurrentFileInfo(unzFile file, unz_file_info *pfile_info, char *filename, unsigned long filename_size,
|
||||
void *extrafield, unsigned long extrafield_size, char *comment,
|
||||
unsigned long comment_size);
|
||||
ZEXPORT int unzGetCurrentFileInfo64(unzFile file, unz_file_info64 *pfile_info, char *filename,
|
||||
unsigned long filename_size, void *extrafield, unsigned long extrafield_size,
|
||||
char *comment, unsigned long comment_size);
|
||||
ZEXPORT int unzGoToFirstFile(unzFile file);
|
||||
ZEXPORT int unzGoToNextFile(unzFile file);
|
||||
#if !defined(MZ_COMPAT_VERSION) || MZ_COMPAT_VERSION < 110
|
||||
ZEXPORT int unzLocateFile(unzFile file, const char *filename, unzFileNameCase filename_case);
|
||||
#else
|
||||
ZEXPORT int unzLocateFile(unzFile file, const char *filename, unzFileNameComparer filename_compare_func);
|
||||
#endif
|
||||
/* unzStringFileNameCompare is too new */
|
||||
ZEXPORT int unzGetLocalExtrafield(unzFile file, void *buf, unsigned int len);
|
||||
|
||||
/* Compatibility layer with older minizip-ng (mz_unzip.h). */
|
||||
unzFile unzOpen_MZ(void *stream);
|
||||
ZEXPORT int unzClose_MZ(unzFile file);
|
||||
ZEXPORT void *unzGetHandle_MZ(unzFile file);
|
||||
ZEXPORT void *unzGetStream_MZ(unzFile file);
|
||||
|
||||
/***************************************************************************/
|
||||
/* Raw access to zip file */
|
||||
|
||||
typedef struct unz_file_pos_s {
|
||||
uint32_t pos_in_zip_directory; /* offset in zip file directory */
|
||||
uint32_t num_of_file; /* # of file */
|
||||
} unz_file_pos;
|
||||
|
||||
typedef struct unz64_file_pos_s {
|
||||
int64_t pos_in_zip_directory; /* offset in zip file directory */
|
||||
uint64_t num_of_file; /* # of file */
|
||||
} unz64_file_pos;
|
||||
|
||||
/* Compatibility layer with the original minizip library (unzip.h). */
|
||||
ZEXPORT int unzGetFilePos(unzFile file, unz_file_pos *file_pos);
|
||||
ZEXPORT int unzGoToFilePos(unzFile file, unz_file_pos *file_pos);
|
||||
ZEXPORT int unzGetFilePos64(unzFile file, unz64_file_pos *file_pos);
|
||||
ZEXPORT int unzGoToFilePos64(unzFile file, const unz64_file_pos *file_pos);
|
||||
ZEXPORT int64_t unzGetOffset64(unzFile file);
|
||||
ZEXPORT unsigned long unzGetOffset(unzFile file);
|
||||
ZEXPORT int unzSetOffset64(unzFile file, int64_t pos);
|
||||
ZEXPORT int unzSetOffset(unzFile file, unsigned long pos);
|
||||
ZEXPORT int32_t unztell(unzFile file);
|
||||
ZEXPORT uint64_t unztell64(unzFile file);
|
||||
ZEXPORT int unzeof(unzFile file);
|
||||
|
||||
/* Compatibility layer with older minizip-ng (mz_unzip.h). */
|
||||
ZEXPORT int32_t unzTell(unzFile file);
|
||||
ZEXPORT uint64_t unzTell64(unzFile file);
|
||||
ZEXPORT int unzSeek(unzFile file, int32_t offset, int origin);
|
||||
ZEXPORT int unzSeek64(unzFile file, int64_t offset, int origin);
|
||||
ZEXPORT int unzEndOfFile(unzFile file);
|
||||
ZEXPORT void *unzGetStream(unzFile file);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _unz64_H */
|
409
compat/zip.c
Normal file
409
compat/zip.c
Normal file
@ -0,0 +1,409 @@
|
||||
/* zip.c -- Backwards compatible zip interface
|
||||
part of the minizip-ng project
|
||||
|
||||
Copyright (C) Nathan Moinvaziri
|
||||
https://github.com/zlib-ng/minizip-ng
|
||||
Copyright (C) 1998-2010 Gilles Vollant
|
||||
https://www.winimage.com/zLibDll/minizip.html
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
#include "mz.h"
|
||||
#include "mz_os.h"
|
||||
#include "mz_strm.h"
|
||||
#include "mz_strm_os.h"
|
||||
#include "mz_zip.h"
|
||||
|
||||
#include "zip.h"
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct mz_zip_compat_s {
|
||||
void *stream;
|
||||
void *handle;
|
||||
} mz_zip_compat;
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
static int32_t zipConvertAppendToStreamMode(int append) {
|
||||
int32_t mode = MZ_OPEN_MODE_WRITE;
|
||||
switch (append) {
|
||||
case APPEND_STATUS_CREATE:
|
||||
mode |= MZ_OPEN_MODE_CREATE;
|
||||
break;
|
||||
case APPEND_STATUS_CREATEAFTER:
|
||||
mode |= MZ_OPEN_MODE_CREATE | MZ_OPEN_MODE_APPEND;
|
||||
break;
|
||||
case APPEND_STATUS_ADDINZIP:
|
||||
mode |= MZ_OPEN_MODE_READ | MZ_OPEN_MODE_APPEND;
|
||||
break;
|
||||
}
|
||||
return mode;
|
||||
}
|
||||
|
||||
zipFile zipOpen(const char *path, int append) {
|
||||
return zipOpen2(path, append, NULL, NULL);
|
||||
}
|
||||
|
||||
zipFile zipOpen64(const void *path, int append) {
|
||||
return zipOpen2(path, append, NULL, NULL);
|
||||
}
|
||||
|
||||
zipFile zipOpen2(const char *path, int append, zipcharpc *globalcomment, zlib_filefunc_def *pzlib_filefunc_def) {
|
||||
zipFile zip = NULL;
|
||||
int32_t mode = zipConvertAppendToStreamMode(append);
|
||||
void *stream = NULL;
|
||||
|
||||
if (pzlib_filefunc_def) {
|
||||
if (pzlib_filefunc_def->zopen_file) {
|
||||
stream = mz_stream_ioapi_create();
|
||||
if (!stream)
|
||||
return NULL;
|
||||
mz_stream_ioapi_set_filefunc(stream, pzlib_filefunc_def);
|
||||
} else if (pzlib_filefunc_def->opaque) {
|
||||
stream = mz_stream_create((mz_stream_vtbl *)pzlib_filefunc_def->opaque);
|
||||
if (!stream)
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if (!stream) {
|
||||
stream = mz_stream_os_create();
|
||||
if (!stream)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (mz_stream_open(stream, path, mode) != MZ_OK) {
|
||||
mz_stream_delete(&stream);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
zip = zipOpen_MZ(stream, append, globalcomment);
|
||||
|
||||
if (!zip) {
|
||||
mz_stream_delete(&stream);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return zip;
|
||||
}
|
||||
|
||||
zipFile zipOpen2_64(const void *path, int append, zipcharpc *globalcomment, zlib_filefunc64_def *pzlib_filefunc_def) {
|
||||
zipFile zip = NULL;
|
||||
int32_t mode = zipConvertAppendToStreamMode(append);
|
||||
void *stream = NULL;
|
||||
|
||||
if (pzlib_filefunc_def) {
|
||||
if (pzlib_filefunc_def->zopen64_file) {
|
||||
stream = mz_stream_ioapi_create();
|
||||
if (!stream)
|
||||
return NULL;
|
||||
mz_stream_ioapi_set_filefunc64(stream, pzlib_filefunc_def);
|
||||
} else if (pzlib_filefunc_def->opaque) {
|
||||
stream = mz_stream_create((mz_stream_vtbl *)pzlib_filefunc_def->opaque);
|
||||
if (!stream)
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if (!stream) {
|
||||
stream = mz_stream_os_create();
|
||||
if (!stream)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (mz_stream_open(stream, path, mode) != MZ_OK) {
|
||||
mz_stream_delete(&stream);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
zip = zipOpen_MZ(stream, append, globalcomment);
|
||||
|
||||
if (!zip) {
|
||||
mz_stream_delete(&stream);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return zip;
|
||||
}
|
||||
|
||||
zipFile zipOpen_MZ(void *stream, int append, zipcharpc *globalcomment) {
|
||||
mz_zip_compat *compat = NULL;
|
||||
int32_t err = MZ_OK;
|
||||
int32_t mode = zipConvertAppendToStreamMode(append);
|
||||
void *handle = NULL;
|
||||
|
||||
handle = mz_zip_create();
|
||||
if (!handle)
|
||||
return NULL;
|
||||
|
||||
err = mz_zip_open(handle, stream, mode);
|
||||
|
||||
if (err != MZ_OK) {
|
||||
mz_zip_delete(&handle);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (globalcomment)
|
||||
mz_zip_get_comment(handle, globalcomment);
|
||||
|
||||
compat = (mz_zip_compat *)calloc(1, sizeof(mz_zip_compat));
|
||||
if (compat) {
|
||||
compat->handle = handle;
|
||||
compat->stream = stream;
|
||||
} else {
|
||||
mz_zip_delete(&handle);
|
||||
}
|
||||
|
||||
return (zipFile)compat;
|
||||
}
|
||||
|
||||
void *zipGetHandle_MZ(zipFile file) {
|
||||
mz_zip_compat *compat = (mz_zip_compat *)file;
|
||||
if (!compat)
|
||||
return NULL;
|
||||
return compat->handle;
|
||||
}
|
||||
|
||||
void *zipGetStream_MZ(zipFile file) {
|
||||
mz_zip_compat *compat = (mz_zip_compat *)file;
|
||||
if (!compat)
|
||||
return NULL;
|
||||
return (void *)compat->stream;
|
||||
}
|
||||
|
||||
static time_t zipConvertZipDateToTime(tm_zip tmz_date) {
|
||||
struct tm tm_date;
|
||||
memset(&tm_date, 0, sizeof(struct tm));
|
||||
memcpy(&tm_date, &tmz_date, sizeof(tm_zip));
|
||||
tm_date.tm_year -= 1900;
|
||||
tm_date.tm_isdst = -1;
|
||||
return mz_zip_tm_to_time_t(&tm_date);
|
||||
}
|
||||
|
||||
int zipOpenNewFileInZip5(zipFile file, const char *filename, const zip_fileinfo *zipfi, const void *extrafield_local,
|
||||
uint16_t size_extrafield_local, const void *extrafield_global, uint16_t size_extrafield_global,
|
||||
const char *comment, int compression_method, int level, int raw, int windowBits, int memLevel,
|
||||
int strategy, const char *password, unsigned long crc_for_crypting,
|
||||
unsigned long version_madeby, unsigned long flag_base, int zip64) {
|
||||
mz_zip_compat *compat = (mz_zip_compat *)file;
|
||||
mz_zip_file file_info;
|
||||
|
||||
MZ_UNUSED(strategy);
|
||||
MZ_UNUSED(memLevel);
|
||||
MZ_UNUSED(windowBits);
|
||||
MZ_UNUSED(size_extrafield_local);
|
||||
MZ_UNUSED(extrafield_local);
|
||||
MZ_UNUSED(crc_for_crypting);
|
||||
|
||||
if (!compat)
|
||||
return ZIP_PARAMERROR;
|
||||
|
||||
/* The filename and comment length must fit in 16 bits. */
|
||||
if (filename && strlen(filename) > 0xffff)
|
||||
return ZIP_PARAMERROR;
|
||||
if (comment && strlen(comment) > 0xffff)
|
||||
return ZIP_PARAMERROR;
|
||||
|
||||
memset(&file_info, 0, sizeof(file_info));
|
||||
|
||||
if (zipfi) {
|
||||
if (zipfi->mz_dos_date != 0)
|
||||
file_info.modified_date = mz_zip_dosdate_to_time_t(zipfi->mz_dos_date);
|
||||
else
|
||||
file_info.modified_date = zipConvertZipDateToTime(zipfi->tmz_date);
|
||||
|
||||
file_info.external_fa = (uint32_t)zipfi->external_fa;
|
||||
file_info.internal_fa = (uint16_t)zipfi->internal_fa;
|
||||
}
|
||||
|
||||
if (!filename)
|
||||
filename = "-";
|
||||
|
||||
file_info.compression_method = (uint16_t)compression_method;
|
||||
file_info.filename = filename;
|
||||
/* file_info.extrafield_local = extrafield_local; */
|
||||
/* file_info.extrafield_local_size = size_extrafield_local; */
|
||||
file_info.extrafield = extrafield_global;
|
||||
file_info.extrafield_size = size_extrafield_global;
|
||||
file_info.version_madeby = (uint16_t)version_madeby;
|
||||
file_info.comment = comment;
|
||||
if (file_info.comment)
|
||||
file_info.comment_size = (uint16_t)strlen(file_info.comment);
|
||||
file_info.flag = (uint16_t)flag_base;
|
||||
if (zip64)
|
||||
file_info.zip64 = MZ_ZIP64_FORCE;
|
||||
else
|
||||
file_info.zip64 = MZ_ZIP64_DISABLE;
|
||||
#ifdef HAVE_WZAES
|
||||
if (password || (raw && (file_info.flag & MZ_ZIP_FLAG_ENCRYPTED)))
|
||||
file_info.aes_version = MZ_AES_VERSION;
|
||||
#endif
|
||||
|
||||
return mz_zip_entry_write_open(compat->handle, &file_info, (int16_t)level, (uint8_t)raw, password);
|
||||
}
|
||||
|
||||
int zipOpenNewFileInZip4_64(zipFile file, const char *filename, const zip_fileinfo *zipfi, const void *extrafield_local,
|
||||
uint16_t size_extrafield_local, const void *extrafield_global,
|
||||
uint16_t size_extrafield_global, const char *comment, int compression_method, int level,
|
||||
int raw, int windowBits, int memLevel, int strategy, const char *password,
|
||||
unsigned long crc_for_crypting, unsigned long version_madeby, unsigned long flag_base,
|
||||
int zip64) {
|
||||
return zipOpenNewFileInZip5(file, filename, zipfi, extrafield_local, size_extrafield_local, extrafield_global,
|
||||
size_extrafield_global, comment, compression_method, level, raw, windowBits, memLevel,
|
||||
strategy, password, crc_for_crypting, version_madeby, flag_base, zip64);
|
||||
}
|
||||
|
||||
int zipOpenNewFileInZip4(zipFile file, const char *filename, const zip_fileinfo *zipfi, const void *extrafield_local,
|
||||
uint16_t size_extrafield_local, const void *extrafield_global, uint16_t size_extrafield_global,
|
||||
const char *comment, int compression_method, int level, int raw, int windowBits, int memLevel,
|
||||
int strategy, const char *password, unsigned long crc_for_crypting,
|
||||
unsigned long version_madeby, unsigned long flag_base) {
|
||||
return zipOpenNewFileInZip4_64(file, filename, zipfi, extrafield_local, size_extrafield_local, extrafield_global,
|
||||
size_extrafield_global, comment, compression_method, level, raw, windowBits,
|
||||
memLevel, strategy, password, crc_for_crypting, version_madeby, flag_base, 0);
|
||||
}
|
||||
|
||||
int zipOpenNewFileInZip3(zipFile file, const char *filename, const zip_fileinfo *zipfi, const void *extrafield_local,
|
||||
uint16_t size_extrafield_local, const void *extrafield_global, uint16_t size_extrafield_global,
|
||||
const char *comment, int compression_method, int level, int raw, int windowBits, int memLevel,
|
||||
int strategy, const char *password, unsigned long crc_for_crypting) {
|
||||
return zipOpenNewFileInZip3_64(file, filename, zipfi, extrafield_local, size_extrafield_local, extrafield_global,
|
||||
size_extrafield_global, comment, compression_method, level, raw, windowBits,
|
||||
memLevel, strategy, password, crc_for_crypting, 0);
|
||||
}
|
||||
|
||||
int zipOpenNewFileInZip3_64(zipFile file, const char *filename, const zip_fileinfo *zipfi, const void *extrafield_local,
|
||||
uint16_t size_extrafield_local, const void *extrafield_global,
|
||||
uint16_t size_extrafield_global, const char *comment, int compression_method, int level,
|
||||
int raw, int windowBits, int memLevel, int strategy, const char *password,
|
||||
unsigned long crc_for_crypting, int zip64) {
|
||||
return zipOpenNewFileInZip4_64(file, filename, zipfi, extrafield_local, size_extrafield_local, extrafield_global,
|
||||
size_extrafield_global, comment, compression_method, level, raw, windowBits,
|
||||
memLevel, strategy, password, crc_for_crypting, MZ_VERSION_MADEBY, 0, zip64);
|
||||
}
|
||||
|
||||
int zipOpenNewFileInZip2(zipFile file, const char *filename, const zip_fileinfo *zipfi, const void *extrafield_local,
|
||||
uint16_t size_extrafield_local, const void *extrafield_global, uint16_t size_extrafield_global,
|
||||
const char *comment, int compression_method, int level, int raw) {
|
||||
return zipOpenNewFileInZip3_64(file, filename, zipfi, extrafield_local, size_extrafield_local, extrafield_global,
|
||||
size_extrafield_global, comment, compression_method, level, raw, 0, 0, 0, NULL, 0,
|
||||
0);
|
||||
}
|
||||
|
||||
int zipOpenNewFileInZip2_64(zipFile file, const char *filename, const zip_fileinfo *zipfi, const void *extrafield_local,
|
||||
uint16_t size_extrafield_local, const void *extrafield_global,
|
||||
uint16_t size_extrafield_global, const char *comment, int compression_method, int level,
|
||||
int raw, int zip64) {
|
||||
return zipOpenNewFileInZip3_64(file, filename, zipfi, extrafield_local, size_extrafield_local, extrafield_global,
|
||||
size_extrafield_global, comment, compression_method, level, raw, 0, 0, 0, NULL, 0,
|
||||
zip64);
|
||||
}
|
||||
|
||||
int zipOpenNewFileInZip(zipFile file, const char *filename, const zip_fileinfo *zipfi, const void *extrafield_local,
|
||||
uint16_t size_extrafield_local, const void *extrafield_global, uint16_t size_extrafield_global,
|
||||
const char *comment, int compression_method, int level) {
|
||||
return zipOpenNewFileInZip_64(file, filename, zipfi, extrafield_local, size_extrafield_local, extrafield_global,
|
||||
size_extrafield_global, comment, compression_method, level, 0);
|
||||
}
|
||||
|
||||
int zipOpenNewFileInZip64(zipFile file, const char *filename, const zip_fileinfo *zipfi, const void *extrafield_local,
|
||||
uint16_t size_extrafield_local, const void *extrafield_global,
|
||||
uint16_t size_extrafield_global, const char *comment, int compression_method, int level,
|
||||
int zip64) {
|
||||
return zipOpenNewFileInZip2_64(file, filename, zipfi, extrafield_local, size_extrafield_local, extrafield_global,
|
||||
size_extrafield_global, comment, compression_method, level, 0, zip64);
|
||||
}
|
||||
|
||||
int zipOpenNewFileInZip_64(zipFile file, const char *filename, const zip_fileinfo *zipfi, const void *extrafield_local,
|
||||
uint16_t size_extrafield_local, const void *extrafield_global,
|
||||
uint16_t size_extrafield_global, const char *comment, int compression_method, int level,
|
||||
int zip64) {
|
||||
return zipOpenNewFileInZip2_64(file, filename, zipfi, extrafield_local, size_extrafield_local, extrafield_global,
|
||||
size_extrafield_global, comment, compression_method, level, 0, zip64);
|
||||
}
|
||||
|
||||
int zipWriteInFileInZip(zipFile file, const void *buf, uint32_t len) {
|
||||
mz_zip_compat *compat = (mz_zip_compat *)file;
|
||||
int32_t written = 0;
|
||||
if (!compat || len >= INT32_MAX)
|
||||
return ZIP_PARAMERROR;
|
||||
written = mz_zip_entry_write(compat->handle, buf, (int32_t)len);
|
||||
if ((written < 0) || ((uint32_t)written != len))
|
||||
return ZIP_ERRNO;
|
||||
return ZIP_OK;
|
||||
}
|
||||
|
||||
int zipCloseFileInZipRaw(zipFile file, unsigned long uncompressed_size, unsigned long crc32) {
|
||||
return zipCloseFileInZipRaw64(file, uncompressed_size, crc32);
|
||||
}
|
||||
|
||||
int zipCloseFileInZipRaw64(zipFile file, uint64_t uncompressed_size, unsigned long crc32) {
|
||||
mz_zip_compat *compat = (mz_zip_compat *)file;
|
||||
if (!compat)
|
||||
return ZIP_PARAMERROR;
|
||||
return mz_zip_entry_close_raw(compat->handle, (int64_t)uncompressed_size, (uint32_t)crc32);
|
||||
}
|
||||
|
||||
int zipCloseFileInZip(zipFile file) {
|
||||
return zipCloseFileInZip64(file);
|
||||
}
|
||||
|
||||
int zipCloseFileInZip64(zipFile file) {
|
||||
mz_zip_compat *compat = (mz_zip_compat *)file;
|
||||
if (!compat)
|
||||
return ZIP_PARAMERROR;
|
||||
return mz_zip_entry_close(compat->handle);
|
||||
}
|
||||
|
||||
int zipClose(zipFile file, const char *global_comment) {
|
||||
return zipClose_64(file, global_comment);
|
||||
}
|
||||
|
||||
int zipClose_64(zipFile file, const char *global_comment) {
|
||||
return zipClose2_64(file, global_comment, MZ_VERSION_MADEBY);
|
||||
}
|
||||
|
||||
int zipClose2_64(zipFile file, const char *global_comment, uint16_t version_madeby) {
|
||||
mz_zip_compat *compat = (mz_zip_compat *)file;
|
||||
int32_t err = MZ_OK;
|
||||
|
||||
if (compat->handle)
|
||||
err = zipClose2_MZ(file, global_comment, version_madeby);
|
||||
|
||||
if (compat->stream) {
|
||||
mz_stream_close(compat->stream);
|
||||
mz_stream_delete(&compat->stream);
|
||||
}
|
||||
|
||||
free(compat);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
/* Only closes the zip handle, does not close the stream */
|
||||
int zipClose_MZ(zipFile file, const char *global_comment) {
|
||||
return zipClose2_MZ(file, global_comment, MZ_VERSION_MADEBY);
|
||||
}
|
||||
|
||||
/* Only closes the zip handle, does not close the stream */
|
||||
int zipClose2_MZ(zipFile file, const char *global_comment, uint16_t version_madeby) {
|
||||
mz_zip_compat *compat = (mz_zip_compat *)file;
|
||||
int32_t err = MZ_OK;
|
||||
|
||||
if (!compat)
|
||||
return ZIP_PARAMERROR;
|
||||
if (!compat->handle)
|
||||
return err;
|
||||
|
||||
if (global_comment)
|
||||
mz_zip_set_comment(compat->handle, global_comment);
|
||||
|
||||
mz_zip_set_version_madeby(compat->handle, version_madeby);
|
||||
err = mz_zip_close(compat->handle);
|
||||
mz_zip_delete(&compat->handle);
|
||||
|
||||
return err;
|
||||
}
|
180
compat/zip.h
Normal file
180
compat/zip.h
Normal file
@ -0,0 +1,180 @@
|
||||
/* zip.h -- Backwards compatible zip interface
|
||||
part of the minizip-ng project
|
||||
|
||||
Copyright (C) Nathan Moinvaziri
|
||||
https://github.com/zlib-ng/minizip-ng
|
||||
Copyright (C) 1998-2010 Gilles Vollant
|
||||
https://www.winimage.com/zLibDll/minizip.html
|
||||
|
||||
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 _zip64_H
|
||||
#define _zip64_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#if !defined(_ZLIB_H) && !defined(ZLIB_H) && !defined(ZLIB_H_)
|
||||
# if __has_include(<zlib-ng.h>)
|
||||
# include <zlib-ng.h>
|
||||
# elif __has_include(<zlib.h>)
|
||||
# include <zlib.h>
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#ifndef _ZLIBIOAPI_H
|
||||
# include "ioapi.h"
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#if defined(STRICTZIP) || defined(STRICTZIPUNZIP)
|
||||
/* like the STRICT of WIN32, we define a pointer that cannot be converted
|
||||
from (void*) without cast */
|
||||
typedef struct TagzipFile__ {
|
||||
int unused;
|
||||
} zipFile__;
|
||||
typedef zipFile__ *zipFile;
|
||||
#else
|
||||
typedef void *zipFile;
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#define ZIP_OK (0)
|
||||
#define ZIP_EOF (0)
|
||||
#define ZIP_ERRNO (Z_ERRNO)
|
||||
#define ZIP_PARAMERROR (-102)
|
||||
#define ZIP_BADZIPFILE (-103)
|
||||
#define ZIP_INTERNALERROR (-104)
|
||||
|
||||
/***************************************************************************/
|
||||
/* default memLevel */
|
||||
#ifndef DEF_MEM_LEVEL
|
||||
# if MAX_MEM_LEVEL >= 8
|
||||
# define DEF_MEM_LEVEL 8
|
||||
# else
|
||||
# define DEF_MEM_LEVEL MAX_MEM_LEVEL
|
||||
# endif
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
/* tm_zip contain date/time info */
|
||||
typedef struct tm_zip_s {
|
||||
int tm_sec; /* seconds after the minute - [0,59] */
|
||||
int tm_min; /* minutes after the hour - [0,59] */
|
||||
int tm_hour; /* hours since midnight - [0,23] */
|
||||
int tm_mday; /* day of the month - [1,31] */
|
||||
int tm_mon; /* months since January - [0,11] */
|
||||
int tm_year; /* years - [1980..2044] */
|
||||
} tm_zip;
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#if !defined(MZ_COMPAT_VERSION) || MZ_COMPAT_VERSION <= 110
|
||||
# define mz_dos_date dosDate
|
||||
#else
|
||||
# define mz_dos_date dos_date
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
tm_zip tmz_date; /* date in understandable format */
|
||||
unsigned long mz_dos_date; /* if dos_date == 0, tmz_date is used */
|
||||
unsigned long internal_fa; /* internal file attributes 2 bytes */
|
||||
unsigned long external_fa; /* external file attributes 4 bytes */
|
||||
} zip_fileinfo;
|
||||
|
||||
typedef const char *zipcharpc;
|
||||
|
||||
#define APPEND_STATUS_CREATE (0)
|
||||
#define APPEND_STATUS_CREATEAFTER (1)
|
||||
#define APPEND_STATUS_ADDINZIP (2)
|
||||
|
||||
/***************************************************************************/
|
||||
/* Writing a zip file */
|
||||
|
||||
/* Compatibility layer with the original minizip library (zip.h). */
|
||||
ZEXPORT zipFile zipOpen(const char *path, int append);
|
||||
ZEXPORT zipFile zipOpen64(const void *path, int append);
|
||||
|
||||
ZEXPORT zipFile zipOpen2(const char *path, int append, zipcharpc *globalcomment, zlib_filefunc_def *pzlib_filefunc_def);
|
||||
ZEXPORT zipFile zipOpen2_64(const void *path, int append, zipcharpc *globalcomment,
|
||||
zlib_filefunc64_def *pzlib_filefunc_def);
|
||||
/* zipOpen3 is not supported */
|
||||
|
||||
ZEXPORT int zipOpenNewFileInZip(zipFile file, const char *filename, const zip_fileinfo *zipfi,
|
||||
const void *extrafield_local, uint16_t size_extrafield_local,
|
||||
const void *extrafield_global, uint16_t size_extrafield_global, const char *comment,
|
||||
int compression_method, int level);
|
||||
ZEXPORT int zipOpenNewFileInZip64(zipFile file, const char *filename, const zip_fileinfo *zipfi,
|
||||
const void *extrafield_local, uint16_t size_extrafield_local,
|
||||
const void *extrafield_global, uint16_t size_extrafield_global, const char *comment,
|
||||
int compression_method, int level, int zip64);
|
||||
ZEXPORT int zipOpenNewFileInZip2(zipFile file, const char *filename, const zip_fileinfo *zipfi,
|
||||
const void *extrafield_local, uint16_t size_extrafield_local,
|
||||
const void *extrafield_global, uint16_t size_extrafield_global, const char *comment,
|
||||
int compression_method, int level, int raw);
|
||||
ZEXPORT int zipOpenNewFileInZip2_64(zipFile file, const char *filename, const zip_fileinfo *zipfi,
|
||||
const void *extrafield_local, uint16_t size_extrafield_local,
|
||||
const void *extrafield_global, uint16_t size_extrafield_global, const char *comment,
|
||||
int compression_method, int level, int raw, int zip64);
|
||||
ZEXPORT int zipOpenNewFileInZip3(zipFile file, const char *filename, const zip_fileinfo *zipfi,
|
||||
const void *extrafield_local, uint16_t size_extrafield_local,
|
||||
const void *extrafield_global, uint16_t size_extrafield_global, const char *comment,
|
||||
int compression_method, int level, int raw, int windowBits, int memLevel, int strategy,
|
||||
const char *password, unsigned long crc_for_crypting);
|
||||
ZEXPORT int zipOpenNewFileInZip3_64(zipFile file, const char *filename, const zip_fileinfo *zipfi,
|
||||
const void *extrafield_local, uint16_t size_extrafield_local,
|
||||
const void *extrafield_global, uint16_t size_extrafield_global, const char *comment,
|
||||
int compression_method, int level, int raw, int windowBits, int memLevel,
|
||||
int strategy, const char *password, unsigned long crc_for_crypting, int zip64);
|
||||
ZEXPORT int zipOpenNewFileInZip4(zipFile file, const char *filename, const zip_fileinfo *zipfi,
|
||||
const void *extrafield_local, uint16_t size_extrafield_local,
|
||||
const void *extrafield_global, uint16_t size_extrafield_global, const char *comment,
|
||||
int compression_method, int level, int raw, int windowBits, int memLevel, int strategy,
|
||||
const char *password, unsigned long crc_for_crypting, unsigned long version_madeby,
|
||||
unsigned long flag_base);
|
||||
ZEXPORT int zipOpenNewFileInZip4_64(zipFile file, const char *filename, const zip_fileinfo *zipfi,
|
||||
const void *extrafield_local, uint16_t size_extrafield_local,
|
||||
const void *extrafield_global, uint16_t size_extrafield_global, const char *comment,
|
||||
int compression_method, int level, int raw, int windowBits, int memLevel,
|
||||
int strategy, const char *password, unsigned long crc_for_crypting,
|
||||
unsigned long version_madeby, unsigned long flag_base, int zip64);
|
||||
ZEXPORT int zipWriteInFileInZip(zipFile file, const void *buf, uint32_t len);
|
||||
ZEXPORT int zipCloseFileInZipRaw(zipFile file, unsigned long uncompressed_size, unsigned long crc32);
|
||||
ZEXPORT int zipCloseFileInZipRaw64(zipFile file, uint64_t uncompressed_size, unsigned long crc32);
|
||||
ZEXPORT int zipCloseFileInZip(zipFile file);
|
||||
/* zipAlreadyThere is too new */
|
||||
ZEXPORT int zipClose(zipFile file, const char *global_comment);
|
||||
/* zipRemoveExtraInfoBlock is not supported */
|
||||
|
||||
/* Compatibility layer with older minizip-ng (mz_zip.h). */
|
||||
ZEXPORT zipFile zipOpen_MZ(void *stream, int append, zipcharpc *globalcomment);
|
||||
ZEXPORT void *zipGetHandle_MZ(zipFile);
|
||||
ZEXPORT void *zipGetStream_MZ(zipFile file);
|
||||
ZEXPORT int zipOpenNewFileInZip_64(zipFile file, const char *filename, const zip_fileinfo *zipfi,
|
||||
const void *extrafield_local, uint16_t size_extrafield_local,
|
||||
const void *extrafield_global, uint16_t size_extrafield_global, const char *comment,
|
||||
int compression_method, int level, int zip64);
|
||||
ZEXPORT int zipOpenNewFileInZip5(zipFile file, const char *filename, const zip_fileinfo *zipfi,
|
||||
const void *extrafield_local, uint16_t size_extrafield_local,
|
||||
const void *extrafield_global, uint16_t size_extrafield_global, const char *comment,
|
||||
int compression_method, int level, int raw, int windowBits, int memLevel, int strategy,
|
||||
const char *password, unsigned long crc_for_crypting, unsigned long version_madeby,
|
||||
unsigned long flag_base, int zip64);
|
||||
ZEXPORT int zipCloseFileInZip64(zipFile file);
|
||||
ZEXPORT int zipClose_64(zipFile file, const char *global_comment);
|
||||
ZEXPORT int zipClose2_64(zipFile file, const char *global_comment, uint16_t version_madeby);
|
||||
int zipClose_MZ(zipFile file, const char *global_comment);
|
||||
int zipClose2_MZ(zipFile file, const char *global_comment, uint16_t version_madeby);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _zip64_H */
|
@ -20,7 +20,7 @@ Zip entry information structure. The _mz_zip_file_ structure is populated when r
|
||||
|uint32_t|disk_number|Starting disk number|4.4.13|
|
||||
|int64_t|disk_offset|Starting disk offset|4.4.16|
|
||||
|uint16_t|internal_fa|Internal file attributes|4.4.14|
|
||||
|uint16_t|external_fa|External file attributes|4.4.15|
|
||||
|uint32_t|external_fa|External file attributes|4.4.15|
|
||||
|const char *|filename|Filename UTF-8 null-terminated string|4.4.17|
|
||||
|const uint8_t *|extrafield|Extrafield buffer array|4.4.28|
|
||||
|const char *|comment|Comment UTF-8 null-terminated string|4.4.18|
|
||||
|
@ -82,7 +82,6 @@ The _mz_zip_reader_ and _mz_zip_writer_ objects allows you to easily extract or
|
||||
- [mz\_zip\_writer\_set\_compress\_method](#mz_zip_writer_set_compress_method)
|
||||
- [mz\_zip\_writer\_set\_compress\_level](#mz_zip_writer_set_compress_level)
|
||||
- [mz\_zip\_writer\_set\_zip\_cd](#mz_zip_writer_set_zip_cd)
|
||||
- [mz\_zip\_writer\_set\_certificate](#mz_zip_writer_set_certificate)
|
||||
- [mz\_zip\_writer\_set\_overwrite\_cb](#mz_zip_writer_set_overwrite_cb)
|
||||
- [mz\_zip\_writer\_set\_password\_cb](#mz_zip_writer_set_password_cb)
|
||||
- [mz\_zip\_writer\_set\_progress\_cb](#mz_zip_writer_set_progress_cb)
|
||||
@ -1815,27 +1814,6 @@ Sets whether or not the central directory should be zipped.
|
||||
mz_zip_writer_set_zip_cd(zip_writer, 1);
|
||||
```
|
||||
|
||||
### mz_zip_writer_set_certificate
|
||||
|
||||
Sets the certificate and timestamp url to use for signing when adding files in zip.
|
||||
|
||||
**Arguments**
|
||||
|Type|Name|Description|
|
||||
|-|-|-|
|
||||
|void *|handle|_mz_zip_writer_ instance|
|
||||
|const char *|cert_path|Path to certificate to sign entries with|
|
||||
|const char *|cert_pwd|Password for certificate to sign with|
|
||||
|
||||
**Return**
|
||||
|Type|Description|
|
||||
|-|-|
|
||||
|int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful|
|
||||
|
||||
**Example**
|
||||
```
|
||||
mz_zip_writer_set_certificate(zip_writer, "c:\\mycerts\\zip_cert.pfx", "mycertpwd");
|
||||
```
|
||||
|
||||
### mz_zip_writer_set_overwrite_cb
|
||||
|
||||
Sets the callback for what to do when a zip file is about to be overwritten.
|
||||
|
13
minigzip.c
13
minigzip.c
@ -33,12 +33,13 @@ int32_t minigzip_banner(void) {
|
||||
}
|
||||
|
||||
int32_t minigzip_help(void) {
|
||||
printf("Usage: minigzip [-x] [-d] [-0 to -9] [files]\n\n" \
|
||||
" -x Extract file\n" \
|
||||
" -d Destination directory\n" \
|
||||
" -0 Store only\n" \
|
||||
" -1 Compress faster\n" \
|
||||
" -9 Compress better\n\n");
|
||||
printf(
|
||||
"Usage: minigzip [-x] [-d] [-0 to -9] [files]\n\n"
|
||||
" -x Extract file\n"
|
||||
" -d Destination directory\n"
|
||||
" -0 Store only\n"
|
||||
" -1 Compress faster\n"
|
||||
" -9 Compress better\n\n");
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
|
195
minizip.c
195
minizip.c
@ -18,25 +18,23 @@
|
||||
#include "mz_zip.h"
|
||||
#include "mz_zip_rw.h"
|
||||
|
||||
#include <stdio.h> /* printf */
|
||||
#include <stdio.h> /* printf */
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct minizip_opt_s {
|
||||
uint8_t include_path;
|
||||
int16_t compress_level;
|
||||
uint8_t compress_method;
|
||||
uint8_t overwrite;
|
||||
uint8_t append;
|
||||
int64_t disk_size;
|
||||
uint8_t follow_links;
|
||||
uint8_t store_links;
|
||||
uint8_t zip_cd;
|
||||
int32_t encoding;
|
||||
uint8_t verbose;
|
||||
uint8_t aes;
|
||||
const char *cert_path;
|
||||
const char *cert_pwd;
|
||||
int64_t disk_size;
|
||||
int32_t encoding;
|
||||
uint8_t include_path;
|
||||
int16_t compress_level;
|
||||
uint8_t compress_method;
|
||||
uint8_t overwrite;
|
||||
uint8_t append;
|
||||
uint8_t follow_links;
|
||||
uint8_t store_links;
|
||||
uint8_t zip_cd;
|
||||
uint8_t verbose;
|
||||
uint8_t aes;
|
||||
} minizip_opt;
|
||||
|
||||
/***************************************************************************/
|
||||
@ -44,7 +42,7 @@ typedef struct minizip_opt_s {
|
||||
int32_t minizip_banner(void);
|
||||
int32_t minizip_help(void);
|
||||
|
||||
int32_t minizip_list(const char *path);
|
||||
int32_t minizip_list(const char *path, int32_t encoding);
|
||||
|
||||
int32_t minizip_add_entry_cb(void *handle, void *userdata, mz_zip_file *file_info);
|
||||
int32_t minizip_add_progress_cb(void *handle, void *userdata, mz_zip_file *file_info, int64_t position);
|
||||
@ -54,7 +52,8 @@ int32_t minizip_add(const char *path, const char *password, minizip_opt *options
|
||||
int32_t minizip_extract_entry_cb(void *handle, void *userdata, mz_zip_file *file_info, const char *path);
|
||||
int32_t minizip_extract_progress_cb(void *handle, void *userdata, mz_zip_file *file_info, int64_t position);
|
||||
int32_t minizip_extract_overwrite_cb(void *handle, void *userdata, mz_zip_file *file_info, const char *path);
|
||||
int32_t minizip_extract(const char *path, const char *pattern, const char *destination, const char *password, minizip_opt *options);
|
||||
int32_t minizip_extract(const char *path, const char *pattern, const char *destination, const char *password,
|
||||
minizip_opt *options);
|
||||
|
||||
int32_t minizip_erase(const char *src_path, const char *target_path, int32_t arg_count, const char **args);
|
||||
|
||||
@ -67,44 +66,46 @@ int32_t minizip_banner(void) {
|
||||
}
|
||||
|
||||
int32_t minizip_help(void) {
|
||||
printf("Usage: minizip [-x][-d dir|-l|-e][-o][-f][-y][-c cp][-a][-0 to -9][-b|-m|-t][-k 512][-p pwd][-s] file.zip [files]\n\n" \
|
||||
" -x Extract files\n" \
|
||||
" -l List files\n" \
|
||||
" -d Destination directory\n" \
|
||||
" -e Erase files\n" \
|
||||
" -o Overwrite existing files\n" \
|
||||
" -c File names use cp437 encoding (or specified codepage)\n" \
|
||||
" -a Append to existing zip file\n" \
|
||||
" -i Include full path of files\n" \
|
||||
" -f Follow symbolic links\n" \
|
||||
" -y Store symbolic links\n" \
|
||||
" -v Verbose info\n" \
|
||||
" -0 Store only\n" \
|
||||
" -1 Compress faster\n" \
|
||||
" -9 Compress better\n" \
|
||||
" -k Disk size in KB\n" \
|
||||
" -z Zip central directory\n" \
|
||||
" -p Encryption password\n" \
|
||||
" -s AES encryption\n" \
|
||||
" -b BZIP2 compression\n" \
|
||||
" -m LZMA compression\n" \
|
||||
" -n XZ compression\n" \
|
||||
" -t ZSTD compression\n\n");
|
||||
printf(
|
||||
"Usage: minizip [-x][-d dir|-l|-e][-o][-f][-y][-c cp][-a][-0 to -9][-b|-m|-t][-k 512][-p pwd][-s] file.zip "
|
||||
"[files]\n\n"
|
||||
" -x Extract files\n"
|
||||
" -l List files\n"
|
||||
" -d Destination directory\n"
|
||||
" -e Erase files\n"
|
||||
" -o Overwrite existing files\n"
|
||||
" -c File names use cp437 encoding (or specified codepage)\n"
|
||||
" -a Append to existing zip file\n"
|
||||
" -i Include full path of files\n"
|
||||
" -f Follow symbolic links\n"
|
||||
" -y Store symbolic links\n"
|
||||
" -v Verbose info\n"
|
||||
" -0 Store only\n"
|
||||
" -1 Compress faster\n"
|
||||
" -9 Compress better\n"
|
||||
" -k Disk size in KB\n"
|
||||
" -z Zip central directory\n"
|
||||
" -p Encryption password\n"
|
||||
" -s AES encryption\n"
|
||||
" -b BZIP2 compression\n"
|
||||
" -m LZMA compression\n"
|
||||
" -n XZ compression\n"
|
||||
" -t ZSTD compression\n\n");
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t minizip_list(const char *path) {
|
||||
int32_t minizip_list(const char *path, int32_t encoding) {
|
||||
mz_zip_file *file_info = NULL;
|
||||
uint32_t ratio = 0;
|
||||
int32_t err = MZ_OK;
|
||||
struct tm tmu_date;
|
||||
const char *method = NULL;
|
||||
char *utf8_string = NULL;
|
||||
char crypt = ' ';
|
||||
void *reader = NULL;
|
||||
|
||||
|
||||
reader = mz_zip_reader_create();
|
||||
if (!reader)
|
||||
return MZ_MEM_ERROR;
|
||||
@ -116,6 +117,8 @@ int32_t minizip_list(const char *path) {
|
||||
return err;
|
||||
}
|
||||
|
||||
mz_zip_reader_set_encoding(reader, encoding);
|
||||
|
||||
err = mz_zip_reader_goto_first_entry(reader);
|
||||
|
||||
if (err != MZ_OK && err != MZ_END_OF_LIST) {
|
||||
@ -128,7 +131,7 @@ int32_t minizip_list(const char *path) {
|
||||
printf(" ------ -------- ----- ------ ------- ---- ---- ------ ----\n");
|
||||
|
||||
/* Enumerate all entries in the archive */
|
||||
do {
|
||||
while (err == MZ_OK) {
|
||||
err = mz_zip_reader_entry_get_info(reader, &file_info);
|
||||
|
||||
if (err != MZ_OK) {
|
||||
@ -149,15 +152,25 @@ int32_t minizip_list(const char *path) {
|
||||
method = mz_zip_get_compression_method_string(file_info->compression_method);
|
||||
mz_zip_time_t_to_tm(file_info->modified_date, &tmu_date);
|
||||
|
||||
if ((encoding > 0) && (file_info->flag & MZ_ZIP_FLAG_UTF8) == 0) {
|
||||
utf8_string = mz_os_utf8_string_create(file_info->filename, encoding);
|
||||
if (!utf8_string) {
|
||||
err = MZ_MEM_ERROR;
|
||||
printf("Error %" PRId32 " creating UTF-8 string\n", err);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* Print entry information */
|
||||
printf("%12" PRId64 " %12" PRId64 " %3" PRIu32 "%% %6s%c %8" PRIx32 " %2.2" PRIu32 \
|
||||
"-%2.2" PRIu32 "-%2.2" PRIu32 " %2.2" PRIu32 ":%2.2" PRIu32 " %8.8" PRIx32 " %s\n",
|
||||
file_info->compressed_size, file_info->uncompressed_size, ratio,
|
||||
method, crypt, file_info->external_fa,
|
||||
(uint32_t)tmu_date.tm_mon + 1, (uint32_t)tmu_date.tm_mday,
|
||||
(uint32_t)tmu_date.tm_year % 100,
|
||||
(uint32_t)tmu_date.tm_hour, (uint32_t)tmu_date.tm_min,
|
||||
file_info->crc, file_info->filename);
|
||||
printf("%12" PRId64 " %12" PRId64 " %3" PRIu32 "%% %6s%c %8" PRIx32 " %2.2" PRIu32 "-%2.2" PRIu32
|
||||
"-%2.2" PRIu32 " %2.2" PRIu32 ":%2.2" PRIu32 " %8.8" PRIx32 " %s\n",
|
||||
file_info->compressed_size, file_info->uncompressed_size, ratio, method, crypt, file_info->external_fa,
|
||||
(uint32_t)tmu_date.tm_mon + 1, (uint32_t)tmu_date.tm_mday, (uint32_t)tmu_date.tm_year % 100,
|
||||
(uint32_t)tmu_date.tm_hour, (uint32_t)tmu_date.tm_min, file_info->crc,
|
||||
utf8_string ? utf8_string : file_info->filename);
|
||||
|
||||
if (utf8_string)
|
||||
mz_os_utf8_string_delete(&utf8_string);
|
||||
|
||||
err = mz_zip_reader_goto_next_entry(reader);
|
||||
|
||||
@ -165,7 +178,7 @@ int32_t minizip_list(const char *path) {
|
||||
printf("Error %" PRId32 " going to next entry in archive\n", err);
|
||||
break;
|
||||
}
|
||||
} while (err == MZ_OK);
|
||||
}
|
||||
|
||||
mz_zip_reader_delete(&reader);
|
||||
|
||||
@ -201,9 +214,10 @@ int32_t minizip_add_progress_cb(void *handle, void *userdata, mz_zip_file *file_
|
||||
progress = ((double)position / file_info->uncompressed_size) * 100;
|
||||
|
||||
/* Print the progress of the current compress operation */
|
||||
if (options->verbose)
|
||||
if (options->verbose) {
|
||||
printf("%s - %" PRId64 " / %" PRId64 " (%.02f%%)\n", file_info->filename, position,
|
||||
file_info->uncompressed_size, progress);
|
||||
file_info->uncompressed_size, progress);
|
||||
}
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
@ -236,14 +250,14 @@ int32_t minizip_add_overwrite_cb(void *handle, void *userdata, const char *path)
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t minizip_add(const char *path, const char *password, minizip_opt *options, int32_t arg_count, const char **args) {
|
||||
int32_t minizip_add(const char *path, const char *password, minizip_opt *options, int32_t arg_count,
|
||||
const char **args) {
|
||||
void *writer = NULL;
|
||||
int32_t err = MZ_OK;
|
||||
int32_t err_close = MZ_OK;
|
||||
int32_t i = 0;
|
||||
const char *filename_in_zip = NULL;
|
||||
|
||||
|
||||
printf("Archive %s\n", path);
|
||||
|
||||
/* Create zip writer */
|
||||
@ -261,8 +275,6 @@ int32_t minizip_add(const char *path, const char *password, minizip_opt *options
|
||||
mz_zip_writer_set_progress_cb(writer, options, minizip_add_progress_cb);
|
||||
mz_zip_writer_set_entry_cb(writer, options, minizip_add_entry_cb);
|
||||
mz_zip_writer_set_zip_cd(writer, options->zip_cd);
|
||||
if (options->cert_path)
|
||||
mz_zip_writer_set_certificate(writer, options->cert_path, options->cert_pwd);
|
||||
|
||||
err = mz_zip_writer_open_file(writer, path, options->disk_size, options->append);
|
||||
|
||||
@ -292,12 +304,23 @@ int32_t minizip_add(const char *path, const char *password, minizip_opt *options
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t minizip_extract_entry_cb(void *handle, void *userdata, mz_zip_file *file_info, const char *path) {
|
||||
MZ_UNUSED(handle);
|
||||
MZ_UNUSED(userdata);
|
||||
minizip_opt *options = (minizip_opt *)userdata;
|
||||
char *utf8_string = NULL;
|
||||
|
||||
MZ_UNUSED(path);
|
||||
|
||||
if ((options->encoding > 0) && (file_info->flag & MZ_ZIP_FLAG_UTF8) == 0) {
|
||||
utf8_string = mz_os_utf8_string_create(file_info->filename, options->encoding);
|
||||
if (!utf8_string)
|
||||
return MZ_MEM_ERROR;
|
||||
}
|
||||
|
||||
/* Print the current entry extracting */
|
||||
printf("Extracting %s\n", file_info->filename);
|
||||
printf("Extracting %s\n", utf8_string ? utf8_string : file_info->filename);
|
||||
|
||||
if (utf8_string)
|
||||
mz_os_utf8_string_delete(&utf8_string);
|
||||
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
@ -306,8 +329,6 @@ int32_t minizip_extract_progress_cb(void *handle, void *userdata, mz_zip_file *f
|
||||
double progress = 0;
|
||||
uint8_t raw = 0;
|
||||
|
||||
MZ_UNUSED(userdata);
|
||||
|
||||
mz_zip_reader_get_raw(handle, &raw);
|
||||
|
||||
if (raw && file_info->compressed_size > 0)
|
||||
@ -316,9 +337,10 @@ int32_t minizip_extract_progress_cb(void *handle, void *userdata, mz_zip_file *f
|
||||
progress = ((double)position / file_info->uncompressed_size) * 100;
|
||||
|
||||
/* Print the progress of the current extraction */
|
||||
if (options->verbose)
|
||||
if (options->verbose) {
|
||||
printf("%s - %" PRId64 " / %" PRId64 " (%.02f%%)\n", file_info->filename, position,
|
||||
file_info->uncompressed_size, progress);
|
||||
file_info->uncompressed_size, progress);
|
||||
}
|
||||
|
||||
return MZ_OK;
|
||||
}
|
||||
@ -351,7 +373,8 @@ int32_t minizip_extract_overwrite_cb(void *handle, void *userdata, mz_zip_file *
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t minizip_extract(const char *path, const char *pattern, const char *destination, const char *password, minizip_opt *options) {
|
||||
int32_t minizip_extract(const char *path, const char *pattern, const char *destination, const char *password,
|
||||
minizip_opt *options) {
|
||||
void *reader = NULL;
|
||||
int32_t err = MZ_OK;
|
||||
int32_t err_close = MZ_OK;
|
||||
@ -573,39 +596,39 @@ int main(int argc, const char *argv[]) {
|
||||
else if ((c == 'v') || (c == 'V'))
|
||||
options.verbose = 1;
|
||||
else if ((c >= '0') && (c <= '9')) {
|
||||
options.compress_level = (c - '0');
|
||||
options.compress_level = (int16_t)atoi(&argv[i][1]);
|
||||
if (options.compress_level == 0)
|
||||
options.compress_method = MZ_COMPRESS_METHOD_STORE;
|
||||
} else if ((c == 'b') || (c == 'B'))
|
||||
#ifdef HAVE_BZIP2
|
||||
# ifdef HAVE_BZIP2
|
||||
options.compress_method = MZ_COMPRESS_METHOD_BZIP2;
|
||||
#else
|
||||
# else
|
||||
err = MZ_SUPPORT_ERROR;
|
||||
#endif
|
||||
# endif
|
||||
else if ((c == 'm') || (c == 'M'))
|
||||
#ifdef HAVE_LZMA
|
||||
# ifdef HAVE_LZMA
|
||||
options.compress_method = MZ_COMPRESS_METHOD_LZMA;
|
||||
#else
|
||||
# else
|
||||
err = MZ_SUPPORT_ERROR;
|
||||
#endif
|
||||
# endif
|
||||
else if ((c == 'n') || (c == 'N'))
|
||||
#if defined(HAVE_LZMA) || defined(HAVE_LIBCOMP)
|
||||
# if defined(HAVE_LZMA) || defined(HAVE_LIBCOMP)
|
||||
options.compress_method = MZ_COMPRESS_METHOD_XZ;
|
||||
#else
|
||||
# else
|
||||
err = MZ_SUPPORT_ERROR;
|
||||
#endif
|
||||
# endif
|
||||
else if ((c == 't') || (c == 'T'))
|
||||
#ifdef HAVE_ZSTD
|
||||
# ifdef HAVE_ZSTD
|
||||
options.compress_method = MZ_COMPRESS_METHOD_ZSTD;
|
||||
#else
|
||||
# else
|
||||
err = MZ_SUPPORT_ERROR;
|
||||
#endif
|
||||
# endif
|
||||
else if ((c == 's') || (c == 'S'))
|
||||
#ifdef HAVE_WZAES
|
||||
# ifdef HAVE_WZAES
|
||||
options.aes = 1;
|
||||
#else
|
||||
# else
|
||||
err = MZ_SUPPORT_ERROR;
|
||||
#endif
|
||||
# endif
|
||||
else if (((c == 'c') || (c == 'C')) && (i + 1 < argc)) {
|
||||
options.encoding = (int32_t)atoi(argv[i + 1]);
|
||||
i += 1;
|
||||
@ -618,12 +641,12 @@ int main(int argc, const char *argv[]) {
|
||||
printf("%s ", argv[i + 1]);
|
||||
i += 1;
|
||||
} else if (((c == 'p') || (c == 'P')) && (i + 1 < argc)) {
|
||||
#ifndef MZ_ZIP_NO_ENCRYPTION
|
||||
# ifndef MZ_ZIP_NO_ENCRYPTION
|
||||
password = argv[i + 1];
|
||||
printf("*** ");
|
||||
#else
|
||||
# else
|
||||
err = MZ_SUPPORT_ERROR;
|
||||
#endif
|
||||
# endif
|
||||
i += 1;
|
||||
}
|
||||
} else if (path_arg == 0)
|
||||
@ -645,7 +668,7 @@ int main(int argc, const char *argv[]) {
|
||||
|
||||
if (do_list) {
|
||||
/* List archive contents */
|
||||
err = minizip_list(path);
|
||||
err = minizip_list(path, options.encoding);
|
||||
} else if (do_extract) {
|
||||
if (argc > path_arg + 1)
|
||||
filename_to_extract = argv[path_arg + 1];
|
||||
|
@ -8,7 +8,7 @@ Name: @MINIZIP_TARGET@
|
||||
Description: Zip manipulation library
|
||||
Version: @VERSION@
|
||||
|
||||
Requires: zlib
|
||||
Requires.private: @PC_PRIVATE_DEPS@
|
||||
Libs: -L${libdir} -L${sharedlibdir} -l@MINIZIP_TARGET@
|
||||
Libs.private:@PC_PRIVATE_LIBS@
|
||||
Cflags: -I${includedir}
|
||||
|
209
mz.h
209
mz.h
@ -14,143 +14,142 @@
|
||||
/***************************************************************************/
|
||||
|
||||
/* MZ_VERSION */
|
||||
#define MZ_VERSION ("4.0.1")
|
||||
#define MZ_VERSION_BUILD (040001)
|
||||
#define MZ_VERSION ("4.0.8")
|
||||
#define MZ_VERSION_BUILD (0x040008)
|
||||
|
||||
/* MZ_ERROR */
|
||||
#define MZ_OK (0) /* zlib */
|
||||
#define MZ_STREAM_ERROR (-1) /* zlib */
|
||||
#define MZ_DATA_ERROR (-3) /* zlib */
|
||||
#define MZ_MEM_ERROR (-4) /* zlib */
|
||||
#define MZ_BUF_ERROR (-5) /* zlib */
|
||||
#define MZ_VERSION_ERROR (-6) /* zlib */
|
||||
#define MZ_OK (0) /* zlib */
|
||||
#define MZ_STREAM_ERROR (-1) /* zlib */
|
||||
#define MZ_DATA_ERROR (-3) /* zlib */
|
||||
#define MZ_MEM_ERROR (-4) /* zlib */
|
||||
#define MZ_BUF_ERROR (-5) /* zlib */
|
||||
#define MZ_VERSION_ERROR (-6) /* zlib */
|
||||
|
||||
#define MZ_END_OF_LIST (-100)
|
||||
#define MZ_END_OF_STREAM (-101)
|
||||
#define MZ_END_OF_LIST (-100)
|
||||
#define MZ_END_OF_STREAM (-101)
|
||||
|
||||
#define MZ_PARAM_ERROR (-102)
|
||||
#define MZ_FORMAT_ERROR (-103)
|
||||
#define MZ_INTERNAL_ERROR (-104)
|
||||
#define MZ_CRC_ERROR (-105)
|
||||
#define MZ_CRYPT_ERROR (-106)
|
||||
#define MZ_EXIST_ERROR (-107)
|
||||
#define MZ_PASSWORD_ERROR (-108)
|
||||
#define MZ_SUPPORT_ERROR (-109)
|
||||
#define MZ_HASH_ERROR (-110)
|
||||
#define MZ_OPEN_ERROR (-111)
|
||||
#define MZ_CLOSE_ERROR (-112)
|
||||
#define MZ_SEEK_ERROR (-113)
|
||||
#define MZ_TELL_ERROR (-114)
|
||||
#define MZ_READ_ERROR (-115)
|
||||
#define MZ_WRITE_ERROR (-116)
|
||||
#define MZ_SIGN_ERROR (-117)
|
||||
#define MZ_SYMLINK_ERROR (-118)
|
||||
#define MZ_PARAM_ERROR (-102)
|
||||
#define MZ_FORMAT_ERROR (-103)
|
||||
#define MZ_INTERNAL_ERROR (-104)
|
||||
#define MZ_CRC_ERROR (-105)
|
||||
#define MZ_CRYPT_ERROR (-106)
|
||||
#define MZ_EXIST_ERROR (-107)
|
||||
#define MZ_PASSWORD_ERROR (-108)
|
||||
#define MZ_SUPPORT_ERROR (-109)
|
||||
#define MZ_HASH_ERROR (-110)
|
||||
#define MZ_OPEN_ERROR (-111)
|
||||
#define MZ_CLOSE_ERROR (-112)
|
||||
#define MZ_SEEK_ERROR (-113)
|
||||
#define MZ_TELL_ERROR (-114)
|
||||
#define MZ_READ_ERROR (-115)
|
||||
#define MZ_WRITE_ERROR (-116)
|
||||
#define MZ_SIGN_ERROR (-117)
|
||||
#define MZ_SYMLINK_ERROR (-118)
|
||||
|
||||
/* MZ_OPEN */
|
||||
#define MZ_OPEN_MODE_READ (0x01)
|
||||
#define MZ_OPEN_MODE_WRITE (0x02)
|
||||
#define MZ_OPEN_MODE_READWRITE (MZ_OPEN_MODE_READ | MZ_OPEN_MODE_WRITE)
|
||||
#define MZ_OPEN_MODE_APPEND (0x04)
|
||||
#define MZ_OPEN_MODE_CREATE (0x08)
|
||||
#define MZ_OPEN_MODE_EXISTING (0x10)
|
||||
#define MZ_OPEN_MODE_READ (0x01)
|
||||
#define MZ_OPEN_MODE_WRITE (0x02)
|
||||
#define MZ_OPEN_MODE_READWRITE (MZ_OPEN_MODE_READ | MZ_OPEN_MODE_WRITE)
|
||||
#define MZ_OPEN_MODE_APPEND (0x04)
|
||||
#define MZ_OPEN_MODE_CREATE (0x08)
|
||||
#define MZ_OPEN_MODE_EXISTING (0x10)
|
||||
|
||||
/* MZ_SEEK */
|
||||
#define MZ_SEEK_SET (0)
|
||||
#define MZ_SEEK_CUR (1)
|
||||
#define MZ_SEEK_END (2)
|
||||
#define MZ_SEEK_SET (0)
|
||||
#define MZ_SEEK_CUR (1)
|
||||
#define MZ_SEEK_END (2)
|
||||
|
||||
/* MZ_COMPRESS */
|
||||
#define MZ_COMPRESS_METHOD_STORE (0)
|
||||
#define MZ_COMPRESS_METHOD_DEFLATE (8)
|
||||
#define MZ_COMPRESS_METHOD_BZIP2 (12)
|
||||
#define MZ_COMPRESS_METHOD_LZMA (14)
|
||||
#define MZ_COMPRESS_METHOD_ZSTD (93)
|
||||
#define MZ_COMPRESS_METHOD_XZ (95)
|
||||
#define MZ_COMPRESS_METHOD_AES (99)
|
||||
#define MZ_COMPRESS_METHOD_STORE (0)
|
||||
#define MZ_COMPRESS_METHOD_DEFLATE (8)
|
||||
#define MZ_COMPRESS_METHOD_BZIP2 (12)
|
||||
#define MZ_COMPRESS_METHOD_LZMA (14)
|
||||
#define MZ_COMPRESS_METHOD_ZSTD (93)
|
||||
#define MZ_COMPRESS_METHOD_XZ (95)
|
||||
#define MZ_COMPRESS_METHOD_AES (99)
|
||||
|
||||
#define MZ_COMPRESS_LEVEL_DEFAULT (-1)
|
||||
#define MZ_COMPRESS_LEVEL_FAST (2)
|
||||
#define MZ_COMPRESS_LEVEL_NORMAL (6)
|
||||
#define MZ_COMPRESS_LEVEL_BEST (9)
|
||||
#define MZ_COMPRESS_LEVEL_DEFAULT (-1)
|
||||
#define MZ_COMPRESS_LEVEL_FAST (2)
|
||||
#define MZ_COMPRESS_LEVEL_NORMAL (6)
|
||||
#define MZ_COMPRESS_LEVEL_BEST (9)
|
||||
|
||||
/* MZ_ZIP_FLAG */
|
||||
#define MZ_ZIP_FLAG_ENCRYPTED (1 << 0)
|
||||
#define MZ_ZIP_FLAG_LZMA_EOS_MARKER (1 << 1)
|
||||
#define MZ_ZIP_FLAG_DEFLATE_MAX (1 << 1)
|
||||
#define MZ_ZIP_FLAG_DEFLATE_NORMAL (0)
|
||||
#define MZ_ZIP_FLAG_DEFLATE_FAST (1 << 2)
|
||||
#define MZ_ZIP_FLAG_DEFLATE_SUPER_FAST (MZ_ZIP_FLAG_DEFLATE_FAST | \
|
||||
MZ_ZIP_FLAG_DEFLATE_MAX)
|
||||
#define MZ_ZIP_FLAG_DATA_DESCRIPTOR (1 << 3)
|
||||
#define MZ_ZIP_FLAG_UTF8 (1 << 11)
|
||||
#define MZ_ZIP_FLAG_MASK_LOCAL_INFO (1 << 13)
|
||||
#define MZ_ZIP_FLAG_ENCRYPTED (1 << 0)
|
||||
#define MZ_ZIP_FLAG_LZMA_EOS_MARKER (1 << 1)
|
||||
#define MZ_ZIP_FLAG_DEFLATE_MAX (1 << 1)
|
||||
#define MZ_ZIP_FLAG_DEFLATE_NORMAL (0)
|
||||
#define MZ_ZIP_FLAG_DEFLATE_FAST (1 << 2)
|
||||
#define MZ_ZIP_FLAG_DEFLATE_SUPER_FAST (MZ_ZIP_FLAG_DEFLATE_FAST | MZ_ZIP_FLAG_DEFLATE_MAX)
|
||||
#define MZ_ZIP_FLAG_DATA_DESCRIPTOR (1 << 3)
|
||||
#define MZ_ZIP_FLAG_UTF8 (1 << 11)
|
||||
#define MZ_ZIP_FLAG_MASK_LOCAL_INFO (1 << 13)
|
||||
|
||||
/* MZ_ZIP_EXTENSION */
|
||||
#define MZ_ZIP_EXTENSION_ZIP64 (0x0001)
|
||||
#define MZ_ZIP_EXTENSION_NTFS (0x000a)
|
||||
#define MZ_ZIP_EXTENSION_AES (0x9901)
|
||||
#define MZ_ZIP_EXTENSION_UNIX1 (0x000d)
|
||||
#define MZ_ZIP_EXTENSION_SIGN (0x10c5)
|
||||
#define MZ_ZIP_EXTENSION_HASH (0x1a51)
|
||||
#define MZ_ZIP_EXTENSION_CDCD (0xcdcd)
|
||||
#define MZ_ZIP_EXTENSION_ZIP64 (0x0001)
|
||||
#define MZ_ZIP_EXTENSION_NTFS (0x000a)
|
||||
#define MZ_ZIP_EXTENSION_AES (0x9901)
|
||||
#define MZ_ZIP_EXTENSION_UNIX1 (0x000d)
|
||||
#define MZ_ZIP_EXTENSION_SIGN (0x10c5)
|
||||
#define MZ_ZIP_EXTENSION_HASH (0x1a51)
|
||||
#define MZ_ZIP_EXTENSION_CDCD (0xcdcd)
|
||||
|
||||
/* MZ_ZIP64 */
|
||||
#define MZ_ZIP64_AUTO (0)
|
||||
#define MZ_ZIP64_FORCE (1)
|
||||
#define MZ_ZIP64_DISABLE (2)
|
||||
#define MZ_ZIP64_AUTO (0)
|
||||
#define MZ_ZIP64_FORCE (1)
|
||||
#define MZ_ZIP64_DISABLE (2)
|
||||
|
||||
/* MZ_HOST_SYSTEM */
|
||||
#define MZ_HOST_SYSTEM(VERSION_MADEBY) ((uint8_t)(VERSION_MADEBY >> 8))
|
||||
#define MZ_HOST_SYSTEM_MSDOS (0)
|
||||
#define MZ_HOST_SYSTEM_UNIX (3)
|
||||
#define MZ_HOST_SYSTEM_WINDOWS_NTFS (10)
|
||||
#define MZ_HOST_SYSTEM_RISCOS (13)
|
||||
#define MZ_HOST_SYSTEM_OSX_DARWIN (19)
|
||||
#define MZ_HOST_SYSTEM(VERSION_MADEBY) ((uint8_t)(VERSION_MADEBY >> 8))
|
||||
#define MZ_HOST_SYSTEM_MSDOS (0)
|
||||
#define MZ_HOST_SYSTEM_UNIX (3)
|
||||
#define MZ_HOST_SYSTEM_WINDOWS_NTFS (10)
|
||||
#define MZ_HOST_SYSTEM_RISCOS (13)
|
||||
#define MZ_HOST_SYSTEM_OSX_DARWIN (19)
|
||||
|
||||
/* MZ_PKCRYPT */
|
||||
#define MZ_PKCRYPT_HEADER_SIZE (12)
|
||||
#define MZ_PKCRYPT_HEADER_SIZE (12)
|
||||
|
||||
/* MZ_AES */
|
||||
#define MZ_AES_VERSION (1)
|
||||
#define MZ_AES_MODE_ECB (0)
|
||||
#define MZ_AES_MODE_CBC (1)
|
||||
#define MZ_AES_MODE_GCM (2)
|
||||
#define MZ_AES_STRENGTH_128 (1)
|
||||
#define MZ_AES_STRENGTH_192 (2)
|
||||
#define MZ_AES_STRENGTH_256 (3)
|
||||
#define MZ_AES_KEY_LENGTH_MAX (32)
|
||||
#define MZ_AES_BLOCK_SIZE (16)
|
||||
#define MZ_AES_FOOTER_SIZE (10)
|
||||
#define MZ_AES_VERSION (1)
|
||||
#define MZ_AES_MODE_ECB (0)
|
||||
#define MZ_AES_MODE_CBC (1)
|
||||
#define MZ_AES_MODE_GCM (2)
|
||||
#define MZ_AES_STRENGTH_128 (1)
|
||||
#define MZ_AES_STRENGTH_192 (2)
|
||||
#define MZ_AES_STRENGTH_256 (3)
|
||||
#define MZ_AES_KEY_LENGTH_MAX (32)
|
||||
#define MZ_AES_BLOCK_SIZE (16)
|
||||
#define MZ_AES_FOOTER_SIZE (10)
|
||||
|
||||
/* MZ_HASH */
|
||||
#define MZ_HASH_MD5 (10)
|
||||
#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)
|
||||
#define MZ_HASH_MD5 (10)
|
||||
#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 */
|
||||
#define MZ_ENCODING_CODEPAGE_437 (437)
|
||||
#define MZ_ENCODING_CODEPAGE_932 (932)
|
||||
#define MZ_ENCODING_CODEPAGE_936 (936)
|
||||
#define MZ_ENCODING_CODEPAGE_950 (950)
|
||||
#define MZ_ENCODING_UTF8 (65001)
|
||||
#define MZ_ENCODING_CODEPAGE_437 (437)
|
||||
#define MZ_ENCODING_CODEPAGE_932 (932)
|
||||
#define MZ_ENCODING_CODEPAGE_936 (936)
|
||||
#define MZ_ENCODING_CODEPAGE_950 (950)
|
||||
#define MZ_ENCODING_UTF8 (65001)
|
||||
|
||||
/* MZ_UTILITY */
|
||||
#define MZ_UNUSED(SYMBOL) ((void)SYMBOL)
|
||||
#define MZ_UNUSED(SYMBOL) ((void)SYMBOL)
|
||||
|
||||
#if defined(_WIN32) && defined(MZ_EXPORTS)
|
||||
#define MZ_EXPORT __declspec(dllexport)
|
||||
# define MZ_EXPORT __declspec(dllexport)
|
||||
#else
|
||||
#define MZ_EXPORT
|
||||
# define MZ_EXPORT
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
|
1315
mz_compat.c
1315
mz_compat.c
File diff suppressed because it is too large
Load Diff
396
mz_compat.h
396
mz_compat.h
@ -1,396 +0,0 @@
|
||||
/* mz_compat.h -- Backwards compatible interface for older versions
|
||||
part of the minizip-ng project
|
||||
|
||||
Copyright (C) Nathan Moinvaziri
|
||||
https://github.com/zlib-ng/minizip-ng
|
||||
Copyright (C) 1998-2010 Gilles Vollant
|
||||
https://www.winimage.com/zLibDll/minizip.html
|
||||
|
||||
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_H
|
||||
#define MZ_COMPAT_H
|
||||
|
||||
#include "mz.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#if defined(HAVE_ZLIB) && defined(MAX_MEM_LEVEL)
|
||||
#ifndef DEF_MEM_LEVEL
|
||||
# if MAX_MEM_LEVEL >= 8
|
||||
# define DEF_MEM_LEVEL 8
|
||||
# else
|
||||
# define DEF_MEM_LEVEL MAX_MEM_LEVEL
|
||||
# endif
|
||||
#endif
|
||||
#endif
|
||||
#ifndef MAX_WBITS
|
||||
#define MAX_WBITS (15)
|
||||
#endif
|
||||
#ifndef DEF_MEM_LEVEL
|
||||
#define DEF_MEM_LEVEL (8)
|
||||
#endif
|
||||
|
||||
#ifndef ZEXPORT
|
||||
# define ZEXPORT MZ_EXPORT
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#if defined(STRICTZIP) || defined(STRICTZIPUNZIP)
|
||||
/* like the STRICT of WIN32, we define a pointer that cannot be converted
|
||||
from (void*) without cast */
|
||||
typedef struct TagzipFile__ { int unused; } zip_file__;
|
||||
typedef zip_file__ *zipFile;
|
||||
#else
|
||||
typedef void *zipFile;
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef uint64_t ZPOS64_T;
|
||||
|
||||
#ifndef ZCALLBACK
|
||||
#define ZCALLBACK
|
||||
#endif
|
||||
|
||||
typedef void* (ZCALLBACK *open_file_func) (void *opaque, const char *filename, int mode);
|
||||
typedef void* (ZCALLBACK *open64_file_func) (void *opaque, const void *filename, int mode);
|
||||
typedef unsigned long (ZCALLBACK *read_file_func) (void *opaque, void *stream, void* buf, unsigned long size);
|
||||
typedef unsigned long (ZCALLBACK *write_file_func) (void *opaque, void *stream, const void* buf,
|
||||
unsigned long size);
|
||||
typedef int (ZCALLBACK *close_file_func) (void *opaque, void *stream);
|
||||
typedef int (ZCALLBACK *testerror_file_func)(void *opaque, void *stream);
|
||||
typedef long (ZCALLBACK *tell_file_func) (void *opaque, void *stream);
|
||||
typedef ZPOS64_T (ZCALLBACK *tell64_file_func) (void *opaque, void *stream);
|
||||
typedef long (ZCALLBACK *seek_file_func) (void *opaque, void *stream, unsigned long offset, int origin);
|
||||
typedef long (ZCALLBACK *seek64_file_func) (void *opaque, void *stream, ZPOS64_T offset, int origin);
|
||||
|
||||
typedef struct zlib_filefunc_def_s
|
||||
{
|
||||
open_file_func zopen_file;
|
||||
read_file_func zread_file;
|
||||
write_file_func zwrite_file;
|
||||
tell_file_func ztell_file;
|
||||
seek_file_func zseek_file;
|
||||
close_file_func zclose_file;
|
||||
testerror_file_func zerror_file;
|
||||
void* opaque;
|
||||
} zlib_filefunc_def;
|
||||
|
||||
typedef struct zlib_filefunc64_def_s
|
||||
{
|
||||
open64_file_func zopen64_file;
|
||||
read_file_func zread_file;
|
||||
write_file_func zwrite_file;
|
||||
tell64_file_func ztell64_file;
|
||||
seek64_file_func zseek64_file;
|
||||
close_file_func zclose_file;
|
||||
testerror_file_func zerror_file;
|
||||
void* opaque;
|
||||
} zlib_filefunc64_def;
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#define ZLIB_FILEFUNC_SEEK_SET (0)
|
||||
#define ZLIB_FILEFUNC_SEEK_CUR (1)
|
||||
#define ZLIB_FILEFUNC_SEEK_END (2)
|
||||
|
||||
#define ZLIB_FILEFUNC_MODE_READ (1)
|
||||
#define ZLIB_FILEFUNC_MODE_WRITE (2)
|
||||
#define ZLIB_FILEFUNC_MODE_READWRITEFILTER (3)
|
||||
|
||||
#define ZLIB_FILEFUNC_MODE_EXISTING (4)
|
||||
#define ZLIB_FILEFUNC_MODE_CREATE (8)
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
ZEXPORT void fill_fopen_filefunc(zlib_filefunc_def *pzlib_filefunc_def);
|
||||
ZEXPORT void fill_fopen64_filefunc(zlib_filefunc64_def *pzlib_filefunc_def);
|
||||
ZEXPORT void fill_win32_filefunc(zlib_filefunc_def *pzlib_filefunc_def);
|
||||
ZEXPORT void fill_win32_filefunc64(zlib_filefunc64_def *pzlib_filefunc_def);
|
||||
ZEXPORT void fill_win32_filefunc64A(zlib_filefunc64_def *pzlib_filefunc_def);
|
||||
ZEXPORT void fill_memory_filefunc(zlib_filefunc_def *pzlib_filefunc_def);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#if MZ_COMPAT_VERSION <= 110
|
||||
#define mz_dos_date dosDate
|
||||
#else
|
||||
#define mz_dos_date dos_date
|
||||
#endif
|
||||
|
||||
typedef struct tm tm_unz;
|
||||
typedef struct tm tm_zip;
|
||||
|
||||
typedef struct {
|
||||
uint32_t mz_dos_date;
|
||||
struct tm tmz_date;
|
||||
uint16_t internal_fa; /* internal file attributes 2 bytes */
|
||||
uint32_t external_fa; /* external file attributes 4 bytes */
|
||||
} zip_fileinfo;
|
||||
|
||||
typedef const char *zipcharpc;
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#define ZIP_OK (0)
|
||||
#define ZIP_EOF (0)
|
||||
#define ZIP_ERRNO (-1)
|
||||
#define ZIP_PARAMERROR (-102)
|
||||
#define ZIP_BADZIPFILE (-103)
|
||||
#define ZIP_INTERNALERROR (-104)
|
||||
|
||||
#ifndef Z_DEFLATED
|
||||
#define Z_DEFLATED (8)
|
||||
#endif
|
||||
#define Z_BZIP2ED (12)
|
||||
|
||||
#define APPEND_STATUS_CREATE (0)
|
||||
#define APPEND_STATUS_CREATEAFTER (1)
|
||||
#define APPEND_STATUS_ADDINZIP (2)
|
||||
|
||||
/***************************************************************************/
|
||||
/* Writing a zip file */
|
||||
|
||||
ZEXPORT zipFile zipOpen(const char *path, int append);
|
||||
ZEXPORT zipFile zipOpen64(const void *path, int append);
|
||||
ZEXPORT zipFile zipOpen2(const char *path, int append, const char **globalcomment,
|
||||
zlib_filefunc_def *pzlib_filefunc_def);
|
||||
|
||||
ZEXPORT zipFile zipOpen2_64(const void *path, int append, const char **globalcomment,
|
||||
zlib_filefunc64_def *pzlib_filefunc_def);
|
||||
ZEXPORT zipFile zipOpen_MZ(void *stream, int append, const char **globalcomment);
|
||||
|
||||
ZEXPORT void* zipGetHandle_MZ(zipFile);
|
||||
ZEXPORT void* zipGetStream_MZ(zipFile file);
|
||||
|
||||
ZEXPORT int zipOpenNewFileInZip(zipFile file, const char *filename, const zip_fileinfo *zipfi,
|
||||
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
|
||||
uint16_t size_extrafield_global, const char *comment, int compression_method, int level);
|
||||
ZEXPORT int zipOpenNewFileInZip_64(zipFile file, const char *filename, const zip_fileinfo *zipfi,
|
||||
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
|
||||
uint16_t size_extrafield_global, const char *comment, int compression_method, int level,
|
||||
int zip64);
|
||||
ZEXPORT int zipOpenNewFileInZip2(zipFile file, const char *filename, const zip_fileinfo *zipfi,
|
||||
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
|
||||
uint16_t size_extrafield_global, const char *comment, int compression_method, int level,
|
||||
int raw);
|
||||
ZEXPORT int zipOpenNewFileInZip2_64(zipFile file, const char *filename, const zip_fileinfo *zipfi,
|
||||
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
|
||||
uint16_t size_extrafield_global, const char *comment, int compression_method, int level,
|
||||
int raw, int zip64);
|
||||
ZEXPORT int zipOpenNewFileInZip3(zipFile file, const char *filename, const zip_fileinfo *zipfi,
|
||||
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
|
||||
uint16_t size_extrafield_global, const char *comment, int compression_method, int level,
|
||||
int raw, int windowBits, int memLevel, int strategy, const char *password,
|
||||
unsigned long crc_for_crypting);
|
||||
ZEXPORT int zipOpenNewFileInZip3_64(zipFile file, const char *filename, const zip_fileinfo *zipfi,
|
||||
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
|
||||
uint16_t size_extrafield_global, const char *comment, int compression_method, int level,
|
||||
int raw, int windowBits, int memLevel, int strategy, const char *password,
|
||||
uint32_t crc_for_crypting, int zip64);
|
||||
ZEXPORT int zipOpenNewFileInZip4(zipFile file, const char *filename, const zip_fileinfo *zipfi,
|
||||
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
|
||||
uint16_t size_extrafield_global, const char *comment, int compression_method, int level,
|
||||
int raw, int windowBits, int memLevel, int strategy, const char *password,
|
||||
unsigned long crc_for_crypting, unsigned long version_madeby, unsigned long flag_base);
|
||||
ZEXPORT int zipOpenNewFileInZip4_64(zipFile file, const char *filename, const zip_fileinfo *zipfi,
|
||||
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
|
||||
uint16_t size_extrafield_global, const char *comment, int compression_method, int level,
|
||||
int raw, int windowBits, int memLevel, int strategy, const char *password,
|
||||
unsigned long crc_for_crypting, unsigned long version_madeby, unsigned long flag_base, int zip64);
|
||||
ZEXPORT int zipOpenNewFileInZip5(zipFile file, const char *filename, const zip_fileinfo *zipfi,
|
||||
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
|
||||
uint16_t size_extrafield_global, const char *comment, int compression_method, int level,
|
||||
int raw, int windowBits, int memLevel, int strategy, const char *password,
|
||||
unsigned long crc_for_crypting, unsigned long version_madeby, unsigned long flag_base, int zip64);
|
||||
|
||||
ZEXPORT int zipWriteInFileInZip(zipFile file, const void *buf, uint32_t len);
|
||||
|
||||
ZEXPORT int zipCloseFileInZipRaw(zipFile file, unsigned long uncompressed_size, unsigned long crc32);
|
||||
ZEXPORT int zipCloseFileInZipRaw64(zipFile file, uint64_t uncompressed_size, unsigned long crc32);
|
||||
ZEXPORT int zipCloseFileInZip(zipFile file);
|
||||
ZEXPORT int zipCloseFileInZip64(zipFile file);
|
||||
|
||||
ZEXPORT int zipClose(zipFile file, const char *global_comment);
|
||||
ZEXPORT int zipClose_64(zipFile file, const char *global_comment);
|
||||
ZEXPORT int zipClose2_64(zipFile file, const char *global_comment, uint16_t version_madeby);
|
||||
int zipClose_MZ(zipFile file, const char *global_comment);
|
||||
int zipClose2_MZ(zipFile file, const char *global_comment, uint16_t version_madeby);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#if defined(STRICTUNZIP) || defined(STRICTZIPUNZIP)
|
||||
/* like the STRICT of WIN32, we define a pointer that cannot be converted
|
||||
from (void*) without cast */
|
||||
typedef struct TagunzFile__ { int unused; } unz_file__;
|
||||
typedef unz_file__ *unzFile;
|
||||
#else
|
||||
typedef void *unzFile;
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#define UNZ_OK (0)
|
||||
#define UNZ_END_OF_LIST_OF_FILE (-100)
|
||||
#define UNZ_ERRNO (-1)
|
||||
#define UNZ_EOF (0)
|
||||
#define UNZ_PARAMERROR (-102)
|
||||
#define UNZ_BADZIPFILE (-103)
|
||||
#define UNZ_INTERNALERROR (-104)
|
||||
#define UNZ_CRCERROR (-105)
|
||||
#define UNZ_BADPASSWORD (-106)
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct unz_global_info64_s {
|
||||
uint64_t number_entry; /* total number of entries in the central dir on this disk */
|
||||
uint32_t number_disk_with_CD; /* number the the disk with central dir, used for spanning ZIP */
|
||||
uint16_t size_comment; /* size of the global comment of the zipfile */
|
||||
} unz_global_info64;
|
||||
|
||||
typedef struct unz_global_info_s {
|
||||
uint32_t number_entry; /* total number of entries in the central dir on this disk */
|
||||
uint32_t number_disk_with_CD; /* number the the disk with central dir, used for spanning ZIP */
|
||||
uint16_t size_comment; /* size of the global comment of the zipfile */
|
||||
} unz_global_info;
|
||||
|
||||
typedef struct unz_file_info64_s {
|
||||
uint16_t version; /* version made by 2 bytes */
|
||||
uint16_t version_needed; /* version needed to extract 2 bytes */
|
||||
uint16_t flag; /* general purpose bit flag 2 bytes */
|
||||
uint16_t compression_method; /* compression method 2 bytes */
|
||||
uint32_t mz_dos_date; /* last mod file date in Dos fmt 4 bytes */
|
||||
struct tm tmu_date;
|
||||
uint32_t crc; /* crc-32 4 bytes */
|
||||
uint64_t compressed_size; /* compressed size 8 bytes */
|
||||
uint64_t uncompressed_size; /* uncompressed size 8 bytes */
|
||||
uint16_t size_filename; /* filename length 2 bytes */
|
||||
uint16_t size_file_extra; /* extra field length 2 bytes */
|
||||
uint16_t size_file_comment; /* file comment length 2 bytes */
|
||||
|
||||
uint32_t disk_num_start; /* disk number start 4 bytes */
|
||||
uint16_t internal_fa; /* internal file attributes 2 bytes */
|
||||
uint32_t external_fa; /* external file attributes 4 bytes */
|
||||
|
||||
uint64_t disk_offset;
|
||||
|
||||
uint16_t size_file_extra_internal;
|
||||
} unz_file_info64;
|
||||
|
||||
typedef struct unz_file_info_s {
|
||||
uint16_t version; /* version made by 2 bytes */
|
||||
uint16_t version_needed; /* version needed to extract 2 bytes */
|
||||
uint16_t flag; /* general purpose bit flag 2 bytes */
|
||||
uint16_t compression_method; /* compression method 2 bytes */
|
||||
uint32_t mz_dos_date; /* last mod file date in Dos fmt 4 bytes */
|
||||
struct tm tmu_date;
|
||||
uint32_t crc; /* crc-32 4 bytes */
|
||||
uint32_t compressed_size; /* compressed size 4 bytes */
|
||||
uint32_t uncompressed_size; /* uncompressed size 4 bytes */
|
||||
uint16_t size_filename; /* filename length 2 bytes */
|
||||
uint16_t size_file_extra; /* extra field length 2 bytes */
|
||||
uint16_t size_file_comment; /* file comment length 2 bytes */
|
||||
|
||||
uint16_t disk_num_start; /* disk number start 2 bytes */
|
||||
uint16_t internal_fa; /* internal file attributes 2 bytes */
|
||||
uint32_t external_fa; /* external file attributes 4 bytes */
|
||||
|
||||
uint64_t disk_offset;
|
||||
} unz_file_info;
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef int (*unzFileNameComparer)(unzFile file, const char *filename1, const char *filename2);
|
||||
typedef int (*unzIteratorFunction)(unzFile file);
|
||||
typedef int (*unzIteratorFunction2)(unzFile file, unz_file_info64 *pfile_info, char *filename,
|
||||
uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment,
|
||||
uint16_t comment_size);
|
||||
|
||||
/***************************************************************************/
|
||||
/* Reading a zip file */
|
||||
|
||||
ZEXPORT unzFile unzOpen(const char *path);
|
||||
ZEXPORT unzFile unzOpen64(const void *path);
|
||||
ZEXPORT unzFile unzOpen2(const char *path, zlib_filefunc_def *pzlib_filefunc_def);
|
||||
ZEXPORT unzFile unzOpen2_64(const void *path, zlib_filefunc64_def *pzlib_filefunc_def);
|
||||
unzFile unzOpen_MZ(void *stream);
|
||||
|
||||
ZEXPORT int unzClose(unzFile file);
|
||||
ZEXPORT int unzClose_MZ(unzFile file);
|
||||
|
||||
ZEXPORT void* unzGetHandle_MZ(unzFile file);
|
||||
ZEXPORT void* unzGetStream_MZ(zipFile file);
|
||||
|
||||
ZEXPORT int unzGetGlobalInfo(unzFile file, unz_global_info* pglobal_info32);
|
||||
ZEXPORT int unzGetGlobalInfo64(unzFile file, unz_global_info64 *pglobal_info);
|
||||
ZEXPORT int unzGetGlobalComment(unzFile file, char *comment, unsigned long comment_size);
|
||||
|
||||
ZEXPORT int unzOpenCurrentFile(unzFile file);
|
||||
ZEXPORT int unzOpenCurrentFilePassword(unzFile file, const char *password);
|
||||
ZEXPORT int unzOpenCurrentFile2(unzFile file, int *method, int *level, int raw);
|
||||
ZEXPORT int unzOpenCurrentFile3(unzFile file, int *method, int *level, int raw, const char *password);
|
||||
ZEXPORT int unzReadCurrentFile(unzFile file, void *buf, uint32_t len);
|
||||
ZEXPORT int unzCloseCurrentFile(unzFile file);
|
||||
|
||||
ZEXPORT int unzGetCurrentFileInfo(unzFile file, unz_file_info *pfile_info, char *filename,
|
||||
unsigned long filename_size, void *extrafield, unsigned long extrafield_size, char *comment,
|
||||
unsigned long comment_size);
|
||||
ZEXPORT int unzGetCurrentFileInfo64(unzFile file, unz_file_info64 * pfile_info, char *filename,
|
||||
unsigned long filename_size, void *extrafield, unsigned long extrafield_size, char *comment,
|
||||
unsigned long comment_size);
|
||||
|
||||
ZEXPORT int unzGoToFirstFile(unzFile file);
|
||||
ZEXPORT int unzGoToNextFile(unzFile file);
|
||||
ZEXPORT int unzLocateFile(unzFile file, const char *filename, unzFileNameComparer filename_compare_func);
|
||||
|
||||
ZEXPORT int unzGetLocalExtrafield(unzFile file, void *buf, unsigned int len);
|
||||
|
||||
/***************************************************************************/
|
||||
/* Raw access to zip file */
|
||||
|
||||
typedef struct unz_file_pos_s {
|
||||
uint32_t pos_in_zip_directory; /* offset in zip file directory */
|
||||
uint32_t num_of_file; /* # of file */
|
||||
} unz_file_pos;
|
||||
|
||||
ZEXPORT int unzGetFilePos(unzFile file, unz_file_pos *file_pos);
|
||||
ZEXPORT int unzGoToFilePos(unzFile file, unz_file_pos *file_pos);
|
||||
|
||||
typedef struct unz64_file_pos_s {
|
||||
int64_t pos_in_zip_directory; /* offset in zip file directory */
|
||||
uint64_t num_of_file; /* # of file */
|
||||
} unz64_file_pos;
|
||||
|
||||
ZEXPORT int unzGetFilePos64(unzFile file, unz64_file_pos *file_pos);
|
||||
ZEXPORT int unzGoToFilePos64(unzFile file, const unz64_file_pos *file_pos);
|
||||
|
||||
ZEXPORT int64_t unzGetOffset64(unzFile file);
|
||||
ZEXPORT unsigned long
|
||||
unzGetOffset(unzFile file);
|
||||
ZEXPORT int unzSetOffset64(unzFile file, int64_t pos);
|
||||
ZEXPORT int unzSetOffset(unzFile file, unsigned long pos);
|
||||
ZEXPORT int32_t unztell(unzFile file);
|
||||
ZEXPORT int32_t unzTell(unzFile file);
|
||||
ZEXPORT uint64_t unztell64(unzFile file);
|
||||
ZEXPORT uint64_t unzTell64(unzFile file);
|
||||
ZEXPORT int unzSeek(unzFile file, int32_t offset, int origin);
|
||||
ZEXPORT int unzSeek64(unzFile file, int64_t offset, int origin);
|
||||
ZEXPORT int unzEndOfFile(unzFile file);
|
||||
ZEXPORT int unzeof(unzFile file);
|
||||
ZEXPORT void* unzGetStream(unzFile file);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -1,12 +0,0 @@
|
||||
/* @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
|
89
mz_crypt.c
89
mz_crypt.c
@ -34,61 +34,50 @@ 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
|
||||
# ifndef ZLIB_VERNUM
|
||||
/* HAVE_ZLIB but no ZLIB_VERNUM? */
|
||||
typedef uint32_t z_crc_t;
|
||||
# elif (ZLIB_VERNUM & 0xf != 0xf) && (ZLIB_VERNUM < 0x1270)
|
||||
/* Define z_crc_t in zlib 1.2.6 and less */
|
||||
typedef unsigned long z_crc_t;
|
||||
# elif (ZLIB_VERNUM & 0xf == 0xf) && (ZLIB_VERNUM < 0x12df)
|
||||
/* Define z_crc_t in zlib-ng 2.0.7 and less */
|
||||
typedef unsigned int 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);
|
||||
#else
|
||||
static uint32_t crc32_table[256] = {
|
||||
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
|
||||
0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
|
||||
0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
|
||||
0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
|
||||
0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
|
||||
0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
|
||||
0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
|
||||
0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
|
||||
0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
|
||||
0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
|
||||
0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
|
||||
0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
|
||||
0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
|
||||
0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
|
||||
0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
|
||||
0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
|
||||
0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
|
||||
0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
|
||||
0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
|
||||
0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
|
||||
0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
|
||||
0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
|
||||
0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
|
||||
0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
|
||||
0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
|
||||
0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
|
||||
0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
|
||||
0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
|
||||
0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
|
||||
0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
|
||||
0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
|
||||
0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
|
||||
0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
|
||||
0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
|
||||
0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
|
||||
0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
|
||||
0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
|
||||
0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
|
||||
0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
|
||||
0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
|
||||
0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
|
||||
0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
|
||||
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
|
||||
};
|
||||
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832,
|
||||
0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
|
||||
0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856, 0x646ba8c0, 0xfd62f97a,
|
||||
0x8a65c9ec, 0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
|
||||
0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3,
|
||||
0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
|
||||
0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, 0x2f6f7c87, 0x58684c11, 0xc1611dab,
|
||||
0xb6662d3d, 0x76dc4190, 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
|
||||
0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01, 0x6b6b51f4,
|
||||
0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
|
||||
0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, 0x4db26158, 0x3ab551ce, 0xa3bc0074,
|
||||
0xd4bb30e2, 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
|
||||
0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086, 0x5768b525,
|
||||
0x206f85b3, 0xb966d409, 0xce61e49f, 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
|
||||
0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615,
|
||||
0x73dc1683, 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
|
||||
0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7, 0xfed41b76,
|
||||
0x89d32be0, 0x10da7a5a, 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
|
||||
0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c, 0x36034af6,
|
||||
0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
|
||||
0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7,
|
||||
0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
|
||||
0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7,
|
||||
0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
|
||||
0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278,
|
||||
0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
|
||||
0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, 0xbdbdf21c, 0xcabac28a, 0x53b39330,
|
||||
0x24b4a3a6, 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
|
||||
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d};
|
||||
value = ~value;
|
||||
|
||||
while (size > 0) {
|
||||
@ -103,8 +92,8 @@ uint32_t mz_crypt_crc32_update(uint32_t value, const uint8_t *buf, int32_t size)
|
||||
}
|
||||
|
||||
#if defined(HAVE_WZAES)
|
||||
int32_t mz_crypt_pbkdf2(uint8_t *password, int32_t password_length, uint8_t *salt,
|
||||
int32_t salt_length, uint32_t iteration_count, uint8_t *key, uint16_t key_length) {
|
||||
int32_t mz_crypt_pbkdf2(uint8_t *password, int32_t password_length, uint8_t *salt, int32_t salt_length,
|
||||
uint32_t iteration_count, uint8_t *key, uint16_t key_length) {
|
||||
void *hmac1 = NULL;
|
||||
void *hmac2 = NULL;
|
||||
void *hmac3 = NULL;
|
||||
|
60
mz_crypt.h
60
mz_crypt.h
@ -19,42 +19,42 @@ extern "C" {
|
||||
|
||||
uint32_t mz_crypt_crc32_update(uint32_t value, const uint8_t *buf, int32_t size);
|
||||
|
||||
int32_t mz_crypt_pbkdf2(uint8_t *password, int32_t password_length, uint8_t *salt,
|
||||
int32_t salt_length, uint32_t iteration_count, uint8_t *key, uint16_t key_length);
|
||||
int32_t mz_crypt_pbkdf2(uint8_t *password, int32_t password_length, uint8_t *salt, int32_t salt_length,
|
||||
uint32_t iteration_count, uint8_t *key, uint16_t key_length);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t mz_crypt_rand(uint8_t *buf, int32_t size);
|
||||
int32_t mz_crypt_rand(uint8_t *buf, int32_t size);
|
||||
|
||||
void mz_crypt_sha_reset(void *handle);
|
||||
int32_t mz_crypt_sha_begin(void *handle);
|
||||
int32_t mz_crypt_sha_update(void *handle, const void *buf, int32_t size);
|
||||
int32_t mz_crypt_sha_end(void *handle, uint8_t *digest, int32_t digest_size);
|
||||
int32_t mz_crypt_sha_set_algorithm(void *handle, uint16_t algorithm);
|
||||
void* mz_crypt_sha_create(void);
|
||||
void mz_crypt_sha_delete(void **handle);
|
||||
void mz_crypt_sha_reset(void *handle);
|
||||
int32_t mz_crypt_sha_begin(void *handle);
|
||||
int32_t mz_crypt_sha_update(void *handle, const void *buf, int32_t size);
|
||||
int32_t mz_crypt_sha_end(void *handle, uint8_t *digest, int32_t digest_size);
|
||||
int32_t mz_crypt_sha_set_algorithm(void *handle, uint16_t algorithm);
|
||||
void *mz_crypt_sha_create(void);
|
||||
void mz_crypt_sha_delete(void **handle);
|
||||
|
||||
void mz_crypt_aes_reset(void *handle);
|
||||
int32_t mz_crypt_aes_encrypt(void *handle, const void *aad, int32_t aad_size, uint8_t *buf, int32_t size);
|
||||
int32_t mz_crypt_aes_encrypt_final(void *handle, uint8_t *buf, int32_t size, uint8_t *tag, int32_t tag_size);
|
||||
int32_t mz_crypt_aes_decrypt(void *handle, const void *aad, int32_t aad_size, uint8_t *buf, int32_t size);
|
||||
int32_t mz_crypt_aes_decrypt_final(void *handle, uint8_t *buf, int32_t size, const uint8_t *tag, int32_t tag_size);
|
||||
int32_t mz_crypt_aes_set_encrypt_key(void *handle, const void *key, int32_t key_length,
|
||||
const void *iv, int32_t iv_length);
|
||||
int32_t mz_crypt_aes_set_decrypt_key(void *handle, const void *key, int32_t key_length,
|
||||
const void *iv, int32_t iv_length);
|
||||
void mz_crypt_aes_set_mode(void *handle, int32_t mode);
|
||||
void* mz_crypt_aes_create(void);
|
||||
void mz_crypt_aes_delete(void **handle);
|
||||
void mz_crypt_aes_reset(void *handle);
|
||||
int32_t mz_crypt_aes_encrypt(void *handle, const void *aad, int32_t aad_size, uint8_t *buf, int32_t size);
|
||||
int32_t mz_crypt_aes_encrypt_final(void *handle, uint8_t *buf, int32_t size, uint8_t *tag, int32_t tag_size);
|
||||
int32_t mz_crypt_aes_decrypt(void *handle, const void *aad, int32_t aad_size, uint8_t *buf, int32_t size);
|
||||
int32_t mz_crypt_aes_decrypt_final(void *handle, uint8_t *buf, int32_t size, const uint8_t *tag, int32_t tag_size);
|
||||
int32_t mz_crypt_aes_set_encrypt_key(void *handle, const void *key, int32_t key_length, const void *iv,
|
||||
int32_t iv_length);
|
||||
int32_t mz_crypt_aes_set_decrypt_key(void *handle, const void *key, int32_t key_length, const void *iv,
|
||||
int32_t iv_length);
|
||||
void mz_crypt_aes_set_mode(void *handle, int32_t mode);
|
||||
void *mz_crypt_aes_create(void);
|
||||
void mz_crypt_aes_delete(void **handle);
|
||||
|
||||
void mz_crypt_hmac_reset(void *handle);
|
||||
int32_t mz_crypt_hmac_init(void *handle, const void *key, int32_t key_length);
|
||||
int32_t mz_crypt_hmac_update(void *handle, const void *buf, int32_t size);
|
||||
int32_t mz_crypt_hmac_end(void *handle, uint8_t *digest, int32_t digest_size);
|
||||
int32_t mz_crypt_hmac_copy(void *src_handle, void *target_handle);
|
||||
void mz_crypt_hmac_set_algorithm(void *handle, uint16_t algorithm);
|
||||
void* mz_crypt_hmac_create(void);
|
||||
void mz_crypt_hmac_delete(void **handle);
|
||||
void mz_crypt_hmac_reset(void *handle);
|
||||
int32_t mz_crypt_hmac_init(void *handle, const void *key, int32_t key_length);
|
||||
int32_t mz_crypt_hmac_update(void *handle, const void *buf, int32_t size);
|
||||
int32_t mz_crypt_hmac_end(void *handle, uint8_t *digest, int32_t digest_size);
|
||||
int32_t mz_crypt_hmac_copy(void *src_handle, void *target_handle);
|
||||
void mz_crypt_hmac_set_algorithm(void *handle, uint16_t algorithm);
|
||||
void *mz_crypt_hmac_create(void);
|
||||
void mz_crypt_hmac_delete(void **handle);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
|
@ -20,8 +20,12 @@
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
/* Avoid use of private API for iOS, Apple does not allow it on App Store. Zip format doesn't need GCM. */
|
||||
#if !TARGET_OS_IPHONE
|
||||
#ifndef MZ_TARGET_APPSTORE
|
||||
# define MZ_TARGET_APPSTORE 1
|
||||
#endif
|
||||
|
||||
/* Avoid use of private API for App Store as Apple does not allow it. Zip format doesn't need GCM. */
|
||||
#if !MZ_TARGET_APPSTORE
|
||||
enum {
|
||||
kCCModeGCM = 11,
|
||||
};
|
||||
@ -46,21 +50,19 @@ int32_t mz_crypt_rand(uint8_t *buf, int32_t size) {
|
||||
|
||||
typedef struct mz_crypt_sha_s {
|
||||
union {
|
||||
CC_SHA1_CTX ctx1;
|
||||
CC_SHA1_CTX ctx1;
|
||||
CC_SHA256_CTX ctx256;
|
||||
CC_SHA512_CTX ctx512;
|
||||
};
|
||||
int32_t error;
|
||||
int32_t initialized;
|
||||
uint16_t algorithm;
|
||||
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
|
||||
};
|
||||
MZ_HASH_SHA1_SIZE, 0, MZ_HASH_SHA224_SIZE, MZ_HASH_SHA256_SIZE, MZ_HASH_SHA384_SIZE, MZ_HASH_SHA512_SIZE};
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
@ -201,8 +203,8 @@ void mz_crypt_sha_delete(void **handle) {
|
||||
|
||||
typedef struct mz_crypt_aes_s {
|
||||
CCCryptorRef crypt;
|
||||
int32_t mode;
|
||||
int32_t error;
|
||||
int32_t mode;
|
||||
int32_t error;
|
||||
} mz_crypt_aes;
|
||||
|
||||
/***************************************************************************/
|
||||
@ -227,7 +229,7 @@ int32_t mz_crypt_aes_encrypt(void *handle, const void *aad, int32_t aad_size, ui
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
if (aes->mode == MZ_AES_MODE_GCM) {
|
||||
#if TARGET_OS_IPHONE
|
||||
#if MZ_TARGET_APPSTORE
|
||||
return MZ_SUPPORT_ERROR;
|
||||
#else
|
||||
if (aad && aad_size > 0) {
|
||||
@ -251,14 +253,14 @@ int32_t mz_crypt_aes_encrypt(void *handle, const void *aad, int32_t aad_size, ui
|
||||
|
||||
int32_t mz_crypt_aes_encrypt_final(void *handle, uint8_t *buf, int32_t size, uint8_t *tag, int32_t tag_size) {
|
||||
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
|
||||
#if !TARGET_OS_IPHONE
|
||||
#if !MZ_TARGET_APPSTORE
|
||||
size_t tag_outsize = tag_size;
|
||||
#endif
|
||||
|
||||
if (!aes || !tag || !tag_size || !aes->crypt || aes->mode != MZ_AES_MODE_GCM)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
#if TARGET_OS_IPHONE
|
||||
#if MZ_TARGET_APPSTORE
|
||||
return MZ_SUPPORT_ERROR;
|
||||
#else
|
||||
aes->error = CCCryptorGCMEncrypt(aes->crypt, buf, size, buf);
|
||||
@ -282,7 +284,7 @@ int32_t mz_crypt_aes_decrypt(void *handle, const void *aad, int32_t aad_size, ui
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
if (aes->mode == MZ_AES_MODE_GCM) {
|
||||
#if TARGET_OS_IPHONE
|
||||
#if MZ_TARGET_APPSTORE
|
||||
return MZ_SUPPORT_ERROR;
|
||||
#else
|
||||
if (aad && aad_size > 0) {
|
||||
@ -306,7 +308,7 @@ int32_t mz_crypt_aes_decrypt(void *handle, const void *aad, int32_t aad_size, ui
|
||||
|
||||
int32_t mz_crypt_aes_decrypt_final(void *handle, uint8_t *buf, int32_t size, const uint8_t *tag, int32_t tag_length) {
|
||||
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
|
||||
#if !TARGET_OS_IPHONE
|
||||
#if !MZ_TARGET_APPSTORE
|
||||
uint8_t tag_actual_buf[MZ_AES_BLOCK_SIZE];
|
||||
size_t tag_actual_len = sizeof(tag_actual_buf);
|
||||
uint8_t *tag_actual = tag_actual_buf;
|
||||
@ -317,7 +319,7 @@ int32_t mz_crypt_aes_decrypt_final(void *handle, uint8_t *buf, int32_t size, con
|
||||
if (!aes || !tag || !tag_length || !aes->crypt || aes->mode != MZ_AES_MODE_GCM)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
#if TARGET_OS_IPHONE
|
||||
#if MZ_TARGET_APPSTORE
|
||||
return MZ_SUPPORT_ERROR;
|
||||
#else
|
||||
aes->error = CCCryptorGCMDecrypt(aes->crypt, buf, size, buf);
|
||||
@ -343,8 +345,8 @@ int32_t mz_crypt_aes_decrypt_final(void *handle, uint8_t *buf, int32_t size, con
|
||||
#endif
|
||||
}
|
||||
|
||||
static int32_t mz_crypt_aes_set_key(void *handle, const void *key, int32_t key_length,
|
||||
const void *iv, int32_t iv_length, CCOperation op) {
|
||||
static int32_t mz_crypt_aes_set_key(void *handle, const void *key, int32_t key_length, const void *iv,
|
||||
int32_t iv_length, CCOperation op) {
|
||||
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
|
||||
CCMode mode;
|
||||
|
||||
@ -353,7 +355,7 @@ static int32_t mz_crypt_aes_set_key(void *handle, const void *key, int32_t key_l
|
||||
else if (aes->mode == MZ_AES_MODE_ECB)
|
||||
mode = kCCModeECB;
|
||||
else if (aes->mode == MZ_AES_MODE_GCM)
|
||||
#if !TARGET_OS_IPHONE
|
||||
#if !MZ_TARGET_APPSTORE
|
||||
mode = kCCModeGCM;
|
||||
#else
|
||||
return MZ_SUPPORT_ERROR;
|
||||
@ -363,13 +365,13 @@ static int32_t mz_crypt_aes_set_key(void *handle, const void *key, int32_t key_l
|
||||
|
||||
mz_crypt_aes_reset(handle);
|
||||
|
||||
aes->error = CCCryptorCreateWithMode(op, mode, kCCAlgorithmAES, ccNoPadding, iv, key, key_length,
|
||||
NULL, 0, 0, 0, &aes->crypt);
|
||||
aes->error = CCCryptorCreateWithMode(op, mode, kCCAlgorithmAES, ccNoPadding, iv, key, key_length, NULL, 0, 0, 0,
|
||||
&aes->crypt);
|
||||
|
||||
if (aes->error != kCCSuccess)
|
||||
return MZ_HASH_ERROR;
|
||||
|
||||
#if !TARGET_OS_IPHONE
|
||||
#if !MZ_TARGET_APPSTORE
|
||||
if (aes->mode == MZ_AES_MODE_GCM) {
|
||||
aes->error = CCCryptorGCMAddIV(aes->crypt, iv, iv_length);
|
||||
|
||||
@ -381,13 +383,13 @@ static int32_t mz_crypt_aes_set_key(void *handle, const void *key, int32_t key_l
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_crypt_aes_set_encrypt_key(void *handle, const void *key, int32_t key_length,
|
||||
const void *iv, int32_t iv_length) {
|
||||
int32_t mz_crypt_aes_set_encrypt_key(void *handle, const void *key, int32_t key_length, const void *iv,
|
||||
int32_t iv_length) {
|
||||
return mz_crypt_aes_set_key(handle, key, key_length, iv, iv_length, kCCEncrypt);
|
||||
}
|
||||
|
||||
int32_t mz_crypt_aes_set_decrypt_key(void *handle, const void *key, int32_t key_length,
|
||||
const void *iv, int32_t iv_length) {
|
||||
int32_t mz_crypt_aes_set_decrypt_key(void *handle, const void *key, int32_t key_length, const void *iv,
|
||||
int32_t iv_length) {
|
||||
return mz_crypt_aes_set_key(handle, key, key_length, iv, iv_length, kCCDecrypt);
|
||||
}
|
||||
|
||||
@ -416,10 +418,10 @@ void mz_crypt_aes_delete(void **handle) {
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct mz_crypt_hmac_s {
|
||||
CCHmacContext ctx;
|
||||
int32_t initialized;
|
||||
int32_t error;
|
||||
uint16_t algorithm;
|
||||
CCHmacContext ctx;
|
||||
int32_t initialized;
|
||||
int32_t error;
|
||||
uint16_t algorithm;
|
||||
} mz_crypt_hmac;
|
||||
|
||||
/***************************************************************************/
|
||||
|
@ -59,22 +59,20 @@ typedef struct mz_crypt_sha_s {
|
||||
union {
|
||||
SHA512_CTX ctx512;
|
||||
SHA256_CTX ctx256;
|
||||
SHA_CTX ctx1;
|
||||
SHA_CTX ctx1;
|
||||
};
|
||||
#else
|
||||
EVP_MD_CTX *ctx;
|
||||
EVP_MD_CTX *ctx;
|
||||
#endif
|
||||
int32_t initialized;
|
||||
int32_t error;
|
||||
uint16_t algorithm;
|
||||
unsigned long 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
|
||||
};
|
||||
MZ_HASH_SHA1_SIZE, 0, MZ_HASH_SHA224_SIZE, MZ_HASH_SHA256_SIZE, MZ_HASH_SHA384_SIZE, MZ_HASH_SHA512_SIZE};
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
@ -269,8 +267,8 @@ void mz_crypt_sha_delete(void **handle) {
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct mz_crypt_aes_s {
|
||||
int32_t mode;
|
||||
int32_t error;
|
||||
int32_t mode;
|
||||
unsigned long error;
|
||||
EVP_CIPHER_CTX *ctx;
|
||||
} mz_crypt_aes;
|
||||
|
||||
@ -382,8 +380,8 @@ int32_t mz_crypt_aes_decrypt_final(void *handle, uint8_t *buf, int32_t size, con
|
||||
return size;
|
||||
}
|
||||
|
||||
static int32_t mz_crypt_aes_set_key(void *handle, const void *key, int32_t key_length,
|
||||
const void *iv, int32_t iv_length, int32_t encrypt) {
|
||||
static int32_t mz_crypt_aes_set_key(void *handle, const void *key, int32_t key_length, const void *iv,
|
||||
int32_t iv_length, int32_t encrypt) {
|
||||
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
|
||||
const EVP_CIPHER *type = NULL;
|
||||
|
||||
@ -430,8 +428,8 @@ static int32_t mz_crypt_aes_set_key(void *handle, const void *key, int32_t key_l
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_crypt_aes_set_encrypt_key(void *handle, const void *key, int32_t key_length,
|
||||
const void *iv, int32_t iv_length) {
|
||||
int32_t mz_crypt_aes_set_encrypt_key(void *handle, const void *key, int32_t key_length, const void *iv,
|
||||
int32_t iv_length) {
|
||||
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
|
||||
|
||||
if (!aes || !key || !key_length)
|
||||
@ -446,8 +444,8 @@ int32_t mz_crypt_aes_set_encrypt_key(void *handle, const void *key, int32_t key_
|
||||
return mz_crypt_aes_set_key(handle, key, key_length, iv, iv_length, 1);
|
||||
}
|
||||
|
||||
int32_t mz_crypt_aes_set_decrypt_key(void *handle, const void *key, int32_t key_length,
|
||||
const void *iv, int32_t iv_length) {
|
||||
int32_t mz_crypt_aes_set_decrypt_key(void *handle, const void *key, int32_t key_length, const void *iv,
|
||||
int32_t iv_length) {
|
||||
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
|
||||
|
||||
if (!aes || !key || !key_length)
|
||||
@ -488,19 +486,19 @@ void mz_crypt_aes_delete(void **handle) {
|
||||
|
||||
typedef struct mz_crypt_hmac_s {
|
||||
#if OPENSSL_VERSION_NUMBER < 0x30000000L
|
||||
HMAC_CTX *ctx;
|
||||
HMAC_CTX *ctx;
|
||||
#else
|
||||
EVP_MAC *mac;
|
||||
EVP_MAC *mac;
|
||||
EVP_MAC_CTX *ctx;
|
||||
#endif
|
||||
int32_t initialized;
|
||||
int32_t error;
|
||||
uint16_t algorithm;
|
||||
unsigned long error;
|
||||
int32_t initialized;
|
||||
uint16_t algorithm;
|
||||
} mz_crypt_hmac;
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#if (OPENSSL_VERSION_NUMBER < 0x10100000L) || \
|
||||
#if (OPENSSL_VERSION_NUMBER < 0x10100000L) || \
|
||||
(defined(LIBRESSL_VERSION_NUMBER) && (LIBRESSL_VERSION_NUMBER < 0x2070000fL))
|
||||
static HMAC_CTX *HMAC_CTX_new(void) {
|
||||
HMAC_CTX *ctx = OPENSSL_malloc(sizeof(HMAC_CTX));
|
||||
|
@ -19,11 +19,11 @@
|
||||
#endif
|
||||
|
||||
#if _WIN32_WINNT >= _WIN32_WINNT_VISTA
|
||||
#include <bcrypt.h>
|
||||
# include <bcrypt.h>
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#define NT_SUCCESS(status) ((status) >= 0)
|
||||
# define NT_SUCCESS(status) ((status) >= 0)
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
@ -47,13 +47,13 @@ int32_t mz_crypt_rand(uint8_t *buf, int32_t size) {
|
||||
typedef struct mz_crypt_sha_s {
|
||||
union {
|
||||
struct {
|
||||
BCRYPT_ALG_HANDLE provider;
|
||||
BCRYPT_ALG_HANDLE provider;
|
||||
BCRYPT_HASH_HANDLE hash;
|
||||
uint8_t *buffer;
|
||||
uint8_t *buffer;
|
||||
};
|
||||
};
|
||||
int32_t error;
|
||||
uint16_t algorithm;
|
||||
int32_t error;
|
||||
uint16_t algorithm;
|
||||
} mz_crypt_sha;
|
||||
|
||||
/***************************************************************************/
|
||||
@ -104,8 +104,8 @@ int32_t mz_crypt_sha_begin(void *handle) {
|
||||
|
||||
status = BCryptOpenAlgorithmProvider(&sha->provider, alg_id, NULL, 0);
|
||||
if (NT_SUCCESS(status)) {
|
||||
status = BCryptGetProperty(sha->provider, BCRYPT_OBJECT_LENGTH, (PUCHAR)&buffer_size, result_size,
|
||||
&result_size, 0);
|
||||
status =
|
||||
BCryptGetProperty(sha->provider, BCRYPT_OBJECT_LENGTH, (PUCHAR)&buffer_size, result_size, &result_size, 0);
|
||||
}
|
||||
if (NT_SUCCESS(status)) {
|
||||
sha->buffer = malloc(buffer_size);
|
||||
@ -134,7 +134,7 @@ int32_t mz_crypt_sha_update(void *handle, const void *buf, int32_t size) {
|
||||
if (sha->hash == 0)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
status = BCryptHashData(sha->hash, (uint8_t*)buf, size, 0);
|
||||
status = BCryptHashData(sha->hash, (uint8_t *)buf, size, 0);
|
||||
if (!NT_SUCCESS(status)) {
|
||||
sha->error = status;
|
||||
return MZ_HASH_ERROR;
|
||||
@ -199,22 +199,22 @@ void mz_crypt_sha_delete(void **handle) {
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#define MZ_AES_MAX_TAG_SIZE (16)
|
||||
#define MZ_AES_MAX_NONCE_SIZE (12)
|
||||
# define MZ_AES_MAX_TAG_SIZE (16)
|
||||
# define MZ_AES_MAX_NONCE_SIZE (12)
|
||||
|
||||
typedef struct mz_crypt_aes_s {
|
||||
BCRYPT_ALG_HANDLE provider;
|
||||
BCRYPT_KEY_HANDLE key;
|
||||
uint8_t *key_buffer;
|
||||
int32_t mode;
|
||||
int32_t error;
|
||||
uint8_t *iv;
|
||||
uint32_t iv_length;
|
||||
uint8_t nonce[MZ_AES_MAX_NONCE_SIZE];
|
||||
uint32_t nonce_length;
|
||||
uint8_t mac[MZ_AES_MAX_TAG_SIZE];
|
||||
uint8_t tag[MZ_AES_MAX_TAG_SIZE];
|
||||
uint32_t tag_size;
|
||||
uint8_t *key_buffer;
|
||||
int32_t mode;
|
||||
int32_t error;
|
||||
uint8_t *iv;
|
||||
uint32_t iv_length;
|
||||
uint8_t nonce[MZ_AES_MAX_NONCE_SIZE];
|
||||
uint32_t nonce_length;
|
||||
uint8_t mac[MZ_AES_MAX_TAG_SIZE];
|
||||
uint8_t tag[MZ_AES_MAX_TAG_SIZE];
|
||||
uint32_t tag_size;
|
||||
BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO *auth_info;
|
||||
} mz_crypt_aes;
|
||||
|
||||
@ -253,12 +253,11 @@ int32_t mz_crypt_aes_encrypt(void *handle, const void *aad, int32_t aad_size, ui
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
if (aad && aes->auth_info && !(aes->auth_info->dwFlags & BCRYPT_AUTH_MODE_IN_PROGRESS_FLAG)) {
|
||||
aes->auth_info->pbAuthData = (uint8_t*)aad;
|
||||
aes->auth_info->pbAuthData = (uint8_t *)aad;
|
||||
aes->auth_info->cbAuthData = aad_size;
|
||||
}
|
||||
|
||||
status = BCryptEncrypt(aes->key, buf, size, aes->auth_info, aes->iv, aes->iv_length, buf, size,
|
||||
&output_size, 0);
|
||||
status = BCryptEncrypt(aes->key, buf, size, aes->auth_info, aes->iv, aes->iv_length, buf, size, &output_size, 0);
|
||||
|
||||
if (aad && aes->auth_info) {
|
||||
aes->auth_info->pbAuthData = NULL;
|
||||
@ -285,8 +284,7 @@ int32_t mz_crypt_aes_encrypt_final(void *handle, uint8_t *buf, int32_t size, uin
|
||||
|
||||
aes->auth_info->dwFlags &= ~BCRYPT_AUTH_MODE_CHAIN_CALLS_FLAG;
|
||||
|
||||
status = BCryptEncrypt(aes->key, buf, size, aes->auth_info, aes->iv, aes->iv_length, buf, size,
|
||||
&output_size, 0);
|
||||
status = BCryptEncrypt(aes->key, buf, size, aes->auth_info, aes->iv, aes->iv_length, buf, size, &output_size, 0);
|
||||
|
||||
if (!NT_SUCCESS(status)) {
|
||||
aes->error = status;
|
||||
@ -309,12 +307,11 @@ int32_t mz_crypt_aes_decrypt(void *handle, const void *aad, int32_t aad_size, ui
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
if (aad && aes->auth_info && !(aes->auth_info->dwFlags & BCRYPT_AUTH_MODE_IN_PROGRESS_FLAG)) {
|
||||
aes->auth_info->pbAuthData = (uint8_t*)aad;
|
||||
aes->auth_info->pbAuthData = (uint8_t *)aad;
|
||||
aes->auth_info->cbAuthData = aad_size;
|
||||
}
|
||||
|
||||
status = BCryptDecrypt(aes->key, buf, size, aes->auth_info, aes->iv, aes->iv_length, buf, size,
|
||||
&output_size, 0);
|
||||
status = BCryptDecrypt(aes->key, buf, size, aes->auth_info, aes->iv, aes->iv_length, buf, size, &output_size, 0);
|
||||
|
||||
if (aad && aes->auth_info) {
|
||||
aes->auth_info->pbAuthData = NULL;
|
||||
@ -341,8 +338,7 @@ int32_t mz_crypt_aes_decrypt_final(void *handle, uint8_t *buf, int32_t size, con
|
||||
|
||||
aes->auth_info->dwFlags &= ~BCRYPT_AUTH_MODE_CHAIN_CALLS_FLAG;
|
||||
|
||||
status = BCryptDecrypt(aes->key, buf, size, aes->auth_info, aes->iv, aes->iv_length, buf, size,
|
||||
&output_size, 0);
|
||||
status = BCryptDecrypt(aes->key, buf, size, aes->auth_info, aes->iv, aes->iv_length, buf, size, &output_size, 0);
|
||||
|
||||
if (!NT_SUCCESS(status)) {
|
||||
aes->error = status;
|
||||
@ -352,8 +348,8 @@ int32_t mz_crypt_aes_decrypt_final(void *handle, uint8_t *buf, int32_t size, con
|
||||
return size;
|
||||
}
|
||||
|
||||
static int32_t mz_crypt_aes_set_key(void *handle, const void *key, int32_t key_length,
|
||||
const void *iv, int32_t iv_length) {
|
||||
static int32_t mz_crypt_aes_set_key(void *handle, const void *key, int32_t key_length, const void *iv,
|
||||
int32_t iv_length) {
|
||||
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
|
||||
BCRYPT_KEY_DATA_BLOB_HEADER *key_blob = NULL;
|
||||
int32_t key_blob_size = 0;
|
||||
@ -399,8 +395,8 @@ static int32_t mz_crypt_aes_set_key(void *handle, const void *key, int32_t key_l
|
||||
|
||||
if (NT_SUCCESS(status)) {
|
||||
ULONG result_size;
|
||||
status = BCryptGetProperty(aes->provider, BCRYPT_OBJECT_LENGTH, (PUCHAR)&key_size,
|
||||
sizeof(key_size), &result_size, 0);
|
||||
status = BCryptGetProperty(aes->provider, BCRYPT_OBJECT_LENGTH, (PUCHAR)&key_size, sizeof(key_size),
|
||||
&result_size, 0);
|
||||
}
|
||||
|
||||
if (NT_SUCCESS(status)) {
|
||||
@ -414,10 +410,10 @@ static int32_t mz_crypt_aes_set_key(void *handle, const void *key, int32_t key_l
|
||||
key_blob->dwVersion = BCRYPT_KEY_DATA_BLOB_VERSION1;
|
||||
key_blob->cbKeyData = key_length;
|
||||
|
||||
memcpy((uint8_t*)key_blob + sizeof(*key_blob), key, key_length);
|
||||
memcpy((uint8_t *)key_blob + sizeof(*key_blob), key, key_length);
|
||||
|
||||
status = BCryptImportKey(aes->provider, NULL, BCRYPT_KEY_DATA_BLOB, &aes->key, aes->key_buffer,
|
||||
key_size, (PUCHAR)key_blob, key_blob_size, 0);
|
||||
status = BCryptImportKey(aes->provider, NULL, BCRYPT_KEY_DATA_BLOB, &aes->key, aes->key_buffer, key_size,
|
||||
(PUCHAR)key_blob, key_blob_size, 0);
|
||||
SecureZeroMemory(key_blob, key_blob_size);
|
||||
free(key_blob);
|
||||
}
|
||||
@ -436,8 +432,8 @@ static int32_t mz_crypt_aes_set_key(void *handle, const void *key, int32_t key_l
|
||||
if (NT_SUCCESS(status)) {
|
||||
BCRYPT_AUTH_TAG_LENGTHS_STRUCT tag_lengths;
|
||||
|
||||
status = BCryptGetProperty(aes->provider, BCRYPT_AUTH_TAG_LENGTH, (PUCHAR)&tag_lengths,
|
||||
sizeof(tag_lengths), &result_size, 0);
|
||||
status = BCryptGetProperty(aes->provider, BCRYPT_AUTH_TAG_LENGTH, (PUCHAR)&tag_lengths, sizeof(tag_lengths),
|
||||
&result_size, 0);
|
||||
|
||||
if (NT_SUCCESS(status)) {
|
||||
aes->tag_size = tag_lengths.dwMaxLength;
|
||||
@ -453,8 +449,8 @@ static int32_t mz_crypt_aes_set_key(void *handle, const void *key, int32_t key_l
|
||||
if (NT_SUCCESS(status)) {
|
||||
ULONG block_length;
|
||||
|
||||
status = BCryptGetProperty(aes->provider, BCRYPT_BLOCK_LENGTH, (PUCHAR)&block_length,
|
||||
sizeof(block_length), &result_size, 0);
|
||||
status = BCryptGetProperty(aes->provider, BCRYPT_BLOCK_LENGTH, (PUCHAR)&block_length, sizeof(block_length),
|
||||
&result_size, 0);
|
||||
|
||||
if (NT_SUCCESS(status) && iv) {
|
||||
if (aes->iv_length > block_length)
|
||||
@ -479,13 +475,13 @@ static int32_t mz_crypt_aes_set_key(void *handle, const void *key, int32_t key_l
|
||||
return err;
|
||||
}
|
||||
|
||||
int32_t mz_crypt_aes_set_encrypt_key(void *handle, const void *key, int32_t key_length,
|
||||
const void *iv, int32_t iv_length) {
|
||||
int32_t mz_crypt_aes_set_encrypt_key(void *handle, const void *key, int32_t key_length, const void *iv,
|
||||
int32_t iv_length) {
|
||||
return mz_crypt_aes_set_key(handle, key, key_length, iv, iv_length);
|
||||
}
|
||||
|
||||
int32_t mz_crypt_aes_set_decrypt_key(void *handle, const void *key, int32_t key_length,
|
||||
const void *iv, int32_t iv_length) {
|
||||
int32_t mz_crypt_aes_set_decrypt_key(void *handle, const void *key, int32_t key_length, const void *iv,
|
||||
int32_t iv_length) {
|
||||
return mz_crypt_aes_set_key(handle, key, key_length, iv, iv_length);
|
||||
}
|
||||
|
||||
@ -514,12 +510,12 @@ void mz_crypt_aes_delete(void **handle) {
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct mz_crypt_hmac_s {
|
||||
BCRYPT_ALG_HANDLE provider;
|
||||
BCRYPT_KEY_HANDLE key;
|
||||
BCRYPT_ALG_HANDLE provider;
|
||||
BCRYPT_KEY_HANDLE key;
|
||||
BCRYPT_HASH_HANDLE hash;
|
||||
uint8_t *buffer;
|
||||
int32_t error;
|
||||
uint16_t algorithm;
|
||||
uint8_t *buffer;
|
||||
int32_t error;
|
||||
uint16_t algorithm;
|
||||
} mz_crypt_hmac;
|
||||
|
||||
/***************************************************************************/
|
||||
@ -561,8 +557,8 @@ int32_t mz_crypt_hmac_init(void *handle, const void *key, int32_t key_length) {
|
||||
|
||||
status = BCryptOpenAlgorithmProvider(&hmac->provider, alg_id, NULL, BCRYPT_ALG_HANDLE_HMAC_FLAG);
|
||||
if (NT_SUCCESS(status)) {
|
||||
status = BCryptGetProperty(hmac->provider, BCRYPT_OBJECT_LENGTH, (PUCHAR)&buffer_size, result_size,
|
||||
&result_size, 0);
|
||||
status =
|
||||
BCryptGetProperty(hmac->provider, BCRYPT_OBJECT_LENGTH, (PUCHAR)&buffer_size, result_size, &result_size, 0);
|
||||
}
|
||||
if (NT_SUCCESS(status)) {
|
||||
hmac->buffer = malloc(buffer_size);
|
||||
@ -590,7 +586,7 @@ int32_t mz_crypt_hmac_update(void *handle, const void *buf, int32_t size) {
|
||||
if (!hmac || !buf || !hmac->hash)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
status = BCryptHashData(hmac->hash, (uint8_t*)buf, size, 0);
|
||||
status = BCryptHashData(hmac->hash, (uint8_t *)buf, size, 0);
|
||||
if (!NT_SUCCESS(status)) {
|
||||
hmac->error = status;
|
||||
return MZ_HASH_ERROR;
|
||||
|
@ -15,7 +15,7 @@
|
||||
#include <windows.h>
|
||||
|
||||
#if _WIN32_WINNT <= _WIN32_WINNT_WINXP
|
||||
#include <wincrypt.h>
|
||||
# include <wincrypt.h>
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
@ -39,12 +39,12 @@ int32_t mz_crypt_rand(uint8_t *buf, int32_t size) {
|
||||
typedef struct mz_crypt_sha_s {
|
||||
union {
|
||||
struct {
|
||||
HCRYPTPROV provider;
|
||||
HCRYPTHASH hash;
|
||||
HCRYPTPROV provider;
|
||||
HCRYPTHASH hash;
|
||||
};
|
||||
};
|
||||
int32_t error;
|
||||
uint16_t algorithm;
|
||||
int32_t error;
|
||||
uint16_t algorithm;
|
||||
} mz_crypt_sha;
|
||||
|
||||
/***************************************************************************/
|
||||
@ -81,7 +81,7 @@ int32_t mz_crypt_sha_begin(void *handle) {
|
||||
case MZ_HASH_SHA1:
|
||||
alg_id = CALG_SHA1;
|
||||
break;
|
||||
#if NTDDI_VERSION > NTDDI_WINXPSP2
|
||||
# if NTDDI_VERSION > NTDDI_WINXPSP2
|
||||
case MZ_HASH_SHA256:
|
||||
alg_id = CALG_SHA_256;
|
||||
break;
|
||||
@ -91,12 +91,12 @@ int32_t mz_crypt_sha_begin(void *handle) {
|
||||
case MZ_HASH_SHA512:
|
||||
alg_id = CALG_SHA_512;
|
||||
break;
|
||||
#else
|
||||
# else
|
||||
case MZ_HASH_SHA256:
|
||||
case MZ_HASH_SHA384:
|
||||
case MZ_HASH_SHA512:
|
||||
return MZ_SUPPORT_ERROR;
|
||||
#endif
|
||||
# endif
|
||||
default:
|
||||
return MZ_PARAM_ERROR;
|
||||
}
|
||||
@ -197,9 +197,9 @@ void mz_crypt_sha_delete(void **handle) {
|
||||
|
||||
typedef struct mz_crypt_aes_s {
|
||||
HCRYPTPROV provider;
|
||||
HCRYPTKEY key;
|
||||
int32_t mode;
|
||||
int32_t error;
|
||||
HCRYPTKEY key;
|
||||
int32_t mode;
|
||||
int32_t error;
|
||||
} mz_crypt_aes;
|
||||
|
||||
/***************************************************************************/
|
||||
@ -253,14 +253,14 @@ int32_t mz_crypt_aes_decrypt_final(void *handle, uint8_t *buf, int32_t size, con
|
||||
return MZ_SUPPORT_ERROR;
|
||||
}
|
||||
|
||||
static int32_t mz_crypt_aes_set_key(void *handle, const void *key, int32_t key_length,
|
||||
const void *iv, int32_t iv_length) {
|
||||
static int32_t mz_crypt_aes_set_key(void *handle, const void *key, int32_t key_length, const void *iv,
|
||||
int32_t iv_length) {
|
||||
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
|
||||
HCRYPTHASH hash = 0;
|
||||
ALG_ID alg_id = 0;
|
||||
typedef struct key_blob_header_s {
|
||||
BLOBHEADER hdr;
|
||||
uint32_t key_length;
|
||||
uint32_t key_length;
|
||||
} key_blob_header_s;
|
||||
key_blob_header_s *key_blob_s = NULL;
|
||||
uint32_t mode;
|
||||
@ -295,7 +295,7 @@ static int32_t mz_crypt_aes_set_key(void *handle, const void *key, int32_t key_l
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
result = CryptAcquireContext(&aes->provider, NULL, MS_ENH_RSA_AES_PROV, PROV_RSA_AES,
|
||||
CRYPT_VERIFYCONTEXT | CRYPT_SILENT);
|
||||
CRYPT_VERIFYCONTEXT | CRYPT_SILENT);
|
||||
if (result) {
|
||||
key_blob_size = sizeof(key_blob_header_s) + key_length;
|
||||
key_blob = (uint8_t *)malloc(key_blob_size);
|
||||
@ -321,7 +321,6 @@ static int32_t mz_crypt_aes_set_key(void *handle, const void *key, int32_t key_l
|
||||
if (result && err == MZ_OK)
|
||||
result = CryptSetKeyParam(aes->key, KP_MODE, (const uint8_t *)&mode, 0);
|
||||
|
||||
|
||||
if (!result && err == MZ_OK) {
|
||||
aes->error = GetLastError();
|
||||
err = MZ_CRYPT_ERROR;
|
||||
@ -345,13 +344,13 @@ static int32_t mz_crypt_aes_set_key(void *handle, const void *key, int32_t key_l
|
||||
return err;
|
||||
}
|
||||
|
||||
int32_t mz_crypt_aes_set_encrypt_key(void *handle, const void *key, int32_t key_length,
|
||||
const void *iv, int32_t iv_length) {
|
||||
int32_t mz_crypt_aes_set_encrypt_key(void *handle, const void *key, int32_t key_length, const void *iv,
|
||||
int32_t iv_length) {
|
||||
return mz_crypt_aes_set_key(handle, key, key_length, iv, iv_length);
|
||||
}
|
||||
|
||||
int32_t mz_crypt_aes_set_decrypt_key(void *handle, const void *key, int32_t key_length,
|
||||
const void *iv, int32_t iv_length) {
|
||||
int32_t mz_crypt_aes_set_decrypt_key(void *handle, const void *key, int32_t key_length, const void *iv,
|
||||
int32_t iv_length) {
|
||||
return mz_crypt_aes_set_key(handle, key, key_length, iv, iv_length);
|
||||
}
|
||||
|
||||
@ -382,11 +381,11 @@ void mz_crypt_aes_delete(void **handle) {
|
||||
typedef struct mz_crypt_hmac_s {
|
||||
HCRYPTPROV provider;
|
||||
HCRYPTHASH hash;
|
||||
HCRYPTKEY key;
|
||||
HMAC_INFO info;
|
||||
int32_t mode;
|
||||
int32_t error;
|
||||
uint16_t algorithm;
|
||||
HCRYPTKEY key;
|
||||
HMAC_INFO info;
|
||||
int32_t mode;
|
||||
int32_t error;
|
||||
uint16_t algorithm;
|
||||
} mz_crypt_hmac;
|
||||
|
||||
/***************************************************************************/
|
||||
@ -414,7 +413,7 @@ int32_t mz_crypt_hmac_init(void *handle, const void *key, int32_t key_length) {
|
||||
ALG_ID alg_id = 0;
|
||||
typedef struct key_blob_header_s {
|
||||
BLOBHEADER hdr;
|
||||
uint32_t key_length;
|
||||
uint32_t key_length;
|
||||
} key_blob_header_s;
|
||||
key_blob_header_s *key_blob_s = NULL;
|
||||
uint8_t *key_blob = NULL;
|
||||
@ -431,16 +430,16 @@ int32_t mz_crypt_hmac_init(void *handle, const void *key, int32_t key_length) {
|
||||
if (hmac->algorithm == MZ_HASH_SHA1)
|
||||
alg_id = CALG_SHA1;
|
||||
else
|
||||
#ifdef CALG_SHA_256
|
||||
# ifdef CALG_SHA_256
|
||||
alg_id = CALG_SHA_256;
|
||||
#else
|
||||
# else
|
||||
return MZ_SUPPORT_ERROR;
|
||||
#endif
|
||||
# endif
|
||||
|
||||
hmac->info.HashAlgid = alg_id;
|
||||
|
||||
result = CryptAcquireContext(&hmac->provider, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL,
|
||||
CRYPT_VERIFYCONTEXT | CRYPT_SILENT);
|
||||
result =
|
||||
CryptAcquireContext(&hmac->provider, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT);
|
||||
|
||||
if (!result) {
|
||||
hmac->error = GetLastError();
|
||||
|
25
mz_os.c
25
mz_os.c
@ -68,7 +68,7 @@ int32_t mz_path_remove_slash(char *path) {
|
||||
|
||||
int32_t mz_path_has_slash(const char *path) {
|
||||
int32_t path_len = (int32_t)strlen(path);
|
||||
if (path[path_len - 1] != '\\' && path[path_len - 1] != '/')
|
||||
if (path_len > 0 && path[path_len - 1] != '\\' && path[path_len - 1] != '/')
|
||||
return MZ_EXIST_ERROR;
|
||||
return MZ_OK;
|
||||
}
|
||||
@ -136,12 +136,12 @@ int32_t mz_path_resolve(const char *path, char *output, int32_t max_output) {
|
||||
|
||||
while (*source != 0 && max_output > 1) {
|
||||
check = source;
|
||||
if ((*check == '\\') || (*check == '/'))
|
||||
if (*check == '\\' || *check == '/')
|
||||
check += 1;
|
||||
|
||||
if ((source == path) || (target == output) || (check != source)) {
|
||||
if (source == path || target == output || check != source) {
|
||||
/* Skip double paths */
|
||||
if ((*check == '\\') || (*check == '/')) {
|
||||
if (*check == '\\' || *check == '/') {
|
||||
source += 1;
|
||||
continue;
|
||||
}
|
||||
@ -149,7 +149,7 @@ int32_t mz_path_resolve(const char *path, char *output, int32_t max_output) {
|
||||
check += 1;
|
||||
|
||||
/* Remove . if at end of string and not at the beginning */
|
||||
if ((*check == 0) && (source != path && target != output)) {
|
||||
if (*check == 0 && source != path && target != output) {
|
||||
/* Copy last slash */
|
||||
*target = *source;
|
||||
target += 1;
|
||||
@ -158,7 +158,7 @@ int32_t mz_path_resolve(const char *path, char *output, int32_t max_output) {
|
||||
continue;
|
||||
}
|
||||
/* Remove . if not at end of string */
|
||||
else if ((*check == '\\') || (*check == '/')) {
|
||||
else if (*check == '\\' || *check == '/') {
|
||||
source += (check - source);
|
||||
/* Skip slash if at beginning of string */
|
||||
if (target == output && *source != 0)
|
||||
@ -168,14 +168,14 @@ int32_t mz_path_resolve(const char *path, char *output, int32_t max_output) {
|
||||
/* Go to parent directory .. */
|
||||
else if (*check == '.') {
|
||||
check += 1;
|
||||
if ((*check == 0) || (*check == '\\' || *check == '/')) {
|
||||
if (*check == 0 || (*check == '\\' || *check == '/')) {
|
||||
source += (check - source);
|
||||
|
||||
/* Search backwards for previous slash */
|
||||
/* Search backwards for previous slash or the start of the output string */
|
||||
if (target != output) {
|
||||
target -= 1;
|
||||
do {
|
||||
if ((*target == '\\') || (*target == '/'))
|
||||
if (target == output || *target == '\\' || *target == '/')
|
||||
break;
|
||||
|
||||
target -= 1;
|
||||
@ -183,9 +183,9 @@ int32_t mz_path_resolve(const char *path, char *output, int32_t max_output) {
|
||||
} while (target > output);
|
||||
}
|
||||
|
||||
if ((target == output) && (*source != 0))
|
||||
if ((target == output) && *source != 0)
|
||||
source += 1;
|
||||
if ((*target == '\\' || *target == '/') && (*source == 0))
|
||||
if ((*target == '\\' || *target == '/') && *source == 0)
|
||||
target += 1;
|
||||
|
||||
*target = 0;
|
||||
@ -283,6 +283,9 @@ int32_t mz_dir_make(const char *path) {
|
||||
char *match = NULL;
|
||||
char hold = 0;
|
||||
|
||||
if (!*path)
|
||||
return MZ_OK;
|
||||
|
||||
current_dir = strdup(path);
|
||||
if (!current_dir)
|
||||
return MZ_MEM_ERROR;
|
||||
|
56
mz_os.h
56
mz_os.h
@ -37,15 +37,14 @@ extern "C" {
|
||||
# define MZ_VERSION_MADEBY_ZIP_VERSION (45)
|
||||
#endif
|
||||
|
||||
#define MZ_VERSION_MADEBY ((MZ_VERSION_MADEBY_HOST_SYSTEM << 8) | \
|
||||
(MZ_VERSION_MADEBY_ZIP_VERSION))
|
||||
#define MZ_VERSION_MADEBY ((MZ_VERSION_MADEBY_HOST_SYSTEM << 8) | (MZ_VERSION_MADEBY_ZIP_VERSION))
|
||||
|
||||
#define MZ_PATH_SLASH_UNIX ('/')
|
||||
#define MZ_PATH_SLASH_WINDOWS ('\\')
|
||||
#define MZ_PATH_SLASH_UNIX ('/')
|
||||
#define MZ_PATH_SLASH_WINDOWS ('\\')
|
||||
#if defined(_WIN32)
|
||||
# define MZ_PATH_SLASH_PLATFORM (MZ_PATH_SLASH_WINDOWS)
|
||||
# define MZ_PATH_SLASH_PLATFORM (MZ_PATH_SLASH_WINDOWS)
|
||||
#else
|
||||
# define MZ_PATH_SLASH_PLATFORM (MZ_PATH_SLASH_UNIX)
|
||||
# define MZ_PATH_SLASH_PLATFORM (MZ_PATH_SLASH_UNIX)
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
@ -54,9 +53,9 @@ extern "C" {
|
||||
struct dirent {
|
||||
char d_name[256];
|
||||
};
|
||||
typedef void* DIR;
|
||||
typedef void *DIR;
|
||||
#else
|
||||
#include <dirent.h>
|
||||
# include <dirent.h>
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
@ -104,64 +103,63 @@ int32_t mz_file_get_crc(const char *path, uint32_t *result_crc);
|
||||
wchar_t *mz_os_unicode_string_create(const char *string, int32_t encoding);
|
||||
/* Create unicode string from a utf8 string */
|
||||
|
||||
void mz_os_unicode_string_delete(wchar_t **string);
|
||||
void mz_os_unicode_string_delete(wchar_t **string);
|
||||
/* Delete a unicode string that was created */
|
||||
|
||||
char *mz_os_utf8_string_create(const char *string, int32_t encoding);
|
||||
char *mz_os_utf8_string_create(const char *string, int32_t encoding);
|
||||
/* Create a utf8 string from a string with another encoding */
|
||||
|
||||
void mz_os_utf8_string_delete(char **string);
|
||||
void mz_os_utf8_string_delete(char **string);
|
||||
/* Delete a utf8 string that was created */
|
||||
|
||||
int32_t mz_os_rand(uint8_t *buf, int32_t size);
|
||||
int32_t mz_os_rand(uint8_t *buf, int32_t size);
|
||||
/* Random number generator (not cryptographically secure) */
|
||||
|
||||
int32_t mz_os_rename(const char *source_path, const char *target_path);
|
||||
int32_t mz_os_rename(const char *source_path, const char *target_path);
|
||||
/* Rename a file */
|
||||
|
||||
int32_t mz_os_unlink(const char *path);
|
||||
int32_t mz_os_unlink(const char *path);
|
||||
/* Delete an existing file */
|
||||
|
||||
int32_t mz_os_file_exists(const char *path);
|
||||
int32_t mz_os_file_exists(const char *path);
|
||||
/* Check to see if a file exists */
|
||||
|
||||
int64_t mz_os_get_file_size(const char *path);
|
||||
int64_t mz_os_get_file_size(const char *path);
|
||||
/* Gets the length of a file */
|
||||
|
||||
int32_t mz_os_get_file_date(const char *path, time_t *modified_date, time_t *accessed_date, time_t *creation_date);
|
||||
int32_t mz_os_get_file_date(const char *path, time_t *modified_date, time_t *accessed_date, time_t *creation_date);
|
||||
/* Gets a file's modified, access, and creation dates if supported */
|
||||
|
||||
int32_t mz_os_set_file_date(const char *path, time_t modified_date, time_t accessed_date, time_t creation_date);
|
||||
int32_t mz_os_set_file_date(const char *path, time_t modified_date, time_t accessed_date, time_t creation_date);
|
||||
/* Sets a file's modified, access, and creation dates if supported */
|
||||
|
||||
int32_t mz_os_get_file_attribs(const char *path, uint32_t *attributes);
|
||||
int32_t mz_os_get_file_attribs(const char *path, uint32_t *attributes);
|
||||
/* Gets a file's attributes */
|
||||
|
||||
int32_t mz_os_set_file_attribs(const char *path, uint32_t attributes);
|
||||
int32_t mz_os_set_file_attribs(const char *path, uint32_t attributes);
|
||||
/* Sets a file's attributes */
|
||||
|
||||
int32_t mz_os_make_dir(const char *path);
|
||||
int32_t mz_os_make_dir(const char *path);
|
||||
/* Recursively creates a directory */
|
||||
|
||||
DIR* mz_os_open_dir(const char *path);
|
||||
DIR *mz_os_open_dir(const char *path);
|
||||
/* Opens a directory for listing */
|
||||
struct
|
||||
dirent* mz_os_read_dir(DIR *dir);
|
||||
struct dirent *mz_os_read_dir(DIR *dir);
|
||||
/* Reads a directory listing entry */
|
||||
|
||||
int32_t mz_os_close_dir(DIR *dir);
|
||||
int32_t mz_os_close_dir(DIR *dir);
|
||||
/* Closes a directory that has been opened for listing */
|
||||
|
||||
int32_t mz_os_is_dir(const char *path);
|
||||
int32_t mz_os_is_dir(const char *path);
|
||||
/* Checks to see if path is a directory */
|
||||
|
||||
int32_t mz_os_is_symlink(const char *path);
|
||||
int32_t mz_os_is_symlink(const char *path);
|
||||
/* Checks to see if path is a symbolic link */
|
||||
|
||||
int32_t mz_os_make_symlink(const char *path, const char *target_path);
|
||||
int32_t mz_os_make_symlink(const char *path, const char *target_path);
|
||||
/* Creates a symbolic link pointing to a target */
|
||||
|
||||
int32_t mz_os_read_symlink(const char *path, char *target_path, int32_t max_target_path);
|
||||
int32_t mz_os_read_symlink(const char *path, char *target_path, int32_t max_target_path);
|
||||
/* Gets the target path for a symbolic link */
|
||||
|
||||
uint64_t mz_os_ms_time(void);
|
||||
|
@ -15,7 +15,7 @@
|
||||
#include <stdio.h> /* rename */
|
||||
#include <errno.h>
|
||||
#if defined(HAVE_ICONV)
|
||||
#include <iconv.h>
|
||||
# include <iconv.h>
|
||||
#endif
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
@ -75,8 +75,7 @@ char *mz_os_utf8_string_create(const char *string, int32_t encoding) {
|
||||
string_utf8_ptr = string_utf8;
|
||||
|
||||
if (string_utf8) {
|
||||
result = iconv(cd, (char **)&string, &string_length,
|
||||
(char **)&string_utf8_ptr, &string_utf8_size);
|
||||
result = iconv(cd, (char **)&string, &string_length, (char **)&string_utf8_ptr, &string_utf8_size);
|
||||
}
|
||||
|
||||
iconv_close(cd);
|
||||
@ -147,7 +146,7 @@ int32_t mz_os_rand(uint8_t *buf, int32_t size) {
|
||||
|
||||
/* Ensure different random header each time */
|
||||
if (++calls == 1) {
|
||||
#define PI_SEED 3141592654UL
|
||||
# define PI_SEED 3141592654UL
|
||||
srand((unsigned)(time(NULL) ^ PI_SEED));
|
||||
}
|
||||
|
||||
@ -272,11 +271,11 @@ int32_t mz_os_make_dir(const char *path) {
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
DIR* mz_os_open_dir(const char *path) {
|
||||
DIR *mz_os_open_dir(const char *path) {
|
||||
return opendir(path);
|
||||
}
|
||||
|
||||
struct dirent* mz_os_read_dir(DIR *dir) {
|
||||
struct dirent *mz_os_read_dir(DIR *dir) {
|
||||
if (!dir)
|
||||
return NULL;
|
||||
return readdir(dir);
|
||||
|
@ -28,10 +28,10 @@
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct DIR_int_s {
|
||||
void *find_handle;
|
||||
void *find_handle;
|
||||
WIN32_FIND_DATAW find_data;
|
||||
struct dirent entry;
|
||||
uint8_t end;
|
||||
struct dirent entry;
|
||||
uint8_t end;
|
||||
} DIR_int;
|
||||
|
||||
/***************************************************************************/
|
||||
@ -234,8 +234,7 @@ static void mz_os_unix_to_file_time(time_t unix_time, FILETIME *file_time) {
|
||||
}
|
||||
|
||||
int32_t mz_os_get_file_date(const char *path, time_t *modified_date, time_t *accessed_date, time_t *creation_date) {
|
||||
WIN32_FIND_DATAW ff32;
|
||||
HANDLE handle = NULL;
|
||||
WIN32_FILE_ATTRIBUTE_DATA wfad;
|
||||
wchar_t *path_wide = NULL;
|
||||
int32_t err = MZ_INTERNAL_ERROR;
|
||||
|
||||
@ -245,21 +244,19 @@ int32_t mz_os_get_file_date(const char *path, time_t *modified_date, time_t *acc
|
||||
if (!path_wide)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
handle = FindFirstFileW(path_wide, &ff32);
|
||||
free(path_wide);
|
||||
|
||||
if (handle != INVALID_HANDLE_VALUE) {
|
||||
if (GetFileAttributesExW(path_wide, GetFileExInfoStandard, &wfad)) {
|
||||
if (modified_date)
|
||||
mz_os_file_to_unix_time(ff32.ftLastWriteTime, modified_date);
|
||||
mz_os_file_to_unix_time(wfad.ftLastWriteTime, modified_date);
|
||||
if (accessed_date)
|
||||
mz_os_file_to_unix_time(ff32.ftLastAccessTime, accessed_date);
|
||||
mz_os_file_to_unix_time(wfad.ftLastAccessTime, accessed_date);
|
||||
if (creation_date)
|
||||
mz_os_file_to_unix_time(ff32.ftCreationTime, creation_date);
|
||||
mz_os_file_to_unix_time(wfad.ftCreationTime, creation_date);
|
||||
|
||||
FindClose(handle);
|
||||
err = MZ_OK;
|
||||
}
|
||||
|
||||
free(path_wide);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
@ -401,7 +398,7 @@ DIR *mz_os_open_dir(const char *path) {
|
||||
return (DIR *)dir_int;
|
||||
}
|
||||
|
||||
struct dirent* mz_os_read_dir(DIR *dir) {
|
||||
struct dirent *mz_os_read_dir(DIR *dir) {
|
||||
DIR_int *dir_int;
|
||||
|
||||
if (!dir)
|
||||
@ -411,8 +408,8 @@ struct dirent* mz_os_read_dir(DIR *dir) {
|
||||
if (dir_int->end)
|
||||
return NULL;
|
||||
|
||||
WideCharToMultiByte(CP_UTF8, 0, dir_int->find_data.cFileName, -1,
|
||||
dir_int->entry.d_name, sizeof(dir_int->entry.d_name), NULL, NULL);
|
||||
WideCharToMultiByte(CP_UTF8, 0, dir_int->find_data.cFileName, -1, dir_int->entry.d_name,
|
||||
sizeof(dir_int->entry.d_name), NULL, NULL);
|
||||
|
||||
if (FindNextFileW(dir_int->find_handle, &dir_int->find_data) == 0) {
|
||||
if (GetLastError() != ERROR_NO_MORE_FILES)
|
||||
@ -480,7 +477,7 @@ int32_t mz_os_is_symlink(const char *path) {
|
||||
}
|
||||
|
||||
int32_t mz_os_make_symlink(const char *path, const char *target_path) {
|
||||
typedef BOOLEAN (WINAPI *LPCREATESYMBOLICLINKW)(LPCWSTR, LPCWSTR, DWORD);
|
||||
typedef BOOLEAN(WINAPI * LPCREATESYMBOLICLINKW)(LPCWSTR, LPCWSTR, DWORD);
|
||||
MEMORY_BASIC_INFORMATION mbi;
|
||||
LPCREATESYMBOLICLINKW create_symbolic_link_w = NULL;
|
||||
HMODULE kernel32_mod = NULL;
|
||||
@ -492,7 +489,7 @@ int32_t mz_os_make_symlink(const char *path, const char *target_path) {
|
||||
if (!path)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
// Use VirtualQuery instead of GetModuleHandleW for UWP
|
||||
/* Use VirtualQuery instead of GetModuleHandleW for UWP */
|
||||
memset(&mbi, 0, sizeof(mbi));
|
||||
VirtualQuery(VirtualQuery, &mbi, sizeof(mbi));
|
||||
kernel32_mod = (HMODULE)mbi.AllocationBase;
|
||||
@ -530,7 +527,7 @@ int32_t mz_os_make_symlink(const char *path, const char *target_path) {
|
||||
|
||||
int32_t mz_os_read_symlink(const char *path, char *target_path, int32_t max_target_path) {
|
||||
typedef struct _REPARSE_DATA_BUFFER {
|
||||
ULONG ReparseTag;
|
||||
ULONG ReparseTag;
|
||||
USHORT ReparseDataLength;
|
||||
USHORT Reserved;
|
||||
union {
|
||||
@ -539,18 +536,18 @@ int32_t mz_os_read_symlink(const char *path, char *target_path, int32_t max_targ
|
||||
USHORT SubstituteNameLength;
|
||||
USHORT PrintNameOffset;
|
||||
USHORT PrintNameLength;
|
||||
ULONG Flags;
|
||||
WCHAR PathBuffer[1];
|
||||
ULONG Flags;
|
||||
WCHAR PathBuffer[1];
|
||||
} SymbolicLinkReparseBuffer;
|
||||
struct {
|
||||
USHORT SubstituteNameOffset;
|
||||
USHORT SubstituteNameLength;
|
||||
USHORT PrintNameOffset;
|
||||
USHORT PrintNameLength;
|
||||
WCHAR PathBuffer[1];
|
||||
WCHAR PathBuffer[1];
|
||||
} MountPointReparseBuffer;
|
||||
struct {
|
||||
UCHAR DataBuffer[1];
|
||||
UCHAR DataBuffer[1];
|
||||
} GenericReparseBuffer;
|
||||
};
|
||||
} REPARSE_DATA_BUFFER;
|
||||
@ -563,7 +560,7 @@ int32_t mz_os_read_symlink(const char *path, char *target_path, int32_t max_targ
|
||||
int32_t target_path_len = 0;
|
||||
int32_t target_path_idx = 0;
|
||||
int32_t err = MZ_OK;
|
||||
char *target_path_utf8 = NULL;
|
||||
char *target_path_utf8 = NULL;
|
||||
|
||||
if (!path)
|
||||
return MZ_PARAM_ERROR;
|
||||
@ -583,7 +580,7 @@ int32_t mz_os_read_symlink(const char *path, char *target_path, int32_t max_targ
|
||||
handle = CreateFile2(path_wide, FILE_READ_EA, FILE_SHARE_READ | FILE_SHARE_WRITE, OPEN_EXISTING, &extended_params);
|
||||
#else
|
||||
handle = CreateFileW(path_wide, FILE_READ_EA, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
|
||||
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, NULL);
|
||||
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, NULL);
|
||||
#endif
|
||||
|
||||
if (handle == INVALID_HANDLE_VALUE) {
|
||||
@ -593,8 +590,7 @@ int32_t mz_os_read_symlink(const char *path, char *target_path, int32_t max_targ
|
||||
|
||||
if (DeviceIoControl(handle, FSCTL_GET_REPARSE_POINT, NULL, 0, buffer, sizeof(buffer), &length, NULL) == TRUE) {
|
||||
reparse_data = (REPARSE_DATA_BUFFER *)buffer;
|
||||
if ((IsReparseTagMicrosoft(reparse_data->ReparseTag)) &&
|
||||
(reparse_data->ReparseTag == IO_REPARSE_TAG_SYMLINK)) {
|
||||
if ((IsReparseTagMicrosoft(reparse_data->ReparseTag)) && (reparse_data->ReparseTag == IO_REPARSE_TAG_SYMLINK)) {
|
||||
target_path_len = max_target_path * sizeof(wchar_t);
|
||||
if (target_path_len > reparse_data->SymbolicLinkReparseBuffer.PrintNameLength)
|
||||
target_path_len = reparse_data->SymbolicLinkReparseBuffer.PrintNameLength;
|
||||
@ -603,7 +599,7 @@ int32_t mz_os_read_symlink(const char *path, char *target_path, int32_t max_targ
|
||||
if (target_path_wide) {
|
||||
target_path_idx = reparse_data->SymbolicLinkReparseBuffer.PrintNameOffset / sizeof(wchar_t);
|
||||
memcpy(target_path_wide, &reparse_data->SymbolicLinkReparseBuffer.PathBuffer[target_path_idx],
|
||||
target_path_len);
|
||||
target_path_len);
|
||||
|
||||
target_path_wide[target_path_len / sizeof(wchar_t)] = 0;
|
||||
target_path_utf8 = mz_os_utf8_string_create_from_unicode(target_path_wide, MZ_ENCODING_UTF8);
|
||||
|
39
mz_strm.c
39
mz_strm.c
@ -158,8 +158,8 @@ int32_t mz_stream_copy_to_end(void *target, void *source) {
|
||||
return mz_stream_copy_stream_to_end(target, NULL, source, NULL);
|
||||
}
|
||||
|
||||
int32_t mz_stream_copy_stream(void *target, mz_stream_write_cb write_cb, void *source,
|
||||
mz_stream_read_cb read_cb, int32_t len) {
|
||||
int32_t mz_stream_copy_stream(void *target, mz_stream_write_cb write_cb, void *source, mz_stream_read_cb read_cb,
|
||||
int32_t len) {
|
||||
uint8_t buf[16384];
|
||||
int32_t bytes_to_copy = 0;
|
||||
int32_t read = 0;
|
||||
@ -187,7 +187,7 @@ int32_t mz_stream_copy_stream(void *target, mz_stream_write_cb write_cb, void *s
|
||||
}
|
||||
|
||||
int32_t mz_stream_copy_stream_to_end(void *target, mz_stream_write_cb write_cb, void *source,
|
||||
mz_stream_read_cb read_cb) {
|
||||
mz_stream_read_cb read_cb) {
|
||||
uint8_t buf[16384];
|
||||
int32_t read = 0;
|
||||
int32_t written = 0;
|
||||
@ -253,8 +253,10 @@ int32_t mz_stream_find(void *stream, const void *find, int32_t find_size, int64_
|
||||
start_pos = mz_stream_tell(stream);
|
||||
|
||||
while (read_pos < max_seek) {
|
||||
if (read_size > (int32_t)(max_seek - read_pos - buf_pos) && (max_seek - read_pos - buf_pos) < (int64_t)sizeof(buf))
|
||||
if (read_size > (int32_t)(max_seek - read_pos - buf_pos) &&
|
||||
(max_seek - read_pos - buf_pos) < (int64_t)sizeof(buf)) {
|
||||
read_size = (int32_t)(max_seek - read_pos - buf_pos);
|
||||
}
|
||||
|
||||
read = mz_stream_read(stream, buf + buf_pos, read_size);
|
||||
if ((read <= 0) || (read + buf_pos < find_size))
|
||||
@ -376,7 +378,7 @@ int32_t mz_stream_set_base(void *stream, void *base) {
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
void* mz_stream_get_interface(void *stream) {
|
||||
void *mz_stream_get_interface(void *stream) {
|
||||
mz_stream *strm = (mz_stream *)stream;
|
||||
if (!strm || !strm->vtbl)
|
||||
return NULL;
|
||||
@ -416,10 +418,10 @@ void mz_stream_delete(void **stream) {
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct mz_stream_raw_s {
|
||||
mz_stream stream;
|
||||
int64_t total_in;
|
||||
int64_t total_out;
|
||||
int64_t max_total_in;
|
||||
mz_stream stream;
|
||||
int64_t total_in;
|
||||
int64_t total_out;
|
||||
int64_t max_total_in;
|
||||
} mz_stream_raw;
|
||||
|
||||
/***************************************************************************/
|
||||
@ -517,19 +519,9 @@ int32_t mz_stream_raw_set_prop_int64(void *stream, int32_t prop, int64_t value)
|
||||
/***************************************************************************/
|
||||
|
||||
static mz_stream_vtbl mz_stream_raw_vtbl = {
|
||||
mz_stream_raw_open,
|
||||
mz_stream_raw_is_open,
|
||||
mz_stream_raw_read,
|
||||
mz_stream_raw_write,
|
||||
mz_stream_raw_tell,
|
||||
mz_stream_raw_seek,
|
||||
mz_stream_raw_close,
|
||||
mz_stream_raw_error,
|
||||
mz_stream_raw_create,
|
||||
mz_stream_raw_delete,
|
||||
mz_stream_raw_get_prop_int64,
|
||||
mz_stream_raw_set_prop_int64
|
||||
};
|
||||
mz_stream_raw_open, mz_stream_raw_is_open, mz_stream_raw_read, mz_stream_raw_write,
|
||||
mz_stream_raw_tell, mz_stream_raw_seek, mz_stream_raw_close, mz_stream_raw_error,
|
||||
mz_stream_raw_create, mz_stream_raw_delete, mz_stream_raw_get_prop_int64, mz_stream_raw_set_prop_int64};
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
@ -545,7 +537,6 @@ void mz_stream_raw_delete(void **stream) {
|
||||
if (!stream)
|
||||
return;
|
||||
raw = (mz_stream_raw *)*stream;
|
||||
if (raw)
|
||||
free(raw);
|
||||
free(raw);
|
||||
*stream = NULL;
|
||||
}
|
||||
|
90
mz_strm.h
90
mz_strm.h
@ -17,58 +17,58 @@ extern "C" {
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#define MZ_STREAM_PROP_TOTAL_IN (1)
|
||||
#define MZ_STREAM_PROP_TOTAL_IN_MAX (2)
|
||||
#define MZ_STREAM_PROP_TOTAL_OUT (3)
|
||||
#define MZ_STREAM_PROP_TOTAL_OUT_MAX (4)
|
||||
#define MZ_STREAM_PROP_HEADER_SIZE (5)
|
||||
#define MZ_STREAM_PROP_FOOTER_SIZE (6)
|
||||
#define MZ_STREAM_PROP_DISK_SIZE (7)
|
||||
#define MZ_STREAM_PROP_DISK_NUMBER (8)
|
||||
#define MZ_STREAM_PROP_COMPRESS_LEVEL (9)
|
||||
#define MZ_STREAM_PROP_COMPRESS_METHOD (10)
|
||||
#define MZ_STREAM_PROP_COMPRESS_WINDOW (11)
|
||||
#define MZ_STREAM_PROP_TOTAL_IN (1)
|
||||
#define MZ_STREAM_PROP_TOTAL_IN_MAX (2)
|
||||
#define MZ_STREAM_PROP_TOTAL_OUT (3)
|
||||
#define MZ_STREAM_PROP_TOTAL_OUT_MAX (4)
|
||||
#define MZ_STREAM_PROP_HEADER_SIZE (5)
|
||||
#define MZ_STREAM_PROP_FOOTER_SIZE (6)
|
||||
#define MZ_STREAM_PROP_DISK_SIZE (7)
|
||||
#define MZ_STREAM_PROP_DISK_NUMBER (8)
|
||||
#define MZ_STREAM_PROP_COMPRESS_LEVEL (9)
|
||||
#define MZ_STREAM_PROP_COMPRESS_METHOD (10)
|
||||
#define MZ_STREAM_PROP_COMPRESS_WINDOW (11)
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef int32_t (*mz_stream_open_cb) (void *stream, const char *path, int32_t mode);
|
||||
typedef int32_t (*mz_stream_is_open_cb) (void *stream);
|
||||
typedef int32_t (*mz_stream_read_cb) (void *stream, void *buf, int32_t size);
|
||||
typedef int32_t (*mz_stream_write_cb) (void *stream, const void *buf, int32_t size);
|
||||
typedef int64_t (*mz_stream_tell_cb) (void *stream);
|
||||
typedef int32_t (*mz_stream_seek_cb) (void *stream, int64_t offset, int32_t origin);
|
||||
typedef int32_t (*mz_stream_close_cb) (void *stream);
|
||||
typedef int32_t (*mz_stream_error_cb) (void *stream);
|
||||
typedef void* (*mz_stream_create_cb) (void);
|
||||
typedef void (*mz_stream_destroy_cb) (void **stream);
|
||||
typedef int32_t (*mz_stream_open_cb)(void *stream, const char *path, int32_t mode);
|
||||
typedef int32_t (*mz_stream_is_open_cb)(void *stream);
|
||||
typedef int32_t (*mz_stream_read_cb)(void *stream, void *buf, int32_t size);
|
||||
typedef int32_t (*mz_stream_write_cb)(void *stream, const void *buf, int32_t size);
|
||||
typedef int64_t (*mz_stream_tell_cb)(void *stream);
|
||||
typedef int32_t (*mz_stream_seek_cb)(void *stream, int64_t offset, int32_t origin);
|
||||
typedef int32_t (*mz_stream_close_cb)(void *stream);
|
||||
typedef int32_t (*mz_stream_error_cb)(void *stream);
|
||||
typedef void *(*mz_stream_create_cb)(void);
|
||||
typedef void (*mz_stream_destroy_cb)(void **stream);
|
||||
|
||||
typedef int32_t (*mz_stream_get_prop_int64_cb) (void *stream, int32_t prop, int64_t *value);
|
||||
typedef int32_t (*mz_stream_set_prop_int64_cb) (void *stream, int32_t prop, int64_t value);
|
||||
typedef int32_t (*mz_stream_get_prop_int64_cb)(void *stream, int32_t prop, int64_t *value);
|
||||
typedef int32_t (*mz_stream_set_prop_int64_cb)(void *stream, int32_t prop, int64_t value);
|
||||
|
||||
typedef int32_t (*mz_stream_find_cb) (void *stream, const void *find, int32_t find_size,
|
||||
int64_t max_seek, int64_t *position);
|
||||
typedef int32_t (*mz_stream_find_cb)(void *stream, const void *find, int32_t find_size, int64_t max_seek,
|
||||
int64_t *position);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct mz_stream_vtbl_s {
|
||||
mz_stream_open_cb open;
|
||||
mz_stream_is_open_cb is_open;
|
||||
mz_stream_read_cb read;
|
||||
mz_stream_write_cb write;
|
||||
mz_stream_tell_cb tell;
|
||||
mz_stream_seek_cb seek;
|
||||
mz_stream_close_cb close;
|
||||
mz_stream_error_cb error;
|
||||
mz_stream_create_cb create;
|
||||
mz_stream_destroy_cb destroy;
|
||||
mz_stream_open_cb open;
|
||||
mz_stream_is_open_cb is_open;
|
||||
mz_stream_read_cb read;
|
||||
mz_stream_write_cb write;
|
||||
mz_stream_tell_cb tell;
|
||||
mz_stream_seek_cb seek;
|
||||
mz_stream_close_cb close;
|
||||
mz_stream_error_cb error;
|
||||
mz_stream_create_cb create;
|
||||
mz_stream_destroy_cb destroy;
|
||||
|
||||
mz_stream_get_prop_int64_cb get_prop_int64;
|
||||
mz_stream_set_prop_int64_cb set_prop_int64;
|
||||
} mz_stream_vtbl;
|
||||
|
||||
typedef struct mz_stream_s {
|
||||
mz_stream_vtbl *vtbl;
|
||||
struct mz_stream_s *base;
|
||||
mz_stream_vtbl *vtbl;
|
||||
struct mz_stream_s *base;
|
||||
} mz_stream;
|
||||
|
||||
/***************************************************************************/
|
||||
@ -89,8 +89,10 @@ int32_t mz_stream_write_int64(void *stream, int64_t value);
|
||||
int32_t mz_stream_write_uint64(void *stream, uint64_t value);
|
||||
int32_t mz_stream_copy(void *target, void *source, int32_t len);
|
||||
int32_t mz_stream_copy_to_end(void *target, void *source);
|
||||
int32_t mz_stream_copy_stream(void *target, mz_stream_write_cb write_cb, void *source, mz_stream_read_cb read_cb, int32_t len);
|
||||
int32_t mz_stream_copy_stream_to_end(void *target, mz_stream_write_cb write_cb, void *source, mz_stream_read_cb read_cb);
|
||||
int32_t mz_stream_copy_stream(void *target, mz_stream_write_cb write_cb, void *source, mz_stream_read_cb read_cb,
|
||||
int32_t len);
|
||||
int32_t mz_stream_copy_stream_to_end(void *target, mz_stream_write_cb write_cb, void *source,
|
||||
mz_stream_read_cb read_cb);
|
||||
int64_t mz_stream_tell(void *stream);
|
||||
int32_t mz_stream_seek(void *stream, int64_t offset, int32_t origin);
|
||||
int32_t mz_stream_find(void *stream, const void *find, int32_t find_size, int64_t max_seek, int64_t *position);
|
||||
@ -99,12 +101,12 @@ int32_t mz_stream_close(void *stream);
|
||||
int32_t mz_stream_error(void *stream);
|
||||
|
||||
int32_t mz_stream_set_base(void *stream, void *base);
|
||||
void* mz_stream_get_interface(void *stream);
|
||||
void *mz_stream_get_interface(void *stream);
|
||||
int32_t mz_stream_get_prop_int64(void *stream, int32_t prop, int64_t *value);
|
||||
int32_t mz_stream_set_prop_int64(void *stream, int32_t prop, int64_t value);
|
||||
|
||||
void* mz_stream_create(mz_stream_vtbl *vtbl);
|
||||
void mz_stream_delete(void **stream);
|
||||
void *mz_stream_create(mz_stream_vtbl *vtbl);
|
||||
void mz_stream_delete(void **stream);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
@ -120,8 +122,8 @@ int32_t mz_stream_raw_error(void *stream);
|
||||
int32_t mz_stream_raw_get_prop_int64(void *stream, int32_t prop, int64_t *value);
|
||||
int32_t mz_stream_raw_set_prop_int64(void *stream, int32_t prop, int64_t value);
|
||||
|
||||
void* mz_stream_raw_create(void);
|
||||
void mz_stream_raw_delete(void **stream);
|
||||
void *mz_stream_raw_create(void);
|
||||
void mz_stream_raw_delete(void **stream);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
|
103
mz_strm_buf.c
103
mz_strm_buf.c
@ -16,37 +16,35 @@
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
static mz_stream_vtbl mz_stream_buffered_vtbl = {
|
||||
mz_stream_buffered_open,
|
||||
mz_stream_buffered_is_open,
|
||||
mz_stream_buffered_read,
|
||||
mz_stream_buffered_write,
|
||||
mz_stream_buffered_tell,
|
||||
mz_stream_buffered_seek,
|
||||
mz_stream_buffered_close,
|
||||
mz_stream_buffered_error,
|
||||
mz_stream_buffered_create,
|
||||
mz_stream_buffered_delete,
|
||||
NULL,
|
||||
NULL
|
||||
};
|
||||
static mz_stream_vtbl mz_stream_buffered_vtbl = {mz_stream_buffered_open,
|
||||
mz_stream_buffered_is_open,
|
||||
mz_stream_buffered_read,
|
||||
mz_stream_buffered_write,
|
||||
mz_stream_buffered_tell,
|
||||
mz_stream_buffered_seek,
|
||||
mz_stream_buffered_close,
|
||||
mz_stream_buffered_error,
|
||||
mz_stream_buffered_create,
|
||||
mz_stream_buffered_delete,
|
||||
NULL,
|
||||
NULL};
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct mz_stream_buffered_s {
|
||||
mz_stream stream;
|
||||
int32_t error;
|
||||
char readbuf[INT16_MAX];
|
||||
int32_t readbuf_len;
|
||||
int32_t readbuf_pos;
|
||||
int32_t readbuf_hits;
|
||||
int32_t readbuf_misses;
|
||||
char writebuf[INT16_MAX];
|
||||
int32_t writebuf_len;
|
||||
int32_t writebuf_pos;
|
||||
int32_t writebuf_hits;
|
||||
int32_t writebuf_misses;
|
||||
int64_t position;
|
||||
int32_t error;
|
||||
char readbuf[INT16_MAX];
|
||||
int32_t readbuf_len;
|
||||
int32_t readbuf_pos;
|
||||
int32_t readbuf_hits;
|
||||
int32_t readbuf_misses;
|
||||
char writebuf[INT16_MAX];
|
||||
int32_t writebuf_len;
|
||||
int32_t writebuf_pos;
|
||||
int32_t writebuf_hits;
|
||||
int32_t writebuf_misses;
|
||||
int64_t position;
|
||||
} mz_stream_buffered;
|
||||
|
||||
/***************************************************************************/
|
||||
@ -54,7 +52,7 @@ typedef struct mz_stream_buffered_s {
|
||||
#if 0
|
||||
# define mz_stream_buffered_print printf
|
||||
#else
|
||||
# define mz_stream_buffered_print(fmt,...)
|
||||
# define mz_stream_buffered_print(fmt, ...)
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
@ -93,16 +91,16 @@ static int32_t mz_stream_buffered_flush(void *stream, int32_t *written) {
|
||||
*written = 0;
|
||||
|
||||
while (bytes_left_to_write > 0) {
|
||||
bytes_written = mz_stream_write(buffered->stream.base,
|
||||
buffered->writebuf + (bytes_to_write - bytes_left_to_write), bytes_left_to_write);
|
||||
bytes_written = mz_stream_write(
|
||||
buffered->stream.base, buffered->writebuf + (bytes_to_write - bytes_left_to_write), bytes_left_to_write);
|
||||
|
||||
if (bytes_written != bytes_left_to_write)
|
||||
return MZ_WRITE_ERROR;
|
||||
|
||||
buffered->writebuf_misses += 1;
|
||||
|
||||
mz_stream_buffered_print("Buffered - Write flush (%" PRId32 ":%" PRId32 " len %" PRId32 ")\n",
|
||||
bytes_to_write, bytes_left_to_write, buffered->writebuf_len);
|
||||
mz_stream_buffered_print("Buffered - Write flush (%" PRId32 ":%" PRId32 " len %" PRId32 ")\n", bytes_to_write,
|
||||
bytes_left_to_write, buffered->writebuf_len);
|
||||
|
||||
total_bytes_written += bytes_written;
|
||||
bytes_left_to_write -= bytes_written;
|
||||
@ -128,7 +126,7 @@ int32_t mz_stream_buffered_read(void *stream, void *buf, int32_t size) {
|
||||
mz_stream_buffered_print("Buffered - Read (size %" PRId32 " pos %" PRId64 ")\n", size, buffered->position);
|
||||
|
||||
if (buffered->writebuf_len > 0) {
|
||||
int64_t position = buffered->position + buffered->writebuf_pos
|
||||
int64_t position = buffered->position + buffered->writebuf_pos;
|
||||
|
||||
mz_stream_buffered_print("Buffered - Switch from write to read, flushing (pos %" PRId64 ")\n", position);
|
||||
|
||||
@ -144,7 +142,8 @@ int32_t mz_stream_buffered_read(void *stream, void *buf, int32_t size) {
|
||||
}
|
||||
|
||||
bytes_to_read = (int32_t)sizeof(buffered->readbuf) - (buffered->readbuf_len - buffered->readbuf_pos);
|
||||
bytes_read = mz_stream_read(buffered->stream.base, buffered->readbuf + buffered->readbuf_pos, bytes_to_read);
|
||||
bytes_read =
|
||||
mz_stream_read(buffered->stream.base, buffered->readbuf + buffered->readbuf_pos, bytes_to_read);
|
||||
if (bytes_read < 0)
|
||||
return bytes_read;
|
||||
|
||||
@ -152,7 +151,8 @@ int32_t mz_stream_buffered_read(void *stream, void *buf, int32_t size) {
|
||||
buffered->readbuf_len += bytes_read;
|
||||
buffered->position += bytes_read;
|
||||
|
||||
mz_stream_buffered_print("Buffered - Filled (read %" PRId32 "/%" PRId32 " buf %" PRId32 ":%" PRId32 " pos %" PRId64 ")\n",
|
||||
mz_stream_buffered_print(
|
||||
"Buffered - Filled (read %" PRId32 "/%" PRId32 " buf %" PRId32 ":%" PRId32 " pos %" PRId64 ")\n",
|
||||
bytes_read, bytes_to_read, buffered->readbuf_pos, buffered->readbuf_len, buffered->position);
|
||||
|
||||
if (bytes_read == 0)
|
||||
@ -172,8 +172,10 @@ int32_t mz_stream_buffered_read(void *stream, void *buf, int32_t size) {
|
||||
buffered->readbuf_hits += 1;
|
||||
buffered->readbuf_pos += bytes_to_copy;
|
||||
|
||||
mz_stream_buffered_print("Buffered - Emptied (copied %" PRId32 " remaining %" PRId32 " buf %" PRId32 ":%" PRId32 " pos %" PRId64 ")\n",
|
||||
bytes_to_copy, bytes_left_to_read, buffered->readbuf_pos, buffered->readbuf_len, buffered->position);
|
||||
mz_stream_buffered_print("Buffered - Emptied (copied %" PRId32 " remaining %" PRId32 " buf %" PRId32
|
||||
":%" PRId32 " pos %" PRId64 ")\n",
|
||||
bytes_to_copy, bytes_left_to_read, buffered->readbuf_pos, buffered->readbuf_len,
|
||||
buffered->position);
|
||||
}
|
||||
}
|
||||
|
||||
@ -189,9 +191,8 @@ int32_t mz_stream_buffered_write(void *stream, const void *buf, int32_t size) {
|
||||
int32_t bytes_flushed = 0;
|
||||
int32_t err = MZ_OK;
|
||||
|
||||
|
||||
mz_stream_buffered_print("Buffered - Write (size %" PRId32 " len %" PRId32 " pos %" PRId64 ")\n",
|
||||
size, buffered->writebuf_len, buffered->position);
|
||||
mz_stream_buffered_print("Buffered - Write (size %" PRId32 " len %" PRId32 " pos %" PRId64 ")\n", size,
|
||||
buffered->writebuf_len, buffered->position);
|
||||
|
||||
if (buffered->readbuf_len > 0) {
|
||||
buffered->position -= buffered->readbuf_len;
|
||||
@ -225,11 +226,12 @@ int32_t mz_stream_buffered_write(void *stream, const void *buf, int32_t size) {
|
||||
continue;
|
||||
}
|
||||
|
||||
memcpy(buffered->writebuf + buffered->writebuf_pos,
|
||||
(const char *)buf + (bytes_to_write - bytes_left_to_write), bytes_to_copy);
|
||||
memcpy(buffered->writebuf + buffered->writebuf_pos, (const char *)buf + (bytes_to_write - bytes_left_to_write),
|
||||
bytes_to_copy);
|
||||
|
||||
mz_stream_buffered_print("Buffered - Write copy (remaining %" PRId32 " write %" PRId32 ":%" PRId32 " len %" PRId32 ")\n",
|
||||
bytes_to_copy, bytes_to_write, bytes_left_to_write, buffered->writebuf_len);
|
||||
mz_stream_buffered_print("Buffered - Write copy (remaining %" PRId32 " write %" PRId32 ":%" PRId32
|
||||
" len %" PRId32 ")\n",
|
||||
bytes_to_copy, bytes_to_write, bytes_left_to_write, buffered->writebuf_len);
|
||||
|
||||
bytes_left_to_write -= bytes_to_copy;
|
||||
|
||||
@ -249,7 +251,7 @@ int64_t mz_stream_buffered_tell(void *stream) {
|
||||
buffered->position = position;
|
||||
|
||||
mz_stream_buffered_print("Buffered - Tell (pos %" PRId64 " readpos %" PRId32 " writepos %" PRId32 ")\n",
|
||||
buffered->position, buffered->readbuf_pos, buffered->writebuf_pos);
|
||||
buffered->position, buffered->readbuf_pos, buffered->writebuf_pos);
|
||||
|
||||
if (buffered->readbuf_len > 0)
|
||||
position -= ((int64_t)buffered->readbuf_len - buffered->readbuf_pos);
|
||||
@ -263,8 +265,8 @@ int32_t mz_stream_buffered_seek(void *stream, int64_t offset, int32_t origin) {
|
||||
int32_t bytes_flushed = 0;
|
||||
int32_t err = MZ_OK;
|
||||
|
||||
mz_stream_buffered_print("Buffered - Seek (origin %" PRId32 " offset %" PRId64 " pos %" PRId64 ")\n",
|
||||
origin, offset, buffered->position);
|
||||
mz_stream_buffered_print("Buffered - Seek (origin %" PRId32 " offset %" PRId64 " pos %" PRId64 ")\n", origin,
|
||||
offset, buffered->position);
|
||||
|
||||
switch (origin) {
|
||||
case MZ_SEEK_SET:
|
||||
@ -337,12 +339,14 @@ int32_t mz_stream_buffered_close(void *stream) {
|
||||
mz_stream_buffered_print("Buffered - Close (flushed %" PRId32 ")\n", bytes_flushed);
|
||||
|
||||
if (buffered->readbuf_hits + buffered->readbuf_misses > 0) {
|
||||
mz_stream_buffered_print("Buffered - Read efficiency %.02f%%\n",
|
||||
mz_stream_buffered_print(
|
||||
"Buffered - Read efficiency %.02f%%\n",
|
||||
(buffered->readbuf_hits / ((float)buffered->readbuf_hits + buffered->readbuf_misses)) * 100);
|
||||
}
|
||||
|
||||
if (buffered->writebuf_hits + buffered->writebuf_misses > 0) {
|
||||
mz_stream_buffered_print("Buffered - Write efficiency %.02f%%\n",
|
||||
mz_stream_buffered_print(
|
||||
"Buffered - Write efficiency %.02f%%\n",
|
||||
(buffered->writebuf_hits / ((float)buffered->writebuf_hits + buffered->writebuf_misses)) * 100);
|
||||
}
|
||||
|
||||
@ -368,8 +372,7 @@ void mz_stream_buffered_delete(void **stream) {
|
||||
if (!stream)
|
||||
return;
|
||||
buffered = (mz_stream_buffered *)*stream;
|
||||
if (buffered)
|
||||
free(buffered);
|
||||
free(buffered);
|
||||
*stream = NULL;
|
||||
}
|
||||
|
||||
|
@ -28,10 +28,10 @@ int32_t mz_stream_buffered_seek(void *stream, int64_t offset, int32_t origin);
|
||||
int32_t mz_stream_buffered_close(void *stream);
|
||||
int32_t mz_stream_buffered_error(void *stream);
|
||||
|
||||
void* mz_stream_buffered_create(void);
|
||||
void mz_stream_buffered_delete(void **stream);
|
||||
void *mz_stream_buffered_create(void);
|
||||
void mz_stream_buffered_delete(void **stream);
|
||||
|
||||
void* mz_stream_buffered_get_interface(void);
|
||||
void *mz_stream_buffered_get_interface(void);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
|
@ -17,35 +17,25 @@
|
||||
/***************************************************************************/
|
||||
|
||||
static mz_stream_vtbl mz_stream_bzip_vtbl = {
|
||||
mz_stream_bzip_open,
|
||||
mz_stream_bzip_is_open,
|
||||
mz_stream_bzip_read,
|
||||
mz_stream_bzip_write,
|
||||
mz_stream_bzip_tell,
|
||||
mz_stream_bzip_seek,
|
||||
mz_stream_bzip_close,
|
||||
mz_stream_bzip_error,
|
||||
mz_stream_bzip_create,
|
||||
mz_stream_bzip_delete,
|
||||
mz_stream_bzip_get_prop_int64,
|
||||
mz_stream_bzip_set_prop_int64
|
||||
};
|
||||
mz_stream_bzip_open, mz_stream_bzip_is_open, mz_stream_bzip_read, mz_stream_bzip_write,
|
||||
mz_stream_bzip_tell, mz_stream_bzip_seek, mz_stream_bzip_close, mz_stream_bzip_error,
|
||||
mz_stream_bzip_create, mz_stream_bzip_delete, mz_stream_bzip_get_prop_int64, mz_stream_bzip_set_prop_int64};
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct mz_stream_bzip_s {
|
||||
mz_stream stream;
|
||||
bz_stream bzstream;
|
||||
int32_t mode;
|
||||
int32_t error;
|
||||
uint8_t buffer[INT16_MAX];
|
||||
int32_t buffer_len;
|
||||
int16_t stream_end;
|
||||
int64_t total_in;
|
||||
int64_t total_out;
|
||||
int64_t max_total_in;
|
||||
int8_t initialized;
|
||||
int16_t level;
|
||||
mz_stream stream;
|
||||
bz_stream bzstream;
|
||||
int32_t mode;
|
||||
int32_t error;
|
||||
uint8_t buffer[INT16_MAX];
|
||||
int32_t buffer_len;
|
||||
int16_t stream_end;
|
||||
int64_t total_in;
|
||||
int64_t total_out;
|
||||
int64_t max_total_in;
|
||||
int8_t initialized;
|
||||
int16_t level;
|
||||
} mz_stream_bzip;
|
||||
|
||||
/***************************************************************************/
|
||||
@ -114,7 +104,6 @@ int32_t mz_stream_bzip_read(void *stream, void *buf, int32_t size) {
|
||||
uint64_t total_out_before = 0;
|
||||
uint64_t total_in_after = 0;
|
||||
uint64_t total_out_after = 0;
|
||||
int32_t total_in = 0;
|
||||
int32_t total_out = 0;
|
||||
int32_t in_bytes = 0;
|
||||
int32_t out_bytes = 0;
|
||||
@ -145,19 +134,16 @@ int32_t mz_stream_bzip_read(void *stream, void *buf, int32_t size) {
|
||||
}
|
||||
|
||||
total_in_before = bzip->bzstream.avail_in;
|
||||
total_out_before = bzip->bzstream.total_out_lo32 +
|
||||
(((uint64_t)bzip->bzstream.total_out_hi32) << 32);
|
||||
total_out_before = bzip->bzstream.total_out_lo32 + (((uint64_t)bzip->bzstream.total_out_hi32) << 32);
|
||||
|
||||
err = BZ2_bzDecompress(&bzip->bzstream);
|
||||
|
||||
total_in_after = bzip->bzstream.avail_in;
|
||||
total_out_after = bzip->bzstream.total_out_lo32 +
|
||||
(((uint64_t)bzip->bzstream.total_out_hi32) << 32);
|
||||
total_out_after = bzip->bzstream.total_out_lo32 + (((uint64_t)bzip->bzstream.total_out_hi32) << 32);
|
||||
|
||||
in_bytes = (int32_t)(total_in_before - total_in_after);
|
||||
out_bytes = (int32_t)(total_out_after - total_out_before);
|
||||
|
||||
total_in += in_bytes;
|
||||
total_out += out_bytes;
|
||||
|
||||
bzip->total_in += in_bytes;
|
||||
@ -207,13 +193,11 @@ static int32_t mz_stream_bzip_compress(void *stream, int flush) {
|
||||
bzip->buffer_len = 0;
|
||||
}
|
||||
|
||||
total_out_before = bzip->bzstream.total_out_lo32 +
|
||||
(((uint64_t)bzip->bzstream.total_out_hi32) << 32);
|
||||
total_out_before = bzip->bzstream.total_out_lo32 + (((uint64_t)bzip->bzstream.total_out_hi32) << 32);
|
||||
|
||||
err = BZ2_bzCompress(&bzip->bzstream, flush);
|
||||
|
||||
total_out_after = bzip->bzstream.total_out_lo32 +
|
||||
(((uint64_t)bzip->bzstream.total_out_hi32) << 32);
|
||||
total_out_after = bzip->bzstream.total_out_lo32 + (((uint64_t)bzip->bzstream.total_out_hi32) << 32);
|
||||
|
||||
out_bytes = (uint32_t)(total_out_after - total_out_before);
|
||||
|
||||
@ -326,7 +310,7 @@ int32_t mz_stream_bzip_set_prop_int64(void *stream, int32_t prop, int64_t value)
|
||||
mz_stream_bzip *bzip = (mz_stream_bzip *)stream;
|
||||
switch (prop) {
|
||||
case MZ_STREAM_PROP_COMPRESS_LEVEL:
|
||||
if (value < 0)
|
||||
if (value == MZ_COMPRESS_LEVEL_DEFAULT)
|
||||
bzip->level = 6;
|
||||
else
|
||||
bzip->level = (int16_t)value;
|
||||
@ -352,8 +336,7 @@ void mz_stream_bzip_delete(void **stream) {
|
||||
if (!stream)
|
||||
return;
|
||||
bzip = (mz_stream_bzip *)*stream;
|
||||
if (bzip)
|
||||
free(bzip);
|
||||
free(bzip);
|
||||
*stream = NULL;
|
||||
}
|
||||
|
||||
|
@ -29,12 +29,12 @@ int32_t mz_stream_bzip_error(void *stream);
|
||||
int32_t mz_stream_bzip_get_prop_int64(void *stream, int32_t prop, int64_t *value);
|
||||
int32_t mz_stream_bzip_set_prop_int64(void *stream, int32_t prop, int64_t value);
|
||||
|
||||
void* mz_stream_bzip_create(void);
|
||||
void mz_stream_bzip_delete(void **stream);
|
||||
void *mz_stream_bzip_create(void);
|
||||
void mz_stream_bzip_delete(void **stream);
|
||||
|
||||
void* mz_stream_bzip_get_interface(void);
|
||||
void *mz_stream_bzip_get_interface(void);
|
||||
|
||||
void bz_internal_error(int errcode);
|
||||
void bz_internal_error(int errcode);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
|
@ -17,35 +17,25 @@
|
||||
/***************************************************************************/
|
||||
|
||||
static mz_stream_vtbl mz_stream_libcomp_vtbl = {
|
||||
mz_stream_libcomp_open,
|
||||
mz_stream_libcomp_is_open,
|
||||
mz_stream_libcomp_read,
|
||||
mz_stream_libcomp_write,
|
||||
mz_stream_libcomp_tell,
|
||||
mz_stream_libcomp_seek,
|
||||
mz_stream_libcomp_close,
|
||||
mz_stream_libcomp_error,
|
||||
mz_stream_libcomp_create,
|
||||
mz_stream_libcomp_delete,
|
||||
mz_stream_libcomp_get_prop_int64,
|
||||
mz_stream_libcomp_set_prop_int64
|
||||
};
|
||||
mz_stream_libcomp_open, mz_stream_libcomp_is_open, mz_stream_libcomp_read,
|
||||
mz_stream_libcomp_write, mz_stream_libcomp_tell, mz_stream_libcomp_seek,
|
||||
mz_stream_libcomp_close, mz_stream_libcomp_error, mz_stream_libcomp_create,
|
||||
mz_stream_libcomp_delete, mz_stream_libcomp_get_prop_int64, mz_stream_libcomp_set_prop_int64};
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct mz_stream_libcomp_s {
|
||||
mz_stream stream;
|
||||
compression_stream
|
||||
cstream;
|
||||
uint8_t buffer[INT16_MAX];
|
||||
int32_t buffer_len;
|
||||
int64_t total_in;
|
||||
int64_t total_out;
|
||||
int64_t max_total_in;
|
||||
int8_t initialized;
|
||||
int32_t mode;
|
||||
int32_t error;
|
||||
int16_t method;
|
||||
mz_stream stream;
|
||||
compression_stream cstream;
|
||||
uint8_t buffer[INT16_MAX];
|
||||
int32_t buffer_len;
|
||||
int64_t total_in;
|
||||
int64_t total_out;
|
||||
int64_t max_total_in;
|
||||
int8_t initialized;
|
||||
int32_t mode;
|
||||
int32_t error;
|
||||
int16_t method;
|
||||
} mz_stream_libcomp;
|
||||
|
||||
/***************************************************************************/
|
||||
@ -116,7 +106,6 @@ int32_t mz_stream_libcomp_read(void *stream, void *buf, int32_t size) {
|
||||
uint64_t total_in_after = 0;
|
||||
uint64_t total_out_before = 0;
|
||||
uint64_t total_out_after = 0;
|
||||
int32_t total_in = 0;
|
||||
int32_t total_out = 0;
|
||||
int32_t in_bytes = 0;
|
||||
int32_t out_bytes = 0;
|
||||
@ -161,7 +150,6 @@ int32_t mz_stream_libcomp_read(void *stream, void *buf, int32_t size) {
|
||||
in_bytes = (int32_t)(total_in_before - total_in_after);
|
||||
out_bytes = (int32_t)(total_out_before - total_out_after);
|
||||
|
||||
total_in += in_bytes;
|
||||
total_out += out_bytes;
|
||||
|
||||
libcomp->total_in += in_bytes;
|
||||
|
@ -31,10 +31,10 @@ int32_t mz_stream_libcomp_error(void *stream);
|
||||
int32_t mz_stream_libcomp_get_prop_int64(void *stream, int32_t prop, int64_t *value);
|
||||
int32_t mz_stream_libcomp_set_prop_int64(void *stream, int32_t prop, int64_t value);
|
||||
|
||||
void* mz_stream_libcomp_create(void);
|
||||
void mz_stream_libcomp_delete(void **stream);
|
||||
void *mz_stream_libcomp_create(void);
|
||||
void mz_stream_libcomp_delete(void **stream);
|
||||
|
||||
void* mz_stream_libcomp_get_interface(void);
|
||||
void *mz_stream_libcomp_get_interface(void);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
|
@ -23,38 +23,28 @@
|
||||
/***************************************************************************/
|
||||
|
||||
static mz_stream_vtbl mz_stream_lzma_vtbl = {
|
||||
mz_stream_lzma_open,
|
||||
mz_stream_lzma_is_open,
|
||||
mz_stream_lzma_read,
|
||||
mz_stream_lzma_write,
|
||||
mz_stream_lzma_tell,
|
||||
mz_stream_lzma_seek,
|
||||
mz_stream_lzma_close,
|
||||
mz_stream_lzma_error,
|
||||
mz_stream_lzma_create,
|
||||
mz_stream_lzma_delete,
|
||||
mz_stream_lzma_get_prop_int64,
|
||||
mz_stream_lzma_set_prop_int64
|
||||
};
|
||||
mz_stream_lzma_open, mz_stream_lzma_is_open, mz_stream_lzma_read, mz_stream_lzma_write,
|
||||
mz_stream_lzma_tell, mz_stream_lzma_seek, mz_stream_lzma_close, mz_stream_lzma_error,
|
||||
mz_stream_lzma_create, mz_stream_lzma_delete, mz_stream_lzma_get_prop_int64, mz_stream_lzma_set_prop_int64};
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct mz_stream_lzma_s {
|
||||
mz_stream stream;
|
||||
mz_stream stream;
|
||||
lzma_stream lstream;
|
||||
int32_t mode;
|
||||
int32_t error;
|
||||
uint8_t buffer[INT16_MAX];
|
||||
int32_t buffer_len;
|
||||
int64_t total_in;
|
||||
int64_t total_out;
|
||||
int64_t max_total_in;
|
||||
int64_t max_total_out;
|
||||
int8_t initialized;
|
||||
int8_t header;
|
||||
int32_t header_size;
|
||||
uint32_t preset;
|
||||
int16_t method;
|
||||
int32_t mode;
|
||||
int32_t error;
|
||||
uint8_t buffer[INT16_MAX];
|
||||
int32_t buffer_len;
|
||||
int64_t total_in;
|
||||
int64_t total_out;
|
||||
int64_t max_total_in;
|
||||
int64_t max_total_out;
|
||||
int8_t initialized;
|
||||
int8_t header;
|
||||
int32_t header_size;
|
||||
uint32_t preset;
|
||||
int16_t method;
|
||||
} mz_stream_lzma;
|
||||
|
||||
/***************************************************************************/
|
||||
@ -415,10 +405,10 @@ int32_t mz_stream_lzma_set_prop_int64(void *stream, int32_t prop, int64_t value)
|
||||
mz_stream_lzma *lzma = (mz_stream_lzma *)stream;
|
||||
switch (prop) {
|
||||
case MZ_STREAM_PROP_COMPRESS_LEVEL:
|
||||
if (value >= 9)
|
||||
lzma->preset = LZMA_PRESET_EXTREME;
|
||||
else
|
||||
if (value == MZ_COMPRESS_LEVEL_DEFAULT)
|
||||
lzma->preset = LZMA_PRESET_DEFAULT;
|
||||
else
|
||||
lzma->preset = (uint32_t)value;
|
||||
break;
|
||||
case MZ_STREAM_PROP_COMPRESS_METHOD:
|
||||
lzma->method = (int16_t)value;
|
||||
@ -453,8 +443,7 @@ void mz_stream_lzma_delete(void **stream) {
|
||||
if (!stream)
|
||||
return;
|
||||
lzma = (mz_stream_lzma *)*stream;
|
||||
if (lzma)
|
||||
free(lzma);
|
||||
free(lzma);
|
||||
*stream = NULL;
|
||||
}
|
||||
|
||||
|
@ -29,10 +29,10 @@ int32_t mz_stream_lzma_error(void *stream);
|
||||
int32_t mz_stream_lzma_get_prop_int64(void *stream, int32_t prop, int64_t *value);
|
||||
int32_t mz_stream_lzma_set_prop_int64(void *stream, int32_t prop, int64_t value);
|
||||
|
||||
void* mz_stream_lzma_create(void);
|
||||
void mz_stream_lzma_delete(void **stream);
|
||||
void *mz_stream_lzma_create(void);
|
||||
void mz_stream_lzma_delete(void **stream);
|
||||
|
||||
void* mz_stream_lzma_get_interface(void);
|
||||
void *mz_stream_lzma_get_interface(void);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
|
@ -22,31 +22,29 @@
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
static mz_stream_vtbl mz_stream_mem_vtbl = {
|
||||
mz_stream_mem_open,
|
||||
mz_stream_mem_is_open,
|
||||
mz_stream_mem_read,
|
||||
mz_stream_mem_write,
|
||||
mz_stream_mem_tell,
|
||||
mz_stream_mem_seek,
|
||||
mz_stream_mem_close,
|
||||
mz_stream_mem_error,
|
||||
mz_stream_mem_create,
|
||||
mz_stream_mem_delete,
|
||||
NULL,
|
||||
NULL
|
||||
};
|
||||
static mz_stream_vtbl mz_stream_mem_vtbl = {mz_stream_mem_open,
|
||||
mz_stream_mem_is_open,
|
||||
mz_stream_mem_read,
|
||||
mz_stream_mem_write,
|
||||
mz_stream_mem_tell,
|
||||
mz_stream_mem_seek,
|
||||
mz_stream_mem_close,
|
||||
mz_stream_mem_error,
|
||||
mz_stream_mem_create,
|
||||
mz_stream_mem_delete,
|
||||
NULL,
|
||||
NULL};
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct mz_stream_mem_s {
|
||||
mz_stream stream;
|
||||
int32_t mode;
|
||||
uint8_t *buffer; /* Memory buffer pointer */
|
||||
int32_t size; /* Size of the memory buffer */
|
||||
int32_t limit; /* Furthest we've written */
|
||||
int32_t position; /* Current position in the memory */
|
||||
int32_t grow_size; /* Size to grow when full */
|
||||
mz_stream stream;
|
||||
int32_t mode;
|
||||
uint8_t *buffer; /* Memory buffer pointer */
|
||||
int32_t size; /* Size of the memory buffer */
|
||||
int32_t limit; /* Furthest we've written */
|
||||
int32_t position; /* Current position in the memory */
|
||||
int32_t grow_size; /* Size to grow when full */
|
||||
} mz_stream_mem;
|
||||
|
||||
/***************************************************************************/
|
||||
@ -211,7 +209,7 @@ int32_t mz_stream_mem_get_buffer(void *stream, const void **buf) {
|
||||
|
||||
int32_t mz_stream_mem_get_buffer_at(void *stream, int64_t position, const void **buf) {
|
||||
mz_stream_mem *mem = (mz_stream_mem *)stream;
|
||||
if (!buf || position < 0 || !mem->buffer|| mem->size < position)
|
||||
if (!buf || position < 0 || !mem->buffer || mem->size < position)
|
||||
return MZ_SEEK_ERROR;
|
||||
*buf = mem->buffer + position;
|
||||
return MZ_OK;
|
||||
|
@ -26,18 +26,18 @@ int32_t mz_stream_mem_seek(void *stream, int64_t offset, int32_t origin);
|
||||
int32_t mz_stream_mem_close(void *stream);
|
||||
int32_t mz_stream_mem_error(void *stream);
|
||||
|
||||
void mz_stream_mem_set_buffer(void *stream, void *buf, int32_t size);
|
||||
void mz_stream_mem_set_buffer(void *stream, void *buf, int32_t size);
|
||||
int32_t mz_stream_mem_get_buffer(void *stream, const void **buf);
|
||||
int32_t mz_stream_mem_get_buffer_at(void *stream, int64_t position, const void **buf);
|
||||
int32_t mz_stream_mem_get_buffer_at_current(void *stream, const void **buf);
|
||||
void mz_stream_mem_get_buffer_length(void *stream, int32_t *length);
|
||||
void mz_stream_mem_set_buffer_limit(void *stream, int32_t limit);
|
||||
void mz_stream_mem_set_grow_size(void *stream, int32_t grow_size);
|
||||
void mz_stream_mem_get_buffer_length(void *stream, int32_t *length);
|
||||
void mz_stream_mem_set_buffer_limit(void *stream, int32_t limit);
|
||||
void mz_stream_mem_set_grow_size(void *stream, int32_t grow_size);
|
||||
|
||||
void* mz_stream_mem_create(void);
|
||||
void mz_stream_mem_delete(void **stream);
|
||||
void *mz_stream_mem_create(void);
|
||||
void mz_stream_mem_delete(void **stream);
|
||||
|
||||
void* mz_stream_mem_get_interface(void);
|
||||
void *mz_stream_mem_get_interface(void);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
|
@ -26,10 +26,10 @@ int32_t mz_stream_os_seek(void *stream, int64_t offset, int32_t origin);
|
||||
int32_t mz_stream_os_close(void *stream);
|
||||
int32_t mz_stream_os_error(void *stream);
|
||||
|
||||
void* mz_stream_os_create(void);
|
||||
void mz_stream_os_delete(void **stream);
|
||||
void *mz_stream_os_create(void);
|
||||
void mz_stream_os_delete(void **stream);
|
||||
|
||||
void* mz_stream_os_get_interface(void);
|
||||
void *mz_stream_os_get_interface(void);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
|
@ -41,27 +41,25 @@
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
static mz_stream_vtbl mz_stream_os_vtbl = {
|
||||
mz_stream_os_open,
|
||||
mz_stream_os_is_open,
|
||||
mz_stream_os_read,
|
||||
mz_stream_os_write,
|
||||
mz_stream_os_tell,
|
||||
mz_stream_os_seek,
|
||||
mz_stream_os_close,
|
||||
mz_stream_os_error,
|
||||
mz_stream_os_create,
|
||||
mz_stream_os_delete,
|
||||
NULL,
|
||||
NULL
|
||||
};
|
||||
static mz_stream_vtbl mz_stream_os_vtbl = {mz_stream_os_open,
|
||||
mz_stream_os_is_open,
|
||||
mz_stream_os_read,
|
||||
mz_stream_os_write,
|
||||
mz_stream_os_tell,
|
||||
mz_stream_os_seek,
|
||||
mz_stream_os_close,
|
||||
mz_stream_os_error,
|
||||
mz_stream_os_create,
|
||||
mz_stream_os_delete,
|
||||
NULL,
|
||||
NULL};
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct mz_stream_posix_s {
|
||||
mz_stream stream;
|
||||
int32_t error;
|
||||
FILE *handle;
|
||||
mz_stream stream;
|
||||
int32_t error;
|
||||
FILE *handle;
|
||||
} mz_stream_posix;
|
||||
|
||||
/***************************************************************************/
|
||||
@ -188,8 +186,7 @@ void mz_stream_os_delete(void **stream) {
|
||||
if (!stream)
|
||||
return;
|
||||
posix = (mz_stream_posix *)*stream;
|
||||
if (posix)
|
||||
free(posix);
|
||||
free(posix);
|
||||
*stream = NULL;
|
||||
}
|
||||
|
||||
|
@ -27,7 +27,7 @@
|
||||
#endif
|
||||
|
||||
#ifndef INVALID_SET_FILE_POINTER
|
||||
# define INVALID_SET_FILE_POINTER (DWORD)-1
|
||||
# define INVALID_SET_FILE_POINTER (DWORD)(-1)
|
||||
#endif
|
||||
|
||||
#ifndef _WIN32_WINNT_WIN8
|
||||
@ -36,27 +36,25 @@
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
static mz_stream_vtbl mz_stream_os_vtbl = {
|
||||
mz_stream_os_open,
|
||||
mz_stream_os_is_open,
|
||||
mz_stream_os_read,
|
||||
mz_stream_os_write,
|
||||
mz_stream_os_tell,
|
||||
mz_stream_os_seek,
|
||||
mz_stream_os_close,
|
||||
mz_stream_os_error,
|
||||
mz_stream_os_create,
|
||||
mz_stream_os_delete,
|
||||
NULL,
|
||||
NULL
|
||||
};
|
||||
static mz_stream_vtbl mz_stream_os_vtbl = {mz_stream_os_open,
|
||||
mz_stream_os_is_open,
|
||||
mz_stream_os_read,
|
||||
mz_stream_os_write,
|
||||
mz_stream_os_tell,
|
||||
mz_stream_os_seek,
|
||||
mz_stream_os_close,
|
||||
mz_stream_os_error,
|
||||
mz_stream_os_create,
|
||||
mz_stream_os_delete,
|
||||
NULL,
|
||||
NULL};
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct mz_stream_win32_s {
|
||||
mz_stream stream;
|
||||
HANDLE handle;
|
||||
int32_t error;
|
||||
mz_stream stream;
|
||||
HANDLE handle;
|
||||
int32_t error;
|
||||
} mz_stream_win32;
|
||||
|
||||
/***************************************************************************/
|
||||
@ -103,11 +101,9 @@ int32_t mz_stream_os_open(void *stream, const char *path, int32_t mode) {
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
#if _WIN32_WINNT >= _WIN32_WINNT_WIN8
|
||||
win32->handle = CreateFile2(path_wide, desired_access, share_mode,
|
||||
creation_disposition, NULL);
|
||||
win32->handle = CreateFile2(path_wide, desired_access, share_mode, creation_disposition, NULL);
|
||||
#else
|
||||
win32->handle = CreateFileW(path_wide, desired_access, share_mode, NULL,
|
||||
creation_disposition, flags_attribs, NULL);
|
||||
win32->handle = CreateFileW(path_wide, desired_access, share_mode, NULL, creation_disposition, flags_attribs, NULL);
|
||||
#endif
|
||||
|
||||
mz_os_unicode_string_delete(&path_wide);
|
||||
@ -166,8 +162,8 @@ int32_t mz_stream_os_write(void *stream, const void *buf, int32_t size) {
|
||||
return written;
|
||||
}
|
||||
|
||||
static int32_t mz_stream_os_seekinternal(HANDLE handle, LARGE_INTEGER large_pos,
|
||||
LARGE_INTEGER *new_pos, uint32_t move_method) {
|
||||
static int32_t mz_stream_os_seekinternal(HANDLE handle, LARGE_INTEGER large_pos, LARGE_INTEGER *new_pos,
|
||||
uint32_t move_method) {
|
||||
#if _WIN32_WINNT >= _WIN32_WINNT_WINXP
|
||||
BOOL success = FALSE;
|
||||
success = SetFilePointerEx(handle, large_pos, new_pos, move_method);
|
||||
|
@ -29,45 +29,35 @@
|
||||
/***************************************************************************/
|
||||
|
||||
static mz_stream_vtbl mz_stream_pkcrypt_vtbl = {
|
||||
mz_stream_pkcrypt_open,
|
||||
mz_stream_pkcrypt_is_open,
|
||||
mz_stream_pkcrypt_read,
|
||||
mz_stream_pkcrypt_write,
|
||||
mz_stream_pkcrypt_tell,
|
||||
mz_stream_pkcrypt_seek,
|
||||
mz_stream_pkcrypt_close,
|
||||
mz_stream_pkcrypt_error,
|
||||
mz_stream_pkcrypt_create,
|
||||
mz_stream_pkcrypt_delete,
|
||||
mz_stream_pkcrypt_get_prop_int64,
|
||||
mz_stream_pkcrypt_set_prop_int64
|
||||
};
|
||||
mz_stream_pkcrypt_open, mz_stream_pkcrypt_is_open, mz_stream_pkcrypt_read,
|
||||
mz_stream_pkcrypt_write, mz_stream_pkcrypt_tell, mz_stream_pkcrypt_seek,
|
||||
mz_stream_pkcrypt_close, mz_stream_pkcrypt_error, mz_stream_pkcrypt_create,
|
||||
mz_stream_pkcrypt_delete, mz_stream_pkcrypt_get_prop_int64, mz_stream_pkcrypt_set_prop_int64};
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct mz_stream_pkcrypt_s {
|
||||
mz_stream stream;
|
||||
int32_t error;
|
||||
int16_t initialized;
|
||||
uint8_t buffer[UINT16_MAX];
|
||||
int64_t total_in;
|
||||
int64_t max_total_in;
|
||||
int64_t total_out;
|
||||
uint32_t keys[3]; /* keys defining the pseudo-random sequence */
|
||||
uint8_t verify1;
|
||||
uint8_t verify2;
|
||||
const char *password;
|
||||
mz_stream stream;
|
||||
int32_t error;
|
||||
int16_t initialized;
|
||||
uint8_t buffer[UINT16_MAX];
|
||||
int64_t total_in;
|
||||
int64_t max_total_in;
|
||||
int64_t total_out;
|
||||
uint32_t keys[3]; /* keys defining the pseudo-random sequence */
|
||||
uint8_t verify1;
|
||||
uint8_t verify2;
|
||||
uint16_t verify_version;
|
||||
const char *password;
|
||||
} mz_stream_pkcrypt;
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#define mz_stream_pkcrypt_decode(strm, c) \
|
||||
(mz_stream_pkcrypt_update_keys(strm, \
|
||||
c ^= mz_stream_pkcrypt_decrypt_byte(strm)))
|
||||
#define mz_stream_pkcrypt_decode(strm, c) \
|
||||
(mz_stream_pkcrypt_update_keys(strm, c ^= mz_stream_pkcrypt_decrypt_byte(strm)))
|
||||
|
||||
#define mz_stream_pkcrypt_encode(strm, c, t) \
|
||||
(t = mz_stream_pkcrypt_decrypt_byte(strm), \
|
||||
mz_stream_pkcrypt_update_keys(strm, (uint8_t)c), (uint8_t)(t^(c)))
|
||||
#define mz_stream_pkcrypt_encode(strm, c, t) \
|
||||
(t = mz_stream_pkcrypt_decrypt_byte(strm), mz_stream_pkcrypt_update_keys(strm, (uint8_t)c), (uint8_t)(t ^ (c)))
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
@ -161,10 +151,14 @@ int32_t mz_stream_pkcrypt_open(void *stream, const char *path, int32_t mode) {
|
||||
verify1 = mz_stream_pkcrypt_decode(stream, header[i++]);
|
||||
verify2 = mz_stream_pkcrypt_decode(stream, header[i++]);
|
||||
|
||||
/* Older versions used 2 byte check, newer versions use 1 byte check. */
|
||||
MZ_UNUSED(verify1);
|
||||
if ((verify2 != 0) && (verify2 != pkcrypt->verify2))
|
||||
/* PKZIP 2.0 and higher use 1 byte check, older versions used 2 byte check.
|
||||
See app note section 6.1.6. */
|
||||
if (verify2 != pkcrypt->verify2)
|
||||
return MZ_PASSWORD_ERROR;
|
||||
if (pkcrypt->verify_version < 2) {
|
||||
if (verify1 != pkcrypt->verify1)
|
||||
return MZ_PASSWORD_ERROR;
|
||||
}
|
||||
|
||||
pkcrypt->total_in += MZ_PKCRYPT_HEADER_SIZE;
|
||||
}
|
||||
@ -259,16 +253,18 @@ void mz_stream_pkcrypt_set_password(void *stream, const char *password) {
|
||||
pkcrypt->password = password;
|
||||
}
|
||||
|
||||
void mz_stream_pkcrypt_set_verify(void *stream, uint8_t verify1, uint8_t verify2) {
|
||||
void mz_stream_pkcrypt_set_verify(void *stream, uint8_t verify1, uint8_t verify2, uint16_t version) {
|
||||
mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
|
||||
pkcrypt->verify1 = verify1;
|
||||
pkcrypt->verify2 = verify2;
|
||||
pkcrypt->verify_version = version;
|
||||
}
|
||||
|
||||
void mz_stream_pkcrypt_get_verify(void *stream, uint8_t *verify1, uint8_t *verify2) {
|
||||
void mz_stream_pkcrypt_get_verify(void *stream, uint8_t *verify1, uint8_t *verify2, uint16_t *version) {
|
||||
mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
|
||||
*verify1 = pkcrypt->verify1;
|
||||
*verify2 = pkcrypt->verify2;
|
||||
*version = pkcrypt->verify_version;
|
||||
}
|
||||
|
||||
int32_t mz_stream_pkcrypt_get_prop_int64(void *stream, int32_t prop, int64_t *value) {
|
||||
@ -319,8 +315,7 @@ void mz_stream_pkcrypt_delete(void **stream) {
|
||||
if (!stream)
|
||||
return;
|
||||
pkcrypt = (mz_stream_pkcrypt *)*stream;
|
||||
if (pkcrypt)
|
||||
free(pkcrypt);
|
||||
free(pkcrypt);
|
||||
*stream = NULL;
|
||||
}
|
||||
|
||||
|
@ -26,16 +26,16 @@ int32_t mz_stream_pkcrypt_seek(void *stream, int64_t offset, int32_t origin);
|
||||
int32_t mz_stream_pkcrypt_close(void *stream);
|
||||
int32_t mz_stream_pkcrypt_error(void *stream);
|
||||
|
||||
void mz_stream_pkcrypt_set_password(void *stream, const char *password);
|
||||
void mz_stream_pkcrypt_set_verify(void *stream, uint8_t verify1, uint8_t verify2);
|
||||
void mz_stream_pkcrypt_get_verify(void *stream, uint8_t *verify1, uint8_t *verify2);
|
||||
void mz_stream_pkcrypt_set_password(void *stream, const char *password);
|
||||
void mz_stream_pkcrypt_set_verify(void *stream, uint8_t verify1, uint8_t verify2, uint16_t version);
|
||||
void mz_stream_pkcrypt_get_verify(void *stream, uint8_t *verify1, uint8_t *verify2, uint16_t *version);
|
||||
int32_t mz_stream_pkcrypt_get_prop_int64(void *stream, int32_t prop, int64_t *value);
|
||||
int32_t mz_stream_pkcrypt_set_prop_int64(void *stream, int32_t prop, int64_t value);
|
||||
|
||||
void* mz_stream_pkcrypt_create(void);
|
||||
void mz_stream_pkcrypt_delete(void **stream);
|
||||
void *mz_stream_pkcrypt_create(void);
|
||||
void mz_stream_pkcrypt_delete(void **stream);
|
||||
|
||||
void* mz_stream_pkcrypt_get_interface(void);
|
||||
void *mz_stream_pkcrypt_get_interface(void);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
|
@ -27,38 +27,28 @@
|
||||
/***************************************************************************/
|
||||
|
||||
static mz_stream_vtbl mz_stream_split_vtbl = {
|
||||
mz_stream_split_open,
|
||||
mz_stream_split_is_open,
|
||||
mz_stream_split_read,
|
||||
mz_stream_split_write,
|
||||
mz_stream_split_tell,
|
||||
mz_stream_split_seek,
|
||||
mz_stream_split_close,
|
||||
mz_stream_split_error,
|
||||
mz_stream_split_create,
|
||||
mz_stream_split_delete,
|
||||
mz_stream_split_get_prop_int64,
|
||||
mz_stream_split_set_prop_int64
|
||||
};
|
||||
mz_stream_split_open, mz_stream_split_is_open, mz_stream_split_read, mz_stream_split_write,
|
||||
mz_stream_split_tell, mz_stream_split_seek, mz_stream_split_close, mz_stream_split_error,
|
||||
mz_stream_split_create, mz_stream_split_delete, mz_stream_split_get_prop_int64, mz_stream_split_set_prop_int64};
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct mz_stream_split_s {
|
||||
mz_stream stream;
|
||||
int32_t is_open;
|
||||
int64_t disk_size;
|
||||
int64_t total_in;
|
||||
int64_t total_in_disk;
|
||||
int64_t total_out;
|
||||
int64_t total_out_disk;
|
||||
int32_t mode;
|
||||
char *path_cd;
|
||||
char *path_disk;
|
||||
uint32_t path_disk_size;
|
||||
int32_t number_disk;
|
||||
int32_t current_disk;
|
||||
int64_t current_disk_size;
|
||||
int32_t reached_end;
|
||||
mz_stream stream;
|
||||
int64_t disk_size;
|
||||
int64_t total_in;
|
||||
int64_t total_in_disk;
|
||||
int64_t total_out;
|
||||
int64_t total_out_disk;
|
||||
int32_t is_open;
|
||||
int32_t mode;
|
||||
char *path_cd;
|
||||
char *path_disk;
|
||||
uint32_t path_disk_size;
|
||||
int32_t number_disk;
|
||||
int64_t current_disk_size;
|
||||
int32_t current_disk;
|
||||
int32_t reached_end;
|
||||
} mz_stream_split;
|
||||
|
||||
/***************************************************************************/
|
||||
@ -92,8 +82,7 @@ static int32_t mz_stream_split_open_disk(void *stream, int32_t number_disk) {
|
||||
for (i = (int32_t)strlen(split->path_disk) - 1; i >= 0; i -= 1) {
|
||||
if (split->path_disk[i] != '.')
|
||||
continue;
|
||||
snprintf(&split->path_disk[i], split->path_disk_size - (uint32_t)i,
|
||||
".z%02" PRId32, number_disk + 1);
|
||||
snprintf(&split->path_disk[i], split->path_disk_size - (uint32_t)i, ".z%02" PRId32, number_disk + 1);
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
@ -104,8 +93,13 @@ static int32_t mz_stream_split_open_disk(void *stream, int32_t number_disk) {
|
||||
mz_stream_split_print("Split - Goto disk - %s (disk %" PRId32 ")\n", split->path_disk, number_disk);
|
||||
|
||||
/* If disk part doesn't exist during reading then return MZ_EXIST_ERROR */
|
||||
if (disk_part == MZ_OPEN_MODE_READ)
|
||||
err = mz_os_file_exists(split->path_disk);
|
||||
if (disk_part == MZ_OPEN_MODE_READ) {
|
||||
if (strcmp(split->path_disk, split->path_cd) == 0) {
|
||||
err = MZ_EXIST_ERROR;
|
||||
} else {
|
||||
err = mz_os_file_exists(split->path_disk);
|
||||
}
|
||||
}
|
||||
|
||||
if (err == MZ_OK)
|
||||
err = mz_stream_open(split->stream.base, split->path_disk, split->mode);
|
||||
@ -238,11 +232,11 @@ int32_t mz_stream_split_read(void *stream, void *buf, int32_t size) {
|
||||
if (read == 0) {
|
||||
if (split->current_disk < 0) /* No more disks to goto */
|
||||
break;
|
||||
err = mz_stream_split_goto_disk(stream, split->current_disk + 1);
|
||||
if (err == MZ_EXIST_ERROR) {
|
||||
split->current_disk = -1;
|
||||
if (size != bytes_left) /* Report read from previous disk before switching */
|
||||
break;
|
||||
}
|
||||
err = mz_stream_split_goto_disk(stream, split->current_disk + 1);
|
||||
if (err == MZ_EXIST_ERROR)
|
||||
split->current_disk = -1;
|
||||
if (err != MZ_OK)
|
||||
return err;
|
||||
}
|
||||
@ -410,11 +404,8 @@ void mz_stream_split_delete(void **stream) {
|
||||
return;
|
||||
split = (mz_stream_split *)*stream;
|
||||
if (split) {
|
||||
if (split->path_cd)
|
||||
free(split->path_cd);
|
||||
if (split->path_disk)
|
||||
free(split->path_disk);
|
||||
|
||||
free(split->path_cd);
|
||||
free(split->path_disk);
|
||||
free(split);
|
||||
}
|
||||
*stream = NULL;
|
||||
|
@ -29,10 +29,10 @@ int32_t mz_stream_split_error(void *stream);
|
||||
int32_t mz_stream_split_get_prop_int64(void *stream, int32_t prop, int64_t *value);
|
||||
int32_t mz_stream_split_set_prop_int64(void *stream, int32_t prop, int64_t value);
|
||||
|
||||
void* mz_stream_split_create(void);
|
||||
void mz_stream_split_delete(void **stream);
|
||||
void *mz_stream_split_create(void);
|
||||
void mz_stream_split_delete(void **stream);
|
||||
|
||||
void* mz_stream_split_get_interface(void);
|
||||
void *mz_stream_split_get_interface(void);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
|
@ -27,38 +27,28 @@
|
||||
/***************************************************************************/
|
||||
|
||||
static mz_stream_vtbl mz_stream_wzaes_vtbl = {
|
||||
mz_stream_wzaes_open,
|
||||
mz_stream_wzaes_is_open,
|
||||
mz_stream_wzaes_read,
|
||||
mz_stream_wzaes_write,
|
||||
mz_stream_wzaes_tell,
|
||||
mz_stream_wzaes_seek,
|
||||
mz_stream_wzaes_close,
|
||||
mz_stream_wzaes_error,
|
||||
mz_stream_wzaes_create,
|
||||
mz_stream_wzaes_delete,
|
||||
mz_stream_wzaes_get_prop_int64,
|
||||
mz_stream_wzaes_set_prop_int64
|
||||
};
|
||||
mz_stream_wzaes_open, mz_stream_wzaes_is_open, mz_stream_wzaes_read, mz_stream_wzaes_write,
|
||||
mz_stream_wzaes_tell, mz_stream_wzaes_seek, mz_stream_wzaes_close, mz_stream_wzaes_error,
|
||||
mz_stream_wzaes_create, mz_stream_wzaes_delete, mz_stream_wzaes_get_prop_int64, mz_stream_wzaes_set_prop_int64};
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct mz_stream_wzaes_s {
|
||||
mz_stream stream;
|
||||
int32_t mode;
|
||||
int32_t error;
|
||||
int16_t initialized;
|
||||
uint8_t buffer[UINT16_MAX];
|
||||
int64_t total_in;
|
||||
int64_t max_total_in;
|
||||
int64_t total_out;
|
||||
uint8_t strength;
|
||||
const char *password;
|
||||
void *aes;
|
||||
uint32_t crypt_pos;
|
||||
uint8_t crypt_block[MZ_AES_BLOCK_SIZE];
|
||||
void *hmac;
|
||||
uint8_t nonce[MZ_AES_BLOCK_SIZE];
|
||||
mz_stream stream;
|
||||
int32_t mode;
|
||||
int32_t error;
|
||||
int16_t initialized;
|
||||
uint8_t buffer[UINT16_MAX];
|
||||
int64_t total_in;
|
||||
int64_t max_total_in;
|
||||
int64_t total_out;
|
||||
uint8_t strength;
|
||||
const char *password;
|
||||
void *aes;
|
||||
uint32_t crypt_pos;
|
||||
uint8_t crypt_block[MZ_AES_BLOCK_SIZE];
|
||||
void *hmac;
|
||||
uint8_t nonce[MZ_AES_BLOCK_SIZE];
|
||||
} mz_stream_wzaes;
|
||||
|
||||
/***************************************************************************/
|
||||
@ -103,8 +93,8 @@ int32_t mz_stream_wzaes_open(void *stream, const char *path, int32_t mode) {
|
||||
}
|
||||
|
||||
/* Derive the encryption and authentication keys and the password verifier */
|
||||
mz_crypt_pbkdf2((uint8_t *)password, password_length, salt_value, salt_length,
|
||||
MZ_AES_KEYING_ITERATIONS, kbuf, 2 * key_length + MZ_AES_PW_VERIFY_SIZE);
|
||||
mz_crypt_pbkdf2((uint8_t *)password, password_length, salt_value, salt_length, MZ_AES_KEYING_ITERATIONS, kbuf,
|
||||
2 * key_length + MZ_AES_PW_VERIFY_SIZE);
|
||||
|
||||
/* Initialize the buffer pos */
|
||||
wzaes->crypt_pos = MZ_AES_BLOCK_SIZE;
|
||||
|
@ -26,16 +26,16 @@ int32_t mz_stream_wzaes_seek(void *stream, int64_t offset, int32_t origin);
|
||||
int32_t mz_stream_wzaes_close(void *stream);
|
||||
int32_t mz_stream_wzaes_error(void *stream);
|
||||
|
||||
void mz_stream_wzaes_set_password(void *stream, const char *password);
|
||||
void mz_stream_wzaes_set_strength(void *stream, uint8_t strength);
|
||||
void mz_stream_wzaes_set_password(void *stream, const char *password);
|
||||
void mz_stream_wzaes_set_strength(void *stream, uint8_t strength);
|
||||
|
||||
int32_t mz_stream_wzaes_get_prop_int64(void *stream, int32_t prop, int64_t *value);
|
||||
int32_t mz_stream_wzaes_set_prop_int64(void *stream, int32_t prop, int64_t value);
|
||||
|
||||
void* mz_stream_wzaes_create(void);
|
||||
void mz_stream_wzaes_delete(void **stream);
|
||||
void *mz_stream_wzaes_create(void);
|
||||
void mz_stream_wzaes_delete(void **stream);
|
||||
|
||||
void* mz_stream_wzaes_get_interface(void);
|
||||
void *mz_stream_wzaes_get_interface(void);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
|
@ -21,53 +21,43 @@
|
||||
/***************************************************************************/
|
||||
|
||||
#if !defined(ZLIB_COMPAT)
|
||||
# define ZLIB_PREFIX(x) zng_ ## x
|
||||
typedef zng_stream zlib_stream;
|
||||
# define ZLIB_PREFIX(x) zng_##x
|
||||
typedef zng_stream zlib_stream;
|
||||
#else
|
||||
# define ZLIB_PREFIX(x) x
|
||||
typedef z_stream zlib_stream;
|
||||
typedef z_stream zlib_stream;
|
||||
#endif
|
||||
|
||||
#if !defined(DEF_MEM_LEVEL)
|
||||
# if MAX_MEM_LEVEL >= 8
|
||||
# define DEF_MEM_LEVEL 8
|
||||
# else
|
||||
# define DEF_MEM_LEVEL MAX_MEM_LEVEL
|
||||
# define DEF_MEM_LEVEL MAX_MEM_LEVEL
|
||||
# endif
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
static mz_stream_vtbl mz_stream_zlib_vtbl = {
|
||||
mz_stream_zlib_open,
|
||||
mz_stream_zlib_is_open,
|
||||
mz_stream_zlib_read,
|
||||
mz_stream_zlib_write,
|
||||
mz_stream_zlib_tell,
|
||||
mz_stream_zlib_seek,
|
||||
mz_stream_zlib_close,
|
||||
mz_stream_zlib_error,
|
||||
mz_stream_zlib_create,
|
||||
mz_stream_zlib_delete,
|
||||
mz_stream_zlib_get_prop_int64,
|
||||
mz_stream_zlib_set_prop_int64
|
||||
};
|
||||
mz_stream_zlib_open, mz_stream_zlib_is_open, mz_stream_zlib_read, mz_stream_zlib_write,
|
||||
mz_stream_zlib_tell, mz_stream_zlib_seek, mz_stream_zlib_close, mz_stream_zlib_error,
|
||||
mz_stream_zlib_create, mz_stream_zlib_delete, mz_stream_zlib_get_prop_int64, mz_stream_zlib_set_prop_int64};
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct mz_stream_zlib_s {
|
||||
mz_stream stream;
|
||||
mz_stream stream;
|
||||
zlib_stream zstream;
|
||||
uint8_t buffer[INT16_MAX];
|
||||
int32_t buffer_len;
|
||||
int64_t total_in;
|
||||
int64_t total_out;
|
||||
int64_t max_total_in;
|
||||
int8_t initialized;
|
||||
int16_t level;
|
||||
int32_t window_bits;
|
||||
int32_t mode;
|
||||
int32_t error;
|
||||
uint8_t buffer[INT16_MAX];
|
||||
int32_t buffer_len;
|
||||
int64_t total_in;
|
||||
int64_t total_out;
|
||||
int64_t max_total_in;
|
||||
int8_t initialized;
|
||||
int16_t level;
|
||||
int32_t window_bits;
|
||||
int32_t mode;
|
||||
int32_t error;
|
||||
} mz_stream_zlib;
|
||||
|
||||
/***************************************************************************/
|
||||
@ -94,8 +84,8 @@ int32_t mz_stream_zlib_open(void *stream, const char *path, int32_t mode) {
|
||||
zlib->zstream.next_out = zlib->buffer;
|
||||
zlib->zstream.avail_out = sizeof(zlib->buffer);
|
||||
|
||||
zlib->error = ZLIB_PREFIX(deflateInit2)(&zlib->zstream, (int8_t)zlib->level, Z_DEFLATED,
|
||||
zlib->window_bits, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
|
||||
zlib->error = ZLIB_PREFIX(deflateInit2)(&zlib->zstream, (int8_t)zlib->level, Z_DEFLATED, zlib->window_bits,
|
||||
DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
|
||||
#endif
|
||||
} else if (mode & MZ_OPEN_MODE_READ) {
|
||||
#ifdef MZ_ZIP_NO_DECOMPRESSION
|
||||
@ -347,7 +337,10 @@ int32_t mz_stream_zlib_set_prop_int64(void *stream, int32_t prop, int64_t value)
|
||||
mz_stream_zlib *zlib = (mz_stream_zlib *)stream;
|
||||
switch (prop) {
|
||||
case MZ_STREAM_PROP_COMPRESS_LEVEL:
|
||||
zlib->level = (int16_t)value;
|
||||
if (value == MZ_COMPRESS_LEVEL_DEFAULT)
|
||||
zlib->level = Z_DEFAULT_COMPRESSION;
|
||||
else
|
||||
zlib->level = (int16_t)value;
|
||||
break;
|
||||
case MZ_STREAM_PROP_TOTAL_IN_MAX:
|
||||
zlib->max_total_in = value;
|
||||
@ -362,7 +355,7 @@ int32_t mz_stream_zlib_set_prop_int64(void *stream, int32_t prop, int64_t value)
|
||||
}
|
||||
|
||||
void *mz_stream_zlib_create(void) {
|
||||
mz_stream_zlib *zlib = zlib = (mz_stream_zlib *)calloc(1, sizeof(mz_stream_zlib));
|
||||
mz_stream_zlib *zlib = (mz_stream_zlib *)calloc(1, sizeof(mz_stream_zlib));
|
||||
if (zlib) {
|
||||
zlib->stream.vtbl = &mz_stream_zlib_vtbl;
|
||||
zlib->level = Z_DEFAULT_COMPRESSION;
|
||||
@ -376,8 +369,7 @@ void mz_stream_zlib_delete(void **stream) {
|
||||
if (!stream)
|
||||
return;
|
||||
zlib = (mz_stream_zlib *)*stream;
|
||||
if (zlib)
|
||||
free(zlib);
|
||||
free(zlib);
|
||||
*stream = NULL;
|
||||
}
|
||||
|
||||
|
@ -29,10 +29,10 @@ int32_t mz_stream_zlib_error(void *stream);
|
||||
int32_t mz_stream_zlib_get_prop_int64(void *stream, int32_t prop, int64_t *value);
|
||||
int32_t mz_stream_zlib_set_prop_int64(void *stream, int32_t prop, int64_t value);
|
||||
|
||||
void* mz_stream_zlib_create(void);
|
||||
void mz_stream_zlib_delete(void **stream);
|
||||
void *mz_stream_zlib_create(void);
|
||||
void mz_stream_zlib_delete(void **stream);
|
||||
|
||||
void* mz_stream_zlib_get_interface(void);
|
||||
void *mz_stream_zlib_get_interface(void);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
|
@ -19,38 +19,28 @@
|
||||
/***************************************************************************/
|
||||
|
||||
static mz_stream_vtbl mz_stream_zstd_vtbl = {
|
||||
mz_stream_zstd_open,
|
||||
mz_stream_zstd_is_open,
|
||||
mz_stream_zstd_read,
|
||||
mz_stream_zstd_write,
|
||||
mz_stream_zstd_tell,
|
||||
mz_stream_zstd_seek,
|
||||
mz_stream_zstd_close,
|
||||
mz_stream_zstd_error,
|
||||
mz_stream_zstd_create,
|
||||
mz_stream_zstd_delete,
|
||||
mz_stream_zstd_get_prop_int64,
|
||||
mz_stream_zstd_set_prop_int64
|
||||
};
|
||||
mz_stream_zstd_open, mz_stream_zstd_is_open, mz_stream_zstd_read, mz_stream_zstd_write,
|
||||
mz_stream_zstd_tell, mz_stream_zstd_seek, mz_stream_zstd_close, mz_stream_zstd_error,
|
||||
mz_stream_zstd_create, mz_stream_zstd_delete, mz_stream_zstd_get_prop_int64, mz_stream_zstd_set_prop_int64};
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct mz_stream_zstd_s {
|
||||
mz_stream stream;
|
||||
ZSTD_CStream *zcstream;
|
||||
ZSTD_DStream *zdstream;
|
||||
ZSTD_outBuffer out;
|
||||
ZSTD_inBuffer in;
|
||||
int32_t mode;
|
||||
int32_t error;
|
||||
uint8_t buffer[INT16_MAX];
|
||||
int32_t buffer_len;
|
||||
int64_t total_in;
|
||||
int64_t total_out;
|
||||
int64_t max_total_in;
|
||||
int64_t max_total_out;
|
||||
int8_t initialized;
|
||||
uint32_t preset;
|
||||
mz_stream stream;
|
||||
ZSTD_CStream *zcstream;
|
||||
ZSTD_DStream *zdstream;
|
||||
ZSTD_outBuffer out;
|
||||
ZSTD_inBuffer in;
|
||||
int32_t mode;
|
||||
int32_t error;
|
||||
uint8_t buffer[INT16_MAX];
|
||||
int32_t buffer_len;
|
||||
int64_t total_in;
|
||||
int64_t total_out;
|
||||
int64_t max_total_in;
|
||||
int64_t max_total_out;
|
||||
int8_t initialized;
|
||||
int32_t preset;
|
||||
} mz_stream_zstd;
|
||||
|
||||
/***************************************************************************/
|
||||
@ -107,7 +97,6 @@ int32_t mz_stream_zstd_read(void *stream, void *buf, int32_t size) {
|
||||
uint64_t total_in_after = 0;
|
||||
uint64_t total_out_before = 0;
|
||||
uint64_t total_out_after = 0;
|
||||
int32_t total_in = 0;
|
||||
int32_t total_out = 0;
|
||||
int32_t in_bytes = 0;
|
||||
int32_t out_bytes = 0;
|
||||
@ -154,7 +143,6 @@ int32_t mz_stream_zstd_read(void *stream, void *buf, int32_t size) {
|
||||
in_bytes = (int32_t)(total_in_after - total_in_before);
|
||||
out_bytes = (int32_t)(total_out_after - total_out_before);
|
||||
|
||||
total_in += in_bytes;
|
||||
total_out += out_bytes;
|
||||
|
||||
zstd->total_in += in_bytes;
|
||||
@ -312,8 +300,8 @@ int32_t mz_stream_zstd_set_prop_int64(void *stream, int32_t prop, int64_t value)
|
||||
mz_stream_zstd *zstd = (mz_stream_zstd *)stream;
|
||||
switch (prop) {
|
||||
case MZ_STREAM_PROP_COMPRESS_LEVEL:
|
||||
if (value < 0)
|
||||
zstd->preset = 6;
|
||||
if (value == MZ_COMPRESS_LEVEL_DEFAULT)
|
||||
zstd->preset = ZSTD_CLEVEL_DEFAULT;
|
||||
else
|
||||
zstd->preset = (int16_t)value;
|
||||
return MZ_OK;
|
||||
@ -329,6 +317,7 @@ void *mz_stream_zstd_create(void) {
|
||||
if (zstd) {
|
||||
zstd->stream.vtbl = &mz_stream_zstd_vtbl;
|
||||
zstd->max_total_out = -1;
|
||||
zstd->preset = ZSTD_CLEVEL_DEFAULT;
|
||||
}
|
||||
return zstd;
|
||||
}
|
||||
@ -338,8 +327,7 @@ void mz_stream_zstd_delete(void **stream) {
|
||||
if (!stream)
|
||||
return;
|
||||
zstd = (mz_stream_zstd *)*stream;
|
||||
if (zstd)
|
||||
free(zstd);
|
||||
free(zstd);
|
||||
*stream = NULL;
|
||||
}
|
||||
|
||||
|
@ -30,10 +30,10 @@ int32_t mz_stream_zstd_error(void *stream);
|
||||
int32_t mz_stream_zstd_get_prop_int64(void *stream, int32_t prop, int64_t *value);
|
||||
int32_t mz_stream_zstd_set_prop_int64(void *stream, int32_t prop, int64_t value);
|
||||
|
||||
void* mz_stream_zstd_create(void);
|
||||
void mz_stream_zstd_delete(void **stream);
|
||||
void *mz_stream_zstd_create(void);
|
||||
void mz_stream_zstd_delete(void **stream);
|
||||
|
||||
void* mz_stream_zstd_get_interface(void);
|
||||
void *mz_stream_zstd_get_interface(void);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
|
297
mz_zip.c
297
mz_zip.c
@ -56,15 +56,15 @@
|
||||
/***************************************************************************/
|
||||
|
||||
#define MZ_ZIP_MAGIC_LOCALHEADER (0x04034b50)
|
||||
#define MZ_ZIP_MAGIC_LOCALHEADERU8 { 0x50, 0x4b, 0x03, 0x04 }
|
||||
#define MZ_ZIP_MAGIC_LOCALHEADERU8 {0x50, 0x4b, 0x03, 0x04}
|
||||
#define MZ_ZIP_MAGIC_CENTRALHEADER (0x02014b50)
|
||||
#define MZ_ZIP_MAGIC_CENTRALHEADERU8 { 0x50, 0x4b, 0x01, 0x02 }
|
||||
#define MZ_ZIP_MAGIC_CENTRALHEADERU8 {0x50, 0x4b, 0x01, 0x02}
|
||||
#define MZ_ZIP_MAGIC_ENDHEADER (0x06054b50)
|
||||
#define MZ_ZIP_MAGIC_ENDHEADERU8 { 0x50, 0x4b, 0x05, 0x06 }
|
||||
#define MZ_ZIP_MAGIC_ENDHEADERU8 {0x50, 0x4b, 0x05, 0x06}
|
||||
#define MZ_ZIP_MAGIC_ENDHEADER64 (0x06064b50)
|
||||
#define MZ_ZIP_MAGIC_ENDLOCHEADER64 (0x07064b50)
|
||||
#define MZ_ZIP_MAGIC_DATADESCRIPTOR (0x08074b50)
|
||||
#define MZ_ZIP_MAGIC_DATADESCRIPTORU8 { 0x50, 0x4b, 0x07, 0x08 }
|
||||
#define MZ_ZIP_MAGIC_DATADESCRIPTORU8 {0x50, 0x4b, 0x07, 0x08}
|
||||
|
||||
#define MZ_ZIP_SIZE_LD_ITEM (30)
|
||||
#define MZ_ZIP_SIZE_CD_ITEM (46)
|
||||
@ -75,7 +75,7 @@
|
||||
#define MZ_ZIP_UNCOMPR_SIZE64_CUSHION (2 * 1024 * 1024)
|
||||
|
||||
#ifndef MZ_ZIP_EOCD_MAX_BACK
|
||||
#define MZ_ZIP_EOCD_MAX_BACK (1 << 20)
|
||||
# define MZ_ZIP_EOCD_MAX_BACK (1 << 20)
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
@ -84,36 +84,36 @@ typedef struct mz_zip_s {
|
||||
mz_zip_file file_info;
|
||||
mz_zip_file local_file_info;
|
||||
|
||||
void *stream; /* main stream */
|
||||
void *cd_stream; /* pointer to the stream with the cd */
|
||||
void *cd_mem_stream; /* memory stream for central directory */
|
||||
void *compress_stream; /* compression stream */
|
||||
void *crypt_stream; /* encryption stream */
|
||||
void *file_info_stream; /* memory stream for storing file info */
|
||||
void *local_file_info_stream; /* memory stream for storing local file info */
|
||||
void *stream; /* main stream */
|
||||
void *cd_stream; /* pointer to the stream with the cd */
|
||||
void *cd_mem_stream; /* memory stream for central directory */
|
||||
void *compress_stream; /* compression stream */
|
||||
void *crypt_stream; /* encryption stream */
|
||||
void *file_info_stream; /* memory stream for storing file info */
|
||||
void *local_file_info_stream; /* memory stream for storing local file info */
|
||||
|
||||
int32_t open_mode;
|
||||
uint8_t recover;
|
||||
uint8_t data_descriptor;
|
||||
int32_t open_mode;
|
||||
uint8_t recover;
|
||||
uint8_t data_descriptor;
|
||||
|
||||
uint32_t disk_number_with_cd; /* number of the disk with the central dir */
|
||||
int64_t disk_offset_shift; /* correction for zips that have wrong offset start of cd */
|
||||
uint32_t disk_number_with_cd; /* number of the disk with the central dir */
|
||||
int64_t disk_offset_shift; /* correction for zips that have wrong offset start of cd */
|
||||
|
||||
int64_t cd_start_pos; /* pos of the first file in the central dir stream */
|
||||
int64_t cd_current_pos; /* pos of the current file in the central dir */
|
||||
int64_t cd_offset; /* offset of start of central directory */
|
||||
int64_t cd_size; /* size of the central directory */
|
||||
uint32_t cd_signature; /* signature of central directory */
|
||||
int64_t cd_start_pos; /* pos of the first file in the central dir stream */
|
||||
int64_t cd_current_pos; /* pos of the current file in the central dir */
|
||||
int64_t cd_offset; /* offset of start of central directory */
|
||||
int64_t cd_size; /* size of the central directory */
|
||||
uint32_t cd_signature; /* signature of central directory */
|
||||
|
||||
uint8_t entry_scanned; /* entry header information read ok */
|
||||
uint8_t entry_opened; /* entry is open for read/write */
|
||||
uint8_t entry_raw; /* entry opened with raw mode */
|
||||
uint32_t entry_crc32; /* entry crc32 */
|
||||
uint8_t entry_scanned; /* entry header information read ok */
|
||||
uint8_t entry_opened; /* entry is open for read/write */
|
||||
uint8_t entry_raw; /* entry opened with raw mode */
|
||||
uint32_t entry_crc32; /* entry crc32 */
|
||||
|
||||
uint64_t number_entry;
|
||||
|
||||
uint16_t version_madeby;
|
||||
char *comment;
|
||||
char *comment;
|
||||
} mz_zip;
|
||||
|
||||
/***************************************************************************/
|
||||
@ -188,8 +188,7 @@ static int32_t mz_zip_search_zip64_eocd(void *stream, const int64_t end_central_
|
||||
|
||||
#ifdef HAVE_PKCRYPT
|
||||
/* Get PKWARE traditional encryption verifier */
|
||||
static uint16_t mz_zip_get_pk_verify(uint32_t dos_date, uint64_t crc, uint16_t flag)
|
||||
{
|
||||
static uint16_t mz_zip_get_pk_verify(uint32_t dos_date, uint64_t crc, uint16_t flag) {
|
||||
/* Info-ZIP modification to ZipCrypto format: if bit 3 of the general
|
||||
* purpose bit flag is set, it uses high byte of 16-bit File Time. */
|
||||
if (flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR)
|
||||
@ -464,22 +463,22 @@ static int32_t mz_zip_entry_read_header(void *stream, uint8_t local, mz_zip_file
|
||||
file_info->linkname = "";
|
||||
|
||||
if (err == MZ_OK) {
|
||||
mz_zip_print("Zip - Entry - Read header - %s (local %" PRId8 ")\n",
|
||||
file_info->filename, local);
|
||||
mz_zip_print("Zip - Entry - Read header - %s (local %" PRId8 ")\n", file_info->filename, local);
|
||||
mz_zip_print("Zip - Entry - Read header compress (ucs %" PRId64 " cs %" PRId64 " crc 0x%08" PRIx32 ")\n",
|
||||
file_info->uncompressed_size, file_info->compressed_size, file_info->crc);
|
||||
file_info->uncompressed_size, file_info->compressed_size, file_info->crc);
|
||||
if (!local) {
|
||||
mz_zip_print("Zip - Entry - Read header disk (disk %" PRIu32 " offset %" PRId64 ")\n",
|
||||
file_info->disk_number, file_info->disk_offset);
|
||||
file_info->disk_number, file_info->disk_offset);
|
||||
}
|
||||
mz_zip_print("Zip - Entry - Read header variable (fnl %" PRId32 " efs %" PRId32 " cms %" PRId32 ")\n",
|
||||
file_info->filename_size, file_info->extrafield_size, file_info->comment_size);
|
||||
file_info->filename_size, file_info->extrafield_size, file_info->comment_size);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
static int32_t mz_zip_entry_read_descriptor(void *stream, uint8_t zip64, uint32_t *crc32, int64_t *compressed_size, int64_t *uncompressed_size) {
|
||||
static int32_t mz_zip_entry_read_descriptor(void *stream, uint8_t zip64, uint32_t *crc32, int64_t *compressed_size,
|
||||
int64_t *uncompressed_size) {
|
||||
uint32_t value32 = 0;
|
||||
int64_t value64 = 0;
|
||||
int32_t err = MZ_OK;
|
||||
@ -564,13 +563,11 @@ static int32_t mz_zip_entry_needs_zip64(mz_zip_file *file_info, uint8_t local, u
|
||||
max_uncompressed_size -= MZ_ZIP_UNCOMPR_SIZE64_CUSHION;
|
||||
}
|
||||
|
||||
needs_zip64 = (file_info->uncompressed_size >= max_uncompressed_size) ||
|
||||
(file_info->compressed_size >= UINT32_MAX);
|
||||
needs_zip64 = (file_info->uncompressed_size >= max_uncompressed_size) || (file_info->compressed_size >= UINT32_MAX);
|
||||
|
||||
if (!local) {
|
||||
/* Disk offset and number only used in central directory header */
|
||||
needs_zip64 |= (file_info->disk_offset >= UINT32_MAX) ||
|
||||
(file_info->disk_number >= UINT16_MAX);
|
||||
needs_zip64 |= (file_info->disk_offset >= UINT32_MAX) || (file_info->disk_number >= UINT16_MAX);
|
||||
}
|
||||
|
||||
if (file_info->zip64 == MZ_ZIP64_AUTO) {
|
||||
@ -646,8 +643,7 @@ static int32_t mz_zip_entry_write_header(void *stream, uint8_t local, mz_zip_fil
|
||||
file_extra_stream = mz_stream_mem_create();
|
||||
if (!file_extra_stream)
|
||||
return MZ_MEM_ERROR;
|
||||
mz_stream_mem_set_buffer(file_extra_stream, (void *)file_info->extrafield,
|
||||
file_info->extrafield_size);
|
||||
mz_stream_mem_set_buffer(file_extra_stream, (void *)file_info->extrafield, file_info->extrafield_size);
|
||||
|
||||
do {
|
||||
err_mem = mz_stream_read_uint16(file_extra_stream, &field_type);
|
||||
@ -682,9 +678,8 @@ static int32_t mz_zip_entry_write_header(void *stream, uint8_t local, mz_zip_fil
|
||||
MZ_UNUSED(skip_aes);
|
||||
#endif
|
||||
/* NTFS timestamps */
|
||||
if ((file_info->modified_date != 0) &&
|
||||
(file_info->accessed_date != 0) &&
|
||||
(file_info->creation_date != 0) && (!mask)) {
|
||||
if ((file_info->modified_date != 0) && (file_info->accessed_date != 0) && (file_info->creation_date != 0) &&
|
||||
(!mask)) {
|
||||
field_length_ntfs = 8 + 8 + 8 + 4 + 2 + 2;
|
||||
extrafield_size += 4 + field_length_ntfs;
|
||||
}
|
||||
@ -711,6 +706,10 @@ static int32_t mz_zip_entry_write_header(void *stream, uint8_t local, mz_zip_fil
|
||||
version_needed = 20;
|
||||
if (zip64)
|
||||
version_needed = 45;
|
||||
#ifdef HAVE_BZIP2
|
||||
if (file_info->compression_method == MZ_COMPRESS_METHOD_BZIP2)
|
||||
version_needed = 46;
|
||||
#endif
|
||||
#ifdef HAVE_WZAES
|
||||
if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
|
||||
version_needed = 51;
|
||||
@ -743,8 +742,8 @@ static int32_t mz_zip_entry_write_header(void *stream, uint8_t local, mz_zip_fil
|
||||
err = mz_zip_entry_write_crc_sizes(stream, zip64, mask, file_info);
|
||||
|
||||
if (mask) {
|
||||
snprintf(masked_name, sizeof(masked_name), "%" PRIx32 "_%" PRIx64,
|
||||
file_info->disk_number, file_info->disk_offset);
|
||||
snprintf(masked_name, sizeof(masked_name), "%" PRIx32 "_%" PRIx64, file_info->disk_number,
|
||||
file_info->disk_offset);
|
||||
filename = masked_name;
|
||||
} else {
|
||||
filename = file_info->filename;
|
||||
@ -787,22 +786,22 @@ static int32_t mz_zip_entry_write_header(void *stream, uint8_t local, mz_zip_fil
|
||||
}
|
||||
|
||||
if (err == MZ_OK) {
|
||||
const char *backslash = NULL;
|
||||
const char *next = filename;
|
||||
int32_t left = filename_length;
|
||||
#if defined(_WIN32)
|
||||
const char *backslash = NULL;
|
||||
|
||||
/* Ensure all slashes are written as forward slashes according to 4.4.17.1 */
|
||||
while ((err == MZ_OK) && (backslash = strchr(next, '\\'))) {
|
||||
int32_t part_length = (int32_t)(backslash - next);
|
||||
|
||||
if (mz_stream_write(stream, next, part_length) != part_length ||
|
||||
mz_stream_write(stream, "/", 1) != 1)
|
||||
if (mz_stream_write(stream, next, part_length) != part_length || mz_stream_write(stream, "/", 1) != 1)
|
||||
err = MZ_WRITE_ERROR;
|
||||
|
||||
left -= part_length + 1;
|
||||
next = backslash + 1;
|
||||
}
|
||||
|
||||
#endif
|
||||
if (err == MZ_OK && left > 0) {
|
||||
if (mz_stream_write(stream, next, left) != left)
|
||||
err = MZ_WRITE_ERROR;
|
||||
@ -918,7 +917,8 @@ static int32_t mz_zip_entry_write_header(void *stream, uint8_t local, mz_zip_fil
|
||||
return err;
|
||||
}
|
||||
|
||||
static int32_t mz_zip_entry_write_descriptor(void *stream, uint8_t zip64, uint32_t crc32, int64_t compressed_size, int64_t uncompressed_size) {
|
||||
static int32_t mz_zip_entry_write_descriptor(void *stream, uint8_t zip64, uint32_t crc32, int64_t compressed_size,
|
||||
int64_t uncompressed_size) {
|
||||
int32_t err = MZ_OK;
|
||||
|
||||
err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_DATADESCRIPTOR);
|
||||
@ -980,7 +980,8 @@ static int32_t mz_zip_read_cd(void *handle) {
|
||||
if (err == MZ_OK)
|
||||
err = mz_stream_read_uint16(zip->stream, &value16);
|
||||
number_entry_cd = value16;
|
||||
if (number_entry_cd != zip->number_entry)
|
||||
/* When recover is enabled, we can ignore incorrect number of entries */
|
||||
if (number_entry_cd != zip->number_entry && !zip->recover)
|
||||
err = MZ_FORMAT_ERROR;
|
||||
/* Size of the central directory */
|
||||
if (err == MZ_OK)
|
||||
@ -1059,7 +1060,7 @@ static int32_t mz_zip_read_cd(void *handle) {
|
||||
|
||||
if (err == MZ_OK) {
|
||||
mz_zip_print("Zip - Read cd (disk %" PRId32 " entries %" PRId64 " offset %" PRId64 " size %" PRId64 ")\n",
|
||||
zip->disk_number_with_cd, zip->number_entry, zip->cd_offset, zip->cd_size);
|
||||
zip->disk_number_with_cd, zip->number_entry, zip->cd_offset, zip->cd_size);
|
||||
|
||||
/* Verify central directory signature exists at offset */
|
||||
err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
|
||||
@ -1114,7 +1115,7 @@ static int32_t mz_zip_write_cd(void *handle) {
|
||||
zip->disk_number_with_cd += 1;
|
||||
mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
|
||||
if ((zip->disk_number_with_cd > 0) && (zip->open_mode & MZ_OPEN_MODE_APPEND)) {
|
||||
// Overwrite existing central directory if using split disks
|
||||
/* Overwrite existing central directory if using split disks */
|
||||
mz_stream_seek(zip->stream, 0, MZ_SEEK_SET);
|
||||
}
|
||||
|
||||
@ -1126,10 +1127,10 @@ static int32_t mz_zip_write_cd(void *handle) {
|
||||
err = mz_stream_copy(zip->stream, zip->cd_mem_stream, (int32_t)zip->cd_size);
|
||||
|
||||
mz_zip_print("Zip - Write cd (disk %" PRId32 " entries %" PRId64 " offset %" PRId64 " size %" PRId64 ")\n",
|
||||
zip->disk_number_with_cd, zip->number_entry, zip->cd_offset, zip->cd_size);
|
||||
zip->disk_number_with_cd, zip->number_entry, zip->cd_offset, zip->cd_size);
|
||||
|
||||
if (zip->cd_size == 0 && zip->number_entry > 0) {
|
||||
// Zip does not contain central directory, open with recovery option
|
||||
/* Zip does not contain central directory, open with recovery option */
|
||||
return MZ_FORMAT_ERROR;
|
||||
}
|
||||
|
||||
@ -1276,8 +1277,8 @@ static int32_t mz_zip_recover_cd(void *handle) {
|
||||
mz_stream_mem_open(local_file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
|
||||
|
||||
if (err == MZ_OK) {
|
||||
err = mz_stream_find(zip->stream, (const void *)local_header_magic, sizeof(local_header_magic),
|
||||
INT64_MAX, &next_header_pos);
|
||||
err = mz_stream_find(zip->stream, (const void *)local_header_magic, sizeof(local_header_magic), INT64_MAX,
|
||||
&next_header_pos);
|
||||
}
|
||||
|
||||
while (err == MZ_OK && !eof) {
|
||||
@ -1304,15 +1305,15 @@ static int32_t mz_zip_recover_cd(void *handle) {
|
||||
|
||||
for (;;) {
|
||||
/* Search for the next local header */
|
||||
err = mz_stream_find(zip->stream, (const void *)local_header_magic, sizeof(local_header_magic),
|
||||
INT64_MAX, &next_header_pos);
|
||||
err = mz_stream_find(zip->stream, (const void *)local_header_magic, sizeof(local_header_magic), INT64_MAX,
|
||||
&next_header_pos);
|
||||
|
||||
if (err == MZ_EXIST_ERROR) {
|
||||
mz_stream_seek(zip->stream, compressed_pos, MZ_SEEK_SET);
|
||||
|
||||
/* Search for central dir if no local header found */
|
||||
err = mz_stream_find(zip->stream, (const void *)central_header_magic, sizeof(central_header_magic),
|
||||
INT64_MAX, &next_header_pos);
|
||||
INT64_MAX, &next_header_pos);
|
||||
|
||||
if (err == MZ_EXIST_ERROR) {
|
||||
/* Get end of stream if no central header found */
|
||||
@ -1324,16 +1325,17 @@ static int32_t mz_zip_recover_cd(void *handle) {
|
||||
}
|
||||
|
||||
if (local_file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR || local_file_info.compressed_size == 0) {
|
||||
/* Search backwards for the descriptor, seeking too far back will be incorrect if compressed size is small */
|
||||
/* Search backwards for the descriptor, seeking too far back will be incorrect if compressed size is
|
||||
* small */
|
||||
err = mz_stream_find_reverse(zip->stream, (const void *)descriptor_magic, sizeof(descriptor_magic),
|
||||
MZ_ZIP_SIZE_MAX_DATA_DESCRIPTOR, &descriptor_pos);
|
||||
MZ_ZIP_SIZE_MAX_DATA_DESCRIPTOR, &descriptor_pos);
|
||||
if (err == MZ_OK) {
|
||||
if (mz_zip_extrafield_contains(local_file_info.extrafield,
|
||||
local_file_info.extrafield_size, MZ_ZIP_EXTENSION_ZIP64, NULL) == MZ_OK)
|
||||
if (mz_zip_extrafield_contains(local_file_info.extrafield, local_file_info.extrafield_size,
|
||||
MZ_ZIP_EXTENSION_ZIP64, NULL) == MZ_OK)
|
||||
zip64 = 1;
|
||||
|
||||
err = mz_zip_entry_read_descriptor(zip->stream, zip64, &crc32,
|
||||
&compressed_size, &uncompressed_size);
|
||||
err =
|
||||
mz_zip_entry_read_descriptor(zip->stream, zip64, &crc32, &compressed_size, &uncompressed_size);
|
||||
|
||||
if (err == MZ_OK) {
|
||||
if (local_file_info.crc == 0)
|
||||
@ -1371,8 +1373,8 @@ static int32_t mz_zip_recover_cd(void *handle) {
|
||||
}
|
||||
|
||||
mz_zip_print("Zip - Recover - Entry %s (csize %" PRId64 " usize %" PRId64 " flags 0x%" PRIx16 ")\n",
|
||||
local_file_info.filename, local_file_info.compressed_size, local_file_info.uncompressed_size,
|
||||
local_file_info.flag);
|
||||
local_file_info.filename, local_file_info.compressed_size, local_file_info.uncompressed_size,
|
||||
local_file_info.flag);
|
||||
|
||||
/* Rewrite central dir with local headers and offsets */
|
||||
err = mz_zip_entry_write_header(cd_mem_stream, 0, &local_file_info);
|
||||
@ -1384,8 +1386,8 @@ static int32_t mz_zip_recover_cd(void *handle) {
|
||||
|
||||
mz_stream_mem_delete(&local_file_info_stream);
|
||||
|
||||
mz_zip_print("Zip - Recover - Complete (cddisk %" PRId32 " entries %" PRId64 ")\n",
|
||||
disk_number_with_cd, number_entry);
|
||||
mz_zip_print("Zip - Recover - Complete (cddisk %" PRId32 " entries %" PRId64 ")\n", disk_number_with_cd,
|
||||
number_entry);
|
||||
|
||||
if (number_entry == 0)
|
||||
return err;
|
||||
@ -1414,9 +1416,7 @@ void mz_zip_delete(void **handle) {
|
||||
if (!handle)
|
||||
return;
|
||||
zip = (mz_zip *)*handle;
|
||||
if (zip) {
|
||||
free(zip);
|
||||
}
|
||||
free(zip);
|
||||
*handle = NULL;
|
||||
}
|
||||
|
||||
@ -1558,8 +1558,7 @@ int32_t mz_zip_set_comment(void *handle, const char *comment) {
|
||||
int32_t comment_size = 0;
|
||||
if (!zip || !comment)
|
||||
return MZ_PARAM_ERROR;
|
||||
if (zip->comment)
|
||||
free(zip->comment);
|
||||
free(zip->comment);
|
||||
comment_size = (int32_t)strlen(comment);
|
||||
if (comment_size > UINT16_MAX)
|
||||
return MZ_PARAM_ERROR;
|
||||
@ -1749,7 +1748,7 @@ static int32_t mz_zip_entry_open_int(void *handle, uint8_t raw, int16_t compress
|
||||
if (!zip->crypt_stream)
|
||||
return MZ_MEM_ERROR;
|
||||
mz_stream_pkcrypt_set_password(zip->crypt_stream, password);
|
||||
mz_stream_pkcrypt_set_verify(zip->crypt_stream, verify1, verify2);
|
||||
mz_stream_pkcrypt_set_verify(zip->crypt_stream, verify1, verify2, zip->file_info.version_needed);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@ -1780,18 +1779,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_DEFLATE ||
|
||||
zip->file_info.compression_method == MZ_COMPRESS_METHOD_XZ) {
|
||||
zip->compress_stream = mz_stream_libcomp_create();
|
||||
if (zip->compress_stream)
|
||||
if (zip->compress_stream) {
|
||||
mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_COMPRESS_METHOD,
|
||||
zip->file_info.compression_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) {
|
||||
zip->compress_stream = mz_stream_lzma_create();
|
||||
if (zip->compress_stream)
|
||||
if (zip->compress_stream) {
|
||||
mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_COMPRESS_METHOD,
|
||||
zip->file_info.compression_method);
|
||||
zip->file_info.compression_method);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#ifdef HAVE_ZSTD
|
||||
@ -1812,8 +1813,7 @@ static int32_t mz_zip_entry_open_int(void *handle, uint8_t raw, int16_t compress
|
||||
int32_t set_end_of_stream = 0;
|
||||
|
||||
#ifndef HAVE_LIBCOMP
|
||||
if (zip->entry_raw ||
|
||||
zip->file_info.compression_method == MZ_COMPRESS_METHOD_STORE ||
|
||||
if (zip->entry_raw || zip->file_info.compression_method == MZ_COMPRESS_METHOD_STORE ||
|
||||
zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED)
|
||||
#endif
|
||||
{
|
||||
@ -1839,8 +1839,10 @@ static int32_t mz_zip_entry_open_int(void *handle, uint8_t raw, int16_t compress
|
||||
}
|
||||
|
||||
if (set_end_of_stream) {
|
||||
mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, zip->file_info.compressed_size);
|
||||
mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_OUT_MAX, zip->file_info.uncompressed_size);
|
||||
mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN_MAX,
|
||||
zip->file_info.compressed_size);
|
||||
mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_OUT_MAX,
|
||||
zip->file_info.uncompressed_size);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1909,7 +1911,8 @@ int32_t mz_zip_entry_read_open(void *handle, uint8_t raw, const char *password)
|
||||
return err;
|
||||
}
|
||||
|
||||
int32_t mz_zip_entry_write_open(void *handle, const mz_zip_file *file_info, int16_t compress_level, uint8_t raw, const char *password) {
|
||||
int32_t mz_zip_entry_write_open(void *handle, const mz_zip_file *file_info, int16_t compress_level, uint8_t raw,
|
||||
const char *password) {
|
||||
mz_zip *zip = (mz_zip *)handle;
|
||||
int64_t filename_pos = -1;
|
||||
int64_t extrafield_pos = 0;
|
||||
@ -1934,8 +1937,8 @@ int32_t mz_zip_entry_write_open(void *handle, const mz_zip_file *file_info, int1
|
||||
|
||||
memcpy(&zip->file_info, file_info, sizeof(mz_zip_file));
|
||||
|
||||
mz_zip_print("Zip - Entry - Write open - %s (level %" PRId16 " raw %" PRId8 ")\n",
|
||||
zip->file_info.filename, compress_level, raw);
|
||||
mz_zip_print("Zip - Entry - Write open - %s (level %" PRId16 " raw %" PRId8 ")\n", zip->file_info.filename,
|
||||
compress_level, raw);
|
||||
|
||||
mz_stream_seek(zip->file_info_stream, 0, MZ_SEEK_SET);
|
||||
mz_stream_write(zip->file_info_stream, file_info, sizeof(mz_zip_file));
|
||||
@ -2060,12 +2063,10 @@ int32_t mz_zip_entry_write(void *handle, const void *buf, int32_t len) {
|
||||
zip->entry_crc32 = mz_crypt_crc32_update(zip->entry_crc32, buf, written);
|
||||
|
||||
mz_zip_print("Zip - Entry - Write - %" PRId32 " (max %" PRId32 ")\n", written, len);
|
||||
|
||||
return written;
|
||||
}
|
||||
|
||||
int32_t mz_zip_entry_read_close(void *handle, uint32_t *crc32, int64_t *compressed_size,
|
||||
int64_t *uncompressed_size) {
|
||||
int32_t mz_zip_entry_read_close(void *handle, uint32_t *crc32, int64_t *compressed_size, int64_t *uncompressed_size) {
|
||||
mz_zip *zip = (mz_zip *)handle;
|
||||
int64_t total_in = 0;
|
||||
int32_t err = MZ_OK;
|
||||
@ -2088,26 +2089,26 @@ int32_t mz_zip_entry_read_close(void *handle, uint32_t *crc32, int64_t *compress
|
||||
mz_stream_get_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN, &total_in);
|
||||
|
||||
if ((zip->file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR) &&
|
||||
((zip->file_info.flag & MZ_ZIP_FLAG_MASK_LOCAL_INFO) == 0) &&
|
||||
(crc32 || compressed_size || uncompressed_size)) {
|
||||
((zip->file_info.flag & MZ_ZIP_FLAG_MASK_LOCAL_INFO) == 0) && (crc32 || compressed_size || uncompressed_size)) {
|
||||
/* Check to see if data descriptor is zip64 bit format or not */
|
||||
if (mz_zip_extrafield_contains(zip->local_file_info.extrafield,
|
||||
zip->local_file_info.extrafield_size, MZ_ZIP_EXTENSION_ZIP64, NULL) == MZ_OK)
|
||||
if (mz_zip_extrafield_contains(zip->local_file_info.extrafield, zip->local_file_info.extrafield_size,
|
||||
MZ_ZIP_EXTENSION_ZIP64, NULL) == MZ_OK) {
|
||||
zip64 = 1;
|
||||
}
|
||||
|
||||
err = mz_zip_entry_seek_local_header(handle);
|
||||
|
||||
/* Seek to end of compressed stream since we might have over-read during compression */
|
||||
if (err == MZ_OK)
|
||||
err = mz_stream_seek(zip->stream, MZ_ZIP_SIZE_LD_ITEM +
|
||||
(int64_t)zip->local_file_info.filename_size +
|
||||
(int64_t)zip->local_file_info.extrafield_size +
|
||||
total_in, MZ_SEEK_CUR);
|
||||
if (err == MZ_OK) {
|
||||
err = mz_stream_seek(zip->stream,
|
||||
MZ_ZIP_SIZE_LD_ITEM + (int64_t)zip->local_file_info.filename_size +
|
||||
(int64_t)zip->local_file_info.extrafield_size + total_in,
|
||||
MZ_SEEK_CUR);
|
||||
}
|
||||
|
||||
/* Read data descriptor */
|
||||
if (err == MZ_OK)
|
||||
err = mz_zip_entry_read_descriptor(zip->stream, zip64,
|
||||
crc32, compressed_size, uncompressed_size);
|
||||
err = mz_zip_entry_read_descriptor(zip->stream, zip64, crc32, compressed_size, uncompressed_size);
|
||||
}
|
||||
|
||||
/* If entire entry was not read verification will fail */
|
||||
@ -2119,7 +2120,7 @@ int32_t mz_zip_entry_read_close(void *handle, uint32_t *crc32, int64_t *compress
|
||||
{
|
||||
if (zip->entry_crc32 != zip->file_info.crc) {
|
||||
mz_zip_print("Zip - Entry - Crc failed (actual 0x%08" PRIx32 " expected 0x%08" PRIx32 ")\n",
|
||||
zip->entry_crc32, zip->file_info.crc);
|
||||
zip->entry_crc32, zip->file_info.crc);
|
||||
|
||||
err = MZ_CRC_ERROR;
|
||||
}
|
||||
@ -2131,8 +2132,7 @@ int32_t mz_zip_entry_read_close(void *handle, uint32_t *crc32, int64_t *compress
|
||||
return err;
|
||||
}
|
||||
|
||||
int32_t mz_zip_entry_write_close(void *handle, uint32_t crc32, int64_t compressed_size,
|
||||
int64_t uncompressed_size) {
|
||||
int32_t mz_zip_entry_write_close(void *handle, uint32_t crc32, int64_t compressed_size, int64_t uncompressed_size) {
|
||||
mz_zip *zip = (mz_zip *)handle;
|
||||
int64_t end_disk_number = 0;
|
||||
int32_t err = MZ_OK;
|
||||
@ -2146,8 +2146,8 @@ int32_t mz_zip_entry_write_close(void *handle, uint32_t crc32, int64_t compresse
|
||||
if (!zip->entry_raw)
|
||||
crc32 = zip->entry_crc32;
|
||||
|
||||
mz_zip_print("Zip - Entry - Write Close (crc 0x%08" PRIx32 " cs %" PRId64 " ucs %" PRId64 ")\n",
|
||||
crc32, compressed_size, uncompressed_size);
|
||||
mz_zip_print("Zip - Entry - Write Close (crc 0x%08" PRIx32 " cs %" PRId64 " ucs %" PRId64 ")\n", crc32,
|
||||
compressed_size, uncompressed_size);
|
||||
|
||||
/* If sizes are not set, then read them from the compression stream */
|
||||
if (compressed_size < 0)
|
||||
@ -2169,17 +2169,15 @@ int32_t mz_zip_entry_write_close(void *handle, uint32_t crc32, int64_t compresse
|
||||
if local extrafield was saved with zip64 extrafield */
|
||||
|
||||
if (zip->file_info.flag & MZ_ZIP_FLAG_MASK_LOCAL_INFO)
|
||||
err = mz_zip_entry_write_descriptor(zip->stream,
|
||||
zip64, 0, compressed_size, 0);
|
||||
err = mz_zip_entry_write_descriptor(zip->stream, zip64, 0, compressed_size, 0);
|
||||
else
|
||||
err = mz_zip_entry_write_descriptor(zip->stream,
|
||||
zip64, crc32, compressed_size, uncompressed_size);
|
||||
err = mz_zip_entry_write_descriptor(zip->stream, zip64, crc32, compressed_size, uncompressed_size);
|
||||
}
|
||||
|
||||
/* Write file info to central directory */
|
||||
|
||||
mz_zip_print("Zip - Entry - Write cd (ucs %" PRId64 " cs %" PRId64 " crc 0x%08" PRIx32 ")\n",
|
||||
uncompressed_size, compressed_size, crc32);
|
||||
mz_zip_print("Zip - Entry - Write cd (ucs %" PRId64 " cs %" PRId64 " crc 0x%08" PRIx32 ")\n", uncompressed_size,
|
||||
compressed_size, crc32);
|
||||
|
||||
zip->file_info.crc = crc32;
|
||||
zip->file_info.compressed_size = compressed_size;
|
||||
@ -2246,12 +2244,11 @@ int32_t mz_zip_entry_seek_local_header(void *handle) {
|
||||
|
||||
mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, disk_number);
|
||||
|
||||
mz_zip_print("Zip - Entry - Seek local (disk %" PRId32 " offset %" PRId64 ")\n",
|
||||
disk_number, zip->file_info.disk_offset);
|
||||
mz_zip_print("Zip - Entry - Seek local (disk %" PRId32 " offset %" PRId64 ")\n", disk_number,
|
||||
zip->file_info.disk_offset);
|
||||
|
||||
/* Guard against seek overflows */
|
||||
if ((zip->disk_offset_shift > 0) &&
|
||||
(zip->file_info.disk_offset > (INT64_MAX - zip->disk_offset_shift)))
|
||||
if ((zip->disk_offset_shift > 0) && (zip->file_info.disk_offset > (INT64_MAX - zip->disk_offset_shift)))
|
||||
return MZ_FORMAT_ERROR;
|
||||
|
||||
return mz_stream_seek(zip->stream, zip->file_info.disk_offset + zip->disk_offset_shift, MZ_SEEK_SET);
|
||||
@ -2518,14 +2515,17 @@ int32_t mz_zip_attrib_convert(uint8_t src_sys, uint32_t src_attrib, uint8_t targ
|
||||
*target_attrib = src_attrib;
|
||||
return MZ_OK;
|
||||
}
|
||||
if ((target_sys == MZ_HOST_SYSTEM_UNIX) || (target_sys == MZ_HOST_SYSTEM_OSX_DARWIN) || (target_sys == MZ_HOST_SYSTEM_RISCOS))
|
||||
if ((target_sys == MZ_HOST_SYSTEM_UNIX) || (target_sys == MZ_HOST_SYSTEM_OSX_DARWIN) ||
|
||||
(target_sys == MZ_HOST_SYSTEM_RISCOS))
|
||||
return mz_zip_attrib_win32_to_posix(src_attrib, target_attrib);
|
||||
} else if ((src_sys == MZ_HOST_SYSTEM_UNIX) || (src_sys == MZ_HOST_SYSTEM_OSX_DARWIN) || (src_sys == MZ_HOST_SYSTEM_RISCOS)) {
|
||||
} else if ((src_sys == MZ_HOST_SYSTEM_UNIX) || (src_sys == MZ_HOST_SYSTEM_OSX_DARWIN) ||
|
||||
(src_sys == MZ_HOST_SYSTEM_RISCOS)) {
|
||||
/* If high bytes are set, it contains unix specific attributes */
|
||||
if ((src_attrib >> 16) != 0)
|
||||
src_attrib >>= 16;
|
||||
|
||||
if ((target_sys == MZ_HOST_SYSTEM_UNIX) || (target_sys == MZ_HOST_SYSTEM_OSX_DARWIN) || (target_sys == MZ_HOST_SYSTEM_RISCOS)) {
|
||||
if ((target_sys == MZ_HOST_SYSTEM_UNIX) || (target_sys == MZ_HOST_SYSTEM_OSX_DARWIN) ||
|
||||
(target_sys == MZ_HOST_SYSTEM_RISCOS)) {
|
||||
*target_attrib = src_attrib;
|
||||
return MZ_OK;
|
||||
}
|
||||
@ -2544,16 +2544,16 @@ int32_t mz_zip_attrib_posix_to_win32(uint32_t posix_attrib, uint32_t *win32_attr
|
||||
|
||||
/* S_IWUSR | S_IWGRP | S_IWOTH | S_IXUSR | S_IXGRP | S_IXOTH */
|
||||
if ((posix_attrib & 0000333) == 0 && (posix_attrib & 0000444) != 0)
|
||||
*win32_attrib |= 0x01; /* FILE_ATTRIBUTE_READONLY */
|
||||
*win32_attrib |= 0x01; /* FILE_ATTRIBUTE_READONLY */
|
||||
/* S_IFLNK */
|
||||
if ((posix_attrib & 0170000) == 0120000)
|
||||
*win32_attrib |= 0x400; /* FILE_ATTRIBUTE_REPARSE_POINT */
|
||||
*win32_attrib |= 0x400; /* FILE_ATTRIBUTE_REPARSE_POINT */
|
||||
/* S_IFDIR */
|
||||
else if ((posix_attrib & 0170000) == 0040000)
|
||||
*win32_attrib |= 0x10; /* FILE_ATTRIBUTE_DIRECTORY */
|
||||
*win32_attrib |= 0x10; /* FILE_ATTRIBUTE_DIRECTORY */
|
||||
/* S_IFREG */
|
||||
else
|
||||
*win32_attrib |= 0x80; /* FILE_ATTRIBUTE_NORMAL */
|
||||
*win32_attrib |= 0x80; /* FILE_ATTRIBUTE_NORMAL */
|
||||
|
||||
return MZ_OK;
|
||||
}
|
||||
@ -2562,18 +2562,18 @@ int32_t mz_zip_attrib_win32_to_posix(uint32_t win32_attrib, uint32_t *posix_attr
|
||||
if (!posix_attrib)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
*posix_attrib = 0000444; /* S_IRUSR | S_IRGRP | S_IROTH */
|
||||
*posix_attrib = 0000444; /* S_IRUSR | S_IRGRP | S_IROTH */
|
||||
/* FILE_ATTRIBUTE_READONLY */
|
||||
if ((win32_attrib & 0x01) == 0)
|
||||
*posix_attrib |= 0000222; /* S_IWUSR | S_IWGRP | S_IWOTH */
|
||||
*posix_attrib |= 0000222; /* S_IWUSR | S_IWGRP | S_IWOTH */
|
||||
/* FILE_ATTRIBUTE_REPARSE_POINT */
|
||||
if ((win32_attrib & 0x400) == 0x400)
|
||||
*posix_attrib |= 0120000; /* S_IFLNK */
|
||||
*posix_attrib |= 0120000; /* S_IFLNK */
|
||||
/* FILE_ATTRIBUTE_DIRECTORY */
|
||||
else if ((win32_attrib & 0x10) == 0x10)
|
||||
*posix_attrib |= 0040111; /* S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH */
|
||||
*posix_attrib |= 0040111; /* S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH */
|
||||
else
|
||||
*posix_attrib |= 0100000; /* S_IFREG */
|
||||
*posix_attrib |= 0100000; /* S_IFREG */
|
||||
|
||||
return MZ_OK;
|
||||
}
|
||||
@ -2611,8 +2611,8 @@ int32_t mz_zip_extrafield_find(void *stream, uint16_t type, int32_t max_seek, ui
|
||||
return MZ_EXIST_ERROR;
|
||||
}
|
||||
|
||||
int32_t mz_zip_extrafield_contains(const uint8_t *extrafield, int32_t extrafield_size,
|
||||
uint16_t type, uint16_t *length) {
|
||||
int32_t mz_zip_extrafield_contains(const uint8_t *extrafield, int32_t extrafield_size, uint16_t type,
|
||||
uint16_t *length) {
|
||||
void *file_extra_stream = NULL;
|
||||
int32_t err = MZ_OK;
|
||||
|
||||
@ -2651,11 +2651,9 @@ int32_t mz_zip_extrafield_write(void *stream, uint16_t type, uint16_t length) {
|
||||
|
||||
static int32_t mz_zip_invalid_date(const struct tm *ptm) {
|
||||
#define datevalue_in_range(min, max, value) ((min) <= (value) && (value) <= (max))
|
||||
return (!datevalue_in_range(0, 127 + 80, ptm->tm_year) || /* 1980-based year, allow 80 extra */
|
||||
!datevalue_in_range(0, 11, ptm->tm_mon) ||
|
||||
!datevalue_in_range(1, 31, ptm->tm_mday) ||
|
||||
!datevalue_in_range(0, 23, ptm->tm_hour) ||
|
||||
!datevalue_in_range(0, 59, ptm->tm_min) ||
|
||||
return (!datevalue_in_range(0, 127 + 80, ptm->tm_year) || /* 1980-based year, allow 80 extra */
|
||||
!datevalue_in_range(0, 11, ptm->tm_mon) || !datevalue_in_range(1, 31, ptm->tm_mday) ||
|
||||
!datevalue_in_range(0, 23, ptm->tm_hour) || !datevalue_in_range(0, 59, ptm->tm_min) ||
|
||||
!datevalue_in_range(0, 59, ptm->tm_sec));
|
||||
#undef datevalue_in_range
|
||||
}
|
||||
@ -2663,12 +2661,12 @@ static int32_t mz_zip_invalid_date(const struct tm *ptm) {
|
||||
static void mz_zip_dosdate_to_raw_tm(uint64_t dos_date, struct tm *ptm) {
|
||||
uint64_t date = (uint64_t)(dos_date >> 16);
|
||||
|
||||
ptm->tm_mday = (uint16_t)(date & 0x1f);
|
||||
ptm->tm_mon = (uint16_t)(((date & 0x1E0) / 0x20) - 1);
|
||||
ptm->tm_year = (uint16_t)(((date & 0x0FE00) / 0x0200) + 80);
|
||||
ptm->tm_hour = (uint16_t)((dos_date & 0xF800) / 0x800);
|
||||
ptm->tm_min = (uint16_t)((dos_date & 0x7E0) / 0x20);
|
||||
ptm->tm_sec = (uint16_t)(2 * (dos_date & 0x1f));
|
||||
ptm->tm_mday = (int16_t)(date & 0x1f);
|
||||
ptm->tm_mon = (int16_t)(((date & 0x1E0) / 0x20) - 1);
|
||||
ptm->tm_year = (int16_t)(((date & 0x0FE00) / 0x0200) + 80);
|
||||
ptm->tm_hour = (int16_t)((dos_date & 0xF800) / 0x800);
|
||||
ptm->tm_min = (int16_t)((dos_date & 0x7E0) / 0x20);
|
||||
ptm->tm_sec = (int16_t)(2 * (dos_date & 0x1f));
|
||||
ptm->tm_isdst = -1;
|
||||
}
|
||||
|
||||
@ -2689,7 +2687,11 @@ int32_t mz_zip_dosdate_to_tm(uint64_t dos_date, struct tm *ptm) {
|
||||
time_t mz_zip_dosdate_to_time_t(uint64_t dos_date) {
|
||||
struct tm ptm;
|
||||
mz_zip_dosdate_to_raw_tm(dos_date, &ptm);
|
||||
return mktime(&ptm);
|
||||
return mz_zip_tm_to_time_t(&ptm);
|
||||
}
|
||||
|
||||
time_t mz_zip_tm_to_time_t(struct tm *ptm) {
|
||||
return mktime(ptm);
|
||||
}
|
||||
|
||||
int32_t mz_zip_time_t_to_tm(time_t unix_time, struct tm *ptm) {
|
||||
@ -2732,7 +2734,8 @@ uint32_t mz_zip_tm_to_dosdate(const struct tm *ptm) {
|
||||
if (mz_zip_invalid_date(&fixed_tm))
|
||||
return 0;
|
||||
|
||||
return (((uint32_t)fixed_tm.tm_mday + (32 * ((uint32_t)fixed_tm.tm_mon + 1)) + (512 * (uint32_t)fixed_tm.tm_year)) << 16) |
|
||||
return (((uint32_t)fixed_tm.tm_mday + (32 * ((uint32_t)fixed_tm.tm_mon + 1)) + (512 * (uint32_t)fixed_tm.tm_year))
|
||||
<< 16) |
|
||||
(((uint32_t)fixed_tm.tm_sec / 2) + (32 * (uint32_t)fixed_tm.tm_min) + (2048 * (uint32_t)fixed_tm.tm_hour));
|
||||
}
|
||||
|
||||
@ -2750,8 +2753,7 @@ int32_t mz_zip_unix_to_ntfs_time(time_t unix_time, uint64_t *ntfs_time) {
|
||||
|
||||
int32_t mz_zip_path_compare(const char *path1, const char *path2, uint8_t ignore_case) {
|
||||
do {
|
||||
if ((*path1 == '\\' && *path2 == '/') ||
|
||||
(*path2 == '\\' && *path1 == '/')) {
|
||||
if ((*path1 == '\\' && *path2 == '/') || (*path2 == '\\' && *path1 == '/')) {
|
||||
/* Ignore comparison of path slashes */
|
||||
} else if (ignore_case) {
|
||||
if (tolower(*path1) != tolower(*path2))
|
||||
@ -2772,8 +2774,7 @@ int32_t mz_zip_path_compare(const char *path1, const char *path2, uint8_t ignore
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
const char* mz_zip_get_compression_method_string(int32_t compression_method)
|
||||
{
|
||||
const char *mz_zip_get_compression_method_string(int32_t compression_method) {
|
||||
const char *method = "?";
|
||||
switch (compression_method) {
|
||||
case MZ_COMPRESS_METHOD_STORE:
|
||||
|
91
mz_zip.h
91
mz_zip.h
@ -23,34 +23,31 @@ extern "C" {
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct mz_zip_file_s {
|
||||
uint16_t version_madeby; /* version made by */
|
||||
uint16_t version_needed; /* version needed to extract */
|
||||
uint16_t flag; /* general purpose bit flag */
|
||||
uint16_t compression_method; /* compression method */
|
||||
time_t modified_date; /* last modified date in unix time */
|
||||
time_t accessed_date; /* last accessed date in unix time */
|
||||
time_t creation_date; /* creation date in unix time */
|
||||
uint32_t crc; /* crc-32 */
|
||||
int64_t compressed_size; /* compressed size */
|
||||
int64_t uncompressed_size; /* uncompressed size */
|
||||
uint16_t filename_size; /* filename length */
|
||||
uint16_t extrafield_size; /* extra field length */
|
||||
uint16_t comment_size; /* file comment length */
|
||||
uint32_t disk_number; /* disk number start */
|
||||
int64_t disk_offset; /* relative offset of local header */
|
||||
uint16_t internal_fa; /* internal file attributes */
|
||||
uint32_t external_fa; /* external file attributes */
|
||||
|
||||
const char *filename; /* filename utf8 null-terminated string */
|
||||
const uint8_t *extrafield; /* extrafield data */
|
||||
const char *comment; /* comment utf8 null-terminated string */
|
||||
const char *linkname; /* sym-link filename utf8 null-terminated string */
|
||||
|
||||
uint16_t zip64; /* zip64 extension mode */
|
||||
uint16_t aes_version; /* winzip aes extension if not 0 */
|
||||
uint8_t aes_strength; /* winzip aes encryption strength */
|
||||
uint16_t pk_verify; /* pkware encryption verifier */
|
||||
|
||||
uint16_t version_madeby; /* version made by */
|
||||
uint16_t version_needed; /* version needed to extract */
|
||||
uint16_t flag; /* general purpose bit flag */
|
||||
uint16_t compression_method; /* compression method */
|
||||
time_t modified_date; /* last modified date in unix time */
|
||||
time_t accessed_date; /* last accessed date in unix time */
|
||||
time_t creation_date; /* creation date in unix time */
|
||||
uint32_t crc; /* crc-32 */
|
||||
int64_t compressed_size; /* compressed size */
|
||||
int64_t uncompressed_size; /* uncompressed size */
|
||||
uint16_t filename_size; /* filename length */
|
||||
uint16_t extrafield_size; /* extra field length */
|
||||
uint16_t comment_size; /* file comment length */
|
||||
uint32_t disk_number; /* disk number start */
|
||||
int64_t disk_offset; /* relative offset of local header */
|
||||
uint16_t internal_fa; /* internal file attributes */
|
||||
uint32_t external_fa; /* external file attributes */
|
||||
const char *filename; /* filename utf8 null-terminated string */
|
||||
const uint8_t *extrafield; /* extrafield data */
|
||||
const char *comment; /* comment utf8 null-terminated string */
|
||||
const char *linkname; /* sym-link filename utf8 null-terminated string */
|
||||
uint16_t zip64; /* zip64 extension mode */
|
||||
uint16_t aes_version; /* winzip aes extension if not 0 */
|
||||
uint8_t aes_strength; /* winzip aes encryption strength */
|
||||
uint16_t pk_verify; /* pkware encryption verifier */
|
||||
} mz_zip_file, mz_zip_entry;
|
||||
|
||||
/***************************************************************************/
|
||||
@ -59,10 +56,10 @@ typedef int32_t (*mz_zip_locate_entry_cb)(void *handle, void *userdata, mz_zip_f
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
void * mz_zip_create(void);
|
||||
void *mz_zip_create(void);
|
||||
/* Create zip instance for opening */
|
||||
|
||||
void mz_zip_delete(void **handle);
|
||||
void mz_zip_delete(void **handle);
|
||||
/* Delete zip object */
|
||||
|
||||
int32_t mz_zip_open(void *handle, void *stream, int32_t mode);
|
||||
@ -121,19 +118,17 @@ int32_t mz_zip_entry_read_open(void *handle, uint8_t raw, const char *password);
|
||||
int32_t mz_zip_entry_read(void *handle, void *buf, int32_t len);
|
||||
/* Read bytes from the current file in the zip file */
|
||||
|
||||
int32_t mz_zip_entry_read_close(void *handle, uint32_t *crc32, int64_t *compressed_size,
|
||||
int64_t *uncompressed_size);
|
||||
int32_t mz_zip_entry_read_close(void *handle, uint32_t *crc32, int64_t *compressed_size, int64_t *uncompressed_size);
|
||||
/* Close the current file for reading and get data descriptor values */
|
||||
|
||||
int32_t mz_zip_entry_write_open(void *handle, const mz_zip_file *file_info,
|
||||
int16_t compress_level, uint8_t raw, const char *password);
|
||||
int32_t mz_zip_entry_write_open(void *handle, const mz_zip_file *file_info, int16_t compress_level, uint8_t raw,
|
||||
const char *password);
|
||||
/* Open for writing the current file in the zip file */
|
||||
|
||||
int32_t mz_zip_entry_write(void *handle, const void *buf, int32_t len);
|
||||
/* Write bytes from the current file in the zip file */
|
||||
|
||||
int32_t mz_zip_entry_write_close(void *handle, uint32_t crc32, int64_t compressed_size,
|
||||
int64_t uncompressed_size);
|
||||
int32_t mz_zip_entry_write_close(void *handle, uint32_t crc32, int64_t compressed_size, int64_t uncompressed_size);
|
||||
/* Close the current file for writing and set data descriptor values */
|
||||
|
||||
int32_t mz_zip_entry_seek_local_header(void *handle);
|
||||
@ -194,8 +189,7 @@ int32_t mz_zip_attrib_is_dir(uint32_t attrib, int32_t version_madeby);
|
||||
int32_t mz_zip_attrib_is_symlink(uint32_t attrib, int32_t version_madeby);
|
||||
/* Checks to see if the attribute is a symbolic link based on platform */
|
||||
|
||||
int32_t mz_zip_attrib_convert(uint8_t src_sys, uint32_t src_attrib, uint8_t target_sys,
|
||||
uint32_t *target_attrib);
|
||||
int32_t mz_zip_attrib_convert(uint8_t src_sys, uint32_t src_attrib, uint8_t target_sys, uint32_t *target_attrib);
|
||||
/* Converts file attributes from one host system to another */
|
||||
|
||||
int32_t mz_zip_attrib_posix_to_win32(uint32_t posix_attrib, uint32_t *win32_attrib);
|
||||
@ -209,8 +203,7 @@ int32_t mz_zip_attrib_win32_to_posix(uint32_t win32_attrib, uint32_t *posix_attr
|
||||
int32_t mz_zip_extrafield_find(void *stream, uint16_t type, int32_t max_seek, uint16_t *length);
|
||||
/* Seeks to extra field by its type and returns its length */
|
||||
|
||||
int32_t mz_zip_extrafield_contains(const uint8_t *extrafield, int32_t extrafield_size,
|
||||
uint16_t type, uint16_t *length);
|
||||
int32_t mz_zip_extrafield_contains(const uint8_t *extrafield, int32_t extrafield_size, uint16_t type, uint16_t *length);
|
||||
/* Gets whether an extrafield exists and its size */
|
||||
|
||||
int32_t mz_zip_extrafield_read(void *stream, uint16_t *type, uint16_t *length);
|
||||
@ -221,13 +214,16 @@ int32_t mz_zip_extrafield_write(void *stream, uint16_t type, uint16_t length);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t mz_zip_dosdate_to_tm(uint64_t dos_date, struct tm *ptm);
|
||||
int32_t mz_zip_dosdate_to_tm(uint64_t dos_date, struct tm *ptm);
|
||||
/* Convert dos date/time format to struct tm */
|
||||
|
||||
time_t mz_zip_dosdate_to_time_t(uint64_t dos_date);
|
||||
time_t mz_zip_dosdate_to_time_t(uint64_t dos_date);
|
||||
/* Convert dos date/time format to time_t */
|
||||
|
||||
int32_t mz_zip_time_t_to_tm(time_t unix_time, struct tm *ptm);
|
||||
time_t mz_zip_tm_to_time_t(struct tm *ptm);
|
||||
/* Convert time struct to time_t */
|
||||
|
||||
int32_t mz_zip_time_t_to_tm(time_t unix_time, struct tm *ptm);
|
||||
/* Convert time_t to time struct */
|
||||
|
||||
uint32_t mz_zip_time_t_to_dos_date(time_t unix_time);
|
||||
@ -236,21 +232,20 @@ uint32_t mz_zip_time_t_to_dos_date(time_t unix_time);
|
||||
uint32_t mz_zip_tm_to_dosdate(const struct tm *ptm);
|
||||
/* Convert struct tm to dos date/time format */
|
||||
|
||||
int32_t mz_zip_ntfs_to_unix_time(uint64_t ntfs_time, time_t *unix_time);
|
||||
int32_t mz_zip_ntfs_to_unix_time(uint64_t ntfs_time, time_t *unix_time);
|
||||
/* Convert ntfs time to unix time */
|
||||
|
||||
int32_t mz_zip_unix_to_ntfs_time(time_t unix_time, uint64_t *ntfs_time);
|
||||
int32_t mz_zip_unix_to_ntfs_time(time_t unix_time, uint64_t *ntfs_time);
|
||||
/* Convert unix time to ntfs time */
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t mz_zip_path_compare(const char *path1, const char *path2, uint8_t ignore_case);
|
||||
int32_t mz_zip_path_compare(const char *path1, const char *path2, uint8_t ignore_case);
|
||||
/* Compare two paths without regard to slashes */
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
const
|
||||
char* mz_zip_get_compression_method_string(int32_t compression_method);
|
||||
const char *mz_zip_get_compression_method_string(int32_t compression_method);
|
||||
/* Gets a string representing the compression method */
|
||||
|
||||
/***************************************************************************/
|
||||
|
291
mz_zip_rw.c
291
mz_zip_rw.c
@ -23,46 +23,42 @@
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#define MZ_DEFAULT_PROGRESS_INTERVAL (1000u)
|
||||
#define MZ_DEFAULT_PROGRESS_INTERVAL (1000u)
|
||||
|
||||
#define MZ_ZIP_CD_FILENAME ("__cdcd__")
|
||||
#define MZ_ZIP_CD_FILENAME ("__cdcd__")
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct mz_zip_reader_s {
|
||||
void *zip_handle;
|
||||
void *file_stream;
|
||||
void *buffered_stream;
|
||||
void *split_stream;
|
||||
void *mem_stream;
|
||||
void *hash;
|
||||
uint16_t hash_algorithm;
|
||||
uint16_t hash_digest_size;
|
||||
void *zip_handle;
|
||||
void *file_stream;
|
||||
void *buffered_stream;
|
||||
void *split_stream;
|
||||
void *mem_stream;
|
||||
void *hash;
|
||||
uint16_t hash_algorithm;
|
||||
uint16_t hash_digest_size;
|
||||
mz_zip_file *file_info;
|
||||
const char *pattern;
|
||||
uint8_t pattern_ignore_case;
|
||||
const char *password;
|
||||
void *overwrite_userdata;
|
||||
mz_zip_reader_overwrite_cb
|
||||
overwrite_cb;
|
||||
void *password_userdata;
|
||||
mz_zip_reader_password_cb
|
||||
password_cb;
|
||||
void *progress_userdata;
|
||||
mz_zip_reader_progress_cb
|
||||
progress_cb;
|
||||
uint32_t progress_cb_interval_ms;
|
||||
void *entry_userdata;
|
||||
mz_zip_reader_entry_cb
|
||||
entry_cb;
|
||||
uint8_t raw;
|
||||
uint8_t buffer[UINT16_MAX];
|
||||
int32_t encoding;
|
||||
uint8_t sign_required;
|
||||
uint8_t cd_verified;
|
||||
uint8_t cd_zipped;
|
||||
uint8_t entry_verified;
|
||||
uint8_t recover;
|
||||
const char *pattern;
|
||||
uint8_t pattern_ignore_case;
|
||||
const char *password;
|
||||
void *overwrite_userdata;
|
||||
mz_zip_reader_overwrite_cb overwrite_cb;
|
||||
void *password_userdata;
|
||||
mz_zip_reader_password_cb password_cb;
|
||||
void *progress_userdata;
|
||||
mz_zip_reader_progress_cb progress_cb;
|
||||
uint32_t progress_cb_interval_ms;
|
||||
void *entry_userdata;
|
||||
mz_zip_reader_entry_cb entry_cb;
|
||||
uint8_t raw;
|
||||
uint8_t buffer[UINT16_MAX];
|
||||
int32_t encoding;
|
||||
uint8_t sign_required;
|
||||
uint8_t cd_verified;
|
||||
uint8_t cd_zipped;
|
||||
uint8_t entry_verified;
|
||||
uint8_t recover;
|
||||
} mz_zip_reader;
|
||||
|
||||
/***************************************************************************/
|
||||
@ -282,9 +278,10 @@ int32_t mz_zip_reader_unzip_cd(void *handle) {
|
||||
mz_stream_mem_open(cd_mem_stream, NULL, MZ_OPEN_MODE_CREATE);
|
||||
|
||||
err = mz_stream_seek(cd_mem_stream, 0, MZ_SEEK_SET);
|
||||
if (err == MZ_OK)
|
||||
if (err == MZ_OK) {
|
||||
err = mz_stream_copy_stream(cd_mem_stream, NULL, handle, mz_zip_reader_entry_read,
|
||||
(int32_t)cd_info->uncompressed_size);
|
||||
(int32_t)cd_info->uncompressed_size);
|
||||
}
|
||||
|
||||
if (err == MZ_OK) {
|
||||
reader->cd_zipped = 1;
|
||||
@ -394,8 +391,7 @@ int32_t mz_zip_reader_entry_open(void *handle) {
|
||||
|
||||
/* Check if we need a password and ask for it if we need to */
|
||||
if (!password && reader->password_cb && (reader->file_info->flag & MZ_ZIP_FLAG_ENCRYPTED)) {
|
||||
reader->password_cb(handle, reader->password_userdata, reader->file_info,
|
||||
password_buf, sizeof(password_buf));
|
||||
reader->password_cb(handle, reader->password_userdata, reader->file_info, password_buf, sizeof(password_buf));
|
||||
|
||||
password = password_buf;
|
||||
}
|
||||
@ -439,8 +435,8 @@ int32_t mz_zip_reader_entry_close(void *handle) {
|
||||
mz_crypt_sha_end(reader->hash, computed_hash, sizeof(computed_hash));
|
||||
mz_crypt_sha_delete(&reader->hash);
|
||||
|
||||
err_hash = mz_zip_reader_entry_get_hash(handle, reader->hash_algorithm, expected_hash,
|
||||
reader->hash_digest_size);
|
||||
err_hash =
|
||||
mz_zip_reader_entry_get_hash(handle, reader->hash_algorithm, expected_hash, reader->hash_digest_size);
|
||||
|
||||
if (err_hash == MZ_OK) {
|
||||
/* Verify expected hash against computed hash */
|
||||
@ -480,7 +476,7 @@ int32_t mz_zip_reader_entry_get_hash(void *handle, uint16_t algorithm, uint8_t *
|
||||
return MZ_MEM_ERROR;
|
||||
|
||||
mz_stream_mem_set_buffer(file_extra_stream, (void *)reader->file_info->extrafield,
|
||||
reader->file_info->extrafield_size);
|
||||
reader->file_info->extrafield_size);
|
||||
|
||||
do {
|
||||
err = mz_zip_extrafield_find(file_extra_stream, MZ_ZIP_EXTENSION_HASH, INT32_MAX, NULL);
|
||||
@ -521,7 +517,7 @@ int32_t mz_zip_reader_entry_get_first_hash(void *handle, uint16_t *algorithm, ui
|
||||
return MZ_MEM_ERROR;
|
||||
|
||||
mz_stream_mem_set_buffer(file_extra_stream, (void *)reader->file_info->extrafield,
|
||||
reader->file_info->extrafield_size);
|
||||
reader->file_info->extrafield_size);
|
||||
|
||||
err = mz_zip_extrafield_find(file_extra_stream, MZ_ZIP_EXTENSION_HASH, INT32_MAX, NULL);
|
||||
if (err == MZ_OK)
|
||||
@ -650,7 +646,6 @@ int32_t mz_zip_reader_entry_save_file(void *handle, const char *path) {
|
||||
int32_t err_attrib = 0;
|
||||
int32_t err = MZ_OK;
|
||||
int32_t err_cb = MZ_OK;
|
||||
size_t path_length = 0;
|
||||
char *pathwfs = NULL;
|
||||
char *directory = NULL;
|
||||
|
||||
@ -659,27 +654,22 @@ int32_t mz_zip_reader_entry_save_file(void *handle, const char *path) {
|
||||
if (!reader->file_info || !path)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
path_length = strlen(path);
|
||||
|
||||
/* Convert to forward slashes for unix which doesn't like backslashes */
|
||||
pathwfs = (char *)calloc(path_length + 1, sizeof(char));
|
||||
pathwfs = (char *)strdup(path);
|
||||
if (!pathwfs)
|
||||
return MZ_MEM_ERROR;
|
||||
strncat(pathwfs, path, path_length);
|
||||
mz_path_convert_slashes(pathwfs, MZ_PATH_SLASH_UNIX);
|
||||
|
||||
if (reader->entry_cb)
|
||||
reader->entry_cb(handle, reader->entry_userdata, reader->file_info, pathwfs);
|
||||
|
||||
directory = (char *)calloc(path_length + 1, sizeof(char));
|
||||
directory = (char *)strdup(pathwfs);
|
||||
if (!directory)
|
||||
return MZ_MEM_ERROR;
|
||||
strncat(directory, pathwfs, path_length);
|
||||
mz_path_remove_filename(directory);
|
||||
|
||||
/* If it is a directory entry then create a directory instead of writing file */
|
||||
if ((mz_zip_entry_is_dir(reader->zip_handle) == MZ_OK) &&
|
||||
(mz_zip_entry_is_symlink(reader->zip_handle) != MZ_OK)) {
|
||||
if ((mz_zip_entry_is_dir(reader->zip_handle) == MZ_OK) && (mz_zip_entry_is_symlink(reader->zip_handle) != MZ_OK)) {
|
||||
err = mz_dir_make(directory);
|
||||
goto save_cleanup;
|
||||
}
|
||||
@ -694,8 +684,7 @@ int32_t mz_zip_reader_entry_save_file(void *handle, const char *path) {
|
||||
}
|
||||
|
||||
/* If symbolic link then properly construct destination path and link path */
|
||||
if ((mz_zip_entry_is_symlink(reader->zip_handle) == MZ_OK) &&
|
||||
(mz_path_has_slash(pathwfs) == MZ_OK)) {
|
||||
if ((mz_zip_entry_is_symlink(reader->zip_handle) == MZ_OK) && (mz_path_has_slash(pathwfs) == MZ_OK)) {
|
||||
mz_path_remove_slash(pathwfs);
|
||||
mz_path_remove_filename(directory);
|
||||
}
|
||||
@ -759,14 +748,15 @@ int32_t mz_zip_reader_entry_save_file(void *handle, const char *path) {
|
||||
|
||||
if (err == MZ_OK) {
|
||||
/* Set the time of the file that has been created */
|
||||
mz_os_set_file_date(pathwfs, reader->file_info->modified_date,
|
||||
reader->file_info->accessed_date, reader->file_info->creation_date);
|
||||
mz_os_set_file_date(pathwfs, reader->file_info->modified_date, reader->file_info->accessed_date,
|
||||
reader->file_info->creation_date);
|
||||
}
|
||||
|
||||
if (err == MZ_OK) {
|
||||
/* Set file attributes for the correct system */
|
||||
err_attrib = mz_zip_attrib_convert(MZ_HOST_SYSTEM(reader->file_info->version_madeby),
|
||||
reader->file_info->external_fa, MZ_VERSION_MADEBY_HOST_SYSTEM, &target_attrib);
|
||||
err_attrib =
|
||||
mz_zip_attrib_convert(MZ_HOST_SYSTEM(reader->file_info->version_madeby), reader->file_info->external_fa,
|
||||
MZ_VERSION_MADEBY_HOST_SYSTEM, &target_attrib);
|
||||
|
||||
if (err_attrib == MZ_OK)
|
||||
mz_os_set_file_attribs(pathwfs, target_attrib);
|
||||
@ -829,6 +819,7 @@ int32_t mz_zip_reader_save_all(void *handle, const char *destination_dir) {
|
||||
char *path = NULL;
|
||||
char *utf8_name = NULL;
|
||||
char *resolved_name = NULL;
|
||||
char *new_alloc = NULL;
|
||||
|
||||
err = mz_zip_reader_goto_first_entry(handle);
|
||||
|
||||
@ -845,20 +836,24 @@ int32_t mz_zip_reader_save_all(void *handle, const char *destination_dir) {
|
||||
resolved_name_size += (int)strlen(destination_dir) + 1;
|
||||
}
|
||||
|
||||
if (!path) {
|
||||
path = (char *)malloc(resolved_name_size);
|
||||
utf8_name = (char *)malloc(utf8_name_size);
|
||||
resolved_name = (char *)malloc(resolved_name_size);
|
||||
} else {
|
||||
path = (char *)realloc(path, resolved_name_size);
|
||||
utf8_name = (char *)realloc(utf8_name, utf8_name_size);
|
||||
resolved_name = (char *)realloc(resolved_name, resolved_name_size);
|
||||
}
|
||||
|
||||
if (!path || !utf8_name || !resolved_name) {
|
||||
new_alloc = (char *)realloc(path, resolved_name_size);
|
||||
if (!new_alloc) {
|
||||
err = MZ_MEM_ERROR;
|
||||
goto save_all_cleanup;
|
||||
}
|
||||
path = new_alloc;
|
||||
new_alloc = (char *)realloc(utf8_name, utf8_name_size);
|
||||
if (!new_alloc) {
|
||||
err = MZ_MEM_ERROR;
|
||||
goto save_all_cleanup;
|
||||
}
|
||||
utf8_name = new_alloc;
|
||||
new_alloc = (char *)realloc(resolved_name, resolved_name_size);
|
||||
if (!new_alloc) {
|
||||
err = MZ_MEM_ERROR;
|
||||
goto save_all_cleanup;
|
||||
}
|
||||
resolved_name = new_alloc;
|
||||
|
||||
/* Construct output path */
|
||||
path[0] = 0;
|
||||
@ -869,7 +864,7 @@ int32_t mz_zip_reader_save_all(void *handle, const char *destination_dir) {
|
||||
if ((reader->encoding > 0) && (reader->file_info->flag & MZ_ZIP_FLAG_UTF8) == 0) {
|
||||
utf8_string = mz_os_utf8_string_create(reader->file_info->filename, reader->encoding);
|
||||
if (utf8_string) {
|
||||
strncpy(utf8_name, (char *)utf8_string, utf8_name_size - 1);
|
||||
strncpy(utf8_name, utf8_string, utf8_name_size - 1);
|
||||
utf8_name[utf8_name_size - 1] = 0;
|
||||
mz_os_utf8_string_delete(&utf8_string);
|
||||
}
|
||||
@ -1023,41 +1018,34 @@ void mz_zip_reader_delete(void **handle) {
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct mz_zip_writer_s {
|
||||
void *zip_handle;
|
||||
void *file_stream;
|
||||
void *buffered_stream;
|
||||
void *split_stream;
|
||||
void *hash;
|
||||
uint16_t hash_algorithm;
|
||||
void *mem_stream;
|
||||
void *file_extra_stream;
|
||||
void *zip_handle;
|
||||
void *file_stream;
|
||||
void *buffered_stream;
|
||||
void *split_stream;
|
||||
void *hash;
|
||||
uint16_t hash_algorithm;
|
||||
void *mem_stream;
|
||||
void *file_extra_stream;
|
||||
mz_zip_file file_info;
|
||||
void *overwrite_userdata;
|
||||
mz_zip_writer_overwrite_cb
|
||||
overwrite_cb;
|
||||
void *password_userdata;
|
||||
mz_zip_writer_password_cb
|
||||
password_cb;
|
||||
void *progress_userdata;
|
||||
mz_zip_writer_progress_cb
|
||||
progress_cb;
|
||||
uint32_t progress_cb_interval_ms;
|
||||
void *entry_userdata;
|
||||
mz_zip_writer_entry_cb
|
||||
entry_cb;
|
||||
const char *password;
|
||||
const char *comment;
|
||||
uint8_t *cert_data;
|
||||
int32_t cert_data_size;
|
||||
const char *cert_pwd;
|
||||
uint16_t compress_method;
|
||||
int16_t compress_level;
|
||||
uint8_t follow_links;
|
||||
uint8_t store_links;
|
||||
uint8_t zip_cd;
|
||||
uint8_t aes;
|
||||
uint8_t raw;
|
||||
uint8_t buffer[UINT16_MAX];
|
||||
void *overwrite_userdata;
|
||||
mz_zip_writer_overwrite_cb overwrite_cb;
|
||||
void *password_userdata;
|
||||
mz_zip_writer_password_cb password_cb;
|
||||
void *progress_userdata;
|
||||
mz_zip_writer_progress_cb progress_cb;
|
||||
uint32_t progress_cb_interval_ms;
|
||||
void *entry_userdata;
|
||||
mz_zip_writer_entry_cb entry_cb;
|
||||
const char *password;
|
||||
const char *comment;
|
||||
uint16_t compress_method;
|
||||
int16_t compress_level;
|
||||
uint8_t follow_links;
|
||||
uint8_t store_links;
|
||||
uint8_t zip_cd;
|
||||
uint8_t aes;
|
||||
uint8_t raw;
|
||||
uint8_t buffer[UINT16_MAX];
|
||||
} mz_zip_writer;
|
||||
|
||||
/***************************************************************************/
|
||||
@ -1106,8 +1094,7 @@ int32_t mz_zip_writer_zip_cd(void *handle) {
|
||||
|
||||
err = mz_zip_writer_entry_open(handle, &cd_file);
|
||||
if (err == MZ_OK) {
|
||||
mz_stream_copy_stream(handle, mz_zip_writer_entry_write, cd_mem_stream,
|
||||
NULL, (int32_t)cd_mem_length);
|
||||
mz_stream_copy_stream(handle, mz_zip_writer_entry_write, cd_mem_stream, NULL, (int32_t)cd_mem_length);
|
||||
|
||||
mz_stream_seek(cd_mem_stream, 0, MZ_SEEK_SET);
|
||||
mz_stream_mem_set_buffer_limit(cd_mem_stream, 0);
|
||||
@ -1324,8 +1311,7 @@ int32_t mz_zip_writer_entry_open(void *handle, mz_zip_file *file_info) {
|
||||
|
||||
/* Check if we need a password and ask for it if we need to */
|
||||
if (!password && writer->password_cb && (writer->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED)) {
|
||||
writer->password_cb(handle, writer->password_userdata, &writer->file_info,
|
||||
password_buf, sizeof(password_buf));
|
||||
writer->password_cb(handle, writer->password_userdata, &writer->file_info, password_buf, sizeof(password_buf));
|
||||
password = password_buf;
|
||||
}
|
||||
|
||||
@ -1347,8 +1333,10 @@ int32_t mz_zip_writer_entry_open(void *handle, mz_zip_file *file_info) {
|
||||
#endif
|
||||
|
||||
/* Open entry in zip */
|
||||
err = mz_zip_entry_write_open(writer->zip_handle, &writer->file_info, writer->compress_level,
|
||||
writer->raw, password);
|
||||
if (err == MZ_OK) {
|
||||
err = mz_zip_entry_write_open(writer->zip_handle, &writer->file_info, writer->compress_level, writer->raw,
|
||||
password);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
@ -1366,14 +1354,14 @@ int32_t mz_zip_writer_entry_close(void *handle) {
|
||||
uint16_t hash_digest_size = 0;
|
||||
|
||||
switch (writer->hash_algorithm) {
|
||||
case MZ_HASH_SHA1:
|
||||
hash_digest_size = MZ_HASH_SHA1_SIZE;
|
||||
break;
|
||||
case MZ_HASH_SHA256:
|
||||
hash_digest_size = MZ_HASH_SHA256_SIZE;
|
||||
break;
|
||||
default:
|
||||
return MZ_PARAM_ERROR;
|
||||
case MZ_HASH_SHA1:
|
||||
hash_digest_size = MZ_HASH_SHA1_SIZE;
|
||||
break;
|
||||
case MZ_HASH_SHA256:
|
||||
hash_digest_size = MZ_HASH_SHA256_SIZE;
|
||||
break;
|
||||
default:
|
||||
return MZ_PARAM_ERROR;
|
||||
}
|
||||
|
||||
mz_crypt_sha_end(writer->hash, hash_digest, hash_digest_size);
|
||||
@ -1398,9 +1386,10 @@ int32_t mz_zip_writer_entry_close(void *handle) {
|
||||
err = MZ_WRITE_ERROR;
|
||||
}
|
||||
|
||||
if ((writer->file_info.extrafield) && (writer->file_info.extrafield_size > 0))
|
||||
if (writer->file_info.extrafield && writer->file_info.extrafield_size > 0) {
|
||||
mz_stream_mem_write(writer->file_extra_stream, writer->file_info.extrafield,
|
||||
writer->file_info.extrafield_size);
|
||||
writer->file_info.extrafield_size);
|
||||
}
|
||||
|
||||
/* Update extra field for central directory after adding extra fields */
|
||||
mz_stream_mem_get_buffer(writer->file_extra_stream, (const void **)&extrafield);
|
||||
@ -1412,8 +1401,8 @@ int32_t mz_zip_writer_entry_close(void *handle) {
|
||||
|
||||
if (err == MZ_OK) {
|
||||
if (writer->raw)
|
||||
err = mz_zip_entry_close_raw(writer->zip_handle, writer->file_info.uncompressed_size,
|
||||
writer->file_info.crc);
|
||||
err =
|
||||
mz_zip_entry_close_raw(writer->zip_handle, writer->file_info.uncompressed_size, writer->file_info.crc);
|
||||
else
|
||||
err = mz_zip_entry_close(writer->zip_handle);
|
||||
}
|
||||
@ -1600,8 +1589,7 @@ int32_t mz_zip_writer_add_file(void *handle, const char *path, const char *filen
|
||||
if (writer->aes)
|
||||
file_info.aes_version = MZ_AES_VERSION;
|
||||
|
||||
mz_os_get_file_date(path, &file_info.modified_date, &file_info.accessed_date,
|
||||
&file_info.creation_date);
|
||||
mz_os_get_file_date(path, &file_info.modified_date, &file_info.accessed_date, &file_info.creation_date);
|
||||
mz_os_get_file_attribs(path, &src_attrib);
|
||||
|
||||
src_sys = MZ_HOST_SYSTEM(file_info.version_madeby);
|
||||
@ -1637,8 +1625,8 @@ int32_t mz_zip_writer_add_file(void *handle, const char *path, const char *filen
|
||||
return err;
|
||||
}
|
||||
|
||||
int32_t mz_zip_writer_add_path(void *handle, const char *path, const char *root_path,
|
||||
uint8_t include_path, uint8_t recursive) {
|
||||
int32_t mz_zip_writer_add_path(void *handle, const char *path, const char *root_path, uint8_t include_path,
|
||||
uint8_t recursive) {
|
||||
mz_zip_writer *writer = (mz_zip_writer *)handle;
|
||||
DIR *dir = NULL;
|
||||
struct dirent *entry = NULL;
|
||||
@ -1835,52 +1823,6 @@ void mz_zip_writer_set_zip_cd(void *handle, uint8_t zip_cd) {
|
||||
writer->zip_cd = zip_cd;
|
||||
}
|
||||
|
||||
int32_t mz_zip_writer_set_certificate(void *handle, const char *cert_path, const char *cert_pwd) {
|
||||
mz_zip_writer *writer = (mz_zip_writer *)handle;
|
||||
void *cert_stream = NULL;
|
||||
uint8_t *cert_data = NULL;
|
||||
int32_t cert_data_size = 0;
|
||||
int32_t err = MZ_OK;
|
||||
|
||||
if (!cert_path)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
cert_data_size = (int32_t)mz_os_get_file_size(cert_path);
|
||||
|
||||
if (cert_data_size == 0)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
if (writer->cert_data) {
|
||||
free(writer->cert_data);
|
||||
writer->cert_data = NULL;
|
||||
}
|
||||
|
||||
cert_data = (uint8_t *)malloc(cert_data_size);
|
||||
|
||||
/* Read pkcs12 certificate from disk */
|
||||
cert_stream = mz_stream_os_create();
|
||||
if (!cert_stream)
|
||||
return MZ_MEM_ERROR;
|
||||
|
||||
err = mz_stream_os_open(cert_stream, cert_path, MZ_OPEN_MODE_READ);
|
||||
if (err == MZ_OK) {
|
||||
if (mz_stream_os_read(cert_stream, cert_data, cert_data_size) != cert_data_size)
|
||||
err = MZ_READ_ERROR;
|
||||
mz_stream_os_close(cert_stream);
|
||||
}
|
||||
mz_stream_os_delete(&cert_stream);
|
||||
|
||||
if (err == MZ_OK) {
|
||||
writer->cert_data = cert_data;
|
||||
writer->cert_data_size = cert_data_size;
|
||||
writer->cert_pwd = cert_pwd;
|
||||
} else {
|
||||
free(cert_data);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
void mz_zip_writer_set_overwrite_cb(void *handle, void *userdata, mz_zip_writer_overwrite_cb cb) {
|
||||
mz_zip_writer *writer = (mz_zip_writer *)handle;
|
||||
writer->overwrite_cb = cb;
|
||||
@ -1950,13 +1892,6 @@ void mz_zip_writer_delete(void **handle) {
|
||||
writer = (mz_zip_writer *)*handle;
|
||||
if (writer) {
|
||||
mz_zip_writer_close(writer);
|
||||
|
||||
if (writer->cert_data)
|
||||
free(writer->cert_data);
|
||||
|
||||
writer->cert_data = NULL;
|
||||
writer->cert_data_size = 0;
|
||||
|
||||
free(writer);
|
||||
}
|
||||
*handle = NULL;
|
||||
|
62
mz_zip_rw.h
62
mz_zip_rw.h
@ -18,7 +18,8 @@ extern "C" {
|
||||
/***************************************************************************/
|
||||
|
||||
typedef int32_t (*mz_zip_reader_overwrite_cb)(void *handle, void *userdata, mz_zip_file *file_info, const char *path);
|
||||
typedef int32_t (*mz_zip_reader_password_cb)(void *handle, void *userdata, mz_zip_file *file_info, char *password, int32_t max_password);
|
||||
typedef int32_t (*mz_zip_reader_password_cb)(void *handle, void *userdata, mz_zip_file *file_info, char *password,
|
||||
int32_t max_password);
|
||||
typedef int32_t (*mz_zip_reader_progress_cb)(void *handle, void *userdata, mz_zip_file *file_info, int64_t position);
|
||||
typedef int32_t (*mz_zip_reader_entry_cb)(void *handle, void *userdata, mz_zip_file *file_info, const char *path);
|
||||
|
||||
@ -101,13 +102,13 @@ int32_t mz_zip_reader_save_all(void *handle, const char *destination_dir);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
void mz_zip_reader_set_pattern(void *handle, const char *pattern, uint8_t ignore_case);
|
||||
void mz_zip_reader_set_pattern(void *handle, const char *pattern, uint8_t ignore_case);
|
||||
/* Sets the match pattern for entries in the zip file, if null all entries are matched */
|
||||
|
||||
void mz_zip_reader_set_password(void *handle, const char *password);
|
||||
void mz_zip_reader_set_password(void *handle, const char *password);
|
||||
/* Sets the password required for extraction */
|
||||
|
||||
void mz_zip_reader_set_raw(void *handle, uint8_t raw);
|
||||
void mz_zip_reader_set_raw(void *handle, uint8_t raw);
|
||||
/* Sets whether or not it should save the entry raw */
|
||||
|
||||
int32_t mz_zip_reader_get_raw(void *handle, uint8_t *raw);
|
||||
@ -122,37 +123,38 @@ int32_t mz_zip_reader_get_comment(void *handle, const char **comment);
|
||||
int32_t mz_zip_reader_set_recover(void *handle, uint8_t recover);
|
||||
/* Sets the ability to recover the central dir by reading local file headers */
|
||||
|
||||
void mz_zip_reader_set_encoding(void *handle, int32_t encoding);
|
||||
void mz_zip_reader_set_encoding(void *handle, int32_t encoding);
|
||||
/* Sets whether or not it should support a special character encoding in zip file names. */
|
||||
|
||||
void mz_zip_reader_set_overwrite_cb(void *handle, void *userdata, mz_zip_reader_overwrite_cb cb);
|
||||
void mz_zip_reader_set_overwrite_cb(void *handle, void *userdata, mz_zip_reader_overwrite_cb cb);
|
||||
/* Callback for what to do when a file is being overwritten */
|
||||
|
||||
void mz_zip_reader_set_password_cb(void *handle, void *userdata, mz_zip_reader_password_cb cb);
|
||||
void mz_zip_reader_set_password_cb(void *handle, void *userdata, mz_zip_reader_password_cb cb);
|
||||
/* Callback for when a password is required and hasn't been set */
|
||||
|
||||
void mz_zip_reader_set_progress_cb(void *handle, void *userdata, mz_zip_reader_progress_cb cb);
|
||||
void mz_zip_reader_set_progress_cb(void *handle, void *userdata, mz_zip_reader_progress_cb cb);
|
||||
/* Callback for extraction progress */
|
||||
|
||||
void mz_zip_reader_set_progress_interval(void *handle, uint32_t milliseconds);
|
||||
void mz_zip_reader_set_progress_interval(void *handle, uint32_t milliseconds);
|
||||
/* Let at least milliseconds pass between calls to progress callback */
|
||||
|
||||
void mz_zip_reader_set_entry_cb(void *handle, void *userdata, mz_zip_reader_entry_cb cb);
|
||||
void mz_zip_reader_set_entry_cb(void *handle, void *userdata, mz_zip_reader_entry_cb cb);
|
||||
/* Callback for zip file entries */
|
||||
|
||||
int32_t mz_zip_reader_get_zip_handle(void *handle, void **zip_handle);
|
||||
/* Gets the underlying zip instance handle */
|
||||
|
||||
void* mz_zip_reader_create(void);
|
||||
void *mz_zip_reader_create(void);
|
||||
/* Create new instance of zip reader */
|
||||
|
||||
void mz_zip_reader_delete(void **handle);
|
||||
void mz_zip_reader_delete(void **handle);
|
||||
/* Delete instance of zip reader */
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef int32_t (*mz_zip_writer_overwrite_cb)(void *handle, void *userdata, const char *path);
|
||||
typedef int32_t (*mz_zip_writer_password_cb)(void *handle, void *userdata, mz_zip_file *file_info, char *password, int32_t max_password);
|
||||
typedef int32_t (*mz_zip_writer_password_cb)(void *handle, void *userdata, mz_zip_file *file_info, char *password,
|
||||
int32_t max_password);
|
||||
typedef int32_t (*mz_zip_writer_progress_cb)(void *handle, void *userdata, mz_zip_file *file_info, int64_t position);
|
||||
typedef int32_t (*mz_zip_writer_entry_cb)(void *handle, void *userdata, mz_zip_file *file_info);
|
||||
|
||||
@ -202,7 +204,7 @@ int32_t mz_zip_writer_add_file(void *handle, const char *path, const char *filen
|
||||
/* Adds an entry to the zip from a file */
|
||||
|
||||
int32_t mz_zip_writer_add_path(void *handle, const char *path, const char *root_path, uint8_t include_path,
|
||||
uint8_t recursive);
|
||||
uint8_t recursive);
|
||||
/* Enumerates a directory or pattern and adds entries to the zip */
|
||||
|
||||
int32_t mz_zip_writer_copy_from_reader(void *handle, void *reader);
|
||||
@ -210,61 +212,61 @@ int32_t mz_zip_writer_copy_from_reader(void *handle, void *reader);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
void mz_zip_writer_set_password(void *handle, const char *password);
|
||||
void mz_zip_writer_set_password(void *handle, const char *password);
|
||||
/* Password to use for encrypting files in the zip */
|
||||
|
||||
void mz_zip_writer_set_comment(void *handle, const char *comment);
|
||||
void mz_zip_writer_set_comment(void *handle, const char *comment);
|
||||
/* Comment to use for the archive */
|
||||
|
||||
void mz_zip_writer_set_raw(void *handle, uint8_t raw);
|
||||
void mz_zip_writer_set_raw(void *handle, uint8_t raw);
|
||||
/* Sets whether or not we should write the entry raw */
|
||||
|
||||
int32_t mz_zip_writer_get_raw(void *handle, uint8_t *raw);
|
||||
/* Gets whether or not we should write the entry raw */
|
||||
|
||||
void mz_zip_writer_set_aes(void *handle, uint8_t aes);
|
||||
void mz_zip_writer_set_aes(void *handle, uint8_t aes);
|
||||
/* Use aes encryption when adding files in zip */
|
||||
|
||||
void mz_zip_writer_set_compress_method(void *handle, uint16_t compress_method);
|
||||
void mz_zip_writer_set_compress_method(void *handle, uint16_t compress_method);
|
||||
/* Sets the compression method when adding files in zip */
|
||||
|
||||
void mz_zip_writer_set_compress_level(void *handle, int16_t compress_level);
|
||||
void mz_zip_writer_set_compress_level(void *handle, int16_t compress_level);
|
||||
/* Sets the compression level when adding files in zip */
|
||||
|
||||
void mz_zip_writer_set_follow_links(void *handle, uint8_t follow_links);
|
||||
void mz_zip_writer_set_follow_links(void *handle, uint8_t follow_links);
|
||||
/* Follow symbolic links when traversing directories and files to add */
|
||||
|
||||
void mz_zip_writer_set_store_links(void *handle, uint8_t store_links);
|
||||
void mz_zip_writer_set_store_links(void *handle, uint8_t store_links);
|
||||
/* Store symbolic links in zip file */
|
||||
|
||||
void mz_zip_writer_set_zip_cd(void *handle, uint8_t zip_cd);
|
||||
void mz_zip_writer_set_zip_cd(void *handle, uint8_t zip_cd);
|
||||
/* Sets whether or not central directory should be zipped */
|
||||
|
||||
int32_t mz_zip_writer_set_certificate(void *handle, const char *cert_path, const char *cert_pwd);
|
||||
/* Sets the certificate and timestamp url to use for signing when adding files in zip */
|
||||
|
||||
void mz_zip_writer_set_overwrite_cb(void *handle, void *userdata, mz_zip_writer_overwrite_cb cb);
|
||||
void mz_zip_writer_set_overwrite_cb(void *handle, void *userdata, mz_zip_writer_overwrite_cb cb);
|
||||
/* Callback for what to do when zip file already exists */
|
||||
|
||||
void mz_zip_writer_set_password_cb(void *handle, void *userdata, mz_zip_writer_password_cb cb);
|
||||
void mz_zip_writer_set_password_cb(void *handle, void *userdata, mz_zip_writer_password_cb cb);
|
||||
/* Callback for ask if a password is required for adding */
|
||||
|
||||
void mz_zip_writer_set_progress_cb(void *handle, void *userdata, mz_zip_writer_progress_cb cb);
|
||||
void mz_zip_writer_set_progress_cb(void *handle, void *userdata, mz_zip_writer_progress_cb cb);
|
||||
/* Callback for compression progress */
|
||||
|
||||
void mz_zip_writer_set_progress_interval(void *handle, uint32_t milliseconds);
|
||||
void mz_zip_writer_set_progress_interval(void *handle, uint32_t milliseconds);
|
||||
/* Let at least milliseconds pass between calls to progress callback */
|
||||
|
||||
void mz_zip_writer_set_entry_cb(void *handle, void *userdata, mz_zip_writer_entry_cb cb);
|
||||
void mz_zip_writer_set_entry_cb(void *handle, void *userdata, mz_zip_writer_entry_cb cb);
|
||||
/* Callback for zip file entries */
|
||||
|
||||
int32_t mz_zip_writer_get_zip_handle(void *handle, void **zip_handle);
|
||||
/* Gets the underlying zip handle */
|
||||
|
||||
void* mz_zip_writer_create(void);
|
||||
void *mz_zip_writer_create(void);
|
||||
/* Create new instance of zip writer */
|
||||
|
||||
void mz_zip_writer_delete(void **handle);
|
||||
void mz_zip_writer_delete(void **handle);
|
||||
/* Delete instance of zip writer */
|
||||
|
||||
/***************************************************************************/
|
||||
|
@ -26,7 +26,7 @@ if(NOT TARGET GTest::GTest)
|
||||
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS_EQUAL 5.3)
|
||||
set(GTEST_TAG release-1.10.0)
|
||||
else()
|
||||
set(GTEST_TAG release-1.11.0)
|
||||
set(GTEST_TAG v1.13.0)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
@ -58,6 +58,10 @@ if(NOT MZ_COMPRESS_ONLY AND NOT MZ_DECOMPRESS_ONLY)
|
||||
list(APPEND TEST_SRCS test_stream_compress.cc)
|
||||
endif()
|
||||
|
||||
if(MSVC)
|
||||
list(APPEND TEST_SRCS test_file.cc)
|
||||
endif()
|
||||
|
||||
add_executable(gtest_minizip test_main.cc ${TEST_SRCS})
|
||||
target_compile_definitions(gtest_minizip PRIVATE ${STDLIB_DEF} ${MINIZIP_DEF})
|
||||
target_include_directories(gtest_minizip PRIVATE
|
||||
|
@ -49,6 +49,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
|
||||
if (!handle)
|
||||
return 1;
|
||||
|
||||
mz_zip_set_recover(handle, (size & 0xE0) == 0xE0);
|
||||
err = mz_zip_open(handle, stream, MZ_OPEN_MODE_READ);
|
||||
|
||||
if (err == MZ_OK) {
|
||||
|
BIN
test/fuzz/unzip_fuzzer_seed_corpus/gh_739.zip
Normal file
BIN
test/fuzz/unzip_fuzzer_seed_corpus/gh_739.zip
Normal file
Binary file not shown.
BIN
test/fuzz/unzip_fuzzer_seed_corpus/gh_740.zip
Normal file
BIN
test/fuzz/unzip_fuzzer_seed_corpus/gh_740.zip
Normal file
Binary file not shown.
BIN
test/fuzz/unzip_fuzzer_seed_corpus/incorrect_number_entries.zip
Normal file
BIN
test/fuzz/unzip_fuzzer_seed_corpus/incorrect_number_entries.zip
Normal file
Binary file not shown.
@ -22,6 +22,7 @@ extern "C" {
|
||||
/***************************************************************************/
|
||||
|
||||
#define MZ_FUZZ_TEST_FILENAME "foo"
|
||||
#define MZ_FUZZ_TEST_PWD "test123"
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
@ -37,6 +38,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
|
||||
int64_t fuzz_pos = 0;
|
||||
int32_t fuzz_length = 0;
|
||||
uint8_t *fuzz_buf = NULL;
|
||||
const char *password = NULL;
|
||||
|
||||
fuzz_stream = mz_stream_mem_create();
|
||||
if (!fuzz_stream)
|
||||
@ -77,6 +79,14 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
|
||||
mz_stream_mem_delete(&fuzz_stream);
|
||||
return 1;
|
||||
}
|
||||
|
||||
err = mz_stream_mem_open(stream, MZ_FUZZ_TEST_FILENAME, MZ_OPEN_MODE_CREATE | MZ_OPEN_MODE_WRITE);
|
||||
if (err != MZ_OK) {
|
||||
mz_stream_mem_delete(&stream);
|
||||
mz_stream_mem_delete(&fuzz_stream);
|
||||
return 1;
|
||||
}
|
||||
|
||||
handle = mz_zip_create();
|
||||
if (!handle) {
|
||||
mz_stream_mem_delete(&stream);
|
||||
@ -86,7 +96,8 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
|
||||
|
||||
err = mz_zip_open(handle, stream, MZ_OPEN_MODE_CREATE | MZ_OPEN_MODE_WRITE);
|
||||
if (err == MZ_OK) {
|
||||
err = mz_zip_entry_write_open(handle, &file_info, compress_level, 0, NULL);
|
||||
password = file_info.flag & MZ_ZIP_FLAG_ENCRYPTED ? MZ_FUZZ_TEST_PWD : NULL;
|
||||
err = mz_zip_entry_write_open(handle, &file_info, compress_level, 0, password);
|
||||
if (err == MZ_OK) {
|
||||
mz_stream_mem_get_buffer_at_current(fuzz_stream, (const void **)&fuzz_buf);
|
||||
fuzz_pos = mz_stream_tell(fuzz_stream);
|
||||
|
@ -11,7 +11,10 @@
|
||||
#include "mz.h"
|
||||
#include "mz_os.h"
|
||||
#include "mz_zip.h"
|
||||
#include "mz_compat.h"
|
||||
|
||||
#include "unzip.h"
|
||||
#include "zip.h"
|
||||
#include "crypt.h"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
@ -24,8 +27,9 @@ static void test_zip_compat(zipFile zip, const char *filename, int32_t level) {
|
||||
memset(&file_info, 0, sizeof(file_info));
|
||||
file_info.dosDate = mz_zip_time_t_to_dos_date(1588561637);
|
||||
|
||||
EXPECT_EQ(err = zipOpenNewFileInZip(zip, filename, &file_info, NULL, 0, NULL, 0, "test local comment",
|
||||
Z_DEFLATED, level), ZIP_OK)
|
||||
EXPECT_EQ(
|
||||
err = zipOpenNewFileInZip(zip, filename, &file_info, NULL, 0, NULL, 0, "test local comment", Z_DEFLATED, level),
|
||||
ZIP_OK)
|
||||
<< "failed to open new file in zip (err: " << err << ")";
|
||||
if (err != ZIP_OK)
|
||||
return;
|
||||
@ -33,8 +37,7 @@ static void test_zip_compat(zipFile zip, const char *filename, int32_t level) {
|
||||
EXPECT_EQ(err = zipWriteInFileInZip(zip, buffer, (uint32_t)strlen(buffer)), ZIP_OK)
|
||||
<< "failed to write to file in zip (err: " << err << ")";
|
||||
|
||||
EXPECT_EQ(err = zipCloseFileInZip(zip), ZIP_OK)
|
||||
<< "failed to close file in zip (err: " << err << ")";
|
||||
EXPECT_EQ(err = zipCloseFileInZip(zip), ZIP_OK) << "failed to close file in zip (err: " << err << ")";
|
||||
}
|
||||
|
||||
TEST(compat, zip) {
|
||||
@ -75,23 +78,17 @@ static void test_unzip_compat(unzFile unzip) {
|
||||
|
||||
EXPECT_STREQ(comment, "test global comment");
|
||||
|
||||
EXPECT_EQ(err = unzGetGlobalInfo(unzip, &global_info), UNZ_OK)
|
||||
<< "global info (err: " << err << ")";
|
||||
EXPECT_EQ(err = unzGetGlobalInfo(unzip, &global_info), UNZ_OK) << "global info (err: " << err << ")";
|
||||
EXPECT_EQ(err = unzGetGlobalInfo64(unzip, &global_info64), UNZ_OK)
|
||||
<< "global info l info 64-bit (err: " << err << ")";
|
||||
|
||||
EXPECT_EQ(global_info.number_entry, 2)
|
||||
<< "invalid number of entries";
|
||||
EXPECT_EQ(global_info64.number_entry, 2)
|
||||
<< "invalid number of entries 64-bit";
|
||||
EXPECT_EQ(global_info.number_entry, 2) << "invalid number of entries";
|
||||
EXPECT_EQ(global_info64.number_entry, 2) << "invalid number of entries 64-bit";
|
||||
|
||||
EXPECT_EQ(global_info.number_disk_with_CD, 0)
|
||||
<< "invalid disk with cd";
|
||||
EXPECT_EQ(global_info64.number_disk_with_CD, 0)
|
||||
<< "invalid disk with cd 64-bit";
|
||||
EXPECT_EQ(global_info.number_disk_with_CD, 0) << "invalid disk with cd";
|
||||
EXPECT_EQ(global_info64.number_disk_with_CD, 0) << "invalid disk with cd 64-bit";
|
||||
|
||||
EXPECT_EQ(err = unzLocateFile(unzip, "test.txt", (unzFileNameComparer)(void *)1), UNZ_OK)
|
||||
<< "cannot locate test file (err: " << err << ")";
|
||||
EXPECT_EQ(err = unzLocateFile(unzip, "test.txt", 1), UNZ_OK) << "cannot locate test file (err: " << err << ")";
|
||||
|
||||
EXPECT_EQ(err = unzGoToFirstFile(unzip), UNZ_OK);
|
||||
if (err != UNZ_OK)
|
||||
@ -99,73 +96,66 @@ static void test_unzip_compat(unzFile unzip) {
|
||||
|
||||
EXPECT_EQ(err = unzGetCurrentFileInfo64(unzip, &file_info64, filename, sizeof(filename), NULL, 0, NULL, 0), UNZ_OK)
|
||||
<< "failed to get current file info 64-bit (err: " << err << ")";
|
||||
EXPECT_EQ(err = unzOpenCurrentFile(unzip), UNZ_OK)
|
||||
<< "failed to open current file (err: " << err << ")";
|
||||
EXPECT_EQ(err = unzOpenCurrentFile(unzip), UNZ_OK) << "failed to open current file (err: " << err << ")";
|
||||
|
||||
EXPECT_EQ(bytes_read = unzReadCurrentFile(unzip, buffer, sizeof(buffer)), (int32_t)strlen(test_data))
|
||||
<< "failed to read zip entry data (err: " << err << ")";
|
||||
|
||||
EXPECT_EQ(unzEndOfFile(unzip), 1)
|
||||
<< "end of zip not reported correctly";
|
||||
EXPECT_EQ(unzEndOfFile(unzip), 1) << "end of zip not reported correctly";
|
||||
|
||||
EXPECT_EQ(err = unzCloseCurrentFile(unzip), UNZ_OK)
|
||||
<< "failed to close current file (err: " << err << ")";
|
||||
EXPECT_EQ(err = unzCloseCurrentFile(unzip), UNZ_OK) << "failed to close current file (err: " << err << ")";
|
||||
|
||||
EXPECT_EQ(unztell(unzip), bytes_read)
|
||||
<< "unzip position not reported correctly";
|
||||
EXPECT_EQ(unztell(unzip), bytes_read) << "unzip position not reported correctly";
|
||||
|
||||
EXPECT_EQ(err = unzGoToNextFile(unzip), UNZ_OK);
|
||||
if (err != UNZ_OK)
|
||||
return;
|
||||
|
||||
comment[0] = 0;
|
||||
EXPECT_EQ(err = unzGetCurrentFileInfo(unzip, &file_info, filename, sizeof(filename), NULL, 0, comment, sizeof(comment)), UNZ_OK)
|
||||
EXPECT_EQ(
|
||||
err = unzGetCurrentFileInfo(unzip, &file_info, filename, sizeof(filename), NULL, 0, comment, sizeof(comment)),
|
||||
UNZ_OK)
|
||||
<< "failed to get current file info (err: " << err << ")";
|
||||
|
||||
EXPECT_STREQ(comment, "test local comment");
|
||||
|
||||
EXPECT_EQ(err = unzGetFilePos(unzip, &file_pos), UNZ_OK)
|
||||
<< "unexpected file position (err: " << err << ")";
|
||||
EXPECT_EQ(err = unzGetFilePos(unzip, &file_pos), UNZ_OK) << "unexpected file position (err: " << err << ")";
|
||||
|
||||
EXPECT_EQ(file_pos.num_of_file, 1)
|
||||
<< "invalid file position";
|
||||
EXPECT_EQ(file_pos.num_of_file, 1) << "invalid file position";
|
||||
|
||||
EXPECT_GT(unzGetOffset(unzip), 0)
|
||||
<< "invalid offset";
|
||||
EXPECT_GT(unzGetOffset(unzip), 0) << "invalid offset";
|
||||
|
||||
EXPECT_EQ(err = unzSeek64(unzip, 0, SEEK_SET), UNZ_OK)
|
||||
<< "cannot seek to beginning (err: " << err << ")";
|
||||
EXPECT_EQ(err = unzSeek64(unzip, 0, SEEK_SET), UNZ_OK) << "cannot seek to beginning (err: " << err << ")";
|
||||
|
||||
EXPECT_EQ(err = unzGoToNextFile(unzip), UNZ_END_OF_LIST_OF_FILE)
|
||||
<< "failed to reach end of list of files (err: " << err << ")";
|
||||
|
||||
EXPECT_EQ(err = unzSeek64(unzip, 0, SEEK_SET), UNZ_PARAMERROR)
|
||||
<< "cannot seek to beginning (err: " << err << ")";
|
||||
EXPECT_EQ(err = unzSeek64(unzip, 0, SEEK_SET), UNZ_PARAMERROR) << "cannot seek to beginning (err: " << err << ")";
|
||||
|
||||
unzCloseCurrentFile(unzip);
|
||||
}
|
||||
|
||||
#ifndef MZ_FILE32_API
|
||||
# ifndef NO_FSEEKO
|
||||
# define ftello64 ftello
|
||||
# define fseeko64 fseeko
|
||||
# elif defined(_MSC_VER) && (_MSC_VER >= 1400)
|
||||
# define ftello64 _ftelli64
|
||||
# define fseeko64 _fseeki64
|
||||
# ifndef MZ_FILE32_API
|
||||
# ifndef NO_FSEEKO
|
||||
# define ftello64 ftello
|
||||
# define fseeko64 fseeko
|
||||
# elif defined(_MSC_VER) && (_MSC_VER >= 1400)
|
||||
# define ftello64 _ftelli64
|
||||
# define fseeko64 _fseeki64
|
||||
# endif
|
||||
# endif
|
||||
# ifndef ftello64
|
||||
# define ftello64 ftell
|
||||
# endif
|
||||
# ifndef fseeko64
|
||||
# define fseeko64 fseek
|
||||
# endif
|
||||
#endif
|
||||
#ifndef ftello64
|
||||
# define ftello64 ftell
|
||||
#endif
|
||||
#ifndef fseeko64
|
||||
# define fseeko64 fseek
|
||||
#endif
|
||||
|
||||
static void *ZCALLBACK fopen_file_func(void *opaque, const char *filename, int mode) {
|
||||
FILE* file = NULL;
|
||||
const char* mode_fopen = NULL;
|
||||
FILE *file = NULL;
|
||||
const char *mode_fopen = NULL;
|
||||
|
||||
if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER)==ZLIB_FILEFUNC_MODE_READ)
|
||||
if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER) == ZLIB_FILEFUNC_MODE_READ)
|
||||
mode_fopen = "rb";
|
||||
else if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
|
||||
mode_fopen = "r+b";
|
||||
@ -197,8 +187,7 @@ static ZPOS64_T ZCALLBACK ftell64_file_func(void *opaque, void *stream) {
|
||||
static long ZCALLBACK fseek_file_func(void *opaque, void *stream, unsigned long offset, int origin) {
|
||||
int fseek_origin = 0;
|
||||
long ret = 0;
|
||||
switch (origin)
|
||||
{
|
||||
switch (origin) {
|
||||
case ZLIB_FILEFUNC_SEEK_CUR:
|
||||
fseek_origin = SEEK_CUR;
|
||||
break;
|
||||
@ -219,8 +208,7 @@ static long ZCALLBACK fseek_file_func(void *opaque, void *stream, unsigned long
|
||||
static long ZCALLBACK fseek64_file_func(void *opaque, void *stream, ZPOS64_T offset, int origin) {
|
||||
int fseek_origin = 0;
|
||||
long ret = 0;
|
||||
switch (origin)
|
||||
{
|
||||
switch (origin) {
|
||||
case ZLIB_FILEFUNC_SEEK_CUR:
|
||||
fseek_origin = SEEK_CUR;
|
||||
break;
|
||||
|
@ -68,9 +68,9 @@ TEST(crypt, sha1) {
|
||||
}
|
||||
|
||||
TEST(crypt, sha224) {
|
||||
#if GTEST_OS_WINDOWS
|
||||
# if GTEST_OS_WINDOWS
|
||||
GTEST_SKIP() << "SHA224 not supported on Windows";
|
||||
#else
|
||||
# else
|
||||
void *sha224 = nullptr;
|
||||
uint8_t hash224[MZ_HASH_SHA224_SIZE];
|
||||
char computed_hash[256];
|
||||
@ -88,13 +88,13 @@ TEST(crypt, sha224) {
|
||||
convert_buffer_to_hex_string(hash224, sizeof(hash224), computed_hash, sizeof(computed_hash));
|
||||
|
||||
EXPECT_STREQ(computed_hash, "9e444f5f0b6582a923bd48696155f4a2f0d914e044cb64b8729a6600");
|
||||
#endif
|
||||
# endif
|
||||
}
|
||||
|
||||
TEST(crypt, sha256) {
|
||||
#if GTEST_OS_WINDOWS && _WIN32_WINNT <= _WIN32_WINNT_XP
|
||||
# if GTEST_OS_WINDOWS && _WIN32_WINNT <= _WIN32_WINNT_XP
|
||||
GTEST_SKIP() << "SHA256 not supported on Windows XP";
|
||||
#else
|
||||
# else
|
||||
void *sha256 = nullptr;
|
||||
uint8_t hash256[MZ_HASH_SHA256_SIZE];
|
||||
char computed_hash[256];
|
||||
@ -112,13 +112,13 @@ TEST(crypt, sha256) {
|
||||
convert_buffer_to_hex_string(hash256, sizeof(hash256), computed_hash, sizeof(computed_hash));
|
||||
|
||||
EXPECT_STREQ(computed_hash, "7a31ea0848525f7ebfeec9ee532bcc5d6d26772427e097b86cf440a56546541c");
|
||||
#endif
|
||||
# endif
|
||||
}
|
||||
|
||||
TEST(crypt, sha384) {
|
||||
#if GTEST_OS_WINDOWS && _WIN32_WINNT <= _WIN32_WINNT_XP
|
||||
# if GTEST_OS_WINDOWS && _WIN32_WINNT <= _WIN32_WINNT_XP
|
||||
GTEST_SKIP() << "SHA384 not supported on Windows XP";
|
||||
#else
|
||||
# else
|
||||
void *sha384 = nullptr;
|
||||
uint8_t hash384[MZ_HASH_SHA384_SIZE];
|
||||
char computed_hash[256];
|
||||
@ -135,14 +135,15 @@ TEST(crypt, sha384) {
|
||||
|
||||
convert_buffer_to_hex_string(hash384, sizeof(hash384), computed_hash, sizeof(computed_hash));
|
||||
|
||||
EXPECT_STREQ(computed_hash, "e1e42e5977965bb3621231a5df3a1e83c471fa91fde33b6a30c8c4fa0d8be29ba7171c7c9487db91e9ee7e85049f7b41");
|
||||
#endif
|
||||
EXPECT_STREQ(computed_hash,
|
||||
"e1e42e5977965bb3621231a5df3a1e83c471fa91fde33b6a30c8c4fa0d8be29ba7171c7c9487db91e9ee7e85049f7b41");
|
||||
# endif
|
||||
}
|
||||
|
||||
TEST(crypt, sha512) {
|
||||
#if GTEST_OS_WINDOWS && _WIN32_WINNT <= _WIN32_WINNT_XP
|
||||
# if GTEST_OS_WINDOWS && _WIN32_WINNT <= _WIN32_WINNT_XP
|
||||
GTEST_SKIP() << "SHA512 not supported on Windows XP";
|
||||
#else
|
||||
# else
|
||||
void *sha512 = nullptr;
|
||||
uint8_t hash512[MZ_HASH_SHA512_SIZE];
|
||||
char computed_hash[256];
|
||||
@ -159,8 +160,10 @@ TEST(crypt, sha512) {
|
||||
|
||||
convert_buffer_to_hex_string(hash512, sizeof(hash512), computed_hash, sizeof(computed_hash));
|
||||
|
||||
EXPECT_STREQ(computed_hash, "6627e7643ee7ce633e03f52d22329c3a32597364247c5275d4369985e1518626da46f595ad327667346479d246359b8b381af791ce2ac8c53a4788050eea11fe");
|
||||
#endif
|
||||
EXPECT_STREQ(computed_hash,
|
||||
"6627e7643ee7ce633e03f52d22329c3a32597364247c5275d4369985e1518626da46f595ad327667346479d246359b8b381af"
|
||||
"791ce2ac8c53a4788050eea11fe");
|
||||
# endif
|
||||
}
|
||||
|
||||
TEST(crypt, aes128) {
|
||||
@ -228,15 +231,14 @@ TEST(crypt, aes128_cbc) {
|
||||
EXPECT_STREQ((char *)buf, test);
|
||||
}
|
||||
|
||||
|
||||
TEST(crypt, aes128_gcm) {
|
||||
#if GTEST_OS_WINDOWS && _WIN32_WINNT <= _WIN32_WINNT_XP
|
||||
# if GTEST_OS_WINDOWS && _WIN32_WINNT <= _WIN32_WINNT_XP
|
||||
GTEST_SKIP() << "SHA256 not supported on Windows XP";
|
||||
#else
|
||||
void* aes = nullptr;
|
||||
const char* key = "awesomekeythisis";
|
||||
const char* test = "youknowitsogrowi";
|
||||
const char* iv = "0123456789123456";
|
||||
# else
|
||||
void *aes = nullptr;
|
||||
const char *key = "awesomekeythisis";
|
||||
const char *test = "youknowitsogrowi";
|
||||
const char *iv = "0123456789123456";
|
||||
const char *aad = "additional authentication data";
|
||||
int32_t key_length = 0;
|
||||
int32_t test_length = 0;
|
||||
@ -250,8 +252,8 @@ TEST(crypt, aes128_gcm) {
|
||||
iv_length = (int32_t)strlen(iv);
|
||||
aad_length = (int32_t)strlen(aad);
|
||||
|
||||
strncpy((char*)buf, test, sizeof(buf));
|
||||
strncpy((char*)buf + test_length, test, sizeof(buf) - test_length);
|
||||
strncpy((char *)buf, test, sizeof(buf));
|
||||
strncpy((char *)buf + test_length, test, sizeof(buf) - test_length);
|
||||
|
||||
aes = mz_crypt_aes_create();
|
||||
ASSERT_NE(aes, nullptr);
|
||||
@ -261,7 +263,7 @@ TEST(crypt, aes128_gcm) {
|
||||
EXPECT_EQ(mz_crypt_aes_encrypt_final(aes, buf + test_length, test_length - 1, tag, sizeof(tag)), test_length - 1);
|
||||
mz_crypt_aes_delete(&aes);
|
||||
|
||||
EXPECT_STRNE((char*)buf, test);
|
||||
EXPECT_STRNE((char *)buf, test);
|
||||
|
||||
aes = mz_crypt_aes_create();
|
||||
ASSERT_NE(aes, nullptr);
|
||||
@ -273,7 +275,7 @@ TEST(crypt, aes128_gcm) {
|
||||
|
||||
EXPECT_EQ(memcmp(buf, test, test_length), 0);
|
||||
EXPECT_EQ(memcmp(buf + test_length, test, test_length - 1), 0);
|
||||
#endif
|
||||
# endif
|
||||
}
|
||||
|
||||
TEST(crypt, aes194) {
|
||||
@ -381,9 +383,9 @@ TEST(crypt, hmac_sha1_short_password) {
|
||||
}
|
||||
|
||||
TEST(crypt, hmac_sha256) {
|
||||
#if GTEST_OS_WINDOWS && _WIN32_WINNT <= _WIN32_WINNT_XP
|
||||
# if GTEST_OS_WINDOWS && _WIN32_WINNT <= _WIN32_WINNT_XP
|
||||
GTEST_SKIP() << "SHA256 not supported on Windows XP";
|
||||
#else
|
||||
# else
|
||||
void *hmac;
|
||||
const char *key = "hm123";
|
||||
const char *test = "12345678";
|
||||
@ -401,10 +403,10 @@ TEST(crypt, hmac_sha256) {
|
||||
convert_buffer_to_hex_string(hash256, sizeof(hash256), computed_hash, sizeof(computed_hash));
|
||||
|
||||
EXPECT_STREQ(computed_hash, "fb22a9c715a47a06bad4f6cee9badc31c921562f5d6b24adf2be009f73181f7a");
|
||||
#endif
|
||||
# endif
|
||||
}
|
||||
|
||||
#ifdef HAVE_WZAES
|
||||
# ifdef HAVE_WZAES
|
||||
TEST(crypt, pbkdf2) {
|
||||
uint16_t iteration_count = 1000;
|
||||
uint8_t key[MZ_HASH_SHA1_SIZE];
|
||||
@ -412,8 +414,9 @@ TEST(crypt, pbkdf2) {
|
||||
const char *password = "passwordpasswordpasswordpassword";
|
||||
const char *salt = "8F3472E4EA57F56E36F30246DC22C173";
|
||||
|
||||
EXPECT_EQ(mz_crypt_pbkdf2((uint8_t *)password, (int32_t)strlen(password),
|
||||
(uint8_t *)salt, (int32_t)strlen(salt), iteration_count, key, (uint16_t)sizeof(key)), MZ_OK);
|
||||
EXPECT_EQ(mz_crypt_pbkdf2((uint8_t *)password, (int32_t)strlen(password), (uint8_t *)salt, (int32_t)strlen(salt),
|
||||
iteration_count, key, (uint16_t)sizeof(key)),
|
||||
MZ_OK);
|
||||
|
||||
convert_buffer_to_hex_string(key, sizeof(key), key_hex, sizeof(key_hex));
|
||||
|
||||
@ -428,8 +431,9 @@ TEST(crypt, pbkdf2_long_odd_password) {
|
||||
const char *password = "passwordpasswordpasswordpasswordpasswordpasswordpasswordpasswordp";
|
||||
const char *salt = "8F3472E4EA57F56E36F30246DC22C173";
|
||||
|
||||
EXPECT_EQ(mz_crypt_pbkdf2((uint8_t *)password, (int32_t)strlen(password),
|
||||
(uint8_t *)salt, (int32_t)strlen(salt), iteration_count, key, (uint16_t)sizeof(key)), MZ_OK);
|
||||
EXPECT_EQ(mz_crypt_pbkdf2((uint8_t *)password, (int32_t)strlen(password), (uint8_t *)salt, (int32_t)strlen(salt),
|
||||
iteration_count, key, (uint16_t)sizeof(key)),
|
||||
MZ_OK);
|
||||
|
||||
convert_buffer_to_hex_string(key, sizeof(key), key_hex, sizeof(key_hex));
|
||||
|
||||
@ -444,8 +448,9 @@ TEST(crypt, pbkdf2_short_password) {
|
||||
const char *password = "p";
|
||||
const char *salt = "8F3472E4EA57F56E36F30246DC22C173";
|
||||
|
||||
EXPECT_EQ(mz_crypt_pbkdf2((uint8_t *)password, (int32_t)strlen(password),
|
||||
(uint8_t *)salt, (int32_t)strlen(salt), iteration_count, key, (uint16_t)sizeof(key)), MZ_OK);
|
||||
EXPECT_EQ(mz_crypt_pbkdf2((uint8_t *)password, (int32_t)strlen(password), (uint8_t *)salt, (int32_t)strlen(salt),
|
||||
iteration_count, key, (uint16_t)sizeof(key)),
|
||||
MZ_OK);
|
||||
|
||||
convert_buffer_to_hex_string(key, sizeof(key), key_hex, sizeof(key_hex));
|
||||
|
||||
@ -460,8 +465,9 @@ TEST(crypt, pbkdf2_rfc6070_v1) {
|
||||
const char *password = "password";
|
||||
const char *salt = "salt";
|
||||
|
||||
EXPECT_EQ(mz_crypt_pbkdf2((uint8_t *)password, (int32_t)strlen(password),
|
||||
(uint8_t *)salt, (int32_t)strlen(salt), iteration_count, key, (uint16_t)sizeof(key)), MZ_OK);
|
||||
EXPECT_EQ(mz_crypt_pbkdf2((uint8_t *)password, (int32_t)strlen(password), (uint8_t *)salt, (int32_t)strlen(salt),
|
||||
iteration_count, key, (uint16_t)sizeof(key)),
|
||||
MZ_OK);
|
||||
|
||||
convert_buffer_to_hex_string(key, sizeof(key), key_hex, sizeof(key_hex));
|
||||
|
||||
@ -476,8 +482,9 @@ TEST(crypt, pbkdf2_rfc6070_v2) {
|
||||
const char *password = "password";
|
||||
const char *salt = "salt";
|
||||
|
||||
EXPECT_EQ(mz_crypt_pbkdf2((uint8_t *)password, (int32_t)strlen(password),
|
||||
(uint8_t *)salt, (int32_t)strlen(salt), iteration_count, key, (uint16_t)sizeof(key)), MZ_OK);
|
||||
EXPECT_EQ(mz_crypt_pbkdf2((uint8_t *)password, (int32_t)strlen(password), (uint8_t *)salt, (int32_t)strlen(salt),
|
||||
iteration_count, key, (uint16_t)sizeof(key)),
|
||||
MZ_OK);
|
||||
|
||||
convert_buffer_to_hex_string(key, sizeof(key), key_hex, sizeof(key_hex));
|
||||
|
||||
@ -492,8 +499,9 @@ TEST(crypt, pbkdf2_rfc6070_v3) {
|
||||
const char *password = "password";
|
||||
const char *salt = "salt";
|
||||
|
||||
EXPECT_EQ(mz_crypt_pbkdf2((uint8_t *)password, (int32_t)strlen(password),
|
||||
(uint8_t *)salt, (int32_t)strlen(salt), iteration_count, key, (uint16_t)sizeof(key)), MZ_OK);
|
||||
EXPECT_EQ(mz_crypt_pbkdf2((uint8_t *)password, (int32_t)strlen(password), (uint8_t *)salt, (int32_t)strlen(salt),
|
||||
iteration_count, key, (uint16_t)sizeof(key)),
|
||||
MZ_OK);
|
||||
|
||||
convert_buffer_to_hex_string(key, sizeof(key), key_hex, sizeof(key_hex));
|
||||
|
||||
@ -508,8 +516,9 @@ TEST(crypt, pbkdf2_rfc6070_v4) {
|
||||
const char *password = "passwordPASSWORDpassword";
|
||||
const char *salt = "saltSALTsaltSALTsaltSALTsaltSALTsalt";
|
||||
|
||||
EXPECT_EQ(mz_crypt_pbkdf2((uint8_t *)password, (int32_t)strlen(password),
|
||||
(uint8_t *)salt, (int32_t)strlen(salt), iteration_count, key, (uint16_t)sizeof(key)), MZ_OK);
|
||||
EXPECT_EQ(mz_crypt_pbkdf2((uint8_t *)password, (int32_t)strlen(password), (uint8_t *)salt, (int32_t)strlen(salt),
|
||||
iteration_count, key, (uint16_t)sizeof(key)),
|
||||
MZ_OK);
|
||||
|
||||
convert_buffer_to_hex_string(key, sizeof(key), key_hex, sizeof(key_hex));
|
||||
|
||||
@ -524,12 +533,12 @@ TEST(crypt, pbkdf2_rfc6070_v5) {
|
||||
const char *password = "pass\0word";
|
||||
const char *salt = "sa\0lt";
|
||||
|
||||
EXPECT_EQ(mz_crypt_pbkdf2((uint8_t *)password, 9,
|
||||
(uint8_t *)salt, 5, iteration_count, key, (uint16_t)sizeof(key)), MZ_OK);
|
||||
EXPECT_EQ(mz_crypt_pbkdf2((uint8_t *)password, 9, (uint8_t *)salt, 5, iteration_count, key, (uint16_t)sizeof(key)),
|
||||
MZ_OK);
|
||||
|
||||
convert_buffer_to_hex_string(key, sizeof(key), key_hex, sizeof(key_hex));
|
||||
|
||||
EXPECT_STREQ(key_hex, "56fa6aa75548099dcc37d7f03425e0c3");
|
||||
}
|
||||
#endif
|
||||
# endif
|
||||
#endif
|
||||
|
44
test/test_file.cc
Normal file
44
test/test_file.cc
Normal file
@ -0,0 +1,44 @@
|
||||
/* test_file.cc - Test file functionality
|
||||
part of the minizip-ng project
|
||||
|
||||
Copyright (C) Nathan Moinvaziri
|
||||
https://github.com/zlib-ng/minizip-ng
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
#include "mz.h"
|
||||
#include "mz_os.h"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <fstream>
|
||||
#include <cstdio>
|
||||
|
||||
TEST(os, get_file_date_ads) {
|
||||
const std::string main_stream_name = "minizip_ads_test";
|
||||
const std::string ads_name = main_stream_name + ":ads";
|
||||
const std::string ads_contents = "Alternate Data Stream";
|
||||
|
||||
// Create main stream
|
||||
std::ofstream main_stream(main_stream_name);
|
||||
main_stream.close();
|
||||
|
||||
// Attach ADS
|
||||
std::ofstream ads(ads_name);
|
||||
ads << ads_contents;
|
||||
ads.close();
|
||||
|
||||
// Get file date
|
||||
time_t modified_date = 0;
|
||||
time_t accessed_date = 0;
|
||||
time_t creation_date = 0;
|
||||
|
||||
EXPECT_EQ(MZ_OK, mz_os_get_file_date(ads_name.c_str(), &modified_date, &accessed_date, &creation_date));
|
||||
|
||||
std::remove(main_stream_name.c_str());
|
||||
|
||||
ASSERT_GT(modified_date, 0);
|
||||
ASSERT_GT(accessed_date, 0);
|
||||
ASSERT_GT(creation_date, 0);
|
||||
}
|
@ -23,31 +23,29 @@ struct resolve_path_param {
|
||||
};
|
||||
|
||||
constexpr resolve_path_param resolve_path_tests[] = {
|
||||
{ "c:\\test\\.", "c:\\test\\" },
|
||||
{ "c:\\test\\.\\", "c:\\test\\" },
|
||||
{ "c:\\test\\.\\.", "c:\\test\\" },
|
||||
{ "c:\\test\\..", "c:\\" },
|
||||
{ "c:\\test\\..\\", "c:\\" },
|
||||
{ "c:\\test\\.\\..", "c:\\" },
|
||||
{ "c:\\test\\.\\\\..", "c:\\" },
|
||||
{ ".", "." },
|
||||
{ ".\\", "" },
|
||||
{ "..", "" },
|
||||
{ "..\\", "" },
|
||||
{ ".\\test\\123", "test\\123" },
|
||||
{ ".\\..\\test\\123", "test\\123" },
|
||||
{ "..\\..\\test\\123", "test\\123" },
|
||||
{ "test\\.abc.txt", "test\\.abc.txt" },
|
||||
{ "c:\\test\\123\\.\\abc.txt", "c:\\test\\123\\abc.txt" },
|
||||
{ "c:\\test\\123\\..\\abc.txt", "c:\\test\\abc.txt" },
|
||||
{ "c:\\test\\123\\..\\..\\abc.txt", "c:\\abc.txt" },
|
||||
{ "c:\\test\\123\\..\\..\\..\\abc.txt", "abc.txt" },
|
||||
{ "c:\\test\\123\\..\\.\\..\\abc.txt", "c:\\abc.txt" },
|
||||
{ "c:\\test\\.", "c:\\test\\"},
|
||||
{ "c:\\test\\.\\", "c:\\test\\"},
|
||||
{ "c:\\test\\.\\.", "c:\\test\\"},
|
||||
{ "c:\\test\\..", "c:\\"},
|
||||
{ "c:\\test\\..\\", "c:\\"},
|
||||
{ "c:\\test\\.\\..", "c:\\"},
|
||||
{ "c:\\test\\.\\\\..", "c:\\"},
|
||||
{ ".", "."},
|
||||
{ ".\\", ""},
|
||||
{ "..", ""},
|
||||
{ "..\\", ""},
|
||||
{ ".\\test\\123", "test\\123"},
|
||||
{ ".\\..\\test\\123", "test\\123"},
|
||||
{ "..\\..\\test\\123", "test\\123"},
|
||||
{ "test\\.abc.txt", "test\\.abc.txt"},
|
||||
{ "c:\\test\\123\\.\\abc.txt", "c:\\test\\123\\abc.txt"},
|
||||
{ "c:\\test\\123\\..\\abc.txt", "c:\\test\\abc.txt"},
|
||||
{ "c:\\test\\123\\..\\..\\abc.txt", "c:\\abc.txt"},
|
||||
{"c:\\test\\123\\..\\..\\..\\abc.txt", "abc.txt"},
|
||||
{ "c:\\test\\123\\..\\.\\..\\abc.txt", "c:\\abc.txt"},
|
||||
};
|
||||
|
||||
class path_resolve : public ::testing::TestWithParam<resolve_path_param> {
|
||||
|
||||
};
|
||||
class path_resolve : public ::testing::TestWithParam<resolve_path_param> {};
|
||||
|
||||
INSTANTIATE_TEST_SUITE_P(os, path_resolve, testing::ValuesIn(resolve_path_tests));
|
||||
|
||||
|
@ -15,10 +15,10 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
typedef void (*stream_test_cb)(const char *name, int32_t count, const uint8_t *find, int32_t find_size,
|
||||
mz_stream_find_cb find_cb);
|
||||
mz_stream_find_cb find_cb);
|
||||
|
||||
static void test_stream_find_begin(const char *name, int32_t count, const uint8_t *find, int32_t find_size,
|
||||
mz_stream_find_cb find_cb) {
|
||||
mz_stream_find_cb find_cb) {
|
||||
void *mem_stream = NULL;
|
||||
int32_t i = 0;
|
||||
int32_t x = 0;
|
||||
@ -49,10 +49,9 @@ static void test_stream_find_begin(const char *name, int32_t count, const uint8_
|
||||
|
||||
/* Should always find at the start of the stream if entire needle
|
||||
was written to stream */
|
||||
EXPECT_EQ(position, (i < find_size) ? -1 : 0)
|
||||
<< "name: " << name << std::endl
|
||||
<< "find_size: " << find_size << std::endl
|
||||
<< "index: " << i << std::endl;
|
||||
EXPECT_EQ(position, (i < find_size) ? -1 : 0) << "name: " << name << std::endl
|
||||
<< "find_size: " << find_size << std::endl
|
||||
<< "index: " << i << std::endl;
|
||||
|
||||
mz_stream_seek(mem_stream, 0, MZ_SEEK_END);
|
||||
last_pos = mz_stream_tell(mem_stream);
|
||||
@ -64,7 +63,7 @@ static void test_stream_find_begin(const char *name, int32_t count, const uint8_
|
||||
}
|
||||
|
||||
static void test_stream_find_end(const char *name, int32_t count, const uint8_t *find, int32_t find_size,
|
||||
mz_stream_find_cb find_cb) {
|
||||
mz_stream_find_cb find_cb) {
|
||||
void *mem_stream = NULL;
|
||||
int32_t i = 0;
|
||||
int32_t x = 0;
|
||||
@ -96,10 +95,9 @@ static void test_stream_find_end(const char *name, int32_t count, const uint8_t
|
||||
|
||||
/* Should always find after zeros if entire needle
|
||||
was written to stream */
|
||||
EXPECT_EQ(position, (i < find_size) ? -1 : (i - find_size))
|
||||
<< "name: " << name << std::endl
|
||||
<< "find_size: " << find_size << std::endl
|
||||
<< "index: " << i << std::endl;
|
||||
EXPECT_EQ(position, (i < find_size) ? -1 : (i - find_size)) << "name: " << name << std::endl
|
||||
<< "find_size: " << find_size << std::endl
|
||||
<< "index: " << i << std::endl;
|
||||
|
||||
mz_stream_seek(mem_stream, 0, MZ_SEEK_END);
|
||||
last_pos = mz_stream_tell(mem_stream);
|
||||
@ -111,7 +109,7 @@ static void test_stream_find_end(const char *name, int32_t count, const uint8_t
|
||||
}
|
||||
|
||||
static void test_stream_find_middle(const char *name, int32_t count, const uint8_t *find, int32_t find_size,
|
||||
mz_stream_find_cb find_cb) {
|
||||
mz_stream_find_cb find_cb) {
|
||||
void *mem_stream = NULL;
|
||||
int32_t i = 0;
|
||||
int32_t x = 0;
|
||||
@ -139,13 +137,12 @@ static void test_stream_find_middle(const char *name, int32_t count, const uint8
|
||||
if (find_cb == mz_stream_find)
|
||||
mz_stream_seek(mem_stream, 0, MZ_SEEK_SET);
|
||||
|
||||
find_cb(mem_stream, (const void *)find, find_size, i + find_size+ i, &position);
|
||||
find_cb(mem_stream, (const void *)find, find_size, i + find_size + i, &position);
|
||||
|
||||
/* Should always find after initial set of zeros */
|
||||
EXPECT_EQ(position, i)
|
||||
<< "name: " << name << std::endl
|
||||
<< "find_size: " << find_size << std::endl
|
||||
<< "index: " << i << std::endl;
|
||||
EXPECT_EQ(position, i) << "name: " << name << std::endl
|
||||
<< "find_size: " << find_size << std::endl
|
||||
<< "index: " << i << std::endl;
|
||||
|
||||
mz_stream_seek(mem_stream, 0, MZ_SEEK_END);
|
||||
last_pos = mz_stream_tell(mem_stream);
|
||||
@ -157,7 +154,7 @@ static void test_stream_find_middle(const char *name, int32_t count, const uint8
|
||||
}
|
||||
|
||||
static void test_stream_find_middle_odd(const char *name, int32_t count, const uint8_t *find, int32_t find_size,
|
||||
mz_stream_find_cb find_cb) {
|
||||
mz_stream_find_cb find_cb) {
|
||||
void *mem_stream = NULL;
|
||||
int32_t i = 0;
|
||||
int32_t x = 0;
|
||||
@ -188,10 +185,9 @@ static void test_stream_find_middle_odd(const char *name, int32_t count, const u
|
||||
find_cb(mem_stream, (const void *)find, find_size, i + find_size + i + 1, &position);
|
||||
|
||||
/* Should always find after initial set of zeros */
|
||||
EXPECT_EQ(position, i)
|
||||
<< "name: " << name << std::endl
|
||||
<< "find_size: " << find_size << std::endl
|
||||
<< "index: " << i << std::endl;
|
||||
EXPECT_EQ(position, i) << "name: " << name << std::endl
|
||||
<< "find_size: " << find_size << std::endl
|
||||
<< "index: " << i << std::endl;
|
||||
|
||||
mz_stream_seek(mem_stream, 0, MZ_SEEK_END);
|
||||
last_pos = mz_stream_tell(mem_stream);
|
||||
@ -203,8 +199,8 @@ static void test_stream_find_middle_odd(const char *name, int32_t count, const u
|
||||
}
|
||||
|
||||
struct stream_find_param {
|
||||
const char *name;
|
||||
stream_test_cb test_cb;
|
||||
const char *name;
|
||||
stream_test_cb test_cb;
|
||||
mz_stream_find_cb find_cb;
|
||||
|
||||
friend std::ostream &operator<<(std::ostream &os, const stream_find_param ¶m) {
|
||||
@ -213,19 +209,17 @@ struct stream_find_param {
|
||||
};
|
||||
|
||||
constexpr stream_find_param find_tests[] = {
|
||||
{ "begin", test_stream_find_begin, mz_stream_find },
|
||||
{ "begin reverse", test_stream_find_begin, mz_stream_find_reverse },
|
||||
{ "end", test_stream_find_end, mz_stream_find },
|
||||
{ "end reverse", test_stream_find_end, mz_stream_find_reverse },
|
||||
{ "middle", test_stream_find_middle, mz_stream_find },
|
||||
{ "middle reverse", test_stream_find_middle, mz_stream_find_reverse },
|
||||
{ "middle odd", test_stream_find_middle_odd, mz_stream_find },
|
||||
{ "middle odd reverse", test_stream_find_middle_odd, mz_stream_find_reverse }
|
||||
{ "begin", test_stream_find_begin, mz_stream_find},
|
||||
{ "begin reverse", test_stream_find_begin, mz_stream_find_reverse},
|
||||
{ "end", test_stream_find_end, mz_stream_find},
|
||||
{ "end reverse", test_stream_find_end, mz_stream_find_reverse},
|
||||
{ "middle", test_stream_find_middle, mz_stream_find},
|
||||
{ "middle reverse", test_stream_find_middle, mz_stream_find_reverse},
|
||||
{ "middle odd", test_stream_find_middle_odd, mz_stream_find},
|
||||
{"middle odd reverse", test_stream_find_middle_odd, mz_stream_find_reverse}
|
||||
};
|
||||
|
||||
class stream_find : public ::testing::TestWithParam<stream_find_param> {
|
||||
|
||||
};
|
||||
class stream_find : public ::testing::TestWithParam<stream_find_param> {};
|
||||
|
||||
INSTANTIATE_TEST_SUITE_P(stream, stream_find, testing::ValuesIn(find_tests));
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user