Coverage Report

Created: 2025-09-04 07:51

/src/fluent-bit/lib/miniz/miniz_tinfl.c
Line
Count
Source (jump to first uncovered line)
1
/**************************************************************************
2
 *
3
 * Copyright 2013-2014 RAD Game Tools and Valve Software
4
 * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
5
 * All Rights Reserved.
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in
15
 * all copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
 * THE SOFTWARE.
24
 *
25
 **************************************************************************/
26
27
#include "miniz.h"
28
29
#ifndef MINIZ_NO_INFLATE_APIS
30
31
#ifdef __cplusplus
32
extern "C" {
33
#endif
34
35
/* ------------------- Low-level Decompression (completely independent from all compression API's) */
36
37
7.70k
#define TINFL_MEMCPY(d, s, l) memcpy(d, s, l)
38
94.3k
#define TINFL_MEMSET(p, c, l) memset(p, c, l)
39
40
#define TINFL_CR_BEGIN  \
41
4.27k
    switch (r->m_state) \
42
4.27k
    {                   \
43
2.13k
        case 0:
44
#define TINFL_CR_RETURN(state_index, result) \
45
2.13k
    do                                       \
46
2.13k
    {                                        \
47
2.13k
        status = result;                     \
48
2.13k
        r->m_state = state_index;            \
49
2.13k
        goto common_exit;                    \
50
2.13k
        case state_index:;                   \
51
0
    }                                        \
52
2.13k
    MZ_MACRO_END
53
#define TINFL_CR_RETURN_FOREVER(state_index, result) \
54
1.95k
    do                                               \
55
1.95k
    {                                                \
56
1.95k
        for (;;)                                     \
57
1.95k
        {                                            \
58
1.95k
            TINFL_CR_RETURN(state_index, result);    \
59
1.95k
        }                                            \
60
1.95k
    }                                                \
61
1.95k
    MZ_MACRO_END
62
2.13k
#define TINFL_CR_FINISH }
63
64
#define TINFL_GET_BYTE(state_index, c)                                                                                                                           \
65
32.4k
    do                                                                                                                                                           \
66
32.4k
    {                                                                                                                                                            \
67
32.4k
        while (pIn_buf_cur >= pIn_buf_end)                                                                                                                       \
68
32.4k
        {                                                                                                                                                        \
69
92
            TINFL_CR_RETURN(state_index, (decomp_flags & TINFL_FLAG_HAS_MORE_INPUT) ? TINFL_STATUS_NEEDS_MORE_INPUT : TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS); \
70
92
        }                                                                                                                                                        \
71
32.4k
        c = *pIn_buf_cur++;                                                                                                                                      \
72
32.3k
    }                                                                                                                                                            \
73
35.4k
    MZ_MACRO_END
74
75
#define TINFL_NEED_BITS(state_index, n)                \
76
24.6k
    do                                                 \
77
24.8k
    {                                                  \
78
24.8k
        mz_uint c;                                     \
79
24.8k
        TINFL_GET_BYTE(state_index, c);                \
80
24.8k
        bit_buf |= (((tinfl_bit_buf_t)c) << num_bits); \
81
24.8k
        num_bits += 8;                                 \
82
24.8k
    } while (num_bits < (mz_uint)(n))
83
#define TINFL_SKIP_BITS(state_index, n)      \
84
4.20k
    do                                       \
85
4.20k
    {                                        \
86
4.20k
        if (num_bits < (mz_uint)(n))         \
87
4.20k
        {                                    \
88
0
            TINFL_NEED_BITS(state_index, n); \
89
0
        }                                    \
90
4.20k
        bit_buf >>= (n);                     \
91
4.20k
        num_bits -= (n);                     \
92
4.20k
    }                                        \
93
4.48k
    MZ_MACRO_END
94
#define TINFL_GET_BITS(state_index, b, n)    \
95
321k
    do                                       \
96
321k
    {                                        \
97
321k
        if (num_bits < (mz_uint)(n))         \
98
321k
        {                                    \
99
24.6k
            TINFL_NEED_BITS(state_index, n); \
100
24.6k
        }                                    \
101
321k
        b = bit_buf & ((1 << (n)) - 1);      \
102
321k
        bit_buf >>= (n);                     \
103
321k
        num_bits -= (n);                     \
104
321k
    }                                        \
105
321k
    MZ_MACRO_END
106
107
/* TINFL_HUFF_BITBUF_FILL() is only used rarely, when the number of bytes remaining in the input buffer falls below 2. */
108
/* 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 */
109
/* 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 */
110
/* bit buffer contains >=15 bits (deflate's max. Huffman code size). */
111
#define TINFL_HUFF_BITBUF_FILL(state_index, pLookUp, pTree)                    \
112
3.24k
    do                                                                         \
113
3.92k
    {                                                                          \
114
3.92k
        temp = pLookUp[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)];                \
115
3.92k
        if (temp >= 0)                                                         \
116
3.92k
        {                                                                      \
117
3.74k
            code_len = temp >> 9;                                              \
118
3.74k
            if ((code_len) && (num_bits >= code_len))                          \
119
3.74k
                break;                                                         \
120
3.74k
        }                                                                      \
121
3.92k
        else if (num_bits > TINFL_FAST_LOOKUP_BITS)                            \
122
179
        {                                                                      \
123
113
            code_len = TINFL_FAST_LOOKUP_BITS;                                 \
124
113
            do                                                                 \
125
231
            {                                                                  \
126
231
                temp = pTree[~temp + ((bit_buf >> code_len++) & 1)];           \
127
231
            } while ((temp < 0) && (num_bits >= (code_len + 1)));              \
128
113
            if (temp >= 0)                                                     \
129
113
                break;                                                         \
130
113
        }                                                                      \
131
3.92k
        TINFL_GET_BYTE(state_index, c);                                        \
132
946
        bit_buf |= (((tinfl_bit_buf_t)c) << num_bits);                         \
133
876
        num_bits += 8;                                                         \
134
876
    } while (num_bits < 15);
135
136
/* 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 */
137
/* 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 */
138
/* 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. */
139
/* The slow path is only executed at the very end of the input buffer. */
140
/* v1.16: The original macro handled the case at the very end of the passed-in input buffer, but we also need to handle the case where the user passes in 1+zillion bytes */
141
/* following the deflate data and our non-conservative read-ahead path won't kick in here on this code. This is much trickier. */
142
#define TINFL_HUFF_DECODE(state_index, sym, pLookUp, pTree)                                                                         \
143
82.4M
    do                                                                                                                              \
144
82.4M
    {                                                                                                                               \
145
82.4M
        int temp;                                                                                                                   \
146
82.4M
        mz_uint code_len, c;                                                                                                        \
147
82.4M
        if (num_bits < 15)                                                                                                          \
148
82.4M
        {                                                                                                                           \
149
86.6k
            if ((pIn_buf_end - pIn_buf_cur) < 2)                                                                                    \
150
86.6k
            {                                                                                                                       \
151
3.24k
                TINFL_HUFF_BITBUF_FILL(state_index, pLookUp, pTree);                                                                \
152
3.17k
            }                                                                                                                       \
153
86.6k
            else                                                                                                                    \
154
86.6k
            {                                                                                                                       \
155
83.4k
                bit_buf |= (((tinfl_bit_buf_t)pIn_buf_cur[0]) << num_bits) | (((tinfl_bit_buf_t)pIn_buf_cur[1]) << (num_bits + 8)); \
156
83.4k
                pIn_buf_cur += 2;                                                                                                   \
157
83.4k
                num_bits += 16;                                                                                                     \
158
83.4k
            }                                                                                                                       \
159
86.6k
        }                                                                                                                           \
160
82.4M
        if ((temp = pLookUp[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0)                                                          \
161
82.4M
            code_len = temp >> 9, temp &= 511;                                                                                      \
162
82.4M
        else                                                                                                                        \
163
82.4M
        {                                                                                                                           \
164
3.87k
            code_len = TINFL_FAST_LOOKUP_BITS;                                                                                      \
165
3.87k
            do                                                                                                                      \
166
14.8k
            {                                                                                                                       \
167
14.8k
                temp = pTree[~temp + ((bit_buf >> code_len++) & 1)];                                                                \
168
14.8k
            } while (temp < 0);                                                                                                     \
169
3.87k
        }                                                                                                                           \
170
82.4M
        sym = temp;                                                                                                                 \
171
82.4M
        bit_buf >>= code_len;                                                                                                       \
172
82.4M
        num_bits -= code_len;                                                                                                       \
173
82.4M
    }                                                                                                                               \
174
82.4M
    MZ_MACRO_END
175
176
static void tinfl_clear_tree(tinfl_decompressor *r)
177
14.0k
{
178
14.0k
    if (r->m_type == 0)
179
5.41k
        MZ_CLEAR_ARR(r->m_tree_0);
180
8.61k
    else if (r->m_type == 1)
181
5.41k
        MZ_CLEAR_ARR(r->m_tree_1);
182
3.20k
    else
183
3.20k
        MZ_CLEAR_ARR(r->m_tree_2);
184
14.0k
}
185
186
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)
187
2.13k
{
188
2.13k
    static const mz_uint16 s_length_base[31] = { 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0 };
189
2.13k
    static const mz_uint8 s_length_extra[31] = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 0, 0 };
190
2.13k
    static const mz_uint16 s_dist_base[32] = { 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577, 0, 0 };
191
2.13k
    static const mz_uint8 s_dist_extra[32] = { 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13 };
192
2.13k
    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 };
193
2.13k
    static const mz_uint16 s_min_table_sizes[3] = { 257, 1, 4 };
194
195
2.13k
    mz_int16 *pTrees[3];
196
2.13k
    mz_uint8 *pCode_sizes[3];
197
198
2.13k
    tinfl_status status = TINFL_STATUS_FAILED;
199
2.13k
    mz_uint32 num_bits, dist, counter, num_extra;
200
2.13k
    tinfl_bit_buf_t bit_buf;
201
2.13k
    const mz_uint8 *pIn_buf_cur = pIn_buf_next, *const pIn_buf_end = pIn_buf_next + *pIn_buf_size;
202
2.13k
    mz_uint8 *pOut_buf_cur = pOut_buf_next, *const pOut_buf_end = pOut_buf_next ? pOut_buf_next + *pOut_buf_size : NULL;
203
2.13k
    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;
204
205
    /* 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). */
206
2.13k
    if (((out_buf_size_mask + 1) & out_buf_size_mask) || (pOut_buf_next < pOut_buf_start))
207
0
    {
208
0
        *pIn_buf_size = *pOut_buf_size = 0;
209
0
        return TINFL_STATUS_BAD_PARAM;
210
0
    }
211
212
2.13k
    pTrees[0] = r->m_tree_0;
213
2.13k
    pTrees[1] = r->m_tree_1;
214
2.13k
    pTrees[2] = r->m_tree_2;
215
2.13k
    pCode_sizes[0] = r->m_code_size_0;
216
2.13k
    pCode_sizes[1] = r->m_code_size_1;
217
2.13k
    pCode_sizes[2] = r->m_code_size_2;
218
219
2.13k
    num_bits = r->m_num_bits;
220
2.13k
    bit_buf = r->m_bit_buf;
221
2.13k
    dist = r->m_dist;
222
2.13k
    counter = r->m_counter;
223
2.13k
    num_extra = r->m_num_extra;
224
2.13k
    dist_from_out_buf_start = r->m_dist_from_out_buf_start;
225
2.13k
    TINFL_CR_BEGIN
226
227
2.13k
    bit_buf = num_bits = dist = counter = num_extra = r->m_zhdr0 = r->m_zhdr1 = 0;
228
2.13k
    r->m_z_adler32 = r->m_check_adler32 = 1;
229
2.13k
    if (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER)
230
0
    {
231
0
        TINFL_GET_BYTE(1, r->m_zhdr0);
232
0
        TINFL_GET_BYTE(2, r->m_zhdr1);
233
0
        counter = (((r->m_zhdr0 * 256 + r->m_zhdr1) % 31 != 0) || (r->m_zhdr1 & 32) || ((r->m_zhdr0 & 15) != 8));
234
0
        if (!(decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF))
235
0
            counter |= (((1U << (8U + (r->m_zhdr0 >> 4))) > 32768U) || ((out_buf_size_mask + 1) < (size_t)((size_t)1 << (8U + (r->m_zhdr0 >> 4)))));
236
0
        if (counter)
237
0
        {
238
0
            TINFL_CR_RETURN_FOREVER(36, TINFL_STATUS_FAILED);
239
0
        }
240
0
    }
241
242
2.13k
    do
243
7.80k
    {
244
7.80k
        TINFL_GET_BITS(3, r->m_final, 3);
245
7.80k
        r->m_type = r->m_final >> 1;
246
7.80k
        if (r->m_type == 0)
247
2.35k
        {
248
2.35k
            TINFL_SKIP_BITS(5, num_bits & 7);
249
11.7k
            for (counter = 0; counter < 4; ++counter)
250
9.39k
            {
251
9.39k
                if (num_bits)
252
2.74k
                    TINFL_GET_BITS(6, r->m_raw_header[counter], 8);
253
6.64k
                else
254
6.64k
                    TINFL_GET_BYTE(7, r->m_raw_header[counter]);
255
9.39k
            }
256
2.34k
            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))))
