Coverage Report

Created: 2026-01-10 07:02

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
114k
local int inflateStateCheck(z_streamp strm) {
89
114k
    struct inflate_state FAR *state;
90
114k
    if (strm == Z_NULL ||
91
114k
        strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0)
92
0
        return 1;
93
114k
    state = (struct inflate_state FAR *)strm->state;
94
114k
    if (state == Z_NULL || state->strm != strm ||
95
114k
        state->mode < HEAD || state->mode > SYNC)
96
0
        return 1;
97
114k
    return 0;
98
114k
}
99
100
8.91k
int ZEXPORT inflateResetKeep(z_streamp strm) {
101
8.91k
    struct inflate_state FAR *state;
102
103
8.91k
    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
104
8.91k
    state = (struct inflate_state FAR *)strm->state;
105
8.91k
    strm->total_in = strm->total_out = state->total = 0;
106
8.91k
    strm->msg = Z_NULL;
107
8.91k
    strm->data_type = 0;
108
8.91k
    if (state->wrap)        /* to support ill-conceived Java test suite */
109
8.91k
        strm->adler = state->wrap & 1;
110
8.91k
    state->mode = HEAD;
111
8.91k
    state->last = 0;
112
8.91k
    state->havedict = 0;
113
8.91k
    state->flags = -1;
114
8.91k
    state->dmax = 32768U;
115
8.91k
    state->head = Z_NULL;
116
8.91k
    state->hold = 0;
117
8.91k
    state->bits = 0;
118
8.91k
    state->lencode = state->distcode = state->next = state->codes;
119
8.91k
    state->sane = 1;
120
8.91k
    state->back = -1;
121
8.91k
    Tracev((stderr, "inflate: reset\n"));
122
8.91k
    return Z_OK;
123
8.91k
}
124
125
8.91k
int ZEXPORT inflateReset(z_streamp strm) {
126
8.91k
    struct inflate_state FAR *state;
127
128
8.91k
    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
129
8.91k
    state = (struct inflate_state FAR *)strm->state;
130
8.91k
    state->wsize = 0;
131
8.91k
    state->whave = 0;
132
8.91k
    state->wnext = 0;
133
8.91k
    return inflateResetKeep(strm);
134
8.91k
}
135
136
8.91k
int ZEXPORT inflateReset2(z_streamp strm, int windowBits) {
137
8.91k
    int wrap;
138
8.91k
    struct inflate_state FAR *state;
139
140
    /* get the state */
141
8.91k
    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
142
8.91k
    state = (struct inflate_state FAR *)strm->state;
143
144
    /* extract wrap request from windowBits parameter */
145
8.91k
    if (windowBits < 0) {
146
0
        if (windowBits < -15)
147
0
            return Z_STREAM_ERROR;
148
0
        wrap = 0;
149
0
        windowBits = -windowBits;
150
0
    }
151
8.91k
    else {
152
8.91k
        wrap = (windowBits >> 4) + 5;
153
8.91k
#ifdef GUNZIP
154
8.91k
        if (windowBits < 48)
155
8.91k
            windowBits &= 15;
156
8.91k
#endif
157
8.91k
    }
158
159
    /* set number of window bits, free window if different */
160
8.91k
    if (windowBits && (windowBits < 8 || windowBits > 15))
161
0
        return Z_STREAM_ERROR;
162
8.91k
    if (state->window != Z_NULL && state->wbits != (unsigned)windowBits) {
163
0
        ZFREE(strm, state->window);
164
0
        state->window = Z_NULL;
165
0
    }
166
167
    /* update state and reset the rest of it */
168
8.91k
    state->wrap = wrap;
169
8.91k
    state->wbits = (unsigned)windowBits;
170
8.91k
    return inflateReset(strm);
171
8.91k
}
172
173
int ZEXPORT inflateInit2_(z_streamp strm, int windowBits,
174
8.91k
                          const char *version, int stream_size) {
175
8.91k
    int ret;
176
8.91k
    struct inflate_state FAR *state;
177
178
8.91k
    if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
179
8.91k
        stream_size != (int)(sizeof(z_stream)))
180
0
        return Z_VERSION_ERROR;
181
8.91k
    if (strm == Z_NULL) return Z_STREAM_ERROR;
182
8.91k
    strm->msg = Z_NULL;                 /* in case we return an error */
183
8.91k
    if (strm->zalloc == (alloc_func)0) {
184
#ifdef Z_SOLO
185
        return Z_STREAM_ERROR;
186
#else
187
8.91k
        strm->zalloc = zcalloc;
188
8.91k
        strm->opaque = (voidpf)0;
189
8.91k
#endif
190
8.91k
    }
191
8.91k
    if (strm->zfree == (free_func)0)
192
#ifdef Z_SOLO
193
        return Z_STREAM_ERROR;
194
#else
195
8.91k
        strm->zfree = zcfree;
196
8.91k
#endif
197
8.91k
    state = (struct inflate_state FAR *)
198
8.91k
            ZALLOC(strm, 1, sizeof(struct inflate_state));
199
8.91k
    if (state == Z_NULL) return Z_MEM_ERROR;
200
8.91k
    Tracev((stderr, "inflate: allocated\n"));
201
8.91k
    strm->state = (struct internal_state FAR *)state;
202
8.91k
    state->strm = strm;
203
8.91k
    state->window = Z_NULL;
204
8.91k
    state->mode = HEAD;     /* to pass state test in inflateReset2() */
205
8.91k
    ret = inflateReset2(strm, windowBits);
206
8.91k
    if (ret != Z_OK) {
207
0
        ZFREE(strm, state);
208
0
        strm->state = Z_NULL;
209
0
    }
210
8.91k
    return ret;
211
8.91k
}
212
213
int ZEXPORT inflateInit_(z_streamp strm, const char *version,
214
8.91k
                         int stream_size) {
215
8.91k
    return inflateInit2_(strm, DEF_WBITS, version, stream_size);
216
8.91k
}
217
218
0
int ZEXPORT inflatePrime(z_streamp strm, int bits, int value) {
219
0
    struct inflate_state FAR *state;
220
221
0
    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
222
0
    if (bits == 0)
223
0
        return Z_OK;
224
0
    state = (struct inflate_state FAR *)strm->state;
225
0
    if (bits < 0) {
226
0
        state->hold = 0;
227
0
        state->bits = 0;
228
0
        return Z_OK;
229
0
    }
230
0
    if (bits > 16 || state->bits + (uInt)bits > 32) return Z_STREAM_ERROR;
231
0
    value &= (1L << bits) - 1;
232
0
    state->hold += (unsigned long)value << state->bits;
233
0
    state->bits += (uInt)bits;
234
0
    return Z_OK;
235
0
}
236
237
/*
238
   Update the window with the last wsize (normally 32K) bytes written before
239
   returning.  If window does not exist yet, create it.  This is only called
240
   when a window is already in use, or when output has been written during this
241
   inflate call, but the end of the deflate stream has not been reached yet.
242
   It is also called to create a window for dictionary data when a dictionary
243
   is loaded.
244
245
   Providing output buffers larger than 32K to inflate() should provide a speed
246
   advantage, since only the last 32K of output is copied to the sliding window
247
   upon return from inflate(), and since all distances after the first 32K of
248
   output will fall in the output data, making match copies simpler and faster.
249
   The advantage may be dependent on the size of the processor's data caches.
250
 */
