Coverage Report

Created: 2025-12-13 07:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/zlib/inflate.c
Line
Count
Source
1
/* inflate.c -- zlib decompression
2
 * Copyright (C) 1995-2025 Mark Adler
3
 * For conditions of distribution and use, see copyright notice in zlib.h
4
 */
5
6
/*
7
 * Change history:
8
 *
9
 * 1.2.beta0    24 Nov 2002
10
 * - First version -- complete rewrite of inflate to simplify code, avoid
11
 *   creation of window when not needed, minimize use of window when it is
12
 *   needed, make inffast.c even faster, implement gzip decoding, and to
13
 *   improve code readability and style over the previous zlib inflate code
14
 *
15
 * 1.2.beta1    25 Nov 2002
16
 * - Use pointers for available input and output checking in inffast.c
17
 * - Remove input and output counters in inffast.c
18
 * - Change inffast.c entry and loop from avail_in >= 7 to >= 6
19
 * - Remove unnecessary second byte pull from length extra in inffast.c
20
 * - Unroll direct copy to three copies per loop in inffast.c
21
 *
22
 * 1.2.beta2    4 Dec 2002
23
 * - Change external routine names to reduce potential conflicts
24
 * - Correct filename to inffixed.h for fixed tables in inflate.c
25
 * - Make hbuf[] unsigned char to match parameter type in inflate.c
26
 * - Change strm->next_out[-state->offset] to *(strm->next_out - state->offset)
27
 *   to avoid negation problem on Alphas (64 bit) in inflate.c
28
 *
29
 * 1.2.beta3    22 Dec 2002
30
 * - Add comments on state->bits assertion in inffast.c
31
 * - Add comments on op field in inftrees.h
32
 * - Fix bug in reuse of allocated window after inflateReset()
33
 * - Remove bit fields--back to byte structure for speed
34
 * - Remove distance extra == 0 check in inflate_fast()--only helps for lengths
35
 * - Change post-increments to pre-increments in inflate_fast(), PPC biased?
36
 * - Add compile time option, POSTINC, to use post-increments instead (Intel?)
37
 * - Make MATCH copy in inflate() much faster for when inflate_fast() not used
38
 * - Use local copies of stream next and avail values, as well as local bit
39
 *   buffer and bit count in inflate()--for speed when inflate_fast() not used
40
 *
41
 * 1.2.beta4    1 Jan 2003
42
 * - Split ptr - 257 statements in inflate_table() to avoid compiler warnings
43
 * - Move a comment on output buffer sizes from inffast.c to inflate.c
44
 * - Add comments in inffast.c to introduce the inflate_fast() routine
45
 * - Rearrange window copies in inflate_fast() for speed and simplification
46
 * - Unroll last copy for window match in inflate_fast()
47
 * - Use local copies of window variables in inflate_fast() for speed
48
 * - Pull out common wnext == 0 case for speed in inflate_fast()
49
 * - Make op and len in inflate_fast() unsigned for consistency
50
 * - Add FAR to lcode and dcode declarations in inflate_fast()
51
 * - Simplified bad distance check in inflate_fast()
52
 * - Added inflateBackInit(), inflateBack(), and inflateBackEnd() in new
53
 *   source file infback.c to provide a call-back interface to inflate for
54
 *   programs like gzip and unzip -- uses window as output buffer to avoid
55
 *   window copying
56
 *
57
 * 1.2.beta5    1 Jan 2003
58
 * - Improved inflateBack() interface to allow the caller to provide initial
59
 *   input in strm.
60
 * - Fixed stored blocks bug in inflateBack()
61
 *
62
 * 1.2.beta6    4 Jan 2003
63
 * - Added comments in inffast.c on effectiveness of POSTINC
64
 * - Typecasting all around to reduce compiler warnings
65
 * - Changed loops from while (1) or do {} while (1) to for (;;), again to
66
 *   make compilers happy
67
 * - Changed type of window in inflateBackInit() to unsigned char *
68
 *
69
 * 1.2.beta7    27 Jan 2003
70
 * - Changed many types to unsigned or unsigned short to avoid warnings
71
 * - Added inflateCopy() function
72
 *
73
 * 1.2.0        9 Mar 2003
74
 * - Changed inflateBack() interface to provide separate opaque descriptors
75
 *   for the in() and out() functions
76
 * - Changed inflateBack() argument and in_func typedef to swap the length
77
 *   and buffer address return values for the input function
78
 * - Check next_in and next_out for Z_NULL on entry to inflate()
79
 *
80
 * The history for versions after 1.2.0 are in ChangeLog in zlib distribution.
81
 */
82
83
#include "zutil.h"
84
#include "inftrees.h"
85
#include "inflate.h"
86
#include "inffast.h"
87
88
#ifdef MAKEFIXED
89
#  ifndef BUILDFIXED
90
#    define BUILDFIXED
91
#  endif
92
#endif
93
94
2.69M
local int inflateStateCheck(z_streamp strm) {
95
2.69M
    struct inflate_state FAR *state;
96
2.69M
    if (strm == Z_NULL ||
97
2.69M
        strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0)
98
0
        return 1;
99
2.69M
    state = (struct inflate_state FAR *)strm->state;
100
2.69M
    if (state == Z_NULL || state->strm != strm ||
101
2.66M
        state->mode < HEAD || state->mode > SYNC)
102
30.6k
        return 1;
103
2.66M
    return 0;
104
2.69M
}
105
106
305k
int ZEXPORT inflateResetKeep(z_streamp strm) {
107
305k
    struct inflate_state FAR *state;
108
109
305k
    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
110
305k
    state = (struct inflate_state FAR *)strm->state;
111
305k
    strm->total_in = strm->total_out = state->total = 0;
112
305k
    strm->msg = Z_NULL;
113
305k
    strm->data_type = 0;
114
305k
    if (state->wrap)        /* to support ill-conceived Java test suite */
115
305k
        strm->adler = state->wrap & 1;
116
305k
    state->mode = HEAD;
117
305k
    state->last = 0;
118
305k
    state->havedict = 0;
119
305k
    state->flags = -1;
120
305k
    state->dmax = 32768U;
121
305k
    state->head = Z_NULL;
122
305k
    state->hold = 0;
123
305k
    state->bits = 0;
124
305k
    state->lencode = state->distcode = state->next = state->codes;
125
305k
    state->sane = 1;
126
305k
    state->back = -1;
127
305k
    Tracev((stderr, "inflate: reset\n"));
128
305k
    return Z_OK;
129
305k
}
130
131
305k
int ZEXPORT inflateReset(z_streamp strm) {
132
305k
    struct inflate_state FAR *state;
133
134
305k
    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
135
305k
    state = (struct inflate_state FAR *)strm->state;
136
305k
    state->wsize = 0;
137
305k
    state->whave = 0;
138
305k
    state->wnext = 0;
139
305k
    return inflateResetKeep(strm);
140
305k
}
141
142
164k
int ZEXPORT inflateReset2(z_streamp strm, int windowBits) {
143
164k
    int wrap;
144
164k
    struct inflate_state FAR *state;
145
146
    /* get the state */
147
164k
    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
148
164k
    state = (struct inflate_state FAR *)strm->state;
149
150
    /* extract wrap request from windowBits parameter */
151
164k
    if (windowBits < 0) {
152
506
        if (windowBits < -15)
153
0
            return Z_STREAM_ERROR;
154
506
        wrap = 0;
155
506
        windowBits = -windowBits;
156
506
    }
157
164k
    else {
158
164k
        wrap = (windowBits >> 4) + 5;
159
164k
#ifdef GUNZIP
160
164k
        if (windowBits < 48)
161
164k
            windowBits &= 15;
162
164k
#endif
163
164k
    }
164
165
    /* set number of window bits, free window if different */
166
164k
    if (windowBits && (windowBits < 8 || windowBits > 15))
167
0
        return Z_STREAM_ERROR;
168
164k
    if (state->window != Z_NULL && state->wbits != (unsigned)windowBits) {
169
30.6k
        ZFREE(strm, state->window);
170
30.6k
        state->window = Z_NULL;
171
30.6k
    }
172
173
    /* update state and reset the rest of it */
174
164k
    state->wrap = wrap;
175
164k
    state->wbits = (unsigned)windowBits;
176
164k
    return inflateReset(strm);
177
164k
}
178
179
int ZEXPORT inflateInit2_(z_streamp strm, int windowBits,
180
112k
                          const char *version, int stream_size) {
181
112k
    int ret;
182
112k
    struct inflate_state FAR *state;
183
184
112k
    if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
185
112k
        stream_size != (int)(sizeof(z_stream)))
