Coverage Report

Created: 2026-04-29 07:00

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