Coverage Report

Created: 2026-03-31 07:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opencv/3rdparty/zlib/inflate.c
Line
Count
Source
1
/* inflate.c -- zlib decompression
2
 * Copyright (C) 1995-2022 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
469k
local int inflateStateCheck(z_streamp strm) {
95
469k
    struct inflate_state FAR *state;
96
469k
    if (strm == Z_NULL ||
97
469k
        strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0)
98
0
        return 1;
99
469k
    state = (struct inflate_state FAR *)strm->state;
100
469k
    if (state == Z_NULL || state->strm != strm ||
101
469k
        state->mode < HEAD || state->mode > SYNC)
102
54
        return 1;
103
469k
    return 0;
104
469k
}
105
106
130k
int ZEXPORT inflateResetKeep(z_streamp strm) {
107
130k
    struct inflate_state FAR *state;
108
109
130k
    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
110
130k
    state = (struct inflate_state FAR *)strm->state;
111
130k
    strm->total_in = strm->total_out = state->total = 0;
112
130k
    strm->msg = Z_NULL;
113
130k
    if (state->wrap)        /* to support ill-conceived Java test suite */
114
130k
        strm->adler = state->wrap & 1;
115
130k
    state->mode = HEAD;
116
130k
    state->last = 0;
117
130k
    state->havedict = 0;
118
130k
    state->flags = -1;
119
130k
    state->dmax = 32768U;
120
130k
    state->head = Z_NULL;
121
130k
    state->hold = 0;
122
130k
    state->bits = 0;
123
130k
    state->lencode = state->distcode = state->next = state->codes;
124
130k
    state->sane = 1;
125
130k
    state->back = -1;
126
130k
    Tracev((stderr, "inflate: reset\n"));
127
130k
    return Z_OK;
128
130k
}
129
130
130k
int ZEXPORT inflateReset(z_streamp strm) {
131
130k
    struct inflate_state FAR *state;
132
133
130k
    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
134
130k
    state = (struct inflate_state FAR *)strm->state;
135
130k
    state->wsize = 0;
136
130k
    state->whave = 0;
137
130k
    state->wnext = 0;
138
130k
    return inflateResetKeep(strm);
139
130k
}
140
141
4.91k
int ZEXPORT inflateReset2(z_streamp strm, int windowBits) {
142
4.91k
    int wrap;
143
4.91k
    struct inflate_state FAR *state;
144
145
    /* get the state */
146
4.91k
    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
147
4.91k
    state = (struct inflate_state FAR *)strm->state;
148
149
    /* extract wrap request from windowBits parameter */
150
4.91k
    if (windowBits < 0) {
151
0
        if (windowBits < -15)
152
0
            return Z_STREAM_ERROR;
153
0
        wrap = 0;
154
0
        windowBits = -windowBits;
155
0
    }
156
4.91k
    else {
157
4.91k
        wrap = (windowBits >> 4) + 5;
158
4.91k
#ifdef GUNZIP
159
4.91k
        if (windowBits < 48)
160
4.91k
            windowBits &= 15;
161
4.91k
#endif
162
4.91k
    }
163
164
    /* set number of window bits, free window if different */
165
4.91k
    if (windowBits && (windowBits < 8 || windowBits > 15))
166
0
        return Z_STREAM_ERROR;
167
4.91k
    if (state->window != Z_NULL && state->wbits != (unsigned)windowBits) {
168
353
        ZFREE(strm, state->window);
169
353
        state->window = Z_NULL;
170
353
    }
171
172
    /* update state and reset the rest of it */
173
4.91k
    state->wrap = wrap;
174
4.91k
    state->wbits = (unsigned)windowBits;
175
4.91k
    return inflateReset(strm);
176
4.91k
}
177
178
int ZEXPORT inflateInit2_(z_streamp strm, int windowBits,
179
555
                          const char *version, int stream_size) {
180
555
    int ret;
181
555
    struct inflate_state FAR *state;
182
183
555
    if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
184
555
        stream_size != (int)(sizeof(z_stream)))
185
0
        return Z_VERSION_ERROR;
186
555
    if (strm == Z_NULL) return Z_STREAM_ERROR;
187
555
    strm->msg = Z_NULL;                 /* in case we return an error */
188
555
    if (strm->zalloc == (alloc_func)0) {
189
#ifdef Z_SOLO
190
        return Z_STREAM_ERROR;
191
#else
192
0
        strm->zalloc = zcalloc;
193
0
        strm->opaque = (voidpf)0;
194
0
#endif
195
0
    }
196
555
    if (strm->zfree == (free_func)0)
197
#ifdef Z_SOLO
198
        return Z_STREAM_ERROR;
199
#else
200
0
        strm->zfree = zcfree;
201
555
#endif
202
555
    state = (struct inflate_state FAR *)
203
555
            ZALLOC(strm, 1, sizeof(struct inflate_state));
204
555
    if (state == Z_NULL) return Z_MEM_ERROR;
205
555
    Tracev((stderr, "inflate: allocated\n"));
206
555
    strm->state = (struct internal_state FAR *)state;
207
555
    state->strm = strm;
208
555
    state->window = Z_NULL;
209
555
    state->mode = HEAD;     /* to pass state test in inflateReset2() */
210
555
    state->check = 1L;      /* 1L is the result of adler32() zero length data */
211
555
    ret = inflateReset2(strm, windowBits);
212
555
    if (ret != Z_OK) {
213
0
        ZFREE(strm, state);
214
0
        strm->state = Z_NULL;
215
0
    }
216
555
    return ret;
217
555
}
218
219
int ZEXPORT inflateInit_(z_streamp strm, const char *version,
220
224
                         int stream_size) {
221
224
    return inflateInit2_(strm, DEF_WBITS, version, stream_size);
222
224
}
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)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
2.19k
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
2.19k
#   include "inffixed.h"
287
2.19k
#endif /* BUILDFIXED */
288
2.19k
    state->lencode = lenfix;
289
2.19k
    state->lenbits = 9;
290
2.19k
    state->distcode = distfix;
291
2.19k
    state->distbits = 5;
