Compare commits

...
342 Commits
Author SHA1 Message Date
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
Nathan Moinvaziri 351b14e512 Version 2.5.2. 2018-08-27 19:06:59 -07:00
Nathan Moinvaziri 7188209f03 Remove check for HAVE_LIBBSD when HAVE_ARC4RANDOM_BUF will do. #300 2018-08-27 19:04:07 -07:00
Nathan Moinvaziri 95047d3c90 Merge pull request #300 from ccawley2011/libbsd
Allow using libbsd on non-Linux systems
2018-08-27 19:01:53 -07:00
Cameron Cawley 359e8e1017 Check the availability of arc4random_buf in libbsd 2018-08-27 19:31:52 +01:00
Nathan Moinvaziri e7df0f7341 Fixed more possible unused variable warnings. 2018-08-27 09:42:03 -07:00
Nathan Moinvaziri da1ab3dab1 Fixed unused variable warning with MZ_ZIP_NO_COMPRESSION. 2018-08-27 09:39:19 -07:00
Cameron Cawley abc29e4429 Allow using libbsd on non-Linux systems 2018-08-27 17:00:22 +01:00
Nathan Moinvaziri b8300ce0ea Added support for CP437 into mz_zip_reader and minizip. 2018-08-27 08:30:57 -07:00
Nathan Moinvaziri 638f31ff41 Move CP437 encoding handling higher up the stack. 2018-08-27 08:17:16 -07:00
Nathan Moinvaziri a0d1877d0e Added support for converting CP437 to UTF8 via MZ_ENCODING_CODEPAGE437.
Rename MZ_USE_FILE32API as MZ_FILE32_API and MZ_USE_WIN32_API as MZ_WIN32_API.
2018-08-26 23:23:02 -07:00
Nathan Moinvaziri 3eaa5560bf Added back ZEXPORT. Coeur/ZipArchive#444 2018-08-26 09:05:19 -07:00
Nathan Moinvaziri d4c4d4f5b1 Removed include "zlib.h" Coeur/ZipArchive#444 2018-08-25 15:37:38 -07:00
Nathan Moinvaziri d79fb39d4b Removed unused variable. 2018-08-25 10:07:27 -07:00
Nathan Moinvaziri a2e89bf85e Fixed potential unused variable warning. 2018-08-24 15:01:38 -07:00
Nathan Moinvaziri e27af27b6e Merge branch 'dev' of https://github.com/nmoinvaz/minizip into dev 2018-08-23 17:03:11 -07:00
Nathan Moinvaziri 72cc7015a2 Added easy check to see if entry is a directory. 2018-08-23 17:03:03 -07:00
Nathan Moinvaziri a2bd3c6d95 Merge pull request #298 from photom/mem_stream
fix mem_stream leak.
2018-08-21 22:02:36 -07:00
shintaro-t dcd9b72bf9 fix mem_stream leak. 2018-08-22 10:55:31 +09:00
Nathan Moinvaziri d2aa2277d9 Removed debug code.
Fixed compiler warnings.
2018-08-20 18:23:50 -07:00
Nathan Moinvaziri da4b58f6e5 Added missing comments. 2018-08-20 18:21:26 -07:00
Nathan Moinvaziri b994caeab9 Added function to get stream pointer used for mz_zip_open. 2018-08-20 16:01:08 -07:00
Nathan Moinvaziri 0b6059fc53 Added Info-ZIP extra field doc. 2018-08-20 14:15:17 -07:00
Nathan Moinvaziri 4b8e4b372c Set the file attributes for the correct system for extracted files.
Added reading of time from info-zip unix extra field v1.
2018-08-20 09:06:23 -07:00
Nathan Moinvaziri bb3d26efb7 Removed table of contents. 2018-08-18 08:42:09 -07:00
Nathan Moinvaziri 74a6a21e56 Remove buffered test since that param is no longer optional. 2018-08-18 08:41:49 -07:00
Nathan Moinvaziri 2b3cd16b6f Version 2.5.1. 2018-08-18 08:14:27 -07:00
Nathan Moinvaziri 79cfab094d Updated readme and copyright statements due to quantity of changes. 2018-08-17 12:21:06 -07:00
Nathan Moinvaziri 0e2201a5e1 Merge branch 'master' into dev 2018-08-17 11:18:38 -07:00
Nathan Moinvaziri e04106b734 Add ability to skip certain parts of install. 2018-08-17 11:18:21 -07:00
Nathan Moinvaziri b9dbacae60 Added pkzip app note and winzip aes doc. 2018-08-17 11:10:52 -07:00
Nathan Moinvaziri 590e5909b3 Removed duplicate extern keyword. 2018-08-17 11:10:35 -07:00
Nathan Moinvaziri 960a6e113c Merge pull request #297 from UncleAlbie/system-bzip2-if-available
use system Bzip2 library if available
2018-08-17 09:17:00 -07:00
Patrik Novotný 01bc6016fb use system Bzip2 library if available 2018-08-17 10:38:13 +02:00
Nathan Moinvaziri 5f01092239 Revert accidental version number change. 2018-08-16 15:52:36 -07:00
Nathan Moinvaziri 444596646e Added encryption header sizes for apps that need it in mz header.
Fixed memory stream not closing in zip reader/writer.
2018-08-16 15:08:59 -07:00
Nathan Moinvaziri c98dec4291 Version 2.5.0. 2018-08-15 07:37:30 -07:00
Nathan Moinvaziri ca014e6f37 Fixed wrong check for writing to stream. #295 2018-08-15 07:31:12 -07:00
Nathan Moinvaziri f5cc4c0355 Fixed potential invalid filename/comment buffer. #295 2018-08-14 17:46:36 -07:00
Nathan Moinvaziri 00520cd631 Fixed linux compiler warnings.
Fixed bug not reading into correct buffer when using read func with zip reader.
2018-08-14 17:09:24 -07:00
Nathan Moinvaziri c56d53156e Remove unused variable. 2018-08-14 15:59:41 -07:00
Nathan Moinvaziri 05aa0f2a07 Fixed error with file info being NULL after locate entry. 2018-08-14 15:59:19 -07:00
Nathan Moinvaziri 829ffb5cce Fixed check for directory if path ends with slash.
Fixed return error if compression method is not supported.
2018-08-14 14:00:16 -07:00
Nathan Moinvaziri b635b7db5a Rename get entry info function for consistency.
Added output path param to entry callback on reader.
Removed unused variables.
2018-08-14 12:49:51 -07:00
Nathan Moinvaziri 9f863b35ca Add zip file entry helpers to zip reader/writer. 2018-08-14 09:20:17 -07:00
Nathan Moinvaziri de09ac796f Fixed empty zip test. 2018-08-13 23:48:40 -07:00
Nathan Moinvaziri cfa2e604f6 Update readme.
Simplify code.
2018-08-13 23:29:11 -07:00
Nathan Moinvaziri a8644a1de8 Fixed const mix-match. 2018-08-13 23:20:02 -07:00
Nathan Moinvaziri 9a170d4d60 Added zip reader/writer class to encapsulate a lot of functionality.
Added create/delete functions for zip class.
Simplify locating an entry by filename.
Added more advanced functions for locating an entry based on callback decision.
Change default mem grow size to 64kb since we have unzOpen_MZ and zipOpen_MZ. #283
2018-08-13 23:07:42 -07:00
Nathan Moinvaziri 77687f3a42 Added default comparison function for locating entry that ignores path slashes.
Check current entry when locating entry before starting from first entry.
Added function to determine if entry has been opened.
Return error if can't find eocd otherwise any file will be considered open successfully.
2018-08-09 16:29:26 -07:00
Nathan Moinvaziri a64c919e0b Code cleanup. 2018-08-08 17:47:12 -07:00
Nathan Moinvaziri 9eb113dee7 Fixed closing entry with raw mode. #293 2018-08-08 17:38:38 -07:00
Nathan Moinvaziri d08e1561d9 Change order of parameters for consistency. #294 2018-08-08 09:43:14 -07:00
Nathan Moinvaziri 26308b244e Use uint8_t for raw parameter. #294 2018-08-08 09:40:15 -07:00
Nathan Moinvaziri 0e4fe82acb Fixes to reading raw encrypted entries. #294 2018-08-08 09:38:39 -07:00
Nathan Moinvaziri fa146ad6bd Clean up reading and writing of raw entries. #294
Fixed ambiguity between store and raw.
Added raw parameter to entry write open function.
2018-08-07 23:50:31 -07:00
Nathan Moinvaziri 0f8d91101b Version 2.4.0. 2018-08-05 10:33:42 -07:00
Nathan Moinvaziri a6d04a7baa Updated comment. 2018-08-05 10:31:41 -07:00
Nathan Moinvaziri 00ad03daf0 Merge branch 'master' into dev 2018-08-05 10:30:53 -07:00
Nathan Moinvaziri 2b43158414 Merge pull request #292 from UncleAlbie/soversion
Adds API versioning.
2018-08-05 09:00:37 -07:00
Patrik Novotný 0e1fd3ee8b Adds API versioning. 2018-08-03 13:10:03 +02:00
Nathan Moinvaziri c65c7a928f Added support for adding wildcard patterns when creating archive. 2018-07-31 14:43:30 -07:00
Nathan Moinvaziri 65a87024d4 Use wildcard path compare when specifying files to erase. 2018-07-31 12:48:31 -07:00
Nathan Moinvaziri c599dd60f4 Fixed compiler warnings. 2018-07-31 11:55:32 -07:00
Nathan Moinvaziri c2d5c47b51 Fixed compiler warnings. 2018-07-31 10:34:38 -07:00
Nathan Moinvaziri 89d6967afc Added erase support for split/buffered streams. 2018-07-31 10:28:46 -07:00
Nathan Moinvaziri 7c29c16575 Merge branch 'master' into dev 2018-07-31 10:21:26 -07:00
Nathan Moinvaziri 7679d219a0 Added support for erasing files from zip. 2018-07-31 10:20:21 -07:00
Nathan Moinvaziri 298126af7f Don't write duplicate ntfs, zip64, and aes extra fields. 2018-07-31 10:19:48 -07:00
Nathan Moinvaziri 7f6f08acfc Merge branch 'dev' of https://github.com/nmoinvaz/minizip into dev 2018-07-31 10:06:33 -07:00
Nathan Moinvaziri c83e4da20d Added os delete and rename functions. 2018-07-31 10:06:12 -07:00
Nathan Moinvaziri b5c2f01c31 Remove debug code 2018-07-30 21:23:07 -07:00
Nathan Moinvaziri 54dc69af1c Merge pull request #291 from vszakats/zipnewlev
fix zipOpenNewFileInZip*() to allow passing level in raw mode
2018-07-27 13:07:14 -07:00
Viktor Szakats 5f2bf575df fix zipOpenNewFileInZip*() to allow passing level in raw mode
This syncs this behaviour with original minizip (as of 1.2.11)

Ref: https://github.com/nmoinvaz/minizip/issues/289#issuecomment-408515522
2018-07-27 19:51:04 +00:00
Nathan Moinvaziri c9addbe56d Fixed returning compression method. #289 2018-07-27 10:34:07 -07:00
Nathan Moinvaziri 155b3f5e42 Return method and level from unzOpenCurrentFile3. #289 2018-07-27 10:32:22 -07:00
Nathan Moinvaziri 32a9d34295 Move #defines out of config.h. #286 2018-07-27 10:22:36 -07:00
Nathan Moinvaziri a8f4a09de6 Version 2.3.9. 2018-07-26 17:56:05 -07:00
Nathan Moinvaziri e824da8453 Updated comments about eocd functions. #288 2018-07-26 17:53:15 -07:00
Nathan Moinvaziri 51bf61d04e Second try at fixing reading global comment. #288 2018-07-26 16:41:54 -07:00
Nathan Moinvaziri f55a3c5a40 Merge branch 'dev' of https://github.com/nmoinvaz/minizip into dev 2018-07-26 13:41:16 -07:00
Nathan Moinvaziri 546607c8e4 Fix reading global comment. #288 2018-07-26 13:40:58 -07:00
Nathan Moinvaziri 4818d8efe8 Merge pull request #286 from ccawley2011/lzma-fix
Fix compilation of internal liblzma on non-x86 systems
2018-07-25 18:39:02 -07:00
Nathan Moinvaziri a2a37040c7 Fixed wrong arg passed in. 2018-07-25 11:01:02 -07:00
Nathan Moinvaziri 0fac5e72ec Fixed formatting. 2018-07-25 09:19:27 -07:00
Cameron Cawley d195fd3a59 Fix compilation of internal liblzma on non-x86 systems 2018-07-25 16:24:18 +01:00
Nathan Moinvaziri f23af14199 Don't expose mode param. 2018-07-24 16:30:18 -07:00
Nathan Moinvaziri 1149bafee3 Rename functions. #283 2018-07-22 15:33:14 -07:00
Nathan Moinvaziri aa311f349b Added function only to close unzip stream for compat. #283 2018-07-22 14:41:09 -07:00
Nathan Moinvaziri 6f32e35278 Added ability to work with mem stream in unzip compatibility. #283 2018-07-22 14:37:15 -07:00
Nathan Moinvaziri 44a14426dc Added low quality entropy alternative. 2018-07-22 12:15:04 -07:00
Nathan Moinvaziri eadf3eb181 Revert wrong version number change. 2018-07-22 12:00:55 -07:00
Nathan Moinvaziri a982abf876 Detect correct entropy function to use in cmake. 2018-07-22 11:55:58 -07:00
Nathan Moinvaziri 3719965652 Don't include sys\random if not detected. 2018-07-22 11:40:45 -07:00
Nathan Moinvaziri 91c028d276 Added compatibility functions to only close the stream in zip. #283 2018-07-22 11:22:16 -07:00
Nathan Moinvaziri 4042763729 Don't include libbsd if we don't have to. #283 2018-07-22 11:12:40 -07:00
Nathan Moinvaziri a6d1f6692e Changed name of compression inclusion #defines. 2018-07-22 10:35:49 -07:00
Nathan Moinvaziri 4d34203968 Additional check to see if we need to include crypto rand functions. #283 2018-07-22 10:32:47 -07:00
Nathan Moinvaziri 93c13564b8 Added fill_memory_filefunc to compatibility layer. 2018-07-22 10:29:52 -07:00
Nathan Moinvaziri f9ee9d7f08 Support MZ_ZIP_DECOMPRESS_ONLY and MZ_ZIP_COMPRESS_ONLY only encryption streams. #283 2018-07-22 10:18:53 -07:00
Nathan Moinvaziri 7332bd6002 Added functions to get stream from compatibility funcs. #283 2018-07-22 10:13:53 -07:00
Nathan Moinvaziri 9645f4638c Change default memory grow size to 128KB to prevent fragmentation. #283 2018-07-22 10:09:07 -07:00
Nathan Moinvaziri 8d128a7c63 Only include <bsd/stdlib.h> if using encryption. #283 2018-07-22 10:02:02 -07:00
Nathan Moinvaziri 627993ef07 Added mz_stream_get_interface. #283 2018-07-22 09:06:16 -07:00
Nathan Moinvaziri 4cfdd5dd83 Fixed correct entry close in case wrong function called. #277 2018-07-21 19:38:39 -07:00
Nathan Moinvaziri 697e995d89 Fixed compatibility for zip close. #277 2018-07-21 19:15:52 -07:00
Nathan Moinvaziri 16d9f47a4f Improve logic around raw entry close. #277 2018-07-21 14:37:56 -07:00
Nathan Moinvaziri 6ac2ff4ec2 Fixed issues with closing entry when raw. #277 2018-07-21 14:29:29 -07:00
Nathan Moinvaziri e099a2a8f7 Fixed writing of encrypted raw entries. #284 2018-07-20 07:54:54 -07:00
Nathan Moinvaziri 163b317ad6 Merge branch 'dev' 2018-07-16 19:18:59 -07:00
Nathan Moinvaziri 446d062b3c Fixed open mode for zip compatibility. 2018-07-16 19:17:46 -07:00
Nathan Moinvaziri 6781995724 Merge pull request #281 from vszakats/appendstat
make APPEND_STATUS_* values identical to minizip1 (for compatibility)
2018-07-16 19:12:21 -07:00
Nathan Moinvaziri b3bf29ab02 Merge pull request #282 from vszakats/defmem
restore DEF_MEM_LEVEL constant in mz_compat.h
2018-07-16 15:51:12 -07:00
Viktor Szakats 7a0a62dc9c restore DEF_MEM_LEVEL constant in mz_compat.h
This is defined in zutil.h in zlib, which is a private
header, so it doesn't get defined when #including zlib.h.
Instead if does get defined by minizip1 in zip.h, so
let's keep doing the same here as well.

