1 /**************************************************************************
3 * Copyright 2013-2014 RAD Game Tools and Valve Software
4 * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 **************************************************************************/
27 // File: vogl_miniz.cpp
28 #include "vogl_core.h"
29 #include "vogl_miniz.h"
31 typedef unsigned char mz_validate_uint16[sizeof(mz_uint16) == 2 ? 1 : -1];
32 typedef unsigned char mz_validate_uint32[sizeof(mz_uint32) == 4 ? 1 : -1];
33 typedef unsigned char mz_validate_uint64[sizeof(mz_uint64) == 8 ? 1 : -1];
39 // ------------------- zlib-style API's
41 mz_ulong mz_adler32(mz_ulong adler, const unsigned char *ptr, size_t buf_len)
43 mz_uint32 i, s1 = (mz_uint32)(adler & 0xffff), s2 = (mz_uint32)(adler >> 16);
44 size_t block_len = buf_len % 5552;
46 return MZ_ADLER32_INIT;
49 for (i = 0; i + 7 < block_len; i += 8, ptr += 8)
51 s1 += ptr[0], s2 += s1;
52 s1 += ptr[1], s2 += s1;
53 s1 += ptr[2], s2 += s1;
54 s1 += ptr[3], s2 += s1;
55 s1 += ptr[4], s2 += s1;
56 s1 += ptr[5], s2 += s1;
57 s1 += ptr[6], s2 += s1;
58 s1 += ptr[7], s2 += s1;
60 for (; i < block_len; ++i)
61 s1 += *ptr++, s2 += s1;
62 s1 %= 65521U, s2 %= 65521U;
66 return (s2 << 16) + s1;
69 // Karl Malbrain's compact CRC-32. See "A compact CCITT crc16 and crc32 C implementation that balances processor cache usage against speed": http://www.geocities.com/malbrain/
71 mz_ulong mz_crc32(mz_ulong crc, const mz_uint8 *ptr, size_t buf_len)
73 static const mz_uint32 s_crc32[16] =
75 0, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
76 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
78 mz_uint32 crcu32 = (mz_uint32)crc;
79 if (!ptr) return MZ_CRC32_INIT;
84 crcu32 = (crcu32 >> 4) ^ s_crc32[(crcu32 & 0xF) ^ (b & 0xF)];
85 crcu32 = (crcu32 >> 4) ^ s_crc32[(crcu32 & 0xF) ^ (b >> 4)];
90 // Faster, but larger CPU cache footprint.
91 mz_ulong mz_crc32(mz_ulong crc, const mz_uint8 *ptr, size_t buf_len)
93 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;
139 crc32 = (crc32 >> 8) ^ s_crc_table[(crc32 ^ pByte_buf[0]) & 0xFF];
140 crc32 = (crc32 >> 8) ^ s_crc_table[(crc32 ^ pByte_buf[1]) & 0xFF];
141 crc32 = (crc32 >> 8) ^ s_crc_table[(crc32 ^ pByte_buf[2]) & 0xFF];
142 crc32 = (crc32 >> 8) ^ s_crc_table[(crc32 ^ pByte_buf[3]) & 0xFF];
149 crc32 = (crc32 >> 8) ^ s_crc_table[(crc32 ^ pByte_buf[0]) & 0xFF];
158 void mz_free(void *p)
163 #ifndef MINIZ_NO_ZLIB_APIS
165 void *miniz_def_alloc_func(void *opaque, size_t items, size_t size)
167 (void)opaque, (void)items, (void)size;
168 return MZ_MALLOC(items * size);
170 void miniz_def_free_func(void *opaque, void *address)
172 (void)opaque, (void)address;
175 void *miniz_def_realloc_func(void *opaque, void *address, size_t items, size_t size)
177 (void)opaque, (void)address, (void)items, (void)size;
178 return MZ_REALLOC(address, items * size);
181 const char *mz_version(void)
186 int mz_deflateInit(mz_streamp pStream, int level)
188 return mz_deflateInit2(pStream, level, MZ_DEFLATED, MZ_DEFAULT_WINDOW_BITS, 9, MZ_DEFAULT_STRATEGY);
191 int mz_deflateInit2(mz_streamp pStream, int level, int method, int window_bits, int mem_level, int strategy)
193 tdefl_compressor *pComp;
194 mz_uint comp_flags = TDEFL_COMPUTE_ADLER32 | tdefl_create_comp_flags_from_zip_params(level, window_bits, strategy);
197 return MZ_STREAM_ERROR;
198 if ((method != MZ_DEFLATED) || ((mem_level < 1) || (mem_level > 9)) || ((window_bits != MZ_DEFAULT_WINDOW_BITS) && (-window_bits != MZ_DEFAULT_WINDOW_BITS)))
199 return MZ_PARAM_ERROR;
201 pStream->data_type = 0;
202 pStream->adler = MZ_ADLER32_INIT;
204 pStream->reserved = 0;
205 pStream->total_in = 0;
206 pStream->total_out = 0;
207 if (!pStream->zalloc)
208 pStream->zalloc = miniz_def_alloc_func;
210 pStream->zfree = miniz_def_free_func;
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)
220 mz_deflateEnd(pStream);
221 return MZ_PARAM_ERROR;
227 int mz_deflateReset(mz_streamp pStream)
229 if ((!pStream) || (!pStream->state) || (!pStream->zalloc) || (!pStream->zfree))
230 return MZ_STREAM_ERROR;
231 pStream->total_in = pStream->total_out = 0;
232 tdefl_init((tdefl_compressor *)pStream->state, NULL, NULL, ((tdefl_compressor *)pStream->state)->m_flags);
236 int mz_deflate(mz_streamp pStream, int flush)
238 size_t in_bytes, out_bytes;
239 mz_ulong orig_total_in, orig_total_out;
240 int mz_status = MZ_OK;
242 if ((!pStream) || (!pStream->state) || (flush < 0) || (flush > MZ_FINISH) || (!pStream->next_out))
243 return MZ_STREAM_ERROR;
244 if (!pStream->avail_out)
247 if (flush == MZ_PARTIAL_FLUSH)
248 flush = MZ_SYNC_FLUSH;
250 if (((tdefl_compressor *)pStream->state)->m_prev_return_status == TDEFL_STATUS_DONE)
251 return (flush == MZ_FINISH) ? MZ_STREAM_END : MZ_BUF_ERROR;
253 orig_total_in = pStream->total_in;
254 orig_total_out = pStream->total_out;
257 tdefl_status defl_status;
258 in_bytes = pStream->avail_in;
259 out_bytes = pStream->avail_out;
261 defl_status = tdefl_compress((tdefl_compressor *)pStream->state, pStream->next_in, &in_bytes, pStream->next_out, &out_bytes, (tdefl_flush)flush);
262 pStream->next_in += (mz_uint)in_bytes;
263 pStream->avail_in -= (mz_uint)in_bytes;
264 pStream->total_in += (mz_uint)in_bytes;
265 pStream->adler = tdefl_get_adler32((tdefl_compressor *)pStream->state);
267 pStream->next_out += (mz_uint)out_bytes;
268 pStream->avail_out -= (mz_uint)out_bytes;
269 pStream->total_out += (mz_uint)out_bytes;
273 mz_status = MZ_STREAM_ERROR;
276 else if (defl_status == TDEFL_STATUS_DONE)
278 mz_status = MZ_STREAM_END;
281 else if (!pStream->avail_out)
283 else if ((!pStream->avail_in) && (flush != MZ_FINISH))
285 if ((flush) || (pStream->total_in != orig_total_in) || (pStream->total_out != orig_total_out))
287 return MZ_BUF_ERROR; // Can't make forward progress without some input.
293 int mz_deflateEnd(mz_streamp pStream)
296 return MZ_STREAM_ERROR;
299 pStream->zfree(pStream->opaque, pStream->state);
300 pStream->state = NULL;
305 mz_ulong mz_deflateBound(mz_streamp pStream, mz_ulong source_len)
308 // This is really over conservative. (And lame, but it's actually pretty tricky to compute a true upper bound given the way tdefl's blocking works.)
309 return MZ_MAX(128 + (source_len * 110) / 100, 128 + source_len + ((source_len / (31 * 1024)) + 1) * 5);
312 int mz_compress2(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len, int level)
316 memset(&stream, 0, sizeof(stream));
318 // In case mz_ulong is 64-bits (argh I hate longs).
319 if ((source_len | *pDest_len) > 0xFFFFFFFFU)
320 return MZ_PARAM_ERROR;
322 stream.next_in = pSource;
323 stream.avail_in = (mz_uint32)source_len;
324 stream.next_out = pDest;
325 stream.avail_out = (mz_uint32) * pDest_len;
327 status = mz_deflateInit(&stream, level);
331 status = mz_deflate(&stream, MZ_FINISH);
332 if (status != MZ_STREAM_END)
334 mz_deflateEnd(&stream);
335 return (status == MZ_OK) ? MZ_BUF_ERROR : status;
338 *pDest_len = stream.total_out;
339 return mz_deflateEnd(&stream);
342 int mz_compress(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len)
344 return mz_compress2(pDest, pDest_len, pSource, source_len, MZ_DEFAULT_COMPRESSION);
347 mz_ulong mz_compressBound(mz_ulong source_len)
349 return mz_deflateBound(NULL, source_len);
354 tinfl_decompressor m_decomp;
355 mz_uint m_dict_ofs, m_dict_avail, m_first_call, m_has_flushed;
357 mz_uint8 m_dict[TINFL_LZ_DICT_SIZE];
358 tinfl_status m_last_status;
361 int mz_inflateInit2(mz_streamp pStream, int window_bits)
363 inflate_state *pDecomp;
365 return MZ_STREAM_ERROR;
366 if ((window_bits != MZ_DEFAULT_WINDOW_BITS) && (-window_bits != MZ_DEFAULT_WINDOW_BITS))
367 return MZ_PARAM_ERROR;
369 pStream->data_type = 0;
372 pStream->total_in = 0;
373 pStream->total_out = 0;
374 pStream->reserved = 0;
375 if (!pStream->zalloc)
376 pStream->zalloc = miniz_def_alloc_func;
378 pStream->zfree = miniz_def_free_func;
380 pDecomp = (inflate_state *)pStream->zalloc(pStream->opaque, 1, sizeof(inflate_state));
384 pStream->state = (struct mz_internal_state *)pDecomp;
386 tinfl_init(&pDecomp->m_decomp);
387 pDecomp->m_dict_ofs = 0;
388 pDecomp->m_dict_avail = 0;
389 pDecomp->m_last_status = TINFL_STATUS_NEEDS_MORE_INPUT;
390 pDecomp->m_first_call = 1;
391 pDecomp->m_has_flushed = 0;
392 pDecomp->m_window_bits = window_bits;
397 int mz_inflateInit(mz_streamp pStream)
399 return mz_inflateInit2(pStream, MZ_DEFAULT_WINDOW_BITS);
402 int mz_inflate(mz_streamp pStream, int flush)
404 inflate_state *pState;
405 mz_uint n, first_call, decomp_flags = TINFL_FLAG_COMPUTE_ADLER32;
406 size_t in_bytes, out_bytes, orig_avail_in;
409 if ((!pStream) || (!pStream->state))
410 return MZ_STREAM_ERROR;
411 if (flush == MZ_PARTIAL_FLUSH)
412 flush = MZ_SYNC_FLUSH;
413 if ((flush) && (flush != MZ_SYNC_FLUSH) && (flush != MZ_FINISH))
414 return MZ_STREAM_ERROR;
416 pState = (inflate_state *)pStream->state;
417 if (pState->m_window_bits > 0)
418 decomp_flags |= TINFL_FLAG_PARSE_ZLIB_HEADER;
419 orig_avail_in = pStream->avail_in;
421 first_call = pState->m_first_call;
422 pState->m_first_call = 0;
423 if (pState->m_last_status < 0)
424 return MZ_DATA_ERROR;
426 if (pState->m_has_flushed && (flush != MZ_FINISH))
427 return MZ_STREAM_ERROR;
428 pState->m_has_flushed |= (flush == MZ_FINISH);
430 if ((flush == MZ_FINISH) && (first_call))
432 // MZ_FINISH on the first call implies that the input and output buffers are large enough to hold the entire compressed/decompressed file.
433 decomp_flags |= TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF;
434 in_bytes = pStream->avail_in;
435 out_bytes = pStream->avail_out;
436 status = tinfl_decompress(&pState->m_decomp, pStream->next_in, &in_bytes, pStream->next_out, pStream->next_out, &out_bytes, decomp_flags);
437 pState->m_last_status = status;
438 pStream->next_in += (mz_uint)in_bytes;
439 pStream->avail_in -= (mz_uint)in_bytes;
440 pStream->total_in += (mz_uint)in_bytes;
441 pStream->adler = tinfl_get_adler32(&pState->m_decomp);
442 pStream->next_out += (mz_uint)out_bytes;
443 pStream->avail_out -= (mz_uint)out_bytes;
444 pStream->total_out += (mz_uint)out_bytes;
447 return MZ_DATA_ERROR;
448 else if (status != TINFL_STATUS_DONE)
450 pState->m_last_status = TINFL_STATUS_FAILED;
453 return MZ_STREAM_END;
455 // flush != MZ_FINISH then we must assume there's more input.
456 if (flush != MZ_FINISH)
457 decomp_flags |= TINFL_FLAG_HAS_MORE_INPUT;
459 if (pState->m_dict_avail)
461 n = MZ_MIN(pState->m_dict_avail, pStream->avail_out);
462 memcpy(pStream->next_out, pState->m_dict + pState->m_dict_ofs, n);
463 pStream->next_out += n;
464 pStream->avail_out -= n;
465 pStream->total_out += n;
466 pState->m_dict_avail -= n;
467 pState->m_dict_ofs = (pState->m_dict_ofs + n) & (TINFL_LZ_DICT_SIZE - 1);
468 return ((pState->m_last_status == TINFL_STATUS_DONE) && (!pState->m_dict_avail)) ? MZ_STREAM_END : MZ_OK;
473 in_bytes = pStream->avail_in;
474 out_bytes = TINFL_LZ_DICT_SIZE - pState->m_dict_ofs;
476 status = tinfl_decompress(&pState->m_decomp, pStream->next_in, &in_bytes, pState->m_dict, pState->m_dict + pState->m_dict_ofs, &out_bytes, decomp_flags);
477 pState->m_last_status = status;
479 pStream->next_in += (mz_uint)in_bytes;
480 pStream->avail_in -= (mz_uint)in_bytes;
481 pStream->total_in += (mz_uint)in_bytes;
482 pStream->adler = tinfl_get_adler32(&pState->m_decomp);
484 pState->m_dict_avail = (mz_uint)out_bytes;
486 n = MZ_MIN(pState->m_dict_avail, pStream->avail_out);
487 memcpy(pStream->next_out, pState->m_dict + pState->m_dict_ofs, n);
488 pStream->next_out += n;
489 pStream->avail_out -= n;
490 pStream->total_out += n;
491 pState->m_dict_avail -= n;
492 pState->m_dict_ofs = (pState->m_dict_ofs + n) & (TINFL_LZ_DICT_SIZE - 1);
495 return MZ_DATA_ERROR; // Stream is corrupted (there could be some uncompressed data left in the output dictionary - oh well).
496 else if ((status == TINFL_STATUS_NEEDS_MORE_INPUT) && (!orig_avail_in))
497 return MZ_BUF_ERROR; // Signal caller that we can't make forward progress without supplying more input or by setting flush to MZ_FINISH.
498 else if (flush == MZ_FINISH)
500 // The output buffer MUST be large to hold the remaining uncompressed data when flush==MZ_FINISH.
501 if (status == TINFL_STATUS_DONE)
502 return pState->m_dict_avail ? MZ_BUF_ERROR : MZ_STREAM_END;
503 // status here must be TINFL_STATUS_HAS_MORE_OUTPUT, which means there's at least 1 more byte on the way. If there's no more room left in the output buffer then something is wrong.
504 else if (!pStream->avail_out)
507 else if ((status == TINFL_STATUS_DONE) || (!pStream->avail_in) || (!pStream->avail_out) || (pState->m_dict_avail))
511 return ((status == TINFL_STATUS_DONE) && (!pState->m_dict_avail)) ? MZ_STREAM_END : MZ_OK;
514 int mz_inflateEnd(mz_streamp pStream)
517 return MZ_STREAM_ERROR;
520 pStream->zfree(pStream->opaque, pStream->state);
521 pStream->state = NULL;
526 int mz_uncompress(unsigned char *pDest, mz_ulong *pDest_len, const unsigned char *pSource, mz_ulong source_len)
530 memset(&stream, 0, sizeof(stream));
532 // In case mz_ulong is 64-bits (argh I hate longs).
533 if ((source_len | *pDest_len) > 0xFFFFFFFFU)
534 return MZ_PARAM_ERROR;
536 stream.next_in = pSource;
537 stream.avail_in = (mz_uint32)source_len;
538 stream.next_out = pDest;
539 stream.avail_out = (mz_uint32) * pDest_len;
541 status = mz_inflateInit(&stream);
545 status = mz_inflate(&stream, MZ_FINISH);
546 if (status != MZ_STREAM_END)
548 mz_inflateEnd(&stream);
549 return ((status == MZ_BUF_ERROR) && (!stream.avail_in)) ? MZ_DATA_ERROR : status;
551 *pDest_len = stream.total_out;
553 return mz_inflateEnd(&stream);
556 const char *mz_error(int err)
564 { MZ_OK, "" }, { MZ_STREAM_END, "stream end" }, { MZ_NEED_DICT, "need dictionary" }, { MZ_ERRNO, "file error" }, { MZ_STREAM_ERROR, "stream error" },
565 { 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" }
568 for (i = 0; i < sizeof(s_error_descs) / sizeof(s_error_descs[0]); ++i)
569 if (s_error_descs[i].m_err == err)
570 return s_error_descs[i].m_pDesc;
574 #endif //MINIZ_NO_ZLIB_APIS
576 // ------------------- Low-level Decompression (completely independent from all compression API's)
578 #define TINFL_MEMCPY(d, s, l) memcpy(d, s, l)
579 #define TINFL_MEMSET(p, c, l) memset(p, c, l)
581 #define TINFL_CR_BEGIN \
582 switch (r->m_state) \
585 #define TINFL_CR_RETURN(state_index, result) \
589 r->m_state = state_index; \
595 #define TINFL_CR_RETURN_FOREVER(state_index, result) \
600 TINFL_CR_RETURN(state_index, result); \
604 #define TINFL_CR_FINISH }
606 #define TINFL_GET_BYTE(state_index, c) \
609 while (pIn_buf_cur >= pIn_buf_end) \
611 TINFL_CR_RETURN(state_index, (decomp_flags &TINFL_FLAG_HAS_MORE_INPUT) ? TINFL_STATUS_NEEDS_MORE_INPUT : TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS); \
613 c = *pIn_buf_cur++; \
617 #define TINFL_NEED_BITS(state_index, n) \
621 TINFL_GET_BYTE(state_index, c); \
622 bit_buf |= (((tinfl_bit_buf_t)c) << num_bits); \
624 } while (num_bits < (mz_uint)(n))
625 #define TINFL_SKIP_BITS(state_index, n) \
628 if (num_bits < (mz_uint)(n)) \
630 TINFL_NEED_BITS(state_index, n); \
636 #define TINFL_GET_BITS(state_index, b, n) \
639 if (num_bits < (mz_uint)(n)) \
641 TINFL_NEED_BITS(state_index, n); \
643 b = bit_buf & ((1 << (n)) - 1); \
649 // TINFL_HUFF_BITBUF_FILL() is only used rarely, when the number of bytes remaining in the input buffer falls below 2.
650 // It reads just enough bytes from the input stream that are needed to decode the next Huffman code (and absolutely no more). It works by trying to fully decode a
651 // Huffman code by using whatever bits are currently present in the bit buffer. If this fails, it reads another byte, and tries again until it succeeds or until the
652 // bit buffer contains >=15 bits (deflate's max. Huffman code size).
653 #define TINFL_HUFF_BITBUF_FILL(state_index, pHuff) \
656 temp = (pHuff)->m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]; \
659 code_len = temp >> 9; \
660 if ((code_len) && (num_bits >= code_len)) \
663 else if (num_bits > TINFL_FAST_LOOKUP_BITS) \
665 code_len = TINFL_FAST_LOOKUP_BITS; \
668 temp = (pHuff)->m_tree[~temp + ((bit_buf >> code_len++) & 1)]; \
669 } while ((temp < 0) && (num_bits >= (code_len + 1))); \
673 TINFL_GET_BYTE(state_index, c); \
674 bit_buf |= (((tinfl_bit_buf_t)c) << num_bits); \
676 } while (num_bits < 15);
678 // TINFL_HUFF_DECODE() decodes the next Huffman coded symbol. It's more complex than you would initially expect because the zlib API expects the decompressor to never read
679 // beyond the final byte of the deflate stream. (In other words, when this macro wants to read another byte from the input, it REALLY needs another byte in order to fully
680 // decode the next Huffman code.) Handling this properly is particularly important on raw deflate (non-zlib) streams, which aren't followed by a byte aligned adler-32.
681 // The slow path is only executed at the very end of the input buffer.
682 // v1.16: The original macro handled the case at the very end of the passed-in input buffer, but we also need to handle the case where the user passes in 1+zillion bytes
683 // following the deflate data and our non-conservative read-ahead path won't kick in here on this code. This is much trickier.
684 #define TINFL_HUFF_DECODE(state_index, sym, pHuff) \
688 mz_uint code_len, c; \
691 if ((pIn_buf_end - pIn_buf_cur) < 2) \
693 TINFL_HUFF_BITBUF_FILL(state_index, pHuff); \
697 bit_buf |= (((tinfl_bit_buf_t)pIn_buf_cur[0]) << num_bits) | (((tinfl_bit_buf_t)pIn_buf_cur[1]) << (num_bits + 8)); \
702 if ((temp = (pHuff)->m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0) \
703 code_len = temp >> 9, temp &= 511; \
706 code_len = TINFL_FAST_LOOKUP_BITS; \
709 temp = (pHuff)->m_tree[~temp + ((bit_buf >> code_len++) & 1)]; \
710 } while (temp < 0); \
713 bit_buf >>= code_len; \
714 num_bits -= code_len; \
718 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)
720 static const int 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 };
721 static const int 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 };
722 static const int 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 };
723 static const int 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 };
724 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 };
725 static const int s_min_table_sizes[3] = { 257, 1, 4 };
727 tinfl_status status = TINFL_STATUS_FAILED;
728 mz_uint32 num_bits, dist, counter, num_extra;
729 tinfl_bit_buf_t bit_buf;
730 const mz_uint8 *pIn_buf_cur = pIn_buf_next, *const pIn_buf_end = pIn_buf_next + *pIn_buf_size;
731 mz_uint8 *pOut_buf_cur = pOut_buf_next, *const pOut_buf_end = pOut_buf_next + *pOut_buf_size;
732 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;
734 // Ensure the output buffer's size is a power of 2, unless the output buffer is large enough to hold the entire output file (in which case it doesn't matter).
735 if (((out_buf_size_mask + 1) & out_buf_size_mask) || (pOut_buf_next < pOut_buf_start))
737 *pIn_buf_size = *pOut_buf_size = 0;
738 return TINFL_STATUS_BAD_PARAM;
741 num_bits = r->m_num_bits;
742 bit_buf = r->m_bit_buf;
744 counter = r->m_counter;
745 num_extra = r->m_num_extra;
746 dist_from_out_buf_start = r->m_dist_from_out_buf_start;
749 bit_buf = num_bits = dist = counter = num_extra = r->m_zhdr0 = r->m_zhdr1 = 0;
750 r->m_z_adler32 = r->m_check_adler32 = 1;
751 if (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER)
753 TINFL_GET_BYTE(1, r->m_zhdr0);
754 TINFL_GET_BYTE(2, r->m_zhdr1);
755 counter = (((r->m_zhdr0 * 256 + r->m_zhdr1) % 31 != 0) || (r->m_zhdr1 & 32) || ((r->m_zhdr0 & 15) != 8));
756 if (!(decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF))
757 counter |= (((1U << (8U + (r->m_zhdr0 >> 4))) > 32768U) || ((out_buf_size_mask + 1) < (size_t)(1U << (8U + (r->m_zhdr0 >> 4)))));
760 TINFL_CR_RETURN_FOREVER(36, TINFL_STATUS_FAILED);
766 TINFL_GET_BITS(3, r->m_final, 3);
767 r->m_type = r->m_final >> 1;
770 TINFL_SKIP_BITS(5, num_bits & 7);
771 for (counter = 0; counter < 4; ++counter)
774 TINFL_GET_BITS(6, r->m_raw_header[counter], 8);
776 TINFL_GET_BYTE(7, r->m_raw_header[counter]);
778 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))))
780 TINFL_CR_RETURN_FOREVER(39, TINFL_STATUS_FAILED);
782 while ((counter) && (num_bits))
784 TINFL_GET_BITS(51, dist, 8);
785 while (pOut_buf_cur >= pOut_buf_end)
787 TINFL_CR_RETURN(52, TINFL_STATUS_HAS_MORE_OUTPUT);
789 *pOut_buf_cur++ = (mz_uint8)dist;
795 while (pOut_buf_cur >= pOut_buf_end)
797 TINFL_CR_RETURN(9, TINFL_STATUS_HAS_MORE_OUTPUT);
799 while (pIn_buf_cur >= pIn_buf_end)
801 TINFL_CR_RETURN(38, (decomp_flags & TINFL_FLAG_HAS_MORE_INPUT) ? TINFL_STATUS_NEEDS_MORE_INPUT : TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS);
803 n = MZ_MIN(MZ_MIN((size_t)(pOut_buf_end - pOut_buf_cur), (size_t)(pIn_buf_end - pIn_buf_cur)), counter);
804 TINFL_MEMCPY(pOut_buf_cur, pIn_buf_cur, n);
807 counter -= (mz_uint)n;
810 else if (r->m_type == 3)
812 TINFL_CR_RETURN_FOREVER(10, TINFL_STATUS_FAILED);
818 mz_uint8 *p = r->m_tables[0].m_code_size;
820 r->m_table_sizes[0] = 288;
821 r->m_table_sizes[1] = 32;
822 TINFL_MEMSET(r->m_tables[1].m_code_size, 5, 32);
823 for (i = 0; i <= 143; ++i)
825 for (; i <= 255; ++i)
827 for (; i <= 279; ++i)
829 for (; i <= 287; ++i)
834 for (counter = 0; counter < 3; counter++)
836 TINFL_GET_BITS(11, r->m_table_sizes[counter], "\05\05\04"[counter]);
837 r->m_table_sizes[counter] += s_min_table_sizes[counter];
839 MZ_CLEAR_OBJ(r->m_tables[2].m_code_size);
840 for (counter = 0; counter < r->m_table_sizes[2]; counter++)
843 TINFL_GET_BITS(14, s, 3);
844 r->m_tables[2].m_code_size[s_length_dezigzag[counter]] = (mz_uint8)s;
846 r->m_table_sizes[2] = 19;
848 for (; (int)r->m_type >= 0; r->m_type--)
850 int tree_next, tree_cur;
851 tinfl_huff_table *pTable;
852 mz_uint i, j, used_syms, total, sym_index, next_code[17], total_syms[16];
853 pTable = &r->m_tables[r->m_type];
854 MZ_CLEAR_OBJ(total_syms);
855 MZ_CLEAR_OBJ(pTable->m_look_up);
856 MZ_CLEAR_OBJ(pTable->m_tree);
857 for (i = 0; i < r->m_table_sizes[r->m_type]; ++i)
858 total_syms[pTable->m_code_size[i]]++;
859 used_syms = 0, total = 0;
860 next_code[0] = next_code[1] = 0;
861 for (i = 1; i <= 15; ++i)
863 used_syms += total_syms[i];
864 next_code[i + 1] = (total = ((total + total_syms[i]) << 1));
866 if ((65536 != total) && (used_syms > 1))
868 TINFL_CR_RETURN_FOREVER(35, TINFL_STATUS_FAILED);
870 for (tree_next = -1, sym_index = 0; sym_index < r->m_table_sizes[r->m_type]; ++sym_index)
872 mz_uint rev_code = 0, l, cur_code, code_size = pTable->m_code_size[sym_index];
875 cur_code = next_code[code_size]++;
876 for (l = code_size; l > 0; l--, cur_code >>= 1)
877 rev_code = (rev_code << 1) | (cur_code & 1);
878 if (code_size <= TINFL_FAST_LOOKUP_BITS)
880 mz_int16 k = (mz_int16)((code_size << 9) | sym_index);
881 while (rev_code < TINFL_FAST_LOOKUP_SIZE)
883 pTable->m_look_up[rev_code] = k;
884 rev_code += (1 << code_size);
888 if (0 == (tree_cur = pTable->m_look_up[rev_code & (TINFL_FAST_LOOKUP_SIZE - 1)]))
890 pTable->m_look_up[rev_code & (TINFL_FAST_LOOKUP_SIZE - 1)] = (mz_int16)tree_next;
891 tree_cur = tree_next;
894 rev_code >>= (TINFL_FAST_LOOKUP_BITS - 1);
895 for (j = code_size; j > (TINFL_FAST_LOOKUP_BITS + 1); j--)
897 tree_cur -= ((rev_code >>= 1) & 1);
898 if (!pTable->m_tree[-tree_cur - 1])
900 pTable->m_tree[-tree_cur - 1] = (mz_int16)tree_next;
901 tree_cur = tree_next;
905 tree_cur = pTable->m_tree[-tree_cur - 1];
907 tree_cur -= ((rev_code >>= 1) & 1);
908 pTable->m_tree[-tree_cur - 1] = (mz_int16)sym_index;
912 for (counter = 0; counter < (r->m_table_sizes[0] + r->m_table_sizes[1]);)
915 TINFL_HUFF_DECODE(16, dist, &r->m_tables[2]);
918 r->m_len_codes[counter++] = (mz_uint8)dist;
921 if ((dist == 16) && (!counter))
923 TINFL_CR_RETURN_FOREVER(17, TINFL_STATUS_FAILED);
925 num_extra = "\02\03\07"[dist - 16];
926 TINFL_GET_BITS(18, s, num_extra);
927 s += "\03\03\013"[dist - 16];
928 TINFL_MEMSET(r->m_len_codes + counter, (dist == 16) ? r->m_len_codes[counter - 1] : 0, s);
931 if ((r->m_table_sizes[0] + r->m_table_sizes[1]) != counter)
933 TINFL_CR_RETURN_FOREVER(21, TINFL_STATUS_FAILED);
935 TINFL_MEMCPY(r->m_tables[0].m_code_size, r->m_len_codes, r->m_table_sizes[0]);
936 TINFL_MEMCPY(r->m_tables[1].m_code_size, r->m_len_codes + r->m_table_sizes[0], r->m_table_sizes[1]);
944 if (((pIn_buf_end - pIn_buf_cur) < 4) || ((pOut_buf_end - pOut_buf_cur) < 2))
946 TINFL_HUFF_DECODE(23, counter, &r->m_tables[0]);
949 while (pOut_buf_cur >= pOut_buf_end)
951 TINFL_CR_RETURN(24, TINFL_STATUS_HAS_MORE_OUTPUT);
953 *pOut_buf_cur++ = (mz_uint8)counter;
959 #if TINFL_USE_64BIT_BITBUF
962 bit_buf |= (((tinfl_bit_buf_t)MZ_READ_LE32(pIn_buf_cur)) << num_bits);
969 bit_buf |= (((tinfl_bit_buf_t)MZ_READ_LE16(pIn_buf_cur)) << num_bits);
974 if ((sym2 = r->m_tables[0].m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0)
975 code_len = sym2 >> 9;
978 code_len = TINFL_FAST_LOOKUP_BITS;
981 sym2 = r->m_tables[0].m_tree[~sym2 + ((bit_buf >> code_len++) & 1)];
985 bit_buf >>= code_len;
986 num_bits -= code_len;
990 #if !TINFL_USE_64BIT_BITBUF
993 bit_buf |= (((tinfl_bit_buf_t)MZ_READ_LE16(pIn_buf_cur)) << num_bits);
998 if ((sym2 = r->m_tables[0].m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0)
999 code_len = sym2 >> 9;
1002 code_len = TINFL_FAST_LOOKUP_BITS;
1005 sym2 = r->m_tables[0].m_tree[~sym2 + ((bit_buf >> code_len++) & 1)];
1008 bit_buf >>= code_len;
1009 num_bits -= code_len;
1011 pOut_buf_cur[0] = (mz_uint8)counter;
1018 pOut_buf_cur[1] = (mz_uint8)sym2;
1022 if ((counter &= 511) == 256)
1025 num_extra = s_length_extra[counter - 257];
1026 counter = s_length_base[counter - 257];
1030 TINFL_GET_BITS(25, extra_bits, num_extra);
1031 counter += extra_bits;
1034 TINFL_HUFF_DECODE(26, dist, &r->m_tables[1]);
1035 num_extra = s_dist_extra[dist];
1036 dist = s_dist_base[dist];
1040 TINFL_GET_BITS(27, extra_bits, num_extra);
1044 dist_from_out_buf_start = pOut_buf_cur - pOut_buf_start;
1045 if ((dist > dist_from_out_buf_start) && (decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF))
1047 TINFL_CR_RETURN_FOREVER(37, TINFL_STATUS_FAILED);
1050 pSrc = pOut_buf_start + ((dist_from_out_buf_start - dist) & out_buf_size_mask);
1052 if ((MZ_MAX(pOut_buf_cur, pSrc) + counter) > pOut_buf_end)
1056 while (pOut_buf_cur >= pOut_buf_end)
1058 TINFL_CR_RETURN(53, TINFL_STATUS_HAS_MORE_OUTPUT);
1060 *pOut_buf_cur++ = pOut_buf_start[(dist_from_out_buf_start++ - dist) & out_buf_size_mask];
1064 #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES
1065 else if ((counter >= 9) && (counter <= dist))
1067 const mz_uint8 *pSrc_end = pSrc + (counter & ~7);
1070 ((mz_uint32 *)pOut_buf_cur)[0] = ((const mz_uint32 *)pSrc)[0];
1071 ((mz_uint32 *)pOut_buf_cur)[1] = ((const mz_uint32 *)pSrc)[1];
1073 } while ((pSrc += 8) < pSrc_end);
1074 if ((counter &= 7) < 3)
1078 pOut_buf_cur[0] = pSrc[0];
1080 pOut_buf_cur[1] = pSrc[1];
1081 pOut_buf_cur += counter;
1089 pOut_buf_cur[0] = pSrc[0];
1090 pOut_buf_cur[1] = pSrc[1];
1091 pOut_buf_cur[2] = pSrc[2];
1094 } while ((int)(counter -= 3) > 2);
1095 if ((int)counter > 0)
1097 pOut_buf_cur[0] = pSrc[0];
1098 if ((int)counter > 1)
1099 pOut_buf_cur[1] = pSrc[1];
1100 pOut_buf_cur += counter;
1104 } while (!(r->m_final & 1));
1106 // Ensure byte alignment and put back any bytes from the bitbuf if we've looked ahead too far on gzip, or other Deflate streams followed by arbitrary data.
1107 // I'm being super conservative here. A number of simplifications can be made to the byte alignment part, and the Adler32 check shouldn't ever need to worry about reading from the bitbuf now.
1108 TINFL_SKIP_BITS(32, num_bits & 7);
1109 while ((pIn_buf_cur > pIn_buf_next) && (num_bits >= 8))
1114 bit_buf &= (tinfl_bit_buf_t)((1ULL << num_bits) - 1ULL);
1115 MZ_ASSERT(!num_bits); // if this assert fires then we've read beyond the end of non-deflate/zlib streams with following data (such as gzip streams).
1117 if (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER)
1119 for (counter = 0; counter < 4; ++counter)
1123 TINFL_GET_BITS(41, s, 8);
1125 TINFL_GET_BYTE(42, s);
1126 r->m_z_adler32 = (r->m_z_adler32 << 8) | s;
1129 TINFL_CR_RETURN_FOREVER(34, TINFL_STATUS_DONE);
1134 // As long as we aren't telling the caller that we NEED more input to make forward progress:
1135 // Put back any bytes from the bitbuf in case we've looked ahead too far on gzip, or other Deflate streams followed by arbitrary data.
1136 // We need to be very careful here to NOT push back any bytes we definitely know we need to make forward progress, though, or we'll lock the caller up into an inf loop.
1137 if ((status != TINFL_STATUS_NEEDS_MORE_INPUT) && (status != TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS))
1139 while ((pIn_buf_cur > pIn_buf_next) && (num_bits >= 8))
1145 r->m_num_bits = num_bits;
1146 r->m_bit_buf = bit_buf & (tinfl_bit_buf_t)((1ULL << num_bits) - 1ULL);
1148 r->m_counter = counter;
1149 r->m_num_extra = num_extra;
1150 r->m_dist_from_out_buf_start = dist_from_out_buf_start;
1151 *pIn_buf_size = pIn_buf_cur - pIn_buf_next;
1152 *pOut_buf_size = pOut_buf_cur - pOut_buf_next;
1153 if ((decomp_flags & (TINFL_FLAG_PARSE_ZLIB_HEADER | TINFL_FLAG_COMPUTE_ADLER32)) && (status >= 0))
1155 const mz_uint8 *ptr = pOut_buf_next;
1156 size_t buf_len = *pOut_buf_size;
1157 mz_uint32 i, s1 = r->m_check_adler32 & 0xffff, s2 = r->m_check_adler32 >> 16;
1158 size_t block_len = buf_len % 5552;
1161 for (i = 0; i + 7 < block_len; i += 8, ptr += 8)
1163 s1 += ptr[0], s2 += s1;
1164 s1 += ptr[1], s2 += s1;
1165 s1 += ptr[2], s2 += s1;
1166 s1 += ptr[3], s2 += s1;
1167 s1 += ptr[4], s2 += s1;
1168 s1 += ptr[5], s2 += s1;
1169 s1 += ptr[6], s2 += s1;
1170 s1 += ptr[7], s2 += s1;
1172 for (; i < block_len; ++i)
1173 s1 += *ptr++, s2 += s1;
1174 s1 %= 65521U, s2 %= 65521U;
1175 buf_len -= block_len;
1178 r->m_check_adler32 = (s2 << 16) + s1;
1179 if ((status == TINFL_STATUS_DONE) && (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER) && (r->m_check_adler32 != r->m_z_adler32))
1180 status = TINFL_STATUS_ADLER32_MISMATCH;
1185 // Higher level helper functions.
1186 void *tinfl_decompress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, size_t *pOut_len, int flags)
1188 tinfl_decompressor decomp;
1189 void *pBuf = NULL, *pNew_buf;
1190 size_t src_buf_ofs = 0, out_buf_capacity = 0;
1192 tinfl_init(&decomp);
1195 size_t src_buf_size = src_buf_len - src_buf_ofs, dst_buf_size = out_buf_capacity - *pOut_len, new_out_buf_capacity;
1196 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,
1197 (flags & ~TINFL_FLAG_HAS_MORE_INPUT) | TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF);
1198 if ((status < 0) || (status == TINFL_STATUS_NEEDS_MORE_INPUT))
1204 src_buf_ofs += src_buf_size;
1205 *pOut_len += dst_buf_size;
1206 if (status == TINFL_STATUS_DONE)
1208 new_out_buf_capacity = out_buf_capacity * 2;
1209 if (new_out_buf_capacity < 128)
1210 new_out_buf_capacity = 128;
1211 pNew_buf = MZ_REALLOC(pBuf, new_out_buf_capacity);
1219 out_buf_capacity = new_out_buf_capacity;
1224 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)
1226 tinfl_decompressor decomp;
1227 tinfl_status status;
1228 tinfl_init(&decomp);
1229 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);
1230 return (status != TINFL_STATUS_DONE) ? TINFL_DECOMPRESS_MEM_TO_MEM_FAILED : out_buf_len;
1233 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)
1236 tinfl_decompressor decomp;
1237 mz_uint8 *pDict = (mz_uint8 *)MZ_MALLOC(TINFL_LZ_DICT_SIZE);
1238 size_t in_buf_ofs = 0, dict_ofs = 0;
1240 return TINFL_STATUS_FAILED;
1241 tinfl_init(&decomp);
1244 size_t in_buf_size = *pIn_buf_size - in_buf_ofs, dst_buf_size = TINFL_LZ_DICT_SIZE - dict_ofs;
1245 tinfl_status status = tinfl_decompress(&decomp, (const mz_uint8 *)pIn_buf + in_buf_ofs, &in_buf_size, pDict, pDict + dict_ofs, &dst_buf_size,
1246 (flags & ~(TINFL_FLAG_HAS_MORE_INPUT | TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF)));
1247 in_buf_ofs += in_buf_size;
1248 if ((dst_buf_size) && (!(*pPut_buf_func)(pDict + dict_ofs, (int)dst_buf_size, pPut_buf_user)))
1250 if (status != TINFL_STATUS_HAS_MORE_OUTPUT)
1252 result = (status == TINFL_STATUS_DONE);
1255 dict_ofs = (dict_ofs + dst_buf_size) & (TINFL_LZ_DICT_SIZE - 1);
1258 *pIn_buf_size = in_buf_ofs;
1262 // ------------------- Low-level Compression (independent from all decompression API's)
1264 // Purposely making these tables static for faster init and thread safety.
1265 static const mz_uint16 s_tdefl_len_sym[256] =
1267 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,
1268 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,
1269 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,
1270 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,
1271 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,
1272 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,
1273 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,
1274 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
1277 static const mz_uint8 s_tdefl_len_extra[256] =
1279 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,
1280 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,
1281 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
1282 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 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
1285 static const mz_uint8 s_tdefl_small_dist_sym[512] =
1287 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,
1288 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,
1289 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,
1290 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,
1291 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,
1292 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,
1293 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,
1294 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,
1295 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,
1296 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,
1297 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,
1298 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
1301 static const mz_uint8 s_tdefl_small_dist_extra[512] =
1303 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,
1304 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 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,
1305 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,
1306 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,
1307 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
1308 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
1309 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
1310 7, 7, 7, 7, 7, 7, 7, 7
1313 static const mz_uint8 s_tdefl_large_dist_sym[128] =
1315 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,
1316 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,
1317 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
1320 static const mz_uint8 s_tdefl_large_dist_extra[128] =
1322 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,
1323 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,
1324 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
1327 // Radix sorts tdefl_sym_freq[] array by 16-bit key m_key. Returns ptr to sorted values.
1330 mz_uint16 m_key, m_sym_index;
1332 static tdefl_sym_freq *tdefl_radix_sort_syms(mz_uint num_syms, tdefl_sym_freq *pSyms0, tdefl_sym_freq *pSyms1)
1334 mz_uint32 total_passes = 2, pass_shift, pass, i, hist[256 * 2];
1335 tdefl_sym_freq *pCur_syms = pSyms0, *pNew_syms = pSyms1;
1337 for (i = 0; i < num_syms; i++)
1339 mz_uint freq = pSyms0[i].m_key;
1340 hist[freq & 0xFF]++;
1341 hist[256 + ((freq >> 8) & 0xFF)]++;
1343 while ((total_passes > 1) && (num_syms == hist[(total_passes - 1) * 256]))
1345 for (pass_shift = 0, pass = 0; pass < total_passes; pass++, pass_shift += 8)
1347 const mz_uint32 *pHist = &hist[pass << 8];
1348 mz_uint offsets[256], cur_ofs = 0;
1349 for (i = 0; i < 256; i++)
1351 offsets[i] = cur_ofs;
1352 cur_ofs += pHist[i];
1354 for (i = 0; i < num_syms; i++)
1355 pNew_syms[offsets[(pCur_syms[i].m_key >> pass_shift) & 0xFF]++] = pCur_syms[i];
1357 tdefl_sym_freq *t = pCur_syms;
1358 pCur_syms = pNew_syms;
1365 // tdefl_calculate_minimum_redundancy() originally written by: Alistair Moffat, alistair@cs.mu.oz.au, Jyrki Katajainen, jyrki@diku.dk, November 1996.
1366 static void tdefl_calculate_minimum_redundancy(tdefl_sym_freq *A, int n)
1368 int root, leaf, next, avbl, used, dpth;
1376 A[0].m_key += A[1].m_key;
1379 for (next = 1; next < n - 1; next++)
1381 if (leaf >= n || A[root].m_key < A[leaf].m_key)
1383 A[next].m_key = A[root].m_key;
1384 A[root++].m_key = (mz_uint16)next;
1387 A[next].m_key = A[leaf++].m_key;
1388 if (leaf >= n || (root < next && A[root].m_key < A[leaf].m_key))
1390 A[next].m_key = (mz_uint16)(A[next].m_key + A[root].m_key);
1391 A[root++].m_key = (mz_uint16)next;
1394 A[next].m_key = (mz_uint16)(A[next].m_key + A[leaf++].m_key);
1397 for (next = n - 3; next >= 0; next--)
1398 A[next].m_key = A[A[next].m_key].m_key + 1;
1405 while (root >= 0 && (int)A[root].m_key == dpth)
1412 A[next--].m_key = (mz_uint16)(dpth);
1421 // Limits canonical Huffman code table's max code size.
1424 TDEFL_MAX_SUPPORTED_HUFF_CODESIZE = 32
1426 static void tdefl_huffman_enforce_max_code_size(int *pNum_codes, int code_list_len, int max_code_size)
1429 mz_uint32 total = 0;
1430 if (code_list_len <= 1)
1432 for (i = max_code_size + 1; i <= TDEFL_MAX_SUPPORTED_HUFF_CODESIZE; i++)
1433 pNum_codes[max_code_size] += pNum_codes[i];
1434 for (i = max_code_size; i > 0; i--)
1435 total += (((mz_uint32)pNum_codes[i]) << (max_code_size - i));
1436 while (total != (1UL << max_code_size))
1438 pNum_codes[max_code_size]--;
1439 for (i = max_code_size - 1; i > 0; i--)
1443 pNum_codes[i + 1] += 2;
1450 static void tdefl_optimize_huffman_table(tdefl_compressor *d, int table_num, int table_len, int code_size_limit, int static_table)
1452 int i, j, l, num_codes[1 + TDEFL_MAX_SUPPORTED_HUFF_CODESIZE];
1453 mz_uint next_code[TDEFL_MAX_SUPPORTED_HUFF_CODESIZE + 1];
1454 MZ_CLEAR_OBJ(num_codes);
1457 for (i = 0; i < table_len; i++)
1458 num_codes[d->m_huff_code_sizes[table_num][i]]++;
1462 tdefl_sym_freq syms0[TDEFL_MAX_HUFF_SYMBOLS], syms1[TDEFL_MAX_HUFF_SYMBOLS], *pSyms;
1463 int num_used_syms = 0;
1464 const mz_uint16 *pSym_count = &d->m_huff_count[table_num][0];
1465 for (i = 0; i < table_len; i++)
1468 syms0[num_used_syms].m_key = (mz_uint16)pSym_count[i];
1469 syms0[num_used_syms++].m_sym_index = (mz_uint16)i;
1472 pSyms = tdefl_radix_sort_syms(num_used_syms, syms0, syms1);
1473 tdefl_calculate_minimum_redundancy(pSyms, num_used_syms);
1475 for (i = 0; i < num_used_syms; i++)
1476 num_codes[pSyms[i].m_key]++;
1478 tdefl_huffman_enforce_max_code_size(num_codes, num_used_syms, code_size_limit);
1480 MZ_CLEAR_OBJ(d->m_huff_code_sizes[table_num]);
1481 MZ_CLEAR_OBJ(d->m_huff_codes[table_num]);
1482 for (i = 1, j = num_used_syms; i <= code_size_limit; i++)
1483 for (l = num_codes[i]; l > 0; l--)
1484 d->m_huff_code_sizes[table_num][pSyms[--j].m_sym_index] = (mz_uint8)(i);
1488 for (j = 0, i = 2; i <= code_size_limit; i++)
1489 next_code[i] = j = ((j + num_codes[i - 1]) << 1);
1491 for (i = 0; i < table_len; i++)
1493 mz_uint rev_code = 0, code, code_size;
1494 if ((code_size = d->m_huff_code_sizes[table_num][i]) == 0)
1496 code = next_code[code_size]++;
1497 for (l = code_size; l > 0; l--, code >>= 1)
1498 rev_code = (rev_code << 1) | (code & 1);
1499 d->m_huff_codes[table_num][i] = (mz_uint16)rev_code;
1503 #define TDEFL_PUT_BITS(b, l) \
1508 MZ_ASSERT(bits <= ((1U << len) - 1U)); \
1509 d->m_bit_buffer |= (bits << d->m_bits_in); \
1510 d->m_bits_in += len; \
1511 while (d->m_bits_in >= 8) \
1513 if (d->m_pOutput_buf < d->m_pOutput_buf_end) \
1514 *d->m_pOutput_buf++ = (mz_uint8)(d->m_bit_buffer); \
1515 d->m_bit_buffer >>= 8; \
1516 d->m_bits_in -= 8; \
1522 #define TDEFL_RLE_PREV_CODE_SIZE() \
1524 if (rle_repeat_count) \
1526 if (rle_repeat_count < 3) \
1528 d->m_huff_count[2][prev_code_size] = (mz_uint16)(d->m_huff_count[2][prev_code_size] + rle_repeat_count); \
1529 while (rle_repeat_count--) \
1530 packed_code_sizes[num_packed_code_sizes++] = prev_code_size; \
1534 d->m_huff_count[2][16] = (mz_uint16)(d->m_huff_count[2][16] + 1); \
1535 packed_code_sizes[num_packed_code_sizes++] = 16; \
1536 packed_code_sizes[num_packed_code_sizes++] = (mz_uint8)(rle_repeat_count - 3); \
1539 rle_repeat_count = 0; \
1543 #define TDEFL_RLE_ZERO_CODE_SIZE() \
1547 if (rle_z_count < 3) \
1549 d->m_huff_count[2][0] = (mz_uint16)(d->m_huff_count[2][0] + rle_z_count); \
1550 while (rle_z_count--) \
1551 packed_code_sizes[num_packed_code_sizes++] = 0; \
1553 else if (rle_z_count <= 10) \
1555 d->m_huff_count[2][17] = (mz_uint16)(d->m_huff_count[2][17] + 1); \
1556 packed_code_sizes[num_packed_code_sizes++] = 17; \
1557 packed_code_sizes[num_packed_code_sizes++] = (mz_uint8)(rle_z_count - 3); \
1561 d->m_huff_count[2][18] = (mz_uint16)(d->m_huff_count[2][18] + 1); \
1562 packed_code_sizes[num_packed_code_sizes++] = 18; \
1563 packed_code_sizes[num_packed_code_sizes++] = (mz_uint8)(rle_z_count - 11); \
1570 static 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 };
1572 static void tdefl_start_dynamic_block(tdefl_compressor *d)
1574 int num_lit_codes, num_dist_codes, num_bit_lengths;
1575 mz_uint i, total_code_sizes_to_pack, num_packed_code_sizes, rle_z_count, rle_repeat_count, packed_code_sizes_index;
1576 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;
1578 d->m_huff_count[0][256] = 1;
1580 tdefl_optimize_huffman_table(d, 0, TDEFL_MAX_HUFF_SYMBOLS_0, 15, MZ_FALSE);
1581 tdefl_optimize_huffman_table(d, 1, TDEFL_MAX_HUFF_SYMBOLS_1, 15, MZ_FALSE);
1583 for (num_lit_codes = 286; num_lit_codes > 257; num_lit_codes--)
1584 if (d->m_huff_code_sizes[0][num_lit_codes - 1])
1586 for (num_dist_codes = 30; num_dist_codes > 1; num_dist_codes--)
1587 if (d->m_huff_code_sizes[1][num_dist_codes - 1])
1590 memcpy(code_sizes_to_pack, &d->m_huff_code_sizes[0][0], num_lit_codes);
1591 memcpy(code_sizes_to_pack + num_lit_codes, &d->m_huff_code_sizes[1][0], num_dist_codes);
1592 total_code_sizes_to_pack = num_lit_codes + num_dist_codes;
1593 num_packed_code_sizes = 0;
1595 rle_repeat_count = 0;
1597 memset(&d->m_huff_count[2][0], 0, sizeof(d->m_huff_count[2][0]) * TDEFL_MAX_HUFF_SYMBOLS_2);
1598 for (i = 0; i < total_code_sizes_to_pack; i++)
1600 mz_uint8 code_size = code_sizes_to_pack[i];
1603 TDEFL_RLE_PREV_CODE_SIZE();
1604 if (++rle_z_count == 138)
1606 TDEFL_RLE_ZERO_CODE_SIZE();
1611 TDEFL_RLE_ZERO_CODE_SIZE();
1612 if (code_size != prev_code_size)
1614 TDEFL_RLE_PREV_CODE_SIZE();
1615 d->m_huff_count[2][code_size] = (mz_uint16)(d->m_huff_count[2][code_size] + 1);
1616 packed_code_sizes[num_packed_code_sizes++] = code_size;
1618 else if (++rle_repeat_count == 6)
1620 TDEFL_RLE_PREV_CODE_SIZE();
1623 prev_code_size = code_size;
1625 if (rle_repeat_count)
1627 TDEFL_RLE_PREV_CODE_SIZE();
1631 TDEFL_RLE_ZERO_CODE_SIZE();
1634 tdefl_optimize_huffman_table(d, 2, TDEFL_MAX_HUFF_SYMBOLS_2, 7, MZ_FALSE);
1636 TDEFL_PUT_BITS(2, 2);
1638 TDEFL_PUT_BITS(num_lit_codes - 257, 5);
1639 TDEFL_PUT_BITS(num_dist_codes - 1, 5);
1641 for (num_bit_lengths = 18; num_bit_lengths >= 0; num_bit_lengths--)
1642 if (d->m_huff_code_sizes[2][s_tdefl_packed_code_size_syms_swizzle[num_bit_lengths]])
1644 num_bit_lengths = MZ_MAX(4, (num_bit_lengths + 1));
1645 TDEFL_PUT_BITS(num_bit_lengths - 4, 4);
1646 for (i = 0; (int)i < num_bit_lengths; i++)
1647 TDEFL_PUT_BITS(d->m_huff_code_sizes[2][s_tdefl_packed_code_size_syms_swizzle[i]], 3);
1649 for (packed_code_sizes_index = 0; packed_code_sizes_index < num_packed_code_sizes;)
1651 mz_uint code = packed_code_sizes[packed_code_sizes_index++];
1652 MZ_ASSERT(code < TDEFL_MAX_HUFF_SYMBOLS_2);
1653 TDEFL_PUT_BITS(d->m_huff_codes[2][code], d->m_huff_code_sizes[2][code]);
1655 TDEFL_PUT_BITS(packed_code_sizes[packed_code_sizes_index++], "\02\03\07"[code - 16]);
1659 static void tdefl_start_static_block(tdefl_compressor *d)
1662 mz_uint8 *p = &d->m_huff_code_sizes[0][0];
1664 for (i = 0; i <= 143; ++i)
1666 for (; i <= 255; ++i)
1668 for (; i <= 279; ++i)
1670 for (; i <= 287; ++i)
1673 memset(d->m_huff_code_sizes[1], 5, 32);
1675 tdefl_optimize_huffman_table(d, 0, 288, 15, MZ_TRUE);
1676 tdefl_optimize_huffman_table(d, 1, 32, 15, MZ_TRUE);
1678 TDEFL_PUT_BITS(1, 2);
1681 static const mz_uint mz_bitmasks[17] = { 0x0000, 0x0001, 0x0003, 0x0007, 0x000F, 0x001F, 0x003F, 0x007F, 0x00FF, 0x01FF, 0x03FF, 0x07FF, 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF };
1683 #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES &&MINIZ_LITTLE_ENDIAN &&MINIZ_HAS_64BIT_REGISTERS
1684 static mz_bool tdefl_compress_lz_codes(tdefl_compressor *d)
1687 mz_uint8 *pLZ_codes;
1688 mz_uint8 *pOutput_buf = d->m_pOutput_buf;
1689 mz_uint8 *pLZ_code_buf_end = d->m_pLZ_code_buf;
1690 mz_uint64 bit_buffer = d->m_bit_buffer;
1691 mz_uint bits_in = d->m_bits_in;
1693 #define TDEFL_PUT_BITS_FAST(b, l) \
1695 bit_buffer |= (((mz_uint64)(b)) << bits_in); \
1700 for (pLZ_codes = d->m_lz_code_buf; pLZ_codes < pLZ_code_buf_end; flags >>= 1)
1703 flags = *pLZ_codes++ | 0x100;
1707 mz_uint s0, s1, n0, n1, sym, num_extra_bits;
1708 mz_uint match_len = pLZ_codes[0], match_dist = *(const mz_uint16 *)(pLZ_codes + 1);
1711 MZ_ASSERT(d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]);
1712 TDEFL_PUT_BITS_FAST(d->m_huff_codes[0][s_tdefl_len_sym[match_len]], d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]);
1713 TDEFL_PUT_BITS_FAST(match_len & mz_bitmasks[s_tdefl_len_extra[match_len]], s_tdefl_len_extra[match_len]);
1715 // This sequence coaxes MSVC into using cmov's vs. jmp's.
1716 s0 = s_tdefl_small_dist_sym[match_dist & 511];
1717 n0 = s_tdefl_small_dist_extra[match_dist & 511];
1718 s1 = s_tdefl_large_dist_sym[match_dist >> 8];
1719 n1 = s_tdefl_large_dist_extra[match_dist >> 8];
1720 sym = (match_dist < 512) ? s0 : s1;
1721 num_extra_bits = (match_dist < 512) ? n0 : n1;
1723 MZ_ASSERT(d->m_huff_code_sizes[1][sym]);
1724 TDEFL_PUT_BITS_FAST(d->m_huff_codes[1][sym], d->m_huff_code_sizes[1][sym]);
1725 TDEFL_PUT_BITS_FAST(match_dist & mz_bitmasks[num_extra_bits], num_extra_bits);
1729 mz_uint lit = *pLZ_codes++;
1730 MZ_ASSERT(d->m_huff_code_sizes[0][lit]);
1731 TDEFL_PUT_BITS_FAST(d->m_huff_codes[0][lit], d->m_huff_code_sizes[0][lit]);
1733 if (((flags & 2) == 0) && (pLZ_codes < pLZ_code_buf_end))
1737 MZ_ASSERT(d->m_huff_code_sizes[0][lit]);
1738 TDEFL_PUT_BITS_FAST(d->m_huff_codes[0][lit], d->m_huff_code_sizes[0][lit]);
1740 if (((flags & 2) == 0) && (pLZ_codes < pLZ_code_buf_end))
1744 MZ_ASSERT(d->m_huff_code_sizes[0][lit]);
1745 TDEFL_PUT_BITS_FAST(d->m_huff_codes[0][lit], d->m_huff_code_sizes[0][lit]);
1750 if (pOutput_buf >= d->m_pOutput_buf_end)
1753 *(mz_uint64 *)pOutput_buf = bit_buffer;
1754 pOutput_buf += (bits_in >> 3);
1755 bit_buffer >>= (bits_in & ~7);
1759 #undef TDEFL_PUT_BITS_FAST
1761 d->m_pOutput_buf = pOutput_buf;
1763 d->m_bit_buffer = 0;
1767 mz_uint32 n = MZ_MIN(bits_in, 16);
1768 TDEFL_PUT_BITS((mz_uint)bit_buffer & mz_bitmasks[n], n);
1773 TDEFL_PUT_BITS(d->m_huff_codes[0][256], d->m_huff_code_sizes[0][256]);
1775 return (d->m_pOutput_buf < d->m_pOutput_buf_end);
1778 static mz_bool tdefl_compress_lz_codes(tdefl_compressor *d)
1781 mz_uint8 *pLZ_codes;
1784 for (pLZ_codes = d->m_lz_code_buf; pLZ_codes < d->m_pLZ_code_buf; flags >>= 1)
1787 flags = *pLZ_codes++ | 0x100;
1790 mz_uint sym, num_extra_bits;
1791 mz_uint match_len = pLZ_codes[0], match_dist = (pLZ_codes[1] | (pLZ_codes[2] << 8));
1794 MZ_ASSERT(d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]);
1795 TDEFL_PUT_BITS(d->m_huff_codes[0][s_tdefl_len_sym[match_len]], d->m_huff_code_sizes[0][s_tdefl_len_sym[match_len]]);
1796 TDEFL_PUT_BITS(match_len & mz_bitmasks[s_tdefl_len_extra[match_len]], s_tdefl_len_extra[match_len]);
1798 if (match_dist < 512)
1800 sym = s_tdefl_small_dist_sym[match_dist];
1801 num_extra_bits = s_tdefl_small_dist_extra[match_dist];
1805 sym = s_tdefl_large_dist_sym[match_dist >> 8];
1806 num_extra_bits = s_tdefl_large_dist_extra[match_dist >> 8];
1808 MZ_ASSERT(d->m_huff_code_sizes[1][sym]);
1809 TDEFL_PUT_BITS(d->m_huff_codes[1][sym], d->m_huff_code_sizes[1][sym]);
1810 TDEFL_PUT_BITS(match_dist & mz_bitmasks[num_extra_bits], num_extra_bits);
1814 mz_uint lit = *pLZ_codes++;
1815 MZ_ASSERT(d->m_huff_code_sizes[0][lit]);
1816 TDEFL_PUT_BITS(d->m_huff_codes[0][lit], d->m_huff_code_sizes[0][lit]);
1820 TDEFL_PUT_BITS(d->m_huff_codes[0][256], d->m_huff_code_sizes[0][256]);
1822 return (d->m_pOutput_buf < d->m_pOutput_buf_end);
1824 #endif // MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN && MINIZ_HAS_64BIT_REGISTERS
1826 static mz_bool tdefl_compress_block(tdefl_compressor *d, mz_bool static_block)
1829 tdefl_start_static_block(d);
1831 tdefl_start_dynamic_block(d);
1832 return tdefl_compress_lz_codes(d);
1835 static int tdefl_flush_block(tdefl_compressor *d, int flush)
1837 mz_uint saved_bit_buf, saved_bits_in;
1838 mz_uint8 *pSaved_output_buf;
1839 mz_bool comp_block_succeeded = MZ_FALSE;
1840 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;
1841 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;
1843 d->m_pOutput_buf = pOutput_buf_start;
1844 d->m_pOutput_buf_end = d->m_pOutput_buf + TDEFL_OUT_BUF_SIZE - 16;
1846 MZ_ASSERT(!d->m_output_flush_remaining);
1847 d->m_output_flush_ofs = 0;
1848 d->m_output_flush_remaining = 0;
1850 *d->m_pLZ_flags = (mz_uint8)(*d->m_pLZ_flags >> d->m_num_flags_left);
1851 d->m_pLZ_code_buf -= (d->m_num_flags_left == 8);
1853 if ((d->m_flags & TDEFL_WRITE_ZLIB_HEADER) && (!d->m_block_index))
1855 TDEFL_PUT_BITS(0x78, 8);
1856 TDEFL_PUT_BITS(0x01, 8);
1859 TDEFL_PUT_BITS(flush == TDEFL_FINISH, 1);
1861 pSaved_output_buf = d->m_pOutput_buf;
1862 saved_bit_buf = d->m_bit_buffer;
1863 saved_bits_in = d->m_bits_in;
1866 comp_block_succeeded = tdefl_compress_block(d, (d->m_flags & TDEFL_FORCE_ALL_STATIC_BLOCKS) || (d->m_total_lz_bytes < 48));
1868 // If the block gets expanded, forget the current contents of the output buffer and send a raw block instead.
1869 if (((use_raw_block) || ((d->m_total_lz_bytes) && ((d->m_pOutput_buf - pSaved_output_buf + 1U) >= d->m_total_lz_bytes))) &&
1870 ((d->m_lookahead_pos - d->m_lz_code_buf_dict_pos) <= d->m_dict_size))
1873 d->m_pOutput_buf = pSaved_output_buf;
1874 d->m_bit_buffer = saved_bit_buf, d->m_bits_in = saved_bits_in;
1875 TDEFL_PUT_BITS(0, 2);
1878 TDEFL_PUT_BITS(0, 8 - d->m_bits_in);
1880 for (i = 2; i; --i, d->m_total_lz_bytes ^= 0xFFFF)
1882 TDEFL_PUT_BITS(d->m_total_lz_bytes & 0xFFFF, 16);
1884 for (i = 0; i < d->m_total_lz_bytes; ++i)
1886 TDEFL_PUT_BITS(d->m_dict[(d->m_lz_code_buf_dict_pos + i) & TDEFL_LZ_DICT_SIZE_MASK], 8);
1889 // Check for the extremely unlikely (if not impossible) case of the compressed block not fitting into the output buffer when using dynamic codes.
1890 else if (!comp_block_succeeded)
1892 d->m_pOutput_buf = pSaved_output_buf;
1893 d->m_bit_buffer = saved_bit_buf, d->m_bits_in = saved_bits_in;
1894 tdefl_compress_block(d, MZ_TRUE);
1899 if (flush == TDEFL_FINISH)
1903 TDEFL_PUT_BITS(0, 8 - d->m_bits_in);
1905 if (d->m_flags & TDEFL_WRITE_ZLIB_HEADER)
1907 mz_uint i, a = d->m_adler32;
1908 for (i = 0; i < 4; i++)
1910 TDEFL_PUT_BITS((a >> 24) & 0xFF, 8);
1918 TDEFL_PUT_BITS(0, 3);
1921 TDEFL_PUT_BITS(0, 8 - d->m_bits_in);
1923 for (i = 2; i; --i, z ^= 0xFFFF)
1925 TDEFL_PUT_BITS(z & 0xFFFF, 16);
1930 MZ_ASSERT(d->m_pOutput_buf < d->m_pOutput_buf_end);
1932 memset(&d->m_huff_count[0][0], 0, sizeof(d->m_huff_count[0][0]) * TDEFL_MAX_HUFF_SYMBOLS_0);
1933 memset(&d->m_huff_count[1][0], 0, sizeof(d->m_huff_count[1][0]) * TDEFL_MAX_HUFF_SYMBOLS_1);
1935 d->m_pLZ_code_buf = d->m_lz_code_buf + 1;
1936 d->m_pLZ_flags = d->m_lz_code_buf;
1937 d->m_num_flags_left = 8;
1938 d->m_lz_code_buf_dict_pos += d->m_total_lz_bytes;
1939 d->m_total_lz_bytes = 0;
1942 if ((n = (int)(d->m_pOutput_buf - pOutput_buf_start)) != 0)
1944 if (d->m_pPut_buf_func)
1946 *d->m_pIn_buf_size = d->m_pSrc - (const mz_uint8 *)d->m_pIn_buf;
1947 if (!(*d->m_pPut_buf_func)(d->m_output_buf, n, d->m_pPut_buf_user))
1948 return (d->m_prev_return_status = TDEFL_STATUS_PUT_BUF_FAILED);
1950 else if (pOutput_buf_start == d->m_output_buf)
1952 int bytes_to_copy = (int)MZ_MIN((size_t)n, (size_t)(*d->m_pOut_buf_size - d->m_out_buf_ofs));
1953 memcpy((mz_uint8 *)d->m_pOut_buf + d->m_out_buf_ofs, d->m_output_buf, bytes_to_copy);
1954 d->m_out_buf_ofs += bytes_to_copy;
1955 if ((n -= bytes_to_copy) != 0)
1957 d->m_output_flush_ofs = bytes_to_copy;
1958 d->m_output_flush_remaining = n;
1963 d->m_out_buf_ofs += n;
1967 return d->m_output_flush_remaining;
1970 #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES
1971 #define TDEFL_READ_UNALIGNED_WORD(p) *(const mz_uint16 *)(p)
1972 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)
1974 mz_uint dist, pos = lookahead_pos & TDEFL_LZ_DICT_SIZE_MASK, match_len = *pMatch_len, probe_pos = pos, next_probe_pos, probe_len;
1975 mz_uint num_probes_left = d->m_max_probes[match_len >= 32];
1976 const mz_uint16 *s = (const mz_uint16 *)(d->m_dict + pos), *p, *q;
1977 mz_uint16 c01 = TDEFL_READ_UNALIGNED_WORD(&d->m_dict[pos + match_len - 1]), s01 = TDEFL_READ_UNALIGNED_WORD(s);
1978 MZ_ASSERT(max_match_len <= TDEFL_MAX_MATCH_LEN);
1979 if (max_match_len <= match_len)
1985 if (--num_probes_left == 0)
1987 #define TDEFL_PROBE \
1988 next_probe_pos = d->m_next[probe_pos]; \
1989 if ((!next_probe_pos) || ((dist = (mz_uint16)(lookahead_pos - next_probe_pos)) > max_dist)) \
1991 probe_pos = next_probe_pos & TDEFL_LZ_DICT_SIZE_MASK; \
1992 if (TDEFL_READ_UNALIGNED_WORD(&d->m_dict[probe_pos + match_len - 1]) == c01) \
2000 q = (const mz_uint16 *)(d->m_dict + probe_pos);
2001 if (TDEFL_READ_UNALIGNED_WORD(q) != s01)
2007 } while ((TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) && (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) &&
2008 (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) && (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) && (--probe_len > 0));
2011 *pMatch_dist = dist;
2012 *pMatch_len = MZ_MIN(max_match_len, (mz_uint)TDEFL_MAX_MATCH_LEN);
2015 else if ((probe_len = ((mz_uint)(p - s) * 2) + (mz_uint)(*(const mz_uint8 *)p == *(const mz_uint8 *)q)) > match_len)
2017 *pMatch_dist = dist;
2018 if ((*pMatch_len = match_len = MZ_MIN(max_match_len, probe_len)) == max_match_len)
2020 c01 = TDEFL_READ_UNALIGNED_WORD(&d->m_dict[pos + match_len - 1]);
2025 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)
2027 mz_uint dist, pos = lookahead_pos & TDEFL_LZ_DICT_SIZE_MASK, match_len = *pMatch_len, probe_pos = pos, next_probe_pos, probe_len;
2028 mz_uint num_probes_left = d->m_max_probes[match_len >= 32];
2029 const mz_uint8 *s = d->m_dict + pos, *p, *q;
2030 mz_uint8 c0 = d->m_dict[pos + match_len], c1 = d->m_dict[pos + match_len - 1];
2031 MZ_ASSERT(max_match_len <= TDEFL_MAX_MATCH_LEN);
2032 if (max_match_len <= match_len)
2038 if (--num_probes_left == 0)
2040 #define TDEFL_PROBE \
2041 next_probe_pos = d->m_next[probe_pos]; \
2042 if ((!next_probe_pos) || ((dist = (mz_uint16)(lookahead_pos - next_probe_pos)) > max_dist)) \
2044 probe_pos = next_probe_pos & TDEFL_LZ_DICT_SIZE_MASK; \
2045 if ((d->m_dict[probe_pos + match_len] == c0) && (d->m_dict[probe_pos + match_len - 1] == c1)) \
2054 q = d->m_dict + probe_pos;
2055 for (probe_len = 0; probe_len < max_match_len; probe_len++)
2058 if (probe_len > match_len)
2060 *pMatch_dist = dist;
2061 if ((*pMatch_len = match_len = probe_len) == max_match_len)
2063 c0 = d->m_dict[pos + match_len];
2064 c1 = d->m_dict[pos + match_len - 1];
2068 #endif // #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES
2070 #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES &&MINIZ_LITTLE_ENDIAN
2071 static mz_bool tdefl_compress_fast(tdefl_compressor *d)
2073 // Faster, minimally featured LZRW1-style match+parse loop with better register utilization. Intended for applications where raw throughput is valued more highly than ratio.
2074 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;
2075 mz_uint8 *pLZ_code_buf = d->m_pLZ_code_buf, *pLZ_flags = d->m_pLZ_flags;
2076 mz_uint cur_pos = lookahead_pos & TDEFL_LZ_DICT_SIZE_MASK;
2078 while ((d->m_src_buf_left) || ((d->m_flush) && (lookahead_size)))
2080 const mz_uint TDEFL_COMP_FAST_LOOKAHEAD_SIZE = 4096;
2081 mz_uint dst_pos = (lookahead_pos + lookahead_size) & TDEFL_LZ_DICT_SIZE_MASK;
2082 mz_uint num_bytes_to_process = (mz_uint)MZ_MIN(d->m_src_buf_left, TDEFL_COMP_FAST_LOOKAHEAD_SIZE - lookahead_size);
2083 d->m_src_buf_left -= num_bytes_to_process;
2084 lookahead_size += num_bytes_to_process;
2086 while (num_bytes_to_process)
2088 mz_uint32 n = MZ_MIN(TDEFL_LZ_DICT_SIZE - dst_pos, num_bytes_to_process);
2089 memcpy(d->m_dict + dst_pos, d->m_pSrc, n);
2090 if (dst_pos < (TDEFL_MAX_MATCH_LEN - 1))
2091 memcpy(d->m_dict + TDEFL_LZ_DICT_SIZE + dst_pos, d->m_pSrc, MZ_MIN(n, (TDEFL_MAX_MATCH_LEN - 1) - dst_pos));
2093 dst_pos = (dst_pos + n) & TDEFL_LZ_DICT_SIZE_MASK;
2094 num_bytes_to_process -= n;
2097 dict_size = MZ_MIN(TDEFL_LZ_DICT_SIZE - lookahead_size, dict_size);
2098 if ((!d->m_flush) && (lookahead_size < TDEFL_COMP_FAST_LOOKAHEAD_SIZE))
2101 while (lookahead_size >= 4)
2103 mz_uint cur_match_dist, cur_match_len = 1;
2104 mz_uint8 *pCur_dict = d->m_dict + cur_pos;
2105 mz_uint first_trigram = (*(const mz_uint32 *)pCur_dict) & 0xFFFFFF;
2106 mz_uint hash = (first_trigram ^ (first_trigram >> (24 - (TDEFL_LZ_HASH_BITS - 8)))) & TDEFL_LEVEL1_HASH_SIZE_MASK;
2107 mz_uint probe_pos = d->m_hash[hash];
2108 d->m_hash[hash] = (mz_uint16)lookahead_pos;
2110 if (((cur_match_dist = (mz_uint16)(lookahead_pos - probe_pos)) <= dict_size) && ((*(const mz_uint32 *)(d->m_dict + (probe_pos &= TDEFL_LZ_DICT_SIZE_MASK)) & 0xFFFFFF) == first_trigram))
2112 const mz_uint16 *p = (const mz_uint16 *)pCur_dict;
2113 const mz_uint16 *q = (const mz_uint16 *)(d->m_dict + probe_pos);
2114 mz_uint32 probe_len = 32;
2117 } while ((TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) && (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) &&
2118 (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) && (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) && (--probe_len > 0));
2119 cur_match_len = ((mz_uint)(p - (const mz_uint16 *)pCur_dict) * 2) + (mz_uint)(*(const mz_uint8 *)p == *(const mz_uint8 *)q);
2121 cur_match_len = cur_match_dist ? TDEFL_MAX_MATCH_LEN : 0;
2123 if ((cur_match_len < TDEFL_MIN_MATCH_LEN) || ((cur_match_len == TDEFL_MIN_MATCH_LEN) && (cur_match_dist >= 8U * 1024U)))
2126 *pLZ_code_buf++ = (mz_uint8)first_trigram;
2127 *pLZ_flags = (mz_uint8)(*pLZ_flags >> 1);
2128 d->m_huff_count[0][(mz_uint8)first_trigram]++;
2133 cur_match_len = MZ_MIN(cur_match_len, lookahead_size);
2135 MZ_ASSERT((cur_match_len >= TDEFL_MIN_MATCH_LEN) && (cur_match_dist >= 1) && (cur_match_dist <= TDEFL_LZ_DICT_SIZE));
2139 pLZ_code_buf[0] = (mz_uint8)(cur_match_len - TDEFL_MIN_MATCH_LEN);
2140 *(mz_uint16 *)(&pLZ_code_buf[1]) = (mz_uint16)cur_match_dist;
2142 *pLZ_flags = (mz_uint8)((*pLZ_flags >> 1) | 0x80);
2144 s0 = s_tdefl_small_dist_sym[cur_match_dist & 511];
2145 s1 = s_tdefl_large_dist_sym[cur_match_dist >> 8];
2146 d->m_huff_count[1][(cur_match_dist < 512) ? s0 : s1]++;
2148 d->m_huff_count[0][s_tdefl_len_sym[cur_match_len - TDEFL_MIN_MATCH_LEN]]++;
2153 *pLZ_code_buf++ = (mz_uint8)first_trigram;
2154 *pLZ_flags = (mz_uint8)(*pLZ_flags >> 1);
2155 d->m_huff_count[0][(mz_uint8)first_trigram]++;
2158 if (--num_flags_left == 0)
2161 pLZ_flags = pLZ_code_buf++;
2164 total_lz_bytes += cur_match_len;
2165 lookahead_pos += cur_match_len;
2166 dict_size = MZ_MIN(dict_size + cur_match_len, (mz_uint)TDEFL_LZ_DICT_SIZE);
2167 cur_pos = (cur_pos + cur_match_len) & TDEFL_LZ_DICT_SIZE_MASK;
2168 MZ_ASSERT(lookahead_size >= cur_match_len);
2169 lookahead_size -= cur_match_len;
2171 if (pLZ_code_buf > &d->m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE - 8])
2174 d->m_lookahead_pos = lookahead_pos;
2175 d->m_lookahead_size = lookahead_size;
2176 d->m_dict_size = dict_size;
2177 d->m_total_lz_bytes = total_lz_bytes;
2178 d->m_pLZ_code_buf = pLZ_code_buf;
2179 d->m_pLZ_flags = pLZ_flags;
2180 d->m_num_flags_left = num_flags_left;
2181 if ((n = tdefl_flush_block(d, 0)) != 0)
2182 return (n < 0) ? MZ_FALSE : MZ_TRUE;
2183 total_lz_bytes = d->m_total_lz_bytes;
2184 pLZ_code_buf = d->m_pLZ_code_buf;
2185 pLZ_flags = d->m_pLZ_flags;
2186 num_flags_left = d->m_num_flags_left;
2190 while (lookahead_size)
2192 mz_uint8 lit = d->m_dict[cur_pos];
2195 *pLZ_code_buf++ = lit;
2196 *pLZ_flags = (mz_uint8)(*pLZ_flags >> 1);
2197 if (--num_flags_left == 0)
2200 pLZ_flags = pLZ_code_buf++;
2203 d->m_huff_count[0][lit]++;
2206 dict_size = MZ_MIN(dict_size + 1, (mz_uint)TDEFL_LZ_DICT_SIZE);
2207 cur_pos = (cur_pos + 1) & TDEFL_LZ_DICT_SIZE_MASK;
2210 if (pLZ_code_buf > &d->m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE - 8])
2213 d->m_lookahead_pos = lookahead_pos;
2214 d->m_lookahead_size = lookahead_size;
2215 d->m_dict_size = dict_size;
2216 d->m_total_lz_bytes = total_lz_bytes;
2217 d->m_pLZ_code_buf = pLZ_code_buf;
2218 d->m_pLZ_flags = pLZ_flags;
2219 d->m_num_flags_left = num_flags_left;
2220 if ((n = tdefl_flush_block(d, 0)) != 0)
2221 return (n < 0) ? MZ_FALSE : MZ_TRUE;
2222 total_lz_bytes = d->m_total_lz_bytes;
2223 pLZ_code_buf = d->m_pLZ_code_buf;
2224 pLZ_flags = d->m_pLZ_flags;
2225 num_flags_left = d->m_num_flags_left;
2230 d->m_lookahead_pos = lookahead_pos;
2231 d->m_lookahead_size = lookahead_size;
2232 d->m_dict_size = dict_size;
2233 d->m_total_lz_bytes = total_lz_bytes;
2234 d->m_pLZ_code_buf = pLZ_code_buf;
2235 d->m_pLZ_flags = pLZ_flags;
2236 d->m_num_flags_left = num_flags_left;
2239 #endif // MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN
2241 static MZ_FORCEINLINE void tdefl_record_literal(tdefl_compressor *d, mz_uint8 lit)
2243 d->m_total_lz_bytes++;
2244 *d->m_pLZ_code_buf++ = lit;
2245 *d->m_pLZ_flags = (mz_uint8)(*d->m_pLZ_flags >> 1);
2246 if (--d->m_num_flags_left == 0)
2248 d->m_num_flags_left = 8;
2249 d->m_pLZ_flags = d->m_pLZ_code_buf++;
2251 d->m_huff_count[0][lit]++;
2254 static MZ_FORCEINLINE void tdefl_record_match(tdefl_compressor *d, mz_uint match_len, mz_uint match_dist)
2258 MZ_ASSERT((match_len >= TDEFL_MIN_MATCH_LEN) && (match_dist >= 1) && (match_dist <= TDEFL_LZ_DICT_SIZE));
2260 d->m_total_lz_bytes += match_len;
2262 d->m_pLZ_code_buf[0] = (mz_uint8)(match_len - TDEFL_MIN_MATCH_LEN);
2265 d->m_pLZ_code_buf[1] = (mz_uint8)(match_dist & 0xFF);
2266 d->m_pLZ_code_buf[2] = (mz_uint8)(match_dist >> 8);
2267 d->m_pLZ_code_buf += 3;
2269 *d->m_pLZ_flags = (mz_uint8)((*d->m_pLZ_flags >> 1) | 0x80);
2270 if (--d->m_num_flags_left == 0)
2272 d->m_num_flags_left = 8;
2273 d->m_pLZ_flags = d->m_pLZ_code_buf++;
2276 s0 = s_tdefl_small_dist_sym[match_dist & 511];
2277 s1 = s_tdefl_large_dist_sym[(match_dist >> 8) & 127];
2278 d->m_huff_count[1][(match_dist < 512) ? s0 : s1]++;
2280 if (match_len >= TDEFL_MIN_MATCH_LEN)
2281 d->m_huff_count[0][s_tdefl_len_sym[match_len - TDEFL_MIN_MATCH_LEN]]++;
2284 static mz_bool tdefl_compress_normal(tdefl_compressor *d)
2286 const mz_uint8 *pSrc = d->m_pSrc;
2287 size_t src_buf_left = d->m_src_buf_left;
2288 tdefl_flush flush = d->m_flush;
2290 while ((src_buf_left) || ((flush) && (d->m_lookahead_size)))
2292 mz_uint len_to_move, cur_match_dist, cur_match_len, cur_pos;
2293 // Update dictionary and hash chains. Keeps the lookahead size equal to TDEFL_MAX_MATCH_LEN.
2294 if ((d->m_lookahead_size + d->m_dict_size) >= (TDEFL_MIN_MATCH_LEN - 1))
2296 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;
2297 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];
2298 mz_uint num_bytes_to_process = (mz_uint)MZ_MIN(src_buf_left, TDEFL_MAX_MATCH_LEN - d->m_lookahead_size);
2299 const mz_uint8 *pSrc_end = pSrc + num_bytes_to_process;
2300 src_buf_left -= num_bytes_to_process;
2301 d->m_lookahead_size += num_bytes_to_process;
2302 while (pSrc != pSrc_end)
2304 mz_uint8 c = *pSrc++;
2305 d->m_dict[dst_pos] = c;
2306 if (dst_pos < (TDEFL_MAX_MATCH_LEN - 1))
2307 d->m_dict[TDEFL_LZ_DICT_SIZE + dst_pos] = c;
2308 hash = ((hash << TDEFL_LZ_HASH_SHIFT) ^ c) & (TDEFL_LZ_HASH_SIZE - 1);
2309 d->m_next[ins_pos & TDEFL_LZ_DICT_SIZE_MASK] = d->m_hash[hash];
2310 d->m_hash[hash] = (mz_uint16)(ins_pos);
2311 dst_pos = (dst_pos + 1) & TDEFL_LZ_DICT_SIZE_MASK;
2317 while ((src_buf_left) && (d->m_lookahead_size < TDEFL_MAX_MATCH_LEN))
2319 mz_uint8 c = *pSrc++;
2320 mz_uint dst_pos = (d->m_lookahead_pos + d->m_lookahead_size) & TDEFL_LZ_DICT_SIZE_MASK;
2322 d->m_dict[dst_pos] = c;
2323 if (dst_pos < (TDEFL_MAX_MATCH_LEN - 1))
2324 d->m_dict[TDEFL_LZ_DICT_SIZE + dst_pos] = c;
2325 if ((++d->m_lookahead_size + d->m_dict_size) >= TDEFL_MIN_MATCH_LEN)
2327 mz_uint ins_pos = d->m_lookahead_pos + (d->m_lookahead_size - 1) - 2;
2328 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);
2329 d->m_next[ins_pos & TDEFL_LZ_DICT_SIZE_MASK] = d->m_hash[hash];
2330 d->m_hash[hash] = (mz_uint16)(ins_pos);
2334 d->m_dict_size = MZ_MIN(TDEFL_LZ_DICT_SIZE - d->m_lookahead_size, d->m_dict_size);
2335 if ((!flush) && (d->m_lookahead_size < TDEFL_MAX_MATCH_LEN))
2338 // Simple lazy/greedy parsing state machine.
2341 cur_match_len = d->m_saved_match_len ? d->m_saved_match_len : (TDEFL_MIN_MATCH_LEN - 1);
2342 cur_pos = d->m_lookahead_pos & TDEFL_LZ_DICT_SIZE_MASK;
2343 if (d->m_flags & (TDEFL_RLE_MATCHES | TDEFL_FORCE_ALL_RAW_BLOCKS))
2345 if ((d->m_dict_size) && (!(d->m_flags & TDEFL_FORCE_ALL_RAW_BLOCKS)))
2347 mz_uint8 c = d->m_dict[(cur_pos - 1) & TDEFL_LZ_DICT_SIZE_MASK];
2349 while (cur_match_len < d->m_lookahead_size)
2351 if (d->m_dict[cur_pos + cur_match_len] != c)
2355 if (cur_match_len < TDEFL_MIN_MATCH_LEN)
2363 tdefl_find_match(d, d->m_lookahead_pos, d->m_dict_size, d->m_lookahead_size, &cur_match_dist, &cur_match_len);
2365 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)))
2367 cur_match_dist = cur_match_len = 0;
2369 if (d->m_saved_match_len)
2371 if (cur_match_len > d->m_saved_match_len)
2373 tdefl_record_literal(d, (mz_uint8)d->m_saved_lit);
2374 if (cur_match_len >= 128)
2376 tdefl_record_match(d, cur_match_len, cur_match_dist);
2377 d->m_saved_match_len = 0;
2378 len_to_move = cur_match_len;
2382 d->m_saved_lit = d->m_dict[cur_pos];
2383 d->m_saved_match_dist = cur_match_dist;
2384 d->m_saved_match_len = cur_match_len;
2389 tdefl_record_match(d, d->m_saved_match_len, d->m_saved_match_dist);
2390 len_to_move = d->m_saved_match_len - 1;
2391 d->m_saved_match_len = 0;
2394 else if (!cur_match_dist)
2395 tdefl_record_literal(d, d->m_dict[MZ_MIN(cur_pos, sizeof(d->m_dict) - 1)]);
2396 else if ((d->m_greedy_parsing) || (d->m_flags & TDEFL_RLE_MATCHES) || (cur_match_len >= 128))
2398 tdefl_record_match(d, cur_match_len, cur_match_dist);
2399 len_to_move = cur_match_len;
2403 d->m_saved_lit = d->m_dict[MZ_MIN(cur_pos, sizeof(d->m_dict) - 1)];
2404 d->m_saved_match_dist = cur_match_dist;
2405 d->m_saved_match_len = cur_match_len;
2407 // Move the lookahead forward by len_to_move bytes.
2408 d->m_lookahead_pos += len_to_move;
2409 MZ_ASSERT(d->m_lookahead_size >= len_to_move);
2410 d->m_lookahead_size -= len_to_move;
2411 d->m_dict_size = MZ_MIN(d->m_dict_size + len_to_move, (mz_uint)TDEFL_LZ_DICT_SIZE);
2412 // Check if it's time to flush the current LZ codes to the internal output buffer.
2413 if ((d->m_pLZ_code_buf > &d->m_lz_code_buf[TDEFL_LZ_CODE_BUF_SIZE - 8]) ||
2414 ((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))))
2418 d->m_src_buf_left = src_buf_left;
2419 if ((n = tdefl_flush_block(d, 0)) != 0)
2420 return (n < 0) ? MZ_FALSE : MZ_TRUE;
2425 d->m_src_buf_left = src_buf_left;
2429 static tdefl_status tdefl_flush_output_buffer(tdefl_compressor *d)
2431 if (d->m_pIn_buf_size)
2433 *d->m_pIn_buf_size = d->m_pSrc - (const mz_uint8 *)d->m_pIn_buf;
2436 if (d->m_pOut_buf_size)
2438 size_t n = MZ_MIN(*d->m_pOut_buf_size - d->m_out_buf_ofs, d->m_output_flush_remaining);
2439 memcpy((mz_uint8 *)d->m_pOut_buf + d->m_out_buf_ofs, d->m_output_buf + d->m_output_flush_ofs, n);
2440 d->m_output_flush_ofs += (mz_uint)n;
2441 d->m_output_flush_remaining -= (mz_uint)n;
2442 d->m_out_buf_ofs += n;
2444 *d->m_pOut_buf_size = d->m_out_buf_ofs;
2447 return (d->m_finished && !d->m_output_flush_remaining) ? TDEFL_STATUS_DONE : TDEFL_STATUS_OKAY;
2450 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)
2458 return TDEFL_STATUS_BAD_PARAM;
2461 d->m_pIn_buf = pIn_buf;
2462 d->m_pIn_buf_size = pIn_buf_size;
2463 d->m_pOut_buf = pOut_buf;
2464 d->m_pOut_buf_size = pOut_buf_size;
2465 d->m_pSrc = (const mz_uint8 *)(pIn_buf);
2466 d->m_src_buf_left = pIn_buf_size ? *pIn_buf_size : 0;
2467 d->m_out_buf_ofs = 0;
2470 if (((d->m_pPut_buf_func != NULL) == ((pOut_buf != NULL) || (pOut_buf_size != NULL))) || (d->m_prev_return_status != TDEFL_STATUS_OKAY) ||
2471 (d->m_wants_to_finish && (flush != TDEFL_FINISH)) || (pIn_buf_size && *pIn_buf_size && !pIn_buf) || (pOut_buf_size && *pOut_buf_size && !pOut_buf))
2477 return (d->m_prev_return_status = TDEFL_STATUS_BAD_PARAM);
2479 d->m_wants_to_finish |= (flush == TDEFL_FINISH);
2481 if ((d->m_output_flush_remaining) || (d->m_finished))
2482 return (d->m_prev_return_status = tdefl_flush_output_buffer(d));
2484 #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES &&MINIZ_LITTLE_ENDIAN
2485 if (((d->m_flags & TDEFL_MAX_PROBES_MASK) == 1) &&
2486 ((d->m_flags & TDEFL_GREEDY_PARSING_FLAG) != 0) &&
2487 ((d->m_flags & (TDEFL_FILTER_MATCHES | TDEFL_FORCE_ALL_RAW_BLOCKS | TDEFL_RLE_MATCHES)) == 0))
2489 if (!tdefl_compress_fast(d))
2490 return d->m_prev_return_status;
2493 #endif // #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN
2495 if (!tdefl_compress_normal(d))
2496 return d->m_prev_return_status;
2499 if ((d->m_flags & (TDEFL_WRITE_ZLIB_HEADER | TDEFL_COMPUTE_ADLER32)) && (pIn_buf))
2500 d->m_adler32 = (mz_uint32)mz_adler32(d->m_adler32, (const mz_uint8 *)pIn_buf, d->m_pSrc - (const mz_uint8 *)pIn_buf);
2502 if ((flush) && (!d->m_lookahead_size) && (!d->m_src_buf_left) && (!d->m_output_flush_remaining))
2504 if (tdefl_flush_block(d, flush) < 0)
2505 return d->m_prev_return_status;
2506 d->m_finished = (flush == TDEFL_FINISH);
2507 if (flush == TDEFL_FULL_FLUSH)
2509 MZ_CLEAR_OBJ(d->m_hash);
2510 MZ_CLEAR_OBJ(d->m_next);
2515 return (d->m_prev_return_status = tdefl_flush_output_buffer(d));
2518 tdefl_status tdefl_compress_buffer(tdefl_compressor *d, const void *pIn_buf, size_t in_buf_size, tdefl_flush flush)
2520 MZ_ASSERT(d->m_pPut_buf_func);
2521 return tdefl_compress(d, pIn_buf, &in_buf_size, NULL, NULL, flush);
2524 tdefl_status tdefl_init(tdefl_compressor *d, tdefl_put_buf_func_ptr pPut_buf_func, void *pPut_buf_user, int flags)
2526 d->m_pPut_buf_func = pPut_buf_func;
2527 d->m_pPut_buf_user = pPut_buf_user;
2528 d->m_flags = (mz_uint)(flags);
2529 d->m_max_probes[0] = 1 + ((flags & 0xFFF) + 2) / 3;
2530 d->m_greedy_parsing = (flags & TDEFL_GREEDY_PARSING_FLAG) != 0;
2531 d->m_max_probes[1] = 1 + (((flags & 0xFFF) >> 2) + 2) / 3;
2532 if (!(flags & TDEFL_NONDETERMINISTIC_PARSING_FLAG))
2533 MZ_CLEAR_OBJ(d->m_hash);
2534 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;
2535 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;
2536 d->m_pLZ_code_buf = d->m_lz_code_buf + 1;
2537 d->m_pLZ_flags = d->m_lz_code_buf;
2538 d->m_num_flags_left = 8;
2539 d->m_pOutput_buf = d->m_output_buf;
2540 d->m_pOutput_buf_end = d->m_output_buf;
2541 d->m_prev_return_status = TDEFL_STATUS_OKAY;
2542 d->m_saved_match_dist = d->m_saved_match_len = d->m_saved_lit = 0;
2544 d->m_pIn_buf = NULL;
2545 d->m_pOut_buf = NULL;
2546 d->m_pIn_buf_size = NULL;
2547 d->m_pOut_buf_size = NULL;
2548 d->m_flush = TDEFL_NO_FLUSH;
2550 d->m_src_buf_left = 0;
2551 d->m_out_buf_ofs = 0;
2552 memset(&d->m_huff_count[0][0], 0, sizeof(d->m_huff_count[0][0]) * TDEFL_MAX_HUFF_SYMBOLS_0);
2553 memset(&d->m_huff_count[1][0], 0, sizeof(d->m_huff_count[1][0]) * TDEFL_MAX_HUFF_SYMBOLS_1);
2554 return TDEFL_STATUS_OKAY;
2557 tdefl_status tdefl_get_prev_return_status(tdefl_compressor *d)
2559 return d->m_prev_return_status;
2562 mz_uint32 tdefl_get_adler32(tdefl_compressor *d)
2564 return d->m_adler32;
2567 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)
2569 tdefl_compressor *pComp;
2571 if (((buf_len) && (!pBuf)) || (!pPut_buf_func))
2573 pComp = (tdefl_compressor *)MZ_MALLOC(sizeof(tdefl_compressor));
2576 succeeded = (tdefl_init(pComp, pPut_buf_func, pPut_buf_user, flags) == TDEFL_STATUS_OKAY);
2577 succeeded = succeeded && (tdefl_compress_buffer(pComp, pBuf, buf_len, TDEFL_FINISH) == TDEFL_STATUS_DONE);
2584 size_t m_size, m_capacity;
2586 mz_bool m_expandable;
2587 } tdefl_output_buffer;
2589 static mz_bool tdefl_output_buffer_putter(const void *pBuf, int len, void *pUser)
2591 tdefl_output_buffer *p = (tdefl_output_buffer *)pUser;
2592 size_t new_size = p->m_size + len;
2593 if (new_size > p->m_capacity)
2595 size_t new_capacity = p->m_capacity;
2597 if (!p->m_expandable)
2601 new_capacity = MZ_MAX(128U, new_capacity << 1U);
2602 } while (new_size > new_capacity);
2603 pNew_buf = (mz_uint8 *)MZ_REALLOC(p->m_pBuf, new_capacity);
2606 p->m_pBuf = pNew_buf;
2607 p->m_capacity = new_capacity;
2609 memcpy((mz_uint8 *)p->m_pBuf + p->m_size, pBuf, len);
2610 p->m_size = new_size;
2614 void *tdefl_compress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, size_t *pOut_len, int flags)
2616 tdefl_output_buffer out_buf;
2617 MZ_CLEAR_OBJ(out_buf);
2622 out_buf.m_expandable = MZ_TRUE;
2623 if (!tdefl_compress_mem_to_output(pSrc_buf, src_buf_len, tdefl_output_buffer_putter, &out_buf, flags))
2625 *pOut_len = out_buf.m_size;
2626 return out_buf.m_pBuf;
2629 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)
2631 tdefl_output_buffer out_buf;
2632 MZ_CLEAR_OBJ(out_buf);
2635 out_buf.m_pBuf = (mz_uint8 *)pOut_buf;
2636 out_buf.m_capacity = out_buf_len;
2637 if (!tdefl_compress_mem_to_output(pSrc_buf, src_buf_len, tdefl_output_buffer_putter, &out_buf, flags))
2639 return out_buf.m_size;
2642 #ifndef MINIZ_NO_ZLIB_APIS
2643 static const mz_uint s_tdefl_num_probes[11] = { 0, 1, 6, 32, 16, 32, 128, 256, 512, 768, 1500 };
2645 // level may actually range from [0,10] (10 is a "hidden" max level, where we want a bit more compression and it's fine if throughput to fall off a cliff on some files).
2646 mz_uint tdefl_create_comp_flags_from_zip_params(int level, int window_bits, int strategy)
2648 mz_uint comp_flags = s_tdefl_num_probes[(level >= 0) ? MZ_MIN(10, level) : MZ_DEFAULT_LEVEL] | ((level <= 3) ? TDEFL_GREEDY_PARSING_FLAG : 0);
2649 if (window_bits > 0)
2650 comp_flags |= TDEFL_WRITE_ZLIB_HEADER;
2653 comp_flags |= TDEFL_FORCE_ALL_RAW_BLOCKS;
2654 else if (strategy == MZ_FILTERED)
2655 comp_flags |= TDEFL_FILTER_MATCHES;
2656 else if (strategy == MZ_HUFFMAN_ONLY)
2657 comp_flags &= ~TDEFL_MAX_PROBES_MASK;
2658 else if (strategy == MZ_FIXED)
2659 comp_flags |= TDEFL_FORCE_ALL_STATIC_BLOCKS;
2660 else if (strategy == MZ_RLE)
2661 comp_flags |= TDEFL_RLE_MATCHES;
2665 #endif //MINIZ_NO_ZLIB_APIS
2668 #pragma warning(push)
2669 #pragma warning(disable : 4204) // nonstandard extension used : non-constant aggregate initializer (also supported by GNU C and C99, so no big deal)
2672 // Simple PNG writer function by Alex Evans, 2011. Released into the public domain: https://gist.github.com/908299, more context at
2673 // http://altdevblogaday.org/2011/04/06/a-smaller-jpg-encoder/.
2674 // This is actually a modification of Alex's original code so PNG files generated by this function pass pngcheck.
2675 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)
2677 // Using a local copy of this array here in case MINIZ_NO_ZLIB_APIS was defined.
2678 static const mz_uint s_tdefl_png_num_probes[11] = { 0, 1, 6, 32, 16, 32, 128, 256, 512, 768, 1500 };
2679 tdefl_compressor *pComp = (tdefl_compressor *)MZ_MALLOC(sizeof(tdefl_compressor));
2680 tdefl_output_buffer out_buf;
2681 int i, bpl = w * num_chans, y, z;
2686 MZ_CLEAR_OBJ(out_buf);
2687 out_buf.m_expandable = MZ_TRUE;
2688 out_buf.m_capacity = 57 + MZ_MAX(64, (1 + bpl) * h);
2689 if (NULL == (out_buf.m_pBuf = (mz_uint8 *)MZ_MALLOC(out_buf.m_capacity)))
2694 // write dummy header
2695 for (z = 41; z; --z)
2696 tdefl_output_buffer_putter(&z, 1, &out_buf);
2697 // compress image data
2698 tdefl_init(pComp, tdefl_output_buffer_putter, &out_buf, s_tdefl_png_num_probes[MZ_MIN(10, level)] | TDEFL_WRITE_ZLIB_HEADER);
2699 for (y = 0; y < h; ++y)
2701 tdefl_compress_buffer(pComp, &z, 1, TDEFL_NO_FLUSH);
2702 tdefl_compress_buffer(pComp, (mz_uint8 *)pImage + (flip ? (h - 1 - y) : y) * bpl, bpl, TDEFL_NO_FLUSH);
2704 if (tdefl_compress_buffer(pComp, NULL, 0, TDEFL_FINISH) != TDEFL_STATUS_DONE)
2707 MZ_FREE(out_buf.m_pBuf);
2710 // write real header
2711 *pLen_out = out_buf.m_size - 41;
2713 static const mz_uint8 chans[] = { 0x00, 0x00, 0x04, 0x02, 0x06 };
2714 mz_uint8 pnghdr[41] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52,
2715 0, 0, (mz_uint8)(w >> 8), (mz_uint8)w, 0, 0, (mz_uint8)(h >> 8), (mz_uint8)h, 8, chans[num_chans], 0, 0, 0, 0, 0, 0, 0,
2716 (mz_uint8)(*pLen_out >> 24), (mz_uint8)(*pLen_out >> 16), (mz_uint8)(*pLen_out >> 8), (mz_uint8) * pLen_out, 0x49, 0x44, 0x41, 0x54 };
2717 c = (mz_uint32)mz_crc32(MZ_CRC32_INIT, pnghdr + 12, 17);
2718 for (i = 0; i < 4; ++i, c <<= 8)
2719 ((mz_uint8 *)(pnghdr + 29))[i] = (mz_uint8)(c >> 24);
2720 memcpy(out_buf.m_pBuf, pnghdr, 41);
2722 // write footer (IDAT CRC-32, followed by IEND chunk)
2723 if (!tdefl_output_buffer_putter("\0\0\0\0\0\0\0\0\x49\x45\x4e\x44\xae\x42\x60\x82", 16, &out_buf))
2727 MZ_FREE(out_buf.m_pBuf);
2730 c = (mz_uint32)mz_crc32(MZ_CRC32_INIT, out_buf.m_pBuf + 41 - 4, *pLen_out + 4);
2731 for (i = 0; i < 4; ++i, c <<= 8)
2732 (out_buf.m_pBuf + out_buf.m_size - 16)[i] = (mz_uint8)(c >> 24);
2733 // compute final size of file, grab compressed data buffer and return
2736 return out_buf.m_pBuf;
2738 void *tdefl_write_image_to_png_file_in_memory(const void *pImage, int w, int h, int num_chans, size_t *pLen_out)
2740 // Level 6 corresponds to TDEFL_DEFAULT_MAX_PROBES or MZ_DEFAULT_LEVEL (but we can't depend on MZ_DEFAULT_LEVEL being available in case the zlib API's where #defined out)
2741 return tdefl_write_image_to_png_file_in_memory_ex(pImage, w, h, num_chans, pLen_out, 6, MZ_FALSE);
2745 #pragma warning(pop)
2753 This is free and unencumbered software released into the public domain.
2755 Anyone is free to copy, modify, publish, use, compile, sell, or
2756 distribute this software, either in source code form or as a compiled
2757 binary, for any purpose, commercial or non-commercial, and by any
2760 In jurisdictions that recognize copyright laws, the author or authors
2761 of this software dedicate any and all copyright interest in the
2762 software to the public domain. We make this dedication for the benefit
2763 of the public at large and to the detriment of our heirs and
2764 successors. We intend this dedication to be an overt act of
2765 relinquishment in perpetuity of all present and future rights to this
2766 software under copyright law.
2768 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2769 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
2770 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
2771 IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
2772 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
2773 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2774 OTHER DEALINGS IN THE SOFTWARE.
2776 For more information, please refer to <http://unlicense.org/>