Compare commits

...
260 Commits
Author SHA1 Message Date
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
Nathan Moinvaziri cfcbccf67e Merge branch 'dev' of https://github.com/nmoinvaz/minizip into dev 2018-07-11 16:45:16 -07:00
Nathan Moinvaziri eb80cd9fa4 Fixed bug extracting 0-byte file when stored. 2018-07-11 16:45:09 -07:00
Nathan Moinvaziri 9e2b2456dc Update README.md 2018-07-11 16:35:32 -07:00
Nathan Moinvaziri ffa830f684 Version 2.3.6. 2018-07-11 16:34:01 -07:00
Nathan Moinvaziri f65b9fecae Fixed bug verifying password header using pkcrypt. 2018-07-11 16:28:56 -07:00
Nathan Moinvaziri f76c04be40 Fixed code accidentally removed. 2018-07-11 15:51:49 -07:00
Nathan Moinvaziri ebb01ce07e Remove unused include. 2018-07-11 15:43:29 -07:00
Nathan Moinvaziri b0a005644d Cleanup, fixed formatting. 2018-07-11 15:34:45 -07:00
Nathan Moinvaziri d7814e9b8d Finialize compression stream even if nothing written due to compatibility with other zip apps. #277
Changed pkcrypt to use crc32 update function instead of doing calculation from crc32 table.
Modified mz_zip_entry_read to not return when uncompressed_size is 0 due to the fact that it is possible for 0-byte files to have packed bytes if using deflate method.
2018-07-11 14:54:14 -07:00
Nathan Moinvaziri 50f3252d2c Fixed missing crc32 stream include. 2018-07-11 09:56:23 -07:00
Nathan Moinvaziri adbbc68fa4 Fixed missing include for crc32 stream. 2018-07-11 09:48:27 -07:00
Nathan Moinvaziri 34e2ab5065 Added crc32 stream. 2018-07-11 09:46:38 -07:00
Nathan Moinvaziri b47b41b8df Move crc32 stream into its own source file and header. 2018-07-11 09:44:29 -07:00
Nathan Moinvaziri 155f8b52b2 Version 2.3.5. 2018-07-09 08:13:28 -07:00
Nathan Moinvaziri 5d3b5c593d Fixed copyright statements on interface headers due to rewrite. 2018-07-09 08:11:16 -07:00
Nathan Moinvaziri e3958d9b46 Rename file. 2018-07-09 07:54:26 -07:00
Nathan Moinvaziri 957e5f92e4 Added 0 byte file for testing. 2018-07-09 07:53:54 -07:00
Nathan Moinvaziri 184f4cbde1 Added comment if entire entry is not read. 2018-07-09 07:53:17 -07:00
Nathan Moinvaziri 8871f6c0f1 Expose more prop values from streams.
Simplify some logic.
2018-07-08 19:23:56 -07:00
Nathan Moinvaziri fa8105b657 Use crypt header size in calculation. 2018-07-08 19:05:00 -07:00
Nathan Moinvaziri 2cf91c09ce Don't finalize compression stream if nothing written. #277 2018-07-08 18:53:52 -07:00
Nathan Moinvaziri 9eecff3140 Simplify command line parsing. 2018-07-08 18:52:41 -07:00
Nathan Moinvaziri 77dcb77506 Fixed lzma not flushing all remaining codes. #275 2018-07-07 12:21:25 -07:00
Nathan Moinvaziri ef776a31e1 Updated to liblzma 5.2.4. 2018-07-07 11:10:11 -07:00
Nathan Moinvaziri b70b824b19 Version 2.3.4. 2018-06-19 12:30:12 -07:00
Nathan Moinvaziri 0a9282d2f0 Added support for only using either compression or decompression with continued support for raw mode.
Renamed MZ_USE_WINRT_API to MZ_WINRT_API.
2018-06-19 11:59:07 -07:00
Nathan Moinvaziri 6be5ec8163 Fixed formatting. 2018-06-19 10:37:58 -07:00
Nathan Moinvaziri 5d4c61fdd5 Moved zlib installation instructions. 2018-06-19 10:36:38 -07:00
Nathan Moinvaziri 4bcaf521d7 Added windows install instructions #269 2018-06-19 10:35:40 -07:00
Nathan Moinvaziri 957f2e9976 Fixed issues with resolving paths.
Added source directory to includes.
Added unit tests for resolving paths.
Fixed compilation issues with test code.
2018-06-19 09:44:06 -07:00
Nathan Moinvaziri dd808239dd Fixed zip slip vulnerability in example app. #268 2018-06-11 07:38:58 -07:00
Nathan Moinvaziri 7bdfbf4699 Version 2.3.3. 2018-06-10 09:09:07 -07:00
Nathan Moinvaziri 4a7723357e Fixed casting issues after crc32 changes for mac. 2018-06-10 09:07:35 -07:00
Nathan Moinvaziri 4925f31e28 Fixed compilation warnings on mac and linux. 2018-05-31 15:05:19 -07:00
Nathan Moinvaziri 669f5a3086 Fixed buffered stream not being reset for reading after close.
Added ability to get buffer length for memory stream.
2018-05-31 13:56:32 -07:00
Nathan Moinvaziri 30e859be0b Version 2.3.2. 2018-05-29 08:48:05 -07:00
Nathan Moinvaziri 5b204622cc Modified pkcrypt to use crc32 table from zlib or lzma. 2018-05-23 20:39:12 -07:00
Nathan Moinvaziri b07ab3e9b9 Merge branch 'dev' of https://github.com/nmoinvaz/minizip into dev 2018-05-23 20:27:23 -07:00
Nathan Moinvaziri 3dc4334d38 Rename crypt to pkcrypt. 2018-05-23 20:27:17 -07:00
Nathan Moinvaziri ca30682447 Fixed formatting 2018-05-23 20:23:41 -07:00
Nathan Moinvaziri 0e5c9dcdf4 Renamed traditional PKware encryption to pkcrypt.
Don't link mz_os_rand if encryption is not turned on. #267
2018-05-23 20:21:48 -07:00
Nathan Moinvaziri ea99a46b6d Define possibly missing #defines from zlib header. #264 2018-05-22 14:19:50 -07:00
Nathan Moinvaziri 23bc9fd943 Merge pull request #266 from asmaloney/dev
Fix build on macOS
2018-05-22 13:00:00 -07:00
Andy Maloney cfcb729f79 Fix build on macOS
(Only Windows needs this __declspec)
2018-05-22 13:01:03 -04:00
Nathan Moinvaziri 51521bddc8 Fixed formatting. 2018-05-22 09:47:53 -07:00
Nathan Moinvaziri 41331dd3ec Fixed issues not being able to compile without zlib. #264 2018-05-22 09:46:03 -07:00
Nathan Moinvaziri 9c155991d7 Merge branch 'master' into dev 2018-05-22 09:35:45 -07:00
Nathan Moinvaziri 43e787bd88 Merge pull request #265 from asmaloney/asm_master
Partial fix for building without zlib
2018-05-22 09:32:42 -07:00
Andy Maloney 3909c23817 Partial fix for building without zlib
(See issue nmoinvaz/minizip#264)
2018-05-22 09:02:09 -04:00
Nathan Moinvaziri c0d3dbf537 Make #define names consistent. 2018-05-09 20:06:12 -07:00
Nathan Moinvaziri 0388124aad Simplify parsing of filename from path. 2018-05-09 20:03:26 -07:00
Nathan Moinvaziri e1aad8c445 Version 2.3.1. 2018-05-09 19:48:55 -07:00
Nathan Moinvaziri b5fb12503d Fixed more warnings. #263 2018-05-09 09:52:43 -07:00
Nathan Moinvaziri bd2f162009 Fixed more warnings. #263 2018-05-09 09:45:15 -07:00
Nathan Moinvaziri 408940f16c Fixed more warnings. #263 2018-05-09 09:42:31 -07:00
Nathan Moinvaziri a710bd714a Fixed reported warnings. #263 2018-05-09 00:46:47 -07:00
Nathan Moinvaziri 7a3b698578 Fixed more mac warnings. #263 2018-05-09 00:45:02 -07:00
Nathan Moinvaziri e275fcf017 Removed ZIP_UNUSED. #263 2018-05-09 00:34:16 -07:00
Nathan Moinvaziri 0e2e1a0b05 Fixed compiler warnings on mac. #263 2018-05-09 00:31:25 -07:00
Nathan Moinvaziri 3431a6635b Fixed more compiler warnings. #263 2018-05-06 16:59:31 -07:00
Nathan Moinvaziri 521a42a4c3 Fixed some compiler warnings. #263 2018-05-06 16:32:46 -07:00
Nathan Moinvaziri a4b83e8e95 Version 2.3.0. 2018-05-03 17:59:04 -07:00
Nathan Moinvaziri 121e088a7d Add back auto detection of zip64. #258 2018-05-03 17:57:20 -07:00
Nathan Moinvaziri 46f714348e Simplification & fixed formatting. #258 2018-05-03 17:24:32 -07:00
Nathan Moinvaziri a56a08cf79 Allow configuration of zip64 support. #258 2018-05-03 09:35:37 -07:00
Nathan Moinvaziri 8a50b4b5df Merge pull request #261 from vszakats/ws2
whitespace fixes
2018-05-02 20:09:48 -07:00
Nathan Moinvaziri 37ceb026db Merge branch 'dev' into ws2 2018-05-02 20:09:25 -07:00
Nathan Moinvaziri 586300a787 Use MZ_UNUSED where applicable. #262 2018-05-02 20:07:45 -07:00
Nathan Moinvaziri 92c8d77cfb Merge pull request #262 from vszakats/warn2
silence various warnings
2018-05-02 20:04:57 -07:00
Nathan Moinvaziri c546a57f90 Fixed wrong defined MZ_ALLOC and MZ_FREE. #249 2018-05-02 20:02:30 -07:00
Nathan Moinvaziri 5244fc00c4 Added MZ_UNUSED #262
Added #define for malloc and free. #249
2018-05-02 20:01:35 -07:00
Viktor Szakats 4c76400cce silence various warnings
- unused parameters
- trivial const stripping cases
- missing NULL initializers from vtbls
- missing static scope for vtbls

Missing newlines at EOF addressed in a separate PR.
It's be nice to replace `ZIP_UNUSED var` and `(void)var`
with a global and non-gcc-specific solution, such as:
   #define MZ_UNUSED(symbol)  (void)symbol
2018-05-02 21:40:29 +00:00
Nathan Moinvaziri 240b3b6709 Added comment for slash appending for dirs. 2018-05-02 13:38:14 -07:00
Nathan Moinvaziri e54582384a Remove any existing slash at the end of filename. 2018-05-02 13:07:03 -07:00
Nathan Moinvaziri 3ad1a92279 Append slash to directories for compatibility. #257 2018-05-02 13:04:41 -07:00
Viktor Szakats 447dd06750 whitespace fixes
- remove line-ending spaces
- always have a file-ending newline
- remove duplicate file-ending newlines
2018-05-02 19:59:38 +00:00
Nathan Moinvaziri 4827f71a08 Fixed formatting. 2018-05-02 10:50:47 -07:00
Nathan Moinvaziri bb3b75bc09 Fixed directory compression method.
Move zip attribute detection to mz_zip. #257
2018-05-02 10:48:51 -07:00
Nathan Moinvaziri b7bd0421c5 Merge branch 'dev' of https://github.com/nmoinvaz/minizip into dev 2018-05-02 09:55:15 -07:00
Nathan Moinvaziri 9a3ae75a48 Fixed print out of directories. #257 2018-05-02 09:54:44 -07:00
Nathan Moinvaziri 120bea1a6f Merge pull request #260 from vszakats/castwarn
fix cast warning (seen with LLVM/Clang)
2018-05-02 09:26:21 -07:00
Nathan Moinvaziri 4a41245099 Added msdos host system and dir detection. 2018-05-02 09:24:09 -07:00
Viktor Szakats 0f0535fcaa fix cast warning (seen with LLVM/Clang)
```
mz_compat.c:390:62: warning: passing 'uint32_t *' (aka 'unsigned int *') to parameter of type
      'int32_t *' (aka 'int *') converts between pointers to integer types with different sign [-Wpointer-sign]
        err = mz_zip_get_disk_number_with_cd(compat->handle, &pglobal_info->number_disk_with_CD);
                                                             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mz_zip.h:112:70: note: passing argument to parameter 'disk_number_with_cd' here
extern int32_t mz_zip_get_disk_number_with_cd(void *handle, int32_t *disk_number_with_cd);
                                                                     ^
```
2018-05-02 13:15:58 +00:00
Nathan Moinvaziri 8d8deb4e5d Added listing of attributes. 2018-05-01 21:54:15 -07:00
Nathan Moinvaziri 83bce6e82a Allow version_needed to be less than customized. #258 2018-05-01 18:10:17 -07:00
Nathan Moinvaziri 739f03f951 Updated comment. 2018-05-01 13:57:28 -07:00
Nathan Moinvaziri 7583be518c Fixed is directory check on linux. 2018-05-01 13:55:28 -07:00
Nathan Moinvaziri 0619c3e6c4 Merge branch 'dev' of https://github.com/nmoinvaz/minizip into dev 2018-05-01 13:51:13 -07:00
Nathan Moinvaziri e33916b608 Added support for directory entries in minizip. #257
Added support for file attributes.
2018-05-01 13:45:08 -07:00
Nathan Moinvaziri 596f19f79e Merge pull request #256 from vszakats/rawlzmaflag
fix to set MZ_ZIP_FLAG_LZMA_EOS_MARKER flag in raw mode
2018-04-30 09:34:30 -07:00
Viktor Szakats 2884e67a1a fix to set MZ_ZIP_FLAG_LZMA_EOS_MARKER flag in raw mode
in function mz_zip_entry_write_open()

Fixes https://github.com/nmoinvaz/minizip/issues/255
2018-04-30 08:16:09 +00:00
Nathan Moinvaziri ae5740b19c Merge pull request #253 from vszakats/eolws
remove whitespace @ eol
2018-04-24 08:50:47 -07:00
Nathan Moinvaziri b14ded7d01 Merge pull request #254 from vszakats/patch-1
README.md: switch an URL to use HTTPS
2018-04-24 08:49:48 -07:00
Viktor Szakats 914049d19c README.md: switch an URL to use HTTPS 2018-04-24 14:20:35 +02:00
Viktor Szakats 915b82e95b remove whitespace @ eol 2018-04-24 10:02:39 +00:00
Nathan Moinvaziri 0f09a00810 Fixed formatting. 2018-04-23 18:48:30 -07:00
Nathan Moinvaziri f5f23217b3 Fixed formatting. 2018-04-23 18:46:09 -07:00
Nathan Moinvaziri 5d362f43eb Merge pull request #252 from vszakats/fixes
improve compatibility with original minizip
2018-04-23 18:31:31 -07:00
Viktor Szakats ce26dba5ba improve compatibility with original minizip
* Fix NULL pointer dereference when calling zipOpen*()
  or unzOpen2*() with NULL filefunc table.
* Rename dos_date to dosDate in zip/unzip structures to
  be compatible with original minizip.
* Add tm struct timestamps to zip/unzip structures and
  populate them with the dosDate value for unzip and
  use the value passed into it on zip, if dosDate is
  zero (mimicing original minizip behaviour.)
* Change zipWriteInFileInZip() return value to be a
  status code (ZIP_OK or ZIP_ERRNO) instead of the
  number of bytes written. This way it's minizip
  compatible.
* Add zipCloseFileInZipRaw64() compatibility wrapper.
* Add these compatibility functions:
  unzGetOffset()
  unzGetOffset64()
  unzSetOffset()
  unzSetOffset64()
* Add stub for compatibility: unzGetLocalExtrafield()
  It will always return zero for now.
* Add original minizip types for compatiblity:
  tm_unz, tm_zip, ZPOS64_T
* Add these minizip (non-compatiblity) APIs:
  mz_zip_get_entry()
  mz_zip_goto_entry()
* zipOpenNewFileInZip*() to use the default filename of
  "-" if filename parameter was NULL. For minizip
  compatibility.
* Fix mz_zip_entry_open_int() and mz_zip_entry_read_open()
  failing with MZ_PARAM_ERROR in raw mode
* Add/cleanup some date-related comments
* mz_zip_time_t_to_tm() to clear the `tm` structure in
  case localtime() call failed.
* Fix mz_zip_tm_to_dosdate() to do the date validation
  on the normalized date (the year in particular), not on
  the raw value passed by the caller. This makes it work
  correctly when year is passed as-is, such as 1990.

The reference minizip was the version that comes with
the latest zlib, currently at 1.2.11.
2018-04-23 22:38:05 +00:00
Nathan Moinvaziri ad8e3dd4b1 Version 2.2.9. 2018-04-18 20:54:52 -07:00
Nathan Moinvaziri 53e1d650e5 Fixed append mode does not work under linux. #250 2018-04-18 20:51:00 -07:00
Nathan Moinvaziri bcd64ca3b8 Merge branch 'dev' 2018-03-27 20:44:10 -07:00
Nathan Moinvaziri 078f20290f Don't add gitignore. 2018-03-27 20:41:18 -07:00
Nathan Moinvaziri 9c98a6b180 Don't use z_crc_t typedef. 2018-03-27 20:40:21 -07:00
Nathan Moinvaziri a30ff41e9d Remove unnecessary aes files. 2018-03-27 20:39:54 -07:00
Nathan Moinvaziri a43eb98099 Remove src directory. 2018-03-27 20:37:53 -07:00
Nathan Moinvaziri 8d8a987eea Fixed overflow when getting the disk number with cd. #248 2018-03-19 18:23:13 -07:00
Nathan Moinvaziri c5f0063c75 Version 2.2.8. 2018-03-15 11:14:11 -07:00
Nathan Moinvaziri d57eb5de40 Merge branch 'master' into dev 2018-03-15 11:12:35 -07:00
Nathan Moinvaziri 672dc834f1 Merge pull request #240 from juanii/fix_233
Fixes #233
2018-03-15 10:43:52 -07:00
Nathan Moinvaziri 5e8f4d7d88 Fixed formatting. 2018-03-15 10:43:18 -07:00
Nathan Moinvaziri 68a48aa20e Merge pull request #247 from tonytheodore/master
Minor fixes for mingw cross-compilation
2018-03-13 14:54:55 -07:00
Tony Theodore b5ac94fd24 Use Requires: zlib to fix pkg-config ordering of static libs 2018-03-13 20:14:45 +11:00
Tony Theodore b4020169b7 Fix naming with automatic lib prefix on mingw shared build 2018-03-13 20:11:13 +11:00
Nathan Moinvaziri b8f38540c2 Define HAVE_ZLIB #244 2018-03-09 01:19:48 -08:00
Nathan Moinvaziri d65cd2ea9d Merge pull request #246 from jthorniley/pkgconfig-linux-only
Make pkg-config build dependency linux only
2018-03-05 19:12:32 -08:00
James Thorniley 6cd3054608 Make pkg-config build dependency linux only 2018-03-05 10:51:11 +00:00
Nathan Moinvaziri f44494aad2 Merge pull request #243 from jhudsonWA/master
Fix Unknown type name 'z_crc_t' in minizip 2.x
2018-02-21 18:49:48 -08:00
Nathan Moinvaziri 7c5ccae39c Fixed formatting. 2018-02-21 18:49:20 -08:00
Joshua Hudson 82df346497 Update mz_strm_crypt.c
fix Unknown type name 'z_crc_t'
2018-02-21 17:18:09 -08:00
Nathan Moinvaziri 225c627f7d Update README.md 2018-02-16 23:11:45 -08:00
Nathan Moinvaziri db8158a5cc Merge pull request #242 from juanii/master
Fix compat layer's unzGetGlobalInfo() for archives without global com…
2018-02-14 17:30:27 -08:00
juanii 62090966b1 Removed unnecessary assignment to size_comment. 2018-02-14 22:27:08 -03:00
juanii dec6a65a64 Merge remote-tracking branch 'upstream/master' 2018-02-14 15:37:29 -03:00
juanii 7d7b1c9554 Fix compat layer's unzGetGlobalInfo() for archives without global comment. 2018-02-14 15:30:31 -03:00
Nathan Moinvaziri a4a4a1c665 Merge pull request #241 from juanii/fix_237
Fixes #237
2018-02-13 20:01:26 -08:00
juanii b8887e9179 Fixes #237 2018-02-14 00:51:05 -03:00
Nathan Moinvaziri c0cc7df743 Merge pull request #236 from juanii/master
Fix: unzGetGlobalInfo failed with MZ_EXIST_ERROR if there was no comment in ZIP file.
2018-02-13 17:23:37 -08:00
Nathan Moinvaziri c5e2d27028 Initialize values to zero when retrieving global info 2018-02-13 17:05:04 -08:00
Nathan Moinvaziri bb7b62fcb4 Merge pull request #238 from juanii/fix_ntfs_extra_field
Fix ntfs extra field
2018-02-13 15:31:48 -08:00
Nathan Moinvaziri e56962c624 Fixed formatting 2018-02-13 15:31:15 -08:00
Nathan Moinvaziri d9e159a395 Fixed formatting 2018-02-13 15:30:30 -08:00
juanii 4578b9dd33 Remove unnecessary assignment. 2018-02-12 15:18:26 -03:00
Nathan Moinvaziri b44f9c07b8 Merge pull request #239 from juanii/fix_disk_number
Fix: Field disk_number is 16bit, will never contain UINT32_MAX.
2018-02-12 07:21:23 -08:00
juanii 55bcdaf3c4 Fixes #233 2018-02-11 20:55:57 -03:00
juanii 8f6f2ad5fb mz_compat: Init file_info struct to zeroes 2018-02-11 14:39:57 -03:00
juanii 7063b0e2e9 Read NTFS extra field only if all (modify, access, creation) timestamps are present. 2018-02-11 14:39:57 -03:00
juanii 3679a3dcdc Add NTFS extra field only if all (modified, accessed, creation) timestamps are present. 2018-02-11 14:39:57 -03:00
juanii 4ae7992c2e Fix: Field disk_number is 16bit, will never contain UINT32_MAX. 2018-02-11 14:29:36 -03:00
juanii 566b978768 Fix: unzGetGlobalInfo failed with MZ_EXIST_ERROR if there was no comment in ZIP file. 2018-02-10 15:08:21 -03:00
Nathan Moinvaziri 7a3820c539 Merge pull request #234 from juanii/master
ZIP64 format is required if number of entries in archive is greater than 65535.
2018-02-09 17:36:53 -08:00
Nathan Moinvaziri bca5f2761d Fixed formatting. 2018-02-09 17:33:49 -08:00
Nathan Moinvaziri f7d49e2d7c Merge pull request #235 from rip-nsk/fix_zipOpenNewFileInZip5
fix zipOpenNewFileInZip5
2018-02-08 19:59:53 -08:00
Nathan Moinvaziri f8b3d67c67 Fixed formatting 2018-02-08 19:59:33 -08:00
rip.nsk 2043f834b0 fix zipOpenNewFileInZip5 2018-02-08 14:08:06 -08:00
juanii c65486db27 Proper value for "total number of disk" field in ZIP64 CD locator 2018-02-08 17:07:07 -03:00
juanii 30589b5419 Host system must be placed in high byte of "version made by" field. ZIP version in lower byte. 2018-02-08 14:43:02 -03:00
juanii 3f59ffc209 ZIP64 format is required if number of entries in archive is greater than 65535. 2018-02-08 12:44:17 -03:00
Nathan Moinvaziri d487d78b30 Merge pull request #232 from rip-nsk/Use_Z_PREFIX_if_needed
Check zlib is compiled with zprefix and use Z_PREFIX if needed.
2018-02-05 17:56:40 -08:00
Nathan Moinvaziri 1d5b57bd9b Merge pull request #231 from rip-nsk/keep_FILE_SHARE_READ_if_MZ_OPEN_MODE_READ
keep FILE_SHARE_READ if MZ_OPEN_MODE_READ
2018-02-05 17:51:21 -08:00
rip.nsk 9e89bb1ee0 Check zlib is compiled with zprefix and use Z_PREFIX if needed. 2018-02-05 12:42:03 -08:00
rip.nsk 6c717b6558 keep FILE_SHARE_READ if MZ_OPEN_MODE_READ 2018-02-05 12:33:30 -08:00
Nathan Moinvaziri 5c3dd5c27e Version 2.2.7. 2018-01-30 12:27:22 -08:00
Nathan Moinvaziri ceca477ed4 Fixed segmentation fault when trying to close posix stream. #225 2018-01-30 12:19:36 -08:00
Nathan Moinvaziri 34eff624c4 Fixed missing include. 2018-01-22 09:25:15 -08:00
Nathan Moinvaziri 930f9188a5 Fixed casting and 16-bit zlib limitation. 2018-01-22 08:51:07 -08:00
Nathan Moinvaziri 397d2d5ab3 Merge pull request #228 from evanmiller/dev
Support reading 65kb+ entries on most machines.
2018-01-22 08:46:39 -08:00
Evan Miller 3064a0e551 Support reading 65kb+ entries on most machines. 2018-01-21 20:32:26 -05:00
Nathan Moinvaziri d22a27a9c3 Merge branch 'master' into dev 2018-01-07 10:30:35 -08:00
Nathan Moinvaziri 98cd93d3a7 Merge pull request #223 from chenxiaolong/x86-features
cmake: Enable x86 optimizations only if supported
2018-01-07 10:24:52 -08:00
Nathan Moinvaziri d52e453488 Merge pull request #222 from chenxiaolong/pic
cmake: Use POSITION_INDEPENDENT_CODE property instead of setting -fPIC
2018-01-07 10:23:57 -08:00
Nathan Moinvaziri 61a4b59216 Merge pull request #221 from chenxiaolong/android-lfs
cmake: Don't define LFS macros on Android API < 24
2018-01-07 10:23:21 -08:00
Andrew Gunnerson 7443318057 cmake: Enable x86 optimizations only if supported
Previously, these optimizations would get enabled when building for
non-x86 architectures using Clang, which would cause a warning to be
output.

Signed-off-by: Andrew Gunnerson <chenxiaolong@cxl.epac.to>
2018-01-06 20:20:39 -05:00
Andrew Gunnerson e474a61e86 cmake: Use POSITION_INDEPENDENT_CODE property instead of setting -fPIC
CMake will figure out the right compiler arguments to use for enabling
PIC on the target platform.

Signed-off-by: Andrew Gunnerson <chenxiaolong@cxl.epac.to>
2018-01-06 19:28:35 -05:00
Andrew Gunnerson 5e67eb720a cmake: Don't define LFS macros on Android API < 24
Android APIs < 24 don't have support for the POSIX LFS API and will
fail at link time if the macros are defined.

Signed-off-by: Andrew Gunnerson <chenxiaolong@cxl.epac.to>
2018-01-06 19:24:38 -05:00
Nathan Moinvaziri d18e09d5b0 Merge branch 'dev' 2018-01-06 08:52:58 -08:00
Nathan Moinvaziri 7855c3efd0 Version 2.2.6. 2018-01-06 08:51:18 -08:00
Nathan Moinvaziri c689d51f79 Merge branch 'dev' of https://github.com/nmoinvaz/minizip into dev 2018-01-06 08:49:08 -08:00
Nathan Moinvaziri fee885a931 Updated copyright years. 2018-01-06 08:49:03 -08:00
Nathan Moinvaziri a1e78c5fc4 Merge pull request #220 from chenxiaolong/fix-data-descriptor
mz_zip.c: Fix incorrect size of data descriptor fields
2018-01-06 08:46:46 -08:00
Andrew Gunnerson 3c52d21ba9 mz_zip.c: Fix incorrect size of data descriptor fields
Previously, the conditions for writing 32-bit vs 64-bit data descriptor
fields were swapped. If the size was smaller than UINT32_MAX, a 64-bit
integer would be written, and if the size was larger, then a truncated
32-bit integer would be written. This prevented programs that don't have
support for the central directory (like busybox) from reading files
created by minizip.

Signed-off-by: Andrew Gunnerson <chenxiaolong@cxl.epac.to>
2018-01-05 22:28:12 -05:00
Nathan Moinvaziri a6e3b69a3b Merge pull request #219 from mikecrowe/cmake-fixes
Cmake fixes
2018-01-04 10:13:36 -08:00
Mike Crowe 0fbdb2298e CMakeLists.txt: Use customisable installation directory for cmake configuration
The existing CMakeLists.txt installed the minizip cmake configuration files
in PREFIX/cmake which doesn't match where other packages install such
files.

Let's add a INSTALL_CMAKE_DIR so that the destination can be specified, and
then default to installing in the standard ${CMAKE_INSTALL_LIBDIR}/cmake
location which should end up being the right place.

Tested with OpenEmbedded and on Debian GNU/Linux 9.3.
2018-01-04 12:46:48 +00:00
Mike Crowe 7df69ea96e CMakeLists: Use GNUInstallDirs to specify default install directories
CMake's GNUInstallDirs knows where particular Linux architectures and
distributions want to have their libraries installed. In particular,
Debian-derived "multi-arch" distributions keep their libraries in triplet
sudirectories under /lib. Other "bi-arch" distributions keep 64-bit
libraries in /lib64.

Including GNUInstallDirs and using CMAKE_INSTALL_ variables means that
minizip's libraries and header files are installed in the correct
locations.

For GNUInstallDirs to work, CMAKE_SYSTEM_NAME needs to be set which only
appears to be the case after the project command so this must be moved
earlier.

Also, since minizip is compiled, its pkgconfig file should be in the
architecture-specific "lib" rather than the architecture-independent
"share" directory.

Tested with OpenEmbedded and on Debian GNU/Linux 9.3 (the special naming
only applies when installing in /usr.)
2018-01-04 12:46:41 +00:00
Nathan Moinvaziri fc746532d8 Change bzip stream code so it is similar to zlib stream code. 2018-01-03 10:24:18 -08:00
Nathan Moinvaziri d60c6d20af Turn off debugging. 2018-01-03 10:14:25 -08:00
Nathan Moinvaziri 55e4fe414e Version 2.2.5. 2018-01-03 10:13:28 -08:00
Nathan Moinvaziri 5bb372d495 Fixed zlib stream not writing all flush/finish bytes. 2018-01-03 10:08:34 -08:00
Nathan Moinvaziri a86a4355da Fixed bug reading entries with 0 size. #217 2018-01-02 13:12:07 -08:00
Nathan Moinvaziri c9fd8d4df0 Uncomment debug statement. 2018-01-02 13:10:04 -08:00
Nathan Moinvaziri 02123b82d5 Fixed bugs in buffered stream. #216
Added buffered stream support to minizip app.
Added unit tests for buffered io.
2018-01-02 09:22:38 -08:00
Nathan Moinvaziri 7140c588c6 Fixed writing raw data to zip file in compatibility layer. 2018-01-02 08:16:40 -08:00
Nathan Moinvaziri f60a99bd90 Merge pull request #215 from chenxiaolong/cmake-global
CMakeLists.txt: Use add_compile_options() instead of modifying CMAKE_C_FLAGS
2018-01-02 07:43:46 -08:00
Nathan Moinvaziri 50fd547660 Merge pull request #214 from chenxiaolong/posix-error-checking
mz_strm_posix: Fix return value checking for POSIX stdio functions
2018-01-02 07:43:01 -08:00
Andrew Gunnerson 2fa05a7264 CMakeLists.txt: Use add_compile_options() instead of modifying CMAKE_C_FLAGS
This allows a parent project to use minizip via:

    add_subdirectory(path/to/minizip)

without the compiler flags of the parent project being affected.

Signed-off-by: Andrew Gunnerson <chenxiaolong@cxl.epac.to>
2018-01-01 15:51:40 -05:00
Andrew Gunnerson 1a20b75c46 CMakeLists.txt: Fix typo: _FILE_OFFSET_BIT -> _FILE_OFFSET_BITS
Signed-off-by: Andrew Gunnerson <chenxiaolong@cxl.epac.to>
2018-01-01 15:45:24 -05:00
Andrew Gunnerson dab094fc31 mz_strm_posix: Fix return value checking for POSIX stdio functions
Signed-off-by: Andrew Gunnerson <chenxiaolong@cxl.epac.to>
2018-01-01 15:41:32 -05:00
Nathan Moinvaziri 13a12e32ab Fixed missing prototypes warning. #213 2017-12-26 12:40:30 -08:00
Nathan Moinvaziri 73418b7e05 Fixed unused variables. #212 2017-12-26 12:29:21 -08:00
Nathan Moinvaziri 3f7de8e289 Simplify cleaning up of resources. 2017-12-11 21:09:52 -08:00
Nathan Moinvaziri 18a30653b1 Verify decrypted header to detect wrong password for Traditional PKZIP. 2017-12-07 06:59:53 -08:00
Nathan Moinvaziri b73887478a Wait for CI for 30 minutes. 2017-12-04 08:33:51 -08:00
Nathan Moinvaziri 51fb0afc82 Fixed casting warning. 2017-12-04 05:42:11 -08:00
Nathan Moinvaziri a45fa6b8a0 Merge pull request #209 from GerbilSoft/bugfix/libbsd-requirements
Mark libbsd as REQUIRED on Linux.
2017-12-03 23:08:51 -08:00
Nathan Moinvaziri 6c6700d2f4 Removed arc4random_buf shim. 2017-12-03 23:07:38 -08:00
Nathan Moinvaziri 0574424b05 Fixed formatting 2017-12-03 23:06:36 -08:00
Nathan Moinvaziri 919d837abf Fixed potential mismatch of calling conventions. #208 2017-12-03 22:59:26 -08:00
David Korth 12db0fc415 mz_strm_split.c: snprintf() isn't defined before MSVC 2015.
_snprintf() is available, so use that instead.
2017-11-26 13:43:38 -05:00
David Korth e4bd6d3191 Removed the executable bit from .c and .h files. 2017-11-26 13:42:31 -05:00
David Korth f9749af452 mz_os_posix.c: Implement arc4random_buf() for Mac OS X earlier than 10.7.
10.6 and earlier has arc4random(), but not arc4random_buf().
2017-11-18 16:51:28 -05:00
David Korth 02a516944d CMakeLists.txt: libbsd is required for Linux builds.
mz_os_posix.c: #include <bsd/stdlib.h> on Linux and <stdlib.h> on other
platforms.
2017-11-18 16:48:52 -05:00
Nathan Moinvaziri 0d4ae83bd2 Update README.md 2017-11-15 22:01:36 -08:00
Nathan Moinvaziri d15b013734 Merge branch 'master' of https://github.com/nmoinvaz/minizip 2017-11-15 22:00:04 -08:00
Nathan Moinvaziri a3dc63a17f Merge pull request #207 from GerbilSoft/bugfix/mz_compat.fix-wrapper-functions
mz_compat.c: Fix zipOpen(), zipOpen64(), and unzOpen64().
2017-11-12 12:35:42 -08:00
David Korth be85b3b4cd mz_compat.c: Fix zipOpen(), zipOpen64(), and unzOpen64().
The previous bugfixes fixed the functions that took a
zlib_filefunc_def* parameter, but they inadvertently broke the
functions that didn't. The fix is to store the filefunc pointer
locally, then pass a pointer to the pointer.
2017-11-12 11:32:40 -05:00
Nathan Moinvaziri 3da621e118 Fixed formatting. 2017-11-11 14:29:25 -08:00
Nathan Moinvaziri 211f14074a Merge pull request #206 from GerbilSoft/bugfix/fix-mz_compat-2.2.3.v2
Fixes for the mz_compat layer.
2017-11-11 14:25:59 -08:00
David Korth 819d0d56f0 test.c: Use "%u" for printing uint32_t, not "%d".
Found using cppcheck-1.81.
2017-11-11 08:16:32 -05:00
David Korth fa7fadebfa minizip.c: Check for 'k' and 'K', not 'k' and 'k'.
Found using cppcheck-1.81.
2017-11-11 08:16:32 -05:00
David Korth a6e7d468e3 mz_compat.c: unzOpen2_64(): Go to the first file immediately.
MiniZip 1.x does this.

TODO: MiniZip 1.x also goes to the first disk when opening a Zip file
for write. Not sure if we should (or can) do that too...
2017-11-11 08:16:32 -05:00
David Korth b8f7cf0fa7 mz_compat.c: unzGetCurrentFileInfo64(): Make sure filename and comment are NULL-terminated.
MiniZip 1.x always NULL-terminated these fields, but MiniZip 2.x does
not. This may break some programs that expect NULL-terminated fields
instead of using the explicit length.

Add a NULL terminator if there's enough space in the buffers.
2017-11-11 08:16:32 -05:00
David Korth 886df95632 mz_compat.c: unzClose(): Close the stream before destroying it.
Otherwise, we end up leaking the file handle. On Windows, the file is
opened with exclusive (non-shared) access, which prevents us from
opening the file again until the program exits.
2017-11-11 08:16:32 -05:00
David Korth 80c2d56436 mz_compat.c: Correctly dereference pzlib_filefunc_def.
In the fill_*() functions, not dereferencing `pzlib_filefunc_def`
results in the functions acting like nops.

In zipOpen2_64(), not dereferencing `pzlib_filefunc_def` causes it
to use the wrong pointer as the vtbl.
2017-11-11 08:16:32 -05:00
61 changed files with 3020 additions and 2461 deletions
-5
View File
@@ -1,5 +0,0 @@
# Xcode
xcuserdata/
# JetBrains
.idea/
-7
View File
@@ -7,13 +7,8 @@ compiler:
- clang
- gcc
python:
- "2.7"
before_install:
- sudo apt-get install libbsd-dev
- sudo pip install --upgrade pip
- sudo pip install numpy
before_script:
- mkdir build
@@ -22,5 +17,3 @@ before_script:
script:
- make
- cd ../test
- python test.py --exe_dir=../build
+143 -70
View File
@@ -3,31 +3,31 @@
# E-Mail: schmieder.matthias@gmail.com
# Year: 2016
#***************************************************************************
cmake_minimum_required(VERSION 2.8)
cmake_minimum_required(VERSION 3.0.2)
option(USE_ZLIB "Enables building with ZLIB library" ON)
option(USE_BZIP2 "Enables building with BZIP2 library" ON)
option(USE_LZMA "Enables building with LZMA library" ON)
option(USE_CRYPT "Enables building with PKWARE traditional encryption" ON)
option(USE_AES "Enables building with AES library" ON)
option(BUILD_TEST "Enables building of minizip executable." OFF)
# 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()
set(INSTALL_BIN_DIR "bin" CACHE PATH "Installation directory for executables")
set(INSTALL_LIB_DIR "lib" CACHE PATH "Installation directory for libraries")
set(INSTALL_INC_DIR "include" CACHE PATH "Installation directory for headers")
set(INSTALL_MAN_DIR "share/man" CACHE PATH "Installation directory for manual pages")
set(INSTALL_PKGCONFIG_DIR "share/pkgconfig" CACHE PATH "Installation directory for pkgconfig (.pc) files")
option(USE_ZLIB "Enables ZLIB compression" ON)
option(USE_BZIP2 "Enables BZIP2 compression" ON)
option(USE_LZMA "Enables LZMA compression" ON)
option(USE_PKCRYPT "Enables PKWARE traditional encryption" ON)
option(USE_AES "Enables AES encryption" ON)
option(COMPRESS_ONLY "Only support compression" OFF)
option(DECOMPRESS_ONLY "Only support decompression" OFF)
option(BUILD_TEST "Builds minizip test executable" OFF)
project("minizip")
set(VERSION "2.2.4")
include(CheckSymbolExists)
include(GNUInstallDirs)
set(INSTALL_BIN_DIR ${CMAKE_INSTALL_BINDIR} CACHE PATH "Installation directory for executables")
set(INSTALL_LIB_DIR ${CMAKE_INSTALL_LIBDIR} CACHE PATH "Installation directory for libraries")
set(INSTALL_INC_DIR ${CMAKE_INSTALL_INCLUDEDIR} CACHE PATH "Installation directory for headers")
set(INSTALL_MAN_DIR ${CMAKE_INSTALL_MANDIR} CACHE PATH "Installation directory for manual pages")
set(INSTALL_PKGCONFIG_DIR ${CMAKE_INSTALL_LIBDIR}/pkgconfig CACHE PATH "Installation directory for pkgconfig (.pc) files")
set(INSTALL_CMAKE_DIR ${CMAKE_INSTALL_LIBDIR}/cmake/minizip CACHE PATH "Installation directory for cmake files.")
# Minizip library version
set(VERSION "2.3.9")
# Set cmake debug postfix to d
set(CMAKE_DEBUG_POSTFIX "d")
@@ -39,6 +39,7 @@ if(USE_ZLIB)
if(ZLIB_FOUND)
include_directories(${ZLIB_INCLUDE_DIRS})
endif()
add_definitions(-DHAVE_ZLIB)
endif()
set(MINIZIP_PC ${CMAKE_CURRENT_BINARY_DIR}/minizip.pc)
@@ -47,83 +48,126 @@ configure_file(${CMAKE_CURRENT_SOURCE_DIR}/minizip.pc.cmakein ${MINIZIP_PC} @ONL
set(PROJECT_NAME libminizip)
set(MINIZIP_SRC
src/mz_os.c
src/mz_compat.c
src/mz_strm.c
src/mz_strm_buf.c
src/mz_strm_mem.c
src/mz_strm_posix.c
src/mz_strm_split.c
src/mz_strm_zlib.c
src/mz_zip.c)
mz_os.c
mz_compat.c
mz_strm.c
mz_strm_buf.c
mz_strm_crc32.c
mz_strm_mem.c
mz_strm_posix.c
mz_strm_split.c
mz_zip.c)
set(MINIZIP_PUBLIC_HEADERS
src/mz.h
src/mz_os.h
src/mz_compat.h
src/mz_strm.h
src/mz_strm_buf.h
src/mz_strm_mem.h
src/mz_strm_posix.h
src/mz_strm_split.h
src/mz_strm_zlib.h
src/mz_zip.h)
mz.h
mz_os.h
mz_compat.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_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()
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
if(WIN32)
list(APPEND MINIZIP_SRC "src/mz_os_win32.c" "src/mz_strm_win32.c")
list(APPEND MINIZIP_PUBLIC_HEADERS "src/mz_os_win32.h" "src/mz_strm_win32.h")
list(APPEND MINIZIP_SRC "mz_os_win32.c" "mz_strm_win32.c")
list(APPEND MINIZIP_PUBLIC_HEADERS "mz_os_win32.h" "mz_strm_win32.h")
add_definitions(-D_CRT_SECURE_NO_DEPRECATE)
endif()
if("${CMAKE_SYSTEM_NAME}" STREQUAL "WindowsStore")
add_definitions(-DMZ_USE_WINRT_API)
add_definitions(-DMZ_WINRT_API)
endif()
if(UNIX)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC -O3")
add_compile_options(-O3)
list(APPEND MINIZIP_SRC "src/mz_os_posix.c")
list(APPEND MINIZIP_PUBLIC_HEADERS "src/mz_os_posix.h")
list(APPEND MINIZIP_SRC "mz_os_posix.c")
list(APPEND MINIZIP_PUBLIC_HEADERS "mz_os_posix.h")
add_definitions(-D__USE_FILE_OFFSET64)
add_definitions(-D__USE_LARGEFILE64)
add_definitions(-D_LARGEFILE64_SOURCE)
add_definitions(-D_FILE_OFFSET_BIT=64)
set(define_lfs_macros TRUE)
find_package(PkgConfig REQUIRED)
if(ANDROID)
string(REGEX REPLACE "android-([0-9+])" "\\1"
android_api "${ANDROID_PLATFORM}")
if(${android_api} LESS 24)
set(define_lfs_macros FALSE)
endif()
endif()
pkg_check_modules(LIBBSD libbsd)
if(define_lfs_macros)
add_definitions(-D__USE_FILE_OFFSET64)
add_definitions(-D__USE_LARGEFILE64)
add_definitions(-D_LARGEFILE64_SOURCE)
add_definitions(-D_FILE_OFFSET_BITS=64)
endif()
set(_bsd "")
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
find_package(PkgConfig REQUIRED)
pkg_check_modules(LIBBSD libbsd)
if (LIBBSD_FOUND)
add_definitions(-DHAVE_LIBBSD)
include_directories(${LIBBSD_INCLUDE_DIRS})
link_directories(${LIBBSD_LIBRARY_DIRS})
set(_bsd "bsd/")
endif()
endif()
check_symbol_exists("getrandom" "sys/random.h" HAVE_GETRANDOM)
if (HAVE_GETRANDOM)
add_definitions(-DHAVE_GETRANDOM)
endif()
check_symbol_exists("arc4random_buf" "${_bsd}stdlib.h" HAVE_ARC4RANDOM_BUF)
if (HAVE_ARC4RANDOM_BUF)
add_definitions(-DHAVE_ARC4RANDOM_BUF)
else()
check_symbol_exists("arc4random" "${_bsd}stdlib.h" HAVE_ARC4RANDOM)
if (HAVE_ARC4RANDOM)
add_definitions(-DHAVE_ARC4RANDOM)
endif()
endif()
include_directories(${LIBBSD_INCLUDE_DIRS})
link_directories(${LIBBSD_LIBRARY_DIRS})
endif()
if(USE_CRYPT)
add_definitions(-DHAVE_CRYPT)
if(USE_PKCRYPT)
add_definitions(-DHAVE_PKCRYPT)
list(APPEND MINIZIP_SRC "src/mz_strm_crypt.c")
list(APPEND MINIZIP_PUBLIC_HEADERS "src/mz_strm_crypt.h")
list(APPEND MINIZIP_SRC "mz_strm_pkcrypt.c")
list(APPEND MINIZIP_PUBLIC_HEADERS "mz_strm_pkcrypt.h")
endif()
if(USE_AES)
add_definitions(-DHAVE_AES)
list(APPEND MINIZIP_SRC "src/mz_strm_aes.c")
list(APPEND MINIZIP_PUBLIC_HEADERS "src/mz_strm_aes.h")
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/aes_ni.c
lib/aes/hmac.c
lib/aes/pwd2key.c
lib/aes/sha1.c)
set(AES_PUBLIC_HEADERS
lib/aes/aes.h
lib/aes/aes_ni.h
lib/aes/aesopt.h
lib/aes/aestab.h
lib/aes/brg_endian.h
@@ -139,13 +183,26 @@ endif()
if(USE_ZLIB)
add_definitions(-DHAVE_ZLIB)
list(APPEND MINIZIP_SRC "mz_strm_zlib.c")
list(APPEND MINIZIP_PUBLIC_HEADERS "mz_strm_zlib.h")
include(CheckFunctionExists)
set(CMAKE_REQUIRED_LIBRARIES ZLIB::ZLIB)
CHECK_FUNCTION_EXISTS(z_get_crc_table
NEEDS_Z_PREFIX)
if(NEEDS_Z_PREFIX)
add_definitions(-DZ_PREFIX)
endif()
endif()
if(USE_BZIP2)
add_definitions(-DHAVE_BZIP2)
add_definitions(-DBZ_NO_STDIO)
list(APPEND MINIZIP_SRC "src/mz_strm_bzip.c")
list(APPEND MINIZIP_PUBLIC_HEADERS "src/mz_strm_bzip.h")
list(APPEND MINIZIP_SRC "mz_strm_bzip.c")
list(APPEND MINIZIP_PUBLIC_HEADERS "mz_strm_bzip.h")
set(BZIP2_SRC
lib/bzip2/blocksort.c
@@ -170,8 +227,8 @@ if(USE_LZMA)
add_definitions(-DHAVE_CONFIG_H)
add_definitions(-DLZMA_API_STATIC)
list(APPEND MINIZIP_SRC "src/mz_strm_lzma.c")
list(APPEND MINIZIP_PUBLIC_HEADERS "src/mz_strm_lzma.h")
list(APPEND MINIZIP_SRC "mz_strm_lzma.c")
list(APPEND MINIZIP_PUBLIC_HEADERS "mz_strm_lzma.h")
set(LZMA_CHECK_SRC
lib/liblzma/check/check.c
@@ -273,8 +330,19 @@ if(USE_LZMA)
source_group("LZMA\\RangeCoder" FILES ${LZMA_RANGECODER_SRC} ${LZMA_RANGECODER_HEADERS})
endif()
# Enable x86 optimizations if supported
if(CMAKE_C_COMPILER MATCHES ".*clang")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -msse3 -msse4.1 -maes")
include(CheckCCompilerFlag)
macro(enable_option_if_supported option variable)
check_c_compiler_flag("-Werror=unused-command-line-argument ${option}" ${variable})
if(${variable})
add_compile_options(${option})
endif()
endmacro()
enable_option_if_supported(-msse3 check_opt_sse3)
enable_option_if_supported(-msse4.1 check_opt_sse41)
enable_option_if_supported(-maes check_opt_aes)
endif()
# Create minizip library
@@ -286,7 +354,12 @@ add_library(${PROJECT_NAME}
${BZIP2_SRC} ${BZIP2_PUBLIC_HEADERS}
${LZMA_SRC} ${LZMA_PUBLIC_HEADERS})
set_target_properties(${PROJECT_NAME} PROPERTIES LINKER_LANGUAGE C PREFIX "")
if (MINGW AND BUILD_SHARED_LIBS)
set_target_properties(${PROJECT_NAME} PROPERTIES ARCHIVE_OUTPUT_NAME "minizip")
endif ()
set_target_properties(${PROJECT_NAME} PROPERTIES LINKER_LANGUAGE C PREFIX ""
POSITION_INDEPENDENT_CODE 1)
if(USE_ZLIB)
target_link_libraries(${PROJECT_NAME} ZLIB::ZLIB)
endif()
@@ -306,14 +379,14 @@ install(TARGETS ${PROJECT_NAME} EXPORT ${PROJECT_NAME}
LIBRARY DESTINATION "${INSTALL_LIB_DIR}")
install(EXPORT ${PROJECT_NAME}
DESTINATION "cmake"
DESTINATION "${INSTALL_CMAKE_DIR}"
NAMESPACE "MINIZIP::")
install(FILES ${MINIZIP_PUBLIC_HEADERS} DESTINATION "${INSTALL_INC_DIR}")
install(FILES ${MINIZIP_PC} DESTINATION "${INSTALL_PKGCONFIG_DIR}")
if(BUILD_TEST)
add_executable(minizip "src/minizip.c")
add_executable(minizip "minizip.c")
target_link_libraries(minizip ${PROJECT_NAME})
install(TARGETS minizip
+2 -2
View File
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'Minizip'
s.version = '2.2.4'
s.version = '2.3.9'
s.license = 'zlib'
s.summary = 'Minizip contrib in zlib with the latest bug fixes and advanced features'
s.description = <<-DESC
@@ -17,7 +17,7 @@ DESC
s.libraries = 'z'
s.subspec 'Core' do |sp|
sp.source_files = 'src/{mz_os,mz_compat,mz_strm,mz_strm_mem,mz_strm_buf,mz_zip,mz_strm_crypt,mz_strm_posix,mz_strm_zlib}.{c,h}'
sp.source_files = '{mz_os,mz_compat,mz_strm,mz_strm_mem,mz_strm_buf,mz_zip,mz_strm_crypt,mz_strm_posix,mz_strm_zlib}.{c,h}'
end
s.subspec 'AES' do |sp|
+55 -19
View File
@@ -1,6 +1,6 @@
# Minizip 2.2.4
# Minizip 2.3.9
This library is a refactoring of the minizip contribution found in the zlib distribution that 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.
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.
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)
@@ -10,11 +10,49 @@ For the original work maintained by Mark Adler checkout the zlib minizip [contr
## Build
To generate the project files for your platform and IDE download and run cmake in the project directory.
To generate project files for your platform:
1. [Download and install](https://cmake.org/install/) cmake.
2. [Download](https://zlib.net/) and install zlib if it is not installed on your system.
3. Run cmake in the minizip directory.
```
cmake . -DBUILD_TEST=ON
cmake --build .
```
## Build Options
| Name | Description | Default Value |
|:- |:-|:-:|
| USE_ZLIB | Enables ZLIB compression | ON |
| USE_BZIP2 | Enables BZIP2 compression | ON |
| USE_LZMA | Enables LZMA compression | ON |
| USE_PKCRYPT | Enables PKWARE traditional encryption | ON |
| COMPRESS_ONLY | Only support compression | OFF |
| DECOMPRESS_ONLY | Only support decompression | OFF |
| BUILD_TEST | Builds minizip test executable | OFF |
## Zlib Installation (Windows)
Option 1. Install the zlib package to the Program Files directory with an Administrator command prompt.
```
cmake . -DCMAKE_INSTALL_PREFIX=%PROGRAMFILES%\zlib
cmake --build . --config Release --target INSTALL
```
Option 2. Compile zlib in minizip's lib directory.
```
cmake .
cmake --build .
cmake --build . --config Release
```
Navigate back to the minizip directory and before building run:
```
cmake . -DZLIB_LIBRARY=lib\zlib\release\zlibstatic.lib -DZLIB_INCLUDE_DIR=lib\zlib\
```
## Contents
@@ -29,13 +67,14 @@ cmake --build .
| mz_strm_aes.\* | WinZIP AES stream | No |
| mz_strm_buf.\* | Buffered stream | No |
| mz_strm_bzip.\* | BZIP2 stream using libbzip2 | No |
| mz_strm_crypt.\* | PKWARE traditional encryption stream | No |
| mz_strm_lzma.\* | LZMA stream using liblzma | No |
| mz_strm_crc32.\* | CRC32 stream | Yes |
| mz_strm_lzma.\* | LZMA stream using liblzma | zlib or liblzma |
| mz_strm_mem.\* | Memory stream | Yes |
| mz_strm_split.\* | Disk splitting stream | No |
| mz_strm_pkcrypt.\* | PKWARE traditional encryption stream | No |
| mz_strm_posix.\* | File stream using Posix functions | Non-windows systems |
| mz_strm_win32.\* | File stream using Win32 API functions | Windows systems |
| mz_strm_zlib.\* | Deflate stream using zlib | Yes |
| mz_strm_zlib.\* | Deflate stream using zlib | zlib or liblzma |
| mz_zip.\* | Zip functionality | Yes |
## Features
@@ -54,10 +93,10 @@ cmake --build .
### Encryption
#### [WinZIP AES Encryption](http://www.winzip.com/aes_info.htm)
#### [WinZIP AES Encryption](https://www.winzip.com/aes_info.htm)
+ Requires ``cmake . -DUSE_AES=ON`` or ``#define HAVE_AES``
+ Requires [Brian Gladman's](https://github.com/BrianGladman/aes) AES library
+ 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.
@@ -68,7 +107,7 @@ To disable encryption use the following cmake commands:
```
cmake . -DUSE_AES=OFF
cmake . -DUSE_CRYPT=OFF
cmake . -DUSE_PKCRYPT=OFF
```
### NTFS Timestamps
@@ -81,7 +120,7 @@ This library has been refactored around streams.
#### Memory Streaming
To unzip from a zip file in memory create a memory stream and pass it to the unzip open functions.
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;
@@ -98,7 +137,7 @@ void *zip_handle = mz_zip_open(mem_stream, MZ_OPEN_MODE_READ);
mz_stream_mem_delete(&mem_stream);
```
To create a zip file in memory first create a growable memory stream and pass it to the zip open functions.
To create a zip file in memory first create a growable memory stream and pass it to the open function.
```
void *mem_stream = NULL;
@@ -135,7 +174,7 @@ 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 for zipping supply a disk size value in bytes.
To create an archive with multiple disks use the disk splitting stream and supply a disk size value in bytes.
```
void *stream = NULL;
@@ -150,19 +189,16 @@ mz_stream_set_base(split_stream, stream);
mz_stream_open(split_stream, path..
handle = mz_zip_open(split_stream, MZ_OPEN_MODE_WRITE);
void *zip_handle = mz_zip_open(split_stream, MZ_OPEN_MODE_WRITE);
```
The central directory is the only data stored in the .zip and doesn't follow disk size restrictions.
When unzipping it will automatically determine when in needs to cross disk boundaries.
### Windows RT
+ Requires ``#define MZ_USE_WINRT_API``
+ 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.
-687
View File
@@ -1,687 +0,0 @@
/*
Copyright (c) 1998-2013, 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: 09/09/2014
*/
#include "aes_ni.h"
#if defined( USE_INTEL_AES_IF_PRESENT )
#if defined(_MSC_VER)
#include <intrin.h>
#pragma intrinsic(__cpuid)
#define INLINE __inline
INLINE int has_aes_ni(void)
{
static int test = -1;
if(test < 0)
{
int cpu_info[4];
__cpuid(cpu_info, 1);
test = cpu_info[2] & 0x02000000;
}
return test;
}
#elif defined( __GNUC__ )
#include <cpuid.h>
#if !defined(__clang__)
#pragma GCC target ("ssse3")
#pragma GCC target ("sse4.1")
#pragma GCC target ("aes")
#endif
#include <x86intrin.h>
#define INLINE static __inline
INLINE int has_aes_ni()
{
static int test = -1;
if(test < 0)
{
unsigned int a, b, c, d;
if(!__get_cpuid(1, &a, &b, &c, &d))
test = 0;
else
test = (c & 0x2000000);
}
return test;
}
#else
#error AES New Instructions require Microsoft, Intel, GNU C, or CLANG
#endif
INLINE __m128i aes_128_assist(__m128i t1, __m128i t2)
{
__m128i t3;
t2 = _mm_shuffle_epi32(t2, 0xff);
t3 = _mm_slli_si128(t1, 0x4);
t1 = _mm_xor_si128(t1, t3);
t3 = _mm_slli_si128(t3, 0x4);
t1 = _mm_xor_si128(t1, t3);
t3 = _mm_slli_si128(t3, 0x4);
t1 = _mm_xor_si128(t1, t3);
t1 = _mm_xor_si128(t1, t2);
return t1;
}
AES_RETURN aes_ni(encrypt_key128)(const unsigned char *key, aes_encrypt_ctx cx[1])
{
__m128i t1, t2;
__m128i *ks = (__m128i*)cx->ks;
if(!has_aes_ni())
{
return aes_xi(encrypt_key128)(key, cx);
}
t1 = _mm_loadu_si128((__m128i*)key);
ks[0] = t1;
t2 = _mm_aeskeygenassist_si128(t1, 0x1);
t1 = aes_128_assist(t1, t2);
ks[1] = t1;
t2 = _mm_aeskeygenassist_si128(t1, 0x2);
t1 = aes_128_assist(t1, t2);
ks[2] = t1;
t2 = _mm_aeskeygenassist_si128(t1, 0x4);
t1 = aes_128_assist(t1, t2);
ks[3] = t1;
t2 = _mm_aeskeygenassist_si128(t1, 0x8);
t1 = aes_128_assist(t1, t2);
ks[4] = t1;
t2 = _mm_aeskeygenassist_si128(t1, 0x10);
t1 = aes_128_assist(t1, t2);
ks[5] = t1;
t2 = _mm_aeskeygenassist_si128(t1, 0x20);
t1 = aes_128_assist(t1, t2);
ks[6] = t1;
t2 = _mm_aeskeygenassist_si128(t1, 0x40);
t1 = aes_128_assist(t1, t2);
ks[7] = t1;
t2 = _mm_aeskeygenassist_si128(t1, 0x80);
t1 = aes_128_assist(t1, t2);
ks[8] = t1;
t2 = _mm_aeskeygenassist_si128(t1, 0x1b);
t1 = aes_128_assist(t1, t2);
ks[9] = t1;
t2 = _mm_aeskeygenassist_si128(t1, 0x36);
t1 = aes_128_assist(t1, t2);
ks[10] = t1;
cx->inf.l = 0;
cx->inf.b[0] = 10 * 16;
return EXIT_SUCCESS;
}
INLINE void aes_192_assist(__m128i* t1, __m128i * t2, __m128i * t3)
{
__m128i t4;
*t2 = _mm_shuffle_epi32(*t2, 0x55);
t4 = _mm_slli_si128(*t1, 0x4);
*t1 = _mm_xor_si128(*t1, t4);
t4 = _mm_slli_si128(t4, 0x4);
*t1 = _mm_xor_si128(*t1, t4);
t4 = _mm_slli_si128(t4, 0x4);
*t1 = _mm_xor_si128(*t1, t4);
*t1 = _mm_xor_si128(*t1, *t2);
*t2 = _mm_shuffle_epi32(*t1, 0xff);
t4 = _mm_slli_si128(*t3, 0x4);
*t3 = _mm_xor_si128(*t3, t4);
*t3 = _mm_xor_si128(*t3, *t2);
}
AES_RETURN aes_ni(encrypt_key192)(const unsigned char *key, aes_encrypt_ctx cx[1])
{
__m128i t1, t2, t3;
__m128i *ks = (__m128i*)cx->ks;
if(!has_aes_ni())
{
return aes_xi(encrypt_key192)(key, cx);
}
t1 = _mm_loadu_si128((__m128i*)key);
t3 = _mm_loadu_si128((__m128i*)(key + 16));
ks[0] = t1;
ks[1] = t3;
t2 = _mm_aeskeygenassist_si128(t3, 0x1);
aes_192_assist(&t1, &t2, &t3);
ks[1] = _mm_castpd_si128(_mm_shuffle_pd(_mm_castsi128_pd(ks[1]), _mm_castsi128_pd(t1), 0));
ks[2] = _mm_castpd_si128(_mm_shuffle_pd(_mm_castsi128_pd(t1), _mm_castsi128_pd(t3), 1));
t2 = _mm_aeskeygenassist_si128(t3, 0x2);
aes_192_assist(&t1, &t2, &t3);
ks[3] = t1;
ks[4] = t3;
t2 = _mm_aeskeygenassist_si128(t3, 0x4);
aes_192_assist(&t1, &t2, &t3);
ks[4] = _mm_castpd_si128(_mm_shuffle_pd(_mm_castsi128_pd(ks[4]), _mm_castsi128_pd(t1), 0));
ks[5] = _mm_castpd_si128(_mm_shuffle_pd(_mm_castsi128_pd(t1), _mm_castsi128_pd(t3), 1));
t2 = _mm_aeskeygenassist_si128(t3, 0x8);
aes_192_assist(&t1, &t2, &t3);
ks[6] = t1;
ks[7] = t3;
t2 = _mm_aeskeygenassist_si128(t3, 0x10);
aes_192_assist(&t1, &t2, &t3);
ks[7] = _mm_castpd_si128(_mm_shuffle_pd(_mm_castsi128_pd(ks[7]), _mm_castsi128_pd(t1), 0));
ks[8] = _mm_castpd_si128(_mm_shuffle_pd(_mm_castsi128_pd(t1), _mm_castsi128_pd(t3), 1));
t2 = _mm_aeskeygenassist_si128(t3, 0x20);
aes_192_assist(&t1, &t2, &t3);
ks[9] = t1;
ks[10] = t3;
t2 = _mm_aeskeygenassist_si128(t3, 0x40);
aes_192_assist(&t1, &t2, &t3);
ks[10] = _mm_castpd_si128(_mm_shuffle_pd(_mm_castsi128_pd(ks[10]), _mm_castsi128_pd(t1), 0));
ks[11] = _mm_castpd_si128(_mm_shuffle_pd(_mm_castsi128_pd(t1), _mm_castsi128_pd(t3), 1));
t2 = _mm_aeskeygenassist_si128(t3, 0x80);
aes_192_assist(&t1, &t2, &t3);
ks[12] = t1;
cx->inf.l = 0;
cx->inf.b[0] = 12 * 16;
return EXIT_SUCCESS;
}
INLINE void aes_256_assist1(__m128i* t1, __m128i * t2)
{
__m128i t4;
*t2 = _mm_shuffle_epi32(*t2, 0xff);
t4 = _mm_slli_si128(*t1, 0x4);
*t1 = _mm_xor_si128(*t1, t4);
t4 = _mm_slli_si128(t4, 0x4);
*t1 = _mm_xor_si128(*t1, t4);
t4 = _mm_slli_si128(t4, 0x4);
*t1 = _mm_xor_si128(*t1, t4);
*t1 = _mm_xor_si128(*t1, *t2);
}
INLINE void aes_256_assist2(__m128i* t1, __m128i * t3)
{
__m128i t2, t4;
t4 = _mm_aeskeygenassist_si128(*t1, 0x0);
t2 = _mm_shuffle_epi32(t4, 0xaa);
t4 = _mm_slli_si128(*t3, 0x4);
*t3 = _mm_xor_si128(*t3, t4);
t4 = _mm_slli_si128(t4, 0x4);
*t3 = _mm_xor_si128(*t3, t4);
t4 = _mm_slli_si128(t4, 0x4);
*t3 = _mm_xor_si128(*t3, t4);
*t3 = _mm_xor_si128(*t3, t2);
}
AES_RETURN aes_ni(encrypt_key256)(const unsigned char *key, aes_encrypt_ctx cx[1])
{
__m128i t1, t2, t3;
__m128i *ks = (__m128i*)cx->ks;
if(!has_aes_ni())
{
return aes_xi(encrypt_key256)(key, cx);
}
t1 = _mm_loadu_si128((__m128i*)key);
t3 = _mm_loadu_si128((__m128i*)(key + 16));
ks[0] = t1;
ks[1] = t3;
t2 = _mm_aeskeygenassist_si128(t3, 0x01);
aes_256_assist1(&t1, &t2);
ks[2] = t1;
aes_256_assist2(&t1, &t3);
ks[3] = t3;
t2 = _mm_aeskeygenassist_si128(t3, 0x02);
aes_256_assist1(&t1, &t2);
ks[4] = t1;
aes_256_assist2(&t1, &t3);
ks[5] = t3;
t2 = _mm_aeskeygenassist_si128(t3, 0x04);
aes_256_assist1(&t1, &t2);
ks[6] = t1;
aes_256_assist2(&t1, &t3);
ks[7] = t3;
t2 = _mm_aeskeygenassist_si128(t3, 0x08);
aes_256_assist1(&t1, &t2);
ks[8] = t1;
aes_256_assist2(&t1, &t3);
ks[9] = t3;
t2 = _mm_aeskeygenassist_si128(t3, 0x10);
aes_256_assist1(&t1, &t2);
ks[10] = t1;
aes_256_assist2(&t1, &t3);
ks[11] = t3;
t2 = _mm_aeskeygenassist_si128(t3, 0x20);
aes_256_assist1(&t1, &t2);
ks[12] = t1;
aes_256_assist2(&t1, &t3);
ks[13] = t3;
t2 = _mm_aeskeygenassist_si128(t3, 0x40);
aes_256_assist1(&t1, &t2);
ks[14] = t1;
cx->inf.l = 0;
cx->inf.b[0] = 14 * 16;
return EXIT_SUCCESS;
}
INLINE void enc_to_dec(aes_decrypt_ctx cx[1])
{
__m128i *ks = (__m128i*)cx->ks;
int j;
for( j = 1 ; j < (cx->inf.b[0] >> 4) ; ++j )
ks[j] = _mm_aesimc_si128(ks[j]);
}
AES_RETURN aes_ni(decrypt_key128)(const unsigned char *key, aes_decrypt_ctx cx[1])
{
if(!has_aes_ni())
{
return aes_xi(decrypt_key128)(key, cx);
}
if(aes_ni(encrypt_key128)(key, (aes_encrypt_ctx*)cx) == EXIT_SUCCESS)
{
enc_to_dec(cx);
return EXIT_SUCCESS;
}
else
return EXIT_FAILURE;
}
AES_RETURN aes_ni(decrypt_key192)(const unsigned char *key, aes_decrypt_ctx cx[1])
{
if(!has_aes_ni())
{
return aes_xi(decrypt_key192)(key, cx);
}
if(aes_ni(encrypt_key192)(key, (aes_encrypt_ctx*)cx) == EXIT_SUCCESS)
{
enc_to_dec(cx);
return EXIT_SUCCESS;
}
else
return EXIT_FAILURE;
}
AES_RETURN aes_ni(decrypt_key256)(const unsigned char *key, aes_decrypt_ctx cx[1])
{
if(!has_aes_ni())
{
return aes_xi(decrypt_key256)(key, cx);
}
if(aes_ni(encrypt_key256)(key, (aes_encrypt_ctx*)cx) == EXIT_SUCCESS)
{
enc_to_dec(cx);
return EXIT_SUCCESS;
}
else
return EXIT_FAILURE;
}
AES_RETURN aes_ni(encrypt)(const unsigned char *in, unsigned char *out, const aes_encrypt_ctx cx[1])
{
__m128i *key = (__m128i*)cx->ks, t;
if(cx->inf.b[0] != 10 * 16 && cx->inf.b[0] != 12 * 16 && cx->inf.b[0] != 14 * 16)
return EXIT_FAILURE;
if(!has_aes_ni())
{
return aes_xi(encrypt)(in, out, cx);
}
t = _mm_xor_si128(_mm_loadu_si128((__m128i*)in), *(__m128i*)key);
switch(cx->inf.b[0])
{
case 14 * 16:
t = _mm_aesenc_si128(t, *(__m128i*)++key);
t = _mm_aesenc_si128(t, *(__m128i*)++key);
case 12 * 16:
t = _mm_aesenc_si128(t, *(__m128i*)++key);
t = _mm_aesenc_si128(t, *(__m128i*)++key);
case 10 * 16:
t = _mm_aesenc_si128(t, *(__m128i*)++key);
t = _mm_aesenc_si128(t, *(__m128i*)++key);
t = _mm_aesenc_si128(t, *(__m128i*)++key);
t = _mm_aesenc_si128(t, *(__m128i*)++key);
t = _mm_aesenc_si128(t, *(__m128i*)++key);
t = _mm_aesenc_si128(t, *(__m128i*)++key);
t = _mm_aesenc_si128(t, *(__m128i*)++key);
t = _mm_aesenc_si128(t, *(__m128i*)++key);
t = _mm_aesenc_si128(t, *(__m128i*)++key);
t = _mm_aesenclast_si128(t, *(__m128i*)++key);
}
_mm_storeu_si128(&((__m128i*)out)[0], t);
return EXIT_SUCCESS;
}
AES_RETURN aes_ni(decrypt)(const unsigned char *in, unsigned char *out, const aes_decrypt_ctx cx[1])
{
__m128i *key = (__m128i*)cx->ks + (cx->inf.b[0] >> 4), t;
if(cx->inf.b[0] != 10 * 16 && cx->inf.b[0] != 12 * 16 && cx->inf.b[0] != 14 * 16)
return EXIT_FAILURE;
if(!has_aes_ni())
{
return aes_xi(decrypt)(in, out, cx);
}
t = _mm_xor_si128(_mm_loadu_si128((__m128i*)in), *(__m128i*)key);
switch(cx->inf.b[0])
{
case 14 * 16:
t = _mm_aesdec_si128(t, *(__m128i*)--key);
t = _mm_aesdec_si128(t, *(__m128i*)--key);
case 12 * 16:
t = _mm_aesdec_si128(t, *(__m128i*)--key);
t = _mm_aesdec_si128(t, *(__m128i*)--key);
case 10 * 16:
t = _mm_aesdec_si128(t, *(__m128i*)--key);
t = _mm_aesdec_si128(t, *(__m128i*)--key);
t = _mm_aesdec_si128(t, *(__m128i*)--key);
t = _mm_aesdec_si128(t, *(__m128i*)--key);
t = _mm_aesdec_si128(t, *(__m128i*)--key);
t = _mm_aesdec_si128(t, *(__m128i*)--key);
t = _mm_aesdec_si128(t, *(__m128i*)--key);
t = _mm_aesdec_si128(t, *(__m128i*)--key);
t = _mm_aesdec_si128(t, *(__m128i*)--key);
t = _mm_aesdeclast_si128(t, *(__m128i*)--key);
}
_mm_storeu_si128((__m128i*)out, t);
return EXIT_SUCCESS;
}
#ifdef ADD_AESNI_MODE_CALLS
#ifdef USE_AES_CONTEXT
AES_RETURN aes_CBC_encrypt(const unsigned char *in,
unsigned char *out,
unsigned char ivec[16],
unsigned long length,
const aes_encrypt_ctx cx[1])
{
__m128i feedback, data, *key = (__m128i*)cx->ks;
int number_of_rounds = cx->inf.b[0] >> 4, j;
unsigned long i;
if(number_of_rounds != 10 && number_of_rounds != 12 && number_of_rounds != 14)
return EXIT_FAILURE;
if(!has_aes_ni())
{
return aes_cbc_encrypt(in, out, length, ivec, cx);
}
if(length % 16)
length = length / 16 + 1;
else length /= 16;
feedback = _mm_loadu_si128((__m128i*)ivec);
for(i = 0; i < length; i++)
{
data = _mm_loadu_si128(&((__m128i*)in)[i]);
feedback = _mm_xor_si128(data, feedback);
feedback = _mm_xor_si128(feedback, ((__m128i*)key)[0]);
for(j = 1; j <number_of_rounds; j++)
feedback = _mm_aesenc_si128(feedback, ((__m128i*)key)[j]);
feedback = _mm_aesenclast_si128(feedback, ((__m128i*)key)[j]);
_mm_storeu_si128(&((__m128i*)out)[i], feedback);
}
return EXIT_SUCCESS;
}
AES_RETURN aes_CBC_decrypt(const unsigned char *in,
unsigned char *out,
unsigned char ivec[16],
unsigned long length,
const aes_decrypt_ctx cx[1])
{
__m128i data, feedback, last_in, *key = (__m128i*)cx->ks;
int number_of_rounds = cx->inf.b[0] >> 4, j;
unsigned long i;
if(number_of_rounds != 10 && number_of_rounds != 12 && number_of_rounds != 14)
return EXIT_FAILURE;
if(!has_aes_ni())
{
return aes_cbc_decrypt(in, out, length, ivec, cx);
}
if(length % 16)
length = length / 16 + 1;
else length /= 16;
feedback = _mm_loadu_si128((__m128i*)ivec);
for(i = 0; i < length; i++)
{
last_in = _mm_loadu_si128(&((__m128i*)in)[i]);
data = _mm_xor_si128(last_in, ((__m128i*)key)[number_of_rounds]);
for(j = number_of_rounds - 1; j > 0; j--)
{
data = _mm_aesdec_si128(data, ((__m128i*)key)[j]);
}
data = _mm_aesdeclast_si128(data, ((__m128i*)key)[0]);
data = _mm_xor_si128(data, feedback);
_mm_storeu_si128(&((__m128i*)out)[i], data);
feedback = last_in;
}
return EXIT_SUCCESS;
}
static void ctr_inc(unsigned char *ctr_blk)
{
uint32_t c;
c = *(uint32_t*)(ctr_blk + 8);
c++;
*(uint32_t*)(ctr_blk + 8) = c;
if(!c)
*(uint32_t*)(ctr_blk + 12) = *(uint32_t*)(ctr_blk + 12) + 1;
}
AES_RETURN AES_CTR_encrypt(const unsigned char *in,
unsigned char *out,
const unsigned char ivec[8],
const unsigned char nonce[4],
unsigned long length,
const aes_encrypt_ctx cx[1])
{
__m128i ctr_block = { 0 }, *key = (__m128i*)cx->ks, tmp, ONE, BSWAP_EPI64;
int number_of_rounds = cx->inf.b[0] >> 4, j;
unsigned long i;
if(number_of_rounds != 10 && number_of_rounds != 12 && number_of_rounds != 14)
return EXIT_FAILURE;
if(!has_aes_ni())
{
unsigned char ctr_blk[16];
*(uint64_t*)ctr_blk = *(uint64_t*)ivec;
*(uint32_t*)(ctr_blk + 8) = *(uint32_t*)nonce;
return aes_ctr_crypt(in, out, length, (unsigned char*)ctr_blk, ctr_inc, cx);
}
if(length % 16)
length = length / 16 + 1;
else length /= 16;
ONE = _mm_set_epi32(0, 1, 0, 0);
BSWAP_EPI64 = _mm_setr_epi8(7, 6, 5, 4, 3, 2, 1, 0, 15, 14, 13, 12, 11, 10, 9, 8);
#ifdef _MSC_VER
ctr_block = _mm_insert_epi64(ctr_block, *(long long*)ivec, 1);
#else
ctr_block = _mm_set_epi64(*(__m64*)ivec, *(__m64*)&ctr_block);
#endif
ctr_block = _mm_insert_epi32(ctr_block, *(long*)nonce, 1);
ctr_block = _mm_srli_si128(ctr_block, 4);
ctr_block = _mm_shuffle_epi8(ctr_block, BSWAP_EPI64);
ctr_block = _mm_add_epi64(ctr_block, ONE);
for(i = 0; i < length; i++)
{
tmp = _mm_shuffle_epi8(ctr_block, BSWAP_EPI64);
ctr_block = _mm_add_epi64(ctr_block, ONE);
tmp = _mm_xor_si128(tmp, ((__m128i*)key)[0]);
for(j = 1; j <number_of_rounds; j++)
{
tmp = _mm_aesenc_si128(tmp, ((__m128i*)key)[j]);
};
tmp = _mm_aesenclast_si128(tmp, ((__m128i*)key)[j]);
tmp = _mm_xor_si128(tmp, _mm_loadu_si128(&((__m128i*)in)[i]));
_mm_storeu_si128(&((__m128i*)out)[i], tmp);
}
return EXIT_SUCCESS;
}
#else
void aes_CBC_encrypt(const unsigned char *in,
unsigned char *out,
unsigned char ivec[16],
unsigned long length,
unsigned char *key,
int number_of_rounds)
{
__m128i feedback, data;
unsigned long i;
int j;
if(length % 16)
length = length / 16 + 1;
else length /= 16;
feedback = _mm_loadu_si128((__m128i*)ivec);
for(i = 0; i < length; i++)
{
data = _mm_loadu_si128(&((__m128i*)in)[i]);
feedback = _mm_xor_si128(data, feedback);
feedback = _mm_xor_si128(feedback, ((__m128i*)key)[0]);
for(j = 1; j <number_of_rounds; j++)
feedback = _mm_aesenc_si128(feedback, ((__m128i*)key)[j]);
feedback = _mm_aesenclast_si128(feedback, ((__m128i*)key)[j]);
_mm_storeu_si128(&((__m128i*)out)[i], feedback);
}
}
void aes_CBC_decrypt(const unsigned char *in,
unsigned char *out,
unsigned char ivec[16],
unsigned long length,
unsigned char *key,
int number_of_rounds)
{
__m128i data, feedback, last_in;
unsigned long i;
int j;
if(length % 16)
length = length / 16 + 1;
else length /= 16;
feedback = _mm_loadu_si128((__m128i*)ivec);
for(i = 0; i < length; i++)
{
last_in = _mm_loadu_si128(&((__m128i*)in)[i]);
data = _mm_xor_si128(last_in, ((__m128i*)key)[0]);
for(j = 1; j <number_of_rounds; j++)
{
data = _mm_aesdec_si128(data, ((__m128i*)key)[j]);
}
data = _mm_aesdeclast_si128(data, ((__m128i*)key)[j]);
data = _mm_xor_si128(data, feedback);
_mm_storeu_si128(&((__m128i*)out)[i], data);
feedback = last_in;
}
}
void AES_CTR_encrypt(const unsigned char *in,
unsigned char *out,
const unsigned char ivec[8],
const unsigned char nonce[4],
unsigned long length,
const unsigned char *key,
int number_of_rounds)
{
__m128i ctr_block = { 0 }, tmp, ONE, BSWAP_EPI64;
unsigned long i;
int j;
if(length % 16)
length = length / 16 + 1;
else length /= 16;
ONE = _mm_set_epi32(0, 1, 0, 0);
BSWAP_EPI64 = _mm_setr_epi8(7, 6, 5, 4, 3, 2, 1, 0, 15, 14, 13, 12, 11, 10, 9, 8);
#ifdef _MSC_VER
ctr_block = _mm_insert_epi64(ctr_block, *(long long*)ivec, 1);
#else
ctr_block = _mm_set_epi64(*(__m64*)ivec, *(__m64*)&ctr_block);
#endif
ctr_block = _mm_insert_epi32(ctr_block, *(long*)nonce, 1);
ctr_block = _mm_srli_si128(ctr_block, 4);
ctr_block = _mm_shuffle_epi8(ctr_block, BSWAP_EPI64);
ctr_block = _mm_add_epi64(ctr_block, ONE);
for(i = 0; i < length; i++)
{
tmp = _mm_shuffle_epi8(ctr_block, BSWAP_EPI64);
ctr_block = _mm_add_epi64(ctr_block, ONE);
tmp = _mm_xor_si128(tmp, ((__m128i*)key)[0]);
for(j = 1; j <number_of_rounds; j++)
{
tmp = _mm_aesenc_si128(tmp, ((__m128i*)key)[j]);
};
tmp = _mm_aesenclast_si128(tmp, ((__m128i*)key)[j]);
tmp = _mm_xor_si128(tmp, _mm_loadu_si128(&((__m128i*)in)[i]));
_mm_storeu_si128(&((__m128i*)out)[i], tmp);
}
}
#endif
#endif
#endif
-59
View File
@@ -1,59 +0,0 @@
/*
Copyright (c) 1998-2013, 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: 13/11/2013
*/
#ifndef AES_NI_H
#define AES_NI_H
#define USE_AES_CONTEXT
#include "aesopt.h"
#if defined( USE_INTEL_AES_IF_PRESENT )
/* map names in C code to make them internal ('name' -> 'aes_name_i') */
#define aes_xi(x) aes_ ## x ## _i
/* map names here to provide the external API ('name' -> 'aes_name') */
#define aes_ni(x) aes_ ## x
AES_RETURN aes_ni(encrypt_key128)(const unsigned char *key, aes_encrypt_ctx cx[1]);
AES_RETURN aes_ni(encrypt_key192)(const unsigned char *key, aes_encrypt_ctx cx[1]);
AES_RETURN aes_ni(encrypt_key256)(const unsigned char *key, aes_encrypt_ctx cx[1]);
AES_RETURN aes_ni(decrypt_key128)(const unsigned char *key, aes_decrypt_ctx cx[1]);
AES_RETURN aes_ni(decrypt_key192)(const unsigned char *key, aes_decrypt_ctx cx[1]);
AES_RETURN aes_ni(decrypt_key256)(const unsigned char *key, aes_decrypt_ctx cx[1]);
AES_RETURN aes_ni(encrypt)(const unsigned char *in, unsigned char *out, const aes_encrypt_ctx cx[1]);
AES_RETURN aes_ni(decrypt)(const unsigned char *in, unsigned char *out, const aes_decrypt_ctx cx[1]);
AES_RETURN aes_xi(encrypt_key128)(const unsigned char *key, aes_encrypt_ctx cx[1]);
AES_RETURN aes_xi(encrypt_key192)(const unsigned char *key, aes_encrypt_ctx cx[1]);
AES_RETURN aes_xi(encrypt_key256)(const unsigned char *key, aes_encrypt_ctx cx[1]);
AES_RETURN aes_xi(decrypt_key128)(const unsigned char *key, aes_decrypt_ctx cx[1]);
AES_RETURN aes_xi(decrypt_key192)(const unsigned char *key, aes_decrypt_ctx cx[1]);
AES_RETURN aes_xi(decrypt_key256)(const unsigned char *key, aes_decrypt_ctx cx[1]);
AES_RETURN aes_xi(encrypt)(const unsigned char *in, unsigned char *out, const aes_encrypt_ctx cx[1]);
AES_RETURN aes_xi(decrypt)(const unsigned char *in, unsigned char *out, const aes_decrypt_ctx cx[1]);
#endif
#endif
+1 -1
View File
@@ -188,7 +188,7 @@ Issue Date: 20/12/2007
built
*/
#if 1 && defined( INTEL_AES_POSSIBLE ) && !defined( USE_INTEL_AES_IF_PRESENT )
#if 0 && defined( INTEL_AES_POSSIBLE ) && !defined( USE_INTEL_AES_IF_PRESENT )
# define USE_INTEL_AES_IF_PRESENT
#endif
+1
View File
@@ -23,6 +23,7 @@ a password and a salt value.
#include <string.h>
#include "hmac.h"
#include "pwd2key.h"
#if defined(__cplusplus)
extern "C"
+5 -1
View File
@@ -219,7 +219,11 @@
*/
#ifndef lzma_nothrow
# if defined(__cplusplus)
# define lzma_nothrow throw()
# if __cplusplus >= 201103L
# define lzma_nothrow noexcept
# else
# define lzma_nothrow throw()
# endif
# elif __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)
# define lzma_nothrow __attribute__((__nothrow__))
# else
+6 -1
View File
@@ -644,11 +644,16 @@ extern LZMA_API(uint64_t) lzma_memlimit_get(const lzma_stream *strm)
* This function is supported only when *strm has been initialized with
* a function that takes a memlimit argument.
*
* liblzma 5.2.3 and earlier has a bug where memlimit value of 0 causes
* this function to do nothing (leaving the limit unchanged) and still
* return LZMA_OK. Later versions treat 0 as if 1 had been specified (so
* lzma_memlimit_get() will return 1 even if you specify 0 here).
*
* \return - LZMA_OK: New memory usage limit successfully set.
* - LZMA_MEMLIMIT_ERROR: The new limit is too small.
* The limit was not changed.
* - LZMA_PROG_ERROR: Invalid arguments, e.g. *strm doesn't
* support memory usage limit or memlimit was zero.
* support memory usage limit.
*/
extern LZMA_API(lzma_ret) lzma_memlimit_set(
lzma_stream *strm, uint64_t memlimit) lzma_nothrow;
+17 -4
View File
@@ -520,7 +520,10 @@ extern LZMA_API(lzma_ret) lzma_stream_buffer_encode(
*
* \param strm Pointer to properly prepared lzma_stream
* \param memlimit Memory usage limit as bytes. Use UINT64_MAX
* to effectively disable the limiter.
* to effectively disable the limiter. liblzma
* 5.2.3 and earlier don't allow 0 here and return
* LZMA_PROG_ERROR; later versions treat 0 as if 1
* had been specified.
* \param flags Bitwise-or of zero or more of the decoder flags:
* LZMA_TELL_NO_CHECK, LZMA_TELL_UNSUPPORTED_CHECK,
* LZMA_TELL_ANY_CHECK, LZMA_CONCATENATED
@@ -544,7 +547,10 @@ extern LZMA_API(lzma_ret) lzma_stream_decoder(
*
* \param strm Pointer to properly prepared lzma_stream
* \param memlimit Memory usage limit as bytes. Use UINT64_MAX
* to effectively disable the limiter.
* to effectively disable the limiter. liblzma
* 5.2.3 and earlier don't allow 0 here and return
* LZMA_PROG_ERROR; later versions treat 0 as if 1
* had been specified.
* \param flags Bitwise-or of flags, or zero for no flags.
*
* \return - LZMA_OK: Initialization was successful.
@@ -560,9 +566,16 @@ extern LZMA_API(lzma_ret) lzma_auto_decoder(
/**
* \brief Initialize .lzma decoder (legacy file format)
*
* \param strm Pointer to properly prepared lzma_stream
* \param memlimit Memory usage limit as bytes. Use UINT64_MAX
* to effectively disable the limiter. liblzma
* 5.2.3 and earlier don't allow 0 here and return
* LZMA_PROG_ERROR; later versions treat 0 as if 1
* had been specified.
*
* Valid `action' arguments to lzma_code() are LZMA_RUN and LZMA_FINISH.
* There is no need to use LZMA_FINISH, but allowing it may simplify
* certain types of applications.
* There is no need to use LZMA_FINISH, but it's allowed because it may
* simplify certain types of applications.
*
* \return - LZMA_OK
* - LZMA_MEM_ERROR
+1 -1
View File
@@ -22,7 +22,7 @@
*/
#define LZMA_VERSION_MAJOR 5
#define LZMA_VERSION_MINOR 2
#define LZMA_VERSION_PATCH 3
#define LZMA_VERSION_PATCH 4
#define LZMA_VERSION_STABILITY LZMA_VERSION_STABILITY_STABLE
#ifndef LZMA_VERSION_COMMIT
+3 -3
View File
@@ -109,9 +109,9 @@ extern const uint64_t lzma_crc64_table[4][256];
/// \brief Initialize *check depending on type
///
/// \param check LZMA_OK on success. LZMA_UNSUPPORTED_CHECK if the type
/// is not supported by the current version or build of
/// liblzma. LZMA_PROG_ERROR if type > LZMA_CHECK_ID_MAX.
/// \return LZMA_OK on success. LZMA_UNSUPPORTED_CHECK if the type is not
/// supported by the current version or build of liblzma.
/// LZMA_PROG_ERROR if type > LZMA_CHECK_ID_MAX.
extern void lzma_check_init(lzma_check_state *check, lzma_check type);
/// Update the check state
+1 -4
View File
@@ -184,9 +184,6 @@ lzma_alone_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
{
lzma_next_coder_init(&lzma_alone_decoder_init, next, allocator);
if (memlimit == 0)
return LZMA_PROG_ERROR;
lzma_alone_coder *coder = next->coder;
if (coder == NULL) {
@@ -208,7 +205,7 @@ lzma_alone_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
coder->options.preset_dict = NULL;
coder->options.preset_dict_size = 0;
coder->uncompressed_size = 0;
coder->memlimit = memlimit;
coder->memlimit = my_max(1, memlimit);
coder->memusage = LZMA_MEMUSAGE_BASE;
return LZMA_OK;
+4 -2
View File
@@ -435,8 +435,10 @@ lzma_memlimit_set(lzma_stream *strm, uint64_t new_memlimit)
|| strm->internal->next.memconfig == NULL)
return LZMA_PROG_ERROR;
if (new_memlimit != 0 && new_memlimit < LZMA_MEMUSAGE_BASE)
return LZMA_MEMLIMIT_ERROR;
// Zero is a special value that cannot be used as an actual limit.
// If 0 was specified, use 1 instead.
if (new_memlimit == 0)
new_memlimit = 1;
return strm->internal->next.memconfig(strm->internal->next.coder,
&memusage, &old_memlimit, new_memlimit);
+4
View File
@@ -146,7 +146,11 @@
/* #undef HAVE_ICONV */
/* Define to 1 if you have the <immintrin.h> header file. */
#if (defined(__GNUC__) && defined(__SSE2_MATH__)) || \
(defined(__INTEL_COMPILER) && defined(__SSE2__)) || \
(defined(_MSC_VER) && defined(_M_IX86_FP) && _M_IX86_FP >= 2)
#define HAVE_IMMINTRIN_H 1
#endif
/* Define to 1 if you have the <inttypes.h> header file. */
#define HAVE_INTTYPES_H 1
+6
View File
@@ -16,6 +16,12 @@
#include "lzma_decoder.h"
#include "range_decoder.h"
// The macros unroll loops with switch statements.
// Silence warnings about missing fall-through comments.
#if TUKLIB_GNUC_REQ(7, 0)
# pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
#endif
#ifdef HAVE_SMALL
Executable → Regular
+277 -173
View File
@@ -1,8 +1,8 @@
/* minizip.c
Version 2.2.4, November 15th, 2017
Version 2.3.9, July 26, 2018
part of the MiniZip project
Copyright (C) 2012-2017 Nathan Moinvaziri
Copyright (C) 2010-2018 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
Copyright (C) 2009-2010 Mathias Svensson
Modifications for Zip64 support
@@ -26,18 +26,19 @@
#include "mz.h"
#include "mz_os.h"
#include "mz_strm.h"
#include "mz_strm_buf.h"
#include "mz_strm_split.h"
#include "mz_zip.h"
/***************************************************************************/
void minizip_banner()
void minizip_banner(void)
{
printf("Minizip %s - https://github.com/nmoinvaz/minizip\n", MZ_VERSION);
printf("---------------------------------------------------\n");
}
void minizip_help()
void minizip_help(void)
{
printf("Usage : minizip [-x -d dir|-l] [-o] [-a] [-j] [-0 to -9] [-b|-m] [-k 512] [-p pwd] [-s] file.zip [files]\n\n" \
" -x Extract files\n" \
@@ -45,7 +46,8 @@ void minizip_help()
" -d Destination directory\n" \
" -o Overwrite existing files\n" \
" -a Append to existing zip file\n" \
" -j Exclude path of files\n" \
" -u Buffered reading and writing\n" \
" -i Include full path of files\n" \
" -0 Store only\n" \
" -1 Compress faster\n" \
" -9 Compress better\n" \
@@ -66,10 +68,10 @@ void minizip_help()
/***************************************************************************/
typedef struct minizip_opt_s {
uint8_t exclude_path;
int16_t include_path;
int16_t compress_level;
int16_t compress_method;
uint8_t overwrite;
int16_t overwrite;
#ifdef HAVE_AES
int16_t aes;
#endif
@@ -77,48 +79,32 @@ typedef struct minizip_opt_s {
/***************************************************************************/
int32_t minizip_add_file(void *handle, const char *path, const char *password, minizip_opt *options)
int32_t minizip_add_path(void *handle, const char *path, const char *filenameinzip, const char *password, int16_t is_dir, minizip_opt *options)
{
mz_zip_file file_info = { 0 };
mz_zip_file file_info;
int32_t read = 0;
int32_t written = 0;
int32_t err = MZ_OK;
int32_t err_close = MZ_OK;
void *stream = NULL;
const char *filenameinzip = NULL;
char buf[INT16_MAX];
// Construct the filename that our file will be stored in the zip as.
// The path name saved, should not include a leading slash.
// If it did, windows/xp and dynazip couldn't read the zip file.
memset(&file_info, 0, sizeof(file_info));
filenameinzip = path;
// The path name saved, should not include a leading slash.
// If it did, windows/xp and dynazip couldn't read the zip file.
if (filenameinzip == NULL)
filenameinzip = path;
while (filenameinzip[0] == '\\' || filenameinzip[0] == '/')
filenameinzip += 1;
// Should the file be stored with any path info at all?
if (options->exclude_path)
{
const char *match = NULL;
const char *last_slash = NULL;
for (match = filenameinzip; *match; match += 1)
{
if (*match == '\\' || *match == '/')
last_slash = match;
}
if (last_slash != NULL)
filenameinzip = last_slash + 1; // base filename follows last slash
}
// Get information about the file on disk so we can store it in zip
printf("Adding: %s\n", filenameinzip);
file_info.version_madeby = MZ_VERSION_MADEBY;
file_info.compression_method = options->compress_method;
file_info.filename = (char *)filenameinzip;
file_info.filename = filenameinzip;
file_info.uncompressed_size = mz_os_get_file_size(path);
#ifdef HAVE_AES
@@ -126,8 +112,11 @@ int32_t minizip_add_file(void *handle, const char *path, const char *password, m
file_info.aes_version = MZ_AES_VERSION;
#endif
mz_os_get_file_date(path, &file_info.modified_date, &file_info.accessed_date,
mz_os_get_file_date(path, &file_info.modified_date, &file_info.accessed_date,
&file_info.creation_date);
mz_os_get_file_attribs(path, &file_info.external_fa);
printf("Adding: %s\n", file_info.filename);
// Add to zip
err = mz_zip_entry_write_open(handle, &file_info, options->compress_level, password);
@@ -137,43 +126,45 @@ int32_t minizip_add_file(void *handle, const char *path, const char *password, m
return err;
}
mz_stream_os_create(&stream);
err = mz_stream_os_open(stream, path, MZ_OPEN_MODE_READ);
if (err == MZ_OK)
if (!is_dir)
{
// Read contents of file and write it to zip
do
mz_stream_os_create(&stream);
err = mz_stream_os_open(stream, path, MZ_OPEN_MODE_READ);
if (err == MZ_OK)
{
read = mz_stream_os_read(stream, buf, sizeof(buf));
if (read < 0)
// Read contents of file and write it to zip
do
{
err = mz_stream_os_error(stream);
printf("Error %d in reading %s\n", err, filenameinzip);
break;
}
if (read == 0)
break;
read = mz_stream_os_read(stream, buf, sizeof(buf));
if (read < 0)
{
err = mz_stream_os_error(stream);
printf("Error %d in reading %s\n", err, filenameinzip);
break;
}
if (read == 0)
break;
written = mz_zip_entry_write(handle, buf, read);
if (written != read)
{
err = mz_stream_os_error(stream);
printf("Error in writing %s in the zip file (%d)\n", filenameinzip, err);
break;
}
written = mz_zip_entry_write(handle, buf, read);
if (written != read)
{
err = mz_stream_os_error(stream);
printf("Error in writing %s in the zip file (%d)\n", filenameinzip, err);
break;
}
} while (err == MZ_OK);
mz_stream_os_close(stream);
}
else
{
printf("Error in opening %s for reading\n", path);
}
while (err == MZ_OK);
mz_stream_os_close(stream);
mz_stream_os_delete(&stream);
}
else
{
printf("Error in opening %s for reading\n", path);
}
mz_stream_os_delete(&stream);
err_close = mz_zip_entry_close(handle);
if (err_close != MZ_OK)
@@ -185,16 +176,43 @@ int32_t minizip_add_file(void *handle, const char *path, const char *password, m
return err;
}
int32_t minizip_add(void *handle, const char *path, const char *password, minizip_opt *options, uint8_t recursive)
int32_t minizip_add(void *handle, const char *path, const char *root_path, const char *password, minizip_opt *options, uint8_t recursive)
{
DIR *dir = NULL;
struct dirent *entry = NULL;
int32_t err = 0;
int32_t err = MZ_OK;
int16_t is_dir = 0;
char full_path[320];
const char *filename = NULL;
const char *filenameinzip = path;
if (mz_os_is_dir(path) != MZ_OK)
return minizip_add_file(handle, path, password, options);
if (mz_os_is_dir(path) == MZ_OK)
is_dir = 1;
// Construct the filename that our file will be stored in the zip as
if (root_path == NULL)
root_path = path;
// Should the file be stored with any path info at all?
if (!options->include_path)
{
if (!is_dir && root_path == path)
{
if (mz_path_get_filename(filenameinzip, &filename) == MZ_OK)
filenameinzip = filename;
}
else
{
filenameinzip += strlen(root_path);
}
}
if (*filenameinzip != 0)
err = minizip_add_path(handle, path, filenameinzip, password, is_dir, options);
if (!is_dir)
return err;
dir = mz_os_open_dir(path);
@@ -216,7 +234,7 @@ int32_t minizip_add(void *handle, const char *path, const char *password, minizi
if (!recursive && mz_os_is_dir(full_path))
continue;
err = minizip_add(handle, full_path, password, options, recursive);
err = minizip_add(handle, full_path, root_path, password, options, recursive);
if (err != MZ_OK)
return err;
}
@@ -233,7 +251,7 @@ int32_t minizip_list(void *handle)
uint32_t ratio = 0;
int16_t level = 0;
int32_t err = MZ_OK;
struct tm tmu_date = { 0 };
struct tm tmu_date;
const char *string_method = NULL;
char crypt = ' ';
@@ -246,8 +264,8 @@ int32_t minizip_list(void *handle)
return err;
}
printf(" Length Method Size Ratio Date Time CRC-32 Name\n");
printf(" ------ ------ ---- ----- ---- ---- ------ ----\n");
printf(" Length Method Size Attribs Ratio Date Time CRC-32 Name\n");
printf(" ------ ------ ---- ------- ----- ---- ---- ------ ----\n");
while (err == MZ_OK)
{
@@ -259,9 +277,10 @@ int32_t minizip_list(void *handle)
break;
}
ratio = 0;
if (file_info->uncompressed_size > 0)
ratio = (uint32_t)((file_info->compressed_size * 100) / file_info->uncompressed_size);
// Display a '*' if the file is encrypted
if (file_info->flag & MZ_ZIP_FLAG_ENCRYPTED)
crypt = '*';
@@ -269,7 +288,7 @@ int32_t minizip_list(void *handle)
switch (file_info->compression_method)
{
case MZ_COMPRESS_METHOD_RAW:
string_method = "Stored";
string_method = "Stored";
break;
case MZ_COMPRESS_METHOD_DEFLATE:
level = (int16_t)((file_info->flag & 0x6) / 2);
@@ -294,9 +313,10 @@ int32_t minizip_list(void *handle)
mz_zip_time_t_to_tm(file_info->modified_date, &tmu_date);
printf(" %7"PRIu64" %6s%c %7"PRIu64" %3"PRIu32"%% %2.2"PRIu32"-%2.2"PRIu32\
printf(" %7"PRIu64" %6s%c %7"PRIu64" %8"PRIx32" %3"PRIu32"%% %2.2"PRIu32"-%2.2"PRIu32\
"-%2.2"PRIu32" %2.2"PRIu32":%2.2"PRIu32" %8.8"PRIx32" %s\n",
file_info->uncompressed_size, string_method, crypt, file_info->compressed_size, ratio,
file_info->uncompressed_size, string_method, crypt,
file_info->compressed_size, file_info->external_fa, ratio,
(uint32_t)tmu_date.tm_mon + 1, (uint32_t)tmu_date.tm_mday,
(uint32_t)tmu_date.tm_year % 100,
(uint32_t)tmu_date.tm_hour, (uint32_t)tmu_date.tm_min,
@@ -327,10 +347,9 @@ int32_t minizip_extract_currentfile(void *handle, const char *destination, const
int32_t written = 0;
int32_t err = MZ_OK;
int32_t err_close = MZ_OK;
uint8_t skip = 0;
void *stream = NULL;
char *match = NULL;
char *filename = NULL;
const char *filename = NULL;
char out_name[512];
char out_path[512];
char directory[512];
@@ -343,32 +362,33 @@ int32_t minizip_extract_currentfile(void *handle, const char *destination, const
return err;
}
match = filename = file_info->filename;
while (*match != 0)
{
if ((*match == '/') || (*match == '\\'))
filename = match + 1;
match += 1;
}
if (mz_path_get_filename(file_info->filename, &filename) != MZ_OK)
filename = file_info->filename;
// Construct output path
out_path[0] = 0;
if (destination != NULL)
mz_path_combine(out_path, destination, sizeof(out_path));
if (options->exclude_path)
mz_path_combine(out_path, filename, sizeof(out_path));
if ((*file_info->filename == '/') || (file_info->filename[1] == ':'))
{
strncpy(out_path, file_info->filename, sizeof(out_path));
}
else
mz_path_combine(out_path, file_info->filename, sizeof(out_path));
{
if (destination != NULL)
mz_path_combine(out_path, destination, sizeof(out_path));
err = mz_path_resolve(file_info->filename, out_name, sizeof(out_name));
if (err != MZ_OK)
{
printf("Error %d resolving output path\n", err);
return err;
}
mz_path_combine(out_path, out_name, sizeof(out_path));
}
// If zip entry is a directory then create it on disk
if (*filename == 0)
if (mz_zip_attrib_is_dir(file_info->external_fa, file_info->version_madeby) == MZ_OK)
{
if (options->exclude_path == 0)
{
printf("Creating directory: %s\n", out_path);
mz_make_dir(out_path);
}
printf("Creating directory: %s\n", out_path);
mz_make_dir(out_path);
return err;
}
@@ -397,7 +417,7 @@ int32_t minizip_extract_currentfile(void *handle, const char *destination, const
while ((rep != 'Y') && (rep != 'N') && (rep != 'A'));
if (rep == 'N')
skip = 1;
return err;
if (rep == 'A')
options->overwrite = 1;
}
@@ -405,33 +425,24 @@ int32_t minizip_extract_currentfile(void *handle, const char *destination, const
mz_stream_os_create(&stream);
// Create the file on disk so we can unzip to it
if ((skip == 0) && (err == MZ_OK))
if (err == MZ_OK)
{
// Some zips don't contain directory alone before file
if ((mz_stream_os_open(stream, out_path, MZ_OPEN_MODE_CREATE) != MZ_OK) &&
(options->exclude_path == 0) && (filename != file_info->filename))
err = mz_stream_os_open(stream, out_path, MZ_OPEN_MODE_CREATE);
if ((err != MZ_OK) && (filename != file_info->filename))
{
// Create the directory of the output path
strncpy(directory, out_path, sizeof(directory));
match = directory + strlen(directory) - 1;
while (match > directory)
{
if ((*match == '/') || (*match == '\\'))
{
*match = 0;
break;
}
match -= 1;
}
mz_path_remove_filename(directory);
mz_make_dir(directory);
mz_stream_os_open(stream, out_path, MZ_OPEN_MODE_CREATE);
err = mz_stream_os_open(stream, out_path, MZ_OPEN_MODE_CREATE);
}
}
// Read from the zip, unzip to buffer, and write to disk
if (mz_stream_os_is_open(stream) == MZ_OK)
if (err == MZ_OK)
{
printf(" Extracting: %s\n", out_path);
while (1)
@@ -458,12 +469,12 @@ int32_t minizip_extract_currentfile(void *handle, const char *destination, const
// Set the time of the file that has been unzipped
if (err == MZ_OK)
mz_os_set_file_date(out_path, file_info->modified_date, file_info->accessed_date,
mz_os_set_file_date(out_path, file_info->modified_date, file_info->accessed_date,
file_info->creation_date);
}
else
{
printf("Error opening %s\n", out_path);
printf("Error %d opening %s\n", err, out_path);
}
mz_stream_os_delete(&stream);
@@ -481,7 +492,6 @@ int32_t minizip_extract_currentfile(void *handle, const char *destination, const
int32_t minizip_extract_all(void *handle, const char *destination, const char *password, minizip_opt *options)
{
int32_t err = MZ_OK;
err = mz_zip_goto_first_entry(handle);
@@ -523,12 +533,13 @@ int32_t minizip_extract_onefile(void *handle, const char *filename, const char *
/***************************************************************************/
#ifndef NOMAIN
#if 0
int main(int argc, char *argv[])
{
void *handle = NULL;
void *file_stream = NULL;
void *split_stream = NULL;
void *buf_stream = NULL;
char *path = NULL;
char *password = NULL;
char *destination = NULL;
@@ -538,6 +549,7 @@ int main(int argc, char *argv[])
int32_t path_arg = 0;
uint8_t do_list = 0;
uint8_t do_extract = 0;
uint8_t buffered = 0;
int16_t mode = 0;
uint8_t append = 0;
int32_t err_close = 0;
@@ -559,63 +571,57 @@ int main(int argc, char *argv[])
// Parse command line options
for (i = 1; i < argc; i += 1)
{
if ((*argv[i]) == '-')
if (argv[i][0] == '-')
{
const char *p = argv[i] + 1;
while ((*p) != '\0')
char c = argv[i][1];
if ((c == 'l') || (c == 'L'))
do_list = 1;
if ((c == 'x') || (c == 'X'))
do_extract = 1;
if ((c == 'a') || (c == 'A'))
append = 1;
if ((c == 'u') || (c == 'U'))
buffered = 1;
if ((c == 'o') || (c == 'O'))
options.overwrite = 1;
if ((c == 'i') || (c == 'I'))
options.include_path = 1;
if ((c >= '0') && (c <= '9'))
{
char c = *(p++);
if ((c == 'l') || (c == 'L'))
do_list = 1;
if ((c == 'x') || (c == 'X'))
do_extract = 1;
if ((c == 'a') || (c == 'A'))
append = 1;
if ((c == 'o') || (c == 'O'))
options.overwrite = 1;
if ((c == 'j') || (c == 'J'))
options.exclude_path = 1;
if ((c >= '0') && (c <= '9'))
{
options.compress_level = (c - '0');
if (options.compress_level == 0)
options.compress_method = MZ_COMPRESS_METHOD_RAW;
}
#ifdef HAVE_BZIP2
if ((c == 'b') || (c == 'B'))
options.compress_method = MZ_COMPRESS_METHOD_BZIP2;
#endif
#ifdef HAVE_LZMA
if ((c == 'm') || (c == 'M'))
options.compress_method = MZ_COMPRESS_METHOD_LZMA;
#endif
#ifdef HAVE_AES
if ((c == 's') || (c == 'S'))
options.aes = 1;
#endif
if (((c == 'k') || (c == 'k')) && (i + 1 < argc))
{
disk_size = atoi(argv[i + 1]) * 1024;
i += 1;
}
if (((c == 'd') || (c == 'D')) && (i + 1 < argc))
{
destination = argv[i + 1];
i += 1;
}
if (((c == 'p') || (c == 'P')) && (i + 1 < argc))
{
password = argv[i + 1];
i += 1;
}
options.compress_level = (c - '0');
if (options.compress_level == 0)
options.compress_method = MZ_COMPRESS_METHOD_RAW;
}
continue;
#ifdef HAVE_BZIP2
if ((c == 'b') || (c == 'B'))
options.compress_method = MZ_COMPRESS_METHOD_BZIP2;
#endif
#ifdef HAVE_LZMA
if ((c == 'm') || (c == 'M'))
options.compress_method = MZ_COMPRESS_METHOD_LZMA;
#endif
#ifdef HAVE_AES
if ((c == 's') || (c == 'S'))
options.aes = 1;
#endif
if (((c == 'k') || (c == 'K')) && (i + 1 < argc))
{
disk_size = atoi(argv[i + 1]) * 1024;
i += 1;
}
if (((c == 'd') || (c == 'D')) && (i + 1 < argc))
{
destination = argv[i + 1];
i += 1;
}
if (((c == 'p') || (c == 'P')) && (i + 1 < argc))
{
password = argv[i + 1];
i += 1;
}
}
if (path_arg == 0)
else if (path_arg == 0)
path_arg = i;
}
@@ -676,9 +682,19 @@ int main(int argc, char *argv[])
}
mz_stream_os_create(&file_stream);
mz_stream_buffered_create(&buf_stream);
mz_stream_split_create(&split_stream);
mz_stream_set_base(split_stream, file_stream);
if (buffered)
{
mz_stream_set_base(buf_stream, file_stream);
mz_stream_set_base(split_stream, buf_stream);
}
else
{
mz_stream_set_base(split_stream, file_stream);
}
mz_stream_split_set_prop_int64(split_stream, MZ_STREAM_PROP_DISK_SIZE, disk_size);
err = mz_stream_open(split_stream, path, mode);
@@ -721,7 +737,7 @@ int main(int argc, char *argv[])
// Go through command line args looking for files to add to zip
for (i = path_arg + 1; (i < argc) && (err == MZ_OK); i += 1)
err = minizip_add(handle, argv[i], password, &options, 1);
err = minizip_add(handle, argv[i], NULL, password, &options, 1);
mz_zip_set_version_madeby(handle, MZ_VERSION_MADEBY);
}
@@ -734,12 +750,100 @@ int main(int argc, char *argv[])
err = err_close;
}
mz_stream_os_close(file_stream);
mz_stream_close(split_stream);
}
mz_stream_split_delete(&split_stream);
mz_stream_buffered_delete(&buf_stream);
mz_stream_os_delete(&file_stream);
return err;
}
#endif
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "mz.h"
#include "mz_zip.h"
#include "mz_compat.h"
static void myextract(const char * zip, const char * fn)
{
unz_file_info64 ufi;
unzFile hUnzip;
void * buffer;
int iRead;
int found;
printf("uopen : %p\n", (void *)(hUnzip = unzOpen2_64(zip, NULL)));
// find requested file
printf("ufi1st: %d\n", unzGoToFirstFile(hUnzip));
found = 0;
for (;;)
{
char fnz[256];
printf("ufinfo: %d\n", unzGetCurrentFileInfo64(hUnzip, &ufi, (char *)&fnz, sizeof(fnz) - 1, NULL, 0, NULL, 0));
if (strcmp(fnz, fn) == 0)
{
printf("FOUND!: %s\n", fn);
printf("uoffst: %lld\n", unzGetOffset64(hUnzip));
printf("ufopen: %d\n", unzOpenCurrentFile3(hUnzip, NULL, NULL, 0, NULL));
buffer = malloc(ufi.uncompressed_size);
printf("uread : %d\n", iRead = unzReadCurrentFile(hUnzip, buffer, ufi.uncompressed_size));
printf("READ : %s\n", buffer); /* WITH ZIP64, IT'S RETURNING THE CONTENT OF THE FIRST FILE WHILE POSITIONED ON THE 2ND */
free(buffer);
printf("ufclos: %d\n", unzCloseCurrentFile(hUnzip));
found = 1;
break;
}
printf("ufinxt: %d\n", unzGoToNextFile(hUnzip));
}
if (found == 0)
printf("not found: %s\n", fn);
printf("uclose: %d\n", unzClose(hUnzip));
}
int main(void)
{
int zip64;
// create two .zip files, one regular and one with ZIP64 enabled, with two file entries each
/*for (zip64 = 0; zip64 <= 1; zip64++)
{
static const unsigned char content1[] = "11.txt-first file";
static const unsigned char content2[] = "22.txt-second file";
zipFile hZip;
zip_fileinfo zfi;
printf("zcreat: %p\n", (void *)(hZip = zipOpen2_64(zip64 ? "test_64.zip" : "test_32.zip", APPEND_STATUS_CREATE, NULL, NULL)));
memset(&zfi, 0, sizeof(zfi));
printf("zaddfi: %d\n", zipOpenNewFileInZip4_64(hZip, "11.txt", &zfi, NULL, 0, NULL, 0, NULL,
MZ_COMPRESS_METHOD_DEFLATE, 6, 0,
-MAX_WBITS, 8, 0,
NULL, 0xD7681E40,
0, 0, zip64));
printf("zwrite: %d\n", zipWriteInFileInZip(hZip, content1, sizeof(content1)));
printf("zfclos: %d\n", zipCloseFileInZip(hZip));
printf("zaddfi: %d\n", zipOpenNewFileInZip4_64(hZip, "22.txt", &zfi, NULL, 0, NULL, 0, NULL,
MZ_COMPRESS_METHOD_DEFLATE, 6, 0,
-MAX_WBITS, 8, 0,
NULL, 0x7A416084,
0, 0, zip64));
printf("zwrite: %d\n", zipWriteInFileInZip(hZip, content2, sizeof(content2)));
printf("zfclos: %d\n", zipCloseFileInZip(hZip));
printf("zclose: %d\n", zipClose(hZip, NULL));
}*/
// try extracting the last (= second) file from each .zip
//myextract("test_32.zip", "22.txt");
myextract("test_64.zip", "22.txt");
return 0;
}
+2 -2
View File
@@ -8,6 +8,6 @@ Name: minizip
Description: Minizip zip file manipulation library
Version: @VERSION@
Requires:
Libs: -L${libdir} -L${sharedlibdir} -lz -lminizip
Requires: zlib
Libs: -L${libdir} -L${sharedlibdir} -lminizip
Cflags: -I${includedir}
+28 -5
View File
@@ -1,16 +1,16 @@
/* mz.h -- Errors codes, zip flags and magic
Version 2.2.4, November 15th, 2017
Version 2.3.9, July 26, 2018
part of the MiniZip project
Copyright (C) 2012-2017 Nathan Moinvaziri
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_H
#define _MZ_H
#ifndef MZ_H
#define MZ_H
#ifdef __cplusplus
extern "C" {
@@ -19,7 +19,7 @@ extern "C" {
/***************************************************************************/
// MZ_VERSION
#define MZ_VERSION ("2.2.4")
#define MZ_VERSION ("2.3.9")
// MZ_ERROR
#define MZ_OK (0)
@@ -34,6 +34,8 @@ extern "C" {
#define MZ_CRC_ERROR (-105)
#define MZ_CRYPT_ERROR (-106)
#define MZ_EXIST_ERROR (-107)
#define MZ_PASSWORD_ERROR (-108)
#define MZ_SUPPORT_ERROR (-109)
// MZ_OPEN
#define MZ_OPEN_MODE_READ (0x01)
@@ -70,12 +72,33 @@ extern "C" {
MZ_ZIP_FLAG_DEFLATE_MAX)
#define MZ_ZIP_FLAG_DATA_DESCRIPTOR (1 << 3)
// MZ_ZIP64
#define MZ_ZIP64_AUTO (0)
#define MZ_ZIP64_FORCE (1)
#define MZ_ZIP64_DISABLE (2)
// MZ_HOST_SYSTEM
#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_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)
// MZ_UTILITY
#define MZ_UNUSED(SYMBOL) ((void)SYMBOL)
#ifndef MZ_CUSTOM_ALLOC
#define MZ_ALLOC(SIZE) (malloc(SIZE))
#endif
#ifndef MZ_CUSTOM_FREE
#define MZ_FREE(PTR) (free(PTR))
#endif
/***************************************************************************/
#ifdef __cplusplus
+318 -113
View File
@@ -1,8 +1,8 @@
/* mz_compat.c -- Backwards compatible interface for older versions
Version 2.2.4, November 15th, 2017
Version 2.3.9, July 26, 2018
part of the MiniZip project
Copyright (C) 2012-2017 Nathan Moinvaziri
Copyright (C) 2010-2018 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
Copyright (C) 1998-2010 Gilles Vollant
http://www.winimage.com/zLibDll/minizip.html
@@ -18,6 +18,7 @@
#include "mz.h"
#include "mz_os.h"
#include "mz_strm.h"
#include "mz_strm_mem.h"
#include "mz_strm_zlib.h"
#include "mz_zip.h"
@@ -26,40 +27,16 @@
/***************************************************************************/
typedef struct mz_compat_s {
void *stream;
void *handle;
void *stream;
void *handle;
int raw;
} mz_compat;
/***************************************************************************/
extern zipFile ZEXPORT zipOpen(const char *path, int append)
static int32_t zipConvertAppendToStreamMode(int append)
{
return zipOpen2(path, append, NULL, mz_stream_os_get_interface());
}
extern zipFile ZEXPORT zipOpen64(const void *path, int append)
{
return zipOpen2(path, append, NULL, mz_stream_os_get_interface());
}
extern 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,
zlib_filefunc64_def *pzlib_filefunc_def)
{
mz_compat *compat = NULL;
int32_t mode = MZ_OPEN_MODE_READWRITE;
int32_t open_existing = 0;
void *handle = NULL;
void *stream = NULL;
if (mz_stream_create(&stream, (mz_stream_vtbl *)pzlib_filefunc_def) == NULL)
return NULL;
int32_t mode = MZ_OPEN_MODE_WRITE;
switch (append)
{
case APPEND_STATUS_CREATE:
@@ -69,9 +46,48 @@ extern zipFile ZEXPORT zipOpen2_64(const void *path, int append, const char **gl
mode |= MZ_OPEN_MODE_CREATE | MZ_OPEN_MODE_APPEND;
break;
case APPEND_STATUS_ADDINZIP:
open_existing = 1;
mode |= MZ_OPEN_MODE_READ;
break;
}
return mode;
}
extern 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)
{
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,
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,
zlib_filefunc64_def *pzlib_filefunc_def)
{
zipFile zip = NULL;
int32_t mode = zipConvertAppendToStreamMode(append);
void *stream = NULL;
if (pzlib_filefunc_def)
{
if (mz_stream_create(&stream, (mz_stream_vtbl *)*pzlib_filefunc_def) == NULL)
return NULL;
}
else
{
if (mz_stream_os_create(&stream) == NULL)
return NULL;
}
if (mz_stream_open(stream, path, mode) != MZ_OK)
{
@@ -79,18 +95,32 @@ extern zipFile ZEXPORT zipOpen2_64(const void *path, int append, const char **gl
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;
}
extern zipFile ZEXPORT zipOpen_MZ(void *stream, int append, const char **globalcomment)
{
mz_compat *compat = NULL;
int32_t mode = zipConvertAppendToStreamMode(append);
void *handle = NULL;
handle = mz_zip_open(stream, mode);
if (handle == NULL)
return NULL;
if (globalcomment != NULL)
mz_zip_get_comment(handle, globalcomment);
compat = (mz_compat *)malloc(sizeof(mz_compat));
compat = (mz_compat *)MZ_ALLOC(sizeof(mz_compat));
compat->handle = handle;
compat->stream = stream;
@@ -99,107 +129,150 @@ extern zipFile ZEXPORT zipOpen2_64(const void *path, int append, const char **gl
extern int ZEXPORT zipOpenNewFileInZip5(zipFile file, const char *filename, const zip_fileinfo *zipfi,
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
uint16_t size_extrafield_global, const char *comment, uint16_t compression_method, int level,
int raw, int windowBits, int memLevel, int strategy, const char *password,
ZIP_UNUSED uint32_t crc_for_crypting, uint16_t version_madeby, uint16_t flag_base, int zip64)
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)
{
mz_compat *compat = (mz_compat *)file;
mz_zip_file file_info;
uint64_t dos_date = 0;
MZ_UNUSED(strategy);
MZ_UNUSED(memLevel);
MZ_UNUSED(windowBits);
MZ_UNUSED(size_extrafield_local);
MZ_UNUSED(extrafield_local);
MZ_UNUSED(crc_for_crypting);
if (compat == NULL)
return MZ_PARAM_ERROR;
return ZIP_PARAMERROR;
compat->raw = raw;
memset(&file_info, 0, sizeof(file_info));
if (zipfi != NULL)
{
file_info.modified_date = mz_zip_dosdate_to_time_t(zipfi->dos_date);
if (zipfi->dosDate != 0)
dos_date = zipfi->dosDate;
else
dos_date = mz_zip_tm_to_dosdate(&zipfi->tmz_date);
file_info.modified_date = mz_zip_dosdate_to_time_t(dos_date);
file_info.external_fa = zipfi->external_fa;
file_info.internal_fa = zipfi->internal_fa;
}
if (filename == NULL)
filename = "-";
file_info.compression_method = compression_method;
file_info.filename = (char *)filename;
file_info.filename = filename;
//file_info.extrafield_local = extrafield_local;
//file_info.extrafield_local_size = size_extrafield_local;
file_info.extrafield = (uint8_t *)extrafield_global;
file_info.extrafield = extrafield_global;
file_info.extrafield_size = size_extrafield_global;
file_info.version_madeby = version_madeby;
file_info.comment = (char *)comment;
file_info.comment = comment;
file_info.flag = flag_base;
if (zip64)
file_info.zip64 = MZ_ZIP64_FORCE;
else
file_info.zip64 = MZ_ZIP64_DISABLE;
#ifdef HAVE_AES
file_info.aes_version = MZ_AES_VERSION;
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);
}
extern int ZEXPORT zipOpenNewFileInZip4_64(zipFile file, const char *filename, const zip_fileinfo *zipfi,
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
uint16_t size_extrafield_global, const char *comment, uint16_t compression_method, int level,
int raw, int windowBits, int memLevel, int strategy, const char *password,
ZIP_UNUSED uint32_t crc_for_crypting, uint16_t version_madeby, uint16_t flag_base, int zip64)
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)
{
return zipOpenNewFileInZip5(file, filename, zipfi, extrafield_local, size_extrafield_local,
extrafield_global, size_extrafield_global, comment, compression_method, level, raw, windowBits,
return zipOpenNewFileInZip5(file, filename, zipfi, extrafield_local, size_extrafield_local,
extrafield_global, size_extrafield_global, comment, compression_method, level, raw, windowBits,
memLevel, strategy, password, crc_for_crypting, version_madeby, flag_base, zip64);
}
extern int ZEXPORT zipOpenNewFileInZip4(zipFile file, const char *filename, const zip_fileinfo *zipfi,
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
uint16_t size_extrafield_global, const char *comment, uint16_t compression_method, int level,
int raw, int windowBits, int memLevel, int strategy, const char *password,
ZIP_UNUSED uint32_t crc_for_crypting, uint16_t version_madeby, uint16_t flag_base)
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)
{
return zipOpenNewFileInZip4_64(file, filename, zipfi, extrafield_local, size_extrafield_local,
extrafield_global, size_extrafield_global, comment, compression_method, level, raw, windowBits,
extrafield_global, size_extrafield_global, comment, compression_method, level, raw, windowBits,
memLevel, strategy, password, crc_for_crypting, version_madeby, flag_base, 0);
}
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,
ZIP_UNUSED uint32_t crc_for_crypting)
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)
{
return zipOpenNewFileInZip4_64(file, filename, zipfi, extrafield_local, size_extrafield_local,
extrafield_global, size_extrafield_global, comment, compression_method, level, raw, windowBits,
extrafield_global, size_extrafield_global, comment, compression_method, level, raw, windowBits,
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,
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
uint16_t size_extrafield_global, const char *comment, uint16_t compression_method, int level,
int raw, int windowBits, int memLevel, int strategy, const char *password,
ZIP_UNUSED uint32_t crc_for_crypting, int zip64)
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)
{
return zipOpenNewFileInZip4_64(file, filename, zipfi, extrafield_local, size_extrafield_local,
extrafield_global, size_extrafield_global, comment, compression_method, level, raw, windowBits,
extrafield_global, size_extrafield_global, comment, compression_method, level, raw, windowBits,
memLevel, strategy, password, crc_for_crypting, MZ_VERSION_MADEBY, 0, zip64);
}
extern int ZEXPORT zipWriteInFileInZip(zipFile file, const void *buf, uint32_t len)
{
mz_compat *compat = (mz_compat *)file;
int32_t written = 0;
if (compat == NULL)
return MZ_PARAM_ERROR;
return mz_zip_entry_write(compat->handle, buf, len);
return ZIP_PARAMERROR;
written = mz_zip_entry_write(compat->handle, buf, 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)
{
return zipCloseFileInZipRaw64(file, uncompressed_size, crc32);
}
extern int ZEXPORT zipCloseFileInZipRaw64(zipFile file, uint64_t uncompressed_size, uint32_t crc32)
{
mz_compat *compat = (mz_compat *)file;
if (compat == NULL)
return MZ_PARAM_ERROR;
return ZIP_PARAMERROR;
if (compat->raw == 0)
return mz_zip_entry_close(compat->handle);
return mz_zip_entry_close_raw(compat->handle, uncompressed_size, crc32);
}
extern int ZEXPORT zipCloseFileInZip(zipFile file)
{
return zipCloseFileInZipRaw(file, 0, 0);
return zipCloseFileInZip64(file);
}
extern 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)
@@ -217,14 +290,8 @@ extern int ZEXPORT zipClose2_64(zipFile file, const char *global_comment, uint16
mz_compat *compat = (mz_compat *)file;
int32_t err = MZ_OK;
if (compat == NULL)
return MZ_PARAM_ERROR;
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)
{
@@ -232,11 +299,47 @@ extern int ZEXPORT zipClose2_64(zipFile file, const char *global_comment, uint16
mz_stream_delete(&compat->stream);
}
free(compat);
MZ_FREE(compat);
return err;
}
// Only closes the zip handle, does not close the stream
extern 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
extern 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);
compat->handle = NULL;
return err;
}
extern 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)
@@ -246,7 +349,8 @@ extern unzFile ZEXPORT unzOpen(const char *path)
extern unzFile ZEXPORT unzOpen64(const void *path)
{
return unzOpen2(path, mz_stream_os_get_interface());
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)
@@ -256,32 +360,50 @@ extern unzFile ZEXPORT unzOpen2(const char *path, zlib_filefunc_def *pzlib_filef
extern 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 (mz_stream_create(&stream, (mz_stream_vtbl *)pzlib_filefunc_def) == NULL)
return NULL;
if (pzlib_filefunc_def)
{
if (mz_stream_create(&stream, (mz_stream_vtbl *)*pzlib_filefunc_def) == NULL)
return NULL;
}
else
{
if (mz_stream_os_create(&stream) == NULL)
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);
unz = unzOpen_MZ(stream);
if (unz == NULL)
{
mz_stream_delete(&stream);
return NULL;
}
return unz;
}
extern unzFile ZEXPORT unzOpen_MZ(void *stream)
{
mz_compat *compat = NULL;
void *handle = NULL;
handle = mz_zip_open(stream, MZ_OPEN_MODE_READ);
if (handle == NULL)
{
mz_stream_delete(&stream);
return NULL;
}
compat = (mz_compat *)malloc(sizeof(mz_compat));
compat = (mz_compat *)MZ_ALLOC(sizeof(mz_compat));
compat->handle = handle;
compat->stream = stream;
mz_zip_goto_first_entry(compat->handle);
return (unzFile)compat;
}
@@ -291,14 +413,33 @@ extern int ZEXPORT unzClose(unzFile file)
int32_t err = MZ_OK;
if (compat == NULL)
return MZ_PARAM_ERROR;
return UNZ_PARAMERROR;
err = mz_zip_close(compat->handle);
if (compat->handle != NULL)
err = unzClose_MZ(file);
if (compat->stream != NULL)
{
mz_stream_close(compat->stream);
mz_stream_delete(&compat->stream);
}
free(compat);
MZ_FREE(compat);
return err;
}
// Only closes the zip handle, does not close the stream
extern 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);
compat->handle = NULL;
return err;
}
@@ -309,17 +450,18 @@ extern int ZEXPORT unzGetGlobalInfo(unzFile file, unz_global_info* pglobal_info3
unz_global_info64 global_info64;
int32_t err = MZ_OK;
memset(pglobal_info32, 0, sizeof(unz_global_info));
if (compat == NULL)
return MZ_PARAM_ERROR;
return UNZ_PARAMERROR;
err = unzGetGlobalInfo64(file, &global_info64);
if (err != UNZ_OK)
if (err == MZ_OK)
{
pglobal_info32->number_entry = (uint32_t)global_info64.number_entry;
pglobal_info32->size_comment = global_info64.size_comment;
pglobal_info32->number_disk_with_CD = global_info64.number_disk_with_CD;
}
return MZ_OK;
return err;
}
extern int ZEXPORT unzGetGlobalInfo64(unzFile file, unz_global_info64 *pglobal_info)
@@ -328,12 +470,16 @@ extern int ZEXPORT unzGetGlobalInfo64(unzFile file, unz_global_info64 *pglobal_i
const char *comment_ptr = NULL;
int32_t err = MZ_OK;
memset(pglobal_info, 0, sizeof(unz_global_info64));
if (compat == NULL)
return MZ_PARAM_ERROR;
return UNZ_PARAMERROR;
err = mz_zip_get_comment(compat->handle, &comment_ptr);
if (err == MZ_OK)
pglobal_info->size_comment = (uint16_t)strlen(comment_ptr);
mz_zip_get_number_entry(compat->handle, (int64_t *)&pglobal_info->number_entry);
if ((err == MZ_OK) || (err == MZ_EXIST_ERROR))
err = mz_zip_get_number_entry(compat->handle, (int64_t *)&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;
}
@@ -344,7 +490,7 @@ extern int ZEXPORT unzGetGlobalComment(unzFile file, char *comment, uint16_t com
int32_t err = MZ_OK;
if (comment == NULL || comment_size == 0)
return MZ_PARAM_ERROR;
return UNZ_PARAMERROR;
err = mz_zip_get_comment(compat->handle, &comment_ptr);
if (err == MZ_OK)
strncpy(comment, comment_ptr, comment_size);
@@ -355,7 +501,7 @@ extern int ZEXPORT unzOpenCurrentFile3(unzFile file, int *method, int *level, in
{
mz_compat *compat = (mz_compat *)file;
if (compat == NULL)
return MZ_PARAM_ERROR;
return UNZ_PARAMERROR;
if (method != NULL)
*method = 0;
if (level != NULL)
@@ -378,11 +524,11 @@ extern int ZEXPORT unzOpenCurrentFile2(unzFile file, int *method, int *level, in
return unzOpenCurrentFile3(file, method, level, raw, NULL);
}
extern int ZEXPORT unzReadCurrentFile(unzFile file, voidp buf, uint32_t len)
extern int ZEXPORT unzReadCurrentFile(unzFile file, void *buf, uint32_t len)
{
mz_compat *compat = (mz_compat *)file;
if (compat == NULL)
return MZ_PARAM_ERROR;
return UNZ_PARAMERROR;
return mz_zip_entry_read(compat->handle, buf, len);
}
@@ -390,7 +536,7 @@ extern int ZEXPORT unzCloseCurrentFile(unzFile file)
{
mz_compat *compat = (mz_compat *)file;
if (compat == NULL)
return MZ_PARAM_ERROR;
return UNZ_PARAMERROR;
return mz_zip_entry_close(compat->handle);
}
@@ -403,7 +549,8 @@ extern int ZEXPORT unzGetCurrentFileInfo(unzFile file, unz_file_info *pfile_info
int32_t err = MZ_OK;
if (compat == NULL)
return MZ_PARAM_ERROR;
return UNZ_PARAMERROR;
err = mz_zip_entry_get_info(compat->handle, &file_info);
if ((err == MZ_OK) && (pfile_info != NULL))
@@ -412,7 +559,9 @@ extern int ZEXPORT unzGetCurrentFileInfo(unzFile file, unz_file_info *pfile_info
pfile_info->version_needed = file_info->version_needed;
pfile_info->flag = file_info->flag;
pfile_info->compression_method = file_info->compression_method;
pfile_info->dos_date = mz_zip_time_t_to_dos_date(file_info->modified_date);
pfile_info->dosDate = mz_zip_time_t_to_dos_date(file_info->modified_date);
mz_zip_time_t_to_tm(file_info->modified_date, &pfile_info->tmu_date);
pfile_info->tmu_date.tm_year += 1900;
pfile_info->crc = file_info->crc;
pfile_info->size_filename = file_info->filename_size;
@@ -460,7 +609,7 @@ extern int ZEXPORT unzGetCurrentFileInfo64(unzFile file, unz_file_info64 * pfile
int32_t err = MZ_OK;
if (compat == NULL)
return MZ_PARAM_ERROR;
return UNZ_PARAMERROR;
err = mz_zip_entry_get_info(compat->handle, &file_info);
@@ -470,7 +619,9 @@ extern int ZEXPORT unzGetCurrentFileInfo64(unzFile file, unz_file_info64 * pfile
pfile_info->version_needed = file_info->version_needed;
pfile_info->flag = file_info->flag;
pfile_info->compression_method = file_info->compression_method;
pfile_info->dos_date = mz_zip_time_t_to_dos_date(file_info->modified_date);
pfile_info->dosDate = mz_zip_time_t_to_dos_date(file_info->modified_date);
mz_zip_time_t_to_tm(file_info->modified_date, &pfile_info->tmu_date);
pfile_info->tmu_date.tm_year += 1900;
pfile_info->crc = file_info->crc;
pfile_info->size_filename = file_info->filename_size;
@@ -490,7 +641,10 @@ extern int ZEXPORT unzGetCurrentFileInfo64(unzFile file, unz_file_info64 * pfile
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 (extrafield_size > 0 && extrafield != NULL)
{
bytes_to_copy = extrafield_size;
@@ -498,12 +652,15 @@ extern int ZEXPORT unzGetCurrentFileInfo64(unzFile file, unz_file_info64 * pfile
bytes_to_copy = file_info->extrafield_size;
memcpy(extrafield, file_info->extrafield, bytes_to_copy);
}
if (comment_size > 0 && 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;
}
}
return err;
@@ -513,7 +670,7 @@ extern int ZEXPORT unzGoToFirstFile(unzFile file)
{
mz_compat *compat = (mz_compat *)file;
if (compat == NULL)
return MZ_PARAM_ERROR;
return UNZ_PARAMERROR;
return mz_zip_goto_first_entry(compat->handle);
}
@@ -521,7 +678,7 @@ extern int ZEXPORT unzGoToNextFile(unzFile file)
{
mz_compat *compat = (mz_compat *)file;
if (compat == NULL)
return MZ_PARAM_ERROR;
return UNZ_PARAMERROR;
return mz_zip_goto_next_entry(compat->handle);
}
@@ -529,46 +686,94 @@ extern int ZEXPORT unzLocateFile(unzFile file, const char *filename, unzFileName
{
mz_compat *compat = (mz_compat *)file;
if (compat == NULL)
return MZ_PARAM_ERROR;
return UNZ_PARAMERROR;
return mz_zip_locate_entry(compat->handle, filename, filename_compare_func);
}
extern int32_t ZEXPORT unzGetOffset(unzFile file)
{
return (int32_t)unzGetOffset64(file);
}
extern int64_t ZEXPORT unzGetOffset64(unzFile file)
{
mz_compat *compat = (mz_compat *)file;
if (compat == NULL)
return UNZ_PARAMERROR;
return mz_zip_get_entry(compat->handle);
}
extern int ZEXPORT unzSetOffset(unzFile file, uint32_t pos)
{
return unzSetOffset64(file, pos);
}
extern int ZEXPORT unzSetOffset64(unzFile file, uint64_t pos)
{
mz_compat *compat = (mz_compat *)file;
if (compat == NULL)
return UNZ_PARAMERROR;
return (int)mz_zip_goto_entry(compat->handle, pos);
}
extern int ZEXPORT unzGetLocalExtrafield(unzFile file, void *buf, unsigned len) // TODO
{
MZ_UNUSED(file);
MZ_UNUSED(buf);
MZ_UNUSED(len);
return 0;
}
extern 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)
{
if (pzlib_filefunc_def != NULL)
pzlib_filefunc_def = mz_stream_os_get_interface();
*pzlib_filefunc_def = mz_stream_os_get_interface();
}
void fill_fopen64_filefunc(zlib_filefunc64_def *pzlib_filefunc_def)
{
if (pzlib_filefunc_def != NULL)
pzlib_filefunc_def = mz_stream_os_get_interface();
*pzlib_filefunc_def = mz_stream_os_get_interface();
}
void fill_win32_filefunc(zlib_filefunc_def *pzlib_filefunc_def)
{
if (pzlib_filefunc_def != NULL)
pzlib_filefunc_def = mz_stream_os_get_interface();
*pzlib_filefunc_def = mz_stream_os_get_interface();
}
void fill_win32_filefunc64(zlib_filefunc64_def *pzlib_filefunc_def)
{
if (pzlib_filefunc_def != NULL)
pzlib_filefunc_def = mz_stream_os_get_interface();
*pzlib_filefunc_def = mz_stream_os_get_interface();
}
void fill_win32_filefunc64A(zlib_filefunc64_def *pzlib_filefunc_def)
{
if (pzlib_filefunc_def != NULL)
pzlib_filefunc_def = mz_stream_os_get_interface();
*pzlib_filefunc_def = mz_stream_os_get_interface();
}
void fill_win32_filefunc64W(zlib_filefunc64_def *pzlib_filefunc_def)
{
// NOTE: You should no longer pass in widechar string to open function
if (pzlib_filefunc_def != NULL)
pzlib_filefunc_def = mz_stream_os_get_interface();
*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();
}
+81 -59
View File
@@ -1,8 +1,8 @@
/* mz_compat.h -- Backwards compatible interface for older versions
Version 2.2.4, November 15th, 2017
Version 2.3.9, July 26, 2018
part of the MiniZip project
Copyright (C) 2012-2017 Nathan Moinvaziri
Copyright (C) 2010-2018 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
Copyright (C) 1998-2010 Gilles Vollant
http://www.winimage.com/zLibDll/minizip.html
@@ -11,8 +11,8 @@
See the accompanying LICENSE file for the full text of the license.
*/
#ifndef _MZ_COMPAT_H
#define _MZ_COMPAT_H
#ifndef MZ_COMPAT_H
#define MZ_COMPAT_H
#include <stdint.h>
@@ -20,16 +20,8 @@
extern "C" {
#endif
#ifdef HAVE_ZLIB
#include "zlib.h"
/***************************************************************************/
#ifdef __GNUC__
# define ZIP_UNUSED __attribute__((__unused__))
#else
# define ZIP_UNUSED
#endif
#ifndef DEF_MEM_LEVEL
# if MAX_MEM_LEVEL >= 8
# define DEF_MEM_LEVEL 8
@@ -37,6 +29,13 @@ extern "C" {
# define DEF_MEM_LEVEL MAX_MEM_LEVEL
# endif
#endif
#else
#define ZEXPORT
#define MAX_WBITS (15)
#define DEF_MEM_LEVEL (8)
#endif
/***************************************************************************/
#if defined(USE_FILE32API)
# define MZ_USE_FILE32API
@@ -70,7 +69,7 @@ extern "C" {
typedef struct TagzipFile__ { int unused; } zip_file__;
typedef zip_file__ *zipFile;
#else
typedef voidp zipFile;
typedef void *zipFile;
#endif
/***************************************************************************/
@@ -79,11 +78,17 @@ typedef void *zlib_filefunc_def;
typedef void *zlib_filefunc64_def;
typedef const char *zipcharpc;
typedef struct tm tm_unz;
typedef struct tm tm_zip;
typedef uint64_t ZPOS64_T;
/***************************************************************************/
typedef struct
{
uint32_t dos_date;
uint32_t dosDate;
struct tm tmz_date;
uint16_t internal_fa; // internal file attributes 2 bytes
uint32_t external_fa; // external file attributes 4 bytes
} zip_fileinfo;
@@ -92,16 +97,16 @@ typedef struct
#define ZIP_OK (0)
#define ZIP_EOF (0)
#define ZIP_ERRNO (Z_ERRNO)
#define ZIP_ERRNO (-1)
#define ZIP_PARAMERROR (-102)
#define ZIP_BADZIPFILE (-103)
#define ZIP_INTERNALERROR (-104)
#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)
/***************************************************************************/
@@ -109,44 +114,49 @@ 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,
extern zipFile ZEXPORT zipOpen2_64(const void *path, int append, const char **globalcomment,
zlib_filefunc64_def *pzlib_filefunc_def);
extern 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,
ZIP_UNUSED uint32_t crc_for_crypting);
uint32_t crc_for_crypting);
extern int ZEXPORT zipOpenNewFileInZip3_64(zipFile file, const char *filename, const zip_fileinfo *zipfi,
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
uint16_t size_extrafield_global, const char *comment, uint16_t compression_method, int level,
int raw, int windowBits, int memLevel, int strategy, const char *password,
ZIP_UNUSED uint32_t crc_for_crypting, int zip64);
uint32_t crc_for_crypting, int zip64);
extern int ZEXPORT zipOpenNewFileInZip4(zipFile file, const char *filename, const zip_fileinfo *zipfi,
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
uint16_t size_extrafield_global, const char *comment, uint16_t compression_method, int level,
int raw, int windowBits, int memLevel, int strategy, const char *password,
ZIP_UNUSED uint32_t crc_for_crypting, uint16_t version_madeby, uint16_t flag_base);
uint32_t crc_for_crypting, uint16_t version_madeby, uint16_t flag_base);
extern int ZEXPORT zipOpenNewFileInZip4_64(zipFile file, const char *filename, const zip_fileinfo *zipfi,
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
uint16_t size_extrafield_global, const char *comment, uint16_t compression_method, int level,
int raw, int windowBits, int memLevel, int strategy, const char *password,
ZIP_UNUSED uint32_t crc_for_crypting, uint16_t version_madeby, uint16_t flag_base, int zip64);
uint32_t crc_for_crypting, uint16_t version_madeby, uint16_t flag_base, int zip64);
extern int ZEXPORT zipOpenNewFileInZip5(zipFile file, const char *filename, const zip_fileinfo *zipfi,
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
uint16_t size_extrafield_global, const char *comment, uint16_t compression_method, int level,
int raw, int windowBits, int memLevel, int strategy, const char *password,
ZIP_UNUSED uint32_t crc_for_crypting, uint16_t version_madeby, uint16_t flag_base, int zip64);
uint32_t crc_for_crypting, uint16_t version_madeby, uint16_t flag_base, int zip64);
extern int ZEXPORT zipWriteInFileInZip(zipFile file, const void *buf, uint32_t len);
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);
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);
extern int ZEXPORT zipClose_MZ(zipFile file, const char *global_comment);
extern int ZEXPORT zipClose2_MZ(zipFile file, const char *global_comment, uint16_t version_madeby);
extern void* ZEXPORT zipGetStream(zipFile file);
/***************************************************************************/
@@ -156,14 +166,14 @@ extern int ZEXPORT zipClose2_64(zipFile file, const char *global_comment, uint16
typedef struct TagunzFile__ { int unused; } unz_file__;
typedef unz_file__ *unzFile;
#else
typedef voidp unzFile;
typedef void *unzFile;
#endif
/***************************************************************************/
#define UNZ_OK (0)
#define UNZ_END_OF_LIST_OF_FILE (-100)
#define UNZ_ERRNO (Z_ERRNO)
#define UNZ_ERRNO (-1)
#define UNZ_EOF (0)
#define UNZ_PARAMERROR (-102)
#define UNZ_BADZIPFILE (-103)
@@ -175,35 +185,36 @@ typedef voidp unzFile;
typedef struct unz_global_info64_s
{
uint64_t number_entry; // total number of entries in the central dir on this disk
uint64_t number_entry; // total number of entries in the central dir on this disk
uint32_t number_disk_with_CD; // number the the disk with central dir, used for spanning ZIP
uint16_t size_comment; // size of the global comment of the zipfile
uint16_t size_comment; // size of the global comment of the zipfile
} unz_global_info64;
typedef struct unz_global_info_s
{
uint32_t number_entry; // total number of entries in the central dir on this disk
uint32_t number_entry; // total number of entries in the central dir on this disk
uint32_t number_disk_with_CD; // number the the disk with central dir, used for spanning ZIP
uint16_t size_comment; // size of the global comment of the zipfile
uint16_t size_comment; // size of the global comment of the zipfile
} unz_global_info;
typedef struct unz_file_info64_s
{
uint16_t version; // version made by 2 bytes
uint16_t version_needed; // version needed to extract 2 bytes
uint16_t flag; // general purpose bit flag 2 bytes
uint16_t compression_method; // compression method 2 bytes
uint32_t dos_date; // last mod file date in Dos fmt 4 bytes
uint32_t crc; // crc-32 4 bytes
uint64_t compressed_size; // compressed size 8 bytes
uint64_t uncompressed_size; // uncompressed size 8 bytes
uint16_t size_filename; // filename length 2 bytes
uint16_t size_file_extra; // extra field length 2 bytes
uint16_t size_file_comment; // file comment length 2 bytes
uint16_t version; // version made by 2 bytes
uint16_t version_needed; // version needed to extract 2 bytes
uint16_t flag; // general purpose bit flag 2 bytes
uint16_t compression_method; // compression method 2 bytes
uint32_t dosDate; // last mod file date in Dos fmt 4 bytes
struct tm tmu_date;
uint32_t crc; // crc-32 4 bytes
uint64_t compressed_size; // compressed size 8 bytes
uint64_t uncompressed_size; // uncompressed size 8 bytes
uint16_t size_filename; // filename length 2 bytes
uint16_t size_file_extra; // extra field length 2 bytes
uint16_t size_file_comment; // file comment length 2 bytes
uint32_t disk_num_start; // disk number start 4 bytes
uint16_t internal_fa; // internal file attributes 2 bytes
uint32_t external_fa; // external file attributes 4 bytes
uint32_t disk_num_start; // disk number start 4 bytes
uint16_t internal_fa; // internal file attributes 2 bytes
uint32_t external_fa; // external file attributes 4 bytes
uint64_t disk_offset;
@@ -212,20 +223,21 @@ typedef struct unz_file_info64_s
typedef struct unz_file_info_s
{
uint16_t version; // version made by 2 bytes
uint16_t version_needed; // version needed to extract 2 bytes
uint16_t flag; // general purpose bit flag 2 bytes
uint16_t compression_method; // compression method 2 bytes
uint32_t dos_date; // last mod file date in Dos fmt 4 bytes
uint32_t crc; // crc-32 4 bytes
uint32_t compressed_size; // compressed size 4 bytes
uint32_t uncompressed_size; // uncompressed size 4 bytes
uint16_t size_filename; // filename length 2 bytes
uint16_t size_file_extra; // extra field length 2 bytes
uint16_t size_file_comment; // file comment length 2 bytes
uint16_t version; // version made by 2 bytes
uint16_t version_needed; // version needed to extract 2 bytes
uint16_t flag; // general purpose bit flag 2 bytes
uint16_t compression_method; // compression method 2 bytes
uint32_t dosDate; // last mod file date in Dos fmt 4 bytes
struct tm tmu_date;
uint32_t crc; // crc-32 4 bytes
uint32_t compressed_size; // compressed size 4 bytes
uint32_t uncompressed_size; // uncompressed size 4 bytes
uint16_t size_filename; // filename length 2 bytes
uint16_t size_file_extra; // extra field length 2 bytes
uint16_t size_file_comment; // file comment length 2 bytes
uint16_t disk_num_start; // disk number start 2 bytes
uint16_t internal_fa; // internal file attributes 2 bytes
uint16_t disk_num_start; // disk number start 2 bytes
uint16_t internal_fa; // internal file attributes 2 bytes
uint32_t external_fa; // external file attributes 4 bytes
uint64_t disk_offset;
@@ -244,8 +256,10 @@ 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);
extern unzFile ZEXPORT unzOpen_MZ(void *stream);
extern int ZEXPORT unzClose(unzFile file);
extern int ZEXPORT unzClose_MZ(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);
@@ -253,7 +267,7 @@ extern int ZEXPORT unzOpenCurrentFile(unzFile file);
extern int ZEXPORT unzOpenCurrentFilePassword(unzFile file, const char *password);
extern int ZEXPORT unzOpenCurrentFile2(unzFile file, int *method, int *level, int raw);
extern int ZEXPORT unzOpenCurrentFile3(unzFile file, int *method, int *level, int raw, const char *password);
extern int ZEXPORT unzReadCurrentFile(unzFile file, voidp buf, uint32_t len);
extern int ZEXPORT unzReadCurrentFile(unzFile file, void *buf, uint32_t len);
extern int ZEXPORT unzCloseCurrentFile(unzFile file);
extern int ZEXPORT unzGetCurrentFileInfo(unzFile file, unz_file_info *pfile_info, char *filename,
@@ -264,6 +278,13 @@ 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);
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);
extern void* ZEXPORT unzGetStream(unzFile file);
/***************************************************************************/
void fill_fopen_filefunc(zlib_filefunc_def *pzlib_filefunc_def);
@@ -272,6 +293,7 @@ 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);
/***************************************************************************/
+279
View File
@@ -0,0 +1,279 @@
/* mz_os.c -- System functions
Version 2.3.9, July 26, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
Copyright (C) 1998-2010 Gilles Vollant
http://www.winimage.com/zLibDll/minizip.html
This program is distributed under the terms of the same license as zlib.
See the accompanying LICENSE file for the full text of the license.
*/
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "mz.h"
#include "mz_os.h"
#include "mz_strm.h"
#include "mz_strm_crc32.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;
if (path == NULL || join == NULL || max_path == 0)
return MZ_PARAM_ERROR;
path_len = strlen(path);
if (path_len == 0)
{
strncpy(path, join, max_path);
}
else
{
if (path[path_len - 1] != '\\' && path[path_len - 1] != '/')
strncat(path, "/", max_path - path_len - 1);
strncat(path, join, max_path - path_len);
}
return MZ_OK;
}
int32_t mz_path_resolve(const char *path, char *output, int32_t max_output)
{
const char *source = path;
const char *check = output;
char *target = output;
if (max_output <= 0)
return MZ_PARAM_ERROR;
while (*source != 0 && max_output > 1)
{
check = source;
if ((*check == '\\') || (*check == '/'))
check += 1;
if ((source == path) || (check != source) || (*target == 0))
{
// Skip double paths
if ((*check == '\\') || (*check == '/'))
{
source += 1;
continue;
}
if ((*check != 0) && (*check == '.'))
{
check += 1;
// Remove current directory . if at end of stirng
if ((*check == 0) && (source != path))
{
// Copy last slash
*target = *source;
target += 1;
max_output -= 1;
source += (check - source);
continue;
}
// Remove current directory . if not at end of stirng
if ((*check == 0) || (*check == '\\' || *check == '/'))
{
// Only proceed if .\ is not entire string
if (check[1] != 0 || (path != source))
{
source += (check - source);
continue;
}
}
// Go to parent directory ..
if ((*check != 0) || (*check == '.'))
{
check += 1;
if ((*check == 0) || (*check == '\\' || *check == '/'))
{
source += (check - source);
// Search backwards for previous slash
if (target != output)
{
target -= 1;
do
{
if ((*target == '\\') || (*target == '/'))
break;
target -= 1;
max_output += 1;
}
while (target > output);
}
if ((target == output) && (*source != 0))
source += 1;
if ((*target == '\\' || *target == '/') && (*source == 0))
target += 1;
*target = 0;
continue;
}
}
}
}
*target = *source;
source += 1;
target += 1;
max_output -= 1;
}
*target = 0;
if (*path == 0)
return MZ_INTERNAL_ERROR;
return MZ_OK;
}
int32_t mz_path_remove_filename(const char *path)
{
char *path_ptr = NULL;
if (path == NULL)
return MZ_PARAM_ERROR;
path_ptr = (char *)(path + strlen(path) - 1);
while (path_ptr > path)
{
if ((*path_ptr == '/') || (*path_ptr == '\\'))
{
*path_ptr = 0;
break;
}
path_ptr -= 1;
}
return MZ_OK;
}
int32_t mz_path_get_filename(const char *path, const char **filename)
{
const char *match = NULL;
if (path == NULL || filename == NULL)
return MZ_PARAM_ERROR;
*filename = NULL;
for (match = path; *match != 0; match += 1)
{
if ((*match == '\\') || (*match == '/'))
*filename = match + 1;
}
if (*filename == NULL)
return MZ_EXIST_ERROR;
return MZ_OK;
}
int32_t mz_get_file_crc(const char *path, uint32_t *result_crc)
{
void *stream = NULL;
void *crc32_stream = NULL;
int32_t read = 0;
int32_t err = MZ_OK;
uint8_t buf[INT16_MAX];
mz_stream_os_create(&stream);
err = mz_stream_os_open(stream, path, MZ_OPEN_MODE_READ);
mz_stream_crc32_create(&crc32_stream);
mz_stream_crc32_open(crc32_stream, NULL, MZ_OPEN_MODE_READ);
mz_stream_set_base(crc32_stream, stream);
if (err == MZ_OK)
{
do
{
read = mz_stream_crc32_read(crc32_stream, buf, sizeof(buf));
if (read < 0)
{
err = read;
break;
}
}
while ((err == MZ_OK) && (read > 0));
mz_stream_os_close(stream);
}
mz_stream_crc32_close(crc32_stream);
*result_crc = mz_stream_crc32_get_value(crc32_stream);
mz_stream_crc32_delete(&crc32_stream);
mz_stream_os_delete(&stream);
return err;
}
+28 -6
View File
@@ -1,18 +1,16 @@
/* mz_os.h -- System functions
Version 2.2.4, November 15th, 2017
Version 2.3.9, July 26, 2018
part of the MiniZip project
Copyright (C) 2012-2017 Nathan Moinvaziri
Copyright (C) 2010-2018 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
Copyright (C) 1998-2010 Gilles Vollant
http://www.winimage.com/zLibDll/minizip.html
This program is distributed under the terms of the same license as zlib.
See the accompanying LICENSE file for the full text of the license.
*/
#ifndef _MZ_OS_H
#define _MZ_OS_H
#ifndef MZ_OS_H
#define MZ_OS_H
#include <stdint.h>
@@ -32,12 +30,36 @@ extern "C" {
/***************************************************************************/
#ifdef HAVE_LZMA
#define MZ_VERSION_MADEBY_ZIP_VERSION (63)
#elif HAVE_AES
#define MZ_VERSION_MADEBY_ZIP_VERSION (51)
#elif HAVE_BZIP2
#define MZ_VERSION_MADEBY_ZIP_VERSION (46)
#else
#define MZ_VERSION_MADEBY_ZIP_VERSION (45)
#endif
#define MZ_VERSION_MADEBY ((MZ_VERSION_MADEBY_HOST_SYSTEM << 8) | \
(MZ_VERSION_MADEBY_ZIP_VERSION))
/***************************************************************************/
int32_t mz_make_dir(const char *path);
// Creates a directory recursively
int32_t mz_path_combine(char *path, const char *join, int32_t max_path);
// Combines two paths
int32_t mz_path_resolve(const char *path, char *target, int32_t max_target);
// Resolves path
int32_t mz_path_remove_filename(const char *path);
// Remove the filename from a path
int32_t mz_path_get_filename(const char *path, const char **filename);
// Get the filename from a path
int32_t mz_get_file_crc(const char *path, uint32_t *result_crc);
// Gets the crc32 hash of a file
+97 -8
View File
@@ -1,8 +1,8 @@
/* mz_os_posix.c -- System functions for posix
Version 2.2.4, November 15th, 2017
Version 2.3.9, July 26, 2018
part of the MiniZip project
Copyright (C) 2012-2017 Nathan Moinvaziri
Copyright (C) 2010-2018 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
This program is distributed under the terms of the same license as zlib.
@@ -16,13 +16,22 @@
#include <sys/types.h>
#include <sys/stat.h>
#ifdef HAVE_GETRANDOM
# include <sys/random.h>
#endif
#if defined unix || defined __APPLE__
# include <unistd.h>
# include <utime.h>
# define HAVE_ARC4RANDOM_BUF
#endif
#if defined unix
# include <bsd/stdlib.h>
#if defined __linux__
# if !defined(MZ_ZIP_NO_COMPRESSION) && \
!defined(MZ_ZIP_NO_ENCRYPTION) && \
defined(HAVE_LIBBSD)
# include <bsd/stdlib.h> // arc4random_buf
# endif
#else
# include <stdlib.h>
#endif
#include "mz.h"
@@ -32,11 +41,71 @@
/***************************************************************************/
#if !defined(MZ_ZIP_NO_COMPRESSION) && !defined(MZ_ZIP_NO_ENCRYPTION)
#if defined(HAVE_LIBBSD) || defined(HAVE_ARC4RANDOM_BUF)
int32_t mz_posix_rand(uint8_t *buf, int32_t size)
{
arc4random_buf(buf, size);
return size;
}
#elif defined(HAVE_ARC4RANDOM)
int32_t mz_posix_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_posix_rand(uint8_t *buf, int32_t size)
{
int32_t left = size;
int32_t written = 0;
while (left > 0)
{
written = getrandom(buf, left, 0);
if (written < 0)
return MZ_INTERNAL_ERROR;
buf += written;
left -= written;
}
return size - left;
}
#else
#if !defined(FORCE_LOWQUALITY_ENTROPY)
# error "Low quality entropy function used for encryption"
#endif
int32_t mz_posix_rand(uint8_t *buf, int32_t size)
{
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
#endif
int32_t mz_posix_file_exists(const char *path)
{
@@ -63,13 +132,10 @@ 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)
{
struct stat stat_info;
struct tm *filedate = NULL;
char *name = NULL;
size_t len = 0;
time_t tm_t = 0;
int32_t err = MZ_INTERNAL_ERROR;
memset(&stat_info, 0, sizeof(stat_info));
if (strcmp(path, "-") != 0)
@@ -108,6 +174,7 @@ int32_t mz_posix_set_file_date(const char *path, time_t modified_date, time_t ac
ut.actime = accessed_date;
ut.modtime = modified_date;
// Creation date not supported
(void)creation_date;
if (utime(path, &ut) != 0)
return MZ_INTERNAL_ERROR;
@@ -115,6 +182,28 @@ 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)
{
struct stat stat_info;
int32_t err = MZ_OK;
memset(&stat_info, 0, sizeof(stat_info));
if (stat(path, &stat_info) == -1)
err = MZ_INTERNAL_ERROR;
*attributes = stat_info.st_mode;
return err;
}
int32_t mz_posix_set_file_attribs(const char *path, uint32_t attributes)
{
int32_t err = MZ_OK;
if (chmod(path, (mode_t)attributes) == -1)
err = MZ_INTERNAL_ERROR;
return err;
}
int32_t mz_posix_make_dir(const char *path)
{
int32_t err = 0;
+21 -17
View File
@@ -1,16 +1,16 @@
/* mz_os_posix.h -- System functions for posix
Version 2.2.4, November 15th, 2017
Version 2.3.9, July 26, 2018
part of the MiniZip project
Copyright (C) 2012-2017 Nathan Moinvaziri
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
#ifndef MZ_OS_POSIX_H
#define MZ_OS_POSIX_H
#include <stdint.h>
#include <time.h>
@@ -23,9 +23,9 @@ extern "C" {
/***************************************************************************/
#if defined(__APPLE__)
#define MZ_VERSION_MADEBY (19)
#define MZ_VERSION_MADEBY_HOST_SYSTEM (MZ_HOST_SYSTEM_OSX_DARWIN)
#elif defined(unix)
#define MZ_VERSION_MADEBY (3)
#define MZ_VERSION_MADEBY_HOST_SYSTEM (MZ_HOST_SYSTEM_UNIX)
#endif
/***************************************************************************/
@@ -35,25 +35,29 @@ 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
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_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
#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
/***************************************************************************/
+49 -18
View File
@@ -1,8 +1,8 @@
/* mz_os_win32.c -- System functions for Windows
Version 2.2.4, November 15th, 2017
Version 2.3.9, July 26, 2018
part of the MiniZip project
Copyright (C) 2012-2017 Nathan Moinvaziri
Copyright (C) 2010-2018 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
This program is distributed under the terms of the same license as zlib.
@@ -17,7 +17,9 @@
#include <errno.h>
#include <windows.h>
#include <wincrypt.h>
#if !defined(MZ_ZIP_NO_COMPRESSION) && !defined(MZ_ZIP_NO_ENCRYPTION)
# include <wincrypt.h>
#endif
#include "mz.h"
@@ -26,9 +28,9 @@
/***************************************************************************/
#if defined(WINAPI_FAMILY_PARTITION) && (!(defined(MZ_USE_WINRT_API)))
#if defined(WINAPI_FAMILY_PARTITION) && (!(defined(MZ_WINRT_API)))
# if !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
# define MZ_USE_WINRT_API 1
# define MZ_WINRT_API 1
# endif
#endif
@@ -43,6 +45,7 @@ typedef struct DIR_int_s {
/***************************************************************************/
#if !defined(MZ_ZIP_NO_COMPRESSION) && !defined(MZ_ZIP_NO_ENCRYPTION)
int32_t mz_win32_rand(uint8_t *buf, int32_t size)
{
HCRYPTPROV provider;
@@ -68,6 +71,7 @@ int32_t mz_win32_rand(uint8_t *buf, int32_t size)
return len;
}
#endif
wchar_t *mz_win32_unicode_path_create(const char *path)
{
@@ -75,7 +79,7 @@ wchar_t *mz_win32_unicode_path_create(const char *path)
uint32_t path_wide_size = 0;
path_wide_size = MultiByteToWideChar(CP_UTF8, 0, path, -1, NULL, 0);
path_wide = (wchar_t *)malloc((path_wide_size + 1) * sizeof(wchar_t));
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);
@@ -87,7 +91,7 @@ void mz_win32_unicode_path_delete(wchar_t **path)
{
if (path != NULL)
{
free(*path);
MZ_FREE(*path);
*path = NULL;
}
}
@@ -113,11 +117,10 @@ int64_t mz_win32_get_file_size(const char *path)
HANDLE handle = NULL;
LARGE_INTEGER large_size;
wchar_t *path_wide = NULL;
int32_t err = MZ_OK;
path_wide = mz_win32_unicode_path_create(path);
#ifdef MZ_USE_WINRT_API
#ifdef MZ_WINRT_API
handle = CreateFile2W(path_wide, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
#else
handle = CreateFileW(path_wide, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
@@ -160,7 +163,7 @@ int32_t mz_win32_get_file_date(const char *path, time_t *modified_date, time_t *
path_wide = mz_win32_unicode_path_create(path);
handle = FindFirstFileW(path_wide, &ff32);
free(path_wide);
MZ_FREE(path_wide);
if (handle != INVALID_HANDLE_VALUE)
{
@@ -187,7 +190,7 @@ int32_t mz_win32_set_file_date(const char *path, time_t modified_date, time_t ac
path_wide = mz_win32_unicode_path_create(path);
#ifdef MZ_USE_WINRT_API
#ifdef MZ_WINRT_API
handle = CreateFile2W(path_wide, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
#else
handle = CreateFileW(path_wide, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
@@ -214,6 +217,34 @@ 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)
{
wchar_t *path_wide = NULL;
int32_t err = MZ_OK;
path_wide = mz_win32_unicode_path_create(path);
*attributes = GetFileAttributesW(path_wide);
MZ_FREE(path_wide);
if (*attributes == INVALID_FILE_ATTRIBUTES)
err = MZ_INTERNAL_ERROR;
return err;
}
int32_t mz_win32_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 (SetFileAttributesW(path_wide, attributes) == 0)
err = MZ_INTERNAL_ERROR;
MZ_FREE(path_wide);
return err;
}
int32_t mz_win32_make_dir(const char *path)
{
wchar_t *path_wide = NULL;
@@ -250,7 +281,7 @@ DIR *mz_win32_open_dir(const char *path)
if (handle == INVALID_HANDLE_VALUE)
return NULL;
dir_int = (DIR_int *)malloc(sizeof(DIR_int));
dir_int = (DIR_int *)MZ_ALLOC(sizeof(DIR_int));
dir_int->find_handle = handle;
dir_int->end = 0;
@@ -270,9 +301,9 @@ struct dirent* mz_win32_read_dir(DIR *dir)
if (dir_int->end)
return NULL;
WideCharToMultiByte(CP_UTF8, 0, dir_int->find_data.cFileName, -1,
WideCharToMultiByte(CP_UTF8, 0, dir_int->find_data.cFileName, -1,
dir_int->entry.d_name, sizeof(dir_int->entry.d_name), NULL, NULL);
if (FindNextFileW(dir_int->find_handle, &dir_int->find_data) == 0)
{
if (GetLastError() != ERROR_NO_MORE_FILES)
@@ -280,7 +311,7 @@ struct dirent* mz_win32_read_dir(DIR *dir)
dir_int->end = 1;
}
return &dir_int->entry;
}
@@ -294,14 +325,14 @@ int32_t mz_win32_close_dir(DIR *dir)
dir_int = (DIR_int *)dir;
if (dir_int->find_handle != INVALID_HANDLE_VALUE)
FindClose(dir_int->find_handle);
free(dir_int);
MZ_FREE(dir_int);
return MZ_OK;
}
int32_t mz_win32_is_dir(const char *path)
{
wchar_t *path_wide = NULL;
int32_t attribs = 0;
uint32_t attribs = 0;
path_wide = mz_win32_unicode_path_create(path);
attribs = GetFileAttributesW(path_wide);
@@ -314,4 +345,4 @@ int32_t mz_win32_is_dir(const char *path)
}
return MZ_EXIST_ERROR;
}
}
+20 -16
View File
@@ -1,16 +1,16 @@
/* mz_os_win32.h -- System functions for Windows
Version 2.2.4, November 15th, 2017
Version 2.3.9, July 26, 2018
part of the MiniZip project
Copyright (C) 2012-2017 Nathan Moinvaziri
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
#ifndef MZ_OS_WIN32_H
#define MZ_OS_WIN32_H
#include <stdint.h>
@@ -20,7 +20,7 @@ extern "C" {
/***************************************************************************/
#define MZ_VERSION_MADEBY (10)
#define MZ_VERSION_MADEBY_HOST_SYSTEM (MZ_HOST_SYSTEM_WINDOWS_NTFS)
/***************************************************************************/
@@ -36,25 +36,29 @@ 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
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_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
#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
/***************************************************************************/
Executable → Regular
+20 -161
View File
@@ -1,8 +1,8 @@
/* mz_strm.c -- Stream interface
Version 2.2.4, November 15th, 2017
Version 2.3.9, July 26, 2018
part of the MiniZip project
Copyright (C) 2012-2017 Nathan Moinvaziri
Copyright (C) 2010-2018 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
This program is distributed under the terms of the same license as zlib.
@@ -13,8 +13,6 @@
#include <stdint.h>
#include <string.h>
#include <time.h>
#include "mz.h"
#include "mz_strm.h"
@@ -173,7 +171,7 @@ int32_t mz_stream_copy(void *target, void *source, int32_t len)
while (len > 0)
{
bytes_to_copy = len;
if (bytes_to_copy > sizeof(buf))
if (bytes_to_copy > (int32_t)sizeof(buf))
bytes_to_copy = sizeof(buf);
read = mz_stream_read(source, buf, bytes_to_copy);
if (read < 0)
@@ -232,6 +230,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;
@@ -270,159 +276,6 @@ void mz_stream_delete(void **stream)
/***************************************************************************/
mz_stream_vtbl mz_stream_crc32_vtbl = {
mz_stream_crc32_open,
mz_stream_crc32_is_open,
mz_stream_crc32_read,
mz_stream_crc32_write,
mz_stream_crc32_tell,
mz_stream_crc32_seek,
mz_stream_crc32_close,
mz_stream_crc32_error,
mz_stream_crc32_create,
mz_stream_crc32_delete,
mz_stream_crc32_get_prop_int64
};
/***************************************************************************/
typedef struct mz_stream_crc32_s {
mz_stream stream;
int8_t initialized;
int32_t value;
int64_t total_in;
int64_t total_out;
mz_stream_crc32_update update;
} mz_stream_crc32;
/***************************************************************************/
int32_t mz_stream_crc32_open(void *stream, const char *path, int32_t mode)
{
mz_stream_crc32 *crc32 = (mz_stream_crc32 *)stream;
crc32->initialized = 1;
crc32->value = 0;
return MZ_OK;
}
int32_t mz_stream_crc32_is_open(void *stream)
{
mz_stream_crc32 *crc32 = (mz_stream_crc32 *)stream;
if (crc32->initialized != 1)
return MZ_STREAM_ERROR;
return MZ_OK;
}
int32_t mz_stream_crc32_read(void *stream, void *buf, int32_t size)
{
mz_stream_crc32 *crc32x = (mz_stream_crc32 *)stream;
int32_t read = 0;
read = mz_stream_read(crc32x->stream.base, buf, size);
if (read > 0)
crc32x->value = crc32x->update(crc32x->value, buf, read);
crc32x->total_in += read;
return read;
}
int32_t mz_stream_crc32_write(void *stream, const void *buf, int32_t size)
{
mz_stream_crc32 *crc32x = (mz_stream_crc32 *)stream;
int32_t written = 0;
crc32x->value = crc32x->update(crc32x->value, buf, size);
written = mz_stream_write(crc32x->stream.base, buf, size);
crc32x->total_out += written;
return written;
}
int64_t mz_stream_crc32_tell(void *stream)
{
mz_stream_crc32 *crc32 = (mz_stream_crc32 *)stream;
return mz_stream_tell(crc32->stream.base);
}
int32_t mz_stream_crc32_seek(void *stream, int64_t offset, int32_t origin)
{
mz_stream_crc32 *crc32 = (mz_stream_crc32 *)stream;
crc32->value = 0;
return mz_stream_seek(crc32->stream.base, offset, origin);
}
int32_t mz_stream_crc32_close(void *stream)
{
mz_stream_crc32 *crc32 = (mz_stream_crc32 *)stream;
crc32->initialized = 0;
return MZ_OK;
}
int32_t mz_stream_crc32_error(void *stream)
{
mz_stream_crc32 *crc32 = (mz_stream_crc32 *)stream;
return mz_stream_error(crc32->stream.base);
}
int32_t mz_stream_crc32_get_value(void *stream)
{
mz_stream_crc32 *crc32 = (mz_stream_crc32 *)stream;
return crc32->value;
}
int32_t mz_stream_crc32_get_prop_int64(void *stream, int32_t prop, int64_t *value)
{
mz_stream_crc32 *crc32 = (mz_stream_crc32 *)stream;
switch (prop)
{
case MZ_STREAM_PROP_TOTAL_IN:
*value = crc32->total_in;
return MZ_OK;
case MZ_STREAM_PROP_TOTAL_OUT:
*value = crc32->total_out;
return MZ_OK;
}
return MZ_EXIST_ERROR;
}
int32_t mz_stream_crc32_set_update_func(void *stream, mz_stream_crc32_update update)
{
mz_stream_crc32 *crc32 = (mz_stream_crc32 *)stream;
crc32->update = update;
return MZ_OK;
}
void *mz_stream_crc32_create(void **stream)
{
mz_stream_crc32 *crc32 = NULL;
crc32 = (mz_stream_crc32 *)malloc(sizeof(mz_stream_crc32));
if (crc32 != NULL)
{
memset(crc32, 0, sizeof(mz_stream_crc32));
crc32->stream.vtbl = &mz_stream_crc32_vtbl;
}
if (stream != NULL)
*stream = crc32;
return crc32;
}
void mz_stream_crc32_delete(void **stream)
{
mz_stream_crc32 *crc32 = NULL;
if (stream == NULL)
return;
crc32 = (mz_stream_crc32 *)*stream;
if (crc32 != NULL)
free(crc32);
*stream = NULL;
}
void *mz_stream_crc32_get_interface(void)
{
return (void *)&mz_stream_crc32_vtbl;
}
/***************************************************************************/
typedef struct mz_stream_raw_s {
mz_stream stream;
int64_t total_in;
@@ -434,6 +287,10 @@ typedef struct mz_stream_raw_s {
int32_t mz_stream_raw_open(void *stream, const char *path, int32_t mode)
{
MZ_UNUSED(stream);
MZ_UNUSED(path);
MZ_UNUSED(mode);
return MZ_OK;
}
@@ -486,6 +343,8 @@ int32_t mz_stream_raw_seek(void *stream, int64_t offset, int32_t origin)
int32_t mz_stream_raw_close(void *stream)
{
MZ_UNUSED(stream);
return MZ_OK;
}
@@ -524,7 +383,7 @@ int32_t mz_stream_raw_set_prop_int64(void *stream, int32_t prop, int64_t value)
/***************************************************************************/
mz_stream_vtbl mz_stream_raw_vtbl = {
static mz_stream_vtbl mz_stream_raw_vtbl = {
mz_stream_raw_open,
mz_stream_raw_is_open,
mz_stream_raw_read,
@@ -545,7 +404,7 @@ void *mz_stream_raw_create(void **stream)
{
mz_stream_raw *raw = NULL;
raw = (mz_stream_raw *)malloc(sizeof(mz_stream_raw));
raw = (mz_stream_raw *)MZ_ALLOC(sizeof(mz_stream_raw));
if (raw != NULL)
{
memset(raw, 0, sizeof(mz_stream_raw));
@@ -564,6 +423,6 @@ void mz_stream_raw_delete(void **stream)
return;
raw = (mz_stream_raw *)*stream;
if (raw != NULL)
free(raw);
MZ_FREE(raw);
*stream = NULL;
}
Executable → Regular
+15 -25
View File
@@ -1,16 +1,16 @@
/* mz_strm.h -- Stream interface
Version 2.2.4, November 15th, 2017
Version 2.3.9, July 26, 2018
part of the MiniZip project
Copyright (C) 2012-2017 Nathan Moinvaziri
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_H
#define _MZ_STREAM_H
#ifndef MZ_STREAM_H
#define MZ_STREAM_H
#include <stdint.h>
@@ -91,6 +91,7 @@ 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);
@@ -99,28 +100,17 @@ void mz_stream_delete(void **stream);
/***************************************************************************/
typedef int32_t (*mz_stream_crc32_update)(int32_t value, const void *buf, int32_t size);
int32_t mz_stream_raw_open(void *stream, const char *filename, int32_t mode);
int32_t mz_stream_raw_is_open(void *stream);
int32_t mz_stream_raw_read(void *stream, void *buf, int32_t size);
int32_t mz_stream_raw_write(void *stream, const void *buf, int32_t size);
int64_t mz_stream_raw_tell(void *stream);
int32_t mz_stream_raw_seek(void *stream, int64_t offset, int32_t origin);
int32_t mz_stream_raw_close(void *stream);
int32_t mz_stream_raw_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);
int32_t mz_stream_crc32_get_prop_int64(void *stream, int32_t prop, int64_t *value);
int32_t mz_stream_crc32_set_update_func(void *stream, mz_stream_crc32_update update);
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_raw_get_prop_int64(void *stream, int32_t prop, int64_t *value);
int32_t mz_stream_raw_set_prop_int64(void *stream, int32_t prop, int64_t value);
void* mz_stream_raw_create(void **stream);
void mz_stream_raw_delete(void **stream);
+49 -20
View File
@@ -1,8 +1,8 @@
/* mz_strm_aes.c -- Stream for WinZip AES encryption
Version 2.2.4, November 15th, 2017
Version 2.3.9, July 26, 2018
part of the MiniZip project
Copyright (C) 2012-2017 Nathan Moinvaziri
Copyright (C) 2010-2018 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
Copyright (C) 1998-2010 Brian Gladman, Worcester, UK
@@ -38,7 +38,7 @@
/***************************************************************************/
mz_stream_vtbl mz_stream_aes_vtbl = {
static mz_stream_vtbl mz_stream_aes_vtbl = {
mz_stream_aes_open,
mz_stream_aes_is_open,
mz_stream_aes_read,
@@ -49,7 +49,8 @@ mz_stream_vtbl mz_stream_aes_vtbl = {
mz_stream_aes_error,
mz_stream_aes_create,
mz_stream_aes_delete,
mz_stream_aes_get_prop_int64
mz_stream_aes_get_prop_int64,
mz_stream_aes_set_prop_int64
};
/***************************************************************************/
@@ -61,12 +62,13 @@ typedef struct mz_stream_aes_s {
int16_t initialized;
uint8_t buffer[INT16_MAX];
int64_t total_in;
int64_t max_total_in;
int64_t total_out;
int16_t encryption_mode;
const char *password;
aes_encrypt_ctx encr_ctx[1];
hmac_ctx auth_ctx[1];
uint8_t nonce[AES_BLOCK_SIZE];
uint8_t nonce[AES_BLOCK_SIZE];
uint8_t encr_bfr[AES_BLOCK_SIZE];
uint32_t encr_pos;
} mz_stream_aes;
@@ -84,7 +86,6 @@ int32_t mz_stream_aes_open(void *stream, const char *path, int32_t mode)
const char *password = path;
int32_t password_length = 0;
int32_t key_length = 0;
int32_t err = MZ_OK;
aes->total_in = 0;
aes->total_out = 0;
@@ -107,22 +108,30 @@ int32_t mz_stream_aes_open(void *stream, const char *path, int32_t mode)
if (mode & MZ_OPEN_MODE_WRITE)
{
#ifdef MZ_ZIP_NO_COMPRESSION
return MZ_SUPPORT_ERROR;
#else
mz_os_rand(salt_value, salt_length);
#endif
}
else if (mode & MZ_OPEN_MODE_READ)
{
#ifdef MZ_ZIP_NO_DECOMPRESSION
return MZ_SUPPORT_ERROR;
#else
if (mz_stream_read(aes->stream.base, salt_value, salt_length) != salt_length)
return MZ_STREAM_ERROR;
#endif
}
key_length = MZ_AES_KEY_LENGTH(aes->encryption_mode);
// Derive the encryption and authentication keys and the password verifier
derive_key(password, password_length, salt_value, salt_length,
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(unsigned char));
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);
@@ -155,7 +164,7 @@ int32_t mz_stream_aes_open(void *stream, const char *path, int32_t mode)
aes->total_in += MZ_AES_PW_VERIFY_SIZE;
if (memcmp(verify_expected, verify, MZ_AES_PW_VERIFY_SIZE) != 0)
return MZ_STREAM_ERROR;
return MZ_PASSWORD_ERROR;
}
aes->mode = mode;
@@ -172,7 +181,7 @@ int32_t mz_stream_aes_is_open(void *stream)
return MZ_OK;
}
int32_t mz_stream_aes_encrypt_data(void *stream, uint8_t *buf, int32_t size)
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;
@@ -220,7 +229,7 @@ int32_t mz_stream_aes_write(void *stream, const void *buf, int32_t size)
mz_stream_aes *aes = (mz_stream_aes *)stream;
int32_t written = 0;
if (size > sizeof(aes->buffer))
if (size > (int32_t)sizeof(aes->buffer))
return MZ_STREAM_ERROR;
memcpy(aes->buffer, buf, size);
@@ -269,6 +278,7 @@ int32_t mz_stream_aes_close(void *stream)
aes->total_in += MZ_AES_AUTHCODE_SIZE;
// If entire entry was not read this will fail
if (memcmp(authcode, verify_authcode, MZ_AES_AUTHCODE_SIZE) != 0)
return MZ_CRC_ERROR;
}
@@ -300,27 +310,46 @@ 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:
case MZ_STREAM_PROP_TOTAL_IN:
*value = aes->total_in;
return MZ_OK;
case MZ_STREAM_PROP_TOTAL_OUT:
break;
case MZ_STREAM_PROP_TOTAL_OUT:
*value = aes->total_out;
return MZ_OK;
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;
return MZ_OK;
break;
case MZ_STREAM_PROP_FOOTER_SIZE:
*value = MZ_AES_AUTHCODE_SIZE;
return MZ_OK;
break;
default:
return MZ_EXIST_ERROR;
}
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 *)malloc(sizeof(mz_stream_aes));
aes = (mz_stream_aes *)MZ_ALLOC(sizeof(mz_stream_aes));
if (aes != NULL)
{
memset(aes, 0, sizeof(mz_stream_aes));
@@ -340,7 +369,7 @@ void mz_stream_aes_delete(void **stream)
return;
aes = (mz_stream_aes *)*stream;
if (aes != NULL)
free(aes);
MZ_FREE(aes);
*stream = NULL;
}
+5 -4
View File
@@ -1,16 +1,16 @@
/* mz_strm_aes.h -- Stream for WinZIP AES encryption
Version 2.2.4, November 15th, 2017
Version 2.3.9, July 26, 2018
part of the MiniZip project
Copyright (C) 2012-2017 Nathan Moinvaziri
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
#ifndef MZ_STREAM_AES_H
#define MZ_STREAM_AES_H
#include <stdint.h>
@@ -33,6 +33,7 @@ void mz_stream_aes_set_password(void *stream, const char *password);
void mz_stream_aes_set_encryption_mode(void *stream, int16_t encryption_mode);
int32_t mz_stream_aes_get_prop_int64(void *stream, int32_t prop, int64_t *value);
int32_t mz_stream_aes_set_prop_int64(void *stream, int32_t prop, int64_t value);
void* mz_stream_aes_create(void **stream);
void mz_stream_aes_delete(void **stream);
+46 -50
View File
@@ -1,10 +1,10 @@
/* mz_strm_buf.c -- Stream for buffering reads/writes
Version 2.2.4, November 15th, 2017
Version 2.3.9, July 26, 2018
part of the MiniZip project
This version of ioapi is designed to buffer IO.
Copyright (C) 2012-2017 Nathan Moinvaziri
Copyright (C) 2010-2018 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
This program is distributed under the terms of the same license as zlib.
@@ -24,14 +24,14 @@
/***************************************************************************/
#if 0
# define mz_stream_buffered_print(o,s,f,...) printf(o,s,f,__VA_ARGS__);
# define mz_stream_buffered_print(s,f,...) printf(f,__VA_ARGS__);
#else
# define mz_stream_buffered_print(o,s,f,...)
# define mz_stream_buffered_print(s,f,...)
#endif
/***************************************************************************/
mz_stream_vtbl mz_stream_buffered_vtbl = {
static mz_stream_vtbl mz_stream_buffered_vtbl = {
mz_stream_buffered_open,
mz_stream_buffered_is_open,
mz_stream_buffered_read,
@@ -41,7 +41,9 @@ mz_stream_vtbl mz_stream_buffered_vtbl = {
mz_stream_buffered_close,
mz_stream_buffered_error,
mz_stream_buffered_create,
mz_stream_buffered_delete
mz_stream_buffered_delete,
NULL,
NULL
};
/***************************************************************************/
@@ -66,17 +68,18 @@ typedef struct mz_stream_buffered_s {
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(opaque, buffered, "open [mode %d]\n", mode);
return mz_stream_open(stream, path, mode);
mz_stream_buffered *buffered = (mz_stream_buffered *)stream;
mz_stream_buffered_print(buffered, "open [mode %d]\n", mode);
return mz_stream_open(buffered->stream.base, path, mode);
}
int32_t mz_stream_buffered_is_open(void *stream)
{
return mz_stream_is_open(stream);
mz_stream_buffered *buffered = (mz_stream_buffered *)stream;
return mz_stream_is_open(buffered->stream.base);
}
int32_t mz_stream_buffered_flush(void *stream, int32_t *written)
static int32_t mz_stream_buffered_flush(void *stream, int32_t *written)
{
mz_stream_buffered *buffered = (mz_stream_buffered *)stream;
int32_t total_bytes_written = 0;
@@ -88,7 +91,7 @@ int32_t mz_stream_buffered_flush(void *stream, int32_t *written)
while (bytes_left_to_write > 0)
{
bytes_written = mz_stream_write(buffered->stream.base,
bytes_written = mz_stream_write(buffered->stream.base,
buffered->writebuf + (bytes_to_write - bytes_left_to_write), bytes_left_to_write);
if (bytes_written != bytes_left_to_write)
@@ -96,7 +99,7 @@ int32_t mz_stream_buffered_flush(void *stream, int32_t *written)
buffered->writebuf_misses += 1;
mz_stream_buffered_print(opaque, stream, "write flush [%d:%d len %d]\n",
mz_stream_buffered_print(stream, "write flush [%d:%d len %d]\n",
bytes_to_write, bytes_left_to_write, buffered->writebuf_len);
total_bytes_written += bytes_written;
@@ -120,10 +123,10 @@ int32_t mz_stream_buffered_read(void *stream, void *buf, int32_t size)
int32_t bytes_left_to_read = size;
int32_t bytes_read = 0;
mz_stream_buffered_print(opaque, stream, "read [size %ld pos %lld]\n", size, buffered->position);
mz_stream_buffered_print(stream, "read [size %ld pos %lld]\n", size, buffered->position);
if (buffered->writebuf_len > 0)
mz_stream_buffered_print(opaque, stream, "switch from write to read, not yet supported [%lld]\n",
mz_stream_buffered_print(stream, "switch from write to read, not yet supported [%lld]\n",
buffered->position);
while (bytes_left_to_read > 0)
@@ -137,15 +140,15 @@ int32_t mz_stream_buffered_read(void *stream, void *buf, int32_t size)
}
bytes_to_read = sizeof(buffered->readbuf) - (buffered->readbuf_len - buffered->readbuf_pos);
if (mz_stream_read(buffered->stream.base, buffered->readbuf + buffered->readbuf_pos, bytes_to_read) != bytes_to_read)
return 0;
bytes_read = mz_stream_read(buffered->stream.base, buffered->readbuf + buffered->readbuf_pos, bytes_to_read);
if (bytes_read < 0)
return bytes_read;
buffered->readbuf_misses += 1;
buffered->readbuf_len += bytes_read;
buffered->position += bytes_read;
mz_stream_buffered_print(opaque, stream, "filled [read %d/%d buf %d:%d pos %lld]\n",
mz_stream_buffered_print(stream, "filled [read %d/%d buf %d:%d pos %lld]\n",
bytes_read, bytes_to_read, buffered->readbuf_pos, buffered->readbuf_len, buffered->position);
if (bytes_read == 0)
@@ -166,7 +169,7 @@ int32_t mz_stream_buffered_read(void *stream, void *buf, int32_t size)
buffered->readbuf_hits += 1;
buffered->readbuf_pos += bytes_to_copy;
mz_stream_buffered_print(opaque, stream, "emptied [copied %d remaining %d buf %d:%d pos %lld]\n",
mz_stream_buffered_print(stream, "emptied [copied %d remaining %d buf %d:%d pos %lld]\n",
bytes_to_copy, bytes_left_to_read, buffered->readbuf_pos, buffered->readbuf_len, buffered->position);
}
}
@@ -184,7 +187,7 @@ int32_t mz_stream_buffered_write(void *stream, const void *buf, int32_t size)
int32_t bytes_flushed = 0;
mz_stream_buffered_print(opaque, stream, "write [size %ld len %d pos %lld]\n",
mz_stream_buffered_print(stream, "write [size %ld len %d pos %lld]\n",
size, buffered->writebuf_len, buffered->position);
if (buffered->readbuf_len > 0)
@@ -195,7 +198,7 @@ int32_t mz_stream_buffered_write(void *stream, const void *buf, int32_t size)
buffered->readbuf_len = 0;
buffered->readbuf_pos = 0;
mz_stream_buffered_print(opaque, stream, "switch from read to write [%lld]\n", buffered->position);
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;
@@ -220,9 +223,9 @@ int32_t mz_stream_buffered_write(void *stream, const void *buf, int32_t size)
continue;
}
memcpy(buffered->writebuf + buffered->writebuf_pos, (char *)buf + (bytes_to_write - bytes_left_to_write), bytes_to_copy);
memcpy(buffered->writebuf + buffered->writebuf_pos, (const char *)buf + (bytes_to_write - bytes_left_to_write), bytes_to_copy);
mz_stream_buffered_print(opaque, stream, "write copy [remaining %d write %d:%d len %d]\n",
mz_stream_buffered_print(stream, "write copy [remaining %d write %d:%d len %d]\n",
bytes_to_copy, bytes_to_write, bytes_left_to_write, buffered->writebuf_len);
bytes_left_to_write -= bytes_to_copy;
@@ -236,12 +239,14 @@ int32_t mz_stream_buffered_write(void *stream, const void *buf, int32_t size)
return size - bytes_left_to_write;
}
int64_t mz_stream_buffered_tellinternal(void *stream, uint64_t position)
int64_t mz_stream_buffered_tell(void *stream)
{
mz_stream_buffered *buffered = (mz_stream_buffered *)stream;
int64_t position = mz_stream_tell(buffered->stream.base);
buffered->position = position;
mz_stream_buffered_print(opaque, stream, "tell [pos %llu readpos %d writepos %d err %d]\n",
mz_stream_buffered_print(stream, "tell [pos %llu readpos %d writepos %d err %d]\n",
buffered->position, buffered->readbuf_pos, buffered->writebuf_pos, errno);
if (buffered->readbuf_len > 0)
@@ -251,19 +256,12 @@ int64_t mz_stream_buffered_tellinternal(void *stream, uint64_t position)
return position;
}
int64_t mz_stream_buffered_tell(void *stream)
{
mz_stream_buffered *buffered = (mz_stream_buffered *)stream;
int64_t position = mz_stream_tell(buffered->stream.base);
return mz_stream_buffered_tellinternal(stream, position);
}
int mz_stream_buffered_seekinternal(void *stream, int64_t offset, int32_t origin)
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;
mz_stream_buffered_print(opaque, stream, "seek [origin %d offset %llu pos %lld]\n", origin, offset, buffered->position);
mz_stream_buffered_print(stream, "seek [origin %d offset %llu pos %lld]\n", origin, offset, buffered->position);
switch (origin)
{
@@ -278,7 +276,7 @@ int mz_stream_buffered_seekinternal(void *stream, int64_t offset, int32_t origin
}
}
if ((buffered->readbuf_len > 0) && (offset < buffered->position) &&
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));
@@ -332,14 +330,7 @@ int mz_stream_buffered_seekinternal(void *stream, int64_t offset, int32_t origin
buffered->readbuf_pos = 0;
buffered->writebuf_len = 0;
buffered->writebuf_pos = 0;
return MZ_STREAM_ERROR;
}
int32_t mz_stream_buffered_seek(void *stream, int64_t offset, int32_t origin)
{
mz_stream_buffered *buffered = (mz_stream_buffered *)stream;
if (mz_stream_buffered_seekinternal(stream, offset, origin) != MZ_OK)
return MZ_STREAM_ERROR;
return mz_stream_seek(buffered->stream.base, offset, origin);
}
@@ -349,30 +340,35 @@ int32_t mz_stream_buffered_close(void *stream)
int32_t bytes_flushed = 0;
mz_stream_buffered_flush(stream, &bytes_flushed);
mz_stream_buffered_print(opaque, stream, "close\n");
mz_stream_buffered_print(stream, "close [flushed %d]\n", bytes_flushed);
if (buffered->readbuf_hits + buffered->readbuf_misses > 0)
mz_stream_buffered_print(opaque, stream, "read efficency %.02f%%\n",
mz_stream_buffered_print(stream, "read efficency %.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(opaque, stream, "write efficency %.02f%%\n",
mz_stream_buffered_print(stream, "write efficency %.02f%%\n",
(buffered->writebuf_hits / ((float)buffered->writebuf_hits + buffered->writebuf_misses)) * 100);
return mz_stream_close(&buffered->stream.base);
buffered->readbuf_len = 0;
buffered->readbuf_pos = 0;
buffered->writebuf_len = 0;
buffered->writebuf_pos = 0;
return mz_stream_close(buffered->stream.base);
}
int32_t mz_stream_buffered_error(void *stream)
{
mz_stream_buffered *buffered = (mz_stream_buffered *)stream;
return mz_stream_error(&buffered->stream.base);
return mz_stream_error(buffered->stream.base);
}
void *mz_stream_buffered_create(void **stream)
{
mz_stream_buffered *buffered = NULL;
buffered = (mz_stream_buffered *)malloc(sizeof(mz_stream_buffered));
buffered = (mz_stream_buffered *)MZ_ALLOC(sizeof(mz_stream_buffered));
if (buffered != NULL)
{
memset(buffered, 0, sizeof(mz_stream_buffered));
@@ -391,7 +387,7 @@ void mz_stream_buffered_delete(void **stream)
return;
buffered = (mz_stream_buffered *)*stream;
if (buffered != NULL)
free(buffered);
MZ_FREE(buffered);
*stream = NULL;
}
+4 -4
View File
@@ -1,18 +1,18 @@
/* mz_strm_buf.h -- Stream for buffering reads/writes
Version 2.2.4, November 15th, 2017
Version 2.3.9, July 26, 2018
part of the MiniZip project
This version of ioapi is designed to buffer IO.
Copyright (C) 2012-2017 Nathan Moinvaziri
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_BUFFERED_H
#define _MZ_STREAM_BUFFERED_H
#ifndef MZ_STREAM_BUFFERED_H
#define MZ_STREAM_BUFFERED_H
#include <stdint.h>
+65 -26
View File
@@ -1,8 +1,8 @@
/* mz_strm_bzip.c -- Stream for bzip inflate/deflate
Version 2.2.4, November 15th, 2017
Version 2.3.9, July 26, 2018
part of the MiniZip project
Copyright (C) 2012-2017 Nathan Moinvaziri
Copyright (C) 2010-2018 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
This program is distributed under the terms of the same license as bzip.
@@ -23,7 +23,7 @@
/***************************************************************************/
mz_stream_vtbl mz_stream_bzip_vtbl = {
static mz_stream_vtbl mz_stream_bzip_vtbl = {
mz_stream_bzip_open,
mz_stream_bzip_is_open,
mz_stream_bzip_read,
@@ -61,6 +61,7 @@ int32_t mz_stream_bzip_open(void *stream, const char *path, int32_t mode)
{
mz_stream_bzip *bzip = (mz_stream_bzip *)stream;
MZ_UNUSED(path);
bzip->bzstream.bzalloc = 0;
bzip->bzstream.bzfree = 0;
@@ -75,17 +76,25 @@ int32_t mz_stream_bzip_open(void *stream, const char *path, int32_t mode)
if (mode & MZ_OPEN_MODE_WRITE)
{
#ifdef MZ_ZIP_NO_COMPRESSION
return MZ_SUPPORT_ERROR;
#else
bzip->bzstream.next_out = (char *)bzip->buffer;
bzip->bzstream.avail_out = sizeof(bzip->buffer);
bzip->error = BZ2_bzCompressInit(&bzip->bzstream, bzip->level, 0, 0);
#endif
}
else if (mode & MZ_OPEN_MODE_READ)
{
#ifdef MZ_ZIP_NO_DECOMPRESSION
return MZ_SUPPORT_ERROR;
#else
bzip->bzstream.next_in = (char *)bzip->buffer;
bzip->bzstream.avail_in = 0;
bzip->error = BZ2_bzDecompressInit(&bzip->bzstream, 0, 0);
#endif
}
if (bzip->error != BZ_OK)
@@ -107,6 +116,9 @@ int32_t mz_stream_bzip_is_open(void *stream)
int32_t mz_stream_bzip_read(void *stream, void *buf, int32_t size)
{
#ifdef MZ_ZIP_NO_DECOMPRESSION
return MZ_SUPPORT_ERROR;
#else
mz_stream_bzip *bzip = (mz_stream_bzip *)stream;
uint64_t total_in_before = 0;
uint64_t total_out_before = 0;
@@ -125,7 +137,7 @@ int32_t mz_stream_bzip_read(void *stream, void *buf, int32_t size)
return 0;
bzip->bzstream.next_out = (char *)buf;
bzip->bzstream.avail_out = (uint16_t)size;
bzip->bzstream.avail_out = (unsigned int)size;
do
{
@@ -134,9 +146,9 @@ int32_t mz_stream_bzip_read(void *stream, void *buf, int32_t size)
bytes_to_read = sizeof(bzip->buffer);
if (bzip->max_total_in > 0)
{
if ((bzip->max_total_in - bzip->total_in) < sizeof(bzip->buffer))
if ((bzip->max_total_in - bzip->total_in) < (int64_t)sizeof(bzip->buffer))
bytes_to_read = (int32_t)(bzip->max_total_in - bzip->total_in);
}
}
read = mz_stream_read(bzip->stream.base, bzip->buffer, bytes_to_read);
@@ -153,13 +165,13 @@ int32_t mz_stream_bzip_read(void *stream, void *buf, int32_t size)
}
total_in_before = bzip->bzstream.avail_in;
total_out_before = bzip->bzstream.total_out_lo32 +
total_out_before = bzip->bzstream.total_out_lo32 +
(((uint64_t)bzip->bzstream.total_out_hi32) << 32);
err = BZ2_bzDecompress(&bzip->bzstream);
total_in_after = bzip->bzstream.avail_in;
total_out_after = bzip->bzstream.total_out_lo32 +
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);
@@ -188,9 +200,10 @@ int32_t mz_stream_bzip_read(void *stream, void *buf, int32_t size)
return bzip->error;
return total_out;
#endif
}
int32_t mz_stream_bzip_flush(void *stream)
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)
@@ -198,7 +211,7 @@ int32_t mz_stream_bzip_flush(void *stream)
return MZ_OK;
}
int32_t mz_stream_bzip_compress(void *stream, int flush)
static int32_t mz_stream_bzip_compress(void *stream, int flush)
{
mz_stream_bzip *bzip = (mz_stream_bzip *)stream;
uint64_t total_out_before = 0;
@@ -222,24 +235,26 @@ int32_t mz_stream_bzip_compress(void *stream, int flush)
bzip->buffer_len = 0;
}
total_out_before = bzip->bzstream.total_out_lo32 +
total_out_before = bzip->bzstream.total_out_lo32 +
(((uint64_t)bzip->bzstream.total_out_hi32) << 32);
err = BZ2_bzCompress(&bzip->bzstream, flush);
total_out_after = bzip->bzstream.total_out_lo32 +
total_out_after = bzip->bzstream.total_out_lo32 +
(((uint64_t)bzip->bzstream.total_out_hi32) << 32);
out_bytes = (uint32_t)(total_out_after - total_out_before);
bzip->buffer_len += out_bytes;
bzip->total_out += out_bytes;
if (err == BZ_STREAM_END)
break;
if (err < 0)
{
bzip->error = err;
return MZ_STREAM_ERROR;
}
bzip->buffer_len += out_bytes;
bzip->total_out += out_bytes;
}
while ((bzip->bzstream.avail_in > 0) || (flush == BZ_FINISH && err == BZ_FINISH_OK));
@@ -249,25 +264,35 @@ int32_t mz_stream_bzip_compress(void *stream, int flush)
int32_t mz_stream_bzip_write(void *stream, const void *buf, int32_t size)
{
mz_stream_bzip *bzip = (mz_stream_bzip *)stream;
int32_t err = size;
bzip->bzstream.next_in = (char *)buf;
bzip->bzstream.avail_in = size;
#ifdef MZ_ZIP_NO_COMPRESSION
MZ_UNUSED(bzip);
err = MZ_SUPPORT_ERROR;
#else
bzip->bzstream.next_in = (char *)(intptr_t)buf;
bzip->bzstream.avail_in = (unsigned int)size;
mz_stream_bzip_compress(stream, BZ_RUN);
bzip->total_in += size;
return size;
#endif
return err;
}
int64_t mz_stream_bzip_tell(void *stream)
{
MZ_UNUSED(stream);
return MZ_STREAM_ERROR;
}
int32_t mz_stream_bzip_seek(void *stream, int64_t offset, int32_t origin)
{
MZ_UNUSED(stream);
MZ_UNUSED(offset);
MZ_UNUSED(origin);
return MZ_STREAM_ERROR;
}
@@ -277,14 +302,22 @@ int32_t mz_stream_bzip_close(void *stream)
if (bzip->mode & MZ_OPEN_MODE_WRITE)
{
#ifdef MZ_ZIP_NO_COMPRESSION
return MZ_SUPPORT_ERROR;
#else
mz_stream_bzip_compress(stream, BZ_FINISH);
mz_stream_bzip_flush(stream);
BZ2_bzCompressEnd(&bzip->bzstream);
#endif
}
else if (bzip->mode & MZ_OPEN_MODE_READ)
{
#ifdef MZ_ZIP_NO_DECOMPRESSION
return MZ_SUPPORT_ERROR;
#else
BZ2_bzDecompressEnd(&bzip->bzstream);
#endif
}
bzip->initialized = 0;
@@ -307,15 +340,20 @@ int32_t mz_stream_bzip_get_prop_int64(void *stream, int32_t prop, int64_t *value
{
case MZ_STREAM_PROP_TOTAL_IN:
*value = bzip->total_in;
return MZ_OK;
break;
case MZ_STREAM_PROP_TOTAL_IN_MAX:
*value = bzip->max_total_in;
break;
case MZ_STREAM_PROP_TOTAL_OUT:
*value = bzip->total_out;
return MZ_OK;
break;
case MZ_STREAM_PROP_HEADER_SIZE:
*value = 0;
return MZ_OK;
break;
default:
return MZ_EXIST_ERROR;
}
return MZ_EXIST_ERROR;
return MZ_OK;
}
int32_t mz_stream_bzip_set_prop_int64(void *stream, int32_t prop, int64_t value)
@@ -340,7 +378,7 @@ void *mz_stream_bzip_create(void **stream)
{
mz_stream_bzip *bzip = NULL;
bzip = (mz_stream_bzip *)malloc(sizeof(mz_stream_bzip));
bzip = (mz_stream_bzip *)MZ_ALLOC(sizeof(mz_stream_bzip));
if (bzip != NULL)
{
memset(bzip, 0, sizeof(mz_stream_bzip));
@@ -360,7 +398,7 @@ void mz_stream_bzip_delete(void **stream)
return;
bzip = (mz_stream_bzip *)*stream;
if (bzip != NULL)
free(bzip);
MZ_FREE(bzip);
*stream = NULL;
}
@@ -371,4 +409,5 @@ void *mz_stream_bzip_get_interface(void)
extern void bz_internal_error(int errcode)
{
MZ_UNUSED(errcode);
}
+6 -4
View File
@@ -1,16 +1,16 @@
/* mz_strm_bzip.h -- Stream for bzip inflate/deflate
Version 2.2.4, November 15th, 2017
Version 2.3.9, July 26, 2018
part of the MiniZip project
Copyright (C) 2012-2017 Nathan Moinvaziri
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_BZIP_H
#define _MZ_STREAM_BZIP_H
#ifndef MZ_STREAM_BZIP_H
#define MZ_STREAM_BZIP_H
#include <stdint.h>
@@ -37,6 +37,8 @@ void mz_stream_bzip_delete(void **stream);
void* mz_stream_bzip_get_interface(void);
void bz_internal_error(int errcode);
/***************************************************************************/
#ifdef __cplusplus
+206
View File
@@ -0,0 +1,206 @@
/* mz_strm_crc32.c -- Stream for CRC32 hashing
Version 2.3.9, July 26, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
This program is distributed under the terms of the same license as zlib.
See the accompanying LICENSE file for the full text of the license.
*/
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "mz.h"
#include "mz_strm.h"
#include "mz_strm_crc32.h"
#ifdef HAVE_LZMA
#include "mz_strm_lzma.h"
#endif
#ifdef HAVE_ZLIB
#include "mz_strm_zlib.h"
#endif
/***************************************************************************/
static mz_stream_vtbl mz_stream_crc32_vtbl = {
mz_stream_crc32_open,
mz_stream_crc32_is_open,
mz_stream_crc32_read,
mz_stream_crc32_write,
mz_stream_crc32_tell,
mz_stream_crc32_seek,
mz_stream_crc32_close,
mz_stream_crc32_error,
mz_stream_crc32_create,
mz_stream_crc32_delete,
mz_stream_crc32_get_prop_int64,
NULL
};
/***************************************************************************/
typedef struct mz_stream_crc32_s {
mz_stream stream;
int8_t initialized;
int64_t value;
int64_t total_in;
int64_t total_out;
mz_stream_crc32_update
update;
} mz_stream_crc32;
/***************************************************************************/
int32_t mz_stream_crc32_open(void *stream, const char *path, int32_t mode)
{
mz_stream_crc32 *crc32 = (mz_stream_crc32 *)stream;
MZ_UNUSED(path);
MZ_UNUSED(mode);
crc32->initialized = 1;
crc32->value = 0;
return MZ_OK;
}
int32_t mz_stream_crc32_is_open(void *stream)
{
mz_stream_crc32 *crc32 = (mz_stream_crc32 *)stream;
if (crc32->initialized != 1)
return MZ_STREAM_ERROR;
return MZ_OK;
}
int32_t mz_stream_crc32_read(void *stream, void *buf, int32_t size)
{
mz_stream_crc32 *crc32x = (mz_stream_crc32 *)stream;
int32_t read = 0;
read = mz_stream_read(crc32x->stream.base, buf, size);
if (read > 0)
{
crc32x->value = crc32x->update(crc32x->value, buf, read);
crc32x->total_in += read;
}
return read;
}
int32_t mz_stream_crc32_write(void *stream, const void *buf, int32_t size)
{
mz_stream_crc32 *crc32x = (mz_stream_crc32 *)stream;
int32_t written = 0;
crc32x->value = crc32x->update(crc32x->value, buf, size);
written = mz_stream_write(crc32x->stream.base, buf, size);
crc32x->total_out += written;
return written;
}
int64_t mz_stream_crc32_tell(void *stream)
{
mz_stream_crc32 *crc32 = (mz_stream_crc32 *)stream;
return mz_stream_tell(crc32->stream.base);
}
int32_t mz_stream_crc32_seek(void *stream, int64_t offset, int32_t origin)
{
mz_stream_crc32 *crc32 = (mz_stream_crc32 *)stream;
crc32->value = 0;
return mz_stream_seek(crc32->stream.base, offset, origin);
}
int32_t mz_stream_crc32_close(void *stream)
{
mz_stream_crc32 *crc32 = (mz_stream_crc32 *)stream;
crc32->initialized = 0;
return MZ_OK;
}
int32_t mz_stream_crc32_error(void *stream)
{
mz_stream_crc32 *crc32 = (mz_stream_crc32 *)stream;
return mz_stream_error(crc32->stream.base);
}
int32_t mz_stream_crc32_get_value(void *stream)
{
mz_stream_crc32 *crc32 = (mz_stream_crc32 *)stream;
return (int32_t)crc32->value;
}
int32_t mz_stream_crc32_get_prop_int64(void *stream, int32_t prop, int64_t *value)
{
mz_stream_crc32 *crc32 = (mz_stream_crc32 *)stream;
switch (prop)
{
case MZ_STREAM_PROP_TOTAL_IN:
*value = crc32->total_in;
break;
case MZ_STREAM_PROP_TOTAL_OUT:
*value = crc32->total_out;
break;
default:
return MZ_EXIST_ERROR;
}
return MZ_OK;
}
void *mz_stream_crc32_create(void **stream)
{
mz_stream_crc32 *crc32 = NULL;
crc32 = (mz_stream_crc32 *)MZ_ALLOC(sizeof(mz_stream_crc32));
if (crc32 != NULL)
{
memset(crc32, 0, sizeof(mz_stream_crc32));
crc32->stream.vtbl = &mz_stream_crc32_vtbl;
}
#ifdef HAVE_ZLIB
crc32->update =
(mz_stream_crc32_update)mz_stream_zlib_get_crc32_update();
#elif defined(HAVE_LZMA)
crc32->update =
(mz_stream_crc32_update)mz_stream_lzma_get_crc32_update();
#else
#error ZLIB or LZMA required for CRC32
#endif
if (stream != NULL)
*stream = crc32;
return crc32;
}
void mz_stream_crc32_delete(void **stream)
{
mz_stream_crc32 *crc32 = NULL;
if (stream == NULL)
return;
crc32 = (mz_stream_crc32 *)*stream;
if (crc32 != NULL)
MZ_FREE(crc32);
*stream = NULL;
}
void *mz_stream_crc32_get_interface(void)
{
return (void *)&mz_stream_crc32_vtbl;
}
int32_t mz_stream_crc32_get_update_func(mz_stream_crc32_update *update)
{
if (update == NULL)
return MZ_PARAM_ERROR;
#ifdef HAVE_ZLIB
*update =
(mz_stream_crc32_update)mz_stream_zlib_get_crc32_update();
#elif defined(HAVE_LZMA)
*update =
(mz_stream_crc32_update)mz_stream_lzma_get_crc32_update();
#else
#error ZLIB or LZMA required for CRC32
#endif
return MZ_OK;
}
+50
View File
@@ -0,0 +1,50 @@
/* mz_strm_crc32.h -- Stream for CRC32 hashing
Version 2.3.9, July 26, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
This program is distributed under the terms of the same license as zlib.
See the accompanying LICENSE file for the full text of the license.
*/
#ifndef MZ_STREAM_CRC32_H
#define MZ_STREAM_CRC32_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/***************************************************************************/
typedef int64_t (*mz_stream_crc32_update)(int64_t value, const void *buf, int32_t size);
int32_t mz_stream_crc32_open(void *stream, const char *filename, int32_t mode);
int32_t mz_stream_crc32_is_open(void *stream);
int32_t mz_stream_crc32_read(void *stream, void *buf, int32_t size);
int32_t mz_stream_crc32_write(void *stream, const void *buf, int32_t size);
int64_t mz_stream_crc32_tell(void *stream);
int32_t mz_stream_crc32_seek(void *stream, int64_t offset, int32_t origin);
int32_t mz_stream_crc32_close(void *stream);
int32_t mz_stream_crc32_error(void *stream);
int32_t mz_stream_crc32_get_value(void *stream);
int32_t mz_stream_crc32_get_prop_int64(void *stream, int32_t prop, int64_t *value);
void* mz_stream_crc32_create(void **stream);
void mz_stream_crc32_delete(void **stream);
void* mz_stream_crc32_get_interface(void);
int32_t mz_stream_crc32_get_update_func(mz_stream_crc32_update *update);
/***************************************************************************/
#ifdef __cplusplus
}
#endif
#endif
+84 -30
View File
@@ -1,8 +1,8 @@
/* mz_strm_lzma.c -- Stream for lzma inflate/deflate
Version 2.2.4, November 15th, 2017
Version 2.3.9, July 26, 2018
part of the MiniZip project
Copyright (C) 2012-2017 Nathan Moinvaziri
Copyright (C) 2010-2018 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
This program is distributed under the terms of the same license as lzma.
@@ -28,7 +28,7 @@
/***************************************************************************/
mz_stream_vtbl mz_stream_lzma_vtbl = {
static mz_stream_vtbl mz_stream_lzma_vtbl = {
mz_stream_lzma_open,
mz_stream_lzma_is_open,
mz_stream_lzma_read,
@@ -55,6 +55,7 @@ typedef struct mz_stream_lzma_s {
int64_t total_in;
int64_t total_out;
int64_t max_total_in;
int64_t max_total_out;
int8_t initialized;
uint32_t preset;
} mz_stream_lzma;
@@ -65,11 +66,15 @@ int32_t mz_stream_lzma_open(void *stream, const char *path, int32_t mode)
{
mz_stream_lzma *lzma = (mz_stream_lzma *)stream;
lzma_filter filters[LZMA_FILTERS_MAX + 1];
lzma_options_lzma opt_lzma = { 0 };
lzma_options_lzma opt_lzma;
uint32_t size = 0;
uint8_t major = 0;
uint8_t minor = 0;
MZ_UNUSED(path);
memset(&opt_lzma, 0, sizeof(opt_lzma));
lzma->lstream.total_in = 0;
lzma->lstream.total_out = 0;
@@ -78,6 +83,10 @@ int32_t mz_stream_lzma_open(void *stream, const char *path, int32_t mode)
if (mode & MZ_OPEN_MODE_WRITE)
{
#ifdef MZ_ZIP_NO_COMPRESSION
MZ_UNUSED(filters);
return MZ_SUPPORT_ERROR;
#else
lzma->lstream.next_out = lzma->buffer;
lzma->lstream.avail_out = sizeof(lzma->buffer);
@@ -91,7 +100,7 @@ int32_t mz_stream_lzma_open(void *stream, const char *path, int32_t mode)
filters[1].id = LZMA_VLI_UNKNOWN;
lzma_properties_size(&size, (lzma_filter *)&filters);
mz_stream_write_uint8(lzma->stream.base, LZMA_VERSION_MAJOR);
mz_stream_write_uint8(lzma->stream.base, LZMA_VERSION_MINOR);
mz_stream_write_uint16(lzma->stream.base, (uint16_t)size);
@@ -99,9 +108,13 @@ int32_t mz_stream_lzma_open(void *stream, const char *path, int32_t mode)
lzma->total_out += MZ_LZMA_HEADER_SIZE;
lzma->error = lzma_alone_encoder(&lzma->lstream, &opt_lzma);
#endif
}
else if (mode & MZ_OPEN_MODE_READ)
{
#ifdef MZ_ZIP_NO_DECOMPRESSION
return MZ_SUPPORT_ERROR;
#else
lzma->lstream.next_in = lzma->buffer;
lzma->lstream.avail_in = 0;
@@ -112,8 +125,9 @@ int32_t mz_stream_lzma_open(void *stream, const char *path, int32_t mode)
lzma->total_in += MZ_LZMA_HEADER_SIZE;
lzma->error = lzma_alone_decoder(&lzma->lstream, UINT64_MAX);
#endif
}
if (lzma->error != LZMA_OK)
return MZ_STREAM_ERROR;
@@ -132,6 +146,9 @@ int32_t mz_stream_lzma_is_open(void *stream)
int32_t mz_stream_lzma_read(void *stream, void *buf, int32_t size)
{
#ifdef MZ_ZIP_NO_DECOMPRESSION
return MZ_SUPPORT_ERROR;
#else
mz_stream_lzma *lzma = (mz_stream_lzma *)stream;
uint64_t total_in_before = 0;
uint64_t total_out_before = 0;
@@ -147,7 +164,7 @@ int32_t mz_stream_lzma_read(void *stream, void *buf, int32_t size)
lzma->lstream.next_out = (uint8_t*)buf;
lzma->lstream.avail_out = (uint16_t)size;
lzma->lstream.avail_out = (size_t)size;
do
{
@@ -156,10 +173,10 @@ int32_t mz_stream_lzma_read(void *stream, void *buf, int32_t size)
bytes_to_read = sizeof(lzma->buffer);
if (lzma->max_total_in > 0)
{
if ((lzma->max_total_in - lzma->total_in) < sizeof(lzma->buffer))
if ((lzma->max_total_in - lzma->total_in) < (int64_t)sizeof(lzma->buffer))
bytes_to_read = (int32_t)(lzma->max_total_in - lzma->total_in);
}
read = mz_stream_read(lzma->stream.base, lzma->buffer, bytes_to_read);
if (read < 0)
@@ -181,7 +198,9 @@ 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;
in_bytes = (uint32_t)(total_in_before - total_in_after);
out_bytes = (uint32_t)(total_out_after - total_out_before);
@@ -205,9 +224,10 @@ int32_t mz_stream_lzma_read(void *stream, void *buf, int32_t size)
return lzma->error;
return total_out;
#endif
}
int32_t mz_stream_lzma_flush(void *stream)
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)
@@ -215,7 +235,7 @@ int32_t mz_stream_lzma_flush(void *stream)
return MZ_OK;
}
int32_t mz_stream_lzma_code(void *stream, int32_t flush)
static int32_t mz_stream_lzma_code(void *stream, int32_t flush)
{
mz_stream_lzma *lzma = (mz_stream_lzma *)stream;
uint64_t total_out_before = 0;
@@ -255,7 +275,7 @@ int32_t mz_stream_lzma_code(void *stream, int32_t flush)
lzma->buffer_len += out_bytes;
lzma->total_out += out_bytes;
}
while (lzma->lstream.avail_in > 0);
while ((lzma->lstream.avail_in > 0) || (flush == LZMA_FINISH && err == LZMA_OK));
return MZ_OK;
}
@@ -263,25 +283,35 @@ int32_t mz_stream_lzma_code(void *stream, int32_t flush)
int32_t mz_stream_lzma_write(void *stream, const void *buf, int32_t size)
{
mz_stream_lzma *lzma = (mz_stream_lzma *)stream;
int32_t err = size;
lzma->lstream.next_in = (uint8_t*)buf;
lzma->lstream.avail_in = size;
#ifdef MZ_ZIP_NO_COMPRESSION
MZ_UNUSED(lzma);
err = MZ_SUPPORT_ERROR;
#else
lzma->lstream.next_in = (uint8_t*)(intptr_t)buf;
lzma->lstream.avail_in = (size_t)size;
mz_stream_lzma_code(stream, LZMA_RUN);
lzma->total_in += size;
return size;
#endif
return err;
}
int64_t mz_stream_lzma_tell(void *stream)
{
MZ_UNUSED(stream);
return MZ_STREAM_ERROR;
}
int32_t mz_stream_lzma_seek(void *stream, int64_t offset, int32_t origin)
{
MZ_UNUSED(stream);
MZ_UNUSED(offset);
MZ_UNUSED(origin);
return MZ_STREAM_ERROR;
}
@@ -291,14 +321,22 @@ int32_t mz_stream_lzma_close(void *stream)
if (lzma->mode & MZ_OPEN_MODE_WRITE)
{
#ifdef MZ_ZIP_NO_COMPRESSION
return MZ_SUPPORT_ERROR;
#else
mz_stream_lzma_code(stream, LZMA_FINISH);
mz_stream_lzma_flush(stream);
lzma_end(&lzma->lstream);
#endif
}
else if (lzma->mode & MZ_OPEN_MODE_READ)
{
#ifdef MZ_ZIP_NO_DECOMPRESSION
return MZ_SUPPORT_ERROR;
#else
lzma_end(&lzma->lstream);
#endif
}
lzma->initialized = 0;
@@ -321,15 +359,23 @@ int32_t mz_stream_lzma_get_prop_int64(void *stream, int32_t prop, int64_t *value
{
case MZ_STREAM_PROP_TOTAL_IN:
*value = lzma->total_in;
return MZ_OK;
break;
case MZ_STREAM_PROP_TOTAL_IN_MAX:
*value = lzma->max_total_in;
break;
case MZ_STREAM_PROP_TOTAL_OUT:
*value = lzma->total_out;
return MZ_OK;
break;
case MZ_STREAM_PROP_TOTAL_OUT_MAX:
*value = lzma->max_total_out;
break;
case MZ_STREAM_PROP_HEADER_SIZE:
*value = MZ_LZMA_HEADER_SIZE;
return MZ_OK;
break;
default:
return MZ_EXIST_ERROR;
}
return MZ_EXIST_ERROR;
return MZ_OK;
}
int32_t mz_stream_lzma_set_prop_int64(void *stream, int32_t prop, int64_t value)
@@ -342,24 +388,32 @@ int32_t mz_stream_lzma_set_prop_int64(void *stream, int32_t prop, int64_t value)
lzma->preset = LZMA_PRESET_EXTREME;
else
lzma->preset = LZMA_PRESET_DEFAULT;
return MZ_OK;
break;
case MZ_STREAM_PROP_TOTAL_IN_MAX:
lzma->max_total_in = value;
return MZ_OK;
break;
case MZ_STREAM_PROP_TOTAL_OUT_MAX:
if (value < -1)
return MZ_PARAM_ERROR;
lzma->max_total_out = value;
break;
default:
return MZ_EXIST_ERROR;
}
return MZ_EXIST_ERROR;
return MZ_OK;
}
void *mz_stream_lzma_create(void **stream)
{
mz_stream_lzma *lzma = NULL;
lzma = (mz_stream_lzma *)malloc(sizeof(mz_stream_lzma));
lzma = (mz_stream_lzma *)MZ_ALLOC(sizeof(mz_stream_lzma));
if (lzma != NULL)
{
memset(lzma, 0, sizeof(mz_stream_lzma));
lzma->stream.vtbl = &mz_stream_lzma_vtbl;
lzma->preset = LZMA_PRESET_DEFAULT;
lzma->max_total_out = -1;
}
if (stream != NULL)
*stream = lzma;
@@ -374,7 +428,7 @@ void mz_stream_lzma_delete(void **stream)
return;
lzma = (mz_stream_lzma *)*stream;
if (lzma != NULL)
free(lzma);
MZ_FREE(lzma);
*stream = NULL;
}
@@ -383,12 +437,12 @@ void *mz_stream_lzma_get_interface(void)
return (void *)&mz_stream_lzma_vtbl;
}
int32_t mz_stream_lzma_crc32(int32_t value, const void *buf, int32_t size)
static int64_t mz_stream_lzma_crc32(int64_t value, const void *buf, int32_t size)
{
return lzma_crc32(buf, size, value);
return (int32_t)lzma_crc32(buf, size, (int32_t)value);
}
void *mz_stream_lzma_get_crc32_update(void)
{
return (void *)mz_stream_lzma_crc32;
}
}
+4 -4
View File
@@ -1,16 +1,16 @@
/* mz_strm_lzma.h -- Stream for lzma inflate/deflate
Version 2.2.4, November 15th, 2017
Version 2.3.9, July 26, 2018
part of the MiniZip project
Copyright (C) 2012-2017 Nathan Moinvaziri
Copyright (C) 2010-2018 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
This program is distributed under the terms of the same license as lzma.
See the accompanying LICENSE file for the full text of the license.
*/
#ifndef _MZ_STREAM_LZMA_H
#define _MZ_STREAM_LZMA_H
#ifndef MZ_STREAM_LZMA_H
#define MZ_STREAM_LZMA_H
#include <stdint.h>
+35 -15
View File
@@ -1,13 +1,13 @@
/* mz_strm_mem.c -- Stream for memory access
Version 2.2.4, November 15th, 2017
Version 2.3.9, July 26, 2018
part of the MiniZip project
This interface is designed to access memory rather than files.
We do use a region of memory to put data in to and take it out of.
We do use a region of memory to put data in to and take it out of.
Based on Unzip ioapi.c version 0.22, May 19th, 2003
Copyright (C) 2012-2017 Nathan Moinvaziri
Copyright (C) 2010-2018 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
Copyright (C) 2003 Justin Fletcher
Copyright (C) 1998-2003 Gilles Vollant
@@ -28,7 +28,7 @@
/***************************************************************************/
mz_stream_vtbl mz_stream_mem_vtbl = {
static mz_stream_vtbl mz_stream_mem_vtbl = {
mz_stream_mem_open,
mz_stream_mem_is_open,
mz_stream_mem_read,
@@ -38,7 +38,9 @@ mz_stream_vtbl mz_stream_mem_vtbl = {
mz_stream_mem_close,
mz_stream_mem_error,
mz_stream_mem_create,
mz_stream_mem_delete
mz_stream_mem_delete,
NULL,
NULL
};
/***************************************************************************/
@@ -46,7 +48,7 @@ mz_stream_vtbl mz_stream_mem_vtbl = {
typedef struct mz_stream_mem_s {
mz_stream stream;
int32_t mode;
char *buffer; // Memory buffer pointer
char *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
@@ -62,11 +64,11 @@ static void mz_stream_mem_set_size(void *stream, int32_t size)
char *new_buf = NULL;
new_buf = (char *)malloc(new_size);
new_buf = (char *)MZ_ALLOC(new_size);
if (mem->buffer)
{
memcpy(new_buf, mem->buffer, mem->size);
free(mem->buffer);
MZ_FREE(mem->buffer);
}
mem->buffer = new_buf;
@@ -77,6 +79,7 @@ int32_t mz_stream_mem_open(void *stream, const char *path, int32_t mode)
{
mz_stream_mem *mem = (mz_stream_mem *)stream;
MZ_UNUSED(path);
mem->mode = mode;
mem->limit = 0;
@@ -189,12 +192,16 @@ int32_t mz_stream_mem_seek(void *stream, int64_t offset, int32_t origin)
int32_t mz_stream_mem_close(void *stream)
{
MZ_UNUSED(stream);
// We never return errors
return MZ_OK;
}
int32_t mz_stream_mem_error(void *stream)
{
MZ_UNUSED(stream);
// We never return errors
return MZ_OK;
}
@@ -207,12 +214,12 @@ void mz_stream_mem_set_buffer(void *stream, void *buf, int32_t size)
mem->limit = size;
}
int32_t mz_stream_mem_get_buffer(void *stream, void **buf)
int32_t mz_stream_mem_get_buffer(void *stream, const void **buf)
{
return mz_stream_mem_get_buffer_at(stream, 0, buf);
}
int32_t mz_stream_mem_get_buffer_at(void *stream, int64_t position, void **buf)
int32_t mz_stream_mem_get_buffer_at(void *stream, int64_t position, const void **buf)
{
mz_stream_mem *mem = (mz_stream_mem *)stream;
if (buf == NULL || position < 0 || mem->size < position || mem->buffer == NULL)
@@ -221,6 +228,18 @@ int32_t mz_stream_mem_get_buffer_at(void *stream, int64_t position, void **buf)
return MZ_OK;
}
void mz_stream_mem_get_buffer_length(void *stream, int32_t *length)
{
mz_stream_mem *mem = (mz_stream_mem *)stream;
*length = mem->limit;
}
void mz_stream_mem_set_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;
@@ -231,12 +250,13 @@ void *mz_stream_mem_create(void **stream)
{
mz_stream_mem *mem = NULL;
mem = (mz_stream_mem *)malloc(sizeof(mz_stream_mem));
mem = (mz_stream_mem *)MZ_ALLOC(sizeof(mz_stream_mem));
if (mem != NULL)
{
memset(mem, 0, sizeof(mz_stream_mem));
mem->stream.vtbl = &mz_stream_mem_vtbl;
mem->grow_size = 4096;
// Large grow size to prevent fragmentation
mem->grow_size = 128 * 1024;
}
if (stream != NULL)
*stream = mem;
@@ -253,8 +273,8 @@ void mz_stream_mem_delete(void **stream)
if (mem != NULL)
{
if ((mem->mode & MZ_OPEN_MODE_CREATE) && (mem->buffer != NULL))
free(mem->buffer);
free(mem);
MZ_FREE(mem->buffer);
MZ_FREE(mem);
}
*stream = NULL;
}
@@ -262,4 +282,4 @@ void mz_stream_mem_delete(void **stream)
void *mz_stream_mem_get_interface(void)
{
return (void *)&mz_stream_mem_vtbl;
}
}
+8 -8
View File
@@ -1,18 +1,16 @@
/* mz_strm_mem.h -- Stream for memory access
Version 2.2.4, November 15th, 2017
Version 2.3.9, July 26, 2018
part of MiniZip project
Copyright (C) 2012-2017 Nathan Moinvaziri
Copyright (C) 2010-2018 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
Copyright (C) 2003 Justin Fletcher
Copyright (C) 1998-2003 Gilles Vollant
This program is distributed under the terms of the same license as zlib.
See the accompanying LICENSE file for the full text of the license.
*/
#ifndef _MZ_STREAM_MEM_H
#define _MZ_STREAM_MEM_H
#ifndef MZ_STREAM_MEM_H
#define MZ_STREAM_MEM_H
#include <stdint.h>
@@ -32,8 +30,10 @@ int32_t mz_stream_mem_close(void *stream);
int32_t mz_stream_mem_error(void *stream);
void mz_stream_mem_set_buffer(void *stream, void *buf, int32_t size);
int32_t mz_stream_mem_get_buffer(void *stream, void **buf);
int32_t mz_stream_mem_get_buffer_at(void *stream, int64_t position, void **buf);
int32_t mz_stream_mem_get_buffer(void *stream, const void **buf);
int32_t mz_stream_mem_get_buffer_at(void *stream, int64_t position, const void **buf);
void mz_stream_mem_get_buffer_length(void *stream, int32_t *length);
void mz_stream_mem_set_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);
+351
View File
@@ -0,0 +1,351 @@
/* mz_strm_pkcrypt.c -- Code for traditional PKWARE encryption
Version 2.3.9, July 26, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
Copyright (C) 1998-2005 Gilles Vollant
Modifications for Info-ZIP crypting
http://www.winimage.com/zLibDll/minizip.html
Copyright (C) 2003 Terry Thorsen
This code is a modified version of crypting code in Info-ZIP distribution
Copyright (C) 1990-2000 Info-ZIP. All rights reserved.
This program is distributed under the terms of the same license as zlib.
See the accompanying LICENSE file for the full text of the license.
This encryption code is a direct transcription of the algorithm from
Roger Schlafly, described by Phil Katz in the file appnote.txt. This
file (appnote.txt) is distributed with the PKZIP program (even in the
version without encryption capabilities).
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mz.h"
#include "mz_os.h"
#include "mz_strm.h"
#include "mz_strm_crc32.h"
#include "mz_strm_pkcrypt.h"
/***************************************************************************/
#define RAND_HEAD_LEN (12)
/***************************************************************************/
static mz_stream_vtbl mz_stream_pkcrypt_vtbl = {
mz_stream_pkcrypt_open,
mz_stream_pkcrypt_is_open,
mz_stream_pkcrypt_read,
mz_stream_pkcrypt_write,
mz_stream_pkcrypt_tell,
mz_stream_pkcrypt_seek,
mz_stream_pkcrypt_close,
mz_stream_pkcrypt_error,
mz_stream_pkcrypt_create,
mz_stream_pkcrypt_delete,
mz_stream_pkcrypt_get_prop_int64,
NULL
};
/***************************************************************************/
typedef struct mz_stream_pkcrypt_s {
mz_stream stream;
int32_t error;
int16_t initialized;
uint8_t buffer[INT16_MAX];
int64_t total_in;
int64_t total_out;
uint32_t keys[3]; // keys defining the pseudo-random sequence
uint8_t verify1;
uint8_t verify2;
const char *password;
mz_stream_crc32_update
crc32_update;
} mz_stream_pkcrypt;
/***************************************************************************/
#define mz_stream_pkcrypt_decode(strm, c) \
(mz_stream_pkcrypt_update_keys(strm, \
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))
/***************************************************************************/
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.
temp = pkcrypt->keys[2] | 2;
return (uint8_t)(((temp * (temp ^ 1)) >> 8) & 0xff);
}
static uint8_t mz_stream_pkcrypt_update_keys(void *stream, uint8_t c)
{
mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
uint8_t buf = c;
pkcrypt->keys[0] = (uint32_t)~pkcrypt->crc32_update(~pkcrypt->keys[0], &buf, 1);
pkcrypt->keys[1] += pkcrypt->keys[0] & 0xff;
pkcrypt->keys[1] *= 134775813L;
pkcrypt->keys[1] += 1;
buf = (uint8_t)(pkcrypt->keys[1] >> 24);
pkcrypt->keys[2] = (uint32_t)~pkcrypt->crc32_update(~pkcrypt->keys[2], &buf, 1);
return (uint8_t)c;
}
static void mz_stream_pkcrypt_init_keys(void *stream, const char *password)
{
mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
pkcrypt->keys[0] = 305419896L;
pkcrypt->keys[1] = 591751049L;
pkcrypt->keys[2] = 878082192L;
while (*password != 0)
{
mz_stream_pkcrypt_update_keys(stream, *password);
password += 1;
}
}
/***************************************************************************/
int32_t mz_stream_pkcrypt_open(void *stream, const char *path, int32_t mode)
{
mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
uint16_t t = 0;
int16_t i = 0;
uint8_t verify1 = 0;
uint8_t verify2 = 0;
uint8_t header[RAND_HEAD_LEN];
const char *password = path;
pkcrypt->total_in = 0;
pkcrypt->total_out = 0;
pkcrypt->initialized = 0;
if (mz_stream_is_open(pkcrypt->stream.base) != MZ_OK)
return MZ_STREAM_ERROR;
if (password == NULL)
password = pkcrypt->password;
if (password == NULL)
return MZ_STREAM_ERROR;
if (mz_stream_crc32_get_update_func(&pkcrypt->crc32_update) != MZ_OK)
return MZ_PARAM_ERROR;
mz_stream_pkcrypt_init_keys(stream, password);
if (mode & MZ_OPEN_MODE_WRITE)
{
#ifdef MZ_ZIP_NO_COMPRESSION
return MZ_SUPPORT_ERROR;
#else
// First generate RAND_HEAD_LEN - 2 random bytes.
mz_os_rand(header, RAND_HEAD_LEN - 2);
// Encrypt random header (last two bytes is high word of crc)
for (i = 0; i < RAND_HEAD_LEN - 2; i++)
header[i] = mz_stream_pkcrypt_encode(stream, header[i], t);
header[i++] = mz_stream_pkcrypt_encode(stream, pkcrypt->verify1, t);
header[i++] = mz_stream_pkcrypt_encode(stream, pkcrypt->verify2, t);
if (mz_stream_write(pkcrypt->stream.base, header, RAND_HEAD_LEN) != RAND_HEAD_LEN)
return MZ_STREAM_ERROR;
pkcrypt->total_out += RAND_HEAD_LEN;
#endif
}
else if (mode & MZ_OPEN_MODE_READ)
{
#ifdef MZ_ZIP_NO_DECOMPRESSION
return MZ_SUPPORT_ERROR;
#else
if (mz_stream_read(pkcrypt->stream.base, header, RAND_HEAD_LEN) != RAND_HEAD_LEN)
return MZ_STREAM_ERROR;
for (i = 0; i < RAND_HEAD_LEN - 2; i++)
header[i] = mz_stream_pkcrypt_decode(stream, header[i]);
verify1 = mz_stream_pkcrypt_decode(stream, header[i++]);
verify2 = mz_stream_pkcrypt_decode(stream, header[i++]);
// Older versions used 2 byte check, newer versions use 1 byte check.
MZ_UNUSED(verify1);
if ((verify2 != 0) && (verify2 != pkcrypt->verify2))
return MZ_PASSWORD_ERROR;
pkcrypt->total_in += RAND_HEAD_LEN;
#endif
}
pkcrypt->initialized = 1;
return MZ_OK;
}
int32_t mz_stream_pkcrypt_is_open(void *stream)
{
mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
if (pkcrypt->initialized == 0)
return MZ_STREAM_ERROR;
return MZ_OK;
}
int32_t mz_stream_pkcrypt_read(void *stream, void *buf, int32_t size)
{
mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
uint8_t *buf_ptr = (uint8_t *)buf;
int32_t read = 0;
int32_t i = 0;
read = mz_stream_read(pkcrypt->stream.base, buf, size);
for (i = 0; i < read; i++)
buf_ptr[i] = mz_stream_pkcrypt_decode(stream, buf_ptr[i]);
pkcrypt->total_in += read;
return read;
}
int32_t mz_stream_pkcrypt_write(void *stream, const void *buf, int32_t size)
{
mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
const uint8_t *buf_ptr = (const uint8_t *)buf;
int32_t written = 0;
int32_t i = 0;
uint16_t t = 0;
if (size > (int32_t)sizeof(pkcrypt->buffer))
return MZ_STREAM_ERROR;
for (i = 0; i < size; i++)
pkcrypt->buffer[i] = mz_stream_pkcrypt_encode(stream, buf_ptr[i], t);
written = mz_stream_write(pkcrypt->stream.base, pkcrypt->buffer, size);
if (written > 0)
pkcrypt->total_out += written;
return written;
}
int64_t mz_stream_pkcrypt_tell(void *stream)
{
mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
return mz_stream_tell(pkcrypt->stream.base);
}
int32_t mz_stream_pkcrypt_seek(void *stream, int64_t offset, int32_t origin)
{
mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
return mz_stream_seek(pkcrypt->stream.base, offset, origin);
}
int32_t mz_stream_pkcrypt_close(void *stream)
{
mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
pkcrypt->initialized = 0;
return MZ_OK;
}
int32_t mz_stream_pkcrypt_error(void *stream)
{
mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
return pkcrypt->error;
}
void mz_stream_pkcrypt_set_password(void *stream, const char *password)
{
mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
pkcrypt->password = password;
}
void mz_stream_pkcrypt_set_verify(void *stream, uint8_t verify1, uint8_t verify2)
{
mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
pkcrypt->verify1 = verify1;
pkcrypt->verify2 = verify2;
}
void mz_stream_pkcrypt_get_verify(void *stream, uint8_t *verify1, uint8_t *verify2)
{
mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
*verify1 = pkcrypt->verify1;
*verify2 = pkcrypt->verify2;
}
int32_t mz_stream_pkcrypt_get_prop_int64(void *stream, int32_t prop, int64_t *value)
{
mz_stream_pkcrypt *pkcrypt = (mz_stream_pkcrypt *)stream;
switch (prop)
{
case MZ_STREAM_PROP_TOTAL_IN:
*value = pkcrypt->total_in;
break;
case MZ_STREAM_PROP_TOTAL_OUT:
*value = pkcrypt->total_out;
break;
case MZ_STREAM_PROP_HEADER_SIZE:
*value = RAND_HEAD_LEN;
break;
case MZ_STREAM_PROP_FOOTER_SIZE:
*value = 0;
break;
default:
return MZ_EXIST_ERROR;
}
return MZ_OK;
}
void *mz_stream_pkcrypt_create(void **stream)
{
mz_stream_pkcrypt *pkcrypt = NULL;
pkcrypt = (mz_stream_pkcrypt *)MZ_ALLOC(sizeof(mz_stream_pkcrypt));
if (pkcrypt != NULL)
{
memset(pkcrypt, 0, sizeof(mz_stream_pkcrypt));
pkcrypt->stream.vtbl = &mz_stream_pkcrypt_vtbl;
}
if (stream != NULL)
*stream = pkcrypt;
return pkcrypt;
}
void mz_stream_pkcrypt_delete(void **stream)
{
mz_stream_pkcrypt *pkcrypt = NULL;
if (stream == NULL)
return;
pkcrypt = (mz_stream_pkcrypt *)*stream;
if (pkcrypt != NULL)
MZ_FREE(pkcrypt);
*stream = NULL;
}
void *mz_stream_pkcrypt_get_interface(void)
{
return (void *)&mz_stream_pkcrypt_vtbl;
}
+48
View File
@@ -0,0 +1,48 @@
/* mz_strm_pkcrypt.h -- Code for traditional PKWARE encryption
Version 2.3.9, July 26, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
This program is distributed under the terms of the same license as zlib.
See the accompanying LICENSE file for the full text of the license.
*/
#ifndef MZ_STREAM_PKCRYPT_H
#define MZ_STREAM_PKCRYPT_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/***************************************************************************/
int32_t mz_stream_pkcrypt_open(void *stream, const char *filename, int32_t mode);
int32_t mz_stream_pkcrypt_is_open(void *stream);
int32_t mz_stream_pkcrypt_read(void *stream, void *buf, int32_t size);
int32_t mz_stream_pkcrypt_write(void *stream, const void *buf, int32_t size);
int64_t mz_stream_pkcrypt_tell(void *stream);
int32_t mz_stream_pkcrypt_seek(void *stream, int64_t offset, int32_t origin);
int32_t mz_stream_pkcrypt_close(void *stream);
int32_t mz_stream_pkcrypt_error(void *stream);
void mz_stream_pkcrypt_set_password(void *stream, const char *password);
void mz_stream_pkcrypt_set_verify(void *stream, uint8_t verify1, uint8_t verify2);
void mz_stream_pkcrypt_get_verify(void *stream, uint8_t *verify1, uint8_t *verify2);
int32_t mz_stream_pkcrypt_get_prop_int64(void *stream, int32_t prop, int64_t *value);
void* mz_stream_pkcrypt_create(void **stream);
void mz_stream_pkcrypt_delete(void **stream);
void* mz_stream_pkcrypt_get_interface(void);
/***************************************************************************/
#ifdef __cplusplus
}
#endif
#endif
+24 -15
View File
@@ -1,8 +1,8 @@
/* mz_strm_posix.c -- Stream for filesystem access for posix/linux
Version 2.2.4, November 15th, 2017
Version 2.3.9, July 26, 2018
part of the MiniZip project
Copyright (C) 2012-2017 Nathan Moinvaziri
Copyright (C) 2010-2018 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
Modifications for Zip64 support
Copyright (C) 2009-2010 Mathias Svensson
@@ -50,7 +50,7 @@
/***************************************************************************/
mz_stream_vtbl mz_stream_posix_vtbl = {
static mz_stream_vtbl mz_stream_posix_vtbl = {
mz_stream_posix_open,
mz_stream_posix_is_open,
mz_stream_posix_read,
@@ -60,7 +60,9 @@ mz_stream_vtbl mz_stream_posix_vtbl = {
mz_stream_posix_close,
mz_stream_posix_error,
mz_stream_posix_create,
mz_stream_posix_delete
mz_stream_posix_delete,
NULL,
NULL
};
/***************************************************************************/
@@ -85,7 +87,7 @@ int32_t mz_stream_posix_open(void *stream, const char *path, int32_t mode)
if ((mode & MZ_OPEN_MODE_READWRITE) == MZ_OPEN_MODE_READ)
mode_fopen = "rb";
else if (mode & MZ_OPEN_MODE_APPEND)
mode_fopen = "ab";
mode_fopen = "r+b";
else if (mode & MZ_OPEN_MODE_CREATE)
mode_fopen = "wb";
else
@@ -113,9 +115,9 @@ int32_t mz_stream_posix_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 < 0)
if (read < size && ferror(posix->handle))
{
posix->error = ferror(posix->handle);
posix->error = errno;
return MZ_STREAM_ERROR;
}
return read;
@@ -125,9 +127,9 @@ int32_t mz_stream_posix_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 < 0)
if (written < size && ferror(posix->handle))
{
posix->error = ferror(posix->handle);
posix->error = errno;
return MZ_STREAM_ERROR;
}
return written;
@@ -139,7 +141,7 @@ int64_t mz_stream_posix_tell(void *stream)
int64_t position = ftello64(posix->handle);
if (position == -1)
{
posix->error = ferror(posix->handle);
posix->error = errno;
return MZ_STREAM_ERROR;
}
return position;
@@ -167,7 +169,7 @@ int32_t mz_stream_posix_seek(void *stream, int64_t offset, int32_t origin)
if (fseeko64(posix->handle, offset, fseek_origin) != 0)
{
posix->error = ferror(posix->handle);
posix->error = errno;
return MZ_STREAM_ERROR;
}
@@ -177,8 +179,12 @@ int32_t mz_stream_posix_seek(void *stream, int64_t offset, int32_t origin)
int32_t mz_stream_posix_close(void *stream)
{
mz_stream_posix *posix = (mz_stream_posix*)stream;
int32_t closed = fclose(posix->handle);
posix->handle = NULL;
int32_t closed = 0;
if (posix->handle != NULL)
{
closed = fclose(posix->handle);
posix->handle = NULL;
}
if (closed != 0)
{
posix->error = errno;
@@ -197,9 +203,12 @@ void *mz_stream_posix_create(void **stream)
{
mz_stream_posix *posix = NULL;
posix = (mz_stream_posix *)malloc(sizeof(mz_stream_posix));
posix = (mz_stream_posix *)MZ_ALLOC(sizeof(mz_stream_posix));
if (posix != NULL)
{
memset(posix, 0, sizeof(mz_stream_posix));
posix->stream.vtbl = &mz_stream_posix_vtbl;
}
if (stream != NULL)
*stream = posix;
@@ -213,7 +222,7 @@ void mz_stream_posix_delete(void **stream)
return;
posix = (mz_stream_posix *)*stream;
if (posix != NULL)
free(posix);
MZ_FREE(posix);
*stream = NULL;
}
+4 -9
View File
@@ -1,21 +1,16 @@
/* mz_strm_posix.h -- Stream for filesystem access for posix/linux
Version 2.2.4, November 15th, 2017
Version 2.3.9, July 26, 2018
part of the MiniZip project
Copyright (C) 2012-2017 Nathan Moinvaziri
Copyright (C) 2010-2018 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
Copyright (C) 2009-2010 Mathias Svensson
Modifications for Zip64 support
http://result42.com
Copyright (C) 1998-2010 Gilles Vollant
http://www.winimage.com/zLibDll/minizip.html
This program is distributed under the terms of the same license as zlib.
See the accompanying LICENSE file for the full text of the license.
*/
#ifndef _MZ_STREAM_POSIX_H
#define _MZ_STREAM_POSIX_H
#ifndef MZ_STREAM_POSIX_H
#define MZ_STREAM_POSIX_H
#include <stdint.h>
+33 -26
View File
@@ -1,8 +1,8 @@
/* mz_strm_split.c -- Stream for split files
Version 2.2.4, November 15th, 2017
Version 2.3.9, July 26, 2018
part of the MiniZip project
Copyright (C) 2012-2017 Nathan Moinvaziri
Copyright (C) 2010-2018 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
This program is distributed under the terms of the same license as zlib.
@@ -23,9 +23,13 @@
#define MZ_ZIP_MAGIC_DISKHEADER (0x08074b50)
#if defined(_MSC_VER) && _MSC_VER < 1900
# define snprintf _snprintf
#endif
/***************************************************************************/
mz_stream_vtbl mz_stream_split_vtbl = {
static mz_stream_vtbl mz_stream_split_vtbl = {
mz_stream_split_open,
mz_stream_split_is_open,
mz_stream_split_read,
@@ -62,14 +66,13 @@ typedef struct mz_stream_split_s {
/***************************************************************************/
int32_t mz_stream_split_open_disk(void *stream, int32_t number_disk)
static int32_t mz_stream_split_open_disk(void *stream, int32_t number_disk)
{
mz_stream_split *split = (mz_stream_split *)stream;
uint32_t magic = 0;
int32_t i = 0;
int32_t err = MZ_OK;
int16_t disk_part = 0;
int16_t read_disk_part = 0;
// Check if we are reading or writing a disk part or the cd disk
@@ -84,7 +87,7 @@ int32_t mz_stream_split_open_disk(void *stream, int32_t number_disk)
// Construct disk path
if (disk_part > 0)
{
for (i = strlen(split->path_disk) - 1; i >= 0; i -= 1)
for (i = (int32_t)strlen(split->path_disk) - 1; i >= 0; i -= 1)
{
if (split->path_disk[i] != '.')
continue;
@@ -136,7 +139,7 @@ int32_t mz_stream_split_open_disk(void *stream, int32_t number_disk)
return err;
}
int32_t mz_stream_split_close_disk(void *stream)
static int32_t mz_stream_split_close_disk(void *stream)
{
mz_stream_split *split = (mz_stream_split *)stream;
@@ -146,7 +149,7 @@ int32_t mz_stream_split_close_disk(void *stream)
return mz_stream_close(split->stream.base);
}
int32_t mz_stream_split_goto_disk(void *stream, int32_t number_disk)
static int32_t mz_stream_split_goto_disk(void *stream, int32_t number_disk)
{
mz_stream_split *split = (mz_stream_split *)stream;
int32_t err = MZ_OK;
@@ -177,12 +180,12 @@ int32_t mz_stream_split_open(void *stream, const char *path, int32_t mode)
split->mode = mode;
split->path_cd_size = strlen(path) + 1;
split->path_cd = (char *)malloc(split->path_cd_size);
split->path_cd_size = (int32_t)strlen(path) + 1;
split->path_cd = (char *)MZ_ALLOC(split->path_cd_size);
strncpy(split->path_cd, path, split->path_cd_size);
split->path_disk_size = strlen(path) + 10;
split->path_disk = (char *)malloc(split->path_disk_size);
split->path_disk_size = (int32_t)strlen(path) + 10;
split->path_disk = (char *)MZ_ALLOC(split->path_disk_size);
strncpy(split->path_disk, path, split->path_disk_size);
if (mode & MZ_OPEN_MODE_WRITE)
@@ -252,7 +255,7 @@ int32_t mz_stream_split_write(void *stream, const void *buf, int32_t size)
int32_t bytes_avail = 0;
int32_t number_disk = -1;
int32_t err = MZ_OK;
uint8_t *buf_ptr = (uint8_t *)buf;
const uint8_t *buf_ptr = (const uint8_t *)buf;
while (bytes_left > 0)
{
@@ -278,11 +281,11 @@ int32_t mz_stream_split_write(void *stream, const void *buf, int32_t size)
bytes_to_write = bytes_avail;
}
}
written = mz_stream_write(split->stream.base, buf_ptr, bytes_to_write);
if (written != bytes_to_write)
return MZ_STREAM_ERROR;
bytes_left -= written;
buf_ptr += written;
split->total_out += written;
@@ -335,15 +338,17 @@ int32_t mz_stream_split_get_prop_int64(void *stream, int32_t prop, int64_t *valu
{
case MZ_STREAM_PROP_TOTAL_OUT:
*value = split->total_out;
return MZ_OK;
break;
case MZ_STREAM_PROP_DISK_NUMBER:
*value = split->number_disk;
return MZ_OK;
break;
case MZ_STREAM_PROP_DISK_SIZE:
*value = split->disk_size;
return MZ_OK;
break;
default:
return MZ_EXIST_ERROR;
}
return MZ_EXIST_ERROR;
return MZ_OK;
}
int32_t mz_stream_split_set_prop_int64(void *stream, int32_t prop, int64_t value)
@@ -353,19 +358,21 @@ int32_t mz_stream_split_set_prop_int64(void *stream, int32_t prop, int64_t value
{
case MZ_STREAM_PROP_DISK_NUMBER:
split->number_disk = (int32_t)value;
return MZ_OK;
break;
case MZ_STREAM_PROP_DISK_SIZE:
split->disk_size = value;
return MZ_OK;
break;
default:
return MZ_EXIST_ERROR;
}
return MZ_EXIST_ERROR;
return MZ_OK;
}
void *mz_stream_split_create(void **stream)
{
mz_stream_split *split = NULL;
split = (mz_stream_split *)malloc(sizeof(mz_stream_split));
split = (mz_stream_split *)MZ_ALLOC(sizeof(mz_stream_split));
if (split != NULL)
{
memset(split, 0, sizeof(mz_stream_split));
@@ -386,11 +393,11 @@ void mz_stream_split_delete(void **stream)
if (split != NULL)
{
if (split->path_cd)
free(split->path_cd);
MZ_FREE(split->path_cd);
if (split->path_disk)
free(split->path_disk);
MZ_FREE(split->path_disk);
free(split);
MZ_FREE(split);
}
*stream = NULL;
}
+4 -4
View File
@@ -1,16 +1,16 @@
/* mz_strm_split.h -- Stream for split files
Version 2.2.4, November 15th, 2017
Version 2.3.9, July 26, 2018
part of MiniZip project
Copyright (C) 2012-2017 Nathan Moinvaziri
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_SPLIT_H
#define _MZ_STREAM_SPLIT_H
#ifndef MZ_STREAM_SPLIT_H
#define MZ_STREAM_SPLIT_H
#include <stdint.h>
+16 -15
View File
@@ -1,8 +1,8 @@
/* mz_strm_win32.c -- Stream for filesystem access for windows
Version 2.2.4, November 15th, 2017
Version 2.3.9, July 26, 2018
part of the MiniZip project
Copyright (C) 2012-2017 Nathan Moinvaziri
Copyright (C) 2010-2018 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
Copyright (C) 2009-2010 Mathias Svensson
Modifications for Zip64 support
@@ -32,15 +32,15 @@
# define INVALID_SET_FILE_POINTER ((DWORD)-1)
#endif
#if defined(WINAPI_FAMILY_ONE_PARTITION) && !defined(MZ_USE_WINRT_API)
#if defined(WINAPI_FAMILY_ONE_PARTITION) && !defined(MZ_WINRT_API)
# if WINAPI_FAMILY_ONE_PARTITION(WINAPI_FAMILY, WINAPI_PARTITION_APP)
# define MZ_USE_WINRT_API 1
# define MZ_WINRT_API 1
# endif
#endif
/***************************************************************************/
mz_stream_vtbl mz_stream_win32_vtbl = {
static mz_stream_vtbl mz_stream_win32_vtbl = {
mz_stream_win32_open,
mz_stream_win32_is_open,
mz_stream_win32_read,
@@ -50,7 +50,9 @@ mz_stream_vtbl mz_stream_win32_vtbl = {
mz_stream_win32_close,
mz_stream_win32_error,
mz_stream_win32_create,
mz_stream_win32_delete
mz_stream_win32_delete,
NULL,
NULL
};
/***************************************************************************/
@@ -82,7 +84,6 @@ int32_t mz_stream_win32_open(void *stream, const char *path, int32_t mode)
{
desired_access = GENERIC_READ;
creation_disposition = OPEN_EXISTING;
share_mode &= FILE_SHARE_WRITE;
}
else if (mode & MZ_OPEN_MODE_APPEND)
{
@@ -100,18 +101,18 @@ int32_t mz_stream_win32_open(void *stream, const char *path, int32_t mode)
}
path_wide_size = MultiByteToWideChar(CP_UTF8, 0, path, -1, NULL, 0);
path_wide = (wchar_t *)malloc((path_wide_size + 1) * sizeof(wchar_t));
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);
#ifdef MZ_USE_WINRT_API
#ifdef MZ_WINRT_API
win32->handle = CreateFile2W(path_wide, desired_access, share_mode, creation_disposition, NULL);
#else
win32->handle = CreateFileW(path_wide, desired_access, share_mode, NULL, creation_disposition, flags_attribs, NULL);
#endif
free(path_wide);
MZ_FREE(path_wide);
if (mz_stream_win32_is_open(stream) != MZ_OK)
{
@@ -122,7 +123,7 @@ int32_t mz_stream_win32_open(void *stream, const char *path, int32_t mode)
if (mode & MZ_OPEN_MODE_APPEND)
return mz_stream_win32_seek(stream, 0, MZ_SEEK_END);
return MZ_OK;
return MZ_OK;
}
int32_t mz_stream_win32_is_open(void *stream)
@@ -171,7 +172,7 @@ int32_t mz_stream_win32_write(void *stream, const void *buf, int32_t size)
static int32_t mz_stream_win32_seekinternal(HANDLE handle, LARGE_INTEGER large_pos, LARGE_INTEGER *new_pos, uint32_t move_method)
{
#ifdef MZ_USE_WINRT_API
#ifdef MZ_WINRT_API
return SetFilePointerEx(handle, pos, newPos, dwMoveMethod);
#else
LONG high_part = large_pos.HighPart;
@@ -262,7 +263,7 @@ void *mz_stream_win32_create(void **stream)
{
mz_stream_win32 *win32 = NULL;
win32 = (mz_stream_win32 *)malloc(sizeof(mz_stream_win32));
win32 = (mz_stream_win32 *)MZ_ALLOC(sizeof(mz_stream_win32));
if (win32 != NULL)
{
memset(win32, 0, sizeof(mz_stream_win32));
@@ -281,11 +282,11 @@ void mz_stream_win32_delete(void **stream)
return;
win32 = (mz_stream_win32 *)*stream;
if (win32 != NULL)
free(win32);
MZ_FREE(win32);
*stream = NULL;
}
void *mz_stream_win32_get_interface(void)
{
return (void *)&mz_stream_win32_vtbl;
}
}
+4 -9
View File
@@ -1,21 +1,16 @@
/* mz_sstrm_win32.h -- Stream for filesystem access for windows
Version 2.2.4, November 15th, 2017
Version 2.3.9, July 26, 2018
part of the MiniZip project
Copyright (C) 2012-2017 Nathan Moinvaziri
Copyright (C) 2010-2018 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
Copyright (C) 2009-2010 Mathias Svensson
Modifications for Zip64 support
http://result42.com
Copyright (C) 1998-2010 Gilles Vollant
http://www.winimage.com/zLibDll/minizip.html
This program is distributed under the terms of the same license as zlib.
See the accompanying LICENSE file for the full text of the license.
*/
#ifndef _MZ_STREAM_WIN32_H
#define _MZ_STREAM_WIN32_H
#ifndef MZ_STREAM_WIN32_H
#define MZ_STREAM_WIN32_H
#include <stdint.h>
+84 -30
View File
@@ -1,8 +1,8 @@
/* mz_strm_zlib.c -- Stream for zlib inflate/deflate
Version 2.2.4, November 15th, 2017
Version 2.3.9, July 26, 2018
part of the MiniZip project
Copyright (C) 2012-2017 Nathan Moinvaziri
Copyright (C) 2010-2018 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
This program is distributed under the terms of the same license as zlib.
@@ -23,6 +23,15 @@
/***************************************************************************/
// 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)
typedef unsigned long z_crc_t;
#endif
/***************************************************************************/
#ifndef DEF_MEM_LEVEL
# if MAX_MEM_LEVEL >= 8
# define DEF_MEM_LEVEL 8
@@ -33,7 +42,7 @@
/***************************************************************************/
mz_stream_vtbl mz_stream_zlib_vtbl = {
static mz_stream_vtbl mz_stream_zlib_vtbl = {
mz_stream_zlib_open,
mz_stream_zlib_is_open,
mz_stream_zlib_read,
@@ -70,6 +79,7 @@ int32_t mz_stream_zlib_open(void *stream, const char *path, int32_t mode)
{
mz_stream_zlib *zlib = (mz_stream_zlib *)stream;
MZ_UNUSED(path);
zlib->zstream.data_type = Z_BINARY;
zlib->zstream.zalloc = Z_NULL;
@@ -83,17 +93,25 @@ int32_t mz_stream_zlib_open(void *stream, const char *path, int32_t mode)
if (mode & MZ_OPEN_MODE_WRITE)
{
#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);
#endif
}
else if (mode & MZ_OPEN_MODE_READ)
{
#ifdef MZ_ZIP_NO_DECOMPRESSION
return MZ_SUPPORT_ERROR;
#else
zlib->zstream.next_in = zlib->buffer;
zlib->zstream.avail_in = 0;
zlib->error = inflateInit2(&zlib->zstream, -MAX_WBITS);
#endif
}
if (zlib->error != Z_OK)
@@ -114,6 +132,9 @@ int32_t mz_stream_zlib_is_open(void *stream)
int32_t mz_stream_zlib_read(void *stream, void *buf, int32_t size)
{
#ifdef MZ_ZIP_NO_DECOMPRESSION
return MZ_SUPPORT_ERROR;
#else
mz_stream_zlib *zlib = (mz_stream_zlib *)stream;
uint64_t total_in_before = 0;
uint64_t total_in_after = 0;
@@ -128,8 +149,8 @@ int32_t mz_stream_zlib_read(void *stream, void *buf, int32_t size)
int32_t err = Z_OK;
zlib->zstream.next_out = (uint8_t*)buf;
zlib->zstream.avail_out = (uint16_t)size;
zlib->zstream.next_out = (Bytef*)buf;
zlib->zstream.avail_out = (uInt)size;
do
{
@@ -138,7 +159,7 @@ int32_t mz_stream_zlib_read(void *stream, void *buf, int32_t size)
bytes_to_read = sizeof(zlib->buffer);
if (zlib->max_total_in > 0)
{
if ((zlib->max_total_in - zlib->total_in) < sizeof(zlib->buffer))
if ((zlib->max_total_in - zlib->total_in) < (int64_t)sizeof(zlib->buffer))
bytes_to_read = (int32_t)(zlib->max_total_in - zlib->total_in);
}
@@ -180,7 +201,7 @@ 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;
@@ -193,9 +214,10 @@ int32_t mz_stream_zlib_read(void *stream, void *buf, int32_t size)
return zlib->error;
return total_out;
#endif
}
int32_t mz_stream_zlib_flush(void *stream)
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)
@@ -203,7 +225,7 @@ int32_t mz_stream_zlib_flush(void *stream)
return MZ_OK;
}
int32_t mz_stream_zlib_deflate(void *stream, int flush)
static int32_t mz_stream_zlib_deflate(void *stream, int flush)
{
mz_stream_zlib *zlib = (mz_stream_zlib *)stream;
uint64_t total_out_before = 0;
@@ -234,16 +256,18 @@ int32_t mz_stream_zlib_deflate(void *stream, int flush)
out_bytes = (uint32_t)(total_out_after - total_out_before);
if (err != Z_OK && err != Z_STREAM_END)
zlib->buffer_len += out_bytes;
zlib->total_out += out_bytes;
if (err == Z_STREAM_END)
break;
if (err != Z_OK)
{
zlib->error = err;
return MZ_STREAM_ERROR;
}
zlib->buffer_len += out_bytes;
zlib->total_out += out_bytes;
}
while (zlib->zstream.avail_in > 0);
while ((zlib->zstream.avail_in > 0) || (flush == Z_FINISH && err == Z_OK));
return MZ_OK;
}
@@ -251,25 +275,35 @@ int32_t mz_stream_zlib_deflate(void *stream, int flush)
int32_t mz_stream_zlib_write(void *stream, const void *buf, int32_t size)
{
mz_stream_zlib *zlib = (mz_stream_zlib *)stream;
int32_t err = size;
zlib->zstream.next_in = (uint8_t*)buf;
zlib->zstream.avail_in = size;
#ifdef MZ_ZIP_NO_COMPRESSION
MZ_UNUSED(zlib);
err = MZ_SUPPORT_ERROR;
#else
zlib->zstream.next_in = (Bytef*)(intptr_t)buf;
zlib->zstream.avail_in = (uInt)size;
mz_stream_zlib_deflate(stream, Z_NO_FLUSH);
zlib->total_in += size;
return size;
#endif
return err;
}
int64_t mz_stream_zlib_tell(void *stream)
{
MZ_UNUSED(stream);
return MZ_STREAM_ERROR;
}
int32_t mz_stream_zlib_seek(void *stream, int64_t offset, int32_t origin)
{
MZ_UNUSED(stream);
MZ_UNUSED(offset);
MZ_UNUSED(origin);
return MZ_STREAM_ERROR;
}
@@ -280,14 +314,22 @@ int32_t mz_stream_zlib_close(void *stream)
if (zlib->mode & MZ_OPEN_MODE_WRITE)
{
#ifdef MZ_ZIP_NO_COMPRESSION
return MZ_SUPPORT_ERROR;
#else
mz_stream_zlib_deflate(stream, Z_FINISH);
mz_stream_zlib_flush(stream);
deflateEnd(&zlib->zstream);
#endif
}
else if (zlib->mode & MZ_OPEN_MODE_READ)
{
#ifdef MZ_ZIP_NO_DECOMPRESSION
return MZ_SUPPORT_ERROR;
#else
inflateEnd(&zlib->zstream);
#endif
}
zlib->initialized = 0;
@@ -310,15 +352,20 @@ int32_t mz_stream_zlib_get_prop_int64(void *stream, int32_t prop, int64_t *value
{
case MZ_STREAM_PROP_TOTAL_IN:
*value = zlib->total_in;
return MZ_OK;
break;
case MZ_STREAM_PROP_TOTAL_IN_MAX:
*value = zlib->max_total_in;
break;
case MZ_STREAM_PROP_TOTAL_OUT:
*value = zlib->total_out;
return MZ_OK;
break;
case MZ_STREAM_PROP_HEADER_SIZE:
*value = 0;
return MZ_OK;
break;
default:
return MZ_EXIST_ERROR;
}
return MZ_EXIST_ERROR;
return MZ_OK;
}
int32_t mz_stream_zlib_set_prop_int64(void *stream, int32_t prop, int64_t value)
@@ -328,19 +375,21 @@ int32_t mz_stream_zlib_set_prop_int64(void *stream, int32_t prop, int64_t value)
{
case MZ_STREAM_PROP_COMPRESS_LEVEL:
zlib->level = (int16_t)value;
return MZ_OK;
break;
case MZ_STREAM_PROP_TOTAL_IN_MAX:
zlib->max_total_in = value;
return MZ_OK;
break;
default:
return MZ_EXIST_ERROR;
}
return MZ_EXIST_ERROR;
return MZ_OK;
}
void *mz_stream_zlib_create(void **stream)
{
mz_stream_zlib *zlib = NULL;
zlib = (mz_stream_zlib *)malloc(sizeof(mz_stream_zlib));
zlib = (mz_stream_zlib *)MZ_ALLOC(sizeof(mz_stream_zlib));
if (zlib != NULL)
{
memset(zlib, 0, sizeof(mz_stream_zlib));
@@ -360,7 +409,7 @@ void mz_stream_zlib_delete(void **stream)
return;
zlib = (mz_stream_zlib *)*stream;
if (zlib != NULL)
free(zlib);
MZ_FREE(zlib);
*stream = NULL;
}
@@ -369,7 +418,12 @@ void *mz_stream_zlib_get_interface(void)
return (void *)&mz_stream_zlib_vtbl;
}
static int64_t mz_stream_zlib_crc32(int64_t value, const void *buf, int32_t size)
{
return crc32((z_crc_t)value, buf, size);
}
void *mz_stream_zlib_get_crc32_update(void)
{
return (void *)crc32;
}
return (void *)mz_stream_zlib_crc32;
}
+4 -4
View File
@@ -1,16 +1,16 @@
/* mz_strm_zlib.h -- Stream for zlib inflate/deflate
Version 2.2.4, November 15th, 2017
Version 2.3.9, July 26, 2018
part of the MiniZip project
Copyright (C) 2012-2017 Nathan Moinvaziri
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_ZLIB_H
#define _MZ_STREAM_ZLIB_H
#ifndef MZ_STREAM_ZLIB_H
#define MZ_STREAM_ZLIB_H
#include <stdint.h>
Executable → Regular
+304 -169
View File
@@ -1,9 +1,8 @@
/* zip.c -- Zip manipulation
Version 2.2.4, November 15th, 2017
Version 2.3.9, July 26, 2018
part of the MiniZip project
Copyright (C) 2010-2017 Nathan Moinvaziri
Modifications for AES, PKWARE disk spanning
Copyright (C) 2010-2018 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
Copyright (C) 2009-2010 Mathias Svensson
Modifications for Zip64 support
@@ -21,6 +20,7 @@
#include <string.h>
#include <time.h>
#include <errno.h>
#include <limits.h>
#include "mz.h"
#include "mz_strm.h"
@@ -30,16 +30,17 @@
#ifdef HAVE_BZIP2
# include "mz_strm_bzip.h"
#endif
#ifdef HAVE_CRYPT
# include "mz_strm_crypt.h"
#endif
#include "mz_strm_crc32.h"
#ifdef HAVE_LZMA
# include "mz_strm_lzma.h"
#endif
#include "mz_strm_mem.h"
#ifdef HAVE_PKCRYPT
# include "mz_strm_pkcrypt.h"
#endif
#ifdef HAVE_ZLIB
# include "mz_strm_zlib.h"
#endif
#include "mz_strm_mem.h"
#include "mz_zip.h"
@@ -54,7 +55,6 @@
#define MZ_ZIP_SIZE_CD_ITEM (0x2e)
#define MZ_ZIP_SIZE_CD_LOCATOR64 (0x14)
#define MZ_ZIP_SIZE_LOCALHEADER (0x1e)
#define MZ_ZIP_EXTENSION_ZIP64 (0x0001)
#define MZ_ZIP_EXTENSION_NTFS (0x000a)
@@ -79,15 +79,15 @@ typedef struct mz_zip_s
int32_t open_mode;
uint32_t disk_number_with_cd; // number of the disk with the central dir
uint64_t disk_offset_shift; // correction for zips that have wrong offset start of cd
uint64_t cd_start_pos; // pos of the first file in the central dir stream
uint64_t cd_current_pos; // pos of the current file in the central dir
uint64_t cd_offset; // offset of start of central directory
uint64_t cd_size; // size of the central directory
uint16_t entry_scanned;
uint16_t entry_scanned;
uint16_t entry_opened; // 1 if a file in the zip is currently writ.
uint64_t entry_read;
int64_t number_entry;
@@ -99,7 +99,7 @@ typedef struct mz_zip_s
/***************************************************************************/
// Locate the central directory of a zip file (at the end, just before the global comment)
// Locate the end of central directory of a zip file (at the end, just before the global comment)
static int32_t mz_zip_search_eocd(void *stream, uint64_t *central_pos)
{
uint8_t buf[1024 + 4];
@@ -154,7 +154,7 @@ static int32_t mz_zip_search_eocd(void *stream, uint64_t *central_pos)
return MZ_EXIST_ERROR;
}
// Locate the central directory 64 of a zip file (at the end, just before the global comment)
// Locate the end of central directory 64 of a zip file
static int32_t mz_zip_search_zip64_eocd(void *stream, const uint64_t end_central_offset, uint64_t *central_pos)
{
uint64_t offset = 0;
@@ -250,10 +250,21 @@ static int32_t mz_zip_read_cd(void *handle)
// Offset of start of central directory with respect to the starting disk number
if (err == MZ_OK)
err = mz_stream_read_uint32(zip->stream, &value32);
zip->cd_offset = value32;
if (err == MZ_OK)
zip->cd_offset = value32;
// Zip file global comment length
if (err == MZ_OK)
err = mz_stream_read_uint16(zip->stream, &comment_size);
if ((err == MZ_OK) && (comment_size > 0))
{
zip->comment = (char *)MZ_ALLOC(comment_size + 1);
if (zip->comment)
{
if (mz_stream_read(zip->stream, zip->comment, comment_size) != comment_size)
err = MZ_STREAM_ERROR;
zip->comment[comment_size] = 0;
}
}
if ((err == MZ_OK) && ((number_entry_cd == UINT16_MAX) || (zip->cd_offset == UINT32_MAX)))
{
@@ -310,14 +321,25 @@ static int32_t mz_zip_read_cd(void *handle)
err = MZ_FORMAT_ERROR;
}
if ((err == MZ_OK) && (comment_size > 0))
if (err == MZ_OK)
{
zip->comment = (char *)malloc(comment_size + 1);
if (zip->comment)
// Verify central directory signature exists at offset
err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
if (err == MZ_OK)
err = mz_stream_read_uint32(zip->stream, &value32);
if (value32 != MZ_ZIP_MAGIC_CENTRALHEADER)
{
if (mz_stream_read(zip->stream, zip->comment, comment_size) != comment_size)
err = MZ_STREAM_ERROR;
zip->comment[comment_size] = 0;
// If not found attempt to seek backward to find it
err = mz_stream_seek(zip->stream, eocd_pos - zip->cd_size, MZ_SEEK_SET);
if (err == MZ_OK)
err = mz_stream_read_uint32(zip->stream, &value32);
if (value32 == MZ_ZIP_MAGIC_CENTRALHEADER)
{
// If found compensate for incorrect locations
value64 = zip->cd_offset;
zip->cd_offset = eocd_pos - zip->cd_size;
zip->disk_offset_shift = zip->cd_offset - value64;
}
}
}
@@ -336,7 +358,7 @@ static int32_t mz_zip_write_cd(void *handle)
if (zip == NULL)
return MZ_PARAM_ERROR;
if (mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number) == MZ_OK)
zip->disk_number_with_cd = (uint32_t)disk_number;
if (mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_SIZE, &disk_size) == MZ_OK && disk_size > 0)
@@ -347,11 +369,11 @@ static int32_t mz_zip_write_cd(void *handle)
mz_stream_seek(zip->cd_mem_stream, 0, MZ_SEEK_END);
zip->cd_size = (uint32_t)mz_stream_tell(zip->cd_mem_stream);
mz_stream_seek(zip->cd_mem_stream, 0, MZ_SEEK_SET);
err = mz_stream_copy(zip->stream, zip->cd_mem_stream, (int32_t)zip->cd_size);
// Write the ZIP64 central directory header
if (zip->cd_offset >= UINT32_MAX || zip->number_entry > UINT32_MAX)
if (zip->cd_offset >= UINT32_MAX || zip->number_entry > UINT16_MAX)
{
zip64_eocd_pos_inzip = mz_stream_tell(zip->stream);
@@ -395,12 +417,12 @@ static int32_t mz_zip_write_cd(void *handle)
err = mz_stream_write_uint64(zip->stream, zip64_eocd_pos_inzip);
// Number of the disk with the start of the central directory
if (err == MZ_OK)
err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd);
err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd + 1);
}
// Write the central directory header
// Signature
// Signature
if (err == MZ_OK)
err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDHEADER);
// Number of this disk
@@ -456,16 +478,15 @@ extern void* mz_zip_open(void *stream, int32_t mode)
int32_t err = MZ_OK;
zip = (mz_zip *)malloc(sizeof(mz_zip));
zip = (mz_zip *)MZ_ALLOC(sizeof(mz_zip));
if (zip == NULL)
return NULL;
memset(zip, 0, sizeof(mz_zip));
zip->stream = stream;
zip->open_mode = mode;
if (zip->open_mode & MZ_OPEN_MODE_WRITE)
if (mode & MZ_OPEN_MODE_WRITE)
{
mz_stream_mem_create(&zip->cd_mem_stream);
mz_stream_mem_open(zip->cd_mem_stream, NULL, MZ_OPEN_MODE_CREATE);
@@ -477,9 +498,10 @@ extern void* mz_zip_open(void *stream, int32_t mode)
zip->cd_stream = stream;
}
if ((zip->open_mode & MZ_OPEN_MODE_READ) || (mode & MZ_OPEN_MODE_APPEND))
if ((mode & MZ_OPEN_MODE_READ) || (mode & MZ_OPEN_MODE_APPEND))
{
err = mz_zip_read_cd(zip);
if ((mode & MZ_OPEN_MODE_CREATE) == 0)
err = mz_zip_read_cd(zip);
if ((err == MZ_OK) && (mode & MZ_OPEN_MODE_APPEND))
{
@@ -514,23 +536,12 @@ extern void* mz_zip_open(void *stream, int32_t mode)
if (err != MZ_OK)
{
if (zip->file_info_stream != NULL)
mz_stream_mem_delete(&zip->file_info_stream);
if (zip->local_file_info_stream != NULL)
mz_stream_mem_delete(&zip->local_file_info_stream);
if (zip->cd_mem_stream != NULL)
{
mz_stream_close(zip->cd_mem_stream);
mz_stream_delete(&zip->cd_mem_stream);
}
if (zip->comment)
free(zip->comment);
free(zip);
mz_zip_close(zip);
return NULL;
}
zip->open_mode = mode;
return zip;
}
@@ -558,15 +569,21 @@ extern int32_t mz_zip_close(void *handle)
mz_stream_delete(&zip->cd_mem_stream);
}
mz_stream_mem_close(zip->file_info_stream);
mz_stream_mem_delete(&zip->file_info_stream);
mz_stream_mem_close(zip->local_file_info_stream);
mz_stream_mem_delete(&zip->local_file_info_stream);
if (zip->file_info_stream != NULL)
{
mz_stream_mem_close(zip->file_info_stream);
mz_stream_mem_delete(&zip->file_info_stream);
}
if (zip->local_file_info_stream != NULL)
{
mz_stream_mem_close(zip->local_file_info_stream);
mz_stream_mem_delete(&zip->local_file_info_stream);
}
if (zip->comment)
free(zip->comment);
MZ_FREE(zip->comment);
free(zip);
MZ_FREE(zip);
return err;
}
@@ -589,9 +606,9 @@ extern int32_t mz_zip_set_comment(void *handle, const char *comment)
if (zip == NULL || comment == NULL)
return MZ_PARAM_ERROR;
if (zip->comment != NULL)
free(zip->comment);
MZ_FREE(zip->comment);
comment_size = (uint16_t)(strlen(comment) + 1);
zip->comment = (char *)malloc(comment_size);
zip->comment = (char *)MZ_ALLOC(comment_size);
strncpy(zip->comment, comment, comment_size);
return MZ_OK;
}
@@ -614,27 +631,6 @@ extern int32_t mz_zip_set_version_madeby(void *handle, uint16_t version_madeby)
return MZ_OK;
}
static int16_t mz_zip_entry_get_version_needed(int16_t zip64, mz_zip_file *file_info)
{
int16_t version_needed = 20;
if (file_info == NULL)
return MZ_PARAM_ERROR;
if (file_info->version_needed > 0)
version_needed = file_info->version_needed;
if (zip64)
version_needed = 45;
#ifdef HAVE_AES
if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
version_needed = 51;
#endif
#ifdef HAVE_LZMA
if (file_info->compression_method == MZ_COMPRESS_METHOD_LZMA)
version_needed = 63;
#endif
return version_needed;
}
// Get info about the current file in the zip file
static int32_t mz_zip_entry_read_header(void *stream, uint8_t local, mz_zip_file *file_info, void *file_info_stream)
{
@@ -668,7 +664,7 @@ static int32_t mz_zip_entry_read_header(void *stream, uint8_t local, mz_zip_file
err = MZ_FORMAT_ERROR;
else if ((!local) && (magic != MZ_ZIP_MAGIC_CENTRALHEADER))
err = MZ_FORMAT_ERROR;
// Read header fields
if (err == MZ_OK)
{
@@ -688,11 +684,15 @@ static int32_t mz_zip_entry_read_header(void *stream, uint8_t local, mz_zip_file
if (err == MZ_OK)
err = mz_stream_read_uint32(stream, &file_info->crc);
if (err == MZ_OK)
{
err = mz_stream_read_uint32(stream, &value32);
file_info->compressed_size = value32;
file_info->compressed_size = value32;
}
if (err == MZ_OK)
{
err = mz_stream_read_uint32(stream, &value32);
file_info->uncompressed_size = value32;
file_info->uncompressed_size = value32;
}
if (err == MZ_OK)
err = mz_stream_read_uint16(stream, &file_info->filename_size);
if (err == MZ_OK)
@@ -702,15 +702,19 @@ static int32_t mz_zip_entry_read_header(void *stream, uint8_t local, mz_zip_file
if (err == MZ_OK)
err = mz_stream_read_uint16(stream, &file_info->comment_size);
if (err == MZ_OK)
{
err = mz_stream_read_uint16(stream, &value16);
file_info->disk_number = value16;
file_info->disk_number = value16;
}
if (err == MZ_OK)
err = mz_stream_read_uint16(stream, &file_info->internal_fa);
if (err == MZ_OK)
err = mz_stream_read_uint32(stream, &file_info->external_fa);
if (err == MZ_OK)
{
err = mz_stream_read_uint32(stream, &value32);
file_info->disk_offset = value32;
file_info->disk_offset = value32;
}
}
}
@@ -722,7 +726,7 @@ static int32_t mz_zip_entry_read_header(void *stream, uint8_t local, mz_zip_file
if ((err == MZ_OK) && (file_info->filename_size > 0))
{
mz_stream_mem_get_buffer(file_info_stream, (void **)&file_info->filename);
mz_stream_mem_get_buffer(file_info_stream, (const void **)&file_info->filename);
err = mz_stream_copy(file_info_stream, stream, file_info->filename_size);
if (err == MZ_OK)
@@ -733,7 +737,7 @@ static int32_t mz_zip_entry_read_header(void *stream, uint8_t local, mz_zip_file
if ((err == MZ_OK) && (file_info->extrafield_size > 0))
{
mz_stream_mem_get_buffer_at(file_info_stream, seek, (void **)&file_info->extrafield);
mz_stream_mem_get_buffer_at(file_info_stream, seek, (const void **)&file_info->extrafield);
err = mz_stream_copy(file_info_stream, stream, file_info->extrafield_size);
if (err == MZ_OK)
@@ -759,15 +763,15 @@ static int32_t mz_zip_entry_read_header(void *stream, uint8_t local, mz_zip_file
if ((err == MZ_OK) && (file_info->compressed_size == UINT32_MAX))
err = mz_stream_read_uint64(file_info_stream, &file_info->compressed_size);
if ((err == MZ_OK) && (file_info->disk_offset == UINT32_MAX))
err = mz_stream_read_uint64(file_info_stream, &value64);
file_info->disk_offset = value64;
if ((err == MZ_OK) && (file_info->disk_number == UINT32_MAX))
err = mz_stream_read_uint64(file_info_stream, &file_info->disk_offset);
if ((err == MZ_OK) && (file_info->disk_number == UINT16_MAX))
err = mz_stream_read_uint32(file_info_stream, &file_info->disk_number);
}
// NTFS extra field
else if (extra_header_id == MZ_ZIP_EXTENSION_NTFS)
{
err = mz_stream_read_uint32(file_info_stream, &reserved);
if (err == MZ_OK)
err = mz_stream_read_uint32(file_info_stream, &reserved);
extra_data_size_read = 4;
while ((err == MZ_OK) && (extra_data_size_read < extra_data_size))
@@ -776,17 +780,17 @@ static int32_t mz_zip_entry_read_header(void *stream, uint8_t local, mz_zip_file
if (err == MZ_OK)
err = mz_stream_read_uint16(file_info_stream, &ntfs_attrib_size);
if (ntfs_attrib_id == 0x01 && ntfs_attrib_size >= 8)
if ((err == MZ_OK) && (ntfs_attrib_id == 0x01) && (ntfs_attrib_size == 24))
{
err = mz_stream_read_uint64(file_info_stream, &ntfs_time);
mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->modified_date);
if ((err == MZ_OK) && (ntfs_attrib_size >= 16))
if (err == MZ_OK)
{
err = mz_stream_read_uint64(file_info_stream, &ntfs_time);
mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->accessed_date);
}
if ((err == MZ_OK) && (ntfs_attrib_size >= 24))
if (err == MZ_OK)
{
err = mz_stream_read_uint64(file_info_stream, &ntfs_time);
mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->creation_date);
@@ -794,7 +798,8 @@ static int32_t mz_zip_entry_read_header(void *stream, uint8_t local, mz_zip_file
}
else
{
err = mz_stream_seek(file_info_stream, ntfs_attrib_size, MZ_SEEK_CUR);
if (err == MZ_OK)
err = mz_stream_seek(file_info_stream, ntfs_attrib_size, MZ_SEEK_CUR);
}
extra_data_size_read += ntfs_attrib_size + 4;
@@ -821,11 +826,15 @@ static int32_t mz_zip_entry_read_header(void *stream, uint8_t local, mz_zip_file
err = MZ_FORMAT_ERROR;
// Get AES encryption strength and actual compression method
if (err == MZ_OK)
{
err = mz_stream_read_uint8(file_info_stream, &value8);
file_info->aes_encryption_mode = value8;
file_info->aes_encryption_mode = value8;
}
if (err == MZ_OK)
{
err = mz_stream_read_uint16(file_info_stream, &value16);
file_info->compression_method = value16;
file_info->compression_method = value16;
}
}
#endif
else
@@ -839,7 +848,7 @@ static int32_t mz_zip_entry_read_header(void *stream, uint8_t local, mz_zip_file
if ((err == MZ_OK) && (file_info->comment_size > 0))
{
mz_stream_mem_get_buffer_at(file_info_stream, seek, (void **)&file_info->comment);
mz_stream_mem_get_buffer_at(file_info_stream, seek, (const void **)&file_info->comment);
err = mz_stream_copy(file_info_stream, stream, file_info->comment_size);
if (err == MZ_OK)
@@ -851,7 +860,6 @@ static int32_t mz_zip_entry_read_header(void *stream, uint8_t local, mz_zip_file
static int32_t mz_zip_entry_write_header(void *stream, uint8_t local, mz_zip_file *file_info)
{
struct tm *local_time = NULL;
uint64_t ntfs_time = 0;
uint32_t reserved = 0;
uint32_t dos_date = 0;
@@ -859,13 +867,16 @@ static int32_t mz_zip_entry_write_header(void *stream, uint8_t local, mz_zip_fil
uint16_t extrafield_zip64_size = 0;
uint16_t extrafield_ntfs_size = 0;
uint16_t filename_size = 0;
uint16_t filename_length = 0;
uint16_t comment_size = 0;
uint16_t version_needed = 0;
uint8_t zip64 = 0;
int32_t err = MZ_OK;
if (file_info == NULL)
return MZ_PARAM_ERROR;
// Calculate extra field sizes
extrafield_size = file_info->extrafield_size;
if (file_info->uncompressed_size >= UINT32_MAX)
@@ -875,8 +886,19 @@ static int32_t mz_zip_entry_write_header(void *stream, uint8_t local, mz_zip_fil
if (file_info->disk_offset >= UINT32_MAX)
extrafield_zip64_size += 8;
// Use 64-bit data descriptor if we don't know uncompressed size ahead of time
zip64 = (local && file_info->uncompressed_size == 0) || (extrafield_zip64_size > 0);
if (file_info->zip64 == MZ_ZIP64_AUTO)
{
// If uncompressed size is unknown, assume zip64 for 64-bit data descriptors
zip64 = (local && file_info->uncompressed_size == 0) || (extrafield_zip64_size > 0);
}
else if (file_info->zip64 == MZ_ZIP64_FORCE)
zip64 = 1;
else if (file_info->zip64 == MZ_ZIP64_DISABLE)
{
// Zip64 extension is required to zip file
if (extrafield_zip64_size > 0)
return MZ_PARAM_ERROR;
}
if (zip64)
{
@@ -888,23 +910,15 @@ static int32_t mz_zip_entry_write_header(void *stream, uint8_t local, mz_zip_fil
extrafield_size += 4 + 7;
#endif
// NTFS timestamps
if (file_info->modified_date != 0)
extrafield_ntfs_size += 8;
if (file_info->accessed_date != 0)
extrafield_ntfs_size += 8;
if (file_info->creation_date != 0)
extrafield_ntfs_size += 8;
if (extrafield_ntfs_size > 4)
if ((file_info->modified_date != 0) &&
(file_info->accessed_date != 0) &&
(file_info->creation_date != 0))
{
extrafield_ntfs_size += 4 + 4;
extrafield_ntfs_size += 8 + 8 + 8 + 4 + 2 + 2;
extrafield_size += 4;
extrafield_size += extrafield_ntfs_size;
}
file_info->version_needed = mz_zip_entry_get_version_needed(zip64, file_info);
if (local)
err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_LOCALHEADER);
else
@@ -914,8 +928,26 @@ static int32_t mz_zip_entry_write_header(void *stream, uint8_t local, mz_zip_fil
err = mz_stream_write_uint16(stream, file_info->version_madeby);
}
// Calculate version needed to extract
if (err == MZ_OK)
err = mz_stream_write_uint16(stream, file_info->version_needed);
{
version_needed = file_info->version_needed;
if (version_needed == 0)
{
version_needed = 20;
if (zip64)
version_needed = 45;
#ifdef HAVE_AES
if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
version_needed = 51;
#endif
#ifdef HAVE_LZMA
if (file_info->compression_method == MZ_COMPRESS_METHOD_LZMA)
version_needed = 63;
#endif
}
err = mz_stream_write_uint16(stream, version_needed);
}
if (err == MZ_OK)
err = mz_stream_write_uint16(stream, file_info->flag);
if (err == MZ_OK)
@@ -951,9 +983,20 @@ static int32_t mz_zip_entry_write_header(void *stream, uint8_t local, mz_zip_fil
err = mz_stream_write_uint32(stream, (uint32_t)file_info->uncompressed_size);
}
filename_size = (uint16_t)strlen(file_info->filename);
filename_length = (uint16_t)strlen(file_info->filename);
if (err == MZ_OK)
{
filename_size = filename_length;
if (mz_zip_attrib_is_dir(file_info->external_fa, file_info->version_madeby) == MZ_OK)
{
if ((file_info->filename[filename_length - 1] == '/') ||
(file_info->filename[filename_length - 1] == '\\'))
filename_length -= 1;
else
filename_size += 1;
}
err = mz_stream_write_uint16(stream, filename_size);
}
if (err == MZ_OK)
err = mz_stream_write_uint16(stream, extrafield_size);
@@ -980,8 +1023,14 @@ static int32_t mz_zip_entry_write_header(void *stream, uint8_t local, mz_zip_fil
if (err == MZ_OK)
{
if (mz_stream_write(stream, file_info->filename, filename_size) != filename_size)
if (mz_stream_write(stream, file_info->filename, filename_length) != filename_length)
err = MZ_STREAM_ERROR;
if (err == MZ_OK)
{
// Ensure that directories have a slash appended to them for compatibility
if (mz_zip_attrib_is_dir(file_info->external_fa, file_info->version_madeby) == MZ_OK)
err = mz_stream_write_uint8(stream, '/');
}
}
if (err == MZ_OK)
{
@@ -1013,17 +1062,17 @@ static int32_t mz_zip_entry_write_header(void *stream, uint8_t local, mz_zip_fil
err = mz_stream_write_uint16(stream, 0x01);
if (err == MZ_OK)
err = mz_stream_write_uint16(stream, extrafield_ntfs_size - 8);
if ((err == MZ_OK) && (file_info->modified_date != 0))
if (err == MZ_OK)
{
mz_zip_unix_to_ntfs_time(file_info->modified_date, &ntfs_time);
err = mz_stream_write_uint64(stream, ntfs_time);
}
if ((err == MZ_OK) && (file_info->accessed_date != 0))
if (err == MZ_OK)
{
mz_zip_unix_to_ntfs_time(file_info->accessed_date, &ntfs_time);
err = mz_stream_write_uint64(stream, ntfs_time);
}
if ((err == MZ_OK) && (file_info->creation_date != 0))
if (err == MZ_OK)
{
mz_zip_unix_to_ntfs_time(file_info->creation_date, &ntfs_time);
err = mz_stream_write_uint64(stream, ntfs_time);
@@ -1061,7 +1110,7 @@ static int32_t mz_zip_entry_open_int(void *handle, int16_t compression_method, i
{
mz_zip *zip = (mz_zip *)handle;
int64_t max_total_in = 0;
int64_t total_in = 0;
int64_t header_size = 0;
int64_t footer_size = 0;
int32_t err = MZ_OK;
@@ -1086,10 +1135,8 @@ static int32_t mz_zip_entry_open_int(void *handle, int16_t compression_method, i
return MZ_PARAM_ERROR;
}
if ((zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED) && (password == NULL))
return MZ_PARAM_ERROR;
if ((err == MZ_OK) && (zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED))
if ((err == MZ_OK) && (zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED) &&
(zip->compression_method != MZ_COMPRESS_METHOD_RAW || password != NULL))
{
#ifdef HAVE_AES
if (zip->file_info.aes_version)
@@ -1101,22 +1148,31 @@ static int32_t mz_zip_entry_open_int(void *handle, int16_t compression_method, i
else
#endif
{
#ifdef HAVE_CRYPT
uint32_t dos_date = 0;
#ifdef HAVE_PKCRYPT
uint8_t verify1 = 0;
uint8_t verify2 = 0;
// Info-ZIP modification to ZipCrypto format:
// If bit 3 of the general purpose bit flag is set, it uses high byte of 16-bit File Time.
dos_date = mz_zip_time_t_to_dos_date(zip->file_info.modified_date);
if (zip->file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR)
{
uint32_t dos_date = 0;
verify1 = (uint8_t)((dos_date >> 16) & 0xff);
verify2 = (uint8_t)((dos_date >> 8) & 0xff);
dos_date = mz_zip_time_t_to_dos_date(zip->file_info.modified_date);
mz_stream_crypt_create(&zip->crypt_stream);
mz_stream_crypt_set_password(zip->crypt_stream, password);
mz_stream_crypt_set_verify(zip->crypt_stream, verify1, verify2);
verify1 = (uint8_t)((dos_date >> 16) & 0xff);
verify2 = (uint8_t)((dos_date >> 8) & 0xff);
}
else
{
verify1 = (uint8_t)((zip->file_info.crc >> 16) & 0xff);
verify2 = (uint8_t)((zip->file_info.crc >> 24) & 0xff);
}
mz_stream_pkcrypt_create(&zip->crypt_stream);
mz_stream_pkcrypt_set_password(zip->crypt_stream, password);
mz_stream_pkcrypt_set_verify(zip->crypt_stream, verify1, verify2);
#endif
}
}
@@ -1135,8 +1191,10 @@ static int32_t mz_zip_entry_open_int(void *handle, int16_t compression_method, i
{
if (zip->compression_method == MZ_COMPRESS_METHOD_RAW)
mz_stream_raw_create(&zip->compress_stream);
#ifdef HAVE_ZLIB
else if (zip->compression_method == MZ_COMPRESS_METHOD_DEFLATE)
mz_stream_zlib_create(&zip->compress_stream);
#endif
#ifdef HAVE_BZIP2
else if (zip->compression_method == MZ_COMPRESS_METHOD_BZIP2)
mz_stream_bzip_create(&zip->compress_stream);
@@ -1157,15 +1215,23 @@ static int32_t mz_zip_entry_open_int(void *handle, int16_t compression_method, i
}
else
{
if (zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED || zip->compression_method == MZ_COMPRESS_METHOD_RAW)
if (zip->compression_method == MZ_COMPRESS_METHOD_RAW || zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED)
{
max_total_in = zip->file_info.compressed_size;
mz_stream_set_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, max_total_in);
if (mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_HEADER_SIZE, &header_size) == MZ_OK)
max_total_in -= header_size;
if (mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_FOOTER_SIZE, &footer_size) == MZ_OK)
max_total_in -= footer_size;
if (mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_TOTAL_IN, &total_in) == MZ_OK)
max_total_in -= total_in;
mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, max_total_in);
}
if (zip->compression_method == MZ_COMPRESS_METHOD_LZMA && (zip->file_info.flag & MZ_ZIP_FLAG_LZMA_EOS_MARKER) == 0)
{
mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, zip->file_info.compressed_size);
mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_OUT_MAX, zip->file_info.uncompressed_size);
}
}
mz_stream_set_base(zip->compress_stream, zip->crypt_stream);
@@ -1175,16 +1241,6 @@ static int32_t mz_zip_entry_open_int(void *handle, int16_t compression_method, i
if (err == MZ_OK)
{
mz_stream_crc32_create(&zip->crc32_stream);
#ifdef HAVE_ZLIB
mz_stream_crc32_set_update_func(zip->crc32_stream,
(mz_stream_crc32_update)mz_stream_zlib_get_crc32_update());
#elif defined(HAVE_LZMA)
mz_stream_crc32_set_update_func(zip->crc32_stream,
(mz_stream_crc32_update)mz_stream_lzma_get_crc32_update());
#else
#error ZLIB or LZMA required for CRC32
#endif
mz_stream_set_base(zip->crc32_stream, zip->compress_stream);
err = mz_stream_open(zip->crc32_stream, NULL, zip->open_mode);
@@ -1204,7 +1260,7 @@ extern int32_t mz_zip_entry_read_open(void *handle, int16_t raw, const char *pas
int16_t compression_method = 0;
int32_t err = MZ_OK;
#if !defined(HAVE_CRYPT) && !defined(HAVE_AES)
#if defined(MZ_ZIP_NO_ENCRYPTION)
if (password != NULL)
return MZ_PARAM_ERROR;
#endif
@@ -1214,7 +1270,7 @@ extern int32_t mz_zip_entry_read_open(void *handle, int16_t raw, const char *pas
return MZ_PARAM_ERROR;
if (zip->entry_scanned == 0)
return MZ_PARAM_ERROR;
if ((zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED) && (password == NULL))
if ((zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED) && (password == NULL) && (!raw))
return MZ_PARAM_ERROR;
if (zip->file_info.disk_number == zip->disk_number_with_cd)
@@ -1222,7 +1278,7 @@ extern int32_t mz_zip_entry_read_open(void *handle, int16_t raw, const char *pas
else
mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, zip->file_info.disk_number);
err = mz_stream_seek(zip->stream, zip->file_info.disk_offset, MZ_SEEK_SET);
err = mz_stream_seek(zip->stream, zip->file_info.disk_offset + zip->disk_offset_shift, MZ_SEEK_SET);
if (err == MZ_OK)
err = mz_zip_entry_read_header(zip->stream, 1, &zip->local_file_info, zip->local_file_info_stream);
@@ -1230,14 +1286,17 @@ extern int32_t mz_zip_entry_read_open(void *handle, int16_t raw, const char *pas
if (raw)
compression_method = MZ_COMPRESS_METHOD_RAW;
#ifdef MZ_ZIP_NO_DECOMPRESSION
if (compression_method != MZ_COMPRESS_METHOD_RAW)
err = MZ_SUPPORT_ERROR;
#endif
if (err == MZ_OK)
err = mz_zip_entry_open_int(handle, compression_method, 0, password);
return err;
}
extern int32_t mz_zip_entry_write_open(void *handle, const mz_zip_file *file_info,
int16_t compress_level, const char *password)
extern int32_t mz_zip_entry_write_open(void *handle, const mz_zip_file *file_info, int16_t compress_level, const char *password)
{
mz_zip *zip = (mz_zip *)handle;
int64_t disk_number = 0;
@@ -1245,7 +1304,7 @@ extern int32_t mz_zip_entry_write_open(void *handle, const mz_zip_file *file_inf
int16_t compression_method = 0;
#if !defined(HAVE_CRYPT) && !defined(HAVE_AES)
#if defined(MZ_ZIP_NO_ENCRYPTION)
if (password != NULL)
return MZ_PARAM_ERROR;
#endif
@@ -1262,8 +1321,6 @@ extern int32_t mz_zip_entry_write_open(void *handle, const mz_zip_file *file_inf
memcpy(&zip->file_info, file_info, sizeof(mz_zip_file));
compression_method = zip->file_info.compression_method;
if (compress_level == 0)
compression_method = MZ_COMPRESS_METHOD_RAW;
if (compression_method == MZ_COMPRESS_METHOD_DEFLATE)
{
@@ -1283,11 +1340,9 @@ extern int32_t mz_zip_entry_write_open(void *handle, const mz_zip_file *file_inf
if (password != NULL)
zip->file_info.flag |= MZ_ZIP_FLAG_ENCRYPTED;
else
zip->file_info.flag &= ~MZ_ZIP_FLAG_ENCRYPTED;
if (mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number) == MZ_OK)
zip->file_info.disk_number = (uint32_t)disk_number;
mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number);
zip->file_info.disk_number = (uint32_t)disk_number;
zip->file_info.disk_offset = mz_stream_tell(zip->stream);
zip->file_info.crc = 0;
@@ -1298,6 +1353,13 @@ extern int32_t mz_zip_entry_write_open(void *handle, const mz_zip_file *file_inf
zip->file_info.aes_encryption_mode = MZ_AES_ENCRYPTION_MODE_256;
#endif
if ((compress_level == 0) || (mz_zip_attrib_is_dir(zip->file_info.external_fa, zip->file_info.version_madeby) == MZ_OK))
compression_method = MZ_COMPRESS_METHOD_RAW;
#ifdef MZ_ZIP_NO_COMPRESSION
if (compression_method != MZ_COMPRESS_METHOD_RAW)
err = MZ_SUPPORT_ERROR;
#endif
if (err == MZ_OK)
err = mz_zip_entry_write_header(zip->stream, 1, &zip->file_info);
if (err == MZ_OK)
@@ -1313,22 +1375,29 @@ extern int32_t mz_zip_entry_read(void *handle, void *buf, uint32_t len)
if (zip == NULL || zip->entry_opened == 0)
return MZ_PARAM_ERROR;
if (len > UINT16_MAX) // Zlib limitation
if (UINT_MAX == UINT16_MAX && len > UINT16_MAX) // Zlib limitation
return MZ_PARAM_ERROR;
if (len == 0)
return MZ_PARAM_ERROR;
if (zip->file_info.compressed_size == 0)
return 0;
// Read entire entry even if uncompressed_size = 0, otherwise
// aes encryption validation will fail if compressed_size > 0
read = mz_stream_read(zip->crc32_stream, buf, len);
if (read > 0)
zip->entry_read += read;
return read;
}
extern int32_t mz_zip_entry_write(void *handle, const void *buf, uint32_t len)
{
mz_zip *zip = (mz_zip *)handle;
int32_t written = 0;
if (zip == NULL || zip->entry_opened == 0)
return MZ_PARAM_ERROR;
return mz_stream_write(zip->crc32_stream, buf, len);
written = mz_stream_write(zip->crc32_stream, buf, len);
return written;
}
extern int32_t mz_zip_entry_get_info(void *handle, mz_zip_file **file_info)
@@ -1349,17 +1418,19 @@ extern int32_t mz_zip_entry_get_local_info(void *handle, mz_zip_file **local_fil
return MZ_OK;
}
extern int32_t mz_zip_entry_close_raw(void *handle, uint64_t uncompressed_size, uint32_t crc32)
static int32_t mz_zip_entry_close_int(void *handle, int16_t raw, uint64_t uncompressed_size, uint32_t crc32)
{
mz_zip *zip = (mz_zip *)handle;
uint64_t compressed_size = 0;
int64_t total_in = 0;
int32_t err = MZ_OK;
if (zip == NULL || zip->entry_opened == 0)
return MZ_PARAM_ERROR;
mz_stream_close(zip->compress_stream);
if (crc32 == 0)
if (raw == 0)
crc32 = mz_stream_crc32_get_value(zip->crc32_stream);
if ((zip->open_mode & MZ_OPEN_MODE_WRITE) == 0)
@@ -1369,7 +1440,9 @@ extern int32_t mz_zip_entry_close_raw(void *handle, uint64_t uncompressed_size,
if (zip->file_info.aes_version <= 0x0001)
#endif
{
if ((zip->entry_read > 0) && (zip->compression_method != MZ_COMPRESS_METHOD_RAW))
mz_stream_get_prop_int64(zip->crc32_stream, MZ_STREAM_PROP_TOTAL_IN, &total_in);
// If entire entry was not read this will fail
if ((total_in > 0) && (zip->compression_method != MZ_COMPRESS_METHOD_RAW))
{
if (crc32 != zip->file_info.crc)
err = MZ_CRC_ERROR;
@@ -1378,7 +1451,7 @@ extern int32_t mz_zip_entry_close_raw(void *handle, uint64_t uncompressed_size,
}
mz_stream_get_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_OUT, (int64_t *)&compressed_size);
if ((zip->compression_method != MZ_COMPRESS_METHOD_RAW) || (uncompressed_size == 0))
if (raw == 0)
mz_stream_get_prop_int64(zip->crc32_stream, MZ_STREAM_PROP_TOTAL_OUT, (int64_t *)&uncompressed_size);
if (zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED)
@@ -1404,16 +1477,16 @@ extern int32_t mz_zip_entry_close_raw(void *handle, uint64_t uncompressed_size,
if (err == MZ_OK)
{
if (zip->file_info.uncompressed_size <= UINT32_MAX)
err = mz_stream_write_uint64(zip->stream, compressed_size);
else
err = mz_stream_write_uint32(zip->stream, (uint32_t)compressed_size);
else
err = mz_stream_write_uint64(zip->stream, compressed_size);
}
if (err == MZ_OK)
{
if (zip->file_info.uncompressed_size <= UINT32_MAX)
err = mz_stream_write_uint64(zip->stream, uncompressed_size);
else
err = mz_stream_write_uint32(zip->stream, (uint32_t)uncompressed_size);
else
err = mz_stream_write_uint64(zip->stream, uncompressed_size);
}
}
@@ -1434,7 +1507,12 @@ extern int32_t mz_zip_entry_close_raw(void *handle, uint64_t uncompressed_size,
extern int32_t mz_zip_entry_close(void *handle)
{
return mz_zip_entry_close_raw(handle, 0, 0);
return mz_zip_entry_close_int(handle, 0, 0, 0);
}
extern int32_t mz_zip_entry_close_raw(void *handle, uint64_t uncompressed_size, uint32_t crc32)
{
return mz_zip_entry_close_int(handle, 1, uncompressed_size, crc32);
}
static int32_t mz_zip_goto_next_entry_int(void *handle)
@@ -1448,7 +1526,7 @@ static int32_t mz_zip_goto_next_entry_int(void *handle)
zip->entry_scanned = 0;
mz_stream_set_prop_int64(zip->cd_stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
err = mz_stream_seek(zip->cd_stream, zip->cd_current_pos, MZ_SEEK_SET);
if (err == MZ_OK)
err = mz_zip_entry_read_header(zip->cd_stream, 0, &zip->file_info, zip->file_info_stream);
@@ -1466,6 +1544,40 @@ extern int32_t mz_zip_get_number_entry(void *handle, int64_t *number_entry)
return MZ_OK;
}
extern int32_t mz_zip_get_disk_number_with_cd(void *handle, uint32_t *disk_number_with_cd)
{
mz_zip *zip = (mz_zip *)handle;
if (zip == NULL || disk_number_with_cd == NULL)
return MZ_PARAM_ERROR;
*disk_number_with_cd = zip->disk_number_with_cd;
return MZ_OK;
}
extern int64_t mz_zip_get_entry(void *handle)
{
mz_zip *zip = (mz_zip *)handle;
if (zip == NULL)
return MZ_PARAM_ERROR;
return zip->cd_current_pos;
}
extern int32_t mz_zip_goto_entry(void *handle, uint64_t cd_pos)
{
mz_zip *zip = (mz_zip *)handle;
if (zip == NULL)
return MZ_PARAM_ERROR;
if (cd_pos < zip->cd_start_pos || cd_pos > zip->cd_start_pos + zip->cd_size)
return MZ_PARAM_ERROR;
zip->cd_current_pos = cd_pos;
return mz_zip_goto_next_entry_int(handle);
}
extern int32_t mz_zip_goto_first_entry(void *handle)
{
mz_zip *zip = (mz_zip *)handle;
@@ -1519,10 +1631,30 @@ extern int32_t mz_zip_locate_entry(void *handle, const char *filename, mz_filena
/***************************************************************************/
int32_t mz_zip_attrib_is_dir(int32_t attributes, int32_t version_madeby)
{
int32_t host_system = (uint8_t)(version_madeby >> 8);
if (host_system == MZ_HOST_SYSTEM_MSDOS || host_system == MZ_HOST_SYSTEM_WINDOWS_NTFS)
{
if ((attributes & 0x10) == 0x10) // FILE_ATTRIBUTE_DIRECTORY
return MZ_OK;
}
else if (host_system == MZ_HOST_SYSTEM_UNIX || host_system == MZ_HOST_SYSTEM_OSX_DARWIN)
{
if ((attributes & 00170000) == 0040000) // S_ISDIR
return MZ_OK;
}
return MZ_EXIST_ERROR;
}
/***************************************************************************/
static int32_t mz_zip_invalid_date(const struct tm *ptm)
{
#define datevalue_in_range(min, max, value) ((min) <= (value) && (value) <= (max))
return (!datevalue_in_range(0, 207, ptm->tm_year) ||
return (!datevalue_in_range(0, 127 + 80, ptm->tm_year) || // 1980-based year, allow 80 extra
!datevalue_in_range(0, 11, ptm->tm_mon) ||
!datevalue_in_range(1, 31, ptm->tm_mday) ||
!datevalue_in_range(0, 23, ptm->tm_hour) ||
@@ -1572,9 +1704,13 @@ int32_t mz_zip_time_t_to_tm(time_t unix_time, struct tm *ptm)
struct tm *ltm = NULL;
if (ptm == NULL)
return MZ_PARAM_ERROR;
ltm = localtime(&unix_time);
ltm = localtime(&unix_time); // Returns a 1900-based year
if (ltm == NULL)
{
// Invalid date stored, so don't return it
memset(ptm, 0, sizeof(struct tm));
return MZ_INTERNAL_ERROR;
}
memcpy(ptm, ltm, sizeof(struct tm));
return MZ_OK;
}
@@ -1588,8 +1724,7 @@ uint32_t mz_zip_time_t_to_dos_date(time_t unix_time)
uint32_t mz_zip_tm_to_dosdate(const struct tm *ptm)
{
struct tm fixed_tm = { 0 };
uint32_t dos_date = 0;
struct tm fixed_tm;
// Years supported:
@@ -1601,12 +1736,12 @@ uint32_t mz_zip_tm_to_dosdate(const struct tm *ptm)
memcpy(&fixed_tm, ptm, sizeof(struct tm));
if (fixed_tm.tm_year >= 1980) // range [1980, 2107]
fixed_tm.tm_year -= 1980;
else if (fixed_tm.tm_year >= 80) // range [80, 99]
else if (fixed_tm.tm_year >= 80) // range [80, 207]
fixed_tm.tm_year -= 80;
else // range [00, 79]
fixed_tm.tm_year += 20;
if (mz_zip_invalid_date(ptm))
if (mz_zip_invalid_date(&fixed_tm))
return 0;
return (uint32_t)(((fixed_tm.tm_mday) + (32 * (fixed_tm.tm_mon + 1)) + (512 * fixed_tm.tm_year)) << 16) |
+25 -12
View File
@@ -1,8 +1,8 @@
/* mz_zip.h -- Zip manipulation
Version 2.2.4, November 15th, 2017
Version 2.3.9, July 26, 2018
part of the MiniZip project
Copyright (C) 2012-2017 Nathan Moinvaziri
Copyright (C) 2010-2018 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
Copyright (C) 2009-2010 Mathias Svensson
Modifications for Zip64 support
@@ -14,8 +14,8 @@
See the accompanying LICENSE file for the full text of the license.
*/
#ifndef _MZ_ZIP_H
#define _MZ_ZIP_H
#ifndef MZ_ZIP_H
#define MZ_ZIP_H
#include <stdint.h>
#include <time.h>
@@ -30,9 +30,9 @@ extern "C" {
typedef struct mz_zip_file_s
{
uint16_t version_madeby; // version made by
uint16_t version_madeby; // version made by
uint16_t version_needed; // version needed to extract
uint16_t flag; // general purpose bit flag
uint16_t flag; // general purpose bit flag
uint16_t compression_method; // compression method
time_t modified_date; // last modified date in unix time
time_t accessed_date; // last accessed date in unix time
@@ -47,10 +47,11 @@ typedef struct mz_zip_file_s
uint64_t disk_offset; // relative offset of local header
uint16_t internal_fa; // internal file attributes
uint32_t external_fa; // external file attributes
uint16_t zip64; // zip64 extension mode
char *filename; // filename string
uint8_t *extrafield; // extrafield data
char *comment; // comment string
const char *filename; // filename string
const uint8_t *extrafield; // extrafield data
const char *comment; // comment string
#ifdef HAVE_AES
uint16_t aes_version; // winzip aes extension if not 0
@@ -108,19 +109,31 @@ extern int32_t mz_zip_entry_close(void *handle);
extern int32_t mz_zip_get_number_entry(void *handle, int64_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);
// Get the the disk number containing the central directory record
extern 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);
// Go to specified entry in the zip file
extern int32_t mz_zip_goto_first_entry(void *handle);
// Go to the first entry in the zip file
// Go to the first entry in the zip file
extern 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,
extern int32_t mz_zip_locate_entry(void *handle, const char *filename,
mz_filename_compare_cb filename_compare_cb);
// Locate the file with the specified name in the zip file or MZ_END_LIST if not found
/***************************************************************************/
int32_t mz_zip_attrib_is_dir(int32_t attributes, int32_t version_madeby);
// Checks to see if the attribute is a directory based on platform
int32_t mz_zip_dosdate_to_tm(uint64_t dos_date, struct tm *ptm);
// Convert dos date/time format to struct tm
@@ -128,7 +141,7 @@ time_t mz_zip_dosdate_to_time_t(uint64_t dos_date);
// Convert dos date/time format to time_t
int32_t mz_zip_time_t_to_tm(time_t unix_time, struct tm *ptm);
// Convert time_t to time struct
// Convert time_t to time struct
uint32_t mz_zip_time_t_to_dos_date(time_t unix_time);
// Convert time_t to dos date/time format
-152
View File
@@ -1,152 +0,0 @@
/* mz_os.c -- System functions
Version 2.2.4, November 15th, 2017
part of the MiniZip project
Copyright (C) 2012-2017 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
Copyright (C) 1998-2010 Gilles Vollant
http://www.winimage.com/zLibDll/minizip.html
This program is distributed under the terms of the same license as zlib.
See the accompanying LICENSE file for the full text of the license.
*/
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "mz.h"
#include "mz_os.h"
#include "mz_strm.h"
#ifdef HAVE_LZMA
# include "mz_strm_lzma.h"
#endif
#ifdef HAVE_ZLIB
# include "mz_strm_zlib.h"
#endif
/***************************************************************************/
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 *)malloc(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;
}
}
free(current_dir);
return err;
}
int32_t mz_path_combine(char *path, const char *join, int32_t max_path)
{
int32_t path_len = 0;
if (path == NULL || join == NULL || max_path == 0)
return MZ_PARAM_ERROR;
path_len = strlen(path);
if (path_len == 0)
{
strncpy(path, join, max_path);
}
else
{
if (path[path_len - 1] != '\\' && path[path_len - 1] != '/')
strncat(path, "/", max_path - path_len - 1);
strncat(path, join, max_path - path_len);
}
return MZ_OK;
}
int32_t mz_get_file_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];
mz_stream_os_create(&stream);
err = mz_stream_os_open(stream, path, MZ_OPEN_MODE_READ);
mz_stream_crc32_create(&crc32_stream);
#ifdef HAVE_ZLIB
mz_stream_crc32_set_update_func(crc32_stream,
(mz_stream_crc32_update)mz_stream_zlib_get_crc32_update());
#elif defined(HAVE_LZMA)
mz_stream_crc32_set_update_func(crc32_stream,
(mz_stream_crc32_update)mz_stream_lzma_get_crc32_update());
#else
#error ZLIB or LZMA required for CRC32
#endif
mz_stream_crc32_open(crc32_stream, NULL, MZ_OPEN_MODE_READ);
mz_stream_set_base(crc32_stream, stream);
if (err == MZ_OK)
{
do
{
read = mz_stream_crc32_read(crc32_stream, buf, sizeof(buf));
if (read < 0)
{
err = read;
break;
}
}
while ((err == MZ_OK) && (read > 0));
mz_stream_os_close(stream);
}
mz_stream_crc32_close(crc32_stream);
*result_crc = mz_stream_crc32_get_value(crc32_stream);
mz_stream_crc32_delete(&crc32_stream);
mz_stream_os_delete(&stream);
return err;
}
-319
View File
@@ -1,319 +0,0 @@
/* mz_strm_crypt.c -- Code for traditional PKWARE encryption
Version 2.2.4, November 15th, 2017
part of the MiniZip project
Copyright (C) 2012-2017 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
Copyright (C) 1998-2005 Gilles Vollant
Modifications for Info-ZIP crypting
http://www.winimage.com/zLibDll/minizip.html
Copyright (C) 2003 Terry Thorsen
This code is a modified version of crypting code in Info-ZIP distribution
Copyright (C) 1990-2000 Info-ZIP. All rights reserved.
This program is distributed under the terms of the same license as zlib.
See the accompanying LICENSE file for the full text of the license.
This encryption code is a direct transcription of the algorithm from
Roger Schlafly, described by Phil Katz in the file appnote.txt. This
file (appnote.txt) is distributed with the PKZIP program (even in the
version without encryption capabilities).
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "zlib.h"
#include "mz.h"
#include "mz_os.h"
#include "mz_strm.h"
#include "mz_strm_crypt.h"
/***************************************************************************/
#define RAND_HEAD_LEN 12
/***************************************************************************/
mz_stream_vtbl mz_stream_crypt_vtbl = {
mz_stream_crypt_open,
mz_stream_crypt_is_open,
mz_stream_crypt_read,
mz_stream_crypt_write,
mz_stream_crypt_tell,
mz_stream_crypt_seek,
mz_stream_crypt_close,
mz_stream_crypt_error,
mz_stream_crypt_create,
mz_stream_crypt_delete,
mz_stream_crypt_get_prop_int64
};
/***************************************************************************/
typedef struct mz_stream_crypt_s {
mz_stream stream;
int32_t error;
int16_t initialized;
uint8_t buffer[INT16_MAX];
int64_t total_in;
int64_t total_out;
uint32_t keys[3]; // keys defining the pseudo-random sequence
const z_crc_t *crc_32_tab;
uint8_t verify1;
uint8_t verify2;
const char *password;
} mz_stream_crypt;
/***************************************************************************/
#define zdecode(keys,crc_32_tab,c) \
(mz_stream_crypt_update_keys(keys,crc_32_tab, c ^= mz_stream_crypt_decrypt_byte(keys)))
#define zencode(keys,crc_32_tab,c,t) \
(t = mz_stream_crypt_decrypt_byte(keys), mz_stream_crypt_update_keys(keys,crc_32_tab,c), t^(c))
/***************************************************************************/
uint8_t mz_stream_crypt_decrypt_byte(uint32_t *keys)
{
unsigned temp; /* POTENTIAL BUG: temp*(temp^1) may overflow in an
* unpredictable manner on 16-bit systems; not a problem
* with any known compiler so far, though */
temp = ((uint32_t)(*(keys+2)) & 0xffff) | 2;
return (uint8_t)(((temp * (temp ^ 1)) >> 8) & 0xff);
}
uint8_t mz_stream_crypt_update_keys(uint32_t *keys, const z_crc_t *crc_32_tab, int32_t c)
{
#define CRC32(c, b) ((*(crc_32_tab+(((uint32_t)(c) ^ (b)) & 0xff))) ^ ((c) >> 8))
(*(keys+0)) = (uint32_t)CRC32((*(keys+0)), c);
(*(keys+1)) += (*(keys+0)) & 0xff;
(*(keys+1)) = (*(keys+1)) * 134775813L + 1;
{
register int32_t keyshift = (int32_t)((*(keys + 1)) >> 24);
(*(keys+2)) = (uint32_t)CRC32((*(keys+2)), keyshift);
}
return (uint8_t)c;
}
void mz_stream_crypt_init_keys(const char *password, uint32_t *keys, const z_crc_t *crc_32_tab)
{
*(keys+0) = 305419896L;
*(keys+1) = 591751049L;
*(keys+2) = 878082192L;
while (*password != 0)
{
mz_stream_crypt_update_keys(keys, crc_32_tab, *password);
password += 1;
}
}
/***************************************************************************/
int32_t mz_stream_crypt_open(void *stream, const char *path, int32_t mode)
{
mz_stream_crypt *crypt = (mz_stream_crypt *)stream;
uint16_t t = 0;
int16_t i = 0;
uint8_t header[RAND_HEAD_LEN];
const char *password = path;
crypt->total_in = 0;
crypt->total_out = 0;
crypt->initialized = 0;
if (mz_stream_is_open(crypt->stream.base) != MZ_OK)
return MZ_STREAM_ERROR;
if (password == NULL)
password = crypt->password;
if (password == NULL)
return MZ_STREAM_ERROR;
crypt->crc_32_tab = get_crc_table();
if (crypt->crc_32_tab == NULL)
return MZ_STREAM_ERROR;
mz_stream_crypt_init_keys(password, crypt->keys, crypt->crc_32_tab);
if (mode & MZ_OPEN_MODE_WRITE)
{
// First generate RAND_HEAD_LEN - 2 random bytes.
mz_os_rand(header, RAND_HEAD_LEN - 2);
// Encrypt random header (last two bytes is high word of crc)
for (i = 0; i < RAND_HEAD_LEN - 2; i++)
header[i] = (uint8_t)zencode(crypt->keys, crypt->crc_32_tab, header[i], t);
header[i++] = (uint8_t)zencode(crypt->keys, crypt->crc_32_tab, crypt->verify1, t);
header[i++] = (uint8_t)zencode(crypt->keys, crypt->crc_32_tab, crypt->verify2, t);
if (mz_stream_write(crypt->stream.base, header, RAND_HEAD_LEN) != RAND_HEAD_LEN)
return MZ_STREAM_ERROR;
crypt->total_out += RAND_HEAD_LEN;
}
else if (mode & MZ_OPEN_MODE_READ)
{
if (mz_stream_read(crypt->stream.base, header, RAND_HEAD_LEN) != RAND_HEAD_LEN)
return MZ_STREAM_ERROR;
for (i = 0; i < RAND_HEAD_LEN - 2; i++)
header[i] = (uint8_t)zdecode(crypt->keys, crypt->crc_32_tab, header[i]);
crypt->verify1 = (uint8_t)zdecode(crypt->keys, crypt->crc_32_tab, header[i++]);
crypt->verify2 = (uint8_t)zdecode(crypt->keys, crypt->crc_32_tab, header[i++]);
crypt->total_in += RAND_HEAD_LEN;
}
crypt->initialized = 1;
return MZ_OK;
}
int32_t mz_stream_crypt_is_open(void *stream)
{
mz_stream_crypt *crypt = (mz_stream_crypt *)stream;
if (crypt->initialized == 0)
return MZ_STREAM_ERROR;
return MZ_OK;
}
int32_t mz_stream_crypt_read(void *stream, void *buf, int32_t size)
{
mz_stream_crypt *crypt = (mz_stream_crypt *)stream;
uint8_t *buf_ptr = (uint8_t *)buf;
uint32_t read = 0;
uint32_t i = 0;
read = mz_stream_read(crypt->stream.base, buf, size);
for (i = 0; i < read; i++)
buf_ptr[i] = (uint8_t)zdecode(crypt->keys, crypt->crc_32_tab, buf_ptr[i]);
crypt->total_in += read;
return read;
}
int32_t mz_stream_crypt_write(void *stream, const void *buf, int32_t size)
{
mz_stream_crypt *crypt = (mz_stream_crypt *)stream;
uint8_t *buf_ptr = (uint8_t *)buf;
uint32_t written = 0;
uint16_t t = 0;
int32_t i = 0;
if (size > sizeof(crypt->buffer))
return MZ_STREAM_ERROR;
for (i = 0; i < size; i++)
crypt->buffer[i] = (uint8_t)zencode(crypt->keys, crypt->crc_32_tab, buf_ptr[i], t);
written = mz_stream_write(crypt->stream.base, crypt->buffer, size);
if (written > 0)
crypt->total_out += written;
return written;
}
int64_t mz_stream_crypt_tell(void *stream)
{
mz_stream_crypt *crypt = (mz_stream_crypt *)stream;
return mz_stream_tell(crypt->stream.base);
}
int32_t mz_stream_crypt_seek(void *stream, int64_t offset, int32_t origin)
{
mz_stream_crypt *crypt = (mz_stream_crypt *)stream;
return mz_stream_seek(crypt->stream.base, offset, origin);
}
int32_t mz_stream_crypt_close(void *stream)
{
mz_stream_crypt *crypt = (mz_stream_crypt *)stream;
crypt->initialized = 0;
return MZ_OK;
}
int32_t mz_stream_crypt_error(void *stream)
{
mz_stream_crypt *crypt = (mz_stream_crypt *)stream;
return crypt->error;
}
void mz_stream_crypt_set_password(void *stream, const char *password)
{
mz_stream_crypt *crypt = (mz_stream_crypt *)stream;
crypt->password = password;
}
void mz_stream_crypt_set_verify(void *stream, uint8_t verify1, uint8_t verify2)
{
mz_stream_crypt *crypt = (mz_stream_crypt *)stream;
crypt->verify1 = verify1;
crypt->verify2 = verify2;
}
void mz_stream_crypt_get_verify(void *stream, uint8_t *verify1, uint8_t *verify2)
{
mz_stream_crypt *crypt = (mz_stream_crypt *)stream;
*verify1 = crypt->verify1;
*verify2 = crypt->verify2;
}
int32_t mz_stream_crypt_get_prop_int64(void *stream, int32_t prop, int64_t *value)
{
mz_stream_crypt *crypt = (mz_stream_crypt *)stream;
switch (prop)
{
case MZ_STREAM_PROP_TOTAL_IN:
*value = crypt->total_in;
return MZ_OK;
case MZ_STREAM_PROP_TOTAL_OUT:
*value = crypt->total_out;
return MZ_OK;
}
return MZ_EXIST_ERROR;
}
void *mz_stream_crypt_create(void **stream)
{
mz_stream_crypt *crypt = NULL;
crypt = (mz_stream_crypt *)malloc(sizeof(mz_stream_crypt));
if (crypt != NULL)
{
memset(crypt, 0, sizeof(mz_stream_crypt));
crypt->stream.vtbl = &mz_stream_crypt_vtbl;
}
if (stream != NULL)
*stream = crypt;
return crypt;
}
void mz_stream_crypt_delete(void **stream)
{
mz_stream_crypt *crypt = NULL;
if (stream == NULL)
return;
crypt = (mz_stream_crypt *)*stream;
if (crypt != NULL)
free(crypt);
*stream = NULL;
}
void *mz_stream_crypt_get_interface(void)
{
return (void *)&mz_stream_crypt_vtbl;
}
-56
View File
@@ -1,56 +0,0 @@
/* mz_strm_crypt.h -- Code for traditional PKWARE encryption
Version 2.2.4, November 15th, 2017
part of the MiniZip project
Copyright (C) 2012-2017 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
Copyright (C) 1998-2005 Gilles Vollant
Modifications for Info-ZIP crypting
http://www.winimage.com/zLibDll/minizip.html
Copyright (C) 2003 Terry Thorsen
This code is a modified version of crypting code in Info-ZIP distribution
Copyright (C) 1990-2000 Info-ZIP. All rights reserved.
This program is distributed under the terms of the same license as zlib.
See the accompanying LICENSE file for the full text of the license.
*/
#ifndef _MZ_STREAM_CRYPT_H
#define _MZ_STREAM_CRYPT_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/***************************************************************************/
int32_t mz_stream_crypt_open(void *stream, const char *filename, int32_t mode);
int32_t mz_stream_crypt_is_open(void *stream);
int32_t mz_stream_crypt_read(void *stream, void *buf, int32_t size);
int32_t mz_stream_crypt_write(void *stream, const void *buf, int32_t size);
int64_t mz_stream_crypt_tell(void *stream);
int32_t mz_stream_crypt_seek(void *stream, int64_t offset, int32_t origin);
int32_t mz_stream_crypt_close(void *stream);
int32_t mz_stream_crypt_error(void *stream);
void mz_stream_crypt_set_password(void *stream, const char *password);
void mz_stream_crypt_set_verify(void *stream, uint8_t verify1, uint8_t verify2);
void mz_stream_crypt_get_verify(void *stream, uint8_t *verify1, uint8_t *verify2);
int32_t mz_stream_crypt_get_prop_int64(void *stream, int32_t prop, int64_t *value);
void* mz_stream_crypt_create(void **stream);
void mz_stream_crypt_delete(void **stream);
void* mz_stream_crypt_get_interface(void);
/***************************************************************************/
#ifdef __cplusplus
}
#endif
#endif
View File
+72 -7
View File
@@ -9,14 +9,72 @@
#include "mz_os.h"
#include "mz_strm.h"
#include "mz_strm_mem.h"
#ifdef HAVE_BZIP
#include "mz_strm_bzip.h"
#include "mz_strm_crypt.h"
#endif
#ifdef HAVE_PKCRYPT
#include "mz_strm_pkcrypt.h"
#endif
#ifdef HAVE_AES
#include "mz_strm_aes.h"
#endif
#ifdef HAVE_ZLIB
#include "mz_strm_zlib.h"
#endif
#include "mz_zip.h"
/***************************************************************************/
void test_path_resolve(void)
{
char output[256];
int32_t ok = 0;
memset(output, 'z', sizeof(output));
mz_path_resolve("c:\\test\\.", output, sizeof(output));
ok = (strcmp(output, "c:\\test\\") == 0);
memset(output, 'z', sizeof(output));
mz_path_resolve("c:\\test\\.\\", output, sizeof(output));
ok = (strcmp(output, "c:\\test\\") == 0);
memset(output, 'z', sizeof(output));
mz_path_resolve("c:\\test\\..", output, sizeof(output));
ok = (strcmp(output, "c:\\") == 0);
memset(output, 'z', sizeof(output));
mz_path_resolve("c:\\test\\..\\", output, sizeof(output));
ok = (strcmp(output, "c:\\") == 0);
memset(output, 'z', sizeof(output));
mz_path_resolve("c:\\test\\.\\..", output, sizeof(output));
ok = (strcmp(output, "c:\\") == 0);
memset(output, 'z', sizeof(output));
mz_path_resolve("c:\\test\\.\\\\..", output, sizeof(output));
ok = (strcmp(output, "c:\\") == 0);
memset(output, 'z', sizeof(output));
mz_path_resolve(".", output, sizeof(output));
ok = (strcmp(output, ".") == 0);
memset(output, 'z', sizeof(output));
mz_path_resolve(".\\", output, sizeof(output));
ok = (strcmp(output, "") == 0);
memset(output, 'z', sizeof(output));
mz_path_resolve("..", output, sizeof(output));
ok = (strcmp(output, "") == 0);
memset(output, 'z', sizeof(output));
mz_path_resolve("..\\", output, sizeof(output));
ok = (strcmp(output, "") == 0);
memset(output, 'z', sizeof(output));
mz_path_resolve("c:\\test\\123\\.\\abc.txt", output, sizeof(output));
ok = (strcmp(output, "c:\\test\\123\\abc.txt") == 0);
memset(output, 'z', sizeof(output));
mz_path_resolve("c:\\test\\123\\..\\abc.txt", output, sizeof(output));
ok = (strcmp(output, "c:\\test\\abc.txt") == 0);
memset(output, 'z', sizeof(output));
mz_path_resolve("c:\\test\\123\\..\\..\\abc.txt", output, sizeof(output));
ok = (strcmp(output, "c:\\abc.txt") == 0);
memset(output, 'z', sizeof(output));
mz_path_resolve("c:\\test\\123\\..\\..\\..\\abc.txt", output, sizeof(output));
ok = (strcmp(output, "abc.txt") == 0);
memset(output, 'z', sizeof(output));
}
void test_encrypt(char *method, mz_stream_create_cb crypt_create, char *password)
{
char buf[UINT16_MAX];
@@ -151,7 +209,7 @@ void test_compress(char *method, mz_stream_create_cb create_compress)
mz_stream_delete(&deflate_stream);
printf("%s compressed from %d to %d\n", filename, (uint32_t)total_in, (uint32_t)total_out);
printf("%s compressed from %u to %u\n", filename, (uint32_t)total_in, (uint32_t)total_out);
mz_stream_os_close(out_stream);
}
@@ -175,7 +233,7 @@ void test_compress(char *method, mz_stream_create_cb create_compress)
mz_stream_os_close(in_stream);
printf("%s uncompressed from %d to %d\n", filename, (uint32_t)total_in, (uint32_t)total_out);
printf("%s uncompressed from %u to %u\n", filename, (uint32_t)total_in, (uint32_t)total_out);
}
mz_stream_os_delete(&in_stream);
@@ -206,25 +264,30 @@ void test_compress(char *method, mz_stream_create_cb create_compress)
/***************************************************************************/
#ifdef HAVE_AES
void test_aes()
{
test_encrypt("aes", mz_stream_aes_create, "hello");
}
#endif
#ifdef HAVE_PKCRYPT
void test_crypt()
{
test_encrypt("crypt", mz_stream_crypt_create, "hello");
test_encrypt("pkcrypt", mz_stream_pkcrypt_create, "hello");
}
#endif
#ifdef HAVE_ZLIB
void test_zlib()
{
test_compress("zlib", mz_stream_zlib_create);
}
#endif
#ifdef HAVE_BZIP
void test_bzip()
{
test_compress("bzip", mz_stream_bzip_create);
}
#endif
/***************************************************************************/
@@ -262,7 +325,9 @@ void test_zip_mem()
file_info.compression_method = MZ_COMPRESS_METHOD_DEFLATE;
file_info.filename = text_name;
file_info.uncompressed_size = text_size;
#ifdef HAVE_AES
file_info.aes_version = MZ_AES_VERSION;
#endif
err = mz_zip_entry_write_open(zip_handle, &file_info, MZ_COMPRESS_LEVEL_DEFAULT, password);
if (err == MZ_OK)
+1
View File
@@ -199,3 +199,4 @@ sfx_zip_test()
compression_method_tests()
encryption_tests()
compression_method_tests('Disk Span', '-k 1024', '')
compression_method_tests('Buffered', '-u', '-u')