Coverage Report

Created: 2025-07-04 07:08

/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.36k
#define TINFL_MEMCPY(d, s, l) memcpy(d, s, l)
38
92.1k
#define TINFL_MEMSET(p, c, l) memset(p, c, l)
39
40
#define TINFL_CR_BEGIN  \
41
4.28k
    switch (r->m_state) \
42
4.28k
    {                   \
43
2.14k
        case 0:
44
#define TINFL_CR_RETURN(state_index, result) \
45
2.14k
    do                                       \
46
2.14k
    {                                        \
47
2.14k
        status = result;                     \
48
2.14k
        r->m_state = state_index;            \
49
2.14k
        goto common_exit;                    \
50
2.14k
        case state_index:;                   \
51
0
    }                                        \
52
2.14k
    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.14k
#define TINFL_CR_FINISH }
63
64
#define TINFL_GET_BYTE(state_index, c)                                                                                                                           \
65
31.8k
    do                                                                                                                                                           \
66
31.8k
    {                                                                                                                                                            \
67
31.8k
        while (pIn_buf_cur >= pIn_buf_end)                                                                                                                       \
68
31.8k
        {                                                                                                                                                        \
69
100
            TINFL_CR_RETURN(state_index, (decomp_flags & TINFL_FLAG_HAS_MORE_INPUT) ? TINFL_STATUS_NEEDS_MORE_INPUT : TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS); \
70
100
        }                                                                                                                                                        \
71
31.8k
        c = *pIn_buf_cur++;                                                                                                                                      \
72
31.7k
    }                                                                                                                                                            \
73
34.8k
    MZ_MACRO_END
74
75
#define TINFL_NEED_BITS(state_index, n)                \
76
24.1k
    do                                                 \
77
24.3k
    {                                                  \
78
24.3k
        mz_uint c;                                     \
79
24.3k
        TINFL_GET_BYTE(state_index, c);                \
80
24.3k
        bit_buf |= (((tinfl_bit_buf_t)c) << num_bits); \
81
24.3k
        num_bits += 8;                                 \
82
24.3k
    } while (num_bits < (mz_uint)(n))
83
#define TINFL_SKIP_BITS(state_index, n)      \
84
4.25k
    do                                       \
85
4.25k
    {                                        \
86
4.25k
        if (num_bits < (mz_uint)(n))         \
87
4.25k
        {                                    \
88
0
            TINFL_NEED_BITS(state_index, n); \
89
0
        }                                    \
90
4.25k
        bit_buf >>= (n);                     \
91
4.25k
        num_bits -= (n);                     \
92
4.25k
    }                                        \
93
4.55k
    MZ_MACRO_END
94
#define TINFL_GET_BITS(state_index, b, n)    \
95
364k
    do                                       \
96
364k
    {                                        \
97
364k
        if (num_bits < (mz_uint)(n))         \
98
364k
        {                                    \
99
24.1k
            TINFL_NEED_BITS(state_index, n); \
100
24.1k
        }                                    \
101
364k
        b = bit_buf & ((1 << (n)) - 1);      \
102
364k
        bit_buf >>= (n);                     \
103
364k
        num_bits -= (n);                     \
104
364k
    }                                        \
105
364k
    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.25k
    do                                                                         \
113
3.95k
    {                                                                          \
114
3.95k
        temp = pLookUp[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)];                \
115
3.95k
        if (temp >= 0)                                                         \
116
3.95k
        {                                                                      \
117
3.77k
            code_len = temp >> 9;                                              \
118
3.77k
            if ((code_len) && (num_bits >= code_len))                          \
119
3.77k
                break;                                                         \
120
3.77k
        }                                                                      \
121
3.95k
        else if (num_bits > TINFL_FAST_LOOKUP_BITS)                            \
122
177
        {                                                                      \
123
124
            code_len = TINFL_FAST_LOOKUP_BITS;                                 \
124
124
            do                                                                 \
125
229
            {                                                                  \
126
229
                temp = pTree[~temp + ((bit_buf >> code_len++) & 1)];           \
127
229
            } while ((temp < 0) && (num_bits >= (code_len + 1)));              \
128
124
            if (temp >= 0)                                                     \
129
124
                break;                                                         \
130
124
        }                                                                      \
131
3.95k
        TINFL_GET_BYTE(state_index, c);                                        \
132
961
        bit_buf |= (((tinfl_bit_buf_t)c) << num_bits);                         \
133
888
        num_bits += 8;                                                         \
134
888
    } 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
