Coverage Report

Created: 2026-11-12 07:35

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