Coverage Report

Created: 2026-10-28 07:09

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
12.6M
local int inflateStateCheck(z_streamp strm) {
89
12.6M
    struct inflate_state FAR *state;
90
12.6M
    if (strm == Z_NULL ||
91
12.6M
        strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0)
92
0
        return 1;
93
12.6M
    state = (struct inflate_state FAR *)strm->state;
94
12.6M
    if (state == Z_NULL || state->strm != strm ||
95
12.5M
        state->mode < HEAD || state->mode > SYNC)
96
36.3k
        return 1;
97
12.5M
    return 0;
98
12.6M
}
99
100
1.70M
int ZEXPORT inflateResetKeep(z_streamp strm) {
101
1.70M
    struct inflate_state FAR *state;
102
103
1.70M
    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
104
1.70M
    state = (struct inflate_state FAR *)strm->state;
105
1.70M
    strm->total_in = strm->total_out = state->total = 0;
106
1.70M
    strm->msg = Z_NULL;
107
1.70M
    strm->data_type = 0;
108
1.70M
    if (state->wrap)        /* to support ill-conceived Java test suite */
109
1.67M
        strm->adler = state->wrap & 1;
110
1.70M
    state->mode = HEAD;
111
1.70M
    state->last = 0;
112
1.70M
    state->havedict = 0;
113
1.70M
    state->flags = -1;
114
1.70M
    state->dmax = 32768U;
115
1.70M
    state->head = Z_NULL;
116
1.70M
    state->hold = 0;
117
1.70M
    state->bits = 0;
118
1.70M
    state->lencode = state->distcode = state->next = state->codes;
119
1.70M
    state->sane = 1;
120
1.70M
    state->back = -1;
121
1.70M
    Tracev((stderr, "inflate: reset\n"));
122
1.70M
    return Z_OK;
123
1.70M
}
124
125
1.70M
int ZEXPORT inflateReset(z_streamp strm) {
126
1.70M
    struct inflate_state FAR *state;
127
128
1.70M
    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
129
1.70M
    state = (struct inflate_state FAR *)strm->state;
130
1.70M
    if (state->wbits != state->reqbits) {
131
0
        if (state->window != Z_NULL) {
132
0
            ZFREE(strm, state->window);
133
0
            state->window = Z_NULL;
134
0
        }
135
0
        state->wbits = state->reqbits;
136
0
    }
137
1.70M
    state->wrap = state->reqwrap;
138
1.70M
    state->wsize = 0;
139
1.70M
    state->whave = 0;
140
1.70M
    state->wnext = 0;
141
1.70M
    return inflateResetKeep(strm);
142
1.70M
}
143
144
1.70M
int ZEXPORT inflateReset2(z_streamp strm, int windowBits) {
145
1.70M
    int wrap;
146
1.70M
    struct inflate_state FAR *state;
147
148
    /* get the state */
149
1.70M
    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
150
1.70M
    state = (struct inflate_state FAR *)strm->state;
151
152
    /* extract wrap request from windowBits parameter */
153
1.70M
    if (windowBits < 0) {
154
29.6k
        if (windowBits < -15)
155
0
            return Z_STREAM_ERROR;
156
29.6k
        wrap = 0;
157
29.6k
        windowBits = -windowBits;
158
29.6k
    }
159
1.67M
    else {
160
1.67M
        wrap = (windowBits >> 4) + 5;
161
1.67M
#ifdef GUNZIP
162
1.67M
        if (windowBits < 48)
163
1.67M
            windowBits &= 15;
164
1.67M
#endif
165
1.67M
    }
166
167
    /* set number of window bits, free window if different */
168
1.70M
    if (windowBits && (windowBits < 8 || windowBits > 15))
169
0
        return Z_STREAM_ERROR;
170
1.70M
    if (state->window != Z_NULL && state->wbits != (unsigned)windowBits) {
171
0
        ZFREE(strm, state->window);
172
0
        state->window = Z_NULL;
173
0
    }
174
175
    /* update state and reset the rest of it */
176
1.70M
    state->reqwrap = state->wrap = wrap;
177
1.70M
    state->reqbits = state->wbits = (unsigned)windowBits;
178
1.70M
    return inflateReset(strm);
179
1.70M
}
180
181
int ZEXPORT inflateInit2_(z_streamp strm, int windowBits,
182
1.69M
                          const char *version, int stream_size) {
183
1.69M
    int ret;
184
1.69M
    struct inflate_state FAR *state;
185
186
1.69M
    if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
187
1.69M
        stream_size != (int)(sizeof(z_stream)))
188
0
        return Z_VERSION_ERROR;
189
1.69M
    if (strm == Z_NULL) return Z_STREAM_ERROR;
190
1.69M
    strm->msg = Z_NULL;                 /* in case we return an error */
191
1.69M
    if (strm->zalloc == (alloc_func)0) {
192
#ifdef Z_SOLO
193
        return Z_STREAM_ERROR;
194
#else
195
803k
        strm->zalloc = zcalloc;
196
803k
        strm->opaque = (voidpf)0;
197
803k
#endif
198
803k
    }
199
1.69M
    if (strm->zfree == (free_func)0)
200
#ifdef Z_SOLO
201
        return Z_STREAM_ERROR;
202
#else
203
803k
        strm->zfree = zcfree;
204
1.69M
#endif
205
1.69M
    state = (struct inflate_state FAR *)
206
1.69M
            ZALLOC(strm, 1, sizeof(struct inflate_state));
207
1.69M
    if (state == Z_NULL) return Z_MEM_ERROR;
208
1.69M
    zmemzero(state, sizeof(struct inflate_state));
209
1.69M
    Tracev((stderr, "inflate: allocated\n"));
210
1.69M
    strm->state = (struct internal_state FAR *)state;
211
1.69M
    state->strm = strm;
212
1.69M
    state->window = Z_NULL;
213
1.69M
    state->mode = HEAD;     /* to pass state test in inflateReset2() */
214
1.69M
    ret = inflateReset2(strm, windowBits);
215
1.69M
    if (ret != Z_OK) {
216
0
        ZFREE(strm, state);
217
0
        strm->state = Z_NULL;
218
0
    }
219
1.69M
    return ret;
