Coverage Report

Created: 2026-09-28 07:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/fluent-bit/lib/miniz/miniz_tinfl.c
Line
Count
Source
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
8.08k
#define TINFL_MEMCPY(d, s, l) memcpy(d, s, l)
38
97.6k
#define TINFL_MEMSET(p, c, l) memset(p, c, l)
39
40
#define TINFL_CR_BEGIN  \
41
4.73k
    switch (r->m_state) \
42
4.73k
    {                   \
43
2.36k
        case 0:
44
#define TINFL_CR_RETURN(state_index, result) \
45
2.36k
    do                                       \
46
2.36k
    {                                        \
47
2.36k
        status = result;                     \
48
2.36k
        r->m_state = state_index;            \
49
2.36k
        goto common_exit;                    \
50
2.36k
        case state_index:;                   \
51
0
    }                                        \
52
2.36k
    MZ_MACRO_END
53
#define TINFL_CR_RETURN_FOREVER(state_index, result) \
54
2.18k
    do                                               \
55
2.18k
    {                                                \
56
2.18k
        for (;;)                                     \
57
2.18k
        {                                            \
58
2.18k
            TINFL_CR_RETURN(state_index, result);    \
59
2.18k
        }                                            \
60
2.18k
    }                                                \
61
2.18k
    MZ_MACRO_END
62
2.36k
#define TINFL_CR_FINISH }
63
64
#define TINFL_GET_BYTE(state_index, c)                                                                                                                           \
65
37.3k
    do                                                                                                                                                           \
66
37.3k
    {                                                                                                                                                            \
67
37.3k
        while (pIn_buf_cur >= pIn_buf_end)                                                                                                                       \
68
37.3k
        {                                                                                                                                                        \
69
104
            TINFL_CR_RETURN(state_index, (decomp_flags & TINFL_FLAG_HAS_MORE_INPUT) ? TINFL_STATUS_NEEDS_MORE_INPUT : TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS); \
70
104
        }                                                                                                                                                        \
71
37.3k
        c = *pIn_buf_cur++;                                                                                                                                      \
72
37.2k
    }                                                                                                                                                            \
73
40.7k
    MZ_MACRO_END
74
75
#define TINFL_NEED_BITS(state_index, n)                \
76
26.4k
    do                                                 \
77
26.7k
    {                                                  \
78
26.7k
        mz_uint c;                                     \
79
26.7k
        TINFL_GET_BYTE(state_index, c);                \
80
26.7k
        bit_buf |= (((tinfl_bit_buf_t)c) << num_bits); \
81
26.7k
        num_bits += 8;                                 \
82
26.7k
    } while (num_bits < (mz_uint)(n))
83
#define TINFL_SKIP_BITS(state_index, n)      \
84
5.16k
    do                                       \
85
5.16k
    {                                        \
86
5.16k
        if (num_bits < (mz_uint)(n))         \
87
5.16k
        {                                    \
88
0
            TINFL_NEED_BITS(state_index, n); \
89
0
        }                                    \
90
5.16k
        bit_buf >>= (n);                     \
91
5.16k
        num_bits -= (n);                     \
92
5.16k
    }                                        \
93
5.48k
    MZ_MACRO_END
94
#define TINFL_GET_BITS(state_index, b, n)    \
95
334k
    do                                       \
96
334k
    {                                        \
97
334k
        if (num_bits < (mz_uint)(n))         \
98
334k
        {                                    \
99
26.4k
            TINFL_NEED_BITS(state_index, n); \
100
26.4k
        }                                    \
101
334k
        b = bit_buf & ((1 << (n)) - 1);      \
102
334k
        bit_buf >>= (n);                     \
103
334k
        num_bits -= (n);                     \
104
334k
    }                                        \
105
334k
    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.66k
    do                                                                         \
113
4.50k
    {                                                                          \
114
4.50k
        temp = pLookUp[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)];                \
115
4.50k
        if (temp >= 0)                                                         \
116
4.50k
        {                                                                      \
117
4.31k
            code_len = temp >> 9;                                              \
118
4.31k
            if ((code_len) && (num_bits >= code_len))                          \
119
4.31k
                break;                                                         \
120
4.31k
        }                                                                      \
121
4.50k
        else if (num_bits > TINFL_FAST_LOOKUP_BITS)                            \
122
184
        {                                                                      \
123
119
            code_len = TINFL_FAST_LOOKUP_BITS;                                 \
124
119
            do                                                                 \
125
216
            {                                                                  \
126
216
                temp = pTree[~temp + ((bit_buf >> code_len++) & 1)];           \
127
216
            } while ((temp < 0) && (num_bits >= (code_len + 1)));              \
128
119
            if (temp >= 0)                                                     \
129
119
                break;                                                         \
130
119
        }                                                                      \
131
4.50k
        TINFL_GET_BYTE(state_index, c);                                        \
132
1.11k
        bit_buf |= (((tinfl_bit_buf_t)c) << num_bits);                         \
133
1.03k
        num_bits += 8;                                                         \
134
1.03k
    } 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
