Coverage Report

Created: 2023-03-10 07:33

/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
8.48k
#define TINFL_MEMCPY(d, s, l) memcpy(d, s, l)
38
116k
#define TINFL_MEMSET(p, c, l) memset(p, c, l)
39
40
#define TINFL_CR_BEGIN  \
41
5.50k
    switch (r->m_state) \
42
5.50k
    {                   \
43
2.75k
        case 0:
44
#define TINFL_CR_RETURN(state_index, result) \
45
2.75k
    do                                       \
46
2.75k
    {                                        \
47
2.75k
        status = result;                     \
48
2.75k
        r->m_state = state_index;            \
49
2.75k
        goto common_exit;                    \
50
2.75k
        case state_index:;                   \
51
0
    }                                        \
52
2.75k
    MZ_MACRO_END
53
#define TINFL_CR_RETURN_FOREVER(state_index, result) \
54
2.56k
    do                                               \
55
2.56k
    {                                                \
56
2.56k
        for (;;)                                     \
57
2.56k
        {                                            \
58
2.56k
            TINFL_CR_RETURN(state_index, result);    \
59
2.56k
        }                                            \
60
2.56k
    }                                                \
61
2.56k
    MZ_MACRO_END
62
2.75k
#define TINFL_CR_FINISH }
63
64
#define TINFL_GET_BYTE(state_index, c)                                                                                                                           \
65
36.5k
    do                                                                                                                                                           \
66
36.5k
    {                                                                                                                                                            \
67
36.5k
        while (pIn_buf_cur >= pIn_buf_end)                                                                                                                       \
68
36.5k
        {                                                                                                                                                        \
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
36.5k
        c = *pIn_buf_cur++;                                                                                                                                      \
72
36.4k
    }                                                                                                                                                            \
73
40.5k
    MZ_MACRO_END
74
75
#define TINFL_NEED_BITS(state_index, n)                \
76
29.9k
    do                                                 \
77
29.9k
    {                                                  \
78
29.9k
        mz_uint c;                                     \
79
29.9k
        TINFL_GET_BYTE(state_index, c);                \
80
29.9k
        bit_buf |= (((tinfl_bit_buf_t)c) << num_bits); \
81
29.9k
        num_bits += 8;                                 \
82
29.9k
    } while (num_bits < (mz_uint)(n))
83
#define TINFL_SKIP_BITS(state_index, n)      \
84
4.43k
    do                                       \
85
4.43k
    {                                        \
86
4.43k
        if (num_bits < (mz_uint)(n))         \
87
4.43k
        {                                    \
88
0
            TINFL_NEED_BITS(state_index, n); \
89
0
        }                                    \
90
4.43k
        bit_buf >>= (n);                     \
91
4.43k
        num_bits -= (n);                     \
92
4.43k
    }                                        \
93
4.75k
    MZ_MACRO_END
94
#define TINFL_GET_BITS(state_index, b, n)    \
95
412k
    do                                       \
96
412k
    {                                        \
97
412k
        if (num_bits < (mz_uint)(n))         \
98
412k
        {                                    \
99
29.9k
            TINFL_NEED_BITS(state_index, n); \
100
29.9k
        }                                    \
101
412k
        b = bit_buf & ((1 << (n)) - 1);      \
102
412k
        bit_buf >>= (n);                     \
103
412k
        num_bits -= (n);                     \
104
412k
    }                                        \
105
412k
    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
4.23k
    do                                                                         \
113
5.22k
    {                                                                          \
114
5.22k
        temp = pLookUp[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)];                \
115
5.22k
        if (temp >= 0)                                                         \
116
5.22k
        {                                                                      \
117
5.03k
            code_len = temp >> 9;                                              \
118
5.03k
            if ((code_len) && (num_bits >= code_len))                          \
119
5.03k
                break;                                                         \
120
5.03k
        }                                                                      \
121
5.22k
        else if (num_bits > TINFL_FAST_LOOKUP_BITS)                            \
122
182
        {                                                                      \
123
137
            code_len = TINFL_FAST_LOOKUP_BITS;                                 \
124
137
            do                                                                 \
125
271
            {                                                                  \
126
271
                temp = pTree[~temp + ((bit_buf >> code_len++) & 1)];           \
127
271
            } while ((temp < 0) && (num_bits >= (code_len + 1)));              \
128
137
            if (temp >= 0)                                                     \
129
137
                break;                                                         \
130
137
        }                                                                      \
131
5.22k
        TINFL_GET_BYTE(state_index, c);                                        \
132
1.27k
        bit_buf |= (((tinfl_bit_buf_t)c) << num_bits);                         \
133
1.20k
        num_bits += 8;                                                         \
134
1.20k
    } 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.3M
    do                                                                                                                              \
144
50.3M
    {                                                                                                                               \
145
50.3M
        int temp;                                                                                                                   \
146
50.3M
        mz_uint code_len, c;                                                                                                        \
147
50.3M
        if (num_bits < 15)                                                                                                          \
148
50.3M
        {                                                                                                                           \
149
107k
            if ((pIn_buf_end - pIn_buf_cur) < 2)                                                                                    \
150
107k
            {                                                                                                                       \
151
4.23k
                TINFL_HUFF_BITBUF_FILL(state_index, pLookUp, pTree);                                                                \
152
4.16k
            }                                                                                                                       \
153
107k
            else                                                                                                                    \
154
107k
            {                                                                                                                       \
155
103k
                bit_buf |= (((tinfl_bit_buf_t)pIn_buf_cur[0]) << num_bits) | (((tinfl_bit_buf_t)pIn_buf_cur[1]) << (num_bits + 8)); \
156
103k
                pIn_buf_cur += 2;                                                                                                   \
157
103k
                num_bits += 16;                                                                                                     \
158
103k
            }                                                                                                                       \
159
107k
        }                                                                                                                           \
160
50.3M
        if ((temp = pLookUp[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0)                                                          \
161
50.3M
            code_len = temp >> 9, temp &= 511;                                                                                      \
162
50.3M
        else                                                                                                                        \
163
50.3M
        {                                                                                                                           \
164
2.51k
            code_len = TINFL_FAST_LOOKUP_BITS;                                                                                      \
165
2.51k
            do                                                                                                                      \
166
8.64k
            {                                                                                                                       \
167
8.64k
                temp = pTree[~temp + ((bit_buf >> code_len++) & 1)];                                                                \
168
8.64k
            } while (temp < 0);                                                                                                     \
169
2.51k
        }                                                                                                                           \
170
50.3M
        sym = temp;                                                                                                                 \
171
50.3M
        bit_buf >>= code_len;                                                                                                       \
172
50.3M
        num_bits -= code_len;                                                                                                       \
173
50.3M
    }                                                                                                                               \
174
50.3M
    MZ_MACRO_END
175
176
static void tinfl_clear_tree(tinfl_decompressor *r)
177
15.8k
{
178
15.8k
    if (r->m_type == 0)
179
6.00k
        MZ_CLEAR_ARR(r->m_tree_0);
180
9.81k
    else if (r->m_type == 1)
181
6.00k
        MZ_CLEAR_ARR(r->m_tree_1);
182
3.80k
    else
183
3.80k
        MZ_CLEAR_ARR(r->m_tree_2);
184
15.8k
}
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.75k
{
188
2.75k
    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.75k
    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.75k
    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.75k
    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.75k
    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.75k
    static const mz_uint16 s_min_table_sizes[3] = { 257, 1, 4 };
194
195
2.75k
    mz_int16 *pTrees[3];
196
2.75k
    mz_uint8 *pCode_sizes[3];
197
198
2.75k
    tinfl_status status = TINFL_STATUS_FAILED;
199
2.75k
    mz_uint32 num_bits, dist, counter, num_extra;
200
2.75k
    tinfl_bit_buf_t bit_buf;
201
2.75k
    const mz_uint8 *pIn_buf_cur = pIn_buf_next, *const pIn_buf_end = pIn_buf_next + *pIn_buf_size;
202
2.75k
    mz_uint8 *pOut_buf_cur = pOut_buf_next, *const pOut_buf_end = pOut_buf_next ? pOut_buf_next + *pOut_buf_size : NULL;
203
2.75k
    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.75k
    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.75k
    pTrees[0] = r->m_tree_0;
213
2.75k
    pTrees[1] = r->m_tree_1;
214
2.75k
    pTrees[2] = r->m_tree_2;
215
2.75k
    pCode_sizes[0] = r->m_code_size_0;
216
2.75k
    pCode_sizes[1] = r->m_code_size_1;
217
2.75k
    pCode_sizes[2] = r->m_code_size_2;
218
219
2.75k
    num_bits = r->m_num_bits;
220
2.75k
    bit_buf = r->m_bit_buf;
221
2.75k
    dist = r->m_dist;
222
2.75k
    counter = r->m_counter;
223
2.75k
    num_extra = r->m_num_extra;
224
2.75k
    dist_from_out_buf_start = r->m_dist_from_out_buf_start;
225
2.75k
    TINFL_CR_BEGIN
226
227
2.75k
    bit_buf = num_bits = dist = counter = num_extra = r->m_zhdr0 = r->m_zhdr1 = 0;
228
2.75k
    r->m_z_adler32 = r->m_check_adler32 = 1;
229
2.75k
    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.75k
    do
243
8.08k
    {
244
8.08k
        TINFL_GET_BITS(3, r->m_final, 3);
245
8.08k
        r->m_type = r->m_final >> 1;
246
8.08k
        if (r->m_type == 0)
247
2.00k
        {
248
2.00k
            TINFL_SKIP_BITS(5, num_bits & 7);
249
10.0k
            for (counter = 0; counter < 4; ++counter)
250
8.00k
            {
251
8.00k
                if (num_bits)
252
2.69k
                    TINFL_GET_BITS(6, r->m_raw_header[counter], 8);
253
5.31k
                else
254
5.31k
                    TINFL_GET_BYTE(7, r->m_raw_header[counter]);
255
8.00k
            }
256
1.99k
            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.19k
            while ((counter) && (num_bits))
261
243
            {
262
243
                TINFL_GET_BITS(51, dist, 8);
263
243
                while (pOut_buf_cur >= pOut_buf_end)
264
2
                {
265
2
                    TINFL_CR_RETURN(52, TINFL_STATUS_HAS_MORE_OUTPUT);
266
2
                }
267
241
                *pOut_buf_cur++ = (mz_uint8)dist;
268
241
                counter--;
269
241
            }
270
2.92k
            while (counter)
271
993
            {
272
993
                size_t n;
273
993
                while (pOut_buf_cur >= pOut_buf_end)
274
3
                {
275
3
                    TINFL_CR_RETURN(9, TINFL_STATUS_HAS_MORE_OUTPUT);
276
3
                }
277
990
                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
975
                n = MZ_MIN(MZ_MIN((size_t)(pOut_buf_end - pOut_buf_cur), (size_t)(pIn_buf_end - pIn_buf_cur)), counter);
282
975
                TINFL_MEMCPY(pOut_buf_cur, pIn_buf_cur, n);
283
975
                pIn_buf_cur += n;
284
975
                pOut_buf_cur += n;
285
975
                counter -= (mz_uint)n;
286
975
            }
287
1.95k
        }
288
6.07k
        else if (r->m_type == 3)
289
10
        {
290
10
            TINFL_CR_RETURN_FOREVER(10, TINFL_STATUS_FAILED);
291
10
        }
292
6.06k
        else
293
6.06k
        {
294
6.06k
            if (r->m_type == 1)
295
2.25k
            {
296
2.25k
                mz_uint8 *p = r->m_code_size_0;
297
2.25k
                mz_uint i;
298
2.25k
                r->m_table_sizes[0] = 288;
299
2.25k
                r->m_table_sizes[1] = 32;
300
2.25k
                TINFL_MEMSET(r->m_code_size_1, 5, 32);
301
326k
                for (i = 0; i <= 143; ++i)
302
324k
                    *p++ = 8;
303
254k
                for (; i <= 255; ++i)
304
252k
                    *p++ = 9;
305
56.3k
                for (; i <= 279; ++i)
306
54.0k
                    *p++ = 7;
307
20.2k
                for (; i <= 287; ++i)
308
18.0k
                    *p++ = 8;
309
2.25k
            }
310
3.81k
            else
311
3.81k
            {
312
15.2k
                for (counter = 0; counter < 3; counter++)
313
11.4k
                {
314
11.4k
                    TINFL_GET_BITS(11, r->m_table_sizes[counter], "\05\05\04"[counter]);
315
11.4k
                    r->m_table_sizes[counter] += s_min_table_sizes[counter];
316
11.4k
                }
317
3.81k
                MZ_CLEAR_ARR(r->m_code_size_2);
318
59.5k
                for (counter = 0; counter < r->m_table_sizes[2]; counter++)
319
55.7k
                {
320
55.7k
                    mz_uint s;
321
55.7k
                    TINFL_GET_BITS(14, s, 3);
322
55.7k
                    r->m_code_size_2[s_length_dezigzag[counter]] = (mz_uint8)s;
323
55.7k
                }
324
3.80k
                r->m_table_sizes[2] = 19;
325
3.80k
            }
326
21.8k
            for (; (int)r->m_type >= 0; r->m_type--)
327
15.8k
            {
328
15.8k
                int tree_next, tree_cur;
329
15.8k
                mz_int16 *pLookUp;
330
15.8k
                mz_int16 *pTree;
331
15.8k
                mz_uint8 *pCode_size;
332
15.8k
                mz_uint i, j, used_syms, total, sym_index, next_code[17], total_syms[16];
333
15.8k
                pLookUp = r->m_look_up[r->m_type];
334
15.8k
                pTree = pTrees[r->m_type];
335
15.8k
                pCode_size = pCode_sizes[r->m_type];
336
15.8k
                MZ_CLEAR_ARR(total_syms);
337
15.8k
                TINFL_MEMSET(pLookUp, 0, sizeof(r->m_look_up[0]));
338
15.8k
                tinfl_clear_tree(r);
339
1.92M
                for (i = 0; i < r->m_table_sizes[r->m_type]; ++i)
340
1.91M
                    total_syms[pCode_size[i]]++;
341
15.8k
                used_syms = 0, total = 0;
342
15.8k
                next_code[0] = next_code[1] = 0;
343
253k
                for (i = 1; i <= 15; ++i)
344
237k
                {
345
237k
                    used_syms += total_syms[i];
346
237k
                    next_code[i + 1] = (total = ((total + total_syms[i]) << 1));
347
237k
                }
348
15.8k
                if ((65536 != total) && (used_syms > 1))
349
44
                {
350
44
                    TINFL_CR_RETURN_FOREVER(35, TINFL_STATUS_FAILED);
351
44
                }
352
1.92M
                for (tree_next = -1, sym_index = 0; sym_index < r->m_table_sizes[r->m_type]; ++sym_index)
353
1.90M
                {
354
1.90M
                    mz_uint rev_code = 0, l, cur_code, code_size = pCode_size[sym_index];
355
1.90M
                    if (!code_size)
356
732k
                        continue;
357
1.17M
                    cur_code = next_code[code_size]++;
358
10.1M
                    for (l = code_size; l > 0; l--, cur_code >>= 1)
359
8.99M
                        rev_code = (rev_code << 1) | (cur_code & 1);
360
1.17M
                    if (code_size <= TINFL_FAST_LOOKUP_BITS)
361
1.16M
                    {
362
1.16M
                        mz_int16 k = (mz_int16)((code_size << 9) | sym_index);
363
15.7M
                        while (rev_code < TINFL_FAST_LOOKUP_SIZE)
364
14.5M
                        {
365
14.5M
                            pLookUp[rev_code] = k;
366
14.5M
                            rev_code += (1 << code_size);
367
14.5M
                        }
368
1.16M
                        continue;
369
1.16M
                    }
370
12.9k
                    if (0 == (tree_cur = pLookUp[rev_code & (TINFL_FAST_LOOKUP_SIZE - 1)]))
371
4.64k
                    {
372
4.64k
                        pLookUp[rev_code & (TINFL_FAST_LOOKUP_SIZE - 1)] = (mz_int16)tree_next;
373
4.64k
                        tree_cur = tree_next;
374
4.64k
                        tree_next -= 2;
375
4.64k
                    }
376
12.9k
                    rev_code >>= (TINFL_FAST_LOOKUP_BITS - 1);
377
23.5k
                    for (j = code_size; j > (TINFL_FAST_LOOKUP_BITS + 1); j--)
378
10.5k
                    {
379
10.5k
                        tree_cur -= ((rev_code >>= 1) & 1);
380
10.5k
                        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.72k
                        else
387
6.72k
                            tree_cur = pTree[-tree_cur - 1];
388
10.5k
                    }
389
12.9k
                    tree_cur -= ((rev_code >>= 1) & 1);
390
12.9k
                    pTree[-tree_cur - 1] = (mz_int16)sym_index;
391
12.9k
                }
392
15.7k
                if (r->m_type == 2)
393
3.78k
                {
394
540k
                    for (counter = 0; counter < (r->m_table_sizes[0] + r->m_table_sizes[1]);)
395
537k
                    {
396
537k
                        mz_uint s;
397
537k
                        TINFL_HUFF_DECODE(16, dist, r->m_look_up[2], r->m_tree_2);
398
537k
                        if (dist < 16)
399
478k
                        {
400
478k
                            r->m_len_codes[counter++] = (mz_uint8)dist;
401
478k
                            continue;
402
478k
                        }
403
58.2k
                        if ((dist == 16) && (!counter))
404
4
                        {
405
4
                            TINFL_CR_RETURN_FOREVER(17, TINFL_STATUS_FAILED);
406
4
                        }
407
58.2k
                        num_extra = "\02\03\07"[dist - 16];
408
58.2k
                        TINFL_GET_BITS(18, s, num_extra);
409
58.2k
                        s += "\03\03\013"[dist - 16];
410
58.2k
                        TINFL_MEMSET(r->m_len_codes + counter, (dist == 16) ? r->m_len_codes[counter - 1] : 0, s);
411
58.2k
                        counter += s;
412
58.2k
                    }
413
3.77k
                    if ((r->m_table_sizes[0] + r->m_table_sizes[1]) != counter)
414
17
                    {
415
17
                        TINFL_CR_RETURN_FOREVER(21, TINFL_STATUS_FAILED);
416
17
                    }
417
3.75k
                    TINFL_MEMCPY(r->m_code_size_0, r->m_len_codes, r->m_table_sizes[0]);
418
3.75k
                    TINFL_MEMCPY(r->m_code_size_1, r->m_len_codes + r->m_table_sizes[0], r->m_table_sizes[1]);
419
3.75k
                }
420
15.7k
            }
421
5.98k
            for (;;)
422
808k
            {
423
808k
                mz_uint8 *pSrc;
424
808k
                for (;;)
425
466M
                {
426
466M
                    if (((pIn_buf_end - pIn_buf_cur) < 4) || ((pOut_buf_end - pOut_buf_cur) < 2))
427
48.9M
                    {
428
48.9M
                        TINFL_HUFF_DECODE(23, counter, r->m_look_up[0], r->m_tree_0);
429
48.9M
                        if (counter >= 256)
430
7.29k
                            break;
431
48.9M
                        while (pOut_buf_cur >= pOut_buf_end)
432
51
                        {
433
51
                            TINFL_CR_RETURN(24, TINFL_STATUS_HAS_MORE_OUTPUT);
434
51
                        }
435
48.9M
                        *pOut_buf_cur++ = (mz_uint8)counter;
436
48.9M
                    }
437
417M
                    else
438
417M
                    {
439
417M
                        int sym2;
440
417M
                        mz_uint code_len;
441
417M
#if TINFL_USE_64BIT_BITBUF
442
417M
                        if (num_bits < 30)
443
970k
                        {
444
970k
                            bit_buf |= (((tinfl_bit_buf_t)MZ_READ_LE32(pIn_buf_cur)) << num_bits);
445
970k
                            pIn_buf_cur += 4;
446
970k
                            num_bits += 32;
447
970k
                        }
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
417M
                        if ((sym2 = r->m_look_up[0][bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0)
457
417M
                            code_len = sym2 >> 9;
458
11.6k
                        else
459
11.6k
                        {
460
11.6k
                            code_len = TINFL_FAST_LOOKUP_BITS;
461
11.6k
                            do
462
23.9k
                            {
463
23.9k
                                sym2 = r->m_tree_0[~sym2 + ((bit_buf >> code_len++) & 1)];
464
23.9k
                            } while (sym2 < 0);
465
11.6k
                        }
466
417M
                        counter = sym2;
467
417M
                        bit_buf >>= code_len;
468
417M
                        num_bits -= code_len;
469
417M
                        if (counter & 256)
470
720k
                            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
416M
                        if ((sym2 = r->m_look_up[0][bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0)
481
416M
                            code_len = sym2 >> 9;
482
8.50k
                        else
483
8.50k
                        {
484
8.50k
                            code_len = TINFL_FAST_LOOKUP_BITS;
485
8.50k
                            do
486
18.5k
                            {
487
18.5k
                                sym2 = r->m_tree_0[~sym2 + ((bit_buf >> code_len++) & 1)];
488
18.5k
                            } while (sym2 < 0);
489
8.50k
                        }
490
416M
                        bit_buf >>= code_len;
491
416M
                        num_bits -= code_len;
492
493
416M
                        pOut_buf_cur[0] = (mz_uint8)counter;
494
416M
                        if (sym2 & 256)
495
81.3k
                        {
496
81.3k
                            pOut_buf_cur++;
497
81.3k
                            counter = sym2;
498
81.3k
                            break;
499
81.3k
                        }
500
416M
                        pOut_buf_cur[1] = (mz_uint8)sym2;
501
416M
                        pOut_buf_cur += 2;
502
416M
                    }
503
466M
                }
504
808k
                if ((counter &= 511) == 256)
505
5.83k
                    break;
506
507
803k
                num_extra = s_length_extra[counter - 257];
508
803k
                counter = s_length_base[counter - 257];
509
803k
                if (num_extra)
510
82.9k
                {
511
82.9k
                    mz_uint extra_bits;
512
82.9k
                    TINFL_GET_BITS(25, extra_bits, num_extra);
513
82.9k
                    counter += extra_bits;
514
82.9k
                }
515
516
802k
                TINFL_HUFF_DECODE(26, dist, r->m_look_up[1], r->m_tree_1);
517
802k
                num_extra = s_dist_extra[dist];
518
802k
                dist = s_dist_base[dist];
519
802k
                if (num_extra)
520
193k
                {
521
193k
                    mz_uint extra_bits;
522
193k
                    TINFL_GET_BITS(27, extra_bits, num_extra);
523
193k
                    dist += extra_bits;
524
193k
                }
525
526
802k
                dist_from_out_buf_start = pOut_buf_cur - pOut_buf_start;
527
802k
                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
15
                {
529
15
                    TINFL_CR_RETURN_FOREVER(37, TINFL_STATUS_FAILED);
530
15
                }
531
532
802k
                pSrc = pOut_buf_start + ((dist_from_out_buf_start - dist) & out_buf_size_mask);
533
534
802k
                if ((MZ_MAX(pOut_buf_cur, pSrc) + counter) > pOut_buf_end)
535
12
                {
536
407
                    while (counter--)
537
407
                    {
538
407
                        while (pOut_buf_cur >= pOut_buf_end)
539
12
                        {
540
12
                            TINFL_CR_RETURN(53, TINFL_STATUS_HAS_MORE_OUTPUT);
541
12
                        }
542
395
                        *pOut_buf_cur++ = pOut_buf_start[(dist_from_out_buf_start++ - dist) & out_buf_size_mask];
543
395
                    }
544
0
                    continue;
545
12
                }
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
50.0M
                while(counter>2)
574
49.2M
                {
575
49.2M
                    pOut_buf_cur[0] = pSrc[0];
576
49.2M
                    pOut_buf_cur[1] = pSrc[1];
577
49.2M
                    pOut_buf_cur[2] = pSrc[2];
578
49.2M
                    pOut_buf_cur += 3;
579
49.2M
                    pSrc += 3;
580
49.2M
          counter -= 3;
581
49.2M
                }
582
802k
                if (counter > 0)
583
115k
                {
584
115k
                    pOut_buf_cur[0] = pSrc[0];
585
115k
                    if (counter > 1)
586
41.9k
                        pOut_buf_cur[1] = pSrc[1];
587
115k
                    pOut_buf_cur += counter;
588
115k
                }
589
802k
            }
590
5.98k
        }
591
8.08k
    } 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.43k
    TINFL_SKIP_BITS(32, num_bits & 7);
596
2.57k
    while ((pIn_buf_cur > pIn_buf_next) && (num_bits >= 8))
597
144
    {
598
144
        --pIn_buf_cur;
599
144
        num_bits -= 8;
600
144
    }
601
2.43k
    bit_buf &= ~(~(tinfl_bit_buf_t)0 << num_bits);
602
2.43k
    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.43k
    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.75k
    TINFL_CR_RETURN_FOREVER(34, TINFL_STATUS_DONE);
617
618
2.75k
    TINFL_CR_FINISH
619
620
2.75k
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.75k
    if ((status != TINFL_STATUS_NEEDS_MORE_INPUT) && (status != TINFL_STATUS_FAILED_CANNOT_MAKE_PROGRESS))
625
2.63k
    {
626
3.01k
        while ((pIn_buf_cur > pIn_buf_next) && (num_bits >= 8))
627
379
        {
628
379
            --pIn_buf_cur;
629
379
            num_bits -= 8;
630
379
        }
631
2.63k
    }
632
2.75k
    r->m_num_bits = num_bits;
633
2.75k
    r->m_bit_buf = bit_buf & ~(~(tinfl_bit_buf_t)0 << num_bits);
634
2.75k
    r->m_dist = dist;
635
2.75k
    r->m_counter = counter;
636
2.75k
    r->m_num_extra = num_extra;
637
2.75k
    r->m_dist_from_out_buf_start = dist_from_out_buf_start;
638
2.75k
    *pIn_buf_size = pIn_buf_cur - pIn_buf_next;
639
2.75k
    *pOut_buf_size = pOut_buf_cur - pOut_buf_next;
640
2.75k
    if ((decomp_flags & (TINFL_FLAG_PARSE_ZLIB_HEADER | TINFL_FLAG_COMPUTE_ADLER32)) && (status >= 0))
641
2.49k
    {
642
2.49k
        const mz_uint8 *ptr = pOut_buf_next;
643
2.49k
        size_t buf_len = *pOut_buf_size;
644
2.49k
        mz_uint32 i, s1 = r->m_check_adler32 & 0xffff, s2 = r->m_check_adler32 >> 16;
645
2.49k
        size_t block_len = buf_len % 5552;
646
165k
        while (buf_len)
647
163k
        {
648
112M
            for (i = 0; i + 7 < block_len; i += 8, ptr += 8)
649
111M
            {
650
111M
                s1 += ptr[0], s2 += s1;
651
111M
                s1 += ptr[1], s2 += s1;
652
111M
                s1 += ptr[2], s2 += s1;
653
111M
                s1 += ptr[3], s2 += s1;
654
111M
                s1 += ptr[4], s2 += s1;
655
111M
                s1 += ptr[5], s2 += s1;
656
111M
                s1 += ptr[6], s2 += s1;
657
111M
                s1 += ptr[7], s2 += s1;
658
111M
            }
659
170k
            for (; i < block_len; ++i)
660
7.80k
                s1 += *ptr++, s2 += s1;
661
163k
            s1 %= 65521U, s2 %= 65521U;
662
163k
            buf_len -= block_len;
663
163k
            block_len = 5552;
664
163k
        }
665
2.49k
        r->m_check_adler32 = (s2 << 16) + s1;
666
2.49k
        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.49k
    }
669
2.75k
    return status;
670
2.75k
}
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*/