251
61.1k
local int updatewindow(z_streamp strm, const Bytef *end, unsigned copy) {
252
61.1k
    struct inflate_state FAR *state;
253
61.1k
    unsigned dist;
254
255
61.1k
    state = (struct inflate_state FAR *)strm->state;
256
257
    /* if it hasn't been done already, allocate space for the window */
258
61.1k
    if (state->window == Z_NULL) {
259
3.11k
        state->window = (unsigned char FAR *)
260
3.11k
                        ZALLOC(strm, 1U << state->wbits,
261
3.11k
                               sizeof(unsigned char));
262
3.11k
        if (state->window == Z_NULL) return 1;
263
3.11k
    }
264
265
    /* if window not in use yet, initialize */
266
61.1k
    if (state->wsize == 0) {
267
3.11k
        state->wsize = 1U << state->wbits;
268
3.11k
        state->wnext = 0;
269
3.11k
        state->whave = 0;
270
3.11k
    }
271
272
    /* copy state->wsize or less output bytes into the circular window */
273
61.1k
    if (copy >= state->wsize) {
274
414
        zmemcpy(state->window, end - state->wsize, state->wsize);
275
414
        state->wnext = 0;
276
414
        state->whave = state->wsize;
277
414
    }
278
60.7k
    else {
279
60.7k
        dist = state->wsize - state->wnext;
280
60.7k
        if (dist > copy) dist = copy;
281
60.7k
        zmemcpy(state->window + state->wnext, end - copy, dist);
282
60.7k
        copy -= dist;
283
60.7k
        if (copy) {
284
154
            zmemcpy(state->window, end - copy, copy);
285
154
            state->wnext = copy;
286
154
            state->whave = state->wsize;
287
154
        }
288
60.5k
        else {
289
60.5k
            state->wnext += dist;
290
60.5k
            if (state->wnext == state->wsize) state->wnext = 0;
291
60.5k
            if (state->whave < state->wsize) state->whave += dist;
292
60.5k
        }
293
60.7k
    }
294
61.1k
    return 0;
295
61.1k
}
296
297
/* Macros for inflate(): */
298
299
/* check function to use adler32() for zlib or crc32() for gzip */
300
#ifdef GUNZIP
301
#  define UPDATE_CHECK(check, buf, len) \
302
60.2k
    (state->flags ? crc32(check, buf, len) : adler32(check, buf, len))
303
#else
304
#  define UPDATE_CHECK(check, buf, len) adler32(check, buf, len)
305
#endif
306
307
/* check macros for header crc */
308
#ifdef GUNZIP
309
#  define CRC2(check, word) \
310
0
    do { \
311
0
        hbuf[0] = (unsigned char)(word); \
312
0
        hbuf[1] = (unsigned char)((word) >> 8); \
313
0
        check = crc32(check, hbuf, 2); \
314
0
    } while (0)
315
316
#  define CRC4(check, word) \
317
0
    do { \
318
0
        hbuf[0] = (unsigned char)(word); \
319
0
        hbuf[1] = (unsigned char)((word) >> 8); \
320
0
        hbuf[2] = (unsigned char)((word) >> 16); \
321
0
        hbuf[3] = (unsigned char)((word) >> 24); \
322
0
        check = crc32(check, hbuf, 4); \
323
0
    } while (0)
324
#endif
325
326
/* Load registers with state in inflate() for speed */
327
#define LOAD() \
328
97.8k
    do { \
329
97.8k
        put = strm->next_out; \
330
97.8k
        left = strm->avail_out; \
331
97.8k
        next = strm->next_in; \
332
97.8k
        have = strm->avail_in; \
333
97.8k
        hold = state->hold; \
334
97.8k
        bits = state->bits; \
335
97.8k
    } while (0)
336
337
/* Restore state from registers in inflate() */
338
#define RESTORE() \
339
97.8k
    do { \
340
97.8k
        strm->next_out = put; \
341
97.8k
        strm->avail_out = left; \
342
97.8k
        strm->next_in = next; \
343
97.8k
        strm->avail_in = have; \
344
97.8k
        state->hold = hold; \
345
97.8k
        state->bits = bits; \
346
97.8k
    } while (0)
347
348
/* Clear the input bit accumulator */
349
#define INITBITS() \
350
10.1k
    do { \
351
10.1k
        hold = 0; \
352
10.1k
        bits = 0; \
353
10.1k
    } while (0)
354
355
/* Get a byte of input into the bit accumulator, or return from inflate()
356
   if there is no input available. */
357
#define PULLBYTE() \
358
621k
    do { \
359
621k
        if (have == 0) goto inf_leave; \
360
621k
        have--; \
361
555k
        hold += (unsigned long)(*next++) << bits; \
362
555k
        bits += 8; \
363
555k
    } while (0)
364
365
/* Assure that there are at least n bits in the bit accumulator.  If there is
366
   not enough available input to do that, then return from inflate(). */
367
#define NEEDBITS(n) \
368
269k
    do { \
369
424k
        while (bits < (unsigned)(n)) \
370
269k
            PULLBYTE(); \
371
269k
    } while (0)
372
373
/* Return the low n bits of the bit accumulator (n < 16) */
374
#define BITS(n) \
375
1.65M
    ((unsigned)hold & ((1U << (n)) - 1))
376
377
/* Remove n bits from the bit accumulator */
378
#define DROPBITS(n) \
379
1.18M
    do { \
380
1.18M
        hold >>= (n); \
381
1.18M
        bits -= (unsigned)(n); \
382
1.18M
    } while (0)
383
384
/* Remove zero to seven bits as needed to go to a byte boundary */
385
#define BYTEBITS() \
386
3.99k
    do { \
387
3.99k
        hold >>= bits & 7; \
388
3.99k
        bits -= bits & 7; \
389
3.99k
    } while (0)
