Compare commits

...
429 Commits
Author SHA1 Message Date
Nathan Moinvaziri 2e2f995eda Version 2.8.3. 2019-01-26 16:49:48 -08:00
Nathan Moinvaziri e54bf2821b Change DEF_ROOT to MZ_ROOT. 2019-01-26 16:45:53 -08:00
Nathan Moinvaziri 840464bc01 Better check for existence of cmakein file. 2019-01-26 09:55:45 -08:00
Nathan Moinvaziri e282739da2 Fixed unit tests.
Added support for ZLIB_COMPAT to be configurable.
2019-01-26 00:05:36 -08:00
Nathan Moinvaziri 0512fd4771 Only configure minizip.pc if it exists in source tree. 2019-01-25 20:28:37 -08:00
Nathan Moinvaziri 40ab797fed Rename project to minizip and test executable to minizip_cmd. 2019-01-25 20:09:00 -08:00
Nathan Moinvaziri 38837cbec7 Don't print warning if Iconv not found. 2019-01-25 19:48:14 -08:00
Nathan Moinvaziri d0605392e6 Don't export _MZ compat functions. 2019-01-24 17:45:13 -08:00
Nathan Moinvaziri aec922930f Remove old minizip example macros. 2019-01-24 17:42:26 -08:00
Nathan Moinvaziri ce5be0f669 Added MZ_EXPORT definition.
Fixed ZEXPORT in wrong place in definitions.
2019-01-24 17:34:24 -08:00
Nathan Moinvaziri 1fc153481a Remove unused. 2019-01-23 10:56:12 -08:00
Nathan Moinvaziri a6ecab5fdc Fix/silence code analysis warnings. 2019-01-22 13:14:01 -08:00
Nathan Moinvaziri 554d3ab702 Fixed uninitialized variable warning. 2019-01-22 12:14:04 -08:00
Nathan Moinvaziri 226d905220 Fixed code analysis warnings. 2019-01-22 11:50:18 -08:00
Nathan Moinvaziri 91b2502219 Fixed code analyze warnings if key_blob is NULL from malloc. 2019-01-22 11:24:17 -08:00
Nathan Moinvaziri 288b484caa Fixed accidental code check-in of ZLIB_COMPAT def. 2019-01-22 11:23:02 -08:00
Nathan Moinvaziri 009a37a512 Fixed formatting. 2019-01-22 11:07:23 -08:00
Nathan Moinvaziri 84ec941f15 ZLIB_COMPAT is not defined for libminizip project, so just always use zng_ when zlib-ng is detected. 2019-01-22 10:23:03 -08:00
Nathan Moinvaziri cdedc087e7 Added prefix to CMakeList.txt options. #357 2019-01-21 15:18:49 -08:00
Nathan Moinvaziri e51dd48a91 Fixed reference to zlib stream. 2019-01-21 15:09:10 -08:00
Nathan Moinvaziri 208a051c36 Added support for compiling without ZLIB_COMPAT. zlib-ng/zlib-ng#156 2019-01-21 14:39:19 -08:00
Nathan Moinvaziri e4968653f4 Pass in arguments to external project. 2019-01-17 15:47:55 -08:00
Nathan Moinvaziri e71e3e3445 Remove ZLIB_COMPAT from external project. 2019-01-17 10:33:56 -08:00
Nathan Moinvaziri e7ede97c07 Fixed files are mistakenly listed as encrypted. #359 2019-01-16 11:15:00 -08:00
Nathan Moinvaziri 2ca7f3950c Version 2.8.2. 2019-01-08 16:07:10 -08:00
Nathan Moinvaziri b150432588 Fixed filename not being removed from path in certain instances. 2019-01-07 13:09:16 -08:00
Nathan Moinvaziri c78c782f63 Added ability to seek backward further for some applications. 2018-12-01 20:42:36 -08:00
Nathan Moinvaziri 1ff40343b5 Version 2.8.1. 2018-12-01 09:59:19 -08:00
Nathan Moinvaziri c1fd2edbd2 Fixed formatting. 2018-12-01 09:15:22 -08:00
Nathan Moinvaziri 2df1ecbce4 Fixed some code analysis warnings. 2018-12-01 08:59:21 -08:00
Nathan Moinvaziri a893e6f678 Merge branch 'master' into dev 2018-12-01 07:57:19 -08:00
Nathan Moinvaziri 8ff664da01 Merge branch 'dev' of https://github.com/nmoinvaz/minizip into dev 2018-12-01 07:56:40 -08:00
Nathan Moinvaziri d931843012 Fixed uncompressed size not being written correctly when store only. #352 2018-12-01 07:56:28 -08:00
Nathan Moinvaziri b99b3bf3fc Merge pull request #350 from UncleAlbie/absolute-install-paths
Use absolute paths for install directories.
2018-11-29 07:28:56 -08:00
Patrik Novotný f2b2dad2c9 Use absolute paths for install directories. 2018-11-29 15:10:38 +01:00
Nathan Moinvaziri 9df028d532 Merge pull request #349 from xcorail/master
Add LGTM code quality badges
2018-11-28 10:44:26 -08:00
Nathan Moinvaziri d058fdb3a1 Remove alerts badge 2018-11-28 10:43:55 -08:00
Nathan Moinvaziri 6675c44613 Set default as aes only if it is available. 2018-11-27 11:26:28 -08:00
Nathan Moinvaziri 4ff891a638 Only defined fixed-width types if not already defined. 2018-11-27 10:31:14 -08:00
Nathan Moinvaziri f9e344888f Move extrafield parameters to end of structure.
Don't use HAVE_WZAES, because another app or library might be using mz.h on which doesn't have HAVE_WZAES defined.
Return MZ_SUPPORT_ERROR if aes_version set, but HAVE_WZAES not defined.
Fixed some LGTM alerts. #349
2018-11-27 09:45:09 -08:00
Nathan Moinvaziri 9e9d0299f1 Fixed returning error in mz_crypt_hmac_copy. 2018-11-27 09:38:32 -08:00
Xavier RENE-CORAIL 5a16351708 Add LGTM.com code quality badges 2018-11-26 14:24:54 +01:00
Nathan Moinvaziri 5cb88b8f09 Version 2.8.0. 2018-11-24 17:59:11 -08:00
Nathan Moinvaziri 536a250734 Fixed unit tests for no encryption. 2018-11-24 17:46:47 -08:00
Nathan Moinvaziri e8c94b0570 Added testing against archives in fuzz seed corpus dir.
Reduced fuzz test sizes.
2018-11-24 17:32:30 -08:00
Nathan Moinvaziri 9bd8e85399 Merge branch 'dev' of https://github.com/nmoinvaz/minizip into dev 2018-11-24 17:14:10 -08:00
Nathan Moinvaziri 41eaa4c0ab Rename fuzzer test file. 2018-11-24 17:14:04 -08:00
Nathan Moinvaziri 025212f6dc Fixed directories returning size of 4096 on linux. 2018-11-24 17:08:17 -08:00
Nathan Moinvaziri 6295dcf005 Merge branch 'dev' of https://github.com/nmoinvaz/minizip into dev 2018-11-24 16:51:38 -08:00
Nathan Moinvaziri 62f435adbb Fixes aes encryption with windows crypto library. 2018-11-24 16:51:30 -08:00
Nathan Moinvaziri fa0c9c8edc Set default compression method based on what is available. 2018-11-23 15:20:21 -08:00
Nathan Moinvaziri 7f73943486 Seek to end of stream when appending. 2018-11-23 15:07:34 -08:00
Nathan Moinvaziri 2ce1fe2732 Fixed appending for zip spanning.
Added append mode cmake unit tests.
2018-11-23 11:53:11 -08:00
Nathan Moinvaziri 6b64b924af Fixed issues copying variable length data internally when large extrafield or comment. 2018-11-23 11:41:35 -08:00
Nathan Moinvaziri 2eb2907823 Fixed check for decompression support when using raw. 2018-11-23 10:32:21 -08:00
Nathan Moinvaziri b4d88d426b Fixed compiler warnings and fixed erase unit test. 2018-11-23 10:30:04 -08:00
Nathan Moinvaziri ba1db16a28 Fixed bug not appending slash on to the end of filename in zip for directories.
Separated out read/write close into their own functions.
Fixed integer overflow for disk offset.
Added unit tests for erase from zip file.
2018-11-23 10:07:35 -08:00
Nathan Moinvaziri 6ac5fe69d0 Fixed definitions of limits and some casting issues. 2018-11-23 08:37:19 -08:00
Nathan Moinvaziri 4c642fc296 Added additional check against negative offset during seek set. 2018-11-22 22:45:51 -08:00
Nathan Moinvaziri d8f00c57cb Merge branch 'dev' of https://github.com/nmoinvaz/minizip into dev 2018-11-22 10:17:46 -08:00
Nathan Moinvaziri 54b9939845 Added check for negative offsets. 2018-11-22 10:17:37 -08:00
Nathan Moinvaziri 13a51b9389 Added a few more comments to example. #346 2018-11-22 09:15:23 -08:00
Nathan Moinvaziri 1b6fcaca41 Code cleanup. 2018-11-22 09:02:10 -08:00
Nathan Moinvaziri cdac37e1fe Move tiny zip to unzip fuzzer seed corpus. 2018-11-21 23:20:17 -08:00
Nathan Moinvaziri 9897259b04 Added additional files to unit tests.
Fixed compiler warnings.
2018-11-21 23:14:22 -08:00
Nathan Moinvaziri e9ae46ba27 Fixed check for zero read to only break when no bytes decompressed. 2018-11-21 19:31:08 -08:00
Nathan Moinvaziri 3e2d07451c Allow writing of more bytes than buffer in pkcrypt and wzaes. 2018-11-21 19:25:20 -08:00
Nathan Moinvaziri d31c5de5b0 Merge branch 'dev' of https://github.com/nmoinvaz/minizip into dev 2018-11-21 19:14:14 -08:00
Nathan Moinvaziri ad82950a53 Add check to see that bytes were written. 2018-11-21 19:14:07 -08:00
Nathan Moinvaziri 39365dd10d Fixed wrong error code set from zlib.h instead of mz.h. 2018-11-21 15:18:47 -08:00
Nathan Moinvaziri 76fb8903e1 Rename HAVE_AES to HAVE_WZAES to make it more clear that only the feature is enabled/disabled.
Added additional cmake unit tests.
2018-11-21 15:14:09 -08:00
Nathan Moinvaziri e849627093 Move stdio to the source files that use them.
Fixed limits redefinition for windows.
Clean up headers.
2018-11-21 15:05:58 -08:00
Nathan Moinvaziri 6f3a0a6ad0 Use all win32 api. 2018-11-21 14:53:36 -08:00
Nathan Moinvaziri b066fd9677 Set COMPRESSION_STREAM_FINALIZE properly. 2018-11-20 20:21:42 -08:00
Nathan Moinvaziri b578fa5ba5 Added signed fuzz test. 2018-11-20 19:42:23 -08:00
Nathan Moinvaziri bacef206da Convert to forward slash when saving files for unix which doesn't like backslashes. #347 2018-11-20 19:42:13 -08:00
Nathan Moinvaziri d96f23f925 Fixed better detection if BRG library should be used.
Turn on debug symbols when not in release config.
2018-11-20 19:13:41 -08:00
Nathan Moinvaziri a5bc018ecc Fixed signature verifications with openssl. 2018-11-20 19:13:10 -08:00
Nathan Moinvaziri 83aa562e32 Fixed possibly missing u_char when using libbsd. 2018-11-20 17:20:54 -08:00
Nathan Moinvaziri 8a35deab70 Define u_char if not available. 2018-11-20 17:12:09 -08:00
Nathan Moinvaziri 783d258c68 Blank out password when printing args. 2018-11-20 16:57:25 -08:00
Nathan Moinvaziri b16ab566ef Change comments to C89 style.
Added _POSIX_C_SOURCE definition for necessary version of POSIX.
2018-11-20 16:56:21 -08:00
Nathan Moinvaziri ddeeae3388 Fixed compiler warnings about ss[] possibly undefined. 2018-11-20 16:52:28 -08:00
Nathan Moinvaziri e231cfd9d8 Fixed compiler warnings. 2018-11-20 13:19:21 -08:00
Nathan Moinvaziri f96e8636a4 Fixed reading pkcrypt stream when uncompressed size is 0.
Fixed pkcrypt buffer was too small.
2018-11-20 10:48:25 -08:00
Nathan Moinvaziri f4207b7a23 Fixed analyze warnings about unreferenced variables. 2018-11-20 10:06:16 -08:00
Nathan Moinvaziri 02c9edb3af Fixed bug not encrypting if size is larger than pkcrypt buffer size. 2018-11-20 10:05:51 -08:00
Nathan Moinvaziri d4f27376aa Print command line args 2018-11-20 10:05:51 -08:00
Nathan Moinvaziri 9475829f15 Fixed analyze warnings about unused params. 2018-11-20 09:38:00 -08:00
Nathan Moinvaziri 19cdb10201 Fixed crash when copying hmac ctx in openssl. 2018-11-20 09:25:56 -08:00
Nathan Moinvaziri d46559398f Allow decompression even though zero bytes read. 2018-11-20 03:18:22 -08:00
Nathan Moinvaziri 90d31c791d Fixed libcompression compilation. 2018-11-20 03:18:04 -08:00
Nathan Moinvaziri 0a60d2ca2a Cleanup comments and includes. 2018-11-19 22:12:23 -08:00
Nathan Moinvaziri 0c59898b2d Skip signing test when not using openssl on apple. 2018-11-19 21:56:28 -08:00
Nathan Moinvaziri 53c3d0ae2f Fixed cmake warnings. 2018-11-19 21:53:23 -08:00
Nathan Moinvaziri 1ee609b759 Make inttypes.h optional. #345 2018-11-19 21:34:35 -08:00
Nathan Moinvaziri 0ecfd22443 Fixed missing time.h include. 2018-11-19 20:52:35 -08:00
Nathan Moinvaziri 7de94cf921 Fixed unused variable when HAVE_AES is not defined.
Fixed compiler errors when not using C99 with liblzma.
2018-11-19 20:51:06 -08:00
Nathan Moinvaziri c1e07aaeb0 Fixed missing time include. 2018-11-19 20:38:20 -08:00
Nathan Moinvaziri 329ac651d8 Fixed for loop initial declarations are only C99. 2018-11-19 20:36:02 -08:00
Nathan Moinvaziri 4d0897361b Fixed zipcd not working correctly.
Turn off zipcd for raw in cmake tests.
2018-11-19 20:24:26 -08:00
Nathan Moinvaziri fd2fb24b4e Fixed warnings when compression is not supported. 2018-11-19 20:03:20 -08:00
Nathan Moinvaziri 82fa1b39c0 Better fseeko detection. 2018-11-19 19:52:47 -08:00
Nathan Moinvaziri 2d37e12f36 Fixed missing time.h include.
Simplified fseek detection.
2018-11-19 19:48:37 -08:00
Nathan Moinvaziri fe9ec41c49 Fixed disk span tests args. 2018-11-19 19:38:11 -08:00
Nathan Moinvaziri a8b23065cd Fixed compiler error with posix. 2018-11-19 19:34:45 -08:00
Nathan Moinvaziri 08b01ca7b7 Fixed missing include. 2018-11-19 19:31:44 -08:00
Nathan Moinvaziri bc64e9f899 Fixed compiler errors with missing include. 2018-11-19 19:10:35 -08:00
Nathan Moinvaziri abb508eabe Fixed cmake unit tests using wrong args. 2018-11-19 19:10:14 -08:00
Nathan Moinvaziri 1139b468c1 Auto-detect compiler support for liblzma. 2018-11-19 18:46:34 -08:00
Nathan Moinvaziri c4916c971a Better check for large file support taken from zlib cmake.
Remove creating dummy inttypes.h. #345
2018-11-19 18:23:52 -08:00
Nathan Moinvaziri d8b232a6e9 Comment out unused #defines. 2018-11-19 18:06:08 -08:00
Nathan Moinvaziri fb8a1cc220 Remove forward includes. 2018-11-19 18:01:53 -08:00
Nathan Moinvaziri a2a3858e05 Only apply optimizations if in Release build type. 2018-11-19 17:54:10 -08:00
Nathan Moinvaziri ea2bd795a3 Check return value of reading extrafield info. 2018-11-19 17:44:44 -08:00
Nathan Moinvaziri 51b050e0b8 Merge branch 'dev' of https://github.com/nmoinvaz/minizip into dev 2018-11-19 17:33:29 -08:00
Nathan Moinvaziri 09e41030a5 Fixed harmless integer overflow. 2018-11-19 17:33:20 -08:00
Nathan Moinvaziri 7817f5c8d5 Update .travis.yml 2018-11-14 08:35:25 -08:00
Nathan Moinvaziri a4549e0689 Fixed compiler warning. 2018-11-13 18:04:23 -08:00
Nathan Moinvaziri 4d29ba4782 Fixed compiler warnings with MZ_ZIP_NO_COMPRESSION. 2018-11-13 18:03:09 -08:00
Nathan Moinvaziri 4d4c9db5b0 Create dummy inttypes.h if it doesn't exist in compatibility mode. #344 2018-11-13 17:55:33 -08:00
Nathan Moinvaziri a93a2ad678 Fixed compiler warnings. 2018-11-13 17:51:42 -08:00
Nathan Moinvaziri e0b2477114 Fixed compiler warning. 2018-11-13 17:48:53 -08:00
Nathan Moinvaziri 264dc189b3 Revert "Remove inttypes.h." 2018-11-13 17:42:13 -08:00
Nathan Moinvaziri b2b082c67d Version 2.7.5. 2018-11-13 15:22:15 -08:00
Nathan Moinvaziri 12d03f4e61 Update README.md 2018-11-13 15:20:26 -08:00
Nathan Moinvaziri dd103f4fc1 Added ability to get comment from reader.
Added better fuzz test with full comment size.
Fixed setting size of comment that is UINT32_MAX length.
Added ability to recover the cd from the local file headers. #341
2018-11-13 14:50:32 -08:00
Nathan Moinvaziri d82579a400 Fixed cmake warnings in 3.12.4. 2018-11-13 09:40:11 -08:00
Nathan Moinvaziri 98cbf2296a Enable c++ language when linking with libFuzzer. 2018-11-13 09:32:25 -08:00
Nathan Moinvaziri 9320beab03 Link fuzzers with CXX. 2018-11-13 08:54:32 -08:00
Nathan Moinvaziri f47e75303d Merge branch 'dev' of https://github.com/nmoinvaz/minizip into dev 2018-11-13 07:57:25 -08:00
Nathan Moinvaziri 11a44ecf33 Better clang detection. google/oss-fuzz#1940. 2018-11-13 07:57:15 -08:00
Nathan Moinvaziri 5498bb6c4f Combine columns. 2018-11-12 22:47:11 -08:00
Nathan Moinvaziri 278770c5f3 Remove need for python in windows. 2018-11-12 22:45:17 -08:00
Nathan Moinvaziri e71d8776be Remove python test script since cmake tests are better.
Added empty unit test.
2018-11-12 22:44:25 -08:00
Nathan Moinvaziri 3c2b4da788 Use better compression for deflate tests. 2018-11-12 17:40:05 -08:00
Nathan Moinvaziri c6443ff336 Removed TESTTOOL variable. 2018-11-12 17:14:18 -08:00
Nathan Moinvaziri a7af82f8cf More ctest fixes. 2018-11-12 17:10:55 -08:00
Nathan Moinvaziri 146fa3a663 Conditional construction of unit tests. 2018-11-12 16:58:29 -08:00
Nathan Moinvaziri 96b401b2c5 Fixed working directory in unit tests. 2018-11-12 16:50:36 -08:00
Nathan Moinvaziri 771856888c Fixed missing configuration. 2018-11-12 16:40:46 -08:00
Nathan Moinvaziri 5502aeaeb2 Update ci to run ctest. 2018-11-12 16:26:21 -08:00
Nathan Moinvaziri 2805bd4688 Move unit tests to cmake. 2018-11-12 16:15:53 -08:00
Nathan Moinvaziri 4d6530cd88 Remove setting fuzzer flag. google/oss-fuzz#1945. 2018-11-12 11:15:12 -08:00
Nathan Moinvaziri 1f9efeae4f Link against fuzzing engine. 2018-11-12 10:12:22 -08:00
Nathan Moinvaziri 0687386d8b Added ability to set comment in zip writer. 2018-11-12 08:14:58 -08:00
Nathan Moinvaziri d5911f4524 Fixed formatting. 2018-11-12 08:14:19 -08:00
Nathan Moinvaziri 23b9d094d7 Add fuzzer flag. 2018-11-12 07:59:32 -08:00
Nathan Moinvaziri 258d42c566 Fixed compiler warnings. 2018-11-12 07:58:10 -08:00
Nathan Moinvaziri 8ce54b4a8d Fuzz minizip google/oss-fuzz#1940. 2018-11-12 07:39:34 -08:00
Nathan Moinvaziri f07057a85e Merge branch 'dev' of https://github.com/nmoinvaz/minizip into dev 2018-11-11 22:58:32 -08:00
Nathan Moinvaziri e440f1e9d5 Fixed possible infinite loop found by fuzzer. 2018-11-11 22:57:33 -08:00
Nathan Moinvaziri 6fab9297d2 Fixed unused variable warning. 2018-11-10 14:09:47 -08:00
Nathan Moinvaziri 1d6d183af6 Fixed more compiler warnings. 2018-11-10 09:03:55 -08:00
Nathan Moinvaziri f274ae0649 Don't add link dir for openssl. 2018-11-08 19:31:30 -08:00
Nathan Moinvaziri 95fec96e05 Fixed compiler warning. 2018-11-08 19:29:42 -08:00
Nathan Moinvaziri 3c7568fa67 Silence compiler warnings. 2018-11-08 19:14:35 -08:00
Nathan Moinvaziri 05a45d61ab Fixed bug writting cd twice. 2018-11-08 18:19:55 -08:00
Nathan Moinvaziri d4d49df0b1 Fixed compiler warnings. 2018-11-08 18:12:51 -08:00
Nathan Moinvaziri f9b4d20535 Merge pull request #343 from akmistry/akmistry-patch-2
Cast day/month/year elements to uint32_t
2018-11-08 18:05:24 -08:00
Anand K Mistry cf622bb55c Guard against seek overflow in mz_zip_entry_read_open
Detected by libfuzzer+ubsan
2018-11-09 11:32:42 +11:00
Anand K Mistry 57b65f8a15 Cast day/month/year elements to uint32_t
If the sum of day/month/year elements exceeds 32767, ubsan will tag it as undefined behaviour since the tm struct elements are int which is only defined to be at least 16-bits and signed. Casting these elements to uint32_t guarantees the calculations will be defined.
2018-11-09 10:52:19 +11:00
Nathan Moinvaziri effc1428ff Remove inttypes.h. 2018-11-08 15:24:17 -08:00
Nathan Moinvaziri 286a8174f6 Allow further seek back incase of really long comment. 2018-11-08 14:56:14 -08:00
Nathan Moinvaziri b08bd9a3fa Fixed bugs in seeking forward and back and add unit tests. 2018-11-08 14:52:52 -08:00
Nathan Moinvaziri 07b0ecadc6 Fixed don't create dir when directory not specified. 2018-11-07 21:07:33 -08:00
Nathan Moinvaziri 0dfcdf91ba Added function to rebuild central directory if corrupted or not found.
Fixed possible memory leak with pkcrypt when failure to open entry. (thx akmistry)
Move the reading and writing of data descriptors to their own functions.
Added debug printing for zip class.
Use mz seek defines.
2018-11-07 20:33:47 -08:00
Nathan Moinvaziri 23342c44d4 Added debug printing for os stream. 2018-11-07 20:24:59 -08:00
Nathan Moinvaziri 65dbda889d Added functions for finding data forwards and backwards in a stream. 2018-11-07 20:23:42 -08:00
Nathan Moinvaziri e39032376e Added debug printing for split stream.
Fixed split stream not re-opening for current disk if closed.
2018-11-07 20:11:29 -08:00
Nathan Moinvaziri 30a5288bf4 Fixed debug printing for buffered stream. 2018-11-07 20:09:36 -08:00
Nathan Moinvaziri 4cceb2f6c8 Create directory when writing zip if it doesn't already exist.
Use mz.h seek defines.
2018-11-07 20:08:26 -08:00
Nathan Moinvaziri 2ff079d5d8 Clean up listing columns. 2018-11-07 20:07:13 -08:00
Nathan Moinvaziri 0d85193f89 Don't update external zlib each time we cmake. 2018-11-07 19:58:14 -08:00
Nathan Moinvaziri c000e8f1f8 Compensate for incorrect central directory size. #342 2018-11-07 07:30:59 -08:00
Nathan Moinvaziri 761d28e30d Added additional checks when reading in extra fields. 2018-11-07 06:47:15 -08:00
Nathan Moinvaziri 6000844769 Move retrieval of pointers to bottom and add some additional checks. 2018-11-07 06:29:01 -08:00
Nathan Moinvaziri 51834cd9f9 Merge branch 'dev' of https://github.com/nmoinvaz/minizip into dev 2018-11-06 21:12:23 -08:00
Nathan Moinvaziri c47090678d Version 2.7.4. 2018-11-06 21:12:16 -08:00
Nathan Moinvaziri 79138fe17c Fixed version number. 2018-11-06 10:35:26 -08:00
Nathan Moinvaziri 9e8d7a8c76 Remove required column. 2018-11-06 10:34:41 -08:00
Nathan Moinvaziri 79244df024 Don't fail if incorrect comment length read. #339 2018-11-06 10:20:28 -08:00
Nathan Moinvaziri 87a668fd64 Move offset check after we have corrected for incorrect offsets. #339 2018-11-06 10:09:58 -08:00
Nathan Moinvaziri a18f49f6d3 Merge branch 'dev' of https://github.com/nmoinvaz/minizip into dev 2018-11-06 09:39:00 -08:00
Nathan Moinvaziri aacf87304e Attempt to correct for invalid central dir offsets. #340 2018-11-06 09:38:46 -08:00
Nathan Moinvaziri 130f0591ba Merge pull request #338 from akmistry/patch-1
Error if seeking to before the start of a mem stream
2018-11-06 08:05:07 -08:00
Anand K Mistry c8501a4216 Error if seeking to before the start of a mem stream 2018-11-06 14:39:17 +11:00
Nathan Moinvaziri a90e29704a Merge branch 'dev' of https://github.com/nmoinvaz/minizip into dev 2018-11-05 15:48:47 -08:00
Nathan Moinvaziri 69e3549034 Clean up copying of variable length data. 2018-11-05 15:48:41 -08:00
Nathan Moinvaziri a4fc2c5e85 Fixed typo 2018-11-05 15:16:23 -08:00
Nathan Moinvaziri 8edafb620a Merge branch 'dev' of https://github.com/nmoinvaz/minizip into dev 2018-11-05 12:45:35 -08:00
Nathan Moinvaziri b4923cd155 Removed unnecessary #includes. 2018-11-05 12:45:17 -08:00
Nathan Moinvaziri 71295ce78d Set mz_os source files as required 2018-11-05 12:40:39 -08:00
Nathan Moinvaziri 088b819ec0 Updated required files 2018-11-05 12:39:30 -08:00
Nathan Moinvaziri a8f23236e8 Update required files 2018-11-05 12:35:21 -08:00
Nathan Moinvaziri b11b6cfe63 Only require zipped central dir to be signed in archive and only sign zipped central dir.
Load signing certificate from disk once.
2018-11-05 12:32:19 -08:00
Nathan Moinvaziri feb86c551a Fixed extraction of malformed extrafield. #337 2018-11-05 11:40:21 -08:00
Nathan Moinvaziri 826113fd6c Version 2.7.3. 2018-11-04 12:52:58 -08:00
Nathan Moinvaziri 4d5a9a66c9 Update README.md 2018-11-04 12:51:18 -08:00
Nathan Moinvaziri 25634010c9 Update README.md 2018-11-04 12:45:57 -08:00
Nathan Moinvaziri 6cda2a7e76 Update README.md 2018-11-04 12:45:09 -08:00
Nathan Moinvaziri 7c2005dcf0 Update README.md 2018-11-04 12:42:51 -08:00
Nathan Moinvaziri cb5798a6b9 Go to next argument correctly. 2018-11-04 10:23:50 -08:00
Nathan Moinvaziri cdef32b909 Notify user when feature not supported. 2018-11-04 10:21:27 -08:00
Nathan Moinvaziri 8721aaf7ec Fixed compiler error. 2018-11-03 22:58:04 -07:00
Nathan Moinvaziri d15b704c85 Fixed compilation issue when no signing. 2018-11-03 22:55:32 -07:00
Nathan Moinvaziri 1b87a5a358 Removed old crc32 streams. 2018-11-03 19:47:20 -07:00
Nathan Moinvaziri e52a3b5732 Added missing source file. 2018-11-03 19:46:20 -07:00
Nathan Moinvaziri e63d231fa5 Moved crc hashing and pbkdf2 to crypt.
Provide backup crc32 function if zlib or lzma not used.
Fixed check for no signing in minizip.
2018-11-03 19:45:41 -07:00
Nathan Moinvaziri 24280b68e8 Merge USE_COMPAT sections. 2018-11-03 10:19:38 -07:00
Nathan Moinvaziri 888e8a7324 Version 2.7.2. 2018-11-02 17:47:53 -07:00
Nathan Moinvaziri c51f2515d6 Merge branch 'dev' of https://github.com/nmoinvaz/minizip into dev 2018-11-02 17:46:52 -07:00
Nathan Moinvaziri adfe53f969 Append extrafield to end so hash extrafield is always guaranteed to be first. 2018-11-02 17:46:47 -07:00
Nathan Moinvaziri f164b6dd37 Update README.md 2018-11-02 17:35:49 -07:00
Nathan Moinvaziri 02760f6257 Update README.md 2018-11-02 17:35:08 -07:00
Nathan Moinvaziri 8541317c10 Fixed more undeclared identifiers. 2018-11-02 17:27:57 -07:00
Nathan Moinvaziri 3223d8d378 Fixed undefined cert_path variable. 2018-11-02 16:43:44 -07:00
Nathan Moinvaziri bdf88e7425 Fixed old unused variables. 2018-11-02 16:29:20 -07:00
Nathan Moinvaziri 6cdd3f54da Merge branch 'dev' of https://github.com/nmoinvaz/minizip into dev 2018-11-02 16:22:50 -07:00
Nathan Moinvaziri 4337bcc752 Move reading of certificate into zip writer. 2018-11-02 16:22:39 -07:00
Nathan Moinvaziri 0d396799c1 Update README.md 2018-11-02 10:37:52 -07:00
Nathan Moinvaziri 0b14aeddde Merge branch 'dev' of https://github.com/nmoinvaz/minizip into dev 2018-11-02 10:35:36 -07:00
Nathan Moinvaziri 1b2b32c8b8 Create zip.h and unzip.h compatibility headers. 2018-11-02 10:35:27 -07:00
Nathan Moinvaziri 66e1631e4b Update README.md 2018-11-02 10:12:51 -07:00
Nathan Moinvaziri 7ff6b8aa9f Add support for compiling with zlib-ng. Dead2/zlib-ng#189 2018-11-02 09:40:34 -07:00
Nathan Moinvaziri b64f4f1228 Added ability to specify zlib tag and repository url. #336 2018-11-02 08:44:54 -07:00
Nathan Moinvaziri 5f381b232d Cleanup verification logic with openssl. 2018-11-02 08:38:55 -07:00
Nathan Moinvaziri f95e3683ee Added signature verification on osx. 2018-11-02 08:38:21 -07:00
Nathan Moinvaziri 0d9d0df40d Implemented signature creation on osx. 2018-11-01 19:03:40 -07:00
Nathan Moinvaziri 20e4d587ca Added common crypto for Apple. 2018-11-01 16:16:19 -07:00
Nathan Moinvaziri 3aaf5bc895 Added 1.1 branch info. 2018-11-01 15:14:21 -07:00
Nathan Moinvaziri 623a844814 Updated readme. 2018-11-01 15:05:44 -07:00
Nathan Moinvaziri c9add13ae6 Merge pull request #335 from vszakats/fixes
misc fixes
2018-11-01 13:18:40 -07:00
Viktor Szakats fcd85ef064 misc fixes
- fix a typo in a cast
- fix wrongly falling into an #if branch meant for MSVC
  when built with clang + mingw-w64
- remove a duplicate option from .travis.yml
- whitespace fixes
2018-11-01 19:31:05 +00:00
Nathan Moinvaziri 54dffe95e9 Version 2.7.1. 2018-11-01 10:40:24 -07:00
Nathan Moinvaziri 82d4fa0dd6 Better check for existence of MAX_MEM_LEVEL. 2018-11-01 10:36:12 -07:00
Nathan Moinvaziri eff49b88ce Remove unused set_algorithm function for aes.
Better setting of errors in crypt functions.
Fixed win32 aes key generated incorrectly.
Fixed openssl aes key bits not being set correctly.
Added option to for using BRG library on windows.
Changed low quality entropy error to warning.
2018-11-01 10:32:42 -07:00
Nathan Moinvaziri 04d821edef Fixed problems with compatibility header. 2018-11-01 09:09:55 -07:00
Nathan Moinvaziri 7eb65ad2ae Removed old function from header. 2018-10-31 18:28:19 -07:00
Nathan Moinvaziri f79ec42ad3 Fixed hmac implementation in brg library. #334
Moved brg setup in cmake.
2018-10-31 18:25:53 -07:00
Nathan Moinvaziri f6557afd71 Merge branch 'dev' of https://github.com/nmoinvaz/minizip into dev 2018-10-31 16:46:43 -07:00
Nathan Moinvaziri 10489ead98 Added write sharing to win32 stream. #332 2018-10-31 16:46:34 -07:00
Nathan Moinvaziri d021ab867b Don't link with iconv if already built-in. 2018-10-30 20:45:39 -07:00
Nathan Moinvaziri 90a8b12562 Fixed iconv references on mac. 2018-10-30 18:09:47 -07:00
Nathan Moinvaziri 6209991d6b Added finding and linking with of Iconv library.
Fixed printing version numbers for Zlib and Bzip2.
2018-10-30 18:07:27 -07:00
Nathan Moinvaziri 3caa105cef Fixed compiler error on windows with missing param. 2018-10-30 15:34:26 -07:00
Nathan Moinvaziri 0bff08751b Fixed compiler errors and warnings on linux. 2018-10-30 15:31:49 -07:00
Nathan Moinvaziri ea5e45dfe2 Initial support for other encodings. #330 2018-10-30 13:59:17 -07:00
Nathan Moinvaziri 1e21dc3a9b Added additional check for required signing. 2018-10-30 09:03:42 -07:00
Nathan Moinvaziri c73ef6e69b Fixed issue detecting crypt rand function on linux. Dead2/zlib-ng#189. 2018-10-30 08:54:30 -07:00
Nathan Moinvaziri 57942ad197 Merge pull request #331 from xwxbug/openssl111
fixed for openssl 1.1.0 to 1.1.1
2018-10-30 08:44:51 -07:00
Nathan Moinvaziri 974410131c Fixed formatting. 2018-10-30 08:44:21 -07:00
Nathan Moinvaziri 848837b78b Merge branch 'dev' into openssl111 2018-10-30 08:43:23 -07:00
Nathan Moinvaziri dc63e1ebcf Fixed formatting. 2018-10-30 08:41:32 -07:00
thegfw 8b5826b3bf fixed for openssl 1.1.0 to 1.1.1 2018-10-30 15:51:17 +08:00
Nathan Moinvaziri f3efcb6227 Possible fix for openssl 1.1 compatibility. #329 2018-10-29 23:13:51 -07:00
Nathan Moinvaziri 43c01101bf Update README.md 2018-10-28 17:01:57 -07:00
Nathan Moinvaziri d1e3877b4d Updated readme. 2018-10-28 16:51:04 -07:00
Nathan Moinvaziri a221da4ae0 Version 2.7.0. 2018-10-28 16:47:53 -07:00
Nathan Moinvaziri 703b3df3ef Added option to require a signature during extracting.
Added new sign error.
2018-10-28 16:41:20 -07:00
Nathan Moinvaziri 05e03cad8e Added additional error codes to aid in debugging. 2018-10-28 16:15:13 -07:00
Nathan Moinvaziri 71027dc8fb Better comparison of os stream is open. 2018-10-28 15:48:44 -07:00
Nathan Moinvaziri 4ae469a0a4 Fixed missing semi-colon. 2018-10-28 15:32:04 -07:00
Nathan Moinvaziri 80b56a8a5a Added self-signed certificate signing test. 2018-10-28 15:30:59 -07:00
Nathan Moinvaziri a2bb08c816 Fixed compiler warning when aes is not enabled. #326 2018-10-28 13:45:30 -07:00
Nathan Moinvaziri 619b2b3c8b Fixed compiler errors and warnings. 2018-10-28 12:36:15 -07:00
Nathan Moinvaziri fac97955d1 Simplify search for matching signature hash. 2018-10-28 11:37:37 -07:00
Nathan Moinvaziri ea66a05adc Updated readme. 2018-10-28 10:01:15 -07:00
Nathan Moinvaziri dd31249b5a Change sys.exit to exit. 2018-10-27 18:21:56 -07:00
Nathan Moinvaziri 37b4da07f1 Move zip cd logic into zip reader/writer.
Fixed compiler error due to #define.
2018-10-27 18:19:18 -07:00
Nathan Moinvaziri cb9c40c23d Fixed bugs in cms generation corrupting the message in openssl.
Don't generate sha256 hash or sign when entry is a directory.
Fixed not printing adding file when new entry is added.
2018-10-27 15:32:28 -07:00
Nathan Moinvaziri cce11ba090 Run tests for disk splitting.
Added tests for zipping cd.
Changed show progress option to verbose.
2018-10-27 08:31:01 -07:00
Nathan Moinvaziri 6e2f181c18 Try this fix for build dir and test script issues. 2018-10-26 23:44:46 -07:00
Nathan Moinvaziri e464430433 Fixed windows build dir. 2018-10-26 23:39:58 -07:00
Nathan Moinvaziri f5b7ed7bfe Make split disk tests to their own flag. 2018-10-26 23:21:38 -07:00
Nathan Moinvaziri 72a099dae1 Fixed paths on windows test script. 2018-10-26 23:15:25 -07:00
Nathan Moinvaziri 13d9e7666c Added command line switch to show progress if wanted.
Install python 2 on windows.
2018-10-26 23:01:24 -07:00
Nathan Moinvaziri fea1232fa6 Make random file tests conditional. 2018-10-26 20:40:03 -07:00
Nathan Moinvaziri 2a172f312d Fixed navigating to test.py. 2018-10-26 20:25:15 -07:00
Nathan Moinvaziri 3c000cf5fb Added python tests back in to travis ci. 2018-10-26 20:20:56 -07:00
Nathan Moinvaziri 915c513abe Fixed winzip aes encryption with zero byte files.
Allow greater buffer to be passed in for hashing functions.
Fixed crc32 values not being stored in zip.
When reading zip get best hash to test against in signature.
Fixed formatting.
2018-10-26 20:00:52 -07:00
Nathan Moinvaziri 2ed269e000 Fixed issues with openssl not being found on mac. 2018-10-26 01:47:36 -07:00
Nathan Moinvaziri 217cd65995 Fixed entry sign not using variables passed in.
Move more configurations to linux for better warnings in logs.
Fixed path to openssl in mac configuration.
Fixed compiler errors.
2018-10-26 01:29:34 -07:00
Nathan Moinvaziri bc21a7456b Fixed compiler error in openssl crypto.
Fixed compiler warnings.
2018-10-26 01:10:20 -07:00
Nathan Moinvaziri 94f8c19db4 Fixed problems finding openssl package on linux.
Fixed compiler warnings.
2018-10-26 01:07:37 -07:00
Nathan Moinvaziri 80d30e6bf8 Possibly fixed openssl travis configurations. 2018-10-26 00:49:33 -07:00
Nathan Moinvaziri 566dfe0dcd Added openssl configurations.
Fixed formatting.
2018-10-26 00:36:30 -07:00
Nathan Moinvaziri 9ad08e1ec6 Remove zlib windows install instructions. 2018-10-26 00:27:03 -07:00
Nathan Moinvaziri 1770d3554e Fixed no encryption configuration.
Added more possible configurations.
2018-10-26 00:18:50 -07:00
Nathan Moinvaziri 13ae3ab2bc Add back linux and mac matrixes. 2018-10-26 00:13:13 -07:00
Nathan Moinvaziri e300b7f3a2 Fixed issue not linking with external lib. 2018-10-26 00:06:08 -07:00
Nathan Moinvaziri f44a0e089c Possibly fix zlib header issue. 2018-10-26 00:01:50 -07:00
Nathan Moinvaziri 5b3039af0e Try to fix where zlib is installed to on Windows. 2018-10-25 23:41:54 -07:00
Nathan Moinvaziri d0ac17fd60 Updated script again for windows. 2018-10-25 23:34:54 -07:00
Nathan Moinvaziri 156a64730b Attempt to fix build. 2018-10-25 23:30:00 -07:00
Nathan Moinvaziri 8cc0800876 Added ability to compile zlib with external project.
Fixed problem compiling with no encryption on linux.
2018-10-25 23:25:01 -07:00
Nathan Moinvaziri 9d6d894153 Added additional configurations. 2018-10-25 22:39:55 -07:00
Nathan Moinvaziri 6cabcc0523 Don't make zlib required in cmake.
Change travis to use libcompression on mac.
2018-10-25 22:26:50 -07:00
Nathan Moinvaziri 6a415925fd Fixed typo. 2018-10-25 22:21:46 -07:00
Nathan Moinvaziri bee75d5f3e Fixed before_install so it doesn't run on windows. 2018-10-25 22:20:08 -07:00
Nathan Moinvaziri 7dce6de14f Added windows to travis ci. 2018-10-25 22:09:41 -07:00
Nathan Moinvaziri 6bde034147 Fixed compiler warnings. 2018-10-25 22:05:13 -07:00
Nathan Moinvaziri c714ef60a4 Fixed more compiler warnings. 2018-10-25 22:01:02 -07:00
Nathan Moinvaziri f97c9f48b2 Fixed compiler warnings.
Fixed brg not compiling into project.
2018-10-25 21:50:15 -07:00
Nathan Moinvaziri d2dc11ca57 Fixed compiler errors and warnings. 2018-10-25 21:36:35 -07:00
Nathan Moinvaziri d45af6d6e9 Fixed compiler errors and warnings. 2018-10-25 21:31:23 -07:00
Nathan Moinvaziri bf7776206e Moved includes to wrong file. 2018-10-25 21:28:26 -07:00
Nathan Moinvaziri 47a2cd7b99 Fixed includes in the wrong place. 2018-10-25 21:25:50 -07:00
Nathan Moinvaziri 9fb8290289 Fixed DIR not recognized error. 2018-10-25 21:21:58 -07:00
Nathan Moinvaziri e5a5617a7c Added USE_OPENSSL. 2018-10-25 20:06:12 -07:00
Nathan Moinvaziri 5352f868bb Updated contents. 2018-10-25 20:00:18 -07:00
Nathan Moinvaziri d9969b545a Added crypt layer for brg compatibility on unix. 2018-10-25 19:52:48 -07:00
Nathan Moinvaziri 8a7383a210 Added openssl crypto. 2018-10-25 19:38:36 -07:00
Nathan Moinvaziri 648f596a99 Implemented crypt using OpenSSL for linux/mac.
Renamed aes folder after source author Brian Gladman.
Added SHA2 source files.
Added USE_OPENSSL option to cmake.
Changed hash that is stored in zip to SHA256.
Commented out timestamping in windows since openssl does not support out of box.
2018-10-25 19:35:50 -07:00
Nathan Moinvaziri 8875d288db Removed unnecessary derive_key. 2018-10-25 09:01:02 -07:00
Nathan Moinvaziri 8cb7df741a Moved cp437 encoding function to zip class.
Removed util class and moved shared functions into mz_os.
Added USE_COMPAT to cmake which turns on/off compatibility layer.
2018-10-25 08:39:48 -07:00
Nathan Moinvaziri 7404279f66 Remove unnecessary #define.
Declare pbkdf2 function for test.c
2018-10-24 18:11:35 -07:00
Nathan Moinvaziri 5f09188ca9 Move cryptography functions to crypt source and header files.
Added pbkdf2 function to wzaes stream.
Changed wzaes stream to use os crypt functions.
2018-10-24 18:06:08 -07:00
Nathan Moinvaziri 21a3102db4 Combine os level headers.
Moved os utility functions into util class.
2018-10-24 09:50:16 -07:00
Nathan Moinvaziri 73025eb9db Renamed aes stream to wzaes for winzip aes.
Removed sha1 stream.
2018-10-24 09:23:14 -07:00
Nathan Moinvaziri a5e06a7bcf Added test functions for windows crypto. 2018-10-24 09:21:49 -07:00
Nathan Moinvaziri eef6ffc75d Added windows hmac, aes, sha functions using crypto api. 2018-10-24 09:21:22 -07:00
Nathan Moinvaziri e1f68fca9c Added signing support for zip (windows only rn).
Moved sha1 hash into zip reader/writer.
Added sha2 library.
2018-10-23 09:04:04 -07:00
Nathan Moinvaziri fa620e473f Added ability to zip/unzip central dir.
Added ability to set extra flags with zip writer.
Changed mask name to be unique based off disk number and offset.
Fixed bug with storing directories when encryption turned on.
2018-10-20 10:37:10 -07:00
Nathan Moinvaziri 413822a040 Store SHA1 hash of uncompressed content when compiling with USE_AES.
Added hash extrafield documented on wiki.
2018-10-20 09:45:07 -07:00
Nathan Moinvaziri a7fecd88dd Added sha1 stream. 2018-10-20 07:40:03 -07:00
Nathan Moinvaziri c565fa8f88 Added support for masking local file info.
Added functions to access the central dir streams.
2018-10-19 08:48:33 -07:00
Nathan Moinvaziri c5da4987c8 Added copy stream function.
Use inttypes for snprintf in split stream.
2018-10-18 11:02:59 -07:00
Nathan Moinvaziri d64e079092 Move section to wiki. 2018-10-18 07:50:37 -07:00
Nathan Moinvaziri f03cdc7604 Merge branch 'dev' of https://github.com/nmoinvaz/minizip into dev 2018-10-17 19:42:54 -07:00
Nathan Moinvaziri 5afeb430ee Added InfoZip app note. 2018-10-17 19:42:40 -07:00
Nathan Moinvaziri b4c953f36a Merge pull request #324 from vszakats/https
Secure URLs
2018-10-17 16:49:41 -07:00
Viktor Szakats 9dea6f2495 Secure URLs 2018-10-17 22:39:01 +00:00
Nathan Moinvaziri 544fc3b97b Merge branch 'dev' of https://github.com/nmoinvaz/minizip into dev 2018-10-09 17:04:05 -07:00
Nathan Moinvaziri 73660f9c98 Fixed potential infinite loop when reading LZMA with data error. 2018-10-09 17:03:57 -07:00
Nathan Moinvaziri da95d6e8b7 Updated comment. 2018-10-08 23:55:54 -07:00
Nathan Moinvaziri ff6f2a1db6 Version 2.6.0. 2018-10-08 23:48:58 -07:00
Nathan Moinvaziri 5e6bac5bcc Updated comments. 2018-10-08 23:47:02 -07:00
Nathan Moinvaziri 09304a6184 Fixed compiler warnings. 2018-10-08 23:44:45 -07:00
Nathan Moinvaziri 004e7d3327 Fixed compiler warnings. 2018-10-08 23:38:36 -07:00
Nathan Moinvaziri fe145502a6 Fixed compiler warnings. 2018-10-08 23:32:38 -07:00
Nathan Moinvaziri 904c408575 Fixed compiler warnings. 2018-10-08 23:31:21 -07:00
Nathan Moinvaziri 98047626d7 Fixed compiler warnings. 2018-10-08 22:41:16 -07:00
Nathan Moinvaziri e6b6616575 Fixed compiler warnings. 2018-10-08 22:40:38 -07:00
Nathan Moinvaziri c7e852e33c Merge branch 'master' into dev 2018-10-08 22:03:19 -07:00
Nathan Moinvaziri 8c272e0ff8 Turn off USE_LIBCOMP by default. 2018-10-08 21:56:40 -07:00
Nathan Moinvaziri 17bad4cf74 Update README.md 2018-10-08 21:56:01 -07:00
Nathan Moinvaziri 3b3840b7b2 Updated readme for Apple compression library. 2018-10-08 21:54:16 -07:00
Nathan Moinvaziri 0900548d8b Added support for Apple's libcompression library. 2018-10-08 21:50:17 -07:00
Nathan Moinvaziri 2bb21d769c Fixed bugs reading posix file attributes. 2018-10-08 21:47:15 -07:00
Nathan Moinvaziri 2630f5f4dc Removed unused variables. 2018-10-08 21:43:36 -07:00
Nathan Moinvaziri 15dbc445d8 Merge pull request #323 from driquet/fix-double-free
Avoid double free on comment
2018-10-08 09:15:08 -07:00
Damien Riquet 9098b6c823 Avoid double free on comment 2018-10-08 14:27:06 +02:00
Nathan Moinvaziri ed403113a2 Update README.md 2018-10-06 12:38:05 -07:00
Nathan Moinvaziri f6fe93b2a8 Merge pull request #321 from vszakats/warnfix
mz_compat.c: fix warnings
2018-10-01 08:16:49 -07:00
Viktor Szakats e5ce077bf3 mz_compat.c: fix warnings
- remove unused variables
- fix signedness disagreement:
```
mz_compat.c:961:27: warning: comparison of integers of different signs: 'int64_t' (aka 'long long') and 'uint64_t'
      (aka 'unsigned long long') [-Wsign-compare]
    if (compat->total_out == file_info->uncompressed_size)
        ~~~~~~~~~~~~~~~~~ ^  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
```
2018-10-01 14:49:49 +00:00
Nathan Moinvaziri fe7983319a Merge branch 'master' into dev 2018-09-30 10:45:03 -07:00
Nathan Moinvaziri ca8059ef61 Version 2.5.4. 2018-09-30 10:44:16 -07:00
Nathan Moinvaziri 15570e0794 Merge pull request #320 from KangLin/master
Modify  target proerty to minizip in only msvc
2018-09-26 09:19:59 -07:00
KangLin a247bfaa68 Modify target proerty to minizip in only msvc 2018-09-26 14:27:32 +08:00
Nathan Moinvaziri 682503ce78 Added compat definition for unztell. #319 2018-09-25 09:35:09 -07:00
Nathan Moinvaziri 709c1f05ce Added missing unzip compatibility functions. #319 2018-09-25 09:32:42 -07:00
Nathan Moinvaziri 0113ad8330 Update README.md 2018-09-20 08:41:29 -07:00
Nathan Moinvaziri 9ba3525779 Merge pull request #317 from vszakats/unconst
mz_zip_reader_entry_read: remove const from buf parameter
2018-09-19 08:35:11 -07:00
Viktor Szakats 9980d8ac3a mz_zip_reader_entry_read: remove const from buf parameter 2018-09-19 14:31:25 +00:00
Nathan Moinvaziri a0f617cd8a Merge pull request #318 from vszakats/owcb
mz_zip_writer_open_file: fix passing userdata pointer to overwrite_cb
2018-09-19 07:19:21 -07:00
Viktor Szakats 9747212a14 mz_zip_writer_open_file: fix passing userdata pointer to overwrite_cb 2018-09-19 08:44:59 +00:00
Nathan Moinvaziri 825bfb8fbf Version 2.5.3. 2018-09-18 20:34:01 -07:00
Nathan Moinvaziri 0863941638 Merge branch 'master' into dev 2018-09-18 20:30:37 -07:00
Nathan Moinvaziri 5469018dbc Fixed formatting. 2018-09-18 20:25:48 -07:00
Nathan Moinvaziri dacd44383d Merge pull request #315 from chrfle/progress_interval
Add mz_zip_[reader,writer]_set_progress_interval
2018-09-18 20:12:34 -07:00
Nathan Moinvaziri e80d8c7d22 Update README.md 2018-09-18 14:10:07 -07:00
Christer Fletcher 72538543aa Add mz_zip_[reader,writer]_set_progress_interval
With mz_zip_reader_set_progress_interval the user can specify how
often in milliseconds the progress callback should be called. This
makes it possible to make a smoother progress bar when using
minizip from a GUI application.
2018-09-18 19:52:44 +02:00
Nathan Moinvaziri 980b3ab699 Fixed formatting. 2018-09-17 07:46:25 -07:00
Nathan Moinvaziri e09ea0e515 Merge branch 'master' into dev 2018-09-17 07:44:45 -07:00
Nathan Moinvaziri 0b1e147394 Merge branch 'dev' of https://github.com/nmoinvaz/minizip into dev 2018-09-17 07:42:33 -07:00
Nathan Moinvaziri 9117655893 Updated test.c to latest version of minizip lib. #314 2018-09-17 07:42:27 -07:00
Nathan Moinvaziri ca505c0fde Fixed compiler warnings. 2018-09-17 07:41:59 -07:00
Nathan Moinvaziri bb96c73f7b Merge pull request #313 from chrfle/master
Make sure progress_cb is called every second
2018-09-13 11:14:47 -04:00
Christer Fletcher fa25b2ecc4 Make sure progress_cb is called every second
The time comparison was wrong and thus progress_cb was called every
second second instead of every second as intended.
2018-09-13 08:37:50 +02:00
Nathan Moinvaziri 0e7b9fc3c8 Merge pull request #310 from Coeur/typo
Fix minor typo
2018-09-11 11:19:46 -04:00
Nathan Moinvaziri 37842b678d Removed misspelling. 2018-09-11 08:18:59 -07:00
Antoine Cœur dd54b153c0 Fix minor typo 2018-09-11 09:59:57 +08:00
Nathan Moinvaziri 0db9d70659 Merge pull request #308 from vszakats/fixes
enable warnings for gcc/clang, misc minor fixes
2018-09-10 11:53:46 -04:00
Nathan Moinvaziri 09ddae6514 Fixed formatting 2018-09-10 08:52:45 -07:00
Nathan Moinvaziri 0457003448 Fixed formatting 2018-09-10 08:52:17 -07:00
Nathan Moinvaziri 1b3ce71663 Fixed formatting. 2018-09-10 08:51:33 -07:00
Nathan Moinvaziri 777db81e88 Fixed formatting 2018-09-10 08:50:29 -07:00
Nathan Moinvaziri 44a2d152de Merge pull request #311 from Alexpux/master
Fix compat header
2018-09-10 11:43:57 -04:00
Alexey Pavlov 165a40221d Fix compat header 2018-09-10 07:42:25 +03:00
Viktor Szakats ab8507608e fix remaining strncpy() calls
to always terminate the target buffer with a zero byte.
2018-09-06 22:15:29 +00:00
Nathan Moinvaziri 67d17c0442 Merge pull request #307 from driquet/fix-infinite-loop
Fix infinite loop if a read operation does not return any bytes
2018-09-05 21:00:05 -04:00
Viktor Szakats e7215072a0 enable warnings for gcc/clang, misc minor fixes
* enable -W -Wall for gcc and clang
* fix strncpy() calls that may potentially leave the target buffer without
  a terminating zero byte (in mz_zip_rw.c)
* fix warnings
* remove const from mz_path_remove_filename() parameter,
  the function is writing the passed buffer
* fix pedantic warnings (in mz_strm_buf.c):
  warning: suggest braces around empty body in an ‘if’ statement [-Wempty-body]
* fix spaces at EOL
* fix a redefinition warning for HAVE_ARC4RANDOM_BUF on macOS
2018-09-04 22:58:02 +00:00
Damien Riquet 7932c261dd Fix infinite loop if a read operation do not return any bytes 2018-09-03 16:30:42 +02:00
Nathan Moinvaziri 85d36c5e83 Clean up includes. #306 2018-08-31 16:43:41 -07:00
Nathan Moinvaziri 75c064bdf6 Merge pull request #306 from vszakats/patch-1
mz_os_posix.c: remove duplicate #include
2018-08-31 14:23:22 -07:00
Viktor Szakats a1e0db1d09 correction 2018-08-31 23:19:54 +02:00
Viktor Szakats 6f40c04998 mz_os_posix.c: remove duplicate #include 2018-08-31 20:45:54 +00:00
Nathan Moinvaziri 6d492ea3e7 Merge branch 'master' into dev 2018-08-30 19:54:52 -07:00
Nathan Moinvaziri a068933451 Merge pull request #305 from KangLin/master
Fix: if(<variable|string> LESS_EQUAL <variable|string>)
2018-08-30 19:50:55 -07:00
KangLin eb923e7d2b Fix: if(<variable|string> LESS_EQUAL <variable|string>) 2018-08-31 10:34:42 +08:00
Nathan Moinvaziri 5d9f95fca1 Fixed code analysis warnings. 2018-08-30 16:22:25 -07:00
Nathan Moinvaziri e52f092b08 Merge pull request #304 from ffontaine/master
fix linking with bzip2
2018-08-29 11:15:07 -07:00
Fabrice Fontaine 353586a4c4 fix linking with bzip2
Replace call to:
target_link_libraries(${PROJECT_NAME} Bzip2::BZip2)
by:
target_link_libraries(${PROJECT_NAME} ${BZIP2_LIBRARIES})

Indeed, BZip2::BZip2 does not seem to work with cmake 3.5.1

Signed-off-by: Fabrice Fontaine <fontaine.fabrice@gmail.com>
2018-08-29 19:47:45 +02:00
Nathan Moinvaziri 7ea3653e77 Merge pull request #303 from ffontaine/master
minizip is a C project file
2018-08-29 10:23:53 -07:00
Fabrice Fontaine 0238f0041e minizip is a C project file
Specify that minizip is a C project file otherwise compilation will
fail if no C++ compiler is found by cmake

Signed-off-by: Fabrice Fontaine <fontaine.fabrice@gmail.com>
2018-08-29 18:56:50 +02:00
Nathan Moinvaziri 242ec32883 Updated readme. 2018-08-29 08:10:09 -07:00
Nathan Moinvaziri 96520fe778 Updated readme. 2018-08-29 08:08:09 -07:00
Nathan Moinvaziri 8e61a208d5 Updated readme. 2018-08-28 10:36:40 -07:00
Nathan Moinvaziri 8e47b249fd Updated readme. 2018-08-28 10:35:59 -07:00
Nathan Moinvaziri fc4a57f14c Updated readme. 2018-08-28 10:16:41 -07:00
Nathan Moinvaziri 611adce2b3 Updated readme. 2018-08-28 10:14:03 -07:00
Nathan Moinvaziri 9eaadb4d3c Updated readme. 2018-08-28 10:11:45 -07:00
Nathan Moinvaziri de98134362 Updated readme. 2018-08-28 10:10:25 -07:00
Nathan Moinvaziri ce2c6c3521 Updated readme. 2018-08-28 10:09:01 -07:00
Nathan Moinvaziri eb60ab3493 Updated readme. 2018-08-28 10:07:58 -07:00
Nathan Moinvaziri 881f2d2ea3 Merge pull request #302 from vszakats/patch-1
README: fix Markdown typo in PR #301
2018-08-28 08:08:25 -07:00
Viktor Szakats 1872771139 README: fix Markdown typo in PR #301 2018-08-28 14:58:59 +00:00
Nathan Moinvaziri f307d59c11 Merge pull request #301 from vszakats/patch-1
README: Update/add Travis CI links, use HTTPS
2018-08-28 07:47:22 -07:00
Viktor Szakats f1698d631f README: Update/add Travis CI links, use HTTPS
* also fold some long lines
* remove double EOL at EOF
2018-08-28 07:04:48 +00:00
Nathan Moinvaziri 59ba13b5a9 Updated readme. 2018-08-27 20:59:56 -07:00
Nathan Moinvaziri d27969a853 Updated readme. 2018-08-27 20:57:52 -07:00
Nathan Moinvaziri 7b6f04e955 Updated readme. 2018-08-27 20:46:43 -07:00
Nathan Moinvaziri bc0a9005f2 Updated readme. 2018-08-27 20:32:04 -07:00
Nathan Moinvaziri c4d130878f Updated readme. 2018-08-27 20:31:22 -07:00
Nathan Moinvaziri 315ca492fd Updated readme. 2018-08-27 20:30:04 -07:00
Nathan Moinvaziri a594982712 Updated readme. 2018-08-27 20:28:14 -07:00
Nathan Moinvaziri d4ed216bd3 Added feature list. 2018-08-27 20:10:19 -07:00
Nathan Moinvaziri 8935dd19db Moved examples to wiki. 2018-08-27 19:19:39 -07:00
115 changed files with 14846 additions and 4365 deletions
+62 -9
View File
@@ -1,19 +1,72 @@
language: c
os:
- linux
compiler:
- clang
- gcc
matrix:
include:
- os: windows
compiler: clang
env: BUILDDIR="..\build\Debug"
- os: windows
compiler: gcc
env: BUILDDIR="..\build\Debug"
- os: linux
compiler: clang
env: BUILDDIR="../build"
- os: linux
compiler: clang
env: TOOL="-DMZ_ZLIB=OFF" BUILDDIR="../build"
- os: linux
compiler: clang
env: TOOL="-DMZ_BZIP=OFF" BUILDDIR="../build"
- os: linux
compiler: clang
env: TOOL="-DMZ_LZMA=OFF" BUILDDIR="../build"
- os: linux
compiler: clang
env: TOOL="-DMZ_WZAES=OFF" BUILDDIR="../build"
- os: linux
compiler: clang
env: TOOL="-DMZ_PKCRYPT=OFF" BUILDDIR="../build"
- os: linux
compiler: clang
env: TOOL="-DMZ_COMPRESS_ONLY=ON" BUILDDIR="../build"
- os: linux
compiler: clang
env: TOOL="-DMZ_DECOMPRESS_ONLY=ON" BUILDDIR="../build"
- os: linux
compiler: clang
env: TOOL="-DMZ_PKCRYPT=OFF -DMZ_WZAES=OFF" BUILDDIR="../build"
- os: linux
compiler: clang
env: TOOL="-DMZ_OPENSSL=ON" BUILDDIR="../build"
- os: linux
compiler: gcc
env: BUILDDIR="../build"
- os: osx
compiler: clang
env: BUILDDIR="../build"
- os: osx
compiler: gcc
env: BUILDDIR="../build"
- os: osx
compiler: clang
env: TOOL="-DMZ_LIBCOMP=ON" BUILDDIR="../build"
- os: osx
compiler: gcc
env: TOOL="-DMZ_LIBCOMP=ON" BUILDDIR="../build"
- os: osx
compiler: clang
env: TOOL="-DMZ_OPENSSL=ON -DOPENSSL_ROOT_DIR=/usr/local/opt/openssl -DOPENSSL_INCLUDE_DIRS=/usr/local/opt/openssl/include -DOPENSSL_CRYPTO_LIBRARY=/usr/local/opt/openssl/lib/libcrypto.dylib -DOPENSSL_SSL_LIBRARY=/usr/local/opt/openssl/lib/libssl.dylib" BUILDDIR="../build"
before_install:
- sudo apt-get install libbsd-dev
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sudo apt-get install libbsd-dev; fi
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sudo apt-get install libssl-dev; fi
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew install openssl; fi
before_script:
- mkdir build
- cd build
- cmake .. -DBUILD_TEST=ON
- cmake .. -DMZ_BUILD_TEST=ON -DMZ_BUILD_UNIT_TEST=ON $TOOL
script:
- make
- cmake --build .
- ctest --output-on-failure -C Debug
+559 -186
View File
@@ -1,5 +1,5 @@
#***************************************************************************
# Copyright (C) 2017-2018 Nathan Moinvaziri
# Copyright (C) 2017-2019 Nathan Moinvaziri
# https://github.com/nmoinvaz/minizip
# Copyright (C) 2016 Matthias Schmieder
# schmieder.matthias@gmail.com
@@ -7,251 +7,446 @@
cmake_minimum_required(VERSION 3.0.2)
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)
message(STATUS "Using CMake version ${CMAKE_VERSION}")
project("minizip")
option(MZ_COMPAT "Enables compatibility layer" ON)
option(MZ_ZLIB "Enables ZLIB compression" ON)
option(MZ_BZIP2 "Enables BZIP2 compression" ON)
option(MZ_LZMA "Enables LZMA compression" ON)
option(MZ_PKCRYPT "Enables PKWARE traditional encryption" ON)
option(MZ_WZAES "Enables WinZIP AES encryption" ON)
option(MZ_LIBCOMP "Enables Apple compression" OFF)
option(MZ_OPENSSL "Enables OpenSSL for encryption" OFF)
option(MZ_BRG "Enables Brian Gladman's encryption library" OFF)
option(MZ_COMPRESS_ONLY "Only support compression" OFF)
option(MZ_DECOMPRESS_ONLY "Only support decompression" OFF)
option(MZ_BUILD_TEST "Builds minizip test executable" OFF)
option(MZ_BUILD_UNIT_TEST "Builds minizip unit test project" OFF)
option(MZ_BUILD_FUZZ_TEST "Builds minizip fuzzer executables" OFF)
include(CheckLibraryExists)
include(CheckSymbolExists)
include(GNUInstallDirs)
# MZ_ZLIB_ROOT - Parent directory of zlib installation
# MZ_BZIP2_ROOT - Parent directory of BZip2 installation
# MZ_OPENSSL_ROOT - Parent directory of OpenSSL installation
set(INSTALL_BIN_DIR ${CMAKE_INSTALL_BINDIR} CACHE PATH "Installation directory for executables")
set(INSTALL_LIB_DIR ${CMAKE_INSTALL_LIBDIR} CACHE PATH "Installation directory for libraries")
set(INSTALL_INC_DIR ${CMAKE_INSTALL_INCLUDEDIR} CACHE PATH "Installation directory for headers")
set(INSTALL_MAN_DIR ${CMAKE_INSTALL_MANDIR} CACHE PATH "Installation directory for manual pages")
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.")
project("minizip" C)
# Minizip library version
set(VERSION "2.5.2")
set(VERSION "2.8.3")
# Minizip api version
set(SOVERSION "2.5")
# Set cmake debug postfix to d
set(CMAKE_DEBUG_POSTFIX "d")
include(CheckLibraryExists)
include(CheckSymbolExists)
include(CheckFunctionExists)
include(CheckIncludeFile)
include(CheckTypeSize)
include(GNUInstallDirs)
include(ExternalProject)
# Ensure correct version of zlib is referenced
if(USE_ZLIB)
set(ZLIB_ROOT ${DEF_ZLIB_ROOT} CACHE PATH "Parent directory of zlib installation")
find_package(ZLIB REQUIRED)
set(INSTALL_BIN_DIR ${CMAKE_INSTALL_FULL_BINDIR} CACHE PATH "Installation directory for executables")
set(INSTALL_LIB_DIR ${CMAKE_INSTALL_FULL_LIBDIR} CACHE PATH "Installation directory for libraries")
set(INSTALL_INC_DIR ${CMAKE_INSTALL_FULL_INCLUDEDIR} CACHE PATH "Installation directory for headers")
set(INSTALL_MAN_DIR ${CMAKE_INSTALL_FULL_MANDIR} CACHE PATH "Installation directory for manual pages")
set(INSTALL_PKGCONFIG_DIR "${CMAKE_INSTALL_FULL_LIBDIR}/pkgconfig" CACHE PATH "Installation directory for pkgconfig (.pc) files")
set(INSTALL_CMAKE_DIR "${CMAKE_INSTALL_FULL_LIBDIR}/cmake/minizip" CACHE PATH "Installation directory for cmake files.")
set(MINIZIP_PC ${CMAKE_CURRENT_BINARY_DIR}/minizip.pc)
if(EXISTS ${MINIZIP_PC}.cmakein)
configure_file(${MINIZIP_PC}.cmakein ${MINIZIP_PC} @ONLY)
endif()
set(PROJECT_NAME minizip)
# Check if zlib installation is present
if(MZ_ZLIB AND NOT MZ_LIBCOMP)
set(ZLIB_ROOT ${MZ_ZLIB_ROOT} CACHE PATH "Parent directory of zlib installation")
find_package(ZLIB)
if(ZLIB_FOUND)
message(STATUS "Using ZLIB ${ZLIB_VERSION_STRING}")
include_directories(${ZLIB_INCLUDE_DIRS})
else()
if (NOT ZLIB_TAG)
set(ZLIB_TAG master)
endif()
if (NOT ZLIB_REPOSITORY)
set(ZLIB_REPOSITORY https://github.com/madler/zlib)
endif()
message(STATUS "Using ZLIB ${ZLIB_REPOSITORY} ${ZLIB_TAG}")
set(ZLIB_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/lib/zlib)
set(ZLIB_INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/install/zlib)
set(ZLIB_INCLUDE_DIRS ${ZLIB_INSTALL_DIR}/include)
if(WIN32)
if(${CMAKE_GENERATOR} MATCHES "Visual Studio.*")
set(ZLIB_LIBRARIES
debug ${ZLIB_INSTALL_DIR}/lib/zlibstaticd.lib
optimized ${ZLIB_INSTALL_DIR}/lib/zlibstatic.lib)
else()
if(CMAKE_BUILD_TYPE EQUAL Debug)
set(ZLIB_LIBRARIES
${ZLIB_INSTALL_DIR}/lib/zlibstaticd.lib)
else()
set(ZLIB_LIBRARIES
${ZLIB_INSTALL_DIR}/lib/zlibstatic.lib)
endif()
endif()
else()
set(ZLIB_LIBRARIES
${ZLIB_INSTALL_DIR}/lib/libz.a)
endif()
# Build args variable based on cache variables
unset(CMAKE_ARGS)
get_cmake_property(CACHE_VARS CACHE_VARIABLES)
foreach(CACHE_VAR ${CACHE_VARS})
get_property(CACHE_VAR_HELPSTRING CACHE ${CACHE_VAR} PROPERTY HELPSTRING)
if(CACHE_VAR_HELPSTRING STREQUAL "No help, variable specified on the command line.")
get_property(CACHE_VAR_TYPE CACHE ${CACHE_VAR} PROPERTY TYPE)
if(CACHE_VAR_TYPE STREQUAL "UNINITIALIZED")
set(CACHE_VAR_TYPE)
else()
set(CACHE_VAR_TYPE :${CACHE_VAR_TYPE})
endif()
if (NOT CACHE_VAR MATCHES "^MZ_")
list(APPEND CMAKE_ARGS "-D${CACHE_VAR}${CACHE_VAR_TYPE}=${${CACHE_VAR}}")
endif()
endif()
endforeach()
ExternalProject_Add(zlib
PREFIX zlib
GIT_REPOSITORY ${ZLIB_REPOSITORY}
GIT_TAG ${ZLIB_TAG}
SOURCE_DIR ${ZLIB_SOURCE_DIR}
BUILD_IN_SOURCE 1
INSTALL_DIR ${ZLIB_INSTALL_DIR}
BUILD_BYPRODUCTS ${ZLIB_STATIC_LIBRARIES}
UPDATE_COMMAND ""
CMAKE_ARGS "${CMAKE_ARGS};-DCMAKE_INSTALL_PREFIX:STRING=${ZLIB_INSTALL_DIR}")
include_directories(${ZLIB_INCLUDE_DIRS})
endif()
add_definitions(-DHAVE_ZLIB)
endif()
# Check if bzip2 installation is present
if(USE_BZIP2)
set(BZIP2_ROOT ${DEF_BZIP2_ROOT} CACHE PATH "Parent directory of bzip2 installation")
if(MZ_BZIP2)
set(BZIP2_ROOT ${MZ_BZIP2_ROOT} CACHE PATH "Parent directory of bzip2 installation")
find_package(BZip2)
if(BZIP2_FOUND)
message(STATUS "Using BZIP2 ${BZIP2_VERSION_STRING}")
include_directories(${BZIP2_INCLUDE_DIRS})
add_definitions(-DHAVE_BZIP2)
endif()
add_definitions(-DHAVE_BZIP2)
endif()
set(MINIZIP_PC ${CMAKE_CURRENT_BINARY_DIR}/minizip.pc)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/minizip.pc.cmakein ${MINIZIP_PC} @ONLY)
set(PROJECT_NAME libminizip)
# Check to see if openssl installation is present
if (MZ_OPENSSL)
set(OPENSSL_ROOT ${MZ_OPENSSL_ROOT} CACHE PATH "Parent directory of openssl installation")
find_package(OpenSSL)
if (OPENSSL_FOUND)
message(STATUS "Using OpenSSL ${OPENSSL_VERSION}")
include_directories(${OPENSSL_INCLUDE_DIRS})
endif()
endif()
# Initial source files
set(MINIZIP_SRC
mz_crypt.c
mz_os.c
mz_compat.c
mz_strm.c
mz_strm_buf.c
mz_strm_crc32.c
mz_strm_mem.c
mz_strm_posix.c
mz_strm_split.c
mz_zip.c
mz_zip_rw.c)
# Initial header files
set(MINIZIP_PUBLIC_HEADERS
mz.h
mz_os.h
mz_compat.h
mz_crypt.h
mz_strm.h
mz_strm_buf.h
mz_strm_crc32.h
mz_strm_mem.h
mz_strm_posix.h
mz_strm_split.h
mz_strm_os.h
mz_zip.h
mz_zip_rw.h)
if(COMPRESS_ONLY)
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
# Setup predefined macros
if(MZ_COMPRESS_ONLY)
add_definitions(-DMZ_ZIP_NO_DECOMPRESSION)
endif()
if(DECOMPRESS_ONLY)
if(MZ_DECOMPRESS_ONLY)
add_definitions(-DMZ_ZIP_NO_COMPRESSION)
endif()
if(NOT USE_PKCRYPT AND NOT USE_AES)
if(NOT MZ_PKCRYPT AND NOT MZ_WZAES)
add_definitions(-DMZ_ZIP_NO_ENCRYPTION)
endif()
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
# Check for system includes
check_include_file(stdint.h HAVE_STDINT_H)
check_include_file(inttypes.h HAVE_INTTYPES_H)
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")
add_definitions(-D_CRT_SECURE_NO_DEPRECATE)
if(HAVE_STDINT_H)
add_definitions(-DHAVE_STDINT_H)
endif()
if(HAVE_INTTYPES_H)
add_definitions(-DHAVE_INTTYPES_H)
endif()
if("${CMAKE_SYSTEM_NAME}" STREQUAL "WindowsStore")
# Check for large file support
check_type_size(off64_t OFF64_T)
if(HAVE_OFF64_T)
add_definitions(-D__USE_FILE_OFFSET64)
add_definitions(-D__USE_LARGEFILE64)
add_definitions(-D_LARGEFILE64_SOURCE)
add_definitions(-D_FILE_OFFSET_BITS=64)
endif()
# Check for fseeko support
check_function_exists(fseeko HAVE_FSEEKO)
if(NOT HAVE_FSEEKO)
add_definitions(-DNO_FSEEKO)
endif()
# Windows specific
if(WIN32)
set(CMAKE_DEBUG_POSTFIX "d")
add_definitions(-D_CRT_SECURE_NO_DEPRECATE)
list(APPEND MINIZIP_SRC "mz_os_win32.c" "mz_strm_os_win32.c")
if (MZ_PKCRYPT OR MZ_WZAES)
if (NOT MZ_OPENSSL AND NOT OPENSSL_FOUND AND NOT MZ_BRG)
list(APPEND MINIZIP_SRC "mz_crypt_win32.c")
endif()
endif()
endif()
if(CMAKE_SYSTEM_NAME STREQUAL "WindowsStore")
add_definitions(-DMZ_WINRT_API)
endif()
# Unix specific
if(UNIX)
add_compile_options(-O3)
list(APPEND MINIZIP_SRC "mz_os_posix.c")
list(APPEND MINIZIP_PUBLIC_HEADERS "mz_os_posix.h")
set(define_lfs_macros TRUE)
if(ANDROID)
string(REGEX REPLACE "android-([0-9+])" "\\1"
android_api "${ANDROID_PLATFORM}")
if(${android_api} LESS 24)
set(define_lfs_macros FALSE)
add_definitions(-D_POSIX_C_SOURCE=200112L)
list(APPEND MINIZIP_SRC "mz_os_posix.c" "mz_strm_os_posix.c")
if ((MZ_PKCRYPT OR MZ_WZAES) AND NOT (MZ_OPENSSL AND OPENSSL_FOUND))
if (APPLE AND NOT MZ_BRG)
list(APPEND MINIZIP_SRC "mz_crypt_apple.c")
else()
set(MZ_BRG TRUE)
endif()
endif()
if(define_lfs_macros)
add_definitions(-D__USE_FILE_OFFSET64)
add_definitions(-D__USE_LARGEFILE64)
add_definitions(-D_LARGEFILE64_SOURCE)
add_definitions(-D_FILE_OFFSET_BITS=64)
endif()
check_symbol_exists("getrandom" "sys/random.h" HAVE_GETRANDOM)
if (HAVE_GETRANDOM)
add_definitions(-DHAVE_GETRANDOM)
endif()
check_symbol_exists("arc4random_buf" "stdlib.h" HAVE_ARC4RANDOM_BUF)
if (HAVE_ARC4RANDOM_BUF)
add_definitions(-DHAVE_ARC4RANDOM_BUF)
else()
check_symbol_exists("arc4random" "stdlib.h" HAVE_ARC4RANDOM)
if (HAVE_ARC4RANDOM)
add_definitions(-DHAVE_ARC4RANDOM)
# Check to see which random generation functions we have
check_symbol_exists("getrandom" "sys/random.h" HAVE_GETRANDOM)
if (HAVE_GETRANDOM)
add_definitions(-DHAVE_GETRANDOM)
endif()
check_symbol_exists("arc4random_buf" "stdlib.h" HAVE_ARC4RANDOM_BUF)
if (HAVE_ARC4RANDOM_BUF)
add_definitions(-DHAVE_ARC4RANDOM_BUF)
else()
check_symbol_exists("arc4random" "stdlib.h" HAVE_ARC4RANDOM)
if (HAVE_ARC4RANDOM)
add_definitions(-DHAVE_ARC4RANDOM)
endif()
endif()
endif()
if(NOT HAVE_ARC4RANDOM_BUF)
find_package(PkgConfig REQUIRED)
if(NOT HAVE_ARC4RANDOM_BUF)
find_package(PkgConfig REQUIRED)
pkg_check_modules(LIBBSD libbsd)
if (LIBBSD_FOUND)
check_library_exists("${LIBBSD_LIBRARIES}" "arc4random_buf" "${LIBBSD_LIBRARY_DIRS}" HAVE_LIBBSD_ARC4RANDOM_BUF)
if (HAVE_LIBBSD_ARC4RANDOM_BUF)
add_definitions(-DHAVE_LIBBSD)
add_definitions(-DHAVE_ARC4RANDOM_BUF)
include_directories(${LIBBSD_INCLUDE_DIRS})
link_directories(${LIBBSD_LIBRARY_DIRS})
pkg_check_modules(LIBBSD libbsd)
if (LIBBSD_FOUND)
check_library_exists("${LIBBSD_LIBRARIES}" "arc4random_buf"
"${LIBBSD_LIBRARY_DIRS}" HAVE_LIBBSD_ARC4RANDOM_BUF)
if (HAVE_LIBBSD_ARC4RANDOM_BUF)
add_definitions(-DHAVE_LIBBSD)
add_definitions(-DHAVE_ARC4RANDOM_BUF)
include_directories(${LIBBSD_INCLUDE_DIRS})
link_directories(${LIBBSD_LIBRARY_DIRS})
endif()
endif()
endif()
endif()
find_package(Iconv QUIET)
if (Iconv_FOUND)
message(STATUS "Using Iconv")
include_directories(${Iconv_INCLUDE_DIRS})
endif()
endif()
if(USE_PKCRYPT)
# Include compatibility layer
if(MZ_COMPAT)
list(APPEND MINIZIP_SRC "mz_compat.c")
list(APPEND MINIZIP_PUBLIC_HEADERS "mz_compat.h")
set(COMPAT_HEADER "\
/* file.h -- Compatibility layer shim\n\
part of the MiniZip project\n\n\
This program is distributed under the terms of the same license as zlib.\n\
See the accompanying LICENSE file for the full text of the license.\n\
*/\n\n\
#ifndef MZ_COMPAT_FILE\n\
#define MZ_COMPAT_FILE\n\n\
#include \"mz_compat.h\"\n\n\
#endif\n")
string(REPLACE "file.h" "zip.h" ZIP_COMPAT_HEADER ${COMPAT_HEADER})
string(REPLACE "MZ_COMPAT_FILE" "MZ_COMPAT_ZIP" ZIP_COMPAT_HEADER ${ZIP_COMPAT_HEADER})
file(WRITE "zip.h" ${ZIP_COMPAT_HEADER})
list(APPEND MINIZIP_PUBLIC_HEADERS "zip.h")
string(REPLACE "file.h" "unzip.h" UNZIP_COMPAT_HEADER ${COMPAT_HEADER})
string(REPLACE "MZ_COMPAT_FILE" "MZ_COMPAT_UNZIP" UNZIP_COMPAT_HEADER ${UNZIP_COMPAT_HEADER})
file(WRITE "unzip.h" ${UNZIP_COMPAT_HEADER})
list(APPEND MINIZIP_PUBLIC_HEADERS "unzip.h")
endif()
# Include PKCRYPT
if(MZ_PKCRYPT)
add_definitions(-DHAVE_PKCRYPT)
list(APPEND MINIZIP_SRC "mz_strm_pkcrypt.c")
list(APPEND MINIZIP_PUBLIC_HEADERS "mz_strm_pkcrypt.h")
endif()
if(USE_AES)
add_definitions(-DHAVE_AES)
# Include WinZIP AES
if(MZ_WZAES)
add_definitions(-DHAVE_WZAES)
list(APPEND MINIZIP_SRC "mz_strm_aes.c")
list(APPEND MINIZIP_PUBLIC_HEADERS "mz_strm_aes.h")
set(AES_SRC
lib/aes/aescrypt.c
lib/aes/aeskey.c
lib/aes/aestab.c
lib/aes/hmac.c
lib/aes/pwd2key.c
lib/aes/sha1.c)
set(AES_PUBLIC_HEADERS
lib/aes/aes.h
lib/aes/aesopt.h
lib/aes/aestab.h
lib/aes/brg_endian.h
lib/aes/brg_types.h
lib/aes/hmac.h
lib/aes/pwd2key.h
lib/aes/sha1.h)
include_directories(lib/aes)
source_group("AES" FILES ${AES_SRC} ${AES_PUBLIC_HEADERS})
list(APPEND MINIZIP_SRC "mz_strm_wzaes.c")
list(APPEND MINIZIP_PUBLIC_HEADERS "mz_strm_wzaes.h")
endif()
if(USE_ZLIB)
add_definitions(-DHAVE_ZLIB)
# Include BRG
if(MZ_BRG)
list(APPEND MINIZIP_SRC "mz_crypt_brg.c")
list(APPEND MINIZIP_SRC "mz_strm_zlib.c")
list(APPEND MINIZIP_PUBLIC_HEADERS "mz_strm_zlib.h")
add_definitions(-DMZ_ZIP_NO_SIGNING)
include(CheckFunctionExists)
set(CMAKE_REQUIRED_LIBRARIES ZLIB::ZLIB)
CHECK_FUNCTION_EXISTS(z_get_crc_table
NEEDS_Z_PREFIX)
set(BRG_SRC
lib/brg/aescrypt.c
lib/brg/aeskey.c
lib/brg/aestab.c
lib/brg/hmac.c
lib/brg/sha1.c
lib/brg/sha2.c)
if(NEEDS_Z_PREFIX)
add_definitions(-DZ_PREFIX)
set(BRG_PUBLIC_HEADERS
lib/brg/aes.h
lib/brg/aesopt.h
lib/brg/aestab.h
lib/brg/brg_endian.h
lib/brg/brg_types.h
lib/brg/hmac.h
lib/brg/sha1.h
lib/brg/sha2.h)
include_directories(lib/brg)
source_group("BRG" FILES ${BRG_SRC} ${BRG_PUBLIC_HEADERS})
endif()
# Include OpenSSL
if(MZ_OPENSSL AND OPENSSL_FOUND)
list(APPEND MINIZIP_SRC "mz_crypt_openssl.c")
endif()
# Include ZLIB
if(MZ_ZLIB)
if(MZ_LIBCOMP)
add_definitions(-DHAVE_LIBCOMP)
list(APPEND MINIZIP_SRC "mz_strm_libcomp.c")
list(APPEND MINIZIP_PUBLIC_HEADERS "mz_strm_libcomp.h")
else()
add_definitions(-DHAVE_ZLIB)
if(ZLIB_COMPAT)
add_definitions(-DZLIB_COMPAT)
endif()
list(APPEND MINIZIP_SRC "mz_strm_zlib.c")
list(APPEND MINIZIP_PUBLIC_HEADERS "mz_strm_zlib.h")
if (ZLIB_FOUND)
include(CheckFunctionExists)
check_function_exists(z_get_crc_table NEEDS_Z_PREFIX)
if(NEEDS_Z_PREFIX)
add_definitions(-DZ_PREFIX)
endif()
endif()
endif()
endif()
if(USE_BZIP2 AND BZIP2_FOUND)
# Include BZIP2
if(MZ_BZIP2)
add_definitions(-DHAVE_BZIP2)
list(APPEND MINIZIP_SRC "mz_strm_bzip.c")
list(APPEND MINIZIP_PUBLIC_HEADERS "mz_strm_bzip.h")
set(CMAKE_REQUIRED_LIBRARIES BZip2::BZip2)
elseif(USE_BZIP2)
add_definitions(-DHAVE_BZIP2)
add_definitions(-DBZ_NO_STDIO)
if(NOT BZIP2_FOUND)
add_definitions(-DBZ_NO_STDIO)
list(APPEND MINIZIP_SRC "mz_strm_bzip.c")
list(APPEND MINIZIP_PUBLIC_HEADERS "mz_strm_bzip.h")
set(BZIP2_SRC
lib/bzip2/blocksort.c
lib/bzip2/bzlib.c
lib/bzip2/compress.c
lib/bzip2/crctable.c
lib/bzip2/decompress.c
lib/bzip2/huffman.c
lib/bzip2/randtable.c)
set(BZIP2_SRC
lib/bzip2/blocksort.c
lib/bzip2/bzlib.c
lib/bzip2/compress.c
lib/bzip2/crctable.c
lib/bzip2/decompress.c
lib/bzip2/huffman.c
lib/bzip2/randtable.c)
set(BZIP2_PUBLIC_HEADERS
lib/bzip2/bzlib.h
lib/bzip2/bzlib_private.h)
set(BZIP2_PUBLIC_HEADERS
lib/bzip2/bzlib.h
lib/bzip2/bzlib_private.h)
include_directories(lib/bzip2)
include_directories(lib/bzip2)
source_group("BZip2" FILES ${BZIP2_SRC} ${BZIP2_PUBLIC_HEADERS})
source_group("BZip2" FILES ${BZIP2_SRC} ${BZIP2_PUBLIC_HEADERS})
endif()
endif()
if(USE_LZMA)
# Include LZMA
if(MZ_LZMA)
add_definitions(-DHAVE_LZMA)
add_definitions(-DHAVE_CONFIG_H)
add_definitions(-DLZMA_API_STATIC)
add_definitions(-DHAVE_CONFIG_H)
check_include_file(limits.h HAVE_LIMITS_H)
check_include_file(string.h HAVE_STRING_H)
check_include_file(strings.h HAVE_STRINGS_H)
check_include_file(memory.h HAVE_MEMORY_H)
check_include_file(stdbool.h HAVE_STDBOOL_H)
check_include_file(immintrin.h HAVE_IMMINTRIN_H)
if(HAVE_LIMITS_H)
add_definitions(-DHAVE_LIMITS_H)
endif()
if(HAVE_STRING_H)
add_definitions(-DHAVE_STRING_H)
endif()
if(HAVE_STRINGS_H)
add_definitions(-DHAVE_STRINGS_H)
endif()
if(HAVE_MEMORY_H)
add_definitions(-DHAVE_MEMORY_H)
endif()
if(HAVE_STDBOOL_H)
add_definitions(-DHAVE_STDBOOL_H)
endif()
if(HAVE_IMMINTRIN_H)
add_definitions(-DHAVE_IMMINTRIN_H)
endif()
list(APPEND MINIZIP_SRC "mz_strm_lzma.c")
list(APPEND MINIZIP_PUBLIC_HEADERS "mz_strm_lzma.h")
@@ -357,18 +552,38 @@ if(USE_LZMA)
endif()
# Enable x86 optimizations if supported
if(CMAKE_C_COMPILER MATCHES ".*clang")
include(CheckCCompilerFlag)
macro(enable_option_if_supported option variable)
check_c_compiler_flag("-Werror=unused-command-line-argument ${option}" ${variable})
if(${variable})
add_compile_options(${option})
endif()
endmacro()
if(CMAKE_BUILD_TYPE MATCHES Release)
if(CMAKE_C_COMPILER_ID MATCHES "Clang")
include(CheckCCompilerFlag)
enable_option_if_supported(-msse3 check_opt_sse3)
enable_option_if_supported(-msse4.1 check_opt_sse41)
enable_option_if_supported(-maes check_opt_aes)
macro(enable_option_if_supported option variable)
check_c_compiler_flag("-Werror=unused-command-line-argument ${option}" ${variable})
if(${variable})
add_compile_options(${option})
endif()
endmacro()
enable_option_if_supported(-msse3 check_opt_sse3)
enable_option_if_supported(-msse4.1 check_opt_sse41)
enable_option_if_supported(-maes check_opt_aes)
endif()
if(NOT MSVC)
add_compile_options(-O3)
endif()
else()
if(MSVC)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Zi")
else()
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g")
endif()
endif()
# Enable compiler warnings
if(MSVC)
add_compile_options(/W3)
else()
add_compile_options(-W -Wall)
endif()
# Create minizip library
@@ -376,33 +591,53 @@ source_group("Minizip" FILES ${MINIZIP_SRC} ${MINIZIP_PUBLIC_HEADERS})
add_library(${PROJECT_NAME}
${MINIZIP_SRC} ${MINIZIP_PUBLIC_HEADERS}
${AES_SRC} ${AES_PUBLIC_HEADERS}
${BRG_SRC} ${BRG_PUBLIC_HEADERS}
${BZIP2_SRC} ${BZIP2_PUBLIC_HEADERS}
${LZMA_SRC} ${LZMA_PUBLIC_HEADERS})
if (MINGW AND BUILD_SHARED_LIBS)
set_target_properties(${PROJECT_NAME} PROPERTIES ARCHIVE_OUTPUT_NAME "minizip")
endif ()
set_target_properties(${PROJECT_NAME} PROPERTIES VERSION ${VERSION} SOVERSION ${SOVERSION})
set_target_properties(${PROJECT_NAME} PROPERTIES LINKER_LANGUAGE C PREFIX ""
POSITION_INDEPENDENT_CODE 1)
if(USE_ZLIB)
target_link_libraries(${PROJECT_NAME} ZLIB::ZLIB)
endif()
if(USE_BZIP2 AND BZIP2_FOUND)
target_link_libraries(${PROJECT_NAME} BZip2::BZip2)
endif()
if(USE_LZMA)
set_target_properties(${PROJECT_NAME} PROPERTIES
VERSION ${VERSION}
SOVERSION ${SOVERSION}
LINKER_LANGUAGE C
PREFIX ""
DEFINE_SYMBOL "MZ_EXPORTS"
POSITION_INDEPENDENT_CODE 1)
if(MZ_LZMA)
set_target_properties(${PROJECT_NAME} PROPERTIES C_STANDARD 99)
endif()
if(UNIX)
target_link_libraries(${PROJECT_NAME} ${LIBBSD_LIBRARIES})
# Link with external libraries
if(MZ_ZLIB AND NOT MZ_LIBCOMP)
target_link_libraries(${PROJECT_NAME} ${ZLIB_LIBRARIES})
if(NOT ZLIB_FOUND)
add_dependencies(${PROJECT_NAME} zlib)
endif()
endif()
if(MZ_BZIP2 AND BZIP2_FOUND)
target_link_libraries(${PROJECT_NAME} ${BZIP2_LIBRARIES})
endif()
if(MZ_LIBCOMP)
target_link_libraries(${PROJECT_NAME} compression)
endif()
if(Iconv_FOUND AND NOT Iconv_IS_BUILT_IN)
target_link_libraries(${PROJECT_NAME} ${Iconv_LIBRARIES})
endif()
if(MZ_OPENSSL AND OPENSSL_FOUND)
target_link_libraries(${PROJECT_NAME} ${OPENSSL_LIBRARIES})
elseif(UNIX)
if(APPLE AND NOT MZ_BRG)
message(STATUS "Using Security Framework")
target_link_libraries(${PROJECT_NAME} "-framework CoreFoundation")
target_link_libraries(${PROJECT_NAME} "-framework Security")
set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS "-Wl,-F/Library/Frameworks")
else()
target_link_libraries(${PROJECT_NAME} ${LIBBSD_LIBRARIES})
endif()
endif()
target_include_directories(${PROJECT_NAME} PUBLIC $<INSTALL_INTERFACE:${INSTALL_INC_DIR}>)
# Install files
if(NOT SKIP_INSTALL_LIBRARIES AND NOT SKIP_INSTALL_ALL)
install(TARGETS ${PROJECT_NAME} EXPORT ${PROJECT_NAME}
INCLUDES DESTINATION "${INSTALL_INC_DIR}"
@@ -420,12 +655,150 @@ if(NOT SKIP_INSTALL_FILES AND NOT SKIP_INSTALL_ALL)
install(FILES ${MINIZIP_PC} DESTINATION "${INSTALL_PKGCONFIG_DIR}")
endif()
if(BUILD_TEST)
add_executable(minizip "minizip.c")
target_link_libraries(minizip ${PROJECT_NAME})
# Build test executable
if(MZ_BUILD_TEST)
add_executable(minizip_cmd "minizip.c" "test/test.c" "test/test.h")
set_target_properties(minizip_cmd PROPERTIES OUTPUT_NAME ${PROJECT_NAME})
target_link_libraries(minizip_cmd ${PROJECT_NAME})
if(NOT SKIP_INSTALL_BINARIES AND NOT SKIP_INSTALL_ALL)
install(TARGETS minizip
RUNTIME DESTINATION "bin")
install(TARGETS minizip_cmd RUNTIME DESTINATION "bin")
endif()
endif()
if(MZ_BUILD_UNIT_TEST)
enable_testing()
function(create_compress_tests EXTRA_NAME EXTRA_ARGS)
if(MZ_DECOMPRESS_ONLY)
return()
endif()
list(FIND EXTRA_ARGS "-z" ZIPCD_IDX)
if(${ZIPCD_IDX} EQUAL -1)
set(COMPRESS_METHOD_NAMES "raw")
set(COMPRESS_METHOD_ARGS "-0")
endif()
if (MZ_ZLIB OR MZ_LIBCOMP)
list(APPEND COMPRESS_METHOD_NAMES "deflate")
list(APPEND COMPRESS_METHOD_ARGS "-9")
endif()
if (MZ_BZIP2)
list(APPEND COMPRESS_METHOD_NAMES "bzip2")
list(APPEND COMPRESS_METHOD_ARGS "-b")
endif()
if (MZ_LZMA)
list(APPEND COMPRESS_METHOD_NAMES "lzma")
list(APPEND COMPRESS_METHOD_ARGS "-m")
endif()
list(LENGTH COMPRESS_METHOD_NAMES COMPRESS_METHOD_COUNT)
math(EXPR COMPRESS_METHOD_COUNT "${COMPRESS_METHOD_COUNT}-1")
foreach(INDEX RANGE ${COMPRESS_METHOD_COUNT})
list(GET COMPRESS_METHOD_NAMES ${INDEX} COMPRESS_METHOD_NAME)
list(GET COMPRESS_METHOD_ARGS ${INDEX} COMPRESS_METHOD_ARG)
add_test(NAME ${COMPRESS_METHOD_NAME}-zip-${EXTRA_NAME}
COMMAND minizip_cmd ${COMPRESS_METHOD_ARG} -o ${EXTRA_ARGS}
result.zip test.c test.h empty.txt random.bin uniform.bin fuzz
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test)
add_test(NAME ${COMPRESS_METHOD_NAME}-list-${EXTRA_NAME}
COMMAND minizip_cmd -l ${EXTRA_ARGS} result.zip
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test)
if(NOT MZ_COMPRESS_ONLY)
add_test(NAME ${COMPRESS_METHOD_NAME}-unzip-${EXTRA_NAME}
COMMAND minizip_cmd -x -o ${EXTRA_ARGS} -d out result.zip
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test)
endif()
add_test(NAME ${COMPRESS_METHOD_NAME}-append-${EXTRA_NAME}
COMMAND minizip_cmd ${COMPRESS_METHOD_ARG} -a ${EXTRA_ARGS}
result.zip single.txt
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test)
if(NOT MZ_COMPRESS_ONLY)
add_test(NAME ${COMPRESS_METHOD_NAME}-append-unzip-${EXTRA_NAME}
COMMAND minizip_cmd -x -o ${EXTRA_ARGS} -d out result.zip
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test)
endif()
add_test(NAME ${COMPRESS_METHOD_NAME}-erase-${EXTRA_NAME}
COMMAND minizip_cmd -o -e result.zip test.c test.h
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test)
if(NOT MZ_COMPRESS_ONLY)
add_test(NAME ${COMPRESS_METHOD_NAME}-erase-unzip-${EXTRA_NAME}
COMMAND minizip_cmd -x -o ${EXTRA_ARGS} -d out result.zip
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test)
endif()
endforeach()
endfunction()
# Perform tests against ourself
create_compress_tests("generic" "")
create_compress_tests("span" "-k;1024")
create_compress_tests("zipcd" "-z")
if(MZ_PKCRYPT)
create_compress_tests("pkcrypt" "-p;test123")
endif()
if(MZ_WZAES)
create_compress_tests("wzaes" "-s;-p;test123")
endif()
if(MZ_OPENSSL AND OPENSSL_FOUND)
create_compress_tests("signed" "-h;ia.p12;-w;test")
create_compress_tests("secure" "-z;-h;ia.p12;-w;test")
endif()
# Perform tests on others
if(NOT MZ_COMPRESS_ONLY)
if(MZ_ZLIB)
add_test(NAME unzip-tiny
COMMAND minizip_cmd -x -o ${EXTRA_ARGS} -d out
fuzz/unzip_fuzzer_seed_corpus/tiny.zip
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test)
endif()
if(MZ_BZIP2)
add_test(NAME unzip-bzip2
COMMAND minizip_cmd -x -o ${EXTRA_ARGS} -d out
fuzz/unzip_fuzzer_seed_corpus/bzip2.zip
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test)
endif()
if(MZ_LZMA)
add_test(NAME unzip-lzma
COMMAND minizip_cmd -x -o ${EXTRA_ARGS} -d out
fuzz/unzip_fuzzer_seed_corpus/lzma.zip
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test)
endif()
if(MZ_PKCRYPT)
add_test(NAME unzip-pkcrypt
COMMAND minizip_cmd -x -o ${EXTRA_ARGS} -d out -p test123
fuzz/unzip_fuzzer_seed_corpus/encrypted_pkcrypt.zip
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test)
endif()
if(MZ_WZAES)
add_test(NAME unzip-wzaes
COMMAND minizip_cmd -x -o ${EXTRA_ARGS} -d out -p test123
fuzz/unzip_fuzzer_seed_corpus/encrypted_wzaes.zip
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test)
endif()
endif()
endif()
#Build fuzzer executables
if(MZ_BUILD_FUZZ_TEST)
if (NOT CMAKE_C_COMPILER_ID STREQUAL "Clang")
set(FUZZER_SRC "test/fuzz/standalone.c")
endif()
add_executable(zip_fuzzer "test/fuzz/zip_fuzzer.c" ${FUZZER_SRC})
add_executable(unzip_fuzzer "test/fuzz/unzip_fuzzer.c" ${FUZZER_SRC})
if (CMAKE_C_COMPILER_ID STREQUAL "Clang")
enable_language(CXX)
target_link_libraries(zip_fuzzer "FuzzingEngine")
set_target_properties(zip_fuzzer PROPERTIES LINKER_LANGUAGE CXX)
target_link_libraries(unzip_fuzzer "FuzzingEngine")
set_target_properties(unzip_fuzzer PROPERTIES LINKER_LANGUAGE CXX)
endif()
target_link_libraries(zip_fuzzer ${PROJECT_NAME})
target_link_libraries(unzip_fuzzer ${PROJECT_NAME})
if(NOT SKIP_INSTALL_BINARIES AND NOT SKIP_INSTALL_ALL)
install(TARGETS zip_fuzzer RUNTIME DESTINATION "bin")
install(TARGETS unzip_fuzzer RUNTIME DESTINATION "bin")
endif()
endif()
+1 -1
View File
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'Minizip'
s.version = '2.5.2'
s.version = '2.8.3'
s.license = 'zlib'
s.summary = 'Minizip contrib in zlib with the latest bug fixes and advanced features'
s.description = <<-DESC
+99 -178
View File
@@ -1,23 +1,67 @@
# Minizip 2.5.2
# minizip 2.8.3
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.
minizip is a zip manipulation library written in C that is supported on Windows, macOS, and Linux.
Dev: ![Dev Branch Status](https://travis-ci.org/nmoinvaz/minizip.svg?branch=dev)
Master: ![Master Branch Status](https://travis-ci.org/nmoinvaz/minizip.svg?branch=master)
[![License: Zlib](https://img.shields.io/badge/license-zlib-lightgrey.svg)](https://github.com/nmoinvaz/minizip/blob/master/LICENSE)
[![Code Quality: Cpp](https://img.shields.io/lgtm/grade/cpp/g/nmoinvaz/minizip.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/nmoinvaz/minizip/context:cpp)
For my older fork of this library checkout the [1.2](https://github.com/nmoinvaz/minizip/tree/1.2) branch.
For the original work maintained by Mark Adler checkout the zlib minizip [contrib](https://github.com/madler/zlib/tree/master/contrib/minizip).
Maintained by Nathan Moinvaziri.
## Branches
| Name | State | Version | Description |
|:- |:-:|:-:|:-|
|[master](https://github.com/nmoinvaz/minizip/tree/master)|Active [![Master Branch Status](https://api.travis-ci.org/nmoinvaz/minizip.svg?branch=master)](https://travis-ci.org/nmoinvaz/minizip/branches)|2.x|Modern rewrite of 1.2 branch that includes more advanced features, improvements in code maintainability and readability, and the reduction of duplicate code. Compatibility layer provided for older versions.|
|[dev](https://github.com/nmoinvaz/minizip/tree/dev)|Active [![Dev Branch Status](https://api.travis-ci.org/nmoinvaz/minizip.svg?branch=dev)](https://travis-ci.org/nmoinvaz/minizip/branches)|2.x|Latest development code|
|[1.2](https://github.com/nmoinvaz/minizip/tree/1.2)|Stale| 1.x|Drop-in replacement for zlib's minizip that includes WinZip AES encryption, disk splitting, I/O buffering and some additional fixes.|
|[1.1](https://github.com/nmoinvaz/minizip/tree/1.1)|Stale| 1.x|Original minizip as of zlib 1.2.11.|
## History
Minizip was originally developed by [Gilles Vollant](https://www.winimage.com/zLibDll/minizip.html) and
had been contributed to by many people. As part of the zlib distribution, Mark Adler still maintains the
original [minizip](https://github.com/madler/zlib/tree/master/contrib/minizip) project which is included in this repository as a reference.
My work with the minizip library began in 2006 when I started submitting bugs I found to
Gilles Vollant. In 2010, I implemented some additional features like WinZip AES encryption, disk splitting, and
I/O buffering that were necessary for another project I was working on. Shortly after, I created this public repository
so I could share these and other improvements with the rest of the world. I have been maintaining and actively
developing this code base ever since. At the beginning of 2017, I began the work to refactor and rewrite
the library as version 2 because it had become difficult to maintain and code readability suffered over the years.
## Features
+ Creating and extracting zip archives.
+ Adding and removing entries from zip archives.
+ Read and write raw zip entry data.
+ Reading and writing zip archives from memory.
+ Zlib, BZIP2, and LZMA compression methods.
+ Password protection through Traditional PKWARE and [WinZIP AES](https://www.winzip.com/aes_info.htm) encryption.
+ Buffered streaming for improved I/O performance.
+ NTFS timestamp support for UTC last modified, last accessed, and creation dates.
+ Disk split support for splitting zip archives into multiple files.
+ Preservation of file attributes across file systems.
+ Unicode filename support through UTF-8 encoding.
+ Legacy character encoding support CP437, CP932, CP936, CP950.
+ Turn off compilation of compression, decompression, or encryption.
+ Windows (Win32 & WinRT), macOS and Linux platform support.
+ Streaming interface for easy implementation of additional platforms.
+ Support for Apple's compression library ZLIB implementation.
+ Zero out local file header information.
+ Zip/unzip of central directory to reduce size.
+ Ability to generate and verify CMS signature for each entry.
+ Recover the central directory if it is corrupt or missing.
+ Example minizip command line tool.
## Build
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.
2. Run cmake in the minizip directory.
```
cmake . -DBUILD_TEST=ON
cmake . -DMZ_BUILD_TEST=ON
cmake --build .
```
@@ -25,181 +69,58 @@ cmake --build .
| 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 . --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\
```
| MZ_COMPAT | Enables compatibility layer | ON |
| MZ_ZLIB | Enables ZLIB compression | ON |
| MZ_BZIP2 | Enables BZIP2 compression | ON |
| MZ_LZMA | Enables LZMA compression | ON |
| MZ_PKCRYPT | Enables PKWARE traditional encryption | ON |
| MZ_WZAES | Enables WinZIP AES encryption | ON |
| MZ_LIBCOMP | Enables Apple compression | OFF |
| MZ_OPENSSL | Enables OpenSSL encryption | OFF |
| MZ_BRG | Enables Brian Gladman's library | OFF |
| MZ_COMPRESS_ONLY | Only support compression | OFF |
| MZ_DECOMPRESS_ONLY | Only support decompression | OFF |
| MZ_BUILD_TEST | Builds minizip test executable | OFF |
| MZ_BUILD_UNIT_TEST | Builds minizip unit test project | OFF |
| MZ_BUILD_FUZZ_TEST | Builds minizip fuzz executables | OFF |
## Contents
| File(s) | Description | Required |
|:- |:-|:-:|
| minizip.c | Sample application | No |
| mz_compat.\* | Minizip 1.0 compatibility layer | No |
| mz.h | Error codes and flags | Yes |
| mz_os\* | OS specific helper functions | Encryption, Disk Splitting |
| mz_strm.\* | Stream interface | Yes |
| mz_strm_aes.\* | WinZIP AES stream | No |
| mz_strm_buf.\* | Buffered stream | No |
| mz_strm_bzip.\* | BZIP2 stream using libbzip2 | No |
| mz_strm_crc32.\* | CRC32 stream | Yes |
| 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 |
| mz_zip.\* | Zip format | Yes |
| mz_zip_rw.\* | Zip reader/writer | No |
| File(s) | Description |
|:- |:-|
| minizip.c | Sample application |
| mz_compat.\* | Minizip 1.x compatibility layer |
| mz.h | Error codes and flags |
| mz_os\* | Platform specific file/utility functions |
| mz_crypt\* | Configuration specific crypto/hashing functions |
| mz_strm.\* | Stream interface |
| mz_strm_buf.\* | Buffered stream |
| mz_strm_bzip.\* | BZIP2 stream using libbzip2 |
| mz_strm_libcomp.\* | Apple compression stream |
| mz_strm_lzma.\* | LZMA stream using liblzma |
| mz_strm_mem.\* | Memory stream |
| mz_strm_split.\* | Disk splitting stream |
| mz_strm_pkcrypt.\* | PKWARE traditional encryption stream |
| mz_strm_os\* | Platform specific file stream |
| mz_strm_wzaes.\* | WinZIP AES stream |
| mz_strm_zlib.\* | Deflate stream using zlib |
| mz_zip.\* | Zip format |
| mz_zip_rw.\* | Zip reader/writer |
## Features
## Third-Party Libraries
### Compression Methods
+ [zlib](https://zlib.net/) written by Mark Adler and Jean-loup Gailly.
+ Not included in this repository
+ Or alternatively, [zlib-ng](https://github.com/Dead2/zlib-ng) by Hans Kristian Rosbach
+ [BZIP2](https://www.sourceware.org/bzip2/) written by Julian Seward.
+ [liblzma](https://tukaani.org/xz/) written by Lasse Collin.
+ Modifications were made to support the ZIP file format specification
+ [AES](https://github.com/BrianGladman/aes) and [SHA](https://github.com/BrianGladman/sha) libraries of Brian Gladman.
#### BZIP2
## Acknowledgments
+ Requires ``cmake . -DUSE_BZIP2=ON`` or ``#define HAVE_BZIP2``
+ Requires [BZIP2](http://www.bzip.org/) library
Thanks to [Gilles Vollant](https://www.winimage.com/zLibDll/minizip.html) on which this work is originally based on.
#### LZMA
Thanks go out to all the people who have taken the time to contribute code reviews, testing and/or patches. This project would not have been nearly as good without you.
+ Requires ``cmake . -DUSE_LZMA=ON`` or ``#define HAVE_LZMA``
+ Requires [liblzma](https://tukaani.org/xz/) library with modifications
### Encryption
#### [WinZIP AES Encryption](https://www.winzip.com/aes_info.htm)
+ Requires ``cmake . -DUSE_AES=ON`` or ``#define HAVE_AES``
+ Requires Brian Gladman's [AES](https://github.com/BrianGladman/aes) and [SHA](https://github.com/BrianGladman/sha) libraries
When zipping with a password it will always use AES 256-bit encryption.
When unzipping it will use AES decryption only if necessary.
#### Disabling All Encryption
To disable encryption use the following cmake commands:
```
cmake . -DUSE_AES=OFF
cmake . -DUSE_PKCRYPT=OFF
```
### NTFS Timestamps
Support has been added for UTC last modified, last accessed, and creation dates.
### Streams
This library has been refactored around streams.
#### Memory Streaming
To unzip from a zip file in memory pass the memory stream to the open function.
```
uint8_t *zip_buffer = NULL;
int32_t zip_buffer_size = 0;
void *mem_stream = NULL;
// fill zip_buffer with zip contents
mz_stream_mem_create(&mem_stream);
mz_stream_mem_set_buffer(mem_stream, zip_buffer, zip_buffer_size);
mz_stream_open(mem_stream, NULL, MZ_OPEN_MODE_READ);
void *zip_handle = mz_zip_open(mem_stream, MZ_OPEN_MODE_READ);
// do unzip operations
mz_stream_mem_delete(&mem_stream);
```
To create a zip file in memory first create a growable memory stream and pass it to the open function.
```
void *mem_stream = NULL;
mz_stream_mem_create(&mem_stream);
mz_stream_mem_set_grow_size(mem_stream, (128 * 1024));
mz_stream_open(mem_stream, NULL, MZ_OPEN_MODE_CREATE);
void *zip_handle = mz_zip_open(mem_stream, MZ_OPEN_MODE_WRITE);
// do unzip operations
mz_stream_mem_delete(&mem_stream);
```
For a complete example, see test_zip_mem() in [test.c](https://github.com/nmoinvaz/minizip/blob/master/test/test.c).
#### Buffered Streaming
By default the library will read bytes typically one at a time. The buffered stream allows for buffered read and write operations to improve I/O performance.
```
void *stream = NULL;
void *buf_stream = NULL;
mz_stream_os_create(&stream)
// do open os stream
mz_stream_buffered_create(&buf_stream);
mz_stream_buffered_open(buf_stream, NULL, MZ_OPEN_MODE_READ);
mz_stream_buffered_set_base(buf_stream, stream);
void *zip_handle = mz_zip_open(buf_stream, MZ_OPEN_MODE_READ);
```
#### Disk Splitting Stream
To create an archive with multiple disks use the disk splitting stream and supply a disk size value in bytes.
```
void *stream = NULL;
void *split_stream = NULL;
mz_stream_os_create(&stream);
mz_stream_split_create(&split_stream);
mz_stream_split_set_prop_int64(split_stream, MZ_STREAM_PROP_DISK_SIZE, 64 * 1024);
mz_stream_set_base(split_stream, stream);
mz_stream_open(split_stream, path..
void *zip_handle = mz_zip_open(split_stream, MZ_OPEN_MODE_WRITE);
```
### Windows RT
+ Requires ``#define MZ_WINRT_API``
## Limitations
+ Archives are required to have a central directory.
+ Central directory header values should be correct and it is necessary for the compressed size to be accurate for AES encryption.
+ Central directory encryption is not supported due to licensing restrictions mentioned by PKWARE in their zip appnote.
+ Central directory is the only data stored on the last disk of a split-disk archive and doesn't follow disk size restrictions.
The [ZIP format](https://github.com/nmoinvaz/minizip/blob/master/doc/appnote.txt) was defined by Phil Katz of PKWARE.
+3686
View File
File diff suppressed because it is too large Load Diff
-182
View File
@@ -1,182 +0,0 @@
/*
---------------------------------------------------------------------------
Copyright (c) 1998-2010, Brian Gladman, Worcester, UK. All rights reserved.
The redistribution and use of this software (with or without changes)
is allowed without the payment of fees or royalties provided that:
source code distributions include the above copyright notice, this
list of conditions and the following disclaimer;
binary distributions include the above copyright notice, this list
of conditions and the following disclaimer in their documentation.
This software is provided 'as is' with no explicit or implied warranties
in respect of its operation, including, but not limited to, correctness
and fitness for purpose.
---------------------------------------------------------------------------
Issue Date: 20/12/2007
This is an implementation of RFC2898, which specifies key derivation from
a password and a salt value.
*/
#include <string.h>
#include "hmac.h"
#include "pwd2key.h"
#if defined(__cplusplus)
extern "C"
{
#endif
void derive_key(const unsigned char pwd[], /* the PASSWORD */
unsigned int pwd_len, /* and its length */
const unsigned char salt[], /* the SALT and its */
unsigned int salt_len, /* length */
unsigned int iter, /* the number of iterations */
unsigned char key[], /* space for the output key */
unsigned int key_len)/* and its required length */
{
unsigned int i, j, k, n_blk, h_size;
unsigned char uu[HMAC_MAX_OUTPUT_SIZE], ux[HMAC_MAX_OUTPUT_SIZE];
hmac_ctx c1[1], c2[1], c3[1];
/* set HMAC context (c1) for password */
h_size = hmac_sha_begin(HMAC_SHA1, c1);
hmac_sha_key(pwd, pwd_len, c1);
/* set HMAC context (c2) for password and salt */
memcpy(c2, c1, sizeof(hmac_ctx));
hmac_sha_data(salt, salt_len, c2);
/* find the number of SHA blocks in the key */
n_blk = 1 + (key_len - 1) / h_size;
for(i = 0; i < n_blk; ++i) /* for each block in key */
{
/* ux[] holds the running xor value */
memset(ux, 0, h_size);
/* set HMAC context (c3) for password and salt */
memcpy(c3, c2, sizeof(hmac_ctx));
/* enter additional data for 1st block into uu */
uu[0] = (unsigned char)((i + 1) >> 24);
uu[1] = (unsigned char)((i + 1) >> 16);
uu[2] = (unsigned char)((i + 1) >> 8);
uu[3] = (unsigned char)(i + 1);
/* this is the key mixing iteration */
for(j = 0, k = 4; j < iter; ++j)
{
/* add previous round data to HMAC */
hmac_sha_data(uu, k, c3);
/* obtain HMAC for uu[] */
hmac_sha_end(uu, h_size, c3);
/* xor into the running xor block */
for(k = 0; k < h_size; ++k)
ux[k] ^= uu[k];
/* set HMAC context (c3) for password */
memcpy(c3, c1, sizeof(hmac_ctx));
}
/* compile key blocks into the key output */
j = 0; k = i * h_size;
while(j < h_size && k < key_len)
key[k++] = ux[j++];
}
}
#ifdef TEST
#include <stdio.h>
struct
{ unsigned int pwd_len;
unsigned int salt_len;
unsigned int it_count;
unsigned char *pwd;
unsigned char salt[32];
unsigned char key[32];
} tests[] =
{
{ 8, 4, 5, (unsigned char*)"password",
{
0x12, 0x34, 0x56, 0x78
},
{
0x5c, 0x75, 0xce, 0xf0, 0x1a, 0x96, 0x0d, 0xf7,
0x4c, 0xb6, 0xb4, 0x9b, 0x9e, 0x38, 0xe6, 0xb5
}
},
{ 8, 8, 5, (unsigned char*)"password",
{
0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12
},
{
0xd1, 0xda, 0xa7, 0x86, 0x15, 0xf2, 0x87, 0xe6,
0xa1, 0xc8, 0xb1, 0x20, 0xd7, 0x06, 0x2a, 0x49
}
},
{ 8, 21, 1, (unsigned char*)"password",
{
"ATHENA.MIT.EDUraeburn"
},
{
0xcd, 0xed, 0xb5, 0x28, 0x1b, 0xb2, 0xf8, 0x01,
0x56, 0x5a, 0x11, 0x22, 0xb2, 0x56, 0x35, 0x15
}
},
{ 8, 21, 2, (unsigned char*)"password",
{
"ATHENA.MIT.EDUraeburn"
},
{
0x01, 0xdb, 0xee, 0x7f, 0x4a, 0x9e, 0x24, 0x3e,
0x98, 0x8b, 0x62, 0xc7, 0x3c, 0xda, 0x93, 0x5d
}
},
{ 8, 21, 1200, (unsigned char*)"password",
{
"ATHENA.MIT.EDUraeburn"
},
{
0x5c, 0x08, 0xeb, 0x61, 0xfd, 0xf7, 0x1e, 0x4e,
0x4e, 0xc3, 0xcf, 0x6b, 0xa1, 0xf5, 0x51, 0x2b
}
}
};
int main()
{ unsigned int i, j, key_len = 256;
unsigned char key[256];
printf("\nTest of RFC2898 Password Based Key Derivation");
for(i = 0; i < 5; ++i)
{
derive_key(tests[i].pwd, tests[i].pwd_len, tests[i].salt,
tests[i].salt_len, tests[i].it_count, key, key_len);
printf("\ntest %i: ", i + 1);
printf("key %s", memcmp(tests[i].key, key, 16) ? "is bad" : "is good");
for(j = 0; j < key_len && j < 64; j += 4)
{
if(j % 16 == 0)
printf("\n");
printf("0x%02x%02x%02x%02x ", key[j], key[j + 1], key[j + 2], key[j + 3]);
}
printf(j < key_len ? " ... \n" : "\n");
}
printf("\n");
return 0;
}
#if defined(__cplusplus)
}
#endif
#endif
-45
View File
@@ -1,45 +0,0 @@
/*
---------------------------------------------------------------------------
Copyright (c) 1998-2010, Brian Gladman, Worcester, UK. All rights reserved.
The redistribution and use of this software (with or without changes)
is allowed without the payment of fees or royalties provided that:
source code distributions include the above copyright notice, this
list of conditions and the following disclaimer;
binary distributions include the above copyright notice, this list
of conditions and the following disclaimer in their documentation.
This software is provided 'as is' with no explicit or implied warranties
in respect of its operation, including, but not limited to, correctness
and fitness for purpose.
---------------------------------------------------------------------------
Issue Date: 20/12/2007
This is an implementation of RFC2898, which specifies key derivation from
a password and a salt value.
*/
#ifndef PWD2KEY_H
#define PWD2KEY_H
#if defined(__cplusplus)
extern "C"
{
#endif
void derive_key(
const unsigned char pwd[], /* the PASSWORD, and */
unsigned int pwd_len, /* its length */
const unsigned char salt[], /* the SALT and its */
unsigned int salt_len, /* length */
unsigned int iter, /* the number of iterations */
unsigned char key[], /* space for the output key */
unsigned int key_len); /* and its required length */
#if defined(__cplusplus)
}
#endif
#endif
View File
+12 -6
View File
@@ -393,8 +393,10 @@ AES_RETURN aes_xi(decrypt_key192)(const unsigned char *key, aes_decrypt_ctx cx[1
cx->ks[v(48,(3))] = ss[3] = word_in(key, 3);
#ifdef DEC_KS_UNROLL
cx->ks[v(48,(4))] = ff(ss[4] = word_in(key, 4));
cx->ks[v(48,(5))] = ff(ss[5] = word_in(key, 5));
ss[4] = word_in(key, 4);
cx->ks[v(48,(4))] = ff(ss[4]);
ss[5] = word_in(key, 5);
cx->ks[v(48,(5))] = ff(ss[5]);
kdf6(cx->ks, 0); kd6(cx->ks, 1);
kd6(cx->ks, 2); kd6(cx->ks, 3);
kd6(cx->ks, 4); kd6(cx->ks, 5);
@@ -485,10 +487,14 @@ AES_RETURN aes_xi(decrypt_key256)(const unsigned char *key, aes_decrypt_ctx cx[1
cx->ks[v(56,(3))] = ss[3] = word_in(key, 3);
#ifdef DEC_KS_UNROLL
cx->ks[v(56,(4))] = ff(ss[4] = word_in(key, 4));
cx->ks[v(56,(5))] = ff(ss[5] = word_in(key, 5));
cx->ks[v(56,(6))] = ff(ss[6] = word_in(key, 6));
cx->ks[v(56,(7))] = ff(ss[7] = word_in(key, 7));
ss[4] = word_in(key, 4);
cx->ks[v(56,(4))] = ff(ss[4]);
ss[5] = word_in(key, 5);
cx->ks[v(56,(5))] = ff(ss[5]);
ss[6] = word_in(key, 6);
cx->ks[v(56,(6))] = ff(ss[6]);
ss[7] = word_in(key, 7);
cx->ks[v(56,(7))] = ff(ss[7]);
kdf8(cx->ks, 0); kd8(cx->ks, 1);
kd8(cx->ks, 2); kd8(cx->ks, 3);
kd8(cx->ks, 4); kd8(cx->ks, 5);
+1 -1
View File
@@ -101,7 +101,7 @@ int hmac_sha_begin(enum hmac_hash hash, hmac_ctx cx[1])
case HMAC_SHA512_128:
cx->f_begin = (hf_begin *)sha512_128_begin;
cx->f_hash = (hf_hash *)sha512_128_hash;
cx->f_end = (hf_begin *)sha512_128_end;
cx->f_end = (hf_end *)sha512_128_end;
cx->input_len = SHA512_128_BLOCK_SIZE;
cx->output_len = SHA512_128_DIGEST_SIZE;
break;
+1
View File
@@ -32,6 +32,7 @@ extern "C"
#endif
#include "sha1.h"
#include "sha2.h"
#if defined(SHA_224) || defined(SHA_256) || defined(SHA_384) || defined(SHA_512)
#define HMAC_MAX_OUTPUT_SIZE SHA2_MAX_DIGEST_SIZE
View File
View File
+949
View File
@@ -0,0 +1,949 @@
/*
---------------------------------------------------------------------------
Copyright (c) 1998-2010, Brian Gladman, Worcester, UK. All rights reserved.
The redistribution and use of this software (with or without changes)
is allowed without the payment of fees or royalties provided that:
source code distributions include the above copyright notice, this
list of conditions and the following disclaimer;
binary distributions include the above copyright notice, this list
of conditions and the following disclaimer in their documentation.
This software is provided 'as is' with no explicit or implied warranties
in respect of its operation, including, but not limited to, correctness
and fitness for purpose.
---------------------------------------------------------------------------
Issue Date: 20/12/2007
This code implements sha256, sha384 and sha512 but the latter two
functions rely on efficient 64-bit integer operations that may not be
very efficient on 32-bit machines
The sha256 functions use a type 'sha256_ctx' to hold details of the
current hash state and uses the following three calls:
void sha256_begin( sha256_ctx ctx[1] )
void sha256_hash( const unsigned char data[],
unsigned long len, sha256_ctx ctx[1] )
void sha_end1( unsigned char hval[], sha256_ctx ctx[1] )
The first subroutine initialises a hash computation by setting up the
context in the sha256_ctx context. The second subroutine hashes 8-bit
bytes from array data[] into the hash state withinh sha256_ctx context,
the number of bytes to be hashed being given by the the unsigned long
integer len. The third subroutine completes the hash calculation and
places the resulting digest value in the array of 8-bit bytes hval[].
The sha384 and sha512 functions are similar and use the interfaces:
void sha384_begin( sha384_ctx ctx[1] );
void sha384_hash( const unsigned char data[],
unsigned long len, sha384_ctx ctx[1] );
void sha384_end( unsigned char hval[], sha384_ctx ctx[1] );
void sha512_begin( sha512_ctx ctx[1] );
void sha512_hash( const unsigned char data[],
unsigned long len, sha512_ctx ctx[1] );
void sha512_end( unsigned char hval[], sha512_ctx ctx[1] );
In addition there is a function sha2 that can be used to call all these
functions using a call with a hash length parameter as follows:
int sha2_begin( unsigned long len, sha2_ctx ctx[1] );
void sha2_hash( const unsigned char data[],
unsigned long len, sha2_ctx ctx[1] );
void sha2_end( unsigned char hval[], sha2_ctx ctx[1] );
The data block length in any one call to any of these hash functions must
be no more than 2^32 - 1 bits or 2^29 - 1 bytes.
My thanks to Erik Andersen <andersen@codepoet.org> for testing this code
on big-endian systems and for his assistance with corrections
*/
#if 1
#define UNROLL_SHA2 /* for SHA2 loop unroll */
#endif
#include <string.h> /* for memcpy() etc. */
#include "sha2.h"
#include "brg_endian.h"
#if defined(__cplusplus)
extern "C"
{
#endif
#if defined( _MSC_VER ) && ( _MSC_VER > 800 )
#pragma intrinsic(memcpy)
#pragma intrinsic(memset)
#endif
#if 0 && defined(_MSC_VER)
#define rotl32 _lrotl
#define rotr32 _lrotr
#else
#define rotl32(x,n) (((x) << n) | ((x) >> (32 - n)))
#define rotr32(x,n) (((x) >> n) | ((x) << (32 - n)))
#endif
#if !defined(bswap_32)
#define bswap_32(x) ((rotr32((x), 24) & 0x00ff00ff) | (rotr32((x), 8) & 0xff00ff00))
#endif
#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
#define SWAP_BYTES
#else
#undef SWAP_BYTES
#endif
#if 0
#define ch(x,y,z) (((x) & (y)) ^ (~(x) & (z)))
#define maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
#else /* Thanks to Rich Schroeppel and Colin Plumb for the following */
#define ch(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
#define maj(x,y,z) (((x) & (y)) | ((z) & ((x) ^ (y))))
#endif
/* round transforms for SHA256 and SHA512 compression functions */
#define vf(n,i) v[(n - i) & 7]
#define hf(i) (p[i & 15] += \
g_1(p[(i + 14) & 15]) + p[(i + 9) & 15] + g_0(p[(i + 1) & 15]))
#define v_cycle(i,j) \
vf(7,i) += (j ? hf(i) : p[i]) + k_0[i+j] \
+ s_1(vf(4,i)) + ch(vf(4,i),vf(5,i),vf(6,i)); \
vf(3,i) += vf(7,i); \
vf(7,i) += s_0(vf(0,i))+ maj(vf(0,i),vf(1,i),vf(2,i))
#if defined(SHA_224) || defined(SHA_256)
#define SHA256_MASK (SHA256_BLOCK_SIZE - 1)
#if defined(SWAP_BYTES)
#define bsw_32(p,n) \
{ int _i = (n); while(_i--) ((uint32_t*)p)[_i] = bswap_32(((uint32_t*)p)[_i]); }
#else
#define bsw_32(p,n)
#endif
#define s_0(x) (rotr32((x), 2) ^ rotr32((x), 13) ^ rotr32((x), 22))
#define s_1(x) (rotr32((x), 6) ^ rotr32((x), 11) ^ rotr32((x), 25))
#define g_0(x) (rotr32((x), 7) ^ rotr32((x), 18) ^ ((x) >> 3))
#define g_1(x) (rotr32((x), 17) ^ rotr32((x), 19) ^ ((x) >> 10))
#define k_0 k256
/* rotated SHA256 round definition. Rather than swapping variables as in */
/* FIPS-180, different variables are 'rotated' on each round, returning */
/* to their starting positions every eight rounds */
#define q(n) v##n
#define one_cycle(a,b,c,d,e,f,g,h,k,w) \
q(h) += s_1(q(e)) + ch(q(e), q(f), q(g)) + k + w; \
q(d) += q(h); q(h) += s_0(q(a)) + maj(q(a), q(b), q(c))
/* SHA256 mixing data */
const uint32_t k256[64] =
{ 0x428a2f98ul, 0x71374491ul, 0xb5c0fbcful, 0xe9b5dba5ul,
0x3956c25bul, 0x59f111f1ul, 0x923f82a4ul, 0xab1c5ed5ul,
0xd807aa98ul, 0x12835b01ul, 0x243185beul, 0x550c7dc3ul,
0x72be5d74ul, 0x80deb1feul, 0x9bdc06a7ul, 0xc19bf174ul,
0xe49b69c1ul, 0xefbe4786ul, 0x0fc19dc6ul, 0x240ca1ccul,
0x2de92c6ful, 0x4a7484aaul, 0x5cb0a9dcul, 0x76f988daul,
0x983e5152ul, 0xa831c66dul, 0xb00327c8ul, 0xbf597fc7ul,
0xc6e00bf3ul, 0xd5a79147ul, 0x06ca6351ul, 0x14292967ul,
0x27b70a85ul, 0x2e1b2138ul, 0x4d2c6dfcul, 0x53380d13ul,
0x650a7354ul, 0x766a0abbul, 0x81c2c92eul, 0x92722c85ul,
0xa2bfe8a1ul, 0xa81a664bul, 0xc24b8b70ul, 0xc76c51a3ul,
0xd192e819ul, 0xd6990624ul, 0xf40e3585ul, 0x106aa070ul,
0x19a4c116ul, 0x1e376c08ul, 0x2748774cul, 0x34b0bcb5ul,
0x391c0cb3ul, 0x4ed8aa4aul, 0x5b9cca4ful, 0x682e6ff3ul,
0x748f82eeul, 0x78a5636ful, 0x84c87814ul, 0x8cc70208ul,
0x90befffaul, 0xa4506cebul, 0xbef9a3f7ul, 0xc67178f2ul,
};
/* Compile 64 bytes of hash data into SHA256 digest value */
/* NOTE: this routine assumes that the byte order in the */
/* ctx->wbuf[] at this point is such that low address bytes */
/* in the ORIGINAL byte stream will go into the high end of */
/* words on BOTH big and little endian systems */
VOID_RETURN sha256_compile(sha256_ctx ctx[1])
{
#if !defined(UNROLL_SHA2)
uint32_t j, *p = ctx->wbuf, v[8];
memcpy(v, ctx->hash, sizeof(ctx->hash));
for(j = 0; j < 64; j += 16)
{
v_cycle( 0, j); v_cycle( 1, j);
v_cycle( 2, j); v_cycle( 3, j);
v_cycle( 4, j); v_cycle( 5, j);
v_cycle( 6, j); v_cycle( 7, j);
v_cycle( 8, j); v_cycle( 9, j);
v_cycle(10, j); v_cycle(11, j);
v_cycle(12, j); v_cycle(13, j);
v_cycle(14, j); v_cycle(15, j);
}
ctx->hash[0] += v[0]; ctx->hash[1] += v[1];
ctx->hash[2] += v[2]; ctx->hash[3] += v[3];
ctx->hash[4] += v[4]; ctx->hash[5] += v[5];
ctx->hash[6] += v[6]; ctx->hash[7] += v[7];
#else
uint32_t *p = ctx->wbuf,v0,v1,v2,v3,v4,v5,v6,v7;
v0 = ctx->hash[0]; v1 = ctx->hash[1];
v2 = ctx->hash[2]; v3 = ctx->hash[3];
v4 = ctx->hash[4]; v5 = ctx->hash[5];
v6 = ctx->hash[6]; v7 = ctx->hash[7];
one_cycle(0,1,2,3,4,5,6,7,k256[ 0],p[ 0]);
one_cycle(7,0,1,2,3,4,5,6,k256[ 1],p[ 1]);
one_cycle(6,7,0,1,2,3,4,5,k256[ 2],p[ 2]);
one_cycle(5,6,7,0,1,2,3,4,k256[ 3],p[ 3]);
one_cycle(4,5,6,7,0,1,2,3,k256[ 4],p[ 4]);
one_cycle(3,4,5,6,7,0,1,2,k256[ 5],p[ 5]);
one_cycle(2,3,4,5,6,7,0,1,k256[ 6],p[ 6]);
one_cycle(1,2,3,4,5,6,7,0,k256[ 7],p[ 7]);
one_cycle(0,1,2,3,4,5,6,7,k256[ 8],p[ 8]);
one_cycle(7,0,1,2,3,4,5,6,k256[ 9],p[ 9]);
one_cycle(6,7,0,1,2,3,4,5,k256[10],p[10]);
one_cycle(5,6,7,0,1,2,3,4,k256[11],p[11]);
one_cycle(4,5,6,7,0,1,2,3,k256[12],p[12]);
one_cycle(3,4,5,6,7,0,1,2,k256[13],p[13]);
one_cycle(2,3,4,5,6,7,0,1,k256[14],p[14]);
one_cycle(1,2,3,4,5,6,7,0,k256[15],p[15]);
one_cycle(0,1,2,3,4,5,6,7,k256[16],hf( 0));
one_cycle(7,0,1,2,3,4,5,6,k256[17],hf( 1));
one_cycle(6,7,0,1,2,3,4,5,k256[18],hf( 2));
one_cycle(5,6,7,0,1,2,3,4,k256[19],hf( 3));
one_cycle(4,5,6,7,0,1,2,3,k256[20],hf( 4));
one_cycle(3,4,5,6,7,0,1,2,k256[21],hf( 5));
one_cycle(2,3,4,5,6,7,0,1,k256[22],hf( 6));
one_cycle(1,2,3,4,5,6,7,0,k256[23],hf( 7));
one_cycle(0,1,2,3,4,5,6,7,k256[24],hf( 8));
one_cycle(7,0,1,2,3,4,5,6,k256[25],hf( 9));
one_cycle(6,7,0,1,2,3,4,5,k256[26],hf(10));
one_cycle(5,6,7,0,1,2,3,4,k256[27],hf(11));
one_cycle(4,5,6,7,0,1,2,3,k256[28],hf(12));
one_cycle(3,4,5,6,7,0,1,2,k256[29],hf(13));
one_cycle(2,3,4,5,6,7,0,1,k256[30],hf(14));
one_cycle(1,2,3,4,5,6,7,0,k256[31],hf(15));
one_cycle(0,1,2,3,4,5,6,7,k256[32],hf( 0));
one_cycle(7,0,1,2,3,4,5,6,k256[33],hf( 1));
one_cycle(6,7,0,1,2,3,4,5,k256[34],hf( 2));
one_cycle(5,6,7,0,1,2,3,4,k256[35],hf( 3));
one_cycle(4,5,6,7,0,1,2,3,k256[36],hf( 4));
one_cycle(3,4,5,6,7,0,1,2,k256[37],hf( 5));
one_cycle(2,3,4,5,6,7,0,1,k256[38],hf( 6));
one_cycle(1,2,3,4,5,6,7,0,k256[39],hf( 7));
one_cycle(0,1,2,3,4,5,6,7,k256[40],hf( 8));
one_cycle(7,0,1,2,3,4,5,6,k256[41],hf( 9));
one_cycle(6,7,0,1,2,3,4,5,k256[42],hf(10));
one_cycle(5,6,7,0,1,2,3,4,k256[43],hf(11));
one_cycle(4,5,6,7,0,1,2,3,k256[44],hf(12));
one_cycle(3,4,5,6,7,0,1,2,k256[45],hf(13));
one_cycle(2,3,4,5,6,7,0,1,k256[46],hf(14));
one_cycle(1,2,3,4,5,6,7,0,k256[47],hf(15));
one_cycle(0,1,2,3,4,5,6,7,k256[48],hf( 0));
one_cycle(7,0,1,2,3,4,5,6,k256[49],hf( 1));
one_cycle(6,7,0,1,2,3,4,5,k256[50],hf( 2));
one_cycle(5,6,7,0,1,2,3,4,k256[51],hf( 3));
one_cycle(4,5,6,7,0,1,2,3,k256[52],hf( 4));
one_cycle(3,4,5,6,7,0,1,2,k256[53],hf( 5));
one_cycle(2,3,4,5,6,7,0,1,k256[54],hf( 6));
one_cycle(1,2,3,4,5,6,7,0,k256[55],hf( 7));
one_cycle(0,1,2,3,4,5,6,7,k256[56],hf( 8));
one_cycle(7,0,1,2,3,4,5,6,k256[57],hf( 9));
one_cycle(6,7,0,1,2,3,4,5,k256[58],hf(10));
one_cycle(5,6,7,0,1,2,3,4,k256[59],hf(11));
one_cycle(4,5,6,7,0,1,2,3,k256[60],hf(12));
one_cycle(3,4,5,6,7,0,1,2,k256[61],hf(13));
one_cycle(2,3,4,5,6,7,0,1,k256[62],hf(14));
one_cycle(1,2,3,4,5,6,7,0,k256[63],hf(15));
ctx->hash[0] += v0; ctx->hash[1] += v1;
ctx->hash[2] += v2; ctx->hash[3] += v3;
ctx->hash[4] += v4; ctx->hash[5] += v5;
ctx->hash[6] += v6; ctx->hash[7] += v7;
#endif
}
/* SHA256 hash data in an array of bytes into hash buffer */
/* and call the hash_compile function as required. */
VOID_RETURN sha256_hash(const unsigned char data[], unsigned long len, sha256_ctx ctx[1])
{ uint32_t pos = (uint32_t)((ctx->count[0] >> 3) & SHA256_MASK);
const unsigned char *sp = data;
unsigned char *w = (unsigned char*)ctx->wbuf;
#if SHA2_BITS == 1
uint32_t ofs = (ctx->count[0] & 7);
#else
len <<= 3;
#endif
if((ctx->count[0] += len) < len)
++(ctx->count[1]);
#if SHA2_BITS == 1
if(ofs) /* if not on a byte boundary */
{
if(ofs + len < 8) /* if no added bytes are needed */
{
w[pos] |= (*sp >> ofs);
}
else /* otherwise and add bytes */
{ unsigned char part = w[pos];
while((int)(ofs + (len -= 8)) >= 0)
{
w[pos++] = part | (*sp >> ofs);
part = *sp++ << (8 - ofs);
if(pos == SHA256_BLOCK_SIZE)
{
bsw_32(w, SHA256_BLOCK_SIZE >> 2);
sha256_compile(ctx); pos = 0;
}
}
w[pos] = part;
}
}
else /* data is byte aligned */
#endif
{ uint32_t space = SHA256_BLOCK_SIZE - pos;
while(len >= (space << 3))
{
memcpy(w + pos, sp, space);
bsw_32(w, SHA256_BLOCK_SIZE >> 2);
sha256_compile(ctx);
sp += space; len -= (space << 3);
space = SHA256_BLOCK_SIZE; pos = 0;
}
memcpy(w + pos, sp, (len + 7 * SHA2_BITS) >> 3);
}
}
/* SHA256 Final padding and digest calculation */
static void sha_end1(unsigned char hval[], sha256_ctx ctx[1], const unsigned int hlen)
{ uint32_t i = (uint32_t)((ctx->count[0] >> 3) & SHA256_MASK), m1;
/* put bytes in the buffer in an order in which references to */
/* 32-bit words will put bytes with lower addresses into the */
/* top of 32 bit words on BOTH big and little endian machines */
bsw_32(ctx->wbuf, (i + 3 + SHA2_BITS) >> 2)
/* we now need to mask valid bytes and add the padding which is */
/* a single 1 bit and as many zero bits as necessary. Note that */
/* we can always add the first padding byte here because the */
/* buffer always has at least one empty slot */
m1 = (unsigned char)0x80 >> (ctx->count[0] & 7);
ctx->wbuf[i >> 2] &= ((0xffffff00 | (~m1 + 1)) << 8 * (~i & 3));
ctx->wbuf[i >> 2] |= (m1 << 8 * (~i & 3));
/* we need 9 or more empty positions, one for the padding byte */
/* (above) and eight for the length count. If there is not */
/* enough space pad and empty the buffer */
if(i > SHA256_BLOCK_SIZE - 9)
{
if(i < 60) ctx->wbuf[15] = 0;
sha256_compile(ctx);
i = 0;
}
else /* compute a word index for the empty buffer positions */
i = (i >> 2) + 1;
while(i < 14) /* and zero pad all but last two positions */
ctx->wbuf[i++] = 0;
/* the following 32-bit length fields are assembled in the */
/* wrong byte order on little endian machines but this is */
/* corrected later since they are only ever used as 32-bit */
/* word values. */
ctx->wbuf[14] = ctx->count[1];
ctx->wbuf[15] = ctx->count[0];
sha256_compile(ctx);
/* extract the hash value as bytes in case the hash buffer is */
/* mislaigned for 32-bit words */
for(i = 0; i < hlen; ++i)
hval[i] = ((ctx->hash[i >> 2] >> (8 * (~i & 3))) & 0xff);
}
#endif
#if defined(SHA_224)
const uint32_t i224[8] =
{
0xc1059ed8ul, 0x367cd507ul, 0x3070dd17ul, 0xf70e5939ul,
0xffc00b31ul, 0x68581511ul, 0x64f98fa7ul, 0xbefa4fa4ul
};
VOID_RETURN sha224_begin(sha224_ctx ctx[1])
{
memset(ctx, 0, sizeof(sha224_ctx));
memcpy(ctx->hash, i224, sizeof(ctx->hash));
}
VOID_RETURN sha224_end(unsigned char hval[], sha224_ctx ctx[1])
{
sha_end1(hval, ctx, SHA224_DIGEST_SIZE);
}
VOID_RETURN sha224(unsigned char hval[], const unsigned char data[], unsigned long len)
{ sha224_ctx cx[1];
sha224_begin(cx);
sha224_hash(data, len, cx);
sha_end1(hval, cx, SHA224_DIGEST_SIZE);
}
#endif
#if defined(SHA_256)
const uint32_t i256[8] =
{
0x6a09e667ul, 0xbb67ae85ul, 0x3c6ef372ul, 0xa54ff53aul,
0x510e527ful, 0x9b05688cul, 0x1f83d9abul, 0x5be0cd19ul
};
VOID_RETURN sha256_begin(sha256_ctx ctx[1])
{
memset(ctx, 0, sizeof(sha256_ctx));
memcpy(ctx->hash, i256, sizeof(ctx->hash));
}
VOID_RETURN sha256_end(unsigned char hval[], sha256_ctx ctx[1])
{
sha_end1(hval, ctx, SHA256_DIGEST_SIZE);
}
VOID_RETURN sha256(unsigned char hval[], const unsigned char data[], unsigned long len)
{ sha256_ctx cx[1];
sha256_begin(cx);
sha256_hash(data, len, cx);
sha_end1(hval, cx, SHA256_DIGEST_SIZE);
}
#endif
#if defined(SHA_384) || defined(SHA_512)
#define SHA512_MASK (SHA512_BLOCK_SIZE - 1)
#define rotr64(x,n) (((x) >> n) | ((x) << (64 - n)))
#if !defined(bswap_64)
#define bswap_64(x) (((uint64_t)(bswap_32((uint32_t)(x)))) << 32 | bswap_32((uint32_t)((x) >> 32)))
#endif
#if defined(SWAP_BYTES)
#define bsw_64(p,n) \
{ int _i = (n); while(_i--) ((uint64_t*)p)[_i] = bswap_64(((uint64_t*)p)[_i]); }
#else
#define bsw_64(p,n)
#endif
/* SHA512 mixing function definitions */
#ifdef s_0
# undef s_0
# undef s_1
# undef g_0
# undef g_1
# undef k_0
#endif
#define s_0(x) (rotr64((x), 28) ^ rotr64((x), 34) ^ rotr64((x), 39))
#define s_1(x) (rotr64((x), 14) ^ rotr64((x), 18) ^ rotr64((x), 41))
#define g_0(x) (rotr64((x), 1) ^ rotr64((x), 8) ^ ((x) >> 7))
#define g_1(x) (rotr64((x), 19) ^ rotr64((x), 61) ^ ((x) >> 6))
#define k_0 k512
/* SHA384/SHA512 mixing data */
const uint64_t k512[80] =
{
li_64(428a2f98d728ae22), li_64(7137449123ef65cd),
li_64(b5c0fbcfec4d3b2f), li_64(e9b5dba58189dbbc),
li_64(3956c25bf348b538), li_64(59f111f1b605d019),
li_64(923f82a4af194f9b), li_64(ab1c5ed5da6d8118),
li_64(d807aa98a3030242), li_64(12835b0145706fbe),
li_64(243185be4ee4b28c), li_64(550c7dc3d5ffb4e2),
li_64(72be5d74f27b896f), li_64(80deb1fe3b1696b1),
li_64(9bdc06a725c71235), li_64(c19bf174cf692694),
li_64(e49b69c19ef14ad2), li_64(efbe4786384f25e3),
li_64(0fc19dc68b8cd5b5), li_64(240ca1cc77ac9c65),
li_64(2de92c6f592b0275), li_64(4a7484aa6ea6e483),
li_64(5cb0a9dcbd41fbd4), li_64(76f988da831153b5),
li_64(983e5152ee66dfab), li_64(a831c66d2db43210),
li_64(b00327c898fb213f), li_64(bf597fc7beef0ee4),
li_64(c6e00bf33da88fc2), li_64(d5a79147930aa725),
li_64(06ca6351e003826f), li_64(142929670a0e6e70),
li_64(27b70a8546d22ffc), li_64(2e1b21385c26c926),
li_64(4d2c6dfc5ac42aed), li_64(53380d139d95b3df),
li_64(650a73548baf63de), li_64(766a0abb3c77b2a8),
li_64(81c2c92e47edaee6), li_64(92722c851482353b),
li_64(a2bfe8a14cf10364), li_64(a81a664bbc423001),
li_64(c24b8b70d0f89791), li_64(c76c51a30654be30),
li_64(d192e819d6ef5218), li_64(d69906245565a910),
li_64(f40e35855771202a), li_64(106aa07032bbd1b8),
li_64(19a4c116b8d2d0c8), li_64(1e376c085141ab53),
li_64(2748774cdf8eeb99), li_64(34b0bcb5e19b48a8),
li_64(391c0cb3c5c95a63), li_64(4ed8aa4ae3418acb),
li_64(5b9cca4f7763e373), li_64(682e6ff3d6b2b8a3),
li_64(748f82ee5defb2fc), li_64(78a5636f43172f60),
li_64(84c87814a1f0ab72), li_64(8cc702081a6439ec),
li_64(90befffa23631e28), li_64(a4506cebde82bde9),
li_64(bef9a3f7b2c67915), li_64(c67178f2e372532b),
li_64(ca273eceea26619c), li_64(d186b8c721c0c207),
li_64(eada7dd6cde0eb1e), li_64(f57d4f7fee6ed178),
li_64(06f067aa72176fba), li_64(0a637dc5a2c898a6),
li_64(113f9804bef90dae), li_64(1b710b35131c471b),
li_64(28db77f523047d84), li_64(32caab7b40c72493),
li_64(3c9ebe0a15c9bebc), li_64(431d67c49c100d4c),
li_64(4cc5d4becb3e42b6), li_64(597f299cfc657e2a),
li_64(5fcb6fab3ad6faec), li_64(6c44198c4a475817)
};
/* Compile 128 bytes of hash data into SHA384/512 digest */
/* NOTE: this routine assumes that the byte order in the */
/* ctx->wbuf[] at this point is such that low address bytes */
/* in the ORIGINAL byte stream will go into the high end of */
/* words on BOTH big and little endian systems */
VOID_RETURN sha512_compile(sha512_ctx ctx[1])
{ uint64_t v[8], *p = ctx->wbuf;
uint32_t j;
memcpy(v, ctx->hash, sizeof(ctx->hash));
for(j = 0; j < 80; j += 16)
{
v_cycle( 0, j); v_cycle( 1, j);
v_cycle( 2, j); v_cycle( 3, j);
v_cycle( 4, j); v_cycle( 5, j);
v_cycle( 6, j); v_cycle( 7, j);
v_cycle( 8, j); v_cycle( 9, j);
v_cycle(10, j); v_cycle(11, j);
v_cycle(12, j); v_cycle(13, j);
v_cycle(14, j); v_cycle(15, j);
}
ctx->hash[0] += v[0]; ctx->hash[1] += v[1];
ctx->hash[2] += v[2]; ctx->hash[3] += v[3];
ctx->hash[4] += v[4]; ctx->hash[5] += v[5];
ctx->hash[6] += v[6]; ctx->hash[7] += v[7];
}
/* Compile 128 bytes of hash data into SHA256 digest value */
/* NOTE: this routine assumes that the byte order in the */
/* ctx->wbuf[] at this point is in such an order that low */
/* address bytes in the ORIGINAL byte stream placed in this */
/* buffer will now go to the high end of words on BOTH big */
/* and little endian systems */
VOID_RETURN sha512_hash(const unsigned char data[], unsigned long len, sha512_ctx ctx[1])
{ uint32_t pos = (uint32_t)(ctx->count[0] >> 3) & SHA512_MASK;
const unsigned char *sp = data;
unsigned char *w = (unsigned char*)ctx->wbuf;
#if SHA2_BITS == 1
uint32_t ofs = (ctx->count[0] & 7);
#else
len <<= 3;
#endif
if((ctx->count[0] += len) < len)
++(ctx->count[1]);
#if SHA2_BITS == 1
if(ofs) /* if not on a byte boundary */
{
if(ofs + len < 8) /* if no added bytes are needed */
{
w[pos] |= (*sp >> ofs);
}
else /* otherwise and add bytes */
{ unsigned char part = w[pos];
while((int)(ofs + (len -= 8)) >= 0)
{
w[pos++] = part | (*sp >> ofs);
part = *sp++ << (8 - ofs);
if(pos == SHA512_BLOCK_SIZE)
{
bsw_64(w, SHA512_BLOCK_SIZE >> 3);
sha512_compile(ctx); pos = 0;
}
}
w[pos] = part;
}
}
else /* data is byte aligned */
#endif
{ uint32_t space = SHA512_BLOCK_SIZE - pos;
while(len >= (space << 3))
{
memcpy(w + pos, sp, space);
bsw_64(w, SHA512_BLOCK_SIZE >> 3);
sha512_compile(ctx);
sp += space; len -= (space << 3);
space = SHA512_BLOCK_SIZE; pos = 0;
}
memcpy(w + pos, sp, (len + 7 * SHA2_BITS) >> 3);
}
}
/* SHA384/512 Final padding and digest calculation */
static void sha_end2(unsigned char hval[], sha512_ctx ctx[1], const unsigned int hlen)
{ uint32_t i = (uint32_t)((ctx->count[0] >> 3) & SHA512_MASK);
uint64_t m1;
/* put bytes in the buffer in an order in which references to */
/* 32-bit words will put bytes with lower addresses into the */
/* top of 32 bit words on BOTH big and little endian machines */
bsw_64(ctx->wbuf, (i + 7 + SHA2_BITS) >> 3);
/* we now need to mask valid bytes and add the padding which is */
/* a single 1 bit and as many zero bits as necessary. Note that */
/* we can always add the first padding byte here because the */
/* buffer always has at least one empty slot */
m1 = (unsigned char)0x80 >> (ctx->count[0] & 7);
ctx->wbuf[i >> 3] &= ((li_64(ffffffffffffff00) | (~m1 + 1)) << 8 * (~i & 7));
ctx->wbuf[i >> 3] |= (m1 << 8 * (~i & 7));
/* we need 17 or more empty byte positions, one for the padding */
/* byte (above) and sixteen for the length count. If there is */
/* not enough space pad and empty the buffer */
if(i > SHA512_BLOCK_SIZE - 17)
{
if(i < 120) ctx->wbuf[15] = 0;
sha512_compile(ctx);
i = 0;
}
else
i = (i >> 3) + 1;
while(i < 14)
ctx->wbuf[i++] = 0;
/* the following 64-bit length fields are assembled in the */
/* wrong byte order on little endian machines but this is */
/* corrected later since they are only ever used as 64-bit */
/* word values. */
ctx->wbuf[14] = ctx->count[1];
ctx->wbuf[15] = ctx->count[0];
sha512_compile(ctx);
/* extract the hash value as bytes in case the hash buffer is */
/* misaligned for 32-bit words */
for(i = 0; i < hlen; ++i)
hval[i] = ((ctx->hash[i >> 3] >> (8 * (~i & 7))) & 0xff);
}
#endif
#if defined(SHA_384)
/* SHA384 initialisation data */
const uint64_t i384[80] =
{
li_64(cbbb9d5dc1059ed8), li_64(629a292a367cd507),
li_64(9159015a3070dd17), li_64(152fecd8f70e5939),
li_64(67332667ffc00b31), li_64(8eb44a8768581511),
li_64(db0c2e0d64f98fa7), li_64(47b5481dbefa4fa4)
};
VOID_RETURN sha384_begin(sha384_ctx ctx[1])
{
memset(ctx, 0, sizeof(sha384_ctx));
memcpy(ctx->hash, i384, sizeof(ctx->hash));
}
VOID_RETURN sha384_end(unsigned char hval[], sha384_ctx ctx[1])
{
sha_end2(hval, ctx, SHA384_DIGEST_SIZE);
}
VOID_RETURN sha384(unsigned char hval[], const unsigned char data[], unsigned long len)
{ sha384_ctx cx[1];
sha384_begin(cx);
sha384_hash(data, len, cx);
sha_end2(hval, cx, SHA384_DIGEST_SIZE);
}
#endif
#if defined(SHA_512)
/* SHA512 initialisation data */
static const uint64_t i512[SHA512_DIGEST_SIZE >> 3] =
{
li_64(6a09e667f3bcc908), li_64(bb67ae8584caa73b),
li_64(3c6ef372fe94f82b), li_64(a54ff53a5f1d36f1),
li_64(510e527fade682d1), li_64(9b05688c2b3e6c1f),
li_64(1f83d9abfb41bd6b), li_64(5be0cd19137e2179)
};
/* FIPS PUB 180-4: SHA-512/256 */
static const uint64_t i512_256[SHA512_DIGEST_SIZE >> 3] =
{
li_64(22312194fc2bf72c), li_64(9f555fa3c84c64c2),
li_64(2393b86b6f53b151), li_64(963877195940eabd),
li_64(96283ee2a88effe3), li_64(be5e1e2553863992),
li_64(2b0199fc2c85b8aa), li_64(0eb72ddc81c52ca2),
};
/* FIPS PUB 180-4: SHA-512/224 */
static const uint64_t i512_224[SHA512_DIGEST_SIZE >> 3] =
{
li_64(8c3d37c819544da2), li_64(73e1996689dcd4d6),
li_64(1dfab7ae32ff9c82), li_64(679dd514582f9fcf),
li_64(0f6d2b697bd44da8), li_64(77e36f7304c48942),
li_64(3f9d85a86a1d36c8), li_64(1112e6ad91d692a1),
};
/* FIPS PUB 180-4: SHA-512/192 (unsanctioned; facilitates using AES-192) */
static const uint64_t i512_192[SHA512_DIGEST_SIZE >> 3] =
{
li_64(010176140648b233), li_64(db92aeb1eebadd6f),
li_64(83a9e27aa1d5ea62), li_64(ec95f77eb609b4e1),
li_64(71a99185c75caefa), li_64(006e8f08baf32e3c),
li_64(6a2b21abd2db2aec), li_64(24926cdbd918a27f),
};
/* FIPS PUB 180-4: SHA-512/128 (unsanctioned; facilitates using AES-128) */
static const uint64_t i512_128[SHA512_DIGEST_SIZE >> 3] =
{
li_64(c953a21464c3e8cc), li_64(06cc9cfd166a34b5),
li_64(647e88dabf8b24ab), li_64(8513e4dc05a078ac),
li_64(7266fcfb7cba0534), li_64(854a78e2ecd19b93),
li_64(8618061711cec2dd), li_64(b20d8506efb929b1),
};
VOID_RETURN sha512_begin(sha512_ctx ctx[1])
{
memset(ctx, 0, sizeof(sha512_ctx));
memcpy(ctx->hash, i512, sizeof(ctx->hash));
}
VOID_RETURN sha512_256_begin(sha512_ctx ctx[1])
{
memset(ctx, 0, sizeof(sha512_ctx));
memcpy(ctx->hash, i512_256, sizeof(ctx->hash));
}
VOID_RETURN sha512_224_begin(sha512_ctx ctx[1])
{
memset(ctx, 0, sizeof(sha512_ctx));
memcpy(ctx->hash, i512_224, sizeof(ctx->hash));
}
VOID_RETURN sha512_192_begin(sha512_ctx ctx[1])
{
memset(ctx, 0, sizeof(sha512_ctx));
memcpy(ctx->hash, i512_192, sizeof(ctx->hash));
}
VOID_RETURN sha512_128_begin(sha512_ctx ctx[1])
{
memset(ctx, 0, sizeof(sha512_ctx));
memcpy(ctx->hash, i512_128, sizeof(ctx->hash));
}
VOID_RETURN sha512_end(unsigned char hval[], sha512_ctx ctx[1])
{
sha_end2(hval, ctx, SHA512_DIGEST_SIZE);
}
VOID_RETURN sha512_256_end(unsigned char hval[], sha512_ctx ctx[1])
{
sha_end2(hval, ctx, SHA512_256_DIGEST_SIZE);
}
VOID_RETURN sha512_224_end(unsigned char hval[], sha512_ctx ctx[1])
{
sha_end2(hval, ctx, SHA512_224_DIGEST_SIZE);
}
VOID_RETURN sha512_192_end(unsigned char hval[], sha512_ctx ctx[1])
{
sha_end2(hval, ctx, SHA512_192_DIGEST_SIZE);
}
VOID_RETURN sha512_128_end(unsigned char hval[], sha512_ctx ctx[1])
{
sha_end2(hval, ctx, SHA512_128_DIGEST_SIZE);
}
VOID_RETURN sha512(unsigned char hval[], const unsigned char data[], unsigned long len)
{ sha512_ctx cx[1];
sha512_begin(cx);
sha512_hash(data, len, cx);
sha512_end(hval, cx);
}
VOID_RETURN sha512_256(unsigned char hval[], const unsigned char data[], unsigned long len)
{ sha512_ctx cx[1];
sha512_256_begin(cx);
sha512_256_hash(data, len, cx);
sha512_256_end(hval, cx);
}
VOID_RETURN sha512_224(unsigned char hval[], const unsigned char data[], unsigned long len)
{ sha512_ctx cx[1];
sha512_224_begin(cx);
sha512_224_hash(data, len, cx);
sha512_224_end(hval, cx);
}
VOID_RETURN sha512_192(unsigned char hval[], const unsigned char data[], unsigned long len)
{ sha512_ctx cx[1];
sha512_192_begin(cx);
sha512_192_hash(data, len, cx);
sha512_192_end(hval, cx);
}
VOID_RETURN sha512_128(unsigned char hval[], const unsigned char data[], unsigned long len)
{ sha512_ctx cx[1];
sha512_128_begin(cx);
sha512_128_hash(data, len, cx);
sha512_128_end(hval, cx);
}
#endif
#if defined(SHA_2)
#define CTX_224(x) ((x)->uu->ctx256)
#define CTX_256(x) ((x)->uu->ctx256)
#define CTX_384(x) ((x)->uu->ctx512)
#define CTX_512(x) ((x)->uu->ctx512)
/* SHA2 initialisation */
INT_RETURN sha2_begin(unsigned long len, sha2_ctx ctx[1])
{
switch(len)
{
#if defined(SHA_224)
case 224:
case 28: CTX_256(ctx)->count[0] = CTX_256(ctx)->count[1] = 0;
memcpy(CTX_256(ctx)->hash, i224, 32);
ctx->sha2_len = 28; return EXIT_SUCCESS;
#endif
#if defined(SHA_256)
case 256:
case 32: CTX_256(ctx)->count[0] = CTX_256(ctx)->count[1] = 0;
memcpy(CTX_256(ctx)->hash, i256, 32);
ctx->sha2_len = 32; return EXIT_SUCCESS;
#endif
#if defined(SHA_384)
case 384:
case 48: CTX_384(ctx)->count[0] = CTX_384(ctx)->count[1] = 0;
memcpy(CTX_384(ctx)->hash, i384, 64);
ctx->sha2_len = 48; return EXIT_SUCCESS;
#endif
#if defined(SHA_512)
case 512:
case 64: CTX_512(ctx)->count[0] = CTX_512(ctx)->count[1] = 0;
memcpy(CTX_512(ctx)->hash, i512, 64);
ctx->sha2_len = 64; return EXIT_SUCCESS;
#endif
default: return EXIT_FAILURE;
}
}
VOID_RETURN sha2_hash(const unsigned char data[], unsigned long len, sha2_ctx ctx[1])
{
switch(ctx->sha2_len)
{
#if defined(SHA_224)
case 28: sha224_hash(data, len, CTX_224(ctx)); return;
#endif
#if defined(SHA_256)
case 32: sha256_hash(data, len, CTX_256(ctx)); return;
#endif
#if defined(SHA_384)
case 48: sha384_hash(data, len, CTX_384(ctx)); return;
#endif
#if defined(SHA_512)
case 64: sha512_hash(data, len, CTX_512(ctx)); return;
#endif
}
}
VOID_RETURN sha2_end(unsigned char hval[], sha2_ctx ctx[1])
{
switch(ctx->sha2_len)
{
#if defined(SHA_224)
case 28: sha_end1(hval, CTX_224(ctx), SHA224_DIGEST_SIZE); return;
#endif
#if defined(SHA_256)
case 32: sha_end1(hval, CTX_256(ctx), SHA256_DIGEST_SIZE); return;
#endif
#if defined(SHA_384)
case 48: sha_end2(hval, CTX_384(ctx), SHA384_DIGEST_SIZE); return;
#endif
#if defined(SHA_512)
case 64: sha_end2(hval, CTX_512(ctx), SHA512_DIGEST_SIZE); return;
#endif
}
}
INT_RETURN sha2(unsigned char hval[], unsigned long size,
const unsigned char data[], unsigned long len)
{ sha2_ctx cx[1];
if(sha2_begin(size, cx) == EXIT_SUCCESS)
{
sha2_hash(data, len, cx); sha2_end(hval, cx); return EXIT_SUCCESS;
}
else
return EXIT_FAILURE;
}
#endif
#if defined(__cplusplus)
}
#endif
+183
View File
@@ -0,0 +1,183 @@
/*
---------------------------------------------------------------------------
Copyright (c) 1998-2010, Brian Gladman, Worcester, UK. All rights reserved.
The redistribution and use of this software (with or without changes)
is allowed without the payment of fees or royalties provided that:
source code distributions include the above copyright notice, this
list of conditions and the following disclaimer;
binary distributions include the above copyright notice, this list
of conditions and the following disclaimer in their documentation.
This software is provided 'as is' with no explicit or implied warranties
in respect of its operation, including, but not limited to, correctness
and fitness for purpose.
---------------------------------------------------------------------------
Issue Date: 20/12/2007
*/
#ifndef _SHA2_H
#define _SHA2_H
#include <stdlib.h>
/* define for bit or byte oriented SHA */
#if 1
# define SHA2_BITS 0 /* byte oriented */
#else
# define SHA2_BITS 1 /* bit oriented */
#endif
/* define the hash functions that you need */
/* define for 64-bit SHA384 and SHA512 */
#define SHA_64BIT
#define SHA_2 /* for dynamic hash length */
#define SHA_224
#define SHA_256
#ifdef SHA_64BIT
# define SHA_384
# define SHA_512
# define NEED_uint64_t
#endif
#define SHA2_MAX_DIGEST_SIZE 64
#define SHA2_MAX_BLOCK_SIZE 128
#include "brg_types.h"
#if defined(__cplusplus)
extern "C"
{
#endif
/* Note that the following function prototypes are the same */
/* for both the bit and byte oriented implementations. But */
/* the length fields are in bytes or bits as is appropriate */
/* for the version used. Bit sequences are arrays of bytes */
/* in which bit sequence indexes increase from the most to */
/* the least significant end of each byte. The value 'len' */
/* in sha<nnn>_hash for the byte oriented versions of SHA2 */
/* is limited to 2^29 bytes, but multiple calls will handle */
/* longer data blocks. */
#define SHA224_DIGEST_SIZE 28
#define SHA224_BLOCK_SIZE 64
#define SHA256_DIGEST_SIZE 32
#define SHA256_BLOCK_SIZE 64
/* type to hold the SHA256 (and SHA224) context */
typedef struct
{ uint32_t count[2];
uint32_t hash[SHA256_DIGEST_SIZE >> 2];
uint32_t wbuf[SHA256_BLOCK_SIZE >> 2];
} sha256_ctx;
typedef sha256_ctx sha224_ctx;
VOID_RETURN sha256_compile(sha256_ctx ctx[1]);
VOID_RETURN sha224_begin(sha224_ctx ctx[1]);
#define sha224_hash sha256_hash
VOID_RETURN sha224_end(unsigned char hval[], sha224_ctx ctx[1]);
VOID_RETURN sha224(unsigned char hval[], const unsigned char data[], unsigned long len);
VOID_RETURN sha256_begin(sha256_ctx ctx[1]);
VOID_RETURN sha256_hash(const unsigned char data[], unsigned long len, sha256_ctx ctx[1]);
VOID_RETURN sha256_end(unsigned char hval[], sha256_ctx ctx[1]);
VOID_RETURN sha256(unsigned char hval[], const unsigned char data[], unsigned long len);
#ifndef SHA_64BIT
typedef struct
{ union
{ sha256_ctx ctx256[1];
} uu[1];
uint32_t sha2_len;
} sha2_ctx;
#else
#define SHA384_DIGEST_SIZE 48
#define SHA384_BLOCK_SIZE 128
#define SHA512_DIGEST_SIZE 64
#define SHA512_BLOCK_SIZE 128
#define SHA512_128_DIGEST_SIZE 16
#define SHA512_128_BLOCK_SIZE SHA512_BLOCK_SIZE
#define SHA512_192_DIGEST_SIZE 24
#define SHA512_192_BLOCK_SIZE SHA512_BLOCK_SIZE
#define SHA512_224_DIGEST_SIZE 28
#define SHA512_224_BLOCK_SIZE SHA512_BLOCK_SIZE
#define SHA512_256_DIGEST_SIZE 32
#define SHA512_256_BLOCK_SIZE SHA512_BLOCK_SIZE
/* type to hold the SHA384 (and SHA512) context */
typedef struct
{ uint64_t count[2];
uint64_t hash[SHA512_DIGEST_SIZE >> 3];
uint64_t wbuf[SHA512_BLOCK_SIZE >> 3];
} sha512_ctx;
typedef sha512_ctx sha384_ctx;
typedef struct
{ union
{ sha256_ctx ctx256[1];
sha512_ctx ctx512[1];
} uu[1];
uint32_t sha2_len;
} sha2_ctx;
VOID_RETURN sha512_compile(sha512_ctx ctx[1]);
VOID_RETURN sha384_begin(sha384_ctx ctx[1]);
#define sha384_hash sha512_hash
VOID_RETURN sha384_end(unsigned char hval[], sha384_ctx ctx[1]);
VOID_RETURN sha384(unsigned char hval[], const unsigned char data[], unsigned long len);
VOID_RETURN sha512_begin(sha512_ctx ctx[1]);
VOID_RETURN sha512_hash(const unsigned char data[], unsigned long len, sha512_ctx ctx[1]);
VOID_RETURN sha512_end(unsigned char hval[], sha512_ctx ctx[1]);
VOID_RETURN sha512(unsigned char hval[], const unsigned char data[], unsigned long len);
VOID_RETURN sha512_256_begin(sha512_ctx ctx[1]);
#define sha512_256_hash sha512_hash
VOID_RETURN sha512_256_end(unsigned char hval[], sha512_ctx ctx[1]);
VOID_RETURN sha512_256(unsigned char hval[], const unsigned char data[], unsigned long len);
VOID_RETURN sha512_224_begin(sha512_ctx ctx[1]);
#define sha512_224_hash sha512_hash
VOID_RETURN sha512_224_end(unsigned char hval[], sha512_ctx ctx[1]);
VOID_RETURN sha512_224(unsigned char hval[], const unsigned char data[], unsigned long len);
VOID_RETURN sha512_192_begin(sha512_ctx ctx[1]);
#define sha512_192_hash sha512_hash
VOID_RETURN sha512_192_end(unsigned char hval[], sha512_ctx ctx[1]);
VOID_RETURN sha512_192(unsigned char hval[], const unsigned char data[], unsigned long len);
VOID_RETURN sha512_128_begin(sha512_ctx ctx[1]);
#define sha512_128_hash sha512_hash
VOID_RETURN sha512_128_end(unsigned char hval[], sha512_ctx ctx[1]);
VOID_RETURN sha512_128(unsigned char hval[], const unsigned char data[], unsigned long len);
INT_RETURN sha2_begin(unsigned long size, sha2_ctx ctx[1]);
VOID_RETURN sha2_hash(const unsigned char data[], unsigned long len, sha2_ctx ctx[1]);
VOID_RETURN sha2_end(unsigned char hval[], sha2_ctx ctx[1]);
INT_RETURN sha2(unsigned char hval[], unsigned long size, const unsigned char data[], unsigned long len);
#endif
#if defined(__cplusplus)
}
#endif
#endif
+2
View File
@@ -102,6 +102,7 @@ static
void* default_bzalloc ( void* opaque, Int32 items, Int32 size )
{
void* v = malloc ( items * size );
(void)opaque;
return v;
}
@@ -109,6 +110,7 @@ static
void default_bzfree ( void* opaque, void* addr )
{
if (addr != NULL) free ( addr );
(void)opaque;
}
+2 -6
View File
@@ -158,6 +158,7 @@ void generateMTFValues ( EState* s )
wr = 0;
zPend = 0;
yy[0] = 0;
for (i = 0; i < s->nInUse; i++) yy[i] = (UChar) i;
for (i = 0; i < s->nblock; i++) {
@@ -240,7 +241,7 @@ void sendMTFValues ( EState* s )
{
Int32 v, t, i, j, gs, ge, totc, bt, bc, iter;
Int32 nSelectors, alphaSize, minLen, maxLen, selCtr;
Int32 nGroups, nBytes;
Int32 nGroups;
/*--
UChar len [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
@@ -500,7 +501,6 @@ void sendMTFValues ( EState* s )
if (s->inUse[i * 16 + j]) inUse16[i] = True;
}
nBytes = s->numZ;
for (i = 0; i < 16; i++)
if (inUse16[i]) bsW(s,1,1); else bsW(s,1,0);
@@ -515,7 +515,6 @@ void sendMTFValues ( EState* s )
}
/*--- Now the selectors. ---*/
nBytes = s->numZ;
bsW ( s, 3, nGroups );
bsW ( s, 15, nSelectors );
for (i = 0; i < nSelectors; i++) {
@@ -526,8 +525,6 @@ void sendMTFValues ( EState* s )
VPrintf1( "selectors %d, ", s->numZ-nBytes );
/*--- Now the coding tables. ---*/
nBytes = s->numZ;
for (t = 0; t < nGroups; t++) {
Int32 curr = s->len[t][0];
bsW ( s, 5, curr );
@@ -542,7 +539,6 @@ void sendMTFValues ( EState* s )
VPrintf1 ( "code lengths %d, ", s->numZ-nBytes );
/*--- And finally, the block data proper ---*/
nBytes = s->numZ;
selCtr = 0;
gs = 0;
while (True) {
+1 -1
View File
@@ -287,7 +287,7 @@ Int32 BZ2_decompress ( DState* s )
GET_BITS(BZ_X_SELECTOR_1, nGroups, 3);
if (nGroups < 2 || nGroups > 6) RETURN(BZ_DATA_ERROR);
GET_BITS(BZ_X_SELECTOR_2, nSelectors, 15);
if (nSelectors < 1) RETURN(BZ_DATA_ERROR);
if (nSelectors < 1 || nSelectors > BZ_MAX_SELECTORS) RETURN(BZ_DATA_ERROR);
for (i = 0; i < nSelectors; i++) {
j = 0;
while (True) {
+3 -1
View File
@@ -115,7 +115,9 @@ void BZ2_hbMakeCodeLengths ( UChar *len,
for (i = 1; i <= alphaSize; i++) {
j = 0;
k = i;
while (parent[k] >= 0) { k = parent[k]; j++; }
while (k < (BZ_MAX_ALPHA_SIZE * 2) && parent[k] >= 0) {
k = parent[k]; j++;
}
len[i-1] = j;
if (j > maxLen) tooLong = True;
}
+8
View File
@@ -83,6 +83,8 @@ lzma_check_size(lzma_check type)
extern void
lzma_check_init(lzma_check_state *check, lzma_check type)
{
(void)check;
switch (type) {
case LZMA_CHECK_NONE:
break;
@@ -117,6 +119,10 @@ extern void
lzma_check_update(lzma_check_state *check, lzma_check type,
const uint8_t *buf, size_t size)
{
(void)check;
(void)buf;
(void)size;
switch (type) {
#ifdef HAVE_CHECK_CRC32
case LZMA_CHECK_CRC32:
@@ -147,6 +153,8 @@ lzma_check_update(lzma_check_state *check, lzma_check type,
extern void
lzma_check_finish(lzma_check_state *check, lzma_check type)
{
(void)check;
switch (type) {
#ifdef HAVE_CHECK_CRC32
case LZMA_CHECK_CRC32:
+1 -1
View File
@@ -20,7 +20,7 @@
#include "crc_macros.h"
// If you make any changes, do some bench marking! Seemingly unrelated
// If you make any changes, do some benchmarking! Seemingly unrelated
// changes can very easily ruin the performance (and very probably is
// very compiler dependent).
extern LZMA_API(uint32_t)
+4 -2
View File
@@ -163,7 +163,8 @@ static const lzma_filter_encoder encoders[] = {
static const lzma_filter_encoder *
encoder_find(lzma_vli id)
{
for (size_t i = 0; i < ARRAY_SIZE(encoders); ++i)
size_t i = 0;
for (i = 0; i < ARRAY_SIZE(encoders); ++i)
if (encoders[i].id == id)
return encoders + i;
@@ -182,8 +183,9 @@ extern uint64_t
lzma_mt_block_size(const lzma_filter *filters)
{
uint64_t max = 0;
size_t i = 0;
for (size_t i = 0; filters[i].id != LZMA_VLI_UNKNOWN; ++i) {
for (i = 0; filters[i].id != LZMA_VLI_UNKNOWN; ++i) {
const lzma_filter_encoder *const fe
= encoder_find(filters[i].id);
if (fe->block_size != NULL) {
+3 -2
View File
@@ -68,7 +68,8 @@ lzma_memcmplen(const uint8_t *buf1, const uint8_t *buf2,
const uint64_t x = *(const uint64_t *)(buf1 + len)
- *(const uint64_t *)(buf2 + len);
if (x != 0) {
# if defined(_M_X64) // MSVC or Intel C compiler on Windows
# if defined(_M_X64) && defined(_WIN32) \
&& (defined(_MSC_VER) || defined(__INTEL_COMPILER)) // MSVC or Intel C compiler on Windows
unsigned long tmp;
_BitScanForward64(&tmp, x);
len += (uint32_t)tmp >> 3;
@@ -106,7 +107,7 @@ lzma_memcmplen(const uint8_t *buf1, const uint8_t *buf2,
# if defined(__INTEL_COMPILER)
len += _bit_scan_forward(x);
# elif defined(_MSC_VER)
unsigned long tmp;
unsigned long tmp = 0;
_BitScanForward(&tmp, x);
len += tmp;
# else
+5 -5
View File
@@ -382,7 +382,7 @@ bsr32(uint32_t n)
#elif defined(_MSC_VER) && _MSC_VER >= 1400
// MSVC isn't supported by tuklib, but since this code exists,
// it doesn't hurt to have it here anyway.
uint32_t i;
uint32_t i = 0;
_BitScanReverse(&i, n);
return i;
@@ -427,14 +427,14 @@ clz32(uint32_t n)
return __builtin_clz(n);
#elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
uint32_t i;
uint32_t i = 0;
__asm__("bsrl %1, %0\n\t"
"xorl $31, %0"
: "=r" (i) : "rm" (n));
return i;
#elif defined(_MSC_VER) && _MSC_VER >= 1400
uint32_t i;
uint32_t i = 0;
_BitScanReverse(&i, n);
return i ^ 31U;
@@ -479,12 +479,12 @@ ctz32(uint32_t n)
return __builtin_ctz(n);
#elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
uint32_t i;
uint32_t i = 0;
__asm__("bsfl %1, %0" : "=r" (i) : "rm" (n));
return i;
#elif defined(_MSC_VER) && _MSC_VER >= 1400
uint32_t i;
uint32_t i = 0;
_BitScanForward(&i, n);
return i;
+23 -23
View File
@@ -5,7 +5,7 @@
/* #undef AC_APPLE_UNIVERSAL_BUILD */
/* How many MiB of RAM to assume if the real amount cannot be determined. */
#define ASSUME_RAM 128
/* #undef ASSUME_RAM 128 */
/* Define to 1 if translation of program messages to the user's native
language is requested. */
@@ -47,7 +47,7 @@
/* #undef HAVE_CHECK_SHA256 */
/* Define to 1 if you have the `clock_gettime' function. */
#define HAVE_CLOCK_GETTIME 1
/* #undef HAVE_CLOCK_GETTIME */
/* Define to 1 if you have the <CommonCrypto/CommonDigest.h> header file. */
/* #undef HAVE_COMMONCRYPTO_COMMONDIGEST_H */
@@ -58,11 +58,11 @@
/* Define to 1 if you have the declaration of `CLOCK_MONOTONIC', and to 0 if
you don't. */
#define HAVE_DECL_CLOCK_MONOTONIC 1
/* #undef HAVE_DECL_CLOCK_MONOTONIC */
/* Define to 1 if you have the declaration of `program_invocation_name', and
to 0 if you don't. */
#define HAVE_DECL_PROGRAM_INVOCATION_NAME 0
/* #undef HAVE_DECL_PROGRAM_INVOCATION_NAME */
/* Define to 1 if arm decoder is enabled. */
/* #undef HAVE_DECODER_ARM */
@@ -122,7 +122,7 @@
/* #undef HAVE_ENCODER_X86 */
/* Define to 1 if you have the <fcntl.h> header file. */
#define HAVE_FCNTL_H 1
/* #undef HAVE_FCNTL_H 1 */
/* Define to 1 if you have the `futimens' function. */
/* #undef HAVE_FUTIMENS */
@@ -134,10 +134,10 @@
/* #undef HAVE_FUTIMESAT */
/* Define to 1 if you have the <getopt.h> header file. */
#define HAVE_GETOPT_H 1
/* #undef HAVE_GETOPT_H */
/* Define to 1 if you have the `getopt_long' function. */
#define HAVE_GETOPT_LONG 1
/* #undef HAVE_GETOPT_LONG */
/* Define if the GNU gettext() function is already present or preinstalled. */
/* #undef HAVE_GETTEXT */
@@ -146,19 +146,19 @@
/* #undef HAVE_ICONV */
/* Define to 1 if you have the <immintrin.h> header file. */
#define HAVE_IMMINTRIN_H 1
/* #undef HAVE_IMMINTRIN_H */
/* Define to 1 if you have the <inttypes.h> header file. */
#define HAVE_INTTYPES_H 1
/* #undef HAVE_INTTYPES_H */
/* Define to 1 if you have the <limits.h> header file. */
#define HAVE_LIMITS_H 1
/* #undef HAVE_LIMITS_H */
/* Define to 1 if mbrtowc and mbstate_t are properly declared. */
#define HAVE_MBRTOWC 1
/* #undef HAVE_MBRTOWC 1 */
/* Define to 1 if you have the <memory.h> header file. */
#define HAVE_MEMORY_H 1
/* #undef HAVE_MEMORY_H */
/* Define to 1 to enable bt2 match finder. */
#define HAVE_MF_BT2 1
@@ -215,10 +215,10 @@
/* #undef HAVE_SMALL */
/* Define to 1 if stdbool.h conforms to C99. */
#define HAVE_STDBOOL_H 1
/* #undef HAVE_STDBOOL_H */
/* Define to 1 if you have the <stdint.h> header file. */
#define HAVE_STDINT_H 1
/* #undef HAVE_STDINT_H */
/* Define to 1 if you have the <stdlib.h> header file. */
#define HAVE_STDLIB_H 1
@@ -227,7 +227,7 @@
/* #undef HAVE_STRINGS_H */
/* Define to 1 if you have the <string.h> header file. */
#define HAVE_STRING_H 1
/* #undef HAVE_STRING_H */
/* Define to 1 if `st_atimensec' is a member of `struct stat'. */
/* #undef HAVE_STRUCT_STAT_ST_ATIMENSEC */
@@ -251,25 +251,25 @@
/* #undef HAVE_SYS_ENDIAN_H */
/* Define to 1 if you have the <sys/param.h> header file. */
#define HAVE_SYS_PARAM_H 1
/* #undef HAVE_SYS_PARAM_H */
/* Define to 1 if you have the <sys/stat.h> header file. */
#define HAVE_SYS_STAT_H 1
/* #undef HAVE_SYS_STAT_H */
/* Define to 1 if you have the <sys/time.h> header file. */
#define HAVE_SYS_TIME_H 1
/* #undef HAVE_SYS_TIME_H */
/* Define to 1 if you have the <sys/types.h> header file. */
#define HAVE_SYS_TYPES_H 1
/* #undef HAVE_SYS_TYPES_H */
/* Define to 1 if the system has the type `uintptr_t'. */
#define HAVE_UINTPTR_T 1
/* #undef HAVE_UINTPTR_T */
/* Define to 1 if you have the <unistd.h> header file. */
#define HAVE_UNISTD_H 1
/* #undef HAVE_UNISTD_H */
/* Define to 1 if you have the `utime' function. */
#define HAVE_UTIME 1
/* #undef HAVE_UTIME */
/* Define to 1 if you have the `utimes' function. */
/* #undef HAVE_UTIMES */
@@ -434,7 +434,7 @@
#endif
/* Number of bits in a file offset, on hosts where this is settable. */
#define _FILE_OFFSET_BITS 64
/* #undef _FILE_OFFSET_BITS 64 */
/* Define for large files, on AIX-style hosts. */
/* #undef _LARGE_FILES */
+5 -4
View File
@@ -33,8 +33,9 @@ lzma_mf_find(lzma_mf *mf, uint32_t *count_ptr, lzma_match *matches)
if (count > 0) {
#ifndef NDEBUG
uint32_t i = 0;
// Validate the matches.
for (uint32_t i = 0; i < count; ++i) {
for (i = 0; i < count; ++i) {
assert(matches[i].len <= mf->nice_len);
assert(matches[i].dist < mf->read_pos);
assert(memcmp(mf_ptr(mf) - 1,
@@ -108,14 +109,14 @@ static void
normalize(lzma_mf *mf)
{
assert(mf->read_pos + mf->offset == MUST_NORMALIZE_POS);
uint32_t i = 0;
// In future we may not want to touch the lowest bits, because there
// may be match finders that use larger resolution than one byte.
const uint32_t subvalue
= (MUST_NORMALIZE_POS - mf->cyclic_size);
// & (~(UINT32_C(1) << 10) - 1);
for (uint32_t i = 0; i < mf->hash_count; ++i) {
for (i = 0; i < mf->hash_count; ++i) {
// If the distance is greater than the dictionary size,
// we can simply mark the hash element as empty.
if (mf->hash[i] <= subvalue)
@@ -124,7 +125,7 @@ normalize(lzma_mf *mf)
mf->hash[i] -= subvalue;
}
for (uint32_t i = 0; i < mf->sons_count; ++i) {
for (i = 0; i < mf->sons_count; ++i) {
// Do the same for mf->son.
//
// NOTE: There may be uninitialized elements in mf->son.
+1 -3
View File
@@ -14,9 +14,7 @@
#ifndef LZMA_RANGE_COMMON_H
#define LZMA_RANGE_COMMON_H
#ifdef HAVE_CONFIG_H
# include "common.h"
#endif
#include "common.h"
///////////////
+249 -128
View File
@@ -1,22 +1,16 @@
/* minizip.c
Version 2.5.2, August 27, 2018
Version 2.8.3, January 26, 2019
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
Copyright (C) 2010-2019 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
Copyright (C) 1998-2010 Gilles Vollant
http://www.winimage.com/zLibDll/minizip.html
https://www.winimage.com/zLibDll/minizip.html
This program is distributed under the terms of the same license as zlib.
See the accompanying LICENSE file for the full text of the license.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <time.h>
#include <errno.h>
#include "mz.h"
#include "mz_os.h"
@@ -26,17 +20,56 @@
#include "mz_zip.h"
#include "mz_zip_rw.h"
#include <stdio.h> /* printf */
/***************************************************************************/
void minizip_banner(void)
typedef struct minizip_opt_s {
uint8_t include_path;
int16_t compress_level;
uint8_t compress_method;
uint8_t overwrite;
uint8_t append;
int64_t disk_size;
uint8_t zip_cd;
int32_t encoding;
uint8_t verbose;
uint8_t aes;
const char *cert_path;
const char *cert_pwd;
} minizip_opt;
/***************************************************************************/
int32_t minizip_banner(void);
int32_t minizip_help(void);
int32_t minizip_list(const char *path);
int32_t minizip_add_entry_cb(void *handle, void *userdata, mz_zip_file *file_info);
int32_t minizip_add_progress_cb(void *handle, void *userdata, mz_zip_file *file_info, int64_t position);
int32_t minizip_add_overwrite_cb(void *handle, void *userdata, const char *path);
int32_t minizip_add(const char *path, const char *password, minizip_opt *options, int32_t arg_count, const char **args);
int32_t minizip_extract_entry_cb(void *handle, void *userdata, mz_zip_file *file_info, const char *path);
int32_t minizip_extract_progress_cb(void *handle, void *userdata, mz_zip_file *file_info, int64_t position);
int32_t minizip_extract_overwrite_cb(void *handle, void *userdata, mz_zip_file *file_info, const char *path);
int32_t minizip_extract(const char *path, const char *pattern, const char *destination, const char *password, minizip_opt *options);
int32_t minizip_erase(const char *src_path, const char *target_path, int32_t arg_count, const char **args);
/***************************************************************************/
int32_t minizip_banner(void)
{
printf("Minizip %s - https://github.com/nmoinvaz/minizip\n", MZ_VERSION);
printf("---------------------------------------------------\n");
return MZ_OK;
}
void minizip_help(void)
int32_t minizip_help(void)
{
printf("Usage : minizip [-x -d dir|-l|-e] [-o] [-c] [-a] [-j] [-0 to -9] [-b|-m] [-k 512] [-p pwd] [-s] file.zip [files]\n\n" \
printf("Usage : minizip [-x -d dir|-l|-e] [-o] [-c codepage] [-a] [-j] [-0 to -9] [-b|-m] [-k 512] [-p pwd] [-s] file.zip [files]\n\n" \
" -x Extract files\n" \
" -l List files\n" \
" -d Destination directory\n" \
@@ -44,40 +77,23 @@ void minizip_help(void)
" -c File names use cp437 encoding\n" \
" -a Append to existing zip file\n" \
" -i Include full path of files\n" \
" -v Verbose info\n" \
" -0 Store only\n" \
" -1 Compress faster\n" \
" -9 Compress better\n" \
" -k Disk size in KB\n" \
" -p Encryption password\n");
#ifdef HAVE_AES
printf(" -s AES encryption\n");
#endif
#ifdef HAVE_BZIP2
printf(" -b BZIP2 compression\n");
#endif
#ifdef HAVE_LZMA
printf(" -m LZMA compression\n");
#endif
printf("\n");
" -z Zip central directory\n" \
" -p Encryption password\n" \
" -s AES encryption\n" \
" -h Pkcs12 certificate path\n" \
" -w Pkcs12 certificate password\n" \
" -b BZIP2 compression\n" \
" -m LZMA compression\n\n");
return MZ_OK;
}
/***************************************************************************/
typedef struct minizip_opt_s {
uint8_t include_path;
int16_t compress_level;
uint8_t compress_method;
uint8_t overwrite;
uint8_t append;
int64_t disk_size;
#ifdef HAVE_AES
uint8_t aes;
#endif
uint8_t legacy_encoding;
} minizip_opt;
/***************************************************************************/
int32_t minizip_list(const char *path)
{
mz_zip_file *file_info = NULL;
@@ -87,39 +103,38 @@ int32_t minizip_list(const char *path)
struct tm tmu_date;
const char *string_method = NULL;
char crypt = ' ';
void *reader = NULL;
mz_zip_reader_create(&reader);
err = mz_zip_reader_open_file(reader, path);
if (err != MZ_OK)
{
printf("Error %d opening zip file %s\n", err, path);
printf("Error %"PRId32" opening zip file %s\n", err, path);
mz_zip_reader_delete(&reader);
return err;
}
err = mz_zip_reader_goto_first_entry(reader);
if (err != MZ_OK && err != MZ_END_OF_LIST)
{
printf("Error %d going to first entry in zip file\n", err);
printf("Error %"PRId32" going to first entry in zip file\n", err);
mz_zip_reader_delete(&reader);
return err;
}
printf(" Length Method Size Attribs Ratio Date Time CRC-32 Name\n");
printf(" ------ ------ ---- ------- ----- ---- ---- ------ ----\n");
printf(" Packed Unpacked Ratio Method Attribs Date Time CRC-32 Name\n");
printf(" ------ -------- ----- ------ ------- ---- ---- ------ ----\n");
/* Enumerate all entries in the zip */
do
{
err = mz_zip_reader_entry_get_info(reader, &file_info);
if (err != MZ_OK)
{
printf("Error %d getting entry info in zip file\n", err);
printf("Error %"PRId32" getting entry info in zip file\n", err);
break;
}
@@ -127,9 +142,11 @@ int32_t minizip_list(const char *path)
if (file_info->uncompressed_size > 0)
ratio = (uint32_t)((file_info->compressed_size * 100) / file_info->uncompressed_size);
// Display a '*' if the file is encrypted
/* Display a '*' if the file is encrypted */
if (file_info->flag & MZ_ZIP_FLAG_ENCRYPTED)
crypt = '*';
else
crypt = ' ';
switch (file_info->compression_method)
{
@@ -143,7 +160,7 @@ int32_t minizip_list(const char *path)
else if (level == 1)
string_method = "Defl:X";
else if ((level == 2) || (level == 3))
string_method = "Defl:F"; // 2: fast , 3: extra fast
string_method = "Defl:F"; /* 2: fast , 3: extra fast */
else
string_method = "Defl:?";
break;
@@ -154,15 +171,16 @@ int32_t minizip_list(const char *path)
string_method = "LZMA";
break;
default:
string_method = "Unknwn";
string_method = "?";
}
mz_zip_time_t_to_tm(file_info->modified_date, &tmu_date);
printf(" %7"PRIu64" %6s%c %7"PRIu64" %8"PRIx32" %3"PRIu32"%% %2.2"PRIu32"-%2.2"PRIu32\
"-%2.2"PRIu32" %2.2"PRIu32":%2.2"PRIu32" %8.8"PRIx32" %s\n",
file_info->uncompressed_size, string_method, crypt,
file_info->compressed_size, file_info->external_fa, ratio,
/* Print entry information */
printf("%12"PRId64" %12"PRId64" %3"PRIu32"%% %6s%c %8"PRIx32" %2.2"PRIu32\
"-%2.2"PRIu32"-%2.2"PRIu32" %2.2"PRIu32":%2.2"PRIu32" %8.8"PRIx32" %s\n",
file_info->compressed_size, file_info->uncompressed_size, ratio,
string_method, crypt, file_info->external_fa,
(uint32_t)tmu_date.tm_mon + 1, (uint32_t)tmu_date.tm_mday,
(uint32_t)tmu_date.tm_year % 100,
(uint32_t)tmu_date.tm_hour, (uint32_t)tmu_date.tm_min,
@@ -172,7 +190,7 @@ int32_t minizip_list(const char *path)
if (err != MZ_OK && err != MZ_END_OF_LIST)
{
printf("Error %d going to next entry in zip file\n", err);
printf("Error %"PRId32" going to next entry in zip file\n", err);
break;
}
}
@@ -190,15 +208,22 @@ int32_t minizip_list(const char *path)
int32_t minizip_add_entry_cb(void *handle, void *userdata, mz_zip_file *file_info)
{
MZ_UNUSED(handle);
MZ_UNUSED(userdata);
/* Print the current file we are trying to compress */
printf("Adding %s\n", file_info->filename);
return MZ_OK;
}
int32_t minizip_add_progress_cb(void *handle, void *userdata, mz_zip_file *file_info, uint64_t position)
int32_t minizip_add_progress_cb(void *handle, void *userdata, mz_zip_file *file_info, int64_t position)
{
minizip_opt *options = (minizip_opt *)userdata;
double progress = 0;
uint8_t raw = 0;
MZ_UNUSED(userdata);
mz_zip_writer_get_raw(handle, &raw);
if (raw && file_info->compressed_size > 0)
@@ -206,17 +231,22 @@ int32_t minizip_add_progress_cb(void *handle, void *userdata, mz_zip_file *file_
else if (!raw && file_info->uncompressed_size > 0)
progress = ((double)position / file_info->uncompressed_size) * 100;
printf("%s - %lld / %lld (%.02f%%)\n", file_info->filename, position, file_info->uncompressed_size, progress);
/* Print the progress of the current compress operation */
if (options->verbose)
printf("%s - %"PRId64" / %"PRId64" (%.02f%%)\n", file_info->filename, position,
file_info->uncompressed_size, progress);
return MZ_OK;
}
int32_t minizip_add_overwrite_cb(void *handle, void *userdata, const char *path)
{
minizip_opt *options = (minizip_opt *)userdata;
MZ_UNUSED(handle);
if (options->overwrite == 0)
{
// If ask the user what to do because append and overwrite args not set
/* If ask the user what to do because append and overwrite args not set */
char rep = 0;
do
{
@@ -255,12 +285,18 @@ int32_t minizip_add(const char *path, const char *password, minizip_opt *options
printf("Archive %s\n", path);
/* Create zip writer */
mz_zip_writer_create(&writer);
mz_zip_writer_set_password(writer, password);
mz_zip_writer_set_compress_method(writer, options->compress_method);
mz_zip_writer_set_compress_level(writer, options->compress_level);
mz_zip_writer_set_overwrite_cb(writer, options, minizip_add_overwrite_cb);
mz_zip_writer_set_comment(writer, "xyz");
mz_zip_writer_set_progress_cb(writer, options, minizip_add_progress_cb);
mz_zip_writer_set_entry_cb(writer, options, minizip_add_entry_cb);
mz_zip_writer_set_zip_cd(writer, options->zip_cd);
if (options->cert_path != NULL)
mz_zip_writer_set_certificate(writer, options->cert_path, options->cert_pwd);
err = mz_zip_writer_open_file(writer, path, options->disk_size, options->append);
@@ -270,20 +306,21 @@ int32_t minizip_add(const char *path, const char *password, minizip_opt *options
{
filename_in_zip = args[i];
/* Add file system path to zip */
err = mz_zip_writer_add_path(writer, filename_in_zip, NULL, options->include_path, 1);
if (err != MZ_OK)
printf("Error %d adding path to zip %s\n", err, filename_in_zip);
printf("Error %"PRId32" adding path to zip %s\n", err, filename_in_zip);
}
}
else
{
printf("Error %d opening zip for writing\n", err);
printf("Error %"PRId32" opening zip for writing\n", err);
}
err_close = mz_zip_writer_close(writer);
if (err_close != MZ_OK)
{
printf("Error %d closing zip for writing %s\n", err_close, path);
printf("Error %"PRId32" closing zip for writing %s\n", err_close, path);
err = err_close;
}
@@ -295,14 +332,22 @@ int32_t minizip_add(const char *path, const char *password, minizip_opt *options
int32_t minizip_extract_entry_cb(void *handle, void *userdata, mz_zip_file *file_info, const char *path)
{
MZ_UNUSED(handle);
MZ_UNUSED(userdata);
MZ_UNUSED(path);
/* Print the current entry extracting */
printf("Extracting %s\n", file_info->filename);
return MZ_OK;
}
int32_t minizip_extract_progress_cb(void *handle, void *userdata, mz_zip_file *file_info, uint64_t position)
int32_t minizip_extract_progress_cb(void *handle, void *userdata, mz_zip_file *file_info, int64_t position)
{
minizip_opt *options = (minizip_opt *)userdata;
double progress = 0;
uint8_t raw = 0;
MZ_UNUSED(userdata);
mz_zip_reader_get_raw(handle, &raw);
@@ -311,14 +356,22 @@ int32_t minizip_extract_progress_cb(void *handle, void *userdata, mz_zip_file *f
else if (!raw && file_info->uncompressed_size > 0)
progress = ((double)position / file_info->uncompressed_size) * 100;
printf("%s - %lld / %lld (%.02f%%)\n", file_info->filename, position, file_info->uncompressed_size, progress);
return MZ_OK;
/* Print the progress of the current extraction */
if (options->verbose)
printf("%s - %"PRId64" / %"PRId64" (%.02f%%)\n", file_info->filename, position,
file_info->uncompressed_size, progress);
return MZ_OK;
}
int32_t minizip_extract_overwrite_cb(void *handle, void *userdata, mz_zip_file *file_info, const char *path)
{
minizip_opt *options = (minizip_opt *)userdata;
MZ_UNUSED(handle);
MZ_UNUSED(file_info);
/* Verify if we want to overwrite current entry on disk */
if (options->overwrite == 0)
{
char rep = 0;
@@ -351,10 +404,11 @@ int32_t minizip_extract(const char *path, const char *pattern, const char *desti
printf("Archive %s\n", path);
/* Create zip reader */
mz_zip_reader_create(&reader);
mz_zip_reader_set_pattern(reader, pattern, 1);
mz_zip_reader_set_password(reader, password);
mz_zip_reader_set_legacy_encoding(reader, options->legacy_encoding);
mz_zip_reader_set_encoding(reader, options->encoding);
mz_zip_reader_set_entry_cb(reader, options, minizip_extract_entry_cb);
mz_zip_reader_set_progress_cb(reader, options, minizip_extract_progress_cb);
mz_zip_reader_set_overwrite_cb(reader, options, minizip_extract_overwrite_cb);
@@ -363,20 +417,23 @@ int32_t minizip_extract(const char *path, const char *pattern, const char *desti
if (err != MZ_OK)
{
printf("Error %d opening zip file %s\n", err, path);
printf("Error %"PRId32" opening zip file %s\n", err, path);
}
else
{
/* Save all entries in zip file to destination directory */
err = mz_zip_reader_save_all(reader, destination);
if (err == MZ_END_OF_LIST && pattern != NULL)
printf("Files matching %s not found in zip file\n", pattern);
if (err != MZ_OK)
printf("Error %d saving zip entries to disk %s\n", err, path);
printf("Error %"PRId32" saving zip entries to disk %s\n", err, path);
}
err_close = mz_zip_reader_close(reader);
if (err_close != MZ_OK)
{
printf("Error %d closing zip for reading\n", err_close);
printf("Error %"PRId32" closing zip for reading\n", err_close);
err = err_close;
}
@@ -390,26 +447,42 @@ int32_t minizip_erase(const char *src_path, const char *target_path, int32_t arg
{
mz_zip_file *file_info = NULL;
const char *filename_in_zip = NULL;
const char *target_path_ptr = target_path;
void *reader = NULL;
void *writer = NULL;
int32_t skip = 0;
int32_t err = MZ_OK;
int32_t i = 0;
uint8_t zip_cd = 0;
char bak_path[256];
char tmp_path[256];
if (target_path == NULL)
{
/* Construct temporary zip name */
strncpy(tmp_path, src_path, sizeof(tmp_path) - 1);
tmp_path[sizeof(tmp_path) - 1] = 0;
strncat(tmp_path, ".tmp.zip", sizeof(tmp_path) - strlen(tmp_path) - 1);
target_path_ptr = tmp_path;
}
mz_zip_reader_create(&reader);
mz_zip_writer_create(&writer);
/* Open original zip file we want to erase an entry in */
err = mz_zip_reader_open_file(reader, src_path);
if (err != MZ_OK)
{
printf("Error %d opening zip for reading %s\n", err, src_path);
printf("Error %"PRId32" opening zip for reading %s\n", err, src_path);
mz_zip_reader_delete(&reader);
return err;
}
err = mz_zip_writer_open_file(writer, target_path, 0, 0);
/* Open temporary zip file */
err = mz_zip_writer_open_file(writer, target_path_ptr, 0, 0);
if (err != MZ_OK)
{
printf("Error %d opening zip for writing %s\n", err, target_path);
printf("Error %"PRId32" opening zip for writing %s\n", err, target_path_ptr);
mz_zip_reader_delete(&reader);
mz_zip_writer_delete(&writer);
return err;
@@ -418,44 +491,52 @@ int32_t minizip_erase(const char *src_path, const char *target_path, int32_t arg
err = mz_zip_reader_goto_first_entry(reader);
if (err != MZ_OK && err != MZ_END_OF_LIST)
printf("Error %d going to first entry in zip file\n", err);
printf("Error %"PRId32" going to first entry in zip file\n", err);
while (err == MZ_OK)
{
err = mz_zip_reader_entry_get_info(reader, &file_info);
if (err != MZ_OK)
{
printf("Error %d getting info from zip\n", err);
printf("Error %"PRId32" getting info from zip\n", err);
break;
}
for (i = 0; i < arg_count; i += 1)
/* Copy all entries from original zip file to temporary zip file
except the ones we don't want */
for (i = 0, skip = 0; i < arg_count; i += 1)
{
filename_in_zip = args[i];
if (mz_path_compare_wc(file_info->filename, filename_in_zip, 1) != MZ_OK)
{
printf("Copying %s\n", file_info->filename);
err = mz_zip_writer_copy_from_reader(writer, reader);
}
else
{
printf("Skipping %s\n", file_info->filename);
}
if (mz_path_compare_wc(file_info->filename, filename_in_zip, 1) == MZ_OK)
skip = 1;
}
if (skip)
{
printf("Skipping %s\n", file_info->filename);
}
else
{
printf("Copying %s\n", file_info->filename);
err = mz_zip_writer_copy_from_reader(writer, reader);
}
if (err != MZ_OK)
{
printf("Error %d copying entry into new zip\n", err);
printf("Error %"PRId32" copying entry into new zip\n", err);
break;
}
err = mz_zip_reader_goto_next_entry(reader);
if (err != MZ_OK && err != MZ_END_OF_LIST)
printf("Error %d going to next entry in zip file\n", err);
printf("Error %"PRId32" going to next entry in zip file\n", err);
}
mz_zip_reader_get_zip_cd(reader, &zip_cd);
mz_zip_writer_set_zip_cd(writer, zip_cd);
mz_zip_reader_close(reader);
mz_zip_reader_delete(&reader);
@@ -463,36 +544,42 @@ int32_t minizip_erase(const char *src_path, const char *target_path, int32_t arg
mz_zip_writer_delete(&writer);
if (err == MZ_END_OF_LIST)
{
if (target_path == NULL)
{
/* Swap original zip with temporary zip, backup old zip if possible */
strncpy(bak_path, src_path, sizeof(bak_path) - 1);
bak_path[sizeof(bak_path) - 1] = 0;
strncat(bak_path, ".bak", sizeof(bak_path) - strlen(bak_path) - 1);
if (mz_os_file_exists(bak_path) == MZ_OK)
mz_os_delete(bak_path);
if (mz_os_rename(src_path, bak_path) != MZ_OK)
printf("Error backing up zip before replacing %s\n", bak_path);
if (mz_os_rename(tmp_path, src_path) != MZ_OK)
printf("Error replacing zip with temp %s\n", tmp_path);
}
return MZ_OK;
}
return err;
}
/***************************************************************************/
#if !defined(MZ_NO_MAIN)
#if !defined(MZ_ZIP_NO_MAIN)
int main(int argc, const char *argv[])
{
minizip_opt options;
void *handle = NULL;
void *target_handle = NULL;
void *file_stream = NULL;
void *split_stream = NULL;
void *buf_stream = NULL;
void *tmp_file_stream = NULL;
void *tmp_split_stream = NULL;
void *tmp_buf_stream = NULL;
int64_t disk_size = 0;
int32_t path_arg = 0;
int32_t err_close = 0;
int32_t err = 0;
int32_t i = 0;
int16_t mode = 0;
uint8_t do_list = 0;
uint8_t do_extract = 0;
uint8_t do_erase = 0;
char bak_path[256];
char tmp_path[256];
const char *path = NULL;
const char *password = NULL;
const char *destination = NULL;
@@ -511,9 +598,10 @@ int main(int argc, const char *argv[])
options.compress_method = MZ_COMPRESS_METHOD_DEFLATE;
options.compress_level = MZ_COMPRESS_LEVEL_DEFAULT;
// Parse command line options
/* Parse command line options */
for (i = 1; i < argc; i += 1)
{
printf("%s ", argv[i]);
if (argv[i][0] == '-')
{
char c = argv[i][1];
@@ -529,46 +617,92 @@ int main(int argc, const char *argv[])
options.overwrite = 1;
else if ((c == 'i') || (c == 'I'))
options.include_path = 1;
else if ((c == 'z') || (c == 'Z'))
options.zip_cd = 1;
else if ((c == 'v') || (c == 'V'))
options.verbose = 1;
else if ((c >= '0') && (c <= '9'))
{
options.compress_level = (c - '0');
if (options.compress_level == 0)
options.compress_method = MZ_COMPRESS_METHOD_STORE;
}
#ifdef HAVE_BZIP2
else if ((c == 'b') || (c == 'B'))
#ifdef HAVE_BZIP2
options.compress_method = MZ_COMPRESS_METHOD_BZIP2;
#else
err = MZ_SUPPORT_ERROR;
#endif
#ifdef HAVE_LZMA
else if ((c == 'm') || (c == 'M'))
#ifdef HAVE_LZMA
options.compress_method = MZ_COMPRESS_METHOD_LZMA;
#else
err = MZ_SUPPORT_ERROR;
#endif
#ifdef HAVE_AES
else if ((c == 's') || (c == 'S'))
#ifdef HAVE_WZAES
options.aes = 1;
#else
err = MZ_SUPPORT_ERROR;
#endif
else if ((c == 'c') || (c == 'C'))
options.legacy_encoding = 1;
else if (((c == 'h') || (c == 'H')) && (i + 1 < argc))
{
#ifndef MZ_ZIP_NO_SIGNING
options.cert_path = argv[i + 1];
printf("%s ", argv[i + 1]);
#else
err = MZ_SUPPORT_ERROR;
#endif
i += 1;
}
else if (((c == 'w') || (c == 'W')) && (i + 1 < argc))
{
#ifndef MZ_ZIP_NO_SIGNING
options.cert_pwd = argv[i + 1];
printf("%s ", argv[i + 1]);
#else
err = MZ_SUPPORT_ERROR;
#endif
i += 1;
}
else if (((c == 'c') || (c == 'C')) && (i + 1 < argc))
{
options.encoding = (int32_t)atoi(argv[i + 1]);
i += 1;
}
else if (((c == 'k') || (c == 'K')) && (i + 1 < argc))
{
options.disk_size = atoi(argv[i + 1]) * 1024;
options.disk_size = (int64_t)atoi(argv[i + 1]) * 1024;
printf("%s ", argv[i + 1]);
i += 1;
}
else if (((c == 'd') || (c == 'D')) && (i + 1 < argc))
{
destination = argv[i + 1];
printf("%s ", argv[i + 1]);
i += 1;
}
else if (((c == 'p') || (c == 'P')) && (i + 1 < argc))
{
#ifndef MZ_ZIP_NO_ENCRYPTION
password = argv[i + 1];
printf("*** ");
#else
err = MZ_SUPPORT_ERROR;
#endif
i += 1;
}
}
else if (path_arg == 0)
path_arg = i;
}
printf("\n");
if (err == MZ_SUPPORT_ERROR)
{
printf("Feature not supported\n");
return err;
}
if (path_arg == 0)
{
@@ -576,10 +710,12 @@ int main(int argc, const char *argv[])
return 0;
}
path = argv[path_arg];
if (do_list)
{
/* List zip file contents */
err = minizip_list(path);
}
else if (do_extract)
@@ -587,36 +723,21 @@ int main(int argc, const char *argv[])
if (argc > path_arg + 1)
filename_to_extract = argv[path_arg + 1];
/* Extract zip file*/
err = minizip_extract(path, filename_to_extract, destination, password, &options);
}
else if (do_erase)
{
strncpy(tmp_path, path, sizeof(tmp_path));
strncat(tmp_path, ".tmp", sizeof(tmp_path) - strlen(tmp_path) - 1);
err = minizip_erase(path, tmp_path, argc - (path_arg + 1), &argv[path_arg + 1]);
/* Erase file from zip */
err = minizip_erase(path, NULL, argc - (path_arg + 1), &argv[path_arg + 1]);
}
else
{
/* Add files to zip */
err = minizip_add(path, password, &options, argc - (path_arg + 1), &argv[path_arg + 1]);
}
if (err == MZ_OK && do_erase)
{
// Swap zip with temporary zip, backup old zip if possible
strncpy(bak_path, path, sizeof(bak_path));
strncat(bak_path, ".bak", sizeof(bak_path) - strlen(bak_path) - 1);
if (mz_os_file_exists(bak_path) == MZ_OK)
mz_os_delete(bak_path);
if (mz_os_rename(path, bak_path) != MZ_OK)
printf("Error backing up zip before replacing %s\n", bak_path);
if (mz_os_rename(tmp_path, path) != MZ_OK)
printf("Error replacing zip with temp %s\n", tmp_path);
}
return err;
}
#endif
#endif
+159 -26
View File
@@ -1,8 +1,8 @@
/* mz.h -- Errors codes, zip flags and magic
Version 2.5.2, August 27, 2018
Version 2.8.3, January 26, 2019
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
Copyright (C) 2010-2019 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
This program is distributed under the terms of the same license as zlib.
@@ -12,24 +12,22 @@
#ifndef MZ_H
#define MZ_H
#ifdef __cplusplus
extern "C" {
#endif
/***************************************************************************/
// MZ_VERSION
#define MZ_VERSION ("2.5.2")
/* MZ_VERSION */
#define MZ_VERSION ("2.8.3")
/* MZ_ERROR */
#define MZ_OK (0) /* zlib */
#define MZ_STREAM_ERROR (-1) /* zlib */
#define MZ_DATA_ERROR (-3) /* zlib */
#define MZ_MEM_ERROR (-4) /* zlib */
#define MZ_BUF_ERROR (-5) /* zlib */
#define MZ_VERSION_ERROR (-6) /* zlib */
// MZ_ERROR
#define MZ_OK (0) // zlib
#define MZ_STREAM_ERROR (-1) // zlib
#define MZ_DATA_ERROR (-3) // zlib
#define MZ_MEM_ERROR (-4) // zlib
#define MZ_BUF_ERROR (-5) // zlib
#define MZ_VERSION_ERROR (-6) // zlib
#define MZ_END_OF_LIST (-100)
#define MZ_END_OF_STREAM (-101)
#define MZ_PARAM_ERROR (-102)
#define MZ_FORMAT_ERROR (-103)
#define MZ_INTERNAL_ERROR (-104)
@@ -38,8 +36,16 @@ extern "C" {
#define MZ_EXIST_ERROR (-107)
#define MZ_PASSWORD_ERROR (-108)
#define MZ_SUPPORT_ERROR (-109)
#define MZ_HASH_ERROR (-110)
#define MZ_OPEN_ERROR (-111)
#define MZ_CLOSE_ERROR (-112)
#define MZ_SEEK_ERROR (-113)
#define MZ_TELL_ERROR (-114)
#define MZ_READ_ERROR (-115)
#define MZ_WRITE_ERROR (-116)
#define MZ_SIGN_ERROR (-117)
// MZ_OPEN
/* MZ_OPEN */
#define MZ_OPEN_MODE_READ (0x01)
#define MZ_OPEN_MODE_WRITE (0x02)
#define MZ_OPEN_MODE_READWRITE (MZ_OPEN_MODE_READ | MZ_OPEN_MODE_WRITE)
@@ -47,12 +53,12 @@ extern "C" {
#define MZ_OPEN_MODE_CREATE (0x08)
#define MZ_OPEN_MODE_EXISTING (0x10)
// MZ_SEEK
/* MZ_SEEK */
#define MZ_SEEK_SET (0)
#define MZ_SEEK_CUR (1)
#define MZ_SEEK_END (2)
// MZ_COMPRESS
/* MZ_COMPRESS */
#define MZ_COMPRESS_METHOD_STORE (0)
#define MZ_COMPRESS_METHOD_DEFLATE (8)
#define MZ_COMPRESS_METHOD_BZIP2 (12)
@@ -64,7 +70,7 @@ extern "C" {
#define MZ_COMPRESS_LEVEL_NORMAL (6)
#define MZ_COMPRESS_LEVEL_BEST (9)
// MZ_ZIP
/* MZ_ZIP_FLAG */
#define MZ_ZIP_FLAG_ENCRYPTED (1 << 0)
#define MZ_ZIP_FLAG_LZMA_EOS_MARKER (1 << 1)
#define MZ_ZIP_FLAG_DEFLATE_MAX (1 << 1)
@@ -74,31 +80,60 @@ extern "C" {
MZ_ZIP_FLAG_DEFLATE_MAX)
#define MZ_ZIP_FLAG_DATA_DESCRIPTOR (1 << 3)
#define MZ_ZIP_FLAG_UTF8 (1 << 11)
#define MZ_ZIP_FLAG_MASK_LOCAL_INFO (1 << 13)
// MZ_ZIP64
/* MZ_ZIP_EXTENSION */
#define MZ_ZIP_EXTENSION_ZIP64 (0x0001)
#define MZ_ZIP_EXTENSION_NTFS (0x000a)
#define MZ_ZIP_EXTENSION_AES (0x9901)
#define MZ_ZIP_EXTENSION_UNIX1 (0x000d)
#define MZ_ZIP_EXTENSION_SIGN (0x10c5)
#define MZ_ZIP_EXTENSION_HASH (0x1a51)
#define MZ_ZIP_EXTENSION_CDCD (0xcdcd)
/* MZ_ZIP64 */
#define MZ_ZIP64_AUTO (0)
#define MZ_ZIP64_FORCE (1)
#define MZ_ZIP64_DISABLE (2)
// MZ_HOST_SYSTEM
/* MZ_HOST_SYSTEM */
#define MZ_HOST_SYSTEM(VERSION_MADEBY) ((uint8_t)(VERSION_MADEBY >> 8))
#define MZ_HOST_SYSTEM_MSDOS (0)
#define MZ_HOST_SYSTEM_UNIX (3)
#define MZ_HOST_SYSTEM_WINDOWS_NTFS (10)
#define MZ_HOST_SYSTEM_OSX_DARWIN (19)
// MZ_PKCRYPT
/* MZ_PKCRYPT */
#define MZ_PKCRYPT_HEADER_SIZE (12)
// MZ_AES
/* MZ_AES */
#define MZ_AES_VERSION (1)
#define MZ_AES_ENCRYPTION_MODE_128 (0x01)
#define MZ_AES_ENCRYPTION_MODE_192 (0x02)
#define MZ_AES_ENCRYPTION_MODE_256 (0x03)
#define MZ_AES_KEY_LENGTH(MODE) (8 * (MODE & 3) + 8)
#define MZ_AES_KEY_LENGTH_MAX (32)
#define MZ_AES_BLOCK_SIZE (16)
#define MZ_AES_HEADER_SIZE(MODE) ((4 * (MODE & 3) + 4) + 2)
#define MZ_AES_FOOTER_SIZE (10)
// MZ_UTILITY
/* MZ_HASH */
#define MZ_HASH_MD5 (10)
#define MZ_HASH_MD5_SIZE (16)
#define MZ_HASH_SHA1 (20)
#define MZ_HASH_SHA1_SIZE (20)
#define MZ_HASH_SHA256 (23)
#define MZ_HASH_SHA256_SIZE (32)
#define MZ_HASH_MAX_SIZE (256)
/* MZ_ENCODING */
#define MZ_ENCODING_CODEPAGE_437 (437)
#define MZ_ENCODING_CODEPAGE_932 (932)
#define MZ_ENCODING_CODEPAGE_936 (936)
#define MZ_ENCODING_CODEPAGE_950 (950)
#define MZ_ENCODING_UTF8 (65001)
/* MZ_UTILITY */
#define MZ_UNUSED(SYMBOL) ((void)SYMBOL)
#ifndef MZ_CUSTOM_ALLOC
@@ -108,10 +143,108 @@ extern "C" {
#define MZ_FREE(PTR) (free(PTR))
#endif
#if defined(_WINDOWS) && defined(MZ_EXPORTS)
#define MZ_EXPORT __declspec(dllexport)
#else
#define MZ_EXPORT
#endif
/***************************************************************************/
#ifdef __cplusplus
}
#include <stdlib.h> /* size_t, NULL, malloc */
#include <time.h> /* time_t, time() */
#include <string.h> /* memset, strncpy, strlen */
#include <limits.h>
#ifdef HAVE_STDINT_H
# include <stdint.h>
#endif
#ifndef __INT8_TYPE__
typedef signed char int8_t;
#endif
#ifndef __INT16_TYPE__
typedef short int16_t;
#endif
#ifndef __INT32_TYPE__
typedef int int32_t;
#endif
#ifndef __INT64_TYPE__
typedef long long int64_t;
#endif
#ifndef __UINT8_TYPE__
typedef unsigned char uint8_t;
#endif
#ifndef __UINT16_TYPE__
typedef unsigned short uint16_t;
#endif
#ifndef __UINT32_TYPE__
typedef unsigned int uint32_t;
#endif
#ifndef __UINT64_TYPE__
typedef unsigned long long uint64_t;
#endif
#ifdef HAVE_INTTYPES_H
# include <inttypes.h>
#endif
#ifndef PRId8
# define PRId8 "hhd"
#endif
#ifndef PRId16
# define PRId16 "hd"
#endif
#ifndef PRId32
# define PRId32 "d"
#endif
#ifndef PRIu32
# define PRIu32 "u"
#endif
#ifndef PRIx32
# define PRIx32 "x"
#endif
#if ULONG_MAX == 4294967295UL
# ifndef PRId64
# define PRId64 "lld"
# endif
# ifndef PRIu64
# define PRIu64 "llu"
# endif
# ifndef PRIx64
# define PRIx64 "llx"
# endif
#else
# ifndef PRId64
# define PRId64 "ld"
# endif
# ifndef PRIu64
# define PRIu64 "lu"
# endif
# ifndef PRIx64
# define PRIx64 "lx"
# endif
#endif
#ifndef INT16_MAX
# define INT16_MAX 32767
#endif
#ifndef INT32_MAX
# define INT32_MAX 2147483647L
#endif
#ifndef INT64_MAX
# define INT64_MAX 9223372036854775807LL
#endif
#ifndef UINT16_MAX
# define UINT16_MAX 65535U
#endif
#ifndef UINT32_MAX
# define UINT32_MAX 4294967295UL
#endif
#ifndef UINT64_MAX
# define UINT64_MAX 18446744073709551615ULL
#endif
/***************************************************************************/
#endif
+287 -89
View File
@@ -1,34 +1,37 @@
/* mz_compat.c -- Backwards compatible interface for older versions
Version 2.5.2, August 27, 2018
Version 2.8.3, January 26, 2019
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
Copyright (C) 2010-2019 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
Copyright (C) 1998-2010 Gilles Vollant
http://www.winimage.com/zLibDll/minizip.html
https://www.winimage.com/zLibDll/minizip.html
This program is distributed under the terms of the same license as zlib.
See the accompanying LICENSE file for the full text of the license.
*/
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "mz.h"
#include "mz_os.h"
#include "mz_strm.h"
#include "mz_strm_mem.h"
#include "mz_strm_os.h"
#include "mz_strm_zlib.h"
#include "mz_zip.h"
#include <stdio.h> /* SEEK */
#include "mz_compat.h"
/***************************************************************************/
typedef struct mz_compat_s {
void *stream;
void *handle;
void *stream;
void *handle;
uint64_t entry_index;
int64_t entry_pos;
int64_t total_out;
} mz_compat;
/***************************************************************************/
@@ -51,25 +54,25 @@ static int32_t zipConvertAppendToStreamMode(int append)
return mode;
}
zipFile ZEXPORT zipOpen(const char *path, int append)
zipFile zipOpen(const char *path, int append)
{
zlib_filefunc64_def pzlib = mz_stream_os_get_interface();
return zipOpen2(path, append, NULL, &pzlib);
}
zipFile ZEXPORT zipOpen64(const void *path, int append)
zipFile zipOpen64(const void *path, int append)
{
zlib_filefunc64_def pzlib = mz_stream_os_get_interface();
return zipOpen2(path, append, NULL, &pzlib);
}
zipFile ZEXPORT zipOpen2(const char *path, int append, const char **globalcomment,
zipFile zipOpen2(const char *path, int append, const char **globalcomment,
zlib_filefunc_def *pzlib_filefunc_def)
{
return zipOpen2_64(path, append, globalcomment, pzlib_filefunc_def);
}
zipFile ZEXPORT zipOpen2_64(const void *path, int append, const char **globalcomment,
zipFile zipOpen2_64(const void *path, int append, const char **globalcomment,
zlib_filefunc64_def *pzlib_filefunc_def)
{
zipFile zip = NULL;
@@ -87,7 +90,6 @@ zipFile ZEXPORT zipOpen2_64(const void *path, int append, const char **globalcom
return NULL;
}
if (mz_stream_open(stream, path, mode) != MZ_OK)
{
mz_stream_delete(&stream);
@@ -105,7 +107,7 @@ zipFile ZEXPORT zipOpen2_64(const void *path, int append, const char **globalcom
return zip;
}
zipFile ZEXPORT zipOpen_MZ(void *stream, int append, const char **globalcomment)
zipFile zipOpen_MZ(void *stream, int append, const char **globalcomment)
{
mz_compat *compat = NULL;
int32_t err = MZ_OK;
@@ -125,13 +127,20 @@ zipFile ZEXPORT zipOpen_MZ(void *stream, int append, const char **globalcomment)
mz_zip_get_comment(handle, globalcomment);
compat = (mz_compat *)MZ_ALLOC(sizeof(mz_compat));
compat->handle = handle;
compat->stream = stream;
if (compat != NULL)
{
compat->handle = handle;
compat->stream = stream;
}
else
{
mz_zip_delete(&handle);
}
return (zipFile)compat;
}
int ZEXPORT zipOpenNewFileInZip5(zipFile file, const char *filename, const zip_fileinfo *zipfi,
int zipOpenNewFileInZip5(zipFile file, const char *filename, const zip_fileinfo *zipfi,
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
uint16_t size_extrafield_global, const char *comment, uint16_t compression_method, int level,
int raw, int windowBits, int memLevel, int strategy, const char *password,
@@ -171,8 +180,8 @@ int ZEXPORT zipOpenNewFileInZip5(zipFile file, const char *filename, const zip_f
file_info.compression_method = compression_method;
file_info.filename = filename;
//file_info.extrafield_local = extrafield_local;
//file_info.extrafield_local_size = size_extrafield_local;
/* file_info.extrafield_local = extrafield_local; */
/* file_info.extrafield_local_size = size_extrafield_local; */
file_info.extrafield = extrafield_global;
file_info.extrafield_size = size_extrafield_global;
file_info.version_madeby = version_madeby;
@@ -182,7 +191,7 @@ int ZEXPORT zipOpenNewFileInZip5(zipFile file, const char *filename, const zip_f
file_info.zip64 = MZ_ZIP64_FORCE;
else
file_info.zip64 = MZ_ZIP64_DISABLE;
#ifdef HAVE_AES
#ifdef HAVE_WZAES
if ((password != NULL) || (raw && (file_info.flag & MZ_ZIP_FLAG_ENCRYPTED)))
file_info.aes_version = MZ_AES_VERSION;
#endif
@@ -190,7 +199,7 @@ int ZEXPORT zipOpenNewFileInZip5(zipFile file, const char *filename, const zip_f
return mz_zip_entry_write_open(compat->handle, &file_info, (int16_t)level, (uint8_t)raw, password);
}
int ZEXPORT zipOpenNewFileInZip4_64(zipFile file, const char *filename, const zip_fileinfo *zipfi,
int zipOpenNewFileInZip4_64(zipFile file, const char *filename, const zip_fileinfo *zipfi,
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
uint16_t size_extrafield_global, const char *comment, uint16_t compression_method, int level,
int raw, int windowBits, int memLevel, int strategy, const char *password,
@@ -201,7 +210,7 @@ int ZEXPORT zipOpenNewFileInZip4_64(zipFile file, const char *filename, const zi
memLevel, strategy, password, crc_for_crypting, version_madeby, flag_base, zip64);
}
int ZEXPORT zipOpenNewFileInZip4(zipFile file, const char *filename, const zip_fileinfo *zipfi,
int zipOpenNewFileInZip4(zipFile file, const char *filename, const zip_fileinfo *zipfi,
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
uint16_t size_extrafield_global, const char *comment, uint16_t compression_method, int level,
int raw, int windowBits, int memLevel, int strategy, const char *password,
@@ -212,7 +221,7 @@ int ZEXPORT zipOpenNewFileInZip4(zipFile file, const char *filename, const zip_f
memLevel, strategy, password, crc_for_crypting, version_madeby, flag_base, 0);
}
int ZEXPORT zipOpenNewFileInZip3(zipFile file, const char *filename, const zip_fileinfo *zipfi,
int zipOpenNewFileInZip3(zipFile file, const char *filename, const zip_fileinfo *zipfi,
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
uint16_t size_extrafield_global, const char *comment, uint16_t compression_method, int level,
int raw, int windowBits, int memLevel, int strategy, const char *password,
@@ -223,7 +232,7 @@ int ZEXPORT zipOpenNewFileInZip3(zipFile file, const char *filename, const zip_f
memLevel, strategy, password, crc_for_crypting, MZ_VERSION_MADEBY, 0, 0);
}
int ZEXPORT zipOpenNewFileInZip3_64(zipFile file, const char *filename, const zip_fileinfo *zipfi,
int zipOpenNewFileInZip3_64(zipFile file, const char *filename, const zip_fileinfo *zipfi,
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
uint16_t size_extrafield_global, const char *comment, uint16_t compression_method, int level,
int raw, int windowBits, int memLevel, int strategy, const char *password,
@@ -234,24 +243,24 @@ int ZEXPORT zipOpenNewFileInZip3_64(zipFile file, const char *filename, const zi
memLevel, strategy, password, crc_for_crypting, MZ_VERSION_MADEBY, 0, zip64);
}
int ZEXPORT zipWriteInFileInZip(zipFile file, const void *buf, uint32_t len)
int zipWriteInFileInZip(zipFile file, const void *buf, uint32_t len)
{
mz_compat *compat = (mz_compat *)file;
int32_t written = 0;
if (compat == NULL)
if (compat == NULL || len >= INT32_MAX)
return ZIP_PARAMERROR;
written = mz_zip_entry_write(compat->handle, buf, len);
written = mz_zip_entry_write(compat->handle, buf, (int32_t)len);
if ((written < 0) || ((uint32_t)written != len))
return ZIP_ERRNO;
return ZIP_OK;
}
int ZEXPORT zipCloseFileInZipRaw(zipFile file, uint32_t uncompressed_size, uint32_t crc32)
int zipCloseFileInZipRaw(zipFile file, uint32_t uncompressed_size, uint32_t crc32)
{
return zipCloseFileInZipRaw64(file, uncompressed_size, crc32);
}
int ZEXPORT zipCloseFileInZipRaw64(zipFile file, uint64_t uncompressed_size, uint32_t crc32)
int zipCloseFileInZipRaw64(zipFile file, int64_t uncompressed_size, uint32_t crc32)
{
mz_compat *compat = (mz_compat *)file;
if (compat == NULL)
@@ -259,12 +268,12 @@ int ZEXPORT zipCloseFileInZipRaw64(zipFile file, uint64_t uncompressed_size, uin
return mz_zip_entry_close_raw(compat->handle, uncompressed_size, crc32);
}
int ZEXPORT zipCloseFileInZip(zipFile file)
int zipCloseFileInZip(zipFile file)
{
return zipCloseFileInZip64(file);
}
int ZEXPORT zipCloseFileInZip64(zipFile file)
int zipCloseFileInZip64(zipFile file)
{
mz_compat *compat = (mz_compat *)file;
if (compat == NULL)
@@ -272,17 +281,17 @@ int ZEXPORT zipCloseFileInZip64(zipFile file)
return mz_zip_entry_close(compat->handle);
}
int ZEXPORT zipClose(zipFile file, const char *global_comment)
int zipClose(zipFile file, const char *global_comment)
{
return zipClose_64(file, global_comment);
}
int ZEXPORT zipClose_64(zipFile file, const char *global_comment)
int zipClose_64(zipFile file, const char *global_comment)
{
return zipClose2_64(file, global_comment, MZ_VERSION_MADEBY);
}
int ZEXPORT zipClose2_64(zipFile file, const char *global_comment, uint16_t version_madeby)
int zipClose2_64(zipFile file, const char *global_comment, uint16_t version_madeby)
{
mz_compat *compat = (mz_compat *)file;
int32_t err = MZ_OK;
@@ -301,14 +310,14 @@ int ZEXPORT zipClose2_64(zipFile file, const char *global_comment, uint16_t vers
return err;
}
// Only closes the zip handle, does not close the stream
int ZEXPORT zipClose_MZ(zipFile file, const char *global_comment)
/* Only closes the zip handle, does not close the stream */
int zipClose_MZ(zipFile file, const char *global_comment)
{
return zipClose2_MZ(file, global_comment, MZ_VERSION_MADEBY);
}
// Only closes the zip handle, does not close the stream
int ZEXPORT zipClose2_MZ(zipFile file, const char *global_comment, uint16_t version_madeby)
/* Only closes the zip handle, does not close the stream */
int zipClose2_MZ(zipFile file, const char *global_comment, uint16_t version_madeby)
{
mz_compat *compat = (mz_compat *)file;
int32_t err = MZ_OK;
@@ -328,7 +337,7 @@ int ZEXPORT zipClose2_MZ(zipFile file, const char *global_comment, uint16_t vers
return err;
}
void* ZEXPORT zipGetStream(zipFile file)
void* zipGetStream(zipFile file)
{
mz_compat *compat = (mz_compat *)file;
if (compat == NULL)
@@ -338,23 +347,23 @@ void* ZEXPORT zipGetStream(zipFile file)
/***************************************************************************/
unzFile ZEXPORT unzOpen(const char *path)
unzFile unzOpen(const char *path)
{
return unzOpen64(path);
}
unzFile ZEXPORT unzOpen64(const void *path)
unzFile unzOpen64(const void *path)
{
zlib_filefunc64_def pzlib = mz_stream_os_get_interface();
return unzOpen2(path, &pzlib);
}
unzFile ZEXPORT unzOpen2(const char *path, zlib_filefunc_def *pzlib_filefunc_def)
unzFile unzOpen2(const char *path, zlib_filefunc_def *pzlib_filefunc_def)
{
return unzOpen2_64(path, pzlib_filefunc_def);
}
unzFile ZEXPORT unzOpen2_64(const void *path, zlib_filefunc64_def *pzlib_filefunc_def)
unzFile unzOpen2_64(const void *path, zlib_filefunc64_def *pzlib_filefunc_def)
{
unzFile unz = NULL;
void *stream = NULL;
@@ -369,7 +378,7 @@ unzFile ZEXPORT unzOpen2_64(const void *path, zlib_filefunc64_def *pzlib_filefun
if (mz_stream_os_create(&stream) == NULL)
return NULL;
}
if (mz_stream_open(stream, path, MZ_OPEN_MODE_READ) != MZ_OK)
{
mz_stream_delete(&stream);
@@ -385,7 +394,7 @@ unzFile ZEXPORT unzOpen2_64(const void *path, zlib_filefunc64_def *pzlib_filefun
return unz;
}
unzFile ZEXPORT unzOpen_MZ(void *stream)
unzFile unzOpen_MZ(void *stream)
{
mz_compat *compat = NULL;
int32_t err = MZ_OK;
@@ -401,14 +410,22 @@ unzFile ZEXPORT unzOpen_MZ(void *stream)
}
compat = (mz_compat *)MZ_ALLOC(sizeof(mz_compat));
compat->handle = handle;
compat->stream = stream;
if (compat != NULL)
{
compat->handle = handle;
compat->stream = stream;
mz_zip_goto_first_entry(compat->handle);
}
else
{
mz_zip_delete(&handle);
}
mz_zip_goto_first_entry(compat->handle);
return (unzFile)compat;
}
int ZEXPORT unzClose(unzFile file)
int unzClose(unzFile file)
{
mz_compat *compat = (mz_compat *)file;
int32_t err = MZ_OK;
@@ -430,8 +447,8 @@ int ZEXPORT unzClose(unzFile file)
return err;
}
// Only closes the zip handle, does not close the stream
int ZEXPORT unzClose_MZ(unzFile file)
/* Only closes the zip handle, does not close the stream */
int unzClose_MZ(unzFile file)
{
mz_compat *compat = (mz_compat *)file;
int32_t err = MZ_OK;
@@ -445,7 +462,7 @@ int ZEXPORT unzClose_MZ(unzFile file)
return err;
}
int ZEXPORT unzGetGlobalInfo(unzFile file, unz_global_info* pglobal_info32)
int unzGetGlobalInfo(unzFile file, unz_global_info* pglobal_info32)
{
mz_compat *compat = (mz_compat *)file;
unz_global_info64 global_info64;
@@ -465,7 +482,7 @@ int ZEXPORT unzGetGlobalInfo(unzFile file, unz_global_info* pglobal_info32)
return err;
}
int ZEXPORT unzGetGlobalInfo64(unzFile file, unz_global_info64 *pglobal_info)
int unzGetGlobalInfo64(unzFile file, unz_global_info64 *pglobal_info)
{
mz_compat *compat = (mz_compat *)file;
const char *comment_ptr = NULL;
@@ -478,13 +495,13 @@ int ZEXPORT unzGetGlobalInfo64(unzFile file, unz_global_info64 *pglobal_info)
if (err == MZ_OK)
pglobal_info->size_comment = (uint16_t)strlen(comment_ptr);
if ((err == MZ_OK) || (err == MZ_EXIST_ERROR))
err = mz_zip_get_number_entry(compat->handle, (int64_t *)&pglobal_info->number_entry);
err = mz_zip_get_number_entry(compat->handle, &pglobal_info->number_entry);
if (err == MZ_OK)
err = mz_zip_get_disk_number_with_cd(compat->handle, &pglobal_info->number_disk_with_CD);
return err;
}
int ZEXPORT unzGetGlobalComment(unzFile file, char *comment, uint16_t comment_size)
int unzGetGlobalComment(unzFile file, char *comment, uint16_t comment_size)
{
mz_compat *compat = (mz_compat *)file;
const char *comment_ptr = NULL;
@@ -494,15 +511,19 @@ int ZEXPORT unzGetGlobalComment(unzFile file, char *comment, uint16_t comment_si
return UNZ_PARAMERROR;
err = mz_zip_get_comment(compat->handle, &comment_ptr);
if (err == MZ_OK)
strncpy(comment, comment_ptr, comment_size);
{
strncpy(comment, comment_ptr, comment_size - 1);
comment[comment_size - 1] = 0;
}
return err;
}
int ZEXPORT unzOpenCurrentFile3(unzFile file, int *method, int *level, int raw, const char *password)
int unzOpenCurrentFile3(unzFile file, int *method, int *level, int raw, const char *password)
{
mz_compat *compat = (mz_compat *)file;
mz_zip_file *file_info = NULL;
int err = MZ_OK;
int32_t err = MZ_OK;
void *stream = NULL;
if (compat == NULL)
return UNZ_PARAMERROR;
@@ -511,6 +532,7 @@ int ZEXPORT unzOpenCurrentFile3(unzFile file, int *method, int *level, int raw,
if (level != NULL)
*level = 0;
compat->total_out = 0;
err = mz_zip_entry_read_open(compat->handle, (uint8_t)raw, password);
if (err == MZ_OK)
err = mz_zip_entry_get_info(compat->handle, &file_info);
@@ -526,7 +548,7 @@ int ZEXPORT unzOpenCurrentFile3(unzFile file, int *method, int *level, int raw,
*level = 6;
switch (file_info->flag & 0x06)
{
case MZ_ZIP_FLAG_DEFLATE_SUPER_FAST:
case MZ_ZIP_FLAG_DEFLATE_SUPER_FAST:
*level = 1;
break;
case MZ_ZIP_FLAG_DEFLATE_FAST:
@@ -538,47 +560,56 @@ int ZEXPORT unzOpenCurrentFile3(unzFile file, int *method, int *level, int raw,
}
}
}
if (err == MZ_OK)
err = mz_zip_get_stream(compat->handle, &stream);
if (err == MZ_OK)
compat->entry_pos = mz_stream_tell(stream);
return err;
}
int ZEXPORT unzOpenCurrentFile(unzFile file)
int unzOpenCurrentFile(unzFile file)
{
return unzOpenCurrentFile3(file, NULL, NULL, 0, NULL);
}
int ZEXPORT unzOpenCurrentFilePassword(unzFile file, const char *password)
int unzOpenCurrentFilePassword(unzFile file, const char *password)
{
return unzOpenCurrentFile3(file, NULL, NULL, 0, password);
}
int ZEXPORT unzOpenCurrentFile2(unzFile file, int *method, int *level, int raw)
int unzOpenCurrentFile2(unzFile file, int *method, int *level, int raw)
{
return unzOpenCurrentFile3(file, method, level, raw, NULL);
}
int ZEXPORT unzReadCurrentFile(unzFile file, void *buf, uint32_t len)
int unzReadCurrentFile(unzFile file, void *buf, uint32_t len)
{
mz_compat *compat = (mz_compat *)file;
if (compat == NULL)
int32_t err = MZ_OK;
if (compat == NULL || len >= INT32_MAX)
return UNZ_PARAMERROR;
return mz_zip_entry_read(compat->handle, buf, len);
err = mz_zip_entry_read(compat->handle, buf, (int32_t)len);
if (err > 0)
compat->total_out += (uint32_t)err;
return err;
}
int ZEXPORT unzCloseCurrentFile(unzFile file)
int unzCloseCurrentFile(unzFile file)
{
mz_compat *compat = (mz_compat *)file;
int32_t err = MZ_OK;
if (compat == NULL)
return UNZ_PARAMERROR;
return mz_zip_entry_close(compat->handle);
err = mz_zip_entry_close(compat->handle);
return err;
}
int ZEXPORT unzGetCurrentFileInfo(unzFile file, unz_file_info *pfile_info, char *filename,
int unzGetCurrentFileInfo(unzFile file, unz_file_info *pfile_info, char *filename,
uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment, uint16_t comment_size)
{
mz_compat *compat = (mz_compat *)file;
mz_zip_file *file_info = NULL;
int16_t bytes_to_copy = 0;
uint16_t bytes_to_copy = 0;
int32_t err = MZ_OK;
if (compat == NULL)
@@ -637,12 +668,12 @@ int ZEXPORT unzGetCurrentFileInfo(unzFile file, unz_file_info *pfile_info, char
return err;
}
int ZEXPORT unzGetCurrentFileInfo64(unzFile file, unz_file_info64 * pfile_info, char *filename,
int unzGetCurrentFileInfo64(unzFile file, unz_file_info64 * pfile_info, char *filename,
uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment, uint16_t comment_size)
{
mz_compat *compat = (mz_compat *)file;
mz_zip_file *file_info = NULL;
int16_t bytes_to_copy = 0;
uint16_t bytes_to_copy = 0;
int32_t err = MZ_OK;
if (compat == NULL)
@@ -669,8 +700,8 @@ int ZEXPORT unzGetCurrentFileInfo64(unzFile file, unz_file_info64 * pfile_info,
pfile_info->internal_fa = file_info->internal_fa;
pfile_info->external_fa = file_info->external_fa;
pfile_info->compressed_size = file_info->compressed_size;
pfile_info->uncompressed_size = file_info->uncompressed_size;
pfile_info->compressed_size = (uint64_t)file_info->compressed_size;
pfile_info->uncompressed_size = (uint64_t)file_info->uncompressed_size;
if (filename_size > 0 && filename != NULL && file_info->filename != NULL)
{
@@ -703,32 +734,40 @@ int ZEXPORT unzGetCurrentFileInfo64(unzFile file, unz_file_info64 * pfile_info,
return err;
}
int ZEXPORT unzGoToFirstFile(unzFile file)
int unzGoToFirstFile(unzFile file)
{
mz_compat *compat = (mz_compat *)file;
if (compat == NULL)
return UNZ_PARAMERROR;
compat->entry_index = 0;
return mz_zip_goto_first_entry(compat->handle);
}
int ZEXPORT unzGoToNextFile(unzFile file)
int unzGoToNextFile(unzFile file)
{
mz_compat *compat = (mz_compat *)file;
int32_t err = MZ_OK;
if (compat == NULL)
return UNZ_PARAMERROR;
return mz_zip_goto_next_entry(compat->handle);
err = mz_zip_goto_next_entry(compat->handle);
if (err != MZ_END_OF_LIST)
compat->entry_index += 1;
return err;
}
int ZEXPORT unzLocateFile(unzFile file, const char *filename, unzFileNameComparer filename_compare_func)
int unzLocateFile(unzFile file, const char *filename, unzFileNameComparer filename_compare_func)
{
mz_compat *compat = (mz_compat *)file;
mz_zip_file *file_info = NULL;
uint64_t preserve_index = 0;
int32_t err = MZ_OK;
int32_t result = 0;
if (compat == NULL)
return UNZ_PARAMERROR;
preserve_index = compat->entry_index;
err = mz_zip_goto_first_entry(compat->handle);
while (err == MZ_OK)
{
@@ -747,15 +786,80 @@ int ZEXPORT unzLocateFile(unzFile file, const char *filename, unzFileNameCompare
err = mz_zip_goto_next_entry(compat->handle);
}
compat->entry_index = preserve_index;
return err;
}
int32_t ZEXPORT unzGetOffset(unzFile file)
/***************************************************************************/
int unzGetFilePos(unzFile file, unz_file_pos *file_pos)
{
mz_compat *compat = (mz_compat *)file;
int32_t offset = 0;
if (compat == NULL || file_pos == NULL)
return UNZ_PARAMERROR;
offset = unzGetOffset(file);
if (offset < 0)
return offset;
file_pos->pos_in_zip_directory = (uint32_t)offset;
file_pos->num_of_file = (uint32_t)compat->entry_index;
return MZ_OK;
}
int unzGoToFilePos(unzFile file, unz_file_pos *file_pos)
{
mz_compat *compat = (mz_compat *)file;
unz64_file_pos file_pos64;
if (compat == NULL || file_pos == NULL)
return UNZ_PARAMERROR;
file_pos64.pos_in_zip_directory = file_pos->pos_in_zip_directory;
file_pos64.num_of_file = file_pos->num_of_file;
return unzGoToFilePos64(file, &file_pos64);
}
int unzGetFilePos64(unzFile file, unz64_file_pos *file_pos)
{
mz_compat *compat = (mz_compat *)file;
int64_t offset = 0;
if (compat == NULL || file_pos == NULL)
return UNZ_PARAMERROR;
offset = unzGetOffset64(file);
if (offset < 0)
return (int)offset;
file_pos->pos_in_zip_directory = offset;
file_pos->num_of_file = compat->entry_index;
return UNZ_OK;
}
int unzGoToFilePos64(unzFile file, const unz64_file_pos *file_pos)
{
mz_compat *compat = (mz_compat *)file;
int32_t err = MZ_OK;
if (compat == NULL || file_pos == NULL)
return UNZ_PARAMERROR;
err = mz_zip_goto_entry(compat->handle, file_pos->pos_in_zip_directory);
if (err == MZ_OK)
compat->entry_index = file_pos->num_of_file;
return err;
}
int32_t unzGetOffset(unzFile file)
{
return (int32_t)unzGetOffset64(file);
}
int64_t ZEXPORT unzGetOffset64(unzFile file)
int64_t unzGetOffset64(unzFile file)
{
mz_compat *compat = (mz_compat *)file;
if (compat == NULL)
@@ -763,12 +867,12 @@ int64_t ZEXPORT unzGetOffset64(unzFile file)
return mz_zip_get_entry(compat->handle);
}
int ZEXPORT unzSetOffset(unzFile file, uint32_t pos)
int unzSetOffset(unzFile file, uint32_t pos)
{
return unzSetOffset64(file, pos);
}
int ZEXPORT unzSetOffset64(unzFile file, uint64_t pos)
int unzSetOffset64(unzFile file, int64_t pos)
{
mz_compat *compat = (mz_compat *)file;
if (compat == NULL)
@@ -776,16 +880,110 @@ int ZEXPORT unzSetOffset64(unzFile file, uint64_t pos)
return (int)mz_zip_goto_entry(compat->handle, pos);
}
int ZEXPORT unzGetLocalExtrafield(unzFile file, void *buf, unsigned len) // TODO
int unzGetLocalExtrafield(unzFile file, void *buf, unsigned int len)
{
MZ_UNUSED(file);
MZ_UNUSED(buf);
MZ_UNUSED(len);
mz_compat *compat = (mz_compat *)file;
mz_zip_file *file_info = NULL;
int32_t err = MZ_OK;
int32_t bytes_to_copy = 0;
if (compat == NULL || buf == NULL || len >= INT32_MAX)
return UNZ_PARAMERROR;
err = mz_zip_entry_get_local_info(compat->handle, &file_info);
if (err != MZ_OK)
return err;
bytes_to_copy = (int32_t)len;
if (bytes_to_copy > file_info->extrafield_size)
bytes_to_copy = file_info->extrafield_size;
memcpy(buf, file_info->extrafield, bytes_to_copy);
return MZ_OK;
}
int64_t unztell(unzFile file)
{
mz_compat *compat = (mz_compat *)file;
if (compat == NULL)
return UNZ_PARAMERROR;
return (int64_t)compat->total_out;
}
int32_t unzTell(unzFile file)
{
mz_compat *compat = (mz_compat *)file;
if (compat == NULL)
return UNZ_PARAMERROR;
return (int32_t)compat->total_out;
}
int64_t unzTell64(unzFile file)
{
mz_compat *compat = (mz_compat *)file;
if (compat == NULL)
return UNZ_PARAMERROR;
return (int64_t)compat->total_out;
}
int unzSeek(unzFile file, int32_t offset, int origin)
{
return unzSeek64(file, offset, origin);
}
int unzSeek64(unzFile file, int64_t offset, int origin)
{
mz_compat *compat = (mz_compat *)file;
mz_zip_file *file_info = NULL;
int64_t position = 0;
int32_t err = MZ_OK;
void *stream = NULL;
if (compat == NULL)
return UNZ_PARAMERROR;
err = mz_zip_entry_get_info(compat->handle, &file_info);
if (err != MZ_OK)
return err;
if (file_info->compression_method != MZ_COMPRESS_METHOD_STORE)
return UNZ_ERRNO;
if (origin == SEEK_SET)
position = offset;
else if (origin == SEEK_CUR)
position = compat->total_out + offset;
else if (origin == SEEK_END)
position = (int64_t)file_info->compressed_size + offset;
else
return UNZ_PARAMERROR;
if (position > (int64_t)file_info->compressed_size)
return UNZ_PARAMERROR;
err = mz_zip_get_stream(compat->handle, &stream);
if (err == MZ_OK)
err = mz_stream_seek(stream, compat->entry_pos + position, MZ_SEEK_SET);
if (err == MZ_OK)
compat->total_out = position;
return err;
}
int unzEndOfFile(unzFile file)
{
mz_compat *compat = (mz_compat *)file;
mz_zip_file *file_info = NULL;
int32_t err = MZ_OK;
if (compat == NULL)
return UNZ_PARAMERROR;
err = mz_zip_entry_get_info(compat->handle, &file_info);
if (err != MZ_OK)
return err;
if (compat->total_out == (int64_t)file_info->uncompressed_size)
return 1;
return 0;
}
void* ZEXPORT unzGetStream(unzFile file)
void* unzGetStream(unzFile file)
{
mz_compat *compat = (mz_compat *)file;
if (compat == NULL)
@@ -827,7 +1025,7 @@ void fill_win32_filefunc64A(zlib_filefunc64_def *pzlib_filefunc_def)
void fill_win32_filefunc64W(zlib_filefunc64_def *pzlib_filefunc_def)
{
// NOTE: You should no longer pass in widechar string to open function
/* NOTE: You should no longer pass in widechar string to open function */
if (pzlib_filefunc_def != NULL)
*pzlib_filefunc_def = mz_stream_os_get_interface();
}
@@ -836,4 +1034,4 @@ void fill_memory_filefunc(zlib_filefunc_def *pzlib_filefunc_def)
{
if (pzlib_filefunc_def != NULL)
*pzlib_filefunc_def = mz_stream_mem_get_interface();
}
}
+150 -146
View File
@@ -1,11 +1,11 @@
/* mz_compat.h -- Backwards compatible interface for older versions
Version 2.5.2, August 27, 2018
Version 2.8.3, January 26, 2019
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
Copyright (C) 2010-2019 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
Copyright (C) 1998-2010 Gilles Vollant
http://www.winimage.com/zLibDll/minizip.html
https://www.winimage.com/zLibDll/minizip.html
This program is distributed under the terms of the same license as zlib.
See the accompanying LICENSE file for the full text of the license.
@@ -14,13 +14,15 @@
#ifndef MZ_COMPAT_H
#define MZ_COMPAT_H
#include <stdint.h>
#include "mz.h"
#ifdef __cplusplus
"C" {
extern "C" {
#endif
#ifdef HAVE_ZLIB
/***************************************************************************/
#if defined(HAVE_ZLIB) && defined(MAX_MEM_LEVEL)
#ifndef DEF_MEM_LEVEL
# if MAX_MEM_LEVEL >= 8
# define DEF_MEM_LEVEL 8
@@ -28,38 +30,16 @@
# define DEF_MEM_LEVEL MAX_MEM_LEVEL
# endif
#endif
#else
#endif
#ifndef MAX_WBITS
#define MAX_WBITS (15)
#endif
#ifndef DEF_MEM_LEVEL
#define DEF_MEM_LEVEL (8)
#endif
#ifndef ZEXPORT
# define ZEXPORT
#endif
/***************************************************************************/
#if defined(USE_FILE32API)
# define fopen64 fopen
# define ftello64 ftell
# define fseeko64 fseek
#else
# if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || \
defined(__OpenBSD__) || defined(__APPLE__) || defined(__ANDROID__)
# define fopen64 fopen
# define ftello64 ftello
# define fseeko64 fseeko
# endif
# ifdef _MSC_VER
# define fopen64 fopen
# if (_MSC_VER >= 1400) && (!(defined(NO_MSCVER_FILE64_FUNC)))
# define ftello64 _ftelli64
# define fseeko64 _fseeki64
# else /* old MSC */
# define ftello64 ftell
# define fseeko64 fseek
# endif
# endif
# define ZEXPORT MZ_EXPORT
#endif
/***************************************************************************/
@@ -90,8 +70,8 @@ typedef struct
{
uint32_t dosDate;
struct tm tmz_date;
uint16_t internal_fa; // internal file attributes 2 bytes
uint32_t external_fa; // external file attributes 4 bytes
uint16_t internal_fa; /* internal file attributes 2 bytes */
uint32_t external_fa; /* external file attributes 4 bytes */
} zip_fileinfo;
/***************************************************************************/
@@ -110,54 +90,55 @@ typedef struct
#define APPEND_STATUS_ADDINZIP (2)
/***************************************************************************/
/* Writing a zip file */
zipFile ZEXPORT zipOpen(const char *path, int append);
zipFile ZEXPORT zipOpen64(const void *path, int append);
zipFile ZEXPORT zipOpen2(const char *path, int append, const char **globalcomment,
ZEXPORT zipFile zipOpen(const char *path, int append);
ZEXPORT zipFile zipOpen64(const void *path, int append);
ZEXPORT zipFile zipOpen2(const char *path, int append, const char **globalcomment,
zlib_filefunc_def *pzlib_filefunc_def);
zipFile ZEXPORT zipOpen2_64(const void *path, int append, const char **globalcomment,
ZEXPORT zipFile zipOpen2_64(const void *path, int append, const char **globalcomment,
zlib_filefunc64_def *pzlib_filefunc_def);
zipFile ZEXPORT zipOpen_MZ(void *stream, int append, const char **globalcomment);
zipFile zipOpen_MZ(void *stream, int append, const char **globalcomment);
int ZEXPORT zipOpenNewFileInZip3(zipFile file, const char *filename, const zip_fileinfo *zipfi,
ZEXPORT int zipOpenNewFileInZip3(zipFile file, const char *filename, const zip_fileinfo *zipfi,
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
uint16_t size_extrafield_global, const char *comment, uint16_t compression_method, int level,
int raw, int windowBits, int memLevel, int strategy, const char *password,
uint32_t crc_for_crypting);
int ZEXPORT zipOpenNewFileInZip3_64(zipFile file, const char *filename, const zip_fileinfo *zipfi,
ZEXPORT int zipOpenNewFileInZip3_64(zipFile file, const char *filename, const zip_fileinfo *zipfi,
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
uint16_t size_extrafield_global, const char *comment, uint16_t compression_method, int level,
int raw, int windowBits, int memLevel, int strategy, const char *password,
uint32_t crc_for_crypting, int zip64);
int ZEXPORT zipOpenNewFileInZip4(zipFile file, const char *filename, const zip_fileinfo *zipfi,
ZEXPORT int zipOpenNewFileInZip4(zipFile file, const char *filename, const zip_fileinfo *zipfi,
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
uint16_t size_extrafield_global, const char *comment, uint16_t compression_method, int level,
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 ZEXPORT zipOpenNewFileInZip4_64(zipFile file, const char *filename, const zip_fileinfo *zipfi,
ZEXPORT int zipOpenNewFileInZip4_64(zipFile file, const char *filename, const zip_fileinfo *zipfi,
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
uint16_t size_extrafield_global, const char *comment, uint16_t compression_method, int level,
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);
int ZEXPORT zipOpenNewFileInZip5(zipFile file, const char *filename, const zip_fileinfo *zipfi,
ZEXPORT int zipOpenNewFileInZip5(zipFile file, const char *filename, const zip_fileinfo *zipfi,
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
uint16_t size_extrafield_global, const char *comment, uint16_t compression_method, int level,
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);
int ZEXPORT zipWriteInFileInZip(zipFile file, const void *buf, uint32_t len);
ZEXPORT int zipWriteInFileInZip(zipFile file, const void *buf, uint32_t len);
int ZEXPORT zipCloseFileInZipRaw(zipFile file, uint32_t uncompressed_size, uint32_t crc32);
int ZEXPORT zipCloseFileInZipRaw64(zipFile file, uint64_t uncompressed_size, uint32_t crc32);
int ZEXPORT zipCloseFileInZip(zipFile file);
int ZEXPORT zipCloseFileInZip64(zipFile file);
ZEXPORT int zipCloseFileInZipRaw(zipFile file, uint32_t uncompressed_size, uint32_t crc32);
ZEXPORT int zipCloseFileInZipRaw64(zipFile file, int64_t uncompressed_size, uint32_t crc32);
ZEXPORT int zipCloseFileInZip(zipFile file);
ZEXPORT int zipCloseFileInZip64(zipFile file);
int ZEXPORT zipClose(zipFile file, const char *global_comment);
int ZEXPORT zipClose_64(zipFile file, const char *global_comment);
int ZEXPORT zipClose2_64(zipFile file, const char *global_comment, uint16_t version_madeby);
int ZEXPORT zipClose_MZ(zipFile file, const char *global_comment);
int ZEXPORT zipClose2_MZ(zipFile file, const char *global_comment, uint16_t version_madeby);
void* ZEXPORT zipGetStream(zipFile file);
ZEXPORT int zipClose(zipFile file, const char *global_comment);
ZEXPORT int zipClose_64(zipFile file, const char *global_comment);
ZEXPORT int zipClose2_64(zipFile file, const char *global_comment, uint16_t version_madeby);
int zipClose_MZ(zipFile file, const char *global_comment);
int zipClose2_MZ(zipFile file, const char *global_comment, uint16_t version_madeby);
ZEXPORT void* zipGetStream(zipFile file);
/***************************************************************************/
@@ -186,36 +167,36 @@ typedef void *unzFile;
typedef struct unz_global_info64_s
{
uint64_t number_entry; // total number of entries in the central dir on this disk
uint32_t number_disk_with_CD; // number the the disk with central dir, used for spanning ZIP
uint16_t size_comment; // size of the global comment of the zipfile
uint64_t number_entry; /* total number of entries in the central dir on this disk */
uint32_t number_disk_with_CD; /* number the the disk with central dir, used for spanning ZIP */
uint16_t size_comment; /* size of the global comment of the zipfile */
} unz_global_info64;
typedef struct unz_global_info_s
{
uint32_t number_entry; // total number of entries in the central dir on this disk
uint32_t number_disk_with_CD; // number the the disk with central dir, used for spanning ZIP
uint16_t size_comment; // size of the global comment of the zipfile
uint32_t number_entry; /* total number of entries in the central dir on this disk */
uint32_t number_disk_with_CD; /* number the the disk with central dir, used for spanning ZIP */
uint16_t size_comment; /* size of the global comment of the zipfile */
} unz_global_info;
typedef struct unz_file_info64_s
{
uint16_t version; // version made by 2 bytes
uint16_t version_needed; // version needed to extract 2 bytes
uint16_t flag; // general purpose bit flag 2 bytes
uint16_t compression_method; // compression method 2 bytes
uint32_t dosDate; // last mod file date in Dos fmt 4 bytes
uint16_t version; /* version made by 2 bytes */
uint16_t version_needed; /* version needed to extract 2 bytes */
uint16_t flag; /* general purpose bit flag 2 bytes */
uint16_t compression_method; /* compression method 2 bytes */
uint32_t dosDate; /* last mod file date in Dos fmt 4 bytes */
struct tm tmu_date;
uint32_t crc; // crc-32 4 bytes
uint64_t compressed_size; // compressed size 8 bytes
uint64_t uncompressed_size; // uncompressed size 8 bytes
uint16_t size_filename; // filename length 2 bytes
uint16_t size_file_extra; // extra field length 2 bytes
uint16_t size_file_comment; // file comment length 2 bytes
uint32_t crc; /* crc-32 4 bytes */
uint64_t compressed_size; /* compressed size 8 bytes */
uint64_t uncompressed_size; /* uncompressed size 8 bytes */
uint16_t size_filename; /* filename length 2 bytes */
uint16_t size_file_extra; /* extra field length 2 bytes */
uint16_t size_file_comment; /* file comment length 2 bytes */
uint32_t disk_num_start; // disk number start 4 bytes
uint16_t internal_fa; // internal file attributes 2 bytes
uint32_t external_fa; // external file attributes 4 bytes
uint32_t disk_num_start; /* disk number start 4 bytes */
uint16_t internal_fa; /* internal file attributes 2 bytes */
uint32_t external_fa; /* external file attributes 4 bytes */
uint64_t disk_offset;
@@ -224,22 +205,22 @@ typedef struct unz_file_info64_s
typedef struct unz_file_info_s
{
uint16_t version; // version made by 2 bytes
uint16_t version_needed; // version needed to extract 2 bytes
uint16_t flag; // general purpose bit flag 2 bytes
uint16_t compression_method; // compression method 2 bytes
uint32_t dosDate; // last mod file date in Dos fmt 4 bytes
uint16_t version; /* version made by 2 bytes */
uint16_t version_needed; /* version needed to extract 2 bytes */
uint16_t flag; /* general purpose bit flag 2 bytes */
uint16_t compression_method; /* compression method 2 bytes */
uint32_t dosDate; /* last mod file date in Dos fmt 4 bytes */
struct tm tmu_date;
uint32_t crc; // crc-32 4 bytes
uint32_t compressed_size; // compressed size 4 bytes
uint32_t uncompressed_size; // uncompressed size 4 bytes
uint16_t size_filename; // filename length 2 bytes
uint16_t size_file_extra; // extra field length 2 bytes
uint16_t size_file_comment; // file comment length 2 bytes
uint32_t crc; /* crc-32 4 bytes */
uint32_t compressed_size; /* compressed size 4 bytes */
uint32_t uncompressed_size; /* uncompressed size 4 bytes */
uint16_t size_filename; /* filename length 2 bytes */
uint16_t size_file_extra; /* extra field length 2 bytes */
uint16_t size_file_comment; /* file comment length 2 bytes */
uint16_t disk_num_start; // disk number start 2 bytes
uint16_t internal_fa; // internal file attributes 2 bytes
uint32_t external_fa; // external file attributes 4 bytes
uint16_t disk_num_start; /* disk number start 2 bytes */
uint16_t internal_fa; /* internal file attributes 2 bytes */
uint32_t external_fa; /* external file attributes 4 bytes */
uint64_t disk_offset;
} unz_file_info;
@@ -249,65 +230,88 @@ typedef struct unz_file_info_s
typedef int (*unzFileNameComparer)(unzFile file, const char *filename1, const char *filename2);
typedef int (*unzIteratorFunction)(unzFile file);
typedef int (*unzIteratorFunction2)(unzFile file, unz_file_info64 *pfile_info, char *filename,
uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment, uint16_t comment_size);
uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment,
uint16_t comment_size);
/***************************************************************************/
/* Reading a zip file */
ZEXPORT unzFile unzOpen(const char *path);
ZEXPORT unzFile unzOpen64(const void *path);
ZEXPORT unzFile unzOpen2(const char *path, zlib_filefunc_def *pzlib_filefunc_def);
ZEXPORT unzFile unzOpen2_64(const void *path, zlib_filefunc64_def *pzlib_filefunc_def);
unzFile unzOpen_MZ(void *stream);
ZEXPORT int unzClose(unzFile file);
int unzClose_MZ(unzFile file);
ZEXPORT int unzGetGlobalInfo(unzFile file, unz_global_info* pglobal_info32);
ZEXPORT int unzGetGlobalInfo64(unzFile file, unz_global_info64 *pglobal_info);
ZEXPORT int unzGetGlobalComment(unzFile file, char *comment, uint16_t comment_size);
ZEXPORT int unzOpenCurrentFile(unzFile file);
ZEXPORT int unzOpenCurrentFilePassword(unzFile file, const char *password);
ZEXPORT int unzOpenCurrentFile2(unzFile file, int *method, int *level, int raw);
ZEXPORT int unzOpenCurrentFile3(unzFile file, int *method, int *level, int raw, const char *password);
ZEXPORT int unzReadCurrentFile(unzFile file, void *buf, uint32_t len);
ZEXPORT int unzCloseCurrentFile(unzFile file);
ZEXPORT int unzGetCurrentFileInfo(unzFile file, unz_file_info *pfile_info, char *filename,
uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment,
uint16_t comment_size);
ZEXPORT int unzGetCurrentFileInfo64(unzFile file, unz_file_info64 * pfile_info, char *filename,
uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment,
uint16_t comment_size);
ZEXPORT int unzGoToFirstFile(unzFile file);
ZEXPORT int unzGoToNextFile(unzFile file);
ZEXPORT int unzLocateFile(unzFile file, const char *filename, unzFileNameComparer filename_compare_func);
ZEXPORT int unzGetLocalExtrafield(unzFile file, void *buf, unsigned int len);
/***************************************************************************/
/* Raw access to zip file */
typedef struct unz_file_pos_s
{
uint32_t pos_in_zip_directory; /* offset in zip file directory */
uint32_t num_of_file; /* # of file */
} unz_file_pos;
ZEXPORT int unzGetFilePos(unzFile file, unz_file_pos *file_pos);
ZEXPORT int unzGoToFilePos(unzFile file, unz_file_pos *file_pos);
typedef struct unz64_file_pos_s
{
int64_t pos_in_zip_directory; /* offset in zip file directory */
uint64_t num_of_file; /* # of file */
} unz64_file_pos;
ZEXPORT int unzGetFilePos64(unzFile file, unz64_file_pos *file_pos);
ZEXPORT int unzGoToFilePos64(unzFile file, const unz64_file_pos *file_pos);
ZEXPORT int64_t unzGetOffset64(unzFile file);
ZEXPORT int32_t unzGetOffset(unzFile file);
ZEXPORT int unzSetOffset64(unzFile file, int64_t pos);
ZEXPORT int unzSetOffset(unzFile file, uint32_t pos);
ZEXPORT int64_t unztell(unzFile file);
ZEXPORT int32_t unzTell(unzFile file);
ZEXPORT int64_t unzTell64(unzFile file);
ZEXPORT int unzSeek(unzFile file, int32_t offset, int origin);
ZEXPORT int unzSeek64(unzFile file, int64_t offset, int origin);
ZEXPORT int unzEndOfFile(unzFile file);
ZEXPORT void* unzGetStream(unzFile file);
/***************************************************************************/
unzFile ZEXPORT unzOpen(const char *path);
unzFile ZEXPORT unzOpen64(const void *path);
unzFile ZEXPORT unzOpen2(const char *path, zlib_filefunc_def *pzlib_filefunc_def);
unzFile ZEXPORT unzOpen2_64(const void *path, zlib_filefunc64_def *pzlib_filefunc_def);
unzFile ZEXPORT unzOpen_MZ(void *stream);
int ZEXPORT unzClose(unzFile file);
int ZEXPORT unzClose_MZ(unzFile file);
int ZEXPORT unzGetGlobalInfo(unzFile file, unz_global_info* pglobal_info32);
int ZEXPORT unzGetGlobalInfo64(unzFile file, unz_global_info64 *pglobal_info);
int ZEXPORT unzGetGlobalComment(unzFile file, char *comment, uint16_t comment_size);
int ZEXPORT unzOpenCurrentFile(unzFile file);
int ZEXPORT unzOpenCurrentFilePassword(unzFile file, const char *password);
int ZEXPORT unzOpenCurrentFile2(unzFile file, int *method, int *level, int raw);
int ZEXPORT unzOpenCurrentFile3(unzFile file, int *method, int *level, int raw, const char *password);
int ZEXPORT unzReadCurrentFile(unzFile file, void *buf, uint32_t len);
int ZEXPORT unzCloseCurrentFile(unzFile file);
int ZEXPORT unzGetCurrentFileInfo(unzFile file, unz_file_info *pfile_info, char *filename,
uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment, uint16_t comment_size);
int ZEXPORT unzGetCurrentFileInfo64(unzFile file, unz_file_info64 * pfile_info, char *filename,
uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment, uint16_t comment_size);
int ZEXPORT unzGoToFirstFile(unzFile file);
int ZEXPORT unzGoToNextFile(unzFile file);
int ZEXPORT unzLocateFile(unzFile file, const char *filename, unzFileNameComparer filename_compare_func);
int64_t ZEXPORT unzGetOffset64(unzFile file);
int32_t ZEXPORT unzGetOffset(unzFile file);
int ZEXPORT unzSetOffset64(unzFile file, uint64_t pos);
int ZEXPORT unzSetOffset(unzFile file, uint32_t pos);
int ZEXPORT unzGetLocalExtrafield(unzFile file, void *buf, unsigned len);
void* ZEXPORT unzGetStream(unzFile file);
/***************************************************************************/
void fill_fopen_filefunc(zlib_filefunc_def *pzlib_filefunc_def);
void fill_fopen64_filefunc(zlib_filefunc64_def *pzlib_filefunc_def);
void fill_win32_filefunc(zlib_filefunc_def *pzlib_filefunc_def);
void fill_win32_filefunc64(zlib_filefunc64_def *pzlib_filefunc_def);
void fill_win32_filefunc64A(zlib_filefunc64_def *pzlib_filefunc_def);
void fill_win32_filefunc64W(zlib_filefunc64_def *pzlib_filefunc_def);
void fill_memory_filefunc(zlib_filefunc_def *pzlib_filefunc_def);
/***************************************************************************/
#define check_file_exists mz_os_file_exists
#define dosdate_to_tm mz_zip_dosdate_to_tm
#define change_file_date mz_os_set_file_date
#define get_file_date mz_os_get_file_date
#define is_large_file(x) (mz_os_get_file_size(x) >= UINT32_MAX)
#define makedir mz_dir_make
#define get_file_crc(p,b,bs,rc) mz_file_get_crc(p,rc)
#define MKDIR mz_os_make_dir
#define CHDIR chdir
ZEXPORT void fill_fopen_filefunc(zlib_filefunc_def *pzlib_filefunc_def);
ZEXPORT void fill_fopen64_filefunc(zlib_filefunc64_def *pzlib_filefunc_def);
ZEXPORT void fill_win32_filefunc(zlib_filefunc_def *pzlib_filefunc_def);
ZEXPORT void fill_win32_filefunc64(zlib_filefunc64_def *pzlib_filefunc_def);
ZEXPORT void fill_win32_filefunc64A(zlib_filefunc64_def *pzlib_filefunc_def);
ZEXPORT void fill_win32_filefunc64W(zlib_filefunc64_def *pzlib_filefunc_def);
ZEXPORT void fill_memory_filefunc(zlib_filefunc_def *pzlib_filefunc_def);
/***************************************************************************/
+194
View File
@@ -0,0 +1,194 @@
/* mz_crypt.c -- Crypto/hash functions
Version 2.8.3, January 26, 2019
part of the MiniZip project
Copyright (C) 2010-2019 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.
*/
#include "mz.h"
#include "mz_crypt.h"
#if defined(HAVE_ZLIB)
# include "zlib.h"
# if defined(ZLIBNG_VERNUM)
# include "zlib-ng.h"
# endif
#elif defined(HAVE_LZMA)
# include "lzma.h"
#endif
/***************************************************************************/
/* Define z_crc_t in zlib 1.2.5 and less or if using zlib-ng */
#if defined(HAVE_ZLIB) && defined(ZLIBNG_VERNUM)
# if defined(ZLIB_COMPAT)
# define ZLIB_PREFIX(x) x
# else
# define ZLIB_PREFIX(x) zng_ ## x
# endif
typedef uint32_t z_crc_t;
#elif defined(HAVE_ZLIB)
# define ZLIB_PREFIX(x) x
# if (ZLIB_VERNUM < 0x1270)
typedef unsigned long z_crc_t;
# endif
#endif
/***************************************************************************/
uint32_t mz_crypt_crc32_update(uint32_t value, const uint8_t *buf, int32_t size)
{
#if defined(HAVE_ZLIB)
return (uint32_t)ZLIB_PREFIX(crc32)((z_crc_t)value, buf, (uInt)size);
#elif defined(HAVE_LZMA)
return (uint32_t)lzma_crc32(buf, (size_t)size, (uint32_t)value);
#else
static uint32_t crc32_table[256] = {
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
};
value = ~value;
while (size > 0)
{
value = (value >> 8) ^ crc32_table[(value ^ *buf) & 0xFF];
buf += 1;
size -= 1;
}
return ~value;
#endif
}
#ifndef MZ_ZIP_NO_ENCRYPTION
int32_t mz_crypt_pbkdf2(uint8_t *password, int32_t password_length, uint8_t *salt,
int32_t salt_length, int32_t iteration_count, uint8_t *key, int32_t key_length)
{
void *hmac1 = NULL;
void *hmac2 = NULL;
void *hmac3 = NULL;
int32_t err = MZ_OK;
uint16_t i = 0;
uint16_t j = 0;
uint16_t k = 0;
uint16_t block_count = 0;
uint8_t uu[MZ_HASH_SHA1_SIZE];
uint8_t ux[MZ_HASH_SHA1_SIZE];
if (password == NULL || salt == NULL || key == NULL)
return MZ_PARAM_ERROR;
memset(key, 0, key_length);
mz_crypt_hmac_create(&hmac1);
mz_crypt_hmac_create(&hmac2);
mz_crypt_hmac_create(&hmac3);
mz_crypt_hmac_set_algorithm(hmac1, MZ_HASH_SHA1);
mz_crypt_hmac_set_algorithm(hmac2, MZ_HASH_SHA1);
mz_crypt_hmac_set_algorithm(hmac3, MZ_HASH_SHA1);
err = mz_crypt_hmac_init(hmac1, password, password_length);
if (err == MZ_OK)
err = mz_crypt_hmac_init(hmac2, password, password_length);
if (err == MZ_OK)
err = mz_crypt_hmac_update(hmac2, salt, salt_length);
block_count = 1 + ((uint16_t)key_length - 1) / MZ_HASH_SHA1_SIZE;
for (i = 0; (err == MZ_OK) && (i < block_count); i += 1)
{
memset(ux, 0, sizeof(ux));
err = mz_crypt_hmac_copy(hmac2, hmac3);
if (err != MZ_OK)
break;
uu[0] = (uint8_t)((i + 1) >> 24);
uu[1] = (uint8_t)((i + 1) >> 16);
uu[2] = (uint8_t)((i + 1) >> 8);
uu[3] = (uint8_t)(i + 1);
for (j = 0, k = 4; j < iteration_count; j += 1)
{
err = mz_crypt_hmac_update(hmac3, uu, k);
if (err == MZ_OK)
err = mz_crypt_hmac_end(hmac3, uu, sizeof(uu));
if (err != MZ_OK)
break;
for(k = 0; k < MZ_HASH_SHA1_SIZE; k += 1)
ux[k] ^= uu[k];
err = mz_crypt_hmac_copy(hmac1, hmac3);
if (err != MZ_OK)
break;
}
if (err != MZ_OK)
break;
j = 0;
k = i * MZ_HASH_SHA1_SIZE;
while (j < MZ_HASH_SHA1_SIZE && k < key_length)
key[k++] = ux[j++];
}
mz_crypt_hmac_delete(&hmac1);
mz_crypt_hmac_delete(&hmac2);
mz_crypt_hmac_delete(&hmac3);
return err;
}
#endif
/***************************************************************************/
+66
View File
@@ -0,0 +1,66 @@
/* mz_crypt.h -- Crypto/hash functions
Version 2.8.3, January 26, 2019
part of the MiniZip project
Copyright (C) 2010-2019 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_CRYPT_H
#define MZ_CRYPT_H
#ifdef __cplusplus
extern "C" {
#endif
/***************************************************************************/
uint32_t mz_crypt_crc32_update(uint32_t value, const uint8_t *buf, int32_t size);
int32_t mz_crypt_pbkdf2(uint8_t *password, int32_t password_length, uint8_t *salt,
int32_t salt_length, int32_t iteration_count, uint8_t *key, int32_t key_length);
/***************************************************************************/
int32_t mz_crypt_rand(uint8_t *buf, int32_t size);
void mz_crypt_sha_reset(void *handle);
int32_t mz_crypt_sha_begin(void *handle);
int32_t mz_crypt_sha_update(void *handle, const void *buf, int32_t size);
int32_t mz_crypt_sha_end(void *handle, uint8_t *digest, int32_t digest_size);
void mz_crypt_sha_set_algorithm(void *handle, uint16_t algorithm);
void* mz_crypt_sha_create(void **handle);
void mz_crypt_sha_delete(void **handle);
void mz_crypt_aes_reset(void *handle);
int32_t mz_crypt_aes_encrypt(void *handle, uint8_t *buf, int32_t size);
int32_t mz_crypt_aes_decrypt(void *handle, uint8_t *buf, int32_t size);
int32_t mz_crypt_aes_set_encrypt_key(void *handle, const void *key, int32_t key_length);
int32_t mz_crypt_aes_set_decrypt_key(void *handle, const void *key, int32_t key_length);
void mz_crypt_aes_set_mode(void *handle, int32_t mode);
void* mz_crypt_aes_create(void **handle);
void mz_crypt_aes_delete(void **handle);
void mz_crypt_hmac_reset(void *handle);
int32_t mz_crypt_hmac_init(void *handle, const void *key, int32_t key_length);
int32_t mz_crypt_hmac_update(void *handle, const void *buf, int32_t size);
int32_t mz_crypt_hmac_end(void *handle, uint8_t *digest, int32_t digest_size);
int32_t mz_crypt_hmac_copy(void *src_handle, void *target_handle);
void mz_crypt_hmac_set_algorithm(void *handle, uint16_t algorithm);
void* mz_crypt_hmac_create(void **handle);
void mz_crypt_hmac_delete(void **handle);
int32_t mz_crypt_sign(uint8_t *message, int32_t message_size, uint8_t *cert_data, int32_t cert_data_size,
const char *cert_pwd, uint8_t **signature, int32_t *signature_size);
int32_t mz_crypt_sign_verify(uint8_t *message, int32_t message_size, uint8_t *signature, int32_t signature_size);
/***************************************************************************/
#ifdef __cplusplus
}
#endif
#endif
+528
View File
@@ -0,0 +1,528 @@
/* mz_crypt_apple.c -- Crypto/hash functions for Apple
Version 2.8.3, January 26, 2019
part of the MiniZip project
Copyright (C) 2010-2019 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.
*/
#include "mz.h"
#include <CoreFoundation/CoreFoundation.h>
#include <CommonCrypto/CommonCryptor.h>
#include <CommonCrypto/CommonDigest.h>
#include <CommonCrypto/CommonHMAC.h>
#include <CommonCrypto/CommonRandom.h>
#include <Security/Security.h>
#include <Security/SecPolicy.h>
/***************************************************************************/
int32_t mz_crypt_rand(uint8_t *buf, int32_t size)
{
if (CCRandomGenerateBytes(buf, size) != kCCSuccess)
return 0;
return size;
}
/***************************************************************************/
typedef struct mz_crypt_sha_s {
CC_SHA1_CTX ctx1;
CC_SHA256_CTX ctx256;
int32_t error;
int32_t initialized;
uint16_t algorithm;
} mz_crypt_sha;
/***************************************************************************/
void mz_crypt_sha_reset(void *handle)
{
mz_crypt_sha *sha = (mz_crypt_sha *)handle;
sha->error = 0;
sha->initialized = 0;
}
int32_t mz_crypt_sha_begin(void *handle)
{
mz_crypt_sha *sha = (mz_crypt_sha *)handle;
if (sha == NULL)
return MZ_PARAM_ERROR;
mz_crypt_sha_reset(handle);
if (sha->algorithm == MZ_HASH_SHA1)
sha->error = CC_SHA1_Init(&sha->ctx1);
else if (sha->algorithm == MZ_HASH_SHA256)
sha->error = CC_SHA256_Init(&sha->ctx256);
else
return MZ_PARAM_ERROR;
if (!sha->error)
return MZ_HASH_ERROR;
sha->initialized = 1;
return MZ_OK;
}
int32_t mz_crypt_sha_update(void *handle, const void *buf, int32_t size)
{
mz_crypt_sha *sha = (mz_crypt_sha *)handle;
if (sha == NULL || buf == NULL || !sha->initialized)
return MZ_PARAM_ERROR;
if (sha->algorithm == MZ_HASH_SHA1)
sha->error = CC_SHA1_Update(&sha->ctx1, buf, size);
else
sha->error = CC_SHA256_Update(&sha->ctx256, buf, size);
if (!sha->error)
return MZ_HASH_ERROR;
return size;
}
int32_t mz_crypt_sha_end(void *handle, uint8_t *digest, int32_t digest_size)
{
mz_crypt_sha *sha = (mz_crypt_sha *)handle;
if (sha == NULL || digest == NULL || !sha->initialized)
return MZ_PARAM_ERROR;
if (sha->algorithm == MZ_HASH_SHA1)
{
if (digest_size < MZ_HASH_SHA1_SIZE)
return MZ_BUF_ERROR;
sha->error = CC_SHA1_Final(digest, &sha->ctx1);
}
else
{
if (digest_size < MZ_HASH_SHA256_SIZE)
return MZ_BUF_ERROR;
sha->error = CC_SHA256_Final(digest, &sha->ctx256);
}
if (!sha->error)
return MZ_HASH_ERROR;
return MZ_OK;
}
void mz_crypt_sha_set_algorithm(void *handle, uint16_t algorithm)
{
mz_crypt_sha *sha = (mz_crypt_sha *)handle;
sha->algorithm = algorithm;
}
void *mz_crypt_sha_create(void **handle)
{
mz_crypt_sha *sha = NULL;
sha = (mz_crypt_sha *)MZ_ALLOC(sizeof(mz_crypt_sha));
if (sha != NULL)
{
memset(sha, 0, sizeof(mz_crypt_sha));
sha->algorithm = MZ_HASH_SHA256;
}
if (handle != NULL)
*handle = sha;
return sha;
}
void mz_crypt_sha_delete(void **handle)
{
mz_crypt_sha *sha = NULL;
if (handle == NULL)
return;
sha = (mz_crypt_sha *)*handle;
if (sha != NULL)
{
mz_crypt_sha_reset(*handle);
MZ_FREE(sha);
}
*handle = NULL;
}
/***************************************************************************/
typedef struct mz_crypt_aes_s {
CCCryptorRef crypt;
int32_t mode;
int32_t error;
} mz_crypt_aes;
/***************************************************************************/
void mz_crypt_aes_reset(void *handle)
{
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
if (aes->crypt != NULL)
CCCryptorRelease(aes->crypt);
aes->crypt = NULL;
}
int32_t mz_crypt_aes_encrypt(void *handle, uint8_t *buf, int32_t size)
{
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
size_t data_moved = 0;
if (aes == NULL || buf == NULL)
return MZ_PARAM_ERROR;
if (size != MZ_AES_BLOCK_SIZE)
return MZ_PARAM_ERROR;
aes->error = CCCryptorUpdate(aes->crypt, buf, size, buf, size, &data_moved);
if (aes->error != kCCSuccess)
return MZ_HASH_ERROR;
return size;
}
int32_t mz_crypt_aes_decrypt(void *handle, uint8_t *buf, int32_t size)
{
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
size_t data_moved = 0;
if (aes == NULL || buf == NULL)
return MZ_PARAM_ERROR;
if (size != MZ_AES_BLOCK_SIZE)
return MZ_PARAM_ERROR;
aes->error = CCCryptorUpdate(aes->crypt, buf, size, buf, size, &data_moved);
if (aes->error != kCCSuccess)
return MZ_HASH_ERROR;
return size;
}
int32_t mz_crypt_aes_set_encrypt_key(void *handle, const void *key, int32_t key_length)
{
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
if (aes == NULL || key == NULL || key_length == 0)
return MZ_PARAM_ERROR;
mz_crypt_aes_reset(handle);
aes->error = CCCryptorCreate(kCCEncrypt, kCCAlgorithmAES, 0, key, key_length, NULL, &aes->crypt);
if (aes->error != kCCSuccess)
return MZ_HASH_ERROR;
return MZ_OK;
}
int32_t mz_crypt_aes_set_decrypt_key(void *handle, const void *key, int32_t key_length)
{
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
if (aes == NULL || key == NULL || key_length == 0)
return MZ_PARAM_ERROR;
mz_crypt_aes_reset(handle);
aes->error = CCCryptorCreate(kCCDecrypt, kCCAlgorithmAES, 0, key, key_length, NULL, &aes->crypt);
if (aes->error != kCCSuccess)
return MZ_HASH_ERROR;
return MZ_OK;
}
void mz_crypt_aes_set_mode(void *handle, int32_t mode)
{
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
aes->mode = mode;
}
void *mz_crypt_aes_create(void **handle)
{
mz_crypt_aes *aes = NULL;
aes = (mz_crypt_aes *)MZ_ALLOC(sizeof(mz_crypt_aes));
if (aes != NULL)
memset(aes, 0, sizeof(mz_crypt_aes));
if (handle != NULL)
*handle = aes;
return aes;
}
void mz_crypt_aes_delete(void **handle)
{
mz_crypt_aes *aes = NULL;
if (handle == NULL)
return;
aes = (mz_crypt_aes *)*handle;
if (aes != NULL)
{
mz_crypt_aes_reset(*handle);
MZ_FREE(aes);
}
*handle = NULL;
}
/***************************************************************************/
typedef struct mz_crypt_hmac_s {
CCHmacContext ctx;
int32_t initialized;
int32_t error;
uint16_t algorithm;
} mz_crypt_hmac;
/***************************************************************************/
static void mz_crypt_hmac_free(void *handle)
{
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
memset(&hmac->ctx, 0, sizeof(hmac->ctx));
}
void mz_crypt_hmac_reset(void *handle)
{
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
mz_crypt_hmac_free(handle);
hmac->error = 0;
}
int32_t mz_crypt_hmac_init(void *handle, const void *key, int32_t key_length)
{
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
CCHmacAlgorithm algorithm = 0;
if (hmac == NULL || key == NULL)
return MZ_PARAM_ERROR;
mz_crypt_hmac_reset(handle);
if (hmac->algorithm == MZ_HASH_SHA1)
algorithm = kCCHmacAlgSHA1;
else if (hmac->algorithm == MZ_HASH_SHA256)
algorithm = kCCHmacAlgSHA256;
else
return MZ_PARAM_ERROR;
CCHmacInit(&hmac->ctx, algorithm, key, key_length);
return MZ_OK;
}
int32_t mz_crypt_hmac_update(void *handle, const void *buf, int32_t size)
{
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
if (hmac == NULL || buf == NULL)
return MZ_PARAM_ERROR;
CCHmacUpdate(&hmac->ctx, buf, size);
return MZ_OK;
}
int32_t mz_crypt_hmac_end(void *handle, uint8_t *digest, int32_t digest_size)
{
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
if (hmac == NULL || digest == NULL)
return MZ_PARAM_ERROR;
if (hmac->algorithm == MZ_HASH_SHA1)
{
if (digest_size < MZ_HASH_SHA1_SIZE)
return MZ_BUF_ERROR;
CCHmacFinal(&hmac->ctx, digest);
}
else
{
if (digest_size < MZ_HASH_SHA256_SIZE)
return MZ_BUF_ERROR;
CCHmacFinal(&hmac->ctx, digest);
}
return MZ_OK;
}
void mz_crypt_hmac_set_algorithm(void *handle, uint16_t algorithm)
{
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
hmac->algorithm = algorithm;
}
int32_t mz_crypt_hmac_copy(void *src_handle, void *target_handle)
{
mz_crypt_hmac *source = (mz_crypt_hmac *)src_handle;
mz_crypt_hmac *target = (mz_crypt_hmac *)target_handle;
if (source == NULL || target == NULL)
return MZ_PARAM_ERROR;
memcpy(&target->ctx, &source->ctx, sizeof(CCHmacContext));
return MZ_OK;
}
void *mz_crypt_hmac_create(void **handle)
{
mz_crypt_hmac *hmac = NULL;
hmac = (mz_crypt_hmac *)MZ_ALLOC(sizeof(mz_crypt_hmac));
if (hmac != NULL)
{
memset(hmac, 0, sizeof(mz_crypt_hmac));
hmac->algorithm = MZ_HASH_SHA256;
}
if (handle != NULL)
*handle = hmac;
return hmac;
}
void mz_crypt_hmac_delete(void **handle)
{
mz_crypt_hmac *hmac = NULL;
if (handle == NULL)
return;
hmac = (mz_crypt_hmac *)*handle;
if (hmac != NULL)
{
mz_crypt_hmac_free(*handle);
MZ_FREE(hmac);
}
*handle = NULL;
}
/***************************************************************************/
int32_t mz_crypt_sign(uint8_t *message, int32_t message_size, uint8_t *cert_data, int32_t cert_data_size,
const char *cert_pwd, uint8_t **signature, int32_t *signature_size)
{
CFStringRef password_ref = NULL;
CFDictionaryRef options_dict = NULL;
CFDictionaryRef identity_trust = NULL;
CFDataRef signature_out = NULL;
CFDataRef pkcs12_data = NULL;
CFArrayRef items = 0;
SecIdentityRef identity = NULL;
SecTrustRef trust = NULL;
OSStatus status = noErr;
const void *options_key[2] = { kSecImportExportPassphrase, kSecReturnRef };
const void *options_values[2] = { 0, kCFBooleanTrue };
int32_t err = MZ_SIGN_ERROR;
if (message == NULL || cert_data == NULL || signature == NULL || signature_size == NULL)
return MZ_PARAM_ERROR;
*signature = NULL;
*signature_size = 0;
password_ref = CFStringCreateWithCString(0, cert_pwd, kCFStringEncodingUTF8);
options_values[0] = password_ref;
options_dict = CFDictionaryCreate(0, options_key, options_values, 2, 0, 0);
if (options_dict)
pkcs12_data = CFDataCreate(0, cert_data, cert_data_size);
if (pkcs12_data)
status = SecPKCS12Import(pkcs12_data, options_dict, &items);
if (status == noErr)
identity_trust = CFArrayGetValueAtIndex(items, 0);
if (identity_trust)
identity = (SecIdentityRef)CFDictionaryGetValue(identity_trust, kSecImportItemIdentity);
if (identity)
trust = (SecTrustRef)CFDictionaryGetValue(identity_trust, kSecImportItemTrust);
if (trust)
{
status = CMSEncodeContent(identity, NULL, NULL, FALSE, 0, message, message_size, &signature_out);
if (status == errSecSuccess)
{
*signature_size = CFDataGetLength(signature_out);
*signature = (uint8_t *)MZ_ALLOC(*signature_size);
memcpy(*signature, CFDataGetBytePtr(signature_out), *signature_size);
err = MZ_OK;
}
}
if (signature_out)
CFRelease(signature_out);
if (items)
CFRelease(items);
if (pkcs12_data)
CFRelease(pkcs12_data);
if (options_dict)
CFRelease(options_dict);
if (password_ref)
CFRelease(password_ref);
return err;
}
int32_t mz_crypt_sign_verify(uint8_t *message, int32_t message_size, uint8_t *signature, int32_t signature_size)
{
CMSDecoderRef decoder = NULL;
CMSSignerStatus signer_status = 0;
CFDataRef message_out = NULL;
SecPolicyRef trust_policy = NULL;
OSStatus status = noErr;
OSStatus verify_status = noErr;
size_t signer_count = 0;
size_t i = 0;
int32_t err = MZ_SIGN_ERROR;
if (message == NULL || signature == NULL)
return MZ_PARAM_ERROR;
status = CMSDecoderCreate(&decoder);
if (status == errSecSuccess)
status = CMSDecoderUpdateMessage(decoder, signature, signature_size);
if (status == errSecSuccess)
status = CMSDecoderFinalizeMessage(decoder);
if (status == errSecSuccess)
trust_policy = SecPolicyCreateBasicX509();
if (status == errSecSuccess && trust_policy)
{
CMSDecoderGetNumSigners(decoder, &signer_count);
if (signer_count > 0)
err = MZ_OK;
for (i = 0; i < signer_count; i += 1)
{
status = CMSDecoderCopySignerStatus(decoder, i, trust_policy, TRUE, &signer_status, NULL, &verify_status);
if (status != errSecSuccess || verify_status != 0 || signer_status != kCMSSignerValid)
{
err = MZ_SIGN_ERROR;
break;
}
}
}
if (err == MZ_OK)
{
status = CMSDecoderCopyContent(decoder, &message_out);
if ((status != errSecSuccess) ||
(CFDataGetLength(message_out) != message_size) ||
(memcmp(message, CFDataGetBytePtr(message_out), message_size) != 0))
err = MZ_SIGN_ERROR;
}
if (trust_policy)
CFRelease(trust_policy);
if (decoder)
CFRelease(decoder);
return err;
}
+442
View File
@@ -0,0 +1,442 @@
/* mz_crypt_brg.c -- Crypto/hash functions using Brian Gladman's library
Version 2.8.3, January 26, 2019
part of the MiniZip project
Copyright (C) 2010-2019 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.
*/
#include "mz.h"
#include "mz_os.h"
#if defined(HAVE_GETRANDOM)
# include <sys/random.h>
#endif
#if defined(HAVE_LIBBSD)
# include <sys/types.h>
# ifndef __u_char_defined
typedef unsigned char u_char;
# endif
# include <bsd/stdlib.h> /* arc4random_buf */
#endif
#include "aes.h"
#include "sha1.h"
#include "sha2.h"
#include "hmac.h"
/***************************************************************************/
#if defined(HAVE_ARC4RANDOM_BUF)
int32_t mz_crypt_rand(uint8_t *buf, int32_t size)
{
if (size < 0)
return 0;
arc4random_buf(buf, (uint32_t)size);
return size;
}
#elif defined(HAVE_ARC4RANDOM)
int32_t mz_crypt_rand(uint8_t *buf, int32_t size)
{
int32_t left = size;
for (; left > 2; left -= 3, buf += 3)
{
uint32_t val = arc4random();
buf[0] = (val) & 0xFF;
buf[1] = (val >> 8) & 0xFF;
buf[2] = (val >> 16) & 0xFF;
}
for (; left > 0; left--, buf++)
{
*buf = arc4random() & 0xFF;
}
return size - left;
}
#elif defined(HAVE_GETRANDOM)
int32_t mz_crypt_rand(uint8_t *buf, int32_t size)
{
int32_t left = size;
int32_t written = 0;
while (left > 0)
{
written = getrandom(buf, left, 0);
if (written < 0)
return MZ_INTERNAL_ERROR;
buf += written;
left -= written;
}
return size - left;
}
#else
#if !defined(FORCE_LOWQUALITY_ENTROPY)
#pragma message("Warning: Low quality entropy function used for encryption")
#endif
int32_t mz_crypt_rand(uint8_t *buf, int32_t size)
{
return mz_os_rand(buf, size);
}
#endif
/***************************************************************************/
typedef struct mz_crypt_sha_s {
sha256_ctx ctx256;
sha1_ctx ctx1;
int32_t initialized;
uint16_t algorithm;
} mz_crypt_sha;
/***************************************************************************/
void mz_crypt_sha_reset(void *handle)
{
mz_crypt_sha *sha = (mz_crypt_sha *)handle;
sha->initialized = 0;
}
int32_t mz_crypt_sha_begin(void *handle)
{
mz_crypt_sha *sha = (mz_crypt_sha *)handle;
if (sha == NULL)
return MZ_PARAM_ERROR;
if (sha->algorithm == MZ_HASH_SHA1)
sha1_begin(&sha->ctx1);
else
sha256_begin(&sha->ctx256);
sha->initialized = 1;
return MZ_OK;
}
int32_t mz_crypt_sha_update(void *handle, const void *buf, int32_t size)
{
mz_crypt_sha *sha = (mz_crypt_sha *)handle;
if (sha == NULL || buf == NULL || !sha->initialized)
return MZ_PARAM_ERROR;
if (sha->algorithm == MZ_HASH_SHA1)
sha1_hash(buf, size, &sha->ctx1);
else
sha256_hash(buf, size, &sha->ctx256);
return size;
}
int32_t mz_crypt_sha_end(void *handle, uint8_t *digest, int32_t digest_size)
{
mz_crypt_sha *sha = (mz_crypt_sha *)handle;
if (sha == NULL || digest == NULL || !sha->initialized)
return MZ_PARAM_ERROR;
if (sha->algorithm == MZ_HASH_SHA1)
{
if (digest_size < MZ_HASH_SHA1_SIZE)
return MZ_BUF_ERROR;
sha1_end(digest, &sha->ctx1);
}
else
{
if (digest_size < MZ_HASH_SHA256_SIZE)
return MZ_BUF_ERROR;
sha256_end(digest, &sha->ctx256);
}
return MZ_OK;
}
void mz_crypt_sha_set_algorithm(void *handle, uint16_t algorithm)
{
mz_crypt_sha *sha = (mz_crypt_sha *)handle;
sha->algorithm = algorithm;
}
void *mz_crypt_sha_create(void **handle)
{
mz_crypt_sha *sha = NULL;
sha = (mz_crypt_sha *)MZ_ALLOC(sizeof(mz_crypt_sha));
if (sha != NULL)
{
memset(sha, 0, sizeof(mz_crypt_sha));
sha->algorithm = MZ_HASH_SHA256;
}
if (handle != NULL)
*handle = sha;
return sha;
}
void mz_crypt_sha_delete(void **handle)
{
mz_crypt_sha *sha = NULL;
if (handle == NULL)
return;
sha = (mz_crypt_sha *)*handle;
if (sha != NULL)
{
mz_crypt_sha_reset(*handle);
MZ_FREE(sha);
}
*handle = NULL;
}
/***************************************************************************/
typedef struct mz_crypt_aes_s {
aes_encrypt_ctx encrypt_ctx;
aes_decrypt_ctx decrypt_ctx;
int32_t mode;
int32_t error;
} mz_crypt_aes;
/***************************************************************************/
void mz_crypt_aes_reset(void *handle)
{
MZ_UNUSED(handle);
}
int32_t mz_crypt_aes_encrypt(void *handle, uint8_t *buf, int32_t size)
{
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
if (aes == NULL || buf == NULL)
return MZ_PARAM_ERROR;
if (size != MZ_AES_BLOCK_SIZE)
return MZ_PARAM_ERROR;
aes->error = aes_encrypt(buf, buf, &aes->encrypt_ctx);
if (aes->error)
return MZ_CRYPT_ERROR;
return size;
}
int32_t mz_crypt_aes_decrypt(void *handle, uint8_t *buf, int32_t size)
{
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
if (aes == NULL || buf == NULL)
return MZ_PARAM_ERROR;
if (size != MZ_AES_BLOCK_SIZE)
return MZ_PARAM_ERROR;
aes->error = aes_decrypt(buf, buf, &aes->decrypt_ctx);
if (aes->error)
return MZ_CRYPT_ERROR;
return size;
}
int32_t mz_crypt_aes_set_encrypt_key(void *handle, const void *key, int32_t key_length)
{
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
if (aes == NULL || key == NULL)
return MZ_PARAM_ERROR;
mz_crypt_aes_reset(handle);
aes->error = aes_encrypt_key(key, key_length, &aes->encrypt_ctx);
if (aes->error)
return MZ_HASH_ERROR;
return MZ_OK;
}
int32_t mz_crypt_aes_set_decrypt_key(void *handle, const void *key, int32_t key_length)
{
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
if (aes == NULL || key == NULL)
return MZ_PARAM_ERROR;
mz_crypt_aes_reset(handle);
aes->error = aes_decrypt_key(key, key_length, &aes->decrypt_ctx);
if (aes->error)
return MZ_HASH_ERROR;
return MZ_OK;
}
void mz_crypt_aes_set_mode(void *handle, int32_t mode)
{
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
aes->mode = mode;
}
void *mz_crypt_aes_create(void **handle)
{
mz_crypt_aes *aes = NULL;
aes = (mz_crypt_aes *)MZ_ALLOC(sizeof(mz_crypt_aes));
if (aes != NULL)
memset(aes, 0, sizeof(mz_crypt_aes));
if (handle != NULL)
*handle = aes;
return aes;
}
void mz_crypt_aes_delete(void **handle)
{
mz_crypt_aes *aes = NULL;
if (handle == NULL)
return;
aes = (mz_crypt_aes *)*handle;
if (aes != NULL)
MZ_FREE(aes);
*handle = NULL;
}
/***************************************************************************/
typedef struct mz_crypt_hmac_s {
hmac_ctx ctx;
int32_t initialized;
int32_t error;
uint16_t algorithm;
} mz_crypt_hmac;
/***************************************************************************/
void mz_crypt_hmac_reset(void *handle)
{
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
hmac->error = 0;
}
int32_t mz_crypt_hmac_init(void *handle, const void *key, int32_t key_length)
{
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
if (hmac == NULL)
return MZ_PARAM_ERROR;
mz_crypt_hmac_reset(handle);
if (hmac->algorithm == MZ_HASH_SHA1)
hmac_sha_begin(HMAC_SHA1, &hmac->ctx);
else
hmac_sha_begin(HMAC_SHA256, &hmac->ctx);
hmac_sha_key(key, key_length, &hmac->ctx);
return MZ_OK;
}
int32_t mz_crypt_hmac_update(void *handle, const void *buf, int32_t size)
{
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
if (hmac == NULL || buf == NULL)
return MZ_PARAM_ERROR;
hmac_sha_data(buf, size, &hmac->ctx);
return MZ_OK;
}
int32_t mz_crypt_hmac_end(void *handle, uint8_t *digest, int32_t digest_size)
{
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
if (hmac == NULL || digest == NULL)
return MZ_PARAM_ERROR;
if (hmac->algorithm == MZ_HASH_SHA1)
{
if (digest_size < MZ_HASH_SHA1_SIZE)
return MZ_BUF_ERROR;
hmac_sha_end(digest, digest_size, &hmac->ctx);
}
else
{
if (digest_size < MZ_HASH_SHA256_SIZE)
return MZ_BUF_ERROR;
hmac_sha_end(digest, digest_size, &hmac->ctx);
}
return MZ_OK;
}
void mz_crypt_hmac_set_algorithm(void *handle, uint16_t algorithm)
{
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
hmac->algorithm = algorithm;
}
int32_t mz_crypt_hmac_copy(void *src_handle, void *target_handle)
{
mz_crypt_hmac *source = (mz_crypt_hmac *)src_handle;
mz_crypt_hmac *target = (mz_crypt_hmac *)target_handle;
if (target == NULL || source == NULL)
return MZ_PARAM_ERROR;
memcpy(&target->ctx, &source->ctx, sizeof(hmac_ctx));
return MZ_OK;
}
void *mz_crypt_hmac_create(void **handle)
{
mz_crypt_hmac *hmac = NULL;
hmac = (mz_crypt_hmac *)MZ_ALLOC(sizeof(mz_crypt_hmac));
if (hmac != NULL)
{
memset(hmac, 0, sizeof(mz_crypt_hmac));
hmac->algorithm = MZ_HASH_SHA256;
}
if (handle != NULL)
*handle = hmac;
return hmac;
}
void mz_crypt_hmac_delete(void **handle)
{
mz_crypt_hmac *hmac = NULL;
if (handle == NULL)
return;
hmac = (mz_crypt_hmac *)*handle;
if (hmac != NULL)
MZ_FREE(hmac);
*handle = NULL;
}
/***************************************************************************/
int32_t mz_crypt_sign(uint8_t *message, int32_t message_size, uint8_t *cert_data, int32_t cert_data_size,
const char *cert_pwd, uint8_t **signature, int32_t *signature_size)
{
MZ_UNUSED(message);
MZ_UNUSED(message_size);
MZ_UNUSED(cert_data);
MZ_UNUSED(cert_data_size);
MZ_UNUSED(cert_pwd);
MZ_UNUSED(signature);
MZ_UNUSED(signature_size);
return MZ_SUPPORT_ERROR;
}
int32_t mz_crypt_sign_verify(uint8_t *message, int32_t message_size, uint8_t *signature, int32_t signature_size)
{
MZ_UNUSED(message);
MZ_UNUSED(message_size);
MZ_UNUSED(signature);
MZ_UNUSED(signature_size);
return MZ_SUPPORT_ERROR;
}
+679
View File
@@ -0,0 +1,679 @@
/* mz_crypt_openssl.c -- Crypto/hash functions for OpenSSL
Version 2.8.3, January 26, 2019
part of the MiniZip project
Copyright (C) 2010-2019 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.
*/
#include "mz.h"
#include <openssl/err.h>
#include <openssl/rand.h>
#include <openssl/sha.h>
#include <openssl/aes.h>
#include <openssl/hmac.h>
#include <openssl/pkcs12.h>
#include <openssl/cms.h>
#include <openssl/x509.h>
/***************************************************************************/
static void mz_crypt_init(void)
{
static int32_t openssl_initialized = 0;
if (openssl_initialized == 0)
{
OpenSSL_add_all_algorithms();
ERR_load_BIO_strings();
ERR_load_crypto_strings();
openssl_initialized = 1;
}
}
int32_t mz_crypt_rand(uint8_t *buf, int32_t size)
{
int32_t result = 0;
result = RAND_bytes(buf, size);
if (!result)
return MZ_CRYPT_ERROR;
return size;
}
/***************************************************************************/
typedef struct mz_crypt_sha_s {
SHA256_CTX ctx256;
SHA_CTX ctx1;
int32_t initialized;
int32_t error;
uint16_t algorithm;
} mz_crypt_sha;
/***************************************************************************/
void mz_crypt_sha_reset(void *handle)
{
mz_crypt_sha *sha = (mz_crypt_sha *)handle;
sha->error = 0;
sha->initialized = 0;
mz_crypt_init();
}
int32_t mz_crypt_sha_begin(void *handle)
{
mz_crypt_sha *sha = (mz_crypt_sha *)handle;
int32_t result = 0;
if (sha == NULL)
return MZ_PARAM_ERROR;
mz_crypt_sha_reset(handle);
if (sha->algorithm == MZ_HASH_SHA1)
result = SHA1_Init(&sha->ctx1);
else
result = SHA256_Init(&sha->ctx256);
if (!result)
{
sha->error = ERR_get_error();
return MZ_HASH_ERROR;
}
sha->initialized = 1;
return MZ_OK;
}
int32_t mz_crypt_sha_update(void *handle, const void *buf, int32_t size)
{
mz_crypt_sha *sha = (mz_crypt_sha *)handle;
int32_t result = 0;
if (sha == NULL || buf == NULL || !sha->initialized)
return MZ_PARAM_ERROR;
if (sha->algorithm == MZ_HASH_SHA1)
result = SHA1_Update(&sha->ctx1, buf, size);
else
result = SHA256_Update(&sha->ctx256, buf, size);
if (!result)
{
sha->error = ERR_get_error();
return MZ_HASH_ERROR;
}
return size;
}
int32_t mz_crypt_sha_end(void *handle, uint8_t *digest, int32_t digest_size)
{
mz_crypt_sha *sha = (mz_crypt_sha *)handle;
int32_t result = 0;
if (sha == NULL || digest == NULL || !sha->initialized)
return MZ_PARAM_ERROR;
if (sha->algorithm == MZ_HASH_SHA1)
{
if (digest_size < MZ_HASH_SHA1_SIZE)
return MZ_BUF_ERROR;
result = SHA1_Final(digest, &sha->ctx1);
}
else
{
if (digest_size < MZ_HASH_SHA256_SIZE)
return MZ_BUF_ERROR;
result = SHA256_Final(digest, &sha->ctx256);
}
if (!result)
{
sha->error = ERR_get_error();
return MZ_HASH_ERROR;
}
return MZ_OK;
}
void mz_crypt_sha_set_algorithm(void *handle, uint16_t algorithm)
{
mz_crypt_sha *sha = (mz_crypt_sha *)handle;
sha->algorithm = algorithm;
}
void *mz_crypt_sha_create(void **handle)
{
mz_crypt_sha *sha = NULL;
sha = (mz_crypt_sha *)MZ_ALLOC(sizeof(mz_crypt_sha));
if (sha != NULL)
{
memset(sha, 0, sizeof(mz_crypt_sha));
sha->algorithm = MZ_HASH_SHA256;
}
if (handle != NULL)
*handle = sha;
return sha;
}
void mz_crypt_sha_delete(void **handle)
{
mz_crypt_sha *sha = NULL;
if (handle == NULL)
return;
sha = (mz_crypt_sha *)*handle;
if (sha != NULL)
{
mz_crypt_sha_reset(*handle);
MZ_FREE(sha);
}
*handle = NULL;
}
/***************************************************************************/
typedef struct mz_crypt_aes_s {
AES_KEY key;
int32_t mode;
int32_t error;
uint8_t *key_copy;
int32_t key_length;
} mz_crypt_aes;
/***************************************************************************/
void mz_crypt_aes_reset(void *handle)
{
MZ_UNUSED(handle);
mz_crypt_init();
}
int32_t mz_crypt_aes_encrypt(void *handle, uint8_t *buf, int32_t size)
{
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
if (aes == NULL || buf == NULL)
return MZ_PARAM_ERROR;
if (size != MZ_AES_BLOCK_SIZE)
return MZ_PARAM_ERROR;
AES_encrypt(buf, buf, &aes->key);
return size;
}
int32_t mz_crypt_aes_decrypt(void *handle, uint8_t *buf, int32_t size)
{
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
if (aes == NULL || buf == NULL)
return MZ_PARAM_ERROR;
if (size != MZ_AES_BLOCK_SIZE)
return MZ_PARAM_ERROR;
AES_decrypt(buf, buf, &aes->key);
return size;
}
int32_t mz_crypt_aes_set_encrypt_key(void *handle, const void *key, int32_t key_length)
{
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
int32_t result = 0;
int32_t key_bits = 0;
if (aes == NULL || key == NULL)
return MZ_PARAM_ERROR;
mz_crypt_aes_reset(handle);
key_bits = key_length * 8;
result = AES_set_encrypt_key(key, key_bits, &aes->key);
if (result)
{
aes->error = ERR_get_error();
return MZ_HASH_ERROR;
}
return MZ_OK;
}
int32_t mz_crypt_aes_set_decrypt_key(void *handle, const void *key, int32_t key_length)
{
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
int32_t result = 0;
int32_t key_bits = 0;
if (aes == NULL || key == NULL)
return MZ_PARAM_ERROR;
mz_crypt_aes_reset(handle);
key_bits = key_length * 8;
result = AES_set_decrypt_key(key, key_bits, &aes->key);
if (result)
{
aes->error = ERR_get_error();
return MZ_HASH_ERROR;
}
return MZ_OK;
}
void mz_crypt_aes_set_mode(void *handle, int32_t mode)
{
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
aes->mode = mode;
}
void *mz_crypt_aes_create(void **handle)
{
mz_crypt_aes *aes = NULL;
aes = (mz_crypt_aes *)MZ_ALLOC(sizeof(mz_crypt_aes));
if (aes != NULL)
memset(aes, 0, sizeof(mz_crypt_aes));
if (handle != NULL)
*handle = aes;
return aes;
}
void mz_crypt_aes_delete(void **handle)
{
mz_crypt_aes *aes = NULL;
if (handle == NULL)
return;
aes = (mz_crypt_aes *)*handle;
if (aes != NULL)
MZ_FREE(aes);
*handle = NULL;
}
/***************************************************************************/
#if (OPENSSL_VERSION_NUMBER < 0x10100000L) || defined (LIBRESSL_VERSION_NUMBER)
static HMAC_CTX *HMAC_CTX_new(void)
{
HMAC_CTX *ctx = OPENSSL_malloc(sizeof(HMAC_CTX));
if (ctx != NULL)
HMAC_CTX_init(ctx);
return ctx;
}
static void HMAC_CTX_free(HMAC_CTX *ctx)
{
if (ctx != NULL) {
HMAC_CTX_cleanup(ctx);
OPENSSL_free(ctx);
}
}
#endif
typedef struct mz_crypt_hmac_s {
HMAC_CTX* ctx;
int32_t initialized;
int32_t error;
uint16_t algorithm;
} mz_crypt_hmac;
/***************************************************************************/
static void mz_crypt_hmac_free(void *handle)
{
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
HMAC_CTX_free(hmac->ctx);
}
void mz_crypt_hmac_reset(void *handle)
{
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
mz_crypt_hmac_free(handle);
mz_crypt_init();
hmac->error = 0;
}
int32_t mz_crypt_hmac_init(void *handle, const void *key, int32_t key_length)
{
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
int32_t result = 0;
const EVP_MD *evp_md = NULL;
if (hmac == NULL || key == NULL)
return MZ_PARAM_ERROR;
mz_crypt_hmac_reset(handle);
hmac->ctx = HMAC_CTX_new();
if (hmac->algorithm == MZ_HASH_SHA1)
evp_md = EVP_sha1();
else
evp_md = EVP_sha256();
result = HMAC_Init(hmac->ctx, key, key_length, evp_md);
if (!result)
{
hmac->error = ERR_get_error();
return MZ_HASH_ERROR;
}
return MZ_OK;
}
int32_t mz_crypt_hmac_update(void *handle, const void *buf, int32_t size)
{
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
int32_t result = 0;
if (hmac == NULL || buf == NULL)
return MZ_PARAM_ERROR;
result = HMAC_Update(hmac->ctx, buf, size);
if (!result)
{
hmac->error = ERR_get_error();
return MZ_HASH_ERROR;
}
return MZ_OK;
}
int32_t mz_crypt_hmac_end(void *handle, uint8_t *digest, int32_t digest_size)
{
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
int32_t result = 0;
if (hmac == NULL || digest == NULL)
return MZ_PARAM_ERROR;
if (hmac->algorithm == MZ_HASH_SHA1)
{
if (digest_size < MZ_HASH_SHA1_SIZE)
return MZ_BUF_ERROR;
result = HMAC_Final(hmac->ctx, digest, (uint32_t *)&digest_size);
}
else
{
if (digest_size < MZ_HASH_SHA256_SIZE)
return MZ_BUF_ERROR;
result = HMAC_Final(hmac->ctx, digest, (uint32_t *)&digest_size);
}
if (!result)
{
hmac->error = ERR_get_error();
return MZ_HASH_ERROR;
}
return MZ_OK;
}
void mz_crypt_hmac_set_algorithm(void *handle, uint16_t algorithm)
{
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
hmac->algorithm = algorithm;
}
int32_t mz_crypt_hmac_copy(void *src_handle, void *target_handle)
{
mz_crypt_hmac *source = (mz_crypt_hmac *)src_handle;
mz_crypt_hmac *target = (mz_crypt_hmac *)target_handle;
int32_t result = 0;
if (source == NULL || target == NULL)
return MZ_PARAM_ERROR;
if (target->ctx == NULL)
target->ctx = HMAC_CTX_new();
result = HMAC_CTX_copy(target->ctx, source->ctx);
if (!result)
{
target->error = ERR_get_error();
return MZ_HASH_ERROR;
}
return MZ_OK;
}
void *mz_crypt_hmac_create(void **handle)
{
mz_crypt_hmac *hmac = NULL;
hmac = (mz_crypt_hmac *)MZ_ALLOC(sizeof(mz_crypt_hmac));
if (hmac != NULL)
{
memset(hmac, 0, sizeof(mz_crypt_hmac));
hmac->algorithm = MZ_HASH_SHA256;
}
if (handle != NULL)
*handle = hmac;
return hmac;
}
void mz_crypt_hmac_delete(void **handle)
{
mz_crypt_hmac *hmac = NULL;
if (handle == NULL)
return;
hmac = (mz_crypt_hmac *)*handle;
if (hmac != NULL)
{
mz_crypt_hmac_free(*handle);
MZ_FREE(hmac);
}
*handle = NULL;
}
/***************************************************************************/
int32_t mz_crypt_sign(uint8_t *message, int32_t message_size, uint8_t *cert_data, int32_t cert_data_size,
const char *cert_pwd, uint8_t **signature, int32_t *signature_size)
{
PKCS12 *p12 = NULL;
EVP_PKEY *evp_pkey = NULL;
BUF_MEM *buf_mem = NULL;
BIO *cert_bio = NULL;
BIO *message_bio = NULL;
BIO *signature_bio = NULL;
CMS_ContentInfo *cms = NULL;
CMS_SignerInfo *signer_info = NULL;
STACK_OF(X509) *ca_stack = NULL;
X509 *cert = NULL;
int32_t result = 0;
int32_t err = MZ_OK;
if (message == NULL || cert_data == NULL || signature == NULL || signature_size == NULL)
return MZ_PARAM_ERROR;
mz_crypt_init();
*signature = NULL;
*signature_size = 0;
cert_bio = BIO_new_mem_buf(cert_data, cert_data_size);
if (d2i_PKCS12_bio(cert_bio, &p12) == NULL)
err = MZ_SIGN_ERROR;
if (err == MZ_OK)
result = PKCS12_parse(p12, cert_pwd, &evp_pkey, &cert, &ca_stack);
if (result)
{
cms = CMS_sign(NULL, NULL, ca_stack, NULL, CMS_BINARY | CMS_PARTIAL);
if (cms)
signer_info = CMS_add1_signer(cms, cert, evp_pkey, EVP_sha256(), 0);
if (signer_info == NULL)
{
err = MZ_SIGN_ERROR;
}
else
{
message_bio = BIO_new_mem_buf(message, message_size);
signature_bio = BIO_new(BIO_s_mem());
result = CMS_final(cms, message_bio, NULL, CMS_BINARY);
if (result)
result = i2d_CMS_bio(signature_bio, cms);
if (result)
{
BIO_flush(signature_bio);
BIO_get_mem_ptr(signature_bio, &buf_mem);
*signature_size = buf_mem->length;
*signature = MZ_ALLOC(buf_mem->length);
memcpy(*signature, buf_mem->data, buf_mem->length);
}
#if 0
BIO *yy = BIO_new_file("xyz", "wb");
BIO_write(yy, *signature, *signature_size);
BIO_flush(yy);
BIO_free(yy);
#endif
}
}
if (!result)
err = MZ_SIGN_ERROR;
if (cms)
CMS_ContentInfo_free(cms);
if (signature_bio)
BIO_free(signature_bio);
if (cert_bio)
BIO_free(cert_bio);
if (message_bio)
BIO_free(message_bio);
if (p12)
PKCS12_free(p12);
if (err != MZ_OK && *signature != NULL)
{
MZ_FREE(*signature);
*signature = NULL;
*signature_size = 0;
}
return err;
}
int32_t mz_crypt_sign_verify(uint8_t *message, int32_t message_size, uint8_t *signature, int32_t signature_size)
{
CMS_ContentInfo *cms = NULL;
STACK_OF(X509) *signers = NULL;
STACK_OF(X509) *intercerts = NULL;
X509_STORE *cert_store = NULL;
X509_LOOKUP *lookup = NULL;
X509_STORE_CTX *store_ctx = NULL;
BIO *message_bio = NULL;
BIO *signature_bio = NULL;
BUF_MEM *buf_mem = NULL;
int32_t signer_count = 0;
int32_t result = 0;
int32_t i = 0;
int32_t err = MZ_SIGN_ERROR;
if (message == NULL || message_size == 0 || signature == NULL || signature_size == 0)
return MZ_PARAM_ERROR;
mz_crypt_init();
cert_store = X509_STORE_new();
X509_STORE_load_locations(cert_store, "cacert.pem", NULL);
X509_STORE_set_default_paths(cert_store);
#if 0
BIO *yy = BIO_new_file("xyz", "wb");
BIO_write(yy, signature, signature_size);
BIO_flush(yy);
BIO_free(yy);
#endif
lookup = X509_STORE_add_lookup(cert_store, X509_LOOKUP_file());
if (lookup != NULL)
X509_LOOKUP_load_file(lookup, "cacert.pem", X509_FILETYPE_PEM);
lookup = X509_STORE_add_lookup(cert_store, X509_LOOKUP_hash_dir());
if (lookup != NULL)
X509_LOOKUP_add_dir(lookup, NULL, X509_FILETYPE_DEFAULT);
signature_bio = BIO_new_mem_buf(signature, signature_size);
message_bio = BIO_new(BIO_s_mem());
cms = d2i_CMS_bio(signature_bio, NULL);
if (cms)
{
result = CMS_verify(cms, NULL, cert_store, NULL, message_bio, CMS_NO_SIGNER_CERT_VERIFY | CMS_BINARY);
if (result)
signers = CMS_get0_signers(cms);
if (signers)
intercerts = CMS_get1_certs(cms);
if (intercerts)
{
/* Verify signer certificates */
signer_count = sk_X509_num(signers);
if (signer_count > 0)
err = MZ_OK;
for (i = 0; i < signer_count; i++)
{
store_ctx = X509_STORE_CTX_new();
X509_STORE_CTX_init(store_ctx, cert_store, sk_X509_value(signers, i), intercerts);
result = X509_verify_cert(store_ctx);
if (store_ctx)
X509_STORE_CTX_free(store_ctx);
if (!result)
{
err = MZ_SIGN_ERROR;
break;
}
}
}
BIO_get_mem_ptr(message_bio, &buf_mem);
if (err == MZ_OK)
{
/* Verify the message */
if (((int32_t)buf_mem->length != message_size) ||
(memcmp(buf_mem->data, message, message_size) != 0))
err = MZ_SIGN_ERROR;
}
}
#if 0
if (!result)
printf(ERR_error_string(ERR_get_error(), NULL));
#endif
if (cms)
CMS_ContentInfo_free(cms);
if (message_bio)
BIO_free(message_bio);
if (signature_bio)
BIO_free(signature_bio);
if (cert_store)
X509_STORE_free(cert_store);
return err;
}
+771
View File
@@ -0,0 +1,771 @@
/* mz_crypt_win32.c -- Crypto/hash functions for Windows
Version 2.8.3, January 26, 2019
part of the MiniZip project
Copyright (C) 2010-2019 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.
*/
#pragma comment(lib, "crypt32.lib")
#include "mz.h"
#include "mz_os.h"
#include "mz_crypt.h"
#include <windows.h>
#include <wincrypt.h>
/***************************************************************************/
int32_t mz_crypt_rand(uint8_t *buf, int32_t size)
{
HCRYPTPROV provider;
int32_t result = 0;
result = CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT);
if (result)
{
result = CryptGenRandom(provider, size, buf);
CryptReleaseContext(provider, 0);
if (result)
return size;
}
return mz_os_rand(buf, size);
}
/***************************************************************************/
typedef struct mz_crypt_sha_s {
HCRYPTPROV provider;
HCRYPTHASH hash;
int32_t error;
uint16_t algorithm;
} mz_crypt_sha;
/***************************************************************************/
void mz_crypt_sha_reset(void *handle)
{
mz_crypt_sha *sha = (mz_crypt_sha *)handle;
if (sha->hash)
CryptDestroyHash(sha->hash);
sha->hash = 0;
if (sha->provider)
CryptReleaseContext(sha->provider, 0);
sha->provider = 0;
sha->error = 0;
}
int32_t mz_crypt_sha_begin(void *handle)
{
mz_crypt_sha *sha = (mz_crypt_sha *)handle;
ALG_ID alg_id = 0;
int32_t result = 0;
int32_t err = MZ_OK;
if (sha == NULL)
return MZ_PARAM_ERROR;
if (sha->algorithm == MZ_HASH_SHA1)
alg_id = CALG_SHA1;
else
alg_id = CALG_SHA_256;
result = CryptAcquireContext(&sha->provider, NULL, NULL, PROV_RSA_AES, CRYPT_VERIFYCONTEXT | CRYPT_SILENT);
if (!result)
{
sha->error = GetLastError();
err = MZ_CRYPT_ERROR;
}
if (result)
{
result = CryptCreateHash(sha->provider, alg_id, 0, 0, &sha->hash);
if (!result)
{
sha->error = GetLastError();
err = MZ_HASH_ERROR;
}
}
return err;
}
int32_t mz_crypt_sha_update(void *handle, const void *buf, int32_t size)
{
mz_crypt_sha *sha = (mz_crypt_sha *)handle;
int32_t result = 0;
if (sha == NULL || buf == NULL || sha->hash == 0)
return MZ_PARAM_ERROR;
result = CryptHashData(sha->hash, buf, size, 0);
if (!result)
{
sha->error = GetLastError();
return MZ_HASH_ERROR;
}
return size;
}
int32_t mz_crypt_sha_end(void *handle, uint8_t *digest, int32_t digest_size)
{
mz_crypt_sha *sha = (mz_crypt_sha *)handle;
int32_t result = 0;
int32_t expected_size = 0;
if (sha == NULL || digest == NULL || sha->hash == 0)
return MZ_PARAM_ERROR;
result = CryptGetHashParam(sha->hash, HP_HASHVAL, NULL, &expected_size, 0);
if (expected_size > digest_size)
return MZ_BUF_ERROR;
if (!result)
return MZ_HASH_ERROR;
result = CryptGetHashParam(sha->hash, HP_HASHVAL, digest, &digest_size, 0);
if (!result)
{
sha->error = GetLastError();
return MZ_HASH_ERROR;
}
return MZ_OK;
}
void mz_crypt_sha_set_algorithm(void *handle, uint16_t algorithm)
{
mz_crypt_sha *sha = (mz_crypt_sha *)handle;
sha->algorithm = algorithm;
}
void *mz_crypt_sha_create(void **handle)
{
mz_crypt_sha *sha = NULL;
sha = (mz_crypt_sha *)MZ_ALLOC(sizeof(mz_crypt_sha));
if (sha != NULL)
{
memset(sha, 0, sizeof(mz_crypt_sha));
sha->algorithm = MZ_HASH_SHA256;
}
if (handle != NULL)
*handle = sha;
return sha;
}
void mz_crypt_sha_delete(void **handle)
{
mz_crypt_sha *sha = NULL;
if (handle == NULL)
return;
sha = (mz_crypt_sha *)*handle;
if (sha != NULL)
{
mz_crypt_sha_reset(*handle);
MZ_FREE(sha);
}
*handle = NULL;
}
/***************************************************************************/
typedef struct mz_crypt_aes_s {
HCRYPTPROV provider;
HCRYPTKEY key;
int32_t mode;
int32_t error;
} mz_crypt_aes;
/***************************************************************************/
static void mz_crypt_aes_free(void *handle)
{
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
if (aes->key)
CryptDestroyKey(aes->key);
aes->key = 0;
if (aes->provider)
CryptReleaseContext(aes->provider, 0);
aes->provider = 0;
}
void mz_crypt_aes_reset(void *handle)
{
mz_crypt_aes_free(handle);
}
int32_t mz_crypt_aes_encrypt(void *handle, uint8_t *buf, int32_t size)
{
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
int32_t result = 0;
if (aes == NULL || buf == NULL)
return MZ_PARAM_ERROR;
if (size != MZ_AES_BLOCK_SIZE)
return MZ_PARAM_ERROR;
result = CryptEncrypt(aes->key, 0, 0, 0, buf, &size, size);
if (!result)
{
aes->error = GetLastError();
return MZ_CRYPT_ERROR;
}
return size;
}
int32_t mz_crypt_aes_decrypt(void *handle, uint8_t *buf, int32_t size)
{
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
int32_t result = 0;
if (aes == NULL || buf == NULL)
return MZ_PARAM_ERROR;
if (size != MZ_AES_BLOCK_SIZE)
return MZ_PARAM_ERROR;
result = CryptDecrypt(aes->key, 0, 0, 0, buf, &size);
if (!result)
{
aes->error = GetLastError();
return MZ_CRYPT_ERROR;
}
return size;
}
static int32_t mz_crypt_aes_set_key(void *handle, const void *key, int32_t key_length)
{
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
HCRYPTHASH hash = 0;
ALG_ID alg_id = 0;
typedef struct key_blob_header_s {
BLOBHEADER hdr;
uint32_t key_length;
} key_blob_header_s;
key_blob_header_s *key_blob_s = NULL;
uint32_t mode = CRYPT_MODE_ECB;
uint8_t *key_blob = NULL;
int32_t key_blob_size = 0;
int32_t result = 0;
int32_t err = MZ_OK;
if (aes == NULL || key == NULL)
return MZ_PARAM_ERROR;
mz_crypt_aes_reset(handle);
if (key_length == MZ_AES_KEY_LENGTH(MZ_AES_ENCRYPTION_MODE_128))
alg_id = CALG_AES_128;
else if (key_length == MZ_AES_KEY_LENGTH(MZ_AES_ENCRYPTION_MODE_192))
alg_id = CALG_AES_192;
else if (key_length == MZ_AES_KEY_LENGTH(MZ_AES_ENCRYPTION_MODE_256))
alg_id = CALG_AES_256;
else
return MZ_PARAM_ERROR;
result = CryptAcquireContext(&aes->provider, NULL, MS_ENH_RSA_AES_PROV, PROV_RSA_AES, CRYPT_VERIFYCONTEXT | CRYPT_SILENT);
if (result)
{
key_blob_size = sizeof(key_blob_header_s) + key_length;
key_blob = (uint8_t *)MZ_ALLOC(key_blob_size);
if (key_blob)
{
key_blob_s = (key_blob_header_s *)key_blob;
key_blob_s->hdr.bType = PLAINTEXTKEYBLOB;
key_blob_s->hdr.bVersion = CUR_BLOB_VERSION;
key_blob_s->hdr.aiKeyAlg = alg_id;
key_blob_s->hdr.reserved = 0;
key_blob_s->key_length = key_length;
memcpy(key_blob + sizeof(key_blob_header_s), key, key_length);
result = CryptImportKey(aes->provider, key_blob, key_blob_size, 0, 0, &aes->key);
MZ_FREE(key_blob);
}
else
{
err = MZ_MEM_ERROR;
}
}
if (result && err == MZ_OK)
result = CryptSetKeyParam(aes->key, KP_MODE, (const uint8_t *)&mode, 0);
if (!result && err == MZ_OK)
{
aes->error = GetLastError();
err = MZ_CRYPT_ERROR;
}
if (hash)
CryptDestroyHash(hash);
return err;
}
int32_t mz_crypt_aes_set_encrypt_key(void *handle, const void *key, int32_t key_length)
{
return mz_crypt_aes_set_key(handle, key, key_length);
}
int32_t mz_crypt_aes_set_decrypt_key(void *handle, const void *key, int32_t key_length)
{
return mz_crypt_aes_set_key(handle, key, key_length);
}
void mz_crypt_aes_set_mode(void *handle, int32_t mode)
{
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
aes->mode = mode;
}
void *mz_crypt_aes_create(void **handle)
{
mz_crypt_aes *aes = NULL;
aes = (mz_crypt_aes *)MZ_ALLOC(sizeof(mz_crypt_aes));
if (aes != NULL)
memset(aes, 0, sizeof(mz_crypt_aes));
if (handle != NULL)
*handle = aes;
return aes;
}
void mz_crypt_aes_delete(void **handle)
{
mz_crypt_aes *aes = NULL;
if (handle == NULL)
return;
aes = (mz_crypt_aes *)*handle;
if (aes != NULL)
{
mz_crypt_aes_free(*handle);
MZ_FREE(aes);
}
*handle = NULL;
}
/***************************************************************************/
typedef struct mz_crypt_hmac_s {
HCRYPTPROV provider;
HCRYPTHASH hash;
HCRYPTKEY key;
HMAC_INFO info;
int32_t mode;
int32_t error;
uint16_t algorithm;
} mz_crypt_hmac;
/***************************************************************************/
static void mz_crypt_hmac_free(void *handle)
{
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
if (hmac->key)
CryptDestroyKey(hmac->key);
hmac->key = 0;
if (hmac->hash)
CryptDestroyHash(hmac->hash);
hmac->hash = 0;
if (hmac->provider)
CryptReleaseContext(hmac->provider, 0);
hmac->provider = 0;
memset(&hmac->info, 0, sizeof(hmac->info));
}
void mz_crypt_hmac_reset(void *handle)
{
mz_crypt_hmac_free(handle);
}
int32_t mz_crypt_hmac_init(void *handle, const void *key, int32_t key_length)
{
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
ALG_ID alg_id = 0;
typedef struct key_blob_header_s {
BLOBHEADER hdr;
uint32_t key_length;
} key_blob_header_s;
key_blob_header_s *key_blob_s = NULL;
uint8_t *key_blob = NULL;
int32_t key_blob_size = 0;
int32_t result = 0;
int32_t err = MZ_OK;
if (hmac == NULL || key == NULL)
return MZ_PARAM_ERROR;
mz_crypt_hmac_reset(handle);
if (hmac->algorithm == MZ_HASH_SHA1)
alg_id = CALG_SHA1;
else
alg_id = CALG_SHA_256;
hmac->info.HashAlgid = alg_id;
result = CryptAcquireContext(&hmac->provider, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT | CRYPT_SILENT);
if (!result)
{
hmac->error = GetLastError();
err = MZ_CRYPT_ERROR;
}
else
{
key_blob_size = sizeof(key_blob_header_s) + key_length;
key_blob = (uint8_t *)MZ_ALLOC(key_blob_size);
}
if (key_blob)
{
key_blob_s = (key_blob_header_s *)key_blob;
key_blob_s->hdr.bType = PLAINTEXTKEYBLOB;
key_blob_s->hdr.bVersion = CUR_BLOB_VERSION;
key_blob_s->hdr.aiKeyAlg = CALG_RC2;
key_blob_s->hdr.reserved = 0;
key_blob_s->key_length = key_length;
memcpy(key_blob + sizeof(key_blob_header_s), key, key_length);
result = CryptImportKey(hmac->provider, key_blob, key_blob_size, 0, CRYPT_IPSEC_HMAC_KEY, &hmac->key);
if (result)
result = CryptCreateHash(hmac->provider, CALG_HMAC, hmac->key, 0, &hmac->hash);
if (result)
result = CryptSetHashParam(hmac->hash, HP_HMAC_INFO, (uint8_t *)&hmac->info, 0);
MZ_FREE(key_blob);
}
else if (err == MZ_OK)
{
err = MZ_MEM_ERROR;
}
if (!result)
{
hmac->error = GetLastError();
err = MZ_CRYPT_ERROR;
}
if (err != MZ_OK)
mz_crypt_hmac_free(handle);
return err;
}
int32_t mz_crypt_hmac_update(void *handle, const void *buf, int32_t size)
{
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
int32_t result = 0;
if (hmac == NULL || buf == NULL || hmac->hash == 0)
return MZ_PARAM_ERROR;
result = CryptHashData(hmac->hash, buf, size, 0);
if (!result)
{
hmac->error = GetLastError();
return MZ_HASH_ERROR;
}
return MZ_OK;
}
int32_t mz_crypt_hmac_end(void *handle, uint8_t *digest, int32_t digest_size)
{
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
int32_t result = 0;
int32_t expected_size = 0;
if (hmac == NULL || digest == NULL || hmac->hash == 0)
return MZ_PARAM_ERROR;
result = CryptGetHashParam(hmac->hash, HP_HASHVAL, NULL, &expected_size, 0);
if (expected_size > digest_size)
return MZ_BUF_ERROR;
if (!result)
return MZ_HASH_ERROR;
result = CryptGetHashParam(hmac->hash, HP_HASHVAL, digest, &digest_size, 0);
if (!result)
{
hmac->error = GetLastError();
return MZ_HASH_ERROR;
}
return MZ_OK;
}
void mz_crypt_hmac_set_algorithm(void *handle, uint16_t algorithm)
{
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
hmac->algorithm = algorithm;
}
int32_t mz_crypt_hmac_copy(void *src_handle, void *target_handle)
{
mz_crypt_hmac *source = (mz_crypt_hmac *)src_handle;
mz_crypt_hmac *target = (mz_crypt_hmac *)target_handle;
int32_t result = 0;
int32_t err = MZ_OK;
result = CryptDuplicateHash(source->hash, NULL, 0, &target->hash);
if (!result)
{
target->error = GetLastError();
err = MZ_HASH_ERROR;
}
return err;
}
void *mz_crypt_hmac_create(void **handle)
{
mz_crypt_hmac *hmac = NULL;
hmac = (mz_crypt_hmac *)MZ_ALLOC(sizeof(mz_crypt_hmac));
if (hmac != NULL)
{
memset(hmac, 0, sizeof(mz_crypt_hmac));
hmac->algorithm = MZ_HASH_SHA256;
}
if (handle != NULL)
*handle = hmac;
return hmac;
}
void mz_crypt_hmac_delete(void **handle)
{
mz_crypt_hmac *hmac = NULL;
if (handle == NULL)
return;
hmac = (mz_crypt_hmac *)*handle;
if (hmac != NULL)
{
mz_crypt_hmac_free(*handle);
MZ_FREE(hmac);
}
*handle = NULL;
}
/***************************************************************************/
int32_t mz_crypt_sign(uint8_t *message, int32_t message_size, uint8_t *cert_data, int32_t cert_data_size,
const char *cert_pwd, uint8_t **signature, int32_t *signature_size)
{
CRYPT_SIGN_MESSAGE_PARA sign_params;
CRYPT_DATA_BLOB cert_data_blob;
PCCERT_CONTEXT cert_context = NULL;
HCERTSTORE cert_store = 0;
wchar_t *password_wide = NULL;
int32_t result = 0;
int32_t err = MZ_OK;
uint32_t messages_sizes[1];
uint8_t *messages[1];
if (message == NULL || cert_data == NULL || signature == NULL || signature_size == NULL)
return MZ_PARAM_ERROR;
*signature = NULL;
*signature_size = 0;
cert_data_blob.pbData = cert_data;
cert_data_blob.cbData = cert_data_size;
password_wide = mz_os_unicode_string_create(cert_pwd, MZ_ENCODING_UTF8);
if (password_wide)
{
cert_store = PFXImportCertStore(&cert_data_blob, password_wide, 0);
mz_os_unicode_string_delete(&password_wide);
}
if (cert_store == NULL)
cert_store = PFXImportCertStore(&cert_data_blob, L"", 0);
if (cert_store == NULL)
cert_store = PFXImportCertStore(&cert_data_blob, NULL, 0);
if (cert_store == NULL)
return MZ_PARAM_ERROR;
if (err == MZ_OK)
{
cert_context = CertFindCertificateInStore(cert_store,
X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, 0, CERT_FIND_HAS_PRIVATE_KEY, NULL, NULL);
if (cert_context == NULL)
err = MZ_PARAM_ERROR;
}
if (err == MZ_OK)
{
memset(&sign_params, 0, sizeof(sign_params));
sign_params.cbSize = sizeof(sign_params);
sign_params.dwMsgEncodingType = PKCS_7_ASN_ENCODING | X509_ASN_ENCODING;
sign_params.pSigningCert = cert_context;
sign_params.HashAlgorithm.pszObjId = szOID_NIST_sha256;
sign_params.cMsgCert = 1;
sign_params.rgpMsgCert = &cert_context;
messages[0] = message;
messages_sizes[0] = message_size;
#if 0 /* Timestamp support */
CRYPT_ATTR_BLOB crypt_blob;
CRYPT_TIMESTAMP_CONTEXT *ts_context = NULL;
CRYPT_ATTRIBUTE unauth_attribs[1];
wchar_t *timestamp_url_wide = NULL;
const char *timestamp_url = NULL;
if (timestamp_url != NULL)
timestamp_url_wide = mz_os_unicode_string_create(timestamp_url);
if (timestamp_url_wide != NULL)
{
result = CryptRetrieveTimeStamp(timestamp_url_wide,
TIMESTAMP_NO_AUTH_RETRIEVAL | TIMESTAMP_VERIFY_CONTEXT_SIGNATURE, 0, szOID_NIST_sha256,
NULL, message, message_size, &ts_context, NULL, NULL);
mz_os_unicode_string_delete(&timestamp_url_wide);
if ((result) && (ts_context != NULL))
{
crypt_blob.cbData = ts_context->cbEncoded;
crypt_blob.pbData = ts_context->pbEncoded;
unauth_attribs[0].pszObjId = "1.2.840.113549.1.9.16.2.14"; //id-smime-aa-timeStampToken
unauth_attribs[0].cValue = 1;
unauth_attribs[0].rgValue = &crypt_blob;
sign_params.rgUnauthAttr = &unauth_attribs[0];
sign_params.cUnauthAttr = 1;
}
}
if (ts_context != NULL)
CryptMemFree(ts_context);
if (result)
#endif
result = CryptSignMessage(&sign_params, FALSE, 1, messages, messages_sizes,
NULL, signature_size);
if (result && *signature_size > 0)
*signature = (uint8_t *)MZ_ALLOC(*signature_size);
if (result && *signature != NULL)
result = CryptSignMessage(&sign_params, FALSE, 1, messages, messages_sizes,
*signature, signature_size);
if (!result)
err = MZ_SIGN_ERROR;
}
if (cert_context != NULL)
CertFreeCertificateContext(cert_context);
if (cert_store != NULL)
CertCloseStore(cert_store, 0);
return err;
}
int32_t mz_crypt_sign_verify(uint8_t *message, int32_t message_size, uint8_t *signature, int32_t signature_size)
{
CRYPT_VERIFY_MESSAGE_PARA verify_params;
HCRYPTMSG crypt_msg = 0;
int32_t result = 0;
int32_t err = MZ_SIGN_ERROR;
uint8_t *decoded = NULL;
int32_t decoded_size = 0;
memset(&verify_params, 0, sizeof(verify_params));
verify_params.cbSize = sizeof(verify_params);
verify_params.dwMsgAndCertEncodingType = PKCS_7_ASN_ENCODING | X509_ASN_ENCODING;
verify_params.hCryptProv = 0;
verify_params.pfnGetSignerCertificate = NULL;
verify_params.pvGetArg = NULL;
result = CryptVerifyMessageSignature(&verify_params, 0, signature, signature_size,
NULL, &decoded_size, NULL);
if (result && decoded_size > 0)
decoded = (uint8_t *)MZ_ALLOC(decoded_size);
if (result && decoded != NULL)
result = CryptVerifyMessageSignature(&verify_params, 0, signature, signature_size,
decoded, &decoded_size, NULL);
#if 0
crypt_msg = CryptMsgOpenToDecode(PKCS_7_ASN_ENCODING | X509_ASN_ENCODING, 0, 0, 0, NULL, NULL);
if (crypt_msg != NULL)
{
/* Timestamp support */
PCRYPT_ATTRIBUTES unauth_attribs = NULL;
HCRYPTMSG ts_msg = 0;
uint8_t *ts_content = NULL;
int32_t ts_content_size = 0;
uint8_t *ts_signature = NULL;
int32_t ts_signature_size = 0;
result = CryptMsgUpdate(crypt_msg, signature, signature_size, 1);
if (result)
CryptMsgGetParam(crypt_msg, CMSG_SIGNER_UNAUTH_ATTR_PARAM, 0, NULL, &ts_signature_size);
if ((result) && (ts_signature_size > 0))
ts_signature = (uint8_t *)MZ_ALLOC(ts_signature_size);
if ((result) && (ts_signature != NULL))
{
result = CryptMsgGetParam(crypt_msg, CMSG_SIGNER_UNAUTH_ATTR_PARAM, 0, ts_signature,
&ts_signature_size);
if (result)
{
unauth_attribs = (PCRYPT_ATTRIBUTES)ts_signature;
if ((unauth_attribs->cAttr > 0) && (unauth_attribs->rgAttr[0].cValue > 0))
{
ts_content = unauth_attribs->rgAttr[0].rgValue->pbData;
ts_content_size = unauth_attribs->rgAttr[0].rgValue->cbData;
}
}
if ((result) && (ts_content != NULL))
result = CryptVerifyTimeStampSignature(ts_content, ts_content_size, decoded,
decoded_size, 0, &crypt_context, NULL, NULL);
if (result)
err = MZ_OK;
}
if (ts_signature != NULL)
MZ_FREE(ts_signature);
if (crypt_context != NULL)
CryptMemFree(crypt_context);
}
else
{
result = 0;
}
#endif
if ((result) && (decoded != NULL) && (decoded_size == message_size))
{
/* Verify cms message with our stored message */
if (memcmp(decoded, message, message_size) == 0)
err = MZ_OK;
}
if (crypt_msg != NULL)
CryptMsgClose(crypt_msg);
if (decoded != NULL)
MZ_FREE(decoded);
return err;
}
+32 -108
View File
@@ -1,62 +1,23 @@
/* mz_os.c -- System functions
Version 2.5.2, August 27, 2018
Version 2.8.3, January 26, 2019
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
Copyright (C) 2010-2019 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
Copyright (C) 1998-2010 Gilles Vollant
http://www.winimage.com/zLibDll/minizip.html
https://www.winimage.com/zLibDll/minizip.html
This program is distributed under the terms of the same license as zlib.
See the accompanying LICENSE file for the full text of the license.
*/
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <ctype.h>
#include "mz.h"
#include "mz_crypt.h"
#include "mz_os.h"
#include "mz_strm.h"
#include "mz_strm_crc32.h"
#include "mz_strm_os.h"
/***************************************************************************/
static uint32_t mz_encoding_codepage437_to_utf8[256] = {
0x000000, 0xba98e2, 0xbb98e2, 0xa599e2, 0xa699e2, 0xa399e2, 0xa099e2, 0xa280e2,
0x9897e2, 0x8b97e2, 0x9997e2, 0x8299e2, 0x8099e2, 0xaa99e2, 0xab99e2, 0xbc98e2,
0xba96e2, 0x8497e2, 0x9586e2, 0xbc80e2, 0x00b6c2, 0x00a7c2, 0xac96e2, 0xa886e2,
0x9186e2, 0x9386e2, 0x9286e2, 0x9086e2, 0x9f88e2, 0x9486e2, 0xb296e2, 0xbc96e2,
0x000020, 0x000021, 0x000022, 0x000023, 0x000024, 0x000025, 0x000026, 0x000027,
0x000028, 0x000029, 0x00002a, 0x00002b, 0x00002c, 0x00002d, 0x00002e, 0x00002f,
0x000030, 0x000031, 0x000032, 0x000033, 0x000034, 0x000035, 0x000036, 0x000037,
0x000038, 0x000039, 0x00003a, 0x00003b, 0x00003c, 0x00003d, 0x00003e, 0x00003f,
0x000040, 0x000041, 0x000042, 0x000043, 0x000044, 0x000045, 0x000046, 0x000047,
0x000048, 0x000049, 0x00004a, 0x00004b, 0x00004c, 0x00004d, 0x00004e, 0x00004f,
0x000050, 0x000051, 0x000052, 0x000053, 0x000054, 0x000055, 0x000056, 0x000057,
0x000058, 0x000059, 0x00005a, 0x00005b, 0x00005c, 0x00005d, 0x00005e, 0x00005f,
0x000060, 0x000061, 0x000062, 0x000063, 0x000064, 0x000065, 0x000066, 0x000067,
0x000068, 0x000069, 0x00006a, 0x00006b, 0x00006c, 0x00006d, 0x00006e, 0x00006f,
0x000070, 0x000071, 0x000072, 0x000073, 0x000074, 0x000075, 0x000076, 0x000077,
0x000078, 0x000079, 0x00007a, 0x00007b, 0x00007c, 0x00007d, 0x00007e, 0x828ce2,
0x0087c3, 0x00bcc3, 0x00a9c3, 0x00a2c3, 0x00a4c3, 0x00a0c3, 0x00a5c3, 0x00a7c3,
0x00aac3, 0x00abc3, 0x00a8c3, 0x00afc3, 0x00aec3, 0x00acc3, 0x0084c3, 0x0085c3,
0x0089c3, 0x00a6c3, 0x0086c3, 0x00b4c3, 0x00b6c3, 0x00b2c3, 0x00bbc3, 0x00b9c3,
0x00bfc3, 0x0096c3, 0x009cc3, 0x00a2c2, 0x00a3c2, 0x00a5c2, 0xa782e2, 0x0092c6,
0x00a1c3, 0x00adc3, 0x00b3c3, 0x00bac3, 0x00b1c3, 0x0091c3, 0x00aac2, 0x00bac2,
0x00bfc2, 0x908ce2, 0x00acc2, 0x00bdc2, 0x00bcc2, 0x00a1c2, 0x00abc2, 0x00bbc2,
0x9196e2, 0x9296e2, 0x9396e2, 0x8294e2, 0xa494e2, 0xa195e2, 0xa295e2, 0x9695e2,
0x9595e2, 0xa395e2, 0x9195e2, 0x9795e2, 0x9d95e2, 0x9c95e2, 0x9b95e2, 0x9094e2,
0x9494e2, 0xb494e2, 0xac94e2, 0x9c94e2, 0x8094e2, 0xbc94e2, 0x9e95e2, 0x9f95e2,
0x9a95e2, 0x9495e2, 0xa995e2, 0xa695e2, 0xa095e2, 0x9095e2, 0xac95e2, 0xa795e2,
0xa895e2, 0xa495e2, 0xa595e2, 0x9995e2, 0x9895e2, 0x9295e2, 0x9395e2, 0xab95e2,
0xaa95e2, 0x9894e2, 0x8c94e2, 0x8896e2, 0x8496e2, 0x8c96e2, 0x9096e2, 0x8096e2,
0x00b1ce, 0x009fc3, 0x0093ce, 0x0080cf, 0x00a3ce, 0x0083cf, 0x00b5c2, 0x0084cf,
0x00a6ce, 0x0098ce, 0x00a9ce, 0x00b4ce, 0x9e88e2, 0x0086cf, 0x00b5ce, 0xa988e2,
0xa189e2, 0x00b1c2, 0xa589e2, 0xa489e2, 0xa08ce2, 0xa18ce2, 0x00b7c3, 0x8889e2,
0x00b0c2, 0x9988e2, 0x00b7c2, 0x9a88e2, 0xbf81e2, 0x00b2c2, 0xa096e2, 0x00a0c2
};
#include <ctype.h> /* tolower */
/***************************************************************************/
@@ -67,11 +28,12 @@ int32_t mz_path_combine(char *path, const char *join, int32_t max_path)
if (path == NULL || join == NULL || max_path == 0)
return MZ_PARAM_ERROR;
path_len = strlen(path);
path_len = (int32_t)strlen(path);
if (path_len == 0)
{
strncpy(path, join, max_path);
strncpy(path, join, max_path - 1);
path[max_path - 1] = 0;
}
else
{
@@ -105,7 +67,7 @@ int32_t mz_path_compare_wc(const char *path, const char *wildcard, uint8_t ignor
return MZ_EXIST_ERROR;
default:
// Ignore differences in path slashes on platforms
/* Ignore differences in path slashes on platforms */
if ((*path == '\\' && *wildcard == '/') || (*path == '/' && *wildcard == '\\'))
break;
@@ -150,7 +112,7 @@ int32_t mz_path_resolve(const char *path, char *output, int32_t max_output)
if ((source == path) || (check != source))
{
// Skip double paths
/* Skip double paths */
if ((*check == '\\') || (*check == '/'))
{
source += 1;
@@ -160,10 +122,10 @@ int32_t mz_path_resolve(const char *path, char *output, int32_t max_output)
{
check += 1;
// Remove current directory . if at end of stirng
/* Remove current directory . if at end of string */
if ((*check == 0) && (source != path))
{
// Copy last slash
/* Copy last slash */
*target = *source;
target += 1;
max_output -= 1;
@@ -171,10 +133,10 @@ int32_t mz_path_resolve(const char *path, char *output, int32_t max_output)
continue;
}
// Remove current directory . if not at end of stirng
/* Remove current directory . if not at end of string */
if ((*check == 0) || (*check == '\\' || *check == '/'))
{
// Only proceed if .\ is not entire string
{
/* Only proceed if .\ is not entire string */
if (check[1] != 0 || (path != source))
{
source += (check - source);
@@ -182,7 +144,7 @@ int32_t mz_path_resolve(const char *path, char *output, int32_t max_output)
}
}
// Go to parent directory ..
/* Go to parent directory .. */
if ((*check != 0) || (*check == '.'))
{
check += 1;
@@ -190,7 +152,7 @@ int32_t mz_path_resolve(const char *path, char *output, int32_t max_output)
{
source += (check - source);
// Search backwards for previous slash
/* Search backwards for previous slash */
if (target != output)
{
target -= 1;
@@ -204,7 +166,7 @@ int32_t mz_path_resolve(const char *path, char *output, int32_t max_output)
}
while (target > output);
}
if ((target == output) && (*source != 0))
source += 1;
if ((*target == '\\' || *target == '/') && (*source == 0))
@@ -232,14 +194,14 @@ int32_t mz_path_resolve(const char *path, char *output, int32_t max_output)
return MZ_OK;
}
int32_t mz_path_remove_filename(const char *path)
int32_t mz_path_remove_filename(char *path)
{
char *path_ptr = NULL;
if (path == NULL)
return MZ_PARAM_ERROR;
path_ptr = (char *)(path + strlen(path) - 1);
path_ptr = path + strlen(path) - 1;
while (path_ptr > path)
{
@@ -251,6 +213,10 @@ int32_t mz_path_remove_filename(const char *path)
path_ptr -= 1;
}
if (path_ptr == path)
*path_ptr = 0;
return MZ_OK;
}
@@ -288,7 +254,7 @@ int32_t mz_dir_make(const char *path)
if (len <= 0)
return 0;
current_dir = (char *)MZ_ALLOC(len + 1);
current_dir = (char *)MZ_ALLOC((uint16_t)len + 1);
if (current_dir == NULL)
return MZ_MEM_ERROR;
@@ -326,81 +292,39 @@ int32_t mz_dir_make(const char *path)
int32_t mz_file_get_crc(const char *path, uint32_t *result_crc)
{
void *stream = NULL;
void *crc32_stream = NULL;
uint32_t crc32 = 0;
int32_t read = 0;
int32_t err = MZ_OK;
uint8_t buf[INT16_MAX];
uint8_t buf[16384];
mz_stream_os_create(&stream);
err = mz_stream_os_open(stream, path, MZ_OPEN_MODE_READ);
mz_stream_crc32_create(&crc32_stream);
mz_stream_crc32_open(crc32_stream, NULL, MZ_OPEN_MODE_READ);
mz_stream_set_base(crc32_stream, stream);
if (err == MZ_OK)
{
do
{
read = mz_stream_crc32_read(crc32_stream, buf, sizeof(buf));
read = mz_stream_os_read(stream, buf, sizeof(buf));
if (read < 0)
{
err = read;
break;
}
crc32 = mz_crypt_crc32_update(crc32, buf, read);
}
while ((err == MZ_OK) && (read > 0));
mz_stream_os_close(stream);
}
mz_stream_crc32_close(crc32_stream);
*result_crc = mz_stream_crc32_get_value(crc32_stream);
mz_stream_crc32_delete(&crc32_stream);
*result_crc = crc32;
mz_stream_os_delete(&stream);
return err;
}
int32_t mz_encoding_cp437_to_utf8(const char *source, char *target, int32_t max_target)
{
uint32_t utf8_char = 0;
uint8_t utf8_byte = 0;
uint8_t cp437_char = 0;
int32_t i = 0;
int32_t x = 0;
int32_t written = 0;
// Convert ibm codepage 437 encoding to utf-8
while (*source != 0)
{
cp437_char = *source;
source += 1;
utf8_char = mz_encoding_codepage437_to_utf8[cp437_char];
for (x = 0; x < 32; x += 8)
{
utf8_byte = (uint8_t)(utf8_char >> x);
if (x > 0 && utf8_byte == 0)
continue;
if (max_target <= 1)
break;
target[written] = utf8_byte;
written += 1;
max_target -= 1;
}
}
if (max_target > 0)
{
target[written] = 0;
written += 1;
}
return written;
}
/***************************************************************************/
+88 -23
View File
@@ -1,8 +1,8 @@
/* mz_os.h -- System functions
Version 2.5.2, August 27, 2018
Version 2.8.3, January 26, 2019
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
Copyright (C) 2010-2019 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
This program is distributed under the terms of the same license as zlib.
@@ -12,27 +12,23 @@
#ifndef MZ_OS_H
#define MZ_OS_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/***************************************************************************/
#if !defined(_WIN32) && !defined(MZ_WIN32_API)
#include "mz_os_posix.h"
#include "mz_strm_posix.h"
#else
#include "mz_os_win32.h"
#include "mz_strm_win32.h"
#if defined(__APPLE__)
#define MZ_VERSION_MADEBY_HOST_SYSTEM (MZ_HOST_SYSTEM_OSX_DARWIN)
#elif defined(__unix__)
#define MZ_VERSION_MADEBY_HOST_SYSTEM (MZ_HOST_SYSTEM_UNIX)
#elif defined(_WIN32)
#define MZ_VERSION_MADEBY_HOST_SYSTEM (MZ_HOST_SYSTEM_WINDOWS_NTFS)
#endif
/***************************************************************************/
#ifdef HAVE_LZMA
#define MZ_VERSION_MADEBY_ZIP_VERSION (63)
#elif HAVE_AES
#elif HAVE_WZAES
#define MZ_VERSION_MADEBY_ZIP_VERSION (51)
#elif HAVE_BZIP2
#define MZ_VERSION_MADEBY_ZIP_VERSION (46)
@@ -45,29 +41,98 @@ extern "C" {
/***************************************************************************/
#if defined(_WIN32)
struct dirent {
char d_name[256];
};
typedef void* DIR;
#else
#include <dirent.h>
#endif
/***************************************************************************/
/* Shared functions */
int32_t mz_path_combine(char *path, const char *join, int32_t max_path);
// Combines two paths
/* Combines two paths */
int32_t mz_path_compare_wc(const char *path, const char *wildcard, uint8_t ignore_case);
// Compare two paths with wildcard
/* Compare two paths with wildcard */
int32_t mz_path_resolve(const char *path, char *target, int32_t max_target);
// Resolves path
/* Resolves path */
int32_t mz_path_remove_filename(const char *path);
// Remove the filename from a path
int32_t mz_path_remove_filename(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
/* Get the filename from a path */
int32_t mz_dir_make(const char *path);
// Creates a directory recursively
/* Creates a directory recursively */
int32_t mz_file_get_crc(const char *path, uint32_t *result_crc);
// Gets the crc32 hash of a file
/* Gets the crc32 hash of a file */
int32_t mz_encoding_cp437_to_utf8(const char *source, char *target, int32_t max_target);
// Converts ibm cp437 encoded string to utf8
/***************************************************************************/
/* Platform specific functions */
wchar_t *mz_os_unicode_string_create(const char *string, int32_t encoding);
/* Create unicode string from a utf8 string */
void mz_os_unicode_string_delete(wchar_t **string);
/* Delete a unicode string that was created */
uint8_t *mz_os_utf8_string_create(const char *string, int32_t encoding);
/* Create a utf8 string from a string with another encoding */
void mz_os_utf8_string_delete(uint8_t **string);
/* Delete a utf8 string that was created */
int32_t mz_os_rand(uint8_t *buf, int32_t size);
/* Random number generator (not cryptographically secure) */
int32_t mz_os_rename(const char *source_path, const char *target_path);
/* Rename a file */
int32_t mz_os_delete(const char *path);
/* Delete an existing file */
int32_t mz_os_file_exists(const char *path);
/* Check to see if a file exists */
int64_t mz_os_get_file_size(const char *path);
/* Gets the length of a file */
int32_t mz_os_get_file_date(const char *path, time_t *modified_date, time_t *accessed_date, time_t *creation_date);
/* Gets a file's modified, access, and creation dates if supported */
int32_t mz_os_set_file_date(const char *path, time_t modified_date, time_t accessed_date, time_t creation_date);
/* Sets a file's modified, access, and creation dates if supported */
int32_t mz_os_get_file_attribs(const char *path, uint32_t *attributes);
/* Gets a file's attributes */
int32_t mz_os_set_file_attribs(const char *path, uint32_t attributes);
/* Sets a file's attributes */
int32_t mz_os_make_dir(const char *path);
/* Recursively creates a directory */
DIR* mz_os_open_dir(const char *path);
/* Opens a directory for listing */
struct
dirent* mz_os_read_dir(DIR *dir);
/* Reads a directory listing entry */
int32_t mz_os_close_dir(DIR *dir);
/* Closes a directory that has been opened for listing */
int32_t mz_os_is_dir(const char *path);
/* Checks to see if path is a directory */
uint64_t mz_os_ms_time(void);
/* Gets the time in milliseconds */
/***************************************************************************/
+144 -105
View File
@@ -1,97 +1,107 @@
/* mz_os_posix.c -- System functions for posix
Version 2.5.2, August 27, 2018
Version 2.8.3, January 26, 2019
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
Copyright (C) 2010-2019 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.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef HAVE_GETRANDOM
# include <sys/random.h>
#endif
#if defined unix || defined __APPLE__
# include <unistd.h>
# include <utime.h>
# define HAVE_ARC4RANDOM_BUF
#endif
#if defined(HAVE_LIBBSD) && \
!defined(MZ_ZIP_NO_COMPRESSION) && \
!defined(MZ_ZIP_NO_ENCRYPTION)
# include <bsd/stdlib.h> // arc4random_buf
#else
# include <stdlib.h>
#endif
#include "mz.h"
#include "mz_strm.h"
#include "mz_os.h"
#include "mz_os_posix.h"
#include <stdio.h> /* rename */
#include <errno.h>
#include <iconv.h>
#include <sys/types.h>
#include <sys/stat.h>
#if defined(__APPLE__) || defined(__unix__)
# include <utime.h>
# include <unistd.h>
#endif
#if defined(__APPLE__)
# include <mach/clock.h>
# include <mach/mach.h>
#endif
/***************************************************************************/
#if !defined(MZ_ZIP_NO_COMPRESSION) && !defined(MZ_ZIP_NO_ENCRYPTION)
#if defined(HAVE_ARC4RANDOM_BUF)
int32_t mz_posix_rand(uint8_t *buf, int32_t size)
uint8_t *mz_os_utf8_string_create(const char *string, int32_t encoding)
{
arc4random_buf(buf, size);
return size;
iconv_t cd;
const char *from_encoding = NULL;
int32_t result = 0;
size_t string_length = 0;
size_t string_utf8_size = 0;
uint8_t *string_utf8 = NULL;
uint8_t *string_utf8_ptr = NULL;
if (string == NULL)
return NULL;
if (encoding == MZ_ENCODING_CODEPAGE_437)
from_encoding = "CP437";
else if (encoding == MZ_ENCODING_CODEPAGE_932)
from_encoding = "CP932";
else if (encoding == MZ_ENCODING_CODEPAGE_936)
from_encoding = "CP936";
else if (encoding == MZ_ENCODING_CODEPAGE_950)
from_encoding = "CP950";
else if (encoding == MZ_ENCODING_UTF8)
from_encoding = "UTF-8";
else
return NULL;
cd = iconv_open("UTF-8", from_encoding);
if (cd == (iconv_t)-1)
return NULL;
string_length = strlen(string);
string_utf8_size = string_length * 2;
string_utf8 = (uint8_t *)MZ_ALLOC((int32_t)(string_utf8_size + 1));
string_utf8_ptr = string_utf8;
if (string_utf8)
{
memset(string_utf8, 0, string_utf8_size + 1);
result = iconv(cd, (char **)&string, &string_length,
(char **)&string_utf8_ptr, &string_utf8_size);
iconv_close(cd);
}
if (result == -1)
{
MZ_FREE(string_utf8);
string_utf8 = NULL;
}
return string_utf8;
}
#elif defined(HAVE_ARC4RANDOM)
int32_t mz_posix_rand(uint8_t *buf, int32_t size)
void mz_os_utf8_string_delete(uint8_t **string)
{
int32_t left = size;
for (; left > 2; left -= 3, buf += 3)
if (string != NULL)
{
uint32_t val = arc4random();
buf[0] = (val) & 0xFF;
buf[1] = (val >> 8) & 0xFF;
buf[2] = (val >> 16) & 0xFF;
MZ_FREE(*string);
*string = NULL;
}
for (; left > 0; left--, buf++)
{
*buf = arc4random() & 0xFF;
}
return size - left;
}
#elif defined(HAVE_GETRANDOM)
int32_t mz_posix_rand(uint8_t *buf, int32_t size)
{
int32_t left = size;
int32_t written = 0;
while (left > 0)
{
written = getrandom(buf, left, 0);
if (written < 0)
return MZ_INTERNAL_ERROR;
/***************************************************************************/
buf += written;
left -= written;
}
return size - left;
}
#else
#if !defined(FORCE_LOWQUALITY_ENTROPY)
# error "Low quality entropy function used for encryption"
#endif
int32_t mz_posix_rand(uint8_t *buf, int32_t size)
int32_t mz_os_rand(uint8_t *buf, int32_t size)
{
static unsigned calls = 0;
int32_t i = 0;
// Ensure different random header each time
/* Ensure different random header each time */
if (++calls == 1)
{
#define PI_SEED 3141592654UL
@@ -103,10 +113,8 @@ int32_t mz_posix_rand(uint8_t *buf, int32_t size)
return size;
}
#endif
#endif
int32_t mz_posix_rename(const char *source_path, const char *target_path)
int32_t mz_os_rename(const char *source_path, const char *target_path)
{
if (rename(source_path, target_path) == -1)
return MZ_EXIST_ERROR;
@@ -114,7 +122,7 @@ int32_t mz_posix_rename(const char *source_path, const char *target_path)
return MZ_OK;
}
int32_t mz_posix_delete(const char *path)
int32_t mz_os_delete(const char *path)
{
if (unlink(path) == -1)
return MZ_EXIST_ERROR;
@@ -122,54 +130,60 @@ int32_t mz_posix_delete(const char *path)
return MZ_OK;
}
int32_t mz_posix_file_exists(const char *path)
int32_t mz_os_file_exists(const char *path)
{
struct stat stat_info;
struct stat path_stat;
memset(&stat_info, 0, sizeof(stat_info));
if (stat(path, &stat_info) == 0)
memset(&path_stat, 0, sizeof(path_stat));
if (stat(path, &path_stat) == 0)
return MZ_OK;
return MZ_EXIST_ERROR;
}
int64_t mz_posix_get_file_size(const char *path)
int64_t mz_os_get_file_size(const char *path)
{
struct stat stat_info;
struct stat path_stat;
memset(&stat_info, 0, sizeof(stat_info));
if (stat(path, &stat_info) == 0)
return stat_info.st_size;
memset(&path_stat, 0, sizeof(path_stat));
if (stat(path, &path_stat) == 0)
{
/* Stat returns size taken up by directory entry, so return 0 */
if (S_ISDIR(path_stat.st_mode))
return 0;
return path_stat.st_size;
}
return 0;
}
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_os_get_file_date(const char *path, time_t *modified_date, time_t *accessed_date, time_t *creation_date)
{
struct stat stat_info;
struct stat path_stat;
char *name = NULL;
size_t len = 0;
int32_t err = MZ_INTERNAL_ERROR;
memset(&stat_info, 0, sizeof(stat_info));
memset(&path_stat, 0, sizeof(path_stat));
if (strcmp(path, "-") != 0)
{
// Not all systems allow stat'ing a file with / appended
/* Not all systems allow stat'ing a file with / appended */
len = strlen(path);
name = (char *)malloc(len + 1);
strncpy(name, path, len + 1);
strncpy(name, path, len);
name[len] = 0;
if (name[len - 1] == '/')
if (name[len - 1] == '/' || name[len - 1] == '\\')
name[len - 1] = 0;
if (stat(name, &stat_info) == 0)
if (stat(name, &path_stat) == 0)
{
if (modified_date != NULL)
*modified_date = stat_info.st_mtime;
*modified_date = path_stat.st_mtime;
if (accessed_date != NULL)
*accessed_date = stat_info.st_atime;
// Creation date not supported
*accessed_date = path_stat.st_atime;
/* Creation date not supported */
if (creation_date != NULL)
*creation_date = 0;
@@ -182,14 +196,15 @@ int32_t mz_posix_get_file_date(const char *path, time_t *modified_date, time_t *
return err;
}
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_os_set_file_date(const char *path, time_t modified_date, time_t accessed_date, time_t creation_date)
{
struct utimbuf ut;
ut.actime = accessed_date;
ut.modtime = modified_date;
// Creation date not supported
(void)creation_date;
/* Creation date not supported */
MZ_UNUSED(creation_date);
if (utime(path, &ut) != 0)
return MZ_INTERNAL_ERROR;
@@ -197,19 +212,19 @@ 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, uint32_t *attributes)
int32_t mz_os_get_file_attribs(const char *path, uint32_t *attributes)
{
struct stat stat_info;
struct stat path_stat;
int32_t err = MZ_OK;
memset(&stat_info, 0, sizeof(stat_info));
if (stat(path, &stat_info) == -1)
memset(&path_stat, 0, sizeof(path_stat));
if (stat(path, &path_stat) == -1)
err = MZ_INTERNAL_ERROR;
*attributes = stat_info.st_mode;
*attributes = path_stat.st_mode;
return err;
}
int32_t mz_posix_set_file_attribs(const char *path, uint32_t attributes)
int32_t mz_os_set_file_attribs(const char *path, uint32_t attributes)
{
int32_t err = MZ_OK;
@@ -219,7 +234,7 @@ int32_t mz_posix_set_file_attribs(const char *path, uint32_t attributes)
return err;
}
int32_t mz_posix_make_dir(const char *path)
int32_t mz_os_make_dir(const char *path)
{
int32_t err = 0;
@@ -231,19 +246,19 @@ int32_t mz_posix_make_dir(const char *path)
return MZ_OK;
}
DIR* mz_posix_open_dir(const char *path)
DIR* mz_os_open_dir(const char *path)
{
return opendir(path);
}
struct dirent* mz_posix_read_dir(DIR *dir)
struct dirent* mz_os_read_dir(DIR *dir)
{
if (dir == NULL)
return NULL;
return readdir(dir);
}
int32_t mz_posix_close_dir(DIR *dir)
int32_t mz_os_close_dir(DIR *dir)
{
if (dir == NULL)
return MZ_PARAM_ERROR;
@@ -252,11 +267,35 @@ int32_t mz_posix_close_dir(DIR *dir)
return MZ_OK;
}
int32_t mz_posix_is_dir(const char *path)
int32_t mz_os_is_dir(const char *path)
{
struct stat path_stat;
memset(&path_stat, 0, sizeof(path_stat));
stat(path, &path_stat);
if (S_ISDIR(path_stat.st_mode))
return MZ_OK;
return MZ_EXIST_ERROR;
}
uint64_t mz_os_ms_time(void)
{
struct timespec ts;
#if defined(__APPLE__)
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
ts.tv_sec = mts.tv_sec;
ts.tv_nsec = mts.tv_nsec;
#else
clock_gettime(CLOCK_MONOTONIC, &ts);
#endif
return ((uint64_t)ts.tv_sec * 1000) + ((uint64_t)ts.tv_nsec / 1000000);
}
-72
View File
@@ -1,72 +0,0 @@
/* mz_os_posix.h -- System functions for posix
Version 2.5.2, August 27, 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_OS_POSIX_H
#define MZ_OS_POSIX_H
#include <stdint.h>
#include <time.h>
#include <dirent.h>
#ifdef __cplusplus
extern "C" {
#endif
/***************************************************************************/
#if defined(__APPLE__)
#define MZ_VERSION_MADEBY_HOST_SYSTEM (MZ_HOST_SYSTEM_OSX_DARWIN)
#elif defined(unix)
#define MZ_VERSION_MADEBY_HOST_SYSTEM (MZ_HOST_SYSTEM_UNIX)
#endif
/***************************************************************************/
int32_t mz_posix_rand(uint8_t *buf, int32_t size);
int32_t mz_posix_rename(const char *source_path, const char *target_path);
int32_t mz_posix_delete(const char *path);
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, 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
dirent* mz_posix_read_dir(DIR *dir);
int32_t mz_posix_close_dir(DIR *dir);
int32_t mz_posix_is_dir(const char *path);
/***************************************************************************/
#define mz_os_rand mz_posix_rand
#define mz_os_rename mz_posix_rename
#define mz_os_delete mz_posix_delete
#define mz_os_file_exists mz_posix_file_exists
#define mz_os_get_file_size mz_posix_get_file_size
#define mz_os_get_file_date mz_posix_get_file_date
#define mz_os_set_file_date mz_posix_set_file_date
#define mz_os_get_file_attribs mz_posix_get_file_attribs
#define mz_os_set_file_attribs mz_posix_set_file_attribs
#define mz_os_make_dir mz_posix_make_dir
#define mz_os_open_dir mz_posix_open_dir
#define mz_os_read_dir mz_posix_read_dir
#define mz_os_close_dir mz_posix_close_dir
#define mz_os_is_dir mz_posix_is_dir
/***************************************************************************/
#ifdef __cplusplus
}
#endif
#endif
+204 -106
View File
@@ -1,30 +1,20 @@
/* mz_os_win32.c -- System functions for Windows
Version 2.5.2, August 27, 2018
Version 2.8.3, January 26, 2019
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
Copyright (C) 2010-2019 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.
*/
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <time.h>
#include <direct.h>
#include <errno.h>
#include <windows.h>
#if !defined(MZ_ZIP_NO_COMPRESSION) && !defined(MZ_ZIP_NO_ENCRYPTION)
# include <wincrypt.h>
#endif
#include "mz.h"
#include "mz_os.h"
#include "mz_os_win32.h"
#include "mz_strm_os.h"
#include <windows.h>
/***************************************************************************/
@@ -45,22 +35,72 @@ typedef struct DIR_int_s {
/***************************************************************************/
#if !defined(MZ_ZIP_NO_COMPRESSION) && !defined(MZ_ZIP_NO_ENCRYPTION)
int32_t mz_win32_rand(uint8_t *buf, int32_t size)
wchar_t *mz_os_unicode_string_create(const char *string, int32_t encoding)
{
wchar_t *string_wide = NULL;
uint32_t string_wide_size = 0;
string_wide_size = MultiByteToWideChar(encoding, 0, string, -1, NULL, 0);
if (string_wide_size == 0)
return NULL;
string_wide = (wchar_t *)MZ_ALLOC((string_wide_size + 1) * sizeof(wchar_t));
if (string_wide == NULL)
return NULL;
memset(string_wide, 0, sizeof(wchar_t) * (string_wide_size + 1));
MultiByteToWideChar(encoding, 0, string, -1, string_wide, string_wide_size);
return string_wide;
}
void mz_os_unicode_string_delete(wchar_t **string)
{
if (string != NULL)
{
MZ_FREE(*string);
*string = NULL;
}
}
uint8_t *mz_os_utf8_string_create(const char *string, int32_t encoding)
{
wchar_t *string_wide = NULL;
uint8_t *string_utf8 = NULL;
uint32_t string_utf8_size = 0;
string_wide = mz_os_unicode_string_create(string, encoding);
if (string_wide)
{
string_utf8_size = WideCharToMultiByte(CP_UTF8, 0, string_wide, -1, NULL, 0, NULL, NULL);
string_utf8 = (uint8_t *)MZ_ALLOC((string_utf8_size + 1) * sizeof(wchar_t));
if (string_utf8)
{
memset(string_utf8, 0, string_utf8_size + 1);
WideCharToMultiByte(CP_UTF8, 0, string_wide, -1, (char *)string_utf8, string_utf8_size, NULL, NULL);
}
mz_os_unicode_string_delete(&string_wide);
}
return string_utf8;
}
void mz_os_utf8_string_delete(uint8_t **string)
{
if (string != NULL)
{
MZ_FREE(*string);
*string = NULL;
}
}
/***************************************************************************/
int32_t mz_os_rand(uint8_t *buf, int32_t size)
{
HCRYPTPROV provider;
unsigned __int64 pentium_tsc[1];
int32_t len = 0;
int32_t result = 0;
if (CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT))
{
result = CryptGenRandom(provider, size, buf);
CryptReleaseContext(provider, 0);
if (result)
return size;
}
for (len = 0; len < (int)size; len += 1)
{
@@ -71,59 +111,57 @@ int32_t mz_win32_rand(uint8_t *buf, int32_t size)
return len;
}
#endif
wchar_t *mz_win32_unicode_path_create(const char *path)
{
wchar_t *path_wide = NULL;
uint32_t path_wide_size = 0;
path_wide_size = MultiByteToWideChar(CP_UTF8, 0, path, -1, NULL, 0);
path_wide = (wchar_t *)MZ_ALLOC((path_wide_size + 1) * sizeof(wchar_t));
memset(path_wide, 0, sizeof(wchar_t) * (path_wide_size + 1));
MultiByteToWideChar(CP_UTF8, 0, path, -1, path_wide, path_wide_size);
return path_wide;
}
void mz_win32_unicode_path_delete(wchar_t **path)
{
if (path != NULL)
{
MZ_FREE(*path);
*path = NULL;
}
}
int32_t mz_win32_rename(const char *source_path, const char *target_path)
int32_t mz_os_rename(const char *source_path, const char *target_path)
{
wchar_t *source_path_wide = NULL;
wchar_t *target_path_wide = NULL;
int32_t result = 0;
int32_t err = MZ_OK;
if (source_path == NULL || target_path == NULL)
return MZ_PARAM_ERROR;
source_path_wide = mz_win32_unicode_path_create(source_path);
target_path_wide = mz_win32_unicode_path_create(target_path);
result = MoveFileW(source_path_wide, target_path_wide);
mz_win32_unicode_path_delete(&source_path_wide);
mz_win32_unicode_path_delete(&target_path_wide);
source_path_wide = mz_os_unicode_string_create(source_path, MZ_ENCODING_UTF8);
if (source_path_wide == NULL)
{
err = MZ_PARAM_ERROR;
}
else
{
target_path_wide = mz_os_unicode_string_create(target_path, MZ_ENCODING_UTF8);
if (target_path_wide == NULL)
err = MZ_PARAM_ERROR;
}
if (result == 0)
return MZ_EXIST_ERROR;
if (err == MZ_OK)
{
result = MoveFileW(source_path_wide, target_path_wide);
if (result == 0)
err = MZ_EXIST_ERROR;
}
return MZ_OK;
if (target_path_wide)
mz_os_unicode_string_delete(&target_path_wide);
if (source_path_wide)
mz_os_unicode_string_delete(&source_path_wide);
return err;
}
int32_t mz_win32_delete(const char *path)
int32_t mz_os_delete(const char *path)
{
wchar_t *path_wide = NULL;
int32_t result = 0;
if (path == NULL)
return MZ_PARAM_ERROR;
path_wide = mz_os_unicode_string_create(path, MZ_ENCODING_UTF8);
if (path_wide == NULL)
return MZ_PARAM_ERROR;
path_wide = mz_win32_unicode_path_create(path);
result = DeleteFileW(path_wide);
mz_win32_unicode_path_delete(&path_wide);
mz_os_unicode_string_delete(&path_wide);
if (result == 0)
return MZ_EXIST_ERROR;
@@ -131,15 +169,19 @@ int32_t mz_win32_delete(const char *path)
return MZ_OK;
}
int32_t mz_win32_file_exists(const char *path)
int32_t mz_os_file_exists(const char *path)
{
wchar_t *path_wide = NULL;
DWORD attribs = 0;
if (path == NULL)
return MZ_PARAM_ERROR;
path_wide = mz_os_unicode_string_create(path, MZ_ENCODING_UTF8);
if (path_wide == NULL)
return MZ_PARAM_ERROR;
path_wide = mz_win32_unicode_path_create(path);
attribs = GetFileAttributesW(path_wide);
mz_win32_unicode_path_delete(&path_wide);
mz_os_unicode_string_delete(&path_wide);
if (attribs == 0xFFFFFFFF)
return MZ_EXIST_ERROR;
@@ -147,20 +189,23 @@ int32_t mz_win32_file_exists(const char *path)
return MZ_OK;
}
int64_t mz_win32_get_file_size(const char *path)
int64_t mz_os_get_file_size(const char *path)
{
HANDLE handle = NULL;
LARGE_INTEGER large_size;
wchar_t *path_wide = NULL;
path_wide = mz_win32_unicode_path_create(path);
if (path == NULL)
return MZ_PARAM_ERROR;
path_wide = mz_os_unicode_string_create(path, MZ_ENCODING_UTF8);
if (path_wide == NULL)
return MZ_PARAM_ERROR;
#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);
#endif
mz_win32_unicode_path_delete(&path_wide);
mz_os_unicode_string_delete(&path_wide);
large_size.QuadPart = 0;
@@ -173,7 +218,7 @@ int64_t mz_win32_get_file_size(const char *path)
return large_size.QuadPart;
}
static void mz_win32_file_to_unix_time(FILETIME file_time, time_t *unix_time)
static void mz_os_file_to_unix_time(FILETIME file_time, time_t *unix_time)
{
uint64_t quad_file_time = 0;
quad_file_time = file_time.dwLowDateTime;
@@ -181,7 +226,7 @@ static void mz_win32_file_to_unix_time(FILETIME file_time, time_t *unix_time)
*unix_time = (time_t)((quad_file_time - 116444736000000000LL) / 10000000);
}
static void mz_win32_unix_to_file_time(time_t unix_time, FILETIME *file_time)
static void mz_os_unix_to_file_time(time_t unix_time, FILETIME *file_time)
{
uint64_t quad_file_time = 0;
quad_file_time = ((uint64_t)unix_time * 10000000) + 116444736000000000LL;
@@ -189,25 +234,30 @@ static void mz_win32_unix_to_file_time(time_t unix_time, FILETIME *file_time)
file_time->dwLowDateTime = (uint32_t)(quad_file_time);
}
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_os_get_file_date(const char *path, time_t *modified_date, time_t *accessed_date, time_t *creation_date)
{
WIN32_FIND_DATAW ff32;
HANDLE handle = NULL;
wchar_t *path_wide = NULL;
int32_t err = MZ_INTERNAL_ERROR;
path_wide = mz_win32_unicode_path_create(path);
if (path == NULL)
return MZ_PARAM_ERROR;
path_wide = mz_os_unicode_string_create(path, MZ_ENCODING_UTF8);
if (path_wide == NULL)
return MZ_PARAM_ERROR;
handle = FindFirstFileW(path_wide, &ff32);
MZ_FREE(path_wide);
if (handle != INVALID_HANDLE_VALUE)
{
if (modified_date != NULL)
mz_win32_file_to_unix_time(ff32.ftLastWriteTime, modified_date);
mz_os_file_to_unix_time(ff32.ftLastWriteTime, modified_date);
if (accessed_date != NULL)
mz_win32_file_to_unix_time(ff32.ftLastAccessTime, accessed_date);
mz_os_file_to_unix_time(ff32.ftLastAccessTime, accessed_date);
if (creation_date != NULL)
mz_win32_file_to_unix_time(ff32.ftCreationTime, creation_date);
mz_os_file_to_unix_time(ff32.ftCreationTime, creation_date);
FindClose(handle);
err = MZ_OK;
@@ -216,32 +266,36 @@ int32_t mz_win32_get_file_date(const char *path, time_t *modified_date, time_t *
return err;
}
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_os_set_file_date(const char *path, time_t modified_date, time_t accessed_date, time_t creation_date)
{
HANDLE handle = NULL;
FILETIME ftm_creation, ftm_accessed, ftm_modified;
wchar_t *path_wide = NULL;
int32_t err = MZ_OK;
if (path == NULL)
return MZ_PARAM_ERROR;
path_wide = mz_os_unicode_string_create(path, MZ_ENCODING_UTF8);
if (path_wide == NULL)
return MZ_PARAM_ERROR;
path_wide = mz_win32_unicode_path_create(path);
#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);
#endif
mz_win32_unicode_path_delete(&path_wide);
mz_os_unicode_string_delete(&path_wide);
if (handle != INVALID_HANDLE_VALUE)
{
GetFileTime(handle, &ftm_creation, &ftm_accessed, &ftm_modified);
if (modified_date != 0)
mz_win32_unix_to_file_time(modified_date, &ftm_modified);
mz_os_unix_to_file_time(modified_date, &ftm_modified);
if (accessed_date != 0)
mz_win32_unix_to_file_time(accessed_date, &ftm_accessed);
mz_os_unix_to_file_time(accessed_date, &ftm_accessed);
if (creation_date != 0)
mz_win32_unix_to_file_time(creation_date, &ftm_creation);
mz_os_unix_to_file_time(creation_date, &ftm_creation);
if (SetFileTime(handle, &ftm_creation, &ftm_accessed, &ftm_modified) == 0)
err = MZ_INTERNAL_ERROR;
@@ -252,14 +306,19 @@ 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, uint32_t *attributes)
int32_t mz_os_get_file_attribs(const char *path, uint32_t *attributes)
{
wchar_t *path_wide = NULL;
int32_t err = MZ_OK;
path_wide = mz_win32_unicode_path_create(path);
if (path == NULL || attributes == NULL)
return MZ_PARAM_ERROR;
path_wide = mz_os_unicode_string_create(path, MZ_ENCODING_UTF8);
if (path_wide == NULL)
return MZ_PARAM_ERROR;
*attributes = GetFileAttributesW(path_wide);
MZ_FREE(path_wide);
mz_os_unicode_string_delete(&path_wide);
if (*attributes == INVALID_FILE_ATTRIBUTES)
err = MZ_INTERNAL_ERROR;
@@ -267,36 +326,47 @@ int32_t mz_win32_get_file_attribs(const char *path, uint32_t *attributes)
return err;
}
int32_t mz_win32_set_file_attribs(const char *path, uint32_t attributes)
int32_t mz_os_set_file_attribs(const char *path, uint32_t attributes)
{
wchar_t *path_wide = NULL;
int32_t err = MZ_OK;
path_wide = mz_win32_unicode_path_create(path);
if (path == NULL)
return MZ_PARAM_ERROR;
path_wide = mz_os_unicode_string_create(path, MZ_ENCODING_UTF8);
if (path_wide == NULL)
return MZ_PARAM_ERROR;
if (SetFileAttributesW(path_wide, attributes) == 0)
err = MZ_INTERNAL_ERROR;
MZ_FREE(path_wide);
mz_os_unicode_string_delete(&path_wide);
return err;
}
int32_t mz_win32_make_dir(const char *path)
int32_t mz_os_make_dir(const char *path)
{
wchar_t *path_wide = NULL;
int32_t err = 0;
int32_t err = MZ_OK;
if (path == NULL)
return MZ_PARAM_ERROR;
path_wide = mz_os_unicode_string_create(path, MZ_ENCODING_UTF8);
if (path_wide == NULL)
return MZ_PARAM_ERROR;
path_wide = mz_win32_unicode_path_create(path);
err = _wmkdir(path_wide);
mz_win32_unicode_path_delete(&path_wide);
if (CreateDirectoryW(path_wide, NULL) == 0)
{
if (GetLastError() != ERROR_ALREADY_EXISTS)
err = MZ_INTERNAL_ERROR;
}
if (err != 0 && errno != EEXIST)
return MZ_INTERNAL_ERROR;
mz_os_unicode_string_delete(&path_wide);
return MZ_OK;
return err;
}
DIR *mz_win32_open_dir(const char *path)
DIR *mz_os_open_dir(const char *path)
{
WIN32_FIND_DATAW find_data;
DIR_int *dir_int = NULL;
@@ -305,18 +375,26 @@ DIR *mz_win32_open_dir(const char *path)
void *handle = NULL;
if (path == NULL)
return NULL;
fixed_path[0] = 0;
mz_path_combine(fixed_path, path, sizeof(fixed_path));
mz_path_combine(fixed_path, "*", sizeof(fixed_path));
path_wide = mz_win32_unicode_path_create(fixed_path);
path_wide = mz_os_unicode_string_create(fixed_path, MZ_ENCODING_UTF8);
if (path_wide == NULL)
return NULL;
handle = FindFirstFileW(path_wide, &find_data);
mz_win32_unicode_path_delete(&path_wide);
mz_os_unicode_string_delete(&path_wide);
if (handle == INVALID_HANDLE_VALUE)
return NULL;
dir_int = (DIR_int *)MZ_ALLOC(sizeof(DIR_int));
if (dir_int == NULL)
return NULL;
dir_int->find_handle = handle;
dir_int->end = 0;
@@ -325,7 +403,7 @@ DIR *mz_win32_open_dir(const char *path)
return (DIR *)dir_int;
}
struct dirent* mz_win32_read_dir(DIR *dir)
struct dirent* mz_os_read_dir(DIR *dir)
{
DIR_int *dir_int;
@@ -350,7 +428,7 @@ struct dirent* mz_win32_read_dir(DIR *dir)
return &dir_int->entry;
}
int32_t mz_win32_close_dir(DIR *dir)
int32_t mz_os_close_dir(DIR *dir)
{
DIR_int *dir_int;
@@ -364,14 +442,19 @@ int32_t mz_win32_close_dir(DIR *dir)
return MZ_OK;
}
int32_t mz_win32_is_dir(const char *path)
int32_t mz_os_is_dir(const char *path)
{
wchar_t *path_wide = NULL;
uint32_t attribs = 0;
path_wide = mz_win32_unicode_path_create(path);
if (path == NULL)
return MZ_PARAM_ERROR;
path_wide = mz_os_unicode_string_create(path, MZ_ENCODING_UTF8);
if (path_wide == NULL)
return MZ_PARAM_ERROR;
attribs = GetFileAttributesW(path_wide);
mz_win32_unicode_path_delete(&path_wide);
mz_os_unicode_string_delete(&path_wide);
if (attribs != 0xFFFFFFFF)
{
@@ -381,3 +464,18 @@ int32_t mz_win32_is_dir(const char *path)
return MZ_EXIST_ERROR;
}
uint64_t mz_os_ms_time(void)
{
SYSTEMTIME system_time;
FILETIME file_time;
uint64_t quad_file_time = 0;
GetSystemTime(&system_time);
SystemTimeToFileTime(&system_time, &file_time);
quad_file_time = file_time.dwLowDateTime;
quad_file_time |= ((uint64_t)file_time.dwHighDateTime << 32);
return quad_file_time / 10000 - 11644473600000LL;
}
-73
View File
@@ -1,73 +0,0 @@
/* mz_os_win32.h -- System functions for Windows
Version 2.5.2, August 27, 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_OS_WIN32_H
#define MZ_OS_WIN32_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/***************************************************************************/
#define MZ_VERSION_MADEBY_HOST_SYSTEM (MZ_HOST_SYSTEM_WINDOWS_NTFS)
/***************************************************************************/
struct dirent {
char d_name[256];
};
typedef void* DIR;
/***************************************************************************/
int32_t mz_win32_rand(uint8_t *buf, int32_t size);
int32_t mz_win32_rename(const char *source_path, const char *target_path);
int32_t mz_win32_delete(const char *path);
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, 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
dirent* mz_win32_read_dir(DIR *dir);
int32_t mz_win32_close_dir(DIR *dir);
int32_t mz_win32_is_dir(const char *path);
/***************************************************************************/
#define mz_os_rand mz_win32_rand
#define mz_os_rename mz_win32_rename
#define mz_os_delete mz_win32_delete
#define mz_os_file_exists mz_win32_file_exists
#define mz_os_get_file_size mz_win32_get_file_size
#define mz_os_get_file_date mz_win32_get_file_date
#define mz_os_set_file_date mz_win32_set_file_date
#define mz_os_get_file_attribs mz_win32_get_file_attribs
#define mz_os_set_file_attribs mz_win32_set_file_attribs
#define mz_os_make_dir mz_win32_make_dir
#define mz_os_open_dir mz_win32_open_dir
#define mz_os_read_dir mz_win32_read_dir
#define mz_os_close_dir mz_win32_close_dir
#define mz_os_is_dir mz_win32_is_dir
/***************************************************************************/
#ifdef __cplusplus
}
#endif
#endif
+177 -22
View File
@@ -1,23 +1,23 @@
/* mz_strm.c -- Stream interface
Version 2.5.2, August 27, 2018
Version 2.8.3, January 26, 2019
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
Copyright (C) 2010-2019 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.
*/
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "mz.h"
#include "mz_strm.h"
/***************************************************************************/
#define MZ_STREAM_FIND_SIZE (1024)
/***************************************************************************/
int32_t mz_stream_open(void *stream, const char *path, int32_t mode)
{
mz_stream *strm = (mz_stream *)stream;
@@ -100,6 +100,11 @@ int32_t mz_stream_read_uint32(void *stream, uint32_t *value)
return err;
}
int32_t mz_stream_read_int64(void *stream, int64_t *value)
{
return mz_stream_read_value(stream, (uint64_t *)value, sizeof(uint64_t));
}
int32_t mz_stream_read_uint64(void *stream, uint64_t *value)
{
return mz_stream_read_value(stream, value, sizeof(uint64_t));
@@ -130,7 +135,7 @@ static int32_t mz_stream_write_value(void *stream, uint64_t value, int32_t len)
if (value != 0)
{
// Data overflow - hack for ZIP64 (X Roche)
/* Data overflow - hack for ZIP64 (X Roche) */
for (n = 0; n < len; n += 1)
buf[n] = 0xff;
}
@@ -156,35 +161,43 @@ int32_t mz_stream_write_uint32(void *stream, uint32_t value)
return mz_stream_write_value(stream, value, sizeof(uint32_t));
}
int32_t mz_stream_write_int64(void *stream, int64_t value)
{
return mz_stream_write_value(stream, (uint64_t)value, sizeof(uint64_t));
}
int32_t mz_stream_write_uint64(void *stream, uint64_t value)
{
return mz_stream_write_value(stream, value, sizeof(uint64_t));
}
int32_t mz_stream_write_chars(void *stream, const char *value, uint8_t null_terminate)
{
int32_t len = (int32_t)strlen(value);
if (null_terminate)
len += 1;
return mz_stream_write(stream, value, len);
}
int32_t mz_stream_copy(void *target, void *source, int32_t len)
{
uint8_t buf[INT16_MAX];
return mz_stream_copy_stream(target, NULL, source, NULL, len);
}
int32_t mz_stream_copy_stream(void *target, mz_stream_write_cb write_cb, void *source,
mz_stream_read_cb read_cb, int32_t len)
{
uint8_t buf[16384];
int32_t bytes_to_copy = 0;
int32_t read = 0;
int32_t written = 0;
if (write_cb == NULL)
write_cb = mz_stream_write;
if (read_cb == NULL)
read_cb = mz_stream_read;
while (len > 0)
{
bytes_to_copy = len;
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)
read = read_cb(source, buf, bytes_to_copy);
if (read <= 0)
return MZ_STREAM_ERROR;
written = mz_stream_write(target, buf, read);
written = write_cb(target, buf, read);
if (written != read)
return MZ_STREAM_ERROR;
len -= read;
@@ -210,9 +223,142 @@ int32_t mz_stream_seek(void *stream, int64_t offset, int32_t origin)
return MZ_PARAM_ERROR;
if (mz_stream_is_open(stream) != MZ_OK)
return MZ_STREAM_ERROR;
if (origin == MZ_SEEK_SET && offset < 0)
return MZ_SEEK_ERROR;
return strm->vtbl->seek(strm, offset, origin);
}
int32_t mz_stream_find(void *stream, const void *find, int32_t find_size, int64_t max_seek, int64_t *position)
{
uint8_t buf[MZ_STREAM_FIND_SIZE];
int32_t buf_pos = 0;
int32_t read_size = sizeof(buf);
int32_t read = 0;
int64_t read_pos = 0;
int64_t start_pos = 0;
int64_t disk_pos = 0;
int32_t i = 0;
uint8_t first = 1;
int32_t err = MZ_OK;
if (stream == NULL || find == NULL || position == NULL)
return MZ_PARAM_ERROR;
if (find_size < 0 || find_size >= (int32_t)sizeof(buf))
return MZ_PARAM_ERROR;
*position = -1;
start_pos = mz_stream_tell(stream);
while (read_pos < max_seek)
{
if (read_size > (int32_t)(max_seek - read_pos - buf_pos))
read_size = (int32_t)(max_seek - read_pos - buf_pos);
read = mz_stream_read(stream, buf + buf_pos, read_size);
if ((read < 0) || (read + buf_pos < find_size))
break;
for (i = 0; i <= read + buf_pos - find_size; i += 1)
{
if (memcmp(&buf[i], find, find_size) != 0)
continue;
disk_pos = mz_stream_tell(stream);
/* Seek to position on disk where the data was found */
err = mz_stream_seek(stream, disk_pos - ((int64_t)read + buf_pos - i), MZ_SEEK_SET);
if (err != MZ_OK)
return MZ_EXIST_ERROR;
*position = start_pos + read_pos + i;
return MZ_OK;
}
if (first)
{
read -= find_size;
read_size -= find_size;
buf_pos = find_size;
first = 0;
}
memmove(buf, buf + read, find_size);
read_pos += read;
}
return MZ_EXIST_ERROR;
}
int32_t mz_stream_find_reverse(void *stream, const void *find, int32_t find_size, int64_t max_seek, int64_t *position)
{
uint8_t buf[MZ_STREAM_FIND_SIZE];
int32_t buf_pos = 0;
int32_t read_size = MZ_STREAM_FIND_SIZE;
int64_t read_pos = 0;
int32_t read = 0;
int64_t start_pos = 0;
int64_t disk_pos = 0;
uint8_t first = 1;
int32_t i = 0;
int32_t err = MZ_OK;
if (stream == NULL || find == NULL || position == NULL)
return MZ_PARAM_ERROR;
if (find_size < 0 || find_size >= (int32_t)sizeof(buf))
return MZ_PARAM_ERROR;
*position = -1;
start_pos = mz_stream_tell(stream);
while (read_pos < max_seek)
{
if (read_size > (int32_t)(max_seek - read_pos))
read_size = (int32_t)(max_seek - read_pos);
if (mz_stream_seek(stream, start_pos - (read_pos + read_size), MZ_SEEK_SET) != MZ_OK)
break;
read = mz_stream_read(stream, buf, read_size);
if ((read < 0) || (read + buf_pos < find_size))
break;
if (read + buf_pos < MZ_STREAM_FIND_SIZE)
memmove(buf + MZ_STREAM_FIND_SIZE - (read + buf_pos), buf, read);
for (i = find_size; i <= (read + buf_pos); i += 1)
{
if (memcmp(&buf[MZ_STREAM_FIND_SIZE - i], find, find_size) != 0)
continue;
disk_pos = mz_stream_tell(stream);
/* Seek to position on disk where the data was found */
err = mz_stream_seek(stream, disk_pos + buf_pos - i, MZ_SEEK_SET);
if (err != MZ_OK)
return MZ_EXIST_ERROR;
*position = start_pos - (read_pos - buf_pos + i);
return MZ_OK;
}
if (first)
{
read -= find_size;
read_size -= find_size;
buf_pos = find_size;
first = 0;
}
if (read == 0)
break;
memmove(buf + read_size, buf, find_size);
read_pos += read;
}
return MZ_EXIST_ERROR;
}
int32_t mz_stream_close(void *stream)
{
mz_stream *strm = (mz_stream *)stream;
@@ -316,14 +462,17 @@ int32_t mz_stream_raw_read(void *stream, void *buf, int32_t size)
if (raw->max_total_in > 0)
{
if ((raw->max_total_in - raw->total_in) < size)
if ((int64_t)bytes_to_read > (raw->max_total_in - raw->total_in))
bytes_to_read = (int32_t)(raw->max_total_in - raw->total_in);
}
read = mz_stream_read(raw->stream.base, buf, bytes_to_read);
if (read > 0)
{
raw->total_in += read;
raw->total_out += read;
}
return read;
}
@@ -331,9 +480,16 @@ int32_t mz_stream_raw_read(void *stream, void *buf, int32_t size)
int32_t mz_stream_raw_write(void *stream, const void *buf, int32_t size)
{
mz_stream_raw *raw = (mz_stream_raw *)stream;
int32_t written = mz_stream_write(raw->stream.base, buf, size);
int32_t written = 0;
written = mz_stream_write(raw->stream.base, buf, size);
if (written > 0)
{
raw->total_out += written;
raw->total_in += written;
}
return written;
}
@@ -352,7 +508,6 @@ int32_t mz_stream_raw_seek(void *stream, int64_t offset, int32_t origin)
int32_t mz_stream_raw_close(void *stream)
{
MZ_UNUSED(stream);
return MZ_OK;
}
+11 -5
View File
@@ -1,8 +1,8 @@
/* mz_strm.h -- Stream interface
Version 2.5.2, August 27, 2018
Version 2.8.3, January 26, 2019
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
Copyright (C) 2010-2019 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
This program is distributed under the terms of the same license as zlib.
@@ -12,8 +12,6 @@
#ifndef MZ_STREAM_H
#define MZ_STREAM_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
@@ -29,6 +27,7 @@ extern "C" {
#define MZ_STREAM_PROP_DISK_SIZE (7)
#define MZ_STREAM_PROP_DISK_NUMBER (8)
#define MZ_STREAM_PROP_COMPRESS_LEVEL (9)
#define MZ_STREAM_PROP_COMPRESS_ALGORITHM (10)
/***************************************************************************/
@@ -46,6 +45,9 @@ typedef void (*mz_stream_destroy_cb) (void **stream);
typedef int32_t (*mz_stream_get_prop_int64_cb) (void *stream, int32_t prop, int64_t *value);
typedef int32_t (*mz_stream_set_prop_int64_cb) (void *stream, int32_t prop, int64_t value);
typedef int32_t (*mz_stream_find_cb) (void *stream, const void *find, int32_t find_size,
int64_t max_seek, int64_t *position);
/***************************************************************************/
typedef struct mz_stream_vtbl_s
@@ -78,16 +80,20 @@ int32_t mz_stream_read(void *stream, void *buf, int32_t size);
int32_t mz_stream_read_uint8(void *stream, uint8_t *value);
int32_t mz_stream_read_uint16(void *stream, uint16_t *value);
int32_t mz_stream_read_uint32(void *stream, uint32_t *value);
int32_t mz_stream_read_int64(void *stream, int64_t *value);
int32_t mz_stream_read_uint64(void *stream, uint64_t *value);
int32_t mz_stream_write(void *stream, const void *buf, int32_t size);
int32_t mz_stream_write_uint8(void *stream, uint8_t value);
int32_t mz_stream_write_uint16(void *stream, uint16_t value);
int32_t mz_stream_write_uint32(void *stream, uint32_t value);
int32_t mz_stream_write_int64(void *stream, int64_t value);
int32_t mz_stream_write_uint64(void *stream, uint64_t value);
int32_t mz_stream_write_chars(void *stream, const char *value, uint8_t null_terminate);
int32_t mz_stream_copy(void *target, void *source, int32_t len);
int32_t mz_stream_copy_stream(void *target, mz_stream_write_cb write_cb, void *source, mz_stream_read_cb read_cb, int32_t len);
int64_t mz_stream_tell(void *stream);
int32_t mz_stream_seek(void *stream, int64_t offset, int32_t origin);
int32_t mz_stream_find(void *stream, const void *find, int32_t find_size, int64_t max_seek, int64_t *position);
int32_t mz_stream_find_reverse(void *stream, const void *find, int32_t find_size, int64_t max_seek, int64_t *position);
int32_t mz_stream_close(void *stream);
int32_t mz_stream_error(void *stream);
-379
View File
@@ -1,379 +0,0 @@
/* mz_strm_aes.c -- Stream for WinZip AES encryption
Version 2.5.2, August 27, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
Copyright (C) 1998-2010 Brian Gladman, Worcester, UK
This program is distributed under the terms of the same license as zlib.
See the accompanying LICENSE file for the full text of the license.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "aes.h"
#include "hmac.h"
#include "pwd2key.h"
#include "mz.h"
#include "mz_os.h"
#include "mz_strm.h"
#include "mz_strm_aes.h"
/***************************************************************************/
#define MZ_AES_KEY_LENGTH(mode) (8 * (mode & 3) + 8)
#define MZ_AES_KEY_LENGTH_MAX (32)
#define MZ_AES_KEYING_ITERATIONS (1000)
#define MZ_AES_SALT_LENGTH(mode) (4 * (mode & 3) + 4)
#define MZ_AES_SALT_LENGTH_MAX (16)
#define MZ_AES_MAC_LENGTH(mode) (10)
#define MZ_AES_PW_LENGTH_MAX (128)
#define MZ_AES_PW_VERIFY_SIZE (2)
#define MZ_AES_AUTHCODE_SIZE (10)
/***************************************************************************/
static mz_stream_vtbl mz_stream_aes_vtbl = {
mz_stream_aes_open,
mz_stream_aes_is_open,
mz_stream_aes_read,
mz_stream_aes_write,
mz_stream_aes_tell,
mz_stream_aes_seek,
mz_stream_aes_close,
mz_stream_aes_error,
mz_stream_aes_create,
mz_stream_aes_delete,
mz_stream_aes_get_prop_int64,
mz_stream_aes_set_prop_int64
};
/***************************************************************************/
typedef struct mz_stream_aes_s {
mz_stream stream;
int32_t mode;
int32_t error;
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;
aes_encrypt_ctx encr_ctx[1];
hmac_ctx auth_ctx[1];
uint8_t nonce[AES_BLOCK_SIZE];
uint8_t encr_bfr[AES_BLOCK_SIZE];
uint32_t encr_pos;
} mz_stream_aes;
/***************************************************************************/
int32_t mz_stream_aes_open(void *stream, const char *path, int32_t mode)
{
mz_stream_aes *aes = (mz_stream_aes *)stream;
uint8_t kbuf[2 * MZ_AES_KEY_LENGTH_MAX + MZ_AES_PW_VERIFY_SIZE];
uint8_t verify[MZ_AES_PW_VERIFY_SIZE];
uint8_t verify_expected[MZ_AES_PW_VERIFY_SIZE];
uint8_t salt_value[MZ_AES_SALT_LENGTH_MAX];
int32_t salt_length = 0;
const char *password = path;
int32_t password_length = 0;
int32_t key_length = 0;
aes->total_in = 0;
aes->total_out = 0;
aes->initialized = 0;
if (mz_stream_is_open(aes->stream.base) != MZ_OK)
return MZ_STREAM_ERROR;
if (password == NULL)
password = aes->password;
if (password == NULL)
return MZ_PARAM_ERROR;
password_length = (int32_t)strlen(password);
if (password_length > MZ_AES_PW_LENGTH_MAX)
return MZ_PARAM_ERROR;
if (aes->encryption_mode < 1 || aes->encryption_mode > 3)
return MZ_PARAM_ERROR;
salt_length = MZ_AES_SALT_LENGTH(aes->encryption_mode);
if (mode & MZ_OPEN_MODE_WRITE)
{
#ifdef MZ_ZIP_NO_COMPRESSION
return MZ_SUPPORT_ERROR;
#else
mz_os_rand(salt_value, salt_length);
#endif
}
else if (mode & MZ_OPEN_MODE_READ)
{
#ifdef MZ_ZIP_NO_DECOMPRESSION
return MZ_SUPPORT_ERROR;
#else
if (mz_stream_read(aes->stream.base, salt_value, salt_length) != salt_length)
return MZ_STREAM_ERROR;
#endif
}
key_length = MZ_AES_KEY_LENGTH(aes->encryption_mode);
// Derive the encryption and authentication keys and the password verifier
derive_key((const uint8_t *)password, password_length, salt_value, salt_length,
MZ_AES_KEYING_ITERATIONS, kbuf, 2 * key_length + MZ_AES_PW_VERIFY_SIZE);
// Initialize the encryption nonce and buffer pos
aes->encr_pos = AES_BLOCK_SIZE;
memset(aes->nonce, 0, AES_BLOCK_SIZE * sizeof(uint8_t));
// Initialize for encryption using key 1
aes_encrypt_key(kbuf, key_length, aes->encr_ctx);
// Initialize for authentication using key 2
hmac_sha_begin(HMAC_SHA1, aes->auth_ctx);
hmac_sha_key(kbuf + key_length, key_length, aes->auth_ctx);
memcpy(verify, kbuf + 2 * key_length, MZ_AES_PW_VERIFY_SIZE);
if (mode & MZ_OPEN_MODE_WRITE)
{
if (mz_stream_write(aes->stream.base, salt_value, salt_length) != salt_length)
return MZ_STREAM_ERROR;
aes->total_out += salt_length;
if (mz_stream_write(aes->stream.base, verify, MZ_AES_PW_VERIFY_SIZE) != MZ_AES_PW_VERIFY_SIZE)
return MZ_STREAM_ERROR;
aes->total_out += MZ_AES_PW_VERIFY_SIZE;
}
else if (mode & MZ_OPEN_MODE_READ)
{
aes->total_in += salt_length;
if (mz_stream_read(aes->stream.base, verify_expected, MZ_AES_PW_VERIFY_SIZE) != MZ_AES_PW_VERIFY_SIZE)
return MZ_STREAM_ERROR;
aes->total_in += MZ_AES_PW_VERIFY_SIZE;
if (memcmp(verify_expected, verify, MZ_AES_PW_VERIFY_SIZE) != 0)
return MZ_PASSWORD_ERROR;
}
aes->mode = mode;
aes->initialized = 1;
return MZ_OK;
}
int32_t mz_stream_aes_is_open(void *stream)
{
mz_stream_aes *aes = (mz_stream_aes *)stream;
if (aes->initialized == 0)
return MZ_STREAM_ERROR;
return MZ_OK;
}
static int32_t mz_stream_aes_encrypt_data(void *stream, uint8_t *buf, int32_t size)
{
mz_stream_aes *aes = (mz_stream_aes *)stream;
uint32_t pos = aes->encr_pos;
uint32_t i = 0;
while (i < (uint32_t)size)
{
if (pos == AES_BLOCK_SIZE)
{
uint32_t j = 0;
// Increment encryption nonce
while (j < 8 && !++aes->nonce[j])
j += 1;
// Encrypt the nonce to form next xor buffer
aes_encrypt(aes->nonce, aes->encr_bfr, aes->encr_ctx);
pos = 0;
}
buf[i++] ^= aes->encr_bfr[pos++];
}
aes->encr_pos = pos;
return MZ_OK;
}
int32_t mz_stream_aes_read(void *stream, void *buf, int32_t size)
{
mz_stream_aes *aes = (mz_stream_aes *)stream;
int32_t read = 0;
read = mz_stream_read(aes->stream.base, buf, size);
if (read > 0)
{
hmac_sha_data((uint8_t *)buf, read, aes->auth_ctx);
mz_stream_aes_encrypt_data(stream, (uint8_t *)buf, read);
}
aes->total_in += read;
return read;
}
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 > (int32_t)sizeof(aes->buffer))
return MZ_STREAM_ERROR;
memcpy(aes->buffer, buf, size);
mz_stream_aes_encrypt_data(stream, (uint8_t *)aes->buffer, size);
hmac_sha_data((uint8_t *)aes->buffer, size, aes->auth_ctx);
written = mz_stream_write(aes->stream.base, aes->buffer, size);
if (written > 0)
aes->total_out += written;
return written;
}
int64_t mz_stream_aes_tell(void *stream)
{
mz_stream_aes *aes = (mz_stream_aes *)stream;
return mz_stream_tell(aes->stream.base);
}
int32_t mz_stream_aes_seek(void *stream, int64_t offset, int32_t origin)
{
mz_stream_aes *aes = (mz_stream_aes *)stream;
return mz_stream_seek(aes->stream.base, offset, origin);
}
int32_t mz_stream_aes_close(void *stream)
{
mz_stream_aes *aes = (mz_stream_aes *)stream;
uint8_t authcode[MZ_AES_AUTHCODE_SIZE];
uint8_t verify_authcode[MZ_AES_AUTHCODE_SIZE];
if (MZ_AES_MAC_LENGTH(aes->encryption_mode) != MZ_AES_AUTHCODE_SIZE)
return MZ_STREAM_ERROR;
hmac_sha_end(authcode, MZ_AES_MAC_LENGTH(aes->encryption_mode), aes->auth_ctx);
if (aes->mode & MZ_OPEN_MODE_WRITE)
{
if (mz_stream_write(aes->stream.base, authcode, MZ_AES_AUTHCODE_SIZE) != MZ_AES_AUTHCODE_SIZE)
return MZ_STREAM_ERROR;
aes->total_out += MZ_AES_AUTHCODE_SIZE;
}
else if (aes->mode & MZ_OPEN_MODE_READ)
{
if (mz_stream_read(aes->stream.base, verify_authcode, MZ_AES_AUTHCODE_SIZE) != MZ_AES_AUTHCODE_SIZE)
return MZ_STREAM_ERROR;
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;
}
aes->initialized = 0;
return MZ_OK;
}
int32_t mz_stream_aes_error(void *stream)
{
mz_stream_aes *aes = (mz_stream_aes *)stream;
return aes->error;
}
void mz_stream_aes_set_password(void *stream, const char *password)
{
mz_stream_aes *aes = (mz_stream_aes *)stream;
aes->password = password;
}
void mz_stream_aes_set_encryption_mode(void *stream, int16_t encryption_mode)
{
mz_stream_aes *aes = (mz_stream_aes *)stream;
aes->encryption_mode = encryption_mode;
}
int32_t mz_stream_aes_get_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:
*value = aes->total_in;
break;
case MZ_STREAM_PROP_TOTAL_OUT:
*value = aes->total_out;
break;
case MZ_STREAM_PROP_TOTAL_IN_MAX:
*value = aes->max_total_in;
break;
case MZ_STREAM_PROP_HEADER_SIZE:
*value = MZ_AES_SALT_LENGTH(aes->encryption_mode) + MZ_AES_PW_VERIFY_SIZE;
break;
case MZ_STREAM_PROP_FOOTER_SIZE:
*value = MZ_AES_AUTHCODE_SIZE;
break;
default:
return MZ_EXIST_ERROR;
}
return MZ_OK;
}
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;
break;
default:
return MZ_EXIST_ERROR;
}
return MZ_OK;
}
void *mz_stream_aes_create(void **stream)
{
mz_stream_aes *aes = NULL;
aes = (mz_stream_aes *)MZ_ALLOC(sizeof(mz_stream_aes));
if (aes != NULL)
{
memset(aes, 0, sizeof(mz_stream_aes));
aes->stream.vtbl = &mz_stream_aes_vtbl;
aes->encryption_mode = MZ_AES_ENCRYPTION_MODE_256;
}
if (stream != NULL)
*stream = aes;
return aes;
}
void mz_stream_aes_delete(void **stream)
{
mz_stream_aes *aes = NULL;
if (stream == NULL)
return;
aes = (mz_stream_aes *)*stream;
if (aes != NULL)
MZ_FREE(aes);
*stream = NULL;
}
void *mz_stream_aes_get_interface(void)
{
return (void *)&mz_stream_aes_vtbl;
}
-49
View File
@@ -1,49 +0,0 @@
/* mz_strm_aes.h -- Stream for WinZIP AES encryption
Version 2.5.2, August 27, 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_AES_H
#define MZ_STREAM_AES_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/***************************************************************************/
int32_t mz_stream_aes_open(void *stream, const char *filename, int32_t mode);
int32_t mz_stream_aes_is_open(void *stream);
int32_t mz_stream_aes_read(void *stream, void *buf, int32_t size);
int32_t mz_stream_aes_write(void *stream, const void *buf, int32_t size);
int64_t mz_stream_aes_tell(void *stream);
int32_t mz_stream_aes_seek(void *stream, int64_t offset, int32_t origin);
int32_t mz_stream_aes_close(void *stream);
int32_t mz_stream_aes_error(void *stream);
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);
void* mz_stream_aes_get_interface(void);
/***************************************************************************/
#ifdef __cplusplus
}
#endif
#endif
+59 -51
View File
@@ -1,36 +1,22 @@
/* mz_strm_buf.c -- Stream for buffering reads/writes
Version 2.5.2, August 27, 2018
Version 2.8.3, January 26, 2019
part of the MiniZip project
This version of ioapi is designed to buffer IO.
Copyright (C) 2010-2018 Nathan Moinvaziri
Copyright (C) 2010-2019 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.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "mz.h"
#include "mz_strm.h"
#include "mz_strm_buf.h"
/***************************************************************************/
#if 0
# define mz_stream_buffered_print(s,f,...) printf(f,__VA_ARGS__);
#else
# define mz_stream_buffered_print(s,f,...)
#endif
/***************************************************************************/
static mz_stream_vtbl mz_stream_buffered_vtbl = {
mz_stream_buffered_open,
mz_stream_buffered_is_open,
@@ -66,6 +52,14 @@ typedef struct mz_stream_buffered_s {
/***************************************************************************/
#if 0
# define mz_stream_buffered_print printf
#else
# define mz_stream_buffered_print(fmt,...)
#endif
/***************************************************************************/
static int32_t mz_stream_buffered_reset(void *stream)
{
mz_stream_buffered *buffered = (mz_stream_buffered *)stream;
@@ -82,7 +76,7 @@ static int32_t mz_stream_buffered_reset(void *stream)
int32_t mz_stream_buffered_open(void *stream, const char *path, int32_t mode)
{
mz_stream_buffered *buffered = (mz_stream_buffered *)stream;
mz_stream_buffered_print(buffered, "open [mode %d]\n", mode);
mz_stream_buffered_print("Buffered - Open (mode %"PRId32")\n", mode);
mz_stream_buffered_reset(buffered);
return mz_stream_open(buffered->stream.base, path, mode);
}
@@ -109,11 +103,11 @@ static int32_t mz_stream_buffered_flush(void *stream, int32_t *written)
buffered->writebuf + (bytes_to_write - bytes_left_to_write), bytes_left_to_write);
if (bytes_written != bytes_left_to_write)
return MZ_STREAM_ERROR;
return MZ_WRITE_ERROR;
buffered->writebuf_misses += 1;
mz_stream_buffered_print(stream, "write flush [%d:%d len %d]\n",
mz_stream_buffered_print("Buffered - Write flush (%"PRId32":%"PRId32" len %"PRId32")\n",
bytes_to_write, bytes_left_to_write, buffered->writebuf_len);
total_bytes_written += bytes_written;
@@ -137,11 +131,13 @@ int32_t mz_stream_buffered_read(void *stream, void *buf, int32_t size)
int32_t bytes_left_to_read = size;
int32_t bytes_read = 0;
mz_stream_buffered_print(stream, "read [size %ld pos %lld]\n", size, buffered->position);
mz_stream_buffered_print("Buffered - Read (size %"PRId32" pos %"PRId64")\n", size, buffered->position);
if (buffered->writebuf_len > 0)
mz_stream_buffered_print(stream, "switch from write to read, not yet supported [%lld]\n",
{
mz_stream_buffered_print("Buffered - Switch from write to read, not yet supported (pos %"PRId64")\n",
buffered->position);
}
while (bytes_left_to_read > 0)
{
@@ -153,7 +149,7 @@ int32_t mz_stream_buffered_read(void *stream, void *buf, int32_t size)
buffered->readbuf_len = 0;
}
bytes_to_read = sizeof(buffered->readbuf) - (buffered->readbuf_len - buffered->readbuf_pos);
bytes_to_read = (int32_t)sizeof(buffered->readbuf) - (buffered->readbuf_len - buffered->readbuf_pos);
bytes_read = mz_stream_read(buffered->stream.base, buffered->readbuf + buffered->readbuf_pos, bytes_to_read);
if (bytes_read < 0)
return bytes_read;
@@ -162,7 +158,7 @@ int32_t mz_stream_buffered_read(void *stream, void *buf, int32_t size)
buffered->readbuf_len += bytes_read;
buffered->position += bytes_read;
mz_stream_buffered_print(stream, "filled [read %d/%d buf %d:%d pos %lld]\n",
mz_stream_buffered_print("Buffered - Filled (read %"PRId32"/%"PRId32" buf %"PRId32":%"PRId32" pos %"PRId64")\n",
bytes_read, bytes_to_read, buffered->readbuf_pos, buffered->readbuf_len, buffered->position);
if (bytes_read == 0)
@@ -171,7 +167,7 @@ int32_t mz_stream_buffered_read(void *stream, void *buf, int32_t size)
if ((buffered->readbuf_len - buffered->readbuf_pos) > 0)
{
bytes_to_copy = (uint32_t)(buffered->readbuf_len - buffered->readbuf_pos);
bytes_to_copy = buffered->readbuf_len - buffered->readbuf_pos;
if (bytes_to_copy > bytes_left_to_read)
bytes_to_copy = bytes_left_to_read;
@@ -183,7 +179,7 @@ int32_t mz_stream_buffered_read(void *stream, void *buf, int32_t size)
buffered->readbuf_hits += 1;
buffered->readbuf_pos += bytes_to_copy;
mz_stream_buffered_print(stream, "emptied [copied %d remaining %d buf %d:%d pos %lld]\n",
mz_stream_buffered_print("Buffered - Emptied (copied %"PRId32" remaining %"PRId32" buf %"PRId32":%"PRId32" pos %"PRId64")\n",
bytes_to_copy, bytes_left_to_read, buffered->readbuf_pos, buffered->readbuf_len, buffered->position);
}
}
@@ -199,9 +195,10 @@ int32_t mz_stream_buffered_write(void *stream, const void *buf, int32_t size)
int32_t bytes_to_copy = 0;
int32_t bytes_used = 0;
int32_t bytes_flushed = 0;
int32_t err = MZ_OK;
mz_stream_buffered_print(stream, "write [size %ld len %d pos %lld]\n",
mz_stream_buffered_print("Buffered - Write (size %"PRId32" len %"PRId32" pos %"PRId64")\n",
size, buffered->writebuf_len, buffered->position);
if (buffered->readbuf_len > 0)
@@ -212,10 +209,11 @@ int32_t mz_stream_buffered_write(void *stream, const void *buf, int32_t size)
buffered->readbuf_len = 0;
buffered->readbuf_pos = 0;
mz_stream_buffered_print(stream, "switch from read to write [%lld]\n", buffered->position);
mz_stream_buffered_print("Buffered - Switch from read to write (pos %"PRId64")\n", buffered->position);
if (mz_stream_seek(buffered->stream.base, buffered->position, MZ_SEEK_SET) != MZ_OK)
return MZ_STREAM_ERROR;
err = mz_stream_seek(buffered->stream.base, buffered->position, MZ_SEEK_SET);
if (err != MZ_OK)
return err;
}
while (bytes_left_to_write > 0)
@@ -223,23 +221,25 @@ int32_t mz_stream_buffered_write(void *stream, const void *buf, int32_t size)
bytes_used = buffered->writebuf_len;
if (bytes_used > buffered->writebuf_pos)
bytes_used = buffered->writebuf_pos;
bytes_to_copy = (uint32_t)(sizeof(buffered->writebuf) - bytes_used);
bytes_to_copy = (int32_t)sizeof(buffered->writebuf) - bytes_used;
if (bytes_to_copy > bytes_left_to_write)
bytes_to_copy = bytes_left_to_write;
if (bytes_to_copy == 0)
{
if (mz_stream_buffered_flush(stream, &bytes_flushed) != MZ_OK)
return MZ_STREAM_ERROR;
err = mz_stream_buffered_flush(stream, &bytes_flushed);
if (err != MZ_OK)
return err;
if (bytes_flushed == 0)
return 0;
continue;
}
memcpy(buffered->writebuf + buffered->writebuf_pos, (const char *)buf + (bytes_to_write - bytes_left_to_write), bytes_to_copy);
memcpy(buffered->writebuf + buffered->writebuf_pos,
(const char *)buf + (bytes_to_write - bytes_left_to_write), bytes_to_copy);
mz_stream_buffered_print(stream, "write copy [remaining %d write %d:%d len %d]\n",
mz_stream_buffered_print("Buffered - Write copy (remaining %"PRId32" write %"PRId32":%"PRId32" len %"PRId32")\n",
bytes_to_copy, bytes_to_write, bytes_left_to_write, buffered->writebuf_len);
bytes_left_to_write -= bytes_to_copy;
@@ -260,11 +260,11 @@ int64_t mz_stream_buffered_tell(void *stream)
buffered->position = position;
mz_stream_buffered_print(stream, "tell [pos %llu readpos %d writepos %d err %d]\n",
buffered->position, buffered->readbuf_pos, buffered->writebuf_pos, errno);
mz_stream_buffered_print("Buffered - Tell (pos %"PRId64" readpos %"PRId32" writepos %"PRId32")\n",
buffered->position, buffered->readbuf_pos, buffered->writebuf_pos);
if (buffered->readbuf_len > 0)
position -= (buffered->readbuf_len - buffered->readbuf_pos);
position -= ((int64_t)buffered->readbuf_len - buffered->readbuf_pos);
if (buffered->writebuf_len > 0)
position += buffered->writebuf_pos;
return position;
@@ -274,8 +274,10 @@ int32_t mz_stream_buffered_seek(void *stream, int64_t offset, int32_t origin)
{
mz_stream_buffered *buffered = (mz_stream_buffered *)stream;
int32_t bytes_flushed = 0;
int32_t err = MZ_OK;
mz_stream_buffered_print(stream, "seek [origin %d offset %llu pos %lld]\n", origin, offset, buffered->position);
mz_stream_buffered_print("Buffered - Seek (origin %"PRId32" offset %"PRId64" pos %"PRId64")\n",
origin, offset, buffered->position);
switch (origin)
{
@@ -285,7 +287,7 @@ int32_t mz_stream_buffered_seek(void *stream, int64_t offset, int32_t origin)
{
if ((offset >= buffered->position) && (offset <= buffered->position + buffered->writebuf_len))
{
buffered->writebuf_pos = (uint32_t)(offset - buffered->position);
buffered->writebuf_pos = (int32_t)(offset - buffered->position);
return MZ_OK;
}
}
@@ -293,12 +295,13 @@ int32_t mz_stream_buffered_seek(void *stream, int64_t offset, int32_t origin)
if ((buffered->readbuf_len > 0) && (offset < buffered->position) &&
(offset >= buffered->position - buffered->readbuf_len))
{
buffered->readbuf_pos = (uint32_t)(offset - (buffered->position - buffered->readbuf_len));
buffered->readbuf_pos = (int32_t)(offset - (buffered->position - buffered->readbuf_len));
return MZ_OK;
}
if (mz_stream_buffered_flush(stream, &bytes_flushed) != MZ_OK)
return MZ_STREAM_ERROR;
err = mz_stream_buffered_flush(stream, &bytes_flushed);
if (err != MZ_OK)
return err;
buffered->position = offset;
break;
@@ -307,26 +310,27 @@ int32_t mz_stream_buffered_seek(void *stream, int64_t offset, int32_t origin)
if (buffered->readbuf_len > 0)
{
if (offset <= (buffered->readbuf_len - buffered->readbuf_pos))
if (offset <= ((int64_t)buffered->readbuf_len - buffered->readbuf_pos))
{
buffered->readbuf_pos += (uint32_t)offset;
return MZ_OK;
}
offset -= (buffered->readbuf_len - buffered->readbuf_pos);
offset -= ((int64_t)buffered->readbuf_len - buffered->readbuf_pos);
buffered->position += offset;
}
if (buffered->writebuf_len > 0)
{
if (offset <= (buffered->writebuf_len - buffered->writebuf_pos))
if (offset <= ((int64_t)buffered->writebuf_len - buffered->writebuf_pos))
{
buffered->writebuf_pos += (uint32_t)offset;
return MZ_OK;
}
//offset -= (buffered->writebuf_len - buffered->writebuf_pos);
/* offset -= (buffered->writebuf_len - buffered->writebuf_pos); */
}
if (mz_stream_buffered_flush(stream, &bytes_flushed) != MZ_OK)
return MZ_STREAM_ERROR;
err = mz_stream_buffered_flush(stream, &bytes_flushed);
if (err != MZ_OK)
return err;
break;
@@ -354,15 +358,19 @@ 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 [flushed %d]\n", bytes_flushed);
mz_stream_buffered_print("Buffered - Close (flushed %"PRId32")\n", bytes_flushed);
if (buffered->readbuf_hits + buffered->readbuf_misses > 0)
mz_stream_buffered_print(stream, "read efficency %.02f%%\n",
{
mz_stream_buffered_print("Buffered - Read efficiency %.02f%%\n",
(buffered->readbuf_hits / ((float)buffered->readbuf_hits + buffered->readbuf_misses)) * 100);
}
if (buffered->writebuf_hits + buffered->writebuf_misses > 0)
mz_stream_buffered_print(stream, "write efficency %.02f%%\n",
{
mz_stream_buffered_print("Buffered - Write efficiency %.02f%%\n",
(buffered->writebuf_hits / ((float)buffered->writebuf_hits + buffered->writebuf_misses)) * 100);
}
mz_stream_buffered_reset(buffered);
+2 -4
View File
@@ -1,10 +1,10 @@
/* mz_strm_buf.h -- Stream for buffering reads/writes
Version 2.5.2, August 27, 2018
Version 2.8.3, January 26, 2019
part of the MiniZip project
This version of ioapi is designed to buffer IO.
Copyright (C) 2010-2018 Nathan Moinvaziri
Copyright (C) 2010-2019 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
This program is distributed under the terms of the same license as zlib.
@@ -14,8 +14,6 @@
#ifndef MZ_STREAM_BUFFERED_H
#define MZ_STREAM_BUFFERED_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
+31 -36
View File
@@ -1,8 +1,8 @@
/* mz_strm_bzip.c -- Stream for bzip inflate/deflate
Version 2.5.2, August 27, 2018
Version 2.8.3, January 26, 2019
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
Copyright (C) 2010-2019 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
This program is distributed under the terms of the same license as bzip.
@@ -10,17 +10,12 @@
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "bzlib.h"
#include "mz.h"
#include "mz_strm.h"
#include "mz_strm_bzip.h"
#include "bzlib.h"
/***************************************************************************/
static mz_stream_vtbl mz_stream_bzip_vtbl = {
@@ -98,7 +93,7 @@ int32_t mz_stream_bzip_open(void *stream, const char *path, int32_t mode)
}
if (bzip->error != BZ_OK)
return MZ_STREAM_ERROR;
return MZ_OPEN_ERROR;
bzip->initialized = 1;
bzip->stream_end = 0;
@@ -110,13 +105,16 @@ int32_t mz_stream_bzip_is_open(void *stream)
{
mz_stream_bzip *bzip = (mz_stream_bzip *)stream;
if (bzip->initialized != 1)
return MZ_STREAM_ERROR;
return MZ_OPEN_ERROR;
return MZ_OK;
}
int32_t mz_stream_bzip_read(void *stream, void *buf, int32_t size)
{
#ifdef MZ_ZIP_NO_DECOMPRESSION
MZ_UNUSED(stream);
MZ_UNUSED(buf);
MZ_UNUSED(size);
return MZ_SUPPORT_ERROR;
#else
mz_stream_bzip *bzip = (mz_stream_bzip *)stream;
@@ -124,11 +122,11 @@ int32_t mz_stream_bzip_read(void *stream, void *buf, int32_t size)
uint64_t total_out_before = 0;
uint64_t total_in_after = 0;
uint64_t total_out_after = 0;
uint32_t total_in = 0;
uint32_t total_out = 0;
uint32_t in_bytes = 0;
uint32_t out_bytes = 0;
int32_t bytes_to_read = 0;
int32_t total_in = 0;
int32_t total_out = 0;
int32_t in_bytes = 0;
int32_t out_bytes = 0;
int32_t bytes_to_read = sizeof(bzip->buffer);
int32_t read = 0;
int32_t err = BZ_OK;
@@ -143,25 +141,21 @@ int32_t mz_stream_bzip_read(void *stream, void *buf, int32_t size)
{
if (bzip->bzstream.avail_in == 0)
{
bytes_to_read = sizeof(bzip->buffer);
if (bzip->max_total_in > 0)
{
if ((bzip->max_total_in - bzip->total_in) < (int64_t)sizeof(bzip->buffer))
if ((int64_t)bytes_to_read > (bzip->max_total_in - bzip->total_in))
bytes_to_read = (int32_t)(bzip->max_total_in - bzip->total_in);
}
read = mz_stream_read(bzip->stream.base, bzip->buffer, bytes_to_read);
if (read < 0)
{
bzip->error = BZ_IO_ERROR;
break;
}
return read;
if (read == 0)
break;
bzip->bzstream.next_in = (char *)bzip->buffer;
bzip->bzstream.avail_in = read;
bzip->bzstream.avail_in = (uint32_t)read;
}
total_in_before = bzip->bzstream.avail_in;
@@ -174,8 +168,8 @@ int32_t mz_stream_bzip_read(void *stream, void *buf, int32_t size)
total_out_after = bzip->bzstream.total_out_lo32 +
(((uint64_t)bzip->bzstream.total_out_hi32) << 32);
in_bytes = (uint32_t)(total_in_before - total_in_after);
out_bytes = (uint32_t)(total_out_after - total_out_before);
in_bytes = (int32_t)(total_in_before - total_in_after);
out_bytes = (int32_t)(total_out_after - total_out_before);
total_in += in_bytes;
total_out += out_bytes;
@@ -197,17 +191,18 @@ int32_t mz_stream_bzip_read(void *stream, void *buf, int32_t size)
while (bzip->bzstream.avail_out > 0);
if (bzip->error != 0)
return bzip->error;
return MZ_DATA_ERROR;
return total_out;
#endif
}
#ifndef MZ_ZIP_NO_COMPRESSION
static int32_t mz_stream_bzip_flush(void *stream)
{
mz_stream_bzip *bzip = (mz_stream_bzip *)stream;
if (mz_stream_write(bzip->stream.base, bzip->buffer, bzip->buffer_len) != bzip->buffer_len)
return MZ_STREAM_ERROR;
return MZ_WRITE_ERROR;
return MZ_OK;
}
@@ -223,11 +218,9 @@ static int32_t mz_stream_bzip_compress(void *stream, int flush)
{
if (bzip->bzstream.avail_out == 0)
{
if (mz_stream_bzip_flush(bzip) != MZ_OK)
{
bzip->error = BZ_DATA_ERROR;
return MZ_STREAM_ERROR;
}
err = mz_stream_bzip_flush(bzip);
if (err != MZ_OK)
return err;
bzip->bzstream.avail_out = sizeof(bzip->buffer);
bzip->bzstream.next_out = (char *)bzip->buffer;
@@ -253,13 +246,14 @@ static int32_t mz_stream_bzip_compress(void *stream, int flush)
if (err < 0)
{
bzip->error = err;
return MZ_STREAM_ERROR;
return MZ_DATA_ERROR;
}
}
while ((bzip->bzstream.avail_in > 0) || (flush == BZ_FINISH && err == BZ_FINISH_OK));
return MZ_OK;
}
#endif
int32_t mz_stream_bzip_write(void *stream, const void *buf, int32_t size)
{
@@ -268,6 +262,7 @@ int32_t mz_stream_bzip_write(void *stream, const void *buf, int32_t size)
#ifdef MZ_ZIP_NO_COMPRESSION
MZ_UNUSED(bzip);
MZ_UNUSED(buf);
err = MZ_SUPPORT_ERROR;
#else
bzip->bzstream.next_in = (char *)(intptr_t)buf;
@@ -284,7 +279,7 @@ int64_t mz_stream_bzip_tell(void *stream)
{
MZ_UNUSED(stream);
return MZ_STREAM_ERROR;
return MZ_TELL_ERROR;
}
int32_t mz_stream_bzip_seek(void *stream, int64_t offset, int32_t origin)
@@ -293,7 +288,7 @@ int32_t mz_stream_bzip_seek(void *stream, int64_t offset, int32_t origin)
MZ_UNUSED(offset);
MZ_UNUSED(origin);
return MZ_STREAM_ERROR;
return MZ_SEEK_ERROR;
}
int32_t mz_stream_bzip_close(void *stream)
@@ -323,7 +318,7 @@ int32_t mz_stream_bzip_close(void *stream)
bzip->initialized = 0;
if (bzip->error != BZ_OK)
return MZ_STREAM_ERROR;
return MZ_CLOSE_ERROR;
return MZ_OK;
}
+2 -4
View File
@@ -1,8 +1,8 @@
/* mz_strm_bzip.h -- Stream for bzip inflate/deflate
Version 2.5.2, August 27, 2018
Version 2.8.3, January 26, 2019
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
Copyright (C) 2010-2019 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
This program is distributed under the terms of the same license as zlib.
@@ -12,8 +12,6 @@
#ifndef MZ_STREAM_BZIP_H
#define MZ_STREAM_BZIP_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
-206
View File
@@ -1,206 +0,0 @@
/* mz_strm_crc32.c -- Stream for CRC32 hashing
Version 2.5.2, August 27, 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.
*/
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "mz.h"
#include "mz_strm.h"
#include "mz_strm_crc32.h"
#ifdef HAVE_LZMA
#include "mz_strm_lzma.h"
#endif
#ifdef HAVE_ZLIB
#include "mz_strm_zlib.h"
#endif
/***************************************************************************/
static mz_stream_vtbl mz_stream_crc32_vtbl = {
mz_stream_crc32_open,
mz_stream_crc32_is_open,
mz_stream_crc32_read,
mz_stream_crc32_write,
mz_stream_crc32_tell,
mz_stream_crc32_seek,
mz_stream_crc32_close,
mz_stream_crc32_error,
mz_stream_crc32_create,
mz_stream_crc32_delete,
mz_stream_crc32_get_prop_int64,
NULL
};
/***************************************************************************/
typedef struct mz_stream_crc32_s {
mz_stream stream;
int8_t initialized;
int64_t value;
int64_t total_in;
int64_t total_out;
mz_stream_crc32_update
update;
} mz_stream_crc32;
/***************************************************************************/
int32_t mz_stream_crc32_open(void *stream, const char *path, int32_t mode)
{
mz_stream_crc32 *crc32 = (mz_stream_crc32 *)stream;
MZ_UNUSED(path);
MZ_UNUSED(mode);
crc32->initialized = 1;
crc32->value = 0;
return MZ_OK;
}
int32_t mz_stream_crc32_is_open(void *stream)
{
mz_stream_crc32 *crc32 = (mz_stream_crc32 *)stream;
if (crc32->initialized != 1)
return MZ_STREAM_ERROR;
return MZ_OK;
}
int32_t mz_stream_crc32_read(void *stream, void *buf, int32_t size)
{
mz_stream_crc32 *crc32x = (mz_stream_crc32 *)stream;
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;
}
return read;
}
int32_t mz_stream_crc32_write(void *stream, const void *buf, int32_t size)
{
mz_stream_crc32 *crc32x = (mz_stream_crc32 *)stream;
int32_t written = 0;
crc32x->value = crc32x->update(crc32x->value, buf, size);
written = mz_stream_write(crc32x->stream.base, buf, size);
crc32x->total_out += written;
return written;
}
int64_t mz_stream_crc32_tell(void *stream)
{
mz_stream_crc32 *crc32 = (mz_stream_crc32 *)stream;
return mz_stream_tell(crc32->stream.base);
}
int32_t mz_stream_crc32_seek(void *stream, int64_t offset, int32_t origin)
{
mz_stream_crc32 *crc32 = (mz_stream_crc32 *)stream;
crc32->value = 0;
return mz_stream_seek(crc32->stream.base, offset, origin);
}
int32_t mz_stream_crc32_close(void *stream)
{
mz_stream_crc32 *crc32 = (mz_stream_crc32 *)stream;
crc32->initialized = 0;
return MZ_OK;
}
int32_t mz_stream_crc32_error(void *stream)
{
mz_stream_crc32 *crc32 = (mz_stream_crc32 *)stream;
return mz_stream_error(crc32->stream.base);
}
int32_t mz_stream_crc32_get_value(void *stream)
{
mz_stream_crc32 *crc32 = (mz_stream_crc32 *)stream;
return (int32_t)crc32->value;
}
int32_t mz_stream_crc32_get_prop_int64(void *stream, int32_t prop, int64_t *value)
{
mz_stream_crc32 *crc32 = (mz_stream_crc32 *)stream;
switch (prop)
{
case MZ_STREAM_PROP_TOTAL_IN:
*value = crc32->total_in;
break;
case MZ_STREAM_PROP_TOTAL_OUT:
*value = crc32->total_out;
break;
default:
return MZ_EXIST_ERROR;
}
return MZ_OK;
}
void *mz_stream_crc32_create(void **stream)
{
mz_stream_crc32 *crc32 = NULL;
crc32 = (mz_stream_crc32 *)MZ_ALLOC(sizeof(mz_stream_crc32));
if (crc32 != NULL)
{
memset(crc32, 0, sizeof(mz_stream_crc32));
crc32->stream.vtbl = &mz_stream_crc32_vtbl;
}
#ifdef HAVE_ZLIB
crc32->update =
(mz_stream_crc32_update)mz_stream_zlib_get_crc32_update();
#elif defined(HAVE_LZMA)
crc32->update =
(mz_stream_crc32_update)mz_stream_lzma_get_crc32_update();
#else
#error ZLIB or LZMA required for CRC32
#endif
if (stream != NULL)
*stream = crc32;
return crc32;
}
void mz_stream_crc32_delete(void **stream)
{
mz_stream_crc32 *crc32 = NULL;
if (stream == NULL)
return;
crc32 = (mz_stream_crc32 *)*stream;
if (crc32 != NULL)
MZ_FREE(crc32);
*stream = NULL;
}
void *mz_stream_crc32_get_interface(void)
{
return (void *)&mz_stream_crc32_vtbl;
}
int32_t mz_stream_crc32_get_update_func(mz_stream_crc32_update *update)
{
if (update == NULL)
return MZ_PARAM_ERROR;
#ifdef HAVE_ZLIB
*update =
(mz_stream_crc32_update)mz_stream_zlib_get_crc32_update();
#elif defined(HAVE_LZMA)
*update =
(mz_stream_crc32_update)mz_stream_lzma_get_crc32_update();
#else
#error ZLIB or LZMA required for CRC32
#endif
return MZ_OK;
}
-50
View File
@@ -1,50 +0,0 @@
/* mz_strm_crc32.h -- Stream for CRC32 hashing
Version 2.5.2, August 27, 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_CRC32_H
#define MZ_STREAM_CRC32_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/***************************************************************************/
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);
int32_t mz_stream_crc32_read(void *stream, void *buf, int32_t size);
int32_t mz_stream_crc32_write(void *stream, const void *buf, int32_t size);
int64_t mz_stream_crc32_tell(void *stream);
int32_t mz_stream_crc32_seek(void *stream, int64_t offset, int32_t origin);
int32_t mz_stream_crc32_close(void *stream);
int32_t mz_stream_crc32_error(void *stream);
int32_t mz_stream_crc32_get_value(void *stream);
int32_t mz_stream_crc32_get_prop_int64(void *stream, int32_t prop, int64_t *value);
void* mz_stream_crc32_create(void **stream);
void mz_stream_crc32_delete(void **stream);
void* mz_stream_crc32_get_interface(void);
int32_t mz_stream_crc32_get_update_func(mz_stream_crc32_update *update);
/***************************************************************************/
#ifdef __cplusplus
}
#endif
#endif
+423
View File
@@ -0,0 +1,423 @@
/* mz_strm_libcomp.c -- Stream for apple compression
Version 2.8.3, January 26, 2019
part of the MiniZip project
Copyright (C) 2010-2019 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.
*/
#include "mz.h"
#include "mz_strm.h"
#include "mz_strm_libcomp.h"
#include <compression.h>
/***************************************************************************/
static mz_stream_vtbl mz_stream_libcomp_vtbl = {
mz_stream_libcomp_open,
mz_stream_libcomp_is_open,
mz_stream_libcomp_read,
mz_stream_libcomp_write,
mz_stream_libcomp_tell,
mz_stream_libcomp_seek,
mz_stream_libcomp_close,
mz_stream_libcomp_error,
mz_stream_libcomp_create,
mz_stream_libcomp_delete,
mz_stream_libcomp_get_prop_int64,
mz_stream_libcomp_set_prop_int64
};
/***************************************************************************/
typedef struct mz_stream_libcomp_s {
mz_stream stream;
compression_stream
cstream;
uint8_t buffer[INT16_MAX];
int32_t buffer_len;
int64_t total_in;
int64_t total_out;
int64_t max_total_in;
int8_t initialized;
int32_t mode;
int32_t error;
int16_t algorithm;
} mz_stream_libcomp;
/***************************************************************************/
int32_t mz_stream_libcomp_open(void *stream, const char *path, int32_t mode)
{
mz_stream_libcomp *libcomp = (mz_stream_libcomp *)stream;
int32_t err = 0;
int16_t operation = 0;
MZ_UNUSED(path);
if (libcomp->algorithm == 0)
return MZ_PARAM_ERROR;
libcomp->total_in = 0;
libcomp->total_out = 0;
if (mode & MZ_OPEN_MODE_WRITE)
{
#ifdef MZ_ZIP_NO_COMPRESSION
return MZ_SUPPORT_ERROR;
#else
operation = COMPRESSION_STREAM_ENCODE;
#endif
}
else if (mode & MZ_OPEN_MODE_READ)
{
#ifdef MZ_ZIP_NO_DECOMPRESSION
return MZ_SUPPORT_ERROR;
#else
operation = COMPRESSION_STREAM_DECODE;
#endif
}
err = compression_stream_init(&libcomp->cstream, (compression_stream_operation)operation,
(compression_algorithm)libcomp->algorithm);
if (err == COMPRESSION_STATUS_ERROR)
{
libcomp->error = err;
return MZ_OPEN_ERROR;
}
libcomp->initialized = 1;
libcomp->mode = mode;
return MZ_OK;
}
int32_t mz_stream_libcomp_is_open(void *stream)
{
mz_stream_libcomp *libcomp = (mz_stream_libcomp *)stream;
if (libcomp->initialized != 1)
return MZ_OPEN_ERROR;
return MZ_OK;
}
int32_t mz_stream_libcomp_read(void *stream, void *buf, int32_t size)
{
#ifdef MZ_ZIP_NO_DECOMPRESSION
MZ_UNUSED(stream);
MZ_UNUSED(buf);
MZ_UNUSED(size);
return MZ_SUPPORT_ERROR;
#else
mz_stream_libcomp *libcomp = (mz_stream_libcomp *)stream;
uint64_t total_in_before = 0;
uint64_t total_in_after = 0;
uint64_t total_out_before = 0;
uint64_t total_out_after = 0;
int32_t total_in = 0;
int32_t total_out = 0;
int32_t in_bytes = 0;
int32_t out_bytes = 0;
int32_t bytes_to_read = sizeof(libcomp->buffer);
int32_t read = 0;
int32_t err = MZ_OK;
int16_t flags = 0;
libcomp->cstream.dst_ptr = buf;
libcomp->cstream.dst_size = (size_t)size;
do
{
if (libcomp->cstream.src_size == 0)
{
if (libcomp->max_total_in > 0)
{
if ((int64_t)bytes_to_read > (libcomp->max_total_in - libcomp->total_in))
bytes_to_read = (int32_t)(libcomp->max_total_in - libcomp->total_in);
}
read = mz_stream_read(libcomp->stream.base, libcomp->buffer, bytes_to_read);
if (read < 0)
return read;
if (read == 0)
flags = COMPRESSION_STREAM_FINALIZE;
libcomp->cstream.src_ptr = libcomp->buffer;
libcomp->cstream.src_size = (size_t)read;
}
total_in_before = libcomp->cstream.src_size;
total_out_before = libcomp->cstream.dst_size;
err = compression_stream_process(&libcomp->cstream, flags);
if (err == COMPRESSION_STATUS_ERROR)
{
libcomp->error = err;
break;
}
total_in_after = libcomp->cstream.src_size;
total_out_after = libcomp->cstream.dst_size;
in_bytes = (int32_t)(total_in_before - total_in_after);
out_bytes = (int32_t)(total_out_before - total_out_after);
total_in += in_bytes;
total_out += out_bytes;
libcomp->total_in += in_bytes;
libcomp->total_out += out_bytes;
if (err == COMPRESSION_STATUS_END)
break;
if (err != COMPRESSION_STATUS_OK)
{
libcomp->error = err;
break;
}
}
while (libcomp->cstream.dst_size > 0);
if (libcomp->error != 0)
return MZ_DATA_ERROR;
return total_out;
#endif
}
static int32_t mz_stream_libcomp_flush(void *stream)
{
mz_stream_libcomp *libcomp = (mz_stream_libcomp *)stream;
if (mz_stream_write(libcomp->stream.base, libcomp->buffer, libcomp->buffer_len) != libcomp->buffer_len)
return MZ_WRITE_ERROR;
return MZ_OK;
}
static int32_t mz_stream_libcomp_deflate(void *stream, int flush)
{
mz_stream_libcomp *libcomp = (mz_stream_libcomp *)stream;
uint64_t total_out_before = 0;
uint64_t total_out_after = 0;
uint32_t out_bytes = 0;
int32_t err = MZ_OK;
do
{
if (libcomp->cstream.dst_size == 0)
{
err = mz_stream_libcomp_flush(libcomp);
if (err != MZ_OK)
{
libcomp->error = err;
return err;
}
libcomp->cstream.dst_size = sizeof(libcomp->buffer);
libcomp->cstream.dst_ptr = libcomp->buffer;
libcomp->buffer_len = 0;
}
total_out_before = libcomp->cstream.dst_size;
err = compression_stream_process(&libcomp->cstream, flush);
total_out_after = libcomp->cstream.dst_size;
out_bytes = (uint32_t)(total_out_before - total_out_after);
libcomp->buffer_len += out_bytes;
libcomp->total_out += out_bytes;
if (err == COMPRESSION_STATUS_END)
break;
if (err != COMPRESSION_STATUS_OK)
{
libcomp->error = err;
return MZ_DATA_ERROR;
}
}
while ((libcomp->cstream.src_size > 0) || (flush == COMPRESSION_STREAM_FINALIZE && err == COMPRESSION_STATUS_OK));
return MZ_OK;
}
int32_t mz_stream_libcomp_write(void *stream, const void *buf, int32_t size)
{
mz_stream_libcomp *libcomp = (mz_stream_libcomp *)stream;
int32_t err = size;
#ifdef MZ_ZIP_NO_COMPRESSION
MZ_UNUSED(libcomp);
err = MZ_SUPPORT_ERROR;
#else
libcomp->cstream.src_ptr = buf;
libcomp->cstream.src_size = (size_t)size;
mz_stream_libcomp_deflate(stream, 0);
libcomp->total_in += size;
#endif
return err;
}
int64_t mz_stream_libcomp_tell(void *stream)
{
MZ_UNUSED(stream);
return MZ_TELL_ERROR;
}
int32_t mz_stream_libcomp_seek(void *stream, int64_t offset, int32_t origin)
{
MZ_UNUSED(stream);
MZ_UNUSED(offset);
MZ_UNUSED(origin);
return MZ_SEEK_ERROR;
}
int32_t mz_stream_libcomp_close(void *stream)
{
mz_stream_libcomp *libcomp = (mz_stream_libcomp *)stream;
if (libcomp->mode & MZ_OPEN_MODE_WRITE)
{
#ifdef MZ_ZIP_NO_COMPRESSION
return MZ_SUPPORT_ERROR;
#else
mz_stream_libcomp_deflate(stream, COMPRESSION_STREAM_FINALIZE);
mz_stream_libcomp_flush(stream);
#endif
}
else if (libcomp->mode & MZ_OPEN_MODE_READ)
{
#ifdef MZ_ZIP_NO_DECOMPRESSION
return MZ_SUPPORT_ERROR;
#endif
}
compression_stream_destroy(&libcomp->cstream);
libcomp->initialized = 0;
if (libcomp->error != MZ_OK)
return MZ_CLOSE_ERROR;
return MZ_OK;
}
int32_t mz_stream_libcomp_error(void *stream)
{
mz_stream_libcomp *libcomp = (mz_stream_libcomp *)stream;
return libcomp->error;
}
int32_t mz_stream_libcomp_get_prop_int64(void *stream, int32_t prop, int64_t *value)
{
mz_stream_libcomp *libcomp = (mz_stream_libcomp *)stream;
switch (prop)
{
case MZ_STREAM_PROP_TOTAL_IN:
*value = libcomp->total_in;
break;
case MZ_STREAM_PROP_TOTAL_IN_MAX:
*value = libcomp->max_total_in;
break;
case MZ_STREAM_PROP_TOTAL_OUT:
*value = libcomp->total_out;
break;
case MZ_STREAM_PROP_HEADER_SIZE:
*value = 0;
break;
default:
return MZ_EXIST_ERROR;
}
return MZ_OK;
}
int32_t mz_stream_libcomp_set_prop_int64(void *stream, int32_t prop, int64_t value)
{
mz_stream_libcomp *libcomp = (mz_stream_libcomp *)stream;
switch (prop)
{
case MZ_STREAM_PROP_COMPRESS_ALGORITHM:
libcomp->algorithm = (int16_t)value;
break;
case MZ_STREAM_PROP_TOTAL_IN_MAX:
libcomp->max_total_in = value;
break;
default:
return MZ_EXIST_ERROR;
}
return MZ_OK;
}
void *mz_stream_libcomp_create(void **stream)
{
mz_stream_libcomp *libcomp = NULL;
libcomp = (mz_stream_libcomp *)MZ_ALLOC(sizeof(mz_stream_libcomp));
if (libcomp != NULL)
{
memset(libcomp, 0, sizeof(mz_stream_libcomp));
libcomp->stream.vtbl = &mz_stream_libcomp_vtbl;
}
if (stream != NULL)
*stream = libcomp;
return libcomp;
}
void mz_stream_libcomp_delete(void **stream)
{
mz_stream_libcomp *libcomp = NULL;
if (stream == NULL)
return;
libcomp = (mz_stream_libcomp *)*stream;
if (libcomp != NULL)
MZ_FREE(libcomp);
*stream = NULL;
}
/***************************************************************************/
static mz_stream_vtbl mz_stream_zlib_vtbl = {
mz_stream_libcomp_open,
mz_stream_libcomp_is_open,
mz_stream_libcomp_read,
mz_stream_libcomp_write,
mz_stream_libcomp_tell,
mz_stream_libcomp_seek,
mz_stream_libcomp_close,
mz_stream_libcomp_error,
mz_stream_zlib_create,
mz_stream_libcomp_delete,
mz_stream_libcomp_get_prop_int64,
mz_stream_libcomp_set_prop_int64
};
void *mz_stream_zlib_create(void **stream)
{
mz_stream_libcomp *libcomp = NULL;
void *stream_int = NULL;
mz_stream_libcomp_create(&stream_int);
if (stream_int != NULL)
{
libcomp = (mz_stream_libcomp *)stream_int;
libcomp->stream.vtbl = &mz_stream_zlib_vtbl;
libcomp->algorithm = COMPRESSION_ZLIB;
}
if (stream != NULL)
*stream = stream_int;
return stream_int;
}
void *mz_stream_zlib_get_interface(void)
{
return (void *)&mz_stream_zlib_vtbl;
}
+67
View File
@@ -0,0 +1,67 @@
/* mz_strm_libcomp.h -- Stream for apple compression
Version 2.8.3, January 26, 2019
part of the MiniZip project
Copyright (C) 2010-2019 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_LIBCOMP_H
#define MZ_STREAM_LIBCOMP_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/***************************************************************************/
int32_t mz_stream_libcomp_open(void *stream, const char *filename, int32_t mode);
int32_t mz_stream_libcomp_is_open(void *stream);
int32_t mz_stream_libcomp_read(void *stream, void *buf, int32_t size);
int32_t mz_stream_libcomp_write(void *stream, const void *buf, int32_t size);
int64_t mz_stream_libcomp_tell(void *stream);
int32_t mz_stream_libcomp_seek(void *stream, int64_t offset, int32_t origin);
int32_t mz_stream_libcomp_close(void *stream);
int32_t mz_stream_libcomp_error(void *stream);
int32_t mz_stream_libcomp_get_prop_int64(void *stream, int32_t prop, int64_t *value);
int32_t mz_stream_libcomp_set_prop_int64(void *stream, int32_t prop, int64_t value);
void* mz_stream_libcomp_create(void **stream);
void mz_stream_libcomp_delete(void **stream);
void* mz_stream_libcomp_get_interface(void);
void* mz_stream_libcomp_get_crc32_update(void);
/***************************************************************************/
int32_t mz_stream_zlib_open(void *stream, const char *filename, int32_t mode);
int32_t mz_stream_zlib_is_open(void *stream);
int32_t mz_stream_zlib_read(void *stream, void *buf, int32_t size);
int32_t mz_stream_zlib_write(void *stream, const void *buf, int32_t size);
int64_t mz_stream_zlib_tell(void *stream);
int32_t mz_stream_zlib_seek(void *stream, int64_t offset, int32_t origin);
int32_t mz_stream_zlib_close(void *stream);
int32_t mz_stream_zlib_error(void *stream);
int32_t mz_stream_zlib_get_prop_int64(void *stream, int32_t prop, int64_t *value);
int32_t mz_stream_zlib_set_prop_int64(void *stream, int32_t prop, int64_t value);
void* mz_stream_zlib_create(void **stream);
void mz_stream_zlib_delete(void **stream);
void* mz_stream_zlib_get_interface(void);
void* mz_stream_zlib_get_crc32_update(void);
/***************************************************************************/
#ifdef __cplusplus
}
#endif
#endif
+38 -48
View File
@@ -1,8 +1,8 @@
/* mz_strm_lzma.c -- Stream for lzma inflate/deflate
Version 2.5.2, August 27, 2018
Version 2.8.3, January 26, 2019
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
Copyright (C) 2010-2019 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
This program is distributed under the terms of the same license as lzma.
@@ -10,18 +10,12 @@
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <lzma.h>
#include "mz.h"
#include "mz_strm.h"
#include "mz_strm_lzma.h"
#include "lzma.h"
/***************************************************************************/
#define MZ_LZMA_HEADER_SIZE (4)
@@ -85,13 +79,15 @@ int32_t mz_stream_lzma_open(void *stream, const char *path, int32_t mode)
{
#ifdef MZ_ZIP_NO_COMPRESSION
MZ_UNUSED(filters);
MZ_UNUSED(major);
MZ_UNUSED(minor);
return MZ_SUPPORT_ERROR;
#else
lzma->lstream.next_out = lzma->buffer;
lzma->lstream.avail_out = sizeof(lzma->buffer);
if (lzma_lzma_preset(&opt_lzma, lzma->preset))
return MZ_STREAM_ERROR;
return MZ_OPEN_ERROR;
memset(&filters, 0, sizeof(filters));
@@ -113,6 +109,9 @@ int32_t mz_stream_lzma_open(void *stream, const char *path, int32_t mode)
else if (mode & MZ_OPEN_MODE_READ)
{
#ifdef MZ_ZIP_NO_DECOMPRESSION
MZ_UNUSED(filters);
MZ_UNUSED(major);
MZ_UNUSED(minor);
return MZ_SUPPORT_ERROR;
#else
lzma->lstream.next_in = lzma->buffer;
@@ -129,7 +128,7 @@ int32_t mz_stream_lzma_open(void *stream, const char *path, int32_t mode)
}
if (lzma->error != LZMA_OK)
return MZ_STREAM_ERROR;
return MZ_OPEN_ERROR;
lzma->initialized = 1;
lzma->mode = mode;
@@ -140,13 +139,16 @@ int32_t mz_stream_lzma_is_open(void *stream)
{
mz_stream_lzma *lzma = (mz_stream_lzma *)stream;
if (lzma->initialized != 1)
return MZ_STREAM_ERROR;
return MZ_OPEN_ERROR;
return MZ_OK;
}
int32_t mz_stream_lzma_read(void *stream, void *buf, int32_t size)
{
#ifdef MZ_ZIP_NO_DECOMPRESSION
MZ_UNUSED(stream);
MZ_UNUSED(buf);
MZ_UNUSED(size);
return MZ_SUPPORT_ERROR;
#else
mz_stream_lzma *lzma = (mz_stream_lzma *)stream;
@@ -154,10 +156,10 @@ int32_t mz_stream_lzma_read(void *stream, void *buf, int32_t size)
uint64_t total_out_before = 0;
uint64_t total_in_after = 0;
uint64_t total_out_after = 0;
uint32_t total_in = 0;
uint32_t total_out = 0;
uint32_t in_bytes = 0;
uint32_t out_bytes = 0;
int32_t total_in = 0;
int32_t total_out = 0;
int32_t in_bytes = 0;
int32_t out_bytes = 0;
int32_t bytes_to_read = 0;
int32_t read = 0;
int32_t err = LZMA_OK;
@@ -173,22 +175,19 @@ 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) < (int64_t)sizeof(lzma->buffer))
if ((int64_t)bytes_to_read > (lzma->max_total_in - lzma->total_in))
bytes_to_read = (int32_t)(lzma->max_total_in - lzma->total_in);
}
read = mz_stream_read(lzma->stream.base, lzma->buffer, bytes_to_read);
if (read < 0)
{
lzma->error = MZ_STREAM_ERROR;
break;
}
return read;
if (read == 0)
break;
lzma->lstream.next_in = lzma->buffer;
lzma->lstream.avail_in = read;
lzma->lstream.avail_in = (size_t)read;
}
total_in_before = lzma->lstream.avail_in;
@@ -199,10 +198,10 @@ int32_t mz_stream_lzma_read(void *stream, void *buf, int32_t size)
total_in_after = lzma->lstream.avail_in;
total_out_after = lzma->lstream.total_out;
if ((lzma->max_total_out != -1) && (int64_t)total_out_after > lzma->max_total_out)
total_out_after = lzma->max_total_out;
total_out_after = (uint64_t)lzma->max_total_out;
in_bytes = (uint32_t)(total_in_before - total_in_after);
out_bytes = (uint32_t)(total_out_after - total_out_before);
in_bytes = (int32_t)(total_in_before - total_in_after);
out_bytes = (int32_t)(total_out_after - total_out_before);
total_in += in_bytes;
total_out += out_bytes;
@@ -221,17 +220,18 @@ int32_t mz_stream_lzma_read(void *stream, void *buf, int32_t size)
while (lzma->lstream.avail_out > 0);
if (lzma->error != 0)
return lzma->error;
return MZ_DATA_ERROR;
return total_out;
#endif
}
#ifndef MZ_ZIP_NO_COMPRESSION
static int32_t mz_stream_lzma_flush(void *stream)
{
mz_stream_lzma *lzma = (mz_stream_lzma *)stream;
if (mz_stream_write(lzma->stream.base, lzma->buffer, lzma->buffer_len) != lzma->buffer_len)
return MZ_STREAM_ERROR;
return MZ_WRITE_ERROR;
return MZ_OK;
}
@@ -248,11 +248,9 @@ static int32_t mz_stream_lzma_code(void *stream, int32_t flush)
{
if (lzma->lstream.avail_out == 0)
{
if (mz_stream_lzma_flush(lzma) != MZ_OK)
{
lzma->error = MZ_STREAM_ERROR;
return MZ_STREAM_ERROR;
}
err = mz_stream_lzma_flush(lzma);
if (err != MZ_OK)
return err;
lzma->lstream.avail_out = sizeof(lzma->buffer);
lzma->lstream.next_out = lzma->buffer;
@@ -261,7 +259,7 @@ static int32_t mz_stream_lzma_code(void *stream, int32_t flush)
}
total_out_before = lzma->lstream.total_out;
err = lzma_code(&lzma->lstream, flush);
err = lzma_code(&lzma->lstream, (lzma_action)flush);
total_out_after = lzma->lstream.total_out;
out_bytes = (uint32_t)(total_out_after - total_out_before);
@@ -269,7 +267,7 @@ static int32_t mz_stream_lzma_code(void *stream, int32_t flush)
if (err != LZMA_OK && err != LZMA_STREAM_END)
{
lzma->error = err;
return MZ_STREAM_ERROR;
return MZ_DATA_ERROR;
}
lzma->buffer_len += out_bytes;
@@ -279,6 +277,7 @@ static int32_t mz_stream_lzma_code(void *stream, int32_t flush)
return MZ_OK;
}
#endif
int32_t mz_stream_lzma_write(void *stream, const void *buf, int32_t size)
{
@@ -287,6 +286,7 @@ int32_t mz_stream_lzma_write(void *stream, const void *buf, int32_t size)
#ifdef MZ_ZIP_NO_COMPRESSION
MZ_UNUSED(lzma);
MZ_UNUSED(buf);
err = MZ_SUPPORT_ERROR;
#else
lzma->lstream.next_in = (uint8_t*)(intptr_t)buf;
@@ -303,7 +303,7 @@ int64_t mz_stream_lzma_tell(void *stream)
{
MZ_UNUSED(stream);
return MZ_STREAM_ERROR;
return MZ_TELL_ERROR;
}
int32_t mz_stream_lzma_seek(void *stream, int64_t offset, int32_t origin)
@@ -312,7 +312,7 @@ int32_t mz_stream_lzma_seek(void *stream, int64_t offset, int32_t origin)
MZ_UNUSED(offset);
MZ_UNUSED(origin);
return MZ_STREAM_ERROR;
return MZ_SEEK_ERROR;
}
int32_t mz_stream_lzma_close(void *stream)
@@ -342,7 +342,7 @@ int32_t mz_stream_lzma_close(void *stream)
lzma->initialized = 0;
if (lzma->error != LZMA_OK)
return MZ_STREAM_ERROR;
return MZ_CLOSE_ERROR;
return MZ_OK;
}
@@ -436,13 +436,3 @@ void *mz_stream_lzma_get_interface(void)
{
return (void *)&mz_stream_lzma_vtbl;
}
static int64_t mz_stream_lzma_crc32(int64_t value, const void *buf, int32_t size)
{
return (int32_t)lzma_crc32(buf, size, (int32_t)value);
}
void *mz_stream_lzma_get_crc32_update(void)
{
return (void *)mz_stream_lzma_crc32;
}
+2 -5
View File
@@ -1,8 +1,8 @@
/* mz_strm_lzma.h -- Stream for lzma inflate/deflate
Version 2.5.2, August 27, 2018
Version 2.8.3, January 26, 2019
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
Copyright (C) 2010-2019 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
This program is distributed under the terms of the same license as lzma.
@@ -12,8 +12,6 @@
#ifndef MZ_STREAM_LZMA_H
#define MZ_STREAM_LZMA_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
@@ -36,7 +34,6 @@ 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_update(void);
/***************************************************************************/
+37 -26
View File
@@ -1,5 +1,5 @@
/* mz_strm_mem.c -- Stream for memory access
Version 2.5.2, August 27, 2018
Version 2.8.3, January 26, 2019
part of the MiniZip project
This interface is designed to access memory rather than files.
@@ -7,21 +7,17 @@
Based on Unzip ioapi.c version 0.22, May 19th, 2003
Copyright (C) 2010-2018 Nathan Moinvaziri
Copyright (C) 2010-2019 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
Copyright (C) 2003 Justin Fletcher
Copyright (C) 1998-2003 Gilles Vollant
http://www.winimage.com/zLibDll/minizip.html
https://www.winimage.com/zLibDll/minizip.html
This program is distributed under the terms of the same license as zlib.
See the accompanying LICENSE file for the full text of the license.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mz.h"
#include "mz_strm.h"
#include "mz_strm_mem.h"
@@ -48,23 +44,26 @@ static mz_stream_vtbl mz_stream_mem_vtbl = {
typedef struct mz_stream_mem_s {
mz_stream stream;
int32_t mode;
uint8_t *buffer; // Memory buffer pointer
int32_t size; // Size of the memory buffer
int32_t limit; // Furthest we've written
int32_t position; // Current position in the memory
int32_t grow_size; // Size to grow when full
uint8_t *buffer; /* Memory buffer pointer */
int32_t size; /* Size of the memory buffer */
int32_t limit; /* Furthest we've written */
int32_t position; /* Current position in the memory */
int32_t grow_size; /* Size to grow when full */
} mz_stream_mem;
/***************************************************************************/
static void mz_stream_mem_set_size(void *stream, int32_t size)
static int32_t mz_stream_mem_set_size(void *stream, int32_t size)
{
mz_stream_mem *mem = (mz_stream_mem *)stream;
int32_t new_size = size;
uint8_t *new_buf = NULL;
new_buf = (uint8_t *)MZ_ALLOC(new_size);
new_buf = (uint8_t *)MZ_ALLOC((uint32_t)new_size);
if (new_buf == NULL)
return MZ_BUF_ERROR;
if (mem->buffer)
{
memcpy(new_buf, mem->buffer, mem->size);
@@ -73,11 +72,13 @@ static void mz_stream_mem_set_size(void *stream, int32_t size)
mem->buffer = new_buf;
mem->size = new_size;
return MZ_OK;
}
int32_t mz_stream_mem_open(void *stream, const char *path, int32_t mode)
{
mz_stream_mem *mem = (mz_stream_mem *)stream;
int32_t err = MZ_OK;
MZ_UNUSED(path);
@@ -86,18 +87,18 @@ int32_t mz_stream_mem_open(void *stream, const char *path, int32_t mode)
mem->position = 0;
if (mem->mode & MZ_OPEN_MODE_CREATE)
mz_stream_mem_set_size(stream, mem->grow_size);
err = mz_stream_mem_set_size(stream, mem->grow_size);
else
mem->limit = mem->size;
return MZ_OK;
return err;
}
int32_t mz_stream_mem_is_open(void *stream)
{
mz_stream_mem *mem = (mz_stream_mem *)stream;
if (mem->buffer == NULL)
return MZ_STREAM_ERROR;
return MZ_OPEN_ERROR;
return MZ_OK;
}
@@ -108,7 +109,7 @@ int32_t mz_stream_mem_read(void *stream, void *buf, int32_t size)
if (size > mem->size - mem->position)
size = mem->size - mem->position;
if (mem->position + size > mem->limit)
if ((size <= 0) || (mem->position + size > mem->limit))
return 0;
memcpy(buf, mem->buffer + mem->position, size);
@@ -121,6 +122,7 @@ int32_t mz_stream_mem_write(void *stream, const void *buf, int32_t size)
{
mz_stream_mem *mem = (mz_stream_mem *)stream;
int32_t new_size = 0;
int32_t err = MZ_OK;
if (size == 0)
return size;
@@ -135,7 +137,9 @@ int32_t mz_stream_mem_write(void *stream, const void *buf, int32_t size)
else
new_size += size;
mz_stream_mem_set_size(stream, new_size);
err = mz_stream_mem_set_size(stream, new_size);
if (err != MZ_OK)
return err;
}
else
{
@@ -162,6 +166,7 @@ int32_t mz_stream_mem_seek(void *stream, int64_t offset, int32_t origin)
{
mz_stream_mem *mem = (mz_stream_mem *)stream;
int64_t new_pos = 0;
int32_t err = MZ_OK;
switch (origin)
{
@@ -175,18 +180,24 @@ int32_t mz_stream_mem_seek(void *stream, int64_t offset, int32_t origin)
new_pos = offset;
break;
default:
return MZ_STREAM_ERROR;
return MZ_SEEK_ERROR;
}
if (new_pos > mem->size)
{
if ((mem->mode & MZ_OPEN_MODE_CREATE) == 0)
return MZ_STREAM_ERROR;
return MZ_SEEK_ERROR;
mz_stream_mem_set_size(stream, (int32_t)new_pos);
err = mz_stream_mem_set_size(stream, (int32_t)new_pos);
if (err != MZ_OK)
return err;
}
else if (new_pos < 0)
{
return MZ_SEEK_ERROR;
}
mem->position = (uint32_t)new_pos;
mem->position = (int32_t)new_pos;
return MZ_OK;
}
@@ -194,7 +205,7 @@ int32_t mz_stream_mem_close(void *stream)
{
MZ_UNUSED(stream);
// We never return errors
/* We never return errors */
return MZ_OK;
}
@@ -202,7 +213,7 @@ int32_t mz_stream_mem_error(void *stream)
{
MZ_UNUSED(stream);
// We never return errors
/* We never return errors */
return MZ_OK;
}
@@ -223,7 +234,7 @@ int32_t mz_stream_mem_get_buffer_at(void *stream, int64_t position, const void *
{
mz_stream_mem *mem = (mz_stream_mem *)stream;
if (buf == NULL || position < 0 || mem->size < position || mem->buffer == NULL)
return MZ_STREAM_ERROR;
return MZ_SEEK_ERROR;
*buf = mem->buffer + position;
return MZ_OK;
}
+2 -4
View File
@@ -1,8 +1,8 @@
/* mz_strm_mem.h -- Stream for memory access
Version 2.5.2, August 27, 2018
Version 2.8.3, January 26, 2019
part of MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
Copyright (C) 2010-2019 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
This program is distributed under the terms of the same license as zlib.
@@ -12,8 +12,6 @@
#ifndef MZ_STREAM_MEM_H
#define MZ_STREAM_MEM_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
+41
View File
@@ -0,0 +1,41 @@
/* mz_sstrm_os.h -- Stream for filesystem access
Version 2.8.3, January 26, 2019
part of the MiniZip project
Copyright (C) 2010-2019 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_OS_H
#define MZ_STREAM_OS_H
#ifdef __cplusplus
extern "C" {
#endif
/***************************************************************************/
int32_t mz_stream_os_open(void *stream, const char *path, int32_t mode);
int32_t mz_stream_os_is_open(void *stream);
int32_t mz_stream_os_read(void *stream, void *buf, int32_t size);
int32_t mz_stream_os_write(void *stream, const void *buf, int32_t size);
int64_t mz_stream_os_tell(void *stream);
int32_t mz_stream_os_seek(void *stream, int64_t offset, int32_t origin);
int32_t mz_stream_os_close(void *stream);
int32_t mz_stream_os_error(void *stream);
void* mz_stream_os_create(void **stream);
void mz_stream_os_delete(void **stream);
void* mz_stream_os_get_interface(void);
/***************************************************************************/
#ifdef __cplusplus
}
#endif
#endif
+56 -60
View File
@@ -1,66 +1,59 @@
/* mz_strm_posix.c -- Stream for filesystem access for posix/linux
Version 2.5.2, August 27, 2018
Version 2.8.3, January 26, 2019
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
Copyright (C) 2010-2019 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
Modifications for Zip64 support
Copyright (C) 2009-2010 Mathias Svensson
http://result42.com
Copyright (C) 1998-2010 Gilles Vollant
http://www.winimage.com/zLibDll/minizip.html
https://www.winimage.com/zLibDll/minizip.html
This program is distributed under the terms of the same license as zlib.
See the accompanying LICENSE file for the full text of the license.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "mz.h"
#include "mz_strm.h"
#include "mz_strm_posix.h"
#include "mz_strm_os.h"
#include <stdio.h> /* fopen, fread.. */
#include <errno.h>
/***************************************************************************/
#if defined(MZ_FILE32_API)
# define fopen64 fopen
# define ftello64 ftell
# define fseeko64 fseek
#else
# if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || \
defined(__OpenBSD__) || defined(__APPLE__) || defined(__ANDROID__)
# define fopen64 fopen
#define fopen64 fopen
#ifndef MZ_FILE32_API
# ifndef NO_FSEEKO
# define ftello64 ftello
# define fseeko64 fseeko
# elif defined(_MSC_VER) && (_MSC_VER >= 1400)
# define ftello64 _ftelli64
# define fseeko64 _fseeki64
# endif
# ifdef _MSC_VER
# define fopen64 fopen
# if (_MSC_VER >= 1400) && (!(defined(NO_MSCVER_FILE64_FUNC)))
# define ftello64 _ftelli64
# define fseeko64 _fseeki64
# else /* old MSC */
# define ftello64 ftell
# define fseeko64 fseek
# endif
# endif
#endif
#ifndef ftello64
# define ftello64 ftell
#endif
#ifndef fseeko64
# define fseeko64 fseek
#endif
/***************************************************************************/
static mz_stream_vtbl mz_stream_posix_vtbl = {
mz_stream_posix_open,
mz_stream_posix_is_open,
mz_stream_posix_read,
mz_stream_posix_write,
mz_stream_posix_tell,
mz_stream_posix_seek,
mz_stream_posix_close,
mz_stream_posix_error,
mz_stream_posix_create,
mz_stream_posix_delete,
static mz_stream_vtbl mz_stream_os_vtbl = {
mz_stream_os_open,
mz_stream_os_is_open,
mz_stream_os_read,
mz_stream_os_write,
mz_stream_os_tell,
mz_stream_os_seek,
mz_stream_os_close,
mz_stream_os_error,
mz_stream_os_create,
mz_stream_os_delete,
NULL,
NULL
};
@@ -76,13 +69,13 @@ typedef struct mz_stream_posix_s
/***************************************************************************/
int32_t mz_stream_posix_open(void *stream, const char *path, int32_t mode)
int32_t mz_stream_os_open(void *stream, const char *path, int32_t mode)
{
mz_stream_posix *posix = (mz_stream_posix *)stream;
const char *mode_fopen = NULL;
if (path == NULL)
return MZ_STREAM_ERROR;
return MZ_PARAM_ERROR;
if ((mode & MZ_OPEN_MODE_READWRITE) == MZ_OPEN_MODE_READ)
mode_fopen = "rb";
@@ -91,63 +84,66 @@ int32_t mz_stream_posix_open(void *stream, const char *path, int32_t mode)
else if (mode & MZ_OPEN_MODE_CREATE)
mode_fopen = "wb";
else
return MZ_STREAM_ERROR;
return MZ_OPEN_ERROR;
posix->handle = fopen64(path, mode_fopen);
if (posix->handle == NULL)
{
posix->error = errno;
return MZ_STREAM_ERROR;
return MZ_OPEN_ERROR;
}
if (mode & MZ_OPEN_MODE_APPEND)
return mz_stream_os_seek(stream, 0, MZ_SEEK_END);
return MZ_OK;
}
int32_t mz_stream_posix_is_open(void *stream)
int32_t mz_stream_os_is_open(void *stream)
{
mz_stream_posix *posix = (mz_stream_posix*)stream;
if (posix->handle == NULL)
return MZ_STREAM_ERROR;
return MZ_OPEN_ERROR;
return MZ_OK;
}
int32_t mz_stream_posix_read(void *stream, void *buf, int32_t size)
int32_t mz_stream_os_read(void *stream, void *buf, int32_t size)
{
mz_stream_posix *posix = (mz_stream_posix*)stream;
int32_t read = (int32_t)fread(buf, 1, (size_t)size, posix->handle);
if (read < size && ferror(posix->handle))
{
posix->error = errno;
return MZ_STREAM_ERROR;
return MZ_READ_ERROR;
}
return read;
}
int32_t mz_stream_posix_write(void *stream, const void *buf, int32_t size)
int32_t mz_stream_os_write(void *stream, const void *buf, int32_t size)
{
mz_stream_posix *posix = (mz_stream_posix*)stream;
int32_t written = (int32_t)fwrite(buf, 1, (size_t)size, posix->handle);
if (written < size && ferror(posix->handle))
{
posix->error = errno;
return MZ_STREAM_ERROR;
return MZ_WRITE_ERROR;
}
return written;
}
int64_t mz_stream_posix_tell(void *stream)
int64_t mz_stream_os_tell(void *stream)
{
mz_stream_posix *posix = (mz_stream_posix*)stream;
int64_t position = ftello64(posix->handle);
if (position == -1)
{
posix->error = errno;
return MZ_STREAM_ERROR;
return MZ_TELL_ERROR;
}
return position;
}
int32_t mz_stream_posix_seek(void *stream, int64_t offset, int32_t origin)
int32_t mz_stream_os_seek(void *stream, int64_t offset, int32_t origin)
{
mz_stream_posix *posix = (mz_stream_posix*)stream;
int32_t fseek_origin = 0;
@@ -164,19 +160,19 @@ int32_t mz_stream_posix_seek(void *stream, int64_t offset, int32_t origin)
fseek_origin = SEEK_SET;
break;
default:
return MZ_STREAM_ERROR;
return MZ_SEEK_ERROR;
}
if (fseeko64(posix->handle, offset, fseek_origin) != 0)
{
posix->error = errno;
return MZ_STREAM_ERROR;
return MZ_SEEK_ERROR;
}
return MZ_OK;
}
int32_t mz_stream_posix_close(void *stream)
int32_t mz_stream_os_close(void *stream)
{
mz_stream_posix *posix = (mz_stream_posix*)stream;
int32_t closed = 0;
@@ -188,18 +184,18 @@ int32_t mz_stream_posix_close(void *stream)
if (closed != 0)
{
posix->error = errno;
return MZ_STREAM_ERROR;
return MZ_CLOSE_ERROR;
}
return MZ_OK;
}
int32_t mz_stream_posix_error(void *stream)
int32_t mz_stream_os_error(void *stream)
{
mz_stream_posix *posix = (mz_stream_posix*)stream;
return posix->error;
}
void *mz_stream_posix_create(void **stream)
void *mz_stream_os_create(void **stream)
{
mz_stream_posix *posix = NULL;
@@ -207,7 +203,7 @@ void *mz_stream_posix_create(void **stream)
if (posix != NULL)
{
memset(posix, 0, sizeof(mz_stream_posix));
posix->stream.vtbl = &mz_stream_posix_vtbl;
posix->stream.vtbl = &mz_stream_os_vtbl;
}
if (stream != NULL)
*stream = posix;
@@ -215,7 +211,7 @@ void *mz_stream_posix_create(void **stream)
return posix;
}
void mz_stream_posix_delete(void **stream)
void mz_stream_os_delete(void **stream)
{
mz_stream_posix *posix = NULL;
if (stream == NULL)
@@ -226,7 +222,7 @@ void mz_stream_posix_delete(void **stream)
*stream = NULL;
}
void *mz_stream_posix_get_interface(void)
void *mz_stream_os_get_interface(void)
{
return (void *)&mz_stream_posix_vtbl;
return (void *)&mz_stream_os_vtbl;
}
+89 -62
View File
@@ -1,26 +1,26 @@
/* mz_strm_win32.c -- Stream for filesystem access for windows
Version 2.5.2, August 27, 2018
Version 2.8.3, January 26, 2019
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
Copyright (C) 2010-2019 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
https://www.winimage.com/zLibDll/minizip.html
This program is distributed under the terms of the same license as zlib.
See the accompanying LICENSE file for the full text of the license.
*/
#include <stdlib.h>
#include <windows.h>
#include "mz.h"
#include "mz_os.h"
#include "mz_strm.h"
#include "mz_strm_win32.h"
#include "mz_strm_os.h"
#include <windows.h>
/***************************************************************************/
@@ -40,17 +40,17 @@
/***************************************************************************/
static mz_stream_vtbl mz_stream_win32_vtbl = {
mz_stream_win32_open,
mz_stream_win32_is_open,
mz_stream_win32_read,
mz_stream_win32_write,
mz_stream_win32_tell,
mz_stream_win32_seek,
mz_stream_win32_close,
mz_stream_win32_error,
mz_stream_win32_create,
mz_stream_win32_delete,
static mz_stream_vtbl mz_stream_os_vtbl = {
mz_stream_os_open,
mz_stream_os_is_open,
mz_stream_os_read,
mz_stream_os_write,
mz_stream_os_tell,
mz_stream_os_seek,
mz_stream_os_close,
mz_stream_os_error,
mz_stream_os_create,
mz_stream_os_delete,
NULL,
NULL
};
@@ -66,7 +66,15 @@ typedef struct mz_stream_win32_s
/***************************************************************************/
int32_t mz_stream_win32_open(void *stream, const char *path, int32_t mode)
#if 0
# define mz_stream_os_print printf
#else
# define mz_stream_os_print(fmt,...)
#endif
/***************************************************************************/
int32_t mz_stream_os_open(void *stream, const char *path, int32_t mode)
{
mz_stream_win32 *win32 = (mz_stream_win32 *)stream;
uint32_t desired_access = 0;
@@ -74,11 +82,13 @@ int32_t mz_stream_win32_open(void *stream, const char *path, int32_t mode)
uint32_t share_mode = FILE_SHARE_READ;
uint32_t flags_attribs = FILE_ATTRIBUTE_NORMAL;
wchar_t *path_wide = NULL;
uint32_t path_wide_size = 0;
if (path == NULL)
return MZ_STREAM_ERROR;
return MZ_PARAM_ERROR;
/* Some use cases require write sharing as well */
share_mode |= FILE_SHARE_WRITE;
if ((mode & MZ_OPEN_MODE_READWRITE) == MZ_OPEN_MODE_READ)
{
@@ -97,50 +107,52 @@ int32_t mz_stream_win32_open(void *stream, const char *path, int32_t mode)
}
else
{
return MZ_STREAM_ERROR;
return MZ_PARAM_ERROR;
}
path_wide_size = MultiByteToWideChar(CP_UTF8, 0, path, -1, NULL, 0);
path_wide = (wchar_t *)MZ_ALLOC((path_wide_size + 1) * sizeof(wchar_t));
memset(path_wide, 0, sizeof(wchar_t) * (path_wide_size + 1));
mz_stream_os_print("Win32 - Open - %s (mode %"PRId32")\n", path);
MultiByteToWideChar(CP_UTF8, 0, path, -1, path_wide, path_wide_size);
path_wide = mz_os_unicode_string_create(path, MZ_ENCODING_UTF8);
if (path_wide == NULL)
return MZ_PARAM_ERROR;
#ifdef MZ_WINRT_API
win32->handle = CreateFile2W(path_wide, desired_access, share_mode, creation_disposition, NULL);
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);
win32->handle = CreateFileW(path_wide, desired_access, share_mode, NULL,
creation_disposition, flags_attribs, NULL);
#endif
MZ_FREE(path_wide);
mz_os_unicode_string_delete(&path_wide);
if (mz_stream_win32_is_open(stream) != MZ_OK)
if (mz_stream_os_is_open(stream) != MZ_OK)
{
win32->error = GetLastError();
return MZ_STREAM_ERROR;
return MZ_OPEN_ERROR;
}
if (mode & MZ_OPEN_MODE_APPEND)
return mz_stream_win32_seek(stream, 0, MZ_SEEK_END);
return mz_stream_os_seek(stream, 0, MZ_SEEK_END);
return MZ_OK;
}
int32_t mz_stream_win32_is_open(void *stream)
int32_t mz_stream_os_is_open(void *stream)
{
mz_stream_win32 *win32 = (mz_stream_win32 *)stream;
if (win32->handle == NULL || win32->handle == INVALID_HANDLE_VALUE)
return MZ_STREAM_ERROR;
return MZ_OPEN_ERROR;
return MZ_OK;
}
int32_t mz_stream_win32_read(void *stream, void *buf, int32_t size)
int32_t mz_stream_os_read(void *stream, void *buf, int32_t size)
{
mz_stream_win32 *win32 = (mz_stream_win32 *)stream;
uint32_t read = 0;
if (mz_stream_win32_is_open(stream) != MZ_OK)
return MZ_STREAM_ERROR;
if (mz_stream_os_is_open(stream) != MZ_OK)
return MZ_OPEN_ERROR;
if (!ReadFile(win32->handle, buf, size, (DWORD *)&read, NULL))
{
@@ -149,16 +161,18 @@ int32_t mz_stream_win32_read(void *stream, void *buf, int32_t size)
win32->error = 0;
}
mz_stream_os_print("Win32 - Read - %"PRId32"\n", read);
return read;
}
int32_t mz_stream_win32_write(void *stream, const void *buf, int32_t size)
int32_t mz_stream_os_write(void *stream, const void *buf, int32_t size)
{
mz_stream_win32 *win32 = (mz_stream_win32 *)stream;
uint32_t written = 0;
int32_t written = 0;
if (mz_stream_win32_is_open(stream) != MZ_OK)
return MZ_STREAM_ERROR;
if (mz_stream_os_is_open(stream) != MZ_OK)
return MZ_OPEN_ERROR;
if (!WriteFile(win32->handle, buf, size, (DWORD *)&written, NULL))
{
@@ -167,19 +181,25 @@ int32_t mz_stream_win32_write(void *stream, const void *buf, int32_t size)
win32->error = 0;
}
mz_stream_os_print("Win32 - Write - %"PRId32"\n", written);
return written;
}
static int32_t mz_stream_win32_seekinternal(HANDLE handle, LARGE_INTEGER large_pos, LARGE_INTEGER *new_pos, uint32_t move_method)
static int32_t mz_stream_os_seekinternal(HANDLE handle, LARGE_INTEGER large_pos,
LARGE_INTEGER *new_pos, uint32_t move_method)
{
#ifdef MZ_WINRT_API
return SetFilePointerEx(handle, pos, newPos, dwMoveMethod);
#else
LONG high_part = large_pos.HighPart;
uint32_t pos = SetFilePointer(handle, large_pos.LowPart, &high_part, move_method);
LONG high_part = 0;
uint32_t pos = 0;
high_part = large_pos.HighPart;
pos = SetFilePointer(handle, large_pos.LowPart, &high_part, move_method);
if ((pos == INVALID_SET_FILE_POINTER) && (GetLastError() != NO_ERROR))
return MZ_STREAM_ERROR;
return MZ_SEEK_ERROR;
if (new_pos != NULL)
{
@@ -191,31 +211,34 @@ static int32_t mz_stream_win32_seekinternal(HANDLE handle, LARGE_INTEGER large_p
#endif
}
int64_t mz_stream_win32_tell(void *stream)
int64_t mz_stream_os_tell(void *stream)
{
mz_stream_win32 *win32 = (mz_stream_win32 *)stream;
LARGE_INTEGER large_pos;
if (mz_stream_win32_is_open(stream) != MZ_OK)
return MZ_STREAM_ERROR;
if (mz_stream_os_is_open(stream) != MZ_OK)
return MZ_OPEN_ERROR;
large_pos.QuadPart = 0;
if (mz_stream_win32_seekinternal(win32->handle, large_pos, &large_pos, FILE_CURRENT) != MZ_OK)
if (mz_stream_os_seekinternal(win32->handle, large_pos, &large_pos, FILE_CURRENT) != MZ_OK)
win32->error = GetLastError();
mz_stream_os_print("Win32 - Tell - %"PRId64"\n", large_pos.QuadPart);
return large_pos.QuadPart;
}
int32_t mz_stream_win32_seek(void *stream, int64_t offset, int32_t origin)
int32_t mz_stream_os_seek(void *stream, int64_t offset, int32_t origin)
{
mz_stream_win32 *win32 = (mz_stream_win32 *)stream;
uint32_t move_method = 0xFFFFFFFF;
int32_t err = MZ_OK;
LARGE_INTEGER large_pos;
if (mz_stream_win32_is_open(stream) == MZ_STREAM_ERROR)
return MZ_STREAM_ERROR;
if (mz_stream_os_is_open(stream) != MZ_OK)
return MZ_OPEN_ERROR;
switch (origin)
{
@@ -229,37 +252,41 @@ int32_t mz_stream_win32_seek(void *stream, int64_t offset, int32_t origin)
move_method = FILE_BEGIN;
break;
default:
return MZ_STREAM_ERROR;
return MZ_SEEK_ERROR;
}
mz_stream_os_print("Win32 - Seek - %"PRId64" (origin %"PRId32")\n", offset, origin);
large_pos.QuadPart = offset;
if (mz_stream_win32_seekinternal(win32->handle, large_pos, NULL, move_method) != MZ_OK)
err = mz_stream_os_seekinternal(win32->handle, large_pos, NULL, move_method);
if (err != MZ_OK)
{
win32->error = GetLastError();
return MZ_STREAM_ERROR;
return err;
}
return MZ_OK;
}
int mz_stream_win32_close(void *stream)
int32_t mz_stream_os_close(void *stream)
{
mz_stream_win32 *win32 = (mz_stream_win32 *)stream;
if (win32->handle != NULL)
CloseHandle(win32->handle);
mz_stream_os_print("Win32 - Close\n");
win32->handle = NULL;
return MZ_OK;
}
int mz_stream_win32_error(void *stream)
int32_t mz_stream_os_error(void *stream)
{
mz_stream_win32 *win32 = (mz_stream_win32 *)stream;
return win32->error;
}
void *mz_stream_win32_create(void **stream)
void *mz_stream_os_create(void **stream)
{
mz_stream_win32 *win32 = NULL;
@@ -267,7 +294,7 @@ void *mz_stream_win32_create(void **stream)
if (win32 != NULL)
{
memset(win32, 0, sizeof(mz_stream_win32));
win32->stream.vtbl = &mz_stream_win32_vtbl;
win32->stream.vtbl = &mz_stream_os_vtbl;
}
if (stream != NULL)
*stream = win32;
@@ -275,7 +302,7 @@ void *mz_stream_win32_create(void **stream)
return win32;
}
void mz_stream_win32_delete(void **stream)
void mz_stream_os_delete(void **stream)
{
mz_stream_win32 *win32 = NULL;
if (stream == NULL)
@@ -286,7 +313,7 @@ void mz_stream_win32_delete(void **stream)
*stream = NULL;
}
void *mz_stream_win32_get_interface(void)
void *mz_stream_os_get_interface(void)
{
return (void *)&mz_stream_win32_vtbl;
return (void *)&mz_stream_os_vtbl;
}
+83 -58
View File
@@ -1,12 +1,12 @@
/* mz_strm_pkcrypt.c -- Code for traditional PKWARE encryption
Version 2.5.2, August 27, 2018
Version 2.8.3, January 26, 2019
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
Copyright (C) 2010-2019 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
https://www.winimage.com/zLibDll/minizip.html
Copyright (C) 2003 Terry Thorsen
This code is a modified version of crypting code in Info-ZIP distribution
@@ -23,22 +23,13 @@
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mz.h"
#include "mz_os.h"
#include "mz_crypt.h"
#include "mz_strm.h"
#include "mz_strm_crc32.h"
#include "mz_strm_pkcrypt.h"
/***************************************************************************/
#define RAND_HEAD_LEN (12)
/***************************************************************************/
static mz_stream_vtbl mz_stream_pkcrypt_vtbl = {
mz_stream_pkcrypt_open,
mz_stream_pkcrypt_is_open,
@@ -51,7 +42,7 @@ static mz_stream_vtbl mz_stream_pkcrypt_vtbl = {
mz_stream_pkcrypt_create,
mz_stream_pkcrypt_delete,
mz_stream_pkcrypt_get_prop_int64,
NULL
mz_stream_pkcrypt_set_prop_int64
};
/***************************************************************************/
@@ -60,26 +51,25 @@ typedef struct mz_stream_pkcrypt_s {
mz_stream stream;
int32_t error;
int16_t initialized;
uint8_t buffer[INT16_MAX];
uint8_t buffer[UINT16_MAX];
int64_t total_in;
int64_t max_total_in;
int64_t total_out;
uint32_t keys[3]; // keys defining the pseudo-random sequence
uint32_t keys[3]; /* keys defining the pseudo-random sequence */
uint8_t verify1;
uint8_t verify2;
const char *password;
mz_stream_crc32_update
crc32_update;
} mz_stream_pkcrypt;
/***************************************************************************/
#define mz_stream_pkcrypt_decode(strm, c) \
(mz_stream_pkcrypt_update_keys(strm, \
#define mz_stream_pkcrypt_decode(strm, c) \
(mz_stream_pkcrypt_update_keys(strm, \
c ^= mz_stream_pkcrypt_decrypt_byte(strm)))
#define mz_stream_pkcrypt_encode(strm, c, t) \
(t = mz_stream_pkcrypt_decrypt_byte(strm), \
mz_stream_pkcrypt_update_keys(strm, c), t^(c))
#define mz_stream_pkcrypt_encode(strm, c, t) \
(t = mz_stream_pkcrypt_decrypt_byte(strm), \
mz_stream_pkcrypt_update_keys(strm, (uint8_t)c), (uint8_t)(t^(c)))
/***************************************************************************/
@@ -87,9 +77,9 @@ static uint8_t mz_stream_pkcrypt_decrypt_byte(void *stream)
{
mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
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.
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 = pkcrypt->keys[2] | 2;
return (uint8_t)(((temp * (temp ^ 1)) >> 8) & 0xff);
@@ -100,14 +90,14 @@ static uint8_t mz_stream_pkcrypt_update_keys(void *stream, uint8_t c)
mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
uint8_t buf = c;
pkcrypt->keys[0] = (uint32_t)~pkcrypt->crc32_update(~pkcrypt->keys[0], &buf, 1);
pkcrypt->keys[0] = (uint32_t)~mz_crypt_crc32_update(~pkcrypt->keys[0], &buf, 1);
pkcrypt->keys[1] += pkcrypt->keys[0] & 0xff;
pkcrypt->keys[1] *= 134775813L;
pkcrypt->keys[1] += 1;
buf = (uint8_t)(pkcrypt->keys[1] >> 24);
pkcrypt->keys[2] = (uint32_t)~pkcrypt->crc32_update(~pkcrypt->keys[2], &buf, 1);
pkcrypt->keys[2] = (uint32_t)~mz_crypt_crc32_update(~pkcrypt->keys[2], &buf, 1);
return (uint8_t)c;
}
@@ -122,7 +112,7 @@ static void mz_stream_pkcrypt_init_keys(void *stream, const char *password)
while (*password != 0)
{
mz_stream_pkcrypt_update_keys(stream, *password);
mz_stream_pkcrypt_update_keys(stream, (uint8_t)*password);
password += 1;
}
}
@@ -136,7 +126,7 @@ int32_t mz_stream_pkcrypt_open(void *stream, const char *path, int32_t mode)
int16_t i = 0;
uint8_t verify1 = 0;
uint8_t verify2 = 0;
uint8_t header[RAND_HEAD_LEN];
uint8_t header[MZ_PKCRYPT_HEADER_SIZE];
const char *password = path;
pkcrypt->total_in = 0;
@@ -144,14 +134,11 @@ int32_t mz_stream_pkcrypt_open(void *stream, const char *path, int32_t mode)
pkcrypt->initialized = 0;
if (mz_stream_is_open(pkcrypt->stream.base) != MZ_OK)
return MZ_STREAM_ERROR;
return MZ_OPEN_ERROR;
if (password == NULL)
password = pkcrypt->password;
if (password == NULL)
return MZ_STREAM_ERROR;
if (mz_stream_crc32_get_update_func(&pkcrypt->crc32_update) != MZ_OK)
return MZ_PARAM_ERROR;
mz_stream_pkcrypt_init_keys(stream, password);
@@ -164,20 +151,20 @@ int32_t mz_stream_pkcrypt_open(void *stream, const char *path, int32_t mode)
return MZ_SUPPORT_ERROR;
#else
// First generate RAND_HEAD_LEN - 2 random bytes.
mz_os_rand(header, RAND_HEAD_LEN - 2);
/* First generate RAND_HEAD_LEN - 2 random bytes. */
mz_crypt_rand(header, MZ_PKCRYPT_HEADER_SIZE - 2);
// Encrypt random header (last two bytes is high word of crc)
for (i = 0; i < RAND_HEAD_LEN - 2; i++)
/* Encrypt random header (last two bytes is high word of crc) */
for (i = 0; i < MZ_PKCRYPT_HEADER_SIZE - 2; i++)
header[i] = mz_stream_pkcrypt_encode(stream, header[i], t);
header[i++] = mz_stream_pkcrypt_encode(stream, pkcrypt->verify1, t);
header[i++] = mz_stream_pkcrypt_encode(stream, pkcrypt->verify2, t);
if (mz_stream_write(pkcrypt->stream.base, header, RAND_HEAD_LEN) != RAND_HEAD_LEN)
return MZ_STREAM_ERROR;
if (mz_stream_write(pkcrypt->stream.base, header, sizeof(header)) != sizeof(header))
return MZ_WRITE_ERROR;
pkcrypt->total_out += RAND_HEAD_LEN;
pkcrypt->total_out += MZ_PKCRYPT_HEADER_SIZE;
#endif
}
else if (mode & MZ_OPEN_MODE_READ)
@@ -190,21 +177,21 @@ int32_t mz_stream_pkcrypt_open(void *stream, const char *path, int32_t mode)
return MZ_SUPPORT_ERROR;
#else
if (mz_stream_read(pkcrypt->stream.base, header, RAND_HEAD_LEN) != RAND_HEAD_LEN)
return MZ_STREAM_ERROR;
if (mz_stream_read(pkcrypt->stream.base, header, sizeof(header)) != sizeof(header))
return MZ_READ_ERROR;
for (i = 0; i < RAND_HEAD_LEN - 2; i++)
for (i = 0; i < MZ_PKCRYPT_HEADER_SIZE - 2; i++)
header[i] = mz_stream_pkcrypt_decode(stream, header[i]);
verify1 = mz_stream_pkcrypt_decode(stream, header[i++]);
verify2 = mz_stream_pkcrypt_decode(stream, header[i++]);
// Older versions used 2 byte check, newer versions use 1 byte check.
/* Older versions used 2 byte check, newer versions use 1 byte check. */
MZ_UNUSED(verify1);
if ((verify2 != 0) && (verify2 != pkcrypt->verify2))
return MZ_PASSWORD_ERROR;
pkcrypt->total_in += RAND_HEAD_LEN;
pkcrypt->total_in += MZ_PKCRYPT_HEADER_SIZE;
#endif
}
@@ -216,7 +203,7 @@ 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_OPEN_ERROR;
return MZ_OK;
}
@@ -224,15 +211,22 @@ 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 bytes_to_read = size;
int32_t read = 0;
int32_t i = 0;
read = mz_stream_read(pkcrypt->stream.base, buf, size);
if ((int64_t)bytes_to_read > (pkcrypt->max_total_in - pkcrypt->total_in))
bytes_to_read = (int32_t)(pkcrypt->max_total_in - pkcrypt->total_in);
read = mz_stream_read(pkcrypt->stream.base, buf, bytes_to_read);
for (i = 0; i < read; i++)
buf_ptr[i] = mz_stream_pkcrypt_decode(stream, buf_ptr[i]);
pkcrypt->total_in += read;
if (read > 0)
pkcrypt->total_in += read;
return read;
}
@@ -240,22 +234,36 @@ 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 bytes_to_write = sizeof(pkcrypt->buffer);
int32_t total_written = 0;
int32_t written = 0;
int32_t i = 0;
uint16_t t = 0;
if (size > (int32_t)sizeof(pkcrypt->buffer))
return MZ_STREAM_ERROR;
if (size < 0)
return MZ_PARAM_ERROR;
for (i = 0; i < size; i++)
pkcrypt->buffer[i] = mz_stream_pkcrypt_encode(stream, buf_ptr[i], t);
do
{
if (bytes_to_write > (size - total_written))
bytes_to_write = (size - total_written);
written = mz_stream_write(pkcrypt->stream.base, pkcrypt->buffer, size);
for (i = 0; i < bytes_to_write; i += 1)
{
pkcrypt->buffer[i] = mz_stream_pkcrypt_encode(stream, *buf_ptr, t);
buf_ptr += 1;
}
if (written > 0)
pkcrypt->total_out += written;
written = mz_stream_write(pkcrypt->stream.base, pkcrypt->buffer, bytes_to_write);
if (written < 0)
return written;
return written;
total_written += written;
}
while (total_written < size && written > 0);
pkcrypt->total_out += total_written;
return total_written;
}
int64_t mz_stream_pkcrypt_tell(void *stream)
@@ -314,8 +322,11 @@ int32_t mz_stream_pkcrypt_get_prop_int64(void *stream, int32_t prop, int64_t *va
case MZ_STREAM_PROP_TOTAL_OUT:
*value = pkcrypt->total_out;
break;
case MZ_STREAM_PROP_TOTAL_IN_MAX:
*value = pkcrypt->max_total_in;
break;
case MZ_STREAM_PROP_HEADER_SIZE:
*value = RAND_HEAD_LEN;
*value = MZ_PKCRYPT_HEADER_SIZE;
break;
case MZ_STREAM_PROP_FOOTER_SIZE:
*value = 0;
@@ -326,6 +337,20 @@ int32_t mz_stream_pkcrypt_get_prop_int64(void *stream, int32_t prop, int64_t *va
return MZ_OK;
}
int32_t mz_stream_pkcrypt_set_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_MAX:
pkcrypt->max_total_in = value;
break;
default:
return MZ_EXIST_ERROR;
}
return MZ_OK;
}
void *mz_stream_pkcrypt_create(void **stream)
{
mz_stream_pkcrypt *pkcrypt = NULL;
+3 -4
View File
@@ -1,8 +1,8 @@
/* mz_strm_pkcrypt.h -- Code for traditional PKWARE encryption
Version 2.5.2, August 27, 2018
Version 2.8.3, January 26, 2019
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
Copyright (C) 2010-2019 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
This program is distributed under the terms of the same license as zlib.
@@ -12,8 +12,6 @@
#ifndef MZ_STREAM_PKCRYPT_H
#define MZ_STREAM_PKCRYPT_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
@@ -33,6 +31,7 @@ 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);
int32_t mz_stream_pkcrypt_set_prop_int64(void *stream, int32_t prop, int64_t value);
void* mz_stream_pkcrypt_create(void **stream);
void mz_stream_pkcrypt_delete(void **stream);
-62
View File
@@ -1,62 +0,0 @@
/* mz_strm_posix.h -- Stream for filesystem access for posix/linux
Version 2.5.2, August 27, 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_POSIX_H
#define MZ_STREAM_POSIX_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/***************************************************************************/
int32_t mz_stream_posix_open(void *stream, const char *path, int32_t mode);
int32_t mz_stream_posix_is_open(void *stream);
int32_t mz_stream_posix_read(void *stream, void *buf, int32_t size);
int32_t mz_stream_posix_write(void *stream, const void *buf, int32_t size);
int64_t mz_stream_posix_tell(void *stream);
int32_t mz_stream_posix_seek(void *stream, int64_t offset, int32_t origin);
int32_t mz_stream_posix_close(void *stream);
int32_t mz_stream_posix_error(void *stream);
void* mz_stream_posix_create(void **stream);
void mz_stream_posix_delete(void **stream);
void* mz_stream_posix_get_interface(void);
/***************************************************************************/
#if !defined(_WIN32) && !defined(MZ_WIN32_API)
#define mz_stream_os_open mz_stream_posix_open
#define mz_stream_os_is_open mz_stream_posix_is_open
#define mz_stream_os_read mz_stream_posix_read
#define mz_stream_os_write mz_stream_posix_write
#define mz_stream_os_tell mz_stream_posix_tell
#define mz_stream_os_seek mz_stream_posix_seek
#define mz_stream_os_close mz_stream_posix_close
#define mz_stream_os_error mz_stream_posix_error
#define mz_stream_os_create mz_stream_posix_create
#define mz_stream_os_delete mz_stream_posix_delete
#define mz_stream_os_get_interface \
mz_stream_posix_get_interface
#endif
/***************************************************************************/
#ifdef __cplusplus
}
#endif
#endif
+96 -30
View File
@@ -1,8 +1,8 @@
/* mz_strm_split.c -- Stream for split files
Version 2.5.2, August 27, 2018
Version 2.8.3, January 26, 2019
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
Copyright (C) 2010-2019 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
This program is distributed under the terms of the same license as zlib.
@@ -10,25 +10,23 @@
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mz.h"
#include "mz_os.h"
#include "mz_strm.h"
#include "mz_strm_split.h"
/***************************************************************************/
#include <stdio.h> /* snprintf */
#define MZ_ZIP_MAGIC_DISKHEADER (0x08074b50)
#if defined(_MSC_VER) && _MSC_VER < 1900
#if defined(_MSC_VER) && (_MSC_VER < 1900)
# define snprintf _snprintf
#endif
/***************************************************************************/
#define MZ_ZIP_MAGIC_DISKHEADER (0x08074b50)
/***************************************************************************/
static mz_stream_vtbl mz_stream_split_vtbl = {
mz_stream_split_open,
mz_stream_split_is_open,
@@ -56,16 +54,25 @@ typedef struct mz_stream_split_s {
int64_t total_out_disk;
int32_t mode;
char *path_cd;
int32_t path_cd_size;
uint32_t path_cd_size;
char *path_disk;
int32_t path_disk_size;
uint32_t path_disk_size;
int32_t number_disk;
int32_t current_disk;
int64_t current_disk_size;
int32_t reached_end;
} mz_stream_split;
/***************************************************************************/
#if 0
# define mz_stream_split_print printf
#else
# define mz_stream_split_print(fmt,...)
#endif
/***************************************************************************/
static int32_t mz_stream_split_open_disk(void *stream, int32_t number_disk)
{
mz_stream_split *split = (mz_stream_split *)stream;
@@ -75,7 +82,7 @@ static int32_t mz_stream_split_open_disk(void *stream, int32_t number_disk)
int16_t disk_part = 0;
// Check if we are reading or writing a disk part or the cd disk
/* Check if we are reading or writing a disk part or the cd disk */
if (number_disk >= 0)
{
if ((split->mode & MZ_OPEN_MODE_WRITE) == 0)
@@ -84,23 +91,27 @@ static int32_t mz_stream_split_open_disk(void *stream, int32_t number_disk)
disk_part = MZ_OPEN_MODE_WRITE;
}
// Construct disk path
/* Construct disk path */
if (disk_part > 0)
{
for (i = (int32_t)strlen(split->path_disk) - 1; i >= 0; i -= 1)
{
if (split->path_disk[i] != '.')
continue;
snprintf(&split->path_disk[i], split->path_disk_size - i, ".z%02d", number_disk + 1);
snprintf(&split->path_disk[i], split->path_disk_size - (uint32_t)i,
".z%02"PRId32, number_disk + 1);
break;
}
}
else
{
strncpy(split->path_disk, split->path_cd, split->path_disk_size);
strncpy(split->path_disk, split->path_cd, split->path_disk_size - 1);
split->path_disk[split->path_disk_size - 1] = 0;
}
// If disk part doesn't exist during reading then return MZ_EXIST_ERROR
mz_stream_split_print("Split - Goto disk - %s (disk %"PRId32")\n", split->path_disk, number_disk);
/* If disk part doesn't exist during reading then return MZ_EXIST_ERROR */
if (disk_part == MZ_OPEN_MODE_READ)
err = mz_os_file_exists(split->path_disk);
@@ -126,6 +137,11 @@ static int32_t mz_stream_split_open_disk(void *stream, int32_t number_disk)
{
if (split->current_disk == 0)
{
/* Get the size of the current disk we are on for seeking */
mz_stream_seek(split->stream.base, 0, MZ_SEEK_END);
split->current_disk_size = mz_stream_tell(split->stream.base);
mz_stream_seek(split->stream.base, 0, MZ_SEEK_SET);
err = mz_stream_read_uint32(split->stream.base, &magic);
if (magic != MZ_ZIP_MAGIC_DISKHEADER)
err = MZ_FORMAT_ERROR;
@@ -146,6 +162,7 @@ static int32_t mz_stream_split_close_disk(void *stream)
if (mz_stream_is_open(split->stream.base) != MZ_OK)
return MZ_OK;
mz_stream_split_print("Split - Close disk\n");
return mz_stream_close(split->stream.base);
}
@@ -153,13 +170,16 @@ static int32_t mz_stream_split_goto_disk(void *stream, int32_t number_disk)
{
mz_stream_split *split = (mz_stream_split *)stream;
int32_t err = MZ_OK;
int32_t err_is_open = MZ_OK;
err_is_open = mz_stream_is_open(split->stream.base);
if ((split->disk_size == 0) && (split->mode & MZ_OPEN_MODE_WRITE))
{
if (mz_stream_is_open(split->stream.base) != MZ_OK)
if (err_is_open != MZ_OK)
err = mz_stream_split_open_disk(stream, number_disk);
}
else if (number_disk != split->current_disk)
else if ((number_disk != split->current_disk) || (err_is_open != MZ_OK))
{
err = mz_stream_split_close_disk(stream);
if (err == MZ_OK)
@@ -180,20 +200,35 @@ int32_t mz_stream_split_open(void *stream, const char *path, int32_t mode)
split->mode = mode;
split->path_cd_size = (int32_t)strlen(path) + 1;
split->path_cd_size = (uint32_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 = (int32_t)strlen(path) + 10;
if (split->path_cd == NULL)
return MZ_MEM_ERROR;
strncpy(split->path_cd, path, split->path_cd_size - 1);
split->path_cd[split->path_cd_size - 1] = 0;
mz_stream_split_print("Split - Open - %s (disk %"PRId32")\n", split->path_cd, number_disk);
split->path_disk_size = (uint32_t)strlen(path) + 10;
split->path_disk = (char *)MZ_ALLOC(split->path_disk_size);
strncpy(split->path_disk, path, split->path_disk_size);
if (mode & MZ_OPEN_MODE_WRITE)
if (split->path_disk == NULL)
{
MZ_FREE(split->path_cd);
return MZ_MEM_ERROR;
}
strncpy(split->path_disk, path, split->path_disk_size - 1);
split->path_disk[split->path_disk_size - 1] = 0;
if ((mode & MZ_OPEN_MODE_WRITE) && ((mode & MZ_OPEN_MODE_APPEND) == 0))
{
number_disk = 0;
split->current_disk = -1;
}
else if (mode & MZ_OPEN_MODE_READ)
else
{
number_disk = -1;
split->current_disk = 0;
@@ -205,9 +240,9 @@ int32_t mz_stream_split_open(void *stream, const char *path, int32_t mode)
int32_t mz_stream_split_is_open(void *stream)
{
mz_stream_split *split = (mz_stream_split *)stream;
if (split->is_open == 1)
return MZ_OK;
return MZ_STREAM_ERROR;
if (split->is_open != 1)
return MZ_OPEN_ERROR;
return MZ_OK;
}
int32_t mz_stream_split_read(void *stream, void *buf, int32_t size)
@@ -225,15 +260,21 @@ int32_t mz_stream_split_read(void *stream, void *buf, int32_t size)
while (bytes_left > 0)
{
read = mz_stream_read(split->stream.base, buf_ptr, bytes_left);
mz_stream_split_print("Split - Read disk - %"PRId32"\n", read);
if (read < 0)
return read;
if (read == 0)
{
if (split->current_disk < 0) // No more disks to goto
if (split->current_disk < 0) /* No more disks to goto */
break;
err = mz_stream_split_goto_disk(stream, split->current_disk + 1);
if (err == MZ_EXIST_ERROR)
{
split->current_disk = -1;
break;
}
if (err != MZ_OK)
return err;
}
@@ -284,7 +325,9 @@ int32_t mz_stream_split_write(void *stream, const void *buf, int32_t size)
written = mz_stream_write(split->stream.base, buf_ptr, bytes_to_write);
if (written != bytes_to_write)
return MZ_STREAM_ERROR;
return MZ_WRITE_ERROR;
mz_stream_split_print("Split - Write disk - %"PRId32"\n", written);
bytes_left -= written;
buf_ptr += written;
@@ -308,10 +351,33 @@ int64_t mz_stream_split_tell(void *stream)
int32_t mz_stream_split_seek(void *stream, int64_t offset, int32_t origin)
{
mz_stream_split *split = (mz_stream_split *)stream;
int64_t disk_left = 0;
int64_t position = 0;
int32_t err = MZ_OK;
err = mz_stream_split_goto_disk(stream, split->number_disk);
if (err != MZ_OK)
return err;
mz_stream_split_print("Split - Seek disk - %"PRId64" (origin %"PRId32")\n", offset, origin);
if ((origin == MZ_SEEK_CUR) && (split->number_disk != -1))
{
position = mz_stream_tell(split->stream.base);
disk_left = split->current_disk_size - position;
while (offset > disk_left)
{
err = mz_stream_split_goto_disk(stream, split->current_disk + 1);
if (err != MZ_OK)
return err;
offset -= disk_left;
disk_left = split->current_disk_size;
}
}
return mz_stream_seek(split->stream.base, offset, origin);
}
+2 -4
View File
@@ -1,8 +1,8 @@
/* mz_strm_split.h -- Stream for split files
Version 2.5.2, August 27, 2018
Version 2.8.3, January 26, 2019
part of MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
Copyright (C) 2010-2019 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
This program is distributed under the terms of the same license as zlib.
@@ -12,8 +12,6 @@
#ifndef MZ_STREAM_SPLIT_H
#define MZ_STREAM_SPLIT_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
-62
View File
@@ -1,62 +0,0 @@
/* mz_sstrm_win32.h -- Stream for filesystem access for windows
Version 2.5.2, August 27, 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_WIN32_H
#define MZ_STREAM_WIN32_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/***************************************************************************/
int32_t mz_stream_win32_open(void *stream, const char *path, int32_t mode);
int32_t mz_stream_win32_is_open(void *stream);
int32_t mz_stream_win32_read(void *stream, void *buf, int32_t size);
int32_t mz_stream_win32_write(void *stream, const void *buf, int32_t size);
int64_t mz_stream_win32_tell(void *stream);
int32_t mz_stream_win32_seek(void *stream, int64_t offset, int32_t origin);
int32_t mz_stream_win32_close(void *stream);
int32_t mz_stream_win32_error(void *stream);
void* mz_stream_win32_create(void **stream);
void mz_stream_win32_delete(void **stream);
void* mz_stream_win32_get_interface(void);
/***************************************************************************/
#if defined(_WIN32) || defined(MZ_WIN32_API)
#define mz_stream_os_open mz_stream_win32_open
#define mz_stream_os_is_open mz_stream_win32_is_open
#define mz_stream_os_read mz_stream_win32_read
#define mz_stream_os_write mz_stream_win32_write
#define mz_stream_os_tell mz_stream_win32_tell
#define mz_stream_os_seek mz_stream_win32_seek
#define mz_stream_os_close mz_stream_win32_close
#define mz_stream_os_error mz_stream_win32_error
#define mz_stream_os_create mz_stream_win32_create
#define mz_stream_os_delete mz_stream_win32_delete
#define mz_stream_os_get_interface \
mz_stream_win32_get_interface
#endif
/***************************************************************************/
#ifdef __cplusplus
}
#endif
#endif
+407
View File
@@ -0,0 +1,407 @@
/* mz_strm_wzaes.c -- Stream for WinZip AES encryption
Version 2.8.3, January 26, 2019
part of the MiniZip project
Copyright (C) 2010-2019 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
Copyright (C) 1998-2010 Brian Gladman, Worcester, UK
This program is distributed under the terms of the same license as zlib.
See the accompanying LICENSE file for the full text of the license.
*/
#include "mz.h"
#include "mz_crypt.h"
#include "mz_strm.h"
#include "mz_strm_wzaes.h"
/***************************************************************************/
#define MZ_AES_KEYING_ITERATIONS (1000)
#define MZ_AES_SALT_LENGTH(MODE) (4 * (MODE & 3) + 4)
#define MZ_AES_SALT_LENGTH_MAX (16)
#define MZ_AES_PW_LENGTH_MAX (128)
#define MZ_AES_PW_VERIFY_SIZE (2)
#define MZ_AES_AUTHCODE_SIZE (10)
/***************************************************************************/
static mz_stream_vtbl mz_stream_wzaes_vtbl = {
mz_stream_wzaes_open,
mz_stream_wzaes_is_open,
mz_stream_wzaes_read,
mz_stream_wzaes_write,
mz_stream_wzaes_tell,
mz_stream_wzaes_seek,
mz_stream_wzaes_close,
mz_stream_wzaes_error,
mz_stream_wzaes_create,
mz_stream_wzaes_delete,
mz_stream_wzaes_get_prop_int64,
mz_stream_wzaes_set_prop_int64
};
/***************************************************************************/
typedef struct mz_stream_wzaes_s {
mz_stream stream;
int32_t mode;
int32_t error;
int16_t initialized;
uint8_t buffer[UINT16_MAX];
int64_t total_in;
int64_t max_total_in;
int64_t total_out;
int16_t encryption_mode;
const char *password;
void *aes;
uint32_t crypt_pos;
uint8_t crypt_block[MZ_AES_BLOCK_SIZE];
void *hmac;
uint8_t nonce[MZ_AES_BLOCK_SIZE];
} mz_stream_wzaes;
/***************************************************************************/
/***************************************************************************/
int32_t mz_stream_wzaes_open(void *stream, const char *path, int32_t mode)
{
mz_stream_wzaes *wzaes = (mz_stream_wzaes *)stream;
uint16_t salt_length = 0;
uint16_t password_length = 0;
uint16_t key_length = 0;
uint8_t kbuf[2 * MZ_AES_KEY_LENGTH_MAX + MZ_AES_PW_VERIFY_SIZE];
uint8_t verify[MZ_AES_PW_VERIFY_SIZE];
uint8_t verify_expected[MZ_AES_PW_VERIFY_SIZE];
uint8_t salt_value[MZ_AES_SALT_LENGTH_MAX];
const char *password = path;
wzaes->total_in = 0;
wzaes->total_out = 0;
wzaes->initialized = 0;
if (mz_stream_is_open(wzaes->stream.base) != MZ_OK)
return MZ_OPEN_ERROR;
if (password == NULL)
password = wzaes->password;
if (password == NULL)
return MZ_PARAM_ERROR;
password_length = (uint16_t)strlen(password);
if (password_length > MZ_AES_PW_LENGTH_MAX)
return MZ_PARAM_ERROR;
if (wzaes->encryption_mode < 1 || wzaes->encryption_mode > 3)
return MZ_PARAM_ERROR;
salt_length = MZ_AES_SALT_LENGTH(wzaes->encryption_mode);
if (mode & MZ_OPEN_MODE_WRITE)
{
#ifdef MZ_ZIP_NO_COMPRESSION
return MZ_SUPPORT_ERROR;
#else
mz_crypt_rand(salt_value, salt_length);
#endif
}
else if (mode & MZ_OPEN_MODE_READ)
{
#ifdef MZ_ZIP_NO_DECOMPRESSION
return MZ_SUPPORT_ERROR;
#else
if (mz_stream_read(wzaes->stream.base, salt_value, salt_length) != salt_length)
return MZ_READ_ERROR;
#endif
}
key_length = MZ_AES_KEY_LENGTH(wzaes->encryption_mode);
/* Derive the encryption and authentication keys and the password verifier */
mz_crypt_pbkdf2((uint8_t *)password, password_length, salt_value, salt_length,
MZ_AES_KEYING_ITERATIONS, kbuf, 2 * key_length + MZ_AES_PW_VERIFY_SIZE);
/* Initialize the encryption nonce and buffer pos */
wzaes->crypt_pos = MZ_AES_BLOCK_SIZE;
memset(wzaes->nonce, 0, sizeof(wzaes->nonce));
/* Initialize for encryption using key 1 */
mz_crypt_aes_reset(wzaes->aes);
mz_crypt_aes_set_mode(wzaes->aes, wzaes->encryption_mode);
mz_crypt_aes_set_encrypt_key(wzaes->aes, kbuf, key_length);
/* Initialize for authentication using key 2 */
mz_crypt_hmac_reset(wzaes->hmac);
mz_crypt_hmac_set_algorithm(wzaes->hmac, MZ_HASH_SHA1);
mz_crypt_hmac_init(wzaes->hmac, kbuf + key_length, key_length);
memcpy(verify, kbuf + (2 * key_length), MZ_AES_PW_VERIFY_SIZE);
if (mode & MZ_OPEN_MODE_WRITE)
{
if (mz_stream_write(wzaes->stream.base, salt_value, salt_length) != salt_length)
return MZ_WRITE_ERROR;
wzaes->total_out += salt_length;
if (mz_stream_write(wzaes->stream.base, verify, MZ_AES_PW_VERIFY_SIZE) != MZ_AES_PW_VERIFY_SIZE)
return MZ_WRITE_ERROR;
wzaes->total_out += MZ_AES_PW_VERIFY_SIZE;
}
else if (mode & MZ_OPEN_MODE_READ)
{
wzaes->total_in += salt_length;
if (mz_stream_read(wzaes->stream.base, verify_expected, MZ_AES_PW_VERIFY_SIZE) != MZ_AES_PW_VERIFY_SIZE)
return MZ_READ_ERROR;
wzaes->total_in += MZ_AES_PW_VERIFY_SIZE;
if (memcmp(verify_expected, verify, MZ_AES_PW_VERIFY_SIZE) != 0)
return MZ_PASSWORD_ERROR;
}
wzaes->mode = mode;
wzaes->initialized = 1;
return MZ_OK;
}
int32_t mz_stream_wzaes_is_open(void *stream)
{
mz_stream_wzaes *wzaes = (mz_stream_wzaes *)stream;
if (wzaes->initialized == 0)
return MZ_OPEN_ERROR;
return MZ_OK;
}
static int32_t mz_stream_wzaes_encrypt_data(void *stream, uint8_t *buf, int32_t size)
{
mz_stream_wzaes *wzaes = (mz_stream_wzaes *)stream;
uint32_t pos = wzaes->crypt_pos;
uint32_t i = 0;
int32_t err = MZ_OK;
while (i < (uint32_t)size)
{
if (pos == MZ_AES_BLOCK_SIZE)
{
uint32_t j = 0;
/* Increment encryption nonce */
while (j < 8 && !++wzaes->nonce[j])
j += 1;
/* Encrypt the nonce to form next xor buffer */
memcpy(wzaes->crypt_block, wzaes->nonce, MZ_AES_BLOCK_SIZE);
mz_crypt_aes_encrypt(wzaes->aes, wzaes->crypt_block, sizeof(wzaes->crypt_block));
pos = 0;
}
buf[i++] ^= wzaes->crypt_block[pos++];
}
wzaes->crypt_pos = pos;
return err;
}
int32_t mz_stream_wzaes_read(void *stream, void *buf, int32_t size)
{
mz_stream_wzaes *wzaes = (mz_stream_wzaes *)stream;
int64_t max_total_in = 0;
int32_t bytes_to_read = size;
int32_t read = 0;
max_total_in = wzaes->max_total_in - MZ_AES_FOOTER_SIZE;
if ((int64_t)bytes_to_read > (max_total_in - wzaes->total_in))
bytes_to_read = (int32_t)(max_total_in - wzaes->total_in);
read = mz_stream_read(wzaes->stream.base, buf, bytes_to_read);
if (read > 0)
{
mz_crypt_hmac_update(wzaes->hmac, (uint8_t *)buf, read);
mz_stream_wzaes_encrypt_data(stream, (uint8_t *)buf, read);
wzaes->total_in += read;
}
return read;
}
int32_t mz_stream_wzaes_write(void *stream, const void *buf, int32_t size)
{
mz_stream_wzaes *wzaes = (mz_stream_wzaes *)stream;
const uint8_t *buf_ptr = (const uint8_t *)buf;
int32_t bytes_to_write = sizeof(wzaes->buffer);
int32_t total_written = 0;
int32_t written = 0;
if (size < 0)
return MZ_PARAM_ERROR;
do
{
if (bytes_to_write > (size - total_written))
bytes_to_write = (size - total_written);
memcpy(wzaes->buffer, buf_ptr, bytes_to_write);
buf_ptr += bytes_to_write;
mz_stream_wzaes_encrypt_data(stream, (uint8_t *)wzaes->buffer, bytes_to_write);
mz_crypt_hmac_update(wzaes->hmac, wzaes->buffer, bytes_to_write);
written = mz_stream_write(wzaes->stream.base, wzaes->buffer, bytes_to_write);
if (written < 0)
return written;
total_written += written;
}
while (total_written < size && written > 0);
wzaes->total_out += total_written;
return total_written;
}
int64_t mz_stream_wzaes_tell(void *stream)
{
mz_stream_wzaes *wzaes = (mz_stream_wzaes *)stream;
return mz_stream_tell(wzaes->stream.base);
}
int32_t mz_stream_wzaes_seek(void *stream, int64_t offset, int32_t origin)
{
mz_stream_wzaes *wzaes = (mz_stream_wzaes *)stream;
return mz_stream_seek(wzaes->stream.base, offset, origin);
}
int32_t mz_stream_wzaes_close(void *stream)
{
mz_stream_wzaes *wzaes = (mz_stream_wzaes *)stream;
uint8_t expected_hash[MZ_AES_AUTHCODE_SIZE];
uint8_t computed_hash[MZ_HASH_SHA1_SIZE];
mz_crypt_hmac_end(wzaes->hmac, computed_hash, sizeof(computed_hash));
if (wzaes->mode & MZ_OPEN_MODE_WRITE)
{
if (mz_stream_write(wzaes->stream.base, computed_hash, MZ_AES_AUTHCODE_SIZE) != MZ_AES_AUTHCODE_SIZE)
return MZ_WRITE_ERROR;
wzaes->total_out += MZ_AES_AUTHCODE_SIZE;
}
else if (wzaes->mode & MZ_OPEN_MODE_READ)
{
if (mz_stream_read(wzaes->stream.base, expected_hash, MZ_AES_AUTHCODE_SIZE) != MZ_AES_AUTHCODE_SIZE)
return MZ_READ_ERROR;
wzaes->total_in += MZ_AES_AUTHCODE_SIZE;
/* If entire entry was not read this will fail */
if (memcmp(computed_hash, expected_hash, MZ_AES_AUTHCODE_SIZE) != 0)
return MZ_CRC_ERROR;
}
wzaes->initialized = 0;
return MZ_OK;
}
int32_t mz_stream_wzaes_error(void *stream)
{
mz_stream_wzaes *wzaes = (mz_stream_wzaes *)stream;
return wzaes->error;
}
void mz_stream_wzaes_set_password(void *stream, const char *password)
{
mz_stream_wzaes *wzaes = (mz_stream_wzaes *)stream;
wzaes->password = password;
}
void mz_stream_wzaes_set_encryption_mode(void *stream, int16_t encryption_mode)
{
mz_stream_wzaes *wzaes = (mz_stream_wzaes *)stream;
wzaes->encryption_mode = encryption_mode;
}
int32_t mz_stream_wzaes_get_prop_int64(void *stream, int32_t prop, int64_t *value)
{
mz_stream_wzaes *wzaes = (mz_stream_wzaes *)stream;
switch (prop)
{
case MZ_STREAM_PROP_TOTAL_IN:
*value = wzaes->total_in;
break;
case MZ_STREAM_PROP_TOTAL_OUT:
*value = wzaes->total_out;
break;
case MZ_STREAM_PROP_TOTAL_IN_MAX:
*value = wzaes->max_total_in;
break;
case MZ_STREAM_PROP_HEADER_SIZE:
*value = MZ_AES_SALT_LENGTH((int64_t)wzaes->encryption_mode) + MZ_AES_PW_VERIFY_SIZE;
break;
case MZ_STREAM_PROP_FOOTER_SIZE:
*value = MZ_AES_AUTHCODE_SIZE;
break;
default:
return MZ_EXIST_ERROR;
}
return MZ_OK;
}
int32_t mz_stream_wzaes_set_prop_int64(void *stream, int32_t prop, int64_t value)
{
mz_stream_wzaes *wzaes = (mz_stream_wzaes *)stream;
switch (prop)
{
case MZ_STREAM_PROP_TOTAL_IN_MAX:
wzaes->max_total_in = value;
break;
default:
return MZ_EXIST_ERROR;
}
return MZ_OK;
}
void *mz_stream_wzaes_create(void **stream)
{
mz_stream_wzaes *wzaes = NULL;
wzaes = (mz_stream_wzaes *)MZ_ALLOC(sizeof(mz_stream_wzaes));
if (wzaes != NULL)
{
memset(wzaes, 0, sizeof(mz_stream_wzaes));
wzaes->stream.vtbl = &mz_stream_wzaes_vtbl;
wzaes->encryption_mode = MZ_AES_ENCRYPTION_MODE_256;
mz_crypt_hmac_create(&wzaes->hmac);
mz_crypt_aes_create(&wzaes->aes);
}
if (stream != NULL)
*stream = wzaes;
return wzaes;
}
void mz_stream_wzaes_delete(void **stream)
{
mz_stream_wzaes *wzaes = NULL;
if (stream == NULL)
return;
wzaes = (mz_stream_wzaes *)*stream;
if (wzaes != NULL)
{
mz_crypt_aes_delete(&wzaes->aes);
mz_crypt_hmac_delete(&wzaes->hmac);
MZ_FREE(wzaes);
}
*stream = NULL;
}
void *mz_stream_wzaes_get_interface(void)
{
return (void *)&mz_stream_wzaes_vtbl;
}
+47
View File
@@ -0,0 +1,47 @@
/* mz_strm_wzaes.h -- Stream for WinZIP AES encryption
Version 2.8.3, January 26, 2019
part of the MiniZip project
Copyright (C) 2010-2019 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_WZAES_SHA1_H
#define MZ_STREAM_WZAES_SHA1_H
#ifdef __cplusplus
extern "C" {
#endif
/***************************************************************************/
int32_t mz_stream_wzaes_open(void *stream, const char *filename, int32_t mode);
int32_t mz_stream_wzaes_is_open(void *stream);
int32_t mz_stream_wzaes_read(void *stream, void *buf, int32_t size);
int32_t mz_stream_wzaes_write(void *stream, const void *buf, int32_t size);
int64_t mz_stream_wzaes_tell(void *stream);
int32_t mz_stream_wzaes_seek(void *stream, int64_t offset, int32_t origin);
int32_t mz_stream_wzaes_close(void *stream);
int32_t mz_stream_wzaes_error(void *stream);
void mz_stream_wzaes_set_password(void *stream, const char *password);
void mz_stream_wzaes_set_encryption_mode(void *stream, int16_t encryption_mode);
int32_t mz_stream_wzaes_get_prop_int64(void *stream, int32_t prop, int64_t *value);
int32_t mz_stream_wzaes_set_prop_int64(void *stream, int32_t prop, int64_t value);
void* mz_stream_wzaes_create(void **stream);
void mz_stream_wzaes_delete(void **stream);
void* mz_stream_wzaes_get_interface(void);
/***************************************************************************/
#ifdef __cplusplus
}
#endif
#endif
+43 -53
View File
@@ -1,8 +1,8 @@
/* mz_strm_zlib.c -- Stream for zlib inflate/deflate
Version 2.5.2, August 27, 2018
Version 2.8.3, January 26, 2019
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
Copyright (C) 2010-2019 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
This program is distributed under the terms of the same license as zlib.
@@ -10,29 +10,26 @@
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "zlib.h"
#include "mz.h"
#include "mz_strm.h"
#include "mz_strm_zlib.h"
/***************************************************************************/
// Define z_crc_t in zlib 1.2.5 and less or if using zlib-ng
#include "zlib.h"
#if defined(ZLIBNG_VERNUM)
typedef uint32_t z_crc_t;
#elif (ZLIB_VERNUM < 0x1270)
typedef unsigned long z_crc_t;
# include "zlib-ng.h"
#endif
/***************************************************************************/
#ifndef DEF_MEM_LEVEL
#if defined(ZLIBNG_VERNUM) && !defined(ZLIB_COMPAT)
# define ZLIB_PREFIX(x) zng_ ## x
typedef zng_stream zlib_stream;
#else
# define ZLIB_PREFIX(x) x
typedef z_stream zlib_stream;
#endif
#if !defined(DEF_MEM_LEVEL)
# if MAX_MEM_LEVEL >= 8
# define DEF_MEM_LEVEL 8
# else
@@ -61,7 +58,7 @@ static mz_stream_vtbl mz_stream_zlib_vtbl = {
typedef struct mz_stream_zlib_s {
mz_stream stream;
z_stream zstream;
zlib_stream zstream;
uint8_t buffer[INT16_MAX];
int32_t buffer_len;
int64_t total_in;
@@ -99,7 +96,8 @@ int32_t mz_stream_zlib_open(void *stream, const char *path, int32_t mode)
zlib->zstream.next_out = zlib->buffer;
zlib->zstream.avail_out = sizeof(zlib->buffer);
zlib->error = deflateInit2(&zlib->zstream, (int8_t)zlib->level, Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
zlib->error = ZLIB_PREFIX(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)
@@ -110,12 +108,12 @@ int32_t mz_stream_zlib_open(void *stream, const char *path, int32_t mode)
zlib->zstream.next_in = zlib->buffer;
zlib->zstream.avail_in = 0;
zlib->error = inflateInit2(&zlib->zstream, -MAX_WBITS);
zlib->error = ZLIB_PREFIX(inflateInit2)(&zlib->zstream, -MAX_WBITS);
#endif
}
if (zlib->error != Z_OK)
return MZ_STREAM_ERROR;
return MZ_OPEN_ERROR;
zlib->initialized = 1;
zlib->mode = mode;
@@ -126,13 +124,16 @@ int32_t mz_stream_zlib_is_open(void *stream)
{
mz_stream_zlib *zlib = (mz_stream_zlib *)stream;
if (zlib->initialized != 1)
return MZ_STREAM_ERROR;
return MZ_OPEN_ERROR;
return MZ_OK;
}
int32_t mz_stream_zlib_read(void *stream, void *buf, int32_t size)
{
#ifdef MZ_ZIP_NO_DECOMPRESSION
MZ_UNUSED(stream);
MZ_UNUSED(buf);
MZ_UNUSED(size);
return MZ_SUPPORT_ERROR;
#else
mz_stream_zlib *zlib = (mz_stream_zlib *)stream;
@@ -144,7 +145,7 @@ int32_t mz_stream_zlib_read(void *stream, void *buf, int32_t size)
uint32_t total_out = 0;
uint32_t in_bytes = 0;
uint32_t out_bytes = 0;
int32_t bytes_to_read = 0;
int32_t bytes_to_read = sizeof(zlib->buffer);
int32_t read = 0;
int32_t err = Z_OK;
@@ -156,20 +157,16 @@ int32_t mz_stream_zlib_read(void *stream, void *buf, int32_t size)
{
if (zlib->zstream.avail_in == 0)
{
bytes_to_read = sizeof(zlib->buffer);
if (zlib->max_total_in > 0)
{
if ((zlib->max_total_in - zlib->total_in) < (int64_t)sizeof(zlib->buffer))
if ((int64_t)bytes_to_read > (zlib->max_total_in - zlib->total_in))
bytes_to_read = (int32_t)(zlib->max_total_in - zlib->total_in);
}
read = mz_stream_read(zlib->stream.base, zlib->buffer, bytes_to_read);
if (read < 0)
{
zlib->error = Z_STREAM_ERROR;
break;
}
return read;
if (read == 0)
break;
@@ -180,7 +177,7 @@ int32_t mz_stream_zlib_read(void *stream, void *buf, int32_t size)
total_in_before = zlib->zstream.avail_in;
total_out_before = zlib->zstream.total_out;
err = inflate(&zlib->zstream, Z_SYNC_FLUSH);
err = ZLIB_PREFIX(inflate)(&zlib->zstream, Z_SYNC_FLUSH);
if ((err >= Z_OK) && (zlib->zstream.msg != NULL))
{
zlib->error = Z_DATA_ERROR;
@@ -201,7 +198,6 @@ int32_t mz_stream_zlib_read(void *stream, void *buf, int32_t size)
if (err == Z_STREAM_END)
break;
if (err != Z_OK)
{
zlib->error = err;
@@ -211,17 +207,21 @@ int32_t mz_stream_zlib_read(void *stream, void *buf, int32_t size)
while (zlib->zstream.avail_out > 0);
if (zlib->error != 0)
{
/* Zlib errors are compatible with MZ */
return zlib->error;
}
return total_out;
#endif
}
#ifndef MZ_ZIP_NO_COMPRESSION
static int32_t mz_stream_zlib_flush(void *stream)
{
mz_stream_zlib *zlib = (mz_stream_zlib *)stream;
if (mz_stream_write(zlib->stream.base, zlib->buffer, zlib->buffer_len) != zlib->buffer_len)
return MZ_STREAM_ERROR;
return MZ_WRITE_ERROR;
return MZ_OK;
}
@@ -238,11 +238,9 @@ static int32_t mz_stream_zlib_deflate(void *stream, int flush)
{
if (zlib->zstream.avail_out == 0)
{
if (mz_stream_zlib_flush(zlib) != MZ_OK)
{
zlib->error = Z_STREAM_ERROR;
return MZ_STREAM_ERROR;
}
err = mz_stream_zlib_flush(zlib);
if (err != MZ_OK)
return err;
zlib->zstream.avail_out = sizeof(zlib->buffer);
zlib->zstream.next_out = zlib->buffer;
@@ -251,7 +249,7 @@ static int32_t mz_stream_zlib_deflate(void *stream, int flush)
}
total_out_before = zlib->zstream.total_out;
err = deflate(&zlib->zstream, flush);
err = ZLIB_PREFIX(deflate)(&zlib->zstream, flush);
total_out_after = zlib->zstream.total_out;
out_bytes = (uint32_t)(total_out_after - total_out_before);
@@ -264,13 +262,14 @@ static int32_t mz_stream_zlib_deflate(void *stream, int flush)
if (err != Z_OK)
{
zlib->error = err;
return MZ_STREAM_ERROR;
return MZ_DATA_ERROR;
}
}
while ((zlib->zstream.avail_in > 0) || (flush == Z_FINISH && err == Z_OK));
return MZ_OK;
}
#endif
int32_t mz_stream_zlib_write(void *stream, const void *buf, int32_t size)
{
@@ -279,6 +278,7 @@ int32_t mz_stream_zlib_write(void *stream, const void *buf, int32_t size)
#ifdef MZ_ZIP_NO_COMPRESSION
MZ_UNUSED(zlib);
MZ_UNUSED(buf);
err = MZ_SUPPORT_ERROR;
#else
zlib->zstream.next_in = (Bytef*)(intptr_t)buf;
@@ -295,7 +295,7 @@ int64_t mz_stream_zlib_tell(void *stream)
{
MZ_UNUSED(stream);
return MZ_STREAM_ERROR;
return MZ_TELL_ERROR;
}
int32_t mz_stream_zlib_seek(void *stream, int64_t offset, int32_t origin)
@@ -304,7 +304,7 @@ int32_t mz_stream_zlib_seek(void *stream, int64_t offset, int32_t origin)
MZ_UNUSED(offset);
MZ_UNUSED(origin);
return MZ_STREAM_ERROR;
return MZ_SEEK_ERROR;
}
int32_t mz_stream_zlib_close(void *stream)
@@ -320,7 +320,7 @@ int32_t mz_stream_zlib_close(void *stream)
mz_stream_zlib_deflate(stream, Z_FINISH);
mz_stream_zlib_flush(stream);
deflateEnd(&zlib->zstream);
ZLIB_PREFIX(deflateEnd)(&zlib->zstream);
#endif
}
else if (zlib->mode & MZ_OPEN_MODE_READ)
@@ -328,14 +328,14 @@ int32_t mz_stream_zlib_close(void *stream)
#ifdef MZ_ZIP_NO_DECOMPRESSION
return MZ_SUPPORT_ERROR;
#else
inflateEnd(&zlib->zstream);
ZLIB_PREFIX(inflateEnd)(&zlib->zstream);
#endif
}
zlib->initialized = 0;
if (zlib->error != Z_OK)
return MZ_STREAM_ERROR;
return MZ_CLOSE_ERROR;
return MZ_OK;
}
@@ -417,13 +417,3 @@ void *mz_stream_zlib_get_interface(void)
{
return (void *)&mz_stream_zlib_vtbl;
}
static int64_t mz_stream_zlib_crc32(int64_t value, const void *buf, int32_t size)
{
return crc32((z_crc_t)value, buf, size);
}
void *mz_stream_zlib_get_crc32_update(void)
{
return (void *)mz_stream_zlib_crc32;
}
+2 -5
View File
@@ -1,8 +1,8 @@
/* mz_strm_zlib.h -- Stream for zlib inflate/deflate
Version 2.5.2, August 27, 2018
Version 2.8.3, January 26, 2019
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
Copyright (C) 2010-2019 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
This program is distributed under the terms of the same license as zlib.
@@ -12,8 +12,6 @@
#ifndef MZ_STREAM_ZLIB_H
#define MZ_STREAM_ZLIB_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
@@ -36,7 +34,6 @@ 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_update(void);
/***************************************************************************/
+1504 -945
View File
File diff suppressed because it is too large Load Diff
+121 -85
View File
@@ -1,14 +1,14 @@
/* mz_zip.h -- Zip manipulation
Version 2.5.2, August 27, 2018
Version 2.8.3, January 26, 2019
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
Copyright (C) 2010-2019 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
https://www.winimage.com/zLibDll/minizip.html
This program is distributed under the terms of the same license as zlib.
See the accompanying LICENSE file for the full text of the license.
@@ -17,11 +17,6 @@
#ifndef MZ_ZIP_H
#define MZ_ZIP_H
#include <stdint.h>
#include <time.h>
#include "mz_strm.h"
#ifdef __cplusplus
extern "C" {
#endif
@@ -30,33 +25,32 @@ extern "C" {
typedef struct mz_zip_file_s
{
uint16_t version_madeby; // version made by
uint16_t version_needed; // version needed to extract
uint16_t flag; // general purpose bit flag
uint16_t compression_method; // compression method
time_t modified_date; // last modified date in unix time
time_t accessed_date; // last accessed date in unix time
time_t creation_date; // creation date in unix time
uint32_t crc; // crc-32
uint64_t compressed_size; // compressed size
uint64_t uncompressed_size; // uncompressed size
uint16_t filename_size; // filename length
uint16_t extrafield_size; // extra field length
uint16_t comment_size; // file comment length
uint32_t disk_number; // disk number start
uint64_t disk_offset; // relative offset of local header
uint16_t internal_fa; // internal file attributes
uint32_t external_fa; // external file attributes
uint16_t zip64; // zip64 extension mode
uint16_t version_madeby; /* version made by */
uint16_t version_needed; /* version needed to extract */
uint16_t flag; /* general purpose bit flag */
uint16_t compression_method; /* compression method */
time_t modified_date; /* last modified date in unix time */
time_t accessed_date; /* last accessed date in unix time */
time_t creation_date; /* creation date in unix time */
uint32_t crc; /* crc-32 */
int64_t compressed_size; /* compressed size */
int64_t uncompressed_size; /* uncompressed size */
uint16_t filename_size; /* filename length */
uint16_t extrafield_size; /* extra field length */
uint16_t comment_size; /* file comment length */
uint32_t disk_number; /* disk number start */
int64_t disk_offset; /* relative offset of local header */
uint16_t internal_fa; /* internal file attributes */
uint32_t external_fa; /* external file attributes */
const char *filename; // filename utf8 null-terminated string
const uint8_t *extrafield; // extrafield data
const char *comment; // comment utf8 null-terminated string
const char *filename; /* filename utf8 null-terminated string */
const uint8_t *extrafield; /* extrafield data */
const char *comment; /* comment utf8 null-terminated string */
uint16_t zip64; /* zip64 extension mode */
uint16_t aes_version; /* winzip aes extension if not 0 */
uint8_t aes_encryption_mode; /* winzip aes encryption mode */
#ifdef HAVE_AES
uint16_t aes_version; // winzip aes extension if not 0
uint8_t aes_encryption_mode; // winzip aes encryption mode
#endif
} mz_zip_file, mz_zip_entry;
/***************************************************************************/
@@ -66,135 +60,177 @@ typedef int32_t (*mz_zip_locate_entry_cb)(void *handle, void *userdata, mz_zip_f
/***************************************************************************/
void * mz_zip_create(void **handle);
// Create zip instance for opening
/* Create zip instance for opening */
void mz_zip_delete(void **handle);
// Delete zip object
/* Delete zip object */
int32_t mz_zip_open(void *handle, void *stream, int32_t mode);
// Create a zip file, no delete file in zip functionality
/* Create a zip file, no delete file in zip functionality */
int32_t mz_zip_close(void *handle);
// Close the zip file
/* Close the zip file */
int32_t mz_zip_get_comment(void *handle, const char **comment);
// Get a pointer to the global comment
/* Get a pointer to the global comment */
int32_t mz_zip_set_comment(void *handle, const char *comment);
// Set the global comment used for writing zip file
/* Set the global comment used for writing zip file */
int32_t mz_zip_get_version_madeby(void *handle, uint16_t *version_madeby);
// Get the version made by
/* Get the version made by */
int32_t mz_zip_set_version_madeby(void *handle, uint16_t version_madeby);
// Set the version made by used for writing zip file
/* Set the version made by used for writing zip file */
int32_t mz_zip_set_recover(void *handle, uint8_t recover);
/* Set the ability to recover the central dir by reading local file headers */
int32_t mz_zip_get_stream(void *handle, void **stream);
// Get a pointer to the stream used to open
/* Get a pointer to the stream used to open */
int32_t mz_zip_set_cd_stream(void *handle, int64_t cd_start_pos, void *cd_stream);
/* Sets the stream to use for reading the central dir */
int32_t mz_zip_get_cd_mem_stream(void *handle, void **cd_mem_stream);
/* Get a pointer to the stream used to store the central dir in memory */
/***************************************************************************/
int32_t mz_zip_entry_is_open(void *handle);
/* Check to see if entry is open for read/write */
int32_t mz_zip_entry_read_open(void *handle, uint8_t raw, const char *password);
/* Open for reading the current file in the zip file */
int32_t mz_zip_entry_read(void *handle, void *buf, int32_t len);
/* Read bytes from the current file in the zip file */
int32_t mz_zip_entry_read_close(void *handle, uint32_t *crc32, int64_t *compressed_size,
int64_t *uncompressed_size);
/* Close the current file for reading and get data descriptor values */
int32_t mz_zip_entry_write_open(void *handle, const mz_zip_file *file_info,
int16_t compress_level, uint8_t raw, const char *password);
// Open for writing the current file in the zip file
/* Open for writing the current file in the zip file */
int32_t mz_zip_entry_write(void *handle, const void *buf, int32_t len);
// Write bytes from the current file in the zip file
/* Write bytes from the current file in the zip file */
int32_t mz_zip_entry_is_open(void *handle);
// Check to see if entry is open for read/write
int32_t mz_zip_entry_write_close(void *handle, uint32_t crc32, int64_t compressed_size,
int64_t uncompressed_size);
/* Close the current file for writing and set data descriptor values */
int32_t mz_zip_entry_is_dir(void *handle);
// Checks to see if the entry is a directory
int32_t mz_zip_entry_read_open(void *handle, uint8_t raw, const char *password);
// Open for reading the current file in the zip file
int32_t mz_zip_entry_read(void *handle, void *buf, int32_t len);
// Read bytes from the current file in the zip file
/* Checks to see if the entry is a directory */
int32_t mz_zip_entry_get_info(void *handle, mz_zip_file **file_info);
// Get info about the current file, only valid while current entry is open
/* Get info about the current file, only valid while current entry is open */
int32_t mz_zip_entry_get_local_info(void *handle, mz_zip_file **local_file_info);
// Get local info about the current file, only valid while current entry is being read
/* Get local info about the current file, only valid while current entry is being read */
int32_t mz_zip_entry_close_raw(void *handle, uint64_t uncompressed_size, uint32_t crc32);
// Close the current file in the zip file where raw is compressed data
int32_t mz_zip_entry_set_extrafield(void *handle, const uint8_t *extrafield, uint16_t extrafield_size);
/* Sets or updates the extra field for the entry to be used before writing cd */
int32_t mz_zip_entry_close_raw(void *handle, int64_t uncompressed_size, uint32_t crc32);
/* Close the current file in the zip file where raw is compressed data */
int32_t mz_zip_entry_close(void *handle);
// Close the current file in the zip file
/* Close the current file in the zip file */
/***************************************************************************/
int32_t mz_zip_get_number_entry(void *handle, int64_t *number_entry);
// Get the total number of entries
int32_t mz_zip_set_number_entry(void *handle, uint64_t number_entry);
/* Sets the total number of entries */
int32_t mz_zip_get_number_entry(void *handle, uint64_t *number_entry);
/* Get the total number of entries */
int32_t mz_zip_set_disk_number_with_cd(void *handle, uint32_t disk_number_with_cd);
/* Sets the disk number containing the central directory record */
int32_t mz_zip_get_disk_number_with_cd(void *handle, uint32_t *disk_number_with_cd);
// Get the the disk number containing the central directory record
/* Get the disk number containing the central directory record */
int64_t mz_zip_get_entry(void *handle);
// Return offset of the current entry in the zip file
/* Return offset of the current entry in the zip file */
int32_t mz_zip_goto_entry(void *handle, uint64_t cd_pos);
// Go to specified entry in the zip file
int32_t mz_zip_goto_entry(void *handle, int64_t cd_pos);
/* Go to specified entry in the zip file */
int32_t mz_zip_goto_first_entry(void *handle);
// Go to the first entry in the zip file
/* Go to the first entry in the zip file */
int32_t mz_zip_goto_next_entry(void *handle);
// Go to the next entry in the zip file or MZ_END_OF_LIST if reaching the end
/* Go to the next entry in the zip file or MZ_END_OF_LIST if reaching the end */
int32_t mz_zip_locate_entry(void *handle, const char *filename, uint8_t ignore_case);
// Locate the file with the specified name in the zip file or MZ_END_LIST if not found
/* Locate the file with the specified name in the zip file or MZ_END_LIST if not found */
int32_t mz_zip_locate_first_entry(void *handle, void *userdata, mz_zip_locate_entry_cb cb);
// Locate the first matching entry based on a match callback
/* Locate the first matching entry based on a match callback */
int32_t mz_zip_locate_next_entry(void *handle, void *userdata, mz_zip_locate_entry_cb cb);
// LOcate the next matching entry based on a match callback
/* LOcate the next matching entry based on a match callback */
/***************************************************************************/
int32_t mz_zip_attrib_is_dir(int32_t attrib, int32_t version_madeby);
// Checks to see if the attribute is a directory based on platform
int32_t mz_zip_attrib_is_dir(uint32_t attrib, int32_t version_madeby);
/* Checks to see if the attribute is a directory based on platform */
int32_t mz_zip_attrib_convert(uint8_t src_sys, int32_t src_attrib, uint8_t target_sys, int32_t *target_attrib);
// Converts file attributes from one host system to another
int32_t mz_zip_attrib_convert(uint8_t src_sys, uint32_t src_attrib, uint8_t target_sys,
uint32_t *target_attrib);
/* Converts file attributes from one host system to another */
int32_t mz_zip_attrib_posix_to_win32(int32_t posix_attrib, int32_t *win32_attrib);
// Converts posix file attributes to win32 file attributes
int32_t mz_zip_attrib_posix_to_win32(uint32_t posix_attrib, uint32_t *win32_attrib);
/* Converts posix file attributes to win32 file attributes */
int32_t mz_zip_attrib_win32_to_posix(int32_t win32_attrib, int32_t *posix_attrib);
// Converts win32 file attributes to posix file attributes
int32_t mz_zip_attrib_win32_to_posix(uint32_t win32_attrib, uint32_t *posix_attrib);
/* Converts win32 file attributes to posix file attributes */
/***************************************************************************/
int32_t mz_zip_extrafield_find(void *stream, uint16_t type, uint16_t *length);
/* Seeks to extra field by its type and returns its length */
int32_t mz_zip_extrafield_contains(const uint8_t *extrafield, int32_t extrafield_size,
uint16_t type, uint16_t *length);
/* Gets whether an extrafield exists and its size */
int32_t mz_zip_extrafield_read(void *stream, uint16_t *type, uint16_t *length);
/* Reads an extrafield header from a stream */
int32_t mz_zip_extrafield_write(void *stream, uint16_t type, uint16_t length);
/* Writes an extrafield header to a stream */
/***************************************************************************/
int32_t mz_zip_dosdate_to_tm(uint64_t dos_date, struct tm *ptm);
// Convert dos date/time format to struct tm
/* Convert dos date/time format to struct tm */
time_t mz_zip_dosdate_to_time_t(uint64_t dos_date);
// Convert dos date/time format to time_t
/* Convert dos date/time format to time_t */
int32_t mz_zip_time_t_to_tm(time_t unix_time, struct tm *ptm);
// Convert time_t to time struct
/* Convert time_t to time struct */
uint32_t mz_zip_time_t_to_dos_date(time_t unix_time);
// Convert time_t to dos date/time format
/* Convert time_t to dos date/time format */
uint32_t mz_zip_tm_to_dosdate(const struct tm *ptm);
// Convert struct tm to dos date/time format
/* Convert struct tm to dos date/time format */
int32_t mz_zip_ntfs_to_unix_time(uint64_t ntfs_time, time_t *unix_time);
// Convert ntfs time to unix time
/* Convert ntfs time to unix time */
int32_t mz_zip_unix_to_ntfs_time(time_t unix_time, uint64_t *ntfs_time);
// Convert unix time to ntfs time
/* Convert unix time to ntfs time */
/***************************************************************************/
int32_t mz_zip_path_compare(const char *path1, const char *path2, uint8_t ignore_case);
// Compare two paths without regard to slashes
/* Compare two paths without regard to slashes */
/***************************************************************************/
+782 -123
View File
File diff suppressed because it is too large Load Diff
+119 -72
View File
@@ -1,8 +1,8 @@
/* mz_zip_rw.h -- Zip reader/writer
Version 2.5.2, August 27, 2018
Version 2.8.3, January 26, 2019
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
Copyright (C) 2010-2019 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
This program is distributed under the terms of the same license as zlib.
@@ -12,223 +12,270 @@
#ifndef MZ_ZIP_RW_H
#define MZ_ZIP_RW_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
#include "mz_zip.h"
/***************************************************************************/
typedef int32_t (*mz_zip_reader_overwrite_cb)(void *handle, void *userdata, mz_zip_file *file_info, const char *path);
typedef int32_t (*mz_zip_reader_password_cb)(void *handle, void *userdata, mz_zip_file *file_info, char *password, int32_t max_password);
typedef int32_t (*mz_zip_reader_progress_cb)(void *handle, void *userdata, mz_zip_file *file_info, uint64_t position);
typedef int32_t (*mz_zip_reader_progress_cb)(void *handle, void *userdata, mz_zip_file *file_info, int64_t position);
typedef int32_t (*mz_zip_reader_entry_cb)(void *handle, void *userdata, mz_zip_file *file_info, const char *path);
/***************************************************************************/
int32_t mz_zip_reader_is_open(void *handle);
// Checks to see if the zip file is open
/* Checks to see if the zip file is open */
int32_t mz_zip_reader_open(void *handle, void *stream);
// Opens zip file from stream
/* Opens zip file from stream */
int32_t mz_zip_reader_open_file(void *handle, const char *path);
// Opens zip file from a file path
/* Opens zip file from a file path */
int32_t mz_zip_reader_open_file_in_memory(void *handle, const char *path);
// Opens zip file from a file path into memory for faster access
/* Opens zip file from a file path into memory for faster access */
int32_t mz_zip_reader_open_buffer(void *handle, uint8_t *buf, int32_t len, uint8_t copy);
// Opens zip file from memory buffer
/* Opens zip file from memory buffer */
int32_t mz_zip_reader_close(void *handle);
// Closes the zip file
/* Closes the zip file */
/***************************************************************************/
int32_t mz_zip_reader_unzip_cd(void *handle);
/* Unzip the central directory */
/***************************************************************************/
int32_t mz_zip_reader_goto_first_entry(void *handle);
// Goto the first entry in the zip file that matches the pattern
/* Goto the first entry in the zip file that matches the pattern */
int32_t mz_zip_reader_goto_next_entry(void *handle);
// Goto the next entry in the zip file that matches the pattern
/* Goto the next entry in the zip file that matches the pattern */
int32_t mz_zip_reader_locate_entry(void *handle, const char *filename, uint8_t ignore_case);
// Locates an entry by filename
/* Locates an entry by filename */
int32_t mz_zip_reader_entry_open(void *handle);
// Opens an entry for reading
/* Opens an entry for reading */
int32_t mz_zip_reader_entry_close(void *handle);
// Closes an entry
/* Closes an entry */
int32_t mz_zip_reader_entry_read(void *handle, const void *buf, int32_t len);
// Reads and entry after being opened
int32_t mz_zip_reader_entry_read(void *handle, void *buf, int32_t len);
/* Reads and entry after being opened */
int32_t mz_zip_reader_entry_has_sign(void *handle);
/* Checks to see if the entry has a signature */
int32_t mz_zip_reader_entry_sign_verify(void *handle);
/* Verifies a signature stored with the entry */
int32_t mz_zip_reader_entry_get_hash(void *handle, uint16_t algorithm, uint8_t *digest, int32_t digest_size);
/* Gets a hash algorithm from the entry's extra field */
int32_t mz_zip_reader_entry_get_first_hash(void *handle, uint16_t *algorithm, uint16_t *digest_size);
/* Gets the most secure hash algorithm from the entry's extra field */
int32_t mz_zip_reader_entry_get_info(void *handle, mz_zip_file **file_info);
// Gets the current entry file info
/* Gets the current entry file info */
int32_t mz_zip_reader_entry_is_dir(void *handle);
// Gets the current entry is a directory
/* Gets the current entry is a directory */
int32_t mz_zip_reader_entry_save(void *handle, void *stream, mz_stream_write_cb write_cb);
// Save the current entry to a steam
/* Save the current entry to a steam */
int32_t mz_zip_reader_entry_save_process(void *handle, void *stream, mz_stream_write_cb write_cb);
// Saves a portion of the current entry to a stream callback
/* Saves a portion of the current entry to a stream callback */
int32_t mz_zip_reader_entry_save_file(void *handle, const char *path);
// Save the current entry to a file
/* Save the current entry to a file */
int32_t mz_zip_reader_entry_save_buffer(void *handle, void *buf, int32_t len);
// Save the current entry to a memory buffer
/* Save the current entry to a memory buffer */
int32_t mz_zip_reader_entry_save_buffer_length(void *handle);
// Gets the length of the buffer required to save
/* Gets the length of the buffer required to save */
/***************************************************************************/
int32_t mz_zip_reader_save_all(void *handle, const char *destination_dir);
// Save all files into a directory
/* Save all files into a directory */
/***************************************************************************/
void mz_zip_reader_set_pattern(void *handle, const char *pattern, uint8_t ignore_case);
// Sets the match pattern for entries in the zip file, if null all entries are matched
/* Sets the match pattern for entries in the zip file, if null all entries are matched */
void mz_zip_reader_set_password(void *handle, const char *password);
// Sets the password required for extraction
/* Sets the password required for extraction */
void mz_zip_reader_set_raw(void *handle, uint8_t raw);
// Sets whether or not it should save the entry raw
/* Sets whether or not it should save the entry raw */
int32_t mz_zip_reader_get_raw(void *handle, uint8_t *raw);
// Gets whether or not it should save the entry raw
/* Gets whether or not it should save the entry raw */
void mz_zip_reader_set_legacy_encoding(void *handle, uint8_t legacy_encoding);
// Sets whether or not it should support cp437 in zip file names
int32_t mz_zip_reader_get_zip_cd(void *handle, uint8_t *zip_cd);
/* Gets whether or not the archive has zipped cd */
int32_t mz_zip_reader_get_comment(void *handle, const char **comment);
/* Gets the comment for the central directory */
void mz_zip_reader_set_encoding(void *handle, int32_t encoding);
/* Sets whether or not it should support cp437 in zip file names */
void mz_zip_reader_set_sign_required(void *handle, uint8_t sign_required);
/* Sets whether or not it a signature is required */
void mz_zip_reader_set_overwrite_cb(void *handle, void *userdata, mz_zip_reader_overwrite_cb cb);
// Callback for what to do when a file is being overwritten
/* Callback for what to do when a file is being overwritten */
void mz_zip_reader_set_password_cb(void *handle, void *userdata, mz_zip_reader_password_cb cb);
// Callback for when a password is required and hasn't been set
/* Callback for when a password is required and hasn't been set */
void mz_zip_reader_set_progress_cb(void *handle, void *userdata, mz_zip_reader_progress_cb cb);
// Callback for extraction progress
/* Callback for extraction progress */
void mz_zip_reader_set_progress_interval(void *handle, uint32_t milliseconds);
/* Let at least milliseconds pass between calls to progress callback */
void mz_zip_reader_set_entry_cb(void *handle, void *userdata, mz_zip_reader_entry_cb cb);
// Callback for zip file entries
/* Callback for zip file entries */
int32_t mz_zip_reader_get_zip_handle(void *handle, void **zip_handle);
// Gets the underlying zip instance handle
/* Gets the underlying zip instance handle */
void* mz_zip_reader_create(void **handle);
// Create new instance of zip reader
/* Create new instance of zip reader */
void mz_zip_reader_delete(void **handle);
// Delete instance of zip reader
/* Delete instance of zip reader */
/***************************************************************************/
typedef int32_t (*mz_zip_writer_overwrite_cb)(void *handle, void *userdata, const char *path);
typedef int32_t (*mz_zip_writer_password_cb)(void *handle, void *userdata, mz_zip_file *file_info, char *password, int32_t max_password);
typedef int32_t (*mz_zip_writer_progress_cb)(void *handle, void *userdata, mz_zip_file *file_info, uint64_t position);
typedef int32_t (*mz_zip_writer_progress_cb)(void *handle, void *userdata, mz_zip_file *file_info, int64_t position);
typedef int32_t (*mz_zip_writer_entry_cb)(void *handle, void *userdata, mz_zip_file *file_info);
/***************************************************************************/
int32_t mz_zip_writer_is_open(void *handle);
// Checks to see if the zip file is open
/* Checks to see if the zip file is open */
int32_t mz_zip_writer_open(void *handle, void *stream);
// Opens zip file from stream
/* Opens zip file from stream */
int32_t mz_zip_writer_open_file(void *handle, const char *path, int64_t disk_size, uint8_t append);
// Opens zip file from a file path
/* Opens zip file from a file path */
int32_t mz_zip_writer_open_file_in_memory(void *handle, const char *path);
// Opens zip file from a file path into memory for faster access
/* Opens zip file from a file path into memory for faster access */
int32_t mz_zip_writer_close(void *handle);
// Closes the zip file
/* Closes the zip file */
/***************************************************************************/
int32_t mz_zip_writer_zip_cd(void *handle);
/* Zip the central directory */
/***************************************************************************/
int32_t mz_zip_writer_entry_open(void *handle, mz_zip_file *file_info);
// Opens an entry in the zip file for writing
/* Opens an entry in the zip file for writing */
int32_t mz_zip_writer_entry_close(void *handle);
// Closes entry in zip file
/* Closes entry in zip file */
int32_t mz_zip_writer_entry_write(void *handle, const void *buf, int32_t len);
// Writes data into entry for zip
/* Writes data into entry for zip */
int32_t mz_zip_writer_entry_sign(void *handle, uint8_t *message, int32_t message_size,
uint8_t *cert_data, int32_t cert_data_size, const char *cert_pwd);
/* Signs uncompressed content of entry, call before closing */
/***************************************************************************/
int32_t mz_zip_writer_add(void *handle, void *stream, mz_stream_read_cb read_cb);
// Writes all data to the currently open entry in the zip
/* Writes all data to the currently open entry in the zip */
int32_t mz_zip_writer_add_process(void *handle, void *stream, mz_stream_read_cb read_cb);
// Writes a portion of data to the currently open entry in the zip
/* Writes a portion of data to the currently open entry in the zip */
int32_t mz_zip_writer_add_info(void *handle, void *stream, mz_stream_read_cb read_cb, mz_zip_file *file_info);
// Adds an entry to the zip based on the info
/* Adds an entry to the zip based on the info */
int32_t mz_zip_writer_add_buffer(void *handle, void *buf, int32_t len, mz_zip_file *file_info);
// Adds an entry to the zip with a memory buffer
/* Adds an entry to the zip with a memory buffer */
int32_t mz_zip_writer_add_file(void *handle, const char *path, const char *filename_in_zip);
// Adds an entry to the zip from a file
/* Adds an entry to the zip from a file */
int32_t mz_zip_writer_add_path(void *handle, const char *path, const char *root_path, uint8_t include_path, uint8_t recursive);
// Enumerates a directory or pattern and adds entries to the zip
int32_t mz_zip_writer_add_path(void *handle, const char *path, const char *root_path, uint8_t include_path,
uint8_t recursive);
/* Enumerates a directory or pattern and adds entries to the zip */
int32_t mz_zip_writer_copy_from_reader(void *handle, void *reader);
// Adds an entry from a zip reader instance
/* Adds an entry from a zip reader instance */
/***************************************************************************/
void mz_zip_writer_set_password(void *handle, const char *password);
// Password to use for encrypting files in the zip
/* Password to use for encrypting files in the zip */
void mz_zip_writer_set_comment(void *handle, const char *comment);
/* Comment to use for the archive */
void mz_zip_writer_set_raw(void *handle, uint8_t raw);
// Sets whether or not we should write the entry raw
/* Sets whether or not we should write the entry raw */
int32_t mz_zip_writer_get_raw(void *handle, uint8_t *raw);
// Gets whether or not we should write the entry raw
/* Gets whether or not we should write the entry raw */
void mz_zip_writer_set_aes(void *handle, uint8_t aes);
// Use aes encryption when adding files in zip
/* Use aes encryption when adding files in zip */
void mz_zip_writer_set_compress_method(void *handle, int16_t compress_method);
// Sets the compression method when adding files in zip
void mz_zip_writer_set_compress_method(void *handle, uint16_t compress_method);
/* Sets the compression method when adding files in zip */
void mz_zip_writer_set_compress_level(void *handle, int16_t compress_level);
// Sets the compression level when adding files in zip
/* Sets the compression level when adding files in zip */
void mz_zip_writer_set_zip_cd(void *handle, uint8_t zip_cd);
/* Sets additional flags to be set when adding files in zip */
int32_t mz_zip_writer_set_certificate(void *handle, const char *cert_path, const char *cert_pwd);
/* Sets the certificate and timestamp url to use for signing when adding files in zip */
void mz_zip_writer_set_overwrite_cb(void *handle, void *userdata, mz_zip_writer_overwrite_cb cb);
// Callback for what to do when zip file already exists
/* Callback for what to do when zip file already exists */
void mz_zip_writer_set_password_cb(void *handle, void *userdata, mz_zip_writer_password_cb cb);
// Callback for ask if a password is required for adding
/* Callback for ask if a password is required for adding */
void mz_zip_writer_set_progress_cb(void *handle, void *userdata, mz_zip_writer_progress_cb cb);
// Callback for compression progress
/* Callback for compression progress */
void mz_zip_writer_set_progress_interval(void *handle, uint32_t milliseconds);
/* Let at least milliseconds pass between calls to progress callback */
void mz_zip_writer_set_entry_cb(void *handle, void *userdata, mz_zip_writer_entry_cb cb);
// Callback for zip file entries
/* Callback for zip file entries */
int32_t mz_zip_writer_get_zip_handle(void *handle, void **zip_handle);
// Gets the underlying zip handle
/* Gets the underlying zip handle */
void* mz_zip_writer_create(void **handle);
// Create new instance of zip writer
/* Create new instance of zip writer */
void mz_zip_writer_delete(void **handle);
// Delete instance of zip writer
/* Delete instance of zip writer */
/***************************************************************************/
+34
View File
@@ -0,0 +1,34 @@
-----BEGIN CERTIFICATE-----
MIIF7TCCA9WgAwIBAgIJALkeEbyIQTYjMA0GCSqGSIb3DQEBBQUAMIGMMQswCQYD
VQQGEwJVUzELMAkGA1UECAwCQVoxEDAOBgNVBAcMB1Bob2VuaXgxGjAYBgNVBAoM
EUJpbGxpb24gRG9sbGFyIENvMRQwEgYDVQQLDAtFbmdpbmVlcmluZzERMA8GA1UE
AwwIYi5kb2xsYXIxGTAXBgkqhkiG9w0BCQEWCmJAYi5kb2xsYXIwHhcNMTgxMDI4
MjIyMTE0WhcNNDYwMzE0MjIyMTE0WjCBjDELMAkGA1UEBhMCVVMxCzAJBgNVBAgM
AkFaMRAwDgYDVQQHDAdQaG9lbml4MRowGAYDVQQKDBFCaWxsaW9uIERvbGxhciBD
bzEUMBIGA1UECwwLRW5naW5lZXJpbmcxETAPBgNVBAMMCGIuZG9sbGFyMRkwFwYJ
KoZIhvcNAQkBFgpiQGIuZG9sbGFyMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIIC
CgKCAgEA4HFpJ2Szh4lqnCXL1T6q3tppX2vKP7lH02Eapab8Um3puN9ftfvG3si0
g+Hs8MK6kMlmG2+cBSCXZX5f6anZOfPUagXdxokhJVhXLrGSo7/N2nBkQGa11pz1
i3tdFnsjZPRsyEU6rKJgVIrbw0wD/reFLc6RdfZKF0Xw+3MpJP8jbNXOJPkkjaKT
0PSoKgl/Dav7RckOzjGKfdzZrY2NeeSSd3yni5NPHgQRNhRxD7kXCexplkS9vh4e
B/eKraOecwewaP+zhC39UlUIwjNezzg+iNeoG7DuuqaRbFAjDj+DVZSgqQdTvTSw
FQ0Xly3ZL9oWAIpLsmKDElEZC3HvCzmxkzmhX3Pe/3FjsghAoR/KWPp8gQFq5Ao3
5ySWHhArjUZf2nOUgrufCgbd+LIi4DtQ3k/K6zuF7oyqVWjKESlX5KkwAW6xvB/x
IukMqyQBts9HRTRUp4LsWz8XxMC6rLcNXa41uhhtgJKFsRjehSfVZPNI3tm1nAna
2SS3TnoVORXDTCxbAAPHdK2jxuQVD9RidCfphuT5cFxfUXChF2Mg+c0p7ujtiVr4
D5CpeqcCqlZMf4mODVqRlKW1rMsbCBrn8NU39Ad7z0fpzqIoKjS8okLii/7oYepV
1ZkWMue1aFKEEReBXk1jd11Iq/B+d1eLnPPPvyTa6DmgkAti9lECAwEAAaNQME4w
HQYDVR0OBBYEFM5shIinr8GUAanRZIRCNT2SKge8MB8GA1UdIwQYMBaAFM5shIin
r8GUAanRZIRCNT2SKge8MAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEFBQADggIB
ALr+1w/2CiHvstlJXyM67bZT0/4eaKaL9Vc3qebROGxvm1TOilQEOnlURlXtfgzq
x5ppnzuWlIr1GtbecJo/zr5ZItTrqCwCSbKdMKcexc+pSeNajxXJ6f/BsDrNfFKI
zPdqBT12Vy++1XI3goa/7KMX/fgqcxeB6uZkMywwpu6Woewd2r+OPSk0MEWyT1zd
Nh0VaHk2jR003n14IAj98NHrT0M9iEzhDkk3nWJfOvvaRVGBoTk+luMZ7FQqcXkD
L1d/DivaEyTFShTHpYfyB7w5ydnL/K4XattMHKOgWXvG7ySpTKPnFCJHPfJThIOp
lJH3c/fJghdFzZj7JPAzHUDotTopGd5JkwRvyGws8WaaLbpCljUms+cnWUCuEPS/
jA0fYvCKtXXeotvPE7eroD5qv74cUHwBA/wsFwC1Nr9Fn7y8xV1hwX/0T4Np0C/+
lM9oPpm+FQAJ0YAyCqC5MXepA4lj2r/jyDivpTa4iRtsd1QdtmNGb445IhFiEXOZ
EElm+OJVDI8zH//4rJwt3e+/4/l9Y0dHhjMSIfMk9Cq6X+xedDPHgVsgydVlEOZw
+aXjXj8nygHXPeOiVvTp2PyNl6l1PV6tWCWzXVgcxhPQ/z3zzaFiBuMEUOIjXH2Q
UzGOvx9YcDa1++j7UwImQYxKiDFw1pc/fBZCmJZ2kilg
-----END CERTIFICATE-----
+51
View File
@@ -0,0 +1,51 @@
-----BEGIN RSA PRIVATE KEY-----
MIIJKAIBAAKCAgEA4HFpJ2Szh4lqnCXL1T6q3tppX2vKP7lH02Eapab8Um3puN9f
tfvG3si0g+Hs8MK6kMlmG2+cBSCXZX5f6anZOfPUagXdxokhJVhXLrGSo7/N2nBk
QGa11pz1i3tdFnsjZPRsyEU6rKJgVIrbw0wD/reFLc6RdfZKF0Xw+3MpJP8jbNXO
JPkkjaKT0PSoKgl/Dav7RckOzjGKfdzZrY2NeeSSd3yni5NPHgQRNhRxD7kXCexp
lkS9vh4eB/eKraOecwewaP+zhC39UlUIwjNezzg+iNeoG7DuuqaRbFAjDj+DVZSg
qQdTvTSwFQ0Xly3ZL9oWAIpLsmKDElEZC3HvCzmxkzmhX3Pe/3FjsghAoR/KWPp8
gQFq5Ao35ySWHhArjUZf2nOUgrufCgbd+LIi4DtQ3k/K6zuF7oyqVWjKESlX5Kkw
AW6xvB/xIukMqyQBts9HRTRUp4LsWz8XxMC6rLcNXa41uhhtgJKFsRjehSfVZPNI
3tm1nAna2SS3TnoVORXDTCxbAAPHdK2jxuQVD9RidCfphuT5cFxfUXChF2Mg+c0p
7ujtiVr4D5CpeqcCqlZMf4mODVqRlKW1rMsbCBrn8NU39Ad7z0fpzqIoKjS8okLi
i/7oYepV1ZkWMue1aFKEEReBXk1jd11Iq/B+d1eLnPPPvyTa6DmgkAti9lECAwEA
AQKCAgBVQP8OZaQEMeB7G6hHghxptlEcW9dhSUHNOy9iCQIDVZjYDpFOkSrDaT2Y
cvMLqQww/y938Hc1Q2H+vXg1S70l1Ea1VzEcvu4GP34uUGlBJhRNiy+pftpHgSuM
JfoAFholuOqxUAqvlKY+ttgvYOqLkK5X7gU256zRFlIpUV8pnHiaWMSLKtAFtOGt
F6HhBeZrDQ7PPUYQcKUIliFhWUmKKZaxKHqD/BqrbiQThMRFkqr7q/+XgtkIJGyH
3jGhL2t6iksqNya7aLS213ISQm0bDWRDmpdSnWyxHaBTokUOnnWGxoQYcJ4QWrkf
5e82D8ZV9OMypVWHlv/urztyK37+8Hqc02FnIvH1wdu/CGB2hP1qwKAoGbc/yLPb
pH0aX1vdARZW6RZTnneLLnxPEJsoRFwpcGpeleG3EHk4Mq56PKuca/Yt4cDRt5TC
mJwAc9jO3+5scffh29aoEWOMLjqPI8dQVaHQ5gACVe7OFBMUhi+FIO05M9vSxAZ0
VIE+S4ZFpOE4kVcIeL+Cfr0AtjI4Sut1EPdB4SYQ23Debuu1QYF684+uhECqCgrd
dSyNAWcHVJfTsgISwGueuKfFi+rELvUloUajTiyo2HPwt19i7rS7v+Vfi4arU6SH
x7bIjSNyyDA7N0fQo1naPPtbRC/nyZ7PGz5aNyy0I3OKNVCsmQKCAQEA8IL58w3m
WfV0NHq2iC5xhpQZ2frQhyI5GL7bmVvhlHLa+15OsN/X87QNoAijNK7XY18+SJVL
9OQKu7dveZSvwLXd4yCvYX1INgLtdOzvW3ss2cMJVTKVLhoruAeV+YFAqTOZZ9PX
EEovK0HIv4V9TA2B/5XnOdEuvtOypqJxt5vrvyJVqpIoq81wh/PhYZu/hK9wOzPO
v+XVG9MRaoWR2PvqNN1t5HSudTPSqzEkjx03DarOCmbMCKjhoZvLfKQRyWP4urp3
Rdhzr4GAEpF2mALVVmGiYTLs+ujduKSg6lDdMzO/dTMkOBMblcsbN8geGpLo4+7q
y3fp+H7sZEuawwKCAQEA7uWH1bNt3jHEyW4qnlShHVDScw3Tq2JsS8mAHX5oXnOw
k33aqYoQruqznTKcYDRUNOx8QX+3LCOD6M3JvN+lnktYU7h3gVHznIf867yWs8Kg
/JlPtwYPlATNcS8EdAW4/hdtD2zdQRmLjCaudr85FVkT1eoGPJqVkLJzcfODwemm
EvQptqq7lIbTur4nvoqGACXc3Pj8Zzil3rEg3eBo4IT7ThC23PNJ5Db3hdUc9UgF
erNrV/mmXI8Yx5udr0q4mV0XUfUJ3BE85csz2eWdZg/ER3X0u9Em668Tw2riGZHf
16WtzPjP50JGMhgv4BDdeDgv1b2FasbvsPVnGBQRWwKCAQA1J30+LlA+iXTJNGWq
6zjD2W9tMXVsqC+ePHAkEJ5mRIwJCeqyRIZhoeVw/Pe06P9JQv9s4zIKSWvl2MCW
SjodiRQk4lBgZ2PSLAt2z/aGbU6YQfaKEcfOjJIk9eg7YEl+G51HyRW6z+pk0QEg
8suiR0n3xpV3vbdrDTZqhXREc/1CHMB6FhtO/PRl14x0GgvVs0BGlUiYOJ9Eqv0q
YaK+Jc65KEblOrRjyrRAiXfe+UH1j05CFj35m/lh0iDCxy/3JlUOT6+UjCXlH+XC
NMvVBKtJkAJnIO4m9dgqupelsYFxk3o42Like/SYwm6dgWjFzroRxfe2SCY5eTOd
ZugpAoIBAQDKxNO4lSVp9QA8oF9YKCjgQxt9QH0q+M1Gvm8ZmZNop/V2GtqgEGn8
rYVqpeyI06GtkhQdXBPTSQnLbfAPehyO8RC1btswTqjhcaNBS772MPqHrHE2vdQ3
JKRjry6uc7UDJksRTqu1d8M0YBiw/bmr3wGxDYPhqKvON0k5rSFmU7T8XXWSb/Pr
K01HMeN2HQmD0p9Wh5dk3XYcQGfPK4yZFQZgylsLywwqwmTxmWOM5WaRMPypnWKR
KXw04GbExD1PHu6OR9fojFEOEp2eEwC4AiXtKbdiOzI31VCHC4+BeIlv9lZApeeB
OIVwO4tXOblhMv+XAdfeO6ByqLiqfU4FAoIBAHQFRCuOgEaNO5qQCV43kq7MguRc
/22yPusUkxCTPZr7q0Xt3szV84x9yuV7cztNWnIKuCvqi+9fzCOagIV7lKIsT+Fo
ELFbt3feYU224rhZhm6J3G2qQHKZH9WOclLUw5fV/XAtJw2IJRDd7NEMvuZPB7Vc
/FdESPPg1j949cfqqkWNxo+p4wX2awRmIX2M1+8+wRVJQY5jvIUbcN1mkz6NDodE
ZNHXOHD8vxO94uILkrFB6LE0d1ttXhZj+Q403gpl+9vlWuhksu7M3XaW6+/XkeMf
xzOarKBtvpBi2oB6kQ1jJRycFgUasZ0fjCz+OOlvK9hh5VvacB1MEBfKgBE=
-----END RSA PRIVATE KEY-----
+34
View File
@@ -0,0 +1,34 @@
-----BEGIN CERTIFICATE-----
MIIF7TCCA9WgAwIBAgIJALkeEbyIQTYjMA0GCSqGSIb3DQEBBQUAMIGMMQswCQYD
VQQGEwJVUzELMAkGA1UECAwCQVoxEDAOBgNVBAcMB1Bob2VuaXgxGjAYBgNVBAoM
EUJpbGxpb24gRG9sbGFyIENvMRQwEgYDVQQLDAtFbmdpbmVlcmluZzERMA8GA1UE
AwwIYi5kb2xsYXIxGTAXBgkqhkiG9w0BCQEWCmJAYi5kb2xsYXIwHhcNMTgxMDI4
MjIyMTE0WhcNNDYwMzE0MjIyMTE0WjCBjDELMAkGA1UEBhMCVVMxCzAJBgNVBAgM
AkFaMRAwDgYDVQQHDAdQaG9lbml4MRowGAYDVQQKDBFCaWxsaW9uIERvbGxhciBD
bzEUMBIGA1UECwwLRW5naW5lZXJpbmcxETAPBgNVBAMMCGIuZG9sbGFyMRkwFwYJ
KoZIhvcNAQkBFgpiQGIuZG9sbGFyMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIIC
CgKCAgEA4HFpJ2Szh4lqnCXL1T6q3tppX2vKP7lH02Eapab8Um3puN9ftfvG3si0
g+Hs8MK6kMlmG2+cBSCXZX5f6anZOfPUagXdxokhJVhXLrGSo7/N2nBkQGa11pz1
i3tdFnsjZPRsyEU6rKJgVIrbw0wD/reFLc6RdfZKF0Xw+3MpJP8jbNXOJPkkjaKT
0PSoKgl/Dav7RckOzjGKfdzZrY2NeeSSd3yni5NPHgQRNhRxD7kXCexplkS9vh4e
B/eKraOecwewaP+zhC39UlUIwjNezzg+iNeoG7DuuqaRbFAjDj+DVZSgqQdTvTSw
FQ0Xly3ZL9oWAIpLsmKDElEZC3HvCzmxkzmhX3Pe/3FjsghAoR/KWPp8gQFq5Ao3
5ySWHhArjUZf2nOUgrufCgbd+LIi4DtQ3k/K6zuF7oyqVWjKESlX5KkwAW6xvB/x
IukMqyQBts9HRTRUp4LsWz8XxMC6rLcNXa41uhhtgJKFsRjehSfVZPNI3tm1nAna
2SS3TnoVORXDTCxbAAPHdK2jxuQVD9RidCfphuT5cFxfUXChF2Mg+c0p7ujtiVr4
D5CpeqcCqlZMf4mODVqRlKW1rMsbCBrn8NU39Ad7z0fpzqIoKjS8okLii/7oYepV
1ZkWMue1aFKEEReBXk1jd11Iq/B+d1eLnPPPvyTa6DmgkAti9lECAwEAAaNQME4w
HQYDVR0OBBYEFM5shIinr8GUAanRZIRCNT2SKge8MB8GA1UdIwQYMBaAFM5shIin
r8GUAanRZIRCNT2SKge8MAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEFBQADggIB
ALr+1w/2CiHvstlJXyM67bZT0/4eaKaL9Vc3qebROGxvm1TOilQEOnlURlXtfgzq
x5ppnzuWlIr1GtbecJo/zr5ZItTrqCwCSbKdMKcexc+pSeNajxXJ6f/BsDrNfFKI
zPdqBT12Vy++1XI3goa/7KMX/fgqcxeB6uZkMywwpu6Woewd2r+OPSk0MEWyT1zd
Nh0VaHk2jR003n14IAj98NHrT0M9iEzhDkk3nWJfOvvaRVGBoTk+luMZ7FQqcXkD
L1d/DivaEyTFShTHpYfyB7w5ydnL/K4XattMHKOgWXvG7ySpTKPnFCJHPfJThIOp
lJH3c/fJghdFzZj7JPAzHUDotTopGd5JkwRvyGws8WaaLbpCljUms+cnWUCuEPS/
jA0fYvCKtXXeotvPE7eroD5qv74cUHwBA/wsFwC1Nr9Fn7y8xV1hwX/0T4Np0C/+
lM9oPpm+FQAJ0YAyCqC5MXepA4lj2r/jyDivpTa4iRtsd1QdtmNGb445IhFiEXOZ
EElm+OJVDI8zH//4rJwt3e+/4/l9Y0dHhjMSIfMk9Cq6X+xedDPHgVsgydVlEOZw
+aXjXj8nygHXPeOiVvTp2PyNl6l1PV6tWCWzXVgcxhPQ/z3zzaFiBuMEUOIjXH2Q
UzGOvx9YcDa1++j7UwImQYxKiDFw1pc/fBZCmJZ2kilg
-----END CERTIFICATE-----
+101
View File
@@ -0,0 +1,101 @@
/* standalone.c - Standalone fuzzer tester
Version 2.8.3, January 26, 2019
part of the MiniZip project
Copyright (C) 2018 sebpop
https://github.com/sebpop
Copyright (C) 2018-2019 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.
*/
#include "mz.h"
#include "mz_strm.h"
#include "mz_strm_os.h"
#include <stdio.h> /* printf */
#ifdef __cplusplus
extern "C" {
#endif
/***************************************************************************/
extern int LLVMFuzzerTestOneInput(const unsigned char *data, size_t size);
/***************************************************************************/
int main(int argc, char **argv)
{
void *stream = NULL;
int64_t file_size = 0;
uint8_t *buf = NULL;
int32_t buf_length = 0;
int32_t err = MZ_OK;
int32_t read = 0;
int32_t i = 0;
if (argc < 1)
{
printf("Must specify an input file\n");
return 1;
}
printf("Running %"PRId32" inputs\n", argc - 1);
for (i = 1; (i < argc) && (err == MZ_OK); i++)
{
read = 0;
mz_stream_os_create(&stream);
err = mz_stream_os_open(stream, argv[i], MZ_OPEN_MODE_READ);
if (err != MZ_OK)
{
printf("Skipping %s (%"PRId32")\n", argv[i], err);
}
else
{
mz_stream_os_seek(stream, 0, MZ_SEEK_END);
file_size = mz_stream_os_tell(stream);
if (file_size > INT32_MAX)
printf("File size is too large (%"PRId64")\n", file_size);
else
buf_length = (int32_t)file_size;
mz_stream_os_seek(stream, 0, MZ_SEEK_SET);
buf = NULL;
if (buf_length > 0)
buf = MZ_ALLOC(buf_length);
if (buf != NULL)
{
printf("Running %s %"PRId32"\n", argv[i], buf_length);
read = mz_stream_os_read(stream, buf, buf_length);
if (read == buf_length)
LLVMFuzzerTestOneInput(buf, buf_length);
else
err = MZ_BUF_ERROR;
MZ_FREE(buf);
}
mz_stream_os_close(stream);
}
mz_stream_os_delete(&stream);
printf("Done %s (%"PRId32")\n", argv[i], err);
}
return 0;
}
/***************************************************************************/
#ifdef __cplusplus
}
#endif
+120
View File
@@ -0,0 +1,120 @@
/* unzip_fuzzer.c - Unzip fuzzer for libFuzzer
Version 2.8.3, January 26, 2019
part of the MiniZip project
Copyright (C) 2018 The Chromium Authors
Copyright (C) 2018 Anand K. Mistry
Copyright (C) 2018-2019 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.
*/
#include "mz.h"
#include "mz_strm.h"
#include "mz_strm_mem.h"
#include "mz_zip.h"
#ifdef __cplusplus
extern "C" {
#endif
/***************************************************************************/
#define MZ_FUZZ_TEST_PWD "test123"
#define MZ_FUZZ_TEST_FILENAME "foo"
#define MZ_FUZZ_TEST_FILENAMEUC "FOO"
/***************************************************************************/
int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
{
mz_zip_file* file_info = NULL;
void *stream = NULL;
void *handle = NULL;
const char* archive_comment = NULL;
char buffer[1024];
uint16_t version_madeby = 0;
uint64_t num_entries = 0;
int64_t entry_pos = 0;
int32_t err = MZ_OK;
uint8_t encrypted = 0;
mz_stream_mem_create(&stream);
mz_stream_mem_set_buffer(stream, (void *)data, size);
mz_zip_create(&handle);
err = mz_zip_open(handle, stream, MZ_OPEN_MODE_READ);
if (err == MZ_OK)
{
/* Some archive properties that are non-fatal for reading the archive. */
mz_zip_get_comment(handle, &archive_comment);
mz_zip_get_version_madeby(handle, &version_madeby);
mz_zip_get_number_entry(handle, &num_entries);
err = mz_zip_goto_first_entry(handle);
while (err == MZ_OK)
{
err = mz_zip_entry_get_info(handle, &file_info);
if (err != MZ_OK)
break;
encrypted = (file_info->flag & MZ_ZIP_FLAG_ENCRYPTED);
err = mz_zip_entry_read_open(handle, 0,
encrypted ? MZ_FUZZ_TEST_PWD : NULL);
if (err != MZ_OK)
break;
err = mz_zip_entry_is_open(handle);
if (err != MZ_OK)
break;
/* Return value isn't checked here because we can't predict
what the value will be. */
mz_zip_entry_is_dir(handle);
entry_pos = mz_zip_get_entry(handle);
if (entry_pos < 0)
break;
err = mz_zip_entry_read(handle, buffer, sizeof(buffer));
if (err < 0)
break;
err = mz_zip_entry_close(handle);
if (err != MZ_OK)
break;
err = mz_zip_goto_next_entry(handle);
}
mz_zip_entry_close(handle);
/* Return value isn't checked here because we can't predict what the value
will be. */
mz_zip_locate_entry(handle, MZ_FUZZ_TEST_FILENAME, 0);
mz_zip_locate_entry(handle, MZ_FUZZ_TEST_FILENAMEUC, 0);
mz_zip_locate_entry(handle, MZ_FUZZ_TEST_FILENAME, 1);
mz_zip_locate_entry(handle, MZ_FUZZ_TEST_FILENAMEUC, 1);
mz_zip_close(handle);
}
mz_zip_delete(&handle);
mz_stream_mem_delete(&stream);
return 0;
}
/***************************************************************************/
#ifdef __cplusplus
}
#endif
+9
View File
@@ -0,0 +1,9 @@
# A dictionary for more efficient fuzzing of DoStuff().
# If the inputs contain multi-byte tokens, list them here.
# See http://libfuzzer.info#dictionaries
"\x50\x4b\x03\x04"
"\x50\x4b\x01\x02"
"\x50\x4b\x05\x06"
"\x50\x4b\x06\x06"
"\x50\x4b\x06\x07"
"\x50\x4b\x07\x08"
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

Some files were not shown because too many files have changed in this diff Show More