miniz.c

filter: Statementstmt 0.0% 0 / 3217 Branchbran 0.0% 0 / 3434 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 6651 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Sereal/Performance.pm ↑ Summary snappy/csnappy_decompress.c »
1 #include "miniz.h"
2 /**************************************************************************
3 *
4 * Copyright 2013-2014 RAD Game Tools and Valve Software
5 * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
6 * All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 *
26 **************************************************************************/
27  
28  
29  
30 typedef unsigned char mz_validate_uint16[sizeof(mz_uint16) == 2 ? 1 : -1];
31 typedef unsigned char mz_validate_uint32[sizeof(mz_uint32) == 4 ? 1 : -1];
32 typedef unsigned char mz_validate_uint64[sizeof(mz_uint64) == 8 ? 1 : -1];
33  
34 #ifdef __cplusplus
35 extern "C"
36 {
37 #endif
38  
39 /* ------------------- zlib-style API's */
40  
41 0 mz_ulong mz_adler32(mz_ulong adler, const unsigned char *ptr, size_t buf_len)
42 {
43 0 mz_uint32 i, s1 = (mz_uint32)(adler & 0xffff), s2 = (mz_uint32)(adler >> 16);
44 0 size_t block_len = buf_len % 5552;
45 0 ▶ if (!ptr)
Branch: 1 branch
BranchTrueFalseTotal
if (!ptr) 0 0 0
46 0 return MZ_ADLER32_INIT;
47 0 ▶ while (buf_len)
Branch: 1 branch
BranchTrueFalseTotal
while (buf_len) 0 0 0
48 {
49 0 ▶ for (i = 0; i + 7 < block_len; i += 8, ptr += 8)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 7 < block_len; i += 8, ptr += 8) 0 0 0
50 {
51 0 s1 += ptr[0], s2 += s1;
52 0 s1 += ptr[1], s2 += s1;
53 0 s1 += ptr[2], s2 += s1;
54 0 s1 += ptr[3], s2 += s1;
55 0 s1 += ptr[4], s2 += s1;
56 0 s1 += ptr[5], s2 += s1;
57 0 s1 += ptr[6], s2 += s1;
58 0 s1 += ptr[7], s2 += s1;
59 }
60 0 ▶ for (; i < block_len; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (; i < block_len; ++i) 0 0 0
61 0 s1 += *ptr++, s2 += s1;
62 0 s1 %= 65521U, s2 %= 65521U;
63 0 buf_len -= block_len;
64 0 block_len = 5552;
65 }
66 0 return (s2 << 16) + s1;
67 }
68  
69 /* Karl Malbrain's compact CRC-32. See "A compact CCITT crc16 and crc32 C implementation that balances processor cache usage against speed": http://www.geocities.com/malbrain/ */
70 #if 0
71 mz_ulong mz_crc32(mz_ulong crc, const mz_uint8 *ptr, size_t buf_len)
72 {
73 static const mz_uint32 s_crc32[16] = { 0, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
74 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c };
75 mz_uint32 crcu32 = (mz_uint32)crc;
76 if (!ptr)
77 return MZ_CRC32_INIT;
78 crcu32 = ~crcu32;
79 while (buf_len--)
80 {
81 mz_uint8 b = *ptr++;
82 crcu32 = (crcu32 >> 4) ^ s_crc32[(crcu32 & 0xF) ^ (b & 0xF)];
83 crcu32 = (crcu32 >> 4) ^ s_crc32[(crcu32 & 0xF) ^ (b >> 4)];
84 }
85 return ~crcu32;
86 }
87 #elif defined(USE_EXTERNAL_MZCRC)
88 /* If USE_EXTERNAL_CRC is defined, an external module will export the
89 * mz_crc32() symbol for us to use, e.g. an SSE-accelerated version.
90 * Depending on the impl, it may be necessary to ~ the input/output crc values.
91 */
92 mz_ulong mz_crc32(mz_ulong crc, const mz_uint8 *ptr, size_t buf_len);
93 #else
94 /* Faster, but larger CPU cache footprint.
95 */
96 0 mz_ulong mz_crc32(mz_ulong crc, const mz_uint8 *ptr, size_t buf_len)
97 {
98 static const mz_uint32 s_crc_table[256] = {
99 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, 0xE963A535,
100 0x9E6495A3, 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD,
101 0xE7B82D07, 0x90BF1D91, 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE, 0x1ADAD47D,
102 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7, 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC,
103 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5, 0x3B6E20C8, 0x4C69105E, 0xD56041E4,
104 0xA2677172, 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, 0x35B5A8FA, 0x42B2986C,
105 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59, 0x26D930AC,
106 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F,
107 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, 0x2F6F7C87, 0x58684C11, 0xC1611DAB,
108 0xB6662D3D, 0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F,
109 0x9FBFE4A5, 0xE8B8D433, 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB,
110 0x086D3D2D, 0x91646C97, 0xE6635C01, 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E,
111 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, 0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA,
112 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65, 0x4DB26158, 0x3AB551CE,
113 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB, 0x4369E96A,
114 0x346ED9FC, 0xAD678846, 0xDA60B8D0, 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9,
115 0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409,
116 0xCE61E49F, 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81,
117 0xB7BD5C3B, 0xC0BA6CAD, 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, 0xEAD54739,
118 0x9DD277AF, 0x04DB2615, 0x73DC1683, 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8,
119 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1, 0xF00F9344, 0x8708A3D2, 0x1E01F268,
120 0x6906C2FE, 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7, 0xFED41B76, 0x89D32BE0,
121 0x10DA7A5A, 0x67DD4ACC, 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5, 0xD6D6A3E8,
122 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B,
123 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55, 0x316E8EEF,
124 0x4669BE79, 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, 0xCC0C7795, 0xBB0B4703,
125 0x220216B9, 0x5505262F, 0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7,
126 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D, 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A,
127 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713, 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE,
128 0x0CB61B38, 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, 0x86D3D2D4, 0xF1D4E242,
129 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777, 0x88085AE6,
130 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45,
131 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2, 0xA7672661, 0xD06016F7, 0x4969474D,
132 0x3E6E77DB, 0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5,
133 0x47B2CF7F, 0x30B5FFE9, 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605,
134 0xCDD70693, 0x54DE5729, 0x23D967BF, 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94,
135 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D
136 };
137  
138 0 mz_uint32 crc32 = (mz_uint32)crc ^ 0xFFFFFFFF;
139 0 const mz_uint8 *pByte_buf = (const mz_uint8 *)ptr;
140  
141 0 ▶ while (buf_len >= 4)
Branch: 1 branch
BranchTrueFalseTotal
while (buf_len >= 4) 0 0 0
142 {
143 0 crc32 = (crc32 >> 8) ^ s_crc_table[(crc32 ^ pByte_buf[0]) & 0xFF];
144 0 crc32 = (crc32 >> 8) ^ s_crc_table[(crc32 ^ pByte_buf[1]) & 0xFF];
145 0 crc32 = (crc32 >> 8) ^ s_crc_table[(crc32 ^ pByte_buf[2]) & 0xFF];
146 0 crc32 = (crc32 >> 8) ^ s_crc_table[(crc32 ^ pByte_buf[3]) & 0xFF];
147 0 pByte_buf += 4;
148 0 buf_len -= 4;
149 }
150  
151 0 ▶ while (buf_len)
Branch: 1 branch
BranchTrueFalseTotal
while (buf_len) 0 0 0
152 {
153 0 crc32 = (crc32 >> 8) ^ s_crc_table[(crc32 ^ pByte_buf[0]) & 0xFF];
154 0 ++pByte_buf;
155 0 --buf_len;
156 }
157  
158 0 return ~crc32;
159 }
160 #endif
161  
162 0 void mz_free(void *p)
163 {
164 0 MZ_FREE(p);
165 0 }
166  
167 0 MINIZ_EXPORT void *miniz_def_alloc_func(void *opaque, size_t items, size_t size)
168 {
169 (void)opaque, (void)items, (void)size;
170 0 return MZ_MALLOC(items * size);
171 }
172 0 MINIZ_EXPORT void miniz_def_free_func(void *opaque, void *address)
173 {
174 (void)opaque, (void)address;
175 0 MZ_FREE(address);
176 0 }
177 0 MINIZ_EXPORT void *miniz_def_realloc_func(void *opaque, void *address, size_t items, size_t size)
178 {
179 (void)opaque, (void)address, (void)items, (void)size;
180 0 return MZ_REALLOC(address, items * size);
181 }
182  
183 0 const char *mz_version(void)
184 {
185 0 return MZ_VERSION;
186 }
187  
188 #ifndef MINIZ_NO_ZLIB_APIS
189  
190 #ifndef MINIZ_NO_DEFLATE_APIS
191  
192 0 int mz_deflateInit(mz_streamp pStream, int level)
193 {
194 0 return mz_deflateInit2(pStream, level, MZ_DEFLATED, MZ_DEFAULT_WINDOW_BITS, 9, MZ_DEFAULT_STRATEGY);
195 }
196  
197 0 int mz_deflateInit2(mz_streamp pStream, int level, int method, int window_bits, int mem_level, int strategy)
198 {
199 tdefl_compressor *pComp;
200 0 mz_uint comp_flags = TDEFL_COMPUTE_ADLER32 | tdefl_create_comp_flags_from_zip_params(level, window_bits, strategy);
201  
202 0 ▶ if (!pStream)
Branch: 1 branch
BranchTrueFalseTotal
if (!pStream) 0 0 0
203 0 return MZ_STREAM_ERROR;
204 0 ▶ if ((method != MZ_DEFLATED) || ((mem_level < 1) || (mem_level > 9)) || ((window_bits != MZ_DEFAULT_WINDOW_BITS) && (-window_bits != MZ_DEFAULT_WINDOW_BITS)))
Branch: 5 branches
BranchTrueFalseTotal
if ((method != MZ_DEFLATED) || ((mem_level < 1) || (mem_level > 9)) || ((window_bits != MZ_DEFAULT_WINDOW_BITS) && (-window_bits != MZ_DEFAULT_WINDOW_BITS))) 0 0 0
if ((method != MZ_DEFLATED) || ((mem_level < 1) || (mem_level > 9)) || ((window_bits != MZ_DEFAULT_WINDOW_BITS) && (-window_bits != MZ_DEFAULT_WINDOW_BITS))) 0 0 0
if ((method != MZ_DEFLATED) || ((mem_level < 1) || (mem_level > 9)) || ((window_bits != MZ_DEFAULT_WINDOW_BITS) && (-window_bits != MZ_DEFAULT_WINDOW_BITS))) 0 0 0
if ((method != MZ_DEFLATED) || ((mem_level < 1) || (mem_level > 9)) || ((window_bits != MZ_DEFAULT_WINDOW_BITS) && (-window_bits != MZ_DEFAULT_WINDOW_BITS))) 0 0 0
if ((method != MZ_DEFLATED) || ((mem_level < 1) || (mem_level > 9)) || ((window_bits != MZ_DEFAULT_WINDOW_BITS) && (-window_bits != MZ_DEFAULT_WINDOW_BITS))) 0 0 0
205 0 return MZ_PARAM_ERROR;
206  
207 0 pStream->data_type = 0;
208 0 pStream->adler = MZ_ADLER32_INIT;
209 0 pStream->msg = NULL;
210 0 pStream->reserved = 0;
211 0 pStream->total_in = 0;
212 0 pStream->total_out = 0;
213 0 ▶ if (!pStream->zalloc)
Branch: 1 branch
BranchTrueFalseTotal
if (!pStream->zalloc) 0 0 0
214 0 pStream->zalloc = miniz_def_alloc_func;
215 0 ▶ if (!pStream->zfree)
Branch: 1 branch
BranchTrueFalseTotal
if (!pStream->zfree) 0 0 0
216 0 pStream->zfree = miniz_def_free_func;
217  
218 0 pComp = (tdefl_compressor *)pStream->zalloc(pStream->opaque, 1, sizeof(tdefl_compressor));
219 0 ▶ if (!pComp)
Branch: 1 branch
BranchTrueFalseTotal
if (!pComp) 0 0 0
220 0 return MZ_MEM_ERROR;
221  
222 0 pStream->state = (struct mz_internal_state *)pComp;
223  
224 0 ▶ if (tdefl_init(pComp, NULL, NULL, comp_flags) != TDEFL_STATUS_OKAY)
Branch: 1 branch
BranchTrueFalseTotal
if (tdefl_init(pComp, NULL, NULL, comp_flags) != TDEFL_STATUS_OKAY) 0 0 0
225 {
226 0 mz_deflateEnd(pStream);
227 0 return MZ_PARAM_ERROR;
228 }
229  
230 0 return MZ_OK;
231 }
232  
233 0 int mz_deflateReset(mz_streamp pStream)
234 {
235 0 ▶ if ((!pStream) || (!pStream->state) || (!pStream->zalloc) || (!pStream->zfree))
Branch: 4 branches
BranchTrueFalseTotal
if ((!pStream) || (!pStream->state) || (!pStream->zalloc) || (!pStream->zfree)) 0 0 0
if ((!pStream) || (!pStream->state) || (!pStream->zalloc) || (!pStream->zfree)) 0 0 0
if ((!pStream) || (!pStream->state) || (!pStream->zalloc) || (!pStream->zfree)) 0 0 0
if ((!pStream) || (!pStream->state) || (!pStream->zalloc) || (!pStream->zfree)) 0 0 0
236 0 return MZ_STREAM_ERROR;
237 0 pStream->total_in = pStream->total_out = 0;
238 0 tdefl_init((tdefl_compressor *)pStream->state, NULL, NULL, ((tdefl_compressor *)pStream->state)->m_flags);
239 0 return MZ_OK;
240 }
241  
242 0 int mz_deflate(mz_streamp pStream, int flush)
243 {
244 size_t in_bytes, out_bytes;
245 mz_ulong orig_total_in, orig_total_out;
246 0 int mz_status = MZ_OK;
247  
248 0 ▶ if ((!pStream) || (!pStream->state) || (flush < 0) || (flush > MZ_FINISH) || (!pStream->next_out))
Branch: 5 branches
BranchTrueFalseTotal
if ((!pStream) || (!pStream->state) || (flush < 0) || (flush > MZ_FINISH) || (!pStream->next_out)) 0 0 0
if ((!pStream) || (!pStream->state) || (flush < 0) || (flush > MZ_FINISH) || (!pStream->next_out)) 0 0 0
if ((!pStream) || (!pStream->state) || (flush < 0) || (flush > MZ_FINISH) || (!pStream->next_out)) 0 0 0
if ((!pStream) || (!pStream->state) || (flush < 0) || (flush > MZ_FINISH) || (!pStream->next_out)) 0 0 0
if ((!pStream) || (!pStream->state) || (flush < 0) || (flush > MZ_FINISH) || (!pStream->next_out)) 0 0 0
249 0 return MZ_STREAM_ERROR;
250 0 ▶ if (!pStream->avail_out)
Branch: 1 branch
BranchTrueFalseTotal
if (!pStream->avail_out) 0 0 0
251 0 return MZ_BUF_ERROR;
252  
253 0 ▶ if (flush == MZ_PARTIAL_FLUSH)
Branch: 1 branch
BranchTrueFalseTotal
if (flush == MZ_PARTIAL_FLUSH) 0 0 0
254 0 flush = MZ_SYNC_FLUSH;
255  
256 0 ▶ if (((tdefl_compressor *)pStream->state)->m_prev_return_status == TDEFL_STATUS_DONE)
Branch: 1 branch
BranchTrueFalseTotal
if (((tdefl_compressor *)pStream->state)->m_prev_return_status == TDEFL_STATUS_DONE) 0 0 0
257 0 ▶ return (flush == MZ_FINISH) ? MZ_STREAM_END : MZ_BUF_ERROR;
Branch: 1 branch
BranchTrueFalseTotal
return (flush == MZ_FINISH) ? MZ_STREAM_END : MZ_BUF_ERROR; 0 0 0
258  
259 0 orig_total_in = pStream->total_in;
260 0 orig_total_out = pStream->total_out;
261 for (;;)
262 0 {
263 tdefl_status defl_status;
264 0 in_bytes = pStream->avail_in;
265 0 out_bytes = pStream->avail_out;
266  
267 0 defl_status = tdefl_compress((tdefl_compressor *)pStream->state, pStream->next_in, &in_bytes, pStream->next_out, &out_bytes, (tdefl_flush)flush);
268 0 pStream->next_in += (mz_uint)in_bytes;
269 0 pStream->avail_in -= (mz_uint)in_bytes;
270 0 pStream->total_in += (mz_uint)in_bytes;
271 0 pStream->adler = tdefl_get_adler32((tdefl_compressor *)pStream->state);
272  
273 0 pStream->next_out += (mz_uint)out_bytes;
274 0 pStream->avail_out -= (mz_uint)out_bytes;
275 0 pStream->total_out += (mz_uint)out_bytes;
276  
277 0 ▶ if (defl_status < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (defl_status < 0) 0 0 0
278 {
279 0 mz_status = MZ_STREAM_ERROR;
280 0 break;
281 }
282 0 ▶ else if (defl_status == TDEFL_STATUS_DONE)
Branch: 1 branch
BranchTrueFalseTotal
else if (defl_status == TDEFL_STATUS_DONE) 0 0 0
283 {
284 0 mz_status = MZ_STREAM_END;
285 0 break;
286 }
287 0 ▶ else if (!pStream->avail_out)
Branch: 1 branch
BranchTrueFalseTotal
else if (!pStream->avail_out) 0 0 0
288 0 break;
289 0 ▶ else if ((!pStream->avail_in) && (flush != MZ_FINISH))
Branch: 2 branches
BranchTrueFalseTotal
else if ((!pStream->avail_in) && (flush != MZ_FINISH)) 0 0 0
else if ((!pStream->avail_in) && (flush != MZ_FINISH)) 0 0 0
290 {
291 0 ▶ if ((flush) || (pStream->total_in != orig_total_in) || (pStream->total_out != orig_total_out))
Branch: 3 branches
BranchTrueFalseTotal
if ((flush) || (pStream->total_in != orig_total_in) || (pStream->total_out != orig_total_out)) 0 0 0
if ((flush) || (pStream->total_in != orig_total_in) || (pStream->total_out != orig_total_out)) 0 0 0
if ((flush) || (pStream->total_in != orig_total_in) || (pStream->total_out != orig_total_out)) 0 0 0
292 break;
293 0 return MZ_BUF_ERROR; /* Can't make forward progress without some input.
294 */
295 }
296 }
297 0 return mz_status;
298 }
299  
300 0 int mz_deflateEnd(mz_streamp pStream)
301 {
302 0 ▶ if (!pStream)
Branch: 1 branch
BranchTrueFalseTotal
if (!pStream) 0 0 0
303 0 return MZ_STREAM_ERROR;
304 0 ▶ if (pStream->state)
Branch: 1 branch
BranchTrueFalseTotal
if (pStream->state) 0 0 0
305 {
306 0 pStream->zfree(pStream->opaque, pStream->state);
307 0 pStream->state = NULL;
308 }
309 0 return MZ_OK;
310 }
311  
312 0 mz_ulong mz_deflateBound(mz_streamp pStream, mz_ulong source_len)
313 {
314 (void)pStream;
315 /* This is really over conservative. (And lame, but it's actually pretty tricky to compute a true upper bound given the way tdefl's blocking works.) */
316 0 return MZ_MAX(128 + (source_len * 110) / 100, 128 + source_len + ((source_len / (31 * 1024)) + 1) * 5);
317 }
318  
319 0 int mz_compress2(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len, int level)
320 {
321 int status;
322 mz_stream stream;
323 0 memset(&stream, 0, sizeof(stream));
324  
325 /* In case mz_ulong is 64-bits (argh I hate longs). */
326 0 ▶ if ((mz_uint64)(source_len | *pDest_len) > 0xFFFFFFFFU)
Branch: 1 branch
BranchTrueFalseTotal
if ((mz_uint64)(source_len | *pDest_len) > 0xFFFFFFFFU) 0 0 0
327 0 return MZ_PARAM_ERROR;
328  
329 0 stream.next_in = pSource;
330 0 stream.avail_in = (mz_uint32)source_len;
331 0 stream.next_out = pDest;
332 0 stream.avail_out = (mz_uint32)*pDest_len;
333  
334 0 status = mz_deflateInit(&stream, level);
335 0 ▶ if (status != MZ_OK)
Branch: 1 branch
BranchTrueFalseTotal
if (status != MZ_OK) 0 0 0
336 0 return status;
337  
338 0 status = mz_deflate(&stream, MZ_FINISH);
339 0 ▶ if (status != MZ_STREAM_END)
Branch: 1 branch
BranchTrueFalseTotal
if (status != MZ_STREAM_END) 0 0 0
340 {
341 0 mz_deflateEnd(&stream);
342 0 ▶ return (status == MZ_OK) ? MZ_BUF_ERROR : status;
Branch: 1 branch
BranchTrueFalseTotal
return (status == MZ_OK) ? MZ_BUF_ERROR : status; 0 0 0
343 }
344  
345 0 *pDest_len = stream.total_out;
346 0 return mz_deflateEnd(&stream);
347 }
348  
349 0 int mz_compress(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len)
350 {
351 0 return mz_compress2(pDest, pDest_len, pSource, source_len, MZ_DEFAULT_COMPRESSION);
352 }
353  
354 0 mz_ulong mz_compressBound(mz_ulong source_len)
355 {
356 0 return mz_deflateBound(NULL, source_len);
357 }
358  
359 #endif /*#ifndef MINIZ_NO_DEFLATE_APIS*/
360  
361 #ifndef MINIZ_NO_INFLATE_APIS
362  
363 typedef struct
364 {
365 tinfl_decompressor m_decomp;
366 mz_uint m_dict_ofs, m_dict_avail, m_first_call, m_has_flushed;
367 int m_window_bits;
368 mz_uint8 m_dict[TINFL_LZ_DICT_SIZE];
369 tinfl_status m_last_status;
370 } inflate_state;
371  
372 0 int mz_inflateInit2(mz_streamp pStream, int window_bits)
373 {
374 inflate_state *pDecomp;
375 0 ▶ if (!pStream)
Branch: 1 branch
BranchTrueFalseTotal
if (!pStream) 0 0 0
376 0 return MZ_STREAM_ERROR;
377 0 ▶ if ((window_bits != MZ_DEFAULT_WINDOW_BITS) && (-window_bits != MZ_DEFAULT_WINDOW_BITS))
Branch: 2 branches
BranchTrueFalseTotal
if ((window_bits != MZ_DEFAULT_WINDOW_BITS) && (-window_bits != MZ_DEFAULT_WINDOW_BITS)) 0 0 0
if ((window_bits != MZ_DEFAULT_WINDOW_BITS) && (-window_bits != MZ_DEFAULT_WINDOW_BITS)) 0 0 0
378 0 return MZ_PARAM_ERROR;
379  
380 0 pStream->data_type = 0;
381 0 pStream->adler = 0;
382 0 pStream->msg = NULL;
383 0 pStream->total_in = 0;
384 0 pStream->total_out = 0;
385 0 pStream->reserved = 0;
386 0 ▶ if (!pStream->zalloc)
Branch: 1 branch
BranchTrueFalseTotal
if (!pStream->zalloc) 0 0 0
387 0 pStream->zalloc = miniz_def_alloc_func;
388 0 ▶ if (!pStream->zfree)
Branch: 1 branch
BranchTrueFalseTotal
if (!pStream->zfree) 0 0 0
389 0 pStream->zfree = miniz_def_free_func;
390  
391 0 pDecomp = (inflate_state *)pStream->zalloc(pStream->opaque, 1, sizeof(inflate_state));
392 0 ▶ if (!pDecomp)
Branch: 1 branch
BranchTrueFalseTotal
if (!pDecomp) 0 0 0
393 0 return MZ_MEM_ERROR;
394  
395 0 pStream->state = (struct mz_internal_state *)pDecomp;
396  
397 0 tinfl_init(&pDecomp->m_decomp);
398 0 pDecomp->m_dict_ofs = 0;
399 0 pDecomp->m_dict_avail = 0;
400 0 pDecomp->m_last_status = TINFL_STATUS_NEEDS_MORE_INPUT;
401 0 pDecomp->m_first_call = 1;
402 0 pDecomp->m_has_flushed = 0;
403 0 pDecomp->m_window_bits = window_bits;
404  
405 0 return MZ_OK;
406 }
407  
408 0 int mz_inflateInit(mz_streamp pStream)
409 {
410 0 return mz_inflateInit2(pStream, MZ_DEFAULT_WINDOW_BITS);
411 }
412  
413 0 int mz_inflateReset(mz_streamp pStream)
414 {
415 inflate_state *pDecomp;
416 0 ▶ if (!pStream)
Branch: 1 branch
BranchTrueFalseTotal
if (!pStream) 0 0 0
417 0 return MZ_STREAM_ERROR;
418  
419 0 pStream->data_type = 0;
420 0 pStream->adler = 0;
421 0 pStream->msg = NULL;
422 0 pStream->total_in = 0;
423 0 pStream->total_out = 0;
424 0 pStream->reserved = 0;
425  
426 0 pDecomp = (inflate_state *)pStream->state;
427  
428 0 tinfl_init(&pDecomp->m_decomp);
429 0 pDecomp->m_dict_ofs = 0;
430 0 pDecomp->m_dict_avail = 0;
431 0 pDecomp->m_last_status = TINFL_STATUS_NEEDS_MORE_INPUT;
432 0 pDecomp->m_first_call = 1;
433 0 pDecomp->m_has_flushed = 0;
434 /* pDecomp->m_window_bits = window_bits */;
435  
436 0 return MZ_OK;
437 }
438  
439 0 int mz_inflate(mz_streamp pStream, int flush)
440 {
441 inflate_state *pState;
442 0 mz_uint n, first_call, decomp_flags = TINFL_FLAG_COMPUTE_ADLER32;
443 size_t in_bytes, out_bytes, orig_avail_in;
444 tinfl_status status;
445  
446 0 ▶ if ((!pStream) || (!pStream->state))
Branch: 2 branches
BranchTrueFalseTotal
if ((!pStream) || (!pStream->state)) 0 0 0
if ((!pStream) || (!pStream->state)) 0 0 0
447 0 return MZ_STREAM_ERROR;
448 0 ▶ if (flush == MZ_PARTIAL_FLUSH)
Branch: 1 branch
BranchTrueFalseTotal
if (flush == MZ_PARTIAL_FLUSH) 0 0 0
449 0 flush = MZ_SYNC_FLUSH;
450 0 ▶ if ((flush) && (flush != MZ_SYNC_FLUSH) && (flush != MZ_FINISH))
Branch: 3 branches
BranchTrueFalseTotal
if ((flush) && (flush != MZ_SYNC_FLUSH) && (flush != MZ_FINISH)) 0 0 0
if ((flush) && (flush != MZ_SYNC_FLUSH) && (flush != MZ_FINISH)) 0 0 0
if ((flush) && (flush != MZ_SYNC_FLUSH) && (flush != MZ_FINISH)) 0 0 0
451 0 return MZ_STREAM_ERROR;
452  
453 0 pState = (inflate_state *)pStream->state;
454 0 ▶ if (pState->m_window_bits > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (pState->m_window_bits > 0) 0 0 0
455 0 decomp_flags |= TINFL_FLAG_PARSE_ZLIB_HEADER;
456 0 orig_avail_in = pStream->avail_in;
457  
458 0 first_call = pState->m_first_call;
459 0 pState->m_first_call = 0;
460 0 ▶ if (pState->m_last_status < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (pState->m_last_status < 0) 0 0 0
461 0 return MZ_DATA_ERROR;
462  
463 0 ▶ if (pState->m_has_flushed && (flush != MZ_FINISH))
Branch: 2 branches
BranchTrueFalseTotal
if (pState->m_has_flushed && (flush != MZ_FINISH)) 0 0 0
if (pState->m_has_flushed && (flush != MZ_FINISH)) 0 0 0
464 0 return MZ_STREAM_ERROR;
465 0 pState->m_has_flushed |= (flush == MZ_FINISH);
466  
467 0 ▶ if ((flush == MZ_FINISH) && (first_call))
Branch: 2 branches
BranchTrueFalseTotal
if ((flush == MZ_FINISH) && (first_call)) 0 0 0
if ((flush == MZ_FINISH) && (first_call)) 0 0 0
468 {
469 /* MZ_FINISH on the first call implies that the input and output buffers are large enough to hold the entire compressed/decompressed file. */
470 0 decomp_flags |= TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF;
471 0 in_bytes = pStream->avail_in;
472 0 out_bytes = pStream->avail_out;
473 0 status = tinfl_decompress(&pState->m_decomp, pStream->next_in, &in_bytes, pStream->next_out, pStream->next_out, &out_bytes, decomp_flags);
474 0 pState->m_last_status = status;
475 0 pStream->next_in += (mz_uint)in_bytes;
476 0 pStream->avail_in -= (mz_uint)in_bytes;
477 0 pStream->total_in += (mz_uint)in_bytes;
478 0 pStream->adler = tinfl_get_adler32(&pState->m_decomp);
479 0 pStream->next_out += (mz_uint)out_bytes;
480 0 pStream->avail_out -= (mz_uint)out_bytes;
481 0 pStream->total_out += (mz_uint)out_bytes;
482  
483 0 ▶ if (status < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (status < 0) 0 0 0
484 0 return MZ_DATA_ERROR;
485 0 ▶ else if (status != TINFL_STATUS_DONE)
Branch: 1 branch
BranchTrueFalseTotal
else if (status != TINFL_STATUS_DONE) 0 0 0
486 {
487 0 pState->m_last_status = TINFL_STATUS_FAILED;
488 0 return MZ_BUF_ERROR;
489 }
490 0 return MZ_STREAM_END;
491 }
492 /* flush != MZ_FINISH then we must assume there's more input. */
493 0 ▶ if (flush != MZ_FINISH)
Branch: 1 branch
BranchTrueFalseTotal
if (flush != MZ_FINISH) 0 0 0
494 0 decomp_flags |= TINFL_FLAG_HAS_MORE_INPUT;
495  
496 0 ▶ if (pState->m_dict_avail)
Branch: 1 branch
BranchTrueFalseTotal
if (pState->m_dict_avail) 0 0 0
497 {
498 0 n = MZ_MIN(pState->m_dict_avail, pStream->avail_out);
499 0 memcpy(pStream->next_out, pState->m_dict + pState->m_dict_ofs, n);
500 0 pStream->next_out += n;
501 0 pStream->avail_out -= n;
502 0 pStream->total_out += n;
503 0 pState->m_dict_avail -= n;
504 0 pState->m_dict_ofs = (pState->m_dict_ofs + n) & (TINFL_LZ_DICT_SIZE - 1);
505 0 ▶ return ((pState->m_last_status == TINFL_STATUS_DONE) && (!pState->m_dict_avail)) ? MZ_STREAM_END : MZ_OK;
Branch: 2 branches
BranchTrueFalseTotal
return ((pState->m_last_status == TINFL_STATUS_DONE) && (!pState->m_dict_avail)) ? MZ_STREAM_END : MZ_OK; 0 0 0
return ((pState->m_last_status == TINFL_STATUS_DONE) && (!pState->m_dict_avail)) ? MZ_STREAM_END : MZ_OK; 0 0 0
506 }
507  
508 for (;;)
509 {
510 0 in_bytes = pStream->avail_in;
511 0 out_bytes = TINFL_LZ_DICT_SIZE - pState->m_dict_ofs;
512  
513 0 status = tinfl_decompress(&pState->m_decomp, pStream->next_in, &in_bytes, pState->m_dict, pState->m_dict + pState->m_dict_ofs, &out_bytes, decomp_flags);
514 0 pState->m_last_status = status;
515  
516 0 pStream->next_in += (mz_uint)in_bytes;
517 0 pStream->avail_in -= (mz_uint)in_bytes;
518 0 pStream->total_in += (mz_uint)in_bytes;
519 0 pStream->adler = tinfl_get_adler32(&pState->m_decomp);
520  
521 0 pState->m_dict_avail = (mz_uint)out_bytes;
522  
523 0 n = MZ_MIN(pState->m_dict_avail, pStream->avail_out);
524 0 memcpy(pStream->next_out, pState->m_dict + pState->m_dict_ofs, n);
525 0 pStream->next_out += n;
526 0 pStream->avail_out -= n;
527 0 pStream->total_out += n;
528 0 pState->m_dict_avail -= n;
529 0 pState->m_dict_ofs = (pState->m_dict_ofs + n) & (TINFL_LZ_DICT_SIZE - 1);
530  
531 0 ▶ if (status < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (status < 0) 0 0 0
532 0 return MZ_DATA_ERROR; /* Stream is corrupted (there could be some uncompressed data left in the output dictionary - oh well). */
533 0 ▶ else if ((status == TINFL_STATUS_NEEDS_MORE_INPUT) && (!orig_avail_in))
Branch: 2 branches
BranchTrueFalseTotal
else if ((status == TINFL_STATUS_NEEDS_MORE_INPUT) && (!orig_avail_in)) 0 0 0
else if ((status == TINFL_STATUS_NEEDS_MORE_INPUT) && (!orig_avail_in)) 0 0 0
534 0 return MZ_BUF_ERROR; /* Signal caller that we can't make forward progress without supplying more input or by setting flush to MZ_FINISH. */
535 0 ▶ else if (flush == MZ_FINISH)
Branch: 1 branch
BranchTrueFalseTotal
else if (flush == MZ_FINISH) 0 0 0
536 {
537 /* The output buffer MUST be large to hold the remaining uncompressed data when flush==MZ_FINISH. */
538 0 ▶ if (status == TINFL_STATUS_DONE)
Branch: 1 branch
BranchTrueFalseTotal
if (status == TINFL_STATUS_DONE) 0 0 0
539 0 ▶ return pState->m_dict_avail ? MZ_BUF_ERROR : MZ_STREAM_END;
Branch: 1 branch
BranchTrueFalseTotal
return pState->m_dict_avail ? MZ_BUF_ERROR : MZ_STREAM_END; 0 0 0
540 /* status here must be TINFL_STATUS_HAS_MORE_OUTPUT, which means there's at least 1 more byte on the way. If there's no more room left in the output buffer then something is wrong. */
541 0 ▶ else if (!pStream->avail_out)
Branch: 1 branch
BranchTrueFalseTotal
else if (!pStream->avail_out) 0 0 0
542 0 return MZ_BUF_ERROR;
543 }
544 0 ▶ else if ((status == TINFL_STATUS_DONE) || (!pStream->avail_in) || (!pStream->avail_out) || (pState->m_dict_avail))
Branch: 4 branches
BranchTrueFalseTotal
else if ((status == TINFL_STATUS_DONE) || (!pStream->avail_in) || (!pStream->avail_out) || (pState->m_dict_avail)) 0 0 0
else if ((status == TINFL_STATUS_DONE) || (!pStream->avail_in) || (!pStream->avail_out) || (pState->m_dict_avail)) 0 0 0
else if ((status == TINFL_STATUS_DONE) || (!pStream->avail_in) || (!pStream->avail_out) || (pState->m_dict_avail)) 0 0 0
else if ((status == TINFL_STATUS_DONE) || (!pStream->avail_in) || (!pStream->avail_out) || (pState->m_dict_avail)) 0 0 0
545 break;
546 }
547  
548 0 ▶ return ((status == TINFL_STATUS_DONE) && (!pState->m_dict_avail)) ? MZ_STREAM_END : MZ_OK;
Branch: 2 branches
BranchTrueFalseTotal
return ((status == TINFL_STATUS_DONE) && (!pState->m_dict_avail)) ? MZ_STREAM_END : MZ_OK; 0 0 0
return ((status == TINFL_STATUS_DONE) && (!pState->m_dict_avail)) ? MZ_STREAM_END : MZ_OK; 0 0 0
549 }
550  
551 0 int mz_inflateEnd(mz_streamp pStream)
552 {
553 0 ▶ if (!pStream)
Branch: 1 branch
BranchTrueFalseTotal
if (!pStream) 0 0 0
554 0 return MZ_STREAM_ERROR;
555 0 ▶ if (pStream->state)
Branch: 1 branch
BranchTrueFalseTotal
if (pStream->state) 0 0 0
556 {
557 0 pStream->zfree(pStream->opaque, pStream->state);
558 0 pStream->state = NULL;
559 }
560 0 return MZ_OK;
561 }
562 0 int mz_uncompress2(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong *pSource_len)
563 {
564 mz_stream stream;
565 int status;
566 0 memset(&stream, 0, sizeof(stream));
567  
568 /* In case mz_ulong is 64-bits (argh I hate longs). */
569 0 ▶ if ((mz_uint64)(*pSource_len | *pDest_len) > 0xFFFFFFFFU)
Branch: 1 branch
BranchTrueFalseTotal
if ((mz_uint64)(*pSource_len | *pDest_len) > 0xFFFFFFFFU) 0 0 0
570 0 return MZ_PARAM_ERROR;
571  
572 0 stream.next_in = pSource;
573 0 stream.avail_in = (mz_uint32)*pSource_len;
574 0 stream.next_out = pDest;
575 0 stream.avail_out = (mz_uint32)*pDest_len;
576  
577 0 status = mz_inflateInit(&stream);
578 0 ▶ if (status != MZ_OK)
Branch: 1 branch
BranchTrueFalseTotal
if (status != MZ_OK) 0 0 0
579 0 return status;
580  
581 0 status = mz_inflate(&stream, MZ_FINISH);
582 0 *pSource_len = *pSource_len - stream.avail_in;
583 0 ▶ if (status != MZ_STREAM_END)
Branch: 1 branch
BranchTrueFalseTotal
if (status != MZ_STREAM_END) 0 0 0
584 {
585 0 mz_inflateEnd(&stream);
586 0 ▶ return ((status == MZ_BUF_ERROR) && (!stream.avail_in)) ? MZ_DATA_ERROR : status;
Branch: 2 branches
BranchTrueFalseTotal
return ((status == MZ_BUF_ERROR) && (!stream.avail_in)) ? MZ_DATA_ERROR : status; 0 0 0
return ((status == MZ_BUF_ERROR) && (!stream.avail_in)) ? MZ_DATA_ERROR : status; 0 0 0
587 }
588 0 *pDest_len = stream.total_out;
589  
590 0 return mz_inflateEnd(&stream);
591 }
592  
593 0 int mz_uncompress(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len)
594 {
595 0 return mz_uncompress2(pDest, pDest_len, pSource, &source_len);
596 }
597  
598 #endif /*#ifndef MINIZ_NO_INFLATE_APIS*/
599  
600 0 const char *mz_error(int err)
601 {
602 static struct
603 {
604 int m_err;
605 const char *m_pDesc;
606 } s_error_descs[] = {
607 { MZ_OK, "" }, { MZ_STREAM_END, "stream end" }, { MZ_NEED_DICT, "need dictionary" }, { MZ_ERRNO, "file error" }, { MZ_STREAM_ERROR, "stream error" }, { MZ_DATA_ERROR, "data error" }, { MZ_MEM_ERROR, "out of memory" }, { MZ_BUF_ERROR, "buf error" }, { MZ_VERSION_ERROR, "version error" }, { MZ_PARAM_ERROR, "parameter error" }
608 };
609 mz_uint i;
610 0 ▶ for (i = 0; i < sizeof(s_error_descs) / sizeof(s_error_descs[0]); ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < sizeof(s_error_descs) / sizeof(s_error_descs[0]); ++i) 0 0 0
611 0 ▶ if (s_error_descs[i].m_err == err)
Branch: 1 branch
BranchTrueFalseTotal
if (s_error_descs[i].m_err == err) 0 0 0
612 0 return s_error_descs[i].m_pDesc;
613 0 return NULL;
614 }
615  
616 #endif /*MINIZ_NO_ZLIB_APIS */
617  
618 #ifdef __cplusplus
619 }
620 #endif
621  
622 /*
623 This is free and unencumbered software released into the public domain.
624  
625 Anyone is free to copy, modify, publish, use, compile, sell, or
626 distribute this software, either in source code form or as a compiled
627 binary, for any purpose, commercial or non-commercial, and by any
628 means.
629  
630 In jurisdictions that recognize copyright laws, the author or authors
631 of this software dedicate any and all copyright interest in the
632 software to the public domain. We make this dedication for the benefit
633 of the public at large and to the detriment of our heirs and
634 successors. We intend this dedication to be an overt act of
635 relinquishment in perpetuity of all present and future rights to this
636 software under copyright law.
637  
638 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
639 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
640 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
641 IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
642 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
643 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
644 OTHER DEALINGS IN THE SOFTWARE.
645  
646 For more information, please refer to <http://unlicense.org/>
647 */
648 /**************************************************************************
649 *
650 * Copyright 2013-2014 RAD Game Tools and Valve Software
651 * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
652 * All Rights Reserved.
653 *
654 * Permission is hereby granted, free of charge, to any person obtaining a copy
655 * of this software and associated documentation files (the "Software"), to deal
656 * in the Software without restriction, including without limitation the rights
657 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
658 * copies of the Software, and to permit persons to whom the Software is
659 * furnished to do so, subject to the following conditions:
660 *
661 * The above copyright notice and this permission notice shall be included in
662 * all copies or substantial portions of the Software.
663 *
664 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
665 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
666 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
667 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
668 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
669 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
670 * THE SOFTWARE.
671 *
672 **************************************************************************/
673  
674  
675  
676 #ifndef MINIZ_NO_DEFLATE_APIS
677  
678 #ifdef __cplusplus
679 extern "C"
680 {
681 #endif
682  
683 /* ------------------- Low-level Compression (independent from all decompression API's) */
684  
685 /* Purposely making these tables static for faster init and thread safety. */
686 static const mz_uint16 s_tdefl_len_sym[256] = {
687 257, 258, 259, 260, 261, 262, 263, 264, 265, 265, 266, 266, 267, 267, 268, 268, 269, 269, 269, 269, 270, 270, 270, 270, 271, 271, 271, 271, 272, 272, 272, 272,
688 273, 273, 273, 273, 273, 273, 273, 273, 274, 274, 274, 274, 274, 274, 274, 274, 275, 275, 275, 275, 275, 275, 275, 275, 276, 276, 276, 276, 276, 276, 276, 276,
689 277, 277, 277, 277, 277, 277, 277, 277, 277, 277, 277, 277, 277, 277, 277, 277, 278, 278, 278, 278, 278, 278, 278, 278, 278, 278, 278, 278, 278, 278, 278, 278,
690 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, 279, 280, 280, 280, 280, 280, 280, 280, 280, 280, 280, 280, 280, 280, 280, 280, 280,
691 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281, 281,
692 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282, 282,
693 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283, 283,
694 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 284, 285
695 };
696  
697 static const mz_uint8 s_tdefl_len_extra[256] = {
698 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
699 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
700 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
701 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 0
702 };
703  
704 static const mz_uint8 s_tdefl_small_dist_sym[512] = {
705 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11,
706 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13,
707 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
708 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
709 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
710 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
711 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
712 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
713 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
714 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
715 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
716 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17
717 };
718  
719 static const mz_uint8 s_tdefl_small_dist_extra[512] = {
720 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,
721 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
722 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
723 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
724 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
725 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
726 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
727 7, 7, 7, 7, 7, 7, 7, 7
728 };
729  
730 static const mz_uint8 s_tdefl_large_dist_sym[128] = {
731 0, 0, 18, 19, 20, 20, 21, 21, 22, 22, 22, 22, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
732 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
733 28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29
734 };
735  
736 static const mz_uint8 s_tdefl_large_dist_extra[128] = {
737 0, 0, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
738 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
739 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13
740 };
741  
742 /* Radix sorts tdefl_sym_freq[] array by 16-bit key m_key. Returns ptr to sorted values. */
743 typedef struct
744 {
745 mz_uint16 m_key, m_sym_index;
746 } tdefl_sym_freq;
747 0 static tdefl_sym_freq *tdefl_radix_sort_syms(mz_uint num_syms, tdefl_sym_freq *pSyms0, tdefl_sym_freq *pSyms1)
748 {
749 0 mz_uint32 total_passes = 2, pass_shift, pass, i, hist[256 * 2];
750 0 tdefl_sym_freq *pCur_syms = pSyms0, *pNew_syms = pSyms1;
751 0 MZ_CLEAR_ARR(hist);
752 0 ▶ for (i = 0; i < num_syms; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < num_syms; i++) 0 0 0
753 {
754 0 mz_uint freq = pSyms0[i].m_key;
755 0 hist[freq & 0xFF]++;
756 0 hist[256 + ((freq >> 8) & 0xFF)]++;
757 }
758 0 ▶ while ((total_passes > 1) && (num_syms == hist[(total_passes - 1) * 256]))
Branch: 2 branches
BranchTrueFalseTotal
while ((total_passes > 1) && (num_syms == hist[(total_passes - 1) * 256])) 0 0 0
while ((total_passes > 1) && (num_syms == hist[(total_passes - 1) * 256])) 0 0 0
759 0 total_passes--;
760 0 ▶ for (pass_shift = 0, pass = 0; pass < total_passes; pass++, pass_shift += 8)
Branch: 1 branch
BranchTrueFalseTotal
for (pass_shift = 0, pass = 0; pass < total_passes; pass++, pass_shift += 8) 0 0 0
761 {
762 0 const mz_uint32 *pHist = &hist[pass << 8];
763 0 mz_uint offsets[256], cur_ofs = 0;
764 0 ▶ for (i = 0; i < 256; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 256; i++) 0 0 0
765 {
766 0 offsets[i] = cur_ofs;
767 0 cur_ofs += pHist[i];
768 }
769 0 ▶ for (i = 0; i < num_syms; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < num_syms; i++) 0 0 0
770 0 pNew_syms[offsets[(pCur_syms[i].m_key >> pass_shift) & 0xFF]++] = pCur_syms[i];
771 {
772 0 tdefl_sym_freq *t = pCur_syms;
773 0 pCur_syms = pNew_syms;
774 0 pNew_syms = t;
775 }
776 }
777 0 return pCur_syms;
778 }
779  
780 /* tdefl_calculate_minimum_redundancy() originally written by: Alistair Moffat, alistair@cs.mu.oz.au, Jyrki Katajainen, jyrki@diku.dk, November 1996. */
781 0 static void tdefl_calculate_minimum_redundancy(tdefl_sym_freq *A, int n)
782 {
783 int root, leaf, next, avbl, used, dpth;
784 0 ▶ if (n == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) 0 0 0
785 0 return;
786 0 ▶ else if (n == 1)
Branch: 1 branch
BranchTrueFalseTotal
else if (n == 1) 0 0 0
787 {
788 0 A[0].m_key = 1;
789 0 return;
790 }
791 0 A[0].m_key += A[1].m_key;
792 0 root = 0;
793 0 leaf = 2;
794 0 ▶ for (next = 1; next < n - 1; next++)
Branch: 1 branch
BranchTrueFalseTotal
for (next = 1; next < n - 1; next++) 0 0 0
795 {
796 0 ▶ if (leaf >= n || A[root].m_key < A[leaf].m_key)
Branch: 2 branches
BranchTrueFalseTotal
if (leaf >= n || A[root].m_key < A[leaf].m_key) 0 0 0
if (leaf >= n || A[root].m_key < A[leaf].m_key) 0 0 0
797 {
798 0 A[next].m_key = A[root].m_key;
799 0 A[root++].m_key = (mz_uint16)next;
800 }
801 else
802 0 A[next].m_key = A[leaf++].m_key;
803 0 ▶ if (leaf >= n || (root < next && A[root].m_key < A[leaf].m_key))
Branch: 3 branches
BranchTrueFalseTotal
if (leaf >= n || (root < next && A[root].m_key < A[leaf].m_key)) 0 0 0
if (leaf >= n || (root < next && A[root].m_key < A[leaf].m_key)) 0 0 0
if (leaf >= n || (root < next && A[root].m_key < A[leaf].m_key)) 0 0 0
804 {
805 0 A[next].m_key = (mz_uint16)(A[next].m_key + A[root].m_key);
806 0 A[root++].m_key = (mz_uint16)next;
807 }
808 else
809 0 A[next].m_key = (mz_uint16)(A[next].m_key + A[leaf++].m_key);
810 }
811 0 A[n - 2].m_key = 0;
812 0 ▶ for (next = n - 3; next >= 0; next--)
Branch: 1 branch
BranchTrueFalseTotal
for (next = n - 3; next >= 0; next--) 0 0 0
813 0 A[next].m_key = A[A[next].m_key].m_key + 1;
814 0 avbl = 1;
815 0 used = dpth = 0;
816 0 root = n - 2;
817 0 next = n - 1;
818 0 ▶ while (avbl > 0)
Branch: 1 branch
BranchTrueFalseTotal
while (avbl > 0) 0 0 0
819 {
820 0 ▶ while (root >= 0 && (int)A[root].m_key == dpth)
Branch: 2 branches
BranchTrueFalseTotal
while (root >= 0 && (int)A[root].m_key == dpth) 0 0 0
while (root >= 0 && (int)A[root].m_key == dpth) 0 0 0
821 {
822 0 used++;
823 0 root--;
824 }
825 0 ▶ while (avbl > used)
Branch: 1 branch
BranchTrueFalseTotal
while (avbl > used) 0 0 0
826 {
827 0 A[next--].m_key = (mz_uint16)(dpth);
828 0 avbl--;
829 }
830 0 avbl = 2 * used;
831 0 dpth++;
832 0 used = 0;
833 }
834 }
835  
836 /* Limits canonical Huffman code table's max code size. */
837 enum
838 {
839 TDEFL_MAX_SUPPORTED_HUFF_CODESIZE = 32
840 };
841 0 static void tdefl_huffman_enforce_max_code_size(int *pNum_codes, int code_list_len, int max_code_size)
842 {
843 int i;
844 0 mz_uint32 total = 0;
845 0 ▶ if (code_list_len <= 1)
Branch: 1 branch
BranchTrueFalseTotal
if (code_list_len <= 1) 0 0 0
846 0 return;
847 0 ▶ for (i = max_code_size + 1; i <= TDEFL_MAX_SUPPORTED_HUFF_CODESIZE; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = max_code_size + 1; i <= TDEFL_MAX_SUPPORTED_HUFF_CODESIZE; i++) 0 0 0
848 0 pNum_codes[max_code_size] += pNum_codes[i];
849 0 ▶ for (i = max_code_size; i > 0; i--)
Branch: 1 branch
BranchTrueFalseTotal
for (i = max_code_size; i > 0; i--) 0 0 0
850 0 total += (((mz_uint32)pNum_codes[i]) << (max_code_size - i));
851 0 ▶ while (total != (1UL << max_code_size))
Branch: 1 branch
BranchTrueFalseTotal
while (total != (1UL << max_code_size)) 0 0 0
852 {
853 0 pNum_codes[max_code_size]--;
854 0 ▶ for (i = max_code_size - 1; i > 0; i--)
Branch: 1 branch
BranchTrueFalseTotal
for (i = max_code_size - 1; i > 0; i--) 0 0 0
855 0 ▶ if (pNum_codes[i])
Branch: 1 branch
BranchTrueFalseTotal
if (pNum_codes[i]) 0 0 0
856 {
857 0 pNum_codes[i]--;
858 0 pNum_codes[i + 1] += 2;
859 0 break;
860 }
861 0 total--;
862 }
863 }
864  
865 0 static void tdefl_optimize_huffman_table(tdefl_compressor *d, int table_num, int table_len, int code_size_limit, int static_table)
866 {
867 int i, j, l, num_codes[1 + TDEFL_MAX_SUPPORTED_HUFF_CODESIZE];
868 mz_uint next_code[TDEFL_MAX_SUPPORTED_HUFF_CODESIZE + 1];
869 0 MZ_CLEAR_ARR(num_codes);
870 0 ▶ if (static_table)
Branch: 1 branch
BranchTrueFalseTotal
if (static_table) 0 0 0
871 {
872 0 ▶ for (i = 0; i < table_len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < table_len; i++) 0 0 0
873 0 num_codes[d->m_huff_code_sizes[table_num][i]]++;
874 }
875 else
876 {
877 tdefl_sym_freq syms0[TDEFL_MAX_HUFF_SYMBOLS], syms1[TDEFL_MAX_HUFF_SYMBOLS], *pSyms;
878 0 int num_used_syms = 0;
879 0 const mz_uint16 *pSym_count = &d->m_huff_count[table_num][0];
880 0 ▶ for (i = 0; i < table_len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < table_len; i++) 0 0 0
881 0 ▶ if (pSym_count[i])
Branch: 1 branch
BranchTrueFalseTotal
if (pSym_count[i]) 0 0 0
882 {
883 0 syms0[num_used_syms].m_key = (mz_uint16)pSym_count[i];
884 0 syms0[num_used_syms++].m_sym_index = (mz_uint16)i;
885 }
886  
887 0 pSyms = tdefl_radix_sort_syms(num_used_syms, syms0, syms1);
888 0 tdefl_calculate_minimum_redundancy(pSyms, num_used_syms);
889  
890 0 ▶ for (i = 0; i < num_used_syms; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < num_used_syms; i++) 0 0 0
891 0 num_codes[pSyms[i].m_key]++;
892  
893 0 tdefl_huffman_enforce_max_code_size(num_codes, num_used_syms, code_size_limit);
894  
895 0 MZ_CLEAR_ARR(d->m_huff_code_sizes[table_num]);
896 0 MZ_CLEAR_ARR(d->m_huff_codes[table_num]);
897 0 ▶ for (i = 1, j = num_used_syms; i <= code_size_limit; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1, j = num_used_syms; i <= code_size_limit; i++) 0 0 0
898 0 ▶ for (l = num_codes[i]; l > 0; l--)
Branch: 1 branch
BranchTrueFalseTotal
for (l = num_codes[i]; l > 0; l--) 0 0 0
899 0 d->m_huff_code_sizes[table_num][pSyms[--j].m_sym_index] = (mz_uint8)(i);
900 }
901  
902 0 next_code[1] = 0;
903 0 ▶ for (j = 0, i = 2; i <= code_size_limit; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0, i = 2; i <= code_size_limit; i++) 0 0 0
904 0 next_code[i] = j = ((j + num_codes[i - 1]) << 1);
905  
906 0 ▶ for (i = 0; i < table_len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < table_len; i++) 0 0 0
907 {
908 0 mz_uint rev_code = 0, code, code_size;
909 0 ▶ if ((code_size = d->m_huff_code_sizes[table_num][i]) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((code_size = d->m_huff_code_sizes[table_num][i]) == 0) 0 0 0
910 0 continue;
911 0 code = next_code[code_size]++;
912 0 ▶ for (l = code_size; l > 0; l--, code >>= 1)
Branch: 1 branch
BranchTrueFalseTotal
for (l = code_size; l > 0; l--, code >>= 1) 0 0 0
913 0 rev_code = (rev_code << 1) | (code & 1);
914 0 d->m_huff_codes[table_num][i] = (mz_uint16)rev_code;
915 }
916 0 }
917  
918 #define TDEFL_PUT_BITS(b, l) \
919 do \
920 { \
921 mz_uint bits = b; \
922 mz_uint len = l; \
923 MZ_ASSERT(bits <= ((1U << len) - 1U)); \
924 d->m_bit_buffer |= (bits << d->m_bits_in); \
925 d->m_bits_in += len; \
926 while (d->m_bits_in >= 8) \
927 { \
928 if (d->m_pOutput_buf < d->m_pOutput_buf_end) \
929 *d->m_pOutput_buf++ = (mz_uint8)(d->m_bit_buffer); \
930 d->m_bit_buffer >>= 8; \
931 d->m_bits_in -= 8; \
932 } \
933 } \
934 MZ_MACRO_END
935  
936 #define TDEFL_RLE_PREV_CODE_SIZE() \
937 { \
938 if (rle_repeat_count) \
939 { \
940 if (rle_repeat_count < 3) \
941 { \
942 d->m_huff_count[2][prev_code_size] = (mz_uint16)(d->m_huff_count[2][prev_code_size] + rle_repeat_count); \
943 while (rle_repeat_count--) \
944 packed_code_sizes[num_packed_code_sizes++] = prev_code_size; \
945 } \
946 else \
947 { \
948 d->m_huff_count[2][16] = (mz_uint16)(d->m_huff_count[2][16] + 1); \
949 packed_code_sizes[num_packed_code_sizes++] = 16; \
950 packed_code_sizes[num_packed_code_sizes++] = (mz_uint8)(rle_repeat_count - 3); \
951 } \
952 rle_repeat_count = 0; \
953 } \
954 }
955  
956 #define TDEFL_RLE_ZERO_CODE_SIZE() \
957 { \
958 if (rle_z_count) \
959 { \
960 if (rle_z_count < 3) \
961 { \
962 d->m_huff_count[2][0] = (mz_uint16)(d->m_huff_count[2][0] + rle_z_count); \
963 while (rle_z_count--) \
964 packed_code_sizes[num_packed_code_sizes++] = 0; \
965 } \
966 else if (rle_z_count <= 10) \
967 { \
968 d->m_huff_count[2][17] = (mz_uint16)(d->m_huff_count[2][17] + 1); \
969 packed_code_sizes[num_packed_code_sizes++] = 17; \
970 packed_code_sizes[num_packed_code_sizes++] = (mz_uint8)(rle_z_count - 3); \
971 } \
972 else \
973 { \
974 d->m_huff_count[2][18] = (mz_uint16)(d->m_huff_count[2][18] + 1); \
975 packed_code_sizes[num_packed_code_sizes++] = 18; \
976 packed_code_sizes[num_packed_code_sizes++] = (mz_uint8)(rle_z_count - 11); \
977 } \
978 rle_z_count = 0; \
979 } \
980 }
981  
982 static const mz_uint8 s_tdefl_packed_code_size_syms_swizzle[] = { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 };
983  
984 0 static void tdefl_start_dynamic_block(tdefl_compressor *d)
985 {
986 int num_lit_codes, num_dist_codes, num_bit_lengths;
987 mz_uint i, total_code_sizes_to_pack, num_packed_code_sizes, rle_z_count, rle_repeat_count, packed_code_sizes_index;
988 0 mz_uint8 code_sizes_to_pack[TDEFL_MAX_HUFF_SYMBOLS_0 + TDEFL_MAX_HUFF_SYMBOLS_1], packed_code_sizes[TDEFL_MAX_HUFF_SYMBOLS_0 + TDEFL_MAX_HUFF_SYMBOLS_1], prev_code_size = 0xFF;
989  
990 0 d->m_huff_count[0][256] = 1;
991  
992 0 tdefl_optimize_huffman_table(d, 0, TDEFL_MAX_HUFF_SYMBOLS_0, 15, MZ_FALSE);
993 0 tdefl_optimize_huffman_table(d, 1, TDEFL_MAX_HUFF_SYMBOLS_1, 15, MZ_FALSE);
994  
995 0 ▶ for (num_lit_codes = 286; num_lit_codes > 257; num_lit_codes--)
Branch: 1 branch
BranchTrueFalseTotal
for (num_lit_codes = 286; num_lit_codes > 257; num_lit_codes--) 0 0 0
996 0 ▶ if (d->m_huff_code_sizes[0][num_lit_codes - 1])
Branch: 1 branch
BranchTrueFalseTotal
if (d->m_huff_code_sizes[0][num_lit_codes - 1]) 0 0 0
997 0 break;
998 0 ▶ for (num_dist_codes = 30; num_dist_codes > 1; num_dist_codes--)
Branch: 1 branch
BranchTrueFalseTotal
for (num_dist_codes = 30; num_dist_codes > 1; num_dist_codes--) 0 0 0
999 0 ▶ if (d->m_huff_code_sizes[1][num_dist_codes - 1])
Branch: 1 branch
BranchTrueFalseTotal
if (d->m_huff_code_sizes[1][num_dist_codes - 1]) 0 0 0
1000 0 break;
1001  
1002 0 memcpy(code_sizes_to_pack, &d->m_huff_code_sizes[0][0], num_lit_codes);
1003 0 memcpy(code_sizes_to_pack + num_lit_codes, &d->m_huff_code_sizes[1][0], num_dist_codes);
1004 0 total_code_sizes_to_pack = num_lit_codes + num_dist_codes;
1005 0 num_packed_code_sizes = 0;
1006 0 rle_z_count = 0;
1007 0 rle_repeat_count = 0;
1008  
1009 0 memset(&d->m_huff_count[2][0], 0, sizeof(d->m_huff_count[2][0]) * TDEFL_MAX_HUFF_SYMBOLS_2);
1010 0 ▶ for (i = 0; i < total_code_sizes_to_pack; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < total_code_sizes_to_pack; i++) 0 0 0
1011 {
1012 0 mz_uint8 code_size = code_sizes_to_pack[i];
1013 0 ▶ if (!code_size)
Branch: 1 branch
BranchTrueFalseTotal
if (!code_size) 0 0 0
1014 {
1015 0 ▶ TDEFL_RLE_PREV_CODE_SIZE();
Branch: 3 branches
BranchTrueFalseTotal
TDEFL_RLE_PREV_CODE_SIZE(); 0 0 0
TDEFL_RLE_PREV_CODE_SIZE(); 0 0 0
TDEFL_RLE_PREV_CODE_SIZE(); 0 0 0
1016 0 ▶ if (++rle_z_count == 138)
Branch: 1 branch
BranchTrueFalseTotal
if (++rle_z_count == 138) 0 0 0
1017 {
1018 0 ▶ TDEFL_RLE_ZERO_CODE_SIZE();
Branch: 4 branches
BranchTrueFalseTotal
TDEFL_RLE_ZERO_CODE_SIZE(); 0 0 0
TDEFL_RLE_ZERO_CODE_SIZE(); 0 0 0
TDEFL_RLE_ZERO_CODE_SIZE(); 0 0 0
TDEFL_RLE_ZERO_CODE_SIZE(); 0 0 0
1019 }
1020 }
1021 else
1022 {
1023 0 ▶ TDEFL_RLE_ZERO_CODE_SIZE();
Branch: 4 branches
BranchTrueFalseTotal
TDEFL_RLE_ZERO_CODE_SIZE(); 0 0 0
TDEFL_RLE_ZERO_CODE_SIZE(); 0 0 0
TDEFL_RLE_ZERO_CODE_SIZE(); 0 0 0
TDEFL_RLE_ZERO_CODE_SIZE(); 0 0 0
1024 0 ▶ if (code_size != prev_code_size)
Branch: 1 branch
BranchTrueFalseTotal
if (code_size != prev_code_size) 0 0 0
1025 {
1026 0 ▶ TDEFL_RLE_PREV_CODE_SIZE();
Branch: 3 branches
BranchTrueFalseTotal
TDEFL_RLE_PREV_CODE_SIZE(); 0 0 0
TDEFL_RLE_PREV_CODE_SIZE(); 0 0 0
TDEFL_RLE_PREV_CODE_SIZE(); 0 0 0
1027 0 d->m_huff_count[2][code_size] = (mz_uint16)(d->m_huff_count[2][code_size] + 1);
1028 0 packed_code_sizes[num_packed_code_sizes++] = code_size;
1029 }
1030 0 ▶ else if (++rle_repeat_count == 6)
Branch: 1 branch
BranchTrueFalseTotal
else if (++rle_repeat_count == 6) 0 0 0
1031 {
1032 0 ▶ TDEFL_RLE_PREV_CODE_SIZE();
Branch: 3 branches
BranchTrueFalseTotal
TDEFL_RLE_PREV_CODE_SIZE(); 0 0 0
TDEFL_RLE_PREV_CODE_SIZE(); 0 0 0
TDEFL_RLE_PREV_CODE_SIZE(); 0 0 0
1033 }
1034 }
1035 0 prev_code_size = code_size;
1036 }
1037 0 ▶ if (rle_repeat_count)
Branch: 1 branch
BranchTrueFalseTotal
if (rle_repeat_count) 0 0 0
1038 {
1039 0 ▶ TDEFL_RLE_PREV_CODE_SIZE();
Branch: 3 branches
BranchTrueFalseTotal
TDEFL_RLE_PREV_CODE_SIZE(); 0 0 0
TDEFL_RLE_PREV_CODE_SIZE(); 0 0 0
TDEFL_RLE_PREV_CODE_SIZE(); 0 0 0
1040 }
1041 else
1042 {
1043 0 ▶ TDEFL_RLE_ZERO_CODE_SIZE();
Branch: 4 branches
BranchTrueFalseTotal
TDEFL_RLE_ZERO_CODE_SIZE(); 0 0 0
TDEFL_RLE_ZERO_CODE_SIZE(); 0 0 0
TDEFL_RLE_ZERO_CODE_SIZE(); 0 0 0
TDEFL_RLE_ZERO_CODE_SIZE(); 0 0 0
1044 }
1045  
1046 0 tdefl_optimize_huffman_table(d, 2, TDEFL_MAX_HUFF_SYMBOLS_2, 7, MZ_FALSE);
1047  
1048 0 ▶ TDEFL_PUT_BITS(2, 2);
Branch: 2 branches
BranchTrueFalseTotal
TDEFL_PUT_BITS(2, 2); 0 0 0
TDEFL_PUT_BITS(2, 2); 0 0 0
1049  
1050 0 ▶ TDEFL_PUT_BITS(num_lit_codes - 257, 5);
Branch: 2 branches
BranchTrueFalseTotal
TDEFL_PUT_BITS(num_lit_codes - 257, 5); 0 0 0
TDEFL_PUT_BITS(num_lit_codes - 257, 5); 0 0 0
1051 0 ▶ TDEFL_PUT_BITS(num_dist_codes - 1, 5);
Branch: 2 branches
BranchTrueFalseTotal
TDEFL_PUT_BITS(num_dist_codes - 1, 5); 0 0 0
TDEFL_PUT_BITS(num_dist_codes - 1, 5); 0 0 0
1052  
1053 0 ▶ for (num_bit_lengths = 18; num_bit_lengths >= 0; num_bit_lengths--)
Branch: 1 branch
BranchTrueFalseTotal
for (num_bit_lengths = 18; num_bit_lengths >= 0; num_bit_lengths--) 0 0 0
1054 0 ▶ if (d->m_huff_code_sizes[2][s_tdefl_packed_code_size_syms_swizzle[num_bit_lengths]])
Branch: 1 branch
BranchTrueFalseTotal
if (d->m_huff_code_sizes[2][s_tdefl_packed_code_size_syms_swizzle[num_bit_lengths]]) 0 0 0
1055 0 break;
1056 0 num_bit_lengths = MZ_MAX(4, (num_bit_lengths + 1));
1057 0 ▶ TDEFL_PUT_BITS(num_bit_lengths - 4, 4);
Branch: 2 branches
BranchTrueFalseTotal
TDEFL_PUT_BITS(num_bit_lengths - 4, 4); 0 0 0
TDEFL_PUT_BITS(num_bit_lengths - 4, 4); 0 0 0
1058 0 ▶ for (i = 0; (int)i < num_bit_lengths; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; (int)i < num_bit_lengths; i++) 0 0 0
1059 0 ▶ TDEFL_PUT_BITS(d->m_huff_code_sizes[2][s_tdefl_packed_code_size_syms_swizzle[i]], 3);
Branch: 2 branches
BranchTrueFalseTotal
TDEFL_PUT_BITS(d->m_huff_code_sizes[2][s_tdefl_packed_code_size_syms_swizzle[i]], 3); 0 0 0
TDEFL_PUT_BITS(d->m_huff_code_sizes[2][s_tdefl_packed_code_size_syms_swizzle[i]], 3); 0 0 0
1060  
1061 0 ▶ for (packed_code_sizes_index = 0; packed_code_sizes_index < num_packed_code_sizes;)
Branch: 1 branch
BranchTrueFalseTotal
for (packed_code_sizes_index = 0; packed_code_sizes_index < num_packed_code_sizes;) 0 0 0
1062 {
1063 0 mz_uint code = packed_code_sizes[packed_code_sizes_index++];
1064 MZ_ASSERT(code < TDEFL_MAX_HUFF_SYMBOLS_2);
1065 0 ▶ TDEFL_PUT_BITS(d->m_huff_codes[2][code], d->m_huff_code_sizes[2][code]);
Branch: 2 branches
BranchTrueFalseTotal
TDEFL_PUT_BITS(d->m_huff_codes[2][code], d->m_huff_code_sizes[2][code]); 0 0 0
TDEFL_PUT_BITS(d->m_huff_codes[2][code], d->m_huff_code_sizes[2][code]); 0 0 0
1066 0 ▶ if (code >= 16)
Branch: 1 branch
BranchTrueFalseTotal
if (code >= 16) 0 0 0
1067 0 ▶ TDEFL_PUT_BITS(packed_code_sizes[packed_code_sizes_index++], "\02\03\07"[code - 16]);
Branch: 2 branches
BranchTrueFalseTotal
TDEFL_PUT_BITS(packed_code_sizes[packed_code_sizes_index++], "\02\03\07"[code - 16]); 0 0 0
TDEFL_PUT_BITS(packed_code_sizes[packed_code_sizes_index++], "\02\03\07"[code - 16]); 0 0 0
1068 }
1069 0 }
1070  
1071 0 static void tdefl_start_static_block(tdefl_compressor *d)
1072 {
1073 mz_uint i;
1074 0 mz_uint8 *p = &d->m_huff_code_sizes[0][0];
1075  
1076 0 ▶ for (i = 0; i <= 143; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= 143; ++i) 0 0 0
1077 0 *p++ = 8;
1078 0 ▶ for (; i <= 255; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (; i <= 255; ++i) 0 0 0
1079 0 *p++ = 9;
1080 0 ▶ for (; i <= 279; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (; i <= 279; ++i) 0 0 0
1081 0 *p++ = 7;
1082 0 ▶ for (; i <= 287; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (; i <= 287; ++i) 0 0 0
1083 0 *p++ = 8;
1084  
1085 0 memset(d->m_huff_code_sizes[1], 5, 32);
1086  
1087 0 tdefl_optimize_huffman_table(d, 0, 288, 15, MZ_TRUE);
1088 0 tdefl_optimize_huffman_table(d, 1, 32, 15, MZ_TRUE);
1089  
1090 0 ▶ TDEFL_PUT_BITS(1, 2);
Branch: 2 branches
BranchTrueFalseTotal
TDEFL_PUT_BITS(1, 2); 0 0 0
TDEFL_PUT_BITS(1, 2); 0 0 0
1091 0 }
1092  
1093 static const mz_uint mz_bitmasks[17] = { 0x0000, 0x0001, 0x0003, 0x0007, 0x000F, 0x001F, 0x003F, 0x007F, 0x00FF, 0x01FF, 0x03FF, 0x07FF, 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF };
1094  
1095 #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN && MINIZ_HAS_64BIT_REGISTERS
1096 static mz_bool tdefl_compress_lz_codes(tdefl_compressor *d)
1097 {
1098 mz_uint flags;
1099 mz_uint8 *pLZ_codes;
1100 mz_uint8 *pOutput_buf = d->m_pOutput_buf;
1101 mz_uint8 *pLZ_code_buf_end = d->m_pLZ_code_buf;
1102 mz_uint64 bit_buffer = d->m_bit_buffer;
1103 mz_uint bits_in = d->m_bits_in;
1104  
1105 #define TDEFL_PUT_BITS_FAST(b, l) \
1106 { \
1107 bit_buffer |= (((mz_uint64)(b)) << bits_in); \
1108 bits_in += (l); \
1109 }
1110  
1111 flags = 1;
1112 for (pLZ_codes = d->m_lz_code_buf; pLZ_codes < pLZ_code_buf_end; flags >>= 1)
1113 {
1114 if (flags == 1)
1115 flags = *pLZ_codes++ | 0x100;
1116  
1117 if (flags & 1)
1118 {
1119 mz_uint s0, s1, n0, n1, sym, num_extra_bits;
1120 mz_uint match_len = pLZ_codes[0];
1121 mz_uint match_dist = (pLZ_codes[1] | (pLZ_codes[2] << 8));
1122 pLZ_codes += 3;
1123  
1124 MZ_ASSERT(d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]);
1125 TDEFL_PUT_BITS_FAST(d->m_huff_codes[0][s_tdefl_len_sym[match_len]], d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]);
1126 TDEFL_PUT_BITS_FAST(match_len & mz_bitmasks[s_tdefl_len_extra[match_len]], s_tdefl_len_extra[match_len]);
1127  
1128 /* This sequence coaxes MSVC into using cmov's vs. jmp's. */
1129 s0 = s_tdefl_small_dist_sym[match_dist & 511];
1130 n0 = s_tdefl_small_dist_extra[match_dist & 511];
1131 s1 = s_tdefl_large_dist_sym[match_dist >> 8];
1132 n1 = s_tdefl_large_dist_extra[match_dist >> 8];
1133 sym = (match_dist < 512) ? s0 : s1;
1134 num_extra_bits = (match_dist < 512) ? n0 : n1;
1135  
1136 MZ_ASSERT(d->m_huff_code_sizes[1][sym]);
1137 TDEFL_PUT_BITS_FAST(d->m_huff_codes[1][sym], d->m_huff_code_sizes[1][sym]);
1138 TDEFL_PUT_BITS_FAST(match_dist & mz_bitmasks[num_extra_bits], num_extra_bits);
1139 }
1140 else
1141 {
1142 mz_uint lit = *pLZ_codes++;
1143 MZ_ASSERT(d->m_huff_code_sizes[0][lit]);
1144 TDEFL_PUT_BITS_FAST(d->m_huff_codes[0][lit], d->m_huff_code_sizes[0][lit]);
1145  
1146 if (((flags & 2) == 0) && (pLZ_codes < pLZ_code_buf_end))
1147 {
1148 flags >>= 1;
1149 lit = *pLZ_codes++;
1150 MZ_ASSERT(d->m_huff_code_sizes[0][lit]);
1151 TDEFL_PUT_BITS_FAST(d->m_huff_codes[0][lit], d->m_huff_code_sizes[0][lit]);
1152  
1153 if (((flags & 2) == 0) && (pLZ_codes < pLZ_code_buf_end))
1154 {
1155 flags >>= 1;
1156 lit = *pLZ_codes++;
1157 MZ_ASSERT(d->m_huff_code_sizes[0][lit]);
1158 TDEFL_PUT_BITS_FAST(d->m_huff_codes[0][lit], d->m_huff_code_sizes[0][lit]);
1159 }
1160 }
1161 }
1162  
1163 if (pOutput_buf >= d->m_pOutput_buf_end)
1164 return MZ_FALSE;
1165  
1166 memcpy(pOutput_buf, &bit_buffer, sizeof(mz_uint64));
1167 pOutput_buf += (bits_in >> 3);
1168 bit_buffer >>= (bits_in & ~7);
1169 bits_in &= 7;
1170 }
1171  
1172 #undef TDEFL_PUT_BITS_FAST
1173  
1174 d->m_pOutput_buf = pOutput_buf;
1175 d->m_bits_in = 0;
1176 d->m_bit_buffer = 0;
1177  
1178 while (bits_in)
1179 {
1180 mz_uint32 n = MZ_MIN(bits_in, 16);
1181 TDEFL_PUT_BITS((mz_uint)bit_buffer & mz_bitmasks[n], n);
1182 bit_buffer >>= n;
1183 bits_in -= n;
1184 }
1185  
1186 TDEFL_PUT_BITS(d->m_huff_codes[0][256], d->m_huff_code_sizes[0][256]);
1187  
1188 return (d->m_pOutput_buf < d->m_pOutput_buf_end);
1189 }
1190 #else
1191 0 static mz_bool tdefl_compress_lz_codes(tdefl_compressor *d)
1192 {
1193 mz_uint flags;
1194 mz_uint8 *pLZ_codes;
1195  
1196 0 flags = 1;
1197 0 ▶ for (pLZ_codes = d->m_lz_code_buf; pLZ_codes < d->m_pLZ_code_buf; flags >>= 1)
Branch: 1 branch
BranchTrueFalseTotal
for (pLZ_codes = d->m_lz_code_buf; pLZ_codes < d->m_pLZ_code_buf; flags >>= 1) 0 0 0
1198 {
1199 0 ▶ if (flags == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (flags == 1) 0 0 0
1200 0 flags = *pLZ_codes++ | 0x100;
1201 0 ▶ if (flags & 1)
Branch: 1 branch
BranchTrueFalseTotal
if (flags & 1) 0 0 0
1202 {
1203 mz_uint sym, num_extra_bits;
1204 0 mz_uint match_len = pLZ_codes[0], match_dist = (pLZ_codes[1] | (pLZ_codes[2] << 8));
1205 0 pLZ_codes += 3;
1206  
1207 MZ_ASSERT(d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]);
1208 0 ▶ TDEFL_PUT_BITS(d->m_huff_codes[0][s_tdefl_len_sym[match_len]], d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]);
Branch: 2 branches
BranchTrueFalseTotal
TDEFL_PUT_BITS(d->m_huff_codes[0][s_tdefl_len_sym[match_len]], d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]); 0 0 0
TDEFL_PUT_BITS(d->m_huff_codes[0][s_tdefl_len_sym[match_len]], d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]); 0 0 0
1209 0 ▶ TDEFL_PUT_BITS(match_len & mz_bitmasks[s_tdefl_len_extra[match_len]], s_tdefl_len_extra[match_len]);
Branch: 2 branches
BranchTrueFalseTotal
TDEFL_PUT_BITS(match_len & mz_bitmasks[s_tdefl_len_extra[match_len]], s_tdefl_len_extra[match_len]); 0 0 0
TDEFL_PUT_BITS(match_len & mz_bitmasks[s_tdefl_len_extra[match_len]], s_tdefl_len_extra[match_len]); 0 0 0
1210  
1211 0 ▶ if (match_dist < 512)
Branch: 1 branch
BranchTrueFalseTotal
if (match_dist < 512) 0 0 0
1212 {
1213 0 sym = s_tdefl_small_dist_sym[match_dist];
1214 0 num_extra_bits = s_tdefl_small_dist_extra[match_dist];
1215 }
1216 else
1217 {
1218 0 sym = s_tdefl_large_dist_sym[match_dist >> 8];
1219 0 num_extra_bits = s_tdefl_large_dist_extra[match_dist >> 8];
1220 }
1221 MZ_ASSERT(d->m_huff_code_sizes[1][sym]);
1222 0 ▶ TDEFL_PUT_BITS(d->m_huff_codes[1][sym], d->m_huff_code_sizes[1][sym]);
Branch: 2 branches
BranchTrueFalseTotal
TDEFL_PUT_BITS(d->m_huff_codes[1][sym], d->m_huff_code_sizes[1][sym]); 0 0 0
TDEFL_PUT_BITS(d->m_huff_codes[1][sym], d->m_huff_code_sizes[1][sym]); 0 0 0
1223 0 ▶ TDEFL_PUT_BITS(match_dist & mz_bitmasks[num_extra_bits], num_extra_bits);
Branch: 2 branches
BranchTrueFalseTotal
TDEFL_PUT_BITS(match_dist & mz_bitmasks[num_extra_bits], num_extra_bits); 0 0 0
TDEFL_PUT_BITS(match_dist & mz_bitmasks[num_extra_bits], num_extra_bits); 0 0 0
1224 }
1225 else
1226 {
1227 0 mz_uint lit = *pLZ_codes++;
1228 MZ_ASSERT(d->m_huff_code_sizes[0][lit]);
1229 0 ▶ TDEFL_PUT_BITS(d->m_huff_codes[0][lit], d->m_huff_code_sizes[0][lit]);
Branch: 2 branches
BranchTrueFalseTotal
TDEFL_PUT_BITS(d->m_huff_codes[0][lit], d->m_huff_code_sizes[0][lit]); 0 0 0
TDEFL_PUT_BITS(d->m_huff_codes[0][lit], d->m_huff_code_sizes[0][lit]); 0 0 0
1230 }
1231 }
1232  
1233 0 ▶ TDEFL_PUT_BITS(d->m_huff_codes[0][256], d->m_huff_code_sizes[0][256]);
Branch: 2 branches
BranchTrueFalseTotal
TDEFL_PUT_BITS(d->m_huff_codes[0][256], d->m_huff_code_sizes[0][256]); 0 0 0
TDEFL_PUT_BITS(d->m_huff_codes[0][256], d->m_huff_code_sizes[0][256]); 0 0 0
1234  
1235 0 return (d->m_pOutput_buf < d->m_pOutput_buf_end);
1236 }
1237 #endif /* MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN && MINIZ_HAS_64BIT_REGISTERS */
1238  
1239 0 static mz_bool tdefl_compress_block(tdefl_compressor *d, mz_bool static_block)
1240 {
1241 0 ▶ if (static_block)
Branch: 1 branch
BranchTrueFalseTotal
if (static_block) 0 0 0
1242 0 tdefl_start_static_block(d);
1243 else
1244 0 tdefl_start_dynamic_block(d);
1245 0 return tdefl_compress_lz_codes(d);
1246 }
1247  
1248 static const mz_uint s_tdefl_num_probes[11] = { 0, 1, 6, 32, 16, 32, 128, 256, 512, 768, 1500 };
1249  
1250 0 static int tdefl_flush_block(tdefl_compressor *d, int flush)
1251 {
1252 mz_uint saved_bit_buf, saved_bits_in;
1253 mz_uint8 *pSaved_output_buf;
1254 0 mz_bool comp_block_succeeded = MZ_FALSE;
1255 0 ▶ int n, use_raw_block = ((d->m_flags & TDEFL_FORCE_ALL_RAW_BLOCKS) != 0) && (d->m_lookahead_pos - d->m_lz_code_buf_dict_pos) <= d->m_dict_size;
Branch: 2 branches
BranchTrueFalseTotal
int n, use_raw_block = ((d->m_flags & TDEFL_FORCE_ALL_RAW_BLOCKS) != 0) && (d->m_lookahead_pos - d->m_lz_code_buf_dict_pos) <= d->m_dict_size; 0 0 0
int n, use_raw_block = ((d->m_flags & TDEFL_FORCE_ALL_RAW_BLOCKS) != 0) && (d->m_lookahead_pos - d->m_lz_code_buf_dict_pos) <= d->m_dict_size; 0 0 0
1256 0 ▶ mz_uint8 *pOutput_buf_start = ((d->m_pPut_buf_func == NULL) && ((*d->m_pOut_buf_size - d->m_out_buf_ofs) >= TDEFL_OUT_BUF_SIZE)) ? ((mz_uint8 *)d->m_pOut_buf + d->m_out_buf_ofs) : d->m_output_buf;
Branch: 2 branches
BranchTrueFalseTotal
mz_uint8 *pOutput_buf_start = ((d->m_pPut_buf_func == NULL) && ((*d->m_pOut_buf_size - d->m_out_buf_ofs) >= TDEFL_OUT_BUF_SIZE)) ? ((mz_uint8 *)d->m_pOut_buf + d->m_out_buf_ofs) : d->m_output_buf; 0 0 0
mz_uint8 *pOutput_buf_start = ((d->m_pPut_buf_func == NULL) && ((*d->m_pOut_buf_size - d->m_out_buf_ofs) >= TDEFL_OUT_BUF_SIZE)) ? ((mz_uint8 *)d->m_pOut_buf + d->m_out_buf_ofs) : d->m_output_buf; 0 0 0
1257  
1258 0 d->m_pOutput_buf = pOutput_buf_start;
1259 0 d->m_pOutput_buf_end = d->m_pOutput_buf + TDEFL_OUT_BUF_SIZE - 16;
1260  
1261 MZ_ASSERT(!d->m_output_flush_remaining);
1262 0 d->m_output_flush_ofs = 0;
1263 0 d->m_output_flush_remaining = 0;
1264  
1265 0 *d->m_pLZ_flags = (mz_uint8)(*d->m_pLZ_flags >> d->m_num_flags_left);
1266 0 d->m_pLZ_code_buf -= (d->m_num_flags_left == 8);
1267  
1268 0 ▶ if ((d->m_flags & TDEFL_WRITE_ZLIB_HEADER) && (!d->m_block_index))
Branch: 2 branches
BranchTrueFalseTotal
if ((d->m_flags & TDEFL_WRITE_ZLIB_HEADER) && (!d->m_block_index)) 0 0 0
if ((d->m_flags & TDEFL_WRITE_ZLIB_HEADER) && (!d->m_block_index)) 0 0 0
1269 {
1270 0 const mz_uint8 cmf = 0x78;
1271 0 mz_uint8 flg, flevel = 3;
1272 0 mz_uint header, i, mz_un = sizeof(s_tdefl_num_probes) / sizeof(mz_uint);
1273  
1274 /* Determine compression level by reversing the process in tdefl_create_comp_flags_from_zip_params() */
1275 0 ▶ for (i = 0; i < mz_un; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < mz_un; i++) 0 0 0
1276 0 ▶ if (s_tdefl_num_probes[i] == (d->m_flags & 0xFFF))
Branch: 1 branch
BranchTrueFalseTotal
if (s_tdefl_num_probes[i] == (d->m_flags & 0xFFF)) 0 0 0
1277 0 break;
1278  
1279 0 ▶ if (i < 2)
Branch: 1 branch
BranchTrueFalseTotal
if (i < 2) 0 0 0
1280 0 flevel = 0;
1281 0 ▶ else if (i < 6)
Branch: 1 branch
BranchTrueFalseTotal
else if (i < 6) 0 0 0
1282 0 flevel = 1;
1283 0 ▶ else if (i == 6)
Branch: 1 branch
BranchTrueFalseTotal
else if (i == 6) 0 0 0
1284 0 flevel = 2;
1285  
1286 0 header = cmf << 8 | (flevel << 6);
1287 0 header += 31 - (header % 31);
1288 0 flg = header & 0xFF;
1289  
1290 0 ▶ TDEFL_PUT_BITS(cmf, 8);
Branch: 2 branches
BranchTrueFalseTotal
TDEFL_PUT_BITS(cmf, 8); 0 0 0
TDEFL_PUT_BITS(cmf, 8); 0 0 0
1291 0 ▶ TDEFL_PUT_BITS(flg, 8);
Branch: 2 branches
BranchTrueFalseTotal
TDEFL_PUT_BITS(flg, 8); 0 0 0
TDEFL_PUT_BITS(flg, 8); 0 0 0
1292 }
1293  
1294 0 ▶ TDEFL_PUT_BITS(flush == TDEFL_FINISH, 1);
Branch: 2 branches
BranchTrueFalseTotal
TDEFL_PUT_BITS(flush == TDEFL_FINISH, 1); 0 0 0
TDEFL_PUT_BITS(flush == TDEFL_FINISH, 1); 0 0 0
1295  
1296 0 pSaved_output_buf = d->m_pOutput_buf;
1297 0 saved_bit_buf = d->m_bit_buffer;
1298 0 saved_bits_in = d->m_bits_in;
1299  
1300 0 ▶ if (!use_raw_block)
Branch: 1 branch
BranchTrueFalseTotal
if (!use_raw_block) 0 0 0
1301 0 ▶ comp_block_succeeded = tdefl_compress_block(d, (d->m_flags & TDEFL_FORCE_ALL_STATIC_BLOCKS) || (d->m_total_lz_bytes < 48));
Branch: 2 branches
BranchTrueFalseTotal
comp_block_succeeded = tdefl_compress_block(d, (d->m_flags & TDEFL_FORCE_ALL_STATIC_BLOCKS) || (d->m_total_lz_bytes < 48)); 0 0 0
comp_block_succeeded = tdefl_compress_block(d, (d->m_flags & TDEFL_FORCE_ALL_STATIC_BLOCKS) || (d->m_total_lz_bytes < 48)); 0 0 0
1302  
1303 /* If the block gets expanded, forget the current contents of the output buffer and send a raw block instead. */
1304 0 ▶ if (((use_raw_block) || ((d->m_total_lz_bytes) && ((d->m_pOutput_buf - pSaved_output_buf + 1U) >= d->m_total_lz_bytes))) &&
Branch: 3 branches
BranchTrueFalseTotal
if (((use_raw_block) || ((d->m_total_lz_bytes) && ((d->m_pOutput_buf - pSaved_output_buf + 1U) >= d->m_total_lz_bytes))) && 0 0 0
if (((use_raw_block) || ((d->m_total_lz_bytes) && ((d->m_pOutput_buf - pSaved_output_buf + 1U) >= d->m_total_lz_bytes))) && 0 0 0
if (((use_raw_block) || ((d->m_total_lz_bytes) && ((d->m_pOutput_buf - pSaved_output_buf + 1U) >= d->m_total_lz_bytes))) && 0 0 0
1305 0 ▶ ((d->m_lookahead_pos - d->m_lz_code_buf_dict_pos) <= d->m_dict_size))
Branch: 1 branch
BranchTrueFalseTotal
((d->m_lookahead_pos - d->m_lz_code_buf_dict_pos) <= d->m_dict_size)) 0 0 0
1306 0 {
1307 mz_uint i;
1308 0 d->m_pOutput_buf = pSaved_output_buf;
1309 0 d->m_bit_buffer = saved_bit_buf, d->m_bits_in = saved_bits_in;
1310 0 ▶ TDEFL_PUT_BITS(0, 2);
Branch: 2 branches
BranchTrueFalseTotal
TDEFL_PUT_BITS(0, 2); 0 0 0
TDEFL_PUT_BITS(0, 2); 0 0 0
1311 0 ▶ if (d->m_bits_in)
Branch: 1 branch
BranchTrueFalseTotal
if (d->m_bits_in) 0 0 0
1312 {
1313 0 ▶ TDEFL_PUT_BITS(0, 8 - d->m_bits_in);
Branch: 2 branches
BranchTrueFalseTotal
TDEFL_PUT_BITS(0, 8 - d->m_bits_in); 0 0 0
TDEFL_PUT_BITS(0, 8 - d->m_bits_in); 0 0 0
1314 }
1315 0 ▶ for (i = 2; i; --i, d->m_total_lz_bytes ^= 0xFFFF)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 2; i; --i, d->m_total_lz_bytes ^= 0xFFFF) 0 0 0
1316 {
1317 0 ▶ TDEFL_PUT_BITS(d->m_total_lz_bytes & 0xFFFF, 16);
Branch: 2 branches
BranchTrueFalseTotal
TDEFL_PUT_BITS(d->m_total_lz_bytes & 0xFFFF, 16); 0 0 0
TDEFL_PUT_BITS(d->m_total_lz_bytes & 0xFFFF, 16); 0 0 0
1318 }
1319 0 ▶ for (i = 0; i < d->m_total_lz_bytes; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < d->m_total_lz_bytes; ++i) 0 0 0
1320 {
1321 0 ▶ TDEFL_PUT_BITS(d->m_dict[(d->m_lz_code_buf_dict_pos + i) & TDEFL_LZ_DICT_SIZE_MASK], 8);
Branch: 2 branches
BranchTrueFalseTotal
TDEFL_PUT_BITS(d->m_dict[(d->m_lz_code_buf_dict_pos + i) & TDEFL_LZ_DICT_SIZE_MASK], 8); 0 0 0
TDEFL_PUT_BITS(d->m_dict[(d->m_lz_code_buf_dict_pos + i) & TDEFL_LZ_DICT_SIZE_MASK], 8); 0 0 0
1322 }
1323 }
1324 /* Check for the extremely unlikely (if not impossible) case of the compressed block not fitting into the output buffer when using dynamic codes. */
1325 0 ▶ else if (!comp_block_succeeded)
Branch: 1 branch
BranchTrueFalseTotal
else if (!comp_block_succeeded) 0 0 0
1326 {
1327 0 d->m_pOutput_buf = pSaved_output_buf;
1328 0 d->m_bit_buffer = saved_bit_buf, d->m_bits_in = saved_bits_in;
1329 0 tdefl_compress_block(d, MZ_TRUE);
1330 }
1331  
1332 0 ▶ if (flush)
Branch: 1 branch
BranchTrueFalseTotal
if (flush) 0 0 0
1333 {
1334 0 ▶ if (flush == TDEFL_FINISH)
Branch: 1 branch
BranchTrueFalseTotal
if (flush == TDEFL_FINISH) 0 0 0
1335 {
1336 0 ▶ if (d->m_bits_in)
Branch: 1 branch
BranchTrueFalseTotal
if (d->m_bits_in) 0 0 0
1337 {
1338 0 ▶ TDEFL_PUT_BITS(0, 8 - d->m_bits_in);
Branch: 2 branches
BranchTrueFalseTotal
TDEFL_PUT_BITS(0, 8 - d->m_bits_in); 0 0 0
TDEFL_PUT_BITS(0, 8 - d->m_bits_in); 0 0 0
1339 }
1340 0 ▶ if (d->m_flags & TDEFL_WRITE_ZLIB_HEADER)
Branch: 1 branch
BranchTrueFalseTotal
if (d->m_flags & TDEFL_WRITE_ZLIB_HEADER) 0 0 0
1341 {
1342 0 mz_uint i, a = d->m_adler32;
1343 0 ▶ for (i = 0; i < 4; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) 0 0 0
1344 {
1345 0 ▶ TDEFL_PUT_BITS((a >> 24) & 0xFF, 8);
Branch: 2 branches
BranchTrueFalseTotal
TDEFL_PUT_BITS((a >> 24) & 0xFF, 8); 0 0 0
TDEFL_PUT_BITS((a >> 24) & 0xFF, 8); 0 0 0
1346 0 a <<= 8;
1347 }
1348 }
1349 }
1350 else
1351 {
1352 0 mz_uint i, z = 0;
1353 0 ▶ TDEFL_PUT_BITS(0, 3);
Branch: 2 branches
BranchTrueFalseTotal
TDEFL_PUT_BITS(0, 3); 0 0 0
TDEFL_PUT_BITS(0, 3); 0 0 0
1354 0 ▶ if (d->m_bits_in)
Branch: 1 branch
BranchTrueFalseTotal
if (d->m_bits_in) 0 0 0
1355 {
1356 0 ▶ TDEFL_PUT_BITS(0, 8 - d->m_bits_in);
Branch: 2 branches
BranchTrueFalseTotal
TDEFL_PUT_BITS(0, 8 - d->m_bits_in); 0 0 0
TDEFL_PUT_BITS(0, 8 - d->m_bits_in); 0 0 0
1357 }
1358 0 ▶ for (i = 2; i; --i, z ^= 0xFFFF)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 2; i; --i, z ^= 0xFFFF) 0 0 0
1359 {
1360 0 ▶ TDEFL_PUT_BITS(z & 0xFFFF, 16);
Branch: 2 branches
BranchTrueFalseTotal
TDEFL_PUT_BITS(z & 0xFFFF, 16); 0 0 0
TDEFL_PUT_BITS(z & 0xFFFF, 16); 0 0 0
1361 }
1362 }
1363 }
1364  
1365 MZ_ASSERT(d->m_pOutput_buf < d->m_pOutput_buf_end);
1366  
1367 0 memset(&d->m_huff_count[0][0], 0, sizeof(d->m_huff_count[0][0]) * TDEFL_MAX_HUFF_SYMBOLS_0);
1368 0 memset(&d->m_huff_count[1][0], 0, sizeof(d->m_huff_count[1][0]) * TDEFL_MAX_HUFF_SYMBOLS_1);
1369  
1370 0 d->m_pLZ_code_buf = d->m_lz_code_buf + 1;
1371 0 d->m_pLZ_flags = d->m_lz_code_buf;
1372 0 d->m_num_flags_left = 8;
1373 0 d->m_lz_code_buf_dict_pos += d->m_total_lz_bytes;
1374 0 d->m_total_lz_bytes = 0;
1375 0 d->m_block_index++;
1376  
1377 0 ▶ if ((n = (int)(d->m_pOutput_buf - pOutput_buf_start)) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((n = (int)(d->m_pOutput_buf - pOutput_buf_start)) != 0) 0 0 0
1378 {
1379 0 ▶ if (d->m_pPut_buf_func)
Branch: 1 branch
BranchTrueFalseTotal
if (d->m_pPut_buf_func) 0 0 0
1380 {
1381 0 *d->m_pIn_buf_size = d->m_pSrc - (const mz_uint8 *)d->m_pIn_buf;
1382 0 ▶ if (!(*d->m_pPut_buf_func)(d->m_output_buf, n, d->m_pPut_buf_user))
Branch: 1 branch
BranchTrueFalseTotal
if (!(*d->m_pPut_buf_func)(d->m_output_buf, n, d->m_pPut_buf_user)) 0 0 0
1383 0 return (d->m_prev_return_status = TDEFL_STATUS_PUT_BUF_FAILED);
1384 }
1385 0 ▶ else if (pOutput_buf_start == d->m_output_buf)
Branch: 1 branch
BranchTrueFalseTotal
else if (pOutput_buf_start == d->m_output_buf) 0 0 0
1386 {
1387 0 ▶ int bytes_to_copy = (int)MZ_MIN((size_t)n, (size_t)(*d->m_pOut_buf_size - d->m_out_buf_ofs));
Branch: 1 branch
BranchTrueFalseTotal
int bytes_to_copy = (int)MZ_MIN((size_t)n, (size_t)(*d->m_pOut_buf_size - d->m_out_buf_ofs)); 0 0 0
1388 0 memcpy((mz_uint8 *)d->m_pOut_buf + d->m_out_buf_ofs, d->m_output_buf, bytes_to_copy);
1389 0 d->m_out_buf_ofs += bytes_to_copy;
1390 0 ▶ if ((n -= bytes_to_copy) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((n -= bytes_to_copy) != 0) 0 0 0
1391 {
1392 0 d->m_output_flush_ofs = bytes_to_copy;
1393 0 d->m_output_flush_remaining = n;
1394 }
1395 }
1396 else
1397 {
1398 0 d->m_out_buf_ofs += n;
1399 }
1400 }
1401  
1402 0 return d->m_output_flush_remaining;
1403 }
1404  
1405 #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES
1406 #ifdef MINIZ_UNALIGNED_USE_MEMCPY
1407 static mz_uint16 TDEFL_READ_UNALIGNED_WORD(const mz_uint8 *p)
1408 {
1409 mz_uint16 ret;
1410 memcpy(&ret, p, sizeof(mz_uint16));
1411 return ret;
1412 }
1413 static mz_uint16 TDEFL_READ_UNALIGNED_WORD2(const mz_uint16 *p)
1414 {
1415 mz_uint16 ret;
1416 memcpy(&ret, p, sizeof(mz_uint16));
1417 return ret;
1418 }
1419 #else
1420 #define TDEFL_READ_UNALIGNED_WORD(p) *(const mz_uint16 *)(p)
1421 #define TDEFL_READ_UNALIGNED_WORD2(p) *(const mz_uint16 *)(p)
1422 #endif
1423 static MZ_FORCEINLINE void tdefl_find_match(tdefl_compressor *d, mz_uint lookahead_pos, mz_uint max_dist, mz_uint max_match_len, mz_uint *pMatch_dist, mz_uint *pMatch_len)
1424 {
1425 mz_uint dist, pos = lookahead_pos & TDEFL_LZ_DICT_SIZE_MASK, match_len = *pMatch_len, probe_pos = pos, next_probe_pos, probe_len;
1426 mz_uint num_probes_left = d->m_max_probes[match_len >= 32];
1427 const mz_uint16 *s = (const mz_uint16 *)(d->m_dict + pos), *p, *q;
1428 mz_uint16 c01 = TDEFL_READ_UNALIGNED_WORD(&d->m_dict[pos + match_len - 1]), s01 = TDEFL_READ_UNALIGNED_WORD2(s);
1429 MZ_ASSERT(max_match_len <= TDEFL_MAX_MATCH_LEN);
1430 if (max_match_len <= match_len)
1431 return;
1432 for (;;)
1433 {
1434 for (;;)
1435 {
1436 if (--num_probes_left == 0)
1437 return;
1438 #define TDEFL_PROBE \
1439 next_probe_pos = d->m_next[probe_pos]; \
1440 if ((!next_probe_pos) || ((dist = (mz_uint16)(lookahead_pos - next_probe_pos)) > max_dist)) \
1441 return; \
1442 probe_pos = next_probe_pos & TDEFL_LZ_DICT_SIZE_MASK; \
1443 if (TDEFL_READ_UNALIGNED_WORD(&d->m_dict[probe_pos + match_len - 1]) == c01) \
1444 break;
1445 TDEFL_PROBE;
1446 TDEFL_PROBE;
1447 TDEFL_PROBE;
1448 }
1449 if (!dist)
1450 break;
1451 q = (const mz_uint16 *)(d->m_dict + probe_pos);
1452 if (TDEFL_READ_UNALIGNED_WORD2(q) != s01)
1453 continue;
1454 p = s;
1455 probe_len = 32;
1456 do
1457 {
1458 } while ((TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) && (TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) &&
1459 (TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) && (TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) && (--probe_len > 0));
1460 if (!probe_len)
1461 {
1462 *pMatch_dist = dist;
1463 *pMatch_len = MZ_MIN(max_match_len, (mz_uint)TDEFL_MAX_MATCH_LEN);
1464 break;
1465 }
1466 else if ((probe_len = ((mz_uint)(p - s) * 2) + (mz_uint)(*(const mz_uint8 *)p == *(const mz_uint8 *)q)) > match_len)
1467 {
1468 *pMatch_dist = dist;
1469 if ((*pMatch_len = match_len = MZ_MIN(max_match_len, probe_len)) == max_match_len)
1470 break;
1471 c01 = TDEFL_READ_UNALIGNED_WORD(&d->m_dict[pos + match_len - 1]);
1472 }
1473 }
1474 }
1475 #else
1476 static MZ_FORCEINLINE void tdefl_find_match(tdefl_compressor *d, mz_uint lookahead_pos, mz_uint max_dist, mz_uint max_match_len, mz_uint *pMatch_dist, mz_uint *pMatch_len)
1477 {
1478 0 mz_uint dist, pos = lookahead_pos & TDEFL_LZ_DICT_SIZE_MASK, match_len = *pMatch_len, probe_pos = pos, next_probe_pos, probe_len;
1479 0 mz_uint num_probes_left = d->m_max_probes[match_len >= 32];
1480 0 const mz_uint8 *s = d->m_dict + pos, *p, *q;
1481 0 mz_uint8 c0 = d->m_dict[pos + match_len], c1 = d->m_dict[pos + match_len - 1];
1482 MZ_ASSERT(max_match_len <= TDEFL_MAX_MATCH_LEN);
1483 0 if (max_match_len <= match_len)
1484 0 return;
1485 0 for (;;)
1486 {
1487 for (;;)
1488 {
1489 0 ▶ if (--num_probes_left == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (--num_probes_left == 0) 0 0 0
1490 0 return;
1491 #define TDEFL_PROBE \
1492 next_probe_pos = d->m_next[probe_pos]; \
1493 if ((!next_probe_pos) || ((dist = (mz_uint16)(lookahead_pos - next_probe_pos)) > max_dist)) \
1494 return; \
1495 probe_pos = next_probe_pos & TDEFL_LZ_DICT_SIZE_MASK; \
1496 if ((d->m_dict[probe_pos + match_len] == c0) && (d->m_dict[probe_pos + match_len - 1] == c1)) \
1497 break;
1498 0 ▶ TDEFL_PROBE;
Branch: 4 branches
BranchTrueFalseTotal
TDEFL_PROBE; 0 0 0
TDEFL_PROBE; 0 0 0
TDEFL_PROBE; 0 0 0
TDEFL_PROBE; 0 0 0
1499 0 ▶ TDEFL_PROBE;
Branch: 4 branches
BranchTrueFalseTotal
TDEFL_PROBE; 0 0 0
TDEFL_PROBE; 0 0 0
TDEFL_PROBE; 0 0 0
TDEFL_PROBE; 0 0 0
1500 0 ▶ TDEFL_PROBE;
Branch: 4 branches
BranchTrueFalseTotal
TDEFL_PROBE; 0 0 0
TDEFL_PROBE; 0 0 0
TDEFL_PROBE; 0 0 0
TDEFL_PROBE; 0 0 0
1501 }
1502 0 ▶ if (!dist)
Branch: 1 branch
BranchTrueFalseTotal
if (!dist) 0 0 0
1503 0 break;
1504 0 p = s;
1505 0 q = d->m_dict + probe_pos;
1506 0 ▶ for (probe_len = 0; probe_len < max_match_len; probe_len++)
Branch: 1 branch
BranchTrueFalseTotal
for (probe_len = 0; probe_len < max_match_len; probe_len++) 0 0 0
1507 0 ▶ if (*p++ != *q++)
Branch: 1 branch
BranchTrueFalseTotal
if (*p++ != *q++) 0 0 0
1508 0 break;
1509 0 ▶ if (probe_len > match_len)
Branch: 1 branch
BranchTrueFalseTotal
if (probe_len > match_len) 0 0 0
1510 {
1511 0 *pMatch_dist = dist;
1512 0 ▶ if ((*pMatch_len = match_len = probe_len) == max_match_len)
Branch: 1 branch
BranchTrueFalseTotal
if ((*pMatch_len = match_len = probe_len) == max_match_len) 0 0 0
1513 0 return;
1514 0 c0 = d->m_dict[pos + match_len];
1515 0 c1 = d->m_dict[pos + match_len - 1];
1516 }
1517 }
1518 }
1519 #endif /* #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES */
1520  
1521 #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN
1522 #ifdef MINIZ_UNALIGNED_USE_MEMCPY
1523 static mz_uint32 TDEFL_READ_UNALIGNED_WORD32(const mz_uint8 *p)
1524 {
1525 mz_uint32 ret;
1526 memcpy(&ret, p, sizeof(mz_uint32));
1527 return ret;
1528 }
1529 #else
1530 #define TDEFL_READ_UNALIGNED_WORD32(p) *(const mz_uint32 *)(p)
1531 #endif
1532 static mz_bool tdefl_compress_fast(tdefl_compressor *d)
1533 {
1534 /* Faster, minimally featured LZRW1-style match+parse loop with better register utilization. Intended for applications where raw throughput is valued more highly than ratio. */
1535 mz_uint lookahead_pos = d->m_lookahead_pos, lookahead_size = d->m_lookahead_size, dict_size = d->m_dict_size, total_lz_bytes = d->m_total_lz_bytes, num_flags_left = d->m_num_flags_left;
1536 mz_uint8 *pLZ_code_buf = d->m_pLZ_code_buf, *pLZ_flags = d->m_pLZ_flags;
1537 mz_uint cur_pos = lookahead_pos & TDEFL_LZ_DICT_SIZE_MASK;
1538  
1539 while ((d->m_src_buf_left) || ((d->m_flush) && (lookahead_size)))
1540 {
1541 const mz_uint TDEFL_COMP_FAST_LOOKAHEAD_SIZE = 4096;
1542 mz_uint dst_pos = (lookahead_pos + lookahead_size) & TDEFL_LZ_DICT_SIZE_MASK;
1543 mz_uint num_bytes_to_process = (mz_uint)MZ_MIN(d->m_src_buf_left, TDEFL_COMP_FAST_LOOKAHEAD_SIZE - lookahead_size);
1544 d->m_src_buf_left -= num_bytes_to_process;
1545 lookahead_size += num_bytes_to_process;
1546  
1547 while (num_bytes_to_process)
1548 {
1549 mz_uint32 n = MZ_MIN(TDEFL_LZ_DICT_SIZE - dst_pos, num_bytes_to_process);
1550 memcpy(d->m_dict + dst_pos, d->m_pSrc, n);
1551 if (dst_pos < (TDEFL_MAX_MATCH_LEN - 1))
1552 memcpy(d->m_dict + TDEFL_LZ_DICT_SIZE + dst_pos, d->m_pSrc, MZ_MIN(n, (TDEFL_MAX_MATCH_LEN - 1) - dst_pos));
1553 d->m_pSrc += n;
1554 dst_pos = (dst_pos + n) & TDEFL_LZ_DICT_SIZE_MASK;
1555 num_bytes_to_process -= n;
1556 }
1557  
1558 dict_size = MZ_MIN(TDEFL_LZ_DICT_SIZE - lookahead_size, dict_size);
1559 if ((!d->m_flush) && (lookahead_size < TDEFL_COMP_FAST_LOOKAHEAD_SIZE))
1560 break;
1561  
1562 while (lookahead_size >= 4)
1563 {
1564 mz_uint cur_match_dist, cur_match_len = 1;
1565 mz_uint8 *pCur_dict = d->m_dict + cur_pos;
1566 mz_uint first_trigram = TDEFL_READ_UNALIGNED_WORD32(pCur_dict) & 0xFFFFFF;
1567 mz_uint hash = (first_trigram ^ (first_trigram >> (24 - (TDEFL_LZ_HASH_BITS - 8)))) & TDEFL_LEVEL1_HASH_SIZE_MASK;
1568 mz_uint probe_pos = d->m_hash[hash];
1569 d->m_hash[hash] = (mz_uint16)lookahead_pos;
1570  
1571 if (((cur_match_dist = (mz_uint16)(lookahead_pos - probe_pos)) <= dict_size) && ((TDEFL_READ_UNALIGNED_WORD32(d->m_dict + (probe_pos &= TDEFL_LZ_DICT_SIZE_MASK)) & 0xFFFFFF) == first_trigram))
1572 {
1573 const mz_uint16 *p = (const mz_uint16 *)pCur_dict;
1574 const mz_uint16 *q = (const mz_uint16 *)(d->m_dict + probe_pos);
1575 mz_uint32 probe_len = 32;
1576 do
1577 {
1578 } while ((TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) && (TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) &&
1579 (TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) && (TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) && (--probe_len > 0));
1580 cur_match_len = ((mz_uint)(p - (const mz_uint16 *)pCur_dict) * 2) + (mz_uint)(*(const mz_uint8 *)p == *(const mz_uint8 *)q);
1581 if (!probe_len)
1582 cur_match_len = cur_match_dist ? TDEFL_MAX_MATCH_LEN : 0;
1583  
1584 if ((cur_match_len < TDEFL_MIN_MATCH_LEN) || ((cur_match_len == TDEFL_MIN_MATCH_LEN) && (cur_match_dist >= 8U * 1024U)))
1585 {
1586 cur_match_len = 1;
1587 *pLZ_code_buf++ = (mz_uint8)first_trigram;
1588 *pLZ_flags = (mz_uint8)(*pLZ_flags >> 1);
1589 d->m_huff_count[0][(mz_uint8)first_trigram]++;
1590 }
1591 else
1592 {
1593 mz_uint32 s0, s1;
1594 cur_match_len = MZ_MIN(cur_match_len, lookahead_size);
1595  
1596 MZ_ASSERT((cur_match_len >= TDEFL_MIN_MATCH_LEN) && (cur_match_dist >= 1) && (cur_match_dist <= TDEFL_LZ_DICT_SIZE));
1597  
1598 cur_match_dist--;
1599  
1600 pLZ_code_buf[0] = (mz_uint8)(cur_match_len - TDEFL_MIN_MATCH_LEN);
1601 #ifdef MINIZ_UNALIGNED_USE_MEMCPY
1602 memcpy(&pLZ_code_buf[1], &cur_match_dist, sizeof(cur_match_dist));
1603 #else
1604 *(mz_uint16 *)(&pLZ_code_buf[1]) = (mz_uint16)cur_match_dist;
1605 #endif
1606 pLZ_code_buf += 3;
1607 *pLZ_flags = (mz_uint8)((*pLZ_flags >> 1) | 0x80);
1608  
1609 s0 = s_tdefl_small_dist_sym[cur_match_dist & 511];
1610 s1 = s_tdefl_large_dist_sym[cur_match_dist >> 8];
1611 d->m_huff_count[1][(cur_match_dist < 512) ? s0 : s1]++;
1612  
1613 d->m_huff_count[0][s_tdefl_len_sym[cur_match_len - TDEFL_MIN_MATCH_LEN]]++;
1614 }
1615 }
1616 else
1617 {
1618 *pLZ_code_buf++ = (mz_uint8)first_trigram;
1619 *pLZ_flags = (mz_uint8)(*pLZ_flags >> 1);
1620 d->m_huff_count[0][(mz_uint8)first_trigram]++;
1621 }
1622  
1623 if (--num_flags_left == 0)
1624 {
1625 num_flags_left = 8;
1626 pLZ_flags = pLZ_code_buf++;
1627 }
1628  
1629 total_lz_bytes += cur_match_len;
1630 lookahead_pos += cur_match_len;
1631 dict_size = MZ_MIN(dict_size + cur_match_len, (mz_uint)TDEFL_LZ_DICT_SIZE);
1632 cur_pos = (cur_pos + cur_match_len) & TDEFL_LZ_DICT_SIZE_MASK;
1633 MZ_ASSERT(lookahead_size >= cur_match_len);
1634 lookahead_size -= cur_match_len;
1635  
1636 if (pLZ_code_buf > &d->m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE - 8])
1637 {
1638 int n;
1639 d->m_lookahead_pos = lookahead_pos;
1640 d->m_lookahead_size = lookahead_size;
1641 d->m_dict_size = dict_size;
1642 d->m_total_lz_bytes = total_lz_bytes;
1643 d->m_pLZ_code_buf = pLZ_code_buf;
1644 d->m_pLZ_flags = pLZ_flags;
1645 d->m_num_flags_left = num_flags_left;
1646 if ((n = tdefl_flush_block(d, 0)) != 0)
1647 return (n < 0) ? MZ_FALSE : MZ_TRUE;
1648 total_lz_bytes = d->m_total_lz_bytes;
1649 pLZ_code_buf = d->m_pLZ_code_buf;
1650 pLZ_flags = d->m_pLZ_flags;
1651 num_flags_left = d->m_num_flags_left;
1652 }
1653 }
1654  
1655 while (lookahead_size)
1656 {
1657 mz_uint8 lit = d->m_dict[cur_pos];
1658  
1659 total_lz_bytes++;
1660 *pLZ_code_buf++ = lit;
1661 *pLZ_flags = (mz_uint8)(*pLZ_flags >> 1);
1662 if (--num_flags_left == 0)
1663 {
1664 num_flags_left = 8;
1665 pLZ_flags = pLZ_code_buf++;
1666 }
1667  
1668 d->m_huff_count[0][lit]++;
1669  
1670 lookahead_pos++;
1671 dict_size = MZ_MIN(dict_size + 1, (mz_uint)TDEFL_LZ_DICT_SIZE);
1672 cur_pos = (cur_pos + 1) & TDEFL_LZ_DICT_SIZE_MASK;
1673 lookahead_size--;
1674  
1675 if (pLZ_code_buf > &d->m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE - 8])
1676 {
1677 int n;
1678 d->m_lookahead_pos = lookahead_pos;
1679 d->m_lookahead_size = lookahead_size;
1680 d->m_dict_size = dict_size;
1681 d->m_total_lz_bytes = total_lz_bytes;
1682 d->m_pLZ_code_buf = pLZ_code_buf;
1683 d->m_pLZ_flags = pLZ_flags;
1684 d->m_num_flags_left = num_flags_left;
1685 if ((n = tdefl_flush_block(d, 0)) != 0)
1686 return (n < 0) ? MZ_FALSE : MZ_TRUE;
1687 total_lz_bytes = d->m_total_lz_bytes;
1688 pLZ_code_buf = d->m_pLZ_code_buf;
1689 pLZ_flags = d->m_pLZ_flags;
1690 num_flags_left = d->m_num_flags_left;
1691 }
1692 }
1693 }
1694  
1695 d->m_lookahead_pos = lookahead_pos;
1696 d->m_lookahead_size = lookahead_size;
1697 d->m_dict_size = dict_size;
1698 d->m_total_lz_bytes = total_lz_bytes;
1699 d->m_pLZ_code_buf = pLZ_code_buf;
1700 d->m_pLZ_flags = pLZ_flags;
1701 d->m_num_flags_left = num_flags_left;
1702 return MZ_TRUE;
1703 }
1704 #endif /* MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN */
1705  
1706 static MZ_FORCEINLINE void tdefl_record_literal(tdefl_compressor *d, mz_uint8 lit)
1707 {
1708 0 d->m_total_lz_bytes++;
1709 0 *d->m_pLZ_code_buf++ = lit;
1710 0 *d->m_pLZ_flags = (mz_uint8)(*d->m_pLZ_flags >> 1);
1711 0 if (--d->m_num_flags_left == 0)
1712 {
1713 0 d->m_num_flags_left = 8;
1714 0 d->m_pLZ_flags = d->m_pLZ_code_buf++;
1715 }
1716 0 d->m_huff_count[0][lit]++;
1717 0 }
1718  
1719 static MZ_FORCEINLINE void tdefl_record_match(tdefl_compressor *d, mz_uint match_len, mz_uint match_dist)
1720 {
1721 mz_uint32 s0, s1;
1722  
1723 MZ_ASSERT((match_len >= TDEFL_MIN_MATCH_LEN) && (match_dist >= 1) && (match_dist <= TDEFL_LZ_DICT_SIZE));
1724  
1725 0 d->m_total_lz_bytes += match_len;
1726  
1727 0 d->m_pLZ_code_buf[0] = (mz_uint8)(match_len - TDEFL_MIN_MATCH_LEN);
1728  
1729 0 match_dist -= 1;
1730 0 d->m_pLZ_code_buf[1] = (mz_uint8)(match_dist & 0xFF);
1731 0 d->m_pLZ_code_buf[2] = (mz_uint8)(match_dist >> 8);
1732 0 d->m_pLZ_code_buf += 3;
1733  
1734 0 *d->m_pLZ_flags = (mz_uint8)((*d->m_pLZ_flags >> 1) | 0x80);
1735 0 if (--d->m_num_flags_left == 0)
1736 {
1737 0 d->m_num_flags_left = 8;
1738 0 d->m_pLZ_flags = d->m_pLZ_code_buf++;
1739 }
1740  
1741 0 s0 = s_tdefl_small_dist_sym[match_dist & 511];
1742 0 s1 = s_tdefl_large_dist_sym[(match_dist >> 8) & 127];
1743 0 ▶ d->m_huff_count[1][(match_dist < 512) ? s0 : s1]++;
Branch: 3 branches
BranchTrueFalseTotal
d->m_huff_count[1][(match_dist < 512) ? s0 : s1]++; 0 0 0
d->m_huff_count[1][(match_dist < 512) ? s0 : s1]++; 0 0 0
d->m_huff_count[1][(match_dist < 512) ? s0 : s1]++; 0 0 0
1744 0 d->m_huff_count[0][s_tdefl_len_sym[match_len - TDEFL_MIN_MATCH_LEN]]++;
1745 0 }
1746  
1747 0 static mz_bool tdefl_compress_normal(tdefl_compressor *d)
1748 {
1749 0 const mz_uint8 *pSrc = d->m_pSrc;
1750 0 size_t src_buf_left = d->m_src_buf_left;
1751 0 tdefl_flush flush = d->m_flush;
1752  
1753 0 ▶ while ((src_buf_left) || ((flush) && (d->m_lookahead_size)))
Branch: 3 branches
BranchTrueFalseTotal
while ((src_buf_left) || ((flush) && (d->m_lookahead_size))) 0 0 0
while ((src_buf_left) || ((flush) && (d->m_lookahead_size))) 0 0 0
while ((src_buf_left) || ((flush) && (d->m_lookahead_size))) 0 0 0
1754 {
1755 mz_uint len_to_move, cur_match_dist, cur_match_len, cur_pos;
1756 /* Update dictionary and hash chains. Keeps the lookahead size equal to TDEFL_MAX_MATCH_LEN. */
1757 0 ▶ if ((d->m_lookahead_size + d->m_dict_size) >= (TDEFL_MIN_MATCH_LEN - 1))
Branch: 1 branch
BranchTrueFalseTotal
if ((d->m_lookahead_size + d->m_dict_size) >= (TDEFL_MIN_MATCH_LEN - 1)) 0 0 0
1758 {
1759 0 mz_uint dst_pos = (d->m_lookahead_pos + d->m_lookahead_size) & TDEFL_LZ_DICT_SIZE_MASK, ins_pos = d->m_lookahead_pos + d->m_lookahead_size - 2;
1760 0 mz_uint hash = (d->m_dict[ins_pos & TDEFL_LZ_DICT_SIZE_MASK] << TDEFL_LZ_HASH_SHIFT) ^ d->m_dict[(ins_pos + 1) & TDEFL_LZ_DICT_SIZE_MASK];
1761 0 ▶ mz_uint num_bytes_to_process = (mz_uint)MZ_MIN(src_buf_left, TDEFL_MAX_MATCH_LEN - d->m_lookahead_size);
Branch: 1 branch
BranchTrueFalseTotal
mz_uint num_bytes_to_process = (mz_uint)MZ_MIN(src_buf_left, TDEFL_MAX_MATCH_LEN - d->m_lookahead_size); 0 0 0
1762 0 ▶ const mz_uint8 *pSrc_end = pSrc ? pSrc + num_bytes_to_process : NULL;
Branch: 1 branch
BranchTrueFalseTotal
const mz_uint8 *pSrc_end = pSrc ? pSrc + num_bytes_to_process : NULL; 0 0 0
1763 0 src_buf_left -= num_bytes_to_process;
1764 0 d->m_lookahead_size += num_bytes_to_process;
1765 0 ▶ while (pSrc != pSrc_end)
Branch: 1 branch
BranchTrueFalseTotal
while (pSrc != pSrc_end) 0 0 0
1766 {
1767 0 mz_uint8 c = *pSrc++;
1768 0 d->m_dict[dst_pos] = c;
1769 0 ▶ if (dst_pos < (TDEFL_MAX_MATCH_LEN - 1))
Branch: 1 branch
BranchTrueFalseTotal
if (dst_pos < (TDEFL_MAX_MATCH_LEN - 1)) 0 0 0
1770 0 d->m_dict[TDEFL_LZ_DICT_SIZE + dst_pos] = c;
1771 0 hash = ((hash << TDEFL_LZ_HASH_SHIFT) ^ c) & (TDEFL_LZ_HASH_SIZE - 1);
1772 0 d->m_next[ins_pos & TDEFL_LZ_DICT_SIZE_MASK] = d->m_hash[hash];
1773 0 d->m_hash[hash] = (mz_uint16)(ins_pos);
1774 0 dst_pos = (dst_pos + 1) & TDEFL_LZ_DICT_SIZE_MASK;
1775 0 ins_pos++;
1776 }
1777 }
1778 else
1779 {
1780 0 ▶ while ((src_buf_left) && (d->m_lookahead_size < TDEFL_MAX_MATCH_LEN))
Branch: 2 branches
BranchTrueFalseTotal
while ((src_buf_left) && (d->m_lookahead_size < TDEFL_MAX_MATCH_LEN)) 0 0 0
while ((src_buf_left) && (d->m_lookahead_size < TDEFL_MAX_MATCH_LEN)) 0 0 0
1781 {
1782 0 mz_uint8 c = *pSrc++;
1783 0 mz_uint dst_pos = (d->m_lookahead_pos + d->m_lookahead_size) & TDEFL_LZ_DICT_SIZE_MASK;
1784 0 src_buf_left--;
1785 0 d->m_dict[dst_pos] = c;
1786 0 ▶ if (dst_pos < (TDEFL_MAX_MATCH_LEN - 1))
Branch: 1 branch
BranchTrueFalseTotal
if (dst_pos < (TDEFL_MAX_MATCH_LEN - 1)) 0 0 0
1787 0 d->m_dict[TDEFL_LZ_DICT_SIZE + dst_pos] = c;
1788 0 ▶ if ((++d->m_lookahead_size + d->m_dict_size) >= TDEFL_MIN_MATCH_LEN)
Branch: 1 branch
BranchTrueFalseTotal
if ((++d->m_lookahead_size + d->m_dict_size) >= TDEFL_MIN_MATCH_LEN) 0 0 0
1789 {
1790 0 mz_uint ins_pos = d->m_lookahead_pos + (d->m_lookahead_size - 1) - 2;
1791 0 mz_uint hash = ((d->m_dict[ins_pos & TDEFL_LZ_DICT_SIZE_MASK] << (TDEFL_LZ_HASH_SHIFT * 2)) ^ (d->m_dict[(ins_pos + 1) & TDEFL_LZ_DICT_SIZE_MASK] << TDEFL_LZ_HASH_SHIFT) ^ c) & (TDEFL_LZ_HASH_SIZE - 1);
1792 0 d->m_next[ins_pos & TDEFL_LZ_DICT_SIZE_MASK] = d->m_hash[hash];
1793 0 d->m_hash[hash] = (mz_uint16)(ins_pos);
1794 }
1795 }
1796 }
1797 0 d->m_dict_size = MZ_MIN(TDEFL_LZ_DICT_SIZE - d->m_lookahead_size, d->m_dict_size);
1798 0 ▶ if ((!flush) && (d->m_lookahead_size < TDEFL_MAX_MATCH_LEN))
Branch: 2 branches
BranchTrueFalseTotal
if ((!flush) && (d->m_lookahead_size < TDEFL_MAX_MATCH_LEN)) 0 0 0
if ((!flush) && (d->m_lookahead_size < TDEFL_MAX_MATCH_LEN)) 0 0 0
1799 0 break;
1800  
1801 /* Simple lazy/greedy parsing state machine. */
1802 0 len_to_move = 1;
1803 0 cur_match_dist = 0;
1804 0 ▶ cur_match_len = d->m_saved_match_len ? d->m_saved_match_len : (TDEFL_MIN_MATCH_LEN - 1);
Branch: 1 branch
BranchTrueFalseTotal
cur_match_len = d->m_saved_match_len ? d->m_saved_match_len : (TDEFL_MIN_MATCH_LEN - 1); 0 0 0
1805 0 cur_pos = d->m_lookahead_pos & TDEFL_LZ_DICT_SIZE_MASK;
1806 0 ▶ if (d->m_flags & (TDEFL_RLE_MATCHES | TDEFL_FORCE_ALL_RAW_BLOCKS))
Branch: 1 branch
BranchTrueFalseTotal
if (d->m_flags & (TDEFL_RLE_MATCHES | TDEFL_FORCE_ALL_RAW_BLOCKS)) 0 0 0
1807 {
1808 0 ▶ if ((d->m_dict_size) && (!(d->m_flags & TDEFL_FORCE_ALL_RAW_BLOCKS)))
Branch: 2 branches
BranchTrueFalseTotal
if ((d->m_dict_size) && (!(d->m_flags & TDEFL_FORCE_ALL_RAW_BLOCKS))) 0 0 0
if ((d->m_dict_size) && (!(d->m_flags & TDEFL_FORCE_ALL_RAW_BLOCKS))) 0 0 0
1809 {
1810 0 mz_uint8 c = d->m_dict[(cur_pos - 1) & TDEFL_LZ_DICT_SIZE_MASK];
1811 0 cur_match_len = 0;
1812 0 ▶ while (cur_match_len < d->m_lookahead_size)
Branch: 1 branch
BranchTrueFalseTotal
while (cur_match_len < d->m_lookahead_size) 0 0 0
1813 {
1814 0 ▶ if (d->m_dict[cur_pos + cur_match_len] != c)
Branch: 1 branch
BranchTrueFalseTotal
if (d->m_dict[cur_pos + cur_match_len] != c) 0 0 0
1815 0 break;
1816 0 cur_match_len++;
1817 }
1818 0 ▶ if (cur_match_len < TDEFL_MIN_MATCH_LEN)
Branch: 1 branch
BranchTrueFalseTotal
if (cur_match_len < TDEFL_MIN_MATCH_LEN) 0 0 0
1819 0 cur_match_len = 0;
1820 else
1821 0 cur_match_dist = 1;
1822 }
1823 }
1824 else
1825 {
1826 0 ▶ tdefl_find_match(d, d->m_lookahead_pos, d->m_dict_size, d->m_lookahead_size, &cur_match_dist, &cur_match_len);
Branch: 1 branch
BranchTrueFalseTotal
tdefl_find_match(d, d->m_lookahead_pos, d->m_dict_size, d->m_lookahead_size, &cur_match_dist, &cur_match_len); 0 0 0
1827 }
1828 0 ▶ if (((cur_match_len == TDEFL_MIN_MATCH_LEN) && (cur_match_dist >= 8U * 1024U)) || (cur_pos == cur_match_dist) || ((d->m_flags & TDEFL_FILTER_MATCHES) && (cur_match_len <= 5)))
Branch: 5 branches
BranchTrueFalseTotal
if (((cur_match_len == TDEFL_MIN_MATCH_LEN) && (cur_match_dist >= 8U * 1024U)) || (cur_pos == cur_match_dist) || ((d->m_flags & TDEFL_FILTER_MATCHES) && (cur_match_len <= 5))) 0 0 0
if (((cur_match_len == TDEFL_MIN_MATCH_LEN) && (cur_match_dist >= 8U * 1024U)) || (cur_pos == cur_match_dist) || ((d->m_flags & TDEFL_FILTER_MATCHES) && (cur_match_len <= 5))) 0 0 0
if (((cur_match_len == TDEFL_MIN_MATCH_LEN) && (cur_match_dist >= 8U * 1024U)) || (cur_pos == cur_match_dist) || ((d->m_flags & TDEFL_FILTER_MATCHES) && (cur_match_len <= 5))) 0 0 0
if (((cur_match_len == TDEFL_MIN_MATCH_LEN) && (cur_match_dist >= 8U * 1024U)) || (cur_pos == cur_match_dist) || ((d->m_flags & TDEFL_FILTER_MATCHES) && (cur_match_len <= 5))) 0 0 0
if (((cur_match_len == TDEFL_MIN_MATCH_LEN) && (cur_match_dist >= 8U * 1024U)) || (cur_pos == cur_match_dist) || ((d->m_flags & TDEFL_FILTER_MATCHES) && (cur_match_len <= 5))) 0 0 0
1829 {
1830 0 cur_match_dist = cur_match_len = 0;
1831 }
1832 0 ▶ if (d->m_saved_match_len)
Branch: 1 branch
BranchTrueFalseTotal
if (d->m_saved_match_len) 0 0 0
1833 {
1834 0 ▶ if (cur_match_len > d->m_saved_match_len)
Branch: 1 branch
BranchTrueFalseTotal
if (cur_match_len > d->m_saved_match_len) 0 0 0
1835 {
1836 0 ▶ tdefl_record_literal(d, (mz_uint8)d->m_saved_lit);
Branch: 1 branch
BranchTrueFalseTotal
tdefl_record_literal(d, (mz_uint8)d->m_saved_lit); 0 0 0
1837 0 ▶ if (cur_match_len >= 128)
Branch: 1 branch
BranchTrueFalseTotal
if (cur_match_len >= 128) 0 0 0
1838 {
1839 0 ▶ tdefl_record_match(d, cur_match_len, cur_match_dist);
Branch: 1 branch
BranchTrueFalseTotal
tdefl_record_match(d, cur_match_len, cur_match_dist); 0 0 0
1840 0 d->m_saved_match_len = 0;
1841 0 len_to_move = cur_match_len;
1842 }
1843 else
1844 {
1845 0 d->m_saved_lit = d->m_dict[cur_pos];
1846 0 d->m_saved_match_dist = cur_match_dist;
1847 0 d->m_saved_match_len = cur_match_len;
1848 }
1849 }
1850 else
1851 {
1852 0 ▶ tdefl_record_match(d, d->m_saved_match_len, d->m_saved_match_dist);
Branch: 1 branch
BranchTrueFalseTotal
tdefl_record_match(d, d->m_saved_match_len, d->m_saved_match_dist); 0 0 0
1853 0 len_to_move = d->m_saved_match_len - 1;
1854 0 d->m_saved_match_len = 0;
1855 }
1856 }
1857 0 ▶ else if (!cur_match_dist)
Branch: 1 branch
BranchTrueFalseTotal
else if (!cur_match_dist) 0 0 0
1858 0 ▶ tdefl_record_literal(d, d->m_dict[MZ_MIN(cur_pos, sizeof(d->m_dict) - 1)]);
Branch: 1 branch
BranchTrueFalseTotal
tdefl_record_literal(d, d->m_dict[MZ_MIN(cur_pos, sizeof(d->m_dict) - 1)]); 0 0 0
1859 0 ▶ else if ((d->m_greedy_parsing) || (d->m_flags & TDEFL_RLE_MATCHES) || (cur_match_len >= 128))
Branch: 3 branches
BranchTrueFalseTotal
else if ((d->m_greedy_parsing) || (d->m_flags & TDEFL_RLE_MATCHES) || (cur_match_len >= 128)) 0 0 0
else if ((d->m_greedy_parsing) || (d->m_flags & TDEFL_RLE_MATCHES) || (cur_match_len >= 128)) 0 0 0
else if ((d->m_greedy_parsing) || (d->m_flags & TDEFL_RLE_MATCHES) || (cur_match_len >= 128)) 0 0 0
1860 {
1861 0 ▶ tdefl_record_match(d, cur_match_len, cur_match_dist);
Branch: 1 branch
BranchTrueFalseTotal
tdefl_record_match(d, cur_match_len, cur_match_dist); 0 0 0
1862 0 len_to_move = cur_match_len;
1863 }
1864 else
1865 {
1866 0 d->m_saved_lit = d->m_dict[MZ_MIN(cur_pos, sizeof(d->m_dict) - 1)];
1867 0 d->m_saved_match_dist = cur_match_dist;
1868 0 d->m_saved_match_len = cur_match_len;
1869 }
1870 /* Move the lookahead forward by len_to_move bytes. */
1871 0 d->m_lookahead_pos += len_to_move;
1872 MZ_ASSERT(d->m_lookahead_size >= len_to_move);
1873 0 d->m_lookahead_size -= len_to_move;
1874 0 d->m_dict_size = MZ_MIN(d->m_dict_size + len_to_move, (mz_uint)TDEFL_LZ_DICT_SIZE);
1875 /* Check if it's time to flush the current LZ codes to the internal output buffer. */
1876 0 ▶ if ((d->m_pLZ_code_buf > &d->m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE - 8]) ||
Branch: 1 branch
BranchTrueFalseTotal
if ((d->m_pLZ_code_buf > &d->m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE - 8]) || 0 0 0
1877 0 ▶ ((d->m_total_lz_bytes > 31 * 1024) && (((((mz_uint)(d->m_pLZ_code_buf - d->m_lz_code_buf) * 115) >> 7) >= d->m_total_lz_bytes) || (d->m_flags & TDEFL_FORCE_ALL_RAW_BLOCKS))))
Branch: 3 branches
BranchTrueFalseTotal
((d->m_total_lz_bytes > 31 * 1024) && (((((mz_uint)(d->m_pLZ_code_buf - d->m_lz_code_buf) * 115) >> 7) >= d->m_total_lz_bytes) || (d->m_flags & TDEFL_FORCE_ALL_RAW_BLOCKS)))) 0 0 0
((d->m_total_lz_bytes > 31 * 1024) && (((((mz_uint)(d->m_pLZ_code_buf - d->m_lz_code_buf) * 115) >> 7) >= d->m_total_lz_bytes) || (d->m_flags & TDEFL_FORCE_ALL_RAW_BLOCKS)))) 0 0 0
((d->m_total_lz_bytes > 31 * 1024) && (((((mz_uint)(d->m_pLZ_code_buf - d->m_lz_code_buf) * 115) >> 7) >= d->m_total_lz_bytes) || (d->m_flags & TDEFL_FORCE_ALL_RAW_BLOCKS)))) 0 0 0
1878 {
1879 int n;
1880 0 d->m_pSrc = pSrc;
1881 0 d->m_src_buf_left = src_buf_left;
1882 0 ▶ if ((n = tdefl_flush_block(d, 0)) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((n = tdefl_flush_block(d, 0)) != 0) 0 0 0
1883 0 return (n < 0) ? MZ_FALSE : MZ_TRUE;
1884 }
1885 }
1886  
1887 0 d->m_pSrc = pSrc;
1888 0 d->m_src_buf_left = src_buf_left;
1889 0 return MZ_TRUE;
1890 }
1891  
1892 0 static tdefl_status tdefl_flush_output_buffer(tdefl_compressor *d)
1893 {
1894 0 ▶ if (d->m_pIn_buf_size)
Branch: 1 branch
BranchTrueFalseTotal
if (d->m_pIn_buf_size) 0 0 0
1895 {
1896 0 *d->m_pIn_buf_size = d->m_pSrc - (const mz_uint8 *)d->m_pIn_buf;
1897 }
1898  
1899 0 ▶ if (d->m_pOut_buf_size)
Branch: 1 branch
BranchTrueFalseTotal
if (d->m_pOut_buf_size) 0 0 0
1900 {
1901 0 size_t n = MZ_MIN(*d->m_pOut_buf_size - d->m_out_buf_ofs, d->m_output_flush_remaining);
1902 0 memcpy((mz_uint8 *)d->m_pOut_buf + d->m_out_buf_ofs, d->m_output_buf + d->m_output_flush_ofs, n);
1903 0 d->m_output_flush_ofs += (mz_uint)n;
1904 0 d->m_output_flush_remaining -= (mz_uint)n;
1905 0 d->m_out_buf_ofs += n;
1906  
1907 0 *d->m_pOut_buf_size = d->m_out_buf_ofs;
1908 }
1909  
1910 0 ▶ return (d->m_finished && !d->m_output_flush_remaining) ? TDEFL_STATUS_DONE : TDEFL_STATUS_OKAY;
Branch: 2 branches
BranchTrueFalseTotal
return (d->m_finished && !d->m_output_flush_remaining) ? TDEFL_STATUS_DONE : TDEFL_STATUS_OKAY; 0 0 0
return (d->m_finished && !d->m_output_flush_remaining) ? TDEFL_STATUS_DONE : TDEFL_STATUS_OKAY; 0 0 0
1911 }
1912  
1913 0 tdefl_status tdefl_compress(tdefl_compressor *d, const void *pIn_buf, size_t *pIn_buf_size, void *pOut_buf, size_t *pOut_buf_size, tdefl_flush flush)
1914 {
1915 0 ▶ if (!d)
Branch: 1 branch
BranchTrueFalseTotal
if (!d) 0 0 0
1916 {
1917 0 ▶ if (pIn_buf_size)
Branch: 1 branch
BranchTrueFalseTotal
if (pIn_buf_size) 0 0 0
1918 0 *pIn_buf_size = 0;
1919 0 ▶ if (pOut_buf_size)
Branch: 1 branch
BranchTrueFalseTotal
if (pOut_buf_size) 0 0 0
1920 0 *pOut_buf_size = 0;
1921 0 return TDEFL_STATUS_BAD_PARAM;
1922 }
1923  
1924 0 d->m_pIn_buf = pIn_buf;
1925 0 d->m_pIn_buf_size = pIn_buf_size;
1926 0 d->m_pOut_buf = pOut_buf;
1927 0 d->m_pOut_buf_size = pOut_buf_size;
1928 0 d->m_pSrc = (const mz_uint8 *)(pIn_buf);
1929 0 ▶ d->m_src_buf_left = pIn_buf_size ? *pIn_buf_size : 0;
Branch: 1 branch
BranchTrueFalseTotal
d->m_src_buf_left = pIn_buf_size ? *pIn_buf_size : 0; 0 0 0
1930 0 d->m_out_buf_ofs = 0;
1931 0 d->m_flush = flush;
1932  
1933 0 ▶ if (((d->m_pPut_buf_func != NULL) == ((pOut_buf != NULL) || (pOut_buf_size != NULL))) || (d->m_prev_return_status != TDEFL_STATUS_OKAY) ||
Branch: 4 branches
BranchTrueFalseTotal
if (((d->m_pPut_buf_func != NULL) == ((pOut_buf != NULL) || (pOut_buf_size != NULL))) || (d->m_prev_return_status != TDEFL_STATUS_OKAY) || 0 0 0
if (((d->m_pPut_buf_func != NULL) == ((pOut_buf != NULL) || (pOut_buf_size != NULL))) || (d->m_prev_return_status != TDEFL_STATUS_OKAY) || 0 0 0
if (((d->m_pPut_buf_func != NULL) == ((pOut_buf != NULL) || (pOut_buf_size != NULL))) || (d->m_prev_return_status != TDEFL_STATUS_OKAY) || 0 0 0
if (((d->m_pPut_buf_func != NULL) == ((pOut_buf != NULL) || (pOut_buf_size != NULL))) || (d->m_prev_return_status != TDEFL_STATUS_OKAY) || 0 0 0
1934 0 ▶ (d->m_wants_to_finish && (flush != TDEFL_FINISH)) || (pIn_buf_size && *pIn_buf_size && !pIn_buf) || (pOut_buf_size && *pOut_buf_size && !pOut_buf))
Branch: 8 branches
BranchTrueFalseTotal
(d->m_wants_to_finish && (flush != TDEFL_FINISH)) || (pIn_buf_size && *pIn_buf_size && !pIn_buf) || (pOut_buf_size && *pOut_buf_size && !pOut_buf)) 0 0 0
(d->m_wants_to_finish && (flush != TDEFL_FINISH)) || (pIn_buf_size && *pIn_buf_size && !pIn_buf) || (pOut_buf_size && *pOut_buf_size && !pOut_buf)) 0 0 0
(d->m_wants_to_finish && (flush != TDEFL_FINISH)) || (pIn_buf_size && *pIn_buf_size && !pIn_buf) || (pOut_buf_size && *pOut_buf_size && !pOut_buf)) 0 0 0
(d->m_wants_to_finish && (flush != TDEFL_FINISH)) || (pIn_buf_size && *pIn_buf_size && !pIn_buf) || (pOut_buf_size && *pOut_buf_size && !pOut_buf)) 0 0 0
(d->m_wants_to_finish && (flush != TDEFL_FINISH)) || (pIn_buf_size && *pIn_buf_size && !pIn_buf) || (pOut_buf_size && *pOut_buf_size && !pOut_buf)) 0 0 0
(d->m_wants_to_finish && (flush != TDEFL_FINISH)) || (pIn_buf_size && *pIn_buf_size && !pIn_buf) || (pOut_buf_size && *pOut_buf_size && !pOut_buf)) 0 0 0
(d->m_wants_to_finish && (flush != TDEFL_FINISH)) || (pIn_buf_size && *pIn_buf_size && !pIn_buf) || (pOut_buf_size && *pOut_buf_size && !pOut_buf)) 0 0 0
(d->m_wants_to_finish && (flush != TDEFL_FINISH)) || (pIn_buf_size && *pIn_buf_size && !pIn_buf) || (pOut_buf_size && *pOut_buf_size && !pOut_buf)) 0 0 0
1935 {
1936 0 ▶ if (pIn_buf_size)
Branch: 1 branch
BranchTrueFalseTotal
if (pIn_buf_size) 0 0 0
1937 0 *pIn_buf_size = 0;
1938 0 ▶ if (pOut_buf_size)
Branch: 1 branch
BranchTrueFalseTotal
if (pOut_buf_size) 0 0 0
1939 0 *pOut_buf_size = 0;
1940 0 return (d->m_prev_return_status = TDEFL_STATUS_BAD_PARAM);
1941 }
1942 0 d->m_wants_to_finish |= (flush == TDEFL_FINISH);
1943  
1944 0 ▶ if ((d->m_output_flush_remaining) || (d->m_finished))
Branch: 2 branches
BranchTrueFalseTotal
if ((d->m_output_flush_remaining) || (d->m_finished)) 0 0 0
if ((d->m_output_flush_remaining) || (d->m_finished)) 0 0 0
1945 0 return (d->m_prev_return_status = tdefl_flush_output_buffer(d));
1946  
1947 #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN
1948 if (((d->m_flags & TDEFL_MAX_PROBES_MASK) == 1) &&
1949 ((d->m_flags & TDEFL_GREEDY_PARSING_FLAG) != 0) &&
1950 ((d->m_flags & (TDEFL_FILTER_MATCHES | TDEFL_FORCE_ALL_RAW_BLOCKS | TDEFL_RLE_MATCHES)) == 0))
1951 {
1952 if (!tdefl_compress_fast(d))
1953 return d->m_prev_return_status;
1954 }
1955 else
1956 #endif /* #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN */
1957 {
1958 0 ▶ if (!tdefl_compress_normal(d))
Branch: 1 branch
BranchTrueFalseTotal
if (!tdefl_compress_normal(d)) 0 0 0
1959 0 return d->m_prev_return_status;
1960 }
1961  
1962 0 ▶ if ((d->m_flags & (TDEFL_WRITE_ZLIB_HEADER | TDEFL_COMPUTE_ADLER32)) && (pIn_buf))
Branch: 2 branches
BranchTrueFalseTotal
if ((d->m_flags & (TDEFL_WRITE_ZLIB_HEADER | TDEFL_COMPUTE_ADLER32)) && (pIn_buf)) 0 0 0
if ((d->m_flags & (TDEFL_WRITE_ZLIB_HEADER | TDEFL_COMPUTE_ADLER32)) && (pIn_buf)) 0 0 0
1963 0 d->m_adler32 = (mz_uint32)mz_adler32(d->m_adler32, (const mz_uint8 *)pIn_buf, d->m_pSrc - (const mz_uint8 *)pIn_buf);
1964  
1965 0 ▶ if ((flush) && (!d->m_lookahead_size) && (!d->m_src_buf_left) && (!d->m_output_flush_remaining))
Branch: 4 branches
BranchTrueFalseTotal
if ((flush) && (!d->m_lookahead_size) && (!d->m_src_buf_left) && (!d->m_output_flush_remaining)) 0 0 0
if ((flush) && (!d->m_lookahead_size) && (!d->m_src_buf_left) && (!d->m_output_flush_remaining)) 0 0 0
if ((flush) && (!d->m_lookahead_size) && (!d->m_src_buf_left) && (!d->m_output_flush_remaining)) 0 0 0
if ((flush) && (!d->m_lookahead_size) && (!d->m_src_buf_left) && (!d->m_output_flush_remaining)) 0 0 0
1966 {
1967 0 ▶ if (tdefl_flush_block(d, flush) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (tdefl_flush_block(d, flush) < 0) 0 0 0
1968 0 return d->m_prev_return_status;
1969 0 d->m_finished = (flush == TDEFL_FINISH);
1970 0 ▶ if (flush == TDEFL_FULL_FLUSH)
Branch: 1 branch
BranchTrueFalseTotal
if (flush == TDEFL_FULL_FLUSH) 0 0 0
1971 {
1972 0 MZ_CLEAR_ARR(d->m_hash);
1973 0 MZ_CLEAR_ARR(d->m_next);
1974 0 d->m_dict_size = 0;
1975 }
1976 }
1977  
1978 0 return (d->m_prev_return_status = tdefl_flush_output_buffer(d));
1979 }
1980  
1981 0 tdefl_status tdefl_compress_buffer(tdefl_compressor *d, const void *pIn_buf, size_t in_buf_size, tdefl_flush flush)
1982 {
1983 MZ_ASSERT(d->m_pPut_buf_func);
1984 0 return tdefl_compress(d, pIn_buf, &in_buf_size, NULL, NULL, flush);
1985 }
1986  
1987 0 tdefl_status tdefl_init(tdefl_compressor *d, tdefl_put_buf_func_ptr pPut_buf_func, void *pPut_buf_user, int flags)
1988 {
1989 0 d->m_pPut_buf_func = pPut_buf_func;
1990 0 d->m_pPut_buf_user = pPut_buf_user;
1991 0 d->m_flags = (mz_uint)(flags);
1992 0 d->m_max_probes[0] = 1 + ((flags & 0xFFF) + 2) / 3;
1993 0 d->m_greedy_parsing = (flags & TDEFL_GREEDY_PARSING_FLAG) != 0;
1994 0 d->m_max_probes[1] = 1 + (((flags & 0xFFF) >> 2) + 2) / 3;
1995 0 ▶ if (!(flags & TDEFL_NONDETERMINISTIC_PARSING_FLAG))
Branch: 1 branch
BranchTrueFalseTotal
if (!(flags & TDEFL_NONDETERMINISTIC_PARSING_FLAG)) 0 0 0
1996 0 MZ_CLEAR_ARR(d->m_hash);
1997 0 d->m_lookahead_pos = d->m_lookahead_size = d->m_dict_size = d->m_total_lz_bytes = d->m_lz_code_buf_dict_pos = d->m_bits_in = 0;
1998 0 d->m_output_flush_ofs = d->m_output_flush_remaining = d->m_finished = d->m_block_index = d->m_bit_buffer = d->m_wants_to_finish = 0;
1999 0 d->m_pLZ_code_buf = d->m_lz_code_buf + 1;
2000 0 d->m_pLZ_flags = d->m_lz_code_buf;
2001 0 *d->m_pLZ_flags = 0;
2002 0 d->m_num_flags_left = 8;
2003 0 d->m_pOutput_buf = d->m_output_buf;
2004 0 d->m_pOutput_buf_end = d->m_output_buf;
2005 0 d->m_prev_return_status = TDEFL_STATUS_OKAY;
2006 0 d->m_saved_match_dist = d->m_saved_match_len = d->m_saved_lit = 0;
2007 0 d->m_adler32 = 1;
2008 0 d->m_pIn_buf = NULL;
2009 0 d->m_pOut_buf = NULL;
2010 0 d->m_pIn_buf_size = NULL;
2011 0 d->m_pOut_buf_size = NULL;
2012 0 d->m_flush = TDEFL_NO_FLUSH;
2013 0 d->m_pSrc = NULL;
2014 0 d->m_src_buf_left = 0;
2015 0 d->m_out_buf_ofs = 0;
2016 0 ▶ if (!(flags & TDEFL_NONDETERMINISTIC_PARSING_FLAG))
Branch: 1 branch
BranchTrueFalseTotal
if (!(flags & TDEFL_NONDETERMINISTIC_PARSING_FLAG)) 0 0 0
2017 0 MZ_CLEAR_ARR(d->m_dict);
2018 0 memset(&d->m_huff_count[0][0], 0, sizeof(d->m_huff_count[0][0]) * TDEFL_MAX_HUFF_SYMBOLS_0);
2019 0 memset(&d->m_huff_count[1][0], 0, sizeof(d->m_huff_count[1][0]) * TDEFL_MAX_HUFF_SYMBOLS_1);
2020 0 return TDEFL_STATUS_OKAY;
2021 }
2022  
2023 0 tdefl_status tdefl_get_prev_return_status(tdefl_compressor *d)
2024 {
2025 0 return d->m_prev_return_status;
2026 }
2027  
2028 0 mz_uint32 tdefl_get_adler32(tdefl_compressor *d)
2029 {
2030 0 return d->m_adler32;
2031 }
2032  
2033 0 mz_bool tdefl_compress_mem_to_output(const void *pBuf, size_t buf_len, tdefl_put_buf_func_ptr pPut_buf_func, void *pPut_buf_user, int flags)
2034 {
2035 tdefl_compressor *pComp;
2036 mz_bool succeeded;
2037 0 ▶ if (((buf_len) && (!pBuf)) || (!pPut_buf_func))
Branch: 3 branches
BranchTrueFalseTotal
if (((buf_len) && (!pBuf)) || (!pPut_buf_func)) 0 0 0
if (((buf_len) && (!pBuf)) || (!pPut_buf_func)) 0 0 0
if (((buf_len) && (!pBuf)) || (!pPut_buf_func)) 0 0 0
2038 0 return MZ_FALSE;
2039 0 pComp = (tdefl_compressor *)MZ_MALLOC(sizeof(tdefl_compressor));
2040 0 ▶ if (!pComp)
Branch: 1 branch
BranchTrueFalseTotal
if (!pComp) 0 0 0
2041 0 return MZ_FALSE;
2042 0 succeeded = (tdefl_init(pComp, pPut_buf_func, pPut_buf_user, flags) == TDEFL_STATUS_OKAY);
2043 0 ▶ succeeded = succeeded && (tdefl_compress_buffer(pComp, pBuf, buf_len, TDEFL_FINISH) == TDEFL_STATUS_DONE);
Branch: 2 branches
BranchTrueFalseTotal
succeeded = succeeded && (tdefl_compress_buffer(pComp, pBuf, buf_len, TDEFL_FINISH) == TDEFL_STATUS_DONE); 0 0 0
succeeded = succeeded && (tdefl_compress_buffer(pComp, pBuf, buf_len, TDEFL_FINISH) == TDEFL_STATUS_DONE); 0 0 0
2044 0 MZ_FREE(pComp);
2045 0 return succeeded;
2046 }
2047  
2048 typedef struct
2049 {
2050 size_t m_size, m_capacity;
2051 mz_uint8 *m_pBuf;
2052 mz_bool m_expandable;
2053 } tdefl_output_buffer;
2054  
2055 0 static mz_bool tdefl_output_buffer_putter(const void *pBuf, int len, void *pUser)
2056 {
2057 0 tdefl_output_buffer *p = (tdefl_output_buffer *)pUser;
2058 0 size_t new_size = p->m_size + len;
2059 0 ▶ if (new_size > p->m_capacity)
Branch: 1 branch
BranchTrueFalseTotal
if (new_size > p->m_capacity) 0 0 0
2060 {
2061 0 size_t new_capacity = p->m_capacity;
2062 mz_uint8 *pNew_buf;
2063 0 ▶ if (!p->m_expandable)
Branch: 1 branch
BranchTrueFalseTotal
if (!p->m_expandable) 0 0 0
2064 0 return MZ_FALSE;
2065 do
2066 {
2067 0 new_capacity = MZ_MAX(128U, new_capacity << 1U);
2068 0 ▶ } while (new_size > new_capacity);
Branch: 1 branch
BranchTrueFalseTotal
} while (new_size > new_capacity); 0 0 0
2069 0 pNew_buf = (mz_uint8 *)MZ_REALLOC(p->m_pBuf, new_capacity);
2070 0 ▶ if (!pNew_buf)
Branch: 1 branch
BranchTrueFalseTotal
if (!pNew_buf) 0 0 0
2071 0 return MZ_FALSE;
2072 0 p->m_pBuf = pNew_buf;
2073 0 p->m_capacity = new_capacity;
2074 }
2075 0 memcpy((mz_uint8 *)p->m_pBuf + p->m_size, pBuf, len);
2076 0 p->m_size = new_size;
2077 0 return MZ_TRUE;
2078 }
2079  
2080 0 void *tdefl_compress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, size_t *pOut_len, int flags)
2081 {
2082 tdefl_output_buffer out_buf;
2083 0 MZ_CLEAR_OBJ(out_buf);
2084 0 ▶ if (!pOut_len)
Branch: 1 branch
BranchTrueFalseTotal
if (!pOut_len) 0 0 0
2085 0 return MZ_FALSE;
2086 else
2087 0 *pOut_len = 0;
2088 0 out_buf.m_expandable = MZ_TRUE;
2089 0 ▶ if (!tdefl_compress_mem_to_output(pSrc_buf, src_buf_len, tdefl_output_buffer_putter, &out_buf, flags))
Branch: 1 branch
BranchTrueFalseTotal
if (!tdefl_compress_mem_to_output(pSrc_buf, src_buf_len, tdefl_output_buffer_putter, &out_buf, flags)) 0 0 0
2090 0 return NULL;
2091 0 *pOut_len = out_buf.m_size;
2092 0 return out_buf.m_pBuf;
2093 }
2094  
2095 0 size_t tdefl_compress_mem_to_mem(void *pOut_buf, size_t out_buf_len, const void *pSrc_buf, size_t src_buf_len, int flags)
2096 {
2097 tdefl_output_buffer out_buf;
2098 0 MZ_CLEAR_OBJ(out_buf);
2099 0 ▶ if (!pOut_buf)
Branch: 1 branch
BranchTrueFalseTotal
if (!pOut_buf) 0 0 0
2100 0 return 0;
2101 0 out_buf.m_pBuf = (mz_uint8 *)pOut_buf;
2102 0 out_buf.m_capacity = out_buf_len;
2103 0 ▶ if (!tdefl_compress_mem_to_output(pSrc_buf, src_buf_len, tdefl_output_buffer_putter, &out_buf, flags))
Branch: 1 branch
BranchTrueFalseTotal
if (!tdefl_compress_mem_to_output(pSrc_buf, src_buf_len, tdefl_output_buffer_putter, &out_buf, flags)) 0 0 0
2104 0 return 0;
2105 0 return out_buf.m_size;
2106 }
2107  
2108 /* level may actually range from [0,10] (10 is a "hidden" max level, where we want a bit more compression and it's fine if throughput to fall off a cliff on some files). */
2109 0 mz_uint tdefl_create_comp_flags_from_zip_params(int level, int window_bits, int strategy)
2110 {
2111 0 ▶ mz_uint comp_flags = s_tdefl_num_probes[(level >= 0) ? MZ_MIN(10, level) : MZ_DEFAULT_LEVEL] | ((level <= 3) ? TDEFL_GREEDY_PARSING_FLAG : 0);
Branch: 2 branches
BranchTrueFalseTotal
mz_uint comp_flags = s_tdefl_num_probes[(level >= 0) ? MZ_MIN(10, level) : MZ_DEFAULT_LEVEL] | ((level <= 3) ? TDEFL_GREEDY_PARSING_FLAG : 0); 0 0 0
mz_uint comp_flags = s_tdefl_num_probes[(level >= 0) ? MZ_MIN(10, level) : MZ_DEFAULT_LEVEL] | ((level <= 3) ? TDEFL_GREEDY_PARSING_FLAG : 0); 0 0 0
2112 0 ▶ if (window_bits > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (window_bits > 0) 0 0 0
2113 0 comp_flags |= TDEFL_WRITE_ZLIB_HEADER;
2114  
2115 0 ▶ if (!level)
Branch: 1 branch
BranchTrueFalseTotal
if (!level) 0 0 0
2116 0 comp_flags |= TDEFL_FORCE_ALL_RAW_BLOCKS;
2117 0 ▶ else if (strategy == MZ_FILTERED)
Branch: 1 branch
BranchTrueFalseTotal
else if (strategy == MZ_FILTERED) 0 0 0
2118 0 comp_flags |= TDEFL_FILTER_MATCHES;
2119 0 ▶ else if (strategy == MZ_HUFFMAN_ONLY)
Branch: 1 branch
BranchTrueFalseTotal
else if (strategy == MZ_HUFFMAN_ONLY) 0 0 0
2120 0 comp_flags &= ~TDEFL_MAX_PROBES_MASK;
2121 0 ▶ else if (strategy == MZ_FIXED)
Branch: 1 branch
BranchTrueFalseTotal
else if (strategy == MZ_FIXED) 0 0 0
2122 0 comp_flags |= TDEFL_FORCE_ALL_STATIC_BLOCKS;
2123 0 ▶ else if (strategy == MZ_RLE)
Branch: 1 branch
BranchTrueFalseTotal
else if (strategy == MZ_RLE) 0 0 0
2124 0 comp_flags |= TDEFL_RLE_MATCHES;
2125  
2126 0 return comp_flags;
2127 }
2128  
2129 #ifdef _MSC_VER
2130 #pragma warning(push)
2131 #pragma warning(disable : 4204) /* nonstandard extension used : non-constant aggregate initializer (also supported by GNU C and C99, so no big deal) */
2132 #endif
2133  
2134 /* Simple PNG writer function by Alex Evans, 2011. Released into the public domain: https://gist.github.com/908299, more context at
2135 http://altdevblogaday.org/2011/04/06/a-smaller-jpg-encoder/.
2136 This is actually a modification of Alex's original code so PNG files generated by this function pass pngcheck. */
2137 0 void *tdefl_write_image_to_png_file_in_memory_ex(const void *pImage, int w, int h, int num_chans, size_t *pLen_out, mz_uint level, mz_bool flip)
2138 {
2139 /* Using a local copy of this array here in case MINIZ_NO_ZLIB_APIS was defined. */
2140 static const mz_uint s_tdefl_png_num_probes[11] = { 0, 1, 6, 32, 16, 32, 128, 256, 512, 768, 1500 };
2141 0 tdefl_compressor *pComp = (tdefl_compressor *)MZ_MALLOC(sizeof(tdefl_compressor));
2142 tdefl_output_buffer out_buf;
2143 0 int i, bpl = w * num_chans, y, z;
2144 mz_uint32 c;
2145 0 *pLen_out = 0;
2146 0 ▶ if (!pComp)
Branch: 1 branch
BranchTrueFalseTotal
if (!pComp) 0 0 0
2147 0 return NULL;
2148 0 ▶ if (w <= 0 || h <= 0 || w > 0xFFFF || h > 0xFFFF || num_chans < 1 || num_chans > 4)
Branch: 6 branches
BranchTrueFalseTotal
if (w <= 0 || h <= 0 || w > 0xFFFF || h > 0xFFFF || num_chans < 1 || num_chans > 4) 0 0 0
if (w <= 0 || h <= 0 || w > 0xFFFF || h > 0xFFFF || num_chans < 1 || num_chans > 4) 0 0 0
if (w <= 0 || h <= 0 || w > 0xFFFF || h > 0xFFFF || num_chans < 1 || num_chans > 4) 0 0 0
if (w <= 0 || h <= 0 || w > 0xFFFF || h > 0xFFFF || num_chans < 1 || num_chans > 4) 0 0 0
if (w <= 0 || h <= 0 || w > 0xFFFF || h > 0xFFFF || num_chans < 1 || num_chans > 4) 0 0 0
if (w <= 0 || h <= 0 || w > 0xFFFF || h > 0xFFFF || num_chans < 1 || num_chans > 4) 0 0 0
2149 {
2150 0 MZ_FREE(pComp);
2151 0 return NULL;
2152 }
2153 0 MZ_CLEAR_OBJ(out_buf);
2154 0 out_buf.m_expandable = MZ_TRUE;
2155 0 out_buf.m_capacity = 57 + MZ_MAX(64, (1 + bpl) * h);
2156 0 ▶ if (NULL == (out_buf.m_pBuf = (mz_uint8 *)MZ_MALLOC(out_buf.m_capacity)))
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == (out_buf.m_pBuf = (mz_uint8 *)MZ_MALLOC(out_buf.m_capacity))) 0 0 0
2157 {
2158 0 MZ_FREE(pComp);
2159 0 return NULL;
2160 }
2161 /* write dummy header */
2162 0 ▶ for (z = 41; z; --z)
Branch: 1 branch
BranchTrueFalseTotal
for (z = 41; z; --z) 0 0 0
2163 0 tdefl_output_buffer_putter(&z, 1, &out_buf);
2164 /* compress image data */
2165 0 tdefl_init(pComp, tdefl_output_buffer_putter, &out_buf, s_tdefl_png_num_probes[MZ_MIN(10, level)] | TDEFL_WRITE_ZLIB_HEADER);
2166 0 ▶ for (y = 0; y < h; ++y)
Branch: 1 branch
BranchTrueFalseTotal
for (y = 0; y < h; ++y) 0 0 0
2167 {
2168 0 tdefl_compress_buffer(pComp, &z, 1, TDEFL_NO_FLUSH);
2169 0 ▶ tdefl_compress_buffer(pComp, (mz_uint8 *)pImage + (flip ? (h - 1 - y) : y) * bpl, bpl, TDEFL_NO_FLUSH);
Branch: 1 branch
BranchTrueFalseTotal
tdefl_compress_buffer(pComp, (mz_uint8 *)pImage + (flip ? (h - 1 - y) : y) * bpl, bpl, TDEFL_NO_FLUSH); 0 0 0
2170 }
2171 0 ▶ if (tdefl_compress_buffer(pComp, NULL, 0, TDEFL_FINISH) != TDEFL_STATUS_DONE)
Branch: 1 branch
BranchTrueFalseTotal
if (tdefl_compress_buffer(pComp, NULL, 0, TDEFL_FINISH) != TDEFL_STATUS_DONE) 0 0 0
2172 {
2173 0 MZ_FREE(pComp);
2174 0 MZ_FREE(out_buf.m_pBuf);
2175 0 return NULL;
2176 }
2177 /* write real header */
2178 0 *pLen_out = out_buf.m_size - 41;
2179 {
2180 static const mz_uint8 chans[] = { 0x00, 0x00, 0x04, 0x02, 0x06 };
2181 0 mz_uint8 pnghdr[41] = { 0x89, 0x50, 0x4e, 0x47, 0x0d,
2182 0x0a, 0x1a, 0x0a, 0x00, 0x00,
2183 0x00, 0x0d, 0x49, 0x48, 0x44,
2184 0x52, 0x00, 0x00, 0x00, 0x00,
2185 0x00, 0x00, 0x00, 0x00, 0x08,
2186 0x00, 0x00, 0x00, 0x00, 0x00,
2187 0x00, 0x00, 0x00, 0x00, 0x00,
2188 0x00, 0x00, 0x49, 0x44, 0x41,
2189 0x54 };
2190 0 pnghdr[18] = (mz_uint8)(w >> 8);
2191 0 pnghdr[19] = (mz_uint8)w;
2192 0 pnghdr[22] = (mz_uint8)(h >> 8);
2193 0 pnghdr[23] = (mz_uint8)h;
2194 0 pnghdr[25] = chans[num_chans];
2195 0 pnghdr[33] = (mz_uint8)(*pLen_out >> 24);
2196 0 pnghdr[34] = (mz_uint8)(*pLen_out >> 16);
2197 0 pnghdr[35] = (mz_uint8)(*pLen_out >> 8);
2198 0 pnghdr[36] = (mz_uint8)*pLen_out;
2199 0 c = (mz_uint32)mz_crc32(MZ_CRC32_INIT, pnghdr + 12, 17);
2200 0 ▶ for (i = 0; i < 4; ++i, c <<= 8)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; ++i, c <<= 8) 0 0 0
2201 0 ((mz_uint8 *)(pnghdr + 29))[i] = (mz_uint8)(c >> 24);
2202 0 memcpy(out_buf.m_pBuf, pnghdr, 41);
2203 }
2204 /* write footer (IDAT CRC-32, followed by IEND chunk) */
2205 0 ▶ if (!tdefl_output_buffer_putter("\0\0\0\0\0\0\0\0\x49\x45\x4e\x44\xae\x42\x60\x82", 16, &out_buf))
Branch: 1 branch
BranchTrueFalseTotal
if (!tdefl_output_buffer_putter("\0\0\0\0\0\0\0\0\x49\x45\x4e\x44\xae\x42\x60\x82", 16, &out_buf)) 0 0 0
2206 {
2207 0 *pLen_out = 0;
2208 0 MZ_FREE(pComp);
2209 0 MZ_FREE(out_buf.m_pBuf);
2210 0 return NULL;
2211 }
2212 0 c = (mz_uint32)mz_crc32(MZ_CRC32_INIT, out_buf.m_pBuf + 41 - 4, *pLen_out + 4);
2213 0 ▶ for (i = 0; i < 4; ++i, c <<= 8)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; ++i, c <<= 8) 0 0 0
2214 0 (out_buf.m_pBuf + out_buf.m_size - 16)[i] = (mz_uint8)(c >> 24);
2215 /* compute final size of file, grab compressed data buffer and return */
2216 0 *pLen_out += 57;
2217 0 MZ_FREE(pComp);
2218 0 return out_buf.m_pBuf;
2219 }
2220 0 void *tdefl_write_image_to_png_file_in_memory(const void *pImage, int w, int h, int num_chans, size_t *pLen_out)
2221 {
2222 /* Level 6 corresponds to TDEFL_DEFAULT_MAX_PROBES or MZ_DEFAULT_LEVEL (but we can't depend on MZ_DEFAULT_LEVEL being available in case the zlib API's where #defined out) */
2223 0 return tdefl_write_image_to_png_file_in_memory_ex(pImage, w, h, num_chans, pLen_out, 6, MZ_FALSE);
2224 }
2225  
2226 #ifndef MINIZ_NO_MALLOC
2227 /* Allocate the tdefl_compressor and tinfl_decompressor structures in C so that */
2228 /* non-C language bindings to tdefL_ and tinfl_ API don't need to worry about */
2229 /* structure size and allocation mechanism. */
2230 0 tdefl_compressor *tdefl_compressor_alloc(void)
2231 {
2232 0 return (tdefl_compressor *)MZ_MALLOC(sizeof(tdefl_compressor));
2233 }
2234  
2235 0 void tdefl_compressor_free(tdefl_compressor *pComp)
2236 {
2237 0 MZ_FREE(pComp);
2238 0 }
2239 #endif
2240  
2241 #ifdef _MSC_VER
2242 #pragma warning(pop)
2243 #endif
2244  
2245 #ifdef __cplusplus
2246 }
2247 #endif
2248  
2249 #endif /*#ifndef MINIZ_NO_DEFLATE_APIS*/
2250 /**************************************************************************
2251 *
2252 * Copyright 2013-2014 RAD Game Tools and Valve Software
2253 * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
2254 * All Rights Reserved.
2255 *
2256 * Permission is hereby granted, free of charge, to any person obtaining a copy
2257 * of this software and associated documentation files (the "Software"), to deal
2258 * in the Software without restriction, including without limitation the rights
2259 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
2260 * copies of the Software, and to permit persons to whom the Software is
2261 * furnished to do so, subject to the following conditions:
2262 *
2263 * The above copyright notice and this permission notice shall be included in
2264 * all copies or substantial portions of the Software.
2265 *
2266 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2267 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2268 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
2269 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2270 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2271 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2272 * THE SOFTWARE.
2273 *
2274 **************************************************************************/
2275  
2276  
2277  
2278 #ifndef MINIZ_NO_INFLATE_APIS
2279  
2280 #ifdef __cplusplus
2281 extern "C"
2282 {
2283 #endif
2284  
2285 /* ------------------- Low-level Decompression (completely independent from all compression API's) */
2286  
2287 #define TINFL_MEMCPY(d, s, l) memcpy(d, s, l)
2288 #define TINFL_MEMSET(p, c, l) memset(p, c, l)
2289  
2290 #define TINFL_CR_BEGIN \
2291 switch (r->m_state) \
2292 { \
2293 case 0:
2294 #define TINFL_CR_RETURN(state_index, result) \
2295 do \
2296 { \
2297 status = result; \
2298 r->m_state = state_index; \
2299 goto common_exit; \
2300 case state_index:; \
2301 } \
2302 MZ_MACRO_END
2303 #define TINFL_CR_RETURN_FOREVER(state_index, result) \
2304 do \
2305 { \
2306 for (;;) \
2307 { \
2308 TINFL_CR_RETURN(state_index, result); \
2309 } \
2310 } \
2311 MZ_MACRO_END
2312 #define TINFL_CR_FINISH }
2313  
2314 #define TINFL_GET_BYTE(state_index, c) \
2315 do \
2316 { \
2317 while (pIn_buf_cur >= pIn_buf_end) \
2318 { \
2319 TINFL_CR_RETURN(state_index, (decomp_flags & TINFL_FLAG_HAS_MORE_INPUT) ? TINFL_STATUS_NEEDS_MORE_INPUT : TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS); \
2320 } \
2321 c = *pIn_buf_cur++; \
2322 } \
2323 MZ_MACRO_END
2324  
2325 #define TINFL_NEED_BITS(state_index, n) \
2326 do \
2327 { \
2328 mz_uint c; \
2329 TINFL_GET_BYTE(state_index, c); \
2330 bit_buf |= (((tinfl_bit_buf_t)c) << num_bits); \
2331 num_bits += 8; \
2332 } while (num_bits < (mz_uint)(n))
2333 #define TINFL_SKIP_BITS(state_index, n) \
2334 do \
2335 { \
2336 if (num_bits < (mz_uint)(n)) \
2337 { \
2338 TINFL_NEED_BITS(state_index, n); \
2339 } \
2340 bit_buf >>= (n); \
2341 num_bits -= (n); \
2342 } \
2343 MZ_MACRO_END
2344 #define TINFL_GET_BITS(state_index, b, n) \
2345 do \
2346 { \
2347 if (num_bits < (mz_uint)(n)) \
2348 { \
2349 TINFL_NEED_BITS(state_index, n); \
2350 } \
2351 b = bit_buf & ((1 << (n)) - 1); \
2352 bit_buf >>= (n); \
2353 num_bits -= (n); \
2354 } \
2355 MZ_MACRO_END
2356  
2357 /* TINFL_HUFF_BITBUF_FILL() is only used rarely, when the number of bytes remaining in the input buffer falls below 2. */
2358 /* It reads just enough bytes from the input stream that are needed to decode the next Huffman code (and absolutely no more). It works by trying to fully decode a */
2359 /* Huffman code by using whatever bits are currently present in the bit buffer. If this fails, it reads another byte, and tries again until it succeeds or until the */
2360 /* bit buffer contains >=15 bits (deflate's max. Huffman code size). */
2361 #define TINFL_HUFF_BITBUF_FILL(state_index, pLookUp, pTree) \
2362 do \
2363 { \
2364 temp = pLookUp[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]; \
2365 if (temp >= 0) \
2366 { \
2367 code_len = temp >> 9; \
2368 if ((code_len) && (num_bits >= code_len)) \
2369 break; \
2370 } \
2371 else if (num_bits > TINFL_FAST_LOOKUP_BITS) \
2372 { \
2373 code_len = TINFL_FAST_LOOKUP_BITS; \
2374 do \
2375 { \
2376 temp = pTree[~temp + ((bit_buf >> code_len++) & 1)]; \
2377 } while ((temp < 0) && (num_bits >= (code_len + 1))); \
2378 if (temp >= 0) \
2379 break; \
2380 } \
2381 TINFL_GET_BYTE(state_index, c); \
2382 bit_buf |= (((tinfl_bit_buf_t)c) << num_bits); \
2383 num_bits += 8; \
2384 } while (num_bits < 15);
2385  
2386 /* TINFL_HUFF_DECODE() decodes the next Huffman coded symbol. It's more complex than you would initially expect because the zlib API expects the decompressor to never read */
2387 /* beyond the final byte of the deflate stream. (In other words, when this macro wants to read another byte from the input, it REALLY needs another byte in order to fully */
2388 /* decode the next Huffman code.) Handling this properly is particularly important on raw deflate (non-zlib) streams, which aren't followed by a byte aligned adler-32. */
2389 /* The slow path is only executed at the very end of the input buffer. */
2390 /* v1.16: The original macro handled the case at the very end of the passed-in input buffer, but we also need to handle the case where the user passes in 1+zillion bytes */
2391 /* following the deflate data and our non-conservative read-ahead path won't kick in here on this code. This is much trickier. */
2392 #define TINFL_HUFF_DECODE(state_index, sym, pLookUp, pTree) \
2393 do \
2394 { \
2395 int temp; \
2396 mz_uint code_len, c; \
2397 if (num_bits < 15) \
2398 { \
2399 if ((pIn_buf_end - pIn_buf_cur) < 2) \
2400 { \
2401 TINFL_HUFF_BITBUF_FILL(state_index, pLookUp, pTree); \
2402 } \
2403 else \
2404 { \
2405 bit_buf |= (((tinfl_bit_buf_t)pIn_buf_cur[0]) << num_bits) | (((tinfl_bit_buf_t)pIn_buf_cur[1]) << (num_bits + 8)); \
2406 pIn_buf_cur += 2; \
2407 num_bits += 16; \
2408 } \
2409 } \
2410 if ((temp = pLookUp[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0) \
2411 code_len = temp >> 9, temp &= 511; \
2412 else \
2413 { \
2414 code_len = TINFL_FAST_LOOKUP_BITS; \
2415 do \
2416 { \
2417 temp = pTree[~temp + ((bit_buf >> code_len++) & 1)]; \
2418 } while (temp < 0); \
2419 } \
2420 sym = temp; \
2421 bit_buf >>= code_len; \
2422 num_bits -= code_len; \
2423 } \
2424 MZ_MACRO_END
2425  
2426 0 static void tinfl_clear_tree(tinfl_decompressor *r)
2427 {
2428 0 ▶ if (r->m_type == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (r->m_type == 0) 0 0 0
2429 0 MZ_CLEAR_ARR(r->m_tree_0);
2430 0 ▶ else if (r->m_type == 1)
Branch: 1 branch
BranchTrueFalseTotal
else if (r->m_type == 1) 0 0 0
2431 0 MZ_CLEAR_ARR(r->m_tree_1);
2432 else
2433 0 MZ_CLEAR_ARR(r->m_tree_2);
2434 0 }
2435  
2436 0 tinfl_status tinfl_decompress(tinfl_decompressor *r, const mz_uint8 *pIn_buf_next, size_t *pIn_buf_size, mz_uint8 *pOut_buf_start, mz_uint8 *pOut_buf_next, size_t *pOut_buf_size, const mz_uint32 decomp_flags)
2437 {
2438 static const mz_uint16 s_length_base[31] = { 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0 };
2439 static const mz_uint8 s_length_extra[31] = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 0, 0 };
2440 static const mz_uint16 s_dist_base[32] = { 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577, 0, 0 };
2441 static const mz_uint8 s_dist_extra[32] = { 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13 };
2442 static const mz_uint8 s_length_dezigzag[19] = { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 };
2443 static const mz_uint16 s_min_table_sizes[3] = { 257, 1, 4 };
2444  
2445 mz_int16 *pTrees[3];
2446 mz_uint8 *pCode_sizes[3];
2447  
2448 0 tinfl_status status = TINFL_STATUS_FAILED;
2449 mz_uint32 num_bits, dist, counter, num_extra;
2450 tinfl_bit_buf_t bit_buf;
2451 0 const mz_uint8 *pIn_buf_cur = pIn_buf_next, *const pIn_buf_end = pIn_buf_next + *pIn_buf_size;
2452 0 ▶ mz_uint8 *pOut_buf_cur = pOut_buf_next, *const pOut_buf_end = pOut_buf_next ? pOut_buf_next + *pOut_buf_size : NULL;
Branch: 1 branch
BranchTrueFalseTotal
mz_uint8 *pOut_buf_cur = pOut_buf_next, *const pOut_buf_end = pOut_buf_next ? pOut_buf_next + *pOut_buf_size : NULL; 0 0 0
2453 0 ▶ size_t out_buf_size_mask = (decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF) ? (size_t)-1 : ((pOut_buf_next - pOut_buf_start) + *pOut_buf_size) - 1, dist_from_out_buf_start;
Branch: 1 branch
BranchTrueFalseTotal
size_t out_buf_size_mask = (decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF) ? (size_t)-1 : ((pOut_buf_next - pOut_buf_start) + *pOut_buf_size) - 1, dist_from_out_buf_start; 0 0 0
2454  
2455 /* Ensure the output buffer's size is a power of 2, unless the output buffer is large enough to hold the entire output file (in which case it doesn't matter). */
2456 0 ▶ if (((out_buf_size_mask + 1) & out_buf_size_mask) || (pOut_buf_next < pOut_buf_start))
Branch: 2 branches
BranchTrueFalseTotal
if (((out_buf_size_mask + 1) & out_buf_size_mask) || (pOut_buf_next < pOut_buf_start)) 0 0 0
if (((out_buf_size_mask + 1) & out_buf_size_mask) || (pOut_buf_next < pOut_buf_start)) 0 0 0
2457 {
2458 0 *pIn_buf_size = *pOut_buf_size = 0;
2459 0 return TINFL_STATUS_BAD_PARAM;
2460 }
2461  
2462 0 pTrees[0] = r->m_tree_0;
2463 0 pTrees[1] = r->m_tree_1;
2464 0 pTrees[2] = r->m_tree_2;
2465 0 pCode_sizes[0] = r->m_code_size_0;
2466 0 pCode_sizes[1] = r->m_code_size_1;
2467 0 pCode_sizes[2] = r->m_code_size_2;
2468  
2469 0 num_bits = r->m_num_bits;
2470 0 bit_buf = r->m_bit_buf;
2471 0 dist = r->m_dist;
2472 0 counter = r->m_counter;
2473 0 num_extra = r->m_num_extra;
2474 0 dist_from_out_buf_start = r->m_dist_from_out_buf_start;
2475 0 TINFL_CR_BEGIN
2476  
2477 0 bit_buf = num_bits = dist = counter = num_extra = r->m_zhdr0 = r->m_zhdr1 = 0;
2478 0 r->m_z_adler32 = r->m_check_adler32 = 1;
2479 0 ▶ if (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER)
Branch: 1 branch
BranchTrueFalseTotal
if (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER) 0 0 0
2480 {
2481 0 ▶ TINFL_GET_BYTE(1, r->m_zhdr0);
Branch: 2 branches
BranchTrueFalseTotal
TINFL_GET_BYTE(1, r->m_zhdr0); 0 0 0
TINFL_GET_BYTE(1, r->m_zhdr0); 0 0 0
2482 0 ▶ TINFL_GET_BYTE(2, r->m_zhdr1);
Branch: 2 branches
BranchTrueFalseTotal
TINFL_GET_BYTE(2, r->m_zhdr1); 0 0 0
TINFL_GET_BYTE(2, r->m_zhdr1); 0 0 0
2483 0 ▶ counter = (((r->m_zhdr0 * 256 + r->m_zhdr1) % 31 != 0) || (r->m_zhdr1 & 32) || ((r->m_zhdr0 & 15) != 8));
Branch: 3 branches
BranchTrueFalseTotal
counter = (((r->m_zhdr0 * 256 + r->m_zhdr1) % 31 != 0) || (r->m_zhdr1 & 32) || ((r->m_zhdr0 & 15) != 8)); 0 0 0
counter = (((r->m_zhdr0 * 256 + r->m_zhdr1) % 31 != 0) || (r->m_zhdr1 & 32) || ((r->m_zhdr0 & 15) != 8)); 0 0 0
counter = (((r->m_zhdr0 * 256 + r->m_zhdr1) % 31 != 0) || (r->m_zhdr1 & 32) || ((r->m_zhdr0 & 15) != 8)); 0 0 0
2484 0 ▶ if (!(decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF))
Branch: 1 branch
BranchTrueFalseTotal
if (!(decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF)) 0 0 0
2485 0 ▶ counter |= (((1U << (8U + (r->m_zhdr0 >> 4))) > 32768U) || ((out_buf_size_mask + 1) < (size_t)((size_t)1 << (8U + (r->m_zhdr0 >> 4)))));
Branch: 2 branches
BranchTrueFalseTotal
counter |= (((1U << (8U + (r->m_zhdr0 >> 4))) > 32768U) || ((out_buf_size_mask + 1) < (size_t)((size_t)1 << (8U + (r->m_zhdr0 >> 4))))); 0 0 0
counter |= (((1U << (8U + (r->m_zhdr0 >> 4))) > 32768U) || ((out_buf_size_mask + 1) < (size_t)((size_t)1 << (8U + (r->m_zhdr0 >> 4))))); 0 0 0
2486 0 ▶ if (counter)
Branch: 1 branch
BranchTrueFalseTotal
if (counter) 0 0 0
2487 {
2488 0 TINFL_CR_RETURN_FOREVER(36, TINFL_STATUS_FAILED);
2489 }
2490 }
2491  
2492 do
2493 {
2494 0 ▶ TINFL_GET_BITS(3, r->m_final, 3);
Branch: 4 branches
BranchTrueFalseTotal
TINFL_GET_BITS(3, r->m_final, 3); 0 0 0
TINFL_GET_BITS(3, r->m_final, 3); 0 0 0
TINFL_GET_BITS(3, r->m_final, 3); 0 0 0
TINFL_GET_BITS(3, r->m_final, 3); 0 0 0
2495 0 r->m_type = r->m_final >> 1;
2496 0 ▶ if (r->m_type == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (r->m_type == 0) 0 0 0
2497 {
2498 0 ▶ TINFL_SKIP_BITS(5, num_bits & 7);
Branch: 4 branches
BranchTrueFalseTotal
TINFL_SKIP_BITS(5, num_bits & 7); 0 0 0
TINFL_SKIP_BITS(5, num_bits & 7); 0 0 0
TINFL_SKIP_BITS(5, num_bits & 7); 0 0 0
TINFL_SKIP_BITS(5, num_bits & 7); 0 0 0
2499 0 ▶ for (counter = 0; counter < 4; ++counter)
Branch: 1 branch
BranchTrueFalseTotal
for (counter = 0; counter < 4; ++counter) 0 0 0
2500 {
2501 0 ▶ if (num_bits)
Branch: 1 branch
BranchTrueFalseTotal
if (num_bits) 0 0 0
2502 0 ▶ TINFL_GET_BITS(6, r->m_raw_header[counter], 8);
Branch: 4 branches
BranchTrueFalseTotal
TINFL_GET_BITS(6, r->m_raw_header[counter], 8); 0 0 0
TINFL_GET_BITS(6, r->m_raw_header[counter], 8); 0 0 0
TINFL_GET_BITS(6, r->m_raw_header[counter], 8); 0 0 0
TINFL_GET_BITS(6, r->m_raw_header[counter], 8); 0 0 0
2503 else
2504 0 ▶ TINFL_GET_BYTE(7, r->m_raw_header[counter]);
Branch: 2 branches
BranchTrueFalseTotal
TINFL_GET_BYTE(7, r->m_raw_header[counter]); 0 0 0
TINFL_GET_BYTE(7, r->m_raw_header[counter]); 0 0 0
2505 }
2506 0 ▶ if ((counter = (r->m_raw_header[0] | (r->m_raw_header[1] << 8))) != (mz_uint)(0xFFFF ^ (r->m_raw_header[2] | (r->m_raw_header[3] << 8))))
Branch: 1 branch
BranchTrueFalseTotal
if ((counter = (r->m_raw_header[0] | (r->m_raw_header[1] << 8))) != (mz_uint)(0xFFFF ^ (r->m_raw_header[2] | (r->m_raw_header[3] << 8)))) 0 0 0
2507 {
2508 0 TINFL_CR_RETURN_FOREVER(39, TINFL_STATUS_FAILED);
2509 }
2510 0 ▶ while ((counter) && (num_bits))
Branch: 2 branches
BranchTrueFalseTotal
while ((counter) && (num_bits)) 0 0 0
while ((counter) && (num_bits)) 0 0 0
2511 {
2512 0 ▶ TINFL_GET_BITS(51, dist, 8);
Branch: 4 branches
BranchTrueFalseTotal
TINFL_GET_BITS(51, dist, 8); 0 0 0
TINFL_GET_BITS(51, dist, 8); 0 0 0
TINFL_GET_BITS(51, dist, 8); 0 0 0
TINFL_GET_BITS(51, dist, 8); 0 0 0
2513 0 while (pOut_buf_cur >= pOut_buf_end)
2514 {
2515 0 ▶ TINFL_CR_RETURN(52, TINFL_STATUS_HAS_MORE_OUTPUT);
Branch: 1 branch
BranchTrueFalseTotal
TINFL_CR_RETURN(52, TINFL_STATUS_HAS_MORE_OUTPUT); 0 0 0
2516 }
2517 0 *pOut_buf_cur++ = (mz_uint8)dist;
2518 0 counter--;
2519 }
2520 0 ▶ while (counter)
Branch: 1 branch
BranchTrueFalseTotal
while (counter) 0 0 0
2521 {
2522 size_t n;
2523 0 while (pOut_buf_cur >= pOut_buf_end)
2524 {
2525 0 ▶ TINFL_CR_RETURN(9, TINFL_STATUS_HAS_MORE_OUTPUT);
Branch: 1 branch
BranchTrueFalseTotal
TINFL_CR_RETURN(9, TINFL_STATUS_HAS_MORE_OUTPUT); 0 0 0
2526 }
2527 0 while (pIn_buf_cur >= pIn_buf_end)
2528 {
2529 0 ▶ TINFL_CR_RETURN(38, (decomp_flags & TINFL_FLAG_HAS_MORE_INPUT) ? TINFL_STATUS_NEEDS_MORE_INPUT : TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS);
Branch: 2 branches
BranchTrueFalseTotal
TINFL_CR_RETURN(38, (decomp_flags & TINFL_FLAG_HAS_MORE_INPUT) ? TINFL_STATUS_NEEDS_MORE_INPUT : TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS); 0 0 0
TINFL_CR_RETURN(38, (decomp_flags & TINFL_FLAG_HAS_MORE_INPUT) ? TINFL_STATUS_NEEDS_MORE_INPUT : TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS); 0 0 0
2530 }
2531 0 n = MZ_MIN(MZ_MIN((size_t)(pOut_buf_end - pOut_buf_cur), (size_t)(pIn_buf_end - pIn_buf_cur)), counter);
2532 0 TINFL_MEMCPY(pOut_buf_cur, pIn_buf_cur, n);
2533 0 pIn_buf_cur += n;
2534 0 pOut_buf_cur += n;
2535 0 counter -= (mz_uint)n;
2536 }
2537 }
2538 0 ▶ else if (r->m_type == 3)
Branch: 1 branch
BranchTrueFalseTotal
else if (r->m_type == 3) 0 0 0
2539 {
2540 0 TINFL_CR_RETURN_FOREVER(10, TINFL_STATUS_FAILED);
2541 }
2542 else
2543 {
2544 0 ▶ if (r->m_type == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (r->m_type == 1) 0 0 0
2545 {
2546 0 mz_uint8 *p = r->m_code_size_0;
2547 mz_uint i;
2548 0 r->m_table_sizes[0] = 288;
2549 0 r->m_table_sizes[1] = 32;
2550 0 TINFL_MEMSET(r->m_code_size_1, 5, 32);
2551 0 ▶ for (i = 0; i <= 143; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= 143; ++i) 0 0 0
2552 0 *p++ = 8;
2553 0 ▶ for (; i <= 255; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (; i <= 255; ++i) 0 0 0
2554 0 *p++ = 9;
2555 0 ▶ for (; i <= 279; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (; i <= 279; ++i) 0 0 0
2556 0 *p++ = 7;
2557 0 ▶ for (; i <= 287; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (; i <= 287; ++i) 0 0 0
2558 0 *p++ = 8;
2559 }
2560 else
2561 {
2562 0 ▶ for (counter = 0; counter < 3; counter++)
Branch: 1 branch
BranchTrueFalseTotal
for (counter = 0; counter < 3; counter++) 0 0 0
2563 {
2564 0 ▶ TINFL_GET_BITS(11, r->m_table_sizes[counter], "\05\05\04"[counter]);
Branch: 4 branches
BranchTrueFalseTotal
TINFL_GET_BITS(11, r->m_table_sizes[counter], "\05\05\04"[counter]); 0 0 0
TINFL_GET_BITS(11, r->m_table_sizes[counter], "\05\05\04"[counter]); 0 0 0
TINFL_GET_BITS(11, r->m_table_sizes[counter], "\05\05\04"[counter]); 0 0 0
TINFL_GET_BITS(11, r->m_table_sizes[counter], "\05\05\04"[counter]); 0 0 0
2565 0 r->m_table_sizes[counter] += s_min_table_sizes[counter];
2566 }
2567 0 MZ_CLEAR_ARR(r->m_code_size_2);
2568 0 ▶ for (counter = 0; counter < r->m_table_sizes[2]; counter++)
Branch: 1 branch
BranchTrueFalseTotal
for (counter = 0; counter < r->m_table_sizes[2]; counter++) 0 0 0
2569 {
2570 mz_uint s;
2571 0 ▶ TINFL_GET_BITS(14, s, 3);
Branch: 4 branches
BranchTrueFalseTotal
TINFL_GET_BITS(14, s, 3); 0 0 0
TINFL_GET_BITS(14, s, 3); 0 0 0
TINFL_GET_BITS(14, s, 3); 0 0 0
TINFL_GET_BITS(14, s, 3); 0 0 0
2572 0 r->m_code_size_2[s_length_dezigzag[counter]] = (mz_uint8)s;
2573 }
2574 0 r->m_table_sizes[2] = 19;
2575 }
2576 0 ▶ for (; (int)r->m_type >= 0; r->m_type--)
Branch: 1 branch
BranchTrueFalseTotal
for (; (int)r->m_type >= 0; r->m_type--) 0 0 0
2577 {
2578 int tree_next, tree_cur;
2579 mz_int16 *pLookUp;
2580 mz_int16 *pTree;
2581 mz_uint8 *pCode_size;
2582 mz_uint i, j, used_syms, total, sym_index, next_code[17], total_syms[16];
2583 0 pLookUp = r->m_look_up[r->m_type];
2584 0 pTree = pTrees[r->m_type];
2585 0 pCode_size = pCode_sizes[r->m_type];
2586 0 MZ_CLEAR_ARR(total_syms);
2587 0 TINFL_MEMSET(pLookUp, 0, sizeof(r->m_look_up[0]));
2588 0 tinfl_clear_tree(r);
2589 0 ▶ for (i = 0; i < r->m_table_sizes[r->m_type]; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < r->m_table_sizes[r->m_type]; ++i) 0 0 0
2590 0 total_syms[pCode_size[i]]++;
2591 0 used_syms = 0, total = 0;
2592 0 next_code[0] = next_code[1] = 0;
2593 0 ▶ for (i = 1; i <= 15; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i <= 15; ++i) 0 0 0
2594 {
2595 0 used_syms += total_syms[i];
2596 0 next_code[i + 1] = (total = ((total + total_syms[i]) << 1));
2597 }
2598 0 ▶ if ((65536 != total) && (used_syms > 1))
Branch: 2 branches
BranchTrueFalseTotal
if ((65536 != total) && (used_syms > 1)) 0 0 0
if ((65536 != total) && (used_syms > 1)) 0 0 0
2599 {
2600 0 TINFL_CR_RETURN_FOREVER(35, TINFL_STATUS_FAILED);
2601 }
2602 0 ▶ for (tree_next = -1, sym_index = 0; sym_index < r->m_table_sizes[r->m_type]; ++sym_index)
Branch: 1 branch
BranchTrueFalseTotal
for (tree_next = -1, sym_index = 0; sym_index < r->m_table_sizes[r->m_type]; ++sym_index) 0 0 0
2603 {
2604 0 mz_uint rev_code = 0, l, cur_code, code_size = pCode_size[sym_index];
2605 0 ▶ if (!code_size)
Branch: 1 branch
BranchTrueFalseTotal
if (!code_size) 0 0 0
2606 0 continue;
2607 0 cur_code = next_code[code_size]++;
2608 0 ▶ for (l = code_size; l > 0; l--, cur_code >>= 1)
Branch: 1 branch
BranchTrueFalseTotal
for (l = code_size; l > 0; l--, cur_code >>= 1) 0 0 0
2609 0 rev_code = (rev_code << 1) | (cur_code & 1);
2610 0 ▶ if (code_size <= TINFL_FAST_LOOKUP_BITS)
Branch: 1 branch
BranchTrueFalseTotal
if (code_size <= TINFL_FAST_LOOKUP_BITS) 0 0 0
2611 0 {
2612 0 mz_int16 k = (mz_int16)((code_size << 9) | sym_index);
2613 0 ▶ while (rev_code < TINFL_FAST_LOOKUP_SIZE)
Branch: 1 branch
BranchTrueFalseTotal
while (rev_code < TINFL_FAST_LOOKUP_SIZE) 0 0 0
2614 {
2615 0 pLookUp[rev_code] = k;
2616 0 rev_code += (1 << code_size);
2617 }
2618 0 continue;
2619 }
2620 0 ▶ if (0 == (tree_cur = pLookUp[rev_code & (TINFL_FAST_LOOKUP_SIZE - 1)]))
Branch: 1 branch
BranchTrueFalseTotal
if (0 == (tree_cur = pLookUp[rev_code & (TINFL_FAST_LOOKUP_SIZE - 1)])) 0 0 0
2621 {
2622 0 pLookUp[rev_code & (TINFL_FAST_LOOKUP_SIZE - 1)] = (mz_int16)tree_next;
2623 0 tree_cur = tree_next;
2624 0 tree_next -= 2;
2625 }
2626 0 rev_code >>= (TINFL_FAST_LOOKUP_BITS - 1);
2627 0 ▶ for (j = code_size; j > (TINFL_FAST_LOOKUP_BITS + 1); j--)
Branch: 1 branch
BranchTrueFalseTotal
for (j = code_size; j > (TINFL_FAST_LOOKUP_BITS + 1); j--) 0 0 0
2628 {
2629 0 tree_cur -= ((rev_code >>= 1) & 1);
2630 0 ▶ if (!pTree[-tree_cur - 1])
Branch: 1 branch
BranchTrueFalseTotal
if (!pTree[-tree_cur - 1]) 0 0 0
2631 {
2632 0 pTree[-tree_cur - 1] = (mz_int16)tree_next;
2633 0 tree_cur = tree_next;
2634 0 tree_next -= 2;
2635 }
2636 else
2637 0 tree_cur = pTree[-tree_cur - 1];
2638 }
2639 0 tree_cur -= ((rev_code >>= 1) & 1);
2640 0 pTree[-tree_cur - 1] = (mz_int16)sym_index;
2641 }
2642 0 ▶ if (r->m_type == 2)
Branch: 1 branch
BranchTrueFalseTotal
if (r->m_type == 2) 0 0 0
2643 {
2644 0 ▶ for (counter = 0; counter < (r->m_table_sizes[0] + r->m_table_sizes[1]);)
Branch: 1 branch
BranchTrueFalseTotal
for (counter = 0; counter < (r->m_table_sizes[0] + r->m_table_sizes[1]);) 0 0 0
2645 {
2646 mz_uint s;
2647 0 ▶ TINFL_HUFF_DECODE(16, dist, r->m_look_up[2], r->m_tree_2);
Branch: 14 branches
BranchTrueFalseTotal
TINFL_HUFF_DECODE(16, dist, r->m_look_up[2], r->m_tree_2); 0 0 0
TINFL_HUFF_DECODE(16, dist, r->m_look_up[2], r->m_tree_2); 0 0 0
TINFL_HUFF_DECODE(16, dist, r->m_look_up[2], r->m_tree_2); 0 0 0
TINFL_HUFF_DECODE(16, dist, r->m_look_up[2], r->m_tree_2); 0 0 0
TINFL_HUFF_DECODE(16, dist, r->m_look_up[2], r->m_tree_2); 0 0 0
TINFL_HUFF_DECODE(16, dist, r->m_look_up[2], r->m_tree_2); 0 0 0
TINFL_HUFF_DECODE(16, dist, r->m_look_up[2], r->m_tree_2); 0 0 0
TINFL_HUFF_DECODE(16, dist, r->m_look_up[2], r->m_tree_2); 0 0 0
TINFL_HUFF_DECODE(16, dist, r->m_look_up[2], r->m_tree_2); 0 0 0
TINFL_HUFF_DECODE(16, dist, r->m_look_up[2], r->m_tree_2); 0 0 0
TINFL_HUFF_DECODE(16, dist, r->m_look_up[2], r->m_tree_2); 0 0 0
TINFL_HUFF_DECODE(16, dist, r->m_look_up[2], r->m_tree_2); 0 0 0
TINFL_HUFF_DECODE(16, dist, r->m_look_up[2], r->m_tree_2); 0 0 0
TINFL_HUFF_DECODE(16, dist, r->m_look_up[2], r->m_tree_2); 0 0 0
2648 0 ▶ if (dist < 16)
Branch: 1 branch
BranchTrueFalseTotal
if (dist < 16) 0 0 0
2649 {
2650 0 r->m_len_codes[counter++] = (mz_uint8)dist;
2651 0 continue;
2652 }
2653 0 ▶ if ((dist == 16) && (!counter))
Branch: 2 branches
BranchTrueFalseTotal
if ((dist == 16) && (!counter)) 0 0 0
if ((dist == 16) && (!counter)) 0 0 0
2654 {
2655 0 TINFL_CR_RETURN_FOREVER(17, TINFL_STATUS_FAILED);
2656 }
2657 0 num_extra = "\02\03\07"[dist - 16];
2658 0 ▶ TINFL_GET_BITS(18, s, num_extra);
Branch: 4 branches
BranchTrueFalseTotal
TINFL_GET_BITS(18, s, num_extra); 0 0 0
TINFL_GET_BITS(18, s, num_extra); 0 0 0
TINFL_GET_BITS(18, s, num_extra); 0 0 0
TINFL_GET_BITS(18, s, num_extra); 0 0 0
2659 0 s += "\03\03\013"[dist - 16];
2660 0 ▶ TINFL_MEMSET(r->m_len_codes + counter, (dist == 16) ? r->m_len_codes[counter - 1] : 0, s);
Branch: 1 branch
BranchTrueFalseTotal
TINFL_MEMSET(r->m_len_codes + counter, (dist == 16) ? r->m_len_codes[counter - 1] : 0, s); 0 0 0
2661 0 counter += s;
2662 }
2663 0 ▶ if ((r->m_table_sizes[0] + r->m_table_sizes[1]) != counter)
Branch: 1 branch
BranchTrueFalseTotal
if ((r->m_table_sizes[0] + r->m_table_sizes[1]) != counter) 0 0 0
2664 {
2665 0 TINFL_CR_RETURN_FOREVER(21, TINFL_STATUS_FAILED);
2666 }
2667 0 TINFL_MEMCPY(r->m_code_size_0, r->m_len_codes, r->m_table_sizes[0]);
2668 0 TINFL_MEMCPY(r->m_code_size_1, r->m_len_codes + r->m_table_sizes[0], r->m_table_sizes[1]);
2669 }
2670 }
2671 for (;;)
2672 0 {
2673 mz_uint8 *pSrc;
2674 for (;;)
2675 {
2676 0 ▶ if (((pIn_buf_end - pIn_buf_cur) < 4) || ((pOut_buf_end - pOut_buf_cur) < 2))
Branch: 2 branches
BranchTrueFalseTotal
if (((pIn_buf_end - pIn_buf_cur) < 4) || ((pOut_buf_end - pOut_buf_cur) < 2)) 0 0 0
if (((pIn_buf_end - pIn_buf_cur) < 4) || ((pOut_buf_end - pOut_buf_cur) < 2)) 0 0 0
2677 {
2678 0 ▶ TINFL_HUFF_DECODE(23, counter, r->m_look_up[0], r->m_tree_0);
Branch: 14 branches
BranchTrueFalseTotal
TINFL_HUFF_DECODE(23, counter, r->m_look_up[0], r->m_tree_0); 0 0 0
TINFL_HUFF_DECODE(23, counter, r->m_look_up[0], r->m_tree_0); 0 0 0
TINFL_HUFF_DECODE(23, counter, r->m_look_up[0], r->m_tree_0); 0 0 0
TINFL_HUFF_DECODE(23, counter, r->m_look_up[0], r->m_tree_0); 0 0 0
TINFL_HUFF_DECODE(23, counter, r->m_look_up[0], r->m_tree_0); 0 0 0
TINFL_HUFF_DECODE(23, counter, r->m_look_up[0], r->m_tree_0); 0 0 0
TINFL_HUFF_DECODE(23, counter, r->m_look_up[0], r->m_tree_0); 0 0 0
TINFL_HUFF_DECODE(23, counter, r->m_look_up[0], r->m_tree_0); 0 0 0
TINFL_HUFF_DECODE(23, counter, r->m_look_up[0], r->m_tree_0); 0 0 0
TINFL_HUFF_DECODE(23, counter, r->m_look_up[0], r->m_tree_0); 0 0 0
TINFL_HUFF_DECODE(23, counter, r->m_look_up[0], r->m_tree_0); 0 0 0
TINFL_HUFF_DECODE(23, counter, r->m_look_up[0], r->m_tree_0); 0 0 0
TINFL_HUFF_DECODE(23, counter, r->m_look_up[0], r->m_tree_0); 0 0 0
TINFL_HUFF_DECODE(23, counter, r->m_look_up[0], r->m_tree_0); 0 0 0
2679 0 ▶ if (counter >= 256)
Branch: 1 branch
BranchTrueFalseTotal
if (counter >= 256) 0 0 0
2680 0 break;
2681 0 while (pOut_buf_cur >= pOut_buf_end)
2682 {
2683 0 ▶ TINFL_CR_RETURN(24, TINFL_STATUS_HAS_MORE_OUTPUT);
Branch: 1 branch
BranchTrueFalseTotal
TINFL_CR_RETURN(24, TINFL_STATUS_HAS_MORE_OUTPUT); 0 0 0
2684 }
2685 0 *pOut_buf_cur++ = (mz_uint8)counter;
2686 }
2687 else
2688 {
2689 int sym2;
2690 mz_uint code_len;
2691 #if TINFL_USE_64BIT_BITBUF
2692 0 ▶ if (num_bits < 30)
Branch: 1 branch
BranchTrueFalseTotal
if (num_bits < 30) 0 0 0
2693 {
2694 0 bit_buf |= (((tinfl_bit_buf_t)MZ_READ_LE32(pIn_buf_cur)) << num_bits);
2695 0 pIn_buf_cur += 4;
2696 0 num_bits += 32;
2697 }
2698 #else
2699 if (num_bits < 15)
2700 {
2701 bit_buf |= (((tinfl_bit_buf_t)MZ_READ_LE16(pIn_buf_cur)) << num_bits);
2702 pIn_buf_cur += 2;
2703 num_bits += 16;
2704 }
2705 #endif
2706 0 ▶ if ((sym2 = r->m_look_up[0][bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((sym2 = r->m_look_up[0][bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0) 0 0 0
2707 0 code_len = sym2 >> 9;
2708 else
2709 {
2710 0 code_len = TINFL_FAST_LOOKUP_BITS;
2711 do
2712 {
2713 0 sym2 = r->m_tree_0[~sym2 + ((bit_buf >> code_len++) & 1)];
2714 0 ▶ } while (sym2 < 0);
Branch: 1 branch
BranchTrueFalseTotal
} while (sym2 < 0); 0 0 0
2715 }
2716 0 counter = sym2;
2717 0 bit_buf >>= code_len;
2718 0 num_bits -= code_len;
2719 0 ▶ if (code_len == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (code_len == 0) 0 0 0
2720 {
2721 0 TINFL_CR_RETURN_FOREVER(40, TINFL_STATUS_FAILED);
2722 }
2723 0 ▶ if (counter & 256)
Branch: 1 branch
BranchTrueFalseTotal
if (counter & 256) 0 0 0
2724 0 break;
2725  
2726 #if !TINFL_USE_64BIT_BITBUF
2727 if (num_bits < 15)
2728 {
2729 bit_buf |= (((tinfl_bit_buf_t)MZ_READ_LE16(pIn_buf_cur)) << num_bits);
2730 pIn_buf_cur += 2;
2731 num_bits += 16;
2732 }
2733 #endif
2734 0 ▶ if ((sym2 = r->m_look_up[0][bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((sym2 = r->m_look_up[0][bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0) 0 0 0
2735 0 code_len = sym2 >> 9;
2736 else
2737 {
2738 0 code_len = TINFL_FAST_LOOKUP_BITS;
2739 do
2740 {
2741 0 sym2 = r->m_tree_0[~sym2 + ((bit_buf >> code_len++) & 1)];
2742 0 ▶ } while (sym2 < 0);
Branch: 1 branch
BranchTrueFalseTotal
} while (sym2 < 0); 0 0 0
2743 }
2744 0 bit_buf >>= code_len;
2745 0 num_bits -= code_len;
2746 0 ▶ if (code_len == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (code_len == 0) 0 0 0
2747 {
2748 0 TINFL_CR_RETURN_FOREVER(54, TINFL_STATUS_FAILED);
2749 }
2750  
2751 0 pOut_buf_cur[0] = (mz_uint8)counter;
2752 0 ▶ if (sym2 & 256)
Branch: 1 branch
BranchTrueFalseTotal
if (sym2 & 256) 0 0 0
2753 {
2754 0 pOut_buf_cur++;
2755 0 counter = sym2;
2756 0 break;
2757 }
2758 0 pOut_buf_cur[1] = (mz_uint8)sym2;
2759 0 pOut_buf_cur += 2;
2760 }
2761 }
2762 0 ▶ if ((counter &= 511) == 256)
Branch: 1 branch
BranchTrueFalseTotal
if ((counter &= 511) == 256) 0 0 0
2763 0 break;
2764  
2765 0 num_extra = s_length_extra[counter - 257];
2766 0 counter = s_length_base[counter - 257];
2767 0 ▶ if (num_extra)
Branch: 1 branch
BranchTrueFalseTotal
if (num_extra) 0 0 0
2768 {
2769 mz_uint extra_bits;
2770 0 ▶ TINFL_GET_BITS(25, extra_bits, num_extra);
Branch: 4 branches
BranchTrueFalseTotal
TINFL_GET_BITS(25, extra_bits, num_extra); 0 0 0
TINFL_GET_BITS(25, extra_bits, num_extra); 0 0 0
TINFL_GET_BITS(25, extra_bits, num_extra); 0 0 0
TINFL_GET_BITS(25, extra_bits, num_extra); 0 0 0
2771 0 counter += extra_bits;
2772 }
2773  
2774 0 ▶ TINFL_HUFF_DECODE(26, dist, r->m_look_up[1], r->m_tree_1);
Branch: 14 branches
BranchTrueFalseTotal
TINFL_HUFF_DECODE(26, dist, r->m_look_up[1], r->m_tree_1); 0 0 0
TINFL_HUFF_DECODE(26, dist, r->m_look_up[1], r->m_tree_1); 0 0 0
TINFL_HUFF_DECODE(26, dist, r->m_look_up[1], r->m_tree_1); 0 0 0
TINFL_HUFF_DECODE(26, dist, r->m_look_up[1], r->m_tree_1); 0 0 0
TINFL_HUFF_DECODE(26, dist, r->m_look_up[1], r->m_tree_1); 0 0 0
TINFL_HUFF_DECODE(26, dist, r->m_look_up[1], r->m_tree_1); 0 0 0
TINFL_HUFF_DECODE(26, dist, r->m_look_up[1], r->m_tree_1); 0 0 0
TINFL_HUFF_DECODE(26, dist, r->m_look_up[1], r->m_tree_1); 0 0 0
TINFL_HUFF_DECODE(26, dist, r->m_look_up[1], r->m_tree_1); 0 0 0
TINFL_HUFF_DECODE(26, dist, r->m_look_up[1], r->m_tree_1); 0 0 0
TINFL_HUFF_DECODE(26, dist, r->m_look_up[1], r->m_tree_1); 0 0 0
TINFL_HUFF_DECODE(26, dist, r->m_look_up[1], r->m_tree_1); 0 0 0
TINFL_HUFF_DECODE(26, dist, r->m_look_up[1], r->m_tree_1); 0 0 0
TINFL_HUFF_DECODE(26, dist, r->m_look_up[1], r->m_tree_1); 0 0 0
2775 0 num_extra = s_dist_extra[dist];
2776 0 dist = s_dist_base[dist];
2777 0 ▶ if (num_extra)
Branch: 1 branch
BranchTrueFalseTotal
if (num_extra) 0 0 0
2778 {
2779 mz_uint extra_bits;
2780 0 ▶ TINFL_GET_BITS(27, extra_bits, num_extra);
Branch: 4 branches
BranchTrueFalseTotal
TINFL_GET_BITS(27, extra_bits, num_extra); 0 0 0
TINFL_GET_BITS(27, extra_bits, num_extra); 0 0 0
TINFL_GET_BITS(27, extra_bits, num_extra); 0 0 0
TINFL_GET_BITS(27, extra_bits, num_extra); 0 0 0
2781 0 dist += extra_bits;
2782 }
2783  
2784 0 dist_from_out_buf_start = pOut_buf_cur - pOut_buf_start;
2785 0 ▶ if ((dist == 0 || dist > dist_from_out_buf_start || dist_from_out_buf_start == 0) && (decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF))
Branch: 4 branches
BranchTrueFalseTotal
if ((dist == 0 || dist > dist_from_out_buf_start || dist_from_out_buf_start == 0) && (decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF)) 0 0 0
if ((dist == 0 || dist > dist_from_out_buf_start || dist_from_out_buf_start == 0) && (decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF)) 0 0 0
if ((dist == 0 || dist > dist_from_out_buf_start || dist_from_out_buf_start == 0) && (decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF)) 0 0 0
if ((dist == 0 || dist > dist_from_out_buf_start || dist_from_out_buf_start == 0) && (decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF)) 0 0 0
2786 {
2787 0 TINFL_CR_RETURN_FOREVER(37, TINFL_STATUS_FAILED);
2788 }
2789  
2790 0 pSrc = pOut_buf_start + ((dist_from_out_buf_start - dist) & out_buf_size_mask);
2791  
2792 0 ▶ if ((MZ_MAX(pOut_buf_cur, pSrc) + counter) > pOut_buf_end)
Branch: 1 branch
BranchTrueFalseTotal
if ((MZ_MAX(pOut_buf_cur, pSrc) + counter) > pOut_buf_end) 0 0 0
2793 {
2794 0 ▶ while (counter--)
Branch: 1 branch
BranchTrueFalseTotal
while (counter--) 0 0 0
2795 {
2796 0 while (pOut_buf_cur >= pOut_buf_end)
2797 {
2798 0 ▶ TINFL_CR_RETURN(53, TINFL_STATUS_HAS_MORE_OUTPUT);
Branch: 1 branch
BranchTrueFalseTotal
TINFL_CR_RETURN(53, TINFL_STATUS_HAS_MORE_OUTPUT); 0 0 0
2799 }
2800 0 *pOut_buf_cur++ = pOut_buf_start[(dist_from_out_buf_start++ - dist) & out_buf_size_mask];
2801 }
2802 0 continue;
2803 }
2804 #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES
2805 else if ((counter >= 9) && (counter <= dist))
2806 {
2807 const mz_uint8 *pSrc_end = pSrc + (counter & ~7);
2808 do
2809 {
2810 #ifdef MINIZ_UNALIGNED_USE_MEMCPY
2811 memcpy(pOut_buf_cur, pSrc, sizeof(mz_uint32) * 2);
2812 #else
2813 ((mz_uint32 *)pOut_buf_cur)[0] = ((const mz_uint32 *)pSrc)[0];
2814 ((mz_uint32 *)pOut_buf_cur)[1] = ((const mz_uint32 *)pSrc)[1];
2815 #endif
2816 pOut_buf_cur += 8;
2817 } while ((pSrc += 8) < pSrc_end);
2818 if ((counter &= 7) < 3)
2819 {
2820 if (counter)
2821 {
2822 pOut_buf_cur[0] = pSrc[0];
2823 if (counter > 1)
2824 pOut_buf_cur[1] = pSrc[1];
2825 pOut_buf_cur += counter;
2826 }
2827 continue;
2828 }
2829 }
2830 #endif
2831 0 ▶ while (counter > 2)
Branch: 1 branch
BranchTrueFalseTotal
while (counter > 2) 0 0 0
2832 {
2833 0 pOut_buf_cur[0] = pSrc[0];
2834 0 pOut_buf_cur[1] = pSrc[1];
2835 0 pOut_buf_cur[2] = pSrc[2];
2836 0 pOut_buf_cur += 3;
2837 0 pSrc += 3;
2838 0 counter -= 3;
2839 }
2840 0 ▶ if (counter > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (counter > 0) 0 0 0
2841 {
2842 0 pOut_buf_cur[0] = pSrc[0];
2843 0 ▶ if (counter > 1)
Branch: 1 branch
BranchTrueFalseTotal
if (counter > 1) 0 0 0
2844 0 pOut_buf_cur[1] = pSrc[1];
2845 0 pOut_buf_cur += counter;
2846 }
2847 }
2848 }
2849 0 ▶ } while (!(r->m_final & 1));
Branch: 1 branch
BranchTrueFalseTotal
} while (!(r->m_final & 1)); 0 0 0
2850  
2851 /* Ensure byte alignment and put back any bytes from the bitbuf if we've looked ahead too far on gzip, or other Deflate streams followed by arbitrary data. */
2852 /* I'm being super conservative here. A number of simplifications can be made to the byte alignment part, and the Adler32 check shouldn't ever need to worry about reading from the bitbuf now. */
2853 0 ▶ TINFL_SKIP_BITS(32, num_bits & 7);
Branch: 4 branches
BranchTrueFalseTotal
TINFL_SKIP_BITS(32, num_bits & 7); 0 0 0
TINFL_SKIP_BITS(32, num_bits & 7); 0 0 0
TINFL_SKIP_BITS(32, num_bits & 7); 0 0 0
TINFL_SKIP_BITS(32, num_bits & 7); 0 0 0
2854 0 ▶ while ((pIn_buf_cur > pIn_buf_next) && (num_bits >= 8))
Branch: 2 branches
BranchTrueFalseTotal
while ((pIn_buf_cur > pIn_buf_next) && (num_bits >= 8)) 0 0 0
while ((pIn_buf_cur > pIn_buf_next) && (num_bits >= 8)) 0 0 0
2855 {
2856 0 --pIn_buf_cur;
2857 0 num_bits -= 8;
2858 }
2859 0 bit_buf &= ~(~(tinfl_bit_buf_t)0 << num_bits);
2860 MZ_ASSERT(!num_bits); /* if this assert fires then we've read beyond the end of non-deflate/zlib streams with following data (such as gzip streams). */
2861  
2862 0 ▶ if (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER)
Branch: 1 branch
BranchTrueFalseTotal
if (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER) 0 0 0
2863 {
2864 0 ▶ for (counter = 0; counter < 4; ++counter)
Branch: 1 branch
BranchTrueFalseTotal
for (counter = 0; counter < 4; ++counter) 0 0 0
2865 {
2866 mz_uint s;
2867 0 ▶ if (num_bits)
Branch: 1 branch
BranchTrueFalseTotal
if (num_bits) 0 0 0
2868 0 ▶ TINFL_GET_BITS(41, s, 8);
Branch: 4 branches
BranchTrueFalseTotal
TINFL_GET_BITS(41, s, 8); 0 0 0
TINFL_GET_BITS(41, s, 8); 0 0 0
TINFL_GET_BITS(41, s, 8); 0 0 0
TINFL_GET_BITS(41, s, 8); 0 0 0
2869 else
2870 0 ▶ TINFL_GET_BYTE(42, s);
Branch: 2 branches
BranchTrueFalseTotal
TINFL_GET_BYTE(42, s); 0 0 0
TINFL_GET_BYTE(42, s); 0 0 0
2871 0 r->m_z_adler32 = (r->m_z_adler32 << 8) | s;
2872 }
2873 }
2874 0 TINFL_CR_RETURN_FOREVER(34, TINFL_STATUS_DONE);
2875  
2876 TINFL_CR_FINISH
2877  
2878 0 common_exit:
2879 /* As long as we aren't telling the caller that we NEED more input to make forward progress: */
2880 /* Put back any bytes from the bitbuf in case we've looked ahead too far on gzip, or other Deflate streams followed by arbitrary data. */
2881 /* We need to be very careful here to NOT push back any bytes we definitely know we need to make forward progress, though, or we'll lock the caller up into an inf loop. */
2882 0 ▶ if ((status != TINFL_STATUS_NEEDS_MORE_INPUT) && (status != TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS))
Branch: 2 branches
BranchTrueFalseTotal
if ((status != TINFL_STATUS_NEEDS_MORE_INPUT) && (status != TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS)) 0 0 0
if ((status != TINFL_STATUS_NEEDS_MORE_INPUT) && (status != TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS)) 0 0 0
2883 {
2884 0 ▶ while ((pIn_buf_cur > pIn_buf_next) && (num_bits >= 8))
Branch: 2 branches
BranchTrueFalseTotal
while ((pIn_buf_cur > pIn_buf_next) && (num_bits >= 8)) 0 0 0
while ((pIn_buf_cur > pIn_buf_next) && (num_bits >= 8)) 0 0 0
2885 {
2886 0 --pIn_buf_cur;
2887 0 num_bits -= 8;
2888 }
2889 }
2890 0 r->m_num_bits = num_bits;
2891 0 r->m_bit_buf = bit_buf & ~(~(tinfl_bit_buf_t)0 << num_bits);
2892 0 r->m_dist = dist;
2893 0 r->m_counter = counter;
2894 0 r->m_num_extra = num_extra;
2895 0 r->m_dist_from_out_buf_start = dist_from_out_buf_start;
2896 0 *pIn_buf_size = pIn_buf_cur - pIn_buf_next;
2897 0 *pOut_buf_size = pOut_buf_cur - pOut_buf_next;
2898 0 ▶ if ((decomp_flags & (TINFL_FLAG_PARSE_ZLIB_HEADER | TINFL_FLAG_COMPUTE_ADLER32)) && (status >= 0))
Branch: 2 branches
BranchTrueFalseTotal
if ((decomp_flags & (TINFL_FLAG_PARSE_ZLIB_HEADER | TINFL_FLAG_COMPUTE_ADLER32)) && (status >= 0)) 0 0 0
if ((decomp_flags & (TINFL_FLAG_PARSE_ZLIB_HEADER | TINFL_FLAG_COMPUTE_ADLER32)) && (status >= 0)) 0 0 0
2899 {
2900 0 const mz_uint8 *ptr = pOut_buf_next;
2901 0 size_t buf_len = *pOut_buf_size;
2902 0 mz_uint32 i, s1 = r->m_check_adler32 & 0xffff, s2 = r->m_check_adler32 >> 16;
2903 0 size_t block_len = buf_len % 5552;
2904 0 ▶ while (buf_len)
Branch: 1 branch
BranchTrueFalseTotal
while (buf_len) 0 0 0
2905 {
2906 0 ▶ for (i = 0; i + 7 < block_len; i += 8, ptr += 8)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 7 < block_len; i += 8, ptr += 8) 0 0 0
2907 {
2908 0 s1 += ptr[0], s2 += s1;
2909 0 s1 += ptr[1], s2 += s1;
2910 0 s1 += ptr[2], s2 += s1;
2911 0 s1 += ptr[3], s2 += s1;
2912 0 s1 += ptr[4], s2 += s1;
2913 0 s1 += ptr[5], s2 += s1;
2914 0 s1 += ptr[6], s2 += s1;
2915 0 s1 += ptr[7], s2 += s1;
2916 }
2917 0 ▶ for (; i < block_len; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (; i < block_len; ++i) 0 0 0
2918 0 s1 += *ptr++, s2 += s1;
2919 0 s1 %= 65521U, s2 %= 65521U;
2920 0 buf_len -= block_len;
2921 0 block_len = 5552;
2922 }
2923 0 r->m_check_adler32 = (s2 << 16) + s1;
2924 0 ▶ if ((status == TINFL_STATUS_DONE) && (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER) && (r->m_check_adler32 != r->m_z_adler32))
Branch: 3 branches
BranchTrueFalseTotal
if ((status == TINFL_STATUS_DONE) && (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER) && (r->m_check_adler32 != r->m_z_adler32)) 0 0 0
if ((status == TINFL_STATUS_DONE) && (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER) && (r->m_check_adler32 != r->m_z_adler32)) 0 0 0
if ((status == TINFL_STATUS_DONE) && (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER) && (r->m_check_adler32 != r->m_z_adler32)) 0 0 0
2925 0 status = TINFL_STATUS_ADLER32_MISMATCH;
2926 }
2927 0 return status;
2928 }
2929  
2930 /* Higher level helper functions. */
2931 0 void *tinfl_decompress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, size_t *pOut_len, int flags)
2932 {
2933 tinfl_decompressor decomp;
2934 0 void *pBuf = NULL, *pNew_buf;
2935 0 size_t src_buf_ofs = 0, out_buf_capacity = 0;
2936 0 *pOut_len = 0;
2937 0 tinfl_init(&decomp);
2938 for (;;)
2939 0 {
2940 0 size_t src_buf_size = src_buf_len - src_buf_ofs, dst_buf_size = out_buf_capacity - *pOut_len, new_out_buf_capacity;
2941 0 tinfl_status status = tinfl_decompress(&decomp, (const mz_uint8 *)pSrc_buf + src_buf_ofs, &src_buf_size, (mz_uint8 *)pBuf, pBuf ? (mz_uint8 *)pBuf + *pOut_len : NULL, &dst_buf_size,
2942 0 ▶ (flags & ~TINFL_FLAG_HAS_MORE_INPUT) | TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF);
Branch: 1 branch
BranchTrueFalseTotal
(flags & ~TINFL_FLAG_HAS_MORE_INPUT) | TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF); 0 0 0
2943 0 ▶ if ((status < 0) || (status == TINFL_STATUS_NEEDS_MORE_INPUT))
Branch: 2 branches
BranchTrueFalseTotal
if ((status < 0) || (status == TINFL_STATUS_NEEDS_MORE_INPUT)) 0 0 0
if ((status < 0) || (status == TINFL_STATUS_NEEDS_MORE_INPUT)) 0 0 0
2944 {
2945 0 MZ_FREE(pBuf);
2946 0 *pOut_len = 0;
2947 0 return NULL;
2948 }
2949 0 src_buf_ofs += src_buf_size;
2950 0 *pOut_len += dst_buf_size;
2951 0 ▶ if (status == TINFL_STATUS_DONE)
Branch: 1 branch
BranchTrueFalseTotal
if (status == TINFL_STATUS_DONE) 0 0 0
2952 0 break;
2953 0 new_out_buf_capacity = out_buf_capacity * 2;
2954 0 ▶ if (new_out_buf_capacity < 128)
Branch: 1 branch
BranchTrueFalseTotal
if (new_out_buf_capacity < 128) 0 0 0
2955 0 new_out_buf_capacity = 128;
2956 0 pNew_buf = MZ_REALLOC(pBuf, new_out_buf_capacity);
2957 0 ▶ if (!pNew_buf)
Branch: 1 branch
BranchTrueFalseTotal
if (!pNew_buf) 0 0 0
2958 {
2959 0 MZ_FREE(pBuf);
2960 0 *pOut_len = 0;
2961 0 return NULL;
2962 }
2963 0 pBuf = pNew_buf;
2964 0 out_buf_capacity = new_out_buf_capacity;
2965 }
2966 0 return pBuf;
2967 }
2968  
2969 0 size_t tinfl_decompress_mem_to_mem(void *pOut_buf, size_t out_buf_len, const void *pSrc_buf, size_t src_buf_len, int flags)
2970 {
2971 tinfl_decompressor decomp;
2972 tinfl_status status;
2973 0 tinfl_init(&decomp);
2974 0 status = tinfl_decompress(&decomp, (const mz_uint8 *)pSrc_buf, &src_buf_len, (mz_uint8 *)pOut_buf, (mz_uint8 *)pOut_buf, &out_buf_len, (flags & ~TINFL_FLAG_HAS_MORE_INPUT) | TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF);
2975 0 ▶ return (status != TINFL_STATUS_DONE) ? TINFL_DECOMPRESS_MEM_TO_MEM_FAILED : out_buf_len;
Branch: 1 branch
BranchTrueFalseTotal
return (status != TINFL_STATUS_DONE) ? TINFL_DECOMPRESS_MEM_TO_MEM_FAILED : out_buf_len; 0 0 0
2976 }
2977  
2978 0 int tinfl_decompress_mem_to_callback(const void *pIn_buf, size_t *pIn_buf_size, tinfl_put_buf_func_ptr pPut_buf_func, void *pPut_buf_user, int flags)
2979 {
2980 0 int result = 0;
2981 tinfl_decompressor decomp;
2982 0 mz_uint8 *pDict = (mz_uint8 *)MZ_MALLOC(TINFL_LZ_DICT_SIZE);
2983 0 size_t in_buf_ofs = 0, dict_ofs = 0;
2984 0 ▶ if (!pDict)
Branch: 1 branch
BranchTrueFalseTotal
if (!pDict) 0 0 0
2985 0 return TINFL_STATUS_FAILED;
2986 0 memset(pDict, 0, TINFL_LZ_DICT_SIZE);
2987 0 tinfl_init(&decomp);
2988 for (;;)
2989 0 {
2990 0 size_t in_buf_size = *pIn_buf_size - in_buf_ofs, dst_buf_size = TINFL_LZ_DICT_SIZE - dict_ofs;
2991 0 tinfl_status status = tinfl_decompress(&decomp, (const mz_uint8 *)pIn_buf + in_buf_ofs, &in_buf_size, pDict, pDict + dict_ofs, &dst_buf_size,
2992 0 (flags & ~(TINFL_FLAG_HAS_MORE_INPUT | TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF)));
2993 0 in_buf_ofs += in_buf_size;
2994 0 ▶ if ((dst_buf_size) && (!(*pPut_buf_func)(pDict + dict_ofs, (int)dst_buf_size, pPut_buf_user)))
Branch: 2 branches
BranchTrueFalseTotal
if ((dst_buf_size) && (!(*pPut_buf_func)(pDict + dict_ofs, (int)dst_buf_size, pPut_buf_user))) 0 0 0
if ((dst_buf_size) && (!(*pPut_buf_func)(pDict + dict_ofs, (int)dst_buf_size, pPut_buf_user))) 0 0 0
2995 0 break;
2996 0 ▶ if (status != TINFL_STATUS_HAS_MORE_OUTPUT)
Branch: 1 branch
BranchTrueFalseTotal
if (status != TINFL_STATUS_HAS_MORE_OUTPUT) 0 0 0
2997 {
2998 0 result = (status == TINFL_STATUS_DONE);
2999 0 break;
3000 }
3001 0 dict_ofs = (dict_ofs + dst_buf_size) & (TINFL_LZ_DICT_SIZE - 1);
3002 }
3003 0 MZ_FREE(pDict);
3004 0 *pIn_buf_size = in_buf_ofs;
3005 0 return result;
3006 }
3007  
3008 #ifndef MINIZ_NO_MALLOC
3009 0 tinfl_decompressor *tinfl_decompressor_alloc(void)
3010 {
3011 0 tinfl_decompressor *pDecomp = (tinfl_decompressor *)MZ_MALLOC(sizeof(tinfl_decompressor));
3012 0 ▶ if (pDecomp)
Branch: 1 branch
BranchTrueFalseTotal
if (pDecomp) 0 0 0
3013 0 tinfl_init(pDecomp);
3014 0 return pDecomp;
3015 }
3016  
3017 0 void tinfl_decompressor_free(tinfl_decompressor *pDecomp)
3018 {
3019 0 MZ_FREE(pDecomp);
3020 0 }
3021 #endif
3022  
3023 #ifdef __cplusplus
3024 }
3025 #endif
3026  
3027 #endif /*#ifndef MINIZ_NO_INFLATE_APIS*/
3028 /**************************************************************************
3029 *
3030 * Copyright 2013-2014 RAD Game Tools and Valve Software
3031 * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
3032 * Copyright 2016 Martin Raiber
3033 * All Rights Reserved.
3034 *
3035 * Permission is hereby granted, free of charge, to any person obtaining a copy
3036 * of this software and associated documentation files (the "Software"), to deal
3037 * in the Software without restriction, including without limitation the rights
3038 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
3039 * copies of the Software, and to permit persons to whom the Software is
3040 * furnished to do so, subject to the following conditions:
3041 *
3042 * The above copyright notice and this permission notice shall be included in
3043 * all copies or substantial portions of the Software.
3044 *
3045 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
3046 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
3047 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
3048 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
3049 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
3050 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
3051 * THE SOFTWARE.
3052 *
3053 **************************************************************************/
3054  
3055  
3056 #ifndef MINIZ_NO_ARCHIVE_APIS
3057  
3058 #ifdef __cplusplus
3059 extern "C"
3060 {
3061 #endif
3062  
3063 /* ------------------- .ZIP archive reading */
3064  
3065 #ifdef MINIZ_NO_STDIO
3066 #define MZ_FILE void *
3067 #else
3068 #include <sys/stat.h>
3069  
3070 #if defined(_MSC_VER) || defined(__MINGW64__) || defined(__MINGW32__)
3071  
3072 #ifndef WIN32_LEAN_AND_MEAN
3073 #define WIN32_LEAN_AND_MEAN
3074 #endif
3075 #ifndef __cplusplus
3076 #define MICROSOFT_WINDOWS_WINBASE_H_DEFINE_INTERLOCKED_CPLUSPLUS_OVERLOADS 0
3077 #endif
3078 #ifndef NOMINMAX
3079 #define NOMINMAX
3080 #endif
3081 #include <windows.h>
3082  
3083 static WCHAR *mz_utf8z_to_widechar(const char *str)
3084 {
3085 int reqChars = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
3086 WCHAR *wStr = (WCHAR *)malloc(reqChars * sizeof(WCHAR));
3087 MultiByteToWideChar(CP_UTF8, 0, str, -1, wStr, reqChars);
3088 return wStr;
3089 }
3090  
3091 static FILE *mz_fopen(const char *pFilename, const char *pMode)
3092 {
3093 WCHAR *wFilename = mz_utf8z_to_widechar(pFilename);
3094 WCHAR *wMode = mz_utf8z_to_widechar(pMode);
3095 FILE *pFile = NULL;
3096 errno_t err = _wfopen_s(&pFile, wFilename, wMode);
3097 free(wFilename);
3098 free(wMode);
3099 return err ? NULL : pFile;
3100 }
3101  
3102 static FILE *mz_freopen(const char *pPath, const char *pMode, FILE *pStream)
3103 {
3104 WCHAR *wPath = mz_utf8z_to_widechar(pPath);
3105 WCHAR *wMode = mz_utf8z_to_widechar(pMode);
3106 FILE *pFile = NULL;
3107 errno_t err = _wfreopen_s(&pFile, wPath, wMode, pStream);
3108 free(wPath);
3109 free(wMode);
3110 return err ? NULL : pFile;
3111 }
3112  
3113 #if defined(__MINGW32__)
3114 static int mz_stat(const char *path, struct _stat *buffer)
3115 {
3116 WCHAR *wPath = mz_utf8z_to_widechar(path);
3117 int res = _wstat(wPath, buffer);
3118 free(wPath);
3119 return res;
3120 }
3121 #else
3122 static int mz_stat64(const char *path, struct __stat64 *buffer)
3123 {
3124 WCHAR *wPath = mz_utf8z_to_widechar(path);
3125 int res = _wstat64(wPath, buffer);
3126 free(wPath);
3127 return res;
3128 }
3129 #endif
3130  
3131 #ifndef MINIZ_NO_TIME
3132 #include <sys/utime.h>
3133 #endif
3134 #define MZ_FOPEN mz_fopen
3135 #define MZ_FCLOSE fclose
3136 #define MZ_FREAD fread
3137 #define MZ_FWRITE fwrite
3138 #define MZ_FTELL64 _ftelli64
3139 #define MZ_FSEEK64 _fseeki64
3140 #if defined(__MINGW32__)
3141 #define MZ_FILE_STAT_STRUCT _stat
3142 #define MZ_FILE_STAT mz_stat
3143 #else
3144 #define MZ_FILE_STAT_STRUCT _stat64
3145 #define MZ_FILE_STAT mz_stat64
3146 #endif
3147 #define MZ_FFLUSH fflush
3148 #define MZ_FREOPEN mz_freopen
3149 #define MZ_DELETE_FILE remove
3150  
3151 #elif defined(__WATCOMC__)
3152 #ifndef MINIZ_NO_TIME
3153 #include <sys/utime.h>
3154 #endif
3155 #define MZ_FOPEN(f, m) fopen(f, m)
3156 #define MZ_FCLOSE fclose
3157 #define MZ_FREAD fread
3158 #define MZ_FWRITE fwrite
3159 #define MZ_FTELL64 _ftelli64
3160 #define MZ_FSEEK64 _fseeki64
3161 #define MZ_FILE_STAT_STRUCT stat
3162 #define MZ_FILE_STAT stat
3163 #define MZ_FFLUSH fflush
3164 #define MZ_FREOPEN(f, m, s) freopen(f, m, s)
3165 #define MZ_DELETE_FILE remove
3166  
3167 #elif defined(__TINYC__)
3168 #ifndef MINIZ_NO_TIME
3169 #include <sys/utime.h>
3170 #endif
3171 #define MZ_FOPEN(f, m) fopen(f, m)
3172 #define MZ_FCLOSE fclose
3173 #define MZ_FREAD fread
3174 #define MZ_FWRITE fwrite
3175 #define MZ_FTELL64 ftell
3176 #define MZ_FSEEK64 fseek
3177 #define MZ_FILE_STAT_STRUCT stat
3178 #define MZ_FILE_STAT stat
3179 #define MZ_FFLUSH fflush
3180 #define MZ_FREOPEN(f, m, s) freopen(f, m, s)
3181 #define MZ_DELETE_FILE remove
3182  
3183 #elif defined(__USE_LARGEFILE64) /* gcc, clang */
3184 #ifndef MINIZ_NO_TIME
3185 #include <utime.h>
3186 #endif
3187 #define MZ_FOPEN(f, m) fopen64(f, m)
3188 #define MZ_FCLOSE fclose
3189 #define MZ_FREAD fread
3190 #define MZ_FWRITE fwrite
3191 #define MZ_FTELL64 ftello64
3192 #define MZ_FSEEK64 fseeko64
3193 #define MZ_FILE_STAT_STRUCT stat64
3194 #define MZ_FILE_STAT stat64
3195 #define MZ_FFLUSH fflush
3196 #define MZ_FREOPEN(p, m, s) freopen64(p, m, s)
3197 #define MZ_DELETE_FILE remove
3198  
3199 #elif defined(__APPLE__) || defined(__FreeBSD__) || (defined(__linux__) && defined(__x86_64__))
3200 #ifndef MINIZ_NO_TIME
3201 #include <utime.h>
3202 #endif
3203 #define MZ_FOPEN(f, m) fopen(f, m)
3204 #define MZ_FCLOSE fclose
3205 #define MZ_FREAD fread
3206 #define MZ_FWRITE fwrite
3207 #define MZ_FTELL64 ftello
3208 #define MZ_FSEEK64 fseeko
3209 #define MZ_FILE_STAT_STRUCT stat
3210 #define MZ_FILE_STAT stat
3211 #define MZ_FFLUSH fflush
3212 #define MZ_FREOPEN(p, m, s) freopen(p, m, s)
3213 #define MZ_DELETE_FILE remove
3214  
3215 #else
3216 #pragma message("Using fopen, ftello, fseeko, stat() etc. path for file I/O - this path may not support large files.")
3217 #ifndef MINIZ_NO_TIME
3218 #include <utime.h>
3219 #endif
3220 #define MZ_FOPEN(f, m) fopen(f, m)
3221 #define MZ_FCLOSE fclose
3222 #define MZ_FREAD fread
3223 #define MZ_FWRITE fwrite
3224 #ifdef __STRICT_ANSI__
3225 #define MZ_FTELL64 ftell
3226 #define MZ_FSEEK64 fseek
3227 #else
3228 #define MZ_FTELL64 ftello
3229 #define MZ_FSEEK64 fseeko
3230 #endif
3231 #define MZ_FILE_STAT_STRUCT stat
3232 #define MZ_FILE_STAT stat
3233 #define MZ_FFLUSH fflush
3234 #define MZ_FREOPEN(f, m, s) freopen(f, m, s)
3235 #define MZ_DELETE_FILE remove
3236 #endif /* #ifdef _MSC_VER */
3237 #endif /* #ifdef MINIZ_NO_STDIO */
3238  
3239 #define MZ_TOLOWER(c) ((((c) >= 'A') && ((c) <= 'Z')) ? ((c) - 'A' + 'a') : (c))
3240  
3241 /* Various ZIP archive enums. To completely avoid cross platform compiler alignment and platform endian issues, miniz.c doesn't use structs for any of this stuff. */
3242 enum
3243 {
3244 /* ZIP archive identifiers and record sizes */
3245 MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIG = 0x06054b50,
3246 MZ_ZIP_CENTRAL_DIR_HEADER_SIG = 0x02014b50,
3247 MZ_ZIP_LOCAL_DIR_HEADER_SIG = 0x04034b50,
3248 MZ_ZIP_LOCAL_DIR_HEADER_SIZE = 30,
3249 MZ_ZIP_CENTRAL_DIR_HEADER_SIZE = 46,
3250 MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE = 22,
3251  
3252 /* ZIP64 archive identifier and record sizes */
3253 MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIG = 0x06064b50,
3254 MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIG = 0x07064b50,
3255 MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE = 56,
3256 MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE = 20,
3257 MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID = 0x0001,
3258 MZ_ZIP_DATA_DESCRIPTOR_ID = 0x08074b50,
3259 MZ_ZIP_DATA_DESCRIPTER_SIZE64 = 24,
3260 MZ_ZIP_DATA_DESCRIPTER_SIZE32 = 16,
3261  
3262 /* Central directory header record offsets */
3263 MZ_ZIP_CDH_SIG_OFS = 0,
3264 MZ_ZIP_CDH_VERSION_MADE_BY_OFS = 4,
3265 MZ_ZIP_CDH_VERSION_NEEDED_OFS = 6,
3266 MZ_ZIP_CDH_BIT_FLAG_OFS = 8,
3267 MZ_ZIP_CDH_METHOD_OFS = 10,
3268 MZ_ZIP_CDH_FILE_TIME_OFS = 12,
3269 MZ_ZIP_CDH_FILE_DATE_OFS = 14,
3270 MZ_ZIP_CDH_CRC32_OFS = 16,
3271 MZ_ZIP_CDH_COMPRESSED_SIZE_OFS = 20,
3272 MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS = 24,
3273 MZ_ZIP_CDH_FILENAME_LEN_OFS = 28,
3274 MZ_ZIP_CDH_EXTRA_LEN_OFS = 30,
3275 MZ_ZIP_CDH_COMMENT_LEN_OFS = 32,
3276 MZ_ZIP_CDH_DISK_START_OFS = 34,
3277 MZ_ZIP_CDH_INTERNAL_ATTR_OFS = 36,
3278 MZ_ZIP_CDH_EXTERNAL_ATTR_OFS = 38,
3279 MZ_ZIP_CDH_LOCAL_HEADER_OFS = 42,
3280  
3281 /* Local directory header offsets */
3282 MZ_ZIP_LDH_SIG_OFS = 0,
3283 MZ_ZIP_LDH_VERSION_NEEDED_OFS = 4,
3284 MZ_ZIP_LDH_BIT_FLAG_OFS = 6,
3285 MZ_ZIP_LDH_METHOD_OFS = 8,
3286 MZ_ZIP_LDH_FILE_TIME_OFS = 10,
3287 MZ_ZIP_LDH_FILE_DATE_OFS = 12,
3288 MZ_ZIP_LDH_CRC32_OFS = 14,
3289 MZ_ZIP_LDH_COMPRESSED_SIZE_OFS = 18,
3290 MZ_ZIP_LDH_DECOMPRESSED_SIZE_OFS = 22,
3291 MZ_ZIP_LDH_FILENAME_LEN_OFS = 26,
3292 MZ_ZIP_LDH_EXTRA_LEN_OFS = 28,
3293 MZ_ZIP_LDH_BIT_FLAG_HAS_LOCATOR = 1 << 3,
3294  
3295 /* End of central directory offsets */
3296 MZ_ZIP_ECDH_SIG_OFS = 0,
3297 MZ_ZIP_ECDH_NUM_THIS_DISK_OFS = 4,
3298 MZ_ZIP_ECDH_NUM_DISK_CDIR_OFS = 6,
3299 MZ_ZIP_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS = 8,
3300 MZ_ZIP_ECDH_CDIR_TOTAL_ENTRIES_OFS = 10,
3301 MZ_ZIP_ECDH_CDIR_SIZE_OFS = 12,
3302 MZ_ZIP_ECDH_CDIR_OFS_OFS = 16,
3303 MZ_ZIP_ECDH_COMMENT_SIZE_OFS = 20,
3304  
3305 /* ZIP64 End of central directory locator offsets */
3306 MZ_ZIP64_ECDL_SIG_OFS = 0, /* 4 bytes */
3307 MZ_ZIP64_ECDL_NUM_DISK_CDIR_OFS = 4, /* 4 bytes */
3308 MZ_ZIP64_ECDL_REL_OFS_TO_ZIP64_ECDR_OFS = 8, /* 8 bytes */
3309 MZ_ZIP64_ECDL_TOTAL_NUMBER_OF_DISKS_OFS = 16, /* 4 bytes */
3310  
3311 /* ZIP64 End of central directory header offsets */
3312 MZ_ZIP64_ECDH_SIG_OFS = 0, /* 4 bytes */
3313 MZ_ZIP64_ECDH_SIZE_OF_RECORD_OFS = 4, /* 8 bytes */
3314 MZ_ZIP64_ECDH_VERSION_MADE_BY_OFS = 12, /* 2 bytes */
3315 MZ_ZIP64_ECDH_VERSION_NEEDED_OFS = 14, /* 2 bytes */
3316 MZ_ZIP64_ECDH_NUM_THIS_DISK_OFS = 16, /* 4 bytes */
3317 MZ_ZIP64_ECDH_NUM_DISK_CDIR_OFS = 20, /* 4 bytes */
3318 MZ_ZIP64_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS = 24, /* 8 bytes */
3319 MZ_ZIP64_ECDH_CDIR_TOTAL_ENTRIES_OFS = 32, /* 8 bytes */
3320 MZ_ZIP64_ECDH_CDIR_SIZE_OFS = 40, /* 8 bytes */
3321 MZ_ZIP64_ECDH_CDIR_OFS_OFS = 48, /* 8 bytes */
3322 MZ_ZIP_VERSION_MADE_BY_DOS_FILESYSTEM_ID = 0,
3323 MZ_ZIP_DOS_DIR_ATTRIBUTE_BITFLAG = 0x10,
3324 MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_IS_ENCRYPTED = 1,
3325 MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_COMPRESSED_PATCH_FLAG = 32,
3326 MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_USES_STRONG_ENCRYPTION = 64,
3327 MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_LOCAL_DIR_IS_MASKED = 8192,
3328 MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_UTF8 = 1 << 11
3329 };
3330  
3331 typedef struct
3332 {
3333 void *m_p;
3334 size_t m_size, m_capacity;
3335 mz_uint m_element_size;
3336 } mz_zip_array;
3337  
3338 struct mz_zip_internal_state_tag
3339 {
3340 mz_zip_array m_central_dir;
3341 mz_zip_array m_central_dir_offsets;
3342 mz_zip_array m_sorted_central_dir_offsets;
3343  
3344 /* The flags passed in when the archive is initially opened. */
3345 mz_uint32 m_init_flags;
3346  
3347 /* MZ_TRUE if the archive has a zip64 end of central directory headers, etc. */
3348 mz_bool m_zip64;
3349  
3350 /* MZ_TRUE if we found zip64 extended info in the central directory (m_zip64 will also be slammed to true too, even if we didn't find a zip64 end of central dir header, etc.) */
3351 mz_bool m_zip64_has_extended_info_fields;
3352  
3353 /* These fields are used by the file, FILE, memory, and memory/heap read/write helpers. */
3354 MZ_FILE *m_pFile;
3355 mz_uint64 m_file_archive_start_ofs;
3356  
3357 void *m_pMem;
3358 size_t m_mem_size;
3359 size_t m_mem_capacity;
3360 };
3361  
3362 #define MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(array_ptr, element_size) (array_ptr)->m_element_size = element_size
3363  
3364 #if defined(DEBUG) || defined(_DEBUG)
3365 static MZ_FORCEINLINE mz_uint mz_zip_array_range_check(const mz_zip_array *pArray, mz_uint index)
3366 {
3367 MZ_ASSERT(index < pArray->m_size);
3368 return index;
3369 }
3370 #define MZ_ZIP_ARRAY_ELEMENT(array_ptr, element_type, index) ((element_type *)((array_ptr)->m_p))[mz_zip_array_range_check(array_ptr, index)]
3371 #else
3372 #define MZ_ZIP_ARRAY_ELEMENT(array_ptr, element_type, index) ((element_type *)((array_ptr)->m_p))[index]
3373 #endif
3374  
3375 static MZ_FORCEINLINE void mz_zip_array_init(mz_zip_array *pArray, mz_uint32 element_size)
3376 {
3377 0 memset(pArray, 0, sizeof(mz_zip_array));
3378 0 pArray->m_element_size = element_size;
3379 0 }
3380  
3381 static MZ_FORCEINLINE void mz_zip_array_clear(mz_zip_archive *pZip, mz_zip_array *pArray)
3382 {
3383 0 pZip->m_pFree(pZip->m_pAlloc_opaque, pArray->m_p);
3384 0 memset(pArray, 0, sizeof(mz_zip_array));
3385 0 }
3386  
3387 0 static mz_bool mz_zip_array_ensure_capacity(mz_zip_archive *pZip, mz_zip_array *pArray, size_t min_new_capacity, mz_uint growing)
3388 {
3389 void *pNew_p;
3390 0 size_t new_capacity = min_new_capacity;
3391 MZ_ASSERT(pArray->m_element_size);
3392 0 ▶ if (pArray->m_capacity >= min_new_capacity)
Branch: 1 branch
BranchTrueFalseTotal
if (pArray->m_capacity >= min_new_capacity) 0 0 0
3393 0 return MZ_TRUE;
3394 0 ▶ if (growing)
Branch: 1 branch
BranchTrueFalseTotal
if (growing) 0 0 0
3395 {
3396 0 ▶ new_capacity = MZ_MAX(1, pArray->m_capacity);
Branch: 1 branch
BranchTrueFalseTotal
new_capacity = MZ_MAX(1, pArray->m_capacity); 0 0 0
3397 0 ▶ while (new_capacity < min_new_capacity)
Branch: 1 branch
BranchTrueFalseTotal
while (new_capacity < min_new_capacity) 0 0 0
3398 0 new_capacity *= 2;
3399 }
3400 0 ▶ if (NULL == (pNew_p = pZip->m_pRealloc(pZip->m_pAlloc_opaque, pArray->m_p, pArray->m_element_size, new_capacity)))
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == (pNew_p = pZip->m_pRealloc(pZip->m_pAlloc_opaque, pArray->m_p, pArray->m_element_size, new_capacity))) 0 0 0
3401 0 return MZ_FALSE;
3402 0 pArray->m_p = pNew_p;
3403 0 pArray->m_capacity = new_capacity;
3404 0 return MZ_TRUE;
3405 }
3406  
3407 static MZ_FORCEINLINE mz_bool mz_zip_array_reserve(mz_zip_archive *pZip, mz_zip_array *pArray, size_t new_capacity, mz_uint growing)
3408 {
3409 0 if (new_capacity > pArray->m_capacity)
3410 {
3411 0 ▶ if (!mz_zip_array_ensure_capacity(pZip, pArray, new_capacity, growing))
Branch: 3 branches
BranchTrueFalseTotal
if (!mz_zip_array_ensure_capacity(pZip, pArray, new_capacity, growing)) 0 0 0
if (!mz_zip_array_ensure_capacity(pZip, pArray, new_capacity, growing)) 0 0 0
if (!mz_zip_array_ensure_capacity(pZip, pArray, new_capacity, growing)) 0 0 0
3412 0 return MZ_FALSE;
3413 }
3414 0 return MZ_TRUE;
3415 }
3416  
3417 static MZ_FORCEINLINE mz_bool mz_zip_array_resize(mz_zip_archive *pZip, mz_zip_array *pArray, size_t new_size, mz_uint growing)
3418 {
3419 0 ▶ if (new_size > pArray->m_capacity)
Branch: 1 branch
BranchTrueFalseTotal
if (new_size > pArray->m_capacity) 0 0 0
3420 {
3421 0 ▶ if (!mz_zip_array_ensure_capacity(pZip, pArray, new_size, growing))
Branch: 28 branches
BranchTrueFalseTotal
if (!mz_zip_array_ensure_capacity(pZip, pArray, new_size, growing)) 0 0 0
if (!mz_zip_array_ensure_capacity(pZip, pArray, new_size, growing)) 0 0 0
if (!mz_zip_array_ensure_capacity(pZip, pArray, new_size, growing)) 0 0 0
if (!mz_zip_array_ensure_capacity(pZip, pArray, new_size, growing)) 0 0 0
if (!mz_zip_array_ensure_capacity(pZip, pArray, new_size, growing)) 0 0 0
if (!mz_zip_array_ensure_capacity(pZip, pArray, new_size, growing)) 0 0 0
if (!mz_zip_array_ensure_capacity(pZip, pArray, new_size, growing)) 0 0 0
if (!mz_zip_array_ensure_capacity(pZip, pArray, new_size, growing)) 0 0 0
if (!mz_zip_array_ensure_capacity(pZip, pArray, new_size, growing)) 0 0 0
if (!mz_zip_array_ensure_capacity(pZip, pArray, new_size, growing)) 0 0 0
if (!mz_zip_array_ensure_capacity(pZip, pArray, new_size, growing)) 0 0 0
if (!mz_zip_array_ensure_capacity(pZip, pArray, new_size, growing)) 0 0 0
if (!mz_zip_array_ensure_capacity(pZip, pArray, new_size, growing)) 0 0 0
if (!mz_zip_array_ensure_capacity(pZip, pArray, new_size, growing)) 0 0 0
if (!mz_zip_array_ensure_capacity(pZip, pArray, new_size, growing)) 0 0 0
if (!mz_zip_array_ensure_capacity(pZip, pArray, new_size, growing)) 0 0 0
if (!mz_zip_array_ensure_capacity(pZip, pArray, new_size, growing)) 0 0 0
if (!mz_zip_array_ensure_capacity(pZip, pArray, new_size, growing)) 0 0 0
if (!mz_zip_array_ensure_capacity(pZip, pArray, new_size, growing)) 0 0 0
if (!mz_zip_array_ensure_capacity(pZip, pArray, new_size, growing)) 0 0 0
if (!mz_zip_array_ensure_capacity(pZip, pArray, new_size, growing)) 0 0 0
if (!mz_zip_array_ensure_capacity(pZip, pArray, new_size, growing)) 0 0 0
if (!mz_zip_array_ensure_capacity(pZip, pArray, new_size, growing)) 0 0 0
if (!mz_zip_array_ensure_capacity(pZip, pArray, new_size, growing)) 0 0 0
if (!mz_zip_array_ensure_capacity(pZip, pArray, new_size, growing)) 0 0 0
if (!mz_zip_array_ensure_capacity(pZip, pArray, new_size, growing)) 0 0 0
if (!mz_zip_array_ensure_capacity(pZip, pArray, new_size, growing)) 0 0 0
if (!mz_zip_array_ensure_capacity(pZip, pArray, new_size, growing)) 0 0 0
3422 0 return MZ_FALSE;
3423 }
3424 0 pArray->m_size = new_size;
3425 0 return MZ_TRUE;
3426 }
3427  
3428 static MZ_FORCEINLINE mz_bool mz_zip_array_ensure_room(mz_zip_archive *pZip, mz_zip_array *pArray, size_t n)
3429 {
3430 0 return mz_zip_array_reserve(pZip, pArray, pArray->m_size + n, MZ_TRUE);
3431 }
3432  
3433 static MZ_FORCEINLINE mz_bool mz_zip_array_push_back(mz_zip_archive *pZip, mz_zip_array *pArray, const void *pElements, size_t n)
3434 {
3435 0 size_t orig_size = pArray->m_size;
3436 0 ▶ if (!mz_zip_array_resize(pZip, pArray, orig_size + n, MZ_TRUE))
Branch: 15 branches
BranchTrueFalseTotal
if (!mz_zip_array_resize(pZip, pArray, orig_size + n, MZ_TRUE)) 0 0 0
if (!mz_zip_array_resize(pZip, pArray, orig_size + n, MZ_TRUE)) 0 0 0
if (!mz_zip_array_resize(pZip, pArray, orig_size + n, MZ_TRUE)) 0 0 0
if (!mz_zip_array_resize(pZip, pArray, orig_size + n, MZ_TRUE)) 0 0 0
if (!mz_zip_array_resize(pZip, pArray, orig_size + n, MZ_TRUE)) 0 0 0
if (!mz_zip_array_resize(pZip, pArray, orig_size + n, MZ_TRUE)) 0 0 0
if (!mz_zip_array_resize(pZip, pArray, orig_size + n, MZ_TRUE)) 0 0 0
if (!mz_zip_array_resize(pZip, pArray, orig_size + n, MZ_TRUE)) 0 0 0
if (!mz_zip_array_resize(pZip, pArray, orig_size + n, MZ_TRUE)) 0 0 0
if (!mz_zip_array_resize(pZip, pArray, orig_size + n, MZ_TRUE)) 0 0 0
if (!mz_zip_array_resize(pZip, pArray, orig_size + n, MZ_TRUE)) 0 0 0
if (!mz_zip_array_resize(pZip, pArray, orig_size + n, MZ_TRUE)) 0 0 0
if (!mz_zip_array_resize(pZip, pArray, orig_size + n, MZ_TRUE)) 0 0 0
if (!mz_zip_array_resize(pZip, pArray, orig_size + n, MZ_TRUE)) 0 0 0
if (!mz_zip_array_resize(pZip, pArray, orig_size + n, MZ_TRUE)) 0 0 0
3437 0 return MZ_FALSE;
3438 0 ▶ if (n > 0)
Branch: 15 branches
BranchTrueFalseTotal
if (n > 0) 0 0 0
if (n > 0) 0 0 0
if (n > 0) 0 0 0
if (n > 0) 0 0 0
if (n > 0) 0 0 0
if (n > 0) 0 0 0
if (n > 0) 0 0 0
if (n > 0) 0 0 0
if (n > 0) 0 0 0
if (n > 0) 0 0 0
if (n > 0) 0 0 0
if (n > 0) 0 0 0
if (n > 0) 0 0 0
if (n > 0) 0 0 0
if (n > 0) 0 0 0
3439 0 memcpy((mz_uint8 *)pArray->m_p + orig_size * pArray->m_element_size, pElements, n * pArray->m_element_size);
3440 0 return MZ_TRUE;
3441 }
3442  
3443 #ifndef MINIZ_NO_TIME
3444 0 static MZ_TIME_T mz_zip_dos_to_time_t(int dos_time, int dos_date)
3445 {
3446 struct tm tm;
3447 0 memset(&tm, 0, sizeof(tm));
3448 0 tm.tm_isdst = -1;
3449 0 tm.tm_year = ((dos_date >> 9) & 127) + 1980 - 1900;
3450 0 tm.tm_mon = ((dos_date >> 5) & 15) - 1;
3451 0 tm.tm_mday = dos_date & 31;
3452 0 tm.tm_hour = (dos_time >> 11) & 31;
3453 0 tm.tm_min = (dos_time >> 5) & 63;
3454 0 tm.tm_sec = (dos_time << 1) & 62;
3455 0 return mktime(&tm);
3456 }
3457  
3458 #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS
3459 0 static void mz_zip_time_t_to_dos_time(MZ_TIME_T time, mz_uint16 *pDOS_time, mz_uint16 *pDOS_date)
3460 {
3461 #ifdef _MSC_VER
3462 struct tm tm_struct;
3463 struct tm *tm = &tm_struct;
3464 errno_t err = localtime_s(tm, &time);
3465 if (err)
3466 {
3467 *pDOS_date = 0;
3468 *pDOS_time = 0;
3469 return;
3470 }
3471 #else
3472 0 struct tm *tm = localtime(&time);
3473 #endif /* #ifdef _MSC_VER */
3474  
3475 0 *pDOS_time = (mz_uint16)(((tm->tm_hour) << 11) + ((tm->tm_min) << 5) + ((tm->tm_sec) >> 1));
3476 0 *pDOS_date = (mz_uint16)(((tm->tm_year + 1900 - 1980) << 9) + ((tm->tm_mon + 1) << 5) + tm->tm_mday);
3477 0 }
3478 #endif /* MINIZ_NO_ARCHIVE_WRITING_APIS */
3479  
3480 #ifndef MINIZ_NO_STDIO
3481 #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS
3482 static mz_bool mz_zip_get_file_modified_time(const char *pFilename, MZ_TIME_T *pTime)
3483 {
3484 struct MZ_FILE_STAT_STRUCT file_stat;
3485  
3486 /* On Linux with x86 glibc, this call will fail on large files (I think >= 0x80000000 bytes) unless you compiled with _LARGEFILE64_SOURCE. Argh. */
3487 if (MZ_FILE_STAT(pFilename, &file_stat) != 0)
3488 return MZ_FALSE;
3489  
3490 *pTime = file_stat.st_mtime;
3491  
3492 return MZ_TRUE;
3493 }
3494 #endif /* #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS*/
3495  
3496 static mz_bool mz_zip_set_file_times(const char *pFilename, MZ_TIME_T access_time, MZ_TIME_T modified_time)
3497 {
3498 struct utimbuf t;
3499  
3500 memset(&t, 0, sizeof(t));
3501 t.actime = access_time;
3502 t.modtime = modified_time;
3503  
3504 return !utime(pFilename, &t);
3505 }
3506 #endif /* #ifndef MINIZ_NO_STDIO */
3507 #endif /* #ifndef MINIZ_NO_TIME */
3508  
3509 static MZ_FORCEINLINE mz_bool mz_zip_set_error(mz_zip_archive *pZip, mz_zip_error err_num)
3510 {
3511 0 ▶ if (pZip)
Branch: 210 branches
BranchTrueFalseTotal
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
if (pZip) 0 0 0
3512 0 pZip->m_last_error = err_num;
3513 0 return MZ_FALSE;
3514 }
3515  
3516 0 static mz_bool mz_zip_reader_init_internal(mz_zip_archive *pZip, mz_uint flags)
3517 {
3518 (void)flags;
3519 0 ▶ if ((!pZip) || (pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_INVALID))
Branch: 3 branches
BranchTrueFalseTotal
if ((!pZip) || (pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_INVALID)) 0 0 0
if ((!pZip) || (pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_INVALID)) 0 0 0
if ((!pZip) || (pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_INVALID)) 0 0 0
3520 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
3521  
3522 0 ▶ if (!pZip->m_pAlloc)
Branch: 1 branch
BranchTrueFalseTotal
if (!pZip->m_pAlloc) 0 0 0
3523 0 pZip->m_pAlloc = miniz_def_alloc_func;
3524 0 ▶ if (!pZip->m_pFree)
Branch: 1 branch
BranchTrueFalseTotal
if (!pZip->m_pFree) 0 0 0
3525 0 pZip->m_pFree = miniz_def_free_func;
3526 0 ▶ if (!pZip->m_pRealloc)
Branch: 1 branch
BranchTrueFalseTotal
if (!pZip->m_pRealloc) 0 0 0
3527 0 pZip->m_pRealloc = miniz_def_realloc_func;
3528  
3529 0 pZip->m_archive_size = 0;
3530 0 pZip->m_central_directory_file_ofs = 0;
3531 0 pZip->m_total_files = 0;
3532 0 pZip->m_last_error = MZ_ZIP_NO_ERROR;
3533  
3534 0 ▶ if (NULL == (pZip->m_pState = (mz_zip_internal_state *)pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, sizeof(mz_zip_internal_state))))
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == (pZip->m_pState = (mz_zip_internal_state *)pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, sizeof(mz_zip_internal_state)))) 0 0 0
3535 0 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
3536  
3537 0 memset(pZip->m_pState, 0, sizeof(mz_zip_internal_state));
3538 0 MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_central_dir, sizeof(mz_uint8));
3539 0 MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_central_dir_offsets, sizeof(mz_uint32));
3540 0 MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_sorted_central_dir_offsets, sizeof(mz_uint32));
3541 0 pZip->m_pState->m_init_flags = flags;
3542 0 pZip->m_pState->m_zip64 = MZ_FALSE;
3543 0 pZip->m_pState->m_zip64_has_extended_info_fields = MZ_FALSE;
3544  
3545 0 pZip->m_zip_mode = MZ_ZIP_MODE_READING;
3546  
3547 0 return MZ_TRUE;
3548 }
3549  
3550 static MZ_FORCEINLINE mz_bool mz_zip_reader_filename_less(const mz_zip_array *pCentral_dir_array, const mz_zip_array *pCentral_dir_offsets, mz_uint l_index, mz_uint r_index)
3551 {
3552 0 const mz_uint8 *pL = &MZ_ZIP_ARRAY_ELEMENT(pCentral_dir_array, mz_uint8, MZ_ZIP_ARRAY_ELEMENT(pCentral_dir_offsets, mz_uint32, l_index)), *pE;
3553 0 const mz_uint8 *pR = &MZ_ZIP_ARRAY_ELEMENT(pCentral_dir_array, mz_uint8, MZ_ZIP_ARRAY_ELEMENT(pCentral_dir_offsets, mz_uint32, r_index));
3554 0 mz_uint l_len = MZ_READ_LE16(pL + MZ_ZIP_CDH_FILENAME_LEN_OFS), r_len = MZ_READ_LE16(pR + MZ_ZIP_CDH_FILENAME_LEN_OFS);
3555 0 mz_uint8 l = 0, r = 0;
3556 0 pL += MZ_ZIP_CENTRAL_DIR_HEADER_SIZE;
3557 0 pR += MZ_ZIP_CENTRAL_DIR_HEADER_SIZE;
3558 0 pE = pL + MZ_MIN(l_len, r_len);
3559 0 ▶ while (pL < pE)
Branch: 4 branches
BranchTrueFalseTotal
while (pL < pE) 0 0 0
while (pL < pE) 0 0 0
while (pL < pE) 0 0 0
while (pL < pE) 0 0 0
3560 {
3561 0 ▶ if ((l = MZ_TOLOWER(*pL)) != (r = MZ_TOLOWER(*pR)))
Branch: 20 branches
BranchTrueFalseTotal
if ((l = MZ_TOLOWER(*pL)) != (r = MZ_TOLOWER(*pR))) 0 0 0
if ((l = MZ_TOLOWER(*pL)) != (r = MZ_TOLOWER(*pR))) 0 0 0
if ((l = MZ_TOLOWER(*pL)) != (r = MZ_TOLOWER(*pR))) 0 0 0
if ((l = MZ_TOLOWER(*pL)) != (r = MZ_TOLOWER(*pR))) 0 0 0
if ((l = MZ_TOLOWER(*pL)) != (r = MZ_TOLOWER(*pR))) 0 0 0
if ((l = MZ_TOLOWER(*pL)) != (r = MZ_TOLOWER(*pR))) 0 0 0
if ((l = MZ_TOLOWER(*pL)) != (r = MZ_TOLOWER(*pR))) 0 0 0
if ((l = MZ_TOLOWER(*pL)) != (r = MZ_TOLOWER(*pR))) 0 0 0
if ((l = MZ_TOLOWER(*pL)) != (r = MZ_TOLOWER(*pR))) 0 0 0
if ((l = MZ_TOLOWER(*pL)) != (r = MZ_TOLOWER(*pR))) 0 0 0
if ((l = MZ_TOLOWER(*pL)) != (r = MZ_TOLOWER(*pR))) 0 0 0
if ((l = MZ_TOLOWER(*pL)) != (r = MZ_TOLOWER(*pR))) 0 0 0
if ((l = MZ_TOLOWER(*pL)) != (r = MZ_TOLOWER(*pR))) 0 0 0
if ((l = MZ_TOLOWER(*pL)) != (r = MZ_TOLOWER(*pR))) 0 0 0
if ((l = MZ_TOLOWER(*pL)) != (r = MZ_TOLOWER(*pR))) 0 0 0
if ((l = MZ_TOLOWER(*pL)) != (r = MZ_TOLOWER(*pR))) 0 0 0
if ((l = MZ_TOLOWER(*pL)) != (r = MZ_TOLOWER(*pR))) 0 0 0
if ((l = MZ_TOLOWER(*pL)) != (r = MZ_TOLOWER(*pR))) 0 0 0
if ((l = MZ_TOLOWER(*pL)) != (r = MZ_TOLOWER(*pR))) 0 0 0
if ((l = MZ_TOLOWER(*pL)) != (r = MZ_TOLOWER(*pR))) 0 0 0
3562 0 break;
3563 0 pL++;
3564 0 pR++;
3565 }
3566 0 ▶ return (pL == pE) ? (l_len < r_len) : (l < r);
Branch: 4 branches
BranchTrueFalseTotal
return (pL == pE) ? (l_len < r_len) : (l < r); 0 0 0
return (pL == pE) ? (l_len < r_len) : (l < r); 0 0 0
return (pL == pE) ? (l_len < r_len) : (l < r); 0 0 0
return (pL == pE) ? (l_len < r_len) : (l < r); 0 0 0
3567 }
3568  
3569 #define MZ_SWAP_UINT32(a, b) \
3570 do \
3571 { \
3572 mz_uint32 t = a; \
3573 a = b; \
3574 b = t; \
3575 } \
3576 MZ_MACRO_END
3577  
3578 /* Heap sort of lowercased filenames, used to help accelerate plain central directory searches by mz_zip_reader_locate_file(). (Could also use qsort(), but it could allocate memory.) */
3579 0 static void mz_zip_reader_sort_central_dir_offsets_by_filename(mz_zip_archive *pZip)
3580 {
3581 0 mz_zip_internal_state *pState = pZip->m_pState;
3582 0 const mz_zip_array *pCentral_dir_offsets = &pState->m_central_dir_offsets;
3583 0 const mz_zip_array *pCentral_dir = &pState->m_central_dir;
3584 mz_uint32 *pIndices;
3585 mz_uint32 start, end;
3586 0 const mz_uint32 size = pZip->m_total_files;
3587  
3588 0 ▶ if (size <= 1U)
Branch: 1 branch
BranchTrueFalseTotal
if (size <= 1U) 0 0 0
3589 0 return;
3590  
3591 0 pIndices = &MZ_ZIP_ARRAY_ELEMENT(&pState->m_sorted_central_dir_offsets, mz_uint32, 0);
3592  
3593 0 start = (size - 2U) >> 1U;
3594 for (;;)
3595 0 {
3596 0 mz_uint64 child, root = start;
3597 for (;;)
3598 {
3599 0 ▶ if ((child = (root << 1U) + 1U) >= size)
Branch: 1 branch
BranchTrueFalseTotal
if ((child = (root << 1U) + 1U) >= size) 0 0 0
3600 0 break;
3601 0 ▶ child += (((child + 1U) < size) && (mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, pIndices[child], pIndices[child + 1U])));
Branch: 2 branches
BranchTrueFalseTotal
child += (((child + 1U) < size) && (mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, pIndices[child], pIndices[child + 1U]))); 0 0 0
child += (((child + 1U) < size) && (mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, pIndices[child], pIndices[child + 1U]))); 0 0 0
3602 0 ▶ if (!mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, pIndices[root], pIndices[child]))
Branch: 1 branch
BranchTrueFalseTotal
if (!mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, pIndices[root], pIndices[child])) 0 0 0
3603 0 break;
3604 0 MZ_SWAP_UINT32(pIndices[root], pIndices[child]);
3605 0 root = child;
3606 }
3607 0 ▶ if (!start)
Branch: 1 branch
BranchTrueFalseTotal
if (!start) 0 0 0
3608 0 break;
3609 0 start--;
3610 }
3611  
3612 0 end = size - 1;
3613 0 ▶ while (end > 0)
Branch: 1 branch
BranchTrueFalseTotal
while (end > 0) 0 0 0
3614 {
3615 0 mz_uint64 child, root = 0;
3616 0 MZ_SWAP_UINT32(pIndices[end], pIndices[0]);
3617 for (;;)
3618 {
3619 0 ▶ if ((child = (root << 1U) + 1U) >= end)
Branch: 1 branch
BranchTrueFalseTotal
if ((child = (root << 1U) + 1U) >= end) 0 0 0
3620 0 break;
3621 0 ▶ child += (((child + 1U) < end) && mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, pIndices[child], pIndices[child + 1U]));
Branch: 2 branches
BranchTrueFalseTotal
child += (((child + 1U) < end) && mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, pIndices[child], pIndices[child + 1U])); 0 0 0
child += (((child + 1U) < end) && mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, pIndices[child], pIndices[child + 1U])); 0 0 0
3622 0 ▶ if (!mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, pIndices[root], pIndices[child]))
Branch: 1 branch
BranchTrueFalseTotal
if (!mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, pIndices[root], pIndices[child])) 0 0 0
3623 0 break;
3624 0 MZ_SWAP_UINT32(pIndices[root], pIndices[child]);
3625 0 root = child;
3626 }
3627 0 end--;
3628 }
3629 }
3630  
3631 0 static mz_bool mz_zip_reader_locate_header_sig(mz_zip_archive *pZip, mz_uint32 record_sig, mz_uint32 record_size, mz_int64 *pOfs)
3632 {
3633 mz_int64 cur_file_ofs;
3634 mz_uint32 buf_u32[4096 / sizeof(mz_uint32)];
3635 0 mz_uint8 *pBuf = (mz_uint8 *)buf_u32;
3636  
3637 /* Basic sanity checks - reject files which are too small */
3638 0 ▶ if (pZip->m_archive_size < record_size)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_archive_size < record_size) 0 0 0
3639 0 return MZ_FALSE;
3640  
3641 /* Find the record by scanning the file from the end towards the beginning. */
3642 0 cur_file_ofs = MZ_MAX((mz_int64)pZip->m_archive_size - (mz_int64)sizeof(buf_u32), 0);
3643 for (;;)
3644 0 {
3645 0 ▶ int i, n = (int)MZ_MIN(sizeof(buf_u32), pZip->m_archive_size - cur_file_ofs);
Branch: 1 branch
BranchTrueFalseTotal
int i, n = (int)MZ_MIN(sizeof(buf_u32), pZip->m_archive_size - cur_file_ofs); 0 0 0
3646  
3647 0 ▶ if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pBuf, n) != (mz_uint)n)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pBuf, n) != (mz_uint)n) 0 0 0
3648 0 return MZ_FALSE;
3649  
3650 0 ▶ for (i = n - 4; i >= 0; --i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = n - 4; i >= 0; --i) 0 0 0
3651 {
3652 0 mz_uint s = MZ_READ_LE32(pBuf + i);
3653 0 ▶ if (s == record_sig)
Branch: 1 branch
BranchTrueFalseTotal
if (s == record_sig) 0 0 0
3654 {
3655 0 ▶ if ((pZip->m_archive_size - (cur_file_ofs + i)) >= record_size)
Branch: 1 branch
BranchTrueFalseTotal
if ((pZip->m_archive_size - (cur_file_ofs + i)) >= record_size) 0 0 0
3656 0 break;
3657 }
3658 }
3659  
3660 0 ▶ if (i >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (i >= 0) 0 0 0
3661 {
3662 0 cur_file_ofs += i;
3663 0 break;
3664 }
3665  
3666 /* Give up if we've searched the entire file, or we've gone back "too far" (~64kb) */
3667 0 ▶ if ((!cur_file_ofs) || ((pZip->m_archive_size - cur_file_ofs) >= ((mz_uint64)(MZ_UINT16_MAX) + record_size)))
Branch: 2 branches
BranchTrueFalseTotal
if ((!cur_file_ofs) || ((pZip->m_archive_size - cur_file_ofs) >= ((mz_uint64)(MZ_UINT16_MAX) + record_size))) 0 0 0
if ((!cur_file_ofs) || ((pZip->m_archive_size - cur_file_ofs) >= ((mz_uint64)(MZ_UINT16_MAX) + record_size))) 0 0 0
3668 0 return MZ_FALSE;
3669  
3670 0 ▶ cur_file_ofs = MZ_MAX(cur_file_ofs - (sizeof(buf_u32) - 3), 0);
Branch: 1 branch
BranchTrueFalseTotal
cur_file_ofs = MZ_MAX(cur_file_ofs - (sizeof(buf_u32) - 3), 0); 0 0 0
3671 }
3672  
3673 0 *pOfs = cur_file_ofs;
3674 0 return MZ_TRUE;
3675 }
3676  
3677 0 static mz_bool mz_zip_reader_eocd64_valid(mz_zip_archive *pZip, uint64_t offset, uint8_t *buf)
3678 {
3679 0 ▶ if (pZip->m_pRead(pZip->m_pIO_opaque, offset, buf, MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE) == MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pRead(pZip->m_pIO_opaque, offset, buf, MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE) == MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE) 0 0 0
3680 {
3681 0 ▶ if (MZ_READ_LE32(buf + MZ_ZIP64_ECDH_SIG_OFS) == MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIG)
Branch: 1 branch
BranchTrueFalseTotal
if (MZ_READ_LE32(buf + MZ_ZIP64_ECDH_SIG_OFS) == MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIG) 0 0 0
3682 {
3683 0 return MZ_TRUE;
3684 }
3685 }
3686  
3687 0 return MZ_FALSE;
3688 }
3689  
3690 0 static mz_bool mz_zip_reader_read_central_dir(mz_zip_archive *pZip, mz_uint flags)
3691 {
3692 0 mz_uint cdir_size = 0, cdir_entries_on_this_disk = 0, num_this_disk = 0, cdir_disk_index = 0;
3693 0 mz_uint64 cdir_ofs = 0, eocd_ofs = 0, archive_ofs = 0;
3694 0 mz_int64 cur_file_ofs = 0;
3695 const mz_uint8 *p;
3696  
3697 mz_uint32 buf_u32[4096 / sizeof(mz_uint32)];
3698 0 mz_uint8 *pBuf = (mz_uint8 *)buf_u32;
3699 0 mz_bool sort_central_dir = ((flags & MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY) == 0);
3700 mz_uint32 zip64_end_of_central_dir_locator_u32[(MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE + sizeof(mz_uint32) - 1) / sizeof(mz_uint32)];
3701 0 mz_uint8 *pZip64_locator = (mz_uint8 *)zip64_end_of_central_dir_locator_u32;
3702  
3703 mz_uint32 zip64_end_of_central_dir_header_u32[(MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE + sizeof(mz_uint32) - 1) / sizeof(mz_uint32)];
3704 0 mz_uint8 *pZip64_end_of_central_dir = (mz_uint8 *)zip64_end_of_central_dir_header_u32;
3705  
3706 0 mz_uint64 zip64_end_of_central_dir_ofs = 0;
3707  
3708 /* Basic sanity checks - reject files which are too small, and check the first 4 bytes of the file to make sure a local header is there. */
3709 0 ▶ if (pZip->m_archive_size < MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_archive_size < MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) 0 0 0
3710 0 return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE);
3711  
3712 0 ▶ if (!mz_zip_reader_locate_header_sig(pZip, MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIG, MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE, &cur_file_ofs))
Branch: 1 branch
BranchTrueFalseTotal
if (!mz_zip_reader_locate_header_sig(pZip, MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIG, MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE, &cur_file_ofs)) 0 0 0
3713 0 return mz_zip_set_error(pZip, MZ_ZIP_FAILED_FINDING_CENTRAL_DIR);
3714  
3715 0 eocd_ofs = cur_file_ofs;
3716 /* Read and verify the end of central directory record. */
3717 0 ▶ if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pBuf, MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) != MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pBuf, MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) != MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) 0 0 0
3718 0 return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
3719  
3720 0 ▶ if (MZ_READ_LE32(pBuf + MZ_ZIP_ECDH_SIG_OFS) != MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIG)
Branch: 1 branch
BranchTrueFalseTotal
if (MZ_READ_LE32(pBuf + MZ_ZIP_ECDH_SIG_OFS) != MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIG) 0 0 0
3721 0 return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE);
3722  
3723 0 ▶ if (cur_file_ofs >= (MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE + MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE))
Branch: 1 branch
BranchTrueFalseTotal
if (cur_file_ofs >= (MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE + MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE)) 0 0 0
3724 {
3725 0 ▶ if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs - MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE, pZip64_locator, MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE) == MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs - MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE, pZip64_locator, MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE) == MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE) 0 0 0
3726 {
3727 0 ▶ if (MZ_READ_LE32(pZip64_locator + MZ_ZIP64_ECDL_SIG_OFS) == MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIG)
Branch: 1 branch
BranchTrueFalseTotal
if (MZ_READ_LE32(pZip64_locator + MZ_ZIP64_ECDL_SIG_OFS) == MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIG) 0 0 0
3728 {
3729 0 pZip->m_pState->m_zip64 = MZ_TRUE;
3730 }
3731 }
3732 }
3733  
3734 0 ▶ if (pZip->m_pState->m_zip64)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pState->m_zip64) 0 0 0
3735 {
3736 /* Try locating the EOCD64 right before the EOCD64 locator. This works even
3737 * when the effective start of the zip header is not yet known. */
3738 0 ▶ if (cur_file_ofs < MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE +
Branch: 1 branch
BranchTrueFalseTotal
if (cur_file_ofs < MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE + 0 0 0
3739 MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE)
3740 0 return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE);
3741  
3742 0 zip64_end_of_central_dir_ofs = cur_file_ofs -
3743 0 MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE -
3744 MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE;
3745  
3746 0 ▶ if (!mz_zip_reader_eocd64_valid(pZip, zip64_end_of_central_dir_ofs,
Branch: 1 branch
BranchTrueFalseTotal
if (!mz_zip_reader_eocd64_valid(pZip, zip64_end_of_central_dir_ofs, 0 0 0
3747 pZip64_end_of_central_dir))
3748 {
3749 /* That failed, try reading where the locator tells us to. */
3750 0 zip64_end_of_central_dir_ofs = MZ_READ_LE64(
3751 pZip64_locator + MZ_ZIP64_ECDL_REL_OFS_TO_ZIP64_ECDR_OFS);
3752  
3753 0 if (zip64_end_of_central_dir_ofs >
3754 0 ▶ (pZip->m_archive_size - MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE))
Branch: 1 branch
BranchTrueFalseTotal
(pZip->m_archive_size - MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE)) 0 0 0
3755 0 return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE);
3756  
3757 0 ▶ if (!mz_zip_reader_eocd64_valid(pZip, zip64_end_of_central_dir_ofs,
Branch: 1 branch
BranchTrueFalseTotal
if (!mz_zip_reader_eocd64_valid(pZip, zip64_end_of_central_dir_ofs, 0 0 0
3758 pZip64_end_of_central_dir))
3759 0 return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE);
3760 }
3761 }
3762  
3763 0 pZip->m_total_files = MZ_READ_LE16(pBuf + MZ_ZIP_ECDH_CDIR_TOTAL_ENTRIES_OFS);
3764 0 cdir_entries_on_this_disk = MZ_READ_LE16(pBuf + MZ_ZIP_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS);
3765 0 num_this_disk = MZ_READ_LE16(pBuf + MZ_ZIP_ECDH_NUM_THIS_DISK_OFS);
3766 0 cdir_disk_index = MZ_READ_LE16(pBuf + MZ_ZIP_ECDH_NUM_DISK_CDIR_OFS);
3767 0 cdir_size = MZ_READ_LE32(pBuf + MZ_ZIP_ECDH_CDIR_SIZE_OFS);
3768 0 cdir_ofs = MZ_READ_LE32(pBuf + MZ_ZIP_ECDH_CDIR_OFS_OFS);
3769  
3770 0 ▶ if (pZip->m_pState->m_zip64)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pState->m_zip64) 0 0 0
3771 {
3772 0 mz_uint32 zip64_total_num_of_disks = MZ_READ_LE32(pZip64_locator + MZ_ZIP64_ECDL_TOTAL_NUMBER_OF_DISKS_OFS);
3773 0 mz_uint64 zip64_cdir_total_entries = MZ_READ_LE64(pZip64_end_of_central_dir + MZ_ZIP64_ECDH_CDIR_TOTAL_ENTRIES_OFS);
3774 0 mz_uint64 zip64_cdir_total_entries_on_this_disk = MZ_READ_LE64(pZip64_end_of_central_dir + MZ_ZIP64_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS);
3775 0 mz_uint64 zip64_size_of_end_of_central_dir_record = MZ_READ_LE64(pZip64_end_of_central_dir + MZ_ZIP64_ECDH_SIZE_OF_RECORD_OFS);
3776 0 mz_uint64 zip64_size_of_central_directory = MZ_READ_LE64(pZip64_end_of_central_dir + MZ_ZIP64_ECDH_CDIR_SIZE_OFS);
3777  
3778 0 ▶ if (zip64_size_of_end_of_central_dir_record < (MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE - 12))
Branch: 1 branch
BranchTrueFalseTotal
if (zip64_size_of_end_of_central_dir_record < (MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE - 12)) 0 0 0
3779 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
3780  
3781 0 ▶ if (zip64_total_num_of_disks != 1U)
Branch: 1 branch
BranchTrueFalseTotal
if (zip64_total_num_of_disks != 1U) 0 0 0
3782 0 return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_MULTIDISK);
3783  
3784 /* Check for miniz's practical limits */
3785 0 ▶ if (zip64_cdir_total_entries > MZ_UINT32_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (zip64_cdir_total_entries > MZ_UINT32_MAX) 0 0 0
3786 0 return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);
3787  
3788 0 pZip->m_total_files = (mz_uint32)zip64_cdir_total_entries;
3789  
3790 0 ▶ if (zip64_cdir_total_entries_on_this_disk > MZ_UINT32_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (zip64_cdir_total_entries_on_this_disk > MZ_UINT32_MAX) 0 0 0
3791 0 return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);
3792  
3793 0 cdir_entries_on_this_disk = (mz_uint32)zip64_cdir_total_entries_on_this_disk;
3794  
3795 /* Check for miniz's current practical limits (sorry, this should be enough for millions of files) */
3796 0 ▶ if (zip64_size_of_central_directory > MZ_UINT32_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (zip64_size_of_central_directory > MZ_UINT32_MAX) 0 0 0
3797 0 return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_CDIR_SIZE);
3798  
3799 0 cdir_size = (mz_uint32)zip64_size_of_central_directory;
3800  
3801 0 num_this_disk = MZ_READ_LE32(pZip64_end_of_central_dir + MZ_ZIP64_ECDH_NUM_THIS_DISK_OFS);
3802  
3803 0 cdir_disk_index = MZ_READ_LE32(pZip64_end_of_central_dir + MZ_ZIP64_ECDH_NUM_DISK_CDIR_OFS);
3804  
3805 0 cdir_ofs = MZ_READ_LE64(pZip64_end_of_central_dir + MZ_ZIP64_ECDH_CDIR_OFS_OFS);
3806 }
3807  
3808 0 ▶ if (pZip->m_total_files != cdir_entries_on_this_disk)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_total_files != cdir_entries_on_this_disk) 0 0 0
3809 0 return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_MULTIDISK);
3810  
3811 0 ▶ if (((num_this_disk | cdir_disk_index) != 0) && ((num_this_disk != 1) || (cdir_disk_index != 1)))
Branch: 3 branches
BranchTrueFalseTotal
if (((num_this_disk | cdir_disk_index) != 0) && ((num_this_disk != 1) || (cdir_disk_index != 1))) 0 0 0
if (((num_this_disk | cdir_disk_index) != 0) && ((num_this_disk != 1) || (cdir_disk_index != 1))) 0 0 0
if (((num_this_disk | cdir_disk_index) != 0) && ((num_this_disk != 1) || (cdir_disk_index != 1))) 0 0 0
3812 0 return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_MULTIDISK);
3813  
3814 0 ▶ if (cdir_size < (mz_uint64)pZip->m_total_files * MZ_ZIP_CENTRAL_DIR_HEADER_SIZE)
Branch: 1 branch
BranchTrueFalseTotal
if (cdir_size < (mz_uint64)pZip->m_total_files * MZ_ZIP_CENTRAL_DIR_HEADER_SIZE) 0 0 0
3815 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
3816  
3817 0 ▶ if (cdir_size> pZip->m_archive_size || cdir_ofs > pZip->m_archive_size - cdir_size)
Branch: 2 branches
BranchTrueFalseTotal
if (cdir_size> pZip->m_archive_size || cdir_ofs > pZip->m_archive_size - cdir_size) 0 0 0
if (cdir_size> pZip->m_archive_size || cdir_ofs > pZip->m_archive_size - cdir_size) 0 0 0
3818 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
3819  
3820 0 ▶ if (eocd_ofs < cdir_ofs + cdir_size)
Branch: 1 branch
BranchTrueFalseTotal
if (eocd_ofs < cdir_ofs + cdir_size) 0 0 0
3821 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
3822  
3823 /* The end of central dir follows the central dir, unless the zip file has
3824 * some trailing data (e.g. it is appended to an executable file). */
3825 0 archive_ofs = eocd_ofs - (cdir_ofs + cdir_size);
3826 0 ▶ if (pZip->m_pState->m_zip64)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pState->m_zip64) 0 0 0
3827 {
3828 0 ▶ if (archive_ofs < MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE +
Branch: 1 branch
BranchTrueFalseTotal
if (archive_ofs < MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE + 0 0 0
3829 MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE)
3830 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
3831  
3832 0 archive_ofs -= MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE +
3833 MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE;
3834 }
3835  
3836 /* Update the archive start position, but only if not specified. */
3837 0 ▶ if ((pZip->m_zip_type == MZ_ZIP_TYPE_FILE || pZip->m_zip_type == MZ_ZIP_TYPE_CFILE ||
Branch: 2 branches
BranchTrueFalseTotal
if ((pZip->m_zip_type == MZ_ZIP_TYPE_FILE || pZip->m_zip_type == MZ_ZIP_TYPE_CFILE || 0 0 0
if ((pZip->m_zip_type == MZ_ZIP_TYPE_FILE || pZip->m_zip_type == MZ_ZIP_TYPE_CFILE || 0 0 0
3838 0 ▶ pZip->m_zip_type == MZ_ZIP_TYPE_USER) && pZip->m_pState->m_file_archive_start_ofs == 0)
Branch: 2 branches
BranchTrueFalseTotal
pZip->m_zip_type == MZ_ZIP_TYPE_USER) && pZip->m_pState->m_file_archive_start_ofs == 0) 0 0 0
pZip->m_zip_type == MZ_ZIP_TYPE_USER) && pZip->m_pState->m_file_archive_start_ofs == 0) 0 0 0
3839 {
3840 0 pZip->m_pState->m_file_archive_start_ofs = archive_ofs;
3841 0 pZip->m_archive_size -= archive_ofs;
3842 }
3843  
3844 0 pZip->m_central_directory_file_ofs = cdir_ofs;
3845  
3846 0 ▶ if (pZip->m_total_files)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_total_files) 0 0 0
3847 {
3848 mz_uint i, n;
3849 /* Read the entire central directory into a heap block, and allocate another heap block to hold the unsorted central dir file record offsets, and possibly another to hold the sorted indices. */
3850 0 ▶ if ((!mz_zip_array_resize(pZip, &pZip->m_pState->m_central_dir, cdir_size, MZ_FALSE)) ||
Branch: 2 branches
BranchTrueFalseTotal
if ((!mz_zip_array_resize(pZip, &pZip->m_pState->m_central_dir, cdir_size, MZ_FALSE)) || 0 0 0
if ((!mz_zip_array_resize(pZip, &pZip->m_pState->m_central_dir, cdir_size, MZ_FALSE)) || 0 0 0
3851 0 ▶ (!mz_zip_array_resize(pZip, &pZip->m_pState->m_central_dir_offsets, pZip->m_total_files, MZ_FALSE)))
Branch: 2 branches
BranchTrueFalseTotal
(!mz_zip_array_resize(pZip, &pZip->m_pState->m_central_dir_offsets, pZip->m_total_files, MZ_FALSE))) 0 0 0
(!mz_zip_array_resize(pZip, &pZip->m_pState->m_central_dir_offsets, pZip->m_total_files, MZ_FALSE))) 0 0 0
3852 0 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
3853  
3854 0 ▶ if (sort_central_dir)
Branch: 1 branch
BranchTrueFalseTotal
if (sort_central_dir) 0 0 0
3855 {
3856 0 ▶ if (!mz_zip_array_resize(pZip, &pZip->m_pState->m_sorted_central_dir_offsets, pZip->m_total_files, MZ_FALSE))
Branch: 2 branches
BranchTrueFalseTotal
if (!mz_zip_array_resize(pZip, &pZip->m_pState->m_sorted_central_dir_offsets, pZip->m_total_files, MZ_FALSE)) 0 0 0
if (!mz_zip_array_resize(pZip, &pZip->m_pState->m_sorted_central_dir_offsets, pZip->m_total_files, MZ_FALSE)) 0 0 0
3857 0 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
3858 }
3859  
3860 0 ▶ if (pZip->m_pRead(pZip->m_pIO_opaque, cdir_ofs, pZip->m_pState->m_central_dir.m_p, cdir_size) != cdir_size)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pRead(pZip->m_pIO_opaque, cdir_ofs, pZip->m_pState->m_central_dir.m_p, cdir_size) != cdir_size) 0 0 0
3861 0 return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
3862  
3863 /* Now create an index into the central directory file records, do some basic sanity checking on each record */
3864 0 p = (const mz_uint8 *)pZip->m_pState->m_central_dir.m_p;
3865 0 ▶ for (n = cdir_size, i = 0; i < pZip->m_total_files; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (n = cdir_size, i = 0; i < pZip->m_total_files; ++i) 0 0 0
3866 {
3867 mz_uint total_header_size, disk_index, bit_flags, filename_size, ext_data_size;
3868 mz_uint64 comp_size, decomp_size, local_header_ofs;
3869  
3870 0 ▶ if ((n < MZ_ZIP_CENTRAL_DIR_HEADER_SIZE) || (MZ_READ_LE32(p) != MZ_ZIP_CENTRAL_DIR_HEADER_SIG))
Branch: 2 branches
BranchTrueFalseTotal
if ((n < MZ_ZIP_CENTRAL_DIR_HEADER_SIZE) || (MZ_READ_LE32(p) != MZ_ZIP_CENTRAL_DIR_HEADER_SIG)) 0 0 0
if ((n < MZ_ZIP_CENTRAL_DIR_HEADER_SIZE) || (MZ_READ_LE32(p) != MZ_ZIP_CENTRAL_DIR_HEADER_SIG)) 0 0 0
3871 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
3872  
3873 0 MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir_offsets, mz_uint32, i) = (mz_uint32)(p - (const mz_uint8 *)pZip->m_pState->m_central_dir.m_p);
3874  
3875 0 ▶ if (sort_central_dir)
Branch: 1 branch
BranchTrueFalseTotal
if (sort_central_dir) 0 0 0
3876 0 MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_sorted_central_dir_offsets, mz_uint32, i) = i;
3877  
3878 0 comp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS);
3879 0 decomp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS);
3880 0 local_header_ofs = MZ_READ_LE32(p + MZ_ZIP_CDH_LOCAL_HEADER_OFS);
3881 0 filename_size = MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS);
3882 0 ext_data_size = MZ_READ_LE16(p + MZ_ZIP_CDH_EXTRA_LEN_OFS);
3883  
3884 0 ▶ if ((!pZip->m_pState->m_zip64_has_extended_info_fields) &&
Branch: 2 branches
BranchTrueFalseTotal
if ((!pZip->m_pState->m_zip64_has_extended_info_fields) && 0 0 0
if ((!pZip->m_pState->m_zip64_has_extended_info_fields) && 0 0 0
3885 0 (ext_data_size) &&
3886 0 ▶ (MZ_MAX(MZ_MAX(comp_size, decomp_size), local_header_ofs) == MZ_UINT32_MAX))
Branch: 1 branch
BranchTrueFalseTotal
(MZ_MAX(MZ_MAX(comp_size, decomp_size), local_header_ofs) == MZ_UINT32_MAX)) 0 0 0
3887 {
3888 /* Attempt to find zip64 extended information field in the entry's extra data */
3889 0 mz_uint32 extra_size_remaining = ext_data_size;
3890  
3891 0 ▶ if (extra_size_remaining)
Branch: 1 branch
BranchTrueFalseTotal
if (extra_size_remaining) 0 0 0
3892 {
3893 const mz_uint8 *pExtra_data;
3894 0 void *buf = NULL;
3895  
3896 0 ▶ if (MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + filename_size + ext_data_size > n)
Branch: 1 branch
BranchTrueFalseTotal
if (MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + filename_size + ext_data_size > n) 0 0 0
3897 {
3898 0 buf = MZ_MALLOC(ext_data_size);
3899 0 ▶ if (buf == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (buf == NULL) 0 0 0
3900 0 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
3901  
3902 0 ▶ if (pZip->m_pRead(pZip->m_pIO_opaque, cdir_ofs + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + filename_size, buf, ext_data_size) != ext_data_size)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pRead(pZip->m_pIO_opaque, cdir_ofs + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + filename_size, buf, ext_data_size) != ext_data_size) 0 0 0
3903 {
3904 0 ▶ MZ_FREE(buf);
Branch: 1 branch
BranchTrueFalseTotal
MZ_FREE(buf); 0 0 0
3905 0 return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
3906 }
3907  
3908 0 pExtra_data = (mz_uint8 *)buf;
3909 }
3910 else
3911 {
3912 0 pExtra_data = p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + filename_size;
3913 }
3914  
3915 do
3916 {
3917 mz_uint32 field_id;
3918 mz_uint32 field_data_size;
3919  
3920 0 ▶ if (extra_size_remaining < (sizeof(mz_uint16) * 2))
Branch: 1 branch
BranchTrueFalseTotal
if (extra_size_remaining < (sizeof(mz_uint16) * 2)) 0 0 0
3921 {
3922 0 ▶ MZ_FREE(buf);
Branch: 1 branch
BranchTrueFalseTotal
MZ_FREE(buf); 0 0 0
3923 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
3924 }
3925  
3926 0 field_id = MZ_READ_LE16(pExtra_data);
3927 0 field_data_size = MZ_READ_LE16(pExtra_data + sizeof(mz_uint16));
3928  
3929 0 ▶ if ((field_data_size + sizeof(mz_uint16) * 2) > extra_size_remaining)
Branch: 1 branch
BranchTrueFalseTotal
if ((field_data_size + sizeof(mz_uint16) * 2) > extra_size_remaining) 0 0 0
3930 {
3931 0 ▶ MZ_FREE(buf);
Branch: 1 branch
BranchTrueFalseTotal
MZ_FREE(buf); 0 0 0
3932 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
3933 }
3934  
3935 0 ▶ if (field_id == MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID)
Branch: 1 branch
BranchTrueFalseTotal
if (field_id == MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID) 0 0 0
3936 {
3937 /* Ok, the archive didn't have any zip64 headers but it uses a zip64 extended information field so mark it as zip64 anyway (this can occur with infozip's zip util when it reads compresses files from stdin). */
3938 0 pZip->m_pState->m_zip64 = MZ_TRUE;
3939 0 pZip->m_pState->m_zip64_has_extended_info_fields = MZ_TRUE;
3940 0 break;
3941 }
3942  
3943 0 pExtra_data += sizeof(mz_uint16) * 2 + field_data_size;
3944 0 extra_size_remaining = extra_size_remaining - sizeof(mz_uint16) * 2 - field_data_size;
3945 0 ▶ } while (extra_size_remaining);
Branch: 1 branch
BranchTrueFalseTotal
} while (extra_size_remaining); 0 0 0
3946  
3947 0 MZ_FREE(buf);
3948 }
3949 }
3950  
3951 /* I've seen archives that aren't marked as zip64 that uses zip64 ext data, argh */
3952 0 ▶ if ((comp_size != MZ_UINT32_MAX) && (decomp_size != MZ_UINT32_MAX))
Branch: 2 branches
BranchTrueFalseTotal
if ((comp_size != MZ_UINT32_MAX) && (decomp_size != MZ_UINT32_MAX)) 0 0 0
if ((comp_size != MZ_UINT32_MAX) && (decomp_size != MZ_UINT32_MAX)) 0 0 0
3953 {
3954 0 ▶ if (((!MZ_READ_LE32(p + MZ_ZIP_CDH_METHOD_OFS)) && (decomp_size != comp_size)) || (decomp_size && !comp_size))
Branch: 4 branches
BranchTrueFalseTotal
if (((!MZ_READ_LE32(p + MZ_ZIP_CDH_METHOD_OFS)) && (decomp_size != comp_size)) || (decomp_size && !comp_size)) 0 0 0
if (((!MZ_READ_LE32(p + MZ_ZIP_CDH_METHOD_OFS)) && (decomp_size != comp_size)) || (decomp_size && !comp_size)) 0 0 0
if (((!MZ_READ_LE32(p + MZ_ZIP_CDH_METHOD_OFS)) && (decomp_size != comp_size)) || (decomp_size && !comp_size)) 0 0 0
if (((!MZ_READ_LE32(p + MZ_ZIP_CDH_METHOD_OFS)) && (decomp_size != comp_size)) || (decomp_size && !comp_size)) 0 0 0
3955 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
3956 }
3957  
3958 0 disk_index = MZ_READ_LE16(p + MZ_ZIP_CDH_DISK_START_OFS);
3959 0 ▶ if ((disk_index == MZ_UINT16_MAX) || ((disk_index != num_this_disk) && (disk_index != 1)))
Branch: 3 branches
BranchTrueFalseTotal
if ((disk_index == MZ_UINT16_MAX) || ((disk_index != num_this_disk) && (disk_index != 1))) 0 0 0
if ((disk_index == MZ_UINT16_MAX) || ((disk_index != num_this_disk) && (disk_index != 1))) 0 0 0
if ((disk_index == MZ_UINT16_MAX) || ((disk_index != num_this_disk) && (disk_index != 1))) 0 0 0
3960 0 return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_MULTIDISK);
3961  
3962 0 ▶ if (comp_size != MZ_UINT32_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (comp_size != MZ_UINT32_MAX) 0 0 0
3963 {
3964 0 ▶ if (((mz_uint64)MZ_READ_LE32(p + MZ_ZIP_CDH_LOCAL_HEADER_OFS) + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + comp_size) > pZip->m_archive_size)
Branch: 1 branch
BranchTrueFalseTotal
if (((mz_uint64)MZ_READ_LE32(p + MZ_ZIP_CDH_LOCAL_HEADER_OFS) + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + comp_size) > pZip->m_archive_size) 0 0 0
3965 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
3966 }
3967  
3968 0 bit_flags = MZ_READ_LE16(p + MZ_ZIP_CDH_BIT_FLAG_OFS);
3969 0 ▶ if (bit_flags & MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_LOCAL_DIR_IS_MASKED)
Branch: 1 branch
BranchTrueFalseTotal
if (bit_flags & MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_LOCAL_DIR_IS_MASKED) 0 0 0
3970 0 return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_ENCRYPTION);
3971  
3972 0 ▶ if ((total_header_size = MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS) + MZ_READ_LE16(p + MZ_ZIP_CDH_EXTRA_LEN_OFS) + MZ_READ_LE16(p + MZ_ZIP_CDH_COMMENT_LEN_OFS)) > n)
Branch: 1 branch
BranchTrueFalseTotal
if ((total_header_size = MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS) + MZ_READ_LE16(p + MZ_ZIP_CDH_EXTRA_LEN_OFS) + MZ_READ_LE16(p + MZ_ZIP_CDH_COMMENT_LEN_OFS)) > n) 0 0 0
3973 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
3974  
3975 0 n -= total_header_size;
3976 0 p += total_header_size;
3977 }
3978 }
3979  
3980 0 ▶ if (sort_central_dir)
Branch: 1 branch
BranchTrueFalseTotal
if (sort_central_dir) 0 0 0
3981 0 mz_zip_reader_sort_central_dir_offsets_by_filename(pZip);
3982  
3983 0 return MZ_TRUE;
3984 }
3985  
3986 0 void mz_zip_zero_struct(mz_zip_archive *pZip)
3987 {
3988 0 ▶ if (pZip)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip) 0 0 0
3989 0 MZ_CLEAR_PTR(pZip);
3990 0 }
3991  
3992 0 static mz_bool mz_zip_reader_end_internal(mz_zip_archive *pZip, mz_bool set_last_error)
3993 {
3994 0 mz_bool status = MZ_TRUE;
3995  
3996 0 ▶ if (!pZip)
Branch: 1 branch
BranchTrueFalseTotal
if (!pZip) 0 0 0
3997 0 return MZ_FALSE;
3998  
3999 0 ▶ if ((!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || (pZip->m_zip_mode != MZ_ZIP_MODE_READING))
Branch: 4 branches
BranchTrueFalseTotal
if ((!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || (pZip->m_zip_mode != MZ_ZIP_MODE_READING)) 0 0 0
if ((!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || (pZip->m_zip_mode != MZ_ZIP_MODE_READING)) 0 0 0
if ((!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || (pZip->m_zip_mode != MZ_ZIP_MODE_READING)) 0 0 0
if ((!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || (pZip->m_zip_mode != MZ_ZIP_MODE_READING)) 0 0 0
4000 {
4001 0 ▶ if (set_last_error)
Branch: 1 branch
BranchTrueFalseTotal
if (set_last_error) 0 0 0
4002 0 pZip->m_last_error = MZ_ZIP_INVALID_PARAMETER;
4003  
4004 0 return MZ_FALSE;
4005 }
4006  
4007 0 ▶ if (pZip->m_pState)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pState) 0 0 0
4008 {
4009 0 mz_zip_internal_state *pState = pZip->m_pState;
4010 0 pZip->m_pState = NULL;
4011  
4012 0 mz_zip_array_clear(pZip, &pState->m_central_dir);
4013 0 mz_zip_array_clear(pZip, &pState->m_central_dir_offsets);
4014 0 mz_zip_array_clear(pZip, &pState->m_sorted_central_dir_offsets);
4015  
4016 #ifndef MINIZ_NO_STDIO
4017 if (pState->m_pFile)
4018 {
4019 if (pZip->m_zip_type == MZ_ZIP_TYPE_FILE)
4020 {
4021 if (MZ_FCLOSE(pState->m_pFile) == EOF)
4022 {
4023 if (set_last_error)
4024 pZip->m_last_error = MZ_ZIP_FILE_CLOSE_FAILED;
4025 status = MZ_FALSE;
4026 }
4027 }
4028 pState->m_pFile = NULL;
4029 }
4030 #endif /* #ifndef MINIZ_NO_STDIO */
4031  
4032 0 pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
4033 }
4034 0 pZip->m_zip_mode = MZ_ZIP_MODE_INVALID;
4035  
4036 0 return status;
4037 }
4038  
4039 0 mz_bool mz_zip_reader_end(mz_zip_archive *pZip)
4040 {
4041 0 return mz_zip_reader_end_internal(pZip, MZ_TRUE);
4042 }
4043 0 mz_bool mz_zip_reader_init(mz_zip_archive *pZip, mz_uint64 size, mz_uint flags)
4044 {
4045 0 ▶ if ((!pZip) || (!pZip->m_pRead))
Branch: 2 branches
BranchTrueFalseTotal
if ((!pZip) || (!pZip->m_pRead)) 0 0 0
if ((!pZip) || (!pZip->m_pRead)) 0 0 0
4046 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
4047  
4048 0 ▶ if (!mz_zip_reader_init_internal(pZip, flags))
Branch: 1 branch
BranchTrueFalseTotal
if (!mz_zip_reader_init_internal(pZip, flags)) 0 0 0
4049 0 return MZ_FALSE;
4050  
4051 0 pZip->m_zip_type = MZ_ZIP_TYPE_USER;
4052 0 pZip->m_archive_size = size;
4053  
4054 0 ▶ if (!mz_zip_reader_read_central_dir(pZip, flags))
Branch: 1 branch
BranchTrueFalseTotal
if (!mz_zip_reader_read_central_dir(pZip, flags)) 0 0 0
4055 {
4056 0 mz_zip_reader_end_internal(pZip, MZ_FALSE);
4057 0 return MZ_FALSE;
4058 }
4059  
4060 0 return MZ_TRUE;
4061 }
4062  
4063 0 static size_t mz_zip_mem_read_func(void *pOpaque, mz_uint64 file_ofs, void *pBuf, size_t n)
4064 {
4065 0 mz_zip_archive *pZip = (mz_zip_archive *)pOpaque;
4066 0 ▶ size_t s = (file_ofs >= pZip->m_archive_size) ? 0 : (size_t)MZ_MIN(pZip->m_archive_size - file_ofs, n);
Branch: 1 branch
BranchTrueFalseTotal
size_t s = (file_ofs >= pZip->m_archive_size) ? 0 : (size_t)MZ_MIN(pZip->m_archive_size - file_ofs, n); 0 0 0
4067 0 memcpy(pBuf, (const mz_uint8 *)pZip->m_pState->m_pMem + file_ofs, s);
4068 0 return s;
4069 }
4070  
4071 0 mz_bool mz_zip_reader_init_mem(mz_zip_archive *pZip, const void *pMem, size_t size, mz_uint flags)
4072 {
4073 0 ▶ if (!pMem)
Branch: 1 branch
BranchTrueFalseTotal
if (!pMem) 0 0 0
4074 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
4075  
4076 0 ▶ if (size < MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE)
Branch: 1 branch
BranchTrueFalseTotal
if (size < MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) 0 0 0
4077 0 return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE);
4078  
4079 0 ▶ if (!mz_zip_reader_init_internal(pZip, flags))
Branch: 1 branch
BranchTrueFalseTotal
if (!mz_zip_reader_init_internal(pZip, flags)) 0 0 0
4080 0 return MZ_FALSE;
4081  
4082 0 pZip->m_zip_type = MZ_ZIP_TYPE_MEMORY;
4083 0 pZip->m_archive_size = size;
4084 0 pZip->m_pRead = mz_zip_mem_read_func;
4085 0 pZip->m_pIO_opaque = pZip;
4086 0 pZip->m_pNeeds_keepalive = NULL;
4087  
4088 #ifdef __cplusplus
4089 pZip->m_pState->m_pMem = const_cast<void *>(pMem);
4090 #else
4091 0 pZip->m_pState->m_pMem = (void *)pMem;
4092 #endif
4093  
4094 0 pZip->m_pState->m_mem_size = size;
4095  
4096 0 ▶ if (!mz_zip_reader_read_central_dir(pZip, flags))
Branch: 1 branch
BranchTrueFalseTotal
if (!mz_zip_reader_read_central_dir(pZip, flags)) 0 0 0
4097 {
4098 0 mz_zip_reader_end_internal(pZip, MZ_FALSE);
4099 0 return MZ_FALSE;
4100 }
4101  
4102 0 return MZ_TRUE;
4103 }
4104  
4105 #ifndef MINIZ_NO_STDIO
4106 static size_t mz_zip_file_read_func(void *pOpaque, mz_uint64 file_ofs, void *pBuf, size_t n)
4107 {
4108 mz_zip_archive *pZip = (mz_zip_archive *)pOpaque;
4109 mz_int64 cur_ofs = MZ_FTELL64(pZip->m_pState->m_pFile);
4110  
4111 file_ofs += pZip->m_pState->m_file_archive_start_ofs;
4112  
4113 if (((mz_int64)file_ofs < 0) || (((cur_ofs != (mz_int64)file_ofs)) && (MZ_FSEEK64(pZip->m_pState->m_pFile, (mz_int64)file_ofs, SEEK_SET))))
4114 return 0;
4115  
4116 return MZ_FREAD(pBuf, 1, n, pZip->m_pState->m_pFile);
4117 }
4118  
4119 mz_bool mz_zip_reader_init_file(mz_zip_archive *pZip, const char *pFilename, mz_uint32 flags)
4120 {
4121 return mz_zip_reader_init_file_v2(pZip, pFilename, flags, 0, 0);
4122 }
4123  
4124 mz_bool mz_zip_reader_init_file_v2(mz_zip_archive *pZip, const char *pFilename, mz_uint flags, mz_uint64 file_start_ofs, mz_uint64 archive_size)
4125 {
4126 mz_uint64 file_size;
4127 MZ_FILE *pFile;
4128  
4129 if ((!pZip) || (!pFilename) || ((archive_size) && (archive_size < MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE)))
4130 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
4131  
4132 pFile = MZ_FOPEN(pFilename, (flags & MZ_ZIP_FLAG_READ_ALLOW_WRITING ) ? "r+b" : "rb");
4133 if (!pFile)
4134 return mz_zip_set_error(pZip, MZ_ZIP_FILE_OPEN_FAILED);
4135  
4136 file_size = archive_size;
4137 if (!file_size)
4138 {
4139 if (MZ_FSEEK64(pFile, 0, SEEK_END))
4140 {
4141 MZ_FCLOSE(pFile);
4142 return mz_zip_set_error(pZip, MZ_ZIP_FILE_SEEK_FAILED);
4143 }
4144  
4145 file_size = MZ_FTELL64(pFile);
4146 }
4147  
4148 /* TODO: Better sanity check archive_size and the # of actual remaining bytes */
4149  
4150 if (file_size < MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE)
4151 {
4152 MZ_FCLOSE(pFile);
4153 return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE);
4154 }
4155  
4156 if (!mz_zip_reader_init_internal(pZip, flags))
4157 {
4158 MZ_FCLOSE(pFile);
4159 return MZ_FALSE;
4160 }
4161  
4162 pZip->m_zip_type = MZ_ZIP_TYPE_FILE;
4163 pZip->m_pRead = mz_zip_file_read_func;
4164 pZip->m_pIO_opaque = pZip;
4165 pZip->m_pState->m_pFile = pFile;
4166 pZip->m_archive_size = file_size;
4167 pZip->m_pState->m_file_archive_start_ofs = file_start_ofs;
4168  
4169 if (!mz_zip_reader_read_central_dir(pZip, flags))
4170 {
4171 mz_zip_reader_end_internal(pZip, MZ_FALSE);
4172 return MZ_FALSE;
4173 }
4174  
4175 return MZ_TRUE;
4176 }
4177  
4178 mz_bool mz_zip_reader_init_cfile(mz_zip_archive *pZip, MZ_FILE *pFile, mz_uint64 archive_size, mz_uint flags)
4179 {
4180 mz_uint64 cur_file_ofs;
4181  
4182 if ((!pZip) || (!pFile))
4183 return mz_zip_set_error(pZip, MZ_ZIP_FILE_OPEN_FAILED);
4184  
4185 cur_file_ofs = MZ_FTELL64(pFile);
4186  
4187 if (!archive_size)
4188 {
4189 if (MZ_FSEEK64(pFile, 0, SEEK_END))
4190 return mz_zip_set_error(pZip, MZ_ZIP_FILE_SEEK_FAILED);
4191  
4192 archive_size = MZ_FTELL64(pFile) - cur_file_ofs;
4193  
4194 if (archive_size < MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE)
4195 return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE);
4196 }
4197  
4198 if (!mz_zip_reader_init_internal(pZip, flags))
4199 return MZ_FALSE;
4200  
4201 pZip->m_zip_type = MZ_ZIP_TYPE_CFILE;
4202 pZip->m_pRead = mz_zip_file_read_func;
4203  
4204 pZip->m_pIO_opaque = pZip;
4205 pZip->m_pState->m_pFile = pFile;
4206 pZip->m_archive_size = archive_size;
4207 pZip->m_pState->m_file_archive_start_ofs = cur_file_ofs;
4208  
4209 if (!mz_zip_reader_read_central_dir(pZip, flags))
4210 {
4211 mz_zip_reader_end_internal(pZip, MZ_FALSE);
4212 return MZ_FALSE;
4213 }
4214  
4215 return MZ_TRUE;
4216 }
4217  
4218 #endif /* #ifndef MINIZ_NO_STDIO */
4219  
4220 static MZ_FORCEINLINE const mz_uint8 *mz_zip_get_cdh(mz_zip_archive *pZip, mz_uint file_index)
4221 {
4222 0 ▶ if ((!pZip) || (!pZip->m_pState) || (file_index >= pZip->m_total_files))
Branch: 15 branches
BranchTrueFalseTotal
if ((!pZip) || (!pZip->m_pState) || (file_index >= pZip->m_total_files)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (file_index >= pZip->m_total_files)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (file_index >= pZip->m_total_files)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (file_index >= pZip->m_total_files)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (file_index >= pZip->m_total_files)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (file_index >= pZip->m_total_files)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (file_index >= pZip->m_total_files)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (file_index >= pZip->m_total_files)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (file_index >= pZip->m_total_files)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (file_index >= pZip->m_total_files)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (file_index >= pZip->m_total_files)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (file_index >= pZip->m_total_files)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (file_index >= pZip->m_total_files)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (file_index >= pZip->m_total_files)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (file_index >= pZip->m_total_files)) 0 0 0
4223 0 return NULL;
4224 0 return &MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir, mz_uint8, MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir_offsets, mz_uint32, file_index));
4225 }
4226  
4227 0 ▶ mz_bool mz_zip_reader_is_file_encrypted(mz_zip_archive *pZip, mz_uint file_index)
Branch: 1 branch
BranchTrueFalseTotal
mz_bool mz_zip_reader_is_file_encrypted(mz_zip_archive *pZip, mz_uint file_index) 0 0 0
4228 {
4229 mz_uint m_bit_flag;
4230 0 const mz_uint8 *p = mz_zip_get_cdh(pZip, file_index);
4231 0 ▶ if (!p)
Branch: 1 branch
BranchTrueFalseTotal
if (!p) 0 0 0
4232 {
4233 mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
4234 0 return MZ_FALSE;
4235 }
4236  
4237 0 m_bit_flag = MZ_READ_LE16(p + MZ_ZIP_CDH_BIT_FLAG_OFS);
4238 0 return (m_bit_flag & (MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_IS_ENCRYPTED | MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_USES_STRONG_ENCRYPTION)) != 0;
4239 }
4240  
4241 0 ▶ mz_bool mz_zip_reader_is_file_supported(mz_zip_archive *pZip, mz_uint file_index)
Branch: 1 branch
BranchTrueFalseTotal
mz_bool mz_zip_reader_is_file_supported(mz_zip_archive *pZip, mz_uint file_index) 0 0 0
4242 {
4243 mz_uint bit_flag;
4244 mz_uint method;
4245  
4246 0 const mz_uint8 *p = mz_zip_get_cdh(pZip, file_index);
4247 0 ▶ if (!p)
Branch: 1 branch
BranchTrueFalseTotal
if (!p) 0 0 0
4248 {
4249 mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
4250 0 return MZ_FALSE;
4251 }
4252  
4253 0 method = MZ_READ_LE16(p + MZ_ZIP_CDH_METHOD_OFS);
4254 0 bit_flag = MZ_READ_LE16(p + MZ_ZIP_CDH_BIT_FLAG_OFS);
4255  
4256 0 ▶ if ((method != 0) && (method != MZ_DEFLATED))
Branch: 2 branches
BranchTrueFalseTotal
if ((method != 0) && (method != MZ_DEFLATED)) 0 0 0
if ((method != 0) && (method != MZ_DEFLATED)) 0 0 0
4257 {
4258 mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_METHOD);
4259 0 return MZ_FALSE;
4260 }
4261  
4262 0 ▶ if (bit_flag & (MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_IS_ENCRYPTED | MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_USES_STRONG_ENCRYPTION))
Branch: 1 branch
BranchTrueFalseTotal
if (bit_flag & (MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_IS_ENCRYPTED | MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_USES_STRONG_ENCRYPTION)) 0 0 0
4263 {
4264 mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_ENCRYPTION);
4265 0 return MZ_FALSE;
4266 }
4267  
4268 0 ▶ if (bit_flag & MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_COMPRESSED_PATCH_FLAG)
Branch: 1 branch
BranchTrueFalseTotal
if (bit_flag & MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_COMPRESSED_PATCH_FLAG) 0 0 0
4269 {
4270 mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_FEATURE);
4271 0 return MZ_FALSE;
4272 }
4273  
4274 0 return MZ_TRUE;
4275 }
4276  
4277 0 ▶ mz_bool mz_zip_reader_is_file_a_directory(mz_zip_archive *pZip, mz_uint file_index)
Branch: 1 branch
BranchTrueFalseTotal
mz_bool mz_zip_reader_is_file_a_directory(mz_zip_archive *pZip, mz_uint file_index) 0 0 0
4278 {
4279 mz_uint filename_len, attribute_mapping_id, external_attr;
4280 0 const mz_uint8 *p = mz_zip_get_cdh(pZip, file_index);
4281 0 ▶ if (!p)
Branch: 1 branch
BranchTrueFalseTotal
if (!p) 0 0 0
4282 {
4283 mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
4284 0 return MZ_FALSE;
4285 }
4286  
4287 0 filename_len = MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS);
4288 0 ▶ if (filename_len)
Branch: 1 branch
BranchTrueFalseTotal
if (filename_len) 0 0 0
4289 {
4290 0 ▶ if (*(p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + filename_len - 1) == '/')
Branch: 1 branch
BranchTrueFalseTotal
if (*(p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + filename_len - 1) == '/') 0 0 0
4291 0 return MZ_TRUE;
4292 }
4293  
4294 /* Bugfix: This code was also checking if the internal attribute was non-zero, which wasn't correct. */
4295 /* Most/all zip writers (hopefully) set DOS file/directory attributes in the low 16-bits, so check for the DOS directory flag and ignore the source OS ID in the created by field. */
4296 /* FIXME: Remove this check? Is it necessary - we already check the filename. */
4297 0 attribute_mapping_id = MZ_READ_LE16(p + MZ_ZIP_CDH_VERSION_MADE_BY_OFS) >> 8;
4298 (void)attribute_mapping_id;
4299  
4300 0 external_attr = MZ_READ_LE32(p + MZ_ZIP_CDH_EXTERNAL_ATTR_OFS);
4301 0 ▶ if ((external_attr & MZ_ZIP_DOS_DIR_ATTRIBUTE_BITFLAG) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((external_attr & MZ_ZIP_DOS_DIR_ATTRIBUTE_BITFLAG) != 0) 0 0 0
4302 {
4303 0 return MZ_TRUE;
4304 }
4305  
4306 0 return MZ_FALSE;
4307 }
4308  
4309 0 static mz_bool mz_zip_file_stat_internal(mz_zip_archive *pZip, mz_uint file_index, const mz_uint8 *pCentral_dir_header, mz_zip_archive_file_stat *pStat, mz_bool *pFound_zip64_extra_data)
4310 {
4311 mz_uint n;
4312 0 const mz_uint8 *p = pCentral_dir_header;
4313  
4314 0 ▶ if (pFound_zip64_extra_data)
Branch: 1 branch
BranchTrueFalseTotal
if (pFound_zip64_extra_data) 0 0 0
4315 0 *pFound_zip64_extra_data = MZ_FALSE;
4316  
4317 0 ▶ if ((!p) || (!pStat))
Branch: 2 branches
BranchTrueFalseTotal
if ((!p) || (!pStat)) 0 0 0
if ((!p) || (!pStat)) 0 0 0
4318 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
4319  
4320 /* Extract fields from the central directory record. */
4321 0 pStat->m_file_index = file_index;
4322 0 pStat->m_central_dir_ofs = MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir_offsets, mz_uint32, file_index);
4323 0 pStat->m_version_made_by = MZ_READ_LE16(p + MZ_ZIP_CDH_VERSION_MADE_BY_OFS);
4324 0 pStat->m_version_needed = MZ_READ_LE16(p + MZ_ZIP_CDH_VERSION_NEEDED_OFS);
4325 0 pStat->m_bit_flag = MZ_READ_LE16(p + MZ_ZIP_CDH_BIT_FLAG_OFS);
4326 0 pStat->m_method = MZ_READ_LE16(p + MZ_ZIP_CDH_METHOD_OFS);
4327 #ifndef MINIZ_NO_TIME
4328 0 pStat->m_time = mz_zip_dos_to_time_t(MZ_READ_LE16(p + MZ_ZIP_CDH_FILE_TIME_OFS), MZ_READ_LE16(p + MZ_ZIP_CDH_FILE_DATE_OFS));
4329 #endif
4330 0 pStat->m_crc32 = MZ_READ_LE32(p + MZ_ZIP_CDH_CRC32_OFS);
4331 0 pStat->m_comp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS);
4332 0 pStat->m_uncomp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS);
4333 0 pStat->m_internal_attr = MZ_READ_LE16(p + MZ_ZIP_CDH_INTERNAL_ATTR_OFS);
4334 0 pStat->m_external_attr = MZ_READ_LE32(p + MZ_ZIP_CDH_EXTERNAL_ATTR_OFS);
4335 0 pStat->m_local_header_ofs = MZ_READ_LE32(p + MZ_ZIP_CDH_LOCAL_HEADER_OFS);
4336  
4337 /* Copy as much of the filename and comment as possible. */
4338 0 n = MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS);
4339 0 n = MZ_MIN(n, MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE - 1);
4340 0 memcpy(pStat->m_filename, p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE, n);
4341 0 pStat->m_filename[n] = '\0';
4342  
4343 0 n = MZ_READ_LE16(p + MZ_ZIP_CDH_COMMENT_LEN_OFS);
4344 0 n = MZ_MIN(n, MZ_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE - 1);
4345 0 pStat->m_comment_size = n;
4346 0 memcpy(pStat->m_comment, p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS) + MZ_READ_LE16(p + MZ_ZIP_CDH_EXTRA_LEN_OFS), n);
4347 0 pStat->m_comment[n] = '\0';
4348  
4349 /* Set some flags for convienance */
4350 0 pStat->m_is_directory = mz_zip_reader_is_file_a_directory(pZip, file_index);
4351 0 pStat->m_is_encrypted = mz_zip_reader_is_file_encrypted(pZip, file_index);
4352 0 pStat->m_is_supported = mz_zip_reader_is_file_supported(pZip, file_index);
4353  
4354 /* See if we need to read any zip64 extended information fields. */
4355 /* Confusingly, these zip64 fields can be present even on non-zip64 archives (Debian zip on a huge files from stdin piped to stdout creates them). */
4356 0 ▶ if (MZ_MAX(MZ_MAX(pStat->m_comp_size, pStat->m_uncomp_size), pStat->m_local_header_ofs) == MZ_UINT32_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (MZ_MAX(MZ_MAX(pStat->m_comp_size, pStat->m_uncomp_size), pStat->m_local_header_ofs) == MZ_UINT32_MAX) 0 0 0
4357 {
4358 /* Attempt to find zip64 extended information field in the entry's extra data */
4359 0 mz_uint32 extra_size_remaining = MZ_READ_LE16(p + MZ_ZIP_CDH_EXTRA_LEN_OFS);
4360  
4361 0 ▶ if (extra_size_remaining)
Branch: 1 branch
BranchTrueFalseTotal
if (extra_size_remaining) 0 0 0
4362 {
4363 0 const mz_uint8 *pExtra_data = p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS);
4364  
4365 do
4366 {
4367 mz_uint32 field_id;
4368 mz_uint32 field_data_size;
4369  
4370 0 ▶ if (extra_size_remaining < (sizeof(mz_uint16) * 2))
Branch: 1 branch
BranchTrueFalseTotal
if (extra_size_remaining < (sizeof(mz_uint16) * 2)) 0 0 0
4371 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
4372  
4373 0 field_id = MZ_READ_LE16(pExtra_data);
4374 0 field_data_size = MZ_READ_LE16(pExtra_data + sizeof(mz_uint16));
4375  
4376 0 ▶ if ((field_data_size + sizeof(mz_uint16) * 2) > extra_size_remaining)
Branch: 1 branch
BranchTrueFalseTotal
if ((field_data_size + sizeof(mz_uint16) * 2) > extra_size_remaining) 0 0 0
4377 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
4378  
4379 0 ▶ if (field_id == MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID)
Branch: 1 branch
BranchTrueFalseTotal
if (field_id == MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID) 0 0 0
4380 {
4381 0 const mz_uint8 *pField_data = pExtra_data + sizeof(mz_uint16) * 2;
4382 0 mz_uint32 field_data_remaining = field_data_size;
4383  
4384 0 ▶ if (pFound_zip64_extra_data)
Branch: 1 branch
BranchTrueFalseTotal
if (pFound_zip64_extra_data) 0 0 0
4385 0 *pFound_zip64_extra_data = MZ_TRUE;
4386  
4387 0 ▶ if (pStat->m_uncomp_size == MZ_UINT32_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (pStat->m_uncomp_size == MZ_UINT32_MAX) 0 0 0
4388 {
4389 0 ▶ if (field_data_remaining < sizeof(mz_uint64))
Branch: 1 branch
BranchTrueFalseTotal
if (field_data_remaining < sizeof(mz_uint64)) 0 0 0
4390 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
4391  
4392 0 pStat->m_uncomp_size = MZ_READ_LE64(pField_data);
4393 0 pField_data += sizeof(mz_uint64);
4394 0 field_data_remaining -= sizeof(mz_uint64);
4395 }
4396  
4397 0 ▶ if (pStat->m_comp_size == MZ_UINT32_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (pStat->m_comp_size == MZ_UINT32_MAX) 0 0 0
4398 {
4399 0 ▶ if (field_data_remaining < sizeof(mz_uint64))
Branch: 1 branch
BranchTrueFalseTotal
if (field_data_remaining < sizeof(mz_uint64)) 0 0 0
4400 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
4401  
4402 0 pStat->m_comp_size = MZ_READ_LE64(pField_data);
4403 0 pField_data += sizeof(mz_uint64);
4404 0 field_data_remaining -= sizeof(mz_uint64);
4405 }
4406  
4407 0 ▶ if (pStat->m_local_header_ofs == MZ_UINT32_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (pStat->m_local_header_ofs == MZ_UINT32_MAX) 0 0 0
4408 {
4409 0 ▶ if (field_data_remaining < sizeof(mz_uint64))
Branch: 1 branch
BranchTrueFalseTotal
if (field_data_remaining < sizeof(mz_uint64)) 0 0 0
4410 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
4411  
4412 0 pStat->m_local_header_ofs = MZ_READ_LE64(pField_data);
4413 0 pField_data += sizeof(mz_uint64);
4414 0 field_data_remaining -= sizeof(mz_uint64);
4415 }
4416  
4417 0 break;
4418 }
4419  
4420 0 pExtra_data += sizeof(mz_uint16) * 2 + field_data_size;
4421 0 extra_size_remaining = extra_size_remaining - sizeof(mz_uint16) * 2 - field_data_size;
4422 0 ▶ } while (extra_size_remaining);
Branch: 1 branch
BranchTrueFalseTotal
} while (extra_size_remaining); 0 0 0
4423 }
4424 }
4425  
4426 0 return MZ_TRUE;
4427 }
4428  
4429 static MZ_FORCEINLINE mz_bool mz_zip_string_equal(const char *pA, const char *pB, mz_uint len, mz_uint flags)
4430 {
4431 mz_uint i;
4432 0 ▶ if (flags & MZ_ZIP_FLAG_CASE_SENSITIVE)
Branch: 2 branches
BranchTrueFalseTotal
if (flags & MZ_ZIP_FLAG_CASE_SENSITIVE) 0 0 0
if (flags & MZ_ZIP_FLAG_CASE_SENSITIVE) 0 0 0
4433 0 return 0 == memcmp(pA, pB, len);
4434 0 ▶ for (i = 0; i < len; ++i)
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < len; ++i) 0 0 0
for (i = 0; i < len; ++i) 0 0 0
4435 0 ▶ if (MZ_TOLOWER(pA[i]) != MZ_TOLOWER(pB[i]))
Branch: 10 branches
BranchTrueFalseTotal
if (MZ_TOLOWER(pA[i]) != MZ_TOLOWER(pB[i])) 0 0 0
if (MZ_TOLOWER(pA[i]) != MZ_TOLOWER(pB[i])) 0 0 0
if (MZ_TOLOWER(pA[i]) != MZ_TOLOWER(pB[i])) 0 0 0
if (MZ_TOLOWER(pA[i]) != MZ_TOLOWER(pB[i])) 0 0 0
if (MZ_TOLOWER(pA[i]) != MZ_TOLOWER(pB[i])) 0 0 0
if (MZ_TOLOWER(pA[i]) != MZ_TOLOWER(pB[i])) 0 0 0
if (MZ_TOLOWER(pA[i]) != MZ_TOLOWER(pB[i])) 0 0 0
if (MZ_TOLOWER(pA[i]) != MZ_TOLOWER(pB[i])) 0 0 0
if (MZ_TOLOWER(pA[i]) != MZ_TOLOWER(pB[i])) 0 0 0
if (MZ_TOLOWER(pA[i]) != MZ_TOLOWER(pB[i])) 0 0 0
4436 0 return MZ_FALSE;
4437 0 return MZ_TRUE;
4438 }
4439  
4440 static MZ_FORCEINLINE int mz_zip_filename_compare(const mz_zip_array *pCentral_dir_array, const mz_zip_array *pCentral_dir_offsets, mz_uint l_index, const char *pR, mz_uint r_len)
4441 {
4442 0 const mz_uint8 *pL = &MZ_ZIP_ARRAY_ELEMENT(pCentral_dir_array, mz_uint8, MZ_ZIP_ARRAY_ELEMENT(pCentral_dir_offsets, mz_uint32, l_index)), *pE;
4443 0 mz_uint l_len = MZ_READ_LE16(pL + MZ_ZIP_CDH_FILENAME_LEN_OFS);
4444 0 mz_uint8 l = 0, r = 0;
4445 0 pL += MZ_ZIP_CENTRAL_DIR_HEADER_SIZE;
4446 0 pE = pL + MZ_MIN(l_len, r_len);
4447 0 ▶ while (pL < pE)
Branch: 1 branch
BranchTrueFalseTotal
while (pL < pE) 0 0 0
4448 {
4449 0 ▶ if ((l = MZ_TOLOWER(*pL)) != (r = MZ_TOLOWER(*pR)))
Branch: 5 branches
BranchTrueFalseTotal
if ((l = MZ_TOLOWER(*pL)) != (r = MZ_TOLOWER(*pR))) 0 0 0
if ((l = MZ_TOLOWER(*pL)) != (r = MZ_TOLOWER(*pR))) 0 0 0
if ((l = MZ_TOLOWER(*pL)) != (r = MZ_TOLOWER(*pR))) 0 0 0
if ((l = MZ_TOLOWER(*pL)) != (r = MZ_TOLOWER(*pR))) 0 0 0
if ((l = MZ_TOLOWER(*pL)) != (r = MZ_TOLOWER(*pR))) 0 0 0
4450 0 break;
4451 0 pL++;
4452 0 pR++;
4453 }
4454 0 ▶ return (pL == pE) ? (int)(l_len - r_len) : (l - r);
Branch: 1 branch
BranchTrueFalseTotal
return (pL == pE) ? (int)(l_len - r_len) : (l - r); 0 0 0
4455 }
4456  
4457 0 static mz_bool mz_zip_locate_file_binary_search(mz_zip_archive *pZip, const char *pFilename, mz_uint32 *pIndex)
4458 {
4459 0 mz_zip_internal_state *pState = pZip->m_pState;
4460 0 const mz_zip_array *pCentral_dir_offsets = &pState->m_central_dir_offsets;
4461 0 const mz_zip_array *pCentral_dir = &pState->m_central_dir;
4462 0 mz_uint32 *pIndices = &MZ_ZIP_ARRAY_ELEMENT(&pState->m_sorted_central_dir_offsets, mz_uint32, 0);
4463 0 const mz_uint32 size = pZip->m_total_files;
4464 0 const mz_uint filename_len = (mz_uint)strlen(pFilename);
4465  
4466 0 ▶ if (pIndex)
Branch: 1 branch
BranchTrueFalseTotal
if (pIndex) 0 0 0
4467 0 *pIndex = 0;
4468  
4469 0 ▶ if (size)
Branch: 1 branch
BranchTrueFalseTotal
if (size) 0 0 0
4470 {
4471 /* yes I could use uint32_t's, but then we would have to add some special case checks in the loop, argh, and */
4472 /* honestly the major expense here on 32-bit CPU's will still be the filename compare */
4473 0 mz_int64 l = 0, h = (mz_int64)size - 1;
4474  
4475 0 ▶ while (l <= h)
Branch: 1 branch
BranchTrueFalseTotal
while (l <= h) 0 0 0
4476 {
4477 0 mz_int64 m = l + ((h - l) >> 1);
4478 0 mz_uint32 file_index = pIndices[(mz_uint32)m];
4479  
4480 0 int comp = mz_zip_filename_compare(pCentral_dir, pCentral_dir_offsets, file_index, pFilename, filename_len);
4481 0 ▶ if (!comp)
Branch: 1 branch
BranchTrueFalseTotal
if (!comp) 0 0 0
4482 {
4483 0 ▶ if (pIndex)
Branch: 1 branch
BranchTrueFalseTotal
if (pIndex) 0 0 0
4484 0 *pIndex = file_index;
4485 0 return MZ_TRUE;
4486 }
4487 0 ▶ else if (comp < 0)
Branch: 1 branch
BranchTrueFalseTotal
else if (comp < 0) 0 0 0
4488 0 l = m + 1;
4489 else
4490 0 h = m - 1;
4491 }
4492 }
4493  
4494 0 return mz_zip_set_error(pZip, MZ_ZIP_FILE_NOT_FOUND);
4495 }
4496  
4497 0 int mz_zip_reader_locate_file(mz_zip_archive *pZip, const char *pName, const char *pComment, mz_uint flags)
4498 {
4499 mz_uint32 index;
4500 0 ▶ if (!mz_zip_reader_locate_file_v2(pZip, pName, pComment, flags, &index))
Branch: 1 branch
BranchTrueFalseTotal
if (!mz_zip_reader_locate_file_v2(pZip, pName, pComment, flags, &index)) 0 0 0
4501 0 return -1;
4502 else
4503 0 return (int)index;
4504 }
4505  
4506 0 mz_bool mz_zip_reader_locate_file_v2(mz_zip_archive *pZip, const char *pName, const char *pComment, mz_uint flags, mz_uint32 *pIndex)
4507 {
4508 mz_uint file_index;
4509 size_t name_len, comment_len;
4510  
4511 0 ▶ if (pIndex)
Branch: 1 branch
BranchTrueFalseTotal
if (pIndex) 0 0 0
4512 0 *pIndex = 0;
4513  
4514 0 ▶ if ((!pZip) || (!pZip->m_pState) || (!pName))
Branch: 3 branches
BranchTrueFalseTotal
if ((!pZip) || (!pZip->m_pState) || (!pName)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (!pName)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (!pName)) 0 0 0
4515 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
4516  
4517 /* See if we can use a binary search */
4518 0 ▶ if (((pZip->m_pState->m_init_flags & MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY) == 0) &&
Branch: 1 branch
BranchTrueFalseTotal
if (((pZip->m_pState->m_init_flags & MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY) == 0) && 0 0 0
4519 0 ▶ (pZip->m_zip_mode == MZ_ZIP_MODE_READING) &&
Branch: 1 branch
BranchTrueFalseTotal
(pZip->m_zip_mode == MZ_ZIP_MODE_READING) && 0 0 0
4520 0 ▶ ((flags & (MZ_ZIP_FLAG_IGNORE_PATH | MZ_ZIP_FLAG_CASE_SENSITIVE)) == 0) && (!pComment) && (pZip->m_pState->m_sorted_central_dir_offsets.m_size))
Branch: 3 branches
BranchTrueFalseTotal
((flags & (MZ_ZIP_FLAG_IGNORE_PATH | MZ_ZIP_FLAG_CASE_SENSITIVE)) == 0) && (!pComment) && (pZip->m_pState->m_sorted_central_dir_offsets.m_size)) 0 0 0
((flags & (MZ_ZIP_FLAG_IGNORE_PATH | MZ_ZIP_FLAG_CASE_SENSITIVE)) == 0) && (!pComment) && (pZip->m_pState->m_sorted_central_dir_offsets.m_size)) 0 0 0
((flags & (MZ_ZIP_FLAG_IGNORE_PATH | MZ_ZIP_FLAG_CASE_SENSITIVE)) == 0) && (!pComment) && (pZip->m_pState->m_sorted_central_dir_offsets.m_size)) 0 0 0
4521 {
4522 0 return mz_zip_locate_file_binary_search(pZip, pName, pIndex);
4523 }
4524  
4525 /* Locate the entry by scanning the entire central directory */
4526 0 name_len = strlen(pName);
4527 0 ▶ if (name_len > MZ_UINT16_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (name_len > MZ_UINT16_MAX) 0 0 0
4528 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
4529  
4530 0 ▶ comment_len = pComment ? strlen(pComment) : 0;
Branch: 1 branch
BranchTrueFalseTotal
comment_len = pComment ? strlen(pComment) : 0; 0 0 0
4531 0 ▶ if (comment_len > MZ_UINT16_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (comment_len > MZ_UINT16_MAX) 0 0 0
4532 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
4533  
4534 0 ▶ for (file_index = 0; file_index < pZip->m_total_files; file_index++)
Branch: 1 branch
BranchTrueFalseTotal
for (file_index = 0; file_index < pZip->m_total_files; file_index++) 0 0 0
4535 {
4536 0 const mz_uint8 *pHeader = &MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir, mz_uint8, MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir_offsets, mz_uint32, file_index));
4537 0 mz_uint filename_len = MZ_READ_LE16(pHeader + MZ_ZIP_CDH_FILENAME_LEN_OFS);
4538 0 const char *pFilename = (const char *)pHeader + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE;
4539 0 ▶ if (filename_len < name_len)
Branch: 1 branch
BranchTrueFalseTotal
if (filename_len < name_len) 0 0 0
4540 0 continue;
4541 0 ▶ if (comment_len)
Branch: 1 branch
BranchTrueFalseTotal
if (comment_len) 0 0 0
4542 {
4543 0 mz_uint file_extra_len = MZ_READ_LE16(pHeader + MZ_ZIP_CDH_EXTRA_LEN_OFS), file_comment_len = MZ_READ_LE16(pHeader + MZ_ZIP_CDH_COMMENT_LEN_OFS);
4544 0 const char *pFile_comment = pFilename + filename_len + file_extra_len;
4545 0 ▶ if ((file_comment_len != comment_len) || (!mz_zip_string_equal(pComment, pFile_comment, file_comment_len, flags)))
Branch: 2 branches
BranchTrueFalseTotal
if ((file_comment_len != comment_len) || (!mz_zip_string_equal(pComment, pFile_comment, file_comment_len, flags))) 0 0 0
if ((file_comment_len != comment_len) || (!mz_zip_string_equal(pComment, pFile_comment, file_comment_len, flags))) 0 0 0
4546 0 continue;
4547 }
4548 0 ▶ if ((flags & MZ_ZIP_FLAG_IGNORE_PATH) && (filename_len))
Branch: 2 branches
BranchTrueFalseTotal
if ((flags & MZ_ZIP_FLAG_IGNORE_PATH) && (filename_len)) 0 0 0
if ((flags & MZ_ZIP_FLAG_IGNORE_PATH) && (filename_len)) 0 0 0
4549 {
4550 0 int ofs = filename_len - 1;
4551 do
4552 {
4553 0 ▶ if ((pFilename[ofs] == '/') || (pFilename[ofs] == '\\') || (pFilename[ofs] == ':'))
Branch: 3 branches
BranchTrueFalseTotal
if ((pFilename[ofs] == '/') || (pFilename[ofs] == '\\') || (pFilename[ofs] == ':')) 0 0 0
if ((pFilename[ofs] == '/') || (pFilename[ofs] == '\\') || (pFilename[ofs] == ':')) 0 0 0
if ((pFilename[ofs] == '/') || (pFilename[ofs] == '\\') || (pFilename[ofs] == ':')) 0 0 0
4554 break;
4555 0 ▶ } while (--ofs >= 0);
Branch: 1 branch
BranchTrueFalseTotal
} while (--ofs >= 0); 0 0 0
4556 0 ofs++;
4557 0 pFilename += ofs;
4558 0 filename_len -= ofs;
4559 }
4560 0 ▶ if ((filename_len == name_len) && (mz_zip_string_equal(pName, pFilename, filename_len, flags)))
Branch: 2 branches
BranchTrueFalseTotal
if ((filename_len == name_len) && (mz_zip_string_equal(pName, pFilename, filename_len, flags))) 0 0 0
if ((filename_len == name_len) && (mz_zip_string_equal(pName, pFilename, filename_len, flags))) 0 0 0
4561 {
4562 0 ▶ if (pIndex)
Branch: 1 branch
BranchTrueFalseTotal
if (pIndex) 0 0 0
4563 0 *pIndex = file_index;
4564 0 return MZ_TRUE;
4565 }
4566 }
4567  
4568 0 return mz_zip_set_error(pZip, MZ_ZIP_FILE_NOT_FOUND);
4569 }
4570  
4571 0 static mz_bool mz_zip_reader_extract_to_mem_no_alloc1(mz_zip_archive *pZip, mz_uint file_index, void *pBuf, size_t buf_size, mz_uint flags, void *pUser_read_buf, size_t user_read_buf_size, const mz_zip_archive_file_stat *st)
4572 {
4573 0 int status = TINFL_STATUS_DONE;
4574 0 mz_uint64 needed_size, cur_file_ofs, comp_remaining, out_buf_ofs = 0, read_buf_size, read_buf_ofs = 0, read_buf_avail;
4575 mz_zip_archive_file_stat file_stat;
4576 void *pRead_buf;
4577 mz_uint32 local_header_u32[(MZ_ZIP_LOCAL_DIR_HEADER_SIZE + sizeof(mz_uint32) - 1) / sizeof(mz_uint32)];
4578 0 mz_uint8 *pLocal_header = (mz_uint8 *)local_header_u32;
4579 tinfl_decompressor inflator;
4580  
4581 0 ▶ if ((!pZip) || (!pZip->m_pState) || ((buf_size) && (!pBuf)) || ((user_read_buf_size) && (!pUser_read_buf)) || (!pZip->m_pRead))
Branch: 7 branches
BranchTrueFalseTotal
if ((!pZip) || (!pZip->m_pState) || ((buf_size) && (!pBuf)) || ((user_read_buf_size) && (!pUser_read_buf)) || (!pZip->m_pRead)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || ((buf_size) && (!pBuf)) || ((user_read_buf_size) && (!pUser_read_buf)) || (!pZip->m_pRead)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || ((buf_size) && (!pBuf)) || ((user_read_buf_size) && (!pUser_read_buf)) || (!pZip->m_pRead)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || ((buf_size) && (!pBuf)) || ((user_read_buf_size) && (!pUser_read_buf)) || (!pZip->m_pRead)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || ((buf_size) && (!pBuf)) || ((user_read_buf_size) && (!pUser_read_buf)) || (!pZip->m_pRead)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || ((buf_size) && (!pBuf)) || ((user_read_buf_size) && (!pUser_read_buf)) || (!pZip->m_pRead)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || ((buf_size) && (!pBuf)) || ((user_read_buf_size) && (!pUser_read_buf)) || (!pZip->m_pRead)) 0 0 0
4582 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
4583  
4584 0 ▶ if (st)
Branch: 1 branch
BranchTrueFalseTotal
if (st) 0 0 0
4585 {
4586 0 file_stat = *st;
4587 }
4588 0 ▶ else if (!mz_zip_reader_file_stat(pZip, file_index, &file_stat))
Branch: 1 branch
BranchTrueFalseTotal
else if (!mz_zip_reader_file_stat(pZip, file_index, &file_stat)) 0 0 0
4589 0 return MZ_FALSE;
4590  
4591 /* A directory or zero length file */
4592 0 ▶ if ((file_stat.m_is_directory) || (!file_stat.m_comp_size))
Branch: 2 branches
BranchTrueFalseTotal
if ((file_stat.m_is_directory) || (!file_stat.m_comp_size)) 0 0 0
if ((file_stat.m_is_directory) || (!file_stat.m_comp_size)) 0 0 0
4593 0 return MZ_TRUE;
4594  
4595 /* Encryption and patch files are not supported. */
4596 0 ▶ if (file_stat.m_bit_flag & (MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_IS_ENCRYPTED | MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_USES_STRONG_ENCRYPTION | MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_COMPRESSED_PATCH_FLAG))
Branch: 1 branch
BranchTrueFalseTotal
if (file_stat.m_bit_flag & (MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_IS_ENCRYPTED | MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_USES_STRONG_ENCRYPTION | MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_COMPRESSED_PATCH_FLAG)) 0 0 0
4597 0 return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_ENCRYPTION);
4598  
4599 /* This function only supports decompressing stored and deflate. */
4600 0 ▶ if ((!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (file_stat.m_method != 0) && (file_stat.m_method != MZ_DEFLATED))
Branch: 3 branches
BranchTrueFalseTotal
if ((!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (file_stat.m_method != 0) && (file_stat.m_method != MZ_DEFLATED)) 0 0 0
if ((!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (file_stat.m_method != 0) && (file_stat.m_method != MZ_DEFLATED)) 0 0 0
if ((!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (file_stat.m_method != 0) && (file_stat.m_method != MZ_DEFLATED)) 0 0 0
4601 0 return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_METHOD);
4602  
4603 /* Ensure supplied output buffer is large enough. */
4604 0 ▶ needed_size = (flags & MZ_ZIP_FLAG_COMPRESSED_DATA) ? file_stat.m_comp_size : file_stat.m_uncomp_size;
Branch: 1 branch
BranchTrueFalseTotal
needed_size = (flags & MZ_ZIP_FLAG_COMPRESSED_DATA) ? file_stat.m_comp_size : file_stat.m_uncomp_size; 0 0 0
4605 0 ▶ if (buf_size < needed_size)
Branch: 1 branch
BranchTrueFalseTotal
if (buf_size < needed_size) 0 0 0
4606 0 return mz_zip_set_error(pZip, MZ_ZIP_BUF_TOO_SMALL);
4607  
4608 /* Read and parse the local directory entry. */
4609 0 cur_file_ofs = file_stat.m_local_header_ofs;
4610 0 ▶ if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pLocal_header, MZ_ZIP_LOCAL_DIR_HEADER_SIZE) != MZ_ZIP_LOCAL_DIR_HEADER_SIZE)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pLocal_header, MZ_ZIP_LOCAL_DIR_HEADER_SIZE) != MZ_ZIP_LOCAL_DIR_HEADER_SIZE) 0 0 0
4611 0 return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
4612  
4613 0 ▶ if (MZ_READ_LE32(pLocal_header) != MZ_ZIP_LOCAL_DIR_HEADER_SIG)
Branch: 1 branch
BranchTrueFalseTotal
if (MZ_READ_LE32(pLocal_header) != MZ_ZIP_LOCAL_DIR_HEADER_SIG) 0 0 0
4614 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
4615  
4616 0 cur_file_ofs += (mz_uint64)(MZ_ZIP_LOCAL_DIR_HEADER_SIZE) + MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_FILENAME_LEN_OFS) + MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_EXTRA_LEN_OFS);
4617 0 ▶ if ((cur_file_ofs + file_stat.m_comp_size) > pZip->m_archive_size)
Branch: 1 branch
BranchTrueFalseTotal
if ((cur_file_ofs + file_stat.m_comp_size) > pZip->m_archive_size) 0 0 0
4618 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
4619  
4620 0 ▶ if ((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!file_stat.m_method))
Branch: 2 branches
BranchTrueFalseTotal
if ((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!file_stat.m_method)) 0 0 0
if ((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!file_stat.m_method)) 0 0 0
4621 {
4622 /* The file is stored or the caller has requested the compressed data. */
4623 0 ▶ if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pBuf, (size_t)needed_size) != needed_size)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pBuf, (size_t)needed_size) != needed_size) 0 0 0
4624 0 return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
4625  
4626 #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
4627 0 ▶ if ((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) == 0) 0 0 0
4628 {
4629 0 ▶ if (mz_crc32(MZ_CRC32_INIT, (const mz_uint8 *)pBuf, (size_t)file_stat.m_uncomp_size) != file_stat.m_crc32)
Branch: 1 branch
BranchTrueFalseTotal
if (mz_crc32(MZ_CRC32_INIT, (const mz_uint8 *)pBuf, (size_t)file_stat.m_uncomp_size) != file_stat.m_crc32) 0 0 0
4630 0 return mz_zip_set_error(pZip, MZ_ZIP_CRC_CHECK_FAILED);
4631 }
4632 #endif
4633  
4634 0 return MZ_TRUE;
4635 }
4636  
4637 /* Decompress the file either directly from memory or from a file input buffer. */
4638 0 tinfl_init(&inflator);
4639  
4640 0 ▶ if (pZip->m_pState->m_pMem)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pState->m_pMem) 0 0 0
4641 {
4642 /* Read directly from the archive in memory. */
4643 0 pRead_buf = (mz_uint8 *)pZip->m_pState->m_pMem + cur_file_ofs;
4644 0 read_buf_size = read_buf_avail = file_stat.m_comp_size;
4645 0 comp_remaining = 0;
4646 }
4647 0 ▶ else if (pUser_read_buf)
Branch: 1 branch
BranchTrueFalseTotal
else if (pUser_read_buf) 0 0 0
4648 {
4649 /* Use a user provided read buffer. */
4650 0 ▶ if (!user_read_buf_size)
Branch: 1 branch
BranchTrueFalseTotal
if (!user_read_buf_size) 0 0 0
4651 0 return MZ_FALSE;
4652 0 pRead_buf = (mz_uint8 *)pUser_read_buf;
4653 0 read_buf_size = user_read_buf_size;
4654 0 read_buf_avail = 0;
4655 0 comp_remaining = file_stat.m_comp_size;
4656 }
4657 else
4658 {
4659 /* Temporarily allocate a read buffer. */
4660 0 read_buf_size = MZ_MIN(file_stat.m_comp_size, (mz_uint64)MZ_ZIP_MAX_IO_BUF_SIZE);
4661 if (((sizeof(size_t) == sizeof(mz_uint32))) && (read_buf_size > 0x7FFFFFFF))
4662 return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);
4663  
4664 0 ▶ if (NULL == (pRead_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, (size_t)read_buf_size)))
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == (pRead_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, (size_t)read_buf_size))) 0 0 0
4665 0 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
4666  
4667 0 read_buf_avail = 0;
4668 0 comp_remaining = file_stat.m_comp_size;
4669 }
4670  
4671 do
4672 {
4673 /* The size_t cast here should be OK because we've verified that the output buffer is >= file_stat.m_uncomp_size above */
4674 0 size_t in_buf_size, out_buf_size = (size_t)(file_stat.m_uncomp_size - out_buf_ofs);
4675 0 ▶ if ((!read_buf_avail) && (!pZip->m_pState->m_pMem))
Branch: 2 branches
BranchTrueFalseTotal
if ((!read_buf_avail) && (!pZip->m_pState->m_pMem)) 0 0 0
if ((!read_buf_avail) && (!pZip->m_pState->m_pMem)) 0 0 0
4676 {
4677 0 read_buf_avail = MZ_MIN(read_buf_size, comp_remaining);
4678 0 ▶ if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pRead_buf, (size_t)read_buf_avail) != read_buf_avail)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pRead_buf, (size_t)read_buf_avail) != read_buf_avail) 0 0 0
4679 {
4680 0 ▶ status = TINFL_STATUS_FAILED;
Branch: 1 branch
BranchTrueFalseTotal
status = TINFL_STATUS_FAILED; 0 0 0
4681 mz_zip_set_error(pZip, MZ_ZIP_DECOMPRESSION_FAILED);
4682 0 break;
4683 }
4684 0 cur_file_ofs += read_buf_avail;
4685 0 comp_remaining -= read_buf_avail;
4686 0 read_buf_ofs = 0;
4687 }
4688 0 in_buf_size = (size_t)read_buf_avail;
4689 0 ▶ status = tinfl_decompress(&inflator, (mz_uint8 *)pRead_buf + read_buf_ofs, &in_buf_size, (mz_uint8 *)pBuf, (mz_uint8 *)pBuf + out_buf_ofs, &out_buf_size, TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF | (comp_remaining ? TINFL_FLAG_HAS_MORE_INPUT : 0));
Branch: 1 branch
BranchTrueFalseTotal
status = tinfl_decompress(&inflator, (mz_uint8 *)pRead_buf + read_buf_ofs, &in_buf_size, (mz_uint8 *)pBuf, (mz_uint8 *)pBuf + out_buf_ofs, &out_buf_size, TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF | (comp_remaining ? TINFL_FLAG_HAS_MORE_INPUT : 0)); 0 0 0
4690 0 read_buf_avail -= in_buf_size;
4691 0 read_buf_ofs += in_buf_size;
4692 0 out_buf_ofs += out_buf_size;
4693 0 ▶ } while (status == TINFL_STATUS_NEEDS_MORE_INPUT);
Branch: 1 branch
BranchTrueFalseTotal
} while (status == TINFL_STATUS_NEEDS_MORE_INPUT); 0 0 0
4694  
4695 0 ▶ if (status == TINFL_STATUS_DONE)
Branch: 1 branch
BranchTrueFalseTotal
if (status == TINFL_STATUS_DONE) 0 0 0
4696 {
4697 /* Make sure the entire file was decompressed, and check its CRC. */
4698 0 ▶ if (out_buf_ofs != file_stat.m_uncomp_size)
Branch: 1 branch
BranchTrueFalseTotal
if (out_buf_ofs != file_stat.m_uncomp_size) 0 0 0
4699 {
4700 mz_zip_set_error(pZip, MZ_ZIP_UNEXPECTED_DECOMPRESSED_SIZE);
4701 0 status = TINFL_STATUS_FAILED;
4702 }
4703 #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
4704 0 ▶ else if (mz_crc32(MZ_CRC32_INIT, (const mz_uint8 *)pBuf, (size_t)file_stat.m_uncomp_size) != file_stat.m_crc32)
Branch: 1 branch
BranchTrueFalseTotal
else if (mz_crc32(MZ_CRC32_INIT, (const mz_uint8 *)pBuf, (size_t)file_stat.m_uncomp_size) != file_stat.m_crc32) 0 0 0
4705 {
4706 mz_zip_set_error(pZip, MZ_ZIP_CRC_CHECK_FAILED);
4707 0 status = TINFL_STATUS_FAILED;
4708 }
4709 #endif
4710 }
4711  
4712 0 ▶ if ((!pZip->m_pState->m_pMem) && (!pUser_read_buf))
Branch: 2 branches
BranchTrueFalseTotal
if ((!pZip->m_pState->m_pMem) && (!pUser_read_buf)) 0 0 0
if ((!pZip->m_pState->m_pMem) && (!pUser_read_buf)) 0 0 0
4713 0 pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf);
4714  
4715 0 return status == TINFL_STATUS_DONE;
4716 }
4717  
4718 0 mz_bool mz_zip_reader_extract_to_mem_no_alloc(mz_zip_archive *pZip, mz_uint file_index, void *pBuf, size_t buf_size, mz_uint flags, void *pUser_read_buf, size_t user_read_buf_size)
4719 {
4720 0 return mz_zip_reader_extract_to_mem_no_alloc1(pZip, file_index, pBuf, buf_size, flags, pUser_read_buf, user_read_buf_size, NULL);
4721 }
4722  
4723 0 mz_bool mz_zip_reader_extract_file_to_mem_no_alloc(mz_zip_archive *pZip, const char *pFilename, void *pBuf, size_t buf_size, mz_uint flags, void *pUser_read_buf, size_t user_read_buf_size)
4724 {
4725 mz_uint32 file_index;
4726 0 ▶ if (!mz_zip_reader_locate_file_v2(pZip, pFilename, NULL, flags, &file_index))
Branch: 1 branch
BranchTrueFalseTotal
if (!mz_zip_reader_locate_file_v2(pZip, pFilename, NULL, flags, &file_index)) 0 0 0
4727 0 return MZ_FALSE;
4728 0 return mz_zip_reader_extract_to_mem_no_alloc1(pZip, file_index, pBuf, buf_size, flags, pUser_read_buf, user_read_buf_size, NULL);
4729 }
4730  
4731 0 mz_bool mz_zip_reader_extract_to_mem(mz_zip_archive *pZip, mz_uint file_index, void *pBuf, size_t buf_size, mz_uint flags)
4732 {
4733 0 return mz_zip_reader_extract_to_mem_no_alloc1(pZip, file_index, pBuf, buf_size, flags, NULL, 0, NULL);
4734 }
4735  
4736 0 mz_bool mz_zip_reader_extract_file_to_mem(mz_zip_archive *pZip, const char *pFilename, void *pBuf, size_t buf_size, mz_uint flags)
4737 {
4738 0 return mz_zip_reader_extract_file_to_mem_no_alloc(pZip, pFilename, pBuf, buf_size, flags, NULL, 0);
4739 }
4740  
4741 0 void *mz_zip_reader_extract_to_heap(mz_zip_archive *pZip, mz_uint file_index, size_t *pSize, mz_uint flags)
4742 {
4743 mz_zip_archive_file_stat file_stat;
4744 mz_uint64 alloc_size;
4745 void *pBuf;
4746  
4747 0 ▶ if (pSize)
Branch: 1 branch
BranchTrueFalseTotal
if (pSize) 0 0 0
4748 0 *pSize = 0;
4749  
4750 0 ▶ if (!mz_zip_reader_file_stat(pZip, file_index, &file_stat))
Branch: 1 branch
BranchTrueFalseTotal
if (!mz_zip_reader_file_stat(pZip, file_index, &file_stat)) 0 0 0
4751 0 return NULL;
4752  
4753 0 ▶ alloc_size = (flags & MZ_ZIP_FLAG_COMPRESSED_DATA) ? file_stat.m_comp_size : file_stat.m_uncomp_size;
Branch: 1 branch
BranchTrueFalseTotal
alloc_size = (flags & MZ_ZIP_FLAG_COMPRESSED_DATA) ? file_stat.m_comp_size : file_stat.m_uncomp_size; 0 0 0
4754 if (((sizeof(size_t) == sizeof(mz_uint32))) && (alloc_size > 0x7FFFFFFF))
4755 {
4756 mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);
4757 return NULL;
4758 }
4759  
4760 0 ▶ if (NULL == (pBuf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, (size_t)alloc_size)))
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == (pBuf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, (size_t)alloc_size))) 0 0 0
4761 {
4762 mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
4763 0 return NULL;
4764 }
4765  
4766 0 ▶ if (!mz_zip_reader_extract_to_mem_no_alloc1(pZip, file_index, pBuf, (size_t)alloc_size, flags, NULL, 0, &file_stat))
Branch: 1 branch
BranchTrueFalseTotal
if (!mz_zip_reader_extract_to_mem_no_alloc1(pZip, file_index, pBuf, (size_t)alloc_size, flags, NULL, 0, &file_stat)) 0 0 0
4767 {
4768 0 pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf);
4769 0 return NULL;
4770 }
4771  
4772 0 ▶ if (pSize)
Branch: 1 branch
BranchTrueFalseTotal
if (pSize) 0 0 0
4773 0 *pSize = (size_t)alloc_size;
4774 0 return pBuf;
4775 }
4776  
4777 0 void *mz_zip_reader_extract_file_to_heap(mz_zip_archive *pZip, const char *pFilename, size_t *pSize, mz_uint flags)
4778 {
4779 mz_uint32 file_index;
4780 0 ▶ if (!mz_zip_reader_locate_file_v2(pZip, pFilename, NULL, flags, &file_index))
Branch: 1 branch
BranchTrueFalseTotal
if (!mz_zip_reader_locate_file_v2(pZip, pFilename, NULL, flags, &file_index)) 0 0 0
4781 {
4782 0 ▶ if (pSize)
Branch: 1 branch
BranchTrueFalseTotal
if (pSize) 0 0 0
4783 0 *pSize = 0;
4784 0 return MZ_FALSE;
4785 }
4786 0 return mz_zip_reader_extract_to_heap(pZip, file_index, pSize, flags);
4787 }
4788  
4789 0 mz_bool mz_zip_reader_extract_to_callback(mz_zip_archive *pZip, mz_uint file_index, mz_file_write_func pCallback, void *pOpaque, mz_uint flags)
4790 {
4791 0 int status = TINFL_STATUS_DONE;
4792 #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
4793 0 mz_uint file_crc32 = MZ_CRC32_INIT;
4794 #endif
4795 0 mz_uint64 read_buf_size, read_buf_ofs = 0, read_buf_avail, comp_remaining, out_buf_ofs = 0, cur_file_ofs;
4796 mz_zip_archive_file_stat file_stat;
4797 0 void *pRead_buf = NULL;
4798 0 void *pWrite_buf = NULL;
4799 mz_uint32 local_header_u32[(MZ_ZIP_LOCAL_DIR_HEADER_SIZE + sizeof(mz_uint32) - 1) / sizeof(mz_uint32)];
4800 0 mz_uint8 *pLocal_header = (mz_uint8 *)local_header_u32;
4801  
4802 0 ▶ if ((!pZip) || (!pZip->m_pState) || (!pCallback) || (!pZip->m_pRead))
Branch: 4 branches
BranchTrueFalseTotal
if ((!pZip) || (!pZip->m_pState) || (!pCallback) || (!pZip->m_pRead)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (!pCallback) || (!pZip->m_pRead)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (!pCallback) || (!pZip->m_pRead)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (!pCallback) || (!pZip->m_pRead)) 0 0 0
4803 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
4804  
4805 0 ▶ if (!mz_zip_reader_file_stat(pZip, file_index, &file_stat))
Branch: 1 branch
BranchTrueFalseTotal
if (!mz_zip_reader_file_stat(pZip, file_index, &file_stat)) 0 0 0
4806 0 return MZ_FALSE;
4807  
4808 /* A directory or zero length file */
4809 0 ▶ if ((file_stat.m_is_directory) || (!file_stat.m_comp_size))
Branch: 2 branches
BranchTrueFalseTotal
if ((file_stat.m_is_directory) || (!file_stat.m_comp_size)) 0 0 0
if ((file_stat.m_is_directory) || (!file_stat.m_comp_size)) 0 0 0
4810 0 return MZ_TRUE;
4811  
4812 /* Encryption and patch files are not supported. */
4813 0 ▶ if (file_stat.m_bit_flag & (MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_IS_ENCRYPTED | MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_USES_STRONG_ENCRYPTION | MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_COMPRESSED_PATCH_FLAG))
Branch: 1 branch
BranchTrueFalseTotal
if (file_stat.m_bit_flag & (MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_IS_ENCRYPTED | MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_USES_STRONG_ENCRYPTION | MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_COMPRESSED_PATCH_FLAG)) 0 0 0
4814 0 return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_ENCRYPTION);
4815  
4816 /* This function only supports decompressing stored and deflate. */
4817 0 ▶ if ((!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (file_stat.m_method != 0) && (file_stat.m_method != MZ_DEFLATED))
Branch: 3 branches
BranchTrueFalseTotal
if ((!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (file_stat.m_method != 0) && (file_stat.m_method != MZ_DEFLATED)) 0 0 0
if ((!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (file_stat.m_method != 0) && (file_stat.m_method != MZ_DEFLATED)) 0 0 0
if ((!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (file_stat.m_method != 0) && (file_stat.m_method != MZ_DEFLATED)) 0 0 0
4818 0 return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_METHOD);
4819  
4820 /* Read and do some minimal validation of the local directory entry (this doesn't crack the zip64 stuff, which we already have from the central dir) */
4821 0 cur_file_ofs = file_stat.m_local_header_ofs;
4822 0 ▶ if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pLocal_header, MZ_ZIP_LOCAL_DIR_HEADER_SIZE) != MZ_ZIP_LOCAL_DIR_HEADER_SIZE)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pLocal_header, MZ_ZIP_LOCAL_DIR_HEADER_SIZE) != MZ_ZIP_LOCAL_DIR_HEADER_SIZE) 0 0 0
4823 0 return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
4824  
4825 0 ▶ if (MZ_READ_LE32(pLocal_header) != MZ_ZIP_LOCAL_DIR_HEADER_SIG)
Branch: 1 branch
BranchTrueFalseTotal
if (MZ_READ_LE32(pLocal_header) != MZ_ZIP_LOCAL_DIR_HEADER_SIG) 0 0 0
4826 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
4827  
4828 0 cur_file_ofs += (mz_uint64)(MZ_ZIP_LOCAL_DIR_HEADER_SIZE) + MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_FILENAME_LEN_OFS) + MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_EXTRA_LEN_OFS);
4829 0 ▶ if ((cur_file_ofs + file_stat.m_comp_size) > pZip->m_archive_size)
Branch: 1 branch
BranchTrueFalseTotal
if ((cur_file_ofs + file_stat.m_comp_size) > pZip->m_archive_size) 0 0 0
4830 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
4831  
4832 /* Decompress the file either directly from memory or from a file input buffer. */
4833 0 ▶ if (pZip->m_pState->m_pMem)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pState->m_pMem) 0 0 0
4834 {
4835 0 pRead_buf = (mz_uint8 *)pZip->m_pState->m_pMem + cur_file_ofs;
4836 0 read_buf_size = read_buf_avail = file_stat.m_comp_size;
4837 0 comp_remaining = 0;
4838 }
4839 else
4840 {
4841 0 read_buf_size = MZ_MIN(file_stat.m_comp_size, (mz_uint64)MZ_ZIP_MAX_IO_BUF_SIZE);
4842 0 ▶ if (NULL == (pRead_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, (size_t)read_buf_size)))
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == (pRead_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, (size_t)read_buf_size))) 0 0 0
4843 0 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
4844  
4845 0 read_buf_avail = 0;
4846 0 comp_remaining = file_stat.m_comp_size;
4847 }
4848  
4849 0 ▶ if ((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!file_stat.m_method))
Branch: 2 branches
BranchTrueFalseTotal
if ((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!file_stat.m_method)) 0 0 0
if ((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!file_stat.m_method)) 0 0 0
4850 {
4851 /* The file is stored or the caller has requested the compressed data. */
4852 0 ▶ if (pZip->m_pState->m_pMem)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pState->m_pMem) 0 0 0
4853 {
4854 if (((sizeof(size_t) == sizeof(mz_uint32))) && (file_stat.m_comp_size > MZ_UINT32_MAX))
4855 return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);
4856  
4857 0 ▶ if (pCallback(pOpaque, out_buf_ofs, pRead_buf, (size_t)file_stat.m_comp_size) != file_stat.m_comp_size)
Branch: 1 branch
BranchTrueFalseTotal
if (pCallback(pOpaque, out_buf_ofs, pRead_buf, (size_t)file_stat.m_comp_size) != file_stat.m_comp_size) 0 0 0
4858 {
4859 mz_zip_set_error(pZip, MZ_ZIP_WRITE_CALLBACK_FAILED);
4860 0 status = TINFL_STATUS_FAILED;
4861 }
4862 0 ▶ else if (!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA))
Branch: 1 branch
BranchTrueFalseTotal
else if (!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) 0 0 0
4863 {
4864 #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
4865 0 file_crc32 = (mz_uint32)mz_crc32(file_crc32, (const mz_uint8 *)pRead_buf, (size_t)file_stat.m_comp_size);
4866 #endif
4867 }
4868  
4869 0 cur_file_ofs += file_stat.m_comp_size;
4870 0 out_buf_ofs += file_stat.m_comp_size;
4871 0 comp_remaining = 0;
4872 }
4873 else
4874 {
4875 0 ▶ while (comp_remaining)
Branch: 1 branch
BranchTrueFalseTotal
while (comp_remaining) 0 0 0
4876 {
4877 0 read_buf_avail = MZ_MIN(read_buf_size, comp_remaining);
4878 0 ▶ if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pRead_buf, (size_t)read_buf_avail) != read_buf_avail)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pRead_buf, (size_t)read_buf_avail) != read_buf_avail) 0 0 0
4879 {
4880 mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
4881 0 status = TINFL_STATUS_FAILED;
4882 0 break;
4883 }
4884  
4885 #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
4886 0 ▶ if (!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA))
Branch: 1 branch
BranchTrueFalseTotal
if (!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) 0 0 0
4887 {
4888 0 file_crc32 = (mz_uint32)mz_crc32(file_crc32, (const mz_uint8 *)pRead_buf, (size_t)read_buf_avail);
4889 }
4890 #endif
4891  
4892 0 ▶ if (pCallback(pOpaque, out_buf_ofs, pRead_buf, (size_t)read_buf_avail) != read_buf_avail)
Branch: 1 branch
BranchTrueFalseTotal
if (pCallback(pOpaque, out_buf_ofs, pRead_buf, (size_t)read_buf_avail) != read_buf_avail) 0 0 0
4893 {
4894 mz_zip_set_error(pZip, MZ_ZIP_WRITE_CALLBACK_FAILED);
4895 0 status = TINFL_STATUS_FAILED;
4896 0 break;
4897 }
4898  
4899 0 cur_file_ofs += read_buf_avail;
4900 0 out_buf_ofs += read_buf_avail;
4901 0 comp_remaining -= read_buf_avail;
4902 }
4903 }
4904 }
4905 else
4906 {
4907 tinfl_decompressor inflator;
4908 0 tinfl_init(&inflator);
4909  
4910 0 ▶ if (NULL == (pWrite_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, TINFL_LZ_DICT_SIZE)))
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == (pWrite_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, TINFL_LZ_DICT_SIZE))) 0 0 0
4911 {
4912 mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
4913 0 status = TINFL_STATUS_FAILED;
4914 }
4915 else
4916 {
4917 do
4918 {
4919 0 mz_uint8 *pWrite_buf_cur = (mz_uint8 *)pWrite_buf + (out_buf_ofs & (TINFL_LZ_DICT_SIZE - 1));
4920 0 size_t in_buf_size, out_buf_size = TINFL_LZ_DICT_SIZE - (out_buf_ofs & (TINFL_LZ_DICT_SIZE - 1));
4921 0 ▶ if ((!read_buf_avail) && (!pZip->m_pState->m_pMem))
Branch: 2 branches
BranchTrueFalseTotal
if ((!read_buf_avail) && (!pZip->m_pState->m_pMem)) 0 0 0
if ((!read_buf_avail) && (!pZip->m_pState->m_pMem)) 0 0 0
4922 {
4923 0 read_buf_avail = MZ_MIN(read_buf_size, comp_remaining);
4924 0 ▶ if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pRead_buf, (size_t)read_buf_avail) != read_buf_avail)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pRead_buf, (size_t)read_buf_avail) != read_buf_avail) 0 0 0
4925 {
4926 mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
4927 0 status = TINFL_STATUS_FAILED;
4928 0 break;
4929 }
4930 0 cur_file_ofs += read_buf_avail;
4931 0 comp_remaining -= read_buf_avail;
4932 0 read_buf_ofs = 0;
4933 }
4934  
4935 0 in_buf_size = (size_t)read_buf_avail;
4936 0 ▶ status = tinfl_decompress(&inflator, (const mz_uint8 *)pRead_buf + read_buf_ofs, &in_buf_size, (mz_uint8 *)pWrite_buf, pWrite_buf_cur, &out_buf_size, comp_remaining ? TINFL_FLAG_HAS_MORE_INPUT : 0);
Branch: 1 branch
BranchTrueFalseTotal
status = tinfl_decompress(&inflator, (const mz_uint8 *)pRead_buf + read_buf_ofs, &in_buf_size, (mz_uint8 *)pWrite_buf, pWrite_buf_cur, &out_buf_size, comp_remaining ? TINFL_FLAG_HAS_MORE_INPUT : 0); 0 0 0
4937 0 read_buf_avail -= in_buf_size;
4938 0 read_buf_ofs += in_buf_size;
4939  
4940 0 ▶ if (out_buf_size)
Branch: 1 branch
BranchTrueFalseTotal
if (out_buf_size) 0 0 0
4941 {
4942 0 ▶ if (pCallback(pOpaque, out_buf_ofs, pWrite_buf_cur, out_buf_size) != out_buf_size)
Branch: 1 branch
BranchTrueFalseTotal
if (pCallback(pOpaque, out_buf_ofs, pWrite_buf_cur, out_buf_size) != out_buf_size) 0 0 0
4943 {
4944 mz_zip_set_error(pZip, MZ_ZIP_WRITE_CALLBACK_FAILED);
4945 0 status = TINFL_STATUS_FAILED;
4946 0 break;
4947 }
4948  
4949 #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
4950 0 file_crc32 = (mz_uint32)mz_crc32(file_crc32, pWrite_buf_cur, out_buf_size);
4951 #endif
4952 0 ▶ if ((out_buf_ofs += out_buf_size) > file_stat.m_uncomp_size)
Branch: 1 branch
BranchTrueFalseTotal
if ((out_buf_ofs += out_buf_size) > file_stat.m_uncomp_size) 0 0 0
4953 {
4954 mz_zip_set_error(pZip, MZ_ZIP_DECOMPRESSION_FAILED);
4955 0 status = TINFL_STATUS_FAILED;
4956 0 break;
4957 }
4958 }
4959 0 ▶ } while ((status == TINFL_STATUS_NEEDS_MORE_INPUT) || (status == TINFL_STATUS_HAS_MORE_OUTPUT));
Branch: 2 branches
BranchTrueFalseTotal
} while ((status == TINFL_STATUS_NEEDS_MORE_INPUT) || (status == TINFL_STATUS_HAS_MORE_OUTPUT)); 0 0 0
} while ((status == TINFL_STATUS_NEEDS_MORE_INPUT) || (status == TINFL_STATUS_HAS_MORE_OUTPUT)); 0 0 0
4960 }
4961 }
4962  
4963 0 ▶ if ((status == TINFL_STATUS_DONE) && (!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)))
Branch: 2 branches
BranchTrueFalseTotal
if ((status == TINFL_STATUS_DONE) && (!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA))) 0 0 0
if ((status == TINFL_STATUS_DONE) && (!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA))) 0 0 0
4964 {
4965 /* Make sure the entire file was decompressed, and check its CRC. */
4966 0 ▶ if (out_buf_ofs != file_stat.m_uncomp_size)
Branch: 1 branch
BranchTrueFalseTotal
if (out_buf_ofs != file_stat.m_uncomp_size) 0 0 0
4967 {
4968 mz_zip_set_error(pZip, MZ_ZIP_UNEXPECTED_DECOMPRESSED_SIZE);
4969 0 status = TINFL_STATUS_FAILED;
4970 }
4971 #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
4972 0 ▶ else if (file_crc32 != file_stat.m_crc32)
Branch: 1 branch
BranchTrueFalseTotal
else if (file_crc32 != file_stat.m_crc32) 0 0 0
4973 {
4974 mz_zip_set_error(pZip, MZ_ZIP_DECOMPRESSION_FAILED);
4975 0 status = TINFL_STATUS_FAILED;
4976 }
4977 #endif
4978 }
4979  
4980 0 ▶ if (!pZip->m_pState->m_pMem)
Branch: 1 branch
BranchTrueFalseTotal
if (!pZip->m_pState->m_pMem) 0 0 0
4981 0 pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf);
4982  
4983 0 ▶ if (pWrite_buf)
Branch: 1 branch
BranchTrueFalseTotal
if (pWrite_buf) 0 0 0
4984 0 pZip->m_pFree(pZip->m_pAlloc_opaque, pWrite_buf);
4985  
4986 0 return status == TINFL_STATUS_DONE;
4987 }
4988  
4989 0 mz_bool mz_zip_reader_extract_file_to_callback(mz_zip_archive *pZip, const char *pFilename, mz_file_write_func pCallback, void *pOpaque, mz_uint flags)
4990 {
4991 mz_uint32 file_index;
4992 0 ▶ if (!mz_zip_reader_locate_file_v2(pZip, pFilename, NULL, flags, &file_index))
Branch: 1 branch
BranchTrueFalseTotal
if (!mz_zip_reader_locate_file_v2(pZip, pFilename, NULL, flags, &file_index)) 0 0 0
4993 0 return MZ_FALSE;
4994  
4995 0 return mz_zip_reader_extract_to_callback(pZip, file_index, pCallback, pOpaque, flags);
4996 }
4997  
4998 0 mz_zip_reader_extract_iter_state *mz_zip_reader_extract_iter_new(mz_zip_archive *pZip, mz_uint file_index, mz_uint flags)
4999 {
5000 mz_zip_reader_extract_iter_state *pState;
5001 mz_uint32 local_header_u32[(MZ_ZIP_LOCAL_DIR_HEADER_SIZE + sizeof(mz_uint32) - 1) / sizeof(mz_uint32)];
5002 0 mz_uint8 *pLocal_header = (mz_uint8 *)local_header_u32;
5003  
5004 /* Argument sanity check */
5005 0 ▶ if ((!pZip) || (!pZip->m_pState))
Branch: 2 branches
BranchTrueFalseTotal
if ((!pZip) || (!pZip->m_pState)) 0 0 0
if ((!pZip) || (!pZip->m_pState)) 0 0 0
5006 0 return NULL;
5007  
5008 /* Allocate an iterator status structure */
5009 0 pState = (mz_zip_reader_extract_iter_state *)pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, sizeof(mz_zip_reader_extract_iter_state));
5010 0 ▶ if (!pState)
Branch: 1 branch
BranchTrueFalseTotal
if (!pState) 0 0 0
5011 {
5012 mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
5013 0 return NULL;
5014 }
5015  
5016 /* Fetch file details */
5017 0 ▶ if (!mz_zip_reader_file_stat(pZip, file_index, &pState->file_stat))
Branch: 1 branch
BranchTrueFalseTotal
if (!mz_zip_reader_file_stat(pZip, file_index, &pState->file_stat)) 0 0 0
5018 {
5019 0 pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
5020 0 return NULL;
5021 }
5022  
5023 /* Encryption and patch files are not supported. */
5024 0 ▶ if (pState->file_stat.m_bit_flag & (MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_IS_ENCRYPTED | MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_USES_STRONG_ENCRYPTION | MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_COMPRESSED_PATCH_FLAG))
Branch: 1 branch
BranchTrueFalseTotal
if (pState->file_stat.m_bit_flag & (MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_IS_ENCRYPTED | MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_USES_STRONG_ENCRYPTION | MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_COMPRESSED_PATCH_FLAG)) 0 0 0
5025 {
5026 mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_ENCRYPTION);
5027 0 pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
5028 0 return NULL;
5029 }
5030  
5031 /* This function only supports decompressing stored and deflate. */
5032 0 ▶ if ((!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (pState->file_stat.m_method != 0) && (pState->file_stat.m_method != MZ_DEFLATED))
Branch: 3 branches
BranchTrueFalseTotal
if ((!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (pState->file_stat.m_method != 0) && (pState->file_stat.m_method != MZ_DEFLATED)) 0 0 0
if ((!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (pState->file_stat.m_method != 0) && (pState->file_stat.m_method != MZ_DEFLATED)) 0 0 0
if ((!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (pState->file_stat.m_method != 0) && (pState->file_stat.m_method != MZ_DEFLATED)) 0 0 0
5033 {
5034 mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_METHOD);
5035 0 pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
5036 0 return NULL;
5037 }
5038  
5039 /* Init state - save args */
5040 0 pState->pZip = pZip;
5041 0 pState->flags = flags;
5042  
5043 /* Init state - reset variables to defaults */
5044 0 pState->status = TINFL_STATUS_DONE;
5045 #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
5046 0 pState->file_crc32 = MZ_CRC32_INIT;
5047 #endif
5048 0 pState->read_buf_ofs = 0;
5049 0 pState->out_buf_ofs = 0;
5050 0 pState->pRead_buf = NULL;
5051 0 pState->pWrite_buf = NULL;
5052 0 pState->out_blk_remain = 0;
5053  
5054 /* Read and parse the local directory entry. */
5055 0 pState->cur_file_ofs = pState->file_stat.m_local_header_ofs;
5056 0 ▶ if (pZip->m_pRead(pZip->m_pIO_opaque, pState->cur_file_ofs, pLocal_header, MZ_ZIP_LOCAL_DIR_HEADER_SIZE) != MZ_ZIP_LOCAL_DIR_HEADER_SIZE)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pRead(pZip->m_pIO_opaque, pState->cur_file_ofs, pLocal_header, MZ_ZIP_LOCAL_DIR_HEADER_SIZE) != MZ_ZIP_LOCAL_DIR_HEADER_SIZE) 0 0 0
5057 {
5058 mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
5059 0 pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
5060 0 return NULL;
5061 }
5062  
5063 0 ▶ if (MZ_READ_LE32(pLocal_header) != MZ_ZIP_LOCAL_DIR_HEADER_SIG)
Branch: 1 branch
BranchTrueFalseTotal
if (MZ_READ_LE32(pLocal_header) != MZ_ZIP_LOCAL_DIR_HEADER_SIG) 0 0 0
5064 {
5065 mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
5066 0 pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
5067 0 return NULL;
5068 }
5069  
5070 0 pState->cur_file_ofs += (mz_uint64)(MZ_ZIP_LOCAL_DIR_HEADER_SIZE) + MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_FILENAME_LEN_OFS) + MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_EXTRA_LEN_OFS);
5071 0 ▶ if ((pState->cur_file_ofs + pState->file_stat.m_comp_size) > pZip->m_archive_size)
Branch: 1 branch
BranchTrueFalseTotal
if ((pState->cur_file_ofs + pState->file_stat.m_comp_size) > pZip->m_archive_size) 0 0 0
5072 {
5073 mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
5074 0 pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
5075 0 return NULL;
5076 }
5077  
5078 /* Decompress the file either directly from memory or from a file input buffer. */
5079 0 ▶ if (pZip->m_pState->m_pMem)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pState->m_pMem) 0 0 0
5080 {
5081 0 pState->pRead_buf = (mz_uint8 *)pZip->m_pState->m_pMem + pState->cur_file_ofs;
5082 0 pState->read_buf_size = pState->read_buf_avail = pState->file_stat.m_comp_size;
5083 0 pState->comp_remaining = pState->file_stat.m_comp_size;
5084 }
5085 else
5086 {
5087 0 ▶ if (!((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!pState->file_stat.m_method)))
Branch: 2 branches
BranchTrueFalseTotal
if (!((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!pState->file_stat.m_method))) 0 0 0
if (!((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!pState->file_stat.m_method))) 0 0 0
5088 {
5089 /* Decompression required, therefore intermediate read buffer required */
5090 0 pState->read_buf_size = MZ_MIN(pState->file_stat.m_comp_size, (mz_uint64)MZ_ZIP_MAX_IO_BUF_SIZE);
5091 0 ▶ if (NULL == (pState->pRead_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, (size_t)pState->read_buf_size)))
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == (pState->pRead_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, (size_t)pState->read_buf_size))) 0 0 0
5092 {
5093 mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
5094 0 pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
5095 0 return NULL;
5096 }
5097 }
5098 else
5099 {
5100 /* Decompression not required - we will be reading directly into user buffer, no temp buf required */
5101 0 pState->read_buf_size = 0;
5102 }
5103 0 pState->read_buf_avail = 0;
5104 0 pState->comp_remaining = pState->file_stat.m_comp_size;
5105 }
5106  
5107 0 ▶ if (!((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!pState->file_stat.m_method)))
Branch: 2 branches
BranchTrueFalseTotal
if (!((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!pState->file_stat.m_method))) 0 0 0
if (!((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!pState->file_stat.m_method))) 0 0 0
5108 {
5109 /* Decompression required, init decompressor */
5110 0 tinfl_init(&pState->inflator);
5111  
5112 /* Allocate write buffer */
5113 0 ▶ if (NULL == (pState->pWrite_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, TINFL_LZ_DICT_SIZE)))
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == (pState->pWrite_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, TINFL_LZ_DICT_SIZE))) 0 0 0
5114 {
5115 mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
5116 0 ▶ if (pState->pRead_buf)
Branch: 1 branch
BranchTrueFalseTotal
if (pState->pRead_buf) 0 0 0
5117 0 pZip->m_pFree(pZip->m_pAlloc_opaque, pState->pRead_buf);
5118 0 pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
5119 0 return NULL;
5120 }
5121 }
5122  
5123 0 return pState;
5124 }
5125  
5126 0 mz_zip_reader_extract_iter_state *mz_zip_reader_extract_file_iter_new(mz_zip_archive *pZip, const char *pFilename, mz_uint flags)
5127 {
5128 mz_uint32 file_index;
5129  
5130 /* Locate file index by name */
5131 0 ▶ if (!mz_zip_reader_locate_file_v2(pZip, pFilename, NULL, flags, &file_index))
Branch: 1 branch
BranchTrueFalseTotal
if (!mz_zip_reader_locate_file_v2(pZip, pFilename, NULL, flags, &file_index)) 0 0 0
5132 0 return NULL;
5133  
5134 /* Construct iterator */
5135 0 return mz_zip_reader_extract_iter_new(pZip, file_index, flags);
5136 }
5137  
5138 0 size_t mz_zip_reader_extract_iter_read(mz_zip_reader_extract_iter_state *pState, void *pvBuf, size_t buf_size)
5139 {
5140 0 size_t copied_to_caller = 0;
5141  
5142 /* Argument sanity check */
5143 0 ▶ if ((!pState) || (!pState->pZip) || (!pState->pZip->m_pState) || (!pvBuf))
Branch: 4 branches
BranchTrueFalseTotal
if ((!pState) || (!pState->pZip) || (!pState->pZip->m_pState) || (!pvBuf)) 0 0 0
if ((!pState) || (!pState->pZip) || (!pState->pZip->m_pState) || (!pvBuf)) 0 0 0
if ((!pState) || (!pState->pZip) || (!pState->pZip->m_pState) || (!pvBuf)) 0 0 0
if ((!pState) || (!pState->pZip) || (!pState->pZip->m_pState) || (!pvBuf)) 0 0 0
5144 0 return 0;
5145  
5146 0 ▶ if ((pState->flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!pState->file_stat.m_method))
Branch: 2 branches
BranchTrueFalseTotal
if ((pState->flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!pState->file_stat.m_method)) 0 0 0
if ((pState->flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!pState->file_stat.m_method)) 0 0 0
5147 {
5148 /* The file is stored or the caller has requested the compressed data, calc amount to return. */
5149 0 copied_to_caller = (size_t)MZ_MIN(buf_size, pState->comp_remaining);
5150  
5151 /* Zip is in memory....or requires reading from a file? */
5152 0 ▶ if (pState->pZip->m_pState->m_pMem)
Branch: 1 branch
BranchTrueFalseTotal
if (pState->pZip->m_pState->m_pMem) 0 0 0
5153 {
5154 /* Copy data to caller's buffer */
5155 0 memcpy(pvBuf, pState->pRead_buf, copied_to_caller);
5156 0 pState->pRead_buf = ((mz_uint8 *)pState->pRead_buf) + copied_to_caller;
5157 }
5158 else
5159 {
5160 /* Read directly into caller's buffer */
5161 0 ▶ if (pState->pZip->m_pRead(pState->pZip->m_pIO_opaque, pState->cur_file_ofs, pvBuf, copied_to_caller) != copied_to_caller)
Branch: 1 branch
BranchTrueFalseTotal
if (pState->pZip->m_pRead(pState->pZip->m_pIO_opaque, pState->cur_file_ofs, pvBuf, copied_to_caller) != copied_to_caller) 0 0 0
5162 {
5163 /* Failed to read all that was asked for, flag failure and alert user */
5164 0 ▶ mz_zip_set_error(pState->pZip, MZ_ZIP_FILE_READ_FAILED);
Branch: 1 branch
BranchTrueFalseTotal
mz_zip_set_error(pState->pZip, MZ_ZIP_FILE_READ_FAILED); 0 0 0
5165 0 pState->status = TINFL_STATUS_FAILED;
5166 0 copied_to_caller = 0;
5167 }
5168 }
5169  
5170 #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
5171 /* Compute CRC if not returning compressed data only */
5172 0 ▶ if (!(pState->flags & MZ_ZIP_FLAG_COMPRESSED_DATA))
Branch: 1 branch
BranchTrueFalseTotal
if (!(pState->flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) 0 0 0
5173 0 pState->file_crc32 = (mz_uint32)mz_crc32(pState->file_crc32, (const mz_uint8 *)pvBuf, copied_to_caller);
5174 #endif
5175  
5176 /* Advance offsets, dec counters */
5177 0 pState->cur_file_ofs += copied_to_caller;
5178 0 pState->out_buf_ofs += copied_to_caller;
5179 0 pState->comp_remaining -= copied_to_caller;
5180 }
5181 else
5182 {
5183 do
5184 {
5185 /* Calc ptr to write buffer - given current output pos and block size */
5186 0 mz_uint8 *pWrite_buf_cur = (mz_uint8 *)pState->pWrite_buf + (pState->out_buf_ofs & (TINFL_LZ_DICT_SIZE - 1));
5187  
5188 /* Calc max output size - given current output pos and block size */
5189 0 size_t in_buf_size, out_buf_size = TINFL_LZ_DICT_SIZE - (pState->out_buf_ofs & (TINFL_LZ_DICT_SIZE - 1));
5190  
5191 0 ▶ if (!pState->out_blk_remain)
Branch: 1 branch
BranchTrueFalseTotal
if (!pState->out_blk_remain) 0 0 0
5192 {
5193 /* Read more data from file if none available (and reading from file) */
5194 0 ▶ if ((!pState->read_buf_avail) && (!pState->pZip->m_pState->m_pMem))
Branch: 2 branches
BranchTrueFalseTotal
if ((!pState->read_buf_avail) && (!pState->pZip->m_pState->m_pMem)) 0 0 0
if ((!pState->read_buf_avail) && (!pState->pZip->m_pState->m_pMem)) 0 0 0
5195 {
5196 /* Calc read size */
5197 0 pState->read_buf_avail = MZ_MIN(pState->read_buf_size, pState->comp_remaining);
5198 0 ▶ if (pState->pZip->m_pRead(pState->pZip->m_pIO_opaque, pState->cur_file_ofs, pState->pRead_buf, (size_t)pState->read_buf_avail) != pState->read_buf_avail)
Branch: 1 branch
BranchTrueFalseTotal
if (pState->pZip->m_pRead(pState->pZip->m_pIO_opaque, pState->cur_file_ofs, pState->pRead_buf, (size_t)pState->read_buf_avail) != pState->read_buf_avail) 0 0 0
5199 {
5200 0 ▶ mz_zip_set_error(pState->pZip, MZ_ZIP_FILE_READ_FAILED);
Branch: 1 branch
BranchTrueFalseTotal
mz_zip_set_error(pState->pZip, MZ_ZIP_FILE_READ_FAILED); 0 0 0
5201 0 pState->status = TINFL_STATUS_FAILED;
5202 0 break;
5203 }
5204  
5205 /* Advance offsets, dec counters */
5206 0 pState->cur_file_ofs += pState->read_buf_avail;
5207 0 pState->comp_remaining -= pState->read_buf_avail;
5208 0 pState->read_buf_ofs = 0;
5209 }
5210  
5211 /* Perform decompression */
5212 0 in_buf_size = (size_t)pState->read_buf_avail;
5213 0 ▶ pState->status = tinfl_decompress(&pState->inflator, (const mz_uint8 *)pState->pRead_buf + pState->read_buf_ofs, &in_buf_size, (mz_uint8 *)pState->pWrite_buf, pWrite_buf_cur, &out_buf_size, pState->comp_remaining ? TINFL_FLAG_HAS_MORE_INPUT : 0);
Branch: 1 branch
BranchTrueFalseTotal
pState->status = tinfl_decompress(&pState->inflator, (const mz_uint8 *)pState->pRead_buf + pState->read_buf_ofs, &in_buf_size, (mz_uint8 *)pState->pWrite_buf, pWrite_buf_cur, &out_buf_size, pState->comp_remaining ? TINFL_FLAG_HAS_MORE_INPUT : 0); 0 0 0
5214 0 pState->read_buf_avail -= in_buf_size;
5215 0 pState->read_buf_ofs += in_buf_size;
5216  
5217 /* Update current output block size remaining */
5218 0 pState->out_blk_remain = out_buf_size;
5219 }
5220  
5221 0 ▶ if (pState->out_blk_remain)
Branch: 1 branch
BranchTrueFalseTotal
if (pState->out_blk_remain) 0 0 0
5222 {
5223 /* Calc amount to return. */
5224 0 size_t to_copy = MZ_MIN((buf_size - copied_to_caller), pState->out_blk_remain);
5225  
5226 /* Copy data to caller's buffer */
5227 0 memcpy((mz_uint8 *)pvBuf + copied_to_caller, pWrite_buf_cur, to_copy);
5228  
5229 #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
5230 /* Perform CRC */
5231 0 pState->file_crc32 = (mz_uint32)mz_crc32(pState->file_crc32, pWrite_buf_cur, to_copy);
5232 #endif
5233  
5234 /* Decrement data consumed from block */
5235 0 pState->out_blk_remain -= to_copy;
5236  
5237 /* Inc output offset, while performing sanity check */
5238 0 ▶ if ((pState->out_buf_ofs += to_copy) > pState->file_stat.m_uncomp_size)
Branch: 1 branch
BranchTrueFalseTotal
if ((pState->out_buf_ofs += to_copy) > pState->file_stat.m_uncomp_size) 0 0 0
5239 {
5240 0 ▶ mz_zip_set_error(pState->pZip, MZ_ZIP_DECOMPRESSION_FAILED);
Branch: 1 branch
BranchTrueFalseTotal
mz_zip_set_error(pState->pZip, MZ_ZIP_DECOMPRESSION_FAILED); 0 0 0
5241 0 pState->status = TINFL_STATUS_FAILED;
5242 0 break;
5243 }
5244  
5245 /* Increment counter of data copied to caller */
5246 0 copied_to_caller += to_copy;
5247 }
5248 0 ▶ } while ((copied_to_caller < buf_size) && ((pState->status == TINFL_STATUS_NEEDS_MORE_INPUT) || (pState->status == TINFL_STATUS_HAS_MORE_OUTPUT)));
Branch: 3 branches
BranchTrueFalseTotal
} while ((copied_to_caller < buf_size) && ((pState->status == TINFL_STATUS_NEEDS_MORE_INPUT) || (pState->status == TINFL_STATUS_HAS_MORE_OUTPUT))); 0 0 0
} while ((copied_to_caller < buf_size) && ((pState->status == TINFL_STATUS_NEEDS_MORE_INPUT) || (pState->status == TINFL_STATUS_HAS_MORE_OUTPUT))); 0 0 0
} while ((copied_to_caller < buf_size) && ((pState->status == TINFL_STATUS_NEEDS_MORE_INPUT) || (pState->status == TINFL_STATUS_HAS_MORE_OUTPUT))); 0 0 0
5249 }
5250  
5251 /* Return how many bytes were copied into user buffer */
5252 0 return copied_to_caller;
5253 }
5254  
5255 0 mz_bool mz_zip_reader_extract_iter_free(mz_zip_reader_extract_iter_state *pState)
5256 {
5257 int status;
5258  
5259 /* Argument sanity check */
5260 0 ▶ if ((!pState) || (!pState->pZip) || (!pState->pZip->m_pState))
Branch: 3 branches
BranchTrueFalseTotal
if ((!pState) || (!pState->pZip) || (!pState->pZip->m_pState)) 0 0 0
if ((!pState) || (!pState->pZip) || (!pState->pZip->m_pState)) 0 0 0
if ((!pState) || (!pState->pZip) || (!pState->pZip->m_pState)) 0 0 0
5261 0 return MZ_FALSE;
5262  
5263 /* Was decompression completed and requested? */
5264 0 ▶ if ((pState->status == TINFL_STATUS_DONE) && (!(pState->flags & MZ_ZIP_FLAG_COMPRESSED_DATA)))
Branch: 2 branches
BranchTrueFalseTotal
if ((pState->status == TINFL_STATUS_DONE) && (!(pState->flags & MZ_ZIP_FLAG_COMPRESSED_DATA))) 0 0 0
if ((pState->status == TINFL_STATUS_DONE) && (!(pState->flags & MZ_ZIP_FLAG_COMPRESSED_DATA))) 0 0 0
5265 {
5266 /* Make sure the entire file was decompressed, and check its CRC. */
5267 0 ▶ if (pState->out_buf_ofs != pState->file_stat.m_uncomp_size)
Branch: 1 branch
BranchTrueFalseTotal
if (pState->out_buf_ofs != pState->file_stat.m_uncomp_size) 0 0 0
5268 {
5269 0 ▶ mz_zip_set_error(pState->pZip, MZ_ZIP_UNEXPECTED_DECOMPRESSED_SIZE);
Branch: 1 branch
BranchTrueFalseTotal
mz_zip_set_error(pState->pZip, MZ_ZIP_UNEXPECTED_DECOMPRESSED_SIZE); 0 0 0
5270 0 pState->status = TINFL_STATUS_FAILED;
5271 }
5272 #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
5273 0 ▶ else if (pState->file_crc32 != pState->file_stat.m_crc32)
Branch: 1 branch
BranchTrueFalseTotal
else if (pState->file_crc32 != pState->file_stat.m_crc32) 0 0 0
5274 {
5275 0 ▶ mz_zip_set_error(pState->pZip, MZ_ZIP_DECOMPRESSION_FAILED);
Branch: 1 branch
BranchTrueFalseTotal
mz_zip_set_error(pState->pZip, MZ_ZIP_DECOMPRESSION_FAILED); 0 0 0
5276 0 pState->status = TINFL_STATUS_FAILED;
5277 }
5278 #endif
5279 }
5280  
5281 /* Free buffers */
5282 0 ▶ if (!pState->pZip->m_pState->m_pMem)
Branch: 1 branch
BranchTrueFalseTotal
if (!pState->pZip->m_pState->m_pMem) 0 0 0
5283 0 pState->pZip->m_pFree(pState->pZip->m_pAlloc_opaque, pState->pRead_buf);
5284 0 ▶ if (pState->pWrite_buf)
Branch: 1 branch
BranchTrueFalseTotal
if (pState->pWrite_buf) 0 0 0
5285 0 pState->pZip->m_pFree(pState->pZip->m_pAlloc_opaque, pState->pWrite_buf);
5286  
5287 /* Save status */
5288 0 status = pState->status;
5289  
5290 /* Free context */
5291 0 pState->pZip->m_pFree(pState->pZip->m_pAlloc_opaque, pState);
5292  
5293 0 return status == TINFL_STATUS_DONE;
5294 }
5295  
5296 #ifndef MINIZ_NO_STDIO
5297 static size_t mz_zip_file_write_callback(void *pOpaque, mz_uint64 ofs, const void *pBuf, size_t n)
5298 {
5299 (void)ofs;
5300  
5301 return MZ_FWRITE(pBuf, 1, n, (MZ_FILE *)pOpaque);
5302 }
5303  
5304 mz_bool mz_zip_reader_extract_to_file(mz_zip_archive *pZip, mz_uint file_index, const char *pDst_filename, mz_uint flags)
5305 {
5306 mz_bool status;
5307 mz_zip_archive_file_stat file_stat;
5308 MZ_FILE *pFile;
5309  
5310 if (!mz_zip_reader_file_stat(pZip, file_index, &file_stat))
5311 return MZ_FALSE;
5312  
5313 if ((file_stat.m_is_directory) || (!file_stat.m_is_supported))
5314 return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_FEATURE);
5315  
5316 pFile = MZ_FOPEN(pDst_filename, "wb");
5317 if (!pFile)
5318 return mz_zip_set_error(pZip, MZ_ZIP_FILE_OPEN_FAILED);
5319  
5320 status = mz_zip_reader_extract_to_callback(pZip, file_index, mz_zip_file_write_callback, pFile, flags);
5321  
5322 if (MZ_FCLOSE(pFile) == EOF)
5323 {
5324 if (status)
5325 mz_zip_set_error(pZip, MZ_ZIP_FILE_CLOSE_FAILED);
5326  
5327 status = MZ_FALSE;
5328 }
5329  
5330 #if !defined(MINIZ_NO_TIME) && !defined(MINIZ_NO_STDIO)
5331 if (status)
5332 mz_zip_set_file_times(pDst_filename, file_stat.m_time, file_stat.m_time);
5333 #endif
5334  
5335 return status;
5336 }
5337  
5338 mz_bool mz_zip_reader_extract_file_to_file(mz_zip_archive *pZip, const char *pArchive_filename, const char *pDst_filename, mz_uint flags)
5339 {
5340 mz_uint32 file_index;
5341 if (!mz_zip_reader_locate_file_v2(pZip, pArchive_filename, NULL, flags, &file_index))
5342 return MZ_FALSE;
5343  
5344 return mz_zip_reader_extract_to_file(pZip, file_index, pDst_filename, flags);
5345 }
5346  
5347 mz_bool mz_zip_reader_extract_to_cfile(mz_zip_archive *pZip, mz_uint file_index, MZ_FILE *pFile, mz_uint flags)
5348 {
5349 mz_zip_archive_file_stat file_stat;
5350  
5351 if (!mz_zip_reader_file_stat(pZip, file_index, &file_stat))
5352 return MZ_FALSE;
5353  
5354 if ((file_stat.m_is_directory) || (!file_stat.m_is_supported))
5355 return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_FEATURE);
5356  
5357 return mz_zip_reader_extract_to_callback(pZip, file_index, mz_zip_file_write_callback, pFile, flags);
5358 }
5359  
5360 mz_bool mz_zip_reader_extract_file_to_cfile(mz_zip_archive *pZip, const char *pArchive_filename, MZ_FILE *pFile, mz_uint flags)
5361 {
5362 mz_uint32 file_index;
5363 if (!mz_zip_reader_locate_file_v2(pZip, pArchive_filename, NULL, flags, &file_index))
5364 return MZ_FALSE;
5365  
5366 return mz_zip_reader_extract_to_cfile(pZip, file_index, pFile, flags);
5367 }
5368 #endif /* #ifndef MINIZ_NO_STDIO */
5369  
5370 0 static size_t mz_zip_compute_crc32_callback(void *pOpaque, mz_uint64 file_ofs, const void *pBuf, size_t n)
5371 {
5372 0 mz_uint32 *p = (mz_uint32 *)pOpaque;
5373 (void)file_ofs;
5374 0 *p = (mz_uint32)mz_crc32(*p, (const mz_uint8 *)pBuf, n);
5375 0 return n;
5376 }
5377  
5378 0 mz_bool mz_zip_validate_file(mz_zip_archive *pZip, mz_uint file_index, mz_uint flags)
5379 {
5380 mz_zip_archive_file_stat file_stat;
5381 mz_zip_internal_state *pState;
5382 const mz_uint8 *pCentral_dir_header;
5383 0 mz_bool found_zip64_ext_data_in_cdir = MZ_FALSE;
5384 0 mz_bool found_zip64_ext_data_in_ldir = MZ_FALSE;
5385 mz_uint32 local_header_u32[(MZ_ZIP_LOCAL_DIR_HEADER_SIZE + sizeof(mz_uint32) - 1) / sizeof(mz_uint32)];
5386 0 mz_uint8 *pLocal_header = (mz_uint8 *)local_header_u32;
5387 0 mz_uint64 local_header_ofs = 0;
5388 mz_uint32 local_header_filename_len, local_header_extra_len, local_header_crc32;
5389 mz_uint64 local_header_comp_size, local_header_uncomp_size;
5390 0 mz_uint32 uncomp_crc32 = MZ_CRC32_INIT;
5391 mz_bool has_data_descriptor;
5392 mz_uint32 local_header_bit_flags;
5393  
5394 mz_zip_array file_data_array;
5395 mz_zip_array_init(&file_data_array, 1);
5396  
5397 0 ▶ if ((!pZip) || (!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || (!pZip->m_pRead))
Branch: 5 branches
BranchTrueFalseTotal
if ((!pZip) || (!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || (!pZip->m_pRead)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || (!pZip->m_pRead)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || (!pZip->m_pRead)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || (!pZip->m_pRead)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || (!pZip->m_pRead)) 0 0 0
5398 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
5399  
5400 0 ▶ if (file_index > pZip->m_total_files)
Branch: 1 branch
BranchTrueFalseTotal
if (file_index > pZip->m_total_files) 0 0 0
5401 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
5402  
5403 0 ▶ pState = pZip->m_pState;
Branch: 1 branch
BranchTrueFalseTotal
pState = pZip->m_pState; 0 0 0
5404  
5405 0 pCentral_dir_header = mz_zip_get_cdh(pZip, file_index);
5406  
5407 0 ▶ if (!mz_zip_file_stat_internal(pZip, file_index, pCentral_dir_header, &file_stat, &found_zip64_ext_data_in_cdir))
Branch: 1 branch
BranchTrueFalseTotal
if (!mz_zip_file_stat_internal(pZip, file_index, pCentral_dir_header, &file_stat, &found_zip64_ext_data_in_cdir)) 0 0 0
5408 0 return MZ_FALSE;
5409  
5410 /* A directory or zero length file */
5411 0 ▶ if ((file_stat.m_is_directory) || (!file_stat.m_uncomp_size))
Branch: 2 branches
BranchTrueFalseTotal
if ((file_stat.m_is_directory) || (!file_stat.m_uncomp_size)) 0 0 0
if ((file_stat.m_is_directory) || (!file_stat.m_uncomp_size)) 0 0 0
5412 0 return MZ_TRUE;
5413  
5414 /* Encryption and patch files are not supported. */
5415 0 ▶ if (file_stat.m_is_encrypted)
Branch: 1 branch
BranchTrueFalseTotal
if (file_stat.m_is_encrypted) 0 0 0
5416 0 return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_ENCRYPTION);
5417  
5418 /* This function only supports stored and deflate. */
5419 0 ▶ if ((file_stat.m_method != 0) && (file_stat.m_method != MZ_DEFLATED))
Branch: 2 branches
BranchTrueFalseTotal
if ((file_stat.m_method != 0) && (file_stat.m_method != MZ_DEFLATED)) 0 0 0
if ((file_stat.m_method != 0) && (file_stat.m_method != MZ_DEFLATED)) 0 0 0
5420 0 return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_METHOD);
5421  
5422 0 ▶ if (!file_stat.m_is_supported)
Branch: 1 branch
BranchTrueFalseTotal
if (!file_stat.m_is_supported) 0 0 0
5423 0 return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_FEATURE);
5424  
5425 /* Read and parse the local directory entry. */
5426 0 local_header_ofs = file_stat.m_local_header_ofs;
5427 0 ▶ if (pZip->m_pRead(pZip->m_pIO_opaque, local_header_ofs, pLocal_header, MZ_ZIP_LOCAL_DIR_HEADER_SIZE) != MZ_ZIP_LOCAL_DIR_HEADER_SIZE)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pRead(pZip->m_pIO_opaque, local_header_ofs, pLocal_header, MZ_ZIP_LOCAL_DIR_HEADER_SIZE) != MZ_ZIP_LOCAL_DIR_HEADER_SIZE) 0 0 0
5428 0 return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
5429  
5430 0 ▶ if (MZ_READ_LE32(pLocal_header) != MZ_ZIP_LOCAL_DIR_HEADER_SIG)
Branch: 1 branch
BranchTrueFalseTotal
if (MZ_READ_LE32(pLocal_header) != MZ_ZIP_LOCAL_DIR_HEADER_SIG) 0 0 0
5431 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
5432  
5433 0 local_header_filename_len = MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_FILENAME_LEN_OFS);
5434 0 local_header_extra_len = MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_EXTRA_LEN_OFS);
5435 0 local_header_comp_size = MZ_READ_LE32(pLocal_header + MZ_ZIP_LDH_COMPRESSED_SIZE_OFS);
5436 0 local_header_uncomp_size = MZ_READ_LE32(pLocal_header + MZ_ZIP_LDH_DECOMPRESSED_SIZE_OFS);
5437 0 local_header_crc32 = MZ_READ_LE32(pLocal_header + MZ_ZIP_LDH_CRC32_OFS);
5438 0 local_header_bit_flags = MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_BIT_FLAG_OFS);
5439 0 has_data_descriptor = (local_header_bit_flags & 8) != 0;
5440  
5441 0 ▶ if (local_header_filename_len != strlen(file_stat.m_filename))
Branch: 1 branch
BranchTrueFalseTotal
if (local_header_filename_len != strlen(file_stat.m_filename)) 0 0 0
5442 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
5443  
5444 0 ▶ if ((local_header_ofs + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + local_header_filename_len + local_header_extra_len + file_stat.m_comp_size) > pZip->m_archive_size)
Branch: 1 branch
BranchTrueFalseTotal
if ((local_header_ofs + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + local_header_filename_len + local_header_extra_len + file_stat.m_comp_size) > pZip->m_archive_size) 0 0 0
5445 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
5446  
5447 0 ▶ if (!mz_zip_array_resize(pZip, &file_data_array, MZ_MAX(local_header_filename_len, local_header_extra_len), MZ_FALSE))
Branch: 2 branches
BranchTrueFalseTotal
if (!mz_zip_array_resize(pZip, &file_data_array, MZ_MAX(local_header_filename_len, local_header_extra_len), MZ_FALSE)) 0 0 0
if (!mz_zip_array_resize(pZip, &file_data_array, MZ_MAX(local_header_filename_len, local_header_extra_len), MZ_FALSE)) 0 0 0
5448 {
5449 mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
5450 0 goto handle_failure;
5451 }
5452  
5453 0 ▶ if (local_header_filename_len)
Branch: 1 branch
BranchTrueFalseTotal
if (local_header_filename_len) 0 0 0
5454 {
5455 0 ▶ if (pZip->m_pRead(pZip->m_pIO_opaque, local_header_ofs + MZ_ZIP_LOCAL_DIR_HEADER_SIZE, file_data_array.m_p, local_header_filename_len) != local_header_filename_len)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pRead(pZip->m_pIO_opaque, local_header_ofs + MZ_ZIP_LOCAL_DIR_HEADER_SIZE, file_data_array.m_p, local_header_filename_len) != local_header_filename_len) 0 0 0
5456 {
5457 mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
5458 0 goto handle_failure;
5459 }
5460  
5461 /* I've seen 1 archive that had the same pathname, but used backslashes in the local dir and forward slashes in the central dir. Do we care about this? For now, this case will fail validation. */
5462 0 ▶ if (memcmp(file_stat.m_filename, file_data_array.m_p, local_header_filename_len) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(file_stat.m_filename, file_data_array.m_p, local_header_filename_len) != 0) 0 0 0
5463 {
5464 mz_zip_set_error(pZip, MZ_ZIP_VALIDATION_FAILED);
5465 0 goto handle_failure;
5466 }
5467 }
5468  
5469 0 ▶ if ((local_header_extra_len) && ((local_header_comp_size == MZ_UINT32_MAX) || (local_header_uncomp_size == MZ_UINT32_MAX)))
Branch: 3 branches
BranchTrueFalseTotal
if ((local_header_extra_len) && ((local_header_comp_size == MZ_UINT32_MAX) || (local_header_uncomp_size == MZ_UINT32_MAX))) 0 0 0
if ((local_header_extra_len) && ((local_header_comp_size == MZ_UINT32_MAX) || (local_header_uncomp_size == MZ_UINT32_MAX))) 0 0 0
if ((local_header_extra_len) && ((local_header_comp_size == MZ_UINT32_MAX) || (local_header_uncomp_size == MZ_UINT32_MAX))) 0 0 0
5470 {
5471 0 mz_uint32 extra_size_remaining = local_header_extra_len;
5472 0 const mz_uint8 *pExtra_data = (const mz_uint8 *)file_data_array.m_p;
5473  
5474 0 ▶ if (pZip->m_pRead(pZip->m_pIO_opaque, local_header_ofs + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + local_header_filename_len, file_data_array.m_p, local_header_extra_len) != local_header_extra_len)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pRead(pZip->m_pIO_opaque, local_header_ofs + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + local_header_filename_len, file_data_array.m_p, local_header_extra_len) != local_header_extra_len) 0 0 0
5475 {
5476 mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
5477 0 goto handle_failure;
5478 }
5479  
5480 do
5481 {
5482 mz_uint32 field_id, field_data_size, field_total_size;
5483  
5484 0 ▶ if (extra_size_remaining < (sizeof(mz_uint16) * 2))
Branch: 1 branch
BranchTrueFalseTotal
if (extra_size_remaining < (sizeof(mz_uint16) * 2)) 0 0 0
5485 {
5486 mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
5487 0 goto handle_failure;
5488 }
5489  
5490 0 field_id = MZ_READ_LE16(pExtra_data);
5491 0 field_data_size = MZ_READ_LE16(pExtra_data + sizeof(mz_uint16));
5492 0 field_total_size = field_data_size + sizeof(mz_uint16) * 2;
5493  
5494 0 ▶ if (field_total_size > extra_size_remaining)
Branch: 1 branch
BranchTrueFalseTotal
if (field_total_size > extra_size_remaining) 0 0 0
5495 {
5496 mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
5497 0 goto handle_failure;
5498 }
5499  
5500 0 ▶ if (field_id == MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID)
Branch: 1 branch
BranchTrueFalseTotal
if (field_id == MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID) 0 0 0
5501 {
5502 0 const mz_uint8 *pSrc_field_data = pExtra_data + sizeof(mz_uint32);
5503  
5504 0 ▶ if (field_data_size < sizeof(mz_uint64) * 2)
Branch: 1 branch
BranchTrueFalseTotal
if (field_data_size < sizeof(mz_uint64) * 2) 0 0 0
5505 {
5506 mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
5507 0 goto handle_failure;
5508 }
5509  
5510 0 local_header_uncomp_size = MZ_READ_LE64(pSrc_field_data);
5511 0 local_header_comp_size = MZ_READ_LE64(pSrc_field_data + sizeof(mz_uint64));
5512  
5513 0 found_zip64_ext_data_in_ldir = MZ_TRUE;
5514 0 break;
5515 }
5516  
5517 0 pExtra_data += field_total_size;
5518 0 extra_size_remaining -= field_total_size;
5519 0 ▶ } while (extra_size_remaining);
Branch: 1 branch
BranchTrueFalseTotal
} while (extra_size_remaining); 0 0 0
5520 }
5521  
5522 /* TODO: parse local header extra data when local_header_comp_size is 0xFFFFFFFF! (big_descriptor.zip) */
5523 /* I've seen zips in the wild with the data descriptor bit set, but proper local header values and bogus data descriptors */
5524 0 ▶ if ((has_data_descriptor) && (!local_header_comp_size) && (!local_header_crc32))
Branch: 3 branches
BranchTrueFalseTotal
if ((has_data_descriptor) && (!local_header_comp_size) && (!local_header_crc32)) 0 0 0
if ((has_data_descriptor) && (!local_header_comp_size) && (!local_header_crc32)) 0 0 0
if ((has_data_descriptor) && (!local_header_comp_size) && (!local_header_crc32)) 0 0 0
5525 0 {
5526 mz_uint8 descriptor_buf[32];
5527 mz_bool has_id;
5528 const mz_uint8 *pSrc;
5529 mz_uint32 file_crc32;
5530 0 mz_uint64 comp_size = 0, uncomp_size = 0;
5531  
5532 0 ▶ mz_uint32 num_descriptor_uint32s = ((pState->m_zip64) || (found_zip64_ext_data_in_ldir)) ? 6 : 4;
Branch: 2 branches
BranchTrueFalseTotal
mz_uint32 num_descriptor_uint32s = ((pState->m_zip64) || (found_zip64_ext_data_in_ldir)) ? 6 : 4; 0 0 0
mz_uint32 num_descriptor_uint32s = ((pState->m_zip64) || (found_zip64_ext_data_in_ldir)) ? 6 : 4; 0 0 0
5533  
5534 0 ▶ if (pZip->m_pRead(pZip->m_pIO_opaque, local_header_ofs + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + local_header_filename_len + local_header_extra_len + file_stat.m_comp_size, descriptor_buf, sizeof(mz_uint32) * num_descriptor_uint32s) != (sizeof(mz_uint32) * num_descriptor_uint32s))
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pRead(pZip->m_pIO_opaque, local_header_ofs + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + local_header_filename_len + local_header_extra_len + file_stat.m_comp_size, descriptor_buf, sizeof(mz_uint32) * num_descriptor_uint32s) != (sizeof(mz_uint32) * num_descriptor_uint32s)) 0 0 0
5535 {
5536 mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
5537 0 goto handle_failure;
5538 }
5539  
5540 0 has_id = (MZ_READ_LE32(descriptor_buf) == MZ_ZIP_DATA_DESCRIPTOR_ID);
5541 0 ▶ pSrc = has_id ? (descriptor_buf + sizeof(mz_uint32)) : descriptor_buf;
Branch: 1 branch
BranchTrueFalseTotal
pSrc = has_id ? (descriptor_buf + sizeof(mz_uint32)) : descriptor_buf; 0 0 0
5542  
5543 0 file_crc32 = MZ_READ_LE32(pSrc);
5544  
5545 0 ▶ if ((pState->m_zip64) || (found_zip64_ext_data_in_ldir))
Branch: 2 branches
BranchTrueFalseTotal
if ((pState->m_zip64) || (found_zip64_ext_data_in_ldir)) 0 0 0
if ((pState->m_zip64) || (found_zip64_ext_data_in_ldir)) 0 0 0
5546 {
5547 0 comp_size = MZ_READ_LE64(pSrc + sizeof(mz_uint32));
5548 0 uncomp_size = MZ_READ_LE64(pSrc + sizeof(mz_uint32) + sizeof(mz_uint64));
5549 }
5550 else
5551 {
5552 0 comp_size = MZ_READ_LE32(pSrc + sizeof(mz_uint32));
5553 0 uncomp_size = MZ_READ_LE32(pSrc + sizeof(mz_uint32) + sizeof(mz_uint32));
5554 }
5555  
5556 0 ▶ if ((file_crc32 != file_stat.m_crc32) || (comp_size != file_stat.m_comp_size) || (uncomp_size != file_stat.m_uncomp_size))
Branch: 3 branches
BranchTrueFalseTotal
if ((file_crc32 != file_stat.m_crc32) || (comp_size != file_stat.m_comp_size) || (uncomp_size != file_stat.m_uncomp_size)) 0 0 0
if ((file_crc32 != file_stat.m_crc32) || (comp_size != file_stat.m_comp_size) || (uncomp_size != file_stat.m_uncomp_size)) 0 0 0
if ((file_crc32 != file_stat.m_crc32) || (comp_size != file_stat.m_comp_size) || (uncomp_size != file_stat.m_uncomp_size)) 0 0 0
5557 {
5558 mz_zip_set_error(pZip, MZ_ZIP_VALIDATION_FAILED);
5559 0 goto handle_failure;
5560 }
5561 }
5562 else
5563 {
5564 0 ▶ if ((local_header_crc32 != file_stat.m_crc32) || (local_header_comp_size != file_stat.m_comp_size) || (local_header_uncomp_size != file_stat.m_uncomp_size))
Branch: 3 branches
BranchTrueFalseTotal
if ((local_header_crc32 != file_stat.m_crc32) || (local_header_comp_size != file_stat.m_comp_size) || (local_header_uncomp_size != file_stat.m_uncomp_size)) 0 0 0
if ((local_header_crc32 != file_stat.m_crc32) || (local_header_comp_size != file_stat.m_comp_size) || (local_header_uncomp_size != file_stat.m_uncomp_size)) 0 0 0
if ((local_header_crc32 != file_stat.m_crc32) || (local_header_comp_size != file_stat.m_comp_size) || (local_header_uncomp_size != file_stat.m_uncomp_size)) 0 0 0
5565 {
5566 mz_zip_set_error(pZip, MZ_ZIP_VALIDATION_FAILED);
5567 0 goto handle_failure;
5568 }
5569 }
5570  
5571 mz_zip_array_clear(pZip, &file_data_array);
5572  
5573 0 ▶ if ((flags & MZ_ZIP_FLAG_VALIDATE_HEADERS_ONLY) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((flags & MZ_ZIP_FLAG_VALIDATE_HEADERS_ONLY) == 0) 0 0 0
5574 {
5575 0 ▶ if (!mz_zip_reader_extract_to_callback(pZip, file_index, mz_zip_compute_crc32_callback, &uncomp_crc32, 0))
Branch: 1 branch
BranchTrueFalseTotal
if (!mz_zip_reader_extract_to_callback(pZip, file_index, mz_zip_compute_crc32_callback, &uncomp_crc32, 0)) 0 0 0
5576 0 return MZ_FALSE;
5577  
5578 /* 1 more check to be sure, although the extract checks too. */
5579 0 ▶ if (uncomp_crc32 != file_stat.m_crc32)
Branch: 1 branch
BranchTrueFalseTotal
if (uncomp_crc32 != file_stat.m_crc32) 0 0 0
5580 {
5581 mz_zip_set_error(pZip, MZ_ZIP_VALIDATION_FAILED);
5582 0 return MZ_FALSE;
5583 }
5584 }
5585  
5586 0 return MZ_TRUE;
5587  
5588 0 handle_failure:
5589 mz_zip_array_clear(pZip, &file_data_array);
5590 0 return MZ_FALSE;
5591 }
5592  
5593 0 mz_bool mz_zip_validate_archive(mz_zip_archive *pZip, mz_uint flags)
5594 {
5595 mz_zip_internal_state *pState;
5596 mz_uint32 i;
5597  
5598 0 ▶ if ((!pZip) || (!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || (!pZip->m_pRead))
Branch: 5 branches
BranchTrueFalseTotal
if ((!pZip) || (!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || (!pZip->m_pRead)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || (!pZip->m_pRead)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || (!pZip->m_pRead)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || (!pZip->m_pRead)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || (!pZip->m_pRead)) 0 0 0
5599 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
5600  
5601 0 pState = pZip->m_pState;
5602  
5603 /* Basic sanity checks */
5604 0 ▶ if (!pState->m_zip64)
Branch: 1 branch
BranchTrueFalseTotal
if (!pState->m_zip64) 0 0 0
5605 {
5606 0 ▶ if (pZip->m_total_files > MZ_UINT16_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_total_files > MZ_UINT16_MAX) 0 0 0
5607 0 return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);
5608  
5609 0 ▶ if (pZip->m_archive_size > MZ_UINT32_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_archive_size > MZ_UINT32_MAX) 0 0 0
5610 0 return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);
5611 }
5612 else
5613 {
5614 0 ▶ if (pState->m_central_dir.m_size >= MZ_UINT32_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (pState->m_central_dir.m_size >= MZ_UINT32_MAX) 0 0 0
5615 0 return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);
5616 }
5617  
5618 0 ▶ for (i = 0; i < pZip->m_total_files; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < pZip->m_total_files; i++) 0 0 0
5619 {
5620 0 ▶ if (MZ_ZIP_FLAG_VALIDATE_LOCATE_FILE_FLAG & flags)
Branch: 1 branch
BranchTrueFalseTotal
if (MZ_ZIP_FLAG_VALIDATE_LOCATE_FILE_FLAG & flags) 0 0 0
5621 {
5622 mz_uint32 found_index;
5623 mz_zip_archive_file_stat stat;
5624  
5625 0 ▶ if (!mz_zip_reader_file_stat(pZip, i, &stat))
Branch: 1 branch
BranchTrueFalseTotal
if (!mz_zip_reader_file_stat(pZip, i, &stat)) 0 0 0
5626 0 return MZ_FALSE;
5627  
5628 0 ▶ if (!mz_zip_reader_locate_file_v2(pZip, stat.m_filename, NULL, 0, &found_index))
Branch: 1 branch
BranchTrueFalseTotal
if (!mz_zip_reader_locate_file_v2(pZip, stat.m_filename, NULL, 0, &found_index)) 0 0 0
5629 0 return MZ_FALSE;
5630  
5631 /* This check can fail if there are duplicate filenames in the archive (which we don't check for when writing - that's up to the user) */
5632 0 ▶ if (found_index != i)
Branch: 1 branch
BranchTrueFalseTotal
if (found_index != i) 0 0 0
5633 0 return mz_zip_set_error(pZip, MZ_ZIP_VALIDATION_FAILED);
5634 }
5635  
5636 0 ▶ if (!mz_zip_validate_file(pZip, i, flags))
Branch: 1 branch
BranchTrueFalseTotal
if (!mz_zip_validate_file(pZip, i, flags)) 0 0 0
5637 0 return MZ_FALSE;
5638 }
5639  
5640 0 return MZ_TRUE;
5641 }
5642  
5643 0 mz_bool mz_zip_validate_mem_archive(const void *pMem, size_t size, mz_uint flags, mz_zip_error *pErr)
5644 {
5645 0 mz_bool success = MZ_TRUE;
5646 mz_zip_archive zip;
5647 0 mz_zip_error actual_err = MZ_ZIP_NO_ERROR;
5648  
5649 0 ▶ if ((!pMem) || (!size))
Branch: 2 branches
BranchTrueFalseTotal
if ((!pMem) || (!size)) 0 0 0
if ((!pMem) || (!size)) 0 0 0
5650 {
5651 0 ▶ if (pErr)
Branch: 1 branch
BranchTrueFalseTotal
if (pErr) 0 0 0
5652 0 *pErr = MZ_ZIP_INVALID_PARAMETER;
5653 0 return MZ_FALSE;
5654 }
5655  
5656 0 mz_zip_zero_struct(&zip);
5657  
5658 0 ▶ if (!mz_zip_reader_init_mem(&zip, pMem, size, flags))
Branch: 1 branch
BranchTrueFalseTotal
if (!mz_zip_reader_init_mem(&zip, pMem, size, flags)) 0 0 0
5659 {
5660 0 ▶ if (pErr)
Branch: 1 branch
BranchTrueFalseTotal
if (pErr) 0 0 0
5661 0 *pErr = zip.m_last_error;
5662 0 return MZ_FALSE;
5663 }
5664  
5665 0 ▶ if (!mz_zip_validate_archive(&zip, flags))
Branch: 1 branch
BranchTrueFalseTotal
if (!mz_zip_validate_archive(&zip, flags)) 0 0 0
5666 {
5667 0 actual_err = zip.m_last_error;
5668 0 success = MZ_FALSE;
5669 }
5670  
5671 0 ▶ if (!mz_zip_reader_end_internal(&zip, success))
Branch: 1 branch
BranchTrueFalseTotal
if (!mz_zip_reader_end_internal(&zip, success)) 0 0 0
5672 {
5673 0 ▶ if (!actual_err)
Branch: 1 branch
BranchTrueFalseTotal
if (!actual_err) 0 0 0
5674 0 actual_err = zip.m_last_error;
5675 0 success = MZ_FALSE;
5676 }
5677  
5678 0 ▶ if (pErr)
Branch: 1 branch
BranchTrueFalseTotal
if (pErr) 0 0 0
5679 0 *pErr = actual_err;
5680  
5681 0 return success;
5682 }
5683  
5684 #ifndef MINIZ_NO_STDIO
5685 mz_bool mz_zip_validate_file_archive(const char *pFilename, mz_uint flags, mz_zip_error *pErr)
5686 {
5687 mz_bool success = MZ_TRUE;
5688 mz_zip_archive zip;
5689 mz_zip_error actual_err = MZ_ZIP_NO_ERROR;
5690  
5691 if (!pFilename)
5692 {
5693 if (pErr)
5694 *pErr = MZ_ZIP_INVALID_PARAMETER;
5695 return MZ_FALSE;
5696 }
5697  
5698 mz_zip_zero_struct(&zip);
5699  
5700 if (!mz_zip_reader_init_file_v2(&zip, pFilename, flags, 0, 0))
5701 {
5702 if (pErr)
5703 *pErr = zip.m_last_error;
5704 return MZ_FALSE;
5705 }
5706  
5707 if (!mz_zip_validate_archive(&zip, flags))
5708 {
5709 actual_err = zip.m_last_error;
5710 success = MZ_FALSE;
5711 }
5712  
5713 if (!mz_zip_reader_end_internal(&zip, success))
5714 {
5715 if (!actual_err)
5716 actual_err = zip.m_last_error;
5717 success = MZ_FALSE;
5718 }
5719  
5720 if (pErr)
5721 *pErr = actual_err;
5722  
5723 return success;
5724 }
5725 #endif /* #ifndef MINIZ_NO_STDIO */
5726  
5727 /* ------------------- .ZIP archive writing */
5728  
5729 #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS
5730  
5731 static MZ_FORCEINLINE void mz_write_le16(mz_uint8 *p, mz_uint16 v)
5732 {
5733 0 p[0] = (mz_uint8)v;
5734 0 p[1] = (mz_uint8)(v >> 8);
5735 0 }
5736 static MZ_FORCEINLINE void mz_write_le32(mz_uint8 *p, mz_uint32 v)
5737 {
5738 0 p[0] = (mz_uint8)v;
5739 0 p[1] = (mz_uint8)(v >> 8);
5740 0 p[2] = (mz_uint8)(v >> 16);
5741 0 p[3] = (mz_uint8)(v >> 24);
5742 0 }
5743 static MZ_FORCEINLINE void mz_write_le64(mz_uint8 *p, mz_uint64 v)
5744 {
5745 0 mz_write_le32(p, (mz_uint32)v);
5746 0 mz_write_le32(p + sizeof(mz_uint32), (mz_uint32)(v >> 32));
5747 0 }
5748  
5749 #define MZ_WRITE_LE16(p, v) mz_write_le16((mz_uint8 *)(p), (mz_uint16)(v))
5750 #define MZ_WRITE_LE32(p, v) mz_write_le32((mz_uint8 *)(p), (mz_uint32)(v))
5751 #define MZ_WRITE_LE64(p, v) mz_write_le64((mz_uint8 *)(p), (mz_uint64)(v))
5752  
5753 0 static size_t mz_zip_heap_write_func(void *pOpaque, mz_uint64 file_ofs, const void *pBuf, size_t n)
5754 {
5755 0 mz_zip_archive *pZip = (mz_zip_archive *)pOpaque;
5756 0 mz_zip_internal_state *pState = pZip->m_pState;
5757 0 mz_uint64 new_size = MZ_MAX(file_ofs + n, pState->m_mem_size);
5758  
5759 0 ▶ if (!n)
Branch: 1 branch
BranchTrueFalseTotal
if (!n) 0 0 0
5760 0 return 0;
5761  
5762 /* An allocation this big is likely to just fail on 32-bit systems, so don't even go there. */
5763 if ((sizeof(size_t) == sizeof(mz_uint32)) && (new_size > 0x7FFFFFFF))
5764 {
5765 mz_zip_set_error(pZip, MZ_ZIP_FILE_TOO_LARGE);
5766 return 0;
5767 }
5768  
5769 0 ▶ if (new_size > pState->m_mem_capacity)
Branch: 1 branch
BranchTrueFalseTotal
if (new_size > pState->m_mem_capacity) 0 0 0
5770 {
5771 void *pNew_block;
5772 0 size_t new_capacity = MZ_MAX(64, pState->m_mem_capacity);
5773  
5774 0 ▶ while (new_capacity < new_size)
Branch: 1 branch
BranchTrueFalseTotal
while (new_capacity < new_size) 0 0 0
5775 0 new_capacity *= 2;
5776  
5777 0 ▶ if (NULL == (pNew_block = pZip->m_pRealloc(pZip->m_pAlloc_opaque, pState->m_pMem, 1, new_capacity)))
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == (pNew_block = pZip->m_pRealloc(pZip->m_pAlloc_opaque, pState->m_pMem, 1, new_capacity))) 0 0 0
5778 {
5779 mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
5780 0 return 0;
5781 }
5782  
5783 0 pState->m_pMem = pNew_block;
5784 0 pState->m_mem_capacity = new_capacity;
5785 }
5786 0 memcpy((mz_uint8 *)pState->m_pMem + file_ofs, pBuf, n);
5787 0 pState->m_mem_size = (size_t)new_size;
5788 0 return n;
5789 }
5790  
5791 0 static mz_bool mz_zip_writer_end_internal(mz_zip_archive *pZip, mz_bool set_last_error)
5792 {
5793 mz_zip_internal_state *pState;
5794 0 mz_bool status = MZ_TRUE;
5795  
5796 0 ▶ if ((!pZip) || (!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || ((pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) && (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED)))
Branch: 6 branches
BranchTrueFalseTotal
if ((!pZip) || (!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || ((pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) && (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED))) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || ((pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) && (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED))) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || ((pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) && (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED))) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || ((pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) && (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED))) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || ((pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) && (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED))) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || ((pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) && (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED))) 0 0 0
5797 {
5798 0 ▶ if (set_last_error)
Branch: 1 branch
BranchTrueFalseTotal
if (set_last_error) 0 0 0
5799 mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
5800 0 return MZ_FALSE;
5801 }
5802  
5803 0 pState = pZip->m_pState;
5804 0 pZip->m_pState = NULL;
5805 0 mz_zip_array_clear(pZip, &pState->m_central_dir);
5806 0 mz_zip_array_clear(pZip, &pState->m_central_dir_offsets);
5807 0 mz_zip_array_clear(pZip, &pState->m_sorted_central_dir_offsets);
5808  
5809 #ifndef MINIZ_NO_STDIO
5810 if (pState->m_pFile)
5811 {
5812 if (pZip->m_zip_type == MZ_ZIP_TYPE_FILE)
5813 {
5814 if (MZ_FCLOSE(pState->m_pFile) == EOF)
5815 {
5816 if (set_last_error)
5817 mz_zip_set_error(pZip, MZ_ZIP_FILE_CLOSE_FAILED);
5818 status = MZ_FALSE;
5819 }
5820 }
5821  
5822 pState->m_pFile = NULL;
5823 }
5824 #endif /* #ifndef MINIZ_NO_STDIO */
5825  
5826 0 ▶ if ((pZip->m_pWrite == mz_zip_heap_write_func) && (pState->m_pMem))
Branch: 2 branches
BranchTrueFalseTotal
if ((pZip->m_pWrite == mz_zip_heap_write_func) && (pState->m_pMem)) 0 0 0
if ((pZip->m_pWrite == mz_zip_heap_write_func) && (pState->m_pMem)) 0 0 0
5827 {
5828 0 pZip->m_pFree(pZip->m_pAlloc_opaque, pState->m_pMem);
5829 0 pState->m_pMem = NULL;
5830 }
5831  
5832 0 pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
5833 0 pZip->m_zip_mode = MZ_ZIP_MODE_INVALID;
5834 0 return status;
5835 }
5836  
5837 0 mz_bool mz_zip_writer_init_v2(mz_zip_archive *pZip, mz_uint64 existing_size, mz_uint flags)
5838 {
5839 0 mz_bool zip64 = (flags & MZ_ZIP_FLAG_WRITE_ZIP64) != 0;
5840  
5841 0 ▶ if ((!pZip) || (pZip->m_pState) || (!pZip->m_pWrite) || (pZip->m_zip_mode != MZ_ZIP_MODE_INVALID))
Branch: 4 branches
BranchTrueFalseTotal
if ((!pZip) || (pZip->m_pState) || (!pZip->m_pWrite) || (pZip->m_zip_mode != MZ_ZIP_MODE_INVALID)) 0 0 0
if ((!pZip) || (pZip->m_pState) || (!pZip->m_pWrite) || (pZip->m_zip_mode != MZ_ZIP_MODE_INVALID)) 0 0 0
if ((!pZip) || (pZip->m_pState) || (!pZip->m_pWrite) || (pZip->m_zip_mode != MZ_ZIP_MODE_INVALID)) 0 0 0
if ((!pZip) || (pZip->m_pState) || (!pZip->m_pWrite) || (pZip->m_zip_mode != MZ_ZIP_MODE_INVALID)) 0 0 0
5842 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
5843  
5844 0 ▶ if (flags & MZ_ZIP_FLAG_WRITE_ALLOW_READING)
Branch: 1 branch
BranchTrueFalseTotal
if (flags & MZ_ZIP_FLAG_WRITE_ALLOW_READING) 0 0 0
5845 {
5846 0 ▶ if (!pZip->m_pRead)
Branch: 1 branch
BranchTrueFalseTotal
if (!pZip->m_pRead) 0 0 0
5847 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
5848 }
5849  
5850 0 ▶ if (pZip->m_file_offset_alignment)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_file_offset_alignment) 0 0 0
5851 {
5852 /* Ensure user specified file offset alignment is a power of 2. */
5853 0 ▶ if (pZip->m_file_offset_alignment & (pZip->m_file_offset_alignment - 1))
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_file_offset_alignment & (pZip->m_file_offset_alignment - 1)) 0 0 0
5854 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
5855 }
5856  
5857 0 ▶ if (!pZip->m_pAlloc)
Branch: 1 branch
BranchTrueFalseTotal
if (!pZip->m_pAlloc) 0 0 0
5858 0 pZip->m_pAlloc = miniz_def_alloc_func;
5859 0 ▶ if (!pZip->m_pFree)
Branch: 1 branch
BranchTrueFalseTotal
if (!pZip->m_pFree) 0 0 0
5860 0 pZip->m_pFree = miniz_def_free_func;
5861 0 ▶ if (!pZip->m_pRealloc)
Branch: 1 branch
BranchTrueFalseTotal
if (!pZip->m_pRealloc) 0 0 0
5862 0 pZip->m_pRealloc = miniz_def_realloc_func;
5863  
5864 0 pZip->m_archive_size = existing_size;
5865 0 pZip->m_central_directory_file_ofs = 0;
5866 0 pZip->m_total_files = 0;
5867  
5868 0 ▶ if (NULL == (pZip->m_pState = (mz_zip_internal_state *)pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, sizeof(mz_zip_internal_state))))
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == (pZip->m_pState = (mz_zip_internal_state *)pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, sizeof(mz_zip_internal_state)))) 0 0 0
5869 0 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
5870  
5871 0 memset(pZip->m_pState, 0, sizeof(mz_zip_internal_state));
5872  
5873 0 MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_central_dir, sizeof(mz_uint8));
5874 0 MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_central_dir_offsets, sizeof(mz_uint32));
5875 0 MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_sorted_central_dir_offsets, sizeof(mz_uint32));
5876  
5877 0 pZip->m_pState->m_zip64 = zip64;
5878 0 pZip->m_pState->m_zip64_has_extended_info_fields = zip64;
5879  
5880 0 pZip->m_zip_type = MZ_ZIP_TYPE_USER;
5881 0 pZip->m_zip_mode = MZ_ZIP_MODE_WRITING;
5882  
5883 0 return MZ_TRUE;
5884 }
5885  
5886 0 mz_bool mz_zip_writer_init(mz_zip_archive *pZip, mz_uint64 existing_size)
5887 {
5888 0 return mz_zip_writer_init_v2(pZip, existing_size, 0);
5889 }
5890  
5891 0 mz_bool mz_zip_writer_init_heap_v2(mz_zip_archive *pZip, size_t size_to_reserve_at_beginning, size_t initial_allocation_size, mz_uint flags)
5892 {
5893 0 pZip->m_pWrite = mz_zip_heap_write_func;
5894 0 pZip->m_pNeeds_keepalive = NULL;
5895  
5896 0 ▶ if (flags & MZ_ZIP_FLAG_WRITE_ALLOW_READING)
Branch: 1 branch
BranchTrueFalseTotal
if (flags & MZ_ZIP_FLAG_WRITE_ALLOW_READING) 0 0 0
5897 0 pZip->m_pRead = mz_zip_mem_read_func;
5898  
5899 0 pZip->m_pIO_opaque = pZip;
5900  
5901 0 ▶ if (!mz_zip_writer_init_v2(pZip, size_to_reserve_at_beginning, flags))
Branch: 1 branch
BranchTrueFalseTotal
if (!mz_zip_writer_init_v2(pZip, size_to_reserve_at_beginning, flags)) 0 0 0
5902 0 return MZ_FALSE;
5903  
5904 0 pZip->m_zip_type = MZ_ZIP_TYPE_HEAP;
5905  
5906 0 ▶ if (0 != (initial_allocation_size = MZ_MAX(initial_allocation_size, size_to_reserve_at_beginning)))
Branch: 1 branch
BranchTrueFalseTotal
if (0 != (initial_allocation_size = MZ_MAX(initial_allocation_size, size_to_reserve_at_beginning))) 0 0 0
5907 {
5908 0 ▶ if (NULL == (pZip->m_pState->m_pMem = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, initial_allocation_size)))
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == (pZip->m_pState->m_pMem = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, initial_allocation_size))) 0 0 0
5909 {
5910 0 mz_zip_writer_end_internal(pZip, MZ_FALSE);
5911 0 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
5912 }
5913 0 pZip->m_pState->m_mem_capacity = initial_allocation_size;
5914 }
5915  
5916 0 return MZ_TRUE;
5917 }
5918  
5919 0 mz_bool mz_zip_writer_init_heap(mz_zip_archive *pZip, size_t size_to_reserve_at_beginning, size_t initial_allocation_size)
5920 {
5921 0 return mz_zip_writer_init_heap_v2(pZip, size_to_reserve_at_beginning, initial_allocation_size, 0);
5922 }
5923  
5924 #ifndef MINIZ_NO_STDIO
5925 static size_t mz_zip_file_write_func(void *pOpaque, mz_uint64 file_ofs, const void *pBuf, size_t n)
5926 {
5927 mz_zip_archive *pZip = (mz_zip_archive *)pOpaque;
5928 mz_int64 cur_ofs = MZ_FTELL64(pZip->m_pState->m_pFile);
5929  
5930 file_ofs += pZip->m_pState->m_file_archive_start_ofs;
5931  
5932 if (((mz_int64)file_ofs < 0) || (((cur_ofs != (mz_int64)file_ofs)) && (MZ_FSEEK64(pZip->m_pState->m_pFile, (mz_int64)file_ofs, SEEK_SET))))
5933 {
5934 mz_zip_set_error(pZip, MZ_ZIP_FILE_SEEK_FAILED);
5935 return 0;
5936 }
5937  
5938 return MZ_FWRITE(pBuf, 1, n, pZip->m_pState->m_pFile);
5939 }
5940  
5941 mz_bool mz_zip_writer_init_file(mz_zip_archive *pZip, const char *pFilename, mz_uint64 size_to_reserve_at_beginning)
5942 {
5943 return mz_zip_writer_init_file_v2(pZip, pFilename, size_to_reserve_at_beginning, 0);
5944 }
5945  
5946 mz_bool mz_zip_writer_init_file_v2(mz_zip_archive *pZip, const char *pFilename, mz_uint64 size_to_reserve_at_beginning, mz_uint flags)
5947 {
5948 MZ_FILE *pFile;
5949  
5950 pZip->m_pWrite = mz_zip_file_write_func;
5951 pZip->m_pNeeds_keepalive = NULL;
5952  
5953 if (flags & MZ_ZIP_FLAG_WRITE_ALLOW_READING)
5954 pZip->m_pRead = mz_zip_file_read_func;
5955  
5956 pZip->m_pIO_opaque = pZip;
5957  
5958 if (!mz_zip_writer_init_v2(pZip, size_to_reserve_at_beginning, flags))
5959 return MZ_FALSE;
5960  
5961 if (NULL == (pFile = MZ_FOPEN(pFilename, (flags & MZ_ZIP_FLAG_WRITE_ALLOW_READING) ? "w+b" : "wb")))
5962 {
5963 mz_zip_writer_end(pZip);
5964 return mz_zip_set_error(pZip, MZ_ZIP_FILE_OPEN_FAILED);
5965 }
5966  
5967 pZip->m_pState->m_pFile = pFile;
5968 pZip->m_zip_type = MZ_ZIP_TYPE_FILE;
5969  
5970 if (size_to_reserve_at_beginning)
5971 {
5972 mz_uint64 cur_ofs = 0;
5973 char buf[4096];
5974  
5975 MZ_CLEAR_ARR(buf);
5976  
5977 do
5978 {
5979 size_t n = (size_t)MZ_MIN(sizeof(buf), size_to_reserve_at_beginning);
5980 if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_ofs, buf, n) != n)
5981 {
5982 mz_zip_writer_end(pZip);
5983 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
5984 }
5985 cur_ofs += n;
5986 size_to_reserve_at_beginning -= n;
5987 } while (size_to_reserve_at_beginning);
5988 }
5989  
5990 return MZ_TRUE;
5991 }
5992  
5993 mz_bool mz_zip_writer_init_cfile(mz_zip_archive *pZip, MZ_FILE *pFile, mz_uint flags)
5994 {
5995 pZip->m_pWrite = mz_zip_file_write_func;
5996 pZip->m_pNeeds_keepalive = NULL;
5997  
5998 if (flags & MZ_ZIP_FLAG_WRITE_ALLOW_READING)
5999 pZip->m_pRead = mz_zip_file_read_func;
6000  
6001 pZip->m_pIO_opaque = pZip;
6002  
6003 if (!mz_zip_writer_init_v2(pZip, 0, flags))
6004 return MZ_FALSE;
6005  
6006 pZip->m_pState->m_pFile = pFile;
6007 pZip->m_pState->m_file_archive_start_ofs = MZ_FTELL64(pZip->m_pState->m_pFile);
6008 pZip->m_zip_type = MZ_ZIP_TYPE_CFILE;
6009  
6010 return MZ_TRUE;
6011 }
6012 #endif /* #ifndef MINIZ_NO_STDIO */
6013  
6014 0 mz_bool mz_zip_writer_init_from_reader_v2(mz_zip_archive *pZip, const char *pFilename, mz_uint flags)
6015 {
6016 mz_zip_internal_state *pState;
6017  
6018 0 ▶ if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_READING))
Branch: 3 branches
BranchTrueFalseTotal
if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_READING)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_READING)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_READING)) 0 0 0
6019 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
6020  
6021 0 ▶ if (flags & MZ_ZIP_FLAG_WRITE_ZIP64)
Branch: 1 branch
BranchTrueFalseTotal
if (flags & MZ_ZIP_FLAG_WRITE_ZIP64) 0 0 0
6022 {
6023 /* We don't support converting a non-zip64 file to zip64 - this seems like more trouble than it's worth. (What about the existing 32-bit data descriptors that could follow the compressed data?) */
6024 0 ▶ if (!pZip->m_pState->m_zip64)
Branch: 1 branch
BranchTrueFalseTotal
if (!pZip->m_pState->m_zip64) 0 0 0
6025 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
6026 }
6027  
6028 /* No sense in trying to write to an archive that's already at the support max size */
6029 0 ▶ if (pZip->m_pState->m_zip64)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pState->m_zip64) 0 0 0
6030 {
6031 0 ▶ if (pZip->m_total_files == MZ_UINT32_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_total_files == MZ_UINT32_MAX) 0 0 0
6032 0 return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);
6033 }
6034 else
6035 {
6036 0 ▶ if (pZip->m_total_files == MZ_UINT16_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_total_files == MZ_UINT16_MAX) 0 0 0
6037 0 return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);
6038  
6039 0 ▶ if ((pZip->m_archive_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + MZ_ZIP_LOCAL_DIR_HEADER_SIZE) > MZ_UINT32_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if ((pZip->m_archive_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + MZ_ZIP_LOCAL_DIR_HEADER_SIZE) > MZ_UINT32_MAX) 0 0 0
6040 0 return mz_zip_set_error(pZip, MZ_ZIP_FILE_TOO_LARGE);
6041 }
6042  
6043 0 pState = pZip->m_pState;
6044  
6045 0 ▶ if (pState->m_pFile)
Branch: 1 branch
BranchTrueFalseTotal
if (pState->m_pFile) 0 0 0
6046 {
6047 #ifdef MINIZ_NO_STDIO
6048 (void)pFilename;
6049 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
6050 #else
6051 if (pZip->m_pIO_opaque != pZip)
6052 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
6053  
6054 if (pZip->m_zip_type == MZ_ZIP_TYPE_FILE &&
6055 !(flags & MZ_ZIP_FLAG_READ_ALLOW_WRITING) )
6056 {
6057 if (!pFilename)
6058 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
6059  
6060 /* Archive is being read from stdio and was originally opened only for reading. Try to reopen as writable. */
6061 if (NULL == (pState->m_pFile = MZ_FREOPEN(pFilename, "r+b", pState->m_pFile)))
6062 {
6063 /* The mz_zip_archive is now in a bogus state because pState->m_pFile is NULL, so just close it. */
6064 mz_zip_reader_end_internal(pZip, MZ_FALSE);
6065 return mz_zip_set_error(pZip, MZ_ZIP_FILE_OPEN_FAILED);
6066 }
6067 }
6068  
6069 pZip->m_pWrite = mz_zip_file_write_func;
6070 pZip->m_pNeeds_keepalive = NULL;
6071 #endif /* #ifdef MINIZ_NO_STDIO */
6072 }
6073 0 ▶ else if (pState->m_pMem)
Branch: 1 branch
BranchTrueFalseTotal
else if (pState->m_pMem) 0 0 0
6074 {
6075 /* Archive lives in a memory block. Assume it's from the heap that we can resize using the realloc callback. */
6076 0 ▶ if (pZip->m_pIO_opaque != pZip)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pIO_opaque != pZip) 0 0 0
6077 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
6078  
6079 0 pState->m_mem_capacity = pState->m_mem_size;
6080 0 pZip->m_pWrite = mz_zip_heap_write_func;
6081 0 pZip->m_pNeeds_keepalive = NULL;
6082 }
6083 /* Archive is being read via a user provided read function - make sure the user has specified a write function too. */
6084 0 ▶ else if (!pZip->m_pWrite)
Branch: 1 branch
BranchTrueFalseTotal
else if (!pZip->m_pWrite) 0 0 0
6085 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
6086  
6087 /* Start writing new files at the archive's current central directory location. */
6088 /* TODO: We could add a flag that lets the user start writing immediately AFTER the existing central dir - this would be safer. */
6089 0 pZip->m_archive_size = pZip->m_central_directory_file_ofs;
6090 0 pZip->m_central_directory_file_ofs = 0;
6091  
6092 /* Clear the sorted central dir offsets, they aren't useful or maintained now. */
6093 /* Even though we're now in write mode, files can still be extracted and verified, but file locates will be slow. */
6094 /* TODO: We could easily maintain the sorted central directory offsets. */
6095 0 mz_zip_array_clear(pZip, &pZip->m_pState->m_sorted_central_dir_offsets);
6096  
6097 0 pZip->m_zip_mode = MZ_ZIP_MODE_WRITING;
6098  
6099 0 return MZ_TRUE;
6100 }
6101  
6102 0 mz_bool mz_zip_writer_init_from_reader(mz_zip_archive *pZip, const char *pFilename)
6103 {
6104 0 return mz_zip_writer_init_from_reader_v2(pZip, pFilename, 0);
6105 }
6106  
6107 /* TODO: pArchive_name is a terrible name here! */
6108 0 mz_bool mz_zip_writer_add_mem(mz_zip_archive *pZip, const char *pArchive_name, const void *pBuf, size_t buf_size, mz_uint level_and_flags)
6109 {
6110 0 return mz_zip_writer_add_mem_ex(pZip, pArchive_name, pBuf, buf_size, NULL, 0, level_and_flags, 0, 0);
6111 }
6112  
6113 typedef struct
6114 {
6115 mz_zip_archive *m_pZip;
6116 mz_uint64 m_cur_archive_file_ofs;
6117 mz_uint64 m_comp_size;
6118 } mz_zip_writer_add_state;
6119  
6120 0 static mz_bool mz_zip_writer_add_put_buf_callback(const void *pBuf, int len, void *pUser)
6121 {
6122 0 mz_zip_writer_add_state *pState = (mz_zip_writer_add_state *)pUser;
6123 0 ▶ if ((int)pState->m_pZip->m_pWrite(pState->m_pZip->m_pIO_opaque, pState->m_cur_archive_file_ofs, pBuf, len) != len)
Branch: 1 branch
BranchTrueFalseTotal
if ((int)pState->m_pZip->m_pWrite(pState->m_pZip->m_pIO_opaque, pState->m_cur_archive_file_ofs, pBuf, len) != len) 0 0 0
6124 0 return MZ_FALSE;
6125  
6126 0 pState->m_cur_archive_file_ofs += len;
6127 0 pState->m_comp_size += len;
6128 0 return MZ_TRUE;
6129 }
6130  
6131 #define MZ_ZIP64_MAX_LOCAL_EXTRA_FIELD_SIZE (sizeof(mz_uint16) * 2 + sizeof(mz_uint64) * 2)
6132 #define MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE (sizeof(mz_uint16) * 2 + sizeof(mz_uint64) * 3)
6133 0 static mz_uint32 mz_zip_writer_create_zip64_extra_data(mz_uint8 *pBuf, mz_uint64 *pUncomp_size, mz_uint64 *pComp_size, mz_uint64 *pLocal_header_ofs)
6134 {
6135 0 mz_uint8 *pDst = pBuf;
6136 0 mz_uint32 field_size = 0;
6137  
6138 MZ_WRITE_LE16(pDst + 0, MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID);
6139 0 MZ_WRITE_LE16(pDst + 2, 0);
6140 0 pDst += sizeof(mz_uint16) * 2;
6141  
6142 0 ▶ if (pUncomp_size)
Branch: 1 branch
BranchTrueFalseTotal
if (pUncomp_size) 0 0 0
6143 {
6144 0 MZ_WRITE_LE64(pDst, *pUncomp_size);
6145 0 pDst += sizeof(mz_uint64);
6146 0 field_size += sizeof(mz_uint64);
6147 }
6148  
6149 0 ▶ if (pComp_size)
Branch: 1 branch
BranchTrueFalseTotal
if (pComp_size) 0 0 0
6150 {
6151 0 MZ_WRITE_LE64(pDst, *pComp_size);
6152 0 pDst += sizeof(mz_uint64);
6153 0 field_size += sizeof(mz_uint64);
6154 }
6155  
6156 0 ▶ if (pLocal_header_ofs)
Branch: 1 branch
BranchTrueFalseTotal
if (pLocal_header_ofs) 0 0 0
6157 {
6158 0 MZ_WRITE_LE64(pDst, *pLocal_header_ofs);
6159 0 pDst += sizeof(mz_uint64);
6160 0 field_size += sizeof(mz_uint64);
6161 }
6162  
6163 0 MZ_WRITE_LE16(pBuf + 2, field_size);
6164  
6165 0 return (mz_uint32)(pDst - pBuf);
6166 }
6167  
6168 0 static mz_bool mz_zip_writer_create_local_dir_header(mz_zip_archive *pZip, mz_uint8 *pDst, mz_uint16 filename_size, mz_uint16 extra_size, mz_uint64 uncomp_size, mz_uint64 comp_size, mz_uint32 uncomp_crc32, mz_uint16 method, mz_uint16 bit_flags, mz_uint16 dos_time, mz_uint16 dos_date)
6169 {
6170 (void)pZip;
6171 0 memset(pDst, 0, MZ_ZIP_LOCAL_DIR_HEADER_SIZE);
6172 MZ_WRITE_LE32(pDst + MZ_ZIP_LDH_SIG_OFS, MZ_ZIP_LOCAL_DIR_HEADER_SIG);
6173 0 ▶ MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_VERSION_NEEDED_OFS, method ? 20 : 0);
Branch: 1 branch
BranchTrueFalseTotal
MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_VERSION_NEEDED_OFS, method ? 20 : 0); 0 0 0
6174 0 MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_BIT_FLAG_OFS, bit_flags);
6175 0 MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_METHOD_OFS, method);
6176 0 MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_FILE_TIME_OFS, dos_time);
6177 0 MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_FILE_DATE_OFS, dos_date);
6178 0 MZ_WRITE_LE32(pDst + MZ_ZIP_LDH_CRC32_OFS, uncomp_crc32);
6179 0 ▶ MZ_WRITE_LE32(pDst + MZ_ZIP_LDH_COMPRESSED_SIZE_OFS, MZ_MIN(comp_size, MZ_UINT32_MAX));
Branch: 1 branch
BranchTrueFalseTotal
MZ_WRITE_LE32(pDst + MZ_ZIP_LDH_COMPRESSED_SIZE_OFS, MZ_MIN(comp_size, MZ_UINT32_MAX)); 0 0 0
6180 0 ▶ MZ_WRITE_LE32(pDst + MZ_ZIP_LDH_DECOMPRESSED_SIZE_OFS, MZ_MIN(uncomp_size, MZ_UINT32_MAX));
Branch: 1 branch
BranchTrueFalseTotal
MZ_WRITE_LE32(pDst + MZ_ZIP_LDH_DECOMPRESSED_SIZE_OFS, MZ_MIN(uncomp_size, MZ_UINT32_MAX)); 0 0 0
6181 0 MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_FILENAME_LEN_OFS, filename_size);
6182 0 MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_EXTRA_LEN_OFS, extra_size);
6183 0 return MZ_TRUE;
6184 }
6185  
6186 0 static mz_bool mz_zip_writer_create_central_dir_header(mz_zip_archive *pZip, mz_uint8 *pDst,
6187 mz_uint16 filename_size, mz_uint16 extra_size, mz_uint16 comment_size,
6188 mz_uint64 uncomp_size, mz_uint64 comp_size, mz_uint32 uncomp_crc32,
6189 mz_uint16 method, mz_uint16 bit_flags, mz_uint16 dos_time, mz_uint16 dos_date,
6190 mz_uint64 local_header_ofs, mz_uint32 ext_attributes)
6191 {
6192 (void)pZip;
6193 0 memset(pDst, 0, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE);
6194 MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_SIG_OFS, MZ_ZIP_CENTRAL_DIR_HEADER_SIG);
6195 0 ▶ MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_VERSION_NEEDED_OFS, method ? 20 : 0);
Branch: 1 branch
BranchTrueFalseTotal
MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_VERSION_NEEDED_OFS, method ? 20 : 0); 0 0 0
6196 0 MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_BIT_FLAG_OFS, bit_flags);
6197 0 MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_METHOD_OFS, method);
6198 0 MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_FILE_TIME_OFS, dos_time);
6199 0 MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_FILE_DATE_OFS, dos_date);
6200 0 MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_CRC32_OFS, uncomp_crc32);
6201 0 ▶ MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS, MZ_MIN(comp_size, MZ_UINT32_MAX));
Branch: 1 branch
BranchTrueFalseTotal
MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS, MZ_MIN(comp_size, MZ_UINT32_MAX)); 0 0 0
6202 0 ▶ MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS, MZ_MIN(uncomp_size, MZ_UINT32_MAX));
Branch: 1 branch
BranchTrueFalseTotal
MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS, MZ_MIN(uncomp_size, MZ_UINT32_MAX)); 0 0 0
6203 0 MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_FILENAME_LEN_OFS, filename_size);
6204 0 MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_EXTRA_LEN_OFS, extra_size);
6205 0 MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_COMMENT_LEN_OFS, comment_size);
6206 0 MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_EXTERNAL_ATTR_OFS, ext_attributes);
6207 0 ▶ MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_LOCAL_HEADER_OFS, MZ_MIN(local_header_ofs, MZ_UINT32_MAX));
Branch: 1 branch
BranchTrueFalseTotal
MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_LOCAL_HEADER_OFS, MZ_MIN(local_header_ofs, MZ_UINT32_MAX)); 0 0 0
6208 0 return MZ_TRUE;
6209 }
6210  
6211 0 static mz_bool mz_zip_writer_add_to_central_dir(mz_zip_archive *pZip, const char *pFilename, mz_uint16 filename_size,
6212 const void *pExtra, mz_uint16 extra_size, const void *pComment, mz_uint16 comment_size,
6213 mz_uint64 uncomp_size, mz_uint64 comp_size, mz_uint32 uncomp_crc32,
6214 mz_uint16 method, mz_uint16 bit_flags, mz_uint16 dos_time, mz_uint16 dos_date,
6215 mz_uint64 local_header_ofs, mz_uint32 ext_attributes,
6216 const char *user_extra_data, mz_uint user_extra_data_len)
6217 {
6218 0 mz_zip_internal_state *pState = pZip->m_pState;
6219 0 mz_uint32 central_dir_ofs = (mz_uint32)pState->m_central_dir.m_size;
6220 0 size_t orig_central_dir_size = pState->m_central_dir.m_size;
6221 mz_uint8 central_dir_header[MZ_ZIP_CENTRAL_DIR_HEADER_SIZE];
6222  
6223 0 ▶ if (!pZip->m_pState->m_zip64)
Branch: 1 branch
BranchTrueFalseTotal
if (!pZip->m_pState->m_zip64) 0 0 0
6224 {
6225 0 ▶ if (local_header_ofs > 0xFFFFFFFF)
Branch: 1 branch
BranchTrueFalseTotal
if (local_header_ofs > 0xFFFFFFFF) 0 0 0
6226 0 return mz_zip_set_error(pZip, MZ_ZIP_FILE_TOO_LARGE);
6227 }
6228  
6229 /* miniz doesn't support central dirs >= MZ_UINT32_MAX bytes yet */
6230 0 ▶ if (((mz_uint64)pState->m_central_dir.m_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + filename_size + extra_size + user_extra_data_len + comment_size) >= MZ_UINT32_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (((mz_uint64)pState->m_central_dir.m_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + filename_size + extra_size + user_extra_data_len + comment_size) >= MZ_UINT32_MAX) 0 0 0
6231 0 return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_CDIR_SIZE);
6232  
6233 0 ▶ if (!mz_zip_writer_create_central_dir_header(pZip, central_dir_header, filename_size, (mz_uint16)(extra_size + user_extra_data_len), comment_size, uncomp_size, comp_size, uncomp_crc32, method, bit_flags, dos_time, dos_date, local_header_ofs, ext_attributes))
Branch: 1 branch
BranchTrueFalseTotal
if (!mz_zip_writer_create_central_dir_header(pZip, central_dir_header, filename_size, (mz_uint16)(extra_size + user_extra_data_len), comment_size, uncomp_size, comp_size, uncomp_crc32, method, bit_flags, dos_time, dos_date, local_header_ofs, ext_attributes)) 0 0 0
6234 0 return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);
6235  
6236 0 ▶ if ((!mz_zip_array_push_back(pZip, &pState->m_central_dir, central_dir_header, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE)) ||
Branch: 2 branches
BranchTrueFalseTotal
if ((!mz_zip_array_push_back(pZip, &pState->m_central_dir, central_dir_header, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE)) || 0 0 0
if ((!mz_zip_array_push_back(pZip, &pState->m_central_dir, central_dir_header, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE)) || 0 0 0
6237 0 ▶ (!mz_zip_array_push_back(pZip, &pState->m_central_dir, pFilename, filename_size)) ||
Branch: 2 branches
BranchTrueFalseTotal
(!mz_zip_array_push_back(pZip, &pState->m_central_dir, pFilename, filename_size)) || 0 0 0
(!mz_zip_array_push_back(pZip, &pState->m_central_dir, pFilename, filename_size)) || 0 0 0
6238 0 ▶ (!mz_zip_array_push_back(pZip, &pState->m_central_dir, pExtra, extra_size)) ||
Branch: 2 branches
BranchTrueFalseTotal
(!mz_zip_array_push_back(pZip, &pState->m_central_dir, pExtra, extra_size)) || 0 0 0
(!mz_zip_array_push_back(pZip, &pState->m_central_dir, pExtra, extra_size)) || 0 0 0
6239 0 ▶ (!mz_zip_array_push_back(pZip, &pState->m_central_dir, user_extra_data, user_extra_data_len)) ||
Branch: 2 branches
BranchTrueFalseTotal
(!mz_zip_array_push_back(pZip, &pState->m_central_dir, user_extra_data, user_extra_data_len)) || 0 0 0
(!mz_zip_array_push_back(pZip, &pState->m_central_dir, user_extra_data, user_extra_data_len)) || 0 0 0
6240 0 ▶ (!mz_zip_array_push_back(pZip, &pState->m_central_dir, pComment, comment_size)) ||
Branch: 2 branches
BranchTrueFalseTotal
(!mz_zip_array_push_back(pZip, &pState->m_central_dir, pComment, comment_size)) || 0 0 0
(!mz_zip_array_push_back(pZip, &pState->m_central_dir, pComment, comment_size)) || 0 0 0
6241 0 ▶ (!mz_zip_array_push_back(pZip, &pState->m_central_dir_offsets, &central_dir_ofs, 1)))
Branch: 2 branches
BranchTrueFalseTotal
(!mz_zip_array_push_back(pZip, &pState->m_central_dir_offsets, &central_dir_ofs, 1))) 0 0 0
(!mz_zip_array_push_back(pZip, &pState->m_central_dir_offsets, &central_dir_ofs, 1))) 0 0 0
6242 {
6243 /* Try to resize the central directory array back into its original state. */
6244 0 ▶ mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE);
Branch: 1 branch
BranchTrueFalseTotal
mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE); 0 0 0
6245 0 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
6246 }
6247  
6248 0 return MZ_TRUE;
6249 }
6250  
6251 0 static mz_bool mz_zip_writer_validate_archive_name(const char *pArchive_name)
6252 {
6253 /* Basic ZIP archive filename validity checks: Valid filenames cannot start with a forward slash, cannot contain a drive letter, and cannot use DOS-style backward slashes. */
6254 0 ▶ if (*pArchive_name == '/')
Branch: 1 branch
BranchTrueFalseTotal
if (*pArchive_name == '/') 0 0 0
6255 0 return MZ_FALSE;
6256  
6257 /* Making sure the name does not contain drive letters or DOS style backward slashes is the responsibility of the program using miniz*/
6258  
6259 0 return MZ_TRUE;
6260 }
6261  
6262 0 static mz_uint mz_zip_writer_compute_padding_needed_for_file_alignment(mz_zip_archive *pZip)
6263 {
6264 mz_uint32 n;
6265 0 ▶ if (!pZip->m_file_offset_alignment)
Branch: 1 branch
BranchTrueFalseTotal
if (!pZip->m_file_offset_alignment) 0 0 0
6266 0 return 0;
6267 0 n = (mz_uint32)(pZip->m_archive_size & (pZip->m_file_offset_alignment - 1));
6268 0 return (mz_uint)((pZip->m_file_offset_alignment - n) & (pZip->m_file_offset_alignment - 1));
6269 }
6270  
6271 0 static mz_bool mz_zip_writer_write_zeros(mz_zip_archive *pZip, mz_uint64 cur_file_ofs, mz_uint32 n)
6272 {
6273 char buf[4096];
6274 0 memset(buf, 0, MZ_MIN(sizeof(buf), n));
6275 0 ▶ while (n)
Branch: 1 branch
BranchTrueFalseTotal
while (n) 0 0 0
6276 {
6277 0 mz_uint32 s = MZ_MIN(sizeof(buf), n);
6278 0 ▶ if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_file_ofs, buf, s) != s)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_file_ofs, buf, s) != s) 0 0 0
6279 0 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
6280  
6281 0 cur_file_ofs += s;
6282 0 n -= s;
6283 }
6284 0 return MZ_TRUE;
6285 }
6286  
6287 0 mz_bool mz_zip_writer_add_mem_ex(mz_zip_archive *pZip, const char *pArchive_name, const void *pBuf, size_t buf_size, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags,
6288 mz_uint64 uncomp_size, mz_uint32 uncomp_crc32)
6289 {
6290 0 return mz_zip_writer_add_mem_ex_v2(pZip, pArchive_name, pBuf, buf_size, pComment, comment_size, level_and_flags, uncomp_size, uncomp_crc32, NULL, NULL, 0, NULL, 0);
6291 }
6292  
6293 0 mz_bool mz_zip_writer_add_mem_ex_v2(mz_zip_archive *pZip, const char *pArchive_name, const void *pBuf, size_t buf_size, const void *pComment, mz_uint16 comment_size,
6294 mz_uint level_and_flags, mz_uint64 uncomp_size, mz_uint32 uncomp_crc32, MZ_TIME_T *last_modified,
6295 const char *user_extra_data, mz_uint user_extra_data_len, const char *user_extra_data_central, mz_uint user_extra_data_central_len)
6296 {
6297 0 mz_uint16 method = 0, dos_time = 0, dos_date = 0;
6298 0 mz_uint level, ext_attributes = 0, num_alignment_padding_bytes;
6299 0 mz_uint64 local_dir_header_ofs = pZip->m_archive_size, cur_archive_file_ofs = pZip->m_archive_size, comp_size = 0;
6300 size_t archive_name_size;
6301 mz_uint8 local_dir_header[MZ_ZIP_LOCAL_DIR_HEADER_SIZE];
6302 0 tdefl_compressor *pComp = NULL;
6303 mz_bool store_data_uncompressed;
6304 mz_zip_internal_state *pState;
6305 0 mz_uint8 *pExtra_data = NULL;
6306 0 mz_uint32 extra_size = 0;
6307 mz_uint8 extra_data[MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE];
6308 0 mz_uint16 bit_flags = 0;
6309  
6310 0 ▶ if ((int)level_and_flags < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((int)level_and_flags < 0) 0 0 0
6311 0 level_and_flags = MZ_DEFAULT_LEVEL;
6312  
6313 0 ▶ if (uncomp_size || (buf_size && !(level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)))
Branch: 3 branches
BranchTrueFalseTotal
if (uncomp_size || (buf_size && !(level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA))) 0 0 0
if (uncomp_size || (buf_size && !(level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA))) 0 0 0
if (uncomp_size || (buf_size && !(level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA))) 0 0 0
6314 0 bit_flags |= MZ_ZIP_LDH_BIT_FLAG_HAS_LOCATOR;
6315  
6316 0 ▶ if (!(level_and_flags & MZ_ZIP_FLAG_ASCII_FILENAME))
Branch: 1 branch
BranchTrueFalseTotal
if (!(level_and_flags & MZ_ZIP_FLAG_ASCII_FILENAME)) 0 0 0
6317 0 bit_flags |= MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_UTF8;
6318  
6319 0 level = level_and_flags & 0xF;
6320 0 ▶ store_data_uncompressed = ((!level) || (level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA));
Branch: 2 branches
BranchTrueFalseTotal
store_data_uncompressed = ((!level) || (level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)); 0 0 0
store_data_uncompressed = ((!level) || (level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)); 0 0 0
6321  
6322 0 ▶ if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || ((buf_size) && (!pBuf)) || (!pArchive_name) || ((comment_size) && (!pComment)) || (level > MZ_UBER_COMPRESSION))
Branch: 9 branches
BranchTrueFalseTotal
if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || ((buf_size) && (!pBuf)) || (!pArchive_name) || ((comment_size) && (!pComment)) || (level > MZ_UBER_COMPRESSION)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || ((buf_size) && (!pBuf)) || (!pArchive_name) || ((comment_size) && (!pComment)) || (level > MZ_UBER_COMPRESSION)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || ((buf_size) && (!pBuf)) || (!pArchive_name) || ((comment_size) && (!pComment)) || (level > MZ_UBER_COMPRESSION)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || ((buf_size) && (!pBuf)) || (!pArchive_name) || ((comment_size) && (!pComment)) || (level > MZ_UBER_COMPRESSION)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || ((buf_size) && (!pBuf)) || (!pArchive_name) || ((comment_size) && (!pComment)) || (level > MZ_UBER_COMPRESSION)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || ((buf_size) && (!pBuf)) || (!pArchive_name) || ((comment_size) && (!pComment)) || (level > MZ_UBER_COMPRESSION)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || ((buf_size) && (!pBuf)) || (!pArchive_name) || ((comment_size) && (!pComment)) || (level > MZ_UBER_COMPRESSION)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || ((buf_size) && (!pBuf)) || (!pArchive_name) || ((comment_size) && (!pComment)) || (level > MZ_UBER_COMPRESSION)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || ((buf_size) && (!pBuf)) || (!pArchive_name) || ((comment_size) && (!pComment)) || (level > MZ_UBER_COMPRESSION)) 0 0 0
6323 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
6324  
6325 0 pState = pZip->m_pState;
6326  
6327 0 ▶ if (pState->m_zip64)
Branch: 1 branch
BranchTrueFalseTotal
if (pState->m_zip64) 0 0 0
6328 {
6329 0 ▶ if (pZip->m_total_files == MZ_UINT32_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_total_files == MZ_UINT32_MAX) 0 0 0
6330 0 return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);
6331 }
6332 else
6333 {
6334 0 ▶ if (pZip->m_total_files == MZ_UINT16_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_total_files == MZ_UINT16_MAX) 0 0 0
6335 {
6336 0 pState->m_zip64 = MZ_TRUE;
6337 /*return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES); */
6338 }
6339 0 ▶ if (((mz_uint64)buf_size > 0xFFFFFFFF) || (uncomp_size > 0xFFFFFFFF))
Branch: 2 branches
BranchTrueFalseTotal
if (((mz_uint64)buf_size > 0xFFFFFFFF) || (uncomp_size > 0xFFFFFFFF)) 0 0 0
if (((mz_uint64)buf_size > 0xFFFFFFFF) || (uncomp_size > 0xFFFFFFFF)) 0 0 0
6340 {
6341 0 pState->m_zip64 = MZ_TRUE;
6342 /*return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); */
6343 }
6344 }
6345  
6346 0 ▶ if ((!(level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (uncomp_size))
Branch: 2 branches
BranchTrueFalseTotal
if ((!(level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (uncomp_size)) 0 0 0
if ((!(level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (uncomp_size)) 0 0 0
6347 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
6348  
6349 0 ▶ if (!mz_zip_writer_validate_archive_name(pArchive_name))
Branch: 1 branch
BranchTrueFalseTotal
if (!mz_zip_writer_validate_archive_name(pArchive_name)) 0 0 0
6350 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_FILENAME);
6351  
6352 #ifndef MINIZ_NO_TIME
6353 0 ▶ if (last_modified != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (last_modified != NULL) 0 0 0
6354 {
6355 0 mz_zip_time_t_to_dos_time(*last_modified, &dos_time, &dos_date);
6356 }
6357 else
6358 {
6359 MZ_TIME_T cur_time;
6360 0 time(&cur_time);
6361 0 mz_zip_time_t_to_dos_time(cur_time, &dos_time, &dos_date);
6362 }
6363 #else
6364 (void)last_modified;
6365 #endif /* #ifndef MINIZ_NO_TIME */
6366  
6367 0 ▶ if (!(level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA))
Branch: 1 branch
BranchTrueFalseTotal
if (!(level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) 0 0 0
6368 {
6369 0 uncomp_crc32 = (mz_uint32)mz_crc32(MZ_CRC32_INIT, (const mz_uint8 *)pBuf, buf_size);
6370 0 uncomp_size = buf_size;
6371 0 ▶ if (uncomp_size <= 3)
Branch: 1 branch
BranchTrueFalseTotal
if (uncomp_size <= 3) 0 0 0
6372 {
6373 0 level = 0;
6374 0 store_data_uncompressed = MZ_TRUE;
6375 }
6376 }
6377  
6378 0 archive_name_size = strlen(pArchive_name);
6379 0 ▶ if (archive_name_size > MZ_UINT16_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (archive_name_size > MZ_UINT16_MAX) 0 0 0
6380 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_FILENAME);
6381  
6382 0 num_alignment_padding_bytes = mz_zip_writer_compute_padding_needed_for_file_alignment(pZip);
6383  
6384 /* miniz doesn't support central dirs >= MZ_UINT32_MAX bytes yet */
6385 0 ▶ if (((mz_uint64)pState->m_central_dir.m_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + archive_name_size + MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE + comment_size) >= MZ_UINT32_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (((mz_uint64)pState->m_central_dir.m_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + archive_name_size + MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE + comment_size) >= MZ_UINT32_MAX) 0 0 0
6386 0 return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_CDIR_SIZE);
6387  
6388 0 ▶ if (!pState->m_zip64)
Branch: 1 branch
BranchTrueFalseTotal
if (!pState->m_zip64) 0 0 0
6389 {
6390 /* Bail early if the archive would obviously become too large */
6391 0 if ((pZip->m_archive_size + num_alignment_padding_bytes + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + archive_name_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + archive_name_size + comment_size + user_extra_data_len +
6392 0 ▶ pState->m_central_dir.m_size + MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE + user_extra_data_central_len + MZ_ZIP_DATA_DESCRIPTER_SIZE32) > 0xFFFFFFFF)
Branch: 1 branch
BranchTrueFalseTotal
pState->m_central_dir.m_size + MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE + user_extra_data_central_len + MZ_ZIP_DATA_DESCRIPTER_SIZE32) > 0xFFFFFFFF) 0 0 0
6393 {
6394 0 pState->m_zip64 = MZ_TRUE;
6395 /*return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); */
6396 }
6397 }
6398  
6399 0 ▶ if ((archive_name_size) && (pArchive_name[archive_name_size - 1] == '/'))
Branch: 2 branches
BranchTrueFalseTotal
if ((archive_name_size) && (pArchive_name[archive_name_size - 1] == '/')) 0 0 0
if ((archive_name_size) && (pArchive_name[archive_name_size - 1] == '/')) 0 0 0
6400 {
6401 /* Set DOS Subdirectory attribute bit. */
6402 0 ext_attributes |= MZ_ZIP_DOS_DIR_ATTRIBUTE_BITFLAG;
6403  
6404 /* Subdirectories cannot contain data. */
6405 0 ▶ if ((buf_size) || (uncomp_size))
Branch: 2 branches
BranchTrueFalseTotal
if ((buf_size) || (uncomp_size)) 0 0 0
if ((buf_size) || (uncomp_size)) 0 0 0
6406 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
6407 }
6408  
6409 /* Try to do any allocations before writing to the archive, so if an allocation fails the file remains unmodified. (A good idea if we're doing an in-place modification.) */
6410 0 ▶ if ((!mz_zip_array_ensure_room(pZip, &pState->m_central_dir, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + archive_name_size + comment_size + (pState->m_zip64 ? MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE : 0))) || (!mz_zip_array_ensure_room(pZip, &pState->m_central_dir_offsets, 1)))
Branch: 5 branches
BranchTrueFalseTotal
if ((!mz_zip_array_ensure_room(pZip, &pState->m_central_dir, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + archive_name_size + comment_size + (pState->m_zip64 ? MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE : 0))) || (!mz_zip_array_ensure_room(pZip, &pState->m_central_dir_offsets, 1))) 0 0 0
if ((!mz_zip_array_ensure_room(pZip, &pState->m_central_dir, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + archive_name_size + comment_size + (pState->m_zip64 ? MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE : 0))) || (!mz_zip_array_ensure_room(pZip, &pState->m_central_dir_offsets, 1))) 0 0 0
if ((!mz_zip_array_ensure_room(pZip, &pState->m_central_dir, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + archive_name_size + comment_size + (pState->m_zip64 ? MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE : 0))) || (!mz_zip_array_ensure_room(pZip, &pState->m_central_dir_offsets, 1))) 0 0 0
if ((!mz_zip_array_ensure_room(pZip, &pState->m_central_dir, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + archive_name_size + comment_size + (pState->m_zip64 ? MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE : 0))) || (!mz_zip_array_ensure_room(pZip, &pState->m_central_dir_offsets, 1))) 0 0 0
if ((!mz_zip_array_ensure_room(pZip, &pState->m_central_dir, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + archive_name_size + comment_size + (pState->m_zip64 ? MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE : 0))) || (!mz_zip_array_ensure_room(pZip, &pState->m_central_dir_offsets, 1))) 0 0 0
6411 0 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
6412  
6413 0 ▶ if ((!store_data_uncompressed) && (buf_size))
Branch: 2 branches
BranchTrueFalseTotal
if ((!store_data_uncompressed) && (buf_size)) 0 0 0
if ((!store_data_uncompressed) && (buf_size)) 0 0 0
6414 {
6415 0 ▶ if (NULL == (pComp = (tdefl_compressor *)pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, sizeof(tdefl_compressor))))
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == (pComp = (tdefl_compressor *)pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, sizeof(tdefl_compressor)))) 0 0 0
6416 0 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
6417 }
6418  
6419 0 ▶ if (!mz_zip_writer_write_zeros(pZip, cur_archive_file_ofs, num_alignment_padding_bytes))
Branch: 1 branch
BranchTrueFalseTotal
if (!mz_zip_writer_write_zeros(pZip, cur_archive_file_ofs, num_alignment_padding_bytes)) 0 0 0
6420 {
6421 0 pZip->m_pFree(pZip->m_pAlloc_opaque, pComp);
6422 0 return MZ_FALSE;
6423 }
6424  
6425 0 local_dir_header_ofs += num_alignment_padding_bytes;
6426 0 if (pZip->m_file_offset_alignment)
6427 {
6428 MZ_ASSERT((local_dir_header_ofs & (pZip->m_file_offset_alignment - 1)) == 0);
6429 }
6430 0 cur_archive_file_ofs += num_alignment_padding_bytes;
6431  
6432 0 MZ_CLEAR_ARR(local_dir_header);
6433  
6434 0 ▶ if (!store_data_uncompressed || (level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA))
Branch: 2 branches
BranchTrueFalseTotal
if (!store_data_uncompressed || (level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) 0 0 0
if (!store_data_uncompressed || (level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) 0 0 0
6435 {
6436 0 method = MZ_DEFLATED;
6437 }
6438  
6439 0 ▶ if (pState->m_zip64)
Branch: 1 branch
BranchTrueFalseTotal
if (pState->m_zip64) 0 0 0
6440 {
6441 0 ▶ if (uncomp_size >= MZ_UINT32_MAX || local_dir_header_ofs >= MZ_UINT32_MAX)
Branch: 2 branches
BranchTrueFalseTotal
if (uncomp_size >= MZ_UINT32_MAX || local_dir_header_ofs >= MZ_UINT32_MAX) 0 0 0
if (uncomp_size >= MZ_UINT32_MAX || local_dir_header_ofs >= MZ_UINT32_MAX) 0 0 0
6442 {
6443 0 pExtra_data = extra_data;
6444 0 ▶ extra_size = mz_zip_writer_create_zip64_extra_data(extra_data, (uncomp_size >= MZ_UINT32_MAX) ? &uncomp_size : NULL,
Branch: 1 branch
BranchTrueFalseTotal
extra_size = mz_zip_writer_create_zip64_extra_data(extra_data, (uncomp_size >= MZ_UINT32_MAX) ? &uncomp_size : NULL, 0 0 0
6445 0 ▶ (uncomp_size >= MZ_UINT32_MAX) ? &comp_size : NULL, (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL);
Branch: 2 branches
BranchTrueFalseTotal
(uncomp_size >= MZ_UINT32_MAX) ? &comp_size : NULL, (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL); 0 0 0
(uncomp_size >= MZ_UINT32_MAX) ? &comp_size : NULL, (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL); 0 0 0
6446 }
6447  
6448 0 ▶ if (!mz_zip_writer_create_local_dir_header(pZip, local_dir_header, (mz_uint16)archive_name_size, (mz_uint16)(extra_size + user_extra_data_len), 0, 0, 0, method, bit_flags, dos_time, dos_date))
Branch: 1 branch
BranchTrueFalseTotal
if (!mz_zip_writer_create_local_dir_header(pZip, local_dir_header, (mz_uint16)archive_name_size, (mz_uint16)(extra_size + user_extra_data_len), 0, 0, 0, method, bit_flags, dos_time, dos_date)) 0 0 0
6449 0 return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);
6450  
6451 0 ▶ if (pZip->m_pWrite(pZip->m_pIO_opaque, local_dir_header_ofs, local_dir_header, sizeof(local_dir_header)) != sizeof(local_dir_header))
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pWrite(pZip->m_pIO_opaque, local_dir_header_ofs, local_dir_header, sizeof(local_dir_header)) != sizeof(local_dir_header)) 0 0 0
6452 0 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
6453  
6454 0 cur_archive_file_ofs += sizeof(local_dir_header);
6455  
6456 0 ▶ if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pArchive_name, archive_name_size) != archive_name_size)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pArchive_name, archive_name_size) != archive_name_size) 0 0 0
6457 {
6458 0 pZip->m_pFree(pZip->m_pAlloc_opaque, pComp);
6459 0 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
6460 }
6461 0 cur_archive_file_ofs += archive_name_size;
6462  
6463 0 ▶ if (pExtra_data != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (pExtra_data != NULL) 0 0 0
6464 {
6465 0 ▶ if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, extra_data, extra_size) != extra_size)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, extra_data, extra_size) != extra_size) 0 0 0
6466 0 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
6467  
6468 0 cur_archive_file_ofs += extra_size;
6469 }
6470 }
6471 else
6472 {
6473 0 ▶ if ((comp_size > MZ_UINT32_MAX) || (cur_archive_file_ofs > MZ_UINT32_MAX))
Branch: 2 branches
BranchTrueFalseTotal
if ((comp_size > MZ_UINT32_MAX) || (cur_archive_file_ofs > MZ_UINT32_MAX)) 0 0 0
if ((comp_size > MZ_UINT32_MAX) || (cur_archive_file_ofs > MZ_UINT32_MAX)) 0 0 0
6474 0 return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);
6475 0 ▶ if (!mz_zip_writer_create_local_dir_header(pZip, local_dir_header, (mz_uint16)archive_name_size, (mz_uint16)user_extra_data_len, 0, 0, 0, method, bit_flags, dos_time, dos_date))
Branch: 1 branch
BranchTrueFalseTotal
if (!mz_zip_writer_create_local_dir_header(pZip, local_dir_header, (mz_uint16)archive_name_size, (mz_uint16)user_extra_data_len, 0, 0, 0, method, bit_flags, dos_time, dos_date)) 0 0 0
6476 0 return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);
6477  
6478 0 ▶ if (pZip->m_pWrite(pZip->m_pIO_opaque, local_dir_header_ofs, local_dir_header, sizeof(local_dir_header)) != sizeof(local_dir_header))
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pWrite(pZip->m_pIO_opaque, local_dir_header_ofs, local_dir_header, sizeof(local_dir_header)) != sizeof(local_dir_header)) 0 0 0
6479 0 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
6480  
6481 0 cur_archive_file_ofs += sizeof(local_dir_header);
6482  
6483 0 ▶ if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pArchive_name, archive_name_size) != archive_name_size)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pArchive_name, archive_name_size) != archive_name_size) 0 0 0
6484 {
6485 0 pZip->m_pFree(pZip->m_pAlloc_opaque, pComp);
6486 0 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
6487 }
6488 0 cur_archive_file_ofs += archive_name_size;
6489 }
6490  
6491 0 ▶ if (user_extra_data_len > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (user_extra_data_len > 0) 0 0 0
6492 {
6493 0 ▶ if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, user_extra_data, user_extra_data_len) != user_extra_data_len)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, user_extra_data, user_extra_data_len) != user_extra_data_len) 0 0 0
6494 0 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
6495  
6496 0 cur_archive_file_ofs += user_extra_data_len;
6497 }
6498  
6499 0 ▶ if (store_data_uncompressed)
Branch: 1 branch
BranchTrueFalseTotal
if (store_data_uncompressed) 0 0 0
6500 {
6501 0 ▶ if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pBuf, buf_size) != buf_size)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pBuf, buf_size) != buf_size) 0 0 0
6502 {
6503 0 pZip->m_pFree(pZip->m_pAlloc_opaque, pComp);
6504 0 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
6505 }
6506  
6507 0 cur_archive_file_ofs += buf_size;
6508 0 comp_size = buf_size;
6509 }
6510 0 ▶ else if (buf_size)
Branch: 1 branch
BranchTrueFalseTotal
else if (buf_size) 0 0 0
6511 {
6512 mz_zip_writer_add_state state;
6513  
6514 0 state.m_pZip = pZip;
6515 0 state.m_cur_archive_file_ofs = cur_archive_file_ofs;
6516 0 state.m_comp_size = 0;
6517  
6518 0 if ((tdefl_init(pComp, mz_zip_writer_add_put_buf_callback, &state, tdefl_create_comp_flags_from_zip_params(level, -15, MZ_DEFAULT_STRATEGY)) != TDEFL_STATUS_OKAY) ||
6519 0 (tdefl_compress_buffer(pComp, pBuf, buf_size, TDEFL_FINISH) != TDEFL_STATUS_DONE))
6520 {
6521 0 pZip->m_pFree(pZip->m_pAlloc_opaque, pComp);
6522 0 return mz_zip_set_error(pZip, MZ_ZIP_COMPRESSION_FAILED);
6523 }
6524  
6525 0 comp_size = state.m_comp_size;
6526 0 cur_archive_file_ofs = state.m_cur_archive_file_ofs;
6527 }
6528  
6529 0 pZip->m_pFree(pZip->m_pAlloc_opaque, pComp);
6530 0 pComp = NULL;
6531  
6532 0 ▶ if (uncomp_size)
Branch: 1 branch
BranchTrueFalseTotal
if (uncomp_size) 0 0 0
6533 {
6534 mz_uint8 local_dir_footer[MZ_ZIP_DATA_DESCRIPTER_SIZE64];
6535 0 mz_uint32 local_dir_footer_size = MZ_ZIP_DATA_DESCRIPTER_SIZE32;
6536  
6537 MZ_ASSERT(bit_flags & MZ_ZIP_LDH_BIT_FLAG_HAS_LOCATOR);
6538  
6539 MZ_WRITE_LE32(local_dir_footer + 0, MZ_ZIP_DATA_DESCRIPTOR_ID);
6540 0 MZ_WRITE_LE32(local_dir_footer + 4, uncomp_crc32);
6541 0 ▶ if (pExtra_data == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (pExtra_data == NULL) 0 0 0
6542 {
6543 0 ▶ if (comp_size > MZ_UINT32_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (comp_size > MZ_UINT32_MAX) 0 0 0
6544 0 return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);
6545  
6546 0 MZ_WRITE_LE32(local_dir_footer + 8, comp_size);
6547 0 MZ_WRITE_LE32(local_dir_footer + 12, uncomp_size);
6548 }
6549 else
6550 {
6551 0 MZ_WRITE_LE64(local_dir_footer + 8, comp_size);
6552 0 MZ_WRITE_LE64(local_dir_footer + 16, uncomp_size);
6553 0 local_dir_footer_size = MZ_ZIP_DATA_DESCRIPTER_SIZE64;
6554 }
6555  
6556 0 ▶ if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, local_dir_footer, local_dir_footer_size) != local_dir_footer_size)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, local_dir_footer, local_dir_footer_size) != local_dir_footer_size) 0 0 0
6557 0 return MZ_FALSE;
6558  
6559 0 cur_archive_file_ofs += local_dir_footer_size;
6560 }
6561  
6562 0 ▶ if (pExtra_data != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (pExtra_data != NULL) 0 0 0
6563 {
6564 0 ▶ extra_size = mz_zip_writer_create_zip64_extra_data(extra_data, (uncomp_size >= MZ_UINT32_MAX) ? &uncomp_size : NULL,
Branch: 1 branch
BranchTrueFalseTotal
extra_size = mz_zip_writer_create_zip64_extra_data(extra_data, (uncomp_size >= MZ_UINT32_MAX) ? &uncomp_size : NULL, 0 0 0
6565 0 ▶ (uncomp_size >= MZ_UINT32_MAX) ? &comp_size : NULL, (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL);
Branch: 2 branches
BranchTrueFalseTotal
(uncomp_size >= MZ_UINT32_MAX) ? &comp_size : NULL, (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL); 0 0 0
(uncomp_size >= MZ_UINT32_MAX) ? &comp_size : NULL, (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL); 0 0 0
6566 }
6567  
6568 0 ▶ if (!mz_zip_writer_add_to_central_dir(pZip, pArchive_name, (mz_uint16)archive_name_size, pExtra_data, (mz_uint16)extra_size, pComment,
Branch: 1 branch
BranchTrueFalseTotal
if (!mz_zip_writer_add_to_central_dir(pZip, pArchive_name, (mz_uint16)archive_name_size, pExtra_data, (mz_uint16)extra_size, pComment, 0 0 0
6569 comment_size, uncomp_size, comp_size, uncomp_crc32, method, bit_flags, dos_time, dos_date, local_dir_header_ofs, ext_attributes,
6570 user_extra_data_central, user_extra_data_central_len))
6571 0 return MZ_FALSE;
6572  
6573 0 pZip->m_total_files++;
6574 0 pZip->m_archive_size = cur_archive_file_ofs;
6575  
6576 0 return MZ_TRUE;
6577 }
6578  
6579 0 mz_bool mz_zip_writer_add_read_buf_callback(mz_zip_archive *pZip, const char *pArchive_name, mz_file_read_func read_callback, void *callback_opaque, mz_uint64 max_size, const MZ_TIME_T *pFile_time, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags,
6580 const char *user_extra_data, mz_uint user_extra_data_len, const char *user_extra_data_central, mz_uint user_extra_data_central_len)
6581 {
6582 mz_uint16 gen_flags;
6583 0 mz_uint uncomp_crc32 = MZ_CRC32_INIT, level, num_alignment_padding_bytes;
6584 0 mz_uint16 method = 0, dos_time = 0, dos_date = 0, ext_attributes = 0;
6585 0 mz_uint64 local_dir_header_ofs, cur_archive_file_ofs = pZip->m_archive_size, uncomp_size = 0, comp_size = 0;
6586 size_t archive_name_size;
6587 mz_uint8 local_dir_header[MZ_ZIP_LOCAL_DIR_HEADER_SIZE];
6588 0 mz_uint8 *pExtra_data = NULL;
6589 0 mz_uint32 extra_size = 0;
6590 mz_uint8 extra_data[MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE];
6591 mz_zip_internal_state *pState;
6592 0 mz_uint64 file_ofs = 0, cur_archive_header_file_ofs;
6593  
6594 0 ▶ if ((int)level_and_flags < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((int)level_and_flags < 0) 0 0 0
6595 0 level_and_flags = MZ_DEFAULT_LEVEL;
6596 0 level = level_and_flags & 0xF;
6597  
6598 0 ▶ gen_flags = (level_and_flags & MZ_ZIP_FLAG_WRITE_HEADER_SET_SIZE) ? 0 : MZ_ZIP_LDH_BIT_FLAG_HAS_LOCATOR;
Branch: 1 branch
BranchTrueFalseTotal
gen_flags = (level_and_flags & MZ_ZIP_FLAG_WRITE_HEADER_SET_SIZE) ? 0 : MZ_ZIP_LDH_BIT_FLAG_HAS_LOCATOR; 0 0 0
6599  
6600 0 ▶ if (!(level_and_flags & MZ_ZIP_FLAG_ASCII_FILENAME))
Branch: 1 branch
BranchTrueFalseTotal
if (!(level_and_flags & MZ_ZIP_FLAG_ASCII_FILENAME)) 0 0 0
6601 0 gen_flags |= MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_UTF8;
6602  
6603 /* Sanity checks */
6604 0 ▶ if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || (!pArchive_name) || ((comment_size) && (!pComment)) || (level > MZ_UBER_COMPRESSION))
Branch: 7 branches
BranchTrueFalseTotal
if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || (!pArchive_name) || ((comment_size) && (!pComment)) || (level > MZ_UBER_COMPRESSION)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || (!pArchive_name) || ((comment_size) && (!pComment)) || (level > MZ_UBER_COMPRESSION)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || (!pArchive_name) || ((comment_size) && (!pComment)) || (level > MZ_UBER_COMPRESSION)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || (!pArchive_name) || ((comment_size) && (!pComment)) || (level > MZ_UBER_COMPRESSION)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || (!pArchive_name) || ((comment_size) && (!pComment)) || (level > MZ_UBER_COMPRESSION)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || (!pArchive_name) || ((comment_size) && (!pComment)) || (level > MZ_UBER_COMPRESSION)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || (!pArchive_name) || ((comment_size) && (!pComment)) || (level > MZ_UBER_COMPRESSION)) 0 0 0
6605 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
6606  
6607 0 pState = pZip->m_pState;
6608  
6609 0 ▶ if ((!pState->m_zip64) && (max_size > MZ_UINT32_MAX))
Branch: 2 branches
BranchTrueFalseTotal
if ((!pState->m_zip64) && (max_size > MZ_UINT32_MAX)) 0 0 0
if ((!pState->m_zip64) && (max_size > MZ_UINT32_MAX)) 0 0 0
6610 {
6611 /* Source file is too large for non-zip64 */
6612 /*return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); */
6613 0 pState->m_zip64 = MZ_TRUE;
6614 }
6615  
6616 /* We could support this, but why? */
6617 0 ▶ if (level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)
Branch: 1 branch
BranchTrueFalseTotal
if (level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA) 0 0 0
6618 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
6619  
6620 0 ▶ if (!mz_zip_writer_validate_archive_name(pArchive_name))
Branch: 1 branch
BranchTrueFalseTotal
if (!mz_zip_writer_validate_archive_name(pArchive_name)) 0 0 0
6621 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_FILENAME);
6622  
6623 0 ▶ if (pState->m_zip64)
Branch: 1 branch
BranchTrueFalseTotal
if (pState->m_zip64) 0 0 0
6624 {
6625 0 ▶ if (pZip->m_total_files == MZ_UINT32_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_total_files == MZ_UINT32_MAX) 0 0 0
6626 0 return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);
6627 }
6628 else
6629 {
6630 0 ▶ if (pZip->m_total_files == MZ_UINT16_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_total_files == MZ_UINT16_MAX) 0 0 0
6631 {
6632 0 pState->m_zip64 = MZ_TRUE;
6633 /*return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES); */
6634 }
6635 }
6636  
6637 0 archive_name_size = strlen(pArchive_name);
6638 0 ▶ if (archive_name_size > MZ_UINT16_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (archive_name_size > MZ_UINT16_MAX) 0 0 0
6639 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_FILENAME);
6640  
6641 0 num_alignment_padding_bytes = mz_zip_writer_compute_padding_needed_for_file_alignment(pZip);
6642  
6643 /* miniz doesn't support central dirs >= MZ_UINT32_MAX bytes yet */
6644 0 ▶ if (((mz_uint64)pState->m_central_dir.m_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + archive_name_size + MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE + comment_size) >= MZ_UINT32_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (((mz_uint64)pState->m_central_dir.m_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + archive_name_size + MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE + comment_size) >= MZ_UINT32_MAX) 0 0 0
6645 0 return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_CDIR_SIZE);
6646  
6647 0 ▶ if (!pState->m_zip64)
Branch: 1 branch
BranchTrueFalseTotal
if (!pState->m_zip64) 0 0 0
6648 {
6649 /* Bail early if the archive would obviously become too large */
6650 0 ▶ if ((pZip->m_archive_size + num_alignment_padding_bytes + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + archive_name_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + archive_name_size + comment_size + user_extra_data_len + pState->m_central_dir.m_size + MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE + 1024 + MZ_ZIP_DATA_DESCRIPTER_SIZE32 + user_extra_data_central_len) > 0xFFFFFFFF)
Branch: 1 branch
BranchTrueFalseTotal
if ((pZip->m_archive_size + num_alignment_padding_bytes + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + archive_name_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + archive_name_size + comment_size + user_extra_data_len + pState->m_central_dir.m_size + MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE + 1024 + MZ_ZIP_DATA_DESCRIPTER_SIZE32 + user_extra_data_central_len) > 0xFFFFFFFF) 0 0 0
6651 {
6652 0 pState->m_zip64 = MZ_TRUE;
6653 /*return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); */
6654 }
6655 }
6656  
6657 #ifndef MINIZ_NO_TIME
6658 0 ▶ if (pFile_time)
Branch: 1 branch
BranchTrueFalseTotal
if (pFile_time) 0 0 0
6659 {
6660 0 mz_zip_time_t_to_dos_time(*pFile_time, &dos_time, &dos_date);
6661 }
6662 #else
6663 (void)pFile_time;
6664 #endif
6665  
6666 0 ▶ if (max_size <= 3)
Branch: 1 branch
BranchTrueFalseTotal
if (max_size <= 3) 0 0 0
6667 0 level = 0;
6668  
6669 0 ▶ if (!mz_zip_writer_write_zeros(pZip, cur_archive_file_ofs, num_alignment_padding_bytes))
Branch: 1 branch
BranchTrueFalseTotal
if (!mz_zip_writer_write_zeros(pZip, cur_archive_file_ofs, num_alignment_padding_bytes)) 0 0 0
6670 {
6671 0 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
6672 }
6673  
6674 0 cur_archive_file_ofs += num_alignment_padding_bytes;
6675 0 local_dir_header_ofs = cur_archive_file_ofs;
6676  
6677 0 if (pZip->m_file_offset_alignment)
6678 {
6679 MZ_ASSERT((cur_archive_file_ofs & (pZip->m_file_offset_alignment - 1)) == 0);
6680 }
6681  
6682 0 ▶ if (max_size && level)
Branch: 2 branches
BranchTrueFalseTotal
if (max_size && level) 0 0 0
if (max_size && level) 0 0 0
6683 {
6684 0 method = MZ_DEFLATED;
6685 }
6686  
6687 0 MZ_CLEAR_ARR(local_dir_header);
6688 0 ▶ if (pState->m_zip64)
Branch: 1 branch
BranchTrueFalseTotal
if (pState->m_zip64) 0 0 0
6689 {
6690 0 ▶ if (max_size >= MZ_UINT32_MAX || local_dir_header_ofs >= MZ_UINT32_MAX)
Branch: 2 branches
BranchTrueFalseTotal
if (max_size >= MZ_UINT32_MAX || local_dir_header_ofs >= MZ_UINT32_MAX) 0 0 0
if (max_size >= MZ_UINT32_MAX || local_dir_header_ofs >= MZ_UINT32_MAX) 0 0 0
6691 {
6692 0 pExtra_data = extra_data;
6693 0 ▶ if (level_and_flags & MZ_ZIP_FLAG_WRITE_HEADER_SET_SIZE)
Branch: 1 branch
BranchTrueFalseTotal
if (level_and_flags & MZ_ZIP_FLAG_WRITE_HEADER_SET_SIZE) 0 0 0
6694 0 ▶ extra_size = mz_zip_writer_create_zip64_extra_data(extra_data, (max_size >= MZ_UINT32_MAX) ? &uncomp_size : NULL,
Branch: 2 branches
BranchTrueFalseTotal
extra_size = mz_zip_writer_create_zip64_extra_data(extra_data, (max_size >= MZ_UINT32_MAX) ? &uncomp_size : NULL, 0 0 0
extra_size = mz_zip_writer_create_zip64_extra_data(extra_data, (max_size >= MZ_UINT32_MAX) ? &uncomp_size : NULL, 0 0 0
6695 (max_size >= MZ_UINT32_MAX) ? &comp_size : NULL,
6696 0 ▶ (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL);
Branch: 1 branch
BranchTrueFalseTotal
(local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL); 0 0 0
6697 else
6698 0 extra_size = mz_zip_writer_create_zip64_extra_data(extra_data, NULL,
6699 NULL,
6700 0 ▶ (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL);
Branch: 1 branch
BranchTrueFalseTotal
(local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL); 0 0 0
6701 }
6702  
6703 0 ▶ if (!mz_zip_writer_create_local_dir_header(pZip, local_dir_header, (mz_uint16)archive_name_size, (mz_uint16)(extra_size + user_extra_data_len), 0, 0, 0, method, gen_flags, dos_time, dos_date))
Branch: 1 branch
BranchTrueFalseTotal
if (!mz_zip_writer_create_local_dir_header(pZip, local_dir_header, (mz_uint16)archive_name_size, (mz_uint16)(extra_size + user_extra_data_len), 0, 0, 0, method, gen_flags, dos_time, dos_date)) 0 0 0
6704 0 return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);
6705  
6706 0 ▶ if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, local_dir_header, sizeof(local_dir_header)) != sizeof(local_dir_header))
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, local_dir_header, sizeof(local_dir_header)) != sizeof(local_dir_header)) 0 0 0
6707 0 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
6708  
6709 0 cur_archive_file_ofs += sizeof(local_dir_header);
6710  
6711 0 ▶ if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pArchive_name, archive_name_size) != archive_name_size)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pArchive_name, archive_name_size) != archive_name_size) 0 0 0
6712 {
6713 0 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
6714 }
6715  
6716 0 cur_archive_file_ofs += archive_name_size;
6717  
6718 0 ▶ if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, extra_data, extra_size) != extra_size)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, extra_data, extra_size) != extra_size) 0 0 0
6719 0 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
6720  
6721 0 cur_archive_file_ofs += extra_size;
6722 }
6723 else
6724 {
6725 0 ▶ if ((comp_size > MZ_UINT32_MAX) || (cur_archive_file_ofs > MZ_UINT32_MAX))
Branch: 2 branches
BranchTrueFalseTotal
if ((comp_size > MZ_UINT32_MAX) || (cur_archive_file_ofs > MZ_UINT32_MAX)) 0 0 0
if ((comp_size > MZ_UINT32_MAX) || (cur_archive_file_ofs > MZ_UINT32_MAX)) 0 0 0
6726 0 return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);
6727 0 ▶ if (!mz_zip_writer_create_local_dir_header(pZip, local_dir_header, (mz_uint16)archive_name_size, (mz_uint16)user_extra_data_len, 0, 0, 0, method, gen_flags, dos_time, dos_date))
Branch: 1 branch
BranchTrueFalseTotal
if (!mz_zip_writer_create_local_dir_header(pZip, local_dir_header, (mz_uint16)archive_name_size, (mz_uint16)user_extra_data_len, 0, 0, 0, method, gen_flags, dos_time, dos_date)) 0 0 0
6728 0 return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);
6729  
6730 0 ▶ if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, local_dir_header, sizeof(local_dir_header)) != sizeof(local_dir_header))
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, local_dir_header, sizeof(local_dir_header)) != sizeof(local_dir_header)) 0 0 0
6731 0 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
6732  
6733 0 cur_archive_file_ofs += sizeof(local_dir_header);
6734  
6735 0 ▶ if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pArchive_name, archive_name_size) != archive_name_size)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pArchive_name, archive_name_size) != archive_name_size) 0 0 0
6736 {
6737 0 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
6738 }
6739  
6740 0 cur_archive_file_ofs += archive_name_size;
6741 }
6742  
6743 0 ▶ if (user_extra_data_len > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (user_extra_data_len > 0) 0 0 0
6744 {
6745 0 ▶ if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, user_extra_data, user_extra_data_len) != user_extra_data_len)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, user_extra_data, user_extra_data_len) != user_extra_data_len) 0 0 0
6746 0 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
6747  
6748 0 cur_archive_file_ofs += user_extra_data_len;
6749 }
6750  
6751 0 ▶ if (max_size)
Branch: 1 branch
BranchTrueFalseTotal
if (max_size) 0 0 0
6752 {
6753 0 void *pRead_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, MZ_ZIP_MAX_IO_BUF_SIZE);
6754 0 ▶ if (!pRead_buf)
Branch: 1 branch
BranchTrueFalseTotal
if (!pRead_buf) 0 0 0
6755 {
6756 0 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
6757 }
6758  
6759 0 ▶ if (!level)
Branch: 1 branch
BranchTrueFalseTotal
if (!level) 0 0 0
6760 {
6761 while (1)
6762 0 {
6763 0 size_t n = read_callback(callback_opaque, file_ofs, pRead_buf, MZ_ZIP_MAX_IO_BUF_SIZE);
6764 0 ▶ if (n == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) 0 0 0
6765 0 break;
6766  
6767 0 ▶ if ((n > MZ_ZIP_MAX_IO_BUF_SIZE) || (file_ofs + n > max_size))
Branch: 2 branches
BranchTrueFalseTotal
if ((n > MZ_ZIP_MAX_IO_BUF_SIZE) || (file_ofs + n > max_size)) 0 0 0
if ((n > MZ_ZIP_MAX_IO_BUF_SIZE) || (file_ofs + n > max_size)) 0 0 0
6768 {
6769 0 pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf);
6770 0 return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
6771 }
6772 0 ▶ if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pRead_buf, n) != n)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pRead_buf, n) != n) 0 0 0
6773 {
6774 0 pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf);
6775 0 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
6776 }
6777 0 file_ofs += n;
6778 0 uncomp_crc32 = (mz_uint32)mz_crc32(uncomp_crc32, (const mz_uint8 *)pRead_buf, n);
6779 0 cur_archive_file_ofs += n;
6780 }
6781 0 uncomp_size = file_ofs;
6782 0 comp_size = uncomp_size;
6783 }
6784 else
6785 {
6786 0 mz_bool result = MZ_FALSE;
6787 mz_zip_writer_add_state state;
6788 0 tdefl_compressor *pComp = (tdefl_compressor *)pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, sizeof(tdefl_compressor));
6789 0 ▶ if (!pComp)
Branch: 1 branch
BranchTrueFalseTotal
if (!pComp) 0 0 0
6790 {
6791 0 pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf);
6792 0 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
6793 }
6794  
6795 0 state.m_pZip = pZip;
6796 0 state.m_cur_archive_file_ofs = cur_archive_file_ofs;
6797 0 state.m_comp_size = 0;
6798  
6799 0 ▶ if (tdefl_init(pComp, mz_zip_writer_add_put_buf_callback, &state, tdefl_create_comp_flags_from_zip_params(level, -15, MZ_DEFAULT_STRATEGY)) != TDEFL_STATUS_OKAY)
Branch: 1 branch
BranchTrueFalseTotal
if (tdefl_init(pComp, mz_zip_writer_add_put_buf_callback, &state, tdefl_create_comp_flags_from_zip_params(level, -15, MZ_DEFAULT_STRATEGY)) != TDEFL_STATUS_OKAY) 0 0 0
6800 {
6801 0 pZip->m_pFree(pZip->m_pAlloc_opaque, pComp);
6802 0 pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf);
6803 0 return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);
6804 }
6805  
6806 for (;;)
6807 0 {
6808 tdefl_status status;
6809 0 tdefl_flush flush = TDEFL_NO_FLUSH;
6810  
6811 0 size_t n = read_callback(callback_opaque, file_ofs, pRead_buf, MZ_ZIP_MAX_IO_BUF_SIZE);
6812 0 ▶ if ((n > MZ_ZIP_MAX_IO_BUF_SIZE) || (file_ofs + n > max_size))
Branch: 2 branches
BranchTrueFalseTotal
if ((n > MZ_ZIP_MAX_IO_BUF_SIZE) || (file_ofs + n > max_size)) 0 0 0
if ((n > MZ_ZIP_MAX_IO_BUF_SIZE) || (file_ofs + n > max_size)) 0 0 0
6813 {
6814 mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
6815 0 break;
6816 }
6817  
6818 0 file_ofs += n;
6819 0 uncomp_crc32 = (mz_uint32)mz_crc32(uncomp_crc32, (const mz_uint8 *)pRead_buf, n);
6820  
6821 0 ▶ if (pZip->m_pNeeds_keepalive != NULL && pZip->m_pNeeds_keepalive(pZip->m_pIO_opaque))
Branch: 2 branches
BranchTrueFalseTotal
if (pZip->m_pNeeds_keepalive != NULL && pZip->m_pNeeds_keepalive(pZip->m_pIO_opaque)) 0 0 0
if (pZip->m_pNeeds_keepalive != NULL && pZip->m_pNeeds_keepalive(pZip->m_pIO_opaque)) 0 0 0
6822 0 flush = TDEFL_FULL_FLUSH;
6823  
6824 0 ▶ if (n == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) 0 0 0
6825 0 flush = TDEFL_FINISH;
6826  
6827 0 status = tdefl_compress_buffer(pComp, pRead_buf, n, flush);
6828 0 ▶ if (status == TDEFL_STATUS_DONE)
Branch: 1 branch
BranchTrueFalseTotal
if (status == TDEFL_STATUS_DONE) 0 0 0
6829 {
6830 0 result = MZ_TRUE;
6831 0 break;
6832 }
6833 0 ▶ else if (status != TDEFL_STATUS_OKAY)
Branch: 1 branch
BranchTrueFalseTotal
else if (status != TDEFL_STATUS_OKAY) 0 0 0
6834 {
6835 mz_zip_set_error(pZip, MZ_ZIP_COMPRESSION_FAILED);
6836 0 break;
6837 }
6838 }
6839  
6840 0 pZip->m_pFree(pZip->m_pAlloc_opaque, pComp);
6841  
6842 0 ▶ if (!result)
Branch: 1 branch
BranchTrueFalseTotal
if (!result) 0 0 0
6843 {
6844 0 pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf);
6845 0 return MZ_FALSE;
6846 }
6847  
6848 0 uncomp_size = file_ofs;
6849 0 comp_size = state.m_comp_size;
6850 0 cur_archive_file_ofs = state.m_cur_archive_file_ofs;
6851 }
6852  
6853 0 pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf);
6854 }
6855  
6856 0 ▶ if (!(level_and_flags & MZ_ZIP_FLAG_WRITE_HEADER_SET_SIZE))
Branch: 1 branch
BranchTrueFalseTotal
if (!(level_and_flags & MZ_ZIP_FLAG_WRITE_HEADER_SET_SIZE)) 0 0 0
6857 {
6858 mz_uint8 local_dir_footer[MZ_ZIP_DATA_DESCRIPTER_SIZE64];
6859 0 mz_uint32 local_dir_footer_size = MZ_ZIP_DATA_DESCRIPTER_SIZE32;
6860  
6861 MZ_WRITE_LE32(local_dir_footer + 0, MZ_ZIP_DATA_DESCRIPTOR_ID);
6862 0 MZ_WRITE_LE32(local_dir_footer + 4, uncomp_crc32);
6863 0 ▶ if (pExtra_data == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (pExtra_data == NULL) 0 0 0
6864 {
6865 0 ▶ if (comp_size > MZ_UINT32_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (comp_size > MZ_UINT32_MAX) 0 0 0
6866 0 return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);
6867  
6868 0 MZ_WRITE_LE32(local_dir_footer + 8, comp_size);
6869 0 MZ_WRITE_LE32(local_dir_footer + 12, uncomp_size);
6870 }
6871 else
6872 {
6873 0 MZ_WRITE_LE64(local_dir_footer + 8, comp_size);
6874 0 MZ_WRITE_LE64(local_dir_footer + 16, uncomp_size);
6875 0 local_dir_footer_size = MZ_ZIP_DATA_DESCRIPTER_SIZE64;
6876 }
6877  
6878 0 ▶ if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, local_dir_footer, local_dir_footer_size) != local_dir_footer_size)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, local_dir_footer, local_dir_footer_size) != local_dir_footer_size) 0 0 0
6879 0 return MZ_FALSE;
6880  
6881 0 cur_archive_file_ofs += local_dir_footer_size;
6882 }
6883  
6884 0 ▶ if (level_and_flags & MZ_ZIP_FLAG_WRITE_HEADER_SET_SIZE)
Branch: 1 branch
BranchTrueFalseTotal
if (level_and_flags & MZ_ZIP_FLAG_WRITE_HEADER_SET_SIZE) 0 0 0
6885 {
6886 0 ▶ if (pExtra_data != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (pExtra_data != NULL) 0 0 0
6887 {
6888 0 ▶ extra_size = mz_zip_writer_create_zip64_extra_data(extra_data, (max_size >= MZ_UINT32_MAX) ? &uncomp_size : NULL,
Branch: 2 branches
BranchTrueFalseTotal
extra_size = mz_zip_writer_create_zip64_extra_data(extra_data, (max_size >= MZ_UINT32_MAX) ? &uncomp_size : NULL, 0 0 0
extra_size = mz_zip_writer_create_zip64_extra_data(extra_data, (max_size >= MZ_UINT32_MAX) ? &uncomp_size : NULL, 0 0 0
6889 0 ▶ (max_size >= MZ_UINT32_MAX) ? &comp_size : NULL, (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL);
Branch: 1 branch
BranchTrueFalseTotal
(max_size >= MZ_UINT32_MAX) ? &comp_size : NULL, (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL); 0 0 0
6890 }
6891  
6892 0 ▶ if (!mz_zip_writer_create_local_dir_header(pZip, local_dir_header,
Branch: 3 branches
BranchTrueFalseTotal
if (!mz_zip_writer_create_local_dir_header(pZip, local_dir_header, 0 0 0
if (!mz_zip_writer_create_local_dir_header(pZip, local_dir_header, 0 0 0
if (!mz_zip_writer_create_local_dir_header(pZip, local_dir_header, 0 0 0
6893 0 (mz_uint16)archive_name_size, (mz_uint16)(extra_size + user_extra_data_len),
6894 (max_size >= MZ_UINT32_MAX) ? MZ_UINT32_MAX : uncomp_size,
6895 (max_size >= MZ_UINT32_MAX) ? MZ_UINT32_MAX : comp_size,
6896 uncomp_crc32, method, gen_flags, dos_time, dos_date))
6897 0 return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);
6898  
6899 0 cur_archive_header_file_ofs = local_dir_header_ofs;
6900  
6901 0 ▶ if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_header_file_ofs, local_dir_header, sizeof(local_dir_header)) != sizeof(local_dir_header))
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_header_file_ofs, local_dir_header, sizeof(local_dir_header)) != sizeof(local_dir_header)) 0 0 0
6902 0 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
6903  
6904 0 ▶ if (pExtra_data != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (pExtra_data != NULL) 0 0 0
6905 {
6906 0 cur_archive_header_file_ofs += sizeof(local_dir_header);
6907  
6908 0 ▶ if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_header_file_ofs, pArchive_name, archive_name_size) != archive_name_size)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_header_file_ofs, pArchive_name, archive_name_size) != archive_name_size) 0 0 0
6909 {
6910 0 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
6911 }
6912  
6913 0 cur_archive_header_file_ofs += archive_name_size;
6914  
6915 0 ▶ if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_header_file_ofs, extra_data, extra_size) != extra_size)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_header_file_ofs, extra_data, extra_size) != extra_size) 0 0 0
6916 0 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
6917  
6918 0 cur_archive_header_file_ofs += extra_size;
6919 }
6920 }
6921  
6922 0 ▶ if (pExtra_data != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (pExtra_data != NULL) 0 0 0
6923 {
6924 0 ▶ extra_size = mz_zip_writer_create_zip64_extra_data(extra_data, (uncomp_size >= MZ_UINT32_MAX) ? &uncomp_size : NULL,
Branch: 1 branch
BranchTrueFalseTotal
extra_size = mz_zip_writer_create_zip64_extra_data(extra_data, (uncomp_size >= MZ_UINT32_MAX) ? &uncomp_size : NULL, 0 0 0
6925 0 ▶ (uncomp_size >= MZ_UINT32_MAX) ? &comp_size : NULL, (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL);
Branch: 2 branches
BranchTrueFalseTotal
(uncomp_size >= MZ_UINT32_MAX) ? &comp_size : NULL, (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL); 0 0 0
(uncomp_size >= MZ_UINT32_MAX) ? &comp_size : NULL, (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL); 0 0 0
6926 }
6927  
6928 0 ▶ if (!mz_zip_writer_add_to_central_dir(pZip, pArchive_name, (mz_uint16)archive_name_size, pExtra_data, (mz_uint16)extra_size, pComment, comment_size,
Branch: 1 branch
BranchTrueFalseTotal
if (!mz_zip_writer_add_to_central_dir(pZip, pArchive_name, (mz_uint16)archive_name_size, pExtra_data, (mz_uint16)extra_size, pComment, comment_size, 0 0 0
6929 uncomp_size, comp_size, uncomp_crc32, method, gen_flags, dos_time, dos_date, local_dir_header_ofs, ext_attributes,
6930 user_extra_data_central, user_extra_data_central_len))
6931 0 return MZ_FALSE;
6932  
6933 0 pZip->m_total_files++;
6934 0 pZip->m_archive_size = cur_archive_file_ofs;
6935  
6936 0 return MZ_TRUE;
6937 }
6938  
6939 #ifndef MINIZ_NO_STDIO
6940  
6941 static size_t mz_file_read_func_stdio(void *pOpaque, mz_uint64 file_ofs, void *pBuf, size_t n)
6942 {
6943 MZ_FILE *pSrc_file = (MZ_FILE *)pOpaque;
6944 mz_int64 cur_ofs = MZ_FTELL64(pSrc_file);
6945  
6946 if (((mz_int64)file_ofs < 0) || (((cur_ofs != (mz_int64)file_ofs)) && (MZ_FSEEK64(pSrc_file, (mz_int64)file_ofs, SEEK_SET))))
6947 return 0;
6948  
6949 return MZ_FREAD(pBuf, 1, n, pSrc_file);
6950 }
6951  
6952 mz_bool mz_zip_writer_add_cfile(mz_zip_archive *pZip, const char *pArchive_name, MZ_FILE *pSrc_file, mz_uint64 max_size, const MZ_TIME_T *pFile_time, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags,
6953 const char *user_extra_data, mz_uint user_extra_data_len, const char *user_extra_data_central, mz_uint user_extra_data_central_len)
6954 {
6955 return mz_zip_writer_add_read_buf_callback(pZip, pArchive_name, mz_file_read_func_stdio, pSrc_file, max_size, pFile_time, pComment, comment_size, level_and_flags,
6956 user_extra_data, user_extra_data_len, user_extra_data_central, user_extra_data_central_len);
6957 }
6958  
6959 mz_bool mz_zip_writer_add_file(mz_zip_archive *pZip, const char *pArchive_name, const char *pSrc_filename, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags)
6960 {
6961 MZ_FILE *pSrc_file = NULL;
6962 mz_uint64 uncomp_size = 0;
6963 MZ_TIME_T file_modified_time;
6964 MZ_TIME_T *pFile_time = NULL;
6965 mz_bool status;
6966  
6967 memset(&file_modified_time, 0, sizeof(file_modified_time));
6968  
6969 #if !defined(MINIZ_NO_TIME) && !defined(MINIZ_NO_STDIO)
6970 pFile_time = &file_modified_time;
6971 if (!mz_zip_get_file_modified_time(pSrc_filename, &file_modified_time))
6972 return mz_zip_set_error(pZip, MZ_ZIP_FILE_STAT_FAILED);
6973 #endif
6974  
6975 pSrc_file = MZ_FOPEN(pSrc_filename, "rb");
6976 if (!pSrc_file)
6977 return mz_zip_set_error(pZip, MZ_ZIP_FILE_OPEN_FAILED);
6978  
6979 MZ_FSEEK64(pSrc_file, 0, SEEK_END);
6980 uncomp_size = MZ_FTELL64(pSrc_file);
6981 MZ_FSEEK64(pSrc_file, 0, SEEK_SET);
6982  
6983 status = mz_zip_writer_add_cfile(pZip, pArchive_name, pSrc_file, uncomp_size, pFile_time, pComment, comment_size, level_and_flags, NULL, 0, NULL, 0);
6984  
6985 MZ_FCLOSE(pSrc_file);
6986  
6987 return status;
6988 }
6989 #endif /* #ifndef MINIZ_NO_STDIO */
6990  
6991 0 static mz_bool mz_zip_writer_update_zip64_extension_block(mz_zip_array *pNew_ext, mz_zip_archive *pZip, const mz_uint8 *pExt, mz_uint32 ext_len, mz_uint64 *pComp_size, mz_uint64 *pUncomp_size, mz_uint64 *pLocal_header_ofs, mz_uint32 *pDisk_start)
6992 {
6993 /* + 64 should be enough for any new zip64 data */
6994 0 ▶ if (!mz_zip_array_reserve(pZip, pNew_ext, ext_len + 64, MZ_FALSE))
Branch: 2 branches
BranchTrueFalseTotal
if (!mz_zip_array_reserve(pZip, pNew_ext, ext_len + 64, MZ_FALSE)) 0 0 0
if (!mz_zip_array_reserve(pZip, pNew_ext, ext_len + 64, MZ_FALSE)) 0 0 0
6995 0 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
6996  
6997 mz_zip_array_resize(pZip, pNew_ext, 0, MZ_FALSE);
6998  
6999 0 ▶ if ((pUncomp_size) || (pComp_size) || (pLocal_header_ofs) || (pDisk_start))
Branch: 4 branches
BranchTrueFalseTotal
if ((pUncomp_size) || (pComp_size) || (pLocal_header_ofs) || (pDisk_start)) 0 0 0
if ((pUncomp_size) || (pComp_size) || (pLocal_header_ofs) || (pDisk_start)) 0 0 0
if ((pUncomp_size) || (pComp_size) || (pLocal_header_ofs) || (pDisk_start)) 0 0 0
if ((pUncomp_size) || (pComp_size) || (pLocal_header_ofs) || (pDisk_start)) 0 0 0
7000 {
7001 mz_uint8 new_ext_block[64];
7002 0 mz_uint8 *pDst = new_ext_block;
7003 mz_write_le16(pDst, MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID);
7004 0 mz_write_le16(pDst + sizeof(mz_uint16), 0);
7005 0 pDst += sizeof(mz_uint16) * 2;
7006  
7007 0 ▶ if (pUncomp_size)
Branch: 1 branch
BranchTrueFalseTotal
if (pUncomp_size) 0 0 0
7008 {
7009 0 mz_write_le64(pDst, *pUncomp_size);
7010 0 pDst += sizeof(mz_uint64);
7011 }
7012  
7013 0 ▶ if (pComp_size)
Branch: 1 branch
BranchTrueFalseTotal
if (pComp_size) 0 0 0
7014 {
7015 0 mz_write_le64(pDst, *pComp_size);
7016 0 pDst += sizeof(mz_uint64);
7017 }
7018  
7019 0 ▶ if (pLocal_header_ofs)
Branch: 1 branch
BranchTrueFalseTotal
if (pLocal_header_ofs) 0 0 0
7020 {
7021 0 mz_write_le64(pDst, *pLocal_header_ofs);
7022 0 pDst += sizeof(mz_uint64);
7023 }
7024  
7025 0 ▶ if (pDisk_start)
Branch: 1 branch
BranchTrueFalseTotal
if (pDisk_start) 0 0 0
7026 {
7027 0 mz_write_le32(pDst, *pDisk_start);
7028 0 pDst += sizeof(mz_uint32);
7029 }
7030  
7031 0 mz_write_le16(new_ext_block + sizeof(mz_uint16), (mz_uint16)((pDst - new_ext_block) - sizeof(mz_uint16) * 2));
7032  
7033 0 ▶ if (!mz_zip_array_push_back(pZip, pNew_ext, new_ext_block, pDst - new_ext_block))
Branch: 2 branches
BranchTrueFalseTotal
if (!mz_zip_array_push_back(pZip, pNew_ext, new_ext_block, pDst - new_ext_block)) 0 0 0
if (!mz_zip_array_push_back(pZip, pNew_ext, new_ext_block, pDst - new_ext_block)) 0 0 0
7034 0 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
7035 }
7036  
7037 0 ▶ if ((pExt) && (ext_len))
Branch: 2 branches
BranchTrueFalseTotal
if ((pExt) && (ext_len)) 0 0 0
if ((pExt) && (ext_len)) 0 0 0
7038 {
7039 0 mz_uint32 extra_size_remaining = ext_len;
7040 0 const mz_uint8 *pExtra_data = pExt;
7041  
7042 do
7043 {
7044 mz_uint32 field_id, field_data_size, field_total_size;
7045  
7046 0 ▶ if (extra_size_remaining < (sizeof(mz_uint16) * 2))
Branch: 1 branch
BranchTrueFalseTotal
if (extra_size_remaining < (sizeof(mz_uint16) * 2)) 0 0 0
7047 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
7048  
7049 0 field_id = MZ_READ_LE16(pExtra_data);
7050 0 field_data_size = MZ_READ_LE16(pExtra_data + sizeof(mz_uint16));
7051 0 field_total_size = field_data_size + sizeof(mz_uint16) * 2;
7052  
7053 0 ▶ if (field_total_size > extra_size_remaining)
Branch: 1 branch
BranchTrueFalseTotal
if (field_total_size > extra_size_remaining) 0 0 0
7054 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
7055  
7056 0 ▶ if (field_id != MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID)
Branch: 1 branch
BranchTrueFalseTotal
if (field_id != MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID) 0 0 0
7057 {
7058 0 ▶ if (!mz_zip_array_push_back(pZip, pNew_ext, pExtra_data, field_total_size))
Branch: 2 branches
BranchTrueFalseTotal
if (!mz_zip_array_push_back(pZip, pNew_ext, pExtra_data, field_total_size)) 0 0 0
if (!mz_zip_array_push_back(pZip, pNew_ext, pExtra_data, field_total_size)) 0 0 0
7059 0 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
7060 }
7061  
7062 0 pExtra_data += field_total_size;
7063 0 extra_size_remaining -= field_total_size;
7064 0 ▶ } while (extra_size_remaining);
Branch: 1 branch
BranchTrueFalseTotal
} while (extra_size_remaining); 0 0 0
7065 }
7066  
7067 0 return MZ_TRUE;
7068 }
7069  
7070 /* TODO: This func is now pretty freakin complex due to zip64, split it up? */
7071 0 mz_bool mz_zip_writer_add_from_zip_reader(mz_zip_archive *pZip, mz_zip_archive *pSource_zip, mz_uint src_file_index)
7072 {
7073 mz_uint n, bit_flags, num_alignment_padding_bytes, src_central_dir_following_data_size;
7074 mz_uint64 src_archive_bytes_remaining, local_dir_header_ofs;
7075 mz_uint64 cur_src_file_ofs, cur_dst_file_ofs;
7076 mz_uint32 local_header_u32[(MZ_ZIP_LOCAL_DIR_HEADER_SIZE + sizeof(mz_uint32) - 1) / sizeof(mz_uint32)];
7077 0 mz_uint8 *pLocal_header = (mz_uint8 *)local_header_u32;
7078 mz_uint8 new_central_header[MZ_ZIP_CENTRAL_DIR_HEADER_SIZE];
7079 size_t orig_central_dir_size;
7080 mz_zip_internal_state *pState;
7081 void *pBuf;
7082 const mz_uint8 *pSrc_central_header;
7083 mz_zip_archive_file_stat src_file_stat;
7084 mz_uint32 src_filename_len, src_comment_len, src_ext_len;
7085 mz_uint32 local_header_filename_size, local_header_extra_len;
7086 mz_uint64 local_header_comp_size, local_header_uncomp_size;
7087 0 mz_bool found_zip64_ext_data_in_ldir = MZ_FALSE;
7088  
7089 /* Sanity checks */
7090 0 ▶ if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || (!pSource_zip->m_pRead))
Branch: 4 branches
BranchTrueFalseTotal
if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || (!pSource_zip->m_pRead)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || (!pSource_zip->m_pRead)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || (!pSource_zip->m_pRead)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || (!pSource_zip->m_pRead)) 0 0 0
7091 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
7092  
7093 0 pState = pZip->m_pState;
7094  
7095 /* Don't support copying files from zip64 archives to non-zip64, even though in some cases this is possible */
7096 0 ▶ if ((pSource_zip->m_pState->m_zip64) && (!pZip->m_pState->m_zip64))
Branch: 2 branches
BranchTrueFalseTotal
if ((pSource_zip->m_pState->m_zip64) && (!pZip->m_pState->m_zip64)) 0 0 0
if ((pSource_zip->m_pState->m_zip64) && (!pZip->m_pState->m_zip64)) 0 0 0
7097 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
7098  
7099 /* Get pointer to the source central dir header and crack it */
7100 0 ▶ if (NULL == (pSrc_central_header = mz_zip_get_cdh(pSource_zip, src_file_index)))
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == (pSrc_central_header = mz_zip_get_cdh(pSource_zip, src_file_index))) 0 0 0
7101 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
7102  
7103 0 ▶ if (MZ_READ_LE32(pSrc_central_header + MZ_ZIP_CDH_SIG_OFS) != MZ_ZIP_CENTRAL_DIR_HEADER_SIG)
Branch: 1 branch
BranchTrueFalseTotal
if (MZ_READ_LE32(pSrc_central_header + MZ_ZIP_CDH_SIG_OFS) != MZ_ZIP_CENTRAL_DIR_HEADER_SIG) 0 0 0
7104 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
7105  
7106 0 src_filename_len = MZ_READ_LE16(pSrc_central_header + MZ_ZIP_CDH_FILENAME_LEN_OFS);
7107 0 src_comment_len = MZ_READ_LE16(pSrc_central_header + MZ_ZIP_CDH_COMMENT_LEN_OFS);
7108 0 src_ext_len = MZ_READ_LE16(pSrc_central_header + MZ_ZIP_CDH_EXTRA_LEN_OFS);
7109 0 src_central_dir_following_data_size = src_filename_len + src_ext_len + src_comment_len;
7110  
7111 /* TODO: We don't support central dir's >= MZ_UINT32_MAX bytes right now (+32 fudge factor in case we need to add more extra data) */
7112 0 ▶ if ((pState->m_central_dir.m_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + src_central_dir_following_data_size + 32) >= MZ_UINT32_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if ((pState->m_central_dir.m_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + src_central_dir_following_data_size + 32) >= MZ_UINT32_MAX) 0 0 0
7113 0 return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_CDIR_SIZE);
7114  
7115 0 num_alignment_padding_bytes = mz_zip_writer_compute_padding_needed_for_file_alignment(pZip);
7116  
7117 0 ▶ if (!pState->m_zip64)
Branch: 1 branch
BranchTrueFalseTotal
if (!pState->m_zip64) 0 0 0
7118 {
7119 0 ▶ if (pZip->m_total_files == MZ_UINT16_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_total_files == MZ_UINT16_MAX) 0 0 0
7120 0 return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);
7121 }
7122 else
7123 {
7124 /* TODO: Our zip64 support still has some 32-bit limits that may not be worth fixing. */
7125 0 ▶ if (pZip->m_total_files == MZ_UINT32_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_total_files == MZ_UINT32_MAX) 0 0 0
7126 0 return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);
7127 }
7128  
7129 0 ▶ if (!mz_zip_file_stat_internal(pSource_zip, src_file_index, pSrc_central_header, &src_file_stat, NULL))
Branch: 1 branch
BranchTrueFalseTotal
if (!mz_zip_file_stat_internal(pSource_zip, src_file_index, pSrc_central_header, &src_file_stat, NULL)) 0 0 0
7130 0 return MZ_FALSE;
7131  
7132 0 cur_src_file_ofs = src_file_stat.m_local_header_ofs;
7133 0 cur_dst_file_ofs = pZip->m_archive_size;
7134  
7135 /* Read the source archive's local dir header */
7136 0 ▶ if (pSource_zip->m_pRead(pSource_zip->m_pIO_opaque, cur_src_file_ofs, pLocal_header, MZ_ZIP_LOCAL_DIR_HEADER_SIZE) != MZ_ZIP_LOCAL_DIR_HEADER_SIZE)
Branch: 1 branch
BranchTrueFalseTotal
if (pSource_zip->m_pRead(pSource_zip->m_pIO_opaque, cur_src_file_ofs, pLocal_header, MZ_ZIP_LOCAL_DIR_HEADER_SIZE) != MZ_ZIP_LOCAL_DIR_HEADER_SIZE) 0 0 0
7137 0 return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
7138  
7139 0 ▶ if (MZ_READ_LE32(pLocal_header) != MZ_ZIP_LOCAL_DIR_HEADER_SIG)
Branch: 1 branch
BranchTrueFalseTotal
if (MZ_READ_LE32(pLocal_header) != MZ_ZIP_LOCAL_DIR_HEADER_SIG) 0 0 0
7140 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
7141  
7142 0 cur_src_file_ofs += MZ_ZIP_LOCAL_DIR_HEADER_SIZE;
7143  
7144 /* Compute the total size we need to copy (filename+extra data+compressed data) */
7145 0 local_header_filename_size = MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_FILENAME_LEN_OFS);
7146 0 local_header_extra_len = MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_EXTRA_LEN_OFS);
7147 0 local_header_comp_size = MZ_READ_LE32(pLocal_header + MZ_ZIP_LDH_COMPRESSED_SIZE_OFS);
7148 0 local_header_uncomp_size = MZ_READ_LE32(pLocal_header + MZ_ZIP_LDH_DECOMPRESSED_SIZE_OFS);
7149 0 src_archive_bytes_remaining = src_file_stat.m_comp_size + local_header_filename_size + local_header_extra_len;
7150  
7151 /* Try to find a zip64 extended information field */
7152 0 ▶ if ((local_header_extra_len) && ((local_header_comp_size == MZ_UINT32_MAX) || (local_header_uncomp_size == MZ_UINT32_MAX)))
Branch: 3 branches
BranchTrueFalseTotal
if ((local_header_extra_len) && ((local_header_comp_size == MZ_UINT32_MAX) || (local_header_uncomp_size == MZ_UINT32_MAX))) 0 0 0
if ((local_header_extra_len) && ((local_header_comp_size == MZ_UINT32_MAX) || (local_header_uncomp_size == MZ_UINT32_MAX))) 0 0 0
if ((local_header_extra_len) && ((local_header_comp_size == MZ_UINT32_MAX) || (local_header_uncomp_size == MZ_UINT32_MAX))) 0 0 0
7153 {
7154 mz_zip_array file_data_array;
7155 const mz_uint8 *pExtra_data;
7156 0 mz_uint32 extra_size_remaining = local_header_extra_len;
7157  
7158 mz_zip_array_init(&file_data_array, 1);
7159 0 ▶ if (!mz_zip_array_resize(pZip, &file_data_array, local_header_extra_len, MZ_FALSE))
Branch: 2 branches
BranchTrueFalseTotal
if (!mz_zip_array_resize(pZip, &file_data_array, local_header_extra_len, MZ_FALSE)) 0 0 0
if (!mz_zip_array_resize(pZip, &file_data_array, local_header_extra_len, MZ_FALSE)) 0 0 0
7160 {
7161 0 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
7162 }
7163  
7164 0 ▶ if (pSource_zip->m_pRead(pSource_zip->m_pIO_opaque, src_file_stat.m_local_header_ofs + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + local_header_filename_size, file_data_array.m_p, local_header_extra_len) != local_header_extra_len)
Branch: 1 branch
BranchTrueFalseTotal
if (pSource_zip->m_pRead(pSource_zip->m_pIO_opaque, src_file_stat.m_local_header_ofs + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + local_header_filename_size, file_data_array.m_p, local_header_extra_len) != local_header_extra_len) 0 0 0
7165 {
7166 mz_zip_array_clear(pZip, &file_data_array);
7167 0 return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
7168 }
7169  
7170 0 pExtra_data = (const mz_uint8 *)file_data_array.m_p;
7171  
7172 do
7173 {
7174 mz_uint32 field_id, field_data_size, field_total_size;
7175  
7176 0 ▶ if (extra_size_remaining < (sizeof(mz_uint16) * 2))
Branch: 1 branch
BranchTrueFalseTotal
if (extra_size_remaining < (sizeof(mz_uint16) * 2)) 0 0 0
7177 {
7178 mz_zip_array_clear(pZip, &file_data_array);
7179 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
7180 }
7181  
7182 0 field_id = MZ_READ_LE16(pExtra_data);
7183 0 field_data_size = MZ_READ_LE16(pExtra_data + sizeof(mz_uint16));
7184 0 field_total_size = field_data_size + sizeof(mz_uint16) * 2;
7185  
7186 0 ▶ if (field_total_size > extra_size_remaining)
Branch: 1 branch
BranchTrueFalseTotal
if (field_total_size > extra_size_remaining) 0 0 0
7187 {
7188 mz_zip_array_clear(pZip, &file_data_array);
7189 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
7190 }
7191  
7192 0 ▶ if (field_id == MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID)
Branch: 1 branch
BranchTrueFalseTotal
if (field_id == MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID) 0 0 0
7193 {
7194 0 const mz_uint8 *pSrc_field_data = pExtra_data + sizeof(mz_uint32);
7195  
7196 0 ▶ if (field_data_size < sizeof(mz_uint64) * 2)
Branch: 1 branch
BranchTrueFalseTotal
if (field_data_size < sizeof(mz_uint64) * 2) 0 0 0
7197 {
7198 mz_zip_array_clear(pZip, &file_data_array);
7199 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
7200 }
7201  
7202 0 local_header_uncomp_size = MZ_READ_LE64(pSrc_field_data);
7203 0 local_header_comp_size = MZ_READ_LE64(pSrc_field_data + sizeof(mz_uint64)); /* may be 0 if there's a descriptor */
7204  
7205 0 found_zip64_ext_data_in_ldir = MZ_TRUE;
7206 0 break;
7207 }
7208  
7209 0 pExtra_data += field_total_size;
7210 0 extra_size_remaining -= field_total_size;
7211 0 ▶ } while (extra_size_remaining);
Branch: 1 branch
BranchTrueFalseTotal
} while (extra_size_remaining); 0 0 0
7212  
7213 mz_zip_array_clear(pZip, &file_data_array);
7214 }
7215  
7216 0 ▶ if (!pState->m_zip64)
Branch: 1 branch
BranchTrueFalseTotal
if (!pState->m_zip64) 0 0 0
7217 {
7218 /* Try to detect if the new archive will most likely wind up too big and bail early (+(sizeof(mz_uint32) * 4) is for the optional descriptor which could be present, +64 is a fudge factor). */
7219 /* We also check when the archive is finalized so this doesn't need to be perfect. */
7220 0 mz_uint64 approx_new_archive_size = cur_dst_file_ofs + num_alignment_padding_bytes + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + src_archive_bytes_remaining + (sizeof(mz_uint32) * 4) +
7221 0 pState->m_central_dir.m_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + src_central_dir_following_data_size + MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE + 64;
7222  
7223 0 ▶ if (approx_new_archive_size >= MZ_UINT32_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (approx_new_archive_size >= MZ_UINT32_MAX) 0 0 0
7224 0 return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);
7225 }
7226  
7227 /* Write dest archive padding */
7228 0 ▶ if (!mz_zip_writer_write_zeros(pZip, cur_dst_file_ofs, num_alignment_padding_bytes))
Branch: 1 branch
BranchTrueFalseTotal
if (!mz_zip_writer_write_zeros(pZip, cur_dst_file_ofs, num_alignment_padding_bytes)) 0 0 0
7229 0 return MZ_FALSE;
7230  
7231 0 cur_dst_file_ofs += num_alignment_padding_bytes;
7232  
7233 0 local_dir_header_ofs = cur_dst_file_ofs;
7234 0 if (pZip->m_file_offset_alignment)
7235 {
7236 MZ_ASSERT((local_dir_header_ofs & (pZip->m_file_offset_alignment - 1)) == 0);
7237 }
7238  
7239 /* The original zip's local header+ext block doesn't change, even with zip64, so we can just copy it over to the dest zip */
7240 0 ▶ if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_dst_file_ofs, pLocal_header, MZ_ZIP_LOCAL_DIR_HEADER_SIZE) != MZ_ZIP_LOCAL_DIR_HEADER_SIZE)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_dst_file_ofs, pLocal_header, MZ_ZIP_LOCAL_DIR_HEADER_SIZE) != MZ_ZIP_LOCAL_DIR_HEADER_SIZE) 0 0 0
7241 0 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
7242  
7243 0 cur_dst_file_ofs += MZ_ZIP_LOCAL_DIR_HEADER_SIZE;
7244  
7245 /* Copy over the source archive bytes to the dest archive, also ensure we have enough buf space to handle optional data descriptor */
7246 0 ▶ if (NULL == (pBuf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, (size_t)MZ_MAX(32U, MZ_MIN((mz_uint64)MZ_ZIP_MAX_IO_BUF_SIZE, src_archive_bytes_remaining)))))
Branch: 2 branches
BranchTrueFalseTotal
if (NULL == (pBuf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, (size_t)MZ_MAX(32U, MZ_MIN((mz_uint64)MZ_ZIP_MAX_IO_BUF_SIZE, src_archive_bytes_remaining))))) 0 0 0
if (NULL == (pBuf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, (size_t)MZ_MAX(32U, MZ_MIN((mz_uint64)MZ_ZIP_MAX_IO_BUF_SIZE, src_archive_bytes_remaining))))) 0 0 0
7247 0 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
7248  
7249 0 ▶ while (src_archive_bytes_remaining)
Branch: 1 branch
BranchTrueFalseTotal
while (src_archive_bytes_remaining) 0 0 0
7250 {
7251 0 ▶ n = (mz_uint)MZ_MIN((mz_uint64)MZ_ZIP_MAX_IO_BUF_SIZE, src_archive_bytes_remaining);
Branch: 1 branch
BranchTrueFalseTotal
n = (mz_uint)MZ_MIN((mz_uint64)MZ_ZIP_MAX_IO_BUF_SIZE, src_archive_bytes_remaining); 0 0 0
7252 0 ▶ if (pSource_zip->m_pRead(pSource_zip->m_pIO_opaque, cur_src_file_ofs, pBuf, n) != n)
Branch: 1 branch
BranchTrueFalseTotal
if (pSource_zip->m_pRead(pSource_zip->m_pIO_opaque, cur_src_file_ofs, pBuf, n) != n) 0 0 0
7253 {
7254 0 pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf);
7255 0 return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
7256 }
7257 0 cur_src_file_ofs += n;
7258  
7259 0 ▶ if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_dst_file_ofs, pBuf, n) != n)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_dst_file_ofs, pBuf, n) != n) 0 0 0
7260 {
7261 0 pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf);
7262 0 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
7263 }
7264 0 cur_dst_file_ofs += n;
7265  
7266 0 src_archive_bytes_remaining -= n;
7267 }
7268  
7269 /* Now deal with the optional data descriptor */
7270 0 bit_flags = MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_BIT_FLAG_OFS);
7271 0 ▶ if (bit_flags & 8)
Branch: 1 branch
BranchTrueFalseTotal
if (bit_flags & 8) 0 0 0
7272 {
7273 /* Copy data descriptor */
7274 0 ▶ if ((pSource_zip->m_pState->m_zip64) || (found_zip64_ext_data_in_ldir))
Branch: 2 branches
BranchTrueFalseTotal
if ((pSource_zip->m_pState->m_zip64) || (found_zip64_ext_data_in_ldir)) 0 0 0
if ((pSource_zip->m_pState->m_zip64) || (found_zip64_ext_data_in_ldir)) 0 0 0
7275 {
7276 /* src is zip64, dest must be zip64 */
7277  
7278 /* name uint32_t's */
7279 /* id 1 (optional in zip64?) */
7280 /* crc 1 */
7281 /* comp_size 2 */
7282 /* uncomp_size 2 */
7283 0 ▶ if (pSource_zip->m_pRead(pSource_zip->m_pIO_opaque, cur_src_file_ofs, pBuf, (sizeof(mz_uint32) * 6)) != (sizeof(mz_uint32) * 6))
Branch: 1 branch
BranchTrueFalseTotal
if (pSource_zip->m_pRead(pSource_zip->m_pIO_opaque, cur_src_file_ofs, pBuf, (sizeof(mz_uint32) * 6)) != (sizeof(mz_uint32) * 6)) 0 0 0
7284 {
7285 0 pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf);
7286 0 return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
7287 }
7288  
7289 0 ▶ n = sizeof(mz_uint32) * ((MZ_READ_LE32(pBuf) == MZ_ZIP_DATA_DESCRIPTOR_ID) ? 6 : 5);
Branch: 1 branch
BranchTrueFalseTotal
n = sizeof(mz_uint32) * ((MZ_READ_LE32(pBuf) == MZ_ZIP_DATA_DESCRIPTOR_ID) ? 6 : 5); 0 0 0
7290 }
7291 else
7292 {
7293 /* src is NOT zip64 */
7294 mz_bool has_id;
7295  
7296 0 ▶ if (pSource_zip->m_pRead(pSource_zip->m_pIO_opaque, cur_src_file_ofs, pBuf, sizeof(mz_uint32) * 4) != sizeof(mz_uint32) * 4)
Branch: 1 branch
BranchTrueFalseTotal
if (pSource_zip->m_pRead(pSource_zip->m_pIO_opaque, cur_src_file_ofs, pBuf, sizeof(mz_uint32) * 4) != sizeof(mz_uint32) * 4) 0 0 0
7297 {
7298 0 pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf);
7299 0 return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
7300 }
7301  
7302 0 has_id = (MZ_READ_LE32(pBuf) == MZ_ZIP_DATA_DESCRIPTOR_ID);
7303  
7304 0 ▶ if (pZip->m_pState->m_zip64)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pState->m_zip64) 0 0 0
7305 {
7306 /* dest is zip64, so upgrade the data descriptor */
7307 0 ▶ const mz_uint8 *pSrc_descriptor = (const mz_uint8 *)pBuf + (has_id ? sizeof(mz_uint32) : 0);
Branch: 1 branch
BranchTrueFalseTotal
const mz_uint8 *pSrc_descriptor = (const mz_uint8 *)pBuf + (has_id ? sizeof(mz_uint32) : 0); 0 0 0
7308 0 const mz_uint32 src_crc32 = MZ_READ_LE32(pSrc_descriptor);
7309 0 const mz_uint64 src_comp_size = MZ_READ_LE32(pSrc_descriptor + sizeof(mz_uint32));
7310 0 const mz_uint64 src_uncomp_size = MZ_READ_LE32(pSrc_descriptor + 2 * sizeof(mz_uint32));
7311  
7312 mz_write_le32((mz_uint8 *)pBuf, MZ_ZIP_DATA_DESCRIPTOR_ID);
7313 0 mz_write_le32((mz_uint8 *)pBuf + sizeof(mz_uint32) * 1, src_crc32);
7314 0 mz_write_le64((mz_uint8 *)pBuf + sizeof(mz_uint32) * 2, src_comp_size);
7315 0 mz_write_le64((mz_uint8 *)pBuf + sizeof(mz_uint32) * 4, src_uncomp_size);
7316  
7317 0 n = sizeof(mz_uint32) * 6;
7318 }
7319 else
7320 {
7321 /* dest is NOT zip64, just copy it as-is */
7322 0 ▶ n = sizeof(mz_uint32) * (has_id ? 4 : 3);
Branch: 1 branch
BranchTrueFalseTotal
n = sizeof(mz_uint32) * (has_id ? 4 : 3); 0 0 0
7323 }
7324 }
7325  
7326 0 ▶ if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_dst_file_ofs, pBuf, n) != n)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_dst_file_ofs, pBuf, n) != n) 0 0 0
7327 {
7328 0 pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf);
7329 0 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
7330 }
7331  
7332 0 cur_src_file_ofs += n;
7333 0 cur_dst_file_ofs += n;
7334 }
7335 0 pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf);
7336  
7337 /* Finally, add the new central dir header */
7338 0 orig_central_dir_size = pState->m_central_dir.m_size;
7339  
7340 0 memcpy(new_central_header, pSrc_central_header, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE);
7341  
7342 0 ▶ if (pState->m_zip64)
Branch: 1 branch
BranchTrueFalseTotal
if (pState->m_zip64) 0 0 0
7343 {
7344 /* This is the painful part: We need to write a new central dir header + ext block with updated zip64 fields, and ensure the old fields (if any) are not included. */
7345 0 const mz_uint8 *pSrc_ext = pSrc_central_header + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + src_filename_len;
7346 mz_zip_array new_ext_block;
7347  
7348 mz_zip_array_init(&new_ext_block, sizeof(mz_uint8));
7349  
7350 0 MZ_WRITE_LE32(new_central_header + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS, MZ_UINT32_MAX);
7351 0 MZ_WRITE_LE32(new_central_header + MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS, MZ_UINT32_MAX);
7352 0 MZ_WRITE_LE32(new_central_header + MZ_ZIP_CDH_LOCAL_HEADER_OFS, MZ_UINT32_MAX);
7353  
7354 0 ▶ if (!mz_zip_writer_update_zip64_extension_block(&new_ext_block, pZip, pSrc_ext, src_ext_len, &src_file_stat.m_comp_size, &src_file_stat.m_uncomp_size, &local_dir_header_ofs, NULL))
Branch: 1 branch
BranchTrueFalseTotal
if (!mz_zip_writer_update_zip64_extension_block(&new_ext_block, pZip, pSrc_ext, src_ext_len, &src_file_stat.m_comp_size, &src_file_stat.m_uncomp_size, &local_dir_header_ofs, NULL)) 0 0 0
7355 {
7356 mz_zip_array_clear(pZip, &new_ext_block);
7357 0 return MZ_FALSE;
7358 }
7359  
7360 0 MZ_WRITE_LE16(new_central_header + MZ_ZIP_CDH_EXTRA_LEN_OFS, new_ext_block.m_size);
7361  
7362 0 ▶ if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, new_central_header, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE))
Branch: 2 branches
BranchTrueFalseTotal
if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, new_central_header, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE)) 0 0 0
if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, new_central_header, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE)) 0 0 0
7363 {
7364 mz_zip_array_clear(pZip, &new_ext_block);
7365 0 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
7366 }
7367  
7368 0 ▶ if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, pSrc_central_header + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE, src_filename_len))
Branch: 2 branches
BranchTrueFalseTotal
if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, pSrc_central_header + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE, src_filename_len)) 0 0 0
if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, pSrc_central_header + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE, src_filename_len)) 0 0 0
7369 {
7370 mz_zip_array_clear(pZip, &new_ext_block);
7371 0 ▶ mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE);
Branch: 1 branch
BranchTrueFalseTotal
mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE); 0 0 0
7372 0 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
7373 }
7374  
7375 0 ▶ if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, new_ext_block.m_p, new_ext_block.m_size))
Branch: 2 branches
BranchTrueFalseTotal
if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, new_ext_block.m_p, new_ext_block.m_size)) 0 0 0
if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, new_ext_block.m_p, new_ext_block.m_size)) 0 0 0
7376 {
7377 mz_zip_array_clear(pZip, &new_ext_block);
7378 0 ▶ mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE);
Branch: 1 branch
BranchTrueFalseTotal
mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE); 0 0 0
7379 0 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
7380 }
7381  
7382 0 ▶ if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, pSrc_central_header + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + src_filename_len + src_ext_len, src_comment_len))
Branch: 2 branches
BranchTrueFalseTotal
if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, pSrc_central_header + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + src_filename_len + src_ext_len, src_comment_len)) 0 0 0
if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, pSrc_central_header + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + src_filename_len + src_ext_len, src_comment_len)) 0 0 0
7383 {
7384 mz_zip_array_clear(pZip, &new_ext_block);
7385 0 ▶ mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE);
Branch: 1 branch
BranchTrueFalseTotal
mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE); 0 0 0
7386 0 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
7387 }
7388  
7389 mz_zip_array_clear(pZip, &new_ext_block);
7390 }
7391 else
7392 {
7393 /* sanity checks */
7394 0 ▶ if (cur_dst_file_ofs > MZ_UINT32_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (cur_dst_file_ofs > MZ_UINT32_MAX) 0 0 0
7395 0 return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);
7396  
7397 0 ▶ if (local_dir_header_ofs >= MZ_UINT32_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (local_dir_header_ofs >= MZ_UINT32_MAX) 0 0 0
7398 0 return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);
7399  
7400 0 MZ_WRITE_LE32(new_central_header + MZ_ZIP_CDH_LOCAL_HEADER_OFS, local_dir_header_ofs);
7401  
7402 0 ▶ if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, new_central_header, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE))
Branch: 2 branches
BranchTrueFalseTotal
if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, new_central_header, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE)) 0 0 0
if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, new_central_header, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE)) 0 0 0
7403 0 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
7404  
7405 0 ▶ if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, pSrc_central_header + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE, src_central_dir_following_data_size))
Branch: 2 branches
BranchTrueFalseTotal
if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, pSrc_central_header + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE, src_central_dir_following_data_size)) 0 0 0
if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, pSrc_central_header + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE, src_central_dir_following_data_size)) 0 0 0
7406 {
7407 0 ▶ mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE);
Branch: 1 branch
BranchTrueFalseTotal
mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE); 0 0 0
7408 0 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
7409 }
7410 }
7411  
7412 /* This shouldn't trigger unless we screwed up during the initial sanity checks */
7413 0 ▶ if (pState->m_central_dir.m_size >= MZ_UINT32_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (pState->m_central_dir.m_size >= MZ_UINT32_MAX) 0 0 0
7414 {
7415 /* TODO: Support central dirs >= 32-bits in size */
7416 0 ▶ mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE);
Branch: 1 branch
BranchTrueFalseTotal
mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE); 0 0 0
7417 0 return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_CDIR_SIZE);
7418 }
7419  
7420 0 n = (mz_uint32)orig_central_dir_size;
7421 0 ▶ if (!mz_zip_array_push_back(pZip, &pState->m_central_dir_offsets, &n, 1))
Branch: 2 branches
BranchTrueFalseTotal
if (!mz_zip_array_push_back(pZip, &pState->m_central_dir_offsets, &n, 1)) 0 0 0
if (!mz_zip_array_push_back(pZip, &pState->m_central_dir_offsets, &n, 1)) 0 0 0
7422 {
7423 0 ▶ mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE);
Branch: 1 branch
BranchTrueFalseTotal
mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE); 0 0 0
7424 0 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
7425 }
7426  
7427 0 pZip->m_total_files++;
7428 0 pZip->m_archive_size = cur_dst_file_ofs;
7429  
7430 0 return MZ_TRUE;
7431 }
7432  
7433 0 mz_bool mz_zip_writer_finalize_archive(mz_zip_archive *pZip)
7434 {
7435 mz_zip_internal_state *pState;
7436 mz_uint64 central_dir_ofs, central_dir_size;
7437 mz_uint8 hdr[256];
7438  
7439 0 ▶ if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING))
Branch: 3 branches
BranchTrueFalseTotal
if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING)) 0 0 0
7440 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
7441  
7442 0 pState = pZip->m_pState;
7443  
7444 0 ▶ if (pState->m_zip64)
Branch: 1 branch
BranchTrueFalseTotal
if (pState->m_zip64) 0 0 0
7445 {
7446 0 ▶ if ((mz_uint64)pState->m_central_dir.m_size >= MZ_UINT32_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if ((mz_uint64)pState->m_central_dir.m_size >= MZ_UINT32_MAX) 0 0 0
7447 0 return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);
7448 }
7449 else
7450 {
7451 0 ▶ if ((pZip->m_total_files > MZ_UINT16_MAX) || ((pZip->m_archive_size + pState->m_central_dir.m_size + MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) > MZ_UINT32_MAX))
Branch: 2 branches
BranchTrueFalseTotal
if ((pZip->m_total_files > MZ_UINT16_MAX) || ((pZip->m_archive_size + pState->m_central_dir.m_size + MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) > MZ_UINT32_MAX)) 0 0 0
if ((pZip->m_total_files > MZ_UINT16_MAX) || ((pZip->m_archive_size + pState->m_central_dir.m_size + MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) > MZ_UINT32_MAX)) 0 0 0
7452 0 return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);
7453 }
7454  
7455 0 central_dir_ofs = 0;
7456 0 central_dir_size = 0;
7457 0 ▶ if (pZip->m_total_files)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_total_files) 0 0 0
7458 {
7459 /* Write central directory */
7460 0 central_dir_ofs = pZip->m_archive_size;
7461 0 central_dir_size = pState->m_central_dir.m_size;
7462 0 pZip->m_central_directory_file_ofs = central_dir_ofs;
7463 0 ▶ if (pZip->m_pWrite(pZip->m_pIO_opaque, central_dir_ofs, pState->m_central_dir.m_p, (size_t)central_dir_size) != central_dir_size)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pWrite(pZip->m_pIO_opaque, central_dir_ofs, pState->m_central_dir.m_p, (size_t)central_dir_size) != central_dir_size) 0 0 0
7464 0 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
7465  
7466 0 pZip->m_archive_size += central_dir_size;
7467 }
7468  
7469 0 ▶ if (pState->m_zip64)
Branch: 1 branch
BranchTrueFalseTotal
if (pState->m_zip64) 0 0 0
7470 {
7471 /* Write zip64 end of central directory header */
7472 0 mz_uint64 rel_ofs_to_zip64_ecdr = pZip->m_archive_size;
7473  
7474 0 MZ_CLEAR_ARR(hdr);
7475 MZ_WRITE_LE32(hdr + MZ_ZIP64_ECDH_SIG_OFS, MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIG);
7476 0 MZ_WRITE_LE64(hdr + MZ_ZIP64_ECDH_SIZE_OF_RECORD_OFS, MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE - sizeof(mz_uint32) - sizeof(mz_uint64));
7477 0 MZ_WRITE_LE16(hdr + MZ_ZIP64_ECDH_VERSION_MADE_BY_OFS, 0x031E); /* TODO: always Unix */
7478 0 MZ_WRITE_LE16(hdr + MZ_ZIP64_ECDH_VERSION_NEEDED_OFS, 0x002D);
7479 0 MZ_WRITE_LE64(hdr + MZ_ZIP64_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS, pZip->m_total_files);
7480 0 MZ_WRITE_LE64(hdr + MZ_ZIP64_ECDH_CDIR_TOTAL_ENTRIES_OFS, pZip->m_total_files);
7481 0 MZ_WRITE_LE64(hdr + MZ_ZIP64_ECDH_CDIR_SIZE_OFS, central_dir_size);
7482 0 MZ_WRITE_LE64(hdr + MZ_ZIP64_ECDH_CDIR_OFS_OFS, central_dir_ofs);
7483 0 ▶ if (pZip->m_pWrite(pZip->m_pIO_opaque, pZip->m_archive_size, hdr, MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE) != MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pWrite(pZip->m_pIO_opaque, pZip->m_archive_size, hdr, MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE) != MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE) 0 0 0
7484 0 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
7485  
7486 0 pZip->m_archive_size += MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE;
7487  
7488 /* Write zip64 end of central directory locator */
7489 0 MZ_CLEAR_ARR(hdr);
7490 MZ_WRITE_LE32(hdr + MZ_ZIP64_ECDL_SIG_OFS, MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIG);
7491 0 MZ_WRITE_LE64(hdr + MZ_ZIP64_ECDL_REL_OFS_TO_ZIP64_ECDR_OFS, rel_ofs_to_zip64_ecdr);
7492 0 MZ_WRITE_LE32(hdr + MZ_ZIP64_ECDL_TOTAL_NUMBER_OF_DISKS_OFS, 1);
7493 0 ▶ if (pZip->m_pWrite(pZip->m_pIO_opaque, pZip->m_archive_size, hdr, MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE) != MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pWrite(pZip->m_pIO_opaque, pZip->m_archive_size, hdr, MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE) != MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE) 0 0 0
7494 0 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
7495  
7496 0 pZip->m_archive_size += MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE;
7497 }
7498  
7499 /* Write end of central directory record */
7500 0 MZ_CLEAR_ARR(hdr);
7501 MZ_WRITE_LE32(hdr + MZ_ZIP_ECDH_SIG_OFS, MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIG);
7502 0 ▶ MZ_WRITE_LE16(hdr + MZ_ZIP_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS, MZ_MIN(MZ_UINT16_MAX, pZip->m_total_files));
Branch: 1 branch
BranchTrueFalseTotal
MZ_WRITE_LE16(hdr + MZ_ZIP_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS, MZ_MIN(MZ_UINT16_MAX, pZip->m_total_files)); 0 0 0
7503 0 ▶ MZ_WRITE_LE16(hdr + MZ_ZIP_ECDH_CDIR_TOTAL_ENTRIES_OFS, MZ_MIN(MZ_UINT16_MAX, pZip->m_total_files));
Branch: 1 branch
BranchTrueFalseTotal
MZ_WRITE_LE16(hdr + MZ_ZIP_ECDH_CDIR_TOTAL_ENTRIES_OFS, MZ_MIN(MZ_UINT16_MAX, pZip->m_total_files)); 0 0 0
7504 0 ▶ MZ_WRITE_LE32(hdr + MZ_ZIP_ECDH_CDIR_SIZE_OFS, MZ_MIN(MZ_UINT32_MAX, central_dir_size));
Branch: 1 branch
BranchTrueFalseTotal
MZ_WRITE_LE32(hdr + MZ_ZIP_ECDH_CDIR_SIZE_OFS, MZ_MIN(MZ_UINT32_MAX, central_dir_size)); 0 0 0
7505 0 ▶ MZ_WRITE_LE32(hdr + MZ_ZIP_ECDH_CDIR_OFS_OFS, MZ_MIN(MZ_UINT32_MAX, central_dir_ofs));
Branch: 1 branch
BranchTrueFalseTotal
MZ_WRITE_LE32(hdr + MZ_ZIP_ECDH_CDIR_OFS_OFS, MZ_MIN(MZ_UINT32_MAX, central_dir_ofs)); 0 0 0
7506  
7507 0 ▶ if (pZip->m_pWrite(pZip->m_pIO_opaque, pZip->m_archive_size, hdr, MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) != MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pWrite(pZip->m_pIO_opaque, pZip->m_archive_size, hdr, MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) != MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) 0 0 0
7508 0 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
7509  
7510 #ifndef MINIZ_NO_STDIO
7511 if ((pState->m_pFile) && (MZ_FFLUSH(pState->m_pFile) == EOF))
7512 return mz_zip_set_error(pZip, MZ_ZIP_FILE_CLOSE_FAILED);
7513 #endif /* #ifndef MINIZ_NO_STDIO */
7514  
7515 0 pZip->m_archive_size += MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE;
7516  
7517 0 pZip->m_zip_mode = MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED;
7518 0 return MZ_TRUE;
7519 }
7520  
7521 0 mz_bool mz_zip_writer_finalize_heap_archive(mz_zip_archive *pZip, void **ppBuf, size_t *pSize)
7522 {
7523 0 ▶ if ((!ppBuf) || (!pSize))
Branch: 2 branches
BranchTrueFalseTotal
if ((!ppBuf) || (!pSize)) 0 0 0
if ((!ppBuf) || (!pSize)) 0 0 0
7524 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
7525  
7526 0 *ppBuf = NULL;
7527 0 *pSize = 0;
7528  
7529 0 ▶ if ((!pZip) || (!pZip->m_pState))
Branch: 2 branches
BranchTrueFalseTotal
if ((!pZip) || (!pZip->m_pState)) 0 0 0
if ((!pZip) || (!pZip->m_pState)) 0 0 0
7530 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
7531  
7532 0 ▶ if (pZip->m_pWrite != mz_zip_heap_write_func)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_pWrite != mz_zip_heap_write_func) 0 0 0
7533 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
7534  
7535 0 ▶ if (!mz_zip_writer_finalize_archive(pZip))
Branch: 1 branch
BranchTrueFalseTotal
if (!mz_zip_writer_finalize_archive(pZip)) 0 0 0
7536 0 return MZ_FALSE;
7537  
7538 0 *ppBuf = pZip->m_pState->m_pMem;
7539 0 *pSize = pZip->m_pState->m_mem_size;
7540 0 pZip->m_pState->m_pMem = NULL;
7541 0 pZip->m_pState->m_mem_size = pZip->m_pState->m_mem_capacity = 0;
7542  
7543 0 return MZ_TRUE;
7544 }
7545  
7546 0 mz_bool mz_zip_writer_end(mz_zip_archive *pZip)
7547 {
7548 0 return mz_zip_writer_end_internal(pZip, MZ_TRUE);
7549 }
7550  
7551 #ifndef MINIZ_NO_STDIO
7552 mz_bool mz_zip_add_mem_to_archive_file_in_place(const char *pZip_filename, const char *pArchive_name, const void *pBuf, size_t buf_size, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags)
7553 {
7554 return mz_zip_add_mem_to_archive_file_in_place_v2(pZip_filename, pArchive_name, pBuf, buf_size, pComment, comment_size, level_and_flags, NULL);
7555 }
7556  
7557 mz_bool mz_zip_add_mem_to_archive_file_in_place_v2(const char *pZip_filename, const char *pArchive_name, const void *pBuf, size_t buf_size, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags, mz_zip_error *pErr)
7558 {
7559 mz_bool status, created_new_archive = MZ_FALSE;
7560 mz_zip_archive zip_archive;
7561 struct MZ_FILE_STAT_STRUCT file_stat;
7562 mz_zip_error actual_err = MZ_ZIP_NO_ERROR;
7563  
7564 mz_zip_zero_struct(&zip_archive);
7565 if ((int)level_and_flags < 0)
7566 level_and_flags = MZ_DEFAULT_LEVEL;
7567  
7568 if ((!pZip_filename) || (!pArchive_name) || ((buf_size) && (!pBuf)) || ((comment_size) && (!pComment)) || ((level_and_flags & 0xF) > MZ_UBER_COMPRESSION))
7569 {
7570 if (pErr)
7571 *pErr = MZ_ZIP_INVALID_PARAMETER;
7572 return MZ_FALSE;
7573 }
7574  
7575 if (!mz_zip_writer_validate_archive_name(pArchive_name))
7576 {
7577 if (pErr)
7578 *pErr = MZ_ZIP_INVALID_FILENAME;
7579 return MZ_FALSE;
7580 }
7581  
7582 /* Important: The regular non-64 bit version of stat() can fail here if the file is very large, which could cause the archive to be overwritten. */
7583 /* So be sure to compile with _LARGEFILE64_SOURCE 1 */
7584 if (MZ_FILE_STAT(pZip_filename, &file_stat) != 0)
7585 {
7586 /* Create a new archive. */
7587 if (!mz_zip_writer_init_file_v2(&zip_archive, pZip_filename, 0, level_and_flags))
7588 {
7589 if (pErr)
7590 *pErr = zip_archive.m_last_error;
7591 return MZ_FALSE;
7592 }
7593  
7594 created_new_archive = MZ_TRUE;
7595 }
7596 else
7597 {
7598 /* Append to an existing archive. */
7599 if (!mz_zip_reader_init_file_v2(&zip_archive, pZip_filename, level_and_flags | MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY | MZ_ZIP_FLAG_READ_ALLOW_WRITING, 0, 0))
7600 {
7601 if (pErr)
7602 *pErr = zip_archive.m_last_error;
7603 return MZ_FALSE;
7604 }
7605  
7606 if (!mz_zip_writer_init_from_reader_v2(&zip_archive, pZip_filename, level_and_flags | MZ_ZIP_FLAG_READ_ALLOW_WRITING))
7607 {
7608 if (pErr)
7609 *pErr = zip_archive.m_last_error;
7610  
7611 mz_zip_reader_end_internal(&zip_archive, MZ_FALSE);
7612  
7613 return MZ_FALSE;
7614 }
7615 }
7616  
7617 status = mz_zip_writer_add_mem_ex(&zip_archive, pArchive_name, pBuf, buf_size, pComment, comment_size, level_and_flags, 0, 0);
7618 actual_err = zip_archive.m_last_error;
7619  
7620 /* Always finalize, even if adding failed for some reason, so we have a valid central directory. (This may not always succeed, but we can try.) */
7621 if (!mz_zip_writer_finalize_archive(&zip_archive))
7622 {
7623 if (!actual_err)
7624 actual_err = zip_archive.m_last_error;
7625  
7626 status = MZ_FALSE;
7627 }
7628  
7629 if (!mz_zip_writer_end_internal(&zip_archive, status))
7630 {
7631 if (!actual_err)
7632 actual_err = zip_archive.m_last_error;
7633  
7634 status = MZ_FALSE;
7635 }
7636  
7637 if ((!status) && (created_new_archive))
7638 {
7639 /* It's a new archive and something went wrong, so just delete it. */
7640 int ignoredStatus = MZ_DELETE_FILE(pZip_filename);
7641 (void)ignoredStatus;
7642 }
7643  
7644 if (pErr)
7645 *pErr = actual_err;
7646  
7647 return status;
7648 }
7649  
7650 void *mz_zip_extract_archive_file_to_heap_v2(const char *pZip_filename, const char *pArchive_name, const char *pComment, size_t *pSize, mz_uint flags, mz_zip_error *pErr)
7651 {
7652 mz_uint32 file_index;
7653 mz_zip_archive zip_archive;
7654 void *p = NULL;
7655  
7656 if (pSize)
7657 *pSize = 0;
7658  
7659 if ((!pZip_filename) || (!pArchive_name))
7660 {
7661 if (pErr)
7662 *pErr = MZ_ZIP_INVALID_PARAMETER;
7663  
7664 return NULL;
7665 }
7666  
7667 mz_zip_zero_struct(&zip_archive);
7668 if (!mz_zip_reader_init_file_v2(&zip_archive, pZip_filename, flags | MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY, 0, 0))
7669 {
7670 if (pErr)
7671 *pErr = zip_archive.m_last_error;
7672  
7673 return NULL;
7674 }
7675  
7676 if (mz_zip_reader_locate_file_v2(&zip_archive, pArchive_name, pComment, flags, &file_index))
7677 {
7678 p = mz_zip_reader_extract_to_heap(&zip_archive, file_index, pSize, flags);
7679 }
7680  
7681 mz_zip_reader_end_internal(&zip_archive, p != NULL);
7682  
7683 if (pErr)
7684 *pErr = zip_archive.m_last_error;
7685  
7686 return p;
7687 }
7688  
7689 void *mz_zip_extract_archive_file_to_heap(const char *pZip_filename, const char *pArchive_name, size_t *pSize, mz_uint flags)
7690 {
7691 return mz_zip_extract_archive_file_to_heap_v2(pZip_filename, pArchive_name, NULL, pSize, flags, NULL);
7692 }
7693  
7694 #endif /* #ifndef MINIZ_NO_STDIO */
7695  
7696 #endif /* #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS */
7697  
7698 /* ------------------- Misc utils */
7699  
7700 0 mz_zip_mode mz_zip_get_mode(mz_zip_archive *pZip)
7701 {
7702 0 ▶ return pZip ? pZip->m_zip_mode : MZ_ZIP_MODE_INVALID;
Branch: 1 branch
BranchTrueFalseTotal
return pZip ? pZip->m_zip_mode : MZ_ZIP_MODE_INVALID; 0 0 0
7703 }
7704  
7705 0 mz_zip_type mz_zip_get_type(mz_zip_archive *pZip)
7706 {
7707 0 ▶ return pZip ? pZip->m_zip_type : MZ_ZIP_TYPE_INVALID;
Branch: 1 branch
BranchTrueFalseTotal
return pZip ? pZip->m_zip_type : MZ_ZIP_TYPE_INVALID; 0 0 0
7708 }
7709  
7710 0 mz_zip_error mz_zip_set_last_error(mz_zip_archive *pZip, mz_zip_error err_num)
7711 {
7712 mz_zip_error prev_err;
7713  
7714 0 ▶ if (!pZip)
Branch: 1 branch
BranchTrueFalseTotal
if (!pZip) 0 0 0
7715 0 return MZ_ZIP_INVALID_PARAMETER;
7716  
7717 0 prev_err = pZip->m_last_error;
7718  
7719 0 pZip->m_last_error = err_num;
7720 0 return prev_err;
7721 }
7722  
7723 0 mz_zip_error mz_zip_peek_last_error(mz_zip_archive *pZip)
7724 {
7725 0 ▶ if (!pZip)
Branch: 1 branch
BranchTrueFalseTotal
if (!pZip) 0 0 0
7726 0 return MZ_ZIP_INVALID_PARAMETER;
7727  
7728 0 return pZip->m_last_error;
7729 }
7730  
7731 0 mz_zip_error mz_zip_clear_last_error(mz_zip_archive *pZip)
7732 {
7733 0 return mz_zip_set_last_error(pZip, MZ_ZIP_NO_ERROR);
7734 }
7735  
7736 0 mz_zip_error mz_zip_get_last_error(mz_zip_archive *pZip)
7737 {
7738 mz_zip_error prev_err;
7739  
7740 0 ▶ if (!pZip)
Branch: 1 branch
BranchTrueFalseTotal
if (!pZip) 0 0 0
7741 0 return MZ_ZIP_INVALID_PARAMETER;
7742  
7743 0 prev_err = pZip->m_last_error;
7744  
7745 0 pZip->m_last_error = MZ_ZIP_NO_ERROR;
7746 0 return prev_err;
7747 }
7748  
7749 0 const char *mz_zip_get_error_string(mz_zip_error mz_err)
7750 {
7751 0 switch (mz_err)
7752 {
7753 0 case MZ_ZIP_NO_ERROR:
7754 0 return "no error";
7755 0 case MZ_ZIP_UNDEFINED_ERROR:
7756 0 return "undefined error";
7757 0 case MZ_ZIP_TOO_MANY_FILES:
7758 0 return "too many files";
7759 0 case MZ_ZIP_FILE_TOO_LARGE:
7760 0 return "file too large";
7761 0 case MZ_ZIP_UNSUPPORTED_METHOD:
7762 0 return "unsupported method";
7763 0 case MZ_ZIP_UNSUPPORTED_ENCRYPTION:
7764 0 return "unsupported encryption";
7765 0 case MZ_ZIP_UNSUPPORTED_FEATURE:
7766 0 return "unsupported feature";
7767 0 case MZ_ZIP_FAILED_FINDING_CENTRAL_DIR:
7768 0 return "failed finding central directory";
7769 0 case MZ_ZIP_NOT_AN_ARCHIVE:
7770 0 return "not a ZIP archive";
7771 0 case MZ_ZIP_INVALID_HEADER_OR_CORRUPTED:
7772 0 return "invalid header or archive is corrupted";
7773 0 case MZ_ZIP_UNSUPPORTED_MULTIDISK:
7774 0 return "unsupported multidisk archive";
7775 0 case MZ_ZIP_DECOMPRESSION_FAILED:
7776 0 return "decompression failed or archive is corrupted";
7777 0 case MZ_ZIP_COMPRESSION_FAILED:
7778 0 return "compression failed";
7779 0 case MZ_ZIP_UNEXPECTED_DECOMPRESSED_SIZE:
7780 0 return "unexpected decompressed size";
7781 0 case MZ_ZIP_CRC_CHECK_FAILED:
7782 0 return "CRC-32 check failed";
7783 0 case MZ_ZIP_UNSUPPORTED_CDIR_SIZE:
7784 0 return "unsupported central directory size";
7785 0 case MZ_ZIP_ALLOC_FAILED:
7786 0 return "allocation failed";
7787 0 case MZ_ZIP_FILE_OPEN_FAILED:
7788 0 return "file open failed";
7789 0 case MZ_ZIP_FILE_CREATE_FAILED:
7790 0 return "file create failed";
7791 0 case MZ_ZIP_FILE_WRITE_FAILED:
7792 0 return "file write failed";
7793 0 case MZ_ZIP_FILE_READ_FAILED:
7794 0 return "file read failed";
7795 0 case MZ_ZIP_FILE_CLOSE_FAILED:
7796 0 return "file close failed";
7797 0 case MZ_ZIP_FILE_SEEK_FAILED:
7798 0 return "file seek failed";
7799 0 case MZ_ZIP_FILE_STAT_FAILED:
7800 0 return "file stat failed";
7801 0 case MZ_ZIP_INVALID_PARAMETER:
7802 0 return "invalid parameter";
7803 0 case MZ_ZIP_INVALID_FILENAME:
7804 0 return "invalid filename";
7805 0 case MZ_ZIP_BUF_TOO_SMALL:
7806 0 return "buffer too small";
7807 0 case MZ_ZIP_INTERNAL_ERROR:
7808 0 return "internal error";
7809 0 case MZ_ZIP_FILE_NOT_FOUND:
7810 0 return "file not found";
7811 0 case MZ_ZIP_ARCHIVE_TOO_LARGE:
7812 0 return "archive is too large";
7813 0 case MZ_ZIP_VALIDATION_FAILED:
7814 0 return "validation failed";
7815 0 case MZ_ZIP_WRITE_CALLBACK_FAILED:
7816 0 return "write callback failed";
7817 0 case MZ_ZIP_TOTAL_ERRORS:
7818 0 return "total errors";
7819 0 default:
7820 0 break;
7821 }
7822  
7823 0 return "unknown error";
7824 }
7825  
7826 /* Note: Just because the archive is not zip64 doesn't necessarily mean it doesn't have Zip64 extended information extra field, argh. */
7827 0 mz_bool mz_zip_is_zip64(mz_zip_archive *pZip)
7828 {
7829 0 ▶ if ((!pZip) || (!pZip->m_pState))
Branch: 2 branches
BranchTrueFalseTotal
if ((!pZip) || (!pZip->m_pState)) 0 0 0
if ((!pZip) || (!pZip->m_pState)) 0 0 0
7830 0 return MZ_FALSE;
7831  
7832 0 return pZip->m_pState->m_zip64;
7833 }
7834  
7835 0 size_t mz_zip_get_central_dir_size(mz_zip_archive *pZip)
7836 {
7837 0 ▶ if ((!pZip) || (!pZip->m_pState))
Branch: 2 branches
BranchTrueFalseTotal
if ((!pZip) || (!pZip->m_pState)) 0 0 0
if ((!pZip) || (!pZip->m_pState)) 0 0 0
7838 0 return 0;
7839  
7840 0 return pZip->m_pState->m_central_dir.m_size;
7841 }
7842  
7843 0 mz_uint mz_zip_reader_get_num_files(mz_zip_archive *pZip)
7844 {
7845 0 ▶ return pZip ? pZip->m_total_files : 0;
Branch: 1 branch
BranchTrueFalseTotal
return pZip ? pZip->m_total_files : 0; 0 0 0
7846 }
7847  
7848 0 mz_uint64 mz_zip_get_archive_size(mz_zip_archive *pZip)
7849 {
7850 0 ▶ if (!pZip)
Branch: 1 branch
BranchTrueFalseTotal
if (!pZip) 0 0 0
7851 0 return 0;
7852 0 return pZip->m_archive_size;
7853 }
7854  
7855 0 mz_uint64 mz_zip_get_archive_file_start_offset(mz_zip_archive *pZip)
7856 {
7857 0 ▶ if ((!pZip) || (!pZip->m_pState))
Branch: 2 branches
BranchTrueFalseTotal
if ((!pZip) || (!pZip->m_pState)) 0 0 0
if ((!pZip) || (!pZip->m_pState)) 0 0 0
7858 0 return 0;
7859 0 return pZip->m_pState->m_file_archive_start_ofs;
7860 }
7861  
7862 0 MZ_FILE *mz_zip_get_cfile(mz_zip_archive *pZip)
7863 {
7864 0 ▶ if ((!pZip) || (!pZip->m_pState))
Branch: 2 branches
BranchTrueFalseTotal
if ((!pZip) || (!pZip->m_pState)) 0 0 0
if ((!pZip) || (!pZip->m_pState)) 0 0 0
7865 0 return 0;
7866 0 return pZip->m_pState->m_pFile;
7867 }
7868  
7869 0 size_t mz_zip_read_archive_data(mz_zip_archive *pZip, mz_uint64 file_ofs, void *pBuf, size_t n)
7870 {
7871 0 ▶ if ((!pZip) || (!pZip->m_pState) || (!pBuf) || (!pZip->m_pRead))
Branch: 4 branches
BranchTrueFalseTotal
if ((!pZip) || (!pZip->m_pState) || (!pBuf) || (!pZip->m_pRead)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (!pBuf) || (!pZip->m_pRead)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (!pBuf) || (!pZip->m_pRead)) 0 0 0
if ((!pZip) || (!pZip->m_pState) || (!pBuf) || (!pZip->m_pRead)) 0 0 0
7872 0 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
7873  
7874 0 return pZip->m_pRead(pZip->m_pIO_opaque, file_ofs, pBuf, n);
7875 }
7876  
7877 0 ▶ mz_uint mz_zip_reader_get_filename(mz_zip_archive *pZip, mz_uint file_index, char *pFilename, mz_uint filename_buf_size)
Branch: 1 branch
BranchTrueFalseTotal
mz_uint mz_zip_reader_get_filename(mz_zip_archive *pZip, mz_uint file_index, char *pFilename, mz_uint filename_buf_size) 0 0 0
7878 {
7879 mz_uint n;
7880 0 const mz_uint8 *p = mz_zip_get_cdh(pZip, file_index);
7881 0 ▶ if (!p)
Branch: 1 branch
BranchTrueFalseTotal
if (!p) 0 0 0
7882 {
7883 0 ▶ if (filename_buf_size)
Branch: 1 branch
BranchTrueFalseTotal
if (filename_buf_size) 0 0 0
7884 0 pFilename[0] = '\0';
7885 mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
7886 0 return 0;
7887 }
7888 0 n = MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS);
7889 0 ▶ if (filename_buf_size)
Branch: 1 branch
BranchTrueFalseTotal
if (filename_buf_size) 0 0 0
7890 {
7891 0 n = MZ_MIN(n, filename_buf_size - 1);
7892 0 memcpy(pFilename, p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE, n);
7893 0 pFilename[n] = '\0';
7894 }
7895 0 return n + 1;
7896 }
7897  
7898 0 ▶ mz_bool mz_zip_reader_file_stat(mz_zip_archive *pZip, mz_uint file_index, mz_zip_archive_file_stat *pStat)
Branch: 1 branch
BranchTrueFalseTotal
mz_bool mz_zip_reader_file_stat(mz_zip_archive *pZip, mz_uint file_index, mz_zip_archive_file_stat *pStat) 0 0 0
7899 {
7900 0 return mz_zip_file_stat_internal(pZip, file_index, mz_zip_get_cdh(pZip, file_index), pStat, NULL);
7901 }
7902  
7903 0 mz_bool mz_zip_end(mz_zip_archive *pZip)
7904 {
7905 0 ▶ if (!pZip)
Branch: 1 branch
BranchTrueFalseTotal
if (!pZip) 0 0 0
7906 0 return MZ_FALSE;
7907  
7908 0 ▶ if (pZip->m_zip_mode == MZ_ZIP_MODE_READING)
Branch: 1 branch
BranchTrueFalseTotal
if (pZip->m_zip_mode == MZ_ZIP_MODE_READING) 0 0 0
7909 0 return mz_zip_reader_end(pZip);
7910 #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS
7911 0 ▶ else if ((pZip->m_zip_mode == MZ_ZIP_MODE_WRITING) || (pZip->m_zip_mode == MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED))
Branch: 2 branches
BranchTrueFalseTotal
else if ((pZip->m_zip_mode == MZ_ZIP_MODE_WRITING) || (pZip->m_zip_mode == MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED)) 0 0 0
else if ((pZip->m_zip_mode == MZ_ZIP_MODE_WRITING) || (pZip->m_zip_mode == MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED)) 0 0 0
7912 0 return mz_zip_writer_end(pZip);
7913 #endif
7914  
7915 0 return MZ_FALSE;
7916 }
7917  
7918 #ifdef __cplusplus
7919 }
7920 #endif
7921  
7922 #endif /*#ifndef MINIZ_NO_ARCHIVE_APIS*/
« blib/lib/Sereal/Performance.pm ↑ Summary snappy/csnappy_decompress.c »