31 #pragma warning(disable:4548)
33 #pragma warning(disable:4061)
52 size_t block_len = buf_len % 5552;
57 for(i = 0; i + 7 < block_len; i += 8, ptr += 8)
68 for(; i < block_len; ++i)
70 s1 %= 65521U,
s2 %= 65521U;
74 return (
s2 << 16) +
s1;
81 static const mz_uint32 s_crc32[16] = { 0, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
82 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c };
90 crcu32 = (crcu32 >> 4) ^ s_crc32[(crcu32 & 0xF) ^ (b & 0xF)];
91 crcu32 = (crcu32 >> 4) ^ s_crc32[(crcu32 & 0xF) ^ (b >> 4)];
100 static const mz_uint32 s_crc_table[256] =
102 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, 0xE963A535,
103 0x9E6495A3, 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD,
104 0xE7B82D07, 0x90BF1D91, 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE, 0x1ADAD47D,
105 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7, 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC,
106 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5, 0x3B6E20C8, 0x4C69105E, 0xD56041E4,
107 0xA2677172, 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, 0x35B5A8FA, 0x42B2986C,
108 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59, 0x26D930AC,
109 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F,
110 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, 0x2F6F7C87, 0x58684C11, 0xC1611DAB,
111 0xB6662D3D, 0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F,
112 0x9FBFE4A5, 0xE8B8D433, 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB,
113 0x086D3D2D, 0x91646C97, 0xE6635C01, 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E,
114 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, 0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA,
115 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65, 0x4DB26158, 0x3AB551CE,
116 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB, 0x4369E96A,
117 0x346ED9FC, 0xAD678846, 0xDA60B8D0, 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9,
118 0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409,
119 0xCE61E49F, 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81,
120 0xB7BD5C3B, 0xC0BA6CAD, 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, 0xEAD54739,
121 0x9DD277AF, 0x04DB2615, 0x73DC1683, 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8,
122 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1, 0xF00F9344, 0x8708A3D2, 0x1E01F268,
123 0x6906C2FE, 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7, 0xFED41B76, 0x89D32BE0,
124 0x10DA7A5A, 0x67DD4ACC, 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5, 0xD6D6A3E8,
125 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B,
126 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55, 0x316E8EEF,
127 0x4669BE79, 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, 0xCC0C7795, 0xBB0B4703,
128 0x220216B9, 0x5505262F, 0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7,
129 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D, 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A,
130 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713, 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE,
131 0x0CB61B38, 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, 0x86D3D2D4, 0xF1D4E242,
132 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777, 0x88085AE6,
133 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45,
134 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2, 0xA7672661, 0xD06016F7, 0x4969474D,
135 0x3E6E77DB, 0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5,
136 0x47B2CF7F, 0x30B5FFE9, 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605,
137 0xCDD70693, 0x54DE5729, 0x23D967BF, 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94,
138 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D
170 #ifndef MINIZ_NO_ZLIB_APIS
174 (void)opaque, (
void)items, (void)size;
179 (void)opaque, (
void)address;
184 (void)opaque, (
void)address, (void)items, (
void)size;
223 pStream->
state = (
struct mz_internal_state *)pComp;
236 if((!pStream) || (!pStream->
state) || (!pStream->
zalloc) || (!pStream->
zfree))
245 size_t in_bytes, out_bytes;
246 mz_ulong orig_total_in, orig_total_out;
247 int mz_status =
MZ_OK;
292 if((flush) || (pStream->
total_in != orig_total_in) || (pStream->
total_out != orig_total_out))
308 pStream->
state = NULL;
317 return MZ_MAX(128 + (source_len * 110) / 100, 128 + source_len + ((source_len / (31 * 1024)) + 1) * 5);
324 memset(&stream, 0,
sizeof(stream));
327 if((source_len | *pDest_len) > 0xFFFFFFFFU)
392 pStream->
state = (
struct mz_internal_state *)pDecomp;
414 size_t in_bytes, out_bytes, orig_avail_in;
417 if((!pStream) || (!pStream->
state))
529 pStream->
state = NULL;
538 memset(&stream, 0,
sizeof(stream));
541 if((source_len | *pDest_len) > 0xFFFFFFFFU)
572 {
MZ_OK,
"" }, {
MZ_STREAM_END,
"stream end" }, {
MZ_NEED_DICT,
"need dictionary" }, {
MZ_ERRNO,
"file error" }, {
MZ_STREAM_ERROR,
"stream error" },
576 for(i = 0; i <
sizeof(s_error_descs) /
sizeof(s_error_descs[0]); ++i)
577 if(s_error_descs[i].m_err == err)
578 return s_error_descs[i].m_pDesc;
652 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,
653 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,
654 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,
655 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,
656 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,
657 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,
658 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,
659 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
664 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,
665 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,
666 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
667 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 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
672 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,
673 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,
674 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,
675 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,
676 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,
677 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,
678 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,
679 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,
680 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,
681 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,
682 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,
683 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
688 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,
689 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 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,
690 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,
691 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,
692 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
693 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
694 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
695 7, 7, 7, 7, 7, 7, 7, 7
700 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,
701 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,
702 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
707 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,
708 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,
709 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
719 mz_uint32 total_passes = 2, pass_shift, pass, i, hist[256 * 2];
722 for(i = 0; i < num_syms; i++)
726 hist[256 + ((freq >> 8) & 0xFF)]++;
728 while((total_passes > 1) && (num_syms == hist[(total_passes - 1) * 256]))
730 for(pass_shift = 0, pass = 0; pass < total_passes; pass++, pass_shift += 8)
732 const mz_uint32 *pHist = &hist[pass << 8];
733 mz_uint offsets[256], cur_ofs = 0;
734 for(i = 0; i < 256; i++)
736 offsets[i] = cur_ofs;
739 for(i = 0; i < num_syms; i++)
740 pNew_syms[offsets[(pCur_syms[i].m_key >> pass_shift) & 0xFF]++] = pCur_syms[i];
743 pCur_syms = pNew_syms;
753 int root, leaf, next, avbl, used, dpth;
764 for(next = 1; next < n - 1; next++)
766 if(leaf >= n || A[root].m_key < A[leaf].m_key)
773 if(leaf >= n || (root < next && A[root].m_key < A[leaf].m_key))
782 for(next = n - 3; next >= 0; next--)
783 A[next].m_key = A[A[next].m_key].m_key + 1;
790 while(root >= 0 && (
int)A[root].
m_key == dpth)
815 if(code_list_len <= 1)
818 pNum_codes[max_code_size] += pNum_codes[i];
819 for(i = max_code_size; i > 0; i--)
820 total += (((
mz_uint32)pNum_codes[i]) << (max_code_size - i));
821 while(total != (1UL << max_code_size))
823 pNum_codes[max_code_size]--;
824 for(i = max_code_size - 1; i > 0; i--)
828 pNum_codes[i + 1] += 2;
842 for(i = 0; i < table_len; i++)
848 int num_used_syms = 0;
850 for(i = 0; i < table_len; i++)
860 for(i = 0; i < num_used_syms; i++)
861 num_codes[pSyms[i].m_key]++;
867 for(i = 1, j = num_used_syms; i <= code_size_limit; i++)
868 for(l = num_codes[i]; l > 0; l--)
873 for(j = 0, i = 2; i <= code_size_limit; i++)
874 next_code[i] = j = ((j + num_codes[i - 1]) << 1);
876 for(i = 0; i < table_len; i++)
878 mz_uint rev_code = 0, code, code_size;
881 code = next_code[code_size]++;
882 for(l = code_size; l > 0; l--, code >>= 1)
883 rev_code = (rev_code << 1) | (code & 1);
888 #define TDEFL_PUT_BITS(b, l) \
893 MZ_ASSERT(bits <= ((1U << len) - 1U)); \
894 d->m_bit_buffer |= (bits << d->m_bits_in); \
895 d->m_bits_in += len; \
896 while(d->m_bits_in >= 8) \
898 if(d->m_pOutput_buf < d->m_pOutput_buf_end) \
899 *d->m_pOutput_buf++ = (mz_uint8)(d->m_bit_buffer); \
900 d->m_bit_buffer >>= 8; \
906 #define TDEFL_RLE_PREV_CODE_SIZE() \
908 if(rle_repeat_count) \
910 if(rle_repeat_count < 3) \
912 d->m_huff_count[2][prev_code_size] = (mz_uint16)(d->m_huff_count[2][prev_code_size] + rle_repeat_count); \
913 while(rle_repeat_count--) \
914 packed_code_sizes[num_packed_code_sizes++] = prev_code_size; \
918 d->m_huff_count[2][16] = (mz_uint16)(d->m_huff_count[2][16] + 1); \
919 packed_code_sizes[num_packed_code_sizes++] = 16; \
920 packed_code_sizes[num_packed_code_sizes++] = (mz_uint8)(rle_repeat_count - 3); \
922 rle_repeat_count = 0; \
926 #define TDEFL_RLE_ZERO_CODE_SIZE() \
930 if(rle_z_count < 3) \
932 d->m_huff_count[2][0] = (mz_uint16)(d->m_huff_count[2][0] + rle_z_count); \
933 while(rle_z_count--) \
934 packed_code_sizes[num_packed_code_sizes++] = 0; \
936 else if(rle_z_count <= 10) \
938 d->m_huff_count[2][17] = (mz_uint16)(d->m_huff_count[2][17] + 1); \
939 packed_code_sizes[num_packed_code_sizes++] = 17; \
940 packed_code_sizes[num_packed_code_sizes++] = (mz_uint8)(rle_z_count - 3); \
944 d->m_huff_count[2][18] = (mz_uint16)(d->m_huff_count[2][18] + 1); \
945 packed_code_sizes[num_packed_code_sizes++] = 18; \
946 packed_code_sizes[num_packed_code_sizes++] = (mz_uint8)(rle_z_count - 11); \
952 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 };
956 int num_lit_codes, num_dist_codes, num_bit_lengths;
957 mz_uint i, total_code_sizes_to_pack, num_packed_code_sizes, rle_z_count, rle_repeat_count, packed_code_sizes_index;
965 for(num_lit_codes = 286; num_lit_codes > 257; num_lit_codes--)
968 for(num_dist_codes = 30; num_dist_codes > 1; num_dist_codes--)
973 memcpy(code_sizes_to_pack + num_lit_codes, &d->
m_huff_code_sizes[1][0], num_dist_codes);
974 total_code_sizes_to_pack = num_lit_codes + num_dist_codes;
975 num_packed_code_sizes = 0;
977 rle_repeat_count = 0;
980 for(i = 0; i < total_code_sizes_to_pack; i++)
982 mz_uint8 code_size = code_sizes_to_pack[i];
986 if(++rle_z_count == 138)
994 if(code_size != prev_code_size)
998 packed_code_sizes[num_packed_code_sizes++] = code_size;
1000 else if(++rle_repeat_count == 6)
1005 prev_code_size = code_size;
1007 if(rle_repeat_count)
1023 for(num_bit_lengths = 18; num_bit_lengths >= 0; num_bit_lengths--)
1026 num_bit_lengths =
MZ_MAX(4, (num_bit_lengths + 1));
1028 for(i = 0; (int)i < num_bit_lengths; i++)
1031 for(packed_code_sizes_index = 0; packed_code_sizes_index < num_packed_code_sizes;)
1033 mz_uint code = packed_code_sizes[packed_code_sizes_index++];
1037 TDEFL_PUT_BITS(packed_code_sizes[packed_code_sizes_index++],
"\02\03\07"[code - 16]);
1046 for(i = 0; i <= 143; ++i)
1048 for(; i <= 255; ++i)
1050 for(; i <= 279; ++i)
1052 for(; i <= 287; ++i)
1063 static const mz_uint mz_bitmasks[17] = { 0x0000, 0x0001, 0x0003, 0x0007, 0x000F, 0x001F, 0x003F, 0x007F, 0x00FF, 0x01FF, 0x03FF, 0x07FF, 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF };
1065 #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES &&MINIZ_LITTLE_ENDIAN &&MINIZ_HAS_64BIT_REGISTERS
1075 #define TDEFL_PUT_BITS_FAST(b, l) \
1077 bit_buffer |= (((mz_uint64)(b)) << bits_in); \
1082 for(pLZ_codes = d->
m_lz_code_buf; pLZ_codes < pLZ_code_buf_end; flags >>= 1)
1085 flags = *pLZ_codes++ | 0x100;
1089 mz_uint s0,
s1, n0, n1, sym, num_extra_bits;
1090 mz_uint match_len = pLZ_codes[0], match_dist = *(
const mz_uint16 *)(pLZ_codes + 1);
1102 sym = (match_dist < 512) ? s0 :
s1;
1103 num_extra_bits = (match_dist < 512) ? n0 : n1;
1107 TDEFL_PUT_BITS_FAST(match_dist &
mz_bitmasks[num_extra_bits], num_extra_bits);
1115 if(((flags & 2) == 0) && (pLZ_codes < pLZ_code_buf_end))
1122 if(((flags & 2) == 0) && (pLZ_codes < pLZ_code_buf_end))
1136 pOutput_buf += (bits_in >> 3);
1137 bit_buffer >>= (bits_in & ~7);
1141 #undef TDEFL_PUT_BITS_FAST
1166 for(pLZ_codes = d->
m_lz_code_buf; pLZ_codes < d->m_pLZ_code_buf; flags >>= 1)
1169 flags = *pLZ_codes++ | 0x100;
1173 mz_uint match_len = pLZ_codes[0], match_dist = (pLZ_codes[1] | (pLZ_codes[2] << 8));
1180 if(match_dist < 512)
1219 mz_uint saved_bit_buf, saved_bits_in;
1272 else if(!comp_block_succeeded)
1290 for(i = 0; i < 4; i++)
1305 for(i = 2; i; --i, z ^= 0xFFFF)
1337 if((n -= bytes_to_copy) != 0)
1352 #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES
1353 #define TDEFL_READ_UNALIGNED_WORD(p) *(const mz_uint16 *)(p)
1359 mz_uint16 c01 = TDEFL_READ_UNALIGNED_WORD(&d->
m_dict[
pos + match_len - 1]), s01 = TDEFL_READ_UNALIGNED_WORD(s);
1361 if(max_match_len <= match_len)
1367 if(--num_probes_left == 0)
1369 #define TDEFL_PROBE \
1370 next_probe_pos = d->m_next[probe_pos]; \
1371 if((!next_probe_pos) || ((dist = (mz_uint16)(lookahead_pos - next_probe_pos)) > max_dist)) \
1373 probe_pos = next_probe_pos & TDEFL_LZ_DICT_SIZE_MASK; \
1374 if(TDEFL_READ_UNALIGNED_WORD(&d->m_dict[probe_pos + match_len - 1]) == c01) \
1383 if(TDEFL_READ_UNALIGNED_WORD(q) != s01)
1389 }
while((TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) && (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) &&
1390 (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) && (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) && (--probe_len > 0));
1393 *pMatch_dist = dist;
1399 *pMatch_dist = dist;
1400 if((*pMatch_len = match_len =
MZ_MIN(max_match_len, probe_len)) == max_match_len)
1402 c01 = TDEFL_READ_UNALIGNED_WORD(&d->
m_dict[
pos + match_len - 1]);
1414 if(max_match_len <= match_len)
1420 if(--num_probes_left == 0)
1422 #define TDEFL_PROBE \
1423 next_probe_pos = d->m_next[probe_pos]; \
1424 if((!next_probe_pos) || ((dist = (mz_uint16)(lookahead_pos - next_probe_pos)) > max_dist)) \
1426 probe_pos = next_probe_pos & TDEFL_LZ_DICT_SIZE_MASK; \
1427 if((d->m_dict[probe_pos + match_len] == c0) && (d->m_dict[probe_pos + match_len - 1] == c1)) \
1436 q = d->
m_dict + probe_pos;
1437 for(probe_len = 0; probe_len < max_match_len; probe_len++)
1440 if(probe_len > match_len)
1442 *pMatch_dist = dist;
1443 if((*pMatch_len = match_len = probe_len) == max_match_len)
1452 #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES &&MINIZ_LITTLE_ENDIAN
1462 const mz_uint TDEFL_COMP_FAST_LOOKAHEAD_SIZE = 4096;
1466 lookahead_size += num_bytes_to_process;
1468 while(num_bytes_to_process)
1476 num_bytes_to_process -= n;
1480 if((!d->
m_flush) && (lookahead_size < TDEFL_COMP_FAST_LOOKAHEAD_SIZE))
1483 while(lookahead_size >= 4)
1485 mz_uint cur_match_dist, cur_match_len = 1;
1499 }
while((TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) && (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) &&
1500 (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) && (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) && (--probe_len > 0));
1508 *pLZ_code_buf++ = (
mz_uint8)first_trigram;
1509 *pLZ_flags = (
mz_uint8)(*pLZ_flags >> 1);
1515 cur_match_len =
MZ_MIN(cur_match_len, lookahead_size);
1524 *pLZ_flags = (
mz_uint8)((*pLZ_flags >> 1) | 0x80);
1535 *pLZ_code_buf++ = (
mz_uint8)first_trigram;
1536 *pLZ_flags = (
mz_uint8)(*pLZ_flags >> 1);
1540 if(--num_flags_left == 0)
1543 pLZ_flags = pLZ_code_buf++;
1546 total_lz_bytes += cur_match_len;
1547 lookahead_pos += cur_match_len;
1550 MZ_ASSERT(lookahead_size >= cur_match_len);
1551 lookahead_size -= cur_match_len;
1572 while(lookahead_size)
1577 *pLZ_code_buf++ = lit;
1578 *pLZ_flags = (
mz_uint8)(*pLZ_flags >> 1);
1579 if(--num_flags_left == 0)
1582 pLZ_flags = pLZ_code_buf++;
1674 mz_uint len_to_move, cur_match_dist, cur_match_len, cur_pos;
1681 const mz_uint8 *pSrc_end = pSrc + num_bytes_to_process;
1682 src_buf_left -= num_bytes_to_process;
1684 while(pSrc != pSrc_end)
1731 while(cur_match_len < d->m_lookahead_size)
1733 if(d->
m_dict[cur_pos + cur_match_len] != c)
1749 cur_match_dist = cur_match_len = 0;
1756 if(cur_match_len >= 128)
1760 len_to_move = cur_match_len;
1776 else if(!cur_match_dist)
1781 len_to_move = cur_match_len;
1853 (d->
m_wants_to_finish && (flush !=
TDEFL_FINISH)) || (pIn_buf_size && *pIn_buf_size && !pIn_buf) || (pOut_buf_size && *pOut_buf_size && !pOut_buf))
1866 #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES &&MINIZ_LITTLE_ENDIAN
1871 if(!tdefl_compress_fast(d))
1903 return tdefl_compress(d, pIn_buf, &in_buf_size, NULL, NULL, flush);
1913 d->
m_max_probes[1] = 1 + (((flags & 0xFFF) >> 2) + 2) / 3;
1953 if(((buf_len) && (!pBuf)) || (!pPut_buf_func))
1974 size_t new_size = p->
m_size + len;
1983 new_capacity =
MZ_MAX(128U, new_capacity << 1U);
1984 }
while(new_size > new_capacity);
2007 *pOut_len = out_buf.
m_size;
2024 #ifndef MINIZ_NO_ZLIB_APIS
2025 static const mz_uint s_tdefl_num_probes[11] = { 0, 1, 6, 32, 16, 32, 128, 256, 512, 768, 1500 };
2042 else if(strategy ==
MZ_RLE)
2050 #pragma warning(push)
2051 #pragma warning(disable : 4204)
2060 static const mz_uint s_tdefl_png_num_probes[11] = { 0, 1, 6, 32, 16, 32, 128, 256, 512, 768, 1500 };
2063 int i, bpl = w * num_chans, y, z;
2081 for(y = 0; y < h; ++y)
2093 *pLen_out = out_buf.
m_size - 41;
2095 static const mz_uint8 chans[] = { 0x00, 0x00, 0x04, 0x02, 0x06 };
2096 mz_uint8 pnghdr[41] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52,
2097 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,
2100 for(i = 0; i < 4; ++i, c <<= 8)
2102 memcpy(out_buf.
m_pBuf, pnghdr, 41);
2113 for(i = 0; i < 4; ++i, c <<= 8)
2140 #pragma warning(pop)
2180 #define TINFL_MEMCPY(d, s, l) memcpy(d, s, l)
2181 #define TINFL_MEMSET(p, c, l) memset(p, c, l)
2183 #define TINFL_CR_BEGIN \
2184 switch(r->m_state) \
2187 #define TINFL_CR_RETURN(state_index, result) \
2191 r->m_state = state_index; \
2197 #define TINFL_CR_RETURN_FOREVER(state_index, result) \
2202 TINFL_CR_RETURN(state_index, result); \
2206 #define TINFL_CR_FINISH }
2208 #define TINFL_GET_BYTE(state_index, c) \
2211 while(pIn_buf_cur >= pIn_buf_end) \
2213 TINFL_CR_RETURN(state_index, (decomp_flags &TINFL_FLAG_HAS_MORE_INPUT) ? TINFL_STATUS_NEEDS_MORE_INPUT : TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS); \
2215 c = *pIn_buf_cur++; \
2219 #define TINFL_NEED_BITS(state_index, n) \
2223 TINFL_GET_BYTE(state_index, c); \
2224 bit_buf |= (((tinfl_bit_buf_t)c) << num_bits); \
2226 } while(num_bits < (mz_uint)(n))
2227 #define TINFL_SKIP_BITS(state_index, n) \
2230 if(num_bits < (mz_uint)(n)) \
2232 TINFL_NEED_BITS(state_index, n); \
2238 #define TINFL_GET_BITS(state_index, b, n) \
2241 if(num_bits < (mz_uint)(n)) \
2243 TINFL_NEED_BITS(state_index, n); \
2245 b = bit_buf & ((1 << (n)) - 1); \
2255 #define TINFL_HUFF_BITBUF_FILL(state_index, pHuff) \
2258 temp = (pHuff)->m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]; \
2261 code_len = temp >> 9; \
2262 if((code_len) && (num_bits >= code_len)) \
2265 else if(num_bits > TINFL_FAST_LOOKUP_BITS) \
2267 code_len = TINFL_FAST_LOOKUP_BITS; \
2270 temp = (pHuff)->m_tree[~temp + ((bit_buf >> code_len++) & 1)]; \
2271 } while((temp < 0) && (num_bits >= (code_len + 1))); \
2275 TINFL_GET_BYTE(state_index, c); \
2276 bit_buf |= (((tinfl_bit_buf_t)c) << num_bits); \
2278 } while(num_bits < 15);
2286 #define TINFL_HUFF_DECODE(state_index, sym, pHuff) \
2290 mz_uint code_len, c; \
2293 if((pIn_buf_end - pIn_buf_cur) < 2) \
2295 TINFL_HUFF_BITBUF_FILL(state_index, pHuff); \
2299 bit_buf |= (((tinfl_bit_buf_t)pIn_buf_cur[0]) << num_bits) | (((tinfl_bit_buf_t)pIn_buf_cur[1]) << (num_bits + 8)); \
2304 if((temp = (pHuff)->m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0) \
2305 code_len = temp >> 9, temp &= 511; \
2308 code_len = TINFL_FAST_LOOKUP_BITS; \
2311 temp = (pHuff)->m_tree[~temp + ((bit_buf >> code_len++) & 1)]; \
2312 } while(temp < 0); \
2315 bit_buf >>= code_len; \
2316 num_bits -= code_len; \
2322 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 };
2323 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 };
2324 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 };
2325 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 };
2326 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 };
2327 static const int s_min_table_sizes[3] = { 257, 1, 4 };
2330 mz_uint32 num_bits, dist, counter, num_extra;
2332 const mz_uint8 *pIn_buf_cur = pIn_buf_next, *
const pIn_buf_end = pIn_buf_next + *pIn_buf_size;
2333 mz_uint8 *pOut_buf_cur = pOut_buf_next, *
const pOut_buf_end = pOut_buf_next + *pOut_buf_size;
2337 if(((out_buf_size_mask + 1) & out_buf_size_mask) || (pOut_buf_next < pOut_buf_start))
2339 *pIn_buf_size = *pOut_buf_size = 0;
2343 num_bits =
r->m_num_bits;
2344 bit_buf =
r->m_bit_buf;
2346 counter =
r->m_counter;
2347 num_extra =
r->m_num_extra;
2348 dist_from_out_buf_start =
r->m_dist_from_out_buf_start;
2351 bit_buf = num_bits = dist = counter = num_extra =
r->m_zhdr0 =
r->m_zhdr1 = 0;
2352 r->m_z_adler32 =
r->m_check_adler32 = 1;
2357 counter = (((
r->m_zhdr0 * 256 +
r->m_zhdr1) % 31 != 0) || (
r->m_zhdr1 & 32) || ((
r->m_zhdr0 & 15) != 8));
2359 counter |= (((1U << (8U + (
r->m_zhdr0 >> 4))) > 32768U) || ((out_buf_size_mask + 1) < (size_t)(1U << (8U + (
r->m_zhdr0 >> 4)))));
2369 r->m_type =
r->m_final >> 1;
2373 for(counter = 0; counter < 4; ++counter)
2380 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))))
2384 while((counter) && (num_bits))
2387 while(pOut_buf_cur >= pOut_buf_end)
2397 while(pOut_buf_cur >= pOut_buf_end)
2401 while(pIn_buf_cur >= pIn_buf_end)
2405 n =
MZ_MIN(
MZ_MIN((
size_t)(pOut_buf_end - pOut_buf_cur), (
size_t)(pIn_buf_end - pIn_buf_cur)), counter);
2412 else if(
r->m_type == 3)
2420 mz_uint8 *p =
r->m_tables[0].m_code_size;
2422 r->m_table_sizes[0] = 288;
2423 r->m_table_sizes[1] = 32;
2425 for(i = 0; i <= 143; ++i)
2427 for(; i <= 255; ++i)
2429 for(; i <= 279; ++i)
2431 for(; i <= 287; ++i)
2436 for(counter = 0; counter < 3; counter++)
2439 r->m_table_sizes[counter] += s_min_table_sizes[counter];
2442 for(counter = 0; counter <
r->m_table_sizes[2]; counter++)
2446 r->m_tables[2].m_code_size[s_length_dezigzag[counter]] = (
mz_uint8)s;
2448 r->m_table_sizes[2] = 19;
2450 for(; (int)
r->m_type >= 0;
r->m_type--)
2452 int tree_next, tree_cur;
2454 mz_uint i, j, used_syms, total, sym_index, next_code[17], total_syms[16];
2455 pTable = &
r->m_tables[
r->m_type];
2459 for(i = 0; i <
r->m_table_sizes[
r->m_type]; ++i)
2461 used_syms = 0, total = 0;
2462 next_code[0] = next_code[1] = 0;
2463 for(i = 1; i <= 15; ++i)
2465 used_syms += total_syms[i];
2466 next_code[i + 1] = (total = ((total + total_syms[i]) << 1));
2468 if((65536 != total) && (used_syms > 1))
2472 for(tree_next = -1, sym_index = 0; sym_index <
r->m_table_sizes[
r->m_type]; ++sym_index)
2477 cur_code = next_code[code_size]++;
2478 for(l = code_size; l > 0; l--, cur_code >>= 1)
2479 rev_code = (rev_code << 1) | (cur_code & 1);
2486 rev_code += (1 << code_size);
2493 tree_cur = tree_next;
2499 tree_cur -= ((rev_code >>= 1) & 1);
2500 if(!pTable->
m_tree[-tree_cur - 1])
2503 tree_cur = tree_next;
2507 tree_cur = pTable->
m_tree[-tree_cur - 1];
2509 tree_cur -= ((rev_code >>= 1) & 1);
2514 for(counter = 0; counter < (
r->m_table_sizes[0] +
r->m_table_sizes[1]);)
2520 r->m_len_codes[counter++] = (
mz_uint8)dist;
2523 if((dist == 16) && (!counter))
2527 num_extra =
"\02\03\07"[dist - 16];
2529 s +=
"\03\03\013"[dist - 16];
2530 TINFL_MEMSET(
r->m_len_codes + counter, (dist == 16) ?
r->m_len_codes[counter - 1] : 0, s);
2533 if((
r->m_table_sizes[0] +
r->m_table_sizes[1]) != counter)
2537 TINFL_MEMCPY(
r->m_tables[0].m_code_size,
r->m_len_codes,
r->m_table_sizes[0]);
2538 TINFL_MEMCPY(
r->m_tables[1].m_code_size,
r->m_len_codes +
r->m_table_sizes[0],
r->m_table_sizes[1]);
2546 if(((pIn_buf_end - pIn_buf_cur) < 4) || ((pOut_buf_end - pOut_buf_cur) < 2))
2551 while(pOut_buf_cur >= pOut_buf_end)
2555 *pOut_buf_cur++ = (
mz_uint8)counter;
2561 #if TINFL_USE_64BIT_BITBUF
2577 code_len = sym2 >> 9;
2583 sym2 =
r->m_tables[0].m_tree[~sym2 + ((bit_buf >> code_len++) & 1)];
2587 bit_buf >>= code_len;
2588 num_bits -= code_len;
2592 #if !TINFL_USE_64BIT_BITBUF
2601 code_len = sym2 >> 9;
2607 sym2 =
r->m_tables[0].m_tree[~sym2 + ((bit_buf >> code_len++) & 1)];
2610 bit_buf >>= code_len;
2611 num_bits -= code_len;
2613 pOut_buf_cur[0] = (
mz_uint8)counter;
2624 if((counter &= 511) == 256)
2627 num_extra = s_length_extra[counter - 257];
2628 counter = s_length_base[counter - 257];
2633 counter += extra_bits;
2637 num_extra = s_dist_extra[dist];
2638 dist = s_dist_base[dist];
2646 dist_from_out_buf_start = pOut_buf_cur - pOut_buf_start;
2652 pSrc = pOut_buf_start + ((dist_from_out_buf_start - dist) & out_buf_size_mask);
2654 if((
MZ_MAX(pOut_buf_cur, pSrc) + counter) > pOut_buf_end)
2658 while(pOut_buf_cur >= pOut_buf_end)
2662 *pOut_buf_cur++ = pOut_buf_start[(dist_from_out_buf_start++ - dist) & out_buf_size_mask];
2666 #if MINIZ_USE_UNALIGNED_LOADS_AND_STORES
2667 else if((counter >= 9) && (counter <= dist))
2669 const mz_uint8 *pSrc_end = pSrc + (counter & ~7);
2675 }
while((pSrc += 8) < pSrc_end);
2676 if((counter &= 7) < 3)
2680 pOut_buf_cur[0] = pSrc[0];
2682 pOut_buf_cur[1] = pSrc[1];
2683 pOut_buf_cur += counter;
2691 pOut_buf_cur[0] = pSrc[0];
2692 pOut_buf_cur[1] = pSrc[1];
2693 pOut_buf_cur[2] = pSrc[2];
2696 }
while((
int)(counter -= 3) > 2);
2697 if((
int)counter > 0)
2699 pOut_buf_cur[0] = pSrc[0];
2700 if((
int)counter > 1)
2701 pOut_buf_cur[1] = pSrc[1];
2702 pOut_buf_cur += counter;
2706 }
while(!(
r->m_final & 1));
2711 while((pIn_buf_cur > pIn_buf_next) && (num_bits >= 8))
2721 for(counter = 0; counter < 4; ++counter)
2728 r->m_z_adler32 = (
r->m_z_adler32 << 8) | s;
2741 while((pIn_buf_cur > pIn_buf_next) && (num_bits >= 8))
2747 r->m_num_bits = num_bits;
2750 r->m_counter = counter;
2751 r->m_num_extra = num_extra;
2752 r->m_dist_from_out_buf_start = dist_from_out_buf_start;
2753 *pIn_buf_size = pIn_buf_cur - pIn_buf_next;
2754 *pOut_buf_size = pOut_buf_cur - pOut_buf_next;
2757 const mz_uint8 *ptr = pOut_buf_next;
2758 size_t buf_len = *pOut_buf_size;
2759 mz_uint32 i,
s1 =
r->m_check_adler32 & 0xffff,
s2 =
r->m_check_adler32 >> 16;
2760 size_t block_len = buf_len % 5552;
2763 for(i = 0; i + 7 < block_len; i += 8, ptr += 8)
2774 for(; i < block_len; ++i)
2776 s1 %= 65521U,
s2 %= 65521U;
2777 buf_len -= block_len;
2780 r->m_check_adler32 = (
s2 << 16) +
s1;
2791 void *pBuf = NULL, *pNew_buf;
2792 size_t src_buf_ofs = 0, out_buf_capacity = 0;
2797 size_t src_buf_size = src_buf_len - src_buf_ofs, dst_buf_size = out_buf_capacity - *pOut_len, new_out_buf_capacity;
2806 src_buf_ofs += src_buf_size;
2807 *pOut_len += dst_buf_size;
2810 new_out_buf_capacity = out_buf_capacity * 2;
2811 if(new_out_buf_capacity < 128)
2812 new_out_buf_capacity = 128;
2813 pNew_buf =
MZ_REALLOC(pBuf, new_out_buf_capacity);
2821 out_buf_capacity = new_out_buf_capacity;
2840 size_t in_buf_ofs = 0, dict_ofs = 0;
2846 size_t in_buf_size = *pIn_buf_size - in_buf_ofs, dst_buf_size =
TINFL_LZ_DICT_SIZE - dict_ofs;
2849 in_buf_ofs += in_buf_size;
2850 if((dst_buf_size) && (!(*pPut_buf_func)(pDict + dict_ofs, (
int)dst_buf_size, pPut_buf_user)))
2860 *pIn_buf_size = in_buf_ofs;
2914 #ifdef MINIZ_NO_STDIO
2915 #define MZ_FILE void *
2917 #include <sys/stat.h>
2919 #if defined(_MSC_VER) || defined(__MINGW64__)
2920 static FILE *mz_fopen(
const char *pFilename,
const char *pMode)
2923 fopen_s(&pFile, pFilename, pMode);
2926 static FILE *mz_freopen(
const char *pPath,
const char *pMode, FILE *pStream)
2929 if(freopen_s(&pFile, pPath, pMode, pStream))
2933 #ifndef MINIZ_NO_TIME
2934 #include <sys/utime.h>
2936 #define MZ_FOPEN mz_fopen
2937 #define MZ_FCLOSE fclose
2938 #define MZ_FREAD fread
2939 #define MZ_FWRITE fwrite
2940 #define MZ_FTELL64 _ftelli64
2941 #define MZ_FSEEK64 _fseeki64
2942 #define MZ_FILE_STAT_STRUCT _stat
2943 #define MZ_FILE_STAT _stat
2944 #define MZ_FFLUSH fflush
2945 #define MZ_FREOPEN mz_freopen
2946 #define MZ_DELETE_FILE remove
2947 #elif defined(__MINGW32__)
2948 #ifndef MINIZ_NO_TIME
2949 #include <sys/utime.h>
2951 #define MZ_FOPEN(f, m) fopen(f, m)
2952 #define MZ_FCLOSE fclose
2953 #define MZ_FREAD fread
2954 #define MZ_FWRITE fwrite
2955 #define MZ_FTELL64 ftello64
2956 #define MZ_FSEEK64 fseeko64
2957 #define MZ_FILE_STAT_STRUCT _stat
2958 #define MZ_FILE_STAT _stat
2959 #define MZ_FFLUSH fflush
2960 #define MZ_FREOPEN(f, m, s) freopen(f, m, s)
2961 #define MZ_DELETE_FILE remove
2962 #elif defined(__TINYC__)
2963 #ifndef MINIZ_NO_TIME
2964 #include <sys/utime.h>
2966 #define MZ_FOPEN(f, m) fopen(f, m)
2967 #define MZ_FCLOSE fclose
2968 #define MZ_FREAD fread
2969 #define MZ_FWRITE fwrite
2970 #define MZ_FTELL64 ftell
2971 #define MZ_FSEEK64 fseek
2972 #define MZ_FILE_STAT_STRUCT stat
2973 #define MZ_FILE_STAT stat
2974 #define MZ_FFLUSH fflush
2975 #define MZ_FREOPEN(f, m, s) freopen(f, m, s)
2976 #define MZ_DELETE_FILE remove
2977 #elif defined(__GNUC__) && _LARGEFILE64_SOURCE
2978 #ifndef MINIZ_NO_TIME
2981 #define MZ_FOPEN(f, m) fopen64(f, m)
2982 #define MZ_FCLOSE fclose
2983 #define MZ_FREAD fread
2984 #define MZ_FWRITE fwrite
2985 #define MZ_FTELL64 ftello64
2986 #define MZ_FSEEK64 fseeko64
2987 #define MZ_FILE_STAT_STRUCT stat64
2988 #define MZ_FILE_STAT stat64
2989 #define MZ_FFLUSH fflush
2990 #define MZ_FREOPEN(p, m, s) freopen64(p, m, s)
2991 #define MZ_DELETE_FILE remove
2992 #elif defined(__APPLE__) && _LARGEFILE64_SOURCE
2993 #ifndef MINIZ_NO_TIME
2996 #define MZ_FOPEN(f, m) fopen(f, m)
2997 #define MZ_FCLOSE fclose
2998 #define MZ_FREAD fread
2999 #define MZ_FWRITE fwrite
3000 #define MZ_FTELL64 ftello
3001 #define MZ_FSEEK64 fseeko
3002 #define MZ_FILE_STAT_STRUCT stat
3003 #define MZ_FILE_STAT stat
3004 #define MZ_FFLUSH fflush
3005 #define MZ_FREOPEN(p, m, s) freopen(p, m, s)
3006 #define MZ_DELETE_FILE remove
3009 #pragma message("Using fopen, ftello, fseeko, stat() etc. path for file I/O - this path may not support large files.")
3010 #ifndef MINIZ_NO_TIME
3013 #define MZ_FOPEN(f, m) fopen(f, m)
3014 #define MZ_FCLOSE fclose
3015 #define MZ_FREAD fread
3016 #define MZ_FWRITE fwrite
3017 #define MZ_FTELL64 ftello
3018 #define MZ_FSEEK64 fseeko
3019 #define MZ_FILE_STAT_STRUCT stat
3020 #define MZ_FILE_STAT stat
3021 #define MZ_FFLUSH fflush
3022 #define MZ_FREOPEN(f, m, s) freopen(f, m, s)
3023 #define MZ_DELETE_FILE remove
3027 #define MZ_TOLOWER(c) ((((c) >= 'A') && ((c) <= 'Z')) ? ((c) - 'A' + 'a') : (c))
3150 #define MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(array_ptr, element_size) (array_ptr)->m_element_size = element_size
3152 #if defined(DEBUG) || defined(_DEBUG) || defined(NDEBUG)
3158 #define MZ_ZIP_ARRAY_ELEMENT(array_ptr, element_type, index) ((element_type *)((array_ptr)->m_p))[mz_zip_array_range_check(array_ptr, index)]
3160 #define MZ_ZIP_ARRAY_ELEMENT(array_ptr, element_type, index) ((element_type *)((array_ptr)->m_p))[index]
3178 size_t new_capacity = min_new_capacity;
3185 while(new_capacity < min_new_capacity)
3190 pArray->
m_p = pNew_p;
3212 pArray->
m_size = new_size;
3223 size_t orig_size = pArray->
m_size;
3230 #ifndef MINIZ_NO_TIME
3234 memset(&tm, 0,
sizeof(tm));
3236 tm.tm_year = ((dos_date >> 9) & 127) + 1980 - 1900;
3237 tm.tm_mon = ((dos_date >> 5) & 15) - 1;
3238 tm.tm_mday = dos_date & 31;
3239 tm.tm_hour = (dos_time >> 11) & 31;
3240 tm.tm_min = (dos_time >> 5) & 63;
3241 tm.tm_sec = (dos_time << 1) & 62;
3248 struct tm tm_struct;
3249 struct tm *tm = &tm_struct;
3250 errno_t err = localtime_s(tm, &time);
3258 struct tm *tm = localtime(&time);
3261 *pDOS_time = (
mz_uint16)(((tm->tm_hour) << 11) + ((tm->tm_min) << 5) + ((tm->tm_sec) >> 1));
3262 *pDOS_date = (
mz_uint16)(((tm->tm_year + 1900 - 1980) << 9) + ((tm->tm_mon + 1) << 5) + tm->tm_mday);
3265 #ifndef MINIZ_NO_STDIO
3274 *pTime = file_stat.st_mtime;
3283 memset(&t, 0,
sizeof(t));
3284 t.actime = access_time;
3285 t.modtime = modified_time;
3287 return !utime(pFilename, &t);
3341 pE = pL +
MZ_MIN(l_len, r_len);
3349 return (pL == pE) ? (l_len < r_len) : (l <
r);
3352 #define MZ_SWAP_UINT32(a, b) \
3376 start = (size - 2U) >> 1U;
3382 if((child = (root << 1U) + 1U) >= size)
3384 child += (((child + 1U) < size) && (
mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, pIndices[child], pIndices[child + 1U])));
3402 if((child = (root << 1U) + 1U) >= end)
3404 child += (((child + 1U) < end) &&
mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, pIndices[child], pIndices[child + 1U]));
3433 for(i = n - 4; i >= 0; --i)
3453 cur_file_ofs =
MZ_MAX(cur_file_ofs - (
sizeof(buf_u32) - 3), 0);
3456 *pOfs = cur_file_ofs;
3462 mz_uint cdir_size = 0, cdir_entries_on_this_disk = 0, num_this_disk = 0, cdir_disk_index = 0;
3471 mz_uint8 *pZip64_locator = (
mz_uint8 *)zip64_end_of_central_dir_locator_u32;
3474 mz_uint8 *pZip64_end_of_central_dir = (
mz_uint8 *)zip64_end_of_central_dir_header_u32;
3476 mz_uint64 zip64_end_of_central_dir_ofs = 0;
3531 if(zip64_total_num_of_disks != 1U)
3543 cdir_entries_on_this_disk = (
mz_uint32)zip64_cdir_total_entries_on_this_disk;
3549 cdir_size = (
mz_uint32)zip64_size_of_central_directory;
3561 if(((num_this_disk | cdir_disk_index) != 0) && ((num_this_disk != 1) || (cdir_disk_index != 1)))
3580 if(sort_central_dir)
3591 for(n = cdir_size, i = 0; i < pZip->m_total_files; ++i)
3593 mz_uint total_header_size, disk_index, bit_flags, filename_size, ext_data_size;
3594 mz_uint64 comp_size, decomp_size, local_header_ofs;
3601 if(sort_central_dir)
3615 mz_uint32 extra_size_remaining = ext_data_size;
3617 if(extra_size_remaining)
3623 if(extra_size_remaining < (
sizeof(
mz_uint16) * 2))
3629 if((field_data_size +
sizeof(
mz_uint16) * 2) > extra_size_remaining)
3640 pExtra_data +=
sizeof(
mz_uint16) * 2 + field_data_size;
3641 extra_size_remaining = extra_size_remaining -
sizeof(
mz_uint16) * 2 - field_data_size;
3642 }
while(extra_size_remaining);
3654 if((disk_index ==
MZ_UINT16_MAX) || ((disk_index != num_this_disk) && (disk_index != 1)))
3670 n -= total_header_size;
3671 p += total_header_size;
3675 if(sort_central_dir)
3711 #ifndef MINIZ_NO_STDIO
3740 if((!pZip) || (!pZip->
m_pRead))
3799 #ifndef MINIZ_NO_STDIO
3828 file_size = archive_size;
3871 if((!pZip) || (!pFile))
3881 archive_size =
MZ_FTELL64(pFile) - cur_file_ofs;
3968 mz_uint filename_len, attribute_mapping_id, external_attr;
3987 (void)attribute_mapping_id;
4001 const mz_uint8 *p = pCentral_dir_header;
4003 if(pFound_zip64_extra_data)
4004 *pFound_zip64_extra_data =
MZ_FALSE;
4006 if((!p) || (!pStat))
4016 #ifndef MINIZ_NO_TIME
4050 if(extra_size_remaining)
4056 if(extra_size_remaining < (
sizeof(
mz_uint16) * 2))
4062 if((field_data_size +
sizeof(
mz_uint16) * 2) > extra_size_remaining)
4068 mz_uint32 field_data_remaining = field_data_size;
4070 if(pFound_zip64_extra_data)
4071 *pFound_zip64_extra_data =
MZ_TRUE;
4075 if(field_data_remaining <
sizeof(
mz_uint64))
4080 field_data_remaining -=
sizeof(
mz_uint64);
4085 if(field_data_remaining <
sizeof(
mz_uint64))
4090 field_data_remaining -=
sizeof(
mz_uint64);
4095 if(field_data_remaining <
sizeof(
mz_uint64))
4100 field_data_remaining -=
sizeof(
mz_uint64);
4106 pExtra_data +=
sizeof(
mz_uint16) * 2 + field_data_size;
4107 extra_size_remaining = extra_size_remaining -
sizeof(
mz_uint16) * 2 - field_data_size;
4108 }
while(extra_size_remaining);
4119 return 0 == memcmp(pA, pB, len);
4120 for(i = 0; i < len; ++i)
4132 pE = pL +
MZ_MIN(l_len, r_len);
4140 return (pL == pE) ? (int)(l_len - r_len) : (l -
r);
4164 uint32_t file_index = pIndices[(uint32_t)m];
4170 *pIndex = file_index;
4195 size_t name_len, comment_len;
4200 if((!pZip) || (!pZip->
m_pState) || (!pName))
4212 name_len = strlen(pName);
4216 comment_len = pComment ? strlen(pComment) : 0;
4220 for(file_index = 0; file_index < pZip->
m_total_files; file_index++)
4225 if(filename_len < name_len)
4230 const char *pFile_comment = pFilename + filename_len + file_extra_len;
4231 if((file_comment_len != comment_len) || (!
mz_zip_string_equal(pComment, pFile_comment, file_comment_len, flags)))
4236 int ofs = filename_len - 1;
4239 if((pFilename[ofs] ==
'/') || (pFilename[ofs] ==
'\\') || (pFilename[ofs] ==
':'))
4241 }
while(--ofs >= 0);
4244 filename_len -= ofs;
4246 if((filename_len == name_len) && (
mz_zip_string_equal(pName, pFilename, filename_len, flags)))
4249 *pIndex = file_index;
4260 mz_uint64 needed_size, cur_file_ofs, comp_remaining, out_buf_ofs = 0, read_buf_size, read_buf_ofs = 0, read_buf_avail;
4267 if((!pZip) || (!pZip->
m_pState) || ((buf_size) && (!pBuf)) || ((user_read_buf_size) && (!pUser_read_buf)) || (!pZip->
m_pRead))
4287 if(buf_size < needed_size)
4305 if(pZip->
m_pRead(pZip->
m_pIO_opaque, cur_file_ofs, pBuf, (
size_t)needed_size) != needed_size)
4308 #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
4326 read_buf_size = read_buf_avail = file_stat.
m_comp_size;
4329 else if(pUser_read_buf)
4332 if(!user_read_buf_size)
4334 pRead_buf = (
mz_uint8 *)pUser_read_buf;
4335 read_buf_size = user_read_buf_size;
4343 if(((
sizeof(
size_t) ==
sizeof(
mz_uint32))) && (read_buf_size > 0x7FFFFFFF))
4356 size_t in_buf_size, out_buf_size = (size_t)(file_stat.
m_uncomp_size - out_buf_ofs);
4359 read_buf_avail =
MZ_MIN(read_buf_size, comp_remaining);
4360 if(pZip->
m_pRead(pZip->
m_pIO_opaque, cur_file_ofs, pRead_buf, (
size_t)read_buf_avail) != read_buf_avail)
4366 cur_file_ofs += read_buf_avail;
4367 comp_remaining -= read_buf_avail;
4370 in_buf_size = (size_t)read_buf_avail;
4372 read_buf_avail -= in_buf_size;
4373 read_buf_ofs += in_buf_size;
4374 out_buf_ofs += out_buf_size;
4385 #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
4420 mz_uint64 comp_size, uncomp_size, alloc_size;
4437 if(((
sizeof(
size_t) ==
sizeof(
mz_uint32))) && (alloc_size > 0x7FFFFFFF))
4456 *pSize = (size_t)alloc_size;
4476 mz_uint64 read_buf_size, read_buf_ofs = 0, read_buf_avail, comp_remaining, out_buf_ofs = 0, cur_file_ofs;
4478 void *pRead_buf = NULL;
4479 void *pWrite_buf = NULL;
4517 read_buf_size = read_buf_avail = file_stat.
m_comp_size;
4545 #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
4556 while(comp_remaining)
4558 read_buf_avail =
MZ_MIN(read_buf_size, comp_remaining);
4559 if(pZip->
m_pRead(pZip->
m_pIO_opaque, cur_file_ofs, pRead_buf, (
size_t)read_buf_avail) != read_buf_avail)
4566 #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
4573 if(pCallback(pOpaque, out_buf_ofs, pRead_buf, (
size_t)read_buf_avail) != read_buf_avail)
4580 cur_file_ofs += read_buf_avail;
4581 out_buf_ofs += read_buf_avail;
4582 comp_remaining -= read_buf_avail;
4604 read_buf_avail =
MZ_MIN(read_buf_size, comp_remaining);
4605 if(pZip->
m_pRead(pZip->
m_pIO_opaque, cur_file_ofs, pRead_buf, (
size_t)read_buf_avail) != read_buf_avail)
4611 cur_file_ofs += read_buf_avail;
4612 comp_remaining -= read_buf_avail;
4616 in_buf_size = (size_t)read_buf_avail;
4618 read_buf_avail -= in_buf_size;
4619 read_buf_ofs += in_buf_size;
4623 if(pCallback(pOpaque, out_buf_ofs, pWrite_buf_cur, out_buf_size) != out_buf_size)
4630 #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
4652 #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
4653 else if(file_crc32 != file_stat.
m_crc32)
4679 #ifndef MINIZ_NO_STDIO
4699 pFile =
MZ_FOPEN(pDst_filename,
"wb");
4713 #if !defined(MINIZ_NO_TIME) && !defined(MINIZ_NO_STDIO)
4765 const mz_uint8 *pCentral_dir_header;
4771 mz_uint32 local_header_filename_len, local_header_extra_len, local_header_crc32;
4772 mz_uint64 local_header_comp_size, local_header_uncomp_size;
4822 has_data_descriptor = (local_header_bit_flags & 8) != 0;
4824 if(local_header_filename_len != strlen(file_stat.
m_filename))
4833 if(local_header_filename_len)
4838 goto handle_failure;
4842 if(memcmp(file_stat.
m_filename, file_data_array.
m_p, local_header_filename_len) != 0)
4845 goto handle_failure;
4854 goto handle_failure;
4857 mz_uint32 extra_size_remaining = local_header_extra_len;
4862 mz_uint32 field_id, field_data_size, field_total_size;
4864 if(extra_size_remaining < (
sizeof(
mz_uint16) * 2))
4869 field_total_size = field_data_size +
sizeof(
mz_uint16) * 2;
4871 if(field_total_size > extra_size_remaining)
4878 if(field_data_size <
sizeof(
mz_uint64) * 2)
4881 goto handle_failure;
4884 local_header_uncomp_size =
MZ_READ_LE64(pSrc_field_data);
4887 found_zip64_ext_data_in_ldir =
MZ_TRUE;
4891 pExtra_data += field_total_size;
4892 extra_size_remaining -= field_total_size;
4893 }
while(extra_size_remaining);
4898 if((has_data_descriptor) && (!local_header_comp_size) && (!local_header_crc32))
4902 mz_uint32 num_descriptor_uint32s = ((pState->m_zip64) || (found_zip64_ext_data_in_ldir)) ? 6 : 4;
4904 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))
4907 goto handle_failure;
4911 const mz_uint8 *pSrc = has_id ? (descriptor_buf +
sizeof(
mz_uint32)) : descriptor_buf;
4914 mz_uint64 comp_size = 0, uncomp_size = 0;
4916 if((pState->m_zip64) || (found_zip64_ext_data_in_ldir))
4927 if((file_crc32 != file_stat.m_crc32) || (comp_size != file_stat.m_comp_size) || (uncomp_size != file_stat.m_uncomp_size))
4930 goto handle_failure;
4935 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))
4938 goto handle_failure;
4950 if(uncomp_crc32 != file_stat.m_crc32)
5006 if(found_index != i)
5023 if((!pMem) || (!size))
5058 #ifndef MINIZ_NO_STDIO
5103 #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS
5123 #define MZ_WRITE_LE16(p, v) mz_write_le16((mz_uint8 *)(p), (mz_uint16)(v))
5124 #define MZ_WRITE_LE32(p, v) mz_write_le32((mz_uint8 *)(p), (mz_uint32)(v))
5125 #define MZ_WRITE_LE64(p, v) mz_write_le64((mz_uint8 *)(p), (mz_uint64)(v))
5137 if((
sizeof(
size_t) ==
sizeof(
mz_uint32)) && (new_size > 0x7FFFFFFF))
5148 while(new_capacity < new_size)
5157 pState->
m_pMem = pNew_block;
5183 #ifndef MINIZ_NO_STDIO
5279 if(0 != (initial_allocation_size =
MZ_MAX(initial_allocation_size, size_to_reserve_at_beginning)))
5297 #ifndef MINIZ_NO_STDIO
5342 if(size_to_reserve_at_beginning)
5351 size_t n = (size_t)
MZ_MIN(
sizeof(buf), size_to_reserve_at_beginning);
5358 size_to_reserve_at_beginning -= n;
5359 }
while(size_to_reserve_at_beginning);
5418 #ifdef MINIZ_NO_STDIO
5499 #define MZ_ZIP64_MAX_LOCAL_EXTRA_FIELD_SIZE (sizeof(mz_uint16) * 2 + sizeof(mz_uint64) * 2)
5500 #define MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE (sizeof(mz_uint16) * 2 + sizeof(mz_uint64) * 3)
5525 if(pLocal_header_ofs)
5537 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)
5581 const void *pExtra,
mz_uint16 extra_size,
const void *pComment,
mz_uint16 comment_size,
5585 const char *user_extra_data,
mz_uint user_extra_data_len)
5594 if(local_header_ofs > 0xFFFFFFFF)
5602 if(!
mz_zip_writer_create_central_dir_header(pZip, central_dir_header, filename_size, 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))
5623 if(*pArchive_name ==
'/')
5626 while(*pArchive_name)
5628 if((*pArchive_name ==
'\\') || (*pArchive_name ==
':'))
5649 memset(buf, 0,
MZ_MIN(
sizeof(buf), n));
5665 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);
5670 const char *user_extra_data,
mz_uint user_extra_data_len,
const char *user_extra_data_central,
mz_uint user_extra_data_central_len)
5672 mz_uint16 method = 0, dos_time = 0, dos_date = 0;
5673 mz_uint level, ext_attributes = 0, num_alignment_padding_bytes;
5675 size_t archive_name_size;
5678 mz_bool store_data_uncompressed;
5691 if((
int)level_and_flags < 0)
5693 level = level_and_flags & 0xF;
5713 if((buf_size > 0xFFFFFFFF) || (uncomp_size > 0xFFFFFFFF))
5726 if(last_modified != NULL)
5732 #ifndef MINIZ_NO_TIME
5741 archive_name_size = strlen(pArchive_name);
5761 if((archive_name_size) && (pArchive_name[archive_name_size - 1] ==
'/'))
5767 if((buf_size) || (uncomp_size))
5775 if((!store_data_uncompressed) && (buf_size))
5787 local_dir_header_ofs += num_alignment_padding_bytes;
5792 cur_archive_file_ofs += num_alignment_padding_bytes;
5805 pExtra_data = extra_data;
5810 if(!
mz_zip_writer_create_local_dir_header(pZip, local_dir_header, (
mz_uint16)archive_name_size, extra_size + user_extra_data_len, 0, 0, 0, method, bit_flags, dos_time, dos_date))
5813 if(pZip->
m_pWrite(pZip->
m_pIO_opaque, local_dir_header_ofs, local_dir_header,
sizeof(local_dir_header)) !=
sizeof(local_dir_header))
5816 cur_archive_file_ofs +=
sizeof(local_dir_header);
5818 if(pZip->
m_pWrite(pZip->
m_pIO_opaque, cur_archive_file_ofs, pArchive_name, archive_name_size) != archive_name_size)
5823 cur_archive_file_ofs += archive_name_size;
5825 if(pExtra_data != NULL)
5827 if(pZip->
m_pWrite(pZip->
m_pIO_opaque, cur_archive_file_ofs, extra_data, extra_size) != extra_size)
5830 cur_archive_file_ofs += extra_size;
5837 if(!
mz_zip_writer_create_local_dir_header(pZip, local_dir_header, (
mz_uint16)archive_name_size, user_extra_data_len, 0, 0, 0, method, bit_flags, dos_time, dos_date))
5840 if(pZip->
m_pWrite(pZip->
m_pIO_opaque, local_dir_header_ofs, local_dir_header,
sizeof(local_dir_header)) !=
sizeof(local_dir_header))
5843 cur_archive_file_ofs +=
sizeof(local_dir_header);
5845 if(pZip->
m_pWrite(pZip->
m_pIO_opaque, cur_archive_file_ofs, pArchive_name, archive_name_size) != archive_name_size)
5850 cur_archive_file_ofs += archive_name_size;
5853 if(user_extra_data_len > 0)
5855 if(pZip->
m_pWrite(pZip->
m_pIO_opaque, cur_archive_file_ofs, user_extra_data, user_extra_data_len) != user_extra_data_len)
5858 cur_archive_file_ofs += user_extra_data_len;
5864 uncomp_size = buf_size;
5865 if(uncomp_size <= 3)
5868 store_data_uncompressed =
MZ_TRUE;
5872 if(store_data_uncompressed)
5880 cur_archive_file_ofs += buf_size;
5881 comp_size = buf_size;
5914 if(pExtra_data == NULL)
5929 if(pZip->
m_pWrite(pZip->
m_pIO_opaque, cur_archive_file_ofs, local_dir_footer, local_dir_footer_size) != local_dir_footer_size)
5932 cur_archive_file_ofs += local_dir_footer_size;
5935 if(pExtra_data != NULL)
5942 comment_size, uncomp_size, comp_size, uncomp_crc32, method, bit_flags, dos_time, dos_date, local_dir_header_ofs, ext_attributes,
5943 user_extra_data_central, user_extra_data_central_len))
5952 #ifndef MINIZ_NO_STDIO
5954 const char *user_extra_data,
mz_uint user_extra_data_len,
const char *user_extra_data_central,
mz_uint user_extra_data_central_len)
5958 mz_uint16 method = 0, dos_time = 0, dos_date = 0, ext_attributes = 0;
5959 mz_uint64 local_dir_header_ofs, cur_archive_file_ofs = pZip->
m_archive_size, uncomp_size = size_to_add, comp_size = 0;
5960 size_t archive_name_size;
5970 if((
int)level_and_flags < 0)
5972 level = level_and_flags & 0xF;
6008 archive_name_size = strlen(pArchive_name);
6028 #ifndef MINIZ_NO_TIME
6035 if(uncomp_size <= 3)
6043 cur_archive_file_ofs += num_alignment_padding_bytes;
6044 local_dir_header_ofs = cur_archive_file_ofs;
6051 if(uncomp_size && level)
6061 pExtra_data = extra_data;
6066 if(!
mz_zip_writer_create_local_dir_header(pZip, local_dir_header, (
mz_uint16)archive_name_size, 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)
6079 cur_archive_file_ofs += archive_name_size;
6081 if(pZip->
m_pWrite(pZip->
m_pIO_opaque, cur_archive_file_ofs, extra_data, extra_size) != extra_size)
6084 cur_archive_file_ofs += extra_size;
6090 if(!
mz_zip_writer_create_local_dir_header(pZip, local_dir_header, (
mz_uint16)archive_name_size, user_extra_data_len, 0, 0, 0, method, gen_flags, dos_time, dos_date))
6093 if(pZip->
m_pWrite(pZip->
m_pIO_opaque, cur_archive_file_ofs, local_dir_header,
sizeof(local_dir_header)) !=
sizeof(local_dir_header))
6096 cur_archive_file_ofs +=
sizeof(local_dir_header);
6098 if(pZip->
m_pWrite(pZip->
m_pIO_opaque, cur_archive_file_ofs, pArchive_name, archive_name_size) != archive_name_size)
6103 cur_archive_file_ofs += archive_name_size;
6106 if(user_extra_data_len > 0)
6108 if(pZip->
m_pWrite(pZip->
m_pIO_opaque, cur_archive_file_ofs, user_extra_data, user_extra_data_len) != user_extra_data_len)
6111 cur_archive_file_ofs += user_extra_data_len;
6116 mz_uint64 uncomp_remaining = uncomp_size;
6125 while(uncomp_remaining)
6134 uncomp_remaining -= n;
6135 cur_archive_file_ofs += n;
6137 comp_size = uncomp_size;
6166 if(
MZ_FREAD(pRead_buf, 1, in_buf_size, pSrc_file) != in_buf_size)
6173 uncomp_remaining -= in_buf_size;
6208 if(pExtra_data == NULL)
6223 if(pZip->
m_pWrite(pZip->
m_pIO_opaque, cur_archive_file_ofs, local_dir_footer, local_dir_footer_size) != local_dir_footer_size)
6226 cur_archive_file_ofs += local_dir_footer_size;
6228 if(pExtra_data != NULL)
6235 uncomp_size, comp_size, uncomp_crc32, method, gen_flags, dos_time, dos_date, local_dir_header_ofs, ext_attributes,
6236 user_extra_data_central, user_extra_data_central_len))
6252 memset(&file_modified_time, 0,
sizeof(file_modified_time));
6254 #if !defined(MINIZ_NO_TIME) && !defined(MINIZ_NO_STDIO)
6255 pFile_time = &file_modified_time;
6260 pSrc_file =
MZ_FOPEN(pSrc_filename,
"rb");
6268 mz_bool 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);
6284 if((pUncomp_size) || (pComp_size) || (pLocal_header_ofs) || (pDisk_start))
6304 if(pLocal_header_ofs)
6322 if((pExt) && (ext_len))
6324 mz_uint32 extra_size_remaining = ext_len;
6325 const mz_uint8 *pExtra_data = pExt;
6329 mz_uint32 field_id, field_data_size, field_total_size;
6331 if(extra_size_remaining < (
sizeof(
mz_uint16) * 2))
6336 field_total_size = field_data_size +
sizeof(
mz_uint16) * 2;
6338 if(field_total_size > extra_size_remaining)
6347 pExtra_data += field_total_size;
6348 extra_size_remaining -= field_total_size;
6349 }
while(extra_size_remaining);
6358 mz_uint n, bit_flags, num_alignment_padding_bytes, src_central_dir_following_data_size;
6359 mz_uint64 src_archive_bytes_remaining, local_dir_header_ofs;
6360 mz_uint64 cur_src_file_ofs, cur_dst_file_ofs;
6364 size_t orig_central_dir_size;
6367 const mz_uint8 *pSrc_central_header;
6369 mz_uint32 src_filename_len, src_comment_len, src_ext_len;
6370 mz_uint32 local_header_filename_size, local_header_extra_len;
6371 mz_uint64 local_header_comp_size, local_header_uncomp_size;
6385 if(NULL == (pSrc_central_header =
mz_zip_get_cdh(pSource_zip, src_file_index)))
6394 src_central_dir_following_data_size = src_filename_len + src_ext_len + src_comment_len;
6434 src_archive_bytes_remaining = local_header_filename_size + local_header_extra_len + src_file_stat.
m_comp_size;
6452 mz_uint32 extra_size_remaining = local_header_extra_len;
6457 mz_uint32 field_id, field_data_size, field_total_size;
6459 if(extra_size_remaining < (
sizeof(
mz_uint16) * 2))
6467 field_total_size = field_data_size +
sizeof(
mz_uint16) * 2;
6469 if(field_total_size > extra_size_remaining)
6479 if(field_data_size <
sizeof(
mz_uint64) * 2)
6485 local_header_uncomp_size =
MZ_READ_LE64(pSrc_field_data);
6488 found_zip64_ext_data_in_ldir =
MZ_TRUE;
6492 pExtra_data += field_total_size;
6493 extra_size_remaining -= field_total_size;
6494 }
while(extra_size_remaining);
6499 if(!pState->m_zip64)
6514 cur_dst_file_ofs += num_alignment_padding_bytes;
6516 local_dir_header_ofs = cur_dst_file_ofs;
6517 if(pZip->m_file_offset_alignment)
6519 MZ_ASSERT((local_dir_header_ofs & (pZip->m_file_offset_alignment - 1)) == 0);
6532 while(src_archive_bytes_remaining)
6535 if(pSource_zip->m_pRead(pSource_zip->m_pIO_opaque, cur_src_file_ofs, pBuf, n) != n)
6537 pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf);
6540 cur_src_file_ofs += n;
6542 if(pZip->m_pWrite(pZip->m_pIO_opaque, cur_dst_file_ofs, pBuf, n) != n)
6544 pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf);
6547 cur_dst_file_ofs += n;
6549 src_archive_bytes_remaining -= n;
6557 if((pSource_zip->m_pState->m_zip64) || (found_zip64_ext_data_in_ldir))
6566 if(pSource_zip->m_pRead(pSource_zip->m_pIO_opaque, cur_src_file_ofs, pBuf, (
sizeof(
mz_uint32) * 6)) != (
sizeof(
mz_uint32) * 6))
6568 pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf);
6579 if(pSource_zip->m_pRead(pSource_zip->m_pIO_opaque, cur_src_file_ofs, pBuf,
sizeof(
mz_uint32) * 4) !=
sizeof(
mz_uint32) * 4)
6581 pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf);
6587 if(pZip->m_pState->m_zip64)
6591 const mz_uint32 src_crc32 = pSrc_descriptor[0];
6592 const mz_uint64 src_comp_size = pSrc_descriptor[1];
6593 const mz_uint64 src_uncomp_size = pSrc_descriptor[2];
6605 n =
sizeof(
mz_uint32) * (has_id ? 4 : 3);
6609 if(pZip->m_pWrite(pZip->m_pIO_opaque, cur_dst_file_ofs, pBuf, n) != n)
6611 pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf);
6615 cur_src_file_ofs += n;
6616 cur_dst_file_ofs += n;
6618 pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf);
6621 orig_central_dir_size = pState->m_central_dir.m_size;
6710 pZip->m_total_files++;
6711 pZip->m_archive_size = cur_dst_file_ofs;
6719 mz_uint64 central_dir_ofs, central_dir_size;
6738 central_dir_ofs = 0;
6739 central_dir_size = 0;
6793 #ifndef MINIZ_NO_STDIO
6806 if((!ppBuf) || (!pSize))
6834 #ifndef MINIZ_NO_STDIO
6848 if((
int)level_and_flags < 0)
6851 if((!pZip_filename) || (!pArchive_name) || ((buf_size) && (!pBuf)) || ((comment_size) && (!pComment)) || ((level_and_flags & 0xF) >
MZ_UBER_COMPRESSION))
6877 created_new_archive =
MZ_TRUE;
6900 status =
mz_zip_writer_add_mem_ex(&zip_archive, pArchive_name, pBuf, buf_size, pComment, comment_size, level_and_flags, 0, 0);
6920 if((!status) && (created_new_archive))
6924 (void)ignoredStatus;
6942 if((!pZip_filename) || (!pArchive_name))
7039 return "undefined error";
7041 return "too many files";
7043 return "file too large";
7045 return "unsupported method";
7047 return "unsupported encryption";
7049 return "unsupported feature";
7051 return "failed finding central directory";
7053 return "not a ZIP archive";
7055 return "invalid header or archive is corrupted";
7057 return "unsupported multidisk archive";
7059 return "decompression failed or archive is corrupted";
7061 return "compression failed";
7063 return "unexpected decompressed size";
7065 return "CRC-32 check failed";
7067 return "unsupported central directory size";
7069 return "allocation failed";
7071 return "file open failed";
7073 return "file create failed";
7075 return "file write failed";
7077 return "file read failed";
7079 return "file close failed";
7081 return "file seek failed";
7083 return "file stat failed";
7085 return "invalid parameter";
7087 return "invalid filename";
7089 return "buffer too small";
7091 return "internal error";
7093 return "file not found";
7095 return "archive is too large";
7097 return "validation failed";
7099 return "write calledback failed";
7104 return "unknown error";
7164 if(filename_buf_size)
7165 pFilename[0] =
'\0';
7170 if(filename_buf_size)
7172 n =
MZ_MIN(n, filename_buf_size - 1);
7174 pFilename[n] =
'\0';