186
0
        return Z_VERSION_ERROR;
187
112k
    if (strm == Z_NULL) return Z_STREAM_ERROR;
188
112k
    strm->msg = Z_NULL;                 /* in case we return an error */
189
112k
    if (strm->zalloc == (alloc_func)0) {
190
#ifdef Z_SOLO
191
        return Z_STREAM_ERROR;
192
#else
193
48.7k
        strm->zalloc = zcalloc;
194
48.7k
        strm->opaque = (voidpf)0;
195
48.7k
#endif
196
48.7k
    }
197
112k
    if (strm->zfree == (free_func)0)
198
#ifdef Z_SOLO
199
        return Z_STREAM_ERROR;
200
#else
201
48.7k
        strm->zfree = zcfree;
202
112k
#endif
203
112k
    state = (struct inflate_state FAR *)
204
112k
            ZALLOC(strm, 1, sizeof(struct inflate_state));
205
112k
    if (state == Z_NULL) return Z_MEM_ERROR;
206
112k
    Tracev((stderr, "inflate: allocated\n"));
207
112k
    strm->state = (struct internal_state FAR *)state;
208
112k
    state->strm = strm;
209
112k
    state->window = Z_NULL;
210
112k
    state->mode = HEAD;     /* to pass state test in inflateReset2() */
211
112k
    ret = inflateReset2(strm, windowBits);
212
112k
    if (ret != Z_OK) {
213
0
        ZFREE(strm, state);
214
0
        strm->state = Z_NULL;
215
0
    }
216
112k
    return ret;
217
112k
}
218
219
int ZEXPORT inflateInit_(z_streamp strm, const char *version,
220
8.11k
                         int stream_size) {
221
8.11k
    return inflateInit2_(strm, DEF_WBITS, version, stream_size);
222
8.11k
}
223
224
0
int ZEXPORT inflatePrime(z_streamp strm, int bits, int value) {
225
0
    struct inflate_state FAR *state;
226
227
0
    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
228
0
    if (bits == 0)
229
0
        return Z_OK;
230
0
    state = (struct inflate_state FAR *)strm->state;
231
0
    if (bits < 0) {
232
0
        state->hold = 0;
233
0
        state->bits = 0;
234
0
        return Z_OK;
235
0
    }
236
0
    if (bits > 16 || state->bits + (uInt)bits > 32) return Z_STREAM_ERROR;
237
0
    value &= (1L << bits) - 1;
238
0
    state->hold += (unsigned long)value << state->bits;
239
0
    state->bits += (uInt)bits;
240
0
    return Z_OK;
241
0
}
242
243
/*
244
   Return state with length and distance decoding tables and index sizes set to
245
   fixed code decoding.  Normally this returns fixed tables from inffixed.h.
246
   If BUILDFIXED is defined, then instead this routine builds the tables the
247
   first time it's called, and returns those tables the first time and
248
   thereafter.  This reduces the size of the code by about 2K bytes, in
249
   exchange for a little execution time.  However, BUILDFIXED should not be
250
   used for threaded applications, since the rewriting of the tables and virgin
251
   may not be thread-safe.
252
 */
253
242k
local void fixedtables(struct inflate_state FAR *state) {
254
#ifdef BUILDFIXED
255
    static int virgin = 1;
256
    static code *lenfix, *distfix;
257
    static code fixed[544];
258
259
    /* build fixed huffman tables if first call (may not be thread safe) */
260
    if (virgin) {
261
        unsigned sym, bits;
262
        static code *next;
263
264
        /* literal/length table */
265
        sym = 0;
266
        while (sym < 144) state->lens[sym++] = 8;
267
        while (sym < 256) state->lens[sym++] = 9;
268
        while (sym < 280) state->lens[sym++] = 7;
269
        while (sym < 288) state->lens[sym++] = 8;
270
        next = fixed;
271
        lenfix = next;
272
        bits = 9;
273
        inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
274
275
        /* distance table */
276
        sym = 0;
277
        while (sym < 32) state->lens[sym++] = 5;
278
        distfix = next;
279
        bits = 5;
280
        inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
281
282
        /* do this just once */
283
        virgin = 0;
284
    }
285
#else /* !BUILDFIXED */
286
242k
#   include "inffixed.h"
287
242k
#endif /* BUILDFIXED */
288
242k
    state->lencode = lenfix;
289
242k
    state->lenbits = 9;
290
242k
    state->distcode = distfix;
291
242k
    state->distbits = 5;
292
242k
}
293
294
#ifdef MAKEFIXED
295
#include <stdio.h>
296
297
/*
298
   Write out the inffixed.h that is #include'd above.  Defining MAKEFIXED also
299
   defines BUILDFIXED, so the tables are built on the fly.  makefixed() writes
300
   those tables to stdout, which would be piped to inffixed.h.  A small program
301
   can simply call makefixed to do this:
302
303
    void makefixed(void);
304
305
    int main(void)
306
    {
307
        makefixed();
308
        return 0;
309
    }
310
311
   Then that can be linked with zlib built with MAKEFIXED defined and run:
312
313
    a.out > inffixed.h
314
 */
315
void makefixed(void)
316
{
317
    unsigned low, size;
318
    struct inflate_state state;
319
320
    fixedtables(&state);
321
    puts("    /* inffixed.h -- table for decoding fixed codes");
322
    puts("     * Generated automatically by makefixed().");
323
    puts("     */");
324
    puts("");
325
    puts("    /* WARNING: this file should *not* be used by applications.");
326
    puts("       It is part of the implementation of this library and is");
327
    puts("       subject to change. Applications should only use zlib.h.");
328
    puts("     */");
329
    puts("");
330
    size = 1U << 9;
331
    printf("    static const code lenfix[%u] = {", size);
332
    low = 0;
333
    for (;;) {
334
        if ((low % 7) == 0) printf("\n        ");
335
        printf("{%u,%u,%d}", (low & 127) == 99 ? 64 : state.lencode[low].op,
336
               state.lencode[low].bits, state.lencode[low].val);
337
        if (++low == size) break;
338
        putchar(',');
339
    }
340
    puts("\n    };");
341
    size = 1U << 5;
342
    printf("\n    static const code distfix[%u] = {", size);
343
    low = 0;
344
    for (;;) {
345
        if ((low % 6) == 0) printf("\n        ");
346
        printf("{%u,%u,%d}", state.distcode[low].op, state.distcode[low].bits,
347
               state.distcode[low].val);
348
        if (++low == size) break;
349
        putchar(',');
350
    }
351
    puts("\n    };");
352
}
353
#endif /* MAKEFIXED */
354
355
/*
356
   Update the window with the last wsize (normally 32K) bytes written before
357
   returning.  If window does not exist yet, create it.  This is only called
358
   when a window is already in use, or when output has been written during this
359
   inflate call, but the end of the deflate stream has not been reached yet.
360
   It is also called to create a window for dictionary data when a dictionary
361
   is loaded.
362
363
   Providing output buffers larger than 32K to inflate() should provide a speed
364
   advantage, since only the last 32K of output is copied to the sliding window
365
   upon return from inflate(), and since all distances after the first 32K of
366
   output will fall in the output data, making match copies simpler and faster.
367
   The advantage may be dependent on the size of the processor's data caches.
368
 */
369
1.62M
local int updatewindow(z_streamp strm, const Bytef *end, unsigned copy) {
370
1.62M
    struct inflate_state FAR *state;
371
1.62M
    unsigned dist;
372
373
1.62M
    state = (struct inflate_state FAR *)strm->state;
374
375
    /* if it hasn't been done already, allocate space for the window */
376
1.62M
    if (state->window == Z_NULL) {
377
102k
        state->window = (unsigned char FAR *)
378
102k
                        ZALLOC(strm, 1U << state->wbits,
379
102k
                               sizeof(unsigned char));
380
102k
        if (state->window == Z_NULL) return 1;
381
102k
    }
382
383
    /* if window not in use yet, initialize */
384
1.62M
    if (state->wsize == 0) {
385
105k
        state->wsize = 1U << state->wbits;
386
105k
        state->wnext = 0;
387
105k
        state->whave = 0;
388
105k
    }
389
390
    /* copy state->wsize or less output bytes into the circular window */
391
1.62M
    if (copy >= state->wsize) {
392
14.8k
        zmemcpy(state->window, end - state->wsize, state->wsize);
393
14.8k
        state->wnext = 0;
394
14.8k
        state->whave = state->wsize;
395
14.8k
    }
396
1.60M
    else {
397
1.60M
        dist = state->wsize - state->wnext;
398
1.60M
        if (dist > copy) dist = copy;
399
1.60M
        zmemcpy(state->window + state->wnext, end - copy, dist);
400
1.60M
        copy -= dist;
401
1.60M
        if (copy) {
402
25.8k
            zmemcpy(state->window, end - copy, copy);
403
25.8k
            state->wnext = copy;
404
25.8k
            state->whave = state->wsize;
405
25.8k
        }
406
1.57M
        else {
407
1.57M
            state->wnext += dist;
408
1.57M
            if (state->wnext == state->wsize) state->wnext = 0;
409
1.57M
            if (state->whave < state->wsize) state->whave += dist;
410
1.57M
        }
411
1.60M
    }
412
1.62M
    return 0;
413
1.62M
}
414
415
/* Macros for inflate(): */
416
417
/* check function to use adler32() for zlib or crc32() for gzip */
418
#ifdef GUNZIP
419
#  define UPDATE_CHECK(check, buf, len) \
420
1.61M
    (state->flags ? crc32(check, buf, len) : adler32(check, buf, len))