390
391
/*
392
   inflate() uses a state machine to process as much input data and generate as
393
   much output data as possible before returning.  The state machine is
394
   structured roughly as follows:
395
396
    for (;;) switch (state) {
397
    ...
398
    case STATEn:
399
        if (not enough input data or output space to make progress)
400
            return;
401
        ... make progress ...
402
        state = STATEm;
403
        break;
404
    ...
405
    }
406
407
   so when inflate() is called again, the same case is attempted again, and
408
   if the appropriate resources are provided, the machine proceeds to the
409
   next state.  The NEEDBITS() macro is usually the way the state evaluates
410
   whether it can proceed or should return.  NEEDBITS() does the return if
411
   the requested bits are not available.  The typical use of the BITS macros
412
   is:
413
414
        NEEDBITS(n);
415
        ... do something with BITS(n) ...
416
        DROPBITS(n);
417
418
   where NEEDBITS(n) either returns from inflate() if there isn't enough
419
   input left to load n bits into the accumulator, or it continues.  BITS(n)
420
   gives the low n bits in the accumulator.  When done, DROPBITS(n) drops
421
   the low n bits off the accumulator.  INITBITS() clears the accumulator
422
   and sets the number of available bits to zero.  BYTEBITS() discards just
423
   enough bits to put the accumulator on a byte boundary.  After BYTEBITS()
424
   and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
425
426
   NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
427
   if there is no input available.  The decoding of variable length codes uses
428
   PULLBYTE() directly in order to pull just enough bytes to decode the next
429
   code, and no more.
430
431
   Some states loop until they get enough input, making sure that enough
432
   state information is maintained to continue the loop where it left off
433
   if NEEDBITS() returns in the loop.  For example, want, need, and keep
434
   would all have to actually be part of the saved state in case NEEDBITS()
435
   returns:
436
437
    case STATEw:
438
        while (want < need) {
439
            NEEDBITS(n);
440
            keep[want++] = BITS(n);
441
            DROPBITS(n);
442
        }
443
        state = STATEx;
444
    case STATEx:
445
446
   As shown above, if the next state is also the next case, then the break
447
   is omitted.
448
449
   A state may also return if there is not enough output space available to
450
   complete that state.  Those states are copying stored data, writing a
451
   literal byte, and copying a matching string.
452
453
   When returning, a "goto inf_leave" is used to update the total counters,
454
   update the check value, and determine whether any progress has been made
455
   during that inflate() call in order to return the proper return code.
456
   Progress is defined as a change in either strm->avail_in or strm->avail_out.
457
   When there is a window, goto inf_leave will update the window with the last
458
   output written.  If a goto inf_leave occurs in the middle of decompression
459
   and there is no window currently, goto inf_leave will create one and copy
460
   output to the window for the next call of inflate().
461
462
   In this implementation, the flush parameter of inflate() only affects the
463
   return code (per zlib.h).  inflate() always writes as much as possible to
464
   strm->next_out, given the space available and the provided input--the effect
465
   documented in zlib.h of Z_SYNC_FLUSH.  Furthermore, inflate() always defers
466
   the allocation of and copying into a sliding window until necessary, which
467
   provides the effect documented in zlib.h for Z_FINISH when the entire input
468
   stream available.  So the only thing the flush parameter actually does is:
469
   when flush is set to Z_FINISH, inflate() cannot return Z_OK.  Instead it
470
   will return Z_BUF_ERROR if it has not reached the end of the stream.
471
 */