220
1.69M
}
221
222
int ZEXPORT inflateInit_(z_streamp strm, const char *version,
223
770k
                         int stream_size) {
224
770k
    return inflateInit2_(strm, DEF_WBITS, version, stream_size);
225
770k
}
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
4.09M
local int updatewindow(z_streamp strm, const Bytef *end, unsigned copy) {
261
4.09M
    struct inflate_state FAR *state;
262
4.09M
    unsigned dist;
263
264
4.09M
    state = (struct inflate_state FAR *)strm->state;
265
266
    /* if it hasn't been done already, allocate space for the window */
267
4.09M
    if (state->window == Z_NULL) {
268
46.5k
        state->window = (unsigned char FAR *)
269
46.5k
                        ZALLOC(strm, 1U << state->wbits,
270
46.5k
                               sizeof(unsigned char));
271
46.5k
        if (state->window == Z_NULL) return 1;
272
46.5k
    }
273
274
    /* if window not in use yet, initialize */
275
4.09M
    if (state->wsize == 0) {
276
55.0k
        state->wsize = 1U << state->wbits;
277
55.0k
        state->wnext = 0;
278
55.0k
        state->whave = 0;
279
55.0k
    }
280
281
    /* copy state->wsize or less output bytes into the circular window */
282
4.09M
    if (copy >= state->wsize) {
283
3.43k
        zmemcpy(state->window, end - state->wsize, state->wsize);
284
3.43k
        state->wnext = 0;
285
3.43k
        state->whave = state->wsize;
286
3.43k
    }
287
4.09M
    else {
288
4.09M
        dist = state->wsize - state->wnext;
289
4.09M
        if (dist > copy) dist = copy;
290
4.09M
        zmemcpy(state->window + state->wnext, end - copy, dist);
291
4.09M
        copy -= dist;
292
4.09M
        if (copy) {
293
59.3k
            zmemcpy(state->window, end - copy, copy);
294
59.3k
            state->wnext = copy;
295
59.3k
            state->whave = state->wsize;
296
59.3k
        }
297
4.03M
        else {
298
4.03M
            state->wnext += dist;
299
4.03M
            if (state->wnext == state->wsize) state->wnext = 0;
300
4.03M
            if (state->whave < state->wsize) state->whave += dist;
301
4.03M
        }
302
4.09M
    }
303
4.09M
    return 0;
304
4.09M
}
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
4.62M
    (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
889k
    do { \
320
889k
        hbuf[0] = (unsigned char)(word); \
321
889k
        hbuf[1] = (unsigned char)((word) >> 8); \
322
889k
        check = crc32(check, hbuf, 2); \
323
889k
    } while (0)
324
325
#  define CRC4(check, word) \
326
1.87k
    do { \
327
1.87k
        hbuf[0] = (unsigned char)(word); \
328
1.87k
        hbuf[1] = (unsigned char)((word) >> 8); \
329
1.87k
        hbuf[2] = (unsigned char)((word) >> 16); \
330
1.87k
        hbuf[3] = (unsigned char)((word) >> 24); \
331
1.87k
        check = crc32(check, hbuf, 4); \
332
1.87k
    } while (0)
333
#endif
334
335
/* Load registers with state in inflate() for speed */
336
#define LOAD() \
337
9.83M
    do { \
338
9.83M
        put = strm->next_out; \
339
9.83M
        left = strm->avail_out; \
340
9.83M
        next = strm->next_in; \
341
9.83M
        have = strm->avail_in; \
342
9.83M
        hold = state->hold; \
343
9.83M
        bits = state->bits; \
344
9.83M
    } while (0)
345
346
/* Restore state from registers in inflate() */
347
#define RESTORE() \
348
9.83M
    do { \
349
9.83M
        strm->next_out = put; \
350
9.83M
        strm->avail_out = left; \
351
9.83M
        strm->next_in = next; \
352
9.83M
        strm->avail_in = have; \
353
9.83M
        state->hold = hold; \
354
9.83M
        state->bits = bits; \
355
9.83M
    } while (0)
356
357
/* Clear the input bit accumulator */
358
#define INITBITS() \
359
6.83M
    do { \
360
6.83M
        hold = 0; \
361
6.83M
        bits = 0; \
362
6.83M
    } 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
111M
    do { \
368
111M
        if (have == 0) goto inf_leave; \
369
111M
        have--; \
370
111M
        hold += (unsigned long)(*next++) << bits; \
371
111M
        bits += 8; \
372
111M
    } 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
41.9M
    do { \
378
83.6M
        while (bits < (unsigned)(n)) \
379
41.9M
            PULLBYTE(); \
380
41.9M
    } while (0)
381
382
/* Return the low n bits of the bit accumulator (n < 16) */
383
#define BITS(n) \
384
261M
    ((unsigned)hold & ((1U << (n)) - 1))
385
386
/* Remove n bits from the bit accumulator */
387
#define DROPBITS(n) \
388
189M
    do { \
389
189M
        hold >>= (n); \
390
189M
        bits -= (unsigned)(n); \
391
189M
    } while (0)
392
393
/* Remove zero to seven bits as needed to go to a byte boundary */
394
#define BYTEBITS() \
395
1.65M
    do { \
396
1.65M
        hold >>= bits & 7; \
397
1.65M
        bits -= bits & 7; \
398
1.65M
    } 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
5.75M
int ZEXPORT inflate(z_streamp strm, int flush) {
483
5.75M
    struct inflate_state FAR *state;
484
5.75M
    z_const unsigned char FAR *next;    /* next input */
485
5.75M
    unsigned char FAR *put;     /* next output */
486
5.75M
    unsigned have, left;        /* available input and output */
487
5.75M
    unsigned long hold;         /* bit buffer */
488
5.75M
    unsigned bits;              /* bits in bit buffer */
489
5.75M
    unsigned in, out;           /* save starting available input and output */
490
5.75M
    unsigned copy;              /* number of stored or match bytes to copy */
491
5.75M
    unsigned char FAR *from;    /* where to copy match bytes from */
492
5.75M
    code here;                  /* current decoding table entry */
493
5.75M
    code last;                  /* parent table entry */
494
5.75M
    unsigned len;               /* length to copy for repeats, bits to drop */
495
5.75M
    int ret;                    /* return code */
496
5.75M
#ifdef GUNZIP
497
5.75M
    unsigned char hbuf[4];      /* buffer for gzip header crc calculation */
498
5.75M
#endif
499
5.75M
    static const unsigned short order[19] = /* permutation of code lengths */
500
5.75M
        {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
501
502
5.75M
    if (inflateStateCheck(strm) || strm->next_out == Z_NULL ||
503
5.75M
        (strm->next_in == Z_NULL && strm->avail_in != 0))
504
0
        return Z_STREAM_ERROR;
505
506
5.75M
    state = (struct inflate_state FAR *)strm->state;
507
5.75M
    if (state->mode == TYPE) state->mode = TYPEDO;      /* skip check */
508
5.75M
    LOAD();
509
5.75M
    in = have;
510
5.75M
    out = left;
511
5.75M
    ret = Z_OK;
512
5.75M
    for (;;)
513
65.9M
        switch (state->mode) {
514
1.70M
        case HEAD:
515
1.70M
            if (state->wrap == 0) {
516
28.6k
                state->mode = TYPEDO;
517
28.6k
                break;
518
28.6k
            }
519
1.67M
            NEEDBITS(16);
520
1.67M
#ifdef GUNZIP
521
1.67M
            if ((state->wrap & 2) && hold == 0x8b1f) {  /* gzip header */
522
884k
                if (state->wbits == 0)
523
0
                    state->wbits = 15;
524
884k
                state->check = crc32(0L, Z_NULL, 0);
525
884k
                CRC2(state->check, hold);
526
884k
                INITBITS();
527
884k
                state->mode = FLAGS;
528
884k
                break;
529
884k
            }
530
791k
            if (state->head != Z_NULL)
531
0
                state->head->done = -1;
532
791k
            if (!(state->wrap & 1) ||   /* check if zlib header allowed */
533
#else
534
            if (
535
#endif
536
791k
                ((BITS(8) << 8) + (hold >> 8)) % 31) {
537
4.52k
                strm->msg = (z_const char *)"incorrect header check";
538
4.52k
                state->mode = BAD;
539
4.52k
                break;
540
4.52k
            }
541
787k
            if (BITS(4) != Z_DEFLATED) {
542
1.60k
                strm->msg = (z_const char *)"unknown compression method";
543
1.60k
                state->mode = BAD;
544
1.60k
                break;
545
1.60k
            }
546
785k
            DROPBITS(4);
547
785k
            len = BITS(4) + 8;
548
785k
            if (state->wbits == 0)
549
0
                state->wbits = len;
550
785k
            if (len > 15 || len > state->wbits) {
551
486
                strm->msg = (z_const char *)"invalid window size";
552
486
                state->mode = BAD;
553
486
                break;
554
486
            }
555
785k
            state->dmax = 1U << len;
556
785k
            state->flags = 0;               /* indicate zlib header */
557
785k
            Tracev((stderr, "inflate:   zlib header ok\n"));
558
785k
            strm->adler = state->check = adler32(0L, Z_NULL, 0);
559
785k
            state->mode = hold & 0x200 ? DICTID : TYPE;
560
785k
            INITBITS();
561
785k
            break;
562
0
#ifdef GUNZIP
563
885k
        case FLAGS:
564
885k
            NEEDBITS(16);
565
884k
            state->flags = (int)(hold);
566
884k
            if ((state->flags & 0xff) != Z_DEFLATED) {
567
61
                strm->msg = (z_const char *)"unknown compression method";
568
61
                state->mode = BAD;
569
61
                break;
570
61
            }
571
884k
            if (state->flags & 0xe000) {
572
38
                strm->msg = (z_const char *)"unknown header flags set";
573
38
                state->mode = BAD;
574
38
                break;
575
38
            }
576
884k
            if (state->head != Z_NULL)
577
0
                state->head->text = (int)((hold >> 8) & 1);
578
884k
            if ((state->flags & 0x0200) && (state->wrap & 4))
579
1.88k
                CRC2(state->check, hold);
580
884k
            INITBITS();
581
884k
            state->mode = TIME;
582
                /* fallthrough */
583
885k
        case TIME:
584
885k
            NEEDBITS(32);
585
884k
            if (state->head != Z_NULL)
586
0
                state->head->time = hold;
587
884k
            if ((state->flags & 0x0200) && (state->wrap & 4))
588
1.87k
                CRC4(state->check, hold);
589
884k
            INITBITS();
590
884k
            state->mode = OS;
591
                /* fallthrough */
592
884k
        case OS:
593
884k
            NEEDBITS(16);
594
884k
            if (state->head != Z_NULL) {
595
0
                state->head->xflags = (int)(hold & 0xff);
596
0
                state->head->os = (int)(hold >> 8);
597
0
            }
598
884k
            if ((state->flags & 0x0200) && (state->wrap & 4))
599
1.84k
                CRC2(state->check, hold);
600
884k
            INITBITS();
601
884k
            state->mode = EXLEN;
602
                /* fallthrough */
603
884k
        case EXLEN:
604
884k
            if (state->flags & 0x0400) {
605
10.2k
                NEEDBITS(16);
606
10.2k
                state->length = (unsigned)(hold);
607
10.2k
                if (state->head != Z_NULL)
608
0
                    state->head->extra_len = (unsigned)hold;
609
10.2k
                if ((state->flags & 0x0200) && (state->wrap & 4))
610
1.47k
                    CRC2(state->check, hold);
611
10.2k
                INITBITS();
612
10.2k
            }
613
874k
            else if (state->head != Z_NULL)
614
0
                state->head->extra = Z_NULL;
615
884k
            state->mode = EXTRA;
616
                /* fallthrough */
617
884k
        case EXTRA:
618
884k
            if (state->flags & 0x0400) {
619
10.3k
                copy = state->length;
620
10.3k
                if (copy > have) copy = have;
621
10.3k
                if (copy) {
622
1.07k
                    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
1.07k
                    if ((state->flags & 0x0200) && (state->wrap & 4))
631
545
                        state->check = crc32(state->check, next, copy);
632
1.07k
                    have -= copy;
633
1.07k
                    next += copy;
634
1.07k
                    state->length -= copy;
635
1.07k
                }
636
10.3k
                if (state->length) goto inf_leave;
637
10.3k
            }
638
884k
            state->length = 0;
639
884k
            state->mode = NAME;
640
                /* fallthrough */
641
885k
        case NAME:
642
885k
            if (state->flags & 0x0800) {
643
877k
                if (have == 0) goto inf_leave;
644
877k
                copy = 0;
645
20.8M
                do {
646
20.8M
                    len = (unsigned)(next[copy++]);
647
20.8M
                    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
20.8M
                } while (len && copy < have);
652
877k
                if ((state->flags & 0x0200) && (state->wrap & 4))
653
1.64k
                    state->check = crc32(state->check, next, copy);
654
877k
                have -= copy;
655
877k
                next += copy;
656
877k
                if (len) goto inf_leave;
657
877k
            }
658
8.29k
            else if (state->head != Z_NULL)
659
0
                state->head->name = Z_NULL;
660
883k
            state->length = 0;
661
883k
            state->mode = COMMENT;
662
                /* fallthrough */
663
884k
        case COMMENT:
664
884k
            if (state->flags & 0x1000) {
665
869k
                if (have == 0) goto inf_leave;
666
869k
                copy = 0;
667
7.68M
                do {
668
7.68M
                    len = (unsigned)(next[copy++]);
669
7.68M
                    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
7.68M
                } while (len && copy < have);
674
869k
                if ((state->flags & 0x0200) && (state->wrap & 4))
675
1.49k
                    state->check = crc32(state->check, next, copy);
676
869k
                have -= copy;
677
869k
                next += copy;
678
869k
                if (len) goto inf_leave;
679
869k
            }
680
15.5k
            else if (state->head != Z_NULL)
681
0
                state->head->comment = Z_NULL;
682
883k
            state->mode = HCRC;
683
                /* fallthrough */
684
883k
        case HCRC:
685
883k
            if (state->flags & 0x0200) {
686
1.43k
                NEEDBITS(16);
687
1.32k
                if ((state->wrap & 4) && hold != (state->check & 0xffff)) {
688
148
                    strm->msg = (z_const char *)"header crc mismatch";
689
148
                    state->mode = BAD;
690
148
                    break;
691
148
                }
692
1.17k
                INITBITS();
693
1.17k
            }
694
883k
            if (state->head != Z_NULL) {
695
0
                state->head->hcrc = (int)((state->flags >> 9) & 1);
696
0
                state->head->done = 1;
697
0
            }
698
883k
            strm->adler = state->check = crc32(0L, Z_NULL, 0);
699
883k
            state->mode = TYPE;
700
883k
            break;
701
0
#endif
702
625
        case DICTID:
703
625
            NEEDBITS(32);
704
512
            strm->adler = state->check = ZSWAP32(hold);
705
512
            INITBITS();
706
512
            state->mode = DICT;
707
                /* fallthrough */
708
512
        case DICT:
709
512
            if (state->havedict == 0) {
710
512
                RESTORE();
711
512
                return Z_NEED_DICT;
712
512
            }
713
0
            strm->adler = state->check = adler32(0L, Z_NULL, 0);
714
0
            state->mode = TYPE;
715
                /* fallthrough */
716
3.39M
        case TYPE:
717
3.39M
            if (flush == Z_BLOCK || flush == Z_TREES) goto inf_leave;
718
                /* fallthrough */
719
3.42M
        case TYPEDO:
720
3.42M
            if (state->last) {
721
1.61M
                BYTEBITS();
722
1.61M
                state->mode = CHECK;
723
1.61M
                break;
724
1.61M
            }
725
1.81M
            NEEDBITS(3);
726
1.81M
            state->last = BITS(1);
727
1.81M
            DROPBITS(1);
728
1.81M
            switch (BITS(2)) {
729
43.2k
            case 0:                             /* stored block */
730
43.2k
                Tracev((stderr, "inflate:     stored block%s\n",
731
43.2k
                        state->last ? " (last)" : ""));
732
43.2k
                state->mode = STORED;
733
43.2k
                break;
734
944k
            case 1:                             /* fixed block */
735
944k
                inflate_fixed(state);
736
944k
                Tracev((stderr, "inflate:     fixed codes block%s\n",
737
944k
                        state->last ? " (last)" : ""));
738
944k
                state->mode = LEN_;             /* decode codes */
739
944k
                if (flush == Z_TREES) {
740
0
                    DROPBITS(2);
741
0
                    goto inf_leave;
742
0
                }
743
944k
                break;
744
944k
            case 2:                             /* dynamic block */
745
822k
                Tracev((stderr, "inflate:     dynamic codes block%s\n",
746
822k
                        state->last ? " (last)" : ""));
747
822k
                state->mode = TABLE;
748
822k
                break;
749
906
            default:
750
906
                strm->msg = (z_const char *)"invalid block type";
751
906
                state->mode = BAD;
752
1.81M
            }
753
1.81M
            DROPBITS(2);
754
1.81M
            break;
755
43.3k
        case STORED:
756
43.3k
            BYTEBITS();                         /* go to byte boundary */
757
43.3k
            NEEDBITS(32);
758
42.4k
            if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
759
6.92k
                strm->msg = (z_const char *)"invalid stored block lengths";
760
6.92k
                state->mode = BAD;
761
6.92k
                break;
762
6.92k
            }
763
35.4k
            state->length = (unsigned)hold & 0xffff;
764
35.4k
            Tracev((stderr, "inflate:       stored length %u\n",
765
35.4k
                    state->length));
766
35.4k
            INITBITS();
767
35.4k
            state->mode = COPY_;
768
35.4k
            if (flush == Z_TREES) goto inf_leave;
769
                /* fallthrough */
770
35.4k
        case COPY_:
771
35.4k
            state->mode = COPY;
772
                /* fallthrough */
773
65.7k
        case COPY:
774
65.7k
            copy = state->length;
775
65.7k
            if (copy) {
776
38.7k
                if (copy > have) copy = have;
777
38.7k
                if (copy > left) copy = left;
778
38.7k
                if (copy == 0) goto inf_leave;
779
21.4k
                zmemcpy(put, next, copy);
780
21.4k
                have -= copy;
781
21.4k
                next += copy;
782
21.4k
                left -= copy;
783
21.4k
                put += copy;
784
21.4k
                state->length -= copy;
785
21.4k
                break;
786
38.7k
            }
787
26.9k
            Tracev((stderr, "inflate:       stored end\n"));
788
26.9k
            state->mode = TYPE;
789
26.9k
            break;
790
823k
        case TABLE:
791
823k
            NEEDBITS(14);
792
821k
            state->nlen = BITS(5) + 257;
793
821k
            DROPBITS(5);
794
821k
            state->ndist = BITS(5) + 1;
795
821k
            DROPBITS(5);
796
821k
            state->ncode = BITS(4) + 4;
797
821k
            DROPBITS(4);
798
821k
#ifndef PKZIP_BUG_WORKAROUND
799
821k
            if (state->nlen > 286 || state->ndist > 30) {
800
685
                strm->msg = (z_const char *)
801
685
                    "too many length or distance symbols";
802
685
                state->mode = BAD;
803
685
                break;
804
685
            }
805
821k
#endif
806
821k
            Tracev((stderr, "inflate:       table sizes ok\n"));
807
821k
            state->have = 0;
808
821k
            state->mode = LENLENS;
809
                /* fallthrough */
810
821k
        case LENLENS:
811
12.3M
            while (state->have < state->ncode) {
812
11.5M
                NEEDBITS(3);
813
11.5M
                state->lens[order[state->have++]] = (unsigned short)BITS(3);
814
11.5M
                DROPBITS(3);
815
11.5M
            }
816
4.64M
            while (state->have < 19)
817
3.84M
                state->lens[order[state->have++]] = 0;
818
800k
            state->next = state->codes;
819
800k
            state->lencode = state->distcode = (const code FAR *)(state->next);
820
800k
            state->lenbits = 7;
821
800k
            ret = inflate_table(CODES, state->lens, 19, &(state->next),
822
800k
                                &(state->lenbits), state->work);
823
800k
            if (ret) {
824
2.63k
                strm->msg = (z_const char *)"invalid code lengths set";
825
2.63k
                state->mode = BAD;
826
2.63k
                break;
827
2.63k
            }
828
797k
            Tracev((stderr, "inflate:       code lengths ok\n"));
829
797k
            state->have = 0;
830
797k
            state->mode = CODELENS;
831
                /* fallthrough */
832
798k
        case CODELENS:
833
112M
            while (state->have < state->nlen + state->ndist) {
834
156M
                for (;;) {
835
156M
                    here = state->lencode[BITS(state->lenbits)];
836
156M
                    if ((unsigned)(here.bits) <= bits) break;
837
44.8M
                    PULLBYTE();
838
44.8M
                }
839
111M
                if (here.val < 16) {
840
104M
                    DROPBITS(here.bits);
841
104M
                    state->lens[state->have++] = here.val;
842
104M
                }
843
7.27M
                else {
844
7.27M
                    if (here.val == 16) {
845
2.53M
                        NEEDBITS(here.bits + 2);
846
2.53M
                        DROPBITS(here.bits);
847
2.53M
                        if (state->have == 0) {
848
400
                            strm->msg = (z_const char *)
849
400
                                "invalid bit length repeat";
850
400
                            state->mode = BAD;
851
400
                            break;
852
400
                        }
853
2.53M
                        len = state->lens[state->have - 1];
854
2.53M
                        copy = 3 + BITS(2);
855
2.53M
                        DROPBITS(2);
856
2.53M
                    }
857
4.73M
                    else if (here.val == 17) {
858
2.78M
                        NEEDBITS(here.bits + 3);
859
2.78M
                        DROPBITS(here.bits);
860
2.78M
                        len = 0;
861
2.78M
                        copy = 3 + BITS(3);
862
2.78M
                        DROPBITS(3);
863
2.78M
                    }
864
1.94M
                    else {
865
1.94M
                        NEEDBITS(here.bits + 7);
866
1.94M
                        DROPBITS(here.bits);
867
1.94M
                        len = 0;
868
1.94M
                        copy = 11 + BITS(7);
869
1.94M
                        DROPBITS(7);
870
1.94M
                    }
871
7.26M
                    if (state->have + copy > state->nlen + state->ndist) {
872
843
                        strm->msg = (z_const char *)
873
843
                            "invalid bit length repeat";
874
843
                        state->mode = BAD;
875
843
                        break;
876
843
                    }
877
149M
                    while (copy--)
878
142M
                        state->lens[state->have++] = (unsigned short)len;
879
7.26M
                }
880
111M
            }
881
882
            /* handle error breaks in while */
883
795k
            if (state->mode == BAD) break;
884
885
            /* check for end-of-block code (better have one) */
886
793k
            if (state->lens[256] == 0) {
887
781
                strm->msg = (z_const char *)
888
781
                    "invalid code -- missing end-of-block";
889
781
                state->mode = BAD;
890
781
                break;
891
781
            }
892
893
            /* build code tables -- note: do not change the lenbits or distbits
894
               values here (9 and 6) without reading the comments in inftrees.h
895
               concerning the ENOUGH constants, which depend on those values */
896
793k
            state->next = state->codes;
897
793k
            state->lencode = (const code FAR *)(state->next);
898
793k
            state->lenbits = 9;
899
793k
            ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
900
793k
                                &(state->lenbits), state->work);
901
793k
            if (ret) {
902
1.04k
                strm->msg = (z_const char *)"invalid literal/lengths set";
903
1.04k
                state->mode = BAD;
904
1.04k
                break;
905
1.04k
            }
906
791k
            state->distcode = (const code FAR *)(state->next);
907
791k
            state->distbits = 6;
908
791k
            ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
909
791k
                            &(state->next), &(state->distbits), state->work);
910
791k
            if (ret) {
911
758
                strm->msg = (z_const char *)"invalid distances set";
912
758
                state->mode = BAD;
913
758
                break;
914
758
            }
915
791k
            Tracev((stderr, "inflate:       codes ok\n"));
916
791k
            state->mode = LEN_;
917
791k
            if (flush == Z_TREES) goto inf_leave;
918
                /* fallthrough */
919
1.73M
        case LEN_:
920
1.73M
            state->mode = LEN;
921
                /* fallthrough */
922
31.2M
        case LEN:
923
31.2M
            if (have >= 6 && left >= 258 && bits < 8) {
924
4.08M
                RESTORE();
925
4.08M
                inflate_fast(strm, out);
926
4.08M
                LOAD();
927
4.08M
                if (state->mode == TYPE)
928
964k
                    state->back = -1;
929
4.08M
                break;
930
4.08M
            }
931
27.1M
            state->back = 0;
932
47.1M
            for (;;) {
933
47.1M
                here = state->lencode[BITS(state->lenbits)];
934
47.1M
                if ((unsigned)(here.bits) <= bits) break;
935
19.9M
                PULLBYTE();
936
19.9M
            }
937
27.1M
            if (here.op && (here.op & 0xf0) == 0) {
938
933k
                last = here;
939
1.06M
                for (;;) {
940
1.06M
                    here = state->lencode[last.val +
941
1.06M
                            (BITS(last.bits + last.op) >> last.bits)];
942
1.06M
                    if ((unsigned)(last.bits + here.bits) <= bits) break;
943
129k
                    PULLBYTE();
944
129k
                }
945
932k
                DROPBITS(last.bits);
946
932k
                state->back += last.bits;
947
932k
            }
948
27.1M
            DROPBITS(here.bits);
949
27.1M
            state->back += here.bits;
950
27.1M
            state->length = (unsigned)here.val;
951
27.1M
            if ((int)(here.op) == 0) {
952
15.6M
                Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
953
15.6M
                        "inflate:         literal '%c'\n" :
954
15.6M
                        "inflate:         literal 0x%02x\n", here.val));
955
15.6M
                state->mode = LIT;
956
15.6M
                break;
957
15.6M
            }