292
2.19k
}
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
80.0k
local int updatewindow(z_streamp strm, const Bytef *end, unsigned copy) {
370
80.0k
    struct inflate_state FAR *state;
371
80.0k
    unsigned dist;
372
373
80.0k
    state = (struct inflate_state FAR *)strm->state;
374
375
    /* if it hasn't been done already, allocate space for the window */
376
80.0k
    if (state->window == Z_NULL) {
377
657
        state->window = (unsigned char FAR *)
378
657
                        ZALLOC(strm, 1U << state->wbits,
379
657
                               sizeof(unsigned char));
380
657
        if (state->window == Z_NULL) return 1;
381
657
    }
382
383
    /* if window not in use yet, initialize */
384
80.0k
    if (state->wsize == 0) {
385
15.0k
        state->wsize = 1U << state->wbits;
386
15.0k
        state->wnext = 0;
387
15.0k
        state->whave = 0;
388
15.0k
    }
389
390
    /* copy state->wsize or less output bytes into the circular window */
391
80.0k
    if (copy >= state->wsize) {
392
69
        zmemcpy(state->window, end - state->wsize, state->wsize);
393
69
        state->wnext = 0;
394
69
        state->whave = state->wsize;
395
69
    }
396
80.0k
    else {
397
80.0k
        dist = state->wsize - state->wnext;
398
80.0k
        if (dist > copy) dist = copy;
399
80.0k
        zmemcpy(state->window + state->wnext, end - copy, dist);
400
80.0k
        copy -= dist;
401
80.0k
        if (copy) {
402
2.47k
            zmemcpy(state->window, end - copy, copy);
403
2.47k
            state->wnext = copy;
404
2.47k
            state->whave = state->wsize;
405
2.47k
        }
406
77.5k
        else {
407
77.5k
            state->wnext += dist;
408
77.5k
            if (state->wnext == state->wsize) state->wnext = 0;
409
77.5k
            if (state->whave < state->wsize) state->whave += dist;
410
77.5k
        }
411
80.0k
    }
412
80.0k
    return 0;
413
80.0k
}
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
80.0k
    (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
0
    do { \
429
0
        hbuf[0] = (unsigned char)(word); \
430
0
        hbuf[1] = (unsigned char)((word) >> 8); \
431
0
        check = crc32(check, hbuf, 2); \
432
0
    } while (0)
433
434
#  define CRC4(check, word) \
435
0
    do { \
436
0
        hbuf[0] = (unsigned char)(word); \
437
0
        hbuf[1] = (unsigned char)((word) >> 8); \
438
0
        hbuf[2] = (unsigned char)((word) >> 16); \
439
0
        hbuf[3] = (unsigned char)((word) >> 24); \
440
0
        check = crc32(check, hbuf, 4); \
441
0
    } while (0)
442
#endif
443
444
/* Load registers with state in inflate() for speed */
445
#define LOAD() \
446
236k
    do { \
447
236k
        put = strm->next_out; \
448
236k
        left = strm->avail_out; \
449
236k
        next = strm->next_in; \
450
236k
        have = strm->avail_in; \
451
236k
        hold = state->hold; \
452
236k
        bits = state->bits; \
453
236k
    } while (0)
454
455
/* Restore state from registers in inflate() */
456
#define RESTORE() \
457
236k
    do { \
458
236k
        strm->next_out = put; \
459
236k
        strm->avail_out = left; \
460
236k
        strm->next_in = next; \
461
236k
        strm->avail_in = have; \
462
236k
        state->hold = hold; \
463
236k
        state->bits = bits; \
464
236k
    } while (0)
465
466
/* Clear the input bit accumulator */
467
#define INITBITS() \
468
31.0k
    do { \
469
31.0k
        hold = 0; \
470
31.0k
        bits = 0; \
471
31.0k
    } 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
2.31M
    do { \
477
2.31M
        if (have == 0) goto inf_leave; \
478
2.31M
        have--; \
479
2.29M
        hold += (unsigned long)(*next++) << bits; \
480
2.29M
        bits += 8; \
481
2.29M
    } 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
979k
    do { \
487
1.72M
        while (bits < (unsigned)(n)) \
488
979k
            PULLBYTE(); \
489
979k
    } while (0)
490
491
/* Return the low n bits of the bit accumulator (n < 16) */
492
#define BITS(n) \
493
5.20M
    ((unsigned)hold & ((1U << (n)) - 1))
494
495
/* Remove n bits from the bit accumulator */
496
#define DROPBITS(n) \
497
3.48M
    do { \
498
3.48M
        hold >>= (n); \
499
3.48M
        bits -= (unsigned)(n); \
500
3.48M
    } while (0)
501
502
/* Remove zero to seven bits as needed to go to a byte boundary */
503
#define BYTEBITS() \
504
1.24k
    do { \
505
1.24k
        hold >>= bits & 7; \
506
1.24k
        bits -= bits & 7; \
507
1.24k
    } 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
203k
int ZEXPORT inflate(z_streamp strm, int flush) {
592
203k
    struct inflate_state FAR *state;
593
203k
    z_const unsigned char FAR *next;    /* next input */
594
203k
    unsigned char FAR *put;     /* next output */
595
203k
    unsigned have, left;        /* available input and output */
596
203k
    unsigned long hold;         /* bit buffer */
597
203k
    unsigned bits;              /* bits in bit buffer */
598
203k
    unsigned in, out;           /* save starting available input and output */
599
203k
    unsigned copy;              /* number of stored or match bytes to copy */
600
203k
    unsigned char FAR *from;    /* where to copy match bytes from */
601
203k
    code here;                  /* current decoding table entry */
602
203k
    code last;                  /* parent table entry */
603
203k
    unsigned len;               /* length to copy for repeats, bits to drop */
604
203k
    int ret;                    /* return code */
605
203k
#ifdef GUNZIP
606
203k
    unsigned char hbuf[4];      /* buffer for gzip header crc calculation */
607
203k
#endif
608
203k
    static const unsigned short order[19] = /* permutation of code lengths */
609
203k
        {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
610
611
203k
    if (inflateStateCheck(strm) || strm->next_out == Z_NULL ||
612
203k
        (strm->next_in == Z_NULL && strm->avail_in != 0))
613
0
        return Z_STREAM_ERROR;
614
615
203k
    state = (struct inflate_state FAR *)strm->state;
616
203k
    if (state->mode == TYPE) state->mode = TYPEDO;      /* skip check */
617
203k
    LOAD();
618
203k
    in = have;
619
203k
    out = left;
620
203k
    ret = Z_OK;
621
203k
    for (;;)
622
2.84M
        switch (state->mode) {
623
136k
        case HEAD:
624
136k
            if (state->wrap == 0) {
625
0
                state->mode = TYPEDO;
626
0
                break;
627
0
            }
628
136k
            NEEDBITS(16);
629
123k
#ifdef GUNZIP
630
123k
            if ((state->wrap & 2) && hold == 0x8b1f) {  /* gzip header */
631
0
                if (state->wbits == 0)
632
0
                    state->wbits = 15;
633
0
                state->check = crc32(0L, Z_NULL, 0);
634
0
                CRC2(state->check, hold);
635
0
                INITBITS();
636
0
                state->mode = FLAGS;
637
0
                break;
638
0
            }
639
123k
            if (state->head != Z_NULL)
640
0
                state->head->done = -1;
641
123k
            if (!(state->wrap & 1) ||   /* check if zlib header allowed */
642
#else
643
            if (
644
#endif
645
123k
                ((BITS(8) << 8) + (hold >> 8)) % 31) {
646
86.7k
                strm->msg = (char *)"incorrect header check";
647
86.7k
                state->mode = BAD;
648
86.7k
                break;
649
86.7k
            }
650
37.2k
            if (BITS(4) != Z_DEFLATED) {
651
9.03k
                strm->msg = (char *)"unknown compression method";
652
9.03k
                state->mode = BAD;
653
9.03k
                break;
654
9.03k
            }
655
28.2k
            DROPBITS(4);
656
28.2k
            len = BITS(4) + 8;
657
28.2k
            if (state->wbits == 0)
658
987
                state->wbits = len;
659
28.2k
            if (len > 15 || len > state->wbits) {
660
1.01k
                strm->msg = (char *)"invalid window size";
661
1.01k
                state->mode = BAD;
662
1.01k
                break;
663
1.01k
            }
664
27.1k
            state->dmax = 1U << len;
665
27.1k
            state->flags = 0;               /* indicate zlib header */
666
27.1k
            Tracev((stderr, "inflate:   zlib header ok\n"));
667
27.1k
            strm->adler = state->check = adler32(0L, Z_NULL, 0);
668
27.1k
            state->mode = hold & 0x200 ? DICTID : TYPE;
669
27.1k
            INITBITS();
670
27.1k
            break;
671
0
#ifdef GUNZIP
672
0
        case FLAGS:
673
0
            NEEDBITS(16);
674
0
            state->flags = (int)(hold);
675
0
            if ((state->flags & 0xff) != Z_DEFLATED) {
676
0
                strm->msg = (char *)"unknown compression method";
677
0
                state->mode = BAD;
678
0
                break;
679
0
            }
680
0
            if (state->flags & 0xe000) {
681
0
                strm->msg = (char *)"unknown header flags set";
682
0
                state->mode = BAD;
683
0
                break;
684
0
            }
685
0
            if (state->head != Z_NULL)
686
0
                state->head->text = (int)((hold >> 8) & 1);
687
0
            if ((state->flags & 0x0200) && (state->wrap & 4))
688
0
                CRC2(state->check, hold);
689
0
            INITBITS();
690
0
            state->mode = TIME;
691
                /* fallthrough */
692
0
        case TIME:
693
0
            NEEDBITS(32);
694
0
            if (state->head != Z_NULL)
695
0
                state->head->time = hold;
696
0
            if ((state->flags & 0x0200) && (state->wrap & 4))
697
0
                CRC4(state->check, hold);
698
0
            INITBITS();
699
0
            state->mode = OS;
700
                /* fallthrough */
701
0
        case OS:
702
0
            NEEDBITS(16);
703
0
            if (state->head != Z_NULL) {
704
0
                state->head->xflags = (int)(hold & 0xff);
705
0
                state->head->os = (int)(hold >> 8);
706
0
            }
707
0
            if ((state->flags & 0x0200) && (state->wrap & 4))
708
0
                CRC2(state->check, hold);
709
0
            INITBITS();
710
0
            state->mode = EXLEN;
711
                /* fallthrough */
712
0
        case EXLEN:
713
0
            if (state->flags & 0x0400) {
714
0
                NEEDBITS(16);
715
0
                state->length = (unsigned)(hold);
716
0
                if (state->head != Z_NULL)
717
0
                    state->head->extra_len = (unsigned)hold;
718
0
                if ((state->flags & 0x0200) && (state->wrap & 4))
719
0
                    CRC2(state->check, hold);
720
0
                INITBITS();
721
0
            }
722
0
            else if (state->head != Z_NULL)
723
0
                state->head->extra = Z_NULL;
724
0
            state->mode = EXTRA;
725
                /* fallthrough */
726
0
        case EXTRA:
727
0
            if (state->flags & 0x0400) {
728
0
                copy = state->length;
729
0
                if (copy > have) copy = have;
730
0
                if (copy) {
731
0
                    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
0
                    if ((state->flags & 0x0200) && (state->wrap & 4))
740
0
                        state->check = crc32(state->check, next, copy);
741
0
                    have -= copy;
742
0
                    next += copy;
743
0
                    state->length -= copy;
744
0
                }
745
0
                if (state->length) goto inf_leave;
746
0
            }
747
0
            state->length = 0;
748
0
            state->mode = NAME;
749
                /* fallthrough */
750
0
        case NAME:
751
0
            if (state->flags & 0x0800) {
752
0
                if (have == 0) goto inf_leave;
753
0
                copy = 0;
754
0
                do {
755
0
                    len = (unsigned)(next[copy++]);
756
0
                    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
0
                } while (len && copy < have);
761
0
                if ((state->flags & 0x0200) && (state->wrap & 4))
762
0
                    state->check = crc32(state->check, next, copy);
763
0
                have -= copy;
764
0
                next += copy;
765
0
                if (len) goto inf_leave;
766
0
            }
767
0
            else if (state->head != Z_NULL)
768
0
                state->head->name = Z_NULL;
769
0
            state->length = 0;
770
0
            state->mode = COMMENT;
771
                /* fallthrough */
772
0
        case COMMENT:
773
0
            if (state->flags & 0x1000) {
774
0
                if (have == 0) goto inf_leave;
775
0
                copy = 0;
776
0
                do {
777
0
                    len = (unsigned)(next[copy++]);
778
0
                    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
0
                } while (len && copy < have);
783
0
                if ((state->flags & 0x0200) && (state->wrap & 4))
784
0
                    state->check = crc32(state->check, next, copy);
785
0
                have -= copy;
786
0
                next += copy;
787
0
                if (len) goto inf_leave;
788
0
            }
789
0
            else if (state->head != Z_NULL)
790
0
                state->head->comment = Z_NULL;
791
0
            state->mode = HCRC;
792
                /* fallthrough */
793
0
        case HCRC:
794
0
            if (state->flags & 0x0200) {
795
0
                NEEDBITS(16);
796
0
                if ((state->wrap & 4) && hold != (state->check & 0xffff)) {
797
0
                    strm->msg = (char *)"header crc mismatch";
798
0
                    state->mode = BAD;
799
0
                    break;
800
0
                }
801
0
                INITBITS();
802
0
            }
803
0
            if (state->head != Z_NULL) {
804
0
                state->head->hcrc = (int)((state->flags >> 9) & 1);
805
0
                state->head->done = 1;
806
0
            }
807
0
            strm->adler = state->check = crc32(0L, Z_NULL, 0);
808
0
            state->mode = TYPE;
809
0
            break;
810
0
#endif
811
3.38k
        case DICTID:
812
3.38k
            NEEDBITS(32);
813
2.93k
            strm->adler = state->check = ZSWAP32(hold);
814
2.93k
            INITBITS();
815
2.93k
            state->mode = DICT;
816
                /* fallthrough */
817
2.93k
        case DICT:
818
2.93k
            if (state->havedict == 0) {
819
2.93k
                RESTORE();
820
2.93k
                return Z_NEED_DICT;
821
2.93k
            }
822
0
            strm->adler = state->check = adler32(0L, Z_NULL, 0);
823
0
            state->mode = TYPE;
824
                /* fallthrough */
825
26.6k
        case TYPE:
826
26.6k
            if (flush == Z_BLOCK || flush == Z_TREES) goto inf_leave;
827
                /* fallthrough */
828
26.8k
        case TYPEDO:
829
26.8k
            if (state->last) {
830
1.22k
                BYTEBITS();
831
1.22k
                state->mode = CHECK;
832
1.22k
                break;
833
1.22k
            }
834
25.5k
            NEEDBITS(3);
835
25.3k
            state->last = BITS(1);
836
25.3k
            DROPBITS(1);
837
25.3k
            switch (BITS(2)) {
838
24
            case 0:                             /* stored block */
839
24
                Tracev((stderr, "inflate:     stored block%s\n",
840
24
                        state->last ? " (last)" : ""));
841
24
                state->mode = STORED;
842
24
                break;
843
2.19k
            case 1:                             /* fixed block */
844
2.19k
                fixedtables(state);
845
2.19k
                Tracev((stderr, "inflate:     fixed codes block%s\n",
846
2.19k
                        state->last ? " (last)" : ""));
847
2.19k
                state->mode = LEN_;             /* decode codes */
848
2.19k
                if (flush == Z_TREES) {
849
0
                    DROPBITS(2);
850
0
                    goto inf_leave;
851
0
                }
852
2.19k
                break;
853
23.1k
            case 2:                             /* dynamic block */
854
23.1k
                Tracev((stderr, "inflate:     dynamic codes block%s\n",
855
23.1k
                        state->last ? " (last)" : ""));
856
23.1k
                state->mode = TABLE;
857
23.1k
                break;
858
15
            case 3:
859
15
                strm->msg = (char *)"invalid block type";
860
15
                state->mode = BAD;
861
25.3k
            }
862
25.3k
            DROPBITS(2);
863
25.3k
            break;
864
24
        case STORED:
865
24
            BYTEBITS();                         /* go to byte boundary */
866
24
            NEEDBITS(32);
867
24
            if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
868
11
                strm->msg = (char *)"invalid stored block lengths";
869
11
                state->mode = BAD;
870
11
                break;
871
11
            }
872
13
            state->length = (unsigned)hold & 0xffff;
873
13
            Tracev((stderr, "inflate:       stored length %u\n",
874
13
                    state->length));
875
13
            INITBITS();
876
13
            state->mode = COPY_;
877
13
            if (flush == Z_TREES) goto inf_leave;
878
                /* fallthrough */
879
13
        case COPY_:
880
13
            state->mode = COPY;
881
                /* fallthrough */
882
45
        case COPY:
883
45
            copy = state->length;
884
45
            if (copy) {
885
41
                if (copy > have) copy = have;
886
41
                if (copy > left) copy = left;
887
41
                if (copy == 0) goto inf_leave;
888
20
                zmemcpy(put, next, copy);
889
20
                have -= copy;
890
20
                next += copy;
891
20
                left -= copy;
892
20
                put += copy;
893
20
                state->length -= copy;
894
20
                break;
895
41
            }
896
4
            Tracev((stderr, "inflate:       stored end\n"));
897
4
            state->mode = TYPE;
898
4
            break;
899
23.4k
        case TABLE:
900
23.4k
            NEEDBITS(14);
901
22.8k
            state->nlen = BITS(5) + 257;
902
22.8k
            DROPBITS(5);
903
22.8k
            state->ndist = BITS(5) + 1;
904
22.8k
            DROPBITS(5);
905
22.8k
            state->ncode = BITS(4) + 4;
906
22.8k
            DROPBITS(4);
907
22.8k
#ifndef PKZIP_BUG_WORKAROUND
908
22.8k
            if (state->nlen > 286 || state->ndist > 30) {
909
60
                strm->msg = (char *)"too many length or distance symbols";
910
60
                state->mode = BAD;
911
60
                break;
912
60
            }
913
22.7k
#endif
914
22.7k
            Tracev((stderr, "inflate:       table sizes ok\n"));
915
22.7k
            state->have = 0;
916
22.7k
            state->mode = LENLENS;
917
                /* fallthrough */
918
23.2k
        case LENLENS:
919
421k
            while (state->have < state->ncode) {
920
399k
                NEEDBITS(3);
921
398k
                state->lens[order[state->have++]] = (unsigned short)BITS(3);
922
398k
                DROPBITS(3);
923
398k
            }
924
52.0k
            while (state->have < 19)
925
29.7k
                state->lens[order[state->have++]] = 0;
926
22.3k
            state->next = state->codes;
927
22.3k
            state->lencode = (const code FAR *)(state->next);
928
22.3k
            state->lenbits = 7;
929
22.3k
            ret = inflate_table(CODES, state->lens, 19, &(state->next),
930
22.3k
                                &(state->lenbits), state->work);
931
22.3k
            if (ret) {
932
208
                strm->msg = (char *)"invalid code lengths set";
933
208
                state->mode = BAD;
934
208
                break;
935
208
            }
936
22.1k
            Tracev((stderr, "inflate:       code lengths ok\n"));
937
22.1k
            state->have = 0;
938
22.1k
            state->mode = CODELENS;
939
                /* fallthrough */
940
23.3k
        case CODELENS:
941
1.05M
            while (state->have < state->nlen + state->ndist) {
942
1.46M
                for (;;) {
943
1.46M
                    here = state->lencode[BITS(state->lenbits)];
944
1.46M
                    if ((unsigned)(here.bits) <= bits) break;
945
436k
                    PULLBYTE();
946
436k
                }
947
1.03M
                if (here.val < 16) {
948
887k
                    DROPBITS(here.bits);
949
887k
                    state->lens[state->have++] = here.val;
950
887k
                }
951
144k
                else {
952
144k
                    if (here.val == 16) {
953
62.6k
                        NEEDBITS(here.bits + 2);
954
62.6k
                        DROPBITS(here.bits);
955
62.6k
                        if (state->have == 0) {
956
0
                            strm->msg = (char *)"invalid bit length repeat";
957
0
                            state->mode = BAD;
958
0
                            break;
959
0
                        }
960
62.6k
                        len = state->lens[state->have - 1];
961
62.6k
                        copy = 3 + BITS(2);
962
62.6k
                        DROPBITS(2);
963
62.6k
                    }
964
81.6k
                    else if (here.val == 17) {
965
26.2k
                        NEEDBITS(here.bits + 3);
966
26.2k
                        DROPBITS(here.bits);
967
26.2k
                        len = 0;
968
26.2k
                        copy = 3 + BITS(3);
969
26.2k
                        DROPBITS(3);
970
26.2k
                    }
971
55.3k
                    else {
972
55.3k
                        NEEDBITS(here.bits + 7);
973
55.0k
                        DROPBITS(here.bits);
974
55.0k
                        len = 0;
975
55.0k
                        copy = 11 + BITS(7);
976
55.0k
                        DROPBITS(7);
977
55.0k
                    }
978
143k
                    if (state->have + copy > state->nlen + state->ndist) {
979
72
                        strm->msg = (char *)"invalid bit length repeat";
980
72
                        state->mode = BAD;
981
72
                        break;
982
72
                    }
983
5.59M
                    while (copy--)
984
5.44M
                        state->lens[state->have++] = (unsigned short)len;
985
143k
                }
986
1.03M
            }
987
988
            /* handle error breaks in while */
989
21.0k
            if (state->mode == BAD) break;
990
991
            /* check for end-of-block code (better have one) */
992
21.0k
            if (state->lens[256] == 0) {
993
161
                strm->msg = (char *)"invalid code -- missing end-of-block";
994
161
                state->mode = BAD;
995
161
                break;
996
161
            }
997
998
            /* build code tables -- note: do not change the lenbits or distbits
999
               values here (9 and 6) without reading the comments in inftrees.h
1000
               concerning the ENOUGH constants, which depend on those values */
1001
20.8k
            state->next = state->codes;
1002
20.8k
            state->lencode = (const code FAR *)(state->next);
1003
20.8k
            state->lenbits = 9;
1004
20.8k
            ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
1005
20.8k
                                &(state->lenbits), state->work);
1006
20.8k
            if (ret) {
1007
1.43k
                strm->msg = (char *)"invalid literal/lengths set";
1008
1.43k
                state->mode = BAD;
1009
1.43k
                break;
1010
1.43k
            }
1011
19.4k
            state->distcode = (const code FAR *)(state->next);
1012
19.4k
            state->distbits = 6;
1013
19.4k
            ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
1014
19.4k
                            &(state->next), &(state->distbits), state->work);
1015
19.4k
            if (ret) {
1016
1.08k
                strm->msg = (char *)"invalid distances set";
1017
1.08k
                state->mode = BAD;
1018
1.08k
                break;
1019
1.08k
            }
1020
18.3k
            Tracev((stderr, "inflate:       codes ok\n"));
1021
18.3k
            state->mode = LEN_;
1022
18.3k
            if (flush == Z_TREES) goto inf_leave;
1023
                /* fallthrough */
1024
20.5k
        case LEN_:
1025
20.5k
            state->mode = LEN;
1026
                /* fallthrough */
1027
1.32M
        case LEN:
1028
1.32M
            if (have >= 6 && left >= 258) {
1029
32.5k
                RESTORE();
1030
32.5k
                inflate_fast(strm, out);
1031
32.5k
                LOAD();
1032
32.5k
                if (state->mode == TYPE)
1033
1.52k
                    state->back = -1;
1034
32.5k
                break;
1035
32.5k
            }
1036
1.28M
            state->back = 0;
1037
2.29M
            for (;;) {
1038
2.29M
                here = state->lencode[BITS(state->lenbits)];
1039
2.29M
                if ((unsigned)(here.bits) <= bits) break;
1040
1.01M
                PULLBYTE();
1041
1.01M
            }
1042
1.28M
            if (here.op && (here.op & 0xf0) == 0) {
1043
35.0k
                last = here;
1044
42.1k
                for (;;) {
1045
42.1k
                    here = state->lencode[last.val +
1046
42.1k
                            (BITS(last.bits + last.op) >> last.bits)];
1047
42.1k
                    if ((unsigned)(last.bits + here.bits) <= bits) break;
1048
7.12k
                    PULLBYTE();
1049
7.12k
                }
1050
35.0k
                DROPBITS(last.bits);
1051
35.0k
                state->back += last.bits;
1052
35.0k
            }
1053
1.28M
            DROPBITS(here.bits);
1054
1.28M
            state->back += here.bits;
1055
1.28M
            state->length = (unsigned)here.val;
1056
1.28M
            if ((int)(here.op) == 0) {
1057
1.08M
                Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
1058
1.08M
                        "inflate:         literal '%c'\n" :
1059
1.08M
                        "inflate:         literal 0x%02x\n", here.val));
1060
1.08M
                state->mode = LIT;
1061
1.08M
                break;
1062
1.08M
            }