472
473
78.9k
int ZEXPORT inflate(z_streamp strm, int flush) {
474
78.9k
    struct inflate_state FAR *state;
475
78.9k
    z_const unsigned char FAR *next;    /* next input */
476
78.9k
    unsigned char FAR *put;     /* next output */
477
78.9k
    unsigned have, left;        /* available input and output */
478
78.9k
    unsigned long hold;         /* bit buffer */
479
78.9k
    unsigned bits;              /* bits in bit buffer */
480
78.9k
    unsigned in, out;           /* save starting available input and output */
481
78.9k
    unsigned copy;              /* number of stored or match bytes to copy */
482
78.9k
    unsigned char FAR *from;    /* where to copy match bytes from */
483
78.9k
    code here;                  /* current decoding table entry */
484
78.9k
    code last;                  /* parent table entry */
485
78.9k
    unsigned len;               /* length to copy for repeats, bits to drop */
486
78.9k
    int ret;                    /* return code */
487
78.9k
#ifdef GUNZIP
488
78.9k
    unsigned char hbuf[4];      /* buffer for gzip header crc calculation */
489
78.9k
#endif
490
78.9k
    static const unsigned short order[19] = /* permutation of code lengths */
491
78.9k
        {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
492
493
78.9k
    if (inflateStateCheck(strm) || strm->next_out == Z_NULL ||
494
78.9k
        (strm->next_in == Z_NULL && strm->avail_in != 0))
495
0
        return Z_STREAM_ERROR;
496
497
78.9k
    state = (struct inflate_state FAR *)strm->state;
498
78.9k
    if (state->mode == TYPE) state->mode = TYPEDO;      /* skip check */
499
78.9k
    LOAD();
500
78.9k
    in = have;
501
78.9k
    out = left;
502
78.9k
    ret = Z_OK;
503
78.9k
    for (;;)
504
554k
        switch (state->mode) {
505
8.98k
        case HEAD:
506
8.98k
            if (state->wrap == 0) {
507
0
                state->mode = TYPEDO;
508
0
                break;
509
0
            }
510
8.98k
            NEEDBITS(16);
511
8.87k
#ifdef GUNZIP
512
8.87k
            if ((state->wrap & 2) && hold == 0x8b1f) {  /* gzip header */
513
0
                if (state->wbits == 0)
514
0
                    state->wbits = 15;
515
0
                state->check = crc32(0L, Z_NULL, 0);
516
0
                CRC2(state->check, hold);
517
0
                INITBITS();
518
0
                state->mode = FLAGS;
519
0
                break;
520
0
            }
521
8.87k
            if (state->head != Z_NULL)
522
0
                state->head->done = -1;
523
8.87k
            if (!(state->wrap & 1) ||   /* check if zlib header allowed */
524
#else
525
            if (
526
#endif
527
8.87k
                ((BITS(8) << 8) + (hold >> 8)) % 31) {
528
1.56k
                strm->msg = (z_const char *)"incorrect header check";
529
1.56k
                state->mode = BAD;
530
1.56k
                break;
531
1.56k
            }
532
7.30k
            if (BITS(4) != Z_DEFLATED) {
533
47
                strm->msg = (z_const char *)"unknown compression method";
534
47
                state->mode = BAD;
535
47
                break;
536
47
            }
537
7.25k
            DROPBITS(4);
538
7.25k
            len = BITS(4) + 8;
539
7.25k
            if (state->wbits == 0)
540
0
                state->wbits = len;
541
7.25k
            if (len > 15 || len > state->wbits) {
542
19
                strm->msg = (z_const char *)"invalid window size";
543
19
                state->mode = BAD;
544
19
                break;
545
19
            }
546
7.23k
            state->dmax = 1U << len;
547
7.23k
            state->flags = 0;               /* indicate zlib header */
548
7.23k
            Tracev((stderr, "inflate:   zlib header ok\n"));
549
7.23k
            strm->adler = state->check = adler32(0L, Z_NULL, 0);
550
7.23k
            state->mode = hold & 0x200 ? DICTID : TYPE;
551
7.23k
            INITBITS();
552
7.23k
            break;
553
0
#ifdef GUNZIP
554
0
        case FLAGS:
555
0
            NEEDBITS(16);
556
0
            state->flags = (int)(hold);
557
0
            if ((state->flags & 0xff) != Z_DEFLATED) {
558
0
                strm->msg = (z_const char *)"unknown compression method";
559
0
                state->mode = BAD;
560
0
                break;
561
0
            }
562
0
            if (state->flags & 0xe000) {
563
0
                strm->msg = (z_const char *)"unknown header flags set";
564
0
                state->mode = BAD;
565
0
                break;
566
0
            }
567
0
            if (state->head != Z_NULL)
568
0
                state->head->text = (int)((hold >> 8) & 1);
569
0
            if ((state->flags & 0x0200) && (state->wrap & 4))
570
0
                CRC2(state->check, hold);
571
0
            INITBITS();
572
0
            state->mode = TIME;
573
                /* fallthrough */
574
0
        case TIME:
575
0
            NEEDBITS(32);
576
0
            if (state->head != Z_NULL)
577
0
                state->head->time = hold;
578
0
            if ((state->flags & 0x0200) && (state->wrap & 4))
579
0
                CRC4(state->check, hold);
580
0
            INITBITS();
581
0
            state->mode = OS;
582
                /* fallthrough */
583
0
        case OS:
584
0
            NEEDBITS(16);
585
0
            if (state->head != Z_NULL) {
586
0
                state->head->xflags = (int)(hold & 0xff);
587
0
                state->head->os = (int)(hold >> 8);
588
0
            }
589
0
            if ((state->flags & 0x0200) && (state->wrap & 4))
590
0
                CRC2(state->check, hold);
591
0
            INITBITS();
592
0
            state->mode = EXLEN;
593
                /* fallthrough */
594
0
        case EXLEN:
595
0
            if (state->flags & 0x0400) {
596
0
                NEEDBITS(16);
597
0
                state->length = (unsigned)(hold);
598
0
                if (state->head != Z_NULL)
599
0
                    state->head->extra_len = (unsigned)hold;
600
0
                if ((state->flags & 0x0200) && (state->wrap & 4))
601
0
                    CRC2(state->check, hold);
602
0
                INITBITS();
603
0
            }
604
0
            else if (state->head != Z_NULL)
605
0
                state->head->extra = Z_NULL;
606
0
            state->mode = EXTRA;
607
                /* fallthrough */
608
0
        case EXTRA:
609
0
            if (state->flags & 0x0400) {
610
0
                copy = state->length;
611
0
                if (copy > have) copy = have;
612
0
                if (copy) {
613
0
                    if (state->head != Z_NULL &&
614
0
                        state->head->extra != Z_NULL &&
615
0
                        (len = state->head->extra_len - state->length) <
616
0
                            state->head->extra_max) {
617
0
                        zmemcpy(state->head->extra + len, next,
618
0
                                len + copy > state->head->extra_max ?
619
0
                                state->head->extra_max - len : copy);
620
0
                    }
621
0
                    if ((state->flags & 0x0200) && (state->wrap & 4))
622
0
                        state->check = crc32(state->check, next, copy);
623
0
                    have -= copy;
624
0
                    next += copy;
625
0
                    state->length -= copy;
626
0
                }
627
0
                if (state->length) goto inf_leave;
628
0
            }
629
0
            state->length = 0;
630
0
            state->mode = NAME;
631
                /* fallthrough */
632
0
        case NAME:
633
0
            if (state->flags & 0x0800) {
634
0
                if (have == 0) goto inf_leave;
635
0
                copy = 0;
636
0
                do {
637
0
                    len = (unsigned)(next[copy++]);
638
0
                    if (state->head != Z_NULL &&
639
0
                            state->head->name != Z_NULL &&
640
0
                            state->length < state->head->name_max)
641
0
                        state->head->name[state->length++] = (Bytef)len;
642
0
                } while (len && copy < have);
643
0
                if ((state->flags & 0x0200) && (state->wrap & 4))
644
0
                    state->check = crc32(state->check, next, copy);
645
0
                have -= copy;
646
0
                next += copy;
647
0
                if (len) goto inf_leave;
648
0
            }
649
0
            else if (state->head != Z_NULL)
650
0
                state->head->name = Z_NULL;
651
0
            state->length = 0;
652
0
            state->mode = COMMENT;
653
                /* fallthrough */
654
0
        case COMMENT:
655
0
            if (state->flags & 0x1000) {
656
0
                if (have == 0) goto inf_leave;
657
0
                copy = 0;
658
0
                do {
659
0
                    len = (unsigned)(next[copy++]);
660
0
                    if (state->head != Z_NULL &&
661
0
                            state->head->comment != Z_NULL &&
662
0
                            state->length < state->head->comm_max)
663
0
                        state->head->comment[state->length++] = (Bytef)len;
664
0
                } while (len && copy < have);
665
0
                if ((state->flags & 0x0200) && (state->wrap & 4))
666
0
                    state->check = crc32(state->check, next, copy);
667
0
                have -= copy;
668
0
                next += copy;
669
0
                if (len) goto inf_leave;
670
0
            }
671
0
            else if (state->head != Z_NULL)
672
0
                state->head->comment = Z_NULL;
673
0
            state->mode = HCRC;
674
                /* fallthrough */
675
0
        case HCRC:
676
0
            if (state->flags & 0x0200) {
677
0
                NEEDBITS(16);
678
0
                if ((state->wrap & 4) && hold != (state->check & 0xffff)) {
679
0
                    strm->msg = (z_const char *)"header crc mismatch";
680
0
                    state->mode = BAD;
681
0
                    break;
682
0
                }
683
0
                INITBITS();
684
0
            }
685
0
            if (state->head != Z_NULL) {
686
0
                state->head->hcrc = (int)((state->flags >> 9) & 1);
687
0
                state->head->done = 1;
688
0
            }
689
0
            strm->adler = state->check = crc32(0L, Z_NULL, 0);
690
0
            state->mode = TYPE;
691
0
            break;
692
0
#endif
693
29
        case DICTID:
694
29
            NEEDBITS(32);
695
15
            strm->adler = state->check = ZSWAP32(hold);
696
15
            INITBITS();
697
15
            state->mode = DICT;
698
                /* fallthrough */
699
29
        case DICT:
700
29
            if (state->havedict == 0) {
701
29
                RESTORE();
702
29
                return Z_NEED_DICT;
703
29
            }
704
0
            strm->adler = state->check = adler32(0L, Z_NULL, 0);
705
0
            state->mode = TYPE;
706
                /* fallthrough */
707
15.8k
        case TYPE:
708
15.8k
            if (flush == Z_BLOCK || flush == Z_TREES) goto inf_leave;
709
                /* fallthrough */
710
16.0k
        case TYPEDO:
711
16.0k
            if (state->last) {
712
3.47k
                BYTEBITS();
713
3.47k
                state->mode = CHECK;
714
3.47k
                break;
715
3.47k
            }
716
12.5k
            NEEDBITS(3);
717
12.3k
            state->last = BITS(1);
718
12.3k
            DROPBITS(1);
719
12.3k
            switch (BITS(2)) {
720
470
            case 0:                             /* stored block */
721
470
                Tracev((stderr, "inflate:     stored block%s\n",
722
470
                        state->last ? " (last)" : ""));
723
470
                state->mode = STORED;
724
470
                break;
725
6.04k
            case 1:                             /* fixed block */
726
6.04k
                inflate_fixed(state);
727
6.04k
                Tracev((stderr, "inflate:     fixed codes block%s\n",
728
6.04k
                        state->last ? " (last)" : ""));
729
6.04k
                state->mode = LEN_;             /* decode codes */
730
6.04k
                if (flush == Z_TREES) {
731
0
                    DROPBITS(2);
732
0
                    goto inf_leave;
733
0
                }
734
6.04k
                break;
735
6.04k
            case 2:                             /* dynamic block */
736
5.77k
                Tracev((stderr, "inflate:     dynamic codes block%s\n",
737
5.77k
                        state->last ? " (last)" : ""));
738
5.77k
                state->mode = TABLE;
739
5.77k
                break;
740
40
            case 3:
741
40
                strm->msg = (z_const char *)"invalid block type";
742
40
                state->mode = BAD;
743
12.3k
            }
744
12.3k
            DROPBITS(2);
745
12.3k
            break;
746
527
        case STORED:
747
527
            BYTEBITS();                         /* go to byte boundary */
748
527
            NEEDBITS(32);
749
424
            if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
750
80
                strm->msg = (z_const char *)"invalid stored block lengths";
751
80
                state->mode = BAD;
752
80
                break;
753
80
            }
754
344
            state->length = (unsigned)hold & 0xffff;
755
344
            Tracev((stderr, "inflate:       stored length %u\n",
756
344
                    state->length));
757
344
            INITBITS();
758
344
            state->mode = COPY_;
759
344
            if (flush == Z_TREES) goto inf_leave;
760
                /* fallthrough */
761
344
        case COPY_:
762
344
            state->mode = COPY;
763
                /* fallthrough */
764
763
        case COPY:
765
763
            copy = state->length;
766
763
            if (copy) {
767
459
                if (copy > have) copy = have;
768
459
                if (copy > left) copy = left;
769
459
                if (copy == 0) goto inf_leave;
770
358
                zmemcpy(put, next, copy);
771
358
                have -= copy;
772
358
                next += copy;
773
358
                left -= copy;
774
358
                put += copy;
775
358
                state->length -= copy;
776
358
                break;
777
459
            }
778
304
            Tracev((stderr, "inflate:       stored end\n"));
779
304
            state->mode = TYPE;
780
304
            break;
781
6.62k
        case TABLE:
782
6.62k
            NEEDBITS(14);
783
5.76k
            state->nlen = BITS(5) + 257;
784
5.76k
            DROPBITS(5);
785
5.76k
            state->ndist = BITS(5) + 1;
786
5.76k
            DROPBITS(5);
787
5.76k
            state->ncode = BITS(4) + 4;
788
5.76k
            DROPBITS(4);
789
5.76k
#ifndef PKZIP_BUG_WORKAROUND
790
5.76k
            if (state->nlen > 286 || state->ndist > 30) {
791
25
                strm->msg = (z_const char *)
792
25
                    "too many length or distance symbols";
793
25
                state->mode = BAD;
794
25
                break;
795
25
            }
796
5.73k
#endif
797
5.73k
            Tracev((stderr, "inflate:       table sizes ok\n"));
798
5.73k
            state->have = 0;
799
5.73k
            state->mode = LENLENS;
800
                /* fallthrough */
801
6.62k
        case LENLENS:
802
99.3k
            while (state->have < state->ncode) {
803
93.6k
                NEEDBITS(3);
804
92.7k
                state->lens[order[state->have++]] = (unsigned short)BITS(3);
805
92.7k
                DROPBITS(3);
806
92.7k
            }
807
21.6k
            while (state->have < 19)
808
15.9k
                state->lens[order[state->have++]] = 0;
809
5.70k
            state->next = state->codes;
810
5.70k
            state->lencode = state->distcode = (const code FAR *)(state->next);
811
5.70k
            state->lenbits = 7;
812
5.70k
            ret = inflate_table(CODES, state->lens, 19, &(state->next),
813
5.70k
                                &(state->lenbits), state->work);
814
5.70k
            if (ret) {
815
165
                strm->msg = (z_const char *)"invalid code lengths set";
816
165
                state->mode = BAD;
817
165
                break;
818
165
            }
819
5.54k
            Tracev((stderr, "inflate:       code lengths ok\n"));
820
5.54k
            state->have = 0;
821
5.54k
            state->mode = CODELENS;
822
                /* fallthrough */
823
10.0k
        case CODELENS:
824
565k
            while (state->have < state->nlen + state->ndist) {
825
761k
                for (;;) {
826
761k
                    here = state->lencode[BITS(state->lenbits)];
827
761k
                    if ((unsigned)(here.bits) <= bits) break;
828
203k
                    PULLBYTE();
829
203k
                }
830
557k
                if (here.val < 16) {
831
485k
                    DROPBITS(here.bits);
832
485k
                    state->lens[state->have++] = here.val;
833
485k
                }
834
72.3k
                else {
835
72.3k
                    if (here.val == 16) {
836
24.7k
                        NEEDBITS(here.bits + 2);
837
24.6k
                        DROPBITS(here.bits);
838
24.6k
                        if (state->have == 0) {
839
11
                            strm->msg = (z_const char *)
840
11
                                "invalid bit length repeat";
841
11
                            state->mode = BAD;
842
11
                            break;
843
11
                        }
844
24.6k
                        len = state->lens[state->have - 1];
845
24.6k
                        copy = 3 + BITS(2);
846
24.6k
                        DROPBITS(2);
847
24.6k
                    }
848
47.5k
                    else if (here.val == 17) {
849
26.7k
                        NEEDBITS(here.bits + 3);
850
26.6k
                        DROPBITS(here.bits);
851
26.6k
                        len = 0;
852
26.6k
                        copy = 3 + BITS(3);
853
26.6k
                        DROPBITS(3);
854
26.6k
                    }
855
20.8k
                    else {
856
20.8k
                        NEEDBITS(here.bits + 7);
857
19.3k
                        DROPBITS(here.bits);
858
19.3k
                        len = 0;
859
19.3k
                        copy = 11 + BITS(7);
860
19.3k
                        DROPBITS(7);
861
19.3k
                    }
862
70.6k
                    if (state->have + copy > state->nlen + state->ndist) {
863
98
                        strm->msg = (z_const char *)
864
98
                            "invalid bit length repeat";
865
98
                        state->mode = BAD;
866
98
                        break;
867
98
                    }
868
1.22M
                    while (copy--)
869
1.15M
                        state->lens[state->have++] = (unsigned short)len;
870
70.5k
                }
871
557k
            }
872
873
            /* handle error breaks in while */
874
5.46k
            if (state->mode == BAD) break;
875
876
            /* check for end-of-block code (better have one) */
877
5.35k
            if (state->lens[256] == 0) {
878
34
                strm->msg = (z_const char *)
879
34
                    "invalid code -- missing end-of-block";
880
34
                state->mode = BAD;
881
34
                break;
882
34
            }
883
884
            /* build code tables -- note: do not change the lenbits or distbits
885
               values here (9 and 6) without reading the comments in inftrees.h
886
               concerning the ENOUGH constants, which depend on those values */
887
5.31k
            state->next = state->codes;
888
5.31k
            state->lencode = (const code FAR *)(state->next);
889
5.31k
            state->lenbits = 9;
890
5.31k
            ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
891
5.31k
                                &(state->lenbits), state->work);
892
5.31k
            if (ret) {
893
80
                strm->msg = (z_const char *)"invalid literal/lengths set";
894
80
                state->mode = BAD;
895
80
                break;
896
80
            }
897
5.23k
            state->distcode = (const code FAR *)(state->next);
898
5.23k
            state->distbits = 6;
899
5.23k
            ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
900
5.23k
                            &(state->next), &(state->distbits), state->work);
901
5.23k
            if (ret) {
902
52
                strm->msg = (z_const char *)"invalid distances set";
903
52
                state->mode = BAD;
904
52
                break;
905
52
            }
906
5.18k
            Tracev((stderr, "inflate:       codes ok\n"));
907
5.18k
            state->mode = LEN_;
908
5.18k
            if (flush == Z_TREES) goto inf_leave;
909
                /* fallthrough */
910
11.2k
        case LEN_:
911
11.2k
            state->mode = LEN;
912
                /* fallthrough */
913
315k
        case LEN:
914
315k
            if (have >= 6 && left >= 258) {
915
18.8k
                RESTORE();
916
18.8k
                inflate_fast(strm, out);
917
18.8k
                LOAD();
918
18.8k
                if (state->mode == TYPE)
919
4.34k
                    state->back = -1;
920
18.8k
                break;
921
18.8k
            }
922
296k
            state->back = 0;
923
466k
            for (;;) {
924
466k
                here = state->lencode[BITS(state->lenbits)];
925
466k
                if ((unsigned)(here.bits) <= bits) break;
926
210k
                PULLBYTE();
927
210k
            }
928
255k
            if (here.op && (here.op & 0xf0) == 0) {
929
11.0k
                last = here;
930
13.2k
                for (;;) {
931
13.2k
                    here = state->lencode[last.val +
932
13.2k
                            (BITS(last.bits + last.op) >> last.bits)];
933
13.2k
                    if ((unsigned)(last.bits + here.bits) <= bits) break;
934
2.67k
                    PULLBYTE();
935
2.67k
                }
936
10.5k
                DROPBITS(last.bits);
937
10.5k
                state->back += last.bits;
938
10.5k
            }
939
255k
            DROPBITS(here.bits);
940
255k
            state->back += here.bits;
941
255k
            state->length = (unsigned)here.val;
942
255k
            if ((int)(here.op) == 0) {
943
167k
                Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
944
167k
                        "inflate:         literal '%c'\n" :
945
167k
                        "inflate:         literal 0x%02x\n", here.val));