Ref: https://github.com/madler/zlib/blob/v1.2.11/contrib/minizip/zip.h#L79-L85
Regression since: https://github.com/nmoinvaz/minizip/commit/ea99a46b6d36a11bc3e3eb3a806482d68ab4a991
2018-07-16 20:11:37 +00:00
Viktor Szakats d50e91aafe make APPEND_STATUS_* values identical to minizip1 (for compatibility)
Ref: https://github.com/madler/zlib/blob/v1.2.11/contrib/minizip/zip.h#L112-L114
2018-07-16 15:50:22 +00:00
Nathan Moinvaziri ae724eff61 Version 2.3.8. 2018-07-14 09:53:56 -07:00
Nathan Moinvaziri 4bdb436e8e Added back errno.h include. #280 2018-07-13 17:28:32 -07:00
Nathan Moinvaziri 2ecd8a0b2a Fixed disk offset not being read correctly with zip64. #279
Fixed zipOpen2_64 not working correctly with APPEND_STATUS_CREATE.
2018-07-13 12:31:39 -07:00
Nathan Moinvaziri f873eccdd2 Version 2.3.7. 2018-07-13 08:31:40 -07:00
Nathan Moinvaziri 9593ca4fc5 Fixed casting warning. 2018-07-12 15:06:19 -07:00
Nathan Moinvaziri 2e4a85e36f Added function to set memory buffer limit if relying on mem stream for its own buffer. 2018-07-12 12:39:02 -07:00
Nathan Moinvaziri ee614ff3ce Fixed loading of zips that have incorrect offsets for central directory - in instances where zips have just been appended on to an existing file.
Fixed reading of raw decrypted data, if you want completely raw then don't pass in a password. #252
2018-07-12 12:36:33 -07:00
Nathan Moinvaziri 6b12c36538 Clean up includes and fixed formatting. 2018-07-12 09:05:25 -07:00
Nathan Moinvaziri a4a5cf66aa Remove calling test script in ci. 2018-07-11 17:17:59 -07:00
84 changed files with 19573 additions and 2803 deletions
+64 -15
View File
@@ -1,26 +1,75 @@
language: c
os:
- linux
compiler:
- clang
- gcc
python:
- "2.7"
matrix:
include:
- os: windows
compiler: clang
env: TESTTOOL="--deflate --lzma --pkcrypt --wzaes --zipcd" BUILDDIR="..\build\Debug"
- os: windows
compiler: gcc
env: TESTTOOL="--bzip2 --pkcrypt --wzaes --split" BUILDDIR="..\build\Debug"
- os: linux
compiler: clang
env: TESTTOOL="--deflate --lzma --pkcrypt --wzaes" BUILDDIR="../build"
- os: linux
compiler: clang
env: TOOL="-DUSE_ZLIB=OFF" TESTTOOL="--lzma --bzip --pkcrypt --wzaes" BUILDDIR="../build"
- os: linux
compiler: clang
env: TOOL="-DUSE_BZIP=OFF" TESTTOOL="--deflate --lzma --pkcrypt --wzaes" BUILDDIR="../build"
- os: linux
compiler: clang
env: TOOL="-DUSE_LZMA=OFF" TESTTOOL="--deflate --bzip --pkcrypt --wzaes" BUILDDIR="../build"
- os: linux
compiler: clang
env: TOOL="-DUSE_AES=OFF" TESTTOOL="--deflate --pkcrypt --zipcd" BUILDDIR="../build"
- os: linux
compiler: clang
env: TOOL="-DUSE_PKCRYPT=OFF" TESTTOOL="--deflate --wzaes" BUILDDIR="../build"
- os: linux
compiler: clang
env: TOOL="-DCOMPRESS_ONLY=ON" TESTTOOL="--deflate" BUILDDIR="../build"
- os: linux
compiler: clang
env: TOOL="-DDECOMPRESS_ONLY=ON" TESTTOOL="--deflate" BUILDDIR="../build"
- os: linux
compiler: clang
env: TOOL="-DUSE_PKCRYPT=OFF -DUSE_AES=OFF" TESTTOOL="--deflate --bzip --lzma" BUILDDIR="../build"
- os: linux
compiler: clang
env: TOOL="-DUSE_OPENSSL=ON" TESTTOOL="--deflate --pkcrypt --wzaes --sign" BUILDDIR="../build"
- os: linux
compiler: gcc
env: TESTTOOL="--bzip2 --pkcrypt --wzaes --zipcd" BUILDDIR="../build"
- os: osx
compiler: clang
env: TESTTOOL="--deflate --lzma --pkcrypt --wzaes" BUILDDIR="../build"
- os: osx
compiler: gcc
env: TESTTOOL="--bzip2 --pkcrypt --wzaes --split" BUILDDIR="../build"
- os: osx
compiler: clang
env: TOOL="-DUSE_LIBCOMP=ON" TESTTOOL="--deflate --pkcrypt --wzaes" BUILDDIR="../build"
- os: osx
compiler: gcc
env: TOOL="-DUSE_LIBCOMP=ON" TESTTOOL="--deflate --pkcrypt --wzaes" BUILDDIR="../build"
- os: osx
compiler: clang
env: TOOL="-DUSE_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" TESTTOOL="--deflate --pkcrypt --wzaes --sign" BUILDDIR="../build"
before_install:
- sudo apt-get install libbsd-dev
- sudo pip install --upgrade pip
- sudo pip install numpy
- 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
- if [[ "$TRAVIS_OS_NAME" == "windows" ]]; then choco install python2; fi
- if [[ "$TRAVIS_OS_NAME" == "windows" ]]; then export PATH="/c/Python27:$PATH"; fi
before_script:
- mkdir build
- cd build
- cmake .. -DBUILD_TEST=ON
- cmake .. -DBUILD_TEST=ON $TOOL
script:
- make
- cmake --build .
- cd ../test
- travis_wait 30 python test.py --exe_dir=../build
- python test.py --raw $TEST_TOOL --exe_dir=$BUILDDIR
+338 -117
View File
@@ -1,29 +1,32 @@
#***************************************************************************
# Copyright: Matthias Schmieder,
# E-Mail: schmieder.matthias@gmail.com
# Year: 2016
# Copyright (C) 2017-2018 Nathan Moinvaziri
# https://github.com/nmoinvaz/minizip
# Copyright (C) 2016 Matthias Schmieder
# schmieder.matthias@gmail.com
#***************************************************************************
cmake_minimum_required(VERSION 2.8)
cmake_minimum_required(VERSION 3.0.2)
message(STATUS "Using CMake version ${CMAKE_VERSION}")
option(USE_COMPAT "Enables compatibility layer" ON)
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(USE_LIBCOMP "Enables Apple compression" OFF)
option(USE_OPENSSL "Enables OpenSSL for encryption" OFF)
option(USE_BRG "Enables Brian Gladman's encryption library" OFF)
option(COMPRESS_ONLY "Only support compression" OFF)
option(DECOMPRESS_ONLY "Only support decompression" OFF)
option(BUILD_TEST "Builds minizip test executable" OFF)
# Set a consistent MACOSX_RPATH default across all CMake versions.
# When CMake 2.8.12 is required, change this default to 1.
# When CMake 3.0.0 is required, remove this block (see CMP0042).
if(NOT DEFINED CMAKE_MACOSX_RPATH)
set(CMAKE_MACOSX_RPATH 0)
endif()
project("minizip")
project("minizip" C)
include(CheckLibraryExists)
include(CheckSymbolExists)
include(GNUInstallDirs)
include(ExternalProject)
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")
@@ -32,81 +35,229 @@ set(INSTALL_MAN_DIR ${CMAKE_INSTALL_MANDIR} CACHE PATH "Installation directory f
set(INSTALL_PKGCONFIG_DIR ${CMAKE_INSTALL_LIBDIR}/pkgconfig CACHE PATH "Installation directory for pkgconfig (.pc) files")
set(INSTALL_CMAKE_DIR ${CMAKE_INSTALL_LIBDIR}/cmake/minizip CACHE PATH "Installation directory for cmake files.")
set(VERSION "2.3.6")
# Minizip library version
set(VERSION "2.7.2")
# Minizip api version
set(SOVERSION "2.5")
# Set cmake debug postfix to d
set(CMAKE_DEBUG_POSTFIX "d")
# 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)
if(ZLIB_FOUND)
include_directories(${ZLIB_INCLUDE_DIRS})
endif()
add_definitions(-DHAVE_ZLIB)
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)
# 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)
if(ZLIB_FOUND)
message(STATUS "Using ZLIB ${ZLIB_VERSION_STRING}")
include_directories(${ZLIB_INCLUDE_DIRS})
add_definitions(-DHAVE_ZLIB)
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_BINARY_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()
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}
CMAKE_CACHE_ARGS
-DCMAKE_INSTALL_PREFIX:STRING=${ZLIB_INSTALL_DIR}
-DZLIB_COMPAT:STRING=ON
)
include_directories(${ZLIB_INCLUDE_DIRS})
add_definitions(-DHAVE_ZLIB)
endif()
endif()
# Check if bzip2 installation is present
if(USE_BZIP2)
set(BZIP2_ROOT ${DEF_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()
endif()
# Check to see if openssl installation is present
if (USE_OPENSSL)
set(OPENSSL_ROOT ${DEF_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})
link_directories(${OPENSSL_LIBRARIES})
endif()
endif()
# Initial source files
set(MINIZIP_SRC
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.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_zip.h)
if(COMPRESS_ONLY)
add_definitions(-DMZ_ZIP_COMPRESS_ONLY)
endif()
if(DECOMPRESS_ONLY)
add_definitions(-DMZ_ZIP_DECOMPRESS_ONLY)
endif()
mz_strm_os.h
mz_zip.h
mz_zip_rw.h)
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
# Setup predefined macros
if(COMPRESS_ONLY)
add_definitions(-DMZ_ZIP_NO_DECOMPRESSION)
endif()
if(DECOMPRESS_ONLY)
add_definitions(-DMZ_ZIP_NO_COMPRESSION)
endif()
if(NOT USE_PKCRYPT AND NOT USE_AES)
add_definitions(-DMZ_ZIP_NO_ENCRYPTION)
endif()
# Windows specific
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")
list(APPEND MINIZIP_SRC "mz_os_win32.c" "mz_strm_os_win32.c")
if (USE_PKCRYPT OR USE_AES)
if (NOT USE_OPENSSL AND NOT OPENSSL_FOUND AND NOT USE_BRG)
list(APPEND MINIZIP_SRC "mz_crypt_win32.c")
endif()
endif()
add_definitions(-D_CRT_SECURE_NO_DEPRECATE)
endif()
# Windows store specific
if("${CMAKE_SYSTEM_NAME}" STREQUAL "WindowsStore")
add_definitions(-DMZ_WINRT_API)
endif()
# Create compatibility headers
if (USE_COMPAT)
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\
#include \"mz_compat.h\"\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()
# Unix specific
if(UNIX)
add_compile_options(-O3)
list(APPEND MINIZIP_SRC "mz_os_posix.c" "mz_strm_os_posix.c")
if ((USE_PKCRYPT OR USE_AES) AND (NOT USE_OPENSSL AND NOT OPENSSL_FOUND))
if (APPLE AND NOT USE_BRG)
list(APPEND MINIZIP_SRC "mz_crypt_apple.c")
else()
set(USE_BRG TRUE)
endif()
# 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()
list(APPEND MINIZIP_SRC "mz_os_posix.c")
list(APPEND MINIZIP_PUBLIC_HEADERS "mz_os_posix.h")
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})
endif()
endif()
endif()
endif()
# Setup and use large file macros if necessary
set(define_lfs_macros TRUE)
if(ANDROID)
string(REGEX REPLACE "android-([0-9+])" "\\1"
android_api "${ANDROID_PLATFORM}")
if(${android_api} LESS 24)
if(android_api LESS 24)
set(define_lfs_macros FALSE)
endif()
endif()
@@ -118,16 +269,21 @@ if(UNIX)
add_definitions(-D_FILE_OFFSET_BITS=64)
endif()
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
find_package(PkgConfig REQUIRED)
pkg_check_modules(LIBBSD libbsd REQUIRED)
include_directories(${LIBBSD_INCLUDE_DIRS})
link_directories(${LIBBSD_LIBRARY_DIRS})
find_package(Iconv)
if (Iconv_FOUND)
message(STATUS "Using Iconv")
include_directories(${Iconv_INCLUDE_DIRS})
endif()
endif()
# Include compatibility layer
if(USE_COMPAT)
list(APPEND MINIZIP_SRC "mz_compat.c")
list(APPEND MINIZIP_PUBLIC_HEADERS "mz_compat.h")
endif()
# Include PKCRYPT
if(USE_PKCRYPT)
add_definitions(-DHAVE_PKCRYPT)
@@ -135,76 +291,104 @@ if(USE_PKCRYPT)
list(APPEND MINIZIP_PUBLIC_HEADERS "mz_strm_pkcrypt.h")
endif()
# Include AES
if(USE_AES)
add_definitions(-DHAVE_AES)
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()
# Include BRG
if(USE_BRG)
list(APPEND MINIZIP_SRC "mz_crypt_brg.c")
add_definitions(-DMZ_ZIP_NO_SIGNING)
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)
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(USE_OPENSSL AND OPENSSL_FOUND)
list(APPEND MINIZIP_SRC "mz_crypt_openssl.c")
endif()
# Include ZLIB
if(USE_ZLIB)
add_definitions(-DHAVE_ZLIB)
list(APPEND MINIZIP_SRC "mz_strm_zlib.c")
list(APPEND MINIZIP_PUBLIC_HEADERS "mz_strm_zlib.h")
include(CheckFunctionExists)
set(CMAKE_REQUIRED_LIBRARIES ZLIB::ZLIB)
CHECK_FUNCTION_EXISTS(z_get_crc_table
NEEDS_Z_PREFIX)
if(NEEDS_Z_PREFIX)
add_definitions(-DZ_PREFIX)
if(USE_LIBCOMP)
list(APPEND MINIZIP_SRC "mz_strm_libcomp.c")
list(APPEND MINIZIP_PUBLIC_HEADERS "mz_strm_libcomp.h")
else()
list(APPEND MINIZIP_SRC "mz_strm_zlib.c")
list(APPEND MINIZIP_PUBLIC_HEADERS "mz_strm_zlib.h")
if (ZLIB_FOUND)
include(CheckFunctionExists)
set(CMAKE_REQUIRED_LIBRARIES ZLIB)
CHECK_FUNCTION_EXISTS(z_get_crc_table
NEEDS_Z_PREFIX)
if(NEEDS_Z_PREFIX)
add_definitions(-DZ_PREFIX)
endif()
endif()
endif()
endif()
# Include BZIP2
if(USE_BZIP2)
add_definitions(-DHAVE_BZIP2)
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)
if(BZIP2_FOUND)
set(CMAKE_REQUIRED_LIBRARIES BZip2::BZip2)
else()
add_definitions(-DBZ_NO_STDIO)
set(BZIP2_PUBLIC_HEADERS
lib/bzip2/bzlib.h
lib/bzip2/bzlib_private.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)
include_directories(lib/bzip2)
set(BZIP2_PUBLIC_HEADERS
lib/bzip2/bzlib.h
lib/bzip2/bzlib_private.h)
source_group("BZip2" FILES ${BZIP2_SRC} ${BZIP2_PUBLIC_HEADERS})
include_directories(lib/bzip2)
source_group("BZip2" FILES ${BZIP2_SRC} ${BZIP2_PUBLIC_HEADERS})
endif()
endif()
# Include LZMA
if(USE_LZMA)
add_definitions(-DHAVE_LZMA)
add_definitions(-DHAVE_CONFIG_H)
@@ -327,51 +511,88 @@ if(CMAKE_C_COMPILER MATCHES ".*clang")
enable_option_if_supported(-msse4.1 check_opt_sse41)
enable_option_if_supported(-maes check_opt_aes)
endif()
if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_CLANG)
add_compile_options(-W -Wall)
add_compile_options(-O3)
endif()
# Create minizip library
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")
if (MSVC 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)
# Link with external libraries
if(USE_ZLIB)
target_link_libraries(${PROJECT_NAME} ZLIB::ZLIB)
target_link_libraries(${PROJECT_NAME} ${ZLIB_LIBRARIES})
if(NOT ZLIB_FOUND)
add_dependencies(${PROJECT_NAME} zlib)
endif()
endif()
if(USE_BZIP2 AND BZIP2_FOUND)
target_link_libraries(${PROJECT_NAME} ${BZIP2_LIBRARIES})
endif()
if(USE_LZMA)
set_target_properties(${PROJECT_NAME} PROPERTIES C_STANDARD 99)
endif()
if(UNIX)
target_link_libraries(${PROJECT_NAME} ${LIBBSD_LIBRARIES})
if(USE_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(USE_OPENSSL AND OPENSSL_FOUND)
target_link_libraries(${PROJECT_NAME} ${OPENSSL_LIBRARIES})
elseif(UNIX)
if(APPLE AND NOT USE_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(TARGETS ${PROJECT_NAME} EXPORT ${PROJECT_NAME}
INCLUDES DESTINATION "${INSTALL_INC_DIR}"
RUNTIME DESTINATION "${INSTALL_BIN_DIR}"
ARCHIVE DESTINATION "${INSTALL_LIB_DIR}"
LIBRARY DESTINATION "${INSTALL_LIB_DIR}")
install(EXPORT ${PROJECT_NAME}
DESTINATION "${INSTALL_CMAKE_DIR}"
NAMESPACE "MINIZIP::")
install(FILES ${MINIZIP_PUBLIC_HEADERS} DESTINATION "${INSTALL_INC_DIR}")
install(FILES ${MINIZIP_PC} DESTINATION "${INSTALL_PKGCONFIG_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}"
RUNTIME DESTINATION "${INSTALL_BIN_DIR}"
ARCHIVE DESTINATION "${INSTALL_LIB_DIR}"
LIBRARY DESTINATION "${INSTALL_LIB_DIR}")
install(EXPORT ${PROJECT_NAME}
DESTINATION "${INSTALL_CMAKE_DIR}"
NAMESPACE "MINIZIP::")
endif()
if(NOT SKIP_INSTALL_HEADERS AND NOT SKIP_INSTALL_ALL)
install(FILES ${MINIZIP_PUBLIC_HEADERS} DESTINATION "${INSTALL_INC_DIR}")
endif()
if(NOT SKIP_INSTALL_FILES AND NOT SKIP_INSTALL_ALL)
install(FILES ${MINIZIP_PC} DESTINATION "${INSTALL_PKGCONFIG_DIR}")
endif()
# Build test executable
if(BUILD_TEST)
add_executable(minizip "minizip.c")
add_executable(minizip "minizip.c" "test/test.c" "test/test.h")
target_link_libraries(minizip ${PROJECT_NAME})
install(TARGETS minizip
if(NOT SKIP_INSTALL_BINARIES AND NOT SKIP_INSTALL_ALL)
install(TARGETS minizip
RUNTIME DESTINATION "bin")
endif()
endif()
+1 -1
View File
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'Minizip'
s.version = '2.3.6'
s.version = '2.7.2'
s.license = 'zlib'
s.summary = 'Minizip contrib in zlib with the latest bug fixes and advanced features'
s.description = <<-DESC
+74 -155
View File
@@ -1,20 +1,62 @@
# Minizip 2.3.6
# minizip 2.7.2
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)
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 | Travis CI |
|:- |:-:|:-:|:-|:-:|
|[master](https://github.com/nmoinvaz/minizip/tree/master)|Active|2.x|Modern refactoring 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.|[![Master Branch Status](https://api.travis-ci.org/nmoinvaz/minizip.svg?branch=master)](https://travis-ci.org/nmoinvaz/minizip/branches)|
|[dev](https://github.com/nmoinvaz/minizip/tree/dev)|Active|2.x|Latest development code|[![Dev Branch Status](https://api.travis-ci.org/nmoinvaz/minizip.svg?branch=dev)](https://travis-ci.org/nmoinvaz/minizip/branches)|
|[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 has maintained the
original [minizip](https://github.com/madler/zlib/tree/master/contrib/minizip) project.
In 2006, I began working with the minizip project and started submitting bugs I found in the library to
Gilles Vollant via e-mail. In 2010, I implemented some additional features like disk splitting,
I/O buffering, and WinZip AES encryption. My continued work on the project necessitated setting up a public
repository so I could share these and other improvements with the rest of the world. I have been maintaining
and actively developing this fork of the library ever since. In 2017, I refactored and rewrote most of
the code as version 2 because it had become difficult to maintain and code readability had 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.
+ 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
@@ -25,36 +67,19 @@ cmake --build .
| Name | Description | Default Value |
|:- |:-|:-:|
| USE_COMPAT | Enables compatibility layer | ON |
| 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 |
| USE_AES | Enables WinZIP AES encryption | ON |
| USE_LIBCOMP | Enables Apple compression | OFF |
| USE_OPENSSL | Enables OpenSSL encryption | OFF |
| USE_BRG | Enables Brian Gladman's library | OFF |
| 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\
```
## Contents
| File(s) | Description | Required |
@@ -62,143 +87,37 @@ cmake . -DZLIB_LIBRARY=lib\zlib\release\zlibstatic.lib -DZLIB_INCLUDE_DIR=lib\zl
| 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_os\* | Platform specific file/utility functions | Likely |
| mz_crypt\* | Configuration specific crypto/hashing functions | Encryption, Signing |
| 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_libcomp.\* | Apple compression stream | No |
| mz_strm_lzma.\* | LZMA stream using liblzma | zlib or liblzma |
| mz_strm_mem.\* | Memory stream | Yes |
| mz_strm_split.\* | Disk splitting stream | No |
| mz_strm_pkcrypt.\* | PKWARE traditional encryption stream | No |
| mz_strm_posix.\* | File stream using Posix functions | Non-windows systems |
| mz_strm_win32.\* | File stream using Win32 API functions | Windows systems |
| mz_strm_os\* | Platform specific file stream | Yes |
| mz_strm_wzaes.\* | WinZIP AES stream | No |
| mz_strm_zlib.\* | Deflate stream using zlib | zlib or liblzma |
| mz_zip.\* | Zip functionality | Yes |
| mz_zip.\* | Zip format | Yes |
| mz_zip_rw.\* | Zip reader/writer | No |
## 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
### 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
+3497
View File
File diff suppressed because it is too large Load Diff
+1243
View File
File diff suppressed because it is too large Load Diff
+261
View File
@@ -0,0 +1,261 @@
# AES Encryption Information:
Encryption Specification AE-1 and AE-2
**Document version: 1.04
Last modified: January 30, 2009**
**NOTE: WinZip® users do not need to read or understand the information contained on this page. It is intended for developers of Zip file utilities.**
Changes since the original version of this document are summarized in the [Change History](https://www.winzip.com/aes_info.htm#changes) section below.
This document describes the file format that WinZip uses to create [AES-encrypted Zip files](https://www.winzip.com/learn/aes-encryption.html). The AE-1 encryption specification was introduced in WinZip 9.0 Beta 1, released in May 2003. The AE-2 encryption specification, a minor variant of the original AE-1 specification differing only in how the CRC is handled, was introduced in WinZip 9.0 Beta 3, released in January, 2004. Note that as of WinZip 11, WinZip itself encrypts most files using the AE-1 format and encrypts others using the AE-2 format.
From time to time we may update the information provided here, for example to document any changes to the file formats, or to add additional notes or implementation tips. If you would like to receive e-mail announcements of any substantive changes we make to this document, you can [sign up below](https://www.winzip.com/aes_info.htm#mailing-list)for our Developer Information mailing list.
Without compromising the basic Zip file format, WinZip Computing has extended the format specification to support AES encryption, and this document fully describes the format extension. Additionally, we are providing information about a no-cost third-party source for the actual AES encryption code--the same code that is used by WinZip. We believe that use of the free encryption code and of this specification will make it easy for all developers to add compatible advanced encryption to their Zip file utilities.
This document is not a tutorial on encryption or Zip file structure. While we have attempted to provide the necessary details of the current WinZip AES encryption format, developers and other interested third parties will need to have or obtain an understanding of basic encryption concepts, Zip file format, etc.
Developers should also review [AES Coding Tips](https://www.winzip.com/aes_tips.html) page.
WinZip Computing makes no warranties regarding the information provided in this document. In particular, WinZip Computing does not represent or warrant that the information provided here is free from errors or is suitable for any particular use, or that the file formats described here will be supported in future versions of WinZip. You should test and validate all code and techniques in accordance with good programming practice.
----------
**I. Encryption services**
To perform AES encryption and decryption, WinZip uses AES functions written by Dr. Brian Gladman. The source code for these functions is available in C/C++ and Pentium family assembler for anyone to use under an open source BSD or GPL license from [the AES project page](https://www.winzip.com/gladman.cgi) on Dr. Gladman's web site. The [AES Coding Tips](https://www.winzip.com/aes_tips.html) page also has some information on the use of these functions. WinZip Computing thanks Dr. Gladman for making his AES functions available to anyone under liberal license terms.
Dr. Gladman's encryption functions are portable to a number of operating systems and can be static linked into your applications, so there are no operating system version or library dependencies. In particular, the functions do not require Microsoft's Cryptography API.
General information on the AES standard and the encryption algorithm (also known as _Rijndael_) is readily available on the Internet. A good place to start is [http://www.nist.gov/public_affairs/releases/g00-176.htm](http://www.nist.gov/public_affairs/releases/g00-176.htm).
**II. Zip file format**
1. Base format reference
AES-encrypted files are stored within the guidelines of the standard Zip file format using only a new "extra data" field, a new compression method code, and a value in the CRC field dependant on the encryption version, AE-1 or AE-2. The basic Zip file format is otherwise unchanged.
WinZip sets the version needed to extract and version made by fields in the local and central headers to the same values it would use if the file were not encrypted.
The basic Zip file format specification used by WinZip is available via FTP from the Info-ZIP group at [ftp://ftp.info-zip.org/pub/infozip/doc/appnote-iz-latest.zip](ftp://ftp.info-zip.org/pub/infozip/doc/appnote-iz-latest.zip).
2. Compression method and encryption flag
As for any encrypted file, bit 0 of the "general purpose bit flags" field must be set to 1 in each AES-encrypted file's local header and central directory entry.
Additionally, the presence of an AES-encrypted file in a Zip file is indicated by a new compression method code (decimal 99) in the file's local header and central directory entry, used along with the AES extra data field described below. There is no change in either the _version made by_ or _version needed to extract_ codes.
The code for the actual compression method is stored in the AES extra data field (see below).
3. CRC value
For files encrypted using the AE-2 method, the standard Zip CRC value is _not_ used, and a 0 must be stored in this field. Corruption of encrypted data within a Zip file is instead detected via the [authentication code](https://www.winzip.com/aes_info.htm#authentication-code) field.
Files encrypted using the AE-1 method _do_ include the standard Zip CRC value. This, along with the fact that the vendor version stored in the AES extra data field is 0x0001 for AE-1 and 0x0002 for AE-2, is the only difference between the AE-1 and AE-2 formats.
**NOTE:** Zip utilities that support the AE-2 format are required to be able to read files that were created in the AE-1 format, and during decryption/extraction of files in AE-1 format should verify that the file's CRC matches the value stored in the CRC field.
4. AES extra data field
1. A file encrypted with AES encryption will have a special "extra data" field associated with it. This extra data field is stored in both the local header and central directory entry for the file.
Note: see the Zip file format document referenced above for general information on the format and use of extra data fields.
2. The extra data header ID for AES encryption is 0x9901. The fields are all stored in Intel low-byte/high-byte order. The extra data field currently has a length of 11: seven data bytes plus two bytes for the header ID and two bytes for the data size. Therefore, the extra data overhead for each file in the archive is 22 bytes (11 bytes in the central header plus 11 bytes in the local header).
3. The format of the data in the AES extra data field is as follows. See the notes below for additional information.
|**Offset**|**Size(bytes)**|**Content**|
|:-|:-|:-|
|0|2|Extra field header ID (0x9901)|
|2|2|Data size (currently 7, but subject to possible increase in the future)|
|4|2|Integer version number specific to the zip vendor|
|6|2|2-character vendor ID|
|8|1|Integer mode value indicating AES encryption strength|
|9|2|The actual compression method used to compress the file|
4. Notes
- **Data size**: this value is currently 7, but because it is possible that this specification will be modified in the future to store additional data in this extra field, vendors should not assume that it will always remain 7.
- **Vendor ID**: the vendor ID field should always be set to the two ASCII characters "AE".
- **Vendor version**: the vendor version for AE-1 is 0x0001. The vendor version for AE-2 is 0x0002.
Zip utilities that support AE-2 must also be able to process files that are encrypted in AE-1 format. The handling of the [CRC value](https://www.winzip.com/aes_info.htm#CRC) is the only difference between the AE-1 and AE-2 formats.
- **Encryption strength**: the mode values (encryption strength) for AE-1 and AE-2 are:
|**Value**|**Strength**|
|-|-|
|0x01|128-bit encryption key|
|0x02|192-bit encryption key|
|0x03|256-bit encryption key|
The encryption specification supports _only_ 128-, 192-, and 256-bit encryption keys. No other key lengths are permitted.
(Note: the current version of WinZip does not support encrypting files using 192-bit keys. This specification, however, does provide for the use of 192-bit keys, and WinZip is able to decrypt such files.)
- **Compression method**: the compression method is the one that would otherwise have been stored in the local and central headers for the file. For example, if the file is imploded, this field will contain the compression code 6. This is needed because a compression method of 99 is used to indicate the presence of an AES-encrypted file (see [above](https://www.winzip.com/aes_info.htm#comp-method)).
**III. Encrypted file storage format**
1. File format
Additional overhead data required for decryption is stored with the encrypted file itself (i.e., not in the headers). The actual format of the stored file is as follows; additional information about these fields is below. All fields are byte-aligned.
|**Size (bytes)**|**Content**|
|:-|:-|
|Variable|Salt value|
|2|Password verification value|
|Variable|Encrypted file data|
|10|Authentication code|
Note that the value in the "compressed size" fields of the local file header and the central directory entry is the total size of all the items listed above. In other words, it is the total size of the salt value, password verification value, encrypted data, and authentication code.
2. Salt value
The "salt" or "salt value" is a random or pseudo-random sequence of bytes that is combined with the encryption password to create encryption and authentication keys. The salt is generated by the encrypting application and is stored unencrypted with the file data. The addition of salt values to passwords provides a number of security benefits and makes dictionary attacks based on precomputed keys much more difficult.
Good cryptographic practice requires that a different salt value be used for each of multiple files encrypted with the same password. If two files are encrypted with the same password and salt, they can leak information about each other. For example, it is possible to determine whether two files encrypted with the same password and salt are identical, and an attacker who somehow already knows the contents of one of two files encrypted with the same password and salt can determine some or all of the contents of the other file. Therefore, you should make every effort to use a unique salt value for each file.
The size of the salt value depends on the length of the encryption key, as follows:
|**Key size**|**Salt size**|
|:-|:-|
|128 bits|8 bytes|
|192 bits|12 bytes|
|256 bits|16 bytes|
3. Password verification value
This two-byte value is produced as part of the process that derives the encryption and decryption keys from the password. When encrypting, a verification value is derived from the encryption password and stored with the encrypted file. Before decrypting, a verification value can be derived from the decryption password and compared to the value stored with the file, serving as a quick check that will detect _most_, but not all, incorrect passwords. There is a 1 in 65,536 chance that an incorrect password will yield a matching verification value; therefore, a matching verification value cannot be absolutely relied on to indicate a correct password.
Information on how to obtain the password verification value from Dr. Gladman's encryption library can be found on the [coding tips](https://www.winzip.com/aes_tips.html) page.
This value is stored unencrypted.
4. Encrypted file data
Encryption is applied only to the content of files. It is performed after compression, and not to any other associated data. The file data is encrypted byte-for-byte using the AES encryption algorithm operating in "CTR" mode, which means that the lengths of the compressed data and the compressed, encrypted data are the same.
It is important for implementors to note that, although the data is encrypted byte-for-byte, it is presented to the encryption and decryption functions in blocks. The block size used for encryption and decryption must be the same. To be compatible with the encryption specification, this block size must be 16 bytes (although the last block may be smaller).
5. Authentication code
Authentication provides a high quality check that the contents of an encrypted file have not been inadvertently changed or deliberately tampered with since they were first encrypted. In effect, this is a super-CRC check on the data in the file _after_ compression and encryption. (Additionally, authentication is essential when using CTR mode encryption because this mode is vulnerable to several trivial attacks in its absence.)
The authentication code is derived from the output of the encryption process. [Dr. Gladman's AES code](https://www.winzip.com/aes_info.htm#encryption) provides this service, and information about how to obtain it is in the [coding tips](https://www.winzip.com/aes_tips.html).
The authentication code is stored unencrypted. It is byte-aligned and immediately follows the last byte of encrypted data.
For more discussion about authentication, see the [authentication code FAQ](https://www.winzip.com/aes_info.htm#auth-faq) below.
**IV. Changes in WinZip 11**
Beginning with WinZip 11, WinZip makes a change in its use of the AE-1 and AE-2 file formats. The file formats themselves have not changed, and AES-encrypted files created by WinZip 11 are completely compatible with version 1.02 the WinZip AES encryption specification, which was published in January 2004.
[WinZip 9.0](https://www.winzip.com/win/en/winzip-9.html) and [WinZip 10.0](https://www.winzip.com/win/en/winzip-10.html) stored all AES-encrypted files using the AE-2 file format, which does not store the encrypted file's CRC. WinZip 11 instead uses the AE-1 file format, which does store the CRC, for most files. This provides an extra integrity check against the possibility of hardware or software errors that occur during the actual process of [file compression/encryption](https://www.winzip.com/win/en/features/data-protection.html) or decryption/decompression. For more information on this point, see the discussion of the [CRC](https://www.winzip.com/aes_info.htm#crc-faq) below.
Because for some very small files the CRC can be used to determine the exact contents of a file, regardless of the encryption method used, WinZip 11 continues to use the AE-2 file format, with no CRC stored, for files with an uncompressed size of less than 20 bytes. WinZip 11 also uses the AE-2 file format for files compressed in BZIP2 format, because the BZIP2 format contains its own integrity checks equivalent to those provided by the Zip format's CRC.
Other vendors who support WinZip's AES encryption specification may want to consider making a similar change to their own implementations of the specification, to get the benefit of the extra integrity check that it provides.
Note that the January 2004 version of the WinZip AE-2 specification, version 1.0.2, already required that all utilities that implemented the AE-2 format also be able to process files in AE-1 format, and should check on decryption/extraction of those files that the CRC was correct.
**V. Notes**
1. Non-files and zero-length files
To reduce Zip file size, it is recommended that non-file entries such as folder/directory entries not be encrypted. This, however, is only a recommendation; it is permissible to encrypt or not encrypt these entries, as you prefer.
On the other hand, it is recommended that you _do_ encrypt zero-length files. The presence of both encrypted and unencrypted files in a Zip file may trigger user warnings in some Zip file utilities, so the user experience may be improved if all files (including zero-length files) are encrypted.
If zero-length files are encrypted, the encrypted data portion of the file storage (see [above](https://www.winzip.com/aes_info.htm#encrypted-data)) will be empty, but the remainder of the encryption overhead data must be present, both in the file storage area and in the local and central headers.
2. "Mixed" Zip files
There is no requirement that all files in a Zip file be encrypted or that all files that _are_ encrypted use the same encryption method or the same password.
A Zip file can contain any combination of unencrypted files and files encrypted with any of the four currently defined encryption methods (Zip 2.0, AES-128, AES-192, AES-256). Encrypted files may use the same password or different passwords.
3. Key Generation
Key derivation, as used by AE-1 and AE-2 and as implemented in Dr. Gladman's library, is done according to the PBKDF2 algorithm, which is described in the [RFC2898](http://www.faqs.org/rfcs/rfc2898.html) guidelines. An iteration count of 1000 is used. An appropriate number of bits from the resulting hash value are used to compose three output values: an encryption key, an authentication key, and a password verification value. The first _n_ bits become the encryption key, the next _m_ bits become the authentication key, and the last 16 bits (two bytes) become the password verification value.
As part of the process outlined in RFC 2898 a pseudo-random function must be called; AE-2 uses the HMAC-SHA1 function, since it is a well-respected algorithm that has been in wide use for this purpose for several years.
Note that, when used in connection with 192- or 256-bit AES encryption, the fact that HMAC-SHA1 produces a 160-bit result means that, regardless of the password that you specify, the search space for the encryption key is unlikely to reach the theoretical 192- or 256-bit maximum, and cannot be guaranteed to exceed 160 bits. This is discussed in section B.1.1 of the [RFC2898 specification](http://www.faqs.org/rfcs/rfc2898.html).
**VI. FAQs**
- **Why is the compression method field used to indicate AES encryption?**
As opposed to using new _version made by_ and _version needed to extract_ values to signal AES encryption for a file, the new compression method is more likely to be handled gracefully by older versions of existing Zip file utilities. Zip file utilities typically do not attempt to extract files compressed with unknown methods, presumably notifying the user with an appropriate message.
- **How can I guarantee that the salt value is unique?**
In principle, the value of the salt should be different whenever the same password is used more than once, for the reasons described [above](https://www.winzip.com/aes_info.htm#salt), but this is difficult to guarantee.
In practice, the number of bytes in the salt (as specified by AE-1 and AE-2) is such that using a pseudo-random value will ensure that the probability of duplicated salt values is very low and can be safely ignored.
There is one exception to this: With the 8-byte salt values used with WinZip's 128-bit encryption it is likely that, if approximately 4 billion files are encrypted with the same password, two of the files will have the same salt, so it is advisable to stay well below this limit. Because of this, when using the same password to encrypt very large numbers of files in WinZip's AES encryption format (that is, files totalling in the millions, for example 2000 Zip files, each containing 1000 encrypted files), we recommend the use of 192-bit or 256-bit AES keys, with their 12- and 16-byte salt values, rather than 128-bit AES keys, with their 8-byte salt values.
Although salt values do not need to be truly random, it is important that they be generated in a way that the probability of duplicated salt values is not significantly higher than that which would be expected if truly random values were being used.
One technique for generating salt values is presented in the [coding tips](https://www.winzip.com/aes_tips.html#prng) page.
- **Why is there an authentication code?**
The purpose of the authentication code is to insure that, once a file's data has been compressed and encrypted, any accidental corruption of the encrypted data, and any deliberate attempts to modify the encrypted data by an attacker who does not know the password, can be detected.
The current consensus in the cryptographic community is that associating a message authentication code (or MAC) with encrypted data has strong security value because it makes a number of attacks more difficult to engineer. For AES CTR mode encryption in particular, a MAC is especially important because a number of trivial attacks are possible in its absence. The MAC used with WinZip's AES encryption is based on[HMAC-SHA1-80](http://www.faqs.org/rfcs/rfc2104.html), a mature and widely respected authentication algorithm.
The MAC is calculated after the file data has been compressed and encrypted. This order of calculation is referred to as Encrypt-then-MAC, and is preferred by many cryptographers to the alternative order of MAC-then-Encrypt because Encrypt-then-MAC is immune to some known attacks on MAC-then-Encrypt.
- **What is the role of the CRC in WinZip 11?**
Within the Zip format, the primary use of the CRC value is to detect accidental corruption of data that has been stored in the Zip file. With files encrypted according to the Zip 2.0 encryption specification, it also functions to some extent as a method of detecting deliberate attempts to modify the encrypted data, but not one that can be considered cryptographically strong. The CRC is not needed for these purposes with the WinZip AES encryption specification, where the HMAC-SHA1-based authentication code instead serves these roles.
The CRC has a drawback in that for very small files, such as files with four or fewer bytes, the CRC can be used, independent of the encryption algorithm, to determine the unencrypted contents of the file. And, in general, it is preferable to store as little information as possible about the encrypted file in the unencrypted Zip headers.
The CRC does serve one purpose that the authentication code does not. The CRC is computed based on the original uncompressed, unencrypted contents of the file, and it is checked after the file has been decrypted and decompressed. In contrast, the authentication code used with WinZip AES encryption is computed after compression/encryption and it is checked before decryption/decompression. In the very rare event of a hardware or software error that corrupts data during compression and encryption, or during decryption and decompression, the CRC will catch the error, but the authentication code will not.
WinZip 9.0 and WinZip 10.0 used AE-2 for all files that they created, and did not store the CRC. As of WinZip 11, WinZip instead uses AE-1 for most files, storing the CRC as an additional integrity check against hardware or software errors occurring during the actual compression/encryption or decryption/decompression processes. WinZip 11 will continue to use AE-2, with no CRC, for very small files of less than 20 bytes. It will also use AE-2 for files compressed in BZIP2 format, because this format has internal integrity checks equivalent to a CRC check built in.
Note that the AES-encrypted files created by WinZip 11 are fully compatible with January 2004's version 1.0.2 of the WinZip AES encryption specification, in which both the AE-1 and AE-2 variants of the file format were already defined.
**VII. Change history**
**Changes made in document version 1.04, January, 2009:** Minor clarification regarding the algorithm used to generate the authentication code.
**Changes made in document version 1.03, November, 2006:** Minor editorial and clarifying changes have been made throughout the document. The following substantive technical changes have been made:
1. WinZip 11 Usage of AE-1 and AE-2
WinZip's AES encryption specification defines two formats, known as AE-1 and AE-2, which differ in whether the CRC of the encrypted file is stored in the Zip headers. While the file formats themselves remain unchanged, WinZip's usage of them is changing. Beginning with [WinZip 11](https://www.winzip.com/aes_info.htm#winzip11), WinZip uses the AE-1 format, which includes the CRC of the encrypted file, for many encrypted files, in order to provide an additional integrity check against hardware or software errors occurring during the compression/encryption or decryption/decompression processes. Note that AES-encrypted files created by WinZip 11 are completely compatible with the previous version of the WinZip encryption specification, January 2004's version 1.0.2.
2. The discussion of [salt values](https://www.winzip.com/aes_info.htm#salt) mentions a limitation that applies to the uniqueness of salt values when very large numbers of files are encrypted with 128-bit encryption.
3. Older versions of this specification suggested that other vendors might want to use their own vendor IDs to create their own unique encryption formats. We no longer suggest that vendor-specific alternative encryption methods be created in this way.
**Changes made in document version 1.02, January, 2004:** The introductory text at the start of the document has been rewritten, and minor editorial and clarifying changes have been made throughout the document. Two substantive technical changes have been made:
1. AE-2 Specification
Standard Zip files store the CRC of each file's unencrypted data. This value is used to help detect damage or other alterations to Zip files. However, storing the CRC value has a drawback in that, for a very small file, such as a file of four or fewer bytes, the CRC value can be used, independent of the encryption algorithm, to help determine the unencrypted contents of the file.
Because of this, files encrypted with the new AE-2 method store a 0 in the CRC field of the Zip header, and use the [authentication code](https://www.winzip.com/aes_info.htm#authentication-code) instead of the CRC value to verify that encrypted data within the Zip file has not been corrupted.
The only differences between the AE-1 and AE-2 methods are the storage in AE-2 of 0 instead of the CRC in the Zip file header,and the use in the AES extra data field of 0x0002 for AE-2 instead of 0x0001 for AE-1 as the vendor version.
Zip utilities that support the AE-2 format are required to be able to read files that were created in the AE-1 format, and during decryption/extraction of files in AE-1 format should verify that the file's CRC matches the value stored in the CRC field.
2. Key Generation and HMAC-SHA1
The description of the [key generation](https://www.winzip.com/aes_info.htm#key-generation) mechanism has been updated to point out a limitation arising from its use of HMAC-SHA1 as the pseudo-random function: When used in connection with 192- or 256-bit AES encryption, the fact that HMAC-SHA1 produces a 160-bit result means that, regardless of the password that you specify, the search space for the encryption key is unlikely to reach the theoretical 192- or 256-bit maximum, and cannot be guaranteed to exceed 160 bits. This is discussed in section B.1.1 of the [RFC2898 specification](http://www.faqs.org/rfcs/rfc2898.html).
Document version: 1.04
Last modified: January 30, 2009
Copyright© 2003-2018 Corel Corporation.
All Rights Reserved
-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
+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
+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:
+6 -1
View File
@@ -16,8 +16,12 @@
#include "common.h"
#ifdef HAVE_IMMINTRIN_H
#if (defined(__GNUC__) && defined(__SSE2_MATH__)) || \
(defined(__INTEL_COMPILER) && defined(__SSE2__)) || \
(defined(_MSC_VER) && defined(_M_IX86_FP) && _M_IX86_FP >= 2)
# include <immintrin.h>
#endif
#endif
/// Find out how many equal bytes the two buffers have.
@@ -64,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;
+421 -495
View File
File diff suppressed because it is too large Load Diff
+56 -8
View File
@@ -1,5 +1,5 @@
/* mz.h -- Errors codes, zip flags and magic
Version 2.3.6, July 11, 2018
Version 2.7.2, November 2, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
@@ -19,15 +19,19 @@ extern "C" {
/***************************************************************************/
// MZ_VERSION
#define MZ_VERSION ("2.3.6")
#define MZ_VERSION ("2.7.2")
// MZ_ERROR
#define MZ_OK (0)
#define MZ_STREAM_ERROR (-1)
#define MZ_DATA_ERROR (-3)
#define MZ_MEM_ERROR (-4)
#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)
@@ -36,6 +40,14 @@ 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
#define MZ_OPEN_MODE_READ (0x01)
@@ -51,7 +63,7 @@ extern "C" {
#define MZ_SEEK_END (2)
// MZ_COMPRESS
#define MZ_COMPRESS_METHOD_RAW (0)
#define MZ_COMPRESS_METHOD_STORE (0)
#define MZ_COMPRESS_METHOD_DEFLATE (8)
#define MZ_COMPRESS_METHOD_BZIP2 (12)
#define MZ_COMPRESS_METHOD_LZMA (14)
@@ -62,7 +74,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)
@@ -71,6 +83,17 @@ extern "C" {
#define MZ_ZIP_FLAG_DEFLATE_SUPER_FAST (MZ_ZIP_FLAG_DEFLATE_FAST | \
MZ_ZIP_FLAG_DEFLATE_MAX)
#define MZ_ZIP_FLAG_DATA_DESCRIPTOR (1 << 3)
#define MZ_ZIP_FLAG_UTF8 (1 << 11)
#define MZ_ZIP_FLAG_MASK_LOCAL_INFO (1 << 13)
// MZ_ZIP_EXTENSION
#define MZ_ZIP_EXTENSION_ZIP64 (0x0001)
#define MZ_ZIP_EXTENSION_NTFS (0x000a)
#define MZ_ZIP_EXTENSION_AES (0x9901)
#define MZ_ZIP_EXTENSION_UNIX1 (0x000d)
#define MZ_ZIP_EXTENSION_SIGN (0x10c5)
#define MZ_ZIP_EXTENSION_HASH (0x1a51)
#define MZ_ZIP_EXTENSION_CDCD (0xcdcd)
// MZ_ZIP64
#define MZ_ZIP64_AUTO (0)
@@ -78,16 +101,41 @@ extern "C" {
#define MZ_ZIP64_DISABLE (2)
// MZ_HOST_SYSTEM
#define MZ_HOST_SYSTEM(VERSION_MADEBY) ((uint8_t)(VERSION_MADEBY >> 8))
#define MZ_HOST_SYSTEM_MSDOS (0)
#define MZ_HOST_SYSTEM_UNIX (3)
#define MZ_HOST_SYSTEM_WINDOWS_NTFS (10)
#define MZ_HOST_SYSTEM_OSX_DARWIN (19)
// MZ_PKCRYPT
#define MZ_PKCRYPT_HEADER_SIZE (12)
// 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_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)
+465 -115
View File
@@ -1,23 +1,26 @@
/* mz_compat.c -- Backwards compatible interface for older versions
Version 2.3.6, July 11, 2018
Version 2.7.2, November 2, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
Copyright (C) 1998-2010 Gilles Vollant
http://www.winimage.com/zLibDll/minizip.html
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 <stdio.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"
@@ -26,36 +29,56 @@
/***************************************************************************/
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;
/***************************************************************************/
extern zipFile ZEXPORT zipOpen(const char *path, int append)
static int32_t zipConvertAppendToStreamMode(int append)
{
int32_t mode = MZ_OPEN_MODE_WRITE;
switch (append)
{
case APPEND_STATUS_CREATE:
mode |= MZ_OPEN_MODE_CREATE;
break;
case APPEND_STATUS_CREATEAFTER:
mode |= MZ_OPEN_MODE_CREATE | MZ_OPEN_MODE_APPEND;
break;
case APPEND_STATUS_ADDINZIP:
mode |= MZ_OPEN_MODE_READ;
break;
}
return mode;
}
zipFile ZEXPORT zipOpen(const char *path, int append)
{
zlib_filefunc64_def pzlib = mz_stream_os_get_interface();
return zipOpen2(path, append, NULL, &pzlib);
}
extern zipFile ZEXPORT zipOpen64(const void *path, int append)
zipFile ZEXPORT zipOpen64(const void *path, int append)
{
zlib_filefunc64_def pzlib = mz_stream_os_get_interface();
return zipOpen2(path, append, NULL, &pzlib);
}
extern zipFile ZEXPORT zipOpen2(const char *path, int append, const char **globalcomment,
zipFile ZEXPORT zipOpen2(const char *path, int append, const char **globalcomment,
zlib_filefunc_def *pzlib_filefunc_def)
{
return zipOpen2_64(path, append, globalcomment, pzlib_filefunc_def);
}
extern zipFile ZEXPORT zipOpen2_64(const void *path, int append, const char **globalcomment,
zipFile ZEXPORT zipOpen2_64(const void *path, int append, const char **globalcomment,
zlib_filefunc64_def *pzlib_filefunc_def)
{
mz_compat *compat = NULL;
int32_t mode = MZ_OPEN_MODE_READWRITE;
void *handle = NULL;
zipFile zip = NULL;
int32_t mode = zipConvertAppendToStreamMode(append);
void *stream = NULL;
if (pzlib_filefunc_def)
@@ -69,43 +92,57 @@ extern zipFile ZEXPORT zipOpen2_64(const void *path, int append, const char **gl
return NULL;
}
switch (append)
{
case APPEND_STATUS_CREATE:
mode |= MZ_OPEN_MODE_CREATE;
break;
case APPEND_STATUS_CREATEAFTER:
mode |= MZ_OPEN_MODE_CREATE | MZ_OPEN_MODE_APPEND;
break;
case APPEND_STATUS_ADDINZIP:
break;
}
if (mz_stream_open(stream, path, mode) != MZ_OK)
{
mz_stream_delete(&stream);
return NULL;
}
handle = mz_zip_open(stream, mode);
zip = zipOpen_MZ(stream, append, globalcomment);
if (handle == NULL)
if (zip == NULL)
{
mz_stream_delete(&stream);
return NULL;
}
return zip;
}
zipFile ZEXPORT zipOpen_MZ(void *stream, int append, const char **globalcomment)
{
mz_compat *compat = NULL;
int32_t err = MZ_OK;
int32_t mode = zipConvertAppendToStreamMode(append);
void *handle = NULL;
mz_zip_create(&handle);
err = mz_zip_open(handle, stream, mode);
if (err != MZ_OK)
{
mz_zip_delete(&handle);
return NULL;
}
if (globalcomment != NULL)
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;
}
extern int ZEXPORT zipOpenNewFileInZip5(zipFile file, const char *filename, const zip_fileinfo *zipfi,
int ZEXPORT zipOpenNewFileInZip5(zipFile file, const char *filename, const zip_fileinfo *zipfi,
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
uint16_t size_extrafield_global, const char *comment, uint16_t compression_method, int level,
int raw, int windowBits, int memLevel, int strategy, const char *password,
@@ -157,17 +194,14 @@ extern int ZEXPORT zipOpenNewFileInZip5(zipFile file, const char *filename, cons
else
file_info.zip64 = MZ_ZIP64_DISABLE;
#ifdef HAVE_AES
if (password)
if ((password != NULL) || (raw && (file_info.flag & MZ_ZIP_FLAG_ENCRYPTED)))
file_info.aes_version = MZ_AES_VERSION;
#endif
if (raw)
level = 0;
return mz_zip_entry_write_open(compat->handle, &file_info, (int16_t)level, password);
return mz_zip_entry_write_open(compat->handle, &file_info, (int16_t)level, (uint8_t)raw, password);
}
extern int ZEXPORT zipOpenNewFileInZip4_64(zipFile file, const char *filename, const zip_fileinfo *zipfi,
int ZEXPORT zipOpenNewFileInZip4_64(zipFile file, const char *filename, const zip_fileinfo *zipfi,
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
uint16_t size_extrafield_global, const char *comment, uint16_t compression_method, int level,
int raw, int windowBits, int memLevel, int strategy, const char *password,
@@ -178,7 +212,7 @@ extern int ZEXPORT zipOpenNewFileInZip4_64(zipFile file, const char *filename, c
memLevel, strategy, password, crc_for_crypting, version_madeby, flag_base, zip64);
}
extern int ZEXPORT zipOpenNewFileInZip4(zipFile file, const char *filename, const zip_fileinfo *zipfi,
int ZEXPORT zipOpenNewFileInZip4(zipFile file, const char *filename, const zip_fileinfo *zipfi,
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
uint16_t size_extrafield_global, const char *comment, uint16_t compression_method, int level,
int raw, int windowBits, int memLevel, int strategy, const char *password,
@@ -189,7 +223,7 @@ extern int ZEXPORT zipOpenNewFileInZip4(zipFile file, const char *filename, cons
memLevel, strategy, password, crc_for_crypting, version_madeby, flag_base, 0);
}
extern int ZEXPORT zipOpenNewFileInZip3(zipFile file, const char *filename, const zip_fileinfo *zipfi,
int ZEXPORT 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,
@@ -200,7 +234,7 @@ extern int ZEXPORT zipOpenNewFileInZip3(zipFile file, const char *filename, cons
memLevel, strategy, password, crc_for_crypting, MZ_VERSION_MADEBY, 0, 0);
}
extern int ZEXPORT zipOpenNewFileInZip3_64(zipFile file, const char *filename, const zip_fileinfo *zipfi,
int ZEXPORT zipOpenNewFileInZip3_64(zipFile file, const char *filename, const zip_fileinfo *zipfi,
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
uint16_t size_extrafield_global, const char *comment, uint16_t compression_method, int level,
int raw, int windowBits, int memLevel, int strategy, const char *password,
@@ -211,24 +245,24 @@ extern int ZEXPORT zipOpenNewFileInZip3_64(zipFile file, const char *filename, c
memLevel, strategy, password, crc_for_crypting, MZ_VERSION_MADEBY, 0, zip64);
}
extern int ZEXPORT zipWriteInFileInZip(zipFile file, const void *buf, uint32_t len)
int ZEXPORT 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;
}
extern int ZEXPORT zipCloseFileInZipRaw(zipFile file, uint32_t uncompressed_size, uint32_t crc32)
int ZEXPORT zipCloseFileInZipRaw(zipFile file, uint32_t uncompressed_size, uint32_t crc32)
{
return zipCloseFileInZipRaw64(file, uncompressed_size, crc32);
}
extern int ZEXPORT zipCloseFileInZipRaw64(zipFile file, uint64_t uncompressed_size, uint32_t crc32)
int ZEXPORT zipCloseFileInZipRaw64(zipFile file, int64_t uncompressed_size, uint32_t crc32)
{
mz_compat *compat = (mz_compat *)file;
if (compat == NULL)
@@ -236,39 +270,36 @@ extern int ZEXPORT zipCloseFileInZipRaw64(zipFile file, uint64_t uncompressed_si
return mz_zip_entry_close_raw(compat->handle, uncompressed_size, crc32);
}
extern int ZEXPORT zipCloseFileInZip(zipFile file)
int ZEXPORT zipCloseFileInZip(zipFile file)
{
return zipCloseFileInZipRaw(file, 0, 0);
return zipCloseFileInZip64(file);
}
extern int ZEXPORT zipCloseFileInZip64(zipFile file)
int ZEXPORT zipCloseFileInZip64(zipFile file)
{
return zipCloseFileInZipRaw(file, 0, 0);
mz_compat *compat = (mz_compat *)file;
if (compat == NULL)
return ZIP_PARAMERROR;
return mz_zip_entry_close(compat->handle);
}
extern int ZEXPORT zipClose(zipFile file, const char *global_comment)
int ZEXPORT zipClose(zipFile file, const char *global_comment)
{
return zipClose_64(file, global_comment);
}
extern int ZEXPORT zipClose_64(zipFile file, const char *global_comment)
int ZEXPORT zipClose_64(zipFile file, const char *global_comment)
{
return zipClose2_64(file, global_comment, MZ_VERSION_MADEBY);
}
extern int ZEXPORT zipClose2_64(zipFile file, const char *global_comment, uint16_t version_madeby)
int ZEXPORT zipClose2_64(zipFile file, const char *global_comment, uint16_t version_madeby)
{
mz_compat *compat = (mz_compat *)file;
int32_t err = MZ_OK;
if (compat == NULL)
return ZIP_PARAMERROR;
if (global_comment != NULL)
mz_zip_set_comment(compat->handle, global_comment);
mz_zip_set_version_madeby(compat->handle, version_madeby);
err = mz_zip_close(compat->handle);
if (compat->handle != NULL)
err = zipClose2_MZ(file, global_comment, version_madeby);
if (compat->stream != NULL)
{
@@ -281,29 +312,62 @@ extern int ZEXPORT zipClose2_64(zipFile file, const char *global_comment, uint16
return err;
}
// Only closes the zip handle, does not close the stream
int ZEXPORT 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)
{
mz_compat *compat = (mz_compat *)file;
int32_t err = MZ_OK;
if (compat == NULL)
return ZIP_PARAMERROR;
if (compat->handle == NULL)
return err;
if (global_comment != NULL)
mz_zip_set_comment(compat->handle, global_comment);
mz_zip_set_version_madeby(compat->handle, version_madeby);
err = mz_zip_close(compat->handle);
mz_zip_delete(&compat->handle);
return err;
}
void* ZEXPORT zipGetStream(zipFile file)
{
mz_compat *compat = (mz_compat *)file;
if (compat == NULL)
return NULL;
return (void *)compat->stream;
}
/***************************************************************************/
extern unzFile ZEXPORT unzOpen(const char *path)
unzFile ZEXPORT unzOpen(const char *path)
{
return unzOpen64(path);
}
extern unzFile ZEXPORT unzOpen64(const void *path)
unzFile ZEXPORT unzOpen64(const void *path)
{
zlib_filefunc64_def pzlib = mz_stream_os_get_interface();
return unzOpen2(path, &pzlib);
}
extern unzFile ZEXPORT unzOpen2(const char *path, zlib_filefunc_def *pzlib_filefunc_def)
unzFile ZEXPORT unzOpen2(const char *path, zlib_filefunc_def *pzlib_filefunc_def)
{
return unzOpen2_64(path, pzlib_filefunc_def);
}
extern unzFile ZEXPORT unzOpen2_64(const void *path, zlib_filefunc64_def *pzlib_filefunc_def)
unzFile ZEXPORT unzOpen2_64(const void *path, zlib_filefunc64_def *pzlib_filefunc_def)
{
mz_compat *compat = NULL;
int32_t mode = MZ_OPEN_MODE_READ;
void *handle = NULL;
unzFile unz = NULL;
void *stream = NULL;
if (pzlib_filefunc_def)
@@ -317,29 +381,53 @@ extern unzFile ZEXPORT unzOpen2_64(const void *path, zlib_filefunc64_def *pzlib_
return NULL;
}
if (mz_stream_open(stream, path, mode) != MZ_OK)
if (mz_stream_open(stream, path, MZ_OPEN_MODE_READ) != MZ_OK)
{
mz_stream_delete(&stream);
return NULL;
}
handle = mz_zip_open(stream, mode);
if (handle == NULL)
unz = unzOpen_MZ(stream);
if (unz == NULL)
{
mz_stream_delete(&stream);
return NULL;
}
return unz;
}
unzFile ZEXPORT unzOpen_MZ(void *stream)
{
mz_compat *compat = NULL;
int32_t err = MZ_OK;
void *handle = NULL;
mz_zip_create(&handle);
err = mz_zip_open(handle, stream, MZ_OPEN_MODE_READ);
if (err != MZ_OK)
{
mz_zip_delete(&handle);
return NULL;
}
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;
}
extern int ZEXPORT unzClose(unzFile file)
int ZEXPORT unzClose(unzFile file)
{
mz_compat *compat = (mz_compat *)file;
int32_t err = MZ_OK;
@@ -347,7 +435,8 @@ extern int ZEXPORT unzClose(unzFile file)
if (compat == NULL)
return UNZ_PARAMERROR;
err = mz_zip_close(compat->handle);
if (compat->handle != NULL)
err = unzClose_MZ(file);
if (compat->stream != NULL)
{
@@ -360,7 +449,22 @@ extern int ZEXPORT unzClose(unzFile file)
return err;
}
extern int ZEXPORT unzGetGlobalInfo(unzFile file, unz_global_info* pglobal_info32)
// Only closes the zip handle, does not close the stream
int ZEXPORT unzClose_MZ(unzFile file)
{
mz_compat *compat = (mz_compat *)file;
int32_t err = MZ_OK;
if (compat == NULL)
return UNZ_PARAMERROR;
err = mz_zip_close(compat->handle);
mz_zip_delete(&compat->handle);
return err;
}
int ZEXPORT unzGetGlobalInfo(unzFile file, unz_global_info* pglobal_info32)
{
mz_compat *compat = (mz_compat *)file;
unz_global_info64 global_info64;
@@ -380,7 +484,7 @@ extern int ZEXPORT unzGetGlobalInfo(unzFile file, unz_global_info* pglobal_info3
return err;
}
extern int ZEXPORT unzGetGlobalInfo64(unzFile file, unz_global_info64 *pglobal_info)
int ZEXPORT unzGetGlobalInfo64(unzFile file, unz_global_info64 *pglobal_info)
{
mz_compat *compat = (mz_compat *)file;
const char *comment_ptr = NULL;
@@ -393,13 +497,13 @@ extern int ZEXPORT unzGetGlobalInfo64(unzFile file, unz_global_info64 *pglobal_i
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;
}
extern int ZEXPORT unzGetGlobalComment(unzFile file, char *comment, uint16_t comment_size)
int ZEXPORT unzGetGlobalComment(unzFile file, char *comment, uint16_t comment_size)
{
mz_compat *compat = (mz_compat *)file;
const char *comment_ptr = NULL;
@@ -409,59 +513,105 @@ extern int ZEXPORT unzGetGlobalComment(unzFile file, char *comment, uint16_t com
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;
}
extern int ZEXPORT unzOpenCurrentFile3(unzFile file, int *method, int *level, int raw, const char *password)
int ZEXPORT unzOpenCurrentFile3(unzFile file, int *method, int *level, int raw, const char *password)
{
mz_compat *compat = (mz_compat *)file;
mz_zip_file *file_info = NULL;
int32_t err = MZ_OK;
void *stream = NULL;
if (compat == NULL)
return UNZ_PARAMERROR;
if (method != NULL)
*method = 0;
if (level != NULL)
*level = 0;
return mz_zip_entry_read_open(compat->handle, (int16_t)raw, password);
compat->total_out = 0;
err = mz_zip_entry_read_open(compat->handle, (uint8_t)raw, password);
if (err == MZ_OK)
err = mz_zip_entry_get_info(compat->handle, &file_info);
if (err == MZ_OK)
{
if (method != NULL)
{
*method = file_info->compression_method;
}
if (level != NULL)
{
*level = 6;
switch (file_info->flag & 0x06)
{
case MZ_ZIP_FLAG_DEFLATE_SUPER_FAST:
*level = 1;
break;
case MZ_ZIP_FLAG_DEFLATE_FAST:
*level = 2;
break;
case MZ_ZIP_FLAG_DEFLATE_MAX:
*level = 9;
break;
}
}
}
if (err == MZ_OK)
err = mz_zip_get_stream(compat->handle, &stream);
if (err == MZ_OK)
compat->entry_pos = mz_stream_tell(stream);
return err;
}
extern int ZEXPORT unzOpenCurrentFile(unzFile file)
int ZEXPORT unzOpenCurrentFile(unzFile file)
{
return unzOpenCurrentFile3(file, NULL, NULL, 0, NULL);
}
extern int ZEXPORT unzOpenCurrentFilePassword(unzFile file, const char *password)
int ZEXPORT unzOpenCurrentFilePassword(unzFile file, const char *password)
{
return unzOpenCurrentFile3(file, NULL, NULL, 0, password);
}
extern int ZEXPORT unzOpenCurrentFile2(unzFile file, int *method, int *level, int raw)
int ZEXPORT unzOpenCurrentFile2(unzFile file, int *method, int *level, int raw)
{
return unzOpenCurrentFile3(file, method, level, raw, NULL);
}
extern int ZEXPORT unzReadCurrentFile(unzFile file, void *buf, uint32_t len)
int ZEXPORT 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;
}
extern int ZEXPORT unzCloseCurrentFile(unzFile file)
int ZEXPORT 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;
}
extern int ZEXPORT unzGetCurrentFileInfo(unzFile file, unz_file_info *pfile_info, char *filename,
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)
{
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)
@@ -491,12 +641,14 @@ extern int ZEXPORT unzGetCurrentFileInfo(unzFile file, unz_file_info *pfile_info
pfile_info->compressed_size = (uint32_t)file_info->compressed_size;
pfile_info->uncompressed_size = (uint32_t)file_info->uncompressed_size;
if (filename_size > 0 && filename != NULL)
if (filename_size > 0 && filename != NULL && file_info->filename != NULL)
{
bytes_to_copy = filename_size;
if (bytes_to_copy > file_info->filename_size)
bytes_to_copy = file_info->filename_size;
memcpy(filename, file_info->filename, bytes_to_copy);
if (bytes_to_copy < filename_size)
filename[bytes_to_copy] = 0;
}
if (extrafield_size > 0 && extrafield != NULL)
{
@@ -505,23 +657,25 @@ extern int ZEXPORT unzGetCurrentFileInfo(unzFile file, unz_file_info *pfile_info
bytes_to_copy = file_info->extrafield_size;
memcpy(extrafield, file_info->extrafield, bytes_to_copy);
}
if (comment_size > 0 && comment != NULL)
if (comment_size > 0 && comment != NULL && file_info->comment != NULL)
{
bytes_to_copy = comment_size;
if (bytes_to_copy > file_info->comment_size)
bytes_to_copy = file_info->comment_size;
memcpy(comment, file_info->comment, bytes_to_copy);
if (bytes_to_copy < comment_size)
comment[bytes_to_copy] = 0;
}
}
return err;
}
extern int ZEXPORT unzGetCurrentFileInfo64(unzFile file, unz_file_info64 * pfile_info, char *filename,
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)
{
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)
@@ -548,17 +702,17 @@ extern int ZEXPORT unzGetCurrentFileInfo64(unzFile file, unz_file_info64 * pfile
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)
if (filename_size > 0 && filename != NULL && file_info->filename != NULL)
{
bytes_to_copy = filename_size;
if (bytes_to_copy > file_info->filename_size)
bytes_to_copy = file_info->filename_size;
memcpy(filename, file_info->filename, bytes_to_copy);
if (file_info->filename_size < filename_size)
filename[file_info->filename_size] = 0;
if (bytes_to_copy < filename_size)
filename[bytes_to_copy] = 0;
}
if (extrafield_size > 0 && extrafield != NULL)
@@ -569,49 +723,145 @@ extern int ZEXPORT unzGetCurrentFileInfo64(unzFile file, unz_file_info64 * pfile
memcpy(extrafield, file_info->extrafield, bytes_to_copy);
}
if (comment_size > 0 && comment != NULL)
if (comment_size > 0 && comment != NULL && file_info->comment != NULL)
{
bytes_to_copy = comment_size;
if (bytes_to_copy > file_info->comment_size)
bytes_to_copy = file_info->comment_size;
memcpy(comment, file_info->comment, bytes_to_copy);
if (file_info->comment_size < comment_size)
comment[file_info->comment_size] = 0;
if (bytes_to_copy < comment_size)
comment[bytes_to_copy] = 0;
}
}
return err;
}
extern int ZEXPORT unzGoToFirstFile(unzFile file)
int ZEXPORT 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);
}
extern int ZEXPORT unzGoToNextFile(unzFile file)
int ZEXPORT 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;
}
extern int ZEXPORT unzLocateFile(unzFile file, const char *filename, unzFileNameComparer filename_compare_func)
int ZEXPORT 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;
return mz_zip_locate_entry(compat->handle, filename, filename_compare_func);
preserve_index = compat->entry_index;
err = mz_zip_goto_first_entry(compat->handle);
while (err == MZ_OK)
{
err = mz_zip_entry_get_info(compat->handle, &file_info);
if (err != MZ_OK)
break;
if (filename_compare_func != NULL)
result = filename_compare_func(file, filename, file_info->filename);
else
result = strcmp(filename, file_info->filename);
if (result == 0)
return MZ_OK;
err = mz_zip_goto_next_entry(compat->handle);
}
compat->entry_index = preserve_index;
return err;
}
extern int32_t ZEXPORT unzGetOffset(unzFile file)
/***************************************************************************/
int ZEXPORT 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 ZEXPORT 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 ZEXPORT 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 ZEXPORT 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 ZEXPORT unzGetOffset(unzFile file)
{
return (int32_t)unzGetOffset64(file);
}
extern int64_t ZEXPORT unzGetOffset64(unzFile file)
int64_t ZEXPORT unzGetOffset64(unzFile file)
{
mz_compat *compat = (mz_compat *)file;
if (compat == NULL)
@@ -619,12 +869,12 @@ extern int64_t ZEXPORT unzGetOffset64(unzFile file)
return mz_zip_get_entry(compat->handle);
}
extern int ZEXPORT unzSetOffset(unzFile file, uint32_t pos)
int ZEXPORT unzSetOffset(unzFile file, uint32_t pos)
{
return unzSetOffset64(file, pos);
}
extern int ZEXPORT unzSetOffset64(unzFile file, uint64_t pos)
int ZEXPORT unzSetOffset64(unzFile file, int64_t pos)
{
mz_compat *compat = (mz_compat *)file;
if (compat == NULL)
@@ -632,15 +882,109 @@ extern int ZEXPORT unzSetOffset64(unzFile file, uint64_t pos)
return (int)mz_zip_goto_entry(compat->handle, pos);
}
extern int ZEXPORT unzGetLocalExtrafield(unzFile file, void *buf, unsigned len) // TODO
int ZEXPORT 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;
}
int32_t ZEXPORT unzTell(unzFile file)
{
mz_compat *compat = (mz_compat *)file;
if (compat == NULL)
return UNZ_PARAMERROR;
return (int32_t)compat->total_out;
}
int64_t ZEXPORT unzTell64(unzFile file)
{
mz_compat *compat = (mz_compat *)file;
if (compat == NULL)
return UNZ_PARAMERROR;
return (int64_t)compat->total_out;
}
int ZEXPORT unzSeek(unzFile file, int32_t offset, int origin)
{
return unzSeek64(file, offset, origin);
}
int ZEXPORT 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, SEEK_SET);
if (err == MZ_OK)
compat->total_out = position;
return err;
}
int ZEXPORT 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)
{
mz_compat *compat = (mz_compat *)file;
if (compat == NULL)
return NULL;
return (void *)compat->stream;
}
/***************************************************************************/
void fill_fopen_filefunc(zlib_filefunc_def *pzlib_filefunc_def)
@@ -679,3 +1023,9 @@ void fill_win32_filefunc64W(zlib_filefunc64_def *pzlib_filefunc_def)
if (pzlib_filefunc_def != NULL)
*pzlib_filefunc_def = mz_stream_os_get_interface();
}
void fill_memory_filefunc(zlib_filefunc_def *pzlib_filefunc_def)
{
if (pzlib_filefunc_def != NULL)
*pzlib_filefunc_def = mz_stream_mem_get_interface();
}
+131 -77
View File
@@ -1,11 +1,11 @@
/* mz_compat.h -- Backwards compatible interface for older versions
Version 2.3.6, July 11, 2018
Version 2.7.2, November 2, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
Copyright (C) 1998-2010 Gilles Vollant
http://www.winimage.com/zLibDll/minizip.html
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.
@@ -15,23 +15,35 @@
#define MZ_COMPAT_H
#include <stdint.h>
#include <time.h>
#ifdef __cplusplus
extern "C" {
#endif
#ifdef HAVE_ZLIB
#include "zlib.h"
#else
#define ZEXPORT
#if defined(HAVE_ZLIB) && defined(MAX_MEM_LEVEL)
#ifndef DEF_MEM_LEVEL
# if MAX_MEM_LEVEL >= 8
# define DEF_MEM_LEVEL 8
# else
# define DEF_MEM_LEVEL MAX_MEM_LEVEL
# endif
#endif
#endif
#ifndef MAX_WBITS
#define MAX_WBITS (15)
#endif
#ifndef DEF_MEM_LEVEL
#define DEF_MEM_LEVEL (8)
#endif
#ifndef ZEXPORT
# define ZEXPORT
#endif
/***************************************************************************/
#if defined(USE_FILE32API)
# define MZ_USE_FILE32API
# define fopen64 fopen
# define ftello64 ftell
# define fseeko64 fseek
@@ -97,55 +109,60 @@ typedef struct
#define Z_BZIP2ED (12)
#define APPEND_STATUS_CREATE (1)
#define APPEND_STATUS_CREATEAFTER (2)
#define APPEND_STATUS_ADDINZIP (3)
#define APPEND_STATUS_CREATE (0)
#define APPEND_STATUS_CREATEAFTER (1)
#define APPEND_STATUS_ADDINZIP (2)
/***************************************************************************/
// Writing a zip file
extern zipFile ZEXPORT zipOpen(const char *path, int append);
extern zipFile ZEXPORT zipOpen64(const void *path, int append);
extern zipFile ZEXPORT zipOpen2(const char *path, int append, const char **globalcomment,
zlib_filefunc_def *pzlib_filefunc_def);
extern zipFile ZEXPORT zipOpen2_64(const void *path, int append, const char **globalcomment,
zlib_filefunc64_def *pzlib_filefunc_def);
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,
zlib_filefunc_def *pzlib_filefunc_def);
zipFile ZEXPORT 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);
extern int ZEXPORT 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);
extern int ZEXPORT zipOpenNewFileInZip3_64(zipFile file, const char *filename, const zip_fileinfo *zipfi,
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
uint16_t size_extrafield_global, const char *comment, uint16_t compression_method, int level,
int raw, int windowBits, int memLevel, int strategy, const char *password,
uint32_t crc_for_crypting, int zip64);
extern int ZEXPORT zipOpenNewFileInZip4(zipFile file, const char *filename, const zip_fileinfo *zipfi,
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
uint16_t size_extrafield_global, const char *comment, uint16_t compression_method, int level,
int raw, int windowBits, int memLevel, int strategy, const char *password,
uint32_t crc_for_crypting, uint16_t version_madeby, uint16_t flag_base);
extern int ZEXPORT zipOpenNewFileInZip4_64(zipFile file, const char *filename, const zip_fileinfo *zipfi,
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
uint16_t size_extrafield_global, const char *comment, uint16_t compression_method, int level,
int raw, int windowBits, int memLevel, int strategy, const char *password,
uint32_t crc_for_crypting, uint16_t version_madeby, uint16_t flag_base, int zip64);
extern int ZEXPORT zipOpenNewFileInZip5(zipFile file, const char *filename, const zip_fileinfo *zipfi,
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
uint16_t size_extrafield_global, const char *comment, uint16_t compression_method, int level,
int raw, int windowBits, int memLevel, int strategy, const char *password,
uint32_t crc_for_crypting, uint16_t version_madeby, uint16_t flag_base, int zip64);
int ZEXPORT 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,
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,
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,
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,
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);
extern int ZEXPORT zipWriteInFileInZip(zipFile file, const void *buf, uint32_t len);
int ZEXPORT zipWriteInFileInZip(zipFile file, const void *buf, uint32_t len);
extern int ZEXPORT zipCloseFileInZipRaw(zipFile file, uint32_t uncompressed_size, uint32_t crc32);
extern int ZEXPORT zipCloseFileInZipRaw64(zipFile file, uint64_t uncompressed_size, uint32_t crc32);
extern int ZEXPORT zipCloseFileInZip(zipFile file);
extern int ZEXPORT zipCloseFileInZip64(zipFile file);
int ZEXPORT zipCloseFileInZipRaw(zipFile file, uint32_t uncompressed_size, uint32_t crc32);
int ZEXPORT zipCloseFileInZipRaw64(zipFile file, int64_t uncompressed_size, uint32_t crc32);
int ZEXPORT zipCloseFileInZip(zipFile file);
int ZEXPORT zipCloseFileInZip64(zipFile file);
extern int ZEXPORT zipClose(zipFile file, const char *global_comment);
extern int ZEXPORT zipClose_64(zipFile file, const char *global_comment);
extern int ZEXPORT zipClose2_64(zipFile file, const char *global_comment, uint16_t version_madeby);
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);
/***************************************************************************/
@@ -240,36 +257,71 @@ typedef int (*unzIteratorFunction2)(unzFile file, unz_file_info64 *pfile_info, c
uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment, uint16_t comment_size);
/***************************************************************************/
// Opening and close a zip file
extern unzFile ZEXPORT unzOpen(const char *path);
extern unzFile ZEXPORT unzOpen64(const void *path);
extern unzFile ZEXPORT unzOpen2(const char *path, zlib_filefunc_def *pzlib_filefunc_def);
extern unzFile ZEXPORT unzOpen2_64(const void *path, zlib_filefunc64_def *pzlib_filefunc_def);
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);
extern int ZEXPORT unzClose(unzFile file);
extern int ZEXPORT unzGetGlobalInfo(unzFile file, unz_global_info* pglobal_info32);
extern int ZEXPORT unzGetGlobalInfo64(unzFile file, unz_global_info64 *pglobal_info);
extern int ZEXPORT unzGetGlobalComment(unzFile file, char *comment, uint16_t comment_size);
extern int ZEXPORT unzOpenCurrentFile(unzFile file);
extern int ZEXPORT unzOpenCurrentFilePassword(unzFile file, const char *password);
extern int ZEXPORT unzOpenCurrentFile2(unzFile file, int *method, int *level, int raw);
extern int ZEXPORT unzOpenCurrentFile3(unzFile file, int *method, int *level, int raw, const char *password);
extern int ZEXPORT unzReadCurrentFile(unzFile file, void *buf, uint32_t len);
extern int ZEXPORT unzCloseCurrentFile(unzFile file);
int ZEXPORT unzClose(unzFile file);
int ZEXPORT unzClose_MZ(unzFile file);
extern 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);
extern 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);
extern int ZEXPORT unzGoToFirstFile(unzFile file);
extern int ZEXPORT unzGoToNextFile(unzFile file);
extern int ZEXPORT unzLocateFile(unzFile file, const char *filename, unzFileNameComparer filename_compare_func);
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);
extern int64_t ZEXPORT unzGetOffset64(unzFile file);
extern int32_t ZEXPORT unzGetOffset(unzFile file);
extern int ZEXPORT unzSetOffset64(unzFile file, uint64_t pos);
extern int ZEXPORT unzSetOffset(unzFile file, uint32_t pos);
extern int ZEXPORT unzGetLocalExtrafield(unzFile file, void *buf, unsigned len);
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);
int ZEXPORT unzGetLocalExtrafield(unzFile file, void *buf, unsigned 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;
int ZEXPORT unzGetFilePos(unzFile file, unz_file_pos *file_pos);
int ZEXPORT 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;
int ZEXPORT unzGetFilePos64(unzFile file, unz64_file_pos *file_pos);
int ZEXPORT unzGoToFilePos64(unzFile file, const unz64_file_pos *file_pos);
int64_t ZEXPORT unzGetOffset64(unzFile file);
int32_t ZEXPORT unzGetOffset(unzFile file);
int ZEXPORT unzSetOffset64(unzFile file, int64_t pos);
int ZEXPORT unzSetOffset(unzFile file, uint32_t pos);
int32_t ZEXPORT unzTell(unzFile file);
int64_t ZEXPORT unzTell64(unzFile file);
int ZEXPORT unzSeek(unzFile file, int32_t offset, int origin);
int ZEXPORT unzSeek64(unzFile file, int64_t offset, int origin);
int ZEXPORT unzEndOfFile(unzFile file);
void* ZEXPORT unzGetStream(unzFile file);
/***************************************************************************/
@@ -279,16 +331,18 @@ 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 unztell unzTell64
#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_make_dir
#define get_file_crc(p,b,bs,rc) mz_get_file_crc(p,rc)
#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
+61
View File
@@ -0,0 +1,61 @@
/* mz_crypt.h -- Crypto/hash functions
Version 2.7.2, November 2, 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_CRYPT_H
#define MZ_CRYPT_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/***************************************************************************/
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
+535
View File
@@ -0,0 +1,535 @@
/* mz_crypt_apple.c -- Crypto/hash functions for Apple
Version 2.7.2, November 2, 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 <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>
#include "mz.h"
#include "mz_os.h"
#include "mz_strm_os.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;
SecTrustRef trust = NULL;
SecPolicyRef trust_policy = NULL;
OSStatus status = noErr;
OSStatus verify_status = noErr;
size_t signer_count = 0;
int32_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;
}
+445
View File
@@ -0,0 +1,445 @@
/* mz_crypt_brg.c -- Crypto/hash functions using Brian Gladman's library
Version 2.7.2, November 2, 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#if defined(HAVE_GETRANDOM)
# include <sys/random.h>
#endif
#if defined(HAVE_LIBBSD)
# include <bsd/stdlib.h> // arc4random_buf
#endif
#include "aes.h"
#include "sha1.h"
#include "sha2.h"
#include "hmac.h"
#include "mz.h"
#include "mz_os.h"
#include "mz_strm_os.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;
}
+681
View File
@@ -0,0 +1,681 @@
/* mz_crypt_openssl.c -- Crypto/hash functions for OpenSSL
Version 2.7.2, November 2, 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 <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>
#include "mz.h"
#include "mz_os.h"
#include "mz_strm_os.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;
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
if (signer_count > 0)
err = MZ_OK;
for (i = 0; i < sk_X509_num(signers); 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;
}
+755
View File
@@ -0,0 +1,755 @@
/* mz_crypt_win32.c -- Crypto/hash functions for Windows
Version 2.7.2, November 2, 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.
*/
#pragma comment(lib, "crypt32.lib")
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <windows.h>
#include <wincrypt.h>
#include "mz.h"
#include "mz_os.h"
#include "mz_strm_os.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 *aes = (mz_crypt_aes *)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;
ALG_ID hash_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 (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);
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, CRYPT_IPSEC_HMAC_KEY, &aes->key);
}
if (!result)
{
aes->error = GetLastError();
err = MZ_CRYPT_ERROR;
}
MZ_FREE(key_blob);
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 *hmac = (mz_crypt_hmac *)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;
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;
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;
}
key_blob_size = sizeof(key_blob_header_s) + key_length;
key_blob = (uint8_t *)MZ_ALLOC(key_blob_size);
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);
if (!result)
{
hmac->error = GetLastError();
err = MZ_CRYPT_ERROR;
}
MZ_FREE(key_blob);
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;
int32_t err = MZ_OK;
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 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)
{
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 key_spec = 0;
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)
result = CryptVerifyMessageSignature(&verify_params, 0, signature, signature_size,
decoded, &decoded_size, NULL);
crypt_msg = CryptMsgOpenToDecode(PKCS_7_ASN_ENCODING | X509_ASN_ENCODING, 0, 0, 0, NULL, NULL);
if (crypt_msg != NULL)
{
#if 0 // 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);
#endif
}
if ((crypt_msg != NULL) && (result) && (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;
}
+116 -61
View File
@@ -1,11 +1,11 @@
/* mz_os.c -- System functions
Version 2.3.6, July 11, 2018
Version 2.7.2, November 2, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
Copyright (C) 1998-2010 Gilles Vollant
http://www.winimage.com/zLibDll/minizip.html
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,62 +14,16 @@
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <ctype.h>
#include "mz.h"
#include "mz_os.h"
#include "mz_strm.h"
#include "mz_strm_crc32.h"
#include "mz_strm_os.h"
/***************************************************************************/
int32_t mz_make_dir(const char *path)
{
int32_t err = MZ_OK;
int16_t len = 0;
char *current_dir = NULL;
char *match = NULL;
char hold = 0;
len = (int16_t)strlen(path);
if (len <= 0)
return 0;
current_dir = (char *)MZ_ALLOC(len + 1);
if (current_dir == NULL)
return MZ_MEM_ERROR;
strcpy(current_dir, path);
if (current_dir[len - 1] == '/')
current_dir[len - 1] = 0;
err = mz_os_make_dir(current_dir);
if (err != MZ_OK)
{
match = current_dir + 1;
while (1)
{
while (*match != 0 && *match != '\\' && *match != '/')
match += 1;
hold = *match;
*match = 0;
err = mz_os_make_dir(current_dir);
if (err != MZ_OK)
break;
if (hold == 0)
break;
*match = hold;
match += 1;
}
}
MZ_FREE(current_dir);
return err;
}
int32_t mz_path_combine(char *path, const char *join, int32_t max_path)
{
int32_t path_len = 0;
@@ -77,11 +31,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
{
@@ -93,6 +48,56 @@ int32_t mz_path_combine(char *path, const char *join, int32_t max_path)
return MZ_OK;
}
int32_t mz_path_compare_wc(const char *path, const char *wildcard, uint8_t ignore_case)
{
while (*path != 0)
{
switch (*wildcard)
{
case '*':
if (*(wildcard + 1) == 0)
return MZ_OK;
while (*path != 0)
{
if (mz_path_compare_wc(path, (wildcard + 1), ignore_case) == MZ_OK)
return MZ_OK;
path += 1;
}
return MZ_EXIST_ERROR;
default:
// Ignore differences in path slashes on platforms
if ((*path == '\\' && *wildcard == '/') || (*path == '/' && *wildcard == '\\'))
break;
if (ignore_case)
{
if (tolower(*path) != tolower(*wildcard))
return MZ_EXIST_ERROR;
}
else
{
if (*path != *wildcard)
return MZ_EXIST_ERROR;
}
break;
}
path += 1;
wildcard += 1;
}
if ((*wildcard != 0) && (*wildcard != '*'))
return MZ_EXIST_ERROR;
return MZ_OK;
}
int32_t mz_path_resolve(const char *path, char *output, int32_t max_output)
{
const char *source = path;
@@ -108,7 +113,7 @@ int32_t mz_path_resolve(const char *path, char *output, int32_t max_output)
if ((*check == '\\') || (*check == '/'))
check += 1;
if ((source == path) || (check != source) || (*target == 0))
if ((source == path) || (check != source))
{
// Skip double paths
if ((*check == '\\') || (*check == '/'))
@@ -120,7 +125,7 @@ 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
@@ -131,9 +136,9 @@ 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
if (check[1] != 0 || (path != source))
{
@@ -164,7 +169,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))
@@ -192,14 +197,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)
{
@@ -235,13 +240,61 @@ int32_t mz_path_get_filename(const char *path, const char **filename)
return MZ_OK;
}
int32_t mz_get_file_crc(const char *path, uint32_t *result_crc)
int32_t mz_dir_make(const char *path)
{
int32_t err = MZ_OK;
int16_t len = 0;
char *current_dir = NULL;
char *match = NULL;
char hold = 0;
len = (int16_t)strlen(path);
if (len <= 0)
return 0;
current_dir = (char *)MZ_ALLOC((uint16_t)len + 1);
if (current_dir == NULL)
return MZ_MEM_ERROR;
strcpy(current_dir, path);
if (current_dir[len - 1] == '/')
current_dir[len - 1] = 0;
err = mz_os_make_dir(current_dir);
if (err != MZ_OK)
{
match = current_dir + 1;
while (1)
{
while (*match != 0 && *match != '\\' && *match != '/')
match += 1;
hold = *match;
*match = 0;
err = mz_os_make_dir(current_dir);
if (err != MZ_OK)
break;
if (hold == 0)
break;
*match = hold;
match += 1;
}
}
MZ_FREE(current_dir);
return err;
}
int32_t mz_file_get_crc(const char *path, uint32_t *result_crc)
{
void *stream = NULL;
void *crc32_stream = NULL;
int32_t read = 0;
int32_t err = MZ_OK;
uint8_t buf[INT16_MAX];
uint8_t buf[16384];
mz_stream_os_create(&stream);
@@ -277,3 +330,5 @@ int32_t mz_get_file_crc(const char *path, uint32_t *result_crc)
return err;
}
/***************************************************************************/
+87 -13
View File
@@ -1,5 +1,5 @@
/* mz_os.h -- System functions
Version 2.3.6, July 11, 2018
Version 2.7.2, November 2, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
@@ -13,6 +13,7 @@
#define MZ_OS_H
#include <stdint.h>
#include <time.h>
#ifdef __cplusplus
extern "C" {
@@ -20,16 +21,14 @@ extern "C" {
/***************************************************************************/
#if !defined(_WIN32) && !defined(MZ_USE_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
@@ -45,24 +44,99 @@ extern "C" {
/***************************************************************************/
int32_t mz_make_dir(const char *path);
// Creates a directory recursively
#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
int32_t mz_path_compare_wc(const char *path, const char *wildcard, uint8_t ignore_case);
// Compare two paths with wildcard
int32_t mz_path_resolve(const char *path, char *target, int32_t max_target);
// Resolves path
int32_t mz_path_remove_filename(const char *path);
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
int32_t mz_get_file_crc(const char *path, uint32_t *result_crc);
int32_t mz_dir_make(const char *path);
// Creates a directory recursively
int32_t mz_file_get_crc(const char *path, uint32_t *result_crc);
// Gets the crc32 hash of a file
/***************************************************************************/
// 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
/***************************************************************************/
#ifdef __cplusplus
+135 -24
View File
@@ -1,5 +1,5 @@
/* mz_os_posix.c -- System functions for posix
Version 2.3.6, July 11, 2018
Version 2.7.2, November 2, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
@@ -13,36 +13,126 @@
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <iconv.h>
#include <sys/types.h>
#include <sys/stat.h>
#if defined unix || defined __APPLE__
# include <unistd.h>
#if defined(__APPLE__) || defined(unix)
# include <utime.h>
# include <unistd.h>
#endif
#if defined __linux__
# include <bsd/stdlib.h>
#else
# include <stdlib.h>
#if defined(__APPLE__)
# include <mach/clock.h>
# include <mach/mach.h>
#endif
#include "mz.h"
#include "mz_strm.h"
#include "mz_os.h"
#include "mz_os_posix.h"
/***************************************************************************/
#if defined(HAVE_PKCRYPT) || defined(HAVE_AES)
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);
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;
}
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)
{
static unsigned calls = 0;
int32_t i = 0;
// Ensure different random header each time
if (++calls == 1)
{
#define PI_SEED 3141592654UL
srand((unsigned)(time(NULL) ^ PI_SEED));
}
while (i < size)
buf[i++] = (rand() >> 7) & 0xff;
return size;
}
#endif
int32_t mz_posix_file_exists(const char *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;
return MZ_OK;
}
int32_t mz_os_delete(const char *path)
{
if (unlink(path) == -1)
return MZ_EXIST_ERROR;
return MZ_OK;
}
int32_t mz_os_file_exists(const char *path)
{
struct stat stat_info;
@@ -53,7 +143,7 @@ int32_t mz_posix_file_exists(const char *path)
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;
@@ -64,7 +154,7 @@ int64_t mz_posix_get_file_size(const char *path)
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;
char *name = NULL;
@@ -78,7 +168,7 @@ int32_t mz_posix_get_file_date(const char *path, time_t *modified_date, time_t *
// 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] == '/')
name[len - 1] = 0;
@@ -102,7 +192,7 @@ 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;
@@ -117,7 +207,7 @@ int32_t mz_posix_set_file_date(const char *path, time_t modified_date, time_t ac
return MZ_OK;
}
int32_t mz_posix_get_file_attribs(const char *path, uint32_t *attributes)
int32_t mz_os_get_file_attribs(const char *path, uint32_t *attributes)
{
struct stat stat_info;
int32_t err = MZ_OK;
@@ -129,7 +219,7 @@ int32_t mz_posix_get_file_attribs(const char *path, uint32_t *attributes)
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;
@@ -139,7 +229,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;
@@ -151,19 +241,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;
@@ -172,7 +262,7 @@ 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;
stat(path, &path_stat);
@@ -180,3 +270,24 @@ int32_t mz_posix_is_dir(const char *path)
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);
}
-68
View File
@@ -1,68 +0,0 @@
/* mz_os_posix.h -- System functions for posix
Version 2.3.6, July 11, 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_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_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
+206 -70
View File
@@ -1,5 +1,5 @@
/* mz_os_win32.c -- System functions for Windows
Version 2.3.6, July 11, 2018
Version 2.7.2, November 2, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
@@ -17,12 +17,11 @@
#include <errno.h>
#include <windows.h>
#include <wincrypt.h>
#include "mz.h"
#include "mz_os.h"
#include "mz_os_win32.h"
#include "mz_strm_os.h"
/***************************************************************************/
@@ -43,22 +42,70 @@ typedef struct DIR_int_s {
/***************************************************************************/
#if defined(HAVE_PKCRYPT) || defined(HAVE_AES)
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));
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, 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)
{
@@ -69,40 +116,77 @@ int32_t mz_win32_rand(uint8_t *buf, int32_t size)
return len;
}
#endif
wchar_t *mz_win32_unicode_path_create(const char *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_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 (err == MZ_OK)
{
result = MoveFileW(source_path_wide, target_path_wide);
if (result == 0)
err = MZ_EXIST_ERROR;
}
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_os_delete(const char *path)
{
wchar_t *path_wide = NULL;
uint32_t path_wide_size = 0;
int32_t result = 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));
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;
MultiByteToWideChar(CP_UTF8, 0, path, -1, path_wide, path_wide_size);
result = DeleteFileW(path_wide);
mz_os_unicode_string_delete(&path_wide);
return path_wide;
if (result == 0)
return MZ_EXIST_ERROR;
return MZ_OK;
}
void mz_win32_unicode_path_delete(wchar_t **path)
{
if (path != NULL)
{
MZ_FREE(*path);
*path = NULL;
}
}
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;
@@ -110,20 +194,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;
@@ -136,7 +223,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;
@@ -144,7 +231,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;
@@ -152,25 +239,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;
@@ -179,32 +271,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;
@@ -215,14 +311,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;
@@ -230,28 +331,37 @@ 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;
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);
mz_os_unicode_string_delete(&path_wide);
if (err != 0 && errno != EEXIST)
return MZ_INTERNAL_ERROR;
@@ -259,7 +369,7 @@ int32_t mz_win32_make_dir(const char *path)
return MZ_OK;
}
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;
@@ -268,13 +378,19 @@ 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;
@@ -288,7 +404,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;
@@ -313,7 +429,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;
@@ -327,14 +443,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)
{
@@ -344,3 +465,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;
}
-69
View File
@@ -1,69 +0,0 @@
/* mz_os_win32.h -- System functions for Windows
Version 2.3.6, July 11, 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_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_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
+41 -5
View File
@@ -1,5 +1,5 @@
/* mz_strm.c -- Stream interface
Version 2.3.6, July 11, 2018
Version 2.7.2, November 2, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
@@ -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));
@@ -156,27 +161,50 @@ 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;
@@ -230,6 +258,14 @@ int32_t mz_stream_set_base(void *stream, void *base)
return MZ_OK;
}
void* mz_stream_get_interface(void *stream)
{
mz_stream *strm = (mz_stream *)stream;
if (strm == NULL || strm->vtbl == NULL)
return NULL;
return (void *)strm->vtbl;
}
int32_t mz_stream_get_prop_int64(void *stream, int32_t prop, int64_t *value)
{
mz_stream *strm = (mz_stream *)stream;
+7 -1
View File
@@ -1,5 +1,5 @@
/* mz_strm.h -- Stream interface
Version 2.3.6, July 11, 2018
Version 2.7.2, November 2, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
@@ -29,6 +29,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)
/***************************************************************************/
@@ -78,19 +79,24 @@ 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_close(void *stream);
int32_t mz_stream_error(void *stream);
int32_t mz_stream_set_base(void *stream, void *base);
void* mz_stream_get_interface(void *stream);
int32_t mz_stream_get_prop_int64(void *stream, int32_t prop, int64_t *value);
int32_t mz_stream_set_prop_int64(void *stream, int32_t prop, int64_t value);
-371
View File
@@ -1,371 +0,0 @@
/* mz_strm_aes.c -- Stream for WinZip AES encryption
Version 2.3.6, July 11, 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)
{
mz_os_rand(salt_value, salt_length);
}
else if (mode & MZ_OPEN_MODE_READ)
{
if (mz_stream_read(aes->stream.base, salt_value, salt_length) != salt_length)
return MZ_STREAM_ERROR;
}
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.3.6, July 11, 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
+50 -27
View File
@@ -1,5 +1,5 @@
/* mz_strm_buf.c -- Stream for buffering reads/writes
Version 2.3.6, July 11, 2018
Version 2.7.2, November 2, 2018
part of the MiniZip project
This version of ioapi is designed to buffer IO.
@@ -11,11 +11,11 @@
See the accompanying LICENSE file for the full text of the license.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>
#include "mz.h"
#include "mz_strm.h"
@@ -66,10 +66,24 @@ typedef struct mz_stream_buffered_s {
/***************************************************************************/
static int32_t mz_stream_buffered_reset(void *stream)
{
mz_stream_buffered *buffered = (mz_stream_buffered *)stream;
buffered->readbuf_len = 0;
buffered->readbuf_pos = 0;
buffered->writebuf_len = 0;
buffered->writebuf_pos = 0;
buffered->position = 0;
return MZ_OK;
}
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_reset(buffered);
return mz_stream_open(buffered->stream.base, path, mode);
}
@@ -95,7 +109,7 @@ 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;
@@ -126,8 +140,10 @@ int32_t mz_stream_buffered_read(void *stream, void *buf, int32_t size)
mz_stream_buffered_print(stream, "read [size %ld pos %lld]\n", size, buffered->position);
if (buffered->writebuf_len > 0)
{
mz_stream_buffered_print(stream, "switch from write to read, not yet supported [%lld]\n",
buffered->position);
}
while (bytes_left_to_read > 0)
{
@@ -139,7 +155,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;
@@ -157,7 +173,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;
@@ -185,6 +201,7 @@ 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",
@@ -200,8 +217,9 @@ int32_t mz_stream_buffered_write(void *stream, const void *buf, int32_t size)
mz_stream_buffered_print(stream, "switch from read to write [%lld]\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)
@@ -209,14 +227,15 @@ 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;
@@ -250,7 +269,7 @@ int64_t mz_stream_buffered_tell(void *stream)
buffered->position, buffered->readbuf_pos, buffered->writebuf_pos, errno);
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;
@@ -260,6 +279,7 @@ 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);
@@ -271,7 +291,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;
}
}
@@ -279,12 +299,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;
@@ -293,17 +314,17 @@ 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;
@@ -311,8 +332,9 @@ int32_t mz_stream_buffered_seek(void *stream, int64_t offset, int32_t origin)
//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;
@@ -343,17 +365,18 @@ int32_t mz_stream_buffered_close(void *stream)
mz_stream_buffered_print(stream, "close [flushed %d]\n", bytes_flushed);
if (buffered->readbuf_hits + buffered->readbuf_misses > 0)
mz_stream_buffered_print(stream, "read efficency %.02f%%\n",
{
mz_stream_buffered_print(stream, "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(stream, "write efficiency %.02f%%\n",
(buffered->writebuf_hits / ((float)buffered->writebuf_hits + buffered->writebuf_misses)) * 100);
}
buffered->readbuf_len = 0;
buffered->readbuf_pos = 0;
buffered->writebuf_len = 0;
buffered->writebuf_pos = 0;
mz_stream_buffered_reset(buffered);
return mz_stream_close(buffered->stream.base);
}
+1 -1
View File
@@ -1,5 +1,5 @@
/* mz_strm_buf.h -- Stream for buffering reads/writes
Version 2.3.6, July 11, 2018
Version 2.7.2, November 2, 2018
part of the MiniZip project
This version of ioapi is designed to buffer IO.
+26 -32
View File
@@ -1,5 +1,5 @@
/* mz_strm_bzip.c -- Stream for bzip inflate/deflate
Version 2.3.6, July 11, 2018
Version 2.7.2, November 2, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
@@ -10,7 +10,6 @@
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
@@ -76,7 +75,7 @@ int32_t mz_stream_bzip_open(void *stream, const char *path, int32_t mode)
if (mode & MZ_OPEN_MODE_WRITE)
{
#ifdef MZ_ZIP_DECOMPRESS_ONLY
#ifdef MZ_ZIP_NO_COMPRESSION
return MZ_SUPPORT_ERROR;
#else
bzip->bzstream.next_out = (char *)bzip->buffer;
@@ -87,7 +86,7 @@ int32_t mz_stream_bzip_open(void *stream, const char *path, int32_t mode)
}
else if (mode & MZ_OPEN_MODE_READ)
{
#ifdef MZ_ZIP_COMPRESS_ONLY
#ifdef MZ_ZIP_NO_DECOMPRESSION
return MZ_SUPPORT_ERROR;
#else
bzip->bzstream.next_in = (char *)bzip->buffer;
@@ -98,7 +97,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 +109,13 @@ 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_COMPRESS_ONLY
#ifdef MZ_ZIP_NO_DECOMPRESSION
return MZ_SUPPORT_ERROR;
#else
mz_stream_bzip *bzip = (mz_stream_bzip *)stream;
@@ -124,10 +123,10 @@ 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 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 = BZ_OK;
@@ -153,15 +152,12 @@ int32_t mz_stream_bzip_read(void *stream, void *buf, int32_t size)
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 +170,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,7 +193,7 @@ 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
@@ -207,7 +203,7 @@ 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 +219,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,7 +247,7 @@ 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));
@@ -266,7 +260,7 @@ int32_t mz_stream_bzip_write(void *stream, const void *buf, int32_t size)
mz_stream_bzip *bzip = (mz_stream_bzip *)stream;
int32_t err = size;
#ifdef MZ_ZIP_DECOMPRESS_ONLY
#ifdef MZ_ZIP_NO_COMPRESSION
MZ_UNUSED(bzip);
err = MZ_SUPPORT_ERROR;
#else
@@ -284,7 +278,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 +287,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)
@@ -302,7 +296,7 @@ int32_t mz_stream_bzip_close(void *stream)
if (bzip->mode & MZ_OPEN_MODE_WRITE)
{
#ifdef MZ_ZIP_DECOMPRESS_ONLY
#ifdef MZ_ZIP_NO_COMPRESSION
return MZ_SUPPORT_ERROR;
#else
mz_stream_bzip_compress(stream, BZ_FINISH);
@@ -313,7 +307,7 @@ int32_t mz_stream_bzip_close(void *stream)
}
else if (bzip->mode & MZ_OPEN_MODE_READ)
{
#ifdef MZ_ZIP_COMPRESS_ONLY
#ifdef MZ_ZIP_NO_DECOMPRESSION
return MZ_SUPPORT_ERROR;
#else
BZ2_bzDecompressEnd(&bzip->bzstream);
@@ -323,7 +317,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;
}
+1 -1
View File
@@ -1,5 +1,5 @@
/* mz_strm_bzip.h -- Stream for bzip inflate/deflate
Version 2.3.6, July 11, 2018
Version 2.7.2, November 2, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
+18 -20
View File
@@ -1,5 +1,5 @@
/* mz_strm_crc32.c -- Stream for CRC32 hashing
Version 2.3.6, July 11, 2018
Version 2.7.2, November 2, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
@@ -13,10 +13,6 @@
#include <stdint.h>
#include <string.h>
#ifdef HAVE_ZLIB
#include "zlib.h"
#endif
#include "mz.h"
#include "mz_strm.h"
#include "mz_strm_crc32.h"
@@ -52,8 +48,8 @@ typedef struct mz_stream_crc32_s {
int64_t value;
int64_t total_in;
int64_t total_out;
mz_stream_crc32_update update;
mz_stream_crc32_update
update;
} mz_stream_crc32;
/***************************************************************************/
@@ -74,7 +70,7 @@ 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_OPEN_ERROR;
return MZ_OK;
}
@@ -95,9 +91,12 @@ 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;
if (written > 0)
{
crc32x->value = crc32x->update(crc32x->value, buf, written);
crc32x->total_out += written;
}
return written;
}
@@ -127,10 +126,10 @@ int32_t mz_stream_crc32_error(void *stream)
return mz_stream_error(crc32->stream.base);
}
int32_t mz_stream_crc32_get_value(void *stream)
uint32_t mz_stream_crc32_get_value(void *stream)
{
mz_stream_crc32 *crc32 = (mz_stream_crc32 *)stream;
return (int32_t)crc32->value;
return (uint32_t)crc32->value;
}
int32_t mz_stream_crc32_get_prop_int64(void *stream, int32_t prop, int64_t *value)
@@ -159,17 +158,16 @@ void *mz_stream_crc32_create(void **stream)
{
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();
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();
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;
@@ -198,10 +196,10 @@ int32_t mz_stream_crc32_get_update_func(mz_stream_crc32_update *update)
if (update == NULL)
return MZ_PARAM_ERROR;
#ifdef HAVE_ZLIB
*update =
*update =
(mz_stream_crc32_update)mz_stream_zlib_get_crc32_update();
#elif defined(HAVE_LZMA)
*update =
*update =
(mz_stream_crc32_update)mz_stream_lzma_get_crc32_update();
#else
#error ZLIB or LZMA required for CRC32
+15 -15
View File
@@ -1,5 +1,5 @@
/* mz_strm_crc32.h -- Stream for CRC32 hashing
Version 2.3.6, July 11, 2018
Version 2.7.2, November 2, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
@@ -22,24 +22,24 @@ extern "C" {
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_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);
uint32_t mz_stream_crc32_get_value(void *stream);
int32_t mz_stream_crc32_get_prop_int64(void *stream, int32_t prop, int64_t *value);
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_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);
void* mz_stream_crc32_get_interface(void);
int32_t mz_stream_crc32_get_update_func(mz_stream_crc32_update *update);
/***************************************************************************/
+444
View File
@@ -0,0 +1,444 @@
/* mz_strm_libcomp.c -- Stream for apple compression
Version 2.7.2, November 2, 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 <compression.h>
#include "zlib.h"
#include "mz.h"
#include "mz_strm.h"
#include "mz_strm_libcomp.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
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 = 0;
int32_t read = 0;
int32_t err = Z_OK;
libcomp->cstream.dst_ptr = buf;
libcomp->cstream.dst_size = (size_t)size;
do
{
if (libcomp->cstream.src_size == 0)
{
bytes_to_read = sizeof(libcomp->buffer);
if (libcomp->max_total_in > 0)
{
if ((libcomp->max_total_in - libcomp->total_in) < (int64_t)sizeof(libcomp->buffer))
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)
break;
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, 0);
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 = Z_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 != Z_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;
}
/***************************************************************************/
// Define z_crc_t in zlib 1.2.5 and less
#if (ZLIB_VERNUM < 0x1270)
typedef unsigned long z_crc_t;
#endif
/***************************************************************************/
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;
}
static int64_t mz_stream_zlib_crc32(int64_t value, const void *buf, int32_t size)
{
return (int64_t)crc32((z_crc_t)value, buf, (uint32_t)size);
}
void *mz_stream_zlib_get_crc32_update(void)
{
return (void *)mz_stream_zlib_crc32;
}
+67
View File
@@ -0,0 +1,67 @@
/* mz_strm_libcomp.h -- Stream for apple compression
Version 2.7.2, November 2, 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_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
+30 -36
View File
@@ -1,5 +1,5 @@
/* mz_strm_lzma.c -- Stream for lzma inflate/deflate
Version 2.3.6, July 11, 2018
Version 2.7.2, November 2, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
@@ -10,7 +10,6 @@
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
@@ -83,7 +82,7 @@ int32_t mz_stream_lzma_open(void *stream, const char *path, int32_t mode)
if (mode & MZ_OPEN_MODE_WRITE)
{
#ifdef MZ_ZIP_DECOMPRESS_ONLY
#ifdef MZ_ZIP_NO_COMPRESSION
MZ_UNUSED(filters);
return MZ_SUPPORT_ERROR;
#else
@@ -91,7 +90,7 @@ int32_t mz_stream_lzma_open(void *stream, const char *path, int32_t mode)
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));
@@ -112,7 +111,7 @@ int32_t mz_stream_lzma_open(void *stream, const char *path, int32_t mode)
}
else if (mode & MZ_OPEN_MODE_READ)
{
#ifdef MZ_ZIP_COMPRESS_ONLY
#ifdef MZ_ZIP_NO_DECOMPRESSION
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,13 @@ 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_COMPRESS_ONLY
#ifdef MZ_ZIP_NO_DECOMPRESSION
return MZ_SUPPORT_ERROR;
#else
mz_stream_lzma *lzma = (mz_stream_lzma *)stream;
@@ -154,10 +153,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;
@@ -180,15 +179,12 @@ int32_t mz_stream_lzma_read(void *stream, void *buf, int32_t size)
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 +195,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,7 +217,7 @@ 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
@@ -231,7 +227,7 @@ 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 +244,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 +255,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 +263,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;
@@ -285,7 +279,7 @@ int32_t mz_stream_lzma_write(void *stream, const void *buf, int32_t size)
mz_stream_lzma *lzma = (mz_stream_lzma *)stream;
int32_t err = size;
#ifdef MZ_ZIP_DECOMPRESS_ONLY
#ifdef MZ_ZIP_NO_COMPRESSION
MZ_UNUSED(lzma);
err = MZ_SUPPORT_ERROR;
#else
@@ -303,7 +297,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 +306,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)
@@ -321,7 +315,7 @@ int32_t mz_stream_lzma_close(void *stream)
if (lzma->mode & MZ_OPEN_MODE_WRITE)
{
#ifdef MZ_ZIP_DECOMPRESS_ONLY
#ifdef MZ_ZIP_NO_COMPRESSION
return MZ_SUPPORT_ERROR;
#else
mz_stream_lzma_code(stream, LZMA_FINISH);
@@ -332,7 +326,7 @@ int32_t mz_stream_lzma_close(void *stream)
}
else if (lzma->mode & MZ_OPEN_MODE_READ)
{
#ifdef MZ_ZIP_COMPRESS_ONLY
#ifdef MZ_ZIP_NO_DECOMPRESSION
return MZ_SUPPORT_ERROR;
#else
lzma_end(&lzma->lstream);
@@ -342,7 +336,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;
}
@@ -439,7 +433,7 @@ void *mz_stream_lzma_get_interface(void)
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);
return (int64_t)lzma_crc32(buf, (size_t)size, (uint32_t)value);
}
void *mz_stream_lzma_get_crc32_update(void)
+1 -1
View File
@@ -1,5 +1,5 @@
/* mz_strm_lzma.h -- Stream for lzma inflate/deflate
Version 2.3.6, July 11, 2018
Version 2.7.2, November 2, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
+24 -13
View File
@@ -1,5 +1,5 @@
/* mz_strm_mem.c -- Stream for memory access
Version 2.3.6, July 11, 2018
Version 2.7.2, November 2, 2018
part of the MiniZip project
This interface is designed to access memory rather than files.
@@ -11,14 +11,13 @@
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>
@@ -48,7 +47,7 @@ static mz_stream_vtbl mz_stream_mem_vtbl = {
typedef struct mz_stream_mem_s {
mz_stream stream;
int32_t mode;
char *buffer; // Memory buffer pointer
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
@@ -61,10 +60,10 @@ static void mz_stream_mem_set_size(void *stream, int32_t size)
{
mz_stream_mem *mem = (mz_stream_mem *)stream;
int32_t new_size = size;
char *new_buf = NULL;
uint8_t *new_buf = NULL;
new_buf = (char *)MZ_ALLOC(new_size);
new_buf = (uint8_t *)MZ_ALLOC((uint32_t)new_size);
if (mem->buffer)
{
memcpy(new_buf, mem->buffer, mem->size);
@@ -97,7 +96,7 @@ 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 +107,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);
@@ -175,18 +174,18 @@ 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);
}
mem->position = (uint32_t)new_pos;
mem->position = (int32_t)new_pos;
return MZ_OK;
}
@@ -209,7 +208,7 @@ int32_t mz_stream_mem_error(void *stream)
void mz_stream_mem_set_buffer(void *stream, void *buf, int32_t size)
{
mz_stream_mem *mem = (mz_stream_mem *)stream;
mem->buffer = buf;
mem->buffer = (uint8_t *)buf;
mem->size = size;
mem->limit = size;
}
@@ -223,17 +222,29 @@ 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;
}
int32_t mz_stream_mem_get_buffer_at_current(void *stream, const void **buf)
{
mz_stream_mem *mem = (mz_stream_mem *)stream;
return mz_stream_mem_get_buffer_at(stream, mem->position, buf);
}
void mz_stream_mem_get_buffer_length(void *stream, int32_t *length)
{
mz_stream_mem *mem = (mz_stream_mem *)stream;
*length = mem->limit;
}
void mz_stream_mem_set_buffer_limit(void *stream, int32_t limit)
{
mz_stream_mem *mem = (mz_stream_mem *)stream;
mem->limit = limit;
}
void mz_stream_mem_set_grow_size(void *stream, int32_t grow_size)
{
mz_stream_mem *mem = (mz_stream_mem *)stream;
+3 -1
View File
@@ -1,5 +1,5 @@
/* mz_strm_mem.h -- Stream for memory access
Version 2.3.6, July 11, 2018
Version 2.7.2, November 2, 2018
part of MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
@@ -32,7 +32,9 @@ int32_t mz_stream_mem_error(void *stream);
void mz_stream_mem_set_buffer(void *stream, void *buf, int32_t size);
int32_t mz_stream_mem_get_buffer(void *stream, const void **buf);
int32_t mz_stream_mem_get_buffer_at(void *stream, int64_t position, const void **buf);
int32_t mz_stream_mem_get_buffer_at_current(void *stream, const void **buf);
void mz_stream_mem_get_buffer_length(void *stream, int32_t *length);
void mz_stream_mem_set_buffer_limit(void *stream, int32_t limit);
void mz_stream_mem_set_grow_size(void *stream, int32_t grow_size);
void* mz_stream_mem_create(void **stream);
+43
View File
@@ -0,0 +1,43 @@
/* mz_sstrm_os.h -- Stream for filesystem access
Version 2.7.2, November 2, 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_OS_H
#define MZ_STREAM_OS_H
#include <stdint.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
+39 -39
View File
@@ -1,5 +1,5 @@
/* mz_strm_posix.c -- Stream for filesystem access for posix/linux
Version 2.3.6, July 11, 2018
Version 2.7.2, November 2, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
@@ -8,24 +8,24 @@
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 <stdio.h>
#include <string.h>
#include <errno.h>
#include "mz.h"
#include "mz_strm.h"
#include "mz_strm_posix.h"
#include "mz_strm_os.h"
/***************************************************************************/
#if defined(MZ_USE_FILE32API)
#if defined(MZ_FILE32_API)
# define fopen64 fopen
# define ftello64 ftell
# define fseeko64 fseek
@@ -50,17 +50,17 @@
/***************************************************************************/
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 +76,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 +91,63 @@ 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;
}
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 +164,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 +188,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 +207,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 +215,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 +226,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;
}
+68 -58
View File
@@ -1,5 +1,5 @@
/* mz_strm_win32.c -- Stream for filesystem access for windows
Version 2.3.6, July 11, 2018
Version 2.7.2, November 2, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
@@ -8,7 +8,7 @@
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.
@@ -19,8 +19,9 @@
#include <windows.h>
#include "mz.h"
#include "mz_os.h"
#include "mz_strm.h"
#include "mz_strm_win32.h"
#include "mz_strm_os.h"
/***************************************************************************/
@@ -40,17 +41,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 +67,7 @@ typedef struct mz_stream_win32_s
/***************************************************************************/
int32_t mz_stream_win32_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_win32 *win32 = (mz_stream_win32 *)stream;
uint32_t desired_access = 0;
@@ -78,7 +79,10 @@ int32_t mz_stream_win32_open(void *stream, const char *path, int32_t mode)
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 +101,50 @@ 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));
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))
{
@@ -152,13 +156,13 @@ int32_t mz_stream_win32_read(void *stream, void *buf, int32_t size)
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))
{
@@ -170,16 +174,20 @@ int32_t mz_stream_win32_write(void *stream, const void *buf, int32_t size)
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 +199,32 @@ 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();
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,21 +238,22 @@ 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;
}
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;
@@ -253,13 +263,13 @@ int mz_stream_win32_close(void *stream)
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 +277,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 +285,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 +296,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;
}
+50 -34
View File
@@ -1,33 +1,33 @@
/* mz_strm_pkcrypt.c -- Code for traditional PKWARE encryption
Version 2.3.6, July 11, 2018
part of the MiniZip project
Version 2.7.2, November 2, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
Copyright (C) 1998-2005 Gilles Vollant
Modifications for Info-ZIP crypting
http://www.winimage.com/zLibDll/minizip.html
Copyright (C) 2003 Terry Thorsen
Copyright (C) 2010-2018 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
Copyright (C) 1998-2005 Gilles Vollant
Modifications for Info-ZIP crypting
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
This code is a modified version of crypting code in Info-ZIP distribution
Copyright (C) 1990-2000 Info-ZIP. All rights reserved.
Copyright (C) 1990-2000 Info-ZIP. All rights reserved.
This program is distributed under the terms of the same license as zlib.
See the accompanying LICENSE file for the full text of the license.
This program is distributed under the terms of the same license as zlib.
See the accompanying LICENSE file for the full text of the license.
This encryption code is a direct transcription of the algorithm from
Roger Schlafly, described by Phil Katz in the file appnote.txt. This
file (appnote.txt) is distributed with the PKZIP program (even in the
version without encryption capabilities).
This encryption code is a direct transcription of the algorithm from
Roger Schlafly, described by Phil Katz in the file appnote.txt. This
file (appnote.txt) is distributed with the PKZIP program (even in the
version without encryption capabilities).
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mz.h"
#include "mz_crypt.h"
#include "mz_os.h"
#include "mz_strm.h"
#include "mz_strm_crc32.h"
@@ -68,18 +68,18 @@ typedef struct mz_stream_pkcrypt_s {
uint8_t verify2;
const char *password;
mz_stream_crc32_update
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 +87,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);
@@ -122,7 +122,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;
}
}
@@ -144,12 +144,12 @@ 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;
return MZ_PARAM_ERROR;
if (mz_stream_crc32_get_update_func(&pkcrypt->crc32_update) != MZ_OK)
return MZ_PARAM_ERROR;
@@ -158,8 +158,14 @@ int32_t mz_stream_pkcrypt_open(void *stream, const char *path, int32_t mode)
if (mode & MZ_OPEN_MODE_WRITE)
{
#ifdef MZ_ZIP_NO_COMPRESSION
MZ_UNUSED(t);
MZ_UNUSED(i);
return MZ_SUPPORT_ERROR;
#else
// First generate RAND_HEAD_LEN - 2 random bytes.
mz_os_rand(header, RAND_HEAD_LEN - 2);
mz_crypt_rand(header, RAND_HEAD_LEN - 2);
// Encrypt random header (last two bytes is high word of crc)
for (i = 0; i < RAND_HEAD_LEN - 2; i++)
@@ -169,14 +175,23 @@ int32_t mz_stream_pkcrypt_open(void *stream, const char *path, int32_t mode)
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;
return MZ_WRITE_ERROR;
pkcrypt->total_out += RAND_HEAD_LEN;
#endif
}
else if (mode & MZ_OPEN_MODE_READ)
{
#ifdef MZ_ZIP_NO_DECOMPRESSION
MZ_UNUSED(t);
MZ_UNUSED(i);
MZ_UNUSED(verify1);
MZ_UNUSED(verify2);
return MZ_SUPPORT_ERROR;
#else
if (mz_stream_read(pkcrypt->stream.base, header, RAND_HEAD_LEN) != RAND_HEAD_LEN)
return MZ_STREAM_ERROR;
return MZ_READ_ERROR;
for (i = 0; i < RAND_HEAD_LEN - 2; i++)
header[i] = mz_stream_pkcrypt_decode(stream, header[i]);
@@ -190,6 +205,7 @@ int32_t mz_stream_pkcrypt_open(void *stream, const char *path, int32_t mode)
return MZ_PASSWORD_ERROR;
pkcrypt->total_in += RAND_HEAD_LEN;
#endif
}
pkcrypt->initialized = 1;
@@ -200,7 +216,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;
}
@@ -229,7 +245,7 @@ int32_t mz_stream_pkcrypt_write(void *stream, const void *buf, int32_t size)
uint16_t t = 0;
if (size > (int32_t)sizeof(pkcrypt->buffer))
return MZ_STREAM_ERROR;
return MZ_BUF_ERROR;
for (i = 0; i < size; i++)
pkcrypt->buffer[i] = mz_stream_pkcrypt_encode(stream, buf_ptr[i], t);
+1 -1
View File
@@ -1,5 +1,5 @@
/* mz_strm_pkcrypt.h -- Code for traditional PKWARE encryption
Version 2.3.6, July 11, 2018
Version 2.7.2, November 2, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
-62
View File
@@ -1,62 +0,0 @@
/* mz_strm_posix.h -- Stream for filesystem access for posix/linux
Version 2.3.6, July 11, 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_USE_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
+30 -15
View File
@@ -1,5 +1,5 @@
/* mz_strm_split.c -- Stream for split files
Version 2.3.6, July 11, 2018
Version 2.7.2, November 2, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
@@ -9,10 +9,10 @@
See the accompanying LICENSE file for the full text of the license.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include "mz.h"
#include "mz_os.h"
@@ -56,9 +56,9 @@ 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;
int32_t reached_end;
@@ -91,13 +91,15 @@ static int32_t mz_stream_split_open_disk(void *stream, int32_t number_disk)
{
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
@@ -180,13 +182,26 @@ 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;
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 (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)
{
@@ -205,9 +220,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)
@@ -284,7 +299,7 @@ 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;
bytes_left -= written;
buf_ptr += written;
+1 -1
View File
@@ -1,5 +1,5 @@
/* mz_strm_split.h -- Stream for split files
Version 2.3.6, July 11, 2018
Version 2.7.2, November 2, 2018
part of MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
-62
View File
@@ -1,62 +0,0 @@
/* mz_sstrm_win32.h -- Stream for filesystem access for windows
Version 2.3.6, July 11, 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_USE_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
+473
View File
@@ -0,0 +1,473 @@
/* mz_strm_wzaes.c -- Stream for WinZip AES encryption
Version 2.7.2, November 2, 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 <stdlib.h>
#include <string.h>
#include "mz.h"
#include "mz_crypt.h"
#include "mz_os.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_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);
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);
err = mz_crypt_hmac_end(hmac3, uu, sizeof(uu));
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;
}
/***************************************************************************/
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_stream_wzaes_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 (bytes_to_read + wzaes->total_in > max_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;
int32_t written = 0;
if (size < 0)
return MZ_PARAM_ERROR;
if (size > (int32_t)sizeof(wzaes->buffer))
return MZ_BUF_ERROR;
memcpy(wzaes->buffer, buf, size);
mz_stream_wzaes_encrypt_data(stream, (uint8_t *)wzaes->buffer, size);
mz_crypt_hmac_update(wzaes->hmac, wzaes->buffer, size);
written = mz_stream_write(wzaes->stream.base, wzaes->buffer, size);
if (written > 0)
wzaes->total_out += written;
return 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;
}
+52
View File
@@ -0,0 +1,52 @@
/* mz_strm_wzaes.h -- Stream for WinZIP AES encryption
Version 2.7.2, November 2, 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_WZAES_SHA1_H
#define MZ_STREAM_WZAES_SHA1_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/***************************************************************************/
int32_t mz_stream_wzaes_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_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
+25 -27
View File
@@ -1,5 +1,5 @@
/* mz_strm_zlib.c -- Stream for zlib inflate/deflate
Version 2.3.6, July 11, 2018
Version 2.7.2, November 2, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
@@ -10,7 +10,6 @@
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
@@ -26,7 +25,7 @@
// Define z_crc_t in zlib 1.2.5 and less or if using zlib-ng
#if defined(ZLIBNG_VERNUM)
typedef uint32_t z_crc_t;
#elif (ZLIB_VERNUM < 0x1270)
#elif (ZLIB_VERNUM < 0x1270)
typedef unsigned long z_crc_t;
#endif
@@ -93,18 +92,19 @@ int32_t mz_stream_zlib_open(void *stream, const char *path, int32_t mode)
if (mode & MZ_OPEN_MODE_WRITE)
{
#ifdef MZ_ZIP_DECOMPRESS_ONLY
#ifdef MZ_ZIP_NO_COMPRESSION
return MZ_SUPPORT_ERROR;
#else
zlib->zstream.next_out = zlib->buffer;
zlib->zstream.avail_out = sizeof(zlib->buffer);
zlib->error = deflateInit2(&zlib->zstream, (int8_t)zlib->level, Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
zlib->error = deflateInit2(&zlib->zstream, (int8_t)zlib->level, Z_DEFLATED,
-MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
#endif
}
else if (mode & MZ_OPEN_MODE_READ)
{
#ifdef MZ_ZIP_COMPRESS_ONLY
#ifdef MZ_ZIP_NO_DECOMPRESSION
return MZ_SUPPORT_ERROR;
#else
zlib->zstream.next_in = zlib->buffer;
@@ -115,7 +115,7 @@ int32_t mz_stream_zlib_open(void *stream, const char *path, int32_t mode)
}
if (zlib->error != Z_OK)
return MZ_STREAM_ERROR;
return MZ_OPEN_ERROR;
zlib->initialized = 1;
zlib->mode = mode;
@@ -126,13 +126,13 @@ 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_COMPRESS_ONLY
#ifdef MZ_ZIP_NO_DECOMPRESSION
return MZ_SUPPORT_ERROR;
#else
mz_stream_zlib *zlib = (mz_stream_zlib *)stream;
@@ -166,10 +166,8 @@ int32_t mz_stream_zlib_read(void *stream, void *buf, int32_t size)
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;
@@ -201,7 +199,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,7 +208,10 @@ 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
@@ -221,7 +221,7 @@ 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;
@@ -264,7 +262,7 @@ 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));
@@ -277,7 +275,7 @@ int32_t mz_stream_zlib_write(void *stream, const void *buf, int32_t size)
mz_stream_zlib *zlib = (mz_stream_zlib *)stream;
int32_t err = size;
#ifdef MZ_ZIP_DECOMPRESS_ONLY
#ifdef MZ_ZIP_NO_COMPRESSION
MZ_UNUSED(zlib);
err = MZ_SUPPORT_ERROR;
#else
@@ -295,7 +293,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 +302,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)
@@ -314,7 +312,7 @@ int32_t mz_stream_zlib_close(void *stream)
if (zlib->mode & MZ_OPEN_MODE_WRITE)
{
#ifdef MZ_ZIP_DECOMPRESS_ONLY
#ifdef MZ_ZIP_NO_COMPRESSION
return MZ_SUPPORT_ERROR;
#else
mz_stream_zlib_deflate(stream, Z_FINISH);
@@ -325,7 +323,7 @@ int32_t mz_stream_zlib_close(void *stream)
}
else if (zlib->mode & MZ_OPEN_MODE_READ)
{
#ifdef MZ_ZIP_COMPRESS_ONLY
#ifdef MZ_ZIP_NO_DECOMPRESSION
return MZ_SUPPORT_ERROR;
#else
inflateEnd(&zlib->zstream);
@@ -335,7 +333,7 @@ int32_t mz_stream_zlib_close(void *stream)
zlib->initialized = 0;
if (zlib->error != Z_OK)
return MZ_STREAM_ERROR;
return MZ_CLOSE_ERROR;
return MZ_OK;
}
+1 -1
View File
@@ -1,5 +1,5 @@
/* mz_strm_zlib.h -- Stream for zlib inflate/deflate
Version 2.3.6, July 11, 2018
Version 2.7.2, November 2, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
+822 -298
View File
File diff suppressed because it is too large Load Diff
+100 -36
View File
@@ -1,5 +1,5 @@
/* mz_zip.h -- Zip manipulation
Version 2.3.6, July 11, 2018
Version 2.7.2, November 2, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
@@ -8,7 +8,7 @@
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.
@@ -38,102 +38,161 @@ typedef struct mz_zip_file_s
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
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
uint64_t disk_offset; // relative offset of local header
int64_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
const char *filename; // filename string
const uint8_t *extrafield; // extrafield data
const char *comment; // comment string
#ifdef HAVE_AES
uint16_t aes_version; // winzip aes extension if not 0
uint8_t aes_encryption_mode; // winzip aes encryption mode
#endif
} mz_zip_file;
const char *filename; // filename utf8 null-terminated string
const uint8_t *extrafield; // extrafield data
const char *comment; // comment utf8 null-terminated string
} mz_zip_file, mz_zip_entry;
/***************************************************************************/
extern void * mz_zip_open(void *stream, int32_t mode);
typedef int32_t (*mz_zip_locate_entry_cb)(void *handle, void *userdata, mz_zip_file *file_info);
/***************************************************************************/
void * mz_zip_create(void **handle);
// Create zip instance for opening
void mz_zip_delete(void **handle);
// 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
extern int32_t mz_zip_close(void *handle);
int32_t mz_zip_close(void *handle);
// Close the zip file
extern int32_t mz_zip_get_comment(void *handle, const char **comment);
int32_t mz_zip_get_comment(void *handle, const char **comment);
// Get a pointer to the global comment
extern int32_t mz_zip_set_comment(void *handle, const char *comment);
int32_t mz_zip_set_comment(void *handle, const char *comment);
// Set the global comment used for writing zip file
extern int32_t mz_zip_get_version_madeby(void *handle, uint16_t *version_madeby);
int32_t mz_zip_get_version_madeby(void *handle, uint16_t *version_madeby);
// Get the version made by
extern int32_t mz_zip_set_version_madeby(void *handle, uint16_t version_madeby);
int32_t mz_zip_set_version_madeby(void *handle, uint16_t version_madeby);
// Set the version made by used for writing zip file
extern int32_t mz_zip_entry_write_open(void *handle, const mz_zip_file *file_info,
int16_t compress_level, const char *password);
int32_t mz_zip_get_stream(void *handle, void **stream);
// 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_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
extern int32_t mz_zip_entry_write(void *handle, const void *buf, uint32_t len);
int32_t mz_zip_entry_write(void *handle, const void *buf, int32_t len);
// Write bytes from the current file in the zip file
extern int32_t mz_zip_entry_read_open(void *handle, int16_t raw, const char *password);
int32_t mz_zip_entry_is_open(void *handle);
// Check to see if entry is open for read/write
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
extern int32_t mz_zip_entry_read(void *handle, void *buf, uint32_t len);
int32_t mz_zip_entry_read(void *handle, void *buf, int32_t len);
// Read bytes from the current file in the zip file
extern int32_t mz_zip_entry_get_info(void *handle, mz_zip_file **file_info);
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
extern int32_t mz_zip_entry_get_local_info(void *handle, mz_zip_file **local_file_info);
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
extern int32_t mz_zip_entry_close_raw(void *handle, uint64_t uncompressed_size, uint32_t crc32);
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
extern int32_t mz_zip_entry_close(void *handle);
int32_t mz_zip_entry_close(void *handle);
// Close the current file in the zip file
/***************************************************************************/
extern int32_t mz_zip_get_number_entry(void *handle, int64_t *number_entry);
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
extern int32_t mz_zip_get_disk_number_with_cd(void *handle, uint32_t *disk_number_with_cd);
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
extern int64_t mz_zip_get_entry(void *handle);
int64_t mz_zip_get_entry(void *handle);
// Return offset of the current entry in the zip file
extern int32_t mz_zip_goto_entry(void *handle, uint64_t cd_pos);
int32_t mz_zip_goto_entry(void *handle, int64_t cd_pos);
// Go to specified entry in the zip file
extern int32_t mz_zip_goto_first_entry(void *handle);
int32_t mz_zip_goto_first_entry(void *handle);
// Go to the first entry in the zip file
extern int32_t mz_zip_goto_next_entry(void *handle);
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
typedef int32_t (*mz_filename_compare_cb)(void *handle, const char *filename1, const char *filename2);
extern int32_t mz_zip_locate_entry(void *handle, const char *filename,
mz_filename_compare_cb filename_compare_cb);
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
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
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
/***************************************************************************/
int32_t mz_zip_attrib_is_dir(int32_t attributes, int32_t version_madeby);
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, uint32_t src_attrib, uint8_t target_sys, uint32_t *target_attrib);
// Converts file attributes from one host system to another
int32_t mz_zip_attrib_posix_to_win32(uint32_t posix_attrib, uint32_t *win32_attrib);
// Converts posix file attributes to win32 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_read(void *stream, uint16_t *type, uint16_t *length);
// Reads and extrafield header from a stream
int32_t mz_zip_extrafield_write(void *stream, uint32_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
@@ -157,6 +216,11 @@ int32_t mz_zip_unix_to_ntfs_time(time_t unix_time, uint64_t *ntfs_time);
/***************************************************************************/
int32_t mz_zip_path_compare(const char *path1, const char *path2, uint8_t ignore_case);
// Compare two paths without regard to slashes
/***************************************************************************/
#ifdef __cplusplus
}
#endif
+1932
View File
File diff suppressed because it is too large Load Diff
+281
View File
@@ -0,0 +1,281 @@
/* mz_zip_rw.h -- Zip reader/writer
Version 2.7.2, November 2, 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_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, 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
int32_t mz_zip_reader_open(void *handle, void *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
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
int32_t mz_zip_reader_open_buffer(void *handle, uint8_t *buf, int32_t len, uint8_t copy);
// Opens zip file from memory buffer
int32_t mz_zip_reader_close(void *handle);
// 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
int32_t mz_zip_reader_goto_next_entry(void *handle);
// 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
int32_t mz_zip_reader_entry_open(void *handle);
// Opens an entry for reading
int32_t mz_zip_reader_entry_close(void *handle);
// Closes an entry
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
int32_t mz_zip_reader_entry_is_dir(void *handle);
// 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
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
int32_t mz_zip_reader_entry_save_file(void *handle, const char *path);
// 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
int32_t mz_zip_reader_entry_save_buffer_length(void *handle);
// 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
/***************************************************************************/
void mz_zip_reader_set_pattern(void *handle, const char *pattern, uint8_t ignore_case);
// Sets the match pattern for entries in the zip file, if null all entries are matched
void mz_zip_reader_set_password(void *handle, const char *password);
// 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
int32_t mz_zip_reader_get_raw(void *handle, uint8_t *raw);
// Gets whether or not it should save the entry raw
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
void mz_zip_reader_set_password_cb(void *handle, void *userdata, mz_zip_reader_password_cb cb);
// Callback for when a password is required and hasn't been set
void mz_zip_reader_set_progress_cb(void *handle, void *userdata, mz_zip_reader_progress_cb cb);
// 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
int32_t mz_zip_reader_get_zip_handle(void *handle, void **zip_handle);
// Gets the underlying zip instance handle
void* mz_zip_reader_create(void **handle);
// Create new instance of zip reader
void mz_zip_reader_delete(void **handle);
// Delete instance of zip reader
/***************************************************************************/
typedef int32_t (*mz_zip_writer_overwrite_cb)(void *handle, void *userdata, const char *path);
typedef int32_t (*mz_zip_writer_password_cb)(void *handle, void *userdata, mz_zip_file *file_info, char *password, int32_t max_password);
typedef int32_t (*mz_zip_writer_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
int32_t mz_zip_writer_open(void *handle, void *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
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
int32_t mz_zip_writer_close(void *handle);
// 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
int32_t mz_zip_writer_entry_close(void *handle);
// 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
int32_t mz_zip_writer_entry_sign(void *handle, uint8_t *message, int32_t message_size,
const char *cert_path, 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
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
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
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
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
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
/***************************************************************************/
void mz_zip_writer_set_password(void *handle, const char *password);
// Password to use for encrypting files in the zip
void mz_zip_writer_set_raw(void *handle, uint8_t raw);
// Sets whether or not we should write the entry raw
int32_t mz_zip_writer_get_raw(void *handle, uint8_t *raw);
// Gets whether or not we should write the entry raw
void mz_zip_writer_set_aes(void *handle, uint8_t aes);
// Use aes encryption 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
void mz_zip_writer_set_zip_cd(void *handle, uint8_t flags);
// Sets additional flags to be set when adding files in zip
void 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
void mz_zip_writer_set_password_cb(void *handle, void *userdata, mz_zip_writer_password_cb cb);
// Callback for ask if a password is required for adding
void mz_zip_writer_set_progress_cb(void *handle, void *userdata, mz_zip_writer_progress_cb cb);
// 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
int32_t mz_zip_writer_get_zip_handle(void *handle, void **zip_handle);
// Gets the underlying zip handle
void* mz_zip_writer_create(void **handle);
// Create new instance of zip writer
void mz_zip_writer_delete(void **handle);
// Delete instance of zip writer
/***************************************************************************/
#ifdef __cplusplus
}
#endif
#endif
+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-----
+32
View File
@@ -0,0 +1,32 @@
-----BEGIN CERTIFICATE-----
MIIFjjCCA3YCAQEwDQYJKoZIhvcNAQEFBQAwgYwxCzAJBgNVBAYTAlVTMQswCQYD
VQQIDAJBWjEQMA4GA1UEBwwHUGhvZW5peDEaMBgGA1UECgwRQmlsbGlvbiBEb2xs
YXIgQ28xFDASBgNVBAsMC0VuZ2luZWVyaW5nMREwDwYDVQQDDAhiLmRvbGxhcjEZ
MBcGCSqGSIb3DQEJARYKYkBiLmRvbGxhcjAeFw0xODEwMjgyMjIyMTFaFw0yMDEw
MjcyMjIyMTFaMIGMMQswCQYDVQQGEwJVUzELMAkGA1UECAwCQVoxEDAOBgNVBAcM
B1Bob2VuaXgxGjAYBgNVBAoMEU1pbGxpb24gRG9sbGFyIENvMRQwEgYDVQQLDAtF
bmdpbmVlcmluZzERMA8GA1UEAwwIbS5kb2xsYXIxGTAXBgkqhkiG9w0BCQEWCm1A
bS5kb2xsYXIwggIiMA0GCSqGSIb3DQEBAQUAA4ICDwAwggIKAoICAQD5oyN3ZFxh
3s6sl9bXQBLDmBT3N5CBlrhvGTllCasYLJT++5XDGQYcJT+FohXc38D4ChWoZVTu
yKaVdXlNiaP6qzRd6yfoyzm3LMwAA6ocUFOKtx9kSzOdpbabCHCfytwzEn4LHT/k
K71FDP1jP2lyWqjEX+oPEaW1QPw72zBIaqQ5LUlJD+vgZKzChQuWqWQuAqPNCdWf
6UXq5NMU6AgaGxSieAHv2gUmQD3TZ+H+KzMqHncg4wdCgUk+WR3spdOulitGmmb8
2bKZJHZ6+JKaWOtgwRPY9Y3KKPW1sO1jLd+j827uAxOE/OwbYH4FVNZCLGocT0N9
hljmxzlua/jW8kee6ADl7kyXduDDgr/L9+mWr/8Bn8Gyhy000yNG2wQnP1KuJjJK
MvgbHzeNgU1RpG66N8A7JOR+rOZVd19/CZudxueVUxiUamvOdyvhyYfcb8av0Wju
DhafNxljmxt0MdpQfcEKX2WK/IdyoAOkzggQ2ygHss0n1ysgcFyjxbhTRoSQxtlT
+Ld4euoJx7F4PizLRk+Y4W74WlgU8rSQWoBI46PBJcBMWi4oKCEmh52SHTu44IpK
0D6V1H/2B0V9zu8y0KoLdFR8N3rSp5pYvki/khVdHdl0WmJij96gygupbfASXBJv
im9UgVtGTDdJ89C9Op+QSBO/MUj+GTK4cwIDAQABMA0GCSqGSIb3DQEBBQUAA4IC
AQCJw15BXRy2CTrTUyLtB/kfhQqaCo21HbJWxWb/sNXivoEH44KJmdOVuDEv9GJ6
q4wcOunhuuodGDLJS8rEF0f6UpewoZjg0gTafD0/iU/z+ydSyoDekl3XmEXPOTyP
VnEHCjnAJxS+eBE6NbAs/DOv3wZJijRp500KPKPyyE9lbjwkF5NepI3kpNsDpIy+
+ktV/btQn50rA3tYgusPOsb+DUis9eT+p9qz9O/vfGvBHnxejN7GcQTr62aAnoGn
0MhOSgP93/LIeerswLqBBmBJ24t2YvZrQ9jCA2j8qAwbrhnj2CFABL+fvfilvPo9
HeKRiU5RKUtXguhmTjTJupwa64lveoZEMeCpQZXbf7MF3rCzZ7vTh270PSbxXAET
JHL0qQQ1K8QzlTbTCGxx05SdyP5xBVjbM0+H5IQjdF8BausU3FZ1E936kJsO241V
FLUTLWdf8VwFjdwkJhKs5FWvK+5zkrY1GbbAQr6IEEVaH4XSwVnZwlOWfAcTfG5M
0F2J3WwBVxeuApIGwi9Tjjmz23Wt222PzLJgbs+yPcOx3rxOQr+YSabfES1G5oJU
j1hCH2PXJzF0TtFhDeXG3v/cyJvPPrOIq/b0Je/hnImrrfm4AZVBoh5kspX0aTT8
8Wi2qiCQIWLVn8G5isCd1tPgG2zvUWfIZ2m82RR2LNJN9g==
-----END CERTIFICATE-----
+29
View File
@@ -0,0 +1,29 @@
-----BEGIN CERTIFICATE REQUEST-----
MIIE5zCCAs8CAQAwgYwxCzAJBgNVBAYTAlVTMQswCQYDVQQIDAJBWjEQMA4GA1UE
BwwHUGhvZW5peDEaMBgGA1UECgwRTWlsbGlvbiBEb2xsYXIgQ28xFDASBgNVBAsM
C0VuZ2luZWVyaW5nMREwDwYDVQQDDAhtLmRvbGxhcjEZMBcGCSqGSIb3DQEJARYK
bUBtLmRvbGxhcjCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAPmjI3dk
XGHezqyX1tdAEsOYFPc3kIGWuG8ZOWUJqxgslP77lcMZBhwlP4WiFdzfwPgKFahl
VO7IppV1eU2Jo/qrNF3rJ+jLObcszAADqhxQU4q3H2RLM52ltpsIcJ/K3DMSfgsd
P+QrvUUM/WM/aXJaqMRf6g8RpbVA/DvbMEhqpDktSUkP6+BkrMKFC5apZC4Co80J
1Z/pRerk0xToCBobFKJ4Ae/aBSZAPdNn4f4rMyoedyDjB0KBST5ZHeyl066WK0aa
ZvzZspkkdnr4kppY62DBE9j1jcoo9bWw7WMt36Pzbu4DE4T87BtgfgVU1kIsahxP
Q32GWObHOW5r+NbyR57oAOXuTJd24MOCv8v36Zav/wGfwbKHLTTTI0bbBCc/Uq4m
Mkoy+BsfN42BTVGkbro3wDsk5H6s5lV3X38Jm53G55VTGJRqa853K+HJh9xvxq/R
aO4OFp83GWObG3Qx2lB9wQpfZYr8h3KgA6TOCBDbKAeyzSfXKyBwXKPFuFNGhJDG
2VP4t3h66gnHsXg+LMtGT5jhbvhaWBTytJBagEjjo8ElwExaLigoISaHnZIdO7jg
ikrQPpXUf/YHRX3O7zLQqgt0VHw3etKnmli+SL+SFV0d2XRaYmKP3qDKC6lt8BJc
Em+Kb1SBW0ZMN0nz0L06n5BIE78xSP4ZMrhzAgMBAAGgFTATBgkqhkiG9w0BCQcx
BgwEdGVzdDANBgkqhkiG9w0BAQUFAAOCAgEA81xopZKKmVAuMWcL6tkXoYLUm+F/
Y+cSLs/wdfXYGcRVRxKB40JcdfrxGJnHWXu8uPGc7eMGMGHqO6T/XpemSkaNhLZW
CUSHGGdsGtU9wINXTtRoXjbDHcDQg7Z82RgFKykl4mp4ERFVYVfBda8ktoIw5aSw
les6BVXNuyf586UyWjw/JE9iagTus8EA8TGHpIIeNPArs18akPhRF/VOH1qBScsj
8fsWkPNvK92XhxEFKggkvOogc3g8Tv/o8BjG3+3rLTgKXUhxCJl9GLMz9pxwlLtb
GKUX1x+SGgdjP6W/mWoMC/grBZWK3KbdUswCl9BeSs61PqMHbfWAteoGCK4Udydn
Akr+41swcHsSSwcGOe7ye1l5d6Eij9jzaUnDXfYOTBCrDEWSN47ja/4iW4h3jIAv
NYp+IvezoizrTPCDert4IV9F0H2KxZK4a5wRleNPTwyR9KUvLsHhSf1MhkXH9LNF
soc2rJe+HoQq9CPIlojiB9ONrcdSJUu9AOl4aJUK70HyWghzp+JIZxqzbriSZdZs
Ss8BE+Jc4UCjpoV4xC5weIWNS2+BRjWb//8yh/80ReEATFi5wERusyYGqjhW1sfE
6+I0g/exQgD8vJige69H35eEQE3mmId1BtuprENV9n1rCOAWupY63N9i8Ksx34td
Ogc2PZRkrx4uxtU=
-----END CERTIFICATE REQUEST-----
+51
View File
@@ -0,0 +1,51 @@
-----BEGIN RSA PRIVATE KEY-----
MIIJKQIBAAKCAgEA+aMjd2RcYd7OrJfW10ASw5gU9zeQgZa4bxk5ZQmrGCyU/vuV
wxkGHCU/haIV3N/A+AoVqGVU7simlXV5TYmj+qs0Xesn6Ms5tyzMAAOqHFBTircf
ZEsznaW2mwhwn8rcMxJ+Cx0/5Cu9RQz9Yz9pclqoxF/qDxGltUD8O9swSGqkOS1J
SQ/r4GSswoULlqlkLgKjzQnVn+lF6uTTFOgIGhsUongB79oFJkA902fh/iszKh53
IOMHQoFJPlkd7KXTrpYrRppm/NmymSR2eviSmljrYMET2PWNyij1tbDtYy3fo/Nu
7gMThPzsG2B+BVTWQixqHE9DfYZY5sc5bmv41vJHnugA5e5Ml3bgw4K/y/fplq//
AZ/BsoctNNMjRtsEJz9SriYySjL4Gx83jYFNUaRuujfAOyTkfqzmVXdffwmbncbn
lVMYlGprzncr4cmH3G/Gr9Fo7g4WnzcZY5sbdDHaUH3BCl9livyHcqADpM4IENso
B7LNJ9crIHBco8W4U0aEkMbZU/i3eHrqCcexeD4sy0ZPmOFu+FpYFPK0kFqASOOj
wSXATFouKCghJoedkh07uOCKStA+ldR/9gdFfc7vMtCqC3RUfDd60qeaWL5Iv5IV
XR3ZdFpiYo/eoMoLqW3wElwSb4pvVIFbRkw3SfPQvTqfkEgTvzFI/hkyuHMCAwEA
AQKCAgBGRTXmlcmrgxyKWPORA+hERrsgG/QBhnvESkVtu5r94w7N9+PgV1rw8UtA
QrAEzLd1GuGd1WhjDvhVegv7V5NhLHBBP8CsNwi4F1QVbPKlpbbehU3A3apZ5hvQ
6D9Lu77gYYQ5fLJPeEB+/3kURvVXy01WeelY9I7Axto5vymIOPyhW64VcvXi5HP9
DZaFgolAql2RigMoUHHt/Rut9uLQ9m6IjV9+NNDeQ5cju0pSUbbFVG4UQ9P3VI1Z
j5xs0QEVVBA6f5wd8OnlUe5ID1C1QfJ/+dmsaPLQxVblHji0gmVBsXS4awVDmi6c
Rj0zIv5tAjOcZHOLEhZUYCA6hQo2aQpFb9ud6JRU/TOxQCGwDs/YsJRrHtNJvK+Q
z4rAw0wML4m40LzToECxjEt64xpUHv3EJgiXQM9ifLeXfWNQnpwL9Q81mQ8HxknM
DF+Sl28GV/oUprYGhRKa/uWbKppvO14boPTJ2HhtWtArQNpvknfxPGuYZJsL+WIz
xSPJTNu7HxGpoLhEER2dpiS0TAL01oEXmaKMy9i9XmPji3XXc3dqLyWeYehumt23
uojHMBpnKzEvstUlVdyJ70xqZ+U52xqRpHmT5M98VYZ2tKLCQgR1nyVUBpYUQrYd
/0QfH7S6LM/n1qosOBc7d8zikCCXzzvuAqRWzwU4P47gFEApAQKCAQEA/Ouad6Zy
wOLePZGC6Z1geZzG3Ky7YO3hqHtkq8NcAksCkC3W/7+tmCk1yUokgCLGjv6yr1Rv
DLvXP9dlqoM7EqbD5F3dyxsVfdVAiunRK1jqwCSchO0j+rpbLo5vCvFYK2MOWZ0H
b6H44iTdeW8IqUtvRz0YjqE/ZrNjWK/f2gvrY2cXJObVIX9HHWYSIbq7OWYiyN/F
7ejaODtPrGx/TKpaksW97EkWeb9gvzHq6t3QFLInK08ZpvNtJPuwKqhC+Gwr9gub
VQrnEeSJ4vYoarBmvYX64eX7Mgjay5l/btLbwWPiRvUd3HVbHAnmGPgB9xNThKZN
VHHQI3kFsRZo+wKCAQEA/K1NH73zOuz6CQI6xbi7MexYz88dWNOyc8TxP6MwJZb1
Oymb8fhvWepnCuwzSweVnKCcPWuc5McjJoj+m2uAjVERmVXWDnnSHIZQVchoOOpe
KUykDQ6W2taaYcpQ7KxN5sCnLXXKW2qtlvpd2ebBQ/BQCE4dUhV5lctC39cx0PXJ
biZ1KLFf2THFaCPn39AX62HiEZyVsYEa7QempLxeopx1OACu+Inak6lb1Uch9KaR
VMyxZwJ2D0pGDSAAwDMTT+aKh95ELshYdpbH1kW7B1iEvfuwqtNZur7EFYDg6ifK
xCo1Uew15P3r3Yq80/PnL0+L7Faa1CjTrZMjqLfE6QKCAQBFzSQhV2+eKj6ZkVom
7Ucd6RW8+3LvSleX7w4WlWEERP1dGzmV9amML/tnJS4zquVPu51G5C+NvR2nweOO
Do+0ExBHK1y13DCvPLaGYAdZRj3rb28/BE+Et//INokQ5RCnULw6IStXNMWCbBgv
lmQ2jNoWOBdZ7ciYqHOHm2pVPCrnkSEDDy3NA9p1k6D3bBiVu8ln1NJEWZo4IK5Z
cMwlNXKvxSEoX/dy/E1QFw+1aCgwPgavXjI/MDl0Fn5FF5D8DKaJTiS9cLfa6U7g
s3zuDOcd8ScPRzBvW6A+xosz47/AfLeLS8TE/iJ594VoaLHqJYxMNLVs08hUsebP
O9cXAoIBAQDCnQQy942YjB+EwsfbtA3SRNSeskpLOTpQOpcJybnyqdOVztWCyDOT
kJvu6MAVAPF14qqUEAeWmd5z9r4LnQA1WARzEo69NYzkVXsYpzPG6pS9wH72Y49D
XFkqCrbLo2aY3TemJvxF7uNmzs1sYHPozPYHf4rLxyiPfC+00j1XUYfkpBBG2F8X
nWMLc+RLGS7jJ9AEepM9VVAU0njClydYnaMXEEezvBGy+5V0rsXzdjunkcTtrxKZ
cj6EK2sIng440ztNrJ7dXYftRvMCEVEdNkt/PJT07bZFxFqoqUwSbbWZ41yQDZvx
FLYPO6V2pnwgj2cZEBdOY2euYA7uCyTJAoIBAQCVScOMkTj0WZ2QYKROOyAWY/jm
C4fuiActBwMowLYyTczVaLg5Ni5BByFwzyqh51NPHwD4NtfOPb8sJ3ZaayW02kxT
O3byjNOnPLwSF8dn2NQcdhx+uWXAcyEAmHqQwP1D0kquZ8o3k5YYVHK6x59N2/hF
xRXwftW+5pc4g+kiXsYhXxWtorFK54loYnerTN6ktkuOiqlDk1hIYf1n2U00g1kF
qzeUlfjWA6Ck9mFiW0q3UeAIutCDSsqJgHJ+ha/cN2tx4Bd6TCf1EmXzwVb6Uo3+
JzhLeT3p1YzoIhx4Vf50SsjLm9bmKHuFV+Br4Y/e0/9P0aOFZuP5MKSj0msM
-----END RSA PRIVATE KEY-----
BIN
View File
Binary file not shown.
+223 -33
View File
@@ -6,17 +6,20 @@
#include <fcntl.h>
#include "mz.h"
#include "mz_crypt.h"
#include "mz_os.h"
#include "mz_strm.h"
#include "mz_strm_mem.h"
#include "mz_strm_crc32.h"
#ifdef HAVE_BZIP
#include "mz_strm_bzip.h"
#endif
#ifdef HAVE_PKCRYPT
#include "mz_strm_pkcrypt.h"
#endif
#include "mz_strm_mem.h"
#include "mz_strm_os.h"
#ifdef HAVE_AES
#include "mz_strm_aes.h"
#include "mz_strm_wzaes.h"
#endif
#ifdef HAVE_ZLIB
#include "mz_strm_zlib.h"
@@ -73,6 +76,7 @@ void test_path_resolve(void)
mz_path_resolve("c:\\test\\123\\..\\..\\..\\abc.txt", output, sizeof(output));
ok = (strcmp(output, "abc.txt") == 0);
memset(output, 'z', sizeof(output));
printf("ok = %d", ok);
}
void test_encrypt(char *method, mz_stream_create_cb crypt_create, char *password)
@@ -83,7 +87,11 @@ void test_encrypt(char *method, mz_stream_create_cb crypt_create, char *password
void *out_stream = NULL;
void *in_stream = NULL;
void *crypt_out_stream = NULL;
char filename[120];
char encrypt_path[120];
char decrypt_path[120];
snprintf(encrypt_path, sizeof(encrypt_path), "LICENSE.encrypt.%s", method);
snprintf(decrypt_path, sizeof(decrypt_path), "LICENSE.decrypt.%s", method);
mz_stream_os_create(&in_stream);
@@ -96,8 +104,7 @@ void test_encrypt(char *method, mz_stream_create_cb crypt_create, char *password
mz_stream_os_delete(&in_stream);
mz_stream_os_create(&out_stream);
snprintf(filename, sizeof(filename), "LICENSE.encrypt.%s", method);
if (mz_stream_os_open(out_stream, filename, MZ_OPEN_MODE_CREATE | MZ_OPEN_MODE_WRITE) == MZ_OK)
if (mz_stream_os_open(out_stream, encrypt_path, MZ_OPEN_MODE_CREATE | MZ_OPEN_MODE_WRITE) == MZ_OK)
{
crypt_create(&crypt_out_stream);
@@ -113,13 +120,13 @@ void test_encrypt(char *method, mz_stream_create_cb crypt_create, char *password
mz_stream_os_close(out_stream);
printf("%s encrypted %d\n", filename, written);
printf("%s encrypted %d\n", encrypt_path, written);
}
mz_stream_os_delete(&out_stream);
mz_stream_os_create(&in_stream);
if (mz_stream_os_open(in_stream, filename, MZ_OPEN_MODE_READ) == MZ_OK)
if (mz_stream_os_open(in_stream, encrypt_path, MZ_OPEN_MODE_READ) == MZ_OK)
{
crypt_create(&crypt_out_stream);
@@ -135,14 +142,14 @@ void test_encrypt(char *method, mz_stream_create_cb crypt_create, char *password
mz_stream_os_close(in_stream);
printf("%s decrypted %d\n", filename, read);
printf("%s decrypted %d\n", decrypt_path, read);
}
mz_stream_os_delete(&in_stream);
mz_stream_os_create(&out_stream);
snprintf(filename, sizeof(filename), "LICENSE.decrypt.%s", method);
if (mz_stream_os_open(out_stream, filename, MZ_OPEN_MODE_CREATE | MZ_OPEN_MODE_WRITE) == MZ_OK)
if (mz_stream_os_open(out_stream, decrypt_path, MZ_OPEN_MODE_CREATE | MZ_OPEN_MODE_WRITE) == MZ_OK)
{
mz_stream_os_write(out_stream, buf, read);
mz_stream_os_close(out_stream);
@@ -155,8 +162,8 @@ void test_compress(char *method, mz_stream_create_cb create_compress)
{
char buf[UINT16_MAX];
int16_t read = 0;
uint64_t total_in = 0;
uint64_t total_out = 0;
int64_t total_in = 0;
int64_t total_out = 0;
void *crc_in_stream = NULL;
void *in_stream = NULL;
void *out_stream = NULL;
@@ -264,34 +271,60 @@ void test_compress(char *method, mz_stream_create_cb create_compress)
/***************************************************************************/
#ifdef HAVE_AES
void test_aes()
#ifdef HAVE_BZIP
void test_stream_bzip(void)
{
test_encrypt("aes", mz_stream_aes_create, "hello");
test_compress("bzip", mz_stream_bzip_create);
}
#endif
#ifdef HAVE_PKCRYPT
void test_crypt()
void test_stream_pkcrypt(void)
{
test_encrypt("pkcrypt", mz_stream_pkcrypt_create, "hello");
}
#endif
#ifdef HAVE_ZLIB
void test_zlib()
#ifdef HAVE_AES
void test_stream_wzaes(void)
{
test_compress("zlib", mz_stream_zlib_create);
int32_t iteration_count = 1000;
int32_t err = MZ_OK;
int32_t i = 0;
uint8_t key[MZ_HASH_SHA1_SIZE];
const char *password = "passwordpasswordpasswordpassword";
const char *salt = "8F3472E4EA57F56E36F30246DC22C173";
printf("Pbkdf2 password - %s\n", password);
printf("Pbkdf2 salt - %s\n", salt);
err = mz_stream_wzaes_pbkdf2((uint8_t *)password, (int32_t)strlen(password),
(uint8_t *)salt, (int32_t)strlen(salt), iteration_count, key, sizeof(key));
if (err == MZ_OK)
{
printf("Pbkdf2 key hex\n");
for (i = 0; i < (int32_t)sizeof(key); i += 1)
printf("%02x", key[i]);
printf("\n");
}
else
{
printf("Pbkdf2 failed - %d", err);
}
test_encrypt("aes", mz_stream_wzaes_create, "hello");
}
#endif
#ifdef HAVE_BZIP
void test_bzip()
#ifdef HAVE_ZLIB
void test_stream_zlib(void)
{
test_compress("bzip", mz_stream_bzip_create);
test_compress("zlib", mz_stream_zlib_create);
}
#endif
/***************************************************************************/
void test_zip_mem()
void test_stream_mem(void)
{
mz_zip_file file_info = { 0 };
void *read_mem_stream = NULL;
@@ -303,7 +336,7 @@ void test_zip_mem()
int32_t text_size = 0;
int32_t buffer_size = 0;
int32_t err = MZ_OK;
char *buffer_ptr = NULL;
const uint8_t *buffer_ptr = NULL;
char *password = "1234";
char *text_name = "test";
char *text_ptr = "test string";
@@ -317,9 +350,10 @@ void test_zip_mem()
mz_stream_mem_set_grow_size(write_mem_stream, 128 * 1024);
mz_stream_open(write_mem_stream, NULL, MZ_OPEN_MODE_CREATE);
zip_handle = mz_zip_open(write_mem_stream, MZ_OPEN_MODE_READWRITE);
mz_zip_create(&zip_handle);
err = mz_zip_open(zip_handle, write_mem_stream, MZ_OPEN_MODE_READWRITE);
if (zip_handle != NULL)
if (err == MZ_OK)
{
file_info.version_madeby = MZ_VERSION_MADEBY;
file_info.compression_method = MZ_COMPRESS_METHOD_DEFLATE;
@@ -329,7 +363,7 @@ void test_zip_mem()
file_info.aes_version = MZ_AES_VERSION;
#endif
err = mz_zip_entry_write_open(zip_handle, &file_info, MZ_COMPRESS_LEVEL_DEFAULT, password);
err = mz_zip_entry_write_open(zip_handle, &file_info, MZ_COMPRESS_LEVEL_DEFAULT, 0, password);
if (err == MZ_OK)
{
written = mz_zip_entry_write(zip_handle, text_ptr, text_size);
@@ -345,7 +379,9 @@ void test_zip_mem()
err = MZ_INTERNAL_ERROR;
}
mz_stream_mem_get_buffer(write_mem_stream, (void **)&buffer_ptr);
mz_zip_delete(&zip_handle);
mz_stream_mem_get_buffer(write_mem_stream, (const void **)&buffer_ptr);
mz_stream_mem_seek(write_mem_stream, 0, MZ_SEEK_END);
buffer_size = (int32_t)mz_stream_mem_tell(write_mem_stream);
@@ -363,12 +399,13 @@ void test_zip_mem()
{
// Read from a memory stream
mz_stream_mem_create(&read_mem_stream);
mz_stream_mem_set_buffer(read_mem_stream, buffer_ptr, buffer_size);
mz_stream_mem_set_buffer(read_mem_stream, (void *)buffer_ptr, buffer_size);
mz_stream_open(read_mem_stream, NULL, MZ_OPEN_MODE_READ);
zip_handle = mz_zip_open(read_mem_stream, MZ_OPEN_MODE_READ);
mz_zip_create(&zip_handle);
err = mz_zip_open(zip_handle, read_mem_stream, MZ_OPEN_MODE_READ);
if (zip_handle != NULL)
if (err == MZ_OK)
{
err = mz_zip_goto_first_entry(zip_handle);
if (err == MZ_OK)
@@ -376,10 +413,14 @@ void test_zip_mem()
if (err == MZ_OK)
read = mz_zip_entry_read(zip_handle, temp, sizeof(temp));
MZ_UNUSED(read);
mz_zip_entry_close(zip_handle);
mz_zip_close(zip_handle);
}
mz_zip_delete(&zip_handle);
mz_stream_mem_close(&read_mem_stream);
mz_stream_mem_delete(&read_mem_stream);
read_mem_stream = NULL;
@@ -390,5 +431,154 @@ void test_zip_mem()
write_mem_stream = NULL;
}
/***************************************************************************/
#ifndef MZ_ZIP_NO_ENCRYPTION
void test_crypt_sha(void)
{
void *sha1 = NULL;
void *sha256 = NULL;
char *test = "the quick and lazy fox did his thang";
char computed_hash[320];
int32_t i = 0;
int32_t p = 0;
uint8_t hash[MZ_HASH_SHA1_SIZE];
uint8_t hash256[MZ_HASH_SHA256_SIZE];
printf("Sha hash input - %s\n", test);
memset(hash, 0, sizeof(hash));
mz_crypt_sha_create(&sha1);
mz_crypt_sha_set_algorithm(sha1, MZ_HASH_SHA1);
mz_crypt_sha_begin(sha1);
mz_crypt_sha_update(sha1, test, strlen(test));
mz_crypt_sha_end(sha1, hash, sizeof(hash));
mz_crypt_sha_delete(&sha1);
computed_hash[0] = 0;
for (i = 0, p = 0; i < (int32_t)sizeof(hash); i += 1, p += 2)
snprintf(computed_hash + p, sizeof(computed_hash) - p, "%02x", hash[i]);
computed_hash[p] = 0;
printf("Sha1 hash computed - %s\n", computed_hash);
printf("Sha1 hash expected - 3efb8392b6cd8e14bd76bd08081521dc73df418c\n");
memset(hash256, 0, sizeof(hash256));
mz_crypt_sha_create(&sha256);
mz_crypt_sha_set_algorithm(sha256, MZ_HASH_SHA256);
mz_crypt_sha_begin(sha256);
mz_crypt_sha_update(sha256, test, strlen(test));
mz_crypt_sha_end(sha256, hash256, sizeof(hash256));
mz_crypt_sha_delete(&sha256);
computed_hash[0] = 0;
for (i = 0, p = 0; i < (int32_t)sizeof(hash256); i += 1, p += 2)
snprintf(computed_hash + p, sizeof(computed_hash) - p, "%02x", hash256[i]);
computed_hash[p] = 0;
printf("Sha256 hash computed - %s\n", computed_hash);
printf("Sha256 hash expected - 7a31ea0848525f7ebfeec9ee532bcc5d6d26772427e097b86cf440a56546541c\n");
}
void test_crypt_aes(void)
{
void *aes = NULL;
char *key = "awesomekeythisis";
char *test = "youknowitsogrowi";
int32_t key_length = 0;
int32_t test_length = 0;
uint8_t buf[120];
int32_t i = 0;
uint8_t hash[MZ_HASH_SHA256_SIZE];
printf("Aes key - %s\n", key);
printf("Aes input - %s\n", test);
memset(hash, 0, sizeof(hash));
key_length = strlen(key);
test_length = strlen(test);
strncpy((char *)buf, test, test_length);
printf("Aes input hex\n");
for (i = 0; i < test_length; i += 1)
printf("%02x", buf[i]);
printf("\n");
mz_crypt_aes_create(&aes);
mz_crypt_aes_set_mode(aes, MZ_AES_ENCRYPTION_MODE_256);
mz_crypt_aes_set_encrypt_key(aes, key, key_length);
mz_crypt_aes_encrypt(aes, buf, test_length);
mz_crypt_aes_delete(&aes);
printf("Aes encrypted\n");
for (i = 0; i < test_length; i += 1)
printf("%02x", buf[i]);
printf("\n");
mz_crypt_aes_create(&aes);
mz_crypt_aes_set_mode(aes, MZ_AES_ENCRYPTION_MODE_256);
mz_crypt_aes_set_decrypt_key(aes, key, key_length);
mz_crypt_aes_decrypt(aes, buf, test_length);
mz_crypt_aes_delete(&aes);
printf("Aes decrypted\n");
for (i = 0; i < test_length; i += 1)
printf("%02x", buf[i]);
printf("\n");
}
void test_crypt_hmac(void)
{
void *hmac;
char *key = "hm123";
char *test = "12345678";
int32_t key_length = 0;
int32_t test_length = 0;
int32_t i = 0;
uint8_t hash[MZ_HASH_SHA1_SIZE];
uint8_t hash256[MZ_HASH_SHA256_SIZE];
key_length = strlen(key);
test_length = strlen(test);
printf("Hmac sha1 key - %s\n", key);
printf("Hmac sha1 input - %s\n", test);
mz_crypt_hmac_create(&hmac);
mz_crypt_hmac_set_algorithm(hmac, MZ_HASH_SHA1);
mz_crypt_hmac_init(hmac, key, key_length);
mz_crypt_hmac_update(hmac, test, test_length);
mz_crypt_hmac_end(hmac, hash, sizeof(hash));
mz_crypt_hmac_delete(&hmac);
printf("Hmac sha1 output hash hex\n");
for (i = 0; i < (int32_t)sizeof(hash); i += 1)
printf("%02x", hash[i]);
printf("\n");
printf("Hmac sha1 expected\n");
printf("c785a02ff303c886c304d9a4c06073dfe4c24aa9\n");
printf("Hmac sha256 key - %s\n", key);
printf("Hmac sha256 input - %s\n", test);
mz_crypt_hmac_create(&hmac);
mz_crypt_hmac_set_algorithm(hmac, MZ_HASH_SHA256);
mz_crypt_hmac_init(hmac, key, key_length);
mz_crypt_hmac_update(hmac, test, test_length);
mz_crypt_hmac_end(hmac, hash256, sizeof(hash256));
mz_crypt_hmac_delete(&hmac);
printf("Hmac sha256 output hash hex\n");
for (i = 0; i < (int32_t)sizeof(hash256); i += 1)
printf("%02x", hash256[i]);
printf("\n");
printf("Hmac sha256 expected\n");
printf("fb22a9c715a47a06bad4f6cee9badc31c921562f5d6b24adf2be009f73181f7a\n");
}
#endif
/***************************************************************************/
+9 -6
View File
@@ -7,12 +7,15 @@ extern "C" {
/***************************************************************************/
void test_aes();
void test_crypt();
void test_inflate();
void test_deflate();
void test_bzip();
void test_zip_mem();
void test_stream_bzip(void);
void test_stream_pkcrypt(void);
void test_stream_wzaes(void);
void test_stream_zlib(void);
void test_stream_mem(void);
void test_crypt_sha(void);
void test_crypt_aes(void);
void test_crypt_hmac(void);
/***************************************************************************/
+58 -40
View File
@@ -7,14 +7,23 @@ import shutil
import fnmatch
import time
import struct
import numpy
import random
import pprint
import hashlib
from sys import platform
parser = argparse.ArgumentParser(description='Test script')
parser.add_argument('--exe_dir', help='Built exes directory', default=None, action='store', required=False)
parser = argparse.ArgumentParser(description='Zip/unzip test script')
parser.add_argument('--exe_dir', help='Minizip command line tool directory', default=None, action='store', required=False)
parser.add_argument('--deflate', help='Runs zlib/deflate tests', action='store_const', const=True, default=None, required=False)
parser.add_argument('--raw', help='Runs raw tests', action='store_const', const=True, default=None, required=False)
parser.add_argument('--bzip2', help='Runs bzip2 tests', action='store_const', const=True, default=None, required=False)
parser.add_argument('--lzma', help='Runs lzma tests', action='store_const', const=True, default=None, required=False)
parser.add_argument('--pkcrypt', help='Runs pkware traditional encryption tests', action='store_const', const=True, default=None, required=False)
parser.add_argument('--wzaes', help='Runs winzip aes encryption tests', action='store_const', const=True, default=None, required=False)
parser.add_argument('--split', help='Runs disk span tests', action='store_const', const=True, default=None, required=False)
parser.add_argument('--zipcd', help='Runs zip/unzip central dir tests', action='store_const', const=True, default=None, required=False)
parser.add_argument('--sign', help='Runs signing tests', action='store_const', const=True, default=None, required=False)
parser.add_argument('--rand', help='Runs tests against random generated files', action='store_const', const=True, default=None, required=False)
args, unknown = parser.parse_known_args()
def hash_file_sha1(path):
@@ -39,33 +48,32 @@ def find_files(directory, pattern, recursive = True):
if fnmatch.fnmatch(basename, pattern):
filename = os.path.join(root, basename)
yield filename
def erase_file(path):
def erase_file(path, retries = 10, sleep = 0.1):
#print('Deleting {0}'.format(path))
os.chmod(path, stat.S_IWUSR)
os.remove(path)
for i in range(retries):
try:
os.chmod(path, stat.S_IWUSR)
os.remove(path)
except WindowsError:
time.sleep(sleep)
else:
break
def erase_files(search_pattern):
search_dir, match = os.path.split(search_pattern)
for path in find_files(search_dir, match):
erase_file(path)
def erase_dir(path):
def erase_dir(path, retries = 10):
if not os.path.exists(path):
return
print('Erasing dir {0}'.format(path))
#print('Erasing dir {0}'.format(path))
# shutil.rmtree doesn't work well and sometimes can't delete
for root, dirs, files in os.walk(path, topdown=False):
for name in files:
filename = os.path.join(root, name)
success = False
while not success:
try:
erase_file(filename)
success = True
except:
time.sleep(4)
pass
erase_file(filename)
for name in dirs:
os.rmdir(os.path.join(root, name))
os.rmdir(path)
@@ -94,6 +102,7 @@ def get_files_info(path):
return info
def create_random_file(path, size):
import numpy
if os.path.exists(path):
return
with open(path, 'wb') as fout:
@@ -113,7 +122,7 @@ def zip(zip_file, zip_args, files):
err = os.system(cmd)
if (err != 0):
print('Zip returned error code {0}'.format(err))
sys.exit(err)
exit(err)
def list(zip_file):
cmd = '{0} -l {1}'.format(get_exec('minizip'), zip_file)
@@ -121,7 +130,7 @@ def list(zip_file):
err = os.system(cmd)
if (err != 0):
print('List returned error code {0}'.format(err))
sys.exit(err)
exit(err)
def unzip(zip_file, dest_dir, unzip_args = ''):
cmd = '{0} -x {1} -d {2} {3}'.format(get_exec('minizip'), unzip_args, dest_dir, zip_file)
@@ -129,14 +138,13 @@ def unzip(zip_file, dest_dir, unzip_args = ''):
err = os.system(cmd)
if (err != 0):
print('Unzip returned error code {0}'.format(err))
sys.exit(err)
exit(err)
def zip_list_unzip(zip_file, dest_dir, zip_args, unzip_args, files):
# Single test run
original_infos = {}
for (i, path) in enumerate(files):
original_infos.update(get_files_info(path))
print original_infos
erase_files(zip_file[0:-2] + "*")
erase_dir(dest_dir)
@@ -157,29 +165,32 @@ def zip_list_unzip(zip_file, dest_dir, zip_args, unzip_args, files):
print(new_infos)
def file_tests(method, zip_arg = '', unzip_arg = ''):
global args
print('Testing {0} on Single File'.format(method))
zip_list_unzip('test.zip', 'out', zip_arg, unzip_arg, ['test.c'])
print('Testing {0} on Two Files'.format(method))
zip_list_unzip('test.zip', 'out', zip_arg, unzip_arg, ['test.c', 'test.h'])
print('Testing {0} Directory'.format(method))
zip_list_unzip('test.zip', 'out', zip_arg, unzip_arg, ['repo'])
print('Testing {0} 1MB file'.format(method))
create_random_file('1MB.dat', 1 * 1024 * 1024)
zip_list_unzip('test.zip', 'out', zip_arg, unzip_arg, ['1MB.dat'])
print('Testing {0} 50MB file'.format(method))
create_random_file('50MB.dat', 50 * 1024 * 1024)
zip_list_unzip('test.zip', 'out', zip_arg, unzip_arg, ['50MB.dat'])
if args.rand:
print('Testing {0} 1MB file'.format(method))
create_random_file('1MB.dat', 1 * 1024 * 1024)
zip_list_unzip('test.zip', 'out', zip_arg, unzip_arg, ['1MB.dat'])
print('Testing {0} 25MB file'.format(method))
create_random_file('25MB.dat', 25 * 1024 * 1024)
zip_list_unzip('test.zip', 'out', zip_arg, unzip_arg, ['25MB.dat'])
def compression_method_tests(method = '', zip_arg = '', unzip_arg = ''):
global args
method = method + ' ' if method != '' else ''
file_tests(method + 'Deflate', zip_arg, unzip_arg)
file_tests(method + 'Raw', '-0 ' + zip_arg, unzip_arg)
file_tests(method + 'BZIP2', '-b ' + zip_arg, unzip_arg)
file_tests(method + 'LZMA', '-m ' + zip_arg, unzip_arg)
def encryption_tests():
compression_method_tests('Crypt', '-p 1234567890', '-p 1234567890')
compression_method_tests('AES', '-s -p 1234567890', '-p 1234567890')
if args.deflate:
file_tests(method + 'Deflate', zip_arg, unzip_arg)
if args.raw:
file_tests(method + 'Raw', '-0 ' + zip_arg, unzip_arg)
if args.bzip2:
file_tests(method + 'BZIP2', '-b ' + zip_arg, unzip_arg)
if args.lzma:
file_tests(method + 'LZMA', '-m ' + zip_arg, unzip_arg)
def empty_zip_test():
unzip('empty.zip', 'out')
@@ -194,9 +205,16 @@ if not os.path.exists('repo'):
os.system('git clone https://github.com/nmoinvaz/minizip repo')
# Run tests
empty_zip_test()
sfx_zip_test()
#empty_zip_test()
#sfx_zip_test()
compression_method_tests()
encryption_tests()
compression_method_tests('Disk Span', '-k 1024', '')
compression_method_tests('Buffered', '-u', '-u')
if args.pkcrypt:
compression_method_tests('PKCrypt', '-p 1234567890', '-p 1234567890')
if args.wzaes:
compression_method_tests('WinZipAES', '-s -p 1234567890', '-p 1234567890')
if args.split:
compression_method_tests('Disk Span', '-k 1024', '')
if args.zipcd:
compression_method_tests('ZipCD', '-z', '')
if args.sign:
compression_method_tests('Signing', '-h ia.p12 -w test', '')