1063
197k
            if (here.op & 32) {
1064
1.12k
                Tracevv((stderr, "inflate:         end of block\n"));
1065
1.12k
                state->back = -1;
1066
1.12k
                state->mode = TYPE;
1067
1.12k
                break;
1068
1.12k
            }
1069
196k
            if (here.op & 64) {
1070
71
                strm->msg = (char *)"invalid literal/length code";
1071
71
                state->mode = BAD;
1072
71
                break;
1073
71
            }
1074
196k
            state->extra = (unsigned)(here.op) & 15;
1075
196k
            state->mode = LENEXT;
1076
                /* fallthrough */
1077
197k
        case LENEXT:
1078
197k
            if (state->extra) {
1079
90.3k
                NEEDBITS(state->extra);
1080
89.9k
                state->length += BITS(state->extra);
1081
89.9k
                DROPBITS(state->extra);
1082
89.9k
                state->back += state->extra;
1083
89.9k
            }
1084
196k
            Tracevv((stderr, "inflate:         length %u\n", state->length));
1085
196k
            state->was = state->length;
1086
196k
            state->mode = DIST;
1087
                /* fallthrough */
1088
197k
        case DIST:
1089
292k
            for (;;) {
1090
292k
                here = state->distcode[BITS(state->distbits)];
1091
292k
                if ((unsigned)(here.bits) <= bits) break;
1092
95.9k
                PULLBYTE();
1093
95.9k
            }