421
#else
422
#  define UPDATE_CHECK(check, buf, len) adler32(check, buf, len)
423
#endif
424
425
/* check macros for header crc */
426
#ifdef GUNZIP
427
#  define CRC2(check, word) \
428
194k
    do { \
429
194k
        hbuf[0] = (unsigned char)(word); \
430
194k
        hbuf[1] = (unsigned char)((word) >> 8); \
431
194k
        check = crc32(check, hbuf, 2); \
432
194k
    } while (0)
433
434
#  define CRC4(check, word) \
435
24.8k
    do { \
436
24.8k
        hbuf[0] = (unsigned char)(word); \
437
24.8k
        hbuf[1] = (unsigned char)((word) >> 8); \
438
24.8k
        hbuf[2] = (unsigned char)((word) >> 16); \
439
24.8k
        hbuf[3] = (unsigned char)((word) >> 24); \
440
24.8k
        check = crc32(check, hbuf, 4); \
441
24.8k
    } while (0)
442
#endif
443
444
/* Load registers with state in inflate() for speed */
445
#define LOAD() \
446
2.50M
    do { \
447
2.50M
        put = strm->next_out; \
448
2.50M
        left = strm->avail_out; \
449
2.50M
        next = strm->next_in; \
450
2.50M
        have = strm->avail_in; \
451
2.50M
        hold = state->hold; \
452
2.50M
        bits = state->bits; \
453
2.50M
    } while (0)
454
455
/* Restore state from registers in inflate() */
456
#define RESTORE() \
457
2.50M
    do { \
458
2.50M
        strm->next_out = put; \
459
2.50M
        strm->avail_out = left; \
460
2.50M
        strm->next_in = next; \
461
2.50M
        strm->avail_in = have; \
462
2.50M
        state->hold = hold; \
463
2.50M
        state->bits = bits; \
464
2.50M
    } while (0)
465
466
/* Clear the input bit accumulator */
467
#define INITBITS() \
468
867k
    do { \
469
867k
        hold = 0; \
470
867k
        bits = 0; \
471
867k
    } while (0)
472
473
/* Get a byte of input into the bit accumulator, or return from inflate()
474
   if there is no input available. */
475
#define PULLBYTE() \
476
11.6M
    do { \
477
11.6M
        if (have == 0) goto inf_leave; \
478
11.6M
        have--; \
479
11.4M
        hold += (unsigned long)(*next++) << bits; \
480
11.4M
        bits += 8; \
481
11.4M
    } while (0)
482
483
/* Assure that there are at least n bits in the bit accumulator.  If there is
484
   not enough available input to do that, then return from inflate(). */
485
#define NEEDBITS(n) \
486
4.86M
    do { \
487
9.36M
        while (bits < (unsigned)(n)) \
488
4.86M
            PULLBYTE(); \
489
4.86M
    } while (0)
490
491
/* Return the low n bits of the bit accumulator (n < 16) */
492
#define BITS(n) \
493
24.4M
    ((unsigned)hold & ((1U << (n)) - 1))
494
495
/* Remove n bits from the bit accumulator */
496
#define DROPBITS(n) \
497
17.1M
    do { \
498
17.1M
        hold >>= (n); \
499
17.1M
        bits -= (unsigned)(n); \
500
17.1M
    } while (0)
501
502
/* Remove zero to seven bits as needed to go to a byte boundary */
503
#define BYTEBITS() \
504
176k
    do { \
505
176k
        hold >>= bits & 7; \
506
176k
        bits -= bits & 7; \
507
176k
    } while (0)
508
509
/*
510
   inflate() uses a state machine to process as much input data and generate as
511
   much output data as possible before returning.  The state machine is
512
   structured roughly as follows:
513
514
    for (;;) switch (state) {
515
    ...
516
    case STATEn:
517
        if (not enough input data or output space to make progress)
518
            return;
519
        ... make progress ...
520
        state = STATEm;
521
        break;
522
    ...
523
    }
524
525
   so when inflate() is called again, the same case is attempted again, and
526
   if the appropriate resources are provided, the machine proceeds to the
527
   next state.  The NEEDBITS() macro is usually the way the state evaluates
528
   whether it can proceed or should return.  NEEDBITS() does the return if
529
   the requested bits are not available.  The typical use of the BITS macros
530
   is:
531
532
        NEEDBITS(n);
533
        ... do something with BITS(n) ...
534
        DROPBITS(n);
535
536
   where NEEDBITS(n) either returns from inflate() if there isn't enough
537
   input left to load n bits into the accumulator, or it continues.  BITS(n)
538
   gives the low n bits in the accumulator.  When done, DROPBITS(n) drops
539
   the low n bits off the accumulator.  INITBITS() clears the accumulator
540
   and sets the number of available bits to zero.  BYTEBITS() discards just
541
   enough bits to put the accumulator on a byte boundary.  After BYTEBITS()
542
   and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
543
544
   NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
545
   if there is no input available.  The decoding of variable length codes uses
546
   PULLBYTE() directly in order to pull just enough bytes to decode the next
547
   code, and no more.
548
549
   Some states loop until they get enough input, making sure that enough
550
   state information is maintained to continue the loop where it left off
551
   if NEEDBITS() returns in the loop.  For example, want, need, and keep
552
   would all have to actually be part of the saved state in case NEEDBITS()
553
   returns:
554
555
    case STATEw:
556
        while (want < need) {
557
            NEEDBITS(n);
558
            keep[want++] = BITS(n);
559
            DROPBITS(n);
560
        }
561
        state = STATEx;
562
    case STATEx:
563
564
   As shown above, if the next state is also the next case, then the break
565
   is omitted.
566
567
   A state may also return if there is not enough output space available to
568
   complete that state.  Those states are copying stored data, writing a
569
   literal byte, and copying a matching string.
570
571
   When returning, a "goto inf_leave" is used to update the total counters,
572
   update the check value, and determine whether any progress has been made
573
   during that inflate() call in order to return the proper return code.
574
   Progress is defined as a change in either strm->avail_in or strm->avail_out.
575
   When there is a window, goto inf_leave will update the window with the last
576
   output written.  If a goto inf_leave occurs in the middle of decompression
577
   and there is no window currently, goto inf_leave will create one and copy
578
   output to the window for the next call of inflate().
579
580
   In this implementation, the flush parameter of inflate() only affects the
581
   return code (per zlib.h).  inflate() always writes as much as possible to
582
   strm->next_out, given the space available and the provided input--the effect
583
   documented in zlib.h of Z_SYNC_FLUSH.  Furthermore, inflate() always defers
584
   the allocation of and copying into a sliding window until necessary, which
585
   provides the effect documented in zlib.h for Z_FINISH when the entire input
586
   stream available.  So the only thing the flush parameter actually does is:
587
   when flush is set to Z_FINISH, inflate() cannot return Z_OK.  Instead it
588
   will return Z_BUF_ERROR if it has not reached the end of the stream.
589
 */
