Compare commits

...
48 Commits
Author SHA1 Message Date
Nathan Moinvaziri 155f8b52b2 Version 2.3.5. 2018-07-09 08:13:28 -07:00
Nathan Moinvaziri 5d3b5c593d Fixed copyright statements on interface headers due to rewrite. 2018-07-09 08:11:16 -07:00
Nathan Moinvaziri e3958d9b46 Rename file. 2018-07-09 07:54:26 -07:00
Nathan Moinvaziri 957e5f92e4 Added 0 byte file for testing. 2018-07-09 07:53:54 -07:00
Nathan Moinvaziri 184f4cbde1 Added comment if entire entry is not read. 2018-07-09 07:53:17 -07:00
Nathan Moinvaziri 8871f6c0f1 Expose more prop values from streams.
Simplify some logic.
2018-07-08 19:23:56 -07:00
Nathan Moinvaziri fa8105b657 Use crypt header size in calculation. 2018-07-08 19:05:00 -07:00
Nathan Moinvaziri 2cf91c09ce Don't finalize compression stream if nothing written. #277 2018-07-08 18:53:52 -07:00
Nathan Moinvaziri 9eecff3140 Simplify command line parsing. 2018-07-08 18:52:41 -07:00
Nathan Moinvaziri 77dcb77506 Fixed lzma not flushing all remaining codes. #275 2018-07-07 12:21:25 -07:00
Nathan Moinvaziri ef776a31e1 Updated to liblzma 5.2.4. 2018-07-07 11:10:11 -07:00
Nathan Moinvaziri b70b824b19 Version 2.3.4. 2018-06-19 12:30:12 -07:00
Nathan Moinvaziri 0a9282d2f0 Added support for only using either compression or decompression with continued support for raw mode.
Renamed MZ_USE_WINRT_API to MZ_WINRT_API.
2018-06-19 11:59:07 -07:00
Nathan Moinvaziri 6be5ec8163 Fixed formatting. 2018-06-19 10:37:58 -07:00
Nathan Moinvaziri 5d4c61fdd5 Moved zlib installation instructions. 2018-06-19 10:36:38 -07:00
Nathan Moinvaziri 4bcaf521d7 Added windows install instructions #269 2018-06-19 10:35:40 -07:00
Nathan Moinvaziri 957f2e9976 Fixed issues with resolving paths.
Added source directory to includes.
Added unit tests for resolving paths.
Fixed compilation issues with test code.
2018-06-19 09:44:06 -07:00
Nathan Moinvaziri dd808239dd Fixed zip slip vulnerability in example app. #268 2018-06-11 07:38:58 -07:00
Nathan Moinvaziri 7bdfbf4699 Version 2.3.3. 2018-06-10 09:09:07 -07:00
Nathan Moinvaziri 4a7723357e Fixed casting issues after crc32 changes for mac. 2018-06-10 09:07:35 -07:00
Nathan Moinvaziri 4925f31e28 Fixed compilation warnings on mac and linux. 2018-05-31 15:05:19 -07:00
Nathan Moinvaziri 669f5a3086 Fixed buffered stream not being reset for reading after close.
Added ability to get buffer length for memory stream.
2018-05-31 13:56:32 -07:00
Nathan Moinvaziri 30e859be0b Version 2.3.2. 2018-05-29 08:48:05 -07:00
Nathan Moinvaziri 5b204622cc Modified pkcrypt to use crc32 table from zlib or lzma. 2018-05-23 20:39:12 -07:00
Nathan Moinvaziri b07ab3e9b9 Merge branch 'dev' of https://github.com/nmoinvaz/minizip into dev 2018-05-23 20:27:23 -07:00
Nathan Moinvaziri 3dc4334d38 Rename crypt to pkcrypt. 2018-05-23 20:27:17 -07:00
Nathan Moinvaziri ca30682447 Fixed formatting 2018-05-23 20:23:41 -07:00
Nathan Moinvaziri 0e5c9dcdf4 Renamed traditional PKware encryption to pkcrypt.
Don't link mz_os_rand if encryption is not turned on. #267
2018-05-23 20:21:48 -07:00
Nathan Moinvaziri ea99a46b6d Define possibly missing #defines from zlib header. #264 2018-05-22 14:19:50 -07:00
Nathan Moinvaziri 23bc9fd943 Merge pull request #266 from asmaloney/dev
Fix build on macOS
2018-05-22 13:00:00 -07:00
Andy Maloney cfcb729f79 Fix build on macOS
(Only Windows needs this __declspec)
2018-05-22 13:01:03 -04:00
Nathan Moinvaziri 51521bddc8 Fixed formatting. 2018-05-22 09:47:53 -07:00
Nathan Moinvaziri 41331dd3ec Fixed issues not being able to compile without zlib. #264 2018-05-22 09:46:03 -07:00
Nathan Moinvaziri 9c155991d7 Merge branch 'master' into dev 2018-05-22 09:35:45 -07:00
Nathan Moinvaziri 43e787bd88 Merge pull request #265 from asmaloney/asm_master
Partial fix for building without zlib
2018-05-22 09:32:42 -07:00
Andy Maloney 3909c23817 Partial fix for building without zlib
(See issue nmoinvaz/minizip#264)
2018-05-22 09:02:09 -04:00
Nathan Moinvaziri c0d3dbf537 Make #define names consistent. 2018-05-09 20:06:12 -07:00
Nathan Moinvaziri 0388124aad Simplify parsing of filename from path. 2018-05-09 20:03:26 -07:00
Nathan Moinvaziri e1aad8c445 Version 2.3.1. 2018-05-09 19:48:55 -07:00
Nathan Moinvaziri b5fb12503d Fixed more warnings. #263 2018-05-09 09:52:43 -07:00
Nathan Moinvaziri bd2f162009 Fixed more warnings. #263 2018-05-09 09:45:15 -07:00
Nathan Moinvaziri 408940f16c Fixed more warnings. #263 2018-05-09 09:42:31 -07:00
Nathan Moinvaziri a710bd714a Fixed reported warnings. #263 2018-05-09 00:46:47 -07:00
Nathan Moinvaziri 7a3b698578 Fixed more mac warnings. #263 2018-05-09 00:45:02 -07:00
Nathan Moinvaziri e275fcf017 Removed ZIP_UNUSED. #263 2018-05-09 00:34:16 -07:00
Nathan Moinvaziri 0e2e1a0b05 Fixed compiler warnings on mac. #263 2018-05-09 00:31:25 -07:00
Nathan Moinvaziri 3431a6635b Fixed more compiler warnings. #263 2018-05-06 16:59:31 -07:00
Nathan Moinvaziri 521a42a4c3 Fixed some compiler warnings. #263 2018-05-06 16:32:46 -07:00
49 changed files with 1164 additions and 722 deletions
+27 -14
View File
@@ -5,12 +5,14 @@
#***************************************************************************
cmake_minimum_required(VERSION 2.8)
option(USE_ZLIB "Enables building with ZLIB library" ON)
option(USE_BZIP2 "Enables building with BZIP2 library" ON)
option(USE_LZMA "Enables building with LZMA library" ON)
option(USE_CRYPT "Enables building with PKWARE traditional encryption" ON)
option(USE_AES "Enables building with AES library" ON)
option(BUILD_TEST "Enables building of minizip executable." OFF)
option(USE_ZLIB "Enables ZLIB compression" ON)
option(USE_BZIP2 "Enables BZIP2 compression" ON)
option(USE_LZMA "Enables LZMA compression" ON)
option(USE_PKCRYPT "Enables PKWARE traditional encryption" ON)
option(USE_AES "Enables AES encryption" ON)
option(COMPRESS_ONLY "Only support compression" OFF)
option(DECOMPRESS_ONLY "Only support decompression" OFF)
option(BUILD_TEST "Builds minizip test executable" OFF)
# Set a consistent MACOSX_RPATH default across all CMake versions.
# When CMake 2.8.12 is required, change this default to 1.
@@ -30,7 +32,7 @@ set(INSTALL_MAN_DIR ${CMAKE_INSTALL_MANDIR} CACHE PATH "Installation directory f
set(INSTALL_PKGCONFIG_DIR ${CMAKE_INSTALL_LIBDIR}/pkgconfig CACHE PATH "Installation directory for pkgconfig (.pc) files")
set(INSTALL_CMAKE_DIR ${CMAKE_INSTALL_LIBDIR}/cmake/minizip CACHE PATH "Installation directory for cmake files.")
set(VERSION "2.3.0")
set(VERSION "2.3.5")
# Set cmake debug postfix to d
set(CMAKE_DEBUG_POSTFIX "d")
@@ -58,7 +60,6 @@ set(MINIZIP_SRC
mz_strm_mem.c
mz_strm_posix.c
mz_strm_split.c
mz_strm_zlib.c
mz_zip.c)
set(MINIZIP_PUBLIC_HEADERS
@@ -70,9 +71,17 @@ set(MINIZIP_PUBLIC_HEADERS
mz_strm_mem.h
mz_strm_posix.h
mz_strm_split.h
mz_strm_zlib.h
mz_zip.h)
if(COMPRESS_ONLY)
add_definitions(-DMZ_ZIP_COMPRESS_ONLY)
endif()
if(DECOMPRESS_ONLY)
add_definitions(-DMZ_ZIP_DECOMPRESS_ONLY)
endif()
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
if(WIN32)
list(APPEND MINIZIP_SRC "mz_os_win32.c" "mz_strm_win32.c")
list(APPEND MINIZIP_PUBLIC_HEADERS "mz_os_win32.h" "mz_strm_win32.h")
@@ -81,7 +90,7 @@ if(WIN32)
endif()
if("${CMAKE_SYSTEM_NAME}" STREQUAL "WindowsStore")
add_definitions(-DMZ_USE_WINRT_API)
add_definitions(-DMZ_WINRT_API)
endif()
if(UNIX)
@@ -117,11 +126,11 @@ if(UNIX)
endif()
endif()
if(USE_CRYPT)
add_definitions(-DHAVE_CRYPT)
if(USE_PKCRYPT)
add_definitions(-DHAVE_PKCRYPT)
list(APPEND MINIZIP_SRC "mz_strm_crypt.c")
list(APPEND MINIZIP_PUBLIC_HEADERS "mz_strm_crypt.h")
list(APPEND MINIZIP_SRC "mz_strm_pkcrypt.c")
list(APPEND MINIZIP_PUBLIC_HEADERS "mz_strm_pkcrypt.h")
endif()
if(USE_AES)
@@ -156,6 +165,9 @@ endif()
if(USE_ZLIB)
add_definitions(-DHAVE_ZLIB)
list(APPEND MINIZIP_SRC "mz_strm_zlib.c")
list(APPEND MINIZIP_PUBLIC_HEADERS "mz_strm_zlib.h")
include(CheckFunctionExists)
set(CMAKE_REQUIRED_LIBRARIES ZLIB::ZLIB)
CHECK_FUNCTION_EXISTS(z_get_crc_table
@@ -165,6 +177,7 @@ if(USE_ZLIB)
add_definitions(-DZ_PREFIX)
endif()
endif()
if(USE_BZIP2)
add_definitions(-DHAVE_BZIP2)
add_definitions(-DBZ_NO_STDIO)
+1 -1
View File
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'Minizip'
s.version = '2.3.0'
s.version = '2.3.5'
s.license = 'zlib'
s.summary = 'Minizip contrib in zlib with the latest bug fixes and advanced features'
s.description = <<-DESC
+44 -6
View File
@@ -1,4 +1,4 @@
# Minizip 2.3.0
# Minizip 2.3.5
This library is a refactoring of the minizip contribution found in the zlib distribution and is supported on Windows, macOS, and Linux. The motivation for this work has been the inclusion of advanced features, improvements in code maintainability and readability, and the reduction of duplicate code. It is based on the original work of [Gilles Vollant](http://www.winimage.com/zLibDll/minizip.html) that has been contributed to by many people over the years.
@@ -10,11 +10,49 @@ For the original work maintained by Mark Adler checkout the zlib minizip [contr
## Build
To generate the project files for your platform and IDE download and run cmake in the project directory.
To generate project files for your platform:
1. [Download and install](https://cmake.org/install/) cmake.
2. [Download](https://zlib.net/) and install zlib if it is not installed on your system.
3. Run cmake in the minizip directory.
```
cmake . -DBUILD_TEST=ON
cmake --build .
```
## Build Options
| Name | Description | Default Value |
|:- |:-|:-:|
| USE_ZLIB | Enables ZLIB compression | ON |
| USE_BZIP2 | Enables BZIP2 compression | ON |
| USE_LZMA | Enables LZMA compression | ON |
| USE_PKCRYPT | Enables PKWARE traditional encryption | ON |
| COMPRESS_ONLY | Only support compression | OFF |
| DECOMPRESS_ONLY | Only support decompression | OFF |
| BUILD_TEST | Builds minizip test executable | OFF |
## Zlib Installation (Windows)
Option 1. Install the zlib package to the Program Files directory with an Administrator command prompt.
```
cmake . -DCMAKE_INSTALL_PREFIX=%PROGRAMFILES%\zlib
cmake --build . --config Release --target INSTALL
```
Option 2. Compile zlib in minizip's lib directory.
```
cmake .
cmake --build .
cmake --build . --config Release
```
Navigate back to the minizip directory and before building run:
```
cmake . -DZLIB_LIBRARY=lib\zlib\release\zlibstatic.lib -DZLIB_INCLUDE_DIR=lib\zlib\
```
## Contents
@@ -29,10 +67,10 @@ cmake --build .
| mz_strm_aes.\* | WinZIP AES stream | No |
| mz_strm_buf.\* | Buffered stream | No |
| mz_strm_bzip.\* | BZIP2 stream using libbzip2 | No |
| mz_strm_crypt.\* | PKWARE traditional encryption stream | No |
| mz_strm_lzma.\* | LZMA stream using liblzma | zlib or liblzma |
| mz_strm_mem.\* | Memory stream | Yes |
| mz_strm_split.\* | Disk splitting stream | No |
| mz_strm_pkcrypt.\* | PKWARE traditional encryption stream | No |
| mz_strm_posix.\* | File stream using Posix functions | Non-windows systems |
| mz_strm_win32.\* | File stream using Win32 API functions | Windows systems |
| mz_strm_zlib.\* | Deflate stream using zlib | zlib or liblzma |
@@ -68,7 +106,7 @@ To disable encryption use the following cmake commands:
```
cmake . -DUSE_AES=OFF
cmake . -DUSE_CRYPT=OFF
cmake . -DUSE_PKCRYPT=OFF
```
### NTFS Timestamps
@@ -155,7 +193,7 @@ void *zip_handle = mz_zip_open(split_stream, MZ_OPEN_MODE_WRITE);
### Windows RT
+ Requires ``#define MZ_USE_WINRT_API``
+ Requires ``#define MZ_WINRT_API``
## Limitations
+5 -1
View File
@@ -219,7 +219,11 @@
*/
#ifndef lzma_nothrow
# if defined(__cplusplus)
# define lzma_nothrow throw()
# if __cplusplus >= 201103L
# define lzma_nothrow noexcept
# else
# define lzma_nothrow throw()
# endif
# elif __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)
# define lzma_nothrow __attribute__((__nothrow__))
# else
+6 -1
View File
@@ -644,11 +644,16 @@ extern LZMA_API(uint64_t) lzma_memlimit_get(const lzma_stream *strm)
* This function is supported only when *strm has been initialized with
* a function that takes a memlimit argument.
*
* liblzma 5.2.3 and earlier has a bug where memlimit value of 0 causes
* this function to do nothing (leaving the limit unchanged) and still
* return LZMA_OK. Later versions treat 0 as if 1 had been specified (so
* lzma_memlimit_get() will return 1 even if you specify 0 here).
*
* \return - LZMA_OK: New memory usage limit successfully set.
* - LZMA_MEMLIMIT_ERROR: The new limit is too small.
* The limit was not changed.
* - LZMA_PROG_ERROR: Invalid arguments, e.g. *strm doesn't
* support memory usage limit or memlimit was zero.
* support memory usage limit.
*/
extern LZMA_API(lzma_ret) lzma_memlimit_set(
lzma_stream *strm, uint64_t memlimit) lzma_nothrow;
+17 -4
View File
@@ -520,7 +520,10 @@ extern LZMA_API(lzma_ret) lzma_stream_buffer_encode(
*
* \param strm Pointer to properly prepared lzma_stream
* \param memlimit Memory usage limit as bytes. Use UINT64_MAX
* to effectively disable the limiter.
* to effectively disable the limiter. liblzma
* 5.2.3 and earlier don't allow 0 here and return
* LZMA_PROG_ERROR; later versions treat 0 as if 1
* had been specified.
* \param flags Bitwise-or of zero or more of the decoder flags:
* LZMA_TELL_NO_CHECK, LZMA_TELL_UNSUPPORTED_CHECK,
* LZMA_TELL_ANY_CHECK, LZMA_CONCATENATED
@@ -544,7 +547,10 @@ extern LZMA_API(lzma_ret) lzma_stream_decoder(
*
* \param strm Pointer to properly prepared lzma_stream
* \param memlimit Memory usage limit as bytes. Use UINT64_MAX
* to effectively disable the limiter.
* to effectively disable the limiter. liblzma
* 5.2.3 and earlier don't allow 0 here and return
* LZMA_PROG_ERROR; later versions treat 0 as if 1
* had been specified.
* \param flags Bitwise-or of flags, or zero for no flags.
*
* \return - LZMA_OK: Initialization was successful.
@@ -560,9 +566,16 @@ extern LZMA_API(lzma_ret) lzma_auto_decoder(
/**
* \brief Initialize .lzma decoder (legacy file format)
*
* \param strm Pointer to properly prepared lzma_stream
* \param memlimit Memory usage limit as bytes. Use UINT64_MAX
* to effectively disable the limiter. liblzma
* 5.2.3 and earlier don't allow 0 here and return
* LZMA_PROG_ERROR; later versions treat 0 as if 1
* had been specified.
*
* Valid `action' arguments to lzma_code() are LZMA_RUN and LZMA_FINISH.
* There is no need to use LZMA_FINISH, but allowing it may simplify
* certain types of applications.
* There is no need to use LZMA_FINISH, but it's allowed because it may
* simplify certain types of applications.
*
* \return - LZMA_OK
* - LZMA_MEM_ERROR
+1 -1
View File
@@ -22,7 +22,7 @@
*/
#define LZMA_VERSION_MAJOR 5
#define LZMA_VERSION_MINOR 2
#define LZMA_VERSION_PATCH 3
#define LZMA_VERSION_PATCH 4
#define LZMA_VERSION_STABILITY LZMA_VERSION_STABILITY_STABLE
#ifndef LZMA_VERSION_COMMIT
+3 -3
View File
@@ -109,9 +109,9 @@ extern const uint64_t lzma_crc64_table[4][256];
/// \brief Initialize *check depending on type
///
/// \param check LZMA_OK on success. LZMA_UNSUPPORTED_CHECK if the type
/// is not supported by the current version or build of
/// liblzma. LZMA_PROG_ERROR if type > LZMA_CHECK_ID_MAX.
/// \return LZMA_OK on success. LZMA_UNSUPPORTED_CHECK if the type is not
/// supported by the current version or build of liblzma.
/// LZMA_PROG_ERROR if type > LZMA_CHECK_ID_MAX.
extern void lzma_check_init(lzma_check_state *check, lzma_check type);
/// Update the check state
+1 -4
View File
@@ -184,9 +184,6 @@ lzma_alone_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
{
lzma_next_coder_init(&lzma_alone_decoder_init, next, allocator);
if (memlimit == 0)
return LZMA_PROG_ERROR;
lzma_alone_coder *coder = next->coder;
if (coder == NULL) {
@@ -208,7 +205,7 @@ lzma_alone_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
coder->options.preset_dict = NULL;
coder->options.preset_dict_size = 0;
coder->uncompressed_size = 0;
coder->memlimit = memlimit;
coder->memlimit = my_max(1, memlimit);
coder->memusage = LZMA_MEMUSAGE_BASE;
return LZMA_OK;
+4 -2
View File
@@ -435,8 +435,10 @@ lzma_memlimit_set(lzma_stream *strm, uint64_t new_memlimit)
|| strm->internal->next.memconfig == NULL)
return LZMA_PROG_ERROR;
if (new_memlimit != 0 && new_memlimit < LZMA_MEMUSAGE_BASE)
return LZMA_MEMLIMIT_ERROR;
// Zero is a special value that cannot be used as an actual limit.
// If 0 was specified, use 1 instead.
if (new_memlimit == 0)
new_memlimit = 1;
return strm->internal->next.memconfig(strm->internal->next.coder,
&memusage, &old_memlimit, new_memlimit);
+6
View File
@@ -16,6 +16,12 @@
#include "lzma_decoder.h"
#include "range_decoder.h"
// The macros unroll loops with switch statements.
// Silence warnings about missing fall-through comments.
#if TUKLIB_GNUC_REQ(7, 0)
# pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
#endif
#ifdef HAVE_SMALL
+74 -97
View File
@@ -1,5 +1,5 @@
/* minizip.c
Version 2.3.0, May 3rd, 2018
Version 2.3.5, July 9, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
@@ -32,13 +32,13 @@
/***************************************************************************/
void minizip_banner()
void minizip_banner(void)
{
printf("Minizip %s - https://github.com/nmoinvaz/minizip\n", MZ_VERSION);
printf("---------------------------------------------------\n");
}
void minizip_help()
void minizip_help(void)
{
printf("Usage : minizip [-x -d dir|-l] [-o] [-a] [-j] [-0 to -9] [-b|-m] [-k 512] [-p pwd] [-s] file.zip [files]\n\n" \
" -x Extract files\n" \
@@ -68,10 +68,10 @@ void minizip_help()
/***************************************************************************/
typedef struct minizip_opt_s {
uint8_t include_path;
int16_t include_path;
int16_t compress_level;
int16_t compress_method;
uint8_t overwrite;
int16_t overwrite;
#ifdef HAVE_AES
int16_t aes;
#endif
@@ -101,11 +101,10 @@ int32_t minizip_add_path(void *handle, const char *path, const char *filenameinz
filenameinzip += 1;
// Get information about the file on disk so we can store it in zip
printf("Adding: %s\n", filenameinzip);
file_info.version_madeby = MZ_VERSION_MADEBY;
file_info.compression_method = options->compress_method;
file_info.filename = (char *)filenameinzip;
file_info.filename = filenameinzip;
file_info.uncompressed_size = mz_os_get_file_size(path);
#ifdef HAVE_AES
@@ -117,6 +116,8 @@ int32_t minizip_add_path(void *handle, const char *path, const char *filenameinz
&file_info.creation_date);
mz_os_get_file_attribs(path, &file_info.external_fa);
printf("Adding: %s\n", file_info.filename);
// Add to zip
err = mz_zip_entry_write_open(handle, &file_info, options->compress_level, password);
if (err != MZ_OK)
@@ -182,6 +183,7 @@ int32_t minizip_add(void *handle, const char *path, const char *root_path, const
int32_t err = MZ_OK;
int16_t is_dir = 0;
char full_path[320];
const char *filename = NULL;
const char *filenameinzip = path;
@@ -197,17 +199,8 @@ int32_t minizip_add(void *handle, const char *path, const char *root_path, const
{
if (!is_dir && root_path == path)
{
const char *match = NULL;
const char *last_slash = NULL;
for (match = filenameinzip; *match; match += 1)
{
if (*match == '\\' || *match == '/')
last_slash = match;
}
if (last_slash != NULL)
filenameinzip = last_slash + 1; // base filename follows last slash
if (mz_path_get_filename(filenameinzip, &filename) == MZ_OK)
filenameinzip = filename;
}
else
{
@@ -355,8 +348,8 @@ int32_t minizip_extract_currentfile(void *handle, const char *destination, const
int32_t err = MZ_OK;
int32_t err_close = MZ_OK;
void *stream = NULL;
char *match = NULL;
char *filename = NULL;
const char *filename = NULL;
char out_name[512];
char out_path[512];
char directory[512];
@@ -369,13 +362,8 @@ int32_t minizip_extract_currentfile(void *handle, const char *destination, const
return err;
}
match = filename = file_info->filename;
while (*match != 0)
{
if ((*match == '/') || (*match == '\\'))
filename = match + 1;
match += 1;
}
if (mz_path_get_filename(file_info->filename, &filename) != MZ_OK)
filename = file_info->filename;
// Construct output path
out_path[0] = 0;
@@ -387,7 +375,13 @@ int32_t minizip_extract_currentfile(void *handle, const char *destination, const
{
if (destination != NULL)
mz_path_combine(out_path, destination, sizeof(out_path));
mz_path_combine(out_path, file_info->filename, sizeof(out_path));
err = mz_path_resolve(file_info->filename, out_name, sizeof(out_name));
if (err != MZ_OK)
{
printf("Error %d resolving output path\n", err);
return err;
}
mz_path_combine(out_path, out_name, sizeof(out_path));
}
// If zip entry is a directory then create it on disk
@@ -434,30 +428,21 @@ int32_t minizip_extract_currentfile(void *handle, const char *destination, const
if (err == MZ_OK)
{
// Some zips don't contain directory alone before file
if ((mz_stream_os_open(stream, out_path, MZ_OPEN_MODE_CREATE) != MZ_OK) &&
(filename != file_info->filename))
err = mz_stream_os_open(stream, out_path, MZ_OPEN_MODE_CREATE);
if ((err != MZ_OK) && (filename != file_info->filename))
{
// Create the directory of the output path
strncpy(directory, out_path, sizeof(directory));
match = directory + strlen(directory) - 1;
while (match > directory)
{
if ((*match == '/') || (*match == '\\'))
{
*match = 0;
break;
}
match -= 1;
}
mz_path_remove_filename(directory);
mz_make_dir(directory);
mz_stream_os_open(stream, out_path, MZ_OPEN_MODE_CREATE);
err = mz_stream_os_open(stream, out_path, MZ_OPEN_MODE_CREATE);
}
}
// Read from the zip, unzip to buffer, and write to disk
if (mz_stream_os_is_open(stream) == MZ_OK)
if (err == MZ_OK)
{
printf(" Extracting: %s\n", out_path);
while (1)
@@ -489,7 +474,7 @@ int32_t minizip_extract_currentfile(void *handle, const char *destination, const
}
else
{
printf("Error opening %s\n", out_path);
printf("Error %d opening %s\n", err, out_path);
}
mz_stream_os_delete(&stream);
@@ -586,65 +571,57 @@ int main(int argc, char *argv[])
// Parse command line options
for (i = 1; i < argc; i += 1)
{
if ((*argv[i]) == '-')
if (argv[i][0] == '-')
{
const char *p = argv[i] + 1;
while ((*p) != '\0')
char c = argv[i][1];
if ((c == 'l') || (c == 'L'))
do_list = 1;
if ((c == 'x') || (c == 'X'))
do_extract = 1;
if ((c == 'a') || (c == 'A'))
append = 1;
if ((c == 'u') || (c == 'U'))
buffered = 1;
if ((c == 'o') || (c == 'O'))
options.overwrite = 1;
if ((c == 'i') || (c == 'I'))
options.include_path = 1;
if ((c >= '0') && (c <= '9'))
{
char c = *(p++);
if ((c == 'l') || (c == 'L'))
do_list = 1;
if ((c == 'x') || (c == 'X'))
do_extract = 1;
if ((c == 'a') || (c == 'A'))
append = 1;
if ((c == 'u') || (c == 'U'))
buffered = 1;
if ((c == 'o') || (c == 'O'))
options.overwrite = 1;
if ((c == 'i') || (c == 'I'))
options.include_path = 1;
if ((c >= '0') && (c <= '9'))
{
options.compress_level = (c - '0');
if (options.compress_level == 0)
options.compress_method = MZ_COMPRESS_METHOD_RAW;
}
#ifdef HAVE_BZIP2
if ((c == 'b') || (c == 'B'))
options.compress_method = MZ_COMPRESS_METHOD_BZIP2;
#endif
#ifdef HAVE_LZMA
if ((c == 'm') || (c == 'M'))
options.compress_method = MZ_COMPRESS_METHOD_LZMA;
#endif
#ifdef HAVE_AES
if ((c == 's') || (c == 'S'))
options.aes = 1;
#endif
if (((c == 'k') || (c == 'K')) && (i + 1 < argc))
{
disk_size = atoi(argv[i + 1]) * 1024;
i += 1;
}
if (((c == 'd') || (c == 'D')) && (i + 1 < argc))
{
destination = argv[i + 1];
i += 1;
}
if (((c == 'p') || (c == 'P')) && (i + 1 < argc))
{
password = argv[i + 1];
i += 1;
}
options.compress_level = (c - '0');
if (options.compress_level == 0)
options.compress_method = MZ_COMPRESS_METHOD_RAW;
}
continue;
#ifdef HAVE_BZIP2
if ((c == 'b') || (c == 'B'))
options.compress_method = MZ_COMPRESS_METHOD_BZIP2;
#endif
#ifdef HAVE_LZMA
if ((c == 'm') || (c == 'M'))
options.compress_method = MZ_COMPRESS_METHOD_LZMA;
#endif
#ifdef HAVE_AES
if ((c == 's') || (c == 'S'))
options.aes = 1;
#endif
if (((c == 'k') || (c == 'K')) && (i + 1 < argc))
{
disk_size = atoi(argv[i + 1]) * 1024;
i += 1;
}
if (((c == 'd') || (c == 'D')) && (i + 1 < argc))
{
destination = argv[i + 1];
i += 1;
}
if (((c == 'p') || (c == 'P')) && (i + 1 < argc))
{
password = argv[i + 1];
i += 1;
}
}
if (path_arg == 0)
else if (path_arg == 0)
path_arg = i;
}
+5 -4
View File
@@ -1,5 +1,5 @@
/* mz.h -- Errors codes, zip flags and magic
Version 2.3.0, May 3rd, 2018
Version 2.3.5, July 9, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
@@ -9,8 +9,8 @@
See the accompanying LICENSE file for the full text of the license.
*/
#ifndef _MZ_H
#define _MZ_H
#ifndef MZ_H
#define MZ_H
#ifdef __cplusplus
extern "C" {
@@ -19,7 +19,7 @@ extern "C" {
/***************************************************************************/
// MZ_VERSION
#define MZ_VERSION ("2.3.0")
#define MZ_VERSION ("2.3.5")
// MZ_ERROR
#define MZ_OK (0)
@@ -35,6 +35,7 @@ extern "C" {
#define MZ_CRYPT_ERROR (-106)
#define MZ_EXIST_ERROR (-107)
#define MZ_PASSWORD_ERROR (-108)
#define MZ_SUPPORT_ERROR (-109)
// MZ_OPEN
#define MZ_OPEN_MODE_READ (0x01)
+20 -12
View File
@@ -1,5 +1,5 @@
/* mz_compat.c -- Backwards compatible interface for older versions
Version 2.3.0, May 3rd, 2018
Version 2.3.5, July 9, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
@@ -55,7 +55,6 @@ extern zipFile ZEXPORT zipOpen2_64(const void *path, int append, const char **gl
{
mz_compat *compat = NULL;
int32_t mode = MZ_OPEN_MODE_READWRITE;
int32_t open_existing = 0;
void *handle = NULL;
void *stream = NULL;
@@ -79,7 +78,6 @@ extern zipFile ZEXPORT zipOpen2_64(const void *path, int append, const char **gl
mode |= MZ_OPEN_MODE_CREATE | MZ_OPEN_MODE_APPEND;
break;
case APPEND_STATUS_ADDINZIP:
open_existing = 1;
break;
}
@@ -108,15 +106,23 @@ extern zipFile ZEXPORT zipOpen2_64(const void *path, int append, const char **gl
}
extern int ZEXPORT zipOpenNewFileInZip5(zipFile file, const char *filename, const zip_fileinfo *zipfi,
ZIP_UNUSED const void *extrafield_local, ZIP_UNUSED uint16_t size_extrafield_local, const void *extrafield_global,
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
uint16_t size_extrafield_global, const char *comment, uint16_t compression_method, int level,
int raw, ZIP_UNUSED int windowBits, ZIP_UNUSED int memLevel, ZIP_UNUSED int strategy, const char *password,
ZIP_UNUSED uint32_t crc_for_crypting, uint16_t version_madeby, uint16_t flag_base, int zip64)
int raw, int windowBits, int memLevel, int strategy, const char *password,
uint32_t crc_for_crypting, uint16_t version_madeby, uint16_t flag_base, int zip64)
{
mz_compat *compat = (mz_compat *)file;
mz_zip_file file_info;
uint64_t dos_date = 0;
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 == NULL)
return ZIP_PARAMERROR;
@@ -138,13 +144,13 @@ extern int ZEXPORT zipOpenNewFileInZip5(zipFile file, const char *filename, cons
filename = "-";
file_info.compression_method = compression_method;
file_info.filename = (char *)filename;
file_info.filename = filename;
//file_info.extrafield_local = extrafield_local;
//file_info.extrafield_local_size = size_extrafield_local;
file_info.extrafield = (uint8_t *)extrafield_global;
file_info.extrafield = extrafield_global;
file_info.extrafield_size = size_extrafield_global;
file_info.version_madeby = version_madeby;
file_info.comment = (char *)comment;
file_info.comment = comment;
file_info.flag = flag_base;
if (zip64)
file_info.zip64 = MZ_ZIP64_FORCE;
@@ -208,9 +214,11 @@ extern int ZEXPORT zipOpenNewFileInZip3_64(zipFile file, const char *filename, c
extern int ZEXPORT zipWriteInFileInZip(zipFile file, const void *buf, uint32_t len)
{
mz_compat *compat = (mz_compat *)file;
int32_t written = 0;
if (compat == NULL)
return ZIP_PARAMERROR;
if (mz_zip_entry_write(compat->handle, buf, len) != len)
written = mz_zip_entry_write(compat->handle, buf, len);
if ((written < 0) || ((uint32_t)written != len))
return ZIP_ERRNO;
return ZIP_OK;
}
@@ -432,7 +440,7 @@ extern int ZEXPORT unzOpenCurrentFile2(unzFile file, int *method, int *level, in
return unzOpenCurrentFile3(file, method, level, raw, NULL);
}
extern int ZEXPORT unzReadCurrentFile(unzFile file, voidp buf, uint32_t len)
extern int ZEXPORT unzReadCurrentFile(unzFile file, void *buf, uint32_t len)
{
mz_compat *compat = (mz_compat *)file;
if (compat == NULL)
@@ -624,7 +632,7 @@ extern int ZEXPORT unzSetOffset64(unzFile file, uint64_t pos)
return (int)mz_zip_goto_entry(compat->handle, pos);
}
extern int ZEXPORT unzGetLocalExtrafield(unzFile file, voidp buf, unsigned len) // TODO
extern int ZEXPORT unzGetLocalExtrafield(unzFile file, void *buf, unsigned len) // TODO
{
MZ_UNUSED(file);
MZ_UNUSED(buf);
+20 -28
View File
@@ -1,5 +1,5 @@
/* mz_compat.h -- Backwards compatible interface for older versions
Version 2.3.0, May 3rd, 2018
Version 2.3.5, July 9, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
@@ -11,8 +11,8 @@
See the accompanying LICENSE file for the full text of the license.
*/
#ifndef _MZ_COMPAT_H
#define _MZ_COMPAT_H
#ifndef MZ_COMPAT_H
#define MZ_COMPAT_H
#include <stdint.h>
@@ -20,24 +20,16 @@
extern "C" {
#endif
#ifdef HAVE_ZLIB
#include "zlib.h"
#else
#define ZEXPORT
#define MAX_WBITS (15)
#define DEF_MEM_LEVEL (8)
#endif
/***************************************************************************/
#ifdef __GNUC__
# define ZIP_UNUSED __attribute__((__unused__))
#else
# define ZIP_UNUSED
#endif
#ifndef DEF_MEM_LEVEL
# if MAX_MEM_LEVEL >= 8
# define DEF_MEM_LEVEL 8
# else
# define DEF_MEM_LEVEL MAX_MEM_LEVEL
# endif
#endif
#if defined(USE_FILE32API)
# define MZ_USE_FILE32API
# define fopen64 fopen
@@ -70,7 +62,7 @@ extern "C" {
typedef struct TagzipFile__ { int unused; } zip_file__;
typedef zip_file__ *zipFile;
#else
typedef voidp zipFile;
typedef void *zipFile;
#endif
/***************************************************************************/
@@ -98,7 +90,7 @@ typedef struct
#define ZIP_OK (0)
#define ZIP_EOF (0)
#define ZIP_ERRNO (Z_ERRNO)
#define ZIP_ERRNO (-1)
#define ZIP_PARAMERROR (-102)
#define ZIP_BADZIPFILE (-103)
#define ZIP_INTERNALERROR (-104)
@@ -122,27 +114,27 @@ extern int ZEXPORT zipOpenNewFileInZip3(zipFile file, const char *filename, cons
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
uint16_t size_extrafield_global, const char *comment, uint16_t compression_method, int level,
int raw, int windowBits, int memLevel, int strategy, const char *password,
ZIP_UNUSED uint32_t crc_for_crypting);
uint32_t crc_for_crypting);
extern int ZEXPORT 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, uint16_t compression_method, int level,
int raw, int windowBits, int memLevel, int strategy, const char *password,
ZIP_UNUSED uint32_t crc_for_crypting, int zip64);
uint32_t crc_for_crypting, int zip64);
extern int ZEXPORT 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, uint16_t compression_method, int level,
int raw, int windowBits, int memLevel, int strategy, const char *password,
ZIP_UNUSED uint32_t crc_for_crypting, uint16_t version_madeby, uint16_t flag_base);
uint32_t crc_for_crypting, uint16_t version_madeby, uint16_t flag_base);
extern int ZEXPORT 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, uint16_t compression_method, int level,
int raw, int windowBits, int memLevel, int strategy, const char *password,
ZIP_UNUSED uint32_t crc_for_crypting, uint16_t version_madeby, uint16_t flag_base, int zip64);
uint32_t crc_for_crypting, uint16_t version_madeby, uint16_t flag_base, int zip64);
extern int ZEXPORT 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, uint16_t compression_method, int level,
int raw, int windowBits, int memLevel, int strategy, const char *password,
ZIP_UNUSED uint32_t crc_for_crypting, uint16_t version_madeby, uint16_t flag_base, int zip64);
uint32_t crc_for_crypting, uint16_t version_madeby, uint16_t flag_base, int zip64);
extern int ZEXPORT zipWriteInFileInZip(zipFile file, const void *buf, uint32_t len);
@@ -163,14 +155,14 @@ extern int ZEXPORT zipClose2_64(zipFile file, const char *global_comment, uint16
typedef struct TagunzFile__ { int unused; } unz_file__;
typedef unz_file__ *unzFile;
#else
typedef voidp unzFile;
typedef void *unzFile;
#endif
/***************************************************************************/
#define UNZ_OK (0)
#define UNZ_END_OF_LIST_OF_FILE (-100)
#define UNZ_ERRNO (Z_ERRNO)
#define UNZ_ERRNO (-1)
#define UNZ_EOF (0)
#define UNZ_PARAMERROR (-102)
#define UNZ_BADZIPFILE (-103)
@@ -262,7 +254,7 @@ extern int ZEXPORT unzOpenCurrentFile(unzFile file);
extern int ZEXPORT unzOpenCurrentFilePassword(unzFile file, const char *password);
extern int ZEXPORT unzOpenCurrentFile2(unzFile file, int *method, int *level, int raw);
extern int ZEXPORT unzOpenCurrentFile3(unzFile file, int *method, int *level, int raw, const char *password);
extern int ZEXPORT unzReadCurrentFile(unzFile file, voidp buf, uint32_t len);
extern int ZEXPORT unzReadCurrentFile(unzFile file, void *buf, uint32_t len);
extern int ZEXPORT unzCloseCurrentFile(unzFile file);
extern int ZEXPORT unzGetCurrentFileInfo(unzFile file, unz_file_info *pfile_info, char *filename,
@@ -277,7 +269,7 @@ extern int64_t ZEXPORT unzGetOffset64(unzFile file);
extern int32_t ZEXPORT unzGetOffset(unzFile file);
extern int ZEXPORT unzSetOffset64(unzFile file, uint64_t pos);
extern int ZEXPORT unzSetOffset(unzFile file, uint32_t pos);
extern int ZEXPORT unzGetLocalExtrafield(unzFile file, voidp buf, unsigned len);
extern int ZEXPORT unzGetLocalExtrafield(unzFile file, void *buf, unsigned len);
/***************************************************************************/
+143 -1
View File
@@ -1,5 +1,5 @@
/* mz_os.c -- System functions
Version 2.3.0, May 3rd, 2018
Version 2.3.5, July 9, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
@@ -98,6 +98,148 @@ int32_t mz_path_combine(char *path, const char *join, int32_t max_path)
return MZ_OK;
}
int32_t mz_path_resolve(const char *path, char *output, int32_t max_output)
{
const char *source = path;
const char *check = output;
char *target = output;
if (max_output <= 0)
return MZ_PARAM_ERROR;
while (*source != 0 && max_output > 1)
{
check = source;
if ((*check == '\\') || (*check == '/'))
check += 1;
if ((source == path) || (check != source) || (*target == 0))
{
// Skip double paths
if ((*check == '\\') || (*check == '/'))
{
source += 1;
continue;
}
if ((*check != 0) && (*check == '.'))
{
check += 1;
// Remove current directory . if at end of stirng
if ((*check == 0) && (source != path))
{
// Copy last slash
*target = *source;
target += 1;
max_output -= 1;
source += (check - source);
continue;
}
// Remove current directory . if not at end of stirng
if ((*check == 0) || (*check == '\\' || *check == '/'))
{
// Only proceed if .\ is not entire string
if (check[1] != 0 || (path != source))
{
source += (check - source);
continue;
}
}
// Go to parent directory ..
if ((*check != 0) || (*check == '.'))
{
check += 1;
if ((*check == 0) || (*check == '\\' || *check == '/'))
{
source += (check - source);
// Search backwards for previous slash
if (target != output)
{
target -= 1;
do
{
if ((*target == '\\') || (*target == '/'))
break;
target -= 1;
max_output += 1;
}
while (target > output);
}
if ((target == output) && (*source != 0))
source += 1;
if ((*target == '\\' || *target == '/') && (*source == 0))
target += 1;
*target = 0;
continue;
}
}
}
}
*target = *source;
source += 1;
target += 1;
max_output -= 1;
}
*target = 0;
if (*path == 0)
return MZ_INTERNAL_ERROR;
return MZ_OK;
}
int32_t mz_path_remove_filename(const char *path)
{
char *path_ptr = NULL;
if (path == NULL)
return MZ_PARAM_ERROR;
path_ptr = (char *)(path + strlen(path) - 1);
while (path_ptr > path)
{
if ((*path_ptr == '/') || (*path_ptr == '\\'))
{
*path_ptr = 0;
break;
}
path_ptr -= 1;
}
return MZ_OK;
}
int32_t mz_path_get_filename(const char *path, const char **filename)
{
const char *match = NULL;
if (path == NULL || filename == NULL)
return MZ_PARAM_ERROR;
*filename = NULL;
for (match = path; *match != 0; match += 1)
{
if ((*match == '\\') || (*match == '/'))
*filename = match + 1;
}
if (*filename == NULL)
return MZ_EXIST_ERROR;
return MZ_OK;
}
int32_t mz_get_file_crc(const char *path, uint32_t *result_crc)
{
void *stream = NULL;
+12 -5
View File
@@ -1,18 +1,16 @@
/* mz_os.h -- System functions
Version 2.3.0, May 3rd, 2018
Version 2.3.5, July 9, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
Copyright (C) 1998-2010 Gilles Vollant
http://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_OS_H
#define _MZ_OS_H
#ifndef MZ_OS_H
#define MZ_OS_H
#include <stdint.h>
@@ -53,6 +51,15 @@ int32_t mz_make_dir(const char *path);
int32_t mz_path_combine(char *path, const char *join, int32_t max_path);
// Combines two paths
int32_t mz_path_resolve(const char *path, char *target, int32_t max_target);
// Resolves path
int32_t mz_path_remove_filename(const char *path);
// Remove the filename from a path
int32_t mz_path_get_filename(const char *path, const char **filename);
// Get the filename from a path
int32_t mz_get_file_crc(const char *path, uint32_t *result_crc);
// Gets the crc32 hash of a file
+6 -4
View File
@@ -1,5 +1,5 @@
/* mz_os_posix.c -- System functions for posix
Version 2.3.0, May 3rd, 2018
Version 2.3.5, July 9, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
@@ -34,11 +34,13 @@
/***************************************************************************/
#if defined(HAVE_PKCRYPT) || defined(HAVE_AES)
int32_t mz_posix_rand(uint8_t *buf, int32_t size)
{
arc4random_buf(buf, size);
return size;
}
#endif
int32_t mz_posix_file_exists(const char *path)
{
@@ -115,7 +117,7 @@ int32_t mz_posix_set_file_date(const char *path, time_t modified_date, time_t ac
return MZ_OK;
}
int32_t mz_posix_get_file_attribs(const char *path, int32_t *attributes)
int32_t mz_posix_get_file_attribs(const char *path, uint32_t *attributes)
{
struct stat stat_info;
int32_t err = MZ_OK;
@@ -127,11 +129,11 @@ int32_t mz_posix_get_file_attribs(const char *path, int32_t *attributes)
return err;
}
int32_t mz_posix_set_file_attribs(const char *path, int32_t attributes)
int32_t mz_posix_set_file_attribs(const char *path, uint32_t attributes)
{
int32_t err = MZ_OK;
if (chmod(path, attributes) == -1)
if (chmod(path, (mode_t)attributes) == -1)
err = MZ_INTERNAL_ERROR;
return err;
+5 -5
View File
@@ -1,5 +1,5 @@
/* mz_os_posix.h -- System functions for posix
Version 2.3.0, May 3rd, 2018
Version 2.3.5, July 9, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
@@ -9,8 +9,8 @@
See the accompanying LICENSE file for the full text of the license.
*/
#ifndef _MZ_OS_POSIX_H
#define _MZ_OS_POSIX_H
#ifndef MZ_OS_POSIX_H
#define MZ_OS_POSIX_H
#include <stdint.h>
#include <time.h>
@@ -35,8 +35,8 @@ int32_t mz_posix_file_exists(const char *path);
int64_t mz_posix_get_file_size(const char *path);
int32_t mz_posix_get_file_date(const char *path, time_t *modified_date, time_t *accessed_date, time_t *creation_date);
int32_t mz_posix_set_file_date(const char *path, time_t modified_date, time_t accessed_date, time_t creation_date);
int32_t mz_posix_get_file_attribs(const char *path, int32_t *attributes);
int32_t mz_posix_set_file_attribs(const char *path, int32_t attributes);
int32_t mz_posix_get_file_attribs(const char *path, uint32_t *attributes);
int32_t mz_posix_set_file_attribs(const char *path, uint32_t attributes);
int32_t mz_posix_make_dir(const char *path);
DIR* mz_posix_open_dir(const char *path);
struct
+10 -8
View File
@@ -1,5 +1,5 @@
/* mz_os_win32.c -- System functions for Windows
Version 2.3.0, May 3rd, 2018
Version 2.3.5, July 9, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
@@ -26,9 +26,9 @@
/***************************************************************************/
#if defined(WINAPI_FAMILY_PARTITION) && (!(defined(MZ_USE_WINRT_API)))
#if defined(WINAPI_FAMILY_PARTITION) && (!(defined(MZ_WINRT_API)))
# if !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
# define MZ_USE_WINRT_API 1
# define MZ_WINRT_API 1
# endif
#endif
@@ -43,6 +43,7 @@ typedef struct DIR_int_s {
/***************************************************************************/
#if defined(HAVE_PKCRYPT) || defined(HAVE_AES)
int32_t mz_win32_rand(uint8_t *buf, int32_t size)
{
HCRYPTPROV provider;
@@ -68,6 +69,7 @@ int32_t mz_win32_rand(uint8_t *buf, int32_t size)
return len;
}
#endif
wchar_t *mz_win32_unicode_path_create(const char *path)
{
@@ -116,7 +118,7 @@ int64_t mz_win32_get_file_size(const char *path)
path_wide = mz_win32_unicode_path_create(path);
#ifdef MZ_USE_WINRT_API
#ifdef MZ_WINRT_API
handle = CreateFile2W(path_wide, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
#else
handle = CreateFileW(path_wide, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
@@ -186,7 +188,7 @@ int32_t mz_win32_set_file_date(const char *path, time_t modified_date, time_t ac
path_wide = mz_win32_unicode_path_create(path);
#ifdef MZ_USE_WINRT_API
#ifdef MZ_WINRT_API
handle = CreateFile2W(path_wide, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
#else
handle = CreateFileW(path_wide, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
@@ -213,7 +215,7 @@ int32_t mz_win32_set_file_date(const char *path, time_t modified_date, time_t ac
return err;
}
int32_t mz_win32_get_file_attribs(const char *path, int32_t *attributes)
int32_t mz_win32_get_file_attribs(const char *path, uint32_t *attributes)
{
wchar_t *path_wide = NULL;
int32_t err = MZ_OK;
@@ -228,7 +230,7 @@ int32_t mz_win32_get_file_attribs(const char *path, int32_t *attributes)
return err;
}
int32_t mz_win32_set_file_attribs(const char *path, int32_t attributes)
int32_t mz_win32_set_file_attribs(const char *path, uint32_t attributes)
{
wchar_t *path_wide = NULL;
int32_t err = MZ_OK;
@@ -328,7 +330,7 @@ int32_t mz_win32_close_dir(DIR *dir)
int32_t mz_win32_is_dir(const char *path)
{
wchar_t *path_wide = NULL;
int32_t attribs = 0;
uint32_t attribs = 0;
path_wide = mz_win32_unicode_path_create(path);
attribs = GetFileAttributesW(path_wide);
+5 -5
View File
@@ -1,5 +1,5 @@
/* mz_os_win32.h -- System functions for Windows
Version 2.3.0, May 3rd, 2018
Version 2.3.5, July 9, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
@@ -9,8 +9,8 @@
See the accompanying LICENSE file for the full text of the license.
*/
#ifndef _MZ_OS_WIN32_H
#define _MZ_OS_WIN32_H
#ifndef MZ_OS_WIN32_H
#define MZ_OS_WIN32_H
#include <stdint.h>
@@ -36,8 +36,8 @@ int32_t mz_win32_file_exists(const char *path);
int64_t mz_win32_get_file_size(const char *path);
int32_t mz_win32_get_file_date(const char *path, time_t *modified_date, time_t *accessed_date, time_t *creation_date);
int32_t mz_win32_set_file_date(const char *path, time_t modified_date, time_t accessed_date, time_t creation_date);
int32_t mz_win32_get_file_attribs(const char *path, int32_t *attributes);
int32_t mz_win32_set_file_attribs(const char *path, int32_t attributes);
int32_t mz_win32_get_file_attribs(const char *path, uint32_t *attributes);
int32_t mz_win32_set_file_attribs(const char *path, uint32_t attributes);
int32_t mz_win32_make_dir(const char *path);
DIR* mz_win32_open_dir(const char *path);
struct
+7 -5
View File
@@ -1,5 +1,5 @@
/* mz_strm.c -- Stream interface
Version 2.3.0, May 3rd, 2018
Version 2.3.5, July 9, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
@@ -173,7 +173,7 @@ int32_t mz_stream_copy(void *target, void *source, int32_t len)
while (len > 0)
{
bytes_to_copy = len;
if (bytes_to_copy > sizeof(buf))
if (bytes_to_copy > (int32_t)sizeof(buf))
bytes_to_copy = sizeof(buf);
read = mz_stream_read(source, buf, bytes_to_copy);
if (read < 0)
@@ -290,7 +290,7 @@ static mz_stream_vtbl mz_stream_crc32_vtbl = {
typedef struct mz_stream_crc32_s {
mz_stream stream;
int8_t initialized;
int32_t value;
int64_t value;
int64_t total_in;
int64_t total_out;
@@ -325,8 +325,10 @@ int32_t mz_stream_crc32_read(void *stream, void *buf, int32_t size)
int32_t read = 0;
read = mz_stream_read(crc32x->stream.base, buf, size);
if (read > 0)
{
crc32x->value = crc32x->update(crc32x->value, buf, read);
crc32x->total_in += read;
crc32x->total_in += read;
}
return read;
}
@@ -369,7 +371,7 @@ int32_t mz_stream_crc32_error(void *stream)
int32_t mz_stream_crc32_get_value(void *stream)
{
mz_stream_crc32 *crc32 = (mz_stream_crc32 *)stream;
return crc32->value;
return (int32_t)crc32->value;
}
int32_t mz_stream_crc32_get_prop_int64(void *stream, int32_t prop, int64_t *value)
+4 -4
View File
@@ -1,5 +1,5 @@
/* mz_strm.h -- Stream interface
Version 2.3.0, May 3rd, 2018
Version 2.3.5, July 9, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
@@ -9,8 +9,8 @@
See the accompanying LICENSE file for the full text of the license.
*/
#ifndef _MZ_STREAM_H
#define _MZ_STREAM_H
#ifndef MZ_STREAM_H
#define MZ_STREAM_H
#include <stdint.h>
@@ -99,7 +99,7 @@ void mz_stream_delete(void **stream);
/***************************************************************************/
typedef int32_t (*mz_stream_crc32_update)(int32_t value, const void *buf, int32_t size);
typedef int64_t (*mz_stream_crc32_update)(int64_t value, const void *buf, int32_t size);
int32_t mz_stream_crc32_open(void *stream, const char *filename, int32_t mode);
int32_t mz_stream_crc32_is_open(void *stream);
+20 -3
View File
@@ -1,5 +1,5 @@
/* mz_strm_aes.c -- Stream for WinZip AES encryption
Version 2.3.0, May 3rd, 2018
Version 2.3.5, July 9, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
@@ -50,7 +50,7 @@ static mz_stream_vtbl mz_stream_aes_vtbl = {
mz_stream_aes_create,
mz_stream_aes_delete,
mz_stream_aes_get_prop_int64,
NULL
mz_stream_aes_set_prop_int64
};
/***************************************************************************/
@@ -62,6 +62,7 @@ typedef struct mz_stream_aes_s {
int16_t initialized;
uint8_t buffer[INT16_MAX];
int64_t total_in;
int64_t max_total_in;
int64_t total_out;
int16_t encryption_mode;
const char *password;
@@ -220,7 +221,7 @@ int32_t mz_stream_aes_write(void *stream, const void *buf, int32_t size)
mz_stream_aes *aes = (mz_stream_aes *)stream;
int32_t written = 0;
if (size > sizeof(aes->buffer))
if (size > (int32_t)sizeof(aes->buffer))
return MZ_STREAM_ERROR;
memcpy(aes->buffer, buf, size);
@@ -269,6 +270,7 @@ int32_t mz_stream_aes_close(void *stream)
aes->total_in += MZ_AES_AUTHCODE_SIZE;
// If entire entry was not read this will fail
if (memcmp(authcode, verify_authcode, MZ_AES_AUTHCODE_SIZE) != 0)
return MZ_CRC_ERROR;
}
@@ -306,6 +308,9 @@ int32_t mz_stream_aes_get_prop_int64(void *stream, int32_t prop, int64_t *value)
case MZ_STREAM_PROP_TOTAL_OUT:
*value = aes->total_out;
return MZ_OK;
case MZ_STREAM_PROP_TOTAL_IN_MAX:
*value = aes->max_total_in;
return MZ_OK;
case MZ_STREAM_PROP_HEADER_SIZE:
*value = MZ_AES_SALT_LENGTH(aes->encryption_mode) + MZ_AES_PW_VERIFY_SIZE;
return MZ_OK;
@@ -316,6 +321,18 @@ int32_t mz_stream_aes_get_prop_int64(void *stream, int32_t prop, int64_t *value)
return MZ_EXIST_ERROR;
}
int32_t mz_stream_aes_set_prop_int64(void *stream, int32_t prop, int64_t value)
{
mz_stream_aes *aes = (mz_stream_aes *)stream;
switch (prop)
{
case MZ_STREAM_PROP_TOTAL_IN_MAX:
aes->max_total_in = value;
return MZ_OK;
}
return MZ_EXIST_ERROR;
}
void *mz_stream_aes_create(void **stream)
{
mz_stream_aes *aes = NULL;
+4 -3
View File
@@ -1,5 +1,5 @@
/* mz_strm_aes.h -- Stream for WinZIP AES encryption
Version 2.3.0, May 3rd, 2018
Version 2.3.5, July 9, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
@@ -9,8 +9,8 @@
See the accompanying LICENSE file for the full text of the license.
*/
#ifndef _MZ_STREAM_AES_H
#define _MZ_STREAM_AES_H
#ifndef MZ_STREAM_AES_H
#define MZ_STREAM_AES_H
#include <stdint.h>
@@ -33,6 +33,7 @@ void mz_stream_aes_set_password(void *stream, const char *password);
void mz_stream_aes_set_encryption_mode(void *stream, int16_t encryption_mode);
int32_t mz_stream_aes_get_prop_int64(void *stream, int32_t prop, int64_t *value);
int32_t mz_stream_aes_set_prop_int64(void *stream, int32_t prop, int64_t value);
void* mz_stream_aes_create(void **stream);
void mz_stream_aes_delete(void **stream);
+7 -2
View File
@@ -1,5 +1,5 @@
/* mz_strm_buf.c -- Stream for buffering reads/writes
Version 2.3.0, May 3rd, 2018
Version 2.3.5, July 9, 2018
part of the MiniZip project
This version of ioapi is designed to buffer IO.
@@ -340,7 +340,7 @@ int32_t mz_stream_buffered_close(void *stream)
int32_t bytes_flushed = 0;
mz_stream_buffered_flush(stream, &bytes_flushed);
mz_stream_buffered_print(stream, "close\n");
mz_stream_buffered_print(stream, "close [flushed %d]\n", bytes_flushed);
if (buffered->readbuf_hits + buffered->readbuf_misses > 0)
mz_stream_buffered_print(stream, "read efficency %.02f%%\n",
@@ -350,6 +350,11 @@ int32_t mz_stream_buffered_close(void *stream)
mz_stream_buffered_print(stream, "write efficency %.02f%%\n",
(buffered->writebuf_hits / ((float)buffered->writebuf_hits + buffered->writebuf_misses)) * 100);
buffered->readbuf_len = 0;
buffered->readbuf_pos = 0;
buffered->writebuf_len = 0;
buffered->writebuf_pos = 0;
return mz_stream_close(buffered->stream.base);
}
+3 -3
View File
@@ -1,5 +1,5 @@
/* mz_strm_buf.h -- Stream for buffering reads/writes
Version 2.3.0, May 3rd, 2018
Version 2.3.5, July 9, 2018
part of the MiniZip project
This version of ioapi is designed to buffer IO.
@@ -11,8 +11,8 @@
See the accompanying LICENSE file for the full text of the license.
*/
#ifndef _MZ_STREAM_BUFFERED_H
#define _MZ_STREAM_BUFFERED_H
#ifndef MZ_STREAM_BUFFERED_H
#define MZ_STREAM_BUFFERED_H
#include <stdint.h>
+38 -8
View File
@@ -1,5 +1,5 @@
/* mz_strm_bzip.c -- Stream for bzip inflate/deflate
Version 2.3.0, May 3rd, 2018
Version 2.3.5, July 9, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
@@ -76,17 +76,25 @@ int32_t mz_stream_bzip_open(void *stream, const char *path, int32_t mode)
if (mode & MZ_OPEN_MODE_WRITE)
{
#ifdef MZ_ZIP_DECOMPRESS_ONLY
return MZ_SUPPORT_ERROR;
#else
bzip->bzstream.next_out = (char *)bzip->buffer;
bzip->bzstream.avail_out = sizeof(bzip->buffer);
bzip->error = BZ2_bzCompressInit(&bzip->bzstream, bzip->level, 0, 0);
#endif
}
else if (mode & MZ_OPEN_MODE_READ)
{
#ifdef MZ_ZIP_COMPRESS_ONLY
return MZ_SUPPORT_ERROR;
#else
bzip->bzstream.next_in = (char *)bzip->buffer;
bzip->bzstream.avail_in = 0;
bzip->error = BZ2_bzDecompressInit(&bzip->bzstream, 0, 0);
#endif
}
if (bzip->error != BZ_OK)
@@ -108,6 +116,9 @@ int32_t mz_stream_bzip_is_open(void *stream)
int32_t mz_stream_bzip_read(void *stream, void *buf, int32_t size)
{
#ifdef MZ_ZIP_COMPRESS_ONLY
return MZ_SUPPORT_ERROR;
#else
mz_stream_bzip *bzip = (mz_stream_bzip *)stream;
uint64_t total_in_before = 0;
uint64_t total_out_before = 0;
@@ -135,7 +146,7 @@ int32_t mz_stream_bzip_read(void *stream, void *buf, int32_t size)
bytes_to_read = sizeof(bzip->buffer);
if (bzip->max_total_in > 0)
{
if ((bzip->max_total_in - bzip->total_in) < sizeof(bzip->buffer))
if ((bzip->max_total_in - bzip->total_in) < (int64_t)sizeof(bzip->buffer))
bytes_to_read = (int32_t)(bzip->max_total_in - bzip->total_in);
}
@@ -189,6 +200,7 @@ int32_t mz_stream_bzip_read(void *stream, void *buf, int32_t size)
return bzip->error;
return total_out;
#endif
}
static int32_t mz_stream_bzip_flush(void *stream)
@@ -252,16 +264,20 @@ static int32_t mz_stream_bzip_compress(void *stream, int flush)
int32_t mz_stream_bzip_write(void *stream, const void *buf, int32_t size)
{
mz_stream_bzip *bzip = (mz_stream_bzip *)stream;
int32_t err = size;
bzip->bzstream.next_in = (char *)buf;
#ifdef MZ_ZIP_DECOMPRESS_ONLY
MZ_UNUSED(bzip);
err = MZ_SUPPORT_ERROR;
#else
bzip->bzstream.next_in = (char *)(intptr_t)buf;
bzip->bzstream.avail_in = (unsigned int)size;
mz_stream_bzip_compress(stream, BZ_RUN);
bzip->total_in += size;
return size;
#endif
return err;
}
int64_t mz_stream_bzip_tell(void *stream)
@@ -286,14 +302,25 @@ int32_t mz_stream_bzip_close(void *stream)
if (bzip->mode & MZ_OPEN_MODE_WRITE)
{
mz_stream_bzip_compress(stream, BZ_FINISH);
mz_stream_bzip_flush(stream);
#ifdef MZ_ZIP_DECOMPRESS_ONLY
return MZ_SUPPORT_ERROR;
#else
if (bzip->total_in > 0)
{
mz_stream_bzip_compress(stream, BZ_FINISH);
mz_stream_bzip_flush(stream);
}
BZ2_bzCompressEnd(&bzip->bzstream);
#endif
}
else if (bzip->mode & MZ_OPEN_MODE_READ)
{
#ifdef MZ_ZIP_COMPRESS_ONLY
return MZ_SUPPORT_ERROR;
#else
BZ2_bzDecompressEnd(&bzip->bzstream);
#endif
}
bzip->initialized = 0;
@@ -317,6 +344,9 @@ int32_t mz_stream_bzip_get_prop_int64(void *stream, int32_t prop, int64_t *value
case MZ_STREAM_PROP_TOTAL_IN:
*value = bzip->total_in;
return MZ_OK;
case MZ_STREAM_PROP_TOTAL_IN_MAX:
*value = bzip->max_total_in;
return MZ_OK;
case MZ_STREAM_PROP_TOTAL_OUT:
*value = bzip->total_out;
return MZ_OK;
+3 -3
View File
@@ -1,5 +1,5 @@
/* mz_strm_bzip.h -- Stream for bzip inflate/deflate
Version 2.3.0, May 3rd, 2018
Version 2.3.5, July 9, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
@@ -9,8 +9,8 @@
See the accompanying LICENSE file for the full text of the license.
*/
#ifndef _MZ_STREAM_BZIP_H
#define _MZ_STREAM_BZIP_H
#ifndef MZ_STREAM_BZIP_H
#define MZ_STREAM_BZIP_H
#include <stdint.h>
-326
View File
@@ -1,326 +0,0 @@
/* mz_strm_crypt.c -- Code for traditional PKWARE encryption
Version 2.3.0, May 3rd, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
Copyright (C) 1998-2005 Gilles Vollant
Modifications for Info-ZIP crypting
http://www.winimage.com/zLibDll/minizip.html
Copyright (C) 2003 Terry Thorsen
This code is a modified version of crypting code in Info-ZIP distribution
Copyright (C) 1990-2000 Info-ZIP. All rights reserved.
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.
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).
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "zlib.h"
#include "mz.h"
#include "mz_os.h"
#include "mz_strm.h"
#include "mz_strm_crypt.h"
/***************************************************************************/
#define RAND_HEAD_LEN 12
/***************************************************************************/
static mz_stream_vtbl mz_stream_crypt_vtbl = {
mz_stream_crypt_open,
mz_stream_crypt_is_open,
mz_stream_crypt_read,
mz_stream_crypt_write,
mz_stream_crypt_tell,
mz_stream_crypt_seek,
mz_stream_crypt_close,
mz_stream_crypt_error,
mz_stream_crypt_create,
mz_stream_crypt_delete,
mz_stream_crypt_get_prop_int64,
NULL
};
/***************************************************************************/
typedef struct mz_stream_crypt_s {
mz_stream stream;
int32_t error;
int16_t initialized;
uint8_t buffer[INT16_MAX];
int64_t total_in;
int64_t total_out;
uint32_t keys[3]; // keys defining the pseudo-random sequence
const uint32_t *crc_32_tab;
uint8_t verify1;
uint8_t verify2;
const char *password;
} mz_stream_crypt;
/***************************************************************************/
#define zdecode(keys,crc_32_tab,c) \
(mz_stream_crypt_update_keys(keys,crc_32_tab, c ^= mz_stream_crypt_decrypt_byte(keys)))
#define zencode(keys,crc_32_tab,c,t) \
(t = mz_stream_crypt_decrypt_byte(keys), mz_stream_crypt_update_keys(keys,crc_32_tab,c), t^(c))
/***************************************************************************/
static uint8_t mz_stream_crypt_decrypt_byte(uint32_t *keys)
{
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 */
temp = ((uint32_t)(*(keys+2)) & 0xffff) | 2;
return (uint8_t)(((temp * (temp ^ 1)) >> 8) & 0xff);
}
static uint8_t mz_stream_crypt_update_keys(uint32_t *keys, const uint32_t *crc_32_tab, int32_t c)
{
#define CRC32(c, b) ((*(crc_32_tab+(((uint32_t)(c) ^ (b)) & 0xff))) ^ ((c) >> 8))
(*(keys+0)) = (uint32_t)CRC32((*(keys+0)), c);
(*(keys+1)) += (*(keys+0)) & 0xff;
(*(keys+1)) = (*(keys+1)) * 134775813L + 1;
{
register int32_t keyshift = (int32_t)((*(keys + 1)) >> 24);
(*(keys+2)) = (uint32_t)CRC32((*(keys+2)), keyshift);
}
return (uint8_t)c;
}
static void mz_stream_crypt_init_keys(const char *password, uint32_t *keys, const uint32_t *crc_32_tab)
{
*(keys+0) = 305419896L;
*(keys+1) = 591751049L;
*(keys+2) = 878082192L;
while (*password != 0)
{
mz_stream_crypt_update_keys(keys, crc_32_tab, *password);
password += 1;
}
}
/***************************************************************************/
int32_t mz_stream_crypt_open(void *stream, const char *path, int32_t mode)
{
mz_stream_crypt *crypt = (mz_stream_crypt *)stream;
uint16_t t = 0;
int16_t i = 0;
uint8_t verify1 = 0;
uint8_t verify2 = 0;
uint8_t header[RAND_HEAD_LEN];
const char *password = path;
crypt->total_in = 0;
crypt->total_out = 0;
crypt->initialized = 0;
if (mz_stream_is_open(crypt->stream.base) != MZ_OK)
return MZ_STREAM_ERROR;
if (password == NULL)
password = crypt->password;
if (password == NULL)
return MZ_STREAM_ERROR;
crypt->crc_32_tab = get_crc_table();
if (crypt->crc_32_tab == NULL)
return MZ_STREAM_ERROR;
mz_stream_crypt_init_keys(password, crypt->keys, crypt->crc_32_tab);
if (mode & MZ_OPEN_MODE_WRITE)
{
// First generate RAND_HEAD_LEN - 2 random bytes.
mz_os_rand(header, RAND_HEAD_LEN - 2);
// Encrypt random header (last two bytes is high word of crc)
for (i = 0; i < RAND_HEAD_LEN - 2; i++)
header[i] = (uint8_t)zencode(crypt->keys, crypt->crc_32_tab, header[i], t);
header[i++] = (uint8_t)zencode(crypt->keys, crypt->crc_32_tab, crypt->verify1, t);
header[i++] = (uint8_t)zencode(crypt->keys, crypt->crc_32_tab, crypt->verify2, t);
if (mz_stream_write(crypt->stream.base, header, RAND_HEAD_LEN) != RAND_HEAD_LEN)
return MZ_STREAM_ERROR;
crypt->total_out += RAND_HEAD_LEN;
}
else if (mode & MZ_OPEN_MODE_READ)
{
if (mz_stream_read(crypt->stream.base, header, RAND_HEAD_LEN) != RAND_HEAD_LEN)
return MZ_STREAM_ERROR;
for (i = 0; i < RAND_HEAD_LEN - 2; i++)
header[i] = (uint8_t)zdecode(crypt->keys, crypt->crc_32_tab, header[i]);
verify1 = (uint8_t)zdecode(crypt->keys, crypt->crc_32_tab, header[i++]);
verify2 = (uint8_t)zdecode(crypt->keys, crypt->crc_32_tab, header[i++]);
// Older versions used 2 byte check, newer versions use 1 byte check.
if ((verify2 != 0) && (verify2 != crypt->verify2))
return MZ_PASSWORD_ERROR;
crypt->total_in += RAND_HEAD_LEN;
}
crypt->initialized = 1;
return MZ_OK;
}
int32_t mz_stream_crypt_is_open(void *stream)
{
mz_stream_crypt *crypt = (mz_stream_crypt *)stream;
if (crypt->initialized == 0)
return MZ_STREAM_ERROR;
return MZ_OK;
}
int32_t mz_stream_crypt_read(void *stream, void *buf, int32_t size)
{
mz_stream_crypt *crypt = (mz_stream_crypt *)stream;
uint8_t *buf_ptr = (uint8_t *)buf;
uint32_t read = 0;
uint32_t i = 0;
read = mz_stream_read(crypt->stream.base, buf, size);
for (i = 0; i < read; i++)
buf_ptr[i] = (uint8_t)zdecode(crypt->keys, crypt->crc_32_tab, buf_ptr[i]);
crypt->total_in += read;
return read;
}
int32_t mz_stream_crypt_write(void *stream, const void *buf, int32_t size)
{
mz_stream_crypt *crypt = (mz_stream_crypt *)stream;
const uint8_t *buf_ptr = (const uint8_t *)buf;
uint32_t written = 0;
uint16_t t = 0;
int32_t i = 0;
if (size > sizeof(crypt->buffer))
return MZ_STREAM_ERROR;
for (i = 0; i < size; i++)
crypt->buffer[i] = (uint8_t)zencode(crypt->keys, crypt->crc_32_tab, buf_ptr[i], t);
written = mz_stream_write(crypt->stream.base, crypt->buffer, size);
if (written > 0)
crypt->total_out += written;
return written;
}
int64_t mz_stream_crypt_tell(void *stream)
{
mz_stream_crypt *crypt = (mz_stream_crypt *)stream;
return mz_stream_tell(crypt->stream.base);
}
int32_t mz_stream_crypt_seek(void *stream, int64_t offset, int32_t origin)
{
mz_stream_crypt *crypt = (mz_stream_crypt *)stream;
return mz_stream_seek(crypt->stream.base, offset, origin);
}
int32_t mz_stream_crypt_close(void *stream)
{
mz_stream_crypt *crypt = (mz_stream_crypt *)stream;
crypt->initialized = 0;
return MZ_OK;
}
int32_t mz_stream_crypt_error(void *stream)
{
mz_stream_crypt *crypt = (mz_stream_crypt *)stream;
return crypt->error;
}
void mz_stream_crypt_set_password(void *stream, const char *password)
{
mz_stream_crypt *crypt = (mz_stream_crypt *)stream;
crypt->password = password;
}
void mz_stream_crypt_set_verify(void *stream, uint8_t verify1, uint8_t verify2)
{
mz_stream_crypt *crypt = (mz_stream_crypt *)stream;
crypt->verify1 = verify1;
crypt->verify2 = verify2;
}
void mz_stream_crypt_get_verify(void *stream, uint8_t *verify1, uint8_t *verify2)
{
mz_stream_crypt *crypt = (mz_stream_crypt *)stream;
*verify1 = crypt->verify1;
*verify2 = crypt->verify2;
}
int32_t mz_stream_crypt_get_prop_int64(void *stream, int32_t prop, int64_t *value)
{
mz_stream_crypt *crypt = (mz_stream_crypt *)stream;
switch (prop)
{
case MZ_STREAM_PROP_TOTAL_IN:
*value = crypt->total_in;
return MZ_OK;
case MZ_STREAM_PROP_TOTAL_OUT:
*value = crypt->total_out;
return MZ_OK;
}
return MZ_EXIST_ERROR;
}
void *mz_stream_crypt_create(void **stream)
{
mz_stream_crypt *crypt = NULL;
crypt = (mz_stream_crypt *)MZ_ALLOC(sizeof(mz_stream_crypt));
if (crypt != NULL)
{
memset(crypt, 0, sizeof(mz_stream_crypt));
crypt->stream.vtbl = &mz_stream_crypt_vtbl;
}
if (stream != NULL)
*stream = crypt;
return crypt;
}
void mz_stream_crypt_delete(void **stream)
{
mz_stream_crypt *crypt = NULL;
if (stream == NULL)
return;
crypt = (mz_stream_crypt *)*stream;
if (crypt != NULL)
MZ_FREE(crypt);
*stream = NULL;
}
void *mz_stream_crypt_get_interface(void)
{
return (void *)&mz_stream_crypt_vtbl;
}
-56
View File
@@ -1,56 +0,0 @@
/* mz_strm_crypt.h -- Code for traditional PKWARE encryption
Version 2.3.0, May 3rd, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
Copyright (C) 1998-2005 Gilles Vollant
Modifications for Info-ZIP crypting
http://www.winimage.com/zLibDll/minizip.html
Copyright (C) 2003 Terry Thorsen
This code is a modified version of crypting code in Info-ZIP distribution
Copyright (C) 1990-2000 Info-ZIP. All rights reserved.
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_STREAM_CRYPT_H
#define _MZ_STREAM_CRYPT_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/***************************************************************************/
int32_t mz_stream_crypt_open(void *stream, const char *filename, int32_t mode);
int32_t mz_stream_crypt_is_open(void *stream);
int32_t mz_stream_crypt_read(void *stream, void *buf, int32_t size);
int32_t mz_stream_crypt_write(void *stream, const void *buf, int32_t size);
int64_t mz_stream_crypt_tell(void *stream);
int32_t mz_stream_crypt_seek(void *stream, int64_t offset, int32_t origin);
int32_t mz_stream_crypt_close(void *stream);
int32_t mz_stream_crypt_error(void *stream);
void mz_stream_crypt_set_password(void *stream, const char *password);
void mz_stream_crypt_set_verify(void *stream, uint8_t verify1, uint8_t verify2);
void mz_stream_crypt_get_verify(void *stream, uint8_t *verify1, uint8_t *verify2);
int32_t mz_stream_crypt_get_prop_int64(void *stream, int32_t prop, int64_t *value);
void* mz_stream_crypt_create(void **stream);
void mz_stream_crypt_delete(void **stream);
void* mz_stream_crypt_get_interface(void);
/***************************************************************************/
#ifdef __cplusplus
}
#endif
#endif
+51 -11
View File
@@ -1,5 +1,5 @@
/* mz_strm_lzma.c -- Stream for lzma inflate/deflate
Version 2.3.0, May 3rd, 2018
Version 2.3.5, July 9, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
@@ -83,6 +83,10 @@ int32_t mz_stream_lzma_open(void *stream, const char *path, int32_t mode)
if (mode & MZ_OPEN_MODE_WRITE)
{
#ifdef MZ_ZIP_DECOMPRESS_ONLY
MZ_UNUSED(filters);
return MZ_SUPPORT_ERROR;
#else
lzma->lstream.next_out = lzma->buffer;
lzma->lstream.avail_out = sizeof(lzma->buffer);
@@ -104,9 +108,13 @@ int32_t mz_stream_lzma_open(void *stream, const char *path, int32_t mode)
lzma->total_out += MZ_LZMA_HEADER_SIZE;
lzma->error = lzma_alone_encoder(&lzma->lstream, &opt_lzma);
#endif
}
else if (mode & MZ_OPEN_MODE_READ)
{
#ifdef MZ_ZIP_COMPRESS_ONLY
return MZ_SUPPORT_ERROR;
#else
lzma->lstream.next_in = lzma->buffer;
lzma->lstream.avail_in = 0;
@@ -117,6 +125,7 @@ int32_t mz_stream_lzma_open(void *stream, const char *path, int32_t mode)
lzma->total_in += MZ_LZMA_HEADER_SIZE;
lzma->error = lzma_alone_decoder(&lzma->lstream, UINT64_MAX);
#endif
}
if (lzma->error != LZMA_OK)
@@ -137,6 +146,9 @@ int32_t mz_stream_lzma_is_open(void *stream)
int32_t mz_stream_lzma_read(void *stream, void *buf, int32_t size)
{
#ifdef MZ_ZIP_COMPRESS_ONLY
return MZ_SUPPORT_ERROR;
#else
mz_stream_lzma *lzma = (mz_stream_lzma *)stream;
uint64_t total_in_before = 0;
uint64_t total_out_before = 0;
@@ -161,7 +173,7 @@ int32_t mz_stream_lzma_read(void *stream, void *buf, int32_t size)
bytes_to_read = sizeof(lzma->buffer);
if (lzma->max_total_in > 0)
{
if ((lzma->max_total_in - lzma->total_in) < sizeof(lzma->buffer))
if ((lzma->max_total_in - lzma->total_in) < (int64_t)sizeof(lzma->buffer))
bytes_to_read = (int32_t)(lzma->max_total_in - lzma->total_in);
}
@@ -212,6 +224,7 @@ int32_t mz_stream_lzma_read(void *stream, void *buf, int32_t size)
return lzma->error;
return total_out;
#endif
}
static int32_t mz_stream_lzma_flush(void *stream)
@@ -262,7 +275,7 @@ static int32_t mz_stream_lzma_code(void *stream, int32_t flush)
lzma->buffer_len += out_bytes;
lzma->total_out += out_bytes;
}
while (lzma->lstream.avail_in > 0);
while ((lzma->lstream.avail_in > 0) || (flush == LZMA_FINISH && err == LZMA_OK));
return MZ_OK;
}
@@ -270,16 +283,20 @@ static int32_t mz_stream_lzma_code(void *stream, int32_t flush)
int32_t mz_stream_lzma_write(void *stream, const void *buf, int32_t size)
{
mz_stream_lzma *lzma = (mz_stream_lzma *)stream;
int32_t err = size;
lzma->lstream.next_in = (uint8_t*)buf;
#ifdef MZ_ZIP_DECOMPRESS_ONLY
MZ_UNUSED(lzma);
err = MZ_SUPPORT_ERROR;
#else
lzma->lstream.next_in = (uint8_t*)(intptr_t)buf;
lzma->lstream.avail_in = (size_t)size;
mz_stream_lzma_code(stream, LZMA_RUN);
lzma->total_in += size;
return size;
#endif
return err;
}
int64_t mz_stream_lzma_tell(void *stream)
@@ -304,14 +321,25 @@ int32_t mz_stream_lzma_close(void *stream)
if (lzma->mode & MZ_OPEN_MODE_WRITE)
{
mz_stream_lzma_code(stream, LZMA_FINISH);
mz_stream_lzma_flush(stream);
#ifdef MZ_ZIP_DECOMPRESS_ONLY
return MZ_SUPPORT_ERROR;
#else
if (lzma->total_in > 0)
{
mz_stream_lzma_code(stream, LZMA_FINISH);
mz_stream_lzma_flush(stream);
}
lzma_end(&lzma->lstream);
#endif
}
else if (lzma->mode & MZ_OPEN_MODE_READ)
{
#ifdef MZ_ZIP_COMPRESS_ONLY
return MZ_SUPPORT_ERROR;
#else
lzma_end(&lzma->lstream);
#endif
}
lzma->initialized = 0;
@@ -335,9 +363,15 @@ int32_t mz_stream_lzma_get_prop_int64(void *stream, int32_t prop, int64_t *value
case MZ_STREAM_PROP_TOTAL_IN:
*value = lzma->total_in;
return MZ_OK;
case MZ_STREAM_PROP_TOTAL_IN_MAX:
*value = lzma->max_total_in;
return MZ_OK;
case MZ_STREAM_PROP_TOTAL_OUT:
*value = lzma->total_out;
return MZ_OK;
case MZ_STREAM_PROP_TOTAL_OUT_MAX:
*value = lzma->max_total_out;
return MZ_OK;
case MZ_STREAM_PROP_HEADER_SIZE:
*value = MZ_LZMA_HEADER_SIZE;
return MZ_OK;
@@ -402,9 +436,15 @@ void *mz_stream_lzma_get_interface(void)
return (void *)&mz_stream_lzma_vtbl;
}
static int32_t mz_stream_lzma_crc32(int32_t value, const void *buf, int32_t size)
static int64_t mz_stream_lzma_crc32(int64_t value, const void *buf, int32_t size)
{
return lzma_crc32(buf, size, value);
return (int32_t)lzma_crc32(buf, size, (int32_t)value);
}
void *mz_stream_lzma_get_crc32_table(void)
{
extern const uint32_t lzma_crc32_table;
return (void *)lzma_crc32_table;
}
void *mz_stream_lzma_get_crc32_update(void)
+4 -3
View File
@@ -1,5 +1,5 @@
/* mz_strm_lzma.h -- Stream for lzma inflate/deflate
Version 2.3.0, May 3rd, 2018
Version 2.3.5, July 9, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
@@ -9,8 +9,8 @@
See the accompanying LICENSE file for the full text of the license.
*/
#ifndef _MZ_STREAM_LZMA_H
#define _MZ_STREAM_LZMA_H
#ifndef MZ_STREAM_LZMA_H
#define MZ_STREAM_LZMA_H
#include <stdint.h>
@@ -36,6 +36,7 @@ void* mz_stream_lzma_create(void **stream);
void mz_stream_lzma_delete(void **stream);
void* mz_stream_lzma_get_interface(void);
void* mz_stream_lzma_get_crc32_table(void);
void* mz_stream_lzma_get_crc32_update(void);
/***************************************************************************/
+9 -3
View File
@@ -1,5 +1,5 @@
/* mz_strm_mem.c -- Stream for memory access
Version 2.3.0, May 3rd, 2018
Version 2.3.5, July 9, 2018
part of the MiniZip project
This interface is designed to access memory rather than files.
@@ -214,12 +214,12 @@ void mz_stream_mem_set_buffer(void *stream, void *buf, int32_t size)
mem->limit = size;
}
int32_t mz_stream_mem_get_buffer(void *stream, void **buf)
int32_t mz_stream_mem_get_buffer(void *stream, const void **buf)
{
return mz_stream_mem_get_buffer_at(stream, 0, buf);
}
int32_t mz_stream_mem_get_buffer_at(void *stream, int64_t position, 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 == NULL || position < 0 || mem->size < position || mem->buffer == NULL)
@@ -228,6 +228,12 @@ int32_t mz_stream_mem_get_buffer_at(void *stream, int64_t position, void **buf)
return MZ_OK;
}
void mz_stream_mem_get_buffer_length(void *stream, int32_t *length)
{
mz_stream_mem *mem = (mz_stream_mem *)stream;
*length = mem->limit;
}
void mz_stream_mem_set_grow_size(void *stream, int32_t grow_size)
{
mz_stream_mem *mem = (mz_stream_mem *)stream;
+6 -7
View File
@@ -1,18 +1,16 @@
/* mz_strm_mem.h -- Stream for memory access
Version 2.3.0, May 3rd, 2018
Version 2.3.5, July 9, 2018
part of MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
Copyright (C) 2003 Justin Fletcher
Copyright (C) 1998-2003 Gilles Vollant
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_STREAM_MEM_H
#define _MZ_STREAM_MEM_H
#ifndef MZ_STREAM_MEM_H
#define MZ_STREAM_MEM_H
#include <stdint.h>
@@ -32,8 +30,9 @@ 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);
int32_t mz_stream_mem_get_buffer(void *stream, void **buf);
int32_t mz_stream_mem_get_buffer_at(void *stream, int64_t position, void **buf);
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);
void mz_stream_mem_get_buffer_length(void *stream, int32_t *length);
void mz_stream_mem_set_grow_size(void *stream, int32_t grow_size);
void* mz_stream_mem_create(void **stream);
+358
View File
@@ -0,0 +1,358 @@
/* mz_strm_pkcrypt.c -- Code for traditional PKWARE encryption
Version 2.3.5, July 9, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
Copyright (C) 1998-2005 Gilles Vollant
Modifications for Info-ZIP crypting
http://www.winimage.com/zLibDll/minizip.html
Copyright (C) 2003 Terry Thorsen
This code is a modified version of crypting code in Info-ZIP distribution
Copyright (C) 1990-2000 Info-ZIP. All rights reserved.
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.
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).
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_ZLIB
#include "zlib.h"
#endif
#include "mz.h"
#include "mz_os.h"
#include "mz_strm.h"
#ifdef HAVE_LZMA
#include "mz_strm_lzma.h"
#endif
#include "mz_strm_pkcrypt.h"
#ifdef HAVE_ZLIB
#include "mz_strm_zlib.h"
#endif
/***************************************************************************/
#define RAND_HEAD_LEN (12)
/***************************************************************************/
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,
NULL
};
/***************************************************************************/
// Define z_crc_t in zlib 1.2.5 and less or if using zlib-ng
#if (ZLIB_VERNUM < 0x1270) || defined(ZLIBNG_VERNUM) || !defined(HAVE_ZLIB)
#ifdef HAVE_ZLIB
typedef unsigned long z_crc_t;
#else
typedef uint32_t z_crc_t;
#endif
#endif
/***************************************************************************/
typedef struct mz_stream_pkcrypt_s {
mz_stream stream;
int32_t error;
int16_t initialized;
uint8_t buffer[INT16_MAX];
int64_t total_in;
int64_t total_out;
uint32_t keys[3]; // keys defining the pseudo-random sequence
const z_crc_t *crc_32_tab;
uint8_t verify1;
uint8_t verify2;
const char *password;
} mz_stream_pkcrypt;
/***************************************************************************/
#define zdecode(keys,crc_32_tab,c) \
(mz_stream_pkcrypt_update_keys(keys,crc_32_tab, c ^= mz_stream_pkcrypt_decrypt_byte(keys)))
#define zencode(keys,crc_32_tab,c,t) \
(t = mz_stream_pkcrypt_decrypt_byte(keys), mz_stream_pkcrypt_update_keys(keys,crc_32_tab,c), t^(c))
/***************************************************************************/
static uint8_t mz_stream_pkcrypt_decrypt_byte(uint32_t *keys)
{
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 */
temp = ((uint32_t)(*(keys+2)) & 0xffff) | 2;
return (uint8_t)(((temp * (temp ^ 1)) >> 8) & 0xff);
}
static uint8_t mz_stream_pkcrypt_update_keys(uint32_t *keys, const z_crc_t *crc_32_tab, int32_t c)
{
#define CRC32(c, b) ((*(crc_32_tab+(((uint32_t)(c) ^ (b)) & 0xff))) ^ ((c) >> 8))
(*(keys+0)) = (uint32_t)CRC32((*(keys+0)), c);
(*(keys+1)) += (*(keys+0)) & 0xff;
(*(keys+1)) = (*(keys+1)) * 134775813L + 1;
{
register int32_t keyshift = (int32_t)((*(keys + 1)) >> 24);
(*(keys+2)) = (uint32_t)CRC32((*(keys+2)), keyshift);
}
return (uint8_t)c;
}
static void mz_stream_pkcrypt_init_keys(const char *password, uint32_t *keys, const z_crc_t *crc_32_tab)
{
*(keys+0) = 305419896L;
*(keys+1) = 591751049L;
*(keys+2) = 878082192L;
while (*password != 0)
{
mz_stream_pkcrypt_update_keys(keys, crc_32_tab, *password);
password += 1;
}
}
/***************************************************************************/
int32_t mz_stream_pkcrypt_open(void *stream, const char *path, int32_t mode)
{
mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
uint16_t t = 0;
int16_t i = 0;
//uint8_t verify1 = 0;
uint8_t verify2 = 0;
uint8_t header[RAND_HEAD_LEN];
const char *password = path;
pkcrypt->total_in = 0;
pkcrypt->total_out = 0;
pkcrypt->initialized = 0;
if (mz_stream_is_open(pkcrypt->stream.base) != MZ_OK)
return MZ_STREAM_ERROR;
if (password == NULL)
password = pkcrypt->password;
if (password == NULL)
return MZ_STREAM_ERROR;
#ifdef HAVE_ZLIB
pkcrypt->crc_32_tab = (z_crc_t *)mz_stream_zlib_get_crc32_table();
#elif defined(HAVE_LZMA)
pkcrypt->crc_32_tab = (z_crc_t *)mz_stream_lzma_get_crc32_table();
#else
#error ZLIB or LZMA required for CRC32
#endif
if (pkcrypt->crc_32_tab == NULL)
return MZ_STREAM_ERROR;
mz_stream_pkcrypt_init_keys(password, pkcrypt->keys, pkcrypt->crc_32_tab);
if (mode & MZ_OPEN_MODE_WRITE)
{
// First generate RAND_HEAD_LEN - 2 random bytes.
mz_os_rand(header, RAND_HEAD_LEN - 2);
// Encrypt random header (last two bytes is high word of crc)
for (i = 0; i < RAND_HEAD_LEN - 2; i++)
header[i] = (uint8_t)zencode(pkcrypt->keys, pkcrypt->crc_32_tab, header[i], t);
header[i++] = (uint8_t)zencode(pkcrypt->keys, pkcrypt->crc_32_tab, pkcrypt->verify1, t);
header[i++] = (uint8_t)zencode(pkcrypt->keys, pkcrypt->crc_32_tab, pkcrypt->verify2, t);
if (mz_stream_write(pkcrypt->stream.base, header, RAND_HEAD_LEN) != RAND_HEAD_LEN)
return MZ_STREAM_ERROR;
pkcrypt->total_out += RAND_HEAD_LEN;
}
else if (mode & MZ_OPEN_MODE_READ)
{
if (mz_stream_read(pkcrypt->stream.base, header, RAND_HEAD_LEN) != RAND_HEAD_LEN)
return MZ_STREAM_ERROR;
for (i = 0; i < RAND_HEAD_LEN - 2; i++)
header[i] = (uint8_t)zdecode(pkcrypt->keys, pkcrypt->crc_32_tab, header[i]);
//verify1 = (uint8_t)zdecode(crypt->keys, crypt->crc_32_tab, header[i++]);
verify2 = (uint8_t)zdecode(pkcrypt->keys, pkcrypt->crc_32_tab, header[i++]);
// Older versions used 2 byte check, newer versions use 1 byte check.
if ((verify2 != 0) && (verify2 != pkcrypt->verify2))
return MZ_PASSWORD_ERROR;
pkcrypt->total_in += RAND_HEAD_LEN;
}
pkcrypt->initialized = 1;
return MZ_OK;
}
int32_t mz_stream_pkcrypt_is_open(void *stream)
{
mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
if (pkcrypt->initialized == 0)
return MZ_STREAM_ERROR;
return MZ_OK;
}
int32_t mz_stream_pkcrypt_read(void *stream, void *buf, int32_t size)
{
mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
uint8_t *buf_ptr = (uint8_t *)buf;
int32_t read = 0;
int32_t i = 0;
read = mz_stream_read(pkcrypt->stream.base, buf, size);
for (i = 0; i < read; i++)
buf_ptr[i] = (uint8_t)zdecode(pkcrypt->keys, pkcrypt->crc_32_tab, buf_ptr[i]);
pkcrypt->total_in += read;
return read;
}
int32_t mz_stream_pkcrypt_write(void *stream, const void *buf, int32_t size)
{
mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
const uint8_t *buf_ptr = (const uint8_t *)buf;
int32_t written = 0;
int32_t i = 0;
uint16_t t = 0;
if (size > (int32_t)sizeof(pkcrypt->buffer))
return MZ_STREAM_ERROR;
for (i = 0; i < size; i++)
pkcrypt->buffer[i] = (uint8_t)zencode(pkcrypt->keys, pkcrypt->crc_32_tab, buf_ptr[i], t);
written = mz_stream_write(pkcrypt->stream.base, pkcrypt->buffer, size);
if (written > 0)
pkcrypt->total_out += written;
return written;
}
int64_t mz_stream_pkcrypt_tell(void *stream)
{
mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
return mz_stream_tell(pkcrypt->stream.base);
}
int32_t mz_stream_pkcrypt_seek(void *stream, int64_t offset, int32_t origin)
{
mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
return mz_stream_seek(pkcrypt->stream.base, offset, origin);
}
int32_t mz_stream_pkcrypt_close(void *stream)
{
mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
pkcrypt->initialized = 0;
return MZ_OK;
}
int32_t mz_stream_pkcrypt_error(void *stream)
{
mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
return pkcrypt->error;
}
void mz_stream_pkcrypt_set_password(void *stream, const char *password)
{
mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
pkcrypt->password = password;
}
void mz_stream_pkcrypt_set_verify(void *stream, uint8_t verify1, uint8_t verify2)
{
mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
pkcrypt->verify1 = verify1;
pkcrypt->verify2 = verify2;
}
void mz_stream_pkcrypt_get_verify(void *stream, uint8_t *verify1, uint8_t *verify2)
{
mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
*verify1 = pkcrypt->verify1;
*verify2 = pkcrypt->verify2;
}
int32_t mz_stream_pkcrypt_get_prop_int64(void *stream, int32_t prop, int64_t *value)
{
mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
switch (prop)
{
case MZ_STREAM_PROP_TOTAL_IN:
*value = pkcrypt->total_in;
return MZ_OK;
case MZ_STREAM_PROP_TOTAL_OUT:
*value = pkcrypt->total_out;
return MZ_OK;
case MZ_STREAM_PROP_HEADER_SIZE:
*value = RAND_HEAD_LEN;
return MZ_OK;
case MZ_STREAM_PROP_FOOTER_SIZE:
*value = 0;
return MZ_OK;
}
return MZ_EXIST_ERROR;
}
void *mz_stream_pkcrypt_create(void **stream)
{
mz_stream_pkcrypt *pkcrypt = NULL;
pkcrypt = (mz_stream_pkcrypt *)MZ_ALLOC(sizeof(mz_stream_pkcrypt));
if (pkcrypt != NULL)
{
memset(pkcrypt, 0, sizeof(mz_stream_pkcrypt));
pkcrypt->stream.vtbl = &mz_stream_pkcrypt_vtbl;
}
if (stream != NULL)
*stream = pkcrypt;
return pkcrypt;
}
void mz_stream_pkcrypt_delete(void **stream)
{
mz_stream_pkcrypt *pkcrypt = NULL;
if (stream == NULL)
return;
pkcrypt = (mz_stream_pkcrypt *)*stream;
if (pkcrypt != NULL)
MZ_FREE(pkcrypt);
*stream = NULL;
}
void *mz_stream_pkcrypt_get_interface(void)
{
return (void *)&mz_stream_pkcrypt_vtbl;
}
+48
View File
@@ -0,0 +1,48 @@
/* mz_strm_pkcrypt.h -- Code for traditional PKWARE encryption
Version 2.3.5, July 9, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
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_STREAM_PKCRYPT_H
#define MZ_STREAM_PKCRYPT_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/***************************************************************************/
int32_t mz_stream_pkcrypt_open(void *stream, const char *filename, int32_t mode);
int32_t mz_stream_pkcrypt_is_open(void *stream);
int32_t mz_stream_pkcrypt_read(void *stream, void *buf, int32_t size);
int32_t mz_stream_pkcrypt_write(void *stream, const void *buf, int32_t size);
int64_t mz_stream_pkcrypt_tell(void *stream);
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);
int32_t mz_stream_pkcrypt_get_prop_int64(void *stream, int32_t prop, int64_t *value);
void* mz_stream_pkcrypt_create(void **stream);
void mz_stream_pkcrypt_delete(void **stream);
void* mz_stream_pkcrypt_get_interface(void);
/***************************************************************************/
#ifdef __cplusplus
}
#endif
#endif
+1 -1
View File
@@ -1,5 +1,5 @@
/* mz_strm_posix.c -- Stream for filesystem access for posix/linux
Version 2.3.0, May 3rd, 2018
Version 2.3.5, July 9, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
+3 -8
View File
@@ -1,21 +1,16 @@
/* mz_strm_posix.h -- Stream for filesystem access for posix/linux
Version 2.3.0, May 3rd, 2018
Version 2.3.5, July 9, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
Copyright (C) 2009-2010 Mathias Svensson
Modifications for Zip64 support
http://result42.com
Copyright (C) 1998-2010 Gilles Vollant
http://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_STREAM_POSIX_H
#define _MZ_STREAM_POSIX_H
#ifndef MZ_STREAM_POSIX_H
#define MZ_STREAM_POSIX_H
#include <stdint.h>
+5 -5
View File
@@ -1,5 +1,5 @@
/* mz_strm_split.c -- Stream for split files
Version 2.3.0, May 3rd, 2018
Version 2.3.5, July 9, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
@@ -87,7 +87,7 @@ static int32_t mz_stream_split_open_disk(void *stream, int32_t number_disk)
// Construct disk path
if (disk_part > 0)
{
for (i = strlen(split->path_disk) - 1; i >= 0; i -= 1)
for (i = (int32_t)strlen(split->path_disk) - 1; i >= 0; i -= 1)
{
if (split->path_disk[i] != '.')
continue;
@@ -180,11 +180,11 @@ int32_t mz_stream_split_open(void *stream, const char *path, int32_t mode)
split->mode = mode;
split->path_cd_size = strlen(path) + 1;
split->path_cd_size = (int32_t)strlen(path) + 1;
split->path_cd = (char *)MZ_ALLOC(split->path_cd_size);
strncpy(split->path_cd, path, split->path_cd_size);
split->path_disk_size = strlen(path) + 10;
split->path_disk_size = (int32_t)strlen(path) + 10;
split->path_disk = (char *)MZ_ALLOC(split->path_disk_size);
strncpy(split->path_disk, path, split->path_disk_size);
@@ -255,7 +255,7 @@ int32_t mz_stream_split_write(void *stream, const void *buf, int32_t size)
int32_t bytes_avail = 0;
int32_t number_disk = -1;
int32_t err = MZ_OK;
uint8_t *buf_ptr = (uint8_t *)buf;
const uint8_t *buf_ptr = (const uint8_t *)buf;
while (bytes_left > 0)
{
+3 -3
View File
@@ -1,5 +1,5 @@
/* mz_strm_split.h -- Stream for split files
Version 2.3.0, May 3rd, 2018
Version 2.3.5, July 9, 2018
part of MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
@@ -9,8 +9,8 @@
See the accompanying LICENSE file for the full text of the license.
*/
#ifndef _MZ_STREAM_SPLIT_H
#define _MZ_STREAM_SPLIT_H
#ifndef MZ_STREAM_SPLIT_H
#define MZ_STREAM_SPLIT_H
#include <stdint.h>
+5 -5
View File
@@ -1,5 +1,5 @@
/* mz_strm_win32.c -- Stream for filesystem access for windows
Version 2.3.0, May 3rd, 2018
Version 2.3.5, July 9, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
@@ -32,9 +32,9 @@
# define INVALID_SET_FILE_POINTER ((DWORD)-1)
#endif
#if defined(WINAPI_FAMILY_ONE_PARTITION) && !defined(MZ_USE_WINRT_API)
#if defined(WINAPI_FAMILY_ONE_PARTITION) && !defined(MZ_WINRT_API)
# if WINAPI_FAMILY_ONE_PARTITION(WINAPI_FAMILY, WINAPI_PARTITION_APP)
# define MZ_USE_WINRT_API 1
# define MZ_WINRT_API 1
# endif
#endif
@@ -106,7 +106,7 @@ int32_t mz_stream_win32_open(void *stream, const char *path, int32_t mode)
MultiByteToWideChar(CP_UTF8, 0, path, -1, path_wide, path_wide_size);
#ifdef MZ_USE_WINRT_API
#ifdef MZ_WINRT_API
win32->handle = CreateFile2W(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);
@@ -172,7 +172,7 @@ int32_t mz_stream_win32_write(void *stream, const void *buf, int32_t size)
static int32_t mz_stream_win32_seekinternal(HANDLE handle, LARGE_INTEGER large_pos, LARGE_INTEGER *new_pos, uint32_t move_method)
{
#ifdef MZ_USE_WINRT_API
#ifdef MZ_WINRT_API
return SetFilePointerEx(handle, pos, newPos, dwMoveMethod);
#else
LONG high_part = large_pos.HighPart;
+3 -8
View File
@@ -1,21 +1,16 @@
/* mz_sstrm_win32.h -- Stream for filesystem access for windows
Version 2.3.0, May 3rd, 2018
Version 2.3.5, July 9, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
Copyright (C) 2009-2010 Mathias Svensson
Modifications for Zip64 support
http://result42.com
Copyright (C) 1998-2010 Gilles Vollant
http://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_STREAM_WIN32_H
#define _MZ_STREAM_WIN32_H
#ifndef MZ_STREAM_WIN32_H
#define MZ_STREAM_WIN32_H
#include <stdint.h>
+45 -10
View File
@@ -1,5 +1,5 @@
/* mz_strm_zlib.c -- Stream for zlib inflate/deflate
Version 2.3.0, May 3rd, 2018
Version 2.3.5, July 9, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
@@ -84,17 +84,25 @@ int32_t mz_stream_zlib_open(void *stream, const char *path, int32_t mode)
if (mode & MZ_OPEN_MODE_WRITE)
{
#ifdef MZ_ZIP_DECOMPRESS_ONLY
return MZ_SUPPORT_ERROR;
#else
zlib->zstream.next_out = zlib->buffer;
zlib->zstream.avail_out = sizeof(zlib->buffer);
zlib->error = deflateInit2(&zlib->zstream, (int8_t)zlib->level, Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
#endif
}
else if (mode & MZ_OPEN_MODE_READ)
{
#ifdef MZ_ZIP_COMPRESS_ONLY
return MZ_SUPPORT_ERROR;
#else
zlib->zstream.next_in = zlib->buffer;
zlib->zstream.avail_in = 0;
zlib->error = inflateInit2(&zlib->zstream, -MAX_WBITS);
#endif
}
if (zlib->error != Z_OK)
@@ -115,6 +123,9 @@ int32_t mz_stream_zlib_is_open(void *stream)
int32_t mz_stream_zlib_read(void *stream, void *buf, int32_t size)
{
#ifdef MZ_ZIP_COMPRESS_ONLY
return MZ_SUPPORT_ERROR;
#else
mz_stream_zlib *zlib = (mz_stream_zlib *)stream;
uint64_t total_in_before = 0;
uint64_t total_in_after = 0;
@@ -139,7 +150,7 @@ int32_t mz_stream_zlib_read(void *stream, void *buf, int32_t size)
bytes_to_read = sizeof(zlib->buffer);
if (zlib->max_total_in > 0)
{
if ((zlib->max_total_in - zlib->total_in) < sizeof(zlib->buffer))
if ((zlib->max_total_in - zlib->total_in) < (int64_t)sizeof(zlib->buffer))
bytes_to_read = (int32_t)(zlib->max_total_in - zlib->total_in);
}
@@ -194,6 +205,7 @@ int32_t mz_stream_zlib_read(void *stream, void *buf, int32_t size)
return zlib->error;
return total_out;
#endif
}
static int32_t mz_stream_zlib_flush(void *stream)
@@ -254,16 +266,20 @@ static int32_t mz_stream_zlib_deflate(void *stream, int flush)
int32_t mz_stream_zlib_write(void *stream, const void *buf, int32_t size)
{
mz_stream_zlib *zlib = (mz_stream_zlib *)stream;
int32_t err = size;
zlib->zstream.next_in = (Bytef*)buf;
#ifdef MZ_ZIP_DECOMPRESS_ONLY
MZ_UNUSED(zlib);
err = MZ_SUPPORT_ERROR;
#else
zlib->zstream.next_in = (Bytef*)(intptr_t)buf;
zlib->zstream.avail_in = (uInt)size;
mz_stream_zlib_deflate(stream, Z_NO_FLUSH);
zlib->total_in += size;
return size;
#endif
return err;
}
int64_t mz_stream_zlib_tell(void *stream)
@@ -289,14 +305,25 @@ int32_t mz_stream_zlib_close(void *stream)
if (zlib->mode & MZ_OPEN_MODE_WRITE)
{
mz_stream_zlib_deflate(stream, Z_FINISH);
mz_stream_zlib_flush(stream);
#ifdef MZ_ZIP_DECOMPRESS_ONLY
return MZ_SUPPORT_ERROR;
#else
if (zlib->total_in > 0)
{
mz_stream_zlib_deflate(stream, Z_FINISH);
mz_stream_zlib_flush(stream);
}
deflateEnd(&zlib->zstream);
#endif
}
else if (zlib->mode & MZ_OPEN_MODE_READ)
{
#ifdef MZ_ZIP_COMPRESS_ONLY
return MZ_SUPPORT_ERROR;
#else
inflateEnd(&zlib->zstream);
#endif
}
zlib->initialized = 0;
@@ -320,6 +347,9 @@ int32_t mz_stream_zlib_get_prop_int64(void *stream, int32_t prop, int64_t *value
case MZ_STREAM_PROP_TOTAL_IN:
*value = zlib->total_in;
return MZ_OK;
case MZ_STREAM_PROP_TOTAL_IN_MAX:
*value = zlib->max_total_in;
return MZ_OK;
case MZ_STREAM_PROP_TOTAL_OUT:
*value = zlib->total_out;
return MZ_OK;
@@ -378,9 +408,14 @@ void *mz_stream_zlib_get_interface(void)
return (void *)&mz_stream_zlib_vtbl;
}
static int32_t mz_stream_zlib_crc32(int32_t value, const void *buf, int32_t size)
static int64_t mz_stream_zlib_crc32(int64_t value, const void *buf, int32_t size)
{
return crc32(value, buf, size);
return crc32((z_crc_t)value, buf, size);
}
void *mz_stream_zlib_get_crc32_table(void)
{
return (void *)get_crc_table();
}
void *mz_stream_zlib_get_crc32_update(void)
+4 -3
View File
@@ -1,5 +1,5 @@
/* mz_strm_zlib.h -- Stream for zlib inflate/deflate
Version 2.3.0, May 3rd, 2018
Version 2.3.5, July 9, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
@@ -9,8 +9,8 @@
See the accompanying LICENSE file for the full text of the license.
*/
#ifndef _MZ_STREAM_ZLIB_H
#define _MZ_STREAM_ZLIB_H
#ifndef MZ_STREAM_ZLIB_H
#define MZ_STREAM_ZLIB_H
#include <stdint.h>
@@ -36,6 +36,7 @@ void* mz_stream_zlib_create(void **stream);
void mz_stream_zlib_delete(void **stream);
void* mz_stream_zlib_get_interface(void);
void* mz_stream_zlib_get_crc32_table(void);
void* mz_stream_zlib_get_crc32_update(void);
/***************************************************************************/
+42 -25
View File
@@ -1,5 +1,5 @@
/* zip.c -- Zip manipulation
Version 2.3.0, May 3rd, 2018
Version 2.3.5, July 9, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
@@ -30,12 +30,12 @@
#ifdef HAVE_BZIP2
# include "mz_strm_bzip.h"
#endif
#ifdef HAVE_CRYPT
# include "mz_strm_crypt.h"
#endif
#ifdef HAVE_LZMA
# include "mz_strm_lzma.h"
#endif
#ifdef HAVE_PKCRYPT
# include "mz_strm_pkcrypt.h"
#endif
#ifdef HAVE_ZLIB
# include "mz_strm_zlib.h"
#endif
@@ -54,7 +54,6 @@
#define MZ_ZIP_SIZE_CD_ITEM (0x2e)
#define MZ_ZIP_SIZE_CD_LOCATOR64 (0x14)
#define MZ_ZIP_SIZE_LOCALHEADER (0x1e)
#define MZ_ZIP_EXTENSION_ZIP64 (0x0001)
#define MZ_ZIP_EXTENSION_NTFS (0x000a)
@@ -87,7 +86,6 @@ typedef struct mz_zip_s
uint16_t entry_scanned;
uint16_t entry_opened; // 1 if a file in the zip is currently writ.
uint64_t entry_read;
int64_t number_entry;
@@ -695,7 +693,7 @@ static int32_t mz_zip_entry_read_header(void *stream, uint8_t local, mz_zip_file
if ((err == MZ_OK) && (file_info->filename_size > 0))
{
mz_stream_mem_get_buffer(file_info_stream, (void **)&file_info->filename);
mz_stream_mem_get_buffer(file_info_stream, (const void **)&file_info->filename);
err = mz_stream_copy(file_info_stream, stream, file_info->filename_size);
if (err == MZ_OK)
@@ -706,7 +704,7 @@ static int32_t mz_zip_entry_read_header(void *stream, uint8_t local, mz_zip_file
if ((err == MZ_OK) && (file_info->extrafield_size > 0))
{
mz_stream_mem_get_buffer_at(file_info_stream, seek, (void **)&file_info->extrafield);
mz_stream_mem_get_buffer_at(file_info_stream, seek, (const void **)&file_info->extrafield);
err = mz_stream_copy(file_info_stream, stream, file_info->extrafield_size);
if (err == MZ_OK)
@@ -740,7 +738,8 @@ static int32_t mz_zip_entry_read_header(void *stream, uint8_t local, mz_zip_file
// NTFS extra field
else if (extra_header_id == MZ_ZIP_EXTENSION_NTFS)
{
err = mz_stream_read_uint32(file_info_stream, &reserved);
if (err == MZ_OK)
err = mz_stream_read_uint32(file_info_stream, &reserved);
extra_data_size_read = 4;
while ((err == MZ_OK) && (extra_data_size_read < extra_data_size))
@@ -749,7 +748,7 @@ static int32_t mz_zip_entry_read_header(void *stream, uint8_t local, mz_zip_file
if (err == MZ_OK)
err = mz_stream_read_uint16(file_info_stream, &ntfs_attrib_size);
if ((ntfs_attrib_id == 0x01) && (ntfs_attrib_size == 24))
if ((err == MZ_OK) && (ntfs_attrib_id == 0x01) && (ntfs_attrib_size == 24))
{
err = mz_stream_read_uint64(file_info_stream, &ntfs_time);
mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->modified_date);
@@ -767,7 +766,8 @@ static int32_t mz_zip_entry_read_header(void *stream, uint8_t local, mz_zip_file
}
else
{
err = mz_stream_seek(file_info_stream, ntfs_attrib_size, MZ_SEEK_CUR);
if (err == MZ_OK)
err = mz_stream_seek(file_info_stream, ntfs_attrib_size, MZ_SEEK_CUR);
}
extra_data_size_read += ntfs_attrib_size + 4;
@@ -812,7 +812,7 @@ static int32_t mz_zip_entry_read_header(void *stream, uint8_t local, mz_zip_file
if ((err == MZ_OK) && (file_info->comment_size > 0))
{
mz_stream_mem_get_buffer_at(file_info_stream, seek, (void **)&file_info->comment);
mz_stream_mem_get_buffer_at(file_info_stream, seek, (const void **)&file_info->comment);
err = mz_stream_copy(file_info_stream, stream, file_info->comment_size);
if (err == MZ_OK)
@@ -1074,7 +1074,7 @@ static int32_t mz_zip_entry_open_int(void *handle, int16_t compression_method, i
{
mz_zip *zip = (mz_zip *)handle;
int64_t max_total_in = 0;
int64_t total_in = 0;
int64_t header_size = 0;
int64_t footer_size = 0;
int32_t err = MZ_OK;
@@ -1114,7 +1114,7 @@ static int32_t mz_zip_entry_open_int(void *handle, int16_t compression_method, i
else
#endif
{
#ifdef HAVE_CRYPT
#ifdef HAVE_PKCRYPT
uint8_t verify1 = 0;
uint8_t verify2 = 0;
@@ -1136,9 +1136,9 @@ static int32_t mz_zip_entry_open_int(void *handle, int16_t compression_method, i
verify2 = (uint8_t)((zip->file_info.crc >> 24) & 0xff);
}
mz_stream_crypt_create(&zip->crypt_stream);
mz_stream_crypt_set_password(zip->crypt_stream, password);
mz_stream_crypt_set_verify(zip->crypt_stream, verify1, verify2);
mz_stream_pkcrypt_create(&zip->crypt_stream);
mz_stream_pkcrypt_set_password(zip->crypt_stream, password);
mz_stream_pkcrypt_set_verify(zip->crypt_stream, verify1, verify2);
#endif
}
}
@@ -1157,8 +1157,10 @@ static int32_t mz_zip_entry_open_int(void *handle, int16_t compression_method, i
{
if (zip->compression_method == MZ_COMPRESS_METHOD_RAW)
mz_stream_raw_create(&zip->compress_stream);
#ifdef HAVE_ZLIB
else if (zip->compression_method == MZ_COMPRESS_METHOD_DEFLATE)
mz_stream_zlib_create(&zip->compress_stream);
#endif
#ifdef HAVE_BZIP2
else if (zip->compression_method == MZ_COMPRESS_METHOD_BZIP2)
mz_stream_bzip_create(&zip->compress_stream);
@@ -1182,10 +1184,13 @@ static int32_t mz_zip_entry_open_int(void *handle, int16_t compression_method, i
if (zip->compression_method == MZ_COMPRESS_METHOD_RAW || zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED)
{
max_total_in = zip->file_info.compressed_size;
mz_stream_set_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, max_total_in);
if (mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_HEADER_SIZE, &header_size) == MZ_OK)
max_total_in -= header_size;
if (mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_FOOTER_SIZE, &footer_size) == MZ_OK)
max_total_in -= footer_size;
if (mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_TOTAL_IN, &total_in) == MZ_OK)
max_total_in -= total_in;
mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, max_total_in);
}
if (zip->compression_method == MZ_COMPRESS_METHOD_LZMA && (zip->file_info.flag & MZ_ZIP_FLAG_LZMA_EOS_MARKER) == 0)
@@ -1231,7 +1236,7 @@ extern int32_t mz_zip_entry_read_open(void *handle, int16_t raw, const char *pas
int16_t compression_method = 0;
int32_t err = MZ_OK;
#if !defined(HAVE_CRYPT) && !defined(HAVE_AES)
#if !defined(HAVE_PKCRYPT) && !defined(HAVE_AES)
if (password != NULL)
return MZ_PARAM_ERROR;
#endif
@@ -1257,6 +1262,10 @@ extern int32_t mz_zip_entry_read_open(void *handle, int16_t raw, const char *pas
if (raw)
compression_method = MZ_COMPRESS_METHOD_RAW;
#ifdef MZ_ZIP_COMPRESS_ONLY
if (compression_method != MZ_COMPRESS_METHOD_RAW)
err = MZ_SUPPORT_ERROR;
#endif
if (err == MZ_OK)
err = mz_zip_entry_open_int(handle, compression_method, 0, password);
@@ -1271,7 +1280,7 @@ extern int32_t mz_zip_entry_write_open(void *handle, const mz_zip_file *file_inf
int16_t compression_method = 0;
#if !defined(HAVE_CRYPT) && !defined(HAVE_AES)
#if !defined(HAVE_PKCRYPT) && !defined(HAVE_AES)
if (password != NULL)
return MZ_PARAM_ERROR;
#endif
@@ -1325,6 +1334,10 @@ extern int32_t mz_zip_entry_write_open(void *handle, const mz_zip_file *file_inf
if ((compress_level == 0) || (mz_zip_attrib_is_dir(zip->file_info.external_fa, zip->file_info.version_madeby) == MZ_OK))
compression_method = MZ_COMPRESS_METHOD_RAW;
#ifdef MZ_ZIP_DECOMPRESS_ONLY
if (compression_method != MZ_COMPRESS_METHOD_RAW)
err = MZ_SUPPORT_ERROR;
#endif
if (err == MZ_OK)
err = mz_zip_entry_write_header(zip->stream, 1, &zip->file_info);
if (err == MZ_OK)
@@ -1345,17 +1358,18 @@ extern int32_t mz_zip_entry_read(void *handle, void *buf, uint32_t len)
if (len == 0 || zip->file_info.uncompressed_size == 0)
return 0;
read = mz_stream_read(zip->crc32_stream, buf, len);
if (read > 0)
zip->entry_read += read;
return read;
}
extern int32_t mz_zip_entry_write(void *handle, const void *buf, uint32_t len)
{
mz_zip *zip = (mz_zip *)handle;
int32_t written = 0;
if (zip == NULL || zip->entry_opened == 0)
return MZ_PARAM_ERROR;
return mz_stream_write(zip->crc32_stream, buf, len);
written = mz_stream_write(zip->crc32_stream, buf, len);
return written;
}
extern int32_t mz_zip_entry_get_info(void *handle, mz_zip_file **file_info)
@@ -1380,6 +1394,7 @@ extern int32_t mz_zip_entry_close_raw(void *handle, uint64_t uncompressed_size,
{
mz_zip *zip = (mz_zip *)handle;
uint64_t compressed_size = 0;
int64_t total_in = 0;
int32_t err = MZ_OK;
if (zip == NULL || zip->entry_opened == 0)
@@ -1396,7 +1411,9 @@ extern int32_t mz_zip_entry_close_raw(void *handle, uint64_t uncompressed_size,
if (zip->file_info.aes_version <= 0x0001)
#endif
{
if ((zip->entry_read > 0) && (zip->compression_method != MZ_COMPRESS_METHOD_RAW))
mz_stream_crc32_get_prop_int64(zip->crc32_stream, MZ_STREAM_PROP_TOTAL_IN, &total_in);
// If entire entry was not read this will fail
if ((total_in > 0) && (zip->compression_method != MZ_COMPRESS_METHOD_RAW))
{
if (crc32 != zip->file_info.crc)
err = MZ_CRC_ERROR;
+6 -6
View File
@@ -1,5 +1,5 @@
/* mz_zip.h -- Zip manipulation
Version 2.3.0, May 3rd, 2018
Version 2.3.5, July 9, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
@@ -14,8 +14,8 @@
See the accompanying LICENSE file for the full text of the license.
*/
#ifndef _MZ_ZIP_H
#define _MZ_ZIP_H
#ifndef MZ_ZIP_H
#define MZ_ZIP_H
#include <stdint.h>
#include <time.h>
@@ -49,9 +49,9 @@ typedef struct mz_zip_file_s
uint32_t external_fa; // external file attributes
uint16_t zip64; // zip64 extension mode
char *filename; // filename string
uint8_t *extrafield; // extrafield data
char *comment; // comment string
const char *filename; // filename string
const uint8_t *extrafield; // extrafield data
const char *comment; // comment string
#ifdef HAVE_AES
uint16_t aes_version; // winzip aes extension if not 0
View File
+70 -5
View File
@@ -9,14 +9,72 @@
#include "mz_os.h"
#include "mz_strm.h"
#include "mz_strm_mem.h"
#ifdef HAVE_BZIP
#include "mz_strm_bzip.h"
#include "mz_strm_crypt.h"
#endif
#ifdef HAVE_PKCRYPT
#include "mz_strm_pkcrypt.h"
#endif
#ifdef HAVE_AES
#include "mz_strm_aes.h"
#endif
#ifdef HAVE_ZLIB
#include "mz_strm_zlib.h"
#endif
#include "mz_zip.h"
/***************************************************************************/
void test_path_resolve(void)
{
char output[256];
int32_t ok = 0;
memset(output, 'z', sizeof(output));
mz_path_resolve("c:\\test\\.", output, sizeof(output));
ok = (strcmp(output, "c:\\test\\") == 0);
memset(output, 'z', sizeof(output));
mz_path_resolve("c:\\test\\.\\", output, sizeof(output));
ok = (strcmp(output, "c:\\test\\") == 0);
memset(output, 'z', sizeof(output));
mz_path_resolve("c:\\test\\..", output, sizeof(output));
ok = (strcmp(output, "c:\\") == 0);
memset(output, 'z', sizeof(output));
mz_path_resolve("c:\\test\\..\\", output, sizeof(output));
ok = (strcmp(output, "c:\\") == 0);
memset(output, 'z', sizeof(output));
mz_path_resolve("c:\\test\\.\\..", output, sizeof(output));
ok = (strcmp(output, "c:\\") == 0);
memset(output, 'z', sizeof(output));
mz_path_resolve("c:\\test\\.\\\\..", output, sizeof(output));
ok = (strcmp(output, "c:\\") == 0);
memset(output, 'z', sizeof(output));
mz_path_resolve(".", output, sizeof(output));
ok = (strcmp(output, ".") == 0);
memset(output, 'z', sizeof(output));
mz_path_resolve(".\\", output, sizeof(output));
ok = (strcmp(output, "") == 0);
memset(output, 'z', sizeof(output));
mz_path_resolve("..", output, sizeof(output));
ok = (strcmp(output, "") == 0);
memset(output, 'z', sizeof(output));
mz_path_resolve("..\\", output, sizeof(output));
ok = (strcmp(output, "") == 0);
memset(output, 'z', sizeof(output));
mz_path_resolve("c:\\test\\123\\.\\abc.txt", output, sizeof(output));
ok = (strcmp(output, "c:\\test\\123\\abc.txt") == 0);
memset(output, 'z', sizeof(output));
mz_path_resolve("c:\\test\\123\\..\\abc.txt", output, sizeof(output));
ok = (strcmp(output, "c:\\test\\abc.txt") == 0);
memset(output, 'z', sizeof(output));
mz_path_resolve("c:\\test\\123\\..\\..\\abc.txt", output, sizeof(output));
ok = (strcmp(output, "c:\\abc.txt") == 0);
memset(output, 'z', sizeof(output));
mz_path_resolve("c:\\test\\123\\..\\..\\..\\abc.txt", output, sizeof(output));
ok = (strcmp(output, "abc.txt") == 0);
memset(output, 'z', sizeof(output));
}
void test_encrypt(char *method, mz_stream_create_cb crypt_create, char *password)
{
char buf[UINT16_MAX];
@@ -206,25 +264,30 @@ void test_compress(char *method, mz_stream_create_cb create_compress)
/***************************************************************************/
#ifdef HAVE_AES
void test_aes()
{
test_encrypt("aes", mz_stream_aes_create, "hello");
}
#endif
#ifdef HAVE_PKCRYPT
void test_crypt()
{
test_encrypt("crypt", mz_stream_crypt_create, "hello");
test_encrypt("pkcrypt", mz_stream_pkcrypt_create, "hello");
}
#endif
#ifdef HAVE_ZLIB
void test_zlib()
{
test_compress("zlib", mz_stream_zlib_create);
}
#endif
#ifdef HAVE_BZIP
void test_bzip()
{
test_compress("bzip", mz_stream_bzip_create);
}
#endif
/***************************************************************************/
@@ -262,7 +325,9 @@ void test_zip_mem()
file_info.compression_method = MZ_COMPRESS_METHOD_DEFLATE;
file_info.filename = text_name;
file_info.uncompressed_size = text_size;
#ifdef HAVE_AES
file_info.aes_version = MZ_AES_VERSION;
#endif
err = mz_zip_entry_write_open(zip_handle, &file_info, MZ_COMPRESS_LEVEL_DEFAULT, password);
if (err == MZ_OK)