946
167k
                state->mode = LIT;
947
167k
                break;
948
167k
            }
949
88.2k
            if (here.op & 32) {
950
3.96k
                Tracevv((stderr, "inflate:         end of block\n"));
951
3.96k
                state->back = -1;
952
3.96k
                state->mode = TYPE;
953
3.96k
                break;
954
3.96k
            }
955
84.2k
            if (here.op & 64) {
956
6
                strm->msg = (z_const char *)"invalid literal/length code";
957
6
                state->mode = BAD;
958
6
                break;
959
6
            }
960
84.2k
            state->extra = (unsigned)(here.op) & 15;
961
84.2k
            state->mode = LENEXT;
962
                /* fallthrough */
963
85.0k
        case LENEXT:
964
85.0k
            if (state->extra) {
965
13.8k
                NEEDBITS(state->extra);
966
12.9k
                state->length += BITS(state->extra);
967
12.9k
                DROPBITS(state->extra);
968
12.9k
                state->back += state->extra;
969
12.9k
            }
970
84.1k
            Tracevv((stderr, "inflate:         length %u\n", state->length));
971
84.1k
            state->was = state->length;
972
84.1k
            state->mode = DIST;
973
                /* fallthrough */
974
90.9k
        case DIST:
975
118k
            for (;;) {
976
118k
                here = state->distcode[BITS(state->distbits)];
977
118k
                if ((unsigned)(here.bits) <= bits) break;
978
34.4k
                PULLBYTE();
979
34.4k
            }
