1/* tinfl.c v1.11 - public domain inflate with zlib header parsing/adler32 checking (inflate-only subset of miniz.c)
2 See "unlicense" statement at the end of this file.
3 Rich Geldreich <richgel99@gmail.com>, last updated May 20, 2011
4 Implements RFC 1950: https://www.ietf.org/rfc/rfc1950.txt and RFC 1951: https://www.ietf.org/rfc/rfc1951.txt
5
6 The entire decompressor coroutine is implemented in tinfl_decompress(). The other functions are optional high-level helpers.
7*/
8#ifndef TINFL_HEADER_INCLUDED
9#define TINFL_HEADER_INCLUDED
10
11typedef PHYSFS_uint8 mz_uint8;
12typedef PHYSFS_sint16 mz_int16;
13typedef PHYSFS_uint16 mz_uint16;
14typedef PHYSFS_uint32 mz_uint32;
15typedef unsigned int mz_uint;
16typedef PHYSFS_uint64 mz_uint64;
17
18/* For more compatibility with zlib, miniz.c uses unsigned long for some parameters/struct members. */
19typedef unsigned long mz_ulong;
20
21/* Heap allocation callbacks. */
22typedef void *(*mz_alloc_func)(void *opaque, unsigned int items, unsigned int size);
23typedef void (*mz_free_func)(void *opaque, void *address);
24
25#ifndef MINIZ_LITTLE_ENDIAN /* if not defined by PHYSFS */
26#if defined(_M_IX86) || defined(_M_X64)
27/* Set MINIZ_USE_UNALIGNED_LOADS_AND_STORES to 1 if integer loads and stores to unaligned addresses are acceptable on the target platform (slightly faster). */
28#define MINIZ_USE_UNALIGNED_LOADS_AND_STORES 1
29/* Set MINIZ_LITTLE_ENDIAN to 1 if the processor is little endian. */
30#define MINIZ_LITTLE_ENDIAN 1
31#endif
32#endif /**/
33
34#if defined(_WIN64) || defined(__MINGW64__) || defined(_LP64) || defined(__LP64__)
35/* Set MINIZ_HAS_64BIT_REGISTERS to 1 if the processor has 64-bit general purpose registers (enables 64-bit bitbuffer in inflator) */
36#define MINIZ_HAS_64BIT_REGISTERS 1
37#endif
38
39/* Works around MSVC's spammy "warning C4127: conditional expression is constant" message. */
40#ifdef _MSC_VER
41#define MZ_MACRO_END while (0, 0)
42#else
43#define MZ_MACRO_END while (0)
44#endif
45
46/* Decompression flags. */
47enum
48{
49 TINFL_FLAG_PARSE_ZLIB_HEADER = 1,
50 TINFL_FLAG_HAS_MORE_INPUT = 2,
51 TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF = 4,
52 TINFL_FLAG_COMPUTE_ADLER32 = 8
53};
54
55struct tinfl_decompressor_tag; typedef struct tinfl_decompressor_tag tinfl_decompressor;
56
57/* Max size of LZ dictionary. */
58#define TINFL_LZ_DICT_SIZE 32768
59
60/* Return status. */
61typedef enum
62{
63 TINFL_STATUS_BAD_PARAM = -3,
64 TINFL_STATUS_ADLER32_MISMATCH = -2,
65 TINFL_STATUS_FAILED = -1,
66 TINFL_STATUS_DONE = 0,
67 TINFL_STATUS_NEEDS_MORE_INPUT = 1,
68 TINFL_STATUS_HAS_MORE_OUTPUT = 2
69} tinfl_status;
70
71/* Initializes the decompressor to its initial state. */
72#define tinfl_init(r) do { (r)->m_state = 0; } MZ_MACRO_END
73#define tinfl_get_adler32(r) (r)->m_check_adler32
74
75/* Main low-level decompressor coroutine function. This is the only function actually needed for decompression. All the other functions are just high-level helpers for improved usability. */
76/* This is a universal API, i.e. it can be used as a building block to build any desired higher level decompression API. In the limit case, it can be called once per every byte input or output. */
77static 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);
78
79/* Internal/private bits follow. */
80enum
81{
82 TINFL_MAX_HUFF_TABLES = 3, TINFL_MAX_HUFF_SYMBOLS_0 = 288, TINFL_MAX_HUFF_SYMBOLS_1 = 32, TINFL_MAX_HUFF_SYMBOLS_2 = 19,
83 TINFL_FAST_LOOKUP_BITS = 10, TINFL_FAST_LOOKUP_SIZE = 1 << TINFL_FAST_LOOKUP_BITS
84};
85
86typedef struct
87{
88 mz_uint8 m_code_size[TINFL_MAX_HUFF_SYMBOLS_0];
89 mz_int16 m_look_up[TINFL_FAST_LOOKUP_SIZE], m_tree[TINFL_MAX_HUFF_SYMBOLS_0 * 2];
90} tinfl_huff_table;
91
92#if MINIZ_HAS_64BIT_REGISTERS
93 #define TINFL_USE_64BIT_BITBUF 1
94#endif
95
96#if TINFL_USE_64BIT_BITBUF
97 typedef mz_uint64 tinfl_bit_buf_t;
98 #define TINFL_BITBUF_SIZE (64)
99#else
100 typedef mz_uint32 tinfl_bit_buf_t;
101 #define TINFL_BITBUF_SIZE (32)
102#endif
103
104struct tinfl_decompressor_tag
105{
106 mz_uint32 m_state, m_num_bits, m_zhdr0, m_zhdr1, m_z_adler32, m_final, m_type, m_check_adler32, m_dist, m_counter, m_num_extra, m_table_sizes[TINFL_MAX_HUFF_TABLES];
107 tinfl_bit_buf_t m_bit_buf;
108 size_t m_dist_from_out_buf_start;
109 tinfl_huff_table m_tables[TINFL_MAX_HUFF_TABLES];
110 mz_uint8 m_raw_header[4], m_len_codes[TINFL_MAX_HUFF_SYMBOLS_0 + TINFL_MAX_HUFF_SYMBOLS_1 + 137];
111};
112
113#endif /* #ifdef TINFL_HEADER_INCLUDED */
114
115/* ------------------- End of Header: Implementation follows. (If you only want the header, define MINIZ_HEADER_FILE_ONLY.) */
116
117#ifndef TINFL_HEADER_FILE_ONLY
118
119#define MZ_MAX(a,b) (((a)>(b))?(a):(b))
120#define MZ_MIN(a,b) (((a)<(b))?(a):(b))
121#define MZ_CLEAR_OBJ(obj) memset(&(obj), 0, sizeof(obj))
122#define MZ_CLEAR_ARR(obj) memset((obj), 0, sizeof(obj))
123#define MZ_CLEAR_PTR(obj) memset((obj), 0, sizeof(*obj))
124
125#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES && MINIZ_LITTLE_ENDIAN
126 #define MZ_READ_LE16(p) *((const mz_uint16 *)(p))
127 #define MZ_READ_LE32(p) *((const mz_uint32 *)(p))
128#else
129 #define MZ_READ_LE16(p) ((mz_uint32)(((const mz_uint8 *)(p))[0]) | ((mz_uint32)(((const mz_uint8 *)(p))[1]) << 8U))
130 #define MZ_READ_LE32(p) ((mz_uint32)(((const mz_uint8 *)(p))[0]) | ((mz_uint32)(((const mz_uint8 *)(p))[1]) << 8U) | ((mz_uint32)(((const mz_uint8 *)(p))[2]) << 16U) | ((mz_uint32)(((const mz_uint8 *)(p))[3]) << 24U))
131#endif
132
133#define TINFL_MEMCPY(d, s, l) memcpy(d, s, l)
134#define TINFL_MEMSET(p, c, l) memset(p, c, l)
135
136#define TINFL_CR_BEGIN switch(r->m_state) { case 0:
137#define TINFL_CR_RETURN(state_index, result) do { status = result; r->m_state = state_index; goto common_exit; case state_index:; } MZ_MACRO_END
138#define TINFL_CR_RETURN_FOREVER(state_index, result) do { for ( ; ; ) { TINFL_CR_RETURN(state_index, result); } } MZ_MACRO_END
139#define TINFL_CR_FINISH }
140
141/* TODO: If the caller has indicated that there's no more input, and we attempt to read beyond the input buf, then something is wrong with the input because the inflator never */
142/* reads ahead more than it needs to. Currently TINFL_GET_BYTE() pads the end of the stream with 0's in this scenario. */
143#define TINFL_GET_BYTE(state_index, c) do { \
144 if (pIn_buf_cur >= pIn_buf_end) { \
145 for ( ; ; ) { \
146 if (decomp_flags & TINFL_FLAG_HAS_MORE_INPUT) { \
147 TINFL_CR_RETURN(state_index, TINFL_STATUS_NEEDS_MORE_INPUT); \
148 if (pIn_buf_cur < pIn_buf_end) { \
149 c = *pIn_buf_cur++; \
150 break; \
151 } \
152 } else { \
153 c = 0; \
154 break; \
155 } \
156 } \
157 } else c = *pIn_buf_cur++; } MZ_MACRO_END
158
159#define TINFL_NEED_BITS(state_index, n) do { mz_uint c; TINFL_GET_BYTE(state_index, c); bit_buf |= (((tinfl_bit_buf_t)c) << num_bits); num_bits += 8; } while (num_bits < (mz_uint)(n))
160#define TINFL_SKIP_BITS(state_index, n) do { if (num_bits < (mz_uint)(n)) { TINFL_NEED_BITS(state_index, n); } bit_buf >>= (n); num_bits -= (n); } MZ_MACRO_END
161#define TINFL_GET_BITS(state_index, b, n) do { if (num_bits < (mz_uint)(n)) { TINFL_NEED_BITS(state_index, n); } b = bit_buf & ((1 << (n)) - 1); bit_buf >>= (n); num_bits -= (n); } MZ_MACRO_END
162
163/* TINFL_HUFF_BITBUF_FILL() is only used rarely, when the number of bytes remaining in the input buffer falls below 2. */
164/* 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 */
165/* 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 */
166/* bit buffer contains >=15 bits (deflate's max. Huffman code size). */
167#define TINFL_HUFF_BITBUF_FILL(state_index, pHuff) \
168 do { \
169 temp = (pHuff)->m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]; \
170 if (temp >= 0) { \
171 code_len = temp >> 9; \
172 if ((code_len) && (num_bits >= code_len)) \
173 break; \
174 } else if (num_bits > TINFL_FAST_LOOKUP_BITS) { \
175 code_len = TINFL_FAST_LOOKUP_BITS; \
176 do { \
177 temp = (pHuff)->m_tree[~temp + ((bit_buf >> code_len++) & 1)]; \
178 } while ((temp < 0) && (num_bits >= (code_len + 1))); \
179 if (temp >= 0) break; \
180 } \
181 TINFL_GET_BYTE(state_index, c); \
182 bit_buf |= (((tinfl_bit_buf_t)c) << num_bits); \
183 num_bits += 8; \
184 } while (num_bits < 15);
185
186/* 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 */
187/* 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 */
188/* 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. */
189/* The slow path is only executed at the very end of the input buffer. */
190#define TINFL_HUFF_DECODE(state_index, sym, pHuff) do { \
191 int temp; mz_uint code_len, c; \
192 if (num_bits < 15) { \
193 if ((pIn_buf_end - pIn_buf_cur) < 2) { \
194 TINFL_HUFF_BITBUF_FILL(state_index, pHuff); \
195 } else { \
196 bit_buf |= (((tinfl_bit_buf_t)pIn_buf_cur[0]) << num_bits) | (((tinfl_bit_buf_t)pIn_buf_cur[1]) << (num_bits + 8)); pIn_buf_cur += 2; num_bits += 16; \
197 } \
198 } \
199 if ((temp = (pHuff)->m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0) \
200 code_len = temp >> 9, temp &= 511; \
201 else { \
202 code_len = TINFL_FAST_LOOKUP_BITS; do { temp = (pHuff)->m_tree[~temp + ((bit_buf >> code_len++) & 1)]; } while (temp < 0); \
203 } sym = temp; bit_buf >>= code_len; num_bits -= code_len; } MZ_MACRO_END
204
205static 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)
206{
207 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 };
208 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 };
209 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};
210 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};
211 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 };
212 static const int s_min_table_sizes[3] = { 257, 1, 4 };
213
214 tinfl_status status = TINFL_STATUS_FAILED; mz_uint32 num_bits, dist, counter, num_extra; tinfl_bit_buf_t bit_buf;
215 const mz_uint8 *pIn_buf_cur = pIn_buf_next, *const pIn_buf_end = pIn_buf_next + *pIn_buf_size;
216 mz_uint8 *pOut_buf_cur = pOut_buf_next, *const pOut_buf_end = pOut_buf_next + *pOut_buf_size;
217 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;
218
219 /* 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). */
220 if (((out_buf_size_mask + 1) & out_buf_size_mask) || (pOut_buf_next < pOut_buf_start)) { *pIn_buf_size = *pOut_buf_size = 0; return TINFL_STATUS_BAD_PARAM; }
221
222 num_bits = r->m_num_bits; bit_buf = r->m_bit_buf; dist = r->m_dist; counter = r->m_counter; num_extra = r->m_num_extra; dist_from_out_buf_start = r->m_dist_from_out_buf_start;
223 TINFL_CR_BEGIN
224
225 bit_buf = num_bits = dist = counter = num_extra = r->m_zhdr0 = r->m_zhdr1 = 0; r->m_z_adler32 = r->m_check_adler32 = 1;
226 if (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER)
227 {
228 TINFL_GET_BYTE(1, r->m_zhdr0); TINFL_GET_BYTE(2, r->m_zhdr1);
229 counter = (((r->m_zhdr0 * 256 + r->m_zhdr1) % 31 != 0) || (r->m_zhdr1 & 32) || ((r->m_zhdr0 & 15) != 8));
230 if (!(decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF)) counter |= (((1U << (8U + (r->m_zhdr0 >> 4))) > 32768U) || ((out_buf_size_mask + 1) < (size_t)(1U << (8U + (r->m_zhdr0 >> 4)))));
231 if (counter) { TINFL_CR_RETURN_FOREVER(36, TINFL_STATUS_FAILED); }
232 }
233
234 do
235 {
236 TINFL_GET_BITS(3, r->m_final, 3); r->m_type = r->m_final >> 1;
237 if (r->m_type == 0)
238 {
239 TINFL_SKIP_BITS(5, num_bits & 7);
240 for (counter = 0; counter < 4; ++counter) { if (num_bits) TINFL_GET_BITS(6, r->m_raw_header[counter], 8); else TINFL_GET_BYTE(7, r->m_raw_header[counter]); }
241 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)))) { TINFL_CR_RETURN_FOREVER(39, TINFL_STATUS_FAILED); }
242 while ((counter) && (num_bits))
243 {
244 TINFL_GET_BITS(51, dist, 8);
245 while (pOut_buf_cur >= pOut_buf_end) { TINFL_CR_RETURN(52, TINFL_STATUS_HAS_MORE_OUTPUT); }
246 *pOut_buf_cur++ = (mz_uint8)dist;
247 counter--;
248 }
249 while (counter)
250 {
251 size_t n; while (pOut_buf_cur >= pOut_buf_end) { TINFL_CR_RETURN(9, TINFL_STATUS_HAS_MORE_OUTPUT); }
252 while (pIn_buf_cur >= pIn_buf_end)
253 {
254 if (decomp_flags & TINFL_FLAG_HAS_MORE_INPUT)
255 {
256 TINFL_CR_RETURN(38, TINFL_STATUS_NEEDS_MORE_INPUT);
257 }
258 else
259 {
260 TINFL_CR_RETURN_FOREVER(40, TINFL_STATUS_FAILED);
261 }
262 }
263 n = MZ_MIN(MZ_MIN((size_t)(pOut_buf_end - pOut_buf_cur), (size_t)(pIn_buf_end - pIn_buf_cur)), counter);
264 TINFL_MEMCPY(pOut_buf_cur, pIn_buf_cur, n); pIn_buf_cur += n; pOut_buf_cur += n; counter -= (mz_uint)n;
265 }
266 }
267 else if (r->m_type == 3)
268 {
269 TINFL_CR_RETURN_FOREVER(10, TINFL_STATUS_FAILED);
270 }
271 else
272 {
273 if (r->m_type == 1)
274 {
275 mz_uint8 *p = r->m_tables[0].m_code_size; mz_uint i;
276 r->m_table_sizes[0] = 288; r->m_table_sizes[1] = 32; TINFL_MEMSET(r->m_tables[1].m_code_size, 5, 32);
277 for ( i = 0; i <= 143; ++i) *p++ = 8;
278 for ( ; i <= 255; ++i) *p++ = 9;
279 for ( ; i <= 279; ++i) *p++ = 7;
280 for ( ; i <= 287; ++i) *p++ = 8;
281 }
282 else
283 {
284 for (counter = 0; counter < 3; counter++) { TINFL_GET_BITS(11, r->m_table_sizes[counter], "\05\05\04"[counter]); r->m_table_sizes[counter] += s_min_table_sizes[counter]; }
285 MZ_CLEAR_ARR(r->m_tables[2].m_code_size); for (counter = 0; counter < r->m_table_sizes[2]; counter++) { mz_uint s; TINFL_GET_BITS(14, s, 3); r->m_tables[2].m_code_size[s_length_dezigzag[counter]] = (mz_uint8)s; }
286 r->m_table_sizes[2] = 19;
287 }
288 for ( ; (int)r->m_type >= 0; r->m_type--)
289 {
290 int tree_next, tree_cur; tinfl_huff_table *pTable;
291 mz_uint i, j, used_syms, total, sym_index, next_code[17], total_syms[16]; pTable = &r->m_tables[r->m_type]; MZ_CLEAR_ARR(total_syms); MZ_CLEAR_ARR(pTable->m_look_up); MZ_CLEAR_ARR(pTable->m_tree);
292 for (i = 0; i < r->m_table_sizes[r->m_type]; ++i) total_syms[pTable->m_code_size[i]]++;
293 used_syms = 0, total = 0; next_code[0] = next_code[1] = 0;
294 for (i = 1; i <= 15; ++i) { used_syms += total_syms[i]; next_code[i + 1] = (total = ((total + total_syms[i]) << 1)); }
295 if ((65536 != total) && (used_syms > 1))
296 {
297 TINFL_CR_RETURN_FOREVER(35, TINFL_STATUS_FAILED);
298 }
299 for (tree_next = -1, sym_index = 0; sym_index < r->m_table_sizes[r->m_type]; ++sym_index)
300 {
301 mz_uint rev_code = 0, l, cur_code, code_size = pTable->m_code_size[sym_index]; if (!code_size) continue;
302 cur_code = next_code[code_size]++; for (l = code_size; l > 0; l--, cur_code >>= 1) rev_code = (rev_code << 1) | (cur_code & 1);
303 if (code_size <= TINFL_FAST_LOOKUP_BITS) { mz_int16 k = (mz_int16)((code_size << 9) | sym_index); while (rev_code < TINFL_FAST_LOOKUP_SIZE) { pTable->m_look_up[rev_code] = k; rev_code += (1 << code_size); } continue; }
304 if (0 == (tree_cur = pTable->m_look_up[rev_code & (TINFL_FAST_LOOKUP_SIZE - 1)])) { pTable->m_look_up[rev_code & (TINFL_FAST_LOOKUP_SIZE - 1)] = (mz_int16)tree_next; tree_cur = tree_next; tree_next -= 2; }
305 rev_code >>= (TINFL_FAST_LOOKUP_BITS - 1);
306 for (j = code_size; j > (TINFL_FAST_LOOKUP_BITS + 1); j--)
307 {
308 tree_cur -= ((rev_code >>= 1) & 1);
309 if (!pTable->m_tree[-tree_cur - 1]) { pTable->m_tree[-tree_cur - 1] = (mz_int16)tree_next; tree_cur = tree_next; tree_next -= 2; } else tree_cur = pTable->m_tree[-tree_cur - 1];
310 }
311 tree_cur -= ((rev_code >>= 1) & 1); pTable->m_tree[-tree_cur - 1] = (mz_int16)sym_index;
312 }
313 if (r->m_type == 2)
314 {
315 for (counter = 0; counter < (r->m_table_sizes[0] + r->m_table_sizes[1]); )
316 {
317 mz_uint s; TINFL_HUFF_DECODE(16, dist, &r->m_tables[2]); if (dist < 16) { r->m_len_codes[counter++] = (mz_uint8)dist; continue; }
318 if ((dist == 16) && (!counter))
319 {
320 TINFL_CR_RETURN_FOREVER(17, TINFL_STATUS_FAILED);
321 }
322 num_extra = "\02\03\07"[dist - 16]; TINFL_GET_BITS(18, s, num_extra); s += "\03\03\013"[dist - 16];
323 TINFL_MEMSET(r->m_len_codes + counter, (dist == 16) ? r->m_len_codes[counter - 1] : 0, s); counter += s;
324 }
325 if ((r->m_table_sizes[0] + r->m_table_sizes[1]) != counter)
326 {
327 TINFL_CR_RETURN_FOREVER(21, TINFL_STATUS_FAILED);
328 }
329 TINFL_MEMCPY(r->m_tables[0].m_code_size, r->m_len_codes, r->m_table_sizes[0]); TINFL_MEMCPY(r->m_tables[1].m_code_size, r->m_len_codes + r->m_table_sizes[0], r->m_table_sizes[1]);
330 }
331 }
332 for ( ; ; )
333 {
334 mz_uint8 *pSrc;
335 for ( ; ; )
336 {
337 if (((pIn_buf_end - pIn_buf_cur) < 4) || ((pOut_buf_end - pOut_buf_cur) < 2))
338 {
339 TINFL_HUFF_DECODE(23, counter, &r->m_tables[0]);
340 if (counter >= 256)
341 break;
342 while (pOut_buf_cur >= pOut_buf_end) { TINFL_CR_RETURN(24, TINFL_STATUS_HAS_MORE_OUTPUT); }
343 *pOut_buf_cur++ = (mz_uint8)counter;
344 }
345 else
346 {
347 int sym2; mz_uint code_len;
348#if TINFL_USE_64BIT_BITBUF
349 if (num_bits < 30) { bit_buf |= (((tinfl_bit_buf_t)MZ_READ_LE32(pIn_buf_cur)) << num_bits); pIn_buf_cur += 4; num_bits += 32; }
350#else
351 if (num_bits < 15) { bit_buf |= (((tinfl_bit_buf_t)MZ_READ_LE16(pIn_buf_cur)) << num_bits); pIn_buf_cur += 2; num_bits += 16; }
352#endif
353 if ((sym2 = r->m_tables[0].m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0)
354 code_len = sym2 >> 9;
355 else
356 {
357 code_len = TINFL_FAST_LOOKUP_BITS; do { sym2 = r->m_tables[0].m_tree[~sym2 + ((bit_buf >> code_len++) & 1)]; } while (sym2 < 0);
358 }
359 counter = sym2; bit_buf >>= code_len; num_bits -= code_len;
360 if (counter & 256)
361 break;
362
363#if !TINFL_USE_64BIT_BITBUF
364 if (num_bits < 15) { bit_buf |= (((tinfl_bit_buf_t)MZ_READ_LE16(pIn_buf_cur)) << num_bits); pIn_buf_cur += 2; num_bits += 16; }
365#endif
366 if ((sym2 = r->m_tables[0].m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0)
367 code_len = sym2 >> 9;
368 else
369 {
370 code_len = TINFL_FAST_LOOKUP_BITS; do { sym2 = r->m_tables[0].m_tree[~sym2 + ((bit_buf >> code_len++) & 1)]; } while (sym2 < 0);
371 }
372 bit_buf >>= code_len; num_bits -= code_len;
373
374 pOut_buf_cur[0] = (mz_uint8)counter;
375 if (sym2 & 256)
376 {
377 pOut_buf_cur++;
378 counter = sym2;
379 break;
380 }
381 pOut_buf_cur[1] = (mz_uint8)sym2;
382 pOut_buf_cur += 2;
383 }
384 }
385 if ((counter &= 511) == 256) break;
386
387 num_extra = s_length_extra[counter - 257]; counter = s_length_base[counter - 257];
388 if (num_extra) { mz_uint extra_bits; TINFL_GET_BITS(25, extra_bits, num_extra); counter += extra_bits; }
389
390 TINFL_HUFF_DECODE(26, dist, &r->m_tables[1]);
391 num_extra = s_dist_extra[dist]; dist = s_dist_base[dist];
392 if (num_extra) { mz_uint extra_bits; TINFL_GET_BITS(27, extra_bits, num_extra); dist += extra_bits; }
393
394 dist_from_out_buf_start = pOut_buf_cur - pOut_buf_start;
395 if ((dist > dist_from_out_buf_start) && (decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF))
396 {
397 TINFL_CR_RETURN_FOREVER(37, TINFL_STATUS_FAILED);
398 }
399
400 pSrc = pOut_buf_start + ((dist_from_out_buf_start - dist) & out_buf_size_mask);
401
402 if ((MZ_MAX(pOut_buf_cur, pSrc) + counter) > pOut_buf_end)
403 {
404 while (counter--)
405 {
406 while (pOut_buf_cur >= pOut_buf_end) { TINFL_CR_RETURN(53, TINFL_STATUS_HAS_MORE_OUTPUT); }
407 *pOut_buf_cur++ = pOut_buf_start[(dist_from_out_buf_start++ - dist) & out_buf_size_mask];
408 }
409 continue;
410 }
411#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES
412 else if ((counter >= 9) && (counter <= dist))
413 {
414 const mz_uint8 *pSrc_end = pSrc + (counter & ~7);
415 do
416 {
417 ((mz_uint32 *)pOut_buf_cur)[0] = ((const mz_uint32 *)pSrc)[0];
418 ((mz_uint32 *)pOut_buf_cur)[1] = ((const mz_uint32 *)pSrc)[1];
419 pOut_buf_cur += 8;
420 } while ((pSrc += 8) < pSrc_end);
421 if ((counter &= 7) < 3)
422 {
423 if (counter)
424 {
425 pOut_buf_cur[0] = pSrc[0];
426 if (counter > 1)
427 pOut_buf_cur[1] = pSrc[1];
428 pOut_buf_cur += counter;
429 }
430 continue;
431 }
432 }
433#endif
434 do
435 {
436 pOut_buf_cur[0] = pSrc[0];
437 pOut_buf_cur[1] = pSrc[1];
438 pOut_buf_cur[2] = pSrc[2];
439 pOut_buf_cur += 3; pSrc += 3;
440 } while ((int)(counter -= 3) > 2);
441 if ((int)counter > 0)
442 {
443 pOut_buf_cur[0] = pSrc[0];
444 if ((int)counter > 1)
445 pOut_buf_cur[1] = pSrc[1];
446 pOut_buf_cur += counter;
447 }
448 }
449 }
450 } while (!(r->m_final & 1));
451 if (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER)
452 {
453 TINFL_SKIP_BITS(32, num_bits & 7); for (counter = 0; counter < 4; ++counter) { mz_uint s; if (num_bits) TINFL_GET_BITS(41, s, 8); else TINFL_GET_BYTE(42, s); r->m_z_adler32 = (r->m_z_adler32 << 8) | s; }
454 }
455 TINFL_CR_RETURN_FOREVER(34, TINFL_STATUS_DONE);
456 TINFL_CR_FINISH
457
458common_exit:
459 r->m_num_bits = num_bits; r->m_bit_buf = bit_buf; r->m_dist = dist; r->m_counter = counter; r->m_num_extra = num_extra; r->m_dist_from_out_buf_start = dist_from_out_buf_start;
460 *pIn_buf_size = pIn_buf_cur - pIn_buf_next; *pOut_buf_size = pOut_buf_cur - pOut_buf_next;
461 if ((decomp_flags & (TINFL_FLAG_PARSE_ZLIB_HEADER | TINFL_FLAG_COMPUTE_ADLER32)) && (status >= 0))
462 {
463 const mz_uint8 *ptr = pOut_buf_next; size_t buf_len = *pOut_buf_size;
464 mz_uint32 i, s1 = r->m_check_adler32 & 0xffff, s2 = r->m_check_adler32 >> 16; size_t block_len = buf_len % 5552;
465 while (buf_len)
466 {
467 for (i = 0; i + 7 < block_len; i += 8, ptr += 8)
468 {
469 s1 += ptr[0], s2 += s1; s1 += ptr[1], s2 += s1; s1 += ptr[2], s2 += s1; s1 += ptr[3], s2 += s1;
470 s1 += ptr[4], s2 += s1; s1 += ptr[5], s2 += s1; s1 += ptr[6], s2 += s1; s1 += ptr[7], s2 += s1;
471 }
472 for ( ; i < block_len; ++i) s1 += *ptr++, s2 += s1;
473 s1 %= 65521U, s2 %= 65521U; buf_len -= block_len; block_len = 5552;
474 }
475 r->m_check_adler32 = (s2 << 16) + s1; if ((status == TINFL_STATUS_DONE) && (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER) && (r->m_check_adler32 != r->m_z_adler32)) status = TINFL_STATUS_ADLER32_MISMATCH;
476 }
477 return status;
478}
479
480/* Flush values. For typical usage you only need MZ_NO_FLUSH and MZ_FINISH. The other stuff is for advanced use. */
481enum { MZ_NO_FLUSH = 0, MZ_PARTIAL_FLUSH = 1, MZ_SYNC_FLUSH = 2, MZ_FULL_FLUSH = 3, MZ_FINISH = 4, MZ_BLOCK = 5 };
482
483/* Return status codes. MZ_PARAM_ERROR is non-standard. */
484enum { MZ_OK = 0, MZ_STREAM_END = 1, MZ_NEED_DICT = 2, MZ_ERRNO = -1, MZ_STREAM_ERROR = -2, MZ_DATA_ERROR = -3, MZ_MEM_ERROR = -4, MZ_BUF_ERROR = -5, MZ_VERSION_ERROR = -6, MZ_PARAM_ERROR = -10000 };
485
486/* Compression levels. */
487enum { MZ_NO_COMPRESSION = 0, MZ_BEST_SPEED = 1, MZ_BEST_COMPRESSION = 9, MZ_DEFAULT_COMPRESSION = -1 };
488
489/* Window bits */
490#define MZ_DEFAULT_WINDOW_BITS 15
491
492struct mz_internal_state;
493
494/* Compression/decompression stream struct. */
495typedef struct mz_stream_s
496{
497 const unsigned char *next_in; /* pointer to next byte to read */
498 unsigned int avail_in; /* number of bytes available at next_in */
499 mz_ulong total_in; /* total number of bytes consumed so far */
500
501 unsigned char *next_out; /* pointer to next byte to write */
502 unsigned int avail_out; /* number of bytes that can be written to next_out */
503 mz_ulong total_out; /* total number of bytes produced so far */
504
505 char *msg; /* error msg (unused) */
506 struct mz_internal_state *state; /* internal state, allocated by zalloc/zfree */
507
508 mz_alloc_func zalloc; /* optional heap allocation function (defaults to malloc) */
509 mz_free_func zfree; /* optional heap free function (defaults to free) */
510 void *opaque; /* heap alloc function user pointer */
511
512 int data_type; /* data_type (unused) */
513 mz_ulong adler; /* adler32 of the source or uncompressed data */
514 mz_ulong reserved; /* not used */
515} mz_stream;
516
517typedef mz_stream *mz_streamp;
518
519
520typedef struct
521{
522 tinfl_decompressor m_decomp;
523 mz_uint m_dict_ofs, m_dict_avail, m_first_call, m_has_flushed; int m_window_bits;
524 mz_uint8 m_dict[TINFL_LZ_DICT_SIZE];
525 tinfl_status m_last_status;
526} inflate_state;
527
528static int mz_inflateInit2(mz_streamp pStream, int window_bits)
529{
530 inflate_state *pDecomp;
531 if (!pStream) return MZ_STREAM_ERROR;
532 if ((window_bits != MZ_DEFAULT_WINDOW_BITS) && (-window_bits != MZ_DEFAULT_WINDOW_BITS)) return MZ_PARAM_ERROR;
533
534 pStream->data_type = 0;
535 pStream->adler = 0;
536 pStream->msg = NULL;
537 pStream->total_in = 0;
538 pStream->total_out = 0;
539 pStream->reserved = 0;
540 /* if (!pStream->zalloc) pStream->zalloc = def_alloc_func; */
541 /* if (!pStream->zfree) pStream->zfree = def_free_func; */
542
543 pDecomp = (inflate_state*)pStream->zalloc(pStream->opaque, 1, sizeof(inflate_state));
544 if (!pDecomp) return MZ_MEM_ERROR;
545
546 pStream->state = (struct mz_internal_state *)pDecomp;
547
548 tinfl_init(&pDecomp->m_decomp);
549 pDecomp->m_dict_ofs = 0;
550 pDecomp->m_dict_avail = 0;
551 pDecomp->m_last_status = TINFL_STATUS_NEEDS_MORE_INPUT;
552 pDecomp->m_first_call = 1;
553 pDecomp->m_has_flushed = 0;
554 pDecomp->m_window_bits = window_bits;
555
556 return MZ_OK;
557}
558
559static int mz_inflate(mz_streamp pStream, int flush)
560{
561 inflate_state* pState;
562 mz_uint n, first_call, decomp_flags = TINFL_FLAG_COMPUTE_ADLER32;
563 size_t in_bytes, out_bytes, orig_avail_in;
564 tinfl_status status;
565
566 if ((!pStream) || (!pStream->state)) return MZ_STREAM_ERROR;
567 if (flush == MZ_PARTIAL_FLUSH) flush = MZ_SYNC_FLUSH;
568 if ((flush) && (flush != MZ_SYNC_FLUSH) && (flush != MZ_FINISH)) return MZ_STREAM_ERROR;
569
570 pState = (inflate_state*)pStream->state;
571 if (pState->m_window_bits > 0) decomp_flags |= TINFL_FLAG_PARSE_ZLIB_HEADER;
572 orig_avail_in = pStream->avail_in;
573
574 first_call = pState->m_first_call; pState->m_first_call = 0;
575 if (pState->m_last_status < 0) return MZ_DATA_ERROR;
576
577 if (pState->m_has_flushed && (flush != MZ_FINISH)) return MZ_STREAM_ERROR;
578 pState->m_has_flushed |= (flush == MZ_FINISH);
579
580 if ((flush == MZ_FINISH) && (first_call))
581 {
582 /* MZ_FINISH on the first call implies that the input and output buffers are large enough to hold the entire compressed/decompressed file. */
583 decomp_flags |= TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF;
584 in_bytes = pStream->avail_in; out_bytes = pStream->avail_out;
585 status = tinfl_decompress(&pState->m_decomp, pStream->next_in, &in_bytes, pStream->next_out, pStream->next_out, &out_bytes, decomp_flags);
586 pState->m_last_status = status;
587 pStream->next_in += (mz_uint)in_bytes; pStream->avail_in -= (mz_uint)in_bytes; pStream->total_in += (mz_uint)in_bytes;
588 pStream->adler = tinfl_get_adler32(&pState->m_decomp);
589 pStream->next_out += (mz_uint)out_bytes; pStream->avail_out -= (mz_uint)out_bytes; pStream->total_out += (mz_uint)out_bytes;
590
591 if (status < 0)
592 return MZ_DATA_ERROR;
593 else if (status != TINFL_STATUS_DONE)
594 {
595 pState->m_last_status = TINFL_STATUS_FAILED;
596 return MZ_BUF_ERROR;
597 }
598 return MZ_STREAM_END;
599 }
600 /* flush != MZ_FINISH then we must assume there's more input. */
601 if (flush != MZ_FINISH) decomp_flags |= TINFL_FLAG_HAS_MORE_INPUT;
602
603 if (pState->m_dict_avail)
604 {
605 n = MZ_MIN(pState->m_dict_avail, pStream->avail_out);
606 memcpy(pStream->next_out, pState->m_dict + pState->m_dict_ofs, n);
607 pStream->next_out += n; pStream->avail_out -= n; pStream->total_out += n;
608 pState->m_dict_avail -= n; pState->m_dict_ofs = (pState->m_dict_ofs + n) & (TINFL_LZ_DICT_SIZE - 1);
609 return ((pState->m_last_status == TINFL_STATUS_DONE) && (!pState->m_dict_avail)) ? MZ_STREAM_END : MZ_OK;
610 }
611
612 for ( ; ; )
613 {
614 in_bytes = pStream->avail_in;
615 out_bytes = TINFL_LZ_DICT_SIZE - pState->m_dict_ofs;
616
617 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);
618 pState->m_last_status = status;
619
620 pStream->next_in += (mz_uint)in_bytes; pStream->avail_in -= (mz_uint)in_bytes;
621 pStream->total_in += (mz_uint)in_bytes; pStream->adler = tinfl_get_adler32(&pState->m_decomp);
622
623 pState->m_dict_avail = (mz_uint)out_bytes;
624
625 n = MZ_MIN(pState->m_dict_avail, pStream->avail_out);
626 memcpy(pStream->next_out, pState->m_dict + pState->m_dict_ofs, n);
627 pStream->next_out += n; pStream->avail_out -= n; pStream->total_out += n;
628 pState->m_dict_avail -= n; pState->m_dict_ofs = (pState->m_dict_ofs + n) & (TINFL_LZ_DICT_SIZE - 1);
629
630 if (status < 0)
631 return MZ_DATA_ERROR; /* Stream is corrupted (there could be some uncompressed data left in the output dictionary - oh well). */
632 else if ((status == TINFL_STATUS_NEEDS_MORE_INPUT) && (!orig_avail_in))
633 return MZ_BUF_ERROR; /* Signal caller that we can't make forward progress without supplying more input or by setting flush to MZ_FINISH. */
634 else if (flush == MZ_FINISH)
635 {
636 /* The output buffer MUST be large to hold the remaining uncompressed data when flush==MZ_FINISH. */
637 if (status == TINFL_STATUS_DONE)
638 return pState->m_dict_avail ? MZ_BUF_ERROR : MZ_STREAM_END;
639 /* 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. */
640 else if (!pStream->avail_out)
641 return MZ_BUF_ERROR;
642 }
643 else if ((status == TINFL_STATUS_DONE) || (!pStream->avail_in) || (!pStream->avail_out) || (pState->m_dict_avail))
644 break;
645 }
646
647 return ((status == TINFL_STATUS_DONE) && (!pState->m_dict_avail)) ? MZ_STREAM_END : MZ_OK;
648}
649
650static int mz_inflateEnd(mz_streamp pStream)
651{
652 if (!pStream)
653 return MZ_STREAM_ERROR;
654 if (pStream->state)
655 {
656 pStream->zfree(pStream->opaque, pStream->state);
657 pStream->state = NULL;
658 }
659 return MZ_OK;
660}
661
662/* make this a drop-in replacement for zlib... */
663 #define voidpf void*
664 #define uInt unsigned int
665 #define z_stream mz_stream
666 #define inflateInit2 mz_inflateInit2
667 #define inflate mz_inflate
668 #define inflateEnd mz_inflateEnd
669 #define Z_SYNC_FLUSH MZ_SYNC_FLUSH
670 #define Z_FINISH MZ_FINISH
671 #define Z_OK MZ_OK
672 #define Z_STREAM_END MZ_STREAM_END
673 #define Z_NEED_DICT MZ_NEED_DICT
674 #define Z_ERRNO MZ_ERRNO
675 #define Z_STREAM_ERROR MZ_STREAM_ERROR
676 #define Z_DATA_ERROR MZ_DATA_ERROR
677 #define Z_MEM_ERROR MZ_MEM_ERROR
678 #define Z_BUF_ERROR MZ_BUF_ERROR
679 #define Z_VERSION_ERROR MZ_VERSION_ERROR
680 #define MAX_WBITS 15
681
682#endif /* #ifndef TINFL_HEADER_FILE_ONLY */
683
684/*
685 This is free and unencumbered software released into the public domain.
686
687 Anyone is free to copy, modify, publish, use, compile, sell, or
688 distribute this software, either in source code form or as a compiled
689 binary, for any purpose, commercial or non-commercial, and by any
690 means.
691
692 In jurisdictions that recognize copyright laws, the author or authors
693 of this software dedicate any and all copyright interest in the
694 software to the public domain. We make this dedication for the benefit
695 of the public at large and to the detriment of our heirs and
696 successors. We intend this dedication to be an overt act of
697 relinquishment in perpetuity of all present and future rights to this
698 software under copyright law.
699
700 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
701 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
702 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
703 IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
704 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
705 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
706 OTHER DEALINGS IN THE SOFTWARE.
707
708 For more information, please refer to <https://unlicense.org/>
709*/
710
711