1094
196k
            if ((here.op & 0xf0) == 0) {
1095
8.04k
                last = here;
1096
10.0k
                for (;;) {
1097
10.0k
                    here = state->distcode[last.val +
1098
10.0k
                            (BITS(last.bits + last.op) >> last.bits)];
1099
10.0k
                    if ((unsigned)(last.bits + here.bits) <= bits) break;
1100
2.03k
                    PULLBYTE();
1101
2.03k
                }
1102
8.04k
                DROPBITS(last.bits);
1103
8.04k
                state->back += last.bits;
1104
8.04k
            }
1105
196k
            DROPBITS(here.bits);
1106
196k
            state->back += here.bits;
1107
196k
            if (here.op & 64) {
1108
2.48k
                strm->msg = (char *)"invalid distance code";
1109
2.48k
                state->mode = BAD;
1110
2.48k
                break;
1111
2.48k
            }
1112
194k
            state->offset = (unsigned)here.val;
1113
194k
            state->extra = (unsigned)(here.op) & 15;
1114
194k
            state->mode = DISTEXT;
1115
                /* fallthrough */
1116
195k
        case DISTEXT:
1117
195k
            if (state->extra) {
1118
155k
                NEEDBITS(state->extra);
1119
154k
                state->offset += BITS(state->extra);
1120
154k
                DROPBITS(state->extra);
1121
154k
                state->back += state->extra;
1122
154k
            }