50.2M
    do                                                                                                                              \
144
50.2M
    {                                                                                                                               \
145
50.2M
        int temp;                                                                                                                   \
146
50.2M
        mz_uint code_len, c;                                                                                                        \
147
50.2M
        if (num_bits < 15)                                                                                                          \
148
50.2M
        {                                                                                                                           \
149
83.2k
            if ((pIn_buf_end - pIn_buf_cur) < 2)                                                                                    \
150
83.2k
            {                                                                                                                       \
151
3.25k
                TINFL_HUFF_BITBUF_FILL(state_index, pLookUp, pTree);                                                                \
152
3.17k
            }                                                                                                                       \
153
83.2k
            else                                                                                                                    \
154
83.2k
            {                                                                                                                       \
155
80.0k
                bit_buf |= (((tinfl_bit_buf_t)pIn_buf_cur[0]) << num_bits) | (((tinfl_bit_buf_t)pIn_buf_cur[1]) << (num_bits + 8)); \
156
80.0k
                pIn_buf_cur += 2;                                                                                                   \
157
80.0k
                num_bits += 16;                                                                                                     \
158
80.0k
            }                                                                                                                       \
159
83.2k
        }                                                                                                                           \
160
50.2M
        if ((temp = pLookUp[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0)                                                          \
161
50.2M
            code_len = temp >> 9, temp &= 511;                                                                                      \
162
50.2M
        else                                                                                                                        \
163
50.2M
        {                                                                                                                           \
164
20.0k
            code_len = TINFL_FAST_LOOKUP_BITS;                                                                                      \
165
20.0k
            do                                                                                                                      \
166
96.0k
            {                                                                                                                       \
167
96.0k
                temp = pTree[~temp + ((bit_buf >> code_len++) & 1)];                                                                \
168
96.0k
            } while (temp < 0);                                                                                                     \
169
20.0k
        }                                                                                                                           \
170
50.2M
        sym = temp;                                                                                                                 \
171
50.2M
        bit_buf >>= code_len;                                                                                                       \
172
50.2M
        num_bits -= code_len;                                                                                                       \
173
50.2M
    }                                                                                                                               \
174
50.2M
    MZ_MACRO_END
175
176
static void tinfl_clear_tree(tinfl_decompressor *r)
177
13.5k
{
178
13.5k
    if (r->m_type == 0)
179
5.25k
        MZ_CLEAR_ARR(r->m_tree_0);
180
8.33k
    else if (r->m_type == 1)
181
5.25k
        MZ_CLEAR_ARR(r->m_tree_1);
182
3.07k
    else
183
3.07k
        MZ_CLEAR_ARR(r->m_tree_2);
184
13.5k
}
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.14k
{
188
2.14k
    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.14k
    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.14k
    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.14k
    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.14k
    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.14k
    static const mz_uint16 s_min_table_sizes[3] = { 257, 1, 4 };
194
195
2.14k
    mz_int16 *pTrees[3];
196
2.14k
    mz_uint8 *pCode_sizes[3];
197
198
2.14k
    tinfl_status status = TINFL_STATUS_FAILED;
199
2.14k
    mz_uint32 num_bits, dist, counter, num_extra;
200
2.14k
    tinfl_bit_buf_t bit_buf;
201
2.14k
    const mz_uint8 *pIn_buf_cur = pIn_buf_next, *const pIn_buf_end = pIn_buf_next + *pIn_buf_size;
202
2.14k
    mz_uint8 *pOut_buf_cur = pOut_buf_next, *const pOut_buf_end = pOut_buf_next ? pOut_buf_next + *pOut_buf_size : NULL;
203
2.14k
    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.14k
    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.14k
    pTrees[0] = r->m_tree_0;
213
2.14k
    pTrees[1] = r->m_tree_1;
214
2.14k
    pTrees[2] = r->m_tree_2;
215
2.14k
    pCode_sizes[0] = r->m_code_size_0;
216
2.14k
    pCode_sizes[1] = r->m_code_size_1;
217
2.14k
    pCode_sizes[2] = r->m_code_size_2;
218
219
2.14k
    num_bits = r->m_num_bits;
220
2.14k
    bit_buf = r->m_bit_buf;
221
2.14k
    dist = r->m_dist;
222
2.14k
    counter = r->m_counter;
223
2.14k
    num_extra = r->m_num_extra;
224
2.14k
    dist_from_out_buf_start = r->m_dist_from_out_buf_start;
225
2.14k
    TINFL_CR_BEGIN
226
227
2.14k
    bit_buf = num_bits = dist = counter = num_extra = r->m_zhdr0 = r->m_zhdr1 = 0;
228
2.14k
    r->m_z_adler32 = r->m_check_adler32 = 1;
229
2.14k
    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.14k
    do
243
7.72k
    {
244
7.72k
        TINFL_GET_BITS(3, r->m_final, 3);
245
7.72k
        r->m_type = r->m_final >> 1;
246
7.72k
        if (r->m_type == 0)
247
2.41k
        {
248
2.41k
            TINFL_SKIP_BITS(5, num_bits & 7);
249
12.0k
            for (counter = 0; counter < 4; ++counter)
250
9.63k
            {
251
9.63k
                if (num_bits)
252
3.10k
                    TINFL_GET_BITS(6, r->m_raw_header[counter], 8);
253
6.53k
                else
254
6.53k
                    TINFL_GET_BYTE(7, r->m_raw_header[counter]);
255
9.63k
            }
256
2.40k
            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
47
            {
258
47
                TINFL_CR_RETURN_FOREVER(39, TINFL_STATUS_FAILED);
259
47
            }
260
2.70k
            while ((counter) && (num_bits))
261
350
            {
262
350
                TINFL_GET_BITS(51, dist, 8);
263
350
                while (pOut_buf_cur >= pOut_buf_end)
264
1
                {
265
1
                    TINFL_CR_RETURN(52, TINFL_STATUS_HAS_MORE_OUTPUT);
266
1
                }
267
349
                *pOut_buf_cur++ = (mz_uint8)dist;
268
349
                counter--;
269
349
            }
270
3.65k
            while (counter)
271
1.31k
            {
272
1.31k
                size_t n;
273
1.31k
                while (pOut_buf_cur >= pOut_buf_end)
274
5
                {
275
5
                    TINFL_CR_RETURN(9, TINFL_STATUS_HAS_MORE_OUTPUT);
276
5
                }
277
1.31k
                while (pIn_buf_cur >= pIn_buf_end)
278
15
                {
279
15
                    TINFL_CR_RETURN(38, (decomp_flags & TINFL_FLAG_HAS_MORE_INPUT) ? TINFL_STATUS_NEEDS_MORE_INPUT : TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS);
280
15
                }
281
1.29k
                n = MZ_MIN(MZ_MIN((size_t)(pOut_buf_end - pOut_buf_cur), (size_t)(pIn_buf_end - pIn_buf_cur)), counter);
282
1.29k
                TINFL_MEMCPY(pOut_buf_cur, pIn_buf_cur, n);
283
1.29k
                pIn_buf_cur += n;
284
1.29k
                pOut_buf_cur += n;
285
1.29k
                counter -= (mz_uint)n;
286
1.29k
            }
287
2.35k
        }
288
5.31k
        else if (r->m_type == 3)
289
8
        {
290
8
            TINFL_CR_RETURN_FOREVER(10, TINFL_STATUS_FAILED);
291
8
        }
292
5.30k
        else
293
5.30k
        {
294
5.30k
            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.4k
                    *p++ = 7;
307
20.0k
                for (; i <= 287; ++i)
308
17.8k
                    *p++ = 8;
309
2.22k
            }
310
3.07k
            else
311
3.07k
            {
312
12.3k
                for (counter = 0; counter < 3; counter++)
313
9.23k
                {
314
9.23k
                    TINFL_GET_BITS(11, r->m_table_sizes[counter], "\05\05\04"[counter]);
315
9.23k
                    r->m_table_sizes[counter] += s_min_table_sizes[counter];
316
9.23k
                }
317
3.07k
                MZ_CLEAR_ARR(r->m_code_size_2);
318
47.2k
                for (counter = 0; counter < r->m_table_sizes[2]; counter++)
319
44.2k
                {
320
44.2k
                    mz_uint s;
321
44.2k
                    TINFL_GET_BITS(14, s, 3);
322
44.2k
                    r->m_code_size_2[s_length_dezigzag[counter]] = (mz_uint8)s;
323
44.2k
                }
324
3.07k
                r->m_table_sizes[2] = 19;
325
3.07k
            }
326
18.8k
            for (; (int)r->m_type >= 0; r->m_type--)
327
13.5k
            {
328
13.5k
                int tree_next, tree_cur;
329
13.5k
                mz_int16 *pLookUp;
330
13.5k
                mz_int16 *pTree;
331
13.5k
                mz_uint8 *pCode_size;
332
13.5k
                mz_uint i, j, used_syms, total, sym_index, next_code[17], total_syms[16];
333
13.5k
                pLookUp = r->m_look_up[r->m_type];
334
13.5k
                pTree = pTrees[r->m_type];
335
13.5k
                pCode_size = pCode_sizes[r->m_type];
336
13.5k
                MZ_CLEAR_ARR(total_syms);
337
13.5k
                TINFL_MEMSET(pLookUp, 0, sizeof(r->m_look_up[0]));
338
13.5k
                tinfl_clear_tree(r);
339
1.69M
                for (i = 0; i < r->m_table_sizes[r->m_type]; ++i)
340
1.67M
                    total_syms[pCode_size[i]]++;
341
13.5k
                used_syms = 0, total = 0;
342
13.5k
                next_code[0] = next_code[1] = 0;
343
217k
                for (i = 1; i <= 15; ++i)
344
203k
                {
345
203k
                    used_syms += total_syms[i];
346
203k
                    next_code[i + 1] = (total = ((total + total_syms[i]) << 1));
347
203k
                }
348
13.5k
                if ((65536 != total) && (used_syms > 1))
349
28
                {
350
28
                    TINFL_CR_RETURN_FOREVER(35, TINFL_STATUS_FAILED);
351
28
                }
352
1.68M
                for (tree_next = -1, sym_index = 0; sym_index < r->m_table_sizes[r->m_type]; ++sym_index)
353
1.67M
                {
354
1.67M
                    mz_uint rev_code = 0, l, cur_code, code_size = pCode_size[sym_index];
355
1.67M
                    if (!code_size)
356
610k
                        continue;
357
1.06M
                    cur_code = next_code[code_size]++;
358
9.24M
                    for (l = code_size; l > 0; l--, cur_code >>= 1)
359
8.18M
                        rev_code = (rev_code << 1) | (cur_code & 1);
360
1.06M
                    if (code_size <= TINFL_FAST_LOOKUP_BITS)
361
1.05M
                    {
362
1.05M
                        mz_int16 k = (mz_int16)((code_size << 9) | sym_index);
363
13.5M
                        while (rev_code < TINFL_FAST_LOOKUP_SIZE)
364
12.4M
                        {
365
12.4M
                            pLookUp[rev_code] = k;
366
12.4M
                            rev_code += (1 << code_size);
367
12.4M
                        }
368
1.05M
                        continue;
369
1.05M
                    }
370
11.5k
                    if (0 == (tree_cur = pLookUp[rev_code & (TINFL_FAST_LOOKUP_SIZE - 1)]))
371
4.17k
                    {
372
4.17k
                        pLookUp[rev_code & (TINFL_FAST_LOOKUP_SIZE - 1)] = (mz_int16)tree_next;
373
4.17k
                        tree_cur = tree_next;
374
4.17k
                        tree_next -= 2;
375
4.17k
                    }
376
11.5k
                    rev_code >>= (TINFL_FAST_LOOKUP_BITS - 1);
377
20.9k
                    for (j = code_size; j > (TINFL_FAST_LOOKUP_BITS + 1); j--)
378
9.45k
                    {
379
9.45k
                        tree_cur -= ((rev_code >>= 1) & 1);
380
9.45k
                        if (!pTree[-tree_cur - 1])
381
3.45k
                        {
382
3.45k
                            pTree[-tree_cur - 1] = (mz_int16)tree_next;
383
3.45k
                            tree_cur = tree_next;
384
3.45k
                            tree_next -= 2;
385
3.45k
                        }
386
5.99k
                        else
387
5.99k
                            tree_cur = pTree[-tree_cur - 1];
388
9.45k
                    }
389
11.5k
                    tree_cur -= ((rev_code >>= 1) & 1);
390
11.5k
                    pTree[-tree_cur - 1] = (mz_int16)sym_index;
391
11.5k
                }
392
13.5k
                if (r->m_type == 2)
393
3.05k
                {
394
415k
                    for (counter = 0; counter < (r->m_table_sizes[0] + r->m_table_sizes[1]);)
395
412k
                    {
396
412k
                        mz_uint s;
397
412k
                        TINFL_HUFF_DECODE(16, dist, r->m_look_up[2], r->m_tree_2);
398
412k
                        if (dist < 16)
399
366k
                        {
400
366k
                            r->m_len_codes[counter++] = (mz_uint8)dist;
401
366k
                            continue;
402
366k
                        }
403
46.0k
                        if ((dist == 16) && (!counter))
404
2
                        {
405
2
                            TINFL_CR_RETURN_FOREVER(17, TINFL_STATUS_FAILED);
406
2
                        }
407
46.0k
                        num_extra = "\02\03\07"[dist - 16];
408
46.0k
                        TINFL_GET_BITS(18, s, num_extra);
409
46.0k
                        s += "\03\03\013"[dist - 16];
410
46.0k
                        TINFL_MEMSET(r->m_len_codes + counter, (dist == 16) ? r->m_len_codes[counter - 1] : 0, s);
411
46.0k
                        counter += s;
412
46.0k
                    }
413
3.04k
                    if ((r->m_table_sizes[0] + r->m_table_sizes[1]) != counter)
414
10
                    {
415
10
                        TINFL_CR_RETURN_FOREVER(21, TINFL_STATUS_FAILED);
416
10
                    }
417
3.03k
                    TINFL_MEMCPY(r->m_code_size_0, r->m_len_codes, r->m_table_sizes[0]);
418
3.03k
                    TINFL_MEMCPY(r->m_code_size_1, r->m_len_codes + r->m_table_sizes[0], r->m_table_sizes[1]);
419
3.03k
                }
420
13.5k
            }
421
5.24k
            for (;;)
422
606k
            {
423
606k
                mz_uint8 *pSrc;
424
606k
                for (;;)
425
409M
                {
426
409M
                    if (((pIn_buf_end - pIn_buf_cur) < 4) || ((pOut_buf_end - pOut_buf_cur) < 2))
427
49.2M
                    {
428
49.2M
                        TINFL_HUFF_DECODE(23, counter, r->m_look_up[0], r->m_tree_0);
429
49.2M
                        if (counter >= 256)
430
5.61k
                            break;
431
49.2M
                        while (pOut_buf_cur >= pOut_buf_end)
432
53
                        {
433
53
                            TINFL_CR_RETURN(24, TINFL_STATUS_HAS_MORE_OUTPUT);
434
53
                        }
435
49.2M
                        *pOut_buf_cur++ = (mz_uint8)counter;
436
49.2M
                    }
437
359M
                    else
438
359M
                    {
439
359M
                        int sym2;
440
359M
                        mz_uint code_len;
441
359M
#if TINFL_USE_64BIT_BITBUF
442
359M
                        if (num_bits < 30)
443
798k
                        {
444
798k
                            bit_buf |= (((tinfl_bit_buf_t)MZ_READ_LE32(pIn_buf_cur)) << num_bits);
445
798k
                            pIn_buf_cur += 4;
446
798k
                            num_bits += 32;
447
798k
                        }
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
359M
                        if ((sym2 = r->m_look_up[0][bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0)
457
359M
                            code_len = sym2 >> 9;
458
10.2k
                        else
459
10.2k
                        {
460
10.2k
                            code_len = TINFL_FAST_LOOKUP_BITS;
461
10.2k
                            do
462
20.2k
                            {
463
20.2k
                                sym2 = r->m_tree_0[~sym2 + ((bit_buf >> code_len++) & 1)];
464
20.2k
                            } while (sym2 < 0);
465
10.2k
                        }
466
359M
                        counter = sym2;
467
359M
                        bit_buf >>= code_len;
468
359M
                        num_bits -= code_len;
469
359M
                        if (counter & 256)
470
533k
                            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
359M
                        if ((sym2 = r->m_look_up[0][bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0)
481
359M
                            code_len = sym2 >> 9;
482
6.93k
                        else
483
6.93k
                        {
484
6.93k
                            code_len = TINFL_FAST_LOOKUP_BITS;
485
6.93k
                            do
486
14.4k
                            {
487
14.4k
                                sym2 = r->m_tree_0[~sym2 + ((bit_buf >> code_len++) & 1)];
488
14.4k
                            } while (sym2 < 0);
489
6.93k
                        }
490
359M
                        bit_buf >>= code_len;
491
359M
                        num_bits -= code_len;
492
493
359M
                        pOut_buf_cur[0] = (mz_uint8)counter;
494
359M
                        if (sym2 & 256)
495
66.6k
                        {
496
66.6k
                            pOut_buf_cur++;
497
66.6k
                            counter = sym2;
498
66.6k
                            break;
499
66.6k
                        }
500
359M
                        pOut_buf_cur[1] = (mz_uint8)sym2;
501
359M
                        pOut_buf_cur += 2;
502
359M
                    }
503
409M
                }
504
606k
                if ((counter &= 511) == 256)
505
5.09k
                    break;
506
507
601k
                num_extra = s_length_extra[counter - 257];
508
601k
                counter = s_length_base[counter - 257];
509
601k
                if (num_extra)
510
84.6k
                {
511
84.6k
                    mz_uint extra_bits;
512
84.6k
                    TINFL_GET_BITS(25, extra_bits, num_extra);
513
84.6k
                    counter += extra_bits;
514
84.6k
                }
515
516
600k
                TINFL_HUFF_DECODE(26, dist, r->m_look_up[1], r->m_tree_1);
517
600k
                num_extra = s_dist_extra[dist];
518
600k
                dist = s_dist_base[dist];
519
600k
                if (num_extra)
520
168k
                {
521
168k
                    mz_uint extra_bits;
522
168k
                    TINFL_GET_BITS(27, extra_bits, num_extra);
523
168k
                    dist += extra_bits;
524
168k
                }
525
526
600k
                dist_from_out_buf_start = pOut_buf_cur - pOut_buf_start;
527
600k
                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
12
                {
529
12
                    TINFL_CR_RETURN_FOREVER(37, TINFL_STATUS_FAILED);
530
12
                }
531
532
600k
                pSrc = pOut_buf_start + ((dist_from_out_buf_start - dist) & out_buf_size_mask);
533
534
600k
                if ((MZ_MAX(pOut_buf_cur, pSrc) + counter) > pOut_buf_end)
535
15
                {
536
622
                    while (counter--)
537
622
                    {
538
622
                        while (pOut_buf_cur >= pOut_buf_end)
539
15
                        {
540
15
                            TINFL_CR_RETURN(53, TINFL_STATUS_HAS_MORE_OUTPUT);
541
15
                        }
542
607
                        *pOut_buf_cur++ = pOut_buf_start[(dist_from_out_buf_start++ - dist) & out_buf_size_mask];
543
607
                    }
544
0
                    continue;
545
15
                }
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
32.6M
                while(counter>2)
574
32.0M
                {
575
32.0M
                    pOut_buf_cur[0] = pSrc[0];
576
32.0M
                    pOut_buf_cur[1] = pSrc[1];
577
32.0M
                    pOut_buf_cur[2] = pSrc[2];
578
32.0M
                    pOut_buf_cur += 3;
579
32.0M
                    pSrc += 3;
580
32.0M
          counter -= 3;
581
32.0M
                }
582
600k
                if (counter > 0)
583
110k
                {
584
110k
                    pOut_buf_cur[0] = pSrc[0];
585
110k
                    if (counter > 1)
586
44.4k
                        pOut_buf_cur[1] = pSrc[1];
587
110k
                    pOut_buf_cur += counter;
588
110k
                }
589
600k
            }
590
5.24k
        }
591
7.72k
    } 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.84k
    TINFL_SKIP_BITS(32, num_bits & 7);
596
2.03k
    while ((pIn_buf_cur > pIn_buf_next) && (num_bits >= 8))
597
186
    {
598
186
        --pIn_buf_cur;
599
186
        num_bits -= 8;
600
186
    }
601
1.84k
    bit_buf &= ~(~(tinfl_bit_buf_t)0 << num_bits);
602
1.84k
    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.84k
    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.14k
    TINFL_CR_RETURN_FOREVER(34, TINFL_STATUS_DONE);
617
618
2.14k
    TINFL_CR_FINISH
619
620
2.14k
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.14k
    if ((status != TINFL_STATUS_NEEDS_MORE_INPUT) && (status != TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS))
625
2.02k
    {
626
2.36k
        while ((pIn_buf_cur > pIn_buf_next) && (num_bits >= 8))
627
343
        {
628
343
            --pIn_buf_cur;
629
343
            num_bits -= 8;
630
343
        }
631
2.02k
    }
632
2.14k
    r->m_num_bits = num_bits;
633
2.14k
    r->m_bit_buf = bit_buf & ~(~(tinfl_bit_buf_t)0 << num_bits);
634
2.14k
    r->m_dist = dist;
635
2.14k
    r->m_counter = counter;
636
2.14k
    r->m_num_extra = num_extra;
637
2.14k
    r->m_dist_from_out_buf_start = dist_from_out_buf_start;
638
2.14k
    *pIn_buf_size = pIn_buf_cur - pIn_buf_next;
639
2.14k
    *pOut_buf_size = pOut_buf_cur - pOut_buf_next;
640
2.14k
    if ((decomp_flags & (TINFL_FLAG_PARSE_ZLIB_HEADER | TINFL_FLAG_COMPUTE_ADLER32)) && (status >= 0))
641
1.91k
    {
642
1.91k
        const mz_uint8 *ptr = pOut_buf_next;
643
1.91k
        size_t buf_len = *pOut_buf_size;
644
1.91k
        mz_uint32 i, s1 = r->m_check_adler32 & 0xffff, s2 = r->m_check_adler32 >> 16;
645
1.91k
        size_t block_len = buf_len % 5552;
646
142k
        while (buf_len)
647
140k
        {
648
96.9M
            for (i = 0; i + 7 < block_len; i += 8, ptr += 8)
649
96.7M
            {
650
96.7M
                s1 += ptr[0], s2 += s1;
651
96.7M
                s1 += ptr[1], s2 += s1;
652
96.7M
                s1 += ptr[2], s2 += s1;
653
96.7M
                s1 += ptr[3], s2 += s1;
654
96.7M
                s1 += ptr[4], s2 += s1;
655
96.7M
                s1 += ptr[5], s2 += s1;
656
96.7M
                s1 += ptr[6], s2 += s1;
657
96.7M
                s1 += ptr[7], s2 += s1;
658
96.7M
            }
659
146k
            for (; i < block_len; ++i)
660
5.92k
                s1 += *ptr++, s2 += s1;
661
140k
            s1 %= 65521U, s2 %= 65521U;
662
140k
            buf_len -= block_len;
663
140k
            block_len = 5552;
664
140k
        }
665
1.91k
        r->m_check_adler32 = (s2 << 16) + s1;
666
1.91k
        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.91k
    }
669
2.14k
    return status;
670
2.14k
}
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*/