257
44
            {
258
44
                TINFL_CR_RETURN_FOREVER(39, TINFL_STATUS_FAILED);
259
44
            }
260
2.55k
            while ((counter) && (num_bits))
261
258
            {
262
258
                TINFL_GET_BITS(51, dist, 8);
263
258
                while (pOut_buf_cur >= pOut_buf_end)
264
1
                {
265
1
                    TINFL_CR_RETURN(52, TINFL_STATUS_HAS_MORE_OUTPUT);
266
1
                }
267
257
                *pOut_buf_cur++ = (mz_uint8)dist;
268
257
                counter--;
269
257
            }
270
3.66k
            while (counter)
271
1.38k
            {
272
1.38k
                size_t n;
273
1.38k
                while (pOut_buf_cur >= pOut_buf_end)
274
7
                {
275
7
                    TINFL_CR_RETURN(9, TINFL_STATUS_HAS_MORE_OUTPUT);
276
7
                }
277
1.38k
                while (pIn_buf_cur >= pIn_buf_end)
278
17
                {
279
17
                    TINFL_CR_RETURN(38, (decomp_flags & TINFL_FLAG_HAS_MORE_INPUT) ? TINFL_STATUS_NEEDS_MORE_INPUT : TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS);
280
17
                }
281
1.36k
                n = MZ_MIN(MZ_MIN((size_t)(pOut_buf_end - pOut_buf_cur), (size_t)(pIn_buf_end - pIn_buf_cur)), counter);
282
1.36k
                TINFL_MEMCPY(pOut_buf_cur, pIn_buf_cur, n);
283
1.36k
                pIn_buf_cur += n;
284
1.36k
                pOut_buf_cur += n;
285
1.36k
                counter -= (mz_uint)n;
286
1.36k
            }