980
83.9k
            if ((here.op & 0xf0) == 0) {
981
1.26k
                last = here;
982
1.69k
                for (;;) {
983
1.69k
                    here = state->distcode[last.val +
984
1.69k
                            (BITS(last.bits + last.op) >> last.bits)];
985
1.69k
                    if ((unsigned)(last.bits + here.bits) <= bits) break;
986
517
                    PULLBYTE();
987
517
                }
988
1.17k
                DROPBITS(last.bits);
989
1.17k
                state->back += last.bits;
990
1.17k
            }
991
83.8k
            DROPBITS(here.bits);
992
83.8k
            state->back += here.bits;
993
83.8k
            if (here.op & 64) {
994
23
                strm->msg = (z_const char *)"invalid distance code";
995
23
                state->mode = BAD;
996
23
                break;
997
23
            }
998
83.7k
            state->offset = (unsigned)here.val;
999
83.7k
            state->extra = (unsigned)(here.op) & 15;
1000
83.7k
            state->mode = DISTEXT;
1001
                /* fallthrough */
1002
92.3k
        case DISTEXT:
1003
92.3k
            if (state->extra) {
1004
56.7k
                NEEDBITS(state->extra);
1005
47.8k
                state->offset += BITS(state->extra);
1006
47.8k
                DROPBITS(state->extra);
1007
47.8k
                state->back += state->extra;
1008
47.8k
            }
1009
#ifdef INFLATE_STRICT
1010
            if (state->offset > state->dmax) {
1011
                strm->msg = (z_const char *)"invalid distance too far back";
1012
                state->mode = BAD;
1013
                break;
1014
            }
1015
#endif
1016
83.3k
            Tracevv((stderr, "inflate:         distance %u\n", state->offset));
1017
83.3k
            state->mode = MATCH;
1018
                /* fallthrough */
1019
92.4k
        case MATCH:
1020
92.4k
            if (left == 0) goto inf_leave;
1021
92.2k
            copy = out - left;
1022
92.2k
            if (state->offset > copy) {         /* copy from window */
1023
40.7k
                copy = state->offset - copy;
1024
40.7k
                if (copy > state->whave) {
1025
84
                    if (state->sane) {
1026
84
                        strm->msg = (z_const char *)
1027
84
                            "invalid distance too far back";
1028
84
                        state->mode = BAD;
1029
84
                        break;
1030
84
                    }
1031
#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
1032
                    Trace((stderr, "inflate.c too far\n"));
1033
                    copy -= state->whave;
1034
                    if (copy > state->length) copy = state->length;
1035
                    if (copy > left) copy = left;
1036
                    left -= copy;
1037
                    state->length -= copy;
1038
                    do {
1039
                        *put++ = 0;
1040
                    } while (--copy);
1041
                    if (state->length == 0) state->mode = LEN;
1042
                    break;
1043
#endif
1044
84
                }
1045
40.7k
                if (copy > state->wnext) {
1046
1.66k
                    copy -= state->wnext;
1047
1.66k
                    from = state->window + (state->wsize - copy);
1048
1.66k
                }
1049
39.0k
                else
1050
39.0k
                    from = state->window + (state->wnext - copy);
1051
40.7k
                if (copy > state->length) copy = state->length;
1052
40.7k
            }
1053
51.4k
            else {                              /* copy from output */
1054
51.4k
                from = put - state->offset;
1055
51.4k
                copy = state->length;
1056
51.4k
            }
1057
92.1k
            if (copy > left) copy = left;
1058
92.1k
            left -= copy;
1059
92.1k
            state->length -= copy;
1060
11.1M
            do {
1061
11.1M
                *put++ = *from++;
1062
11.1M
            } while (--copy);
1063
92.1k
            if (state->length == 0) state->mode = LEN;
1064
92.1k
            break;
1065
167k
        case LIT:
1066
167k
            if (left == 0) goto inf_leave;
1067
167k
            *put++ = (unsigned char)(state->length);
1068
167k
            left--;
1069
167k
            state->mode = LEN;
1070
167k
            break;
1071
4.07k
        case CHECK:
