1#define MINGW_HAS_SECURE_API 1
41 mz_uint32 i, s1 = (mz_uint32)(adler & 0xffff), s2 = (mz_uint32)(adler >> 16);
42 size_t block_len = buf_len % 5552;
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;
56 for (; i < block_len; ++i)
57 s1 += *ptr++, s2 += s1;
58 s1 %= 65521U, s2 %= 65521U;
62 return (s2 << 16) + s1;
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;
78 crcu32 = (crcu32 >> 4) ^ s_crc32[(crcu32 & 0xF) ^ (b & 0xF)];
79 crcu32 = (crcu32 >> 4) ^ s_crc32[(crcu32 & 0xF) ^ (b >> 4)];
83#elif defined(USE_EXTERNAL_MZCRC)
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
134 mz_uint32
crc32 = (mz_uint32)crc ^ 0xFFFFFFFF;
135 const mz_uint8* pByte_buf = (
const mz_uint8*)ptr;
137 while (buf_len >= 4) {
163 (void)opaque, (
void)items, (void)
size;
164 return MZ_MALLOC(items *
size);
168 (void)opaque, (
void)address;
173 (void)opaque, (
void)address, (void)items, (
void)
size;
174 return MZ_REALLOC(address, items *
size);
182#ifndef MINIZ_NO_ZLIB_APIS
184#ifndef MINIZ_NO_DEFLATE_APIS
193 tdefl_compressor* pComp;
212 pComp = (tdefl_compressor*)pStream->
zalloc(pStream->
opaque, 1,
sizeof(tdefl_compressor));
216 pStream->
state = (
struct mz_internal_state*)pComp;
218 if (
tdefl_init(pComp, NULL, NULL, comp_flags) != TDEFL_STATUS_OKAY) {
228 if ((!pStream) || (!pStream->
state) || (!pStream->
zalloc) || (!pStream->
zfree))
231 tdefl_init((tdefl_compressor*)pStream->
state, NULL, NULL, ((tdefl_compressor*)pStream->
state)->m_flags);
237 size_t in_bytes, out_bytes;
238 mz_ulong orig_total_in, orig_total_out;
239 int mz_status =
MZ_OK;
249 if (((tdefl_compressor*)pStream->
state)->m_prev_return_status == TDEFL_STATUS_DONE)
255 tdefl_status defl_status;
260 pStream->
next_in += (mz_uint)in_bytes;
261 pStream->
avail_in -= (mz_uint)in_bytes;
262 pStream->
total_in += (mz_uint)in_bytes;
265 pStream->
next_out += (mz_uint)out_bytes;
266 pStream->
avail_out -= (mz_uint)out_bytes;
267 pStream->
total_out += (mz_uint)out_bytes;
269 if (defl_status < 0) {
272 }
else if (defl_status == TDEFL_STATUS_DONE) {
278 if ((flush) || (pStream->
total_in != orig_total_in) || (pStream->
total_out != orig_total_out))
291 if (pStream->
state) {
293 pStream->
state = NULL;
302 return MZ_MAX(128 + (source_len * 110) / 100, 128 + source_len + ((source_len / (31 * 1024)) + 1) * 5);
312 if ((mz_uint64)(source_len | *pDest_len) > 0xFFFFFFFFU)
316 stream.avail_in = (mz_uint32)source_len;
318 stream.avail_out = (mz_uint32)*pDest_len;
330 *pDest_len =
stream.total_out;
346#ifndef MINIZ_NO_INFLATE_APIS
380 pStream->
state = (
struct mz_internal_state*)pDecomp;
427 mz_uint n, first_call, decomp_flags = TINFL_FLAG_COMPUTE_ADLER32;
428 size_t in_bytes, out_bytes, orig_avail_in;
431 if ((!pStream) || (!pStream->
state))
440 decomp_flags |= TINFL_FLAG_PARSE_ZLIB_HEADER;
452 if ((flush ==
MZ_FINISH) && (first_call)) {
454 decomp_flags |= TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF;
459 pStream->
next_in += (mz_uint)in_bytes;
460 pStream->
avail_in -= (mz_uint)in_bytes;
461 pStream->
total_in += (mz_uint)in_bytes;
463 pStream->
next_out += (mz_uint)out_bytes;
464 pStream->
avail_out -= (mz_uint)out_bytes;
465 pStream->
total_out += (mz_uint)out_bytes;
469 else if (status != TINFL_STATUS_DONE) {
477 decomp_flags |= TINFL_FLAG_HAS_MORE_INPUT;
492 out_bytes = TINFL_LZ_DICT_SIZE - pState->
m_dict_ofs;
497 pStream->
next_in += (mz_uint)in_bytes;
498 pStream->
avail_in -= (mz_uint)in_bytes;
499 pStream->
total_in += (mz_uint)in_bytes;
514 else if ((status == TINFL_STATUS_NEEDS_MORE_INPUT) && (!orig_avail_in))
518 if (status == TINFL_STATUS_DONE)
534 if (pStream->
state) {
536 pStream->
state = NULL;
547 if ((mz_uint64)(*pSource_len | *pDest_len) > 0xFFFFFFFFU)
551 stream.avail_in = (mz_uint32)*pSource_len;
553 stream.avail_out = (mz_uint32)*pDest_len;
560 *pSource_len = *pSource_len -
stream.avail_in;
565 *pDest_len =
stream.total_out;
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" }
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;
651#ifndef MINIZ_NO_DEFLATE_APIS
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
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
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
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
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
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
723 mz_uint32 total_passes = 2, pass_shift, pass, i, hist[256 * 2];
726 for (i = 0; i < num_syms; i++) {
727 mz_uint freq = pSyms0[i].
m_key;
729 hist[256 + ((freq >> 8) & 0xFF)]++;
731 while ((total_passes > 1) && (num_syms == hist[(total_passes - 1) * 256]))
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;
740 for (i = 0; i < num_syms; i++)
741 pNew_syms[offsets[(pCur_syms[i].m_key >> pass_shift) & 0xFF]++] = pCur_syms[i];
744 pCur_syms = pNew_syms;
754 int root, leaf, next, avbl, used, dpth;
764 for (next = 1; next < n - 1; next++) {
765 if (leaf >= n || A[root].m_key < A[leaf].m_key) {
767 A[root++].
m_key = (mz_uint16)next;
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;
774 A[next].
m_key = (mz_uint16)(A[next].m_key + A[leaf++].m_key);
777 for (next = n - 3; next >= 0; next--)
778 A[next].m_key = A[A[next].m_key].m_key + 1;
784 while (root >= 0 && (
int)A[root].m_key == dpth) {
788 while (avbl > used) {
789 A[next--].
m_key = (mz_uint16)(dpth);
806 if (code_list_len <= 1)
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--)
817 pNum_codes[i + 1] += 2;
828 MZ_CLEAR_ARR(num_codes);
830 for (i = 0; i < table_len; i++)
831 num_codes[d->m_huff_code_sizes[table_num][i]]++;
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++)
838 syms0[num_used_syms].
m_key = (mz_uint16)pSym_count[i];
845 for (i = 0; i < num_used_syms; i++)
846 num_codes[pSyms[i].m_key]++;
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);
858 for (j = 0, i = 2; i <= code_size_limit; i++)
859 next_code[i] = j = ((j + num_codes[i - 1]) << 1);
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)
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;
872#define TDEFL_PUT_BITS(b, 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; \
888#define TDEFL_RLE_PREV_CODE_SIZE() \
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; \
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); \
900 rle_repeat_count = 0; \
904#define TDEFL_RLE_ZERO_CODE_SIZE() \
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); \
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); \
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 };
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;
932 d->m_huff_count[0][256] = 1;
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])
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])
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;
949 rle_repeat_count = 0;
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];
956 if (++rle_z_count == 138) {
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) {
969 prev_code_size = code_size;
971 if (rle_repeat_count) {
984 for (num_bit_lengths = 18; num_bit_lengths >= 0; num_bit_lengths--)
987 num_bit_lengths = MZ_MAX(4, (num_bit_lengths + 1));
989 for (i = 0; (
int)i < num_bit_lengths; i++)
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]);
997 TDEFL_PUT_BITS(packed_code_sizes[packed_code_sizes_index++],
"\02\03\07"[code - 16]);
1004 mz_uint8* p = &d->m_huff_code_sizes[0][0];
1006 for (i = 0; i <= 143; ++i)
1008 for (; i <= 255; ++i)
1010 for (; i <= 279; ++i)
1012 for (; i <= 287; ++i)
1015 memset(d->m_huff_code_sizes[1], 5, 32);
1023static const mz_uint
mz_bitmasks[17] = { 0x0000, 0x0001, 0x0003, 0x0007, 0x000F, 0x001F, 0x003F, 0x007F, 0x00FF, 0x01FF, 0x03FF, 0x07FF, 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF };
1025#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN && MINIZ_HAS_64BIT_REGISTERS
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;
1035#define TDEFL_PUT_BITS_FAST(b, l) \
1037 bit_buffer |= (((mz_uint64)(b)) << bits_in); \
1042 for (pLZ_codes = d->m_lz_code_buf; pLZ_codes < pLZ_code_buf_end; flags >>= 1) {
1044 flags = *pLZ_codes++ | 0x100;
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));
1061 sym = (match_dist < 512) ? s0 : s1;
1062 num_extra_bits = (match_dist < 512) ? n0 : n1;
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);
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]);
1072 if (((flags & 2) == 0) && (pLZ_codes < pLZ_code_buf_end)) {
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]);
1078 if (((flags & 2) == 0) && (pLZ_codes < pLZ_code_buf_end)) {
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]);
1087 if (pOutput_buf >= d->m_pOutput_buf_end)
1090 memcpy(pOutput_buf, &bit_buffer,
sizeof(mz_uint64));
1091 pOutput_buf += (bits_in >> 3);
1092 bit_buffer >>= (bits_in & ~7);
1096#undef TDEFL_PUT_BITS_FAST
1098 d->m_pOutput_buf = pOutput_buf;
1100 d->m_bit_buffer = 0;
1103 mz_uint32 n = MZ_MIN(bits_in, 16);
1109 TDEFL_PUT_BITS(d->m_huff_codes[0][256], d->m_huff_code_sizes[0][256]);
1111 return (d->m_pOutput_buf < d->m_pOutput_buf_end);
1117 mz_uint8* pLZ_codes;
1120 for (pLZ_codes = d->m_lz_code_buf; pLZ_codes < d->m_pLZ_code_buf; flags >>= 1) {
1122 flags = *pLZ_codes++ | 0x100;
1124 mz_uint sym, num_extra_bits;
1125 mz_uint match_len = pLZ_codes[0], match_dist = (pLZ_codes[1] | (pLZ_codes[2] << 8));
1132 if (match_dist < 512) {
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]);
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]);
1149 TDEFL_PUT_BITS(d->m_huff_codes[0][256], d->m_huff_code_sizes[0][256]);
1151 return (d->m_pOutput_buf < d->m_pOutput_buf_end);
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;
1174 d->m_pOutput_buf = pOutput_buf_start;
1175 d->m_pOutput_buf_end = d->m_pOutput_buf + TDEFL_OUT_BUF_SIZE - 16;
1177 MZ_ASSERT(!d->m_output_flush_remaining);
1178 d->m_output_flush_ofs = 0;
1179 d->m_output_flush_remaining = 0;
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);
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;
1190 for (i = 0; i < mz_un; i++)
1201 header = cmf << 8 | (flevel << 6);
1202 header += 31 - (header % 31);
1203 flg = header & 0xFF;
1211 pSaved_output_buf = d->m_pOutput_buf;
1212 saved_bit_buf = d->m_bit_buffer;
1213 saved_bits_in = d->m_bits_in;
1216 comp_block_succeeded =
tdefl_compress_block(d, (d->m_flags & TDEFL_FORCE_ALL_STATIC_BLOCKS) || (d->m_total_lz_bytes < 48));
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)) {
1221 d->m_pOutput_buf = pSaved_output_buf;
1222 d->m_bit_buffer = saved_bit_buf, d->m_bits_in = saved_bits_in;
1227 for (i = 2; i; --i, d->m_total_lz_bytes ^= 0xFFFF) {
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);
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;
1242 if (flush == TDEFL_FINISH) {
1246 if (d->m_flags & TDEFL_WRITE_ZLIB_HEADER) {
1247 mz_uint i, a = d->m_adler32;
1248 for (i = 0; i < 4; i++) {
1259 for (i = 2; i; --i, z ^= 0xFFFF) {
1265 MZ_ASSERT(d->m_pOutput_buf < d->m_pOutput_buf_end);
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);
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;
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;
1291 d->m_out_buf_ofs += n;
1295 return d->m_output_flush_remaining;
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)
1303 memcpy(&ret, p,
sizeof(mz_uint16));
1306static mz_uint16 TDEFL_READ_UNALIGNED_WORD2(
const mz_uint16* p)
1309 memcpy(&ret, p,
sizeof(mz_uint16));
1313#define TDEFL_READ_UNALIGNED_WORD(p) *(const mz_uint16*)(p)
1314#define TDEFL_READ_UNALIGNED_WORD2(p) *(const mz_uint16*)(p)
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)
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)
1327 if (--num_probes_left == 0)
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)) \
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) \
1342 q = (
const mz_uint16*)(d->m_dict + probe_pos);
1343 if (TDEFL_READ_UNALIGNED_WORD2(q) != s01)
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));
1350 *pMatch_dist = dist;
1351 *pMatch_len = MZ_MIN(max_match_len, (mz_uint)TDEFL_MAX_MATCH_LEN);
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)
1357 c01 = TDEFL_READ_UNALIGNED_WORD(&d->m_dict[pos + match_len - 1]);
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)
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)
1373 if (--num_probes_left == 0)
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)) \
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)) \
1389 q = d->m_dict + probe_pos;
1390 for (probe_len = 0; probe_len < max_match_len; probe_len++)
1393 if (probe_len > match_len) {
1394 *pMatch_dist = dist;
1395 if ((*pMatch_len = match_len = probe_len) == max_match_len)
1397 c0 = d->m_dict[pos + match_len];
1398 c1 = d->m_dict[pos + match_len - 1];
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)
1409 memcpy(&ret, p,
sizeof(mz_uint32));
1413#define TDEFL_READ_UNALIGNED_WORD32(p) *(const mz_uint32*)(p)
1415static mz_bool tdefl_compress_fast(tdefl_compressor* d)
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;
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;
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));
1435 dst_pos = (dst_pos + n) & TDEFL_LZ_DICT_SIZE_MASK;
1436 num_bytes_to_process -= n;
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))
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;
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;
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);
1459 cur_match_len = cur_match_dist ? TDEFL_MAX_MATCH_LEN : 0;
1461 if ((cur_match_len < TDEFL_MIN_MATCH_LEN) || ((cur_match_len == TDEFL_MIN_MATCH_LEN) && (cur_match_dist >= 8U * 1024U))) {
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]++;
1468 cur_match_len = MZ_MIN(cur_match_len, lookahead_size);
1470 MZ_ASSERT((cur_match_len >= TDEFL_MIN_MATCH_LEN) && (cur_match_dist >= 1) && (cur_match_dist <= TDEFL_LZ_DICT_SIZE));
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));
1478 *(mz_uint16*)(&pLZ_code_buf[1]) = (mz_uint16)cur_match_dist;
1481 *pLZ_flags = (mz_uint8)((*pLZ_flags >> 1) | 0x80);
1485 d->m_huff_count[1][(cur_match_dist < 512) ? s0 : s1]++;
1487 d->m_huff_count[0][
s_tdefl_len_sym[cur_match_len - TDEFL_MIN_MATCH_LEN]]++;
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]++;
1495 if (--num_flags_left == 0) {
1497 pLZ_flags = pLZ_code_buf++;
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;
1507 if (pLZ_code_buf > &d->m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE - 8]) {
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;
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;
1525 while (lookahead_size) {
1526 mz_uint8 lit = d->m_dict[cur_pos];
1529 *pLZ_code_buf++ = lit;
1530 *pLZ_flags = (mz_uint8)(*pLZ_flags >> 1);
1531 if (--num_flags_left == 0) {
1533 pLZ_flags = pLZ_code_buf++;
1536 d->m_huff_count[0][lit]++;
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;
1543 if (pLZ_code_buf > &d->m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE - 8]) {
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;
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;
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;
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++;
1582 d->m_huff_count[0][lit]++;
1589 MZ_ASSERT((match_len >= TDEFL_MIN_MATCH_LEN) && (match_dist >= 1) && (match_dist <= TDEFL_LZ_DICT_SIZE));
1591 d->m_total_lz_bytes += match_len;
1593 d->m_pLZ_code_buf[0] = (mz_uint8)(match_len - TDEFL_MIN_MATCH_LEN);
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;
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++;
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]]++;
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;
1618 while ((src_buf_left) || ((flush) && (d->m_lookahead_size))) {
1619 mz_uint len_to_move, cur_match_dist, cur_match_len, cur_pos;
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;
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;
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);
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))
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];
1668 while (cur_match_len < d->m_lookahead_size) {
1669 if (d->m_dict[cur_pos + cur_match_len] != c)
1673 if (cur_match_len < TDEFL_MIN_MATCH_LEN)
1679 tdefl_find_match(d, d->m_lookahead_pos, d->m_dict_size, d->m_lookahead_size, &cur_match_dist, &cur_match_len);
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;
1684 if (d->m_saved_match_len) {
1685 if (cur_match_len > d->m_saved_match_len) {
1687 if (cur_match_len >= 128) {
1689 d->m_saved_match_len = 0;
1690 len_to_move = cur_match_len;
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;
1698 len_to_move = d->m_saved_match_len - 1;
1699 d->m_saved_match_len = 0;
1701 }
else if (!cur_match_dist)
1703 else if ((d->m_greedy_parsing) || (d->m_flags & TDEFL_RLE_MATCHES) || (cur_match_len >= 128)) {
1705 len_to_move = cur_match_len;
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;
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);
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)))) {
1720 d->m_src_buf_left = src_buf_left;
1722 return (n < 0) ? MZ_FALSE : MZ_TRUE;
1727 d->m_src_buf_left = src_buf_left;
1733 if (d->m_pIn_buf_size) {
1734 *d->m_pIn_buf_size = d->m_pSrc - (
const mz_uint8*)d->m_pIn_buf;
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;
1744 *d->m_pOut_buf_size = d->m_out_buf_ofs;
1747 return (d->m_finished && !d->m_output_flush_remaining) ? TDEFL_STATUS_DONE : TDEFL_STATUS_OKAY;
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)
1757 return TDEFL_STATUS_BAD_PARAM;
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;
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)) {
1774 return (d->m_prev_return_status = TDEFL_STATUS_BAD_PARAM);
1776 d->m_wants_to_finish |= (flush == TDEFL_FINISH);
1778 if ((d->m_output_flush_remaining) || (d->m_finished))
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;
1789 return d->m_prev_return_status;
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);
1795 if ((flush) && (!d->m_lookahead_size) && (!d->m_src_buf_left) && (!d->m_output_flush_remaining)) {
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);
1811 MZ_ASSERT(d->m_pPut_buf_func);
1812 return tdefl_compress(d, pIn_buf, &in_buf_size, NULL, NULL, flush);
1815tdefl_status
tdefl_init(tdefl_compressor* d, tdefl_put_buf_func_ptr pPut_buf_func,
void* pPut_buf_user,
int flags)
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;
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;
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;
1853 return d->m_prev_return_status;
1858 return d->m_adler32;
1863 tdefl_compressor* pComp;
1865 if (((buf_len) && (!pBuf)) || (!pPut_buf_func))
1867 pComp = (tdefl_compressor*)MZ_MALLOC(
sizeof(tdefl_compressor));
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);
1886 size_t new_size = p->
m_size + len;
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);
1909 MZ_CLEAR_OBJ(out_buf);
1917 *pOut_len = out_buf.
m_size;
1924 MZ_CLEAR_OBJ(out_buf);
1927 out_buf.
m_pBuf = (mz_uint8*)pOut_buf;
1934static const mz_uint
s_tdefl_num_probes[11] = { 0, 1, 6, 32, 16, 32, 128, 256, 512, 768, 1500 };
1940 if (window_bits > 0)
1941 comp_flags |= TDEFL_WRITE_ZLIB_HEADER;
1944 comp_flags |= TDEFL_FORCE_ALL_RAW_BLOCKS;
1946 comp_flags |= TDEFL_FILTER_MATCHES;
1948 comp_flags &= ~TDEFL_MAX_PROBES_MASK;
1950 comp_flags |= TDEFL_FORCE_ALL_STATIC_BLOCKS;
1951 else if (strategy ==
MZ_RLE)
1952 comp_flags |= TDEFL_RLE_MATCHES;
1958#pragma warning(push)
1959#pragma warning(disable : 4204)
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));
1971 int i, bpl = w * num_chans,
y, z;
1976 MZ_CLEAR_OBJ(out_buf);
1978 out_buf.
m_capacity = 57 + MZ_MAX(64, (1 + bpl) * h);
1984 for (z = 41; z; --z)
1988 for (
y = 0;
y < h; ++
y) {
1998 *pLen_out = out_buf.
m_size - 41;
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,
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;
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);
2032 for (i = 0; i < 4; ++i, c <<= 8)
2033 (out_buf.
m_pBuf + out_buf.
m_size - 16)[i] = (mz_uint8)(c >> 24);
2045#ifndef MINIZ_NO_MALLOC
2051 return (tdefl_compressor*)MZ_MALLOC(
sizeof(tdefl_compressor));
2095#ifndef MINIZ_NO_INFLATE_APIS
2103#define TINFL_MEMCPY(d, s, l) memcpy(d, s, l)
2104#define TINFL_MEMSET(p, c, l) memset(p, c, l)
2106#define TINFL_CR_BEGIN \
2107 switch (r->m_state) { \
2109#define TINFL_CR_RETURN(state_index, result) \
2112 r->m_state = state_index; \
2114 case state_index:; \
2117#define TINFL_CR_RETURN_FOREVER(state_index, result) \
2120 TINFL_CR_RETURN(state_index, result); \
2124#define TINFL_CR_FINISH }
2126#define TINFL_GET_BYTE(state_index, c) \
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); \
2131 c = *pIn_buf_cur++; \
2135#define TINFL_NEED_BITS(state_index, n) \
2138 TINFL_GET_BYTE(state_index, c); \
2139 bit_buf |= (((tinfl_bit_buf_t)c) << num_bits); \
2141 } while (num_bits < (mz_uint)(n))
2142#define TINFL_SKIP_BITS(state_index, n) \
2144 if (num_bits < (mz_uint)(n)) { \
2145 TINFL_NEED_BITS(state_index, n); \
2151#define TINFL_GET_BITS(state_index, b, n) \
2153 if (num_bits < (mz_uint)(n)) { \
2154 TINFL_NEED_BITS(state_index, n); \
2156 b = bit_buf & ((1 << (n)) - 1); \
2166#define TINFL_HUFF_BITBUF_FILL(state_index, pLookUp, pTree) \
2168 temp = pLookUp[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]; \
2170 code_len = temp >> 9; \
2171 if ((code_len) && (num_bits >= code_len)) \
2173 } else if (num_bits > TINFL_FAST_LOOKUP_BITS) { \
2174 code_len = TINFL_FAST_LOOKUP_BITS; \
2176 temp = pTree[~temp + ((bit_buf >> code_len++) & 1)]; \
2177 } while ((temp < 0) && (num_bits >= (code_len + 1))); \
2181 TINFL_GET_BYTE(state_index, c); \
2182 bit_buf |= (((tinfl_bit_buf_t)c) << num_bits); \
2184 } while (num_bits < 15);
2192#define TINFL_HUFF_DECODE(state_index, sym, pLookUp, pTree) \
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); \
2200 bit_buf |= (((tinfl_bit_buf_t)pIn_buf_cur[0]) << num_bits) | (((tinfl_bit_buf_t)pIn_buf_cur[1]) << (num_bits + 8)); \
2205 if ((temp = pLookUp[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0) \
2206 code_len = temp >> 9, temp &= 511; \
2208 code_len = TINFL_FAST_LOOKUP_BITS; \
2210 temp = pTree[~temp + ((bit_buf >> code_len++) & 1)]; \
2211 } while (temp < 0); \
2214 bit_buf >>= code_len; \
2215 num_bits -= code_len; \
2222 MZ_CLEAR_ARR(r->m_tree_0);
2223 else if (r->m_type == 1)
2224 MZ_CLEAR_ARR(r->m_tree_1);
2226 MZ_CLEAR_ARR(r->m_tree_2);
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)
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 };
2238 mz_int16* pTrees[3];
2239 mz_uint8* pCode_sizes[3];
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;
2249 if ((!pOut_buf_start) || (!pOut_buf_next) || (!pIn_buf_size) || (!pOut_buf_size)) {
2250 return TINFL_STATUS_BAD_PARAM;
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;
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;
2264 num_bits = r->m_num_bits;
2265 bit_buf = r->m_bit_buf;
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;
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) {
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)))));
2287 r->m_type = r->m_final >> 1;
2288 if (r->m_type == 0) {
2290 for (counter = 0; counter < 4; ++counter) {
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)))) {
2299 while ((counter) && (num_bits)) {
2301 while (pOut_buf_cur >= pOut_buf_end) {
2304 *pOut_buf_cur++ = (mz_uint8)dist;
2309 while (pOut_buf_cur >= pOut_buf_end) {
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);
2315 n = MZ_MIN(MZ_MIN((
size_t)(pOut_buf_end - pOut_buf_cur), (
size_t)(pIn_buf_end - pIn_buf_cur)), counter);
2319 counter -= (mz_uint)n;
2321 }
else if (r->m_type == 3) {
2324 if (r->m_type == 1) {
2325 mz_uint8* p = r->m_code_size_0;
2327 r->m_table_sizes[0] = 288;
2328 r->m_table_sizes[1] = 32;
2330 for (i = 0; i <= 143; ++i)
2332 for (; i <= 255; ++i)
2334 for (; i <= 279; ++i)
2336 for (; i <= 287; ++i)
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];
2343 MZ_CLEAR_ARR(r->m_code_size_2);
2344 for (counter = 0; counter < r->m_table_sizes[2]; counter++) {
2347 r->m_code_size_2[s_length_dezigzag[counter]] = (mz_uint8)s;
2349 r->m_table_sizes[2] = 19;
2351 for (; (
int)r->m_type >= 0; r->m_type--) {
2352 int tree_next, tree_cur;
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);
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));
2371 if ((65536 != total) && (used_syms > 1)) {
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];
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);
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;
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;
2402 tree_cur = pTree[-tree_cur - 1];
2404 tree_cur -= ((rev_code >>= 1) & 1);
2405 pTree[-tree_cur - 1] = (mz_int16)sym_index;
2407 if (r->m_type == 2) {
2408 for (counter = 0; counter < (r->m_table_sizes[0] + r->m_table_sizes[1]);) {
2412 r->m_len_codes[counter++] = (mz_uint8)dist;
2415 if ((dist == 16) && (!counter)) {
2418 num_extra =
"\02\03\07"[dist - 16];
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);
2424 if ((r->m_table_sizes[0] + r->m_table_sizes[1]) != counter) {
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]);
2434 if (((pIn_buf_end - pIn_buf_cur) < 4) || ((pOut_buf_end - pOut_buf_cur) < 2)) {
2438 while (pOut_buf_cur >= pOut_buf_end) {
2441 *pOut_buf_cur++ = (mz_uint8)counter;
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);
2452 if (num_bits < 15) {
2453 bit_buf |= (((tinfl_bit_buf_t)MZ_READ_LE16(pIn_buf_cur)) << num_bits);
2458 if ((sym2 = r->m_look_up[0][bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0)
2459 code_len = sym2 >> 9;
2461 code_len = TINFL_FAST_LOOKUP_BITS;
2463 sym2 = r->m_tree_0[~sym2 + ((bit_buf >> code_len++) & 1)];
2467 bit_buf >>= code_len;
2468 num_bits -= code_len;
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);
2479 if ((sym2 = r->m_look_up[0][bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0)
2480 code_len = sym2 >> 9;
2482 code_len = TINFL_FAST_LOOKUP_BITS;
2484 sym2 = r->m_tree_0[~sym2 + ((bit_buf >> code_len++) & 1)];
2487 bit_buf >>= code_len;
2488 num_bits -= code_len;
2490 pOut_buf_cur[0] = (mz_uint8)counter;
2496 pOut_buf_cur[1] = (mz_uint8)sym2;
2500 if ((counter &= 511) == 256)
2503 num_extra = s_length_extra[counter - 257];
2504 counter = s_length_base[counter - 257];
2508 counter += extra_bits;
2512 num_extra = s_dist_extra[dist];
2513 dist = s_dist_base[dist];
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)) {
2525 pSrc = pOut_buf_start + ((dist_from_out_buf_start - dist) & out_buf_size_mask);
2527 if ((MZ_MAX(pOut_buf_cur, pSrc) + counter) > pOut_buf_end) {
2529 while (pOut_buf_cur >= pOut_buf_end) {
2532 *pOut_buf_cur++ = pOut_buf_start[(dist_from_out_buf_start++ - dist) & out_buf_size_mask];
2536#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES
2537 else if ((counter >= 9) && (counter <= dist)) {
2538 const mz_uint8* pSrc_end = pSrc + (counter & ~7);
2540#ifdef MINIZ_UNALIGNED_USE_MEMCPY
2541 memcpy(pOut_buf_cur, pSrc,
sizeof(mz_uint32) * 2);
2543 ((mz_uint32*)pOut_buf_cur)[0] = ((
const mz_uint32*)pSrc)[0];
2544 ((mz_uint32*)pOut_buf_cur)[1] = ((
const mz_uint32*)pSrc)[1];
2547 }
while ((pSrc += 8) < pSrc_end);
2548 if ((counter &= 7) < 3) {
2550 pOut_buf_cur[0] = pSrc[0];
2552 pOut_buf_cur[1] = pSrc[1];
2553 pOut_buf_cur += counter;
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];
2568 pOut_buf_cur[0] = pSrc[0];
2570 pOut_buf_cur[1] = pSrc[1];
2571 pOut_buf_cur += counter;
2575 }
while (!(r->m_final & 1));
2580 while ((pIn_buf_cur > pIn_buf_next) && (num_bits >= 8)) {
2584 bit_buf &= ~(~(tinfl_bit_buf_t)0 << num_bits);
2585 MZ_ASSERT(!num_bits);
2587 if (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER) {
2588 for (counter = 0; counter < 4; ++counter) {
2594 r->m_z_adler32 = (r->m_z_adler32 << 8) | s;
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)) {
2611 r->m_num_bits = num_bits;
2612 r->m_bit_buf = bit_buf & ~(~(tinfl_bit_buf_t)0 << num_bits);
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;
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;
2635 for (; i < block_len; ++i)
2636 s1 += *ptr++, s2 += s1;
2637 s1 %= 65521U, s2 %= 65521U;
2638 buf_len -= block_len;
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;
2651 tinfl_decompressor decomp;
2652 void *pBuf = NULL, *pNew_buf;
2653 size_t src_buf_ofs = 0, out_buf_capacity = 0;
2655 tinfl_init(&decomp);
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)) {
2665 src_buf_ofs += src_buf_size;
2666 *pOut_len += dst_buf_size;
2667 if (status == TINFL_STATUS_DONE)
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);
2679 out_buf_capacity = new_out_buf_capacity;
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;
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;
2700 return TINFL_STATUS_FAILED;
2701 memset(pDict, 0, TINFL_LZ_DICT_SIZE);
2702 tinfl_init(&decomp);
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)))
2710 if (status != TINFL_STATUS_HAS_MORE_OUTPUT) {
2711 result = (status == TINFL_STATUS_DONE);
2714 dict_ofs = (dict_ofs + dst_buf_size) & (TINFL_LZ_DICT_SIZE - 1);
2717 *pIn_buf_size = in_buf_ofs;
2721#ifndef MINIZ_NO_MALLOC
2724 tinfl_decompressor* pDecomp = (tinfl_decompressor*)MZ_MALLOC(
sizeof(tinfl_decompressor));
2726 tinfl_init(pDecomp);
2768#ifndef MINIZ_NO_ARCHIVE_APIS
2776#ifdef MINIZ_NO_STDIO
2777#define MZ_FILE void*
2779#include <sys/stat.h>
2781#if defined(_MSC_VER) || defined(__MINGW64__) || defined(__MINGW32__)
2783#define WIN32_LEAN_AND_MEAN
2786static WCHAR* mz_utf8z_to_widechar(
const char* str)
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);
2794static FILE* mz_fopen(
const char* pFilename,
const char* pMode)
2796 WCHAR* wFilename = mz_utf8z_to_widechar(pFilename);
2797 WCHAR* wMode = mz_utf8z_to_widechar(pMode);
2799 errno_t err = _wfopen_s(&pFile, wFilename, wMode);
2802 return err ? NULL : pFile;
2805static FILE* mz_freopen(
const char* pPath,
const char* pMode, FILE* pStream)
2807 WCHAR* wPath = mz_utf8z_to_widechar(pPath);
2808 WCHAR* wMode = mz_utf8z_to_widechar(pMode);
2810 errno_t err = _wfreopen_s(&pFile, wPath, wMode, pStream);
2813 return err ? NULL : pFile;
2816#if defined(__MINGW32__)
2817static int mz_stat(
const char* path,
struct _stat* buffer)
2819 WCHAR* wPath = mz_utf8z_to_widechar(path);
2820 int res = _wstat(wPath, buffer);
2825static int mz_stat64(
const char* path,
struct __stat64* buffer)
2827 WCHAR* wPath = mz_utf8z_to_widechar(path);
2828 int res = _wstat64(wPath, buffer);
2834#ifndef MINIZ_NO_TIME
2835#include <sys/utime.h>
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
2847#define MZ_FILE_STAT_STRUCT _stat64
2848#define MZ_FILE_STAT mz_stat64
2850#define MZ_FFLUSH fflush
2851#define MZ_FREOPEN mz_freopen
2852#define MZ_DELETE_FILE remove
2854#elif defined(__WATCOMC__)
2855#ifndef MINIZ_NO_TIME
2856#include <sys/utime.h>
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
2870#elif defined(__TINYC__)
2871#ifndef MINIZ_NO_TIME
2872#include <sys/utime.h>
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
2886#elif defined(__USE_LARGEFILE64)
2887#ifndef MINIZ_NO_TIME
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
2902#elif defined(__APPLE__) || defined(__FreeBSD__)
2903#ifndef MINIZ_NO_TIME
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
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
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
2931#define MZ_FTELL64 ftello
2932#define MZ_FSEEK64 fseeko
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
2942#define MZ_TOLOWER(c) ((((c) >= 'A') && ((c) <= 'Z')) ? ((c) - 'A' + 'a') : (c))
3063#define MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(array_ptr, element_size) (array_ptr)->m_element_size = element_size
3065#if defined(DEBUG) || defined(_DEBUG)
3066static MZ_FORCEINLINE mz_uint mz_zip_array_range_check(
const mz_zip_array* pArray, mz_uint index)
3068 MZ_ASSERT(index < pArray->m_size);
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)]
3073#define MZ_ZIP_ARRAY_ELEMENT(array_ptr, element_type, index) ((element_type*)((array_ptr)->m_p))[index]
3084 pZip->m_pFree(pZip->m_pAlloc_opaque, pArray->
m_p);
3091 size_t new_capacity = min_new_capacity;
3096 new_capacity = MZ_MAX(1, pArray->
m_capacity);
3097 while (new_capacity < min_new_capacity)
3100 if (NULL == (pNew_p = pZip->m_pRealloc(pZip->m_pAlloc_opaque, pArray->
m_p, pArray->
m_element_size, new_capacity)))
3102 pArray->
m_p = pNew_p;
3122 pArray->
m_size = new_size;
3133 size_t orig_size = pArray->
m_size;
3141#ifndef MINIZ_NO_TIME
3145 memset(&tm, 0,
sizeof(tm));
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;
3156#ifndef MINIZ_NO_ARCHIVE_WRITING_APIS
3160 struct tm tm_struct;
3161 struct tm* tm = &tm_struct;
3162 errno_t err = localtime_s(tm, &time);
3169 struct tm* tm = localtime(&time);
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);
3177#ifndef MINIZ_NO_STDIO
3178#ifndef MINIZ_NO_ARCHIVE_WRITING_APIS
3187 *pTime = file_stat.st_mtime;
3197 memset(&t, 0,
sizeof(t));
3198 t.actime = access_time;
3199 t.modtime = modified_time;
3201 return !utime(pFilename, &t);
3209 pZip->m_last_error = err_num;
3216 if ((!pZip) || (pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_INVALID))
3219 if (!pZip->m_pAlloc)
3223 if (!pZip->m_pRealloc)
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;
3231 if (NULL == (pZip->m_pState = (mz_zip_internal_state*)pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1,
sizeof(mz_zip_internal_state))))
3234 memset(pZip->m_pState, 0,
sizeof(mz_zip_internal_state));
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;
3242 pZip->m_zip_mode = MZ_ZIP_MODE_READING;
3252 mz_uint8 l = 0, r = 0;
3255 pE = pL + MZ_MIN(l_len, r_len);
3262 return (pL == pE) ? (l_len < r_len) : (l < r);
3265#define MZ_SWAP_UINT32(a, b) \
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;
3288 start = (
size - 2U) >> 1U;
3290 mz_uint64 child, root = start;
3292 if ((child = (root << 1U) + 1U) >=
size)
3307 mz_uint64 child, root = 0;
3310 if ((child = (root << 1U) + 1U) >= end)
3312 child += (((child + 1U) < end) &&
mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, pIndices[child], pIndices[child + 1U]));
3324 mz_int64 cur_file_ofs;
3325 mz_uint32 buf_u32[4096 /
sizeof(mz_uint32)];
3326 mz_uint8* pBuf = (mz_uint8*)buf_u32;
3329 if (pZip->m_archive_size < record_size)
3333 cur_file_ofs = MZ_MAX((mz_int64)pZip->m_archive_size - (mz_int64)
sizeof(buf_u32), 0);
3335 int i, n = (
int)MZ_MIN(
sizeof(buf_u32), pZip->m_archive_size - cur_file_ofs);
3337 if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pBuf, n) != (mz_uint)n)
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)
3354 if ((!cur_file_ofs) || ((pZip->m_archive_size - cur_file_ofs) >= ((mz_uint64)(MZ_UINT16_MAX) + record_size)))
3357 cur_file_ofs = MZ_MAX(cur_file_ofs - (
sizeof(buf_u32) - 3), 0);
3360 *pOfs = cur_file_ofs;
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;
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);
3375 mz_uint8* pZip64_locator = (mz_uint8*)zip64_end_of_central_dir_locator_u32;
3378 mz_uint8* pZip64_end_of_central_dir = (mz_uint8*)zip64_end_of_central_dir_header_u32;
3380 mz_uint64 zip64_end_of_central_dir_ofs = 0;
3405 pZip->m_pState->m_zip64 = MZ_TRUE;
3419 if (pZip->m_pState->m_zip64) {
3429 if (zip64_total_num_of_disks != 1U)
3433 if (zip64_cdir_total_entries > MZ_UINT32_MAX)
3436 pZip->m_total_files = (mz_uint32)zip64_cdir_total_entries;
3438 if (zip64_cdir_total_entries_on_this_disk > MZ_UINT32_MAX)
3441 cdir_entries_on_this_disk = (mz_uint32)zip64_cdir_total_entries_on_this_disk;
3444 if (zip64_size_of_central_directory > MZ_UINT32_MAX)
3447 cdir_size = (mz_uint32)zip64_size_of_central_directory;
3456 if (pZip->m_total_files != cdir_entries_on_this_disk)
3459 if (((num_this_disk | cdir_disk_index) != 0) && ((num_this_disk != 1) || (cdir_disk_index != 1)))
3465 if ((cdir_ofs + (mz_uint64)cdir_size) > pZip->m_archive_size)
3468 pZip->m_central_directory_file_ofs = cdir_ofs;
3470 if (pZip->m_total_files) {
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)))
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))
3481 if (pZip->m_pRead(pZip->m_pIO_opaque, cdir_ofs, pZip->m_pState->m_central_dir.m_p, cdir_size) != cdir_size)
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;
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);
3495 if (sort_central_dir)
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)) {
3506 mz_uint32 extra_size_remaining = ext_data_size;
3508 if (extra_size_remaining) {
3509 const mz_uint8* pExtra_data;
3513 buf = MZ_MALLOC(ext_data_size);
3522 pExtra_data = (mz_uint8*)
buf;
3529 mz_uint32 field_data_size;
3531 if (extra_size_remaining < (
sizeof(mz_uint16) * 2)) {
3536 field_id = MZ_READ_LE16(pExtra_data);
3537 field_data_size = MZ_READ_LE16(pExtra_data +
sizeof(mz_uint16));
3539 if ((field_data_size +
sizeof(mz_uint16) * 2) > extra_size_remaining) {
3546 pZip->m_pState->m_zip64 = MZ_TRUE;
3547 pZip->m_pState->m_zip64_has_extended_info_fields = MZ_TRUE;
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);
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))
3566 if ((disk_index == MZ_UINT16_MAX) || ((disk_index != num_this_disk) && (disk_index != 1)))
3569 if (comp_size != MZ_UINT32_MAX) {
3581 n -= total_header_size;
3582 p += total_header_size;
3586 if (sort_central_dir)
3600 mz_bool status = MZ_TRUE;
3605 if ((!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || (pZip->m_zip_mode != MZ_ZIP_MODE_READING)) {
3607 pZip->m_last_error = MZ_ZIP_INVALID_PARAMETER;
3612 if (pZip->m_pState) {
3613 mz_zip_internal_state* pState = pZip->m_pState;
3614 pZip->m_pState = NULL;
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) {
3625 pZip->m_last_error = MZ_ZIP_FILE_CLOSE_FAILED;
3629 pState->m_pFile = NULL;
3633 pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
3635 pZip->m_zip_mode = MZ_ZIP_MODE_INVALID;
3646 if ((!pZip) || (!pZip->m_pRead))
3652 pZip->m_zip_type = MZ_ZIP_TYPE_USER;
3653 pZip->m_archive_size =
size;
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);
3682 pZip->m_zip_type = MZ_ZIP_TYPE_MEMORY;
3683 pZip->m_archive_size =
size;
3685 pZip->m_pIO_opaque = pZip;
3686 pZip->m_pNeeds_keepalive = NULL;
3689 pZip->m_pState->m_pMem =
const_cast<void*
>(pMem);
3691 pZip->m_pState->m_pMem = (
void*)pMem;
3694 pZip->m_pState->m_mem_size =
size;
3704#ifndef MINIZ_NO_STDIO
3707 mz_zip_archive* pZip = (mz_zip_archive*)pOpaque;
3708 mz_int64 cur_ofs =
MZ_FTELL64(pZip->m_pState->m_pFile);
3710 file_ofs += pZip->m_pState->m_file_archive_start_ofs;
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))))
3715 return MZ_FREAD(pBuf, 1, n, pZip->m_pState->m_pFile);
3725 mz_uint64 file_size;
3735 file_size = archive_size;
3757 pZip->m_zip_type = MZ_ZIP_TYPE_FILE;
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;
3774 mz_uint64 cur_file_ofs;
3776 if ((!pZip) || (!pFile))
3781 if (!archive_size) {
3785 archive_size =
MZ_FTELL64(pFile) - cur_file_ofs;
3794 pZip->m_zip_type = MZ_ZIP_TYPE_CFILE;
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;
3812static MZ_FORCEINLINE
const mz_uint8*
mz_zip_get_cdh(mz_zip_archive* pZip, mz_uint file_index)
3814 if ((!pZip) || (!pZip->m_pState) || (file_index >= pZip->m_total_files))
3866 mz_uint filename_len, attribute_mapping_id, external_attr;
3883 (void)attribute_mapping_id;
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)
3896 const mz_uint8* p = pCentral_dir_header;
3898 if (pFound_zip64_extra_data)
3899 *pFound_zip64_extra_data = MZ_FALSE;
3901 if ((!p) || (!pStat))
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);
3911#ifndef MINIZ_NO_TIME
3923 n = MZ_MIN(n, MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE - 1);
3925 pStat->m_filename[n] =
'\0';
3928 n = MZ_MIN(n, MZ_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE - 1);
3929 pStat->m_comment_size = n;
3931 pStat->m_comment[n] =
'\0';
3940 if (MZ_MAX(MZ_MAX(pStat->m_comp_size, pStat->m_uncomp_size), pStat->m_local_header_ofs) == MZ_UINT32_MAX) {
3944 if (extra_size_remaining) {
3949 mz_uint32 field_data_size;
3951 if (extra_size_remaining < (
sizeof(mz_uint16) * 2))
3954 field_id = MZ_READ_LE16(pExtra_data);
3955 field_data_size = MZ_READ_LE16(pExtra_data +
sizeof(mz_uint16));
3957 if ((field_data_size +
sizeof(mz_uint16) * 2) > extra_size_remaining)
3961 const mz_uint8* pField_data = pExtra_data +
sizeof(mz_uint16) * 2;
3962 mz_uint32 field_data_remaining = field_data_size;
3964 if (pFound_zip64_extra_data)
3965 *pFound_zip64_extra_data = MZ_TRUE;
3967 if (pStat->m_uncomp_size == MZ_UINT32_MAX) {
3968 if (field_data_remaining <
sizeof(mz_uint64))
3971 pStat->m_uncomp_size = MZ_READ_LE64(pField_data);
3972 pField_data +=
sizeof(mz_uint64);
3973 field_data_remaining -=
sizeof(mz_uint64);
3976 if (pStat->m_comp_size == MZ_UINT32_MAX) {
3977 if (field_data_remaining <
sizeof(mz_uint64))
3980 pStat->m_comp_size = MZ_READ_LE64(pField_data);
3981 pField_data +=
sizeof(mz_uint64);
3982 field_data_remaining -=
sizeof(mz_uint64);
3985 if (pStat->m_local_header_ofs == MZ_UINT32_MAX) {
3986 if (field_data_remaining <
sizeof(mz_uint64))
3989 pStat->m_local_header_ofs = MZ_READ_LE64(pField_data);
3990 pField_data +=
sizeof(mz_uint64);
3991 field_data_remaining -=
sizeof(mz_uint64);
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);
4009 if (flags & MZ_ZIP_FLAG_CASE_SENSITIVE)
4010 return 0 == memcmp(
pA, pB, len);
4011 for (i = 0; i < len; ++i)
4021 mz_uint8 l = 0, r = 0;
4023 pE = pL + MZ_MIN(l_len, r_len);
4030 return (pL == pE) ? (
int)(l_len - r_len) : (l - r);
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);
4048 mz_int64 l = 0, h = (mz_int64)
size - 1;
4051 mz_int64 m = l + ((h - l) >> 1);
4052 mz_uint32 file_index = pIndices[(mz_uint32)m];
4057 *pIndex = file_index;
4059 }
else if (comp < 0)
4081 size_t name_len, comment_len;
4086 if ((!pZip) || (!pZip->m_pState) || (!pName))
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)) {
4095 name_len = strlen(pName);
4096 if (name_len > MZ_UINT16_MAX)
4099 comment_len = pComment ? strlen(pComment) : 0;
4100 if (comment_len > MZ_UINT16_MAX)
4103 for (file_index = 0; file_index < pZip->m_total_files; file_index++) {
4107 if (filename_len < name_len)
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)))
4115 if ((flags & MZ_ZIP_FLAG_IGNORE_PATH) && (filename_len)) {
4116 int ofs = filename_len - 1;
4118 if ((pFilename[ofs] ==
'/') || (pFilename[ofs] ==
'\\') || (pFilename[ofs] ==
':'))
4120 }
while (--ofs >= 0);
4123 filename_len -= ofs;
4125 if ((filename_len == name_len) && (
mz_zip_string_equal(pName, pFilename, filename_len, flags))) {
4127 *pIndex = file_index;
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)
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;
4142 mz_uint8* pLocal_header = (mz_uint8*)local_header_u32;
4143 tinfl_decompressor inflator;
4145 if ((!pZip) || (!pZip->m_pState) || ((buf_size) && (!pBuf)) || ((user_read_buf_size) && (!pUser_read_buf)) || (!pZip->m_pRead))
4154 if ((file_stat.m_is_directory) || (!file_stat.m_comp_size))
4162 if ((!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (file_stat.m_method != 0) && (file_stat.m_method !=
MZ_DEFLATED))
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)
4171 cur_file_ofs = file_stat.m_local_header_ofs;
4179 if ((cur_file_ofs + file_stat.m_comp_size) > pZip->m_archive_size)
4182 if ((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!file_stat.m_method)) {
4184 if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pBuf, (
size_t)needed_size) != needed_size)
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)
4198 tinfl_init(&inflator);
4200 if (pZip->m_pState->m_pMem) {
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;
4205 }
else if (pUser_read_buf) {
4207 if (!user_read_buf_size)
4209 pRead_buf = (mz_uint8*)pUser_read_buf;
4210 read_buf_size = user_read_buf_size;
4212 comp_remaining = file_stat.m_comp_size;
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))
4219 if (NULL == (pRead_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, (
size_t)read_buf_size)))
4223 comp_remaining = file_stat.m_comp_size;
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;
4236 cur_file_ofs += read_buf_avail;
4237 comp_remaining -= read_buf_avail;
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);
4247 if (status == TINFL_STATUS_DONE) {
4249 if (out_buf_ofs != file_stat.m_uncomp_size) {
4251 status = TINFL_STATUS_FAILED;
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) {
4256 status = TINFL_STATUS_FAILED;
4261 if ((!pZip->m_pState->m_pMem) && (!pUser_read_buf))
4262 pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf);
4264 return status == TINFL_STATUS_DONE;
4274 mz_uint32 file_index;
4292 mz_zip_archive_file_stat file_stat;
4293 mz_uint64 alloc_size;
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)) {
4308 if (NULL == (pBuf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, (
size_t)alloc_size))) {
4314 pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf);
4319 *pSize = (size_t)alloc_size;
4325 mz_uint32 file_index;
4336 int status = TINFL_STATUS_DONE;
4337#ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
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;
4345 mz_uint8* pLocal_header = (mz_uint8*)local_header_u32;
4347 if ((!pZip) || (!pZip->m_pState) || (!pCallback) || (!pZip->m_pRead))
4354 if ((file_stat.m_is_directory) || (!file_stat.m_comp_size))
4362 if ((!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (file_stat.m_method != 0) && (file_stat.m_method !=
MZ_DEFLATED))
4366 cur_file_ofs = file_stat.m_local_header_ofs;
4374 if ((cur_file_ofs + file_stat.m_comp_size) > pZip->m_archive_size)
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;
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)))
4388 comp_remaining = file_stat.m_comp_size;
4391 if ((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!file_stat.m_method)) {
4393 if (pZip->m_pState->m_pMem) {
4394 if (((
sizeof(
size_t) ==
sizeof(mz_uint32))) && (file_stat.m_comp_size > MZ_UINT32_MAX))
4397 if (pCallback(pOpaque, out_buf_ofs, pRead_buf, (
size_t)file_stat.m_comp_size) != file_stat.m_comp_size) {
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);
4406 cur_file_ofs += file_stat.m_comp_size;
4407 out_buf_ofs += file_stat.m_comp_size;
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) {
4414 status = TINFL_STATUS_FAILED;
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);
4424 if (pCallback(pOpaque, out_buf_ofs, pRead_buf, (
size_t)read_buf_avail) != read_buf_avail) {
4426 status = TINFL_STATUS_FAILED;
4430 cur_file_ofs += read_buf_avail;
4431 out_buf_ofs += read_buf_avail;
4432 comp_remaining -= read_buf_avail;
4436 tinfl_decompressor inflator;
4437 tinfl_init(&inflator);
4439 if (NULL == (pWrite_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, TINFL_LZ_DICT_SIZE))) {
4441 status = TINFL_STATUS_FAILED;
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) {
4450 status = TINFL_STATUS_FAILED;
4453 cur_file_ofs += read_buf_avail;
4454 comp_remaining -= read_buf_avail;
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;
4464 if (pCallback(pOpaque, out_buf_ofs, pWrite_buf_cur, out_buf_size) != out_buf_size) {
4466 status = TINFL_STATUS_FAILED;
4470#ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
4471 file_crc32 = (mz_uint32)
mz_crc32(file_crc32, pWrite_buf_cur, out_buf_size);
4473 if ((out_buf_ofs += out_buf_size) > file_stat.m_uncomp_size) {
4475 status = TINFL_STATUS_FAILED;
4479 }
while ((status == TINFL_STATUS_NEEDS_MORE_INPUT) || (status == TINFL_STATUS_HAS_MORE_OUTPUT));
4483 if ((status == TINFL_STATUS_DONE) && (!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA))) {
4485 if (out_buf_ofs != file_stat.m_uncomp_size) {
4487 status = TINFL_STATUS_FAILED;
4489#ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
4490 else if (file_crc32 != file_stat.m_crc32) {
4492 status = TINFL_STATUS_FAILED;
4497 if (!pZip->m_pState->m_pMem)
4498 pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf);
4501 pZip->m_pFree(pZip->m_pAlloc_opaque, pWrite_buf);
4503 return status == TINFL_STATUS_DONE;
4508 mz_uint32 file_index;
4517 mz_zip_reader_extract_iter_state* pState;
4519 mz_uint8* pLocal_header = (mz_uint8*)local_header_u32;
4522 if ((!pZip) || (!pZip->m_pState))
4526 pState = (mz_zip_reader_extract_iter_state*)pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1,
sizeof(mz_zip_reader_extract_iter_state));
4534 pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
4541 pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
4546 if ((!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (pState->file_stat.m_method != 0) && (pState->file_stat.m_method !=
MZ_DEFLATED)) {
4548 pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
4553 pState->pZip = pZip;
4554 pState->flags = flags;
4557 pState->status = TINFL_STATUS_DONE;
4558#ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
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;
4568 pState->cur_file_ofs = pState->file_stat.m_local_header_ofs;
4571 pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
4577 pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
4582 if ((pState->cur_file_ofs + pState->file_stat.m_comp_size) > pZip->m_archive_size) {
4584 pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
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;
4594 if (!((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!pState->file_stat.m_method))) {
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))) {
4599 pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
4604 pState->read_buf_size = 0;
4606 pState->read_buf_avail = 0;
4607 pState->comp_remaining = pState->file_stat.m_comp_size;
4610 if (!((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!pState->file_stat.m_method))) {
4612 tinfl_init(&pState->inflator);
4615 if (NULL == (pState->pWrite_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, TINFL_LZ_DICT_SIZE))) {
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);
4629 mz_uint32 file_index;
4641 size_t copied_to_caller = 0;
4644 if ((!pState) || (!pState->pZip) || (!pState->pZip->m_pState) || (!pvBuf))
4647 if ((pState->flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!pState->file_stat.m_method)) {
4649 copied_to_caller = (size_t)MZ_MIN(buf_size, pState->comp_remaining);
4652 if (pState->pZip->m_pState->m_pMem) {
4654 memcpy(pvBuf, pState->pRead_buf, copied_to_caller);
4655 pState->pRead_buf = ((mz_uint8*)pState->pRead_buf) + copied_to_caller;
4658 if (pState->pZip->m_pRead(pState->pZip->m_pIO_opaque, pState->cur_file_ofs, pvBuf, copied_to_caller) != copied_to_caller) {
4661 pState->status = TINFL_STATUS_FAILED;
4662 copied_to_caller = 0;
4666#ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
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);
4673 pState->cur_file_ofs += copied_to_caller;
4674 pState->out_buf_ofs += copied_to_caller;
4675 pState->comp_remaining -= copied_to_caller;
4679 mz_uint8* pWrite_buf_cur = (mz_uint8*)pState->pWrite_buf + (pState->out_buf_ofs & (TINFL_LZ_DICT_SIZE - 1));
4682 size_t in_buf_size, out_buf_size = TINFL_LZ_DICT_SIZE - (pState->out_buf_ofs & (TINFL_LZ_DICT_SIZE - 1));
4684 if (!pState->out_blk_remain) {
4686 if ((!pState->read_buf_avail) && (!pState->pZip->m_pState->m_pMem)) {
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) {
4691 pState->status = TINFL_STATUS_FAILED;
4696 pState->cur_file_ofs += pState->read_buf_avail;
4697 pState->comp_remaining -= pState->read_buf_avail;
4698 pState->read_buf_ofs = 0;
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;
4708 pState->out_blk_remain = out_buf_size;
4711 if (pState->out_blk_remain) {
4713 size_t to_copy = MZ_MIN((buf_size - copied_to_caller), pState->out_blk_remain);
4716 memcpy((mz_uint8*)pvBuf + copied_to_caller, pWrite_buf_cur, to_copy);
4718#ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
4720 pState->file_crc32 = (mz_uint32)
mz_crc32(pState->file_crc32, pWrite_buf_cur, to_copy);
4724 pState->out_blk_remain -= to_copy;
4727 if ((pState->out_buf_ofs += to_copy) > pState->file_stat.m_uncomp_size) {
4729 pState->status = TINFL_STATUS_FAILED;
4734 copied_to_caller += to_copy;
4736 }
while ((copied_to_caller < buf_size) && ((pState->status == TINFL_STATUS_NEEDS_MORE_INPUT) || (pState->status == TINFL_STATUS_HAS_MORE_OUTPUT)));
4740 return copied_to_caller;
4748 if ((!pState) || (!pState->pZip) || (!pState->pZip->m_pState))
4752 if ((pState->status == TINFL_STATUS_DONE) && (!(pState->flags & MZ_ZIP_FLAG_COMPRESSED_DATA))) {
4754 if (pState->out_buf_ofs != pState->file_stat.m_uncomp_size) {
4756 pState->status = TINFL_STATUS_FAILED;
4758#ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
4759 else if (pState->file_crc32 != pState->file_stat.m_crc32) {
4761 pState->status = TINFL_STATUS_FAILED;
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);
4773 status = pState->status;
4776 pState->pZip->m_pFree(pState->pZip->m_pAlloc_opaque, pState);
4778 return status == TINFL_STATUS_DONE;
4781#ifndef MINIZ_NO_STDIO
4786 return MZ_FWRITE(pBuf, 1, n, (MZ_FILE*)pOpaque);
4792 mz_zip_archive_file_stat file_stat;
4798 if ((file_stat.m_is_directory) || (!file_stat.m_is_supported))
4801 pFile =
MZ_FOPEN(pDst_filename,
"wb");
4814#if !defined(MINIZ_NO_TIME) && !defined(MINIZ_NO_STDIO)
4824 mz_uint32 file_index;
4833 mz_zip_archive_file_stat file_stat;
4838 if ((file_stat.m_is_directory) || (!file_stat.m_is_supported))
4846 mz_uint32 file_index;
4856 mz_uint32* p = (mz_uint32*)pOpaque;
4858 *p = (mz_uint32)
mz_crc32(*p, (
const mz_uint8*)pBuf, n);
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;
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;
4875 mz_bool has_data_descriptor;
4876 mz_uint32 local_header_bit_flags;
4881 if ((!pZip) || (!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || (!pZip->m_pRead))
4884 if (file_index > pZip->m_total_files)
4887 pState = pZip->m_pState;
4895 if ((file_stat.m_is_directory) || (!file_stat.m_uncomp_size))
4899 if (file_stat.m_is_encrypted)
4903 if ((file_stat.m_method != 0) && (file_stat.m_method !=
MZ_DEFLATED))
4906 if (!file_stat.m_is_supported)
4910 local_header_ofs = file_stat.m_local_header_ofs;
4923 has_data_descriptor = (local_header_bit_flags & 8) != 0;
4925 if (local_header_filename_len != strlen(file_stat.m_filename))
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)
4931 if (!
mz_zip_array_resize(pZip, &file_data_array, MZ_MAX(local_header_filename_len, local_header_extra_len), MZ_FALSE)) {
4933 goto handle_failure;
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) {
4939 goto handle_failure;
4943 if (memcmp(file_stat.m_filename, file_data_array.
m_p, local_header_filename_len) != 0) {
4945 goto handle_failure;
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;
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) {
4955 goto handle_failure;
4959 mz_uint32 field_id, field_data_size, field_total_size;
4961 if (extra_size_remaining < (
sizeof(mz_uint16) * 2)) {
4963 goto handle_failure;
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;
4970 if (field_total_size > extra_size_remaining) {
4972 goto handle_failure;
4976 const mz_uint8* pSrc_field_data = pExtra_data +
sizeof(mz_uint32);
4978 if (field_data_size <
sizeof(mz_uint64) * 2) {
4980 goto handle_failure;
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));
4986 found_zip64_ext_data_in_ldir = MZ_TRUE;
4990 pExtra_data += field_total_size;
4991 extra_size_remaining -= field_total_size;
4992 }
while (extra_size_remaining);
4997 if ((has_data_descriptor) && (!local_header_comp_size) && (!local_header_crc32)) {
4998 mz_uint8 descriptor_buf[32];
5000 const mz_uint8* pSrc;
5001 mz_uint32 file_crc32;
5002 mz_uint64 comp_size = 0, uncomp_size = 0;
5004 mz_uint32 num_descriptor_uint32s = ((pState->m_zip64) || (found_zip64_ext_data_in_ldir)) ? 6 : 4;
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)) {
5008 goto handle_failure;
5012 pSrc = has_id ? (descriptor_buf +
sizeof(mz_uint32)) : descriptor_buf;
5014 file_crc32 = MZ_READ_LE32(pSrc);
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));
5020 comp_size = MZ_READ_LE32(pSrc +
sizeof(mz_uint32));
5021 uncomp_size = MZ_READ_LE32(pSrc +
sizeof(mz_uint32) +
sizeof(mz_uint32));
5024 if ((file_crc32 != file_stat.m_crc32) || (comp_size != file_stat.m_comp_size) || (uncomp_size != file_stat.m_uncomp_size)) {
5026 goto handle_failure;
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)) {
5031 goto handle_failure;
5037 if ((flags & MZ_ZIP_FLAG_VALIDATE_HEADERS_ONLY) == 0) {
5042 if (uncomp_crc32 != file_stat.m_crc32) {
5057 mz_zip_internal_state* pState;
5060 if ((!pZip) || (!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || (!pZip->m_pRead))
5063 pState = pZip->m_pState;
5066 if (!pState->m_zip64) {
5067 if (pZip->m_total_files > MZ_UINT16_MAX)
5070 if (pZip->m_archive_size > MZ_UINT32_MAX)
5073 if (pState->m_central_dir.m_size >= MZ_UINT32_MAX)
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;
5089 if (found_index != i)
5102 mz_bool success = MZ_TRUE;
5104 mz_zip_error actual_err = MZ_ZIP_NO_ERROR;
5106 if ((!pMem) || (!
size)) {
5108 *pErr = MZ_ZIP_INVALID_PARAMETER;
5116 *pErr = zip.m_last_error;
5121 actual_err = zip.m_last_error;
5127 actual_err = zip.m_last_error;
5137#ifndef MINIZ_NO_STDIO
5140 mz_bool success = MZ_TRUE;
5142 mz_zip_error actual_err = MZ_ZIP_NO_ERROR;
5146 *pErr = MZ_ZIP_INVALID_PARAMETER;
5154 *pErr = zip.m_last_error;
5159 actual_err = zip.m_last_error;
5165 actual_err = zip.m_last_error;
5178#ifndef MINIZ_NO_ARCHIVE_WRITING_APIS
5183 p[1] = (mz_uint8)(v >> 8);
5188 p[1] = (mz_uint8)(v >> 8);
5189 p[2] = (mz_uint8)(v >> 16);
5190 p[3] = (mz_uint8)(v >> 24);
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))
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);
5212 if ((
sizeof(
size_t) ==
sizeof(mz_uint32)) && (new_size > 0x7FFFFFFF)) {
5217 if (new_size > pState->m_mem_capacity) {
5219 size_t new_capacity = MZ_MAX(64, pState->m_mem_capacity);
5221 while (new_capacity < new_size)
5224 if (NULL == (pNew_block = pZip->m_pRealloc(pZip->m_pAlloc_opaque, pState->m_pMem, 1, new_capacity))) {
5229 pState->m_pMem = pNew_block;
5230 pState->m_mem_capacity = new_capacity;
5232 memcpy((mz_uint8*)pState->m_pMem + file_ofs, pBuf, n);
5233 pState->m_mem_size = (size_t)new_size;
5239 mz_zip_internal_state* pState;
5240 mz_bool status = MZ_TRUE;
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))) {
5248 pState = pZip->m_pState;
5249 pZip->m_pState = NULL;
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) {
5264 pState->m_pFile = NULL;
5269 pZip->m_pFree(pZip->m_pAlloc_opaque, pState->m_pMem);
5270 pState->m_pMem = NULL;
5273 pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
5274 pZip->m_zip_mode = MZ_ZIP_MODE_INVALID;
5280 mz_bool zip64 = (flags & MZ_ZIP_FLAG_WRITE_ZIP64) != 0;
5282 if ((!pZip) || (pZip->m_pState) || (!pZip->m_pWrite) || (pZip->m_zip_mode != MZ_ZIP_MODE_INVALID))
5285 if (flags & MZ_ZIP_FLAG_WRITE_ALLOW_READING) {
5290 if (pZip->m_file_offset_alignment) {
5292 if (pZip->m_file_offset_alignment & (pZip->m_file_offset_alignment - 1))
5296 if (!pZip->m_pAlloc)
5300 if (!pZip->m_pRealloc)
5303 pZip->m_archive_size = existing_size;
5304 pZip->m_central_directory_file_ofs = 0;
5305 pZip->m_total_files = 0;
5307 if (NULL == (pZip->m_pState = (mz_zip_internal_state*)pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1,
sizeof(mz_zip_internal_state))))
5310 memset(pZip->m_pState, 0,
sizeof(mz_zip_internal_state));
5316 pZip->m_pState->m_zip64 = zip64;
5317 pZip->m_pState->m_zip64_has_extended_info_fields = zip64;
5319 pZip->m_zip_type = MZ_ZIP_TYPE_USER;
5320 pZip->m_zip_mode = MZ_ZIP_MODE_WRITING;
5333 pZip->m_pNeeds_keepalive = NULL;
5335 if (flags & MZ_ZIP_FLAG_WRITE_ALLOW_READING)
5338 pZip->m_pIO_opaque = pZip;
5343 pZip->m_zip_type = MZ_ZIP_TYPE_HEAP;
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))) {
5350 pZip->m_pState->m_mem_capacity = initial_allocation_size;
5361#ifndef MINIZ_NO_STDIO
5364 mz_zip_archive* pZip = (mz_zip_archive*)pOpaque;
5365 mz_int64 cur_ofs =
MZ_FTELL64(pZip->m_pState->m_pFile);
5367 file_ofs += pZip->m_pState->m_file_archive_start_ofs;
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)))) {
5374 return MZ_FWRITE(pBuf, 1, n, pZip->m_pState->m_pFile);
5387 pZip->m_pNeeds_keepalive = NULL;
5389 if (flags & MZ_ZIP_FLAG_WRITE_ALLOW_READING)
5392 pZip->m_pIO_opaque = pZip;
5397 if (NULL == (pFile =
MZ_FOPEN(pFilename, (flags & MZ_ZIP_FLAG_WRITE_ALLOW_READING) ?
"w+b" :
"wb"))) {
5402 pZip->m_pState->m_pFile = pFile;
5403 pZip->m_zip_type = MZ_ZIP_TYPE_FILE;
5405 if (size_to_reserve_at_beginning) {
5406 mz_uint64 cur_ofs = 0;
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) {
5418 size_to_reserve_at_beginning -= n;
5419 }
while (size_to_reserve_at_beginning);
5428 pZip->m_pNeeds_keepalive = NULL;
5430 if (flags & MZ_ZIP_FLAG_WRITE_ALLOW_READING)
5433 pZip->m_pIO_opaque = pZip;
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;
5448 mz_zip_internal_state* pState;
5450 if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_READING))
5453 if (flags & MZ_ZIP_FLAG_WRITE_ZIP64) {
5455 if (!pZip->m_pState->m_zip64)
5460 if (pZip->m_pState->m_zip64) {
5461 if (pZip->m_total_files == MZ_UINT32_MAX)
5464 if (pZip->m_total_files == MZ_UINT16_MAX)
5471 pState = pZip->m_pState;
5473 if (pState->m_pFile) {
5474#ifdef MINIZ_NO_STDIO
5478 if (pZip->m_pIO_opaque != pZip)
5481 if (pZip->m_zip_type == MZ_ZIP_TYPE_FILE) {
5486 if (NULL == (pState->m_pFile =
MZ_FREOPEN(pFilename,
"r+b", pState->m_pFile))) {
5494 pZip->m_pNeeds_keepalive = NULL;
5496 }
else if (pState->m_pMem) {
5498 if (pZip->m_pIO_opaque != pZip)
5501 pState->m_mem_capacity = pState->m_mem_size;
5503 pZip->m_pNeeds_keepalive = NULL;
5506 else if (!pZip->m_pWrite)
5511 pZip->m_archive_size = pZip->m_central_directory_file_ofs;
5512 pZip->m_central_directory_file_ofs = 0;
5519 pZip->m_zip_mode = MZ_ZIP_MODE_WRITING;
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)
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)
5557 mz_uint8* pDst = pBuf;
5558 mz_uint32 field_size = 0;
5562 pDst +=
sizeof(mz_uint16) * 2;
5566 pDst +=
sizeof(mz_uint64);
5567 field_size +=
sizeof(mz_uint64);
5572 pDst +=
sizeof(mz_uint64);
5573 field_size +=
sizeof(mz_uint64);
5576 if (pLocal_header_ofs) {
5578 pDst +=
sizeof(mz_uint64);
5579 field_size +=
sizeof(mz_uint64);
5584 return (mz_uint32)(pDst - pBuf);
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)
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)
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)
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;
5642 if (!pZip->m_pState->m_zip64) {
5643 if (local_header_ofs > 0xFFFFFFFF)
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)
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))
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, ¢ral_dir_ofs, 1))) {
5666 if (*pArchive_name ==
'/')
5677 if (!pZip->m_file_offset_alignment)
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));
5686 memset(
buf, 0, MZ_MIN(
sizeof(
buf), 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)
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)
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);
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)
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;
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;
5719 mz_uint16 bit_flags = 0;
5721 if ((
int)level_and_flags < 0)
5724 if (uncomp_size || (buf_size && !(level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)))
5727 if (!(level_and_flags & MZ_ZIP_FLAG_ASCII_FILENAME))
5730 level = level_and_flags & 0xF;
5731 store_data_uncompressed = ((!level) || (level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA));
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))
5736 pState = pZip->m_pState;
5738 if (pState->m_zip64) {
5739 if (pZip->m_total_files == MZ_UINT32_MAX)
5742 if (pZip->m_total_files == MZ_UINT16_MAX) {
5743 pState->m_zip64 = MZ_TRUE;
5746 if (((mz_uint64)buf_size > 0xFFFFFFFF) || (uncomp_size > 0xFFFFFFFF)) {
5747 pState->m_zip64 = MZ_TRUE;
5752 if ((!(level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (uncomp_size))
5758#ifndef MINIZ_NO_TIME
5759 if (last_modified != NULL) {
5768 if (!(level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) {
5770 uncomp_size = buf_size;
5771 if (uncomp_size <= 3) {
5773 store_data_uncompressed = MZ_TRUE;
5777 archive_name_size = strlen(pArchive_name);
5778 if (archive_name_size > MZ_UINT16_MAX)
5787 if (!pState->m_zip64) {
5793 pState->m_zip64 = MZ_TRUE;
5798 if ((archive_name_size) && (pArchive_name[archive_name_size - 1] ==
'/')) {
5803 if ((buf_size) || (uncomp_size))
5811 if ((!store_data_uncompressed) && (buf_size)) {
5812 if (NULL == (pComp = (tdefl_compressor*)pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1,
sizeof(tdefl_compressor))))
5817 pZip->m_pFree(pZip->m_pAlloc_opaque, pComp);
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);
5825 cur_archive_file_ofs += num_alignment_padding_bytes;
5827 MZ_CLEAR_ARR(local_dir_header);
5829 if (!store_data_uncompressed || (level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) {
5833 if (pState->m_zip64) {
5834 if (uncomp_size >= MZ_UINT32_MAX || local_dir_header_ofs >= MZ_UINT32_MAX) {
5835 pExtra_data = extra_data;
5837 (uncomp_size >= MZ_UINT32_MAX) ? &comp_size : NULL, (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL);
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))
5843 if (pZip->m_pWrite(pZip->m_pIO_opaque, local_dir_header_ofs, local_dir_header,
sizeof(local_dir_header)) !=
sizeof(local_dir_header))
5846 cur_archive_file_ofs +=
sizeof(local_dir_header);
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);
5852 cur_archive_file_ofs += archive_name_size;
5854 if (pExtra_data != NULL) {
5855 if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, extra_data, extra_size) != extra_size)
5858 cur_archive_file_ofs += extra_size;
5861 if ((comp_size > MZ_UINT32_MAX) || (cur_archive_file_ofs > MZ_UINT32_MAX))
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))
5866 if (pZip->m_pWrite(pZip->m_pIO_opaque, local_dir_header_ofs, local_dir_header,
sizeof(local_dir_header)) !=
sizeof(local_dir_header))
5869 cur_archive_file_ofs +=
sizeof(local_dir_header);
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);
5875 cur_archive_file_ofs += archive_name_size;
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)
5882 cur_archive_file_ofs += user_extra_data_len;
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);
5891 cur_archive_file_ofs += buf_size;
5892 comp_size = buf_size;
5893 }
else if (buf_size) {
5901 pZip->m_pFree(pZip->m_pAlloc_opaque, pComp);
5909 pZip->m_pFree(pZip->m_pAlloc_opaque, pComp);
5920 if (pExtra_data == NULL) {
5921 if (comp_size > MZ_UINT32_MAX)
5932 if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, local_dir_footer, local_dir_footer_size) != local_dir_footer_size)
5935 cur_archive_file_ofs += local_dir_footer_size;
5938 if (pExtra_data != NULL) {
5940 (uncomp_size >= MZ_UINT32_MAX) ? &comp_size : NULL, (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL);
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))
5948 pZip->m_total_files++;
5949 pZip->m_archive_size = cur_archive_file_ofs;
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)
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;
5963 mz_uint8* pExtra_data = NULL;
5964 mz_uint32 extra_size = 0;
5966 mz_zip_internal_state* pState;
5967 mz_uint64 file_ofs = 0, cur_archive_header_file_ofs;
5969 if ((
int)level_and_flags < 0)
5971 level = level_and_flags & 0xF;
5975 if (!(level_and_flags & MZ_ZIP_FLAG_ASCII_FILENAME))
5979 if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || (!pArchive_name) || ((comment_size) && (!pComment)) || (level >
MZ_UBER_COMPRESSION))
5982 pState = pZip->m_pState;
5984 if ((!pState->m_zip64) && (max_size > MZ_UINT32_MAX)) {
5987 pState->m_zip64 = MZ_TRUE;
5991 if (level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)
5997 if (pState->m_zip64) {
5998 if (pZip->m_total_files == MZ_UINT32_MAX)
6001 if (pZip->m_total_files == MZ_UINT16_MAX) {
6002 pState->m_zip64 = MZ_TRUE;
6007 archive_name_size = strlen(pArchive_name);
6008 if (archive_name_size > MZ_UINT16_MAX)
6017 if (!pState->m_zip64) {
6023 pState->m_zip64 = MZ_TRUE;
6028#ifndef MINIZ_NO_TIME
6041 cur_archive_file_ofs += num_alignment_padding_bytes;
6042 local_dir_header_ofs = cur_archive_file_ofs;
6044 if (pZip->m_file_offset_alignment) {
6045 MZ_ASSERT((cur_archive_file_ofs & (pZip->m_file_offset_alignment - 1)) == 0);
6048 if (max_size && level) {
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)
6058 (max_size >= MZ_UINT32_MAX) ? &comp_size : NULL,
6059 (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL);
6063 (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL);
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))
6069 if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, local_dir_header,
sizeof(local_dir_header)) !=
sizeof(local_dir_header))
6072 cur_archive_file_ofs +=
sizeof(local_dir_header);
6074 if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pArchive_name, archive_name_size) != archive_name_size) {
6078 cur_archive_file_ofs += archive_name_size;
6080 if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, extra_data, extra_size) != extra_size)
6083 cur_archive_file_ofs += extra_size;
6085 if ((comp_size > MZ_UINT32_MAX) || (cur_archive_file_ofs > MZ_UINT32_MAX))
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))
6090 if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, local_dir_header,
sizeof(local_dir_header)) !=
sizeof(local_dir_header))
6093 cur_archive_file_ofs +=
sizeof(local_dir_header);
6095 if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pArchive_name, archive_name_size) != archive_name_size) {
6099 cur_archive_file_ofs += archive_name_size;
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)
6106 cur_archive_file_ofs += user_extra_data_len;
6110 void* pRead_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, MZ_ZIP_MAX_IO_BUF_SIZE);
6117 size_t n = read_callback(callback_opaque, file_ofs, pRead_buf, MZ_ZIP_MAX_IO_BUF_SIZE);
6121 if ((n > MZ_ZIP_MAX_IO_BUF_SIZE) || (file_ofs + n > max_size)) {
6122 pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf);
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);
6130 uncomp_crc32 = (mz_uint32)
mz_crc32(uncomp_crc32, (
const mz_uint8*)pRead_buf, n);
6131 cur_archive_file_ofs += n;
6133 uncomp_size = file_ofs;
6134 comp_size = uncomp_size;
6136 mz_bool result = MZ_FALSE;
6138 tdefl_compressor* pComp = (tdefl_compressor*)pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1,
sizeof(tdefl_compressor));
6140 pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf);
6149 pZip->m_pFree(pZip->m_pAlloc_opaque, pComp);
6150 pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf);
6155 tdefl_status status;
6156 tdefl_flush flush = TDEFL_NO_FLUSH;
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)) {
6165 uncomp_crc32 = (mz_uint32)
mz_crc32(uncomp_crc32, (
const mz_uint8*)pRead_buf, n);
6167 if (pZip->m_pNeeds_keepalive != NULL && pZip->m_pNeeds_keepalive(pZip->m_pIO_opaque))
6168 flush = TDEFL_FULL_FLUSH;
6171 flush = TDEFL_FINISH;
6174 if (status == TDEFL_STATUS_DONE) {
6177 }
else if (status != TDEFL_STATUS_OKAY) {
6183 pZip->m_pFree(pZip->m_pAlloc_opaque, pComp);
6186 pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf);
6190 uncomp_size = file_ofs;
6195 pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf);
6198 if (!(level_and_flags & MZ_ZIP_FLAG_WRITE_HEADER_SET_SIZE)) {
6204 if (pExtra_data == NULL) {
6205 if (comp_size > MZ_UINT32_MAX)
6216 if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, local_dir_footer, local_dir_footer_size) != local_dir_footer_size)
6219 cur_archive_file_ofs += local_dir_footer_size;
6222 if (level_and_flags & MZ_ZIP_FLAG_WRITE_HEADER_SET_SIZE) {
6223 if (pExtra_data != NULL) {
6225 (max_size >= MZ_UINT32_MAX) ? &comp_size : NULL, (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL);
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))
6235 cur_archive_header_file_ofs = local_dir_header_ofs;
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))
6240 if (pExtra_data != NULL) {
6241 cur_archive_header_file_ofs +=
sizeof(local_dir_header);
6243 if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_header_file_ofs, pArchive_name, archive_name_size) != archive_name_size) {
6247 cur_archive_header_file_ofs += archive_name_size;
6249 if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_header_file_ofs, extra_data, extra_size) != extra_size)
6252 cur_archive_header_file_ofs += extra_size;
6256 if (pExtra_data != NULL) {
6258 (uncomp_size >= MZ_UINT32_MAX) ? &comp_size : NULL, (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL);
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))
6266 pZip->m_total_files++;
6267 pZip->m_archive_size = cur_archive_file_ofs;
6272#ifndef MINIZ_NO_STDIO
6276 MZ_FILE* pSrc_file = (MZ_FILE*)pOpaque;
6279 if (((mz_int64)file_ofs < 0) || (((cur_ofs != (mz_int64)file_ofs)) && (
MZ_FSEEK64(pSrc_file, (mz_int64)file_ofs, SEEK_SET))))
6282 return MZ_FREAD(pBuf, 1, n, pSrc_file);
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)
6289 user_extra_data, user_extra_data_len, user_extra_data_central, user_extra_data_central_len);
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)
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;
6300 memset(&file_modified_time, 0,
sizeof(file_modified_time));
6302#if !defined(MINIZ_NO_TIME) && !defined(MINIZ_NO_STDIO)
6303 pFile_time = &file_modified_time;
6308 pSrc_file =
MZ_FOPEN(pSrc_filename,
"rb");
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);
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;
6337 pDst +=
sizeof(mz_uint16) * 2;
6341 pDst +=
sizeof(mz_uint64);
6346 pDst +=
sizeof(mz_uint64);
6349 if (pLocal_header_ofs) {
6351 pDst +=
sizeof(mz_uint64);
6356 pDst +=
sizeof(mz_uint32);
6359 mz_write_le16(new_ext_block +
sizeof(mz_uint16), (mz_uint16)((pDst - new_ext_block) -
sizeof(mz_uint16) * 2));
6365 if ((pExt) && (ext_len)) {
6366 mz_uint32 extra_size_remaining = ext_len;
6367 const mz_uint8* pExtra_data = pExt;
6370 mz_uint32 field_id, field_data_size, field_total_size;
6372 if (extra_size_remaining < (
sizeof(mz_uint16) * 2))
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;
6379 if (field_total_size > extra_size_remaining)
6387 pExtra_data += field_total_size;
6388 extra_size_remaining -= field_total_size;
6389 }
while (extra_size_remaining);
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;
6402 mz_uint8* pLocal_header = (mz_uint8*)local_header_u32;
6404 size_t orig_central_dir_size;
6405 mz_zip_internal_state* pState;
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;
6415 if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || (!pSource_zip->m_pRead))
6418 pState = pZip->m_pState;
6421 if ((pSource_zip->m_pState->m_zip64) && (!pZip->m_pState->m_zip64))
6425 if (NULL == (pSrc_central_header =
mz_zip_get_cdh(pSource_zip, src_file_index)))
6434 src_central_dir_following_data_size = src_filename_len + src_ext_len + src_comment_len;
6442 if (!pState->m_zip64) {
6443 if (pZip->m_total_files == MZ_UINT16_MAX)
6447 if (pZip->m_total_files == MZ_UINT32_MAX)
6454 cur_src_file_ofs = src_file_stat.m_local_header_ofs;
6455 cur_dst_file_ofs = pZip->m_archive_size;
6471 src_archive_bytes_remaining = src_file_stat.m_comp_size + local_header_filename_size + local_header_extra_len;
6474 if ((local_header_extra_len) && ((local_header_comp_size == MZ_UINT32_MAX) || (local_header_uncomp_size == MZ_UINT32_MAX))) {
6476 const mz_uint8* pExtra_data;
6477 mz_uint32 extra_size_remaining = local_header_extra_len;
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) {
6489 pExtra_data = (
const mz_uint8*)file_data_array.
m_p;
6492 mz_uint32 field_id, field_data_size, field_total_size;
6494 if (extra_size_remaining < (
sizeof(mz_uint16) * 2)) {
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;
6503 if (field_total_size > extra_size_remaining) {
6509 const mz_uint8* pSrc_field_data = pExtra_data +
sizeof(mz_uint32);
6511 if (field_data_size <
sizeof(mz_uint64) * 2) {
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));
6519 found_zip64_ext_data_in_ldir = MZ_TRUE;
6523 pExtra_data += field_total_size;
6524 extra_size_remaining -= field_total_size;
6525 }
while (extra_size_remaining);
6530 if (!pState->m_zip64) {
6535 if (approx_new_archive_size >= MZ_UINT32_MAX)
6543 cur_dst_file_ofs += num_alignment_padding_bytes;
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);
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)))))
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);
6566 cur_src_file_ofs += n;
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);
6572 cur_dst_file_ofs += n;
6574 src_archive_bytes_remaining -= n;
6579 if (bit_flags & 8) {
6581 if ((pSource_zip->m_pState->m_zip64) || (found_zip64_ext_data_in_ldir)) {
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);
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);
6606 if (pZip->m_pState->m_zip64) {
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));
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);
6618 n =
sizeof(mz_uint32) * 6;
6621 n =
sizeof(mz_uint32) * (has_id ? 4 : 3);
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);
6630 cur_src_file_ofs += n;
6631 cur_dst_file_ofs += n;
6633 pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf);
6636 orig_central_dir_size = pState->m_central_dir.m_size;
6640 if (pState->m_zip64) {
6684 if (cur_dst_file_ofs > MZ_UINT32_MAX)
6687 if (local_dir_header_ofs >= MZ_UINT32_MAX)
6702 if (pState->m_central_dir.m_size >= MZ_UINT32_MAX) {
6708 n = (mz_uint32)orig_central_dir_size;
6714 pZip->m_total_files++;
6715 pZip->m_archive_size = cur_dst_file_ofs;
6722 mz_zip_internal_state* pState;
6723 mz_uint64 central_dir_ofs, central_dir_size;
6726 if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING))
6729 pState = pZip->m_pState;
6731 if (pState->m_zip64) {
6732 if ((mz_uint64)pState->m_central_dir.m_size >= MZ_UINT32_MAX)
6739 central_dir_ofs = 0;
6740 central_dir_size = 0;
6741 if (pZip->m_total_files) {
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)
6749 pZip->m_archive_size += central_dir_size;
6752 if (pState->m_zip64) {
6754 mz_uint64 rel_ofs_to_zip64_ecdr = pZip->m_archive_size;
6792#ifndef MINIZ_NO_STDIO
6793 if ((pState->m_pFile) && (
MZ_FFLUSH(pState->m_pFile) == EOF))
6799 pZip->m_zip_mode = MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED;
6805 if ((!ppBuf) || (!pSize))
6811 if ((!pZip) || (!pZip->m_pState))
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;
6833#ifndef MINIZ_NO_STDIO
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)
6841 mz_bool status, created_new_archive = MZ_FALSE;
6842 mz_zip_archive zip_archive;
6844 mz_zip_error actual_err = MZ_ZIP_NO_ERROR;
6847 if ((
int)level_and_flags < 0)
6850 if ((!pZip_filename) || (!pArchive_name) || ((buf_size) && (!pBuf)) || ((comment_size) && (!pComment)) || ((level_and_flags & 0xF) >
MZ_UBER_COMPRESSION)) {
6852 *pErr = MZ_ZIP_INVALID_PARAMETER;
6858 *pErr = MZ_ZIP_INVALID_FILENAME;
6868 *pErr = zip_archive.m_last_error;
6872 created_new_archive = MZ_TRUE;
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)) {
6877 *pErr = zip_archive.m_last_error;
6883 *pErr = zip_archive.m_last_error;
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;
6897 actual_err = zip_archive.m_last_error;
6904 actual_err = zip_archive.m_last_error;
6909 if ((!status) && (created_new_archive)) {
6912 (void)ignoredStatus;
6923 mz_uint32 file_index;
6924 mz_zip_archive zip_archive;
6930 if ((!pZip_filename) || (!pArchive_name)) {
6932 *pErr = MZ_ZIP_INVALID_PARAMETER;
6940 *pErr = zip_archive.m_last_error;
6952 *pErr = zip_archive.m_last_error;
6970 return pZip ? pZip->m_zip_mode : MZ_ZIP_MODE_INVALID;
6975 return pZip ? pZip->m_zip_type : MZ_ZIP_TYPE_INVALID;
6980 mz_zip_error prev_err;
6983 return MZ_ZIP_INVALID_PARAMETER;
6985 prev_err = pZip->m_last_error;
6987 pZip->m_last_error = err_num;
6994 return MZ_ZIP_INVALID_PARAMETER;
6996 return pZip->m_last_error;
7006 mz_zip_error prev_err;
7009 return MZ_ZIP_INVALID_PARAMETER;
7011 prev_err = pZip->m_last_error;
7013 pZip->m_last_error = MZ_ZIP_NO_ERROR;
7020 case MZ_ZIP_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";
7090 return "unknown error";
7096 if ((!pZip) || (!pZip->m_pState))
7099 return pZip->m_pState->m_zip64;
7104 if ((!pZip) || (!pZip->m_pState))
7107 return pZip->m_pState->m_central_dir.m_size;
7112 return pZip ? pZip->m_total_files : 0;
7119 return pZip->m_archive_size;
7124 if ((!pZip) || (!pZip->m_pState))
7126 return pZip->m_pState->m_file_archive_start_ofs;
7131 if ((!pZip) || (!pZip->m_pState))
7133 return pZip->m_pState->m_pFile;
7138 if ((!pZip) || (!pZip->m_pState) || (!pBuf) || (!pZip->m_pRead))
7141 return pZip->m_pRead(pZip->m_pIO_opaque, file_ofs, pBuf, n);
7149 if (filename_buf_size)
7150 pFilename[0] =
'\0';
7155 if (filename_buf_size) {
7156 n = MZ_MIN(n, filename_buf_size - 1);
7158 pFilename[n] =
'\0';
7173 if (pZip->m_zip_mode == MZ_ZIP_MODE_READING)
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))
typedef int(ZCALLBACK *close_file_func) OF((voidpf opaque
#define MZ_WRITE_LE32(p, v)
#define TINFL_HUFF_DECODE(state_index, sym, pLookUp, pTree)
mz_ulong mz_adler32(mz_ulong adler, const unsigned char *ptr, size_t buf_len)
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)
mz_bool mz_zip_is_zip64(mz_zip_archive *pZip)
static MZ_FORCEINLINE mz_bool mz_zip_array_ensure_room(mz_zip_archive *pZip, mz_zip_array *pArray, size_t n)
static const mz_uint8 s_tdefl_len_extra[256]
mz_bool mz_zip_reader_end(mz_zip_archive *pZip)
int mz_uncompress2(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong *pSource_len)
mz_bool mz_zip_validate_archive(mz_zip_archive *pZip, mz_uint flags)
mz_bool mz_zip_writer_finalize_archive(mz_zip_archive *pZip)
mz_zip_type mz_zip_get_type(mz_zip_archive *pZip)
static const mz_uint s_tdefl_num_probes[11]
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)
static size_t mz_zip_file_read_func(void *pOpaque, mz_uint64 file_ofs, void *pBuf, size_t n)
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)
static const mz_uint16 s_tdefl_len_sym[256]
mz_bool mz_zip_end(mz_zip_archive *pZip)
static const mz_uint8 s_tdefl_packed_code_size_syms_swizzle[]
static int tdefl_flush_block(tdefl_compressor *d, int flush)
static MZ_FORCEINLINE void mz_write_le32(mz_uint8 *p, mz_uint32 v)
mz_uint mz_zip_reader_get_num_files(mz_zip_archive *pZip)
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)
mz_bool mz_zip_writer_init_file(mz_zip_archive *pZip, const char *pFilename, mz_uint64 size_to_reserve_at_beginning)
#define MZ_WRITE_LE16(p, v)
void * tdefl_compress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, size_t *pOut_len, int flags)
static void mz_zip_reader_sort_central_dir_offsets_by_filename(mz_zip_archive *pZip)
static void tdefl_start_dynamic_block(tdefl_compressor *d)
mz_bool mz_zip_reader_init_file(mz_zip_archive *pZip, const char *pFilename, mz_uint32 flags)
int mz_deflateReset(mz_streamp pStream)
#define TINFL_GET_BYTE(state_index, c)
#define MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(array_ptr, element_size)
static mz_bool mz_zip_writer_end_internal(mz_zip_archive *pZip, mz_bool set_last_error)
size_t mz_zip_get_central_dir_size(mz_zip_archive *pZip)
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)
int mz_inflateInit(mz_streamp pStream)
int mz_inflate(mz_streamp pStream, int flush)
int mz_compress(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len)
mz_uint32 tdefl_get_adler32(tdefl_compressor *d)
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)
#define TDEFL_RLE_ZERO_CODE_SIZE()
mz_ulong mz_compressBound(mz_ulong source_len)
static mz_bool mz_zip_set_file_times(const char *pFilename, MZ_TIME_T access_time, MZ_TIME_T modified_time)
static MZ_FORCEINLINE void mz_write_le16(mz_uint8 *p, mz_uint16 v)
mz_zip_reader_extract_iter_state * mz_zip_reader_extract_iter_new(mz_zip_archive *pZip, mz_uint file_index, mz_uint flags)
static mz_bool mz_zip_reader_locate_header_sig(mz_zip_archive *pZip, mz_uint32 record_sig, mz_uint32 record_size, mz_int64 *pOfs)
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)
int mz_deflateEnd(mz_streamp pStream)
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)
mz_zip_mode mz_zip_get_mode(mz_zip_archive *pZip)
#define MZ_FREOPEN(f, m, s)
static void tdefl_calculate_minimum_redundancy(tdefl_sym_freq *A, int n)
static const mz_uint8 s_tdefl_small_dist_sym[512]
MINIZ_EXPORT void * miniz_def_alloc_func(void *opaque, size_t items, size_t size)
tdefl_status tdefl_init(tdefl_compressor *d, tdefl_put_buf_func_ptr pPut_buf_func, void *pPut_buf_user, int flags)
static mz_bool tdefl_compress_lz_codes(tdefl_compressor *d)
MINIZ_EXPORT void * miniz_def_realloc_func(void *opaque, void *address, size_t items, size_t size)
size_t mz_zip_read_archive_data(mz_zip_archive *pZip, mz_uint64 file_ofs, void *pBuf, size_t n)
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)
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)
static size_t mz_zip_file_write_func(void *pOpaque, mz_uint64 file_ofs, const void *pBuf, size_t n)
mz_bool mz_zip_reader_is_file_a_directory(mz_zip_archive *pZip, mz_uint file_index)
void * tinfl_decompress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, size_t *pOut_len, int flags)
static mz_bool tdefl_output_buffer_putter(const void *pBuf, int len, void *pUser)
static mz_bool mz_zip_get_file_modified_time(const char *pFilename, MZ_TIME_T *pTime)
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)
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)
unsigned char mz_validate_uint32[sizeof(mz_uint32)==4 ? 1 :-1]
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)
#define TINFL_MEMCPY(d, s, l)
#define TINFL_GET_BITS(state_index, b, n)
mz_bool mz_zip_reader_locate_file_v2(mz_zip_archive *pZip, const char *pName, const char *pComment, mz_uint flags, mz_uint32 *pIndex)
static const mz_uint8 s_tdefl_small_dist_extra[512]
tdefl_status tdefl_get_prev_return_status(tdefl_compressor *d)
static void tinfl_clear_tree(tinfl_decompressor *r)
mz_bool mz_zip_writer_init_heap(mz_zip_archive *pZip, size_t size_to_reserve_at_beginning, size_t initial_allocation_size)
static mz_bool mz_zip_reader_init_internal(mz_zip_archive *pZip, mz_uint flags)
static mz_bool mz_zip_reader_end_internal(mz_zip_archive *pZip, mz_bool set_last_error)
mz_zip_error mz_zip_peek_last_error(mz_zip_archive *pZip)
mz_bool mz_zip_reader_init(mz_zip_archive *pZip, mz_uint64 size, mz_uint flags)
mz_zip_error mz_zip_get_last_error(mz_zip_archive *pZip)
mz_bool mz_zip_reader_extract_file_to_file(mz_zip_archive *pZip, const char *pArchive_filename, const char *pDst_filename, mz_uint flags)
tdefl_status tdefl_compress_buffer(tdefl_compressor *d, const void *pIn_buf, size_t in_buf_size, tdefl_flush flush)
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)
mz_bool mz_zip_validate_file_archive(const char *pFilename, mz_uint flags, mz_zip_error *pErr)
static const mz_uint8 s_tdefl_large_dist_extra[128]
int mz_deflateInit2(mz_streamp pStream, int level, int method, int window_bits, int mem_level, int strategy)
mz_bool mz_zip_reader_init_mem(mz_zip_archive *pZip, const void *pMem, size_t size, mz_uint flags)
const char * mz_version(void)
static mz_bool tdefl_compress_normal(tdefl_compressor *d)
int mz_deflateInit(mz_streamp pStream, int level)
static size_t mz_zip_file_write_callback(void *pOpaque, mz_uint64 ofs, const void *pBuf, size_t n)
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)
static MZ_FORCEINLINE void mz_zip_array_init(mz_zip_array *pArray, mz_uint32 element_size)
const char * mz_zip_get_error_string(mz_zip_error mz_err)
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)
void * mz_zip_reader_extract_to_heap(mz_zip_archive *pZip, mz_uint file_index, size_t *pSize, mz_uint flags)
static MZ_FORCEINLINE const mz_uint8 * mz_zip_get_cdh(mz_zip_archive *pZip, mz_uint file_index)
static void tdefl_huffman_enforce_max_code_size(int *pNum_codes, int code_list_len, int max_code_size)
static void tdefl_start_static_block(tdefl_compressor *d)
int mz_compress2(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len, int level)
static mz_bool mz_zip_reader_read_central_dir(mz_zip_archive *pZip, mz_uint flags)
mz_bool mz_zip_writer_end(mz_zip_archive *pZip)
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)
mz_bool mz_zip_writer_init_cfile(mz_zip_archive *pZip, MZ_FILE *pFile, mz_uint flags)
#define TINFL_MEMSET(p, c, l)
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)
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)
static size_t mz_zip_heap_write_func(void *pOpaque, mz_uint64 file_ofs, const void *pBuf, size_t n)
static void mz_zip_time_t_to_dos_time(MZ_TIME_T time, mz_uint16 *pDOS_time, mz_uint16 *pDOS_date)
int mz_inflateReset(mz_streamp pStream)
int mz_zip_reader_locate_file(mz_zip_archive *pZip, const char *pName, const char *pComment, mz_uint flags)
static MZ_FORCEINLINE mz_bool mz_zip_array_push_back(mz_zip_archive *pZip, mz_zip_array *pArray, const void *pElements, size_t n)
static MZ_TIME_T mz_zip_dos_to_time_t(int dos_time, int dos_date)
mz_bool mz_zip_reader_is_file_encrypted(mz_zip_archive *pZip, mz_uint file_index)
mz_bool mz_zip_writer_init(mz_zip_archive *pZip, mz_uint64 existing_size)
const char * mz_error(int err)
mz_ulong mz_crc32(mz_ulong crc, const mz_uint8 *ptr, size_t buf_len)
void mz_zip_zero_struct(mz_zip_archive *pZip)
static MZ_FORCEINLINE void mz_zip_array_clear(mz_zip_archive *pZip, mz_zip_array *pArray)
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)
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)
mz_uint64 mz_zip_get_archive_size(mz_zip_archive *pZip)
size_t mz_zip_reader_extract_iter_read(mz_zip_reader_extract_iter_state *pState, void *pvBuf, size_t buf_size)
MZ_FILE * mz_zip_get_cfile(mz_zip_archive *pZip)
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)
static mz_uint mz_zip_writer_compute_padding_needed_for_file_alignment(mz_zip_archive *pZip)
int mz_inflateInit2(mz_streamp pStream, int window_bits)
static mz_bool mz_zip_writer_add_put_buf_callback(const void *pBuf, int len, void *pUser)
static mz_bool mz_zip_writer_validate_archive_name(const char *pArchive_name)
static size_t mz_zip_mem_read_func(void *pOpaque, mz_uint64 file_ofs, void *pBuf, size_t n)
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)
static MZ_FORCEINLINE mz_bool mz_zip_set_error(mz_zip_archive *pZip, mz_zip_error err_num)
mz_bool mz_zip_writer_add_from_zip_reader(mz_zip_archive *pZip, mz_zip_archive *pSource_zip, mz_uint src_file_index)
mz_bool mz_zip_reader_is_file_supported(mz_zip_archive *pZip, mz_uint file_index)
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)
#define TINFL_CR_RETURN(state_index, result)
#define TINFL_CR_RETURN_FOREVER(state_index, result)
mz_zip_error mz_zip_set_last_error(mz_zip_archive *pZip, mz_zip_error err_num)
MINIZ_EXPORT void miniz_def_free_func(void *opaque, void *address)
tinfl_decompressor * tinfl_decompressor_alloc(void)
int mz_uncompress(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len)
#define MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE
static MZ_FORCEINLINE void tdefl_record_literal(tdefl_compressor *d, mz_uint8 lit)
mz_zip_reader_extract_iter_state * mz_zip_reader_extract_file_iter_new(mz_zip_archive *pZip, const char *pFilename, mz_uint flags)
void * mz_zip_extract_archive_file_to_heap(const char *pZip_filename, const char *pArchive_name, size_t *pSize, mz_uint flags)
static MZ_FORCEINLINE mz_bool mz_zip_array_reserve(mz_zip_archive *pZip, mz_zip_array *pArray, size_t new_capacity, mz_uint growing)
mz_bool mz_zip_reader_file_stat(mz_zip_archive *pZip, mz_uint file_index, mz_zip_archive_file_stat *pStat)
mz_bool mz_zip_writer_init_from_reader_v2(mz_zip_archive *pZip, const char *pFilename, mz_uint flags)
static mz_bool mz_zip_locate_file_binary_search(mz_zip_archive *pZip, const char *pFilename, mz_uint32 *pIndex)
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)
void tdefl_compressor_free(tdefl_compressor *pComp)
mz_uint64 mz_zip_get_archive_file_start_offset(mz_zip_archive *pZip)
unsigned char mz_validate_uint64[sizeof(mz_uint64)==8 ? 1 :-1]
int mz_inflateEnd(mz_streamp pStream)
@ TDEFL_MAX_SUPPORTED_HUFF_CODESIZE
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)
static const mz_uint mz_bitmasks[17]
void * mz_zip_reader_extract_file_to_heap(mz_zip_archive *pZip, const char *pFilename, size_t *pSize, mz_uint flags)
int mz_deflate(mz_streamp pStream, int flush)
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)
#define MZ_FILE_STAT_STRUCT
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)
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)
static mz_bool mz_zip_writer_write_zeros(mz_zip_archive *pZip, mz_uint64 cur_file_ofs, mz_uint32 n)
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)
static MZ_FORCEINLINE mz_bool mz_zip_string_equal(const char *pA, const char *pB, mz_uint len, mz_uint flags)
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)
#define MZ_SWAP_UINT32(a, b)
tdefl_compressor * tdefl_compressor_alloc(void)
static void tdefl_optimize_huffman_table(tdefl_compressor *d, int table_num, int table_len, int code_size_limit, int static_table)
static const mz_uint8 s_tdefl_large_dist_sym[128]
mz_bool mz_zip_validate_mem_archive(const void *pMem, size_t size, mz_uint flags, mz_zip_error *pErr)
mz_bool mz_zip_validate_file(mz_zip_archive *pZip, mz_uint file_index, mz_uint flags)
void * tdefl_write_image_to_png_file_in_memory(const void *pImage, int w, int h, int num_chans, size_t *pLen_out)
mz_ulong mz_deflateBound(mz_streamp pStream, mz_ulong source_len)
mz_uint tdefl_create_comp_flags_from_zip_params(int level, int window_bits, int strategy)
static mz_bool tdefl_compress_block(tdefl_compressor *d, mz_bool static_block)
static size_t mz_zip_compute_crc32_callback(void *pOpaque, mz_uint64 file_ofs, const void *pBuf, size_t n)
static MZ_FORCEINLINE void tdefl_record_match(tdefl_compressor *d, mz_uint match_len, mz_uint match_dist)
#define MZ_ZIP_ARRAY_ELEMENT(array_ptr, element_type, index)
mz_bool mz_zip_reader_extract_iter_free(mz_zip_reader_extract_iter_state *pState)
mz_bool mz_zip_reader_extract_to_cfile(mz_zip_archive *pZip, mz_uint file_index, MZ_FILE *pFile, mz_uint flags)
mz_bool mz_zip_reader_init_cfile(mz_zip_archive *pZip, MZ_FILE *pFile, mz_uint64 archive_size, mz_uint flags)
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)
mz_bool mz_zip_writer_finalize_heap_archive(mz_zip_archive *pZip, void **ppBuf, size_t *pSize)
unsigned char mz_validate_uint16[sizeof(mz_uint16)==2 ? 1 :-1]
static MZ_FORCEINLINE void mz_write_le64(mz_uint8 *p, mz_uint64 v)
@ MZ_ZIP64_ECDH_VERSION_MADE_BY_OFS
@ MZ_ZIP_CDH_FILENAME_LEN_OFS
@ MZ_ZIP_LDH_BIT_FLAG_HAS_LOCATOR
@ MZ_ZIP_LOCAL_DIR_HEADER_SIG
@ MZ_ZIP_CDH_FILE_DATE_OFS
@ MZ_ZIP_LDH_BIT_FLAG_OFS
@ MZ_ZIP_ECDH_NUM_DISK_CDIR_OFS
@ MZ_ZIP_CDH_VERSION_NEEDED_OFS
@ MZ_ZIP_DATA_DESCRIPTER_SIZE64
@ MZ_ZIP_CDH_LOCAL_HEADER_OFS
@ MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIG
@ MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_UTF8
@ MZ_ZIP64_ECDH_NUM_DISK_CDIR_OFS
@ MZ_ZIP_CDH_COMPRESSED_SIZE_OFS
@ MZ_ZIP_DATA_DESCRIPTER_SIZE32
@ MZ_ZIP_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS
@ MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_COMPRESSED_PATCH_FLAG
@ MZ_ZIP64_ECDL_NUM_DISK_CDIR_OFS
@ MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS
@ MZ_ZIP_ECDH_CDIR_SIZE_OFS
@ MZ_ZIP_LDH_FILENAME_LEN_OFS
@ MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE
@ MZ_ZIP_DOS_DIR_ATTRIBUTE_BITFLAG
@ MZ_ZIP_LDH_FILE_TIME_OFS
@ MZ_ZIP64_ECDH_VERSION_NEEDED_OFS
@ MZ_ZIP_CDH_EXTRA_LEN_OFS
@ MZ_ZIP_LDH_FILE_DATE_OFS
@ MZ_ZIP64_ECDL_REL_OFS_TO_ZIP64_ECDR_OFS
@ MZ_ZIP64_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS
@ MZ_ZIP_LDH_COMPRESSED_SIZE_OFS
@ MZ_ZIP_CDH_FILE_TIME_OFS
@ MZ_ZIP_VERSION_MADE_BY_DOS_FILESYSTEM_ID
@ MZ_ZIP_ECDH_CDIR_TOTAL_ENTRIES_OFS
@ MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID
@ MZ_ZIP_CENTRAL_DIR_HEADER_SIG
@ MZ_ZIP_ECDH_COMMENT_SIZE_OFS
@ MZ_ZIP64_ECDH_CDIR_SIZE_OFS
@ MZ_ZIP64_ECDL_TOTAL_NUMBER_OF_DISKS_OFS
@ MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_USES_STRONG_ENCRYPTION
@ MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIG
@ MZ_ZIP_CDH_DISK_START_OFS
@ MZ_ZIP64_ECDH_CDIR_TOTAL_ENTRIES_OFS
@ MZ_ZIP_CDH_INTERNAL_ATTR_OFS
@ MZ_ZIP_LDH_VERSION_NEEDED_OFS
@ MZ_ZIP_ECDH_CDIR_OFS_OFS
@ MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIG
@ MZ_ZIP_LDH_EXTRA_LEN_OFS
@ MZ_ZIP_CDH_EXTERNAL_ATTR_OFS
@ MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_LOCAL_DIR_IS_MASKED
@ MZ_ZIP64_ECDH_NUM_THIS_DISK_OFS
@ MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE
@ MZ_ZIP_LOCAL_DIR_HEADER_SIZE
@ MZ_ZIP64_ECDH_CDIR_OFS_OFS
@ MZ_ZIP_LDH_DECOMPRESSED_SIZE_OFS
@ MZ_ZIP_CDH_BIT_FLAG_OFS
@ MZ_ZIP_CDH_COMMENT_LEN_OFS
@ MZ_ZIP_CENTRAL_DIR_HEADER_SIZE
@ MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE
@ MZ_ZIP_DATA_DESCRIPTOR_ID
@ MZ_ZIP_CDH_VERSION_MADE_BY_OFS
@ MZ_ZIP_ECDH_NUM_THIS_DISK_OFS
@ MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_IS_ENCRYPTED
@ MZ_ZIP64_ECDH_SIZE_OF_RECORD_OFS
mz_bool mz_zip_reader_extract_file_to_cfile(mz_zip_archive *pZip, const char *pArchive_filename, MZ_FILE *pFile, mz_uint flags)
#define MZ_WRITE_LE64(p, v)
mz_bool mz_zip_writer_init_v2(mz_zip_archive *pZip, mz_uint64 existing_size, mz_uint flags)
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)
mz_bool mz_zip_writer_init_from_reader(mz_zip_archive *pZip, const char *pFilename)
static mz_bool mz_zip_array_ensure_capacity(mz_zip_archive *pZip, mz_zip_array *pArray, size_t min_new_capacity, mz_uint growing)
mz_zip_error mz_zip_clear_last_error(mz_zip_archive *pZip)
#define TINFL_SKIP_BITS(state_index, n)
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)
static MZ_FORCEINLINE mz_bool mz_zip_array_resize(mz_zip_archive *pZip, mz_zip_array *pArray, size_t new_size, mz_uint growing)
#define TDEFL_RLE_PREV_CODE_SIZE()
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)
mz_bool mz_zip_reader_extract_to_file(mz_zip_archive *pZip, mz_uint file_index, const char *pDst_filename, mz_uint flags)
mz_uint mz_zip_reader_get_filename(mz_zip_archive *pZip, mz_uint file_index, char *pFilename, mz_uint filename_buf_size)
static tdefl_status tdefl_flush_output_buffer(tdefl_compressor *d)
void tinfl_decompressor_free(tinfl_decompressor *pDecomp)
#define TDEFL_PUT_BITS(b, l)
static size_t mz_file_read_func_stdio(void *pOpaque, mz_uint64 file_ofs, void *pBuf, size_t n)
static tdefl_sym_freq * tdefl_radix_sort_syms(mz_uint num_syms, tdefl_sym_freq *pSyms0, tdefl_sym_freq *pSyms1)
struct mz_stream_s mz_stream
#define MZ_DEFAULT_WINDOW_BITS
mz_uint8 m_dict[TINFL_LZ_DICT_SIZE]
tinfl_status m_last_status
tinfl_decompressor m_decomp
struct mz_internal_state * state
const unsigned char * next_in
mz_bool m_zip64_has_extended_info_fields
mz_zip_array m_sorted_central_dir_offsets
mz_uint64 m_file_archive_start_ofs
mz_zip_array m_central_dir_offsets
mz_zip_array m_central_dir
mz_uint64 m_cur_archive_file_ofs