590
591
1.77M
int ZEXPORT inflate(z_streamp strm, int flush) {
592
1.77M
    struct inflate_state FAR *state;
593
1.77M
    z_const unsigned char FAR *next;    /* next input */
594
1.77M
    unsigned char FAR *put;     /* next output */
595
1.77M
    unsigned have, left;        /* available input and output */
596
1.77M
    unsigned long hold;         /* bit buffer */
597
1.77M
    unsigned bits;              /* bits in bit buffer */
598
1.77M
    unsigned in, out;           /* save starting available input and output */
599
1.77M
    unsigned copy;              /* number of stored or match bytes to copy */
600
1.77M
    unsigned char FAR *from;    /* where to copy match bytes from */
601
1.77M
    code here;                  /* current decoding table entry */
602
1.77M
    code last;                  /* parent table entry */
603
1.77M
    unsigned len;               /* length to copy for repeats, bits to drop */
604
1.77M
    int ret;                    /* return code */
605
1.77M
#ifdef GUNZIP
606
1.77M
    unsigned char hbuf[4];      /* buffer for gzip header crc calculation */
607
1.77M
#endif
608
1.77M
    static const unsigned short order[19] = /* permutation of code lengths */
609
1.77M
        {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
610
611
1.77M
    if (inflateStateCheck(strm) || strm->next_out == Z_NULL ||
612
1.77M
        (strm->next_in == Z_NULL && strm->avail_in != 0))
613
0
        return Z_STREAM_ERROR;
614
615
1.77M
    state = (struct inflate_state FAR *)strm->state;
616
1.77M
    if (state->mode == TYPE) state->mode = TYPEDO;      /* skip check */
617
1.77M
    LOAD();
618
1.77M
    in = have;
619
1.77M
    out = left;
620
1.77M
    ret = Z_OK;
621
1.77M
    for (;;)
622
15.5M
        switch (state->mode) {
623
243k
        case HEAD:
624
243k
            if (state->wrap == 0) {
625
652
                state->mode = TYPEDO;
626
652
                break;
627
652
            }
628
242k
            NEEDBITS(16);
629
240k
#ifdef GUNZIP
630
240k
            if ((state->wrap & 2) && hold == 0x8b1f) {  /* gzip header */
631
121k
                if (state->wbits == 0)
632
0
                    state->wbits = 15;
633
121k
                state->check = crc32(0L, Z_NULL, 0);
634
121k
                CRC2(state->check, hold);
635
121k
                INITBITS();
636
121k
                state->mode = FLAGS;
637
121k
                break;
638
121k
            }
639
118k
            if (state->head != Z_NULL)
640
0
                state->head->done = -1;
641
118k
            if (!(state->wrap & 1) ||   /* check if zlib header allowed */
642
#else
643
            if (
644
#endif
645
117k
                ((BITS(8) << 8) + (hold >> 8)) % 31) {
646
2.14k
                strm->msg = (z_const char *)"incorrect header check";
647
2.14k
                state->mode = BAD;
648
2.14k
                break;
649
2.14k
            }
650
115k
            if (BITS(4) != Z_DEFLATED) {
651
1.58k
                strm->msg = (z_const char *)"unknown compression method";
652
1.58k
                state->mode = BAD;
653
1.58k
                break;
654
1.58k
            }
655
114k
            DROPBITS(4);
656
114k
            len = BITS(4) + 8;
657
114k
            if (state->wbits == 0)
658
103k
                state->wbits = len;
659
114k
            if (len > 15 || len > state->wbits) {
660
34
                strm->msg = (z_const char *)"invalid window size";
661
34
                state->mode = BAD;
662
34
                break;
663
34
            }
664
114k
            state->dmax = 1U << len;
665
114k
            state->flags = 0;               /* indicate zlib header */
666
114k
            Tracev((stderr, "inflate:   zlib header ok\n"));
667
114k
            strm->adler = state->check = adler32(0L, Z_NULL, 0);
668
114k
            state->mode = hold & 0x200 ? DICTID : TYPE;
669
114k
            INITBITS();
670
114k
            break;
671
0
#ifdef GUNZIP
672
121k
        case FLAGS:
673
121k
            NEEDBITS(16);
674
121k
            state->flags = (int)(hold);
675
121k
            if ((state->flags & 0xff) != Z_DEFLATED) {
676
77
                strm->msg = (z_const char *)"unknown compression method";
677
77
                state->mode = BAD;
678
77
                break;
679
77
            }
680
121k
            if (state->flags & 0xe000) {
681
38
                strm->msg = (z_const char *)"unknown header flags set";
682
38
                state->mode = BAD;
683
38
                break;
684
38
            }
685
121k
            if (state->head != Z_NULL)
686
0
                state->head->text = (int)((hold >> 8) & 1);
687
121k
            if ((state->flags & 0x0200) && (state->wrap & 4))
688
25.0k
                CRC2(state->check, hold);
689
121k
            INITBITS();
690
121k
            state->mode = TIME;
691
                /* fallthrough */
692
121k
        case TIME:
693
121k
            NEEDBITS(32);
694
121k
            if (state->head != Z_NULL)
695
0
                state->head->time = hold;
696
121k
            if ((state->flags & 0x0200) && (state->wrap & 4))
697
24.8k
                CRC4(state->check, hold);
698
121k
            INITBITS();
699
121k
            state->mode = OS;
700
                /* fallthrough */
701
121k
        case OS:
702
121k
            NEEDBITS(16);
703
120k
            if (state->head != Z_NULL) {
704
0
                state->head->xflags = (int)(hold & 0xff);
705
0
                state->head->os = (int)(hold >> 8);
706
0
            }
707
120k
            if ((state->flags & 0x0200) && (state->wrap & 4))
708
24.7k
                CRC2(state->check, hold);
709
120k
            INITBITS();
710
120k
            state->mode = EXLEN;
711
                /* fallthrough */
712
120k
        case EXLEN:
713
120k
            if (state->flags & 0x0400) {
714
31.0k
                NEEDBITS(16);
715
30.8k
                state->length = (unsigned)(hold);
716
30.8k
                if (state->head != Z_NULL)
717
0
                    state->head->extra_len = (unsigned)hold;
718
30.8k
                if ((state->flags & 0x0200) && (state->wrap & 4))
719
23.1k
                    CRC2(state->check, hold);
720
30.8k
                INITBITS();
721
30.8k
            }
722
89.7k
            else if (state->head != Z_NULL)
723
0
                state->head->extra = Z_NULL;
724
120k
            state->mode = EXTRA;
725
                /* fallthrough */
726
120k
        case EXTRA:
727
120k
            if (state->flags & 0x0400) {
728
30.8k
                copy = state->length;
729
30.8k
                if (copy > have) copy = have;
730
30.8k
                if (copy) {
731
7.24k
                    if (state->head != Z_NULL &&
732
0
                        state->head->extra != Z_NULL &&
733
0
                        (len = state->head->extra_len - state->length) <
734
0
                            state->head->extra_max) {
735
0
                        zmemcpy(state->head->extra + len, next,
736
0
                                len + copy > state->head->extra_max ?
737
0
                                state->head->extra_max - len : copy);
738
0
                    }
739
7.24k
                    if ((state->flags & 0x0200) && (state->wrap & 4))
740
3.57k
                        state->check = crc32(state->check, next, copy);
741
7.24k
                    have -= copy;
742
7.24k
                    next += copy;
743
7.24k
                    state->length -= copy;
744
7.24k
                }
745
30.8k
                if (state->length) goto inf_leave;
746
30.8k
            }
747
119k
            state->length = 0;
748
119k
            state->mode = NAME;
749
                /* fallthrough */
750
119k
        case NAME:
751
119k
            if (state->flags & 0x0800) {
752
32.3k
                if (have == 0) goto inf_leave;
753
32.2k
                copy = 0;
754
150k
                do {
755
150k
                    len = (unsigned)(next[copy++]);
756
150k
                    if (state->head != Z_NULL &&
757
0
                            state->head->name != Z_NULL &&
758
0
                            state->length < state->head->name_max)
759
0
                        state->head->name[state->length++] = (Bytef)len;
760
150k
                } while (len && copy < have);
761
32.2k
                if ((state->flags & 0x0200) && (state->wrap & 4))
762
22.7k
                    state->check = crc32(state->check, next, copy);
763
32.2k
                have -= copy;
764
32.2k
                next += copy;
765
32.2k
                if (len) goto inf_leave;
766
32.2k
            }
767
87.3k
            else if (state->head != Z_NULL)
768
0
                state->head->name = Z_NULL;
769
119k
            state->length = 0;
770
119k
            state->mode = COMMENT;
771
                /* fallthrough */
772
119k
        case COMMENT:
773
119k
            if (state->flags & 0x1000) {
774
34.6k
                if (have == 0) goto inf_leave;
775
34.5k
                copy = 0;
776
192k
                do {
777
192k
                    len = (unsigned)(next[copy++]);
778
192k
                    if (state->head != Z_NULL &&
779
0
                            state->head->comment != Z_NULL &&
780
0
                            state->length < state->head->comm_max)
781
0
                        state->head->comment[state->length++] = (Bytef)len;
782
192k
                } while (len && copy < have);
783
34.5k
                if ((state->flags & 0x0200) && (state->wrap & 4))
784
22.7k
                    state->check = crc32(state->check, next, copy);
785
34.5k
                have -= copy;
786
34.5k
                next += copy;
787
34.5k
                if (len) goto inf_leave;
788
34.5k
            }
789
84.6k
            else if (state->head != Z_NULL)
790
0
                state->head->comment = Z_NULL;
791
118k
            state->mode = HCRC;
792
                /* fallthrough */
793
118k
        case HCRC:
794
118k
            if (state->flags & 0x0200) {
795
23.4k
                NEEDBITS(16);
796
23.1k
                if ((state->wrap & 4) && hold != (state->check & 0xffff)) {
797
615
                    strm->msg = (z_const char *)"header crc mismatch";
798
615
                    state->mode = BAD;
799
615
                    break;
800
615
                }
801
22.4k
                INITBITS();
802
22.4k
            }
803
117k
            if (state->head != Z_NULL) {
804
0
                state->head->hcrc = (int)((state->flags >> 9) & 1);
805
0
                state->head->done = 1;
806
0
            }
807
117k
            strm->adler = state->check = crc32(0L, Z_NULL, 0);
808
117k
            state->mode = TYPE;
809
117k
            break;
810
0
#endif
811
1.43k
        case DICTID:
812
1.43k
            NEEDBITS(32);
813
1.20k
            strm->adler = state->check = ZSWAP32(hold);
814
1.20k
            INITBITS();
815
1.20k
            state->mode = DICT;
816
                /* fallthrough */
817
1.20k
        case DICT:
818
1.20k
            if (state->havedict == 0) {
819
1.20k
                RESTORE();
820
1.20k
                return Z_NEED_DICT;
821
1.20k
            }
822
0
            strm->adler = state->check = adler32(0L, Z_NULL, 0);
823
0
            state->mode = TYPE;
824
                /* fallthrough */
825
521k
        case TYPE:
826
521k
            if (flush == Z_BLOCK || flush == Z_TREES) goto inf_leave;
827
                /* fallthrough */
828
525k
        case TYPEDO:
829
525k
            if (state->last) {
830
128k
                BYTEBITS();
831
128k
                state->mode = CHECK;
832
128k
                break;
833
128k
            }
834
396k
            NEEDBITS(3);
835
390k
            state->last = BITS(1);
836
390k
            DROPBITS(1);
837
390k
            switch (BITS(2)) {
838
46.4k
            case 0:                             /* stored block */
839
46.4k
                Tracev((stderr, "inflate:     stored block%s\n",
840
46.4k
                        state->last ? " (last)" : ""));
841
46.4k
                state->mode = STORED;
842
46.4k
                break;
843
242k
            case 1:                             /* fixed block */
844
242k
                fixedtables(state);
845
242k
                Tracev((stderr, "inflate:     fixed codes block%s\n",
846
242k
                        state->last ? " (last)" : ""));
847
242k
                state->mode = LEN_;             /* decode codes */
848
242k
                if (flush == Z_TREES) {
849
0
                    DROPBITS(2);
850
0
                    goto inf_leave;
851
0
                }
852
242k
                break;
853
242k
            case 2:                             /* dynamic block */
854
100k
                Tracev((stderr, "inflate:     dynamic codes block%s\n",
855
100k
                        state->last ? " (last)" : ""));
856
100k
                state->mode = TABLE;
857
100k
                break;
858
1.24k
            case 3:
859
1.24k
                strm->msg = (z_const char *)"invalid block type";
860
1.24k
                state->mode = BAD;
861
390k
            }
862
390k
            DROPBITS(2);
863
390k
            break;
864
47.6k
        case STORED:
865
47.6k
            BYTEBITS();                         /* go to byte boundary */
866
47.6k
            NEEDBITS(32);
867
44.6k
            if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
868
6.08k
                strm->msg = (z_const char *)"invalid stored block lengths";
869
6.08k
                state->mode = BAD;
870
6.08k
                break;
871
6.08k
            }
872
38.5k
            state->length = (unsigned)hold & 0xffff;
873
38.5k
            Tracev((stderr, "inflate:       stored length %u\n",
874
38.5k
                    state->length));
875
38.5k
            INITBITS();
876
38.5k
            state->mode = COPY_;
877
38.5k
            if (flush == Z_TREES) goto inf_leave;
878
                /* fallthrough */
879
38.5k
        case COPY_:
880
38.5k
            state->mode = COPY;
881
                /* fallthrough */
882
91.6k
        case COPY:
883
91.6k
            copy = state->length;
884
91.6k
            if (copy) {
885
58.1k
                if (copy > have) copy = have;
886
58.1k
                if (copy > left) copy = left;
887
58.1k
                if (copy == 0) goto inf_leave;
888
37.8k
                zmemcpy(put, next, copy);
889
37.8k
                have -= copy;
890
37.8k
                next += copy;
891
37.8k
                left -= copy;
892
37.8k
                put += copy;
893
37.8k
                state->length -= copy;
894
37.8k
                break;
895
58.1k
            }
896
33.4k
            Tracev((stderr, "inflate:       stored end\n"));
897
33.4k
            state->mode = TYPE;
898
33.4k
            break;
899
102k
        case TABLE:
900
102k
            NEEDBITS(14);
901
98.8k
            state->nlen = BITS(5) + 257;
902
98.8k
            DROPBITS(5);
903
98.8k
            state->ndist = BITS(5) + 1;
904
98.8k
            DROPBITS(5);
905
98.8k
            state->ncode = BITS(4) + 4;
906
98.8k
            DROPBITS(4);
907
98.8k
#ifndef PKZIP_BUG_WORKAROUND
908
98.8k
            if (state->nlen > 286 || state->ndist > 30) {
909
1.89k
                strm->msg = (z_const char *)
910
1.89k
                    "too many length or distance symbols";
911
1.89k
                state->mode = BAD;
912
1.89k
                break;
913
1.89k
            }
914
96.9k
#endif
915
96.9k
            Tracev((stderr, "inflate:       table sizes ok\n"));
916
96.9k
            state->have = 0;
917
96.9k
            state->mode = LENLENS;
918
                /* fallthrough */
919
100k
        case LENLENS:
920
1.63M
            while (state->have < state->ncode) {
921
1.53M
                NEEDBITS(3);
922
1.53M
                state->lens[order[state->have++]] = (unsigned short)BITS(3);
923
1.53M
                DROPBITS(3);
924
1.53M
            }
925
394k
            while (state->have < 19)
926
298k
                state->lens[order[state->have++]] = 0;
927
96.0k
            state->next = state->codes;
928
96.0k
            state->lencode = state->distcode = (const code FAR *)(state->next);
929
96.0k
            state->lenbits = 7;
930
96.0k
            ret = inflate_table(CODES, state->lens, 19, &(state->next),
931
96.0k
                                &(state->lenbits), state->work);
932
96.0k
            if (ret) {
933
4.16k
                strm->msg = (z_const char *)"invalid code lengths set";
934
4.16k
                state->mode = BAD;
935
4.16k
                break;
936
4.16k
            }
937
91.9k
            Tracev((stderr, "inflate:       code lengths ok\n"));
938
91.9k
            state->have = 0;
939
91.9k
            state->mode = CODELENS;
940
                /* fallthrough */
941
107k
        case CODELENS:
942
5.43M
            while (state->have < state->nlen + state->ndist) {
943
7.08M
                for (;;) {
944
7.08M
                    here = state->lencode[BITS(state->lenbits)];
945
7.08M
                    if ((unsigned)(here.bits) <= bits) break;
946
1.75M
                    PULLBYTE();
947
1.75M
                }
948
5.33M
                if (here.val < 16) {
949
4.55M
                    DROPBITS(here.bits);
950
4.55M
                    state->lens[state->have++] = here.val;
951
4.55M
                }
952
779k
                else {
953
779k
                    if (here.val == 16) {
954
379k
                        NEEDBITS(here.bits + 2);
955
378k
                        DROPBITS(here.bits);
956
378k
                        if (state->have == 0) {
957
1.04k
                            strm->msg = (z_const char *)
958
1.04k
                                "invalid bit length repeat";
959
1.04k
                            state->mode = BAD;
960
1.04k
                            break;
961
1.04k
                        }
962
377k
                        len = state->lens[state->have - 1];
963
377k
                        copy = 3 + BITS(2);
964
377k
                        DROPBITS(2);
965
377k
                    }
966
400k
                    else if (here.val == 17) {
967
178k
                        NEEDBITS(here.bits + 3);
968
176k
                        DROPBITS(here.bits);
969
176k
                        len = 0;
970
176k
                        copy = 3 + BITS(3);
971
176k
                        DROPBITS(3);
972
176k
                    }
973
221k
                    else {
974
221k
                        NEEDBITS(here.bits + 7);
975
219k
                        DROPBITS(here.bits);
976
219k
                        len = 0;
977
219k
                        copy = 11 + BITS(7);
978
219k
                        DROPBITS(7);
979
219k
                    }
980
773k
                    if (state->have + copy > state->nlen + state->ndist) {
981
1.77k
                        strm->msg = (z_const char *)
982
1.77k
                            "invalid bit length repeat";
983
1.77k
                        state->mode = BAD;
984
1.77k
                        break;
985
1.77k
                    }
986
21.3M
                    while (copy--)
987
20.5M
                        state->lens[state->have++] = (unsigned short)len;
988
771k
                }
989
5.33M
            }
990
991
            /* handle error breaks in while */
992
88.1k
            if (state->mode == BAD) break;
993
994
            /* check for end-of-block code (better have one) */
995
85.2k
            if (state->lens[256] == 0) {
996
987
                strm->msg = (z_const char *)
997
987
                    "invalid code -- missing end-of-block";
998
987
                state->mode = BAD;
999
987
                break;
1000
987
            }
1001
1002
            /* build code tables -- note: do not change the lenbits or distbits
1003
               values here (9 and 6) without reading the comments in inftrees.h
1004
               concerning the ENOUGH constants, which depend on those values */
1005
84.2k
            state->next = state->codes;
1006
84.2k
            state->lencode = (const code FAR *)(state->next);
1007
84.2k
            state->lenbits = 9;
1008
84.2k
            ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
1009
84.2k
                                &(state->lenbits), state->work);
1010
84.2k
            if (ret) {
1011
2.18k
                strm->msg = (z_const char *)"invalid literal/lengths set";
1012
2.18k
                state->mode = BAD;
1013
2.18k
                break;
1014
2.18k
            }
1015
82.1k
            state->distcode = (const code FAR *)(state->next);
1016
82.1k
            state->distbits = 6;
1017
82.1k
            ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
1018
82.1k
                            &(state->next), &(state->distbits), state->work);
1019
82.1k
            if (ret) {
1020
2.47k
                strm->msg = (z_const char *)"invalid distances set";
1021
2.47k
                state->mode = BAD;
1022
2.47k
                break;
1023
2.47k
            }
1024
79.6k
            Tracev((stderr, "inflate:       codes ok\n"));
1025
79.6k
            state->mode = LEN_;
1026
79.6k
            if (flush == Z_TREES) goto inf_leave;
1027
                /* fallthrough */
1028
322k
        case LEN_:
1029
322k
            state->mode = LEN;
1030
                /* fallthrough */
1031
6.45M
        case LEN:
1032
6.45M
            if (have >= 6 && left >= 258) {
1033
726k
                RESTORE();
1034
726k
                inflate_fast(strm, out);
1035
726k
                LOAD();
1036
726k
                if (state->mode == TYPE)
1037
185k
                    state->back = -1;
1038
726k
                break;
1039
726k
            }
1040
5.73M
            state->back = 0;
1041
10.3M
            for (;;) {
1042
10.3M
                here = state->lencode[BITS(state->lenbits)];
1043
10.3M
                if ((unsigned)(here.bits) <= bits) break;
1044
4.70M
                PULLBYTE();
1045
4.70M
            }
1046
5.67M
            if (here.op && (here.op & 0xf0) == 0) {
1047
158k
                last = here;
1048
198k
                for (;;) {
1049
198k
                    here = state->lencode[last.val +
1050
198k
                            (BITS(last.bits + last.op) >> last.bits)];
1051
198k
                    if ((unsigned)(last.bits + here.bits) <= bits) break;
1052
40.8k
                    PULLBYTE();
1053
40.8k
                }
1054
158k
                DROPBITS(last.bits);
1055
158k
                state->back += last.bits;
1056
158k
            }
1057
5.67M
            DROPBITS(here.bits);
1058
5.67M
            state->back += here.bits;
1059
5.67M
            state->length = (unsigned)here.val;
1060
5.67M
            if ((int)(here.op) == 0) {
1061
4.23M
                Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
1062
4.23M
                        "inflate:         literal '%c'\n" :
1063
4.23M
                        "inflate:         literal 0x%02x\n", here.val));