1123
#ifdef INFLATE_STRICT
1124
            if (state->offset > state->dmax) {
1125
                strm->msg = (char *)"invalid distance too far back";
1126
                state->mode = BAD;
1127
                break;
1128
            }
1129
#endif
1130
194k
            Tracevv((stderr, "inflate:         distance %u\n", state->offset));
1131
194k
            state->mode = MATCH;
1132
                /* fallthrough */
1133
344k
        case MATCH:
1134
344k
            if (left == 0) goto inf_leave;
1135
276k
            copy = out - left;
1136
276k
            if (state->offset > copy) {         /* copy from window */
1137
108k
                copy = state->offset - copy;
1138
108k
                if (copy > state->whave) {
1139
384
                    if (state->sane) {
1140
384
                        strm->msg = (char *)"invalid distance too far back";
1141
384
                        state->mode = BAD;
1142
384
                        break;
1143
384
                    }
1144
#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
1145
                    Trace((stderr, "inflate.c too far\n"));
1146
                    copy -= state->whave;
1147
                    if (copy > state->length) copy = state->length;
1148
                    if (copy > left) copy = left;
1149
                    left -= copy;
1150
                    state->length -= copy;
1151
                    do {
1152
                        *put++ = 0;
1153
                    } while (--copy);
1154
                    if (state->length == 0) state->mode = LEN;
1155
                    break;
1156
#endif
1157
384
                }
1158
108k
                if (copy > state->wnext) {
1159
16.0k
                    copy -= state->wnext;
1160
16.0k
                    from = state->window + (state->wsize - copy);
1161
16.0k
                }
1162
91.9k
                else
1163
91.9k
                    from = state->window + (state->wnext - copy);
1164
108k
                if (copy > state->length) copy = state->length;
1165
108k
            }
