Oolite
Loading...
Searching...
No Matches
miniz.c
Go to the documentation of this file.
1#define MINGW_HAS_SECURE_API 1
2#include "miniz.h"
3/**************************************************************************
4 *
5 * Copyright 2013-2014 RAD Game Tools and Valve Software
6 * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
7 * All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
26 *
27 **************************************************************************/
28
29typedef unsigned char mz_validate_uint16[sizeof(mz_uint16) == 2 ? 1 : -1];
30typedef unsigned char mz_validate_uint32[sizeof(mz_uint32) == 4 ? 1 : -1];
31typedef unsigned char mz_validate_uint64[sizeof(mz_uint64) == 8 ? 1 : -1];
32
33#ifdef __cplusplus
34extern "C" {
35#endif
36
37/* ------------------- zlib-style API's */
38
39mz_ulong mz_adler32(mz_ulong adler, const unsigned char* ptr, size_t buf_len)
40{
41 mz_uint32 i, s1 = (mz_uint32)(adler & 0xffff), s2 = (mz_uint32)(adler >> 16);
42 size_t block_len = buf_len % 5552;
43 if (!ptr)
44 return MZ_ADLER32_INIT;
45 while (buf_len) {
46 for (i = 0; i + 7 < block_len; i += 8, ptr += 8) {
47 s1 += ptr[0], s2 += s1;
48 s1 += ptr[1], s2 += s1;
49 s1 += ptr[2], s2 += s1;
50 s1 += ptr[3], s2 += s1;
51 s1 += ptr[4], s2 += s1;
52 s1 += ptr[5], s2 += s1;
53 s1 += ptr[6], s2 += s1;
54 s1 += ptr[7], s2 += s1;
55 }
56 for (; i < block_len; ++i)
57 s1 += *ptr++, s2 += s1;
58 s1 %= 65521U, s2 %= 65521U;
59 buf_len -= block_len;
60 block_len = 5552;
61 }
62 return (s2 << 16) + s1;
63}
64
65/* 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/ */
66#if 0
67 mz_ulong mz_crc32(mz_ulong crc, const mz_uint8 *ptr, size_t buf_len)
68 {
69 static const mz_uint32 s_crc32[16] = { 0, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
70 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c };
71 mz_uint32 crcu32 = (mz_uint32)crc;
72 if (!ptr)
73 return MZ_CRC32_INIT;
74 crcu32 = ~crcu32;
75 while (buf_len--)
76 {
77 mz_uint8 b = *ptr++;
78 crcu32 = (crcu32 >> 4) ^ s_crc32[(crcu32 & 0xF) ^ (b & 0xF)];
79 crcu32 = (crcu32 >> 4) ^ s_crc32[(crcu32 & 0xF) ^ (b >> 4)];
80 }
81 return ~crcu32;
82 }
83#elif defined(USE_EXTERNAL_MZCRC)
84/* If USE_EXTERNAL_CRC is defined, an external module will export the
85 * mz_crc32() symbol for us to use, e.g. an SSE-accelerated version.
86 * Depending on the impl, it may be necessary to ~ the input/output crc values.
87 */
88mz_ulong mz_crc32(mz_ulong crc, const mz_uint8* ptr, size_t buf_len);
89#else
90/* Faster, but larger CPU cache footprint.
91 */
92mz_ulong mz_crc32(mz_ulong crc, const mz_uint8* ptr, size_t buf_len)
93{
94 static const mz_uint32 s_crc_table[256] = {
95 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, 0xE963A535,
96 0x9E6495A3, 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD,
97 0xE7B82D07, 0x90BF1D91, 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE, 0x1ADAD47D,
98 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7, 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC,
99 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5, 0x3B6E20C8, 0x4C69105E, 0xD56041E4,
100 0xA2677172, 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, 0x35B5A8FA, 0x42B2986C,
101 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59, 0x26D930AC,
102 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F,
103 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, 0x2F6F7C87, 0x58684C11, 0xC1611DAB,
104 0xB6662D3D, 0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F,
105 0x9FBFE4A5, 0xE8B8D433, 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB,
106 0x086D3D2D, 0x91646C97, 0xE6635C01, 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E,
107 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, 0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA,
108 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65, 0x4DB26158, 0x3AB551CE,
109 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB, 0x4369E96A,
110 0x346ED9FC, 0xAD678846, 0xDA60B8D0, 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9,
111 0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409,
112 0xCE61E49F, 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81,
113 0xB7BD5C3B, 0xC0BA6CAD, 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, 0xEAD54739,
114 0x9DD277AF, 0x04DB2615, 0x73DC1683, 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8,
115 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1, 0xF00F9344, 0x8708A3D2, 0x1E01F268,
116 0x6906C2FE, 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7, 0xFED41B76, 0x89D32BE0,
117 0x10DA7A5A, 0x67DD4ACC, 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5, 0xD6D6A3E8,
118 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B,
119 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55, 0x316E8EEF,
120 0x4669BE79, 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, 0xCC0C7795, 0xBB0B4703,
121 0x220216B9, 0x5505262F, 0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7,
122 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D, 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A,
123 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713, 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE,
124 0x0CB61B38, 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, 0x86D3D2D4, 0xF1D4E242,
125 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777, 0x88085AE6,
126 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45,
127 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2, 0xA7672661, 0xD06016F7, 0x4969474D,
128 0x3E6E77DB, 0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5,
129 0x47B2CF7F, 0x30B5FFE9, 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605,
130 0xCDD70693, 0x54DE5729, 0x23D967BF, 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94,
131 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D
132 };
133
134 mz_uint32 crc32 = (mz_uint32)crc ^ 0xFFFFFFFF;
135 const mz_uint8* pByte_buf = (const mz_uint8*)ptr;
136
137 while (buf_len >= 4) {
138 crc32 = (crc32 >> 8) ^ s_crc_table[(crc32 ^ pByte_buf[0]) & 0xFF];
139 crc32 = (crc32 >> 8) ^ s_crc_table[(crc32 ^ pByte_buf[1]) & 0xFF];
140 crc32 = (crc32 >> 8) ^ s_crc_table[(crc32 ^ pByte_buf[2]) & 0xFF];
141 crc32 = (crc32 >> 8) ^ s_crc_table[(crc32 ^ pByte_buf[3]) & 0xFF];
142 pByte_buf += 4;
143 buf_len -= 4;
144 }
145
146 while (buf_len) {
147 crc32 = (crc32 >> 8) ^ s_crc_table[(crc32 ^ pByte_buf[0]) & 0xFF];
148 ++pByte_buf;
149 --buf_len;
150 }
151
152 return ~crc32;
153}
154#endif
155
156void mz_free(void* p)
157{
158 MZ_FREE(p);
159}
160
161MINIZ_EXPORT void* miniz_def_alloc_func(void* opaque, size_t items, size_t size)
162{
163 (void)opaque, (void)items, (void)size;
164 return MZ_MALLOC(items * size);
165}
166MINIZ_EXPORT void miniz_def_free_func(void* opaque, void* address)
167{
168 (void)opaque, (void)address;
169 MZ_FREE(address);
170}
171MINIZ_EXPORT void* miniz_def_realloc_func(void* opaque, void* address, size_t items, size_t size)
172{
173 (void)opaque, (void)address, (void)items, (void)size;
174 return MZ_REALLOC(address, items * size);
175}
176
177const char* mz_version(void)
178{
179 return MZ_VERSION;
180}
181
182#ifndef MINIZ_NO_ZLIB_APIS
183
184#ifndef MINIZ_NO_DEFLATE_APIS
185
186int mz_deflateInit(mz_streamp pStream, int level)
187{
189}
190
191int mz_deflateInit2(mz_streamp pStream, int level, int method, int window_bits, int mem_level, int strategy)
192{
193 tdefl_compressor* pComp;
194 mz_uint comp_flags = TDEFL_COMPUTE_ADLER32 | tdefl_create_comp_flags_from_zip_params(level, window_bits, strategy);
195
196 if (!pStream)
197 return MZ_STREAM_ERROR;
198 if ((method != MZ_DEFLATED) || ((mem_level < 1) || (mem_level > 9)) || ((window_bits != MZ_DEFAULT_WINDOW_BITS) && (-window_bits != MZ_DEFAULT_WINDOW_BITS)))
199 return MZ_PARAM_ERROR;
200
201 pStream->data_type = 0;
202 pStream->adler = MZ_ADLER32_INIT;
203 pStream->msg = NULL;
204 pStream->reserved = 0;
205 pStream->total_in = 0;
206 pStream->total_out = 0;
207 if (!pStream->zalloc)
208 pStream->zalloc = miniz_def_alloc_func;
209 if (!pStream->zfree)
210 pStream->zfree = miniz_def_free_func;
211
212 pComp = (tdefl_compressor*)pStream->zalloc(pStream->opaque, 1, sizeof(tdefl_compressor));
213 if (!pComp)
214 return MZ_MEM_ERROR;
215
216 pStream->state = (struct mz_internal_state*)pComp;
217
218 if (tdefl_init(pComp, NULL, NULL, comp_flags) != TDEFL_STATUS_OKAY) {
219 mz_deflateEnd(pStream);
220 return MZ_PARAM_ERROR;
221 }
222
223 return MZ_OK;
224}
225
227{
228 if ((!pStream) || (!pStream->state) || (!pStream->zalloc) || (!pStream->zfree))
229 return MZ_STREAM_ERROR;
230 pStream->total_in = pStream->total_out = 0;
231 tdefl_init((tdefl_compressor*)pStream->state, NULL, NULL, ((tdefl_compressor*)pStream->state)->m_flags);
232 return MZ_OK;
233}
234
235int mz_deflate(mz_streamp pStream, int flush)
236{
237 size_t in_bytes, out_bytes;
238 mz_ulong orig_total_in, orig_total_out;
239 int mz_status = MZ_OK;
240
241 if ((!pStream) || (!pStream->state) || (flush < 0) || (flush > MZ_FINISH) || (!pStream->next_out))
242 return MZ_STREAM_ERROR;
243 if (!pStream->avail_out)
244 return MZ_BUF_ERROR;
245
246 if (flush == MZ_PARTIAL_FLUSH)
247 flush = MZ_SYNC_FLUSH;
248
249 if (((tdefl_compressor*)pStream->state)->m_prev_return_status == TDEFL_STATUS_DONE)
250 return (flush == MZ_FINISH) ? MZ_STREAM_END : MZ_BUF_ERROR;
251
252 orig_total_in = pStream->total_in;
253 orig_total_out = pStream->total_out;
254 for (;;) {
255 tdefl_status defl_status;
256 in_bytes = pStream->avail_in;
257 out_bytes = pStream->avail_out;
258
259 defl_status = tdefl_compress((tdefl_compressor*)pStream->state, pStream->next_in, &in_bytes, pStream->next_out, &out_bytes, (tdefl_flush)flush);
260 pStream->next_in += (mz_uint)in_bytes;
261 pStream->avail_in -= (mz_uint)in_bytes;
262 pStream->total_in += (mz_uint)in_bytes;
263 pStream->adler = tdefl_get_adler32((tdefl_compressor*)pStream->state);
264
265 pStream->next_out += (mz_uint)out_bytes;
266 pStream->avail_out -= (mz_uint)out_bytes;
267 pStream->total_out += (mz_uint)out_bytes;
268
269 if (defl_status < 0) {
270 mz_status = MZ_STREAM_ERROR;
271 break;
272 } else if (defl_status == TDEFL_STATUS_DONE) {
273 mz_status = MZ_STREAM_END;
274 break;
275 } else if (!pStream->avail_out)
276 break;
277 else if ((!pStream->avail_in) && (flush != MZ_FINISH)) {
278 if ((flush) || (pStream->total_in != orig_total_in) || (pStream->total_out != orig_total_out))
279 break;
280 return MZ_BUF_ERROR; /* Can't make forward progress without some input.
281 */
282 }
283 }
284 return mz_status;
285}
286
288{
289 if (!pStream)
290 return MZ_STREAM_ERROR;
291 if (pStream->state) {
292 pStream->zfree(pStream->opaque, pStream->state);
293 pStream->state = NULL;
294 }
295 return MZ_OK;
296}
297
299{
300 (void)pStream;
301 /* 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.) */
302 return MZ_MAX(128 + (source_len * 110) / 100, 128 + source_len + ((source_len / (31 * 1024)) + 1) * 5);
303}
304
305int mz_compress2(unsigned char* pDest, mz_ulong* pDest_len, const unsigned char* pSource, mz_ulong source_len, int level)
306{
307 int status;
309 memset(&stream, 0, sizeof(stream));
310
311 /* In case mz_ulong is 64-bits (argh I hate longs). */
312 if ((mz_uint64)(source_len | *pDest_len) > 0xFFFFFFFFU)
313 return MZ_PARAM_ERROR;
314
315 stream.next_in = pSource;
316 stream.avail_in = (mz_uint32)source_len;
317 stream.next_out = pDest;
318 stream.avail_out = (mz_uint32)*pDest_len;
319
320 status = mz_deflateInit(&stream, level);
321 if (status != MZ_OK)
322 return status;
323
324 status = mz_deflate(&stream, MZ_FINISH);
325 if (status != MZ_STREAM_END) {
327 return (status == MZ_OK) ? MZ_BUF_ERROR : status;
328 }
329
330 *pDest_len = stream.total_out;
331 return mz_deflateEnd(&stream);
332}
333
334int mz_compress(unsigned char* pDest, mz_ulong* pDest_len, const unsigned char* pSource, mz_ulong source_len)
335{
336 return mz_compress2(pDest, pDest_len, pSource, source_len, MZ_DEFAULT_COMPRESSION);
337}
338
340{
341 return mz_deflateBound(NULL, source_len);
342}
343
344#endif /*#ifndef MINIZ_NO_DEFLATE_APIS*/
345
346#ifndef MINIZ_NO_INFLATE_APIS
347
348typedef struct
349{
350 tinfl_decompressor m_decomp;
353 mz_uint8 m_dict[TINFL_LZ_DICT_SIZE];
354 tinfl_status m_last_status;
356
357int mz_inflateInit2(mz_streamp pStream, int window_bits)
358{
359 inflate_state* pDecomp;
360 if (!pStream)
361 return MZ_STREAM_ERROR;
362 if ((window_bits != MZ_DEFAULT_WINDOW_BITS) && (-window_bits != MZ_DEFAULT_WINDOW_BITS))
363 return MZ_PARAM_ERROR;
364
365 pStream->data_type = 0;
366 pStream->adler = 0;
367 pStream->msg = NULL;
368 pStream->total_in = 0;
369 pStream->total_out = 0;
370 pStream->reserved = 0;
371 if (!pStream->zalloc)
372 pStream->zalloc = miniz_def_alloc_func;
373 if (!pStream->zfree)
374 pStream->zfree = miniz_def_free_func;
375
376 pDecomp = (inflate_state*)pStream->zalloc(pStream->opaque, 1, sizeof(inflate_state));
377 if (!pDecomp)
378 return MZ_MEM_ERROR;
379
380 pStream->state = (struct mz_internal_state*)pDecomp;
381
382 tinfl_init(&pDecomp->m_decomp);
383 pDecomp->m_dict_ofs = 0;
384 pDecomp->m_dict_avail = 0;
385 pDecomp->m_last_status = TINFL_STATUS_NEEDS_MORE_INPUT;
386 pDecomp->m_first_call = 1;
387 pDecomp->m_has_flushed = 0;
388 pDecomp->m_window_bits = window_bits;
389
390 return MZ_OK;
391}
392
394{
396}
397
399{
400 inflate_state* pDecomp;
401 if (!pStream)
402 return MZ_STREAM_ERROR;
403
404 pStream->data_type = 0;
405 pStream->adler = 0;
406 pStream->msg = NULL;
407 pStream->total_in = 0;
408 pStream->total_out = 0;
409 pStream->reserved = 0;
410
411 pDecomp = (inflate_state*)pStream->state;
412
413 tinfl_init(&pDecomp->m_decomp);
414 pDecomp->m_dict_ofs = 0;
415 pDecomp->m_dict_avail = 0;
416 pDecomp->m_last_status = TINFL_STATUS_NEEDS_MORE_INPUT;
417 pDecomp->m_first_call = 1;
418 pDecomp->m_has_flushed = 0;
419 /* pDecomp->m_window_bits = window_bits */;
420
421 return MZ_OK;
422}
423
424int mz_inflate(mz_streamp pStream, int flush)
425{
426 inflate_state* pState;
427 mz_uint n, first_call, decomp_flags = TINFL_FLAG_COMPUTE_ADLER32;
428 size_t in_bytes, out_bytes, orig_avail_in;
429 tinfl_status status;
430
431 if ((!pStream) || (!pStream->state))
432 return MZ_STREAM_ERROR;
433 if (flush == MZ_PARTIAL_FLUSH)
434 flush = MZ_SYNC_FLUSH;
435 if ((flush) && (flush != MZ_SYNC_FLUSH) && (flush != MZ_FINISH))
436 return MZ_STREAM_ERROR;
437
438 pState = (inflate_state*)pStream->state;
439 if (pState->m_window_bits > 0)
440 decomp_flags |= TINFL_FLAG_PARSE_ZLIB_HEADER;
441 orig_avail_in = pStream->avail_in;
442
443 first_call = pState->m_first_call;
444 pState->m_first_call = 0;
445 if (pState->m_last_status < 0)
446 return MZ_DATA_ERROR;
447
448 if (pState->m_has_flushed && (flush != MZ_FINISH))
449 return MZ_STREAM_ERROR;
450 pState->m_has_flushed |= (flush == MZ_FINISH);
451
452 if ((flush == MZ_FINISH) && (first_call)) {
453 /* MZ_FINISH on the first call implies that the input and output buffers are large enough to hold the entire compressed/decompressed file. */
454 decomp_flags |= TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF;
455 in_bytes = pStream->avail_in;
456 out_bytes = pStream->avail_out;
457 status = tinfl_decompress(&pState->m_decomp, pStream->next_in, &in_bytes, pStream->next_out, pStream->next_out, &out_bytes, decomp_flags);
458 pState->m_last_status = status;
459 pStream->next_in += (mz_uint)in_bytes;
460 pStream->avail_in -= (mz_uint)in_bytes;
461 pStream->total_in += (mz_uint)in_bytes;
462 pStream->adler = tinfl_get_adler32(&pState->m_decomp);
463 pStream->next_out += (mz_uint)out_bytes;
464 pStream->avail_out -= (mz_uint)out_bytes;
465 pStream->total_out += (mz_uint)out_bytes;
466
467 if (status < 0)
468 return MZ_DATA_ERROR;
469 else if (status != TINFL_STATUS_DONE) {
470 pState->m_last_status = TINFL_STATUS_FAILED;
471 return MZ_BUF_ERROR;
472 }
473 return MZ_STREAM_END;
474 }
475 /* flush != MZ_FINISH then we must assume there's more input. */
476 if (flush != MZ_FINISH)
477 decomp_flags |= TINFL_FLAG_HAS_MORE_INPUT;
478
479 if (pState->m_dict_avail) {
480 n = MZ_MIN(pState->m_dict_avail, pStream->avail_out);
481 memcpy(pStream->next_out, pState->m_dict + pState->m_dict_ofs, n);
482 pStream->next_out += n;
483 pStream->avail_out -= n;
484 pStream->total_out += n;
485 pState->m_dict_avail -= n;
486 pState->m_dict_ofs = (pState->m_dict_ofs + n) & (TINFL_LZ_DICT_SIZE - 1);
487 return ((pState->m_last_status == TINFL_STATUS_DONE) && (!pState->m_dict_avail)) ? MZ_STREAM_END : MZ_OK;
488 }
489
490 for (;;) {
491 in_bytes = pStream->avail_in;
492 out_bytes = TINFL_LZ_DICT_SIZE - pState->m_dict_ofs;
493
494 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);
495 pState->m_last_status = status;
496
497 pStream->next_in += (mz_uint)in_bytes;
498 pStream->avail_in -= (mz_uint)in_bytes;
499 pStream->total_in += (mz_uint)in_bytes;
500 pStream->adler = tinfl_get_adler32(&pState->m_decomp);
501
502 pState->m_dict_avail = (mz_uint)out_bytes;
503
504 n = MZ_MIN(pState->m_dict_avail, pStream->avail_out);
505 memcpy(pStream->next_out, pState->m_dict + pState->m_dict_ofs, n);
506 pStream->next_out += n;
507 pStream->avail_out -= n;
508 pStream->total_out += n;
509 pState->m_dict_avail -= n;
510 pState->m_dict_ofs = (pState->m_dict_ofs + n) & (TINFL_LZ_DICT_SIZE - 1);
511
512 if (status < 0)
513 return MZ_DATA_ERROR; /* Stream is corrupted (there could be some uncompressed data left in the output dictionary - oh well). */
514 else if ((status == TINFL_STATUS_NEEDS_MORE_INPUT) && (!orig_avail_in))
515 return MZ_BUF_ERROR; /* Signal caller that we can't make forward progress without supplying more input or by setting flush to MZ_FINISH. */
516 else if (flush == MZ_FINISH) {
517 /* The output buffer MUST be large to hold the remaining uncompressed data when flush==MZ_FINISH. */
518 if (status == TINFL_STATUS_DONE)
519 return pState->m_dict_avail ? MZ_BUF_ERROR : MZ_STREAM_END;
520 /* 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. */
521 else if (!pStream->avail_out)
522 return MZ_BUF_ERROR;
523 } else if ((status == TINFL_STATUS_DONE) || (!pStream->avail_in) || (!pStream->avail_out) || (pState->m_dict_avail))
524 break;
525 }
526
527 return ((status == TINFL_STATUS_DONE) && (!pState->m_dict_avail)) ? MZ_STREAM_END : MZ_OK;
528}
529
531{
532 if (!pStream)
533 return MZ_STREAM_ERROR;
534 if (pStream->state) {
535 pStream->zfree(pStream->opaque, pStream->state);
536 pStream->state = NULL;
537 }
538 return MZ_OK;
539}
540int mz_uncompress2(unsigned char* pDest, mz_ulong* pDest_len, const unsigned char* pSource, mz_ulong* pSource_len)
541{
543 int status;
544 memset(&stream, 0, sizeof(stream));
545
546 /* In case mz_ulong is 64-bits (argh I hate longs). */
547 if ((mz_uint64)(*pSource_len | *pDest_len) > 0xFFFFFFFFU)
548 return MZ_PARAM_ERROR;
549
550 stream.next_in = pSource;
551 stream.avail_in = (mz_uint32)*pSource_len;
552 stream.next_out = pDest;
553 stream.avail_out = (mz_uint32)*pDest_len;
554
555 status = mz_inflateInit(&stream);
556 if (status != MZ_OK)
557 return status;
558
559 status = mz_inflate(&stream, MZ_FINISH);
560 *pSource_len = *pSource_len - stream.avail_in;
561 if (status != MZ_STREAM_END) {
563 return ((status == MZ_BUF_ERROR) && (!stream.avail_in)) ? MZ_DATA_ERROR : status;
564 }
565 *pDest_len = stream.total_out;
566
567 return mz_inflateEnd(&stream);
568}
569
570int mz_uncompress(unsigned char* pDest, mz_ulong* pDest_len, const unsigned char* pSource, mz_ulong source_len)
571{
572 return mz_uncompress2(pDest, pDest_len, pSource, &source_len);
573}
574
575#endif /*#ifndef MINIZ_NO_INFLATE_APIS*/
576
577const char* mz_error(int err)
578{
579 static struct
580 {
581 int m_err;
582 const char* m_pDesc;
583 } s_error_descs[] = {
584 { 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" }
585 };
586 mz_uint i;
587 for (i = 0; i < sizeof(s_error_descs) / sizeof(s_error_descs[0]); ++i)
588 if (s_error_descs[i].m_err == err)
589 return s_error_descs[i].m_pDesc;
590 return NULL;
591}
592
593#endif /*MINIZ_NO_ZLIB_APIS */
594
595#ifdef __cplusplus
596}
597#endif
598
599/*
600 This is free and unencumbered software released into the public domain.
601
602 Anyone is free to copy, modify, publish, use, compile, sell, or
603 distribute this software, either in source code form or as a compiled
604 binary, for any purpose, commercial or non-commercial, and by any
605 means.
606
607 In jurisdictions that recognize copyright laws, the author or authors
608 of this software dedicate any and all copyright interest in the
609 software to the public domain. We make this dedication for the benefit
610 of the public at large and to the detriment of our heirs and
611 successors. We intend this dedication to be an overt act of
612 relinquishment in perpetuity of all present and future rights to this
613 software under copyright law.
614
615 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
616 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
617 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
618 IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
619 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
620 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
621 OTHER DEALINGS IN THE SOFTWARE.
622
623 For more information, please refer to <http://unlicense.org/>
624*/
625/**************************************************************************
626 *
627 * Copyright 2013-2014 RAD Game Tools and Valve Software
628 * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
629 * All Rights Reserved.
630 *
631 * Permission is hereby granted, free of charge, to any person obtaining a copy
632 * of this software and associated documentation files (the "Software"), to deal
633 * in the Software without restriction, including without limitation the rights
634 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
635 * copies of the Software, and to permit persons to whom the Software is
636 * furnished to do so, subject to the following conditions:
637 *
638 * The above copyright notice and this permission notice shall be included in
639 * all copies or substantial portions of the Software.
640 *
641 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
642 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
643 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
644 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
645 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
646 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
647 * THE SOFTWARE.
648 *
649 **************************************************************************/
650
651#ifndef MINIZ_NO_DEFLATE_APIS
652
653#ifdef __cplusplus
654extern "C" {
655#endif
656
657/* ------------------- Low-level Compression (independent from all decompression API's) */
658
659/* Purposely making these tables static for faster init and thread safety. */
660static const mz_uint16 s_tdefl_len_sym[256] = {
661 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,
662 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,
663 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,
664 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,
665 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,
666 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,
667 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,
668 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
669};
670
671static const mz_uint8 s_tdefl_len_extra[256] = {
672 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,
673 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,
674 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,
675 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
676};
677
678static const mz_uint8 s_tdefl_small_dist_sym[512] = {
679 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,
680 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,
681 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,
682 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,
683 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,
684 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,
685 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,
686 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,
687 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,
688 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,
689 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,
690 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
691};
692
693static const mz_uint8 s_tdefl_small_dist_extra[512] = {
694 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,
695 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,
696 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,
697 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,
698 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,
699 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,
700 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,
701 7, 7, 7, 7, 7, 7, 7, 7
702};
703
704static const mz_uint8 s_tdefl_large_dist_sym[128] = {
705 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,
706 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,
707 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
708};
709
710static const mz_uint8 s_tdefl_large_dist_extra[128] = {
711 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,
712 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,
713 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
714};
715
716/* Radix sorts tdefl_sym_freq[] array by 16-bit key m_key. Returns ptr to sorted values. */
717typedef struct
718{
719 mz_uint16 m_key, m_sym_index;
721static tdefl_sym_freq* tdefl_radix_sort_syms(mz_uint num_syms, tdefl_sym_freq* pSyms0, tdefl_sym_freq* pSyms1)
722{
723 mz_uint32 total_passes = 2, pass_shift, pass, i, hist[256 * 2];
724 tdefl_sym_freq *pCur_syms = pSyms0, *pNew_syms = pSyms1;
725 MZ_CLEAR_ARR(hist);
726 for (i = 0; i < num_syms; i++) {
727 mz_uint freq = pSyms0[i].m_key;
728 hist[freq & 0xFF]++;
729 hist[256 + ((freq >> 8) & 0xFF)]++;
730 }
731 while ((total_passes > 1) && (num_syms == hist[(total_passes - 1) * 256]))
732 total_passes--;
733 for (pass_shift = 0, pass = 0; pass < total_passes; pass++, pass_shift += 8) {
734 const mz_uint32* pHist = &hist[pass << 8];
735 mz_uint offsets[256], cur_ofs = 0;
736 for (i = 0; i < 256; i++) {
737 offsets[i] = cur_ofs;
738 cur_ofs += pHist[i];
739 }
740 for (i = 0; i < num_syms; i++)
741 pNew_syms[offsets[(pCur_syms[i].m_key >> pass_shift) & 0xFF]++] = pCur_syms[i];
742 {
743 tdefl_sym_freq* t = pCur_syms;
744 pCur_syms = pNew_syms;
745 pNew_syms = t;
746 }
747 }
748 return pCur_syms;
749}
750
751/* tdefl_calculate_minimum_redundancy() originally written by: Alistair Moffat, alistair@cs.mu.oz.au, Jyrki Katajainen, jyrki@diku.dk, November 1996. */
753{
754 int root, leaf, next, avbl, used, dpth;
755 if (n == 0)
756 return;
757 else if (n == 1) {
758 A[0].m_key = 1;
759 return;
760 }
761 A[0].m_key += A[1].m_key;
762 root = 0;
763 leaf = 2;
764 for (next = 1; next < n - 1; next++) {
765 if (leaf >= n || A[root].m_key < A[leaf].m_key) {
766 A[next].m_key = A[root].m_key;
767 A[root++].m_key = (mz_uint16)next;
768 } else
769 A[next].m_key = A[leaf++].m_key;
770 if (leaf >= n || (root < next && A[root].m_key < A[leaf].m_key)) {
771 A[next].m_key = (mz_uint16)(A[next].m_key + A[root].m_key);
772 A[root++].m_key = (mz_uint16)next;
773 } else
774 A[next].m_key = (mz_uint16)(A[next].m_key + A[leaf++].m_key);
775 }
776 A[n - 2].m_key = 0;
777 for (next = n - 3; next >= 0; next--)
778 A[next].m_key = A[A[next].m_key].m_key + 1;
779 avbl = 1;
780 used = dpth = 0;
781 root = n - 2;
782 next = n - 1;
783 while (avbl > 0) {
784 while (root >= 0 && (int)A[root].m_key == dpth) {
785 used++;
786 root--;
787 }
788 while (avbl > used) {
789 A[next--].m_key = (mz_uint16)(dpth);
790 avbl--;
791 }
792 avbl = 2 * used;
793 dpth++;
794 used = 0;
795 }
796}
797
798/* Limits canonical Huffman code table's max code size. */
799enum {
801};
802static void tdefl_huffman_enforce_max_code_size(int* pNum_codes, int code_list_len, int max_code_size)
803{
804 int i;
805 mz_uint32 total = 0;
806 if (code_list_len <= 1)
807 return;
808 for (i = max_code_size + 1; i <= TDEFL_MAX_SUPPORTED_HUFF_CODESIZE; i++)
809 pNum_codes[max_code_size] += pNum_codes[i];
810 for (i = max_code_size; i > 0; i--)
811 total += (((mz_uint32)pNum_codes[i]) << (max_code_size - i));
812 while (total != (1UL << max_code_size)) {
813 pNum_codes[max_code_size]--;
814 for (i = max_code_size - 1; i > 0; i--)
815 if (pNum_codes[i]) {
816 pNum_codes[i]--;
817 pNum_codes[i + 1] += 2;
818 break;
819 }
820 total--;
821 }
822}
823
824static void tdefl_optimize_huffman_table(tdefl_compressor* d, int table_num, int table_len, int code_size_limit, int static_table)
825{
826 int i, j, l, num_codes[1 + TDEFL_MAX_SUPPORTED_HUFF_CODESIZE];
827 mz_uint next_code[TDEFL_MAX_SUPPORTED_HUFF_CODESIZE + 1];
828 MZ_CLEAR_ARR(num_codes);
829 if (static_table) {
830 for (i = 0; i < table_len; i++)
831 num_codes[d->m_huff_code_sizes[table_num][i]]++;
832 } else {
833 tdefl_sym_freq syms0[TDEFL_MAX_HUFF_SYMBOLS], syms1[TDEFL_MAX_HUFF_SYMBOLS], *pSyms;
834 int num_used_syms = 0;
835 const mz_uint16* pSym_count = &d->m_huff_count[table_num][0];
836 for (i = 0; i < table_len; i++)
837 if (pSym_count[i]) {
838 syms0[num_used_syms].m_key = (mz_uint16)pSym_count[i];
839 syms0[num_used_syms++].m_sym_index = (mz_uint16)i;
840 }
841
842 pSyms = tdefl_radix_sort_syms(num_used_syms, syms0, syms1);
843 tdefl_calculate_minimum_redundancy(pSyms, num_used_syms);
844
845 for (i = 0; i < num_used_syms; i++)
846 num_codes[pSyms[i].m_key]++;
847
848 tdefl_huffman_enforce_max_code_size(num_codes, num_used_syms, code_size_limit);
849
850 MZ_CLEAR_ARR(d->m_huff_code_sizes[table_num]);
851 MZ_CLEAR_ARR(d->m_huff_codes[table_num]);
852 for (i = 1, j = num_used_syms; i <= code_size_limit; i++)
853 for (l = num_codes[i]; l > 0; l--)
854 d->m_huff_code_sizes[table_num][pSyms[--j].m_sym_index] = (mz_uint8)(i);
855 }
856
857 next_code[1] = 0;
858 for (j = 0, i = 2; i <= code_size_limit; i++)
859 next_code[i] = j = ((j + num_codes[i - 1]) << 1);
860
861 for (i = 0; i < table_len; i++) {
862 mz_uint rev_code = 0, code, code_size;
863 if ((code_size = d->m_huff_code_sizes[table_num][i]) == 0)
864 continue;
865 code = next_code[code_size]++;
866 for (l = code_size; l > 0; l--, code >>= 1)
867 rev_code = (rev_code << 1) | (code & 1);
868 d->m_huff_codes[table_num][i] = (mz_uint16)rev_code;
869 }
870}
871
872#define TDEFL_PUT_BITS(b, l) \
873 do { \
874 mz_uint bits = b; \
875 mz_uint len = l; \
876 MZ_ASSERT(bits <= ((1U << len) - 1U)); \
877 d->m_bit_buffer |= (bits << d->m_bits_in); \
878 d->m_bits_in += len; \
879 while (d->m_bits_in >= 8) { \
880 if (d->m_pOutput_buf < d->m_pOutput_buf_end) \
881 *d->m_pOutput_buf++ = (mz_uint8)(d->m_bit_buffer); \
882 d->m_bit_buffer >>= 8; \
883 d->m_bits_in -= 8; \
884 } \
885 } \
886 MZ_MACRO_END
887
888#define TDEFL_RLE_PREV_CODE_SIZE() \
889 { \
890 if (rle_repeat_count) { \
891 if (rle_repeat_count < 3) { \
892 d->m_huff_count[2][prev_code_size] = (mz_uint16)(d->m_huff_count[2][prev_code_size] + rle_repeat_count); \
893 while (rle_repeat_count--) \
894 packed_code_sizes[num_packed_code_sizes++] = prev_code_size; \
895 } else { \
896 d->m_huff_count[2][16] = (mz_uint16)(d->m_huff_count[2][16] + 1); \
897 packed_code_sizes[num_packed_code_sizes++] = 16; \
898 packed_code_sizes[num_packed_code_sizes++] = (mz_uint8)(rle_repeat_count - 3); \
899 } \
900 rle_repeat_count = 0; \
901 } \
902 }
903
904#define TDEFL_RLE_ZERO_CODE_SIZE() \
905 { \
906 if (rle_z_count) { \
907 if (rle_z_count < 3) { \
908 d->m_huff_count[2][0] = (mz_uint16)(d->m_huff_count[2][0] + rle_z_count); \
909 while (rle_z_count--) \
910 packed_code_sizes[num_packed_code_sizes++] = 0; \
911 } else if (rle_z_count <= 10) { \
912 d->m_huff_count[2][17] = (mz_uint16)(d->m_huff_count[2][17] + 1); \
913 packed_code_sizes[num_packed_code_sizes++] = 17; \
914 packed_code_sizes[num_packed_code_sizes++] = (mz_uint8)(rle_z_count - 3); \
915 } else { \
916 d->m_huff_count[2][18] = (mz_uint16)(d->m_huff_count[2][18] + 1); \
917 packed_code_sizes[num_packed_code_sizes++] = 18; \
918 packed_code_sizes[num_packed_code_sizes++] = (mz_uint8)(rle_z_count - 11); \
919 } \
920 rle_z_count = 0; \
921 } \
922 }
923
924static 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 };
925
926static void tdefl_start_dynamic_block(tdefl_compressor* d)
927{
928 int num_lit_codes, num_dist_codes, num_bit_lengths;
929 mz_uint i, total_code_sizes_to_pack, num_packed_code_sizes, rle_z_count, rle_repeat_count, packed_code_sizes_index;
930 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;
931
932 d->m_huff_count[0][256] = 1;
933
934 tdefl_optimize_huffman_table(d, 0, TDEFL_MAX_HUFF_SYMBOLS_0, 15, MZ_FALSE);
935 tdefl_optimize_huffman_table(d, 1, TDEFL_MAX_HUFF_SYMBOLS_1, 15, MZ_FALSE);
936
937 for (num_lit_codes = 286; num_lit_codes > 257; num_lit_codes--)
938 if (d->m_huff_code_sizes[0][num_lit_codes - 1])
939 break;
940 for (num_dist_codes = 30; num_dist_codes > 1; num_dist_codes--)
941 if (d->m_huff_code_sizes[1][num_dist_codes - 1])
942 break;
943
944 memcpy(code_sizes_to_pack, &d->m_huff_code_sizes[0][0], num_lit_codes);
945 memcpy(code_sizes_to_pack + num_lit_codes, &d->m_huff_code_sizes[1][0], num_dist_codes);
946 total_code_sizes_to_pack = num_lit_codes + num_dist_codes;
947 num_packed_code_sizes = 0;
948 rle_z_count = 0;
949 rle_repeat_count = 0;
950
951 memset(&d->m_huff_count[2][0], 0, sizeof(d->m_huff_count[2][0]) * TDEFL_MAX_HUFF_SYMBOLS_2);
952 for (i = 0; i < total_code_sizes_to_pack; i++) {
953 mz_uint8 code_size = code_sizes_to_pack[i];
954 if (!code_size) {
956 if (++rle_z_count == 138) {
958 }
959 } else {
961 if (code_size != prev_code_size) {
963 d->m_huff_count[2][code_size] = (mz_uint16)(d->m_huff_count[2][code_size] + 1);
964 packed_code_sizes[num_packed_code_sizes++] = code_size;
965 } else if (++rle_repeat_count == 6) {
967 }
968 }
969 prev_code_size = code_size;
970 }
971 if (rle_repeat_count) {
973 } else {
975 }
976
977 tdefl_optimize_huffman_table(d, 2, TDEFL_MAX_HUFF_SYMBOLS_2, 7, MZ_FALSE);
978
979 TDEFL_PUT_BITS(2, 2);
980
981 TDEFL_PUT_BITS(num_lit_codes - 257, 5);
982 TDEFL_PUT_BITS(num_dist_codes - 1, 5);
983
984 for (num_bit_lengths = 18; num_bit_lengths >= 0; num_bit_lengths--)
985 if (d->m_huff_code_sizes[2][s_tdefl_packed_code_size_syms_swizzle[num_bit_lengths]])
986 break;
987 num_bit_lengths = MZ_MAX(4, (num_bit_lengths + 1));
988 TDEFL_PUT_BITS(num_bit_lengths - 4, 4);
989 for (i = 0; (int)i < num_bit_lengths; i++)
990 TDEFL_PUT_BITS(d->m_huff_code_sizes[2][s_tdefl_packed_code_size_syms_swizzle[i]], 3);
991
992 for (packed_code_sizes_index = 0; packed_code_sizes_index < num_packed_code_sizes;) {
993 mz_uint code = packed_code_sizes[packed_code_sizes_index++];
994 MZ_ASSERT(code < TDEFL_MAX_HUFF_SYMBOLS_2);
995 TDEFL_PUT_BITS(d->m_huff_codes[2][code], d->m_huff_code_sizes[2][code]);
996 if (code >= 16)
997 TDEFL_PUT_BITS(packed_code_sizes[packed_code_sizes_index++], "\02\03\07"[code - 16]);
998 }
999}
1000
1001static void tdefl_start_static_block(tdefl_compressor* d)
1002{
1003 mz_uint i;
1004 mz_uint8* p = &d->m_huff_code_sizes[0][0];
1005
1006 for (i = 0; i <= 143; ++i)
1007 *p++ = 8;
1008 for (; i <= 255; ++i)
1009 *p++ = 9;
1010 for (; i <= 279; ++i)
1011 *p++ = 7;
1012 for (; i <= 287; ++i)
1013 *p++ = 8;
1014
1015 memset(d->m_huff_code_sizes[1], 5, 32);
1016
1017 tdefl_optimize_huffman_table(d, 0, 288, 15, MZ_TRUE);
1018 tdefl_optimize_huffman_table(d, 1, 32, 15, MZ_TRUE);
1019
1020 TDEFL_PUT_BITS(1, 2);
1021}
1022
1023static const mz_uint mz_bitmasks[17] = { 0x0000, 0x0001, 0x0003, 0x0007, 0x000F, 0x001F, 0x003F, 0x007F, 0x00FF, 0x01FF, 0x03FF, 0x07FF, 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF };
1024
1025#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN && MINIZ_HAS_64BIT_REGISTERS
1026static mz_bool tdefl_compress_lz_codes(tdefl_compressor* d)
1027{
1028 mz_uint flags;
1029 mz_uint8* pLZ_codes;
1030 mz_uint8* pOutput_buf = d->m_pOutput_buf;
1031 mz_uint8* pLZ_code_buf_end = d->m_pLZ_code_buf;
1032 mz_uint64 bit_buffer = d->m_bit_buffer;
1033 mz_uint bits_in = d->m_bits_in;
1034
1035#define TDEFL_PUT_BITS_FAST(b, l) \
1036 { \
1037 bit_buffer |= (((mz_uint64)(b)) << bits_in); \
1038 bits_in += (l); \
1039 }
1040
1041 flags = 1;
1042 for (pLZ_codes = d->m_lz_code_buf; pLZ_codes < pLZ_code_buf_end; flags >>= 1) {
1043 if (flags == 1)
1044 flags = *pLZ_codes++ | 0x100;
1045
1046 if (flags & 1) {
1047 mz_uint s0, s1, n0, n1, sym, num_extra_bits;
1048 mz_uint match_len = pLZ_codes[0];
1049 mz_uint match_dist = (pLZ_codes[1] | (pLZ_codes[2] << 8));
1050 pLZ_codes += 3;
1051
1052 MZ_ASSERT(d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]);
1053 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]]);
1054 TDEFL_PUT_BITS_FAST(match_len & mz_bitmasks[s_tdefl_len_extra[match_len]], s_tdefl_len_extra[match_len]);
1055
1056 /* This sequence coaxes MSVC into using cmov's vs. jmp's. */
1057 s0 = s_tdefl_small_dist_sym[match_dist & 511];
1058 n0 = s_tdefl_small_dist_extra[match_dist & 511];
1059 s1 = s_tdefl_large_dist_sym[match_dist >> 8];
1060 n1 = s_tdefl_large_dist_extra[match_dist >> 8];
1061 sym = (match_dist < 512) ? s0 : s1;
1062 num_extra_bits = (match_dist < 512) ? n0 : n1;
1063
1064 MZ_ASSERT(d->m_huff_code_sizes[1][sym]);
1065 TDEFL_PUT_BITS_FAST(d->m_huff_codes[1][sym], d->m_huff_code_sizes[1][sym]);
1066 TDEFL_PUT_BITS_FAST(match_dist & mz_bitmasks[num_extra_bits], num_extra_bits);
1067 } else {
1068 mz_uint lit = *pLZ_codes++;
1069 MZ_ASSERT(d->m_huff_code_sizes[0][lit]);
1070 TDEFL_PUT_BITS_FAST(d->m_huff_codes[0][lit], d->m_huff_code_sizes[0][lit]);
1071
1072 if (((flags & 2) == 0) && (pLZ_codes < pLZ_code_buf_end)) {
1073 flags >>= 1;
1074 lit = *pLZ_codes++;
1075 MZ_ASSERT(d->m_huff_code_sizes[0][lit]);
1076 TDEFL_PUT_BITS_FAST(d->m_huff_codes[0][lit], d->m_huff_code_sizes[0][lit]);
1077
1078 if (((flags & 2) == 0) && (pLZ_codes < pLZ_code_buf_end)) {
1079 flags >>= 1;
1080 lit = *pLZ_codes++;
1081 MZ_ASSERT(d->m_huff_code_sizes[0][lit]);
1082 TDEFL_PUT_BITS_FAST(d->m_huff_codes[0][lit], d->m_huff_code_sizes[0][lit]);
1083 }
1084 }
1085 }
1086
1087 if (pOutput_buf >= d->m_pOutput_buf_end)
1088 return MZ_FALSE;
1089
1090 memcpy(pOutput_buf, &bit_buffer, sizeof(mz_uint64));
1091 pOutput_buf += (bits_in >> 3);
1092 bit_buffer >>= (bits_in & ~7);
1093 bits_in &= 7;
1094 }
1095
1096#undef TDEFL_PUT_BITS_FAST
1097
1098 d->m_pOutput_buf = pOutput_buf;
1099 d->m_bits_in = 0;
1100 d->m_bit_buffer = 0;
1101
1102 while (bits_in) {
1103 mz_uint32 n = MZ_MIN(bits_in, 16);
1104 TDEFL_PUT_BITS((mz_uint)bit_buffer & mz_bitmasks[n], n);
1105 bit_buffer >>= n;
1106 bits_in -= n;
1107 }
1108
1109 TDEFL_PUT_BITS(d->m_huff_codes[0][256], d->m_huff_code_sizes[0][256]);
1110
1111 return (d->m_pOutput_buf < d->m_pOutput_buf_end);
1112}
1113#else
1114static mz_bool tdefl_compress_lz_codes(tdefl_compressor* d)
1115{
1116 mz_uint flags;
1117 mz_uint8* pLZ_codes;
1118
1119 flags = 1;
1120 for (pLZ_codes = d->m_lz_code_buf; pLZ_codes < d->m_pLZ_code_buf; flags >>= 1) {
1121 if (flags == 1)
1122 flags = *pLZ_codes++ | 0x100;
1123 if (flags & 1) {
1124 mz_uint sym, num_extra_bits;
1125 mz_uint match_len = pLZ_codes[0], match_dist = (pLZ_codes[1] | (pLZ_codes[2] << 8));
1126 pLZ_codes += 3;
1127
1128 MZ_ASSERT(d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]);
1129 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]]);
1130 TDEFL_PUT_BITS(match_len & mz_bitmasks[s_tdefl_len_extra[match_len]], s_tdefl_len_extra[match_len]);
1131
1132 if (match_dist < 512) {
1133 sym = s_tdefl_small_dist_sym[match_dist];
1134 num_extra_bits = s_tdefl_small_dist_extra[match_dist];
1135 } else {
1136 sym = s_tdefl_large_dist_sym[match_dist >> 8];
1137 num_extra_bits = s_tdefl_large_dist_extra[match_dist >> 8];
1138 }
1139 MZ_ASSERT(d->m_huff_code_sizes[1][sym]);
1140 TDEFL_PUT_BITS(d->m_huff_codes[1][sym], d->m_huff_code_sizes[1][sym]);
1141 TDEFL_PUT_BITS(match_dist & mz_bitmasks[num_extra_bits], num_extra_bits);
1142 } else {
1143 mz_uint lit = *pLZ_codes++;
1144 MZ_ASSERT(d->m_huff_code_sizes[0][lit]);
1145 TDEFL_PUT_BITS(d->m_huff_codes[0][lit], d->m_huff_code_sizes[0][lit]);
1146 }
1147 }
1148
1149 TDEFL_PUT_BITS(d->m_huff_codes[0][256], d->m_huff_code_sizes[0][256]);
1150
1151 return (d->m_pOutput_buf < d->m_pOutput_buf_end);
1152}
1153#endif /* MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN && MINIZ_HAS_64BIT_REGISTERS */
1154
1155static mz_bool tdefl_compress_block(tdefl_compressor* d, mz_bool static_block)
1156{
1157 if (static_block)
1159 else
1161 return tdefl_compress_lz_codes(d);
1162}
1163
1164static const mz_uint s_tdefl_num_probes[11];
1165
1166static int tdefl_flush_block(tdefl_compressor* d, int flush)
1167{
1168 mz_uint saved_bit_buf, saved_bits_in;
1169 mz_uint8* pSaved_output_buf;
1170 mz_bool comp_block_succeeded = MZ_FALSE;
1171 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;
1172 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;
1173
1174 d->m_pOutput_buf = pOutput_buf_start;
1175 d->m_pOutput_buf_end = d->m_pOutput_buf + TDEFL_OUT_BUF_SIZE - 16;
1176
1177 MZ_ASSERT(!d->m_output_flush_remaining);
1178 d->m_output_flush_ofs = 0;
1179 d->m_output_flush_remaining = 0;
1180
1181 *d->m_pLZ_flags = (mz_uint8)(*d->m_pLZ_flags >> d->m_num_flags_left);
1182 d->m_pLZ_code_buf -= (d->m_num_flags_left == 8);
1183
1184 if ((d->m_flags & TDEFL_WRITE_ZLIB_HEADER) && (!d->m_block_index)) {
1185 const mz_uint8 cmf = 0x78;
1186 mz_uint8 flg, flevel = 3;
1187 mz_uint header, i, mz_un = sizeof(s_tdefl_num_probes) / sizeof(mz_uint);
1188
1189 /* Determine compression level by reversing the process in tdefl_create_comp_flags_from_zip_params() */
1190 for (i = 0; i < mz_un; i++)
1191 if (s_tdefl_num_probes[i] == (d->m_flags & 0xFFF))
1192 break;
1193
1194 if (i < 2)
1195 flevel = 0;
1196 else if (i < 6)
1197 flevel = 1;
1198 else if (i == 6)
1199 flevel = 2;
1200
1201 header = cmf << 8 | (flevel << 6);
1202 header += 31 - (header % 31);
1203 flg = header & 0xFF;
1204
1205 TDEFL_PUT_BITS(cmf, 8);
1206 TDEFL_PUT_BITS(flg, 8);
1207 }
1208
1209 TDEFL_PUT_BITS(flush == TDEFL_FINISH, 1);
1210
1211 pSaved_output_buf = d->m_pOutput_buf;
1212 saved_bit_buf = d->m_bit_buffer;
1213 saved_bits_in = d->m_bits_in;
1214
1215 if (!use_raw_block)
1216 comp_block_succeeded = tdefl_compress_block(d, (d->m_flags & TDEFL_FORCE_ALL_STATIC_BLOCKS) || (d->m_total_lz_bytes < 48));
1217
1218 /* If the block gets expanded, forget the current contents of the output buffer and send a raw block instead. */
1219 if (((use_raw_block) || ((d->m_total_lz_bytes) && ((d->m_pOutput_buf - pSaved_output_buf + 1U) >= d->m_total_lz_bytes))) && ((d->m_lookahead_pos - d->m_lz_code_buf_dict_pos) <= d->m_dict_size)) {
1220 mz_uint i;
1221 d->m_pOutput_buf = pSaved_output_buf;
1222 d->m_bit_buffer = saved_bit_buf, d->m_bits_in = saved_bits_in;
1223 TDEFL_PUT_BITS(0, 2);
1224 if (d->m_bits_in) {
1225 TDEFL_PUT_BITS(0, 8 - d->m_bits_in);
1226 }
1227 for (i = 2; i; --i, d->m_total_lz_bytes ^= 0xFFFF) {
1228 TDEFL_PUT_BITS(d->m_total_lz_bytes & 0xFFFF, 16);
1229 }
1230 for (i = 0; i < d->m_total_lz_bytes; ++i) {
1231 TDEFL_PUT_BITS(d->m_dict[(d->m_lz_code_buf_dict_pos + i) & TDEFL_LZ_DICT_SIZE_MASK], 8);
1232 }
1233 }
1234 /* Check for the extremely unlikely (if not impossible) case of the compressed block not fitting into the output buffer when using dynamic codes. */
1235 else if (!comp_block_succeeded) {
1236 d->m_pOutput_buf = pSaved_output_buf;
1237 d->m_bit_buffer = saved_bit_buf, d->m_bits_in = saved_bits_in;
1238 tdefl_compress_block(d, MZ_TRUE);
1239 }
1240
1241 if (flush) {
1242 if (flush == TDEFL_FINISH) {
1243 if (d->m_bits_in) {
1244 TDEFL_PUT_BITS(0, 8 - d->m_bits_in);
1245 }
1246 if (d->m_flags & TDEFL_WRITE_ZLIB_HEADER) {
1247 mz_uint i, a = d->m_adler32;
1248 for (i = 0; i < 4; i++) {
1249 TDEFL_PUT_BITS((a >> 24) & 0xFF, 8);
1250 a <<= 8;
1251 }
1252 }
1253 } else {
1254 mz_uint i, z = 0;
1255 TDEFL_PUT_BITS(0, 3);
1256 if (d->m_bits_in) {
1257 TDEFL_PUT_BITS(0, 8 - d->m_bits_in);
1258 }
1259 for (i = 2; i; --i, z ^= 0xFFFF) {
1260 TDEFL_PUT_BITS(z & 0xFFFF, 16);
1261 }
1262 }
1263 }
1264
1265 MZ_ASSERT(d->m_pOutput_buf < d->m_pOutput_buf_end);
1266
1267 memset(&d->m_huff_count[0][0], 0, sizeof(d->m_huff_count[0][0]) * TDEFL_MAX_HUFF_SYMBOLS_0);
1268 memset(&d->m_huff_count[1][0], 0, sizeof(d->m_huff_count[1][0]) * TDEFL_MAX_HUFF_SYMBOLS_1);
1269
1270 d->m_pLZ_code_buf = d->m_lz_code_buf + 1;
1271 d->m_pLZ_flags = d->m_lz_code_buf;
1272 d->m_num_flags_left = 8;
1273 d->m_lz_code_buf_dict_pos += d->m_total_lz_bytes;
1274 d->m_total_lz_bytes = 0;
1275 d->m_block_index++;
1276
1277 if ((n = (int)(d->m_pOutput_buf - pOutput_buf_start)) != 0) {
1278 if (d->m_pPut_buf_func) {
1279 *d->m_pIn_buf_size = d->m_pSrc - (const mz_uint8*)d->m_pIn_buf;
1280 if (!(*d->m_pPut_buf_func)(d->m_output_buf, n, d->m_pPut_buf_user))
1281 return (d->m_prev_return_status = TDEFL_STATUS_PUT_BUF_FAILED);
1282 } else if (pOutput_buf_start == d->m_output_buf) {
1283 int bytes_to_copy = (int)MZ_MIN((size_t)n, (size_t)(*d->m_pOut_buf_size - d->m_out_buf_ofs));
1284 memcpy((mz_uint8*)d->m_pOut_buf + d->m_out_buf_ofs, d->m_output_buf, bytes_to_copy);
1285 d->m_out_buf_ofs += bytes_to_copy;
1286 if ((n -= bytes_to_copy) != 0) {
1287 d->m_output_flush_ofs = bytes_to_copy;
1288 d->m_output_flush_remaining = n;
1289 }
1290 } else {
1291 d->m_out_buf_ofs += n;
1292 }
1293 }
1294
1295 return d->m_output_flush_remaining;
1296}
1297
1298#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES
1299#ifdef MINIZ_UNALIGNED_USE_MEMCPY
1300static mz_uint16 TDEFL_READ_UNALIGNED_WORD(const mz_uint8* p)
1301{
1302 mz_uint16 ret;
1303 memcpy(&ret, p, sizeof(mz_uint16));
1304 return ret;
1305}
1306static mz_uint16 TDEFL_READ_UNALIGNED_WORD2(const mz_uint16* p)
1307{
1308 mz_uint16 ret;
1309 memcpy(&ret, p, sizeof(mz_uint16));
1310 return ret;
1311}
1312#else
1313#define TDEFL_READ_UNALIGNED_WORD(p) *(const mz_uint16*)(p)
1314#define TDEFL_READ_UNALIGNED_WORD2(p) *(const mz_uint16*)(p)
1315#endif
1316static 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)
1317{
1318 mz_uint dist, pos = lookahead_pos & TDEFL_LZ_DICT_SIZE_MASK, match_len = *pMatch_len, probe_pos = pos, next_probe_pos, probe_len;
1319 mz_uint num_probes_left = d->m_max_probes[match_len >= 32];
1320 const mz_uint16 *s = (const mz_uint16*)(d->m_dict + pos), *p, *q;
1321 mz_uint16 c01 = TDEFL_READ_UNALIGNED_WORD(&d->m_dict[pos + match_len - 1]), s01 = TDEFL_READ_UNALIGNED_WORD2(s);
1322 MZ_ASSERT(max_match_len <= TDEFL_MAX_MATCH_LEN);
1323 if (max_match_len <= match_len)
1324 return;
1325 for (;;) {
1326 for (;;) {
1327 if (--num_probes_left == 0)
1328 return;
1329#define TDEFL_PROBE \
1330 next_probe_pos = d->m_next[probe_pos]; \
1331 if ((!next_probe_pos) || ((dist = (mz_uint16)(lookahead_pos - next_probe_pos)) > max_dist)) \
1332 return; \
1333 probe_pos = next_probe_pos & TDEFL_LZ_DICT_SIZE_MASK; \
1334 if (TDEFL_READ_UNALIGNED_WORD(&d->m_dict[probe_pos + match_len - 1]) == c01) \
1335 break;
1339 }
1340 if (!dist)
1341 break;
1342 q = (const mz_uint16*)(d->m_dict + probe_pos);
1343 if (TDEFL_READ_UNALIGNED_WORD2(q) != s01)
1344 continue;
1345 p = s;
1346 probe_len = 32;
1347 do {
1348 } while ((TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) && (TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) && (TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) && (TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) && (--probe_len > 0));
1349 if (!probe_len) {
1350 *pMatch_dist = dist;
1351 *pMatch_len = MZ_MIN(max_match_len, (mz_uint)TDEFL_MAX_MATCH_LEN);
1352 break;
1353 } else if ((probe_len = ((mz_uint)(p - s) * 2) + (mz_uint)(*(const mz_uint8*)p == *(const mz_uint8*)q)) > match_len) {
1354 *pMatch_dist = dist;
1355 if ((*pMatch_len = match_len = MZ_MIN(max_match_len, probe_len)) == max_match_len)
1356 break;
1357 c01 = TDEFL_READ_UNALIGNED_WORD(&d->m_dict[pos + match_len - 1]);
1358 }
1359 }
1360}
1361#else
1362static 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)
1363{
1364 mz_uint dist, pos = lookahead_pos & TDEFL_LZ_DICT_SIZE_MASK, match_len = *pMatch_len, probe_pos = pos, next_probe_pos, probe_len;
1365 mz_uint num_probes_left = d->m_max_probes[match_len >= 32];
1366 const mz_uint8 *s = d->m_dict + pos, *p, *q;
1367 mz_uint8 c0 = d->m_dict[pos + match_len], c1 = d->m_dict[pos + match_len - 1];
1368 MZ_ASSERT(max_match_len <= TDEFL_MAX_MATCH_LEN);
1369 if (max_match_len <= match_len)
1370 return;
1371 for (;;) {
1372 for (;;) {
1373 if (--num_probes_left == 0)
1374 return;
1375#define TDEFL_PROBE \
1376 next_probe_pos = d->m_next[probe_pos]; \
1377 if ((!next_probe_pos) || ((dist = (mz_uint16)(lookahead_pos - next_probe_pos)) > max_dist)) \
1378 return; \
1379 probe_pos = next_probe_pos & TDEFL_LZ_DICT_SIZE_MASK; \
1380 if ((d->m_dict[probe_pos + match_len] == c0) && (d->m_dict[probe_pos + match_len - 1] == c1)) \
1381 break;
1385 }
1386 if (!dist)
1387 break;
1388 p = s;
1389 q = d->m_dict + probe_pos;
1390 for (probe_len = 0; probe_len < max_match_len; probe_len++)
1391 if (*p++ != *q++)
1392 break;
1393 if (probe_len > match_len) {
1394 *pMatch_dist = dist;
1395 if ((*pMatch_len = match_len = probe_len) == max_match_len)
1396 return;
1397 c0 = d->m_dict[pos + match_len];
1398 c1 = d->m_dict[pos + match_len - 1];
1399 }
1400 }
1401}
1402#endif /* #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES */
1403
1404#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN
1405#ifdef MINIZ_UNALIGNED_USE_MEMCPY
1406static mz_uint32 TDEFL_READ_UNALIGNED_WORD32(const mz_uint8* p)
1407{
1408 mz_uint32 ret;
1409 memcpy(&ret, p, sizeof(mz_uint32));
1410 return ret;
1411}
1412#else
1413#define TDEFL_READ_UNALIGNED_WORD32(p) *(const mz_uint32*)(p)
1414#endif
1415static mz_bool tdefl_compress_fast(tdefl_compressor* d)
1416{
1417 /* Faster, minimally featured LZRW1-style match+parse loop with better register utilization. Intended for applications where raw throughput is valued more highly than ratio. */
1418 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;
1419 mz_uint8 *pLZ_code_buf = d->m_pLZ_code_buf, *pLZ_flags = d->m_pLZ_flags;
1420 mz_uint cur_pos = lookahead_pos & TDEFL_LZ_DICT_SIZE_MASK;
1421
1422 while ((d->m_src_buf_left) || ((d->m_flush) && (lookahead_size))) {
1423 const mz_uint TDEFL_COMP_FAST_LOOKAHEAD_SIZE = 4096;
1424 mz_uint dst_pos = (lookahead_pos + lookahead_size) & TDEFL_LZ_DICT_SIZE_MASK;
1425 mz_uint num_bytes_to_process = (mz_uint)MZ_MIN(d->m_src_buf_left, TDEFL_COMP_FAST_LOOKAHEAD_SIZE - lookahead_size);
1426 d->m_src_buf_left -= num_bytes_to_process;
1427 lookahead_size += num_bytes_to_process;
1428
1429 while (num_bytes_to_process) {
1430 mz_uint32 n = MZ_MIN(TDEFL_LZ_DICT_SIZE - dst_pos, num_bytes_to_process);
1431 memcpy(d->m_dict + dst_pos, d->m_pSrc, n);
1432 if (dst_pos < (TDEFL_MAX_MATCH_LEN - 1))
1433 memcpy(d->m_dict + TDEFL_LZ_DICT_SIZE + dst_pos, d->m_pSrc, MZ_MIN(n, (TDEFL_MAX_MATCH_LEN - 1) - dst_pos));
1434 d->m_pSrc += n;
1435 dst_pos = (dst_pos + n) & TDEFL_LZ_DICT_SIZE_MASK;
1436 num_bytes_to_process -= n;
1437 }
1438
1439 dict_size = MZ_MIN(TDEFL_LZ_DICT_SIZE - lookahead_size, dict_size);
1440 if ((!d->m_flush) && (lookahead_size < TDEFL_COMP_FAST_LOOKAHEAD_SIZE))
1441 break;
1442
1443 while (lookahead_size >= 4) {
1444 mz_uint cur_match_dist, cur_match_len = 1;
1445 mz_uint8* pCur_dict = d->m_dict + cur_pos;
1446 mz_uint first_trigram = TDEFL_READ_UNALIGNED_WORD32(pCur_dict) & 0xFFFFFF;
1447 mz_uint hash = (first_trigram ^ (first_trigram >> (24 - (TDEFL_LZ_HASH_BITS - 8)))) & TDEFL_LEVEL1_HASH_SIZE_MASK;
1448 mz_uint probe_pos = d->m_hash[hash];
1449 d->m_hash[hash] = (mz_uint16)lookahead_pos;
1450
1451 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)) {
1452 const mz_uint16* p = (const mz_uint16*)pCur_dict;
1453 const mz_uint16* q = (const mz_uint16*)(d->m_dict + probe_pos);
1454 mz_uint32 probe_len = 32;
1455 do {
1456 } while ((TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) && (TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) && (TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) && (TDEFL_READ_UNALIGNED_WORD2(++p) == TDEFL_READ_UNALIGNED_WORD2(++q)) && (--probe_len > 0));
1457 cur_match_len = ((mz_uint)(p - (const mz_uint16*)pCur_dict) * 2) + (mz_uint)(*(const mz_uint8*)p == *(const mz_uint8*)q);
1458 if (!probe_len)
1459 cur_match_len = cur_match_dist ? TDEFL_MAX_MATCH_LEN : 0;
1460
1461 if ((cur_match_len < TDEFL_MIN_MATCH_LEN) || ((cur_match_len == TDEFL_MIN_MATCH_LEN) && (cur_match_dist >= 8U * 1024U))) {
1462 cur_match_len = 1;
1463 *pLZ_code_buf++ = (mz_uint8)first_trigram;
1464 *pLZ_flags = (mz_uint8)(*pLZ_flags >> 1);
1465 d->m_huff_count[0][(mz_uint8)first_trigram]++;
1466 } else {
1467 mz_uint32 s0, s1;
1468 cur_match_len = MZ_MIN(cur_match_len, lookahead_size);
1469
1470 MZ_ASSERT((cur_match_len >= TDEFL_MIN_MATCH_LEN) && (cur_match_dist >= 1) && (cur_match_dist <= TDEFL_LZ_DICT_SIZE));
1471
1472 cur_match_dist--;
1473
1474 pLZ_code_buf[0] = (mz_uint8)(cur_match_len - TDEFL_MIN_MATCH_LEN);
1475#ifdef MINIZ_UNALIGNED_USE_MEMCPY
1476 memcpy(&pLZ_code_buf[1], &cur_match_dist, sizeof(cur_match_dist));
1477#else
1478 *(mz_uint16*)(&pLZ_code_buf[1]) = (mz_uint16)cur_match_dist;
1479#endif
1480 pLZ_code_buf += 3;
1481 *pLZ_flags = (mz_uint8)((*pLZ_flags >> 1) | 0x80);
1482
1483 s0 = s_tdefl_small_dist_sym[cur_match_dist & 511];
1484 s1 = s_tdefl_large_dist_sym[cur_match_dist >> 8];
1485 d->m_huff_count[1][(cur_match_dist < 512) ? s0 : s1]++;
1486
1487 d->m_huff_count[0][s_tdefl_len_sym[cur_match_len - TDEFL_MIN_MATCH_LEN]]++;
1488 }
1489 } else {
1490 *pLZ_code_buf++ = (mz_uint8)first_trigram;
1491 *pLZ_flags = (mz_uint8)(*pLZ_flags >> 1);
1492 d->m_huff_count[0][(mz_uint8)first_trigram]++;
1493 }
1494
1495 if (--num_flags_left == 0) {
1496 num_flags_left = 8;
1497 pLZ_flags = pLZ_code_buf++;
1498 }
1499
1500 total_lz_bytes += cur_match_len;
1501 lookahead_pos += cur_match_len;
1502 dict_size = MZ_MIN(dict_size + cur_match_len, (mz_uint)TDEFL_LZ_DICT_SIZE);
1503 cur_pos = (cur_pos + cur_match_len) & TDEFL_LZ_DICT_SIZE_MASK;
1504 MZ_ASSERT(lookahead_size >= cur_match_len);
1505 lookahead_size -= cur_match_len;
1506
1507 if (pLZ_code_buf > &d->m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE - 8]) {
1508 int n;
1509 d->m_lookahead_pos = lookahead_pos;
1510 d->m_lookahead_size = lookahead_size;
1511 d->m_dict_size = dict_size;
1512 d->m_total_lz_bytes = total_lz_bytes;
1513 d->m_pLZ_code_buf = pLZ_code_buf;
1514 d->m_pLZ_flags = pLZ_flags;
1515 d->m_num_flags_left = num_flags_left;
1516 if ((n = tdefl_flush_block(d, 0)) != 0)
1517 return (n < 0) ? MZ_FALSE : MZ_TRUE;
1518 total_lz_bytes = d->m_total_lz_bytes;
1519 pLZ_code_buf = d->m_pLZ_code_buf;
1520 pLZ_flags = d->m_pLZ_flags;
1521 num_flags_left = d->m_num_flags_left;
1522 }
1523 }
1524
1525 while (lookahead_size) {
1526 mz_uint8 lit = d->m_dict[cur_pos];
1527
1528 total_lz_bytes++;
1529 *pLZ_code_buf++ = lit;
1530 *pLZ_flags = (mz_uint8)(*pLZ_flags >> 1);
1531 if (--num_flags_left == 0) {
1532 num_flags_left = 8;
1533 pLZ_flags = pLZ_code_buf++;
1534 }
1535
1536 d->m_huff_count[0][lit]++;
1537
1538 lookahead_pos++;
1539 dict_size = MZ_MIN(dict_size + 1, (mz_uint)TDEFL_LZ_DICT_SIZE);
1540 cur_pos = (cur_pos + 1) & TDEFL_LZ_DICT_SIZE_MASK;
1541 lookahead_size--;
1542
1543 if (pLZ_code_buf > &d->m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE - 8]) {
1544 int n;
1545 d->m_lookahead_pos = lookahead_pos;
1546 d->m_lookahead_size = lookahead_size;
1547 d->m_dict_size = dict_size;
1548 d->m_total_lz_bytes = total_lz_bytes;
1549 d->m_pLZ_code_buf = pLZ_code_buf;
1550 d->m_pLZ_flags = pLZ_flags;
1551 d->m_num_flags_left = num_flags_left;
1552 if ((n = tdefl_flush_block(d, 0)) != 0)
1553 return (n < 0) ? MZ_FALSE : MZ_TRUE;
1554 total_lz_bytes = d->m_total_lz_bytes;
1555 pLZ_code_buf = d->m_pLZ_code_buf;
1556 pLZ_flags = d->m_pLZ_flags;
1557 num_flags_left = d->m_num_flags_left;
1558 }
1559 }
1560 }
1561
1562 d->m_lookahead_pos = lookahead_pos;
1563 d->m_lookahead_size = lookahead_size;
1564 d->m_dict_size = dict_size;
1565 d->m_total_lz_bytes = total_lz_bytes;
1566 d->m_pLZ_code_buf = pLZ_code_buf;
1567 d->m_pLZ_flags = pLZ_flags;
1568 d->m_num_flags_left = num_flags_left;
1569 return MZ_TRUE;
1570}
1571#endif /* MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN */
1572
1573static MZ_FORCEINLINE void tdefl_record_literal(tdefl_compressor* d, mz_uint8 lit)
1574{
1575 d->m_total_lz_bytes++;
1576 *d->m_pLZ_code_buf++ = lit;
1577 *d->m_pLZ_flags = (mz_uint8)(*d->m_pLZ_flags >> 1);
1578 if (--d->m_num_flags_left == 0) {
1579 d->m_num_flags_left = 8;
1580 d->m_pLZ_flags = d->m_pLZ_code_buf++;
1581 }
1582 d->m_huff_count[0][lit]++;
1583}
1584
1585static MZ_FORCEINLINE void tdefl_record_match(tdefl_compressor* d, mz_uint match_len, mz_uint match_dist)
1586{
1587 mz_uint32 s0, s1;
1588
1589 MZ_ASSERT((match_len >= TDEFL_MIN_MATCH_LEN) && (match_dist >= 1) && (match_dist <= TDEFL_LZ_DICT_SIZE));
1590
1591 d->m_total_lz_bytes += match_len;
1592
1593 d->m_pLZ_code_buf[0] = (mz_uint8)(match_len - TDEFL_MIN_MATCH_LEN);
1594
1595 match_dist -= 1;
1596 d->m_pLZ_code_buf[1] = (mz_uint8)(match_dist & 0xFF);
1597 d->m_pLZ_code_buf[2] = (mz_uint8)(match_dist >> 8);
1598 d->m_pLZ_code_buf += 3;
1599
1600 *d->m_pLZ_flags = (mz_uint8)((*d->m_pLZ_flags >> 1) | 0x80);
1601 if (--d->m_num_flags_left == 0) {
1602 d->m_num_flags_left = 8;
1603 d->m_pLZ_flags = d->m_pLZ_code_buf++;
1604 }
1605
1606 s0 = s_tdefl_small_dist_sym[match_dist & 511];
1607 s1 = s_tdefl_large_dist_sym[(match_dist >> 8) & 127];
1608 d->m_huff_count[1][(match_dist < 512) ? s0 : s1]++;
1609 d->m_huff_count[0][s_tdefl_len_sym[match_len - TDEFL_MIN_MATCH_LEN]]++;
1610}
1611
1612static mz_bool tdefl_compress_normal(tdefl_compressor* d)
1613{
1614 const mz_uint8* pSrc = d->m_pSrc;
1615 size_t src_buf_left = d->m_src_buf_left;
1616 tdefl_flush flush = d->m_flush;
1617
1618 while ((src_buf_left) || ((flush) && (d->m_lookahead_size))) {
1619 mz_uint len_to_move, cur_match_dist, cur_match_len, cur_pos;
1620 /* Update dictionary and hash chains. Keeps the lookahead size equal to TDEFL_MAX_MATCH_LEN. */
1621 if ((d->m_lookahead_size + d->m_dict_size) >= (TDEFL_MIN_MATCH_LEN - 1)) {
1622 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;
1623 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];
1624 mz_uint num_bytes_to_process = (mz_uint)MZ_MIN(src_buf_left, TDEFL_MAX_MATCH_LEN - d->m_lookahead_size);
1625 const mz_uint8* pSrc_end = pSrc ? pSrc + num_bytes_to_process : NULL;
1626 src_buf_left -= num_bytes_to_process;
1627 d->m_lookahead_size += num_bytes_to_process;
1628 while (pSrc != pSrc_end) {
1629 mz_uint8 c = *pSrc++;
1630 d->m_dict[dst_pos] = c;
1631 if (dst_pos < (TDEFL_MAX_MATCH_LEN - 1))
1632 d->m_dict[TDEFL_LZ_DICT_SIZE + dst_pos] = c;
1633 hash = ((hash << TDEFL_LZ_HASH_SHIFT) ^ c) & (TDEFL_LZ_HASH_SIZE - 1);
1634 d->m_next[ins_pos & TDEFL_LZ_DICT_SIZE_MASK] = d->m_hash[hash];
1635 d->m_hash[hash] = (mz_uint16)(ins_pos);
1636 dst_pos = (dst_pos + 1) & TDEFL_LZ_DICT_SIZE_MASK;
1637 ins_pos++;
1638 }
1639 } else {
1640 while ((src_buf_left) && (d->m_lookahead_size < TDEFL_MAX_MATCH_LEN)) {
1641 mz_uint8 c = *pSrc++;
1642 mz_uint dst_pos = (d->m_lookahead_pos + d->m_lookahead_size) & TDEFL_LZ_DICT_SIZE_MASK;
1643 src_buf_left--;
1644 d->m_dict[dst_pos] = c;
1645 if (dst_pos < (TDEFL_MAX_MATCH_LEN - 1))
1646 d->m_dict[TDEFL_LZ_DICT_SIZE + dst_pos] = c;
1647 if ((++d->m_lookahead_size + d->m_dict_size) >= TDEFL_MIN_MATCH_LEN) {
1648 mz_uint ins_pos = d->m_lookahead_pos + (d->m_lookahead_size - 1) - 2;
1649 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);
1650 d->m_next[ins_pos & TDEFL_LZ_DICT_SIZE_MASK] = d->m_hash[hash];
1651 d->m_hash[hash] = (mz_uint16)(ins_pos);
1652 }
1653 }
1654 }
1655 d->m_dict_size = MZ_MIN(TDEFL_LZ_DICT_SIZE - d->m_lookahead_size, d->m_dict_size);
1656 if ((!flush) && (d->m_lookahead_size < TDEFL_MAX_MATCH_LEN))
1657 break;
1658
1659 /* Simple lazy/greedy parsing state machine. */
1660 len_to_move = 1;
1661 cur_match_dist = 0;
1662 cur_match_len = d->m_saved_match_len ? d->m_saved_match_len : (TDEFL_MIN_MATCH_LEN - 1);
1663 cur_pos = d->m_lookahead_pos & TDEFL_LZ_DICT_SIZE_MASK;
1664 if (d->m_flags & (TDEFL_RLE_MATCHES | TDEFL_FORCE_ALL_RAW_BLOCKS)) {
1665 if ((d->m_dict_size) && (!(d->m_flags & TDEFL_FORCE_ALL_RAW_BLOCKS))) {
1666 mz_uint8 c = d->m_dict[(cur_pos - 1) & TDEFL_LZ_DICT_SIZE_MASK];
1667 cur_match_len = 0;
1668 while (cur_match_len < d->m_lookahead_size) {
1669 if (d->m_dict[cur_pos + cur_match_len] != c)
1670 break;
1671 cur_match_len++;
1672 }
1673 if (cur_match_len < TDEFL_MIN_MATCH_LEN)
1674 cur_match_len = 0;
1675 else
1676 cur_match_dist = 1;
1677 }
1678 } else {
1679 tdefl_find_match(d, d->m_lookahead_pos, d->m_dict_size, d->m_lookahead_size, &cur_match_dist, &cur_match_len);
1680 }
1681 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))) {
1682 cur_match_dist = cur_match_len = 0;
1683 }
1684 if (d->m_saved_match_len) {
1685 if (cur_match_len > d->m_saved_match_len) {
1686 tdefl_record_literal(d, (mz_uint8)d->m_saved_lit);
1687 if (cur_match_len >= 128) {
1688 tdefl_record_match(d, cur_match_len, cur_match_dist);
1689 d->m_saved_match_len = 0;
1690 len_to_move = cur_match_len;
1691 } else {
1692 d->m_saved_lit = d->m_dict[cur_pos];
1693 d->m_saved_match_dist = cur_match_dist;
1694 d->m_saved_match_len = cur_match_len;
1695 }
1696 } else {
1697 tdefl_record_match(d, d->m_saved_match_len, d->m_saved_match_dist);
1698 len_to_move = d->m_saved_match_len - 1;
1699 d->m_saved_match_len = 0;
1700 }
1701 } else if (!cur_match_dist)
1702 tdefl_record_literal(d, d->m_dict[MZ_MIN(cur_pos, sizeof(d->m_dict) - 1)]);
1703 else if ((d->m_greedy_parsing) || (d->m_flags & TDEFL_RLE_MATCHES) || (cur_match_len >= 128)) {
1704 tdefl_record_match(d, cur_match_len, cur_match_dist);
1705 len_to_move = cur_match_len;
1706 } else {
1707 d->m_saved_lit = d->m_dict[MZ_MIN(cur_pos, sizeof(d->m_dict) - 1)];
1708 d->m_saved_match_dist = cur_match_dist;
1709 d->m_saved_match_len = cur_match_len;
1710 }
1711 /* Move the lookahead forward by len_to_move bytes. */
1712 d->m_lookahead_pos += len_to_move;
1713 MZ_ASSERT(d->m_lookahead_size >= len_to_move);
1714 d->m_lookahead_size -= len_to_move;
1715 d->m_dict_size = MZ_MIN(d->m_dict_size + len_to_move, (mz_uint)TDEFL_LZ_DICT_SIZE);
1716 /* Check if it's time to flush the current LZ codes to the internal output buffer. */
1717 if ((d->m_pLZ_code_buf > &d->m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE - 8]) || ((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)))) {
1718 int n;
1719 d->m_pSrc = pSrc;
1720 d->m_src_buf_left = src_buf_left;
1721 if ((n = tdefl_flush_block(d, 0)) != 0)
1722 return (n < 0) ? MZ_FALSE : MZ_TRUE;
1723 }
1724 }
1725
1726 d->m_pSrc = pSrc;
1727 d->m_src_buf_left = src_buf_left;
1728 return MZ_TRUE;
1729}
1730
1731static tdefl_status tdefl_flush_output_buffer(tdefl_compressor* d)
1732{
1733 if (d->m_pIn_buf_size) {
1734 *d->m_pIn_buf_size = d->m_pSrc - (const mz_uint8*)d->m_pIn_buf;
1735 }
1736
1737 if (d->m_pOut_buf_size) {
1738 size_t n = MZ_MIN(*d->m_pOut_buf_size - d->m_out_buf_ofs, d->m_output_flush_remaining);
1739 memcpy((mz_uint8*)d->m_pOut_buf + d->m_out_buf_ofs, d->m_output_buf + d->m_output_flush_ofs, n);
1740 d->m_output_flush_ofs += (mz_uint)n;
1741 d->m_output_flush_remaining -= (mz_uint)n;
1742 d->m_out_buf_ofs += n;
1743
1744 *d->m_pOut_buf_size = d->m_out_buf_ofs;
1745 }
1746
1747 return (d->m_finished && !d->m_output_flush_remaining) ? TDEFL_STATUS_DONE : TDEFL_STATUS_OKAY;
1748}
1749
1750tdefl_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)
1751{
1752 if (!d) {
1753 if (pIn_buf_size)
1754 *pIn_buf_size = 0;
1755 if (pOut_buf_size)
1756 *pOut_buf_size = 0;
1757 return TDEFL_STATUS_BAD_PARAM;
1758 }
1759
1760 d->m_pIn_buf = pIn_buf;
1761 d->m_pIn_buf_size = pIn_buf_size;
1762 d->m_pOut_buf = pOut_buf;
1763 d->m_pOut_buf_size = pOut_buf_size;
1764 d->m_pSrc = (const mz_uint8*)(pIn_buf);
1765 d->m_src_buf_left = pIn_buf_size ? *pIn_buf_size : 0;
1766 d->m_out_buf_ofs = 0;
1767 d->m_flush = flush;
1768
1769 if (((d->m_pPut_buf_func != NULL) == ((pOut_buf != NULL) || (pOut_buf_size != NULL))) || (d->m_prev_return_status != TDEFL_STATUS_OKAY) || (d->m_wants_to_finish && (flush != TDEFL_FINISH)) || (pIn_buf_size && *pIn_buf_size && !pIn_buf) || (pOut_buf_size && *pOut_buf_size && !pOut_buf)) {
1770 if (pIn_buf_size)
1771 *pIn_buf_size = 0;
1772 if (pOut_buf_size)
1773 *pOut_buf_size = 0;
1774 return (d->m_prev_return_status = TDEFL_STATUS_BAD_PARAM);
1775 }
1776 d->m_wants_to_finish |= (flush == TDEFL_FINISH);
1777
1778 if ((d->m_output_flush_remaining) || (d->m_finished))
1779 return (d->m_prev_return_status = tdefl_flush_output_buffer(d));
1780
1781#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN
1782 if (((d->m_flags & TDEFL_MAX_PROBES_MASK) == 1) && ((d->m_flags & TDEFL_GREEDY_PARSING_FLAG) != 0) && ((d->m_flags & (TDEFL_FILTER_MATCHES | TDEFL_FORCE_ALL_RAW_BLOCKS | TDEFL_RLE_MATCHES)) == 0)) {
1783 if (!tdefl_compress_fast(d))
1784 return d->m_prev_return_status;
1785 } else
1786#endif /* #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN */
1787 {
1788 if (!tdefl_compress_normal(d))
1789 return d->m_prev_return_status;
1790 }
1791
1792 if ((d->m_flags & (TDEFL_WRITE_ZLIB_HEADER | TDEFL_COMPUTE_ADLER32)) && (pIn_buf))
1793 d->m_adler32 = (mz_uint32)mz_adler32(d->m_adler32, (const mz_uint8*)pIn_buf, d->m_pSrc - (const mz_uint8*)pIn_buf);
1794
1795 if ((flush) && (!d->m_lookahead_size) && (!d->m_src_buf_left) && (!d->m_output_flush_remaining)) {
1796 if (tdefl_flush_block(d, flush) < 0)
1797 return d->m_prev_return_status;
1798 d->m_finished = (flush == TDEFL_FINISH);
1799 if (flush == TDEFL_FULL_FLUSH) {
1800 MZ_CLEAR_ARR(d->m_hash);
1801 MZ_CLEAR_ARR(d->m_next);
1802 d->m_dict_size = 0;
1803 }
1804 }
1805
1806 return (d->m_prev_return_status = tdefl_flush_output_buffer(d));
1807}
1808
1809tdefl_status tdefl_compress_buffer(tdefl_compressor* d, const void* pIn_buf, size_t in_buf_size, tdefl_flush flush)
1810{
1811 MZ_ASSERT(d->m_pPut_buf_func);
1812 return tdefl_compress(d, pIn_buf, &in_buf_size, NULL, NULL, flush);
1813}
1814
1815tdefl_status tdefl_init(tdefl_compressor* d, tdefl_put_buf_func_ptr pPut_buf_func, void* pPut_buf_user, int flags)
1816{
1817 d->m_pPut_buf_func = pPut_buf_func;
1818 d->m_pPut_buf_user = pPut_buf_user;
1819 d->m_flags = (mz_uint)(flags);
1820 d->m_max_probes[0] = 1 + ((flags & 0xFFF) + 2) / 3;
1821 d->m_greedy_parsing = (flags & TDEFL_GREEDY_PARSING_FLAG) != 0;
1822 d->m_max_probes[1] = 1 + (((flags & 0xFFF) >> 2) + 2) / 3;
1823 if (!(flags & TDEFL_NONDETERMINISTIC_PARSING_FLAG))
1824 MZ_CLEAR_ARR(d->m_hash);
1825 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;
1826 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;
1827 d->m_pLZ_code_buf = d->m_lz_code_buf + 1;
1828 d->m_pLZ_flags = d->m_lz_code_buf;
1829 *d->m_pLZ_flags = 0;
1830 d->m_num_flags_left = 8;
1831 d->m_pOutput_buf = d->m_output_buf;
1832 d->m_pOutput_buf_end = d->m_output_buf;
1833 d->m_prev_return_status = TDEFL_STATUS_OKAY;
1834 d->m_saved_match_dist = d->m_saved_match_len = d->m_saved_lit = 0;
1835 d->m_adler32 = 1;
1836 d->m_pIn_buf = NULL;
1837 d->m_pOut_buf = NULL;
1838 d->m_pIn_buf_size = NULL;
1839 d->m_pOut_buf_size = NULL;
1840 d->m_flush = TDEFL_NO_FLUSH;
1841 d->m_pSrc = NULL;
1842 d->m_src_buf_left = 0;
1843 d->m_out_buf_ofs = 0;
1844 if (!(flags & TDEFL_NONDETERMINISTIC_PARSING_FLAG))
1845 MZ_CLEAR_ARR(d->m_dict);
1846 memset(&d->m_huff_count[0][0], 0, sizeof(d->m_huff_count[0][0]) * TDEFL_MAX_HUFF_SYMBOLS_0);
1847 memset(&d->m_huff_count[1][0], 0, sizeof(d->m_huff_count[1][0]) * TDEFL_MAX_HUFF_SYMBOLS_1);
1848 return TDEFL_STATUS_OKAY;
1849}
1850
1851tdefl_status tdefl_get_prev_return_status(tdefl_compressor* d)
1852{
1853 return d->m_prev_return_status;
1854}
1855
1856mz_uint32 tdefl_get_adler32(tdefl_compressor* d)
1857{
1858 return d->m_adler32;
1859}
1860
1861mz_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)
1862{
1863 tdefl_compressor* pComp;
1864 mz_bool succeeded;
1865 if (((buf_len) && (!pBuf)) || (!pPut_buf_func))
1866 return MZ_FALSE;
1867 pComp = (tdefl_compressor*)MZ_MALLOC(sizeof(tdefl_compressor));
1868 if (!pComp)
1869 return MZ_FALSE;
1870 succeeded = (tdefl_init(pComp, pPut_buf_func, pPut_buf_user, flags) == TDEFL_STATUS_OKAY);
1871 succeeded = succeeded && (tdefl_compress_buffer(pComp, pBuf, buf_len, TDEFL_FINISH) == TDEFL_STATUS_DONE);
1872 MZ_FREE(pComp);
1873 return succeeded;
1874}
1875
1876typedef struct
1877{
1879 mz_uint8* m_pBuf;
1882
1883static mz_bool tdefl_output_buffer_putter(const void* pBuf, int len, void* pUser)
1884{
1886 size_t new_size = p->m_size + len;
1887 if (new_size > p->m_capacity) {
1888 size_t new_capacity = p->m_capacity;
1889 mz_uint8* pNew_buf;
1890 if (!p->m_expandable)
1891 return MZ_FALSE;
1892 do {
1893 new_capacity = MZ_MAX(128U, new_capacity << 1U);
1894 } while (new_size > new_capacity);
1895 pNew_buf = (mz_uint8*)MZ_REALLOC(p->m_pBuf, new_capacity);
1896 if (!pNew_buf)
1897 return MZ_FALSE;
1898 p->m_pBuf = pNew_buf;
1899 p->m_capacity = new_capacity;
1900 }
1901 memcpy((mz_uint8*)p->m_pBuf + p->m_size, pBuf, len);
1902 p->m_size = new_size;
1903 return MZ_TRUE;
1904}
1905
1906void* tdefl_compress_mem_to_heap(const void* pSrc_buf, size_t src_buf_len, size_t* pOut_len, int flags)
1907{
1908 tdefl_output_buffer out_buf;
1909 MZ_CLEAR_OBJ(out_buf);
1910 if (!pOut_len)
1911 return MZ_FALSE;
1912 else
1913 *pOut_len = 0;
1914 out_buf.m_expandable = MZ_TRUE;
1915 if (!tdefl_compress_mem_to_output(pSrc_buf, src_buf_len, tdefl_output_buffer_putter, &out_buf, flags))
1916 return NULL;
1917 *pOut_len = out_buf.m_size;
1918 return out_buf.m_pBuf;
1919}
1920
1921size_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)
1922{
1923 tdefl_output_buffer out_buf;
1924 MZ_CLEAR_OBJ(out_buf);
1925 if (!pOut_buf)
1926 return 0;
1927 out_buf.m_pBuf = (mz_uint8*)pOut_buf;
1928 out_buf.m_capacity = out_buf_len;
1929 if (!tdefl_compress_mem_to_output(pSrc_buf, src_buf_len, tdefl_output_buffer_putter, &out_buf, flags))
1930 return 0;
1931 return out_buf.m_size;
1932}
1933
1934static const mz_uint s_tdefl_num_probes[11] = { 0, 1, 6, 32, 16, 32, 128, 256, 512, 768, 1500 };
1935
1936/* 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). */
1937mz_uint tdefl_create_comp_flags_from_zip_params(int level, int window_bits, int strategy)
1938{
1939 mz_uint comp_flags = s_tdefl_num_probes[(level >= 0) ? MZ_MIN(10, level) : MZ_DEFAULT_LEVEL] | ((level <= 3) ? TDEFL_GREEDY_PARSING_FLAG : 0);
1940 if (window_bits > 0)
1941 comp_flags |= TDEFL_WRITE_ZLIB_HEADER;
1942
1943 if (!level)
1944 comp_flags |= TDEFL_FORCE_ALL_RAW_BLOCKS;
1945 else if (strategy == MZ_FILTERED)
1946 comp_flags |= TDEFL_FILTER_MATCHES;
1947 else if (strategy == MZ_HUFFMAN_ONLY)
1948 comp_flags &= ~TDEFL_MAX_PROBES_MASK;
1949 else if (strategy == MZ_FIXED)
1950 comp_flags |= TDEFL_FORCE_ALL_STATIC_BLOCKS;
1951 else if (strategy == MZ_RLE)
1952 comp_flags |= TDEFL_RLE_MATCHES;
1953
1954 return comp_flags;
1955}
1956
1957#ifdef _MSC_VER
1958#pragma warning(push)
1959#pragma warning(disable : 4204) /* nonstandard extension used : non-constant aggregate initializer (also supported by GNU C and C99, so no big deal) */
1960#endif
1961
1962/* Simple PNG writer function by Alex Evans, 2011. Released into the public domain: https://gist.github.com/908299, more context at
1963 http://altdevblogaday.org/2011/04/06/a-smaller-jpg-encoder/.
1964 This is actually a modification of Alex's original code so PNG files generated by this function pass pngcheck. */
1965void* 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)
1966{
1967 /* Using a local copy of this array here in case MINIZ_NO_ZLIB_APIS was defined. */
1968 static const mz_uint s_tdefl_png_num_probes[11] = { 0, 1, 6, 32, 16, 32, 128, 256, 512, 768, 1500 };
1969 tdefl_compressor* pComp = (tdefl_compressor*)MZ_MALLOC(sizeof(tdefl_compressor));
1970 tdefl_output_buffer out_buf;
1971 int i, bpl = w * num_chans, y, z;
1972 mz_uint32 c;
1973 *pLen_out = 0;
1974 if (!pComp)
1975 return NULL;
1976 MZ_CLEAR_OBJ(out_buf);
1977 out_buf.m_expandable = MZ_TRUE;
1978 out_buf.m_capacity = 57 + MZ_MAX(64, (1 + bpl) * h);
1979 if (NULL == (out_buf.m_pBuf = (mz_uint8*)MZ_MALLOC(out_buf.m_capacity))) {
1980 MZ_FREE(pComp);
1981 return NULL;
1982 }
1983 /* write dummy header */
1984 for (z = 41; z; --z)
1985 tdefl_output_buffer_putter(&z, 1, &out_buf);
1986 /* compress image data */
1987 tdefl_init(pComp, tdefl_output_buffer_putter, &out_buf, s_tdefl_png_num_probes[MZ_MIN(10, level)] | TDEFL_WRITE_ZLIB_HEADER);
1988 for (y = 0; y < h; ++y) {
1989 tdefl_compress_buffer(pComp, &z, 1, TDEFL_NO_FLUSH);
1990 tdefl_compress_buffer(pComp, (mz_uint8*)pImage + (flip ? (h - 1 - y) : y) * bpl, bpl, TDEFL_NO_FLUSH);
1991 }
1992 if (tdefl_compress_buffer(pComp, NULL, 0, TDEFL_FINISH) != TDEFL_STATUS_DONE) {
1993 MZ_FREE(pComp);
1994 MZ_FREE(out_buf.m_pBuf);
1995 return NULL;
1996 }
1997 /* write real header */
1998 *pLen_out = out_buf.m_size - 41;
1999 {
2000 static const mz_uint8 chans[] = { 0x00, 0x00, 0x04, 0x02, 0x06 };
2001 mz_uint8 pnghdr[41] = { 0x89, 0x50, 0x4e, 0x47, 0x0d,
2002 0x0a, 0x1a, 0x0a, 0x00, 0x00,
2003 0x00, 0x0d, 0x49, 0x48, 0x44,
2004 0x52, 0x00, 0x00, 0x00, 0x00,
2005 0x00, 0x00, 0x00, 0x00, 0x08,
2006 0x00, 0x00, 0x00, 0x00, 0x00,
2007 0x00, 0x00, 0x00, 0x00, 0x00,
2008 0x00, 0x00, 0x49, 0x44, 0x41,
2009 0x54 };
2010 pnghdr[18] = (mz_uint8)(w >> 8);
2011 pnghdr[19] = (mz_uint8)w;
2012 pnghdr[22] = (mz_uint8)(h >> 8);
2013 pnghdr[23] = (mz_uint8)h;
2014 pnghdr[25] = chans[num_chans];
2015 pnghdr[33] = (mz_uint8)(*pLen_out >> 24);
2016 pnghdr[34] = (mz_uint8)(*pLen_out >> 16);
2017 pnghdr[35] = (mz_uint8)(*pLen_out >> 8);
2018 pnghdr[36] = (mz_uint8)*pLen_out;
2019 c = (mz_uint32)mz_crc32(MZ_CRC32_INIT, pnghdr + 12, 17);
2020 for (i = 0; i < 4; ++i, c <<= 8)
2021 ((mz_uint8*)(pnghdr + 29))[i] = (mz_uint8)(c >> 24);
2022 memcpy(out_buf.m_pBuf, pnghdr, 41);
2023 }
2024 /* write footer (IDAT CRC-32, followed by IEND chunk) */
2025 if (!tdefl_output_buffer_putter("\0\0\0\0\0\0\0\0\x49\x45\x4e\x44\xae\x42\x60\x82", 16, &out_buf)) {
2026 *pLen_out = 0;
2027 MZ_FREE(pComp);
2028 MZ_FREE(out_buf.m_pBuf);
2029 return NULL;
2030 }
2031 c = (mz_uint32)mz_crc32(MZ_CRC32_INIT, out_buf.m_pBuf + 41 - 4, *pLen_out + 4);
2032 for (i = 0; i < 4; ++i, c <<= 8)
2033 (out_buf.m_pBuf + out_buf.m_size - 16)[i] = (mz_uint8)(c >> 24);
2034 /* compute final size of file, grab compressed data buffer and return */
2035 *pLen_out += 57;
2036 MZ_FREE(pComp);
2037 return out_buf.m_pBuf;
2038}
2039void* tdefl_write_image_to_png_file_in_memory(const void* pImage, int w, int h, int num_chans, size_t* pLen_out)
2040{
2041 /* 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) */
2042 return tdefl_write_image_to_png_file_in_memory_ex(pImage, w, h, num_chans, pLen_out, 6, MZ_FALSE);
2043}
2044
2045#ifndef MINIZ_NO_MALLOC
2046/* Allocate the tdefl_compressor and tinfl_decompressor structures in C so that */
2047/* non-C language bindings to tdefL_ and tinfl_ API don't need to worry about */
2048/* structure size and allocation mechanism. */
2049tdefl_compressor* tdefl_compressor_alloc(void)
2050{
2051 return (tdefl_compressor*)MZ_MALLOC(sizeof(tdefl_compressor));
2052}
2053
2054void tdefl_compressor_free(tdefl_compressor* pComp)
2055{
2056 MZ_FREE(pComp);
2057}
2058#endif
2059
2060#ifdef _MSC_VER
2061#pragma warning(pop)
2062#endif
2063
2064#ifdef __cplusplus
2065}
2066#endif
2067
2068#endif /*#ifndef MINIZ_NO_DEFLATE_APIS*/
2069/**************************************************************************
2070 *
2071 * Copyright 2013-2014 RAD Game Tools and Valve Software
2072 * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
2073 * All Rights Reserved.
2074 *
2075 * Permission is hereby granted, free of charge, to any person obtaining a copy
2076 * of this software and associated documentation files (the "Software"), to deal
2077 * in the Software without restriction, including without limitation the rights
2078 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
2079 * copies of the Software, and to permit persons to whom the Software is
2080 * furnished to do so, subject to the following conditions:
2081 *
2082 * The above copyright notice and this permission notice shall be included in
2083 * all copies or substantial portions of the Software.
2084 *
2085 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2086 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2087 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
2088 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2089 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2090 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2091 * THE SOFTWARE.
2092 *
2093 **************************************************************************/
2094
2095#ifndef MINIZ_NO_INFLATE_APIS
2096
2097#ifdef __cplusplus
2098extern "C" {
2099#endif
2100
2101/* ------------------- Low-level Decompression (completely independent from all compression API's) */
2102
2103#define TINFL_MEMCPY(d, s, l) memcpy(d, s, l)
2104#define TINFL_MEMSET(p, c, l) memset(p, c, l)
2105
2106#define TINFL_CR_BEGIN \
2107 switch (r->m_state) { \
2108 case 0:
2109#define TINFL_CR_RETURN(state_index, result) \
2110 do { \
2111 status = result; \
2112 r->m_state = state_index; \
2113 goto common_exit; \
2114 case state_index:; \
2115 } \
2116 MZ_MACRO_END
2117#define TINFL_CR_RETURN_FOREVER(state_index, result) \
2118 do { \
2119 for (;;) { \
2120 TINFL_CR_RETURN(state_index, result); \
2121 } \
2122 } \
2123 MZ_MACRO_END
2124#define TINFL_CR_FINISH }
2125
2126#define TINFL_GET_BYTE(state_index, c) \
2127 do { \
2128 while (pIn_buf_cur >= pIn_buf_end) { \
2129 TINFL_CR_RETURN(state_index, (decomp_flags & TINFL_FLAG_HAS_MORE_INPUT) ? TINFL_STATUS_NEEDS_MORE_INPUT : TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS); \
2130 } \
2131 c = *pIn_buf_cur++; \
2132 } \
2133 MZ_MACRO_END
2134
2135#define TINFL_NEED_BITS(state_index, n) \
2136 do { \
2137 mz_uint c; \
2138 TINFL_GET_BYTE(state_index, c); \
2139 bit_buf |= (((tinfl_bit_buf_t)c) << num_bits); \
2140 num_bits += 8; \
2141 } while (num_bits < (mz_uint)(n))
2142#define TINFL_SKIP_BITS(state_index, n) \
2143 do { \
2144 if (num_bits < (mz_uint)(n)) { \
2145 TINFL_NEED_BITS(state_index, n); \
2146 } \
2147 bit_buf >>= (n); \
2148 num_bits -= (n); \
2149 } \
2150 MZ_MACRO_END
2151#define TINFL_GET_BITS(state_index, b, n) \
2152 do { \
2153 if (num_bits < (mz_uint)(n)) { \
2154 TINFL_NEED_BITS(state_index, n); \
2155 } \
2156 b = bit_buf & ((1 << (n)) - 1); \
2157 bit_buf >>= (n); \
2158 num_bits -= (n); \
2159 } \
2160 MZ_MACRO_END
2161
2162/* TINFL_HUFF_BITBUF_FILL() is only used rarely, when the number of bytes remaining in the input buffer falls below 2. */
2163/* 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 */
2164/* 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 */
2165/* bit buffer contains >=15 bits (deflate's max. Huffman code size). */
2166#define TINFL_HUFF_BITBUF_FILL(state_index, pLookUp, pTree) \
2167 do { \
2168 temp = pLookUp[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]; \
2169 if (temp >= 0) { \
2170 code_len = temp >> 9; \
2171 if ((code_len) && (num_bits >= code_len)) \
2172 break; \
2173 } else if (num_bits > TINFL_FAST_LOOKUP_BITS) { \
2174 code_len = TINFL_FAST_LOOKUP_BITS; \
2175 do { \
2176 temp = pTree[~temp + ((bit_buf >> code_len++) & 1)]; \
2177 } while ((temp < 0) && (num_bits >= (code_len + 1))); \
2178 if (temp >= 0) \
2179 break; \
2180 } \
2181 TINFL_GET_BYTE(state_index, c); \
2182 bit_buf |= (((tinfl_bit_buf_t)c) << num_bits); \
2183 num_bits += 8; \
2184 } while (num_bits < 15);
2185
2186/* 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 */
2187/* 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 */
2188/* 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. */
2189/* The slow path is only executed at the very end of the input buffer. */
2190/* 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 */
2191/* following the deflate data and our non-conservative read-ahead path won't kick in here on this code. This is much trickier. */
2192#define TINFL_HUFF_DECODE(state_index, sym, pLookUp, pTree) \
2193 do { \
2194 int temp; \
2195 mz_uint code_len, c; \
2196 if (num_bits < 15) { \
2197 if ((pIn_buf_end - pIn_buf_cur) < 2) { \
2198 TINFL_HUFF_BITBUF_FILL(state_index, pLookUp, pTree); \
2199 } else { \
2200 bit_buf |= (((tinfl_bit_buf_t)pIn_buf_cur[0]) << num_bits) | (((tinfl_bit_buf_t)pIn_buf_cur[1]) << (num_bits + 8)); \
2201 pIn_buf_cur += 2; \
2202 num_bits += 16; \
2203 } \
2204 } \
2205 if ((temp = pLookUp[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0) \
2206 code_len = temp >> 9, temp &= 511; \
2207 else { \
2208 code_len = TINFL_FAST_LOOKUP_BITS; \
2209 do { \
2210 temp = pTree[~temp + ((bit_buf >> code_len++) & 1)]; \
2211 } while (temp < 0); \
2212 } \
2213 sym = temp; \
2214 bit_buf >>= code_len; \
2215 num_bits -= code_len; \
2216 } \
2217 MZ_MACRO_END
2218
2219static void tinfl_clear_tree(tinfl_decompressor* r)
2220{
2221 if (r->m_type == 0)
2222 MZ_CLEAR_ARR(r->m_tree_0);
2223 else if (r->m_type == 1)
2224 MZ_CLEAR_ARR(r->m_tree_1);
2225 else
2226 MZ_CLEAR_ARR(r->m_tree_2);
2227}
2228
2229tinfl_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)
2230{
2231 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 };
2232 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 };
2233 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 };
2234 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 };
2235 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 };
2236 static const mz_uint16 s_min_table_sizes[3] = { 257, 1, 4 };
2237
2238 mz_int16* pTrees[3];
2239 mz_uint8* pCode_sizes[3];
2240
2241 tinfl_status status = TINFL_STATUS_FAILED;
2242 mz_uint32 num_bits, dist, counter, num_extra;
2243 tinfl_bit_buf_t bit_buf;
2244 const mz_uint8 *pIn_buf_cur = pIn_buf_next, *const pIn_buf_end = pIn_buf_next + *pIn_buf_size;
2245 mz_uint8 *pOut_buf_cur = pOut_buf_next, *const pOut_buf_end = pOut_buf_next ? pOut_buf_next + *pOut_buf_size : NULL;
2246 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;
2247
2248 /* 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). */
2249 if ((!pOut_buf_start) || (!pOut_buf_next) || (!pIn_buf_size) || (!pOut_buf_size)) {
2250 return TINFL_STATUS_BAD_PARAM;
2251 }
2252 if (((out_buf_size_mask + 1) & out_buf_size_mask) || (pOut_buf_next < pOut_buf_start)) {
2253 *pIn_buf_size = *pOut_buf_size = 0;
2254 return TINFL_STATUS_BAD_PARAM;
2255 }
2256
2257 pTrees[0] = r->m_tree_0;
2258 pTrees[1] = r->m_tree_1;
2259 pTrees[2] = r->m_tree_2;
2260 pCode_sizes[0] = r->m_code_size_0;
2261 pCode_sizes[1] = r->m_code_size_1;
2262 pCode_sizes[2] = r->m_code_size_2;
2263
2264 num_bits = r->m_num_bits;
2265 bit_buf = r->m_bit_buf;
2266 dist = r->m_dist;
2267 counter = r->m_counter;
2268 num_extra = r->m_num_extra;
2269 dist_from_out_buf_start = r->m_dist_from_out_buf_start;
2271
2272 bit_buf = num_bits = dist = counter = num_extra = r->m_zhdr0 = r->m_zhdr1 = 0;
2273 r->m_z_adler32 = r->m_check_adler32 = 1;
2274 if (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER) {
2275 TINFL_GET_BYTE(1, r->m_zhdr0);
2276 TINFL_GET_BYTE(2, r->m_zhdr1);
2277 counter = (((r->m_zhdr0 * 256 + r->m_zhdr1) % 31 != 0) || (r->m_zhdr1 & 32) || ((r->m_zhdr0 & 15) != 8));
2278 if (!(decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF))
2279 counter |= (((1U << (8U + (r->m_zhdr0 >> 4))) > 32768U) || ((out_buf_size_mask + 1) < (size_t)((size_t)1 << (8U + (r->m_zhdr0 >> 4)))));
2280 if (counter) {
2281 TINFL_CR_RETURN_FOREVER(36, TINFL_STATUS_FAILED);
2282 }
2283 }
2284
2285 do {
2286 TINFL_GET_BITS(3, r->m_final, 3);
2287 r->m_type = r->m_final >> 1;
2288 if (r->m_type == 0) {
2289 TINFL_SKIP_BITS(5, num_bits & 7);
2290 for (counter = 0; counter < 4; ++counter) {
2291 if (num_bits)
2292 TINFL_GET_BITS(6, r->m_raw_header[counter], 8);
2293 else
2294 TINFL_GET_BYTE(7, r->m_raw_header[counter]);
2295 }
2296 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)))) {
2297 TINFL_CR_RETURN_FOREVER(39, TINFL_STATUS_FAILED);
2298 }
2299 while ((counter) && (num_bits)) {
2300 TINFL_GET_BITS(51, dist, 8);
2301 while (pOut_buf_cur >= pOut_buf_end) {
2302 TINFL_CR_RETURN(52, TINFL_STATUS_HAS_MORE_OUTPUT);
2303 }
2304 *pOut_buf_cur++ = (mz_uint8)dist;
2305 counter--;
2306 }
2307 while (counter) {
2308 size_t n;
2309 while (pOut_buf_cur >= pOut_buf_end) {
2310 TINFL_CR_RETURN(9, TINFL_STATUS_HAS_MORE_OUTPUT);
2311 }
2312 while (pIn_buf_cur >= pIn_buf_end) {
2313 TINFL_CR_RETURN(38, (decomp_flags & TINFL_FLAG_HAS_MORE_INPUT) ? TINFL_STATUS_NEEDS_MORE_INPUT : TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS);
2314 }
2315 n = MZ_MIN(MZ_MIN((size_t)(pOut_buf_end - pOut_buf_cur), (size_t)(pIn_buf_end - pIn_buf_cur)), counter);
2316 TINFL_MEMCPY(pOut_buf_cur, pIn_buf_cur, n);
2317 pIn_buf_cur += n;
2318 pOut_buf_cur += n;
2319 counter -= (mz_uint)n;
2320 }
2321 } else if (r->m_type == 3) {
2322 TINFL_CR_RETURN_FOREVER(10, TINFL_STATUS_FAILED);
2323 } else {
2324 if (r->m_type == 1) {
2325 mz_uint8* p = r->m_code_size_0;
2326 mz_uint i;
2327 r->m_table_sizes[0] = 288;
2328 r->m_table_sizes[1] = 32;
2329 TINFL_MEMSET(r->m_code_size_1, 5, 32);
2330 for (i = 0; i <= 143; ++i)
2331 *p++ = 8;
2332 for (; i <= 255; ++i)
2333 *p++ = 9;
2334 for (; i <= 279; ++i)
2335 *p++ = 7;
2336 for (; i <= 287; ++i)
2337 *p++ = 8;
2338 } else {
2339 for (counter = 0; counter < 3; counter++) {
2340 TINFL_GET_BITS(11, r->m_table_sizes[counter], "\05\05\04"[counter]);
2341 r->m_table_sizes[counter] += s_min_table_sizes[counter];
2342 }
2343 MZ_CLEAR_ARR(r->m_code_size_2);
2344 for (counter = 0; counter < r->m_table_sizes[2]; counter++) {
2345 mz_uint s;
2346 TINFL_GET_BITS(14, s, 3);
2347 r->m_code_size_2[s_length_dezigzag[counter]] = (mz_uint8)s;
2348 }
2349 r->m_table_sizes[2] = 19;
2350 }
2351 for (; (int)r->m_type >= 0; r->m_type--) {
2352 int tree_next, tree_cur;
2353 mz_int16* pLookUp;
2354 mz_int16* pTree;
2355 mz_uint8* pCode_size;
2356 mz_uint i, j, used_syms, total, sym_index, next_code[17], total_syms[16];
2357 pLookUp = r->m_look_up[r->m_type];
2358 pTree = pTrees[r->m_type];
2359 pCode_size = pCode_sizes[r->m_type];
2360 MZ_CLEAR_ARR(total_syms);
2361 TINFL_MEMSET(pLookUp, 0, sizeof(r->m_look_up[0]));
2363 for (i = 0; i < r->m_table_sizes[r->m_type]; ++i)
2364 total_syms[pCode_size[i]]++;
2365 used_syms = 0, total = 0;
2366 next_code[0] = next_code[1] = 0;
2367 for (i = 1; i <= 15; ++i) {
2368 used_syms += total_syms[i];
2369 next_code[i + 1] = (total = ((total + total_syms[i]) << 1));
2370 }
2371 if ((65536 != total) && (used_syms > 1)) {
2372 TINFL_CR_RETURN_FOREVER(35, TINFL_STATUS_FAILED);
2373 }
2374 for (tree_next = -1, sym_index = 0; sym_index < r->m_table_sizes[r->m_type]; ++sym_index) {
2375 mz_uint rev_code = 0, l, cur_code, code_size = pCode_size[sym_index];
2376 if (!code_size)
2377 continue;
2378 cur_code = next_code[code_size]++;
2379 for (l = code_size; l > 0; l--, cur_code >>= 1)
2380 rev_code = (rev_code << 1) | (cur_code & 1);
2381 if (code_size <= TINFL_FAST_LOOKUP_BITS) {
2382 mz_int16 k = (mz_int16)((code_size << 9) | sym_index);
2383 while (rev_code < TINFL_FAST_LOOKUP_SIZE) {
2384 pLookUp[rev_code] = k;
2385 rev_code += (1 << code_size);
2386 }
2387 continue;
2388 }
2389 if (0 == (tree_cur = pLookUp[rev_code & (TINFL_FAST_LOOKUP_SIZE - 1)])) {
2390 pLookUp[rev_code & (TINFL_FAST_LOOKUP_SIZE - 1)] = (mz_int16)tree_next;
2391 tree_cur = tree_next;
2392 tree_next -= 2;
2393 }
2394 rev_code >>= (TINFL_FAST_LOOKUP_BITS - 1);
2395 for (j = code_size; j > (TINFL_FAST_LOOKUP_BITS + 1); j--) {
2396 tree_cur -= ((rev_code >>= 1) & 1);
2397 if (!pTree[-tree_cur - 1]) {
2398 pTree[-tree_cur - 1] = (mz_int16)tree_next;
2399 tree_cur = tree_next;
2400 tree_next -= 2;
2401 } else
2402 tree_cur = pTree[-tree_cur - 1];
2403 }
2404 tree_cur -= ((rev_code >>= 1) & 1);
2405 pTree[-tree_cur - 1] = (mz_int16)sym_index;
2406 }
2407 if (r->m_type == 2) {
2408 for (counter = 0; counter < (r->m_table_sizes[0] + r->m_table_sizes[1]);) {
2409 mz_uint s;
2410 TINFL_HUFF_DECODE(16, dist, r->m_look_up[2], r->m_tree_2);
2411 if (dist < 16) {
2412 r->m_len_codes[counter++] = (mz_uint8)dist;
2413 continue;
2414 }
2415 if ((dist == 16) && (!counter)) {
2416 TINFL_CR_RETURN_FOREVER(17, TINFL_STATUS_FAILED);
2417 }
2418 num_extra = "\02\03\07"[dist - 16];
2419 TINFL_GET_BITS(18, s, num_extra);
2420 s += "\03\03\013"[dist - 16];
2421 TINFL_MEMSET(r->m_len_codes + counter, (dist == 16) ? r->m_len_codes[counter - 1] : 0, s);
2422 counter += s;
2423 }
2424 if ((r->m_table_sizes[0] + r->m_table_sizes[1]) != counter) {
2425 TINFL_CR_RETURN_FOREVER(21, TINFL_STATUS_FAILED);
2426 }
2427 TINFL_MEMCPY(r->m_code_size_0, r->m_len_codes, r->m_table_sizes[0]);
2428 TINFL_MEMCPY(r->m_code_size_1, r->m_len_codes + r->m_table_sizes[0], r->m_table_sizes[1]);
2429 }
2430 }
2431 for (;;) {
2432 mz_uint8* pSrc;
2433 for (;;) {
2434 if (((pIn_buf_end - pIn_buf_cur) < 4) || ((pOut_buf_end - pOut_buf_cur) < 2)) {
2435 TINFL_HUFF_DECODE(23, counter, r->m_look_up[0], r->m_tree_0);
2436 if (counter >= 256)
2437 break;
2438 while (pOut_buf_cur >= pOut_buf_end) {
2439 TINFL_CR_RETURN(24, TINFL_STATUS_HAS_MORE_OUTPUT);
2440 }
2441 *pOut_buf_cur++ = (mz_uint8)counter;
2442 } else {
2443 int sym2;
2444 mz_uint code_len;
2445#if TINFL_USE_64BIT_BITBUF
2446 if (num_bits < 30) {
2447 bit_buf |= (((tinfl_bit_buf_t)MZ_READ_LE32(pIn_buf_cur)) << num_bits);
2448 pIn_buf_cur += 4;
2449 num_bits += 32;
2450 }
2451#else
2452 if (num_bits < 15) {
2453 bit_buf |= (((tinfl_bit_buf_t)MZ_READ_LE16(pIn_buf_cur)) << num_bits);
2454 pIn_buf_cur += 2;
2455 num_bits += 16;
2456 }
2457#endif
2458 if ((sym2 = r->m_look_up[0][bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0)
2459 code_len = sym2 >> 9;
2460 else {
2461 code_len = TINFL_FAST_LOOKUP_BITS;
2462 do {
2463 sym2 = r->m_tree_0[~sym2 + ((bit_buf >> code_len++) & 1)];
2464 } while (sym2 < 0);
2465 }
2466 counter = sym2;
2467 bit_buf >>= code_len;
2468 num_bits -= code_len;
2469 if (counter & 256)
2470 break;
2471
2472#if !TINFL_USE_64BIT_BITBUF
2473 if (num_bits < 15) {
2474 bit_buf |= (((tinfl_bit_buf_t)MZ_READ_LE16(pIn_buf_cur)) << num_bits);
2475 pIn_buf_cur += 2;
2476 num_bits += 16;
2477 }
2478#endif
2479 if ((sym2 = r->m_look_up[0][bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0)
2480 code_len = sym2 >> 9;
2481 else {
2482 code_len = TINFL_FAST_LOOKUP_BITS;
2483 do {
2484 sym2 = r->m_tree_0[~sym2 + ((bit_buf >> code_len++) & 1)];
2485 } while (sym2 < 0);
2486 }
2487 bit_buf >>= code_len;
2488 num_bits -= code_len;
2489
2490 pOut_buf_cur[0] = (mz_uint8)counter;
2491 if (sym2 & 256) {
2492 pOut_buf_cur++;
2493 counter = sym2;
2494 break;
2495 }
2496 pOut_buf_cur[1] = (mz_uint8)sym2;
2497 pOut_buf_cur += 2;
2498 }
2499 }
2500 if ((counter &= 511) == 256)
2501 break;
2502
2503 num_extra = s_length_extra[counter - 257];
2504 counter = s_length_base[counter - 257];
2505 if (num_extra) {
2506 mz_uint extra_bits;
2507 TINFL_GET_BITS(25, extra_bits, num_extra);
2508 counter += extra_bits;
2509 }
2510
2511 TINFL_HUFF_DECODE(26, dist, r->m_look_up[1], r->m_tree_1);
2512 num_extra = s_dist_extra[dist];
2513 dist = s_dist_base[dist];
2514 if (num_extra) {
2515 mz_uint extra_bits;
2516 TINFL_GET_BITS(27, extra_bits, num_extra);
2517 dist += extra_bits;
2518 }
2519
2520 dist_from_out_buf_start = pOut_buf_cur - pOut_buf_start;
2521 if ((dist == 0 || dist > dist_from_out_buf_start || dist_from_out_buf_start == 0) && (decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF)) {
2522 TINFL_CR_RETURN_FOREVER(37, TINFL_STATUS_FAILED);
2523 }
2524
2525 pSrc = pOut_buf_start + ((dist_from_out_buf_start - dist) & out_buf_size_mask);
2526
2527 if ((MZ_MAX(pOut_buf_cur, pSrc) + counter) > pOut_buf_end) {
2528 while (counter--) {
2529 while (pOut_buf_cur >= pOut_buf_end) {
2530 TINFL_CR_RETURN(53, TINFL_STATUS_HAS_MORE_OUTPUT);
2531 }
2532 *pOut_buf_cur++ = pOut_buf_start[(dist_from_out_buf_start++ - dist) & out_buf_size_mask];
2533 }
2534 continue;
2535 }
2536#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES
2537 else if ((counter >= 9) && (counter <= dist)) {
2538 const mz_uint8* pSrc_end = pSrc + (counter & ~7);
2539 do {
2540#ifdef MINIZ_UNALIGNED_USE_MEMCPY
2541 memcpy(pOut_buf_cur, pSrc, sizeof(mz_uint32) * 2);
2542#else
2543 ((mz_uint32*)pOut_buf_cur)[0] = ((const mz_uint32*)pSrc)[0];
2544 ((mz_uint32*)pOut_buf_cur)[1] = ((const mz_uint32*)pSrc)[1];
2545#endif
2546 pOut_buf_cur += 8;
2547 } while ((pSrc += 8) < pSrc_end);
2548 if ((counter &= 7) < 3) {
2549 if (counter) {
2550 pOut_buf_cur[0] = pSrc[0];
2551 if (counter > 1)
2552 pOut_buf_cur[1] = pSrc[1];
2553 pOut_buf_cur += counter;
2554 }
2555 continue;
2556 }
2557 }
2558#endif
2559 while (counter > 2) {
2560 pOut_buf_cur[0] = pSrc[0];
2561 pOut_buf_cur[1] = pSrc[1];
2562 pOut_buf_cur[2] = pSrc[2];
2563 pOut_buf_cur += 3;
2564 pSrc += 3;
2565 counter -= 3;
2566 }
2567 if (counter > 0) {
2568 pOut_buf_cur[0] = pSrc[0];
2569 if (counter > 1)
2570 pOut_buf_cur[1] = pSrc[1];
2571 pOut_buf_cur += counter;
2572 }
2573 }
2574 }
2575 } while (!(r->m_final & 1));
2576
2577 /* 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. */
2578 /* 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. */
2579 TINFL_SKIP_BITS(32, num_bits & 7);
2580 while ((pIn_buf_cur > pIn_buf_next) && (num_bits >= 8)) {
2581 --pIn_buf_cur;
2582 num_bits -= 8;
2583 }
2584 bit_buf &= ~(~(tinfl_bit_buf_t)0 << num_bits);
2585 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). */
2586
2587 if (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER) {
2588 for (counter = 0; counter < 4; ++counter) {
2589 mz_uint s;
2590 if (num_bits)
2591 TINFL_GET_BITS(41, s, 8);
2592 else
2593 TINFL_GET_BYTE(42, s);
2594 r->m_z_adler32 = (r->m_z_adler32 << 8) | s;
2595 }
2596 }
2597 TINFL_CR_RETURN_FOREVER(34, TINFL_STATUS_DONE);
2598
2600
2601common_exit:
2602 /* As long as we aren't telling the caller that we NEED more input to make forward progress: */
2603 /* 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. */
2604 /* 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. */
2605 if ((status != TINFL_STATUS_NEEDS_MORE_INPUT) && (status != TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS)) {
2606 while ((pIn_buf_cur > pIn_buf_next) && (num_bits >= 8)) {
2607 --pIn_buf_cur;
2608 num_bits -= 8;
2609 }
2610 }
2611 r->m_num_bits = num_bits;
2612 r->m_bit_buf = bit_buf & ~(~(tinfl_bit_buf_t)0 << num_bits);
2613 r->m_dist = dist;
2614 r->m_counter = counter;
2615 r->m_num_extra = num_extra;
2616 r->m_dist_from_out_buf_start = dist_from_out_buf_start;
2617 *pIn_buf_size = pIn_buf_cur - pIn_buf_next;
2618 *pOut_buf_size = pOut_buf_cur - pOut_buf_next;
2619 if ((decomp_flags & (TINFL_FLAG_PARSE_ZLIB_HEADER | TINFL_FLAG_COMPUTE_ADLER32)) && (status >= 0)) {
2620 const mz_uint8* ptr = pOut_buf_next;
2621 size_t buf_len = *pOut_buf_size;
2622 mz_uint32 i, s1 = r->m_check_adler32 & 0xffff, s2 = r->m_check_adler32 >> 16;
2623 size_t block_len = buf_len % 5552;
2624 while (buf_len) {
2625 for (i = 0; i + 7 < block_len; i += 8, ptr += 8) {
2626 s1 += ptr[0], s2 += s1;
2627 s1 += ptr[1], s2 += s1;
2628 s1 += ptr[2], s2 += s1;
2629 s1 += ptr[3], s2 += s1;
2630 s1 += ptr[4], s2 += s1;
2631 s1 += ptr[5], s2 += s1;
2632 s1 += ptr[6], s2 += s1;
2633 s1 += ptr[7], s2 += s1;
2634 }
2635 for (; i < block_len; ++i)
2636 s1 += *ptr++, s2 += s1;
2637 s1 %= 65521U, s2 %= 65521U;
2638 buf_len -= block_len;
2639 block_len = 5552;
2640 }
2641 r->m_check_adler32 = (s2 << 16) + s1;
2642 if ((status == TINFL_STATUS_DONE) && (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER) && (r->m_check_adler32 != r->m_z_adler32))
2643 status = TINFL_STATUS_ADLER32_MISMATCH;
2644 }
2645 return status;
2646}
2647
2648/* Higher level helper functions. */
2649void* tinfl_decompress_mem_to_heap(const void* pSrc_buf, size_t src_buf_len, size_t* pOut_len, int flags)
2650{
2651 tinfl_decompressor decomp;
2652 void *pBuf = NULL, *pNew_buf;
2653 size_t src_buf_ofs = 0, out_buf_capacity = 0;
2654 *pOut_len = 0;
2655 tinfl_init(&decomp);
2656 for (;;) {
2657 size_t src_buf_size = src_buf_len - src_buf_ofs, dst_buf_size = out_buf_capacity - *pOut_len, new_out_buf_capacity;
2658 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,
2659 (flags & ~TINFL_FLAG_HAS_MORE_INPUT) | TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF);
2660 if ((status < 0) || (status == TINFL_STATUS_NEEDS_MORE_INPUT)) {
2661 MZ_FREE(pBuf);
2662 *pOut_len = 0;
2663 return NULL;
2664 }
2665 src_buf_ofs += src_buf_size;
2666 *pOut_len += dst_buf_size;
2667 if (status == TINFL_STATUS_DONE)
2668 break;
2669 new_out_buf_capacity = out_buf_capacity * 2;
2670 if (new_out_buf_capacity < 128)
2671 new_out_buf_capacity = 128;
2672 pNew_buf = MZ_REALLOC(pBuf, new_out_buf_capacity);
2673 if (!pNew_buf) {
2674 MZ_FREE(pBuf);
2675 *pOut_len = 0;
2676 return NULL;
2677 }
2678 pBuf = pNew_buf;
2679 out_buf_capacity = new_out_buf_capacity;
2680 }
2681 return pBuf;
2682}
2683
2684size_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)
2685{
2686 tinfl_decompressor decomp;
2687 tinfl_status status;
2688 tinfl_init(&decomp);
2689 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);
2690 return (status != TINFL_STATUS_DONE) ? TINFL_DECOMPRESS_MEM_TO_MEM_FAILED : out_buf_len;
2691}
2692
2693int 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)
2694{
2695 int result = 0;
2696 tinfl_decompressor decomp;
2697 mz_uint8* pDict = (mz_uint8*)MZ_MALLOC(TINFL_LZ_DICT_SIZE);
2698 size_t in_buf_ofs = 0, dict_ofs = 0;
2699 if (!pDict)
2700 return TINFL_STATUS_FAILED;
2701 memset(pDict, 0, TINFL_LZ_DICT_SIZE);
2702 tinfl_init(&decomp);
2703 for (;;) {
2704 size_t in_buf_size = *pIn_buf_size - in_buf_ofs, dst_buf_size = TINFL_LZ_DICT_SIZE - dict_ofs;
2705 tinfl_status status = tinfl_decompress(&decomp, (const mz_uint8*)pIn_buf + in_buf_ofs, &in_buf_size, pDict, pDict + dict_ofs, &dst_buf_size,
2706 (flags & ~(TINFL_FLAG_HAS_MORE_INPUT | TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF)));
2707 in_buf_ofs += in_buf_size;
2708 if ((dst_buf_size) && (!(*pPut_buf_func)(pDict + dict_ofs, (int)dst_buf_size, pPut_buf_user)))
2709 break;
2710 if (status != TINFL_STATUS_HAS_MORE_OUTPUT) {
2711 result = (status == TINFL_STATUS_DONE);
2712 break;
2713 }
2714 dict_ofs = (dict_ofs + dst_buf_size) & (TINFL_LZ_DICT_SIZE - 1);
2715 }
2716 MZ_FREE(pDict);
2717 *pIn_buf_size = in_buf_ofs;
2718 return result;
2719}
2720
2721#ifndef MINIZ_NO_MALLOC
2722tinfl_decompressor* tinfl_decompressor_alloc(void)
2723{
2724 tinfl_decompressor* pDecomp = (tinfl_decompressor*)MZ_MALLOC(sizeof(tinfl_decompressor));
2725 if (pDecomp)
2726 tinfl_init(pDecomp);
2727 return pDecomp;
2728}
2729
2730void tinfl_decompressor_free(tinfl_decompressor* pDecomp)
2731{
2732 MZ_FREE(pDecomp);
2733}
2734#endif
2735
2736#ifdef __cplusplus
2737}
2738#endif
2739
2740#endif /*#ifndef MINIZ_NO_INFLATE_APIS*/
2741/**************************************************************************
2742 *
2743 * Copyright 2013-2014 RAD Game Tools and Valve Software
2744 * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
2745 * Copyright 2016 Martin Raiber
2746 * All Rights Reserved.
2747 *
2748 * Permission is hereby granted, free of charge, to any person obtaining a copy
2749 * of this software and associated documentation files (the "Software"), to deal
2750 * in the Software without restriction, including without limitation the rights
2751 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
2752 * copies of the Software, and to permit persons to whom the Software is
2753 * furnished to do so, subject to the following conditions:
2754 *
2755 * The above copyright notice and this permission notice shall be included in
2756 * all copies or substantial portions of the Software.
2757 *
2758 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2759 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2760 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
2761 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2762 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2763 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2764 * THE SOFTWARE.
2765 *
2766 **************************************************************************/
2767
2768#ifndef MINIZ_NO_ARCHIVE_APIS
2769
2770#ifdef __cplusplus
2771extern "C" {
2772#endif
2773
2774/* ------------------- .ZIP archive reading */
2775
2776#ifdef MINIZ_NO_STDIO
2777#define MZ_FILE void*
2778#else
2779#include <sys/stat.h>
2780
2781#if defined(_MSC_VER) || defined(__MINGW64__) || defined(__MINGW32__)
2782
2783#define WIN32_LEAN_AND_MEAN
2784#include <windows.h>
2785
2786static WCHAR* mz_utf8z_to_widechar(const char* str)
2787{
2788 int reqChars = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
2789 WCHAR* wStr = (WCHAR*)malloc(reqChars * sizeof(WCHAR));
2790 MultiByteToWideChar(CP_UTF8, 0, str, -1, wStr, reqChars);
2791 return wStr;
2792}
2793
2794static FILE* mz_fopen(const char* pFilename, const char* pMode)
2795{
2796 WCHAR* wFilename = mz_utf8z_to_widechar(pFilename);
2797 WCHAR* wMode = mz_utf8z_to_widechar(pMode);
2798 FILE* pFile = NULL;
2799 errno_t err = _wfopen_s(&pFile, wFilename, wMode);
2800 free(wFilename);
2801 free(wMode);
2802 return err ? NULL : pFile;
2803}
2804
2805static FILE* mz_freopen(const char* pPath, const char* pMode, FILE* pStream)
2806{
2807 WCHAR* wPath = mz_utf8z_to_widechar(pPath);
2808 WCHAR* wMode = mz_utf8z_to_widechar(pMode);
2809 FILE* pFile = NULL;
2810 errno_t err = _wfreopen_s(&pFile, wPath, wMode, pStream);
2811 free(wPath);
2812 free(wMode);
2813 return err ? NULL : pFile;
2814}
2815
2816#if defined(__MINGW32__)
2817static int mz_stat(const char* path, struct _stat* buffer)
2818{
2819 WCHAR* wPath = mz_utf8z_to_widechar(path);
2820 int res = _wstat(wPath, buffer);
2821 free(wPath);
2822 return res;
2823}
2824#else
2825static int mz_stat64(const char* path, struct __stat64* buffer)
2826{
2827 WCHAR* wPath = mz_utf8z_to_widechar(path);
2828 int res = _wstat64(wPath, buffer);
2829 free(wPath);
2830 return res;
2831}
2832#endif
2833
2834#ifndef MINIZ_NO_TIME
2835#include <sys/utime.h>
2836#endif
2837#define MZ_FOPEN mz_fopen
2838#define MZ_FCLOSE fclose
2839#define MZ_FREAD fread
2840#define MZ_FWRITE fwrite
2841#define MZ_FTELL64 _ftelli64
2842#define MZ_FSEEK64 _fseeki64
2843#if defined(__MINGW32__)
2844#define MZ_FILE_STAT_STRUCT _stat
2845#define MZ_FILE_STAT mz_stat
2846#else
2847#define MZ_FILE_STAT_STRUCT _stat64
2848#define MZ_FILE_STAT mz_stat64
2849#endif
2850#define MZ_FFLUSH fflush
2851#define MZ_FREOPEN mz_freopen
2852#define MZ_DELETE_FILE remove
2853
2854#elif defined(__WATCOMC__)
2855#ifndef MINIZ_NO_TIME
2856#include <sys/utime.h>
2857#endif
2858#define MZ_FOPEN(f, m) fopen(f, m)
2859#define MZ_FCLOSE fclose
2860#define MZ_FREAD fread
2861#define MZ_FWRITE fwrite
2862#define MZ_FTELL64 _ftelli64
2863#define MZ_FSEEK64 _fseeki64
2864#define MZ_FILE_STAT_STRUCT stat
2865#define MZ_FILE_STAT stat
2866#define MZ_FFLUSH fflush
2867#define MZ_FREOPEN(f, m, s) freopen(f, m, s)
2868#define MZ_DELETE_FILE remove
2869
2870#elif defined(__TINYC__)
2871#ifndef MINIZ_NO_TIME
2872#include <sys/utime.h>
2873#endif
2874#define MZ_FOPEN(f, m) fopen(f, m)
2875#define MZ_FCLOSE fclose
2876#define MZ_FREAD fread
2877#define MZ_FWRITE fwrite
2878#define MZ_FTELL64 ftell
2879#define MZ_FSEEK64 fseek
2880#define MZ_FILE_STAT_STRUCT stat
2881#define MZ_FILE_STAT stat
2882#define MZ_FFLUSH fflush
2883#define MZ_FREOPEN(f, m, s) freopen(f, m, s)
2884#define MZ_DELETE_FILE remove
2885
2886#elif defined(__USE_LARGEFILE64) /* gcc, clang */
2887#ifndef MINIZ_NO_TIME
2888#include <utime.h>
2889#endif
2890#define MZ_FOPEN(f, m) fopen64(f, m)
2891#define MZ_FCLOSE fclose
2892#define MZ_FREAD fread
2893#define MZ_FWRITE fwrite
2894#define MZ_FTELL64 ftello64
2895#define MZ_FSEEK64 fseeko64
2896#define MZ_FILE_STAT_STRUCT stat64
2897#define MZ_FILE_STAT stat64
2898#define MZ_FFLUSH fflush
2899#define MZ_FREOPEN(p, m, s) freopen64(p, m, s)
2900#define MZ_DELETE_FILE remove
2901
2902#elif defined(__APPLE__) || defined(__FreeBSD__)
2903#ifndef MINIZ_NO_TIME
2904#include <utime.h>
2905#endif
2906#define MZ_FOPEN(f, m) fopen(f, m)
2907#define MZ_FCLOSE fclose
2908#define MZ_FREAD fread
2909#define MZ_FWRITE fwrite
2910#define MZ_FTELL64 ftello
2911#define MZ_FSEEK64 fseeko
2912#define MZ_FILE_STAT_STRUCT stat
2913#define MZ_FILE_STAT stat
2914#define MZ_FFLUSH fflush
2915#define MZ_FREOPEN(p, m, s) freopen(p, m, s)
2916#define MZ_DELETE_FILE remove
2917
2918#else
2919#pragma message("Using fopen, ftello, fseeko, stat() etc. path for file I/O - this path may not support large files.")
2920#ifndef MINIZ_NO_TIME
2921#include <utime.h>
2922#endif
2923#define MZ_FOPEN(f, m) fopen(f, m)
2924#define MZ_FCLOSE fclose
2925#define MZ_FREAD fread
2926#define MZ_FWRITE fwrite
2927#ifdef __STRICT_ANSI__
2928#define MZ_FTELL64 ftell
2929#define MZ_FSEEK64 fseek
2930#else
2931#define MZ_FTELL64 ftello
2932#define MZ_FSEEK64 fseeko
2933#endif
2934#define MZ_FILE_STAT_STRUCT stat
2935#define MZ_FILE_STAT stat
2936#define MZ_FFLUSH fflush
2937#define MZ_FREOPEN(f, m, s) freopen(f, m, s)
2938#define MZ_DELETE_FILE remove
2939#endif /* #ifdef _MSC_VER */
2940#endif /* #ifdef MINIZ_NO_STDIO */
2941
2942#define MZ_TOLOWER(c) ((((c) >= 'A') && ((c) <= 'Z')) ? ((c) - 'A' + 'a') : (c))
2943
2944/* 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. */
2945enum {
2946 /* ZIP archive identifiers and record sizes */
2953
2954 /* ZIP64 archive identifier and record sizes */
2963
2964 /* Central directory header record offsets */
2982
2983 /* Local directory header offsets */
2996
2997 /* End of central directory offsets */
3006
3007 /* ZIP64 End of central directory locator offsets */
3008 MZ_ZIP64_ECDL_SIG_OFS = 0, /* 4 bytes */
3012
3013 /* ZIP64 End of central directory header offsets */
3014 MZ_ZIP64_ECDH_SIG_OFS = 0, /* 4 bytes */
3023 MZ_ZIP64_ECDH_CDIR_OFS_OFS = 48, /* 8 bytes */
3031};
3032
3033typedef struct
3034{
3035 void* m_p;
3038} mz_zip_array;
3039
3044
3045 /* The flags passed in when the archive is initially opened. */
3046 mz_uint32 m_init_flags;
3047
3048 /* MZ_TRUE if the archive has a zip64 end of central directory headers, etc. */
3049 mz_bool m_zip64;
3050
3051 /* 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.) */
3053
3054 /* These fields are used by the file, FILE, memory, and memory/heap read/write helpers. */
3055 MZ_FILE* m_pFile;
3057
3058 void* m_pMem;
3061};
3062
3063#define MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(array_ptr, element_size) (array_ptr)->m_element_size = element_size
3064
3065#if defined(DEBUG) || defined(_DEBUG)
3066static MZ_FORCEINLINE mz_uint mz_zip_array_range_check(const mz_zip_array* pArray, mz_uint index)
3067{
3068 MZ_ASSERT(index < pArray->m_size);
3069 return index;
3070}
3071#define MZ_ZIP_ARRAY_ELEMENT(array_ptr, element_type, index) ((element_type*)((array_ptr)->m_p))[mz_zip_array_range_check(array_ptr, index)]
3072#else
3073#define MZ_ZIP_ARRAY_ELEMENT(array_ptr, element_type, index) ((element_type*)((array_ptr)->m_p))[index]
3074#endif
3075
3076static MZ_FORCEINLINE void mz_zip_array_init(mz_zip_array* pArray, mz_uint32 element_size)
3077{
3078 memset(pArray, 0, sizeof(mz_zip_array));
3079 pArray->m_element_size = element_size;
3080}
3081
3082static MZ_FORCEINLINE void mz_zip_array_clear(mz_zip_archive* pZip, mz_zip_array* pArray)
3083{
3084 pZip->m_pFree(pZip->m_pAlloc_opaque, pArray->m_p);
3085 memset(pArray, 0, sizeof(mz_zip_array));
3086}
3087
3088static mz_bool mz_zip_array_ensure_capacity(mz_zip_archive* pZip, mz_zip_array* pArray, size_t min_new_capacity, mz_uint growing)
3089{
3090 void* pNew_p;
3091 size_t new_capacity = min_new_capacity;
3092 MZ_ASSERT(pArray->m_element_size);
3093 if (pArray->m_capacity >= min_new_capacity)
3094 return MZ_TRUE;
3095 if (growing) {
3096 new_capacity = MZ_MAX(1, pArray->m_capacity);
3097 while (new_capacity < min_new_capacity)
3098 new_capacity *= 2;
3099 }
3100 if (NULL == (pNew_p = pZip->m_pRealloc(pZip->m_pAlloc_opaque, pArray->m_p, pArray->m_element_size, new_capacity)))
3101 return MZ_FALSE;
3102 pArray->m_p = pNew_p;
3103 pArray->m_capacity = new_capacity;
3104 return MZ_TRUE;
3105}
3106
3107static MZ_FORCEINLINE mz_bool mz_zip_array_reserve(mz_zip_archive* pZip, mz_zip_array* pArray, size_t new_capacity, mz_uint growing)
3108{
3109 if (new_capacity > pArray->m_capacity) {
3110 if (!mz_zip_array_ensure_capacity(pZip, pArray, new_capacity, growing))
3111 return MZ_FALSE;
3112 }
3113 return MZ_TRUE;
3114}
3115
3116static MZ_FORCEINLINE mz_bool mz_zip_array_resize(mz_zip_archive* pZip, mz_zip_array* pArray, size_t new_size, mz_uint growing)
3117{
3118 if (new_size > pArray->m_capacity) {
3119 if (!mz_zip_array_ensure_capacity(pZip, pArray, new_size, growing))
3120 return MZ_FALSE;
3121 }
3122 pArray->m_size = new_size;
3123 return MZ_TRUE;
3124}
3125
3126static MZ_FORCEINLINE mz_bool mz_zip_array_ensure_room(mz_zip_archive* pZip, mz_zip_array* pArray, size_t n)
3127{
3128 return mz_zip_array_reserve(pZip, pArray, pArray->m_size + n, MZ_TRUE);
3129}
3130
3131static MZ_FORCEINLINE mz_bool mz_zip_array_push_back(mz_zip_archive* pZip, mz_zip_array* pArray, const void* pElements, size_t n)
3132{
3133 size_t orig_size = pArray->m_size;
3134 if (!mz_zip_array_resize(pZip, pArray, orig_size + n, MZ_TRUE))
3135 return MZ_FALSE;
3136 if (n > 0)
3137 memcpy((mz_uint8*)pArray->m_p + orig_size * pArray->m_element_size, pElements, n * pArray->m_element_size);
3138 return MZ_TRUE;
3139}
3140
3141#ifndef MINIZ_NO_TIME
3142static MZ_TIME_T mz_zip_dos_to_time_t(int dos_time, int dos_date)
3143{
3144 struct tm tm;
3145 memset(&tm, 0, sizeof(tm));
3146 tm.tm_isdst = -1;
3147 tm.tm_year = ((dos_date >> 9) & 127) + 1980 - 1900;
3148 tm.tm_mon = ((dos_date >> 5) & 15) - 1;
3149 tm.tm_mday = dos_date & 31;
3150 tm.tm_hour = (dos_time >> 11) & 31;
3151 tm.tm_min = (dos_time >> 5) & 63;
3152 tm.tm_sec = (dos_time << 1) & 62;
3153 return mktime(&tm);
3154}
3155
3156#ifndef MINIZ_NO_ARCHIVE_WRITING_APIS
3157static void mz_zip_time_t_to_dos_time(MZ_TIME_T time, mz_uint16* pDOS_time, mz_uint16* pDOS_date)
3158{
3159#ifdef _MSC_VER
3160 struct tm tm_struct;
3161 struct tm* tm = &tm_struct;
3162 errno_t err = localtime_s(tm, &time);
3163 if (err) {
3164 *pDOS_date = 0;
3165 *pDOS_time = 0;
3166 return;
3167 }
3168#else
3169 struct tm* tm = localtime(&time);
3170#endif /* #ifdef _MSC_VER */
3171
3172 *pDOS_time = (mz_uint16)(((tm->tm_hour) << 11) + ((tm->tm_min) << 5) + ((tm->tm_sec) >> 1));
3173 *pDOS_date = (mz_uint16)(((tm->tm_year + 1900 - 1980) << 9) + ((tm->tm_mon + 1) << 5) + tm->tm_mday);
3174}
3175#endif /* MINIZ_NO_ARCHIVE_WRITING_APIS */
3176
3177#ifndef MINIZ_NO_STDIO
3178#ifndef MINIZ_NO_ARCHIVE_WRITING_APIS
3179static mz_bool mz_zip_get_file_modified_time(const char* pFilename, MZ_TIME_T* pTime)
3180{
3181 struct MZ_FILE_STAT_STRUCT file_stat;
3182
3183 /* On Linux with x86 glibc, this call will fail on large files (I think >= 0x80000000 bytes) unless you compiled with _LARGEFILE64_SOURCE. Argh. */
3184 if (MZ_FILE_STAT(pFilename, &file_stat) != 0)
3185 return MZ_FALSE;
3186
3187 *pTime = file_stat.st_mtime;
3188
3189 return MZ_TRUE;
3190}
3191#endif /* #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS*/
3192
3193static mz_bool mz_zip_set_file_times(const char* pFilename, MZ_TIME_T access_time, MZ_TIME_T modified_time)
3194{
3195 struct utimbuf t;
3196
3197 memset(&t, 0, sizeof(t));
3198 t.actime = access_time;
3199 t.modtime = modified_time;
3200
3201 return !utime(pFilename, &t);
3202}
3203#endif /* #ifndef MINIZ_NO_STDIO */
3204#endif /* #ifndef MINIZ_NO_TIME */
3205
3206static MZ_FORCEINLINE mz_bool mz_zip_set_error(mz_zip_archive* pZip, mz_zip_error err_num)
3207{
3208 if (pZip)
3209 pZip->m_last_error = err_num;
3210 return MZ_FALSE;
3211}
3212
3213static mz_bool mz_zip_reader_init_internal(mz_zip_archive* pZip, mz_uint flags)
3214{
3215 (void)flags;
3216 if ((!pZip) || (pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_INVALID))
3217 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
3218
3219 if (!pZip->m_pAlloc)
3220 pZip->m_pAlloc = miniz_def_alloc_func;
3221 if (!pZip->m_pFree)
3222 pZip->m_pFree = miniz_def_free_func;
3223 if (!pZip->m_pRealloc)
3224 pZip->m_pRealloc = miniz_def_realloc_func;
3225
3226 pZip->m_archive_size = 0;
3227 pZip->m_central_directory_file_ofs = 0;
3228 pZip->m_total_files = 0;
3229 pZip->m_last_error = MZ_ZIP_NO_ERROR;
3230
3231 if (NULL == (pZip->m_pState = (mz_zip_internal_state*)pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, sizeof(mz_zip_internal_state))))
3232 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
3233
3234 memset(pZip->m_pState, 0, sizeof(mz_zip_internal_state));
3235 MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_central_dir, sizeof(mz_uint8));
3236 MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_central_dir_offsets, sizeof(mz_uint32));
3237 MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_sorted_central_dir_offsets, sizeof(mz_uint32));
3238 pZip->m_pState->m_init_flags = flags;
3239 pZip->m_pState->m_zip64 = MZ_FALSE;
3240 pZip->m_pState->m_zip64_has_extended_info_fields = MZ_FALSE;
3241
3242 pZip->m_zip_mode = MZ_ZIP_MODE_READING;
3243
3244 return MZ_TRUE;
3245}
3246
3247static 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)
3248{
3249 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;
3250 const mz_uint8* pR = &MZ_ZIP_ARRAY_ELEMENT(pCentral_dir_array, mz_uint8, MZ_ZIP_ARRAY_ELEMENT(pCentral_dir_offsets, mz_uint32, r_index));
3251 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);
3252 mz_uint8 l = 0, r = 0;
3255 pE = pL + MZ_MIN(l_len, r_len);
3256 while (pL < pE) {
3257 if ((l = MZ_TOLOWER(*pL)) != (r = MZ_TOLOWER(*pR)))
3258 break;
3259 pL++;
3260 pR++;
3261 }
3262 return (pL == pE) ? (l_len < r_len) : (l < r);
3263}
3264
3265#define MZ_SWAP_UINT32(a, b) \
3266 do { \
3267 mz_uint32 t = a; \
3268 a = b; \
3269 b = t; \
3270 } \
3271 MZ_MACRO_END
3272
3273/* 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.) */
3275{
3276 mz_zip_internal_state* pState = pZip->m_pState;
3277 const mz_zip_array* pCentral_dir_offsets = &pState->m_central_dir_offsets;
3278 const mz_zip_array* pCentral_dir = &pState->m_central_dir;
3279 mz_uint32* pIndices;
3280 mz_uint32 start, end;
3281 const mz_uint32 size = pZip->m_total_files;
3282
3283 if (size <= 1U)
3284 return;
3285
3286 pIndices = &MZ_ZIP_ARRAY_ELEMENT(&pState->m_sorted_central_dir_offsets, mz_uint32, 0);
3287
3288 start = (size - 2U) >> 1U;
3289 for (;;) {
3290 mz_uint64 child, root = start;
3291 for (;;) {
3292 if ((child = (root << 1U) + 1U) >= size)
3293 break;
3294 child += (((child + 1U) < size) && (mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, pIndices[child], pIndices[child + 1U])));
3295 if (!mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, pIndices[root], pIndices[child]))
3296 break;
3297 MZ_SWAP_UINT32(pIndices[root], pIndices[child]);
3298 root = child;
3299 }
3300 if (!start)
3301 break;
3302 start--;
3303 }
3304
3305 end = size - 1;
3306 while (end > 0) {
3307 mz_uint64 child, root = 0;
3308 MZ_SWAP_UINT32(pIndices[end], pIndices[0]);
3309 for (;;) {
3310 if ((child = (root << 1U) + 1U) >= end)
3311 break;
3312 child += (((child + 1U) < end) && mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, pIndices[child], pIndices[child + 1U]));
3313 if (!mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, pIndices[root], pIndices[child]))
3314 break;
3315 MZ_SWAP_UINT32(pIndices[root], pIndices[child]);
3316 root = child;
3317 }
3318 end--;
3319 }
3320}
3321
3322static mz_bool mz_zip_reader_locate_header_sig(mz_zip_archive* pZip, mz_uint32 record_sig, mz_uint32 record_size, mz_int64* pOfs)
3323{
3324 mz_int64 cur_file_ofs;
3325 mz_uint32 buf_u32[4096 / sizeof(mz_uint32)];
3326 mz_uint8* pBuf = (mz_uint8*)buf_u32;
3327
3328 /* Basic sanity checks - reject files which are too small */
3329 if (pZip->m_archive_size < record_size)
3330 return MZ_FALSE;
3331
3332 /* Find the record by scanning the file from the end towards the beginning. */
3333 cur_file_ofs = MZ_MAX((mz_int64)pZip->m_archive_size - (mz_int64)sizeof(buf_u32), 0);
3334 for (;;) {
3335 int i, n = (int)MZ_MIN(sizeof(buf_u32), pZip->m_archive_size - cur_file_ofs);
3336
3337 if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pBuf, n) != (mz_uint)n)
3338 return MZ_FALSE;
3339
3340 for (i = n - 4; i >= 0; --i) {
3341 mz_uint s = MZ_READ_LE32(pBuf + i);
3342 if (s == record_sig) {
3343 if ((pZip->m_archive_size - (cur_file_ofs + i)) >= record_size)
3344 break;
3345 }
3346 }
3347
3348 if (i >= 0) {
3349 cur_file_ofs += i;
3350 break;
3351 }
3352
3353 /* Give up if we've searched the entire file, or we've gone back "too far" (~64kb) */
3354 if ((!cur_file_ofs) || ((pZip->m_archive_size - cur_file_ofs) >= ((mz_uint64)(MZ_UINT16_MAX) + record_size)))
3355 return MZ_FALSE;
3356
3357 cur_file_ofs = MZ_MAX(cur_file_ofs - (sizeof(buf_u32) - 3), 0);
3358 }
3359
3360 *pOfs = cur_file_ofs;
3361 return MZ_TRUE;
3362}
3363
3364static mz_bool mz_zip_reader_read_central_dir(mz_zip_archive* pZip, mz_uint flags)
3365{
3366 mz_uint cdir_size = 0, cdir_entries_on_this_disk = 0, num_this_disk = 0, cdir_disk_index = 0;
3367 mz_uint64 cdir_ofs = 0;
3368 mz_int64 cur_file_ofs = 0;
3369 const mz_uint8* p;
3370
3371 mz_uint32 buf_u32[4096 / sizeof(mz_uint32)];
3372 mz_uint8* pBuf = (mz_uint8*)buf_u32;
3373 mz_bool sort_central_dir = ((flags & MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY) == 0);
3374 mz_uint32 zip64_end_of_central_dir_locator_u32[(MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE + sizeof(mz_uint32) - 1) / sizeof(mz_uint32)];
3375 mz_uint8* pZip64_locator = (mz_uint8*)zip64_end_of_central_dir_locator_u32;
3376
3377 mz_uint32 zip64_end_of_central_dir_header_u32[(MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE + sizeof(mz_uint32) - 1) / sizeof(mz_uint32)];
3378 mz_uint8* pZip64_end_of_central_dir = (mz_uint8*)zip64_end_of_central_dir_header_u32;
3379
3380 mz_uint64 zip64_end_of_central_dir_ofs = 0;
3381
3382 /* 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. */
3383 if (pZip->m_archive_size < MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE)
3384 return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE);
3385
3387 return mz_zip_set_error(pZip, MZ_ZIP_FAILED_FINDING_CENTRAL_DIR);
3388
3389 /* Read and verify the end of central directory record. */
3390 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)
3391 return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
3392
3394 return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE);
3395
3397 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) {
3398 if (MZ_READ_LE32(pZip64_locator + MZ_ZIP64_ECDL_SIG_OFS) == MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIG) {
3399 zip64_end_of_central_dir_ofs = MZ_READ_LE64(pZip64_locator + MZ_ZIP64_ECDL_REL_OFS_TO_ZIP64_ECDR_OFS);
3400 if (zip64_end_of_central_dir_ofs > (pZip->m_archive_size - MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE))
3401 return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE);
3402
3403 if (pZip->m_pRead(pZip->m_pIO_opaque, zip64_end_of_central_dir_ofs, pZip64_end_of_central_dir, MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE) == MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE) {
3404 if (MZ_READ_LE32(pZip64_end_of_central_dir + MZ_ZIP64_ECDH_SIG_OFS) == MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIG) {
3405 pZip->m_pState->m_zip64 = MZ_TRUE;
3406 }
3407 }
3408 }
3409 }
3410 }
3411
3412 pZip->m_total_files = MZ_READ_LE16(pBuf + MZ_ZIP_ECDH_CDIR_TOTAL_ENTRIES_OFS);
3413 cdir_entries_on_this_disk = MZ_READ_LE16(pBuf + MZ_ZIP_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS);
3414 num_this_disk = MZ_READ_LE16(pBuf + MZ_ZIP_ECDH_NUM_THIS_DISK_OFS);
3415 cdir_disk_index = MZ_READ_LE16(pBuf + MZ_ZIP_ECDH_NUM_DISK_CDIR_OFS);
3416 cdir_size = MZ_READ_LE32(pBuf + MZ_ZIP_ECDH_CDIR_SIZE_OFS);
3417 cdir_ofs = MZ_READ_LE32(pBuf + MZ_ZIP_ECDH_CDIR_OFS_OFS);
3418
3419 if (pZip->m_pState->m_zip64) {
3420 mz_uint32 zip64_total_num_of_disks = MZ_READ_LE32(pZip64_locator + MZ_ZIP64_ECDL_TOTAL_NUMBER_OF_DISKS_OFS);
3421 mz_uint64 zip64_cdir_total_entries = MZ_READ_LE64(pZip64_end_of_central_dir + MZ_ZIP64_ECDH_CDIR_TOTAL_ENTRIES_OFS);
3422 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);
3423 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);
3424 mz_uint64 zip64_size_of_central_directory = MZ_READ_LE64(pZip64_end_of_central_dir + MZ_ZIP64_ECDH_CDIR_SIZE_OFS);
3425
3426 if (zip64_size_of_end_of_central_dir_record < (MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE - 12))
3427 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
3428
3429 if (zip64_total_num_of_disks != 1U)
3430 return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_MULTIDISK);
3431
3432 /* Check for miniz's practical limits */
3433 if (zip64_cdir_total_entries > MZ_UINT32_MAX)
3434 return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);
3435
3436 pZip->m_total_files = (mz_uint32)zip64_cdir_total_entries;
3437
3438 if (zip64_cdir_total_entries_on_this_disk > MZ_UINT32_MAX)
3439 return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);
3440
3441 cdir_entries_on_this_disk = (mz_uint32)zip64_cdir_total_entries_on_this_disk;
3442
3443 /* Check for miniz's current practical limits (sorry, this should be enough for millions of files) */
3444 if (zip64_size_of_central_directory > MZ_UINT32_MAX)
3445 return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_CDIR_SIZE);
3446
3447 cdir_size = (mz_uint32)zip64_size_of_central_directory;
3448
3449 num_this_disk = MZ_READ_LE32(pZip64_end_of_central_dir + MZ_ZIP64_ECDH_NUM_THIS_DISK_OFS);
3450
3451 cdir_disk_index = MZ_READ_LE32(pZip64_end_of_central_dir + MZ_ZIP64_ECDH_NUM_DISK_CDIR_OFS);
3452
3453 cdir_ofs = MZ_READ_LE64(pZip64_end_of_central_dir + MZ_ZIP64_ECDH_CDIR_OFS_OFS);
3454 }
3455
3456 if (pZip->m_total_files != cdir_entries_on_this_disk)
3457 return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_MULTIDISK);
3458
3459 if (((num_this_disk | cdir_disk_index) != 0) && ((num_this_disk != 1) || (cdir_disk_index != 1)))
3460 return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_MULTIDISK);
3461
3462 if (cdir_size < (mz_uint64)pZip->m_total_files * MZ_ZIP_CENTRAL_DIR_HEADER_SIZE)
3463 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
3464
3465 if ((cdir_ofs + (mz_uint64)cdir_size) > pZip->m_archive_size)
3466 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
3467
3468 pZip->m_central_directory_file_ofs = cdir_ofs;
3469
3470 if (pZip->m_total_files) {
3471 mz_uint i, n;
3472 /* 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. */
3473 if ((!mz_zip_array_resize(pZip, &pZip->m_pState->m_central_dir, cdir_size, MZ_FALSE)) || (!mz_zip_array_resize(pZip, &pZip->m_pState->m_central_dir_offsets, pZip->m_total_files, MZ_FALSE)))
3474 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
3475
3476 if (sort_central_dir) {
3477 if (!mz_zip_array_resize(pZip, &pZip->m_pState->m_sorted_central_dir_offsets, pZip->m_total_files, MZ_FALSE))
3478 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
3479 }
3480
3481 if (pZip->m_pRead(pZip->m_pIO_opaque, cdir_ofs, pZip->m_pState->m_central_dir.m_p, cdir_size) != cdir_size)
3482 return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
3483
3484 /* Now create an index into the central directory file records, do some basic sanity checking on each record */
3485 p = (const mz_uint8*)pZip->m_pState->m_central_dir.m_p;
3486 for (n = cdir_size, i = 0; i < pZip->m_total_files; ++i) {
3487 mz_uint total_header_size, disk_index, bit_flags, filename_size, ext_data_size;
3488 mz_uint64 comp_size, decomp_size, local_header_ofs;
3489
3490 if ((n < MZ_ZIP_CENTRAL_DIR_HEADER_SIZE) || (MZ_READ_LE32(p) != MZ_ZIP_CENTRAL_DIR_HEADER_SIG))
3491 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
3492
3493 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);
3494
3495 if (sort_central_dir)
3496 MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_sorted_central_dir_offsets, mz_uint32, i) = i;
3497
3498 comp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS);
3499 decomp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS);
3500 local_header_ofs = MZ_READ_LE32(p + MZ_ZIP_CDH_LOCAL_HEADER_OFS);
3501 filename_size = MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS);
3502 ext_data_size = MZ_READ_LE16(p + MZ_ZIP_CDH_EXTRA_LEN_OFS);
3503
3504 if ((!pZip->m_pState->m_zip64_has_extended_info_fields) && (ext_data_size) && (MZ_MAX(MZ_MAX(comp_size, decomp_size), local_header_ofs) == MZ_UINT32_MAX)) {
3505 /* Attempt to find zip64 extended information field in the entry's extra data */
3506 mz_uint32 extra_size_remaining = ext_data_size;
3507
3508 if (extra_size_remaining) {
3509 const mz_uint8* pExtra_data;
3510 void* buf = NULL;
3511
3512 if (MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + filename_size + ext_data_size > n) {
3513 buf = MZ_MALLOC(ext_data_size);
3514 if (buf == NULL)
3515 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
3516
3517 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) {
3518 MZ_FREE(buf);
3519 return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
3520 }
3521
3522 pExtra_data = (mz_uint8*)buf;
3523 } else {
3524 pExtra_data = p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + filename_size;
3525 }
3526
3527 do {
3528 mz_uint32 field_id;
3529 mz_uint32 field_data_size;
3530
3531 if (extra_size_remaining < (sizeof(mz_uint16) * 2)) {
3532 MZ_FREE(buf);
3533 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
3534 }
3535
3536 field_id = MZ_READ_LE16(pExtra_data);
3537 field_data_size = MZ_READ_LE16(pExtra_data + sizeof(mz_uint16));
3538
3539 if ((field_data_size + sizeof(mz_uint16) * 2) > extra_size_remaining) {
3540 MZ_FREE(buf);
3541 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
3542 }
3543
3545 /* 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). */
3546 pZip->m_pState->m_zip64 = MZ_TRUE;
3547 pZip->m_pState->m_zip64_has_extended_info_fields = MZ_TRUE;
3548 break;
3549 }
3550
3551 pExtra_data += sizeof(mz_uint16) * 2 + field_data_size;
3552 extra_size_remaining = extra_size_remaining - sizeof(mz_uint16) * 2 - field_data_size;
3553 } while (extra_size_remaining);
3554
3555 MZ_FREE(buf);
3556 }
3557 }
3558
3559 /* I've seen archives that aren't marked as zip64 that uses zip64 ext data, argh */
3560 if ((comp_size != MZ_UINT32_MAX) && (decomp_size != MZ_UINT32_MAX)) {
3561 if (((!MZ_READ_LE32(p + MZ_ZIP_CDH_METHOD_OFS)) && (decomp_size != comp_size)) || (decomp_size && !comp_size))
3562 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
3563 }
3564
3565 disk_index = MZ_READ_LE16(p + MZ_ZIP_CDH_DISK_START_OFS);
3566 if ((disk_index == MZ_UINT16_MAX) || ((disk_index != num_this_disk) && (disk_index != 1)))
3567 return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_MULTIDISK);
3568
3569 if (comp_size != MZ_UINT32_MAX) {
3570 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)
3571 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
3572 }
3573
3574 bit_flags = MZ_READ_LE16(p + MZ_ZIP_CDH_BIT_FLAG_OFS);
3576 return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_ENCRYPTION);
3577
3578 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)
3579 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
3580
3581 n -= total_header_size;
3582 p += total_header_size;
3583 }
3584 }
3585
3586 if (sort_central_dir)
3588
3589 return MZ_TRUE;
3590}
3591
3592void mz_zip_zero_struct(mz_zip_archive* pZip)
3593{
3594 if (pZip)
3595 MZ_CLEAR_PTR(pZip);
3596}
3597
3598static mz_bool mz_zip_reader_end_internal(mz_zip_archive* pZip, mz_bool set_last_error)
3599{
3600 mz_bool status = MZ_TRUE;
3601
3602 if (!pZip)
3603 return MZ_FALSE;
3604
3605 if ((!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || (pZip->m_zip_mode != MZ_ZIP_MODE_READING)) {
3606 if (set_last_error)
3607 pZip->m_last_error = MZ_ZIP_INVALID_PARAMETER;
3608
3609 return MZ_FALSE;
3610 }
3611
3612 if (pZip->m_pState) {
3613 mz_zip_internal_state* pState = pZip->m_pState;
3614 pZip->m_pState = NULL;
3615
3616 mz_zip_array_clear(pZip, &pState->m_central_dir);
3617 mz_zip_array_clear(pZip, &pState->m_central_dir_offsets);
3618 mz_zip_array_clear(pZip, &pState->m_sorted_central_dir_offsets);
3619
3620#ifndef MINIZ_NO_STDIO
3621 if (pState->m_pFile) {
3622 if (pZip->m_zip_type == MZ_ZIP_TYPE_FILE) {
3623 if (MZ_FCLOSE(pState->m_pFile) == EOF) {
3624 if (set_last_error)
3625 pZip->m_last_error = MZ_ZIP_FILE_CLOSE_FAILED;
3626 status = MZ_FALSE;
3627 }
3628 }
3629 pState->m_pFile = NULL;
3630 }
3631#endif /* #ifndef MINIZ_NO_STDIO */
3632
3633 pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
3634 }
3635 pZip->m_zip_mode = MZ_ZIP_MODE_INVALID;
3636
3637 return status;
3638}
3639
3640mz_bool mz_zip_reader_end(mz_zip_archive* pZip)
3641{
3642 return mz_zip_reader_end_internal(pZip, MZ_TRUE);
3643}
3644mz_bool mz_zip_reader_init(mz_zip_archive* pZip, mz_uint64 size, mz_uint flags)
3645{
3646 if ((!pZip) || (!pZip->m_pRead))
3647 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
3648
3649 if (!mz_zip_reader_init_internal(pZip, flags))
3650 return MZ_FALSE;
3651
3652 pZip->m_zip_type = MZ_ZIP_TYPE_USER;
3653 pZip->m_archive_size = size;
3654
3655 if (!mz_zip_reader_read_central_dir(pZip, flags)) {
3656 mz_zip_reader_end_internal(pZip, MZ_FALSE);
3657 return MZ_FALSE;
3658 }
3659
3660 return MZ_TRUE;
3661}
3662
3663static size_t mz_zip_mem_read_func(void* pOpaque, mz_uint64 file_ofs, void* pBuf, size_t n)
3664{
3665 mz_zip_archive* pZip = (mz_zip_archive*)pOpaque;
3666 size_t s = (file_ofs >= pZip->m_archive_size) ? 0 : (size_t)MZ_MIN(pZip->m_archive_size - file_ofs, n);
3667 memcpy(pBuf, (const mz_uint8*)pZip->m_pState->m_pMem + file_ofs, s);
3668 return s;
3669}
3670
3671mz_bool mz_zip_reader_init_mem(mz_zip_archive* pZip, const void* pMem, size_t size, mz_uint flags)
3672{
3673 if (!pMem)
3674 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
3675
3677 return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE);
3678
3679 if (!mz_zip_reader_init_internal(pZip, flags))
3680 return MZ_FALSE;
3681
3682 pZip->m_zip_type = MZ_ZIP_TYPE_MEMORY;
3683 pZip->m_archive_size = size;
3684 pZip->m_pRead = mz_zip_mem_read_func;
3685 pZip->m_pIO_opaque = pZip;
3686 pZip->m_pNeeds_keepalive = NULL;
3687
3688#ifdef __cplusplus
3689 pZip->m_pState->m_pMem = const_cast<void*>(pMem);
3690#else
3691 pZip->m_pState->m_pMem = (void*)pMem;
3692#endif
3693
3694 pZip->m_pState->m_mem_size = size;
3695
3696 if (!mz_zip_reader_read_central_dir(pZip, flags)) {
3697 mz_zip_reader_end_internal(pZip, MZ_FALSE);
3698 return MZ_FALSE;
3699 }
3700
3701 return MZ_TRUE;
3702}
3703
3704#ifndef MINIZ_NO_STDIO
3705static size_t mz_zip_file_read_func(void* pOpaque, mz_uint64 file_ofs, void* pBuf, size_t n)
3706{
3707 mz_zip_archive* pZip = (mz_zip_archive*)pOpaque;
3708 mz_int64 cur_ofs = MZ_FTELL64(pZip->m_pState->m_pFile);
3709
3710 file_ofs += pZip->m_pState->m_file_archive_start_ofs;
3711
3712 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))))
3713 return 0;
3714
3715 return MZ_FREAD(pBuf, 1, n, pZip->m_pState->m_pFile);
3716}
3717
3718mz_bool mz_zip_reader_init_file(mz_zip_archive* pZip, const char* pFilename, mz_uint32 flags)
3719{
3720 return mz_zip_reader_init_file_v2(pZip, pFilename, flags, 0, 0);
3721}
3722
3723mz_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)
3724{
3725 mz_uint64 file_size;
3726 MZ_FILE* pFile;
3727
3728 if ((!pZip) || (!pFilename) || ((archive_size) && (archive_size < MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE)))
3729 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
3730
3731 pFile = MZ_FOPEN(pFilename, "rb");
3732 if (!pFile)
3733 return mz_zip_set_error(pZip, MZ_ZIP_FILE_OPEN_FAILED);
3734
3735 file_size = archive_size;
3736 if (!file_size) {
3737 if (MZ_FSEEK64(pFile, 0, SEEK_END)) {
3738 MZ_FCLOSE(pFile);
3739 return mz_zip_set_error(pZip, MZ_ZIP_FILE_SEEK_FAILED);
3740 }
3741
3742 file_size = MZ_FTELL64(pFile);
3743 }
3744
3745 /* TODO: Better sanity check archive_size and the # of actual remaining bytes */
3746
3747 if (file_size < MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) {
3748 MZ_FCLOSE(pFile);
3749 return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE);
3750 }
3751
3752 if (!mz_zip_reader_init_internal(pZip, flags)) {
3753 MZ_FCLOSE(pFile);
3754 return MZ_FALSE;
3755 }
3756
3757 pZip->m_zip_type = MZ_ZIP_TYPE_FILE;
3758 pZip->m_pRead = mz_zip_file_read_func;
3759 pZip->m_pIO_opaque = pZip;
3760 pZip->m_pState->m_pFile = pFile;
3761 pZip->m_archive_size = file_size;
3762 pZip->m_pState->m_file_archive_start_ofs = file_start_ofs;
3763
3764 if (!mz_zip_reader_read_central_dir(pZip, flags)) {
3765 mz_zip_reader_end_internal(pZip, MZ_FALSE);
3766 return MZ_FALSE;
3767 }
3768
3769 return MZ_TRUE;
3770}
3771
3772mz_bool mz_zip_reader_init_cfile(mz_zip_archive* pZip, MZ_FILE* pFile, mz_uint64 archive_size, mz_uint flags)
3773{
3774 mz_uint64 cur_file_ofs;
3775
3776 if ((!pZip) || (!pFile))
3777 return mz_zip_set_error(pZip, MZ_ZIP_FILE_OPEN_FAILED);
3778
3779 cur_file_ofs = MZ_FTELL64(pFile);
3780
3781 if (!archive_size) {
3782 if (MZ_FSEEK64(pFile, 0, SEEK_END))
3783 return mz_zip_set_error(pZip, MZ_ZIP_FILE_SEEK_FAILED);
3784
3785 archive_size = MZ_FTELL64(pFile) - cur_file_ofs;
3786
3787 if (archive_size < MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE)
3788 return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE);
3789 }
3790
3791 if (!mz_zip_reader_init_internal(pZip, flags))
3792 return MZ_FALSE;
3793
3794 pZip->m_zip_type = MZ_ZIP_TYPE_CFILE;
3795 pZip->m_pRead = mz_zip_file_read_func;
3796
3797 pZip->m_pIO_opaque = pZip;
3798 pZip->m_pState->m_pFile = pFile;
3799 pZip->m_archive_size = archive_size;
3800 pZip->m_pState->m_file_archive_start_ofs = cur_file_ofs;
3801
3802 if (!mz_zip_reader_read_central_dir(pZip, flags)) {
3803 mz_zip_reader_end_internal(pZip, MZ_FALSE);
3804 return MZ_FALSE;
3805 }
3806
3807 return MZ_TRUE;
3808}
3809
3810#endif /* #ifndef MINIZ_NO_STDIO */
3811
3812static MZ_FORCEINLINE const mz_uint8* mz_zip_get_cdh(mz_zip_archive* pZip, mz_uint file_index)
3813{
3814 if ((!pZip) || (!pZip->m_pState) || (file_index >= pZip->m_total_files))
3815 return NULL;
3816 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));
3817}
3818
3819mz_bool mz_zip_reader_is_file_encrypted(mz_zip_archive* pZip, mz_uint file_index)
3820{
3821 mz_uint m_bit_flag;
3822 const mz_uint8* p = mz_zip_get_cdh(pZip, file_index);
3823 if (!p) {
3824 mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
3825 return MZ_FALSE;
3826 }
3827
3828 m_bit_flag = MZ_READ_LE16(p + MZ_ZIP_CDH_BIT_FLAG_OFS);
3830}
3831
3832mz_bool mz_zip_reader_is_file_supported(mz_zip_archive* pZip, mz_uint file_index)
3833{
3834 mz_uint bit_flag;
3835 mz_uint method;
3836
3837 const mz_uint8* p = mz_zip_get_cdh(pZip, file_index);
3838 if (!p) {
3839 mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
3840 return MZ_FALSE;
3841 }
3842
3843 method = MZ_READ_LE16(p + MZ_ZIP_CDH_METHOD_OFS);
3844 bit_flag = MZ_READ_LE16(p + MZ_ZIP_CDH_BIT_FLAG_OFS);
3845
3846 if ((method != 0) && (method != MZ_DEFLATED)) {
3847 mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_METHOD);
3848 return MZ_FALSE;
3849 }
3850
3852 mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_ENCRYPTION);
3853 return MZ_FALSE;
3854 }
3855
3857 mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_FEATURE);
3858 return MZ_FALSE;
3859 }
3860
3861 return MZ_TRUE;
3862}
3863
3864mz_bool mz_zip_reader_is_file_a_directory(mz_zip_archive* pZip, mz_uint file_index)
3865{
3866 mz_uint filename_len, attribute_mapping_id, external_attr;
3867 const mz_uint8* p = mz_zip_get_cdh(pZip, file_index);
3868 if (!p) {
3869 mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
3870 return MZ_FALSE;
3871 }
3872
3873 filename_len = MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS);
3874 if (filename_len) {
3875 if (*(p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + filename_len - 1) == '/')
3876 return MZ_TRUE;
3877 }
3878
3879 /* Bugfix: This code was also checking if the internal attribute was non-zero, which wasn't correct. */
3880 /* 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. */
3881 /* FIXME: Remove this check? Is it necessary - we already check the filename. */
3882 attribute_mapping_id = MZ_READ_LE16(p + MZ_ZIP_CDH_VERSION_MADE_BY_OFS) >> 8;
3883 (void)attribute_mapping_id;
3884
3885 external_attr = MZ_READ_LE32(p + MZ_ZIP_CDH_EXTERNAL_ATTR_OFS);
3886 if ((external_attr & MZ_ZIP_DOS_DIR_ATTRIBUTE_BITFLAG) != 0) {
3887 return MZ_TRUE;
3888 }
3889
3890 return MZ_FALSE;
3891}
3892
3893static 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)
3894{
3895 mz_uint n;
3896 const mz_uint8* p = pCentral_dir_header;
3897
3898 if (pFound_zip64_extra_data)
3899 *pFound_zip64_extra_data = MZ_FALSE;
3900
3901 if ((!p) || (!pStat))
3902 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
3903
3904 /* Extract fields from the central directory record. */
3905 pStat->m_file_index = file_index;
3906 pStat->m_central_dir_ofs = MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir_offsets, mz_uint32, file_index);
3907 pStat->m_version_made_by = MZ_READ_LE16(p + MZ_ZIP_CDH_VERSION_MADE_BY_OFS);
3908 pStat->m_version_needed = MZ_READ_LE16(p + MZ_ZIP_CDH_VERSION_NEEDED_OFS);
3909 pStat->m_bit_flag = MZ_READ_LE16(p + MZ_ZIP_CDH_BIT_FLAG_OFS);
3910 pStat->m_method = MZ_READ_LE16(p + MZ_ZIP_CDH_METHOD_OFS);
3911#ifndef MINIZ_NO_TIME
3912 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));
3913#endif
3914 pStat->m_crc32 = MZ_READ_LE32(p + MZ_ZIP_CDH_CRC32_OFS);
3915 pStat->m_comp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS);
3916 pStat->m_uncomp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS);
3917 pStat->m_internal_attr = MZ_READ_LE16(p + MZ_ZIP_CDH_INTERNAL_ATTR_OFS);
3918 pStat->m_external_attr = MZ_READ_LE32(p + MZ_ZIP_CDH_EXTERNAL_ATTR_OFS);
3919 pStat->m_local_header_ofs = MZ_READ_LE32(p + MZ_ZIP_CDH_LOCAL_HEADER_OFS);
3920
3921 /* Copy as much of the filename and comment as possible. */
3922 n = MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS);
3923 n = MZ_MIN(n, MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE - 1);
3924 memcpy(pStat->m_filename, p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE, n);
3925 pStat->m_filename[n] = '\0';
3926
3927 n = MZ_READ_LE16(p + MZ_ZIP_CDH_COMMENT_LEN_OFS);
3928 n = MZ_MIN(n, MZ_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE - 1);
3929 pStat->m_comment_size = n;
3930 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);
3931 pStat->m_comment[n] = '\0';
3932
3933 /* Set some flags for convienance */
3934 pStat->m_is_directory = mz_zip_reader_is_file_a_directory(pZip, file_index);
3935 pStat->m_is_encrypted = mz_zip_reader_is_file_encrypted(pZip, file_index);
3936 pStat->m_is_supported = mz_zip_reader_is_file_supported(pZip, file_index);
3937
3938 /* See if we need to read any zip64 extended information fields. */
3939 /* 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). */
3940 if (MZ_MAX(MZ_MAX(pStat->m_comp_size, pStat->m_uncomp_size), pStat->m_local_header_ofs) == MZ_UINT32_MAX) {
3941 /* Attempt to find zip64 extended information field in the entry's extra data */
3942 mz_uint32 extra_size_remaining = MZ_READ_LE16(p + MZ_ZIP_CDH_EXTRA_LEN_OFS);
3943
3944 if (extra_size_remaining) {
3945 const mz_uint8* pExtra_data = p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS);
3946
3947 do {
3948 mz_uint32 field_id;
3949 mz_uint32 field_data_size;
3950
3951 if (extra_size_remaining < (sizeof(mz_uint16) * 2))
3952 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
3953
3954 field_id = MZ_READ_LE16(pExtra_data);
3955 field_data_size = MZ_READ_LE16(pExtra_data + sizeof(mz_uint16));
3956
3957 if ((field_data_size + sizeof(mz_uint16) * 2) > extra_size_remaining)
3958 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
3959
3961 const mz_uint8* pField_data = pExtra_data + sizeof(mz_uint16) * 2;
3962 mz_uint32 field_data_remaining = field_data_size;
3963
3964 if (pFound_zip64_extra_data)
3965 *pFound_zip64_extra_data = MZ_TRUE;
3966
3967 if (pStat->m_uncomp_size == MZ_UINT32_MAX) {
3968 if (field_data_remaining < sizeof(mz_uint64))
3969 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
3970
3971 pStat->m_uncomp_size = MZ_READ_LE64(pField_data);
3972 pField_data += sizeof(mz_uint64);
3973 field_data_remaining -= sizeof(mz_uint64);
3974 }
3975
3976 if (pStat->m_comp_size == MZ_UINT32_MAX) {
3977 if (field_data_remaining < sizeof(mz_uint64))
3978 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
3979
3980 pStat->m_comp_size = MZ_READ_LE64(pField_data);
3981 pField_data += sizeof(mz_uint64);
3982 field_data_remaining -= sizeof(mz_uint64);
3983 }
3984
3985 if (pStat->m_local_header_ofs == MZ_UINT32_MAX) {
3986 if (field_data_remaining < sizeof(mz_uint64))
3987 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
3988
3989 pStat->m_local_header_ofs = MZ_READ_LE64(pField_data);
3990 pField_data += sizeof(mz_uint64);
3991 field_data_remaining -= sizeof(mz_uint64);
3992 }
3993
3994 break;
3995 }
3996
3997 pExtra_data += sizeof(mz_uint16) * 2 + field_data_size;
3998 extra_size_remaining = extra_size_remaining - sizeof(mz_uint16) * 2 - field_data_size;
3999 } while (extra_size_remaining);
4000 }
4001 }
4002
4003 return MZ_TRUE;
4004}
4005
4006static MZ_FORCEINLINE mz_bool mz_zip_string_equal(const char* pA, const char* pB, mz_uint len, mz_uint flags)
4007{
4008 mz_uint i;
4009 if (flags & MZ_ZIP_FLAG_CASE_SENSITIVE)
4010 return 0 == memcmp(pA, pB, len);
4011 for (i = 0; i < len; ++i)
4012 if (MZ_TOLOWER(pA[i]) != MZ_TOLOWER(pB[i]))
4013 return MZ_FALSE;
4014 return MZ_TRUE;
4015}
4016
4017static 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)
4018{
4019 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;
4020 mz_uint l_len = MZ_READ_LE16(pL + MZ_ZIP_CDH_FILENAME_LEN_OFS);
4021 mz_uint8 l = 0, r = 0;
4023 pE = pL + MZ_MIN(l_len, r_len);
4024 while (pL < pE) {
4025 if ((l = MZ_TOLOWER(*pL)) != (r = MZ_TOLOWER(*pR)))
4026 break;
4027 pL++;
4028 pR++;
4029 }
4030 return (pL == pE) ? (int)(l_len - r_len) : (l - r);
4031}
4032
4033static mz_bool mz_zip_locate_file_binary_search(mz_zip_archive* pZip, const char* pFilename, mz_uint32* pIndex)
4034{
4035 mz_zip_internal_state* pState = pZip->m_pState;
4036 const mz_zip_array* pCentral_dir_offsets = &pState->m_central_dir_offsets;
4037 const mz_zip_array* pCentral_dir = &pState->m_central_dir;
4038 mz_uint32* pIndices = &MZ_ZIP_ARRAY_ELEMENT(&pState->m_sorted_central_dir_offsets, mz_uint32, 0);
4039 const mz_uint32 size = pZip->m_total_files;
4040 const mz_uint filename_len = (mz_uint)strlen(pFilename);
4041
4042 if (pIndex)
4043 *pIndex = 0;
4044
4045 if (size) {
4046 /* yes I could use uint32_t's, but then we would have to add some special case checks in the loop, argh, and */
4047 /* honestly the major expense here on 32-bit CPU's will still be the filename compare */
4048 mz_int64 l = 0, h = (mz_int64)size - 1;
4049
4050 while (l <= h) {
4051 mz_int64 m = l + ((h - l) >> 1);
4052 mz_uint32 file_index = pIndices[(mz_uint32)m];
4053
4054 int comp = mz_zip_filename_compare(pCentral_dir, pCentral_dir_offsets, file_index, pFilename, filename_len);
4055 if (!comp) {
4056 if (pIndex)
4057 *pIndex = file_index;
4058 return MZ_TRUE;
4059 } else if (comp < 0)
4060 l = m + 1;
4061 else
4062 h = m - 1;
4063 }
4064 }
4065
4066 return mz_zip_set_error(pZip, MZ_ZIP_FILE_NOT_FOUND);
4067}
4068
4069int mz_zip_reader_locate_file(mz_zip_archive* pZip, const char* pName, const char* pComment, mz_uint flags)
4070{
4071 mz_uint32 index;
4072 if (!mz_zip_reader_locate_file_v2(pZip, pName, pComment, flags, &index))
4073 return -1;
4074 else
4075 return (int)index;
4076}
4077
4078mz_bool mz_zip_reader_locate_file_v2(mz_zip_archive* pZip, const char* pName, const char* pComment, mz_uint flags, mz_uint32* pIndex)
4079{
4080 mz_uint file_index;
4081 size_t name_len, comment_len;
4082
4083 if (pIndex)
4084 *pIndex = 0;
4085
4086 if ((!pZip) || (!pZip->m_pState) || (!pName))
4087 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
4088
4089 /* See if we can use a binary search */
4090 if (((pZip->m_pState->m_init_flags & MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY) == 0) && (pZip->m_zip_mode == MZ_ZIP_MODE_READING) && ((flags & (MZ_ZIP_FLAG_IGNORE_PATH | MZ_ZIP_FLAG_CASE_SENSITIVE)) == 0) && (!pComment) && (pZip->m_pState->m_sorted_central_dir_offsets.m_size)) {
4091 return mz_zip_locate_file_binary_search(pZip, pName, pIndex);
4092 }
4093
4094 /* Locate the entry by scanning the entire central directory */
4095 name_len = strlen(pName);
4096 if (name_len > MZ_UINT16_MAX)
4097 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
4098
4099 comment_len = pComment ? strlen(pComment) : 0;
4100 if (comment_len > MZ_UINT16_MAX)
4101 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
4102
4103 for (file_index = 0; file_index < pZip->m_total_files; file_index++) {
4104 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));
4105 mz_uint filename_len = MZ_READ_LE16(pHeader + MZ_ZIP_CDH_FILENAME_LEN_OFS);
4106 const char* pFilename = (const char*)pHeader + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE;
4107 if (filename_len < name_len)
4108 continue;
4109 if (comment_len) {
4110 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);
4111 const char* pFile_comment = pFilename + filename_len + file_extra_len;
4112 if ((file_comment_len != comment_len) || (!mz_zip_string_equal(pComment, pFile_comment, file_comment_len, flags)))
4113 continue;
4114 }
4115 if ((flags & MZ_ZIP_FLAG_IGNORE_PATH) && (filename_len)) {
4116 int ofs = filename_len - 1;
4117 do {
4118 if ((pFilename[ofs] == '/') || (pFilename[ofs] == '\\') || (pFilename[ofs] == ':'))
4119 break;
4120 } while (--ofs >= 0);
4121 ofs++;
4122 pFilename += ofs;
4123 filename_len -= ofs;
4124 }
4125 if ((filename_len == name_len) && (mz_zip_string_equal(pName, pFilename, filename_len, flags))) {
4126 if (pIndex)
4127 *pIndex = file_index;
4128 return MZ_TRUE;
4129 }
4130 }
4131
4132 return mz_zip_set_error(pZip, MZ_ZIP_FILE_NOT_FOUND);
4133}
4134
4135static 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)
4136{
4137 int status = TINFL_STATUS_DONE;
4138 mz_uint64 needed_size, cur_file_ofs, comp_remaining, out_buf_ofs = 0, read_buf_size, read_buf_ofs = 0, read_buf_avail;
4139 mz_zip_archive_file_stat file_stat;
4140 void* pRead_buf;
4141 mz_uint32 local_header_u32[(MZ_ZIP_LOCAL_DIR_HEADER_SIZE + sizeof(mz_uint32) - 1) / sizeof(mz_uint32)];
4142 mz_uint8* pLocal_header = (mz_uint8*)local_header_u32;
4143 tinfl_decompressor inflator;
4144
4145 if ((!pZip) || (!pZip->m_pState) || ((buf_size) && (!pBuf)) || ((user_read_buf_size) && (!pUser_read_buf)) || (!pZip->m_pRead))
4146 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
4147
4148 if (st) {
4149 file_stat = *st;
4150 } else if (!mz_zip_reader_file_stat(pZip, file_index, &file_stat))
4151 return MZ_FALSE;
4152
4153 /* A directory or zero length file */
4154 if ((file_stat.m_is_directory) || (!file_stat.m_comp_size))
4155 return MZ_TRUE;
4156
4157 /* Encryption and patch files are not supported. */
4159 return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_ENCRYPTION);
4160
4161 /* This function only supports decompressing stored and deflate. */
4162 if ((!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (file_stat.m_method != 0) && (file_stat.m_method != MZ_DEFLATED))
4163 return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_METHOD);
4164
4165 /* Ensure supplied output buffer is large enough. */
4166 needed_size = (flags & MZ_ZIP_FLAG_COMPRESSED_DATA) ? file_stat.m_comp_size : file_stat.m_uncomp_size;
4167 if (buf_size < needed_size)
4168 return mz_zip_set_error(pZip, MZ_ZIP_BUF_TOO_SMALL);
4169
4170 /* Read and parse the local directory entry. */
4171 cur_file_ofs = file_stat.m_local_header_ofs;
4172 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)
4173 return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
4174
4175 if (MZ_READ_LE32(pLocal_header) != MZ_ZIP_LOCAL_DIR_HEADER_SIG)
4176 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
4177
4178 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);
4179 if ((cur_file_ofs + file_stat.m_comp_size) > pZip->m_archive_size)
4180 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
4181
4182 if ((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!file_stat.m_method)) {
4183 /* The file is stored or the caller has requested the compressed data. */
4184 if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pBuf, (size_t)needed_size) != needed_size)
4185 return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
4186
4187#ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
4188 if ((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) == 0) {
4189 if (mz_crc32(MZ_CRC32_INIT, (const mz_uint8*)pBuf, (size_t)file_stat.m_uncomp_size) != file_stat.m_crc32)
4190 return mz_zip_set_error(pZip, MZ_ZIP_CRC_CHECK_FAILED);
4191 }
4192#endif
4193
4194 return MZ_TRUE;
4195 }
4196
4197 /* Decompress the file either directly from memory or from a file input buffer. */
4198 tinfl_init(&inflator);
4199
4200 if (pZip->m_pState->m_pMem) {
4201 /* Read directly from the archive in memory. */
4202 pRead_buf = (mz_uint8*)pZip->m_pState->m_pMem + cur_file_ofs;
4203 read_buf_size = read_buf_avail = file_stat.m_comp_size;
4204 comp_remaining = 0;
4205 } else if (pUser_read_buf) {
4206 /* Use a user provided read buffer. */
4207 if (!user_read_buf_size)
4208 return MZ_FALSE;
4209 pRead_buf = (mz_uint8*)pUser_read_buf;
4210 read_buf_size = user_read_buf_size;
4211 read_buf_avail = 0;
4212 comp_remaining = file_stat.m_comp_size;
4213 } else {
4214 /* Temporarily allocate a read buffer. */
4215 read_buf_size = MZ_MIN(file_stat.m_comp_size, (mz_uint64)MZ_ZIP_MAX_IO_BUF_SIZE);
4216 if (((sizeof(size_t) == sizeof(mz_uint32))) && (read_buf_size > 0x7FFFFFFF))
4217 return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);
4218
4219 if (NULL == (pRead_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, (size_t)read_buf_size)))
4220 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
4221
4222 read_buf_avail = 0;
4223 comp_remaining = file_stat.m_comp_size;
4224 }
4225
4226 do {
4227 /* The size_t cast here should be OK because we've verified that the output buffer is >= file_stat.m_uncomp_size above */
4228 size_t in_buf_size, out_buf_size = (size_t)(file_stat.m_uncomp_size - out_buf_ofs);
4229 if ((!read_buf_avail) && (!pZip->m_pState->m_pMem)) {
4230 read_buf_avail = MZ_MIN(read_buf_size, comp_remaining);
4231 if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pRead_buf, (size_t)read_buf_avail) != read_buf_avail) {
4232 status = TINFL_STATUS_FAILED;
4233 mz_zip_set_error(pZip, MZ_ZIP_DECOMPRESSION_FAILED);
4234 break;
4235 }
4236 cur_file_ofs += read_buf_avail;
4237 comp_remaining -= read_buf_avail;
4238 read_buf_ofs = 0;
4239 }
4240 in_buf_size = (size_t)read_buf_avail;
4241 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));
4242 read_buf_avail -= in_buf_size;
4243 read_buf_ofs += in_buf_size;
4244 out_buf_ofs += out_buf_size;
4245 } while (status == TINFL_STATUS_NEEDS_MORE_INPUT);
4246
4247 if (status == TINFL_STATUS_DONE) {
4248 /* Make sure the entire file was decompressed, and check its CRC. */
4249 if (out_buf_ofs != file_stat.m_uncomp_size) {
4250 mz_zip_set_error(pZip, MZ_ZIP_UNEXPECTED_DECOMPRESSED_SIZE);
4251 status = TINFL_STATUS_FAILED;
4252 }
4253#ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
4254 else if (mz_crc32(MZ_CRC32_INIT, (const mz_uint8*)pBuf, (size_t)file_stat.m_uncomp_size) != file_stat.m_crc32) {
4255 mz_zip_set_error(pZip, MZ_ZIP_CRC_CHECK_FAILED);
4256 status = TINFL_STATUS_FAILED;
4257 }
4258#endif
4259 }
4260
4261 if ((!pZip->m_pState->m_pMem) && (!pUser_read_buf))
4262 pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf);
4263
4264 return status == TINFL_STATUS_DONE;
4265}
4266
4267mz_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)
4268{
4269 return mz_zip_reader_extract_to_mem_no_alloc1(pZip, file_index, pBuf, buf_size, flags, pUser_read_buf, user_read_buf_size, NULL);
4270}
4271
4272mz_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)
4273{
4274 mz_uint32 file_index;
4275 if (!mz_zip_reader_locate_file_v2(pZip, pFilename, NULL, flags, &file_index))
4276 return MZ_FALSE;
4277 return mz_zip_reader_extract_to_mem_no_alloc1(pZip, file_index, pBuf, buf_size, flags, pUser_read_buf, user_read_buf_size, NULL);
4278}
4279
4280mz_bool mz_zip_reader_extract_to_mem(mz_zip_archive* pZip, mz_uint file_index, void* pBuf, size_t buf_size, mz_uint flags)
4281{
4282 return mz_zip_reader_extract_to_mem_no_alloc1(pZip, file_index, pBuf, buf_size, flags, NULL, 0, NULL);
4283}
4284
4285mz_bool mz_zip_reader_extract_file_to_mem(mz_zip_archive* pZip, const char* pFilename, void* pBuf, size_t buf_size, mz_uint flags)
4286{
4287 return mz_zip_reader_extract_file_to_mem_no_alloc(pZip, pFilename, pBuf, buf_size, flags, NULL, 0);
4288}
4289
4290void* mz_zip_reader_extract_to_heap(mz_zip_archive* pZip, mz_uint file_index, size_t* pSize, mz_uint flags)
4291{
4292 mz_zip_archive_file_stat file_stat;
4293 mz_uint64 alloc_size;
4294 void* pBuf;
4295
4296 if (pSize)
4297 *pSize = 0;
4298
4299 if (!mz_zip_reader_file_stat(pZip, file_index, &file_stat))
4300 return NULL;
4301
4302 alloc_size = (flags & MZ_ZIP_FLAG_COMPRESSED_DATA) ? file_stat.m_comp_size : file_stat.m_uncomp_size;
4303 if (((sizeof(size_t) == sizeof(mz_uint32))) && (alloc_size > 0x7FFFFFFF)) {
4304 mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);
4305 return NULL;
4306 }
4307
4308 if (NULL == (pBuf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, (size_t)alloc_size))) {
4309 mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
4310 return NULL;
4311 }
4312
4313 if (!mz_zip_reader_extract_to_mem_no_alloc1(pZip, file_index, pBuf, (size_t)alloc_size, flags, NULL, 0, &file_stat)) {
4314 pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf);
4315 return NULL;
4316 }
4317
4318 if (pSize)
4319 *pSize = (size_t)alloc_size;
4320 return pBuf;
4321}
4322
4323void* mz_zip_reader_extract_file_to_heap(mz_zip_archive* pZip, const char* pFilename, size_t* pSize, mz_uint flags)
4324{
4325 mz_uint32 file_index;
4326 if (!mz_zip_reader_locate_file_v2(pZip, pFilename, NULL, flags, &file_index)) {
4327 if (pSize)
4328 *pSize = 0;
4329 return MZ_FALSE;
4330 }
4331 return mz_zip_reader_extract_to_heap(pZip, file_index, pSize, flags);
4332}
4333
4334mz_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)
4335{
4336 int status = TINFL_STATUS_DONE;
4337#ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
4338 mz_uint file_crc32 = MZ_CRC32_INIT;
4339#endif
4340 mz_uint64 read_buf_size, read_buf_ofs = 0, read_buf_avail, comp_remaining, out_buf_ofs = 0, cur_file_ofs;
4341 mz_zip_archive_file_stat file_stat;
4342 void* pRead_buf = NULL;
4343 void* pWrite_buf = NULL;
4344 mz_uint32 local_header_u32[(MZ_ZIP_LOCAL_DIR_HEADER_SIZE + sizeof(mz_uint32) - 1) / sizeof(mz_uint32)];
4345 mz_uint8* pLocal_header = (mz_uint8*)local_header_u32;
4346
4347 if ((!pZip) || (!pZip->m_pState) || (!pCallback) || (!pZip->m_pRead))
4348 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
4349
4350 if (!mz_zip_reader_file_stat(pZip, file_index, &file_stat))
4351 return MZ_FALSE;
4352
4353 /* A directory or zero length file */
4354 if ((file_stat.m_is_directory) || (!file_stat.m_comp_size))
4355 return MZ_TRUE;
4356
4357 /* Encryption and patch files are not supported. */
4359 return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_ENCRYPTION);
4360
4361 /* This function only supports decompressing stored and deflate. */
4362 if ((!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (file_stat.m_method != 0) && (file_stat.m_method != MZ_DEFLATED))
4363 return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_METHOD);
4364
4365 /* 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) */
4366 cur_file_ofs = file_stat.m_local_header_ofs;
4367 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)
4368 return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
4369
4370 if (MZ_READ_LE32(pLocal_header) != MZ_ZIP_LOCAL_DIR_HEADER_SIG)
4371 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
4372
4373 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);
4374 if ((cur_file_ofs + file_stat.m_comp_size) > pZip->m_archive_size)
4375 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
4376
4377 /* Decompress the file either directly from memory or from a file input buffer. */
4378 if (pZip->m_pState->m_pMem) {
4379 pRead_buf = (mz_uint8*)pZip->m_pState->m_pMem + cur_file_ofs;
4380 read_buf_size = read_buf_avail = file_stat.m_comp_size;
4381 comp_remaining = 0;
4382 } else {
4383 read_buf_size = MZ_MIN(file_stat.m_comp_size, (mz_uint64)MZ_ZIP_MAX_IO_BUF_SIZE);
4384 if (NULL == (pRead_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, (size_t)read_buf_size)))
4385 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
4386
4387 read_buf_avail = 0;
4388 comp_remaining = file_stat.m_comp_size;
4389 }
4390
4391 if ((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!file_stat.m_method)) {
4392 /* The file is stored or the caller has requested the compressed data. */
4393 if (pZip->m_pState->m_pMem) {
4394 if (((sizeof(size_t) == sizeof(mz_uint32))) && (file_stat.m_comp_size > MZ_UINT32_MAX))
4395 return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);
4396
4397 if (pCallback(pOpaque, out_buf_ofs, pRead_buf, (size_t)file_stat.m_comp_size) != file_stat.m_comp_size) {
4398 mz_zip_set_error(pZip, MZ_ZIP_WRITE_CALLBACK_FAILED);
4399 status = TINFL_STATUS_FAILED;
4400 } else if (!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) {
4401#ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
4402 file_crc32 = (mz_uint32)mz_crc32(file_crc32, (const mz_uint8*)pRead_buf, (size_t)file_stat.m_comp_size);
4403#endif
4404 }
4405
4406 cur_file_ofs += file_stat.m_comp_size;
4407 out_buf_ofs += file_stat.m_comp_size;
4408 comp_remaining = 0;
4409 } else {
4410 while (comp_remaining) {
4411 read_buf_avail = MZ_MIN(read_buf_size, comp_remaining);
4412 if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pRead_buf, (size_t)read_buf_avail) != read_buf_avail) {
4413 mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
4414 status = TINFL_STATUS_FAILED;
4415 break;
4416 }
4417
4418#ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
4419 if (!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) {
4420 file_crc32 = (mz_uint32)mz_crc32(file_crc32, (const mz_uint8*)pRead_buf, (size_t)read_buf_avail);
4421 }
4422#endif
4423
4424 if (pCallback(pOpaque, out_buf_ofs, pRead_buf, (size_t)read_buf_avail) != read_buf_avail) {
4425 mz_zip_set_error(pZip, MZ_ZIP_WRITE_CALLBACK_FAILED);
4426 status = TINFL_STATUS_FAILED;
4427 break;
4428 }
4429
4430 cur_file_ofs += read_buf_avail;
4431 out_buf_ofs += read_buf_avail;
4432 comp_remaining -= read_buf_avail;
4433 }
4434 }
4435 } else {
4436 tinfl_decompressor inflator;
4437 tinfl_init(&inflator);
4438
4439 if (NULL == (pWrite_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, TINFL_LZ_DICT_SIZE))) {
4440 mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
4441 status = TINFL_STATUS_FAILED;
4442 } else {
4443 do {
4444 mz_uint8* pWrite_buf_cur = (mz_uint8*)pWrite_buf + (out_buf_ofs & (TINFL_LZ_DICT_SIZE - 1));
4445 size_t in_buf_size, out_buf_size = TINFL_LZ_DICT_SIZE - (out_buf_ofs & (TINFL_LZ_DICT_SIZE - 1));
4446 if ((!read_buf_avail) && (!pZip->m_pState->m_pMem)) {
4447 read_buf_avail = MZ_MIN(read_buf_size, comp_remaining);
4448 if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pRead_buf, (size_t)read_buf_avail) != read_buf_avail) {
4449 mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
4450 status = TINFL_STATUS_FAILED;
4451 break;
4452 }
4453 cur_file_ofs += read_buf_avail;
4454 comp_remaining -= read_buf_avail;
4455 read_buf_ofs = 0;
4456 }
4457
4458 in_buf_size = (size_t)read_buf_avail;
4459 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);
4460 read_buf_avail -= in_buf_size;
4461 read_buf_ofs += in_buf_size;
4462
4463 if (out_buf_size) {
4464 if (pCallback(pOpaque, out_buf_ofs, pWrite_buf_cur, out_buf_size) != out_buf_size) {
4465 mz_zip_set_error(pZip, MZ_ZIP_WRITE_CALLBACK_FAILED);
4466 status = TINFL_STATUS_FAILED;
4467 break;
4468 }
4469
4470#ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
4471 file_crc32 = (mz_uint32)mz_crc32(file_crc32, pWrite_buf_cur, out_buf_size);
4472#endif
4473 if ((out_buf_ofs += out_buf_size) > file_stat.m_uncomp_size) {
4474 mz_zip_set_error(pZip, MZ_ZIP_DECOMPRESSION_FAILED);
4475 status = TINFL_STATUS_FAILED;
4476 break;
4477 }
4478 }
4479 } while ((status == TINFL_STATUS_NEEDS_MORE_INPUT) || (status == TINFL_STATUS_HAS_MORE_OUTPUT));
4480 }
4481 }
4482
4483 if ((status == TINFL_STATUS_DONE) && (!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA))) {
4484 /* Make sure the entire file was decompressed, and check its CRC. */
4485 if (out_buf_ofs != file_stat.m_uncomp_size) {
4486 mz_zip_set_error(pZip, MZ_ZIP_UNEXPECTED_DECOMPRESSED_SIZE);
4487 status = TINFL_STATUS_FAILED;
4488 }
4489#ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
4490 else if (file_crc32 != file_stat.m_crc32) {
4491 mz_zip_set_error(pZip, MZ_ZIP_DECOMPRESSION_FAILED);
4492 status = TINFL_STATUS_FAILED;
4493 }
4494#endif
4495 }
4496
4497 if (!pZip->m_pState->m_pMem)
4498 pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf);
4499
4500 if (pWrite_buf)
4501 pZip->m_pFree(pZip->m_pAlloc_opaque, pWrite_buf);
4502
4503 return status == TINFL_STATUS_DONE;
4504}
4505
4506mz_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)
4507{
4508 mz_uint32 file_index;
4509 if (!mz_zip_reader_locate_file_v2(pZip, pFilename, NULL, flags, &file_index))
4510 return MZ_FALSE;
4511
4512 return mz_zip_reader_extract_to_callback(pZip, file_index, pCallback, pOpaque, flags);
4513}
4514
4515mz_zip_reader_extract_iter_state* mz_zip_reader_extract_iter_new(mz_zip_archive* pZip, mz_uint file_index, mz_uint flags)
4516{
4517 mz_zip_reader_extract_iter_state* pState;
4518 mz_uint32 local_header_u32[(MZ_ZIP_LOCAL_DIR_HEADER_SIZE + sizeof(mz_uint32) - 1) / sizeof(mz_uint32)];
4519 mz_uint8* pLocal_header = (mz_uint8*)local_header_u32;
4520
4521 /* Argument sanity check */
4522 if ((!pZip) || (!pZip->m_pState))
4523 return NULL;
4524
4525 /* Allocate an iterator status structure */
4526 pState = (mz_zip_reader_extract_iter_state*)pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, sizeof(mz_zip_reader_extract_iter_state));
4527 if (!pState) {
4528 mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
4529 return NULL;
4530 }
4531
4532 /* Fetch file details */
4533 if (!mz_zip_reader_file_stat(pZip, file_index, &pState->file_stat)) {
4534 pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
4535 return NULL;
4536 }
4537
4538 /* Encryption and patch files are not supported. */
4540 mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_ENCRYPTION);
4541 pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
4542 return NULL;
4543 }
4544
4545 /* This function only supports decompressing stored and deflate. */
4546 if ((!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (pState->file_stat.m_method != 0) && (pState->file_stat.m_method != MZ_DEFLATED)) {
4547 mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_METHOD);
4548 pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
4549 return NULL;
4550 }
4551
4552 /* Init state - save args */
4553 pState->pZip = pZip;
4554 pState->flags = flags;
4555
4556 /* Init state - reset variables to defaults */
4557 pState->status = TINFL_STATUS_DONE;
4558#ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
4559 pState->file_crc32 = MZ_CRC32_INIT;
4560#endif
4561 pState->read_buf_ofs = 0;
4562 pState->out_buf_ofs = 0;
4563 pState->pRead_buf = NULL;
4564 pState->pWrite_buf = NULL;
4565 pState->out_blk_remain = 0;
4566
4567 /* Read and parse the local directory entry. */
4568 pState->cur_file_ofs = pState->file_stat.m_local_header_ofs;
4569 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) {
4570 mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
4571 pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
4572 return NULL;
4573 }
4574
4575 if (MZ_READ_LE32(pLocal_header) != MZ_ZIP_LOCAL_DIR_HEADER_SIG) {
4576 mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
4577 pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
4578 return NULL;
4579 }
4580
4581 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);
4582 if ((pState->cur_file_ofs + pState->file_stat.m_comp_size) > pZip->m_archive_size) {
4583 mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
4584 pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
4585 return NULL;
4586 }
4587
4588 /* Decompress the file either directly from memory or from a file input buffer. */
4589 if (pZip->m_pState->m_pMem) {
4590 pState->pRead_buf = (mz_uint8*)pZip->m_pState->m_pMem + pState->cur_file_ofs;
4591 pState->read_buf_size = pState->read_buf_avail = pState->file_stat.m_comp_size;
4592 pState->comp_remaining = pState->file_stat.m_comp_size;
4593 } else {
4594 if (!((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!pState->file_stat.m_method))) {
4595 /* Decompression required, therefore intermediate read buffer required */
4596 pState->read_buf_size = MZ_MIN(pState->file_stat.m_comp_size, (mz_uint64)MZ_ZIP_MAX_IO_BUF_SIZE);
4597 if (NULL == (pState->pRead_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, (size_t)pState->read_buf_size))) {
4598 mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
4599 pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
4600 return NULL;
4601 }
4602 } else {
4603 /* Decompression not required - we will be reading directly into user buffer, no temp buf required */
4604 pState->read_buf_size = 0;
4605 }
4606 pState->read_buf_avail = 0;
4607 pState->comp_remaining = pState->file_stat.m_comp_size;
4608 }
4609
4610 if (!((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!pState->file_stat.m_method))) {
4611 /* Decompression required, init decompressor */
4612 tinfl_init(&pState->inflator);
4613
4614 /* Allocate write buffer */
4615 if (NULL == (pState->pWrite_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, TINFL_LZ_DICT_SIZE))) {
4616 mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
4617 if (pState->pRead_buf)
4618 pZip->m_pFree(pZip->m_pAlloc_opaque, pState->pRead_buf);
4619 pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
4620 return NULL;
4621 }
4622 }
4623
4624 return pState;
4625}
4626
4627mz_zip_reader_extract_iter_state* mz_zip_reader_extract_file_iter_new(mz_zip_archive* pZip, const char* pFilename, mz_uint flags)
4628{
4629 mz_uint32 file_index;
4630
4631 /* Locate file index by name */
4632 if (!mz_zip_reader_locate_file_v2(pZip, pFilename, NULL, flags, &file_index))
4633 return NULL;
4634
4635 /* Construct iterator */
4636 return mz_zip_reader_extract_iter_new(pZip, file_index, flags);
4637}
4638
4639size_t mz_zip_reader_extract_iter_read(mz_zip_reader_extract_iter_state* pState, void* pvBuf, size_t buf_size)
4640{
4641 size_t copied_to_caller = 0;
4642
4643 /* Argument sanity check */
4644 if ((!pState) || (!pState->pZip) || (!pState->pZip->m_pState) || (!pvBuf))
4645 return 0;
4646
4647 if ((pState->flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!pState->file_stat.m_method)) {
4648 /* The file is stored or the caller has requested the compressed data, calc amount to return. */
4649 copied_to_caller = (size_t)MZ_MIN(buf_size, pState->comp_remaining);
4650
4651 /* Zip is in memory....or requires reading from a file? */
4652 if (pState->pZip->m_pState->m_pMem) {
4653 /* Copy data to caller's buffer */
4654 memcpy(pvBuf, pState->pRead_buf, copied_to_caller);
4655 pState->pRead_buf = ((mz_uint8*)pState->pRead_buf) + copied_to_caller;
4656 } else {
4657 /* Read directly into caller's buffer */
4658 if (pState->pZip->m_pRead(pState->pZip->m_pIO_opaque, pState->cur_file_ofs, pvBuf, copied_to_caller) != copied_to_caller) {
4659 /* Failed to read all that was asked for, flag failure and alert user */
4660 mz_zip_set_error(pState->pZip, MZ_ZIP_FILE_READ_FAILED);
4661 pState->status = TINFL_STATUS_FAILED;
4662 copied_to_caller = 0;
4663 }
4664 }
4665
4666#ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
4667 /* Compute CRC if not returning compressed data only */
4668 if (!(pState->flags & MZ_ZIP_FLAG_COMPRESSED_DATA))
4669 pState->file_crc32 = (mz_uint32)mz_crc32(pState->file_crc32, (const mz_uint8*)pvBuf, copied_to_caller);
4670#endif
4671
4672 /* Advance offsets, dec counters */
4673 pState->cur_file_ofs += copied_to_caller;
4674 pState->out_buf_ofs += copied_to_caller;
4675 pState->comp_remaining -= copied_to_caller;
4676 } else {
4677 do {
4678 /* Calc ptr to write buffer - given current output pos and block size */
4679 mz_uint8* pWrite_buf_cur = (mz_uint8*)pState->pWrite_buf + (pState->out_buf_ofs & (TINFL_LZ_DICT_SIZE - 1));
4680
4681 /* Calc max output size - given current output pos and block size */
4682 size_t in_buf_size, out_buf_size = TINFL_LZ_DICT_SIZE - (pState->out_buf_ofs & (TINFL_LZ_DICT_SIZE - 1));
4683
4684 if (!pState->out_blk_remain) {
4685 /* Read more data from file if none available (and reading from file) */
4686 if ((!pState->read_buf_avail) && (!pState->pZip->m_pState->m_pMem)) {
4687 /* Calc read size */
4688 pState->read_buf_avail = MZ_MIN(pState->read_buf_size, pState->comp_remaining);
4689 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) {
4690 mz_zip_set_error(pState->pZip, MZ_ZIP_FILE_READ_FAILED);
4691 pState->status = TINFL_STATUS_FAILED;
4692 break;
4693 }
4694
4695 /* Advance offsets, dec counters */
4696 pState->cur_file_ofs += pState->read_buf_avail;
4697 pState->comp_remaining -= pState->read_buf_avail;
4698 pState->read_buf_ofs = 0;
4699 }
4700
4701 /* Perform decompression */
4702 in_buf_size = (size_t)pState->read_buf_avail;
4703 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);
4704 pState->read_buf_avail -= in_buf_size;
4705 pState->read_buf_ofs += in_buf_size;
4706
4707 /* Update current output block size remaining */
4708 pState->out_blk_remain = out_buf_size;
4709 }
4710
4711 if (pState->out_blk_remain) {
4712 /* Calc amount to return. */
4713 size_t to_copy = MZ_MIN((buf_size - copied_to_caller), pState->out_blk_remain);
4714
4715 /* Copy data to caller's buffer */
4716 memcpy((mz_uint8*)pvBuf + copied_to_caller, pWrite_buf_cur, to_copy);
4717
4718#ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
4719 /* Perform CRC */
4720 pState->file_crc32 = (mz_uint32)mz_crc32(pState->file_crc32, pWrite_buf_cur, to_copy);
4721#endif
4722
4723 /* Decrement data consumed from block */
4724 pState->out_blk_remain -= to_copy;
4725
4726 /* Inc output offset, while performing sanity check */
4727 if ((pState->out_buf_ofs += to_copy) > pState->file_stat.m_uncomp_size) {
4728 mz_zip_set_error(pState->pZip, MZ_ZIP_DECOMPRESSION_FAILED);
4729 pState->status = TINFL_STATUS_FAILED;
4730 break;
4731 }
4732
4733 /* Increment counter of data copied to caller */
4734 copied_to_caller += to_copy;
4735 }
4736 } while ((copied_to_caller < buf_size) && ((pState->status == TINFL_STATUS_NEEDS_MORE_INPUT) || (pState->status == TINFL_STATUS_HAS_MORE_OUTPUT)));
4737 }
4738
4739 /* Return how many bytes were copied into user buffer */
4740 return copied_to_caller;
4741}
4742
4743mz_bool mz_zip_reader_extract_iter_free(mz_zip_reader_extract_iter_state* pState)
4744{
4745 int status;
4746
4747 /* Argument sanity check */
4748 if ((!pState) || (!pState->pZip) || (!pState->pZip->m_pState))
4749 return MZ_FALSE;
4750
4751 /* Was decompression completed and requested? */
4752 if ((pState->status == TINFL_STATUS_DONE) && (!(pState->flags & MZ_ZIP_FLAG_COMPRESSED_DATA))) {
4753 /* Make sure the entire file was decompressed, and check its CRC. */
4754 if (pState->out_buf_ofs != pState->file_stat.m_uncomp_size) {
4755 mz_zip_set_error(pState->pZip, MZ_ZIP_UNEXPECTED_DECOMPRESSED_SIZE);
4756 pState->status = TINFL_STATUS_FAILED;
4757 }
4758#ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
4759 else if (pState->file_crc32 != pState->file_stat.m_crc32) {
4760 mz_zip_set_error(pState->pZip, MZ_ZIP_DECOMPRESSION_FAILED);
4761 pState->status = TINFL_STATUS_FAILED;
4762 }
4763#endif
4764 }
4765
4766 /* Free buffers */
4767 if (!pState->pZip->m_pState->m_pMem)
4768 pState->pZip->m_pFree(pState->pZip->m_pAlloc_opaque, pState->pRead_buf);
4769 if (pState->pWrite_buf)
4770 pState->pZip->m_pFree(pState->pZip->m_pAlloc_opaque, pState->pWrite_buf);
4771
4772 /* Save status */
4773 status = pState->status;
4774
4775 /* Free context */
4776 pState->pZip->m_pFree(pState->pZip->m_pAlloc_opaque, pState);
4777
4778 return status == TINFL_STATUS_DONE;
4779}
4780
4781#ifndef MINIZ_NO_STDIO
4782static size_t mz_zip_file_write_callback(void* pOpaque, mz_uint64 ofs, const void* pBuf, size_t n)
4783{
4784 (void)ofs;
4785
4786 return MZ_FWRITE(pBuf, 1, n, (MZ_FILE*)pOpaque);
4787}
4788
4789mz_bool mz_zip_reader_extract_to_file(mz_zip_archive* pZip, mz_uint file_index, const char* pDst_filename, mz_uint flags)
4790{
4791 mz_bool status;
4792 mz_zip_archive_file_stat file_stat;
4793 MZ_FILE* pFile;
4794
4795 if (!mz_zip_reader_file_stat(pZip, file_index, &file_stat))
4796 return MZ_FALSE;
4797
4798 if ((file_stat.m_is_directory) || (!file_stat.m_is_supported))
4799 return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_FEATURE);
4800
4801 pFile = MZ_FOPEN(pDst_filename, "wb");
4802 if (!pFile)
4803 return mz_zip_set_error(pZip, MZ_ZIP_FILE_OPEN_FAILED);
4804
4805 status = mz_zip_reader_extract_to_callback(pZip, file_index, mz_zip_file_write_callback, pFile, flags);
4806
4807 if (MZ_FCLOSE(pFile) == EOF) {
4808 if (status)
4809 mz_zip_set_error(pZip, MZ_ZIP_FILE_CLOSE_FAILED);
4810
4811 status = MZ_FALSE;
4812 }
4813
4814#if !defined(MINIZ_NO_TIME) && !defined(MINIZ_NO_STDIO)
4815 if (status)
4816 mz_zip_set_file_times(pDst_filename, file_stat.m_time, file_stat.m_time);
4817#endif
4818
4819 return status;
4820}
4821
4822mz_bool mz_zip_reader_extract_file_to_file(mz_zip_archive* pZip, const char* pArchive_filename, const char* pDst_filename, mz_uint flags)
4823{
4824 mz_uint32 file_index;
4825 if (!mz_zip_reader_locate_file_v2(pZip, pArchive_filename, NULL, flags, &file_index))
4826 return MZ_FALSE;
4827
4828 return mz_zip_reader_extract_to_file(pZip, file_index, pDst_filename, flags);
4829}
4830
4831mz_bool mz_zip_reader_extract_to_cfile(mz_zip_archive* pZip, mz_uint file_index, MZ_FILE* pFile, mz_uint flags)
4832{
4833 mz_zip_archive_file_stat file_stat;
4834
4835 if (!mz_zip_reader_file_stat(pZip, file_index, &file_stat))
4836 return MZ_FALSE;
4837
4838 if ((file_stat.m_is_directory) || (!file_stat.m_is_supported))
4839 return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_FEATURE);
4840
4841 return mz_zip_reader_extract_to_callback(pZip, file_index, mz_zip_file_write_callback, pFile, flags);
4842}
4843
4844mz_bool mz_zip_reader_extract_file_to_cfile(mz_zip_archive* pZip, const char* pArchive_filename, MZ_FILE* pFile, mz_uint flags)
4845{
4846 mz_uint32 file_index;
4847 if (!mz_zip_reader_locate_file_v2(pZip, pArchive_filename, NULL, flags, &file_index))
4848 return MZ_FALSE;
4849
4850 return mz_zip_reader_extract_to_cfile(pZip, file_index, pFile, flags);
4851}
4852#endif /* #ifndef MINIZ_NO_STDIO */
4853
4854static size_t mz_zip_compute_crc32_callback(void* pOpaque, mz_uint64 file_ofs, const void* pBuf, size_t n)
4855{
4856 mz_uint32* p = (mz_uint32*)pOpaque;
4857 (void)file_ofs;
4858 *p = (mz_uint32)mz_crc32(*p, (const mz_uint8*)pBuf, n);
4859 return n;
4860}
4861
4862mz_bool mz_zip_validate_file(mz_zip_archive* pZip, mz_uint file_index, mz_uint flags)
4863{
4864 mz_zip_archive_file_stat file_stat;
4865 mz_zip_internal_state* pState;
4866 const mz_uint8* pCentral_dir_header;
4867 mz_bool found_zip64_ext_data_in_cdir = MZ_FALSE;
4868 mz_bool found_zip64_ext_data_in_ldir = MZ_FALSE;
4869 mz_uint32 local_header_u32[(MZ_ZIP_LOCAL_DIR_HEADER_SIZE + sizeof(mz_uint32) - 1) / sizeof(mz_uint32)];
4870 mz_uint8* pLocal_header = (mz_uint8*)local_header_u32;
4871 mz_uint64 local_header_ofs = 0;
4872 mz_uint32 local_header_filename_len, local_header_extra_len, local_header_crc32;
4873 mz_uint64 local_header_comp_size, local_header_uncomp_size;
4874 mz_uint32 uncomp_crc32 = MZ_CRC32_INIT;
4875 mz_bool has_data_descriptor;
4876 mz_uint32 local_header_bit_flags;
4877
4878 mz_zip_array file_data_array;
4879 mz_zip_array_init(&file_data_array, 1);
4880
4881 if ((!pZip) || (!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || (!pZip->m_pRead))
4882 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
4883
4884 if (file_index > pZip->m_total_files)
4885 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
4886
4887 pState = pZip->m_pState;
4888
4889 pCentral_dir_header = mz_zip_get_cdh(pZip, file_index);
4890
4891 if (!mz_zip_file_stat_internal(pZip, file_index, pCentral_dir_header, &file_stat, &found_zip64_ext_data_in_cdir))
4892 return MZ_FALSE;
4893
4894 /* A directory or zero length file */
4895 if ((file_stat.m_is_directory) || (!file_stat.m_uncomp_size))
4896 return MZ_TRUE;
4897
4898 /* Encryption and patch files are not supported. */
4899 if (file_stat.m_is_encrypted)
4900 return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_ENCRYPTION);
4901
4902 /* This function only supports stored and deflate. */
4903 if ((file_stat.m_method != 0) && (file_stat.m_method != MZ_DEFLATED))
4904 return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_METHOD);
4905
4906 if (!file_stat.m_is_supported)
4907 return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_FEATURE);
4908
4909 /* Read and parse the local directory entry. */
4910 local_header_ofs = file_stat.m_local_header_ofs;
4911 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)
4912 return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
4913
4914 if (MZ_READ_LE32(pLocal_header) != MZ_ZIP_LOCAL_DIR_HEADER_SIG)
4915 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
4916
4917 local_header_filename_len = MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_FILENAME_LEN_OFS);
4918 local_header_extra_len = MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_EXTRA_LEN_OFS);
4919 local_header_comp_size = MZ_READ_LE32(pLocal_header + MZ_ZIP_LDH_COMPRESSED_SIZE_OFS);
4920 local_header_uncomp_size = MZ_READ_LE32(pLocal_header + MZ_ZIP_LDH_DECOMPRESSED_SIZE_OFS);
4921 local_header_crc32 = MZ_READ_LE32(pLocal_header + MZ_ZIP_LDH_CRC32_OFS);
4922 local_header_bit_flags = MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_BIT_FLAG_OFS);
4923 has_data_descriptor = (local_header_bit_flags & 8) != 0;
4924
4925 if (local_header_filename_len != strlen(file_stat.m_filename))
4926 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
4927
4928 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)
4929 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
4930
4931 if (!mz_zip_array_resize(pZip, &file_data_array, MZ_MAX(local_header_filename_len, local_header_extra_len), MZ_FALSE)) {
4932 mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
4933 goto handle_failure;
4934 }
4935
4936 if (local_header_filename_len) {
4937 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) {
4938 mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
4939 goto handle_failure;
4940 }
4941
4942 /* 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. */
4943 if (memcmp(file_stat.m_filename, file_data_array.m_p, local_header_filename_len) != 0) {
4944 mz_zip_set_error(pZip, MZ_ZIP_VALIDATION_FAILED);
4945 goto handle_failure;
4946 }
4947 }
4948
4949 if ((local_header_extra_len) && ((local_header_comp_size == MZ_UINT32_MAX) || (local_header_uncomp_size == MZ_UINT32_MAX))) {
4950 mz_uint32 extra_size_remaining = local_header_extra_len;
4951 const mz_uint8* pExtra_data = (const mz_uint8*)file_data_array.m_p;
4952
4953 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) {
4954 mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
4955 goto handle_failure;
4956 }
4957
4958 do {
4959 mz_uint32 field_id, field_data_size, field_total_size;
4960
4961 if (extra_size_remaining < (sizeof(mz_uint16) * 2)) {
4962 mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
4963 goto handle_failure;
4964 }
4965
4966 field_id = MZ_READ_LE16(pExtra_data);
4967 field_data_size = MZ_READ_LE16(pExtra_data + sizeof(mz_uint16));
4968 field_total_size = field_data_size + sizeof(mz_uint16) * 2;
4969
4970 if (field_total_size > extra_size_remaining) {
4971 mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
4972 goto handle_failure;
4973 }
4974
4976 const mz_uint8* pSrc_field_data = pExtra_data + sizeof(mz_uint32);
4977
4978 if (field_data_size < sizeof(mz_uint64) * 2) {
4979 mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
4980 goto handle_failure;
4981 }
4982
4983 local_header_uncomp_size = MZ_READ_LE64(pSrc_field_data);
4984 local_header_comp_size = MZ_READ_LE64(pSrc_field_data + sizeof(mz_uint64));
4985
4986 found_zip64_ext_data_in_ldir = MZ_TRUE;
4987 break;
4988 }
4989
4990 pExtra_data += field_total_size;
4991 extra_size_remaining -= field_total_size;
4992 } while (extra_size_remaining);
4993 }
4994
4995 /* TODO: parse local header extra data when local_header_comp_size is 0xFFFFFFFF! (big_descriptor.zip) */
4996 /* I've seen zips in the wild with the data descriptor bit set, but proper local header values and bogus data descriptors */
4997 if ((has_data_descriptor) && (!local_header_comp_size) && (!local_header_crc32)) {
4998 mz_uint8 descriptor_buf[32];
4999 mz_bool has_id;
5000 const mz_uint8* pSrc;
5001 mz_uint32 file_crc32;
5002 mz_uint64 comp_size = 0, uncomp_size = 0;
5003
5004 mz_uint32 num_descriptor_uint32s = ((pState->m_zip64) || (found_zip64_ext_data_in_ldir)) ? 6 : 4;
5005
5006 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)) {
5007 mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
5008 goto handle_failure;
5009 }
5010
5011 has_id = (MZ_READ_LE32(descriptor_buf) == MZ_ZIP_DATA_DESCRIPTOR_ID);
5012 pSrc = has_id ? (descriptor_buf + sizeof(mz_uint32)) : descriptor_buf;
5013
5014 file_crc32 = MZ_READ_LE32(pSrc);
5015
5016 if ((pState->m_zip64) || (found_zip64_ext_data_in_ldir)) {
5017 comp_size = MZ_READ_LE64(pSrc + sizeof(mz_uint32));
5018 uncomp_size = MZ_READ_LE64(pSrc + sizeof(mz_uint32) + sizeof(mz_uint64));
5019 } else {
5020 comp_size = MZ_READ_LE32(pSrc + sizeof(mz_uint32));
5021 uncomp_size = MZ_READ_LE32(pSrc + sizeof(mz_uint32) + sizeof(mz_uint32));
5022 }
5023
5024 if ((file_crc32 != file_stat.m_crc32) || (comp_size != file_stat.m_comp_size) || (uncomp_size != file_stat.m_uncomp_size)) {
5025 mz_zip_set_error(pZip, MZ_ZIP_VALIDATION_FAILED);
5026 goto handle_failure;
5027 }
5028 } else {
5029 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)) {
5030 mz_zip_set_error(pZip, MZ_ZIP_VALIDATION_FAILED);
5031 goto handle_failure;
5032 }
5033 }
5034
5035 mz_zip_array_clear(pZip, &file_data_array);
5036
5037 if ((flags & MZ_ZIP_FLAG_VALIDATE_HEADERS_ONLY) == 0) {
5038 if (!mz_zip_reader_extract_to_callback(pZip, file_index, mz_zip_compute_crc32_callback, &uncomp_crc32, 0))
5039 return MZ_FALSE;
5040
5041 /* 1 more check to be sure, although the extract checks too. */
5042 if (uncomp_crc32 != file_stat.m_crc32) {
5043 mz_zip_set_error(pZip, MZ_ZIP_VALIDATION_FAILED);
5044 return MZ_FALSE;
5045 }
5046 }
5047
5048 return MZ_TRUE;
5049
5050handle_failure:
5051 mz_zip_array_clear(pZip, &file_data_array);
5052 return MZ_FALSE;
5053}
5054
5055mz_bool mz_zip_validate_archive(mz_zip_archive* pZip, mz_uint flags)
5056{
5057 mz_zip_internal_state* pState;
5058 mz_uint32 i;
5059
5060 if ((!pZip) || (!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || (!pZip->m_pRead))
5061 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
5062
5063 pState = pZip->m_pState;
5064
5065 /* Basic sanity checks */
5066 if (!pState->m_zip64) {
5067 if (pZip->m_total_files > MZ_UINT16_MAX)
5068 return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);
5069
5070 if (pZip->m_archive_size > MZ_UINT32_MAX)
5071 return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);
5072 } else {
5073 if (pState->m_central_dir.m_size >= MZ_UINT32_MAX)
5074 return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);
5075 }
5076
5077 for (i = 0; i < pZip->m_total_files; i++) {
5078 if (MZ_ZIP_FLAG_VALIDATE_LOCATE_FILE_FLAG & flags) {
5079 mz_uint32 found_index;
5080 mz_zip_archive_file_stat stat;
5081
5082 if (!mz_zip_reader_file_stat(pZip, i, &stat))
5083 return MZ_FALSE;
5084
5085 if (!mz_zip_reader_locate_file_v2(pZip, stat.m_filename, NULL, 0, &found_index))
5086 return MZ_FALSE;
5087
5088 /* 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) */
5089 if (found_index != i)
5090 return mz_zip_set_error(pZip, MZ_ZIP_VALIDATION_FAILED);
5091 }
5092
5093 if (!mz_zip_validate_file(pZip, i, flags))
5094 return MZ_FALSE;
5095 }
5096
5097 return MZ_TRUE;
5098}
5099
5100mz_bool mz_zip_validate_mem_archive(const void* pMem, size_t size, mz_uint flags, mz_zip_error* pErr)
5101{
5102 mz_bool success = MZ_TRUE;
5103 mz_zip_archive zip;
5104 mz_zip_error actual_err = MZ_ZIP_NO_ERROR;
5105
5106 if ((!pMem) || (!size)) {
5107 if (pErr)
5108 *pErr = MZ_ZIP_INVALID_PARAMETER;
5109 return MZ_FALSE;
5110 }
5111
5112 mz_zip_zero_struct(&zip);
5113
5114 if (!mz_zip_reader_init_mem(&zip, pMem, size, flags)) {
5115 if (pErr)
5116 *pErr = zip.m_last_error;
5117 return MZ_FALSE;
5118 }
5119
5120 if (!mz_zip_validate_archive(&zip, flags)) {
5121 actual_err = zip.m_last_error;
5122 success = MZ_FALSE;
5123 }
5124
5125 if (!mz_zip_reader_end_internal(&zip, success)) {
5126 if (!actual_err)
5127 actual_err = zip.m_last_error;
5128 success = MZ_FALSE;
5129 }
5130
5131 if (pErr)
5132 *pErr = actual_err;
5133
5134 return success;
5135}
5136
5137#ifndef MINIZ_NO_STDIO
5138mz_bool mz_zip_validate_file_archive(const char* pFilename, mz_uint flags, mz_zip_error* pErr)
5139{
5140 mz_bool success = MZ_TRUE;
5141 mz_zip_archive zip;
5142 mz_zip_error actual_err = MZ_ZIP_NO_ERROR;
5143
5144 if (!pFilename) {
5145 if (pErr)
5146 *pErr = MZ_ZIP_INVALID_PARAMETER;
5147 return MZ_FALSE;
5148 }
5149
5150 mz_zip_zero_struct(&zip);
5151
5152 if (!mz_zip_reader_init_file_v2(&zip, pFilename, flags, 0, 0)) {
5153 if (pErr)
5154 *pErr = zip.m_last_error;
5155 return MZ_FALSE;
5156 }
5157
5158 if (!mz_zip_validate_archive(&zip, flags)) {
5159 actual_err = zip.m_last_error;
5160 success = MZ_FALSE;
5161 }
5162
5163 if (!mz_zip_reader_end_internal(&zip, success)) {
5164 if (!actual_err)
5165 actual_err = zip.m_last_error;
5166 success = MZ_FALSE;
5167 }
5168
5169 if (pErr)
5170 *pErr = actual_err;
5171
5172 return success;
5173}
5174#endif /* #ifndef MINIZ_NO_STDIO */
5175
5176/* ------------------- .ZIP archive writing */
5177
5178#ifndef MINIZ_NO_ARCHIVE_WRITING_APIS
5179
5180static MZ_FORCEINLINE void mz_write_le16(mz_uint8* p, mz_uint16 v)
5181{
5182 p[0] = (mz_uint8)v;
5183 p[1] = (mz_uint8)(v >> 8);
5184}
5185static MZ_FORCEINLINE void mz_write_le32(mz_uint8* p, mz_uint32 v)
5186{
5187 p[0] = (mz_uint8)v;
5188 p[1] = (mz_uint8)(v >> 8);
5189 p[2] = (mz_uint8)(v >> 16);
5190 p[3] = (mz_uint8)(v >> 24);
5191}
5192static MZ_FORCEINLINE void mz_write_le64(mz_uint8* p, mz_uint64 v)
5193{
5194 mz_write_le32(p, (mz_uint32)v);
5195 mz_write_le32(p + sizeof(mz_uint32), (mz_uint32)(v >> 32));
5196}
5197
5198#define MZ_WRITE_LE16(p, v) mz_write_le16((mz_uint8*)(p), (mz_uint16)(v))
5199#define MZ_WRITE_LE32(p, v) mz_write_le32((mz_uint8*)(p), (mz_uint32)(v))
5200#define MZ_WRITE_LE64(p, v) mz_write_le64((mz_uint8*)(p), (mz_uint64)(v))
5201
5202static size_t mz_zip_heap_write_func(void* pOpaque, mz_uint64 file_ofs, const void* pBuf, size_t n)
5203{
5204 mz_zip_archive* pZip = (mz_zip_archive*)pOpaque;
5205 mz_zip_internal_state* pState = pZip->m_pState;
5206 mz_uint64 new_size = MZ_MAX(file_ofs + n, pState->m_mem_size);
5207
5208 if (!n)
5209 return 0;
5210
5211 /* An allocation this big is likely to just fail on 32-bit systems, so don't even go there. */
5212 if ((sizeof(size_t) == sizeof(mz_uint32)) && (new_size > 0x7FFFFFFF)) {
5213 mz_zip_set_error(pZip, MZ_ZIP_FILE_TOO_LARGE);
5214 return 0;
5215 }
5216
5217 if (new_size > pState->m_mem_capacity) {
5218 void* pNew_block;
5219 size_t new_capacity = MZ_MAX(64, pState->m_mem_capacity);
5220
5221 while (new_capacity < new_size)
5222 new_capacity *= 2;
5223
5224 if (NULL == (pNew_block = pZip->m_pRealloc(pZip->m_pAlloc_opaque, pState->m_pMem, 1, new_capacity))) {
5225 mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
5226 return 0;
5227 }
5228
5229 pState->m_pMem = pNew_block;
5230 pState->m_mem_capacity = new_capacity;
5231 }
5232 memcpy((mz_uint8*)pState->m_pMem + file_ofs, pBuf, n);
5233 pState->m_mem_size = (size_t)new_size;
5234 return n;
5235}
5236
5237static mz_bool mz_zip_writer_end_internal(mz_zip_archive* pZip, mz_bool set_last_error)
5238{
5239 mz_zip_internal_state* pState;
5240 mz_bool status = MZ_TRUE;
5241
5242 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))) {
5243 if (set_last_error)
5244 mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
5245 return MZ_FALSE;
5246 }
5247
5248 pState = pZip->m_pState;
5249 pZip->m_pState = NULL;
5250 mz_zip_array_clear(pZip, &pState->m_central_dir);
5251 mz_zip_array_clear(pZip, &pState->m_central_dir_offsets);
5252 mz_zip_array_clear(pZip, &pState->m_sorted_central_dir_offsets);
5253
5254#ifndef MINIZ_NO_STDIO
5255 if (pState->m_pFile) {
5256 if (pZip->m_zip_type == MZ_ZIP_TYPE_FILE) {
5257 if (MZ_FCLOSE(pState->m_pFile) == EOF) {
5258 if (set_last_error)
5259 mz_zip_set_error(pZip, MZ_ZIP_FILE_CLOSE_FAILED);
5260 status = MZ_FALSE;
5261 }
5262 }
5263
5264 pState->m_pFile = NULL;
5265 }
5266#endif /* #ifndef MINIZ_NO_STDIO */
5267
5268 if ((pZip->m_pWrite == mz_zip_heap_write_func) && (pState->m_pMem)) {
5269 pZip->m_pFree(pZip->m_pAlloc_opaque, pState->m_pMem);
5270 pState->m_pMem = NULL;
5271 }
5272
5273 pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
5274 pZip->m_zip_mode = MZ_ZIP_MODE_INVALID;
5275 return status;
5276}
5277
5278mz_bool mz_zip_writer_init_v2(mz_zip_archive* pZip, mz_uint64 existing_size, mz_uint flags)
5279{
5280 mz_bool zip64 = (flags & MZ_ZIP_FLAG_WRITE_ZIP64) != 0;
5281
5282 if ((!pZip) || (pZip->m_pState) || (!pZip->m_pWrite) || (pZip->m_zip_mode != MZ_ZIP_MODE_INVALID))
5283 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
5284
5285 if (flags & MZ_ZIP_FLAG_WRITE_ALLOW_READING) {
5286 if (!pZip->m_pRead)
5287 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
5288 }
5289
5290 if (pZip->m_file_offset_alignment) {
5291 /* Ensure user specified file offset alignment is a power of 2. */
5292 if (pZip->m_file_offset_alignment & (pZip->m_file_offset_alignment - 1))
5293 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
5294 }
5295
5296 if (!pZip->m_pAlloc)
5297 pZip->m_pAlloc = miniz_def_alloc_func;
5298 if (!pZip->m_pFree)
5299 pZip->m_pFree = miniz_def_free_func;
5300 if (!pZip->m_pRealloc)
5301 pZip->m_pRealloc = miniz_def_realloc_func;
5302
5303 pZip->m_archive_size = existing_size;
5304 pZip->m_central_directory_file_ofs = 0;
5305 pZip->m_total_files = 0;
5306
5307 if (NULL == (pZip->m_pState = (mz_zip_internal_state*)pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, sizeof(mz_zip_internal_state))))
5308 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
5309
5310 memset(pZip->m_pState, 0, sizeof(mz_zip_internal_state));
5311
5312 MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_central_dir, sizeof(mz_uint8));
5313 MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_central_dir_offsets, sizeof(mz_uint32));
5314 MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_sorted_central_dir_offsets, sizeof(mz_uint32));
5315
5316 pZip->m_pState->m_zip64 = zip64;
5317 pZip->m_pState->m_zip64_has_extended_info_fields = zip64;
5318
5319 pZip->m_zip_type = MZ_ZIP_TYPE_USER;
5320 pZip->m_zip_mode = MZ_ZIP_MODE_WRITING;
5321
5322 return MZ_TRUE;
5323}
5324
5325mz_bool mz_zip_writer_init(mz_zip_archive* pZip, mz_uint64 existing_size)
5326{
5327 return mz_zip_writer_init_v2(pZip, existing_size, 0);
5328}
5329
5330mz_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)
5331{
5332 pZip->m_pWrite = mz_zip_heap_write_func;
5333 pZip->m_pNeeds_keepalive = NULL;
5334
5335 if (flags & MZ_ZIP_FLAG_WRITE_ALLOW_READING)
5336 pZip->m_pRead = mz_zip_mem_read_func;
5337
5338 pZip->m_pIO_opaque = pZip;
5339
5340 if (!mz_zip_writer_init_v2(pZip, size_to_reserve_at_beginning, flags))
5341 return MZ_FALSE;
5342
5343 pZip->m_zip_type = MZ_ZIP_TYPE_HEAP;
5344
5345 if (0 != (initial_allocation_size = MZ_MAX(initial_allocation_size, size_to_reserve_at_beginning))) {
5346 if (NULL == (pZip->m_pState->m_pMem = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, initial_allocation_size))) {
5347 mz_zip_writer_end_internal(pZip, MZ_FALSE);
5348 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
5349 }
5350 pZip->m_pState->m_mem_capacity = initial_allocation_size;
5351 }
5352
5353 return MZ_TRUE;
5354}
5355
5356mz_bool mz_zip_writer_init_heap(mz_zip_archive* pZip, size_t size_to_reserve_at_beginning, size_t initial_allocation_size)
5357{
5358 return mz_zip_writer_init_heap_v2(pZip, size_to_reserve_at_beginning, initial_allocation_size, 0);
5359}
5360
5361#ifndef MINIZ_NO_STDIO
5362static size_t mz_zip_file_write_func(void* pOpaque, mz_uint64 file_ofs, const void* pBuf, size_t n)
5363{
5364 mz_zip_archive* pZip = (mz_zip_archive*)pOpaque;
5365 mz_int64 cur_ofs = MZ_FTELL64(pZip->m_pState->m_pFile);
5366
5367 file_ofs += pZip->m_pState->m_file_archive_start_ofs;
5368
5369 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)))) {
5370 mz_zip_set_error(pZip, MZ_ZIP_FILE_SEEK_FAILED);
5371 return 0;
5372 }
5373
5374 return MZ_FWRITE(pBuf, 1, n, pZip->m_pState->m_pFile);
5375}
5376
5377mz_bool mz_zip_writer_init_file(mz_zip_archive* pZip, const char* pFilename, mz_uint64 size_to_reserve_at_beginning)
5378{
5379 return mz_zip_writer_init_file_v2(pZip, pFilename, size_to_reserve_at_beginning, 0);
5380}
5381
5382mz_bool mz_zip_writer_init_file_v2(mz_zip_archive* pZip, const char* pFilename, mz_uint64 size_to_reserve_at_beginning, mz_uint flags)
5383{
5384 MZ_FILE* pFile;
5385
5386 pZip->m_pWrite = mz_zip_file_write_func;
5387 pZip->m_pNeeds_keepalive = NULL;
5388
5389 if (flags & MZ_ZIP_FLAG_WRITE_ALLOW_READING)
5390 pZip->m_pRead = mz_zip_file_read_func;
5391
5392 pZip->m_pIO_opaque = pZip;
5393
5394 if (!mz_zip_writer_init_v2(pZip, size_to_reserve_at_beginning, flags))
5395 return MZ_FALSE;
5396
5397 if (NULL == (pFile = MZ_FOPEN(pFilename, (flags & MZ_ZIP_FLAG_WRITE_ALLOW_READING) ? "w+b" : "wb"))) {
5398 mz_zip_writer_end(pZip);
5399 return mz_zip_set_error(pZip, MZ_ZIP_FILE_OPEN_FAILED);
5400 }
5401
5402 pZip->m_pState->m_pFile = pFile;
5403 pZip->m_zip_type = MZ_ZIP_TYPE_FILE;
5404
5405 if (size_to_reserve_at_beginning) {
5406 mz_uint64 cur_ofs = 0;
5407 char buf[4096];
5408
5409 MZ_CLEAR_ARR(buf);
5410
5411 do {
5412 size_t n = (size_t)MZ_MIN(sizeof(buf), size_to_reserve_at_beginning);
5413 if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_ofs, buf, n) != n) {
5414 mz_zip_writer_end(pZip);
5415 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
5416 }
5417 cur_ofs += n;
5418 size_to_reserve_at_beginning -= n;
5419 } while (size_to_reserve_at_beginning);
5420 }
5421
5422 return MZ_TRUE;
5423}
5424
5425mz_bool mz_zip_writer_init_cfile(mz_zip_archive* pZip, MZ_FILE* pFile, mz_uint flags)
5426{
5427 pZip->m_pWrite = mz_zip_file_write_func;
5428 pZip->m_pNeeds_keepalive = NULL;
5429
5430 if (flags & MZ_ZIP_FLAG_WRITE_ALLOW_READING)
5431 pZip->m_pRead = mz_zip_file_read_func;
5432
5433 pZip->m_pIO_opaque = pZip;
5434
5435 if (!mz_zip_writer_init_v2(pZip, 0, flags))
5436 return MZ_FALSE;
5437
5438 pZip->m_pState->m_pFile = pFile;
5439 pZip->m_pState->m_file_archive_start_ofs = MZ_FTELL64(pZip->m_pState->m_pFile);
5440 pZip->m_zip_type = MZ_ZIP_TYPE_CFILE;
5441
5442 return MZ_TRUE;
5443}
5444#endif /* #ifndef MINIZ_NO_STDIO */
5445
5446mz_bool mz_zip_writer_init_from_reader_v2(mz_zip_archive* pZip, const char* pFilename, mz_uint flags)
5447{
5448 mz_zip_internal_state* pState;
5449
5450 if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_READING))
5451 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
5452
5453 if (flags & MZ_ZIP_FLAG_WRITE_ZIP64) {
5454 /* 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?) */
5455 if (!pZip->m_pState->m_zip64)
5456 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
5457 }
5458
5459 /* No sense in trying to write to an archive that's already at the support max size */
5460 if (pZip->m_pState->m_zip64) {
5461 if (pZip->m_total_files == MZ_UINT32_MAX)
5462 return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);
5463 } else {
5464 if (pZip->m_total_files == MZ_UINT16_MAX)
5465 return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);
5466
5467 if ((pZip->m_archive_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + MZ_ZIP_LOCAL_DIR_HEADER_SIZE) > MZ_UINT32_MAX)
5468 return mz_zip_set_error(pZip, MZ_ZIP_FILE_TOO_LARGE);
5469 }
5470
5471 pState = pZip->m_pState;
5472
5473 if (pState->m_pFile) {
5474#ifdef MINIZ_NO_STDIO
5475 (void)pFilename;
5476 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
5477#else
5478 if (pZip->m_pIO_opaque != pZip)
5479 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
5480
5481 if (pZip->m_zip_type == MZ_ZIP_TYPE_FILE) {
5482 if (!pFilename)
5483 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
5484
5485 /* Archive is being read from stdio and was originally opened only for reading. Try to reopen as writable. */
5486 if (NULL == (pState->m_pFile = MZ_FREOPEN(pFilename, "r+b", pState->m_pFile))) {
5487 /* The mz_zip_archive is now in a bogus state because pState->m_pFile is NULL, so just close it. */
5488 mz_zip_reader_end_internal(pZip, MZ_FALSE);
5489 return mz_zip_set_error(pZip, MZ_ZIP_FILE_OPEN_FAILED);
5490 }
5491 }
5492
5493 pZip->m_pWrite = mz_zip_file_write_func;
5494 pZip->m_pNeeds_keepalive = NULL;
5495#endif /* #ifdef MINIZ_NO_STDIO */
5496 } else if (pState->m_pMem) {
5497 /* Archive lives in a memory block. Assume it's from the heap that we can resize using the realloc callback. */
5498 if (pZip->m_pIO_opaque != pZip)
5499 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
5500
5501 pState->m_mem_capacity = pState->m_mem_size;
5502 pZip->m_pWrite = mz_zip_heap_write_func;
5503 pZip->m_pNeeds_keepalive = NULL;
5504 }
5505 /* Archive is being read via a user provided read function - make sure the user has specified a write function too. */
5506 else if (!pZip->m_pWrite)
5507 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
5508
5509 /* Start writing new files at the archive's current central directory location. */
5510 /* TODO: We could add a flag that lets the user start writing immediately AFTER the existing central dir - this would be safer. */
5511 pZip->m_archive_size = pZip->m_central_directory_file_ofs;
5512 pZip->m_central_directory_file_ofs = 0;
5513
5514 /* Clear the sorted central dir offsets, they aren't useful or maintained now. */
5515 /* Even though we're now in write mode, files can still be extracted and verified, but file locates will be slow. */
5516 /* TODO: We could easily maintain the sorted central directory offsets. */
5517 mz_zip_array_clear(pZip, &pZip->m_pState->m_sorted_central_dir_offsets);
5518
5519 pZip->m_zip_mode = MZ_ZIP_MODE_WRITING;
5520
5521 return MZ_TRUE;
5522}
5523
5524mz_bool mz_zip_writer_init_from_reader(mz_zip_archive* pZip, const char* pFilename)
5525{
5526 return mz_zip_writer_init_from_reader_v2(pZip, pFilename, 0);
5527}
5528
5529/* TODO: pArchive_name is a terrible name here! */
5530mz_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)
5531{
5532 return mz_zip_writer_add_mem_ex(pZip, pArchive_name, pBuf, buf_size, NULL, 0, level_and_flags, 0, 0);
5533}
5534
5535typedef struct
5536{
5537 mz_zip_archive* m_pZip;
5539 mz_uint64 m_comp_size;
5541
5542static mz_bool mz_zip_writer_add_put_buf_callback(const void* pBuf, int len, void* pUser)
5543{
5545 if ((int)pState->m_pZip->m_pWrite(pState->m_pZip->m_pIO_opaque, pState->m_cur_archive_file_ofs, pBuf, len) != len)
5546 return MZ_FALSE;
5547
5548 pState->m_cur_archive_file_ofs += len;
5549 pState->m_comp_size += len;
5550 return MZ_TRUE;
5551}
5552
5553#define MZ_ZIP64_MAX_LOCAL_EXTRA_FIELD_SIZE (sizeof(mz_uint16) * 2 + sizeof(mz_uint64) * 2)
5554#define MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE (sizeof(mz_uint16) * 2 + sizeof(mz_uint64) * 3)
5555static 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)
5556{
5557 mz_uint8* pDst = pBuf;
5558 mz_uint32 field_size = 0;
5559
5561 MZ_WRITE_LE16(pDst + 2, 0);
5562 pDst += sizeof(mz_uint16) * 2;
5563
5564 if (pUncomp_size) {
5565 MZ_WRITE_LE64(pDst, *pUncomp_size);
5566 pDst += sizeof(mz_uint64);
5567 field_size += sizeof(mz_uint64);
5568 }
5569
5570 if (pComp_size) {
5571 MZ_WRITE_LE64(pDst, *pComp_size);
5572 pDst += sizeof(mz_uint64);
5573 field_size += sizeof(mz_uint64);
5574 }
5575
5576 if (pLocal_header_ofs) {
5577 MZ_WRITE_LE64(pDst, *pLocal_header_ofs);
5578 pDst += sizeof(mz_uint64);
5579 field_size += sizeof(mz_uint64);
5580 }
5581
5582 MZ_WRITE_LE16(pBuf + 2, field_size);
5583
5584 return (mz_uint32)(pDst - pBuf);
5585}
5586
5587static 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)
5588{
5589 (void)pZip;
5590 memset(pDst, 0, MZ_ZIP_LOCAL_DIR_HEADER_SIZE);
5592 MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_VERSION_NEEDED_OFS, method ? 20 : 0);
5593 MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_BIT_FLAG_OFS, bit_flags);
5594 MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_METHOD_OFS, method);
5595 MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_FILE_TIME_OFS, dos_time);
5596 MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_FILE_DATE_OFS, dos_date);
5597 MZ_WRITE_LE32(pDst + MZ_ZIP_LDH_CRC32_OFS, uncomp_crc32);
5598 MZ_WRITE_LE32(pDst + MZ_ZIP_LDH_COMPRESSED_SIZE_OFS, MZ_MIN(comp_size, MZ_UINT32_MAX));
5599 MZ_WRITE_LE32(pDst + MZ_ZIP_LDH_DECOMPRESSED_SIZE_OFS, MZ_MIN(uncomp_size, MZ_UINT32_MAX));
5600 MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_FILENAME_LEN_OFS, filename_size);
5601 MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_EXTRA_LEN_OFS, extra_size);
5602 return MZ_TRUE;
5603}
5604
5605static mz_bool mz_zip_writer_create_central_dir_header(mz_zip_archive* pZip, mz_uint8* pDst,
5606 mz_uint16 filename_size, mz_uint16 extra_size, mz_uint16 comment_size,
5607 mz_uint64 uncomp_size, mz_uint64 comp_size, mz_uint32 uncomp_crc32,
5608 mz_uint16 method, mz_uint16 bit_flags, mz_uint16 dos_time, mz_uint16 dos_date,
5609 mz_uint64 local_header_ofs, mz_uint32 ext_attributes)
5610{
5611 (void)pZip;
5612 memset(pDst, 0, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE);
5614 MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_VERSION_NEEDED_OFS, method ? 20 : 0);
5615 MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_BIT_FLAG_OFS, bit_flags);
5616 MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_METHOD_OFS, method);
5617 MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_FILE_TIME_OFS, dos_time);
5618 MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_FILE_DATE_OFS, dos_date);
5619 MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_CRC32_OFS, uncomp_crc32);
5620 MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS, MZ_MIN(comp_size, MZ_UINT32_MAX));
5621 MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS, MZ_MIN(uncomp_size, MZ_UINT32_MAX));
5622 MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_FILENAME_LEN_OFS, filename_size);
5623 MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_EXTRA_LEN_OFS, extra_size);
5624 MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_COMMENT_LEN_OFS, comment_size);
5625 MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_EXTERNAL_ATTR_OFS, ext_attributes);
5626 MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_LOCAL_HEADER_OFS, MZ_MIN(local_header_ofs, MZ_UINT32_MAX));
5627 return MZ_TRUE;
5628}
5629
5630static mz_bool mz_zip_writer_add_to_central_dir(mz_zip_archive* pZip, const char* pFilename, mz_uint16 filename_size,
5631 const void* pExtra, mz_uint16 extra_size, const void* pComment, mz_uint16 comment_size,
5632 mz_uint64 uncomp_size, mz_uint64 comp_size, mz_uint32 uncomp_crc32,
5633 mz_uint16 method, mz_uint16 bit_flags, mz_uint16 dos_time, mz_uint16 dos_date,
5634 mz_uint64 local_header_ofs, mz_uint32 ext_attributes,
5635 const char* user_extra_data, mz_uint user_extra_data_len)
5636{
5637 mz_zip_internal_state* pState = pZip->m_pState;
5638 mz_uint32 central_dir_ofs = (mz_uint32)pState->m_central_dir.m_size;
5639 size_t orig_central_dir_size = pState->m_central_dir.m_size;
5640 mz_uint8 central_dir_header[MZ_ZIP_CENTRAL_DIR_HEADER_SIZE];
5641
5642 if (!pZip->m_pState->m_zip64) {
5643 if (local_header_ofs > 0xFFFFFFFF)
5644 return mz_zip_set_error(pZip, MZ_ZIP_FILE_TOO_LARGE);
5645 }
5646
5647 /* miniz doesn't support central dirs >= MZ_UINT32_MAX bytes yet */
5648 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)
5649 return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_CDIR_SIZE);
5650
5651 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))
5652 return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);
5653
5654 if ((!mz_zip_array_push_back(pZip, &pState->m_central_dir, central_dir_header, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE)) || (!mz_zip_array_push_back(pZip, &pState->m_central_dir, pFilename, filename_size)) || (!mz_zip_array_push_back(pZip, &pState->m_central_dir, pExtra, extra_size)) || (!mz_zip_array_push_back(pZip, &pState->m_central_dir, user_extra_data, user_extra_data_len)) || (!mz_zip_array_push_back(pZip, &pState->m_central_dir, pComment, comment_size)) || (!mz_zip_array_push_back(pZip, &pState->m_central_dir_offsets, &central_dir_ofs, 1))) {
5655 /* Try to resize the central directory array back into its original state. */
5656 mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE);
5657 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
5658 }
5659
5660 return MZ_TRUE;
5661}
5662
5663static mz_bool mz_zip_writer_validate_archive_name(const char* pArchive_name)
5664{
5665 /* 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. */
5666 if (*pArchive_name == '/')
5667 return MZ_FALSE;
5668
5669 /* Making sure the name does not contain drive letters or DOS style backward slashes is the responsibility of the program using miniz*/
5670
5671 return MZ_TRUE;
5672}
5673
5675{
5676 mz_uint32 n;
5677 if (!pZip->m_file_offset_alignment)
5678 return 0;
5679 n = (mz_uint32)(pZip->m_archive_size & (pZip->m_file_offset_alignment - 1));
5680 return (mz_uint)((pZip->m_file_offset_alignment - n) & (pZip->m_file_offset_alignment - 1));
5681}
5682
5683static mz_bool mz_zip_writer_write_zeros(mz_zip_archive* pZip, mz_uint64 cur_file_ofs, mz_uint32 n)
5684{
5685 char buf[4096];
5686 memset(buf, 0, MZ_MIN(sizeof(buf), n));
5687 while (n) {
5688 mz_uint32 s = MZ_MIN(sizeof(buf), n);
5689 if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_file_ofs, buf, s) != s)
5690 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
5691
5692 cur_file_ofs += s;
5693 n -= s;
5694 }
5695 return MZ_TRUE;
5696}
5697
5698mz_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,
5699 mz_uint64 uncomp_size, mz_uint32 uncomp_crc32)
5700{
5701 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);
5702}
5703
5704mz_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,
5705 mz_uint level_and_flags, mz_uint64 uncomp_size, mz_uint32 uncomp_crc32, MZ_TIME_T* last_modified,
5706 const char* user_extra_data, mz_uint user_extra_data_len, const char* user_extra_data_central, mz_uint user_extra_data_central_len)
5707{
5708 mz_uint16 method = 0, dos_time = 0, dos_date = 0;
5709 mz_uint level, ext_attributes = 0, num_alignment_padding_bytes;
5710 mz_uint64 local_dir_header_ofs = pZip->m_archive_size, cur_archive_file_ofs = pZip->m_archive_size, comp_size = 0;
5711 size_t archive_name_size;
5712 mz_uint8 local_dir_header[MZ_ZIP_LOCAL_DIR_HEADER_SIZE];
5713 tdefl_compressor* pComp = NULL;
5714 mz_bool store_data_uncompressed;
5715 mz_zip_internal_state* pState;
5716 mz_uint8* pExtra_data = NULL;
5717 mz_uint32 extra_size = 0;
5718 mz_uint8 extra_data[MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE];
5719 mz_uint16 bit_flags = 0;
5720
5721 if ((int)level_and_flags < 0)
5722 level_and_flags = MZ_DEFAULT_LEVEL;
5723
5724 if (uncomp_size || (buf_size && !(level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)))
5726
5727 if (!(level_and_flags & MZ_ZIP_FLAG_ASCII_FILENAME))
5729
5730 level = level_and_flags & 0xF;
5731 store_data_uncompressed = ((!level) || (level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA));
5732
5733 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))
5734 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
5735
5736 pState = pZip->m_pState;
5737
5738 if (pState->m_zip64) {
5739 if (pZip->m_total_files == MZ_UINT32_MAX)
5740 return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);
5741 } else {
5742 if (pZip->m_total_files == MZ_UINT16_MAX) {
5743 pState->m_zip64 = MZ_TRUE;
5744 /*return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES); */
5745 }
5746 if (((mz_uint64)buf_size > 0xFFFFFFFF) || (uncomp_size > 0xFFFFFFFF)) {
5747 pState->m_zip64 = MZ_TRUE;
5748 /*return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); */
5749 }
5750 }
5751
5752 if ((!(level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (uncomp_size))
5753 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
5754
5755 if (!mz_zip_writer_validate_archive_name(pArchive_name))
5756 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_FILENAME);
5757
5758#ifndef MINIZ_NO_TIME
5759 if (last_modified != NULL) {
5760 mz_zip_time_t_to_dos_time(*last_modified, &dos_time, &dos_date);
5761 } else {
5762 MZ_TIME_T cur_time;
5763 time(&cur_time);
5764 mz_zip_time_t_to_dos_time(cur_time, &dos_time, &dos_date);
5765 }
5766#endif /* #ifndef MINIZ_NO_TIME */
5767
5768 if (!(level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) {
5769 uncomp_crc32 = (mz_uint32)mz_crc32(MZ_CRC32_INIT, (const mz_uint8*)pBuf, buf_size);
5770 uncomp_size = buf_size;
5771 if (uncomp_size <= 3) {
5772 level = 0;
5773 store_data_uncompressed = MZ_TRUE;
5774 }
5775 }
5776
5777 archive_name_size = strlen(pArchive_name);
5778 if (archive_name_size > MZ_UINT16_MAX)
5779 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_FILENAME);
5780
5781 num_alignment_padding_bytes = mz_zip_writer_compute_padding_needed_for_file_alignment(pZip);
5782
5783 /* miniz doesn't support central dirs >= MZ_UINT32_MAX bytes yet */
5784 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)
5785 return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_CDIR_SIZE);
5786
5787 if (!pState->m_zip64) {
5788 /* Bail early if the archive would obviously become too large */
5789 if ((pZip->m_archive_size + num_alignment_padding_bytes + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + archive_name_size
5790 + 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 + user_extra_data_central_len
5792 > 0xFFFFFFFF) {
5793 pState->m_zip64 = MZ_TRUE;
5794 /*return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); */
5795 }
5796 }
5797
5798 if ((archive_name_size) && (pArchive_name[archive_name_size - 1] == '/')) {
5799 /* Set DOS Subdirectory attribute bit. */
5800 ext_attributes |= MZ_ZIP_DOS_DIR_ATTRIBUTE_BITFLAG;
5801
5802 /* Subdirectories cannot contain data. */
5803 if ((buf_size) || (uncomp_size))
5804 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
5805 }
5806
5807 /* 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.) */
5808 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)))
5809 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
5810
5811 if ((!store_data_uncompressed) && (buf_size)) {
5812 if (NULL == (pComp = (tdefl_compressor*)pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, sizeof(tdefl_compressor))))
5813 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
5814 }
5815
5816 if (!mz_zip_writer_write_zeros(pZip, cur_archive_file_ofs, num_alignment_padding_bytes)) {
5817 pZip->m_pFree(pZip->m_pAlloc_opaque, pComp);
5818 return MZ_FALSE;
5819 }
5820
5821 local_dir_header_ofs += num_alignment_padding_bytes;
5822 if (pZip->m_file_offset_alignment) {
5823 MZ_ASSERT((local_dir_header_ofs & (pZip->m_file_offset_alignment - 1)) == 0);
5824 }
5825 cur_archive_file_ofs += num_alignment_padding_bytes;
5826
5827 MZ_CLEAR_ARR(local_dir_header);
5828
5829 if (!store_data_uncompressed || (level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) {
5830 method = MZ_DEFLATED;
5831 }
5832
5833 if (pState->m_zip64) {
5834 if (uncomp_size >= MZ_UINT32_MAX || local_dir_header_ofs >= MZ_UINT32_MAX) {
5835 pExtra_data = extra_data;
5836 extra_size = mz_zip_writer_create_zip64_extra_data(extra_data, (uncomp_size >= MZ_UINT32_MAX) ? &uncomp_size : NULL,
5837 (uncomp_size >= MZ_UINT32_MAX) ? &comp_size : NULL, (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL);
5838 }
5839
5840 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))
5841 return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);
5842
5843 if (pZip->m_pWrite(pZip->m_pIO_opaque, local_dir_header_ofs, local_dir_header, sizeof(local_dir_header)) != sizeof(local_dir_header))
5844 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
5845
5846 cur_archive_file_ofs += sizeof(local_dir_header);
5847
5848 if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pArchive_name, archive_name_size) != archive_name_size) {
5849 pZip->m_pFree(pZip->m_pAlloc_opaque, pComp);
5850 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
5851 }
5852 cur_archive_file_ofs += archive_name_size;
5853
5854 if (pExtra_data != NULL) {
5855 if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, extra_data, extra_size) != extra_size)
5856 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
5857
5858 cur_archive_file_ofs += extra_size;
5859 }
5860 } else {
5861 if ((comp_size > MZ_UINT32_MAX) || (cur_archive_file_ofs > MZ_UINT32_MAX))
5862 return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);
5863 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))
5864 return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);
5865
5866 if (pZip->m_pWrite(pZip->m_pIO_opaque, local_dir_header_ofs, local_dir_header, sizeof(local_dir_header)) != sizeof(local_dir_header))
5867 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
5868
5869 cur_archive_file_ofs += sizeof(local_dir_header);
5870
5871 if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pArchive_name, archive_name_size) != archive_name_size) {
5872 pZip->m_pFree(pZip->m_pAlloc_opaque, pComp);
5873 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
5874 }
5875 cur_archive_file_ofs += archive_name_size;
5876 }
5877
5878 if (user_extra_data_len > 0) {
5879 if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, user_extra_data, user_extra_data_len) != user_extra_data_len)
5880 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
5881
5882 cur_archive_file_ofs += user_extra_data_len;
5883 }
5884
5885 if (store_data_uncompressed) {
5886 if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pBuf, buf_size) != buf_size) {
5887 pZip->m_pFree(pZip->m_pAlloc_opaque, pComp);
5888 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
5889 }
5890
5891 cur_archive_file_ofs += buf_size;
5892 comp_size = buf_size;
5893 } else if (buf_size) {
5895
5896 state.m_pZip = pZip;
5897 state.m_cur_archive_file_ofs = cur_archive_file_ofs;
5898 state.m_comp_size = 0;
5899
5900 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) || (tdefl_compress_buffer(pComp, pBuf, buf_size, TDEFL_FINISH) != TDEFL_STATUS_DONE)) {
5901 pZip->m_pFree(pZip->m_pAlloc_opaque, pComp);
5902 return mz_zip_set_error(pZip, MZ_ZIP_COMPRESSION_FAILED);
5903 }
5904
5905 comp_size = state.m_comp_size;
5906 cur_archive_file_ofs = state.m_cur_archive_file_ofs;
5907 }
5908
5909 pZip->m_pFree(pZip->m_pAlloc_opaque, pComp);
5910 pComp = NULL;
5911
5912 if (uncomp_size) {
5913 mz_uint8 local_dir_footer[MZ_ZIP_DATA_DESCRIPTER_SIZE64];
5914 mz_uint32 local_dir_footer_size = MZ_ZIP_DATA_DESCRIPTER_SIZE32;
5915
5916 MZ_ASSERT(bit_flags & MZ_ZIP_LDH_BIT_FLAG_HAS_LOCATOR);
5917
5918 MZ_WRITE_LE32(local_dir_footer + 0, MZ_ZIP_DATA_DESCRIPTOR_ID);
5919 MZ_WRITE_LE32(local_dir_footer + 4, uncomp_crc32);
5920 if (pExtra_data == NULL) {
5921 if (comp_size > MZ_UINT32_MAX)
5922 return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);
5923
5924 MZ_WRITE_LE32(local_dir_footer + 8, comp_size);
5925 MZ_WRITE_LE32(local_dir_footer + 12, uncomp_size);
5926 } else {
5927 MZ_WRITE_LE64(local_dir_footer + 8, comp_size);
5928 MZ_WRITE_LE64(local_dir_footer + 16, uncomp_size);
5929 local_dir_footer_size = MZ_ZIP_DATA_DESCRIPTER_SIZE64;
5930 }
5931
5932 if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, local_dir_footer, local_dir_footer_size) != local_dir_footer_size)
5933 return MZ_FALSE;
5934
5935 cur_archive_file_ofs += local_dir_footer_size;
5936 }
5937
5938 if (pExtra_data != NULL) {
5939 extra_size = mz_zip_writer_create_zip64_extra_data(extra_data, (uncomp_size >= MZ_UINT32_MAX) ? &uncomp_size : NULL,
5940 (uncomp_size >= MZ_UINT32_MAX) ? &comp_size : NULL, (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL);
5941 }
5942
5943 if (!mz_zip_writer_add_to_central_dir(pZip, pArchive_name, (mz_uint16)archive_name_size, pExtra_data, (mz_uint16)extra_size, pComment,
5944 comment_size, uncomp_size, comp_size, uncomp_crc32, method, bit_flags, dos_time, dos_date, local_dir_header_ofs, ext_attributes,
5945 user_extra_data_central, user_extra_data_central_len))
5946 return MZ_FALSE;
5947
5948 pZip->m_total_files++;
5949 pZip->m_archive_size = cur_archive_file_ofs;
5950
5951 return MZ_TRUE;
5952}
5953
5954mz_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,
5955 const char* user_extra_data, mz_uint user_extra_data_len, const char* user_extra_data_central, mz_uint user_extra_data_central_len)
5956{
5957 mz_uint16 gen_flags;
5958 mz_uint uncomp_crc32 = MZ_CRC32_INIT, level, num_alignment_padding_bytes;
5959 mz_uint16 method = 0, dos_time = 0, dos_date = 0, ext_attributes = 0;
5960 mz_uint64 local_dir_header_ofs, cur_archive_file_ofs = pZip->m_archive_size, uncomp_size = 0, comp_size = 0;
5961 size_t archive_name_size;
5962 mz_uint8 local_dir_header[MZ_ZIP_LOCAL_DIR_HEADER_SIZE];
5963 mz_uint8* pExtra_data = NULL;
5964 mz_uint32 extra_size = 0;
5965 mz_uint8 extra_data[MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE];
5966 mz_zip_internal_state* pState;
5967 mz_uint64 file_ofs = 0, cur_archive_header_file_ofs;
5968
5969 if ((int)level_and_flags < 0)
5970 level_and_flags = MZ_DEFAULT_LEVEL;
5971 level = level_and_flags & 0xF;
5972
5973 gen_flags = (level_and_flags & MZ_ZIP_FLAG_WRITE_HEADER_SET_SIZE) ? 0 : MZ_ZIP_LDH_BIT_FLAG_HAS_LOCATOR;
5974
5975 if (!(level_and_flags & MZ_ZIP_FLAG_ASCII_FILENAME))
5977
5978 /* Sanity checks */
5979 if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || (!pArchive_name) || ((comment_size) && (!pComment)) || (level > MZ_UBER_COMPRESSION))
5980 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
5981
5982 pState = pZip->m_pState;
5983
5984 if ((!pState->m_zip64) && (max_size > MZ_UINT32_MAX)) {
5985 /* Source file is too large for non-zip64 */
5986 /*return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); */
5987 pState->m_zip64 = MZ_TRUE;
5988 }
5989
5990 /* We could support this, but why? */
5991 if (level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)
5992 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
5993
5994 if (!mz_zip_writer_validate_archive_name(pArchive_name))
5995 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_FILENAME);
5996
5997 if (pState->m_zip64) {
5998 if (pZip->m_total_files == MZ_UINT32_MAX)
5999 return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);
6000 } else {
6001 if (pZip->m_total_files == MZ_UINT16_MAX) {
6002 pState->m_zip64 = MZ_TRUE;
6003 /*return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES); */
6004 }
6005 }
6006
6007 archive_name_size = strlen(pArchive_name);
6008 if (archive_name_size > MZ_UINT16_MAX)
6009 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_FILENAME);
6010
6011 num_alignment_padding_bytes = mz_zip_writer_compute_padding_needed_for_file_alignment(pZip);
6012
6013 /* miniz doesn't support central dirs >= MZ_UINT32_MAX bytes yet */
6014 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)
6015 return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_CDIR_SIZE);
6016
6017 if (!pState->m_zip64) {
6018 /* Bail early if the archive would obviously become too large */
6019 if ((pZip->m_archive_size + num_alignment_padding_bytes + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + archive_name_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE
6020 + archive_name_size + comment_size + user_extra_data_len + pState->m_central_dir.m_size + MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE + 1024
6021 + MZ_ZIP_DATA_DESCRIPTER_SIZE32 + user_extra_data_central_len)
6022 > 0xFFFFFFFF) {
6023 pState->m_zip64 = MZ_TRUE;
6024 /*return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE); */
6025 }
6026 }
6027
6028#ifndef MINIZ_NO_TIME
6029 if (pFile_time) {
6030 mz_zip_time_t_to_dos_time(*pFile_time, &dos_time, &dos_date);
6031 }
6032#endif
6033
6034 if (max_size <= 3)
6035 level = 0;
6036
6037 if (!mz_zip_writer_write_zeros(pZip, cur_archive_file_ofs, num_alignment_padding_bytes)) {
6038 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
6039 }
6040
6041 cur_archive_file_ofs += num_alignment_padding_bytes;
6042 local_dir_header_ofs = cur_archive_file_ofs;
6043
6044 if (pZip->m_file_offset_alignment) {
6045 MZ_ASSERT((cur_archive_file_ofs & (pZip->m_file_offset_alignment - 1)) == 0);
6046 }
6047
6048 if (max_size && level) {
6049 method = MZ_DEFLATED;
6050 }
6051
6052 MZ_CLEAR_ARR(local_dir_header);
6053 if (pState->m_zip64) {
6054 if (max_size >= MZ_UINT32_MAX || local_dir_header_ofs >= MZ_UINT32_MAX) {
6055 pExtra_data = extra_data;
6056 if (level_and_flags & MZ_ZIP_FLAG_WRITE_HEADER_SET_SIZE)
6057 extra_size = mz_zip_writer_create_zip64_extra_data(extra_data, (max_size >= MZ_UINT32_MAX) ? &uncomp_size : NULL,
6058 (max_size >= MZ_UINT32_MAX) ? &comp_size : NULL,
6059 (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL);
6060 else
6061 extra_size = mz_zip_writer_create_zip64_extra_data(extra_data, NULL,
6062 NULL,
6063 (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL);
6064 }
6065
6066 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))
6067 return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);
6068
6069 if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, local_dir_header, sizeof(local_dir_header)) != sizeof(local_dir_header))
6070 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
6071
6072 cur_archive_file_ofs += sizeof(local_dir_header);
6073
6074 if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pArchive_name, archive_name_size) != archive_name_size) {
6075 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
6076 }
6077
6078 cur_archive_file_ofs += archive_name_size;
6079
6080 if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, extra_data, extra_size) != extra_size)
6081 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
6082
6083 cur_archive_file_ofs += extra_size;
6084 } else {
6085 if ((comp_size > MZ_UINT32_MAX) || (cur_archive_file_ofs > MZ_UINT32_MAX))
6086 return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);
6087 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))
6088 return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);
6089
6090 if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, local_dir_header, sizeof(local_dir_header)) != sizeof(local_dir_header))
6091 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
6092
6093 cur_archive_file_ofs += sizeof(local_dir_header);
6094
6095 if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pArchive_name, archive_name_size) != archive_name_size) {
6096 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
6097 }
6098
6099 cur_archive_file_ofs += archive_name_size;
6100 }
6101
6102 if (user_extra_data_len > 0) {
6103 if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, user_extra_data, user_extra_data_len) != user_extra_data_len)
6104 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
6105
6106 cur_archive_file_ofs += user_extra_data_len;
6107 }
6108
6109 if (max_size) {
6110 void* pRead_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, MZ_ZIP_MAX_IO_BUF_SIZE);
6111 if (!pRead_buf) {
6112 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
6113 }
6114
6115 if (!level) {
6116 while (1) {
6117 size_t n = read_callback(callback_opaque, file_ofs, pRead_buf, MZ_ZIP_MAX_IO_BUF_SIZE);
6118 if (n == 0)
6119 break;
6120
6121 if ((n > MZ_ZIP_MAX_IO_BUF_SIZE) || (file_ofs + n > max_size)) {
6122 pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf);
6123 return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
6124 }
6125 if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pRead_buf, n) != n) {
6126 pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf);
6127 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
6128 }
6129 file_ofs += n;
6130 uncomp_crc32 = (mz_uint32)mz_crc32(uncomp_crc32, (const mz_uint8*)pRead_buf, n);
6131 cur_archive_file_ofs += n;
6132 }
6133 uncomp_size = file_ofs;
6134 comp_size = uncomp_size;
6135 } else {
6136 mz_bool result = MZ_FALSE;
6138 tdefl_compressor* pComp = (tdefl_compressor*)pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, sizeof(tdefl_compressor));
6139 if (!pComp) {
6140 pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf);
6141 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
6142 }
6143
6144 state.m_pZip = pZip;
6145 state.m_cur_archive_file_ofs = cur_archive_file_ofs;
6146 state.m_comp_size = 0;
6147
6149 pZip->m_pFree(pZip->m_pAlloc_opaque, pComp);
6150 pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf);
6151 return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);
6152 }
6153
6154 for (;;) {
6155 tdefl_status status;
6156 tdefl_flush flush = TDEFL_NO_FLUSH;
6157
6158 size_t n = read_callback(callback_opaque, file_ofs, pRead_buf, MZ_ZIP_MAX_IO_BUF_SIZE);
6159 if ((n > MZ_ZIP_MAX_IO_BUF_SIZE) || (file_ofs + n > max_size)) {
6160 mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
6161 break;
6162 }
6163
6164 file_ofs += n;
6165 uncomp_crc32 = (mz_uint32)mz_crc32(uncomp_crc32, (const mz_uint8*)pRead_buf, n);
6166
6167 if (pZip->m_pNeeds_keepalive != NULL && pZip->m_pNeeds_keepalive(pZip->m_pIO_opaque))
6168 flush = TDEFL_FULL_FLUSH;
6169
6170 if (n == 0)
6171 flush = TDEFL_FINISH;
6172
6173 status = tdefl_compress_buffer(pComp, pRead_buf, n, flush);
6174 if (status == TDEFL_STATUS_DONE) {
6175 result = MZ_TRUE;
6176 break;
6177 } else if (status != TDEFL_STATUS_OKAY) {
6178 mz_zip_set_error(pZip, MZ_ZIP_COMPRESSION_FAILED);
6179 break;
6180 }
6181 }
6182
6183 pZip->m_pFree(pZip->m_pAlloc_opaque, pComp);
6184
6185 if (!result) {
6186 pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf);
6187 return MZ_FALSE;
6188 }
6189
6190 uncomp_size = file_ofs;
6191 comp_size = state.m_comp_size;
6192 cur_archive_file_ofs = state.m_cur_archive_file_ofs;
6193 }
6194
6195 pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf);
6196 }
6197
6198 if (!(level_and_flags & MZ_ZIP_FLAG_WRITE_HEADER_SET_SIZE)) {
6199 mz_uint8 local_dir_footer[MZ_ZIP_DATA_DESCRIPTER_SIZE64];
6200 mz_uint32 local_dir_footer_size = MZ_ZIP_DATA_DESCRIPTER_SIZE32;
6201
6202 MZ_WRITE_LE32(local_dir_footer + 0, MZ_ZIP_DATA_DESCRIPTOR_ID);
6203 MZ_WRITE_LE32(local_dir_footer + 4, uncomp_crc32);
6204 if (pExtra_data == NULL) {
6205 if (comp_size > MZ_UINT32_MAX)
6206 return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);
6207
6208 MZ_WRITE_LE32(local_dir_footer + 8, comp_size);
6209 MZ_WRITE_LE32(local_dir_footer + 12, uncomp_size);
6210 } else {
6211 MZ_WRITE_LE64(local_dir_footer + 8, comp_size);
6212 MZ_WRITE_LE64(local_dir_footer + 16, uncomp_size);
6213 local_dir_footer_size = MZ_ZIP_DATA_DESCRIPTER_SIZE64;
6214 }
6215
6216 if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, local_dir_footer, local_dir_footer_size) != local_dir_footer_size)
6217 return MZ_FALSE;
6218
6219 cur_archive_file_ofs += local_dir_footer_size;
6220 }
6221
6222 if (level_and_flags & MZ_ZIP_FLAG_WRITE_HEADER_SET_SIZE) {
6223 if (pExtra_data != NULL) {
6224 extra_size = mz_zip_writer_create_zip64_extra_data(extra_data, (max_size >= MZ_UINT32_MAX) ? &uncomp_size : NULL,
6225 (max_size >= MZ_UINT32_MAX) ? &comp_size : NULL, (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL);
6226 }
6227
6228 if (!mz_zip_writer_create_local_dir_header(pZip, local_dir_header,
6229 (mz_uint16)archive_name_size, (mz_uint16)(extra_size + user_extra_data_len),
6230 (max_size >= MZ_UINT32_MAX) ? MZ_UINT32_MAX : uncomp_size,
6231 (max_size >= MZ_UINT32_MAX) ? MZ_UINT32_MAX : comp_size,
6232 uncomp_crc32, method, gen_flags, dos_time, dos_date))
6233 return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);
6234
6235 cur_archive_header_file_ofs = local_dir_header_ofs;
6236
6237 if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_header_file_ofs, local_dir_header, sizeof(local_dir_header)) != sizeof(local_dir_header))
6238 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
6239
6240 if (pExtra_data != NULL) {
6241 cur_archive_header_file_ofs += sizeof(local_dir_header);
6242
6243 if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_header_file_ofs, pArchive_name, archive_name_size) != archive_name_size) {
6244 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
6245 }
6246
6247 cur_archive_header_file_ofs += archive_name_size;
6248
6249 if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_header_file_ofs, extra_data, extra_size) != extra_size)
6250 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
6251
6252 cur_archive_header_file_ofs += extra_size;
6253 }
6254 }
6255
6256 if (pExtra_data != NULL) {
6257 extra_size = mz_zip_writer_create_zip64_extra_data(extra_data, (uncomp_size >= MZ_UINT32_MAX) ? &uncomp_size : NULL,
6258 (uncomp_size >= MZ_UINT32_MAX) ? &comp_size : NULL, (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL);
6259 }
6260
6261 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,
6262 uncomp_size, comp_size, uncomp_crc32, method, gen_flags, dos_time, dos_date, local_dir_header_ofs, ext_attributes,
6263 user_extra_data_central, user_extra_data_central_len))
6264 return MZ_FALSE;
6265
6266 pZip->m_total_files++;
6267 pZip->m_archive_size = cur_archive_file_ofs;
6268
6269 return MZ_TRUE;
6270}
6271
6272#ifndef MINIZ_NO_STDIO
6273
6274static size_t mz_file_read_func_stdio(void* pOpaque, mz_uint64 file_ofs, void* pBuf, size_t n)
6275{
6276 MZ_FILE* pSrc_file = (MZ_FILE*)pOpaque;
6277 mz_int64 cur_ofs = MZ_FTELL64(pSrc_file);
6278
6279 if (((mz_int64)file_ofs < 0) || (((cur_ofs != (mz_int64)file_ofs)) && (MZ_FSEEK64(pSrc_file, (mz_int64)file_ofs, SEEK_SET))))
6280 return 0;
6281
6282 return MZ_FREAD(pBuf, 1, n, pSrc_file);
6283}
6284
6285mz_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,
6286 const char* user_extra_data, mz_uint user_extra_data_len, const char* user_extra_data_central, mz_uint user_extra_data_central_len)
6287{
6288 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,
6289 user_extra_data, user_extra_data_len, user_extra_data_central, user_extra_data_central_len);
6290}
6291
6292mz_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)
6293{
6294 MZ_FILE* pSrc_file = NULL;
6295 mz_uint64 uncomp_size = 0;
6296 MZ_TIME_T file_modified_time;
6297 MZ_TIME_T* pFile_time = NULL;
6298 mz_bool status;
6299
6300 memset(&file_modified_time, 0, sizeof(file_modified_time));
6301
6302#if !defined(MINIZ_NO_TIME) && !defined(MINIZ_NO_STDIO)
6303 pFile_time = &file_modified_time;
6304 if (!mz_zip_get_file_modified_time(pSrc_filename, &file_modified_time))
6305 return mz_zip_set_error(pZip, MZ_ZIP_FILE_STAT_FAILED);
6306#endif
6307
6308 pSrc_file = MZ_FOPEN(pSrc_filename, "rb");
6309 if (!pSrc_file)
6310 return mz_zip_set_error(pZip, MZ_ZIP_FILE_OPEN_FAILED);
6311
6312 MZ_FSEEK64(pSrc_file, 0, SEEK_END);
6313 uncomp_size = MZ_FTELL64(pSrc_file);
6314 MZ_FSEEK64(pSrc_file, 0, SEEK_SET);
6315
6316 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);
6317
6318 MZ_FCLOSE(pSrc_file);
6319
6320 return status;
6321}
6322#endif /* #ifndef MINIZ_NO_STDIO */
6323
6324static 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)
6325{
6326 /* + 64 should be enough for any new zip64 data */
6327 if (!mz_zip_array_reserve(pZip, pNew_ext, ext_len + 64, MZ_FALSE))
6328 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
6329
6330 mz_zip_array_resize(pZip, pNew_ext, 0, MZ_FALSE);
6331
6332 if ((pUncomp_size) || (pComp_size) || (pLocal_header_ofs) || (pDisk_start)) {
6333 mz_uint8 new_ext_block[64];
6334 mz_uint8* pDst = new_ext_block;
6336 mz_write_le16(pDst + sizeof(mz_uint16), 0);
6337 pDst += sizeof(mz_uint16) * 2;
6338
6339 if (pUncomp_size) {
6340 mz_write_le64(pDst, *pUncomp_size);
6341 pDst += sizeof(mz_uint64);
6342 }
6343
6344 if (pComp_size) {
6345 mz_write_le64(pDst, *pComp_size);
6346 pDst += sizeof(mz_uint64);
6347 }
6348
6349 if (pLocal_header_ofs) {
6350 mz_write_le64(pDst, *pLocal_header_ofs);
6351 pDst += sizeof(mz_uint64);
6352 }
6353
6354 if (pDisk_start) {
6355 mz_write_le32(pDst, *pDisk_start);
6356 pDst += sizeof(mz_uint32);
6357 }
6358
6359 mz_write_le16(new_ext_block + sizeof(mz_uint16), (mz_uint16)((pDst - new_ext_block) - sizeof(mz_uint16) * 2));
6360
6361 if (!mz_zip_array_push_back(pZip, pNew_ext, new_ext_block, pDst - new_ext_block))
6362 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
6363 }
6364
6365 if ((pExt) && (ext_len)) {
6366 mz_uint32 extra_size_remaining = ext_len;
6367 const mz_uint8* pExtra_data = pExt;
6368
6369 do {
6370 mz_uint32 field_id, field_data_size, field_total_size;
6371
6372 if (extra_size_remaining < (sizeof(mz_uint16) * 2))
6373 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
6374
6375 field_id = MZ_READ_LE16(pExtra_data);
6376 field_data_size = MZ_READ_LE16(pExtra_data + sizeof(mz_uint16));
6377 field_total_size = field_data_size + sizeof(mz_uint16) * 2;
6378
6379 if (field_total_size > extra_size_remaining)
6380 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
6381
6383 if (!mz_zip_array_push_back(pZip, pNew_ext, pExtra_data, field_total_size))
6384 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
6385 }
6386
6387 pExtra_data += field_total_size;
6388 extra_size_remaining -= field_total_size;
6389 } while (extra_size_remaining);
6390 }
6391
6392 return MZ_TRUE;
6393}
6394
6395/* TODO: This func is now pretty freakin complex due to zip64, split it up? */
6396mz_bool mz_zip_writer_add_from_zip_reader(mz_zip_archive* pZip, mz_zip_archive* pSource_zip, mz_uint src_file_index)
6397{
6398 mz_uint n, bit_flags, num_alignment_padding_bytes, src_central_dir_following_data_size;
6399 mz_uint64 src_archive_bytes_remaining, local_dir_header_ofs;
6400 mz_uint64 cur_src_file_ofs, cur_dst_file_ofs;
6401 mz_uint32 local_header_u32[(MZ_ZIP_LOCAL_DIR_HEADER_SIZE + sizeof(mz_uint32) - 1) / sizeof(mz_uint32)];
6402 mz_uint8* pLocal_header = (mz_uint8*)local_header_u32;
6403 mz_uint8 new_central_header[MZ_ZIP_CENTRAL_DIR_HEADER_SIZE];
6404 size_t orig_central_dir_size;
6405 mz_zip_internal_state* pState;
6406 void* pBuf;
6407 const mz_uint8* pSrc_central_header;
6408 mz_zip_archive_file_stat src_file_stat;
6409 mz_uint32 src_filename_len, src_comment_len, src_ext_len;
6410 mz_uint32 local_header_filename_size, local_header_extra_len;
6411 mz_uint64 local_header_comp_size, local_header_uncomp_size;
6412 mz_bool found_zip64_ext_data_in_ldir = MZ_FALSE;
6413
6414 /* Sanity checks */
6415 if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || (!pSource_zip->m_pRead))
6416 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
6417
6418 pState = pZip->m_pState;
6419
6420 /* Don't support copying files from zip64 archives to non-zip64, even though in some cases this is possible */
6421 if ((pSource_zip->m_pState->m_zip64) && (!pZip->m_pState->m_zip64))
6422 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
6423
6424 /* Get pointer to the source central dir header and crack it */
6425 if (NULL == (pSrc_central_header = mz_zip_get_cdh(pSource_zip, src_file_index)))
6426 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
6427
6428 if (MZ_READ_LE32(pSrc_central_header + MZ_ZIP_CDH_SIG_OFS) != MZ_ZIP_CENTRAL_DIR_HEADER_SIG)
6429 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
6430
6431 src_filename_len = MZ_READ_LE16(pSrc_central_header + MZ_ZIP_CDH_FILENAME_LEN_OFS);
6432 src_comment_len = MZ_READ_LE16(pSrc_central_header + MZ_ZIP_CDH_COMMENT_LEN_OFS);
6433 src_ext_len = MZ_READ_LE16(pSrc_central_header + MZ_ZIP_CDH_EXTRA_LEN_OFS);
6434 src_central_dir_following_data_size = src_filename_len + src_ext_len + src_comment_len;
6435
6436 /* 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) */
6437 if ((pState->m_central_dir.m_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + src_central_dir_following_data_size + 32) >= MZ_UINT32_MAX)
6438 return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_CDIR_SIZE);
6439
6440 num_alignment_padding_bytes = mz_zip_writer_compute_padding_needed_for_file_alignment(pZip);
6441
6442 if (!pState->m_zip64) {
6443 if (pZip->m_total_files == MZ_UINT16_MAX)
6444 return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);
6445 } else {
6446 /* TODO: Our zip64 support still has some 32-bit limits that may not be worth fixing. */
6447 if (pZip->m_total_files == MZ_UINT32_MAX)
6448 return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);
6449 }
6450
6451 if (!mz_zip_file_stat_internal(pSource_zip, src_file_index, pSrc_central_header, &src_file_stat, NULL))
6452 return MZ_FALSE;
6453
6454 cur_src_file_ofs = src_file_stat.m_local_header_ofs;
6455 cur_dst_file_ofs = pZip->m_archive_size;
6456
6457 /* Read the source archive's local dir header */
6458 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)
6459 return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
6460
6461 if (MZ_READ_LE32(pLocal_header) != MZ_ZIP_LOCAL_DIR_HEADER_SIG)
6462 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
6463
6464 cur_src_file_ofs += MZ_ZIP_LOCAL_DIR_HEADER_SIZE;
6465
6466 /* Compute the total size we need to copy (filename+extra data+compressed data) */
6467 local_header_filename_size = MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_FILENAME_LEN_OFS);
6468 local_header_extra_len = MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_EXTRA_LEN_OFS);
6469 local_header_comp_size = MZ_READ_LE32(pLocal_header + MZ_ZIP_LDH_COMPRESSED_SIZE_OFS);
6470 local_header_uncomp_size = MZ_READ_LE32(pLocal_header + MZ_ZIP_LDH_DECOMPRESSED_SIZE_OFS);
6471 src_archive_bytes_remaining = src_file_stat.m_comp_size + local_header_filename_size + local_header_extra_len;
6472
6473 /* Try to find a zip64 extended information field */
6474 if ((local_header_extra_len) && ((local_header_comp_size == MZ_UINT32_MAX) || (local_header_uncomp_size == MZ_UINT32_MAX))) {
6475 mz_zip_array file_data_array;
6476 const mz_uint8* pExtra_data;
6477 mz_uint32 extra_size_remaining = local_header_extra_len;
6478
6479 mz_zip_array_init(&file_data_array, 1);
6480 if (!mz_zip_array_resize(pZip, &file_data_array, local_header_extra_len, MZ_FALSE)) {
6481 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
6482 }
6483
6484 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) {
6485 mz_zip_array_clear(pZip, &file_data_array);
6486 return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
6487 }
6488
6489 pExtra_data = (const mz_uint8*)file_data_array.m_p;
6490
6491 do {
6492 mz_uint32 field_id, field_data_size, field_total_size;
6493
6494 if (extra_size_remaining < (sizeof(mz_uint16) * 2)) {
6495 mz_zip_array_clear(pZip, &file_data_array);
6496 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
6497 }
6498
6499 field_id = MZ_READ_LE16(pExtra_data);
6500 field_data_size = MZ_READ_LE16(pExtra_data + sizeof(mz_uint16));
6501 field_total_size = field_data_size + sizeof(mz_uint16) * 2;
6502
6503 if (field_total_size > extra_size_remaining) {
6504 mz_zip_array_clear(pZip, &file_data_array);
6505 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
6506 }
6507
6509 const mz_uint8* pSrc_field_data = pExtra_data + sizeof(mz_uint32);
6510
6511 if (field_data_size < sizeof(mz_uint64) * 2) {
6512 mz_zip_array_clear(pZip, &file_data_array);
6513 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
6514 }
6515
6516 local_header_uncomp_size = MZ_READ_LE64(pSrc_field_data);
6517 local_header_comp_size = MZ_READ_LE64(pSrc_field_data + sizeof(mz_uint64)); /* may be 0 if there's a descriptor */
6518
6519 found_zip64_ext_data_in_ldir = MZ_TRUE;
6520 break;
6521 }
6522
6523 pExtra_data += field_total_size;
6524 extra_size_remaining -= field_total_size;
6525 } while (extra_size_remaining);
6526
6527 mz_zip_array_clear(pZip, &file_data_array);
6528 }
6529
6530 if (!pState->m_zip64) {
6531 /* 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). */
6532 /* We also check when the archive is finalized so this doesn't need to be perfect. */
6533 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) + 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;
6534
6535 if (approx_new_archive_size >= MZ_UINT32_MAX)
6536 return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);
6537 }
6538
6539 /* Write dest archive padding */
6540 if (!mz_zip_writer_write_zeros(pZip, cur_dst_file_ofs, num_alignment_padding_bytes))
6541 return MZ_FALSE;
6542
6543 cur_dst_file_ofs += num_alignment_padding_bytes;
6544
6545 local_dir_header_ofs = cur_dst_file_ofs;
6546 if (pZip->m_file_offset_alignment) {
6547 MZ_ASSERT((local_dir_header_ofs & (pZip->m_file_offset_alignment - 1)) == 0);
6548 }
6549
6550 /* 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 */
6551 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)
6552 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
6553
6554 cur_dst_file_ofs += MZ_ZIP_LOCAL_DIR_HEADER_SIZE;
6555
6556 /* Copy over the source archive bytes to the dest archive, also ensure we have enough buf space to handle optional data descriptor */
6557 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)))))
6558 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
6559
6560 while (src_archive_bytes_remaining) {
6561 n = (mz_uint)MZ_MIN((mz_uint64)MZ_ZIP_MAX_IO_BUF_SIZE, src_archive_bytes_remaining);
6562 if (pSource_zip->m_pRead(pSource_zip->m_pIO_opaque, cur_src_file_ofs, pBuf, n) != n) {
6563 pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf);
6564 return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
6565 }
6566 cur_src_file_ofs += n;
6567
6568 if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_dst_file_ofs, pBuf, n) != n) {
6569 pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf);
6570 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
6571 }
6572 cur_dst_file_ofs += n;
6573
6574 src_archive_bytes_remaining -= n;
6575 }
6576
6577 /* Now deal with the optional data descriptor */
6578 bit_flags = MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_BIT_FLAG_OFS);
6579 if (bit_flags & 8) {
6580 /* Copy data descriptor */
6581 if ((pSource_zip->m_pState->m_zip64) || (found_zip64_ext_data_in_ldir)) {
6582 /* src is zip64, dest must be zip64 */
6583
6584 /* name uint32_t's */
6585 /* id 1 (optional in zip64?) */
6586 /* crc 1 */
6587 /* comp_size 2 */
6588 /* uncomp_size 2 */
6589 if (pSource_zip->m_pRead(pSource_zip->m_pIO_opaque, cur_src_file_ofs, pBuf, (sizeof(mz_uint32) * 6)) != (sizeof(mz_uint32) * 6)) {
6590 pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf);
6591 return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
6592 }
6593
6594 n = sizeof(mz_uint32) * ((MZ_READ_LE32(pBuf) == MZ_ZIP_DATA_DESCRIPTOR_ID) ? 6 : 5);
6595 } else {
6596 /* src is NOT zip64 */
6597 mz_bool has_id;
6598
6599 if (pSource_zip->m_pRead(pSource_zip->m_pIO_opaque, cur_src_file_ofs, pBuf, sizeof(mz_uint32) * 4) != sizeof(mz_uint32) * 4) {
6600 pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf);
6601 return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
6602 }
6603
6604 has_id = (MZ_READ_LE32(pBuf) == MZ_ZIP_DATA_DESCRIPTOR_ID);
6605
6606 if (pZip->m_pState->m_zip64) {
6607 /* dest is zip64, so upgrade the data descriptor */
6608 const mz_uint8* pSrc_descriptor = (const mz_uint8*)pBuf + (has_id ? sizeof(mz_uint32) : 0);
6609 const mz_uint32 src_crc32 = MZ_READ_LE32(pSrc_descriptor);
6610 const mz_uint64 src_comp_size = MZ_READ_LE32(pSrc_descriptor + sizeof(mz_uint32));
6611 const mz_uint64 src_uncomp_size = MZ_READ_LE32(pSrc_descriptor + 2 * sizeof(mz_uint32));
6612
6613 mz_write_le32((mz_uint8*)pBuf, MZ_ZIP_DATA_DESCRIPTOR_ID);
6614 mz_write_le32((mz_uint8*)pBuf + sizeof(mz_uint32) * 1, src_crc32);
6615 mz_write_le64((mz_uint8*)pBuf + sizeof(mz_uint32) * 2, src_comp_size);
6616 mz_write_le64((mz_uint8*)pBuf + sizeof(mz_uint32) * 4, src_uncomp_size);
6617
6618 n = sizeof(mz_uint32) * 6;
6619 } else {
6620 /* dest is NOT zip64, just copy it as-is */
6621 n = sizeof(mz_uint32) * (has_id ? 4 : 3);
6622 }
6623 }
6624
6625 if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_dst_file_ofs, pBuf, n) != n) {
6626 pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf);
6627 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
6628 }
6629
6630 cur_src_file_ofs += n;
6631 cur_dst_file_ofs += n;
6632 }
6633 pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf);
6634
6635 /* Finally, add the new central dir header */
6636 orig_central_dir_size = pState->m_central_dir.m_size;
6637
6638 memcpy(new_central_header, pSrc_central_header, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE);
6639
6640 if (pState->m_zip64) {
6641 /* 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. */
6642 const mz_uint8* pSrc_ext = pSrc_central_header + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + src_filename_len;
6643 mz_zip_array new_ext_block;
6644
6645 mz_zip_array_init(&new_ext_block, sizeof(mz_uint8));
6646
6647 MZ_WRITE_LE32(new_central_header + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS, MZ_UINT32_MAX);
6648 MZ_WRITE_LE32(new_central_header + MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS, MZ_UINT32_MAX);
6649 MZ_WRITE_LE32(new_central_header + MZ_ZIP_CDH_LOCAL_HEADER_OFS, MZ_UINT32_MAX);
6650
6651 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)) {
6652 mz_zip_array_clear(pZip, &new_ext_block);
6653 return MZ_FALSE;
6654 }
6655
6656 MZ_WRITE_LE16(new_central_header + MZ_ZIP_CDH_EXTRA_LEN_OFS, new_ext_block.m_size);
6657
6658 if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, new_central_header, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE)) {
6659 mz_zip_array_clear(pZip, &new_ext_block);
6660 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
6661 }
6662
6663 if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, pSrc_central_header + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE, src_filename_len)) {
6664 mz_zip_array_clear(pZip, &new_ext_block);
6665 mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE);
6666 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
6667 }
6668
6669 if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, new_ext_block.m_p, new_ext_block.m_size)) {
6670 mz_zip_array_clear(pZip, &new_ext_block);
6671 mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE);
6672 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
6673 }
6674
6675 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)) {
6676 mz_zip_array_clear(pZip, &new_ext_block);
6677 mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE);
6678 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
6679 }
6680
6681 mz_zip_array_clear(pZip, &new_ext_block);
6682 } else {
6683 /* sanity checks */
6684 if (cur_dst_file_ofs > MZ_UINT32_MAX)
6685 return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);
6686
6687 if (local_dir_header_ofs >= MZ_UINT32_MAX)
6688 return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);
6689
6690 MZ_WRITE_LE32(new_central_header + MZ_ZIP_CDH_LOCAL_HEADER_OFS, local_dir_header_ofs);
6691
6692 if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, new_central_header, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE))
6693 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
6694
6695 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)) {
6696 mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE);
6697 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
6698 }
6699 }
6700
6701 /* This shouldn't trigger unless we screwed up during the initial sanity checks */
6702 if (pState->m_central_dir.m_size >= MZ_UINT32_MAX) {
6703 /* TODO: Support central dirs >= 32-bits in size */
6704 mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE);
6705 return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_CDIR_SIZE);
6706 }
6707
6708 n = (mz_uint32)orig_central_dir_size;
6709 if (!mz_zip_array_push_back(pZip, &pState->m_central_dir_offsets, &n, 1)) {
6710 mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE);
6711 return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
6712 }
6713
6714 pZip->m_total_files++;
6715 pZip->m_archive_size = cur_dst_file_ofs;
6716
6717 return MZ_TRUE;
6718}
6719
6720mz_bool mz_zip_writer_finalize_archive(mz_zip_archive* pZip)
6721{
6722 mz_zip_internal_state* pState;
6723 mz_uint64 central_dir_ofs, central_dir_size;
6724 mz_uint8 hdr[256];
6725
6726 if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING))
6727 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
6728
6729 pState = pZip->m_pState;
6730
6731 if (pState->m_zip64) {
6732 if ((mz_uint64)pState->m_central_dir.m_size >= MZ_UINT32_MAX)
6733 return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);
6734 } else {
6735 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))
6736 return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);
6737 }
6738
6739 central_dir_ofs = 0;
6740 central_dir_size = 0;
6741 if (pZip->m_total_files) {
6742 /* Write central directory */
6743 central_dir_ofs = pZip->m_archive_size;
6744 central_dir_size = pState->m_central_dir.m_size;
6745 pZip->m_central_directory_file_ofs = central_dir_ofs;
6746 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)
6747 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
6748
6749 pZip->m_archive_size += central_dir_size;
6750 }
6751
6752 if (pState->m_zip64) {
6753 /* Write zip64 end of central directory header */
6754 mz_uint64 rel_ofs_to_zip64_ecdr = pZip->m_archive_size;
6755
6756 MZ_CLEAR_ARR(hdr);
6758 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));
6759 MZ_WRITE_LE16(hdr + MZ_ZIP64_ECDH_VERSION_MADE_BY_OFS, 0x031E); /* TODO: always Unix */
6762 MZ_WRITE_LE64(hdr + MZ_ZIP64_ECDH_CDIR_TOTAL_ENTRIES_OFS, pZip->m_total_files);
6763 MZ_WRITE_LE64(hdr + MZ_ZIP64_ECDH_CDIR_SIZE_OFS, central_dir_size);
6764 MZ_WRITE_LE64(hdr + MZ_ZIP64_ECDH_CDIR_OFS_OFS, central_dir_ofs);
6765 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)
6766 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
6767
6768 pZip->m_archive_size += MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE;
6769
6770 /* Write zip64 end of central directory locator */
6771 MZ_CLEAR_ARR(hdr);
6773 MZ_WRITE_LE64(hdr + MZ_ZIP64_ECDL_REL_OFS_TO_ZIP64_ECDR_OFS, rel_ofs_to_zip64_ecdr);
6775 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)
6776 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
6777
6778 pZip->m_archive_size += MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE;
6779 }
6780
6781 /* Write end of central directory record */
6782 MZ_CLEAR_ARR(hdr);
6784 MZ_WRITE_LE16(hdr + MZ_ZIP_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS, MZ_MIN(MZ_UINT16_MAX, pZip->m_total_files));
6785 MZ_WRITE_LE16(hdr + MZ_ZIP_ECDH_CDIR_TOTAL_ENTRIES_OFS, MZ_MIN(MZ_UINT16_MAX, pZip->m_total_files));
6786 MZ_WRITE_LE32(hdr + MZ_ZIP_ECDH_CDIR_SIZE_OFS, MZ_MIN(MZ_UINT32_MAX, central_dir_size));
6787 MZ_WRITE_LE32(hdr + MZ_ZIP_ECDH_CDIR_OFS_OFS, MZ_MIN(MZ_UINT32_MAX, central_dir_ofs));
6788
6789 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)
6790 return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
6791
6792#ifndef MINIZ_NO_STDIO
6793 if ((pState->m_pFile) && (MZ_FFLUSH(pState->m_pFile) == EOF))
6794 return mz_zip_set_error(pZip, MZ_ZIP_FILE_CLOSE_FAILED);
6795#endif /* #ifndef MINIZ_NO_STDIO */
6796
6797 pZip->m_archive_size += MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE;
6798
6799 pZip->m_zip_mode = MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED;
6800 return MZ_TRUE;
6801}
6802
6803mz_bool mz_zip_writer_finalize_heap_archive(mz_zip_archive* pZip, void** ppBuf, size_t* pSize)
6804{
6805 if ((!ppBuf) || (!pSize))
6806 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
6807
6808 *ppBuf = NULL;
6809 *pSize = 0;
6810
6811 if ((!pZip) || (!pZip->m_pState))
6812 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
6813
6814 if (pZip->m_pWrite != mz_zip_heap_write_func)
6815 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
6816
6818 return MZ_FALSE;
6819
6820 *ppBuf = pZip->m_pState->m_pMem;
6821 *pSize = pZip->m_pState->m_mem_size;
6822 pZip->m_pState->m_pMem = NULL;
6823 pZip->m_pState->m_mem_size = pZip->m_pState->m_mem_capacity = 0;
6824
6825 return MZ_TRUE;
6826}
6827
6828mz_bool mz_zip_writer_end(mz_zip_archive* pZip)
6829{
6830 return mz_zip_writer_end_internal(pZip, MZ_TRUE);
6831}
6832
6833#ifndef MINIZ_NO_STDIO
6834mz_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)
6835{
6836 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);
6837}
6838
6839mz_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)
6840{
6841 mz_bool status, created_new_archive = MZ_FALSE;
6842 mz_zip_archive zip_archive;
6843 struct MZ_FILE_STAT_STRUCT file_stat;
6844 mz_zip_error actual_err = MZ_ZIP_NO_ERROR;
6845
6846 mz_zip_zero_struct(&zip_archive);
6847 if ((int)level_and_flags < 0)
6848 level_and_flags = MZ_DEFAULT_LEVEL;
6849
6850 if ((!pZip_filename) || (!pArchive_name) || ((buf_size) && (!pBuf)) || ((comment_size) && (!pComment)) || ((level_and_flags & 0xF) > MZ_UBER_COMPRESSION)) {
6851 if (pErr)
6852 *pErr = MZ_ZIP_INVALID_PARAMETER;
6853 return MZ_FALSE;
6854 }
6855
6856 if (!mz_zip_writer_validate_archive_name(pArchive_name)) {
6857 if (pErr)
6858 *pErr = MZ_ZIP_INVALID_FILENAME;
6859 return MZ_FALSE;
6860 }
6861
6862 /* 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. */
6863 /* So be sure to compile with _LARGEFILE64_SOURCE 1 */
6864 if (MZ_FILE_STAT(pZip_filename, &file_stat) != 0) {
6865 /* Create a new archive. */
6866 if (!mz_zip_writer_init_file_v2(&zip_archive, pZip_filename, 0, level_and_flags)) {
6867 if (pErr)
6868 *pErr = zip_archive.m_last_error;
6869 return MZ_FALSE;
6870 }
6871
6872 created_new_archive = MZ_TRUE;
6873 } else {
6874 /* Append to an existing archive. */
6875 if (!mz_zip_reader_init_file_v2(&zip_archive, pZip_filename, level_and_flags | MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY, 0, 0)) {
6876 if (pErr)
6877 *pErr = zip_archive.m_last_error;
6878 return MZ_FALSE;
6879 }
6880
6881 if (!mz_zip_writer_init_from_reader_v2(&zip_archive, pZip_filename, level_and_flags)) {
6882 if (pErr)
6883 *pErr = zip_archive.m_last_error;
6884
6885 mz_zip_reader_end_internal(&zip_archive, MZ_FALSE);
6886
6887 return MZ_FALSE;
6888 }
6889 }
6890
6891 status = mz_zip_writer_add_mem_ex(&zip_archive, pArchive_name, pBuf, buf_size, pComment, comment_size, level_and_flags, 0, 0);
6892 actual_err = zip_archive.m_last_error;
6893
6894 /* 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.) */
6895 if (!mz_zip_writer_finalize_archive(&zip_archive)) {
6896 if (!actual_err)
6897 actual_err = zip_archive.m_last_error;
6898
6899 status = MZ_FALSE;
6900 }
6901
6902 if (!mz_zip_writer_end_internal(&zip_archive, status)) {
6903 if (!actual_err)
6904 actual_err = zip_archive.m_last_error;
6905
6906 status = MZ_FALSE;
6907 }
6908
6909 if ((!status) && (created_new_archive)) {
6910 /* It's a new archive and something went wrong, so just delete it. */
6911 int ignoredStatus = MZ_DELETE_FILE(pZip_filename);
6912 (void)ignoredStatus;
6913 }
6914
6915 if (pErr)
6916 *pErr = actual_err;
6917
6918 return status;
6919}
6920
6921void* 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)
6922{
6923 mz_uint32 file_index;
6924 mz_zip_archive zip_archive;
6925 void* p = NULL;
6926
6927 if (pSize)
6928 *pSize = 0;
6929
6930 if ((!pZip_filename) || (!pArchive_name)) {
6931 if (pErr)
6932 *pErr = MZ_ZIP_INVALID_PARAMETER;
6933
6934 return NULL;
6935 }
6936
6937 mz_zip_zero_struct(&zip_archive);
6938 if (!mz_zip_reader_init_file_v2(&zip_archive, pZip_filename, flags | MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY, 0, 0)) {
6939 if (pErr)
6940 *pErr = zip_archive.m_last_error;
6941
6942 return NULL;
6943 }
6944
6945 if (mz_zip_reader_locate_file_v2(&zip_archive, pArchive_name, pComment, flags, &file_index)) {
6946 p = mz_zip_reader_extract_to_heap(&zip_archive, file_index, pSize, flags);
6947 }
6948
6949 mz_zip_reader_end_internal(&zip_archive, p != NULL);
6950
6951 if (pErr)
6952 *pErr = zip_archive.m_last_error;
6953
6954 return p;
6955}
6956
6957void* mz_zip_extract_archive_file_to_heap(const char* pZip_filename, const char* pArchive_name, size_t* pSize, mz_uint flags)
6958{
6959 return mz_zip_extract_archive_file_to_heap_v2(pZip_filename, pArchive_name, NULL, pSize, flags, NULL);
6960}
6961
6962#endif /* #ifndef MINIZ_NO_STDIO */
6963
6964#endif /* #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS */
6965
6966/* ------------------- Misc utils */
6967
6968mz_zip_mode mz_zip_get_mode(mz_zip_archive* pZip)
6969{
6970 return pZip ? pZip->m_zip_mode : MZ_ZIP_MODE_INVALID;
6971}
6972
6973mz_zip_type mz_zip_get_type(mz_zip_archive* pZip)
6974{
6975 return pZip ? pZip->m_zip_type : MZ_ZIP_TYPE_INVALID;
6976}
6977
6978mz_zip_error mz_zip_set_last_error(mz_zip_archive* pZip, mz_zip_error err_num)
6979{
6980 mz_zip_error prev_err;
6981
6982 if (!pZip)
6983 return MZ_ZIP_INVALID_PARAMETER;
6984
6985 prev_err = pZip->m_last_error;
6986
6987 pZip->m_last_error = err_num;
6988 return prev_err;
6989}
6990
6991mz_zip_error mz_zip_peek_last_error(mz_zip_archive* pZip)
6992{
6993 if (!pZip)
6994 return MZ_ZIP_INVALID_PARAMETER;
6995
6996 return pZip->m_last_error;
6997}
6998
6999mz_zip_error mz_zip_clear_last_error(mz_zip_archive* pZip)
7000{
7001 return mz_zip_set_last_error(pZip, MZ_ZIP_NO_ERROR);
7002}
7003
7004mz_zip_error mz_zip_get_last_error(mz_zip_archive* pZip)
7005{
7006 mz_zip_error prev_err;
7007
7008 if (!pZip)
7009 return MZ_ZIP_INVALID_PARAMETER;
7010
7011 prev_err = pZip->m_last_error;
7012
7013 pZip->m_last_error = MZ_ZIP_NO_ERROR;
7014 return prev_err;
7015}
7016
7017const char* mz_zip_get_error_string(mz_zip_error mz_err)
7018{
7019 switch (mz_err) {
7020 case MZ_ZIP_NO_ERROR:
7021 return "no error";
7022 case MZ_ZIP_UNDEFINED_ERROR:
7023 return "undefined error";
7024 case MZ_ZIP_TOO_MANY_FILES:
7025 return "too many files";
7026 case MZ_ZIP_FILE_TOO_LARGE:
7027 return "file too large";
7028 case MZ_ZIP_UNSUPPORTED_METHOD:
7029 return "unsupported method";
7030 case MZ_ZIP_UNSUPPORTED_ENCRYPTION:
7031 return "unsupported encryption";
7032 case MZ_ZIP_UNSUPPORTED_FEATURE:
7033 return "unsupported feature";
7034 case MZ_ZIP_FAILED_FINDING_CENTRAL_DIR:
7035 return "failed finding central directory";
7036 case MZ_ZIP_NOT_AN_ARCHIVE:
7037 return "not a ZIP archive";
7038 case MZ_ZIP_INVALID_HEADER_OR_CORRUPTED:
7039 return "invalid header or archive is corrupted";
7040 case MZ_ZIP_UNSUPPORTED_MULTIDISK:
7041 return "unsupported multidisk archive";
7042 case MZ_ZIP_DECOMPRESSION_FAILED:
7043 return "decompression failed or archive is corrupted";
7044 case MZ_ZIP_COMPRESSION_FAILED:
7045 return "compression failed";
7046 case MZ_ZIP_UNEXPECTED_DECOMPRESSED_SIZE:
7047 return "unexpected decompressed size";
7048 case MZ_ZIP_CRC_CHECK_FAILED:
7049 return "CRC-32 check failed";
7050 case MZ_ZIP_UNSUPPORTED_CDIR_SIZE:
7051 return "unsupported central directory size";
7052 case MZ_ZIP_ALLOC_FAILED:
7053 return "allocation failed";
7054 case MZ_ZIP_FILE_OPEN_FAILED:
7055 return "file open failed";
7056 case MZ_ZIP_FILE_CREATE_FAILED:
7057 return "file create failed";
7058 case MZ_ZIP_FILE_WRITE_FAILED:
7059 return "file write failed";
7060 case MZ_ZIP_FILE_READ_FAILED:
7061 return "file read failed";
7062 case MZ_ZIP_FILE_CLOSE_FAILED:
7063 return "file close failed";
7064 case MZ_ZIP_FILE_SEEK_FAILED:
7065 return "file seek failed";
7066 case MZ_ZIP_FILE_STAT_FAILED:
7067 return "file stat failed";
7068 case MZ_ZIP_INVALID_PARAMETER:
7069 return "invalid parameter";
7070 case MZ_ZIP_INVALID_FILENAME:
7071 return "invalid filename";
7072 case MZ_ZIP_BUF_TOO_SMALL:
7073 return "buffer too small";
7074 case MZ_ZIP_INTERNAL_ERROR:
7075 return "internal error";
7076 case MZ_ZIP_FILE_NOT_FOUND:
7077 return "file not found";
7078 case MZ_ZIP_ARCHIVE_TOO_LARGE:
7079 return "archive is too large";
7080 case MZ_ZIP_VALIDATION_FAILED:
7081 return "validation failed";
7082 case MZ_ZIP_WRITE_CALLBACK_FAILED:
7083 return "write callback failed";
7084 case MZ_ZIP_TOTAL_ERRORS:
7085 return "total errors";
7086 default:
7087 break;
7088 }
7089
7090 return "unknown error";
7091}
7092
7093/* Note: Just because the archive is not zip64 doesn't necessarily mean it doesn't have Zip64 extended information extra field, argh. */
7094mz_bool mz_zip_is_zip64(mz_zip_archive* pZip)
7095{
7096 if ((!pZip) || (!pZip->m_pState))
7097 return MZ_FALSE;
7098
7099 return pZip->m_pState->m_zip64;
7100}
7101
7102size_t mz_zip_get_central_dir_size(mz_zip_archive* pZip)
7103{
7104 if ((!pZip) || (!pZip->m_pState))
7105 return 0;
7106
7107 return pZip->m_pState->m_central_dir.m_size;
7108}
7109
7110mz_uint mz_zip_reader_get_num_files(mz_zip_archive* pZip)
7111{
7112 return pZip ? pZip->m_total_files : 0;
7113}
7114
7115mz_uint64 mz_zip_get_archive_size(mz_zip_archive* pZip)
7116{
7117 if (!pZip)
7118 return 0;
7119 return pZip->m_archive_size;
7120}
7121
7122mz_uint64 mz_zip_get_archive_file_start_offset(mz_zip_archive* pZip)
7123{
7124 if ((!pZip) || (!pZip->m_pState))
7125 return 0;
7126 return pZip->m_pState->m_file_archive_start_ofs;
7127}
7128
7129MZ_FILE* mz_zip_get_cfile(mz_zip_archive* pZip)
7130{
7131 if ((!pZip) || (!pZip->m_pState))
7132 return 0;
7133 return pZip->m_pState->m_pFile;
7134}
7135
7136size_t mz_zip_read_archive_data(mz_zip_archive* pZip, mz_uint64 file_ofs, void* pBuf, size_t n)
7137{
7138 if ((!pZip) || (!pZip->m_pState) || (!pBuf) || (!pZip->m_pRead))
7139 return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
7140
7141 return pZip->m_pRead(pZip->m_pIO_opaque, file_ofs, pBuf, n);
7142}
7143
7144mz_uint mz_zip_reader_get_filename(mz_zip_archive* pZip, mz_uint file_index, char* pFilename, mz_uint filename_buf_size)
7145{
7146 mz_uint n;
7147 const mz_uint8* p = mz_zip_get_cdh(pZip, file_index);
7148 if (!p) {
7149 if (filename_buf_size)
7150 pFilename[0] = '\0';
7151 mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
7152 return 0;
7153 }
7154 n = MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS);
7155 if (filename_buf_size) {
7156 n = MZ_MIN(n, filename_buf_size - 1);
7157 memcpy(pFilename, p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE, n);
7158 pFilename[n] = '\0';
7159 }
7160 return n + 1;
7161}
7162
7163mz_bool mz_zip_reader_file_stat(mz_zip_archive* pZip, mz_uint file_index, mz_zip_archive_file_stat* pStat)
7164{
7165 return mz_zip_file_stat_internal(pZip, file_index, mz_zip_get_cdh(pZip, file_index), pStat, NULL);
7166}
7167
7168mz_bool mz_zip_end(mz_zip_archive* pZip)
7169{
7170 if (!pZip)
7171 return MZ_FALSE;
7172
7173 if (pZip->m_zip_mode == MZ_ZIP_MODE_READING)
7174 return mz_zip_reader_end(pZip);
7175#ifndef MINIZ_NO_ARCHIVE_WRITING_APIS
7176 else if ((pZip->m_zip_mode == MZ_ZIP_MODE_WRITING) || (pZip->m_zip_mode == MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED))
7177 return mz_zip_writer_end(pZip);
7178#endif
7179
7180 return MZ_FALSE;
7181}
7182
7183#ifdef __cplusplus
7184}
7185#endif
7186
7187#endif /*#ifndef MINIZ_NO_ARCHIVE_APIS*/
static GLfloat pA[6]
float y
voidpf void uLong size
Definition ioapi.h:126
voidpf stream
Definition ioapi.h:126
typedef int(ZCALLBACK *close_file_func) OF((voidpf opaque
voidpf void * buf
Definition ioapi.h:126
#define MZ_WRITE_LE32(p, v)
Definition miniz.c:5199
#define MZ_FILE_STAT
Definition miniz.c:2935
#define TINFL_HUFF_DECODE(state_index, sym, pLookUp, pTree)
Definition miniz.c:2192
mz_ulong mz_adler32(mz_ulong adler, const unsigned char *ptr, size_t buf_len)
Definition miniz.c:39
static mz_bool mz_zip_writer_add_to_central_dir(mz_zip_archive *pZip, const char *pFilename, mz_uint16 filename_size, const void *pExtra, mz_uint16 extra_size, const void *pComment, mz_uint16 comment_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, mz_uint64 local_header_ofs, mz_uint32 ext_attributes, const char *user_extra_data, mz_uint user_extra_data_len)
Definition miniz.c:5630
mz_bool mz_zip_is_zip64(mz_zip_archive *pZip)
Definition miniz.c:7094
static MZ_FORCEINLINE mz_bool mz_zip_array_ensure_room(mz_zip_archive *pZip, mz_zip_array *pArray, size_t n)
Definition miniz.c:3126
static const mz_uint8 s_tdefl_len_extra[256]
Definition miniz.c:671
mz_bool mz_zip_reader_end(mz_zip_archive *pZip)
Definition miniz.c:3640
int mz_uncompress2(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong *pSource_len)
Definition miniz.c:540
mz_bool mz_zip_validate_archive(mz_zip_archive *pZip, mz_uint flags)
Definition miniz.c:5055
mz_bool mz_zip_writer_finalize_archive(mz_zip_archive *pZip)
Definition miniz.c:6720
mz_zip_type mz_zip_get_type(mz_zip_archive *pZip)
Definition miniz.c:6973
static const mz_uint s_tdefl_num_probes[11]
Definition miniz.c:1164
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, const char *user_extra_data, mz_uint user_extra_data_len, const char *user_extra_data_central, mz_uint user_extra_data_central_len)
Definition miniz.c:5954
static size_t mz_zip_file_read_func(void *pOpaque, mz_uint64 file_ofs, void *pBuf, size_t n)
Definition miniz.c:3705
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)
Definition miniz.c:1861
static const mz_uint16 s_tdefl_len_sym[256]
Definition miniz.c:660
mz_bool mz_zip_end(mz_zip_archive *pZip)
Definition miniz.c:7168
static const mz_uint8 s_tdefl_packed_code_size_syms_swizzle[]
Definition miniz.c:924
static int tdefl_flush_block(tdefl_compressor *d, int flush)
Definition miniz.c:1166
static MZ_FORCEINLINE void mz_write_le32(mz_uint8 *p, mz_uint32 v)
Definition miniz.c:5185
#define TINFL_CR_BEGIN
Definition miniz.c:2106
mz_uint mz_zip_reader_get_num_files(mz_zip_archive *pZip)
Definition miniz.c:7110
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)
Definition miniz.c:1965
mz_bool mz_zip_writer_init_file(mz_zip_archive *pZip, const char *pFilename, mz_uint64 size_to_reserve_at_beginning)
Definition miniz.c:5377
#define MZ_WRITE_LE16(p, v)
Definition miniz.c:5198
void * tdefl_compress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, size_t *pOut_len, int flags)
Definition miniz.c:1906
static void mz_zip_reader_sort_central_dir_offsets_by_filename(mz_zip_archive *pZip)
Definition miniz.c:3274
static void tdefl_start_dynamic_block(tdefl_compressor *d)
Definition miniz.c:926
mz_bool mz_zip_reader_init_file(mz_zip_archive *pZip, const char *pFilename, mz_uint32 flags)
Definition miniz.c:3718
int mz_deflateReset(mz_streamp pStream)
Definition miniz.c:226
#define TINFL_GET_BYTE(state_index, c)
Definition miniz.c:2126
#define MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(array_ptr, element_size)
Definition miniz.c:3063
static mz_bool mz_zip_writer_end_internal(mz_zip_archive *pZip, mz_bool set_last_error)
Definition miniz.c:5237
size_t mz_zip_get_central_dir_size(mz_zip_archive *pZip)
Definition miniz.c:7102
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)
Definition miniz.c:3893
#define MZ_FFLUSH
Definition miniz.c:2936
int mz_inflateInit(mz_streamp pStream)
Definition miniz.c:393
int mz_inflate(mz_streamp pStream, int flush)
Definition miniz.c:424
int mz_compress(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len)
Definition miniz.c:334
mz_uint32 tdefl_get_adler32(tdefl_compressor *d)
Definition miniz.c:1856
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)
Definition miniz.c:4506
#define TDEFL_RLE_ZERO_CODE_SIZE()
Definition miniz.c:904
mz_ulong mz_compressBound(mz_ulong source_len)
Definition miniz.c:339
static mz_bool mz_zip_set_file_times(const char *pFilename, MZ_TIME_T access_time, MZ_TIME_T modified_time)
Definition miniz.c:3193
static MZ_FORCEINLINE void mz_write_le16(mz_uint8 *p, mz_uint16 v)
Definition miniz.c:5180
mz_zip_reader_extract_iter_state * mz_zip_reader_extract_iter_new(mz_zip_archive *pZip, mz_uint file_index, mz_uint flags)
Definition miniz.c:4515
static mz_bool mz_zip_reader_locate_header_sig(mz_zip_archive *pZip, mz_uint32 record_sig, mz_uint32 record_size, mz_int64 *pOfs)
Definition miniz.c:3322
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)
Definition miniz.c:6834
int mz_deflateEnd(mz_streamp pStream)
Definition miniz.c:287
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)
Definition miniz.c:1921
mz_zip_mode mz_zip_get_mode(mz_zip_archive *pZip)
Definition miniz.c:6968
#define MZ_FREOPEN(f, m, s)
Definition miniz.c:2937
static void tdefl_calculate_minimum_redundancy(tdefl_sym_freq *A, int n)
Definition miniz.c:752
static const mz_uint8 s_tdefl_small_dist_sym[512]
Definition miniz.c:678
MINIZ_EXPORT void * miniz_def_alloc_func(void *opaque, size_t items, size_t size)
Definition miniz.c:161
tdefl_status tdefl_init(tdefl_compressor *d, tdefl_put_buf_func_ptr pPut_buf_func, void *pPut_buf_user, int flags)
Definition miniz.c:1815
static mz_bool tdefl_compress_lz_codes(tdefl_compressor *d)
Definition miniz.c:1114
MINIZ_EXPORT void * miniz_def_realloc_func(void *opaque, void *address, size_t items, size_t size)
Definition miniz.c:171
size_t mz_zip_read_archive_data(mz_zip_archive *pZip, mz_uint64 file_ofs, void *pBuf, size_t n)
Definition miniz.c:7136
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)
Definition miniz.c:2693
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)
Definition miniz.c:5382
static size_t mz_zip_file_write_func(void *pOpaque, mz_uint64 file_ofs, const void *pBuf, size_t n)
Definition miniz.c:5362
mz_bool mz_zip_reader_is_file_a_directory(mz_zip_archive *pZip, mz_uint file_index)
Definition miniz.c:3864
void * tinfl_decompress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, size_t *pOut_len, int flags)
Definition miniz.c:2649
static mz_bool tdefl_output_buffer_putter(const void *pBuf, int len, void *pUser)
Definition miniz.c:1883
static mz_bool mz_zip_get_file_modified_time(const char *pFilename, MZ_TIME_T *pTime)
Definition miniz.c:3179
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)
Definition miniz.c:6839
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)
Definition miniz.c:4267
unsigned char mz_validate_uint32[sizeof(mz_uint32)==4 ? 1 :-1]
Definition miniz.c:30
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, mz_uint64 uncomp_size, mz_uint32 uncomp_crc32)
Definition miniz.c:5698
#define TINFL_MEMCPY(d, s, l)
Definition miniz.c:2103
#define TINFL_GET_BITS(state_index, b, n)
Definition miniz.c:2151
mz_bool mz_zip_reader_locate_file_v2(mz_zip_archive *pZip, const char *pName, const char *pComment, mz_uint flags, mz_uint32 *pIndex)
Definition miniz.c:4078
static const mz_uint8 s_tdefl_small_dist_extra[512]
Definition miniz.c:693
tdefl_status tdefl_get_prev_return_status(tdefl_compressor *d)
Definition miniz.c:1851
static void tinfl_clear_tree(tinfl_decompressor *r)
Definition miniz.c:2219
mz_bool mz_zip_writer_init_heap(mz_zip_archive *pZip, size_t size_to_reserve_at_beginning, size_t initial_allocation_size)
Definition miniz.c:5356
#define MZ_FREAD
Definition miniz.c:2925
static mz_bool mz_zip_reader_init_internal(mz_zip_archive *pZip, mz_uint flags)
Definition miniz.c:3213
static mz_bool mz_zip_reader_end_internal(mz_zip_archive *pZip, mz_bool set_last_error)
Definition miniz.c:3598
mz_zip_error mz_zip_peek_last_error(mz_zip_archive *pZip)
Definition miniz.c:6991
mz_bool mz_zip_reader_init(mz_zip_archive *pZip, mz_uint64 size, mz_uint flags)
Definition miniz.c:3644
mz_zip_error mz_zip_get_last_error(mz_zip_archive *pZip)
Definition miniz.c:7004
mz_bool mz_zip_reader_extract_file_to_file(mz_zip_archive *pZip, const char *pArchive_filename, const char *pDst_filename, mz_uint flags)
Definition miniz.c:4822
#define MZ_FWRITE
Definition miniz.c:2926
tdefl_status tdefl_compress_buffer(tdefl_compressor *d, const void *pIn_buf, size_t in_buf_size, tdefl_flush flush)
Definition miniz.c:1809
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)
Definition miniz.c:4334
mz_bool mz_zip_validate_file_archive(const char *pFilename, mz_uint flags, mz_zip_error *pErr)
Definition miniz.c:5138
#define MZ_TOLOWER(c)
Definition miniz.c:2942
static const mz_uint8 s_tdefl_large_dist_extra[128]
Definition miniz.c:710
int mz_deflateInit2(mz_streamp pStream, int level, int method, int window_bits, int mem_level, int strategy)
Definition miniz.c:191
mz_bool mz_zip_reader_init_mem(mz_zip_archive *pZip, const void *pMem, size_t size, mz_uint flags)
Definition miniz.c:3671
const char * mz_version(void)
Definition miniz.c:177
static mz_bool tdefl_compress_normal(tdefl_compressor *d)
Definition miniz.c:1612
int mz_deflateInit(mz_streamp pStream, int level)
Definition miniz.c:186
static size_t mz_zip_file_write_callback(void *pOpaque, mz_uint64 ofs, const void *pBuf, size_t n)
Definition miniz.c:4782
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)
Definition miniz.c:6921
#define MZ_FSEEK64
Definition miniz.c:2932
static MZ_FORCEINLINE void mz_zip_array_init(mz_zip_array *pArray, mz_uint32 element_size)
Definition miniz.c:3076
const char * mz_zip_get_error_string(mz_zip_error mz_err)
Definition miniz.c:7017
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)
Definition miniz.c:1362
void * mz_zip_reader_extract_to_heap(mz_zip_archive *pZip, mz_uint file_index, size_t *pSize, mz_uint flags)
Definition miniz.c:4290
static MZ_FORCEINLINE const mz_uint8 * mz_zip_get_cdh(mz_zip_archive *pZip, mz_uint file_index)
Definition miniz.c:3812
static void tdefl_huffman_enforce_max_code_size(int *pNum_codes, int code_list_len, int max_code_size)
Definition miniz.c:802
static void tdefl_start_static_block(tdefl_compressor *d)
Definition miniz.c:1001
int mz_compress2(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len, int level)
Definition miniz.c:305
#define MZ_FOPEN(f, m)
Definition miniz.c:2923
static mz_bool mz_zip_reader_read_central_dir(mz_zip_archive *pZip, mz_uint flags)
Definition miniz.c:3364
mz_bool mz_zip_writer_end(mz_zip_archive *pZip)
Definition miniz.c:6828
static mz_bool mz_zip_writer_create_central_dir_header(mz_zip_archive *pZip, mz_uint8 *pDst, mz_uint16 filename_size, mz_uint16 extra_size, mz_uint16 comment_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, mz_uint64 local_header_ofs, mz_uint32 ext_attributes)
Definition miniz.c:5605
void mz_free(void *p)
Definition miniz.c:156
mz_bool mz_zip_writer_init_cfile(mz_zip_archive *pZip, MZ_FILE *pFile, mz_uint flags)
Definition miniz.c:5425
#define TINFL_MEMSET(p, c, l)
Definition miniz.c:2104
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)
Definition miniz.c:6292
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)
Definition miniz.c:4135
static size_t mz_zip_heap_write_func(void *pOpaque, mz_uint64 file_ofs, const void *pBuf, size_t n)
Definition miniz.c:5202
static void mz_zip_time_t_to_dos_time(MZ_TIME_T time, mz_uint16 *pDOS_time, mz_uint16 *pDOS_date)
Definition miniz.c:3157
int mz_inflateReset(mz_streamp pStream)
Definition miniz.c:398
int mz_zip_reader_locate_file(mz_zip_archive *pZip, const char *pName, const char *pComment, mz_uint flags)
Definition miniz.c:4069
static MZ_FORCEINLINE mz_bool mz_zip_array_push_back(mz_zip_archive *pZip, mz_zip_array *pArray, const void *pElements, size_t n)
Definition miniz.c:3131
static MZ_TIME_T mz_zip_dos_to_time_t(int dos_time, int dos_date)
Definition miniz.c:3142
mz_bool mz_zip_reader_is_file_encrypted(mz_zip_archive *pZip, mz_uint file_index)
Definition miniz.c:3819
mz_bool mz_zip_writer_init(mz_zip_archive *pZip, mz_uint64 existing_size)
Definition miniz.c:5325
const char * mz_error(int err)
Definition miniz.c:577
mz_ulong mz_crc32(mz_ulong crc, const mz_uint8 *ptr, size_t buf_len)
Definition miniz.c:92
void mz_zip_zero_struct(mz_zip_archive *pZip)
Definition miniz.c:3592
static MZ_FORCEINLINE void mz_zip_array_clear(mz_zip_archive *pZip, mz_zip_array *pArray)
Definition miniz.c:3082
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)
Definition miniz.c:4017
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)
Definition miniz.c:5555
mz_uint64 mz_zip_get_archive_size(mz_zip_archive *pZip)
Definition miniz.c:7115
size_t mz_zip_reader_extract_iter_read(mz_zip_reader_extract_iter_state *pState, void *pvBuf, size_t buf_size)
Definition miniz.c:4639
MZ_FILE * mz_zip_get_cfile(mz_zip_archive *pZip)
Definition miniz.c:7129
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)
Definition miniz.c:6324
static mz_uint mz_zip_writer_compute_padding_needed_for_file_alignment(mz_zip_archive *pZip)
Definition miniz.c:5674
int mz_inflateInit2(mz_streamp pStream, int window_bits)
Definition miniz.c:357
static mz_bool mz_zip_writer_add_put_buf_callback(const void *pBuf, int len, void *pUser)
Definition miniz.c:5542
static mz_bool mz_zip_writer_validate_archive_name(const char *pArchive_name)
Definition miniz.c:5663
static size_t mz_zip_mem_read_func(void *pOpaque, mz_uint64 file_ofs, void *pBuf, size_t n)
Definition miniz.c:3663
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)
Definition miniz.c:3247
static MZ_FORCEINLINE mz_bool mz_zip_set_error(mz_zip_archive *pZip, mz_zip_error err_num)
Definition miniz.c:3206
mz_bool mz_zip_writer_add_from_zip_reader(mz_zip_archive *pZip, mz_zip_archive *pSource_zip, mz_uint src_file_index)
Definition miniz.c:6396
mz_bool mz_zip_reader_is_file_supported(mz_zip_archive *pZip, mz_uint file_index)
Definition miniz.c:3832
#define TINFL_CR_FINISH
Definition miniz.c:2124
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)
Definition miniz.c:4280
#define TINFL_CR_RETURN(state_index, result)
Definition miniz.c:2109
#define TINFL_CR_RETURN_FOREVER(state_index, result)
Definition miniz.c:2117
mz_zip_error mz_zip_set_last_error(mz_zip_archive *pZip, mz_zip_error err_num)
Definition miniz.c:6978
MINIZ_EXPORT void miniz_def_free_func(void *opaque, void *address)
Definition miniz.c:166
tinfl_decompressor * tinfl_decompressor_alloc(void)
Definition miniz.c:2722
int mz_uncompress(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len)
Definition miniz.c:570
#define MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE
Definition miniz.c:5554
static MZ_FORCEINLINE void tdefl_record_literal(tdefl_compressor *d, mz_uint8 lit)
Definition miniz.c:1573
mz_zip_reader_extract_iter_state * mz_zip_reader_extract_file_iter_new(mz_zip_archive *pZip, const char *pFilename, mz_uint flags)
Definition miniz.c:4627
void * mz_zip_extract_archive_file_to_heap(const char *pZip_filename, const char *pArchive_name, size_t *pSize, mz_uint flags)
Definition miniz.c:6957
static MZ_FORCEINLINE mz_bool mz_zip_array_reserve(mz_zip_archive *pZip, mz_zip_array *pArray, size_t new_capacity, mz_uint growing)
Definition miniz.c:3107
mz_bool mz_zip_reader_file_stat(mz_zip_archive *pZip, mz_uint file_index, mz_zip_archive_file_stat *pStat)
Definition miniz.c:7163
mz_bool mz_zip_writer_init_from_reader_v2(mz_zip_archive *pZip, const char *pFilename, mz_uint flags)
Definition miniz.c:5446
static mz_bool mz_zip_locate_file_binary_search(mz_zip_archive *pZip, const char *pFilename, mz_uint32 *pIndex)
Definition miniz.c:4033
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)
Definition miniz.c:3723
void tdefl_compressor_free(tdefl_compressor *pComp)
Definition miniz.c:2054
mz_uint64 mz_zip_get_archive_file_start_offset(mz_zip_archive *pZip)
Definition miniz.c:7122
unsigned char mz_validate_uint64[sizeof(mz_uint64)==8 ? 1 :-1]
Definition miniz.c:31
int mz_inflateEnd(mz_streamp pStream)
Definition miniz.c:530
@ TDEFL_MAX_SUPPORTED_HUFF_CODESIZE
Definition miniz.c:800
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)
Definition miniz.c:5530
static const mz_uint mz_bitmasks[17]
Definition miniz.c:1023
void * mz_zip_reader_extract_file_to_heap(mz_zip_archive *pZip, const char *pFilename, size_t *pSize, mz_uint flags)
Definition miniz.c:4323
int mz_deflate(mz_streamp pStream, int flush)
Definition miniz.c:235
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)
Definition miniz.c:4285
#define MZ_FILE_STAT_STRUCT
Definition miniz.c:2934
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, const char *user_extra_data, mz_uint user_extra_data_len, const char *user_extra_data_central, mz_uint user_extra_data_central_len)
Definition miniz.c:6285
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)
Definition miniz.c:4272
static mz_bool mz_zip_writer_write_zeros(mz_zip_archive *pZip, mz_uint64 cur_file_ofs, mz_uint32 n)
Definition miniz.c:5683
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)
Definition miniz.c:2684
static MZ_FORCEINLINE mz_bool mz_zip_string_equal(const char *pA, const char *pB, mz_uint len, mz_uint flags)
Definition miniz.c:4006
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)
Definition miniz.c:2229
#define MZ_SWAP_UINT32(a, b)
Definition miniz.c:3265
tdefl_compressor * tdefl_compressor_alloc(void)
Definition miniz.c:2049
static void tdefl_optimize_huffman_table(tdefl_compressor *d, int table_num, int table_len, int code_size_limit, int static_table)
Definition miniz.c:824
static const mz_uint8 s_tdefl_large_dist_sym[128]
Definition miniz.c:704
#define TDEFL_PROBE
#define MZ_FCLOSE
Definition miniz.c:2924
mz_bool mz_zip_validate_mem_archive(const void *pMem, size_t size, mz_uint flags, mz_zip_error *pErr)
Definition miniz.c:5100
mz_bool mz_zip_validate_file(mz_zip_archive *pZip, mz_uint file_index, mz_uint flags)
Definition miniz.c:4862
void * tdefl_write_image_to_png_file_in_memory(const void *pImage, int w, int h, int num_chans, size_t *pLen_out)
Definition miniz.c:2039
mz_ulong mz_deflateBound(mz_streamp pStream, mz_ulong source_len)
Definition miniz.c:298
mz_uint tdefl_create_comp_flags_from_zip_params(int level, int window_bits, int strategy)
Definition miniz.c:1937
static mz_bool tdefl_compress_block(tdefl_compressor *d, mz_bool static_block)
Definition miniz.c:1155
static size_t mz_zip_compute_crc32_callback(void *pOpaque, mz_uint64 file_ofs, const void *pBuf, size_t n)
Definition miniz.c:4854
static MZ_FORCEINLINE void tdefl_record_match(tdefl_compressor *d, mz_uint match_len, mz_uint match_dist)
Definition miniz.c:1585
#define MZ_ZIP_ARRAY_ELEMENT(array_ptr, element_type, index)
Definition miniz.c:3073
mz_bool mz_zip_reader_extract_iter_free(mz_zip_reader_extract_iter_state *pState)
Definition miniz.c:4743
mz_bool mz_zip_reader_extract_to_cfile(mz_zip_archive *pZip, mz_uint file_index, MZ_FILE *pFile, mz_uint flags)
Definition miniz.c:4831
mz_bool mz_zip_reader_init_cfile(mz_zip_archive *pZip, MZ_FILE *pFile, mz_uint64 archive_size, mz_uint flags)
Definition miniz.c:3772
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)
Definition miniz.c:5587
mz_bool mz_zip_writer_finalize_heap_archive(mz_zip_archive *pZip, void **ppBuf, size_t *pSize)
Definition miniz.c:6803
unsigned char mz_validate_uint16[sizeof(mz_uint16)==2 ? 1 :-1]
Definition miniz.c:29
static MZ_FORCEINLINE void mz_write_le64(mz_uint8 *p, mz_uint64 v)
Definition miniz.c:5192
@ MZ_ZIP64_ECDH_VERSION_MADE_BY_OFS
Definition miniz.c:3016
@ MZ_ZIP_CDH_FILENAME_LEN_OFS
Definition miniz.c:2975
@ MZ_ZIP_LDH_BIT_FLAG_HAS_LOCATOR
Definition miniz.c:2995
@ MZ_ZIP_LOCAL_DIR_HEADER_SIG
Definition miniz.c:2949
@ MZ_ZIP_CDH_FILE_DATE_OFS
Definition miniz.c:2971
@ MZ_ZIP_LDH_CRC32_OFS
Definition miniz.c:2990
@ MZ_ZIP64_ECDH_SIG_OFS
Definition miniz.c:3014
@ MZ_ZIP_LDH_BIT_FLAG_OFS
Definition miniz.c:2986
@ MZ_ZIP_ECDH_NUM_DISK_CDIR_OFS
Definition miniz.c:3000
@ MZ_ZIP_CDH_VERSION_NEEDED_OFS
Definition miniz.c:2967
@ MZ_ZIP_DATA_DESCRIPTER_SIZE64
Definition miniz.c:2961
@ MZ_ZIP_CDH_LOCAL_HEADER_OFS
Definition miniz.c:2981
@ MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIG
Definition miniz.c:2955
@ MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_UTF8
Definition miniz.c:3030
@ MZ_ZIP64_ECDH_NUM_DISK_CDIR_OFS
Definition miniz.c:3019
@ MZ_ZIP_CDH_COMPRESSED_SIZE_OFS
Definition miniz.c:2973
@ MZ_ZIP_DATA_DESCRIPTER_SIZE32
Definition miniz.c:2962
@ MZ_ZIP_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS
Definition miniz.c:3001
@ MZ_ZIP_CDH_SIG_OFS
Definition miniz.c:2965
@ MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_COMPRESSED_PATCH_FLAG
Definition miniz.c:3027
@ MZ_ZIP64_ECDL_NUM_DISK_CDIR_OFS
Definition miniz.c:3009
@ MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS
Definition miniz.c:2974
@ MZ_ZIP_ECDH_CDIR_SIZE_OFS
Definition miniz.c:3003
@ MZ_ZIP64_ECDL_SIG_OFS
Definition miniz.c:3008
@ MZ_ZIP_CDH_METHOD_OFS
Definition miniz.c:2969
@ MZ_ZIP_LDH_FILENAME_LEN_OFS
Definition miniz.c:2993
@ MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE
Definition miniz.c:2958
@ MZ_ZIP_DOS_DIR_ATTRIBUTE_BITFLAG
Definition miniz.c:3025
@ MZ_ZIP_LDH_FILE_TIME_OFS
Definition miniz.c:2988
@ MZ_ZIP64_ECDH_VERSION_NEEDED_OFS
Definition miniz.c:3017
@ MZ_ZIP_CDH_EXTRA_LEN_OFS
Definition miniz.c:2976
@ MZ_ZIP_LDH_FILE_DATE_OFS
Definition miniz.c:2989
@ MZ_ZIP64_ECDL_REL_OFS_TO_ZIP64_ECDR_OFS
Definition miniz.c:3010
@ MZ_ZIP_LDH_METHOD_OFS
Definition miniz.c:2987
@ MZ_ZIP64_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS
Definition miniz.c:3020
@ MZ_ZIP_LDH_COMPRESSED_SIZE_OFS
Definition miniz.c:2991
@ MZ_ZIP_CDH_FILE_TIME_OFS
Definition miniz.c:2970
@ MZ_ZIP_VERSION_MADE_BY_DOS_FILESYSTEM_ID
Definition miniz.c:3024
@ MZ_ZIP_ECDH_CDIR_TOTAL_ENTRIES_OFS
Definition miniz.c:3002
@ MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID
Definition miniz.c:2959
@ MZ_ZIP_CENTRAL_DIR_HEADER_SIG
Definition miniz.c:2948
@ MZ_ZIP_ECDH_SIG_OFS
Definition miniz.c:2998
@ MZ_ZIP_ECDH_COMMENT_SIZE_OFS
Definition miniz.c:3005
@ MZ_ZIP64_ECDH_CDIR_SIZE_OFS
Definition miniz.c:3022
@ MZ_ZIP64_ECDL_TOTAL_NUMBER_OF_DISKS_OFS
Definition miniz.c:3011
@ MZ_ZIP_LDH_SIG_OFS
Definition miniz.c:2984
@ MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_USES_STRONG_ENCRYPTION
Definition miniz.c:3028
@ MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIG
Definition miniz.c:2947
@ MZ_ZIP_CDH_CRC32_OFS
Definition miniz.c:2972
@ MZ_ZIP_CDH_DISK_START_OFS
Definition miniz.c:2978
@ MZ_ZIP64_ECDH_CDIR_TOTAL_ENTRIES_OFS
Definition miniz.c:3021
@ MZ_ZIP_CDH_INTERNAL_ATTR_OFS
Definition miniz.c:2979
@ MZ_ZIP_LDH_VERSION_NEEDED_OFS
Definition miniz.c:2985
@ MZ_ZIP_ECDH_CDIR_OFS_OFS
Definition miniz.c:3004
@ MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIG
Definition miniz.c:2956
@ MZ_ZIP_LDH_EXTRA_LEN_OFS
Definition miniz.c:2994
@ MZ_ZIP_CDH_EXTERNAL_ATTR_OFS
Definition miniz.c:2980
@ MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_LOCAL_DIR_IS_MASKED
Definition miniz.c:3029
@ MZ_ZIP64_ECDH_NUM_THIS_DISK_OFS
Definition miniz.c:3018
@ MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE
Definition miniz.c:2952
@ MZ_ZIP_LOCAL_DIR_HEADER_SIZE
Definition miniz.c:2950
@ MZ_ZIP64_ECDH_CDIR_OFS_OFS
Definition miniz.c:3023
@ MZ_ZIP_LDH_DECOMPRESSED_SIZE_OFS
Definition miniz.c:2992
@ MZ_ZIP_CDH_BIT_FLAG_OFS
Definition miniz.c:2968
@ MZ_ZIP_CDH_COMMENT_LEN_OFS
Definition miniz.c:2977
@ MZ_ZIP_CENTRAL_DIR_HEADER_SIZE
Definition miniz.c:2951
@ MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE
Definition miniz.c:2957
@ MZ_ZIP_DATA_DESCRIPTOR_ID
Definition miniz.c:2960
@ MZ_ZIP_CDH_VERSION_MADE_BY_OFS
Definition miniz.c:2966
@ MZ_ZIP_ECDH_NUM_THIS_DISK_OFS
Definition miniz.c:2999
@ MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_IS_ENCRYPTED
Definition miniz.c:3026
@ MZ_ZIP64_ECDH_SIZE_OF_RECORD_OFS
Definition miniz.c:3015
mz_bool mz_zip_reader_extract_file_to_cfile(mz_zip_archive *pZip, const char *pArchive_filename, MZ_FILE *pFile, mz_uint flags)
Definition miniz.c:4844
#define MZ_WRITE_LE64(p, v)
Definition miniz.c:5200
mz_bool mz_zip_writer_init_v2(mz_zip_archive *pZip, mz_uint64 existing_size, mz_uint flags)
Definition miniz.c:5278
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)
Definition miniz.c:1750
mz_bool mz_zip_writer_init_from_reader(mz_zip_archive *pZip, const char *pFilename)
Definition miniz.c:5524
static mz_bool mz_zip_array_ensure_capacity(mz_zip_archive *pZip, mz_zip_array *pArray, size_t min_new_capacity, mz_uint growing)
Definition miniz.c:3088
mz_zip_error mz_zip_clear_last_error(mz_zip_archive *pZip)
Definition miniz.c:6999
#define TINFL_SKIP_BITS(state_index, n)
Definition miniz.c:2142
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, mz_uint level_and_flags, mz_uint64 uncomp_size, mz_uint32 uncomp_crc32, MZ_TIME_T *last_modified, const char *user_extra_data, mz_uint user_extra_data_len, const char *user_extra_data_central, mz_uint user_extra_data_central_len)
Definition miniz.c:5704
static MZ_FORCEINLINE mz_bool mz_zip_array_resize(mz_zip_archive *pZip, mz_zip_array *pArray, size_t new_size, mz_uint growing)
Definition miniz.c:3116
#define TDEFL_RLE_PREV_CODE_SIZE()
Definition miniz.c:888
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)
Definition miniz.c:5330
mz_bool mz_zip_reader_extract_to_file(mz_zip_archive *pZip, mz_uint file_index, const char *pDst_filename, mz_uint flags)
Definition miniz.c:4789
#define MZ_FTELL64
Definition miniz.c:2931
mz_uint mz_zip_reader_get_filename(mz_zip_archive *pZip, mz_uint file_index, char *pFilename, mz_uint filename_buf_size)
Definition miniz.c:7144
static tdefl_status tdefl_flush_output_buffer(tdefl_compressor *d)
Definition miniz.c:1731
void tinfl_decompressor_free(tinfl_decompressor *pDecomp)
Definition miniz.c:2730
#define TDEFL_PUT_BITS(b, l)
Definition miniz.c:872
#define MZ_DELETE_FILE
Definition miniz.c:2938
static size_t mz_file_read_func_stdio(void *pOpaque, mz_uint64 file_ofs, void *pBuf, size_t n)
Definition miniz.c:6274
static tdefl_sym_freq * tdefl_radix_sort_syms(mz_uint num_syms, tdefl_sym_freq *pSyms0, tdefl_sym_freq *pSyms1)
Definition miniz.c:721
unsigned long mz_ulong
Definition miniz.h:233
struct mz_stream_s mz_stream
#define MZ_DEFLATED
Definition miniz.h:256
#define MZ_ADLER32_INIT
Definition miniz.h:238
#define MZ_CRC32_INIT
Definition miniz.h:242
@ MZ_FILTERED
Definition miniz.h:249
@ MZ_FIXED
Definition miniz.h:252
@ MZ_DEFAULT_STRATEGY
Definition miniz.h:248
@ MZ_RLE
Definition miniz.h:251
@ MZ_HUFFMAN_ONLY
Definition miniz.h:250
#define MZ_DEFAULT_WINDOW_BITS
Definition miniz.h:308
@ MZ_MEM_ERROR
Definition miniz.h:301
@ MZ_PARAM_ERROR
Definition miniz.h:304
@ MZ_NEED_DICT
Definition miniz.h:297
@ MZ_VERSION_ERROR
Definition miniz.h:303
@ MZ_STREAM_END
Definition miniz.h:296
@ MZ_ERRNO
Definition miniz.h:298
@ MZ_OK
Definition miniz.h:295
@ MZ_BUF_ERROR
Definition miniz.h:302
@ MZ_STREAM_ERROR
Definition miniz.h:299
@ MZ_DATA_ERROR
Definition miniz.h:300
@ MZ_UBER_COMPRESSION
Definition miniz.h:269
@ MZ_DEFAULT_LEVEL
Definition miniz.h:270
@ MZ_DEFAULT_COMPRESSION
Definition miniz.h:271
#define MZ_VERSION
Definition miniz.h:274
#define crc32
Definition miniz.h:504
@ MZ_SYNC_FLUSH
Definition miniz.h:287
@ MZ_FINISH
Definition miniz.h:289
@ MZ_PARTIAL_FLUSH
Definition miniz.h:286
mz_stream * mz_streamp
Definition miniz.h:334
int m_window_bits
Definition miniz.c:352
mz_uint m_dict_ofs
Definition miniz.c:351
mz_uint m_has_flushed
Definition miniz.c:351
mz_uint m_dict_avail
Definition miniz.c:351
mz_uint8 m_dict[TINFL_LZ_DICT_SIZE]
Definition miniz.c:353
mz_uint m_first_call
Definition miniz.c:351
tinfl_status m_last_status
Definition miniz.c:354
tinfl_decompressor m_decomp
Definition miniz.c:350
mz_ulong adler
Definition miniz.h:330
unsigned char * next_out
Definition miniz.h:318
void * opaque
Definition miniz.h:327
int data_type
Definition miniz.h:329
mz_free_func zfree
Definition miniz.h:326
mz_ulong total_out
Definition miniz.h:320
unsigned int avail_out
Definition miniz.h:319
struct mz_internal_state * state
Definition miniz.h:323
const unsigned char * next_in
Definition miniz.h:314
unsigned int avail_in
Definition miniz.h:315
mz_alloc_func zalloc
Definition miniz.h:325
mz_ulong total_in
Definition miniz.h:316
char * msg
Definition miniz.h:322
mz_ulong reserved
Definition miniz.h:331
size_t m_size
Definition miniz.c:3036
void * m_p
Definition miniz.c:3035
size_t m_capacity
Definition miniz.c:3036
mz_uint m_element_size
Definition miniz.c:3037
mz_bool m_zip64_has_extended_info_fields
Definition miniz.c:3052
mz_zip_array m_sorted_central_dir_offsets
Definition miniz.c:3043
mz_uint64 m_file_archive_start_ofs
Definition miniz.c:3056
mz_zip_array m_central_dir_offsets
Definition miniz.c:3042
mz_zip_array m_central_dir
Definition miniz.c:3041
mz_uint64 m_cur_archive_file_ofs
Definition miniz.c:5538
mz_uint64 m_comp_size
Definition miniz.c:5539
mz_zip_archive * m_pZip
Definition miniz.c:5537
mz_bool m_expandable
Definition miniz.c:1880
mz_uint8 * m_pBuf
Definition miniz.c:1879
mz_uint16 m_sym_index
Definition miniz.c:719
mz_uint16 m_key
Definition miniz.c:719