Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
edfe614184 | ||
|
|
ca5551c49d | ||
|
|
9805ab4117 | ||
|
|
9ee875d2ad | ||
|
|
d484eca50e | ||
|
|
6be7b59732 | ||
|
|
ba49029b54 | ||
|
|
6b220aa5fb | ||
|
|
303e6f365b | ||
|
|
49ee078edd | ||
|
|
a044b7ee04 | ||
|
|
92b0d46d8d | ||
|
|
c5b292e5c8 | ||
|
|
bfe9d6498c | ||
|
|
720d95a461 | ||
|
|
db21e42a05 | ||
|
|
73d3da0c8c | ||
|
|
fac5012bf5 | ||
|
|
33485822c0 | ||
|
|
cd7646ab60 | ||
|
|
2e2f995eda | ||
|
|
e54bf2821b | ||
|
|
840464bc01 | ||
|
|
e282739da2 | ||
|
|
0512fd4771 | ||
|
|
40ab797fed | ||
|
|
38837cbec7 | ||
|
|
d0605392e6 | ||
|
|
aec922930f | ||
|
|
ce5be0f669 | ||
|
|
1fc153481a | ||
|
|
a6ecab5fdc | ||
|
|
554d3ab702 | ||
|
|
226d905220 | ||
|
|
91b2502219 | ||
|
|
288b484caa | ||
|
|
009a37a512 | ||
|
|
84ec941f15 | ||
|
|
cdedc087e7 | ||
|
|
e51dd48a91 | ||
|
|
208a051c36 | ||
|
|
e4968653f4 | ||
|
|
e71e3e3445 | ||
|
|
e7ede97c07 | ||
|
|
2ca7f3950c | ||
|
|
b150432588 | ||
|
|
c78c782f63 | ||
|
|
1ff40343b5 | ||
|
|
c1fd2edbd2 | ||
|
|
2df1ecbce4 | ||
|
|
a893e6f678 | ||
|
|
8ff664da01 | ||
|
|
d931843012 | ||
|
|
b99b3bf3fc | ||
|
|
f2b2dad2c9 | ||
|
|
9df028d532 | ||
|
|
d058fdb3a1 | ||
|
|
6675c44613 | ||
|
|
4ff891a638 | ||
|
|
f9e344888f | ||
|
|
9e9d0299f1 | ||
|
|
5a16351708 | ||
|
|
5cb88b8f09 | ||
|
|
536a250734 | ||
|
|
e8c94b0570 | ||
|
|
9bd8e85399 | ||
|
|
41eaa4c0ab | ||
|
|
025212f6dc | ||
|
|
6295dcf005 | ||
|
|
62f435adbb | ||
|
|
fa0c9c8edc | ||
|
|
7f73943486 | ||
|
|
2ce1fe2732 | ||
|
|
6b64b924af | ||
|
|
2eb2907823 | ||
|
|
b4d88d426b | ||
|
|
ba1db16a28 | ||
|
|
6ac5fe69d0 | ||
|
|
4c642fc296 | ||
|
|
d8f00c57cb | ||
|
|
54b9939845 | ||
|
|
13a51b9389 | ||
|
|
1b6fcaca41 | ||
|
|
cdac37e1fe | ||
|
|
9897259b04 | ||
|
|
e9ae46ba27 | ||
|
|
3e2d07451c | ||
|
|
d31c5de5b0 | ||
|
|
ad82950a53 | ||
|
|
39365dd10d | ||
|
|
76fb8903e1 | ||
|
|
e849627093 | ||
|
|
6f3a0a6ad0 | ||
|
|
b066fd9677 | ||
|
|
b578fa5ba5 | ||
|
|
bacef206da | ||
|
|
d96f23f925 | ||
|
|
a5bc018ecc | ||
|
|
83aa562e32 | ||
|
|
8a35deab70 | ||
|
|
783d258c68 | ||
|
|
b16ab566ef | ||
|
|
ddeeae3388 | ||
|
|
e231cfd9d8 | ||
|
|
f96e8636a4 | ||
|
|
f4207b7a23 | ||
|
|
02c9edb3af | ||
|
|
d4f27376aa | ||
|
|
9475829f15 | ||
|
|
19cdb10201 | ||
|
|
d46559398f | ||
|
|
90d31c791d | ||
|
|
0a60d2ca2a | ||
|
|
0c59898b2d | ||
|
|
53c3d0ae2f | ||
|
|
1ee609b759 | ||
|
|
0ecfd22443 | ||
|
|
7de94cf921 | ||
|
|
c1e07aaeb0 | ||
|
|
329ac651d8 | ||
|
|
4d0897361b | ||
|
|
fd2fb24b4e | ||
|
|
82fa1b39c0 | ||
|
|
2d37e12f36 | ||
|
|
fe9ec41c49 | ||
|
|
a8b23065cd | ||
|
|
08b01ca7b7 | ||
|
|
bc64e9f899 | ||
|
|
abb508eabe | ||
|
|
1139b468c1 | ||
|
|
c4916c971a | ||
|
|
d8b232a6e9 | ||
|
|
fb8a1cc220 | ||
|
|
a2a3858e05 | ||
|
|
ea2bd795a3 | ||
|
|
51b050e0b8 | ||
|
|
09e41030a5 | ||
|
|
7817f5c8d5 | ||
|
|
a4549e0689 | ||
|
|
4d29ba4782 | ||
|
|
4d4c9db5b0 | ||
|
|
a93a2ad678 | ||
|
|
e0b2477114 | ||
|
|
264dc189b3 | ||
|
|
b2b082c67d | ||
|
|
12d03f4e61 | ||
|
|
dd103f4fc1 | ||
|
|
d82579a400 | ||
|
|
98cbf2296a | ||
|
|
9320beab03 | ||
|
|
f47e75303d | ||
|
|
11a44ecf33 | ||
|
|
5498bb6c4f | ||
|
|
278770c5f3 | ||
|
|
e71d8776be | ||
|
|
3c2b4da788 | ||
|
|
c6443ff336 | ||
|
|
a7af82f8cf | ||
|
|
146fa3a663 | ||
|
|
96b401b2c5 | ||
|
|
771856888c | ||
|
|
5502aeaeb2 | ||
|
|
2805bd4688 | ||
|
|
4d6530cd88 | ||
|
|
1f9efeae4f | ||
|
|
0687386d8b | ||
|
|
d5911f4524 | ||
|
|
23b9d094d7 | ||
|
|
258d42c566 | ||
|
|
8ce54b4a8d | ||
|
|
f07057a85e | ||
|
|
e440f1e9d5 | ||
|
|
6fab9297d2 | ||
|
|
1d6d183af6 | ||
|
|
f274ae0649 | ||
|
|
95fec96e05 | ||
|
|
3c7568fa67 | ||
|
|
05a45d61ab | ||
|
|
d4d49df0b1 | ||
|
|
f9b4d20535 | ||
|
|
cf622bb55c | ||
|
|
57b65f8a15 | ||
|
|
effc1428ff | ||
|
|
286a8174f6 | ||
|
|
b08bd9a3fa | ||
|
|
07b0ecadc6 | ||
|
|
0dfcdf91ba | ||
|
|
23342c44d4 | ||
|
|
65dbda889d | ||
|
|
e39032376e | ||
|
|
30a5288bf4 | ||
|
|
4cceb2f6c8 | ||
|
|
2ff079d5d8 | ||
|
|
0d85193f89 | ||
|
|
c000e8f1f8 | ||
|
|
761d28e30d | ||
|
|
6000844769 | ||
|
|
51834cd9f9 | ||
|
|
c47090678d | ||
|
|
79138fe17c | ||
|
|
9e8d7a8c76 | ||
|
|
79244df024 | ||
|
|
87a668fd64 | ||
|
|
a18f49f6d3 | ||
|
|
aacf87304e | ||
|
|
130f0591ba | ||
|
|
c8501a4216 | ||
|
|
a90e29704a | ||
|
|
69e3549034 | ||
|
|
a4fc2c5e85 | ||
|
|
8edafb620a | ||
|
|
b4923cd155 | ||
|
|
71295ce78d | ||
|
|
088b819ec0 | ||
|
|
a8f23236e8 | ||
|
|
b11b6cfe63 | ||
|
|
feb86c551a | ||
|
|
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 | ||
|
|
5c3dd5c27e | ||
|
|
ceca477ed4 | ||
|
|
34eff624c4 | ||
|
|
930f9188a5 | ||
|
|
397d2d5ab3 | ||
|
|
3064a0e551 | ||
|
|
d22a27a9c3 | ||
|
|
98cd93d3a7 | ||
|
|
d52e453488 | ||
|
|
61a4b59216 | ||
|
|
7443318057 | ||
|
|
e474a61e86 | ||
|
|
5e67eb720a | ||
|
|
d18e09d5b0 | ||
|
|
7855c3efd0 | ||
|
|
c689d51f79 | ||
|
|
fee885a931 | ||
|
|
a1e78c5fc4 | ||
|
|
3c52d21ba9 | ||
|
|
a6e3b69a3b | ||
|
|
0fbdb2298e | ||
|
|
7df69ea96e | ||
|
|
fc746532d8 | ||
|
|
d60c6d20af | ||
|
|
55e4fe414e | ||
|
|
5bb372d495 | ||
|
|
a86a4355da | ||
|
|
c9fd8d4df0 | ||
|
|
02123b82d5 | ||
|
|
7140c588c6 | ||
|
|
f60a99bd90 | ||
|
|
50fd547660 | ||
|
|
2fa05a7264 | ||
|
|
1a20b75c46 | ||
|
|
dab094fc31 | ||
|
|
13a12e32ab | ||
|
|
73418b7e05 | ||
|
|
3f7de8e289 | ||
|
|
18a30653b1 | ||
|
|
b73887478a | ||
|
|
51fb0afc82 | ||
|
|
a45fa6b8a0 | ||
|
|
6c6700d2f4 | ||
|
|
0574424b05 | ||
|
|
919d837abf | ||
|
|
12db0fc415 | ||
|
|
e4bd6d3191 | ||
|
|
f9749af452 | ||
|
|
02a516944d | ||
|
|
0d4ae83bd2 | ||
|
|
d15b013734 | ||
|
|
7e49256146 | ||
|
|
97a759a14b | ||
|
|
a3dc63a17f | ||
|
|
be85b3b4cd | ||
|
|
3da621e118 | ||
|
|
211f14074a | ||
|
|
819d0d56f0 | ||
|
|
fa7fadebfa | ||
|
|
a6e7d468e3 | ||
|
|
b8f7cf0fa7 | ||
|
|
886df95632 | ||
|
|
80c2d56436 | ||
|
|
4f6a0e3fc1 | ||
|
|
91a76f61c9 | ||
|
|
0cdc6af8cd | ||
|
|
6186dd6dc6 | ||
|
|
bf26d0e7f7 | ||
|
|
2aa8ed38a2 | ||
|
|
b1b25804fc | ||
|
|
053d00c451 | ||
|
|
b579191d9f | ||
|
|
db8df5bd96 | ||
|
|
507074781d | ||
|
|
62e7e2b0b9 | ||
|
|
f2b096a82e | ||
|
|
ec6d4535ab | ||
|
|
83a886f154 | ||
|
|
8d2ce022c1 | ||
|
|
d42bff7e8f | ||
|
|
80fa033e18 | ||
|
|
bd247ee400 | ||
|
|
76c44d530d | ||
|
|
1d82e28fd6 | ||
|
|
1250e1ea6a |
@@ -1,5 +0,0 @@
|
||||
# Xcode
|
||||
xcuserdata/
|
||||
|
||||
# JetBrains
|
||||
.idea/
|
||||
+104
-17
@@ -1,26 +1,113 @@
|
||||
language: c
|
||||
|
||||
os:
|
||||
- linux
|
||||
stages:
|
||||
- windows
|
||||
- linux
|
||||
- macintosh
|
||||
|
||||
compiler:
|
||||
- clang
|
||||
- gcc
|
||||
|
||||
python:
|
||||
- "2.7"
|
||||
matrix:
|
||||
include:
|
||||
- stage: windows
|
||||
os: windows
|
||||
compiler: clang
|
||||
before_deploy:
|
||||
- cd Release
|
||||
- 7z a -tzip minizip-$TRAVIS_OS_NAME.zip *minizip*
|
||||
deploy: &deploy_base
|
||||
provider: releases
|
||||
api_key:
|
||||
secure: FJaUejGb9fRijDoIwLdaekcYRKCLVwyHJ2qv3kf6oaa5OTG9np73U3GYxImj20Z03GvsL2PIFJCKt3SbZ07prHL9Hf01llvH5RpbT29mepQiYbB9UZQKTh6syeHvYxWTMfw+lxMBs1QOnrYZtRzFeYQgnOSvLG2mudAnjxeBhH3duGnh9/uMW2+1UjNzld49HnR1F+AiVdZVAfKqwcZUsQENQ3svb4oNwMfAZrDMcfb5bO3aNat9EF5RBqOvtSFqrIGX6btsU/mh3acAoP2gcCjxmMcfCNlwaFYo8lcbNi4u0vW4nPUMXYfZCyc5KyIu/h/3Lu5ddVaw0ra51f25iT9e6hCBG9kBjk41Mu6AVKhebSvL2zd9/Q41BcVUbBxmGaDGORAJlCXNjqflVkAgYhRRjW5n//kN5TqObPqrIgTTbvhKTQYADQZ5Gza0zX0lZGlTZ9aT1r+gvSrX2T7DGnvz7zSKpGP4HIixPfaB9x+ZxAjohFhi47MLURtr1UhokvS3wonF5OR6dE/jHXdbS6Wi4GiHqKsWLC9a0iYrpkNZLp37byENCiOnZib/orOSerj2PgQFIGtfwt8osunxf6H1yI9A9VpysSsihmD65RvN3ii8HfTASU/K++VQdBvX7wjt2MP+KA59nKwGcobfSkB6nd7GYJhMgfOrsVfzJ4o=
|
||||
file_glob: true
|
||||
file: minizip-$TRAVIS_OS_NAME.zip
|
||||
skip_cleanup: true
|
||||
on:
|
||||
tags: true
|
||||
- stage: windows
|
||||
os: windows
|
||||
compiler: gcc
|
||||
- stage: linux
|
||||
os: linux
|
||||
compiler: clang
|
||||
before_deploy:
|
||||
- ls *minizip* | tar -czvf minizip-$TRAVIS_OS_NAME.tar.gz -T -
|
||||
deploy:
|
||||
<<: *deploy_base
|
||||
file: minizip-$TRAVIS_OS_NAME.tar.gz
|
||||
- stage: linux
|
||||
os: linux
|
||||
compiler: clang
|
||||
env: TOOL="-DMZ_ZLIB=OFF"
|
||||
- stage: linux
|
||||
os: linux
|
||||
compiler: clang
|
||||
env: TOOL="-DMZ_BZIP=OFF"
|
||||
- stage: linux
|
||||
os: linux
|
||||
compiler: clang
|
||||
env: TOOL="-DMZ_LZMA=OFF"
|
||||
- stage: linux
|
||||
os: linux
|
||||
compiler: clang
|
||||
env: TOOL="-DMZ_WZAES=OFF"
|
||||
- stage: linux
|
||||
os: linux
|
||||
compiler: clang
|
||||
env: TOOL="-DMZ_PKCRYPT=OFF"
|
||||
- stage: linux
|
||||
os: linux
|
||||
compiler: clang
|
||||
env: TOOL="-DMZ_COMPRESS_ONLY=ON"
|
||||
- stage: linux
|
||||
os: linux
|
||||
compiler: clang
|
||||
env: TOOL="-DMZ_DECOMPRESS_ONLY=ON"
|
||||
- stage: linux
|
||||
os: linux
|
||||
compiler: clang
|
||||
env: TOOL="-DMZ_PKCRYPT=OFF -DMZ_WZAES=OFF"
|
||||
- stage: linux
|
||||
os: linux
|
||||
compiler: clang
|
||||
env: TOOL="-DMZ_OPENSSL=ON"
|
||||
- stage: linux
|
||||
os: linux
|
||||
compiler: gcc
|
||||
- stage: macintosh
|
||||
os: osx
|
||||
compiler: clang
|
||||
before_deploy:
|
||||
- ls *minizip* | tar -czvf minizip-$TRAVIS_OS_NAME.tar.gz -T -
|
||||
deploy:
|
||||
<<: *deploy_base
|
||||
file: minizip-$TRAVIS_OS_NAME.tar.gz
|
||||
on:
|
||||
tags: true
|
||||
- stage: macintosh
|
||||
os: osx
|
||||
compiler: gcc
|
||||
- stage: macintosh
|
||||
os: osx
|
||||
compiler: clang
|
||||
env: TOOL="-DMZ_LIBCOMP=ON"
|
||||
- stage: macintosh
|
||||
os: osx
|
||||
compiler: gcc
|
||||
env: TOOL="-DMZ_LIBCOMP=ON"
|
||||
- stage: macintosh
|
||||
os: osx
|
||||
compiler: clang
|
||||
before_install:
|
||||
- brew install openssl
|
||||
env: TOOL="-DMZ_OPENSSL=ON -DOPENSSL_ROOT_DIR=/usr/local/opt/openssl -DOPENSSL_INCLUDE_DIRS=/usr/local/opt/openssl/include -DOPENSSL_CRYPTO_LIBRARY=/usr/local/opt/openssl/lib/libcrypto.dylib -DOPENSSL_SSL_LIBRARY=/usr/local/opt/openssl/lib/libssl.dylib"
|
||||
|
||||
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 -y install libbsd-dev libssl-dev; fi
|
||||
|
||||
before_script:
|
||||
- mkdir build
|
||||
- cd build
|
||||
- cmake .. -DBUILD_TEST=ON
|
||||
- mkdir build
|
||||
- cd build
|
||||
- cmake .. -DMZ_BUILD_TEST=ON -DMZ_BUILD_UNIT_TEST=ON -DCMAKE_BUILD_TYPE=Release $TOOL
|
||||
|
||||
script:
|
||||
- make
|
||||
- cd ../test
|
||||
- python test.py --exe_dir=../build
|
||||
- cmake --build . --config Release
|
||||
- ctest --output-on-failure -C Release
|
||||
|
||||
+632
-233
@@ -1,177 +1,468 @@
|
||||
#***************************************************************************
|
||||
# Copyright: Matthias Schmieder,
|
||||
# E-Mail: schmieder.matthias@gmail.com
|
||||
# Year: 2016
|
||||
# Copyright (C) 2017-2019 Nathan Moinvaziri
|
||||
# https://github.com/nmoinvaz/minizip
|
||||
# Copyright (C) 2016 Matthias Schmieder
|
||||
# schmieder.matthias@gmail.com
|
||||
#***************************************************************************
|
||||
cmake_minimum_required(VERSION 2.8)
|
||||
|
||||
option(USE_CRYPT "Enables building with PKWARE traditional encryption" ON)
|
||||
option(USE_AES "Enables building with AES library" ON)
|
||||
option(USE_BZIP2 "Enables building with BZIP2 library" ON)
|
||||
option(USE_LZMA "Enables building with LZMA library" ON)
|
||||
option(BUILD_TEST "Enables building of executables. Requires ZLIB!" OFF)
|
||||
cmake_minimum_required(VERSION 3.0.2)
|
||||
|
||||
# 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)
|
||||
message(STATUS "Using CMake version ${CMAKE_VERSION}")
|
||||
|
||||
option(MZ_COMPAT "Enables compatibility layer" ON)
|
||||
option(MZ_ZLIB "Enables ZLIB compression" ON)
|
||||
option(MZ_BZIP2 "Enables BZIP2 compression" ON)
|
||||
option(MZ_LZMA "Enables LZMA compression" ON)
|
||||
option(MZ_PKCRYPT "Enables PKWARE traditional encryption" ON)
|
||||
option(MZ_WZAES "Enables WinZIP AES encryption" ON)
|
||||
option(MZ_LIBCOMP "Enables Apple compression" OFF)
|
||||
option(MZ_OPENSSL "Enables OpenSSL for encryption" OFF)
|
||||
option(MZ_BRG "Enables Brian Gladman's encryption library" OFF)
|
||||
option(MZ_COMPRESS_ONLY "Only support compression" OFF)
|
||||
option(MZ_DECOMPRESS_ONLY "Only support decompression" OFF)
|
||||
option(MZ_BUILD_TEST "Builds minizip test executable" OFF)
|
||||
option(MZ_BUILD_UNIT_TEST "Builds minizip unit test project" OFF)
|
||||
option(MZ_BUILD_FUZZ_TEST "Builds minizip fuzzer executables" OFF)
|
||||
|
||||
if(POLICY CMP0074)
|
||||
cmake_policy(SET CMP0074 OLD)
|
||||
endif()
|
||||
# ZLIB_ROOT - Parent directory of zlib installation
|
||||
# BZIP2_ROOT - Parent directory of BZip2 installation
|
||||
# OPENSSL_ROOT - Parent directory of OpenSSL installation
|
||||
|
||||
set(INSTALL_BIN_DIR "bin" CACHE PATH "Installation directory for executables")
|
||||
set(INSTALL_LIB_DIR "lib" CACHE PATH "Installation directory for libraries")
|
||||
set(INSTALL_INC_DIR "include" CACHE PATH "Installation directory for headers")
|
||||
set(INSTALL_MAN_DIR "share/man" CACHE PATH "Installation directory for manual pages")
|
||||
set(INSTALL_PKGCONFIG_DIR "share/pkgconfig" CACHE PATH "Installation directory for pkgconfig (.pc) files")
|
||||
enable_language(C)
|
||||
|
||||
project("minizip")
|
||||
# Minizip library version
|
||||
set(VERSION "2.8.4")
|
||||
|
||||
set(VERSION "2.2.2")
|
||||
# Minizip api version
|
||||
set(SOVERSION "2.5")
|
||||
|
||||
# Set cmake debug postfix to d
|
||||
set(CMAKE_DEBUG_POSTFIX "d")
|
||||
include(CheckLibraryExists)
|
||||
include(CheckSymbolExists)
|
||||
include(CheckFunctionExists)
|
||||
include(CheckIncludeFile)
|
||||
include(CheckTypeSize)
|
||||
include(GNUInstallDirs)
|
||||
include(ExternalProject)
|
||||
|
||||
# Ensure correct version of zlib is referenced
|
||||
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()
|
||||
set(INSTALL_BIN_DIR ${CMAKE_INSTALL_FULL_BINDIR} CACHE PATH "Installation directory for executables")
|
||||
set(INSTALL_LIB_DIR ${CMAKE_INSTALL_FULL_LIBDIR} CACHE PATH "Installation directory for libraries")
|
||||
set(INSTALL_INC_DIR ${CMAKE_INSTALL_FULL_INCLUDEDIR} CACHE PATH "Installation directory for headers")
|
||||
set(INSTALL_MAN_DIR ${CMAKE_INSTALL_FULL_MANDIR} CACHE PATH "Installation directory for manual pages")
|
||||
set(INSTALL_PKGCONFIG_DIR "${CMAKE_INSTALL_FULL_LIBDIR}/pkgconfig" CACHE PATH "Installation directory for pkgconfig (.pc) files")
|
||||
set(INSTALL_CMAKE_DIR "${CMAKE_INSTALL_FULL_LIBDIR}/cmake/minizip" CACHE PATH "Installation directory for cmake files.")
|
||||
|
||||
set(MINIZIP_PC ${CMAKE_CURRENT_BINARY_DIR}/minizip.pc)
|
||||
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/minizip.pc.cmakein ${MINIZIP_PC} @ONLY)
|
||||
|
||||
set(PROJECT_NAME libminizip)
|
||||
|
||||
set(MINIZIP_SRC
|
||||
src/mz_os.c
|
||||
src/mz_compat.c
|
||||
src/mz_strm.c
|
||||
src/mz_strm_buf.c
|
||||
src/mz_strm_mem.c
|
||||
src/mz_strm_posix.c
|
||||
src/mz_strm_split.c
|
||||
src/mz_strm_zlib.c
|
||||
src/mz_zip.c)
|
||||
|
||||
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)
|
||||
|
||||
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")
|
||||
|
||||
add_definitions(-D_CRT_SECURE_NO_DEPRECATE)
|
||||
if(EXISTS ${MINIZIP_PC}.cmakein)
|
||||
configure_file(${MINIZIP_PC}.cmakein ${MINIZIP_PC} @ONLY)
|
||||
endif()
|
||||
|
||||
if("${CMAKE_SYSTEM_NAME}" STREQUAL "WindowsStore")
|
||||
add_definitions(-DMZ_USE_WINRT_API)
|
||||
# Check if zlib installation is present
|
||||
if(MZ_ZLIB AND NOT MZ_LIBCOMP)
|
||||
find_package(ZLIB)
|
||||
if(ZLIB_FOUND)
|
||||
message(STATUS "Using ZLIB ${ZLIB_VERSION_STRING}")
|
||||
include_directories(${ZLIB_INCLUDE_DIRS})
|
||||
else()
|
||||
if (NOT ZLIB_TAG)
|
||||
set(ZLIB_TAG master)
|
||||
endif()
|
||||
if (NOT ZLIB_REPOSITORY)
|
||||
set(ZLIB_REPOSITORY https://github.com/madler/zlib)
|
||||
endif()
|
||||
|
||||
message(STATUS "Using ZLIB ${ZLIB_REPOSITORY} ${ZLIB_TAG}")
|
||||
|
||||
set(ZLIB_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/lib/zlib)
|
||||
set(ZLIB_INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/install/zlib)
|
||||
set(ZLIB_INCLUDE_DIRS ${ZLIB_INSTALL_DIR}/include)
|
||||
|
||||
if(WIN32)
|
||||
set(ZLIB_LIBRARIES
|
||||
${ZLIB_INSTALL_DIR}/lib/zlibstatic$<$<CONFIG:Debug>:d>.lib)
|
||||
else()
|
||||
set(ZLIB_LIBRARIES
|
||||
${ZLIB_INSTALL_DIR}/lib/libz.a)
|
||||
endif()
|
||||
|
||||
# Build args variable based on cache variables
|
||||
unset(CMAKE_ARGS)
|
||||
get_cmake_property(CACHE_VARS CACHE_VARIABLES)
|
||||
foreach(CACHE_VAR ${CACHE_VARS})
|
||||
get_property(CACHE_VAR_HELPSTRING CACHE ${CACHE_VAR} PROPERTY HELPSTRING)
|
||||
if(CACHE_VAR_HELPSTRING STREQUAL "No help, variable specified on the command line.")
|
||||
get_property(CACHE_VAR_TYPE CACHE ${CACHE_VAR} PROPERTY TYPE)
|
||||
if(CACHE_VAR_TYPE STREQUAL "UNINITIALIZED")
|
||||
set(CACHE_VAR_TYPE)
|
||||
else()
|
||||
set(CACHE_VAR_TYPE :${CACHE_VAR_TYPE})
|
||||
endif()
|
||||
if (NOT CACHE_VAR MATCHES "^MZ_")
|
||||
list(APPEND CMAKE_ARGS "-D${CACHE_VAR}${CACHE_VAR_TYPE}=${${CACHE_VAR}}")
|
||||
endif()
|
||||
endif()
|
||||
endforeach()
|
||||
|
||||
ExternalProject_Add(zlib
|
||||
PREFIX zlib
|
||||
GIT_REPOSITORY ${ZLIB_REPOSITORY}
|
||||
GIT_TAG ${ZLIB_TAG}
|
||||
SOURCE_DIR ${ZLIB_SOURCE_DIR}
|
||||
BUILD_IN_SOURCE 1
|
||||
INSTALL_DIR ${ZLIB_INSTALL_DIR}
|
||||
BUILD_BYPRODUCTS ${ZLIB_STATIC_LIBRARIES}
|
||||
UPDATE_COMMAND ""
|
||||
CMAKE_ARGS "${CMAKE_ARGS};-DCMAKE_INSTALL_PREFIX:STRING=${ZLIB_INSTALL_DIR}")
|
||||
|
||||
include_directories(${ZLIB_INCLUDE_DIRS})
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if(UNIX)
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC -O3")
|
||||
# Check if bzip2 installation is present
|
||||
if(MZ_BZIP2)
|
||||
find_package(BZip2)
|
||||
if(BZIP2_FOUND)
|
||||
message(STATUS "Using BZIP2 ${BZIP2_VERSION_STRING}")
|
||||
include_directories(${BZIP2_INCLUDE_DIRS})
|
||||
add_definitions(-DHAVE_BZIP2)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
list(APPEND MINIZIP_SRC "src/mz_os_posix.c")
|
||||
list(APPEND MINIZIP_PUBLIC_HEADERS "src/mz_os_posix.h")
|
||||
# Check to see if openssl installation is present
|
||||
if (MZ_OPENSSL)
|
||||
find_package(OpenSSL)
|
||||
if (OPENSSL_FOUND)
|
||||
message(STATUS "Using OpenSSL ${OPENSSL_VERSION}")
|
||||
include_directories(${OPENSSL_INCLUDE_DIRS})
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# Check for system includes
|
||||
check_include_file(stdint.h HAVE_STDINT_H)
|
||||
check_include_file(inttypes.h HAVE_INTTYPES_H)
|
||||
|
||||
if(HAVE_STDINT_H)
|
||||
add_definitions(-DHAVE_STDINT_H)
|
||||
endif()
|
||||
if(HAVE_INTTYPES_H)
|
||||
add_definitions(-DHAVE_INTTYPES_H)
|
||||
endif()
|
||||
|
||||
# Check for large file support
|
||||
check_type_size(off64_t OFF64_T)
|
||||
if(HAVE_OFF64_T)
|
||||
add_definitions(-D__USE_FILE_OFFSET64)
|
||||
add_definitions(-D__USE_LARGEFILE64)
|
||||
add_definitions(-D_LARGEFILE64_SOURCE)
|
||||
add_definitions(-D_FILE_OFFSET_BIT=64)
|
||||
|
||||
find_package(PkgConfig REQUIRED)
|
||||
|
||||
pkg_check_modules(LIBBSD libbsd)
|
||||
|
||||
include_directories(${LIBBSD_INCLUDE_DIRS})
|
||||
link_directories(${LIBBSD_LIBRARY_DIRS})
|
||||
add_definitions(-D_FILE_OFFSET_BITS=64)
|
||||
endif()
|
||||
# Check for fseeko support
|
||||
check_function_exists(fseeko HAVE_FSEEKO)
|
||||
if(NOT HAVE_FSEEKO)
|
||||
add_definitions(-DNO_FSEEKO)
|
||||
endif()
|
||||
|
||||
if(USE_CRYPT)
|
||||
add_definitions(-DHAVE_CRYPT)
|
||||
# Enable compiler warnings
|
||||
if(CMAKE_C_COMPILER_ID MATCHES "MSVC")
|
||||
add_compile_options(/W3)
|
||||
else()
|
||||
add_compile_options(-W -Wall)
|
||||
endif()
|
||||
# Enable x86 optimizations if supported
|
||||
if(CMAKE_C_COMPILER_ID MATCHES "Clang")
|
||||
include(CheckCCompilerFlag)
|
||||
|
||||
list(APPEND MINIZIP_SRC "src/mz_strm_crypt.c")
|
||||
list(APPEND MINIZIP_PUBLIC_HEADERS "src/mz_strm_crypt.h")
|
||||
macro(enable_option_if_supported option variable)
|
||||
check_c_compiler_flag(
|
||||
"-Werror=unused-command-line-argument ${option}" ${variable})
|
||||
if(${variable})
|
||||
add_compile_options("$<$<CONFIG:Release>:${option}>")
|
||||
endif()
|
||||
endmacro()
|
||||
|
||||
enable_option_if_supported(-msse3 check_opt_sse3)
|
||||
enable_option_if_supported(-msse4.1 check_opt_sse41)
|
||||
enable_option_if_supported(-maes check_opt_aes)
|
||||
endif()
|
||||
# Set compiler options
|
||||
if(CMAKE_C_COMPILER_ID MATCHES "MSVC")
|
||||
add_compile_options(
|
||||
$<$<CONFIG:Debug>:/Zi>
|
||||
$<$<CONFIG:Debug>:/Od>
|
||||
$<$<CONFIG:Release>:/Ox>
|
||||
$<$<CONFIG:Release>:/Os>)
|
||||
else()
|
||||
add_compile_options(
|
||||
$<$<CONFIG:Debug>:-g>
|
||||
$<$<CONFIG:Release>:-Os>)
|
||||
endif()
|
||||
|
||||
if(USE_AES)
|
||||
add_definitions(-DHAVE_AES)
|
||||
# Initial source files
|
||||
set(MINIZIP_SRC
|
||||
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)
|
||||
|
||||
list(APPEND MINIZIP_SRC "src/mz_strm_aes.c")
|
||||
list(APPEND MINIZIP_PUBLIC_HEADERS "src/mz_strm_aes.h")
|
||||
# Initial header files
|
||||
set(MINIZIP_PUBLIC_HEADERS
|
||||
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)
|
||||
|
||||
set(AES_SRC
|
||||
lib/aes/aescrypt.c
|
||||
lib/aes/aeskey.c
|
||||
lib/aes/aestab.c
|
||||
lib/aes/aes_ni.c
|
||||
lib/aes/fileenc.c
|
||||
lib/aes/hmac.c
|
||||
lib/aes/pwd2key.c
|
||||
lib/aes/sha1.c)
|
||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
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/fileenc.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})
|
||||
# Setup predefined macros
|
||||
if(MZ_COMPRESS_ONLY)
|
||||
add_definitions(-DMZ_ZIP_NO_DECOMPRESSION)
|
||||
endif()
|
||||
if(MZ_DECOMPRESS_ONLY)
|
||||
add_definitions(-DMZ_ZIP_NO_COMPRESSION)
|
||||
endif()
|
||||
if(NOT MZ_PKCRYPT AND NOT MZ_WZAES)
|
||||
add_definitions(-DMZ_ZIP_NO_ENCRYPTION)
|
||||
endif()
|
||||
|
||||
if(USE_BZIP2)
|
||||
add_definitions(-DHAVE_BZIP2)
|
||||
add_definitions(-DBZ_NO_STDIO)
|
||||
# Windows specific
|
||||
if(WIN32)
|
||||
set(CMAKE_DEBUG_POSTFIX "d")
|
||||
add_definitions(-D_CRT_SECURE_NO_DEPRECATE)
|
||||
|
||||
list(APPEND MINIZIP_SRC "src/mz_strm_bzip.c")
|
||||
list(APPEND MINIZIP_PUBLIC_HEADERS "src/mz_strm_bzip.h")
|
||||
|
||||
set(BZIP2_SRC
|
||||
lib/bzip2/blocksort.c
|
||||
lib/bzip2/bzlib.c
|
||||
lib/bzip2/compress.c
|
||||
lib/bzip2/crctable.c
|
||||
lib/bzip2/decompress.c
|
||||
lib/bzip2/huffman.c
|
||||
lib/bzip2/randtable.c)
|
||||
|
||||
set(BZIP2_PUBLIC_HEADERS
|
||||
lib/bzip2/bzlib.h
|
||||
lib/bzip2/bzlib_private.h)
|
||||
|
||||
include_directories(lib/bzip2)
|
||||
|
||||
source_group("BZip2" FILES ${BZIP2_SRC} ${BZIP2_PUBLIC_HEADERS})
|
||||
list(APPEND MINIZIP_SRC "mz_os_win32.c" "mz_strm_os_win32.c")
|
||||
if (MZ_PKCRYPT OR MZ_WZAES)
|
||||
if (NOT MZ_OPENSSL AND NOT OPENSSL_FOUND AND NOT MZ_BRG)
|
||||
list(APPEND MINIZIP_SRC "mz_crypt_win32.c")
|
||||
endif()
|
||||
endif()
|
||||
endif()
|
||||
if(CMAKE_SYSTEM_NAME STREQUAL "WindowsStore")
|
||||
add_definitions(-DMZ_WINRT_API)
|
||||
endif()
|
||||
|
||||
if(USE_LZMA)
|
||||
add_definitions(-DHAVE_LZMA)
|
||||
add_definitions(-DHAVE_CONFIG_H)
|
||||
add_definitions(-DLZMA_API_STATIC)
|
||||
# Unix specific
|
||||
if(UNIX)
|
||||
add_definitions(-D_POSIX_C_SOURCE=200112L)
|
||||
list(APPEND MINIZIP_SRC "mz_os_posix.c" "mz_strm_os_posix.c")
|
||||
|
||||
if ((MZ_PKCRYPT OR MZ_WZAES) AND NOT (MZ_OPENSSL AND OPENSSL_FOUND))
|
||||
|
||||
if (APPLE AND NOT MZ_BRG)
|
||||
list(APPEND MINIZIP_SRC "mz_crypt_apple.c")
|
||||
else()
|
||||
set(MZ_BRG TRUE)
|
||||
endif()
|
||||
|
||||
if(UNIX)
|
||||
add_definitions(-DMYTHREAD_POSIX)
|
||||
# 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()
|
||||
|
||||
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()
|
||||
|
||||
list(APPEND MINIZIP_SRC "src/mz_strm_lzma.c")
|
||||
list(APPEND MINIZIP_PUBLIC_HEADERS "src/mz_strm_lzma.h")
|
||||
find_package(Iconv QUIET)
|
||||
if (Iconv_FOUND)
|
||||
message(STATUS "Using Iconv")
|
||||
include_directories(${Iconv_INCLUDE_DIRS})
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# Include compatibility layer
|
||||
if(MZ_COMPAT)
|
||||
list(APPEND MINIZIP_SRC "mz_compat.c")
|
||||
list(APPEND MINIZIP_PUBLIC_HEADERS "mz_compat.h")
|
||||
|
||||
set(COMPAT_HEADER "\
|
||||
/* file.h -- Compatibility layer shim\n\
|
||||
part of the MiniZip project\n\n\
|
||||
This program is distributed under the terms of the same license as zlib.\n\
|
||||
See the accompanying LICENSE file for the full text of the license.\n\
|
||||
*/\n\n\
|
||||
#ifndef MZ_COMPAT_FILE\n\
|
||||
#define MZ_COMPAT_FILE\n\n\
|
||||
#include \"mz_compat.h\"\n\n\
|
||||
#endif\n")
|
||||
|
||||
string(REPLACE "file.h" "zip.h" ZIP_COMPAT_HEADER ${COMPAT_HEADER})
|
||||
string(REPLACE "MZ_COMPAT_FILE" "MZ_COMPAT_ZIP" ZIP_COMPAT_HEADER ${ZIP_COMPAT_HEADER})
|
||||
file(WRITE "zip.h" ${ZIP_COMPAT_HEADER})
|
||||
list(APPEND MINIZIP_PUBLIC_HEADERS "zip.h")
|
||||
|
||||
string(REPLACE "file.h" "unzip.h" UNZIP_COMPAT_HEADER ${COMPAT_HEADER})
|
||||
string(REPLACE "MZ_COMPAT_FILE" "MZ_COMPAT_UNZIP" UNZIP_COMPAT_HEADER ${UNZIP_COMPAT_HEADER})
|
||||
file(WRITE "unzip.h" ${UNZIP_COMPAT_HEADER})
|
||||
list(APPEND MINIZIP_PUBLIC_HEADERS "unzip.h")
|
||||
endif()
|
||||
|
||||
# Include PKCRYPT
|
||||
if(MZ_PKCRYPT)
|
||||
add_definitions(-DHAVE_PKCRYPT)
|
||||
|
||||
list(APPEND MINIZIP_SRC "mz_strm_pkcrypt.c")
|
||||
list(APPEND MINIZIP_PUBLIC_HEADERS "mz_strm_pkcrypt.h")
|
||||
endif()
|
||||
|
||||
# Include WinZIP AES
|
||||
if(MZ_WZAES)
|
||||
add_definitions(-DHAVE_WZAES)
|
||||
|
||||
list(APPEND MINIZIP_SRC "mz_strm_wzaes.c")
|
||||
list(APPEND MINIZIP_PUBLIC_HEADERS "mz_strm_wzaes.h")
|
||||
endif()
|
||||
|
||||
# Include BRG
|
||||
if(MZ_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(MZ_OPENSSL AND OPENSSL_FOUND)
|
||||
list(APPEND MINIZIP_SRC "mz_crypt_openssl.c")
|
||||
endif()
|
||||
|
||||
# Include ZLIB
|
||||
if(MZ_ZLIB)
|
||||
if(MZ_LIBCOMP)
|
||||
add_definitions(-DHAVE_LIBCOMP)
|
||||
|
||||
list(APPEND MINIZIP_SRC "mz_strm_libcomp.c")
|
||||
list(APPEND MINIZIP_PUBLIC_HEADERS "mz_strm_libcomp.h")
|
||||
else()
|
||||
add_definitions(-DHAVE_ZLIB)
|
||||
|
||||
if(ZLIB_COMPAT)
|
||||
add_definitions(-DZLIB_COMPAT)
|
||||
endif()
|
||||
|
||||
list(APPEND MINIZIP_SRC "mz_strm_zlib.c")
|
||||
list(APPEND MINIZIP_PUBLIC_HEADERS "mz_strm_zlib.h")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# Include BZIP2
|
||||
if(MZ_BZIP2)
|
||||
add_definitions(-DHAVE_BZIP2)
|
||||
|
||||
list(APPEND MINIZIP_SRC "mz_strm_bzip.c")
|
||||
list(APPEND MINIZIP_PUBLIC_HEADERS "mz_strm_bzip.h")
|
||||
|
||||
if(NOT BZIP2_FOUND)
|
||||
add_definitions(-DBZ_NO_STDIO)
|
||||
|
||||
set(BZIP2_SRC
|
||||
lib/bzip2/blocksort.c
|
||||
lib/bzip2/bzlib.c
|
||||
lib/bzip2/compress.c
|
||||
lib/bzip2/crctable.c
|
||||
lib/bzip2/decompress.c
|
||||
lib/bzip2/huffman.c
|
||||
lib/bzip2/randtable.c)
|
||||
|
||||
set(BZIP2_PUBLIC_HEADERS
|
||||
lib/bzip2/bzlib.h
|
||||
lib/bzip2/bzlib_private.h)
|
||||
|
||||
include_directories(lib/bzip2)
|
||||
|
||||
source_group("BZip2" FILES ${BZIP2_SRC} ${BZIP2_PUBLIC_HEADERS})
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# Include LZMA
|
||||
if(MZ_LZMA)
|
||||
add_definitions(-DHAVE_LZMA)
|
||||
add_definitions(-DLZMA_API_STATIC)
|
||||
add_definitions(-DHAVE_CONFIG_H)
|
||||
|
||||
check_include_file(limits.h HAVE_LIMITS_H)
|
||||
check_include_file(string.h HAVE_STRING_H)
|
||||
check_include_file(strings.h HAVE_STRINGS_H)
|
||||
check_include_file(memory.h HAVE_MEMORY_H)
|
||||
check_include_file(stdbool.h HAVE_STDBOOL_H)
|
||||
check_include_file(immintrin.h HAVE_IMMINTRIN_H)
|
||||
|
||||
if(HAVE_LIMITS_H)
|
||||
add_definitions(-DHAVE_LIMITS_H)
|
||||
endif()
|
||||
if(HAVE_STRING_H)
|
||||
add_definitions(-DHAVE_STRING_H)
|
||||
endif()
|
||||
if(HAVE_STRINGS_H)
|
||||
add_definitions(-DHAVE_STRINGS_H)
|
||||
endif()
|
||||
if(HAVE_MEMORY_H)
|
||||
add_definitions(-DHAVE_MEMORY_H)
|
||||
endif()
|
||||
if(HAVE_STDBOOL_H)
|
||||
add_definitions(-DHAVE_STDBOOL_H)
|
||||
endif()
|
||||
if(HAVE_IMMINTRIN_H)
|
||||
add_definitions(-DHAVE_IMMINTRIN_H)
|
||||
endif()
|
||||
|
||||
list(APPEND MINIZIP_SRC "mz_strm_lzma.c")
|
||||
list(APPEND MINIZIP_PUBLIC_HEADERS "mz_strm_lzma.h")
|
||||
|
||||
set(LZMA_CHECK_SRC
|
||||
lib/liblzma/check/check.c
|
||||
@@ -180,53 +471,8 @@ if(USE_LZMA)
|
||||
set(LZMA_COMMON_SRC
|
||||
lib/liblzma/common/alone_decoder.c
|
||||
lib/liblzma/common/alone_encoder.c
|
||||
lib/liblzma/common/auto_decoder.c
|
||||
lib/liblzma/common/block_buffer_decoder.c
|
||||
lib/liblzma/common/block_buffer_encoder.c
|
||||
lib/liblzma/common/block_decoder.c
|
||||
lib/liblzma/common/block_encoder.c
|
||||
lib/liblzma/common/block_header_decoder.c
|
||||
lib/liblzma/common/block_header_encoder.c
|
||||
lib/liblzma/common/block_util.c
|
||||
lib/liblzma/common/common.c
|
||||
lib/liblzma/common/easy_buffer_encoder.c
|
||||
lib/liblzma/common/easy_decoder_memusage.c
|
||||
lib/liblzma/common/easy_encoder.c
|
||||
lib/liblzma/common/easy_encoder_memusage.c
|
||||
lib/liblzma/common/easy_preset.c
|
||||
lib/liblzma/common/filter_buffer_decoder.c
|
||||
lib/liblzma/common/filter_buffer_encoder.c
|
||||
lib/liblzma/common/filter_common.c
|
||||
lib/liblzma/common/filter_decoder.c
|
||||
lib/liblzma/common/filter_encoder.c
|
||||
lib/liblzma/common/filter_flags_decoder.c
|
||||
lib/liblzma/common/filter_flags_encoder.c
|
||||
lib/liblzma/common/hardware_cputhreads.c
|
||||
lib/liblzma/common/hardware_physmem.c
|
||||
lib/liblzma/common/index.c
|
||||
lib/liblzma/common/index_decoder.c
|
||||
lib/liblzma/common/index_encoder.c
|
||||
lib/liblzma/common/index_hash.c
|
||||
lib/liblzma/common/outqueue.c
|
||||
lib/liblzma/common/stream_buffer_decoder.c
|
||||
lib/liblzma/common/stream_buffer_encoder.c
|
||||
lib/liblzma/common/stream_decoder.c
|
||||
lib/liblzma/common/stream_encoder.c
|
||||
lib/liblzma/common/stream_encoder_mt.c
|
||||
lib/liblzma/common/stream_flags_common.c
|
||||
lib/liblzma/common/stream_flags_decoder.c
|
||||
lib/liblzma/common/stream_flags_encoder.c
|
||||
lib/liblzma/common/tuklib_cpucores.c
|
||||
lib/liblzma/common/tuklib_exit.c
|
||||
lib/liblzma/common/tuklib_mbstr_fw.c
|
||||
lib/liblzma/common/tuklib_mbstr_width.c
|
||||
lib/liblzma/common/tuklib_open_stdxxx.c
|
||||
lib/liblzma/common/tuklib_physmem.c
|
||||
lib/liblzma/common/tuklib_progname.c
|
||||
lib/liblzma/common/tuklib_progname.h
|
||||
lib/liblzma/common/vli_decoder.c
|
||||
lib/liblzma/common/vli_encoder.c
|
||||
lib/liblzma/common/vli_size.c)
|
||||
lib/liblzma/common/filter_encoder.c)
|
||||
set(LZMA_LZ_SRC
|
||||
lib/liblzma/lz/lz_decoder.c
|
||||
lib/liblzma/lz/lz_encoder.c
|
||||
@@ -247,17 +493,10 @@ if(USE_LZMA)
|
||||
set(LZMA_API_HEADERS
|
||||
lib/liblzma/api/lzma.h
|
||||
lib/liblzma/api/lzma/base.h
|
||||
lib/liblzma/api/lzma/bcj.h
|
||||
lib/liblzma/api/lzma/block.h
|
||||
lib/liblzma/api/lzma/check.h
|
||||
lib/liblzma/api/lzma/container.h
|
||||
lib/liblzma/api/lzma/delta.h
|
||||
lib/liblzma/api/lzma/filter.h
|
||||
lib/liblzma/api/lzma/hardware.h
|
||||
lib/liblzma/api/lzma/index.h
|
||||
lib/liblzma/api/lzma/index_hash.h
|
||||
lib/liblzma/api/lzma/lzma12.h
|
||||
lib/liblzma/api/lzma/stream_flags.h
|
||||
lib/liblzma/api/lzma/version.h
|
||||
lib/liblzma/api/lzma/vli.h)
|
||||
set(LZMA_CHECK_HEADERS
|
||||
@@ -267,31 +506,14 @@ if(USE_LZMA)
|
||||
lib/liblzma/check/crc_macros.h)
|
||||
set(LZMA_COMMON_HEADERS
|
||||
lib/liblzma/common/alone_decoder.h
|
||||
lib/liblzma/common/block_buffer_encoder.h
|
||||
lib/liblzma/common/block_decoder.h
|
||||
lib/liblzma/common/block_encoder.h
|
||||
lib/liblzma/common/common.h
|
||||
lib/liblzma/common/easy_preset.h
|
||||
lib/liblzma/common/filter_common.h
|
||||
lib/liblzma/common/filter_decoder.h
|
||||
lib/liblzma/common/filter_encoder.h
|
||||
lib/liblzma/common/index.h
|
||||
lib/liblzma/common/index_encoder.h
|
||||
lib/liblzma/common/memcmplen.h
|
||||
lib/liblzma/common/mythread.h
|
||||
lib/liblzma/common/outqueue.h
|
||||
lib/liblzma/common/stream_decoder.h
|
||||
lib/liblzma/common/stream_flags_common.h
|
||||
lib/liblzma/common/sysdefs.h
|
||||
lib/liblzma/common/tuklib_common.h
|
||||
lib/liblzma/common/tuklib_config.h
|
||||
lib/liblzma/common/tuklib_cpucores.h
|
||||
lib/liblzma/common/tuklib_exit.h
|
||||
lib/liblzma/common/tuklib_gettext.h
|
||||
lib/liblzma/common/tuklib_integer.h
|
||||
lib/liblzma/common/tuklib_mbstr.h
|
||||
lib/liblzma/common/tuklib_open_stdxxx.h
|
||||
lib/liblzma/common/tuklib_physmem.h)
|
||||
lib/liblzma/common/tuklib_integer.h)
|
||||
set(LZMA_LZ_HEADERS
|
||||
lib/liblzma/lz/lz_decoder.h
|
||||
lib/liblzma/lz/lz_encoder.h
|
||||
@@ -342,47 +564,224 @@ if(USE_LZMA)
|
||||
source_group("LZMA\\RangeCoder" FILES ${LZMA_RANGECODER_SRC} ${LZMA_RANGECODER_HEADERS})
|
||||
endif()
|
||||
|
||||
if(CMAKE_C_COMPILER MATCHES ".*clang")
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -msse3 -msse4.1 -maes")
|
||||
endif()
|
||||
|
||||
# Create minizip library
|
||||
source_group("Minizip" FILES ${MINIZIP_SRC} ${MINIZIP_PUBLIC_HEADERS})
|
||||
|
||||
project(minizip)
|
||||
|
||||
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})
|
||||
|
||||
set_target_properties(${PROJECT_NAME} PROPERTIES LINKER_LANGUAGE C PREFIX "")
|
||||
if(USE_LZMA)
|
||||
set_target_properties(${PROJECT_NAME} PROPERTIES
|
||||
VERSION ${VERSION}
|
||||
SOVERSION ${SOVERSION}
|
||||
LINKER_LANGUAGE C
|
||||
DEFINE_SYMBOL "MZ_EXPORTS"
|
||||
POSITION_INDEPENDENT_CODE 1)
|
||||
|
||||
if(MZ_LZMA)
|
||||
set_target_properties(${PROJECT_NAME} PROPERTIES C_STANDARD 99)
|
||||
endif()
|
||||
target_link_libraries(${PROJECT_NAME} ZLIB::ZLIB)
|
||||
if(UNIX)
|
||||
target_link_libraries(${PROJECT_NAME} ${LIBBSD_LIBRARIES})
|
||||
|
||||
# Link with external libraries
|
||||
if(MZ_ZLIB AND NOT MZ_LIBCOMP)
|
||||
target_link_libraries(${PROJECT_NAME} ${ZLIB_LIBRARIES})
|
||||
if(NOT ZLIB_FOUND)
|
||||
add_dependencies(${PROJECT_NAME} zlib)
|
||||
endif()
|
||||
endif()
|
||||
if(MZ_BZIP2 AND BZIP2_FOUND)
|
||||
target_link_libraries(${PROJECT_NAME} ${BZIP2_LIBRARIES})
|
||||
endif()
|
||||
if(MZ_LIBCOMP)
|
||||
target_link_libraries(${PROJECT_NAME} compression)
|
||||
endif()
|
||||
if(Iconv_FOUND AND NOT Iconv_IS_BUILT_IN)
|
||||
target_link_libraries(${PROJECT_NAME} ${Iconv_LIBRARIES})
|
||||
endif()
|
||||
if(MZ_OPENSSL AND OPENSSL_FOUND)
|
||||
target_link_libraries(${PROJECT_NAME} ${OPENSSL_LIBRARIES})
|
||||
elseif(UNIX)
|
||||
if(APPLE AND NOT MZ_BRG)
|
||||
message(STATUS "Using CoreFoundation Framework")
|
||||
find_library(COREFOUNDATION_LIBRARY CoreFoundation)
|
||||
target_link_libraries(${PROJECT_NAME} ${COREFOUNDATION_LIBRARY})
|
||||
message(STATUS "Using Security Framework")
|
||||
find_library(SECURITY_LIBRARY Security)
|
||||
target_link_libraries(${PROJECT_NAME} ${SECURITY_LIBRARY})
|
||||
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 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()
|
||||
|
||||
install(EXPORT ${PROJECT_NAME}
|
||||
DESTINATION "cmake"
|
||||
NAMESPACE "MINIZIP::")
|
||||
# Build test executable
|
||||
if(MZ_BUILD_TEST)
|
||||
add_executable(minizip_cmd "minizip.c" "test/test.c" "test/test.h")
|
||||
set_target_properties(minizip_cmd PROPERTIES OUTPUT_NAME ${PROJECT_NAME})
|
||||
target_link_libraries(minizip_cmd ${PROJECT_NAME})
|
||||
|
||||
install(FILES ${MINIZIP_PUBLIC_HEADERS} DESTINATION "${INSTALL_INC_DIR}")
|
||||
install(FILES ${MINIZIP_PC} DESTINATION "${INSTALL_PKGCONFIG_DIR}")
|
||||
if(NOT SKIP_INSTALL_BINARIES AND NOT SKIP_INSTALL_ALL)
|
||||
install(TARGETS minizip_cmd RUNTIME DESTINATION "bin")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if(BUILD_TEST)
|
||||
add_executable(minizip "src/minizip.c")
|
||||
target_link_libraries(minizip ${PROJECT_NAME})
|
||||
if(MZ_BUILD_UNIT_TEST)
|
||||
enable_testing()
|
||||
|
||||
install(TARGETS minizip
|
||||
RUNTIME DESTINATION "bin")
|
||||
function(create_compress_tests EXTRA_NAME EXTRA_ARGS)
|
||||
if(MZ_DECOMPRESS_ONLY)
|
||||
return()
|
||||
endif()
|
||||
list(FIND EXTRA_ARGS "-z" ZIPCD_IDX)
|
||||
if(${ZIPCD_IDX} EQUAL -1)
|
||||
set(COMPRESS_METHOD_NAMES "raw")
|
||||
set(COMPRESS_METHOD_ARGS "-0")
|
||||
endif()
|
||||
if (MZ_ZLIB OR MZ_LIBCOMP)
|
||||
list(APPEND COMPRESS_METHOD_NAMES "deflate")
|
||||
list(APPEND COMPRESS_METHOD_ARGS "-9")
|
||||
endif()
|
||||
if (MZ_BZIP2)
|
||||
list(APPEND COMPRESS_METHOD_NAMES "bzip2")
|
||||
list(APPEND COMPRESS_METHOD_ARGS "-b")
|
||||
endif()
|
||||
if (MZ_LZMA)
|
||||
list(APPEND COMPRESS_METHOD_NAMES "lzma")
|
||||
list(APPEND COMPRESS_METHOD_ARGS "-m")
|
||||
endif()
|
||||
list(LENGTH COMPRESS_METHOD_NAMES COMPRESS_METHOD_COUNT)
|
||||
math(EXPR COMPRESS_METHOD_COUNT "${COMPRESS_METHOD_COUNT}-1")
|
||||
foreach(INDEX RANGE ${COMPRESS_METHOD_COUNT})
|
||||
list(GET COMPRESS_METHOD_NAMES ${INDEX} COMPRESS_METHOD_NAME)
|
||||
list(GET COMPRESS_METHOD_ARGS ${INDEX} COMPRESS_METHOD_ARG)
|
||||
add_test(NAME ${COMPRESS_METHOD_NAME}-zip-${EXTRA_NAME}
|
||||
COMMAND minizip_cmd ${COMPRESS_METHOD_ARG} -o ${EXTRA_ARGS}
|
||||
result.zip test.c test.h empty.txt random.bin uniform.bin fuzz
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test)
|
||||
add_test(NAME ${COMPRESS_METHOD_NAME}-list-${EXTRA_NAME}
|
||||
COMMAND minizip_cmd -l ${EXTRA_ARGS} result.zip
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test)
|
||||
if(NOT MZ_COMPRESS_ONLY)
|
||||
add_test(NAME ${COMPRESS_METHOD_NAME}-unzip-${EXTRA_NAME}
|
||||
COMMAND minizip_cmd -x -o ${EXTRA_ARGS} -d out result.zip
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test)
|
||||
endif()
|
||||
add_test(NAME ${COMPRESS_METHOD_NAME}-append-${EXTRA_NAME}
|
||||
COMMAND minizip_cmd ${COMPRESS_METHOD_ARG} -a ${EXTRA_ARGS}
|
||||
result.zip single.txt
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test)
|
||||
if(NOT MZ_COMPRESS_ONLY)
|
||||
add_test(NAME ${COMPRESS_METHOD_NAME}-append-unzip-${EXTRA_NAME}
|
||||
COMMAND minizip_cmd -x -o ${EXTRA_ARGS} -d out result.zip
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test)
|
||||
endif()
|
||||
add_test(NAME ${COMPRESS_METHOD_NAME}-erase-${EXTRA_NAME}
|
||||
COMMAND minizip_cmd -o -e result.zip test.c test.h
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test)
|
||||
if(NOT MZ_COMPRESS_ONLY)
|
||||
add_test(NAME ${COMPRESS_METHOD_NAME}-erase-unzip-${EXTRA_NAME}
|
||||
COMMAND minizip_cmd -x -o ${EXTRA_ARGS} -d out result.zip
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test)
|
||||
endif()
|
||||
endforeach()
|
||||
endfunction()
|
||||
|
||||
# Perform tests against ourself
|
||||
create_compress_tests("generic" "")
|
||||
create_compress_tests("span" "-k;1024")
|
||||
create_compress_tests("zipcd" "-z")
|
||||
if(MZ_PKCRYPT)
|
||||
create_compress_tests("pkcrypt" "-p;test123")
|
||||
endif()
|
||||
if(MZ_WZAES)
|
||||
create_compress_tests("wzaes" "-s;-p;test123")
|
||||
endif()
|
||||
if(MZ_OPENSSL AND OPENSSL_FOUND)
|
||||
create_compress_tests("signed" "-h;ia.p12;-w;test")
|
||||
create_compress_tests("secure" "-z;-h;ia.p12;-w;test")
|
||||
endif()
|
||||
# Perform tests on others
|
||||
if(NOT MZ_COMPRESS_ONLY)
|
||||
if(MZ_ZLIB)
|
||||
add_test(NAME unzip-tiny
|
||||
COMMAND minizip_cmd -x -o ${EXTRA_ARGS} -d out
|
||||
fuzz/unzip_fuzzer_seed_corpus/tiny.zip
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test)
|
||||
endif()
|
||||
if(MZ_BZIP2)
|
||||
add_test(NAME unzip-bzip2
|
||||
COMMAND minizip_cmd -x -o ${EXTRA_ARGS} -d out
|
||||
fuzz/unzip_fuzzer_seed_corpus/bzip2.zip
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test)
|
||||
endif()
|
||||
if(MZ_LZMA)
|
||||
add_test(NAME unzip-lzma
|
||||
COMMAND minizip_cmd -x -o ${EXTRA_ARGS} -d out
|
||||
fuzz/unzip_fuzzer_seed_corpus/lzma.zip
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test)
|
||||
endif()
|
||||
if(MZ_PKCRYPT)
|
||||
add_test(NAME unzip-pkcrypt
|
||||
COMMAND minizip_cmd -x -o ${EXTRA_ARGS} -d out -p test123
|
||||
fuzz/unzip_fuzzer_seed_corpus/encrypted_pkcrypt.zip
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test)
|
||||
endif()
|
||||
if(MZ_WZAES)
|
||||
add_test(NAME unzip-wzaes
|
||||
COMMAND minizip_cmd -x -o ${EXTRA_ARGS} -d out -p test123
|
||||
fuzz/unzip_fuzzer_seed_corpus/encrypted_wzaes.zip
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
endif()
|
||||
|
||||
#Build fuzzer executables
|
||||
if(MZ_BUILD_FUZZ_TEST)
|
||||
if (NOT CMAKE_C_COMPILER_ID STREQUAL "Clang")
|
||||
set(FUZZER_SRC "test/fuzz/standalone.c")
|
||||
endif()
|
||||
|
||||
add_executable(zip_fuzzer "test/fuzz/zip_fuzzer.c" ${FUZZER_SRC})
|
||||
add_executable(unzip_fuzzer "test/fuzz/unzip_fuzzer.c" ${FUZZER_SRC})
|
||||
|
||||
if (CMAKE_C_COMPILER_ID STREQUAL "Clang")
|
||||
enable_language(CXX)
|
||||
target_link_libraries(zip_fuzzer "FuzzingEngine")
|
||||
set_target_properties(zip_fuzzer PROPERTIES LINKER_LANGUAGE CXX)
|
||||
target_link_libraries(unzip_fuzzer "FuzzingEngine")
|
||||
set_target_properties(unzip_fuzzer PROPERTIES LINKER_LANGUAGE CXX)
|
||||
endif()
|
||||
|
||||
target_link_libraries(zip_fuzzer ${PROJECT_NAME})
|
||||
target_link_libraries(unzip_fuzzer ${PROJECT_NAME})
|
||||
|
||||
if(NOT SKIP_INSTALL_BINARIES AND NOT SKIP_INSTALL_ALL)
|
||||
install(TARGETS zip_fuzzer RUNTIME DESTINATION "bin")
|
||||
install(TARGETS unzip_fuzzer RUNTIME DESTINATION "bin")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
Pod::Spec.new do |s|
|
||||
s.name = 'Minizip'
|
||||
s.version = '2.2.2'
|
||||
s.version = '2.8.4'
|
||||
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,165 +1,126 @@
|
||||
# Minizip 2.2.2
|
||||
# minizip 2.8.4
|
||||
|
||||
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)
|
||||
[](https://lgtm.com/projects/g/nmoinvaz/minizip/context:cpp)
|
||||
|
||||
For my older fork of this library checkout the [1.2](https://github.com/nmoinvaz/minizip/tree/1.2) branch.
|
||||
For the original work maintained by Mark Adler checkout the zlib minizip [contrib](https://github.com/madler/zlib/tree/master/contrib/minizip).
|
||||
Maintained by Nathan Moinvaziri.
|
||||
|
||||
## Build
|
||||
## Branches
|
||||
|
||||
To generate the project files for your platform and IDE download and run cmake in the project directory.
|
||||
| Name | State | Version | Description |
|
||||
|:- |:-:|:-:|:-|
|
||||
|[master](https://github.com/nmoinvaz/minizip/tree/master)|Active [](https://travis-ci.org/nmoinvaz/minizip/branches)|2.x|Modern rewrite of 1.2 branch that includes more advanced features, improvements in code maintainability and readability, and the reduction of duplicate code. Compatibility layer provided for older versions.|
|
||||
|[dev](https://github.com/nmoinvaz/minizip/tree/dev)|Active [](https://travis-ci.org/nmoinvaz/minizip/branches)|2.x|Latest development code|
|
||||
|[1.2](https://github.com/nmoinvaz/minizip/tree/1.2)|Stale| 1.x|Drop-in replacement for zlib's minizip that includes WinZip AES encryption, disk splitting, I/O buffering and some additional fixes.|
|
||||
|[1.1](https://github.com/nmoinvaz/minizip/tree/1.1)|Stale| 1.x|Original minizip as of zlib 1.2.11.|
|
||||
|
||||
```
|
||||
cmake .
|
||||
cmake --build .
|
||||
```
|
||||
## History
|
||||
|
||||
## Contents
|
||||
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.
|
||||
|
||||
| File(s) | Description | Required |
|
||||
|:- |:-|:-:|
|
||||
| minizip.c | Sample application | No |
|
||||
| mz_compat.\* | Minizip 1.0 compatibility layer | No |
|
||||
| mz.h | Error codes and flags | Yes |
|
||||
| mz_os\* | OS specific helper functions | Encryption |
|
||||
| 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_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 |
|
||||
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
|
||||
|
||||
### Compression Methods
|
||||
+ Creating and extracting zip archives.
|
||||
+ Adding and removing entries from zip archives.
|
||||
+ Read and write raw zip entry data.
|
||||
+ Reading and writing zip archives from memory.
|
||||
+ Zlib, BZIP2, and LZMA compression methods.
|
||||
+ Password protection through Traditional PKWARE and [WinZIP AES](https://www.winzip.com/aes_info.htm) encryption.
|
||||
+ Buffered streaming for improved I/O performance.
|
||||
+ NTFS timestamp support for UTC last modified, last accessed, and creation dates.
|
||||
+ Disk split support for splitting zip archives into multiple files.
|
||||
+ Preservation of file attributes across file systems.
|
||||
+ Unicode filename support through UTF-8 encoding.
|
||||
+ Legacy character encoding support CP437, CP932, CP936, CP950.
|
||||
+ Turn off compilation of compression, decompression, or encryption.
|
||||
+ Windows (Win32 & WinRT), macOS and Linux platform support.
|
||||
+ Streaming interface for easy implementation of additional platforms.
|
||||
+ Support for Apple's compression library ZLIB implementation.
|
||||
+ Zero out local file header information.
|
||||
+ Zip/unzip of central directory to reduce size.
|
||||
+ Ability to generate and verify CMS signature for each entry.
|
||||
+ Recover the central directory if it is corrupt or missing.
|
||||
+ Example minizip command line tool.
|
||||
|
||||
#### BZIP2
|
||||
## Build
|
||||
|
||||
+ Requires ``cmake . -DUSE_BZIP2=ON`` or ``#define HAVE_BZIP2``
|
||||
+ Requires [BZIP2](http://www.bzip.org/) library
|
||||
To generate project files for your platform:
|
||||
|
||||
#### LZMA
|
||||
|
||||
+ 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](https://github.com/BrianGladman/aes) AES library
|
||||
|
||||
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:
|
||||
1. [Download and install](https://cmake.org/install/) cmake.
|
||||
2. Run cmake in the minizip directory.
|
||||
|
||||
```
|
||||
cmake . -DUSE_AES=OFF
|
||||
cmake . -DUSE_CRYPT=OFF
|
||||
cmake . -DMZ_BUILD_TEST=ON
|
||||
cmake --build .
|
||||
```
|
||||
|
||||
### NTFS Timestamps
|
||||
## Build Options
|
||||
|
||||
Support has been added for UTC last modified, last accessed, and creation dates.
|
||||
| Name | Description | Default Value |
|
||||
|:- |:-|:-:|
|
||||
| MZ_COMPAT | Enables compatibility layer | ON |
|
||||
| MZ_ZLIB | Enables ZLIB compression | ON |
|
||||
| MZ_BZIP2 | Enables BZIP2 compression | ON |
|
||||
| MZ_LZMA | Enables LZMA compression | ON |
|
||||
| MZ_PKCRYPT | Enables PKWARE traditional encryption | ON |
|
||||
| MZ_WZAES | Enables WinZIP AES encryption | ON |
|
||||
| MZ_LIBCOMP | Enables Apple compression | OFF |
|
||||
| MZ_OPENSSL | Enables OpenSSL encryption | OFF |
|
||||
| MZ_BRG | Enables Brian Gladman's library | OFF |
|
||||
| MZ_COMPRESS_ONLY | Only support compression | OFF |
|
||||
| MZ_DECOMPRESS_ONLY | Only support decompression | OFF |
|
||||
| MZ_BUILD_TEST | Builds minizip test executable | OFF |
|
||||
| MZ_BUILD_UNIT_TEST | Builds minizip unit test project | OFF |
|
||||
| MZ_BUILD_FUZZ_TEST | Builds minizip fuzz executables | OFF |
|
||||
|
||||
### Streams
|
||||
## Contents
|
||||
|
||||
This library has been refactored around streams.
|
||||
| File(s) | Description |
|
||||
|:- |:-|
|
||||
| minizip.c | Sample application |
|
||||
| mz_compat.\* | Minizip 1.x compatibility layer |
|
||||
| mz.h | Error codes and flags |
|
||||
| mz_os\* | Platform specific file/utility functions |
|
||||
| mz_crypt\* | Configuration specific crypto/hashing functions |
|
||||
| mz_strm.\* | Stream interface |
|
||||
| mz_strm_buf.\* | Buffered stream |
|
||||
| mz_strm_bzip.\* | BZIP2 stream using libbzip2 |
|
||||
| mz_strm_libcomp.\* | Apple compression stream |
|
||||
| mz_strm_lzma.\* | LZMA stream using liblzma |
|
||||
| mz_strm_mem.\* | Memory stream |
|
||||
| mz_strm_split.\* | Disk splitting stream |
|
||||
| mz_strm_pkcrypt.\* | PKWARE traditional encryption stream |
|
||||
| mz_strm_os\* | Platform specific file stream |
|
||||
| mz_strm_wzaes.\* | WinZIP AES stream |
|
||||
| mz_strm_zlib.\* | Deflate stream using zlib |
|
||||
| mz_zip.\* | Zip format |
|
||||
| mz_zip_rw.\* | Zip reader/writer |
|
||||
|
||||
#### Memory Streaming
|
||||
## Third-Party Libraries
|
||||
|
||||
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;
|
||||
+ [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.
|
||||
|
||||
// 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_STREAM_MODE_READ);
|
||||
## Acknowledgments
|
||||
|
||||
void *zip_handle = mz_zip_open(mem_stream, MZ_STREAM_MODE_READ);
|
||||
// do unzip operations
|
||||
Thanks to [Gilles Vollant](https://www.winimage.com/zLibDll/minizip.html) on which this work is originally based on.
|
||||
|
||||
mz_stream_mem_delete(&mem_stream);
|
||||
```
|
||||
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.
|
||||
|
||||
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);
|
||||
```
|
||||
#### 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,145 +0,0 @@
|
||||
/*
|
||||
---------------------------------------------------------------------------
|
||||
Copyright (c) 2002, Dr Brian Gladman < >, Worcester, UK.
|
||||
All rights reserved.
|
||||
|
||||
LICENSE TERMS
|
||||
|
||||
The free distribution and use of this software in both source and binary
|
||||
form is allowed (with or without changes) provided that:
|
||||
|
||||
1. distributions of this source code include the above copyright
|
||||
notice, this list of conditions and the following disclaimer;
|
||||
|
||||
2. distributions in binary form include the above copyright
|
||||
notice, this list of conditions and the following disclaimer
|
||||
in the documentation and/or other associated materials;
|
||||
|
||||
3. the copyright holder's name is not used to endorse products
|
||||
built using this software without specific written permission.
|
||||
|
||||
ALTERNATIVELY, provided that this notice is retained in full, this product
|
||||
may be distributed under the terms of the GNU General Public License (GPL),
|
||||
in which case the provisions of the GPL apply INSTEAD OF those given above.
|
||||
|
||||
DISCLAIMER
|
||||
|
||||
This software is provided 'as is' with no explicit or implied warranties
|
||||
in respect of its properties, including, but not limited to, correctness
|
||||
and/or fitness for purpose.
|
||||
-------------------------------------------------------------------------
|
||||
Issue Date: 24/01/2003
|
||||
|
||||
This file implements password based file encryption and authentication
|
||||
using AES in CTR mode, HMAC-SHA1 authentication and RFC2898 password
|
||||
based key derivation.
|
||||
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "fileenc.h"
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
/* subroutine for data encryption/decryption */
|
||||
/* this could be speeded up a lot by aligning */
|
||||
/* buffers and using 32 bit operations */
|
||||
|
||||
static void encr_data(unsigned char data[], unsigned long d_len, fcrypt_ctx cx[1])
|
||||
{
|
||||
unsigned int i = 0, pos = cx->encr_pos;
|
||||
|
||||
while (i < d_len)
|
||||
{
|
||||
if (pos == AES_BLOCK_SIZE)
|
||||
{
|
||||
unsigned int j = 0;
|
||||
/* increment encryption nonce */
|
||||
while (j < 8 && !++cx->nonce[j])
|
||||
++j;
|
||||
/* encrypt the nonce to form next xor buffer */
|
||||
aes_encrypt(cx->nonce, cx->encr_bfr, cx->encr_ctx);
|
||||
pos = 0;
|
||||
}
|
||||
|
||||
data[i++] ^= cx->encr_bfr[pos++];
|
||||
}
|
||||
|
||||
cx->encr_pos = pos;
|
||||
}
|
||||
|
||||
int fcrypt_init(
|
||||
int mode, /* the mode to be used (input) */
|
||||
const unsigned char pwd[], /* the user specified password (input) */
|
||||
unsigned int pwd_len, /* the length of the password (input) */
|
||||
const unsigned char salt[], /* the salt (input) */
|
||||
#ifdef PASSWORD_VERIFIER
|
||||
unsigned char pwd_ver[PWD_VER_LENGTH], /* 2 byte password verifier (output) */
|
||||
#endif
|
||||
fcrypt_ctx cx[1]) /* the file encryption context (output) */
|
||||
{ unsigned char kbuf[2 * MAX_KEY_LENGTH + PWD_VER_LENGTH];
|
||||
|
||||
if (pwd_len > MAX_PWD_LENGTH)
|
||||
return PASSWORD_TOO_LONG;
|
||||
|
||||
if (mode < 1 || mode > 3)
|
||||
return BAD_MODE;
|
||||
|
||||
cx->mode = mode;
|
||||
cx->pwd_len = pwd_len;
|
||||
|
||||
/* derive the encryption and authentication keys and the password verifier */
|
||||
derive_key(pwd, pwd_len, salt, SALT_LENGTH(mode), KEYING_ITERATIONS,
|
||||
kbuf, 2 * KEY_LENGTH(mode) + PWD_VER_LENGTH);
|
||||
|
||||
/* initialise the encryption nonce and buffer pos */
|
||||
cx->encr_pos = AES_BLOCK_SIZE;
|
||||
/* if we need a random component in the encryption */
|
||||
/* nonce, this is where it would have to be set */
|
||||
memset(cx->nonce, 0, AES_BLOCK_SIZE * sizeof(unsigned char));
|
||||
|
||||
/* initialise for encryption using key 1 */
|
||||
aes_encrypt_key(kbuf, KEY_LENGTH(mode), cx->encr_ctx);
|
||||
|
||||
/* initialise for authentication using key 2 */
|
||||
hmac_sha_begin(HMAC_SHA1, cx->auth_ctx);
|
||||
hmac_sha_key(kbuf + KEY_LENGTH(mode), KEY_LENGTH(mode), cx->auth_ctx);
|
||||
|
||||
#ifdef PASSWORD_VERIFIER
|
||||
memcpy(pwd_ver, kbuf + 2 * KEY_LENGTH(mode), PWD_VER_LENGTH);
|
||||
#endif
|
||||
|
||||
return GOOD_RETURN;
|
||||
}
|
||||
|
||||
/* perform 'in place' encryption and authentication */
|
||||
|
||||
void fcrypt_encrypt(unsigned char data[], unsigned int data_len, fcrypt_ctx cx[1])
|
||||
{
|
||||
encr_data(data, data_len, cx);
|
||||
hmac_sha_data(data, data_len, cx->auth_ctx);
|
||||
}
|
||||
|
||||
/* perform 'in place' authentication and decryption */
|
||||
|
||||
void fcrypt_decrypt(unsigned char data[], unsigned int data_len, fcrypt_ctx cx[1])
|
||||
{
|
||||
hmac_sha_data(data, data_len, cx->auth_ctx);
|
||||
encr_data(data, data_len, cx);
|
||||
}
|
||||
|
||||
/* close encryption/decryption and return the MAC value */
|
||||
|
||||
int fcrypt_end(unsigned char mac[], fcrypt_ctx cx[1])
|
||||
{
|
||||
hmac_sha_end(mac, MAC_LENGTH(cx->mode), cx->auth_ctx);
|
||||
return MAC_LENGTH(cx->mode); /* return MAC length in bytes */
|
||||
}
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif
|
||||
@@ -1,121 +0,0 @@
|
||||
/*
|
||||
---------------------------------------------------------------------------
|
||||
Copyright (c) 2002, Dr Brian Gladman < >, Worcester, UK.
|
||||
All rights reserved.
|
||||
|
||||
LICENSE TERMS
|
||||
|
||||
The free distribution and use of this software in both source and binary
|
||||
form is allowed (with or without changes) provided that:
|
||||
|
||||
1. distributions of this source code include the above copyright
|
||||
notice, this list of conditions and the following disclaimer;
|
||||
|
||||
2. distributions in binary form include the above copyright
|
||||
notice, this list of conditions and the following disclaimer
|
||||
in the documentation and/or other associated materials;
|
||||
|
||||
3. the copyright holder's name is not used to endorse products
|
||||
built using this software without specific written permission.
|
||||
|
||||
ALTERNATIVELY, provided that this notice is retained in full, this product
|
||||
may be distributed under the terms of the GNU General Public License (GPL),
|
||||
in which case the provisions of the GPL apply INSTEAD OF those given above.
|
||||
|
||||
DISCLAIMER
|
||||
|
||||
This software is provided 'as is' with no explicit or implied warranties
|
||||
in respect of its properties, including, but not limited to, correctness
|
||||
and/or fitness for purpose.
|
||||
---------------------------------------------------------------------------
|
||||
Issue Date: 24/01/2003
|
||||
|
||||
This file contains the header file for fileenc.c, which implements password
|
||||
based file encryption and authentication using AES in CTR mode, HMAC-SHA1
|
||||
authentication and RFC2898 password based key derivation.
|
||||
*/
|
||||
|
||||
#ifndef _FENC_H
|
||||
#define _FENC_H
|
||||
|
||||
#include "aes.h"
|
||||
#include "hmac.h"
|
||||
#include "pwd2key.h"
|
||||
|
||||
#define PASSWORD_VERIFIER
|
||||
|
||||
#define MAX_KEY_LENGTH 32
|
||||
#define MAX_PWD_LENGTH 128
|
||||
#define MAX_SALT_LENGTH 16
|
||||
#define KEYING_ITERATIONS 1000
|
||||
|
||||
#ifdef PASSWORD_VERIFIER
|
||||
#define PWD_VER_LENGTH 2
|
||||
#else
|
||||
#define PWD_VER_LENGTH 0
|
||||
#endif
|
||||
|
||||
#define GOOD_RETURN 0
|
||||
#define PASSWORD_TOO_LONG -100
|
||||
#define BAD_MODE -101
|
||||
|
||||
/*
|
||||
Field lengths (in bytes) versus File Encryption Mode (0 < mode < 4)
|
||||
|
||||
Mode Key Salt MAC Overhead
|
||||
1 16 8 10 18
|
||||
2 24 12 10 22
|
||||
3 32 16 10 26
|
||||
|
||||
The following macros assume that the mode value is correct.
|
||||
*/
|
||||
|
||||
#define KEY_LENGTH(mode) (8 * (mode & 3) + 8)
|
||||
#define SALT_LENGTH(mode) (4 * (mode & 3) + 4)
|
||||
#define MAC_LENGTH(mode) (10)
|
||||
|
||||
/* the context for file encryption */
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
typedef struct
|
||||
{ unsigned char nonce[AES_BLOCK_SIZE]; /* the CTR nonce */
|
||||
unsigned char encr_bfr[AES_BLOCK_SIZE]; /* encrypt buffer */
|
||||
aes_encrypt_ctx encr_ctx[1]; /* encryption context */
|
||||
hmac_ctx auth_ctx[1]; /* authentication context */
|
||||
unsigned int encr_pos; /* block position (enc) */
|
||||
unsigned int pwd_len; /* password length */
|
||||
unsigned int mode; /* File encryption mode */
|
||||
} fcrypt_ctx;
|
||||
|
||||
/* initialise file encryption or decryption */
|
||||
|
||||
int fcrypt_init(
|
||||
int mode, /* the mode to be used (input) */
|
||||
const unsigned char pwd[], /* the user specified password (input) */
|
||||
unsigned int pwd_len, /* the length of the password (input) */
|
||||
const unsigned char salt[], /* the salt (input) */
|
||||
#ifdef PASSWORD_VERIFIER
|
||||
unsigned char pwd_ver[PWD_VER_LENGTH], /* 2 byte password verifier (output) */
|
||||
#endif
|
||||
fcrypt_ctx cx[1]); /* the file encryption context (output) */
|
||||
|
||||
/* perform 'in place' encryption or decryption and authentication */
|
||||
|
||||
void fcrypt_encrypt(unsigned char data[], unsigned int data_len, fcrypt_ctx cx[1]);
|
||||
void fcrypt_decrypt(unsigned char data[], unsigned int data_len, fcrypt_ctx cx[1]);
|
||||
|
||||
/* close encryption/decryption and return the MAC value */
|
||||
/* the return value is the length of the MAC */
|
||||
|
||||
int fcrypt_end(unsigned char mac[], /* the MAC value (output) */
|
||||
fcrypt_ctx cx[1]); /* the context (input) */
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,181 +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"
|
||||
|
||||
#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
|
||||
@@ -393,8 +393,10 @@ AES_RETURN aes_xi(decrypt_key192)(const unsigned char *key, aes_decrypt_ctx cx[1
|
||||
cx->ks[v(48,(3))] = ss[3] = word_in(key, 3);
|
||||
|
||||
#ifdef DEC_KS_UNROLL
|
||||
cx->ks[v(48,(4))] = ff(ss[4] = word_in(key, 4));
|
||||
cx->ks[v(48,(5))] = ff(ss[5] = word_in(key, 5));
|
||||
ss[4] = word_in(key, 4);
|
||||
cx->ks[v(48,(4))] = ff(ss[4]);
|
||||
ss[5] = word_in(key, 5);
|
||||
cx->ks[v(48,(5))] = ff(ss[5]);
|
||||
kdf6(cx->ks, 0); kd6(cx->ks, 1);
|
||||
kd6(cx->ks, 2); kd6(cx->ks, 3);
|
||||
kd6(cx->ks, 4); kd6(cx->ks, 5);
|
||||
@@ -485,10 +487,14 @@ AES_RETURN aes_xi(decrypt_key256)(const unsigned char *key, aes_decrypt_ctx cx[1
|
||||
cx->ks[v(56,(3))] = ss[3] = word_in(key, 3);
|
||||
|
||||
#ifdef DEC_KS_UNROLL
|
||||
cx->ks[v(56,(4))] = ff(ss[4] = word_in(key, 4));
|
||||
cx->ks[v(56,(5))] = ff(ss[5] = word_in(key, 5));
|
||||
cx->ks[v(56,(6))] = ff(ss[6] = word_in(key, 6));
|
||||
cx->ks[v(56,(7))] = ff(ss[7] = word_in(key, 7));
|
||||
ss[4] = word_in(key, 4);
|
||||
cx->ks[v(56,(4))] = ff(ss[4]);
|
||||
ss[5] = word_in(key, 5);
|
||||
cx->ks[v(56,(5))] = ff(ss[5]);
|
||||
ss[6] = word_in(key, 6);
|
||||
cx->ks[v(56,(6))] = ff(ss[6]);
|
||||
ss[7] = word_in(key, 7);
|
||||
cx->ks[v(56,(7))] = ff(ss[7]);
|
||||
kdf8(cx->ks, 0); kd8(cx->ks, 1);
|
||||
kd8(cx->ks, 2); kd8(cx->ks, 3);
|
||||
kd8(cx->ks, 4); kd8(cx->ks, 5);
|
||||
@@ -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
|
||||
@@ -102,6 +102,7 @@ static
|
||||
void* default_bzalloc ( void* opaque, Int32 items, Int32 size )
|
||||
{
|
||||
void* v = malloc ( items * size );
|
||||
(void)opaque;
|
||||
return v;
|
||||
}
|
||||
|
||||
@@ -109,6 +110,7 @@ static
|
||||
void default_bzfree ( void* opaque, void* addr )
|
||||
{
|
||||
if (addr != NULL) free ( addr );
|
||||
(void)opaque;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -158,6 +158,7 @@ void generateMTFValues ( EState* s )
|
||||
|
||||
wr = 0;
|
||||
zPend = 0;
|
||||
yy[0] = 0;
|
||||
for (i = 0; i < s->nInUse; i++) yy[i] = (UChar) i;
|
||||
|
||||
for (i = 0; i < s->nblock; i++) {
|
||||
@@ -240,7 +241,7 @@ void sendMTFValues ( EState* s )
|
||||
{
|
||||
Int32 v, t, i, j, gs, ge, totc, bt, bc, iter;
|
||||
Int32 nSelectors, alphaSize, minLen, maxLen, selCtr;
|
||||
Int32 nGroups, nBytes;
|
||||
Int32 nGroups;
|
||||
|
||||
/*--
|
||||
UChar len [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
|
||||
@@ -500,7 +501,6 @@ void sendMTFValues ( EState* s )
|
||||
if (s->inUse[i * 16 + j]) inUse16[i] = True;
|
||||
}
|
||||
|
||||
nBytes = s->numZ;
|
||||
for (i = 0; i < 16; i++)
|
||||
if (inUse16[i]) bsW(s,1,1); else bsW(s,1,0);
|
||||
|
||||
@@ -515,7 +515,6 @@ void sendMTFValues ( EState* s )
|
||||
}
|
||||
|
||||
/*--- Now the selectors. ---*/
|
||||
nBytes = s->numZ;
|
||||
bsW ( s, 3, nGroups );
|
||||
bsW ( s, 15, nSelectors );
|
||||
for (i = 0; i < nSelectors; i++) {
|
||||
@@ -526,8 +525,6 @@ void sendMTFValues ( EState* s )
|
||||
VPrintf1( "selectors %d, ", s->numZ-nBytes );
|
||||
|
||||
/*--- Now the coding tables. ---*/
|
||||
nBytes = s->numZ;
|
||||
|
||||
for (t = 0; t < nGroups; t++) {
|
||||
Int32 curr = s->len[t][0];
|
||||
bsW ( s, 5, curr );
|
||||
@@ -542,7 +539,6 @@ void sendMTFValues ( EState* s )
|
||||
VPrintf1 ( "code lengths %d, ", s->numZ-nBytes );
|
||||
|
||||
/*--- And finally, the block data proper ---*/
|
||||
nBytes = s->numZ;
|
||||
selCtr = 0;
|
||||
gs = 0;
|
||||
while (True) {
|
||||
|
||||
@@ -287,7 +287,7 @@ Int32 BZ2_decompress ( DState* s )
|
||||
GET_BITS(BZ_X_SELECTOR_1, nGroups, 3);
|
||||
if (nGroups < 2 || nGroups > 6) RETURN(BZ_DATA_ERROR);
|
||||
GET_BITS(BZ_X_SELECTOR_2, nSelectors, 15);
|
||||
if (nSelectors < 1) RETURN(BZ_DATA_ERROR);
|
||||
if (nSelectors < 1 || nSelectors > BZ_MAX_SELECTORS) RETURN(BZ_DATA_ERROR);
|
||||
for (i = 0; i < nSelectors; i++) {
|
||||
j = 0;
|
||||
while (True) {
|
||||
|
||||
+3
-1
@@ -115,7 +115,9 @@ void BZ2_hbMakeCodeLengths ( UChar *len,
|
||||
for (i = 1; i <= alphaSize; i++) {
|
||||
j = 0;
|
||||
k = i;
|
||||
while (parent[k] >= 0) { k = parent[k]; j++; }
|
||||
while (k < (BZ_MAX_ALPHA_SIZE * 2) && parent[k] >= 0) {
|
||||
k = parent[k]; j++;
|
||||
}
|
||||
len[i-1] = j;
|
||||
if (j > maxLen) tooLong = True;
|
||||
}
|
||||
|
||||
+12
-8
@@ -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
|
||||
@@ -292,21 +296,21 @@ extern "C" {
|
||||
|
||||
/* Filters */
|
||||
#include "lzma/filter.h"
|
||||
#include "lzma/bcj.h"
|
||||
#include "lzma/delta.h"
|
||||
//#include "lzma/bcj.h"
|
||||
//#include "lzma/delta.h"
|
||||
#include "lzma/lzma12.h"
|
||||
|
||||
/* Container formats */
|
||||
#include "lzma/container.h"
|
||||
|
||||
/* Advanced features */
|
||||
#include "lzma/stream_flags.h"
|
||||
#include "lzma/block.h"
|
||||
#include "lzma/index.h"
|
||||
#include "lzma/index_hash.h"
|
||||
//#include "lzma/stream_flags.h"
|
||||
//#include "lzma/block.h"
|
||||
//#include "lzma/index.h"
|
||||
//#include "lzma/index_hash.h"
|
||||
|
||||
/* Hardware information */
|
||||
#include "lzma/hardware.h"
|
||||
//#include "lzma/hardware.h"
|
||||
|
||||
/*
|
||||
* All subheaders included. Undefine LZMA_H_INTERNAL to prevent applications
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -1,90 +0,0 @@
|
||||
/**
|
||||
* \file lzma/bcj.h
|
||||
* \brief Branch/Call/Jump conversion filters
|
||||
*/
|
||||
|
||||
/*
|
||||
* Author: Lasse Collin
|
||||
*
|
||||
* This file has been put into the public domain.
|
||||
* You can do whatever you want with this file.
|
||||
*
|
||||
* See ../lzma.h for information about liblzma as a whole.
|
||||
*/
|
||||
|
||||
#ifndef LZMA_H_INTERNAL
|
||||
# error Never include this file directly. Use <lzma.h> instead.
|
||||
#endif
|
||||
|
||||
|
||||
/* Filter IDs for lzma_filter.id */
|
||||
|
||||
#define LZMA_FILTER_X86 LZMA_VLI_C(0x04)
|
||||
/**<
|
||||
* Filter for x86 binaries
|
||||
*/
|
||||
|
||||
#define LZMA_FILTER_POWERPC LZMA_VLI_C(0x05)
|
||||
/**<
|
||||
* Filter for Big endian PowerPC binaries
|
||||
*/
|
||||
|
||||
#define LZMA_FILTER_IA64 LZMA_VLI_C(0x06)
|
||||
/**<
|
||||
* Filter for IA-64 (Itanium) binaries.
|
||||
*/
|
||||
|
||||
#define LZMA_FILTER_ARM LZMA_VLI_C(0x07)
|
||||
/**<
|
||||
* Filter for ARM binaries.
|
||||
*/
|
||||
|
||||
#define LZMA_FILTER_ARMTHUMB LZMA_VLI_C(0x08)
|
||||
/**<
|
||||
* Filter for ARM-Thumb binaries.
|
||||
*/
|
||||
|
||||
#define LZMA_FILTER_SPARC LZMA_VLI_C(0x09)
|
||||
/**<
|
||||
* Filter for SPARC binaries.
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* \brief Options for BCJ filters
|
||||
*
|
||||
* The BCJ filters never change the size of the data. Specifying options
|
||||
* for them is optional: if pointer to options is NULL, default value is
|
||||
* used. You probably never need to specify options to BCJ filters, so just
|
||||
* set the options pointer to NULL and be happy.
|
||||
*
|
||||
* If options with non-default values have been specified when encoding,
|
||||
* the same options must also be specified when decoding.
|
||||
*
|
||||
* \note At the moment, none of the BCJ filters support
|
||||
* LZMA_SYNC_FLUSH. If LZMA_SYNC_FLUSH is specified,
|
||||
* LZMA_OPTIONS_ERROR will be returned. If there is need,
|
||||
* partial support for LZMA_SYNC_FLUSH can be added in future.
|
||||
* Partial means that flushing would be possible only at
|
||||
* offsets that are multiple of 2, 4, or 16 depending on
|
||||
* the filter, except x86 which cannot be made to support
|
||||
* LZMA_SYNC_FLUSH predictably.
|
||||
*/
|
||||
typedef struct {
|
||||
/**
|
||||
* \brief Start offset for conversions
|
||||
*
|
||||
* This setting is useful only when the same filter is used
|
||||
* _separately_ for multiple sections of the same executable file,
|
||||
* and the sections contain cross-section branch/call/jump
|
||||
* instructions. In that case it is beneficial to set the start
|
||||
* offset of the non-first sections so that the relative addresses
|
||||
* of the cross-section branch/call/jump instructions will use the
|
||||
* same absolute addresses as in the first section.
|
||||
*
|
||||
* When the pointer to options is NULL, the default value (zero)
|
||||
* is used.
|
||||
*/
|
||||
uint32_t start_offset;
|
||||
|
||||
} lzma_options_bcj;
|
||||
@@ -1,581 +0,0 @@
|
||||
/**
|
||||
* \file lzma/block.h
|
||||
* \brief .xz Block handling
|
||||
*/
|
||||
|
||||
/*
|
||||
* Author: Lasse Collin
|
||||
*
|
||||
* This file has been put into the public domain.
|
||||
* You can do whatever you want with this file.
|
||||
*
|
||||
* See ../lzma.h for information about liblzma as a whole.
|
||||
*/
|
||||
|
||||
#ifndef LZMA_H_INTERNAL
|
||||
# error Never include this file directly. Use <lzma.h> instead.
|
||||
#endif
|
||||
|
||||
|
||||
/**
|
||||
* \brief Options for the Block and Block Header encoders and decoders
|
||||
*
|
||||
* Different Block handling functions use different parts of this structure.
|
||||
* Some read some members, other functions write, and some do both. Only the
|
||||
* members listed for reading need to be initialized when the specified
|
||||
* functions are called. The members marked for writing will be assigned
|
||||
* new values at some point either by calling the given function or by
|
||||
* later calls to lzma_code().
|
||||
*/
|
||||
typedef struct {
|
||||
/**
|
||||
* \brief Block format version
|
||||
*
|
||||
* To prevent API and ABI breakages when new features are needed,
|
||||
* a version number is used to indicate which fields in this
|
||||
* structure are in use:
|
||||
* - liblzma >= 5.0.0: version = 0 is supported.
|
||||
* - liblzma >= 5.1.4beta: Support for version = 1 was added,
|
||||
* which adds the ignore_check field.
|
||||
*
|
||||
* If version is greater than one, most Block related functions
|
||||
* will return LZMA_OPTIONS_ERROR (lzma_block_header_decode() works
|
||||
* with any version value).
|
||||
*
|
||||
* Read by:
|
||||
* - All functions that take pointer to lzma_block as argument,
|
||||
* including lzma_block_header_decode().
|
||||
*
|
||||
* Written by:
|
||||
* - lzma_block_header_decode()
|
||||
*/
|
||||
uint32_t version;
|
||||
|
||||
/**
|
||||
* \brief Size of the Block Header field
|
||||
*
|
||||
* This is always a multiple of four.
|
||||
*
|
||||
* Read by:
|
||||
* - lzma_block_header_encode()
|
||||
* - lzma_block_header_decode()
|
||||
* - lzma_block_compressed_size()
|
||||
* - lzma_block_unpadded_size()
|
||||
* - lzma_block_total_size()
|
||||
* - lzma_block_decoder()
|
||||
* - lzma_block_buffer_decode()
|
||||
*
|
||||
* Written by:
|
||||
* - lzma_block_header_size()
|
||||
* - lzma_block_buffer_encode()
|
||||
*/
|
||||
uint32_t header_size;
|
||||
# define LZMA_BLOCK_HEADER_SIZE_MIN 8
|
||||
# define LZMA_BLOCK_HEADER_SIZE_MAX 1024
|
||||
|
||||
/**
|
||||
* \brief Type of integrity Check
|
||||
*
|
||||
* The Check ID is not stored into the Block Header, thus its value
|
||||
* must be provided also when decoding.
|
||||
*
|
||||
* Read by:
|
||||
* - lzma_block_header_encode()
|
||||
* - lzma_block_header_decode()
|
||||
* - lzma_block_compressed_size()
|
||||
* - lzma_block_unpadded_size()
|
||||
* - lzma_block_total_size()
|
||||
* - lzma_block_encoder()
|
||||
* - lzma_block_decoder()
|
||||
* - lzma_block_buffer_encode()
|
||||
* - lzma_block_buffer_decode()
|
||||
*/
|
||||
lzma_check check;
|
||||
|
||||
/**
|
||||
* \brief Size of the Compressed Data in bytes
|
||||
*
|
||||
* Encoding: If this is not LZMA_VLI_UNKNOWN, Block Header encoder
|
||||
* will store this value to the Block Header. Block encoder doesn't
|
||||
* care about this value, but will set it once the encoding has been
|
||||
* finished.
|
||||
*
|
||||
* Decoding: If this is not LZMA_VLI_UNKNOWN, Block decoder will
|
||||
* verify that the size of the Compressed Data field matches
|
||||
* compressed_size.
|
||||
*
|
||||
* Usually you don't know this value when encoding in streamed mode,
|
||||
* and thus cannot write this field into the Block Header.
|
||||
*
|
||||
* In non-streamed mode you can reserve space for this field before
|
||||
* encoding the actual Block. After encoding the data, finish the
|
||||
* Block by encoding the Block Header. Steps in detail:
|
||||
*
|
||||
* - Set compressed_size to some big enough value. If you don't know
|
||||
* better, use LZMA_VLI_MAX, but remember that bigger values take
|
||||
* more space in Block Header.
|
||||
*
|
||||
* - Call lzma_block_header_size() to see how much space you need to
|
||||
* reserve for the Block Header.
|
||||
*
|
||||
* - Encode the Block using lzma_block_encoder() and lzma_code().
|
||||
* It sets compressed_size to the correct value.
|
||||
*
|
||||
* - Use lzma_block_header_encode() to encode the Block Header.
|
||||
* Because space was reserved in the first step, you don't need
|
||||
* to call lzma_block_header_size() anymore, because due to
|
||||
* reserving, header_size has to be big enough. If it is "too big",
|
||||
* lzma_block_header_encode() will add enough Header Padding to
|
||||
* make Block Header to match the size specified by header_size.
|
||||
*
|
||||
* Read by:
|
||||
* - lzma_block_header_size()
|
||||
* - lzma_block_header_encode()
|
||||
* - lzma_block_compressed_size()
|
||||
* - lzma_block_unpadded_size()
|
||||
* - lzma_block_total_size()
|
||||
* - lzma_block_decoder()
|
||||
* - lzma_block_buffer_decode()
|
||||
*
|
||||
* Written by:
|
||||
* - lzma_block_header_decode()
|
||||
* - lzma_block_compressed_size()
|
||||
* - lzma_block_encoder()
|
||||
* - lzma_block_decoder()
|
||||
* - lzma_block_buffer_encode()
|
||||
* - lzma_block_buffer_decode()
|
||||
*/
|
||||
lzma_vli compressed_size;
|
||||
|
||||
/**
|
||||
* \brief Uncompressed Size in bytes
|
||||
*
|
||||
* This is handled very similarly to compressed_size above.
|
||||
*
|
||||
* uncompressed_size is needed by fewer functions than
|
||||
* compressed_size. This is because uncompressed_size isn't
|
||||
* needed to validate that Block stays within proper limits.
|
||||
*
|
||||
* Read by:
|
||||
* - lzma_block_header_size()
|
||||
* - lzma_block_header_encode()
|
||||
* - lzma_block_decoder()
|
||||
* - lzma_block_buffer_decode()
|
||||
*
|
||||
* Written by:
|
||||
* - lzma_block_header_decode()
|
||||
* - lzma_block_encoder()
|
||||
* - lzma_block_decoder()
|
||||
* - lzma_block_buffer_encode()
|
||||
* - lzma_block_buffer_decode()
|
||||
*/
|
||||
lzma_vli uncompressed_size;
|
||||
|
||||
/**
|
||||
* \brief Array of filters
|
||||
*
|
||||
* There can be 1-4 filters. The end of the array is marked with
|
||||
* .id = LZMA_VLI_UNKNOWN.
|
||||
*
|
||||
* Read by:
|
||||
* - lzma_block_header_size()
|
||||
* - lzma_block_header_encode()
|
||||
* - lzma_block_encoder()
|
||||
* - lzma_block_decoder()
|
||||
* - lzma_block_buffer_encode()
|
||||
* - lzma_block_buffer_decode()
|
||||
*
|
||||
* Written by:
|
||||
* - lzma_block_header_decode(): Note that this does NOT free()
|
||||
* the old filter options structures. All unused filters[] will
|
||||
* have .id == LZMA_VLI_UNKNOWN and .options == NULL. If
|
||||
* decoding fails, all filters[] are guaranteed to be
|
||||
* LZMA_VLI_UNKNOWN and NULL.
|
||||
*
|
||||
* \note Because of the array is terminated with
|
||||
* .id = LZMA_VLI_UNKNOWN, the actual array must
|
||||
* have LZMA_FILTERS_MAX + 1 members or the Block
|
||||
* Header decoder will overflow the buffer.
|
||||
*/
|
||||
lzma_filter *filters;
|
||||
|
||||
/**
|
||||
* \brief Raw value stored in the Check field
|
||||
*
|
||||
* After successful coding, the first lzma_check_size(check) bytes
|
||||
* of this array contain the raw value stored in the Check field.
|
||||
*
|
||||
* Note that CRC32 and CRC64 are stored in little endian byte order.
|
||||
* Take it into account if you display the Check values to the user.
|
||||
*
|
||||
* Written by:
|
||||
* - lzma_block_encoder()
|
||||
* - lzma_block_decoder()
|
||||
* - lzma_block_buffer_encode()
|
||||
* - lzma_block_buffer_decode()
|
||||
*/
|
||||
uint8_t raw_check[LZMA_CHECK_SIZE_MAX];
|
||||
|
||||
/*
|
||||
* Reserved space to allow possible future extensions without
|
||||
* breaking the ABI. You should not touch these, because the names
|
||||
* of these variables may change. These are and will never be used
|
||||
* with the currently supported options, so it is safe to leave these
|
||||
* uninitialized.
|
||||
*/
|
||||
void *reserved_ptr1;
|
||||
void *reserved_ptr2;
|
||||
void *reserved_ptr3;
|
||||
uint32_t reserved_int1;
|
||||
uint32_t reserved_int2;
|
||||
lzma_vli reserved_int3;
|
||||
lzma_vli reserved_int4;
|
||||
lzma_vli reserved_int5;
|
||||
lzma_vli reserved_int6;
|
||||
lzma_vli reserved_int7;
|
||||
lzma_vli reserved_int8;
|
||||
lzma_reserved_enum reserved_enum1;
|
||||
lzma_reserved_enum reserved_enum2;
|
||||
lzma_reserved_enum reserved_enum3;
|
||||
lzma_reserved_enum reserved_enum4;
|
||||
|
||||
/**
|
||||
* \brief A flag to Block decoder to not verify the Check field
|
||||
*
|
||||
* This field is supported by liblzma >= 5.1.4beta if .version >= 1.
|
||||
*
|
||||
* If this is set to true, the integrity check won't be calculated
|
||||
* and verified. Unless you know what you are doing, you should
|
||||
* leave this to false. (A reason to set this to true is when the
|
||||
* file integrity is verified externally anyway and you want to
|
||||
* speed up the decompression, which matters mostly when using
|
||||
* SHA-256 as the integrity check.)
|
||||
*
|
||||
* If .version >= 1, read by:
|
||||
* - lzma_block_decoder()
|
||||
* - lzma_block_buffer_decode()
|
||||
*
|
||||
* Written by (.version is ignored):
|
||||
* - lzma_block_header_decode() always sets this to false
|
||||
*/
|
||||
lzma_bool ignore_check;
|
||||
|
||||
lzma_bool reserved_bool2;
|
||||
lzma_bool reserved_bool3;
|
||||
lzma_bool reserved_bool4;
|
||||
lzma_bool reserved_bool5;
|
||||
lzma_bool reserved_bool6;
|
||||
lzma_bool reserved_bool7;
|
||||
lzma_bool reserved_bool8;
|
||||
|
||||
} lzma_block;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Decode the Block Header Size field
|
||||
*
|
||||
* To decode Block Header using lzma_block_header_decode(), the size of the
|
||||
* Block Header has to be known and stored into lzma_block.header_size.
|
||||
* The size can be calculated from the first byte of a Block using this macro.
|
||||
* Note that if the first byte is 0x00, it indicates beginning of Index; use
|
||||
* this macro only when the byte is not 0x00.
|
||||
*
|
||||
* There is no encoding macro, because Block Header encoder is enough for that.
|
||||
*/
|
||||
#define lzma_block_header_size_decode(b) (((uint32_t)(b) + 1) * 4)
|
||||
|
||||
|
||||
/**
|
||||
* \brief Calculate Block Header Size
|
||||
*
|
||||
* Calculate the minimum size needed for the Block Header field using the
|
||||
* settings specified in the lzma_block structure. Note that it is OK to
|
||||
* increase the calculated header_size value as long as it is a multiple of
|
||||
* four and doesn't exceed LZMA_BLOCK_HEADER_SIZE_MAX. Increasing header_size
|
||||
* just means that lzma_block_header_encode() will add Header Padding.
|
||||
*
|
||||
* \return - LZMA_OK: Size calculated successfully and stored to
|
||||
* block->header_size.
|
||||
* - LZMA_OPTIONS_ERROR: Unsupported version, filters or
|
||||
* filter options.
|
||||
* - LZMA_PROG_ERROR: Invalid values like compressed_size == 0.
|
||||
*
|
||||
* \note This doesn't check that all the options are valid i.e. this
|
||||
* may return LZMA_OK even if lzma_block_header_encode() or
|
||||
* lzma_block_encoder() would fail. If you want to validate the
|
||||
* filter chain, consider using lzma_memlimit_encoder() which as
|
||||
* a side-effect validates the filter chain.
|
||||
*/
|
||||
extern LZMA_API(lzma_ret) lzma_block_header_size(lzma_block *block)
|
||||
lzma_nothrow lzma_attr_warn_unused_result;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Encode Block Header
|
||||
*
|
||||
* The caller must have calculated the size of the Block Header already with
|
||||
* lzma_block_header_size(). If a value larger than the one calculated by
|
||||
* lzma_block_header_size() is used, the Block Header will be padded to the
|
||||
* specified size.
|
||||
*
|
||||
* \param out Beginning of the output buffer. This must be
|
||||
* at least block->header_size bytes.
|
||||
* \param block Block options to be encoded.
|
||||
*
|
||||
* \return - LZMA_OK: Encoding was successful. block->header_size
|
||||
* bytes were written to output buffer.
|
||||
* - LZMA_OPTIONS_ERROR: Invalid or unsupported options.
|
||||
* - LZMA_PROG_ERROR: Invalid arguments, for example
|
||||
* block->header_size is invalid or block->filters is NULL.
|
||||
*/
|
||||
extern LZMA_API(lzma_ret) lzma_block_header_encode(
|
||||
const lzma_block *block, uint8_t *out)
|
||||
lzma_nothrow lzma_attr_warn_unused_result;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Decode Block Header
|
||||
*
|
||||
* block->version should (usually) be set to the highest value supported
|
||||
* by the application. If the application sets block->version to a value
|
||||
* higher than supported by the current liblzma version, this function will
|
||||
* downgrade block->version to the highest value supported by it. Thus one
|
||||
* should check the value of block->version after calling this function if
|
||||
* block->version was set to a non-zero value and the application doesn't
|
||||
* otherwise know that the liblzma version being used is new enough to
|
||||
* support the specified block->version.
|
||||
*
|
||||
* The size of the Block Header must have already been decoded with
|
||||
* lzma_block_header_size_decode() macro and stored to block->header_size.
|
||||
*
|
||||
* The integrity check type from Stream Header must have been stored
|
||||
* to block->check.
|
||||
*
|
||||
* block->filters must have been allocated, but they don't need to be
|
||||
* initialized (possible existing filter options are not freed).
|
||||
*
|
||||
* \param block Destination for Block options.
|
||||
* \param allocator lzma_allocator for custom allocator functions.
|
||||
* Set to NULL to use malloc() (and also free()
|
||||
* if an error occurs).
|
||||
* \param in Beginning of the input buffer. This must be
|
||||
* at least block->header_size bytes.
|
||||
*
|
||||
* \return - LZMA_OK: Decoding was successful. block->header_size
|
||||
* bytes were read from the input buffer.
|
||||
* - LZMA_OPTIONS_ERROR: The Block Header specifies some
|
||||
* unsupported options such as unsupported filters. This can
|
||||
* happen also if block->version was set to a too low value
|
||||
* compared to what would be required to properly represent
|
||||
* the information stored in the Block Header.
|
||||
* - LZMA_DATA_ERROR: Block Header is corrupt, for example,
|
||||
* the CRC32 doesn't match.
|
||||
* - LZMA_PROG_ERROR: Invalid arguments, for example
|
||||
* block->header_size is invalid or block->filters is NULL.
|
||||
*/
|
||||
extern LZMA_API(lzma_ret) lzma_block_header_decode(lzma_block *block,
|
||||
const lzma_allocator *allocator, const uint8_t *in)
|
||||
lzma_nothrow lzma_attr_warn_unused_result;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Validate and set Compressed Size according to Unpadded Size
|
||||
*
|
||||
* Block Header stores Compressed Size, but Index has Unpadded Size. If the
|
||||
* application has already parsed the Index and is now decoding Blocks,
|
||||
* it can calculate Compressed Size from Unpadded Size. This function does
|
||||
* exactly that with error checking:
|
||||
*
|
||||
* - Compressed Size calculated from Unpadded Size must be positive integer,
|
||||
* that is, Unpadded Size must be big enough that after Block Header and
|
||||
* Check fields there's still at least one byte for Compressed Size.
|
||||
*
|
||||
* - If Compressed Size was present in Block Header, the new value
|
||||
* calculated from Unpadded Size is compared against the value
|
||||
* from Block Header.
|
||||
*
|
||||
* \note This function must be called _after_ decoding the Block Header
|
||||
* field so that it can properly validate Compressed Size if it
|
||||
* was present in Block Header.
|
||||
*
|
||||
* \return - LZMA_OK: block->compressed_size was set successfully.
|
||||
* - LZMA_DATA_ERROR: unpadded_size is too small compared to
|
||||
* block->header_size and lzma_check_size(block->check).
|
||||
* - LZMA_PROG_ERROR: Some values are invalid. For example,
|
||||
* block->header_size must be a multiple of four and
|
||||
* between 8 and 1024 inclusive.
|
||||
*/
|
||||
extern LZMA_API(lzma_ret) lzma_block_compressed_size(
|
||||
lzma_block *block, lzma_vli unpadded_size)
|
||||
lzma_nothrow lzma_attr_warn_unused_result;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Calculate Unpadded Size
|
||||
*
|
||||
* The Index field stores Unpadded Size and Uncompressed Size. The latter
|
||||
* can be taken directly from the lzma_block structure after coding a Block,
|
||||
* but Unpadded Size needs to be calculated from Block Header Size,
|
||||
* Compressed Size, and size of the Check field. This is where this function
|
||||
* is needed.
|
||||
*
|
||||
* \return Unpadded Size on success, or zero on error.
|
||||
*/
|
||||
extern LZMA_API(lzma_vli) lzma_block_unpadded_size(const lzma_block *block)
|
||||
lzma_nothrow lzma_attr_pure;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Calculate the total encoded size of a Block
|
||||
*
|
||||
* This is equivalent to lzma_block_unpadded_size() except that the returned
|
||||
* value includes the size of the Block Padding field.
|
||||
*
|
||||
* \return On success, total encoded size of the Block. On error,
|
||||
* zero is returned.
|
||||
*/
|
||||
extern LZMA_API(lzma_vli) lzma_block_total_size(const lzma_block *block)
|
||||
lzma_nothrow lzma_attr_pure;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Initialize .xz Block encoder
|
||||
*
|
||||
* Valid actions for lzma_code() are LZMA_RUN, LZMA_SYNC_FLUSH (only if the
|
||||
* filter chain supports it), and LZMA_FINISH.
|
||||
*
|
||||
* \return - LZMA_OK: All good, continue with lzma_code().
|
||||
* - LZMA_MEM_ERROR
|
||||
* - LZMA_OPTIONS_ERROR
|
||||
* - LZMA_UNSUPPORTED_CHECK: block->check specifies a Check ID
|
||||
* that is not supported by this buid of liblzma. Initializing
|
||||
* the encoder failed.
|
||||
* - LZMA_PROG_ERROR
|
||||
*/
|
||||
extern LZMA_API(lzma_ret) lzma_block_encoder(
|
||||
lzma_stream *strm, lzma_block *block)
|
||||
lzma_nothrow lzma_attr_warn_unused_result;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Initialize .xz Block decoder
|
||||
*
|
||||
* Valid actions for lzma_code() are LZMA_RUN and LZMA_FINISH. Using
|
||||
* LZMA_FINISH is not required. It is supported only for convenience.
|
||||
*
|
||||
* \return - LZMA_OK: All good, continue with lzma_code().
|
||||
* - LZMA_UNSUPPORTED_CHECK: Initialization was successful, but
|
||||
* the given Check ID is not supported, thus Check will be
|
||||
* ignored.
|
||||
* - LZMA_PROG_ERROR
|
||||
* - LZMA_MEM_ERROR
|
||||
*/
|
||||
extern LZMA_API(lzma_ret) lzma_block_decoder(
|
||||
lzma_stream *strm, lzma_block *block)
|
||||
lzma_nothrow lzma_attr_warn_unused_result;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Calculate maximum output size for single-call Block encoding
|
||||
*
|
||||
* This is equivalent to lzma_stream_buffer_bound() but for .xz Blocks.
|
||||
* See the documentation of lzma_stream_buffer_bound().
|
||||
*/
|
||||
extern LZMA_API(size_t) lzma_block_buffer_bound(size_t uncompressed_size)
|
||||
lzma_nothrow;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Single-call .xz Block encoder
|
||||
*
|
||||
* In contrast to the multi-call encoder initialized with
|
||||
* lzma_block_encoder(), this function encodes also the Block Header. This
|
||||
* is required to make it possible to write appropriate Block Header also
|
||||
* in case the data isn't compressible, and different filter chain has to be
|
||||
* used to encode the data in uncompressed form using uncompressed chunks
|
||||
* of the LZMA2 filter.
|
||||
*
|
||||
* When the data isn't compressible, header_size, compressed_size, and
|
||||
* uncompressed_size are set just like when the data was compressible, but
|
||||
* it is possible that header_size is too small to hold the filter chain
|
||||
* specified in block->filters, because that isn't necessarily the filter
|
||||
* chain that was actually used to encode the data. lzma_block_unpadded_size()
|
||||
* still works normally, because it doesn't read the filters array.
|
||||
*
|
||||
* \param block Block options: block->version, block->check,
|
||||
* and block->filters must have been initialized.
|
||||
* \param allocator lzma_allocator for custom allocator functions.
|
||||
* Set to NULL to use malloc() and free().
|
||||
* \param in Beginning of the input buffer
|
||||
* \param in_size Size of the input buffer
|
||||
* \param out Beginning of the output buffer
|
||||
* \param out_pos The next byte will be written to out[*out_pos].
|
||||
* *out_pos is updated only if encoding succeeds.
|
||||
* \param out_size Size of the out buffer; the first byte into
|
||||
* which no data is written to is out[out_size].
|
||||
*
|
||||
* \return - LZMA_OK: Encoding was successful.
|
||||
* - LZMA_BUF_ERROR: Not enough output buffer space.
|
||||
* - LZMA_UNSUPPORTED_CHECK
|
||||
* - LZMA_OPTIONS_ERROR
|
||||
* - LZMA_MEM_ERROR
|
||||
* - LZMA_DATA_ERROR
|
||||
* - LZMA_PROG_ERROR
|
||||
*/
|
||||
extern LZMA_API(lzma_ret) lzma_block_buffer_encode(
|
||||
lzma_block *block, const lzma_allocator *allocator,
|
||||
const uint8_t *in, size_t in_size,
|
||||
uint8_t *out, size_t *out_pos, size_t out_size)
|
||||
lzma_nothrow lzma_attr_warn_unused_result;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Single-call uncompressed .xz Block encoder
|
||||
*
|
||||
* This is like lzma_block_buffer_encode() except this doesn't try to
|
||||
* compress the data and instead encodes the data using LZMA2 uncompressed
|
||||
* chunks. The required output buffer size can be determined with
|
||||
* lzma_block_buffer_bound().
|
||||
*
|
||||
* Since the data won't be compressed, this function ignores block->filters.
|
||||
* This function doesn't take lzma_allocator because this function doesn't
|
||||
* allocate any memory from the heap.
|
||||
*/
|
||||
extern LZMA_API(lzma_ret) lzma_block_uncomp_encode(lzma_block *block,
|
||||
const uint8_t *in, size_t in_size,
|
||||
uint8_t *out, size_t *out_pos, size_t out_size)
|
||||
lzma_nothrow lzma_attr_warn_unused_result;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Single-call .xz Block decoder
|
||||
*
|
||||
* This is single-call equivalent of lzma_block_decoder(), and requires that
|
||||
* the caller has already decoded Block Header and checked its memory usage.
|
||||
*
|
||||
* \param block Block options just like with lzma_block_decoder().
|
||||
* \param allocator lzma_allocator for custom allocator functions.
|
||||
* Set to NULL to use malloc() and free().
|
||||
* \param in Beginning of the input buffer
|
||||
* \param in_pos The next byte will be read from in[*in_pos].
|
||||
* *in_pos is updated only if decoding succeeds.
|
||||
* \param in_size Size of the input buffer; the first byte that
|
||||
* won't be read is in[in_size].
|
||||
* \param out Beginning of the output buffer
|
||||
* \param out_pos The next byte will be written to out[*out_pos].
|
||||
* *out_pos is updated only if encoding succeeds.
|
||||
* \param out_size Size of the out buffer; the first byte into
|
||||
* which no data is written to is out[out_size].
|
||||
*
|
||||
* \return - LZMA_OK: Decoding was successful.
|
||||
* - LZMA_OPTIONS_ERROR
|
||||
* - LZMA_DATA_ERROR
|
||||
* - LZMA_MEM_ERROR
|
||||
* - LZMA_BUF_ERROR: Output buffer was too small.
|
||||
* - LZMA_PROG_ERROR
|
||||
*/
|
||||
extern LZMA_API(lzma_ret) lzma_block_buffer_decode(
|
||||
lzma_block *block, const lzma_allocator *allocator,
|
||||
const uint8_t *in, size_t *in_pos, size_t in_size,
|
||||
uint8_t *out, size_t *out_pos, size_t out_size)
|
||||
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
|
||||
|
||||
@@ -1,77 +0,0 @@
|
||||
/**
|
||||
* \file lzma/delta.h
|
||||
* \brief Delta filter
|
||||
*/
|
||||
|
||||
/*
|
||||
* Author: Lasse Collin
|
||||
*
|
||||
* This file has been put into the public domain.
|
||||
* You can do whatever you want with this file.
|
||||
*
|
||||
* See ../lzma.h for information about liblzma as a whole.
|
||||
*/
|
||||
|
||||
#ifndef LZMA_H_INTERNAL
|
||||
# error Never include this file directly. Use <lzma.h> instead.
|
||||
#endif
|
||||
|
||||
|
||||
/**
|
||||
* \brief Filter ID
|
||||
*
|
||||
* Filter ID of the Delta filter. This is used as lzma_filter.id.
|
||||
*/
|
||||
#define LZMA_FILTER_DELTA LZMA_VLI_C(0x03)
|
||||
|
||||
|
||||
/**
|
||||
* \brief Type of the delta calculation
|
||||
*
|
||||
* Currently only byte-wise delta is supported. Other possible types could
|
||||
* be, for example, delta of 16/32/64-bit little/big endian integers, but
|
||||
* these are not currently planned since byte-wise delta is almost as good.
|
||||
*/
|
||||
typedef enum {
|
||||
LZMA_DELTA_TYPE_BYTE
|
||||
} lzma_delta_type;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Options for the Delta filter
|
||||
*
|
||||
* These options are needed by both encoder and decoder.
|
||||
*/
|
||||
typedef struct {
|
||||
/** For now, this must always be LZMA_DELTA_TYPE_BYTE. */
|
||||
lzma_delta_type type;
|
||||
|
||||
/**
|
||||
* \brief Delta distance
|
||||
*
|
||||
* With the only currently supported type, LZMA_DELTA_TYPE_BYTE,
|
||||
* the distance is as bytes.
|
||||
*
|
||||
* Examples:
|
||||
* - 16-bit stereo audio: distance = 4 bytes
|
||||
* - 24-bit RGB image data: distance = 3 bytes
|
||||
*/
|
||||
uint32_t dist;
|
||||
# define LZMA_DELTA_DIST_MIN 1
|
||||
# define LZMA_DELTA_DIST_MAX 256
|
||||
|
||||
/*
|
||||
* Reserved space to allow possible future extensions without
|
||||
* breaking the ABI. You should not touch these, because the names
|
||||
* of these variables may change. These are and will never be used
|
||||
* when type is LZMA_DELTA_TYPE_BYTE, so it is safe to leave these
|
||||
* uninitialized.
|
||||
*/
|
||||
uint32_t reserved_int1;
|
||||
uint32_t reserved_int2;
|
||||
uint32_t reserved_int3;
|
||||
uint32_t reserved_int4;
|
||||
void *reserved_ptr1;
|
||||
void *reserved_ptr2;
|
||||
|
||||
} lzma_options_delta;
|
||||
@@ -1,64 +0,0 @@
|
||||
/**
|
||||
* \file lzma/hardware.h
|
||||
* \brief Hardware information
|
||||
*
|
||||
* Since liblzma can consume a lot of system resources, it also provides
|
||||
* ways to limit the resource usage. Applications linking against liblzma
|
||||
* need to do the actual decisions how much resources to let liblzma to use.
|
||||
* To ease making these decisions, liblzma provides functions to find out
|
||||
* the relevant capabilities of the underlaying hardware. Currently there
|
||||
* is only a function to find out the amount of RAM, but in the future there
|
||||
* will be also a function to detect how many concurrent threads the system
|
||||
* can run.
|
||||
*
|
||||
* \note On some operating systems, these function may temporarily
|
||||
* load a shared library or open file descriptor(s) to find out
|
||||
* the requested hardware information. Unless the application
|
||||
* assumes that specific file descriptors are not touched by
|
||||
* other threads, this should have no effect on thread safety.
|
||||
* Possible operations involving file descriptors will restart
|
||||
* the syscalls if they return EINTR.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Author: Lasse Collin
|
||||
*
|
||||
* This file has been put into the public domain.
|
||||
* You can do whatever you want with this file.
|
||||
*
|
||||
* See ../lzma.h for information about liblzma as a whole.
|
||||
*/
|
||||
|
||||
#ifndef LZMA_H_INTERNAL
|
||||
# error Never include this file directly. Use <lzma.h> instead.
|
||||
#endif
|
||||
|
||||
|
||||
/**
|
||||
* \brief Get the total amount of physical memory (RAM) in bytes
|
||||
*
|
||||
* This function may be useful when determining a reasonable memory
|
||||
* usage limit for decompressing or how much memory it is OK to use
|
||||
* for compressing.
|
||||
*
|
||||
* \return On success, the total amount of physical memory in bytes
|
||||
* is returned. If the amount of RAM cannot be determined,
|
||||
* zero is returned. This can happen if an error occurs
|
||||
* or if there is no code in liblzma to detect the amount
|
||||
* of RAM on the specific operating system.
|
||||
*/
|
||||
extern LZMA_API(uint64_t) lzma_physmem(void) lzma_nothrow;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Get the number of processor cores or threads
|
||||
*
|
||||
* This function may be useful when determining how many threads to use.
|
||||
* If the hardware supports more than one thread per CPU core, the number
|
||||
* of hardware threads is returned if that information is available.
|
||||
*
|
||||
* \return On success, the number of available CPU threads or cores is
|
||||
* returned. If this information isn't available or an error
|
||||
* occurs, zero is returned.
|
||||
*/
|
||||
extern LZMA_API(uint32_t) lzma_cputhreads(void) lzma_nothrow;
|
||||
@@ -1,682 +0,0 @@
|
||||
/**
|
||||
* \file lzma/index.h
|
||||
* \brief Handling of .xz Index and related information
|
||||
*/
|
||||
|
||||
/*
|
||||
* Author: Lasse Collin
|
||||
*
|
||||
* This file has been put into the public domain.
|
||||
* You can do whatever you want with this file.
|
||||
*
|
||||
* See ../lzma.h for information about liblzma as a whole.
|
||||
*/
|
||||
|
||||
#ifndef LZMA_H_INTERNAL
|
||||
# error Never include this file directly. Use <lzma.h> instead.
|
||||
#endif
|
||||
|
||||
|
||||
/**
|
||||
* \brief Opaque data type to hold the Index(es) and other information
|
||||
*
|
||||
* lzma_index often holds just one .xz Index and possibly the Stream Flags
|
||||
* of the same Stream and size of the Stream Padding field. However,
|
||||
* multiple lzma_indexes can be concatenated with lzma_index_cat() and then
|
||||
* there may be information about multiple Streams in the same lzma_index.
|
||||
*
|
||||
* Notes about thread safety: Only one thread may modify lzma_index at
|
||||
* a time. All functions that take non-const pointer to lzma_index
|
||||
* modify it. As long as no thread is modifying the lzma_index, getting
|
||||
* information from the same lzma_index can be done from multiple threads
|
||||
* at the same time with functions that take a const pointer to
|
||||
* lzma_index or use lzma_index_iter. The same iterator must be used
|
||||
* only by one thread at a time, of course, but there can be as many
|
||||
* iterators for the same lzma_index as needed.
|
||||
*/
|
||||
typedef struct lzma_index_s lzma_index;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Iterator to get information about Blocks and Streams
|
||||
*/
|
||||
typedef struct {
|
||||
struct {
|
||||
/**
|
||||
* \brief Pointer to Stream Flags
|
||||
*
|
||||
* This is NULL if Stream Flags have not been set for
|
||||
* this Stream with lzma_index_stream_flags().
|
||||
*/
|
||||
const lzma_stream_flags *flags;
|
||||
|
||||
const void *reserved_ptr1;
|
||||
const void *reserved_ptr2;
|
||||
const void *reserved_ptr3;
|
||||
|
||||
/**
|
||||
* \brief Stream number in the lzma_index
|
||||
*
|
||||
* The first Stream is 1.
|
||||
*/
|
||||
lzma_vli number;
|
||||
|
||||
/**
|
||||
* \brief Number of Blocks in the Stream
|
||||
*
|
||||
* If this is zero, the block structure below has
|
||||
* undefined values.
|
||||
*/
|
||||
lzma_vli block_count;
|
||||
|
||||
/**
|
||||
* \brief Compressed start offset of this Stream
|
||||
*
|
||||
* The offset is relative to the beginning of the lzma_index
|
||||
* (i.e. usually the beginning of the .xz file).
|
||||
*/
|
||||
lzma_vli compressed_offset;
|
||||
|
||||
/**
|
||||
* \brief Uncompressed start offset of this Stream
|
||||
*
|
||||
* The offset is relative to the beginning of the lzma_index
|
||||
* (i.e. usually the beginning of the .xz file).
|
||||
*/
|
||||
lzma_vli uncompressed_offset;
|
||||
|
||||
/**
|
||||
* \brief Compressed size of this Stream
|
||||
*
|
||||
* This includes all headers except the possible
|
||||
* Stream Padding after this Stream.
|
||||
*/
|
||||
lzma_vli compressed_size;
|
||||
|
||||
/**
|
||||
* \brief Uncompressed size of this Stream
|
||||
*/
|
||||
lzma_vli uncompressed_size;
|
||||
|
||||
/**
|
||||
* \brief Size of Stream Padding after this Stream
|
||||
*
|
||||
* If it hasn't been set with lzma_index_stream_padding(),
|
||||
* this defaults to zero. Stream Padding is always
|
||||
* a multiple of four bytes.
|
||||
*/
|
||||
lzma_vli padding;
|
||||
|
||||
lzma_vli reserved_vli1;
|
||||
lzma_vli reserved_vli2;
|
||||
lzma_vli reserved_vli3;
|
||||
lzma_vli reserved_vli4;
|
||||
} stream;
|
||||
|
||||
struct {
|
||||
/**
|
||||
* \brief Block number in the file
|
||||
*
|
||||
* The first Block is 1.
|
||||
*/
|
||||
lzma_vli number_in_file;
|
||||
|
||||
/**
|
||||
* \brief Compressed start offset of this Block
|
||||
*
|
||||
* This offset is relative to the beginning of the
|
||||
* lzma_index (i.e. usually the beginning of the .xz file).
|
||||
* Normally this is where you should seek in the .xz file
|
||||
* to start decompressing this Block.
|
||||
*/
|
||||
lzma_vli compressed_file_offset;
|
||||
|
||||
/**
|
||||
* \brief Uncompressed start offset of this Block
|
||||
*
|
||||
* This offset is relative to the beginning of the lzma_index
|
||||
* (i.e. usually the beginning of the .xz file).
|
||||
*
|
||||
* When doing random-access reading, it is possible that
|
||||
* the target offset is not exactly at Block boundary. One
|
||||
* will need to compare the target offset against
|
||||
* uncompressed_file_offset or uncompressed_stream_offset,
|
||||
* and possibly decode and throw away some amount of data
|
||||
* before reaching the target offset.
|
||||
*/
|
||||
lzma_vli uncompressed_file_offset;
|
||||
|
||||
/**
|
||||
* \brief Block number in this Stream
|
||||
*
|
||||
* The first Block is 1.
|
||||
*/
|
||||
lzma_vli number_in_stream;
|
||||
|
||||
/**
|
||||
* \brief Compressed start offset of this Block
|
||||
*
|
||||
* This offset is relative to the beginning of the Stream
|
||||
* containing this Block.
|
||||
*/
|
||||
lzma_vli compressed_stream_offset;
|
||||
|
||||
/**
|
||||
* \brief Uncompressed start offset of this Block
|
||||
*
|
||||
* This offset is relative to the beginning of the Stream
|
||||
* containing this Block.
|
||||
*/
|
||||
lzma_vli uncompressed_stream_offset;
|
||||
|
||||
/**
|
||||
* \brief Uncompressed size of this Block
|
||||
*
|
||||
* You should pass this to the Block decoder if you will
|
||||
* decode this Block. It will allow the Block decoder to
|
||||
* validate the uncompressed size.
|
||||
*/
|
||||
lzma_vli uncompressed_size;
|
||||
|
||||
/**
|
||||
* \brief Unpadded size of this Block
|
||||
*
|
||||
* You should pass this to the Block decoder if you will
|
||||
* decode this Block. It will allow the Block decoder to
|
||||
* validate the unpadded size.
|
||||
*/
|
||||
lzma_vli unpadded_size;
|
||||
|
||||
/**
|
||||
* \brief Total compressed size
|
||||
*
|
||||
* This includes all headers and padding in this Block.
|
||||
* This is useful if you need to know how many bytes
|
||||
* the Block decoder will actually read.
|
||||
*/
|
||||
lzma_vli total_size;
|
||||
|
||||
lzma_vli reserved_vli1;
|
||||
lzma_vli reserved_vli2;
|
||||
lzma_vli reserved_vli3;
|
||||
lzma_vli reserved_vli4;
|
||||
|
||||
const void *reserved_ptr1;
|
||||
const void *reserved_ptr2;
|
||||
const void *reserved_ptr3;
|
||||
const void *reserved_ptr4;
|
||||
} block;
|
||||
|
||||
/*
|
||||
* Internal data which is used to store the state of the iterator.
|
||||
* The exact format may vary between liblzma versions, so don't
|
||||
* touch these in any way.
|
||||
*/
|
||||
union {
|
||||
const void *p;
|
||||
size_t s;
|
||||
lzma_vli v;
|
||||
} internal[6];
|
||||
} lzma_index_iter;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Operation mode for lzma_index_iter_next()
|
||||
*/
|
||||
typedef enum {
|
||||
LZMA_INDEX_ITER_ANY = 0,
|
||||
/**<
|
||||
* \brief Get the next Block or Stream
|
||||
*
|
||||
* Go to the next Block if the current Stream has at least
|
||||
* one Block left. Otherwise go to the next Stream even if
|
||||
* it has no Blocks. If the Stream has no Blocks
|
||||
* (lzma_index_iter.stream.block_count == 0),
|
||||
* lzma_index_iter.block will have undefined values.
|
||||
*/
|
||||
|
||||
LZMA_INDEX_ITER_STREAM = 1,
|
||||
/**<
|
||||
* \brief Get the next Stream
|
||||
*
|
||||
* Go to the next Stream even if the current Stream has
|
||||
* unread Blocks left. If the next Stream has at least one
|
||||
* Block, the iterator will point to the first Block.
|
||||
* If there are no Blocks, lzma_index_iter.block will have
|
||||
* undefined values.
|
||||
*/
|
||||
|
||||
LZMA_INDEX_ITER_BLOCK = 2,
|
||||
/**<
|
||||
* \brief Get the next Block
|
||||
*
|
||||
* Go to the next Block if the current Stream has at least
|
||||
* one Block left. If the current Stream has no Blocks left,
|
||||
* the next Stream with at least one Block is located and
|
||||
* the iterator will be made to point to the first Block of
|
||||
* that Stream.
|
||||
*/
|
||||
|
||||
LZMA_INDEX_ITER_NONEMPTY_BLOCK = 3
|
||||
/**<
|
||||
* \brief Get the next non-empty Block
|
||||
*
|
||||
* This is like LZMA_INDEX_ITER_BLOCK except that it will
|
||||
* skip Blocks whose Uncompressed Size is zero.
|
||||
*/
|
||||
|
||||
} lzma_index_iter_mode;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Calculate memory usage of lzma_index
|
||||
*
|
||||
* On disk, the size of the Index field depends on both the number of Records
|
||||
* stored and how big values the Records store (due to variable-length integer
|
||||
* encoding). When the Index is kept in lzma_index structure, the memory usage
|
||||
* depends only on the number of Records/Blocks stored in the Index(es), and
|
||||
* in case of concatenated lzma_indexes, the number of Streams. The size in
|
||||
* RAM is almost always significantly bigger than in the encoded form on disk.
|
||||
*
|
||||
* This function calculates an approximate amount of memory needed hold
|
||||
* the given number of Streams and Blocks in lzma_index structure. This
|
||||
* value may vary between CPU architectures and also between liblzma versions
|
||||
* if the internal implementation is modified.
|
||||
*/
|
||||
extern LZMA_API(uint64_t) lzma_index_memusage(
|
||||
lzma_vli streams, lzma_vli blocks) lzma_nothrow;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Calculate the memory usage of an existing lzma_index
|
||||
*
|
||||
* This is a shorthand for lzma_index_memusage(lzma_index_stream_count(i),
|
||||
* lzma_index_block_count(i)).
|
||||
*/
|
||||
extern LZMA_API(uint64_t) lzma_index_memused(const lzma_index *i)
|
||||
lzma_nothrow;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Allocate and initialize a new lzma_index structure
|
||||
*
|
||||
* \return On success, a pointer to an empty initialized lzma_index is
|
||||
* returned. If allocation fails, NULL is returned.
|
||||
*/
|
||||
extern LZMA_API(lzma_index *) lzma_index_init(const lzma_allocator *allocator)
|
||||
lzma_nothrow;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Deallocate lzma_index
|
||||
*
|
||||
* If i is NULL, this does nothing.
|
||||
*/
|
||||
extern LZMA_API(void) lzma_index_end(
|
||||
lzma_index *i, const lzma_allocator *allocator) lzma_nothrow;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Add a new Block to lzma_index
|
||||
*
|
||||
* \param i Pointer to a lzma_index structure
|
||||
* \param allocator Pointer to lzma_allocator, or NULL to
|
||||
* use malloc()
|
||||
* \param unpadded_size Unpadded Size of a Block. This can be
|
||||
* calculated with lzma_block_unpadded_size()
|
||||
* after encoding or decoding the Block.
|
||||
* \param uncompressed_size Uncompressed Size of a Block. This can be
|
||||
* taken directly from lzma_block structure
|
||||
* after encoding or decoding the Block.
|
||||
*
|
||||
* Appending a new Block does not invalidate iterators. For example,
|
||||
* if an iterator was pointing to the end of the lzma_index, after
|
||||
* lzma_index_append() it is possible to read the next Block with
|
||||
* an existing iterator.
|
||||
*
|
||||
* \return - LZMA_OK
|
||||
* - LZMA_MEM_ERROR
|
||||
* - LZMA_DATA_ERROR: Compressed or uncompressed size of the
|
||||
* Stream or size of the Index field would grow too big.
|
||||
* - LZMA_PROG_ERROR
|
||||
*/
|
||||
extern LZMA_API(lzma_ret) lzma_index_append(
|
||||
lzma_index *i, const lzma_allocator *allocator,
|
||||
lzma_vli unpadded_size, lzma_vli uncompressed_size)
|
||||
lzma_nothrow lzma_attr_warn_unused_result;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Set the Stream Flags
|
||||
*
|
||||
* Set the Stream Flags of the last (and typically the only) Stream
|
||||
* in lzma_index. This can be useful when reading information from the
|
||||
* lzma_index, because to decode Blocks, knowing the integrity check type
|
||||
* is needed.
|
||||
*
|
||||
* The given Stream Flags are copied into internal preallocated structure
|
||||
* in the lzma_index, thus the caller doesn't need to keep the *stream_flags
|
||||
* available after calling this function.
|
||||
*
|
||||
* \return - LZMA_OK
|
||||
* - LZMA_OPTIONS_ERROR: Unsupported stream_flags->version.
|
||||
* - LZMA_PROG_ERROR
|
||||
*/
|
||||
extern LZMA_API(lzma_ret) lzma_index_stream_flags(
|
||||
lzma_index *i, const lzma_stream_flags *stream_flags)
|
||||
lzma_nothrow lzma_attr_warn_unused_result;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Get the types of integrity Checks
|
||||
*
|
||||
* If lzma_index_stream_flags() is used to set the Stream Flags for
|
||||
* every Stream, lzma_index_checks() can be used to get a bitmask to
|
||||
* indicate which Check types have been used. It can be useful e.g. if
|
||||
* showing the Check types to the user.
|
||||
*
|
||||
* The bitmask is 1 << check_id, e.g. CRC32 is 1 << 1 and SHA-256 is 1 << 10.
|
||||
*/
|
||||
extern LZMA_API(uint32_t) lzma_index_checks(const lzma_index *i)
|
||||
lzma_nothrow lzma_attr_pure;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Set the amount of Stream Padding
|
||||
*
|
||||
* Set the amount of Stream Padding of the last (and typically the only)
|
||||
* Stream in the lzma_index. This is needed when planning to do random-access
|
||||
* reading within multiple concatenated Streams.
|
||||
*
|
||||
* By default, the amount of Stream Padding is assumed to be zero bytes.
|
||||
*
|
||||
* \return - LZMA_OK
|
||||
* - LZMA_DATA_ERROR: The file size would grow too big.
|
||||
* - LZMA_PROG_ERROR
|
||||
*/
|
||||
extern LZMA_API(lzma_ret) lzma_index_stream_padding(
|
||||
lzma_index *i, lzma_vli stream_padding)
|
||||
lzma_nothrow lzma_attr_warn_unused_result;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Get the number of Streams
|
||||
*/
|
||||
extern LZMA_API(lzma_vli) lzma_index_stream_count(const lzma_index *i)
|
||||
lzma_nothrow lzma_attr_pure;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Get the number of Blocks
|
||||
*
|
||||
* This returns the total number of Blocks in lzma_index. To get number
|
||||
* of Blocks in individual Streams, use lzma_index_iter.
|
||||
*/
|
||||
extern LZMA_API(lzma_vli) lzma_index_block_count(const lzma_index *i)
|
||||
lzma_nothrow lzma_attr_pure;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Get the size of the Index field as bytes
|
||||
*
|
||||
* This is needed to verify the Backward Size field in the Stream Footer.
|
||||
*/
|
||||
extern LZMA_API(lzma_vli) lzma_index_size(const lzma_index *i)
|
||||
lzma_nothrow lzma_attr_pure;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Get the total size of the Stream
|
||||
*
|
||||
* If multiple lzma_indexes have been combined, this works as if the Blocks
|
||||
* were in a single Stream. This is useful if you are going to combine
|
||||
* Blocks from multiple Streams into a single new Stream.
|
||||
*/
|
||||
extern LZMA_API(lzma_vli) lzma_index_stream_size(const lzma_index *i)
|
||||
lzma_nothrow lzma_attr_pure;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Get the total size of the Blocks
|
||||
*
|
||||
* This doesn't include the Stream Header, Stream Footer, Stream Padding,
|
||||
* or Index fields.
|
||||
*/
|
||||
extern LZMA_API(lzma_vli) lzma_index_total_size(const lzma_index *i)
|
||||
lzma_nothrow lzma_attr_pure;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Get the total size of the file
|
||||
*
|
||||
* When no lzma_indexes have been combined with lzma_index_cat() and there is
|
||||
* no Stream Padding, this function is identical to lzma_index_stream_size().
|
||||
* If multiple lzma_indexes have been combined, this includes also the headers
|
||||
* of each separate Stream and the possible Stream Padding fields.
|
||||
*/
|
||||
extern LZMA_API(lzma_vli) lzma_index_file_size(const lzma_index *i)
|
||||
lzma_nothrow lzma_attr_pure;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Get the uncompressed size of the file
|
||||
*/
|
||||
extern LZMA_API(lzma_vli) lzma_index_uncompressed_size(const lzma_index *i)
|
||||
lzma_nothrow lzma_attr_pure;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Initialize an iterator
|
||||
*
|
||||
* \param iter Pointer to a lzma_index_iter structure
|
||||
* \param i lzma_index to which the iterator will be associated
|
||||
*
|
||||
* This function associates the iterator with the given lzma_index, and calls
|
||||
* lzma_index_iter_rewind() on the iterator.
|
||||
*
|
||||
* This function doesn't allocate any memory, thus there is no
|
||||
* lzma_index_iter_end(). The iterator is valid as long as the
|
||||
* associated lzma_index is valid, that is, until lzma_index_end() or
|
||||
* using it as source in lzma_index_cat(). Specifically, lzma_index doesn't
|
||||
* become invalid if new Blocks are added to it with lzma_index_append() or
|
||||
* if it is used as the destination in lzma_index_cat().
|
||||
*
|
||||
* It is safe to make copies of an initialized lzma_index_iter, for example,
|
||||
* to easily restart reading at some particular position.
|
||||
*/
|
||||
extern LZMA_API(void) lzma_index_iter_init(
|
||||
lzma_index_iter *iter, const lzma_index *i) lzma_nothrow;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Rewind the iterator
|
||||
*
|
||||
* Rewind the iterator so that next call to lzma_index_iter_next() will
|
||||
* return the first Block or Stream.
|
||||
*/
|
||||
extern LZMA_API(void) lzma_index_iter_rewind(lzma_index_iter *iter)
|
||||
lzma_nothrow;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Get the next Block or Stream
|
||||
*
|
||||
* \param iter Iterator initialized with lzma_index_iter_init()
|
||||
* \param mode Specify what kind of information the caller wants
|
||||
* to get. See lzma_index_iter_mode for details.
|
||||
*
|
||||
* \return If next Block or Stream matching the mode was found, *iter
|
||||
* is updated and this function returns false. If no Block or
|
||||
* Stream matching the mode is found, *iter is not modified
|
||||
* and this function returns true. If mode is set to an unknown
|
||||
* value, *iter is not modified and this function returns true.
|
||||
*/
|
||||
extern LZMA_API(lzma_bool) lzma_index_iter_next(
|
||||
lzma_index_iter *iter, lzma_index_iter_mode mode)
|
||||
lzma_nothrow lzma_attr_warn_unused_result;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Locate a Block
|
||||
*
|
||||
* If it is possible to seek in the .xz file, it is possible to parse
|
||||
* the Index field(s) and use lzma_index_iter_locate() to do random-access
|
||||
* reading with granularity of Block size.
|
||||
*
|
||||
* \param iter Iterator that was earlier initialized with
|
||||
* lzma_index_iter_init().
|
||||
* \param target Uncompressed target offset which the caller would
|
||||
* like to locate from the Stream
|
||||
*
|
||||
* If the target is smaller than the uncompressed size of the Stream (can be
|
||||
* checked with lzma_index_uncompressed_size()):
|
||||
* - Information about the Stream and Block containing the requested
|
||||
* uncompressed offset is stored into *iter.
|
||||
* - Internal state of the iterator is adjusted so that
|
||||
* lzma_index_iter_next() can be used to read subsequent Blocks or Streams.
|
||||
* - This function returns false.
|
||||
*
|
||||
* If target is greater than the uncompressed size of the Stream, *iter
|
||||
* is not modified, and this function returns true.
|
||||
*/
|
||||
extern LZMA_API(lzma_bool) lzma_index_iter_locate(
|
||||
lzma_index_iter *iter, lzma_vli target) lzma_nothrow;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Concatenate lzma_indexes
|
||||
*
|
||||
* Concatenating lzma_indexes is useful when doing random-access reading in
|
||||
* multi-Stream .xz file, or when combining multiple Streams into single
|
||||
* Stream.
|
||||
*
|
||||
* \param dest lzma_index after which src is appended
|
||||
* \param src lzma_index to be appended after dest. If this
|
||||
* function succeeds, the memory allocated for src
|
||||
* is freed or moved to be part of dest, and all
|
||||
* iterators pointing to src will become invalid.
|
||||
* \param allocator Custom memory allocator; can be NULL to use
|
||||
* malloc() and free().
|
||||
*
|
||||
* \return - LZMA_OK: lzma_indexes were concatenated successfully.
|
||||
* src is now a dangling pointer.
|
||||
* - LZMA_DATA_ERROR: *dest would grow too big.
|
||||
* - LZMA_MEM_ERROR
|
||||
* - LZMA_PROG_ERROR
|
||||
*/
|
||||
extern LZMA_API(lzma_ret) lzma_index_cat(lzma_index *dest, lzma_index *src,
|
||||
const lzma_allocator *allocator)
|
||||
lzma_nothrow lzma_attr_warn_unused_result;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Duplicate lzma_index
|
||||
*
|
||||
* \return A copy of the lzma_index, or NULL if memory allocation failed.
|
||||
*/
|
||||
extern LZMA_API(lzma_index *) lzma_index_dup(
|
||||
const lzma_index *i, const lzma_allocator *allocator)
|
||||
lzma_nothrow lzma_attr_warn_unused_result;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Initialize .xz Index encoder
|
||||
*
|
||||
* \param strm Pointer to properly prepared lzma_stream
|
||||
* \param i Pointer to lzma_index which should be encoded.
|
||||
*
|
||||
* The valid `action' values for lzma_code() are LZMA_RUN and LZMA_FINISH.
|
||||
* It is enough to use only one of them (you can choose freely; use LZMA_RUN
|
||||
* to support liblzma versions older than 5.0.0).
|
||||
*
|
||||
* \return - LZMA_OK: Initialization succeeded, continue with lzma_code().
|
||||
* - LZMA_MEM_ERROR
|
||||
* - LZMA_PROG_ERROR
|
||||
*/
|
||||
extern LZMA_API(lzma_ret) lzma_index_encoder(
|
||||
lzma_stream *strm, const lzma_index *i)
|
||||
lzma_nothrow lzma_attr_warn_unused_result;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Initialize .xz Index decoder
|
||||
*
|
||||
* \param strm Pointer to properly prepared lzma_stream
|
||||
* \param i The decoded Index will be made available via
|
||||
* this pointer. Initially this function will
|
||||
* set *i to NULL (the old value is ignored). If
|
||||
* decoding succeeds (lzma_code() returns
|
||||
* LZMA_STREAM_END), *i will be set to point
|
||||
* to a new lzma_index, which the application
|
||||
* has to later free with lzma_index_end().
|
||||
* \param memlimit How much memory the resulting lzma_index is
|
||||
* allowed to require.
|
||||
*
|
||||
* The valid `action' values for lzma_code() are LZMA_RUN and LZMA_FINISH.
|
||||
* It is enough to use only one of them (you can choose freely; use LZMA_RUN
|
||||
* to support liblzma versions older than 5.0.0).
|
||||
*
|
||||
* \return - LZMA_OK: Initialization succeeded, continue with lzma_code().
|
||||
* - LZMA_MEM_ERROR
|
||||
* - LZMA_MEMLIMIT_ERROR
|
||||
* - LZMA_PROG_ERROR
|
||||
*/
|
||||
extern LZMA_API(lzma_ret) lzma_index_decoder(
|
||||
lzma_stream *strm, lzma_index **i, uint64_t memlimit)
|
||||
lzma_nothrow lzma_attr_warn_unused_result;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Single-call .xz Index encoder
|
||||
*
|
||||
* \param i lzma_index to be encoded
|
||||
* \param out Beginning of the output buffer
|
||||
* \param out_pos The next byte will be written to out[*out_pos].
|
||||
* *out_pos is updated only if encoding succeeds.
|
||||
* \param out_size Size of the out buffer; the first byte into
|
||||
* which no data is written to is out[out_size].
|
||||
*
|
||||
* \return - LZMA_OK: Encoding was successful.
|
||||
* - LZMA_BUF_ERROR: Output buffer is too small. Use
|
||||
* lzma_index_size() to find out how much output
|
||||
* space is needed.
|
||||
* - LZMA_PROG_ERROR
|
||||
*
|
||||
* \note This function doesn't take allocator argument since all
|
||||
* the internal data is allocated on stack.
|
||||
*/
|
||||
extern LZMA_API(lzma_ret) lzma_index_buffer_encode(const lzma_index *i,
|
||||
uint8_t *out, size_t *out_pos, size_t out_size) lzma_nothrow;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Single-call .xz Index decoder
|
||||
*
|
||||
* \param i If decoding succeeds, *i will point to a new
|
||||
* lzma_index, which the application has to
|
||||
* later free with lzma_index_end(). If an error
|
||||
* occurs, *i will be NULL. The old value of *i
|
||||
* is always ignored and thus doesn't need to be
|
||||
* initialized by the caller.
|
||||
* \param memlimit Pointer to how much memory the resulting
|
||||
* lzma_index is allowed to require. The value
|
||||
* pointed by this pointer is modified if and only
|
||||
* if LZMA_MEMLIMIT_ERROR is returned.
|
||||
* \param allocator Pointer to lzma_allocator, or NULL to use malloc()
|
||||
* \param in Beginning of the input buffer
|
||||
* \param in_pos The next byte will be read from in[*in_pos].
|
||||
* *in_pos is updated only if decoding succeeds.
|
||||
* \param in_size Size of the input buffer; the first byte that
|
||||
* won't be read is in[in_size].
|
||||
*
|
||||
* \return - LZMA_OK: Decoding was successful.
|
||||
* - LZMA_MEM_ERROR
|
||||
* - LZMA_MEMLIMIT_ERROR: Memory usage limit was reached.
|
||||
* The minimum required memlimit value was stored to *memlimit.
|
||||
* - LZMA_DATA_ERROR
|
||||
* - LZMA_PROG_ERROR
|
||||
*/
|
||||
extern LZMA_API(lzma_ret) lzma_index_buffer_decode(lzma_index **i,
|
||||
uint64_t *memlimit, const lzma_allocator *allocator,
|
||||
const uint8_t *in, size_t *in_pos, size_t in_size)
|
||||
lzma_nothrow;
|
||||
@@ -1,107 +0,0 @@
|
||||
/**
|
||||
* \file lzma/index_hash.h
|
||||
* \brief Validate Index by using a hash function
|
||||
*
|
||||
* Hashing makes it possible to use constant amount of memory to validate
|
||||
* Index of arbitrary size.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Author: Lasse Collin
|
||||
*
|
||||
* This file has been put into the public domain.
|
||||
* You can do whatever you want with this file.
|
||||
*
|
||||
* See ../lzma.h for information about liblzma as a whole.
|
||||
*/
|
||||
|
||||
#ifndef LZMA_H_INTERNAL
|
||||
# error Never include this file directly. Use <lzma.h> instead.
|
||||
#endif
|
||||
|
||||
/**
|
||||
* \brief Opaque data type to hold the Index hash
|
||||
*/
|
||||
typedef struct lzma_index_hash_s lzma_index_hash;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Allocate and initialize a new lzma_index_hash structure
|
||||
*
|
||||
* If index_hash is NULL, a new lzma_index_hash structure is allocated,
|
||||
* initialized, and a pointer to it returned. If allocation fails, NULL
|
||||
* is returned.
|
||||
*
|
||||
* If index_hash is non-NULL, it is reinitialized and the same pointer
|
||||
* returned. In this case, return value cannot be NULL or a different
|
||||
* pointer than the index_hash that was given as an argument.
|
||||
*/
|
||||
extern LZMA_API(lzma_index_hash *) lzma_index_hash_init(
|
||||
lzma_index_hash *index_hash, const lzma_allocator *allocator)
|
||||
lzma_nothrow lzma_attr_warn_unused_result;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Deallocate lzma_index_hash structure
|
||||
*/
|
||||
extern LZMA_API(void) lzma_index_hash_end(
|
||||
lzma_index_hash *index_hash, const lzma_allocator *allocator)
|
||||
lzma_nothrow;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Add a new Record to an Index hash
|
||||
*
|
||||
* \param index_hash Pointer to a lzma_index_hash structure
|
||||
* \param unpadded_size Unpadded Size of a Block
|
||||
* \param uncompressed_size Uncompressed Size of a Block
|
||||
*
|
||||
* \return - LZMA_OK
|
||||
* - LZMA_DATA_ERROR: Compressed or uncompressed size of the
|
||||
* Stream or size of the Index field would grow too big.
|
||||
* - LZMA_PROG_ERROR: Invalid arguments or this function is being
|
||||
* used when lzma_index_hash_decode() has already been used.
|
||||
*/
|
||||
extern LZMA_API(lzma_ret) lzma_index_hash_append(lzma_index_hash *index_hash,
|
||||
lzma_vli unpadded_size, lzma_vli uncompressed_size)
|
||||
lzma_nothrow lzma_attr_warn_unused_result;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Decode and validate the Index field
|
||||
*
|
||||
* After telling the sizes of all Blocks with lzma_index_hash_append(),
|
||||
* the actual Index field is decoded with this function. Specifically,
|
||||
* once decoding of the Index field has been started, no more Records
|
||||
* can be added using lzma_index_hash_append().
|
||||
*
|
||||
* This function doesn't use lzma_stream structure to pass the input data.
|
||||
* Instead, the input buffer is specified using three arguments. This is
|
||||
* because it matches better the internal APIs of liblzma.
|
||||
*
|
||||
* \param index_hash Pointer to a lzma_index_hash structure
|
||||
* \param in Pointer to the beginning of the input buffer
|
||||
* \param in_pos in[*in_pos] is the next byte to process
|
||||
* \param in_size in[in_size] is the first byte not to process
|
||||
*
|
||||
* \return - LZMA_OK: So far good, but more input is needed.
|
||||
* - LZMA_STREAM_END: Index decoded successfully and it matches
|
||||
* the Records given with lzma_index_hash_append().
|
||||
* - LZMA_DATA_ERROR: Index is corrupt or doesn't match the
|
||||
* information given with lzma_index_hash_append().
|
||||
* - LZMA_BUF_ERROR: Cannot progress because *in_pos >= in_size.
|
||||
* - LZMA_PROG_ERROR
|
||||
*/
|
||||
extern LZMA_API(lzma_ret) lzma_index_hash_decode(lzma_index_hash *index_hash,
|
||||
const uint8_t *in, size_t *in_pos, size_t in_size)
|
||||
lzma_nothrow lzma_attr_warn_unused_result;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Get the size of the Index field as bytes
|
||||
*
|
||||
* This is needed to verify the Backward Size field in the Stream Footer.
|
||||
*/
|
||||
extern LZMA_API(lzma_vli) lzma_index_hash_size(
|
||||
const lzma_index_hash *index_hash)
|
||||
lzma_nothrow lzma_attr_pure;
|
||||
@@ -1,223 +0,0 @@
|
||||
/**
|
||||
* \file lzma/stream_flags.h
|
||||
* \brief .xz Stream Header and Stream Footer encoder and decoder
|
||||
*/
|
||||
|
||||
/*
|
||||
* Author: Lasse Collin
|
||||
*
|
||||
* This file has been put into the public domain.
|
||||
* You can do whatever you want with this file.
|
||||
*
|
||||
* See ../lzma.h for information about liblzma as a whole.
|
||||
*/
|
||||
|
||||
#ifndef LZMA_H_INTERNAL
|
||||
# error Never include this file directly. Use <lzma.h> instead.
|
||||
#endif
|
||||
|
||||
|
||||
/**
|
||||
* \brief Size of Stream Header and Stream Footer
|
||||
*
|
||||
* Stream Header and Stream Footer have the same size and they are not
|
||||
* going to change even if a newer version of the .xz file format is
|
||||
* developed in future.
|
||||
*/
|
||||
#define LZMA_STREAM_HEADER_SIZE 12
|
||||
|
||||
|
||||
/**
|
||||
* \brief Options for encoding/decoding Stream Header and Stream Footer
|
||||
*/
|
||||
typedef struct {
|
||||
/**
|
||||
* \brief Stream Flags format version
|
||||
*
|
||||
* To prevent API and ABI breakages if new features are needed in
|
||||
* Stream Header or Stream Footer, a version number is used to
|
||||
* indicate which fields in this structure are in use. For now,
|
||||
* version must always be zero. With non-zero version, the
|
||||
* lzma_stream_header_encode() and lzma_stream_footer_encode()
|
||||
* will return LZMA_OPTIONS_ERROR.
|
||||
*
|
||||
* lzma_stream_header_decode() and lzma_stream_footer_decode()
|
||||
* will always set this to the lowest value that supports all the
|
||||
* features indicated by the Stream Flags field. The application
|
||||
* must check that the version number set by the decoding functions
|
||||
* is supported by the application. Otherwise it is possible that
|
||||
* the application will decode the Stream incorrectly.
|
||||
*/
|
||||
uint32_t version;
|
||||
|
||||
/**
|
||||
* \brief Backward Size
|
||||
*
|
||||
* Backward Size must be a multiple of four bytes. In this Stream
|
||||
* format version, Backward Size is the size of the Index field.
|
||||
*
|
||||
* Backward Size isn't actually part of the Stream Flags field, but
|
||||
* it is convenient to include in this structure anyway. Backward
|
||||
* Size is present only in the Stream Footer. There is no need to
|
||||
* initialize backward_size when encoding Stream Header.
|
||||
*
|
||||
* lzma_stream_header_decode() always sets backward_size to
|
||||
* LZMA_VLI_UNKNOWN so that it is convenient to use
|
||||
* lzma_stream_flags_compare() when both Stream Header and Stream
|
||||
* Footer have been decoded.
|
||||
*/
|
||||
lzma_vli backward_size;
|
||||
# define LZMA_BACKWARD_SIZE_MIN 4
|
||||
# define LZMA_BACKWARD_SIZE_MAX (LZMA_VLI_C(1) << 34)
|
||||
|
||||
/**
|
||||
* \brief Check ID
|
||||
*
|
||||
* This indicates the type of the integrity check calculated from
|
||||
* uncompressed data.
|
||||
*/
|
||||
lzma_check check;
|
||||
|
||||
/*
|
||||
* Reserved space to allow possible future extensions without
|
||||
* breaking the ABI. You should not touch these, because the
|
||||
* names of these variables may change.
|
||||
*
|
||||
* (We will never be able to use all of these since Stream Flags
|
||||
* is just two bytes plus Backward Size of four bytes. But it's
|
||||
* nice to have the proper types when they are needed.)
|
||||
*/
|
||||
lzma_reserved_enum reserved_enum1;
|
||||
lzma_reserved_enum reserved_enum2;
|
||||
lzma_reserved_enum reserved_enum3;
|
||||
lzma_reserved_enum reserved_enum4;
|
||||
lzma_bool reserved_bool1;
|
||||
lzma_bool reserved_bool2;
|
||||
lzma_bool reserved_bool3;
|
||||
lzma_bool reserved_bool4;
|
||||
lzma_bool reserved_bool5;
|
||||
lzma_bool reserved_bool6;
|
||||
lzma_bool reserved_bool7;
|
||||
lzma_bool reserved_bool8;
|
||||
uint32_t reserved_int1;
|
||||
uint32_t reserved_int2;
|
||||
|
||||
} lzma_stream_flags;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Encode Stream Header
|
||||
*
|
||||
* \param options Stream Header options to be encoded.
|
||||
* options->backward_size is ignored and doesn't
|
||||
* need to be initialized.
|
||||
* \param out Beginning of the output buffer of
|
||||
* LZMA_STREAM_HEADER_SIZE bytes.
|
||||
*
|
||||
* \return - LZMA_OK: Encoding was successful.
|
||||
* - LZMA_OPTIONS_ERROR: options->version is not supported by
|
||||
* this liblzma version.
|
||||
* - LZMA_PROG_ERROR: Invalid options.
|
||||
*/
|
||||
extern LZMA_API(lzma_ret) lzma_stream_header_encode(
|
||||
const lzma_stream_flags *options, uint8_t *out)
|
||||
lzma_nothrow lzma_attr_warn_unused_result;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Encode Stream Footer
|
||||
*
|
||||
* \param options Stream Footer options to be encoded.
|
||||
* \param out Beginning of the output buffer of
|
||||
* LZMA_STREAM_HEADER_SIZE bytes.
|
||||
*
|
||||
* \return - LZMA_OK: Encoding was successful.
|
||||
* - LZMA_OPTIONS_ERROR: options->version is not supported by
|
||||
* this liblzma version.
|
||||
* - LZMA_PROG_ERROR: Invalid options.
|
||||
*/
|
||||
extern LZMA_API(lzma_ret) lzma_stream_footer_encode(
|
||||
const lzma_stream_flags *options, uint8_t *out)
|
||||
lzma_nothrow lzma_attr_warn_unused_result;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Decode Stream Header
|
||||
*
|
||||
* \param options Target for the decoded Stream Header options.
|
||||
* \param in Beginning of the input buffer of
|
||||
* LZMA_STREAM_HEADER_SIZE bytes.
|
||||
*
|
||||
* options->backward_size is always set to LZMA_VLI_UNKNOWN. This is to
|
||||
* help comparing Stream Flags from Stream Header and Stream Footer with
|
||||
* lzma_stream_flags_compare().
|
||||
*
|
||||
* \return - LZMA_OK: Decoding was successful.
|
||||
* - LZMA_FORMAT_ERROR: Magic bytes don't match, thus the given
|
||||
* buffer cannot be Stream Header.
|
||||
* - LZMA_DATA_ERROR: CRC32 doesn't match, thus the header
|
||||
* is corrupt.
|
||||
* - LZMA_OPTIONS_ERROR: Unsupported options are present
|
||||
* in the header.
|
||||
*
|
||||
* \note When decoding .xz files that contain multiple Streams, it may
|
||||
* make sense to print "file format not recognized" only if
|
||||
* decoding of the Stream Header of the _first_ Stream gives
|
||||
* LZMA_FORMAT_ERROR. If non-first Stream Header gives
|
||||
* LZMA_FORMAT_ERROR, the message used for LZMA_DATA_ERROR is
|
||||
* probably more appropriate.
|
||||
*
|
||||
* For example, Stream decoder in liblzma uses LZMA_DATA_ERROR if
|
||||
* LZMA_FORMAT_ERROR is returned by lzma_stream_header_decode()
|
||||
* when decoding non-first Stream.
|
||||
*/
|
||||
extern LZMA_API(lzma_ret) lzma_stream_header_decode(
|
||||
lzma_stream_flags *options, const uint8_t *in)
|
||||
lzma_nothrow lzma_attr_warn_unused_result;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Decode Stream Footer
|
||||
*
|
||||
* \param options Target for the decoded Stream Header options.
|
||||
* \param in Beginning of the input buffer of
|
||||
* LZMA_STREAM_HEADER_SIZE bytes.
|
||||
*
|
||||
* \return - LZMA_OK: Decoding was successful.
|
||||
* - LZMA_FORMAT_ERROR: Magic bytes don't match, thus the given
|
||||
* buffer cannot be Stream Footer.
|
||||
* - LZMA_DATA_ERROR: CRC32 doesn't match, thus the Stream Footer
|
||||
* is corrupt.
|
||||
* - LZMA_OPTIONS_ERROR: Unsupported options are present
|
||||
* in Stream Footer.
|
||||
*
|
||||
* \note If Stream Header was already decoded successfully, but
|
||||
* decoding Stream Footer returns LZMA_FORMAT_ERROR, the
|
||||
* application should probably report some other error message
|
||||
* than "file format not recognized", since the file more likely
|
||||
* is corrupt (possibly truncated). Stream decoder in liblzma
|
||||
* uses LZMA_DATA_ERROR in this situation.
|
||||
*/
|
||||
extern LZMA_API(lzma_ret) lzma_stream_footer_decode(
|
||||
lzma_stream_flags *options, const uint8_t *in)
|
||||
lzma_nothrow lzma_attr_warn_unused_result;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Compare two lzma_stream_flags structures
|
||||
*
|
||||
* backward_size values are compared only if both are not
|
||||
* LZMA_VLI_UNKNOWN.
|
||||
*
|
||||
* \return - LZMA_OK: Both are equal. If either had backward_size set
|
||||
* to LZMA_VLI_UNKNOWN, backward_size values were not
|
||||
* compared or validated.
|
||||
* - LZMA_DATA_ERROR: The structures differ.
|
||||
* - LZMA_OPTIONS_ERROR: version in either structure is greater
|
||||
* than the maximum supported version (currently zero).
|
||||
* - LZMA_PROG_ERROR: Invalid value, e.g. invalid check or
|
||||
* backward_size.
|
||||
*/
|
||||
extern LZMA_API(lzma_ret) lzma_stream_flags_compare(
|
||||
const lzma_stream_flags *a, const lzma_stream_flags *b)
|
||||
lzma_nothrow lzma_attr_pure;
|
||||
@@ -22,7 +22,7 @@
|
||||
*/
|
||||
#define LZMA_VERSION_MAJOR 5
|
||||
#define LZMA_VERSION_MINOR 2
|
||||
#define LZMA_VERSION_PATCH 2
|
||||
#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:
|
||||
|
||||
+25
-15
@@ -15,7 +15,18 @@
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#if defined(HAVE_COMMONCRYPTO_COMMONDIGEST_H)
|
||||
// If the function for external SHA-256 is missing, use the internal SHA-256
|
||||
// code. Due to how configure works, these defines can only get defined when
|
||||
// both a usable header and a type have already been found.
|
||||
#if !(defined(HAVE_CC_SHA256_INIT) \
|
||||
|| defined(HAVE_SHA256_INIT) \
|
||||
|| defined(HAVE_SHA256INIT))
|
||||
# define HAVE_INTERNAL_SHA256 1
|
||||
#endif
|
||||
|
||||
#if defined(HAVE_INTERNAL_SHA256)
|
||||
// Nothing
|
||||
#elif defined(HAVE_COMMONCRYPTO_COMMONDIGEST_H)
|
||||
# include <CommonCrypto/CommonDigest.h>
|
||||
#elif defined(HAVE_SHA256_H)
|
||||
# include <sys/types.h>
|
||||
@@ -23,18 +34,9 @@
|
||||
#elif defined(HAVE_SHA2_H)
|
||||
# include <sys/types.h>
|
||||
# include <sha2.h>
|
||||
#elif defined(HAVE_MINIX_SHA2_H)
|
||||
# include <sys/types.h>
|
||||
# include <minix/sha2.h>
|
||||
#endif
|
||||
|
||||
#if defined(HAVE_CC_SHA256_CTX)
|
||||
typedef CC_SHA256_CTX lzma_sha256_state;
|
||||
#elif defined(HAVE_SHA256_CTX)
|
||||
typedef SHA256_CTX lzma_sha256_state;
|
||||
#elif defined(HAVE_SHA2_CTX)
|
||||
typedef SHA2_CTX lzma_sha256_state;
|
||||
#else
|
||||
#if defined(HAVE_INTERNAL_SHA256)
|
||||
/// State for the internal SHA-256 implementation
|
||||
typedef struct {
|
||||
/// Internal state
|
||||
@@ -43,9 +45,17 @@ typedef struct {
|
||||
/// Size of the message excluding padding
|
||||
uint64_t size;
|
||||
} lzma_sha256_state;
|
||||
#elif defined(HAVE_CC_SHA256_CTX)
|
||||
typedef CC_SHA256_CTX lzma_sha256_state;
|
||||
#elif defined(HAVE_SHA256_CTX)
|
||||
typedef SHA256_CTX lzma_sha256_state;
|
||||
#elif defined(HAVE_SHA2_CTX)
|
||||
typedef SHA2_CTX lzma_sha256_state;
|
||||
#endif
|
||||
|
||||
#if defined(HAVE_CC_SHA256_INIT)
|
||||
#if defined(HAVE_INTERNAL_SHA256)
|
||||
// Nothing
|
||||
#elif defined(HAVE_CC_SHA256_INIT)
|
||||
# define LZMA_SHA256FUNC(x) CC_SHA256_ ## x
|
||||
#elif defined(HAVE_SHA256_INIT)
|
||||
# define LZMA_SHA256FUNC(x) SHA256_ ## x
|
||||
@@ -99,9 +109,9 @@ extern const uint64_t lzma_crc64_table[4][256];
|
||||
|
||||
/// \brief Initialize *check depending on type
|
||||
///
|
||||
/// \param check LZMA_OK on success. LZMA_UNSUPPORTED_CHECK if the type
|
||||
/// is not supported by the current version or build of
|
||||
/// liblzma. LZMA_PROG_ERROR if type > LZMA_CHECK_ID_MAX.
|
||||
/// \return LZMA_OK on success. LZMA_UNSUPPORTED_CHECK if the type is not
|
||||
/// supported by the current version or build of liblzma.
|
||||
/// LZMA_PROG_ERROR if type > LZMA_CHECK_ID_MAX.
|
||||
extern void lzma_check_init(lzma_check_state *check, lzma_check type);
|
||||
|
||||
/// Update the check state
|
||||
|
||||
@@ -1,61 +0,0 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
/// \file crc32_small.c
|
||||
/// \brief CRC32 calculation (size-optimized)
|
||||
//
|
||||
// Author: Lasse Collin
|
||||
//
|
||||
// This file has been put into the public domain.
|
||||
// You can do whatever you want with this file.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "check.h"
|
||||
|
||||
|
||||
uint32_t lzma_crc32_table[1][256];
|
||||
|
||||
|
||||
static void
|
||||
crc32_init(void)
|
||||
{
|
||||
static const uint32_t poly32 = UINT32_C(0xEDB88320);
|
||||
|
||||
for (size_t b = 0; b < 256; ++b) {
|
||||
uint32_t r = b;
|
||||
for (size_t i = 0; i < 8; ++i) {
|
||||
if (r & 1)
|
||||
r = (r >> 1) ^ poly32;
|
||||
else
|
||||
r >>= 1;
|
||||
}
|
||||
|
||||
lzma_crc32_table[0][b] = r;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
extern void
|
||||
lzma_crc32_init(void)
|
||||
{
|
||||
mythread_once(crc32_init);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
extern LZMA_API(uint32_t)
|
||||
lzma_crc32(const uint8_t *buf, size_t size, uint32_t crc)
|
||||
{
|
||||
lzma_crc32_init();
|
||||
|
||||
crc = ~crc;
|
||||
|
||||
while (size != 0) {
|
||||
crc = lzma_crc32_table[0][*buf++ ^ (crc & 0xFF)] ^ (crc >> 8);
|
||||
--size;
|
||||
}
|
||||
|
||||
return ~crc;
|
||||
}
|
||||
@@ -15,7 +15,7 @@
|
||||
#include "lz_decoder.h"
|
||||
|
||||
|
||||
struct lzma_coder_s {
|
||||
typedef struct {
|
||||
lzma_next_coder next;
|
||||
|
||||
enum {
|
||||
@@ -46,17 +46,19 @@ struct lzma_coder_s {
|
||||
/// Options decoded from the header needed to initialize
|
||||
/// the LZMA decoder
|
||||
lzma_options_lzma options;
|
||||
};
|
||||
} lzma_alone_coder;
|
||||
|
||||
|
||||
static lzma_ret
|
||||
alone_decode(lzma_coder *coder,
|
||||
alone_decode(void *coder_ptr,
|
||||
const lzma_allocator *allocator lzma_attribute((__unused__)),
|
||||
const uint8_t *restrict in, size_t *restrict in_pos,
|
||||
size_t in_size, uint8_t *restrict out,
|
||||
size_t *restrict out_pos, size_t out_size,
|
||||
lzma_action action)
|
||||
{
|
||||
lzma_alone_coder *coder = coder_ptr;
|
||||
|
||||
while (*out_pos < out_size
|
||||
&& (coder->sequence == SEQ_CODE || *in_pos < in_size))
|
||||
switch (coder->sequence) {
|
||||
@@ -96,13 +98,13 @@ alone_decode(lzma_coder *coder,
|
||||
|
||||
coder->uncompressed_size = LZMA_VLI_UNKNOWN;
|
||||
|
||||
// Calculate the memory usage so that it is ready
|
||||
// for SEQ_CODER_INIT.
|
||||
coder->memusage = lzma_lzma_decoder_memusage(&coder->options)
|
||||
+ LZMA_MEMUSAGE_BASE;
|
||||
// Calculate the memory usage so that it is ready
|
||||
// for SEQ_CODER_INIT.
|
||||
coder->memusage = lzma_lzma_decoder_memusage(&coder->options)
|
||||
+ LZMA_MEMUSAGE_BASE;
|
||||
|
||||
coder->pos = 0;
|
||||
coder->sequence = SEQ_CODER_INIT;
|
||||
coder->pos = 0;
|
||||
coder->sequence = SEQ_CODER_INIT;
|
||||
|
||||
// Fall through
|
||||
|
||||
@@ -147,8 +149,9 @@ alone_decode(lzma_coder *coder,
|
||||
|
||||
|
||||
static void
|
||||
alone_decoder_end(lzma_coder *coder, const lzma_allocator *allocator)
|
||||
alone_decoder_end(void *coder_ptr, const lzma_allocator *allocator)
|
||||
{
|
||||
lzma_alone_coder *coder = coder_ptr;
|
||||
lzma_next_end(&coder->next, allocator);
|
||||
lzma_free(coder, allocator);
|
||||
return;
|
||||
@@ -156,9 +159,11 @@ alone_decoder_end(lzma_coder *coder, const lzma_allocator *allocator)
|
||||
|
||||
|
||||
static lzma_ret
|
||||
alone_decoder_memconfig(lzma_coder *coder, uint64_t *memusage,
|
||||
alone_decoder_memconfig(void *coder_ptr, uint64_t *memusage,
|
||||
uint64_t *old_memlimit, uint64_t new_memlimit)
|
||||
{
|
||||
lzma_alone_coder *coder = coder_ptr;
|
||||
|
||||
*memusage = coder->memusage;
|
||||
*old_memlimit = coder->memlimit;
|
||||
|
||||
@@ -179,29 +184,29 @@ 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 (next->coder == NULL) {
|
||||
next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
|
||||
if (next->coder == NULL)
|
||||
if (coder == NULL) {
|
||||
coder = lzma_alloc(sizeof(lzma_alone_coder), allocator);
|
||||
if (coder == NULL)
|
||||
return LZMA_MEM_ERROR;
|
||||
|
||||
next->coder = coder;
|
||||
next->code = &alone_decode;
|
||||
next->end = &alone_decoder_end;
|
||||
next->memconfig = &alone_decoder_memconfig;
|
||||
next->coder->next = LZMA_NEXT_CODER_INIT;
|
||||
coder->next = LZMA_NEXT_CODER_INIT;
|
||||
}
|
||||
|
||||
next->coder->sequence = SEQ_PROPERTIES;
|
||||
next->coder->picky = picky;
|
||||
next->coder->pos = 0;
|
||||
next->coder->options.dict_size = 0;
|
||||
next->coder->options.preset_dict = NULL;
|
||||
next->coder->options.preset_dict_size = 0;
|
||||
next->coder->uncompressed_size = 0;
|
||||
next->coder->memlimit = memlimit;
|
||||
next->coder->memusage = LZMA_MEMUSAGE_BASE;
|
||||
coder->sequence = SEQ_PROPERTIES;
|
||||
coder->picky = picky;
|
||||
coder->pos = 0;
|
||||
coder->options.dict_size = 0;
|
||||
coder->options.preset_dict = NULL;
|
||||
coder->options.preset_dict_size = 0;
|
||||
coder->uncompressed_size = 0;
|
||||
coder->memlimit = my_max(1, memlimit);
|
||||
coder->memusage = LZMA_MEMUSAGE_BASE;
|
||||
|
||||
return LZMA_OK;
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
#define ALONE_HEADER_SIZE (1 + 4)
|
||||
|
||||
|
||||
struct lzma_coder_s {
|
||||
typedef struct {
|
||||
lzma_next_coder next;
|
||||
|
||||
enum {
|
||||
@@ -27,17 +27,19 @@ struct lzma_coder_s {
|
||||
|
||||
size_t header_pos;
|
||||
uint8_t header[ALONE_HEADER_SIZE];
|
||||
};
|
||||
} lzma_alone_coder;
|
||||
|
||||
|
||||
static lzma_ret
|
||||
alone_encode(lzma_coder *coder,
|
||||
alone_encode(void *coder_ptr,
|
||||
const lzma_allocator *allocator lzma_attribute((__unused__)),
|
||||
const uint8_t *restrict in, size_t *restrict in_pos,
|
||||
size_t in_size, uint8_t *restrict out,
|
||||
size_t *restrict out_pos, size_t out_size,
|
||||
lzma_action action)
|
||||
{
|
||||
lzma_alone_coder *coder = coder_ptr;
|
||||
|
||||
while (*out_pos < out_size)
|
||||
switch (coder->sequence) {
|
||||
case SEQ_HEADER:
|
||||
@@ -65,8 +67,9 @@ alone_encode(lzma_coder *coder,
|
||||
|
||||
|
||||
static void
|
||||
alone_encoder_end(lzma_coder *coder, const lzma_allocator *allocator)
|
||||
alone_encoder_end(void *coder_ptr, const lzma_allocator *allocator)
|
||||
{
|
||||
lzma_alone_coder *coder = coder_ptr;
|
||||
lzma_next_end(&coder->next, allocator);
|
||||
lzma_free(coder, allocator);
|
||||
return;
|
||||
@@ -80,23 +83,26 @@ alone_encoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
|
||||
{
|
||||
lzma_next_coder_init(&alone_encoder_init, next, allocator);
|
||||
|
||||
if (next->coder == NULL) {
|
||||
next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
|
||||
if (next->coder == NULL)
|
||||
lzma_alone_coder *coder = next->coder;
|
||||
|
||||
if (coder == NULL) {
|
||||
coder = lzma_alloc(sizeof(lzma_alone_coder), allocator);
|
||||
if (coder == NULL)
|
||||
return LZMA_MEM_ERROR;
|
||||
|
||||
next->coder = coder;
|
||||
next->code = &alone_encode;
|
||||
next->end = &alone_encoder_end;
|
||||
next->coder->next = LZMA_NEXT_CODER_INIT;
|
||||
coder->next = LZMA_NEXT_CODER_INIT;
|
||||
}
|
||||
|
||||
// Basic initializations
|
||||
next->coder->sequence = SEQ_HEADER;
|
||||
next->coder->header_pos = 0;
|
||||
coder->sequence = SEQ_HEADER;
|
||||
coder->header_pos = 0;
|
||||
|
||||
// Encode the header:
|
||||
// - Properties (1 byte)
|
||||
if (lzma_lzma_lclppb_encode(options, next->coder->header))
|
||||
if (lzma_lzma_lclppb_encode(options, coder->header))
|
||||
return LZMA_OPTIONS_ERROR;
|
||||
|
||||
// - Dictionary size (4 bytes)
|
||||
@@ -116,7 +122,7 @@ alone_encoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
|
||||
if (d != UINT32_MAX)
|
||||
++d;
|
||||
|
||||
unaligned_write32le(next->coder->header + 1, d);
|
||||
unaligned_write32le(coder->header + 1, d);
|
||||
|
||||
// Initialize the LZMA encoder.
|
||||
const lzma_filter_info filters[2] = {
|
||||
@@ -128,7 +134,7 @@ alone_encoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
|
||||
}
|
||||
};
|
||||
|
||||
return lzma_next_filter_init(&next->coder->next, allocator, filters);
|
||||
return lzma_next_filter_init(&coder->next, allocator, filters);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,186 +0,0 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
/// \file auto_decoder.c
|
||||
/// \brief Autodetect between .xz Stream and .lzma (LZMA_Alone) formats
|
||||
//
|
||||
// Author: Lasse Collin
|
||||
//
|
||||
// This file has been put into the public domain.
|
||||
// You can do whatever you want with this file.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "stream_decoder.h"
|
||||
#include "alone_decoder.h"
|
||||
|
||||
|
||||
struct lzma_coder_s {
|
||||
/// Stream decoder or LZMA_Alone decoder
|
||||
lzma_next_coder next;
|
||||
|
||||
uint64_t memlimit;
|
||||
uint32_t flags;
|
||||
|
||||
enum {
|
||||
SEQ_INIT,
|
||||
SEQ_CODE,
|
||||
SEQ_FINISH,
|
||||
} sequence;
|
||||
};
|
||||
|
||||
|
||||
static lzma_ret
|
||||
auto_decode(lzma_coder *coder, const lzma_allocator *allocator,
|
||||
const uint8_t *restrict in, size_t *restrict in_pos,
|
||||
size_t in_size, uint8_t *restrict out,
|
||||
size_t *restrict out_pos, size_t out_size, lzma_action action)
|
||||
{
|
||||
switch (coder->sequence) {
|
||||
case SEQ_INIT:
|
||||
if (*in_pos >= in_size)
|
||||
return LZMA_OK;
|
||||
|
||||
// Update the sequence now, because we want to continue from
|
||||
// SEQ_CODE even if we return some LZMA_*_CHECK.
|
||||
coder->sequence = SEQ_CODE;
|
||||
|
||||
// Detect the file format. For now this is simple, since if
|
||||
// it doesn't start with 0xFD (the first magic byte of the
|
||||
// new format), it has to be LZMA_Alone, or something that
|
||||
// we don't support at all.
|
||||
if (in[*in_pos] == 0xFD) {
|
||||
return_if_error(lzma_stream_decoder_init(
|
||||
&coder->next, allocator,
|
||||
coder->memlimit, coder->flags));
|
||||
} else {
|
||||
return_if_error(lzma_alone_decoder_init(&coder->next,
|
||||
allocator, coder->memlimit, true));
|
||||
|
||||
// If the application wants to know about missing
|
||||
// integrity check or about the check in general, we
|
||||
// need to handle it here, because LZMA_Alone decoder
|
||||
// doesn't accept any flags.
|
||||
if (coder->flags & LZMA_TELL_NO_CHECK)
|
||||
return LZMA_NO_CHECK;
|
||||
|
||||
if (coder->flags & LZMA_TELL_ANY_CHECK)
|
||||
return LZMA_GET_CHECK;
|
||||
}
|
||||
|
||||
// Fall through
|
||||
|
||||
case SEQ_CODE: {
|
||||
const lzma_ret ret = coder->next.code(
|
||||
coder->next.coder, allocator,
|
||||
in, in_pos, in_size,
|
||||
out, out_pos, out_size, action);
|
||||
if (ret != LZMA_STREAM_END
|
||||
|| (coder->flags & LZMA_CONCATENATED) == 0)
|
||||
return ret;
|
||||
|
||||
coder->sequence = SEQ_FINISH;
|
||||
}
|
||||
|
||||
// Fall through
|
||||
|
||||
case SEQ_FINISH:
|
||||
// When LZMA_DECODE_CONCATENATED was used and we were decoding
|
||||
// LZMA_Alone file, we need to check check that there is no
|
||||
// trailing garbage and wait for LZMA_FINISH.
|
||||
if (*in_pos < in_size)
|
||||
return LZMA_DATA_ERROR;
|
||||
|
||||
return action == LZMA_FINISH ? LZMA_STREAM_END : LZMA_OK;
|
||||
|
||||
default:
|
||||
assert(0);
|
||||
return LZMA_PROG_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
auto_decoder_end(lzma_coder *coder, const lzma_allocator *allocator)
|
||||
{
|
||||
lzma_next_end(&coder->next, allocator);
|
||||
lzma_free(coder, allocator);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
static lzma_check
|
||||
auto_decoder_get_check(const lzma_coder *coder)
|
||||
{
|
||||
// It is LZMA_Alone if get_check is NULL.
|
||||
return coder->next.get_check == NULL ? LZMA_CHECK_NONE
|
||||
: coder->next.get_check(coder->next.coder);
|
||||
}
|
||||
|
||||
|
||||
static lzma_ret
|
||||
auto_decoder_memconfig(lzma_coder *coder, uint64_t *memusage,
|
||||
uint64_t *old_memlimit, uint64_t new_memlimit)
|
||||
{
|
||||
lzma_ret ret;
|
||||
|
||||
if (coder->next.memconfig != NULL) {
|
||||
ret = coder->next.memconfig(coder->next.coder,
|
||||
memusage, old_memlimit, new_memlimit);
|
||||
assert(*old_memlimit == coder->memlimit);
|
||||
} else {
|
||||
// No coder is configured yet. Use the base value as
|
||||
// the current memory usage.
|
||||
*memusage = LZMA_MEMUSAGE_BASE;
|
||||
*old_memlimit = coder->memlimit;
|
||||
ret = LZMA_OK;
|
||||
}
|
||||
|
||||
if (ret == LZMA_OK && new_memlimit != 0)
|
||||
coder->memlimit = new_memlimit;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
static lzma_ret
|
||||
auto_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
|
||||
uint64_t memlimit, uint32_t flags)
|
||||
{
|
||||
lzma_next_coder_init(&auto_decoder_init, next, allocator);
|
||||
|
||||
if (memlimit == 0)
|
||||
return LZMA_PROG_ERROR;
|
||||
|
||||
if (flags & ~LZMA_SUPPORTED_FLAGS)
|
||||
return LZMA_OPTIONS_ERROR;
|
||||
|
||||
if (next->coder == NULL) {
|
||||
next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
|
||||
if (next->coder == NULL)
|
||||
return LZMA_MEM_ERROR;
|
||||
|
||||
next->code = &auto_decode;
|
||||
next->end = &auto_decoder_end;
|
||||
next->get_check = &auto_decoder_get_check;
|
||||
next->memconfig = &auto_decoder_memconfig;
|
||||
next->coder->next = LZMA_NEXT_CODER_INIT;
|
||||
}
|
||||
|
||||
next->coder->memlimit = memlimit;
|
||||
next->coder->flags = flags;
|
||||
next->coder->sequence = SEQ_INIT;
|
||||
|
||||
return LZMA_OK;
|
||||
}
|
||||
|
||||
|
||||
extern LZMA_API(lzma_ret)
|
||||
lzma_auto_decoder(lzma_stream *strm, uint64_t memlimit, uint32_t flags)
|
||||
{
|
||||
lzma_next_strm_init(auto_decoder_init, strm, memlimit, flags);
|
||||
|
||||
strm->internal->supported_actions[LZMA_RUN] = true;
|
||||
strm->internal->supported_actions[LZMA_FINISH] = true;
|
||||
|
||||
return LZMA_OK;
|
||||
}
|
||||
@@ -1,80 +0,0 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
/// \file block_buffer_decoder.c
|
||||
/// \brief Single-call .xz Block decoder
|
||||
//
|
||||
// Author: Lasse Collin
|
||||
//
|
||||
// This file has been put into the public domain.
|
||||
// You can do whatever you want with this file.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "block_decoder.h"
|
||||
|
||||
|
||||
extern LZMA_API(lzma_ret)
|
||||
lzma_block_buffer_decode(lzma_block *block, const lzma_allocator *allocator,
|
||||
const uint8_t *in, size_t *in_pos, size_t in_size,
|
||||
uint8_t *out, size_t *out_pos, size_t out_size)
|
||||
{
|
||||
if (in_pos == NULL || (in == NULL && *in_pos != in_size)
|
||||
|| *in_pos > in_size || out_pos == NULL
|
||||
|| (out == NULL && *out_pos != out_size)
|
||||
|| *out_pos > out_size)
|
||||
return LZMA_PROG_ERROR;
|
||||
|
||||
// Initialize the Block decoder.
|
||||
lzma_next_coder block_decoder = LZMA_NEXT_CODER_INIT;
|
||||
lzma_ret ret = lzma_block_decoder_init(
|
||||
&block_decoder, allocator, block);
|
||||
|
||||
if (ret == LZMA_OK) {
|
||||
// Save the positions so that we can restore them in case
|
||||
// an error occurs.
|
||||
const size_t in_start = *in_pos;
|
||||
const size_t out_start = *out_pos;
|
||||
|
||||
// Do the actual decoding.
|
||||
ret = block_decoder.code(block_decoder.coder, allocator,
|
||||
in, in_pos, in_size, out, out_pos, out_size,
|
||||
LZMA_FINISH);
|
||||
|
||||
if (ret == LZMA_STREAM_END) {
|
||||
ret = LZMA_OK;
|
||||
} else {
|
||||
if (ret == LZMA_OK) {
|
||||
// Either the input was truncated or the
|
||||
// output buffer was too small.
|
||||
assert(*in_pos == in_size
|
||||
|| *out_pos == out_size);
|
||||
|
||||
// If all the input was consumed, then the
|
||||
// input is truncated, even if the output
|
||||
// buffer is also full. This is because
|
||||
// processing the last byte of the Block
|
||||
// never produces output.
|
||||
//
|
||||
// NOTE: This assumption may break when new
|
||||
// filters are added, if the end marker of
|
||||
// the filter doesn't consume at least one
|
||||
// complete byte.
|
||||
if (*in_pos == in_size)
|
||||
ret = LZMA_DATA_ERROR;
|
||||
else
|
||||
ret = LZMA_BUF_ERROR;
|
||||
}
|
||||
|
||||
// Restore the positions.
|
||||
*in_pos = in_start;
|
||||
*out_pos = out_start;
|
||||
}
|
||||
}
|
||||
|
||||
// Free the decoder memory. This needs to be done even if
|
||||
// initialization fails, because the internal API doesn't
|
||||
// require the initialization function to free its memory on error.
|
||||
lzma_next_end(&block_decoder, allocator);
|
||||
|
||||
return ret;
|
||||
}
|
||||
@@ -1,337 +0,0 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
/// \file block_buffer_encoder.c
|
||||
/// \brief Single-call .xz Block encoder
|
||||
//
|
||||
// Author: Lasse Collin
|
||||
//
|
||||
// This file has been put into the public domain.
|
||||
// You can do whatever you want with this file.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "block_buffer_encoder.h"
|
||||
#include "block_encoder.h"
|
||||
#include "filter_encoder.h"
|
||||
#include "lzma2_encoder.h"
|
||||
#include "check.h"
|
||||
|
||||
|
||||
/// Estimate the maximum size of the Block Header and Check fields for
|
||||
/// a Block that uses LZMA2 uncompressed chunks. We could use
|
||||
/// lzma_block_header_size() but this is simpler.
|
||||
///
|
||||
/// Block Header Size + Block Flags + Compressed Size
|
||||
/// + Uncompressed Size + Filter Flags for LZMA2 + CRC32 + Check
|
||||
/// and round up to the next multiple of four to take Header Padding
|
||||
/// into account.
|
||||
#define HEADERS_BOUND ((1 + 1 + 2 * LZMA_VLI_BYTES_MAX + 3 + 4 \
|
||||
+ LZMA_CHECK_SIZE_MAX + 3) & ~3)
|
||||
|
||||
|
||||
static uint64_t
|
||||
lzma2_bound(uint64_t uncompressed_size)
|
||||
{
|
||||
// Prevent integer overflow in overhead calculation.
|
||||
if (uncompressed_size > COMPRESSED_SIZE_MAX)
|
||||
return 0;
|
||||
|
||||
// Calculate the exact overhead of the LZMA2 headers: Round
|
||||
// uncompressed_size up to the next multiple of LZMA2_CHUNK_MAX,
|
||||
// multiply by the size of per-chunk header, and add one byte for
|
||||
// the end marker.
|
||||
const uint64_t overhead = ((uncompressed_size + LZMA2_CHUNK_MAX - 1)
|
||||
/ LZMA2_CHUNK_MAX)
|
||||
* LZMA2_HEADER_UNCOMPRESSED + 1;
|
||||
|
||||
// Catch the possible integer overflow.
|
||||
if (COMPRESSED_SIZE_MAX - overhead < uncompressed_size)
|
||||
return 0;
|
||||
|
||||
return uncompressed_size + overhead;
|
||||
}
|
||||
|
||||
|
||||
extern uint64_t
|
||||
lzma_block_buffer_bound64(uint64_t uncompressed_size)
|
||||
{
|
||||
// If the data doesn't compress, we always use uncompressed
|
||||
// LZMA2 chunks.
|
||||
uint64_t lzma2_size = lzma2_bound(uncompressed_size);
|
||||
if (lzma2_size == 0)
|
||||
return 0;
|
||||
|
||||
// Take Block Padding into account.
|
||||
lzma2_size = (lzma2_size + 3) & ~UINT64_C(3);
|
||||
|
||||
// No risk of integer overflow because lzma2_bound() already takes
|
||||
// into account the size of the headers in the Block.
|
||||
return HEADERS_BOUND + lzma2_size;
|
||||
}
|
||||
|
||||
|
||||
extern LZMA_API(size_t)
|
||||
lzma_block_buffer_bound(size_t uncompressed_size)
|
||||
{
|
||||
uint64_t ret = lzma_block_buffer_bound64(uncompressed_size);
|
||||
|
||||
#if SIZE_MAX < UINT64_MAX
|
||||
// Catch the possible integer overflow on 32-bit systems.
|
||||
if (ret > SIZE_MAX)
|
||||
return 0;
|
||||
#endif
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
static lzma_ret
|
||||
block_encode_uncompressed(lzma_block *block, const uint8_t *in, size_t in_size,
|
||||
uint8_t *out, size_t *out_pos, size_t out_size)
|
||||
{
|
||||
// Use LZMA2 uncompressed chunks. We wouldn't need a dictionary at
|
||||
// all, but LZMA2 always requires a dictionary, so use the minimum
|
||||
// value to minimize memory usage of the decoder.
|
||||
lzma_options_lzma lzma2 = {
|
||||
.dict_size = LZMA_DICT_SIZE_MIN,
|
||||
};
|
||||
|
||||
lzma_filter filters[2];
|
||||
filters[0].id = LZMA_FILTER_LZMA2;
|
||||
filters[0].options = &lzma2;
|
||||
filters[1].id = LZMA_VLI_UNKNOWN;
|
||||
|
||||
// Set the above filter options to *block temporarily so that we can
|
||||
// encode the Block Header.
|
||||
lzma_filter *filters_orig = block->filters;
|
||||
block->filters = filters;
|
||||
|
||||
if (lzma_block_header_size(block) != LZMA_OK) {
|
||||
block->filters = filters_orig;
|
||||
return LZMA_PROG_ERROR;
|
||||
}
|
||||
|
||||
// Check that there's enough output space. The caller has already
|
||||
// set block->compressed_size to what lzma2_bound() has returned,
|
||||
// so we can reuse that value. We know that compressed_size is a
|
||||
// known valid VLI and header_size is a small value so their sum
|
||||
// will never overflow.
|
||||
assert(block->compressed_size == lzma2_bound(in_size));
|
||||
if (out_size - *out_pos
|
||||
< block->header_size + block->compressed_size) {
|
||||
block->filters = filters_orig;
|
||||
return LZMA_BUF_ERROR;
|
||||
}
|
||||
|
||||
if (lzma_block_header_encode(block, out + *out_pos) != LZMA_OK) {
|
||||
block->filters = filters_orig;
|
||||
return LZMA_PROG_ERROR;
|
||||
}
|
||||
|
||||
block->filters = filters_orig;
|
||||
*out_pos += block->header_size;
|
||||
|
||||
// Encode the data using LZMA2 uncompressed chunks.
|
||||
size_t in_pos = 0;
|
||||
uint8_t control = 0x01; // Dictionary reset
|
||||
|
||||
while (in_pos < in_size) {
|
||||
// Control byte: Indicate uncompressed chunk, of which
|
||||
// the first resets the dictionary.
|
||||
out[(*out_pos)++] = control;
|
||||
control = 0x02; // No dictionary reset
|
||||
|
||||
// Size of the uncompressed chunk
|
||||
const size_t copy_size
|
||||
= my_min(in_size - in_pos, LZMA2_CHUNK_MAX);
|
||||
out[(*out_pos)++] = (copy_size - 1) >> 8;
|
||||
out[(*out_pos)++] = (copy_size - 1) & 0xFF;
|
||||
|
||||
// The actual data
|
||||
assert(*out_pos + copy_size <= out_size);
|
||||
memcpy(out + *out_pos, in + in_pos, copy_size);
|
||||
|
||||
in_pos += copy_size;
|
||||
*out_pos += copy_size;
|
||||
}
|
||||
|
||||
// End marker
|
||||
out[(*out_pos)++] = 0x00;
|
||||
assert(*out_pos <= out_size);
|
||||
|
||||
return LZMA_OK;
|
||||
}
|
||||
|
||||
|
||||
static lzma_ret
|
||||
block_encode_normal(lzma_block *block, const lzma_allocator *allocator,
|
||||
const uint8_t *in, size_t in_size,
|
||||
uint8_t *out, size_t *out_pos, size_t out_size)
|
||||
{
|
||||
// Find out the size of the Block Header.
|
||||
return_if_error(lzma_block_header_size(block));
|
||||
|
||||
// Reserve space for the Block Header and skip it for now.
|
||||
if (out_size - *out_pos <= block->header_size)
|
||||
return LZMA_BUF_ERROR;
|
||||
|
||||
const size_t out_start = *out_pos;
|
||||
*out_pos += block->header_size;
|
||||
|
||||
// Limit out_size so that we stop encoding if the output would grow
|
||||
// bigger than what uncompressed Block would be.
|
||||
if (out_size - *out_pos > block->compressed_size)
|
||||
out_size = *out_pos + block->compressed_size;
|
||||
|
||||
// TODO: In many common cases this could be optimized to use
|
||||
// significantly less memory.
|
||||
lzma_next_coder raw_encoder = LZMA_NEXT_CODER_INIT;
|
||||
lzma_ret ret = lzma_raw_encoder_init(
|
||||
&raw_encoder, allocator, block->filters);
|
||||
|
||||
if (ret == LZMA_OK) {
|
||||
size_t in_pos = 0;
|
||||
ret = raw_encoder.code(raw_encoder.coder, allocator,
|
||||
in, &in_pos, in_size, out, out_pos, out_size,
|
||||
LZMA_FINISH);
|
||||
}
|
||||
|
||||
// NOTE: This needs to be run even if lzma_raw_encoder_init() failed.
|
||||
lzma_next_end(&raw_encoder, allocator);
|
||||
|
||||
if (ret == LZMA_STREAM_END) {
|
||||
// Compression was successful. Write the Block Header.
|
||||
block->compressed_size
|
||||
= *out_pos - (out_start + block->header_size);
|
||||
ret = lzma_block_header_encode(block, out + out_start);
|
||||
if (ret != LZMA_OK)
|
||||
ret = LZMA_PROG_ERROR;
|
||||
|
||||
} else if (ret == LZMA_OK) {
|
||||
// Output buffer became full.
|
||||
ret = LZMA_BUF_ERROR;
|
||||
}
|
||||
|
||||
// Reset *out_pos if something went wrong.
|
||||
if (ret != LZMA_OK)
|
||||
*out_pos = out_start;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
static lzma_ret
|
||||
block_buffer_encode(lzma_block *block, const lzma_allocator *allocator,
|
||||
const uint8_t *in, size_t in_size,
|
||||
uint8_t *out, size_t *out_pos, size_t out_size,
|
||||
bool try_to_compress)
|
||||
{
|
||||
// Validate the arguments.
|
||||
if (block == NULL || (in == NULL && in_size != 0) || out == NULL
|
||||
|| out_pos == NULL || *out_pos > out_size)
|
||||
return LZMA_PROG_ERROR;
|
||||
|
||||
// The contents of the structure may depend on the version so
|
||||
// check the version before validating the contents of *block.
|
||||
if (block->version > 1)
|
||||
return LZMA_OPTIONS_ERROR;
|
||||
|
||||
if ((unsigned int)(block->check) > LZMA_CHECK_ID_MAX
|
||||
|| (try_to_compress && block->filters == NULL))
|
||||
return LZMA_PROG_ERROR;
|
||||
|
||||
if (!lzma_check_is_supported(block->check))
|
||||
return LZMA_UNSUPPORTED_CHECK;
|
||||
|
||||
// Size of a Block has to be a multiple of four, so limit the size
|
||||
// here already. This way we don't need to check it again when adding
|
||||
// Block Padding.
|
||||
out_size -= (out_size - *out_pos) & 3;
|
||||
|
||||
// Get the size of the Check field.
|
||||
const size_t check_size = lzma_check_size(block->check);
|
||||
assert(check_size != UINT32_MAX);
|
||||
|
||||
// Reserve space for the Check field.
|
||||
if (out_size - *out_pos <= check_size)
|
||||
return LZMA_BUF_ERROR;
|
||||
|
||||
out_size -= check_size;
|
||||
|
||||
// Initialize block->uncompressed_size and calculate the worst-case
|
||||
// value for block->compressed_size.
|
||||
block->uncompressed_size = in_size;
|
||||
block->compressed_size = lzma2_bound(in_size);
|
||||
if (block->compressed_size == 0)
|
||||
return LZMA_DATA_ERROR;
|
||||
|
||||
// Do the actual compression.
|
||||
lzma_ret ret = LZMA_BUF_ERROR;
|
||||
if (try_to_compress)
|
||||
ret = block_encode_normal(block, allocator,
|
||||
in, in_size, out, out_pos, out_size);
|
||||
|
||||
if (ret != LZMA_OK) {
|
||||
// If the error was something else than output buffer
|
||||
// becoming full, return the error now.
|
||||
if (ret != LZMA_BUF_ERROR)
|
||||
return ret;
|
||||
|
||||
// The data was uncompressible (at least with the options
|
||||
// given to us) or the output buffer was too small. Use the
|
||||
// uncompressed chunks of LZMA2 to wrap the data into a valid
|
||||
// Block. If we haven't been given enough output space, even
|
||||
// this may fail.
|
||||
return_if_error(block_encode_uncompressed(block, in, in_size,
|
||||
out, out_pos, out_size));
|
||||
}
|
||||
|
||||
assert(*out_pos <= out_size);
|
||||
|
||||
// Block Padding. No buffer overflow here, because we already adjusted
|
||||
// out_size so that (out_size - out_start) is a multiple of four.
|
||||
// Thus, if the buffer is full, the loop body can never run.
|
||||
for (size_t i = (size_t)(block->compressed_size); i & 3; ++i) {
|
||||
assert(*out_pos < out_size);
|
||||
out[(*out_pos)++] = 0x00;
|
||||
}
|
||||
|
||||
// If there's no Check field, we are done now.
|
||||
if (check_size > 0) {
|
||||
// Calculate the integrity check. We reserved space for
|
||||
// the Check field earlier so we don't need to check for
|
||||
// available output space here.
|
||||
lzma_check_state check;
|
||||
lzma_check_init(&check, block->check);
|
||||
lzma_check_update(&check, block->check, in, in_size);
|
||||
lzma_check_finish(&check, block->check);
|
||||
|
||||
memcpy(block->raw_check, check.buffer.u8, check_size);
|
||||
memcpy(out + *out_pos, check.buffer.u8, check_size);
|
||||
*out_pos += check_size;
|
||||
}
|
||||
|
||||
return LZMA_OK;
|
||||
}
|
||||
|
||||
|
||||
extern LZMA_API(lzma_ret)
|
||||
lzma_block_buffer_encode(lzma_block *block, const lzma_allocator *allocator,
|
||||
const uint8_t *in, size_t in_size,
|
||||
uint8_t *out, size_t *out_pos, size_t out_size)
|
||||
{
|
||||
return block_buffer_encode(block, allocator,
|
||||
in, in_size, out, out_pos, out_size, true);
|
||||
}
|
||||
|
||||
|
||||
extern LZMA_API(lzma_ret)
|
||||
lzma_block_uncomp_encode(lzma_block *block,
|
||||
const uint8_t *in, size_t in_size,
|
||||
uint8_t *out, size_t *out_pos, size_t out_size)
|
||||
{
|
||||
// It won't allocate any memory from heap so no need
|
||||
// for lzma_allocator.
|
||||
return block_buffer_encode(block, NULL,
|
||||
in, in_size, out, out_pos, out_size, false);
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
/// \file block_buffer_encoder.h
|
||||
/// \brief Single-call .xz Block encoder
|
||||
//
|
||||
// Author: Lasse Collin
|
||||
//
|
||||
// This file has been put into the public domain.
|
||||
// You can do whatever you want with this file.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef LZMA_BLOCK_BUFFER_ENCODER_H
|
||||
#define LZMA_BLOCK_BUFFER_ENCODER_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
|
||||
/// uint64_t version of lzma_block_buffer_bound(). It is used by
|
||||
/// stream_encoder_mt.c. Probably the original lzma_block_buffer_bound()
|
||||
/// should have been 64-bit, but fixing it would break the ABI.
|
||||
extern uint64_t lzma_block_buffer_bound64(uint64_t uncompressed_size);
|
||||
|
||||
#endif
|
||||
@@ -1,252 +0,0 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
/// \file block_decoder.c
|
||||
/// \brief Decodes .xz Blocks
|
||||
//
|
||||
// Author: Lasse Collin
|
||||
//
|
||||
// This file has been put into the public domain.
|
||||
// You can do whatever you want with this file.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "block_decoder.h"
|
||||
#include "filter_decoder.h"
|
||||
#include "check.h"
|
||||
|
||||
|
||||
struct lzma_coder_s {
|
||||
enum {
|
||||
SEQ_CODE,
|
||||
SEQ_PADDING,
|
||||
SEQ_CHECK,
|
||||
} sequence;
|
||||
|
||||
/// The filters in the chain; initialized with lzma_raw_decoder_init().
|
||||
lzma_next_coder next;
|
||||
|
||||
/// Decoding options; we also write Compressed Size and Uncompressed
|
||||
/// Size back to this structure when the decoding has been finished.
|
||||
lzma_block *block;
|
||||
|
||||
/// Compressed Size calculated while decoding
|
||||
lzma_vli compressed_size;
|
||||
|
||||
/// Uncompressed Size calculated while decoding
|
||||
lzma_vli uncompressed_size;
|
||||
|
||||
/// Maximum allowed Compressed Size; this takes into account the
|
||||
/// size of the Block Header and Check fields when Compressed Size
|
||||
/// is unknown.
|
||||
lzma_vli compressed_limit;
|
||||
|
||||
/// Position when reading the Check field
|
||||
size_t check_pos;
|
||||
|
||||
/// Check of the uncompressed data
|
||||
lzma_check_state check;
|
||||
|
||||
/// True if the integrity check won't be calculated and verified.
|
||||
bool ignore_check;
|
||||
};
|
||||
|
||||
|
||||
static inline bool
|
||||
update_size(lzma_vli *size, lzma_vli add, lzma_vli limit)
|
||||
{
|
||||
if (limit > LZMA_VLI_MAX)
|
||||
limit = LZMA_VLI_MAX;
|
||||
|
||||
if (limit < *size || limit - *size < add)
|
||||
return true;
|
||||
|
||||
*size += add;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
static inline bool
|
||||
is_size_valid(lzma_vli size, lzma_vli reference)
|
||||
{
|
||||
return reference == LZMA_VLI_UNKNOWN || reference == size;
|
||||
}
|
||||
|
||||
|
||||
static lzma_ret
|
||||
block_decode(lzma_coder *coder, const lzma_allocator *allocator,
|
||||
const uint8_t *restrict in, size_t *restrict in_pos,
|
||||
size_t in_size, uint8_t *restrict out,
|
||||
size_t *restrict out_pos, size_t out_size, lzma_action action)
|
||||
{
|
||||
switch (coder->sequence) {
|
||||
case SEQ_CODE: {
|
||||
const size_t in_start = *in_pos;
|
||||
const size_t out_start = *out_pos;
|
||||
|
||||
const lzma_ret ret = coder->next.code(coder->next.coder,
|
||||
allocator, in, in_pos, in_size,
|
||||
out, out_pos, out_size, action);
|
||||
|
||||
const size_t in_used = *in_pos - in_start;
|
||||
const size_t out_used = *out_pos - out_start;
|
||||
|
||||
// NOTE: We compare to compressed_limit here, which prevents
|
||||
// the total size of the Block growing past LZMA_VLI_MAX.
|
||||
if (update_size(&coder->compressed_size, in_used,
|
||||
coder->compressed_limit)
|
||||
|| update_size(&coder->uncompressed_size,
|
||||
out_used,
|
||||
coder->block->uncompressed_size))
|
||||
return LZMA_DATA_ERROR;
|
||||
|
||||
if (!coder->ignore_check)
|
||||
lzma_check_update(&coder->check, coder->block->check,
|
||||
out + out_start, out_used);
|
||||
|
||||
if (ret != LZMA_STREAM_END)
|
||||
return ret;
|
||||
|
||||
// Compressed and Uncompressed Sizes are now at their final
|
||||
// values. Verify that they match the values given to us.
|
||||
if (!is_size_valid(coder->compressed_size,
|
||||
coder->block->compressed_size)
|
||||
|| !is_size_valid(coder->uncompressed_size,
|
||||
coder->block->uncompressed_size))
|
||||
return LZMA_DATA_ERROR;
|
||||
|
||||
// Copy the values into coder->block. The caller
|
||||
// may use this information to construct Index.
|
||||
coder->block->compressed_size = coder->compressed_size;
|
||||
coder->block->uncompressed_size = coder->uncompressed_size;
|
||||
|
||||
coder->sequence = SEQ_PADDING;
|
||||
}
|
||||
|
||||
// Fall through
|
||||
|
||||
case SEQ_PADDING:
|
||||
// Compressed Data is padded to a multiple of four bytes.
|
||||
while (coder->compressed_size & 3) {
|
||||
if (*in_pos >= in_size)
|
||||
return LZMA_OK;
|
||||
|
||||
// We use compressed_size here just get the Padding
|
||||
// right. The actual Compressed Size was stored to
|
||||
// coder->block already, and won't be modified by
|
||||
// us anymore.
|
||||
++coder->compressed_size;
|
||||
|
||||
if (in[(*in_pos)++] != 0x00)
|
||||
return LZMA_DATA_ERROR;
|
||||
}
|
||||
|
||||
if (coder->block->check == LZMA_CHECK_NONE)
|
||||
return LZMA_STREAM_END;
|
||||
|
||||
if (!coder->ignore_check)
|
||||
lzma_check_finish(&coder->check, coder->block->check);
|
||||
|
||||
coder->sequence = SEQ_CHECK;
|
||||
|
||||
// Fall through
|
||||
|
||||
case SEQ_CHECK: {
|
||||
const size_t check_size = lzma_check_size(coder->block->check);
|
||||
lzma_bufcpy(in, in_pos, in_size, coder->block->raw_check,
|
||||
&coder->check_pos, check_size);
|
||||
if (coder->check_pos < check_size)
|
||||
return LZMA_OK;
|
||||
|
||||
// Validate the Check only if we support it.
|
||||
// coder->check.buffer may be uninitialized
|
||||
// when the Check ID is not supported.
|
||||
if (!coder->ignore_check
|
||||
&& lzma_check_is_supported(coder->block->check)
|
||||
&& memcmp(coder->block->raw_check,
|
||||
coder->check.buffer.u8,
|
||||
check_size) != 0)
|
||||
return LZMA_DATA_ERROR;
|
||||
|
||||
return LZMA_STREAM_END;
|
||||
}
|
||||
}
|
||||
|
||||
return LZMA_PROG_ERROR;
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
block_decoder_end(lzma_coder *coder, const lzma_allocator *allocator)
|
||||
{
|
||||
lzma_next_end(&coder->next, allocator);
|
||||
lzma_free(coder, allocator);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
extern lzma_ret
|
||||
lzma_block_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
|
||||
lzma_block *block)
|
||||
{
|
||||
lzma_next_coder_init(&lzma_block_decoder_init, next, allocator);
|
||||
|
||||
// Validate the options. lzma_block_unpadded_size() does that for us
|
||||
// except for Uncompressed Size and filters. Filters are validated
|
||||
// by the raw decoder.
|
||||
if (lzma_block_unpadded_size(block) == 0
|
||||
|| !lzma_vli_is_valid(block->uncompressed_size))
|
||||
return LZMA_PROG_ERROR;
|
||||
|
||||
// Allocate and initialize *next->coder if needed.
|
||||
if (next->coder == NULL) {
|
||||
next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
|
||||
if (next->coder == NULL)
|
||||
return LZMA_MEM_ERROR;
|
||||
|
||||
next->code = &block_decode;
|
||||
next->end = &block_decoder_end;
|
||||
next->coder->next = LZMA_NEXT_CODER_INIT;
|
||||
}
|
||||
|
||||
// Basic initializations
|
||||
next->coder->sequence = SEQ_CODE;
|
||||
next->coder->block = block;
|
||||
next->coder->compressed_size = 0;
|
||||
next->coder->uncompressed_size = 0;
|
||||
|
||||
// If Compressed Size is not known, we calculate the maximum allowed
|
||||
// value so that encoded size of the Block (including Block Padding)
|
||||
// is still a valid VLI and a multiple of four.
|
||||
next->coder->compressed_limit
|
||||
= block->compressed_size == LZMA_VLI_UNKNOWN
|
||||
? (LZMA_VLI_MAX & ~LZMA_VLI_C(3))
|
||||
- block->header_size
|
||||
- lzma_check_size(block->check)
|
||||
: block->compressed_size;
|
||||
|
||||
// Initialize the check. It's caller's problem if the Check ID is not
|
||||
// supported, and the Block decoder cannot verify the Check field.
|
||||
// Caller can test lzma_check_is_supported(block->check).
|
||||
next->coder->check_pos = 0;
|
||||
lzma_check_init(&next->coder->check, block->check);
|
||||
|
||||
next->coder->ignore_check = block->version >= 1
|
||||
? block->ignore_check : false;
|
||||
|
||||
// Initialize the filter chain.
|
||||
return lzma_raw_decoder_init(&next->coder->next, allocator,
|
||||
block->filters);
|
||||
}
|
||||
|
||||
|
||||
extern LZMA_API(lzma_ret)
|
||||
lzma_block_decoder(lzma_stream *strm, lzma_block *block)
|
||||
{
|
||||
lzma_next_strm_init(lzma_block_decoder_init, strm, block);
|
||||
|
||||
strm->internal->supported_actions[LZMA_RUN] = true;
|
||||
strm->internal->supported_actions[LZMA_FINISH] = true;
|
||||
|
||||
return LZMA_OK;
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
/// \file block_decoder.h
|
||||
/// \brief Decodes .xz Blocks
|
||||
//
|
||||
// Author: Lasse Collin
|
||||
//
|
||||
// This file has been put into the public domain.
|
||||
// You can do whatever you want with this file.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef LZMA_BLOCK_DECODER_H
|
||||
#define LZMA_BLOCK_DECODER_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
|
||||
extern lzma_ret lzma_block_decoder_init(lzma_next_coder *next,
|
||||
const lzma_allocator *allocator, lzma_block *block);
|
||||
|
||||
#endif
|
||||
@@ -1,217 +0,0 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
/// \file block_encoder.c
|
||||
/// \brief Encodes .xz Blocks
|
||||
//
|
||||
// Author: Lasse Collin
|
||||
//
|
||||
// This file has been put into the public domain.
|
||||
// You can do whatever you want with this file.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "block_encoder.h"
|
||||
#include "filter_encoder.h"
|
||||
#include "check.h"
|
||||
|
||||
|
||||
struct lzma_coder_s {
|
||||
/// The filters in the chain; initialized with lzma_raw_decoder_init().
|
||||
lzma_next_coder next;
|
||||
|
||||
/// Encoding options; we also write Unpadded Size, Compressed Size,
|
||||
/// and Uncompressed Size back to this structure when the encoding
|
||||
/// has been finished.
|
||||
lzma_block *block;
|
||||
|
||||
enum {
|
||||
SEQ_CODE,
|
||||
SEQ_PADDING,
|
||||
SEQ_CHECK,
|
||||
} sequence;
|
||||
|
||||
/// Compressed Size calculated while encoding
|
||||
lzma_vli compressed_size;
|
||||
|
||||
/// Uncompressed Size calculated while encoding
|
||||
lzma_vli uncompressed_size;
|
||||
|
||||
/// Position in the Check field
|
||||
size_t pos;
|
||||
|
||||
/// Check of the uncompressed data
|
||||
lzma_check_state check;
|
||||
};
|
||||
|
||||
|
||||
static lzma_ret
|
||||
block_encode(lzma_coder *coder, const lzma_allocator *allocator,
|
||||
const uint8_t *restrict in, size_t *restrict in_pos,
|
||||
size_t in_size, uint8_t *restrict out,
|
||||
size_t *restrict out_pos, size_t out_size, lzma_action action)
|
||||
{
|
||||
// Check that our amount of input stays in proper limits.
|
||||
if (LZMA_VLI_MAX - coder->uncompressed_size < in_size - *in_pos)
|
||||
return LZMA_DATA_ERROR;
|
||||
|
||||
switch (coder->sequence) {
|
||||
case SEQ_CODE: {
|
||||
const size_t in_start = *in_pos;
|
||||
const size_t out_start = *out_pos;
|
||||
|
||||
const lzma_ret ret = coder->next.code(coder->next.coder,
|
||||
allocator, in, in_pos, in_size,
|
||||
out, out_pos, out_size, action);
|
||||
|
||||
const size_t in_used = *in_pos - in_start;
|
||||
const size_t out_used = *out_pos - out_start;
|
||||
|
||||
if (COMPRESSED_SIZE_MAX - coder->compressed_size < out_used)
|
||||
return LZMA_DATA_ERROR;
|
||||
|
||||
coder->compressed_size += out_used;
|
||||
|
||||
// No need to check for overflow because we have already
|
||||
// checked it at the beginning of this function.
|
||||
coder->uncompressed_size += in_used;
|
||||
|
||||
lzma_check_update(&coder->check, coder->block->check,
|
||||
in + in_start, in_used);
|
||||
|
||||
if (ret != LZMA_STREAM_END || action == LZMA_SYNC_FLUSH)
|
||||
return ret;
|
||||
|
||||
assert(*in_pos == in_size);
|
||||
assert(action == LZMA_FINISH);
|
||||
|
||||
// Copy the values into coder->block. The caller
|
||||
// may use this information to construct Index.
|
||||
coder->block->compressed_size = coder->compressed_size;
|
||||
coder->block->uncompressed_size = coder->uncompressed_size;
|
||||
|
||||
coder->sequence = SEQ_PADDING;
|
||||
}
|
||||
|
||||
// Fall through
|
||||
|
||||
case SEQ_PADDING:
|
||||
// Pad Compressed Data to a multiple of four bytes. We can
|
||||
// use coder->compressed_size for this since we don't need
|
||||
// it for anything else anymore.
|
||||
while (coder->compressed_size & 3) {
|
||||
if (*out_pos >= out_size)
|
||||
return LZMA_OK;
|
||||
|
||||
out[*out_pos] = 0x00;
|
||||
++*out_pos;
|
||||
++coder->compressed_size;
|
||||
}
|
||||
|
||||
if (coder->block->check == LZMA_CHECK_NONE)
|
||||
return LZMA_STREAM_END;
|
||||
|
||||
lzma_check_finish(&coder->check, coder->block->check);
|
||||
|
||||
coder->sequence = SEQ_CHECK;
|
||||
|
||||
// Fall through
|
||||
|
||||
case SEQ_CHECK: {
|
||||
const size_t check_size = lzma_check_size(coder->block->check);
|
||||
lzma_bufcpy(coder->check.buffer.u8, &coder->pos, check_size,
|
||||
out, out_pos, out_size);
|
||||
if (coder->pos < check_size)
|
||||
return LZMA_OK;
|
||||
|
||||
memcpy(coder->block->raw_check, coder->check.buffer.u8,
|
||||
check_size);
|
||||
return LZMA_STREAM_END;
|
||||
}
|
||||
}
|
||||
|
||||
return LZMA_PROG_ERROR;
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
block_encoder_end(lzma_coder *coder, const lzma_allocator *allocator)
|
||||
{
|
||||
lzma_next_end(&coder->next, allocator);
|
||||
lzma_free(coder, allocator);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
static lzma_ret
|
||||
block_encoder_update(lzma_coder *coder, const lzma_allocator *allocator,
|
||||
const lzma_filter *filters lzma_attribute((__unused__)),
|
||||
const lzma_filter *reversed_filters)
|
||||
{
|
||||
if (coder->sequence != SEQ_CODE)
|
||||
return LZMA_PROG_ERROR;
|
||||
|
||||
return lzma_next_filter_update(
|
||||
&coder->next, allocator, reversed_filters);
|
||||
}
|
||||
|
||||
|
||||
extern lzma_ret
|
||||
lzma_block_encoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
|
||||
lzma_block *block)
|
||||
{
|
||||
lzma_next_coder_init(&lzma_block_encoder_init, next, allocator);
|
||||
|
||||
if (block == NULL)
|
||||
return LZMA_PROG_ERROR;
|
||||
|
||||
// The contents of the structure may depend on the version so
|
||||
// check the version first.
|
||||
if (block->version > 1)
|
||||
return LZMA_OPTIONS_ERROR;
|
||||
|
||||
// If the Check ID is not supported, we cannot calculate the check and
|
||||
// thus not create a proper Block.
|
||||
if ((unsigned int)(block->check) > LZMA_CHECK_ID_MAX)
|
||||
return LZMA_PROG_ERROR;
|
||||
|
||||
if (!lzma_check_is_supported(block->check))
|
||||
return LZMA_UNSUPPORTED_CHECK;
|
||||
|
||||
// Allocate and initialize *next->coder if needed.
|
||||
if (next->coder == NULL) {
|
||||
next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
|
||||
if (next->coder == NULL)
|
||||
return LZMA_MEM_ERROR;
|
||||
|
||||
next->code = &block_encode;
|
||||
next->end = &block_encoder_end;
|
||||
next->update = &block_encoder_update;
|
||||
next->coder->next = LZMA_NEXT_CODER_INIT;
|
||||
}
|
||||
|
||||
// Basic initializations
|
||||
next->coder->sequence = SEQ_CODE;
|
||||
next->coder->block = block;
|
||||
next->coder->compressed_size = 0;
|
||||
next->coder->uncompressed_size = 0;
|
||||
next->coder->pos = 0;
|
||||
|
||||
// Initialize the check
|
||||
lzma_check_init(&next->coder->check, block->check);
|
||||
|
||||
// Initialize the requested filters.
|
||||
return lzma_raw_encoder_init(&next->coder->next, allocator,
|
||||
block->filters);
|
||||
}
|
||||
|
||||
|
||||
extern LZMA_API(lzma_ret)
|
||||
lzma_block_encoder(lzma_stream *strm, lzma_block *block)
|
||||
{
|
||||
lzma_next_strm_init(lzma_block_encoder_init, strm, block);
|
||||
|
||||
strm->internal->supported_actions[LZMA_RUN] = true;
|
||||
strm->internal->supported_actions[LZMA_FINISH] = true;
|
||||
|
||||
return LZMA_OK;
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
/// \file block_encoder.h
|
||||
/// \brief Encodes .xz Blocks
|
||||
//
|
||||
// Author: Lasse Collin
|
||||
//
|
||||
// This file has been put into the public domain.
|
||||
// You can do whatever you want with this file.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef LZMA_BLOCK_ENCODER_H
|
||||
#define LZMA_BLOCK_ENCODER_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
|
||||
/// \brief Biggest Compressed Size value that the Block encoder supports
|
||||
///
|
||||
/// The maximum size of a single Block is limited by the maximum size of
|
||||
/// a Stream, which in theory is 2^63 - 3 bytes (i.e. LZMA_VLI_MAX - 3).
|
||||
/// While the size is really big and no one should hit it in practice, we
|
||||
/// take it into account in some places anyway to catch some errors e.g. if
|
||||
/// application passes insanely big value to some function.
|
||||
///
|
||||
/// We could take into account the headers etc. to determine the exact
|
||||
/// maximum size of the Compressed Data field, but the complexity would give
|
||||
/// us nothing useful. Instead, limit the size of Compressed Data so that
|
||||
/// even with biggest possible Block Header and Check fields the total
|
||||
/// encoded size of the Block stays as a valid VLI. This doesn't guarantee
|
||||
/// that the size of the Stream doesn't grow too big, but that problem is
|
||||
/// taken care outside the Block handling code.
|
||||
///
|
||||
/// ~LZMA_VLI_C(3) is to guarantee that if we need padding at the end of
|
||||
/// the Compressed Data field, it will still stay in the proper limit.
|
||||
///
|
||||
/// This constant is in this file because it is needed in both
|
||||
/// block_encoder.c and block_buffer_encoder.c.
|
||||
#define COMPRESSED_SIZE_MAX ((LZMA_VLI_MAX - LZMA_BLOCK_HEADER_SIZE_MAX \
|
||||
- LZMA_CHECK_SIZE_MAX) & ~LZMA_VLI_C(3))
|
||||
|
||||
|
||||
extern lzma_ret lzma_block_encoder_init(lzma_next_coder *next,
|
||||
const lzma_allocator *allocator, lzma_block *block);
|
||||
|
||||
#endif
|
||||
@@ -1,124 +0,0 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
/// \file block_header_decoder.c
|
||||
/// \brief Decodes Block Header from .xz files
|
||||
//
|
||||
// Author: Lasse Collin
|
||||
//
|
||||
// This file has been put into the public domain.
|
||||
// You can do whatever you want with this file.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "common.h"
|
||||
#include "check.h"
|
||||
|
||||
|
||||
static void
|
||||
free_properties(lzma_block *block, const lzma_allocator *allocator)
|
||||
{
|
||||
// Free allocated filter options. The last array member is not
|
||||
// touched after the initialization in the beginning of
|
||||
// lzma_block_header_decode(), so we don't need to touch that here.
|
||||
for (size_t i = 0; i < LZMA_FILTERS_MAX; ++i) {
|
||||
lzma_free(block->filters[i].options, allocator);
|
||||
block->filters[i].id = LZMA_VLI_UNKNOWN;
|
||||
block->filters[i].options = NULL;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
extern LZMA_API(lzma_ret)
|
||||
lzma_block_header_decode(lzma_block *block,
|
||||
const lzma_allocator *allocator, const uint8_t *in)
|
||||
{
|
||||
// NOTE: We consider the header to be corrupt not only when the
|
||||
// CRC32 doesn't match, but also when variable-length integers
|
||||
// are invalid or over 63 bits, or if the header is too small
|
||||
// to contain the claimed information.
|
||||
|
||||
// Initialize the filter options array. This way the caller can
|
||||
// safely free() the options even if an error occurs in this function.
|
||||
for (size_t i = 0; i <= LZMA_FILTERS_MAX; ++i) {
|
||||
block->filters[i].id = LZMA_VLI_UNKNOWN;
|
||||
block->filters[i].options = NULL;
|
||||
}
|
||||
|
||||
// Versions 0 and 1 are supported. If a newer version was specified,
|
||||
// we need to downgrade it.
|
||||
if (block->version > 1)
|
||||
block->version = 1;
|
||||
|
||||
// This isn't a Block Header option, but since the decompressor will
|
||||
// read it if version >= 1, it's better to initialize it here than
|
||||
// to expect the caller to do it since in almost all cases this
|
||||
// should be false.
|
||||
block->ignore_check = false;
|
||||
|
||||
// Validate Block Header Size and Check type. The caller must have
|
||||
// already set these, so it is a programming error if this test fails.
|
||||
if (lzma_block_header_size_decode(in[0]) != block->header_size
|
||||
|| (unsigned int)(block->check) > LZMA_CHECK_ID_MAX)
|
||||
return LZMA_PROG_ERROR;
|
||||
|
||||
// Exclude the CRC32 field.
|
||||
const size_t in_size = block->header_size - 4;
|
||||
|
||||
// Verify CRC32
|
||||
if (lzma_crc32(in, in_size, 0) != unaligned_read32le(in + in_size))
|
||||
return LZMA_DATA_ERROR;
|
||||
|
||||
// Check for unsupported flags.
|
||||
if (in[1] & 0x3C)
|
||||
return LZMA_OPTIONS_ERROR;
|
||||
|
||||
// Start after the Block Header Size and Block Flags fields.
|
||||
size_t in_pos = 2;
|
||||
|
||||
// Compressed Size
|
||||
if (in[1] & 0x40) {
|
||||
return_if_error(lzma_vli_decode(&block->compressed_size,
|
||||
NULL, in, &in_pos, in_size));
|
||||
|
||||
// Validate Compressed Size. This checks that it isn't zero
|
||||
// and that the total size of the Block is a valid VLI.
|
||||
if (lzma_block_unpadded_size(block) == 0)
|
||||
return LZMA_DATA_ERROR;
|
||||
} else {
|
||||
block->compressed_size = LZMA_VLI_UNKNOWN;
|
||||
}
|
||||
|
||||
// Uncompressed Size
|
||||
if (in[1] & 0x80)
|
||||
return_if_error(lzma_vli_decode(&block->uncompressed_size,
|
||||
NULL, in, &in_pos, in_size));
|
||||
else
|
||||
block->uncompressed_size = LZMA_VLI_UNKNOWN;
|
||||
|
||||
// Filter Flags
|
||||
const size_t filter_count = (in[1] & 3) + 1;
|
||||
for (size_t i = 0; i < filter_count; ++i) {
|
||||
const lzma_ret ret = lzma_filter_flags_decode(
|
||||
&block->filters[i], allocator,
|
||||
in, &in_pos, in_size);
|
||||
if (ret != LZMA_OK) {
|
||||
free_properties(block, allocator);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
// Padding
|
||||
while (in_pos < in_size) {
|
||||
if (in[in_pos++] != 0x00) {
|
||||
free_properties(block, allocator);
|
||||
|
||||
// Possibly some new field present so use
|
||||
// LZMA_OPTIONS_ERROR instead of LZMA_DATA_ERROR.
|
||||
return LZMA_OPTIONS_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
return LZMA_OK;
|
||||
}
|
||||
@@ -1,132 +0,0 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
/// \file block_header_encoder.c
|
||||
/// \brief Encodes Block Header for .xz files
|
||||
//
|
||||
// Author: Lasse Collin
|
||||
//
|
||||
// This file has been put into the public domain.
|
||||
// You can do whatever you want with this file.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "common.h"
|
||||
#include "check.h"
|
||||
|
||||
|
||||
extern LZMA_API(lzma_ret)
|
||||
lzma_block_header_size(lzma_block *block)
|
||||
{
|
||||
if (block->version > 1)
|
||||
return LZMA_OPTIONS_ERROR;
|
||||
|
||||
// Block Header Size + Block Flags + CRC32.
|
||||
uint32_t size = 1 + 1 + 4;
|
||||
|
||||
// Compressed Size
|
||||
if (block->compressed_size != LZMA_VLI_UNKNOWN) {
|
||||
const uint32_t add = lzma_vli_size(block->compressed_size);
|
||||
if (add == 0 || block->compressed_size == 0)
|
||||
return LZMA_PROG_ERROR;
|
||||
|
||||
size += add;
|
||||
}
|
||||
|
||||
// Uncompressed Size
|
||||
if (block->uncompressed_size != LZMA_VLI_UNKNOWN) {
|
||||
const uint32_t add = lzma_vli_size(block->uncompressed_size);
|
||||
if (add == 0)
|
||||
return LZMA_PROG_ERROR;
|
||||
|
||||
size += add;
|
||||
}
|
||||
|
||||
// List of Filter Flags
|
||||
if (block->filters == NULL || block->filters[0].id == LZMA_VLI_UNKNOWN)
|
||||
return LZMA_PROG_ERROR;
|
||||
|
||||
for (size_t i = 0; block->filters[i].id != LZMA_VLI_UNKNOWN; ++i) {
|
||||
// Don't allow too many filters.
|
||||
if (i == LZMA_FILTERS_MAX)
|
||||
return LZMA_PROG_ERROR;
|
||||
|
||||
uint32_t add;
|
||||
return_if_error(lzma_filter_flags_size(&add,
|
||||
block->filters + i));
|
||||
|
||||
size += add;
|
||||
}
|
||||
|
||||
// Pad to a multiple of four bytes.
|
||||
block->header_size = (size + 3) & ~UINT32_C(3);
|
||||
|
||||
// NOTE: We don't verify that the encoded size of the Block stays
|
||||
// within limits. This is because it is possible that we are called
|
||||
// with exaggerated Compressed Size (e.g. LZMA_VLI_MAX) to reserve
|
||||
// space for Block Header, and later called again with lower,
|
||||
// real values.
|
||||
|
||||
return LZMA_OK;
|
||||
}
|
||||
|
||||
|
||||
extern LZMA_API(lzma_ret)
|
||||
lzma_block_header_encode(const lzma_block *block, uint8_t *out)
|
||||
{
|
||||
// Validate everything but filters.
|
||||
if (lzma_block_unpadded_size(block) == 0
|
||||
|| !lzma_vli_is_valid(block->uncompressed_size))
|
||||
return LZMA_PROG_ERROR;
|
||||
|
||||
// Indicate the size of the buffer _excluding_ the CRC32 field.
|
||||
const size_t out_size = block->header_size - 4;
|
||||
|
||||
// Store the Block Header Size.
|
||||
out[0] = out_size / 4;
|
||||
|
||||
// We write Block Flags in pieces.
|
||||
out[1] = 0x00;
|
||||
size_t out_pos = 2;
|
||||
|
||||
// Compressed Size
|
||||
if (block->compressed_size != LZMA_VLI_UNKNOWN) {
|
||||
return_if_error(lzma_vli_encode(block->compressed_size, NULL,
|
||||
out, &out_pos, out_size));
|
||||
|
||||
out[1] |= 0x40;
|
||||
}
|
||||
|
||||
// Uncompressed Size
|
||||
if (block->uncompressed_size != LZMA_VLI_UNKNOWN) {
|
||||
return_if_error(lzma_vli_encode(block->uncompressed_size, NULL,
|
||||
out, &out_pos, out_size));
|
||||
|
||||
out[1] |= 0x80;
|
||||
}
|
||||
|
||||
// Filter Flags
|
||||
if (block->filters == NULL || block->filters[0].id == LZMA_VLI_UNKNOWN)
|
||||
return LZMA_PROG_ERROR;
|
||||
|
||||
size_t filter_count = 0;
|
||||
do {
|
||||
// There can be a maximum of four filters.
|
||||
if (filter_count == LZMA_FILTERS_MAX)
|
||||
return LZMA_PROG_ERROR;
|
||||
|
||||
return_if_error(lzma_filter_flags_encode(
|
||||
block->filters + filter_count,
|
||||
out, &out_pos, out_size));
|
||||
|
||||
} while (block->filters[++filter_count].id != LZMA_VLI_UNKNOWN);
|
||||
|
||||
out[1] |= filter_count - 1;
|
||||
|
||||
// Padding
|
||||
memzero(out + out_pos, out_size - out_pos);
|
||||
|
||||
// CRC32
|
||||
unaligned_write32le(out + out_size, lzma_crc32(out, out_size, 0));
|
||||
|
||||
return LZMA_OK;
|
||||
}
|
||||
@@ -1,90 +0,0 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
/// \file block_header.c
|
||||
/// \brief Utility functions to handle lzma_block
|
||||
//
|
||||
// Author: Lasse Collin
|
||||
//
|
||||
// This file has been put into the public domain.
|
||||
// You can do whatever you want with this file.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "common.h"
|
||||
#include "index.h"
|
||||
|
||||
|
||||
extern LZMA_API(lzma_ret)
|
||||
lzma_block_compressed_size(lzma_block *block, lzma_vli unpadded_size)
|
||||
{
|
||||
// Validate everything but Uncompressed Size and filters.
|
||||
if (lzma_block_unpadded_size(block) == 0)
|
||||
return LZMA_PROG_ERROR;
|
||||
|
||||
const uint32_t container_size = block->header_size
|
||||
+ lzma_check_size(block->check);
|
||||
|
||||
// Validate that Compressed Size will be greater than zero.
|
||||
if (unpadded_size <= container_size)
|
||||
return LZMA_DATA_ERROR;
|
||||
|
||||
// Calculate what Compressed Size is supposed to be.
|
||||
// If Compressed Size was present in Block Header,
|
||||
// compare that the new value matches it.
|
||||
const lzma_vli compressed_size = unpadded_size - container_size;
|
||||
if (block->compressed_size != LZMA_VLI_UNKNOWN
|
||||
&& block->compressed_size != compressed_size)
|
||||
return LZMA_DATA_ERROR;
|
||||
|
||||
block->compressed_size = compressed_size;
|
||||
|
||||
return LZMA_OK;
|
||||
}
|
||||
|
||||
|
||||
extern LZMA_API(lzma_vli)
|
||||
lzma_block_unpadded_size(const lzma_block *block)
|
||||
{
|
||||
// Validate the values that we are interested in i.e. all but
|
||||
// Uncompressed Size and the filters.
|
||||
//
|
||||
// NOTE: This function is used for validation too, so it is
|
||||
// essential that these checks are always done even if
|
||||
// Compressed Size is unknown.
|
||||
if (block == NULL || block->version > 1
|
||||
|| block->header_size < LZMA_BLOCK_HEADER_SIZE_MIN
|
||||
|| block->header_size > LZMA_BLOCK_HEADER_SIZE_MAX
|
||||
|| (block->header_size & 3)
|
||||
|| !lzma_vli_is_valid(block->compressed_size)
|
||||
|| block->compressed_size == 0
|
||||
|| (unsigned int)(block->check) > LZMA_CHECK_ID_MAX)
|
||||
return 0;
|
||||
|
||||
// If Compressed Size is unknown, return that we cannot know
|
||||
// size of the Block either.
|
||||
if (block->compressed_size == LZMA_VLI_UNKNOWN)
|
||||
return LZMA_VLI_UNKNOWN;
|
||||
|
||||
// Calculate Unpadded Size and validate it.
|
||||
const lzma_vli unpadded_size = block->compressed_size
|
||||
+ block->header_size
|
||||
+ lzma_check_size(block->check);
|
||||
|
||||
assert(unpadded_size >= UNPADDED_SIZE_MIN);
|
||||
if (unpadded_size > UNPADDED_SIZE_MAX)
|
||||
return 0;
|
||||
|
||||
return unpadded_size;
|
||||
}
|
||||
|
||||
|
||||
extern LZMA_API(lzma_vli)
|
||||
lzma_block_total_size(const lzma_block *block)
|
||||
{
|
||||
lzma_vli unpadded_size = lzma_block_unpadded_size(block);
|
||||
|
||||
if (unpadded_size != LZMA_VLI_UNKNOWN)
|
||||
unpadded_size = vli_ceil4(unpadded_size);
|
||||
|
||||
return unpadded_size;
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
@@ -14,7 +14,6 @@
|
||||
#define LZMA_COMMON_H
|
||||
|
||||
#include "sysdefs.h"
|
||||
#include "mythread.h"
|
||||
#include "tuklib_integer.h"
|
||||
|
||||
#if defined(_WIN32) || defined(__CYGWIN__)
|
||||
@@ -88,10 +87,6 @@
|
||||
#define LZMA_TIMED_OUT 32
|
||||
|
||||
|
||||
/// Type of encoder/decoder specific data; the actual structure is defined
|
||||
/// differently in different coders.
|
||||
typedef struct lzma_coder_s lzma_coder;
|
||||
|
||||
typedef struct lzma_next_coder_s lzma_next_coder;
|
||||
|
||||
typedef struct lzma_filter_info_s lzma_filter_info;
|
||||
@@ -107,7 +102,7 @@ typedef lzma_ret (*lzma_init_function)(
|
||||
/// input and output buffers, but for simplicity they still use this same
|
||||
/// function prototype.
|
||||
typedef lzma_ret (*lzma_code_function)(
|
||||
lzma_coder *coder, const lzma_allocator *allocator,
|
||||
void *coder, const lzma_allocator *allocator,
|
||||
const uint8_t *restrict in, size_t *restrict in_pos,
|
||||
size_t in_size, uint8_t *restrict out,
|
||||
size_t *restrict out_pos, size_t out_size,
|
||||
@@ -115,7 +110,7 @@ typedef lzma_ret (*lzma_code_function)(
|
||||
|
||||
/// Type of a function to free the memory allocated for the coder
|
||||
typedef void (*lzma_end_function)(
|
||||
lzma_coder *coder, const lzma_allocator *allocator);
|
||||
void *coder, const lzma_allocator *allocator);
|
||||
|
||||
|
||||
/// Raw coder validates and converts an array of lzma_filter structures to
|
||||
@@ -138,7 +133,7 @@ struct lzma_filter_info_s {
|
||||
/// Hold data and function pointers of the next filter in the chain.
|
||||
struct lzma_next_coder_s {
|
||||
/// Pointer to coder-specific data
|
||||
lzma_coder *coder;
|
||||
void *coder;
|
||||
|
||||
/// Filter ID. This is LZMA_VLI_UNKNOWN when this structure doesn't
|
||||
/// point to a filter coder.
|
||||
@@ -160,21 +155,21 @@ struct lzma_next_coder_s {
|
||||
|
||||
/// Pointer to a function to get progress information. If this is NULL,
|
||||
/// lzma_stream.total_in and .total_out are used instead.
|
||||
void (*get_progress)(lzma_coder *coder,
|
||||
void (*get_progress)(void *coder,
|
||||
uint64_t *progress_in, uint64_t *progress_out);
|
||||
|
||||
/// Pointer to function to return the type of the integrity check.
|
||||
/// Most coders won't support this.
|
||||
lzma_check (*get_check)(const lzma_coder *coder);
|
||||
lzma_check (*get_check)(const void *coder);
|
||||
|
||||
/// Pointer to function to get and/or change the memory usage limit.
|
||||
/// If new_memlimit == 0, the limit is not changed.
|
||||
lzma_ret (*memconfig)(lzma_coder *coder, uint64_t *memusage,
|
||||
lzma_ret (*memconfig)(void *coder, uint64_t *memusage,
|
||||
uint64_t *old_memlimit, uint64_t new_memlimit);
|
||||
|
||||
/// Update the filter-specific options or the whole filter chain
|
||||
/// in the encoder.
|
||||
lzma_ret (*update)(lzma_coder *coder, const lzma_allocator *allocator,
|
||||
lzma_ret (*update)(void *coder, const lzma_allocator *allocator,
|
||||
const lzma_filter *filters,
|
||||
const lzma_filter *reversed_filters);
|
||||
};
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
/// \file easy_buffer_encoder.c
|
||||
/// \brief Easy single-call .xz Stream encoder
|
||||
//
|
||||
// Author: Lasse Collin
|
||||
//
|
||||
// This file has been put into the public domain.
|
||||
// You can do whatever you want with this file.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "easy_preset.h"
|
||||
|
||||
|
||||
extern LZMA_API(lzma_ret)
|
||||
lzma_easy_buffer_encode(uint32_t preset, lzma_check check,
|
||||
const lzma_allocator *allocator, const uint8_t *in,
|
||||
size_t in_size, uint8_t *out, size_t *out_pos, size_t out_size)
|
||||
{
|
||||
lzma_options_easy opt_easy;
|
||||
if (lzma_easy_preset(&opt_easy, preset))
|
||||
return LZMA_OPTIONS_ERROR;
|
||||
|
||||
return lzma_stream_buffer_encode(opt_easy.filters, check,
|
||||
allocator, in, in_size, out, out_pos, out_size);
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
/// \file easy_decoder_memusage.c
|
||||
/// \brief Decoder memory usage calculation to match easy encoder presets
|
||||
//
|
||||
// Author: Lasse Collin
|
||||
//
|
||||
// This file has been put into the public domain.
|
||||
// You can do whatever you want with this file.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "easy_preset.h"
|
||||
|
||||
|
||||
extern LZMA_API(uint64_t)
|
||||
lzma_easy_decoder_memusage(uint32_t preset)
|
||||
{
|
||||
lzma_options_easy opt_easy;
|
||||
if (lzma_easy_preset(&opt_easy, preset))
|
||||
return UINT32_MAX;
|
||||
|
||||
return lzma_raw_decoder_memusage(opt_easy.filters);
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
/// \file easy_encoder.c
|
||||
/// \brief Easy .xz Stream encoder initialization
|
||||
//
|
||||
// Author: Lasse Collin
|
||||
//
|
||||
// This file has been put into the public domain.
|
||||
// You can do whatever you want with this file.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "easy_preset.h"
|
||||
|
||||
|
||||
extern LZMA_API(lzma_ret)
|
||||
lzma_easy_encoder(lzma_stream *strm, uint32_t preset, lzma_check check)
|
||||
{
|
||||
lzma_options_easy opt_easy;
|
||||
if (lzma_easy_preset(&opt_easy, preset))
|
||||
return LZMA_OPTIONS_ERROR;
|
||||
|
||||
return lzma_stream_encoder(strm, opt_easy.filters, check);
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
/// \file easy_encoder_memusage.c
|
||||
/// \brief Easy .xz Stream encoder memory usage calculation
|
||||
//
|
||||
// Author: Lasse Collin
|
||||
//
|
||||
// This file has been put into the public domain.
|
||||
// You can do whatever you want with this file.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "easy_preset.h"
|
||||
|
||||
|
||||
extern LZMA_API(uint64_t)
|
||||
lzma_easy_encoder_memusage(uint32_t preset)
|
||||
{
|
||||
lzma_options_easy opt_easy;
|
||||
if (lzma_easy_preset(&opt_easy, preset))
|
||||
return UINT32_MAX;
|
||||
|
||||
return lzma_raw_encoder_memusage(opt_easy.filters);
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
/// \file easy_preset.c
|
||||
/// \brief Preset handling for easy encoder and decoder
|
||||
//
|
||||
// Author: Lasse Collin
|
||||
//
|
||||
// This file has been put into the public domain.
|
||||
// You can do whatever you want with this file.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "easy_preset.h"
|
||||
|
||||
|
||||
extern bool
|
||||
lzma_easy_preset(lzma_options_easy *opt_easy, uint32_t preset)
|
||||
{
|
||||
if (lzma_lzma_preset(&opt_easy->opt_lzma, preset))
|
||||
return true;
|
||||
|
||||
opt_easy->filters[0].id = LZMA_FILTER_LZMA2;
|
||||
opt_easy->filters[0].options = &opt_easy->opt_lzma;
|
||||
opt_easy->filters[1].id = LZMA_VLI_UNKNOWN;
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
/// \file easy_preset.h
|
||||
/// \brief Preset handling for easy encoder and decoder
|
||||
//
|
||||
// Author: Lasse Collin
|
||||
//
|
||||
// This file has been put into the public domain.
|
||||
// You can do whatever you want with this file.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "common.h"
|
||||
|
||||
|
||||
typedef struct {
|
||||
/// We need to keep the filters array available in case
|
||||
/// LZMA_FULL_FLUSH is used.
|
||||
lzma_filter filters[LZMA_FILTERS_MAX + 1];
|
||||
|
||||
/// Options for LZMA2
|
||||
lzma_options_lzma opt_lzma;
|
||||
|
||||
// Options for more filters can be added later, so this struct
|
||||
// is not ready to be put into the public API.
|
||||
|
||||
} lzma_options_easy;
|
||||
|
||||
|
||||
/// Set *easy to the settings given by the preset. Returns true on error,
|
||||
/// false on success.
|
||||
extern bool lzma_easy_preset(lzma_options_easy *easy, uint32_t preset);
|
||||
@@ -1,88 +0,0 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
/// \file filter_buffer_decoder.c
|
||||
/// \brief Single-call raw decoding
|
||||
//
|
||||
// Author: Lasse Collin
|
||||
//
|
||||
// This file has been put into the public domain.
|
||||
// You can do whatever you want with this file.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "filter_decoder.h"
|
||||
|
||||
|
||||
extern LZMA_API(lzma_ret)
|
||||
lzma_raw_buffer_decode(
|
||||
const lzma_filter *filters, const lzma_allocator *allocator,
|
||||
const uint8_t *in, size_t *in_pos, size_t in_size,
|
||||
uint8_t *out, size_t *out_pos, size_t out_size)
|
||||
{
|
||||
// Validate what isn't validated later in filter_common.c.
|
||||
if (in == NULL || in_pos == NULL || *in_pos > in_size || out == NULL
|
||||
|| out_pos == NULL || *out_pos > out_size)
|
||||
return LZMA_PROG_ERROR;
|
||||
|
||||
// Initialize the decoer.
|
||||
lzma_next_coder next = LZMA_NEXT_CODER_INIT;
|
||||
return_if_error(lzma_raw_decoder_init(&next, allocator, filters));
|
||||
|
||||
// Store the positions so that we can restore them if something
|
||||
// goes wrong.
|
||||
const size_t in_start = *in_pos;
|
||||
const size_t out_start = *out_pos;
|
||||
|
||||
// Do the actual decoding and free decoder's memory.
|
||||
lzma_ret ret = next.code(next.coder, allocator, in, in_pos, in_size,
|
||||
out, out_pos, out_size, LZMA_FINISH);
|
||||
|
||||
if (ret == LZMA_STREAM_END) {
|
||||
ret = LZMA_OK;
|
||||
} else {
|
||||
if (ret == LZMA_OK) {
|
||||
// Either the input was truncated or the
|
||||
// output buffer was too small.
|
||||
assert(*in_pos == in_size || *out_pos == out_size);
|
||||
|
||||
if (*in_pos != in_size) {
|
||||
// Since input wasn't consumed completely,
|
||||
// the output buffer became full and is
|
||||
// too small.
|
||||
ret = LZMA_BUF_ERROR;
|
||||
|
||||
} else if (*out_pos != out_size) {
|
||||
// Since output didn't became full, the input
|
||||
// has to be truncated.
|
||||
ret = LZMA_DATA_ERROR;
|
||||
|
||||
} else {
|
||||
// All the input was consumed and output
|
||||
// buffer is full. Now we don't immediately
|
||||
// know the reason for the error. Try
|
||||
// decoding one more byte. If it succeeds,
|
||||
// then the output buffer was too small. If
|
||||
// we cannot get a new output byte, the input
|
||||
// is truncated.
|
||||
uint8_t tmp[1];
|
||||
size_t tmp_pos = 0;
|
||||
(void)next.code(next.coder, allocator,
|
||||
in, in_pos, in_size,
|
||||
tmp, &tmp_pos, 1, LZMA_FINISH);
|
||||
|
||||
if (tmp_pos == 1)
|
||||
ret = LZMA_BUF_ERROR;
|
||||
else
|
||||
ret = LZMA_DATA_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
// Restore the positions.
|
||||
*in_pos = in_start;
|
||||
*out_pos = out_start;
|
||||
}
|
||||
|
||||
lzma_next_end(&next, allocator);
|
||||
|
||||
return ret;
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
/// \file filter_buffer_encoder.c
|
||||
/// \brief Single-call raw encoding
|
||||
//
|
||||
// Author: Lasse Collin
|
||||
//
|
||||
// This file has been put into the public domain.
|
||||
// You can do whatever you want with this file.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "filter_encoder.h"
|
||||
|
||||
|
||||
extern LZMA_API(lzma_ret)
|
||||
lzma_raw_buffer_encode(
|
||||
const lzma_filter *filters, const lzma_allocator *allocator,
|
||||
const uint8_t *in, size_t in_size,
|
||||
uint8_t *out, size_t *out_pos, size_t out_size)
|
||||
{
|
||||
// Validate what isn't validated later in filter_common.c.
|
||||
if ((in == NULL && in_size != 0) || out == NULL
|
||||
|| out_pos == NULL || *out_pos > out_size)
|
||||
return LZMA_PROG_ERROR;
|
||||
|
||||
// Initialize the encoder
|
||||
lzma_next_coder next = LZMA_NEXT_CODER_INIT;
|
||||
return_if_error(lzma_raw_encoder_init(&next, allocator, filters));
|
||||
|
||||
// Store the output position so that we can restore it if
|
||||
// something goes wrong.
|
||||
const size_t out_start = *out_pos;
|
||||
|
||||
// Do the actual encoding and free coder's memory.
|
||||
size_t in_pos = 0;
|
||||
lzma_ret ret = next.code(next.coder, allocator, in, &in_pos, in_size,
|
||||
out, out_pos, out_size, LZMA_FINISH);
|
||||
lzma_next_end(&next, allocator);
|
||||
|
||||
if (ret == LZMA_STREAM_END) {
|
||||
ret = LZMA_OK;
|
||||
} else {
|
||||
if (ret == LZMA_OK) {
|
||||
// Output buffer was too small.
|
||||
assert(*out_pos == out_size);
|
||||
ret = LZMA_BUF_ERROR;
|
||||
}
|
||||
|
||||
// Restore the output position.
|
||||
*out_pos = out_start;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
@@ -1,337 +0,0 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
/// \file filter_common.c
|
||||
/// \brief Filter-specific stuff common for both encoder and decoder
|
||||
//
|
||||
// Author: Lasse Collin
|
||||
//
|
||||
// This file has been put into the public domain.
|
||||
// You can do whatever you want with this file.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "filter_common.h"
|
||||
|
||||
|
||||
static const struct {
|
||||
/// Filter ID
|
||||
lzma_vli id;
|
||||
|
||||
/// Size of the filter-specific options structure
|
||||
size_t options_size;
|
||||
|
||||
/// True if it is OK to use this filter as non-last filter in
|
||||
/// the chain.
|
||||
bool non_last_ok;
|
||||
|
||||
/// True if it is OK to use this filter as the last filter in
|
||||
/// the chain.
|
||||
bool last_ok;
|
||||
|
||||
/// True if the filter may change the size of the data (that is, the
|
||||
/// amount of encoded output can be different than the amount of
|
||||
/// uncompressed input).
|
||||
bool changes_size;
|
||||
|
||||
} features[] = {
|
||||
#if defined (HAVE_ENCODER_LZMA1) || defined(HAVE_DECODER_LZMA1)
|
||||
{
|
||||
.id = LZMA_FILTER_LZMA1,
|
||||
.options_size = sizeof(lzma_options_lzma),
|
||||
.non_last_ok = false,
|
||||
.last_ok = true,
|
||||
.changes_size = true,
|
||||
},
|
||||
#endif
|
||||
#if defined(HAVE_ENCODER_LZMA2) || defined(HAVE_DECODER_LZMA2)
|
||||
{
|
||||
.id = LZMA_FILTER_LZMA2,
|
||||
.options_size = sizeof(lzma_options_lzma),
|
||||
.non_last_ok = false,
|
||||
.last_ok = true,
|
||||
.changes_size = true,
|
||||
},
|
||||
#endif
|
||||
#if defined(HAVE_ENCODER_X86) || defined(HAVE_DECODER_X86)
|
||||
{
|
||||
.id = LZMA_FILTER_X86,
|
||||
.options_size = sizeof(lzma_options_bcj),
|
||||
.non_last_ok = true,
|
||||
.last_ok = false,
|
||||
.changes_size = false,
|
||||
},
|
||||
#endif
|
||||
#if defined(HAVE_ENCODER_POWERPC) || defined(HAVE_DECODER_POWERPC)
|
||||
{
|
||||
.id = LZMA_FILTER_POWERPC,
|
||||
.options_size = sizeof(lzma_options_bcj),
|
||||
.non_last_ok = true,
|
||||
.last_ok = false,
|
||||
.changes_size = false,
|
||||
},
|
||||
#endif
|
||||
#if defined(HAVE_ENCODER_IA64) || defined(HAVE_DECODER_IA64)
|
||||
{
|
||||
.id = LZMA_FILTER_IA64,
|
||||
.options_size = sizeof(lzma_options_bcj),
|
||||
.non_last_ok = true,
|
||||
.last_ok = false,
|
||||
.changes_size = false,
|
||||
},
|
||||
#endif
|
||||
#if defined(HAVE_ENCODER_ARM) || defined(HAVE_DECODER_ARM)
|
||||
{
|
||||
.id = LZMA_FILTER_ARM,
|
||||
.options_size = sizeof(lzma_options_bcj),
|
||||
.non_last_ok = true,
|
||||
.last_ok = false,
|
||||
.changes_size = false,
|
||||
},
|
||||
#endif
|
||||
#if defined(HAVE_ENCODER_ARMTHUMB) || defined(HAVE_DECODER_ARMTHUMB)
|
||||
{
|
||||
.id = LZMA_FILTER_ARMTHUMB,
|
||||
.options_size = sizeof(lzma_options_bcj),
|
||||
.non_last_ok = true,
|
||||
.last_ok = false,
|
||||
.changes_size = false,
|
||||
},
|
||||
#endif
|
||||
#if defined(HAVE_ENCODER_SPARC) || defined(HAVE_DECODER_SPARC)
|
||||
{
|
||||
.id = LZMA_FILTER_SPARC,
|
||||
.options_size = sizeof(lzma_options_bcj),
|
||||
.non_last_ok = true,
|
||||
.last_ok = false,
|
||||
.changes_size = false,
|
||||
},
|
||||
#endif
|
||||
#if defined(HAVE_ENCODER_DELTA) || defined(HAVE_DECODER_DELTA)
|
||||
{
|
||||
.id = LZMA_FILTER_DELTA,
|
||||
.options_size = sizeof(lzma_options_delta),
|
||||
.non_last_ok = true,
|
||||
.last_ok = false,
|
||||
.changes_size = false,
|
||||
},
|
||||
#endif
|
||||
{
|
||||
.id = LZMA_VLI_UNKNOWN
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
extern LZMA_API(lzma_ret)
|
||||
lzma_filters_copy(const lzma_filter *src, lzma_filter *dest,
|
||||
const lzma_allocator *allocator)
|
||||
{
|
||||
if (src == NULL || dest == NULL)
|
||||
return LZMA_PROG_ERROR;
|
||||
|
||||
lzma_ret ret;
|
||||
size_t i;
|
||||
for (i = 0; src[i].id != LZMA_VLI_UNKNOWN; ++i) {
|
||||
// There must be a maximum of four filters plus
|
||||
// the array terminator.
|
||||
if (i == LZMA_FILTERS_MAX) {
|
||||
ret = LZMA_OPTIONS_ERROR;
|
||||
goto error;
|
||||
}
|
||||
|
||||
dest[i].id = src[i].id;
|
||||
|
||||
if (src[i].options == NULL) {
|
||||
dest[i].options = NULL;
|
||||
} else {
|
||||
// See if the filter is supported only when the
|
||||
// options is not NULL. This might be convenient
|
||||
// sometimes if the app is actually copying only
|
||||
// a partial filter chain with a place holder ID.
|
||||
//
|
||||
// When options is not NULL, the Filter ID must be
|
||||
// supported by us, because otherwise we don't know
|
||||
// how big the options are.
|
||||
size_t j;
|
||||
for (j = 0; src[i].id != features[j].id; ++j) {
|
||||
if (features[j].id == LZMA_VLI_UNKNOWN) {
|
||||
ret = LZMA_OPTIONS_ERROR;
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
|
||||
// Allocate and copy the options.
|
||||
dest[i].options = lzma_alloc(features[j].options_size,
|
||||
allocator);
|
||||
if (dest[i].options == NULL) {
|
||||
ret = LZMA_MEM_ERROR;
|
||||
goto error;
|
||||
}
|
||||
|
||||
memcpy(dest[i].options, src[i].options,
|
||||
features[j].options_size);
|
||||
}
|
||||
}
|
||||
|
||||
// Terminate the filter array.
|
||||
assert(i <= LZMA_FILTERS_MAX + 1);
|
||||
dest[i].id = LZMA_VLI_UNKNOWN;
|
||||
dest[i].options = NULL;
|
||||
|
||||
return LZMA_OK;
|
||||
|
||||
error:
|
||||
// Free the options which we have already allocated.
|
||||
while (i-- > 0) {
|
||||
lzma_free(dest[i].options, allocator);
|
||||
dest[i].options = NULL;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
static lzma_ret
|
||||
validate_chain(const lzma_filter *filters, size_t *count)
|
||||
{
|
||||
// There must be at least one filter.
|
||||
if (filters == NULL || filters[0].id == LZMA_VLI_UNKNOWN)
|
||||
return LZMA_PROG_ERROR;
|
||||
|
||||
// Number of non-last filters that may change the size of the data
|
||||
// significantly (that is, more than 1-2 % or so).
|
||||
size_t changes_size_count = 0;
|
||||
|
||||
// True if it is OK to add a new filter after the current filter.
|
||||
bool non_last_ok = true;
|
||||
|
||||
// True if the last filter in the given chain is actually usable as
|
||||
// the last filter. Only filters that support embedding End of Payload
|
||||
// Marker can be used as the last filter in the chain.
|
||||
bool last_ok = false;
|
||||
|
||||
size_t i = 0;
|
||||
do {
|
||||
size_t j;
|
||||
for (j = 0; filters[i].id != features[j].id; ++j)
|
||||
if (features[j].id == LZMA_VLI_UNKNOWN)
|
||||
return LZMA_OPTIONS_ERROR;
|
||||
|
||||
// If the previous filter in the chain cannot be a non-last
|
||||
// filter, the chain is invalid.
|
||||
if (!non_last_ok)
|
||||
return LZMA_OPTIONS_ERROR;
|
||||
|
||||
non_last_ok = features[j].non_last_ok;
|
||||
last_ok = features[j].last_ok;
|
||||
changes_size_count += features[j].changes_size;
|
||||
|
||||
} while (filters[++i].id != LZMA_VLI_UNKNOWN);
|
||||
|
||||
// There must be 1-4 filters. The last filter must be usable as
|
||||
// the last filter in the chain. A maximum of three filters are
|
||||
// allowed to change the size of the data.
|
||||
if (i > LZMA_FILTERS_MAX || !last_ok || changes_size_count > 3)
|
||||
return LZMA_OPTIONS_ERROR;
|
||||
|
||||
*count = i;
|
||||
return LZMA_OK;
|
||||
}
|
||||
|
||||
|
||||
extern lzma_ret
|
||||
lzma_raw_coder_init(lzma_next_coder *next, const lzma_allocator *allocator,
|
||||
const lzma_filter *options,
|
||||
lzma_filter_find coder_find, bool is_encoder)
|
||||
{
|
||||
// Do some basic validation and get the number of filters.
|
||||
size_t count;
|
||||
return_if_error(validate_chain(options, &count));
|
||||
|
||||
// Set the filter functions and copy the options pointer.
|
||||
lzma_filter_info filters[LZMA_FILTERS_MAX + 1];
|
||||
if (is_encoder) {
|
||||
for (size_t i = 0; i < count; ++i) {
|
||||
// The order of the filters is reversed in the
|
||||
// encoder. It allows more efficient handling
|
||||
// of the uncompressed data.
|
||||
const size_t j = count - i - 1;
|
||||
|
||||
const lzma_filter_coder *const fc
|
||||
= coder_find(options[i].id);
|
||||
if (fc == NULL || fc->init == NULL)
|
||||
return LZMA_OPTIONS_ERROR;
|
||||
|
||||
filters[j].id = options[i].id;
|
||||
filters[j].init = fc->init;
|
||||
filters[j].options = options[i].options;
|
||||
}
|
||||
} else {
|
||||
for (size_t i = 0; i < count; ++i) {
|
||||
const lzma_filter_coder *const fc
|
||||
= coder_find(options[i].id);
|
||||
if (fc == NULL || fc->init == NULL)
|
||||
return LZMA_OPTIONS_ERROR;
|
||||
|
||||
filters[i].id = options[i].id;
|
||||
filters[i].init = fc->init;
|
||||
filters[i].options = options[i].options;
|
||||
}
|
||||
}
|
||||
|
||||
// Terminate the array.
|
||||
filters[count].id = LZMA_VLI_UNKNOWN;
|
||||
filters[count].init = NULL;
|
||||
|
||||
// Initialize the filters.
|
||||
const lzma_ret ret = lzma_next_filter_init(next, allocator, filters);
|
||||
if (ret != LZMA_OK)
|
||||
lzma_next_end(next, allocator);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
extern uint64_t
|
||||
lzma_raw_coder_memusage(lzma_filter_find coder_find,
|
||||
const lzma_filter *filters)
|
||||
{
|
||||
// The chain has to have at least one filter.
|
||||
{
|
||||
size_t tmp;
|
||||
if (validate_chain(filters, &tmp) != LZMA_OK)
|
||||
return UINT64_MAX;
|
||||
}
|
||||
|
||||
uint64_t total = 0;
|
||||
size_t i = 0;
|
||||
|
||||
do {
|
||||
const lzma_filter_coder *const fc
|
||||
= coder_find(filters[i].id);
|
||||
if (fc == NULL)
|
||||
return UINT64_MAX; // Unsupported Filter ID
|
||||
|
||||
if (fc->memusage == NULL) {
|
||||
// This filter doesn't have a function to calculate
|
||||
// the memory usage and validate the options. Such
|
||||
// filters need only little memory, so we use 1 KiB
|
||||
// as a good estimate. They also accept all possible
|
||||
// options, so there's no need to worry about lack
|
||||
// of validation.
|
||||
total += 1024;
|
||||
} else {
|
||||
// Call the filter-specific memory usage calculation
|
||||
// function.
|
||||
const uint64_t usage
|
||||
= fc->memusage(filters[i].options);
|
||||
if (usage == UINT64_MAX)
|
||||
return UINT64_MAX; // Invalid options
|
||||
|
||||
total += usage;
|
||||
}
|
||||
} while (filters[++i].id != LZMA_VLI_UNKNOWN);
|
||||
|
||||
// Add some fixed amount of extra. It's to compensate memory usage
|
||||
// of Stream, Block etc. coders, malloc() overhead, stack etc.
|
||||
return total + LZMA_MEMUSAGE_BASE;
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
/// \file filter_common.c
|
||||
/// \brief Filter-specific stuff common for both encoder and decoder
|
||||
//
|
||||
// Author: Lasse Collin
|
||||
//
|
||||
// This file has been put into the public domain.
|
||||
// You can do whatever you want with this file.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef LZMA_FILTER_COMMON_H
|
||||
#define LZMA_FILTER_COMMON_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
|
||||
/// Both lzma_filter_encoder and lzma_filter_decoder begin with these members.
|
||||
typedef struct {
|
||||
/// Filter ID
|
||||
lzma_vli id;
|
||||
|
||||
/// Initializes the filter encoder and calls lzma_next_filter_init()
|
||||
/// for filters + 1.
|
||||
lzma_init_function init;
|
||||
|
||||
/// Calculates memory usage of the encoder. If the options are
|
||||
/// invalid, UINT64_MAX is returned.
|
||||
uint64_t (*memusage)(const void *options);
|
||||
|
||||
} lzma_filter_coder;
|
||||
|
||||
|
||||
typedef const lzma_filter_coder *(*lzma_filter_find)(lzma_vli id);
|
||||
|
||||
|
||||
extern lzma_ret lzma_raw_coder_init(
|
||||
lzma_next_coder *next, const lzma_allocator *allocator,
|
||||
const lzma_filter *filters,
|
||||
lzma_filter_find coder_find, bool is_encoder);
|
||||
|
||||
|
||||
extern uint64_t lzma_raw_coder_memusage(lzma_filter_find coder_find,
|
||||
const lzma_filter *filters);
|
||||
|
||||
|
||||
#endif
|
||||
@@ -1,195 +0,0 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
/// \file filter_decoder.c
|
||||
/// \brief Filter ID mapping to filter-specific functions
|
||||
//
|
||||
// Author: Lasse Collin
|
||||
//
|
||||
// This file has been put into the public domain.
|
||||
// You can do whatever you want with this file.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "filter_decoder.h"
|
||||
#include "filter_common.h"
|
||||
#include "lzma_decoder.h"
|
||||
#ifdef HAVE_DECODER_LZMA2
|
||||
#include "lzma2_decoder.h"
|
||||
#endif
|
||||
#if defined(HAVE_DECODER_X86) || \
|
||||
defined(HAVE_DECODER_POWERPC) || \
|
||||
defined(HAVE_DECODER_IA64) || \
|
||||
defined(HAVE_DECODER_ARM) || \
|
||||
defined(HAVE_DECODER_ARMTHUMB) || \
|
||||
defined(HAVE_DECODER_SPARC)
|
||||
#include "simple_decoder.h"
|
||||
#endif
|
||||
#ifdef HAVE_DECODER_DELTA
|
||||
#include "delta_decoder.h"
|
||||
#endif
|
||||
|
||||
|
||||
typedef struct {
|
||||
/// Filter ID
|
||||
lzma_vli id;
|
||||
|
||||
/// Initializes the filter encoder and calls lzma_next_filter_init()
|
||||
/// for filters + 1.
|
||||
lzma_init_function init;
|
||||
|
||||
/// Calculates memory usage of the encoder. If the options are
|
||||
/// invalid, UINT64_MAX is returned.
|
||||
uint64_t (*memusage)(const void *options);
|
||||
|
||||
/// Decodes Filter Properties.
|
||||
///
|
||||
/// \return - LZMA_OK: Properties decoded successfully.
|
||||
/// - LZMA_OPTIONS_ERROR: Unsupported properties
|
||||
/// - LZMA_MEM_ERROR: Memory allocation failed.
|
||||
lzma_ret (*props_decode)(
|
||||
void **options, const lzma_allocator *allocator,
|
||||
const uint8_t *props, size_t props_size);
|
||||
|
||||
} lzma_filter_decoder;
|
||||
|
||||
|
||||
static const lzma_filter_decoder decoders[] = {
|
||||
#ifdef HAVE_DECODER_LZMA1
|
||||
{
|
||||
.id = LZMA_FILTER_LZMA1,
|
||||
.init = &lzma_lzma_decoder_init,
|
||||
.memusage = &lzma_lzma_decoder_memusage,
|
||||
.props_decode = &lzma_lzma_props_decode,
|
||||
},
|
||||
#endif
|
||||
#ifdef HAVE_DECODER_LZMA2
|
||||
{
|
||||
.id = LZMA_FILTER_LZMA2,
|
||||
.init = &lzma_lzma2_decoder_init,
|
||||
.memusage = &lzma_lzma2_decoder_memusage,
|
||||
.props_decode = &lzma_lzma2_props_decode,
|
||||
},
|
||||
#endif
|
||||
#ifdef HAVE_DECODER_X86
|
||||
{
|
||||
.id = LZMA_FILTER_X86,
|
||||
.init = &lzma_simple_x86_decoder_init,
|
||||
.memusage = NULL,
|
||||
.props_decode = &lzma_simple_props_decode,
|
||||
},
|
||||
#endif
|
||||
#ifdef HAVE_DECODER_POWERPC
|
||||
{
|
||||
.id = LZMA_FILTER_POWERPC,
|
||||
.init = &lzma_simple_powerpc_decoder_init,
|
||||
.memusage = NULL,
|
||||
.props_decode = &lzma_simple_props_decode,
|
||||
},
|
||||
#endif
|
||||
#ifdef HAVE_DECODER_IA64
|
||||
{
|
||||
.id = LZMA_FILTER_IA64,
|
||||
.init = &lzma_simple_ia64_decoder_init,
|
||||
.memusage = NULL,
|
||||
.props_decode = &lzma_simple_props_decode,
|
||||
},
|
||||
#endif
|
||||
#ifdef HAVE_DECODER_ARM
|
||||
{
|
||||
.id = LZMA_FILTER_ARM,
|
||||
.init = &lzma_simple_arm_decoder_init,
|
||||
.memusage = NULL,
|
||||
.props_decode = &lzma_simple_props_decode,
|
||||
},
|
||||
#endif
|
||||
#ifdef HAVE_DECODER_ARMTHUMB
|
||||
{
|
||||
.id = LZMA_FILTER_ARMTHUMB,
|
||||
.init = &lzma_simple_armthumb_decoder_init,
|
||||
.memusage = NULL,
|
||||
.props_decode = &lzma_simple_props_decode,
|
||||
},
|
||||
#endif
|
||||
#ifdef HAVE_DECODER_SPARC
|
||||
{
|
||||
.id = LZMA_FILTER_SPARC,
|
||||
.init = &lzma_simple_sparc_decoder_init,
|
||||
.memusage = NULL,
|
||||
.props_decode = &lzma_simple_props_decode,
|
||||
},
|
||||
#endif
|
||||
#ifdef HAVE_DECODER_DELTA
|
||||
{
|
||||
.id = LZMA_FILTER_DELTA,
|
||||
.init = &lzma_delta_decoder_init,
|
||||
.memusage = &lzma_delta_coder_memusage,
|
||||
.props_decode = &lzma_delta_props_decode,
|
||||
},
|
||||
#endif
|
||||
};
|
||||
|
||||
|
||||
static const lzma_filter_decoder *
|
||||
decoder_find(lzma_vli id)
|
||||
{
|
||||
for (size_t i = 0; i < ARRAY_SIZE(decoders); ++i)
|
||||
if (decoders[i].id == id)
|
||||
return decoders + i;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
extern LZMA_API(lzma_bool)
|
||||
lzma_filter_decoder_is_supported(lzma_vli id)
|
||||
{
|
||||
return decoder_find(id) != NULL;
|
||||
}
|
||||
|
||||
|
||||
extern lzma_ret
|
||||
lzma_raw_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
|
||||
const lzma_filter *options)
|
||||
{
|
||||
return lzma_raw_coder_init(next, allocator,
|
||||
options, (lzma_filter_find)(&decoder_find), false);
|
||||
}
|
||||
|
||||
|
||||
extern LZMA_API(lzma_ret)
|
||||
lzma_raw_decoder(lzma_stream *strm, const lzma_filter *options)
|
||||
{
|
||||
lzma_next_strm_init(lzma_raw_decoder_init, strm, options);
|
||||
|
||||
strm->internal->supported_actions[LZMA_RUN] = true;
|
||||
strm->internal->supported_actions[LZMA_FINISH] = true;
|
||||
|
||||
return LZMA_OK;
|
||||
}
|
||||
|
||||
|
||||
extern LZMA_API(uint64_t)
|
||||
lzma_raw_decoder_memusage(const lzma_filter *filters)
|
||||
{
|
||||
return lzma_raw_coder_memusage(
|
||||
(lzma_filter_find)(&decoder_find), filters);
|
||||
}
|
||||
|
||||
|
||||
extern LZMA_API(lzma_ret)
|
||||
lzma_properties_decode(lzma_filter *filter, const lzma_allocator *allocator,
|
||||
const uint8_t *props, size_t props_size)
|
||||
{
|
||||
// Make it always NULL so that the caller can always safely free() it.
|
||||
filter->options = NULL;
|
||||
|
||||
const lzma_filter_decoder *const fd = decoder_find(filter->id);
|
||||
if (fd == NULL)
|
||||
return LZMA_OPTIONS_ERROR;
|
||||
|
||||
if (fd->props_decode == NULL)
|
||||
return props_size == 0 ? LZMA_OK : LZMA_OPTIONS_ERROR;
|
||||
|
||||
return fd->props_decode(
|
||||
&filter->options, allocator, props, props_size);
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
/// \file filter_decoder.c
|
||||
/// \brief Filter ID mapping to filter-specific functions
|
||||
//
|
||||
// Author: Lasse Collin
|
||||
//
|
||||
// This file has been put into the public domain.
|
||||
// You can do whatever you want with this file.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef LZMA_FILTER_DECODER_H
|
||||
#define LZMA_FILTER_DECODER_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
|
||||
extern lzma_ret lzma_raw_decoder_init(
|
||||
lzma_next_coder *next, const lzma_allocator *allocator,
|
||||
const lzma_filter *options);
|
||||
|
||||
#endif
|
||||
@@ -11,7 +11,6 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "filter_encoder.h"
|
||||
#include "filter_common.h"
|
||||
#include "lzma_encoder.h"
|
||||
#ifdef HAVE_DECODER_LZMA2
|
||||
#include "lzma2_encoder.h"
|
||||
@@ -164,7 +163,8 @@ static const lzma_filter_encoder encoders[] = {
|
||||
static const lzma_filter_encoder *
|
||||
encoder_find(lzma_vli id)
|
||||
{
|
||||
for (size_t i = 0; i < ARRAY_SIZE(encoders); ++i)
|
||||
size_t i = 0;
|
||||
for (i = 0; i < ARRAY_SIZE(encoders); ++i)
|
||||
if (encoders[i].id == id)
|
||||
return encoders + i;
|
||||
|
||||
@@ -179,70 +179,13 @@ lzma_filter_encoder_is_supported(lzma_vli id)
|
||||
}
|
||||
|
||||
|
||||
extern LZMA_API(lzma_ret)
|
||||
lzma_filters_update(lzma_stream *strm, const lzma_filter *filters)
|
||||
{
|
||||
if (strm->internal->next.update == NULL)
|
||||
return LZMA_PROG_ERROR;
|
||||
|
||||
// Validate the filter chain.
|
||||
if (lzma_raw_encoder_memusage(filters) == UINT64_MAX)
|
||||
return LZMA_OPTIONS_ERROR;
|
||||
|
||||
// The actual filter chain in the encoder is reversed. Some things
|
||||
// still want the normal order chain, so we provide both.
|
||||
size_t count = 1;
|
||||
while (filters[count].id != LZMA_VLI_UNKNOWN)
|
||||
++count;
|
||||
|
||||
lzma_filter reversed_filters[LZMA_FILTERS_MAX + 1];
|
||||
for (size_t i = 0; i < count; ++i)
|
||||
reversed_filters[count - i - 1] = filters[i];
|
||||
|
||||
reversed_filters[count].id = LZMA_VLI_UNKNOWN;
|
||||
|
||||
return strm->internal->next.update(strm->internal->next.coder,
|
||||
strm->allocator, filters, reversed_filters);
|
||||
}
|
||||
|
||||
|
||||
extern lzma_ret
|
||||
lzma_raw_encoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
|
||||
const lzma_filter *options)
|
||||
{
|
||||
return lzma_raw_coder_init(next, allocator,
|
||||
options, (lzma_filter_find)(&encoder_find), true);
|
||||
}
|
||||
|
||||
|
||||
extern LZMA_API(lzma_ret)
|
||||
lzma_raw_encoder(lzma_stream *strm, const lzma_filter *options)
|
||||
{
|
||||
lzma_next_strm_init(lzma_raw_coder_init, strm, options,
|
||||
(lzma_filter_find)(&encoder_find), true);
|
||||
|
||||
strm->internal->supported_actions[LZMA_RUN] = true;
|
||||
strm->internal->supported_actions[LZMA_SYNC_FLUSH] = true;
|
||||
strm->internal->supported_actions[LZMA_FINISH] = true;
|
||||
|
||||
return LZMA_OK;
|
||||
}
|
||||
|
||||
|
||||
extern LZMA_API(uint64_t)
|
||||
lzma_raw_encoder_memusage(const lzma_filter *filters)
|
||||
{
|
||||
return lzma_raw_coder_memusage(
|
||||
(lzma_filter_find)(&encoder_find), filters);
|
||||
}
|
||||
|
||||
|
||||
extern uint64_t
|
||||
lzma_mt_block_size(const lzma_filter *filters)
|
||||
{
|
||||
uint64_t max = 0;
|
||||
size_t i = 0;
|
||||
|
||||
for (size_t i = 0; filters[i].id != LZMA_VLI_UNKNOWN; ++i) {
|
||||
for (i = 0; filters[i].id != LZMA_VLI_UNKNOWN; ++i) {
|
||||
const lzma_filter_encoder *const fe
|
||||
= encoder_find(filters[i].id);
|
||||
if (fe->block_size != NULL) {
|
||||
|
||||
@@ -1,46 +0,0 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
/// \file filter_flags_decoder.c
|
||||
/// \brief Decodes a Filter Flags field
|
||||
//
|
||||
// Author: Lasse Collin
|
||||
//
|
||||
// This file has been put into the public domain.
|
||||
// You can do whatever you want with this file.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "filter_decoder.h"
|
||||
|
||||
|
||||
extern LZMA_API(lzma_ret)
|
||||
lzma_filter_flags_decode(
|
||||
lzma_filter *filter, const lzma_allocator *allocator,
|
||||
const uint8_t *in, size_t *in_pos, size_t in_size)
|
||||
{
|
||||
// Set the pointer to NULL so the caller can always safely free it.
|
||||
filter->options = NULL;
|
||||
|
||||
// Filter ID
|
||||
return_if_error(lzma_vli_decode(&filter->id, NULL,
|
||||
in, in_pos, in_size));
|
||||
|
||||
if (filter->id >= LZMA_FILTER_RESERVED_START)
|
||||
return LZMA_DATA_ERROR;
|
||||
|
||||
// Size of Properties
|
||||
lzma_vli props_size;
|
||||
return_if_error(lzma_vli_decode(&props_size, NULL,
|
||||
in, in_pos, in_size));
|
||||
|
||||
// Filter Properties
|
||||
if (in_size - *in_pos < props_size)
|
||||
return LZMA_DATA_ERROR;
|
||||
|
||||
const lzma_ret ret = lzma_properties_decode(
|
||||
filter, allocator, in + *in_pos, props_size);
|
||||
|
||||
*in_pos += props_size;
|
||||
|
||||
return ret;
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
/// \file filter_flags_encoder.c
|
||||
/// \brief Decodes a Filter Flags field
|
||||
//
|
||||
// Author: Lasse Collin
|
||||
//
|
||||
// This file has been put into the public domain.
|
||||
// You can do whatever you want with this file.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "filter_encoder.h"
|
||||
|
||||
|
||||
extern LZMA_API(lzma_ret)
|
||||
lzma_filter_flags_size(uint32_t *size, const lzma_filter *filter)
|
||||
{
|
||||
if (filter->id >= LZMA_FILTER_RESERVED_START)
|
||||
return LZMA_PROG_ERROR;
|
||||
|
||||
return_if_error(lzma_properties_size(size, filter));
|
||||
|
||||
*size += lzma_vli_size(filter->id) + lzma_vli_size(*size);
|
||||
|
||||
return LZMA_OK;
|
||||
}
|
||||
|
||||
|
||||
extern LZMA_API(lzma_ret)
|
||||
lzma_filter_flags_encode(const lzma_filter *filter,
|
||||
uint8_t *out, size_t *out_pos, size_t out_size)
|
||||
{
|
||||
// Filter ID
|
||||
if (filter->id >= LZMA_FILTER_RESERVED_START)
|
||||
return LZMA_PROG_ERROR;
|
||||
|
||||
return_if_error(lzma_vli_encode(filter->id, NULL,
|
||||
out, out_pos, out_size));
|
||||
|
||||
// Size of Properties
|
||||
uint32_t props_size;
|
||||
return_if_error(lzma_properties_size(&props_size, filter));
|
||||
return_if_error(lzma_vli_encode(props_size, NULL,
|
||||
out, out_pos, out_size));
|
||||
|
||||
// Filter Properties
|
||||
if (out_size - *out_pos < props_size)
|
||||
return LZMA_PROG_ERROR;
|
||||
|
||||
return_if_error(lzma_properties_encode(filter, out + *out_pos));
|
||||
|
||||
*out_pos += props_size;
|
||||
|
||||
return LZMA_OK;
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
/// \file hardware_cputhreads.c
|
||||
/// \brief Get the number of CPU threads or cores
|
||||
//
|
||||
// Author: Lasse Collin
|
||||
//
|
||||
// This file has been put into the public domain.
|
||||
// You can do whatever you want with this file.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include "tuklib_cpucores.h"
|
||||
|
||||
|
||||
extern LZMA_API(uint32_t)
|
||||
lzma_cputhreads(void)
|
||||
{
|
||||
return tuklib_cpucores();
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
/// \file hardware_physmem.c
|
||||
/// \brief Get the total amount of physical memory (RAM)
|
||||
//
|
||||
// Author: Jonathan Nieder
|
||||
//
|
||||
// This file has been put into the public domain.
|
||||
// You can do whatever you want with this file.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include "tuklib_physmem.h"
|
||||
|
||||
|
||||
extern LZMA_API(uint64_t)
|
||||
lzma_physmem(void)
|
||||
{
|
||||
// It is simpler to make lzma_physmem() a wrapper for
|
||||
// tuklib_physmem() than to hack appropriate symbol visiblity
|
||||
// support for the tuklib modules.
|
||||
return tuklib_physmem();
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,345 +0,0 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
/// \file index_decoder.c
|
||||
/// \brief Decodes the Index field
|
||||
//
|
||||
// Author: Lasse Collin
|
||||
//
|
||||
// This file has been put into the public domain.
|
||||
// You can do whatever you want with this file.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "index.h"
|
||||
#include "check.h"
|
||||
|
||||
|
||||
struct lzma_coder_s {
|
||||
enum {
|
||||
SEQ_INDICATOR,
|
||||
SEQ_COUNT,
|
||||
SEQ_MEMUSAGE,
|
||||
SEQ_UNPADDED,
|
||||
SEQ_UNCOMPRESSED,
|
||||
SEQ_PADDING_INIT,
|
||||
SEQ_PADDING,
|
||||
SEQ_CRC32,
|
||||
} sequence;
|
||||
|
||||
/// Memory usage limit
|
||||
uint64_t memlimit;
|
||||
|
||||
/// Target Index
|
||||
lzma_index *index;
|
||||
|
||||
/// Pointer give by the application, which is set after
|
||||
/// successful decoding.
|
||||
lzma_index **index_ptr;
|
||||
|
||||
/// Number of Records left to decode.
|
||||
lzma_vli count;
|
||||
|
||||
/// The most recent Unpadded Size field
|
||||
lzma_vli unpadded_size;
|
||||
|
||||
/// The most recent Uncompressed Size field
|
||||
lzma_vli uncompressed_size;
|
||||
|
||||
/// Position in integers
|
||||
size_t pos;
|
||||
|
||||
/// CRC32 of the List of Records field
|
||||
uint32_t crc32;
|
||||
};
|
||||
|
||||
|
||||
static lzma_ret
|
||||
index_decode(lzma_coder *coder, const lzma_allocator *allocator,
|
||||
const uint8_t *restrict in, size_t *restrict in_pos,
|
||||
size_t in_size,
|
||||
uint8_t *restrict out lzma_attribute((__unused__)),
|
||||
size_t *restrict out_pos lzma_attribute((__unused__)),
|
||||
size_t out_size lzma_attribute((__unused__)),
|
||||
lzma_action action lzma_attribute((__unused__)))
|
||||
{
|
||||
// Similar optimization as in index_encoder.c
|
||||
const size_t in_start = *in_pos;
|
||||
lzma_ret ret = LZMA_OK;
|
||||
|
||||
while (*in_pos < in_size)
|
||||
switch (coder->sequence) {
|
||||
case SEQ_INDICATOR:
|
||||
// Return LZMA_DATA_ERROR instead of e.g. LZMA_PROG_ERROR or
|
||||
// LZMA_FORMAT_ERROR, because a typical usage case for Index
|
||||
// decoder is when parsing the Stream backwards. If seeking
|
||||
// backward from the Stream Footer gives us something that
|
||||
// doesn't begin with Index Indicator, the file is considered
|
||||
// corrupt, not "programming error" or "unrecognized file
|
||||
// format". One could argue that the application should
|
||||
// verify the Index Indicator before trying to decode the
|
||||
// Index, but well, I suppose it is simpler this way.
|
||||
if (in[(*in_pos)++] != 0x00)
|
||||
return LZMA_DATA_ERROR;
|
||||
|
||||
coder->sequence = SEQ_COUNT;
|
||||
break;
|
||||
|
||||
case SEQ_COUNT:
|
||||
ret = lzma_vli_decode(&coder->count, &coder->pos,
|
||||
in, in_pos, in_size);
|
||||
if (ret != LZMA_STREAM_END)
|
||||
goto out;
|
||||
|
||||
coder->pos = 0;
|
||||
coder->sequence = SEQ_MEMUSAGE;
|
||||
|
||||
// Fall through
|
||||
|
||||
case SEQ_MEMUSAGE:
|
||||
if (lzma_index_memusage(1, coder->count) > coder->memlimit) {
|
||||
ret = LZMA_MEMLIMIT_ERROR;
|
||||
goto out;
|
||||
}
|
||||
|
||||
// Tell the Index handling code how many Records this
|
||||
// Index has to allow it to allocate memory more efficiently.
|
||||
lzma_index_prealloc(coder->index, coder->count);
|
||||
|
||||
ret = LZMA_OK;
|
||||
coder->sequence = coder->count == 0
|
||||
? SEQ_PADDING_INIT : SEQ_UNPADDED;
|
||||
break;
|
||||
|
||||
case SEQ_UNPADDED:
|
||||
case SEQ_UNCOMPRESSED: {
|
||||
lzma_vli *size = coder->sequence == SEQ_UNPADDED
|
||||
? &coder->unpadded_size
|
||||
: &coder->uncompressed_size;
|
||||
|
||||
ret = lzma_vli_decode(size, &coder->pos,
|
||||
in, in_pos, in_size);
|
||||
if (ret != LZMA_STREAM_END)
|
||||
goto out;
|
||||
|
||||
ret = LZMA_OK;
|
||||
coder->pos = 0;
|
||||
|
||||
if (coder->sequence == SEQ_UNPADDED) {
|
||||
// Validate that encoded Unpadded Size isn't too small
|
||||
// or too big.
|
||||
if (coder->unpadded_size < UNPADDED_SIZE_MIN
|
||||
|| coder->unpadded_size
|
||||
> UNPADDED_SIZE_MAX)
|
||||
return LZMA_DATA_ERROR;
|
||||
|
||||
coder->sequence = SEQ_UNCOMPRESSED;
|
||||
} else {
|
||||
// Add the decoded Record to the Index.
|
||||
return_if_error(lzma_index_append(
|
||||
coder->index, allocator,
|
||||
coder->unpadded_size,
|
||||
coder->uncompressed_size));
|
||||
|
||||
// Check if this was the last Record.
|
||||
coder->sequence = --coder->count == 0
|
||||
? SEQ_PADDING_INIT
|
||||
: SEQ_UNPADDED;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case SEQ_PADDING_INIT:
|
||||
coder->pos = lzma_index_padding_size(coder->index);
|
||||
coder->sequence = SEQ_PADDING;
|
||||
|
||||
// Fall through
|
||||
|
||||
case SEQ_PADDING:
|
||||
if (coder->pos > 0) {
|
||||
--coder->pos;
|
||||
if (in[(*in_pos)++] != 0x00)
|
||||
return LZMA_DATA_ERROR;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
// Finish the CRC32 calculation.
|
||||
coder->crc32 = lzma_crc32(in + in_start,
|
||||
*in_pos - in_start, coder->crc32);
|
||||
|
||||
coder->sequence = SEQ_CRC32;
|
||||
|
||||
// Fall through
|
||||
|
||||
case SEQ_CRC32:
|
||||
do {
|
||||
if (*in_pos == in_size)
|
||||
return LZMA_OK;
|
||||
|
||||
if (((coder->crc32 >> (coder->pos * 8)) & 0xFF)
|
||||
!= in[(*in_pos)++])
|
||||
return LZMA_DATA_ERROR;
|
||||
|
||||
} while (++coder->pos < 4);
|
||||
|
||||
// Decoding was successful, now we can let the application
|
||||
// see the decoded Index.
|
||||
*coder->index_ptr = coder->index;
|
||||
|
||||
// Make index NULL so we don't free it unintentionally.
|
||||
coder->index = NULL;
|
||||
|
||||
return LZMA_STREAM_END;
|
||||
|
||||
default:
|
||||
assert(0);
|
||||
return LZMA_PROG_ERROR;
|
||||
}
|
||||
|
||||
out:
|
||||
// Update the CRC32,
|
||||
coder->crc32 = lzma_crc32(in + in_start,
|
||||
*in_pos - in_start, coder->crc32);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
index_decoder_end(lzma_coder *coder, const lzma_allocator *allocator)
|
||||
{
|
||||
lzma_index_end(coder->index, allocator);
|
||||
lzma_free(coder, allocator);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
static lzma_ret
|
||||
index_decoder_memconfig(lzma_coder *coder, uint64_t *memusage,
|
||||
uint64_t *old_memlimit, uint64_t new_memlimit)
|
||||
{
|
||||
*memusage = lzma_index_memusage(1, coder->count);
|
||||
*old_memlimit = coder->memlimit;
|
||||
|
||||
if (new_memlimit != 0) {
|
||||
if (new_memlimit < *memusage)
|
||||
return LZMA_MEMLIMIT_ERROR;
|
||||
|
||||
coder->memlimit = new_memlimit;
|
||||
}
|
||||
|
||||
return LZMA_OK;
|
||||
}
|
||||
|
||||
|
||||
static lzma_ret
|
||||
index_decoder_reset(lzma_coder *coder, const lzma_allocator *allocator,
|
||||
lzma_index **i, uint64_t memlimit)
|
||||
{
|
||||
// Remember the pointer given by the application. We will set it
|
||||
// to point to the decoded Index only if decoding is successful.
|
||||
// Before that, keep it NULL so that applications can always safely
|
||||
// pass it to lzma_index_end() no matter did decoding succeed or not.
|
||||
coder->index_ptr = i;
|
||||
*i = NULL;
|
||||
|
||||
// We always allocate a new lzma_index.
|
||||
coder->index = lzma_index_init(allocator);
|
||||
if (coder->index == NULL)
|
||||
return LZMA_MEM_ERROR;
|
||||
|
||||
// Initialize the rest.
|
||||
coder->sequence = SEQ_INDICATOR;
|
||||
coder->memlimit = memlimit;
|
||||
coder->count = 0; // Needs to be initialized due to _memconfig().
|
||||
coder->pos = 0;
|
||||
coder->crc32 = 0;
|
||||
|
||||
return LZMA_OK;
|
||||
}
|
||||
|
||||
|
||||
static lzma_ret
|
||||
index_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
|
||||
lzma_index **i, uint64_t memlimit)
|
||||
{
|
||||
lzma_next_coder_init(&index_decoder_init, next, allocator);
|
||||
|
||||
if (i == NULL || memlimit == 0)
|
||||
return LZMA_PROG_ERROR;
|
||||
|
||||
if (next->coder == NULL) {
|
||||
next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
|
||||
if (next->coder == NULL)
|
||||
return LZMA_MEM_ERROR;
|
||||
|
||||
next->code = &index_decode;
|
||||
next->end = &index_decoder_end;
|
||||
next->memconfig = &index_decoder_memconfig;
|
||||
next->coder->index = NULL;
|
||||
} else {
|
||||
lzma_index_end(next->coder->index, allocator);
|
||||
}
|
||||
|
||||
return index_decoder_reset(next->coder, allocator, i, memlimit);
|
||||
}
|
||||
|
||||
|
||||
extern LZMA_API(lzma_ret)
|
||||
lzma_index_decoder(lzma_stream *strm, lzma_index **i, uint64_t memlimit)
|
||||
{
|
||||
lzma_next_strm_init(index_decoder_init, strm, i, memlimit);
|
||||
|
||||
strm->internal->supported_actions[LZMA_RUN] = true;
|
||||
strm->internal->supported_actions[LZMA_FINISH] = true;
|
||||
|
||||
return LZMA_OK;
|
||||
}
|
||||
|
||||
|
||||
extern LZMA_API(lzma_ret)
|
||||
lzma_index_buffer_decode(lzma_index **i, uint64_t *memlimit,
|
||||
const lzma_allocator *allocator,
|
||||
const uint8_t *in, size_t *in_pos, size_t in_size)
|
||||
{
|
||||
// Sanity checks
|
||||
if (i == NULL || memlimit == NULL
|
||||
|| in == NULL || in_pos == NULL || *in_pos > in_size)
|
||||
return LZMA_PROG_ERROR;
|
||||
|
||||
// Initialize the decoder.
|
||||
lzma_coder coder;
|
||||
return_if_error(index_decoder_reset(&coder, allocator, i, *memlimit));
|
||||
|
||||
// Store the input start position so that we can restore it in case
|
||||
// of an error.
|
||||
const size_t in_start = *in_pos;
|
||||
|
||||
// Do the actual decoding.
|
||||
lzma_ret ret = index_decode(&coder, allocator, in, in_pos, in_size,
|
||||
NULL, NULL, 0, LZMA_RUN);
|
||||
|
||||
if (ret == LZMA_STREAM_END) {
|
||||
ret = LZMA_OK;
|
||||
} else {
|
||||
// Something went wrong, free the Index structure and restore
|
||||
// the input position.
|
||||
lzma_index_end(coder.index, allocator);
|
||||
*in_pos = in_start;
|
||||
|
||||
if (ret == LZMA_OK) {
|
||||
// The input is truncated or otherwise corrupt.
|
||||
// Use LZMA_DATA_ERROR instead of LZMA_BUF_ERROR
|
||||
// like lzma_vli_decode() does in single-call mode.
|
||||
ret = LZMA_DATA_ERROR;
|
||||
|
||||
} else if (ret == LZMA_MEMLIMIT_ERROR) {
|
||||
// Tell the caller how much memory would have
|
||||
// been needed.
|
||||
*memlimit = lzma_index_memusage(1, coder.count);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
@@ -1,254 +0,0 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
/// \file index_encoder.c
|
||||
/// \brief Encodes the Index field
|
||||
//
|
||||
// Author: Lasse Collin
|
||||
//
|
||||
// This file has been put into the public domain.
|
||||
// You can do whatever you want with this file.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "index_encoder.h"
|
||||
#include "index.h"
|
||||
#include "check.h"
|
||||
|
||||
|
||||
struct lzma_coder_s {
|
||||
enum {
|
||||
SEQ_INDICATOR,
|
||||
SEQ_COUNT,
|
||||
SEQ_UNPADDED,
|
||||
SEQ_UNCOMPRESSED,
|
||||
SEQ_NEXT,
|
||||
SEQ_PADDING,
|
||||
SEQ_CRC32,
|
||||
} sequence;
|
||||
|
||||
/// Index being encoded
|
||||
const lzma_index *index;
|
||||
|
||||
/// Iterator for the Index being encoded
|
||||
lzma_index_iter iter;
|
||||
|
||||
/// Position in integers
|
||||
size_t pos;
|
||||
|
||||
/// CRC32 of the List of Records field
|
||||
uint32_t crc32;
|
||||
};
|
||||
|
||||
|
||||
static lzma_ret
|
||||
index_encode(lzma_coder *coder,
|
||||
const lzma_allocator *allocator lzma_attribute((__unused__)),
|
||||
const uint8_t *restrict in lzma_attribute((__unused__)),
|
||||
size_t *restrict in_pos lzma_attribute((__unused__)),
|
||||
size_t in_size lzma_attribute((__unused__)),
|
||||
uint8_t *restrict out, size_t *restrict out_pos,
|
||||
size_t out_size,
|
||||
lzma_action action lzma_attribute((__unused__)))
|
||||
{
|
||||
// Position where to start calculating CRC32. The idea is that we
|
||||
// need to call lzma_crc32() only once per call to index_encode().
|
||||
const size_t out_start = *out_pos;
|
||||
|
||||
// Return value to use if we return at the end of this function.
|
||||
// We use "goto out" to jump out of the while-switch construct
|
||||
// instead of returning directly, because that way we don't need
|
||||
// to copypaste the lzma_crc32() call to many places.
|
||||
lzma_ret ret = LZMA_OK;
|
||||
|
||||
while (*out_pos < out_size)
|
||||
switch (coder->sequence) {
|
||||
case SEQ_INDICATOR:
|
||||
out[*out_pos] = 0x00;
|
||||
++*out_pos;
|
||||
coder->sequence = SEQ_COUNT;
|
||||
break;
|
||||
|
||||
case SEQ_COUNT: {
|
||||
const lzma_vli count = lzma_index_block_count(coder->index);
|
||||
ret = lzma_vli_encode(count, &coder->pos,
|
||||
out, out_pos, out_size);
|
||||
if (ret != LZMA_STREAM_END)
|
||||
goto out;
|
||||
|
||||
ret = LZMA_OK;
|
||||
coder->pos = 0;
|
||||
coder->sequence = SEQ_NEXT;
|
||||
break;
|
||||
}
|
||||
|
||||
case SEQ_NEXT:
|
||||
if (lzma_index_iter_next(
|
||||
&coder->iter, LZMA_INDEX_ITER_BLOCK)) {
|
||||
// Get the size of the Index Padding field.
|
||||
coder->pos = lzma_index_padding_size(coder->index);
|
||||
assert(coder->pos <= 3);
|
||||
coder->sequence = SEQ_PADDING;
|
||||
break;
|
||||
}
|
||||
|
||||
coder->sequence = SEQ_UNPADDED;
|
||||
|
||||
// Fall through
|
||||
|
||||
case SEQ_UNPADDED:
|
||||
case SEQ_UNCOMPRESSED: {
|
||||
const lzma_vli size = coder->sequence == SEQ_UNPADDED
|
||||
? coder->iter.block.unpadded_size
|
||||
: coder->iter.block.uncompressed_size;
|
||||
|
||||
ret = lzma_vli_encode(size, &coder->pos,
|
||||
out, out_pos, out_size);
|
||||
if (ret != LZMA_STREAM_END)
|
||||
goto out;
|
||||
|
||||
ret = LZMA_OK;
|
||||
coder->pos = 0;
|
||||
|
||||
// Advance to SEQ_UNCOMPRESSED or SEQ_NEXT.
|
||||
++coder->sequence;
|
||||
break;
|
||||
}
|
||||
|
||||
case SEQ_PADDING:
|
||||
if (coder->pos > 0) {
|
||||
--coder->pos;
|
||||
out[(*out_pos)++] = 0x00;
|
||||
break;
|
||||
}
|
||||
|
||||
// Finish the CRC32 calculation.
|
||||
coder->crc32 = lzma_crc32(out + out_start,
|
||||
*out_pos - out_start, coder->crc32);
|
||||
|
||||
coder->sequence = SEQ_CRC32;
|
||||
|
||||
// Fall through
|
||||
|
||||
case SEQ_CRC32:
|
||||
// We don't use the main loop, because we don't want
|
||||
// coder->crc32 to be touched anymore.
|
||||
do {
|
||||
if (*out_pos == out_size)
|
||||
return LZMA_OK;
|
||||
|
||||
out[*out_pos] = (coder->crc32 >> (coder->pos * 8))
|
||||
& 0xFF;
|
||||
++*out_pos;
|
||||
|
||||
} while (++coder->pos < 4);
|
||||
|
||||
return LZMA_STREAM_END;
|
||||
|
||||
default:
|
||||
assert(0);
|
||||
return LZMA_PROG_ERROR;
|
||||
}
|
||||
|
||||
out:
|
||||
// Update the CRC32.
|
||||
coder->crc32 = lzma_crc32(out + out_start,
|
||||
*out_pos - out_start, coder->crc32);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
index_encoder_end(lzma_coder *coder, const lzma_allocator *allocator)
|
||||
{
|
||||
lzma_free(coder, allocator);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
index_encoder_reset(lzma_coder *coder, const lzma_index *i)
|
||||
{
|
||||
lzma_index_iter_init(&coder->iter, i);
|
||||
|
||||
coder->sequence = SEQ_INDICATOR;
|
||||
coder->index = i;
|
||||
coder->pos = 0;
|
||||
coder->crc32 = 0;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
extern lzma_ret
|
||||
lzma_index_encoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
|
||||
const lzma_index *i)
|
||||
{
|
||||
lzma_next_coder_init(&lzma_index_encoder_init, next, allocator);
|
||||
|
||||
if (i == NULL)
|
||||
return LZMA_PROG_ERROR;
|
||||
|
||||
if (next->coder == NULL) {
|
||||
next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
|
||||
if (next->coder == NULL)
|
||||
return LZMA_MEM_ERROR;
|
||||
|
||||
next->code = &index_encode;
|
||||
next->end = &index_encoder_end;
|
||||
}
|
||||
|
||||
index_encoder_reset(next->coder, i);
|
||||
|
||||
return LZMA_OK;
|
||||
}
|
||||
|
||||
|
||||
extern LZMA_API(lzma_ret)
|
||||
lzma_index_encoder(lzma_stream *strm, const lzma_index *i)
|
||||
{
|
||||
lzma_next_strm_init(lzma_index_encoder_init, strm, i);
|
||||
|
||||
strm->internal->supported_actions[LZMA_RUN] = true;
|
||||
strm->internal->supported_actions[LZMA_FINISH] = true;
|
||||
|
||||
return LZMA_OK;
|
||||
}
|
||||
|
||||
|
||||
extern LZMA_API(lzma_ret)
|
||||
lzma_index_buffer_encode(const lzma_index *i,
|
||||
uint8_t *out, size_t *out_pos, size_t out_size)
|
||||
{
|
||||
// Validate the arguments.
|
||||
if (i == NULL || out == NULL || out_pos == NULL || *out_pos > out_size)
|
||||
return LZMA_PROG_ERROR;
|
||||
|
||||
// Don't try to encode if there's not enough output space.
|
||||
if (out_size - *out_pos < lzma_index_size(i))
|
||||
return LZMA_BUF_ERROR;
|
||||
|
||||
// The Index encoder needs just one small data structure so we can
|
||||
// allocate it on stack.
|
||||
lzma_coder coder;
|
||||
index_encoder_reset(&coder, i);
|
||||
|
||||
// Do the actual encoding. This should never fail, but store
|
||||
// the original *out_pos just in case.
|
||||
const size_t out_start = *out_pos;
|
||||
lzma_ret ret = index_encode(&coder, NULL, NULL, NULL, 0,
|
||||
out, out_pos, out_size, LZMA_RUN);
|
||||
|
||||
if (ret == LZMA_STREAM_END) {
|
||||
ret = LZMA_OK;
|
||||
} else {
|
||||
// We should never get here, but just in case, restore the
|
||||
// output position and set the error accordingly if something
|
||||
// goes wrong and debugging isn't enabled.
|
||||
assert(0);
|
||||
*out_pos = out_start;
|
||||
ret = LZMA_PROG_ERROR;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
/// \file index_encoder.h
|
||||
/// \brief Encodes the Index field
|
||||
//
|
||||
// Author: Lasse Collin
|
||||
//
|
||||
// This file has been put into the public domain.
|
||||
// You can do whatever you want with this file.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef LZMA_INDEX_ENCODER_H
|
||||
#define LZMA_INDEX_ENCODER_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
|
||||
extern lzma_ret lzma_index_encoder_init(lzma_next_coder *next,
|
||||
const lzma_allocator *allocator, const lzma_index *i);
|
||||
|
||||
|
||||
#endif
|
||||
@@ -1,334 +0,0 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
/// \file index_hash.c
|
||||
/// \brief Validates Index by using a hash function
|
||||
//
|
||||
// Author: Lasse Collin
|
||||
//
|
||||
// This file has been put into the public domain.
|
||||
// You can do whatever you want with this file.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "common.h"
|
||||
#include "index.h"
|
||||
#include "check.h"
|
||||
|
||||
|
||||
typedef struct {
|
||||
/// Sum of the Block sizes (including Block Padding)
|
||||
lzma_vli blocks_size;
|
||||
|
||||
/// Sum of the Uncompressed Size fields
|
||||
lzma_vli uncompressed_size;
|
||||
|
||||
/// Number of Records
|
||||
lzma_vli count;
|
||||
|
||||
/// Size of the List of Index Records as bytes
|
||||
lzma_vli index_list_size;
|
||||
|
||||
/// Check calculated from Unpadded Sizes and Uncompressed Sizes.
|
||||
lzma_check_state check;
|
||||
|
||||
} lzma_index_hash_info;
|
||||
|
||||
|
||||
struct lzma_index_hash_s {
|
||||
enum {
|
||||
SEQ_BLOCK,
|
||||
SEQ_COUNT,
|
||||
SEQ_UNPADDED,
|
||||
SEQ_UNCOMPRESSED,
|
||||
SEQ_PADDING_INIT,
|
||||
SEQ_PADDING,
|
||||
SEQ_CRC32,
|
||||
} sequence;
|
||||
|
||||
/// Information collected while decoding the actual Blocks.
|
||||
lzma_index_hash_info blocks;
|
||||
|
||||
/// Information collected from the Index field.
|
||||
lzma_index_hash_info records;
|
||||
|
||||
/// Number of Records not fully decoded
|
||||
lzma_vli remaining;
|
||||
|
||||
/// Unpadded Size currently being read from an Index Record.
|
||||
lzma_vli unpadded_size;
|
||||
|
||||
/// Uncompressed Size currently being read from an Index Record.
|
||||
lzma_vli uncompressed_size;
|
||||
|
||||
/// Position in variable-length integers when decoding them from
|
||||
/// the List of Records.
|
||||
size_t pos;
|
||||
|
||||
/// CRC32 of the Index
|
||||
uint32_t crc32;
|
||||
};
|
||||
|
||||
|
||||
extern LZMA_API(lzma_index_hash *)
|
||||
lzma_index_hash_init(lzma_index_hash *index_hash,
|
||||
const lzma_allocator *allocator)
|
||||
{
|
||||
if (index_hash == NULL) {
|
||||
index_hash = lzma_alloc(sizeof(lzma_index_hash), allocator);
|
||||
if (index_hash == NULL)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
index_hash->sequence = SEQ_BLOCK;
|
||||
index_hash->blocks.blocks_size = 0;
|
||||
index_hash->blocks.uncompressed_size = 0;
|
||||
index_hash->blocks.count = 0;
|
||||
index_hash->blocks.index_list_size = 0;
|
||||
index_hash->records.blocks_size = 0;
|
||||
index_hash->records.uncompressed_size = 0;
|
||||
index_hash->records.count = 0;
|
||||
index_hash->records.index_list_size = 0;
|
||||
index_hash->unpadded_size = 0;
|
||||
index_hash->uncompressed_size = 0;
|
||||
index_hash->pos = 0;
|
||||
index_hash->crc32 = 0;
|
||||
|
||||
// These cannot fail because LZMA_CHECK_BEST is known to be supported.
|
||||
(void)lzma_check_init(&index_hash->blocks.check, LZMA_CHECK_BEST);
|
||||
(void)lzma_check_init(&index_hash->records.check, LZMA_CHECK_BEST);
|
||||
|
||||
return index_hash;
|
||||
}
|
||||
|
||||
|
||||
extern LZMA_API(void)
|
||||
lzma_index_hash_end(lzma_index_hash *index_hash,
|
||||
const lzma_allocator *allocator)
|
||||
{
|
||||
lzma_free(index_hash, allocator);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
extern LZMA_API(lzma_vli)
|
||||
lzma_index_hash_size(const lzma_index_hash *index_hash)
|
||||
{
|
||||
// Get the size of the Index from ->blocks instead of ->records for
|
||||
// cases where application wants to know the Index Size before
|
||||
// decoding the Index.
|
||||
return index_size(index_hash->blocks.count,
|
||||
index_hash->blocks.index_list_size);
|
||||
}
|
||||
|
||||
|
||||
/// Updates the sizes and the hash without any validation.
|
||||
static lzma_ret
|
||||
hash_append(lzma_index_hash_info *info, lzma_vli unpadded_size,
|
||||
lzma_vli uncompressed_size)
|
||||
{
|
||||
info->blocks_size += vli_ceil4(unpadded_size);
|
||||
info->uncompressed_size += uncompressed_size;
|
||||
info->index_list_size += lzma_vli_size(unpadded_size)
|
||||
+ lzma_vli_size(uncompressed_size);
|
||||
++info->count;
|
||||
|
||||
const lzma_vli sizes[2] = { unpadded_size, uncompressed_size };
|
||||
lzma_check_update(&info->check, LZMA_CHECK_BEST,
|
||||
(const uint8_t *)(sizes), sizeof(sizes));
|
||||
|
||||
return LZMA_OK;
|
||||
}
|
||||
|
||||
|
||||
extern LZMA_API(lzma_ret)
|
||||
lzma_index_hash_append(lzma_index_hash *index_hash, lzma_vli unpadded_size,
|
||||
lzma_vli uncompressed_size)
|
||||
{
|
||||
// Validate the arguments.
|
||||
if (index_hash->sequence != SEQ_BLOCK
|
||||
|| unpadded_size < UNPADDED_SIZE_MIN
|
||||
|| unpadded_size > UNPADDED_SIZE_MAX
|
||||
|| uncompressed_size > LZMA_VLI_MAX)
|
||||
return LZMA_PROG_ERROR;
|
||||
|
||||
// Update the hash.
|
||||
return_if_error(hash_append(&index_hash->blocks,
|
||||
unpadded_size, uncompressed_size));
|
||||
|
||||
// Validate the properties of *info are still in allowed limits.
|
||||
if (index_hash->blocks.blocks_size > LZMA_VLI_MAX
|
||||
|| index_hash->blocks.uncompressed_size > LZMA_VLI_MAX
|
||||
|| index_size(index_hash->blocks.count,
|
||||
index_hash->blocks.index_list_size)
|
||||
> LZMA_BACKWARD_SIZE_MAX
|
||||
|| index_stream_size(index_hash->blocks.blocks_size,
|
||||
index_hash->blocks.count,
|
||||
index_hash->blocks.index_list_size)
|
||||
> LZMA_VLI_MAX)
|
||||
return LZMA_DATA_ERROR;
|
||||
|
||||
return LZMA_OK;
|
||||
}
|
||||
|
||||
|
||||
extern LZMA_API(lzma_ret)
|
||||
lzma_index_hash_decode(lzma_index_hash *index_hash, const uint8_t *in,
|
||||
size_t *in_pos, size_t in_size)
|
||||
{
|
||||
// Catch zero input buffer here, because in contrast to Index encoder
|
||||
// and decoder functions, applications call this function directly
|
||||
// instead of via lzma_code(), which does the buffer checking.
|
||||
if (*in_pos >= in_size)
|
||||
return LZMA_BUF_ERROR;
|
||||
|
||||
// NOTE: This function has many similarities to index_encode() and
|
||||
// index_decode() functions found from index_encoder.c and
|
||||
// index_decoder.c. See the comments especially in index_encoder.c.
|
||||
const size_t in_start = *in_pos;
|
||||
lzma_ret ret = LZMA_OK;
|
||||
|
||||
while (*in_pos < in_size)
|
||||
switch (index_hash->sequence) {
|
||||
case SEQ_BLOCK:
|
||||
// Check the Index Indicator is present.
|
||||
if (in[(*in_pos)++] != 0x00)
|
||||
return LZMA_DATA_ERROR;
|
||||
|
||||
index_hash->sequence = SEQ_COUNT;
|
||||
break;
|
||||
|
||||
case SEQ_COUNT: {
|
||||
ret = lzma_vli_decode(&index_hash->remaining,
|
||||
&index_hash->pos, in, in_pos, in_size);
|
||||
if (ret != LZMA_STREAM_END)
|
||||
goto out;
|
||||
|
||||
// The count must match the count of the Blocks decoded.
|
||||
if (index_hash->remaining != index_hash->blocks.count)
|
||||
return LZMA_DATA_ERROR;
|
||||
|
||||
ret = LZMA_OK;
|
||||
index_hash->pos = 0;
|
||||
|
||||
// Handle the special case when there are no Blocks.
|
||||
index_hash->sequence = index_hash->remaining == 0
|
||||
? SEQ_PADDING_INIT : SEQ_UNPADDED;
|
||||
break;
|
||||
}
|
||||
|
||||
case SEQ_UNPADDED:
|
||||
case SEQ_UNCOMPRESSED: {
|
||||
lzma_vli *size = index_hash->sequence == SEQ_UNPADDED
|
||||
? &index_hash->unpadded_size
|
||||
: &index_hash->uncompressed_size;
|
||||
|
||||
ret = lzma_vli_decode(size, &index_hash->pos,
|
||||
in, in_pos, in_size);
|
||||
if (ret != LZMA_STREAM_END)
|
||||
goto out;
|
||||
|
||||
ret = LZMA_OK;
|
||||
index_hash->pos = 0;
|
||||
|
||||
if (index_hash->sequence == SEQ_UNPADDED) {
|
||||
if (index_hash->unpadded_size < UNPADDED_SIZE_MIN
|
||||
|| index_hash->unpadded_size
|
||||
> UNPADDED_SIZE_MAX)
|
||||
return LZMA_DATA_ERROR;
|
||||
|
||||
index_hash->sequence = SEQ_UNCOMPRESSED;
|
||||
} else {
|
||||
// Update the hash.
|
||||
return_if_error(hash_append(&index_hash->records,
|
||||
index_hash->unpadded_size,
|
||||
index_hash->uncompressed_size));
|
||||
|
||||
// Verify that we don't go over the known sizes. Note
|
||||
// that this validation is simpler than the one used
|
||||
// in lzma_index_hash_append(), because here we know
|
||||
// that values in index_hash->blocks are already
|
||||
// validated and we are fine as long as we don't
|
||||
// exceed them in index_hash->records.
|
||||
if (index_hash->blocks.blocks_size
|
||||
< index_hash->records.blocks_size
|
||||
|| index_hash->blocks.uncompressed_size
|
||||
< index_hash->records.uncompressed_size
|
||||
|| index_hash->blocks.index_list_size
|
||||
< index_hash->records.index_list_size)
|
||||
return LZMA_DATA_ERROR;
|
||||
|
||||
// Check if this was the last Record.
|
||||
index_hash->sequence = --index_hash->remaining == 0
|
||||
? SEQ_PADDING_INIT : SEQ_UNPADDED;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case SEQ_PADDING_INIT:
|
||||
index_hash->pos = (LZMA_VLI_C(4) - index_size_unpadded(
|
||||
index_hash->records.count,
|
||||
index_hash->records.index_list_size)) & 3;
|
||||
index_hash->sequence = SEQ_PADDING;
|
||||
|
||||
// Fall through
|
||||
|
||||
case SEQ_PADDING:
|
||||
if (index_hash->pos > 0) {
|
||||
--index_hash->pos;
|
||||
if (in[(*in_pos)++] != 0x00)
|
||||
return LZMA_DATA_ERROR;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
// Compare the sizes.
|
||||
if (index_hash->blocks.blocks_size
|
||||
!= index_hash->records.blocks_size
|
||||
|| index_hash->blocks.uncompressed_size
|
||||
!= index_hash->records.uncompressed_size
|
||||
|| index_hash->blocks.index_list_size
|
||||
!= index_hash->records.index_list_size)
|
||||
return LZMA_DATA_ERROR;
|
||||
|
||||
// Finish the hashes and compare them.
|
||||
lzma_check_finish(&index_hash->blocks.check, LZMA_CHECK_BEST);
|
||||
lzma_check_finish(&index_hash->records.check, LZMA_CHECK_BEST);
|
||||
if (memcmp(index_hash->blocks.check.buffer.u8,
|
||||
index_hash->records.check.buffer.u8,
|
||||
lzma_check_size(LZMA_CHECK_BEST)) != 0)
|
||||
return LZMA_DATA_ERROR;
|
||||
|
||||
// Finish the CRC32 calculation.
|
||||
index_hash->crc32 = lzma_crc32(in + in_start,
|
||||
*in_pos - in_start, index_hash->crc32);
|
||||
|
||||
index_hash->sequence = SEQ_CRC32;
|
||||
|
||||
// Fall through
|
||||
|
||||
case SEQ_CRC32:
|
||||
do {
|
||||
if (*in_pos == in_size)
|
||||
return LZMA_OK;
|
||||
|
||||
if (((index_hash->crc32 >> (index_hash->pos * 8))
|
||||
& 0xFF) != in[(*in_pos)++])
|
||||
return LZMA_DATA_ERROR;
|
||||
|
||||
} while (++index_hash->pos < 4);
|
||||
|
||||
return LZMA_STREAM_END;
|
||||
|
||||
default:
|
||||
assert(0);
|
||||
return LZMA_PROG_ERROR;
|
||||
}
|
||||
|
||||
out:
|
||||
// Update the CRC32,
|
||||
index_hash->crc32 = lzma_crc32(in + in_start,
|
||||
*in_pos - in_start, index_hash->crc32);
|
||||
|
||||
return ret;
|
||||
}
|
||||
@@ -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;
|
||||
@@ -102,7 +107,7 @@ lzma_memcmplen(const uint8_t *buf1, const uint8_t *buf2,
|
||||
# if defined(__INTEL_COMPILER)
|
||||
len += _bit_scan_forward(x);
|
||||
# elif defined(_MSC_VER)
|
||||
unsigned long tmp;
|
||||
unsigned long tmp = 0;
|
||||
_BitScanForward(&tmp, x);
|
||||
len += tmp;
|
||||
# else
|
||||
|
||||
@@ -1,521 +0,0 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
/// \file mythread.h
|
||||
/// \brief Some threading related helper macros and functions
|
||||
//
|
||||
// Author: Lasse Collin
|
||||
//
|
||||
// This file has been put into the public domain.
|
||||
// You can do whatever you want with this file.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef MYTHREAD_H
|
||||
#define MYTHREAD_H
|
||||
|
||||
#include "sysdefs.h"
|
||||
|
||||
// If any type of threading is enabled, #define MYTHREAD_ENABLED.
|
||||
#if defined(MYTHREAD_POSIX) || defined(MYTHREAD_WIN95) \
|
||||
|| defined(MYTHREAD_VISTA)
|
||||
# define MYTHREAD_ENABLED 1
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef MYTHREAD_ENABLED
|
||||
|
||||
////////////////////////////////////////
|
||||
// Shared between all threading types //
|
||||
////////////////////////////////////////
|
||||
|
||||
// Locks a mutex for a duration of a block.
|
||||
//
|
||||
// Perform mythread_mutex_lock(&mutex) in the beginning of a block
|
||||
// and mythread_mutex_unlock(&mutex) at the end of the block. "break"
|
||||
// may be used to unlock the mutex and jump out of the block.
|
||||
// mythread_sync blocks may be nested.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// mythread_sync(mutex) {
|
||||
// foo();
|
||||
// if (some_error)
|
||||
// break; // Skips bar()
|
||||
// bar();
|
||||
// }
|
||||
//
|
||||
// At least GCC optimizes the loops completely away so it doesn't slow
|
||||
// things down at all compared to plain mythread_mutex_lock(&mutex)
|
||||
// and mythread_mutex_unlock(&mutex) calls.
|
||||
//
|
||||
#define mythread_sync(mutex) mythread_sync_helper1(mutex, __LINE__)
|
||||
#define mythread_sync_helper1(mutex, line) mythread_sync_helper2(mutex, line)
|
||||
#define mythread_sync_helper2(mutex, line) \
|
||||
for (unsigned int mythread_i_ ## line = 0; \
|
||||
mythread_i_ ## line \
|
||||
? (mythread_mutex_unlock(&(mutex)), 0) \
|
||||
: (mythread_mutex_lock(&(mutex)), 1); \
|
||||
mythread_i_ ## line = 1) \
|
||||
for (unsigned int mythread_j_ ## line = 0; \
|
||||
!mythread_j_ ## line; \
|
||||
mythread_j_ ## line = 1)
|
||||
#endif
|
||||
|
||||
|
||||
#if !defined(MYTHREAD_ENABLED)
|
||||
|
||||
//////////////////
|
||||
// No threading //
|
||||
//////////////////
|
||||
|
||||
// Calls the given function once. This isn't thread safe.
|
||||
#define mythread_once(func) \
|
||||
do { \
|
||||
static bool once_ = false; \
|
||||
if (!once_) { \
|
||||
func(); \
|
||||
once_ = true; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
|
||||
#if !(defined(_WIN32) && !defined(__CYGWIN__))
|
||||
// Use sigprocmask() to set the signal mask in single-threaded programs.
|
||||
#include <signal.h>
|
||||
|
||||
static inline void
|
||||
mythread_sigmask(int how, const sigset_t *restrict set,
|
||||
sigset_t *restrict oset)
|
||||
{
|
||||
int ret = sigprocmask(how, set, oset);
|
||||
assert(ret == 0);
|
||||
(void)ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#elif defined(MYTHREAD_POSIX)
|
||||
|
||||
////////////////////
|
||||
// Using pthreads //
|
||||
////////////////////
|
||||
|
||||
#include <sys/time.h>
|
||||
#include <pthread.h>
|
||||
#include <signal.h>
|
||||
#include <time.h>
|
||||
#include <errno.h>
|
||||
|
||||
#define MYTHREAD_RET_TYPE void *
|
||||
#define MYTHREAD_RET_VALUE NULL
|
||||
|
||||
typedef pthread_t mythread;
|
||||
typedef pthread_mutex_t mythread_mutex;
|
||||
|
||||
typedef struct {
|
||||
pthread_cond_t cond;
|
||||
#ifdef HAVE_CLOCK_GETTIME
|
||||
// Clock ID (CLOCK_REALTIME or CLOCK_MONOTONIC) associated with
|
||||
// the condition variable.
|
||||
clockid_t clk_id;
|
||||
#endif
|
||||
} mythread_cond;
|
||||
|
||||
typedef struct timespec mythread_condtime;
|
||||
|
||||
|
||||
// Calls the given function once in a thread-safe way.
|
||||
#define mythread_once(func) \
|
||||
do { \
|
||||
static pthread_once_t once_ = PTHREAD_ONCE_INIT; \
|
||||
pthread_once(&once_, &func); \
|
||||
} while (0)
|
||||
|
||||
|
||||
// Use pthread_sigmask() to set the signal mask in multi-threaded programs.
|
||||
// Do nothing on OpenVMS since it lacks pthread_sigmask().
|
||||
static inline void
|
||||
mythread_sigmask(int how, const sigset_t *restrict set,
|
||||
sigset_t *restrict oset)
|
||||
{
|
||||
#ifdef __VMS
|
||||
(void)how;
|
||||
(void)set;
|
||||
(void)oset;
|
||||
#else
|
||||
int ret = pthread_sigmask(how, set, oset);
|
||||
assert(ret == 0);
|
||||
(void)ret;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
// Creates a new thread with all signals blocked. Returns zero on success
|
||||
// and non-zero on error.
|
||||
static inline int
|
||||
mythread_create(mythread *thread, void *(*func)(void *arg), void *arg)
|
||||
{
|
||||
sigset_t old;
|
||||
sigset_t all;
|
||||
sigfillset(&all);
|
||||
|
||||
mythread_sigmask(SIG_SETMASK, &all, &old);
|
||||
const int ret = pthread_create(thread, NULL, func, arg);
|
||||
mythread_sigmask(SIG_SETMASK, &old, NULL);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Joins a thread. Returns zero on success and non-zero on error.
|
||||
static inline int
|
||||
mythread_join(mythread thread)
|
||||
{
|
||||
return pthread_join(thread, NULL);
|
||||
}
|
||||
|
||||
|
||||
// Initiatlizes a mutex. Returns zero on success and non-zero on error.
|
||||
static inline int
|
||||
mythread_mutex_init(mythread_mutex *mutex)
|
||||
{
|
||||
return pthread_mutex_init(mutex, NULL);
|
||||
}
|
||||
|
||||
static inline void
|
||||
mythread_mutex_destroy(mythread_mutex *mutex)
|
||||
{
|
||||
int ret = pthread_mutex_destroy(mutex);
|
||||
assert(ret == 0);
|
||||
(void)ret;
|
||||
}
|
||||
|
||||
static inline void
|
||||
mythread_mutex_lock(mythread_mutex *mutex)
|
||||
{
|
||||
int ret = pthread_mutex_lock(mutex);
|
||||
assert(ret == 0);
|
||||
(void)ret;
|
||||
}
|
||||
|
||||
static inline void
|
||||
mythread_mutex_unlock(mythread_mutex *mutex)
|
||||
{
|
||||
int ret = pthread_mutex_unlock(mutex);
|
||||
assert(ret == 0);
|
||||
(void)ret;
|
||||
}
|
||||
|
||||
|
||||
// Initializes a condition variable.
|
||||
//
|
||||
// Using CLOCK_MONOTONIC instead of the default CLOCK_REALTIME makes the
|
||||
// timeout in pthread_cond_timedwait() work correctly also if system time
|
||||
// is suddenly changed. Unfortunately CLOCK_MONOTONIC isn't available
|
||||
// everywhere while the default CLOCK_REALTIME is, so the default is
|
||||
// used if CLOCK_MONOTONIC isn't available.
|
||||
//
|
||||
// If clock_gettime() isn't available at all, gettimeofday() will be used.
|
||||
static inline int
|
||||
mythread_cond_init(mythread_cond *mycond)
|
||||
{
|
||||
#ifdef HAVE_CLOCK_GETTIME
|
||||
// NOTE: HAVE_DECL_CLOCK_MONOTONIC is always defined to 0 or 1.
|
||||
# if defined(HAVE_PTHREAD_CONDATTR_SETCLOCK) && HAVE_DECL_CLOCK_MONOTONIC
|
||||
struct timespec ts;
|
||||
pthread_condattr_t condattr;
|
||||
|
||||
// POSIX doesn't seem to *require* that pthread_condattr_setclock()
|
||||
// will fail if given an unsupported clock ID. Test that
|
||||
// CLOCK_MONOTONIC really is supported using clock_gettime().
|
||||
if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0
|
||||
&& pthread_condattr_init(&condattr) == 0) {
|
||||
int ret = pthread_condattr_setclock(
|
||||
&condattr, CLOCK_MONOTONIC);
|
||||
if (ret == 0)
|
||||
ret = pthread_cond_init(&mycond->cond, &condattr);
|
||||
|
||||
pthread_condattr_destroy(&condattr);
|
||||
|
||||
if (ret == 0) {
|
||||
mycond->clk_id = CLOCK_MONOTONIC;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// If anything above fails, fall back to the default CLOCK_REALTIME.
|
||||
// POSIX requires that all implementations of clock_gettime() must
|
||||
// support at least CLOCK_REALTIME.
|
||||
# endif
|
||||
|
||||
mycond->clk_id = CLOCK_REALTIME;
|
||||
#endif
|
||||
|
||||
return pthread_cond_init(&mycond->cond, NULL);
|
||||
}
|
||||
|
||||
static inline void
|
||||
mythread_cond_destroy(mythread_cond *cond)
|
||||
{
|
||||
int ret = pthread_cond_destroy(&cond->cond);
|
||||
assert(ret == 0);
|
||||
(void)ret;
|
||||
}
|
||||
|
||||
static inline void
|
||||
mythread_cond_signal(mythread_cond *cond)
|
||||
{
|
||||
int ret = pthread_cond_signal(&cond->cond);
|
||||
assert(ret == 0);
|
||||
(void)ret;
|
||||
}
|
||||
|
||||
static inline void
|
||||
mythread_cond_wait(mythread_cond *cond, mythread_mutex *mutex)
|
||||
{
|
||||
int ret = pthread_cond_wait(&cond->cond, mutex);
|
||||
assert(ret == 0);
|
||||
(void)ret;
|
||||
}
|
||||
|
||||
// Waits on a condition or until a timeout expires. If the timeout expires,
|
||||
// non-zero is returned, otherwise zero is returned.
|
||||
static inline int
|
||||
mythread_cond_timedwait(mythread_cond *cond, mythread_mutex *mutex,
|
||||
const mythread_condtime *condtime)
|
||||
{
|
||||
int ret = pthread_cond_timedwait(&cond->cond, mutex, condtime);
|
||||
assert(ret == 0 || ret == ETIMEDOUT);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Sets condtime to the absolute time that is timeout_ms milliseconds
|
||||
// in the future. The type of the clock to use is taken from cond.
|
||||
static inline void
|
||||
mythread_condtime_set(mythread_condtime *condtime, const mythread_cond *cond,
|
||||
uint32_t timeout_ms)
|
||||
{
|
||||
condtime->tv_sec = timeout_ms / 1000;
|
||||
condtime->tv_nsec = (timeout_ms % 1000) * 1000000;
|
||||
|
||||
#ifdef HAVE_CLOCK_GETTIME
|
||||
struct timespec now;
|
||||
int ret = clock_gettime(cond->clk_id, &now);
|
||||
assert(ret == 0);
|
||||
(void)ret;
|
||||
|
||||
condtime->tv_sec += now.tv_sec;
|
||||
condtime->tv_nsec += now.tv_nsec;
|
||||
#else
|
||||
(void)cond;
|
||||
|
||||
struct timeval now;
|
||||
gettimeofday(&now, NULL);
|
||||
|
||||
condtime->tv_sec += now.tv_sec;
|
||||
condtime->tv_nsec += now.tv_usec * 1000L;
|
||||
#endif
|
||||
|
||||
// tv_nsec must stay in the range [0, 999_999_999].
|
||||
if (condtime->tv_nsec >= 1000000000L) {
|
||||
condtime->tv_nsec -= 1000000000L;
|
||||
++condtime->tv_sec;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#elif defined(MYTHREAD_WIN95) || defined(MYTHREAD_VISTA)
|
||||
|
||||
/////////////////////
|
||||
// Windows threads //
|
||||
/////////////////////
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#ifdef MYTHREAD_VISTA
|
||||
# undef _WIN32_WINNT
|
||||
# define _WIN32_WINNT 0x0600
|
||||
#endif
|
||||
#include <windows.h>
|
||||
#include <process.h>
|
||||
|
||||
#define MYTHREAD_RET_TYPE unsigned int __stdcall
|
||||
#define MYTHREAD_RET_VALUE 0
|
||||
|
||||
typedef HANDLE mythread;
|
||||
typedef CRITICAL_SECTION mythread_mutex;
|
||||
|
||||
#ifdef MYTHREAD_WIN95
|
||||
typedef HANDLE mythread_cond;
|
||||
#else
|
||||
typedef CONDITION_VARIABLE mythread_cond;
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
// Tick count (milliseconds) in the beginning of the timeout.
|
||||
// NOTE: This is 32 bits so it wraps around after 49.7 days.
|
||||
// Multi-day timeouts may not work as expected.
|
||||
DWORD start;
|
||||
|
||||
// Length of the timeout in milliseconds. The timeout expires
|
||||
// when the current tick count minus "start" is equal or greater
|
||||
// than "timeout".
|
||||
DWORD timeout;
|
||||
} mythread_condtime;
|
||||
|
||||
|
||||
// mythread_once() is only available with Vista threads.
|
||||
#ifdef MYTHREAD_VISTA
|
||||
#define mythread_once(func) \
|
||||
do { \
|
||||
static INIT_ONCE once_ = INIT_ONCE_STATIC_INIT; \
|
||||
BOOL pending_; \
|
||||
if (!InitOnceBeginInitialize(&once_, 0, &pending_, NULL)) \
|
||||
abort(); \
|
||||
if (pending_) \
|
||||
func(); \
|
||||
if (!InitOnceComplete(&once, 0, NULL)) \
|
||||
abort(); \
|
||||
} while (0)
|
||||
#endif
|
||||
|
||||
|
||||
// mythread_sigmask() isn't available on Windows. Even a dummy version would
|
||||
// make no sense because the other POSIX signal functions are missing anyway.
|
||||
|
||||
|
||||
static inline int
|
||||
mythread_create(mythread *thread,
|
||||
unsigned int (__stdcall *func)(void *arg), void *arg)
|
||||
{
|
||||
uintptr_t ret = _beginthreadex(NULL, 0, func, arg, 0, NULL);
|
||||
if (ret == 0)
|
||||
return -1;
|
||||
|
||||
*thread = (HANDLE)ret;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline int
|
||||
mythread_join(mythread thread)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
if (WaitForSingleObject(thread, INFINITE) != WAIT_OBJECT_0)
|
||||
ret = -1;
|
||||
|
||||
if (!CloseHandle(thread))
|
||||
ret = -1;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
static inline int
|
||||
mythread_mutex_init(mythread_mutex *mutex)
|
||||
{
|
||||
InitializeCriticalSection(mutex);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline void
|
||||
mythread_mutex_destroy(mythread_mutex *mutex)
|
||||
{
|
||||
DeleteCriticalSection(mutex);
|
||||
}
|
||||
|
||||
static inline void
|
||||
mythread_mutex_lock(mythread_mutex *mutex)
|
||||
{
|
||||
EnterCriticalSection(mutex);
|
||||
}
|
||||
|
||||
static inline void
|
||||
mythread_mutex_unlock(mythread_mutex *mutex)
|
||||
{
|
||||
LeaveCriticalSection(mutex);
|
||||
}
|
||||
|
||||
|
||||
static inline int
|
||||
mythread_cond_init(mythread_cond *cond)
|
||||
{
|
||||
#ifdef MYTHREAD_WIN95
|
||||
*cond = CreateEvent(NULL, FALSE, FALSE, NULL);
|
||||
return *cond == NULL ? -1 : 0;
|
||||
#else
|
||||
InitializeConditionVariable(cond);
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
static inline void
|
||||
mythread_cond_destroy(mythread_cond *cond)
|
||||
{
|
||||
#ifdef MYTHREAD_WIN95
|
||||
CloseHandle(*cond);
|
||||
#else
|
||||
(void)cond;
|
||||
#endif
|
||||
}
|
||||
|
||||
static inline void
|
||||
mythread_cond_signal(mythread_cond *cond)
|
||||
{
|
||||
#ifdef MYTHREAD_WIN95
|
||||
SetEvent(*cond);
|
||||
#else
|
||||
WakeConditionVariable(cond);
|
||||
#endif
|
||||
}
|
||||
|
||||
static inline void
|
||||
mythread_cond_wait(mythread_cond *cond, mythread_mutex *mutex)
|
||||
{
|
||||
#ifdef MYTHREAD_WIN95
|
||||
LeaveCriticalSection(mutex);
|
||||
WaitForSingleObject(*cond, INFINITE);
|
||||
EnterCriticalSection(mutex);
|
||||
#else
|
||||
BOOL ret = SleepConditionVariableCS(cond, mutex, INFINITE);
|
||||
assert(ret);
|
||||
(void)ret;
|
||||
#endif
|
||||
}
|
||||
|
||||
static inline int
|
||||
mythread_cond_timedwait(mythread_cond *cond, mythread_mutex *mutex,
|
||||
const mythread_condtime *condtime)
|
||||
{
|
||||
#ifdef MYTHREAD_WIN95
|
||||
LeaveCriticalSection(mutex);
|
||||
#endif
|
||||
|
||||
DWORD elapsed = GetTickCount() - condtime->start;
|
||||
DWORD timeout = elapsed >= condtime->timeout
|
||||
? 0 : condtime->timeout - elapsed;
|
||||
|
||||
#ifdef MYTHREAD_WIN95
|
||||
DWORD ret = WaitForSingleObject(*cond, timeout);
|
||||
assert(ret == WAIT_OBJECT_0 || ret == WAIT_TIMEOUT);
|
||||
|
||||
EnterCriticalSection(mutex);
|
||||
|
||||
return ret == WAIT_TIMEOUT;
|
||||
#else
|
||||
BOOL ret = SleepConditionVariableCS(cond, mutex, timeout);
|
||||
assert(ret || GetLastError() == ERROR_TIMEOUT);
|
||||
return !ret;
|
||||
#endif
|
||||
}
|
||||
|
||||
static inline void
|
||||
mythread_condtime_set(mythread_condtime *condtime, const mythread_cond *cond,
|
||||
uint32_t timeout)
|
||||
{
|
||||
(void)cond;
|
||||
condtime->start = GetTickCount();
|
||||
condtime->timeout = timeout;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,184 +0,0 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
/// \file outqueue.c
|
||||
/// \brief Output queue handling in multithreaded coding
|
||||
//
|
||||
// Author: Lasse Collin
|
||||
//
|
||||
// This file has been put into the public domain.
|
||||
// You can do whatever you want with this file.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "outqueue.h"
|
||||
|
||||
|
||||
/// This is to ease integer overflow checking: We may allocate up to
|
||||
/// 2 * LZMA_THREADS_MAX buffers and we need some extra memory for other
|
||||
/// data structures (that's the second /2).
|
||||
#define BUF_SIZE_MAX (UINT64_MAX / LZMA_THREADS_MAX / 2 / 2)
|
||||
|
||||
|
||||
static lzma_ret
|
||||
get_options(uint64_t *bufs_alloc_size, uint32_t *bufs_count,
|
||||
uint64_t buf_size_max, uint32_t threads)
|
||||
{
|
||||
if (threads > LZMA_THREADS_MAX || buf_size_max > BUF_SIZE_MAX)
|
||||
return LZMA_OPTIONS_ERROR;
|
||||
|
||||
// The number of buffers is twice the number of threads.
|
||||
// This wastes RAM but keeps the threads busy when buffers
|
||||
// finish out of order.
|
||||
//
|
||||
// NOTE: If this is changed, update BUF_SIZE_MAX too.
|
||||
*bufs_count = threads * 2;
|
||||
*bufs_alloc_size = *bufs_count * buf_size_max;
|
||||
|
||||
return LZMA_OK;
|
||||
}
|
||||
|
||||
|
||||
extern uint64_t
|
||||
lzma_outq_memusage(uint64_t buf_size_max, uint32_t threads)
|
||||
{
|
||||
uint64_t bufs_alloc_size;
|
||||
uint32_t bufs_count;
|
||||
|
||||
if (get_options(&bufs_alloc_size, &bufs_count, buf_size_max, threads)
|
||||
!= LZMA_OK)
|
||||
return UINT64_MAX;
|
||||
|
||||
return sizeof(lzma_outq) + bufs_count * sizeof(lzma_outbuf)
|
||||
+ bufs_alloc_size;
|
||||
}
|
||||
|
||||
|
||||
extern lzma_ret
|
||||
lzma_outq_init(lzma_outq *outq, const lzma_allocator *allocator,
|
||||
uint64_t buf_size_max, uint32_t threads)
|
||||
{
|
||||
uint64_t bufs_alloc_size;
|
||||
uint32_t bufs_count;
|
||||
|
||||
// Set bufs_count and bufs_alloc_size.
|
||||
return_if_error(get_options(&bufs_alloc_size, &bufs_count,
|
||||
buf_size_max, threads));
|
||||
|
||||
// Allocate memory if needed.
|
||||
if (outq->buf_size_max != buf_size_max
|
||||
|| outq->bufs_allocated != bufs_count) {
|
||||
lzma_outq_end(outq, allocator);
|
||||
|
||||
#if SIZE_MAX < UINT64_MAX
|
||||
if (bufs_alloc_size > SIZE_MAX)
|
||||
return LZMA_MEM_ERROR;
|
||||
#endif
|
||||
|
||||
outq->bufs = lzma_alloc(bufs_count * sizeof(lzma_outbuf),
|
||||
allocator);
|
||||
outq->bufs_mem = lzma_alloc((size_t)(bufs_alloc_size),
|
||||
allocator);
|
||||
|
||||
if (outq->bufs == NULL || outq->bufs_mem == NULL) {
|
||||
lzma_outq_end(outq, allocator);
|
||||
return LZMA_MEM_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize the rest of the main structure. Initialization of
|
||||
// outq->bufs[] is done when they are actually needed.
|
||||
outq->buf_size_max = (size_t)(buf_size_max);
|
||||
outq->bufs_allocated = bufs_count;
|
||||
outq->bufs_pos = 0;
|
||||
outq->bufs_used = 0;
|
||||
outq->read_pos = 0;
|
||||
|
||||
return LZMA_OK;
|
||||
}
|
||||
|
||||
|
||||
extern void
|
||||
lzma_outq_end(lzma_outq *outq, const lzma_allocator *allocator)
|
||||
{
|
||||
lzma_free(outq->bufs, allocator);
|
||||
outq->bufs = NULL;
|
||||
|
||||
lzma_free(outq->bufs_mem, allocator);
|
||||
outq->bufs_mem = NULL;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
extern lzma_outbuf *
|
||||
lzma_outq_get_buf(lzma_outq *outq)
|
||||
{
|
||||
// Caller must have checked it with lzma_outq_has_buf().
|
||||
assert(outq->bufs_used < outq->bufs_allocated);
|
||||
|
||||
// Initialize the new buffer.
|
||||
lzma_outbuf *buf = &outq->bufs[outq->bufs_pos];
|
||||
buf->buf = outq->bufs_mem + outq->bufs_pos * outq->buf_size_max;
|
||||
buf->size = 0;
|
||||
buf->finished = false;
|
||||
|
||||
// Update the queue state.
|
||||
if (++outq->bufs_pos == outq->bufs_allocated)
|
||||
outq->bufs_pos = 0;
|
||||
|
||||
++outq->bufs_used;
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
|
||||
extern bool
|
||||
lzma_outq_is_readable(const lzma_outq *outq)
|
||||
{
|
||||
uint32_t i = outq->bufs_pos - outq->bufs_used;
|
||||
if (outq->bufs_pos < outq->bufs_used)
|
||||
i += outq->bufs_allocated;
|
||||
|
||||
return outq->bufs[i].finished;
|
||||
}
|
||||
|
||||
|
||||
extern lzma_ret
|
||||
lzma_outq_read(lzma_outq *restrict outq, uint8_t *restrict out,
|
||||
size_t *restrict out_pos, size_t out_size,
|
||||
lzma_vli *restrict unpadded_size,
|
||||
lzma_vli *restrict uncompressed_size)
|
||||
{
|
||||
// There must be at least one buffer from which to read.
|
||||
if (outq->bufs_used == 0)
|
||||
return LZMA_OK;
|
||||
|
||||
// Get the buffer.
|
||||
uint32_t i = outq->bufs_pos - outq->bufs_used;
|
||||
if (outq->bufs_pos < outq->bufs_used)
|
||||
i += outq->bufs_allocated;
|
||||
|
||||
lzma_outbuf *buf = &outq->bufs[i];
|
||||
|
||||
// If it isn't finished yet, we cannot read from it.
|
||||
if (!buf->finished)
|
||||
return LZMA_OK;
|
||||
|
||||
// Copy from the buffer to output.
|
||||
lzma_bufcpy(buf->buf, &outq->read_pos, buf->size,
|
||||
out, out_pos, out_size);
|
||||
|
||||
// Return if we didn't get all the data from the buffer.
|
||||
if (outq->read_pos < buf->size)
|
||||
return LZMA_OK;
|
||||
|
||||
// The buffer was finished. Tell the caller its size information.
|
||||
*unpadded_size = buf->unpadded_size;
|
||||
*uncompressed_size = buf->uncompressed_size;
|
||||
|
||||
// Free this buffer for further use.
|
||||
--outq->bufs_used;
|
||||
outq->read_pos = 0;
|
||||
|
||||
return LZMA_STREAM_END;
|
||||
}
|
||||
@@ -1,156 +0,0 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
/// \file outqueue.h
|
||||
/// \brief Output queue handling in multithreaded coding
|
||||
//
|
||||
// Author: Lasse Collin
|
||||
//
|
||||
// This file has been put into the public domain.
|
||||
// You can do whatever you want with this file.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "common.h"
|
||||
|
||||
|
||||
/// Output buffer for a single thread
|
||||
typedef struct {
|
||||
/// Pointer to the output buffer of lzma_outq.buf_size_max bytes
|
||||
uint8_t *buf;
|
||||
|
||||
/// Amount of data written to buf
|
||||
size_t size;
|
||||
|
||||
/// Additional size information
|
||||
lzma_vli unpadded_size;
|
||||
lzma_vli uncompressed_size;
|
||||
|
||||
/// True when no more data will be written into this buffer.
|
||||
///
|
||||
/// \note This is read by another thread and thus access
|
||||
/// to this variable needs a mutex.
|
||||
bool finished;
|
||||
|
||||
} lzma_outbuf;
|
||||
|
||||
|
||||
typedef struct {
|
||||
/// Array of buffers that are used cyclically.
|
||||
lzma_outbuf *bufs;
|
||||
|
||||
/// Memory allocated for all the buffers
|
||||
uint8_t *bufs_mem;
|
||||
|
||||
/// Amount of buffer space available in each buffer
|
||||
size_t buf_size_max;
|
||||
|
||||
/// Number of buffers allocated
|
||||
uint32_t bufs_allocated;
|
||||
|
||||
/// Position in the bufs array. The next buffer to be taken
|
||||
/// into use is bufs[bufs_pos].
|
||||
uint32_t bufs_pos;
|
||||
|
||||
/// Number of buffers in use
|
||||
uint32_t bufs_used;
|
||||
|
||||
/// Position in the buffer in lzma_outq_read()
|
||||
size_t read_pos;
|
||||
|
||||
} lzma_outq;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Calculate the memory usage of an output queue
|
||||
*
|
||||
* \return Approximate memory usage in bytes or UINT64_MAX on error.
|
||||
*/
|
||||
extern uint64_t lzma_outq_memusage(uint64_t buf_size_max, uint32_t threads);
|
||||
|
||||
|
||||
/// \brief Initialize an output queue
|
||||
///
|
||||
/// \param outq Pointer to an output queue. Before calling
|
||||
/// this function the first time, *outq should
|
||||
/// have been zeroed with memzero() so that this
|
||||
/// function knows that there are no previous
|
||||
/// allocations to free.
|
||||
/// \param allocator Pointer to allocator or NULL
|
||||
/// \param buf_size_max Maximum amount of data that a single buffer
|
||||
/// in the queue may need to store.
|
||||
/// \param threads Number of buffers that may be in use
|
||||
/// concurrently. Note that more than this number
|
||||
/// of buffers will actually get allocated to
|
||||
/// improve performance when buffers finish
|
||||
/// out of order.
|
||||
///
|
||||
/// \return - LZMA_OK
|
||||
/// - LZMA_MEM_ERROR
|
||||
///
|
||||
extern lzma_ret lzma_outq_init(
|
||||
lzma_outq *outq, const lzma_allocator *allocator,
|
||||
uint64_t buf_size_max, uint32_t threads);
|
||||
|
||||
|
||||
/// \brief Free the memory associated with the output queue
|
||||
extern void lzma_outq_end(lzma_outq *outq, const lzma_allocator *allocator);
|
||||
|
||||
|
||||
/// \brief Get a new buffer
|
||||
///
|
||||
/// lzma_outq_has_buf() must be used to check that there is a buffer
|
||||
/// available before calling lzma_outq_get_buf().
|
||||
///
|
||||
extern lzma_outbuf *lzma_outq_get_buf(lzma_outq *outq);
|
||||
|
||||
|
||||
/// \brief Test if there is data ready to be read
|
||||
///
|
||||
/// Call to this function must be protected with the same mutex that
|
||||
/// is used to protect lzma_outbuf.finished.
|
||||
///
|
||||
extern bool lzma_outq_is_readable(const lzma_outq *outq);
|
||||
|
||||
|
||||
/// \brief Read finished data
|
||||
///
|
||||
/// \param outq Pointer to an output queue
|
||||
/// \param out Beginning of the output buffer
|
||||
/// \param out_pos The next byte will be written to
|
||||
/// out[*out_pos].
|
||||
/// \param out_size Size of the out buffer; the first byte into
|
||||
/// which no data is written to is out[out_size].
|
||||
/// \param unpadded_size Unpadded Size from the Block encoder
|
||||
/// \param uncompressed_size Uncompressed Size from the Block encoder
|
||||
///
|
||||
/// \return - LZMA: All OK. Either no data was available or the buffer
|
||||
/// being read didn't become empty yet.
|
||||
/// - LZMA_STREAM_END: The buffer being read was finished.
|
||||
/// *unpadded_size and *uncompressed_size were set.
|
||||
///
|
||||
/// \note This reads lzma_outbuf.finished variables and thus call
|
||||
/// to this function needs to be protected with a mutex.
|
||||
///
|
||||
extern lzma_ret lzma_outq_read(lzma_outq *restrict outq,
|
||||
uint8_t *restrict out, size_t *restrict out_pos,
|
||||
size_t out_size, lzma_vli *restrict unpadded_size,
|
||||
lzma_vli *restrict uncompressed_size);
|
||||
|
||||
|
||||
/// \brief Test if there is at least one buffer free
|
||||
///
|
||||
/// This must be used before getting a new buffer with lzma_outq_get_buf().
|
||||
///
|
||||
static inline bool
|
||||
lzma_outq_has_buf(const lzma_outq *outq)
|
||||
{
|
||||
return outq->bufs_used < outq->bufs_allocated;
|
||||
}
|
||||
|
||||
|
||||
/// \brief Test if the queue is completely empty
|
||||
static inline bool
|
||||
lzma_outq_is_empty(const lzma_outq *outq)
|
||||
{
|
||||
return outq->bufs_used == 0;
|
||||
}
|
||||
@@ -1,91 +0,0 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
/// \file stream_buffer_decoder.c
|
||||
/// \brief Single-call .xz Stream decoder
|
||||
//
|
||||
// Author: Lasse Collin
|
||||
//
|
||||
// This file has been put into the public domain.
|
||||
// You can do whatever you want with this file.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "stream_decoder.h"
|
||||
|
||||
|
||||
extern LZMA_API(lzma_ret)
|
||||
lzma_stream_buffer_decode(uint64_t *memlimit, uint32_t flags,
|
||||
const lzma_allocator *allocator,
|
||||
const uint8_t *in, size_t *in_pos, size_t in_size,
|
||||
uint8_t *out, size_t *out_pos, size_t out_size)
|
||||
{
|
||||
// Sanity checks
|
||||
if (in_pos == NULL || (in == NULL && *in_pos != in_size)
|
||||
|| *in_pos > in_size || out_pos == NULL
|
||||
|| (out == NULL && *out_pos != out_size)
|
||||
|| *out_pos > out_size)
|
||||
return LZMA_PROG_ERROR;
|
||||
|
||||
// Catch flags that are not allowed in buffer-to-buffer decoding.
|
||||
if (flags & LZMA_TELL_ANY_CHECK)
|
||||
return LZMA_PROG_ERROR;
|
||||
|
||||
// Initialize the Stream decoder.
|
||||
// TODO: We need something to tell the decoder that it can use the
|
||||
// output buffer as workspace, and thus save significant amount of RAM.
|
||||
lzma_next_coder stream_decoder = LZMA_NEXT_CODER_INIT;
|
||||
lzma_ret ret = lzma_stream_decoder_init(
|
||||
&stream_decoder, allocator, *memlimit, flags);
|
||||
|
||||
if (ret == LZMA_OK) {
|
||||
// Save the positions so that we can restore them in case
|
||||
// an error occurs.
|
||||
const size_t in_start = *in_pos;
|
||||
const size_t out_start = *out_pos;
|
||||
|
||||
// Do the actual decoding.
|
||||
ret = stream_decoder.code(stream_decoder.coder, allocator,
|
||||
in, in_pos, in_size, out, out_pos, out_size,
|
||||
LZMA_FINISH);
|
||||
|
||||
if (ret == LZMA_STREAM_END) {
|
||||
ret = LZMA_OK;
|
||||
} else {
|
||||
// Something went wrong, restore the positions.
|
||||
*in_pos = in_start;
|
||||
*out_pos = out_start;
|
||||
|
||||
if (ret == LZMA_OK) {
|
||||
// Either the input was truncated or the
|
||||
// output buffer was too small.
|
||||
assert(*in_pos == in_size
|
||||
|| *out_pos == out_size);
|
||||
|
||||
// If all the input was consumed, then the
|
||||
// input is truncated, even if the output
|
||||
// buffer is also full. This is because
|
||||
// processing the last byte of the Stream
|
||||
// never produces output.
|
||||
if (*in_pos == in_size)
|
||||
ret = LZMA_DATA_ERROR;
|
||||
else
|
||||
ret = LZMA_BUF_ERROR;
|
||||
|
||||
} else if (ret == LZMA_MEMLIMIT_ERROR) {
|
||||
// Let the caller know how much memory would
|
||||
// have been needed.
|
||||
uint64_t memusage;
|
||||
(void)stream_decoder.memconfig(
|
||||
stream_decoder.coder,
|
||||
memlimit, &memusage, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Free the decoder memory. This needs to be done even if
|
||||
// initialization fails, because the internal API doesn't
|
||||
// require the initialization function to free its memory on error.
|
||||
lzma_next_end(&stream_decoder, allocator);
|
||||
|
||||
return ret;
|
||||
}
|
||||
@@ -1,141 +0,0 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
/// \file stream_buffer_encoder.c
|
||||
/// \brief Single-call .xz Stream encoder
|
||||
//
|
||||
// Author: Lasse Collin
|
||||
//
|
||||
// This file has been put into the public domain.
|
||||
// You can do whatever you want with this file.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "index.h"
|
||||
|
||||
|
||||
/// Maximum size of Index that has exactly one Record.
|
||||
/// Index Indicator + Number of Records + Record + CRC32 rounded up to
|
||||
/// the next multiple of four.
|
||||
#define INDEX_BOUND ((1 + 1 + 2 * LZMA_VLI_BYTES_MAX + 4 + 3) & ~3)
|
||||
|
||||
/// Stream Header, Stream Footer, and Index
|
||||
#define HEADERS_BOUND (2 * LZMA_STREAM_HEADER_SIZE + INDEX_BOUND)
|
||||
|
||||
|
||||
extern LZMA_API(size_t)
|
||||
lzma_stream_buffer_bound(size_t uncompressed_size)
|
||||
{
|
||||
// Get the maximum possible size of a Block.
|
||||
const size_t block_bound = lzma_block_buffer_bound(uncompressed_size);
|
||||
if (block_bound == 0)
|
||||
return 0;
|
||||
|
||||
// Catch the possible integer overflow and also prevent the size of
|
||||
// the Stream exceeding LZMA_VLI_MAX (theoretically possible on
|
||||
// 64-bit systems).
|
||||
if (my_min(SIZE_MAX, LZMA_VLI_MAX) - block_bound < HEADERS_BOUND)
|
||||
return 0;
|
||||
|
||||
return block_bound + HEADERS_BOUND;
|
||||
}
|
||||
|
||||
|
||||
extern LZMA_API(lzma_ret)
|
||||
lzma_stream_buffer_encode(lzma_filter *filters, lzma_check check,
|
||||
const lzma_allocator *allocator,
|
||||
const uint8_t *in, size_t in_size,
|
||||
uint8_t *out, size_t *out_pos_ptr, size_t out_size)
|
||||
{
|
||||
// Sanity checks
|
||||
if (filters == NULL || (unsigned int)(check) > LZMA_CHECK_ID_MAX
|
||||
|| (in == NULL && in_size != 0) || out == NULL
|
||||
|| out_pos_ptr == NULL || *out_pos_ptr > out_size)
|
||||
return LZMA_PROG_ERROR;
|
||||
|
||||
if (!lzma_check_is_supported(check))
|
||||
return LZMA_UNSUPPORTED_CHECK;
|
||||
|
||||
// Note for the paranoids: Index encoder prevents the Stream from
|
||||
// getting too big and still being accepted with LZMA_OK, and Block
|
||||
// encoder catches if the input is too big. So we don't need to
|
||||
// separately check if the buffers are too big.
|
||||
|
||||
// Use a local copy. We update *out_pos_ptr only if everything
|
||||
// succeeds.
|
||||
size_t out_pos = *out_pos_ptr;
|
||||
|
||||
// Check that there's enough space for both Stream Header and
|
||||
// Stream Footer.
|
||||
if (out_size - out_pos <= 2 * LZMA_STREAM_HEADER_SIZE)
|
||||
return LZMA_BUF_ERROR;
|
||||
|
||||
// Reserve space for Stream Footer so we don't need to check for
|
||||
// available space again before encoding Stream Footer.
|
||||
out_size -= LZMA_STREAM_HEADER_SIZE;
|
||||
|
||||
// Encode the Stream Header.
|
||||
lzma_stream_flags stream_flags = {
|
||||
.version = 0,
|
||||
.check = check,
|
||||
};
|
||||
|
||||
if (lzma_stream_header_encode(&stream_flags, out + out_pos)
|
||||
!= LZMA_OK)
|
||||
return LZMA_PROG_ERROR;
|
||||
|
||||
out_pos += LZMA_STREAM_HEADER_SIZE;
|
||||
|
||||
// Encode a Block but only if there is at least one byte of input.
|
||||
lzma_block block = {
|
||||
.version = 0,
|
||||
.check = check,
|
||||
.filters = filters,
|
||||
};
|
||||
|
||||
if (in_size > 0)
|
||||
return_if_error(lzma_block_buffer_encode(&block, allocator,
|
||||
in, in_size, out, &out_pos, out_size));
|
||||
|
||||
// Index
|
||||
{
|
||||
// Create an Index. It will have one Record if there was
|
||||
// at least one byte of input to encode. Otherwise the
|
||||
// Index will be empty.
|
||||
lzma_index *i = lzma_index_init(allocator);
|
||||
if (i == NULL)
|
||||
return LZMA_MEM_ERROR;
|
||||
|
||||
lzma_ret ret = LZMA_OK;
|
||||
|
||||
if (in_size > 0)
|
||||
ret = lzma_index_append(i, allocator,
|
||||
lzma_block_unpadded_size(&block),
|
||||
block.uncompressed_size);
|
||||
|
||||
// If adding the Record was successful, encode the Index
|
||||
// and get its size which will be stored into Stream Footer.
|
||||
if (ret == LZMA_OK) {
|
||||
ret = lzma_index_buffer_encode(
|
||||
i, out, &out_pos, out_size);
|
||||
|
||||
stream_flags.backward_size = lzma_index_size(i);
|
||||
}
|
||||
|
||||
lzma_index_end(i, allocator);
|
||||
|
||||
if (ret != LZMA_OK)
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Stream Footer. We have already reserved space for this.
|
||||
if (lzma_stream_footer_encode(&stream_flags, out + out_pos)
|
||||
!= LZMA_OK)
|
||||
return LZMA_PROG_ERROR;
|
||||
|
||||
out_pos += LZMA_STREAM_HEADER_SIZE;
|
||||
|
||||
// Everything went fine, make the new output position available
|
||||
// to the application.
|
||||
*out_pos_ptr = out_pos;
|
||||
return LZMA_OK;
|
||||
}
|
||||
@@ -1,462 +0,0 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
/// \file stream_decoder.c
|
||||
/// \brief Decodes .xz Streams
|
||||
//
|
||||
// Author: Lasse Collin
|
||||
//
|
||||
// This file has been put into the public domain.
|
||||
// You can do whatever you want with this file.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "stream_decoder.h"
|
||||
#include "block_decoder.h"
|
||||
|
||||
|
||||
struct lzma_coder_s {
|
||||
enum {
|
||||
SEQ_STREAM_HEADER,
|
||||
SEQ_BLOCK_HEADER,
|
||||
SEQ_BLOCK,
|
||||
SEQ_INDEX,
|
||||
SEQ_STREAM_FOOTER,
|
||||
SEQ_STREAM_PADDING,
|
||||
} sequence;
|
||||
|
||||
/// Block or Metadata decoder. This takes little memory and the same
|
||||
/// data structure can be used to decode every Block Header, so it's
|
||||
/// a good idea to have a separate lzma_next_coder structure for it.
|
||||
lzma_next_coder block_decoder;
|
||||
|
||||
/// Block options decoded by the Block Header decoder and used by
|
||||
/// the Block decoder.
|
||||
lzma_block block_options;
|
||||
|
||||
/// Stream Flags from Stream Header
|
||||
lzma_stream_flags stream_flags;
|
||||
|
||||
/// Index is hashed so that it can be compared to the sizes of Blocks
|
||||
/// with O(1) memory usage.
|
||||
lzma_index_hash *index_hash;
|
||||
|
||||
/// Memory usage limit
|
||||
uint64_t memlimit;
|
||||
|
||||
/// Amount of memory actually needed (only an estimate)
|
||||
uint64_t memusage;
|
||||
|
||||
/// If true, LZMA_NO_CHECK is returned if the Stream has
|
||||
/// no integrity check.
|
||||
bool tell_no_check;
|
||||
|
||||
/// If true, LZMA_UNSUPPORTED_CHECK is returned if the Stream has
|
||||
/// an integrity check that isn't supported by this liblzma build.
|
||||
bool tell_unsupported_check;
|
||||
|
||||
/// If true, LZMA_GET_CHECK is returned after decoding Stream Header.
|
||||
bool tell_any_check;
|
||||
|
||||
/// If true, we will tell the Block decoder to skip calculating
|
||||
/// and verifying the integrity check.
|
||||
bool ignore_check;
|
||||
|
||||
/// If true, we will decode concatenated Streams that possibly have
|
||||
/// Stream Padding between or after them. LZMA_STREAM_END is returned
|
||||
/// once the application isn't giving us any new input, and we aren't
|
||||
/// in the middle of a Stream, and possible Stream Padding is a
|
||||
/// multiple of four bytes.
|
||||
bool concatenated;
|
||||
|
||||
/// When decoding concatenated Streams, this is true as long as we
|
||||
/// are decoding the first Stream. This is needed to avoid misleading
|
||||
/// LZMA_FORMAT_ERROR in case the later Streams don't have valid magic
|
||||
/// bytes.
|
||||
bool first_stream;
|
||||
|
||||
/// Write position in buffer[] and position in Stream Padding
|
||||
size_t pos;
|
||||
|
||||
/// Buffer to hold Stream Header, Block Header, and Stream Footer.
|
||||
/// Block Header has biggest maximum size.
|
||||
uint8_t buffer[LZMA_BLOCK_HEADER_SIZE_MAX];
|
||||
};
|
||||
|
||||
|
||||
static lzma_ret
|
||||
stream_decoder_reset(lzma_coder *coder, const lzma_allocator *allocator)
|
||||
{
|
||||
// Initialize the Index hash used to verify the Index.
|
||||
coder->index_hash = lzma_index_hash_init(coder->index_hash, allocator);
|
||||
if (coder->index_hash == NULL)
|
||||
return LZMA_MEM_ERROR;
|
||||
|
||||
// Reset the rest of the variables.
|
||||
coder->sequence = SEQ_STREAM_HEADER;
|
||||
coder->pos = 0;
|
||||
|
||||
return LZMA_OK;
|
||||
}
|
||||
|
||||
|
||||
static lzma_ret
|
||||
stream_decode(lzma_coder *coder, const lzma_allocator *allocator,
|
||||
const uint8_t *restrict in, size_t *restrict in_pos,
|
||||
size_t in_size, uint8_t *restrict out,
|
||||
size_t *restrict out_pos, size_t out_size, lzma_action action)
|
||||
{
|
||||
// When decoding the actual Block, it may be able to produce more
|
||||
// output even if we don't give it any new input.
|
||||
while (true)
|
||||
switch (coder->sequence) {
|
||||
case SEQ_STREAM_HEADER: {
|
||||
// Copy the Stream Header to the internal buffer.
|
||||
lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos,
|
||||
LZMA_STREAM_HEADER_SIZE);
|
||||
|
||||
// Return if we didn't get the whole Stream Header yet.
|
||||
if (coder->pos < LZMA_STREAM_HEADER_SIZE)
|
||||
return LZMA_OK;
|
||||
|
||||
coder->pos = 0;
|
||||
|
||||
// Decode the Stream Header.
|
||||
const lzma_ret ret = lzma_stream_header_decode(
|
||||
&coder->stream_flags, coder->buffer);
|
||||
if (ret != LZMA_OK)
|
||||
return ret == LZMA_FORMAT_ERROR && !coder->first_stream
|
||||
? LZMA_DATA_ERROR : ret;
|
||||
|
||||
// If we are decoding concatenated Streams, and the later
|
||||
// Streams have invalid Header Magic Bytes, we give
|
||||
// LZMA_DATA_ERROR instead of LZMA_FORMAT_ERROR.
|
||||
coder->first_stream = false;
|
||||
|
||||
// Copy the type of the Check so that Block Header and Block
|
||||
// decoders see it.
|
||||
coder->block_options.check = coder->stream_flags.check;
|
||||
|
||||
// Even if we return LZMA_*_CHECK below, we want
|
||||
// to continue from Block Header decoding.
|
||||
coder->sequence = SEQ_BLOCK_HEADER;
|
||||
|
||||
// Detect if there's no integrity check or if it is
|
||||
// unsupported if those were requested by the application.
|
||||
if (coder->tell_no_check && coder->stream_flags.check
|
||||
== LZMA_CHECK_NONE)
|
||||
return LZMA_NO_CHECK;
|
||||
|
||||
if (coder->tell_unsupported_check
|
||||
&& !lzma_check_is_supported(
|
||||
coder->stream_flags.check))
|
||||
return LZMA_UNSUPPORTED_CHECK;
|
||||
|
||||
if (coder->tell_any_check)
|
||||
return LZMA_GET_CHECK;
|
||||
}
|
||||
|
||||
// Fall through
|
||||
|
||||
case SEQ_BLOCK_HEADER: {
|
||||
if (*in_pos >= in_size)
|
||||
return LZMA_OK;
|
||||
|
||||
if (coder->pos == 0) {
|
||||
// Detect if it's Index.
|
||||
if (in[*in_pos] == 0x00) {
|
||||
coder->sequence = SEQ_INDEX;
|
||||
break;
|
||||
}
|
||||
|
||||
// Calculate the size of the Block Header. Note that
|
||||
// Block Header decoder wants to see this byte too
|
||||
// so don't advance *in_pos.
|
||||
coder->block_options.header_size
|
||||
= lzma_block_header_size_decode(
|
||||
in[*in_pos]);
|
||||
}
|
||||
|
||||
// Copy the Block Header to the internal buffer.
|
||||
lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos,
|
||||
coder->block_options.header_size);
|
||||
|
||||
// Return if we didn't get the whole Block Header yet.
|
||||
if (coder->pos < coder->block_options.header_size)
|
||||
return LZMA_OK;
|
||||
|
||||
coder->pos = 0;
|
||||
|
||||
// Version 1 is needed to support the .ignore_check option.
|
||||
coder->block_options.version = 1;
|
||||
|
||||
// Set up a buffer to hold the filter chain. Block Header
|
||||
// decoder will initialize all members of this array so
|
||||
// we don't need to do it here.
|
||||
lzma_filter filters[LZMA_FILTERS_MAX + 1];
|
||||
coder->block_options.filters = filters;
|
||||
|
||||
// Decode the Block Header.
|
||||
return_if_error(lzma_block_header_decode(&coder->block_options,
|
||||
allocator, coder->buffer));
|
||||
|
||||
// If LZMA_IGNORE_CHECK was used, this flag needs to be set.
|
||||
// It has to be set after lzma_block_header_decode() because
|
||||
// it always resets this to false.
|
||||
coder->block_options.ignore_check = coder->ignore_check;
|
||||
|
||||
// Check the memory usage limit.
|
||||
const uint64_t memusage = lzma_raw_decoder_memusage(filters);
|
||||
lzma_ret ret;
|
||||
|
||||
if (memusage == UINT64_MAX) {
|
||||
// One or more unknown Filter IDs.
|
||||
ret = LZMA_OPTIONS_ERROR;
|
||||
} else {
|
||||
// Now we can set coder->memusage since we know that
|
||||
// the filter chain is valid. We don't want
|
||||
// lzma_memusage() to return UINT64_MAX in case of
|
||||
// invalid filter chain.
|
||||
coder->memusage = memusage;
|
||||
|
||||
if (memusage > coder->memlimit) {
|
||||
// The chain would need too much memory.
|
||||
ret = LZMA_MEMLIMIT_ERROR;
|
||||
} else {
|
||||
// Memory usage is OK.
|
||||
// Initialize the Block decoder.
|
||||
ret = lzma_block_decoder_init(
|
||||
&coder->block_decoder,
|
||||
allocator,
|
||||
&coder->block_options);
|
||||
}
|
||||
}
|
||||
|
||||
// Free the allocated filter options since they are needed
|
||||
// only to initialize the Block decoder.
|
||||
for (size_t i = 0; i < LZMA_FILTERS_MAX; ++i)
|
||||
lzma_free(filters[i].options, allocator);
|
||||
|
||||
coder->block_options.filters = NULL;
|
||||
|
||||
// Check if memory usage calculation and Block enocoder
|
||||
// initialization succeeded.
|
||||
if (ret != LZMA_OK)
|
||||
return ret;
|
||||
|
||||
coder->sequence = SEQ_BLOCK;
|
||||
}
|
||||
|
||||
// Fall through
|
||||
|
||||
case SEQ_BLOCK: {
|
||||
const lzma_ret ret = coder->block_decoder.code(
|
||||
coder->block_decoder.coder, allocator,
|
||||
in, in_pos, in_size, out, out_pos, out_size,
|
||||
action);
|
||||
|
||||
if (ret != LZMA_STREAM_END)
|
||||
return ret;
|
||||
|
||||
// Block decoded successfully. Add the new size pair to
|
||||
// the Index hash.
|
||||
return_if_error(lzma_index_hash_append(coder->index_hash,
|
||||
lzma_block_unpadded_size(
|
||||
&coder->block_options),
|
||||
coder->block_options.uncompressed_size));
|
||||
|
||||
coder->sequence = SEQ_BLOCK_HEADER;
|
||||
break;
|
||||
}
|
||||
|
||||
case SEQ_INDEX: {
|
||||
// If we don't have any input, don't call
|
||||
// lzma_index_hash_decode() since it would return
|
||||
// LZMA_BUF_ERROR, which we must not do here.
|
||||
if (*in_pos >= in_size)
|
||||
return LZMA_OK;
|
||||
|
||||
// Decode the Index and compare it to the hash calculated
|
||||
// from the sizes of the Blocks (if any).
|
||||
const lzma_ret ret = lzma_index_hash_decode(coder->index_hash,
|
||||
in, in_pos, in_size);
|
||||
if (ret != LZMA_STREAM_END)
|
||||
return ret;
|
||||
|
||||
coder->sequence = SEQ_STREAM_FOOTER;
|
||||
}
|
||||
|
||||
// Fall through
|
||||
|
||||
case SEQ_STREAM_FOOTER: {
|
||||
// Copy the Stream Footer to the internal buffer.
|
||||
lzma_bufcpy(in, in_pos, in_size, coder->buffer, &coder->pos,
|
||||
LZMA_STREAM_HEADER_SIZE);
|
||||
|
||||
// Return if we didn't get the whole Stream Footer yet.
|
||||
if (coder->pos < LZMA_STREAM_HEADER_SIZE)
|
||||
return LZMA_OK;
|
||||
|
||||
coder->pos = 0;
|
||||
|
||||
// Decode the Stream Footer. The decoder gives
|
||||
// LZMA_FORMAT_ERROR if the magic bytes don't match,
|
||||
// so convert that return code to LZMA_DATA_ERROR.
|
||||
lzma_stream_flags footer_flags;
|
||||
const lzma_ret ret = lzma_stream_footer_decode(
|
||||
&footer_flags, coder->buffer);
|
||||
if (ret != LZMA_OK)
|
||||
return ret == LZMA_FORMAT_ERROR
|
||||
? LZMA_DATA_ERROR : ret;
|
||||
|
||||
// Check that Index Size stored in the Stream Footer matches
|
||||
// the real size of the Index field.
|
||||
if (lzma_index_hash_size(coder->index_hash)
|
||||
!= footer_flags.backward_size)
|
||||
return LZMA_DATA_ERROR;
|
||||
|
||||
// Compare that the Stream Flags fields are identical in
|
||||
// both Stream Header and Stream Footer.
|
||||
return_if_error(lzma_stream_flags_compare(
|
||||
&coder->stream_flags, &footer_flags));
|
||||
|
||||
if (!coder->concatenated)
|
||||
return LZMA_STREAM_END;
|
||||
|
||||
coder->sequence = SEQ_STREAM_PADDING;
|
||||
}
|
||||
|
||||
// Fall through
|
||||
|
||||
case SEQ_STREAM_PADDING:
|
||||
assert(coder->concatenated);
|
||||
|
||||
// Skip over possible Stream Padding.
|
||||
while (true) {
|
||||
if (*in_pos >= in_size) {
|
||||
// Unless LZMA_FINISH was used, we cannot
|
||||
// know if there's more input coming later.
|
||||
if (action != LZMA_FINISH)
|
||||
return LZMA_OK;
|
||||
|
||||
// Stream Padding must be a multiple of
|
||||
// four bytes.
|
||||
return coder->pos == 0
|
||||
? LZMA_STREAM_END
|
||||
: LZMA_DATA_ERROR;
|
||||
}
|
||||
|
||||
// If the byte is not zero, it probably indicates
|
||||
// beginning of a new Stream (or the file is corrupt).
|
||||
if (in[*in_pos] != 0x00)
|
||||
break;
|
||||
|
||||
++*in_pos;
|
||||
coder->pos = (coder->pos + 1) & 3;
|
||||
}
|
||||
|
||||
// Stream Padding must be a multiple of four bytes (empty
|
||||
// Stream Padding is OK).
|
||||
if (coder->pos != 0) {
|
||||
++*in_pos;
|
||||
return LZMA_DATA_ERROR;
|
||||
}
|
||||
|
||||
// Prepare to decode the next Stream.
|
||||
return_if_error(stream_decoder_reset(coder, allocator));
|
||||
break;
|
||||
|
||||
default:
|
||||
assert(0);
|
||||
return LZMA_PROG_ERROR;
|
||||
}
|
||||
|
||||
// Never reached
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
stream_decoder_end(lzma_coder *coder, const lzma_allocator *allocator)
|
||||
{
|
||||
lzma_next_end(&coder->block_decoder, allocator);
|
||||
lzma_index_hash_end(coder->index_hash, allocator);
|
||||
lzma_free(coder, allocator);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
static lzma_check
|
||||
stream_decoder_get_check(const lzma_coder *coder)
|
||||
{
|
||||
return coder->stream_flags.check;
|
||||
}
|
||||
|
||||
|
||||
static lzma_ret
|
||||
stream_decoder_memconfig(lzma_coder *coder, uint64_t *memusage,
|
||||
uint64_t *old_memlimit, uint64_t new_memlimit)
|
||||
{
|
||||
*memusage = coder->memusage;
|
||||
*old_memlimit = coder->memlimit;
|
||||
|
||||
if (new_memlimit != 0) {
|
||||
if (new_memlimit < coder->memusage)
|
||||
return LZMA_MEMLIMIT_ERROR;
|
||||
|
||||
coder->memlimit = new_memlimit;
|
||||
}
|
||||
|
||||
return LZMA_OK;
|
||||
}
|
||||
|
||||
|
||||
extern lzma_ret
|
||||
lzma_stream_decoder_init(
|
||||
lzma_next_coder *next, const lzma_allocator *allocator,
|
||||
uint64_t memlimit, uint32_t flags)
|
||||
{
|
||||
lzma_next_coder_init(&lzma_stream_decoder_init, next, allocator);
|
||||
|
||||
if (memlimit == 0)
|
||||
return LZMA_PROG_ERROR;
|
||||
|
||||
if (flags & ~LZMA_SUPPORTED_FLAGS)
|
||||
return LZMA_OPTIONS_ERROR;
|
||||
|
||||
if (next->coder == NULL) {
|
||||
next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
|
||||
if (next->coder == NULL)
|
||||
return LZMA_MEM_ERROR;
|
||||
|
||||
next->code = &stream_decode;
|
||||
next->end = &stream_decoder_end;
|
||||
next->get_check = &stream_decoder_get_check;
|
||||
next->memconfig = &stream_decoder_memconfig;
|
||||
|
||||
next->coder->block_decoder = LZMA_NEXT_CODER_INIT;
|
||||
next->coder->index_hash = NULL;
|
||||
}
|
||||
|
||||
next->coder->memlimit = memlimit;
|
||||
next->coder->memusage = LZMA_MEMUSAGE_BASE;
|
||||
next->coder->tell_no_check = (flags & LZMA_TELL_NO_CHECK) != 0;
|
||||
next->coder->tell_unsupported_check
|
||||
= (flags & LZMA_TELL_UNSUPPORTED_CHECK) != 0;
|
||||
next->coder->tell_any_check = (flags & LZMA_TELL_ANY_CHECK) != 0;
|
||||
next->coder->ignore_check = (flags & LZMA_IGNORE_CHECK) != 0;
|
||||
next->coder->concatenated = (flags & LZMA_CONCATENATED) != 0;
|
||||
next->coder->first_stream = true;
|
||||
|
||||
return stream_decoder_reset(next->coder, allocator);
|
||||
}
|
||||
|
||||
|
||||
extern LZMA_API(lzma_ret)
|
||||
lzma_stream_decoder(lzma_stream *strm, uint64_t memlimit, uint32_t flags)
|
||||
{
|
||||
lzma_next_strm_init(lzma_stream_decoder_init, strm, memlimit, flags);
|
||||
|
||||
strm->internal->supported_actions[LZMA_RUN] = true;
|
||||
strm->internal->supported_actions[LZMA_FINISH] = true;
|
||||
|
||||
return LZMA_OK;
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
/// \file stream_decoder.h
|
||||
/// \brief Decodes .xz Streams
|
||||
//
|
||||
// Author: Lasse Collin
|
||||
//
|
||||
// This file has been put into the public domain.
|
||||
// You can do whatever you want with this file.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef LZMA_STREAM_DECODER_H
|
||||
#define LZMA_STREAM_DECODER_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
extern lzma_ret lzma_stream_decoder_init(
|
||||
lzma_next_coder *next, const lzma_allocator *allocator,
|
||||
uint64_t memlimit, uint32_t flags);
|
||||
|
||||
#endif
|
||||
@@ -1,332 +0,0 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
/// \file stream_encoder.c
|
||||
/// \brief Encodes .xz Streams
|
||||
//
|
||||
// Author: Lasse Collin
|
||||
//
|
||||
// This file has been put into the public domain.
|
||||
// You can do whatever you want with this file.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "block_encoder.h"
|
||||
#include "index_encoder.h"
|
||||
|
||||
|
||||
struct lzma_coder_s {
|
||||
enum {
|
||||
SEQ_STREAM_HEADER,
|
||||
SEQ_BLOCK_INIT,
|
||||
SEQ_BLOCK_HEADER,
|
||||
SEQ_BLOCK_ENCODE,
|
||||
SEQ_INDEX_ENCODE,
|
||||
SEQ_STREAM_FOOTER,
|
||||
} sequence;
|
||||
|
||||
/// True if Block encoder has been initialized by
|
||||
/// stream_encoder_init() or stream_encoder_update()
|
||||
/// and thus doesn't need to be initialized in stream_encode().
|
||||
bool block_encoder_is_initialized;
|
||||
|
||||
/// Block
|
||||
lzma_next_coder block_encoder;
|
||||
|
||||
/// Options for the Block encoder
|
||||
lzma_block block_options;
|
||||
|
||||
/// The filter chain currently in use
|
||||
lzma_filter filters[LZMA_FILTERS_MAX + 1];
|
||||
|
||||
/// Index encoder. This is separate from Block encoder, because this
|
||||
/// doesn't take much memory, and when encoding multiple Streams
|
||||
/// with the same encoding options we avoid reallocating memory.
|
||||
lzma_next_coder index_encoder;
|
||||
|
||||
/// Index to hold sizes of the Blocks
|
||||
lzma_index *index;
|
||||
|
||||
/// Read position in buffer[]
|
||||
size_t buffer_pos;
|
||||
|
||||
/// Total number of bytes in buffer[]
|
||||
size_t buffer_size;
|
||||
|
||||
/// Buffer to hold Stream Header, Block Header, and Stream Footer.
|
||||
/// Block Header has biggest maximum size.
|
||||
uint8_t buffer[LZMA_BLOCK_HEADER_SIZE_MAX];
|
||||
};
|
||||
|
||||
|
||||
static lzma_ret
|
||||
block_encoder_init(lzma_coder *coder, const lzma_allocator *allocator)
|
||||
{
|
||||
// Prepare the Block options. Even though Block encoder doesn't need
|
||||
// compressed_size, uncompressed_size, and header_size to be
|
||||
// initialized, it is a good idea to do it here, because this way
|
||||
// we catch if someone gave us Filter ID that cannot be used in
|
||||
// Blocks/Streams.
|
||||
coder->block_options.compressed_size = LZMA_VLI_UNKNOWN;
|
||||
coder->block_options.uncompressed_size = LZMA_VLI_UNKNOWN;
|
||||
|
||||
return_if_error(lzma_block_header_size(&coder->block_options));
|
||||
|
||||
// Initialize the actual Block encoder.
|
||||
return lzma_block_encoder_init(&coder->block_encoder, allocator,
|
||||
&coder->block_options);
|
||||
}
|
||||
|
||||
|
||||
static lzma_ret
|
||||
stream_encode(lzma_coder *coder, const lzma_allocator *allocator,
|
||||
const uint8_t *restrict in, size_t *restrict in_pos,
|
||||
size_t in_size, uint8_t *restrict out,
|
||||
size_t *restrict out_pos, size_t out_size, lzma_action action)
|
||||
{
|
||||
// Main loop
|
||||
while (*out_pos < out_size)
|
||||
switch (coder->sequence) {
|
||||
case SEQ_STREAM_HEADER:
|
||||
case SEQ_BLOCK_HEADER:
|
||||
case SEQ_STREAM_FOOTER:
|
||||
lzma_bufcpy(coder->buffer, &coder->buffer_pos,
|
||||
coder->buffer_size, out, out_pos, out_size);
|
||||
if (coder->buffer_pos < coder->buffer_size)
|
||||
return LZMA_OK;
|
||||
|
||||
if (coder->sequence == SEQ_STREAM_FOOTER)
|
||||
return LZMA_STREAM_END;
|
||||
|
||||
coder->buffer_pos = 0;
|
||||
++coder->sequence;
|
||||
break;
|
||||
|
||||
case SEQ_BLOCK_INIT: {
|
||||
if (*in_pos == in_size) {
|
||||
// If we are requested to flush or finish the current
|
||||
// Block, return LZMA_STREAM_END immediately since
|
||||
// there's nothing to do.
|
||||
if (action != LZMA_FINISH)
|
||||
return action == LZMA_RUN
|
||||
? LZMA_OK : LZMA_STREAM_END;
|
||||
|
||||
// The application had used LZMA_FULL_FLUSH to finish
|
||||
// the previous Block, but now wants to finish without
|
||||
// encoding new data, or it is simply creating an
|
||||
// empty Stream with no Blocks.
|
||||
//
|
||||
// Initialize the Index encoder, and continue to
|
||||
// actually encoding the Index.
|
||||
return_if_error(lzma_index_encoder_init(
|
||||
&coder->index_encoder, allocator,
|
||||
coder->index));
|
||||
coder->sequence = SEQ_INDEX_ENCODE;
|
||||
break;
|
||||
}
|
||||
|
||||
// Initialize the Block encoder unless it was already
|
||||
// initialized by stream_encoder_init() or
|
||||
// stream_encoder_update().
|
||||
if (!coder->block_encoder_is_initialized)
|
||||
return_if_error(block_encoder_init(coder, allocator));
|
||||
|
||||
// Make it false so that we don't skip the initialization
|
||||
// with the next Block.
|
||||
coder->block_encoder_is_initialized = false;
|
||||
|
||||
// Encode the Block Header. This shouldn't fail since we have
|
||||
// already initialized the Block encoder.
|
||||
if (lzma_block_header_encode(&coder->block_options,
|
||||
coder->buffer) != LZMA_OK)
|
||||
return LZMA_PROG_ERROR;
|
||||
|
||||
coder->buffer_size = coder->block_options.header_size;
|
||||
coder->sequence = SEQ_BLOCK_HEADER;
|
||||
break;
|
||||
}
|
||||
|
||||
case SEQ_BLOCK_ENCODE: {
|
||||
static const lzma_action convert[LZMA_ACTION_MAX + 1] = {
|
||||
LZMA_RUN,
|
||||
LZMA_SYNC_FLUSH,
|
||||
LZMA_FINISH,
|
||||
LZMA_FINISH,
|
||||
LZMA_FINISH,
|
||||
};
|
||||
|
||||
const lzma_ret ret = coder->block_encoder.code(
|
||||
coder->block_encoder.coder, allocator,
|
||||
in, in_pos, in_size,
|
||||
out, out_pos, out_size, convert[action]);
|
||||
if (ret != LZMA_STREAM_END || action == LZMA_SYNC_FLUSH)
|
||||
return ret;
|
||||
|
||||
// Add a new Index Record.
|
||||
const lzma_vli unpadded_size = lzma_block_unpadded_size(
|
||||
&coder->block_options);
|
||||
assert(unpadded_size != 0);
|
||||
return_if_error(lzma_index_append(coder->index, allocator,
|
||||
unpadded_size,
|
||||
coder->block_options.uncompressed_size));
|
||||
|
||||
coder->sequence = SEQ_BLOCK_INIT;
|
||||
break;
|
||||
}
|
||||
|
||||
case SEQ_INDEX_ENCODE: {
|
||||
// Call the Index encoder. It doesn't take any input, so
|
||||
// those pointers can be NULL.
|
||||
const lzma_ret ret = coder->index_encoder.code(
|
||||
coder->index_encoder.coder, allocator,
|
||||
NULL, NULL, 0,
|
||||
out, out_pos, out_size, LZMA_RUN);
|
||||
if (ret != LZMA_STREAM_END)
|
||||
return ret;
|
||||
|
||||
// Encode the Stream Footer into coder->buffer.
|
||||
const lzma_stream_flags stream_flags = {
|
||||
.version = 0,
|
||||
.backward_size = lzma_index_size(coder->index),
|
||||
.check = coder->block_options.check,
|
||||
};
|
||||
|
||||
if (lzma_stream_footer_encode(&stream_flags, coder->buffer)
|
||||
!= LZMA_OK)
|
||||
return LZMA_PROG_ERROR;
|
||||
|
||||
coder->buffer_size = LZMA_STREAM_HEADER_SIZE;
|
||||
coder->sequence = SEQ_STREAM_FOOTER;
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
assert(0);
|
||||
return LZMA_PROG_ERROR;
|
||||
}
|
||||
|
||||
return LZMA_OK;
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
stream_encoder_end(lzma_coder *coder, const lzma_allocator *allocator)
|
||||
{
|
||||
lzma_next_end(&coder->block_encoder, allocator);
|
||||
lzma_next_end(&coder->index_encoder, allocator);
|
||||
lzma_index_end(coder->index, allocator);
|
||||
|
||||
for (size_t i = 0; coder->filters[i].id != LZMA_VLI_UNKNOWN; ++i)
|
||||
lzma_free(coder->filters[i].options, allocator);
|
||||
|
||||
lzma_free(coder, allocator);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
static lzma_ret
|
||||
stream_encoder_update(lzma_coder *coder, const lzma_allocator *allocator,
|
||||
const lzma_filter *filters,
|
||||
const lzma_filter *reversed_filters)
|
||||
{
|
||||
if (coder->sequence <= SEQ_BLOCK_INIT) {
|
||||
// There is no incomplete Block waiting to be finished,
|
||||
// thus we can change the whole filter chain. Start by
|
||||
// trying to initialize the Block encoder with the new
|
||||
// chain. This way we detect if the chain is valid.
|
||||
coder->block_encoder_is_initialized = false;
|
||||
coder->block_options.filters = (lzma_filter *)(filters);
|
||||
const lzma_ret ret = block_encoder_init(coder, allocator);
|
||||
coder->block_options.filters = coder->filters;
|
||||
if (ret != LZMA_OK)
|
||||
return ret;
|
||||
|
||||
coder->block_encoder_is_initialized = true;
|
||||
|
||||
} else if (coder->sequence <= SEQ_BLOCK_ENCODE) {
|
||||
// We are in the middle of a Block. Try to update only
|
||||
// the filter-specific options.
|
||||
return_if_error(coder->block_encoder.update(
|
||||
coder->block_encoder.coder, allocator,
|
||||
filters, reversed_filters));
|
||||
} else {
|
||||
// Trying to update the filter chain when we are already
|
||||
// encoding Index or Stream Footer.
|
||||
return LZMA_PROG_ERROR;
|
||||
}
|
||||
|
||||
// Free the copy of the old chain and make a copy of the new chain.
|
||||
for (size_t i = 0; coder->filters[i].id != LZMA_VLI_UNKNOWN; ++i)
|
||||
lzma_free(coder->filters[i].options, allocator);
|
||||
|
||||
return lzma_filters_copy(filters, coder->filters, allocator);
|
||||
}
|
||||
|
||||
|
||||
static lzma_ret
|
||||
stream_encoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
|
||||
const lzma_filter *filters, lzma_check check)
|
||||
{
|
||||
lzma_next_coder_init(&stream_encoder_init, next, allocator);
|
||||
|
||||
if (filters == NULL)
|
||||
return LZMA_PROG_ERROR;
|
||||
|
||||
if (next->coder == NULL) {
|
||||
next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
|
||||
if (next->coder == NULL)
|
||||
return LZMA_MEM_ERROR;
|
||||
|
||||
next->code = &stream_encode;
|
||||
next->end = &stream_encoder_end;
|
||||
next->update = &stream_encoder_update;
|
||||
|
||||
next->coder->filters[0].id = LZMA_VLI_UNKNOWN;
|
||||
next->coder->block_encoder = LZMA_NEXT_CODER_INIT;
|
||||
next->coder->index_encoder = LZMA_NEXT_CODER_INIT;
|
||||
next->coder->index = NULL;
|
||||
}
|
||||
|
||||
// Basic initializations
|
||||
next->coder->sequence = SEQ_STREAM_HEADER;
|
||||
next->coder->block_options.version = 0;
|
||||
next->coder->block_options.check = check;
|
||||
|
||||
// Initialize the Index
|
||||
lzma_index_end(next->coder->index, allocator);
|
||||
next->coder->index = lzma_index_init(allocator);
|
||||
if (next->coder->index == NULL)
|
||||
return LZMA_MEM_ERROR;
|
||||
|
||||
// Encode the Stream Header
|
||||
lzma_stream_flags stream_flags = {
|
||||
.version = 0,
|
||||
.check = check,
|
||||
};
|
||||
return_if_error(lzma_stream_header_encode(
|
||||
&stream_flags, next->coder->buffer));
|
||||
|
||||
next->coder->buffer_pos = 0;
|
||||
next->coder->buffer_size = LZMA_STREAM_HEADER_SIZE;
|
||||
|
||||
// Initialize the Block encoder. This way we detect unsupported
|
||||
// filter chains when initializing the Stream encoder instead of
|
||||
// giving an error after Stream Header has already written out.
|
||||
return stream_encoder_update(
|
||||
next->coder, allocator, filters, NULL);
|
||||
}
|
||||
|
||||
|
||||
extern LZMA_API(lzma_ret)
|
||||
lzma_stream_encoder(lzma_stream *strm,
|
||||
const lzma_filter *filters, lzma_check check)
|
||||
{
|
||||
lzma_next_strm_init(stream_encoder_init, strm, filters, check);
|
||||
|
||||
strm->internal->supported_actions[LZMA_RUN] = true;
|
||||
strm->internal->supported_actions[LZMA_SYNC_FLUSH] = true;
|
||||
strm->internal->supported_actions[LZMA_FULL_FLUSH] = true;
|
||||
strm->internal->supported_actions[LZMA_FULL_BARRIER] = true;
|
||||
strm->internal->supported_actions[LZMA_FINISH] = true;
|
||||
|
||||
return LZMA_OK;
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,47 +0,0 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
/// \file stream_flags_common.c
|
||||
/// \brief Common stuff for Stream flags coders
|
||||
//
|
||||
// Author: Lasse Collin
|
||||
//
|
||||
// This file has been put into the public domain.
|
||||
// You can do whatever you want with this file.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "stream_flags_common.h"
|
||||
|
||||
|
||||
const uint8_t lzma_header_magic[6] = { 0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00 };
|
||||
const uint8_t lzma_footer_magic[2] = { 0x59, 0x5A };
|
||||
|
||||
|
||||
extern LZMA_API(lzma_ret)
|
||||
lzma_stream_flags_compare(
|
||||
const lzma_stream_flags *a, const lzma_stream_flags *b)
|
||||
{
|
||||
// We can compare only version 0 structures.
|
||||
if (a->version != 0 || b->version != 0)
|
||||
return LZMA_OPTIONS_ERROR;
|
||||
|
||||
// Check type
|
||||
if ((unsigned int)(a->check) > LZMA_CHECK_ID_MAX
|
||||
|| (unsigned int)(b->check) > LZMA_CHECK_ID_MAX)
|
||||
return LZMA_PROG_ERROR;
|
||||
|
||||
if (a->check != b->check)
|
||||
return LZMA_DATA_ERROR;
|
||||
|
||||
// Backward Sizes are compared only if they are known in both.
|
||||
if (a->backward_size != LZMA_VLI_UNKNOWN
|
||||
&& b->backward_size != LZMA_VLI_UNKNOWN) {
|
||||
if (!is_backward_size_valid(a) || !is_backward_size_valid(b))
|
||||
return LZMA_PROG_ERROR;
|
||||
|
||||
if (a->backward_size != b->backward_size)
|
||||
return LZMA_DATA_ERROR;
|
||||
}
|
||||
|
||||
return LZMA_OK;
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
/// \file stream_flags_common.h
|
||||
/// \brief Common stuff for Stream flags coders
|
||||
//
|
||||
// Author: Lasse Collin
|
||||
//
|
||||
// This file has been put into the public domain.
|
||||
// You can do whatever you want with this file.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef LZMA_STREAM_FLAGS_COMMON_H
|
||||
#define LZMA_STREAM_FLAGS_COMMON_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
/// Size of the Stream Flags field
|
||||
#define LZMA_STREAM_FLAGS_SIZE 2
|
||||
|
||||
extern const uint8_t lzma_header_magic[6];
|
||||
extern const uint8_t lzma_footer_magic[2];
|
||||
|
||||
|
||||
static inline bool
|
||||
is_backward_size_valid(const lzma_stream_flags *options)
|
||||
{
|
||||
return options->backward_size >= LZMA_BACKWARD_SIZE_MIN
|
||||
&& options->backward_size <= LZMA_BACKWARD_SIZE_MAX
|
||||
&& (options->backward_size & 3) == 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -1,82 +0,0 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
/// \file stream_flags_decoder.c
|
||||
/// \brief Decodes Stream Header and Stream Footer from .xz files
|
||||
//
|
||||
// Author: Lasse Collin
|
||||
//
|
||||
// This file has been put into the public domain.
|
||||
// You can do whatever you want with this file.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "stream_flags_common.h"
|
||||
|
||||
|
||||
static bool
|
||||
stream_flags_decode(lzma_stream_flags *options, const uint8_t *in)
|
||||
{
|
||||
// Reserved bits must be unset.
|
||||
if (in[0] != 0x00 || (in[1] & 0xF0))
|
||||
return true;
|
||||
|
||||
options->version = 0;
|
||||
options->check = in[1] & 0x0F;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
extern LZMA_API(lzma_ret)
|
||||
lzma_stream_header_decode(lzma_stream_flags *options, const uint8_t *in)
|
||||
{
|
||||
// Magic
|
||||
if (memcmp(in, lzma_header_magic, sizeof(lzma_header_magic)) != 0)
|
||||
return LZMA_FORMAT_ERROR;
|
||||
#if 0
|
||||
// Verify the CRC32 so we can distinguish between corrupt
|
||||
// and unsupported files.
|
||||
const uint32_t crc = lzma_crc32(in + sizeof(lzma_header_magic),
|
||||
LZMA_STREAM_FLAGS_SIZE, 0);
|
||||
if (crc != unaligned_read32le(in + sizeof(lzma_header_magic)
|
||||
+ LZMA_STREAM_FLAGS_SIZE))
|
||||
return LZMA_DATA_ERROR;
|
||||
#endif
|
||||
// Stream Flags
|
||||
if (stream_flags_decode(options, in + sizeof(lzma_header_magic)))
|
||||
return LZMA_OPTIONS_ERROR;
|
||||
|
||||
// Set Backward Size to indicate unknown value. That way
|
||||
// lzma_stream_flags_compare() can be used to compare Stream Header
|
||||
// and Stream Footer while keeping it useful also for comparing
|
||||
// two Stream Footers.
|
||||
options->backward_size = LZMA_VLI_UNKNOWN;
|
||||
|
||||
return LZMA_OK;
|
||||
}
|
||||
|
||||
|
||||
extern LZMA_API(lzma_ret)
|
||||
lzma_stream_footer_decode(lzma_stream_flags *options, const uint8_t *in)
|
||||
{
|
||||
// Magic
|
||||
if (memcmp(in + sizeof(uint32_t) * 2 + LZMA_STREAM_FLAGS_SIZE,
|
||||
lzma_footer_magic, sizeof(lzma_footer_magic)) != 0)
|
||||
return LZMA_FORMAT_ERROR;
|
||||
#if 0
|
||||
// CRC32
|
||||
const uint32_t crc = lzma_crc32(in + sizeof(uint32_t),
|
||||
sizeof(uint32_t) + LZMA_STREAM_FLAGS_SIZE, 0);
|
||||
if (crc != unaligned_read32le(in))
|
||||
return LZMA_DATA_ERROR;
|
||||
#endif
|
||||
// Stream Flags
|
||||
if (stream_flags_decode(options, in + sizeof(uint32_t) * 2))
|
||||
return LZMA_OPTIONS_ERROR;
|
||||
|
||||
// Backward Size
|
||||
options->backward_size = unaligned_read32le(in + sizeof(uint32_t));
|
||||
options->backward_size = (options->backward_size + 1) * 4;
|
||||
|
||||
return LZMA_OK;
|
||||
}
|
||||
@@ -1,86 +0,0 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
/// \file stream_flags_encoder.c
|
||||
/// \brief Encodes Stream Header and Stream Footer for .xz files
|
||||
//
|
||||
// Author: Lasse Collin
|
||||
//
|
||||
// This file has been put into the public domain.
|
||||
// You can do whatever you want with this file.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "stream_flags_common.h"
|
||||
|
||||
|
||||
static bool
|
||||
stream_flags_encode(const lzma_stream_flags *options, uint8_t *out)
|
||||
{
|
||||
if ((unsigned int)(options->check) > LZMA_CHECK_ID_MAX)
|
||||
return true;
|
||||
|
||||
out[0] = 0x00;
|
||||
out[1] = options->check;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
extern LZMA_API(lzma_ret)
|
||||
lzma_stream_header_encode(const lzma_stream_flags *options, uint8_t *out)
|
||||
{
|
||||
assert(sizeof(lzma_header_magic) + LZMA_STREAM_FLAGS_SIZE
|
||||
+ 4 == LZMA_STREAM_HEADER_SIZE);
|
||||
|
||||
if (options->version != 0)
|
||||
return LZMA_OPTIONS_ERROR;
|
||||
|
||||
// Magic
|
||||
memcpy(out, lzma_header_magic, sizeof(lzma_header_magic));
|
||||
|
||||
// Stream Flags
|
||||
if (stream_flags_encode(options, out + sizeof(lzma_header_magic)))
|
||||
return LZMA_PROG_ERROR;
|
||||
|
||||
// CRC32 of the Stream Header
|
||||
const uint32_t crc = lzma_crc32(out + sizeof(lzma_header_magic),
|
||||
LZMA_STREAM_FLAGS_SIZE, 0);
|
||||
|
||||
unaligned_write32le(out + sizeof(lzma_header_magic)
|
||||
+ LZMA_STREAM_FLAGS_SIZE, crc);
|
||||
|
||||
return LZMA_OK;
|
||||
}
|
||||
|
||||
|
||||
extern LZMA_API(lzma_ret)
|
||||
lzma_stream_footer_encode(const lzma_stream_flags *options, uint8_t *out)
|
||||
{
|
||||
assert(2 * 4 + LZMA_STREAM_FLAGS_SIZE + sizeof(lzma_footer_magic)
|
||||
== LZMA_STREAM_HEADER_SIZE);
|
||||
|
||||
if (options->version != 0)
|
||||
return LZMA_OPTIONS_ERROR;
|
||||
|
||||
// Backward Size
|
||||
if (!is_backward_size_valid(options))
|
||||
return LZMA_PROG_ERROR;
|
||||
|
||||
unaligned_write32le(out + 4, options->backward_size / 4 - 1);
|
||||
|
||||
// Stream Flags
|
||||
if (stream_flags_encode(options, out + 2 * 4))
|
||||
return LZMA_PROG_ERROR;
|
||||
|
||||
// CRC32
|
||||
const uint32_t crc = lzma_crc32(
|
||||
out + 4, 4 + LZMA_STREAM_FLAGS_SIZE, 0);
|
||||
|
||||
unaligned_write32le(out, crc);
|
||||
|
||||
// Magic
|
||||
memcpy(out + 2 * 4 + LZMA_STREAM_FLAGS_SIZE,
|
||||
lzma_footer_magic, sizeof(lzma_footer_magic));
|
||||
|
||||
return LZMA_OK;
|
||||
}
|
||||
@@ -1,91 +0,0 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
/// \file tuklib_cpucores.c
|
||||
/// \brief Get the number of CPU cores online
|
||||
//
|
||||
// Author: Lasse Collin
|
||||
//
|
||||
// This file has been put into the public domain.
|
||||
// You can do whatever you want with this file.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "tuklib_cpucores.h"
|
||||
|
||||
#if defined(_WIN32) || defined(__CYGWIN__)
|
||||
# ifndef _WIN32_WINNT
|
||||
# define _WIN32_WINNT 0x0500
|
||||
# endif
|
||||
# include <windows.h>
|
||||
|
||||
// FreeBSD
|
||||
#elif defined(TUKLIB_CPUCORES_CPUSET)
|
||||
# include <sys/param.h>
|
||||
# include <sys/cpuset.h>
|
||||
|
||||
#elif defined(TUKLIB_CPUCORES_SYSCTL)
|
||||
# ifdef HAVE_SYS_PARAM_H
|
||||
# include <sys/param.h>
|
||||
# endif
|
||||
# include <sys/sysctl.h>
|
||||
|
||||
#elif defined(TUKLIB_CPUCORES_SYSCONF)
|
||||
# include <unistd.h>
|
||||
|
||||
// HP-UX
|
||||
#elif defined(TUKLIB_CPUCORES_PSTAT_GETDYNAMIC)
|
||||
# include <sys/param.h>
|
||||
# include <sys/pstat.h>
|
||||
#endif
|
||||
|
||||
|
||||
extern uint32_t
|
||||
tuklib_cpucores(void)
|
||||
{
|
||||
uint32_t ret = 0;
|
||||
|
||||
#if defined(_WIN32) || defined(__CYGWIN__)
|
||||
SYSTEM_INFO sysinfo;
|
||||
GetSystemInfo(&sysinfo);
|
||||
ret = sysinfo.dwNumberOfProcessors;
|
||||
|
||||
#elif defined(TUKLIB_CPUCORES_CPUSET)
|
||||
cpuset_t set;
|
||||
if (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1,
|
||||
sizeof(set), &set) == 0) {
|
||||
# ifdef CPU_COUNT
|
||||
ret = CPU_COUNT(&set);
|
||||
# else
|
||||
for (unsigned i = 0; i < CPU_SETSIZE; ++i)
|
||||
if (CPU_ISSET(i, &set))
|
||||
++ret;
|
||||
# endif
|
||||
}
|
||||
|
||||
#elif defined(TUKLIB_CPUCORES_SYSCTL)
|
||||
int name[2] = { CTL_HW, HW_NCPU };
|
||||
int cpus;
|
||||
size_t cpus_size = sizeof(cpus);
|
||||
if (sysctl(name, 2, &cpus, &cpus_size, NULL, 0) != -1
|
||||
&& cpus_size == sizeof(cpus) && cpus > 0)
|
||||
ret = cpus;
|
||||
|
||||
#elif defined(TUKLIB_CPUCORES_SYSCONF)
|
||||
# ifdef _SC_NPROCESSORS_ONLN
|
||||
// Most systems
|
||||
const long cpus = sysconf(_SC_NPROCESSORS_ONLN);
|
||||
# else
|
||||
// IRIX
|
||||
const long cpus = sysconf(_SC_NPROC_ONLN);
|
||||
# endif
|
||||
if (cpus > 0)
|
||||
ret = cpus;
|
||||
|
||||
#elif defined(TUKLIB_CPUCORES_PSTAT_GETDYNAMIC)
|
||||
struct pst_dynamic pst;
|
||||
if (pstat_getdynamic(&pst, sizeof(pst), 1, 0) != -1)
|
||||
ret = pst.psd_proc_cnt;
|
||||
#endif
|
||||
|
||||
return ret;
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
/// \file tuklib_cpucores.h
|
||||
/// \brief Get the number of CPU cores online
|
||||
//
|
||||
// Author: Lasse Collin
|
||||
//
|
||||
// This file has been put into the public domain.
|
||||
// You can do whatever you want with this file.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef TUKLIB_CPUCORES_H
|
||||
#define TUKLIB_CPUCORES_H
|
||||
|
||||
#include "tuklib_common.h"
|
||||
TUKLIB_DECLS_BEGIN
|
||||
|
||||
#define tuklib_cpucores TUKLIB_SYMBOL(tuklib_cpucores)
|
||||
extern uint32_t tuklib_cpucores(void);
|
||||
|
||||
TUKLIB_DECLS_END
|
||||
#endif
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user