1166
168k
            else {                              /* copy from output */
1167
168k
                from = put - state->offset;
1168
168k
                copy = state->length;
1169
168k
            }
1170
276k
            if (copy > left) copy = left;
1171
276k
            left -= copy;
1172
276k
            state->length -= copy;
1173
7.19M
            do {
1174
7.19M
                *put++ = *from++;
1175
7.19M
            } while (--copy);
1176
276k
            if (state->length == 0) state->mode = LEN;
1177
276k
            break;
1178
1.09M
        case LIT:
1179
1.09M
            if (left == 0) goto inf_leave;
1180
1.08M
            *put++ = (unsigned char)(state->length);
1181
1.08M
            left--;
1182
1.08M
            state->mode = LEN;
1183
1.08M
            break;
1184
1.22k
        case CHECK:
1185
1.22k
            if (state->wrap) {
1186
1.22k
                NEEDBITS(32);
1187
1.22k
                out -= left;
1188
1.22k
                strm->total_out += out;
1189
1.22k
                state->total += out;
1190
1.22k
                if ((state->wrap & 4) && out)
1191
597
                    strm->adler = state->check =
1192
597
                        UPDATE_CHECK(state->check, put - out, out);
1193
1.22k
                out = left;
1194
1.22k
                if ((state->wrap & 4) && (
1195
1.22k
#ifdef GUNZIP
1196
1.22k
                     state->flags ? hold :
1197
1.22k
#endif
1198
1.22k
                     ZSWAP32(hold)) != state->check) {
1199
336
                    strm->msg = (char *)"incorrect data check";
1200
336
                    state->mode = BAD;
1201
336
                    break;
1202
336
                }
1203
885
                INITBITS();
1204
885
                Tracev((stderr, "inflate:   check matches trailer\n"));
1205
885
            }
1206
885
#ifdef GUNZIP
1207
885
            state->mode = LENGTH;
1208
                /* fallthrough */
1209
885
        case LENGTH:
1210
885
            if (state->wrap && state->flags) {
1211
0
                NEEDBITS(32);
1212
0
                if ((state->wrap & 4) && hold != (state->total & 0xffffffff)) {
1213
0
                    strm->msg = (char *)"incorrect length check";
1214
0
                    state->mode = BAD;
1215
0
                    break;
1216
0
                }
1217
0
                INITBITS();
1218
0
                Tracev((stderr, "inflate:   length matches trailer\n"));
1219
0
            }
1220
885
#endif
1221
885
            state->mode = DONE;
1222
                /* fallthrough */
1223
885
        case DONE:
1224
885
            ret = Z_STREAM_END;
1225
885
            goto inf_leave;
1226
103k
        case BAD:
1227
103k
            ret = Z_DATA_ERROR;
1228
103k
            goto inf_leave;