958
11.5M
            if (here.op & 32) {
959
739k
                Tracevv((stderr, "inflate:         end of block\n"));
960
739k
                state->back = -1;
961
739k
                state->mode = TYPE;
962
739k
                break;
963
739k
            }
964
10.7M
            if (here.op & 64) {
965
603
                strm->msg = (z_const char *)"invalid literal/length code";
966
603
                state->mode = BAD;
967
603
                break;
968
603
            }
969
10.7M
            state->extra = (unsigned)(here.op) & 15;
970
10.7M
            state->mode = LENEXT;
971
                /* fallthrough */
972
10.7M
        case LENEXT:
973
10.7M
            if (state->extra) {
974
4.69M
                NEEDBITS(state->extra);
975
4.69M
                state->length += BITS(state->extra);
976
4.69M
                DROPBITS(state->extra);
977
4.69M
                state->back += state->extra;
978
4.69M
            }
979
10.7M
            Tracevv((stderr, "inflate:         length %u\n", state->length));
980
10.7M
            state->was = state->length;
981
10.7M
            state->mode = DIST;
982
                /* fallthrough */
983
10.7M
        case DIST:
984
15.5M
            for (;;) {
985
15.5M
                here = state->distcode[BITS(state->distbits)];
986
15.5M
                if ((unsigned)(here.bits) <= bits) break;
987
4.78M
                PULLBYTE();
988
4.78M
            }