287
2.30k
        }
288
5.45k
        else if (r->m_type == 3)
289
4
        {
290
4
            TINFL_CR_RETURN_FOREVER(10, TINFL_STATUS_FAILED);
291
4
        }
292
5.45k
        else
293
5.45k
        {
294
5.45k
            if (r->m_type == 1)
295
2.24k
            {
296
2.24k
                mz_uint8 *p = r->m_code_size_0;
297
2.24k
                mz_uint i;
298
2.24k
                r->m_table_sizes[0] = 288;
299
2.24k
                r->m_table_sizes[1] = 32;
300
2.24k
                TINFL_MEMSET(r->m_code_size_1, 5, 32);
301
325k
                for (i = 0; i <= 143; ++i)
302
322k
                    *p++ = 8;
303
253k
                for (; i <= 255; ++i)
304
251k
                    *p++ = 9;
305
56.0k
                for (; i <= 279; ++i)
306
53.8k
                    *p++ = 7;
307
20.1k
                for (; i <= 287; ++i)
308
17.9k
                    *p++ = 8;
309
2.24k
            }
310
3.21k
            else
311
3.21k
            {
312
12.8k
                for (counter = 0; counter < 3; counter++)
313
9.62k
                {
314
9.62k
                    TINFL_GET_BITS(11, r->m_table_sizes[counter], "\05\05\04"[counter]);
315
9.62k
                    r->m_table_sizes[counter] += s_min_table_sizes[counter];
316
9.62k
                }
317
3.20k
                MZ_CLEAR_ARR(r->m_code_size_2);
318
49.0k
                for (counter = 0; counter < r->m_table_sizes[2]; counter++)
319
45.8k
                {
320
45.8k
                    mz_uint s;
321
45.8k
                    TINFL_GET_BITS(14, s, 3);
322
45.8k
                    r->m_code_size_2[s_length_dezigzag[counter]] = (mz_uint8)s;
323
45.8k
                }
324
3.20k
                r->m_table_sizes[2] = 19;
325
3.20k
            }
326
19.4k
            for (; (int)r->m_type >= 0; r->m_type--)
327
14.0k
            {
328
14.0k
                int tree_next, tree_cur;
329
14.0k
                mz_int16 *pLookUp;
330
14.0k
                mz_int16 *pTree;
331
14.0k
                mz_uint8 *pCode_size;
332
14.0k
                mz_uint i, j, used_syms, total, sym_index, next_code[17], total_syms[16];
333
14.0k
                pLookUp = r->m_look_up[r->m_type];
334
14.0k
                pTree = pTrees[r->m_type];
335
14.0k
                pCode_size = pCode_sizes[r->m_type];
336
14.0k
                MZ_CLEAR_ARR(total_syms);
337
14.0k
                TINFL_MEMSET(pLookUp, 0, sizeof(r->m_look_up[0]));
338
14.0k
                tinfl_clear_tree(r);
339
1.74M
                for (i = 0; i < r->m_table_sizes[r->m_type]; ++i)
340
1.72M
                    total_syms[pCode_size[i]]++;
341
14.0k
                used_syms = 0, total = 0;
342
14.0k
                next_code[0] = next_code[1] = 0;
343
224k
                for (i = 1; i <= 15; ++i)
344
210k
                {
345
210k
                    used_syms += total_syms[i];
346
210k
                    next_code[i + 1] = (total = ((total + total_syms[i]) << 1));
347
210k
                }
348
14.0k
                if ((65536 != total) && (used_syms > 1))
349
33
                {
350
33
                    TINFL_CR_RETURN_FOREVER(35, TINFL_STATUS_FAILED);
351
33
                }
352
1.73M
                for (tree_next = -1, sym_index = 0; sym_index < r->m_table_sizes[r->m_type]; ++sym_index)
353
1.72M
                {
354
1.72M
                    mz_uint rev_code = 0, l, cur_code, code_size = pCode_size[sym_index];
355
1.72M
                    if (!code_size)
356
630k
                        continue;
357
1.08M
                    cur_code = next_code[code_size]++;
358
9.48M
                    for (l = code_size; l > 0; l--, cur_code >>= 1)
359
8.39M
                        rev_code = (rev_code << 1) | (cur_code & 1);
360
1.08M
                    if (code_size <= TINFL_FAST_LOOKUP_BITS)
361
1.07M
                    {
362
1.07M
                        mz_int16 k = (mz_int16)((code_size << 9) | sym_index);
363
13.8M
                        while (rev_code < TINFL_FAST_LOOKUP_SIZE)
364
12.7M
                        {
365
12.7M
                            pLookUp[rev_code] = k;
366
12.7M
                            rev_code += (1 << code_size);
367
12.7M
                        }
368
1.07M
                        continue;
369
1.07M
                    }
370
12.2k
                    if (0 == (tree_cur = pLookUp[rev_code & (TINFL_FAST_LOOKUP_SIZE - 1)]))
371
4.33k
                    {
372
4.33k
                        pLookUp[rev_code & (TINFL_FAST_LOOKUP_SIZE - 1)] = (mz_int16)tree_next;
373
4.33k
                        tree_cur = tree_next;
374
4.33k
                        tree_next -= 2;
375
4.33k
                    }
376
12.2k
                    rev_code >>= (TINFL_FAST_LOOKUP_BITS - 1);
377
22.9k
                    for (j = code_size; j > (TINFL_FAST_LOOKUP_BITS + 1); j--)
378
10.7k
                    {
379
10.7k
                        tree_cur -= ((rev_code >>= 1) & 1);
380
10.7k
                        if (!pTree[-tree_cur - 1])
381
3.84k
                        {
382
3.84k
                            pTree[-tree_cur - 1] = (mz_int16)tree_next;
383
3.84k
                            tree_cur = tree_next;
384
3.84k
                            tree_next -= 2;
385
3.84k
                        }
386
6.86k
                        else
387
6.86k
                            tree_cur = pTree[-tree_cur - 1];
388
10.7k
                    }
389
12.2k
                    tree_cur -= ((rev_code >>= 1) & 1);
390
12.2k
                    pTree[-tree_cur - 1] = (mz_int16)sym_index;
391
12.2k
                }
392
13.9k
                if (r->m_type == 2)
393
3.19k
                {
394
434k
                    for (counter = 0; counter < (r->m_table_sizes[0] + r->m_table_sizes[1]);)
395
430k
                    {
396
430k
                        mz_uint s;
397
430k
                        TINFL_HUFF_DECODE(16, dist, r->m_look_up[2], r->m_tree_2);
398
430k
                        if (dist < 16)
399
383k
                        {
400
383k
                            r->m_len_codes[counter++] = (mz_uint8)dist;
401
383k
                            continue;
402
383k
                        }
403
47.1k
                        if ((dist == 16) && (!counter))
404
3
                        {
405
3
                            TINFL_CR_RETURN_FOREVER(17, TINFL_STATUS_FAILED);
406
3
                        }
407
47.1k
                        num_extra = "\02\03\07"[dist - 16];
408
47.1k
                        TINFL_GET_BITS(18, s, num_extra);
409
47.1k
                        s += "\03\03\013"[dist - 16];
410
47.1k
                        TINFL_MEMSET(r->m_len_codes + counter, (dist == 16) ? r->m_len_codes[counter - 1] : 0, s);
411
47.1k
                        counter += s;
412
47.1k
                    }
413
3.18k
                    if ((r->m_table_sizes[0] + r->m_table_sizes[1]) != counter)
414
11
                    {
415
11
                        TINFL_CR_RETURN_FOREVER(21, TINFL_STATUS_FAILED);
416
11
                    }
417
3.17k
                    TINFL_MEMCPY(r->m_code_size_0, r->m_len_codes, r->m_table_sizes[0]);
418
3.17k
                    TINFL_MEMCPY(r->m_code_size_1, r->m_len_codes + r->m_table_sizes[0], r->m_table_sizes[1]);
419
3.17k
                }
420
13.9k
            }
421
5.39k
            for (;;)
422
668k
            {
423
668k
                mz_uint8 *pSrc;
424
668k
                for (;;)
425
340M
                {
426
340M
                    if (((pIn_buf_end - pIn_buf_cur) < 4) || ((pOut_buf_end - pOut_buf_cur) < 2))
427
81.3M
                    {
428
81.3M
                        TINFL_HUFF_DECODE(23, counter, r->m_look_up[0], r->m_tree_0);
429
81.3M
                        if (counter >= 256)
430
5.52k
                            break;
431
81.3M
                        while (pOut_buf_cur >= pOut_buf_end)
432
47
                        {
433
47
                            TINFL_CR_RETURN(24, TINFL_STATUS_HAS_MORE_OUTPUT);
434
47
                        }
435
81.3M
                        *pOut_buf_cur++ = (mz_uint8)counter;
436
81.3M
                    }
437
258M
                    else
438
258M
                    {
439
258M
                        int sym2;
440
258M
                        mz_uint code_len;
441
258M
#if TINFL_USE_64BIT_BITBUF
442
258M
                        if (num_bits < 30)
443
902k
                        {
444
902k
                            bit_buf |= (((tinfl_bit_buf_t)MZ_READ_LE32(pIn_buf_cur)) << num_bits);
445
902k
                            pIn_buf_cur += 4;
446
902k
                            num_bits += 32;
447
902k
                        }
448
#else
449
                        if (num_bits < 15)
450
                        {
451
                            bit_buf |= (((tinfl_bit_buf_t)MZ_READ_LE16(pIn_buf_cur)) << num_bits);
452
                            pIn_buf_cur += 2;
453
                            num_bits += 16;
454
                        }
455
#endif
456
258M
                        if ((sym2 = r->m_look_up[0][bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0)
457
258M
                            code_len = sym2 >> 9;
458
10.1k
                        else
459
10.1k
                        {
460
10.1k
                            code_len = TINFL_FAST_LOOKUP_BITS;
461
10.1k
                            do
462
20.6k
                            {
463
20.6k
                                sym2 = r->m_tree_0[~sym2 + ((bit_buf >> code_len++) & 1)];
464
20.6k
                            } while (sym2 < 0);
465
10.1k
                        }
466
258M
                        counter = sym2;
467
258M
                        bit_buf >>= code_len;
468
258M
                        num_bits -= code_len;
469
258M
                        if (counter & 256)
470
597k
                            break;
471
472
#if !TINFL_USE_64BIT_BITBUF
473
                        if (num_bits < 15)
474
                        {
475
                            bit_buf |= (((tinfl_bit_buf_t)MZ_READ_LE16(pIn_buf_cur)) << num_bits);
476
                            pIn_buf_cur += 2;
477
                            num_bits += 16;
478
                        }
479
#endif
480
258M
                        if ((sym2 = r->m_look_up[0][bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0)
481
258M
                            code_len = sym2 >> 9;
482
7.26k
                        else
483
7.26k
                        {
484
7.26k
                            code_len = TINFL_FAST_LOOKUP_BITS;
485
7.26k
                            do
486
15.6k
                            {
487
15.6k
                                sym2 = r->m_tree_0[~sym2 + ((bit_buf >> code_len++) & 1)];
488
15.6k
                            } while (sym2 < 0);
489
7.26k
                        }
490
258M
                        bit_buf >>= code_len;
491
258M
                        num_bits -= code_len;
492
493
258M
                        pOut_buf_cur[0] = (mz_uint8)counter;
494
258M
                        if (sym2 & 256)
495
65.7k
                        {
496
65.7k
                            pOut_buf_cur++;
497
65.7k
                            counter = sym2;
498
65.7k
                            break;
499
65.7k
                        }
500
258M
                        pOut_buf_cur[1] = (mz_uint8)sym2;
501
258M
                        pOut_buf_cur += 2;
502
258M
                    }
503
340M
                }
504
668k
                if ((counter &= 511) == 256)
505
5.24k
                    break;
506
507
663k
                num_extra = s_length_extra[counter - 257];
508
663k
                counter = s_length_base[counter - 257];
509
663k
                if (num_extra)
510
59.1k
                {
511
59.1k
                    mz_uint extra_bits;
512
59.1k
                    TINFL_GET_BITS(25, extra_bits, num_extra);
513
59.1k
                    counter += extra_bits;
514
59.1k
                }
515
516
663k
                TINFL_HUFF_DECODE(26, dist, r->m_look_up[1], r->m_tree_1);
517
663k
                num_extra = s_dist_extra[dist];
518
663k
                dist = s_dist_base[dist];
519
663k
                if (num_extra)
520
148k
                {
521
148k
                    mz_uint extra_bits;
522
148k
                    TINFL_GET_BITS(27, extra_bits, num_extra);
523
148k
                    dist += extra_bits;
524
148k
                }
525
526
663k
                dist_from_out_buf_start = pOut_buf_cur - pOut_buf_start;
527
663k
                if ((dist == 0 || dist > dist_from_out_buf_start || dist_from_out_buf_start == 0) && (decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF))
528
9
                {
529
9
                    TINFL_CR_RETURN_FOREVER(37, TINFL_STATUS_FAILED);
530
9
                }
531
532
663k
                pSrc = pOut_buf_start + ((dist_from_out_buf_start - dist) & out_buf_size_mask);
533
534
663k
                if ((MZ_MAX(pOut_buf_cur, pSrc) + counter) > pOut_buf_end)
535
16
                {
536
1.03k
                    while (counter--)
537
1.03k
                    {
538
1.03k
                        while (pOut_buf_cur >= pOut_buf_end)
539
16
                        {
540
16
                            TINFL_CR_RETURN(53, TINFL_STATUS_HAS_MORE_OUTPUT);
541
16
                        }
542
1.02k
                        *pOut_buf_cur++ = pOut_buf_start[(dist_from_out_buf_start++ - dist) & out_buf_size_mask];
543
1.02k
                    }
544
0
                    continue;
545
16
                }
546
#if MINIZ_USE_UNALIGNED_LOADS_AND_STORES
547
                else if ((counter >= 9) && (counter <= dist))
548
                {
549
                    const mz_uint8 *pSrc_end = pSrc + (counter & ~7);
550
                    do
551
                    {
552
#ifdef MINIZ_UNALIGNED_USE_MEMCPY
553
            memcpy(pOut_buf_cur, pSrc, sizeof(mz_uint32)*2);
554
#else
555
                        ((mz_uint32 *)pOut_buf_cur)[0] = ((const mz_uint32 *)pSrc)[0];
556
                        ((mz_uint32 *)pOut_buf_cur)[1] = ((const mz_uint32 *)pSrc)[1];
557
#endif
558
                        pOut_buf_cur += 8;
559
                    } while ((pSrc += 8) < pSrc_end);
560
                    if ((counter &= 7) < 3)
561
                    {
562
                        if (counter)
563
                        {
564
                            pOut_buf_cur[0] = pSrc[0];
565
                            if (counter > 1)
566
                                pOut_buf_cur[1] = pSrc[1];
567
                            pOut_buf_cur += counter;
568
                        }
569
                        continue;
570
                    }
571
                }
572
#endif
573
42.6M
                while(counter>2)
574
42.0M
                {
575
42.0M
                    pOut_buf_cur[0] = pSrc[0];
576
42.0M
                    pOut_buf_cur[1] = pSrc[1];
577
42.0M
                    pOut_buf_cur[2] = pSrc[2];
578
42.0M
                    pOut_buf_cur += 3;
579
42.0M
                    pSrc += 3;
580
42.0M
          counter -= 3;
581
42.0M
                }
582
663k
                if (counter > 0)
583
88.8k
                {
584
88.8k
                    pOut_buf_cur[0] = pSrc[0];
585
88.8k
                    if (counter > 1)
586
34.6k
                        pOut_buf_cur[1] = pSrc[1];
587
88.8k
                    pOut_buf_cur += counter;
588
88.8k
                }
589
663k
            }
590
5.39k
        }
591
7.80k
    } while (!(r->m_final & 1));
592
593
    /* Ensure byte alignment and put back any bytes from the bitbuf if we've looked ahead too far on gzip, or other Deflate streams followed by arbitrary data. */
594
    /* I'm being super conservative here. A number of simplifications can be made to the byte alignment part, and the Adler32 check shouldn't ever need to worry about reading from the bitbuf now. */
595
1.85k
    TINFL_SKIP_BITS(32, num_bits & 7);
596
2.04k
    while ((pIn_buf_cur > pIn_buf_next) && (num_bits >= 8))
597
197
    {
598
197
        --pIn_buf_cur;
599
197
        num_bits -= 8;
600
197
    }
601
1.85k
    bit_buf &= ~(~(tinfl_bit_buf_t)0 << num_bits);
602
1.85k
    MZ_ASSERT(!num_bits); /* if this assert fires then we've read beyond the end of non-deflate/zlib streams with following data (such as gzip streams). */
603
604
1.85k
    if (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER)
605
0
    {
606
0
        for (counter = 0; counter < 4; ++counter)
607
0
        {
608
0
            mz_uint s;
609
0
            if (num_bits)
610
0
                TINFL_GET_BITS(41, s, 8);
611
0
            else
612
0
                TINFL_GET_BYTE(42, s);
613
0
            r->m_z_adler32 = (r->m_z_adler32 << 8) | s;
614
0
        }
615
0
    }
616
2.13k
    TINFL_CR_RETURN_FOREVER(34, TINFL_STATUS_DONE);
617
618
2.13k
    TINFL_CR_FINISH
619
620
2.13k
common_exit:
621
    /* As long as we aren't telling the caller that we NEED more input to make forward progress: */
622
    /* Put back any bytes from the bitbuf in case we've looked ahead too far on gzip, or other Deflate streams followed by arbitrary data. */
623
    /* We need to be very careful here to NOT push back any bytes we definitely know we need to make forward progress, though, or we'll lock the caller up into an inf loop. */
624
2.13k
    if ((status != TINFL_STATUS_NEEDS_MORE_INPUT) && (status != TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS))
625
2.02k
    {
626
2.34k
        while ((pIn_buf_cur > pIn_buf_next) && (num_bits >= 8))
627
321
        {
628
321
            --pIn_buf_cur;
629
321
            num_bits -= 8;
630
321
        }
631
2.02k
    }
632
2.13k
    r->m_num_bits = num_bits;
633
2.13k
    r->m_bit_buf = bit_buf & ~(~(tinfl_bit_buf_t)0 << num_bits);
634
2.13k
    r->m_dist = dist;
635
2.13k
    r->m_counter = counter;
636
2.13k
    r->m_num_extra = num_extra;
637
2.13k
    r->m_dist_from_out_buf_start = dist_from_out_buf_start;
638
2.13k
    *pIn_buf_size = pIn_buf_cur - pIn_buf_next;
639
2.13k
    *pOut_buf_size = pOut_buf_cur - pOut_buf_next;
640
2.13k
    if ((decomp_flags & (TINFL_FLAG_PARSE_ZLIB_HEADER | TINFL_FLAG_COMPUTE_ADLER32)) && (status >= 0))
641
1.92k
    {
642
1.92k
        const mz_uint8 *ptr = pOut_buf_next;
643
1.92k
        size_t buf_len = *pOut_buf_size;
644
1.92k
        mz_uint32 i, s1 = r->m_check_adler32 & 0xffff, s2 = r->m_check_adler32 >> 16;
645
1.92k
        size_t block_len = buf_len % 5552;
646
112k
        while (buf_len)
647
110k
        {
648
75.7M
            for (i = 0; i + 7 < block_len; i += 8, ptr += 8)
649
75.6M
            {
650
75.6M
                s1 += ptr[0], s2 += s1;
651
75.6M
                s1 += ptr[1], s2 += s1;
652
75.6M
                s1 += ptr[2], s2 += s1;
653
75.6M
                s1 += ptr[3], s2 += s1;
654
75.6M
                s1 += ptr[4], s2 += s1;
655
75.6M
                s1 += ptr[5], s2 += s1;
656
75.6M
                s1 += ptr[6], s2 += s1;
657
75.6M
                s1 += ptr[7], s2 += s1;
658
75.6M
            }
659
116k
            for (; i < block_len; ++i)
660
5.98k
                s1 += *ptr++, s2 += s1;
661
110k
            s1 %= 65521U, s2 %= 65521U;
662
110k
            buf_len -= block_len;
663
110k
            block_len = 5552;
664
110k
        }
665
1.92k
        r->m_check_adler32 = (s2 << 16) + s1;
666
1.92k
        if ((status == TINFL_STATUS_DONE) && (decomp_flags & TINFL_FLAG_PARSE_ZLIB_HEADER) && (r->m_check_adler32 != r->m_z_adler32))
667
0
            status = TINFL_STATUS_ADLER32_MISMATCH;
668
1.92k
    }
669
2.13k
    return status;
670
2.13k
}
671
672
/* Higher level helper functions. */
673
void *tinfl_decompress_mem_to_heap(const void *pSrc_buf, size_t src_buf_len, size_t *pOut_len, int flags)
674
0
{
675
0
    tinfl_decompressor decomp;
676
0
    void *pBuf = NULL, *pNew_buf;
677
0
    size_t src_buf_ofs = 0, out_buf_capacity = 0;
678
0
    *pOut_len = 0;
679
0
    tinfl_init(&decomp);
680
0
    for (;;)
681
0
    {
682
0
        size_t src_buf_size = src_buf_len - src_buf_ofs, dst_buf_size = out_buf_capacity - *pOut_len, new_out_buf_capacity;
683
0
        tinfl_status status = tinfl_decompress(&decomp, (const mz_uint8 *)pSrc_buf + src_buf_ofs, &src_buf_size, (mz_uint8 *)pBuf, pBuf ? (mz_uint8 *)pBuf + *pOut_len : NULL, &dst_buf_size,
684
0
                                               (flags & ~TINFL_FLAG_HAS_MORE_INPUT) | TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF);
685
0
        if ((status < 0) || (status == TINFL_STATUS_NEEDS_MORE_INPUT))
686
0
        {
687
0
            MZ_FREE(pBuf);
688
0
            *pOut_len = 0;
689
0
            return NULL;
690
0
        }
691
0
        src_buf_ofs += src_buf_size;
692
0
        *pOut_len += dst_buf_size;
693
0
        if (status == TINFL_STATUS_DONE)
694
0
            break;
695
0
        new_out_buf_capacity = out_buf_capacity * 2;
696
0
        if (new_out_buf_capacity < 128)
697
0
            new_out_buf_capacity = 128;
698
0
        pNew_buf = MZ_REALLOC(pBuf, new_out_buf_capacity);
699
0
        if (!pNew_buf)
700
0
        {
701
0
            MZ_FREE(pBuf);
702
0
            *pOut_len = 0;
703
0
            return NULL;
704
0
        }
705
0
        pBuf = pNew_buf;
706
0
        out_buf_capacity = new_out_buf_capacity;
707
0
    }
708
0
    return pBuf;
709
0
}
710
711
size_t tinfl_decompress_mem_to_mem(void *pOut_buf, size_t out_buf_len, const void *pSrc_buf, size_t src_buf_len, int flags)
712
0
{
713
0
    tinfl_decompressor decomp;
714
0
    tinfl_status status;
715
0
    tinfl_init(&decomp);
716
0
    status = tinfl_decompress(&decomp, (const mz_uint8 *)pSrc_buf, &src_buf_len, (mz_uint8 *)pOut_buf, (mz_uint8 *)pOut_buf, &out_buf_len, (flags & ~TINFL_FLAG_HAS_MORE_INPUT) | TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF);
717
0
    return (status != TINFL_STATUS_DONE) ? TINFL_DECOMPRESS_MEM_TO_MEM_FAILED : out_buf_len;
718
0
}
719
720
int tinfl_decompress_mem_to_callback(const void *pIn_buf, size_t *pIn_buf_size, tinfl_put_buf_func_ptr pPut_buf_func, void *pPut_buf_user, int flags)
721
0
{
722
0
    int result = 0;
723
0
    tinfl_decompressor decomp;
724
0
    mz_uint8 *pDict = (mz_uint8 *)MZ_MALLOC(TINFL_LZ_DICT_SIZE);
725
0
    size_t in_buf_ofs = 0, dict_ofs = 0;
726
0
    if (!pDict)
727
0
        return TINFL_STATUS_FAILED;
728
0
    memset(pDict,0,TINFL_LZ_DICT_SIZE);
729
0
    tinfl_init(&decomp);
730
0
    for (;;)
731
0
    {
732
0
        size_t in_buf_size = *pIn_buf_size - in_buf_ofs, dst_buf_size = TINFL_LZ_DICT_SIZE - dict_ofs;
733
0
        tinfl_status status = tinfl_decompress(&decomp, (const mz_uint8 *)pIn_buf + in_buf_ofs, &in_buf_size, pDict, pDict + dict_ofs, &dst_buf_size,
734
0
                                               (flags & ~(TINFL_FLAG_HAS_MORE_INPUT | TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF)));
735
0
        in_buf_ofs += in_buf_size;
736
0
        if ((dst_buf_size) && (!(*pPut_buf_func)(pDict + dict_ofs, (int)dst_buf_size, pPut_buf_user)))
737
0
            break;
738
0
        if (status != TINFL_STATUS_HAS_MORE_OUTPUT)
739
0
        {
740
0
            result = (status == TINFL_STATUS_DONE);
741
0
            break;
742
0
        }
743
0
        dict_ofs = (dict_ofs + dst_buf_size) & (TINFL_LZ_DICT_SIZE - 1);
744
0
    }
745
0
    MZ_FREE(pDict);
746
0
    *pIn_buf_size = in_buf_ofs;
747
0
    return result;
748
0
}
749
750
#ifndef MINIZ_NO_MALLOC
751
tinfl_decompressor *tinfl_decompressor_alloc(void)
752
0
{
753
0
    tinfl_decompressor *pDecomp = (tinfl_decompressor *)MZ_MALLOC(sizeof(tinfl_decompressor));
754
0
    if (pDecomp)
755
0
        tinfl_init(pDecomp);
756
0
    return pDecomp;
757
0
}
758
759
void tinfl_decompressor_free(tinfl_decompressor *pDecomp)
760
0
{
761
0
    MZ_FREE(pDecomp);
762
0
}
763
#endif
764
765
#ifdef __cplusplus
766
}
767
#endif
768
769
#endif /*#ifndef MINIZ_NO_INFLATE_APIS*/