1229
0
        case MEM:
1230
0
            return Z_MEM_ERROR;
1231
0
        case SYNC:
1232
                /* fallthrough */
1233
0
        default:
1234
0
            return Z_STREAM_ERROR;
1235
2.84M
        }
1236
1237
    /*
1238
       Return from inflate(), updating the total counts and the check value.
1239
       If there was no progress during the inflate() call, return a buffer
1240
       error.  Call updatewindow() to create and/or update the window state.
1241
       Note: a memory error from inflate() is non-recoverable.
1242
     */
1243
200k
  inf_leave:
1244
200k
    RESTORE();
1245
200k
    if (state->wsize || (out != strm->avail_out && state->mode < BAD &&
1246
15.0k
            (state->mode < CHECK || flush != Z_FINISH)))
1247
80.0k
        if (updatewindow(strm, strm->next_out, out - strm->avail_out)) {
1248
0
            state->mode = MEM;
1249
0
            return Z_MEM_ERROR;
1250
0
        }
1251
200k
    in -= strm->avail_in;
1252
200k
    out -= strm->avail_out;
1253
200k
    strm->total_in += in;
1254
200k
    strm->total_out += out;
1255
200k
    state->total += out;
1256
200k
    if ((state->wrap & 4) && out)
1257
79.4k
        strm->adler = state->check =
1258
79.4k
            UPDATE_CHECK(state->check, strm->next_out - out, out);
1259
200k
    strm->data_type = (int)state->bits + (state->last ? 64 : 0) +
1260
200k
                      (state->mode == TYPE ? 128 : 0) +
1261
200k
                      (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0);
1262
200k
    if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
1263
8.35k
        ret = Z_BUF_ERROR;
1264
200k
    return ret;
1265
200k
}
1266
1267
609
int ZEXPORT inflateEnd(z_streamp strm) {
1268
609
    struct inflate_state FAR *state;
1269
609
    if (inflateStateCheck(strm))
1270
54
        return Z_STREAM_ERROR;
1271
555
    state = (struct inflate_state FAR *)strm->state;
1272
555
    if (state->window != Z_NULL) ZFREE(strm, state->window);
1273
555
    ZFREE(strm, strm->state);
1274
555
    strm->state = Z_NULL;
1275
555
    Tracev((stderr, "inflate: end\n"));
1276
555
    return Z_OK;
1277
609
}
1278
1279
int ZEXPORT inflateGetDictionary(z_streamp strm, Bytef *dictionary,
1280
0
                                 uInt *dictLength) {
1281
0
    struct inflate_state FAR *state;
1282
1283
    /* check state */
1284
0
    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1285
0
    state = (struct inflate_state FAR *)strm->state;
1286
1287
    /* copy dictionary */
1288
0
    if (state->whave && dictionary != Z_NULL) {
1289
0
        zmemcpy(dictionary, state->window + state->wnext,
1290
0
                state->whave - state->wnext);
1291
0
        zmemcpy(dictionary + state->whave - state->wnext,
1292
0
                state->window, state->wnext);
1293
0
    }
1294
0
    if (dictLength != Z_NULL)
1295
0
        *dictLength = state->whave;
1296
0
    return Z_OK;
1297
0
}
1298
1299
int ZEXPORT inflateSetDictionary(z_streamp strm, const Bytef *dictionary,
1300
0
                                 uInt dictLength) {
1301
0
    struct inflate_state FAR *state;
1302
0
    unsigned long dictid;
1303
0
    int ret;
1304
1305
    /* check state */
1306
0
    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1307
0
    state = (struct inflate_state FAR *)strm->state;
1308
0
    if (state->wrap != 0 && state->mode != DICT)
1309
0
        return Z_STREAM_ERROR;
1310
1311
    /* check for correct dictionary identifier */
1312
0
    if (state->mode == DICT) {
1313
0
        dictid = adler32(0L, Z_NULL, 0);
1314
0
        dictid = adler32(dictid, dictionary, dictLength);
1315
0
        if (dictid != state->check)
1316
0
            return Z_DATA_ERROR;
1317
0
    }
1318
1319
    /* copy dictionary to window using updatewindow(), which will amend the
1320
       existing dictionary if appropriate */
1321
0
    ret = updatewindow(strm, dictionary + dictLength, dictLength);
1322
0
    if (ret) {
1323
0
        state->mode = MEM;
1324
0
        return Z_MEM_ERROR;
1325
0
    }
1326
0
    state->havedict = 1;
1327
0
    Tracev((stderr, "inflate:   dictionary set\n"));
1328
0
    return Z_OK;
1329
0
}
1330
1331
0
int ZEXPORT inflateGetHeader(z_streamp strm, gz_headerp head) {
1332
0
    struct inflate_state FAR *state;
1333
1334
    /* check state */
1335
0
    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1336
0
    state = (struct inflate_state FAR *)strm->state;
1337
0
    if ((state->wrap & 2) == 0) return Z_STREAM_ERROR;
1338
1339
    /* save header structure */
1340
0
    state->head = head;
1341
0
    head->done = 0;
1342
0
    return Z_OK;
1343
0
}
1344
1345
/*
1346
   Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff.  Return when found
1347
   or when out of input.  When called, *have is the number of pattern bytes
1348
   found in order so far, in 0..3.  On return *have is updated to the new
1349
   state.  If on return *have equals four, then the pattern was found and the
1350
   return value is how many bytes were read including the last byte of the
1351
   pattern.  If *have is less than four, then the pattern has not been found
1352
   yet and the return value is len.  In the latter case, syncsearch() can be
1353
   called again with more data and the *have state.  *have is initialized to
1354
   zero for the first call.
1355
 */