989
10.7M
            if ((here.op & 0xf0) == 0) {
990
214k
                last = here;
991
294k
                for (;;) {
992
294k
                    here = state->distcode[last.val +
993
294k
                            (BITS(last.bits + last.op) >> last.bits)];
994
294k
                    if ((unsigned)(last.bits + here.bits) <= bits) break;
995
81.0k
                    PULLBYTE();
996
81.0k
                }
997
213k
                DROPBITS(last.bits);
998
213k
                state->back += last.bits;
999
213k
            }
1000
10.7M
            DROPBITS(here.bits);
1001
10.7M
            state->back += here.bits;
1002
10.7M
            if (here.op & 64) {
1003
732
                strm->msg = (z_const char *)"invalid distance code";
1004
732
                state->mode = BAD;
1005
732
                break;
1006
732
            }
1007
10.7M
            state->offset = (unsigned)here.val;
1008
10.7M
            state->extra = (unsigned)(here.op) & 15;
1009
10.7M
            state->mode = DISTEXT;
1010
                /* fallthrough */
1011
10.7M
        case DISTEXT:
1012
10.7M
            if (state->extra) {
1013
8.88M
                NEEDBITS(state->extra);
1014
8.87M
                state->offset += BITS(state->extra);
1015
8.87M
                DROPBITS(state->extra);
1016
8.87M
                state->back += state->extra;
1017
8.87M
            }