1064
4.23M
                state->mode = LIT;
1065
4.23M
                break;
1066
4.23M
            }
1067
1.44M
            if (here.op & 32) {
1068
72.0k
                Tracevv((stderr, "inflate:         end of block\n"));
1069
72.0k
                state->back = -1;
1070
72.0k
                state->mode = TYPE;
1071
72.0k
                break;
1072
72.0k
            }
1073
1.37M
            if (here.op & 64) {
1074
3.09k
                strm->msg = (z_const char *)"invalid literal/length code";
1075
3.09k
                state->mode = BAD;
1076
3.09k
                break;
1077
3.09k
            }
1078
1.37M
            state->extra = (unsigned)(here.op) & 15;
1079
1.37M
            state->mode = LENEXT;
1080
                /* fallthrough */
1081
1.38M
        case LENEXT:
1082
1.38M
            if (state->extra) {
1083
439k
                NEEDBITS(state->extra);
1084
427k
                state->length += BITS(state->extra);
1085
427k
                DROPBITS(state->extra);
1086
427k
                state->back += state->extra;
1087
427k
            }
1088
1.37M
            Tracevv((stderr, "inflate:         length %u\n", state->length));
1089
1.37M
            state->was = state->length;
1090
1.37M
            state->mode = DIST;
1091
                /* fallthrough */
