Coverage Report

Created: 2026-12-01 06:46

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