1072
4.07k
            if (state->wrap) {
1073
4.07k
                NEEDBITS(32);
1074
3.40k
                out -= left;
1075
3.40k
                strm->total_out += out;
1076
3.40k
                state->total += out;
1077
3.40k
                if ((state->wrap & 4) && out)
1078
2.86k
                    strm->adler = state->check =
1079
2.86k
                        UPDATE_CHECK(state->check, put - out, out);
1080
3.40k
                out = left;
1081
3.40k
                if ((state->wrap & 4) && (
1082
3.40k
#ifdef GUNZIP
1083
3.40k
                     state->flags ? hold :
1084
3.40k
#endif
1085
3.40k
                     ZSWAP32(hold)) != state->check) {
1086
818
                    strm->msg = (z_const char *)"incorrect data check";
1087
818
                    state->mode = BAD;
1088
818
                    break;
1089
818
                }
1090
2.58k
                INITBITS();
1091
2.58k
                Tracev((stderr, "inflate:   check matches trailer\n"));
1092
2.58k
            }
1093
2.58k
#ifdef GUNZIP
1094
2.58k
            state->mode = LENGTH;
1095
                /* fallthrough */
1096
2.58k
        case LENGTH:
1097
2.58k
            if (state->wrap && state->flags) {
1098
0
                NEEDBITS(32);
1099
0
                if ((state->wrap & 4) && hold != (state->total & 0xffffffff)) {
1100
0
                    strm->msg = (z_const char *)"incorrect length check";
1101
0
                    state->mode = BAD;
1102
0
                    break;
1103
0
                }
1104
0
                INITBITS();
1105
0
                Tracev((stderr, "inflate:   length matches trailer\n"));
1106
0
            }
1107
2.58k
#endif
1108
2.58k
            state->mode = DONE;
1109
                /* fallthrough */
1110
5.36k
        case DONE:
1111
5.36k
            ret = Z_STREAM_END;
1112
5.36k
            goto inf_leave;
1113
7.35k
        case BAD:
1114
7.35k
            ret = Z_DATA_ERROR;
1115
7.35k
            goto inf_leave;
1116
0
        case MEM:
1117
0
            return Z_MEM_ERROR;
1118
0
        case SYNC:
1119
                /* fallthrough */
1120
0
        default:
1121
0
            return Z_STREAM_ERROR;
1122
554k
        }
1123
1124
    /*
1125
       Return from inflate(), updating the total counts and the check value.
1126
       If there was no progress during the inflate() call, return a buffer
1127
       error.  Call updatewindow() to create and/or update the window state.
1128
       Note: a memory error from inflate() is non-recoverable.
1129
     */
1130
78.9k
  inf_leave:
1131
78.9k
    RESTORE();
1132
78.9k
    if (state->wsize || (out != strm->avail_out && state->mode < BAD &&
1133
3.11k
            (state->mode < CHECK || flush != Z_FINISH)))
1134
61.1k
        if (updatewindow(strm, strm->next_out, out - strm->avail_out)) {
1135
0
            state->mode = MEM;
1136
0
            return Z_MEM_ERROR;
1137
0
        }
1138
78.9k
    in -= strm->avail_in;
1139
78.9k
    out -= strm->avail_out;
1140
78.9k
    strm->total_in += in;
1141
78.9k
    strm->total_out += out;
1142
78.9k
    state->total += out;
1143
78.9k
    if ((state->wrap & 4) && out)
1144
57.4k
        strm->adler = state->check =
1145
57.4k
            UPDATE_CHECK(state->check, strm->next_out - out, out);
1146
78.9k
    strm->data_type = (int)state->bits + (state->last ? 64 : 0) +
1147
78.9k
                      (state->mode == TYPE ? 128 : 0) +
1148
78.9k
                      (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0);
1149
78.9k
    if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
1150
2.53k
        ret = Z_BUF_ERROR;
1151
78.9k
    return ret;
1152
78.9k
}
1153
1154
8.91k
int ZEXPORT inflateEnd(z_streamp strm) {
1155
8.91k
    struct inflate_state FAR *state;
1156
8.91k
    if (inflateStateCheck(strm))
1157
0
        return Z_STREAM_ERROR;
1158
8.91k
    state = (struct inflate_state FAR *)strm->state;
1159
8.91k
    if (state->window != Z_NULL) ZFREE(strm, state->window);
1160
8.91k
    ZFREE(strm, strm->state);
1161
8.91k
    strm->state = Z_NULL;
1162
8.91k
    Tracev((stderr, "inflate: end\n"));
1163
8.91k
    return Z_OK;
1164
8.91k
}
1165
1166
int ZEXPORT inflateGetDictionary(z_streamp strm, Bytef *dictionary,
1167
0
                                 uInt *dictLength) {
1168
0
    struct inflate_state FAR *state;
1169
1170
    /* check state */
1171
0
    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1172
0
    state = (struct inflate_state FAR *)strm->state;
1173
1174
    /* copy dictionary */
1175
0
    if (state->whave && dictionary != Z_NULL) {
1176
0
        zmemcpy(dictionary, state->window + state->wnext,
1177
0
                state->whave - state->wnext);
1178
0
        zmemcpy(dictionary + state->whave - state->wnext,
1179
0
                state->window, state->wnext);
1180
0
    }
1181
0
    if (dictLength != Z_NULL)
1182
0
        *dictLength = state->whave;
1183
0
    return Z_OK;
1184
0
}
1185
1186
int ZEXPORT inflateSetDictionary(z_streamp strm, const Bytef *dictionary,
1187
0
                                 uInt dictLength) {
1188
0
    struct inflate_state FAR *state;
1189
0
    unsigned long dictid;
1190
0
    int ret;
1191
1192
    /* check state */
1193
0
    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1194
0
    state = (struct inflate_state FAR *)strm->state;
1195
0
    if (state->wrap != 0 && state->mode != DICT)
1196
0
        return Z_STREAM_ERROR;
1197
1198
    /* check for correct dictionary identifier */
1199
0
    if (state->mode == DICT) {
1200
0
        dictid = adler32(0L, Z_NULL, 0);
1201
0
        dictid = adler32(dictid, dictionary, dictLength);
1202
0
        if (dictid != state->check)
1203
0
            return Z_DATA_ERROR;
1204
0
    }
1205
1206
    /* copy dictionary to window using updatewindow(), which will amend the
1207
       existing dictionary if appropriate */
1208
0
    ret = updatewindow(strm, dictionary + dictLength, dictLength);
1209
0
    if (ret) {
1210
0
        state->mode = MEM;
1211
0
        return Z_MEM_ERROR;
1212
0
    }
1213
0
    state->havedict = 1;
1214
0
    Tracev((stderr, "inflate:   dictionary set\n"));
1215
0
    return Z_OK;
1216
0
}
1217
1218
0
int ZEXPORT inflateGetHeader(z_streamp strm, gz_headerp head) {
1219
0
    struct inflate_state FAR *state;
1220
1221
    /* check state */
1222
0
    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1223
0
    state = (struct inflate_state FAR *)strm->state;
1224
0
    if ((state->wrap & 2) == 0) return Z_STREAM_ERROR;
1225
1226
    /* save header structure */
1227
0
    state->head = head;
1228
0
    head->done = 0;
1229
0
    return Z_OK;
1230
0
}
1231
1232
/*
1233
   Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff.  Return when found
1234
   or when out of input.  When called, *have is the number of pattern bytes
1235
   found in order so far, in 0..3.  On return *have is updated to the new
1236
   state.  If on return *have equals four, then the pattern was found and the
1237
   return value is how many bytes were read including the last byte of the
1238
   pattern.  If *have is less than four, then the pattern has not been found
1239
   yet and the return value is len.  In the latter case, syncsearch() can be
1240
   called again with more data and the *have state.  *have is initialized to
1241
   zero for the first call.
1242
 */
