Coverage Report

Created: 2026-05-16 06:40

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