1018
#ifdef INFLATE_STRICT
1019
            if (state->offset > state->dmax) {
1020
                strm->msg = (z_const char *)"invalid distance too far back";
1021
                state->mode = BAD;
1022
                break;
1023
            }
1024
#endif
1025
10.7M
            Tracevv((stderr, "inflate:         distance %u\n", state->offset));
1026
10.7M
            state->mode = MATCH;
1027
                /* fallthrough */
1028
22.0M
        case MATCH:
1029
22.0M
            if (left == 0) goto inf_leave;
1030
18.0M
            copy = out - left;
1031
18.0M
            if (state->offset > copy) {         /* copy from window */
1032
4.18M
                copy = state->offset - copy;
1033
4.18M
                if (copy > state->whave) {
1034
3.07k
                    if (state->sane) {
1035
3.07k
                        strm->msg = (z_const char *)
1036
3.07k
                            "invalid distance too far back";
1037
3.07k
                        state->mode = BAD;
1038
3.07k
                        break;
1039
3.07k
                    }
1040
#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
1041
                    Trace((stderr, "inflate.c too far\n"));
1042
                    copy -= state->whave;
1043
                    if (copy > state->length) copy = state->length;
1044
                    if (copy > left) copy = left;
1045
                    left -= copy;
1046
                    state->length -= copy;
1047
                    do {
1048
                        *put++ = 0;
1049
                    } while (--copy);
1050
                    if (state->length == 0) state->mode = LEN;
1051
                    break;
1052
#endif
1053
3.07k
                }
1054
4.17M
                if (copy > state->wnext) {
1055
403k
                    copy -= state->wnext;
1056
403k
                    from = state->window + (state->wsize - copy);
1057
403k
                }
1058
3.77M
                else
1059
3.77M
                    from = state->window + (state->wnext - copy);
1060
4.17M
                if (copy > state->length) copy = state->length;
1061
4.17M
            }