1356
local unsigned syncsearch(unsigned FAR *have, const unsigned char FAR *buf,
1357
0
                          unsigned len) {
1358
0
    unsigned got;
1359
0
    unsigned next;
1360
1361
0
    got = *have;
1362
0
    next = 0;
1363
0
    while (next < len && got < 4) {
1364
0
        if ((int)(buf[next]) == (got < 2 ? 0 : 0xff))
1365
0
            got++;
1366
0
        else if (buf[next])
1367
0
            got = 0;
1368
0
        else
1369
0
            got = 4 - got;
1370
0
        next++;
1371
0
    }
1372
0
    *have = got;
1373
0
    return next;
1374
0
}
1375
1376
0
int ZEXPORT inflateSync(z_streamp strm) {
1377
0
    unsigned len;               /* number of bytes to look at or looked at */
1378
0
    int flags;                  /* temporary to save header status */
1379
0
    unsigned long in, out;      /* temporary to save total_in and total_out */
1380
0
    unsigned char buf[4];       /* to restore bit buffer to byte string */
1381
0
    struct inflate_state FAR *state;
1382
1383
    /* check parameters */
1384
0
    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1385
0
    state = (struct inflate_state FAR *)strm->state;
1386
0
    if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR;
1387
1388
    /* if first time, start search in bit buffer */
1389
0
    if (state->mode != SYNC) {
1390
0
        state->mode = SYNC;
1391
0
        state->hold >>= state->bits & 7;
1392
0
        state->bits -= state->bits & 7;
1393
0
        len = 0;
1394
0
        while (state->bits >= 8) {
1395
0
            buf[len++] = (unsigned char)(state->hold);
1396
0
            state->hold >>= 8;
1397
0
            state->bits -= 8;
1398
0
        }
1399
0
        state->have = 0;
1400
0
        syncsearch(&(state->have), buf, len);
1401
0
    }
1402
1403
    /* search available input */
1404
0
    len = syncsearch(&(state->have), strm->next_in, strm->avail_in);
1405
0
    strm->avail_in -= len;
1406
0
    strm->next_in += len;
1407
0
    strm->total_in += len;
1408
1409
    /* return no joy or set up to restart inflate() on a new block */
1410
0
    if (state->have != 4) return Z_DATA_ERROR;
1411
0
    if (state->flags == -1)
1412
0
        state->wrap = 0;    /* if no header yet, treat as raw */
1413
0
    else
1414
0
        state->wrap &= ~4;  /* no point in computing a check value now */
1415
0
    flags = state->flags;
1416
0
    in = strm->total_in;  out = strm->total_out;
1417
0
    inflateReset(strm);
1418
0
    strm->total_in = in;  strm->total_out = out;
1419
0
    state->flags = flags;
1420
0
    state->mode = TYPE;
1421
0
    return Z_OK;
1422
0
}
1423
1424
/*
1425
   Returns true if inflate is currently at the end of a block generated by
1426
   Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
1427
   implementation to provide an additional safety check. PPP uses
1428
   Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored
1429
   block. When decompressing, PPP checks that at the end of input packet,
1430
   inflate is waiting for these length bytes.
1431
 */
1432
0
int ZEXPORT inflateSyncPoint(z_streamp strm) {
1433
0
    struct inflate_state FAR *state;
1434
1435
0
    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1436
0
    state = (struct inflate_state FAR *)strm->state;
1437
0
    return state->mode == STORED && state->bits == 0;
1438
0
}
1439
1440
0
int ZEXPORT inflateCopy(z_streamp dest, z_streamp source) {
1441
0
    struct inflate_state FAR *state;
1442
0
    struct inflate_state FAR *copy;
1443
0
    unsigned char FAR *window;
1444
0
    unsigned wsize;
1445
1446
    /* check input */
1447
0
    if (inflateStateCheck(source) || dest == Z_NULL)
1448
0
        return Z_STREAM_ERROR;
1449
0
    state = (struct inflate_state FAR *)source->state;
1450
1451
    /* allocate space */
1452
0
    copy = (struct inflate_state FAR *)
1453
0
           ZALLOC(source, 1, sizeof(struct inflate_state));
1454
0
    if (copy == Z_NULL) return Z_MEM_ERROR;
1455
0
    window = Z_NULL;
1456
0
    if (state->window != Z_NULL) {
1457
0
        window = (unsigned char FAR *)
1458
0
                 ZALLOC(source, 1U << state->wbits, sizeof(unsigned char));
1459
0
        if (window == Z_NULL) {
1460
0
            ZFREE(source, copy);
1461
0
            return Z_MEM_ERROR;
1462
0
        }
1463
0
    }
1464
1465
    /* copy state */
1466
0
    zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));
1467
0
    zmemcpy((voidpf)copy, (voidpf)state, sizeof(struct inflate_state));
1468
0
    copy->strm = dest;
1469
0
    if (state->lencode >= state->codes &&
1470
0
        state->lencode <= state->codes + ENOUGH - 1) {
1471
0
        copy->lencode = copy->codes + (state->lencode - state->codes);
1472
0
        copy->distcode = copy->codes + (state->distcode - state->codes);
1473
0
    }
1474
0
    copy->next = copy->codes + (state->next - state->codes);
1475
0
    if (window != Z_NULL) {
1476
0
        wsize = 1U << state->wbits;
1477
0
        zmemcpy(window, state->window, wsize);
1478
0
    }
1479
0
    copy->window = window;
1480
0
    dest->state = (struct internal_state FAR *)copy;
1481
0
    return Z_OK;
1482
0
}
1483
1484
0
int ZEXPORT inflateUndermine(z_streamp strm, int subvert) {
1485
0
    struct inflate_state FAR *state;
1486
1487
0
    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1488
0
    state = (struct inflate_state FAR *)strm->state;
1489
#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
1490
    state->sane = !subvert;
1491
    return Z_OK;
1492
#else
1493
0
    (void)subvert;
1494
0
    state->sane = 1;
1495
0
    return Z_DATA_ERROR;
1496
0
#endif
1497
0
}
1498
1499
0
int ZEXPORT inflateValidate(z_streamp strm, int check) {
1500
0
    struct inflate_state FAR *state;
1501
1502
0
    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1503
0
    state = (struct inflate_state FAR *)strm->state;
1504
0
    if (check && state->wrap)
1505
0
        state->wrap |= 4;
1506
0
    else
1507
0
        state->wrap &= ~4;
1508
0
    return Z_OK;
1509
0
}
1510
1511
0
long ZEXPORT inflateMark(z_streamp strm) {
1512
0
    struct inflate_state FAR *state;
1513
1514
0
    if (inflateStateCheck(strm))
1515
0
        return -(1L << 16);
1516
0
    state = (struct inflate_state FAR *)strm->state;
1517
0
    return (long)(((unsigned long)((long)state->back)) << 16) +
1518
0
        (state->mode == COPY ? state->length :
1519
0
            (state->mode == MATCH ? state->was - state->length : 0));
1520
0
}
1521
1522
0
unsigned long ZEXPORT inflateCodesUsed(z_streamp strm) {
1523
0
    struct inflate_state FAR *state;
1524
0
    if (inflateStateCheck(strm)) return (unsigned long)-1;
1525
0
    state = (struct inflate_state FAR *)strm->state;
1526
0
    return (unsigned long)(state->next - state->codes);
1527
0
}