1092
1.39M
        case DIST:
1093
1.90M
            for (;;) {
1094
1.90M
                here = state->distcode[BITS(state->distbits)];
1095
1.90M
                if ((unsigned)(here.bits) <= bits) break;
1096
539k
                PULLBYTE();
1097
539k
            }
1098
1.36M
            if ((here.op & 0xf0) == 0) {
1099
51.3k
                last = here;
1100
75.6k
                for (;;) {
1101
75.6k
                    here = state->distcode[last.val +
1102
75.6k
                            (BITS(last.bits + last.op) >> last.bits)];
1103
75.6k
                    if ((unsigned)(last.bits + here.bits) <= bits) break;
1104
24.6k
                    PULLBYTE();
1105
24.6k
                }
1106
50.9k
                DROPBITS(last.bits);
1107
50.9k
                state->back += last.bits;
1108
50.9k
            }
1109
1.36M
            DROPBITS(here.bits);
1110
1.36M
            state->back += here.bits;
1111
1.36M
            if (here.op & 64) {
1112
1.61k
                strm->msg = (z_const char *)"invalid distance code";
1113
1.61k
                state->mode = BAD;
1114
1.61k
                break;
1115
1.61k
            }
1116
1.36M
            state->offset = (unsigned)here.val;
1117
1.36M
            state->extra = (unsigned)(here.op) & 15;
1118
1.36M
            state->mode = DISTEXT;
1119
                /* fallthrough */
1120
1.37M
        case DISTEXT:
1121
1.37M
            if (state->extra) {
1122
688k
                NEEDBITS(state->extra);
1123
670k
                state->offset += BITS(state->extra);
1124
670k
                DROPBITS(state->extra);
1125
670k
                state->back += state->extra;
1126
670k
            }
1127
#ifdef INFLATE_STRICT
1128
            if (state->offset > state->dmax) {
1129
                strm->msg = (z_const char *)"invalid distance too far back";
1130
                state->mode = BAD;
1131
                break;
1132
            }
1133
#endif
1134
1.36M
            Tracevv((stderr, "inflate:         distance %u\n", state->offset));
1135
1.36M
            state->mode = MATCH;
1136
                /* fallthrough */
1137
4.94M
        case MATCH:
1138
4.94M
            if (left == 0) goto inf_leave;
1139
3.56M
            copy = out - left;