1062
13.8M
            else {                              /* copy from output */
1063
13.8M
                from = put - state->offset;
1064
13.8M
                copy = state->length;
1065
13.8M
            }
1066
18.0M
            if (copy > left) copy = left;
1067
18.0M
            left -= copy;
1068
18.0M
            state->length -= copy;
1069
768M
            do {
1070
768M
                *put++ = *from++;
1071
768M
            } while (--copy);
1072
18.0M
            if (state->length == 0) state->mode = LEN;
1073
18.0M
            break;
1074
15.6M
        case LIT:
1075
15.6M
            if (left == 0) goto inf_leave;
1076
15.6M
            *put++ = (unsigned char)(state->length);
1077
15.6M
            left--;
1078
15.6M
            state->mode = LEN;
1079
15.6M
            break;
1080
1.61M
        case CHECK:
1081
1.61M
            if (state->wrap) {
1082
1.60M
                NEEDBITS(32);
1083
1.59M
                out -= left;
1084
1.59M
                strm->total_out += out;
1085
1.59M
                state->total += out;
1086
1.59M
                if ((state->wrap & 4) && out)
1087
728k
                    strm->adler = state->check =
1088
728k
                        UPDATE_CHECK(state->check, put - out, out);
1089
1.59M
                out = left;
1090
1.59M
                if ((state->wrap & 4) && (
1091
1.59M
#ifdef GUNZIP
1092
1.59M
                     state->flags ? hold :
1093
1.59M
#endif
1094
1.59M
                     ZSWAP32(hold)) != state->check) {
1095
4.22k
                    strm->msg = (z_const char *)"incorrect data check";
1096
4.22k
                    state->mode = BAD;
1097
4.22k
                    break;
1098
4.22k
                }
1099
1.59M
                INITBITS();
1100
1.59M
                Tracev((stderr, "inflate:   check matches trailer\n"));
1101
1.59M
            }
1102
1.61M
#ifdef GUNZIP
1103
1.61M
            state->mode = LENGTH;
1104
                /* fallthrough */
1105
1.61M
        case LENGTH:
1106
1.61M
            if (state->wrap && state->flags) {
1107
869k
                NEEDBITS(32);
1108
869k
                if ((state->wrap & 4) && hold != (state->total & 0xffffffff)) {
1109
175
                    strm->msg = (z_const char *)"incorrect length check";
1110
175
                    state->mode = BAD;
1111
175
                    break;
1112
175
                }
1113
868k
                INITBITS();
1114
868k
                Tracev((stderr, "inflate:   length matches trailer\n"));
1115
868k
            }
1116
1.61M
#endif
1117
1.61M
            state->mode = DONE;
1118
                /* fallthrough */
1119
1.61M
        case DONE:
1120
1.61M
            ret = Z_STREAM_END;
1121
1.61M
            goto inf_leave;
1122
34.1k
        case BAD:
1123
34.1k
            ret = Z_DATA_ERROR;
1124
34.1k
            goto inf_leave;
1125
0
        case MEM:
1126
0
            return Z_MEM_ERROR;
1127
0
        case SYNC:
1128
                /* fallthrough */
1129
0
        default:
1130
0
            return Z_STREAM_ERROR;
1131
65.9M
        }
1132
1133
    /*
1134
       Return from inflate(), updating the total counts and the check value.
1135
       If there was no progress during the inflate() call, return a buffer
1136
       error.  Call updatewindow() to create and/or update the window state.
1137
       Note: a memory error from inflate() is non-recoverable.
1138
     */