1243
local unsigned syncsearch(unsigned FAR *have, const unsigned char FAR *buf,
1244
0
                          unsigned len) {
1245
0
    unsigned got;
1246
0
    unsigned next;
1247
1248
0
    got = *have;
1249
0
    next = 0;
1250
0
    while (next < len && got < 4) {
1251
0
        if ((int)(buf[next]) == (got < 2 ? 0 : 0xff))
1252
0
            got++;
1253
0
        else if (buf[next])
1254
0
            got = 0;
1255
0
        else
1256
0
            got = 4 - got;
1257
0
        next++;
1258
0
    }
1259
0
    *have = got;
1260
0
    return next;
1261
0
}
1262
1263
0
int ZEXPORT inflateSync(z_streamp strm) {
1264
0
    unsigned len;               /* number of bytes to look at or looked at */
1265
0
    int flags;                  /* temporary to save header status */
1266
0
    unsigned long in, out;      /* temporary to save total_in and total_out */
1267
0
    unsigned char buf[4];       /* to restore bit buffer to byte string */
1268
0
    struct inflate_state FAR *state;
1269
1270
    /* check parameters */
1271
0
    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1272
0
    state = (struct inflate_state FAR *)strm->state;
1273
0
    if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR;
1274
1275
    /* if first time, start search in bit buffer */
1276
0
    if (state->mode != SYNC) {
1277
0
        state->mode = SYNC;
1278
0
        state->hold >>= state->bits & 7;
1279
0
        state->bits -= state->bits & 7;
1280
0
        len = 0;
1281
0
        while (state->bits >= 8) {
1282
0
            buf[len++] = (unsigned char)(state->hold);
1283
0
            state->hold >>= 8;
1284
0
            state->bits -= 8;
1285
0
        }
1286
0
        state->have = 0;
1287
0
        syncsearch(&(state->have), buf, len);
1288
0
    }
1289
1290
    /* search available input */
1291
0
    len = syncsearch(&(state->have), strm->next_in, strm->avail_in);
1292
0
    strm->avail_in -= len;
1293
0
    strm->next_in += len;
1294
0
    strm->total_in += len;
1295
1296
    /* return no joy or set up to restart inflate() on a new block */
1297
0
    if (state->have != 4) return Z_DATA_ERROR;
1298
0
    if (state->flags == -1)
1299
0
        state->wrap = 0;    /* if no header yet, treat as raw */
1300
0
    else
1301
0
        state->wrap &= ~4;  /* no point in computing a check value now */
1302
0
    flags = state->flags;
1303
0
    in = strm->total_in;  out = strm->total_out;
1304
0
    inflateReset(strm);
1305
0
    strm->total_in = in;  strm->total_out = out;
1306
0
    state->flags = flags;
1307
0
    state->mode = TYPE;
1308
0
    return Z_OK;
1309
0
}
1310
1311
/*
1312
   Returns true if inflate is currently at the end of a block generated by
1313
   Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
1314
   implementation to provide an additional safety check. PPP uses
1315
   Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored
1316
   block. When decompressing, PPP checks that at the end of input packet,
1317
   inflate is waiting for these length bytes.
1318
 */
1319
0
int ZEXPORT inflateSyncPoint(z_streamp strm) {
1320
0
    struct inflate_state FAR *state;
1321
1322
0
    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1323
0
    state = (struct inflate_state FAR *)strm->state;
1324
0
    return state->mode == STORED && state->bits == 0;
1325
0
}
1326
1327
0
int ZEXPORT inflateCopy(z_streamp dest, z_streamp source) {
1328
0
    struct inflate_state FAR *state;
1329
0
    struct inflate_state FAR *copy;
1330
0
    unsigned char FAR *window;
1331
1332
    /* check input */
1333
0
    if (inflateStateCheck(source) || dest == Z_NULL)
1334
0
        return Z_STREAM_ERROR;
1335
0
    state = (struct inflate_state FAR *)source->state;
1336
1337
    /* allocate space */
1338
0
    copy = (struct inflate_state FAR *)
1339
0
           ZALLOC(source, 1, sizeof(struct inflate_state));
1340
0
    if (copy == Z_NULL) return Z_MEM_ERROR;
1341
0
    window = Z_NULL;
1342
0
    if (state->window != Z_NULL) {
1343
0
        window = (unsigned char FAR *)
1344
0
                 ZALLOC(source, 1U << state->wbits, sizeof(unsigned char));
1345
0
        if (window == Z_NULL) {
1346
0
            ZFREE(source, copy);
1347
0
            return Z_MEM_ERROR;
1348
0
        }
1349
0
    }
1350
1351
    /* copy state */
1352
0
    zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));
1353
0
    zmemcpy((voidpf)copy, (voidpf)state, sizeof(struct inflate_state));
1354
0
    copy->strm = dest;
1355
0
    if (state->lencode >= state->codes &&
1356
0
        state->lencode <= state->codes + ENOUGH - 1) {
1357
0
        copy->lencode = copy->codes + (state->lencode - state->codes);
1358
0
        copy->distcode = copy->codes + (state->distcode - state->codes);
1359
0
    }
1360
0
    copy->next = copy->codes + (state->next - state->codes);
1361
0
    if (window != Z_NULL)
1362
0
        zmemcpy(window, state->window, state->whave);
1363
0
    copy->window = window;
1364
0
    dest->state = (struct internal_state FAR *)copy;
1365
0
    return Z_OK;
1366
0
}
1367
1368
0
int ZEXPORT inflateUndermine(z_streamp strm, int subvert) {
1369
0
    struct inflate_state FAR *state;
1370
1371
0
    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1372
0
    state = (struct inflate_state FAR *)strm->state;
1373
#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
1374
    state->sane = !subvert;
1375
    return Z_OK;
1376
#else
1377
0
    (void)subvert;
1378
0
    state->sane = 1;
1379
0
    return Z_DATA_ERROR;
1380
0
#endif
1381
0
}
1382
1383
0
int ZEXPORT inflateValidate(z_streamp strm, int check) {
1384
0
    struct inflate_state FAR *state;
1385
1386
0
    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1387
0
    state = (struct inflate_state FAR *)strm->state;
1388
0
    if (check && state->wrap)
1389
0
        state->wrap |= 4;
1390
0
    else
1391
0
        state->wrap &= ~4;
1392
0
    return Z_OK;
1393
0
}
1394
1395
0
long ZEXPORT inflateMark(z_streamp strm) {
1396
0
    struct inflate_state FAR *state;
1397
1398
0
    if (inflateStateCheck(strm))
1399
0
        return -(1L << 16);
1400
0
    state = (struct inflate_state FAR *)strm->state;
1401
0
    return (long)(((unsigned long)((long)state->back)) << 16) +
1402
0
        (state->mode == COPY ? state->length :
1403
0
            (state->mode == MATCH ? state->was - state->length : 0));
1404
0
}
1405
1406
0
unsigned long ZEXPORT inflateCodesUsed(z_streamp strm) {
1407
0
    struct inflate_state FAR *state;
1408
0
    if (inflateStateCheck(strm)) return (unsigned long)-1;
1409
0
    state = (struct inflate_state FAR *)strm->state;
1410
0
    return (unsigned long)(state->next - state->codes);
1411
0
}