1140
3.56M
            if (state->offset > copy) {         /* copy from window */
1141
1.66M
                copy = state->offset - copy;
1142
1.66M
                if (copy > state->whave) {
1143
5.70k
                    if (state->sane) {
1144
5.70k
                        strm->msg = (z_const char *)
1145
5.70k
                            "invalid distance too far back";
1146
5.70k
                        state->mode = BAD;
1147
5.70k
                        break;
1148
5.70k
                    }
1149
#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
1150
                    Trace((stderr, "inflate.c too far\n"));
1151
                    copy -= state->whave;
1152
                    if (copy > state->length) copy = state->length;
1153
                    if (copy > left) copy = left;
1154
                    left -= copy;
1155
                    state->length -= copy;
1156
                    do {
1157
                        *put++ = 0;
1158
                    } while (--copy);
1159
                    if (state->length == 0) state->mode = LEN;
1160
                    break;
1161
#endif
1162
5.70k
                }
1163
1.65M
                if (copy > state->wnext) {
1164
191k
                    copy -= state->wnext;
1165
191k
                    from = state->window + (state->wsize - copy);
1166
191k
                }
1167
1.46M
                else
1168
1.46M
                    from = state->window + (state->wnext - copy);
1169
1.65M
                if (copy > state->length) copy = state->length;
1170
1.65M
            }
1171
1.89M
            else {                              /* copy from output */
1172
1.89M
                from = put - state->offset;
1173
1.89M
                copy = state->length;
1174
1.89M
            }
1175
3.55M
            if (copy > left) copy = left;
1176
3.55M
            left -= copy;
1177
3.55M
            state->length -= copy;
1178
181M
            do {
1179
181M
                *put++ = *from++;
1180
181M
            } while (--copy);
1181
3.55M
            if (state->length == 0) state->mode = LEN;
1182
3.55M
            break;
1183
4.27M
        case LIT:
1184
4.27M
            if (left == 0) goto inf_leave;
1185
4.22M
            *put++ = (unsigned char)(state->length);
1186
4.22M
            left--;
1187
4.22M
            state->mode = LEN;
1188
4.22M
            break;
1189
130k
        case CHECK:
1190
130k
            if (state->wrap) {
1191
130k
                NEEDBITS(32);
1192
125k
                out -= left;
1193
125k
                strm->total_out += out;
1194
125k
                state->total += out;
1195
125k
                if ((state->wrap & 4) && out)
1196
53.2k
                    strm->adler = state->check =
1197
53.2k
                        UPDATE_CHECK(state->check, put - out, out);
1198
125k
                out = left;
1199
125k
                if ((state->wrap & 4) && (
1200
125k
#ifdef GUNZIP
1201
125k
                     state->flags ? hold :
1202
125k
#endif
1203
125k
                     ZSWAP32(hold)) != state->check) {
1204
24.7k
                    strm->msg = (z_const char *)"incorrect data check";
1205
24.7k
                    state->mode = BAD;
1206
24.7k
                    break;
1207
24.7k
                }
1208
100k
                INITBITS();
1209
100k
                Tracev((stderr, "inflate:   check matches trailer\n"));
1210
100k
            }
1211
101k
#ifdef GUNZIP
1212
101k
            state->mode = LENGTH;
1213
                /* fallthrough */
1214
101k
        case LENGTH:
1215
101k
            if (state->wrap && state->flags) {
1216
75.1k
                NEEDBITS(32);
1217
74.9k
                if ((state->wrap & 4) && hold != (state->total & 0xffffffff)) {
1218
960
                    strm->msg = (z_const char *)"incorrect length check";
1219
960
                    state->mode = BAD;
1220
960
                    break;
1221
960
                }
1222
73.9k
                INITBITS();
1223
73.9k
                Tracev((stderr, "inflate:   length matches trailer\n"));
1224
73.9k
            }
1225
99.8k
#endif
1226
99.8k
            state->mode = DONE;
1227
                /* fallthrough */
1228
102k
        case DONE:
1229
102k
            ret = Z_STREAM_END;
1230
102k
            goto inf_leave;
1231
69.4k
        case BAD:
1232
69.4k
            ret = Z_DATA_ERROR;
1233
69.4k
            goto inf_leave;
1234
0
        case MEM:
1235
0
            return Z_MEM_ERROR;
1236
0
        case SYNC:
1237
                /* fallthrough */
1238
0
        default:
1239
0
            return Z_STREAM_ERROR;
1240
15.5M
        }
1241
1242
    /*
1243
       Return from inflate(), updating the total counts and the check value.
1244
       If there was no progress during the inflate() call, return a buffer
1245
       error.  Call updatewindow() to create and/or update the window state.
1246
       Note: a memory error from inflate() is non-recoverable.
1247
     */
1248
1.77M
  inf_leave:
1249
1.77M
    RESTORE();
1250
1.77M
    if (state->wsize || (out != strm->avail_out && state->mode < BAD &&
1251
105k
            (state->mode < CHECK || flush != Z_FINISH)))
1252
1.62M
        if (updatewindow(strm, strm->next_out, out - strm->avail_out)) {
1253
0
            state->mode = MEM;
1254
0
            return Z_MEM_ERROR;
1255
0
        }
1256
1.77M
    in -= strm->avail_in;
1257
1.77M
    out -= strm->avail_out;
1258
1.77M
    strm->total_in += in;
1259
1.77M
    strm->total_out += out;
1260
1.77M
    state->total += out;
1261
1.77M
    if ((state->wrap & 4) && out)
1262
1.56M
        strm->adler = state->check =
1263
1.56M
            UPDATE_CHECK(state->check, strm->next_out - out, out);
1264
1.77M
    strm->data_type = (int)state->bits + (state->last ? 64 : 0) +
1265
1.77M
                      (state->mode == TYPE ? 128 : 0) +
1266
1.77M
                      (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0);
1267
1.77M
    if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
1268
23.0k
        ret = Z_BUF_ERROR;
1269
1.77M
    return ret;
1270
1.77M
}
1271
1272
143k
int ZEXPORT inflateEnd(z_streamp strm) {
1273
143k
    struct inflate_state FAR *state;
1274
143k
    if (inflateStateCheck(strm))
1275
30.6k
        return Z_STREAM_ERROR;
1276
112k
    state = (struct inflate_state FAR *)strm->state;
1277
112k
    if (state->window != Z_NULL) ZFREE(strm, state->window);
1278
112k
    ZFREE(strm, strm->state);
1279
112k
    strm->state = Z_NULL;
1280
112k
    Tracev((stderr, "inflate: end\n"));
1281
112k
    return Z_OK;
1282
143k
}
1283
1284
int ZEXPORT inflateGetDictionary(z_streamp strm, Bytef *dictionary,
1285
0
                                 uInt *dictLength) {
1286
0
    struct inflate_state FAR *state;
1287
1288
    /* check state */
1289
0
    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1290
0
    state = (struct inflate_state FAR *)strm->state;
1291
1292
    /* copy dictionary */
1293
0
    if (state->whave && dictionary != Z_NULL) {
1294
0
        zmemcpy(dictionary, state->window + state->wnext,
1295
0
                state->whave - state->wnext);
1296
0
        zmemcpy(dictionary + state->whave - state->wnext,
1297
0
                state->window, state->wnext);
1298
0
    }
1299
0
    if (dictLength != Z_NULL)
1300
0
        *dictLength = state->whave;
1301
0
    return Z_OK;
1302
0
}
1303
1304
int ZEXPORT inflateSetDictionary(z_streamp strm, const Bytef *dictionary,
1305
0
                                 uInt dictLength) {
1306
0
    struct inflate_state FAR *state;
1307
0
    unsigned long dictid;
1308
0
    int ret;
1309
1310
    /* check state */
1311
0
    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1312
0
    state = (struct inflate_state FAR *)strm->state;
1313
0
    if (state->wrap != 0 && state->mode != DICT)
1314
0
        return Z_STREAM_ERROR;
1315
1316
    /* check for correct dictionary identifier */
1317
0
    if (state->mode == DICT) {
1318
0
        dictid = adler32(0L, Z_NULL, 0);
1319
0
        dictid = adler32(dictid, dictionary, dictLength);
1320
0
        if (dictid != state->check)
1321
0
            return Z_DATA_ERROR;
1322
0
    }
1323
1324
    /* copy dictionary to window using updatewindow(), which will amend the
1325
       existing dictionary if appropriate */
1326
0
    ret = updatewindow(strm, dictionary + dictLength, dictLength);
1327
0
    if (ret) {
1328
0
        state->mode = MEM;
1329
0
        return Z_MEM_ERROR;
1330
0
    }
1331
0
    state->havedict = 1;
1332
0
    Tracev((stderr, "inflate:   dictionary set\n"));
1333
0
    return Z_OK;
1334
0
}
1335
1336
0
int ZEXPORT inflateGetHeader(z_streamp strm, gz_headerp head) {
1337
0
    struct inflate_state FAR *state;
1338
1339
    /* check state */
1340
0
    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1341
0
    state = (struct inflate_state FAR *)strm->state;
1342
0
    if ((state->wrap & 2) == 0) return Z_STREAM_ERROR;
1343
1344
    /* save header structure */
1345
0
    state->head = head;
1346
0
    head->done = 0;
1347
0
    return Z_OK;
1348
0
}
1349
1350
/*
1351
   Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff.  Return when found
1352
   or when out of input.  When called, *have is the number of pattern bytes
1353
   found in order so far, in 0..3.  On return *have is updated to the new
1354
   state.  If on return *have equals four, then the pattern was found and the
1355
   return value is how many bytes were read including the last byte of the
1356
   pattern.  If *have is less than four, then the pattern has not been found
1357
   yet and the return value is len.  In the latter case, syncsearch() can be
1358
   called again with more data and the *have state.  *have is initialized to
1359
   zero for the first call.
1360
 */