1139
5.75M
  inf_leave:
1140
5.75M
    RESTORE();
1141
5.75M
    if (state->wsize || (out != strm->avail_out && state->mode < BAD &&
1142
55.0k
            (state->mode < CHECK || flush != Z_FINISH)))
1143
4.09M
        if (updatewindow(strm, strm->next_out, out - strm->avail_out)) {
1144
0
            state->mode = MEM;
1145
0
            return Z_MEM_ERROR;
1146
0
        }
1147
5.75M
    in -= strm->avail_in;
1148
5.75M
    out -= strm->avail_out;
1149
5.75M
    strm->total_in += in;
1150
5.75M
    strm->total_out += out;
1151
5.75M
    state->total += out;
1152
5.75M
    if ((state->wrap & 4) && out)
1153
3.89M
        strm->adler = state->check =
1154
3.89M
            UPDATE_CHECK(state->check, strm->next_out - out, out);
1155
5.75M
    strm->data_type = (int)state->bits + (state->last ? 64 : 0) +
1156
5.75M
                      (state->mode == TYPE ? 128 : 0) +
1157
5.75M
                      (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0);
1158
5.75M
    if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
1159
9.38k
        ret = Z_BUF_ERROR;
1160
5.75M
    return ret;
1161
5.75M
}
1162
1163
1.72M
int ZEXPORT inflateEnd(z_streamp strm) {
1164
1.72M
    struct inflate_state FAR *state;
1165
1.72M
    if (inflateStateCheck(strm))
1166
36.3k
        return Z_STREAM_ERROR;
1167
1.69M
    state = (struct inflate_state FAR *)strm->state;
1168
1.69M
    if (state->window != Z_NULL) ZFREE(strm, state->window);
1169
1.69M
    ZFREE(strm, strm->state);
1170
1.69M
    strm->state = Z_NULL;
1171
1.69M
    Tracev((stderr, "inflate: end\n"));
1172
1.69M
    return Z_OK;
1173
1.72M
}
1174
1175
int ZEXPORT inflateGetDictionary(z_streamp strm, Bytef *dictionary,
1176
0
                                 uInt *dictLength) {
1177
0
    struct inflate_state FAR *state;
1178
1179
    /* check state */
1180
0
    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1181
0
    state = (struct inflate_state FAR *)strm->state;
1182
1183
    /* copy dictionary */
1184
0
    if (state->whave && dictionary != Z_NULL) {
1185
0
        zmemcpy(dictionary, state->window + state->wnext,
1186
0
                state->whave - state->wnext);
1187
0
        zmemcpy(dictionary + state->whave - state->wnext,
1188
0
                state->window, state->wnext);
1189
0
    }
1190
0
    if (dictLength != Z_NULL)
1191
0
        *dictLength = state->whave;
1192
0
    return Z_OK;
1193
0
}
1194
1195
int ZEXPORT inflateSetDictionary(z_streamp strm, const Bytef *dictionary,
1196
0
                                 uInt dictLength) {
1197
0
    struct inflate_state FAR *state;
1198
0
    unsigned long dictid;
1199
0
    int ret;
1200
1201
    /* check state */
1202
0
    if (inflateStateCheck(strm) || dictionary == Z_NULL)
1203
0
        return Z_STREAM_ERROR;
1204
0
    state = (struct inflate_state FAR *)strm->state;
1205
0
    if (state->wrap != 0 && state->mode != DICT)
1206
0
        return Z_STREAM_ERROR;
1207
1208
    /* check for correct dictionary identifier */
1209
0
    if (state->mode == DICT) {
1210
0
        dictid = adler32(0L, Z_NULL, 0);
1211
0
        dictid = adler32(dictid, dictionary, dictLength);
1212
0
        if (dictid != state->check)
1213
0
            return Z_DATA_ERROR;
1214
0
    }
1215
1216
    /* copy dictionary to window using updatewindow(), which will amend the
1217
       existing dictionary if appropriate */
1218
0
    ret = updatewindow(strm, dictionary + dictLength, dictLength);
1219
0
    if (ret) {
1220
0
        state->mode = MEM;
1221
0
        return Z_MEM_ERROR;
1222
0
    }
1223
0
    state->havedict = 1;
1224
0
    Tracev((stderr, "inflate:   dictionary set\n"));
1225
0
    return Z_OK;
1226
0
}
1227
1228
0
int ZEXPORT inflateGetHeader(z_streamp strm, gz_headerp head) {
1229
0
    struct inflate_state FAR *state;
1230
1231
    /* check state */
1232
0
    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1233
0
    state = (struct inflate_state FAR *)strm->state;
1234
0
    if ((state->wrap & 2) == 0) return Z_STREAM_ERROR;
1235
1236
    /* save header structure */
1237
0
    state->head = head;
1238
0
    head->done = 0;
1239
0
    return Z_OK;
1240
0
}
1241
1242
/*
1243
   Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff.  Return when found
1244
   or when out of input.  When called, *have is the number of pattern bytes
1245
   found in order so far, in 0..3.  On return *have is updated to the new
1246
   state.  If on return *have equals four, then the pattern was found and the
1247
   return value is how many bytes were read including the last byte of the
1248
   pattern.  If *have is less than four, then the pattern has not been found
1249
   yet and the return value is len.  In the latter case, syncsearch() can be
1250
   called again with more data and the *have state.  *have is initialized to
1251
   zero for the first call.
1252
 */