44.5M
    do                                                                                                                              \
144
44.5M
    {                                                                                                                               \
145
44.5M
        int temp;                                                                                                                   \
146
44.5M
        mz_uint code_len, c;                                                                                                        \
147
44.5M
        if (num_bits < 15)                                                                                                          \
148
44.5M
        {                                                                                                                           \
149
88.7k
            if ((pIn_buf_end - pIn_buf_cur) < 2)                                                                                    \
150
88.7k
            {                                                                                                                       \
151
3.66k
                TINFL_HUFF_BITBUF_FILL(state_index, pLookUp, pTree);                                                                \
152
3.58k
            }                                                                                                                       \
153
88.7k
            else                                                                                                                    \
154
88.7k
            {                                                                                                                       \
155
85.1k
                bit_buf |= (((tinfl_bit_buf_t)pIn_buf_cur[0]) << num_bits) | (((tinfl_bit_buf_t)pIn_buf_cur[1]) << (num_bits + 8)); \
156
85.1k
                pIn_buf_cur += 2;                                                                                                   \
157
85.1k
                num_bits += 16;                                                                                                     \
158
85.1k
            }                                                                                                                       \
159
88.7k
        }                                                                                                                           \
160
44.5M
        if ((temp = pLookUp[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0)                                                          \
161
44.5M
            code_len = temp >> 9, temp &= 511;                                                                                      \
162
44.5M
        else                                                                                                                        \
163
44.5M
        {                                                                                                                           \
164
2.98k
            code_len = TINFL_FAST_LOOKUP_BITS;                                                                                      \
165
2.98k
            do                                                                                                                      \
166
10.5k
            {                                                                                                                       \
167
10.5k
                temp = pTree[~temp + ((bit_buf >> code_len++) & 1)];                                                                \
168
10.5k
            } while (temp < 0);                                                                                                     \
169
2.98k
        }                                                                                                                           \
170
44.5M
        sym = temp;                                                                                                                 \
171
44.5M
        bit_buf >>= code_len;                                                                                                       \
172
44.5M
        num_bits -= code_len;                                                                                                       \
173
44.5M
    }                                                                                                                               \
174
44.5M
    MZ_MACRO_END
175
176
static void tinfl_clear_tree(tinfl_decompressor *r)
177
13.7k
{
178
13.7k
    if (r->m_type == 0)
179
5.31k
        MZ_CLEAR_ARR(r->m_tree_0);
180
8.45k
    else if (r->m_type == 1)
181
5.31k
        MZ_CLEAR_ARR(r->m_tree_1);
182
3.13k
    else
183
3.13k
        MZ_CLEAR_ARR(r->m_tree_2);
184
13.7k
}
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.36k
{
188
2.36k
    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.36k
    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.36k
    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.36k
    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.36k
    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.36k
    static const mz_uint16 s_min_table_sizes[3] = { 257, 1, 4 };
194
195
2.36k
    mz_int16 *pTrees[3];
196
2.36k
    mz_uint8 *pCode_sizes[3];
197
198
2.36k
    tinfl_status status = TINFL_STATUS_FAILED;
199
2.36k
    mz_uint32 num_bits, dist, counter, num_extra;
200
2.36k
    tinfl_bit_buf_t bit_buf;
201
2.36k
    const mz_uint8 *pIn_buf_cur = pIn_buf_next, *const pIn_buf_end = pIn_buf_next + *pIn_buf_size;
202
2.36k
    mz_uint8 *pOut_buf_cur = pOut_buf_next, *const pOut_buf_end = pOut_buf_next ? pOut_buf_next + *pOut_buf_size : NULL;
203
2.36k
    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.36k
    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.36k
    pTrees[0] = r->m_tree_0;
213
2.36k
    pTrees[1] = r->m_tree_1;
214
2.36k
    pTrees[2] = r->m_tree_2;
215
2.36k
    pCode_sizes[0] = r->m_code_size_0;
216
2.36k
    pCode_sizes[1] = r->m_code_size_1;
217
2.36k
    pCode_sizes[2] = r->m_code_size_2;
218
219
2.36k
    num_bits = r->m_num_bits;
220
2.36k
    bit_buf = r->m_bit_buf;
221
2.36k
    dist = r->m_dist;
222
2.36k
    counter = r->m_counter;
223
2.36k
    num_extra = r->m_num_extra;
224
2.36k
    dist_from_out_buf_start = r->m_dist_from_out_buf_start;
225
2.36k
    TINFL_CR_BEGIN
226
227
2.36k
    bit_buf = num_bits = dist = counter = num_extra = r->m_zhdr0 = r->m_zhdr1 = 0;
228
2.36k
    r->m_z_adler32 = r->m_check_adler32 = 1;
229
2.36k
    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.36k
    do
243
8.49k
    {
244
8.49k
        TINFL_GET_BITS(3, r->m_final, 3);
245
8.48k
        r->m_type = r->m_final >> 1;
246
8.48k
        if (r->m_type == 0)
247
3.12k
        {
248
3.12k
            TINFL_SKIP_BITS(5, num_bits & 7);
249
15.5k
            for (counter = 0; counter < 4; ++counter)
250
12.4k
            {
251
12.4k
                if (num_bits)
252
3.03k
                    TINFL_GET_BITS(6, r->m_raw_header[counter], 8);
253
9.43k
                else
254
9.43k
                    TINFL_GET_BYTE(7, r->m_raw_header[counter]);
255
12.4k
            }
256
3.11k
            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
56
            {
258
56
                TINFL_CR_RETURN_FOREVER(39, TINFL_STATUS_FAILED);
259
56
            }
260
3.35k
            while ((counter) && (num_bits))
261
302
            {
262
302
                TINFL_GET_BITS(51, dist, 8);
263
302
                while (pOut_buf_cur >= pOut_buf_end)
264
2
                {
265
2
                    TINFL_CR_RETURN(52, TINFL_STATUS_HAS_MORE_OUTPUT);
266
2
                }
267
300
                *pOut_buf_cur++ = (mz_uint8)dist;
268
300
                counter--;
269
300
            }
270
4.95k
            while (counter)
271
1.91k
            {
272
1.91k
                size_t n;
273
1.91k
                while (pOut_buf_cur >= pOut_buf_end)
274
4
                {
275
4
                    TINFL_CR_RETURN(9, TINFL_STATUS_HAS_MORE_OUTPUT);
276
4
                }
277
1.91k
                while (pIn_buf_cur >= pIn_buf_end)
278
10
                {
279
10
                    TINFL_CR_RETURN(38, (decomp_flags & TINFL_FLAG_HAS_MORE_INPUT) ? TINFL_STATUS_NEEDS_MORE_INPUT : TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS);
280
10
                }
281
1.90k
                n = MZ_MIN(MZ_MIN((size_t)(pOut_buf_end - pOut_buf_cur), (size_t)(pIn_buf_end - pIn_buf_cur)), counter);
282
1.90k
                TINFL_MEMCPY(pOut_buf_cur, pIn_buf_cur, n);
283
1.90k
                pIn_buf_cur += n;
284
1.90k
                pOut_buf_cur += n;
285
1.90k
                counter -= (mz_uint)n;
286
1.90k
            }
287
3.05k
        }
288
5.36k
        else if (r->m_type == 3)
289
7
        {
290
7
            TINFL_CR_RETURN_FOREVER(10, TINFL_STATUS_FAILED);
291
7
        }
292
5.36k
        else
293
5.36k
        {
294
5.36k
            if (r->m_type == 1)
295
2.22k
            {
296
2.22k
                mz_uint8 *p = r->m_code_size_0;
297
2.22k
                mz_uint i;
298
2.22k
                r->m_table_sizes[0] = 288;
299
2.22k
                r->m_table_sizes[1] = 32;
300
2.22k
                TINFL_MEMSET(r->m_code_size_1, 5, 32);
301
322k
                for (i = 0; i <= 143; ++i)
302
320k
                    *p++ = 8;
303
251k
                for (; i <= 255; ++i)
304
249k
                    *p++ = 9;
305
55.6k
                for (; i <= 279; ++i)
306
53.3k
                    *p++ = 7;
307
20.0k
                for (; i <= 287; ++i)
308
17.7k
                    *p++ = 8;
309
2.22k
            }
310
3.13k
            else
311
3.13k
            {
312
12.5k
                for (counter = 0; counter < 3; counter++)
313
9.41k
                {
314
9.41k
                    TINFL_GET_BITS(11, r->m_table_sizes[counter], "\05\05\04"[counter]);
315
9.40k
                    r->m_table_sizes[counter] += s_min_table_sizes[counter];
316
9.40k
                }
317
3.13k
                MZ_CLEAR_ARR(r->m_code_size_2);
318
49.1k
                for (counter = 0; counter < r->m_table_sizes[2]; counter++)
319
46.0k
                {
320
46.0k
                    mz_uint s;
321
46.0k
                    TINFL_GET_BITS(14, s, 3);
322
46.0k
                    r->m_code_size_2[s_length_dezigzag[counter]] = (mz_uint8)s;
323
46.0k
                }
324
3.13k
                r->m_table_sizes[2] = 19;
325
3.13k
            }
326
19.0k
            for (; (int)r->m_type >= 0; r->m_type--)
327
13.7k
            {
328
13.7k
                int tree_next, tree_cur;
329
13.7k
                mz_int16 *pLookUp;
330
13.7k
                mz_int16 *pTree;
331
13.7k
                mz_uint8 *pCode_size;
332
13.7k
                mz_uint i, j, used_syms, total, sym_index, next_code[17], total_syms[16];
333
13.7k
                pLookUp = r->m_look_up[r->m_type];
334
13.7k
                pTree = pTrees[r->m_type];
335
13.7k
                pCode_size = pCode_sizes[r->m_type];
336
13.7k
                MZ_CLEAR_ARR(total_syms);
337
13.7k
                TINFL_MEMSET(pLookUp, 0, sizeof(r->m_look_up[0]));
338
13.7k
                tinfl_clear_tree(r);
339
1.70M
                for (i = 0; i < r->m_table_sizes[r->m_type]; ++i)
340
1.69M
                    total_syms[pCode_size[i]]++;
341
13.7k
                used_syms = 0, total = 0;
342
13.7k
                next_code[0] = next_code[1] = 0;
343
220k
                for (i = 1; i <= 15; ++i)
344
206k
                {
345
206k
                    used_syms += total_syms[i];
346
206k
                    next_code[i + 1] = (total = ((total + total_syms[i]) << 1));
347
206k
                }
348
13.7k
                if ((65536 != total) && (used_syms > 1))
349
44
                {
350
44
                    TINFL_CR_RETURN_FOREVER(35, TINFL_STATUS_FAILED);
351
44
                }
352
1.70M
                for (tree_next = -1, sym_index = 0; sym_index < r->m_table_sizes[r->m_type]; ++sym_index)
353
1.68M
                {
354
1.68M
                    mz_uint rev_code = 0, l, cur_code, code_size = pCode_size[sym_index];
355
1.68M
                    if (!code_size)
356
602k
                        continue;
357
1.08M
                    cur_code = next_code[code_size]++;
358
9.45M
                    for (l = code_size; l > 0; l--, cur_code >>= 1)
359
8.37M
                        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
11.3k
                    if (0 == (tree_cur = pLookUp[rev_code & (TINFL_FAST_LOOKUP_SIZE - 1)]))
371
4.16k
                    {
372
4.16k
                        pLookUp[rev_code & (TINFL_FAST_LOOKUP_SIZE - 1)] = (mz_int16)tree_next;
373
4.16k
                        tree_cur = tree_next;
374
4.16k
                        tree_next -= 2;
375
4.16k
                    }
376
11.3k
                    rev_code >>= (TINFL_FAST_LOOKUP_BITS - 1);
377
20.2k
                    for (j = code_size; j > (TINFL_FAST_LOOKUP_BITS + 1); j--)
378
8.92k
                    {
379
8.92k
                        tree_cur -= ((rev_code >>= 1) & 1);
380
8.92k
                        if (!pTree[-tree_cur - 1])
381
3.24k
                        {
382
3.24k
                            pTree[-tree_cur - 1] = (mz_int16)tree_next;
383
3.24k
                            tree_cur = tree_next;
384
3.24k
                            tree_next -= 2;
385
3.24k
                        }
386
5.68k
                        else
387
5.68k
                            tree_cur = pTree[-tree_cur - 1];
388
8.92k
                    }
389
11.3k
                    tree_cur -= ((rev_code >>= 1) & 1);
390
11.3k
                    pTree[-tree_cur - 1] = (mz_int16)sym_index;
391
11.3k
                }
392
13.7k
                if (r->m_type == 2)
393
3.11k
                {
394
445k
                    for (counter = 0; counter < (r->m_table_sizes[0] + r->m_table_sizes[1]);)
395
442k
                    {
396
442k
                        mz_uint s;
397
442k
                        TINFL_HUFF_DECODE(16, dist, r->m_look_up[2], r->m_tree_2);
398
442k
                        if (dist < 16)
399
394k
                        {
400
394k
                            r->m_len_codes[counter++] = (mz_uint8)dist;
401
394k
                            continue;
402
394k
                        }
403
48.8k
                        if ((dist == 16) && (!counter))
404
2
                        {
405
2
                            TINFL_CR_RETURN_FOREVER(17, TINFL_STATUS_FAILED);
406
2
                        }
407
48.8k
                        num_extra = "\02\03\07"[dist - 16];
408
48.8k
                        TINFL_GET_BITS(18, s, num_extra);
409
48.8k
                        s += "\03\03\013"[dist - 16];
410
48.8k
                        TINFL_MEMSET(r->m_len_codes + counter, (dist == 16) ? r->m_len_codes[counter - 1] : 0, s);
411
48.8k
                        counter += s;
412
48.8k
                    }
413
3.10k
                    if ((r->m_table_sizes[0] + r->m_table_sizes[1]) != counter)
414
15
                    {
415
15
                        TINFL_CR_RETURN_FOREVER(21, TINFL_STATUS_FAILED);
416
15
                    }
417
3.09k
                    TINFL_MEMCPY(r->m_code_size_0, r->m_len_codes, r->m_table_sizes[0]);
418
3.09k
                    TINFL_MEMCPY(r->m_code_size_1, r->m_len_codes + r->m_table_sizes[0], r->m_table_sizes[1]);
419
3.09k
                }
420
13.7k
            }
421
5.28k
            for (;;)
422
958k
            {
423
958k
                mz_uint8 *pSrc;
424
958k
                for (;;)
425
612M
                {
426
612M
                    if (((pIn_buf_end - pIn_buf_cur) < 4) || ((pOut_buf_end - pOut_buf_cur) < 2))
427
43.1M
                    {
428
43.1M
                        TINFL_HUFF_DECODE(23, counter, r->m_look_up[0], r->m_tree_0);
429
43.1M
                        if (counter >= 256)
430
9.12k
                            break;
431
43.1M
                        while (pOut_buf_cur >= pOut_buf_end)
432
49
                        {
433
49
                            TINFL_CR_RETURN(24, TINFL_STATUS_HAS_MORE_OUTPUT);
434
49
                        }
435
43.1M
                        *pOut_buf_cur++ = (mz_uint8)counter;
436
43.1M
                    }
437
569M
                    else
438
569M
                    {
439
569M
                        int sym2;
440
569M
                        mz_uint code_len;
441
569M
#if TINFL_USE_64BIT_BITBUF
442
569M
                        if (num_bits < 30)
443
782k
                        {
444
782k
                            bit_buf |= (((tinfl_bit_buf_t)MZ_READ_LE32(pIn_buf_cur)) << num_bits);
445
782k
                            pIn_buf_cur += 4;
446
782k
                            num_bits += 32;
447
782k
                        }
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
569M
                        if ((sym2 = r->m_look_up[0][bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0)
457
569M
                            code_len = sym2 >> 9;
458
9.95k
                        else
459
9.95k
                        {
460
9.95k
                            code_len = TINFL_FAST_LOOKUP_BITS;
461
9.95k
                            do
462
18.8k
                            {
463
18.8k
                                sym2 = r->m_tree_0[~sym2 + ((bit_buf >> code_len++) & 1)];
464
18.8k
                            } while (sym2 < 0);
465
9.95k
                        }
466
569M
                        counter = sym2;
467
569M
                        bit_buf >>= code_len;
468
569M
                        num_bits -= code_len;
469
569M
                        if (counter & 256)
470
885k
                            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
568M
                        if ((sym2 = r->m_look_up[0][bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0)
481
568M
                            code_len = sym2 >> 9;
482
6.70k
                        else
483
6.70k
                        {
484
6.70k
                            code_len = TINFL_FAST_LOOKUP_BITS;
485
6.70k
                            do
486
13.8k
                            {
487
13.8k
                                sym2 = r->m_tree_0[~sym2 + ((bit_buf >> code_len++) & 1)];
488
13.8k
                            } while (sym2 < 0);
489
6.70k
                        }
490
568M
                        bit_buf >>= code_len;
491
568M
                        num_bits -= code_len;
492
493
568M
                        pOut_buf_cur[0] = (mz_uint8)counter;
494
568M
                        if (sym2 & 256)
495
64.3k
                        {
496
64.3k
                            pOut_buf_cur++;
497
64.3k
                            counter = sym2;
498
64.3k
                            break;
499
64.3k
                        }
500
568M
                        pOut_buf_cur[1] = (mz_uint8)sym2;
501
568M
                        pOut_buf_cur += 2;
502
568M
                    }
503
612M
                }
504
958k
                if ((counter &= 511) == 256)
505
5.12k
                    break;
506
507
953k
                num_extra = s_length_extra[counter - 257];
508
953k
                counter = s_length_base[counter - 257];
509
953k
                if (num_extra)
510
71.8k
                {
511
71.8k
                    mz_uint extra_bits;
512
71.8k
                    TINFL_GET_BITS(25, extra_bits, num_extra);
513
71.8k
                    counter += extra_bits;
514
71.8k
                }
515
516
953k
                TINFL_HUFF_DECODE(26, dist, r->m_look_up[1], r->m_tree_1);
517
953k
                num_extra = s_dist_extra[dist];
518
953k
                dist = s_dist_base[dist];
519
953k
                if (num_extra)
520
146k
                {
521
146k
                    mz_uint extra_bits;
522
146k
                    TINFL_GET_BITS(27, extra_bits, num_extra);
523
146k
                    dist += extra_bits;
524
146k
                }
525
526
953k
                dist_from_out_buf_start = pOut_buf_cur - pOut_buf_start;
527
953k
                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
14
                {
529
14
                    TINFL_CR_RETURN_FOREVER(37, TINFL_STATUS_FAILED);
530
14
                }
531
532
953k
                pSrc = pOut_buf_start + ((dist_from_out_buf_start - dist) & out_buf_size_mask);
533
534
953k
                if ((MZ_MAX(pOut_buf_cur, pSrc) + counter) > pOut_buf_end)
535
16
                {
536
788
                    while (counter--)
537
788
                    {
538
788
                        while (pOut_buf_cur >= pOut_buf_end)
539
16
                        {
540
16
                            TINFL_CR_RETURN(53, TINFL_STATUS_HAS_MORE_OUTPUT);
541
16
                        }
542
772
                        *pOut_buf_cur++ = pOut_buf_start[(dist_from_out_buf_start++ - dist) & out_buf_size_mask];
543
772
                    }
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
67.7M
                while(counter>2)
574
66.7M
                {
575
66.7M
                    pOut_buf_cur[0] = pSrc[0];
576
66.7M
                    pOut_buf_cur[1] = pSrc[1];
577
66.7M
                    pOut_buf_cur[2] = pSrc[2];
578
66.7M
                    pOut_buf_cur += 3;
579
66.7M
                    pSrc += 3;
580
66.7M
          counter -= 3;
581
66.7M
                }
582
953k
                if (counter > 0)
583
97.7k
                {
584
97.7k
                    pOut_buf_cur[0] = pSrc[0];
585
97.7k
                    if (counter > 1)
586
40.8k
                        pOut_buf_cur[1] = pSrc[1];
587
97.7k
                    pOut_buf_cur += counter;
588
97.7k
                }
589
953k
            }
590
5.28k
        }
591
8.48k
    } 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
2.04k
    TINFL_SKIP_BITS(32, num_bits & 7);
596
2.22k
    while ((pIn_buf_cur > pIn_buf_next) && (num_bits >= 8))
597
182
    {
598
182
        --pIn_buf_cur;
599
182
        num_bits -= 8;
600
182
    }
601
2.04k
    bit_buf &= ~(~(tinfl_bit_buf_t)0 << num_bits);
602
2.04k
    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
2.04k
    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.36k
    TINFL_CR_RETURN_FOREVER(34, TINFL_STATUS_DONE);
617
618
2.36k
    TINFL_CR_FINISH
619
620
2.36k
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.36k
    if ((status != TINFL_STATUS_NEEDS_MORE_INPUT) && (status != TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS))
625
2.25k
    {
626
2.60k
        while ((pIn_buf_cur > pIn_buf_next) && (num_bits >= 8))
627
355
        {
628
355
            --pIn_buf_cur;
629
355
            num_bits -= 8;
630
355
        }
631
2.25k
    }
632
2.36k
    r->m_num_bits = num_bits;
633
2.36k
    r->m_bit_buf = bit_buf & ~(~(tinfl_bit_buf_t)0 << num_bits);
634
2.36k
    r->m_dist = dist;
635
2.36k
    r->m_counter = counter;
636
2.36k
    r->m_num_extra = num_extra;
637
2.36k
    r->m_dist_from_out_buf_start = dist_from_out_buf_start;
638
2.36k
    *pIn_buf_size = pIn_buf_cur - pIn_buf_next;
639
2.36k
    *pOut_buf_size = pOut_buf_cur - pOut_buf_next;
640
2.36k
    if ((decomp_flags & (TINFL_FLAG_PARSE_ZLIB_HEADER | TINFL_FLAG_COMPUTE_ADLER32)) && (status >= 0))
641
2.11k
    {
642
2.11k
        const mz_uint8 *ptr = pOut_buf_next;
643
2.11k
        size_t buf_len = *pOut_buf_size;
644
2.11k
        mz_uint32 i, s1 = r->m_check_adler32 & 0xffff, s2 = r->m_check_adler32 >> 16;
645
2.11k
        size_t block_len = buf_len % 5552;
646
217k
        while (buf_len)
647
215k
        {
648
148M
            for (i = 0; i + 7 < block_len; i += 8, ptr += 8)
649
148M
            {
650
148M
                s1 += ptr[0], s2 += s1;
651
148M
                s1 += ptr[1], s2 += s1;
652
148M
                s1 += ptr[2], s2 += s1;
653
148M
                s1 += ptr[3], s2 += s1;
654
148M
                s1 += ptr[4], s2 += s1;
655
148M
                s1 += ptr[5], s2 += s1;
656
148M
                s1 += ptr[6], s2 += s1;
657
148M
                s1 += ptr[7], s2 += s1;
658
148M
            }
659
222k
            for (; i < block_len; ++i)
660
6.71k
                s1 += *ptr++, s2 += s1;
661
215k
            s1 %= 65521U, s2 %= 65521U;
662
215k
            buf_len -= block_len;
663
215k
            block_len = 5552;
664
215k
        }
665
2.11k
        r->m_check_adler32 = (s2 << 16) + s1;
666
2.11k
        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
2.11k
    }
669
2.36k
    return status;
670
2.36k
}
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*/