1361
local unsigned syncsearch(unsigned FAR *have, const unsigned char FAR *buf,
1362
0
                          unsigned len) {
1363
0
    unsigned got;
1364
0
    unsigned next;
1365
1366
0
    got = *have;
1367
0
    next = 0;
1368
0
    while (next < len && got < 4) {
1369
0
        if ((int)(buf[next]) == (got < 2 ? 0 : 0xff))
1370
0
            got++;
1371
0
        else if (buf[next])
1372
0
            got = 0;
1373
0
        else
1374
0
            got = 4 - got;
1375
0
        next++;
1376
0
    }
1377
0
    *have = got;
1378
0
    return next;
1379
0
}
1380
1381
0
int ZEXPORT inflateSync(z_streamp strm) {
1382
0
    unsigned len;               /* number of bytes to look at or looked at */
1383
0
    int flags;                  /* temporary to save header status */
1384
0
    unsigned long in, out;      /* temporary to save total_in and total_out */
1385
0
    unsigned char buf[4];       /* to restore bit buffer to byte string */
1386
0
    struct inflate_state FAR *state;
1387
1388
    /* check parameters */
1389
0
    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1390
0
    state = (struct inflate_state FAR *)strm->state;
1391
0
    if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR;
1392
1393
    /* if first time, start search in bit buffer */
1394
0
    if (state->mode != SYNC) {
1395
0
        state->mode = SYNC;
1396
0
        state->hold >>= state->bits & 7;
1397
0
        state->bits -= state->bits & 7;
1398
0
        len = 0;
1399
0
        while (state->bits >= 8) {
1400
0
            buf[len++] = (unsigned char)(state->hold);
1401
0
            state->hold >>= 8;
1402
0
            state->bits -= 8;
1403
0
        }
1404
0
        state->have = 0;
1405
0
        syncsearch(&(state->have), buf, len);
1406
0
    }
1407
1408
    /* search available input */
1409
0
    len = syncsearch(&(state->have), strm->next_in, strm->avail_in);
1410
0
    strm->avail_in -= len;
1411
0
    strm->next_in += len;
1412
0
    strm->total_in += len;
1413
1414
    /* return no joy or set up to restart inflate() on a new block */
1415
0
    if (state->have != 4) return Z_DATA_ERROR;
1416
0
    if (state->flags == -1)
1417
0
        state->wrap = 0;    /* if no header yet, treat as raw */
1418
0
    else
1419
0
        state->wrap &= ~4;  /* no point in computing a check value now */
1420
0
    flags = state->flags;
1421
0
    in = strm->total_in;  out = strm->total_out;
1422
0
    inflateReset(strm);
1423
0
    strm->total_in = in;  strm->total_out = out;
1424
0
    state->flags = flags;
1425
0
    state->mode = TYPE;
1426
0
    return Z_OK;
1427
0
}
1428
1429
/*
1430
   Returns true if inflate is currently at the end of a block generated by
1431
   Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
1432
   implementation to provide an additional safety check. PPP uses
1433
   Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored
1434
   block. When decompressing, PPP checks that at the end of input packet,
1435
   inflate is waiting for these length bytes.
1436
 */
1437
0
int ZEXPORT inflateSyncPoint(z_streamp strm) {
1438
0
    struct inflate_state FAR *state;
1439
1440
0
    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1441
0
    state = (struct inflate_state FAR *)strm->state;
1442
0
    return state->mode == STORED && state->bits == 0;
1443
0
}
1444
1445
0
int ZEXPORT inflateCopy(z_streamp dest, z_streamp source) {
1446
0
    struct inflate_state FAR *state;
1447
0
    struct inflate_state FAR *copy;
1448
0
    unsigned char FAR *window;
1449
0
    unsigned wsize;
1450
1451
    /* check input */
1452
0
    if (inflateStateCheck(source) || dest == Z_NULL)
1453
0
        return Z_STREAM_ERROR;
1454
0
    state = (struct inflate_state FAR *)source->state;
1455
1456
    /* allocate space */
1457
0
    copy = (struct inflate_state FAR *)
1458
0
           ZALLOC(source, 1, sizeof(struct inflate_state));
1459
0
    if (copy == Z_NULL) return Z_MEM_ERROR;
1460
0
    window = Z_NULL;
1461
0
    if (state->window != Z_NULL) {
1462
0
        window = (unsigned char FAR *)
1463
0
                 ZALLOC(source, 1U << state->wbits, sizeof(unsigned char));
1464
0
        if (window == Z_NULL) {
1465
0
            ZFREE(source, copy);
1466
0
            return Z_MEM_ERROR;
1467
0
        }
1468
0
    }
1469
1470
    /* copy state */
1471
0
    zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));
1472
0
    zmemcpy((voidpf)copy, (voidpf)state, sizeof(struct inflate_state));
1473
0
    copy->strm = dest;
1474
0
    if (state->lencode >= state->codes &&
1475
0
        state->lencode <= state->codes + ENOUGH - 1) {
1476
0
        copy->lencode = copy->codes + (state->lencode - state->codes);
1477
0
        copy->distcode = copy->codes + (state->distcode - state->codes);
1478
0
    }
1479
0
    copy->next = copy->codes + (state->next - state->codes);
1480
0
    if (window != Z_NULL) {
1481
0
        wsize = 1U << state->wbits;
1482
0
        zmemcpy(window, state->window, wsize);
1483
0
    }
1484
0
    copy->window = window;
1485
0
    dest->state = (struct internal_state FAR *)copy;
1486
0
    return Z_OK;
1487
0
}
1488
1489
0
int ZEXPORT inflateUndermine(z_streamp strm, int subvert) {
1490
0
    struct inflate_state FAR *state;
1491
1492
0
    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1493
0
    state = (struct inflate_state FAR *)strm->state;
1494
#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
1495
    state->sane = !subvert;
1496
    return Z_OK;
1497
#else
1498
0
    (void)subvert;
1499
0
    state->sane = 1;
1500
0
    return Z_DATA_ERROR;
1501
0
#endif
1502
0
}
1503
1504
0
int ZEXPORT inflateValidate(z_streamp strm, int check) {
1505
0
    struct inflate_state FAR *state;
1506
1507
0
    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1508
0
    state = (struct inflate_state FAR *)strm->state;
1509
0
    if (check && state->wrap)
1510
0
        state->wrap |= 4;
1511
0
    else
1512
0
        state->wrap &= ~4;
1513
0
    return Z_OK;
1514
0
}
1515
1516
0
long ZEXPORT inflateMark(z_streamp strm) {
1517
0
    struct inflate_state FAR *state;
1518
1519
0
    if (inflateStateCheck(strm))
1520
0
        return -(1L << 16);
1521
0
    state = (struct inflate_state FAR *)strm->state;
1522
0
    return (long)(((unsigned long)((long)state->back)) << 16) +
1523
0
        (state->mode == COPY ? state->length :
1524
0
            (state->mode == MATCH ? state->was - state->length : 0));
1525
0
}
1526
1527
0
unsigned long ZEXPORT inflateCodesUsed(z_streamp strm) {
1528
0
    struct inflate_state FAR *state;
1529
0
    if (inflateStateCheck(strm)) return (unsigned long)-1;
1530
0
    state = (struct inflate_state FAR *)strm->state;
1531
0
    return (unsigned long)(state->next - state->codes);
1532
0
}