1253
local unsigned syncsearch(unsigned FAR *have, const unsigned char FAR *buf,
1254
0
                          unsigned len) {
1255
0
    unsigned got;
1256
0
    unsigned next;
1257
1258
0
    got = *have;
1259
0
    next = 0;
1260
0
    while (next < len && got < 4) {
1261
0
        if ((int)(buf[next]) == (got < 2 ? 0 : 0xff))
1262
0
            got++;
1263
0
        else if (buf[next])
1264
0
            got = 0;
1265
0
        else
1266
0
            got = 4 - got;
1267
0
        next++;
1268
0
    }
1269
0
    *have = got;
1270
0
    return next;
1271
0
}
1272
1273
0
int ZEXPORT inflateSync(z_streamp strm) {
1274
0
    unsigned len;               /* number of bytes to look at or looked at */
1275
0
    int flags;                  /* temporary to save header status */
1276
0
    unsigned long in, out;      /* temporary to save total_in and total_out */
1277
0
    unsigned char buf[4];       /* to restore bit buffer to byte string */
1278
0
    struct inflate_state FAR *state;
1279
1280
    /* check parameters */
1281
0
    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1282
0
    state = (struct inflate_state FAR *)strm->state;
1283
0
    if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR;
1284
1285
    /* if first time, start search in bit buffer */
1286
0
    if (state->mode != SYNC) {
1287
0
        state->mode = SYNC;
1288
0
        state->hold >>= state->bits & 7;
1289
0
        state->bits -= state->bits & 7;
1290
0
        len = 0;
1291
0
        while (state->bits >= 8) {
1292
0
            buf[len++] = (unsigned char)(state->hold);
1293
0
            state->hold >>= 8;
1294
0
            state->bits -= 8;
1295
0
        }
1296
0
        state->have = 0;
1297
0
        syncsearch(&(state->have), buf, len);
1298
0
    }
1299
1300
    /* search available input */
1301
0
    len = syncsearch(&(state->have), strm->next_in, strm->avail_in);
1302
0
    strm->avail_in -= len;
1303
0
    strm->next_in += len;
1304
0
    strm->total_in += len;
1305
1306
    /* return no joy or set up to restart inflate() on a new block */
1307
0
    if (state->have != 4) return Z_DATA_ERROR;
1308
0
    flags = state->flags;
1309
0
    in = strm->total_in;  out = strm->total_out;
1310
0
    inflateReset(strm);
1311
0
    strm->total_in = in;  strm->total_out = out;
1312
0
    state->flags = flags;
1313
0
    if (flags == -1)
1314
0
        state->wrap = 0;    /* if no header yet, treat as raw */
1315
0
    else
1316
0
        state->wrap &= ~4;  /* no point in computing a check value now */
1317
0
    if (state->wbits == 0) {
1318
        /* we never got to see a header for the window size -- set to max */
1319
0
        if (state->window != Z_NULL) {
1320
0
            ZFREE(strm, state->window);
1321
0
            state->window = Z_NULL;
1322
0
        }
1323
0
        state->wbits = 15;
1324
0
    }
1325
0
    state->mode = TYPE;
1326
0
    return Z_OK;
1327
0
}
1328
1329
/*
1330
   Returns true if inflate is currently at the end of a block generated by
1331
   Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
1332
   implementation to provide an additional safety check. PPP uses
1333
   Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored
1334
   block. When decompressing, PPP checks that at the end of input packet,
1335
   inflate is waiting for these length bytes.
1336
 */
1337
0
int ZEXPORT inflateSyncPoint(z_streamp strm) {
1338
0
    struct inflate_state FAR *state;
1339
1340
0
    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1341
0
    state = (struct inflate_state FAR *)strm->state;
1342
0
    return state->mode == STORED && state->bits == 0;
1343
0
}
1344
1345
0
int ZEXPORT inflateCopy(z_streamp dest, z_streamp source) {
1346
0
    struct inflate_state FAR *state;
1347
0
    struct inflate_state FAR *copy;
1348
0
    unsigned char FAR *window;
1349
1350
    /* check input */
1351
0
    if (dest == Z_NULL)
1352
0
        return Z_STREAM_ERROR;
1353
0
    if (inflateStateCheck(source)) {
1354
0
        dest->state = Z_NULL;
1355
0
        return Z_STREAM_ERROR;
1356
0
    }
1357
0
    state = (struct inflate_state FAR *)source->state;
1358
1359
    /* allocate space */
1360
0
    copy = (struct inflate_state FAR *)
1361
0
           ZALLOC(source, 1, sizeof(struct inflate_state));
1362
0
    if (copy == Z_NULL) {
1363
0
        dest->state = Z_NULL;
1364
0
        return Z_MEM_ERROR;
1365
0
    }
1366
0
    zmemzero(copy, sizeof(struct inflate_state));
1367
0
    window = Z_NULL;
1368
0
    if (state->window != Z_NULL) {
1369
0
        window = (unsigned char FAR *)
1370
0
                 ZALLOC(source, 1U << state->wbits, sizeof(unsigned char));
1371
0
        if (window == Z_NULL) {
1372
0
            ZFREE(source, copy);
1373
0
            dest->state = Z_NULL;
1374
0
            return Z_MEM_ERROR;
1375
0
        }
1376
0
    }
1377
1378
    /* copy state */
1379
0
    zmemcpy(dest, source, sizeof(z_stream));
1380
0
    zmemcpy(copy, state, sizeof(struct inflate_state));
1381
0
    copy->strm = dest;
1382
0
    if (state->lencode >= state->codes &&
1383
0
        state->lencode <= state->codes + ENOUGH - 1) {
1384
0
        copy->lencode = copy->codes + (state->lencode - state->codes);
1385
0
        copy->distcode = copy->codes + (state->distcode - state->codes);
1386
0
    }
1387
0
    copy->next = copy->codes + (state->next - state->codes);
1388
0
    if (window != Z_NULL)
1389
0
        zmemcpy(window, state->window, state->whave);
1390
0
    copy->window = window;
1391
0
    dest->state = (struct internal_state FAR *)copy;
1392
0
    return Z_OK;
1393
0
}
1394
1395
0
int ZEXPORT inflateUndermine(z_streamp strm, int subvert) {
1396
0
    struct inflate_state FAR *state;
1397
1398
0
    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1399
0
    state = (struct inflate_state FAR *)strm->state;
1400
#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
1401
    state->sane = !subvert;
1402
    return Z_OK;
1403
#else
1404
0
    (void)subvert;
1405
0
    state->sane = 1;
1406
0
    return Z_DATA_ERROR;
1407
0
#endif
1408
0
}
1409
1410
0
int ZEXPORT inflateValidate(z_streamp strm, int check) {
1411
0
    struct inflate_state FAR *state;
1412
1413
0
    if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
1414
0
    state = (struct inflate_state FAR *)strm->state;
1415
0
    if (check && state->wrap)
1416
0
        state->wrap |= 4;
1417
0
    else
1418
0
        state->wrap &= ~4;
1419
0
    if (check && state->reqwrap)
1420
0
        state->reqwrap |= 4;
1421
0
    else
1422
0
        state->reqwrap &= ~4;
1423
0
    return Z_OK;
1424
0
}
1425
1426
0
long ZEXPORT inflateMark(z_streamp strm) {
1427
0
    struct inflate_state FAR *state;
1428
1429
0
    if (inflateStateCheck(strm))
1430
0
        return -(1L << 16);
1431
0
    state = (struct inflate_state FAR *)strm->state;
1432
0
    return (long)(((unsigned long)((long)state->back)) << 16) +
1433
0
        (state->mode == COPY ? state->length :
1434
0
            (state->mode == MATCH ? state->was - state->length : 0));
1435
0
}
1436
1437
0
unsigned long ZEXPORT inflateCodesUsed(z_streamp strm) {
1438
0
    struct inflate_state FAR *state;
1439
0
    if (inflateStateCheck(strm)) return (unsigned long)-1;
1440
0
    state = (struct inflate_state FAR *)strm->state;
1441
0
    return (unsigned long)(state->next - state->codes);
1442
0
}