Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
826113fd6c | ||
|
|
4d5a9a66c9 | ||
|
|
25634010c9 | ||
|
|
6cda2a7e76 | ||
|
|
7c2005dcf0 | ||
|
|
cb5798a6b9 | ||
|
|
cdef32b909 | ||
|
|
8721aaf7ec | ||
|
|
d15b704c85 | ||
|
|
1b87a5a358 | ||
|
|
e52a3b5732 | ||
|
|
e63d231fa5 | ||
|
|
24280b68e8 | ||
|
|
888e8a7324 | ||
|
|
c51f2515d6 | ||
|
|
adfe53f969 | ||
|
|
f164b6dd37 | ||
|
|
02760f6257 | ||
|
|
8541317c10 | ||
|
|
3223d8d378 | ||
|
|
bdf88e7425 | ||
|
|
6cdd3f54da | ||
|
|
4337bcc752 | ||
|
|
0d396799c1 | ||
|
|
0b14aeddde | ||
|
|
1b2b32c8b8 | ||
|
|
66e1631e4b | ||
|
|
7ff6b8aa9f | ||
|
|
b64f4f1228 | ||
|
|
5f381b232d | ||
|
|
f95e3683ee | ||
|
|
0d9d0df40d | ||
|
|
20e4d587ca | ||
|
|
3aaf5bc895 | ||
|
|
623a844814 | ||
|
|
c9add13ae6 | ||
|
|
fcd85ef064 | ||
|
|
54dffe95e9 | ||
|
|
82d4fa0dd6 | ||
|
|
eff49b88ce | ||
|
|
04d821edef | ||
|
|
7eb65ad2ae | ||
|
|
f79ec42ad3 | ||
|
|
f6557afd71 | ||
|
|
10489ead98 | ||
|
|
d021ab867b | ||
|
|
90a8b12562 | ||
|
|
6209991d6b | ||
|
|
3caa105cef | ||
|
|
0bff08751b | ||
|
|
ea5e45dfe2 | ||
|
|
1e21dc3a9b | ||
|
|
c73ef6e69b | ||
|
|
57942ad197 | ||
|
|
974410131c | ||
|
|
848837b78b | ||
|
|
dc63e1ebcf | ||
|
|
8b5826b3bf | ||
|
|
f3efcb6227 | ||
|
|
43c01101bf | ||
|
|
d1e3877b4d | ||
|
|
a221da4ae0 | ||
|
|
703b3df3ef | ||
|
|
05e03cad8e | ||
|
|
71027dc8fb | ||
|
|
4ae469a0a4 | ||
|
|
80b56a8a5a | ||
|
|
a2bb08c816 | ||
|
|
619b2b3c8b | ||
|
|
fac97955d1 | ||
|
|
ea66a05adc | ||
|
|
dd31249b5a | ||
|
|
37b4da07f1 | ||
|
|
cb9c40c23d | ||
|
|
cce11ba090 | ||
|
|
6e2f181c18 | ||
|
|
e464430433 | ||
|
|
f5b7ed7bfe | ||
|
|
72a099dae1 | ||
|
|
13d9e7666c | ||
|
|
fea1232fa6 | ||
|
|
2a172f312d | ||
|
|
3c000cf5fb | ||
|
|
915c513abe | ||
|
|
2ed269e000 | ||
|
|
217cd65995 | ||
|
|
bc21a7456b | ||
|
|
94f8c19db4 | ||
|
|
80d30e6bf8 | ||
|
|
566dfe0dcd | ||
|
|
9ad08e1ec6 | ||
|
|
1770d3554e | ||
|
|
13ae3ab2bc | ||
|
|
e300b7f3a2 | ||
|
|
f44a0e089c | ||
|
|
5b3039af0e | ||
|
|
d0ac17fd60 | ||
|
|
156a64730b | ||
|
|
8cc0800876 | ||
|
|
9d6d894153 | ||
|
|
6cabcc0523 | ||
|
|
6a415925fd | ||
|
|
bee75d5f3e | ||
|
|
7dce6de14f | ||
|
|
6bde034147 | ||
|
|
c714ef60a4 | ||
|
|
f97c9f48b2 | ||
|
|
d2dc11ca57 | ||
|
|
d45af6d6e9 | ||
|
|
bf7776206e | ||
|
|
47a2cd7b99 | ||
|
|
9fb8290289 | ||
|
|
e5a5617a7c | ||
|
|
5352f868bb | ||
|
|
d9969b545a | ||
|
|
8a7383a210 | ||
|
|
648f596a99 | ||
|
|
8875d288db | ||
|
|
8cb7df741a | ||
|
|
7404279f66 | ||
|
|
5f09188ca9 | ||
|
|
21a3102db4 | ||
|
|
73025eb9db | ||
|
|
a5e06a7bcf | ||
|
|
eef6ffc75d | ||
|
|
e1f68fca9c | ||
|
|
fa620e473f | ||
|
|
413822a040 | ||
|
|
a7fecd88dd | ||
|
|
c565fa8f88 | ||
|
|
c5da4987c8 | ||
|
|
d64e079092 | ||
|
|
f03cdc7604 | ||
|
|
5afeb430ee | ||
|
|
b4c953f36a | ||
|
|
9dea6f2495 | ||
|
|
544fc3b97b | ||
|
|
73660f9c98 | ||
|
|
da95d6e8b7 | ||
|
|
ff6f2a1db6 | ||
|
|
5e6bac5bcc | ||
|
|
09304a6184 | ||
|
|
004e7d3327 | ||
|
|
fe145502a6 | ||
|
|
904c408575 | ||
|
|
98047626d7 | ||
|
|
e6b6616575 | ||
|
|
c7e852e33c | ||
|
|
8c272e0ff8 | ||
|
|
17bad4cf74 | ||
|
|
3b3840b7b2 | ||
|
|
0900548d8b | ||
|
|
2bb21d769c | ||
|
|
2630f5f4dc | ||
|
|
15dbc445d8 | ||
|
|
9098b6c823 | ||
|
|
ed403113a2 | ||
|
|
f6fe93b2a8 | ||
|
|
e5ce077bf3 | ||
|
|
fe7983319a | ||
|
|
ca8059ef61 | ||
|
|
15570e0794 | ||
|
|
a247bfaa68 | ||
|
|
682503ce78 | ||
|
|
709c1f05ce | ||
|
|
0113ad8330 | ||
|
|
9ba3525779 | ||
|
|
9980d8ac3a | ||
|
|
a0f617cd8a | ||
|
|
9747212a14 | ||
|
|
825bfb8fbf | ||
|
|
0863941638 | ||
|
|
5469018dbc | ||
|
|
dacd44383d | ||
|
|
e80d8c7d22 | ||
|
|
72538543aa | ||
|
|
980b3ab699 | ||
|
|
e09ea0e515 | ||
|
|
0b1e147394 | ||
|
|
9117655893 | ||
|
|
ca505c0fde | ||
|
|
bb96c73f7b | ||
|
|
fa25b2ecc4 | ||
|
|
0e7b9fc3c8 | ||
|
|
37842b678d | ||
|
|
dd54b153c0 | ||
|
|
0db9d70659 | ||
|
|
09ddae6514 | ||
|
|
0457003448 | ||
|
|
1b3ce71663 | ||
|
|
777db81e88 | ||
|
|
44a2d152de | ||
|
|
165a40221d | ||
|
|
ab8507608e | ||
|
|
67d17c0442 | ||
|
|
e7215072a0 | ||
|
|
7932c261dd | ||
|
|
85d36c5e83 | ||
|
|
75c064bdf6 | ||
|
|
a1e0db1d09 | ||
|
|
6f40c04998 | ||
|
|
6d492ea3e7 | ||
|
|
a068933451 | ||
|
|
eb923e7d2b | ||
|
|
5d9f95fca1 | ||
|
|
e52f092b08 | ||
|
|
353586a4c4 | ||
|
|
7ea3653e77 | ||
|
|
0238f0041e | ||
|
|
242ec32883 | ||
|
|
96520fe778 | ||
|
|
8e61a208d5 | ||
|
|
8e47b249fd | ||
|
|
fc4a57f14c | ||
|
|
611adce2b3 | ||
|
|
9eaadb4d3c | ||
|
|
de98134362 | ||
|
|
ce2c6c3521 | ||
|
|
eb60ab3493 | ||
|
|
881f2d2ea3 | ||
|
|
1872771139 | ||
|
|
f307d59c11 | ||
|
|
f1698d631f | ||
|
|
59ba13b5a9 | ||
|
|
d27969a853 | ||
|
|
7b6f04e955 | ||
|
|
bc0a9005f2 | ||
|
|
c4d130878f | ||
|
|
315ca492fd | ||
|
|
a594982712 | ||
|
|
d4ed216bd3 | ||
|
|
8935dd19db | ||
|
|
351b14e512 | ||
|
|
7188209f03 | ||
|
|
95047d3c90 | ||
|
|
359e8e1017 | ||
|
|
e7df0f7341 | ||
|
|
da1ab3dab1 | ||
|
|
abc29e4429 | ||
|
|
b8300ce0ea | ||
|
|
638f31ff41 | ||
|
|
a0d1877d0e | ||
|
|
3eaa5560bf | ||
|
|
d4c4d4f5b1 | ||
|
|
d79fb39d4b | ||
|
|
a2e89bf85e | ||
|
|
e27af27b6e | ||
|
|
72cc7015a2 | ||
|
|
a2bd3c6d95 | ||
|
|
dcd9b72bf9 | ||
|
|
d2aa2277d9 | ||
|
|
da4b58f6e5 | ||
|
|
b994caeab9 | ||
|
|
0b6059fc53 | ||
|
|
4b8e4b372c | ||
|
|
bb3d26efb7 | ||
|
|
74a6a21e56 | ||
|
|
2b3cd16b6f | ||
|
|
79cfab094d | ||
|
|
0e2201a5e1 | ||
|
|
e04106b734 | ||
|
|
b9dbacae60 | ||
|
|
590e5909b3 | ||
|
|
960a6e113c | ||
|
|
01bc6016fb | ||
|
|
5f01092239 | ||
|
|
444596646e | ||
|
|
c98dec4291 | ||
|
|
ca014e6f37 | ||
|
|
f5cc4c0355 | ||
|
|
00520cd631 | ||
|
|
c56d53156e | ||
|
|
05aa0f2a07 | ||
|
|
829ffb5cce | ||
|
|
b635b7db5a | ||
|
|
9f863b35ca | ||
|
|
de09ac796f | ||
|
|
cfa2e604f6 | ||
|
|
a8644a1de8 | ||
|
|
9a170d4d60 | ||
|
|
77687f3a42 | ||
|
|
a64c919e0b | ||
|
|
9eb113dee7 | ||
|
|
d08e1561d9 | ||
|
|
26308b244e | ||
|
|
0e4fe82acb | ||
|
|
fa146ad6bd | ||
|
|
0f8d91101b | ||
|
|
a6d04a7baa | ||
|
|
00ad03daf0 | ||
|
|
2b43158414 | ||
|
|
0e1fd3ee8b | ||
|
|
c65c7a928f | ||
|
|
65a87024d4 | ||
|
|
c599dd60f4 | ||
|
|
c2d5c47b51 | ||
|
|
89d6967afc | ||
|
|
7c29c16575 | ||
|
|
7679d219a0 | ||
|
|
298126af7f | ||
|
|
7f6f08acfc | ||
|
|
c83e4da20d | ||
|
|
b5c2f01c31 | ||
|
|
54dc69af1c | ||
|
|
5f2bf575df | ||
|
|
c9addbe56d | ||
|
|
155b3f5e42 | ||
|
|
32a9d34295 | ||
|
|
a8f4a09de6 | ||
|
|
e824da8453 | ||
|
|
51bf61d04e | ||
|
|
f55a3c5a40 | ||
|
|
546607c8e4 | ||
|
|
4818d8efe8 | ||
|
|
a2a37040c7 | ||
|
|
0fac5e72ec | ||
|
|
d195fd3a59 | ||
|
|
f23af14199 | ||
|
|
1149bafee3 | ||
|
|
aa311f349b | ||
|
|
6f32e35278 | ||
|
|
44a14426dc | ||
|
|
eadf3eb181 | ||
|
|
a982abf876 | ||
|
|
3719965652 | ||
|
|
91c028d276 | ||
|
|
4042763729 | ||
|
|
a6d1f6692e | ||
|
|
4d34203968 | ||
|
|
93c13564b8 | ||
|
|
f9ee9d7f08 | ||
|
|
7332bd6002 | ||
|
|
9645f4638c | ||
|
|
8d128a7c63 | ||
|
|
627993ef07 | ||
|
|
4cfdd5dd83 | ||
|
|
697e995d89 | ||
|
|
16d9f47a4f | ||
|
|
6ac2ff4ec2 | ||
|
|
e099a2a8f7 | ||
|
|
163b317ad6 | ||
|
|
446d062b3c | ||
|
|
6781995724 | ||
|
|
b3bf29ab02 | ||
|
|
7a0a62dc9c | ||
|
|
d50e91aafe | ||
|
|
ae724eff61 | ||
|
|
4bdb436e8e | ||
|
|
2ecd8a0b2a | ||
|
|
f873eccdd2 | ||
|
|
9593ca4fc5 | ||
|
|
2e4a85e36f | ||
|
|
ee614ff3ce | ||
|
|
6b12c36538 | ||
|
|
a4a5cf66aa | ||
|
|
cfcbccf67e | ||
|
|
eb80cd9fa4 | ||
|
|
9e2b2456dc | ||
|
|
ffa830f684 | ||
|
|
f65b9fecae | ||
|
|
f76c04be40 | ||
|
|
ebb01ce07e | ||
|
|
b0a005644d | ||
|
|
d7814e9b8d | ||
|
|
50f3252d2c | ||
|
|
adbbc68fa4 | ||
|
|
34e2ab5065 | ||
|
|
b47b41b8df | ||
|
|
155f8b52b2 | ||
|
|
5d3b5c593d | ||
|
|
e3958d9b46 | ||
|
|
957e5f92e4 | ||
|
|
184f4cbde1 | ||
|
|
8871f6c0f1 | ||
|
|
fa8105b657 | ||
|
|
2cf91c09ce | ||
|
|
9eecff3140 | ||
|
|
77dcb77506 | ||
|
|
ef776a31e1 | ||
|
|
b70b824b19 | ||
|
|
0a9282d2f0 | ||
|
|
6be5ec8163 | ||
|
|
5d4c61fdd5 | ||
|
|
4bcaf521d7 | ||
|
|
957f2e9976 | ||
|
|
dd808239dd | ||
|
|
7bdfbf4699 | ||
|
|
4a7723357e | ||
|
|
4925f31e28 | ||
|
|
669f5a3086 | ||
|
|
30e859be0b | ||
|
|
5b204622cc | ||
|
|
b07ab3e9b9 | ||
|
|
3dc4334d38 | ||
|
|
ca30682447 | ||
|
|
0e5c9dcdf4 | ||
|
|
ea99a46b6d | ||
|
|
23bc9fd943 | ||
|
|
cfcb729f79 | ||
|
|
51521bddc8 | ||
|
|
41331dd3ec | ||
|
|
9c155991d7 | ||
|
|
43e787bd88 | ||
|
|
3909c23817 | ||
|
|
c0d3dbf537 | ||
|
|
0388124aad | ||
|
|
e1aad8c445 | ||
|
|
b5fb12503d | ||
|
|
bd2f162009 | ||
|
|
408940f16c | ||
|
|
a710bd714a | ||
|
|
7a3b698578 | ||
|
|
e275fcf017 | ||
|
|
0e2e1a0b05 | ||
|
|
3431a6635b | ||
|
|
521a42a4c3 | ||
|
|
a4b83e8e95 | ||
|
|
121e088a7d | ||
|
|
46f714348e | ||
|
|
a56a08cf79 | ||
|
|
8a50b4b5df | ||
|
|
37ceb026db | ||
|
|
586300a787 | ||
|
|
92c8d77cfb | ||
|
|
c546a57f90 | ||
|
|
5244fc00c4 | ||
|
|
4c76400cce | ||
|
|
240b3b6709 | ||
|
|
e54582384a | ||
|
|
3ad1a92279 | ||
|
|
447dd06750 | ||
|
|
4827f71a08 | ||
|
|
bb3b75bc09 | ||
|
|
b7bd0421c5 | ||
|
|
9a3ae75a48 | ||
|
|
120bea1a6f | ||
|
|
4a41245099 | ||
|
|
0f0535fcaa | ||
|
|
8d8deb4e5d | ||
|
|
83bce6e82a | ||
|
|
739f03f951 | ||
|
|
7583be518c | ||
|
|
0619c3e6c4 | ||
|
|
e33916b608 | ||
|
|
596f19f79e | ||
|
|
2884e67a1a | ||
|
|
ae5740b19c | ||
|
|
b14ded7d01 | ||
|
|
914049d19c | ||
|
|
915b82e95b | ||
|
|
0f09a00810 | ||
|
|
f5f23217b3 | ||
|
|
5d362f43eb | ||
|
|
ce26dba5ba | ||
|
|
ad8e3dd4b1 | ||
|
|
53e1d650e5 | ||
|
|
bcd64ca3b8 | ||
|
|
078f20290f | ||
|
|
9c98a6b180 | ||
|
|
a30ff41e9d | ||
|
|
a43eb98099 | ||
|
|
8d8a987eea | ||
|
|
c5f0063c75 | ||
|
|
d57eb5de40 | ||
|
|
672dc834f1 | ||
|
|
5e8f4d7d88 | ||
|
|
68a48aa20e | ||
|
|
b5ac94fd24 | ||
|
|
b4020169b7 | ||
|
|
b8f38540c2 | ||
|
|
d65cd2ea9d | ||
|
|
6cd3054608 | ||
|
|
f44494aad2 | ||
|
|
7c5ccae39c | ||
|
|
82df346497 | ||
|
|
225c627f7d | ||
|
|
db8158a5cc | ||
|
|
62090966b1 | ||
|
|
dec6a65a64 | ||
|
|
7d7b1c9554 | ||
|
|
a4a4a1c665 | ||
|
|
b8887e9179 | ||
|
|
c0cc7df743 | ||
|
|
c5e2d27028 | ||
|
|
bb7b62fcb4 | ||
|
|
e56962c624 | ||
|
|
d9e159a395 | ||
|
|
4578b9dd33 | ||
|
|
b44f9c07b8 | ||
|
|
55bcdaf3c4 | ||
|
|
8f6f2ad5fb | ||
|
|
7063b0e2e9 | ||
|
|
3679a3dcdc | ||
|
|
4ae7992c2e | ||
|
|
566b978768 | ||
|
|
7a3820c539 | ||
|
|
bca5f2761d | ||
|
|
f7d49e2d7c | ||
|
|
f8b3d67c67 | ||
|
|
2043f834b0 | ||
|
|
c65486db27 | ||
|
|
30589b5419 | ||
|
|
3f59ffc209 | ||
|
|
d487d78b30 | ||
|
|
1d5b57bd9b | ||
|
|
9e89bb1ee0 | ||
|
|
6c717b6558 |
@@ -1,5 +0,0 @@
|
||||
# Xcode
|
||||
xcuserdata/
|
||||
|
||||
# JetBrains
|
||||
.idea/
|
||||
+64
-15
@@ -1,26 +1,75 @@
|
||||
language: c
|
||||
|
||||
os:
|
||||
- linux
|
||||
|
||||
compiler:
|
||||
- clang
|
||||
- gcc
|
||||
|
||||
python:
|
||||
- "2.7"
|
||||
matrix:
|
||||
include:
|
||||
- os: windows
|
||||
compiler: clang
|
||||
env: TESTTOOL="--deflate --lzma --pkcrypt --wzaes --zipcd" BUILDDIR="..\build\Debug"
|
||||
- os: windows
|
||||
compiler: gcc
|
||||
env: TESTTOOL="--bzip2 --pkcrypt --wzaes --split" BUILDDIR="..\build\Debug"
|
||||
- os: linux
|
||||
compiler: clang
|
||||
env: TESTTOOL="--deflate --lzma --pkcrypt --wzaes" BUILDDIR="../build"
|
||||
- os: linux
|
||||
compiler: clang
|
||||
env: TOOL="-DUSE_ZLIB=OFF" TESTTOOL="--lzma --bzip --pkcrypt --wzaes" BUILDDIR="../build"
|
||||
- os: linux
|
||||
compiler: clang
|
||||
env: TOOL="-DUSE_BZIP=OFF" TESTTOOL="--deflate --lzma --pkcrypt --wzaes" BUILDDIR="../build"
|
||||
- os: linux
|
||||
compiler: clang
|
||||
env: TOOL="-DUSE_LZMA=OFF" TESTTOOL="--deflate --bzip --pkcrypt --wzaes" BUILDDIR="../build"
|
||||
- os: linux
|
||||
compiler: clang
|
||||
env: TOOL="-DUSE_AES=OFF" TESTTOOL="--deflate --pkcrypt --zipcd" BUILDDIR="../build"
|
||||
- os: linux
|
||||
compiler: clang
|
||||
env: TOOL="-DUSE_PKCRYPT=OFF" TESTTOOL="--deflate --wzaes" BUILDDIR="../build"
|
||||
- os: linux
|
||||
compiler: clang
|
||||
env: TOOL="-DCOMPRESS_ONLY=ON" TESTTOOL="--deflate" BUILDDIR="../build"
|
||||
- os: linux
|
||||
compiler: clang
|
||||
env: TOOL="-DDECOMPRESS_ONLY=ON" TESTTOOL="--deflate" BUILDDIR="../build"
|
||||
- os: linux
|
||||
compiler: clang
|
||||
env: TOOL="-DUSE_PKCRYPT=OFF -DUSE_AES=OFF" TESTTOOL="--deflate --bzip --lzma" BUILDDIR="../build"
|
||||
- os: linux
|
||||
compiler: clang
|
||||
env: TOOL="-DUSE_OPENSSL=ON" TESTTOOL="--deflate --pkcrypt --wzaes --sign" BUILDDIR="../build"
|
||||
- os: linux
|
||||
compiler: gcc
|
||||
env: TESTTOOL="--bzip2 --pkcrypt --wzaes --zipcd" BUILDDIR="../build"
|
||||
- os: osx
|
||||
compiler: clang
|
||||
env: TESTTOOL="--deflate --lzma --pkcrypt --wzaes" BUILDDIR="../build"
|
||||
- os: osx
|
||||
compiler: gcc
|
||||
env: TESTTOOL="--bzip2 --pkcrypt --wzaes --split" BUILDDIR="../build"
|
||||
- os: osx
|
||||
compiler: clang
|
||||
env: TOOL="-DUSE_LIBCOMP=ON" TESTTOOL="--deflate --pkcrypt --wzaes" BUILDDIR="../build"
|
||||
- os: osx
|
||||
compiler: gcc
|
||||
env: TOOL="-DUSE_LIBCOMP=ON" TESTTOOL="--deflate --pkcrypt --wzaes" BUILDDIR="../build"
|
||||
- os: osx
|
||||
compiler: clang
|
||||
env: TOOL="-DUSE_OPENSSL=ON -DOPENSSL_ROOT_DIR=/usr/local/opt/openssl -DOPENSSL_INCLUDE_DIRS=/usr/local/opt/openssl/include -DOPENSSL_CRYPTO_LIBRARY=/usr/local/opt/openssl/lib/libcrypto.dylib -DOPENSSL_SSL_LIBRARY=/usr/local/opt/openssl/lib/libssl.dylib" TESTTOOL="--deflate --pkcrypt --wzaes --sign" BUILDDIR="../build"
|
||||
|
||||
before_install:
|
||||
- sudo apt-get install libbsd-dev
|
||||
- sudo pip install --upgrade pip
|
||||
- sudo pip install numpy
|
||||
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sudo apt-get install libbsd-dev; fi
|
||||
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sudo apt-get install libssl-dev; fi
|
||||
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew install openssl; fi
|
||||
- if [[ "$TRAVIS_OS_NAME" == "windows" ]]; then choco install python2; fi
|
||||
- if [[ "$TRAVIS_OS_NAME" == "windows" ]]; then export PATH="/c/Python27:$PATH"; fi
|
||||
|
||||
before_script:
|
||||
- mkdir build
|
||||
- cd build
|
||||
- cmake .. -DBUILD_TEST=ON
|
||||
- cmake .. -DBUILD_TEST=ON $TOOL
|
||||
|
||||
script:
|
||||
- make
|
||||
- cmake --build .
|
||||
- cd ../test
|
||||
- travis_wait 30 python test.py --exe_dir=../build
|
||||
- python test.py --raw $TEST_TOOL --exe_dir=$BUILDDIR
|
||||
|
||||
+372
-126
@@ -1,27 +1,32 @@
|
||||
#***************************************************************************
|
||||
# Copyright: Matthias Schmieder,
|
||||
# E-Mail: schmieder.matthias@gmail.com
|
||||
# Year: 2016
|
||||
# Copyright (C) 2017-2018 Nathan Moinvaziri
|
||||
# https://github.com/nmoinvaz/minizip
|
||||
# Copyright (C) 2016 Matthias Schmieder
|
||||
# schmieder.matthias@gmail.com
|
||||
#***************************************************************************
|
||||
cmake_minimum_required(VERSION 2.8)
|
||||
|
||||
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)
|
||||
cmake_minimum_required(VERSION 3.0.2)
|
||||
message(STATUS "Using CMake version ${CMAKE_VERSION}")
|
||||
|
||||
# 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()
|
||||
option(USE_COMPAT "Enables compatibility layer" ON)
|
||||
option(USE_ZLIB "Enables ZLIB compression" ON)
|
||||
option(USE_BZIP2 "Enables BZIP2 compression" ON)
|
||||
option(USE_LZMA "Enables LZMA compression" ON)
|
||||
option(USE_PKCRYPT "Enables PKWARE traditional encryption" ON)
|
||||
option(USE_AES "Enables AES encryption" ON)
|
||||
option(USE_LIBCOMP "Enables Apple compression" OFF)
|
||||
option(USE_OPENSSL "Enables OpenSSL for encryption" OFF)
|
||||
option(USE_BRG "Enables Brian Gladman's encryption library" OFF)
|
||||
option(COMPRESS_ONLY "Only support compression" OFF)
|
||||
option(DECOMPRESS_ONLY "Only support decompression" OFF)
|
||||
option(BUILD_TEST "Builds minizip test executable" OFF)
|
||||
|
||||
project("minizip")
|
||||
project("minizip" C)
|
||||
|
||||
include(CheckLibraryExists)
|
||||
include(CheckSymbolExists)
|
||||
include(GNUInstallDirs)
|
||||
include(ExternalProject)
|
||||
|
||||
set(INSTALL_BIN_DIR ${CMAKE_INSTALL_BINDIR} CACHE PATH "Installation directory for executables")
|
||||
set(INSTALL_LIB_DIR ${CMAKE_INSTALL_LIBDIR} CACHE PATH "Installation directory for libraries")
|
||||
@@ -30,71 +35,206 @@ set(INSTALL_MAN_DIR ${CMAKE_INSTALL_MANDIR} CACHE PATH "Installation directory f
|
||||
set(INSTALL_PKGCONFIG_DIR ${CMAKE_INSTALL_LIBDIR}/pkgconfig CACHE PATH "Installation directory for pkgconfig (.pc) files")
|
||||
set(INSTALL_CMAKE_DIR ${CMAKE_INSTALL_LIBDIR}/cmake/minizip CACHE PATH "Installation directory for cmake files.")
|
||||
|
||||
set(VERSION "2.2.7")
|
||||
# Minizip library version
|
||||
set(VERSION "2.7.3")
|
||||
|
||||
# Minizip api version
|
||||
set(SOVERSION "2.5")
|
||||
|
||||
# Set cmake debug postfix to d
|
||||
set(CMAKE_DEBUG_POSTFIX "d")
|
||||
|
||||
# Ensure correct version of zlib is referenced
|
||||
if(USE_ZLIB)
|
||||
set(ZLIB_ROOT ${DEF_ZLIB_ROOT} CACHE PATH "Parent directory of zlib installation")
|
||||
find_package(ZLIB REQUIRED)
|
||||
if(ZLIB_FOUND)
|
||||
include_directories(${ZLIB_INCLUDE_DIRS})
|
||||
endif()
|
||||
endif()
|
||||
|
||||
set(MINIZIP_PC ${CMAKE_CURRENT_BINARY_DIR}/minizip.pc)
|
||||
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/minizip.pc.cmakein ${MINIZIP_PC} @ONLY)
|
||||
|
||||
set(PROJECT_NAME libminizip)
|
||||
|
||||
# Ensure correct version of zlib is referenced
|
||||
if(USE_ZLIB)
|
||||
set(ZLIB_ROOT ${DEF_ZLIB_ROOT} CACHE PATH "Parent directory of zlib installation")
|
||||
find_package(ZLIB)
|
||||
if(ZLIB_FOUND)
|
||||
message(STATUS "Using ZLIB ${ZLIB_VERSION_STRING}")
|
||||
include_directories(${ZLIB_INCLUDE_DIRS})
|
||||
add_definitions(-DHAVE_ZLIB)
|
||||
else()
|
||||
if (NOT ZLIB_TAG)
|
||||
set(ZLIB_TAG master)
|
||||
endif()
|
||||
if (NOT ZLIB_REPOSITORY)
|
||||
set(ZLIB_REPOSITORY https://github.com/madler/zlib)
|
||||
endif()
|
||||
|
||||
message(STATUS "Using ZLIB ${ZLIB_REPOSITORY} ${ZLIB_TAG}")
|
||||
|
||||
set(ZLIB_SOURCE_DIR ${CMAKE_CURRENT_BINARY_DIR}/lib/zlib)
|
||||
set(ZLIB_INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/install/zlib)
|
||||
set(ZLIB_INCLUDE_DIRS ${ZLIB_INSTALL_DIR}/include)
|
||||
|
||||
if(WIN32)
|
||||
if(${CMAKE_GENERATOR} MATCHES "Visual Studio.*")
|
||||
set(ZLIB_LIBRARIES
|
||||
debug ${ZLIB_INSTALL_DIR}/lib/zlibstaticd.lib
|
||||
optimized ${ZLIB_INSTALL_DIR}/lib/zlibstatic.lib)
|
||||
else()
|
||||
if(CMAKE_BUILD_TYPE EQUAL Debug)
|
||||
set(ZLIB_LIBRARIES
|
||||
${ZLIB_INSTALL_DIR}/lib/zlibstaticd.lib)
|
||||
else()
|
||||
set(ZLIB_LIBRARIES
|
||||
${ZLIB_INSTALL_DIR}/lib/zlibstatic.lib)
|
||||
endif()
|
||||
endif()
|
||||
else()
|
||||
set(ZLIB_LIBRARIES
|
||||
${ZLIB_INSTALL_DIR}/lib/libz.a)
|
||||
endif()
|
||||
|
||||
ExternalProject_Add(zlib
|
||||
PREFIX zlib
|
||||
GIT_REPOSITORY ${ZLIB_REPOSITORY}
|
||||
GIT_TAG ${ZLIB_TAG}
|
||||
SOURCE_DIR ${ZLIB_SOURCE_DIR}
|
||||
BUILD_IN_SOURCE 1
|
||||
INSTALL_DIR ${ZLIB_INSTALL_DIR}
|
||||
BUILD_BYPRODUCTS ${ZLIB_STATIC_LIBRARIES}
|
||||
CMAKE_CACHE_ARGS
|
||||
-DCMAKE_INSTALL_PREFIX:STRING=${ZLIB_INSTALL_DIR}
|
||||
-DZLIB_COMPAT:STRING=ON
|
||||
)
|
||||
|
||||
include_directories(${ZLIB_INCLUDE_DIRS})
|
||||
add_definitions(-DHAVE_ZLIB)
|
||||
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# Check if bzip2 installation is present
|
||||
if(USE_BZIP2)
|
||||
set(BZIP2_ROOT ${DEF_BZIP2_ROOT} CACHE PATH "Parent directory of bzip2 installation")
|
||||
find_package(BZip2)
|
||||
if(BZIP2_FOUND)
|
||||
message(STATUS "Using BZIP2 ${BZIP2_VERSION_STRING}")
|
||||
include_directories(${BZIP2_INCLUDE_DIRS})
|
||||
add_definitions(-DHAVE_BZIP2)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# Check to see if openssl installation is present
|
||||
if (USE_OPENSSL)
|
||||
set(OPENSSL_ROOT ${DEF_OPENSSL_ROOT} CACHE PATH "Parent directory of openssl installation")
|
||||
find_package(OpenSSL)
|
||||
if (OPENSSL_FOUND)
|
||||
message(STATUS "Using OpenSSL ${OPENSSL_VERSION}")
|
||||
include_directories(${OPENSSL_INCLUDE_DIRS})
|
||||
link_directories(${OPENSSL_LIBRARIES})
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# Initial source files
|
||||
set(MINIZIP_SRC
|
||||
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_crypt.c
|
||||
mz_os.c
|
||||
mz_strm.c
|
||||
mz_strm_buf.c
|
||||
mz_strm_mem.c
|
||||
mz_strm_split.c
|
||||
mz_zip.c
|
||||
mz_zip_rw.c)
|
||||
|
||||
# Initial header files
|
||||
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_crypt.h
|
||||
mz_strm.h
|
||||
mz_strm_buf.h
|
||||
mz_strm_mem.h
|
||||
mz_strm_split.h
|
||||
mz_strm_os.h
|
||||
mz_zip.h
|
||||
mz_zip_rw.h)
|
||||
|
||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
# Setup predefined macros
|
||||
if(COMPRESS_ONLY)
|
||||
add_definitions(-DMZ_ZIP_NO_DECOMPRESSION)
|
||||
endif()
|
||||
if(DECOMPRESS_ONLY)
|
||||
add_definitions(-DMZ_ZIP_NO_COMPRESSION)
|
||||
endif()
|
||||
if(NOT USE_PKCRYPT AND NOT USE_AES)
|
||||
add_definitions(-DMZ_ZIP_NO_ENCRYPTION)
|
||||
endif()
|
||||
|
||||
# Windows specific
|
||||
if(WIN32)
|
||||
list(APPEND MINIZIP_SRC "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_os_win32.c")
|
||||
if (USE_PKCRYPT OR USE_AES)
|
||||
if (NOT USE_OPENSSL AND NOT OPENSSL_FOUND AND NOT USE_BRG)
|
||||
list(APPEND MINIZIP_SRC "mz_crypt_win32.c")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
add_definitions(-D_CRT_SECURE_NO_DEPRECATE)
|
||||
endif()
|
||||
|
||||
# Windows store specific
|
||||
if("${CMAKE_SYSTEM_NAME}" STREQUAL "WindowsStore")
|
||||
add_definitions(-DMZ_USE_WINRT_API)
|
||||
add_definitions(-DMZ_WINRT_API)
|
||||
endif()
|
||||
|
||||
# Unix specific
|
||||
if(UNIX)
|
||||
add_compile_options(-O3)
|
||||
list(APPEND MINIZIP_SRC "mz_os_posix.c" "mz_strm_os_posix.c")
|
||||
|
||||
if ((USE_PKCRYPT OR USE_AES) AND (NOT USE_OPENSSL AND NOT OPENSSL_FOUND))
|
||||
|
||||
if (APPLE AND NOT USE_BRG)
|
||||
list(APPEND MINIZIP_SRC "mz_crypt_apple.c")
|
||||
else()
|
||||
set(USE_BRG TRUE)
|
||||
endif()
|
||||
|
||||
# Check to see which random generation functions we have
|
||||
check_symbol_exists("getrandom" "sys/random.h" HAVE_GETRANDOM)
|
||||
if (HAVE_GETRANDOM)
|
||||
add_definitions(-DHAVE_GETRANDOM)
|
||||
endif()
|
||||
check_symbol_exists("arc4random_buf" "stdlib.h" HAVE_ARC4RANDOM_BUF)
|
||||
if (HAVE_ARC4RANDOM_BUF)
|
||||
add_definitions(-DHAVE_ARC4RANDOM_BUF)
|
||||
else()
|
||||
check_symbol_exists("arc4random" "stdlib.h" HAVE_ARC4RANDOM)
|
||||
if (HAVE_ARC4RANDOM)
|
||||
add_definitions(-DHAVE_ARC4RANDOM)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
list(APPEND MINIZIP_SRC "src/mz_os_posix.c")
|
||||
list(APPEND MINIZIP_PUBLIC_HEADERS "src/mz_os_posix.h")
|
||||
if(NOT HAVE_ARC4RANDOM_BUF)
|
||||
find_package(PkgConfig REQUIRED)
|
||||
|
||||
pkg_check_modules(LIBBSD libbsd)
|
||||
if (LIBBSD_FOUND)
|
||||
check_library_exists("${LIBBSD_LIBRARIES}" "arc4random_buf" "${LIBBSD_LIBRARY_DIRS}" HAVE_LIBBSD_ARC4RANDOM_BUF)
|
||||
if (HAVE_LIBBSD_ARC4RANDOM_BUF)
|
||||
add_definitions(-DHAVE_LIBBSD)
|
||||
add_definitions(-DHAVE_ARC4RANDOM_BUF)
|
||||
include_directories(${LIBBSD_INCLUDE_DIRS})
|
||||
link_directories(${LIBBSD_LIBRARY_DIRS})
|
||||
endif()
|
||||
endif()
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# Setup and use large file macros if necessary
|
||||
set(define_lfs_macros TRUE)
|
||||
|
||||
if(ANDROID)
|
||||
string(REGEX REPLACE "android-([0-9+])" "\\1"
|
||||
android_api "${ANDROID_PLATFORM}")
|
||||
if(${android_api} LESS 24)
|
||||
if(android_api LESS 24)
|
||||
set(define_lfs_macros FALSE)
|
||||
endif()
|
||||
endif()
|
||||
@@ -106,89 +246,154 @@ if(UNIX)
|
||||
add_definitions(-D_FILE_OFFSET_BITS=64)
|
||||
endif()
|
||||
|
||||
find_package(PkgConfig REQUIRED)
|
||||
|
||||
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
|
||||
pkg_check_modules(LIBBSD libbsd REQUIRED)
|
||||
|
||||
include_directories(${LIBBSD_INCLUDE_DIRS})
|
||||
link_directories(${LIBBSD_LIBRARY_DIRS})
|
||||
find_package(Iconv)
|
||||
if (Iconv_FOUND)
|
||||
message(STATUS "Using Iconv")
|
||||
include_directories(${Iconv_INCLUDE_DIRS})
|
||||
endif()
|
||||
|
||||
endif()
|
||||
|
||||
if(USE_CRYPT)
|
||||
add_definitions(-DHAVE_CRYPT)
|
||||
# Include compatibility layer
|
||||
if(USE_COMPAT)
|
||||
list(APPEND MINIZIP_SRC "mz_compat.c")
|
||||
list(APPEND MINIZIP_PUBLIC_HEADERS "mz_compat.h")
|
||||
|
||||
list(APPEND MINIZIP_SRC "src/mz_strm_crypt.c")
|
||||
list(APPEND MINIZIP_PUBLIC_HEADERS "src/mz_strm_crypt.h")
|
||||
set(COMPAT_HEADER "\
|
||||
/* file.h -- Compatibility layer shim\n\
|
||||
part of the MiniZip project\n\n\
|
||||
This program is distributed under the terms of the same license as zlib.\n\
|
||||
See the accompanying LICENSE file for the full text of the license.\n\
|
||||
*/\n\n\
|
||||
#ifndef MZ_COMPAT_FILE\n\
|
||||
#define MZ_COMPAT_FILE\n\
|
||||
#include \"mz_compat.h\"\n\
|
||||
#endif\n")
|
||||
|
||||
string(REPLACE "file.h" "zip.h" ZIP_COMPAT_HEADER ${COMPAT_HEADER})
|
||||
string(REPLACE "MZ_COMPAT_FILE" "MZ_COMPAT_ZIP" ZIP_COMPAT_HEADER ${ZIP_COMPAT_HEADER})
|
||||
file(WRITE "zip.h" ${ZIP_COMPAT_HEADER})
|
||||
list(APPEND MINIZIP_PUBLIC_HEADERS "zip.h")
|
||||
|
||||
string(REPLACE "file.h" "unzip.h" UNZIP_COMPAT_HEADER ${COMPAT_HEADER})
|
||||
string(REPLACE "MZ_COMPAT_FILE" "MZ_COMPAT_UNZIP" UNZIP_COMPAT_HEADER ${UNZIP_COMPAT_HEADER})
|
||||
file(WRITE "unzip.h" ${UNZIP_COMPAT_HEADER})
|
||||
list(APPEND MINIZIP_PUBLIC_HEADERS "unzip.h")
|
||||
endif()
|
||||
|
||||
# Include PKCRYPT
|
||||
if(USE_PKCRYPT)
|
||||
add_definitions(-DHAVE_PKCRYPT)
|
||||
|
||||
list(APPEND MINIZIP_SRC "mz_strm_pkcrypt.c")
|
||||
list(APPEND MINIZIP_PUBLIC_HEADERS "mz_strm_pkcrypt.h")
|
||||
endif()
|
||||
|
||||
# Include AES
|
||||
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")
|
||||
|
||||
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
|
||||
lib/aes/brg_types.h
|
||||
lib/aes/hmac.h
|
||||
lib/aes/pwd2key.h
|
||||
lib/aes/sha1.h)
|
||||
|
||||
include_directories(lib/aes)
|
||||
|
||||
source_group("AES" FILES ${AES_SRC} ${AES_PUBLIC_HEADERS})
|
||||
list(APPEND MINIZIP_SRC "mz_strm_wzaes.c")
|
||||
list(APPEND MINIZIP_PUBLIC_HEADERS "mz_strm_wzaes.h")
|
||||
endif()
|
||||
|
||||
# Include BRG
|
||||
if(USE_BRG)
|
||||
list(APPEND MINIZIP_SRC "mz_crypt_brg.c")
|
||||
|
||||
add_definitions(-DMZ_ZIP_NO_SIGNING)
|
||||
|
||||
set(BRG_SRC
|
||||
lib/brg/aescrypt.c
|
||||
lib/brg/aeskey.c
|
||||
lib/brg/aestab.c
|
||||
lib/brg/hmac.c
|
||||
lib/brg/sha1.c
|
||||
lib/brg/sha2.c)
|
||||
|
||||
set(BRG_PUBLIC_HEADERS
|
||||
lib/brg/aes.h
|
||||
lib/brg/aesopt.h
|
||||
lib/brg/aestab.h
|
||||
lib/brg/brg_endian.h
|
||||
lib/brg/brg_types.h
|
||||
lib/brg/hmac.h
|
||||
lib/brg/sha1.h
|
||||
lib/brg/sha2.h)
|
||||
|
||||
include_directories(lib/brg)
|
||||
|
||||
source_group("BRG" FILES ${BRG_SRC} ${BRG_PUBLIC_HEADERS})
|
||||
endif()
|
||||
|
||||
# Include OpenSSL
|
||||
if(USE_OPENSSL AND OPENSSL_FOUND)
|
||||
list(APPEND MINIZIP_SRC "mz_crypt_openssl.c")
|
||||
endif()
|
||||
|
||||
# Include ZLIB
|
||||
if(USE_ZLIB)
|
||||
add_definitions(-DHAVE_ZLIB)
|
||||
|
||||
if(USE_LIBCOMP)
|
||||
list(APPEND MINIZIP_SRC "mz_strm_libcomp.c")
|
||||
list(APPEND MINIZIP_PUBLIC_HEADERS "mz_strm_libcomp.h")
|
||||
else()
|
||||
list(APPEND MINIZIP_SRC "mz_strm_zlib.c")
|
||||
list(APPEND MINIZIP_PUBLIC_HEADERS "mz_strm_zlib.h")
|
||||
|
||||
if (ZLIB_FOUND)
|
||||
include(CheckFunctionExists)
|
||||
set(CMAKE_REQUIRED_LIBRARIES ZLIB)
|
||||
CHECK_FUNCTION_EXISTS(z_get_crc_table
|
||||
NEEDS_Z_PREFIX)
|
||||
|
||||
if(NEEDS_Z_PREFIX)
|
||||
add_definitions(-DZ_PREFIX)
|
||||
endif()
|
||||
endif()
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# Include BZIP2
|
||||
if(USE_BZIP2)
|
||||
add_definitions(-DHAVE_BZIP2)
|
||||
add_definitions(-DBZ_NO_STDIO)
|
||||
|
||||
list(APPEND MINIZIP_SRC "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
|
||||
lib/bzip2/bzlib.c
|
||||
lib/bzip2/compress.c
|
||||
lib/bzip2/crctable.c
|
||||
lib/bzip2/decompress.c
|
||||
lib/bzip2/huffman.c
|
||||
lib/bzip2/randtable.c)
|
||||
if(BZIP2_FOUND)
|
||||
set(CMAKE_REQUIRED_LIBRARIES BZip2::BZip2)
|
||||
else()
|
||||
add_definitions(-DBZ_NO_STDIO)
|
||||
|
||||
set(BZIP2_PUBLIC_HEADERS
|
||||
lib/bzip2/bzlib.h
|
||||
lib/bzip2/bzlib_private.h)
|
||||
set(BZIP2_SRC
|
||||
lib/bzip2/blocksort.c
|
||||
lib/bzip2/bzlib.c
|
||||
lib/bzip2/compress.c
|
||||
lib/bzip2/crctable.c
|
||||
lib/bzip2/decompress.c
|
||||
lib/bzip2/huffman.c
|
||||
lib/bzip2/randtable.c)
|
||||
|
||||
include_directories(lib/bzip2)
|
||||
set(BZIP2_PUBLIC_HEADERS
|
||||
lib/bzip2/bzlib.h
|
||||
lib/bzip2/bzlib_private.h)
|
||||
|
||||
source_group("BZip2" FILES ${BZIP2_SRC} ${BZIP2_PUBLIC_HEADERS})
|
||||
include_directories(lib/bzip2)
|
||||
|
||||
source_group("BZip2" FILES ${BZIP2_SRC} ${BZIP2_PUBLIC_HEADERS})
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# Include LZMA
|
||||
if(USE_LZMA)
|
||||
add_definitions(-DHAVE_LZMA)
|
||||
add_definitions(-DHAVE_CONFIG_H)
|
||||
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
|
||||
@@ -304,47 +509,88 @@ if(CMAKE_C_COMPILER MATCHES ".*clang")
|
||||
enable_option_if_supported(-msse4.1 check_opt_sse41)
|
||||
enable_option_if_supported(-maes check_opt_aes)
|
||||
endif()
|
||||
if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_CLANG)
|
||||
add_compile_options(-W -Wall)
|
||||
add_compile_options(-O3)
|
||||
endif()
|
||||
|
||||
# Create minizip library
|
||||
source_group("Minizip" FILES ${MINIZIP_SRC} ${MINIZIP_PUBLIC_HEADERS})
|
||||
|
||||
add_library(${PROJECT_NAME}
|
||||
${MINIZIP_SRC} ${MINIZIP_PUBLIC_HEADERS}
|
||||
${AES_SRC} ${AES_PUBLIC_HEADERS}
|
||||
${BRG_SRC} ${BRG_PUBLIC_HEADERS}
|
||||
${BZIP2_SRC} ${BZIP2_PUBLIC_HEADERS}
|
||||
${LZMA_SRC} ${LZMA_PUBLIC_HEADERS})
|
||||
|
||||
if (MSVC AND BUILD_SHARED_LIBS)
|
||||
set_target_properties(${PROJECT_NAME} PROPERTIES ARCHIVE_OUTPUT_NAME "minizip")
|
||||
endif ()
|
||||
|
||||
set_target_properties(${PROJECT_NAME} PROPERTIES VERSION ${VERSION} SOVERSION ${SOVERSION})
|
||||
|
||||
set_target_properties(${PROJECT_NAME} PROPERTIES LINKER_LANGUAGE C PREFIX ""
|
||||
POSITION_INDEPENDENT_CODE 1)
|
||||
|
||||
# Link with external libraries
|
||||
if(USE_ZLIB)
|
||||
target_link_libraries(${PROJECT_NAME} ZLIB::ZLIB)
|
||||
target_link_libraries(${PROJECT_NAME} ${ZLIB_LIBRARIES})
|
||||
if(NOT ZLIB_FOUND)
|
||||
add_dependencies(${PROJECT_NAME} zlib)
|
||||
endif()
|
||||
endif()
|
||||
if(USE_BZIP2 AND BZIP2_FOUND)
|
||||
target_link_libraries(${PROJECT_NAME} ${BZIP2_LIBRARIES})
|
||||
endif()
|
||||
if(USE_LZMA)
|
||||
set_target_properties(${PROJECT_NAME} PROPERTIES C_STANDARD 99)
|
||||
endif()
|
||||
if(UNIX)
|
||||
target_link_libraries(${PROJECT_NAME} ${LIBBSD_LIBRARIES})
|
||||
if(USE_LIBCOMP)
|
||||
target_link_libraries(${PROJECT_NAME} compression)
|
||||
endif()
|
||||
if(Iconv_FOUND AND NOT Iconv_IS_BUILT_IN)
|
||||
target_link_libraries(${PROJECT_NAME} ${Iconv_LIBRARIES})
|
||||
endif()
|
||||
if(USE_OPENSSL AND OPENSSL_FOUND)
|
||||
target_link_libraries(${PROJECT_NAME} ${OPENSSL_LIBRARIES})
|
||||
elseif(UNIX)
|
||||
if(APPLE AND NOT USE_BRG)
|
||||
message(STATUS "Using Security Framework")
|
||||
target_link_libraries(${PROJECT_NAME} "-framework CoreFoundation")
|
||||
target_link_libraries(${PROJECT_NAME} "-framework Security")
|
||||
set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS "-Wl,-F/Library/Frameworks")
|
||||
else()
|
||||
target_link_libraries(${PROJECT_NAME} ${LIBBSD_LIBRARIES})
|
||||
endif()
|
||||
endif()
|
||||
|
||||
target_include_directories(${PROJECT_NAME} PUBLIC $<INSTALL_INTERFACE:${INSTALL_INC_DIR}>)
|
||||
|
||||
install(TARGETS ${PROJECT_NAME} EXPORT ${PROJECT_NAME}
|
||||
INCLUDES DESTINATION "${INSTALL_INC_DIR}"
|
||||
RUNTIME DESTINATION "${INSTALL_BIN_DIR}"
|
||||
ARCHIVE DESTINATION "${INSTALL_LIB_DIR}"
|
||||
LIBRARY DESTINATION "${INSTALL_LIB_DIR}")
|
||||
|
||||
install(EXPORT ${PROJECT_NAME}
|
||||
DESTINATION "${INSTALL_CMAKE_DIR}"
|
||||
NAMESPACE "MINIZIP::")
|
||||
|
||||
install(FILES ${MINIZIP_PUBLIC_HEADERS} DESTINATION "${INSTALL_INC_DIR}")
|
||||
install(FILES ${MINIZIP_PC} DESTINATION "${INSTALL_PKGCONFIG_DIR}")
|
||||
# Install files
|
||||
if(NOT SKIP_INSTALL_LIBRARIES AND NOT SKIP_INSTALL_ALL)
|
||||
install(TARGETS ${PROJECT_NAME} EXPORT ${PROJECT_NAME}
|
||||
INCLUDES DESTINATION "${INSTALL_INC_DIR}"
|
||||
RUNTIME DESTINATION "${INSTALL_BIN_DIR}"
|
||||
ARCHIVE DESTINATION "${INSTALL_LIB_DIR}"
|
||||
LIBRARY DESTINATION "${INSTALL_LIB_DIR}")
|
||||
install(EXPORT ${PROJECT_NAME}
|
||||
DESTINATION "${INSTALL_CMAKE_DIR}"
|
||||
NAMESPACE "MINIZIP::")
|
||||
endif()
|
||||
if(NOT SKIP_INSTALL_HEADERS AND NOT SKIP_INSTALL_ALL)
|
||||
install(FILES ${MINIZIP_PUBLIC_HEADERS} DESTINATION "${INSTALL_INC_DIR}")
|
||||
endif()
|
||||
if(NOT SKIP_INSTALL_FILES AND NOT SKIP_INSTALL_ALL)
|
||||
install(FILES ${MINIZIP_PC} DESTINATION "${INSTALL_PKGCONFIG_DIR}")
|
||||
endif()
|
||||
|
||||
# Build test executable
|
||||
if(BUILD_TEST)
|
||||
add_executable(minizip "src/minizip.c")
|
||||
add_executable(minizip "minizip.c" "test/test.c" "test/test.h")
|
||||
target_link_libraries(minizip ${PROJECT_NAME})
|
||||
|
||||
install(TARGETS minizip
|
||||
if(NOT SKIP_INSTALL_BINARIES AND NOT SKIP_INSTALL_ALL)
|
||||
install(TARGETS minizip
|
||||
RUNTIME DESTINATION "bin")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
Pod::Spec.new do |s|
|
||||
s.name = 'Minizip'
|
||||
s.version = '2.2.7'
|
||||
s.version = '2.7.3'
|
||||
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|
|
||||
|
||||
@@ -1,22 +1,85 @@
|
||||
# Minizip 2.2.7
|
||||
# minizip 2.7.3
|
||||
|
||||
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.
|
||||
minizip is a zip manipulation library written in C that is supported on Windows, macOS, and Linux.
|
||||
|
||||
Dev: 
|
||||
Master: 
|
||||
[](https://github.com/nmoinvaz/minizip/blob/master/LICENSE)
|
||||
|
||||
For my older fork of this library checkout the [1.2](https://github.com/nmoinvaz/minizip/tree/1.2) branch.
|
||||
For the original work maintained by Mark Adler checkout the zlib minizip [contrib](https://github.com/madler/zlib/tree/master/contrib/minizip).
|
||||
Maintained by Nathan Moinvaziri.
|
||||
|
||||
## Branches
|
||||
|
||||
| Name | State | Version | Description | Travis CI |
|
||||
|:- |:-:|:-:|:-|:-:|
|
||||
|[master](https://github.com/nmoinvaz/minizip/tree/master)|Active|2.x|Modern rewrite of 1.2 branch that includes more advanced features, improvements in code maintainability and readability, and the reduction of duplicate code. Compatibility layer provided for older versions.|[](https://travis-ci.org/nmoinvaz/minizip/branches)|
|
||||
|[dev](https://github.com/nmoinvaz/minizip/tree/dev)|Active|2.x|Latest development code|[](https://travis-ci.org/nmoinvaz/minizip/branches)|
|
||||
|[1.2](https://github.com/nmoinvaz/minizip/tree/1.2)|Stale| 1.x|Drop-in replacement for zlib's minizip that includes WinZip AES encryption, disk splitting, I/O buffering and some additional fixes.||
|
||||
|[1.1](https://github.com/nmoinvaz/minizip/tree/1.1)|Stale| 1.x|Original minizip as of zlib 1.2.11.||
|
||||
|
||||
## History
|
||||
|
||||
Minizip was originally developed by [Gilles Vollant](https://www.winimage.com/zLibDll/minizip.html) and
|
||||
had been contributed to by many people. As part of the zlib distribution, Mark Adler still maintains the
|
||||
original [minizip](https://github.com/madler/zlib/tree/master/contrib/minizip) project which is included in this repository as a reference.
|
||||
|
||||
My work with the minizip library began in 2006 when I started submitting bugs I found to
|
||||
Gilles Vollant. In 2010, I implemented some additional features like WinZip AES encryption, disk splitting, and
|
||||
I/O buffering that were necessary for another project I was working on. Shortly after, I created this public repository
|
||||
so I could share these and other improvements with the rest of the world. I have been maintaining and actively
|
||||
developing this code base ever since. At the beginning of 2017, I began the work to refactor and rewrite
|
||||
the library as version 2 because it had become difficult to maintain and code readability suffered over the years.
|
||||
|
||||
## Features
|
||||
|
||||
+ Creating and extracting zip archives.
|
||||
+ Adding and removing entries from zip archives.
|
||||
+ Read and write raw zip entry data.
|
||||
+ Reading and writing zip archives from memory.
|
||||
+ Zlib, BZIP2, and LZMA compression methods.
|
||||
+ Password protection through Traditional PKWARE and [WinZIP AES](https://www.winzip.com/aes_info.htm) encryption.
|
||||
+ Buffered streaming for improved I/O performance.
|
||||
+ NTFS timestamp support for UTC last modified, last accessed, and creation dates.
|
||||
+ Disk split support for splitting zip archives into multiple files.
|
||||
+ Preservation of file attributes across file systems.
|
||||
+ Unicode filename support through UTF-8 encoding.
|
||||
+ Legacy character encoding support CP437, CP932, CP936, CP950.
|
||||
+ Turn off compilation of compression, decompression, or encryption.
|
||||
+ Windows (Win32 & WinRT), macOS and Linux platform support.
|
||||
+ Streaming interface for easy implementation of additional platforms.
|
||||
+ Support for Apple's compression library ZLIB implementation.
|
||||
+ Zero out local file header information.
|
||||
+ Zip/unzip of central directory to reduce size.
|
||||
+ Ability to generate and verify CMS signature for each entry.
|
||||
+ Example minizip command line tool.
|
||||
|
||||
## 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. Run cmake in the minizip directory.
|
||||
|
||||
```
|
||||
cmake .
|
||||
cmake . -DBUILD_TEST=ON
|
||||
cmake --build .
|
||||
```
|
||||
|
||||
## Build Options
|
||||
|
||||
| Name | Description | Default Value |
|
||||
|:- |:-|:-:|
|
||||
| USE_COMPAT | Enables compatibility layer | ON |
|
||||
| USE_ZLIB | Enables ZLIB compression | ON |
|
||||
| USE_BZIP2 | Enables BZIP2 compression | ON |
|
||||
| USE_LZMA | Enables LZMA compression | ON |
|
||||
| USE_PKCRYPT | Enables PKWARE traditional encryption | ON |
|
||||
| USE_AES | Enables WinZIP AES encryption | ON |
|
||||
| USE_LIBCOMP | Enables Apple compression | OFF |
|
||||
| USE_OPENSSL | Enables OpenSSL encryption | OFF |
|
||||
| USE_BRG | Enables Brian Gladman's library | OFF |
|
||||
| COMPRESS_ONLY | Only support compression | OFF |
|
||||
| DECOMPRESS_ONLY | Only support decompression | OFF |
|
||||
| BUILD_TEST | Builds minizip test executable | OFF |
|
||||
|
||||
## Contents
|
||||
|
||||
| File(s) | Description | Required |
|
||||
@@ -24,145 +87,36 @@ cmake --build .
|
||||
| minizip.c | Sample application | No |
|
||||
| mz_compat.\* | Minizip 1.0 compatibility layer | No |
|
||||
| mz.h | Error codes and flags | Yes |
|
||||
| mz_os\* | OS specific helper functions | Encryption, Disk Splitting |
|
||||
| mz_os\* | Platform specific file/utility functions | Likely |
|
||||
| mz_crypt\* | Configuration specific crypto/hashing functions | Encryption, Signing |
|
||||
| mz_strm.\* | Stream interface | Yes |
|
||||
| mz_strm_aes.\* | WinZIP AES stream | No |
|
||||
| mz_strm_buf.\* | Buffered stream | No |
|
||||
| mz_strm_bzip.\* | BZIP2 stream using libbzip2 | No |
|
||||
| mz_strm_crypt.\* | PKWARE traditional encryption stream | No |
|
||||
| mz_strm_libcomp.\* | Apple compression stream | No |
|
||||
| mz_strm_lzma.\* | LZMA stream using liblzma | No |
|
||||
| mz_strm_mem.\* | Memory stream | Yes |
|
||||
| mz_strm_split.\* | Disk splitting 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_zip.\* | Zip functionality | Yes |
|
||||
| mz_strm_pkcrypt.\* | PKWARE traditional encryption stream | No |
|
||||
| mz_strm_os\* | Platform specific file stream | Yes |
|
||||
| mz_strm_wzaes.\* | WinZIP AES stream | No |
|
||||
| mz_strm_zlib.\* | Deflate stream using zlib | No |
|
||||
| mz_zip.\* | Zip format | Yes |
|
||||
| mz_zip_rw.\* | Zip reader/writer | No |
|
||||
|
||||
## Features
|
||||
## Third-Party Libraries
|
||||
|
||||
### Compression Methods
|
||||
+ [zlib](https://zlib.net/) written by Mark Adler and Jean-loup Gailly.
|
||||
+ Not included in this repository
|
||||
+ Or alternatively, [zlib-ng](https://github.com/Dead2/zlib-ng) by Hans Kristian Rosbach
|
||||
+ [BZIP2](https://www.sourceware.org/bzip2/) written by Julian Seward.
|
||||
+ [liblzma](https://tukaani.org/xz/) written by Lasse Collin.
|
||||
+ Modifications were made to support the ZIP file format specification
|
||||
+ [AES](https://github.com/BrianGladman/aes) and [SHA](https://github.com/BrianGladman/sha) libraries of Brian Gladman.
|
||||
|
||||
#### BZIP2
|
||||
## Acknowledgments
|
||||
|
||||
+ Requires ``cmake . -DUSE_BZIP2=ON`` or ``#define HAVE_BZIP2``
|
||||
+ Requires [BZIP2](http://www.bzip.org/) library
|
||||
Thanks to [Gilles Vollant](https://www.winimage.com/zLibDll/minizip.html) on which this work is originally based on.
|
||||
|
||||
#### LZMA
|
||||
Thanks go out to all the people who have taken the time to contribute code reviews, testing and/or patches. This project would not have been nearly as good without you.
|
||||
|
||||
+ Requires ``cmake . -DUSE_LZMA=ON`` or ``#define HAVE_LZMA``
|
||||
+ Requires [liblzma](https://tukaani.org/xz/) library
|
||||
|
||||
### Encryption
|
||||
|
||||
#### [WinZIP AES Encryption](http://www.winzip.com/aes_info.htm)
|
||||
|
||||
+ Requires ``cmake . -DUSE_AES=ON`` or ``#define HAVE_AES``
|
||||
+ Requires Brian Gladman's [AES](https://github.com/BrianGladman/aes) and [SHA](https://github.com/BrianGladman/sha) libraries
|
||||
|
||||
When zipping with a password it will always use AES 256-bit encryption.
|
||||
When unzipping it will use AES decryption only if necessary.
|
||||
|
||||
#### Disabling All Encryption
|
||||
|
||||
To disable encryption use the following cmake commands:
|
||||
|
||||
```
|
||||
cmake . -DUSE_AES=OFF
|
||||
cmake . -DUSE_CRYPT=OFF
|
||||
```
|
||||
|
||||
### NTFS Timestamps
|
||||
|
||||
Support has been added for UTC last modified, last accessed, and creation dates.
|
||||
|
||||
### Streams
|
||||
|
||||
This library has been refactored around streams.
|
||||
|
||||
#### Memory Streaming
|
||||
|
||||
To unzip from a zip file in memory create a memory stream and pass it to the unzip open functions.
|
||||
```
|
||||
uint8_t *zip_buffer = NULL;
|
||||
int32_t zip_buffer_size = 0;
|
||||
void *mem_stream = NULL;
|
||||
|
||||
// fill zip_buffer with zip contents
|
||||
mz_stream_mem_create(&mem_stream);
|
||||
mz_stream_mem_set_buffer(mem_stream, zip_buffer, zip_buffer_size);
|
||||
mz_stream_open(mem_stream, NULL, MZ_OPEN_MODE_READ);
|
||||
|
||||
void *zip_handle = mz_zip_open(mem_stream, MZ_OPEN_MODE_READ);
|
||||
// do unzip operations
|
||||
|
||||
mz_stream_mem_delete(&mem_stream);
|
||||
```
|
||||
|
||||
To create a zip file in memory first create a growable memory stream and pass it to the zip open functions.
|
||||
|
||||
```
|
||||
void *mem_stream = NULL;
|
||||
|
||||
mz_stream_mem_create(&mem_stream);
|
||||
mz_stream_mem_set_grow_size(mem_stream, (128 * 1024));
|
||||
mz_stream_open(mem_stream, NULL, MZ_OPEN_MODE_CREATE);
|
||||
|
||||
void *zip_handle = mz_zip_open(mem_stream, MZ_OPEN_MODE_WRITE);
|
||||
// do unzip operations
|
||||
|
||||
mz_stream_mem_delete(&mem_stream);
|
||||
```
|
||||
|
||||
For a complete example, see test_zip_mem() in [test.c](https://github.com/nmoinvaz/minizip/blob/master/test/test.c).
|
||||
|
||||
#### Buffered Streaming
|
||||
|
||||
By default the library will read bytes typically one at a time. The buffered stream allows for buffered read and write operations to improve I/O performance.
|
||||
|
||||
```
|
||||
void *stream = NULL;
|
||||
void *buf_stream = NULL;
|
||||
|
||||
mz_stream_os_create(&stream)
|
||||
// do open os stream
|
||||
|
||||
mz_stream_buffered_create(&buf_stream);
|
||||
mz_stream_buffered_open(buf_stream, NULL, MZ_OPEN_MODE_READ);
|
||||
mz_stream_buffered_set_base(buf_stream, stream);
|
||||
|
||||
void *zip_handle = mz_zip_open(buf_stream, MZ_OPEN_MODE_READ);
|
||||
```
|
||||
|
||||
#### Disk Splitting Stream
|
||||
|
||||
To create an archive with multiple disks use the disk splitting stream and for zipping supply a disk size value in bytes.
|
||||
|
||||
```
|
||||
void *stream = NULL;
|
||||
void *split_stream = NULL;
|
||||
|
||||
mz_stream_os_create(&stream);
|
||||
|
||||
mz_stream_split_create(&split_stream);
|
||||
mz_stream_split_set_prop_int64(split_stream, MZ_STREAM_PROP_DISK_SIZE, 64 * 1024);
|
||||
|
||||
mz_stream_set_base(split_stream, stream);
|
||||
|
||||
mz_stream_open(split_stream, path..
|
||||
|
||||
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``
|
||||
|
||||
## 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.
|
||||
The [ZIP format](https://github.com/nmoinvaz/minizip/blob/master/doc/appnote.txt) was defined by Phil Katz of PKWARE.
|
||||
|
||||
+3686
File diff suppressed because it is too large
Load Diff
+3497
File diff suppressed because it is too large
Load Diff
+1243
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,261 @@
|
||||
|
||||
# AES Encryption Information:
|
||||
Encryption Specification AE-1 and AE-2
|
||||
|
||||
**Document version: 1.04
|
||||
Last modified: January 30, 2009**
|
||||
|
||||
**NOTE: WinZip® users do not need to read or understand the information contained on this page. It is intended for developers of Zip file utilities.**
|
||||
|
||||
Changes since the original version of this document are summarized in the [Change History](https://www.winzip.com/aes_info.htm#changes) section below.
|
||||
|
||||
This document describes the file format that WinZip uses to create [AES-encrypted Zip files](https://www.winzip.com/learn/aes-encryption.html). The AE-1 encryption specification was introduced in WinZip 9.0 Beta 1, released in May 2003. The AE-2 encryption specification, a minor variant of the original AE-1 specification differing only in how the CRC is handled, was introduced in WinZip 9.0 Beta 3, released in January, 2004. Note that as of WinZip 11, WinZip itself encrypts most files using the AE-1 format and encrypts others using the AE-2 format.
|
||||
|
||||
From time to time we may update the information provided here, for example to document any changes to the file formats, or to add additional notes or implementation tips. If you would like to receive e-mail announcements of any substantive changes we make to this document, you can [sign up below](https://www.winzip.com/aes_info.htm#mailing-list)for our Developer Information mailing list.
|
||||
|
||||
Without compromising the basic Zip file format, WinZip Computing has extended the format specification to support AES encryption, and this document fully describes the format extension. Additionally, we are providing information about a no-cost third-party source for the actual AES encryption code--the same code that is used by WinZip. We believe that use of the free encryption code and of this specification will make it easy for all developers to add compatible advanced encryption to their Zip file utilities.
|
||||
|
||||
This document is not a tutorial on encryption or Zip file structure. While we have attempted to provide the necessary details of the current WinZip AES encryption format, developers and other interested third parties will need to have or obtain an understanding of basic encryption concepts, Zip file format, etc.
|
||||
|
||||
Developers should also review [AES Coding Tips](https://www.winzip.com/aes_tips.html) page.
|
||||
|
||||
WinZip Computing makes no warranties regarding the information provided in this document. In particular, WinZip Computing does not represent or warrant that the information provided here is free from errors or is suitable for any particular use, or that the file formats described here will be supported in future versions of WinZip. You should test and validate all code and techniques in accordance with good programming practice.
|
||||
|
||||
----------
|
||||
|
||||
**I. Encryption services**
|
||||
|
||||
To perform AES encryption and decryption, WinZip uses AES functions written by Dr. Brian Gladman. The source code for these functions is available in C/C++ and Pentium family assembler for anyone to use under an open source BSD or GPL license from [the AES project page](https://www.winzip.com/gladman.cgi) on Dr. Gladman's web site. The [AES Coding Tips](https://www.winzip.com/aes_tips.html) page also has some information on the use of these functions. WinZip Computing thanks Dr. Gladman for making his AES functions available to anyone under liberal license terms.
|
||||
|
||||
Dr. Gladman's encryption functions are portable to a number of operating systems and can be static linked into your applications, so there are no operating system version or library dependencies. In particular, the functions do not require Microsoft's Cryptography API.
|
||||
|
||||
General information on the AES standard and the encryption algorithm (also known as _Rijndael_) is readily available on the Internet. A good place to start is [http://www.nist.gov/public_affairs/releases/g00-176.htm](http://www.nist.gov/public_affairs/releases/g00-176.htm).
|
||||
|
||||
**II. Zip file format**
|
||||
|
||||
1. Base format reference
|
||||
|
||||
AES-encrypted files are stored within the guidelines of the standard Zip file format using only a new "extra data" field, a new compression method code, and a value in the CRC field dependant on the encryption version, AE-1 or AE-2. The basic Zip file format is otherwise unchanged.
|
||||
|
||||
WinZip sets the version needed to extract and version made by fields in the local and central headers to the same values it would use if the file were not encrypted.
|
||||
|
||||
The basic Zip file format specification used by WinZip is available via FTP from the Info-ZIP group at [ftp://ftp.info-zip.org/pub/infozip/doc/appnote-iz-latest.zip](ftp://ftp.info-zip.org/pub/infozip/doc/appnote-iz-latest.zip).
|
||||
|
||||
2. Compression method and encryption flag
|
||||
|
||||
As for any encrypted file, bit 0 of the "general purpose bit flags" field must be set to 1 in each AES-encrypted file's local header and central directory entry.
|
||||
|
||||
Additionally, the presence of an AES-encrypted file in a Zip file is indicated by a new compression method code (decimal 99) in the file's local header and central directory entry, used along with the AES extra data field described below. There is no change in either the _version made by_ or _version needed to extract_ codes.
|
||||
|
||||
The code for the actual compression method is stored in the AES extra data field (see below).
|
||||
|
||||
3. CRC value
|
||||
|
||||
For files encrypted using the AE-2 method, the standard Zip CRC value is _not_ used, and a 0 must be stored in this field. Corruption of encrypted data within a Zip file is instead detected via the [authentication code](https://www.winzip.com/aes_info.htm#authentication-code) field.
|
||||
|
||||
Files encrypted using the AE-1 method _do_ include the standard Zip CRC value. This, along with the fact that the vendor version stored in the AES extra data field is 0x0001 for AE-1 and 0x0002 for AE-2, is the only difference between the AE-1 and AE-2 formats.
|
||||
|
||||
**NOTE:** Zip utilities that support the AE-2 format are required to be able to read files that were created in the AE-1 format, and during decryption/extraction of files in AE-1 format should verify that the file's CRC matches the value stored in the CRC field.
|
||||
|
||||
4. AES extra data field
|
||||
1. A file encrypted with AES encryption will have a special "extra data" field associated with it. This extra data field is stored in both the local header and central directory entry for the file.
|
||||
|
||||
Note: see the Zip file format document referenced above for general information on the format and use of extra data fields.
|
||||
|
||||
2. The extra data header ID for AES encryption is 0x9901. The fields are all stored in Intel low-byte/high-byte order. The extra data field currently has a length of 11: seven data bytes plus two bytes for the header ID and two bytes for the data size. Therefore, the extra data overhead for each file in the archive is 22 bytes (11 bytes in the central header plus 11 bytes in the local header).
|
||||
3. The format of the data in the AES extra data field is as follows. See the notes below for additional information.
|
||||
|
||||
|**Offset**|**Size(bytes)**|**Content**|
|
||||
|:-|:-|:-|
|
||||
|0|2|Extra field header ID (0x9901)|
|
||||
|2|2|Data size (currently 7, but subject to possible increase in the future)|
|
||||
|4|2|Integer version number specific to the zip vendor|
|
||||
|6|2|2-character vendor ID|
|
||||
|8|1|Integer mode value indicating AES encryption strength|
|
||||
|9|2|The actual compression method used to compress the file|
|
||||
|
||||
4. Notes
|
||||
- **Data size**: this value is currently 7, but because it is possible that this specification will be modified in the future to store additional data in this extra field, vendors should not assume that it will always remain 7.
|
||||
- **Vendor ID**: the vendor ID field should always be set to the two ASCII characters "AE".
|
||||
- **Vendor version**: the vendor version for AE-1 is 0x0001. The vendor version for AE-2 is 0x0002.
|
||||
|
||||
Zip utilities that support AE-2 must also be able to process files that are encrypted in AE-1 format. The handling of the [CRC value](https://www.winzip.com/aes_info.htm#CRC) is the only difference between the AE-1 and AE-2 formats.
|
||||
|
||||
- **Encryption strength**: the mode values (encryption strength) for AE-1 and AE-2 are:
|
||||
|
||||
|**Value**|**Strength**|
|
||||
|-|-|
|
||||
|0x01|128-bit encryption key|
|
||||
|0x02|192-bit encryption key|
|
||||
|0x03|256-bit encryption key|
|
||||
|
||||
The encryption specification supports _only_ 128-, 192-, and 256-bit encryption keys. No other key lengths are permitted.
|
||||
|
||||
(Note: the current version of WinZip does not support encrypting files using 192-bit keys. This specification, however, does provide for the use of 192-bit keys, and WinZip is able to decrypt such files.)
|
||||
|
||||
- **Compression method**: the compression method is the one that would otherwise have been stored in the local and central headers for the file. For example, if the file is imploded, this field will contain the compression code 6. This is needed because a compression method of 99 is used to indicate the presence of an AES-encrypted file (see [above](https://www.winzip.com/aes_info.htm#comp-method)).
|
||||
|
||||
**III. Encrypted file storage format**
|
||||
|
||||
1. File format
|
||||
|
||||
Additional overhead data required for decryption is stored with the encrypted file itself (i.e., not in the headers). The actual format of the stored file is as follows; additional information about these fields is below. All fields are byte-aligned.
|
||||
|
||||
|**Size (bytes)**|**Content**|
|
||||
|:-|:-|
|
||||
|Variable|Salt value|
|
||||
|2|Password verification value|
|
||||
|Variable|Encrypted file data|
|
||||
|10|Authentication code|
|
||||
|
||||
Note that the value in the "compressed size" fields of the local file header and the central directory entry is the total size of all the items listed above. In other words, it is the total size of the salt value, password verification value, encrypted data, and authentication code.
|
||||
|
||||
2. Salt value
|
||||
|
||||
The "salt" or "salt value" is a random or pseudo-random sequence of bytes that is combined with the encryption password to create encryption and authentication keys. The salt is generated by the encrypting application and is stored unencrypted with the file data. The addition of salt values to passwords provides a number of security benefits and makes dictionary attacks based on precomputed keys much more difficult.
|
||||
|
||||
Good cryptographic practice requires that a different salt value be used for each of multiple files encrypted with the same password. If two files are encrypted with the same password and salt, they can leak information about each other. For example, it is possible to determine whether two files encrypted with the same password and salt are identical, and an attacker who somehow already knows the contents of one of two files encrypted with the same password and salt can determine some or all of the contents of the other file. Therefore, you should make every effort to use a unique salt value for each file.
|
||||
|
||||
The size of the salt value depends on the length of the encryption key, as follows:
|
||||
|
||||
|**Key size**|**Salt size**|
|
||||
|:-|:-|
|
||||
|128 bits|8 bytes|
|
||||
|192 bits|12 bytes|
|
||||
|256 bits|16 bytes|
|
||||
|
||||
3. Password verification value
|
||||
|
||||
This two-byte value is produced as part of the process that derives the encryption and decryption keys from the password. When encrypting, a verification value is derived from the encryption password and stored with the encrypted file. Before decrypting, a verification value can be derived from the decryption password and compared to the value stored with the file, serving as a quick check that will detect _most_, but not all, incorrect passwords. There is a 1 in 65,536 chance that an incorrect password will yield a matching verification value; therefore, a matching verification value cannot be absolutely relied on to indicate a correct password.
|
||||
|
||||
Information on how to obtain the password verification value from Dr. Gladman's encryption library can be found on the [coding tips](https://www.winzip.com/aes_tips.html) page.
|
||||
|
||||
This value is stored unencrypted.
|
||||
|
||||
4. Encrypted file data
|
||||
|
||||
Encryption is applied only to the content of files. It is performed after compression, and not to any other associated data. The file data is encrypted byte-for-byte using the AES encryption algorithm operating in "CTR" mode, which means that the lengths of the compressed data and the compressed, encrypted data are the same.
|
||||
|
||||
It is important for implementors to note that, although the data is encrypted byte-for-byte, it is presented to the encryption and decryption functions in blocks. The block size used for encryption and decryption must be the same. To be compatible with the encryption specification, this block size must be 16 bytes (although the last block may be smaller).
|
||||
|
||||
5. Authentication code
|
||||
|
||||
Authentication provides a high quality check that the contents of an encrypted file have not been inadvertently changed or deliberately tampered with since they were first encrypted. In effect, this is a super-CRC check on the data in the file _after_ compression and encryption. (Additionally, authentication is essential when using CTR mode encryption because this mode is vulnerable to several trivial attacks in its absence.)
|
||||
|
||||
The authentication code is derived from the output of the encryption process. [Dr. Gladman's AES code](https://www.winzip.com/aes_info.htm#encryption) provides this service, and information about how to obtain it is in the [coding tips](https://www.winzip.com/aes_tips.html).
|
||||
|
||||
The authentication code is stored unencrypted. It is byte-aligned and immediately follows the last byte of encrypted data.
|
||||
|
||||
For more discussion about authentication, see the [authentication code FAQ](https://www.winzip.com/aes_info.htm#auth-faq) below.
|
||||
|
||||
|
||||
**IV. Changes in WinZip 11**
|
||||
|
||||
Beginning with WinZip 11, WinZip makes a change in its use of the AE-1 and AE-2 file formats. The file formats themselves have not changed, and AES-encrypted files created by WinZip 11 are completely compatible with version 1.02 the WinZip AES encryption specification, which was published in January 2004.
|
||||
|
||||
[WinZip 9.0](https://www.winzip.com/win/en/winzip-9.html) and [WinZip 10.0](https://www.winzip.com/win/en/winzip-10.html) stored all AES-encrypted files using the AE-2 file format, which does not store the encrypted file's CRC. WinZip 11 instead uses the AE-1 file format, which does store the CRC, for most files. This provides an extra integrity check against the possibility of hardware or software errors that occur during the actual process of [file compression/encryption](https://www.winzip.com/win/en/features/data-protection.html) or decryption/decompression. For more information on this point, see the discussion of the [CRC](https://www.winzip.com/aes_info.htm#crc-faq) below.
|
||||
|
||||
Because for some very small files the CRC can be used to determine the exact contents of a file, regardless of the encryption method used, WinZip 11 continues to use the AE-2 file format, with no CRC stored, for files with an uncompressed size of less than 20 bytes. WinZip 11 also uses the AE-2 file format for files compressed in BZIP2 format, because the BZIP2 format contains its own integrity checks equivalent to those provided by the Zip format's CRC.
|
||||
|
||||
Other vendors who support WinZip's AES encryption specification may want to consider making a similar change to their own implementations of the specification, to get the benefit of the extra integrity check that it provides.
|
||||
|
||||
Note that the January 2004 version of the WinZip AE-2 specification, version 1.0.2, already required that all utilities that implemented the AE-2 format also be able to process files in AE-1 format, and should check on decryption/extraction of those files that the CRC was correct.
|
||||
|
||||
**V. Notes**
|
||||
|
||||
1. Non-files and zero-length files
|
||||
|
||||
To reduce Zip file size, it is recommended that non-file entries such as folder/directory entries not be encrypted. This, however, is only a recommendation; it is permissible to encrypt or not encrypt these entries, as you prefer.
|
||||
|
||||
On the other hand, it is recommended that you _do_ encrypt zero-length files. The presence of both encrypted and unencrypted files in a Zip file may trigger user warnings in some Zip file utilities, so the user experience may be improved if all files (including zero-length files) are encrypted.
|
||||
|
||||
If zero-length files are encrypted, the encrypted data portion of the file storage (see [above](https://www.winzip.com/aes_info.htm#encrypted-data)) will be empty, but the remainder of the encryption overhead data must be present, both in the file storage area and in the local and central headers.
|
||||
|
||||
2. "Mixed" Zip files
|
||||
|
||||
There is no requirement that all files in a Zip file be encrypted or that all files that _are_ encrypted use the same encryption method or the same password.
|
||||
|
||||
A Zip file can contain any combination of unencrypted files and files encrypted with any of the four currently defined encryption methods (Zip 2.0, AES-128, AES-192, AES-256). Encrypted files may use the same password or different passwords.
|
||||
|
||||
3. Key Generation
|
||||
|
||||
Key derivation, as used by AE-1 and AE-2 and as implemented in Dr. Gladman's library, is done according to the PBKDF2 algorithm, which is described in the [RFC2898](http://www.faqs.org/rfcs/rfc2898.html) guidelines. An iteration count of 1000 is used. An appropriate number of bits from the resulting hash value are used to compose three output values: an encryption key, an authentication key, and a password verification value. The first _n_ bits become the encryption key, the next _m_ bits become the authentication key, and the last 16 bits (two bytes) become the password verification value.
|
||||
|
||||
As part of the process outlined in RFC 2898 a pseudo-random function must be called; AE-2 uses the HMAC-SHA1 function, since it is a well-respected algorithm that has been in wide use for this purpose for several years.
|
||||
|
||||
Note that, when used in connection with 192- or 256-bit AES encryption, the fact that HMAC-SHA1 produces a 160-bit result means that, regardless of the password that you specify, the search space for the encryption key is unlikely to reach the theoretical 192- or 256-bit maximum, and cannot be guaranteed to exceed 160 bits. This is discussed in section B.1.1 of the [RFC2898 specification](http://www.faqs.org/rfcs/rfc2898.html).
|
||||
|
||||
|
||||
**VI. FAQs**
|
||||
|
||||
- **Why is the compression method field used to indicate AES encryption?**
|
||||
|
||||
As opposed to using new _version made by_ and _version needed to extract_ values to signal AES encryption for a file, the new compression method is more likely to be handled gracefully by older versions of existing Zip file utilities. Zip file utilities typically do not attempt to extract files compressed with unknown methods, presumably notifying the user with an appropriate message.
|
||||
|
||||
- **How can I guarantee that the salt value is unique?**
|
||||
|
||||
In principle, the value of the salt should be different whenever the same password is used more than once, for the reasons described [above](https://www.winzip.com/aes_info.htm#salt), but this is difficult to guarantee.
|
||||
|
||||
In practice, the number of bytes in the salt (as specified by AE-1 and AE-2) is such that using a pseudo-random value will ensure that the probability of duplicated salt values is very low and can be safely ignored.
|
||||
|
||||
There is one exception to this: With the 8-byte salt values used with WinZip's 128-bit encryption it is likely that, if approximately 4 billion files are encrypted with the same password, two of the files will have the same salt, so it is advisable to stay well below this limit. Because of this, when using the same password to encrypt very large numbers of files in WinZip's AES encryption format (that is, files totalling in the millions, for example 2000 Zip files, each containing 1000 encrypted files), we recommend the use of 192-bit or 256-bit AES keys, with their 12- and 16-byte salt values, rather than 128-bit AES keys, with their 8-byte salt values.
|
||||
|
||||
Although salt values do not need to be truly random, it is important that they be generated in a way that the probability of duplicated salt values is not significantly higher than that which would be expected if truly random values were being used.
|
||||
|
||||
One technique for generating salt values is presented in the [coding tips](https://www.winzip.com/aes_tips.html#prng) page.
|
||||
|
||||
- **Why is there an authentication code?**
|
||||
|
||||
The purpose of the authentication code is to insure that, once a file's data has been compressed and encrypted, any accidental corruption of the encrypted data, and any deliberate attempts to modify the encrypted data by an attacker who does not know the password, can be detected.
|
||||
|
||||
The current consensus in the cryptographic community is that associating a message authentication code (or MAC) with encrypted data has strong security value because it makes a number of attacks more difficult to engineer. For AES CTR mode encryption in particular, a MAC is especially important because a number of trivial attacks are possible in its absence. The MAC used with WinZip's AES encryption is based on[HMAC-SHA1-80](http://www.faqs.org/rfcs/rfc2104.html), a mature and widely respected authentication algorithm.
|
||||
|
||||
The MAC is calculated after the file data has been compressed and encrypted. This order of calculation is referred to as Encrypt-then-MAC, and is preferred by many cryptographers to the alternative order of MAC-then-Encrypt because Encrypt-then-MAC is immune to some known attacks on MAC-then-Encrypt.
|
||||
|
||||
- **What is the role of the CRC in WinZip 11?**
|
||||
|
||||
Within the Zip format, the primary use of the CRC value is to detect accidental corruption of data that has been stored in the Zip file. With files encrypted according to the Zip 2.0 encryption specification, it also functions to some extent as a method of detecting deliberate attempts to modify the encrypted data, but not one that can be considered cryptographically strong. The CRC is not needed for these purposes with the WinZip AES encryption specification, where the HMAC-SHA1-based authentication code instead serves these roles.
|
||||
|
||||
The CRC has a drawback in that for very small files, such as files with four or fewer bytes, the CRC can be used, independent of the encryption algorithm, to determine the unencrypted contents of the file. And, in general, it is preferable to store as little information as possible about the encrypted file in the unencrypted Zip headers.
|
||||
|
||||
The CRC does serve one purpose that the authentication code does not. The CRC is computed based on the original uncompressed, unencrypted contents of the file, and it is checked after the file has been decrypted and decompressed. In contrast, the authentication code used with WinZip AES encryption is computed after compression/encryption and it is checked before decryption/decompression. In the very rare event of a hardware or software error that corrupts data during compression and encryption, or during decryption and decompression, the CRC will catch the error, but the authentication code will not.
|
||||
|
||||
WinZip 9.0 and WinZip 10.0 used AE-2 for all files that they created, and did not store the CRC. As of WinZip 11, WinZip instead uses AE-1 for most files, storing the CRC as an additional integrity check against hardware or software errors occurring during the actual compression/encryption or decryption/decompression processes. WinZip 11 will continue to use AE-2, with no CRC, for very small files of less than 20 bytes. It will also use AE-2 for files compressed in BZIP2 format, because this format has internal integrity checks equivalent to a CRC check built in.
|
||||
|
||||
Note that the AES-encrypted files created by WinZip 11 are fully compatible with January 2004's version 1.0.2 of the WinZip AES encryption specification, in which both the AE-1 and AE-2 variants of the file format were already defined.
|
||||
|
||||
|
||||
**VII. Change history**
|
||||
|
||||
**Changes made in document version 1.04, January, 2009:** Minor clarification regarding the algorithm used to generate the authentication code.
|
||||
|
||||
**Changes made in document version 1.03, November, 2006:** Minor editorial and clarifying changes have been made throughout the document. The following substantive technical changes have been made:
|
||||
|
||||
1. WinZip 11 Usage of AE-1 and AE-2
|
||||
|
||||
WinZip's AES encryption specification defines two formats, known as AE-1 and AE-2, which differ in whether the CRC of the encrypted file is stored in the Zip headers. While the file formats themselves remain unchanged, WinZip's usage of them is changing. Beginning with [WinZip 11](https://www.winzip.com/aes_info.htm#winzip11), WinZip uses the AE-1 format, which includes the CRC of the encrypted file, for many encrypted files, in order to provide an additional integrity check against hardware or software errors occurring during the compression/encryption or decryption/decompression processes. Note that AES-encrypted files created by WinZip 11 are completely compatible with the previous version of the WinZip encryption specification, January 2004's version 1.0.2.
|
||||
|
||||
2. The discussion of [salt values](https://www.winzip.com/aes_info.htm#salt) mentions a limitation that applies to the uniqueness of salt values when very large numbers of files are encrypted with 128-bit encryption.
|
||||
3. Older versions of this specification suggested that other vendors might want to use their own vendor IDs to create their own unique encryption formats. We no longer suggest that vendor-specific alternative encryption methods be created in this way.
|
||||
|
||||
**Changes made in document version 1.02, January, 2004:** The introductory text at the start of the document has been rewritten, and minor editorial and clarifying changes have been made throughout the document. Two substantive technical changes have been made:
|
||||
|
||||
1. AE-2 Specification
|
||||
|
||||
Standard Zip files store the CRC of each file's unencrypted data. This value is used to help detect damage or other alterations to Zip files. However, storing the CRC value has a drawback in that, for a very small file, such as a file of four or fewer bytes, the CRC value can be used, independent of the encryption algorithm, to help determine the unencrypted contents of the file.
|
||||
|
||||
Because of this, files encrypted with the new AE-2 method store a 0 in the CRC field of the Zip header, and use the [authentication code](https://www.winzip.com/aes_info.htm#authentication-code) instead of the CRC value to verify that encrypted data within the Zip file has not been corrupted.
|
||||
|
||||
The only differences between the AE-1 and AE-2 methods are the storage in AE-2 of 0 instead of the CRC in the Zip file header,and the use in the AES extra data field of 0x0002 for AE-2 instead of 0x0001 for AE-1 as the vendor version.
|
||||
|
||||
Zip utilities that support the AE-2 format are required to be able to read files that were created in the AE-1 format, and during decryption/extraction of files in AE-1 format should verify that the file's CRC matches the value stored in the CRC field.
|
||||
|
||||
2. Key Generation and HMAC-SHA1
|
||||
|
||||
The description of the [key generation](https://www.winzip.com/aes_info.htm#key-generation) mechanism has been updated to point out a limitation arising from its use of HMAC-SHA1 as the pseudo-random function: When used in connection with 192- or 256-bit AES encryption, the fact that HMAC-SHA1 produces a 160-bit result means that, regardless of the password that you specify, the search space for the encryption key is unlikely to reach the theoretical 192- or 256-bit maximum, and cannot be guaranteed to exceed 160 bits. This is discussed in section B.1.1 of the [RFC2898 specification](http://www.faqs.org/rfcs/rfc2898.html).
|
||||
|
||||
Document version: 1.04
|
||||
Last modified: January 30, 2009
|
||||
|
||||
Copyright© 2003-2018 Corel Corporation.
|
||||
All Rights Reserved
|
||||
@@ -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
|
||||
@@ -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,182 +0,0 @@
|
||||
/*
|
||||
---------------------------------------------------------------------------
|
||||
Copyright (c) 1998-2010, Brian Gladman, Worcester, UK. All rights reserved.
|
||||
|
||||
The redistribution and use of this software (with or without changes)
|
||||
is allowed without the payment of fees or royalties provided that:
|
||||
|
||||
source code distributions include the above copyright notice, this
|
||||
list of conditions and the following disclaimer;
|
||||
|
||||
binary distributions include the above copyright notice, this list
|
||||
of conditions and the following disclaimer in their documentation.
|
||||
|
||||
This software is provided 'as is' with no explicit or implied warranties
|
||||
in respect of its operation, including, but not limited to, correctness
|
||||
and fitness for purpose.
|
||||
---------------------------------------------------------------------------
|
||||
Issue Date: 20/12/2007
|
||||
|
||||
This is an implementation of RFC2898, which specifies key derivation from
|
||||
a password and a salt value.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include "hmac.h"
|
||||
#include "pwd2key.h"
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
void derive_key(const unsigned char pwd[], /* the PASSWORD */
|
||||
unsigned int pwd_len, /* and its length */
|
||||
const unsigned char salt[], /* the SALT and its */
|
||||
unsigned int salt_len, /* length */
|
||||
unsigned int iter, /* the number of iterations */
|
||||
unsigned char key[], /* space for the output key */
|
||||
unsigned int key_len)/* and its required length */
|
||||
{
|
||||
unsigned int i, j, k, n_blk, h_size;
|
||||
unsigned char uu[HMAC_MAX_OUTPUT_SIZE], ux[HMAC_MAX_OUTPUT_SIZE];
|
||||
hmac_ctx c1[1], c2[1], c3[1];
|
||||
|
||||
/* set HMAC context (c1) for password */
|
||||
h_size = hmac_sha_begin(HMAC_SHA1, c1);
|
||||
hmac_sha_key(pwd, pwd_len, c1);
|
||||
|
||||
/* set HMAC context (c2) for password and salt */
|
||||
memcpy(c2, c1, sizeof(hmac_ctx));
|
||||
hmac_sha_data(salt, salt_len, c2);
|
||||
|
||||
/* find the number of SHA blocks in the key */
|
||||
n_blk = 1 + (key_len - 1) / h_size;
|
||||
|
||||
for(i = 0; i < n_blk; ++i) /* for each block in key */
|
||||
{
|
||||
/* ux[] holds the running xor value */
|
||||
memset(ux, 0, h_size);
|
||||
|
||||
/* set HMAC context (c3) for password and salt */
|
||||
memcpy(c3, c2, sizeof(hmac_ctx));
|
||||
|
||||
/* enter additional data for 1st block into uu */
|
||||
uu[0] = (unsigned char)((i + 1) >> 24);
|
||||
uu[1] = (unsigned char)((i + 1) >> 16);
|
||||
uu[2] = (unsigned char)((i + 1) >> 8);
|
||||
uu[3] = (unsigned char)(i + 1);
|
||||
|
||||
/* this is the key mixing iteration */
|
||||
for(j = 0, k = 4; j < iter; ++j)
|
||||
{
|
||||
/* add previous round data to HMAC */
|
||||
hmac_sha_data(uu, k, c3);
|
||||
|
||||
/* obtain HMAC for uu[] */
|
||||
hmac_sha_end(uu, h_size, c3);
|
||||
|
||||
/* xor into the running xor block */
|
||||
for(k = 0; k < h_size; ++k)
|
||||
ux[k] ^= uu[k];
|
||||
|
||||
/* set HMAC context (c3) for password */
|
||||
memcpy(c3, c1, sizeof(hmac_ctx));
|
||||
}
|
||||
|
||||
/* compile key blocks into the key output */
|
||||
j = 0; k = i * h_size;
|
||||
while(j < h_size && k < key_len)
|
||||
key[k++] = ux[j++];
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef TEST
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
struct
|
||||
{ unsigned int pwd_len;
|
||||
unsigned int salt_len;
|
||||
unsigned int it_count;
|
||||
unsigned char *pwd;
|
||||
unsigned char salt[32];
|
||||
unsigned char key[32];
|
||||
} tests[] =
|
||||
{
|
||||
{ 8, 4, 5, (unsigned char*)"password",
|
||||
{
|
||||
0x12, 0x34, 0x56, 0x78
|
||||
},
|
||||
{
|
||||
0x5c, 0x75, 0xce, 0xf0, 0x1a, 0x96, 0x0d, 0xf7,
|
||||
0x4c, 0xb6, 0xb4, 0x9b, 0x9e, 0x38, 0xe6, 0xb5
|
||||
}
|
||||
},
|
||||
{ 8, 8, 5, (unsigned char*)"password",
|
||||
{
|
||||
0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12
|
||||
},
|
||||
{
|
||||
0xd1, 0xda, 0xa7, 0x86, 0x15, 0xf2, 0x87, 0xe6,
|
||||
0xa1, 0xc8, 0xb1, 0x20, 0xd7, 0x06, 0x2a, 0x49
|
||||
}
|
||||
},
|
||||
{ 8, 21, 1, (unsigned char*)"password",
|
||||
{
|
||||
"ATHENA.MIT.EDUraeburn"
|
||||
},
|
||||
{
|
||||
0xcd, 0xed, 0xb5, 0x28, 0x1b, 0xb2, 0xf8, 0x01,
|
||||
0x56, 0x5a, 0x11, 0x22, 0xb2, 0x56, 0x35, 0x15
|
||||
}
|
||||
},
|
||||
{ 8, 21, 2, (unsigned char*)"password",
|
||||
{
|
||||
"ATHENA.MIT.EDUraeburn"
|
||||
},
|
||||
{
|
||||
0x01, 0xdb, 0xee, 0x7f, 0x4a, 0x9e, 0x24, 0x3e,
|
||||
0x98, 0x8b, 0x62, 0xc7, 0x3c, 0xda, 0x93, 0x5d
|
||||
}
|
||||
},
|
||||
{ 8, 21, 1200, (unsigned char*)"password",
|
||||
{
|
||||
"ATHENA.MIT.EDUraeburn"
|
||||
},
|
||||
{
|
||||
0x5c, 0x08, 0xeb, 0x61, 0xfd, 0xf7, 0x1e, 0x4e,
|
||||
0x4e, 0xc3, 0xcf, 0x6b, 0xa1, 0xf5, 0x51, 0x2b
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{ unsigned int i, j, key_len = 256;
|
||||
unsigned char key[256];
|
||||
|
||||
printf("\nTest of RFC2898 Password Based Key Derivation");
|
||||
for(i = 0; i < 5; ++i)
|
||||
{
|
||||
derive_key(tests[i].pwd, tests[i].pwd_len, tests[i].salt,
|
||||
tests[i].salt_len, tests[i].it_count, key, key_len);
|
||||
|
||||
printf("\ntest %i: ", i + 1);
|
||||
printf("key %s", memcmp(tests[i].key, key, 16) ? "is bad" : "is good");
|
||||
for(j = 0; j < key_len && j < 64; j += 4)
|
||||
{
|
||||
if(j % 16 == 0)
|
||||
printf("\n");
|
||||
printf("0x%02x%02x%02x%02x ", key[j], key[j + 1], key[j + 2], key[j + 3]);
|
||||
}
|
||||
printf(j < key_len ? " ... \n" : "\n");
|
||||
}
|
||||
printf("\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,45 +0,0 @@
|
||||
/*
|
||||
---------------------------------------------------------------------------
|
||||
Copyright (c) 1998-2010, Brian Gladman, Worcester, UK. All rights reserved.
|
||||
|
||||
The redistribution and use of this software (with or without changes)
|
||||
is allowed without the payment of fees or royalties provided that:
|
||||
|
||||
source code distributions include the above copyright notice, this
|
||||
list of conditions and the following disclaimer;
|
||||
|
||||
binary distributions include the above copyright notice, this list
|
||||
of conditions and the following disclaimer in their documentation.
|
||||
|
||||
This software is provided 'as is' with no explicit or implied warranties
|
||||
in respect of its operation, including, but not limited to, correctness
|
||||
and fitness for purpose.
|
||||
---------------------------------------------------------------------------
|
||||
Issue Date: 20/12/2007
|
||||
|
||||
This is an implementation of RFC2898, which specifies key derivation from
|
||||
a password and a salt value.
|
||||
*/
|
||||
|
||||
#ifndef PWD2KEY_H
|
||||
#define PWD2KEY_H
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
void derive_key(
|
||||
const unsigned char pwd[], /* the PASSWORD, and */
|
||||
unsigned int pwd_len, /* its length */
|
||||
const unsigned char salt[], /* the SALT and its */
|
||||
unsigned int salt_len, /* length */
|
||||
unsigned int iter, /* the number of iterations */
|
||||
unsigned char key[], /* space for the output key */
|
||||
unsigned int key_len); /* and its required length */
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -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
|
||||
|
||||
@@ -101,7 +101,7 @@ int hmac_sha_begin(enum hmac_hash hash, hmac_ctx cx[1])
|
||||
case HMAC_SHA512_128:
|
||||
cx->f_begin = (hf_begin *)sha512_128_begin;
|
||||
cx->f_hash = (hf_hash *)sha512_128_hash;
|
||||
cx->f_end = (hf_begin *)sha512_128_end;
|
||||
cx->f_end = (hf_end *)sha512_128_end;
|
||||
cx->input_len = SHA512_128_BLOCK_SIZE;
|
||||
cx->output_len = SHA512_128_DIGEST_SIZE;
|
||||
break;
|
||||
@@ -32,6 +32,7 @@ extern "C"
|
||||
#endif
|
||||
|
||||
#include "sha1.h"
|
||||
#include "sha2.h"
|
||||
|
||||
#if defined(SHA_224) || defined(SHA_256) || defined(SHA_384) || defined(SHA_512)
|
||||
#define HMAC_MAX_OUTPUT_SIZE SHA2_MAX_DIGEST_SIZE
|
||||
+949
@@ -0,0 +1,949 @@
|
||||
/*
|
||||
---------------------------------------------------------------------------
|
||||
Copyright (c) 1998-2010, Brian Gladman, Worcester, UK. All rights reserved.
|
||||
|
||||
The redistribution and use of this software (with or without changes)
|
||||
is allowed without the payment of fees or royalties provided that:
|
||||
|
||||
source code distributions include the above copyright notice, this
|
||||
list of conditions and the following disclaimer;
|
||||
|
||||
binary distributions include the above copyright notice, this list
|
||||
of conditions and the following disclaimer in their documentation.
|
||||
|
||||
This software is provided 'as is' with no explicit or implied warranties
|
||||
in respect of its operation, including, but not limited to, correctness
|
||||
and fitness for purpose.
|
||||
---------------------------------------------------------------------------
|
||||
Issue Date: 20/12/2007
|
||||
|
||||
This code implements sha256, sha384 and sha512 but the latter two
|
||||
functions rely on efficient 64-bit integer operations that may not be
|
||||
very efficient on 32-bit machines
|
||||
|
||||
The sha256 functions use a type 'sha256_ctx' to hold details of the
|
||||
current hash state and uses the following three calls:
|
||||
|
||||
void sha256_begin( sha256_ctx ctx[1] )
|
||||
void sha256_hash( const unsigned char data[],
|
||||
unsigned long len, sha256_ctx ctx[1] )
|
||||
void sha_end1( unsigned char hval[], sha256_ctx ctx[1] )
|
||||
|
||||
The first subroutine initialises a hash computation by setting up the
|
||||
context in the sha256_ctx context. The second subroutine hashes 8-bit
|
||||
bytes from array data[] into the hash state withinh sha256_ctx context,
|
||||
the number of bytes to be hashed being given by the the unsigned long
|
||||
integer len. The third subroutine completes the hash calculation and
|
||||
places the resulting digest value in the array of 8-bit bytes hval[].
|
||||
|
||||
The sha384 and sha512 functions are similar and use the interfaces:
|
||||
|
||||
void sha384_begin( sha384_ctx ctx[1] );
|
||||
void sha384_hash( const unsigned char data[],
|
||||
unsigned long len, sha384_ctx ctx[1] );
|
||||
void sha384_end( unsigned char hval[], sha384_ctx ctx[1] );
|
||||
|
||||
void sha512_begin( sha512_ctx ctx[1] );
|
||||
void sha512_hash( const unsigned char data[],
|
||||
unsigned long len, sha512_ctx ctx[1] );
|
||||
void sha512_end( unsigned char hval[], sha512_ctx ctx[1] );
|
||||
|
||||
In addition there is a function sha2 that can be used to call all these
|
||||
functions using a call with a hash length parameter as follows:
|
||||
|
||||
int sha2_begin( unsigned long len, sha2_ctx ctx[1] );
|
||||
void sha2_hash( const unsigned char data[],
|
||||
unsigned long len, sha2_ctx ctx[1] );
|
||||
void sha2_end( unsigned char hval[], sha2_ctx ctx[1] );
|
||||
|
||||
The data block length in any one call to any of these hash functions must
|
||||
be no more than 2^32 - 1 bits or 2^29 - 1 bytes.
|
||||
|
||||
My thanks to Erik Andersen <andersen@codepoet.org> for testing this code
|
||||
on big-endian systems and for his assistance with corrections
|
||||
*/
|
||||
|
||||
#if 1
|
||||
#define UNROLL_SHA2 /* for SHA2 loop unroll */
|
||||
#endif
|
||||
|
||||
#include <string.h> /* for memcpy() etc. */
|
||||
|
||||
#include "sha2.h"
|
||||
#include "brg_endian.h"
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#if defined( _MSC_VER ) && ( _MSC_VER > 800 )
|
||||
#pragma intrinsic(memcpy)
|
||||
#pragma intrinsic(memset)
|
||||
#endif
|
||||
|
||||
#if 0 && defined(_MSC_VER)
|
||||
#define rotl32 _lrotl
|
||||
#define rotr32 _lrotr
|
||||
#else
|
||||
#define rotl32(x,n) (((x) << n) | ((x) >> (32 - n)))
|
||||
#define rotr32(x,n) (((x) >> n) | ((x) << (32 - n)))
|
||||
#endif
|
||||
|
||||
#if !defined(bswap_32)
|
||||
#define bswap_32(x) ((rotr32((x), 24) & 0x00ff00ff) | (rotr32((x), 8) & 0xff00ff00))
|
||||
#endif
|
||||
|
||||
#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
|
||||
#define SWAP_BYTES
|
||||
#else
|
||||
#undef SWAP_BYTES
|
||||
#endif
|
||||
|
||||
#if 0
|
||||
|
||||
#define ch(x,y,z) (((x) & (y)) ^ (~(x) & (z)))
|
||||
#define maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
|
||||
|
||||
#else /* Thanks to Rich Schroeppel and Colin Plumb for the following */
|
||||
|
||||
#define ch(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
|
||||
#define maj(x,y,z) (((x) & (y)) | ((z) & ((x) ^ (y))))
|
||||
|
||||
#endif
|
||||
|
||||
/* round transforms for SHA256 and SHA512 compression functions */
|
||||
|
||||
#define vf(n,i) v[(n - i) & 7]
|
||||
|
||||
#define hf(i) (p[i & 15] += \
|
||||
g_1(p[(i + 14) & 15]) + p[(i + 9) & 15] + g_0(p[(i + 1) & 15]))
|
||||
|
||||
#define v_cycle(i,j) \
|
||||
vf(7,i) += (j ? hf(i) : p[i]) + k_0[i+j] \
|
||||
+ s_1(vf(4,i)) + ch(vf(4,i),vf(5,i),vf(6,i)); \
|
||||
vf(3,i) += vf(7,i); \
|
||||
vf(7,i) += s_0(vf(0,i))+ maj(vf(0,i),vf(1,i),vf(2,i))
|
||||
|
||||
#if defined(SHA_224) || defined(SHA_256)
|
||||
|
||||
#define SHA256_MASK (SHA256_BLOCK_SIZE - 1)
|
||||
|
||||
#if defined(SWAP_BYTES)
|
||||
#define bsw_32(p,n) \
|
||||
{ int _i = (n); while(_i--) ((uint32_t*)p)[_i] = bswap_32(((uint32_t*)p)[_i]); }
|
||||
#else
|
||||
#define bsw_32(p,n)
|
||||
#endif
|
||||
|
||||
#define s_0(x) (rotr32((x), 2) ^ rotr32((x), 13) ^ rotr32((x), 22))
|
||||
#define s_1(x) (rotr32((x), 6) ^ rotr32((x), 11) ^ rotr32((x), 25))
|
||||
#define g_0(x) (rotr32((x), 7) ^ rotr32((x), 18) ^ ((x) >> 3))
|
||||
#define g_1(x) (rotr32((x), 17) ^ rotr32((x), 19) ^ ((x) >> 10))
|
||||
#define k_0 k256
|
||||
|
||||
/* rotated SHA256 round definition. Rather than swapping variables as in */
|
||||
/* FIPS-180, different variables are 'rotated' on each round, returning */
|
||||
/* to their starting positions every eight rounds */
|
||||
|
||||
#define q(n) v##n
|
||||
|
||||
#define one_cycle(a,b,c,d,e,f,g,h,k,w) \
|
||||
q(h) += s_1(q(e)) + ch(q(e), q(f), q(g)) + k + w; \
|
||||
q(d) += q(h); q(h) += s_0(q(a)) + maj(q(a), q(b), q(c))
|
||||
|
||||
/* SHA256 mixing data */
|
||||
|
||||
const uint32_t k256[64] =
|
||||
{ 0x428a2f98ul, 0x71374491ul, 0xb5c0fbcful, 0xe9b5dba5ul,
|
||||
0x3956c25bul, 0x59f111f1ul, 0x923f82a4ul, 0xab1c5ed5ul,
|
||||
0xd807aa98ul, 0x12835b01ul, 0x243185beul, 0x550c7dc3ul,
|
||||
0x72be5d74ul, 0x80deb1feul, 0x9bdc06a7ul, 0xc19bf174ul,
|
||||
0xe49b69c1ul, 0xefbe4786ul, 0x0fc19dc6ul, 0x240ca1ccul,
|
||||
0x2de92c6ful, 0x4a7484aaul, 0x5cb0a9dcul, 0x76f988daul,
|
||||
0x983e5152ul, 0xa831c66dul, 0xb00327c8ul, 0xbf597fc7ul,
|
||||
0xc6e00bf3ul, 0xd5a79147ul, 0x06ca6351ul, 0x14292967ul,
|
||||
0x27b70a85ul, 0x2e1b2138ul, 0x4d2c6dfcul, 0x53380d13ul,
|
||||
0x650a7354ul, 0x766a0abbul, 0x81c2c92eul, 0x92722c85ul,
|
||||
0xa2bfe8a1ul, 0xa81a664bul, 0xc24b8b70ul, 0xc76c51a3ul,
|
||||
0xd192e819ul, 0xd6990624ul, 0xf40e3585ul, 0x106aa070ul,
|
||||
0x19a4c116ul, 0x1e376c08ul, 0x2748774cul, 0x34b0bcb5ul,
|
||||
0x391c0cb3ul, 0x4ed8aa4aul, 0x5b9cca4ful, 0x682e6ff3ul,
|
||||
0x748f82eeul, 0x78a5636ful, 0x84c87814ul, 0x8cc70208ul,
|
||||
0x90befffaul, 0xa4506cebul, 0xbef9a3f7ul, 0xc67178f2ul,
|
||||
};
|
||||
|
||||
/* Compile 64 bytes of hash data into SHA256 digest value */
|
||||
/* NOTE: this routine assumes that the byte order in the */
|
||||
/* ctx->wbuf[] at this point is such that low address bytes */
|
||||
/* in the ORIGINAL byte stream will go into the high end of */
|
||||
/* words on BOTH big and little endian systems */
|
||||
|
||||
VOID_RETURN sha256_compile(sha256_ctx ctx[1])
|
||||
{
|
||||
#if !defined(UNROLL_SHA2)
|
||||
|
||||
uint32_t j, *p = ctx->wbuf, v[8];
|
||||
|
||||
memcpy(v, ctx->hash, sizeof(ctx->hash));
|
||||
|
||||
for(j = 0; j < 64; j += 16)
|
||||
{
|
||||
v_cycle( 0, j); v_cycle( 1, j);
|
||||
v_cycle( 2, j); v_cycle( 3, j);
|
||||
v_cycle( 4, j); v_cycle( 5, j);
|
||||
v_cycle( 6, j); v_cycle( 7, j);
|
||||
v_cycle( 8, j); v_cycle( 9, j);
|
||||
v_cycle(10, j); v_cycle(11, j);
|
||||
v_cycle(12, j); v_cycle(13, j);
|
||||
v_cycle(14, j); v_cycle(15, j);
|
||||
}
|
||||
|
||||
ctx->hash[0] += v[0]; ctx->hash[1] += v[1];
|
||||
ctx->hash[2] += v[2]; ctx->hash[3] += v[3];
|
||||
ctx->hash[4] += v[4]; ctx->hash[5] += v[5];
|
||||
ctx->hash[6] += v[6]; ctx->hash[7] += v[7];
|
||||
|
||||
#else
|
||||
|
||||
uint32_t *p = ctx->wbuf,v0,v1,v2,v3,v4,v5,v6,v7;
|
||||
|
||||
v0 = ctx->hash[0]; v1 = ctx->hash[1];
|
||||
v2 = ctx->hash[2]; v3 = ctx->hash[3];
|
||||
v4 = ctx->hash[4]; v5 = ctx->hash[5];
|
||||
v6 = ctx->hash[6]; v7 = ctx->hash[7];
|
||||
|
||||
one_cycle(0,1,2,3,4,5,6,7,k256[ 0],p[ 0]);
|
||||
one_cycle(7,0,1,2,3,4,5,6,k256[ 1],p[ 1]);
|
||||
one_cycle(6,7,0,1,2,3,4,5,k256[ 2],p[ 2]);
|
||||
one_cycle(5,6,7,0,1,2,3,4,k256[ 3],p[ 3]);
|
||||
one_cycle(4,5,6,7,0,1,2,3,k256[ 4],p[ 4]);
|
||||
one_cycle(3,4,5,6,7,0,1,2,k256[ 5],p[ 5]);
|
||||
one_cycle(2,3,4,5,6,7,0,1,k256[ 6],p[ 6]);
|
||||
one_cycle(1,2,3,4,5,6,7,0,k256[ 7],p[ 7]);
|
||||
one_cycle(0,1,2,3,4,5,6,7,k256[ 8],p[ 8]);
|
||||
one_cycle(7,0,1,2,3,4,5,6,k256[ 9],p[ 9]);
|
||||
one_cycle(6,7,0,1,2,3,4,5,k256[10],p[10]);
|
||||
one_cycle(5,6,7,0,1,2,3,4,k256[11],p[11]);
|
||||
one_cycle(4,5,6,7,0,1,2,3,k256[12],p[12]);
|
||||
one_cycle(3,4,5,6,7,0,1,2,k256[13],p[13]);
|
||||
one_cycle(2,3,4,5,6,7,0,1,k256[14],p[14]);
|
||||
one_cycle(1,2,3,4,5,6,7,0,k256[15],p[15]);
|
||||
|
||||
one_cycle(0,1,2,3,4,5,6,7,k256[16],hf( 0));
|
||||
one_cycle(7,0,1,2,3,4,5,6,k256[17],hf( 1));
|
||||
one_cycle(6,7,0,1,2,3,4,5,k256[18],hf( 2));
|
||||
one_cycle(5,6,7,0,1,2,3,4,k256[19],hf( 3));
|
||||
one_cycle(4,5,6,7,0,1,2,3,k256[20],hf( 4));
|
||||
one_cycle(3,4,5,6,7,0,1,2,k256[21],hf( 5));
|
||||
one_cycle(2,3,4,5,6,7,0,1,k256[22],hf( 6));
|
||||
one_cycle(1,2,3,4,5,6,7,0,k256[23],hf( 7));
|
||||
one_cycle(0,1,2,3,4,5,6,7,k256[24],hf( 8));
|
||||
one_cycle(7,0,1,2,3,4,5,6,k256[25],hf( 9));
|
||||
one_cycle(6,7,0,1,2,3,4,5,k256[26],hf(10));
|
||||
one_cycle(5,6,7,0,1,2,3,4,k256[27],hf(11));
|
||||
one_cycle(4,5,6,7,0,1,2,3,k256[28],hf(12));
|
||||
one_cycle(3,4,5,6,7,0,1,2,k256[29],hf(13));
|
||||
one_cycle(2,3,4,5,6,7,0,1,k256[30],hf(14));
|
||||
one_cycle(1,2,3,4,5,6,7,0,k256[31],hf(15));
|
||||
|
||||
one_cycle(0,1,2,3,4,5,6,7,k256[32],hf( 0));
|
||||
one_cycle(7,0,1,2,3,4,5,6,k256[33],hf( 1));
|
||||
one_cycle(6,7,0,1,2,3,4,5,k256[34],hf( 2));
|
||||
one_cycle(5,6,7,0,1,2,3,4,k256[35],hf( 3));
|
||||
one_cycle(4,5,6,7,0,1,2,3,k256[36],hf( 4));
|
||||
one_cycle(3,4,5,6,7,0,1,2,k256[37],hf( 5));
|
||||
one_cycle(2,3,4,5,6,7,0,1,k256[38],hf( 6));
|
||||
one_cycle(1,2,3,4,5,6,7,0,k256[39],hf( 7));
|
||||
one_cycle(0,1,2,3,4,5,6,7,k256[40],hf( 8));
|
||||
one_cycle(7,0,1,2,3,4,5,6,k256[41],hf( 9));
|
||||
one_cycle(6,7,0,1,2,3,4,5,k256[42],hf(10));
|
||||
one_cycle(5,6,7,0,1,2,3,4,k256[43],hf(11));
|
||||
one_cycle(4,5,6,7,0,1,2,3,k256[44],hf(12));
|
||||
one_cycle(3,4,5,6,7,0,1,2,k256[45],hf(13));
|
||||
one_cycle(2,3,4,5,6,7,0,1,k256[46],hf(14));
|
||||
one_cycle(1,2,3,4,5,6,7,0,k256[47],hf(15));
|
||||
|
||||
one_cycle(0,1,2,3,4,5,6,7,k256[48],hf( 0));
|
||||
one_cycle(7,0,1,2,3,4,5,6,k256[49],hf( 1));
|
||||
one_cycle(6,7,0,1,2,3,4,5,k256[50],hf( 2));
|
||||
one_cycle(5,6,7,0,1,2,3,4,k256[51],hf( 3));
|
||||
one_cycle(4,5,6,7,0,1,2,3,k256[52],hf( 4));
|
||||
one_cycle(3,4,5,6,7,0,1,2,k256[53],hf( 5));
|
||||
one_cycle(2,3,4,5,6,7,0,1,k256[54],hf( 6));
|
||||
one_cycle(1,2,3,4,5,6,7,0,k256[55],hf( 7));
|
||||
one_cycle(0,1,2,3,4,5,6,7,k256[56],hf( 8));
|
||||
one_cycle(7,0,1,2,3,4,5,6,k256[57],hf( 9));
|
||||
one_cycle(6,7,0,1,2,3,4,5,k256[58],hf(10));
|
||||
one_cycle(5,6,7,0,1,2,3,4,k256[59],hf(11));
|
||||
one_cycle(4,5,6,7,0,1,2,3,k256[60],hf(12));
|
||||
one_cycle(3,4,5,6,7,0,1,2,k256[61],hf(13));
|
||||
one_cycle(2,3,4,5,6,7,0,1,k256[62],hf(14));
|
||||
one_cycle(1,2,3,4,5,6,7,0,k256[63],hf(15));
|
||||
|
||||
ctx->hash[0] += v0; ctx->hash[1] += v1;
|
||||
ctx->hash[2] += v2; ctx->hash[3] += v3;
|
||||
ctx->hash[4] += v4; ctx->hash[5] += v5;
|
||||
ctx->hash[6] += v6; ctx->hash[7] += v7;
|
||||
#endif
|
||||
}
|
||||
|
||||
/* SHA256 hash data in an array of bytes into hash buffer */
|
||||
/* and call the hash_compile function as required. */
|
||||
|
||||
VOID_RETURN sha256_hash(const unsigned char data[], unsigned long len, sha256_ctx ctx[1])
|
||||
{ uint32_t pos = (uint32_t)((ctx->count[0] >> 3) & SHA256_MASK);
|
||||
const unsigned char *sp = data;
|
||||
unsigned char *w = (unsigned char*)ctx->wbuf;
|
||||
#if SHA2_BITS == 1
|
||||
uint32_t ofs = (ctx->count[0] & 7);
|
||||
#else
|
||||
len <<= 3;
|
||||
#endif
|
||||
if((ctx->count[0] += len) < len)
|
||||
++(ctx->count[1]);
|
||||
|
||||
#if SHA2_BITS == 1
|
||||
if(ofs) /* if not on a byte boundary */
|
||||
{
|
||||
if(ofs + len < 8) /* if no added bytes are needed */
|
||||
{
|
||||
w[pos] |= (*sp >> ofs);
|
||||
}
|
||||
else /* otherwise and add bytes */
|
||||
{ unsigned char part = w[pos];
|
||||
|
||||
while((int)(ofs + (len -= 8)) >= 0)
|
||||
{
|
||||
w[pos++] = part | (*sp >> ofs);
|
||||
part = *sp++ << (8 - ofs);
|
||||
if(pos == SHA256_BLOCK_SIZE)
|
||||
{
|
||||
bsw_32(w, SHA256_BLOCK_SIZE >> 2);
|
||||
sha256_compile(ctx); pos = 0;
|
||||
}
|
||||
}
|
||||
|
||||
w[pos] = part;
|
||||
}
|
||||
}
|
||||
else /* data is byte aligned */
|
||||
#endif
|
||||
{ uint32_t space = SHA256_BLOCK_SIZE - pos;
|
||||
|
||||
while(len >= (space << 3))
|
||||
{
|
||||
memcpy(w + pos, sp, space);
|
||||
bsw_32(w, SHA256_BLOCK_SIZE >> 2);
|
||||
sha256_compile(ctx);
|
||||
sp += space; len -= (space << 3);
|
||||
space = SHA256_BLOCK_SIZE; pos = 0;
|
||||
}
|
||||
memcpy(w + pos, sp, (len + 7 * SHA2_BITS) >> 3);
|
||||
}
|
||||
}
|
||||
|
||||
/* SHA256 Final padding and digest calculation */
|
||||
|
||||
static void sha_end1(unsigned char hval[], sha256_ctx ctx[1], const unsigned int hlen)
|
||||
{ uint32_t i = (uint32_t)((ctx->count[0] >> 3) & SHA256_MASK), m1;
|
||||
|
||||
/* put bytes in the buffer in an order in which references to */
|
||||
/* 32-bit words will put bytes with lower addresses into the */
|
||||
/* top of 32 bit words on BOTH big and little endian machines */
|
||||
bsw_32(ctx->wbuf, (i + 3 + SHA2_BITS) >> 2)
|
||||
|
||||
/* we now need to mask valid bytes and add the padding which is */
|
||||
/* a single 1 bit and as many zero bits as necessary. Note that */
|
||||
/* we can always add the first padding byte here because the */
|
||||
/* buffer always has at least one empty slot */
|
||||
m1 = (unsigned char)0x80 >> (ctx->count[0] & 7);
|
||||
ctx->wbuf[i >> 2] &= ((0xffffff00 | (~m1 + 1)) << 8 * (~i & 3));
|
||||
ctx->wbuf[i >> 2] |= (m1 << 8 * (~i & 3));
|
||||
|
||||
/* we need 9 or more empty positions, one for the padding byte */
|
||||
/* (above) and eight for the length count. If there is not */
|
||||
/* enough space pad and empty the buffer */
|
||||
if(i > SHA256_BLOCK_SIZE - 9)
|
||||
{
|
||||
if(i < 60) ctx->wbuf[15] = 0;
|
||||
sha256_compile(ctx);
|
||||
i = 0;
|
||||
}
|
||||
else /* compute a word index for the empty buffer positions */
|
||||
i = (i >> 2) + 1;
|
||||
|
||||
while(i < 14) /* and zero pad all but last two positions */
|
||||
ctx->wbuf[i++] = 0;
|
||||
|
||||
/* the following 32-bit length fields are assembled in the */
|
||||
/* wrong byte order on little endian machines but this is */
|
||||
/* corrected later since they are only ever used as 32-bit */
|
||||
/* word values. */
|
||||
ctx->wbuf[14] = ctx->count[1];
|
||||
ctx->wbuf[15] = ctx->count[0];
|
||||
sha256_compile(ctx);
|
||||
|
||||
/* extract the hash value as bytes in case the hash buffer is */
|
||||
/* mislaigned for 32-bit words */
|
||||
for(i = 0; i < hlen; ++i)
|
||||
hval[i] = ((ctx->hash[i >> 2] >> (8 * (~i & 3))) & 0xff);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#if defined(SHA_224)
|
||||
|
||||
const uint32_t i224[8] =
|
||||
{
|
||||
0xc1059ed8ul, 0x367cd507ul, 0x3070dd17ul, 0xf70e5939ul,
|
||||
0xffc00b31ul, 0x68581511ul, 0x64f98fa7ul, 0xbefa4fa4ul
|
||||
};
|
||||
|
||||
VOID_RETURN sha224_begin(sha224_ctx ctx[1])
|
||||
{
|
||||
memset(ctx, 0, sizeof(sha224_ctx));
|
||||
memcpy(ctx->hash, i224, sizeof(ctx->hash));
|
||||
}
|
||||
|
||||
VOID_RETURN sha224_end(unsigned char hval[], sha224_ctx ctx[1])
|
||||
{
|
||||
sha_end1(hval, ctx, SHA224_DIGEST_SIZE);
|
||||
}
|
||||
|
||||
VOID_RETURN sha224(unsigned char hval[], const unsigned char data[], unsigned long len)
|
||||
{ sha224_ctx cx[1];
|
||||
|
||||
sha224_begin(cx);
|
||||
sha224_hash(data, len, cx);
|
||||
sha_end1(hval, cx, SHA224_DIGEST_SIZE);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#if defined(SHA_256)
|
||||
|
||||
const uint32_t i256[8] =
|
||||
{
|
||||
0x6a09e667ul, 0xbb67ae85ul, 0x3c6ef372ul, 0xa54ff53aul,
|
||||
0x510e527ful, 0x9b05688cul, 0x1f83d9abul, 0x5be0cd19ul
|
||||
};
|
||||
|
||||
VOID_RETURN sha256_begin(sha256_ctx ctx[1])
|
||||
{
|
||||
memset(ctx, 0, sizeof(sha256_ctx));
|
||||
memcpy(ctx->hash, i256, sizeof(ctx->hash));
|
||||
}
|
||||
|
||||
VOID_RETURN sha256_end(unsigned char hval[], sha256_ctx ctx[1])
|
||||
{
|
||||
sha_end1(hval, ctx, SHA256_DIGEST_SIZE);
|
||||
}
|
||||
|
||||
VOID_RETURN sha256(unsigned char hval[], const unsigned char data[], unsigned long len)
|
||||
{ sha256_ctx cx[1];
|
||||
|
||||
sha256_begin(cx);
|
||||
sha256_hash(data, len, cx);
|
||||
sha_end1(hval, cx, SHA256_DIGEST_SIZE);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#if defined(SHA_384) || defined(SHA_512)
|
||||
|
||||
#define SHA512_MASK (SHA512_BLOCK_SIZE - 1)
|
||||
|
||||
#define rotr64(x,n) (((x) >> n) | ((x) << (64 - n)))
|
||||
|
||||
#if !defined(bswap_64)
|
||||
#define bswap_64(x) (((uint64_t)(bswap_32((uint32_t)(x)))) << 32 | bswap_32((uint32_t)((x) >> 32)))
|
||||
#endif
|
||||
|
||||
#if defined(SWAP_BYTES)
|
||||
#define bsw_64(p,n) \
|
||||
{ int _i = (n); while(_i--) ((uint64_t*)p)[_i] = bswap_64(((uint64_t*)p)[_i]); }
|
||||
#else
|
||||
#define bsw_64(p,n)
|
||||
#endif
|
||||
|
||||
/* SHA512 mixing function definitions */
|
||||
|
||||
#ifdef s_0
|
||||
# undef s_0
|
||||
# undef s_1
|
||||
# undef g_0
|
||||
# undef g_1
|
||||
# undef k_0
|
||||
#endif
|
||||
|
||||
#define s_0(x) (rotr64((x), 28) ^ rotr64((x), 34) ^ rotr64((x), 39))
|
||||
#define s_1(x) (rotr64((x), 14) ^ rotr64((x), 18) ^ rotr64((x), 41))
|
||||
#define g_0(x) (rotr64((x), 1) ^ rotr64((x), 8) ^ ((x) >> 7))
|
||||
#define g_1(x) (rotr64((x), 19) ^ rotr64((x), 61) ^ ((x) >> 6))
|
||||
#define k_0 k512
|
||||
|
||||
/* SHA384/SHA512 mixing data */
|
||||
|
||||
const uint64_t k512[80] =
|
||||
{
|
||||
li_64(428a2f98d728ae22), li_64(7137449123ef65cd),
|
||||
li_64(b5c0fbcfec4d3b2f), li_64(e9b5dba58189dbbc),
|
||||
li_64(3956c25bf348b538), li_64(59f111f1b605d019),
|
||||
li_64(923f82a4af194f9b), li_64(ab1c5ed5da6d8118),
|
||||
li_64(d807aa98a3030242), li_64(12835b0145706fbe),
|
||||
li_64(243185be4ee4b28c), li_64(550c7dc3d5ffb4e2),
|
||||
li_64(72be5d74f27b896f), li_64(80deb1fe3b1696b1),
|
||||
li_64(9bdc06a725c71235), li_64(c19bf174cf692694),
|
||||
li_64(e49b69c19ef14ad2), li_64(efbe4786384f25e3),
|
||||
li_64(0fc19dc68b8cd5b5), li_64(240ca1cc77ac9c65),
|
||||
li_64(2de92c6f592b0275), li_64(4a7484aa6ea6e483),
|
||||
li_64(5cb0a9dcbd41fbd4), li_64(76f988da831153b5),
|
||||
li_64(983e5152ee66dfab), li_64(a831c66d2db43210),
|
||||
li_64(b00327c898fb213f), li_64(bf597fc7beef0ee4),
|
||||
li_64(c6e00bf33da88fc2), li_64(d5a79147930aa725),
|
||||
li_64(06ca6351e003826f), li_64(142929670a0e6e70),
|
||||
li_64(27b70a8546d22ffc), li_64(2e1b21385c26c926),
|
||||
li_64(4d2c6dfc5ac42aed), li_64(53380d139d95b3df),
|
||||
li_64(650a73548baf63de), li_64(766a0abb3c77b2a8),
|
||||
li_64(81c2c92e47edaee6), li_64(92722c851482353b),
|
||||
li_64(a2bfe8a14cf10364), li_64(a81a664bbc423001),
|
||||
li_64(c24b8b70d0f89791), li_64(c76c51a30654be30),
|
||||
li_64(d192e819d6ef5218), li_64(d69906245565a910),
|
||||
li_64(f40e35855771202a), li_64(106aa07032bbd1b8),
|
||||
li_64(19a4c116b8d2d0c8), li_64(1e376c085141ab53),
|
||||
li_64(2748774cdf8eeb99), li_64(34b0bcb5e19b48a8),
|
||||
li_64(391c0cb3c5c95a63), li_64(4ed8aa4ae3418acb),
|
||||
li_64(5b9cca4f7763e373), li_64(682e6ff3d6b2b8a3),
|
||||
li_64(748f82ee5defb2fc), li_64(78a5636f43172f60),
|
||||
li_64(84c87814a1f0ab72), li_64(8cc702081a6439ec),
|
||||
li_64(90befffa23631e28), li_64(a4506cebde82bde9),
|
||||
li_64(bef9a3f7b2c67915), li_64(c67178f2e372532b),
|
||||
li_64(ca273eceea26619c), li_64(d186b8c721c0c207),
|
||||
li_64(eada7dd6cde0eb1e), li_64(f57d4f7fee6ed178),
|
||||
li_64(06f067aa72176fba), li_64(0a637dc5a2c898a6),
|
||||
li_64(113f9804bef90dae), li_64(1b710b35131c471b),
|
||||
li_64(28db77f523047d84), li_64(32caab7b40c72493),
|
||||
li_64(3c9ebe0a15c9bebc), li_64(431d67c49c100d4c),
|
||||
li_64(4cc5d4becb3e42b6), li_64(597f299cfc657e2a),
|
||||
li_64(5fcb6fab3ad6faec), li_64(6c44198c4a475817)
|
||||
};
|
||||
|
||||
/* Compile 128 bytes of hash data into SHA384/512 digest */
|
||||
/* NOTE: this routine assumes that the byte order in the */
|
||||
/* ctx->wbuf[] at this point is such that low address bytes */
|
||||
/* in the ORIGINAL byte stream will go into the high end of */
|
||||
/* words on BOTH big and little endian systems */
|
||||
|
||||
VOID_RETURN sha512_compile(sha512_ctx ctx[1])
|
||||
{ uint64_t v[8], *p = ctx->wbuf;
|
||||
uint32_t j;
|
||||
|
||||
memcpy(v, ctx->hash, sizeof(ctx->hash));
|
||||
|
||||
for(j = 0; j < 80; j += 16)
|
||||
{
|
||||
v_cycle( 0, j); v_cycle( 1, j);
|
||||
v_cycle( 2, j); v_cycle( 3, j);
|
||||
v_cycle( 4, j); v_cycle( 5, j);
|
||||
v_cycle( 6, j); v_cycle( 7, j);
|
||||
v_cycle( 8, j); v_cycle( 9, j);
|
||||
v_cycle(10, j); v_cycle(11, j);
|
||||
v_cycle(12, j); v_cycle(13, j);
|
||||
v_cycle(14, j); v_cycle(15, j);
|
||||
}
|
||||
|
||||
ctx->hash[0] += v[0]; ctx->hash[1] += v[1];
|
||||
ctx->hash[2] += v[2]; ctx->hash[3] += v[3];
|
||||
ctx->hash[4] += v[4]; ctx->hash[5] += v[5];
|
||||
ctx->hash[6] += v[6]; ctx->hash[7] += v[7];
|
||||
}
|
||||
|
||||
/* Compile 128 bytes of hash data into SHA256 digest value */
|
||||
/* NOTE: this routine assumes that the byte order in the */
|
||||
/* ctx->wbuf[] at this point is in such an order that low */
|
||||
/* address bytes in the ORIGINAL byte stream placed in this */
|
||||
/* buffer will now go to the high end of words on BOTH big */
|
||||
/* and little endian systems */
|
||||
|
||||
VOID_RETURN sha512_hash(const unsigned char data[], unsigned long len, sha512_ctx ctx[1])
|
||||
{ uint32_t pos = (uint32_t)(ctx->count[0] >> 3) & SHA512_MASK;
|
||||
const unsigned char *sp = data;
|
||||
unsigned char *w = (unsigned char*)ctx->wbuf;
|
||||
#if SHA2_BITS == 1
|
||||
uint32_t ofs = (ctx->count[0] & 7);
|
||||
#else
|
||||
len <<= 3;
|
||||
#endif
|
||||
|
||||
if((ctx->count[0] += len) < len)
|
||||
++(ctx->count[1]);
|
||||
|
||||
#if SHA2_BITS == 1
|
||||
if(ofs) /* if not on a byte boundary */
|
||||
{
|
||||
if(ofs + len < 8) /* if no added bytes are needed */
|
||||
{
|
||||
w[pos] |= (*sp >> ofs);
|
||||
}
|
||||
else /* otherwise and add bytes */
|
||||
{ unsigned char part = w[pos];
|
||||
|
||||
while((int)(ofs + (len -= 8)) >= 0)
|
||||
{
|
||||
w[pos++] = part | (*sp >> ofs);
|
||||
part = *sp++ << (8 - ofs);
|
||||
if(pos == SHA512_BLOCK_SIZE)
|
||||
{
|
||||
bsw_64(w, SHA512_BLOCK_SIZE >> 3);
|
||||
sha512_compile(ctx); pos = 0;
|
||||
}
|
||||
}
|
||||
|
||||
w[pos] = part;
|
||||
}
|
||||
}
|
||||
else /* data is byte aligned */
|
||||
#endif
|
||||
{ uint32_t space = SHA512_BLOCK_SIZE - pos;
|
||||
|
||||
while(len >= (space << 3))
|
||||
{
|
||||
memcpy(w + pos, sp, space);
|
||||
bsw_64(w, SHA512_BLOCK_SIZE >> 3);
|
||||
sha512_compile(ctx);
|
||||
sp += space; len -= (space << 3);
|
||||
space = SHA512_BLOCK_SIZE; pos = 0;
|
||||
}
|
||||
memcpy(w + pos, sp, (len + 7 * SHA2_BITS) >> 3);
|
||||
}
|
||||
}
|
||||
|
||||
/* SHA384/512 Final padding and digest calculation */
|
||||
|
||||
static void sha_end2(unsigned char hval[], sha512_ctx ctx[1], const unsigned int hlen)
|
||||
{ uint32_t i = (uint32_t)((ctx->count[0] >> 3) & SHA512_MASK);
|
||||
uint64_t m1;
|
||||
|
||||
/* put bytes in the buffer in an order in which references to */
|
||||
/* 32-bit words will put bytes with lower addresses into the */
|
||||
/* top of 32 bit words on BOTH big and little endian machines */
|
||||
bsw_64(ctx->wbuf, (i + 7 + SHA2_BITS) >> 3);
|
||||
|
||||
/* we now need to mask valid bytes and add the padding which is */
|
||||
/* a single 1 bit and as many zero bits as necessary. Note that */
|
||||
/* we can always add the first padding byte here because the */
|
||||
/* buffer always has at least one empty slot */
|
||||
m1 = (unsigned char)0x80 >> (ctx->count[0] & 7);
|
||||
ctx->wbuf[i >> 3] &= ((li_64(ffffffffffffff00) | (~m1 + 1)) << 8 * (~i & 7));
|
||||
ctx->wbuf[i >> 3] |= (m1 << 8 * (~i & 7));
|
||||
|
||||
/* we need 17 or more empty byte positions, one for the padding */
|
||||
/* byte (above) and sixteen for the length count. If there is */
|
||||
/* not enough space pad and empty the buffer */
|
||||
if(i > SHA512_BLOCK_SIZE - 17)
|
||||
{
|
||||
if(i < 120) ctx->wbuf[15] = 0;
|
||||
sha512_compile(ctx);
|
||||
i = 0;
|
||||
}
|
||||
else
|
||||
i = (i >> 3) + 1;
|
||||
|
||||
while(i < 14)
|
||||
ctx->wbuf[i++] = 0;
|
||||
|
||||
/* the following 64-bit length fields are assembled in the */
|
||||
/* wrong byte order on little endian machines but this is */
|
||||
/* corrected later since they are only ever used as 64-bit */
|
||||
/* word values. */
|
||||
ctx->wbuf[14] = ctx->count[1];
|
||||
ctx->wbuf[15] = ctx->count[0];
|
||||
sha512_compile(ctx);
|
||||
|
||||
/* extract the hash value as bytes in case the hash buffer is */
|
||||
/* misaligned for 32-bit words */
|
||||
for(i = 0; i < hlen; ++i)
|
||||
hval[i] = ((ctx->hash[i >> 3] >> (8 * (~i & 7))) & 0xff);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#if defined(SHA_384)
|
||||
|
||||
/* SHA384 initialisation data */
|
||||
|
||||
const uint64_t i384[80] =
|
||||
{
|
||||
li_64(cbbb9d5dc1059ed8), li_64(629a292a367cd507),
|
||||
li_64(9159015a3070dd17), li_64(152fecd8f70e5939),
|
||||
li_64(67332667ffc00b31), li_64(8eb44a8768581511),
|
||||
li_64(db0c2e0d64f98fa7), li_64(47b5481dbefa4fa4)
|
||||
};
|
||||
|
||||
VOID_RETURN sha384_begin(sha384_ctx ctx[1])
|
||||
{
|
||||
memset(ctx, 0, sizeof(sha384_ctx));
|
||||
memcpy(ctx->hash, i384, sizeof(ctx->hash));
|
||||
}
|
||||
|
||||
VOID_RETURN sha384_end(unsigned char hval[], sha384_ctx ctx[1])
|
||||
{
|
||||
sha_end2(hval, ctx, SHA384_DIGEST_SIZE);
|
||||
}
|
||||
|
||||
VOID_RETURN sha384(unsigned char hval[], const unsigned char data[], unsigned long len)
|
||||
{ sha384_ctx cx[1];
|
||||
|
||||
sha384_begin(cx);
|
||||
sha384_hash(data, len, cx);
|
||||
sha_end2(hval, cx, SHA384_DIGEST_SIZE);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#if defined(SHA_512)
|
||||
|
||||
/* SHA512 initialisation data */
|
||||
|
||||
static const uint64_t i512[SHA512_DIGEST_SIZE >> 3] =
|
||||
{
|
||||
li_64(6a09e667f3bcc908), li_64(bb67ae8584caa73b),
|
||||
li_64(3c6ef372fe94f82b), li_64(a54ff53a5f1d36f1),
|
||||
li_64(510e527fade682d1), li_64(9b05688c2b3e6c1f),
|
||||
li_64(1f83d9abfb41bd6b), li_64(5be0cd19137e2179)
|
||||
};
|
||||
|
||||
/* FIPS PUB 180-4: SHA-512/256 */
|
||||
|
||||
static const uint64_t i512_256[SHA512_DIGEST_SIZE >> 3] =
|
||||
{
|
||||
li_64(22312194fc2bf72c), li_64(9f555fa3c84c64c2),
|
||||
li_64(2393b86b6f53b151), li_64(963877195940eabd),
|
||||
li_64(96283ee2a88effe3), li_64(be5e1e2553863992),
|
||||
li_64(2b0199fc2c85b8aa), li_64(0eb72ddc81c52ca2),
|
||||
};
|
||||
|
||||
/* FIPS PUB 180-4: SHA-512/224 */
|
||||
|
||||
static const uint64_t i512_224[SHA512_DIGEST_SIZE >> 3] =
|
||||
{
|
||||
li_64(8c3d37c819544da2), li_64(73e1996689dcd4d6),
|
||||
li_64(1dfab7ae32ff9c82), li_64(679dd514582f9fcf),
|
||||
li_64(0f6d2b697bd44da8), li_64(77e36f7304c48942),
|
||||
li_64(3f9d85a86a1d36c8), li_64(1112e6ad91d692a1),
|
||||
};
|
||||
|
||||
/* FIPS PUB 180-4: SHA-512/192 (unsanctioned; facilitates using AES-192) */
|
||||
|
||||
static const uint64_t i512_192[SHA512_DIGEST_SIZE >> 3] =
|
||||
{
|
||||
li_64(010176140648b233), li_64(db92aeb1eebadd6f),
|
||||
li_64(83a9e27aa1d5ea62), li_64(ec95f77eb609b4e1),
|
||||
li_64(71a99185c75caefa), li_64(006e8f08baf32e3c),
|
||||
li_64(6a2b21abd2db2aec), li_64(24926cdbd918a27f),
|
||||
};
|
||||
|
||||
/* FIPS PUB 180-4: SHA-512/128 (unsanctioned; facilitates using AES-128) */
|
||||
|
||||
static const uint64_t i512_128[SHA512_DIGEST_SIZE >> 3] =
|
||||
{
|
||||
li_64(c953a21464c3e8cc), li_64(06cc9cfd166a34b5),
|
||||
li_64(647e88dabf8b24ab), li_64(8513e4dc05a078ac),
|
||||
li_64(7266fcfb7cba0534), li_64(854a78e2ecd19b93),
|
||||
li_64(8618061711cec2dd), li_64(b20d8506efb929b1),
|
||||
};
|
||||
|
||||
VOID_RETURN sha512_begin(sha512_ctx ctx[1])
|
||||
{
|
||||
memset(ctx, 0, sizeof(sha512_ctx));
|
||||
memcpy(ctx->hash, i512, sizeof(ctx->hash));
|
||||
}
|
||||
|
||||
VOID_RETURN sha512_256_begin(sha512_ctx ctx[1])
|
||||
{
|
||||
memset(ctx, 0, sizeof(sha512_ctx));
|
||||
memcpy(ctx->hash, i512_256, sizeof(ctx->hash));
|
||||
}
|
||||
|
||||
VOID_RETURN sha512_224_begin(sha512_ctx ctx[1])
|
||||
{
|
||||
memset(ctx, 0, sizeof(sha512_ctx));
|
||||
memcpy(ctx->hash, i512_224, sizeof(ctx->hash));
|
||||
}
|
||||
|
||||
VOID_RETURN sha512_192_begin(sha512_ctx ctx[1])
|
||||
{
|
||||
memset(ctx, 0, sizeof(sha512_ctx));
|
||||
memcpy(ctx->hash, i512_192, sizeof(ctx->hash));
|
||||
}
|
||||
|
||||
VOID_RETURN sha512_128_begin(sha512_ctx ctx[1])
|
||||
{
|
||||
memset(ctx, 0, sizeof(sha512_ctx));
|
||||
memcpy(ctx->hash, i512_128, sizeof(ctx->hash));
|
||||
}
|
||||
|
||||
VOID_RETURN sha512_end(unsigned char hval[], sha512_ctx ctx[1])
|
||||
{
|
||||
sha_end2(hval, ctx, SHA512_DIGEST_SIZE);
|
||||
}
|
||||
|
||||
VOID_RETURN sha512_256_end(unsigned char hval[], sha512_ctx ctx[1])
|
||||
{
|
||||
sha_end2(hval, ctx, SHA512_256_DIGEST_SIZE);
|
||||
}
|
||||
|
||||
VOID_RETURN sha512_224_end(unsigned char hval[], sha512_ctx ctx[1])
|
||||
{
|
||||
sha_end2(hval, ctx, SHA512_224_DIGEST_SIZE);
|
||||
}
|
||||
|
||||
VOID_RETURN sha512_192_end(unsigned char hval[], sha512_ctx ctx[1])
|
||||
{
|
||||
sha_end2(hval, ctx, SHA512_192_DIGEST_SIZE);
|
||||
}
|
||||
|
||||
VOID_RETURN sha512_128_end(unsigned char hval[], sha512_ctx ctx[1])
|
||||
{
|
||||
sha_end2(hval, ctx, SHA512_128_DIGEST_SIZE);
|
||||
}
|
||||
|
||||
VOID_RETURN sha512(unsigned char hval[], const unsigned char data[], unsigned long len)
|
||||
{ sha512_ctx cx[1];
|
||||
|
||||
sha512_begin(cx);
|
||||
sha512_hash(data, len, cx);
|
||||
sha512_end(hval, cx);
|
||||
}
|
||||
|
||||
VOID_RETURN sha512_256(unsigned char hval[], const unsigned char data[], unsigned long len)
|
||||
{ sha512_ctx cx[1];
|
||||
|
||||
sha512_256_begin(cx);
|
||||
sha512_256_hash(data, len, cx);
|
||||
sha512_256_end(hval, cx);
|
||||
}
|
||||
|
||||
VOID_RETURN sha512_224(unsigned char hval[], const unsigned char data[], unsigned long len)
|
||||
{ sha512_ctx cx[1];
|
||||
|
||||
sha512_224_begin(cx);
|
||||
sha512_224_hash(data, len, cx);
|
||||
sha512_224_end(hval, cx);
|
||||
}
|
||||
|
||||
VOID_RETURN sha512_192(unsigned char hval[], const unsigned char data[], unsigned long len)
|
||||
{ sha512_ctx cx[1];
|
||||
|
||||
sha512_192_begin(cx);
|
||||
sha512_192_hash(data, len, cx);
|
||||
sha512_192_end(hval, cx);
|
||||
}
|
||||
|
||||
VOID_RETURN sha512_128(unsigned char hval[], const unsigned char data[], unsigned long len)
|
||||
{ sha512_ctx cx[1];
|
||||
|
||||
sha512_128_begin(cx);
|
||||
sha512_128_hash(data, len, cx);
|
||||
sha512_128_end(hval, cx);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#if defined(SHA_2)
|
||||
|
||||
#define CTX_224(x) ((x)->uu->ctx256)
|
||||
#define CTX_256(x) ((x)->uu->ctx256)
|
||||
#define CTX_384(x) ((x)->uu->ctx512)
|
||||
#define CTX_512(x) ((x)->uu->ctx512)
|
||||
|
||||
/* SHA2 initialisation */
|
||||
|
||||
INT_RETURN sha2_begin(unsigned long len, sha2_ctx ctx[1])
|
||||
{
|
||||
switch(len)
|
||||
{
|
||||
#if defined(SHA_224)
|
||||
case 224:
|
||||
case 28: CTX_256(ctx)->count[0] = CTX_256(ctx)->count[1] = 0;
|
||||
memcpy(CTX_256(ctx)->hash, i224, 32);
|
||||
ctx->sha2_len = 28; return EXIT_SUCCESS;
|
||||
#endif
|
||||
#if defined(SHA_256)
|
||||
case 256:
|
||||
case 32: CTX_256(ctx)->count[0] = CTX_256(ctx)->count[1] = 0;
|
||||
memcpy(CTX_256(ctx)->hash, i256, 32);
|
||||
ctx->sha2_len = 32; return EXIT_SUCCESS;
|
||||
#endif
|
||||
#if defined(SHA_384)
|
||||
case 384:
|
||||
case 48: CTX_384(ctx)->count[0] = CTX_384(ctx)->count[1] = 0;
|
||||
memcpy(CTX_384(ctx)->hash, i384, 64);
|
||||
ctx->sha2_len = 48; return EXIT_SUCCESS;
|
||||
#endif
|
||||
#if defined(SHA_512)
|
||||
case 512:
|
||||
case 64: CTX_512(ctx)->count[0] = CTX_512(ctx)->count[1] = 0;
|
||||
memcpy(CTX_512(ctx)->hash, i512, 64);
|
||||
ctx->sha2_len = 64; return EXIT_SUCCESS;
|
||||
#endif
|
||||
default: return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
VOID_RETURN sha2_hash(const unsigned char data[], unsigned long len, sha2_ctx ctx[1])
|
||||
{
|
||||
switch(ctx->sha2_len)
|
||||
{
|
||||
#if defined(SHA_224)
|
||||
case 28: sha224_hash(data, len, CTX_224(ctx)); return;
|
||||
#endif
|
||||
#if defined(SHA_256)
|
||||
case 32: sha256_hash(data, len, CTX_256(ctx)); return;
|
||||
#endif
|
||||
#if defined(SHA_384)
|
||||
case 48: sha384_hash(data, len, CTX_384(ctx)); return;
|
||||
#endif
|
||||
#if defined(SHA_512)
|
||||
case 64: sha512_hash(data, len, CTX_512(ctx)); return;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
VOID_RETURN sha2_end(unsigned char hval[], sha2_ctx ctx[1])
|
||||
{
|
||||
switch(ctx->sha2_len)
|
||||
{
|
||||
#if defined(SHA_224)
|
||||
case 28: sha_end1(hval, CTX_224(ctx), SHA224_DIGEST_SIZE); return;
|
||||
#endif
|
||||
#if defined(SHA_256)
|
||||
case 32: sha_end1(hval, CTX_256(ctx), SHA256_DIGEST_SIZE); return;
|
||||
#endif
|
||||
#if defined(SHA_384)
|
||||
case 48: sha_end2(hval, CTX_384(ctx), SHA384_DIGEST_SIZE); return;
|
||||
#endif
|
||||
#if defined(SHA_512)
|
||||
case 64: sha_end2(hval, CTX_512(ctx), SHA512_DIGEST_SIZE); return;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
INT_RETURN sha2(unsigned char hval[], unsigned long size,
|
||||
const unsigned char data[], unsigned long len)
|
||||
{ sha2_ctx cx[1];
|
||||
|
||||
if(sha2_begin(size, cx) == EXIT_SUCCESS)
|
||||
{
|
||||
sha2_hash(data, len, cx); sha2_end(hval, cx); return EXIT_SUCCESS;
|
||||
}
|
||||
else
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif
|
||||
+183
@@ -0,0 +1,183 @@
|
||||
/*
|
||||
---------------------------------------------------------------------------
|
||||
Copyright (c) 1998-2010, Brian Gladman, Worcester, UK. All rights reserved.
|
||||
|
||||
The redistribution and use of this software (with or without changes)
|
||||
is allowed without the payment of fees or royalties provided that:
|
||||
|
||||
source code distributions include the above copyright notice, this
|
||||
list of conditions and the following disclaimer;
|
||||
|
||||
binary distributions include the above copyright notice, this list
|
||||
of conditions and the following disclaimer in their documentation.
|
||||
|
||||
This software is provided 'as is' with no explicit or implied warranties
|
||||
in respect of its operation, including, but not limited to, correctness
|
||||
and fitness for purpose.
|
||||
---------------------------------------------------------------------------
|
||||
Issue Date: 20/12/2007
|
||||
*/
|
||||
|
||||
#ifndef _SHA2_H
|
||||
#define _SHA2_H
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
/* define for bit or byte oriented SHA */
|
||||
#if 1
|
||||
# define SHA2_BITS 0 /* byte oriented */
|
||||
#else
|
||||
# define SHA2_BITS 1 /* bit oriented */
|
||||
#endif
|
||||
|
||||
/* define the hash functions that you need */
|
||||
/* define for 64-bit SHA384 and SHA512 */
|
||||
#define SHA_64BIT
|
||||
#define SHA_2 /* for dynamic hash length */
|
||||
#define SHA_224
|
||||
#define SHA_256
|
||||
#ifdef SHA_64BIT
|
||||
# define SHA_384
|
||||
# define SHA_512
|
||||
# define NEED_uint64_t
|
||||
#endif
|
||||
|
||||
#define SHA2_MAX_DIGEST_SIZE 64
|
||||
#define SHA2_MAX_BLOCK_SIZE 128
|
||||
|
||||
#include "brg_types.h"
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
/* Note that the following function prototypes are the same */
|
||||
/* for both the bit and byte oriented implementations. But */
|
||||
/* the length fields are in bytes or bits as is appropriate */
|
||||
/* for the version used. Bit sequences are arrays of bytes */
|
||||
/* in which bit sequence indexes increase from the most to */
|
||||
/* the least significant end of each byte. The value 'len' */
|
||||
/* in sha<nnn>_hash for the byte oriented versions of SHA2 */
|
||||
/* is limited to 2^29 bytes, but multiple calls will handle */
|
||||
/* longer data blocks. */
|
||||
|
||||
#define SHA224_DIGEST_SIZE 28
|
||||
#define SHA224_BLOCK_SIZE 64
|
||||
|
||||
#define SHA256_DIGEST_SIZE 32
|
||||
#define SHA256_BLOCK_SIZE 64
|
||||
|
||||
/* type to hold the SHA256 (and SHA224) context */
|
||||
|
||||
typedef struct
|
||||
{ uint32_t count[2];
|
||||
uint32_t hash[SHA256_DIGEST_SIZE >> 2];
|
||||
uint32_t wbuf[SHA256_BLOCK_SIZE >> 2];
|
||||
} sha256_ctx;
|
||||
|
||||
typedef sha256_ctx sha224_ctx;
|
||||
|
||||
VOID_RETURN sha256_compile(sha256_ctx ctx[1]);
|
||||
|
||||
VOID_RETURN sha224_begin(sha224_ctx ctx[1]);
|
||||
#define sha224_hash sha256_hash
|
||||
VOID_RETURN sha224_end(unsigned char hval[], sha224_ctx ctx[1]);
|
||||
VOID_RETURN sha224(unsigned char hval[], const unsigned char data[], unsigned long len);
|
||||
|
||||
VOID_RETURN sha256_begin(sha256_ctx ctx[1]);
|
||||
VOID_RETURN sha256_hash(const unsigned char data[], unsigned long len, sha256_ctx ctx[1]);
|
||||
VOID_RETURN sha256_end(unsigned char hval[], sha256_ctx ctx[1]);
|
||||
VOID_RETURN sha256(unsigned char hval[], const unsigned char data[], unsigned long len);
|
||||
|
||||
#ifndef SHA_64BIT
|
||||
|
||||
typedef struct
|
||||
{ union
|
||||
{ sha256_ctx ctx256[1];
|
||||
} uu[1];
|
||||
uint32_t sha2_len;
|
||||
} sha2_ctx;
|
||||
|
||||
#else
|
||||
|
||||
#define SHA384_DIGEST_SIZE 48
|
||||
#define SHA384_BLOCK_SIZE 128
|
||||
|
||||
#define SHA512_DIGEST_SIZE 64
|
||||
#define SHA512_BLOCK_SIZE 128
|
||||
|
||||
#define SHA512_128_DIGEST_SIZE 16
|
||||
#define SHA512_128_BLOCK_SIZE SHA512_BLOCK_SIZE
|
||||
|
||||
#define SHA512_192_DIGEST_SIZE 24
|
||||
#define SHA512_192_BLOCK_SIZE SHA512_BLOCK_SIZE
|
||||
|
||||
#define SHA512_224_DIGEST_SIZE 28
|
||||
#define SHA512_224_BLOCK_SIZE SHA512_BLOCK_SIZE
|
||||
|
||||
#define SHA512_256_DIGEST_SIZE 32
|
||||
#define SHA512_256_BLOCK_SIZE SHA512_BLOCK_SIZE
|
||||
|
||||
/* type to hold the SHA384 (and SHA512) context */
|
||||
|
||||
typedef struct
|
||||
{ uint64_t count[2];
|
||||
uint64_t hash[SHA512_DIGEST_SIZE >> 3];
|
||||
uint64_t wbuf[SHA512_BLOCK_SIZE >> 3];
|
||||
} sha512_ctx;
|
||||
|
||||
typedef sha512_ctx sha384_ctx;
|
||||
|
||||
typedef struct
|
||||
{ union
|
||||
{ sha256_ctx ctx256[1];
|
||||
sha512_ctx ctx512[1];
|
||||
} uu[1];
|
||||
uint32_t sha2_len;
|
||||
} sha2_ctx;
|
||||
|
||||
VOID_RETURN sha512_compile(sha512_ctx ctx[1]);
|
||||
|
||||
VOID_RETURN sha384_begin(sha384_ctx ctx[1]);
|
||||
#define sha384_hash sha512_hash
|
||||
VOID_RETURN sha384_end(unsigned char hval[], sha384_ctx ctx[1]);
|
||||
VOID_RETURN sha384(unsigned char hval[], const unsigned char data[], unsigned long len);
|
||||
|
||||
VOID_RETURN sha512_begin(sha512_ctx ctx[1]);
|
||||
VOID_RETURN sha512_hash(const unsigned char data[], unsigned long len, sha512_ctx ctx[1]);
|
||||
VOID_RETURN sha512_end(unsigned char hval[], sha512_ctx ctx[1]);
|
||||
VOID_RETURN sha512(unsigned char hval[], const unsigned char data[], unsigned long len);
|
||||
|
||||
VOID_RETURN sha512_256_begin(sha512_ctx ctx[1]);
|
||||
#define sha512_256_hash sha512_hash
|
||||
VOID_RETURN sha512_256_end(unsigned char hval[], sha512_ctx ctx[1]);
|
||||
VOID_RETURN sha512_256(unsigned char hval[], const unsigned char data[], unsigned long len);
|
||||
|
||||
VOID_RETURN sha512_224_begin(sha512_ctx ctx[1]);
|
||||
#define sha512_224_hash sha512_hash
|
||||
VOID_RETURN sha512_224_end(unsigned char hval[], sha512_ctx ctx[1]);
|
||||
VOID_RETURN sha512_224(unsigned char hval[], const unsigned char data[], unsigned long len);
|
||||
|
||||
VOID_RETURN sha512_192_begin(sha512_ctx ctx[1]);
|
||||
#define sha512_192_hash sha512_hash
|
||||
VOID_RETURN sha512_192_end(unsigned char hval[], sha512_ctx ctx[1]);
|
||||
VOID_RETURN sha512_192(unsigned char hval[], const unsigned char data[], unsigned long len);
|
||||
|
||||
VOID_RETURN sha512_128_begin(sha512_ctx ctx[1]);
|
||||
#define sha512_128_hash sha512_hash
|
||||
VOID_RETURN sha512_128_end(unsigned char hval[], sha512_ctx ctx[1]);
|
||||
VOID_RETURN sha512_128(unsigned char hval[], const unsigned char data[], unsigned long len);
|
||||
|
||||
INT_RETURN sha2_begin(unsigned long size, sha2_ctx ctx[1]);
|
||||
VOID_RETURN sha2_hash(const unsigned char data[], unsigned long len, sha2_ctx ctx[1]);
|
||||
VOID_RETURN sha2_end(unsigned char hval[], sha2_ctx ctx[1]);
|
||||
INT_RETURN sha2(unsigned char hval[], unsigned long size, const unsigned char data[], unsigned long len);
|
||||
|
||||
#endif
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -83,6 +83,8 @@ lzma_check_size(lzma_check type)
|
||||
extern void
|
||||
lzma_check_init(lzma_check_state *check, lzma_check type)
|
||||
{
|
||||
(void)check;
|
||||
|
||||
switch (type) {
|
||||
case LZMA_CHECK_NONE:
|
||||
break;
|
||||
@@ -117,6 +119,10 @@ extern void
|
||||
lzma_check_update(lzma_check_state *check, lzma_check type,
|
||||
const uint8_t *buf, size_t size)
|
||||
{
|
||||
(void)check;
|
||||
(void)buf;
|
||||
(void)size;
|
||||
|
||||
switch (type) {
|
||||
#ifdef HAVE_CHECK_CRC32
|
||||
case LZMA_CHECK_CRC32:
|
||||
@@ -147,6 +153,8 @@ lzma_check_update(lzma_check_state *check, lzma_check type,
|
||||
extern void
|
||||
lzma_check_finish(lzma_check_state *check, lzma_check type)
|
||||
{
|
||||
(void)check;
|
||||
|
||||
switch (type) {
|
||||
#ifdef HAVE_CHECK_CRC32
|
||||
case LZMA_CHECK_CRC32:
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -16,8 +16,12 @@
|
||||
#include "common.h"
|
||||
|
||||
#ifdef HAVE_IMMINTRIN_H
|
||||
#if (defined(__GNUC__) && defined(__SSE2_MATH__)) || \
|
||||
(defined(__INTEL_COMPILER) && defined(__SSE2__)) || \
|
||||
(defined(_MSC_VER) && defined(_M_IX86_FP) && _M_IX86_FP >= 2)
|
||||
# include <immintrin.h>
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
/// Find out how many equal bytes the two buffers have.
|
||||
@@ -64,7 +68,8 @@ lzma_memcmplen(const uint8_t *buf1, const uint8_t *buf2,
|
||||
const uint64_t x = *(const uint64_t *)(buf1 + len)
|
||||
- *(const uint64_t *)(buf2 + len);
|
||||
if (x != 0) {
|
||||
# if defined(_M_X64) // MSVC or Intel C compiler on Windows
|
||||
# if defined(_M_X64) && defined(_WIN32) \
|
||||
&& (defined(_MSC_VER) || defined(__INTEL_COMPILER)) // MSVC or Intel C compiler on Windows
|
||||
unsigned long tmp;
|
||||
_BitScanForward64(&tmp, x);
|
||||
len += (uint32_t)tmp >> 3;
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -0,0 +1,699 @@
|
||||
/* minizip.c
|
||||
Version 2.7.3, November 4, 2018
|
||||
part of the MiniZip project
|
||||
|
||||
Copyright (C) 2010-2018 Nathan Moinvaziri
|
||||
https://github.com/nmoinvaz/minizip
|
||||
Copyright (C) 1998-2010 Gilles Vollant
|
||||
https://www.winimage.com/zLibDll/minizip.html
|
||||
|
||||
This program is distributed under the terms of the same license as zlib.
|
||||
See the accompanying LICENSE file for the full text of the license.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <inttypes.h>
|
||||
#include <time.h>
|
||||
|
||||
#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"
|
||||
#include "mz_zip_rw.h"
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct minizip_opt_s {
|
||||
uint8_t include_path;
|
||||
int16_t compress_level;
|
||||
uint8_t compress_method;
|
||||
uint8_t overwrite;
|
||||
uint8_t append;
|
||||
int64_t disk_size;
|
||||
uint8_t zip_cd;
|
||||
int32_t encoding;
|
||||
uint8_t verbose;
|
||||
uint8_t aes;
|
||||
const char *cert_path;
|
||||
const char *cert_pwd;
|
||||
} minizip_opt;
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t minizip_banner(void);
|
||||
int32_t minizip_help(void);
|
||||
|
||||
int32_t minizip_list(const char *path);
|
||||
|
||||
int32_t minizip_add_entry_cb(void *handle, void *userdata, mz_zip_file *file_info);
|
||||
int32_t minizip_add_progress_cb(void *handle, void *userdata, mz_zip_file *file_info, int64_t position);
|
||||
int32_t minizip_add_overwrite_cb(void *handle, void *userdata, const char *path);
|
||||
int32_t minizip_add(const char *path, const char *password, minizip_opt *options, int32_t arg_count, const char **args);
|
||||
|
||||
int32_t minizip_extract_entry_cb(void *handle, void *userdata, mz_zip_file *file_info, const char *path);
|
||||
int32_t minizip_extract_progress_cb(void *handle, void *userdata, mz_zip_file *file_info, int64_t position);
|
||||
int32_t minizip_extract_overwrite_cb(void *handle, void *userdata, mz_zip_file *file_info, const char *path);
|
||||
int32_t minizip_extract(const char *path, const char *pattern, const char *destination, const char *password, minizip_opt *options);
|
||||
|
||||
int32_t minizip_erase(const char *src_path, const char *target_path, int32_t arg_count, const char **args);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t minizip_banner(void)
|
||||
{
|
||||
printf("Minizip %s - https://github.com/nmoinvaz/minizip\n", MZ_VERSION);
|
||||
printf("---------------------------------------------------\n");
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t minizip_help(void)
|
||||
{
|
||||
printf("Usage : minizip [-x -d dir|-l|-e] [-o] [-c codepage] [-a] [-j] [-0 to -9] [-b|-m] [-k 512] [-p pwd] [-s] file.zip [files]\n\n" \
|
||||
" -x Extract files\n" \
|
||||
" -l List files\n" \
|
||||
" -d Destination directory\n" \
|
||||
" -o Overwrite existing files\n" \
|
||||
" -c File names use cp437 encoding\n" \
|
||||
" -a Append to existing zip file\n" \
|
||||
" -i Include full path of files\n" \
|
||||
" -v Verbose info\n" \
|
||||
" -0 Store only\n" \
|
||||
" -1 Compress faster\n" \
|
||||
" -9 Compress better\n" \
|
||||
" -k Disk size in KB\n" \
|
||||
" -z Zip central directory\n" \
|
||||
" -p Encryption password\n" \
|
||||
" -s AES encryption\n" \
|
||||
" -h Pkcs12 certificate path\n" \
|
||||
" -w Pkcs12 certificate password\n" \
|
||||
" -b BZIP2 compression\n" \
|
||||
" -m LZMA compression\n\n");
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t minizip_list(const char *path)
|
||||
{
|
||||
mz_zip_file *file_info = NULL;
|
||||
uint32_t ratio = 0;
|
||||
int16_t level = 0;
|
||||
int32_t err = MZ_OK;
|
||||
struct tm tmu_date;
|
||||
const char *string_method = NULL;
|
||||
char crypt = ' ';
|
||||
void *reader = NULL;
|
||||
|
||||
|
||||
mz_zip_reader_create(&reader);
|
||||
err = mz_zip_reader_open_file(reader, path);
|
||||
if (err != MZ_OK)
|
||||
{
|
||||
printf("Error %d opening zip file %s\n", err, path);
|
||||
mz_zip_reader_delete(&reader);
|
||||
return err;
|
||||
}
|
||||
|
||||
err = mz_zip_reader_goto_first_entry(reader);
|
||||
|
||||
if (err != MZ_OK && err != MZ_END_OF_LIST)
|
||||
{
|
||||
printf("Error %d going to first entry in zip file\n", err);
|
||||
mz_zip_reader_delete(&reader);
|
||||
return err;
|
||||
}
|
||||
|
||||
printf(" Length Method Size Attribs Ratio Date Time CRC-32 Name\n");
|
||||
printf(" ------ ------ ---- ------- ----- ---- ---- ------ ----\n");
|
||||
|
||||
do
|
||||
{
|
||||
err = mz_zip_reader_entry_get_info(reader, &file_info);
|
||||
|
||||
if (err != MZ_OK)
|
||||
{
|
||||
printf("Error %d getting entry info in zip file\n", err);
|
||||
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 = '*';
|
||||
|
||||
switch (file_info->compression_method)
|
||||
{
|
||||
case MZ_COMPRESS_METHOD_STORE:
|
||||
string_method = "Stored";
|
||||
break;
|
||||
case MZ_COMPRESS_METHOD_DEFLATE:
|
||||
level = (int16_t)((file_info->flag & 0x6) / 2);
|
||||
if (level == 0)
|
||||
string_method = "Defl:N";
|
||||
else if (level == 1)
|
||||
string_method = "Defl:X";
|
||||
else if ((level == 2) || (level == 3))
|
||||
string_method = "Defl:F"; // 2: fast , 3: extra fast
|
||||
else
|
||||
string_method = "Defl:?";
|
||||
break;
|
||||
case MZ_COMPRESS_METHOD_BZIP2:
|
||||
string_method = "BZip2";
|
||||
break;
|
||||
case MZ_COMPRESS_METHOD_LZMA:
|
||||
string_method = "LZMA";
|
||||
break;
|
||||
default:
|
||||
string_method = "?";
|
||||
}
|
||||
|
||||
mz_zip_time_t_to_tm(file_info->modified_date, &tmu_date);
|
||||
|
||||
printf(" %7"PRIu64" %6s%c %7"PRIu64" %8"PRIx32" %3"PRIu32"%% %2.2"PRIu32"-%2.2"PRIu32\
|
||||
"-%2.2"PRIu32" %2.2"PRIu32":%2.2"PRIu32" %8.8"PRIx32" %s\n",
|
||||
file_info->uncompressed_size, string_method, crypt,
|
||||
file_info->compressed_size, file_info->external_fa, ratio,
|
||||
(uint32_t)tmu_date.tm_mon + 1, (uint32_t)tmu_date.tm_mday,
|
||||
(uint32_t)tmu_date.tm_year % 100,
|
||||
(uint32_t)tmu_date.tm_hour, (uint32_t)tmu_date.tm_min,
|
||||
file_info->crc, file_info->filename);
|
||||
|
||||
err = mz_zip_reader_goto_next_entry(reader);
|
||||
|
||||
if (err != MZ_OK && err != MZ_END_OF_LIST)
|
||||
{
|
||||
printf("Error %d going to next entry in zip file\n", err);
|
||||
break;
|
||||
}
|
||||
}
|
||||
while (err == MZ_OK);
|
||||
|
||||
mz_zip_reader_delete(&reader);
|
||||
|
||||
if (err == MZ_END_OF_LIST)
|
||||
return MZ_OK;
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t minizip_add_entry_cb(void *handle, void *userdata, mz_zip_file *file_info)
|
||||
{
|
||||
MZ_UNUSED(handle);
|
||||
MZ_UNUSED(userdata);
|
||||
|
||||
printf("Adding %s\n", file_info->filename);
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t minizip_add_progress_cb(void *handle, void *userdata, mz_zip_file *file_info, int64_t position)
|
||||
{
|
||||
minizip_opt *options = (minizip_opt *)userdata;
|
||||
double progress = 0;
|
||||
uint8_t raw = 0;
|
||||
|
||||
MZ_UNUSED(userdata);
|
||||
|
||||
mz_zip_writer_get_raw(handle, &raw);
|
||||
|
||||
if (raw && file_info->compressed_size > 0)
|
||||
progress = ((double)position / file_info->compressed_size) * 100;
|
||||
else if (!raw && file_info->uncompressed_size > 0)
|
||||
progress = ((double)position / file_info->uncompressed_size) * 100;
|
||||
|
||||
if (options->verbose)
|
||||
printf("%s - %"PRIu64" / %"PRIu64" (%.02f%%)\n", file_info->filename, position,
|
||||
file_info->uncompressed_size, progress);
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t minizip_add_overwrite_cb(void *handle, void *userdata, const char *path)
|
||||
{
|
||||
minizip_opt *options = (minizip_opt *)userdata;
|
||||
|
||||
MZ_UNUSED(handle);
|
||||
|
||||
if (options->overwrite == 0)
|
||||
{
|
||||
// If ask the user what to do because append and overwrite args not set
|
||||
char rep = 0;
|
||||
do
|
||||
{
|
||||
char answer[128];
|
||||
printf("The file %s exists. Overwrite ? [y]es, [n]o, [a]ppend : ", path);
|
||||
if (scanf("%1s", answer) != 1)
|
||||
exit(EXIT_FAILURE);
|
||||
rep = answer[0];
|
||||
|
||||
if ((rep >= 'a') && (rep <= 'z'))
|
||||
rep -= 0x20;
|
||||
}
|
||||
while ((rep != 'Y') && (rep != 'N') && (rep != 'A'));
|
||||
|
||||
if (rep == 'A')
|
||||
{
|
||||
return MZ_EXIST_ERROR;
|
||||
}
|
||||
else if (rep == 'N')
|
||||
{
|
||||
return MZ_INTERNAL_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t minizip_add(const char *path, const char *password, minizip_opt *options, int32_t arg_count, const char **args)
|
||||
{
|
||||
void *writer = NULL;
|
||||
int32_t err = MZ_OK;
|
||||
int32_t err_close = MZ_OK;
|
||||
int32_t i = 0;
|
||||
const char *filename_in_zip = NULL;
|
||||
|
||||
|
||||
printf("Archive %s\n", path);
|
||||
|
||||
mz_zip_writer_create(&writer);
|
||||
mz_zip_writer_set_password(writer, password);
|
||||
mz_zip_writer_set_compress_method(writer, options->compress_method);
|
||||
mz_zip_writer_set_compress_level(writer, options->compress_level);
|
||||
mz_zip_writer_set_overwrite_cb(writer, options, minizip_add_overwrite_cb);
|
||||
mz_zip_writer_set_progress_cb(writer, options, minizip_add_progress_cb);
|
||||
mz_zip_writer_set_entry_cb(writer, options, minizip_add_entry_cb);
|
||||
mz_zip_writer_set_zip_cd(writer, options->zip_cd);
|
||||
if (options->cert_path != NULL)
|
||||
mz_zip_writer_set_certificate(writer, options->cert_path, options->cert_pwd);
|
||||
|
||||
err = mz_zip_writer_open_file(writer, path, options->disk_size, options->append);
|
||||
|
||||
if (err == MZ_OK)
|
||||
{
|
||||
for (i = 0; i < arg_count; i += 1)
|
||||
{
|
||||
filename_in_zip = args[i];
|
||||
|
||||
err = mz_zip_writer_add_path(writer, filename_in_zip, NULL, options->include_path, 1);
|
||||
if (err != MZ_OK)
|
||||
printf("Error %d adding path to zip %s\n", err, filename_in_zip);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Error %d opening zip for writing\n", err);
|
||||
}
|
||||
|
||||
err_close = mz_zip_writer_close(writer);
|
||||
if (err_close != MZ_OK)
|
||||
{
|
||||
printf("Error %d closing zip for writing %s\n", err_close, path);
|
||||
err = err_close;
|
||||
}
|
||||
|
||||
mz_zip_writer_delete(&writer);
|
||||
return err;
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t minizip_extract_entry_cb(void *handle, void *userdata, mz_zip_file *file_info, const char *path)
|
||||
{
|
||||
MZ_UNUSED(handle);
|
||||
MZ_UNUSED(userdata);
|
||||
MZ_UNUSED(path);
|
||||
|
||||
printf("Extracting %s\n", file_info->filename);
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t minizip_extract_progress_cb(void *handle, void *userdata, mz_zip_file *file_info, int64_t position)
|
||||
{
|
||||
minizip_opt *options = (minizip_opt *)userdata;
|
||||
double progress = 0;
|
||||
uint8_t raw = 0;
|
||||
|
||||
MZ_UNUSED(userdata);
|
||||
|
||||
mz_zip_reader_get_raw(handle, &raw);
|
||||
|
||||
if (raw && file_info->compressed_size > 0)
|
||||
progress = ((double)position / file_info->compressed_size) * 100;
|
||||
else if (!raw && file_info->uncompressed_size > 0)
|
||||
progress = ((double)position / file_info->uncompressed_size) * 100;
|
||||
|
||||
if (options->verbose)
|
||||
printf("%s - %"PRIu64" / %"PRIu64" (%.02f%%)\n", file_info->filename, position,
|
||||
file_info->uncompressed_size, progress);
|
||||
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t minizip_extract_overwrite_cb(void *handle, void *userdata, mz_zip_file *file_info, const char *path)
|
||||
{
|
||||
minizip_opt *options = (minizip_opt *)userdata;
|
||||
|
||||
MZ_UNUSED(handle);
|
||||
MZ_UNUSED(file_info);
|
||||
|
||||
if (options->overwrite == 0)
|
||||
{
|
||||
char rep = 0;
|
||||
do
|
||||
{
|
||||
char answer[128];
|
||||
printf("The file %s exists. Overwrite ? [y]es, [n]o, [A]ll: ", path);
|
||||
if (scanf("%1s", answer) != 1)
|
||||
exit(EXIT_FAILURE);
|
||||
rep = answer[0];
|
||||
if ((rep >= 'a') && (rep <= 'z'))
|
||||
rep -= 0x20;
|
||||
} while ((rep != 'Y') && (rep != 'N') && (rep != 'A'));
|
||||
|
||||
if (rep == 'N')
|
||||
return MZ_EXIST_ERROR;
|
||||
if (rep == 'A')
|
||||
options->overwrite = 1;
|
||||
}
|
||||
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t minizip_extract(const char *path, const char *pattern, const char *destination, const char *password, minizip_opt *options)
|
||||
{
|
||||
void *reader = NULL;
|
||||
int32_t err = MZ_OK;
|
||||
int32_t err_close = MZ_OK;
|
||||
|
||||
|
||||
printf("Archive %s\n", path);
|
||||
|
||||
mz_zip_reader_create(&reader);
|
||||
mz_zip_reader_set_pattern(reader, pattern, 1);
|
||||
mz_zip_reader_set_password(reader, password);
|
||||
mz_zip_reader_set_encoding(reader, options->encoding);
|
||||
mz_zip_reader_set_entry_cb(reader, options, minizip_extract_entry_cb);
|
||||
mz_zip_reader_set_progress_cb(reader, options, minizip_extract_progress_cb);
|
||||
mz_zip_reader_set_overwrite_cb(reader, options, minizip_extract_overwrite_cb);
|
||||
|
||||
err = mz_zip_reader_open_file(reader, path);
|
||||
|
||||
if (err != MZ_OK)
|
||||
{
|
||||
printf("Error %d opening zip file %s\n", err, path);
|
||||
}
|
||||
else
|
||||
{
|
||||
err = mz_zip_reader_save_all(reader, destination);
|
||||
if (err == MZ_END_OF_LIST && pattern != NULL)
|
||||
printf("Files matching %s not found in zip file\n", pattern);
|
||||
if (err != MZ_OK)
|
||||
printf("Error %d saving zip entries to disk %s\n", err, path);
|
||||
}
|
||||
|
||||
err_close = mz_zip_reader_close(reader);
|
||||
if (err_close != MZ_OK)
|
||||
{
|
||||
printf("Error %d closing zip for reading\n", err_close);
|
||||
err = err_close;
|
||||
}
|
||||
|
||||
mz_zip_reader_delete(&reader);
|
||||
return err;
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t minizip_erase(const char *src_path, const char *target_path, int32_t arg_count, const char **args)
|
||||
{
|
||||
mz_zip_file *file_info = NULL;
|
||||
const char *filename_in_zip = NULL;
|
||||
void *reader = NULL;
|
||||
void *writer = NULL;
|
||||
int32_t err = MZ_OK;
|
||||
int32_t i = 0;
|
||||
|
||||
mz_zip_reader_create(&reader);
|
||||
mz_zip_writer_create(&writer);
|
||||
|
||||
err = mz_zip_reader_open_file(reader, src_path);
|
||||
if (err != MZ_OK)
|
||||
{
|
||||
printf("Error %d opening zip for reading %s\n", err, src_path);
|
||||
mz_zip_reader_delete(&reader);
|
||||
return err;
|
||||
}
|
||||
|
||||
err = mz_zip_writer_open_file(writer, target_path, 0, 0);
|
||||
if (err != MZ_OK)
|
||||
{
|
||||
printf("Error %d opening zip for writing %s\n", err, target_path);
|
||||
mz_zip_reader_delete(&reader);
|
||||
mz_zip_writer_delete(&writer);
|
||||
return err;
|
||||
}
|
||||
|
||||
err = mz_zip_reader_goto_first_entry(reader);
|
||||
|
||||
if (err != MZ_OK && err != MZ_END_OF_LIST)
|
||||
printf("Error %d going to first entry in zip file\n", err);
|
||||
|
||||
while (err == MZ_OK)
|
||||
{
|
||||
err = mz_zip_reader_entry_get_info(reader, &file_info);
|
||||
if (err != MZ_OK)
|
||||
{
|
||||
printf("Error %d getting info from zip\n", err);
|
||||
break;
|
||||
}
|
||||
|
||||
for (i = 0; i < arg_count; i += 1)
|
||||
{
|
||||
filename_in_zip = args[i];
|
||||
|
||||
if (mz_path_compare_wc(file_info->filename, filename_in_zip, 1) != MZ_OK)
|
||||
{
|
||||
printf("Copying %s\n", file_info->filename);
|
||||
err = mz_zip_writer_copy_from_reader(writer, reader);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Skipping %s\n", file_info->filename);
|
||||
}
|
||||
}
|
||||
|
||||
if (err != MZ_OK)
|
||||
{
|
||||
printf("Error %d copying entry into new zip\n", err);
|
||||
break;
|
||||
}
|
||||
|
||||
err = mz_zip_reader_goto_next_entry(reader);
|
||||
|
||||
if (err != MZ_OK && err != MZ_END_OF_LIST)
|
||||
printf("Error %d going to next entry in zip file\n", err);
|
||||
}
|
||||
|
||||
mz_zip_reader_close(reader);
|
||||
mz_zip_reader_delete(&reader);
|
||||
|
||||
mz_zip_writer_close(writer);
|
||||
mz_zip_writer_delete(&writer);
|
||||
|
||||
if (err == MZ_END_OF_LIST)
|
||||
return MZ_OK;
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#if !defined(MZ_ZIP_NO_MAIN)
|
||||
int main(int argc, const char *argv[])
|
||||
{
|
||||
minizip_opt options;
|
||||
int32_t path_arg = 0;
|
||||
int32_t err = 0;
|
||||
int32_t i = 0;
|
||||
uint8_t do_list = 0;
|
||||
uint8_t do_extract = 0;
|
||||
uint8_t do_erase = 0;
|
||||
char bak_path[256];
|
||||
char tmp_path[256];
|
||||
const char *path = NULL;
|
||||
const char *password = NULL;
|
||||
const char *destination = NULL;
|
||||
const char *filename_to_extract = NULL;
|
||||
|
||||
|
||||
minizip_banner();
|
||||
if (argc == 1)
|
||||
{
|
||||
minizip_help();
|
||||
return 0;
|
||||
}
|
||||
|
||||
memset(&options, 0, sizeof(options));
|
||||
|
||||
options.compress_method = MZ_COMPRESS_METHOD_DEFLATE;
|
||||
options.compress_level = MZ_COMPRESS_LEVEL_DEFAULT;
|
||||
|
||||
// Parse command line options
|
||||
for (i = 1; i < argc; i += 1)
|
||||
{
|
||||
if (argv[i][0] == '-')
|
||||
{
|
||||
char c = argv[i][1];
|
||||
if ((c == 'l') || (c == 'L'))
|
||||
do_list = 1;
|
||||
else if ((c == 'x') || (c == 'X'))
|
||||
do_extract = 1;
|
||||
else if ((c == 'e') || (c == 'E'))
|
||||
do_erase = 1;
|
||||
else if ((c == 'a') || (c == 'A'))
|
||||
options.append = 1;
|
||||
else if ((c == 'o') || (c == 'O'))
|
||||
options.overwrite = 1;
|
||||
else if ((c == 'i') || (c == 'I'))
|
||||
options.include_path = 1;
|
||||
else if ((c == 'z') || (c == 'Z'))
|
||||
options.zip_cd = 1;
|
||||
else if ((c == 'v') || (c == 'V'))
|
||||
options.verbose = 1;
|
||||
else if ((c >= '0') && (c <= '9'))
|
||||
{
|
||||
options.compress_level = (c - '0');
|
||||
if (options.compress_level == 0)
|
||||
options.compress_method = MZ_COMPRESS_METHOD_STORE;
|
||||
}
|
||||
else if ((c == 'b') || (c == 'B'))
|
||||
#ifdef HAVE_BZIP2
|
||||
options.compress_method = MZ_COMPRESS_METHOD_BZIP2;
|
||||
#else
|
||||
err = MZ_SUPPORT_ERROR;
|
||||
#endif
|
||||
else if ((c == 'm') || (c == 'M'))
|
||||
#ifdef HAVE_LZMA
|
||||
options.compress_method = MZ_COMPRESS_METHOD_LZMA;
|
||||
#else
|
||||
err = MZ_SUPPORT_ERROR;
|
||||
#endif
|
||||
else if ((c == 's') || (c == 'S'))
|
||||
#ifdef HAVE_AES
|
||||
options.aes = 1;
|
||||
#else
|
||||
err = MZ_SUPPORT_ERROR;
|
||||
#endif
|
||||
else if (((c == 'h') || (c == 'H')) && (i + 1 < argc))
|
||||
{
|
||||
#ifndef MZ_ZIP_NO_SIGNING
|
||||
options.cert_path = argv[i + 1];
|
||||
#else
|
||||
err = MZ_SUPPORT_ERROR;
|
||||
#endif
|
||||
i += 1;
|
||||
}
|
||||
else if (((c == 'w') || (c == 'W')) && (i + 1 < argc))
|
||||
{
|
||||
#ifndef MZ_ZIP_NO_SIGNING
|
||||
options.cert_pwd = argv[i + 1];
|
||||
#else
|
||||
err = MZ_SUPPORT_ERROR;
|
||||
#endif
|
||||
i += 1;
|
||||
}
|
||||
else if (((c == 'c') || (c == 'C')) && (i + 1 < argc))
|
||||
{
|
||||
options.encoding = (int32_t)atoi(argv[i + 1]);
|
||||
i += 1;
|
||||
}
|
||||
else if (((c == 'k') || (c == 'K')) && (i + 1 < argc))
|
||||
{
|
||||
options.disk_size = (int64_t)atoi(argv[i + 1]) * 1024;
|
||||
i += 1;
|
||||
}
|
||||
else if (((c == 'd') || (c == 'D')) && (i + 1 < argc))
|
||||
{
|
||||
destination = argv[i + 1];
|
||||
i += 1;
|
||||
}
|
||||
else if (((c == 'p') || (c == 'P')) && (i + 1 < argc))
|
||||
{
|
||||
#ifndef MZ_ZIP_NO_ENCRYPTION
|
||||
password = argv[i + 1];
|
||||
#else
|
||||
err = MZ_SUPPORT_ERROR;
|
||||
#endif
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
else if (path_arg == 0)
|
||||
path_arg = i;
|
||||
}
|
||||
|
||||
if (err == MZ_SUPPORT_ERROR)
|
||||
{
|
||||
printf("Feature not supported\n");
|
||||
return err;
|
||||
}
|
||||
|
||||
if (path_arg == 0)
|
||||
{
|
||||
minizip_help();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
path = argv[path_arg];
|
||||
|
||||
if (do_list)
|
||||
{
|
||||
err = minizip_list(path);
|
||||
}
|
||||
else if (do_extract)
|
||||
{
|
||||
if (argc > path_arg + 1)
|
||||
filename_to_extract = argv[path_arg + 1];
|
||||
|
||||
err = minizip_extract(path, filename_to_extract, destination, password, &options);
|
||||
}
|
||||
else if (do_erase)
|
||||
{
|
||||
strncpy(tmp_path, path, sizeof(tmp_path) - 1);
|
||||
tmp_path[sizeof(tmp_path) - 1] = 0;
|
||||
strncat(tmp_path, ".tmp", sizeof(tmp_path) - strlen(tmp_path) - 1);
|
||||
|
||||
err = minizip_erase(path, tmp_path, argc - (path_arg + 1), &argv[path_arg + 1]);
|
||||
}
|
||||
else
|
||||
{
|
||||
err = minizip_add(path, password, &options, argc - (path_arg + 1), &argv[path_arg + 1]);
|
||||
}
|
||||
|
||||
if (err == MZ_OK && do_erase)
|
||||
{
|
||||
// Swap zip with temporary zip, backup old zip if possible
|
||||
strncpy(bak_path, path, sizeof(bak_path) - 1);
|
||||
bak_path[sizeof(bak_path) - 1] = 0;
|
||||
strncat(bak_path, ".bak", sizeof(bak_path) - strlen(bak_path) - 1);
|
||||
|
||||
if (mz_os_file_exists(bak_path) == MZ_OK)
|
||||
mz_os_delete(bak_path);
|
||||
|
||||
if (mz_os_rename(path, bak_path) != MZ_OK)
|
||||
printf("Error backing up zip before replacing %s\n", bak_path);
|
||||
|
||||
if (mz_os_rename(tmp_path, path) != MZ_OK)
|
||||
printf("Error replacing zip with temp %s\n", tmp_path);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
#endif
|
||||
+2
-2
@@ -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}
|
||||
|
||||
@@ -0,0 +1,156 @@
|
||||
/* mz.h -- Errors codes, zip flags and magic
|
||||
Version 2.7.3, November 4, 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_H
|
||||
#define MZ_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
// MZ_VERSION
|
||||
#define MZ_VERSION ("2.7.3")
|
||||
|
||||
// MZ_ERROR
|
||||
#define MZ_OK (0) // zlib
|
||||
#define MZ_STREAM_ERROR (-1) // zlib
|
||||
#define MZ_DATA_ERROR (-3) // zlib
|
||||
#define MZ_MEM_ERROR (-4) // zlib
|
||||
#define MZ_BUF_ERROR (-5) // zlib
|
||||
#define MZ_VERSION_ERROR (-6) // zlib
|
||||
|
||||
#define MZ_END_OF_LIST (-100)
|
||||
#define MZ_END_OF_STREAM (-101)
|
||||
|
||||
#define MZ_PARAM_ERROR (-102)
|
||||
#define MZ_FORMAT_ERROR (-103)
|
||||
#define MZ_INTERNAL_ERROR (-104)
|
||||
#define MZ_CRC_ERROR (-105)
|
||||
#define MZ_CRYPT_ERROR (-106)
|
||||
#define MZ_EXIST_ERROR (-107)
|
||||
#define MZ_PASSWORD_ERROR (-108)
|
||||
#define MZ_SUPPORT_ERROR (-109)
|
||||
#define MZ_HASH_ERROR (-110)
|
||||
#define MZ_OPEN_ERROR (-111)
|
||||
#define MZ_CLOSE_ERROR (-112)
|
||||
#define MZ_SEEK_ERROR (-113)
|
||||
#define MZ_TELL_ERROR (-114)
|
||||
#define MZ_READ_ERROR (-115)
|
||||
#define MZ_WRITE_ERROR (-116)
|
||||
#define MZ_SIGN_ERROR (-117)
|
||||
|
||||
// MZ_OPEN
|
||||
#define MZ_OPEN_MODE_READ (0x01)
|
||||
#define MZ_OPEN_MODE_WRITE (0x02)
|
||||
#define MZ_OPEN_MODE_READWRITE (MZ_OPEN_MODE_READ | MZ_OPEN_MODE_WRITE)
|
||||
#define MZ_OPEN_MODE_APPEND (0x04)
|
||||
#define MZ_OPEN_MODE_CREATE (0x08)
|
||||
#define MZ_OPEN_MODE_EXISTING (0x10)
|
||||
|
||||
// MZ_SEEK
|
||||
#define MZ_SEEK_SET (0)
|
||||
#define MZ_SEEK_CUR (1)
|
||||
#define MZ_SEEK_END (2)
|
||||
|
||||
// MZ_COMPRESS
|
||||
#define MZ_COMPRESS_METHOD_STORE (0)
|
||||
#define MZ_COMPRESS_METHOD_DEFLATE (8)
|
||||
#define MZ_COMPRESS_METHOD_BZIP2 (12)
|
||||
#define MZ_COMPRESS_METHOD_LZMA (14)
|
||||
#define MZ_COMPRESS_METHOD_AES (99)
|
||||
|
||||
#define MZ_COMPRESS_LEVEL_DEFAULT (-1)
|
||||
#define MZ_COMPRESS_LEVEL_FAST (2)
|
||||
#define MZ_COMPRESS_LEVEL_NORMAL (6)
|
||||
#define MZ_COMPRESS_LEVEL_BEST (9)
|
||||
|
||||
// MZ_ZIP_FLAG
|
||||
#define MZ_ZIP_FLAG_ENCRYPTED (1 << 0)
|
||||
#define MZ_ZIP_FLAG_LZMA_EOS_MARKER (1 << 1)
|
||||
#define MZ_ZIP_FLAG_DEFLATE_MAX (1 << 1)
|
||||
#define MZ_ZIP_FLAG_DEFLATE_NORMAL (0)
|
||||
#define MZ_ZIP_FLAG_DEFLATE_FAST (1 << 2)
|
||||
#define MZ_ZIP_FLAG_DEFLATE_SUPER_FAST (MZ_ZIP_FLAG_DEFLATE_FAST | \
|
||||
MZ_ZIP_FLAG_DEFLATE_MAX)
|
||||
#define MZ_ZIP_FLAG_DATA_DESCRIPTOR (1 << 3)
|
||||
#define MZ_ZIP_FLAG_UTF8 (1 << 11)
|
||||
#define MZ_ZIP_FLAG_MASK_LOCAL_INFO (1 << 13)
|
||||
|
||||
// MZ_ZIP_EXTENSION
|
||||
#define MZ_ZIP_EXTENSION_ZIP64 (0x0001)
|
||||
#define MZ_ZIP_EXTENSION_NTFS (0x000a)
|
||||
#define MZ_ZIP_EXTENSION_AES (0x9901)
|
||||
#define MZ_ZIP_EXTENSION_UNIX1 (0x000d)
|
||||
#define MZ_ZIP_EXTENSION_SIGN (0x10c5)
|
||||
#define MZ_ZIP_EXTENSION_HASH (0x1a51)
|
||||
#define MZ_ZIP_EXTENSION_CDCD (0xcdcd)
|
||||
|
||||
// MZ_ZIP64
|
||||
#define MZ_ZIP64_AUTO (0)
|
||||
#define MZ_ZIP64_FORCE (1)
|
||||
#define MZ_ZIP64_DISABLE (2)
|
||||
|
||||
// MZ_HOST_SYSTEM
|
||||
#define MZ_HOST_SYSTEM(VERSION_MADEBY) ((uint8_t)(VERSION_MADEBY >> 8))
|
||||
#define MZ_HOST_SYSTEM_MSDOS (0)
|
||||
#define MZ_HOST_SYSTEM_UNIX (3)
|
||||
#define MZ_HOST_SYSTEM_WINDOWS_NTFS (10)
|
||||
#define MZ_HOST_SYSTEM_OSX_DARWIN (19)
|
||||
|
||||
// MZ_PKCRYPT
|
||||
#define MZ_PKCRYPT_HEADER_SIZE (12)
|
||||
|
||||
// MZ_AES
|
||||
#define MZ_AES_VERSION (1)
|
||||
#define MZ_AES_ENCRYPTION_MODE_128 (0x01)
|
||||
#define MZ_AES_ENCRYPTION_MODE_192 (0x02)
|
||||
#define MZ_AES_ENCRYPTION_MODE_256 (0x03)
|
||||
#define MZ_AES_KEY_LENGTH(MODE) (8 * (MODE & 3) + 8)
|
||||
#define MZ_AES_KEY_LENGTH_MAX (32)
|
||||
#define MZ_AES_BLOCK_SIZE (16)
|
||||
#define MZ_AES_HEADER_SIZE(MODE) ((4 * (MODE & 3) + 4) + 2)
|
||||
#define MZ_AES_FOOTER_SIZE (10)
|
||||
|
||||
// MZ_HASH
|
||||
#define MZ_HASH_MD5 (10)
|
||||
#define MZ_HASH_MD5_SIZE (16)
|
||||
#define MZ_HASH_SHA1 (20)
|
||||
#define MZ_HASH_SHA1_SIZE (20)
|
||||
#define MZ_HASH_SHA256 (23)
|
||||
#define MZ_HASH_SHA256_SIZE (32)
|
||||
#define MZ_HASH_MAX_SIZE (256)
|
||||
|
||||
// MZ_ENCODING
|
||||
#define MZ_ENCODING_CODEPAGE_437 (437)
|
||||
#define MZ_ENCODING_CODEPAGE_932 (932)
|
||||
#define MZ_ENCODING_CODEPAGE_936 (936)
|
||||
#define MZ_ENCODING_CODEPAGE_950 (950)
|
||||
#define MZ_ENCODING_UTF8 (65001)
|
||||
|
||||
// MZ_UTILITY
|
||||
#define MZ_UNUSED(SYMBOL) ((void)SYMBOL)
|
||||
|
||||
#ifndef MZ_CUSTOM_ALLOC
|
||||
#define MZ_ALLOC(SIZE) (malloc(SIZE))
|
||||
#endif
|
||||
#ifndef MZ_CUSTOM_FREE
|
||||
#define MZ_FREE(PTR) (free(PTR))
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
+1031
File diff suppressed because it is too large
Load Diff
+356
@@ -0,0 +1,356 @@
|
||||
/* mz_compat.h -- Backwards compatible interface for older versions
|
||||
Version 2.7.3, November 4, 2018
|
||||
part of the MiniZip project
|
||||
|
||||
Copyright (C) 2010-2018 Nathan Moinvaziri
|
||||
https://github.com/nmoinvaz/minizip
|
||||
Copyright (C) 1998-2010 Gilles Vollant
|
||||
https://www.winimage.com/zLibDll/minizip.html
|
||||
|
||||
This program is distributed under the terms of the same license as zlib.
|
||||
See the accompanying LICENSE file for the full text of the license.
|
||||
*/
|
||||
|
||||
#ifndef MZ_COMPAT_H
|
||||
#define MZ_COMPAT_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <time.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#if defined(HAVE_ZLIB) && defined(MAX_MEM_LEVEL)
|
||||
#ifndef DEF_MEM_LEVEL
|
||||
# if MAX_MEM_LEVEL >= 8
|
||||
# define DEF_MEM_LEVEL 8
|
||||
# else
|
||||
# define DEF_MEM_LEVEL MAX_MEM_LEVEL
|
||||
# endif
|
||||
#endif
|
||||
#endif
|
||||
#ifndef MAX_WBITS
|
||||
#define MAX_WBITS (15)
|
||||
#endif
|
||||
#ifndef DEF_MEM_LEVEL
|
||||
#define DEF_MEM_LEVEL (8)
|
||||
#endif
|
||||
|
||||
#ifndef ZEXPORT
|
||||
# define ZEXPORT
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#if defined(USE_FILE32API)
|
||||
# define fopen64 fopen
|
||||
# define ftello64 ftell
|
||||
# define fseeko64 fseek
|
||||
#else
|
||||
# if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || \
|
||||
defined(__OpenBSD__) || defined(__APPLE__) || defined(__ANDROID__)
|
||||
# define fopen64 fopen
|
||||
# define ftello64 ftello
|
||||
# define fseeko64 fseeko
|
||||
# endif
|
||||
# ifdef _MSC_VER
|
||||
# define fopen64 fopen
|
||||
# if (_MSC_VER >= 1400) && (!(defined(NO_MSCVER_FILE64_FUNC)))
|
||||
# define ftello64 _ftelli64
|
||||
# define fseeko64 _fseeki64
|
||||
# else /* old MSC */
|
||||
# define ftello64 ftell
|
||||
# define fseeko64 fseek
|
||||
# endif
|
||||
# endif
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#if defined(STRICTZIP) || defined(STRICTZIPUNZIP)
|
||||
/* like the STRICT of WIN32, we define a pointer that cannot be converted
|
||||
from (void*) without cast */
|
||||
typedef struct TagzipFile__ { int unused; } zip_file__;
|
||||
typedef zip_file__ *zipFile;
|
||||
#else
|
||||
typedef void *zipFile;
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef 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 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;
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#define ZIP_OK (0)
|
||||
#define ZIP_EOF (0)
|
||||
#define ZIP_ERRNO (-1)
|
||||
#define ZIP_PARAMERROR (-102)
|
||||
#define ZIP_BADZIPFILE (-103)
|
||||
#define ZIP_INTERNALERROR (-104)
|
||||
|
||||
#define Z_BZIP2ED (12)
|
||||
|
||||
#define APPEND_STATUS_CREATE (0)
|
||||
#define APPEND_STATUS_CREATEAFTER (1)
|
||||
#define APPEND_STATUS_ADDINZIP (2)
|
||||
|
||||
/***************************************************************************/
|
||||
// Writing a zip file
|
||||
|
||||
zipFile ZEXPORT zipOpen(const char *path, int append);
|
||||
zipFile ZEXPORT zipOpen64(const void *path, int append);
|
||||
zipFile ZEXPORT zipOpen2(const char *path, int append, const char **globalcomment,
|
||||
zlib_filefunc_def *pzlib_filefunc_def);
|
||||
zipFile ZEXPORT zipOpen2_64(const void *path, int append, const char **globalcomment,
|
||||
zlib_filefunc64_def *pzlib_filefunc_def);
|
||||
zipFile ZEXPORT zipOpen_MZ(void *stream, int append, const char **globalcomment);
|
||||
|
||||
int ZEXPORT zipOpenNewFileInZip3(zipFile file, const char *filename, const zip_fileinfo *zipfi,
|
||||
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
|
||||
uint16_t size_extrafield_global, const char *comment, uint16_t compression_method, int level,
|
||||
int raw, int windowBits, int memLevel, int strategy, const char *password,
|
||||
uint32_t crc_for_crypting);
|
||||
int ZEXPORT zipOpenNewFileInZip3_64(zipFile file, const char *filename, const zip_fileinfo *zipfi,
|
||||
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
|
||||
uint16_t size_extrafield_global, const char *comment, uint16_t compression_method, int level,
|
||||
int raw, int windowBits, int memLevel, int strategy, const char *password,
|
||||
uint32_t crc_for_crypting, int zip64);
|
||||
int ZEXPORT zipOpenNewFileInZip4(zipFile file, const char *filename, const zip_fileinfo *zipfi,
|
||||
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
|
||||
uint16_t size_extrafield_global, const char *comment, uint16_t compression_method, int level,
|
||||
int raw, int windowBits, int memLevel, int strategy, const char *password,
|
||||
uint32_t crc_for_crypting, uint16_t version_madeby, uint16_t flag_base);
|
||||
int ZEXPORT zipOpenNewFileInZip4_64(zipFile file, const char *filename, const zip_fileinfo *zipfi,
|
||||
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
|
||||
uint16_t size_extrafield_global, const char *comment, uint16_t compression_method, int level,
|
||||
int raw, int windowBits, int memLevel, int strategy, const char *password,
|
||||
uint32_t crc_for_crypting, uint16_t version_madeby, uint16_t flag_base, int zip64);
|
||||
int ZEXPORT zipOpenNewFileInZip5(zipFile file, const char *filename, const zip_fileinfo *zipfi,
|
||||
const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
|
||||
uint16_t size_extrafield_global, const char *comment, uint16_t compression_method, int level,
|
||||
int raw, int windowBits, int memLevel, int strategy, const char *password,
|
||||
uint32_t crc_for_crypting, uint16_t version_madeby, uint16_t flag_base, int zip64);
|
||||
|
||||
int ZEXPORT zipWriteInFileInZip(zipFile file, const void *buf, uint32_t len);
|
||||
|
||||
int ZEXPORT zipCloseFileInZipRaw(zipFile file, uint32_t uncompressed_size, uint32_t crc32);
|
||||
int ZEXPORT zipCloseFileInZipRaw64(zipFile file, int64_t uncompressed_size, uint32_t crc32);
|
||||
int ZEXPORT zipCloseFileInZip(zipFile file);
|
||||
int ZEXPORT zipCloseFileInZip64(zipFile file);
|
||||
|
||||
int ZEXPORT zipClose(zipFile file, const char *global_comment);
|
||||
int ZEXPORT zipClose_64(zipFile file, const char *global_comment);
|
||||
int ZEXPORT zipClose2_64(zipFile file, const char *global_comment, uint16_t version_madeby);
|
||||
int ZEXPORT zipClose_MZ(zipFile file, const char *global_comment);
|
||||
int ZEXPORT zipClose2_MZ(zipFile file, const char *global_comment, uint16_t version_madeby);
|
||||
void* ZEXPORT zipGetStream(zipFile file);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#if defined(STRICTUNZIP) || defined(STRICTZIPUNZIP)
|
||||
/* like the STRICT of WIN32, we define a pointer that cannot be converted
|
||||
from (void*) without cast */
|
||||
typedef struct TagunzFile__ { int unused; } unz_file__;
|
||||
typedef unz_file__ *unzFile;
|
||||
#else
|
||||
typedef void *unzFile;
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#define UNZ_OK (0)
|
||||
#define UNZ_END_OF_LIST_OF_FILE (-100)
|
||||
#define UNZ_ERRNO (-1)
|
||||
#define UNZ_EOF (0)
|
||||
#define UNZ_PARAMERROR (-102)
|
||||
#define UNZ_BADZIPFILE (-103)
|
||||
#define UNZ_INTERNALERROR (-104)
|
||||
#define UNZ_CRCERROR (-105)
|
||||
#define UNZ_BADPASSWORD (-106)
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct unz_global_info64_s
|
||||
{
|
||||
uint64_t number_entry; // total number of entries in the central dir on this disk
|
||||
uint32_t number_disk_with_CD; // number the the disk with central dir, used for spanning ZIP
|
||||
uint16_t size_comment; // size of the global comment of the zipfile
|
||||
} unz_global_info64;
|
||||
|
||||
typedef struct unz_global_info_s
|
||||
{
|
||||
uint32_t number_entry; // total number of entries in the central dir on this disk
|
||||
uint32_t number_disk_with_CD; // number the the disk with central dir, used for spanning ZIP
|
||||
uint16_t size_comment; // size of the global comment of the zipfile
|
||||
} unz_global_info;
|
||||
|
||||
typedef struct unz_file_info64_s
|
||||
{
|
||||
uint16_t version; // version made by 2 bytes
|
||||
uint16_t version_needed; // version needed to extract 2 bytes
|
||||
uint16_t flag; // general purpose bit flag 2 bytes
|
||||
uint16_t compression_method; // compression method 2 bytes
|
||||
uint32_t 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
|
||||
|
||||
uint64_t disk_offset;
|
||||
|
||||
uint16_t size_file_extra_internal;
|
||||
} unz_file_info64;
|
||||
|
||||
typedef struct unz_file_info_s
|
||||
{
|
||||
uint16_t version; // version made by 2 bytes
|
||||
uint16_t version_needed; // version needed to extract 2 bytes
|
||||
uint16_t flag; // general purpose bit flag 2 bytes
|
||||
uint16_t compression_method; // compression method 2 bytes
|
||||
uint32_t 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
|
||||
uint32_t external_fa; // external file attributes 4 bytes
|
||||
|
||||
uint64_t disk_offset;
|
||||
} unz_file_info;
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef int (*unzFileNameComparer)(unzFile file, const char *filename1, const char *filename2);
|
||||
typedef int (*unzIteratorFunction)(unzFile file);
|
||||
typedef int (*unzIteratorFunction2)(unzFile file, unz_file_info64 *pfile_info, char *filename,
|
||||
uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment, uint16_t comment_size);
|
||||
|
||||
/***************************************************************************/
|
||||
// Opening and close a zip file
|
||||
|
||||
unzFile ZEXPORT unzOpen(const char *path);
|
||||
unzFile ZEXPORT unzOpen64(const void *path);
|
||||
unzFile ZEXPORT unzOpen2(const char *path, zlib_filefunc_def *pzlib_filefunc_def);
|
||||
unzFile ZEXPORT unzOpen2_64(const void *path, zlib_filefunc64_def *pzlib_filefunc_def);
|
||||
unzFile ZEXPORT unzOpen_MZ(void *stream);
|
||||
|
||||
int ZEXPORT unzClose(unzFile file);
|
||||
int ZEXPORT unzClose_MZ(unzFile file);
|
||||
|
||||
int ZEXPORT unzGetGlobalInfo(unzFile file, unz_global_info* pglobal_info32);
|
||||
int ZEXPORT unzGetGlobalInfo64(unzFile file, unz_global_info64 *pglobal_info);
|
||||
int ZEXPORT unzGetGlobalComment(unzFile file, char *comment, uint16_t comment_size);
|
||||
|
||||
int ZEXPORT unzOpenCurrentFile(unzFile file);
|
||||
int ZEXPORT unzOpenCurrentFilePassword(unzFile file, const char *password);
|
||||
int ZEXPORT unzOpenCurrentFile2(unzFile file, int *method, int *level, int raw);
|
||||
int ZEXPORT unzOpenCurrentFile3(unzFile file, int *method, int *level, int raw, const char *password);
|
||||
int ZEXPORT unzReadCurrentFile(unzFile file, void *buf, uint32_t len);
|
||||
int ZEXPORT unzCloseCurrentFile(unzFile file);
|
||||
|
||||
|
||||
int ZEXPORT unzGetCurrentFileInfo(unzFile file, unz_file_info *pfile_info, char *filename,
|
||||
uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment, uint16_t comment_size);
|
||||
int ZEXPORT unzGetCurrentFileInfo64(unzFile file, unz_file_info64 * pfile_info, char *filename,
|
||||
uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment, uint16_t comment_size);
|
||||
|
||||
int ZEXPORT unzGoToFirstFile(unzFile file);
|
||||
int ZEXPORT unzGoToNextFile(unzFile file);
|
||||
int ZEXPORT unzLocateFile(unzFile file, const char *filename, unzFileNameComparer filename_compare_func);
|
||||
|
||||
int ZEXPORT unzGetLocalExtrafield(unzFile file, void *buf, unsigned len);
|
||||
|
||||
/***************************************************************************/
|
||||
// Raw access to zip file
|
||||
|
||||
typedef struct unz_file_pos_s
|
||||
{
|
||||
uint32_t pos_in_zip_directory; // offset in zip file directory
|
||||
uint32_t num_of_file; // # of file
|
||||
} unz_file_pos;
|
||||
|
||||
int ZEXPORT unzGetFilePos(unzFile file, unz_file_pos *file_pos);
|
||||
int ZEXPORT unzGoToFilePos(unzFile file, unz_file_pos *file_pos);
|
||||
|
||||
typedef struct unz64_file_pos_s
|
||||
{
|
||||
int64_t pos_in_zip_directory; // offset in zip file directory
|
||||
uint64_t num_of_file; // # of file
|
||||
} unz64_file_pos;
|
||||
|
||||
int ZEXPORT unzGetFilePos64(unzFile file, unz64_file_pos *file_pos);
|
||||
int ZEXPORT unzGoToFilePos64(unzFile file, const unz64_file_pos *file_pos);
|
||||
|
||||
int64_t ZEXPORT unzGetOffset64(unzFile file);
|
||||
int32_t ZEXPORT unzGetOffset(unzFile file);
|
||||
int ZEXPORT unzSetOffset64(unzFile file, int64_t pos);
|
||||
int ZEXPORT unzSetOffset(unzFile file, uint32_t pos);
|
||||
int32_t ZEXPORT unzTell(unzFile file);
|
||||
int64_t ZEXPORT unzTell64(unzFile file);
|
||||
int ZEXPORT unzSeek(unzFile file, int32_t offset, int origin);
|
||||
int ZEXPORT unzSeek64(unzFile file, int64_t offset, int origin);
|
||||
int ZEXPORT unzEndOfFile(unzFile file);
|
||||
void* ZEXPORT unzGetStream(unzFile file);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
void fill_fopen_filefunc(zlib_filefunc_def *pzlib_filefunc_def);
|
||||
void fill_fopen64_filefunc(zlib_filefunc64_def *pzlib_filefunc_def);
|
||||
void fill_win32_filefunc(zlib_filefunc_def *pzlib_filefunc_def);
|
||||
void fill_win32_filefunc64(zlib_filefunc64_def *pzlib_filefunc_def);
|
||||
void fill_win32_filefunc64A(zlib_filefunc64_def *pzlib_filefunc_def);
|
||||
void fill_win32_filefunc64W(zlib_filefunc64_def *pzlib_filefunc_def);
|
||||
void fill_memory_filefunc(zlib_filefunc_def *pzlib_filefunc_def);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#define unztell unzTell64
|
||||
#define check_file_exists mz_os_file_exists
|
||||
#define dosdate_to_tm mz_zip_dosdate_to_tm
|
||||
#define change_file_date mz_os_set_file_date
|
||||
#define get_file_date mz_os_get_file_date
|
||||
#define is_large_file(x) (mz_os_get_file_size(x) >= UINT32_MAX)
|
||||
#define makedir mz_dir_make
|
||||
#define get_file_crc(p,b,bs,rc) mz_file_get_crc(p,rc)
|
||||
|
||||
#define MKDIR mz_os_make_dir
|
||||
#define CHDIR chdir
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
+180
@@ -0,0 +1,180 @@
|
||||
/* mz_crypt.c -- Crypto/hash functions
|
||||
Version 2.7.3, November 4, 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 <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#if defined(HAVE_ZLIB)
|
||||
#include "zlib.h"
|
||||
#elif defined(HAVE_LZMA)
|
||||
#include "lzma.h"
|
||||
#endif
|
||||
|
||||
#include "mz.h"
|
||||
#include "mz_crypt.h"
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
uint32_t mz_crypt_crc32_update(uint32_t value, const uint8_t *buf, int32_t size)
|
||||
{
|
||||
#if defined(HAVE_ZLIB)
|
||||
// Define z_crc_t in zlib 1.2.5 and less or if using zlib-ng
|
||||
#if defined(ZLIBNG_VERNUM)
|
||||
typedef uint32_t z_crc_t;
|
||||
#elif (ZLIB_VERNUM < 0x1270)
|
||||
typedef unsigned long z_crc_t;
|
||||
#endif
|
||||
|
||||
return (uint32_t)crc32((z_crc_t)value, buf, (uInt)size);
|
||||
#elif defined(HAVE_LZMA)
|
||||
return (uint32_t)lzma_crc32(buf, (size_t)size, (uint32_t)value);
|
||||
#else
|
||||
static uint32_t crc32_table[256] = {
|
||||
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
|
||||
0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
|
||||
0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
|
||||
0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
|
||||
0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
|
||||
0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
|
||||
0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
|
||||
0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
|
||||
0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
|
||||
0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
|
||||
0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
|
||||
0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
|
||||
0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
|
||||
0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
|
||||
0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
|
||||
0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
|
||||
0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
|
||||
0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
|
||||
0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
|
||||
0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
|
||||
0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
|
||||
0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
|
||||
0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
|
||||
0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
|
||||
0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
|
||||
0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
|
||||
0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
|
||||
0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
|
||||
0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
|
||||
0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
|
||||
0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
|
||||
0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
|
||||
0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
|
||||
0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
|
||||
0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
|
||||
0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
|
||||
0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
|
||||
0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
|
||||
0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
|
||||
0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
|
||||
0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
|
||||
0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
|
||||
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
|
||||
};
|
||||
value = ~value;
|
||||
|
||||
while (size > 0)
|
||||
{
|
||||
value = (value >> 8) ^ crc32_table[(value ^ *buf) & 0xFF];
|
||||
|
||||
buf += 1;
|
||||
size -= 1;
|
||||
}
|
||||
|
||||
return ~value;
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifndef MZ_ZIP_NO_ENCRYPTION
|
||||
int32_t mz_crypt_pbkdf2(uint8_t *password, int32_t password_length, uint8_t *salt,
|
||||
int32_t salt_length, int32_t iteration_count, uint8_t *key, int32_t key_length)
|
||||
{
|
||||
void *hmac1 = NULL;
|
||||
void *hmac2 = NULL;
|
||||
void *hmac3 = NULL;
|
||||
int32_t err = MZ_OK;
|
||||
uint16_t i = 0;
|
||||
uint16_t j = 0;
|
||||
uint16_t k = 0;
|
||||
uint16_t block_count = 0;
|
||||
uint8_t uu[MZ_HASH_SHA1_SIZE];
|
||||
uint8_t ux[MZ_HASH_SHA1_SIZE];
|
||||
|
||||
if (password == NULL || salt == NULL || key == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
memset(key, 0, key_length);
|
||||
|
||||
mz_crypt_hmac_create(&hmac1);
|
||||
mz_crypt_hmac_create(&hmac2);
|
||||
mz_crypt_hmac_create(&hmac3);
|
||||
|
||||
mz_crypt_hmac_set_algorithm(hmac1, MZ_HASH_SHA1);
|
||||
mz_crypt_hmac_set_algorithm(hmac2, MZ_HASH_SHA1);
|
||||
mz_crypt_hmac_set_algorithm(hmac3, MZ_HASH_SHA1);
|
||||
|
||||
err = mz_crypt_hmac_init(hmac1, password, password_length);
|
||||
err = mz_crypt_hmac_init(hmac2, password, password_length);
|
||||
if (err == MZ_OK)
|
||||
err = mz_crypt_hmac_update(hmac2, salt, salt_length);
|
||||
|
||||
block_count = 1 + ((uint16_t)key_length - 1) / MZ_HASH_SHA1_SIZE;
|
||||
|
||||
for (i = 0; (err == MZ_OK) && (i < block_count); i += 1)
|
||||
{
|
||||
memset(ux, 0, sizeof(ux));
|
||||
|
||||
err = mz_crypt_hmac_copy(hmac2, hmac3);
|
||||
if (err != MZ_OK)
|
||||
break;
|
||||
|
||||
uu[0] = (uint8_t)((i + 1) >> 24);
|
||||
uu[1] = (uint8_t)((i + 1) >> 16);
|
||||
uu[2] = (uint8_t)((i + 1) >> 8);
|
||||
uu[3] = (uint8_t)(i + 1);
|
||||
|
||||
for (j = 0, k = 4; j < iteration_count; j += 1)
|
||||
{
|
||||
err = mz_crypt_hmac_update(hmac3, uu, k);
|
||||
err = mz_crypt_hmac_end(hmac3, uu, sizeof(uu));
|
||||
|
||||
for(k = 0; k < MZ_HASH_SHA1_SIZE; k += 1)
|
||||
ux[k] ^= uu[k];
|
||||
|
||||
err = mz_crypt_hmac_copy(hmac1, hmac3);
|
||||
if (err != MZ_OK)
|
||||
break;
|
||||
}
|
||||
|
||||
if (err != MZ_OK)
|
||||
break;
|
||||
|
||||
j = 0;
|
||||
k = i * MZ_HASH_SHA1_SIZE;
|
||||
|
||||
while (j < MZ_HASH_SHA1_SIZE && k < key_length)
|
||||
key[k++] = ux[j++];
|
||||
}
|
||||
|
||||
mz_crypt_hmac_delete(&hmac1);
|
||||
mz_crypt_hmac_delete(&hmac2);
|
||||
mz_crypt_hmac_delete(&hmac3);
|
||||
|
||||
return err;
|
||||
}
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
/* mz_crypt.h -- Crypto/hash functions
|
||||
Version 2.7.3, November 4, 2018
|
||||
part of the MiniZip project
|
||||
|
||||
Copyright (C) 2010-2018 Nathan Moinvaziri
|
||||
https://github.com/nmoinvaz/minizip
|
||||
|
||||
This program is distributed under the terms of the same license as zlib.
|
||||
See the accompanying LICENSE file for the full text of the license.
|
||||
*/
|
||||
|
||||
#ifndef MZ_CRYPT_H
|
||||
#define MZ_CRYPT_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
uint32_t mz_crypt_crc32_update(uint32_t value, const uint8_t *buf, int32_t size);
|
||||
|
||||
int32_t mz_crypt_pbkdf2(uint8_t *password, int32_t password_length, uint8_t *salt,
|
||||
int32_t salt_length, int32_t iteration_count, uint8_t *key, int32_t key_length);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t mz_crypt_rand(uint8_t *buf, int32_t size);
|
||||
|
||||
void mz_crypt_sha_reset(void *handle);
|
||||
int32_t mz_crypt_sha_begin(void *handle);
|
||||
int32_t mz_crypt_sha_update(void *handle, const void *buf, int32_t size);
|
||||
int32_t mz_crypt_sha_end(void *handle, uint8_t *digest, int32_t digest_size);
|
||||
void mz_crypt_sha_set_algorithm(void *handle, uint16_t algorithm);
|
||||
void* mz_crypt_sha_create(void **handle);
|
||||
void mz_crypt_sha_delete(void **handle);
|
||||
|
||||
void mz_crypt_aes_reset(void *handle);
|
||||
int32_t mz_crypt_aes_encrypt(void *handle, uint8_t *buf, int32_t size);
|
||||
int32_t mz_crypt_aes_decrypt(void *handle, uint8_t *buf, int32_t size);
|
||||
int32_t mz_crypt_aes_set_encrypt_key(void *handle, const void *key, int32_t key_length);
|
||||
int32_t mz_crypt_aes_set_decrypt_key(void *handle, const void *key, int32_t key_length);
|
||||
void mz_crypt_aes_set_mode(void *handle, int32_t mode);
|
||||
void* mz_crypt_aes_create(void **handle);
|
||||
void mz_crypt_aes_delete(void **handle);
|
||||
|
||||
void mz_crypt_hmac_reset(void *handle);
|
||||
int32_t mz_crypt_hmac_init(void *handle, const void *key, int32_t key_length);
|
||||
int32_t mz_crypt_hmac_update(void *handle, const void *buf, int32_t size);
|
||||
int32_t mz_crypt_hmac_end(void *handle, uint8_t *digest, int32_t digest_size);
|
||||
int32_t mz_crypt_hmac_copy(void *src_handle, void *target_handle);
|
||||
void mz_crypt_hmac_set_algorithm(void *handle, uint16_t algorithm);
|
||||
void* mz_crypt_hmac_create(void **handle);
|
||||
void mz_crypt_hmac_delete(void **handle);
|
||||
|
||||
int32_t mz_crypt_sign(uint8_t *message, int32_t message_size, uint8_t *cert_data, int32_t cert_data_size,
|
||||
const char *cert_pwd, uint8_t **signature, int32_t *signature_size);
|
||||
int32_t mz_crypt_sign_verify(uint8_t *message, int32_t message_size, uint8_t *signature, int32_t signature_size);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,535 @@
|
||||
/* mz_crypt_apple.c -- Crypto/hash functions for Apple
|
||||
Version 2.7.3, November 4, 2018
|
||||
part of the MiniZip project
|
||||
|
||||
Copyright (C) 2010-2018 Nathan Moinvaziri
|
||||
https://github.com/nmoinvaz/minizip
|
||||
|
||||
This program is distributed under the terms of the same license as zlib.
|
||||
See the accompanying LICENSE file for the full text of the license.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <CoreFoundation/CoreFoundation.h>
|
||||
#include <CommonCrypto/CommonCryptor.h>
|
||||
#include <CommonCrypto/CommonDigest.h>
|
||||
#include <CommonCrypto/CommonHMAC.h>
|
||||
#include <CommonCrypto/CommonRandom.h>
|
||||
#include <Security/Security.h>
|
||||
#include <Security/SecPolicy.h>
|
||||
|
||||
#include "mz.h"
|
||||
|
||||
#include "mz_os.h"
|
||||
#include "mz_strm_os.h"
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t mz_crypt_rand(uint8_t *buf, int32_t size)
|
||||
{
|
||||
if (CCRandomGenerateBytes(buf, size) != kCCSuccess)
|
||||
return 0;
|
||||
return size;
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct mz_crypt_sha_s {
|
||||
CC_SHA1_CTX ctx1;
|
||||
CC_SHA256_CTX ctx256;
|
||||
int32_t error;
|
||||
int32_t initialized;
|
||||
uint16_t algorithm;
|
||||
} mz_crypt_sha;
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
void mz_crypt_sha_reset(void *handle)
|
||||
{
|
||||
mz_crypt_sha *sha = (mz_crypt_sha *)handle;
|
||||
|
||||
sha->error = 0;
|
||||
sha->initialized = 0;
|
||||
}
|
||||
|
||||
int32_t mz_crypt_sha_begin(void *handle)
|
||||
{
|
||||
mz_crypt_sha *sha = (mz_crypt_sha *)handle;
|
||||
|
||||
if (sha == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
mz_crypt_sha_reset(handle);
|
||||
|
||||
if (sha->algorithm == MZ_HASH_SHA1)
|
||||
sha->error = CC_SHA1_Init(&sha->ctx1);
|
||||
else if (sha->algorithm == MZ_HASH_SHA256)
|
||||
sha->error = CC_SHA256_Init(&sha->ctx256);
|
||||
else
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
if (!sha->error)
|
||||
return MZ_HASH_ERROR;
|
||||
|
||||
sha->initialized = 1;
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_crypt_sha_update(void *handle, const void *buf, int32_t size)
|
||||
{
|
||||
mz_crypt_sha *sha = (mz_crypt_sha *)handle;
|
||||
|
||||
if (sha == NULL || buf == NULL || !sha->initialized)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
if (sha->algorithm == MZ_HASH_SHA1)
|
||||
sha->error = CC_SHA1_Update(&sha->ctx1, buf, size);
|
||||
else
|
||||
sha->error = CC_SHA256_Update(&sha->ctx256, buf, size);
|
||||
|
||||
if (!sha->error)
|
||||
return MZ_HASH_ERROR;
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
int32_t mz_crypt_sha_end(void *handle, uint8_t *digest, int32_t digest_size)
|
||||
{
|
||||
mz_crypt_sha *sha = (mz_crypt_sha *)handle;
|
||||
|
||||
if (sha == NULL || digest == NULL || !sha->initialized)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
if (sha->algorithm == MZ_HASH_SHA1)
|
||||
{
|
||||
if (digest_size < MZ_HASH_SHA1_SIZE)
|
||||
return MZ_BUF_ERROR;
|
||||
sha->error = CC_SHA1_Final(digest, &sha->ctx1);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (digest_size < MZ_HASH_SHA256_SIZE)
|
||||
return MZ_BUF_ERROR;
|
||||
sha->error = CC_SHA256_Final(digest, &sha->ctx256);
|
||||
}
|
||||
|
||||
if (!sha->error)
|
||||
return MZ_HASH_ERROR;
|
||||
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
void mz_crypt_sha_set_algorithm(void *handle, uint16_t algorithm)
|
||||
{
|
||||
mz_crypt_sha *sha = (mz_crypt_sha *)handle;
|
||||
sha->algorithm = algorithm;
|
||||
}
|
||||
|
||||
void *mz_crypt_sha_create(void **handle)
|
||||
{
|
||||
mz_crypt_sha *sha = NULL;
|
||||
|
||||
sha = (mz_crypt_sha *)MZ_ALLOC(sizeof(mz_crypt_sha));
|
||||
if (sha != NULL)
|
||||
{
|
||||
memset(sha, 0, sizeof(mz_crypt_sha));
|
||||
sha->algorithm = MZ_HASH_SHA256;
|
||||
}
|
||||
if (handle != NULL)
|
||||
*handle = sha;
|
||||
|
||||
return sha;
|
||||
}
|
||||
|
||||
void mz_crypt_sha_delete(void **handle)
|
||||
{
|
||||
mz_crypt_sha *sha = NULL;
|
||||
if (handle == NULL)
|
||||
return;
|
||||
sha = (mz_crypt_sha *)*handle;
|
||||
if (sha != NULL)
|
||||
{
|
||||
mz_crypt_sha_reset(*handle);
|
||||
MZ_FREE(sha);
|
||||
}
|
||||
*handle = NULL;
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct mz_crypt_aes_s {
|
||||
CCCryptorRef crypt;
|
||||
int32_t mode;
|
||||
int32_t error;
|
||||
} mz_crypt_aes;
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
void mz_crypt_aes_reset(void *handle)
|
||||
{
|
||||
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
|
||||
|
||||
if (aes->crypt != NULL)
|
||||
CCCryptorRelease(aes->crypt);
|
||||
aes->crypt = NULL;
|
||||
}
|
||||
|
||||
int32_t mz_crypt_aes_encrypt(void *handle, uint8_t *buf, int32_t size)
|
||||
{
|
||||
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
|
||||
size_t data_moved = 0;
|
||||
|
||||
if (aes == NULL || buf == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
if (size != MZ_AES_BLOCK_SIZE)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
aes->error = CCCryptorUpdate(aes->crypt, buf, size, buf, size, &data_moved);
|
||||
|
||||
if (aes->error != kCCSuccess)
|
||||
return MZ_HASH_ERROR;
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
int32_t mz_crypt_aes_decrypt(void *handle, uint8_t *buf, int32_t size)
|
||||
{
|
||||
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
|
||||
size_t data_moved = 0;
|
||||
|
||||
if (aes == NULL || buf == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
if (size != MZ_AES_BLOCK_SIZE)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
aes->error = CCCryptorUpdate(aes->crypt, buf, size, buf, size, &data_moved);
|
||||
|
||||
if (aes->error != kCCSuccess)
|
||||
return MZ_HASH_ERROR;
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
int32_t mz_crypt_aes_set_encrypt_key(void *handle, const void *key, int32_t key_length)
|
||||
{
|
||||
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
|
||||
|
||||
|
||||
if (aes == NULL || key == NULL || key_length == 0)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
mz_crypt_aes_reset(handle);
|
||||
|
||||
aes->error = CCCryptorCreate(kCCEncrypt, kCCAlgorithmAES, 0, key, key_length, NULL, &aes->crypt);
|
||||
|
||||
if (aes->error != kCCSuccess)
|
||||
return MZ_HASH_ERROR;
|
||||
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_crypt_aes_set_decrypt_key(void *handle, const void *key, int32_t key_length)
|
||||
{
|
||||
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
|
||||
|
||||
|
||||
if (aes == NULL || key == NULL || key_length == 0)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
mz_crypt_aes_reset(handle);
|
||||
|
||||
aes->error = CCCryptorCreate(kCCDecrypt, kCCAlgorithmAES, 0, key, key_length, NULL, &aes->crypt);
|
||||
|
||||
if (aes->error != kCCSuccess)
|
||||
return MZ_HASH_ERROR;
|
||||
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
void mz_crypt_aes_set_mode(void *handle, int32_t mode)
|
||||
{
|
||||
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
|
||||
aes->mode = mode;
|
||||
}
|
||||
|
||||
void *mz_crypt_aes_create(void **handle)
|
||||
{
|
||||
mz_crypt_aes *aes = NULL;
|
||||
|
||||
aes = (mz_crypt_aes *)MZ_ALLOC(sizeof(mz_crypt_aes));
|
||||
if (aes != NULL)
|
||||
memset(aes, 0, sizeof(mz_crypt_aes));
|
||||
if (handle != NULL)
|
||||
*handle = aes;
|
||||
|
||||
return aes;
|
||||
}
|
||||
|
||||
void mz_crypt_aes_delete(void **handle)
|
||||
{
|
||||
mz_crypt_aes *aes = NULL;
|
||||
if (handle == NULL)
|
||||
return;
|
||||
aes = (mz_crypt_aes *)*handle;
|
||||
if (aes != NULL)
|
||||
{
|
||||
mz_crypt_aes_reset(*handle);
|
||||
MZ_FREE(aes);
|
||||
}
|
||||
*handle = NULL;
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct mz_crypt_hmac_s {
|
||||
CCHmacContext ctx;
|
||||
int32_t initialized;
|
||||
int32_t error;
|
||||
uint16_t algorithm;
|
||||
} mz_crypt_hmac;
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
static void mz_crypt_hmac_free(void *handle)
|
||||
{
|
||||
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
|
||||
memset(&hmac->ctx, 0, sizeof(hmac->ctx));
|
||||
}
|
||||
|
||||
void mz_crypt_hmac_reset(void *handle)
|
||||
{
|
||||
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
|
||||
mz_crypt_hmac_free(handle);
|
||||
hmac->error = 0;
|
||||
}
|
||||
|
||||
int32_t mz_crypt_hmac_init(void *handle, const void *key, int32_t key_length)
|
||||
{
|
||||
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
|
||||
CCHmacAlgorithm algorithm = 0;
|
||||
|
||||
if (hmac == NULL || key == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
mz_crypt_hmac_reset(handle);
|
||||
|
||||
if (hmac->algorithm == MZ_HASH_SHA1)
|
||||
algorithm = kCCHmacAlgSHA1;
|
||||
else if (hmac->algorithm == MZ_HASH_SHA256)
|
||||
algorithm = kCCHmacAlgSHA256;
|
||||
else
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
CCHmacInit(&hmac->ctx, algorithm, key, key_length);
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_crypt_hmac_update(void *handle, const void *buf, int32_t size)
|
||||
{
|
||||
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
|
||||
|
||||
if (hmac == NULL || buf == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
CCHmacUpdate(&hmac->ctx, buf, size);
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_crypt_hmac_end(void *handle, uint8_t *digest, int32_t digest_size)
|
||||
{
|
||||
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
|
||||
|
||||
if (hmac == NULL || digest == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
if (hmac->algorithm == MZ_HASH_SHA1)
|
||||
{
|
||||
if (digest_size < MZ_HASH_SHA1_SIZE)
|
||||
return MZ_BUF_ERROR;
|
||||
CCHmacFinal(&hmac->ctx, digest);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (digest_size < MZ_HASH_SHA256_SIZE)
|
||||
return MZ_BUF_ERROR;
|
||||
CCHmacFinal(&hmac->ctx, digest);
|
||||
}
|
||||
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
void mz_crypt_hmac_set_algorithm(void *handle, uint16_t algorithm)
|
||||
{
|
||||
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
|
||||
hmac->algorithm = algorithm;
|
||||
}
|
||||
|
||||
int32_t mz_crypt_hmac_copy(void *src_handle, void *target_handle)
|
||||
{
|
||||
mz_crypt_hmac *source = (mz_crypt_hmac *)src_handle;
|
||||
mz_crypt_hmac *target = (mz_crypt_hmac *)target_handle;
|
||||
|
||||
if (source == NULL || target == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
memcpy(&target->ctx, &source->ctx, sizeof(CCHmacContext));
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
void *mz_crypt_hmac_create(void **handle)
|
||||
{
|
||||
mz_crypt_hmac *hmac = NULL;
|
||||
|
||||
hmac = (mz_crypt_hmac *)MZ_ALLOC(sizeof(mz_crypt_hmac));
|
||||
if (hmac != NULL)
|
||||
{
|
||||
memset(hmac, 0, sizeof(mz_crypt_hmac));
|
||||
hmac->algorithm = MZ_HASH_SHA256;
|
||||
}
|
||||
if (handle != NULL)
|
||||
*handle = hmac;
|
||||
|
||||
return hmac;
|
||||
}
|
||||
|
||||
void mz_crypt_hmac_delete(void **handle)
|
||||
{
|
||||
mz_crypt_hmac *hmac = NULL;
|
||||
if (handle == NULL)
|
||||
return;
|
||||
hmac = (mz_crypt_hmac *)*handle;
|
||||
if (hmac != NULL)
|
||||
{
|
||||
mz_crypt_hmac_free(*handle);
|
||||
MZ_FREE(hmac);
|
||||
}
|
||||
*handle = NULL;
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t mz_crypt_sign(uint8_t *message, int32_t message_size, uint8_t *cert_data, int32_t cert_data_size,
|
||||
const char *cert_pwd, uint8_t **signature, int32_t *signature_size)
|
||||
{
|
||||
CFStringRef password_ref = NULL;
|
||||
CFDictionaryRef options_dict = NULL;
|
||||
CFDictionaryRef identity_trust = NULL;
|
||||
CFDataRef signature_out = NULL;
|
||||
CFDataRef pkcs12_data = NULL;
|
||||
CFArrayRef items = 0;
|
||||
SecIdentityRef identity = NULL;
|
||||
SecTrustRef trust = NULL;
|
||||
OSStatus status = noErr;
|
||||
const void *options_key[2] = { kSecImportExportPassphrase, kSecReturnRef };
|
||||
const void *options_values[2] = { 0, kCFBooleanTrue };
|
||||
int32_t err = MZ_SIGN_ERROR;
|
||||
|
||||
|
||||
if (message == NULL || cert_data == NULL || signature == NULL || signature_size == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
*signature = NULL;
|
||||
*signature_size = 0;
|
||||
|
||||
password_ref = CFStringCreateWithCString(0, cert_pwd, kCFStringEncodingUTF8);
|
||||
options_values[0] = password_ref;
|
||||
|
||||
options_dict = CFDictionaryCreate(0, options_key, options_values, 2, 0, 0);
|
||||
if (options_dict)
|
||||
pkcs12_data = CFDataCreate(0, cert_data, cert_data_size);
|
||||
if (pkcs12_data)
|
||||
status = SecPKCS12Import(pkcs12_data, options_dict, &items);
|
||||
if (status == noErr)
|
||||
identity_trust = CFArrayGetValueAtIndex(items, 0);
|
||||
if (identity_trust)
|
||||
identity = (SecIdentityRef)CFDictionaryGetValue(identity_trust, kSecImportItemIdentity);
|
||||
if (identity)
|
||||
trust = (SecTrustRef)CFDictionaryGetValue(identity_trust, kSecImportItemTrust);
|
||||
if (trust)
|
||||
{
|
||||
status = CMSEncodeContent(identity, NULL, NULL, FALSE, 0, message, message_size, &signature_out);
|
||||
|
||||
if (status == errSecSuccess)
|
||||
{
|
||||
*signature_size = CFDataGetLength(signature_out);
|
||||
*signature = (uint8_t *)MZ_ALLOC(*signature_size);
|
||||
|
||||
memcpy(*signature, CFDataGetBytePtr(signature_out), *signature_size);
|
||||
|
||||
err = MZ_OK;
|
||||
}
|
||||
}
|
||||
|
||||
if (signature_out)
|
||||
CFRelease(signature_out);
|
||||
if (items)
|
||||
CFRelease(items);
|
||||
if (pkcs12_data)
|
||||
CFRelease(pkcs12_data);
|
||||
if (options_dict)
|
||||
CFRelease(options_dict);
|
||||
if (password_ref)
|
||||
CFRelease(password_ref);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
int32_t mz_crypt_sign_verify(uint8_t *message, int32_t message_size, uint8_t *signature, int32_t signature_size)
|
||||
{
|
||||
CMSDecoderRef decoder = NULL;
|
||||
CMSSignerStatus signer_status = 0;
|
||||
CFDataRef message_out = NULL;
|
||||
SecTrustRef trust = NULL;
|
||||
SecPolicyRef trust_policy = NULL;
|
||||
OSStatus status = noErr;
|
||||
OSStatus verify_status = noErr;
|
||||
size_t signer_count = 0;
|
||||
int32_t i = 0;
|
||||
int32_t err = MZ_SIGN_ERROR;
|
||||
|
||||
if (message == NULL || signature == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
status = CMSDecoderCreate(&decoder);
|
||||
if (status == errSecSuccess)
|
||||
status = CMSDecoderUpdateMessage(decoder, signature, signature_size);
|
||||
if (status == errSecSuccess)
|
||||
status = CMSDecoderFinalizeMessage(decoder);
|
||||
if (status == errSecSuccess)
|
||||
trust_policy = SecPolicyCreateBasicX509();
|
||||
|
||||
if (status == errSecSuccess && trust_policy)
|
||||
{
|
||||
CMSDecoderGetNumSigners(decoder, &signer_count);
|
||||
if (signer_count > 0)
|
||||
err = MZ_OK;
|
||||
for (i = 0; i < signer_count; i += 1)
|
||||
{
|
||||
status = CMSDecoderCopySignerStatus(decoder, i, trust_policy, TRUE, &signer_status, NULL, &verify_status);
|
||||
if (status != errSecSuccess || verify_status != 0 || signer_status != kCMSSignerValid)
|
||||
{
|
||||
err = MZ_SIGN_ERROR;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (err == MZ_OK)
|
||||
{
|
||||
status = CMSDecoderCopyContent(decoder, &message_out);
|
||||
if ((status != errSecSuccess) ||
|
||||
(CFDataGetLength(message_out) != message_size) ||
|
||||
(memcmp(message, CFDataGetBytePtr(message_out), message_size) != 0))
|
||||
err = MZ_SIGN_ERROR;
|
||||
}
|
||||
|
||||
if (trust_policy)
|
||||
CFRelease(trust_policy);
|
||||
if (decoder)
|
||||
CFRelease(decoder);
|
||||
|
||||
return err;
|
||||
}
|
||||
+445
@@ -0,0 +1,445 @@
|
||||
/* mz_crypt_brg.c -- Crypto/hash functions using Brian Gladman's library
|
||||
Version 2.7.3, November 4, 2018
|
||||
part of the MiniZip project
|
||||
|
||||
Copyright (C) 2010-2018 Nathan Moinvaziri
|
||||
https://github.com/nmoinvaz/minizip
|
||||
|
||||
This program is distributed under the terms of the same license as zlib.
|
||||
See the accompanying LICENSE file for the full text of the license.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#if defined(HAVE_GETRANDOM)
|
||||
# include <sys/random.h>
|
||||
#endif
|
||||
#if defined(HAVE_LIBBSD)
|
||||
# include <bsd/stdlib.h> // arc4random_buf
|
||||
#endif
|
||||
|
||||
#include "aes.h"
|
||||
#include "sha1.h"
|
||||
#include "sha2.h"
|
||||
#include "hmac.h"
|
||||
|
||||
#include "mz.h"
|
||||
|
||||
#include "mz_os.h"
|
||||
#include "mz_strm_os.h"
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#if defined(HAVE_ARC4RANDOM_BUF)
|
||||
int32_t mz_crypt_rand(uint8_t *buf, int32_t size)
|
||||
{
|
||||
if (size < 0)
|
||||
return 0;
|
||||
arc4random_buf(buf, (uint32_t)size);
|
||||
return size;
|
||||
}
|
||||
#elif defined(HAVE_ARC4RANDOM)
|
||||
int32_t mz_crypt_rand(uint8_t *buf, int32_t size)
|
||||
{
|
||||
int32_t left = size;
|
||||
for (; left > 2; left -= 3, buf += 3)
|
||||
{
|
||||
uint32_t val = arc4random();
|
||||
|
||||
buf[0] = (val) & 0xFF;
|
||||
buf[1] = (val >> 8) & 0xFF;
|
||||
buf[2] = (val >> 16) & 0xFF;
|
||||
}
|
||||
for (; left > 0; left--, buf++)
|
||||
{
|
||||
*buf = arc4random() & 0xFF;
|
||||
}
|
||||
return size - left;
|
||||
}
|
||||
#elif defined(HAVE_GETRANDOM)
|
||||
int32_t mz_crypt_rand(uint8_t *buf, int32_t size)
|
||||
{
|
||||
int32_t left = size;
|
||||
int32_t written = 0;
|
||||
|
||||
while (left > 0)
|
||||
{
|
||||
written = getrandom(buf, left, 0);
|
||||
if (written < 0)
|
||||
return MZ_INTERNAL_ERROR;
|
||||
|
||||
buf += written;
|
||||
left -= written;
|
||||
}
|
||||
return size - left;
|
||||
}
|
||||
#else
|
||||
#if !defined(FORCE_LOWQUALITY_ENTROPY)
|
||||
#pragma message("Warning: Low quality entropy function used for encryption")
|
||||
#endif
|
||||
int32_t mz_crypt_rand(uint8_t *buf, int32_t size)
|
||||
{
|
||||
return mz_os_rand(buf, size);
|
||||
}
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct mz_crypt_sha_s {
|
||||
sha256_ctx ctx256;
|
||||
sha1_ctx ctx1;
|
||||
int32_t initialized;
|
||||
uint16_t algorithm;
|
||||
} mz_crypt_sha;
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
void mz_crypt_sha_reset(void *handle)
|
||||
{
|
||||
mz_crypt_sha *sha = (mz_crypt_sha *)handle;
|
||||
sha->initialized = 0;
|
||||
}
|
||||
|
||||
int32_t mz_crypt_sha_begin(void *handle)
|
||||
{
|
||||
mz_crypt_sha *sha = (mz_crypt_sha *)handle;
|
||||
|
||||
if (sha == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
if (sha->algorithm == MZ_HASH_SHA1)
|
||||
sha1_begin(&sha->ctx1);
|
||||
else
|
||||
sha256_begin(&sha->ctx256);
|
||||
|
||||
sha->initialized = 1;
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_crypt_sha_update(void *handle, const void *buf, int32_t size)
|
||||
{
|
||||
mz_crypt_sha *sha = (mz_crypt_sha *)handle;
|
||||
|
||||
if (sha == NULL || buf == NULL || !sha->initialized)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
if (sha->algorithm == MZ_HASH_SHA1)
|
||||
sha1_hash(buf, size, &sha->ctx1);
|
||||
else
|
||||
sha256_hash(buf, size, &sha->ctx256);
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
int32_t mz_crypt_sha_end(void *handle, uint8_t *digest, int32_t digest_size)
|
||||
{
|
||||
mz_crypt_sha *sha = (mz_crypt_sha *)handle;
|
||||
|
||||
if (sha == NULL || digest == NULL || !sha->initialized)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
if (sha->algorithm == MZ_HASH_SHA1)
|
||||
{
|
||||
if (digest_size < MZ_HASH_SHA1_SIZE)
|
||||
return MZ_BUF_ERROR;
|
||||
sha1_end(digest, &sha->ctx1);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (digest_size < MZ_HASH_SHA256_SIZE)
|
||||
return MZ_BUF_ERROR;
|
||||
sha256_end(digest, &sha->ctx256);
|
||||
}
|
||||
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
void mz_crypt_sha_set_algorithm(void *handle, uint16_t algorithm)
|
||||
{
|
||||
mz_crypt_sha *sha = (mz_crypt_sha *)handle;
|
||||
sha->algorithm = algorithm;
|
||||
}
|
||||
|
||||
void *mz_crypt_sha_create(void **handle)
|
||||
{
|
||||
mz_crypt_sha *sha = NULL;
|
||||
|
||||
sha = (mz_crypt_sha *)MZ_ALLOC(sizeof(mz_crypt_sha));
|
||||
if (sha != NULL)
|
||||
{
|
||||
memset(sha, 0, sizeof(mz_crypt_sha));
|
||||
sha->algorithm = MZ_HASH_SHA256;
|
||||
}
|
||||
if (handle != NULL)
|
||||
*handle = sha;
|
||||
|
||||
return sha;
|
||||
}
|
||||
|
||||
void mz_crypt_sha_delete(void **handle)
|
||||
{
|
||||
mz_crypt_sha *sha = NULL;
|
||||
if (handle == NULL)
|
||||
return;
|
||||
sha = (mz_crypt_sha *)*handle;
|
||||
if (sha != NULL)
|
||||
{
|
||||
mz_crypt_sha_reset(*handle);
|
||||
MZ_FREE(sha);
|
||||
}
|
||||
*handle = NULL;
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct mz_crypt_aes_s {
|
||||
aes_encrypt_ctx encrypt_ctx;
|
||||
aes_decrypt_ctx decrypt_ctx;
|
||||
int32_t mode;
|
||||
int32_t error;
|
||||
} mz_crypt_aes;
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
void mz_crypt_aes_reset(void *handle)
|
||||
{
|
||||
MZ_UNUSED(handle);
|
||||
}
|
||||
|
||||
int32_t mz_crypt_aes_encrypt(void *handle, uint8_t *buf, int32_t size)
|
||||
{
|
||||
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
|
||||
|
||||
if (aes == NULL || buf == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
if (size != MZ_AES_BLOCK_SIZE)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
aes->error = aes_encrypt(buf, buf, &aes->encrypt_ctx);
|
||||
if (aes->error)
|
||||
return MZ_CRYPT_ERROR;
|
||||
return size;
|
||||
}
|
||||
|
||||
int32_t mz_crypt_aes_decrypt(void *handle, uint8_t *buf, int32_t size)
|
||||
{
|
||||
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
|
||||
if (aes == NULL || buf == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
if (size != MZ_AES_BLOCK_SIZE)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
aes->error = aes_decrypt(buf, buf, &aes->decrypt_ctx);
|
||||
if (aes->error)
|
||||
return MZ_CRYPT_ERROR;
|
||||
return size;
|
||||
}
|
||||
|
||||
int32_t mz_crypt_aes_set_encrypt_key(void *handle, const void *key, int32_t key_length)
|
||||
{
|
||||
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
|
||||
|
||||
|
||||
if (aes == NULL || key == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
mz_crypt_aes_reset(handle);
|
||||
|
||||
aes->error = aes_encrypt_key(key, key_length, &aes->encrypt_ctx);
|
||||
if (aes->error)
|
||||
return MZ_HASH_ERROR;
|
||||
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_crypt_aes_set_decrypt_key(void *handle, const void *key, int32_t key_length)
|
||||
{
|
||||
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
|
||||
|
||||
|
||||
if (aes == NULL || key == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
mz_crypt_aes_reset(handle);
|
||||
|
||||
aes->error = aes_decrypt_key(key, key_length, &aes->decrypt_ctx);
|
||||
if (aes->error)
|
||||
return MZ_HASH_ERROR;
|
||||
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
void mz_crypt_aes_set_mode(void *handle, int32_t mode)
|
||||
{
|
||||
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
|
||||
aes->mode = mode;
|
||||
}
|
||||
|
||||
void *mz_crypt_aes_create(void **handle)
|
||||
{
|
||||
mz_crypt_aes *aes = NULL;
|
||||
|
||||
aes = (mz_crypt_aes *)MZ_ALLOC(sizeof(mz_crypt_aes));
|
||||
if (aes != NULL)
|
||||
memset(aes, 0, sizeof(mz_crypt_aes));
|
||||
if (handle != NULL)
|
||||
*handle = aes;
|
||||
|
||||
return aes;
|
||||
}
|
||||
|
||||
void mz_crypt_aes_delete(void **handle)
|
||||
{
|
||||
mz_crypt_aes *aes = NULL;
|
||||
if (handle == NULL)
|
||||
return;
|
||||
aes = (mz_crypt_aes *)*handle;
|
||||
if (aes != NULL)
|
||||
MZ_FREE(aes);
|
||||
*handle = NULL;
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct mz_crypt_hmac_s {
|
||||
hmac_ctx ctx;
|
||||
int32_t initialized;
|
||||
int32_t error;
|
||||
uint16_t algorithm;
|
||||
} mz_crypt_hmac;
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
void mz_crypt_hmac_reset(void *handle)
|
||||
{
|
||||
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
|
||||
hmac->error = 0;
|
||||
}
|
||||
|
||||
int32_t mz_crypt_hmac_init(void *handle, const void *key, int32_t key_length)
|
||||
{
|
||||
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
|
||||
|
||||
if (hmac == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
mz_crypt_hmac_reset(handle);
|
||||
|
||||
if (hmac->algorithm == MZ_HASH_SHA1)
|
||||
hmac_sha_begin(HMAC_SHA1, &hmac->ctx);
|
||||
else
|
||||
hmac_sha_begin(HMAC_SHA256, &hmac->ctx);
|
||||
|
||||
hmac_sha_key(key, key_length, &hmac->ctx);
|
||||
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_crypt_hmac_update(void *handle, const void *buf, int32_t size)
|
||||
{
|
||||
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
|
||||
|
||||
if (hmac == NULL || buf == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
hmac_sha_data(buf, size, &hmac->ctx);
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_crypt_hmac_end(void *handle, uint8_t *digest, int32_t digest_size)
|
||||
{
|
||||
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
|
||||
|
||||
if (hmac == NULL || digest == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
if (hmac->algorithm == MZ_HASH_SHA1)
|
||||
{
|
||||
if (digest_size < MZ_HASH_SHA1_SIZE)
|
||||
return MZ_BUF_ERROR;
|
||||
hmac_sha_end(digest, digest_size, &hmac->ctx);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (digest_size < MZ_HASH_SHA256_SIZE)
|
||||
return MZ_BUF_ERROR;
|
||||
hmac_sha_end(digest, digest_size, &hmac->ctx);
|
||||
}
|
||||
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
void mz_crypt_hmac_set_algorithm(void *handle, uint16_t algorithm)
|
||||
{
|
||||
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
|
||||
hmac->algorithm = algorithm;
|
||||
}
|
||||
|
||||
int32_t mz_crypt_hmac_copy(void *src_handle, void *target_handle)
|
||||
{
|
||||
mz_crypt_hmac *source = (mz_crypt_hmac *)src_handle;
|
||||
mz_crypt_hmac *target = (mz_crypt_hmac *)target_handle;
|
||||
|
||||
if (target == NULL || source == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
memcpy(&target->ctx, &source->ctx, sizeof(hmac_ctx));
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
void *mz_crypt_hmac_create(void **handle)
|
||||
{
|
||||
mz_crypt_hmac *hmac = NULL;
|
||||
|
||||
hmac = (mz_crypt_hmac *)MZ_ALLOC(sizeof(mz_crypt_hmac));
|
||||
if (hmac != NULL)
|
||||
{
|
||||
memset(hmac, 0, sizeof(mz_crypt_hmac));
|
||||
hmac->algorithm = MZ_HASH_SHA256;
|
||||
}
|
||||
if (handle != NULL)
|
||||
*handle = hmac;
|
||||
|
||||
return hmac;
|
||||
}
|
||||
|
||||
void mz_crypt_hmac_delete(void **handle)
|
||||
{
|
||||
mz_crypt_hmac *hmac = NULL;
|
||||
if (handle == NULL)
|
||||
return;
|
||||
hmac = (mz_crypt_hmac *)*handle;
|
||||
if (hmac != NULL)
|
||||
MZ_FREE(hmac);
|
||||
*handle = NULL;
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t mz_crypt_sign(uint8_t *message, int32_t message_size, uint8_t *cert_data, int32_t cert_data_size,
|
||||
const char *cert_pwd, uint8_t **signature, int32_t *signature_size)
|
||||
{
|
||||
MZ_UNUSED(message);
|
||||
MZ_UNUSED(message_size);
|
||||
MZ_UNUSED(cert_data);
|
||||
MZ_UNUSED(cert_data_size);
|
||||
MZ_UNUSED(cert_pwd);
|
||||
MZ_UNUSED(signature);
|
||||
MZ_UNUSED(signature_size);
|
||||
|
||||
return MZ_SUPPORT_ERROR;
|
||||
}
|
||||
|
||||
int32_t mz_crypt_sign_verify(uint8_t *message, int32_t message_size, uint8_t *signature, int32_t signature_size)
|
||||
{
|
||||
MZ_UNUSED(message);
|
||||
MZ_UNUSED(message_size);
|
||||
MZ_UNUSED(signature);
|
||||
MZ_UNUSED(signature_size);
|
||||
|
||||
return MZ_SUPPORT_ERROR;
|
||||
}
|
||||
@@ -0,0 +1,681 @@
|
||||
/* mz_crypt_openssl.c -- Crypto/hash functions for OpenSSL
|
||||
Version 2.7.3, November 4, 2018
|
||||
part of the MiniZip project
|
||||
|
||||
Copyright (C) 2010-2018 Nathan Moinvaziri
|
||||
https://github.com/nmoinvaz/minizip
|
||||
|
||||
This program is distributed under the terms of the same license as zlib.
|
||||
See the accompanying LICENSE file for the full text of the license.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/rand.h>
|
||||
#include <openssl/sha.h>
|
||||
#include <openssl/aes.h>
|
||||
#include <openssl/hmac.h>
|
||||
#include <openssl/pkcs12.h>
|
||||
#include <openssl/cms.h>
|
||||
#include <openssl/x509.h>
|
||||
|
||||
#include "mz.h"
|
||||
|
||||
#include "mz_os.h"
|
||||
#include "mz_strm_os.h"
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
static void mz_crypt_init(void)
|
||||
{
|
||||
static int32_t openssl_initialized = 0;
|
||||
if (openssl_initialized == 0)
|
||||
{
|
||||
OpenSSL_add_all_algorithms();
|
||||
|
||||
ERR_load_BIO_strings();
|
||||
ERR_load_crypto_strings();
|
||||
|
||||
openssl_initialized = 1;
|
||||
}
|
||||
}
|
||||
|
||||
int32_t mz_crypt_rand(uint8_t *buf, int32_t size)
|
||||
{
|
||||
int32_t result = 0;
|
||||
|
||||
result = RAND_bytes(buf, size);
|
||||
|
||||
if (!result)
|
||||
return MZ_CRYPT_ERROR;
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct mz_crypt_sha_s {
|
||||
SHA256_CTX ctx256;
|
||||
SHA_CTX ctx1;
|
||||
int32_t initialized;
|
||||
int32_t error;
|
||||
uint16_t algorithm;
|
||||
} mz_crypt_sha;
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
void mz_crypt_sha_reset(void *handle)
|
||||
{
|
||||
mz_crypt_sha *sha = (mz_crypt_sha *)handle;
|
||||
|
||||
sha->error = 0;
|
||||
sha->initialized = 0;
|
||||
|
||||
mz_crypt_init();
|
||||
}
|
||||
|
||||
int32_t mz_crypt_sha_begin(void *handle)
|
||||
{
|
||||
mz_crypt_sha *sha = (mz_crypt_sha *)handle;
|
||||
int32_t result = 0;
|
||||
|
||||
|
||||
if (sha == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
mz_crypt_sha_reset(handle);
|
||||
|
||||
if (sha->algorithm == MZ_HASH_SHA1)
|
||||
result = SHA1_Init(&sha->ctx1);
|
||||
else
|
||||
result = SHA256_Init(&sha->ctx256);
|
||||
|
||||
if (!result)
|
||||
{
|
||||
sha->error = ERR_get_error();
|
||||
return MZ_HASH_ERROR;
|
||||
}
|
||||
|
||||
sha->initialized = 1;
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_crypt_sha_update(void *handle, const void *buf, int32_t size)
|
||||
{
|
||||
mz_crypt_sha *sha = (mz_crypt_sha *)handle;
|
||||
int32_t result = 0;
|
||||
|
||||
if (sha == NULL || buf == NULL || !sha->initialized)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
if (sha->algorithm == MZ_HASH_SHA1)
|
||||
result = SHA1_Update(&sha->ctx1, buf, size);
|
||||
else
|
||||
result = SHA256_Update(&sha->ctx256, buf, size);
|
||||
|
||||
if (!result)
|
||||
{
|
||||
sha->error = ERR_get_error();
|
||||
return MZ_HASH_ERROR;
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
int32_t mz_crypt_sha_end(void *handle, uint8_t *digest, int32_t digest_size)
|
||||
{
|
||||
mz_crypt_sha *sha = (mz_crypt_sha *)handle;
|
||||
int32_t result = 0;
|
||||
|
||||
if (sha == NULL || digest == NULL || !sha->initialized)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
if (sha->algorithm == MZ_HASH_SHA1)
|
||||
{
|
||||
if (digest_size < MZ_HASH_SHA1_SIZE)
|
||||
return MZ_BUF_ERROR;
|
||||
result = SHA1_Final(digest, &sha->ctx1);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (digest_size < MZ_HASH_SHA256_SIZE)
|
||||
return MZ_BUF_ERROR;
|
||||
result = SHA256_Final(digest, &sha->ctx256);
|
||||
}
|
||||
|
||||
if (!result)
|
||||
{
|
||||
sha->error = ERR_get_error();
|
||||
return MZ_HASH_ERROR;
|
||||
}
|
||||
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
void mz_crypt_sha_set_algorithm(void *handle, uint16_t algorithm)
|
||||
{
|
||||
mz_crypt_sha *sha = (mz_crypt_sha *)handle;
|
||||
sha->algorithm = algorithm;
|
||||
}
|
||||
|
||||
void *mz_crypt_sha_create(void **handle)
|
||||
{
|
||||
mz_crypt_sha *sha = NULL;
|
||||
|
||||
sha = (mz_crypt_sha *)MZ_ALLOC(sizeof(mz_crypt_sha));
|
||||
if (sha != NULL)
|
||||
{
|
||||
memset(sha, 0, sizeof(mz_crypt_sha));
|
||||
sha->algorithm = MZ_HASH_SHA256;
|
||||
}
|
||||
if (handle != NULL)
|
||||
*handle = sha;
|
||||
|
||||
return sha;
|
||||
}
|
||||
|
||||
void mz_crypt_sha_delete(void **handle)
|
||||
{
|
||||
mz_crypt_sha *sha = NULL;
|
||||
if (handle == NULL)
|
||||
return;
|
||||
sha = (mz_crypt_sha *)*handle;
|
||||
if (sha != NULL)
|
||||
{
|
||||
mz_crypt_sha_reset(*handle);
|
||||
MZ_FREE(sha);
|
||||
}
|
||||
*handle = NULL;
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct mz_crypt_aes_s {
|
||||
AES_KEY key;
|
||||
int32_t mode;
|
||||
int32_t error;
|
||||
uint8_t *key_copy;
|
||||
int32_t key_length;
|
||||
} mz_crypt_aes;
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
void mz_crypt_aes_reset(void *handle)
|
||||
{
|
||||
MZ_UNUSED(handle);
|
||||
|
||||
mz_crypt_init();
|
||||
}
|
||||
|
||||
int32_t mz_crypt_aes_encrypt(void *handle, uint8_t *buf, int32_t size)
|
||||
{
|
||||
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
|
||||
|
||||
if (aes == NULL || buf == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
if (size != MZ_AES_BLOCK_SIZE)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
AES_encrypt(buf, buf, &aes->key);
|
||||
return size;
|
||||
}
|
||||
|
||||
int32_t mz_crypt_aes_decrypt(void *handle, uint8_t *buf, int32_t size)
|
||||
{
|
||||
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
|
||||
if (aes == NULL || buf == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
if (size != MZ_AES_BLOCK_SIZE)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
AES_decrypt(buf, buf, &aes->key);
|
||||
return size;
|
||||
}
|
||||
|
||||
int32_t mz_crypt_aes_set_encrypt_key(void *handle, const void *key, int32_t key_length)
|
||||
{
|
||||
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
|
||||
int32_t result = 0;
|
||||
int32_t key_bits = 0;
|
||||
|
||||
|
||||
if (aes == NULL || key == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
mz_crypt_aes_reset(handle);
|
||||
|
||||
key_bits = key_length * 8;
|
||||
result = AES_set_encrypt_key(key, key_bits, &aes->key);
|
||||
if (result)
|
||||
{
|
||||
aes->error = ERR_get_error();
|
||||
return MZ_HASH_ERROR;
|
||||
}
|
||||
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_crypt_aes_set_decrypt_key(void *handle, const void *key, int32_t key_length)
|
||||
{
|
||||
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
|
||||
int32_t result = 0;
|
||||
int32_t key_bits = 0;
|
||||
|
||||
|
||||
if (aes == NULL || key == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
mz_crypt_aes_reset(handle);
|
||||
|
||||
key_bits = key_length * 8;
|
||||
result = AES_set_decrypt_key(key, key_bits, &aes->key);
|
||||
if (result)
|
||||
{
|
||||
aes->error = ERR_get_error();
|
||||
return MZ_HASH_ERROR;
|
||||
}
|
||||
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
void mz_crypt_aes_set_mode(void *handle, int32_t mode)
|
||||
{
|
||||
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
|
||||
aes->mode = mode;
|
||||
}
|
||||
|
||||
void *mz_crypt_aes_create(void **handle)
|
||||
{
|
||||
mz_crypt_aes *aes = NULL;
|
||||
|
||||
aes = (mz_crypt_aes *)MZ_ALLOC(sizeof(mz_crypt_aes));
|
||||
if (aes != NULL)
|
||||
memset(aes, 0, sizeof(mz_crypt_aes));
|
||||
if (handle != NULL)
|
||||
*handle = aes;
|
||||
|
||||
return aes;
|
||||
}
|
||||
|
||||
void mz_crypt_aes_delete(void **handle)
|
||||
{
|
||||
mz_crypt_aes *aes = NULL;
|
||||
if (handle == NULL)
|
||||
return;
|
||||
aes = (mz_crypt_aes *)*handle;
|
||||
if (aes != NULL)
|
||||
MZ_FREE(aes);
|
||||
*handle = NULL;
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#if (OPENSSL_VERSION_NUMBER < 0x10100000L) || defined (LIBRESSL_VERSION_NUMBER)
|
||||
static HMAC_CTX *HMAC_CTX_new(void)
|
||||
{
|
||||
HMAC_CTX *ctx = OPENSSL_malloc(sizeof(HMAC_CTX));
|
||||
if (ctx != NULL)
|
||||
HMAC_CTX_init(ctx);
|
||||
return ctx;
|
||||
}
|
||||
|
||||
static void HMAC_CTX_free(HMAC_CTX *ctx)
|
||||
{
|
||||
if (ctx != NULL) {
|
||||
HMAC_CTX_cleanup(ctx);
|
||||
OPENSSL_free(ctx);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
typedef struct mz_crypt_hmac_s {
|
||||
HMAC_CTX* ctx;
|
||||
int32_t initialized;
|
||||
int32_t error;
|
||||
uint16_t algorithm;
|
||||
} mz_crypt_hmac;
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
static void mz_crypt_hmac_free(void *handle)
|
||||
{
|
||||
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
|
||||
HMAC_CTX_free(hmac->ctx);
|
||||
}
|
||||
|
||||
void mz_crypt_hmac_reset(void *handle)
|
||||
{
|
||||
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
|
||||
mz_crypt_hmac_free(handle);
|
||||
mz_crypt_init();
|
||||
hmac->error = 0;
|
||||
}
|
||||
|
||||
int32_t mz_crypt_hmac_init(void *handle, const void *key, int32_t key_length)
|
||||
{
|
||||
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
|
||||
int32_t result = 0;
|
||||
const EVP_MD *evp_md = NULL;
|
||||
|
||||
if (hmac == NULL || key == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
mz_crypt_hmac_reset(handle);
|
||||
|
||||
hmac->ctx = HMAC_CTX_new();
|
||||
if (hmac->algorithm == MZ_HASH_SHA1)
|
||||
evp_md = EVP_sha1();
|
||||
else
|
||||
evp_md = EVP_sha256();
|
||||
|
||||
result = HMAC_Init(hmac->ctx, key, key_length, evp_md);
|
||||
if (!result)
|
||||
{
|
||||
hmac->error = ERR_get_error();
|
||||
return MZ_HASH_ERROR;
|
||||
}
|
||||
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_crypt_hmac_update(void *handle, const void *buf, int32_t size)
|
||||
{
|
||||
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
|
||||
int32_t result = 0;
|
||||
|
||||
if (hmac == NULL || buf == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
result = HMAC_Update(hmac->ctx, buf, size);
|
||||
if (!result)
|
||||
{
|
||||
hmac->error = ERR_get_error();
|
||||
return MZ_HASH_ERROR;
|
||||
}
|
||||
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_crypt_hmac_end(void *handle, uint8_t *digest, int32_t digest_size)
|
||||
{
|
||||
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
|
||||
int32_t result = 0;
|
||||
|
||||
if (hmac == NULL || digest == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
if (hmac->algorithm == MZ_HASH_SHA1)
|
||||
{
|
||||
if (digest_size < MZ_HASH_SHA1_SIZE)
|
||||
return MZ_BUF_ERROR;
|
||||
|
||||
result = HMAC_Final(hmac->ctx, digest, (uint32_t *)&digest_size);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (digest_size < MZ_HASH_SHA256_SIZE)
|
||||
return MZ_BUF_ERROR;
|
||||
result = HMAC_Final(hmac->ctx, digest, (uint32_t *)&digest_size);
|
||||
}
|
||||
|
||||
if (!result)
|
||||
{
|
||||
hmac->error = ERR_get_error();
|
||||
return MZ_HASH_ERROR;
|
||||
}
|
||||
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
void mz_crypt_hmac_set_algorithm(void *handle, uint16_t algorithm)
|
||||
{
|
||||
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
|
||||
hmac->algorithm = algorithm;
|
||||
}
|
||||
|
||||
int32_t mz_crypt_hmac_copy(void *src_handle, void *target_handle)
|
||||
{
|
||||
mz_crypt_hmac *source = (mz_crypt_hmac *)src_handle;
|
||||
mz_crypt_hmac *target = (mz_crypt_hmac *)target_handle;
|
||||
int32_t result = 0;
|
||||
|
||||
if (source == NULL || target == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
result = HMAC_CTX_copy(target->ctx, source->ctx);
|
||||
if (!result)
|
||||
{
|
||||
target->error = ERR_get_error();
|
||||
return MZ_HASH_ERROR;
|
||||
}
|
||||
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
void *mz_crypt_hmac_create(void **handle)
|
||||
{
|
||||
mz_crypt_hmac *hmac = NULL;
|
||||
|
||||
hmac = (mz_crypt_hmac *)MZ_ALLOC(sizeof(mz_crypt_hmac));
|
||||
if (hmac != NULL)
|
||||
{
|
||||
memset(hmac, 0, sizeof(mz_crypt_hmac));
|
||||
hmac->algorithm = MZ_HASH_SHA256;
|
||||
}
|
||||
if (handle != NULL)
|
||||
*handle = hmac;
|
||||
|
||||
return hmac;
|
||||
}
|
||||
|
||||
void mz_crypt_hmac_delete(void **handle)
|
||||
{
|
||||
mz_crypt_hmac *hmac = NULL;
|
||||
if (handle == NULL)
|
||||
return;
|
||||
hmac = (mz_crypt_hmac *)*handle;
|
||||
if (hmac != NULL)
|
||||
{
|
||||
mz_crypt_hmac_free(*handle);
|
||||
MZ_FREE(hmac);
|
||||
}
|
||||
*handle = NULL;
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t mz_crypt_sign(uint8_t *message, int32_t message_size, uint8_t *cert_data, int32_t cert_data_size,
|
||||
const char *cert_pwd, uint8_t **signature, int32_t *signature_size)
|
||||
{
|
||||
PKCS12 *p12 = NULL;
|
||||
EVP_PKEY *evp_pkey = NULL;
|
||||
BUF_MEM *buf_mem = NULL;
|
||||
BIO *cert_bio = NULL;
|
||||
BIO *message_bio = NULL;
|
||||
BIO *signature_bio = NULL;
|
||||
CMS_ContentInfo *cms = NULL;
|
||||
CMS_SignerInfo *signer_info = NULL;
|
||||
STACK_OF(X509) *ca_stack = NULL;
|
||||
X509 *cert = NULL;
|
||||
int32_t result = 0;
|
||||
int32_t err = MZ_OK;
|
||||
|
||||
|
||||
if (message == NULL || cert_data == NULL || signature == NULL || signature_size == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
mz_crypt_init();
|
||||
|
||||
*signature = NULL;
|
||||
*signature_size = 0;
|
||||
|
||||
cert_bio = BIO_new_mem_buf(cert_data, cert_data_size);
|
||||
|
||||
if (d2i_PKCS12_bio(cert_bio, &p12) == NULL)
|
||||
err = MZ_SIGN_ERROR;
|
||||
if (err == MZ_OK)
|
||||
result = PKCS12_parse(p12, cert_pwd, &evp_pkey, &cert, &ca_stack);
|
||||
if (result)
|
||||
{
|
||||
cms = CMS_sign(NULL, NULL, ca_stack, NULL, CMS_BINARY | CMS_PARTIAL);
|
||||
if (cms)
|
||||
signer_info = CMS_add1_signer(cms, cert, evp_pkey, EVP_sha256(), 0);
|
||||
if (signer_info == NULL)
|
||||
{
|
||||
err = MZ_SIGN_ERROR;
|
||||
}
|
||||
else
|
||||
{
|
||||
message_bio = BIO_new_mem_buf(message, message_size);
|
||||
signature_bio = BIO_new(BIO_s_mem());
|
||||
|
||||
result = CMS_final(cms, message_bio, NULL, CMS_BINARY);
|
||||
if (result)
|
||||
result = i2d_CMS_bio(signature_bio, cms);
|
||||
if (result)
|
||||
{
|
||||
BIO_flush(signature_bio);
|
||||
BIO_get_mem_ptr(signature_bio, &buf_mem);
|
||||
|
||||
*signature_size = buf_mem->length;
|
||||
*signature = MZ_ALLOC(buf_mem->length);
|
||||
|
||||
memcpy(*signature, buf_mem->data, buf_mem->length);
|
||||
}
|
||||
#if 0
|
||||
BIO *yy = BIO_new_file("xyz", "wb");
|
||||
BIO_write(yy, *signature, *signature_size);
|
||||
BIO_flush(yy);
|
||||
BIO_free(yy);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
if (!result)
|
||||
err = MZ_SIGN_ERROR;
|
||||
|
||||
if (cms)
|
||||
CMS_ContentInfo_free(cms);
|
||||
if (signature_bio)
|
||||
BIO_free(signature_bio);
|
||||
if (cert_bio)
|
||||
BIO_free(cert_bio);
|
||||
if (message_bio)
|
||||
BIO_free(message_bio);
|
||||
if (p12)
|
||||
PKCS12_free(p12);
|
||||
|
||||
if (err != MZ_OK && *signature != NULL)
|
||||
{
|
||||
MZ_FREE(*signature);
|
||||
*signature = NULL;
|
||||
*signature_size = 0;
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
int32_t mz_crypt_sign_verify(uint8_t *message, int32_t message_size, uint8_t *signature, int32_t signature_size)
|
||||
{
|
||||
CMS_ContentInfo *cms = NULL;
|
||||
STACK_OF(X509) *signers = NULL;
|
||||
STACK_OF(X509) *intercerts = NULL;
|
||||
X509_STORE *cert_store = NULL;
|
||||
X509_LOOKUP *lookup = NULL;
|
||||
X509_STORE_CTX *store_ctx = NULL;
|
||||
BIO *message_bio = NULL;
|
||||
BIO *signature_bio = NULL;
|
||||
BUF_MEM *buf_mem = NULL;
|
||||
int32_t signer_count = 0;
|
||||
int32_t result = 0;
|
||||
int32_t i = 0;
|
||||
int32_t err = MZ_SIGN_ERROR;
|
||||
|
||||
|
||||
if (message == NULL || message_size == 0 || signature == NULL || signature_size == 0)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
mz_crypt_init();
|
||||
|
||||
cert_store = X509_STORE_new();
|
||||
|
||||
X509_STORE_load_locations(cert_store, "cacert.pem", NULL);
|
||||
X509_STORE_set_default_paths(cert_store);
|
||||
|
||||
#if 0
|
||||
BIO *yy = BIO_new_file("xyz", "wb");
|
||||
BIO_write(yy, signature, signature_size);
|
||||
BIO_flush(yy);
|
||||
BIO_free(yy);
|
||||
#endif
|
||||
|
||||
lookup = X509_STORE_add_lookup(cert_store, X509_LOOKUP_file());
|
||||
if (lookup != NULL)
|
||||
X509_LOOKUP_load_file(lookup, "cacert.pem", X509_FILETYPE_PEM);
|
||||
lookup = X509_STORE_add_lookup(cert_store, X509_LOOKUP_hash_dir());
|
||||
if (lookup != NULL)
|
||||
X509_LOOKUP_add_dir(lookup, NULL, X509_FILETYPE_DEFAULT);
|
||||
|
||||
signature_bio = BIO_new_mem_buf(signature, signature_size);
|
||||
message_bio = BIO_new(BIO_s_mem());
|
||||
|
||||
cms = d2i_CMS_bio(signature_bio, NULL);
|
||||
if (cms)
|
||||
{
|
||||
result = CMS_verify(cms, NULL, cert_store, NULL, message_bio, CMS_NO_SIGNER_CERT_VERIFY | CMS_BINARY);
|
||||
if (result)
|
||||
signers = CMS_get0_signers(cms);
|
||||
if (signers)
|
||||
intercerts = CMS_get1_certs(cms);
|
||||
if (intercerts)
|
||||
{
|
||||
// Verify signer certificates
|
||||
if (signer_count > 0)
|
||||
err = MZ_OK;
|
||||
|
||||
for (i = 0; i < sk_X509_num(signers); i++)
|
||||
{
|
||||
store_ctx = X509_STORE_CTX_new();
|
||||
X509_STORE_CTX_init(store_ctx, cert_store, sk_X509_value(signers, i), intercerts);
|
||||
result = X509_verify_cert(store_ctx);
|
||||
if (store_ctx)
|
||||
X509_STORE_CTX_free(store_ctx);
|
||||
|
||||
if (!result)
|
||||
{
|
||||
err = MZ_SIGN_ERROR;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
BIO_get_mem_ptr(message_bio, &buf_mem);
|
||||
|
||||
if (err == MZ_OK)
|
||||
{
|
||||
// Verify the message
|
||||
if (((int32_t)buf_mem->length != message_size) ||
|
||||
(memcmp(buf_mem->data, message, message_size) != 0))
|
||||
err = MZ_SIGN_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
#if 0
|
||||
if (!result)
|
||||
printf(ERR_error_string(ERR_get_error(), NULL));
|
||||
#endif
|
||||
|
||||
if (cms)
|
||||
CMS_ContentInfo_free(cms);
|
||||
if (message_bio)
|
||||
BIO_free(message_bio);
|
||||
if (signature_bio)
|
||||
BIO_free(signature_bio);
|
||||
if (cert_store)
|
||||
X509_STORE_free(cert_store);
|
||||
|
||||
return err;
|
||||
}
|
||||
@@ -0,0 +1,755 @@
|
||||
/* mz_crypt_win32.c -- Crypto/hash functions for Windows
|
||||
Version 2.7.3, November 4, 2018
|
||||
part of the MiniZip project
|
||||
|
||||
Copyright (C) 2010-2018 Nathan Moinvaziri
|
||||
https://github.com/nmoinvaz/minizip
|
||||
|
||||
This program is distributed under the terms of the same license as zlib.
|
||||
See the accompanying LICENSE file for the full text of the license.
|
||||
*/
|
||||
|
||||
#pragma comment(lib, "crypt32.lib")
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <windows.h>
|
||||
#include <wincrypt.h>
|
||||
|
||||
#include "mz.h"
|
||||
|
||||
#include "mz_os.h"
|
||||
#include "mz_strm_os.h"
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t mz_crypt_rand(uint8_t *buf, int32_t size)
|
||||
{
|
||||
HCRYPTPROV provider;
|
||||
int32_t result = 0;
|
||||
|
||||
|
||||
result = CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT);
|
||||
if (result)
|
||||
{
|
||||
result = CryptGenRandom(provider, size, buf);
|
||||
CryptReleaseContext(provider, 0);
|
||||
if (result)
|
||||
return size;
|
||||
}
|
||||
|
||||
return mz_os_rand(buf, size);
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct mz_crypt_sha_s {
|
||||
HCRYPTPROV provider;
|
||||
HCRYPTHASH hash;
|
||||
int32_t error;
|
||||
uint16_t algorithm;
|
||||
} mz_crypt_sha;
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
void mz_crypt_sha_reset(void *handle)
|
||||
{
|
||||
mz_crypt_sha *sha = (mz_crypt_sha *)handle;
|
||||
if (sha->hash)
|
||||
CryptDestroyHash(sha->hash);
|
||||
sha->hash = 0;
|
||||
if (sha->provider)
|
||||
CryptReleaseContext(sha->provider, 0);
|
||||
sha->provider = 0;
|
||||
sha->error = 0;
|
||||
}
|
||||
|
||||
int32_t mz_crypt_sha_begin(void *handle)
|
||||
{
|
||||
mz_crypt_sha *sha = (mz_crypt_sha *)handle;
|
||||
ALG_ID alg_id = 0;
|
||||
int32_t result = 0;
|
||||
int32_t err = MZ_OK;
|
||||
|
||||
|
||||
if (sha == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
if (sha->algorithm == MZ_HASH_SHA1)
|
||||
alg_id = CALG_SHA1;
|
||||
else
|
||||
alg_id = CALG_SHA_256;
|
||||
|
||||
result = CryptAcquireContext(&sha->provider, NULL, NULL, PROV_RSA_AES, CRYPT_VERIFYCONTEXT | CRYPT_SILENT);
|
||||
if (!result)
|
||||
{
|
||||
sha->error = GetLastError();
|
||||
err = MZ_CRYPT_ERROR;
|
||||
}
|
||||
|
||||
if (result)
|
||||
{
|
||||
result = CryptCreateHash(sha->provider, alg_id, 0, 0, &sha->hash);
|
||||
if (!result)
|
||||
{
|
||||
sha->error = GetLastError();
|
||||
err = MZ_HASH_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
int32_t mz_crypt_sha_update(void *handle, const void *buf, int32_t size)
|
||||
{
|
||||
mz_crypt_sha *sha = (mz_crypt_sha *)handle;
|
||||
int32_t result = 0;
|
||||
|
||||
if (sha == NULL || buf == NULL || sha->hash == 0)
|
||||
return MZ_PARAM_ERROR;
|
||||
result = CryptHashData(sha->hash, buf, size, 0);
|
||||
if (!result)
|
||||
{
|
||||
sha->error = GetLastError();
|
||||
return MZ_HASH_ERROR;
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
int32_t mz_crypt_sha_end(void *handle, uint8_t *digest, int32_t digest_size)
|
||||
{
|
||||
mz_crypt_sha *sha = (mz_crypt_sha *)handle;
|
||||
int32_t result = 0;
|
||||
int32_t expected_size = 0;
|
||||
|
||||
if (sha == NULL || digest == NULL || sha->hash == 0)
|
||||
return MZ_PARAM_ERROR;
|
||||
result = CryptGetHashParam(sha->hash, HP_HASHVAL, NULL, &expected_size, 0);
|
||||
if (expected_size > digest_size)
|
||||
return MZ_BUF_ERROR;
|
||||
if (!result)
|
||||
return MZ_HASH_ERROR;
|
||||
result = CryptGetHashParam(sha->hash, HP_HASHVAL, digest, &digest_size, 0);
|
||||
if (!result)
|
||||
{
|
||||
sha->error = GetLastError();
|
||||
return MZ_HASH_ERROR;
|
||||
}
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
void mz_crypt_sha_set_algorithm(void *handle, uint16_t algorithm)
|
||||
{
|
||||
mz_crypt_sha *sha = (mz_crypt_sha *)handle;
|
||||
sha->algorithm = algorithm;
|
||||
}
|
||||
|
||||
void *mz_crypt_sha_create(void **handle)
|
||||
{
|
||||
mz_crypt_sha *sha = NULL;
|
||||
|
||||
sha = (mz_crypt_sha *)MZ_ALLOC(sizeof(mz_crypt_sha));
|
||||
if (sha != NULL)
|
||||
{
|
||||
memset(sha, 0, sizeof(mz_crypt_sha));
|
||||
sha->algorithm = MZ_HASH_SHA256;
|
||||
}
|
||||
if (handle != NULL)
|
||||
*handle = sha;
|
||||
|
||||
return sha;
|
||||
}
|
||||
|
||||
void mz_crypt_sha_delete(void **handle)
|
||||
{
|
||||
mz_crypt_sha *sha = NULL;
|
||||
if (handle == NULL)
|
||||
return;
|
||||
sha = (mz_crypt_sha *)*handle;
|
||||
if (sha != NULL)
|
||||
{
|
||||
mz_crypt_sha_reset(*handle);
|
||||
MZ_FREE(sha);
|
||||
}
|
||||
*handle = NULL;
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct mz_crypt_aes_s {
|
||||
HCRYPTPROV provider;
|
||||
HCRYPTKEY key;
|
||||
int32_t mode;
|
||||
int32_t error;
|
||||
} mz_crypt_aes;
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
static void mz_crypt_aes_free(void *handle)
|
||||
{
|
||||
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
|
||||
if (aes->key)
|
||||
CryptDestroyKey(aes->key);
|
||||
aes->key = 0;
|
||||
if (aes->provider)
|
||||
CryptReleaseContext(aes->provider, 0);
|
||||
aes->provider = 0;
|
||||
}
|
||||
|
||||
void mz_crypt_aes_reset(void *handle)
|
||||
{
|
||||
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
|
||||
mz_crypt_aes_free(handle);
|
||||
}
|
||||
|
||||
int32_t mz_crypt_aes_encrypt(void *handle, uint8_t *buf, int32_t size)
|
||||
{
|
||||
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
|
||||
int32_t result = 0;
|
||||
|
||||
if (aes == NULL || buf == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
if (size != MZ_AES_BLOCK_SIZE)
|
||||
return MZ_PARAM_ERROR;
|
||||
result = CryptEncrypt(aes->key, 0, 0, 0, buf, &size, size);
|
||||
if (!result)
|
||||
{
|
||||
aes->error = GetLastError();
|
||||
return MZ_CRYPT_ERROR;
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
int32_t mz_crypt_aes_decrypt(void *handle, uint8_t *buf, int32_t size)
|
||||
{
|
||||
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
|
||||
int32_t result = 0;
|
||||
if (aes == NULL || buf == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
if (size != MZ_AES_BLOCK_SIZE)
|
||||
return MZ_PARAM_ERROR;
|
||||
result = CryptDecrypt(aes->key, 0, 0, 0, buf, &size);
|
||||
if (!result)
|
||||
{
|
||||
aes->error = GetLastError();
|
||||
return MZ_CRYPT_ERROR;
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
static int32_t mz_crypt_aes_set_key(void *handle, const void *key, int32_t key_length)
|
||||
{
|
||||
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
|
||||
HCRYPTHASH hash = 0;
|
||||
ALG_ID alg_id = 0;
|
||||
ALG_ID hash_alg_id = 0;
|
||||
typedef struct key_blob_header_s {
|
||||
BLOBHEADER hdr;
|
||||
uint32_t key_length;
|
||||
} key_blob_header_s;
|
||||
key_blob_header_s *key_blob_s = NULL;
|
||||
uint8_t *key_blob = NULL;
|
||||
int32_t key_blob_size = 0;
|
||||
int32_t result = 0;
|
||||
int32_t err = MZ_OK;
|
||||
|
||||
|
||||
if (aes == NULL || key == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
mz_crypt_aes_reset(handle);
|
||||
|
||||
if (key_length == MZ_AES_KEY_LENGTH(MZ_AES_ENCRYPTION_MODE_128))
|
||||
alg_id = CALG_AES_128;
|
||||
else if (key_length == MZ_AES_KEY_LENGTH(MZ_AES_ENCRYPTION_MODE_192))
|
||||
alg_id = CALG_AES_192;
|
||||
else if (key_length == MZ_AES_KEY_LENGTH(MZ_AES_ENCRYPTION_MODE_256))
|
||||
alg_id = CALG_AES_256;
|
||||
else
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
result = CryptAcquireContext(&aes->provider, NULL, MS_ENH_RSA_AES_PROV, PROV_RSA_AES, CRYPT_VERIFYCONTEXT | CRYPT_SILENT);
|
||||
if (result)
|
||||
{
|
||||
key_blob_size = sizeof(key_blob_header_s) + key_length;
|
||||
key_blob = (uint8_t *)MZ_ALLOC(key_blob_size);
|
||||
|
||||
key_blob_s = (key_blob_header_s *)key_blob;
|
||||
key_blob_s->hdr.bType = PLAINTEXTKEYBLOB;
|
||||
key_blob_s->hdr.bVersion = CUR_BLOB_VERSION;
|
||||
key_blob_s->hdr.aiKeyAlg = alg_id;
|
||||
key_blob_s->hdr.reserved = 0;
|
||||
key_blob_s->key_length = key_length;
|
||||
|
||||
memcpy(key_blob + sizeof(key_blob_header_s), key, key_length);
|
||||
|
||||
result = CryptImportKey(aes->provider, key_blob, key_blob_size, 0, CRYPT_IPSEC_HMAC_KEY, &aes->key);
|
||||
}
|
||||
if (!result)
|
||||
{
|
||||
aes->error = GetLastError();
|
||||
err = MZ_CRYPT_ERROR;
|
||||
}
|
||||
MZ_FREE(key_blob);
|
||||
|
||||
if (hash)
|
||||
CryptDestroyHash(hash);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
int32_t mz_crypt_aes_set_encrypt_key(void *handle, const void *key, int32_t key_length)
|
||||
{
|
||||
return mz_crypt_aes_set_key(handle, key, key_length);
|
||||
}
|
||||
|
||||
int32_t mz_crypt_aes_set_decrypt_key(void *handle, const void *key, int32_t key_length)
|
||||
{
|
||||
return mz_crypt_aes_set_key(handle, key, key_length);
|
||||
}
|
||||
|
||||
void mz_crypt_aes_set_mode(void *handle, int32_t mode)
|
||||
{
|
||||
mz_crypt_aes *aes = (mz_crypt_aes *)handle;
|
||||
aes->mode = mode;
|
||||
}
|
||||
|
||||
void *mz_crypt_aes_create(void **handle)
|
||||
{
|
||||
mz_crypt_aes *aes = NULL;
|
||||
|
||||
aes = (mz_crypt_aes *)MZ_ALLOC(sizeof(mz_crypt_aes));
|
||||
if (aes != NULL)
|
||||
memset(aes, 0, sizeof(mz_crypt_aes));
|
||||
if (handle != NULL)
|
||||
*handle = aes;
|
||||
|
||||
return aes;
|
||||
}
|
||||
|
||||
void mz_crypt_aes_delete(void **handle)
|
||||
{
|
||||
mz_crypt_aes *aes = NULL;
|
||||
if (handle == NULL)
|
||||
return;
|
||||
aes = (mz_crypt_aes *)*handle;
|
||||
if (aes != NULL)
|
||||
{
|
||||
mz_crypt_aes_free(*handle);
|
||||
MZ_FREE(aes);
|
||||
}
|
||||
*handle = NULL;
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct mz_crypt_hmac_s {
|
||||
HCRYPTPROV provider;
|
||||
HCRYPTHASH hash;
|
||||
HCRYPTKEY key;
|
||||
HMAC_INFO info;
|
||||
int32_t mode;
|
||||
int32_t error;
|
||||
uint16_t algorithm;
|
||||
} mz_crypt_hmac;
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
static void mz_crypt_hmac_free(void *handle)
|
||||
{
|
||||
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
|
||||
if (hmac->key)
|
||||
CryptDestroyKey(hmac->key);
|
||||
hmac->key = 0;
|
||||
if (hmac->hash)
|
||||
CryptDestroyHash(hmac->hash);
|
||||
hmac->hash = 0;
|
||||
if (hmac->provider)
|
||||
CryptReleaseContext(hmac->provider, 0);
|
||||
hmac->provider = 0;
|
||||
memset(&hmac->info, 0, sizeof(hmac->info));
|
||||
}
|
||||
|
||||
void mz_crypt_hmac_reset(void *handle)
|
||||
{
|
||||
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
|
||||
mz_crypt_hmac_free(handle);
|
||||
}
|
||||
|
||||
int32_t mz_crypt_hmac_init(void *handle, const void *key, int32_t key_length)
|
||||
{
|
||||
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
|
||||
HCRYPTHASH hash = 0;
|
||||
ALG_ID alg_id = 0;
|
||||
typedef struct key_blob_header_s {
|
||||
BLOBHEADER hdr;
|
||||
uint32_t key_length;
|
||||
} key_blob_header_s;
|
||||
key_blob_header_s *key_blob_s = NULL;
|
||||
uint8_t *key_blob = NULL;
|
||||
int32_t key_blob_size = 0;
|
||||
int32_t result = 0;
|
||||
int32_t err = MZ_OK;
|
||||
|
||||
|
||||
if (hmac == NULL || key == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
mz_crypt_hmac_reset(handle);
|
||||
|
||||
if (hmac->algorithm == MZ_HASH_SHA1)
|
||||
alg_id = CALG_SHA1;
|
||||
else
|
||||
alg_id = CALG_SHA_256;
|
||||
|
||||
hmac->info.HashAlgid = alg_id;
|
||||
|
||||
result = CryptAcquireContext(&hmac->provider, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL,
|
||||
CRYPT_VERIFYCONTEXT | CRYPT_SILENT);
|
||||
|
||||
if (!result)
|
||||
{
|
||||
hmac->error = GetLastError();
|
||||
err = MZ_CRYPT_ERROR;
|
||||
}
|
||||
|
||||
key_blob_size = sizeof(key_blob_header_s) + key_length;
|
||||
key_blob = (uint8_t *)MZ_ALLOC(key_blob_size);
|
||||
|
||||
key_blob_s = (key_blob_header_s *)key_blob;
|
||||
key_blob_s->hdr.bType = PLAINTEXTKEYBLOB;
|
||||
key_blob_s->hdr.bVersion = CUR_BLOB_VERSION;
|
||||
key_blob_s->hdr.aiKeyAlg = CALG_RC2;
|
||||
key_blob_s->hdr.reserved = 0;
|
||||
key_blob_s->key_length = key_length;
|
||||
|
||||
memcpy(key_blob + sizeof(key_blob_header_s), key, key_length);
|
||||
|
||||
result = CryptImportKey(hmac->provider, key_blob, key_blob_size, 0, CRYPT_IPSEC_HMAC_KEY, &hmac->key);
|
||||
if (result)
|
||||
result = CryptCreateHash(hmac->provider, CALG_HMAC, hmac->key, 0, &hmac->hash);
|
||||
if (result)
|
||||
result = CryptSetHashParam(hmac->hash, HP_HMAC_INFO, (uint8_t *)&hmac->info, 0);
|
||||
if (!result)
|
||||
{
|
||||
hmac->error = GetLastError();
|
||||
err = MZ_CRYPT_ERROR;
|
||||
}
|
||||
|
||||
MZ_FREE(key_blob);
|
||||
|
||||
if (err != MZ_OK)
|
||||
mz_crypt_hmac_free(handle);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
int32_t mz_crypt_hmac_update(void *handle, const void *buf, int32_t size)
|
||||
{
|
||||
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
|
||||
int32_t result = 0;
|
||||
|
||||
if (hmac == NULL || buf == NULL || hmac->hash == 0)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
result = CryptHashData(hmac->hash, buf, size, 0);
|
||||
if (!result)
|
||||
{
|
||||
hmac->error = GetLastError();
|
||||
return MZ_HASH_ERROR;
|
||||
}
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_crypt_hmac_end(void *handle, uint8_t *digest, int32_t digest_size)
|
||||
{
|
||||
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
|
||||
int32_t result = 0;
|
||||
int32_t expected_size = 0;
|
||||
int32_t err = MZ_OK;
|
||||
|
||||
if (hmac == NULL || digest == NULL || hmac->hash == 0)
|
||||
return MZ_PARAM_ERROR;
|
||||
result = CryptGetHashParam(hmac->hash, HP_HASHVAL, NULL, &expected_size, 0);
|
||||
if (expected_size > digest_size)
|
||||
return MZ_BUF_ERROR;
|
||||
if (!result)
|
||||
return MZ_HASH_ERROR;
|
||||
result = CryptGetHashParam(hmac->hash, HP_HASHVAL, digest, &digest_size, 0);
|
||||
if (!result)
|
||||
{
|
||||
hmac->error = GetLastError();
|
||||
return MZ_HASH_ERROR;
|
||||
}
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
void mz_crypt_hmac_set_algorithm(void *handle, uint16_t algorithm)
|
||||
{
|
||||
mz_crypt_hmac *hmac = (mz_crypt_hmac *)handle;
|
||||
hmac->algorithm = algorithm;
|
||||
}
|
||||
|
||||
int32_t mz_crypt_hmac_copy(void *src_handle, void *target_handle)
|
||||
{
|
||||
mz_crypt_hmac *source = (mz_crypt_hmac *)src_handle;
|
||||
mz_crypt_hmac *target = (mz_crypt_hmac *)target_handle;
|
||||
int32_t result = 0;
|
||||
int32_t err = MZ_OK;
|
||||
|
||||
result = CryptDuplicateHash(source->hash, NULL, 0, &target->hash);
|
||||
|
||||
if (!result)
|
||||
{
|
||||
target->error = GetLastError();
|
||||
err = MZ_HASH_ERROR;
|
||||
}
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
void *mz_crypt_hmac_create(void **handle)
|
||||
{
|
||||
mz_crypt_hmac *hmac = NULL;
|
||||
|
||||
hmac = (mz_crypt_hmac *)MZ_ALLOC(sizeof(mz_crypt_hmac));
|
||||
if (hmac != NULL)
|
||||
{
|
||||
memset(hmac, 0, sizeof(mz_crypt_hmac));
|
||||
hmac->algorithm = MZ_HASH_SHA256;
|
||||
}
|
||||
if (handle != NULL)
|
||||
*handle = hmac;
|
||||
|
||||
return hmac;
|
||||
}
|
||||
|
||||
void mz_crypt_hmac_delete(void **handle)
|
||||
{
|
||||
mz_crypt_hmac *hmac = NULL;
|
||||
if (handle == NULL)
|
||||
return;
|
||||
hmac = (mz_crypt_hmac *)*handle;
|
||||
if (hmac != NULL)
|
||||
{
|
||||
mz_crypt_hmac_free(*handle);
|
||||
MZ_FREE(hmac);
|
||||
}
|
||||
*handle = NULL;
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t mz_crypt_sign(uint8_t *message, int32_t message_size, uint8_t *cert_data, int32_t cert_data_size,
|
||||
const char *cert_pwd, uint8_t **signature, int32_t *signature_size)
|
||||
{
|
||||
CRYPT_SIGN_MESSAGE_PARA sign_params;
|
||||
CRYPT_DATA_BLOB cert_data_blob;
|
||||
PCCERT_CONTEXT cert_context = NULL;
|
||||
HCERTSTORE cert_store = 0;
|
||||
wchar_t *password_wide = NULL;
|
||||
int32_t result = 0;
|
||||
int32_t err = MZ_OK;
|
||||
uint32_t key_spec = 0;
|
||||
uint32_t messages_sizes[1];
|
||||
uint8_t *messages[1];
|
||||
|
||||
|
||||
if (message == NULL || cert_data == NULL || signature == NULL || signature_size == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
*signature = NULL;
|
||||
*signature_size = 0;
|
||||
|
||||
cert_data_blob.pbData = cert_data;
|
||||
cert_data_blob.cbData = cert_data_size;
|
||||
|
||||
password_wide = mz_os_unicode_string_create(cert_pwd, MZ_ENCODING_UTF8);
|
||||
if (password_wide)
|
||||
{
|
||||
cert_store = PFXImportCertStore(&cert_data_blob, password_wide, 0);
|
||||
mz_os_unicode_string_delete(&password_wide);
|
||||
}
|
||||
|
||||
if (cert_store == NULL)
|
||||
cert_store = PFXImportCertStore(&cert_data_blob, L"", 0);
|
||||
if (cert_store == NULL)
|
||||
cert_store = PFXImportCertStore(&cert_data_blob, NULL, 0);
|
||||
if (cert_store == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
if (err == MZ_OK)
|
||||
{
|
||||
cert_context = CertFindCertificateInStore(cert_store,
|
||||
X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, 0, CERT_FIND_HAS_PRIVATE_KEY, NULL, NULL);
|
||||
if (cert_context == NULL)
|
||||
err = MZ_PARAM_ERROR;
|
||||
}
|
||||
if (err == MZ_OK)
|
||||
{
|
||||
memset(&sign_params, 0, sizeof(sign_params));
|
||||
|
||||
sign_params.cbSize = sizeof(sign_params);
|
||||
sign_params.dwMsgEncodingType = PKCS_7_ASN_ENCODING | X509_ASN_ENCODING;
|
||||
sign_params.pSigningCert = cert_context;
|
||||
sign_params.HashAlgorithm.pszObjId = szOID_NIST_sha256;
|
||||
sign_params.cMsgCert = 1;
|
||||
sign_params.rgpMsgCert = &cert_context;
|
||||
|
||||
messages[0] = message;
|
||||
messages_sizes[0] = message_size;
|
||||
|
||||
#if 0 // Timestamp support
|
||||
CRYPT_ATTR_BLOB crypt_blob;
|
||||
CRYPT_TIMESTAMP_CONTEXT *ts_context = NULL;
|
||||
CRYPT_ATTRIBUTE unauth_attribs[1];
|
||||
wchar_t *timestamp_url_wide = NULL;
|
||||
const char *timestamp_url = NULL;
|
||||
|
||||
if (timestamp_url != NULL)
|
||||
timestamp_url_wide = mz_os_unicode_string_create(timestamp_url);
|
||||
if (timestamp_url_wide != NULL)
|
||||
{
|
||||
result = CryptRetrieveTimeStamp(timestamp_url_wide,
|
||||
TIMESTAMP_NO_AUTH_RETRIEVAL | TIMESTAMP_VERIFY_CONTEXT_SIGNATURE, 0, szOID_NIST_sha256,
|
||||
NULL, message, message_size, &ts_context, NULL, NULL);
|
||||
|
||||
mz_os_unicode_string_delete(×tamp_url_wide);
|
||||
|
||||
if ((result) && (ts_context != NULL))
|
||||
{
|
||||
crypt_blob.cbData = ts_context->cbEncoded;
|
||||
crypt_blob.pbData = ts_context->pbEncoded;
|
||||
|
||||
unauth_attribs[0].pszObjId = "1.2.840.113549.1.9.16.2.14"; //id-smime-aa-timeStampToken
|
||||
unauth_attribs[0].cValue = 1;
|
||||
unauth_attribs[0].rgValue = &crypt_blob;
|
||||
|
||||
sign_params.rgUnauthAttr = &unauth_attribs[0];
|
||||
sign_params.cUnauthAttr = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (ts_context != NULL)
|
||||
CryptMemFree(ts_context);
|
||||
|
||||
if (result)
|
||||
#endif
|
||||
|
||||
result = CryptSignMessage(&sign_params, FALSE, 1, messages, messages_sizes,
|
||||
NULL, signature_size);
|
||||
|
||||
if (result && *signature_size > 0)
|
||||
*signature = (uint8_t *)MZ_ALLOC(*signature_size);
|
||||
|
||||
if (result && *signature != NULL)
|
||||
result = CryptSignMessage(&sign_params, FALSE, 1, messages, messages_sizes,
|
||||
*signature, signature_size);
|
||||
|
||||
if (!result)
|
||||
err = MZ_SIGN_ERROR;
|
||||
}
|
||||
|
||||
if (cert_context != NULL)
|
||||
CertFreeCertificateContext(cert_context);
|
||||
if (cert_store != NULL)
|
||||
CertCloseStore(cert_store, 0);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
int32_t mz_crypt_sign_verify(uint8_t *message, int32_t message_size, uint8_t *signature, int32_t signature_size)
|
||||
{
|
||||
CRYPT_VERIFY_MESSAGE_PARA verify_params;
|
||||
HCRYPTMSG crypt_msg = 0;
|
||||
int32_t result = 0;
|
||||
int32_t err = MZ_SIGN_ERROR;
|
||||
uint8_t *decoded = NULL;
|
||||
int32_t decoded_size = 0;
|
||||
|
||||
|
||||
memset(&verify_params, 0, sizeof(verify_params));
|
||||
|
||||
verify_params.cbSize = sizeof(verify_params);
|
||||
|
||||
verify_params.dwMsgAndCertEncodingType = PKCS_7_ASN_ENCODING | X509_ASN_ENCODING;
|
||||
verify_params.hCryptProv = 0;
|
||||
verify_params.pfnGetSignerCertificate = NULL;
|
||||
verify_params.pvGetArg = NULL;
|
||||
|
||||
result = CryptVerifyMessageSignature(&verify_params, 0, signature, signature_size,
|
||||
NULL, &decoded_size, NULL);
|
||||
|
||||
if (result && decoded_size > 0)
|
||||
decoded = (uint8_t *)MZ_ALLOC(decoded_size);
|
||||
|
||||
if (result)
|
||||
result = CryptVerifyMessageSignature(&verify_params, 0, signature, signature_size,
|
||||
decoded, &decoded_size, NULL);
|
||||
|
||||
crypt_msg = CryptMsgOpenToDecode(PKCS_7_ASN_ENCODING | X509_ASN_ENCODING, 0, 0, 0, NULL, NULL);
|
||||
if (crypt_msg != NULL)
|
||||
{
|
||||
#if 0 // Timestamp support
|
||||
PCRYPT_ATTRIBUTES unauth_attribs = NULL;
|
||||
HCRYPTMSG ts_msg = 0;
|
||||
uint8_t *ts_content = NULL;
|
||||
int32_t ts_content_size = 0;
|
||||
uint8_t *ts_signature = NULL;
|
||||
int32_t ts_signature_size = 0;
|
||||
|
||||
result = CryptMsgUpdate(crypt_msg, signature, signature_size, 1);
|
||||
|
||||
if (result)
|
||||
CryptMsgGetParam(crypt_msg, CMSG_SIGNER_UNAUTH_ATTR_PARAM, 0, NULL, &ts_signature_size);
|
||||
|
||||
if ((result) && (ts_signature_size > 0))
|
||||
ts_signature = (uint8_t *)MZ_ALLOC(ts_signature_size);
|
||||
|
||||
if ((result) && (ts_signature != NULL))
|
||||
{
|
||||
result = CryptMsgGetParam(crypt_msg, CMSG_SIGNER_UNAUTH_ATTR_PARAM, 0, ts_signature,
|
||||
&ts_signature_size);
|
||||
if (result)
|
||||
{
|
||||
unauth_attribs = (PCRYPT_ATTRIBUTES)ts_signature;
|
||||
|
||||
if ((unauth_attribs->cAttr > 0) && (unauth_attribs->rgAttr[0].cValue > 0))
|
||||
{
|
||||
ts_content = unauth_attribs->rgAttr[0].rgValue->pbData;
|
||||
ts_content_size = unauth_attribs->rgAttr[0].rgValue->cbData;
|
||||
}
|
||||
}
|
||||
|
||||
if ((result) && (ts_content != NULL))
|
||||
result = CryptVerifyTimeStampSignature(ts_content, ts_content_size, decoded,
|
||||
decoded_size, 0, &crypt_context, NULL, NULL);
|
||||
|
||||
if (result)
|
||||
err = MZ_OK;
|
||||
}
|
||||
|
||||
if (ts_signature != NULL)
|
||||
MZ_FREE(ts_signature);
|
||||
|
||||
if (crypt_context != NULL)
|
||||
CryptMemFree(crypt_context);
|
||||
#endif
|
||||
}
|
||||
|
||||
if ((crypt_msg != NULL) && (result) && (decoded_size == message_size))
|
||||
{
|
||||
// Verify cms message with our stored message
|
||||
if (memcmp(decoded, message, message_size) == 0)
|
||||
err = MZ_OK;
|
||||
}
|
||||
|
||||
if (crypt_msg != NULL)
|
||||
CryptMsgClose(crypt_msg);
|
||||
|
||||
if (decoded != NULL)
|
||||
MZ_FREE(decoded);
|
||||
|
||||
return err;
|
||||
}
|
||||
@@ -0,0 +1,329 @@
|
||||
/* mz_os.c -- System functions
|
||||
Version 2.7.3, November 4, 2018
|
||||
part of the MiniZip project
|
||||
|
||||
Copyright (C) 2010-2018 Nathan Moinvaziri
|
||||
https://github.com/nmoinvaz/minizip
|
||||
Copyright (C) 1998-2010 Gilles Vollant
|
||||
https://www.winimage.com/zLibDll/minizip.html
|
||||
|
||||
This program is distributed under the terms of the same license as zlib.
|
||||
See the accompanying LICENSE file for the full text of the license.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#include "mz.h"
|
||||
#include "mz_crypt.h"
|
||||
#include "mz_os.h"
|
||||
#include "mz_strm.h"
|
||||
#include "mz_strm_os.h"
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
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 = (int32_t)strlen(path);
|
||||
|
||||
if (path_len == 0)
|
||||
{
|
||||
strncpy(path, join, max_path - 1);
|
||||
path[max_path - 1] = 0;
|
||||
}
|
||||
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_compare_wc(const char *path, const char *wildcard, uint8_t ignore_case)
|
||||
{
|
||||
while (*path != 0)
|
||||
{
|
||||
switch (*wildcard)
|
||||
{
|
||||
case '*':
|
||||
|
||||
if (*(wildcard + 1) == 0)
|
||||
return MZ_OK;
|
||||
|
||||
while (*path != 0)
|
||||
{
|
||||
if (mz_path_compare_wc(path, (wildcard + 1), ignore_case) == MZ_OK)
|
||||
return MZ_OK;
|
||||
|
||||
path += 1;
|
||||
}
|
||||
|
||||
return MZ_EXIST_ERROR;
|
||||
|
||||
default:
|
||||
// Ignore differences in path slashes on platforms
|
||||
if ((*path == '\\' && *wildcard == '/') || (*path == '/' && *wildcard == '\\'))
|
||||
break;
|
||||
|
||||
if (ignore_case)
|
||||
{
|
||||
if (tolower(*path) != tolower(*wildcard))
|
||||
return MZ_EXIST_ERROR;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (*path != *wildcard)
|
||||
return MZ_EXIST_ERROR;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
path += 1;
|
||||
wildcard += 1;
|
||||
}
|
||||
|
||||
if ((*wildcard != 0) && (*wildcard != '*'))
|
||||
return MZ_EXIST_ERROR;
|
||||
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_path_resolve(const char *path, char *output, int32_t max_output)
|
||||
{
|
||||
const char *source = path;
|
||||
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))
|
||||
{
|
||||
// Skip double paths
|
||||
if ((*check == '\\') || (*check == '/'))
|
||||
{
|
||||
source += 1;
|
||||
continue;
|
||||
}
|
||||
if ((*check != 0) && (*check == '.'))
|
||||
{
|
||||
check += 1;
|
||||
|
||||
// Remove current directory . if at end of string
|
||||
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 string
|
||||
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(char *path)
|
||||
{
|
||||
char *path_ptr = NULL;
|
||||
|
||||
if (path == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
path_ptr = 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_dir_make(const char *path)
|
||||
{
|
||||
int32_t err = MZ_OK;
|
||||
int16_t len = 0;
|
||||
char *current_dir = NULL;
|
||||
char *match = NULL;
|
||||
char hold = 0;
|
||||
|
||||
|
||||
len = (int16_t)strlen(path);
|
||||
if (len <= 0)
|
||||
return 0;
|
||||
|
||||
current_dir = (char *)MZ_ALLOC((uint16_t)len + 1);
|
||||
if (current_dir == NULL)
|
||||
return MZ_MEM_ERROR;
|
||||
|
||||
strcpy(current_dir, path);
|
||||
|
||||
if (current_dir[len - 1] == '/')
|
||||
current_dir[len - 1] = 0;
|
||||
|
||||
err = mz_os_make_dir(current_dir);
|
||||
if (err != MZ_OK)
|
||||
{
|
||||
match = current_dir + 1;
|
||||
while (1)
|
||||
{
|
||||
while (*match != 0 && *match != '\\' && *match != '/')
|
||||
match += 1;
|
||||
hold = *match;
|
||||
*match = 0;
|
||||
|
||||
err = mz_os_make_dir(current_dir);
|
||||
if (err != MZ_OK)
|
||||
break;
|
||||
if (hold == 0)
|
||||
break;
|
||||
|
||||
*match = hold;
|
||||
match += 1;
|
||||
}
|
||||
}
|
||||
|
||||
MZ_FREE(current_dir);
|
||||
return err;
|
||||
}
|
||||
|
||||
int32_t mz_file_get_crc(const char *path, uint32_t *result_crc)
|
||||
{
|
||||
void *stream = NULL;
|
||||
uint32_t crc32 = 0;
|
||||
int32_t read = 0;
|
||||
int32_t err = MZ_OK;
|
||||
uint8_t buf[16384];
|
||||
|
||||
mz_stream_os_create(&stream);
|
||||
|
||||
err = mz_stream_os_open(stream, path, MZ_OPEN_MODE_READ);
|
||||
|
||||
if (err == MZ_OK)
|
||||
{
|
||||
do
|
||||
{
|
||||
read = mz_stream_os_read(stream, buf, sizeof(buf));
|
||||
|
||||
if (read < 0)
|
||||
{
|
||||
err = read;
|
||||
break;
|
||||
}
|
||||
|
||||
crc32 = mz_crypt_crc32_update(crc32, buf, read);
|
||||
}
|
||||
while ((err == MZ_OK) && (read > 0));
|
||||
|
||||
mz_stream_os_close(stream);
|
||||
}
|
||||
|
||||
*result_crc = crc32;
|
||||
|
||||
mz_stream_os_delete(&stream);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
||||
@@ -0,0 +1,146 @@
|
||||
/* mz_os.h -- System functions
|
||||
Version 2.7.3, November 4, 2018
|
||||
part of the MiniZip project
|
||||
|
||||
Copyright (C) 2010-2018 Nathan Moinvaziri
|
||||
https://github.com/nmoinvaz/minizip
|
||||
|
||||
This program is distributed under the terms of the same license as zlib.
|
||||
See the accompanying LICENSE file for the full text of the license.
|
||||
*/
|
||||
|
||||
#ifndef MZ_OS_H
|
||||
#define MZ_OS_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <time.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#if defined(__APPLE__)
|
||||
#define MZ_VERSION_MADEBY_HOST_SYSTEM (MZ_HOST_SYSTEM_OSX_DARWIN)
|
||||
#elif defined(unix)
|
||||
#define MZ_VERSION_MADEBY_HOST_SYSTEM (MZ_HOST_SYSTEM_UNIX)
|
||||
#elif defined(_WIN32)
|
||||
#define MZ_VERSION_MADEBY_HOST_SYSTEM (MZ_HOST_SYSTEM_WINDOWS_NTFS)
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_LZMA
|
||||
#define MZ_VERSION_MADEBY_ZIP_VERSION (63)
|
||||
#elif HAVE_AES
|
||||
#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))
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#if defined(_WIN32)
|
||||
struct dirent {
|
||||
char d_name[256];
|
||||
};
|
||||
typedef void* DIR;
|
||||
#else
|
||||
#include <dirent.h>
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
// Shared functions
|
||||
|
||||
int32_t mz_path_combine(char *path, const char *join, int32_t max_path);
|
||||
// Combines two paths
|
||||
|
||||
int32_t mz_path_compare_wc(const char *path, const char *wildcard, uint8_t ignore_case);
|
||||
// Compare two paths with wildcard
|
||||
|
||||
int32_t mz_path_resolve(const char *path, char *target, int32_t max_target);
|
||||
// Resolves path
|
||||
|
||||
int32_t mz_path_remove_filename(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_dir_make(const char *path);
|
||||
// Creates a directory recursively
|
||||
|
||||
int32_t mz_file_get_crc(const char *path, uint32_t *result_crc);
|
||||
// Gets the crc32 hash of a file
|
||||
|
||||
/***************************************************************************/
|
||||
// Platform specific functions
|
||||
|
||||
wchar_t *mz_os_unicode_string_create(const char *string, int32_t encoding);
|
||||
// Create unicode string from a utf8 string
|
||||
|
||||
void mz_os_unicode_string_delete(wchar_t **string);
|
||||
// Delete a unicode string that was created
|
||||
|
||||
uint8_t *mz_os_utf8_string_create(const char *string, int32_t encoding);
|
||||
// Create a utf8 string from a string with another encoding
|
||||
|
||||
void mz_os_utf8_string_delete(uint8_t **string);
|
||||
// Delete a utf8 string that was created
|
||||
|
||||
int32_t mz_os_rand(uint8_t *buf, int32_t size);
|
||||
// Random number generator (not cryptographically secure)
|
||||
|
||||
int32_t mz_os_rename(const char *source_path, const char *target_path);
|
||||
// Rename a file
|
||||
|
||||
int32_t mz_os_delete(const char *path);
|
||||
// Delete an existing file
|
||||
|
||||
int32_t mz_os_file_exists(const char *path);
|
||||
// Check to see if a file exists
|
||||
|
||||
int64_t mz_os_get_file_size(const char *path);
|
||||
// Gets the length of a file
|
||||
|
||||
int32_t mz_os_get_file_date(const char *path, time_t *modified_date, time_t *accessed_date, time_t *creation_date);
|
||||
// Gets a file's modified, access, and creation dates if supported
|
||||
|
||||
int32_t mz_os_set_file_date(const char *path, time_t modified_date, time_t accessed_date, time_t creation_date);
|
||||
// Sets a file's modified, access, and creation dates if supported
|
||||
|
||||
int32_t mz_os_get_file_attribs(const char *path, uint32_t *attributes);
|
||||
// Gets a file's attributes
|
||||
|
||||
int32_t mz_os_set_file_attribs(const char *path, uint32_t attributes);
|
||||
// Sets a file's attributes
|
||||
|
||||
int32_t mz_os_make_dir(const char *path);
|
||||
// Recursively creates a directory
|
||||
|
||||
DIR* mz_os_open_dir(const char *path);
|
||||
// Opens a directory for listing
|
||||
struct
|
||||
dirent* mz_os_read_dir(DIR *dir);
|
||||
// Reads a directory listing entry
|
||||
|
||||
int32_t mz_os_close_dir(DIR *dir);
|
||||
// Closes a directory that has been opened for listing
|
||||
|
||||
int32_t mz_os_is_dir(const char *path);
|
||||
// Checks to see if path is a directory
|
||||
|
||||
uint64_t mz_os_ms_time(void);
|
||||
// Gets the time in milliseconds
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
+293
@@ -0,0 +1,293 @@
|
||||
/* mz_os_posix.c -- System functions for posix
|
||||
Version 2.7.3, November 4, 2018
|
||||
part of the MiniZip project
|
||||
|
||||
Copyright (C) 2010-2018 Nathan Moinvaziri
|
||||
https://github.com/nmoinvaz/minizip
|
||||
|
||||
This program is distributed under the terms of the same license as zlib.
|
||||
See the accompanying LICENSE file for the full text of the license.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <iconv.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#if defined(__APPLE__) || defined(unix)
|
||||
# include <utime.h>
|
||||
# include <unistd.h>
|
||||
#endif
|
||||
#if defined(__APPLE__)
|
||||
# include <mach/clock.h>
|
||||
# include <mach/mach.h>
|
||||
#endif
|
||||
|
||||
#include "mz.h"
|
||||
#include "mz_strm.h"
|
||||
#include "mz_os.h"
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
uint8_t *mz_os_utf8_string_create(const char *string, int32_t encoding)
|
||||
{
|
||||
iconv_t cd;
|
||||
const char *from_encoding = NULL;
|
||||
int32_t result = 0;
|
||||
size_t string_length = 0;
|
||||
size_t string_utf8_size = 0;
|
||||
uint8_t *string_utf8 = NULL;
|
||||
uint8_t *string_utf8_ptr = NULL;
|
||||
|
||||
if (string == NULL)
|
||||
return NULL;
|
||||
|
||||
if (encoding == MZ_ENCODING_CODEPAGE_437)
|
||||
from_encoding = "CP437";
|
||||
else if (encoding == MZ_ENCODING_CODEPAGE_932)
|
||||
from_encoding = "CP932";
|
||||
else if (encoding == MZ_ENCODING_CODEPAGE_936)
|
||||
from_encoding = "CP936";
|
||||
else if (encoding == MZ_ENCODING_CODEPAGE_950)
|
||||
from_encoding = "CP950";
|
||||
else if (encoding == MZ_ENCODING_UTF8)
|
||||
from_encoding = "UTF-8";
|
||||
else
|
||||
return NULL;
|
||||
|
||||
cd = iconv_open("UTF-8", from_encoding);
|
||||
if (cd == (iconv_t)-1)
|
||||
return NULL;
|
||||
|
||||
string_length = strlen(string);
|
||||
string_utf8_size = string_length * 2;
|
||||
string_utf8 = (uint8_t *)MZ_ALLOC((int32_t)(string_utf8_size + 1));
|
||||
string_utf8_ptr = string_utf8;
|
||||
|
||||
if (string_utf8)
|
||||
{
|
||||
memset(string_utf8, 0, string_utf8_size + 1);
|
||||
|
||||
result = iconv(cd, (char **)&string, &string_length,
|
||||
(char **)&string_utf8_ptr, &string_utf8_size);
|
||||
|
||||
iconv_close(cd);
|
||||
}
|
||||
|
||||
if (result == -1)
|
||||
{
|
||||
MZ_FREE(string_utf8);
|
||||
string_utf8 = NULL;
|
||||
}
|
||||
|
||||
return string_utf8;
|
||||
}
|
||||
|
||||
void mz_os_utf8_string_delete(uint8_t **string)
|
||||
{
|
||||
if (string != NULL)
|
||||
{
|
||||
MZ_FREE(*string);
|
||||
*string = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t mz_os_rand(uint8_t *buf, int32_t size)
|
||||
{
|
||||
static unsigned calls = 0;
|
||||
int32_t i = 0;
|
||||
|
||||
// Ensure different random header each time
|
||||
if (++calls == 1)
|
||||
{
|
||||
#define PI_SEED 3141592654UL
|
||||
srand((unsigned)(time(NULL) ^ PI_SEED));
|
||||
}
|
||||
|
||||
while (i < size)
|
||||
buf[i++] = (rand() >> 7) & 0xff;
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
int32_t mz_os_rename(const char *source_path, const char *target_path)
|
||||
{
|
||||
if (rename(source_path, target_path) == -1)
|
||||
return MZ_EXIST_ERROR;
|
||||
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_os_delete(const char *path)
|
||||
{
|
||||
if (unlink(path) == -1)
|
||||
return MZ_EXIST_ERROR;
|
||||
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_os_file_exists(const char *path)
|
||||
{
|
||||
struct stat stat_info;
|
||||
|
||||
memset(&stat_info, 0, sizeof(stat_info));
|
||||
if (stat(path, &stat_info) == 0)
|
||||
return MZ_OK;
|
||||
|
||||
return MZ_EXIST_ERROR;
|
||||
}
|
||||
|
||||
int64_t mz_os_get_file_size(const char *path)
|
||||
{
|
||||
struct stat stat_info;
|
||||
|
||||
memset(&stat_info, 0, sizeof(stat_info));
|
||||
if (stat(path, &stat_info) == 0)
|
||||
return stat_info.st_size;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t mz_os_get_file_date(const char *path, time_t *modified_date, time_t *accessed_date, time_t *creation_date)
|
||||
{
|
||||
struct stat stat_info;
|
||||
char *name = NULL;
|
||||
size_t len = 0;
|
||||
int32_t err = MZ_INTERNAL_ERROR;
|
||||
|
||||
memset(&stat_info, 0, sizeof(stat_info));
|
||||
|
||||
if (strcmp(path, "-") != 0)
|
||||
{
|
||||
// Not all systems allow stat'ing a file with / appended
|
||||
len = strlen(path);
|
||||
name = (char *)malloc(len + 1);
|
||||
strncpy(name, path, len);
|
||||
name[len] = 0;
|
||||
if (name[len - 1] == '/')
|
||||
name[len - 1] = 0;
|
||||
|
||||
if (stat(name, &stat_info) == 0)
|
||||
{
|
||||
if (modified_date != NULL)
|
||||
*modified_date = stat_info.st_mtime;
|
||||
if (accessed_date != NULL)
|
||||
*accessed_date = stat_info.st_atime;
|
||||
// Creation date not supported
|
||||
if (creation_date != NULL)
|
||||
*creation_date = 0;
|
||||
|
||||
err = MZ_OK;
|
||||
}
|
||||
|
||||
free(name);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
int32_t mz_os_set_file_date(const char *path, time_t modified_date, time_t accessed_date, time_t creation_date)
|
||||
{
|
||||
struct utimbuf ut;
|
||||
|
||||
ut.actime = accessed_date;
|
||||
ut.modtime = modified_date;
|
||||
// Creation date not supported
|
||||
(void)creation_date;
|
||||
|
||||
if (utime(path, &ut) != 0)
|
||||
return MZ_INTERNAL_ERROR;
|
||||
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_os_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_os_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_os_make_dir(const char *path)
|
||||
{
|
||||
int32_t err = 0;
|
||||
|
||||
err = mkdir(path, 0755);
|
||||
|
||||
if (err != 0 && errno != EEXIST)
|
||||
return MZ_INTERNAL_ERROR;
|
||||
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
DIR* mz_os_open_dir(const char *path)
|
||||
{
|
||||
return opendir(path);
|
||||
}
|
||||
|
||||
struct dirent* mz_os_read_dir(DIR *dir)
|
||||
{
|
||||
if (dir == NULL)
|
||||
return NULL;
|
||||
return readdir(dir);
|
||||
}
|
||||
|
||||
int32_t mz_os_close_dir(DIR *dir)
|
||||
{
|
||||
if (dir == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
if (closedir(dir) == -1)
|
||||
return MZ_INTERNAL_ERROR;
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_os_is_dir(const char *path)
|
||||
{
|
||||
struct stat path_stat;
|
||||
stat(path, &path_stat);
|
||||
if (S_ISDIR(path_stat.st_mode))
|
||||
return MZ_OK;
|
||||
return MZ_EXIST_ERROR;
|
||||
}
|
||||
|
||||
uint64_t mz_os_ms_time(void)
|
||||
{
|
||||
struct timespec ts;
|
||||
|
||||
#if defined(__APPLE__)
|
||||
clock_serv_t cclock;
|
||||
mach_timespec_t mts;
|
||||
|
||||
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
|
||||
clock_get_time(cclock, &mts);
|
||||
mach_port_deallocate(mach_task_self(), cclock);
|
||||
|
||||
ts.tv_sec = mts.tv_sec;
|
||||
ts.tv_nsec = mts.tv_nsec;
|
||||
#else
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
#endif
|
||||
|
||||
return ((uint64_t)ts.tv_sec * 1000) + ((uint64_t)ts.tv_nsec / 1000000);
|
||||
}
|
||||
+482
@@ -0,0 +1,482 @@
|
||||
/* mz_os_win32.c -- System functions for Windows
|
||||
Version 2.7.3, November 4, 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 <time.h>
|
||||
#include <direct.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
#include "mz.h"
|
||||
|
||||
#include "mz_os.h"
|
||||
#include "mz_strm_os.h"
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#if defined(WINAPI_FAMILY_PARTITION) && (!(defined(MZ_WINRT_API)))
|
||||
# if !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
|
||||
# define MZ_WINRT_API 1
|
||||
# endif
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct DIR_int_s {
|
||||
void *find_handle;
|
||||
WIN32_FIND_DATAW find_data;
|
||||
struct dirent entry;
|
||||
uint8_t end;
|
||||
} DIR_int;
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
wchar_t *mz_os_unicode_string_create(const char *string, int32_t encoding)
|
||||
{
|
||||
wchar_t *string_wide = NULL;
|
||||
uint32_t string_wide_size = 0;
|
||||
|
||||
string_wide_size = MultiByteToWideChar(encoding, 0, string, -1, NULL, 0);
|
||||
if (string_wide_size == 0)
|
||||
return NULL;
|
||||
string_wide = (wchar_t *)MZ_ALLOC((string_wide_size + 1) * sizeof(wchar_t));
|
||||
memset(string_wide, 0, sizeof(wchar_t) * (string_wide_size + 1));
|
||||
|
||||
MultiByteToWideChar(encoding, 0, string, -1, string_wide, string_wide_size);
|
||||
|
||||
return string_wide;
|
||||
}
|
||||
|
||||
void mz_os_unicode_string_delete(wchar_t **string)
|
||||
{
|
||||
if (string != NULL)
|
||||
{
|
||||
MZ_FREE(*string);
|
||||
*string = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t *mz_os_utf8_string_create(const char *string, int32_t encoding)
|
||||
{
|
||||
wchar_t *string_wide = NULL;
|
||||
uint8_t *string_utf8 = NULL;
|
||||
uint32_t string_utf8_size = 0;
|
||||
|
||||
string_wide = mz_os_unicode_string_create(string, encoding);
|
||||
if (string_wide)
|
||||
{
|
||||
string_utf8_size = WideCharToMultiByte(CP_UTF8, 0, string_wide, -1, NULL, 0, NULL, NULL);
|
||||
string_utf8 = (uint8_t *)MZ_ALLOC((string_utf8_size + 1) * sizeof(wchar_t));
|
||||
|
||||
if (string_utf8)
|
||||
{
|
||||
memset(string_utf8, 0, string_utf8_size + 1);
|
||||
WideCharToMultiByte(CP_UTF8, 0, string_wide, -1, string_utf8, string_utf8_size, NULL, NULL);
|
||||
}
|
||||
|
||||
mz_os_unicode_string_delete(&string_wide);
|
||||
}
|
||||
|
||||
return string_utf8;
|
||||
}
|
||||
|
||||
void mz_os_utf8_string_delete(uint8_t **string)
|
||||
{
|
||||
if (string != NULL)
|
||||
{
|
||||
MZ_FREE(*string);
|
||||
*string = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t mz_os_rand(uint8_t *buf, int32_t size)
|
||||
{
|
||||
unsigned __int64 pentium_tsc[1];
|
||||
int32_t len = 0;
|
||||
|
||||
for (len = 0; len < (int)size; len += 1)
|
||||
{
|
||||
if (len % 8 == 0)
|
||||
QueryPerformanceCounter((LARGE_INTEGER *)pentium_tsc);
|
||||
buf[len] = ((unsigned char*)pentium_tsc)[len % 8];
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
int32_t mz_os_rename(const char *source_path, const char *target_path)
|
||||
{
|
||||
wchar_t *source_path_wide = NULL;
|
||||
wchar_t *target_path_wide = NULL;
|
||||
int32_t result = 0;
|
||||
int32_t err = MZ_OK;
|
||||
|
||||
if (source_path == NULL || target_path == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
source_path_wide = mz_os_unicode_string_create(source_path, MZ_ENCODING_UTF8);
|
||||
if (source_path_wide == NULL)
|
||||
{
|
||||
err = MZ_PARAM_ERROR;
|
||||
}
|
||||
else
|
||||
{
|
||||
target_path_wide = mz_os_unicode_string_create(target_path, MZ_ENCODING_UTF8);
|
||||
if (target_path_wide == NULL)
|
||||
err = MZ_PARAM_ERROR;
|
||||
}
|
||||
|
||||
if (err == MZ_OK)
|
||||
{
|
||||
result = MoveFileW(source_path_wide, target_path_wide);
|
||||
if (result == 0)
|
||||
err = MZ_EXIST_ERROR;
|
||||
}
|
||||
|
||||
if (target_path_wide)
|
||||
mz_os_unicode_string_delete(&target_path_wide);
|
||||
if (source_path_wide)
|
||||
mz_os_unicode_string_delete(&source_path_wide);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
int32_t mz_os_delete(const char *path)
|
||||
{
|
||||
wchar_t *path_wide = NULL;
|
||||
int32_t result = 0;
|
||||
|
||||
if (path == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
path_wide = mz_os_unicode_string_create(path, MZ_ENCODING_UTF8);
|
||||
if (path_wide == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
result = DeleteFileW(path_wide);
|
||||
mz_os_unicode_string_delete(&path_wide);
|
||||
|
||||
if (result == 0)
|
||||
return MZ_EXIST_ERROR;
|
||||
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_os_file_exists(const char *path)
|
||||
{
|
||||
wchar_t *path_wide = NULL;
|
||||
DWORD attribs = 0;
|
||||
|
||||
if (path == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
path_wide = mz_os_unicode_string_create(path, MZ_ENCODING_UTF8);
|
||||
if (path_wide == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
attribs = GetFileAttributesW(path_wide);
|
||||
mz_os_unicode_string_delete(&path_wide);
|
||||
|
||||
if (attribs == 0xFFFFFFFF)
|
||||
return MZ_EXIST_ERROR;
|
||||
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int64_t mz_os_get_file_size(const char *path)
|
||||
{
|
||||
HANDLE handle = NULL;
|
||||
LARGE_INTEGER large_size;
|
||||
wchar_t *path_wide = NULL;
|
||||
|
||||
if (path == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
path_wide = mz_os_unicode_string_create(path, MZ_ENCODING_UTF8);
|
||||
if (path_wide == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
#ifdef MZ_WINRT_API
|
||||
handle = CreateFile2W(path_wide, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||
#else
|
||||
handle = CreateFileW(path_wide, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||
#endif
|
||||
mz_os_unicode_string_delete(&path_wide);
|
||||
|
||||
large_size.QuadPart = 0;
|
||||
|
||||
if (handle != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
GetFileSizeEx(handle, &large_size);
|
||||
CloseHandle(handle);
|
||||
}
|
||||
|
||||
return large_size.QuadPart;
|
||||
}
|
||||
|
||||
static void mz_os_file_to_unix_time(FILETIME file_time, time_t *unix_time)
|
||||
{
|
||||
uint64_t quad_file_time = 0;
|
||||
quad_file_time = file_time.dwLowDateTime;
|
||||
quad_file_time |= ((uint64_t)file_time.dwHighDateTime << 32);
|
||||
*unix_time = (time_t)((quad_file_time - 116444736000000000LL) / 10000000);
|
||||
}
|
||||
|
||||
static void mz_os_unix_to_file_time(time_t unix_time, FILETIME *file_time)
|
||||
{
|
||||
uint64_t quad_file_time = 0;
|
||||
quad_file_time = ((uint64_t)unix_time * 10000000) + 116444736000000000LL;
|
||||
file_time->dwHighDateTime = (quad_file_time >> 32);
|
||||
file_time->dwLowDateTime = (uint32_t)(quad_file_time);
|
||||
}
|
||||
|
||||
int32_t mz_os_get_file_date(const char *path, time_t *modified_date, time_t *accessed_date, time_t *creation_date)
|
||||
{
|
||||
WIN32_FIND_DATAW ff32;
|
||||
HANDLE handle = NULL;
|
||||
wchar_t *path_wide = NULL;
|
||||
int32_t err = MZ_INTERNAL_ERROR;
|
||||
|
||||
if (path == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
path_wide = mz_os_unicode_string_create(path, MZ_ENCODING_UTF8);
|
||||
if (path_wide == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
handle = FindFirstFileW(path_wide, &ff32);
|
||||
MZ_FREE(path_wide);
|
||||
|
||||
if (handle != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
if (modified_date != NULL)
|
||||
mz_os_file_to_unix_time(ff32.ftLastWriteTime, modified_date);
|
||||
if (accessed_date != NULL)
|
||||
mz_os_file_to_unix_time(ff32.ftLastAccessTime, accessed_date);
|
||||
if (creation_date != NULL)
|
||||
mz_os_file_to_unix_time(ff32.ftCreationTime, creation_date);
|
||||
|
||||
FindClose(handle);
|
||||
err = MZ_OK;
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
int32_t mz_os_set_file_date(const char *path, time_t modified_date, time_t accessed_date, time_t creation_date)
|
||||
{
|
||||
HANDLE handle = NULL;
|
||||
FILETIME ftm_creation, ftm_accessed, ftm_modified;
|
||||
wchar_t *path_wide = NULL;
|
||||
int32_t err = MZ_OK;
|
||||
|
||||
if (path == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
path_wide = mz_os_unicode_string_create(path, MZ_ENCODING_UTF8);
|
||||
if (path_wide == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
#ifdef MZ_WINRT_API
|
||||
handle = CreateFile2W(path_wide, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
|
||||
#else
|
||||
handle = CreateFileW(path_wide, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
|
||||
#endif
|
||||
mz_os_unicode_string_delete(&path_wide);
|
||||
|
||||
if (handle != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
GetFileTime(handle, &ftm_creation, &ftm_accessed, &ftm_modified);
|
||||
|
||||
if (modified_date != 0)
|
||||
mz_os_unix_to_file_time(modified_date, &ftm_modified);
|
||||
if (accessed_date != 0)
|
||||
mz_os_unix_to_file_time(accessed_date, &ftm_accessed);
|
||||
if (creation_date != 0)
|
||||
mz_os_unix_to_file_time(creation_date, &ftm_creation);
|
||||
|
||||
if (SetFileTime(handle, &ftm_creation, &ftm_accessed, &ftm_modified) == 0)
|
||||
err = MZ_INTERNAL_ERROR;
|
||||
|
||||
CloseHandle(handle);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
int32_t mz_os_get_file_attribs(const char *path, uint32_t *attributes)
|
||||
{
|
||||
wchar_t *path_wide = NULL;
|
||||
int32_t err = MZ_OK;
|
||||
|
||||
if (path == NULL || attributes == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
path_wide = mz_os_unicode_string_create(path, MZ_ENCODING_UTF8);
|
||||
if (path_wide == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
*attributes = GetFileAttributesW(path_wide);
|
||||
mz_os_unicode_string_delete(&path_wide);
|
||||
|
||||
if (*attributes == INVALID_FILE_ATTRIBUTES)
|
||||
err = MZ_INTERNAL_ERROR;
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
int32_t mz_os_set_file_attribs(const char *path, uint32_t attributes)
|
||||
{
|
||||
wchar_t *path_wide = NULL;
|
||||
int32_t err = MZ_OK;
|
||||
|
||||
if (path == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
path_wide = mz_os_unicode_string_create(path, MZ_ENCODING_UTF8);
|
||||
if (path_wide == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
if (SetFileAttributesW(path_wide, attributes) == 0)
|
||||
err = MZ_INTERNAL_ERROR;
|
||||
mz_os_unicode_string_delete(&path_wide);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
int32_t mz_os_make_dir(const char *path)
|
||||
{
|
||||
wchar_t *path_wide = NULL;
|
||||
int32_t err = 0;
|
||||
|
||||
if (path == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
path_wide = mz_os_unicode_string_create(path, MZ_ENCODING_UTF8);
|
||||
if (path_wide == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
err = _wmkdir(path_wide);
|
||||
mz_os_unicode_string_delete(&path_wide);
|
||||
|
||||
if (err != 0 && errno != EEXIST)
|
||||
return MZ_INTERNAL_ERROR;
|
||||
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
DIR *mz_os_open_dir(const char *path)
|
||||
{
|
||||
WIN32_FIND_DATAW find_data;
|
||||
DIR_int *dir_int = NULL;
|
||||
wchar_t *path_wide = NULL;
|
||||
char fixed_path[320];
|
||||
void *handle = NULL;
|
||||
|
||||
|
||||
if (path == NULL)
|
||||
return NULL;
|
||||
|
||||
fixed_path[0] = 0;
|
||||
mz_path_combine(fixed_path, path, sizeof(fixed_path));
|
||||
mz_path_combine(fixed_path, "*", sizeof(fixed_path));
|
||||
|
||||
path_wide = mz_os_unicode_string_create(fixed_path, MZ_ENCODING_UTF8);
|
||||
if (path_wide == NULL)
|
||||
return NULL;
|
||||
|
||||
handle = FindFirstFileW(path_wide, &find_data);
|
||||
mz_os_unicode_string_delete(&path_wide);
|
||||
|
||||
if (handle == INVALID_HANDLE_VALUE)
|
||||
return NULL;
|
||||
|
||||
dir_int = (DIR_int *)MZ_ALLOC(sizeof(DIR_int));
|
||||
dir_int->find_handle = handle;
|
||||
dir_int->end = 0;
|
||||
|
||||
memcpy(&dir_int->find_data, &find_data, sizeof(dir_int->find_data));
|
||||
|
||||
return (DIR *)dir_int;
|
||||
}
|
||||
|
||||
struct dirent* mz_os_read_dir(DIR *dir)
|
||||
{
|
||||
DIR_int *dir_int;
|
||||
|
||||
if (dir == NULL)
|
||||
return NULL;
|
||||
|
||||
dir_int = (DIR_int *)dir;
|
||||
if (dir_int->end)
|
||||
return NULL;
|
||||
|
||||
WideCharToMultiByte(CP_UTF8, 0, dir_int->find_data.cFileName, -1,
|
||||
dir_int->entry.d_name, sizeof(dir_int->entry.d_name), NULL, NULL);
|
||||
|
||||
if (FindNextFileW(dir_int->find_handle, &dir_int->find_data) == 0)
|
||||
{
|
||||
if (GetLastError() != ERROR_NO_MORE_FILES)
|
||||
return NULL;
|
||||
|
||||
dir_int->end = 1;
|
||||
}
|
||||
|
||||
return &dir_int->entry;
|
||||
}
|
||||
|
||||
int32_t mz_os_close_dir(DIR *dir)
|
||||
{
|
||||
DIR_int *dir_int;
|
||||
|
||||
if (dir == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
dir_int = (DIR_int *)dir;
|
||||
if (dir_int->find_handle != INVALID_HANDLE_VALUE)
|
||||
FindClose(dir_int->find_handle);
|
||||
MZ_FREE(dir_int);
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_os_is_dir(const char *path)
|
||||
{
|
||||
wchar_t *path_wide = NULL;
|
||||
uint32_t attribs = 0;
|
||||
|
||||
if (path == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
path_wide = mz_os_unicode_string_create(path, MZ_ENCODING_UTF8);
|
||||
if (path_wide == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
attribs = GetFileAttributesW(path_wide);
|
||||
mz_os_unicode_string_delete(&path_wide);
|
||||
|
||||
if (attribs != 0xFFFFFFFF)
|
||||
{
|
||||
if (attribs & FILE_ATTRIBUTE_DIRECTORY)
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
return MZ_EXIST_ERROR;
|
||||
}
|
||||
|
||||
uint64_t mz_os_ms_time(void)
|
||||
{
|
||||
SYSTEMTIME system_time;
|
||||
FILETIME file_time;
|
||||
uint64_t quad_file_time = 0;
|
||||
|
||||
GetSystemTime(&system_time);
|
||||
SystemTimeToFileTime(&system_time, &file_time);
|
||||
|
||||
quad_file_time = file_time.dwLowDateTime;
|
||||
quad_file_time |= ((uint64_t)file_time.dwHighDateTime << 32);
|
||||
|
||||
return quad_file_time / 10000 - 11644473600000LL;
|
||||
}
|
||||
+51
-164
@@ -1,5 +1,5 @@
|
||||
/* mz_strm.c -- Stream interface
|
||||
Version 2.2.7, January 30th, 2018
|
||||
Version 2.7.3, November 4, 2018
|
||||
part of the MiniZip project
|
||||
|
||||
Copyright (C) 2010-2018 Nathan Moinvaziri
|
||||
@@ -13,8 +13,6 @@
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <time.h>
|
||||
|
||||
#include "mz.h"
|
||||
#include "mz_strm.h"
|
||||
|
||||
@@ -102,6 +100,11 @@ int32_t mz_stream_read_uint32(void *stream, uint32_t *value)
|
||||
return err;
|
||||
}
|
||||
|
||||
int32_t mz_stream_read_int64(void *stream, int64_t *value)
|
||||
{
|
||||
return mz_stream_read_value(stream, (uint64_t *)value, sizeof(uint64_t));
|
||||
}
|
||||
|
||||
int32_t mz_stream_read_uint64(void *stream, uint64_t *value)
|
||||
{
|
||||
return mz_stream_read_value(stream, value, sizeof(uint64_t));
|
||||
@@ -158,27 +161,50 @@ int32_t mz_stream_write_uint32(void *stream, uint32_t value)
|
||||
return mz_stream_write_value(stream, value, sizeof(uint32_t));
|
||||
}
|
||||
|
||||
int32_t mz_stream_write_int64(void *stream, int64_t value)
|
||||
{
|
||||
return mz_stream_write_value(stream, (uint64_t)value, sizeof(uint64_t));
|
||||
}
|
||||
|
||||
int32_t mz_stream_write_uint64(void *stream, uint64_t value)
|
||||
{
|
||||
return mz_stream_write_value(stream, value, sizeof(uint64_t));
|
||||
}
|
||||
|
||||
int32_t mz_stream_write_chars(void *stream, const char *value, uint8_t null_terminate)
|
||||
{
|
||||
int32_t len = (int32_t)strlen(value);
|
||||
if (null_terminate)
|
||||
len += 1;
|
||||
return mz_stream_write(stream, value, len);
|
||||
}
|
||||
|
||||
int32_t mz_stream_copy(void *target, void *source, int32_t len)
|
||||
{
|
||||
uint8_t buf[INT16_MAX];
|
||||
return mz_stream_copy_stream(target, NULL, source, NULL, len);
|
||||
}
|
||||
|
||||
int32_t mz_stream_copy_stream(void *target, mz_stream_write_cb write_cb, void *source, mz_stream_read_cb read_cb, int32_t len)
|
||||
{
|
||||
uint8_t buf[16384];
|
||||
int32_t bytes_to_copy = 0;
|
||||
int32_t read = 0;
|
||||
int32_t written = 0;
|
||||
|
||||
if (write_cb == NULL)
|
||||
write_cb = mz_stream_write;
|
||||
if (read_cb == NULL)
|
||||
read_cb = mz_stream_read;
|
||||
|
||||
while (len > 0)
|
||||
{
|
||||
bytes_to_copy = len;
|
||||
if (bytes_to_copy > 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)
|
||||
read = read_cb(source, buf, bytes_to_copy);
|
||||
if (read <= 0)
|
||||
return MZ_STREAM_ERROR;
|
||||
written = mz_stream_write(target, buf, read);
|
||||
written = write_cb(target, buf, read);
|
||||
if (written != read)
|
||||
return MZ_STREAM_ERROR;
|
||||
len -= read;
|
||||
@@ -232,6 +258,14 @@ int32_t mz_stream_set_base(void *stream, void *base)
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
void* mz_stream_get_interface(void *stream)
|
||||
{
|
||||
mz_stream *strm = (mz_stream *)stream;
|
||||
if (strm == NULL || strm->vtbl == NULL)
|
||||
return NULL;
|
||||
return (void *)strm->vtbl;
|
||||
}
|
||||
|
||||
int32_t mz_stream_get_prop_int64(void *stream, int32_t prop, int64_t *value)
|
||||
{
|
||||
mz_stream *strm = (mz_stream *)stream;
|
||||
@@ -270,159 +304,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 +315,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 +371,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 +411,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 +432,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 +451,6 @@ void mz_stream_raw_delete(void **stream)
|
||||
return;
|
||||
raw = (mz_stream_raw *)*stream;
|
||||
if (raw != NULL)
|
||||
free(raw);
|
||||
MZ_FREE(raw);
|
||||
*stream = NULL;
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/* mz_strm.h -- Stream interface
|
||||
Version 2.2.7, January 30th, 2018
|
||||
Version 2.7.3, November 4, 2018
|
||||
part of the MiniZip project
|
||||
|
||||
Copyright (C) 2010-2018 Nathan Moinvaziri
|
||||
@@ -9,8 +9,8 @@
|
||||
See the accompanying LICENSE file for the full text of the license.
|
||||
*/
|
||||
|
||||
#ifndef _MZ_STREAM_H
|
||||
#define _MZ_STREAM_H
|
||||
#ifndef MZ_STREAM_H
|
||||
#define MZ_STREAM_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
@@ -29,6 +29,7 @@ extern "C" {
|
||||
#define MZ_STREAM_PROP_DISK_SIZE (7)
|
||||
#define MZ_STREAM_PROP_DISK_NUMBER (8)
|
||||
#define MZ_STREAM_PROP_COMPRESS_LEVEL (9)
|
||||
#define MZ_STREAM_PROP_COMPRESS_ALGORITHM (10)
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
@@ -78,19 +79,24 @@ int32_t mz_stream_read(void *stream, void *buf, int32_t size);
|
||||
int32_t mz_stream_read_uint8(void *stream, uint8_t *value);
|
||||
int32_t mz_stream_read_uint16(void *stream, uint16_t *value);
|
||||
int32_t mz_stream_read_uint32(void *stream, uint32_t *value);
|
||||
int32_t mz_stream_read_int64(void *stream, int64_t *value);
|
||||
int32_t mz_stream_read_uint64(void *stream, uint64_t *value);
|
||||
int32_t mz_stream_write(void *stream, const void *buf, int32_t size);
|
||||
int32_t mz_stream_write_uint8(void *stream, uint8_t value);
|
||||
int32_t mz_stream_write_uint16(void *stream, uint16_t value);
|
||||
int32_t mz_stream_write_uint32(void *stream, uint32_t value);
|
||||
int32_t mz_stream_write_int64(void *stream, int64_t value);
|
||||
int32_t mz_stream_write_uint64(void *stream, uint64_t value);
|
||||
int32_t mz_stream_write_chars(void *stream, const char *value, uint8_t null_terminate);
|
||||
int32_t mz_stream_copy(void *target, void *source, int32_t len);
|
||||
int32_t mz_stream_copy_stream(void *target, mz_stream_write_cb write_cb, void *source, mz_stream_read_cb read_cb, int32_t len);
|
||||
int64_t mz_stream_tell(void *stream);
|
||||
int32_t mz_stream_seek(void *stream, int64_t offset, int32_t origin);
|
||||
int32_t mz_stream_close(void *stream);
|
||||
int32_t mz_stream_error(void *stream);
|
||||
|
||||
int32_t mz_stream_set_base(void *stream, void *base);
|
||||
void* mz_stream_get_interface(void *stream);
|
||||
int32_t mz_stream_get_prop_int64(void *stream, int32_t prop, int64_t *value);
|
||||
int32_t mz_stream_set_prop_int64(void *stream, int32_t prop, int64_t value);
|
||||
|
||||
@@ -99,29 +105,6 @@ 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_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_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);
|
||||
@@ -1,5 +1,5 @@
|
||||
/* mz_strm_buf.c -- Stream for buffering reads/writes
|
||||
Version 2.2.7, January 30th, 2018
|
||||
Version 2.7.3, November 4, 2018
|
||||
part of the MiniZip project
|
||||
|
||||
This version of ioapi is designed to buffer IO.
|
||||
@@ -11,11 +11,11 @@
|
||||
See the accompanying LICENSE file for the full text of the license.
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "mz.h"
|
||||
#include "mz_strm.h"
|
||||
@@ -31,7 +31,7 @@
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
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
|
||||
};
|
||||
|
||||
/***************************************************************************/
|
||||
@@ -64,10 +66,24 @@ typedef struct mz_stream_buffered_s {
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
static int32_t mz_stream_buffered_reset(void *stream)
|
||||
{
|
||||
mz_stream_buffered *buffered = (mz_stream_buffered *)stream;
|
||||
|
||||
buffered->readbuf_len = 0;
|
||||
buffered->readbuf_pos = 0;
|
||||
buffered->writebuf_len = 0;
|
||||
buffered->writebuf_pos = 0;
|
||||
buffered->position = 0;
|
||||
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_stream_buffered_open(void *stream, const char *path, int32_t mode)
|
||||
{
|
||||
mz_stream_buffered *buffered = (mz_stream_buffered *)stream;
|
||||
mz_stream_buffered_print(buffered, "open [mode %d]\n", mode);
|
||||
mz_stream_buffered_reset(buffered);
|
||||
return mz_stream_open(buffered->stream.base, path, mode);
|
||||
}
|
||||
|
||||
@@ -89,15 +105,15 @@ static 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)
|
||||
return MZ_STREAM_ERROR;
|
||||
return MZ_WRITE_ERROR;
|
||||
|
||||
buffered->writebuf_misses += 1;
|
||||
|
||||
mz_stream_buffered_print(stream, "write flush [%d:%d len %d]\n",
|
||||
mz_stream_buffered_print(stream, "write flush [%d:%d len %d]\n",
|
||||
bytes_to_write, bytes_left_to_write, buffered->writebuf_len);
|
||||
|
||||
total_bytes_written += bytes_written;
|
||||
@@ -124,8 +140,10 @@ int32_t mz_stream_buffered_read(void *stream, void *buf, int32_t size)
|
||||
mz_stream_buffered_print(stream, "read [size %ld pos %lld]\n", size, buffered->position);
|
||||
|
||||
if (buffered->writebuf_len > 0)
|
||||
mz_stream_buffered_print(stream, "switch from write to read, not yet supported [%lld]\n",
|
||||
{
|
||||
mz_stream_buffered_print(stream, "switch from write to read, not yet supported [%lld]\n",
|
||||
buffered->position);
|
||||
}
|
||||
|
||||
while (bytes_left_to_read > 0)
|
||||
{
|
||||
@@ -137,7 +155,7 @@ int32_t mz_stream_buffered_read(void *stream, void *buf, int32_t size)
|
||||
buffered->readbuf_len = 0;
|
||||
}
|
||||
|
||||
bytes_to_read = sizeof(buffered->readbuf) - (buffered->readbuf_len - buffered->readbuf_pos);
|
||||
bytes_to_read = (int32_t)sizeof(buffered->readbuf) - (buffered->readbuf_len - buffered->readbuf_pos);
|
||||
bytes_read = mz_stream_read(buffered->stream.base, buffered->readbuf + buffered->readbuf_pos, bytes_to_read);
|
||||
if (bytes_read < 0)
|
||||
return bytes_read;
|
||||
@@ -146,7 +164,7 @@ int32_t mz_stream_buffered_read(void *stream, void *buf, int32_t size)
|
||||
buffered->readbuf_len += bytes_read;
|
||||
buffered->position += bytes_read;
|
||||
|
||||
mz_stream_buffered_print(stream, "filled [read %d/%d buf %d:%d pos %lld]\n",
|
||||
mz_stream_buffered_print(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)
|
||||
@@ -155,7 +173,7 @@ int32_t mz_stream_buffered_read(void *stream, void *buf, int32_t size)
|
||||
|
||||
if ((buffered->readbuf_len - buffered->readbuf_pos) > 0)
|
||||
{
|
||||
bytes_to_copy = (uint32_t)(buffered->readbuf_len - buffered->readbuf_pos);
|
||||
bytes_to_copy = buffered->readbuf_len - buffered->readbuf_pos;
|
||||
if (bytes_to_copy > bytes_left_to_read)
|
||||
bytes_to_copy = bytes_left_to_read;
|
||||
|
||||
@@ -167,7 +185,7 @@ int32_t mz_stream_buffered_read(void *stream, void *buf, int32_t size)
|
||||
buffered->readbuf_hits += 1;
|
||||
buffered->readbuf_pos += bytes_to_copy;
|
||||
|
||||
mz_stream_buffered_print(stream, "emptied [copied %d remaining %d buf %d:%d pos %lld]\n",
|
||||
mz_stream_buffered_print(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);
|
||||
}
|
||||
}
|
||||
@@ -183,9 +201,10 @@ int32_t mz_stream_buffered_write(void *stream, const void *buf, int32_t size)
|
||||
int32_t bytes_to_copy = 0;
|
||||
int32_t bytes_used = 0;
|
||||
int32_t bytes_flushed = 0;
|
||||
int32_t err = MZ_OK;
|
||||
|
||||
|
||||
mz_stream_buffered_print(stream, "write [size %ld len %d pos %lld]\n",
|
||||
mz_stream_buffered_print(stream, "write [size %ld len %d pos %lld]\n",
|
||||
size, buffered->writebuf_len, buffered->position);
|
||||
|
||||
if (buffered->readbuf_len > 0)
|
||||
@@ -198,8 +217,9 @@ int32_t mz_stream_buffered_write(void *stream, const void *buf, int32_t size)
|
||||
|
||||
mz_stream_buffered_print(stream, "switch from read to write [%lld]\n", buffered->position);
|
||||
|
||||
if (mz_stream_seek(buffered->stream.base, buffered->position, MZ_SEEK_SET) != MZ_OK)
|
||||
return MZ_STREAM_ERROR;
|
||||
err = mz_stream_seek(buffered->stream.base, buffered->position, MZ_SEEK_SET);
|
||||
if (err != MZ_OK)
|
||||
return err;
|
||||
}
|
||||
|
||||
while (bytes_left_to_write > 0)
|
||||
@@ -207,23 +227,24 @@ int32_t mz_stream_buffered_write(void *stream, const void *buf, int32_t size)
|
||||
bytes_used = buffered->writebuf_len;
|
||||
if (bytes_used > buffered->writebuf_pos)
|
||||
bytes_used = buffered->writebuf_pos;
|
||||
bytes_to_copy = (uint32_t)(sizeof(buffered->writebuf) - bytes_used);
|
||||
bytes_to_copy = (int32_t)sizeof(buffered->writebuf) - bytes_used;
|
||||
if (bytes_to_copy > bytes_left_to_write)
|
||||
bytes_to_copy = bytes_left_to_write;
|
||||
|
||||
if (bytes_to_copy == 0)
|
||||
{
|
||||
if (mz_stream_buffered_flush(stream, &bytes_flushed) != MZ_OK)
|
||||
return MZ_STREAM_ERROR;
|
||||
err = mz_stream_buffered_flush(stream, &bytes_flushed);
|
||||
if (err != MZ_OK)
|
||||
return err;
|
||||
if (bytes_flushed == 0)
|
||||
return 0;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
memcpy(buffered->writebuf + buffered->writebuf_pos, (char *)buf + (bytes_to_write - bytes_left_to_write), bytes_to_copy);
|
||||
memcpy(buffered->writebuf + buffered->writebuf_pos, (const char *)buf + (bytes_to_write - bytes_left_to_write), bytes_to_copy);
|
||||
|
||||
mz_stream_buffered_print(stream, "write copy [remaining %d write %d:%d len %d]\n",
|
||||
mz_stream_buffered_print(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;
|
||||
@@ -248,7 +269,7 @@ int64_t mz_stream_buffered_tell(void *stream)
|
||||
buffered->position, buffered->readbuf_pos, buffered->writebuf_pos, errno);
|
||||
|
||||
if (buffered->readbuf_len > 0)
|
||||
position -= (buffered->readbuf_len - buffered->readbuf_pos);
|
||||
position -= ((int64_t)buffered->readbuf_len - buffered->readbuf_pos);
|
||||
if (buffered->writebuf_len > 0)
|
||||
position += buffered->writebuf_pos;
|
||||
return position;
|
||||
@@ -258,6 +279,7 @@ int32_t mz_stream_buffered_seek(void *stream, int64_t offset, int32_t origin)
|
||||
{
|
||||
mz_stream_buffered *buffered = (mz_stream_buffered *)stream;
|
||||
int32_t bytes_flushed = 0;
|
||||
int32_t err = MZ_OK;
|
||||
|
||||
mz_stream_buffered_print(stream, "seek [origin %d offset %llu pos %lld]\n", origin, offset, buffered->position);
|
||||
|
||||
@@ -269,20 +291,21 @@ int32_t mz_stream_buffered_seek(void *stream, int64_t offset, int32_t origin)
|
||||
{
|
||||
if ((offset >= buffered->position) && (offset <= buffered->position + buffered->writebuf_len))
|
||||
{
|
||||
buffered->writebuf_pos = (uint32_t)(offset - buffered->position);
|
||||
buffered->writebuf_pos = (int32_t)(offset - buffered->position);
|
||||
return MZ_OK;
|
||||
}
|
||||
}
|
||||
|
||||
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));
|
||||
buffered->readbuf_pos = (int32_t)(offset - (buffered->position - buffered->readbuf_len));
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
if (mz_stream_buffered_flush(stream, &bytes_flushed) != MZ_OK)
|
||||
return MZ_STREAM_ERROR;
|
||||
err = mz_stream_buffered_flush(stream, &bytes_flushed);
|
||||
if (err != MZ_OK)
|
||||
return err;
|
||||
|
||||
buffered->position = offset;
|
||||
break;
|
||||
@@ -291,17 +314,17 @@ int32_t mz_stream_buffered_seek(void *stream, int64_t offset, int32_t origin)
|
||||
|
||||
if (buffered->readbuf_len > 0)
|
||||
{
|
||||
if (offset <= (buffered->readbuf_len - buffered->readbuf_pos))
|
||||
if (offset <= ((int64_t)buffered->readbuf_len - buffered->readbuf_pos))
|
||||
{
|
||||
buffered->readbuf_pos += (uint32_t)offset;
|
||||
return MZ_OK;
|
||||
}
|
||||
offset -= (buffered->readbuf_len - buffered->readbuf_pos);
|
||||
offset -= ((int64_t)buffered->readbuf_len - buffered->readbuf_pos);
|
||||
buffered->position += offset;
|
||||
}
|
||||
if (buffered->writebuf_len > 0)
|
||||
{
|
||||
if (offset <= (buffered->writebuf_len - buffered->writebuf_pos))
|
||||
if (offset <= ((int64_t)buffered->writebuf_len - buffered->writebuf_pos))
|
||||
{
|
||||
buffered->writebuf_pos += (uint32_t)offset;
|
||||
return MZ_OK;
|
||||
@@ -309,8 +332,9 @@ int32_t mz_stream_buffered_seek(void *stream, int64_t offset, int32_t origin)
|
||||
//offset -= (buffered->writebuf_len - buffered->writebuf_pos);
|
||||
}
|
||||
|
||||
if (mz_stream_buffered_flush(stream, &bytes_flushed) != MZ_OK)
|
||||
return MZ_STREAM_ERROR;
|
||||
err = mz_stream_buffered_flush(stream, &bytes_flushed);
|
||||
if (err != MZ_OK)
|
||||
return err;
|
||||
|
||||
break;
|
||||
|
||||
@@ -338,15 +362,21 @@ int32_t mz_stream_buffered_close(void *stream)
|
||||
int32_t bytes_flushed = 0;
|
||||
|
||||
mz_stream_buffered_flush(stream, &bytes_flushed);
|
||||
mz_stream_buffered_print(stream, "close\n");
|
||||
mz_stream_buffered_print(stream, "close [flushed %d]\n", bytes_flushed);
|
||||
|
||||
if (buffered->readbuf_hits + buffered->readbuf_misses > 0)
|
||||
mz_stream_buffered_print(stream, "read efficency %.02f%%\n",
|
||||
{
|
||||
mz_stream_buffered_print(stream, "read efficiency %.02f%%\n",
|
||||
(buffered->readbuf_hits / ((float)buffered->readbuf_hits + buffered->readbuf_misses)) * 100);
|
||||
}
|
||||
|
||||
if (buffered->writebuf_hits + buffered->writebuf_misses > 0)
|
||||
mz_stream_buffered_print(stream, "write efficency %.02f%%\n",
|
||||
{
|
||||
mz_stream_buffered_print(stream, "write efficiency %.02f%%\n",
|
||||
(buffered->writebuf_hits / ((float)buffered->writebuf_hits + buffered->writebuf_misses)) * 100);
|
||||
}
|
||||
|
||||
mz_stream_buffered_reset(buffered);
|
||||
|
||||
return mz_stream_close(buffered->stream.base);
|
||||
}
|
||||
@@ -361,7 +391,7 @@ 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));
|
||||
@@ -380,7 +410,7 @@ void mz_stream_buffered_delete(void **stream)
|
||||
return;
|
||||
buffered = (mz_stream_buffered *)*stream;
|
||||
if (buffered != NULL)
|
||||
free(buffered);
|
||||
MZ_FREE(buffered);
|
||||
*stream = NULL;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/* mz_strm_buf.h -- Stream for buffering reads/writes
|
||||
Version 2.2.7, January 30th, 2018
|
||||
Version 2.7.3, November 4, 2018
|
||||
part of the MiniZip project
|
||||
|
||||
This version of ioapi is designed to buffer IO.
|
||||
@@ -11,8 +11,8 @@
|
||||
See the accompanying LICENSE file for the full text of the license.
|
||||
*/
|
||||
|
||||
#ifndef _MZ_STREAM_BUFFERED_H
|
||||
#define _MZ_STREAM_BUFFERED_H
|
||||
#ifndef MZ_STREAM_BUFFERED_H
|
||||
#define MZ_STREAM_BUFFERED_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/* mz_strm_bzip.c -- Stream for bzip inflate/deflate
|
||||
Version 2.2.7, January 30th, 2018
|
||||
Version 2.7.3, November 4, 2018
|
||||
part of the MiniZip project
|
||||
|
||||
Copyright (C) 2010-2018 Nathan Moinvaziri
|
||||
@@ -10,7 +10,6 @@
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
@@ -23,7 +22,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 +60,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,21 +75,29 @@ 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)
|
||||
return MZ_STREAM_ERROR;
|
||||
return MZ_OPEN_ERROR;
|
||||
|
||||
bzip->initialized = 1;
|
||||
bzip->stream_end = 0;
|
||||
@@ -101,21 +109,24 @@ int32_t mz_stream_bzip_is_open(void *stream)
|
||||
{
|
||||
mz_stream_bzip *bzip = (mz_stream_bzip *)stream;
|
||||
if (bzip->initialized != 1)
|
||||
return MZ_STREAM_ERROR;
|
||||
return MZ_OPEN_ERROR;
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_stream_bzip_read(void *stream, void *buf, int32_t size)
|
||||
{
|
||||
#ifdef MZ_ZIP_NO_DECOMPRESSION
|
||||
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;
|
||||
uint64_t total_in_after = 0;
|
||||
uint64_t total_out_after = 0;
|
||||
uint32_t total_in = 0;
|
||||
uint32_t total_out = 0;
|
||||
uint32_t in_bytes = 0;
|
||||
uint32_t out_bytes = 0;
|
||||
int32_t total_in = 0;
|
||||
int32_t total_out = 0;
|
||||
int32_t in_bytes = 0;
|
||||
int32_t out_bytes = 0;
|
||||
int32_t bytes_to_read = 0;
|
||||
int32_t read = 0;
|
||||
int32_t err = BZ_OK;
|
||||
@@ -134,36 +145,33 @@ 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);
|
||||
|
||||
if (read < 0)
|
||||
{
|
||||
bzip->error = BZ_IO_ERROR;
|
||||
break;
|
||||
}
|
||||
return read;
|
||||
if (read == 0)
|
||||
break;
|
||||
|
||||
bzip->bzstream.next_in = (char *)bzip->buffer;
|
||||
bzip->bzstream.avail_in = read;
|
||||
bzip->bzstream.avail_in = (uint32_t)read;
|
||||
}
|
||||
|
||||
total_in_before = bzip->bzstream.avail_in;
|
||||
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);
|
||||
out_bytes = (uint32_t)(total_out_after - total_out_before);
|
||||
in_bytes = (int32_t)(total_in_before - total_in_after);
|
||||
out_bytes = (int32_t)(total_out_after - total_out_before);
|
||||
|
||||
total_in += in_bytes;
|
||||
total_out += out_bytes;
|
||||
@@ -185,16 +193,17 @@ int32_t mz_stream_bzip_read(void *stream, void *buf, int32_t size)
|
||||
while (bzip->bzstream.avail_out > 0);
|
||||
|
||||
if (bzip->error != 0)
|
||||
return bzip->error;
|
||||
return MZ_DATA_ERROR;
|
||||
|
||||
return total_out;
|
||||
#endif
|
||||
}
|
||||
|
||||
static int32_t mz_stream_bzip_flush(void *stream)
|
||||
{
|
||||
mz_stream_bzip *bzip = (mz_stream_bzip *)stream;
|
||||
if (mz_stream_write(bzip->stream.base, bzip->buffer, bzip->buffer_len) != bzip->buffer_len)
|
||||
return MZ_STREAM_ERROR;
|
||||
return MZ_WRITE_ERROR;
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
@@ -210,11 +219,9 @@ static int32_t mz_stream_bzip_compress(void *stream, int flush)
|
||||
{
|
||||
if (bzip->bzstream.avail_out == 0)
|
||||
{
|
||||
if (mz_stream_bzip_flush(bzip) != MZ_OK)
|
||||
{
|
||||
bzip->error = BZ_DATA_ERROR;
|
||||
return MZ_STREAM_ERROR;
|
||||
}
|
||||
err = mz_stream_bzip_flush(bzip);
|
||||
if (err != MZ_OK)
|
||||
return err;
|
||||
|
||||
bzip->bzstream.avail_out = sizeof(bzip->buffer);
|
||||
bzip->bzstream.next_out = (char *)bzip->buffer;
|
||||
@@ -222,12 +229,12 @@ static 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);
|
||||
@@ -240,7 +247,7 @@ static int32_t mz_stream_bzip_compress(void *stream, int flush)
|
||||
if (err < 0)
|
||||
{
|
||||
bzip->error = err;
|
||||
return MZ_STREAM_ERROR;
|
||||
return MZ_DATA_ERROR;
|
||||
}
|
||||
}
|
||||
while ((bzip->bzstream.avail_in > 0) || (flush == BZ_FINISH && err == BZ_FINISH_OK));
|
||||
@@ -251,26 +258,36 @@ static int32_t mz_stream_bzip_compress(void *stream, int flush)
|
||||
int32_t mz_stream_bzip_write(void *stream, const void *buf, int32_t size)
|
||||
{
|
||||
mz_stream_bzip *bzip = (mz_stream_bzip *)stream;
|
||||
int32_t err = size;
|
||||
|
||||
|
||||
bzip->bzstream.next_in = (char *)buf;
|
||||
#ifdef MZ_ZIP_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)
|
||||
{
|
||||
return MZ_STREAM_ERROR;
|
||||
MZ_UNUSED(stream);
|
||||
|
||||
return MZ_TELL_ERROR;
|
||||
}
|
||||
|
||||
int32_t mz_stream_bzip_seek(void *stream, int64_t offset, int32_t origin)
|
||||
{
|
||||
return MZ_STREAM_ERROR;
|
||||
MZ_UNUSED(stream);
|
||||
MZ_UNUSED(offset);
|
||||
MZ_UNUSED(origin);
|
||||
|
||||
return MZ_SEEK_ERROR;
|
||||
}
|
||||
|
||||
int32_t mz_stream_bzip_close(void *stream)
|
||||
@@ -279,20 +296,28 @@ 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;
|
||||
|
||||
if (bzip->error != BZ_OK)
|
||||
return MZ_STREAM_ERROR;
|
||||
return MZ_CLOSE_ERROR;
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
@@ -309,15 +334,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)
|
||||
@@ -342,7 +372,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));
|
||||
@@ -362,7 +392,7 @@ void mz_stream_bzip_delete(void **stream)
|
||||
return;
|
||||
bzip = (mz_stream_bzip *)*stream;
|
||||
if (bzip != NULL)
|
||||
free(bzip);
|
||||
MZ_FREE(bzip);
|
||||
*stream = NULL;
|
||||
}
|
||||
|
||||
@@ -373,4 +403,5 @@ void *mz_stream_bzip_get_interface(void)
|
||||
|
||||
extern void bz_internal_error(int errcode)
|
||||
{
|
||||
MZ_UNUSED(errcode);
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/* mz_strm_bzip.h -- Stream for bzip inflate/deflate
|
||||
Version 2.2.7, January 30th, 2018
|
||||
Version 2.7.3, November 4, 2018
|
||||
part of the MiniZip project
|
||||
|
||||
Copyright (C) 2010-2018 Nathan Moinvaziri
|
||||
@@ -9,8 +9,8 @@
|
||||
See the accompanying LICENSE file for the full text of the license.
|
||||
*/
|
||||
|
||||
#ifndef _MZ_STREAM_BZIP_H
|
||||
#define _MZ_STREAM_BZIP_H
|
||||
#ifndef MZ_STREAM_BZIP_H
|
||||
#define MZ_STREAM_BZIP_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
@@ -0,0 +1,444 @@
|
||||
/* mz_strm_libcomp.c -- Stream for apple compression
|
||||
Version 2.7.3, November 4, 2018
|
||||
part of the MiniZip project
|
||||
|
||||
Copyright (C) 2010-2018 Nathan Moinvaziri
|
||||
https://github.com/nmoinvaz/minizip
|
||||
|
||||
This program is distributed under the terms of the same license as zlib.
|
||||
See the accompanying LICENSE file for the full text of the license.
|
||||
*/
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <compression.h>
|
||||
|
||||
#include "zlib.h"
|
||||
|
||||
#include "mz.h"
|
||||
#include "mz_strm.h"
|
||||
#include "mz_strm_libcomp.h"
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
static mz_stream_vtbl mz_stream_libcomp_vtbl = {
|
||||
mz_stream_libcomp_open,
|
||||
mz_stream_libcomp_is_open,
|
||||
mz_stream_libcomp_read,
|
||||
mz_stream_libcomp_write,
|
||||
mz_stream_libcomp_tell,
|
||||
mz_stream_libcomp_seek,
|
||||
mz_stream_libcomp_close,
|
||||
mz_stream_libcomp_error,
|
||||
mz_stream_libcomp_create,
|
||||
mz_stream_libcomp_delete,
|
||||
mz_stream_libcomp_get_prop_int64,
|
||||
mz_stream_libcomp_set_prop_int64
|
||||
};
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct mz_stream_libcomp_s {
|
||||
mz_stream stream;
|
||||
compression_stream
|
||||
cstream;
|
||||
uint8_t buffer[INT16_MAX];
|
||||
int32_t buffer_len;
|
||||
int64_t total_in;
|
||||
int64_t total_out;
|
||||
int64_t max_total_in;
|
||||
int8_t initialized;
|
||||
int32_t mode;
|
||||
int32_t error;
|
||||
int16_t algorithm;
|
||||
} mz_stream_libcomp;
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t mz_stream_libcomp_open(void *stream, const char *path, int32_t mode)
|
||||
{
|
||||
mz_stream_libcomp *libcomp = (mz_stream_libcomp *)stream;
|
||||
int32_t err = 0;
|
||||
int16_t operation = 0;
|
||||
|
||||
MZ_UNUSED(path);
|
||||
|
||||
if (libcomp->algorithm == 0)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
libcomp->total_in = 0;
|
||||
libcomp->total_out = 0;
|
||||
|
||||
if (mode & MZ_OPEN_MODE_WRITE)
|
||||
{
|
||||
#ifdef MZ_ZIP_NO_COMPRESSION
|
||||
return MZ_SUPPORT_ERROR;
|
||||
#else
|
||||
operation = COMPRESSION_STREAM_ENCODE;
|
||||
#endif
|
||||
}
|
||||
else if (mode & MZ_OPEN_MODE_READ)
|
||||
{
|
||||
#ifdef MZ_ZIP_NO_DECOMPRESSION
|
||||
return MZ_SUPPORT_ERROR;
|
||||
#else
|
||||
operation = COMPRESSION_STREAM_DECODE;
|
||||
#endif
|
||||
}
|
||||
|
||||
err = compression_stream_init(&libcomp->cstream, (compression_stream_operation)operation,
|
||||
(compression_algorithm)libcomp->algorithm);
|
||||
|
||||
if (err == COMPRESSION_STATUS_ERROR)
|
||||
{
|
||||
libcomp->error = err;
|
||||
return MZ_OPEN_ERROR;
|
||||
}
|
||||
|
||||
libcomp->initialized = 1;
|
||||
libcomp->mode = mode;
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_stream_libcomp_is_open(void *stream)
|
||||
{
|
||||
mz_stream_libcomp *libcomp = (mz_stream_libcomp *)stream;
|
||||
if (libcomp->initialized != 1)
|
||||
return MZ_OPEN_ERROR;
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_stream_libcomp_read(void *stream, void *buf, int32_t size)
|
||||
{
|
||||
#ifdef MZ_ZIP_NO_DECOMPRESSION
|
||||
return MZ_SUPPORT_ERROR;
|
||||
#else
|
||||
mz_stream_libcomp *libcomp = (mz_stream_libcomp *)stream;
|
||||
uint64_t total_in_before = 0;
|
||||
uint64_t total_in_after = 0;
|
||||
uint64_t total_out_before = 0;
|
||||
uint64_t total_out_after = 0;
|
||||
int32_t total_in = 0;
|
||||
int32_t total_out = 0;
|
||||
int32_t in_bytes = 0;
|
||||
int32_t out_bytes = 0;
|
||||
int32_t bytes_to_read = 0;
|
||||
int32_t read = 0;
|
||||
int32_t err = Z_OK;
|
||||
|
||||
|
||||
libcomp->cstream.dst_ptr = buf;
|
||||
libcomp->cstream.dst_size = (size_t)size;
|
||||
|
||||
do
|
||||
{
|
||||
if (libcomp->cstream.src_size == 0)
|
||||
{
|
||||
bytes_to_read = sizeof(libcomp->buffer);
|
||||
if (libcomp->max_total_in > 0)
|
||||
{
|
||||
if ((libcomp->max_total_in - libcomp->total_in) < (int64_t)sizeof(libcomp->buffer))
|
||||
bytes_to_read = (int32_t)(libcomp->max_total_in - libcomp->total_in);
|
||||
}
|
||||
|
||||
read = mz_stream_read(libcomp->stream.base, libcomp->buffer, bytes_to_read);
|
||||
|
||||
if (read < 0)
|
||||
return read;
|
||||
if (read == 0)
|
||||
break;
|
||||
|
||||
libcomp->cstream.src_ptr = libcomp->buffer;
|
||||
libcomp->cstream.src_size = (size_t)read;
|
||||
}
|
||||
|
||||
total_in_before = libcomp->cstream.src_size;
|
||||
total_out_before = libcomp->cstream.dst_size;
|
||||
|
||||
err = compression_stream_process(&libcomp->cstream, 0);
|
||||
if (err == COMPRESSION_STATUS_ERROR)
|
||||
{
|
||||
libcomp->error = err;
|
||||
break;
|
||||
}
|
||||
|
||||
total_in_after = libcomp->cstream.src_size;
|
||||
total_out_after = libcomp->cstream.dst_size;
|
||||
|
||||
in_bytes = (int32_t)(total_in_before - total_in_after);
|
||||
out_bytes = (int32_t)(total_out_before - total_out_after);
|
||||
|
||||
total_in += in_bytes;
|
||||
total_out += out_bytes;
|
||||
|
||||
libcomp->total_in += in_bytes;
|
||||
libcomp->total_out += out_bytes;
|
||||
|
||||
if (err == COMPRESSION_STATUS_END)
|
||||
break;
|
||||
if (err != COMPRESSION_STATUS_OK)
|
||||
{
|
||||
libcomp->error = err;
|
||||
break;
|
||||
}
|
||||
}
|
||||
while (libcomp->cstream.dst_size > 0);
|
||||
|
||||
if (libcomp->error != 0)
|
||||
return MZ_DATA_ERROR;
|
||||
|
||||
return total_out;
|
||||
#endif
|
||||
}
|
||||
|
||||
static int32_t mz_stream_libcomp_flush(void *stream)
|
||||
{
|
||||
mz_stream_libcomp *libcomp = (mz_stream_libcomp *)stream;
|
||||
if (mz_stream_write(libcomp->stream.base, libcomp->buffer, libcomp->buffer_len) != libcomp->buffer_len)
|
||||
return MZ_WRITE_ERROR;
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
static int32_t mz_stream_libcomp_deflate(void *stream, int flush)
|
||||
{
|
||||
mz_stream_libcomp *libcomp = (mz_stream_libcomp *)stream;
|
||||
uint64_t total_out_before = 0;
|
||||
uint64_t total_out_after = 0;
|
||||
uint32_t out_bytes = 0;
|
||||
int32_t err = Z_OK;
|
||||
|
||||
|
||||
do
|
||||
{
|
||||
if (libcomp->cstream.dst_size == 0)
|
||||
{
|
||||
err = mz_stream_libcomp_flush(libcomp);
|
||||
if (err != MZ_OK)
|
||||
{
|
||||
libcomp->error = err;
|
||||
return err;
|
||||
}
|
||||
|
||||
libcomp->cstream.dst_size = sizeof(libcomp->buffer);
|
||||
libcomp->cstream.dst_ptr = libcomp->buffer;
|
||||
|
||||
libcomp->buffer_len = 0;
|
||||
}
|
||||
|
||||
total_out_before = libcomp->cstream.dst_size;
|
||||
err = compression_stream_process(&libcomp->cstream, flush);
|
||||
total_out_after = libcomp->cstream.dst_size;
|
||||
|
||||
out_bytes = (uint32_t)(total_out_before - total_out_after);
|
||||
|
||||
libcomp->buffer_len += out_bytes;
|
||||
libcomp->total_out += out_bytes;
|
||||
|
||||
if (err == COMPRESSION_STATUS_END)
|
||||
break;
|
||||
if (err != COMPRESSION_STATUS_OK)
|
||||
{
|
||||
libcomp->error = err;
|
||||
return MZ_DATA_ERROR;
|
||||
}
|
||||
}
|
||||
while ((libcomp->cstream.src_size > 0) || (flush == COMPRESSION_STREAM_FINALIZE && err == COMPRESSION_STATUS_OK));
|
||||
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_stream_libcomp_write(void *stream, const void *buf, int32_t size)
|
||||
{
|
||||
mz_stream_libcomp *libcomp = (mz_stream_libcomp *)stream;
|
||||
int32_t err = size;
|
||||
|
||||
#ifdef MZ_ZIP_NO_COMPRESSION
|
||||
MZ_UNUSED(libcomp);
|
||||
err = MZ_SUPPORT_ERROR;
|
||||
#else
|
||||
libcomp->cstream.src_ptr = buf;
|
||||
libcomp->cstream.src_size = (size_t)size;
|
||||
|
||||
mz_stream_libcomp_deflate(stream, 0);
|
||||
|
||||
libcomp->total_in += size;
|
||||
#endif
|
||||
return err;
|
||||
}
|
||||
|
||||
int64_t mz_stream_libcomp_tell(void *stream)
|
||||
{
|
||||
MZ_UNUSED(stream);
|
||||
|
||||
return MZ_TELL_ERROR;
|
||||
}
|
||||
|
||||
int32_t mz_stream_libcomp_seek(void *stream, int64_t offset, int32_t origin)
|
||||
{
|
||||
MZ_UNUSED(stream);
|
||||
MZ_UNUSED(offset);
|
||||
MZ_UNUSED(origin);
|
||||
|
||||
return MZ_SEEK_ERROR;
|
||||
}
|
||||
|
||||
int32_t mz_stream_libcomp_close(void *stream)
|
||||
{
|
||||
mz_stream_libcomp *libcomp = (mz_stream_libcomp *)stream;
|
||||
|
||||
|
||||
if (libcomp->mode & MZ_OPEN_MODE_WRITE)
|
||||
{
|
||||
#ifdef MZ_ZIP_NO_COMPRESSION
|
||||
return MZ_SUPPORT_ERROR;
|
||||
#else
|
||||
mz_stream_libcomp_deflate(stream, COMPRESSION_STREAM_FINALIZE);
|
||||
mz_stream_libcomp_flush(stream);
|
||||
#endif
|
||||
}
|
||||
else if (libcomp->mode & MZ_OPEN_MODE_READ)
|
||||
{
|
||||
#ifdef MZ_ZIP_NO_DECOMPRESSION
|
||||
return MZ_SUPPORT_ERROR;
|
||||
#endif
|
||||
}
|
||||
|
||||
compression_stream_destroy(&libcomp->cstream);
|
||||
|
||||
libcomp->initialized = 0;
|
||||
|
||||
if (libcomp->error != Z_OK)
|
||||
return MZ_CLOSE_ERROR;
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_stream_libcomp_error(void *stream)
|
||||
{
|
||||
mz_stream_libcomp *libcomp = (mz_stream_libcomp *)stream;
|
||||
return libcomp->error;
|
||||
}
|
||||
|
||||
int32_t mz_stream_libcomp_get_prop_int64(void *stream, int32_t prop, int64_t *value)
|
||||
{
|
||||
mz_stream_libcomp *libcomp = (mz_stream_libcomp *)stream;
|
||||
switch (prop)
|
||||
{
|
||||
case MZ_STREAM_PROP_TOTAL_IN:
|
||||
*value = libcomp->total_in;
|
||||
break;
|
||||
case MZ_STREAM_PROP_TOTAL_IN_MAX:
|
||||
*value = libcomp->max_total_in;
|
||||
break;
|
||||
case MZ_STREAM_PROP_TOTAL_OUT:
|
||||
*value = libcomp->total_out;
|
||||
break;
|
||||
case MZ_STREAM_PROP_HEADER_SIZE:
|
||||
*value = 0;
|
||||
break;
|
||||
default:
|
||||
return MZ_EXIST_ERROR;
|
||||
}
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_stream_libcomp_set_prop_int64(void *stream, int32_t prop, int64_t value)
|
||||
{
|
||||
mz_stream_libcomp *libcomp = (mz_stream_libcomp *)stream;
|
||||
switch (prop)
|
||||
{
|
||||
case MZ_STREAM_PROP_COMPRESS_ALGORITHM:
|
||||
libcomp->algorithm = (int16_t)value;
|
||||
break;
|
||||
case MZ_STREAM_PROP_TOTAL_IN_MAX:
|
||||
libcomp->max_total_in = value;
|
||||
break;
|
||||
default:
|
||||
return MZ_EXIST_ERROR;
|
||||
}
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
void *mz_stream_libcomp_create(void **stream)
|
||||
{
|
||||
mz_stream_libcomp *libcomp = NULL;
|
||||
|
||||
libcomp = (mz_stream_libcomp *)MZ_ALLOC(sizeof(mz_stream_libcomp));
|
||||
if (libcomp != NULL)
|
||||
{
|
||||
memset(libcomp, 0, sizeof(mz_stream_libcomp));
|
||||
libcomp->stream.vtbl = &mz_stream_libcomp_vtbl;
|
||||
}
|
||||
if (stream != NULL)
|
||||
*stream = libcomp;
|
||||
|
||||
return libcomp;
|
||||
}
|
||||
|
||||
void mz_stream_libcomp_delete(void **stream)
|
||||
{
|
||||
mz_stream_libcomp *libcomp = NULL;
|
||||
if (stream == NULL)
|
||||
return;
|
||||
libcomp = (mz_stream_libcomp *)*stream;
|
||||
if (libcomp != NULL)
|
||||
MZ_FREE(libcomp);
|
||||
*stream = NULL;
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
// Define z_crc_t in zlib 1.2.5 and less
|
||||
#if (ZLIB_VERNUM < 0x1270)
|
||||
typedef unsigned long z_crc_t;
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
static mz_stream_vtbl mz_stream_zlib_vtbl = {
|
||||
mz_stream_libcomp_open,
|
||||
mz_stream_libcomp_is_open,
|
||||
mz_stream_libcomp_read,
|
||||
mz_stream_libcomp_write,
|
||||
mz_stream_libcomp_tell,
|
||||
mz_stream_libcomp_seek,
|
||||
mz_stream_libcomp_close,
|
||||
mz_stream_libcomp_error,
|
||||
mz_stream_zlib_create,
|
||||
mz_stream_libcomp_delete,
|
||||
mz_stream_libcomp_get_prop_int64,
|
||||
mz_stream_libcomp_set_prop_int64
|
||||
};
|
||||
|
||||
void *mz_stream_zlib_create(void **stream)
|
||||
{
|
||||
mz_stream_libcomp *libcomp = NULL;
|
||||
void *stream_int = NULL;
|
||||
mz_stream_libcomp_create(&stream_int);
|
||||
if (stream_int != NULL)
|
||||
{
|
||||
libcomp = (mz_stream_libcomp *)stream_int;
|
||||
libcomp->stream.vtbl = &mz_stream_zlib_vtbl;
|
||||
libcomp->algorithm = COMPRESSION_ZLIB;
|
||||
}
|
||||
if (stream != NULL)
|
||||
*stream = stream_int;
|
||||
return stream_int;
|
||||
}
|
||||
|
||||
void *mz_stream_zlib_get_interface(void)
|
||||
{
|
||||
return (void *)&mz_stream_zlib_vtbl;
|
||||
}
|
||||
|
||||
static int64_t mz_stream_zlib_crc32(int64_t value, const void *buf, int32_t size)
|
||||
{
|
||||
return (int64_t)crc32((z_crc_t)value, buf, (uint32_t)size);
|
||||
}
|
||||
|
||||
void *mz_stream_zlib_get_crc32_update(void)
|
||||
{
|
||||
return (void *)mz_stream_zlib_crc32;
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
/* mz_strm_libcomp.h -- Stream for apple compression
|
||||
Version 2.7.3, November 4, 2018
|
||||
part of the MiniZip project
|
||||
|
||||
Copyright (C) 2010-2018 Nathan Moinvaziri
|
||||
https://github.com/nmoinvaz/minizip
|
||||
|
||||
This program is distributed under the terms of the same license as zlib.
|
||||
See the accompanying LICENSE file for the full text of the license.
|
||||
*/
|
||||
|
||||
#ifndef MZ_STREAM_LIBCOMP_H
|
||||
#define MZ_STREAM_LIBCOMP_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t mz_stream_libcomp_open(void *stream, const char *filename, int32_t mode);
|
||||
int32_t mz_stream_libcomp_is_open(void *stream);
|
||||
int32_t mz_stream_libcomp_read(void *stream, void *buf, int32_t size);
|
||||
int32_t mz_stream_libcomp_write(void *stream, const void *buf, int32_t size);
|
||||
int64_t mz_stream_libcomp_tell(void *stream);
|
||||
int32_t mz_stream_libcomp_seek(void *stream, int64_t offset, int32_t origin);
|
||||
int32_t mz_stream_libcomp_close(void *stream);
|
||||
int32_t mz_stream_libcomp_error(void *stream);
|
||||
|
||||
int32_t mz_stream_libcomp_get_prop_int64(void *stream, int32_t prop, int64_t *value);
|
||||
int32_t mz_stream_libcomp_set_prop_int64(void *stream, int32_t prop, int64_t value);
|
||||
|
||||
void* mz_stream_libcomp_create(void **stream);
|
||||
void mz_stream_libcomp_delete(void **stream);
|
||||
|
||||
void* mz_stream_libcomp_get_interface(void);
|
||||
void* mz_stream_libcomp_get_crc32_update(void);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t mz_stream_zlib_open(void *stream, const char *filename, int32_t mode);
|
||||
int32_t mz_stream_zlib_is_open(void *stream);
|
||||
int32_t mz_stream_zlib_read(void *stream, void *buf, int32_t size);
|
||||
int32_t mz_stream_zlib_write(void *stream, const void *buf, int32_t size);
|
||||
int64_t mz_stream_zlib_tell(void *stream);
|
||||
int32_t mz_stream_zlib_seek(void *stream, int64_t offset, int32_t origin);
|
||||
int32_t mz_stream_zlib_close(void *stream);
|
||||
int32_t mz_stream_zlib_error(void *stream);
|
||||
|
||||
int32_t mz_stream_zlib_get_prop_int64(void *stream, int32_t prop, int64_t *value);
|
||||
int32_t mz_stream_zlib_set_prop_int64(void *stream, int32_t prop, int64_t value);
|
||||
|
||||
void* mz_stream_zlib_create(void **stream);
|
||||
void mz_stream_zlib_delete(void **stream);
|
||||
|
||||
void* mz_stream_zlib_get_interface(void);
|
||||
void* mz_stream_zlib_get_crc32_update(void);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,5 +1,5 @@
|
||||
/* mz_strm_lzma.c -- Stream for lzma inflate/deflate
|
||||
Version 2.2.7, January 30th, 2018
|
||||
Version 2.7.3, November 4, 2018
|
||||
part of the MiniZip project
|
||||
|
||||
Copyright (C) 2010-2018 Nathan Moinvaziri
|
||||
@@ -10,13 +10,12 @@
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <lzma.h>
|
||||
#include "lzma.h"
|
||||
|
||||
#include "mz.h"
|
||||
#include "mz_strm.h"
|
||||
@@ -28,7 +27,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 +54,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 +65,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,11 +82,15 @@ 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);
|
||||
|
||||
if (lzma_lzma_preset(&opt_lzma, lzma->preset))
|
||||
return MZ_STREAM_ERROR;
|
||||
return MZ_OPEN_ERROR;
|
||||
|
||||
memset(&filters, 0, sizeof(filters));
|
||||
|
||||
@@ -91,7 +99,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 +107,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,10 +124,11 @@ 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;
|
||||
return MZ_OPEN_ERROR;
|
||||
|
||||
lzma->initialized = 1;
|
||||
lzma->mode = mode;
|
||||
@@ -126,21 +139,24 @@ int32_t mz_stream_lzma_is_open(void *stream)
|
||||
{
|
||||
mz_stream_lzma *lzma = (mz_stream_lzma *)stream;
|
||||
if (lzma->initialized != 1)
|
||||
return MZ_STREAM_ERROR;
|
||||
return MZ_OPEN_ERROR;
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_stream_lzma_read(void *stream, void *buf, int32_t size)
|
||||
{
|
||||
#ifdef MZ_ZIP_NO_DECOMPRESSION
|
||||
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;
|
||||
uint64_t total_in_after = 0;
|
||||
uint64_t total_out_after = 0;
|
||||
uint32_t total_in = 0;
|
||||
uint32_t total_out = 0;
|
||||
uint32_t in_bytes = 0;
|
||||
uint32_t out_bytes = 0;
|
||||
int32_t total_in = 0;
|
||||
int32_t total_out = 0;
|
||||
int32_t in_bytes = 0;
|
||||
int32_t out_bytes = 0;
|
||||
int32_t bytes_to_read = 0;
|
||||
int32_t read = 0;
|
||||
int32_t err = LZMA_OK;
|
||||
@@ -156,22 +172,19 @@ int32_t mz_stream_lzma_read(void *stream, void *buf, int32_t size)
|
||||
bytes_to_read = sizeof(lzma->buffer);
|
||||
if (lzma->max_total_in > 0)
|
||||
{
|
||||
if ((lzma->max_total_in - lzma->total_in) < 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)
|
||||
{
|
||||
lzma->error = MZ_STREAM_ERROR;
|
||||
break;
|
||||
}
|
||||
return read;
|
||||
if (read == 0)
|
||||
break;
|
||||
|
||||
lzma->lstream.next_in = lzma->buffer;
|
||||
lzma->lstream.avail_in = read;
|
||||
lzma->lstream.avail_in = (size_t)read;
|
||||
}
|
||||
|
||||
total_in_before = lzma->lstream.avail_in;
|
||||
@@ -181,9 +194,11 @@ 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;
|
||||
|
||||
in_bytes = (uint32_t)(total_in_before - total_in_after);
|
||||
out_bytes = (uint32_t)(total_out_after - total_out_before);
|
||||
if ((lzma->max_total_out != -1) && (int64_t)total_out_after > lzma->max_total_out)
|
||||
total_out_after = (uint64_t)lzma->max_total_out;
|
||||
|
||||
in_bytes = (int32_t)(total_in_before - total_in_after);
|
||||
out_bytes = (int32_t)(total_out_after - total_out_before);
|
||||
|
||||
total_in += in_bytes;
|
||||
total_out += out_bytes;
|
||||
@@ -202,16 +217,17 @@ int32_t mz_stream_lzma_read(void *stream, void *buf, int32_t size)
|
||||
while (lzma->lstream.avail_out > 0);
|
||||
|
||||
if (lzma->error != 0)
|
||||
return lzma->error;
|
||||
return MZ_DATA_ERROR;
|
||||
|
||||
return total_out;
|
||||
#endif
|
||||
}
|
||||
|
||||
static int32_t mz_stream_lzma_flush(void *stream)
|
||||
{
|
||||
mz_stream_lzma *lzma = (mz_stream_lzma *)stream;
|
||||
if (mz_stream_write(lzma->stream.base, lzma->buffer, lzma->buffer_len) != lzma->buffer_len)
|
||||
return MZ_STREAM_ERROR;
|
||||
return MZ_WRITE_ERROR;
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
@@ -228,11 +244,9 @@ static int32_t mz_stream_lzma_code(void *stream, int32_t flush)
|
||||
{
|
||||
if (lzma->lstream.avail_out == 0)
|
||||
{
|
||||
if (mz_stream_lzma_flush(lzma) != MZ_OK)
|
||||
{
|
||||
lzma->error = MZ_STREAM_ERROR;
|
||||
return MZ_STREAM_ERROR;
|
||||
}
|
||||
err = mz_stream_lzma_flush(lzma);
|
||||
if (err != MZ_OK)
|
||||
return err;
|
||||
|
||||
lzma->lstream.avail_out = sizeof(lzma->buffer);
|
||||
lzma->lstream.next_out = lzma->buffer;
|
||||
@@ -241,7 +255,7 @@ static int32_t mz_stream_lzma_code(void *stream, int32_t flush)
|
||||
}
|
||||
|
||||
total_out_before = lzma->lstream.total_out;
|
||||
err = lzma_code(&lzma->lstream, flush);
|
||||
err = lzma_code(&lzma->lstream, (lzma_action)flush);
|
||||
total_out_after = lzma->lstream.total_out;
|
||||
|
||||
out_bytes = (uint32_t)(total_out_after - total_out_before);
|
||||
@@ -249,13 +263,13 @@ static int32_t mz_stream_lzma_code(void *stream, int32_t flush)
|
||||
if (err != LZMA_OK && err != LZMA_STREAM_END)
|
||||
{
|
||||
lzma->error = err;
|
||||
return MZ_STREAM_ERROR;
|
||||
return MZ_DATA_ERROR;
|
||||
}
|
||||
|
||||
lzma->buffer_len += out_bytes;
|
||||
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,26 +277,36 @@ static int32_t mz_stream_lzma_code(void *stream, int32_t flush)
|
||||
int32_t mz_stream_lzma_write(void *stream, const void *buf, int32_t size)
|
||||
{
|
||||
mz_stream_lzma *lzma = (mz_stream_lzma *)stream;
|
||||
int32_t err = size;
|
||||
|
||||
|
||||
lzma->lstream.next_in = (uint8_t*)buf;
|
||||
#ifdef MZ_ZIP_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)
|
||||
{
|
||||
return MZ_STREAM_ERROR;
|
||||
MZ_UNUSED(stream);
|
||||
|
||||
return MZ_TELL_ERROR;
|
||||
}
|
||||
|
||||
int32_t mz_stream_lzma_seek(void *stream, int64_t offset, int32_t origin)
|
||||
{
|
||||
return MZ_STREAM_ERROR;
|
||||
MZ_UNUSED(stream);
|
||||
MZ_UNUSED(offset);
|
||||
MZ_UNUSED(origin);
|
||||
|
||||
return MZ_SEEK_ERROR;
|
||||
}
|
||||
|
||||
int32_t mz_stream_lzma_close(void *stream)
|
||||
@@ -291,20 +315,28 @@ 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;
|
||||
|
||||
if (lzma->error != LZMA_OK)
|
||||
return MZ_STREAM_ERROR;
|
||||
return MZ_CLOSE_ERROR;
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
@@ -321,15 +353,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 +382,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 +422,7 @@ void mz_stream_lzma_delete(void **stream)
|
||||
return;
|
||||
lzma = (mz_stream_lzma *)*stream;
|
||||
if (lzma != NULL)
|
||||
free(lzma);
|
||||
MZ_FREE(lzma);
|
||||
*stream = NULL;
|
||||
}
|
||||
|
||||
@@ -382,13 +430,3 @@ void *mz_stream_lzma_get_interface(void)
|
||||
{
|
||||
return (void *)&mz_stream_lzma_vtbl;
|
||||
}
|
||||
|
||||
static int32_t mz_stream_lzma_crc32(int32_t value, const void *buf, int32_t size)
|
||||
{
|
||||
return lzma_crc32(buf, size, value);
|
||||
}
|
||||
|
||||
void *mz_stream_lzma_get_crc32_update(void)
|
||||
{
|
||||
return (void *)mz_stream_lzma_crc32;
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/* mz_strm_lzma.h -- Stream for lzma inflate/deflate
|
||||
Version 2.2.7, January 30th, 2018
|
||||
Version 2.7.3, November 4, 2018
|
||||
part of the MiniZip project
|
||||
|
||||
Copyright (C) 2010-2018 Nathan Moinvaziri
|
||||
@@ -9,8 +9,8 @@
|
||||
See the accompanying LICENSE file for the full text of the license.
|
||||
*/
|
||||
|
||||
#ifndef _MZ_STREAM_LZMA_H
|
||||
#define _MZ_STREAM_LZMA_H
|
||||
#ifndef MZ_STREAM_LZMA_H
|
||||
#define MZ_STREAM_LZMA_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
@@ -36,7 +36,6 @@ void* mz_stream_lzma_create(void **stream);
|
||||
void mz_stream_lzma_delete(void **stream);
|
||||
|
||||
void* mz_stream_lzma_get_interface(void);
|
||||
void* mz_stream_lzma_get_crc32_update(void);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
/* mz_strm_mem.c -- Stream for memory access
|
||||
Version 2.2.7, January 30th, 2018
|
||||
Version 2.7.3, November 4, 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
|
||||
|
||||
@@ -11,14 +11,13 @@
|
||||
https://github.com/nmoinvaz/minizip
|
||||
Copyright (C) 2003 Justin Fletcher
|
||||
Copyright (C) 1998-2003 Gilles Vollant
|
||||
http://www.winimage.com/zLibDll/minizip.html
|
||||
https://www.winimage.com/zLibDll/minizip.html
|
||||
|
||||
This program is distributed under the terms of the same license as zlib.
|
||||
See the accompanying LICENSE file for the full text of the license.
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
@@ -28,7 +27,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 +37,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 +47,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
|
||||
uint8_t *buffer; // Memory buffer pointer
|
||||
int32_t size; // Size of the memory buffer
|
||||
int32_t limit; // Furthest we've written
|
||||
int32_t position; // Current position in the memory
|
||||
@@ -59,14 +60,14 @@ static void mz_stream_mem_set_size(void *stream, int32_t size)
|
||||
{
|
||||
mz_stream_mem *mem = (mz_stream_mem *)stream;
|
||||
int32_t new_size = size;
|
||||
char *new_buf = NULL;
|
||||
uint8_t *new_buf = NULL;
|
||||
|
||||
|
||||
new_buf = (char *)malloc(new_size);
|
||||
new_buf = (uint8_t *)MZ_ALLOC((uint32_t)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 +78,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;
|
||||
@@ -94,7 +96,7 @@ int32_t mz_stream_mem_is_open(void *stream)
|
||||
{
|
||||
mz_stream_mem *mem = (mz_stream_mem *)stream;
|
||||
if (mem->buffer == NULL)
|
||||
return MZ_STREAM_ERROR;
|
||||
return MZ_OPEN_ERROR;
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
@@ -105,7 +107,7 @@ int32_t mz_stream_mem_read(void *stream, void *buf, int32_t size)
|
||||
if (size > mem->size - mem->position)
|
||||
size = mem->size - mem->position;
|
||||
|
||||
if (mem->position + size > mem->limit)
|
||||
if ((size <= 0) || (mem->position + size > mem->limit))
|
||||
return 0;
|
||||
|
||||
memcpy(buf, mem->buffer + mem->position, size);
|
||||
@@ -172,29 +174,33 @@ int32_t mz_stream_mem_seek(void *stream, int64_t offset, int32_t origin)
|
||||
new_pos = offset;
|
||||
break;
|
||||
default:
|
||||
return MZ_STREAM_ERROR;
|
||||
return MZ_SEEK_ERROR;
|
||||
}
|
||||
|
||||
if (new_pos > mem->size)
|
||||
{
|
||||
if ((mem->mode & MZ_OPEN_MODE_CREATE) == 0)
|
||||
return MZ_STREAM_ERROR;
|
||||
return MZ_SEEK_ERROR;
|
||||
|
||||
mz_stream_mem_set_size(stream, (int32_t)new_pos);
|
||||
}
|
||||
|
||||
mem->position = (uint32_t)new_pos;
|
||||
mem->position = (int32_t)new_pos;
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -202,25 +208,43 @@ int32_t mz_stream_mem_error(void *stream)
|
||||
void mz_stream_mem_set_buffer(void *stream, void *buf, int32_t size)
|
||||
{
|
||||
mz_stream_mem *mem = (mz_stream_mem *)stream;
|
||||
mem->buffer = buf;
|
||||
mem->buffer = (uint8_t *)buf;
|
||||
mem->size = size;
|
||||
mem->limit = size;
|
||||
}
|
||||
|
||||
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)
|
||||
return MZ_STREAM_ERROR;
|
||||
return MZ_SEEK_ERROR;
|
||||
*buf = mem->buffer + position;
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_stream_mem_get_buffer_at_current(void *stream, const void **buf)
|
||||
{
|
||||
mz_stream_mem *mem = (mz_stream_mem *)stream;
|
||||
return mz_stream_mem_get_buffer_at(stream, mem->position, buf);
|
||||
}
|
||||
|
||||
void mz_stream_mem_get_buffer_length(void *stream, int32_t *length)
|
||||
{
|
||||
mz_stream_mem *mem = (mz_stream_mem *)stream;
|
||||
*length = mem->limit;
|
||||
}
|
||||
|
||||
void mz_stream_mem_set_buffer_limit(void *stream, int32_t limit)
|
||||
{
|
||||
mz_stream_mem *mem = (mz_stream_mem *)stream;
|
||||
mem->limit = limit;
|
||||
}
|
||||
|
||||
void mz_stream_mem_set_grow_size(void *stream, int32_t grow_size)
|
||||
{
|
||||
mz_stream_mem *mem = (mz_stream_mem *)stream;
|
||||
@@ -231,7 +255,7 @@ 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));
|
||||
@@ -253,8 +277,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 +286,4 @@ void mz_stream_mem_delete(void **stream)
|
||||
void *mz_stream_mem_get_interface(void)
|
||||
{
|
||||
return (void *)&mz_stream_mem_vtbl;
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,16 @@
|
||||
/* mz_strm_mem.h -- Stream for memory access
|
||||
Version 2.2.7, January 30th, 2018
|
||||
Version 2.7.3, November 4, 2018
|
||||
part of MiniZip project
|
||||
|
||||
Copyright (C) 2010-2018 Nathan Moinvaziri
|
||||
https://github.com/nmoinvaz/minizip
|
||||
Copyright (C) 2003 Justin Fletcher
|
||||
Copyright (C) 1998-2003 Gilles Vollant
|
||||
|
||||
This program is distributed under the terms of the same license as zlib.
|
||||
See the accompanying LICENSE file for the full text of the license.
|
||||
*/
|
||||
|
||||
#ifndef _MZ_STREAM_MEM_H
|
||||
#define _MZ_STREAM_MEM_H
|
||||
#ifndef MZ_STREAM_MEM_H
|
||||
#define MZ_STREAM_MEM_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
@@ -32,8 +30,11 @@ 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);
|
||||
int32_t mz_stream_mem_get_buffer_at_current(void *stream, const void **buf);
|
||||
void mz_stream_mem_get_buffer_length(void *stream, int32_t *length);
|
||||
void mz_stream_mem_set_buffer_limit(void *stream, int32_t limit);
|
||||
void mz_stream_mem_set_grow_size(void *stream, int32_t grow_size);
|
||||
|
||||
void* mz_stream_mem_create(void **stream);
|
||||
@@ -0,0 +1,43 @@
|
||||
/* mz_sstrm_os.h -- Stream for filesystem access
|
||||
Version 2.7.3, November 4, 2018
|
||||
part of the MiniZip project
|
||||
|
||||
Copyright (C) 2010-2018 Nathan Moinvaziri
|
||||
https://github.com/nmoinvaz/minizip
|
||||
|
||||
This program is distributed under the terms of the same license as zlib.
|
||||
See the accompanying LICENSE file for the full text of the license.
|
||||
*/
|
||||
|
||||
#ifndef MZ_STREAM_OS_H
|
||||
#define MZ_STREAM_OS_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t mz_stream_os_open(void *stream, const char *path, int32_t mode);
|
||||
int32_t mz_stream_os_is_open(void *stream);
|
||||
int32_t mz_stream_os_read(void *stream, void *buf, int32_t size);
|
||||
int32_t mz_stream_os_write(void *stream, const void *buf, int32_t size);
|
||||
int64_t mz_stream_os_tell(void *stream);
|
||||
int32_t mz_stream_os_seek(void *stream, int64_t offset, int32_t origin);
|
||||
int32_t mz_stream_os_close(void *stream);
|
||||
int32_t mz_stream_os_error(void *stream);
|
||||
|
||||
void* mz_stream_os_create(void **stream);
|
||||
void mz_stream_os_delete(void **stream);
|
||||
|
||||
void* mz_stream_os_get_interface(void);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,5 +1,5 @@
|
||||
/* mz_strm_posix.c -- Stream for filesystem access for posix/linux
|
||||
Version 2.2.7, January 30th, 2018
|
||||
Version 2.7.3, November 4, 2018
|
||||
part of the MiniZip project
|
||||
|
||||
Copyright (C) 2010-2018 Nathan Moinvaziri
|
||||
@@ -8,24 +8,24 @@
|
||||
Copyright (C) 2009-2010 Mathias Svensson
|
||||
http://result42.com
|
||||
Copyright (C) 1998-2010 Gilles Vollant
|
||||
http://www.winimage.com/zLibDll/minizip.html
|
||||
https://www.winimage.com/zLibDll/minizip.html
|
||||
|
||||
This program is distributed under the terms of the same license as zlib.
|
||||
See the accompanying LICENSE file for the full text of the license.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "mz.h"
|
||||
#include "mz_strm.h"
|
||||
#include "mz_strm_posix.h"
|
||||
#include "mz_strm_os.h"
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#if defined(MZ_USE_FILE32API)
|
||||
#if defined(MZ_FILE32_API)
|
||||
# define fopen64 fopen
|
||||
# define ftello64 ftell
|
||||
# define fseeko64 fseek
|
||||
@@ -50,17 +50,19 @@
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
mz_stream_vtbl mz_stream_posix_vtbl = {
|
||||
mz_stream_posix_open,
|
||||
mz_stream_posix_is_open,
|
||||
mz_stream_posix_read,
|
||||
mz_stream_posix_write,
|
||||
mz_stream_posix_tell,
|
||||
mz_stream_posix_seek,
|
||||
mz_stream_posix_close,
|
||||
mz_stream_posix_error,
|
||||
mz_stream_posix_create,
|
||||
mz_stream_posix_delete
|
||||
static mz_stream_vtbl mz_stream_os_vtbl = {
|
||||
mz_stream_os_open,
|
||||
mz_stream_os_is_open,
|
||||
mz_stream_os_read,
|
||||
mz_stream_os_write,
|
||||
mz_stream_os_tell,
|
||||
mz_stream_os_seek,
|
||||
mz_stream_os_close,
|
||||
mz_stream_os_error,
|
||||
mz_stream_os_create,
|
||||
mz_stream_os_delete,
|
||||
NULL,
|
||||
NULL
|
||||
};
|
||||
|
||||
/***************************************************************************/
|
||||
@@ -74,78 +76,78 @@ typedef struct mz_stream_posix_s
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t mz_stream_posix_open(void *stream, const char *path, int32_t mode)
|
||||
int32_t mz_stream_os_open(void *stream, const char *path, int32_t mode)
|
||||
{
|
||||
mz_stream_posix *posix = (mz_stream_posix *)stream;
|
||||
const char *mode_fopen = NULL;
|
||||
|
||||
if (path == NULL)
|
||||
return MZ_STREAM_ERROR;
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
if ((mode & MZ_OPEN_MODE_READWRITE) == MZ_OPEN_MODE_READ)
|
||||
mode_fopen = "rb";
|
||||
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
|
||||
return MZ_STREAM_ERROR;
|
||||
return MZ_OPEN_ERROR;
|
||||
|
||||
posix->handle = fopen64(path, mode_fopen);
|
||||
if (posix->handle == NULL)
|
||||
{
|
||||
posix->error = errno;
|
||||
return MZ_STREAM_ERROR;
|
||||
return MZ_OPEN_ERROR;
|
||||
}
|
||||
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_stream_posix_is_open(void *stream)
|
||||
int32_t mz_stream_os_is_open(void *stream)
|
||||
{
|
||||
mz_stream_posix *posix = (mz_stream_posix*)stream;
|
||||
if (posix->handle == NULL)
|
||||
return MZ_STREAM_ERROR;
|
||||
return MZ_OPEN_ERROR;
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_stream_posix_read(void *stream, void *buf, int32_t size)
|
||||
int32_t mz_stream_os_read(void *stream, void *buf, int32_t size)
|
||||
{
|
||||
mz_stream_posix *posix = (mz_stream_posix*)stream;
|
||||
int32_t read = (int32_t)fread(buf, 1, (size_t)size, posix->handle);
|
||||
if (read < size && ferror(posix->handle))
|
||||
{
|
||||
posix->error = errno;
|
||||
return MZ_STREAM_ERROR;
|
||||
return MZ_READ_ERROR;
|
||||
}
|
||||
return read;
|
||||
}
|
||||
|
||||
int32_t mz_stream_posix_write(void *stream, const void *buf, int32_t size)
|
||||
int32_t mz_stream_os_write(void *stream, const void *buf, int32_t size)
|
||||
{
|
||||
mz_stream_posix *posix = (mz_stream_posix*)stream;
|
||||
int32_t written = (int32_t)fwrite(buf, 1, (size_t)size, posix->handle);
|
||||
if (written < size && ferror(posix->handle))
|
||||
{
|
||||
posix->error = errno;
|
||||
return MZ_STREAM_ERROR;
|
||||
return MZ_WRITE_ERROR;
|
||||
}
|
||||
return written;
|
||||
}
|
||||
|
||||
int64_t mz_stream_posix_tell(void *stream)
|
||||
int64_t mz_stream_os_tell(void *stream)
|
||||
{
|
||||
mz_stream_posix *posix = (mz_stream_posix*)stream;
|
||||
int64_t position = ftello64(posix->handle);
|
||||
if (position == -1)
|
||||
{
|
||||
posix->error = errno;
|
||||
return MZ_STREAM_ERROR;
|
||||
return MZ_TELL_ERROR;
|
||||
}
|
||||
return position;
|
||||
}
|
||||
|
||||
int32_t mz_stream_posix_seek(void *stream, int64_t offset, int32_t origin)
|
||||
int32_t mz_stream_os_seek(void *stream, int64_t offset, int32_t origin)
|
||||
{
|
||||
mz_stream_posix *posix = (mz_stream_posix*)stream;
|
||||
int32_t fseek_origin = 0;
|
||||
@@ -162,19 +164,19 @@ int32_t mz_stream_posix_seek(void *stream, int64_t offset, int32_t origin)
|
||||
fseek_origin = SEEK_SET;
|
||||
break;
|
||||
default:
|
||||
return MZ_STREAM_ERROR;
|
||||
return MZ_SEEK_ERROR;
|
||||
}
|
||||
|
||||
if (fseeko64(posix->handle, offset, fseek_origin) != 0)
|
||||
{
|
||||
posix->error = errno;
|
||||
return MZ_STREAM_ERROR;
|
||||
return MZ_SEEK_ERROR;
|
||||
}
|
||||
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_stream_posix_close(void *stream)
|
||||
int32_t mz_stream_os_close(void *stream)
|
||||
{
|
||||
mz_stream_posix *posix = (mz_stream_posix*)stream;
|
||||
int32_t closed = 0;
|
||||
@@ -186,45 +188,45 @@ int32_t mz_stream_posix_close(void *stream)
|
||||
if (closed != 0)
|
||||
{
|
||||
posix->error = errno;
|
||||
return MZ_STREAM_ERROR;
|
||||
return MZ_CLOSE_ERROR;
|
||||
}
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_stream_posix_error(void *stream)
|
||||
int32_t mz_stream_os_error(void *stream)
|
||||
{
|
||||
mz_stream_posix *posix = (mz_stream_posix*)stream;
|
||||
return posix->error;
|
||||
}
|
||||
|
||||
void *mz_stream_posix_create(void **stream)
|
||||
void *mz_stream_os_create(void **stream)
|
||||
{
|
||||
mz_stream_posix *posix = NULL;
|
||||
|
||||
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;
|
||||
posix->stream.vtbl = &mz_stream_os_vtbl;
|
||||
}
|
||||
if (stream != NULL)
|
||||
*stream = posix;
|
||||
|
||||
|
||||
return posix;
|
||||
}
|
||||
|
||||
void mz_stream_posix_delete(void **stream)
|
||||
void mz_stream_os_delete(void **stream)
|
||||
{
|
||||
mz_stream_posix *posix = NULL;
|
||||
if (stream == NULL)
|
||||
return;
|
||||
posix = (mz_stream_posix *)*stream;
|
||||
if (posix != NULL)
|
||||
free(posix);
|
||||
MZ_FREE(posix);
|
||||
*stream = NULL;
|
||||
}
|
||||
|
||||
void *mz_stream_posix_get_interface(void)
|
||||
void *mz_stream_os_get_interface(void)
|
||||
{
|
||||
return (void *)&mz_stream_posix_vtbl;
|
||||
return (void *)&mz_stream_os_vtbl;
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/* mz_strm_win32.c -- Stream for filesystem access for windows
|
||||
Version 2.2.7, January 30th, 2018
|
||||
Version 2.7.3, November 4, 2018
|
||||
part of the MiniZip project
|
||||
|
||||
Copyright (C) 2010-2018 Nathan Moinvaziri
|
||||
@@ -8,7 +8,7 @@
|
||||
Modifications for Zip64 support
|
||||
http://result42.com
|
||||
Copyright (C) 1998-2010 Gilles Vollant
|
||||
http://www.winimage.com/zLibDll/minizip.html
|
||||
https://www.winimage.com/zLibDll/minizip.html
|
||||
|
||||
This program is distributed under the terms of the same license as zlib.
|
||||
See the accompanying LICENSE file for the full text of the license.
|
||||
@@ -19,8 +19,9 @@
|
||||
#include <windows.h>
|
||||
|
||||
#include "mz.h"
|
||||
#include "mz_os.h"
|
||||
#include "mz_strm.h"
|
||||
#include "mz_strm_win32.h"
|
||||
#include "mz_strm_os.h"
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
@@ -32,25 +33,27 @@
|
||||
# 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 = {
|
||||
mz_stream_win32_open,
|
||||
mz_stream_win32_is_open,
|
||||
mz_stream_win32_read,
|
||||
mz_stream_win32_write,
|
||||
mz_stream_win32_tell,
|
||||
mz_stream_win32_seek,
|
||||
mz_stream_win32_close,
|
||||
mz_stream_win32_error,
|
||||
mz_stream_win32_create,
|
||||
mz_stream_win32_delete
|
||||
static mz_stream_vtbl mz_stream_os_vtbl = {
|
||||
mz_stream_os_open,
|
||||
mz_stream_os_is_open,
|
||||
mz_stream_os_read,
|
||||
mz_stream_os_write,
|
||||
mz_stream_os_tell,
|
||||
mz_stream_os_seek,
|
||||
mz_stream_os_close,
|
||||
mz_stream_os_error,
|
||||
mz_stream_os_create,
|
||||
mz_stream_os_delete,
|
||||
NULL,
|
||||
NULL
|
||||
};
|
||||
|
||||
/***************************************************************************/
|
||||
@@ -64,7 +67,7 @@ typedef struct mz_stream_win32_s
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t mz_stream_win32_open(void *stream, const char *path, int32_t mode)
|
||||
int32_t mz_stream_os_open(void *stream, const char *path, int32_t mode)
|
||||
{
|
||||
mz_stream_win32 *win32 = (mz_stream_win32 *)stream;
|
||||
uint32_t desired_access = 0;
|
||||
@@ -76,13 +79,15 @@ int32_t mz_stream_win32_open(void *stream, const char *path, int32_t mode)
|
||||
|
||||
|
||||
if (path == NULL)
|
||||
return MZ_STREAM_ERROR;
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
// Some use cases require write sharing as well
|
||||
share_mode |= FILE_SHARE_WRITE;
|
||||
|
||||
if ((mode & MZ_OPEN_MODE_READWRITE) == MZ_OPEN_MODE_READ)
|
||||
{
|
||||
desired_access = GENERIC_READ;
|
||||
creation_disposition = OPEN_EXISTING;
|
||||
share_mode &= FILE_SHARE_WRITE;
|
||||
}
|
||||
else if (mode & MZ_OPEN_MODE_APPEND)
|
||||
{
|
||||
@@ -96,50 +101,50 @@ int32_t mz_stream_win32_open(void *stream, const char *path, int32_t mode)
|
||||
}
|
||||
else
|
||||
{
|
||||
return MZ_STREAM_ERROR;
|
||||
return MZ_PARAM_ERROR;
|
||||
}
|
||||
|
||||
path_wide_size = MultiByteToWideChar(CP_UTF8, 0, path, -1, NULL, 0);
|
||||
path_wide = (wchar_t *)malloc((path_wide_size + 1) * sizeof(wchar_t));
|
||||
memset(path_wide, 0, sizeof(wchar_t) * (path_wide_size + 1));
|
||||
path_wide = mz_os_unicode_string_create(path, MZ_ENCODING_UTF8);
|
||||
if (path_wide == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
MultiByteToWideChar(CP_UTF8, 0, path, -1, path_wide, path_wide_size);
|
||||
|
||||
#ifdef MZ_USE_WINRT_API
|
||||
win32->handle = CreateFile2W(path_wide, desired_access, share_mode, creation_disposition, NULL);
|
||||
#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);
|
||||
win32->handle = CreateFileW(path_wide, desired_access, share_mode, NULL,
|
||||
creation_disposition, flags_attribs, NULL);
|
||||
#endif
|
||||
|
||||
free(path_wide);
|
||||
mz_os_unicode_string_delete(&path_wide);
|
||||
|
||||
if (mz_stream_win32_is_open(stream) != MZ_OK)
|
||||
if (mz_stream_os_is_open(stream) != MZ_OK)
|
||||
{
|
||||
win32->error = GetLastError();
|
||||
return MZ_STREAM_ERROR;
|
||||
return MZ_OPEN_ERROR;
|
||||
}
|
||||
|
||||
if (mode & MZ_OPEN_MODE_APPEND)
|
||||
return mz_stream_win32_seek(stream, 0, MZ_SEEK_END);
|
||||
return mz_stream_os_seek(stream, 0, MZ_SEEK_END);
|
||||
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_stream_win32_is_open(void *stream)
|
||||
{
|
||||
mz_stream_win32 *win32 = (mz_stream_win32 *)stream;
|
||||
if (win32->handle == NULL || win32->handle == INVALID_HANDLE_VALUE)
|
||||
return MZ_STREAM_ERROR;
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_stream_win32_read(void *stream, void *buf, int32_t size)
|
||||
int32_t mz_stream_os_is_open(void *stream)
|
||||
{
|
||||
mz_stream_win32 *win32 = (mz_stream_win32 *)stream;
|
||||
if (win32->handle == NULL || win32->handle == INVALID_HANDLE_VALUE)
|
||||
return MZ_OPEN_ERROR;
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_stream_os_read(void *stream, void *buf, int32_t size)
|
||||
{
|
||||
mz_stream_win32 *win32 = (mz_stream_win32 *)stream;
|
||||
uint32_t read = 0;
|
||||
|
||||
if (mz_stream_win32_is_open(stream) != MZ_OK)
|
||||
return MZ_STREAM_ERROR;
|
||||
if (mz_stream_os_is_open(stream) != MZ_OK)
|
||||
return MZ_OPEN_ERROR;
|
||||
|
||||
if (!ReadFile(win32->handle, buf, size, (DWORD *)&read, NULL))
|
||||
{
|
||||
@@ -151,13 +156,13 @@ int32_t mz_stream_win32_read(void *stream, void *buf, int32_t size)
|
||||
return read;
|
||||
}
|
||||
|
||||
int32_t mz_stream_win32_write(void *stream, const void *buf, int32_t size)
|
||||
int32_t mz_stream_os_write(void *stream, const void *buf, int32_t size)
|
||||
{
|
||||
mz_stream_win32 *win32 = (mz_stream_win32 *)stream;
|
||||
uint32_t written = 0;
|
||||
int32_t written = 0;
|
||||
|
||||
if (mz_stream_win32_is_open(stream) != MZ_OK)
|
||||
return MZ_STREAM_ERROR;
|
||||
if (mz_stream_os_is_open(stream) != MZ_OK)
|
||||
return MZ_OPEN_ERROR;
|
||||
|
||||
if (!WriteFile(win32->handle, buf, size, (DWORD *)&written, NULL))
|
||||
{
|
||||
@@ -169,16 +174,20 @@ int32_t mz_stream_win32_write(void *stream, const void *buf, int32_t size)
|
||||
return written;
|
||||
}
|
||||
|
||||
static int32_t mz_stream_win32_seekinternal(HANDLE handle, LARGE_INTEGER large_pos, LARGE_INTEGER *new_pos, uint32_t move_method)
|
||||
static int32_t mz_stream_os_seekinternal(HANDLE handle, LARGE_INTEGER large_pos,
|
||||
LARGE_INTEGER *new_pos, uint32_t move_method)
|
||||
{
|
||||
#ifdef MZ_USE_WINRT_API
|
||||
#ifdef MZ_WINRT_API
|
||||
return SetFilePointerEx(handle, pos, newPos, dwMoveMethod);
|
||||
#else
|
||||
LONG high_part = large_pos.HighPart;
|
||||
uint32_t pos = SetFilePointer(handle, large_pos.LowPart, &high_part, move_method);
|
||||
LONG high_part = 0;
|
||||
uint32_t pos = 0;
|
||||
|
||||
high_part = large_pos.HighPart;
|
||||
pos = SetFilePointer(handle, large_pos.LowPart, &high_part, move_method);
|
||||
|
||||
if ((pos == INVALID_SET_FILE_POINTER) && (GetLastError() != NO_ERROR))
|
||||
return MZ_STREAM_ERROR;
|
||||
return MZ_SEEK_ERROR;
|
||||
|
||||
if (new_pos != NULL)
|
||||
{
|
||||
@@ -190,31 +199,32 @@ static int32_t mz_stream_win32_seekinternal(HANDLE handle, LARGE_INTEGER large_p
|
||||
#endif
|
||||
}
|
||||
|
||||
int64_t mz_stream_win32_tell(void *stream)
|
||||
int64_t mz_stream_os_tell(void *stream)
|
||||
{
|
||||
mz_stream_win32 *win32 = (mz_stream_win32 *)stream;
|
||||
LARGE_INTEGER large_pos;
|
||||
|
||||
if (mz_stream_win32_is_open(stream) != MZ_OK)
|
||||
return MZ_STREAM_ERROR;
|
||||
if (mz_stream_os_is_open(stream) != MZ_OK)
|
||||
return MZ_OPEN_ERROR;
|
||||
|
||||
large_pos.QuadPart = 0;
|
||||
|
||||
if (mz_stream_win32_seekinternal(win32->handle, large_pos, &large_pos, FILE_CURRENT) != MZ_OK)
|
||||
if (mz_stream_os_seekinternal(win32->handle, large_pos, &large_pos, FILE_CURRENT) != MZ_OK)
|
||||
win32->error = GetLastError();
|
||||
|
||||
return large_pos.QuadPart;
|
||||
}
|
||||
|
||||
int32_t mz_stream_win32_seek(void *stream, int64_t offset, int32_t origin)
|
||||
int32_t mz_stream_os_seek(void *stream, int64_t offset, int32_t origin)
|
||||
{
|
||||
mz_stream_win32 *win32 = (mz_stream_win32 *)stream;
|
||||
uint32_t move_method = 0xFFFFFFFF;
|
||||
int32_t err = MZ_OK;
|
||||
LARGE_INTEGER large_pos;
|
||||
|
||||
|
||||
if (mz_stream_win32_is_open(stream) == MZ_STREAM_ERROR)
|
||||
return MZ_STREAM_ERROR;
|
||||
if (mz_stream_os_is_open(stream) != MZ_OK)
|
||||
return MZ_OPEN_ERROR;
|
||||
|
||||
switch (origin)
|
||||
{
|
||||
@@ -228,21 +238,22 @@ int32_t mz_stream_win32_seek(void *stream, int64_t offset, int32_t origin)
|
||||
move_method = FILE_BEGIN;
|
||||
break;
|
||||
default:
|
||||
return MZ_STREAM_ERROR;
|
||||
return MZ_SEEK_ERROR;
|
||||
}
|
||||
|
||||
large_pos.QuadPart = offset;
|
||||
|
||||
if (mz_stream_win32_seekinternal(win32->handle, large_pos, NULL, move_method) != MZ_OK)
|
||||
err = mz_stream_os_seekinternal(win32->handle, large_pos, NULL, move_method);
|
||||
if (err != MZ_OK)
|
||||
{
|
||||
win32->error = GetLastError();
|
||||
return MZ_STREAM_ERROR;
|
||||
return err;
|
||||
}
|
||||
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int mz_stream_win32_close(void *stream)
|
||||
int32_t mz_stream_os_close(void *stream)
|
||||
{
|
||||
mz_stream_win32 *win32 = (mz_stream_win32 *)stream;
|
||||
|
||||
@@ -252,21 +263,21 @@ int mz_stream_win32_close(void *stream)
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int mz_stream_win32_error(void *stream)
|
||||
int32_t mz_stream_os_error(void *stream)
|
||||
{
|
||||
mz_stream_win32 *win32 = (mz_stream_win32 *)stream;
|
||||
return win32->error;
|
||||
}
|
||||
|
||||
void *mz_stream_win32_create(void **stream)
|
||||
void *mz_stream_os_create(void **stream)
|
||||
{
|
||||
mz_stream_win32 *win32 = NULL;
|
||||
|
||||
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));
|
||||
win32->stream.vtbl = &mz_stream_win32_vtbl;
|
||||
win32->stream.vtbl = &mz_stream_os_vtbl;
|
||||
}
|
||||
if (stream != NULL)
|
||||
*stream = win32;
|
||||
@@ -274,18 +285,18 @@ void *mz_stream_win32_create(void **stream)
|
||||
return win32;
|
||||
}
|
||||
|
||||
void mz_stream_win32_delete(void **stream)
|
||||
void mz_stream_os_delete(void **stream)
|
||||
{
|
||||
mz_stream_win32 *win32 = NULL;
|
||||
if (stream == NULL)
|
||||
return;
|
||||
win32 = (mz_stream_win32 *)*stream;
|
||||
if (win32 != NULL)
|
||||
free(win32);
|
||||
MZ_FREE(win32);
|
||||
*stream = NULL;
|
||||
}
|
||||
|
||||
void *mz_stream_win32_get_interface(void)
|
||||
void *mz_stream_os_get_interface(void)
|
||||
{
|
||||
return (void *)&mz_stream_win32_vtbl;
|
||||
}
|
||||
return (void *)&mz_stream_os_vtbl;
|
||||
}
|
||||
@@ -0,0 +1,353 @@
|
||||
/* mz_strm_pkcrypt.c -- Code for traditional PKWARE encryption
|
||||
Version 2.7.3, November 4, 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
|
||||
https://www.winimage.com/zLibDll/minizip.html
|
||||
Copyright (C) 2003 Terry Thorsen
|
||||
|
||||
This code is a modified version of crypting code in Info-ZIP distribution
|
||||
|
||||
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 <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "mz.h"
|
||||
#include "mz_crypt.h"
|
||||
#include "mz_os.h"
|
||||
#include "mz_strm.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_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, (uint8_t)c), (uint8_t)(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)~mz_crypt_crc32_update(~pkcrypt->keys[0], &buf, 1);
|
||||
|
||||
pkcrypt->keys[1] += pkcrypt->keys[0] & 0xff;
|
||||
pkcrypt->keys[1] *= 134775813L;
|
||||
pkcrypt->keys[1] += 1;
|
||||
|
||||
buf = (uint8_t)(pkcrypt->keys[1] >> 24);
|
||||
pkcrypt->keys[2] = (uint32_t)~mz_crypt_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, (uint8_t)*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_OPEN_ERROR;
|
||||
|
||||
if (password == NULL)
|
||||
password = pkcrypt->password;
|
||||
if (password == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
mz_stream_pkcrypt_init_keys(stream, password);
|
||||
|
||||
if (mode & MZ_OPEN_MODE_WRITE)
|
||||
{
|
||||
#ifdef MZ_ZIP_NO_COMPRESSION
|
||||
MZ_UNUSED(t);
|
||||
MZ_UNUSED(i);
|
||||
|
||||
return MZ_SUPPORT_ERROR;
|
||||
#else
|
||||
// First generate RAND_HEAD_LEN - 2 random bytes.
|
||||
mz_crypt_rand(header, RAND_HEAD_LEN - 2);
|
||||
|
||||
// Encrypt random header (last two bytes is high word of crc)
|
||||
for (i = 0; i < RAND_HEAD_LEN - 2; i++)
|
||||
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_WRITE_ERROR;
|
||||
|
||||
pkcrypt->total_out += RAND_HEAD_LEN;
|
||||
#endif
|
||||
}
|
||||
else if (mode & MZ_OPEN_MODE_READ)
|
||||
{
|
||||
#ifdef MZ_ZIP_NO_DECOMPRESSION
|
||||
MZ_UNUSED(t);
|
||||
MZ_UNUSED(i);
|
||||
MZ_UNUSED(verify1);
|
||||
MZ_UNUSED(verify2);
|
||||
|
||||
return MZ_SUPPORT_ERROR;
|
||||
#else
|
||||
if (mz_stream_read(pkcrypt->stream.base, header, RAND_HEAD_LEN) != RAND_HEAD_LEN)
|
||||
return MZ_READ_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_OPEN_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_BUF_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;
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/* mz_strm_pkcrypt.h -- Code for traditional PKWARE encryption
|
||||
Version 2.7.3, November 4, 2018
|
||||
part of the MiniZip project
|
||||
|
||||
Copyright (C) 2010-2018 Nathan Moinvaziri
|
||||
https://github.com/nmoinvaz/minizip
|
||||
|
||||
This program is distributed under the terms of the same license as zlib.
|
||||
See the accompanying LICENSE file for the full text of the license.
|
||||
*/
|
||||
|
||||
#ifndef MZ_STREAM_PKCRYPT_H
|
||||
#define MZ_STREAM_PKCRYPT_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t mz_stream_pkcrypt_open(void *stream, const char *filename, int32_t mode);
|
||||
int32_t mz_stream_pkcrypt_is_open(void *stream);
|
||||
int32_t mz_stream_pkcrypt_read(void *stream, void *buf, int32_t size);
|
||||
int32_t mz_stream_pkcrypt_write(void *stream, const void *buf, int32_t size);
|
||||
int64_t mz_stream_pkcrypt_tell(void *stream);
|
||||
int32_t mz_stream_pkcrypt_seek(void *stream, int64_t offset, int32_t origin);
|
||||
int32_t mz_stream_pkcrypt_close(void *stream);
|
||||
int32_t mz_stream_pkcrypt_error(void *stream);
|
||||
|
||||
void mz_stream_pkcrypt_set_password(void *stream, const char *password);
|
||||
void mz_stream_pkcrypt_set_verify(void *stream, uint8_t verify1, uint8_t verify2);
|
||||
void mz_stream_pkcrypt_get_verify(void *stream, uint8_t *verify1, uint8_t *verify2);
|
||||
int32_t mz_stream_pkcrypt_get_prop_int64(void *stream, int32_t prop, int64_t *value);
|
||||
|
||||
void* mz_stream_pkcrypt_create(void **stream);
|
||||
void mz_stream_pkcrypt_delete(void **stream);
|
||||
|
||||
void* mz_stream_pkcrypt_get_interface(void);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,5 +1,5 @@
|
||||
/* mz_strm_split.c -- Stream for split files
|
||||
Version 2.2.7, January 30th, 2018
|
||||
Version 2.7.3, November 4, 2018
|
||||
part of the MiniZip project
|
||||
|
||||
Copyright (C) 2010-2018 Nathan Moinvaziri
|
||||
@@ -9,10 +9,10 @@
|
||||
See the accompanying LICENSE file for the full text of the license.
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
#include "mz.h"
|
||||
#include "mz_os.h"
|
||||
@@ -29,7 +29,7 @@
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
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,
|
||||
@@ -56,9 +56,9 @@ typedef struct mz_stream_split_s {
|
||||
int64_t total_out_disk;
|
||||
int32_t mode;
|
||||
char *path_cd;
|
||||
int32_t path_cd_size;
|
||||
uint32_t path_cd_size;
|
||||
char *path_disk;
|
||||
int32_t path_disk_size;
|
||||
uint32_t path_disk_size;
|
||||
int32_t number_disk;
|
||||
int32_t current_disk;
|
||||
int32_t reached_end;
|
||||
@@ -87,17 +87,19 @@ static int32_t mz_stream_split_open_disk(void *stream, int32_t number_disk)
|
||||
// Construct disk path
|
||||
if (disk_part > 0)
|
||||
{
|
||||
for (i = strlen(split->path_disk) - 1; i >= 0; i -= 1)
|
||||
for (i = (int32_t)strlen(split->path_disk) - 1; i >= 0; i -= 1)
|
||||
{
|
||||
if (split->path_disk[i] != '.')
|
||||
continue;
|
||||
snprintf(&split->path_disk[i], split->path_disk_size - i, ".z%02d", number_disk + 1);
|
||||
snprintf(&split->path_disk[i], split->path_disk_size - (uint32_t)i,
|
||||
".z%02"PRId32, number_disk + 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
strncpy(split->path_disk, split->path_cd, split->path_disk_size);
|
||||
strncpy(split->path_disk, split->path_cd, split->path_disk_size - 1);
|
||||
split->path_disk[split->path_disk_size - 1] = 0;
|
||||
}
|
||||
|
||||
// If disk part doesn't exist during reading then return MZ_EXIST_ERROR
|
||||
@@ -180,13 +182,26 @@ int32_t mz_stream_split_open(void *stream, const char *path, int32_t mode)
|
||||
|
||||
split->mode = mode;
|
||||
|
||||
split->path_cd_size = strlen(path) + 1;
|
||||
split->path_cd = (char *)malloc(split->path_cd_size);
|
||||
strncpy(split->path_cd, path, split->path_cd_size);
|
||||
split->path_cd_size = (uint32_t)strlen(path) + 1;
|
||||
split->path_cd = (char *)MZ_ALLOC(split->path_cd_size);
|
||||
|
||||
split->path_disk_size = strlen(path) + 10;
|
||||
split->path_disk = (char *)malloc(split->path_disk_size);
|
||||
strncpy(split->path_disk, path, split->path_disk_size);
|
||||
if (split->path_cd == NULL)
|
||||
return MZ_MEM_ERROR;
|
||||
|
||||
strncpy(split->path_cd, path, split->path_cd_size - 1);
|
||||
split->path_cd[split->path_cd_size - 1] = 0;
|
||||
|
||||
split->path_disk_size = (uint32_t)strlen(path) + 10;
|
||||
split->path_disk = (char *)MZ_ALLOC(split->path_disk_size);
|
||||
|
||||
if (split->path_disk == NULL)
|
||||
{
|
||||
MZ_FREE(split->path_cd);
|
||||
return MZ_MEM_ERROR;
|
||||
}
|
||||
|
||||
strncpy(split->path_disk, path, split->path_disk_size - 1);
|
||||
split->path_disk[split->path_disk_size - 1] = 0;
|
||||
|
||||
if (mode & MZ_OPEN_MODE_WRITE)
|
||||
{
|
||||
@@ -205,9 +220,9 @@ int32_t mz_stream_split_open(void *stream, const char *path, int32_t mode)
|
||||
int32_t mz_stream_split_is_open(void *stream)
|
||||
{
|
||||
mz_stream_split *split = (mz_stream_split *)stream;
|
||||
if (split->is_open == 1)
|
||||
return MZ_OK;
|
||||
return MZ_STREAM_ERROR;
|
||||
if (split->is_open != 1)
|
||||
return MZ_OPEN_ERROR;
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_stream_split_read(void *stream, void *buf, int32_t size)
|
||||
@@ -255,7 +270,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)
|
||||
{
|
||||
@@ -281,11 +296,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;
|
||||
|
||||
return MZ_WRITE_ERROR;
|
||||
|
||||
bytes_left -= written;
|
||||
buf_ptr += written;
|
||||
split->total_out += written;
|
||||
@@ -338,15 +353,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)
|
||||
@@ -356,19 +373,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));
|
||||
@@ -389,11 +408,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;
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/* mz_strm_split.h -- Stream for split files
|
||||
Version 2.2.7, January 30th, 2018
|
||||
Version 2.7.3, November 4, 2018
|
||||
part of MiniZip project
|
||||
|
||||
Copyright (C) 2010-2018 Nathan Moinvaziri
|
||||
@@ -9,8 +9,8 @@
|
||||
See the accompanying LICENSE file for the full text of the license.
|
||||
*/
|
||||
|
||||
#ifndef _MZ_STREAM_SPLIT_H
|
||||
#define _MZ_STREAM_SPLIT_H
|
||||
#ifndef MZ_STREAM_SPLIT_H
|
||||
#define MZ_STREAM_SPLIT_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
+396
@@ -0,0 +1,396 @@
|
||||
/* mz_strm_wzaes.c -- Stream for WinZip AES encryption
|
||||
Version 2.7.3, November 4, 2018
|
||||
part of the MiniZip project
|
||||
|
||||
Copyright (C) 2010-2018 Nathan Moinvaziri
|
||||
https://github.com/nmoinvaz/minizip
|
||||
Copyright (C) 1998-2010 Brian Gladman, Worcester, UK
|
||||
|
||||
This program is distributed under the terms of the same license as zlib.
|
||||
See the accompanying LICENSE file for the full text of the license.
|
||||
*/
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "mz.h"
|
||||
#include "mz_crypt.h"
|
||||
#include "mz_os.h"
|
||||
#include "mz_strm.h"
|
||||
#include "mz_strm_wzaes.h"
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#define MZ_AES_KEYING_ITERATIONS (1000)
|
||||
#define MZ_AES_SALT_LENGTH(MODE) (4 * (MODE & 3) + 4)
|
||||
#define MZ_AES_SALT_LENGTH_MAX (16)
|
||||
#define MZ_AES_PW_LENGTH_MAX (128)
|
||||
#define MZ_AES_PW_VERIFY_SIZE (2)
|
||||
#define MZ_AES_AUTHCODE_SIZE (10)
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
static mz_stream_vtbl mz_stream_wzaes_vtbl = {
|
||||
mz_stream_wzaes_open,
|
||||
mz_stream_wzaes_is_open,
|
||||
mz_stream_wzaes_read,
|
||||
mz_stream_wzaes_write,
|
||||
mz_stream_wzaes_tell,
|
||||
mz_stream_wzaes_seek,
|
||||
mz_stream_wzaes_close,
|
||||
mz_stream_wzaes_error,
|
||||
mz_stream_wzaes_create,
|
||||
mz_stream_wzaes_delete,
|
||||
mz_stream_wzaes_get_prop_int64,
|
||||
mz_stream_wzaes_set_prop_int64
|
||||
};
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct mz_stream_wzaes_s {
|
||||
mz_stream stream;
|
||||
int32_t mode;
|
||||
int32_t error;
|
||||
int16_t initialized;
|
||||
uint8_t buffer[UINT16_MAX];
|
||||
int64_t total_in;
|
||||
int64_t max_total_in;
|
||||
int64_t total_out;
|
||||
int16_t encryption_mode;
|
||||
const char *password;
|
||||
void *aes;
|
||||
uint32_t crypt_pos;
|
||||
uint8_t crypt_block[MZ_AES_BLOCK_SIZE];
|
||||
void *hmac;
|
||||
uint8_t nonce[MZ_AES_BLOCK_SIZE];
|
||||
} mz_stream_wzaes;
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t mz_stream_wzaes_open(void *stream, const char *path, int32_t mode)
|
||||
{
|
||||
mz_stream_wzaes *wzaes = (mz_stream_wzaes *)stream;
|
||||
uint16_t salt_length = 0;
|
||||
uint16_t password_length = 0;
|
||||
uint16_t key_length = 0;
|
||||
uint8_t kbuf[2 * MZ_AES_KEY_LENGTH_MAX + MZ_AES_PW_VERIFY_SIZE];
|
||||
uint8_t verify[MZ_AES_PW_VERIFY_SIZE];
|
||||
uint8_t verify_expected[MZ_AES_PW_VERIFY_SIZE];
|
||||
uint8_t salt_value[MZ_AES_SALT_LENGTH_MAX];
|
||||
const char *password = path;
|
||||
|
||||
wzaes->total_in = 0;
|
||||
wzaes->total_out = 0;
|
||||
wzaes->initialized = 0;
|
||||
|
||||
if (mz_stream_is_open(wzaes->stream.base) != MZ_OK)
|
||||
return MZ_OPEN_ERROR;
|
||||
|
||||
if (password == NULL)
|
||||
password = wzaes->password;
|
||||
if (password == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
password_length = (uint16_t)strlen(password);
|
||||
if (password_length > MZ_AES_PW_LENGTH_MAX)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
if (wzaes->encryption_mode < 1 || wzaes->encryption_mode > 3)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
salt_length = MZ_AES_SALT_LENGTH(wzaes->encryption_mode);
|
||||
|
||||
if (mode & MZ_OPEN_MODE_WRITE)
|
||||
{
|
||||
#ifdef MZ_ZIP_NO_COMPRESSION
|
||||
return MZ_SUPPORT_ERROR;
|
||||
#else
|
||||
mz_crypt_rand(salt_value, salt_length);
|
||||
#endif
|
||||
}
|
||||
else if (mode & MZ_OPEN_MODE_READ)
|
||||
{
|
||||
#ifdef MZ_ZIP_NO_DECOMPRESSION
|
||||
return MZ_SUPPORT_ERROR;
|
||||
#else
|
||||
if (mz_stream_read(wzaes->stream.base, salt_value, salt_length) != salt_length)
|
||||
return MZ_READ_ERROR;
|
||||
#endif
|
||||
}
|
||||
|
||||
key_length = MZ_AES_KEY_LENGTH(wzaes->encryption_mode);
|
||||
|
||||
// Derive the encryption and authentication keys and the password verifier
|
||||
mz_crypt_pbkdf2((uint8_t *)password, password_length, salt_value, salt_length,
|
||||
MZ_AES_KEYING_ITERATIONS, kbuf, 2 * key_length + MZ_AES_PW_VERIFY_SIZE);
|
||||
|
||||
// Initialize the encryption nonce and buffer pos
|
||||
wzaes->crypt_pos = MZ_AES_BLOCK_SIZE;
|
||||
memset(wzaes->nonce, 0, sizeof(wzaes->nonce));
|
||||
|
||||
// Initialize for encryption using key 1
|
||||
mz_crypt_aes_reset(wzaes->aes);
|
||||
mz_crypt_aes_set_mode(wzaes->aes, wzaes->encryption_mode);
|
||||
mz_crypt_aes_set_encrypt_key(wzaes->aes, kbuf, key_length);
|
||||
|
||||
// Initialize for authentication using key 2
|
||||
mz_crypt_hmac_reset(wzaes->hmac);
|
||||
mz_crypt_hmac_set_algorithm(wzaes->hmac, MZ_HASH_SHA1);
|
||||
mz_crypt_hmac_init(wzaes->hmac, kbuf + key_length, key_length);
|
||||
|
||||
memcpy(verify, kbuf + (2 * key_length), MZ_AES_PW_VERIFY_SIZE);
|
||||
|
||||
if (mode & MZ_OPEN_MODE_WRITE)
|
||||
{
|
||||
if (mz_stream_write(wzaes->stream.base, salt_value, salt_length) != salt_length)
|
||||
return MZ_WRITE_ERROR;
|
||||
|
||||
wzaes->total_out += salt_length;
|
||||
|
||||
if (mz_stream_write(wzaes->stream.base, verify, MZ_AES_PW_VERIFY_SIZE) != MZ_AES_PW_VERIFY_SIZE)
|
||||
return MZ_WRITE_ERROR;
|
||||
|
||||
wzaes->total_out += MZ_AES_PW_VERIFY_SIZE;
|
||||
}
|
||||
else if (mode & MZ_OPEN_MODE_READ)
|
||||
{
|
||||
wzaes->total_in += salt_length;
|
||||
|
||||
if (mz_stream_read(wzaes->stream.base, verify_expected, MZ_AES_PW_VERIFY_SIZE) != MZ_AES_PW_VERIFY_SIZE)
|
||||
return MZ_READ_ERROR;
|
||||
|
||||
wzaes->total_in += MZ_AES_PW_VERIFY_SIZE;
|
||||
|
||||
if (memcmp(verify_expected, verify, MZ_AES_PW_VERIFY_SIZE) != 0)
|
||||
return MZ_PASSWORD_ERROR;
|
||||
}
|
||||
|
||||
wzaes->mode = mode;
|
||||
wzaes->initialized = 1;
|
||||
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_stream_wzaes_is_open(void *stream)
|
||||
{
|
||||
mz_stream_wzaes *wzaes = (mz_stream_wzaes *)stream;
|
||||
if (wzaes->initialized == 0)
|
||||
return MZ_OPEN_ERROR;
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
static int32_t mz_stream_wzaes_encrypt_data(void *stream, uint8_t *buf, int32_t size)
|
||||
{
|
||||
mz_stream_wzaes *wzaes = (mz_stream_wzaes *)stream;
|
||||
uint32_t pos = wzaes->crypt_pos;
|
||||
uint32_t i = 0;
|
||||
int32_t err = MZ_OK;
|
||||
|
||||
while (i < (uint32_t)size)
|
||||
{
|
||||
if (pos == MZ_AES_BLOCK_SIZE)
|
||||
{
|
||||
uint32_t j = 0;
|
||||
|
||||
// Increment encryption nonce
|
||||
while (j < 8 && !++wzaes->nonce[j])
|
||||
j += 1;
|
||||
|
||||
// Encrypt the nonce to form next xor buffer
|
||||
memcpy(wzaes->crypt_block, wzaes->nonce, MZ_AES_BLOCK_SIZE);
|
||||
mz_crypt_aes_encrypt(wzaes->aes, wzaes->crypt_block, sizeof(wzaes->crypt_block));
|
||||
pos = 0;
|
||||
}
|
||||
|
||||
buf[i++] ^= wzaes->crypt_block[pos++];
|
||||
}
|
||||
|
||||
wzaes->crypt_pos = pos;
|
||||
return err;
|
||||
}
|
||||
|
||||
int32_t mz_stream_wzaes_read(void *stream, void *buf, int32_t size)
|
||||
{
|
||||
mz_stream_wzaes *wzaes = (mz_stream_wzaes *)stream;
|
||||
int64_t max_total_in = 0;
|
||||
int32_t bytes_to_read = size;
|
||||
int32_t read = 0;
|
||||
|
||||
max_total_in = wzaes->max_total_in - MZ_AES_FOOTER_SIZE;
|
||||
if (bytes_to_read + wzaes->total_in > max_total_in)
|
||||
bytes_to_read = (int32_t)(max_total_in - wzaes->total_in);
|
||||
|
||||
read = mz_stream_read(wzaes->stream.base, buf, bytes_to_read);
|
||||
|
||||
if (read > 0)
|
||||
{
|
||||
mz_crypt_hmac_update(wzaes->hmac, (uint8_t *)buf, read);
|
||||
mz_stream_wzaes_encrypt_data(stream, (uint8_t *)buf, read);
|
||||
}
|
||||
|
||||
wzaes->total_in += read;
|
||||
return read;
|
||||
}
|
||||
|
||||
int32_t mz_stream_wzaes_write(void *stream, const void *buf, int32_t size)
|
||||
{
|
||||
mz_stream_wzaes *wzaes = (mz_stream_wzaes *)stream;
|
||||
int32_t written = 0;
|
||||
|
||||
if (size < 0)
|
||||
return MZ_PARAM_ERROR;
|
||||
if (size > (int32_t)sizeof(wzaes->buffer))
|
||||
return MZ_BUF_ERROR;
|
||||
|
||||
memcpy(wzaes->buffer, buf, size);
|
||||
mz_stream_wzaes_encrypt_data(stream, (uint8_t *)wzaes->buffer, size);
|
||||
mz_crypt_hmac_update(wzaes->hmac, wzaes->buffer, size);
|
||||
|
||||
written = mz_stream_write(wzaes->stream.base, wzaes->buffer, size);
|
||||
if (written > 0)
|
||||
wzaes->total_out += written;
|
||||
return written;
|
||||
}
|
||||
|
||||
int64_t mz_stream_wzaes_tell(void *stream)
|
||||
{
|
||||
mz_stream_wzaes *wzaes = (mz_stream_wzaes *)stream;
|
||||
return mz_stream_tell(wzaes->stream.base);
|
||||
}
|
||||
|
||||
int32_t mz_stream_wzaes_seek(void *stream, int64_t offset, int32_t origin)
|
||||
{
|
||||
mz_stream_wzaes *wzaes = (mz_stream_wzaes *)stream;
|
||||
return mz_stream_seek(wzaes->stream.base, offset, origin);
|
||||
}
|
||||
|
||||
int32_t mz_stream_wzaes_close(void *stream)
|
||||
{
|
||||
mz_stream_wzaes *wzaes = (mz_stream_wzaes *)stream;
|
||||
uint8_t expected_hash[MZ_AES_AUTHCODE_SIZE];
|
||||
uint8_t computed_hash[MZ_HASH_SHA1_SIZE];
|
||||
|
||||
mz_crypt_hmac_end(wzaes->hmac, computed_hash, sizeof(computed_hash));
|
||||
|
||||
if (wzaes->mode & MZ_OPEN_MODE_WRITE)
|
||||
{
|
||||
if (mz_stream_write(wzaes->stream.base, computed_hash, MZ_AES_AUTHCODE_SIZE) != MZ_AES_AUTHCODE_SIZE)
|
||||
return MZ_WRITE_ERROR;
|
||||
|
||||
wzaes->total_out += MZ_AES_AUTHCODE_SIZE;
|
||||
}
|
||||
else if (wzaes->mode & MZ_OPEN_MODE_READ)
|
||||
{
|
||||
if (mz_stream_read(wzaes->stream.base, expected_hash, MZ_AES_AUTHCODE_SIZE) != MZ_AES_AUTHCODE_SIZE)
|
||||
return MZ_READ_ERROR;
|
||||
|
||||
wzaes->total_in += MZ_AES_AUTHCODE_SIZE;
|
||||
|
||||
// If entire entry was not read this will fail
|
||||
if (memcmp(computed_hash, expected_hash, MZ_AES_AUTHCODE_SIZE) != 0)
|
||||
return MZ_CRC_ERROR;
|
||||
}
|
||||
|
||||
wzaes->initialized = 0;
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_stream_wzaes_error(void *stream)
|
||||
{
|
||||
mz_stream_wzaes *wzaes = (mz_stream_wzaes *)stream;
|
||||
return wzaes->error;
|
||||
}
|
||||
|
||||
void mz_stream_wzaes_set_password(void *stream, const char *password)
|
||||
{
|
||||
mz_stream_wzaes *wzaes = (mz_stream_wzaes *)stream;
|
||||
wzaes->password = password;
|
||||
}
|
||||
|
||||
void mz_stream_wzaes_set_encryption_mode(void *stream, int16_t encryption_mode)
|
||||
{
|
||||
mz_stream_wzaes *wzaes = (mz_stream_wzaes *)stream;
|
||||
wzaes->encryption_mode = encryption_mode;
|
||||
}
|
||||
|
||||
int32_t mz_stream_wzaes_get_prop_int64(void *stream, int32_t prop, int64_t *value)
|
||||
{
|
||||
mz_stream_wzaes *wzaes = (mz_stream_wzaes *)stream;
|
||||
switch (prop)
|
||||
{
|
||||
case MZ_STREAM_PROP_TOTAL_IN:
|
||||
*value = wzaes->total_in;
|
||||
break;
|
||||
case MZ_STREAM_PROP_TOTAL_OUT:
|
||||
*value = wzaes->total_out;
|
||||
break;
|
||||
case MZ_STREAM_PROP_TOTAL_IN_MAX:
|
||||
*value = wzaes->max_total_in;
|
||||
break;
|
||||
case MZ_STREAM_PROP_HEADER_SIZE:
|
||||
*value = MZ_AES_SALT_LENGTH((int64_t)wzaes->encryption_mode) + MZ_AES_PW_VERIFY_SIZE;
|
||||
break;
|
||||
case MZ_STREAM_PROP_FOOTER_SIZE:
|
||||
*value = MZ_AES_AUTHCODE_SIZE;
|
||||
break;
|
||||
default:
|
||||
return MZ_EXIST_ERROR;
|
||||
}
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_stream_wzaes_set_prop_int64(void *stream, int32_t prop, int64_t value)
|
||||
{
|
||||
mz_stream_wzaes *wzaes = (mz_stream_wzaes *)stream;
|
||||
switch (prop)
|
||||
{
|
||||
case MZ_STREAM_PROP_TOTAL_IN_MAX:
|
||||
wzaes->max_total_in = value;
|
||||
break;
|
||||
default:
|
||||
return MZ_EXIST_ERROR;
|
||||
}
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
void *mz_stream_wzaes_create(void **stream)
|
||||
{
|
||||
mz_stream_wzaes *wzaes = NULL;
|
||||
|
||||
wzaes = (mz_stream_wzaes *)MZ_ALLOC(sizeof(mz_stream_wzaes));
|
||||
if (wzaes != NULL)
|
||||
{
|
||||
memset(wzaes, 0, sizeof(mz_stream_wzaes));
|
||||
wzaes->stream.vtbl = &mz_stream_wzaes_vtbl;
|
||||
wzaes->encryption_mode = MZ_AES_ENCRYPTION_MODE_256;
|
||||
|
||||
mz_crypt_hmac_create(&wzaes->hmac);
|
||||
mz_crypt_aes_create(&wzaes->aes);
|
||||
}
|
||||
if (stream != NULL)
|
||||
*stream = wzaes;
|
||||
|
||||
return wzaes;
|
||||
}
|
||||
|
||||
void mz_stream_wzaes_delete(void **stream)
|
||||
{
|
||||
mz_stream_wzaes *wzaes = NULL;
|
||||
if (stream == NULL)
|
||||
return;
|
||||
wzaes = (mz_stream_wzaes *)*stream;
|
||||
if (wzaes != NULL)
|
||||
{
|
||||
mz_crypt_aes_delete(&wzaes->aes);
|
||||
mz_crypt_hmac_delete(&wzaes->hmac);
|
||||
MZ_FREE(wzaes);
|
||||
}
|
||||
*stream = NULL;
|
||||
}
|
||||
|
||||
void *mz_stream_wzaes_get_interface(void)
|
||||
{
|
||||
return (void *)&mz_stream_wzaes_vtbl;
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/* mz_strm_wzaes.h -- Stream for WinZIP AES encryption
|
||||
Version 2.7.3, November 4, 2018
|
||||
part of the MiniZip project
|
||||
|
||||
Copyright (C) 2010-2018 Nathan Moinvaziri
|
||||
https://github.com/nmoinvaz/minizip
|
||||
|
||||
This program is distributed under the terms of the same license as zlib.
|
||||
See the accompanying LICENSE file for the full text of the license.
|
||||
*/
|
||||
|
||||
#ifndef MZ_STREAM_WZAES_SHA1_H
|
||||
#define MZ_STREAM_WZAES_SHA1_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t mz_stream_wzaes_open(void *stream, const char *filename, int32_t mode);
|
||||
int32_t mz_stream_wzaes_is_open(void *stream);
|
||||
int32_t mz_stream_wzaes_read(void *stream, void *buf, int32_t size);
|
||||
int32_t mz_stream_wzaes_write(void *stream, const void *buf, int32_t size);
|
||||
int64_t mz_stream_wzaes_tell(void *stream);
|
||||
int32_t mz_stream_wzaes_seek(void *stream, int64_t offset, int32_t origin);
|
||||
int32_t mz_stream_wzaes_close(void *stream);
|
||||
int32_t mz_stream_wzaes_error(void *stream);
|
||||
|
||||
void mz_stream_wzaes_set_password(void *stream, const char *password);
|
||||
void mz_stream_wzaes_set_encryption_mode(void *stream, int16_t encryption_mode);
|
||||
|
||||
int32_t mz_stream_wzaes_get_prop_int64(void *stream, int32_t prop, int64_t *value);
|
||||
int32_t mz_stream_wzaes_set_prop_int64(void *stream, int32_t prop, int64_t value);
|
||||
|
||||
void* mz_stream_wzaes_create(void **stream);
|
||||
void mz_stream_wzaes_delete(void **stream);
|
||||
|
||||
void* mz_stream_wzaes_get_interface(void);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,5 +1,5 @@
|
||||
/* mz_strm_zlib.c -- Stream for zlib inflate/deflate
|
||||
Version 2.2.7, January 30th, 2018
|
||||
Version 2.7.3, November 4, 2018
|
||||
part of the MiniZip project
|
||||
|
||||
Copyright (C) 2010-2018 Nathan Moinvaziri
|
||||
@@ -10,7 +10,6 @@
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
@@ -33,7 +32,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 +69,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,21 +83,30 @@ 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);
|
||||
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)
|
||||
return MZ_STREAM_ERROR;
|
||||
return MZ_OPEN_ERROR;
|
||||
|
||||
zlib->initialized = 1;
|
||||
zlib->mode = mode;
|
||||
@@ -108,12 +117,15 @@ int32_t mz_stream_zlib_is_open(void *stream)
|
||||
{
|
||||
mz_stream_zlib *zlib = (mz_stream_zlib *)stream;
|
||||
if (zlib->initialized != 1)
|
||||
return MZ_STREAM_ERROR;
|
||||
return MZ_OPEN_ERROR;
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_stream_zlib_read(void *stream, void *buf, int32_t size)
|
||||
{
|
||||
#ifdef MZ_ZIP_NO_DECOMPRESSION
|
||||
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;
|
||||
@@ -138,17 +150,15 @@ 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);
|
||||
}
|
||||
|
||||
read = mz_stream_read(zlib->stream.base, zlib->buffer, bytes_to_read);
|
||||
|
||||
if (read < 0)
|
||||
{
|
||||
zlib->error = Z_STREAM_ERROR;
|
||||
break;
|
||||
}
|
||||
return read;
|
||||
|
||||
if (read == 0)
|
||||
break;
|
||||
|
||||
@@ -180,7 +190,6 @@ int32_t mz_stream_zlib_read(void *stream, void *buf, int32_t size)
|
||||
|
||||
if (err == Z_STREAM_END)
|
||||
break;
|
||||
|
||||
if (err != Z_OK)
|
||||
{
|
||||
zlib->error = err;
|
||||
@@ -190,16 +199,20 @@ int32_t mz_stream_zlib_read(void *stream, void *buf, int32_t size)
|
||||
while (zlib->zstream.avail_out > 0);
|
||||
|
||||
if (zlib->error != 0)
|
||||
{
|
||||
// Zlib errors are compatible with MZ
|
||||
return zlib->error;
|
||||
}
|
||||
|
||||
return total_out;
|
||||
#endif
|
||||
}
|
||||
|
||||
static int32_t mz_stream_zlib_flush(void *stream)
|
||||
{
|
||||
mz_stream_zlib *zlib = (mz_stream_zlib *)stream;
|
||||
if (mz_stream_write(zlib->stream.base, zlib->buffer, zlib->buffer_len) != zlib->buffer_len)
|
||||
return MZ_STREAM_ERROR;
|
||||
return MZ_WRITE_ERROR;
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
@@ -216,11 +229,9 @@ static int32_t mz_stream_zlib_deflate(void *stream, int flush)
|
||||
{
|
||||
if (zlib->zstream.avail_out == 0)
|
||||
{
|
||||
if (mz_stream_zlib_flush(zlib) != MZ_OK)
|
||||
{
|
||||
zlib->error = Z_STREAM_ERROR;
|
||||
return MZ_STREAM_ERROR;
|
||||
}
|
||||
err = mz_stream_zlib_flush(zlib);
|
||||
if (err != MZ_OK)
|
||||
return err;
|
||||
|
||||
zlib->zstream.avail_out = sizeof(zlib->buffer);
|
||||
zlib->zstream.next_out = zlib->buffer;
|
||||
@@ -242,7 +253,7 @@ static int32_t mz_stream_zlib_deflate(void *stream, int flush)
|
||||
if (err != Z_OK)
|
||||
{
|
||||
zlib->error = err;
|
||||
return MZ_STREAM_ERROR;
|
||||
return MZ_DATA_ERROR;
|
||||
}
|
||||
}
|
||||
while ((zlib->zstream.avail_in > 0) || (flush == Z_FINISH && err == Z_OK));
|
||||
@@ -253,26 +264,36 @@ static int32_t mz_stream_zlib_deflate(void *stream, int flush)
|
||||
int32_t mz_stream_zlib_write(void *stream, const void *buf, int32_t size)
|
||||
{
|
||||
mz_stream_zlib *zlib = (mz_stream_zlib *)stream;
|
||||
int32_t err = size;
|
||||
|
||||
|
||||
zlib->zstream.next_in = (Bytef*)buf;
|
||||
#ifdef MZ_ZIP_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)
|
||||
{
|
||||
return MZ_STREAM_ERROR;
|
||||
MZ_UNUSED(stream);
|
||||
|
||||
return MZ_TELL_ERROR;
|
||||
}
|
||||
|
||||
int32_t mz_stream_zlib_seek(void *stream, int64_t offset, int32_t origin)
|
||||
{
|
||||
return MZ_STREAM_ERROR;
|
||||
MZ_UNUSED(stream);
|
||||
MZ_UNUSED(offset);
|
||||
MZ_UNUSED(origin);
|
||||
|
||||
return MZ_SEEK_ERROR;
|
||||
}
|
||||
|
||||
int32_t mz_stream_zlib_close(void *stream)
|
||||
@@ -282,20 +303,28 @@ 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;
|
||||
|
||||
if (zlib->error != Z_OK)
|
||||
return MZ_STREAM_ERROR;
|
||||
return MZ_CLOSE_ERROR;
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
@@ -312,15 +341,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)
|
||||
@@ -330,19 +364,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));
|
||||
@@ -362,7 +398,7 @@ void mz_stream_zlib_delete(void **stream)
|
||||
return;
|
||||
zlib = (mz_stream_zlib *)*stream;
|
||||
if (zlib != NULL)
|
||||
free(zlib);
|
||||
MZ_FREE(zlib);
|
||||
*stream = NULL;
|
||||
}
|
||||
|
||||
@@ -370,13 +406,3 @@ void *mz_stream_zlib_get_interface(void)
|
||||
{
|
||||
return (void *)&mz_stream_zlib_vtbl;
|
||||
}
|
||||
|
||||
static int32_t mz_stream_zlib_crc32(int32_t value, const void *buf, int32_t size)
|
||||
{
|
||||
return crc32(value, buf, size);
|
||||
}
|
||||
|
||||
void *mz_stream_zlib_get_crc32_update(void)
|
||||
{
|
||||
return (void *)mz_stream_zlib_crc32;
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/* mz_strm_zlib.h -- Stream for zlib inflate/deflate
|
||||
Version 2.2.7, January 30th, 2018
|
||||
Version 2.7.3, November 4, 2018
|
||||
part of the MiniZip project
|
||||
|
||||
Copyright (C) 2010-2018 Nathan Moinvaziri
|
||||
@@ -9,8 +9,8 @@
|
||||
See the accompanying LICENSE file for the full text of the license.
|
||||
*/
|
||||
|
||||
#ifndef _MZ_STREAM_ZLIB_H
|
||||
#define _MZ_STREAM_ZLIB_H
|
||||
#ifndef MZ_STREAM_ZLIB_H
|
||||
#define MZ_STREAM_ZLIB_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
@@ -36,7 +36,6 @@ void* mz_stream_zlib_create(void **stream);
|
||||
void mz_stream_zlib_delete(void **stream);
|
||||
|
||||
void* mz_stream_zlib_get_interface(void);
|
||||
void* mz_stream_zlib_get_crc32_update(void);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
+966
-351
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,228 @@
|
||||
/* mz_zip.h -- Zip manipulation
|
||||
Version 2.7.3, November 4, 2018
|
||||
part of the MiniZip project
|
||||
|
||||
Copyright (C) 2010-2018 Nathan Moinvaziri
|
||||
https://github.com/nmoinvaz/minizip
|
||||
Copyright (C) 2009-2010 Mathias Svensson
|
||||
Modifications for Zip64 support
|
||||
http://result42.com
|
||||
Copyright (C) 1998-2010 Gilles Vollant
|
||||
https://www.winimage.com/zLibDll/minizip.html
|
||||
|
||||
This program is distributed under the terms of the same license as zlib.
|
||||
See the accompanying LICENSE file for the full text of the license.
|
||||
*/
|
||||
|
||||
#ifndef MZ_ZIP_H
|
||||
#define MZ_ZIP_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "mz_strm.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct mz_zip_file_s
|
||||
{
|
||||
uint16_t version_madeby; // version made by
|
||||
uint16_t version_needed; // version needed to extract
|
||||
uint16_t flag; // general purpose bit flag
|
||||
uint16_t compression_method; // compression method
|
||||
time_t modified_date; // last modified date in unix time
|
||||
time_t accessed_date; // last accessed date in unix time
|
||||
time_t creation_date; // creation date in unix time
|
||||
uint32_t crc; // crc-32
|
||||
int64_t compressed_size; // compressed size
|
||||
int64_t uncompressed_size; // uncompressed size
|
||||
uint16_t filename_size; // filename length
|
||||
uint16_t extrafield_size; // extra field length
|
||||
uint16_t comment_size; // file comment length
|
||||
uint32_t disk_number; // disk number start
|
||||
int64_t disk_offset; // relative offset of local header
|
||||
uint16_t internal_fa; // internal file attributes
|
||||
uint32_t external_fa; // external file attributes
|
||||
uint16_t zip64; // zip64 extension mode
|
||||
#ifdef HAVE_AES
|
||||
uint16_t aes_version; // winzip aes extension if not 0
|
||||
uint8_t aes_encryption_mode; // winzip aes encryption mode
|
||||
#endif
|
||||
|
||||
const char *filename; // filename utf8 null-terminated string
|
||||
const uint8_t *extrafield; // extrafield data
|
||||
const char *comment; // comment utf8 null-terminated string
|
||||
|
||||
} mz_zip_file, mz_zip_entry;
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef int32_t (*mz_zip_locate_entry_cb)(void *handle, void *userdata, mz_zip_file *file_info);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
void * mz_zip_create(void **handle);
|
||||
// Create zip instance for opening
|
||||
|
||||
void mz_zip_delete(void **handle);
|
||||
// Delete zip object
|
||||
|
||||
int32_t mz_zip_open(void *handle, void *stream, int32_t mode);
|
||||
// Create a zip file, no delete file in zip functionality
|
||||
|
||||
int32_t mz_zip_close(void *handle);
|
||||
// Close the zip file
|
||||
|
||||
int32_t mz_zip_get_comment(void *handle, const char **comment);
|
||||
// Get a pointer to the global comment
|
||||
|
||||
int32_t mz_zip_set_comment(void *handle, const char *comment);
|
||||
// Set the global comment used for writing zip file
|
||||
|
||||
int32_t mz_zip_get_version_madeby(void *handle, uint16_t *version_madeby);
|
||||
// Get the version made by
|
||||
|
||||
int32_t mz_zip_set_version_madeby(void *handle, uint16_t version_madeby);
|
||||
// Set the version made by used for writing zip file
|
||||
|
||||
int32_t mz_zip_get_stream(void *handle, void **stream);
|
||||
// Get a pointer to the stream used to open
|
||||
|
||||
int32_t mz_zip_set_cd_stream(void *handle, int64_t cd_start_pos, void *cd_stream);
|
||||
// Sets the stream to use for reading the central dir
|
||||
|
||||
int32_t mz_zip_get_cd_mem_stream(void *handle, void **cd_mem_stream);
|
||||
// Get a pointer to the stream used to store the central dir in memory
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t mz_zip_entry_write_open(void *handle, const mz_zip_file *file_info,
|
||||
int16_t compress_level, uint8_t raw, const char *password);
|
||||
// Open for writing the current file in the zip file
|
||||
|
||||
int32_t mz_zip_entry_write(void *handle, const void *buf, int32_t len);
|
||||
// Write bytes from the current file in the zip file
|
||||
|
||||
int32_t mz_zip_entry_is_open(void *handle);
|
||||
// Check to see if entry is open for read/write
|
||||
|
||||
int32_t mz_zip_entry_is_dir(void *handle);
|
||||
// Checks to see if the entry is a directory
|
||||
|
||||
int32_t mz_zip_entry_read_open(void *handle, uint8_t raw, const char *password);
|
||||
// Open for reading the current file in the zip file
|
||||
|
||||
int32_t mz_zip_entry_read(void *handle, void *buf, int32_t len);
|
||||
// Read bytes from the current file in the zip file
|
||||
|
||||
int32_t mz_zip_entry_get_info(void *handle, mz_zip_file **file_info);
|
||||
// Get info about the current file, only valid while current entry is open
|
||||
|
||||
int32_t mz_zip_entry_get_local_info(void *handle, mz_zip_file **local_file_info);
|
||||
// Get local info about the current file, only valid while current entry is being read
|
||||
|
||||
int32_t mz_zip_entry_set_extrafield(void *handle, const uint8_t *extrafield, uint16_t extrafield_size);
|
||||
// Sets or updates the extra field for the entry to be used before writing cd
|
||||
|
||||
int32_t mz_zip_entry_close_raw(void *handle, int64_t uncompressed_size, uint32_t crc32);
|
||||
// Close the current file in the zip file where raw is compressed data
|
||||
|
||||
int32_t mz_zip_entry_close(void *handle);
|
||||
// Close the current file in the zip file
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t mz_zip_set_number_entry(void *handle, uint64_t number_entry);
|
||||
// Sets the total number of entries
|
||||
|
||||
int32_t mz_zip_get_number_entry(void *handle, uint64_t *number_entry);
|
||||
// Get the total number of entries
|
||||
|
||||
int32_t mz_zip_get_disk_number_with_cd(void *handle, uint32_t *disk_number_with_cd);
|
||||
// Get the the disk number containing the central directory record
|
||||
|
||||
int64_t mz_zip_get_entry(void *handle);
|
||||
// Return offset of the current entry in the zip file
|
||||
|
||||
int32_t mz_zip_goto_entry(void *handle, int64_t cd_pos);
|
||||
// Go to specified entry in the zip file
|
||||
|
||||
int32_t mz_zip_goto_first_entry(void *handle);
|
||||
// Go to the first entry in the zip file
|
||||
|
||||
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
|
||||
|
||||
int32_t mz_zip_locate_entry(void *handle, const char *filename, uint8_t ignore_case);
|
||||
// Locate the file with the specified name in the zip file or MZ_END_LIST if not found
|
||||
|
||||
int32_t mz_zip_locate_first_entry(void *handle, void *userdata, mz_zip_locate_entry_cb cb);
|
||||
// Locate the first matching entry based on a match callback
|
||||
|
||||
int32_t mz_zip_locate_next_entry(void *handle, void *userdata, mz_zip_locate_entry_cb cb);
|
||||
// LOcate the next matching entry based on a match callback
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t mz_zip_attrib_is_dir(uint32_t attrib, int32_t version_madeby);
|
||||
// Checks to see if the attribute is a directory based on platform
|
||||
|
||||
int32_t mz_zip_attrib_convert(uint8_t src_sys, uint32_t src_attrib, uint8_t target_sys, uint32_t *target_attrib);
|
||||
// Converts file attributes from one host system to another
|
||||
|
||||
int32_t mz_zip_attrib_posix_to_win32(uint32_t posix_attrib, uint32_t *win32_attrib);
|
||||
// Converts posix file attributes to win32 file attributes
|
||||
|
||||
int32_t mz_zip_attrib_win32_to_posix(uint32_t win32_attrib, uint32_t *posix_attrib);
|
||||
// Converts win32 file attributes to posix file attributes
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t mz_zip_extrafield_find(void *stream, uint16_t type, uint16_t *length);
|
||||
// Seeks to extra field by its type and returns its length
|
||||
|
||||
int32_t mz_zip_extrafield_read(void *stream, uint16_t *type, uint16_t *length);
|
||||
// Reads and extrafield header from a stream
|
||||
|
||||
int32_t mz_zip_extrafield_write(void *stream, uint32_t type, uint16_t length);
|
||||
// Writes an extrafield header to a stream
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t mz_zip_dosdate_to_tm(uint64_t dos_date, struct tm *ptm);
|
||||
// Convert dos date/time format to struct tm
|
||||
|
||||
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
|
||||
|
||||
uint32_t mz_zip_time_t_to_dos_date(time_t unix_time);
|
||||
// Convert time_t to dos date/time format
|
||||
|
||||
uint32_t mz_zip_tm_to_dosdate(const struct tm *ptm);
|
||||
// Convert struct tm to dos date/time format
|
||||
|
||||
int32_t mz_zip_ntfs_to_unix_time(uint64_t ntfs_time, time_t *unix_time);
|
||||
// Convert ntfs time to unix time
|
||||
|
||||
int32_t mz_zip_unix_to_ntfs_time(time_t unix_time, uint64_t *ntfs_time);
|
||||
// Convert unix time to ntfs time
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t mz_zip_path_compare(const char *path1, const char *path2, uint8_t ignore_case);
|
||||
// Compare two paths without regard to slashes
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _ZIP_H */
|
||||
+1932
File diff suppressed because it is too large
Load Diff
+281
@@ -0,0 +1,281 @@
|
||||
/* mz_zip_rw.h -- Zip reader/writer
|
||||
Version 2.7.3, November 4, 2018
|
||||
part of the MiniZip project
|
||||
|
||||
Copyright (C) 2010-2018 Nathan Moinvaziri
|
||||
https://github.com/nmoinvaz/minizip
|
||||
|
||||
This program is distributed under the terms of the same license as zlib.
|
||||
See the accompanying LICENSE file for the full text of the license.
|
||||
*/
|
||||
|
||||
#ifndef MZ_ZIP_RW_H
|
||||
#define MZ_ZIP_RW_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "mz_zip.h"
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef int32_t (*mz_zip_reader_overwrite_cb)(void *handle, void *userdata, mz_zip_file *file_info, const char *path);
|
||||
typedef int32_t (*mz_zip_reader_password_cb)(void *handle, void *userdata, mz_zip_file *file_info, char *password, int32_t max_password);
|
||||
typedef int32_t (*mz_zip_reader_progress_cb)(void *handle, void *userdata, mz_zip_file *file_info, int64_t position);
|
||||
typedef int32_t (*mz_zip_reader_entry_cb)(void *handle, void *userdata, mz_zip_file *file_info, const char *path);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t mz_zip_reader_is_open(void *handle);
|
||||
// Checks to see if the zip file is open
|
||||
|
||||
int32_t mz_zip_reader_open(void *handle, void *stream);
|
||||
// Opens zip file from stream
|
||||
|
||||
int32_t mz_zip_reader_open_file(void *handle, const char *path);
|
||||
// Opens zip file from a file path
|
||||
|
||||
int32_t mz_zip_reader_open_file_in_memory(void *handle, const char *path);
|
||||
// Opens zip file from a file path into memory for faster access
|
||||
|
||||
int32_t mz_zip_reader_open_buffer(void *handle, uint8_t *buf, int32_t len, uint8_t copy);
|
||||
// Opens zip file from memory buffer
|
||||
|
||||
int32_t mz_zip_reader_close(void *handle);
|
||||
// Closes the zip file
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t mz_zip_reader_unzip_cd(void *handle);
|
||||
// Unzip the central directory
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t mz_zip_reader_goto_first_entry(void *handle);
|
||||
// Goto the first entry in the zip file that matches the pattern
|
||||
|
||||
int32_t mz_zip_reader_goto_next_entry(void *handle);
|
||||
// Goto the next entry in the zip file that matches the pattern
|
||||
|
||||
int32_t mz_zip_reader_locate_entry(void *handle, const char *filename, uint8_t ignore_case);
|
||||
// Locates an entry by filename
|
||||
|
||||
int32_t mz_zip_reader_entry_open(void *handle);
|
||||
// Opens an entry for reading
|
||||
|
||||
int32_t mz_zip_reader_entry_close(void *handle);
|
||||
// Closes an entry
|
||||
|
||||
int32_t mz_zip_reader_entry_read(void *handle, void *buf, int32_t len);
|
||||
// Reads and entry after being opened
|
||||
|
||||
int32_t mz_zip_reader_entry_has_sign(void *handle);
|
||||
// Checks to see if the entry has a signature
|
||||
|
||||
int32_t mz_zip_reader_entry_sign_verify(void *handle);
|
||||
// Verifies a signature stored with the entry
|
||||
|
||||
int32_t mz_zip_reader_entry_get_hash(void *handle, uint16_t algorithm, uint8_t *digest, int32_t digest_size);
|
||||
// Gets a hash algorithm from the entry's extra field
|
||||
|
||||
int32_t mz_zip_reader_entry_get_first_hash(void *handle, uint16_t *algorithm, uint16_t *digest_size);
|
||||
// Gets the most secure hash algorithm from the entry's extra field
|
||||
|
||||
int32_t mz_zip_reader_entry_get_info(void *handle, mz_zip_file **file_info);
|
||||
// Gets the current entry file info
|
||||
|
||||
int32_t mz_zip_reader_entry_is_dir(void *handle);
|
||||
// Gets the current entry is a directory
|
||||
|
||||
int32_t mz_zip_reader_entry_save(void *handle, void *stream, mz_stream_write_cb write_cb);
|
||||
// Save the current entry to a steam
|
||||
|
||||
int32_t mz_zip_reader_entry_save_process(void *handle, void *stream, mz_stream_write_cb write_cb);
|
||||
// Saves a portion of the current entry to a stream callback
|
||||
|
||||
int32_t mz_zip_reader_entry_save_file(void *handle, const char *path);
|
||||
// Save the current entry to a file
|
||||
|
||||
int32_t mz_zip_reader_entry_save_buffer(void *handle, void *buf, int32_t len);
|
||||
// Save the current entry to a memory buffer
|
||||
|
||||
int32_t mz_zip_reader_entry_save_buffer_length(void *handle);
|
||||
// Gets the length of the buffer required to save
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t mz_zip_reader_save_all(void *handle, const char *destination_dir);
|
||||
// Save all files into a directory
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
void mz_zip_reader_set_pattern(void *handle, const char *pattern, uint8_t ignore_case);
|
||||
// Sets the match pattern for entries in the zip file, if null all entries are matched
|
||||
|
||||
void mz_zip_reader_set_password(void *handle, const char *password);
|
||||
// Sets the password required for extraction
|
||||
|
||||
void mz_zip_reader_set_raw(void *handle, uint8_t raw);
|
||||
// Sets whether or not it should save the entry raw
|
||||
|
||||
int32_t mz_zip_reader_get_raw(void *handle, uint8_t *raw);
|
||||
// Gets whether or not it should save the entry raw
|
||||
|
||||
void mz_zip_reader_set_encoding(void *handle, int32_t encoding);
|
||||
// Sets whether or not it should support cp437 in zip file names
|
||||
|
||||
void mz_zip_reader_set_sign_required(void *handle, uint8_t sign_required);
|
||||
// Sets whether or not it a signature is required
|
||||
|
||||
void mz_zip_reader_set_overwrite_cb(void *handle, void *userdata, mz_zip_reader_overwrite_cb cb);
|
||||
// Callback for what to do when a file is being overwritten
|
||||
|
||||
void mz_zip_reader_set_password_cb(void *handle, void *userdata, mz_zip_reader_password_cb cb);
|
||||
// Callback for when a password is required and hasn't been set
|
||||
|
||||
void mz_zip_reader_set_progress_cb(void *handle, void *userdata, mz_zip_reader_progress_cb cb);
|
||||
// Callback for extraction progress
|
||||
|
||||
void mz_zip_reader_set_progress_interval(void *handle, uint32_t milliseconds);
|
||||
// Let at least milliseconds pass between calls to progress callback
|
||||
|
||||
void mz_zip_reader_set_entry_cb(void *handle, void *userdata, mz_zip_reader_entry_cb cb);
|
||||
// Callback for zip file entries
|
||||
|
||||
int32_t mz_zip_reader_get_zip_handle(void *handle, void **zip_handle);
|
||||
// Gets the underlying zip instance handle
|
||||
|
||||
void* mz_zip_reader_create(void **handle);
|
||||
// Create new instance of zip reader
|
||||
|
||||
void mz_zip_reader_delete(void **handle);
|
||||
// Delete instance of zip reader
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef int32_t (*mz_zip_writer_overwrite_cb)(void *handle, void *userdata, const char *path);
|
||||
typedef int32_t (*mz_zip_writer_password_cb)(void *handle, void *userdata, mz_zip_file *file_info, char *password, int32_t max_password);
|
||||
typedef int32_t (*mz_zip_writer_progress_cb)(void *handle, void *userdata, mz_zip_file *file_info, int64_t position);
|
||||
typedef int32_t (*mz_zip_writer_entry_cb)(void *handle, void *userdata, mz_zip_file *file_info);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t mz_zip_writer_is_open(void *handle);
|
||||
// Checks to see if the zip file is open
|
||||
|
||||
int32_t mz_zip_writer_open(void *handle, void *stream);
|
||||
// Opens zip file from stream
|
||||
|
||||
int32_t mz_zip_writer_open_file(void *handle, const char *path, int64_t disk_size, uint8_t append);
|
||||
// Opens zip file from a file path
|
||||
|
||||
int32_t mz_zip_writer_open_file_in_memory(void *handle, const char *path);
|
||||
// Opens zip file from a file path into memory for faster access
|
||||
|
||||
int32_t mz_zip_writer_close(void *handle);
|
||||
// Closes the zip file
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t mz_zip_writer_zip_cd(void *handle);
|
||||
// Zip the central directory
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t mz_zip_writer_entry_open(void *handle, mz_zip_file *file_info);
|
||||
// Opens an entry in the zip file for writing
|
||||
|
||||
int32_t mz_zip_writer_entry_close(void *handle);
|
||||
// Closes entry in zip file
|
||||
|
||||
int32_t mz_zip_writer_entry_write(void *handle, const void *buf, int32_t len);
|
||||
// Writes data into entry for zip
|
||||
|
||||
int32_t mz_zip_writer_entry_sign(void *handle, uint8_t *message, int32_t message_size,
|
||||
const char *cert_path, const char *cert_pwd);
|
||||
// Signs uncompressed content of entry, call before closing
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t mz_zip_writer_add(void *handle, void *stream, mz_stream_read_cb read_cb);
|
||||
// Writes all data to the currently open entry in the zip
|
||||
|
||||
int32_t mz_zip_writer_add_process(void *handle, void *stream, mz_stream_read_cb read_cb);
|
||||
// Writes a portion of data to the currently open entry in the zip
|
||||
|
||||
int32_t mz_zip_writer_add_info(void *handle, void *stream, mz_stream_read_cb read_cb, mz_zip_file *file_info);
|
||||
// Adds an entry to the zip based on the info
|
||||
|
||||
int32_t mz_zip_writer_add_buffer(void *handle, void *buf, int32_t len, mz_zip_file *file_info);
|
||||
// Adds an entry to the zip with a memory buffer
|
||||
|
||||
int32_t mz_zip_writer_add_file(void *handle, const char *path, const char *filename_in_zip);
|
||||
// Adds an entry to the zip from a file
|
||||
|
||||
int32_t mz_zip_writer_add_path(void *handle, const char *path, const char *root_path, uint8_t include_path,
|
||||
uint8_t recursive);
|
||||
// Enumerates a directory or pattern and adds entries to the zip
|
||||
|
||||
int32_t mz_zip_writer_copy_from_reader(void *handle, void *reader);
|
||||
// Adds an entry from a zip reader instance
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
void mz_zip_writer_set_password(void *handle, const char *password);
|
||||
// Password to use for encrypting files in the zip
|
||||
|
||||
void mz_zip_writer_set_raw(void *handle, uint8_t raw);
|
||||
// Sets whether or not we should write the entry raw
|
||||
|
||||
int32_t mz_zip_writer_get_raw(void *handle, uint8_t *raw);
|
||||
// Gets whether or not we should write the entry raw
|
||||
|
||||
void mz_zip_writer_set_aes(void *handle, uint8_t aes);
|
||||
// Use aes encryption when adding files in zip
|
||||
|
||||
void mz_zip_writer_set_compress_method(void *handle, uint16_t compress_method);
|
||||
// Sets the compression method when adding files in zip
|
||||
|
||||
void mz_zip_writer_set_compress_level(void *handle, int16_t compress_level);
|
||||
// Sets the compression level when adding files in zip
|
||||
|
||||
void mz_zip_writer_set_zip_cd(void *handle, uint8_t flags);
|
||||
// Sets additional flags to be set when adding files in zip
|
||||
|
||||
void mz_zip_writer_set_certificate(void *handle, const char *cert_path, const char *cert_pwd);
|
||||
// Sets the certificate and timestamp url to use for signing when adding files in zip
|
||||
|
||||
void mz_zip_writer_set_overwrite_cb(void *handle, void *userdata, mz_zip_writer_overwrite_cb cb);
|
||||
// Callback for what to do when zip file already exists
|
||||
|
||||
void mz_zip_writer_set_password_cb(void *handle, void *userdata, mz_zip_writer_password_cb cb);
|
||||
// Callback for ask if a password is required for adding
|
||||
|
||||
void mz_zip_writer_set_progress_cb(void *handle, void *userdata, mz_zip_writer_progress_cb cb);
|
||||
// Callback for compression progress
|
||||
|
||||
void mz_zip_writer_set_progress_interval(void *handle, uint32_t milliseconds);
|
||||
// Let at least milliseconds pass between calls to progress callback
|
||||
|
||||
void mz_zip_writer_set_entry_cb(void *handle, void *userdata, mz_zip_writer_entry_cb cb);
|
||||
// Callback for zip file entries
|
||||
|
||||
int32_t mz_zip_writer_get_zip_handle(void *handle, void **zip_handle);
|
||||
// Gets the underlying zip handle
|
||||
|
||||
void* mz_zip_writer_create(void **handle);
|
||||
// Create new instance of zip writer
|
||||
|
||||
void mz_zip_writer_delete(void **handle);
|
||||
// Delete instance of zip writer
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
-762
@@ -1,762 +0,0 @@
|
||||
/* minizip.c
|
||||
Version 2.2.7, January 30th, 2018
|
||||
part of the MiniZip project
|
||||
|
||||
Copyright (C) 2010-2018 Nathan Moinvaziri
|
||||
https://github.com/nmoinvaz/minizip
|
||||
Copyright (C) 2009-2010 Mathias Svensson
|
||||
Modifications for Zip64 support
|
||||
http://result42.com
|
||||
Copyright (C) 2007-2008 Even Rouault
|
||||
Modifications of Unzip for Zip64
|
||||
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 <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <inttypes.h>
|
||||
#include <time.h>
|
||||
#include <errno.h>
|
||||
|
||||
#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()
|
||||
{
|
||||
printf("Minizip %s - https://github.com/nmoinvaz/minizip\n", MZ_VERSION);
|
||||
printf("---------------------------------------------------\n");
|
||||
}
|
||||
|
||||
void minizip_help()
|
||||
{
|
||||
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" \
|
||||
" -l List files\n" \
|
||||
" -d Destination directory\n" \
|
||||
" -o Overwrite existing files\n" \
|
||||
" -a Append to existing zip file\n" \
|
||||
" -u Buffered reading and writing\n" \
|
||||
" -j Exclude path of files\n" \
|
||||
" -0 Store only\n" \
|
||||
" -1 Compress faster\n" \
|
||||
" -9 Compress better\n" \
|
||||
" -k Disk size in KB\n" \
|
||||
" -p Encryption password\n");
|
||||
#ifdef HAVE_AES
|
||||
printf(" -s AES encryption\n");
|
||||
#endif
|
||||
#ifdef HAVE_BZIP2
|
||||
printf(" -b BZIP2 compression\n");
|
||||
#endif
|
||||
#ifdef HAVE_LZMA
|
||||
printf(" -m LZMA compression\n");
|
||||
#endif
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct minizip_opt_s {
|
||||
uint8_t exclude_path;
|
||||
int16_t compress_level;
|
||||
int16_t compress_method;
|
||||
uint8_t overwrite;
|
||||
#ifdef HAVE_AES
|
||||
int16_t aes;
|
||||
#endif
|
||||
} minizip_opt;
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t minizip_add_file(void *handle, const char *path, const char *password, minizip_opt *options)
|
||||
{
|
||||
mz_zip_file file_info = { 0 };
|
||||
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.
|
||||
|
||||
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.uncompressed_size = mz_os_get_file_size(path);
|
||||
|
||||
#ifdef HAVE_AES
|
||||
if (options->aes)
|
||||
file_info.aes_version = MZ_AES_VERSION;
|
||||
#endif
|
||||
|
||||
mz_os_get_file_date(path, &file_info.modified_date, &file_info.accessed_date,
|
||||
&file_info.creation_date);
|
||||
|
||||
// Add to zip
|
||||
err = mz_zip_entry_write_open(handle, &file_info, options->compress_level, password);
|
||||
if (err != MZ_OK)
|
||||
{
|
||||
printf("Error in opening %s in zip file (%d)\n", filenameinzip, err);
|
||||
return err;
|
||||
}
|
||||
|
||||
mz_stream_os_create(&stream);
|
||||
|
||||
err = mz_stream_os_open(stream, path, MZ_OPEN_MODE_READ);
|
||||
|
||||
if (err == MZ_OK)
|
||||
{
|
||||
// Read contents of file and write it to zip
|
||||
do
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
while (err == MZ_OK);
|
||||
|
||||
mz_stream_os_close(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)
|
||||
{
|
||||
printf("Error in closing %s in the zip file (%d)\n", filenameinzip, err_close);
|
||||
err = err_close;
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
int32_t minizip_add(void *handle, const char *path, const char *password, minizip_opt *options, uint8_t recursive)
|
||||
{
|
||||
DIR *dir = NULL;
|
||||
struct dirent *entry = NULL;
|
||||
int32_t err = 0;
|
||||
char full_path[320];
|
||||
|
||||
|
||||
if (mz_os_is_dir(path) != MZ_OK)
|
||||
return minizip_add_file(handle, path, password, options);
|
||||
|
||||
dir = mz_os_open_dir(path);
|
||||
|
||||
if (dir == NULL)
|
||||
{
|
||||
printf("Cannot enumerate directory %s\n", path);
|
||||
return MZ_EXIST_ERROR;
|
||||
}
|
||||
|
||||
while ((entry = mz_os_read_dir(dir)) != NULL)
|
||||
{
|
||||
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
|
||||
continue;
|
||||
|
||||
full_path[0] = 0;
|
||||
mz_path_combine(full_path, path, sizeof(full_path));
|
||||
mz_path_combine(full_path, entry->d_name, sizeof(full_path));
|
||||
|
||||
if (!recursive && mz_os_is_dir(full_path))
|
||||
continue;
|
||||
|
||||
err = minizip_add(handle, full_path, password, options, recursive);
|
||||
if (err != MZ_OK)
|
||||
return err;
|
||||
}
|
||||
|
||||
mz_os_close_dir(dir);
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t minizip_list(void *handle)
|
||||
{
|
||||
mz_zip_file *file_info = NULL;
|
||||
uint32_t ratio = 0;
|
||||
int16_t level = 0;
|
||||
int32_t err = MZ_OK;
|
||||
struct tm tmu_date = { 0 };
|
||||
const char *string_method = NULL;
|
||||
char crypt = ' ';
|
||||
|
||||
|
||||
err = mz_zip_goto_first_entry(handle);
|
||||
|
||||
if (err != MZ_OK && err != MZ_END_OF_LIST)
|
||||
{
|
||||
printf("Error %d going to first entry in zip file\n", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
printf(" Length Method Size Ratio Date Time CRC-32 Name\n");
|
||||
printf(" ------ ------ ---- ----- ---- ---- ------ ----\n");
|
||||
|
||||
while (err == MZ_OK)
|
||||
{
|
||||
err = mz_zip_entry_get_info(handle, &file_info);
|
||||
|
||||
if (err != MZ_OK)
|
||||
{
|
||||
printf("Error %d getting entry info in zip file\n", err);
|
||||
break;
|
||||
}
|
||||
|
||||
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 = '*';
|
||||
|
||||
switch (file_info->compression_method)
|
||||
{
|
||||
case MZ_COMPRESS_METHOD_RAW:
|
||||
string_method = "Stored";
|
||||
break;
|
||||
case MZ_COMPRESS_METHOD_DEFLATE:
|
||||
level = (int16_t)((file_info->flag & 0x6) / 2);
|
||||
if (level == 0)
|
||||
string_method = "Defl:N";
|
||||
else if (level == 1)
|
||||
string_method = "Defl:X";
|
||||
else if ((level == 2) || (level == 3))
|
||||
string_method = "Defl:F"; // 2: fast , 3: extra fast
|
||||
else
|
||||
string_method = "Defl:?";
|
||||
break;
|
||||
case MZ_COMPRESS_METHOD_BZIP2:
|
||||
string_method = "BZip2";
|
||||
break;
|
||||
case MZ_COMPRESS_METHOD_LZMA:
|
||||
string_method = "LZMA";
|
||||
break;
|
||||
default:
|
||||
string_method = "Unknwn";
|
||||
}
|
||||
|
||||
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\
|
||||
"-%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,
|
||||
(uint32_t)tmu_date.tm_mon + 1, (uint32_t)tmu_date.tm_mday,
|
||||
(uint32_t)tmu_date.tm_year % 100,
|
||||
(uint32_t)tmu_date.tm_hour, (uint32_t)tmu_date.tm_min,
|
||||
file_info->crc, file_info->filename);
|
||||
|
||||
err = mz_zip_goto_next_entry(handle);
|
||||
|
||||
if (err != MZ_OK && err != MZ_END_OF_LIST)
|
||||
{
|
||||
printf("Error %d going to next entry in zip file\n", err);
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
if (err == MZ_END_OF_LIST)
|
||||
return MZ_OK;
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t minizip_extract_currentfile(void *handle, const char *destination, const char *password, minizip_opt *options)
|
||||
{
|
||||
mz_zip_file *file_info = NULL;
|
||||
uint8_t buf[INT16_MAX];
|
||||
int32_t read = 0;
|
||||
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;
|
||||
char out_path[512];
|
||||
char directory[512];
|
||||
|
||||
|
||||
err = mz_zip_entry_get_info(handle, &file_info);
|
||||
|
||||
if (err != MZ_OK)
|
||||
{
|
||||
printf("Error %d getting entry info in zip file\n", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
match = filename = file_info->filename;
|
||||
while (*match != 0)
|
||||
{
|
||||
if ((*match == '/') || (*match == '\\'))
|
||||
filename = match + 1;
|
||||
match += 1;
|
||||
}
|
||||
|
||||
// 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));
|
||||
else
|
||||
mz_path_combine(out_path, file_info->filename, sizeof(out_path));
|
||||
|
||||
// If zip entry is a directory then create it on disk
|
||||
if (*filename == 0)
|
||||
{
|
||||
if (options->exclude_path == 0)
|
||||
{
|
||||
printf("Creating directory: %s\n", out_path);
|
||||
mz_make_dir(out_path);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
err = mz_zip_entry_read_open(handle, 0, password);
|
||||
|
||||
if (err != MZ_OK)
|
||||
{
|
||||
printf("Error %d opening entry in zip file\n", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
// Determine if the file should be overwritten or not and ask the user if needed
|
||||
if ((err == MZ_OK) && (options->overwrite == 0) && (mz_os_file_exists(out_path) == MZ_OK))
|
||||
{
|
||||
char rep = 0;
|
||||
do
|
||||
{
|
||||
char answer[128];
|
||||
printf("The file %s exists. Overwrite ? [y]es, [n]o, [A]ll: ", out_path);
|
||||
if (scanf("%1s", answer) != 1)
|
||||
exit(EXIT_FAILURE);
|
||||
rep = answer[0];
|
||||
if ((rep >= 'a') && (rep <= 'z'))
|
||||
rep -= 0x20;
|
||||
}
|
||||
while ((rep != 'Y') && (rep != 'N') && (rep != 'A'));
|
||||
|
||||
if (rep == 'N')
|
||||
skip = 1;
|
||||
if (rep == 'A')
|
||||
options->overwrite = 1;
|
||||
}
|
||||
|
||||
mz_stream_os_create(&stream);
|
||||
|
||||
// Create the file on disk so we can unzip to it
|
||||
if ((skip == 0) && (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))
|
||||
{
|
||||
// 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_make_dir(directory);
|
||||
|
||||
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)
|
||||
{
|
||||
printf(" Extracting: %s\n", out_path);
|
||||
while (1)
|
||||
{
|
||||
read = mz_zip_entry_read(handle, buf, sizeof(buf));
|
||||
if (read < 0)
|
||||
{
|
||||
err = read;
|
||||
printf("Error %d reading entry in zip file\n", err);
|
||||
break;
|
||||
}
|
||||
if (read == 0)
|
||||
break;
|
||||
written = mz_stream_os_write(stream, buf, read);
|
||||
if (written != read)
|
||||
{
|
||||
err = mz_stream_os_error(stream);
|
||||
printf("Error %d in writing extracted file\n", err);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
mz_stream_os_close(stream);
|
||||
|
||||
// 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,
|
||||
file_info->creation_date);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Error opening %s\n", out_path);
|
||||
}
|
||||
|
||||
mz_stream_os_delete(&stream);
|
||||
|
||||
err_close = mz_zip_entry_close(handle);
|
||||
if (err_close != MZ_OK)
|
||||
{
|
||||
printf("Error %d closing entry in zip file\n", err_close);
|
||||
err = err_close;
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
if (err == MZ_END_OF_LIST)
|
||||
return MZ_OK;
|
||||
if (err != MZ_OK)
|
||||
printf("Error %d going to first entry in zip file\n", err);
|
||||
|
||||
while (err == MZ_OK)
|
||||
{
|
||||
err = minizip_extract_currentfile(handle, destination, password, options);
|
||||
|
||||
if (err != MZ_OK)
|
||||
break;
|
||||
|
||||
err = mz_zip_goto_next_entry(handle);
|
||||
|
||||
if (err == MZ_END_OF_LIST)
|
||||
return MZ_OK;
|
||||
if (err != MZ_OK)
|
||||
printf("Error %d going to next entry in zip file\n", err);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
int32_t minizip_extract_onefile(void *handle, const char *filename, const char *destination, const char *password, minizip_opt *options)
|
||||
{
|
||||
int32_t err = mz_zip_locate_entry(handle, filename, NULL);
|
||||
|
||||
if (err != MZ_OK)
|
||||
{
|
||||
printf("File %s not found in the zip file\n", filename);
|
||||
return err;
|
||||
}
|
||||
|
||||
return minizip_extract_currentfile(handle, destination, password, options);
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#ifndef NOMAIN
|
||||
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;
|
||||
char *filename_to_extract = NULL;
|
||||
minizip_opt options;
|
||||
int64_t disk_size = 0;
|
||||
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;
|
||||
int32_t err = 0;
|
||||
int32_t i = 0;
|
||||
|
||||
minizip_banner();
|
||||
if (argc == 1)
|
||||
{
|
||||
minizip_help();
|
||||
return 0;
|
||||
}
|
||||
|
||||
memset(&options, 0, sizeof(options));
|
||||
|
||||
options.compress_method = MZ_COMPRESS_METHOD_DEFLATE;
|
||||
options.compress_level = MZ_COMPRESS_LEVEL_DEFAULT;
|
||||
|
||||
// Parse command line options
|
||||
for (i = 1; i < argc; i += 1)
|
||||
{
|
||||
if ((*argv[i]) == '-')
|
||||
{
|
||||
const char *p = argv[i] + 1;
|
||||
|
||||
while ((*p) != '\0')
|
||||
{
|
||||
char c = *(p++);
|
||||
if ((c == 'l') || (c == 'L'))
|
||||
do_list = 1;
|
||||
if ((c == 'x') || (c == 'X'))
|
||||
do_extract = 1;
|
||||
if ((c == 'a') || (c == 'A'))
|
||||
append = 1;
|
||||
if ((c == 'u') || (c == 'U'))
|
||||
buffered = 1;
|
||||
if ((c == 'o') || (c == 'O'))
|
||||
options.overwrite = 1;
|
||||
if ((c == '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;
|
||||
}
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (path_arg == 0)
|
||||
path_arg = i;
|
||||
}
|
||||
|
||||
if (path_arg == 0)
|
||||
{
|
||||
minizip_help();
|
||||
return 0;
|
||||
}
|
||||
|
||||
path = argv[path_arg];
|
||||
|
||||
mode = MZ_OPEN_MODE_READ;
|
||||
|
||||
if ((do_list == 0) && (do_extract == 0))
|
||||
{
|
||||
mode |= MZ_OPEN_MODE_WRITE;
|
||||
|
||||
if (mz_os_file_exists(path) != MZ_OK)
|
||||
{
|
||||
// If the file doesn't exist, we don't append file
|
||||
mode |= MZ_OPEN_MODE_CREATE;
|
||||
}
|
||||
else if (append == 1)
|
||||
{
|
||||
mode |= MZ_OPEN_MODE_APPEND;
|
||||
}
|
||||
else if (options.overwrite == 0)
|
||||
{
|
||||
// If ask the user what to do because append and overwrite args not set
|
||||
char rep = 0;
|
||||
do
|
||||
{
|
||||
char answer[128];
|
||||
printf("The file %s exists. Overwrite ? [y]es, [n]o, [a]ppend : ", path);
|
||||
if (scanf("%1s", answer) != 1)
|
||||
exit(EXIT_FAILURE);
|
||||
rep = answer[0];
|
||||
|
||||
if ((rep >= 'a') && (rep <= 'z'))
|
||||
rep -= 0x20;
|
||||
}
|
||||
while ((rep != 'Y') && (rep != 'N') && (rep != 'A'));
|
||||
|
||||
if (rep == 'A')
|
||||
{
|
||||
mode |= MZ_OPEN_MODE_APPEND;
|
||||
}
|
||||
else if (rep == 'Y')
|
||||
{
|
||||
mode |= MZ_OPEN_MODE_CREATE;
|
||||
}
|
||||
else if (rep == 'N')
|
||||
{
|
||||
minizip_help();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mz_stream_os_create(&file_stream);
|
||||
mz_stream_buffered_create(&buf_stream);
|
||||
mz_stream_split_create(&split_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);
|
||||
|
||||
if (err != MZ_OK)
|
||||
{
|
||||
printf("Error opening file %s\n", path);
|
||||
}
|
||||
else
|
||||
{
|
||||
handle = mz_zip_open(split_stream, mode);
|
||||
|
||||
if (handle == NULL)
|
||||
{
|
||||
printf("Error opening zip %s\n", path);
|
||||
err = MZ_FORMAT_ERROR;
|
||||
}
|
||||
|
||||
if (do_list)
|
||||
{
|
||||
err = minizip_list(handle);
|
||||
}
|
||||
else if (do_extract)
|
||||
{
|
||||
// Create target directory if it doesn't exist
|
||||
if (destination != NULL)
|
||||
mz_make_dir(destination);
|
||||
|
||||
if (argc > path_arg + 1)
|
||||
filename_to_extract = argv[path_arg + 1];
|
||||
|
||||
if (filename_to_extract == NULL)
|
||||
err = minizip_extract_all(handle, destination, password, &options);
|
||||
else
|
||||
err = minizip_extract_onefile(handle, filename_to_extract, destination, password, &options);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Creating %s\n", path);
|
||||
|
||||
// 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);
|
||||
|
||||
mz_zip_set_version_madeby(handle, MZ_VERSION_MADEBY);
|
||||
}
|
||||
|
||||
err_close = mz_zip_close(handle);
|
||||
|
||||
if (err_close != MZ_OK)
|
||||
{
|
||||
printf("Error in closing %s (%d)\n", path, err_close);
|
||||
err = err_close;
|
||||
}
|
||||
|
||||
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
|
||||
@@ -1,86 +0,0 @@
|
||||
/* mz.h -- Errors codes, zip flags and magic
|
||||
Version 2.2.7, January 30th, 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_H
|
||||
#define _MZ_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
// MZ_VERSION
|
||||
#define MZ_VERSION ("2.2.7")
|
||||
|
||||
// MZ_ERROR
|
||||
#define MZ_OK (0)
|
||||
#define MZ_STREAM_ERROR (-1)
|
||||
#define MZ_DATA_ERROR (-3)
|
||||
#define MZ_MEM_ERROR (-4)
|
||||
#define MZ_END_OF_LIST (-100)
|
||||
#define MZ_END_OF_STREAM (-101)
|
||||
#define MZ_PARAM_ERROR (-102)
|
||||
#define MZ_FORMAT_ERROR (-103)
|
||||
#define MZ_INTERNAL_ERROR (-104)
|
||||
#define MZ_CRC_ERROR (-105)
|
||||
#define MZ_CRYPT_ERROR (-106)
|
||||
#define MZ_EXIST_ERROR (-107)
|
||||
#define MZ_PASSWORD_ERROR (-108)
|
||||
|
||||
// MZ_OPEN
|
||||
#define MZ_OPEN_MODE_READ (0x01)
|
||||
#define MZ_OPEN_MODE_WRITE (0x02)
|
||||
#define MZ_OPEN_MODE_READWRITE (MZ_OPEN_MODE_READ | MZ_OPEN_MODE_WRITE)
|
||||
#define MZ_OPEN_MODE_APPEND (0x04)
|
||||
#define MZ_OPEN_MODE_CREATE (0x08)
|
||||
#define MZ_OPEN_MODE_EXISTING (0x10)
|
||||
|
||||
// MZ_SEEK
|
||||
#define MZ_SEEK_SET (0)
|
||||
#define MZ_SEEK_CUR (1)
|
||||
#define MZ_SEEK_END (2)
|
||||
|
||||
// MZ_COMPRESS
|
||||
#define MZ_COMPRESS_METHOD_RAW (0)
|
||||
#define MZ_COMPRESS_METHOD_DEFLATE (8)
|
||||
#define MZ_COMPRESS_METHOD_BZIP2 (12)
|
||||
#define MZ_COMPRESS_METHOD_LZMA (14)
|
||||
#define MZ_COMPRESS_METHOD_AES (99)
|
||||
|
||||
#define MZ_COMPRESS_LEVEL_DEFAULT (-1)
|
||||
#define MZ_COMPRESS_LEVEL_FAST (2)
|
||||
#define MZ_COMPRESS_LEVEL_NORMAL (6)
|
||||
#define MZ_COMPRESS_LEVEL_BEST (9)
|
||||
|
||||
// MZ_ZIP
|
||||
#define MZ_ZIP_FLAG_ENCRYPTED (1 << 0)
|
||||
#define MZ_ZIP_FLAG_LZMA_EOS_MARKER (1 << 1)
|
||||
#define MZ_ZIP_FLAG_DEFLATE_MAX (1 << 1)
|
||||
#define MZ_ZIP_FLAG_DEFLATE_NORMAL (0)
|
||||
#define MZ_ZIP_FLAG_DEFLATE_FAST (1 << 2)
|
||||
#define MZ_ZIP_FLAG_DEFLATE_SUPER_FAST (MZ_ZIP_FLAG_DEFLATE_FAST | \
|
||||
MZ_ZIP_FLAG_DEFLATE_MAX)
|
||||
#define MZ_ZIP_FLAG_DATA_DESCRIPTOR (1 << 3)
|
||||
|
||||
// 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)
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
-590
@@ -1,590 +0,0 @@
|
||||
/* mz_compat.c -- Backwards compatible interface for older versions
|
||||
Version 2.2.7, January 30th, 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_zlib.h"
|
||||
#include "mz_zip.h"
|
||||
|
||||
#include "mz_compat.h"
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct mz_compat_s {
|
||||
void *stream;
|
||||
void *handle;
|
||||
} mz_compat;
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
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)
|
||||
{
|
||||
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;
|
||||
|
||||
switch (append)
|
||||
{
|
||||
case APPEND_STATUS_CREATE:
|
||||
mode |= MZ_OPEN_MODE_CREATE;
|
||||
break;
|
||||
case APPEND_STATUS_CREATEAFTER:
|
||||
mode |= MZ_OPEN_MODE_CREATE | MZ_OPEN_MODE_APPEND;
|
||||
break;
|
||||
case APPEND_STATUS_ADDINZIP:
|
||||
open_existing = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
if (mz_stream_open(stream, path, mode) != MZ_OK)
|
||||
{
|
||||
mz_stream_delete(&stream);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
handle = mz_zip_open(stream, mode);
|
||||
|
||||
if (handle == NULL)
|
||||
{
|
||||
mz_stream_delete(&stream);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (globalcomment != NULL)
|
||||
mz_zip_get_comment(handle, globalcomment);
|
||||
|
||||
compat = (mz_compat *)malloc(sizeof(mz_compat));
|
||||
compat->handle = handle;
|
||||
compat->stream = stream;
|
||||
|
||||
return (zipFile)compat;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
mz_compat *compat = (mz_compat *)file;
|
||||
mz_zip_file file_info;
|
||||
|
||||
if (compat == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
if (zipfi != NULL)
|
||||
{
|
||||
file_info.modified_date = mz_zip_dosdate_to_time_t(zipfi->dos_date);
|
||||
file_info.external_fa = zipfi->external_fa;
|
||||
file_info.internal_fa = zipfi->internal_fa;
|
||||
}
|
||||
|
||||
file_info.compression_method = compression_method;
|
||||
file_info.filename = (char *)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_size = size_extrafield_global;
|
||||
file_info.version_madeby = version_madeby;
|
||||
file_info.comment = (char *)comment;
|
||||
file_info.flag = flag_base;
|
||||
#ifdef HAVE_AES
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
return zipOpenNewFileInZip4_64(file, filename, zipfi, extrafield_local, size_extrafield_local,
|
||||
extrafield_global, size_extrafield_global, comment, compression_method, level, raw, windowBits,
|
||||
memLevel, strategy, password, crc_for_crypting, version_madeby, flag_base, 0);
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
return zipOpenNewFileInZip4_64(file, filename, zipfi, extrafield_local, size_extrafield_local,
|
||||
extrafield_global, size_extrafield_global, comment, compression_method, level, raw, windowBits,
|
||||
memLevel, strategy, password, crc_for_crypting, MZ_VERSION_MADEBY, 0, 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)
|
||||
{
|
||||
return zipOpenNewFileInZip4_64(file, filename, zipfi, extrafield_local, size_extrafield_local,
|
||||
extrafield_global, size_extrafield_global, comment, compression_method, level, raw, windowBits,
|
||||
memLevel, strategy, password, crc_for_crypting, MZ_VERSION_MADEBY, 0, zip64);
|
||||
}
|
||||
|
||||
extern int ZEXPORT zipWriteInFileInZip(zipFile file, const void *buf, uint32_t len)
|
||||
{
|
||||
mz_compat *compat = (mz_compat *)file;
|
||||
if (compat == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
return mz_zip_entry_write(compat->handle, buf, len);
|
||||
}
|
||||
|
||||
extern int ZEXPORT zipCloseFileInZipRaw(zipFile file, uint32_t uncompressed_size, uint32_t crc32)
|
||||
{
|
||||
mz_compat *compat = (mz_compat *)file;
|
||||
if (compat == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
return mz_zip_entry_close_raw(compat->handle, uncompressed_size, crc32);
|
||||
}
|
||||
|
||||
extern int ZEXPORT zipCloseFileInZip(zipFile file)
|
||||
{
|
||||
return zipCloseFileInZipRaw(file, 0, 0);
|
||||
}
|
||||
|
||||
extern int ZEXPORT zipCloseFileInZip64(zipFile file)
|
||||
{
|
||||
return zipCloseFileInZipRaw(file, 0, 0);
|
||||
}
|
||||
|
||||
extern int ZEXPORT zipClose(zipFile file, const char *global_comment)
|
||||
{
|
||||
return zipClose_64(file, global_comment);
|
||||
}
|
||||
|
||||
extern int ZEXPORT zipClose_64(zipFile file, const char *global_comment)
|
||||
{
|
||||
return zipClose2_64(file, global_comment, MZ_VERSION_MADEBY);
|
||||
}
|
||||
|
||||
extern int ZEXPORT zipClose2_64(zipFile file, const char *global_comment, uint16_t version_madeby)
|
||||
{
|
||||
mz_compat *compat = (mz_compat *)file;
|
||||
int32_t err = MZ_OK;
|
||||
|
||||
if (compat == NULL)
|
||||
return 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->stream != NULL)
|
||||
{
|
||||
mz_stream_close(compat->stream);
|
||||
mz_stream_delete(&compat->stream);
|
||||
}
|
||||
|
||||
free(compat);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
extern unzFile ZEXPORT unzOpen(const char *path)
|
||||
{
|
||||
return unzOpen64(path);
|
||||
}
|
||||
|
||||
extern unzFile ZEXPORT unzOpen64(const void *path)
|
||||
{
|
||||
zlib_filefunc64_def pzlib = mz_stream_os_get_interface();
|
||||
return unzOpen2(path, &pzlib);
|
||||
}
|
||||
|
||||
extern unzFile ZEXPORT unzOpen2(const char *path, zlib_filefunc_def *pzlib_filefunc_def)
|
||||
{
|
||||
return unzOpen2_64(path, pzlib_filefunc_def);
|
||||
}
|
||||
|
||||
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;
|
||||
void *stream = NULL;
|
||||
|
||||
if (mz_stream_create(&stream, (mz_stream_vtbl *)*pzlib_filefunc_def) == NULL)
|
||||
return NULL;
|
||||
|
||||
if (mz_stream_open(stream, path, mode) != MZ_OK)
|
||||
{
|
||||
mz_stream_delete(&stream);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
handle = mz_zip_open(stream, mode);
|
||||
|
||||
if (handle == NULL)
|
||||
{
|
||||
mz_stream_delete(&stream);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
compat = (mz_compat *)malloc(sizeof(mz_compat));
|
||||
compat->handle = handle;
|
||||
compat->stream = stream;
|
||||
|
||||
mz_zip_goto_first_entry(compat->handle);
|
||||
return (unzFile)compat;
|
||||
}
|
||||
|
||||
extern int ZEXPORT unzClose(unzFile file)
|
||||
{
|
||||
mz_compat *compat = (mz_compat *)file;
|
||||
int32_t err = MZ_OK;
|
||||
|
||||
if (compat == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
err = mz_zip_close(compat->handle);
|
||||
|
||||
if (compat->stream != NULL)
|
||||
{
|
||||
mz_stream_close(compat->stream);
|
||||
mz_stream_delete(&compat->stream);
|
||||
}
|
||||
|
||||
free(compat);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
extern int ZEXPORT unzGetGlobalInfo(unzFile file, unz_global_info* pglobal_info32)
|
||||
{
|
||||
mz_compat *compat = (mz_compat *)file;
|
||||
unz_global_info64 global_info64;
|
||||
int32_t err = MZ_OK;
|
||||
|
||||
if (compat == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
err = unzGetGlobalInfo64(file, &global_info64);
|
||||
if (err != UNZ_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;
|
||||
}
|
||||
|
||||
extern int ZEXPORT unzGetGlobalInfo64(unzFile file, unz_global_info64 *pglobal_info)
|
||||
{
|
||||
mz_compat *compat = (mz_compat *)file;
|
||||
const char *comment_ptr = NULL;
|
||||
int32_t err = MZ_OK;
|
||||
|
||||
if (compat == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
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);
|
||||
return err;
|
||||
}
|
||||
|
||||
extern int ZEXPORT unzGetGlobalComment(unzFile file, char *comment, uint16_t comment_size)
|
||||
{
|
||||
mz_compat *compat = (mz_compat *)file;
|
||||
const char *comment_ptr = NULL;
|
||||
int32_t err = MZ_OK;
|
||||
|
||||
if (comment == NULL || comment_size == 0)
|
||||
return MZ_PARAM_ERROR;
|
||||
err = mz_zip_get_comment(compat->handle, &comment_ptr);
|
||||
if (err == MZ_OK)
|
||||
strncpy(comment, comment_ptr, comment_size);
|
||||
return err;
|
||||
}
|
||||
|
||||
extern int ZEXPORT unzOpenCurrentFile3(unzFile file, int *method, int *level, int raw, const char *password)
|
||||
{
|
||||
mz_compat *compat = (mz_compat *)file;
|
||||
if (compat == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
if (method != NULL)
|
||||
*method = 0;
|
||||
if (level != NULL)
|
||||
*level = 0;
|
||||
return mz_zip_entry_read_open(compat->handle, (int16_t)raw, password);
|
||||
}
|
||||
|
||||
extern int ZEXPORT unzOpenCurrentFile(unzFile file)
|
||||
{
|
||||
return unzOpenCurrentFile3(file, NULL, NULL, 0, NULL);
|
||||
}
|
||||
|
||||
extern int ZEXPORT unzOpenCurrentFilePassword(unzFile file, const char *password)
|
||||
{
|
||||
return unzOpenCurrentFile3(file, NULL, NULL, 0, password);
|
||||
}
|
||||
|
||||
extern int ZEXPORT unzOpenCurrentFile2(unzFile file, int *method, int *level, int raw)
|
||||
{
|
||||
return unzOpenCurrentFile3(file, method, level, raw, NULL);
|
||||
}
|
||||
|
||||
extern int ZEXPORT unzReadCurrentFile(unzFile file, voidp buf, uint32_t len)
|
||||
{
|
||||
mz_compat *compat = (mz_compat *)file;
|
||||
if (compat == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
return mz_zip_entry_read(compat->handle, buf, len);
|
||||
}
|
||||
|
||||
extern int ZEXPORT unzCloseCurrentFile(unzFile file)
|
||||
{
|
||||
mz_compat *compat = (mz_compat *)file;
|
||||
if (compat == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
return mz_zip_entry_close(compat->handle);
|
||||
}
|
||||
|
||||
extern int ZEXPORT unzGetCurrentFileInfo(unzFile file, unz_file_info *pfile_info, char *filename,
|
||||
uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment, uint16_t comment_size)
|
||||
{
|
||||
mz_compat *compat = (mz_compat *)file;
|
||||
mz_zip_file *file_info = NULL;
|
||||
int16_t bytes_to_copy = 0;
|
||||
int32_t err = MZ_OK;
|
||||
|
||||
if (compat == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
err = mz_zip_entry_get_info(compat->handle, &file_info);
|
||||
|
||||
if ((err == MZ_OK) && (pfile_info != NULL))
|
||||
{
|
||||
pfile_info->version = file_info->version_madeby;
|
||||
pfile_info->version_needed = file_info->version_needed;
|
||||
pfile_info->flag = file_info->flag;
|
||||
pfile_info->compression_method = file_info->compression_method;
|
||||
pfile_info->dos_date = mz_zip_time_t_to_dos_date(file_info->modified_date);
|
||||
pfile_info->crc = file_info->crc;
|
||||
|
||||
pfile_info->size_filename = file_info->filename_size;
|
||||
pfile_info->size_file_extra = file_info->extrafield_size;
|
||||
pfile_info->size_file_comment = file_info->comment_size;
|
||||
|
||||
pfile_info->disk_num_start = (uint16_t)file_info->disk_number;
|
||||
pfile_info->internal_fa = file_info->internal_fa;
|
||||
pfile_info->external_fa = file_info->external_fa;
|
||||
|
||||
pfile_info->compressed_size = (uint32_t)file_info->compressed_size;
|
||||
pfile_info->uncompressed_size = (uint32_t)file_info->uncompressed_size;
|
||||
|
||||
if (filename_size > 0 && filename != NULL)
|
||||
{
|
||||
bytes_to_copy = filename_size;
|
||||
if (bytes_to_copy > file_info->filename_size)
|
||||
bytes_to_copy = file_info->filename_size;
|
||||
memcpy(filename, file_info->filename, bytes_to_copy);
|
||||
}
|
||||
if (extrafield_size > 0 && extrafield != NULL)
|
||||
{
|
||||
bytes_to_copy = extrafield_size;
|
||||
if (bytes_to_copy > file_info->extrafield_size)
|
||||
bytes_to_copy = file_info->extrafield_size;
|
||||
memcpy(extrafield, file_info->extrafield, bytes_to_copy);
|
||||
}
|
||||
if (comment_size > 0 && comment != 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);
|
||||
}
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
extern int ZEXPORT unzGetCurrentFileInfo64(unzFile file, unz_file_info64 * pfile_info, char *filename,
|
||||
uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment, uint16_t comment_size)
|
||||
{
|
||||
mz_compat *compat = (mz_compat *)file;
|
||||
mz_zip_file *file_info = NULL;
|
||||
int16_t bytes_to_copy = 0;
|
||||
int32_t err = MZ_OK;
|
||||
|
||||
if (compat == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
err = mz_zip_entry_get_info(compat->handle, &file_info);
|
||||
|
||||
if ((err == MZ_OK) && (pfile_info != NULL))
|
||||
{
|
||||
pfile_info->version = file_info->version_madeby;
|
||||
pfile_info->version_needed = file_info->version_needed;
|
||||
pfile_info->flag = file_info->flag;
|
||||
pfile_info->compression_method = file_info->compression_method;
|
||||
pfile_info->dos_date = mz_zip_time_t_to_dos_date(file_info->modified_date);
|
||||
pfile_info->crc = file_info->crc;
|
||||
|
||||
pfile_info->size_filename = file_info->filename_size;
|
||||
pfile_info->size_file_extra = file_info->extrafield_size;
|
||||
pfile_info->size_file_comment = file_info->comment_size;
|
||||
|
||||
pfile_info->disk_num_start = file_info->disk_number;
|
||||
pfile_info->internal_fa = file_info->internal_fa;
|
||||
pfile_info->external_fa = file_info->external_fa;
|
||||
|
||||
pfile_info->compressed_size = file_info->compressed_size;
|
||||
pfile_info->uncompressed_size = file_info->uncompressed_size;
|
||||
|
||||
if (filename_size > 0 && filename != NULL)
|
||||
{
|
||||
bytes_to_copy = filename_size;
|
||||
if (bytes_to_copy > file_info->filename_size)
|
||||
bytes_to_copy = file_info->filename_size;
|
||||
memcpy(filename, file_info->filename, bytes_to_copy);
|
||||
if (file_info->filename_size < filename_size)
|
||||
filename[file_info->filename_size] = 0;
|
||||
}
|
||||
|
||||
if (extrafield_size > 0 && extrafield != NULL)
|
||||
{
|
||||
bytes_to_copy = extrafield_size;
|
||||
if (bytes_to_copy > file_info->extrafield_size)
|
||||
bytes_to_copy = file_info->extrafield_size;
|
||||
memcpy(extrafield, file_info->extrafield, bytes_to_copy);
|
||||
}
|
||||
|
||||
if (comment_size > 0 && comment != 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;
|
||||
}
|
||||
|
||||
extern int ZEXPORT unzGoToFirstFile(unzFile file)
|
||||
{
|
||||
mz_compat *compat = (mz_compat *)file;
|
||||
if (compat == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
return mz_zip_goto_first_entry(compat->handle);
|
||||
}
|
||||
|
||||
extern int ZEXPORT unzGoToNextFile(unzFile file)
|
||||
{
|
||||
mz_compat *compat = (mz_compat *)file;
|
||||
if (compat == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
return mz_zip_goto_next_entry(compat->handle);
|
||||
}
|
||||
|
||||
extern int ZEXPORT unzLocateFile(unzFile file, const char *filename, unzFileNameComparer filename_compare_func)
|
||||
{
|
||||
mz_compat *compat = (mz_compat *)file;
|
||||
if (compat == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
return mz_zip_locate_entry(compat->handle, filename, filename_compare_func);
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
void fill_fopen_filefunc(zlib_filefunc_def *pzlib_filefunc_def)
|
||||
{
|
||||
if (pzlib_filefunc_def != NULL)
|
||||
*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();
|
||||
}
|
||||
|
||||
void fill_win32_filefunc(zlib_filefunc_def *pzlib_filefunc_def)
|
||||
{
|
||||
if (pzlib_filefunc_def != NULL)
|
||||
*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();
|
||||
}
|
||||
|
||||
void fill_win32_filefunc64A(zlib_filefunc64_def *pzlib_filefunc_def)
|
||||
{
|
||||
if (pzlib_filefunc_def != NULL)
|
||||
*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();
|
||||
}
|
||||
|
||||
-295
@@ -1,295 +0,0 @@
|
||||
/* mz_compat.h -- Backwards compatible interface for older versions
|
||||
Version 2.2.7, January 30th, 2018
|
||||
part of the MiniZip project
|
||||
|
||||
Copyright (C) 2010-2018 Nathan Moinvaziri
|
||||
https://github.com/nmoinvaz/minizip
|
||||
Copyright (C) 1998-2010 Gilles Vollant
|
||||
http://www.winimage.com/zLibDll/minizip.html
|
||||
|
||||
This program is distributed under the terms of the same license as zlib.
|
||||
See the accompanying LICENSE file for the full text of the license.
|
||||
*/
|
||||
|
||||
#ifndef _MZ_COMPAT_H
|
||||
#define _MZ_COMPAT_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#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
|
||||
# else
|
||||
# define DEF_MEM_LEVEL MAX_MEM_LEVEL
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#if defined(USE_FILE32API)
|
||||
# define MZ_USE_FILE32API
|
||||
# define fopen64 fopen
|
||||
# define ftello64 ftell
|
||||
# define fseeko64 fseek
|
||||
#else
|
||||
# if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || \
|
||||
defined(__OpenBSD__) || defined(__APPLE__) || defined(__ANDROID__)
|
||||
# define fopen64 fopen
|
||||
# define ftello64 ftello
|
||||
# define fseeko64 fseeko
|
||||
# endif
|
||||
# ifdef _MSC_VER
|
||||
# define fopen64 fopen
|
||||
# if (_MSC_VER >= 1400) && (!(defined(NO_MSCVER_FILE64_FUNC)))
|
||||
# define ftello64 _ftelli64
|
||||
# define fseeko64 _fseeki64
|
||||
# else /* old MSC */
|
||||
# define ftello64 ftell
|
||||
# define fseeko64 fseek
|
||||
# endif
|
||||
# endif
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#if defined(STRICTZIP) || defined(STRICTZIPUNZIP)
|
||||
/* like the STRICT of WIN32, we define a pointer that cannot be converted
|
||||
from (void*) without cast */
|
||||
typedef struct TagzipFile__ { int unused; } zip_file__;
|
||||
typedef zip_file__ *zipFile;
|
||||
#else
|
||||
typedef voidp zipFile;
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef void *zlib_filefunc_def;
|
||||
typedef void *zlib_filefunc64_def;
|
||||
typedef const char *zipcharpc;
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint32_t dos_date;
|
||||
uint16_t internal_fa; // internal file attributes 2 bytes
|
||||
uint32_t external_fa; // external file attributes 4 bytes
|
||||
} zip_fileinfo;
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#define ZIP_OK (0)
|
||||
#define ZIP_EOF (0)
|
||||
#define ZIP_ERRNO (Z_ERRNO)
|
||||
#define ZIP_PARAMERROR (-102)
|
||||
#define ZIP_BADZIPFILE (-103)
|
||||
#define ZIP_INTERNALERROR (-104)
|
||||
|
||||
#define Z_BZIP2ED (12)
|
||||
|
||||
#define APPEND_STATUS_CREATE (1)
|
||||
#define APPEND_STATUS_CREATEAFTER (2)
|
||||
#define APPEND_STATUS_ADDINZIP (3)
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
extern zipFile ZEXPORT zipOpen(const char *path, int append);
|
||||
extern zipFile ZEXPORT zipOpen64(const void *path, int append);
|
||||
extern zipFile ZEXPORT zipOpen2(const char *path, int append, const char **globalcomment,
|
||||
zlib_filefunc_def *pzlib_filefunc_def);
|
||||
extern zipFile ZEXPORT zipOpen2_64(const void *path, int append, const char **globalcomment,
|
||||
zlib_filefunc64_def *pzlib_filefunc_def);
|
||||
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
|
||||
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 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);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#if defined(STRICTUNZIP) || defined(STRICTZIPUNZIP)
|
||||
/* like the STRICT of WIN32, we define a pointer that cannot be converted
|
||||
from (void*) without cast */
|
||||
typedef struct TagunzFile__ { int unused; } unz_file__;
|
||||
typedef unz_file__ *unzFile;
|
||||
#else
|
||||
typedef voidp unzFile;
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#define UNZ_OK (0)
|
||||
#define UNZ_END_OF_LIST_OF_FILE (-100)
|
||||
#define UNZ_ERRNO (Z_ERRNO)
|
||||
#define UNZ_EOF (0)
|
||||
#define UNZ_PARAMERROR (-102)
|
||||
#define UNZ_BADZIPFILE (-103)
|
||||
#define UNZ_INTERNALERROR (-104)
|
||||
#define UNZ_CRCERROR (-105)
|
||||
#define UNZ_BADPASSWORD (-106)
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct unz_global_info64_s
|
||||
{
|
||||
uint64_t number_entry; // total number of entries in the central dir on this disk
|
||||
uint32_t number_disk_with_CD; // number the the disk with central dir, used for spanning ZIP
|
||||
uint16_t size_comment; // size of the global comment of the zipfile
|
||||
} unz_global_info64;
|
||||
|
||||
typedef struct unz_global_info_s
|
||||
{
|
||||
uint32_t number_entry; // total number of entries in the central dir on this disk
|
||||
uint32_t number_disk_with_CD; // number the the disk with central dir, used for spanning ZIP
|
||||
uint16_t size_comment; // size of the global comment of the zipfile
|
||||
} unz_global_info;
|
||||
|
||||
typedef struct unz_file_info64_s
|
||||
{
|
||||
uint16_t version; // version made by 2 bytes
|
||||
uint16_t version_needed; // version needed to extract 2 bytes
|
||||
uint16_t flag; // general purpose bit flag 2 bytes
|
||||
uint16_t compression_method; // compression method 2 bytes
|
||||
uint32_t 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
|
||||
|
||||
uint32_t disk_num_start; // disk number start 4 bytes
|
||||
uint16_t internal_fa; // internal file attributes 2 bytes
|
||||
uint32_t external_fa; // external file attributes 4 bytes
|
||||
|
||||
uint64_t disk_offset;
|
||||
|
||||
uint16_t size_file_extra_internal;
|
||||
} unz_file_info64;
|
||||
|
||||
typedef struct unz_file_info_s
|
||||
{
|
||||
uint16_t version; // version made by 2 bytes
|
||||
uint16_t version_needed; // version needed to extract 2 bytes
|
||||
uint16_t flag; // general purpose bit flag 2 bytes
|
||||
uint16_t compression_method; // compression method 2 bytes
|
||||
uint32_t 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 disk_num_start; // disk number start 2 bytes
|
||||
uint16_t internal_fa; // internal file attributes 2 bytes
|
||||
uint32_t external_fa; // external file attributes 4 bytes
|
||||
|
||||
uint64_t disk_offset;
|
||||
} unz_file_info;
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef int (*unzFileNameComparer)(unzFile file, const char *filename1, const char *filename2);
|
||||
typedef int (*unzIteratorFunction)(unzFile file);
|
||||
typedef int (*unzIteratorFunction2)(unzFile file, unz_file_info64 *pfile_info, char *filename,
|
||||
uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment, uint16_t comment_size);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
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 int ZEXPORT unzClose(unzFile file);
|
||||
extern int ZEXPORT unzGetGlobalInfo(unzFile file, unz_global_info* pglobal_info32);
|
||||
extern int ZEXPORT unzGetGlobalInfo64(unzFile file, unz_global_info64 *pglobal_info);
|
||||
extern int ZEXPORT unzGetGlobalComment(unzFile file, char *comment, uint16_t comment_size);
|
||||
extern int ZEXPORT unzOpenCurrentFile(unzFile file);
|
||||
extern int ZEXPORT unzOpenCurrentFilePassword(unzFile file, const char *password);
|
||||
extern int ZEXPORT unzOpenCurrentFile2(unzFile file, int *method, int *level, int raw);
|
||||
extern int ZEXPORT unzOpenCurrentFile3(unzFile file, int *method, int *level, int raw, const char *password);
|
||||
extern int ZEXPORT unzReadCurrentFile(unzFile file, voidp buf, uint32_t len);
|
||||
extern int ZEXPORT unzCloseCurrentFile(unzFile file);
|
||||
|
||||
extern int ZEXPORT unzGetCurrentFileInfo(unzFile file, unz_file_info *pfile_info, char *filename,
|
||||
uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment, uint16_t comment_size);
|
||||
extern int ZEXPORT unzGetCurrentFileInfo64(unzFile file, unz_file_info64 * pfile_info, char *filename,
|
||||
uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment, uint16_t comment_size);
|
||||
extern int ZEXPORT unzGoToFirstFile(unzFile file);
|
||||
extern int ZEXPORT unzGoToNextFile(unzFile file);
|
||||
extern int ZEXPORT unzLocateFile(unzFile file, const char *filename, unzFileNameComparer filename_compare_func);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
void fill_fopen_filefunc(zlib_filefunc_def *pzlib_filefunc_def);
|
||||
void fill_fopen64_filefunc(zlib_filefunc64_def *pzlib_filefunc_def);
|
||||
void fill_win32_filefunc(zlib_filefunc_def *pzlib_filefunc_def);
|
||||
void fill_win32_filefunc64(zlib_filefunc64_def *pzlib_filefunc_def);
|
||||
void fill_win32_filefunc64A(zlib_filefunc64_def *pzlib_filefunc_def);
|
||||
void fill_win32_filefunc64W(zlib_filefunc64_def *pzlib_filefunc_def);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#define check_file_exists mz_os_file_exists
|
||||
#define dosdate_to_tm mz_zip_dosdate_to_tm
|
||||
#define change_file_date mz_os_set_file_date
|
||||
#define get_file_date mz_os_get_file_date
|
||||
#define is_large_file(x) (mz_os_get_file_size(x) >= UINT32_MAX)
|
||||
#define makedir mz_make_dir
|
||||
#define get_file_crc(p,b,bs,rc) mz_get_file_crc(p,rc)
|
||||
|
||||
#define MKDIR mz_os_make_dir
|
||||
#define CHDIR chdir
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
-152
@@ -1,152 +0,0 @@
|
||||
/* mz_os.c -- System functions
|
||||
Version 2.2.7, January 30th, 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"
|
||||
#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;
|
||||
}
|
||||
-50
@@ -1,50 +0,0 @@
|
||||
/* mz_os.h -- System functions
|
||||
Version 2.2.7, January 30th, 2018
|
||||
part of the MiniZip project
|
||||
|
||||
Copyright (C) 2010-2018 Nathan Moinvaziri
|
||||
https://github.com/nmoinvaz/minizip
|
||||
Copyright (C) 1998-2010 Gilles Vollant
|
||||
http://www.winimage.com/zLibDll/minizip.html
|
||||
|
||||
This program is distributed under the terms of the same license as zlib.
|
||||
See the accompanying LICENSE file for the full text of the license.
|
||||
*/
|
||||
|
||||
#ifndef _MZ_OS_H
|
||||
#define _MZ_OS_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#if !defined(_WIN32) && !defined(MZ_USE_WIN32_API)
|
||||
#include "mz_os_posix.h"
|
||||
#include "mz_strm_posix.h"
|
||||
#else
|
||||
#include "mz_os_win32.h"
|
||||
#include "mz_strm_win32.h"
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
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_get_file_crc(const char *path, uint32_t *result_crc);
|
||||
// Gets the crc32 hash of a file
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,158 +0,0 @@
|
||||
/* mz_os_posix.c -- System functions for posix
|
||||
Version 2.2.7, January 30th, 2018
|
||||
part of the MiniZip project
|
||||
|
||||
Copyright (C) 2010-2018 Nathan Moinvaziri
|
||||
https://github.com/nmoinvaz/minizip
|
||||
|
||||
This program is distributed under the terms of the same license as zlib.
|
||||
See the accompanying LICENSE file for the full text of the license.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#if defined unix || defined __APPLE__
|
||||
# include <unistd.h>
|
||||
# include <utime.h>
|
||||
#endif
|
||||
#if defined __linux__
|
||||
# include <bsd/stdlib.h>
|
||||
#else
|
||||
# include <stdlib.h>
|
||||
#endif
|
||||
|
||||
#include "mz.h"
|
||||
#include "mz_strm.h"
|
||||
#include "mz_os.h"
|
||||
#include "mz_os_posix.h"
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t mz_posix_rand(uint8_t *buf, int32_t size)
|
||||
{
|
||||
arc4random_buf(buf, size);
|
||||
return size;
|
||||
}
|
||||
|
||||
int32_t mz_posix_file_exists(const char *path)
|
||||
{
|
||||
struct stat stat_info;
|
||||
|
||||
memset(&stat_info, 0, sizeof(stat_info));
|
||||
if (stat(path, &stat_info) == 0)
|
||||
return MZ_OK;
|
||||
|
||||
return MZ_EXIST_ERROR;
|
||||
}
|
||||
|
||||
int64_t mz_posix_get_file_size(const char *path)
|
||||
{
|
||||
struct stat stat_info;
|
||||
|
||||
memset(&stat_info, 0, sizeof(stat_info));
|
||||
if (stat(path, &stat_info) == 0)
|
||||
return stat_info.st_size;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t mz_posix_get_file_date(const char *path, time_t *modified_date, time_t *accessed_date, time_t *creation_date)
|
||||
{
|
||||
struct stat stat_info;
|
||||
char *name = NULL;
|
||||
size_t len = 0;
|
||||
int32_t err = MZ_INTERNAL_ERROR;
|
||||
|
||||
|
||||
memset(&stat_info, 0, sizeof(stat_info));
|
||||
|
||||
if (strcmp(path, "-") != 0)
|
||||
{
|
||||
// Not all systems allow stat'ing a file with / appended
|
||||
len = strlen(path);
|
||||
name = (char *)malloc(len + 1);
|
||||
strncpy(name, path, len + 1);
|
||||
name[len] = 0;
|
||||
if (name[len - 1] == '/')
|
||||
name[len - 1] = 0;
|
||||
|
||||
if (stat(name, &stat_info) == 0)
|
||||
{
|
||||
if (modified_date != NULL)
|
||||
*modified_date = stat_info.st_mtime;
|
||||
if (accessed_date != NULL)
|
||||
*accessed_date = stat_info.st_atime;
|
||||
// Creation date not supported
|
||||
if (creation_date != NULL)
|
||||
*creation_date = 0;
|
||||
|
||||
err = MZ_OK;
|
||||
}
|
||||
|
||||
free(name);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
int32_t mz_posix_set_file_date(const char *path, time_t modified_date, time_t accessed_date, time_t creation_date)
|
||||
{
|
||||
struct utimbuf ut;
|
||||
|
||||
ut.actime = accessed_date;
|
||||
ut.modtime = modified_date;
|
||||
// Creation date not supported
|
||||
|
||||
if (utime(path, &ut) != 0)
|
||||
return MZ_INTERNAL_ERROR;
|
||||
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_posix_make_dir(const char *path)
|
||||
{
|
||||
int32_t err = 0;
|
||||
|
||||
err = mkdir(path, 0755);
|
||||
|
||||
if (err != 0 && errno != EEXIST)
|
||||
return MZ_INTERNAL_ERROR;
|
||||
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
DIR* mz_posix_open_dir(const char *path)
|
||||
{
|
||||
return opendir(path);
|
||||
}
|
||||
|
||||
struct dirent* mz_posix_read_dir(DIR *dir)
|
||||
{
|
||||
if (dir == NULL)
|
||||
return NULL;
|
||||
return readdir(dir);
|
||||
}
|
||||
|
||||
int32_t mz_posix_close_dir(DIR *dir)
|
||||
{
|
||||
if (dir == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
if (closedir(dir) == -1)
|
||||
return MZ_INTERNAL_ERROR;
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_posix_is_dir(const char *path)
|
||||
{
|
||||
struct stat path_stat;
|
||||
stat(path, &path_stat);
|
||||
if (S_ISDIR(path_stat.st_mode))
|
||||
return MZ_OK;
|
||||
return MZ_EXIST_ERROR;
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
/* mz_os_posix.h -- System functions for posix
|
||||
Version 2.2.7, January 30th, 2018
|
||||
part of the MiniZip project
|
||||
|
||||
Copyright (C) 2010-2018 Nathan Moinvaziri
|
||||
https://github.com/nmoinvaz/minizip
|
||||
|
||||
This program is distributed under the terms of the same license as zlib.
|
||||
See the accompanying LICENSE file for the full text of the license.
|
||||
*/
|
||||
|
||||
#ifndef _MZ_OS_POSIX_H
|
||||
#define _MZ_OS_POSIX_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <time.h>
|
||||
#include <dirent.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#if defined(__APPLE__)
|
||||
#define MZ_VERSION_MADEBY (19)
|
||||
#elif defined(unix)
|
||||
#define MZ_VERSION_MADEBY (3)
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t mz_posix_rand(uint8_t *buf, int32_t size);
|
||||
int32_t mz_posix_file_exists(const char *path);
|
||||
int64_t mz_posix_get_file_size(const char *path);
|
||||
int32_t mz_posix_get_file_date(const char *path, time_t *modified_date, time_t *accessed_date, time_t *creation_date);
|
||||
int32_t mz_posix_set_file_date(const char *path, time_t modified_date, time_t accessed_date, time_t creation_date);
|
||||
int32_t mz_posix_make_dir(const char *path);
|
||||
DIR* mz_posix_open_dir(const char *path);
|
||||
struct
|
||||
dirent* mz_posix_read_dir(DIR *dir);
|
||||
int32_t mz_posix_close_dir(DIR *dir);
|
||||
int32_t mz_posix_is_dir(const char *path);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#define mz_os_rand mz_posix_rand
|
||||
#define mz_os_file_exists mz_posix_file_exists
|
||||
#define mz_os_get_file_size mz_posix_get_file_size
|
||||
#define mz_os_get_file_date mz_posix_get_file_date
|
||||
#define mz_os_set_file_date mz_posix_set_file_date
|
||||
#define mz_os_make_dir mz_posix_make_dir
|
||||
#define mz_os_open_dir mz_posix_open_dir
|
||||
#define mz_os_read_dir mz_posix_read_dir
|
||||
#define mz_os_close_dir mz_posix_close_dir
|
||||
#define mz_os_is_dir mz_posix_is_dir
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,316 +0,0 @@
|
||||
/* mz_os_win32.c -- System functions for Windows
|
||||
Version 2.2.7, January 30th, 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 <time.h>
|
||||
#include <direct.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <windows.h>
|
||||
#include <wincrypt.h>
|
||||
|
||||
#include "mz.h"
|
||||
|
||||
#include "mz_os.h"
|
||||
#include "mz_os_win32.h"
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#if defined(WINAPI_FAMILY_PARTITION) && (!(defined(MZ_USE_WINRT_API)))
|
||||
# if !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
|
||||
# define MZ_USE_WINRT_API 1
|
||||
# endif
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct DIR_int_s {
|
||||
void *find_handle;
|
||||
WIN32_FIND_DATAW find_data;
|
||||
struct dirent entry;
|
||||
uint8_t end;
|
||||
} DIR_int;
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t mz_win32_rand(uint8_t *buf, int32_t size)
|
||||
{
|
||||
HCRYPTPROV provider;
|
||||
unsigned __int64 pentium_tsc[1];
|
||||
int32_t len = 0;
|
||||
int32_t result = 0;
|
||||
|
||||
|
||||
if (CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT))
|
||||
{
|
||||
result = CryptGenRandom(provider, size, buf);
|
||||
CryptReleaseContext(provider, 0);
|
||||
if (result)
|
||||
return size;
|
||||
}
|
||||
|
||||
for (len = 0; len < (int)size; len += 1)
|
||||
{
|
||||
if (len % 8 == 0)
|
||||
QueryPerformanceCounter((LARGE_INTEGER *)pentium_tsc);
|
||||
buf[len] = ((unsigned char*)pentium_tsc)[len % 8];
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
wchar_t *mz_win32_unicode_path_create(const char *path)
|
||||
{
|
||||
wchar_t *path_wide = NULL;
|
||||
uint32_t path_wide_size = 0;
|
||||
|
||||
path_wide_size = MultiByteToWideChar(CP_UTF8, 0, path, -1, NULL, 0);
|
||||
path_wide = (wchar_t *)malloc((path_wide_size + 1) * sizeof(wchar_t));
|
||||
memset(path_wide, 0, sizeof(wchar_t) * (path_wide_size + 1));
|
||||
|
||||
MultiByteToWideChar(CP_UTF8, 0, path, -1, path_wide, path_wide_size);
|
||||
|
||||
return path_wide;
|
||||
}
|
||||
|
||||
void mz_win32_unicode_path_delete(wchar_t **path)
|
||||
{
|
||||
if (path != NULL)
|
||||
{
|
||||
free(*path);
|
||||
*path = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
int32_t mz_win32_file_exists(const char *path)
|
||||
{
|
||||
wchar_t *path_wide = NULL;
|
||||
DWORD attribs = 0;
|
||||
|
||||
|
||||
path_wide = mz_win32_unicode_path_create(path);
|
||||
attribs = GetFileAttributesW(path_wide);
|
||||
mz_win32_unicode_path_delete(&path_wide);
|
||||
|
||||
if (attribs == 0xFFFFFFFF)
|
||||
return MZ_EXIST_ERROR;
|
||||
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int64_t mz_win32_get_file_size(const char *path)
|
||||
{
|
||||
HANDLE handle = NULL;
|
||||
LARGE_INTEGER large_size;
|
||||
wchar_t *path_wide = NULL;
|
||||
|
||||
|
||||
path_wide = mz_win32_unicode_path_create(path);
|
||||
#ifdef MZ_USE_WINRT_API
|
||||
handle = CreateFile2W(path_wide, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||
#else
|
||||
handle = CreateFileW(path_wide, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||
#endif
|
||||
mz_win32_unicode_path_delete(&path_wide);
|
||||
|
||||
large_size.QuadPart = 0;
|
||||
|
||||
if (handle != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
GetFileSizeEx(handle, &large_size);
|
||||
CloseHandle(handle);
|
||||
}
|
||||
|
||||
return large_size.QuadPart;
|
||||
}
|
||||
|
||||
static void mz_win32_file_to_unix_time(FILETIME file_time, time_t *unix_time)
|
||||
{
|
||||
uint64_t quad_file_time = 0;
|
||||
quad_file_time = file_time.dwLowDateTime;
|
||||
quad_file_time |= ((uint64_t)file_time.dwHighDateTime << 32);
|
||||
*unix_time = (time_t)((quad_file_time - 116444736000000000LL) / 10000000);
|
||||
}
|
||||
|
||||
static void mz_win32_unix_to_file_time(time_t unix_time, FILETIME *file_time)
|
||||
{
|
||||
uint64_t quad_file_time = 0;
|
||||
quad_file_time = ((uint64_t)unix_time * 10000000) + 116444736000000000LL;
|
||||
file_time->dwHighDateTime = (quad_file_time >> 32);
|
||||
file_time->dwLowDateTime = (uint32_t)(quad_file_time);
|
||||
}
|
||||
|
||||
int32_t mz_win32_get_file_date(const char *path, time_t *modified_date, time_t *accessed_date, time_t *creation_date)
|
||||
{
|
||||
WIN32_FIND_DATAW ff32;
|
||||
HANDLE handle = NULL;
|
||||
wchar_t *path_wide = NULL;
|
||||
int32_t err = MZ_INTERNAL_ERROR;
|
||||
|
||||
path_wide = mz_win32_unicode_path_create(path);
|
||||
handle = FindFirstFileW(path_wide, &ff32);
|
||||
free(path_wide);
|
||||
|
||||
if (handle != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
if (modified_date != NULL)
|
||||
mz_win32_file_to_unix_time(ff32.ftLastWriteTime, modified_date);
|
||||
if (accessed_date != NULL)
|
||||
mz_win32_file_to_unix_time(ff32.ftLastAccessTime, accessed_date);
|
||||
if (creation_date != NULL)
|
||||
mz_win32_file_to_unix_time(ff32.ftCreationTime, creation_date);
|
||||
|
||||
FindClose(handle);
|
||||
err = MZ_OK;
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
int32_t mz_win32_set_file_date(const char *path, time_t modified_date, time_t accessed_date, time_t creation_date)
|
||||
{
|
||||
HANDLE handle = NULL;
|
||||
FILETIME ftm_creation, ftm_accessed, ftm_modified;
|
||||
wchar_t *path_wide = NULL;
|
||||
int32_t err = MZ_OK;
|
||||
|
||||
|
||||
path_wide = mz_win32_unicode_path_create(path);
|
||||
#ifdef MZ_USE_WINRT_API
|
||||
handle = CreateFile2W(path_wide, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
|
||||
#else
|
||||
handle = CreateFileW(path_wide, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
|
||||
#endif
|
||||
mz_win32_unicode_path_delete(&path_wide);
|
||||
|
||||
if (handle != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
GetFileTime(handle, &ftm_creation, &ftm_accessed, &ftm_modified);
|
||||
|
||||
if (modified_date != 0)
|
||||
mz_win32_unix_to_file_time(modified_date, &ftm_modified);
|
||||
if (accessed_date != 0)
|
||||
mz_win32_unix_to_file_time(accessed_date, &ftm_accessed);
|
||||
if (creation_date != 0)
|
||||
mz_win32_unix_to_file_time(creation_date, &ftm_creation);
|
||||
|
||||
if (SetFileTime(handle, &ftm_creation, &ftm_accessed, &ftm_modified) == 0)
|
||||
err = MZ_INTERNAL_ERROR;
|
||||
|
||||
CloseHandle(handle);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
int32_t mz_win32_make_dir(const char *path)
|
||||
{
|
||||
wchar_t *path_wide = NULL;
|
||||
int32_t err = 0;
|
||||
|
||||
|
||||
path_wide = mz_win32_unicode_path_create(path);
|
||||
err = _wmkdir(path_wide);
|
||||
mz_win32_unicode_path_delete(&path_wide);
|
||||
|
||||
if (err != 0 && errno != EEXIST)
|
||||
return MZ_INTERNAL_ERROR;
|
||||
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
DIR *mz_win32_open_dir(const char *path)
|
||||
{
|
||||
WIN32_FIND_DATAW find_data;
|
||||
DIR_int *dir_int = NULL;
|
||||
wchar_t *path_wide = NULL;
|
||||
char fixed_path[320];
|
||||
void *handle = NULL;
|
||||
|
||||
|
||||
fixed_path[0] = 0;
|
||||
mz_path_combine(fixed_path, path, sizeof(fixed_path));
|
||||
mz_path_combine(fixed_path, "*", sizeof(fixed_path));
|
||||
|
||||
path_wide = mz_win32_unicode_path_create(fixed_path);
|
||||
handle = FindFirstFileW(path_wide, &find_data);
|
||||
mz_win32_unicode_path_delete(&path_wide);
|
||||
|
||||
if (handle == INVALID_HANDLE_VALUE)
|
||||
return NULL;
|
||||
|
||||
dir_int = (DIR_int *)malloc(sizeof(DIR_int));
|
||||
dir_int->find_handle = handle;
|
||||
dir_int->end = 0;
|
||||
|
||||
memcpy(&dir_int->find_data, &find_data, sizeof(dir_int->find_data));
|
||||
|
||||
return (DIR *)dir_int;
|
||||
}
|
||||
|
||||
struct dirent* mz_win32_read_dir(DIR *dir)
|
||||
{
|
||||
DIR_int *dir_int;
|
||||
|
||||
if (dir == NULL)
|
||||
return NULL;
|
||||
|
||||
dir_int = (DIR_int *)dir;
|
||||
if (dir_int->end)
|
||||
return NULL;
|
||||
|
||||
WideCharToMultiByte(CP_UTF8, 0, dir_int->find_data.cFileName, -1,
|
||||
dir_int->entry.d_name, sizeof(dir_int->entry.d_name), NULL, NULL);
|
||||
|
||||
if (FindNextFileW(dir_int->find_handle, &dir_int->find_data) == 0)
|
||||
{
|
||||
if (GetLastError() != ERROR_NO_MORE_FILES)
|
||||
return NULL;
|
||||
|
||||
dir_int->end = 1;
|
||||
}
|
||||
|
||||
return &dir_int->entry;
|
||||
}
|
||||
|
||||
int32_t mz_win32_close_dir(DIR *dir)
|
||||
{
|
||||
DIR_int *dir_int;
|
||||
|
||||
if (dir == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
dir_int = (DIR_int *)dir;
|
||||
if (dir_int->find_handle != INVALID_HANDLE_VALUE)
|
||||
FindClose(dir_int->find_handle);
|
||||
free(dir_int);
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_win32_is_dir(const char *path)
|
||||
{
|
||||
wchar_t *path_wide = NULL;
|
||||
int32_t attribs = 0;
|
||||
|
||||
path_wide = mz_win32_unicode_path_create(path);
|
||||
attribs = GetFileAttributesW(path_wide);
|
||||
mz_win32_unicode_path_delete(&path_wide);
|
||||
|
||||
if (attribs != 0xFFFFFFFF)
|
||||
{
|
||||
if (attribs & FILE_ATTRIBUTE_DIRECTORY)
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
return MZ_EXIST_ERROR;
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
/* mz_os_win32.h -- System functions for Windows
|
||||
Version 2.2.7, January 30th, 2018
|
||||
part of the MiniZip project
|
||||
|
||||
Copyright (C) 2010-2018 Nathan Moinvaziri
|
||||
https://github.com/nmoinvaz/minizip
|
||||
|
||||
This program is distributed under the terms of the same license as zlib.
|
||||
See the accompanying LICENSE file for the full text of the license.
|
||||
*/
|
||||
|
||||
#ifndef _MZ_OS_WIN32_H
|
||||
#define _MZ_OS_WIN32_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#define MZ_VERSION_MADEBY (10)
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
struct dirent {
|
||||
char d_name[256];
|
||||
};
|
||||
typedef void* DIR;
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t mz_win32_rand(uint8_t *buf, int32_t size);
|
||||
int32_t mz_win32_file_exists(const char *path);
|
||||
int64_t mz_win32_get_file_size(const char *path);
|
||||
int32_t mz_win32_get_file_date(const char *path, time_t *modified_date, time_t *accessed_date, time_t *creation_date);
|
||||
int32_t mz_win32_set_file_date(const char *path, time_t modified_date, time_t accessed_date, time_t creation_date);
|
||||
int32_t mz_win32_make_dir(const char *path);
|
||||
DIR* mz_win32_open_dir(const char *path);
|
||||
struct
|
||||
dirent* mz_win32_read_dir(DIR *dir);
|
||||
int32_t mz_win32_close_dir(DIR *dir);
|
||||
int32_t mz_win32_is_dir(const char *path);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#define mz_os_rand mz_win32_rand
|
||||
#define mz_os_file_exists mz_win32_file_exists
|
||||
#define mz_os_get_file_size mz_win32_get_file_size
|
||||
#define mz_os_get_file_date mz_win32_get_file_date
|
||||
#define mz_os_set_file_date mz_win32_set_file_date
|
||||
#define mz_os_make_dir mz_win32_make_dir
|
||||
#define mz_os_open_dir mz_win32_open_dir
|
||||
#define mz_os_read_dir mz_win32_read_dir
|
||||
#define mz_os_close_dir mz_win32_close_dir
|
||||
#define mz_os_is_dir mz_win32_is_dir
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,349 +0,0 @@
|
||||
/* mz_strm_aes.c -- Stream for WinZip AES encryption
|
||||
Version 2.2.7, January 30th, 2018
|
||||
part of the MiniZip project
|
||||
|
||||
Copyright (C) 2010-2018 Nathan Moinvaziri
|
||||
https://github.com/nmoinvaz/minizip
|
||||
Copyright (C) 1998-2010 Brian Gladman, Worcester, UK
|
||||
|
||||
This program is distributed under the terms of the same license as zlib.
|
||||
See the accompanying LICENSE file for the full text of the license.
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "aes.h"
|
||||
#include "hmac.h"
|
||||
#include "pwd2key.h"
|
||||
|
||||
#include "mz.h"
|
||||
#include "mz_os.h"
|
||||
#include "mz_strm.h"
|
||||
#include "mz_strm_aes.h"
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#define MZ_AES_KEY_LENGTH(mode) (8 * (mode & 3) + 8)
|
||||
#define MZ_AES_KEY_LENGTH_MAX (32)
|
||||
#define MZ_AES_KEYING_ITERATIONS (1000)
|
||||
#define MZ_AES_SALT_LENGTH(mode) (4 * (mode & 3) + 4)
|
||||
#define MZ_AES_SALT_LENGTH_MAX (16)
|
||||
#define MZ_AES_MAC_LENGTH(mode) (10)
|
||||
#define MZ_AES_PW_LENGTH_MAX (128)
|
||||
#define MZ_AES_PW_VERIFY_SIZE (2)
|
||||
#define MZ_AES_AUTHCODE_SIZE (10)
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
mz_stream_vtbl mz_stream_aes_vtbl = {
|
||||
mz_stream_aes_open,
|
||||
mz_stream_aes_is_open,
|
||||
mz_stream_aes_read,
|
||||
mz_stream_aes_write,
|
||||
mz_stream_aes_tell,
|
||||
mz_stream_aes_seek,
|
||||
mz_stream_aes_close,
|
||||
mz_stream_aes_error,
|
||||
mz_stream_aes_create,
|
||||
mz_stream_aes_delete,
|
||||
mz_stream_aes_get_prop_int64
|
||||
};
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct mz_stream_aes_s {
|
||||
mz_stream stream;
|
||||
int32_t mode;
|
||||
int32_t error;
|
||||
int16_t initialized;
|
||||
uint8_t buffer[INT16_MAX];
|
||||
int64_t total_in;
|
||||
int64_t total_out;
|
||||
int16_t encryption_mode;
|
||||
const char *password;
|
||||
aes_encrypt_ctx encr_ctx[1];
|
||||
hmac_ctx auth_ctx[1];
|
||||
uint8_t nonce[AES_BLOCK_SIZE];
|
||||
uint8_t encr_bfr[AES_BLOCK_SIZE];
|
||||
uint32_t encr_pos;
|
||||
} mz_stream_aes;
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t mz_stream_aes_open(void *stream, const char *path, int32_t mode)
|
||||
{
|
||||
mz_stream_aes *aes = (mz_stream_aes *)stream;
|
||||
uint8_t kbuf[2 * MZ_AES_KEY_LENGTH_MAX + MZ_AES_PW_VERIFY_SIZE];
|
||||
uint8_t verify[MZ_AES_PW_VERIFY_SIZE];
|
||||
uint8_t verify_expected[MZ_AES_PW_VERIFY_SIZE];
|
||||
uint8_t salt_value[MZ_AES_SALT_LENGTH_MAX];
|
||||
int32_t salt_length = 0;
|
||||
const char *password = path;
|
||||
int32_t password_length = 0;
|
||||
int32_t key_length = 0;
|
||||
|
||||
aes->total_in = 0;
|
||||
aes->total_out = 0;
|
||||
aes->initialized = 0;
|
||||
|
||||
if (mz_stream_is_open(aes->stream.base) != MZ_OK)
|
||||
return MZ_STREAM_ERROR;
|
||||
|
||||
if (password == NULL)
|
||||
password = aes->password;
|
||||
if (password == NULL)
|
||||
return MZ_PARAM_ERROR;
|
||||
password_length = (int32_t)strlen(password);
|
||||
if (password_length > MZ_AES_PW_LENGTH_MAX)
|
||||
return MZ_PARAM_ERROR;
|
||||
|
||||
if (aes->encryption_mode < 1 || aes->encryption_mode > 3)
|
||||
return MZ_PARAM_ERROR;
|
||||
salt_length = MZ_AES_SALT_LENGTH(aes->encryption_mode);
|
||||
|
||||
if (mode & MZ_OPEN_MODE_WRITE)
|
||||
{
|
||||
mz_os_rand(salt_value, salt_length);
|
||||
}
|
||||
else if (mode & MZ_OPEN_MODE_READ)
|
||||
{
|
||||
if (mz_stream_read(aes->stream.base, salt_value, salt_length) != salt_length)
|
||||
return MZ_STREAM_ERROR;
|
||||
}
|
||||
|
||||
key_length = MZ_AES_KEY_LENGTH(aes->encryption_mode);
|
||||
// Derive the encryption and authentication keys and the password verifier
|
||||
derive_key((const uint8_t *)password, password_length, salt_value, salt_length,
|
||||
MZ_AES_KEYING_ITERATIONS, kbuf, 2 * key_length + MZ_AES_PW_VERIFY_SIZE);
|
||||
|
||||
// Initialize the encryption nonce and buffer pos
|
||||
aes->encr_pos = AES_BLOCK_SIZE;
|
||||
memset(aes->nonce, 0, AES_BLOCK_SIZE * sizeof(uint8_t));
|
||||
|
||||
// Initialize for encryption using key 1
|
||||
aes_encrypt_key(kbuf, key_length, aes->encr_ctx);
|
||||
|
||||
// Initialize for authentication using key 2
|
||||
hmac_sha_begin(HMAC_SHA1, aes->auth_ctx);
|
||||
hmac_sha_key(kbuf + key_length, key_length, aes->auth_ctx);
|
||||
|
||||
memcpy(verify, kbuf + 2 * key_length, MZ_AES_PW_VERIFY_SIZE);
|
||||
|
||||
if (mode & MZ_OPEN_MODE_WRITE)
|
||||
{
|
||||
if (mz_stream_write(aes->stream.base, salt_value, salt_length) != salt_length)
|
||||
return MZ_STREAM_ERROR;
|
||||
|
||||
aes->total_out += salt_length;
|
||||
|
||||
if (mz_stream_write(aes->stream.base, verify, MZ_AES_PW_VERIFY_SIZE) != MZ_AES_PW_VERIFY_SIZE)
|
||||
return MZ_STREAM_ERROR;
|
||||
|
||||
aes->total_out += MZ_AES_PW_VERIFY_SIZE;
|
||||
}
|
||||
else if (mode & MZ_OPEN_MODE_READ)
|
||||
{
|
||||
aes->total_in += salt_length;
|
||||
|
||||
if (mz_stream_read(aes->stream.base, verify_expected, MZ_AES_PW_VERIFY_SIZE) != MZ_AES_PW_VERIFY_SIZE)
|
||||
return MZ_STREAM_ERROR;
|
||||
|
||||
aes->total_in += MZ_AES_PW_VERIFY_SIZE;
|
||||
|
||||
if (memcmp(verify_expected, verify, MZ_AES_PW_VERIFY_SIZE) != 0)
|
||||
return MZ_PASSWORD_ERROR;
|
||||
}
|
||||
|
||||
aes->mode = mode;
|
||||
aes->initialized = 1;
|
||||
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_stream_aes_is_open(void *stream)
|
||||
{
|
||||
mz_stream_aes *aes = (mz_stream_aes *)stream;
|
||||
if (aes->initialized == 0)
|
||||
return MZ_STREAM_ERROR;
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
static int32_t mz_stream_aes_encrypt_data(void *stream, uint8_t *buf, int32_t size)
|
||||
{
|
||||
mz_stream_aes *aes = (mz_stream_aes *)stream;
|
||||
uint32_t pos = aes->encr_pos;
|
||||
uint32_t i = 0;
|
||||
|
||||
while (i < (uint32_t)size)
|
||||
{
|
||||
if (pos == AES_BLOCK_SIZE)
|
||||
{
|
||||
uint32_t j = 0;
|
||||
|
||||
// Increment encryption nonce
|
||||
while (j < 8 && !++aes->nonce[j])
|
||||
j += 1;
|
||||
|
||||
// Encrypt the nonce to form next xor buffer
|
||||
aes_encrypt(aes->nonce, aes->encr_bfr, aes->encr_ctx);
|
||||
pos = 0;
|
||||
}
|
||||
|
||||
buf[i++] ^= aes->encr_bfr[pos++];
|
||||
}
|
||||
|
||||
aes->encr_pos = pos;
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_stream_aes_read(void *stream, void *buf, int32_t size)
|
||||
{
|
||||
mz_stream_aes *aes = (mz_stream_aes *)stream;
|
||||
int32_t read = 0;
|
||||
read = mz_stream_read(aes->stream.base, buf, size);
|
||||
if (read > 0)
|
||||
{
|
||||
hmac_sha_data((uint8_t *)buf, read, aes->auth_ctx);
|
||||
mz_stream_aes_encrypt_data(stream, (uint8_t *)buf, read);
|
||||
}
|
||||
|
||||
aes->total_in += read;
|
||||
return read;
|
||||
}
|
||||
|
||||
int32_t mz_stream_aes_write(void *stream, const void *buf, int32_t size)
|
||||
{
|
||||
mz_stream_aes *aes = (mz_stream_aes *)stream;
|
||||
int32_t written = 0;
|
||||
|
||||
if (size > sizeof(aes->buffer))
|
||||
return MZ_STREAM_ERROR;
|
||||
|
||||
memcpy(aes->buffer, buf, size);
|
||||
mz_stream_aes_encrypt_data(stream, (uint8_t *)aes->buffer, size);
|
||||
hmac_sha_data((uint8_t *)aes->buffer, size, aes->auth_ctx);
|
||||
|
||||
written = mz_stream_write(aes->stream.base, aes->buffer, size);
|
||||
if (written > 0)
|
||||
aes->total_out += written;
|
||||
return written;
|
||||
}
|
||||
|
||||
int64_t mz_stream_aes_tell(void *stream)
|
||||
{
|
||||
mz_stream_aes *aes = (mz_stream_aes *)stream;
|
||||
return mz_stream_tell(aes->stream.base);
|
||||
}
|
||||
|
||||
int32_t mz_stream_aes_seek(void *stream, int64_t offset, int32_t origin)
|
||||
{
|
||||
mz_stream_aes *aes = (mz_stream_aes *)stream;
|
||||
return mz_stream_seek(aes->stream.base, offset, origin);
|
||||
}
|
||||
|
||||
int32_t mz_stream_aes_close(void *stream)
|
||||
{
|
||||
mz_stream_aes *aes = (mz_stream_aes *)stream;
|
||||
uint8_t authcode[MZ_AES_AUTHCODE_SIZE];
|
||||
uint8_t verify_authcode[MZ_AES_AUTHCODE_SIZE];
|
||||
|
||||
if (MZ_AES_MAC_LENGTH(aes->encryption_mode) != MZ_AES_AUTHCODE_SIZE)
|
||||
return MZ_STREAM_ERROR;
|
||||
hmac_sha_end(authcode, MZ_AES_MAC_LENGTH(aes->encryption_mode), aes->auth_ctx);
|
||||
|
||||
if (aes->mode & MZ_OPEN_MODE_WRITE)
|
||||
{
|
||||
if (mz_stream_write(aes->stream.base, authcode, MZ_AES_AUTHCODE_SIZE) != MZ_AES_AUTHCODE_SIZE)
|
||||
return MZ_STREAM_ERROR;
|
||||
|
||||
aes->total_out += MZ_AES_AUTHCODE_SIZE;
|
||||
}
|
||||
else if (aes->mode & MZ_OPEN_MODE_READ)
|
||||
{
|
||||
if (mz_stream_read(aes->stream.base, verify_authcode, MZ_AES_AUTHCODE_SIZE) != MZ_AES_AUTHCODE_SIZE)
|
||||
return MZ_STREAM_ERROR;
|
||||
|
||||
aes->total_in += MZ_AES_AUTHCODE_SIZE;
|
||||
|
||||
if (memcmp(authcode, verify_authcode, MZ_AES_AUTHCODE_SIZE) != 0)
|
||||
return MZ_CRC_ERROR;
|
||||
}
|
||||
|
||||
aes->initialized = 0;
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_stream_aes_error(void *stream)
|
||||
{
|
||||
mz_stream_aes *aes = (mz_stream_aes *)stream;
|
||||
return aes->error;
|
||||
}
|
||||
|
||||
void mz_stream_aes_set_password(void *stream, const char *password)
|
||||
{
|
||||
mz_stream_aes *aes = (mz_stream_aes *)stream;
|
||||
aes->password = password;
|
||||
}
|
||||
|
||||
void mz_stream_aes_set_encryption_mode(void *stream, int16_t encryption_mode)
|
||||
{
|
||||
mz_stream_aes *aes = (mz_stream_aes *)stream;
|
||||
aes->encryption_mode = encryption_mode;
|
||||
}
|
||||
|
||||
int32_t mz_stream_aes_get_prop_int64(void *stream, int32_t prop, int64_t *value)
|
||||
{
|
||||
mz_stream_aes *aes = (mz_stream_aes *)stream;
|
||||
switch (prop)
|
||||
{
|
||||
case MZ_STREAM_PROP_TOTAL_IN:
|
||||
*value = aes->total_in;
|
||||
return MZ_OK;
|
||||
case MZ_STREAM_PROP_TOTAL_OUT:
|
||||
*value = aes->total_out;
|
||||
return MZ_OK;
|
||||
case MZ_STREAM_PROP_HEADER_SIZE:
|
||||
*value = MZ_AES_SALT_LENGTH(aes->encryption_mode) + MZ_AES_PW_VERIFY_SIZE;
|
||||
return MZ_OK;
|
||||
case MZ_STREAM_PROP_FOOTER_SIZE:
|
||||
*value = MZ_AES_AUTHCODE_SIZE;
|
||||
return MZ_OK;
|
||||
}
|
||||
return MZ_EXIST_ERROR;
|
||||
}
|
||||
|
||||
void *mz_stream_aes_create(void **stream)
|
||||
{
|
||||
mz_stream_aes *aes = NULL;
|
||||
|
||||
aes = (mz_stream_aes *)malloc(sizeof(mz_stream_aes));
|
||||
if (aes != NULL)
|
||||
{
|
||||
memset(aes, 0, sizeof(mz_stream_aes));
|
||||
aes->stream.vtbl = &mz_stream_aes_vtbl;
|
||||
aes->encryption_mode = MZ_AES_ENCRYPTION_MODE_256;
|
||||
}
|
||||
if (stream != NULL)
|
||||
*stream = aes;
|
||||
|
||||
return aes;
|
||||
}
|
||||
|
||||
void mz_stream_aes_delete(void **stream)
|
||||
{
|
||||
mz_stream_aes *aes = NULL;
|
||||
if (stream == NULL)
|
||||
return;
|
||||
aes = (mz_stream_aes *)*stream;
|
||||
if (aes != NULL)
|
||||
free(aes);
|
||||
*stream = NULL;
|
||||
}
|
||||
|
||||
void *mz_stream_aes_get_interface(void)
|
||||
{
|
||||
return (void *)&mz_stream_aes_vtbl;
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
/* mz_strm_aes.h -- Stream for WinZIP AES encryption
|
||||
Version 2.2.7, January 30th, 2018
|
||||
part of the MiniZip project
|
||||
|
||||
Copyright (C) 2010-2018 Nathan Moinvaziri
|
||||
https://github.com/nmoinvaz/minizip
|
||||
|
||||
This program is distributed under the terms of the same license as zlib.
|
||||
See the accompanying LICENSE file for the full text of the license.
|
||||
*/
|
||||
|
||||
#ifndef _MZ_STREAM_AES_H
|
||||
#define _MZ_STREAM_AES_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t mz_stream_aes_open(void *stream, const char *filename, int32_t mode);
|
||||
int32_t mz_stream_aes_is_open(void *stream);
|
||||
int32_t mz_stream_aes_read(void *stream, void *buf, int32_t size);
|
||||
int32_t mz_stream_aes_write(void *stream, const void *buf, int32_t size);
|
||||
int64_t mz_stream_aes_tell(void *stream);
|
||||
int32_t mz_stream_aes_seek(void *stream, int64_t offset, int32_t origin);
|
||||
int32_t mz_stream_aes_close(void *stream);
|
||||
int32_t mz_stream_aes_error(void *stream);
|
||||
|
||||
void mz_stream_aes_set_password(void *stream, const char *password);
|
||||
void mz_stream_aes_set_encryption_mode(void *stream, int16_t encryption_mode);
|
||||
|
||||
int32_t mz_stream_aes_get_prop_int64(void *stream, int32_t prop, int64_t *value);
|
||||
|
||||
void* mz_stream_aes_create(void **stream);
|
||||
void mz_stream_aes_delete(void **stream);
|
||||
|
||||
void* mz_stream_aes_get_interface(void);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,325 +0,0 @@
|
||||
/* mz_strm_crypt.c -- Code for traditional PKWARE encryption
|
||||
Version 2.2.7, January 30th, 2018
|
||||
part of the MiniZip project
|
||||
|
||||
Copyright (C) 2010-2018 Nathan Moinvaziri
|
||||
https://github.com/nmoinvaz/minizip
|
||||
Copyright (C) 1998-2005 Gilles Vollant
|
||||
Modifications for Info-ZIP crypting
|
||||
http://www.winimage.com/zLibDll/minizip.html
|
||||
Copyright (C) 2003 Terry Thorsen
|
||||
|
||||
This code is a modified version of crypting code in Info-ZIP distribution
|
||||
|
||||
Copyright (C) 1990-2000 Info-ZIP. All rights reserved.
|
||||
|
||||
This program is distributed under the terms of the same license as zlib.
|
||||
See the accompanying LICENSE file for the full text of the license.
|
||||
|
||||
This encryption code is a direct transcription of the algorithm from
|
||||
Roger Schlafly, described by Phil Katz in the file appnote.txt. This
|
||||
file (appnote.txt) is distributed with the PKZIP program (even in the
|
||||
version without encryption capabilities).
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "zlib.h"
|
||||
|
||||
#include "mz.h"
|
||||
#include "mz_os.h"
|
||||
#include "mz_strm.h"
|
||||
#include "mz_strm_crypt.h"
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#define RAND_HEAD_LEN 12
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
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))
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
static uint8_t mz_stream_crypt_decrypt_byte(uint32_t *keys)
|
||||
{
|
||||
unsigned temp; /* POTENTIAL BUG: temp*(temp^1) may overflow in an
|
||||
* unpredictable manner on 16-bit systems; not a problem
|
||||
* with any known compiler so far, though */
|
||||
|
||||
temp = ((uint32_t)(*(keys+2)) & 0xffff) | 2;
|
||||
return (uint8_t)(((temp * (temp ^ 1)) >> 8) & 0xff);
|
||||
}
|
||||
|
||||
static uint8_t mz_stream_crypt_update_keys(uint32_t *keys, const z_crc_t *crc_32_tab, int32_t c)
|
||||
{
|
||||
#define CRC32(c, b) ((*(crc_32_tab+(((uint32_t)(c) ^ (b)) & 0xff))) ^ ((c) >> 8))
|
||||
|
||||
(*(keys+0)) = (uint32_t)CRC32((*(keys+0)), c);
|
||||
(*(keys+1)) += (*(keys+0)) & 0xff;
|
||||
(*(keys+1)) = (*(keys+1)) * 134775813L + 1;
|
||||
{
|
||||
register int32_t keyshift = (int32_t)((*(keys + 1)) >> 24);
|
||||
(*(keys+2)) = (uint32_t)CRC32((*(keys+2)), keyshift);
|
||||
}
|
||||
return (uint8_t)c;
|
||||
}
|
||||
|
||||
static void mz_stream_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 verify1 = 0;
|
||||
uint8_t verify2 = 0;
|
||||
uint8_t header[RAND_HEAD_LEN];
|
||||
const char *password = path;
|
||||
|
||||
crypt->total_in = 0;
|
||||
crypt->total_out = 0;
|
||||
crypt->initialized = 0;
|
||||
|
||||
if (mz_stream_is_open(crypt->stream.base) != MZ_OK)
|
||||
return MZ_STREAM_ERROR;
|
||||
|
||||
if (password == NULL)
|
||||
password = crypt->password;
|
||||
if (password == NULL)
|
||||
return MZ_STREAM_ERROR;
|
||||
|
||||
crypt->crc_32_tab = get_crc_table();
|
||||
if (crypt->crc_32_tab == NULL)
|
||||
return MZ_STREAM_ERROR;
|
||||
|
||||
mz_stream_crypt_init_keys(password, crypt->keys, crypt->crc_32_tab);
|
||||
|
||||
if (mode & MZ_OPEN_MODE_WRITE)
|
||||
{
|
||||
// First generate RAND_HEAD_LEN - 2 random bytes.
|
||||
mz_os_rand(header, RAND_HEAD_LEN - 2);
|
||||
|
||||
// Encrypt random header (last two bytes is high word of crc)
|
||||
for (i = 0; i < RAND_HEAD_LEN - 2; i++)
|
||||
header[i] = (uint8_t)zencode(crypt->keys, crypt->crc_32_tab, header[i], t);
|
||||
|
||||
header[i++] = (uint8_t)zencode(crypt->keys, crypt->crc_32_tab, crypt->verify1, t);
|
||||
header[i++] = (uint8_t)zencode(crypt->keys, crypt->crc_32_tab, crypt->verify2, t);
|
||||
|
||||
if (mz_stream_write(crypt->stream.base, header, RAND_HEAD_LEN) != RAND_HEAD_LEN)
|
||||
return MZ_STREAM_ERROR;
|
||||
|
||||
crypt->total_out += RAND_HEAD_LEN;
|
||||
}
|
||||
else if (mode & MZ_OPEN_MODE_READ)
|
||||
{
|
||||
if (mz_stream_read(crypt->stream.base, header, RAND_HEAD_LEN) != RAND_HEAD_LEN)
|
||||
return MZ_STREAM_ERROR;
|
||||
|
||||
for (i = 0; i < RAND_HEAD_LEN - 2; i++)
|
||||
header[i] = (uint8_t)zdecode(crypt->keys, crypt->crc_32_tab, header[i]);
|
||||
|
||||
verify1 = (uint8_t)zdecode(crypt->keys, crypt->crc_32_tab, header[i++]);
|
||||
verify2 = (uint8_t)zdecode(crypt->keys, crypt->crc_32_tab, header[i++]);
|
||||
|
||||
// Older versions used 2 byte check, newer versions use 1 byte check.
|
||||
if ((verify2 != 0) && (verify2 != crypt->verify2))
|
||||
return MZ_PASSWORD_ERROR;
|
||||
|
||||
crypt->total_in += RAND_HEAD_LEN;
|
||||
}
|
||||
|
||||
crypt->initialized = 1;
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_stream_crypt_is_open(void *stream)
|
||||
{
|
||||
mz_stream_crypt *crypt = (mz_stream_crypt *)stream;
|
||||
if (crypt->initialized == 0)
|
||||
return MZ_STREAM_ERROR;
|
||||
return MZ_OK;
|
||||
}
|
||||
|
||||
int32_t mz_stream_crypt_read(void *stream, void *buf, int32_t size)
|
||||
{
|
||||
mz_stream_crypt *crypt = (mz_stream_crypt *)stream;
|
||||
uint8_t *buf_ptr = (uint8_t *)buf;
|
||||
uint32_t read = 0;
|
||||
uint32_t i = 0;
|
||||
|
||||
read = mz_stream_read(crypt->stream.base, buf, size);
|
||||
|
||||
for (i = 0; i < read; i++)
|
||||
buf_ptr[i] = (uint8_t)zdecode(crypt->keys, crypt->crc_32_tab, buf_ptr[i]);
|
||||
|
||||
crypt->total_in += read;
|
||||
return read;
|
||||
}
|
||||
|
||||
int32_t mz_stream_crypt_write(void *stream, const void *buf, int32_t size)
|
||||
{
|
||||
mz_stream_crypt *crypt = (mz_stream_crypt *)stream;
|
||||
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;
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
/* mz_strm_crypt.h -- Code for traditional PKWARE encryption
|
||||
Version 2.2.7, January 30th, 2018
|
||||
part of the MiniZip project
|
||||
|
||||
Copyright (C) 2010-2018 Nathan Moinvaziri
|
||||
https://github.com/nmoinvaz/minizip
|
||||
Copyright (C) 1998-2005 Gilles Vollant
|
||||
Modifications for Info-ZIP crypting
|
||||
http://www.winimage.com/zLibDll/minizip.html
|
||||
Copyright (C) 2003 Terry Thorsen
|
||||
|
||||
This code is a modified version of crypting code in Info-ZIP distribution
|
||||
|
||||
Copyright (C) 1990-2000 Info-ZIP. All rights reserved.
|
||||
|
||||
This program is distributed under the terms of the same license as zlib.
|
||||
See the accompanying LICENSE file for the full text of the license.
|
||||
*/
|
||||
|
||||
#ifndef _MZ_STREAM_CRYPT_H
|
||||
#define _MZ_STREAM_CRYPT_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t mz_stream_crypt_open(void *stream, const char *filename, int32_t mode);
|
||||
int32_t mz_stream_crypt_is_open(void *stream);
|
||||
int32_t mz_stream_crypt_read(void *stream, void *buf, int32_t size);
|
||||
int32_t mz_stream_crypt_write(void *stream, const void *buf, int32_t size);
|
||||
int64_t mz_stream_crypt_tell(void *stream);
|
||||
int32_t mz_stream_crypt_seek(void *stream, int64_t offset, int32_t origin);
|
||||
int32_t mz_stream_crypt_close(void *stream);
|
||||
int32_t mz_stream_crypt_error(void *stream);
|
||||
|
||||
void mz_stream_crypt_set_password(void *stream, const char *password);
|
||||
void mz_stream_crypt_set_verify(void *stream, uint8_t verify1, uint8_t verify2);
|
||||
void mz_stream_crypt_get_verify(void *stream, uint8_t *verify1, uint8_t *verify2);
|
||||
int32_t mz_stream_crypt_get_prop_int64(void *stream, int32_t prop, int64_t *value);
|
||||
|
||||
void* mz_stream_crypt_create(void **stream);
|
||||
void mz_stream_crypt_delete(void **stream);
|
||||
|
||||
void* mz_stream_crypt_get_interface(void);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,67 +0,0 @@
|
||||
/* mz_strm_posix.h -- Stream for filesystem access for posix/linux
|
||||
Version 2.2.7, January 30th, 2018
|
||||
part of the MiniZip project
|
||||
|
||||
Copyright (C) 2010-2018 Nathan Moinvaziri
|
||||
https://github.com/nmoinvaz/minizip
|
||||
Copyright (C) 2009-2010 Mathias Svensson
|
||||
Modifications for Zip64 support
|
||||
http://result42.com
|
||||
Copyright (C) 1998-2010 Gilles Vollant
|
||||
http://www.winimage.com/zLibDll/minizip.html
|
||||
|
||||
This program is distributed under the terms of the same license as zlib.
|
||||
See the accompanying LICENSE file for the full text of the license.
|
||||
*/
|
||||
|
||||
#ifndef _MZ_STREAM_POSIX_H
|
||||
#define _MZ_STREAM_POSIX_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t mz_stream_posix_open(void *stream, const char *path, int32_t mode);
|
||||
int32_t mz_stream_posix_is_open(void *stream);
|
||||
int32_t mz_stream_posix_read(void *stream, void *buf, int32_t size);
|
||||
int32_t mz_stream_posix_write(void *stream, const void *buf, int32_t size);
|
||||
int64_t mz_stream_posix_tell(void *stream);
|
||||
int32_t mz_stream_posix_seek(void *stream, int64_t offset, int32_t origin);
|
||||
int32_t mz_stream_posix_close(void *stream);
|
||||
int32_t mz_stream_posix_error(void *stream);
|
||||
|
||||
void* mz_stream_posix_create(void **stream);
|
||||
void mz_stream_posix_delete(void **stream);
|
||||
|
||||
void* mz_stream_posix_get_interface(void);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#if !defined(_WIN32) && !defined(MZ_USE_WIN32_API)
|
||||
#define mz_stream_os_open mz_stream_posix_open
|
||||
#define mz_stream_os_is_open mz_stream_posix_is_open
|
||||
#define mz_stream_os_read mz_stream_posix_read
|
||||
#define mz_stream_os_write mz_stream_posix_write
|
||||
#define mz_stream_os_tell mz_stream_posix_tell
|
||||
#define mz_stream_os_seek mz_stream_posix_seek
|
||||
#define mz_stream_os_close mz_stream_posix_close
|
||||
#define mz_stream_os_error mz_stream_posix_error
|
||||
|
||||
#define mz_stream_os_create mz_stream_posix_create
|
||||
#define mz_stream_os_delete mz_stream_posix_delete
|
||||
|
||||
#define mz_stream_os_get_interface \
|
||||
mz_stream_posix_get_interface
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,67 +0,0 @@
|
||||
/* mz_sstrm_win32.h -- Stream for filesystem access for windows
|
||||
Version 2.2.7, January 30th, 2018
|
||||
part of the MiniZip project
|
||||
|
||||
Copyright (C) 2010-2018 Nathan Moinvaziri
|
||||
https://github.com/nmoinvaz/minizip
|
||||
Copyright (C) 2009-2010 Mathias Svensson
|
||||
Modifications for Zip64 support
|
||||
http://result42.com
|
||||
Copyright (C) 1998-2010 Gilles Vollant
|
||||
http://www.winimage.com/zLibDll/minizip.html
|
||||
|
||||
This program is distributed under the terms of the same license as zlib.
|
||||
See the accompanying LICENSE file for the full text of the license.
|
||||
*/
|
||||
|
||||
#ifndef _MZ_STREAM_WIN32_H
|
||||
#define _MZ_STREAM_WIN32_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t mz_stream_win32_open(void *stream, const char *path, int32_t mode);
|
||||
int32_t mz_stream_win32_is_open(void *stream);
|
||||
int32_t mz_stream_win32_read(void *stream, void *buf, int32_t size);
|
||||
int32_t mz_stream_win32_write(void *stream, const void *buf, int32_t size);
|
||||
int64_t mz_stream_win32_tell(void *stream);
|
||||
int32_t mz_stream_win32_seek(void *stream, int64_t offset, int32_t origin);
|
||||
int32_t mz_stream_win32_close(void *stream);
|
||||
int32_t mz_stream_win32_error(void *stream);
|
||||
|
||||
void* mz_stream_win32_create(void **stream);
|
||||
void mz_stream_win32_delete(void **stream);
|
||||
|
||||
void* mz_stream_win32_get_interface(void);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#if defined(_WIN32) || defined(MZ_USE_WIN32_API)
|
||||
#define mz_stream_os_open mz_stream_win32_open
|
||||
#define mz_stream_os_is_open mz_stream_win32_is_open
|
||||
#define mz_stream_os_read mz_stream_win32_read
|
||||
#define mz_stream_os_write mz_stream_win32_write
|
||||
#define mz_stream_os_tell mz_stream_win32_tell
|
||||
#define mz_stream_os_seek mz_stream_win32_seek
|
||||
#define mz_stream_os_close mz_stream_win32_close
|
||||
#define mz_stream_os_error mz_stream_win32_error
|
||||
|
||||
#define mz_stream_os_create mz_stream_win32_create
|
||||
#define mz_stream_os_delete mz_stream_win32_delete
|
||||
|
||||
#define mz_stream_os_get_interface \
|
||||
mz_stream_win32_get_interface
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
-151
@@ -1,151 +0,0 @@
|
||||
/* mz_zip.h -- Zip manipulation
|
||||
Version 2.2.7, January 30th, 2018
|
||||
part of the MiniZip project
|
||||
|
||||
Copyright (C) 2010-2018 Nathan Moinvaziri
|
||||
https://github.com/nmoinvaz/minizip
|
||||
Copyright (C) 2009-2010 Mathias Svensson
|
||||
Modifications for Zip64 support
|
||||
http://result42.com
|
||||
Copyright (C) 1998-2010 Gilles Vollant
|
||||
http://www.winimage.com/zLibDll/minizip.html
|
||||
|
||||
This program is distributed under the terms of the same license as zlib.
|
||||
See the accompanying LICENSE file for the full text of the license.
|
||||
*/
|
||||
|
||||
#ifndef _MZ_ZIP_H
|
||||
#define _MZ_ZIP_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "mz_strm.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
typedef struct mz_zip_file_s
|
||||
{
|
||||
uint16_t version_madeby; // version made by
|
||||
uint16_t version_needed; // version needed to extract
|
||||
uint16_t flag; // general purpose bit flag
|
||||
uint16_t compression_method; // compression method
|
||||
time_t modified_date; // last modified date in unix time
|
||||
time_t accessed_date; // last accessed date in unix time
|
||||
time_t creation_date; // creation date in unix time
|
||||
uint32_t crc; // crc-32
|
||||
uint64_t compressed_size; // compressed size
|
||||
uint64_t uncompressed_size; // uncompressed size
|
||||
uint16_t filename_size; // filename length
|
||||
uint16_t extrafield_size; // extra field length
|
||||
uint16_t comment_size; // file comment length
|
||||
uint32_t disk_number; // disk number start
|
||||
uint64_t disk_offset; // relative offset of local header
|
||||
uint16_t internal_fa; // internal file attributes
|
||||
uint32_t external_fa; // external file attributes
|
||||
|
||||
char *filename; // filename string
|
||||
uint8_t *extrafield; // extrafield data
|
||||
char *comment; // comment string
|
||||
|
||||
#ifdef HAVE_AES
|
||||
uint16_t aes_version; // winzip aes extension if not 0
|
||||
uint8_t aes_encryption_mode; // winzip aes encryption mode
|
||||
#endif
|
||||
} mz_zip_file;
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
extern void * mz_zip_open(void *stream, int32_t mode);
|
||||
// Create a zip file, no delete file in zip functionality
|
||||
|
||||
extern int32_t mz_zip_close(void *handle);
|
||||
// Close the zip file
|
||||
|
||||
extern int32_t mz_zip_get_comment(void *handle, const char **comment);
|
||||
// Get a pointer to the global comment
|
||||
|
||||
extern int32_t mz_zip_set_comment(void *handle, const char *comment);
|
||||
// Set the global comment used for writing zip file
|
||||
|
||||
extern int32_t mz_zip_get_version_madeby(void *handle, uint16_t *version_madeby);
|
||||
// Get the version made by
|
||||
|
||||
extern int32_t mz_zip_set_version_madeby(void *handle, uint16_t version_madeby);
|
||||
// Set the version made by used for writing zip file
|
||||
|
||||
extern int32_t mz_zip_entry_write_open(void *handle, const mz_zip_file *file_info,
|
||||
int16_t compress_level, const char *password);
|
||||
// Open for writing the current file in the zip file
|
||||
|
||||
extern int32_t mz_zip_entry_write(void *handle, const void *buf, uint32_t len);
|
||||
// Write bytes from the current file in the zip file
|
||||
|
||||
extern int32_t mz_zip_entry_read_open(void *handle, int16_t raw, const char *password);
|
||||
// Open for reading the current file in the zip file
|
||||
|
||||
extern int32_t mz_zip_entry_read(void *handle, void *buf, uint32_t len);
|
||||
// Read bytes from the current file in the zip file
|
||||
|
||||
extern int32_t mz_zip_entry_get_info(void *handle, mz_zip_file **file_info);
|
||||
// Get info about the current file, only valid while current entry is open
|
||||
|
||||
extern int32_t mz_zip_entry_get_local_info(void *handle, mz_zip_file **local_file_info);
|
||||
// Get local info about the current file, only valid while current entry is being read
|
||||
|
||||
extern int32_t mz_zip_entry_close_raw(void *handle, uint64_t uncompressed_size, uint32_t crc32);
|
||||
// Close the current file in the zip file where raw is compressed data
|
||||
|
||||
extern int32_t mz_zip_entry_close(void *handle);
|
||||
// Close the current file in the zip file
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
extern int32_t mz_zip_get_number_entry(void *handle, int64_t *number_entry);
|
||||
// Get the total number of entries
|
||||
|
||||
extern int32_t mz_zip_goto_first_entry(void *handle);
|
||||
// Go to the first entry in the zip file
|
||||
|
||||
extern int32_t mz_zip_goto_next_entry(void *handle);
|
||||
// Go to the next entry in the zip file or MZ_END_OF_LIST if reaching the end
|
||||
|
||||
typedef int32_t (*mz_filename_compare_cb)(void *handle, const char *filename1, const char *filename2);
|
||||
extern int32_t mz_zip_locate_entry(void *handle, const char *filename,
|
||||
mz_filename_compare_cb filename_compare_cb);
|
||||
// Locate the file with the specified name in the zip file or MZ_END_LIST if not found
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int32_t mz_zip_dosdate_to_tm(uint64_t dos_date, struct tm *ptm);
|
||||
// Convert dos date/time format to struct tm
|
||||
|
||||
time_t mz_zip_dosdate_to_time_t(uint64_t dos_date);
|
||||
// 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
|
||||
|
||||
uint32_t mz_zip_time_t_to_dos_date(time_t unix_time);
|
||||
// Convert time_t to dos date/time format
|
||||
|
||||
uint32_t mz_zip_tm_to_dosdate(const struct tm *ptm);
|
||||
// Convert struct tm to dos date/time format
|
||||
|
||||
int32_t mz_zip_ntfs_to_unix_time(uint64_t ntfs_time, time_t *unix_time);
|
||||
// Convert ntfs time to unix time
|
||||
|
||||
int32_t mz_zip_unix_to_ntfs_time(time_t unix_time, uint64_t *ntfs_time);
|
||||
// Convert unix time to ntfs time
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _ZIP_H */
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MIIF7TCCA9WgAwIBAgIJALkeEbyIQTYjMA0GCSqGSIb3DQEBBQUAMIGMMQswCQYD
|
||||
VQQGEwJVUzELMAkGA1UECAwCQVoxEDAOBgNVBAcMB1Bob2VuaXgxGjAYBgNVBAoM
|
||||
EUJpbGxpb24gRG9sbGFyIENvMRQwEgYDVQQLDAtFbmdpbmVlcmluZzERMA8GA1UE
|
||||
AwwIYi5kb2xsYXIxGTAXBgkqhkiG9w0BCQEWCmJAYi5kb2xsYXIwHhcNMTgxMDI4
|
||||
MjIyMTE0WhcNNDYwMzE0MjIyMTE0WjCBjDELMAkGA1UEBhMCVVMxCzAJBgNVBAgM
|
||||
AkFaMRAwDgYDVQQHDAdQaG9lbml4MRowGAYDVQQKDBFCaWxsaW9uIERvbGxhciBD
|
||||
bzEUMBIGA1UECwwLRW5naW5lZXJpbmcxETAPBgNVBAMMCGIuZG9sbGFyMRkwFwYJ
|
||||
KoZIhvcNAQkBFgpiQGIuZG9sbGFyMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIIC
|
||||
CgKCAgEA4HFpJ2Szh4lqnCXL1T6q3tppX2vKP7lH02Eapab8Um3puN9ftfvG3si0
|
||||
g+Hs8MK6kMlmG2+cBSCXZX5f6anZOfPUagXdxokhJVhXLrGSo7/N2nBkQGa11pz1
|
||||
i3tdFnsjZPRsyEU6rKJgVIrbw0wD/reFLc6RdfZKF0Xw+3MpJP8jbNXOJPkkjaKT
|
||||
0PSoKgl/Dav7RckOzjGKfdzZrY2NeeSSd3yni5NPHgQRNhRxD7kXCexplkS9vh4e
|
||||
B/eKraOecwewaP+zhC39UlUIwjNezzg+iNeoG7DuuqaRbFAjDj+DVZSgqQdTvTSw
|
||||
FQ0Xly3ZL9oWAIpLsmKDElEZC3HvCzmxkzmhX3Pe/3FjsghAoR/KWPp8gQFq5Ao3
|
||||
5ySWHhArjUZf2nOUgrufCgbd+LIi4DtQ3k/K6zuF7oyqVWjKESlX5KkwAW6xvB/x
|
||||
IukMqyQBts9HRTRUp4LsWz8XxMC6rLcNXa41uhhtgJKFsRjehSfVZPNI3tm1nAna
|
||||
2SS3TnoVORXDTCxbAAPHdK2jxuQVD9RidCfphuT5cFxfUXChF2Mg+c0p7ujtiVr4
|
||||
D5CpeqcCqlZMf4mODVqRlKW1rMsbCBrn8NU39Ad7z0fpzqIoKjS8okLii/7oYepV
|
||||
1ZkWMue1aFKEEReBXk1jd11Iq/B+d1eLnPPPvyTa6DmgkAti9lECAwEAAaNQME4w
|
||||
HQYDVR0OBBYEFM5shIinr8GUAanRZIRCNT2SKge8MB8GA1UdIwQYMBaAFM5shIin
|
||||
r8GUAanRZIRCNT2SKge8MAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEFBQADggIB
|
||||
ALr+1w/2CiHvstlJXyM67bZT0/4eaKaL9Vc3qebROGxvm1TOilQEOnlURlXtfgzq
|
||||
x5ppnzuWlIr1GtbecJo/zr5ZItTrqCwCSbKdMKcexc+pSeNajxXJ6f/BsDrNfFKI
|
||||
zPdqBT12Vy++1XI3goa/7KMX/fgqcxeB6uZkMywwpu6Woewd2r+OPSk0MEWyT1zd
|
||||
Nh0VaHk2jR003n14IAj98NHrT0M9iEzhDkk3nWJfOvvaRVGBoTk+luMZ7FQqcXkD
|
||||
L1d/DivaEyTFShTHpYfyB7w5ydnL/K4XattMHKOgWXvG7ySpTKPnFCJHPfJThIOp
|
||||
lJH3c/fJghdFzZj7JPAzHUDotTopGd5JkwRvyGws8WaaLbpCljUms+cnWUCuEPS/
|
||||
jA0fYvCKtXXeotvPE7eroD5qv74cUHwBA/wsFwC1Nr9Fn7y8xV1hwX/0T4Np0C/+
|
||||
lM9oPpm+FQAJ0YAyCqC5MXepA4lj2r/jyDivpTa4iRtsd1QdtmNGb445IhFiEXOZ
|
||||
EElm+OJVDI8zH//4rJwt3e+/4/l9Y0dHhjMSIfMk9Cq6X+xedDPHgVsgydVlEOZw
|
||||
+aXjXj8nygHXPeOiVvTp2PyNl6l1PV6tWCWzXVgcxhPQ/z3zzaFiBuMEUOIjXH2Q
|
||||
UzGOvx9YcDa1++j7UwImQYxKiDFw1pc/fBZCmJZ2kilg
|
||||
-----END CERTIFICATE-----
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
-----BEGIN RSA PRIVATE KEY-----
|
||||
MIIJKAIBAAKCAgEA4HFpJ2Szh4lqnCXL1T6q3tppX2vKP7lH02Eapab8Um3puN9f
|
||||
tfvG3si0g+Hs8MK6kMlmG2+cBSCXZX5f6anZOfPUagXdxokhJVhXLrGSo7/N2nBk
|
||||
QGa11pz1i3tdFnsjZPRsyEU6rKJgVIrbw0wD/reFLc6RdfZKF0Xw+3MpJP8jbNXO
|
||||
JPkkjaKT0PSoKgl/Dav7RckOzjGKfdzZrY2NeeSSd3yni5NPHgQRNhRxD7kXCexp
|
||||
lkS9vh4eB/eKraOecwewaP+zhC39UlUIwjNezzg+iNeoG7DuuqaRbFAjDj+DVZSg
|
||||
qQdTvTSwFQ0Xly3ZL9oWAIpLsmKDElEZC3HvCzmxkzmhX3Pe/3FjsghAoR/KWPp8
|
||||
gQFq5Ao35ySWHhArjUZf2nOUgrufCgbd+LIi4DtQ3k/K6zuF7oyqVWjKESlX5Kkw
|
||||
AW6xvB/xIukMqyQBts9HRTRUp4LsWz8XxMC6rLcNXa41uhhtgJKFsRjehSfVZPNI
|
||||
3tm1nAna2SS3TnoVORXDTCxbAAPHdK2jxuQVD9RidCfphuT5cFxfUXChF2Mg+c0p
|
||||
7ujtiVr4D5CpeqcCqlZMf4mODVqRlKW1rMsbCBrn8NU39Ad7z0fpzqIoKjS8okLi
|
||||
i/7oYepV1ZkWMue1aFKEEReBXk1jd11Iq/B+d1eLnPPPvyTa6DmgkAti9lECAwEA
|
||||
AQKCAgBVQP8OZaQEMeB7G6hHghxptlEcW9dhSUHNOy9iCQIDVZjYDpFOkSrDaT2Y
|
||||
cvMLqQww/y938Hc1Q2H+vXg1S70l1Ea1VzEcvu4GP34uUGlBJhRNiy+pftpHgSuM
|
||||
JfoAFholuOqxUAqvlKY+ttgvYOqLkK5X7gU256zRFlIpUV8pnHiaWMSLKtAFtOGt
|
||||
F6HhBeZrDQ7PPUYQcKUIliFhWUmKKZaxKHqD/BqrbiQThMRFkqr7q/+XgtkIJGyH
|
||||
3jGhL2t6iksqNya7aLS213ISQm0bDWRDmpdSnWyxHaBTokUOnnWGxoQYcJ4QWrkf
|
||||
5e82D8ZV9OMypVWHlv/urztyK37+8Hqc02FnIvH1wdu/CGB2hP1qwKAoGbc/yLPb
|
||||
pH0aX1vdARZW6RZTnneLLnxPEJsoRFwpcGpeleG3EHk4Mq56PKuca/Yt4cDRt5TC
|
||||
mJwAc9jO3+5scffh29aoEWOMLjqPI8dQVaHQ5gACVe7OFBMUhi+FIO05M9vSxAZ0
|
||||
VIE+S4ZFpOE4kVcIeL+Cfr0AtjI4Sut1EPdB4SYQ23Debuu1QYF684+uhECqCgrd
|
||||
dSyNAWcHVJfTsgISwGueuKfFi+rELvUloUajTiyo2HPwt19i7rS7v+Vfi4arU6SH
|
||||
x7bIjSNyyDA7N0fQo1naPPtbRC/nyZ7PGz5aNyy0I3OKNVCsmQKCAQEA8IL58w3m
|
||||
WfV0NHq2iC5xhpQZ2frQhyI5GL7bmVvhlHLa+15OsN/X87QNoAijNK7XY18+SJVL
|
||||
9OQKu7dveZSvwLXd4yCvYX1INgLtdOzvW3ss2cMJVTKVLhoruAeV+YFAqTOZZ9PX
|
||||
EEovK0HIv4V9TA2B/5XnOdEuvtOypqJxt5vrvyJVqpIoq81wh/PhYZu/hK9wOzPO
|
||||
v+XVG9MRaoWR2PvqNN1t5HSudTPSqzEkjx03DarOCmbMCKjhoZvLfKQRyWP4urp3
|
||||
Rdhzr4GAEpF2mALVVmGiYTLs+ujduKSg6lDdMzO/dTMkOBMblcsbN8geGpLo4+7q
|
||||
y3fp+H7sZEuawwKCAQEA7uWH1bNt3jHEyW4qnlShHVDScw3Tq2JsS8mAHX5oXnOw
|
||||
k33aqYoQruqznTKcYDRUNOx8QX+3LCOD6M3JvN+lnktYU7h3gVHznIf867yWs8Kg
|
||||
/JlPtwYPlATNcS8EdAW4/hdtD2zdQRmLjCaudr85FVkT1eoGPJqVkLJzcfODwemm
|
||||
EvQptqq7lIbTur4nvoqGACXc3Pj8Zzil3rEg3eBo4IT7ThC23PNJ5Db3hdUc9UgF
|
||||
erNrV/mmXI8Yx5udr0q4mV0XUfUJ3BE85csz2eWdZg/ER3X0u9Em668Tw2riGZHf
|
||||
16WtzPjP50JGMhgv4BDdeDgv1b2FasbvsPVnGBQRWwKCAQA1J30+LlA+iXTJNGWq
|
||||
6zjD2W9tMXVsqC+ePHAkEJ5mRIwJCeqyRIZhoeVw/Pe06P9JQv9s4zIKSWvl2MCW
|
||||
SjodiRQk4lBgZ2PSLAt2z/aGbU6YQfaKEcfOjJIk9eg7YEl+G51HyRW6z+pk0QEg
|
||||
8suiR0n3xpV3vbdrDTZqhXREc/1CHMB6FhtO/PRl14x0GgvVs0BGlUiYOJ9Eqv0q
|
||||
YaK+Jc65KEblOrRjyrRAiXfe+UH1j05CFj35m/lh0iDCxy/3JlUOT6+UjCXlH+XC
|
||||
NMvVBKtJkAJnIO4m9dgqupelsYFxk3o42Like/SYwm6dgWjFzroRxfe2SCY5eTOd
|
||||
ZugpAoIBAQDKxNO4lSVp9QA8oF9YKCjgQxt9QH0q+M1Gvm8ZmZNop/V2GtqgEGn8
|
||||
rYVqpeyI06GtkhQdXBPTSQnLbfAPehyO8RC1btswTqjhcaNBS772MPqHrHE2vdQ3
|
||||
JKRjry6uc7UDJksRTqu1d8M0YBiw/bmr3wGxDYPhqKvON0k5rSFmU7T8XXWSb/Pr
|
||||
K01HMeN2HQmD0p9Wh5dk3XYcQGfPK4yZFQZgylsLywwqwmTxmWOM5WaRMPypnWKR
|
||||
KXw04GbExD1PHu6OR9fojFEOEp2eEwC4AiXtKbdiOzI31VCHC4+BeIlv9lZApeeB
|
||||
OIVwO4tXOblhMv+XAdfeO6ByqLiqfU4FAoIBAHQFRCuOgEaNO5qQCV43kq7MguRc
|
||||
/22yPusUkxCTPZr7q0Xt3szV84x9yuV7cztNWnIKuCvqi+9fzCOagIV7lKIsT+Fo
|
||||
ELFbt3feYU224rhZhm6J3G2qQHKZH9WOclLUw5fV/XAtJw2IJRDd7NEMvuZPB7Vc
|
||||
/FdESPPg1j949cfqqkWNxo+p4wX2awRmIX2M1+8+wRVJQY5jvIUbcN1mkz6NDodE
|
||||
ZNHXOHD8vxO94uILkrFB6LE0d1ttXhZj+Q403gpl+9vlWuhksu7M3XaW6+/XkeMf
|
||||
xzOarKBtvpBi2oB6kQ1jJRycFgUasZ0fjCz+OOlvK9hh5VvacB1MEBfKgBE=
|
||||
-----END RSA PRIVATE KEY-----
|
||||
@@ -0,0 +1,34 @@
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MIIF7TCCA9WgAwIBAgIJALkeEbyIQTYjMA0GCSqGSIb3DQEBBQUAMIGMMQswCQYD
|
||||
VQQGEwJVUzELMAkGA1UECAwCQVoxEDAOBgNVBAcMB1Bob2VuaXgxGjAYBgNVBAoM
|
||||
EUJpbGxpb24gRG9sbGFyIENvMRQwEgYDVQQLDAtFbmdpbmVlcmluZzERMA8GA1UE
|
||||
AwwIYi5kb2xsYXIxGTAXBgkqhkiG9w0BCQEWCmJAYi5kb2xsYXIwHhcNMTgxMDI4
|
||||
MjIyMTE0WhcNNDYwMzE0MjIyMTE0WjCBjDELMAkGA1UEBhMCVVMxCzAJBgNVBAgM
|
||||
AkFaMRAwDgYDVQQHDAdQaG9lbml4MRowGAYDVQQKDBFCaWxsaW9uIERvbGxhciBD
|
||||
bzEUMBIGA1UECwwLRW5naW5lZXJpbmcxETAPBgNVBAMMCGIuZG9sbGFyMRkwFwYJ
|
||||
KoZIhvcNAQkBFgpiQGIuZG9sbGFyMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIIC
|
||||
CgKCAgEA4HFpJ2Szh4lqnCXL1T6q3tppX2vKP7lH02Eapab8Um3puN9ftfvG3si0
|
||||
g+Hs8MK6kMlmG2+cBSCXZX5f6anZOfPUagXdxokhJVhXLrGSo7/N2nBkQGa11pz1
|
||||
i3tdFnsjZPRsyEU6rKJgVIrbw0wD/reFLc6RdfZKF0Xw+3MpJP8jbNXOJPkkjaKT
|
||||
0PSoKgl/Dav7RckOzjGKfdzZrY2NeeSSd3yni5NPHgQRNhRxD7kXCexplkS9vh4e
|
||||
B/eKraOecwewaP+zhC39UlUIwjNezzg+iNeoG7DuuqaRbFAjDj+DVZSgqQdTvTSw
|
||||
FQ0Xly3ZL9oWAIpLsmKDElEZC3HvCzmxkzmhX3Pe/3FjsghAoR/KWPp8gQFq5Ao3
|
||||
5ySWHhArjUZf2nOUgrufCgbd+LIi4DtQ3k/K6zuF7oyqVWjKESlX5KkwAW6xvB/x
|
||||
IukMqyQBts9HRTRUp4LsWz8XxMC6rLcNXa41uhhtgJKFsRjehSfVZPNI3tm1nAna
|
||||
2SS3TnoVORXDTCxbAAPHdK2jxuQVD9RidCfphuT5cFxfUXChF2Mg+c0p7ujtiVr4
|
||||
D5CpeqcCqlZMf4mODVqRlKW1rMsbCBrn8NU39Ad7z0fpzqIoKjS8okLii/7oYepV
|
||||
1ZkWMue1aFKEEReBXk1jd11Iq/B+d1eLnPPPvyTa6DmgkAti9lECAwEAAaNQME4w
|
||||
HQYDVR0OBBYEFM5shIinr8GUAanRZIRCNT2SKge8MB8GA1UdIwQYMBaAFM5shIin
|
||||
r8GUAanRZIRCNT2SKge8MAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEFBQADggIB
|
||||
ALr+1w/2CiHvstlJXyM67bZT0/4eaKaL9Vc3qebROGxvm1TOilQEOnlURlXtfgzq
|
||||
x5ppnzuWlIr1GtbecJo/zr5ZItTrqCwCSbKdMKcexc+pSeNajxXJ6f/BsDrNfFKI
|
||||
zPdqBT12Vy++1XI3goa/7KMX/fgqcxeB6uZkMywwpu6Woewd2r+OPSk0MEWyT1zd
|
||||
Nh0VaHk2jR003n14IAj98NHrT0M9iEzhDkk3nWJfOvvaRVGBoTk+luMZ7FQqcXkD
|
||||
L1d/DivaEyTFShTHpYfyB7w5ydnL/K4XattMHKOgWXvG7ySpTKPnFCJHPfJThIOp
|
||||
lJH3c/fJghdFzZj7JPAzHUDotTopGd5JkwRvyGws8WaaLbpCljUms+cnWUCuEPS/
|
||||
jA0fYvCKtXXeotvPE7eroD5qv74cUHwBA/wsFwC1Nr9Fn7y8xV1hwX/0T4Np0C/+
|
||||
lM9oPpm+FQAJ0YAyCqC5MXepA4lj2r/jyDivpTa4iRtsd1QdtmNGb445IhFiEXOZ
|
||||
EElm+OJVDI8zH//4rJwt3e+/4/l9Y0dHhjMSIfMk9Cq6X+xedDPHgVsgydVlEOZw
|
||||
+aXjXj8nygHXPeOiVvTp2PyNl6l1PV6tWCWzXVgcxhPQ/z3zzaFiBuMEUOIjXH2Q
|
||||
UzGOvx9YcDa1++j7UwImQYxKiDFw1pc/fBZCmJZ2kilg
|
||||
-----END CERTIFICATE-----
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user