Coverage Report

Created: 2026-09-13 06:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/zlib-ng/inflate.c
Line
Count
Source
1
/* inflate.c -- zlib decompression
2
 * Copyright (C) 1995-2022 Mark Adler
3
 * For conditions of distribution and use, see copyright notice in zlib.h
4
 */
5
6
#include "zbuild.h"
7
#include "zsanitizer.h"
8
#include "zutil.h"
9
#include "inftrees.h"
10
#include "inflate.h"
11
#include "inflate_p.h"
12
#include "inffixed_tbl.h"
13
#include "functable.h"
14
#include "arch_functions.h"
15
16
/* Avoid conflicts with zlib.h macros */
17
#ifdef ZLIB_COMPAT
18
# undef inflateInit
19
# undef inflateInit2
20
#endif
21
22
/* function prototypes */
23
static int inflateStateCheck(PREFIX3(stream) *strm);
24
static void updatewindow(PREFIX3(stream) *strm, const uint8_t *end, uint32_t len, int32_t cksum);
25
static uint32_t syncsearch(uint32_t *have, const unsigned char *buf, uint32_t len);
26
27
static inline void inf_chksum_cpy(PREFIX3(stream) *strm, uint8_t *dst,
28
148M
                           const uint8_t *src, uint32_t copy) {
29
148M
    if (!copy) return;
30
146M
    struct inflate_state *state = (struct inflate_state*)strm->state;
31
146M
#ifdef GUNZIP
32
146M
    if (state->flags) {
33
3.12k
        strm->adler = state->check = FUNCTABLE_CALL(crc32_copy)(state->check, dst, src, copy);
34
3.12k
    } else
35
146M
#endif
36
146M
    {
37
146M
        strm->adler = state->check = FUNCTABLE_CALL(adler32_copy)(state->check, dst, src, copy);
38
146M
    }
39
146M
}
40
41
53.1k
static inline void inf_chksum(PREFIX3(stream) *strm, const uint8_t *src, uint32_t len) {
42
53.1k
    struct inflate_state *state = (struct inflate_state*)strm->state;
43
53.1k
#ifdef GUNZIP
44
53.1k
    if (state->flags) {
45
12.7k
        strm->adler = state->check = FUNCTABLE_CALL(crc32)(state->check, src, len);
46
12.7k
    } else
47
40.3k
#endif
48
40.3k
    {
49
40.3k
        strm->adler = state->check = FUNCTABLE_CALL(adler32)(state->check, src, len);
50
40.3k
    }
51
53.1k
}
52
53
148M
static int inflateStateCheck(PREFIX3(stream) *strm) {
54
148M
    struct inflate_state *state;
55
148M
    if (strm == NULL || strm->zalloc == NULL || strm->zfree == NULL)
56
0
        return 1;
57
148M
    state = (struct inflate_state *)strm->state;
58
148M
    if (state == NULL || state->alloc_bufs == NULL || state->strm != strm || state->mode < HEAD || state->mode > SYNC)
59
0
        return 1;
60
148M
    return 0;
61
148M
}
62
63
77.9k
int32_t Z_EXPORT PREFIX(inflateResetKeep)(PREFIX3(stream) *strm) {
64
77.9k
    struct inflate_state *state;
65
66
77.9k
    if (inflateStateCheck(strm))
67
0
        return Z_STREAM_ERROR;
68
77.9k
    state = (struct inflate_state *)strm->state;
69
77.9k
    strm->total_in = strm->total_out = state->total = 0;
70
77.9k
    strm->msg = NULL;
71
77.9k
    strm->data_type = 0;
72
77.9k
    if (state->wrap)        /* to support ill-conceived Java test suite */
73
77.9k
        strm->adler = state->wrap & 1;
74
77.9k
    state->mode = HEAD;
75
77.9k
    state->check = ADLER32_INITIAL_VALUE;
76
77.9k
    state->last = 0;
77
77.9k
    state->havedict = 0;
78
77.9k
    state->flags = -1;
79
77.9k
    state->head = NULL;
80
77.9k
    state->hold = 0;
81
77.9k
    state->bits = 0;
82
77.9k
    state->lencode = state->distcode = state->next = state->codes;
83
77.9k
    state->back = -1;
84
#ifdef INFLATE_STRICT
85
    state->dmax = 32768U;
86
#endif
87
#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
88
    state->sane = 1;
89
#endif
90
77.9k
    INFLATE_RESET_KEEP_HOOK(strm);  /* hook for IBM Z DFLTCC */
91
77.9k
    Tracev((stderr, "inflate: reset\n"));
92
77.9k
    return Z_OK;
93
77.9k
}
94
95
77.9k
int32_t Z_EXPORT PREFIX(inflateReset)(PREFIX3(stream) *strm) {
96
77.9k
    struct inflate_state *state;
97
98
77.9k
    if (inflateStateCheck(strm))
99
0
        return Z_STREAM_ERROR;
100
77.9k
    state = (struct inflate_state *)strm->state;
101
77.9k
    state->wsize = 0;
102
77.9k
    state->whave = 0;
103
77.9k
    state->wnext = 0;
104
77.9k
    return PREFIX(inflateResetKeep)(strm);
105
77.9k
}
106
107
63.1k
int32_t Z_EXPORT PREFIX(inflateReset2)(PREFIX3(stream) *strm, int32_t windowBits) {
108
63.1k
    int wrap;
109
63.1k
    struct inflate_state *state;
110
111
    /* get the state */
112
63.1k
    if (inflateStateCheck(strm))
113
0
        return Z_STREAM_ERROR;
114
63.1k
    state = (struct inflate_state *)strm->state;
115
116
    /* extract wrap request from windowBits parameter */
117
63.1k
    if (windowBits < 0) {
118
0
        wrap = 0;
119
0
        if (windowBits < -MAX_WBITS)
120
0
            return Z_STREAM_ERROR;
121
0
        windowBits = -windowBits;
122
63.1k
    } else {
123
63.1k
        wrap = (windowBits >> 4) + 5;
124
63.1k
#ifdef GUNZIP
125
63.1k
        if (windowBits < 48)
126
63.1k
            windowBits &= MAX_WBITS;
127
63.1k
#endif
128
63.1k
    }
129
130
    /* set number of window bits */
131
63.1k
    if (windowBits && (windowBits < MIN_WBITS || windowBits > MAX_WBITS))
132
0
        return Z_STREAM_ERROR;
133
134
    /* update state and reset the rest of it */
135
63.1k
    state->wrap = wrap;
136
63.1k
    state->wbits = (unsigned)windowBits;
137
63.1k
    return PREFIX(inflateReset)(strm);
138
63.1k
}
139
140
#ifdef INF_ALLOC_DEBUG
141
#  include <stdio.h>
142
#  define LOGSZ(name,size)           fprintf(stderr, "%s is %d bytes\n", name, size)
143
#  define LOGSZP(name,size,loc,pad)  fprintf(stderr, "%s is %d bytes, offset %d, padded %d\n", name, size, loc, pad)
144
#  define LOGSZPL(name,size,loc,pad) fprintf(stderr, "%s is %d bytes, offset %ld, padded %d\n", name, size, loc, pad)
145
#else
146
#  define LOGSZ(name,size)
147
#  define LOGSZP(name,size,loc,pad)
148
#  define LOGSZPL(name,size,loc,pad)
149
#endif
150
151
/* ===========================================================================
152
 * Allocate a big buffer and divide it up into the various buffers inflate needs.
153
 * Handles alignment of allocated buffer and alignment of individual buffers.
154
 */
155
63.1k
Z_INTERNAL inflate_allocs* alloc_inflate(PREFIX3(stream) *strm) {
156
63.1k
    int curr_size = 0;
157
158
    /* Define sizes */
159
63.1k
    int window_size = INFLATE_ADJUST_WINDOW_SIZE((1 << MAX_WBITS) + 64); /* 64B padding for chunksize */
160
63.1k
    int state_size = sizeof(inflate_state);
161
63.1k
    int alloc_size = sizeof(inflate_allocs);
162
163
    /* Calculate relative buffer positions and paddings */
164
63.1k
    LOGSZP("window", window_size, PAD_WINDOW(curr_size), PADSZ(curr_size,WINDOW_PAD_SIZE));
165
63.1k
    int window_pos = PAD_WINDOW(curr_size);
166
63.1k
    curr_size = window_pos + window_size;
167
168
63.1k
    LOGSZP("state", state_size, PAD_64(curr_size), PADSZ(curr_size,64));
169
63.1k
    int state_pos = PAD_64(curr_size);
170
63.1k
    curr_size = state_pos + state_size;
171
172
63.1k
    LOGSZP("alloc", alloc_size, PAD_16(curr_size), PADSZ(curr_size,16));
173
63.1k
    int alloc_pos = PAD_16(curr_size);
174
63.1k
    curr_size = alloc_pos + alloc_size;
175
176
    /* Add 64-1 or 4096-1 to allow window alignment, and round size of buffer up to multiple of 64 */
177
63.1k
    int total_size = PAD_64(curr_size + (WINDOW_PAD_SIZE - 1));
178
179
    /* Allocate buffer, align to 64-byte cacheline, and zerofill the resulting buffer */
180
63.1k
    char *original_buf = (char *)strm->zalloc(strm->opaque, 1, total_size);
181
63.1k
    if (original_buf == NULL)
182
0
        return NULL;
183
184
63.1k
    char *buff = (char *)HINT_ALIGNED_WINDOW((char *)PAD_WINDOW(original_buf));
185
63.1k
    LOGSZPL("Buffer alloc", total_size, PADSZ((uintptr_t)original_buf,WINDOW_PAD_SIZE), PADSZ(curr_size,WINDOW_PAD_SIZE));
186
187
    /* Initialize alloc_bufs */
188
63.1k
    inflate_allocs *alloc_bufs  = (struct inflate_allocs_s *)(buff + alloc_pos);
189
63.1k
    alloc_bufs->buf_start = original_buf;
190
63.1k
    alloc_bufs->zfree = strm->zfree;
191
192
63.1k
    alloc_bufs->window =  (unsigned char *)HINT_ALIGNED_WINDOW((buff + window_pos));
193
63.1k
    alloc_bufs->state = (inflate_state *)HINT_ALIGNED_64((buff + state_pos));
194
195
#ifdef Z_MEMORY_SANITIZER
196
    /* This is _not_ to subvert the memory sanitizer but to instead unposion some
197
       data we willingly and purposefully load uninitialized into vector registers
198
       in order to safely read the last < chunksize bytes of the window. */
199
    __msan_unpoison(alloc_bufs->window + window_size, 64);
200
#endif
201
202
63.1k
    return alloc_bufs;
203
63.1k
}
204
205
/* ===========================================================================
206
 * Free all allocated inflate buffers
207
 */
208
63.1k
Z_INTERNAL void free_inflate(PREFIX3(stream) *strm) {
209
63.1k
    struct inflate_state *state = (struct inflate_state *)strm->state;
210
211
63.1k
    if (state->alloc_bufs != NULL) {
212
63.1k
        inflate_allocs *alloc_bufs = state->alloc_bufs;
213
63.1k
        alloc_bufs->zfree(strm->opaque, alloc_bufs->buf_start);
214
63.1k
        strm->state = NULL;
215
63.1k
    }
216
63.1k
}
217
218
/* ===========================================================================
219
 * Initialize inflate state and buffers.
220
 * This function is hidden in ZLIB_COMPAT builds.
221
 */
222
63.1k
int32_t ZNG_CONDEXPORT PREFIX(inflateInit2)(PREFIX3(stream) *strm, int32_t windowBits) {
223
63.1k
    struct inflate_state *state;
224
63.1k
    int32_t ret;
225
226
    /* Initialize functable */
227
63.1k
    FUNCTABLE_INIT;
228
229
63.1k
    if (strm == NULL)
230
0
        return Z_STREAM_ERROR;
231
63.1k
    strm->msg = NULL;                   /* in case we return an error */
232
63.1k
    if (strm->zalloc == NULL) {
233
63.1k
        strm->zalloc = PREFIX(zcalloc);
234
63.1k
        strm->opaque = NULL;
235
63.1k
    }
236
63.1k
    if (strm->zfree == NULL)
237
63.1k
        strm->zfree = PREFIX(zcfree);
238
239
63.1k
    inflate_allocs *alloc_bufs = alloc_inflate(strm);
240
63.1k
    if (alloc_bufs == NULL)
241
0
        return Z_MEM_ERROR;
242
243
63.1k
    state = alloc_bufs->state;
244
63.1k
    state->window = alloc_bufs->window;
245
63.1k
    state->alloc_bufs = alloc_bufs;
246
63.1k
    state->wbufsize = INFLATE_ADJUST_WINDOW_SIZE((1 << MAX_WBITS) + 64);
247
63.1k
    Tracev((stderr, "inflate: allocated\n"));
248
249
63.1k
    strm->state = (struct internal_state *)state;
250
63.1k
    state->strm = strm;
251
63.1k
    state->mode = HEAD;     /* to pass state test in inflateReset2() */
252
63.1k
    ret = PREFIX(inflateReset2)(strm, windowBits);
253
63.1k
    if (ret != Z_OK) {
254
0
        free_inflate(strm);
255
0
    }
256
63.1k
    return ret;
257
63.1k
}
258
259
#ifndef ZLIB_COMPAT
260
52.9k
int32_t Z_EXPORT PREFIX(inflateInit)(PREFIX3(stream) *strm) {
261
52.9k
    return PREFIX(inflateInit2)(strm, DEF_WBITS);
262
52.9k
}
263
#endif
264
265
/* Function used by zlib.h and zlib-ng version 2.0 macros */
266
0
int32_t Z_EXPORT PREFIX(inflateInit_)(PREFIX3(stream) *strm, const char *version, int32_t stream_size) {
267
0
    if (CHECK_VER_STSIZE(version, stream_size))
268
0
        return Z_VERSION_ERROR;
269
0
    return PREFIX(inflateInit2)(strm, DEF_WBITS);
270
0
}
271
272
/* Function used by zlib.h and zlib-ng version 2.0 macros */
273
0
int32_t Z_EXPORT PREFIX(inflateInit2_)(PREFIX3(stream) *strm, int32_t windowBits, const char *version, int32_t stream_size) {
274
0
    if (CHECK_VER_STSIZE(version, stream_size))
275
0
        return Z_VERSION_ERROR;
276
0
    return PREFIX(inflateInit2)(strm, windowBits);
277
0
}
278
279
0
int32_t Z_EXPORT PREFIX(inflatePrime)(PREFIX3(stream) *strm, int32_t bits, int32_t value) {
280
0
    struct inflate_state *state;
281
282
0
    if (inflateStateCheck(strm))
283
0
        return Z_STREAM_ERROR;
284
0
    if (bits == 0)
285
0
        return Z_OK;
286
0
    INFLATE_PRIME_HOOK(strm, bits, value);  /* hook for IBM Z DFLTCC */
287
0
    state = (struct inflate_state *)strm->state;
288
0
    if (bits < 0) {
289
0
        state->hold = 0;
290
0
        state->bits = 0;
291
0
        return Z_OK;
292
0
    }
293
0
    if (bits > 16 || state->bits + (unsigned int)bits > 32)
294
0
        return Z_STREAM_ERROR;
295
0
    value &= (1L << bits) - 1;
296
0
    state->hold += (uint64_t)value << state->bits;
297
0
    state->bits += (unsigned int)bits;
298
0
    return Z_OK;
299
0
}
300
301
/*
302
   Return state with length and distance decoding tables and index sizes set to
303
   fixed code decoding.  This returns fixed tables from inffixed_tbl.h.
304
 */
305
306
11.9M
void Z_INTERNAL PREFIX(fixedtables)(struct inflate_state *state) {
307
11.9M
    state->lencode = lenfix;
308
11.9M
    state->lenbits = 9;
309
11.9M
    state->distcode = distfix;
310
11.9M
    state->distbits = 5;
311
11.9M
}
312
313
/*
314
   Update the window with the last wsize (normally 32K) bytes written before
315
   returning.  If window does not exist yet, create it.  This is only called
316
   when a window is already in use, or when output has been written during this
317
   inflate call, but the end of the deflate stream has not been reached yet.
318
   It is also called to create a window for dictionary data when a dictionary
319
   is loaded.
320
321
   Providing output buffers larger than 32K to inflate() should provide a speed
322
   advantage, since only the last 32K of output is copied to the sliding window
323
   upon return from inflate(), and since all distances after the first 32K of
324
   output will fall in the output data, making match copies simpler and faster.
325
   The advantage may be dependent on the size of the processor's data caches.
326
 */
327
148M
static void updatewindow(PREFIX3(stream) *strm, const uint8_t *end, uint32_t len, int32_t cksum) {
328
148M
    struct inflate_state *state;
329
148M
    uint32_t dist;
330
331
148M
    state = (struct inflate_state *)strm->state;
332
333
    /* if window not in use yet, initialize */
334
148M
    if (state->wsize == 0)
335
19.9k
        state->wsize = 1U << state->wbits;
336
337
    /* len state->wsize or less output bytes into the circular window */
338
148M
    if (len >= state->wsize) {
339
        /* Only do this if the caller specifies to checksum bytes AND the platform requires
340
         * it (s/390 being the primary exception to this) */
341
4.24k
        if (INFLATE_NEED_CHECKSUM(strm) && cksum) {
342
            /* We have to split the checksum over non-copied and copied bytes */
343
4.24k
            if (len > state->wsize)
344
4.24k
                inf_chksum(strm, end - len, len - state->wsize);
345
4.24k
            inf_chksum_cpy(strm, state->window, end - state->wsize, state->wsize);
346
4.24k
        } else {
347
0
            memcpy(state->window, end - state->wsize, state->wsize);
348
0
        }
349
350
4.24k
        state->wnext = 0;
351
4.24k
        state->whave = state->wsize;
352
148M
    } else {
353
148M
        dist = state->wsize - state->wnext;
354
        /* Only do this if the caller specifies to checksum bytes AND the platform requires
355
         * We need to maintain the correct order here for the checksum */
356
148M
        dist = MIN(dist, len);
357
148M
        if (INFLATE_NEED_CHECKSUM(strm) && cksum) {
358
148M
            inf_chksum_cpy(strm, state->window + state->wnext, end - len, dist);
359
148M
        } else {
360
12.4k
            memcpy(state->window + state->wnext, end - len, dist);
361
12.4k
        }
362
148M
        len -= dist;
363
148M
        if (len) {
364
180
            if (INFLATE_NEED_CHECKSUM(strm) && cksum) {
365
180
                inf_chksum_cpy(strm, state->window, end - len, len);
366
180
            } else {
367
0
                memcpy(state->window, end - len, len);
368
0
            }
369
370
180
            state->wnext = len;
371
180
            state->whave = state->wsize;
372
148M
        } else {
373
148M
            state->wnext += dist;
374
148M
            if (state->wnext == state->wsize)
375
3.97k
                state->wnext = 0;
376
148M
            if (state->whave < state->wsize)
377
28.5M
                state->whave += dist;
378
148M
        }
379
148M
    }
380
148M
}
381
382
/*
383
   Private macros for inflate()
384
   Look in inflate_p.h for macros shared with inflateBack()
385
*/
386
387
/* Get a byte of input into the bit accumulator, or return from inflate() if there is no input available. */
388
#define PULLBYTE() \
389
136M
    do { \
390
136M
        if (have == 0) goto inf_leave; \
391
136M
        have--; \
392
101M
        hold += ((uint64_t)(*next++) << bits); \
393
101M
        bits += 8; \
394
101M
    } while (0)
395
396
/*
397
   inflate() uses a state machine to process as much input data and generate as
398
   much output data as possible before returning.  The state machine is
399
   structured roughly as follows:
400
401
    for (;;) switch (state) {
402
    ...
403
    case STATEn:
404
        if (not enough input data or output space to make progress)
405
            return;
406
        ... make progress ...
407
        state = STATEm;
408
        break;
409
    ...
410
    }
411
412
   so when inflate() is called again, the same case is attempted again, and
413
   if the appropriate resources are provided, the machine proceeds to the
414
   next state.  The NEEDBITS() macro is usually the way the state evaluates
415
   whether it can proceed or should return.  NEEDBITS() does the return if
416
   the requested bits are not available.  The typical use of the BITS macros
417
   is:
418
419
        NEEDBITS(n);
420
        ... do something with BITS(n) ...
421
        DROPBITS(n);
422
423
   where NEEDBITS(n) either returns from inflate() if there isn't enough
424
   input left to load n bits into the accumulator, or it continues.  BITS(n)
425
   gives the low n bits in the accumulator.  When done, DROPBITS(n) drops
426
   the low n bits off the accumulator.  INITBITS() clears the accumulator
427
   and sets the number of available bits to zero.  BYTEBITS() discards just
428
   enough bits to put the accumulator on a byte boundary.  After BYTEBITS()
429
   and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
430
431
   NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
432
   if there is no input available.  The decoding of variable length codes uses
433
   PULLBYTE() directly in order to pull just enough bytes to decode the next
434
   code, and no more.
435
436
   Some states loop until they get enough input, making sure that enough
437
   state information is maintained to continue the loop where it left off
438
   if NEEDBITS() returns in the loop.  For example, want, need, and keep
439
   would all have to actually be part of the saved state in case NEEDBITS()
440
   returns:
441
442
    case STATEw:
443
        while (want < need) {
444
            NEEDBITS(n);
445
            keep[want++] = BITS(n);
446
            DROPBITS(n);
447
        }
448
        state = STATEx;
449
    case STATEx:
450
451
   As shown above, if the next state is also the next case, then the break
452
   is omitted.
453
454
   A state may also return if there is not enough output space available to
455
   complete that state.  Those states are copying stored data, writing a
456
   literal byte, and copying a matching string.
457
458
   When returning, a "goto inf_leave" is used to update the total counters,
459
   update the check value, and determine whether any progress has been made
460
   during that inflate() call in order to return the proper return code.
461
   Progress is defined as a change in either strm->avail_in or strm->avail_out.
462
   When there is a window, goto inf_leave will update the window with the last
463
   output written.  If a goto inf_leave occurs in the middle of decompression
464
   and there is no window currently, goto inf_leave will create one and copy
465
   output to the window for the next call of inflate().
466
467
   In this implementation, the flush parameter of inflate() only affects the
468
   return code (per zlib.h).  inflate() always writes as much as possible to
469
   strm->next_out, given the space available and the provided input--the effect
470
   documented in zlib.h of Z_SYNC_FLUSH.  Furthermore, inflate() always defers
471
   the allocation of and copying into a sliding window until necessary, which
472
   provides the effect documented in zlib.h for Z_FINISH when the entire input
473
   stream available.  So the only thing the flush parameter actually does is:
474
   when flush is set to Z_FINISH, inflate() cannot return Z_OK.  Instead it
475
   will return Z_BUF_ERROR if it has not reached the end of the stream.
476
 */
477
478
148M
int32_t Z_EXPORT PREFIX(inflate)(PREFIX3(stream) *strm, int32_t flush) {
479
148M
    struct inflate_state *state;
480
148M
    const unsigned char *next;  /* next input */
481
148M
    unsigned char *put;         /* next output */
482
148M
    unsigned char *from;        /* where to copy match bytes from */
483
148M
    unsigned have, left;        /* available input and output */
484
148M
    uint64_t hold;              /* bit buffer */
485
148M
    bits_t bits;                /* bits in bit buffer */
486
148M
    uint32_t in, out;           /* save starting available input and output */
487
148M
    unsigned copy;              /* number of stored or match bytes to copy */
488
148M
    code here;                  /* current decoding table entry */
489
148M
    code last;                  /* parent table entry */
490
148M
    unsigned len;               /* length to copy for repeats, bits to drop */
491
148M
    unsigned code_bits;         /* bits in current/parent code */
492
148M
    int32_t ret;                /* return code */
493
148M
    static const uint16_t order[19] = /* permutation of code lengths */
494
148M
        {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
495
496
148M
    if (inflateStateCheck(strm) || strm->next_out == NULL ||
497
148M
        (strm->next_in == NULL && strm->avail_in != 0))
498
0
        return Z_STREAM_ERROR;
499
500
148M
    state = (struct inflate_state *)strm->state;
501
148M
    if (state->mode == TYPE)      /* skip check */
502
12.0k
        state->mode = TYPEDO;
503
148M
    LOAD();
504
148M
    in = have;
505
148M
    out = left;
506
148M
    ret = Z_OK;
507
148M
    for (;;)
508
413M
        switch (state->mode) {
509
66.8k
        case HEAD:
510
66.8k
            if (state->wrap == 0) {
511
0
                state->mode = TYPEDO;
512
0
                break;
513
0
            }
514
66.8k
            NEEDBITS(16);
515
63.1k
#ifdef GUNZIP
516
63.1k
            if ((state->wrap & 2) && hold == 0x8b1f) {  /* gzip header */
517
10.1k
                if (state->wbits == 0)
518
0
                    state->wbits = MAX_WBITS;
519
10.1k
                state->check = CRC32_INITIAL_VALUE;
520
10.1k
                CRC2(state->check, hold);
521
10.1k
                INITBITS();
522
10.1k
                state->mode = FLAGS;
523
10.1k
                break;
524
10.1k
            }
525
52.9k
            if (state->head != NULL)
526
0
                state->head->done = -1;
527
52.9k
            if (!(state->wrap & 1) ||   /* check if zlib header allowed */
528
#else
529
            if (
530
#endif
531
52.9k
                ((BITS(8) << 8) + (hold >> 8)) % 31) {
532
0
                SET_BAD("incorrect header check");
533
0
                break;
534
0
            }
535
52.9k
            if (BITS(4) != Z_DEFLATED) {
536
0
                SET_BAD("unknown compression method");
537
0
                break;
538
0
            }
539
52.9k
            DROPBITS(4);
540
52.9k
            len = BITS(4) + 8;
541
52.9k
            if (state->wbits == 0)
542
0
                state->wbits = len;
543
52.9k
            if (len > MAX_WBITS || len > state->wbits) {
544
0
                SET_BAD("invalid window size");
545
0
                break;
546
0
            }
547
#ifdef INFLATE_STRICT
548
            state->dmax = 1U << len;
549
#endif
550
52.9k
            state->flags = 0;               /* indicate zlib header */
551
52.9k
            Tracev((stderr, "inflate:   zlib header ok\n"));
552
52.9k
            strm->adler = state->check = ADLER32_INITIAL_VALUE;
553
52.9k
            state->mode = hold & 0x200 ? DICTID : TYPE;
554
52.9k
            INITBITS();
555
52.9k
            break;
556
0
#ifdef GUNZIP
557
558
10.1k
        case FLAGS:
559
10.1k
            NEEDBITS(16);
560
10.1k
            state->flags = (int)(hold);
561
10.1k
            if ((state->flags & 0xff) != Z_DEFLATED) {
562
0
                SET_BAD("unknown compression method");
563
0
                break;
564
0
            }
565
10.1k
            if (state->flags & 0xe000) {
566
0
                SET_BAD("unknown header flags set");
567
0
                break;
568
0
            }
569
10.1k
            if (state->head != NULL)
570
0
                state->head->text = (int)((hold >> 8) & 1);
571
10.1k
            if ((state->flags & 0x0200) && (state->wrap & 4))
572
0
                CRC2(state->check, hold);
573
10.1k
            INITBITS();
574
10.1k
            state->mode = TIME;
575
10.1k
            Z_FALLTHROUGH;
576
577
10.1k
        case TIME:
578
10.1k
            NEEDBITS(32);
579
10.1k
            if (state->head != NULL)
580
0
                state->head->time = (unsigned)(hold);
581
10.1k
            if ((state->flags & 0x0200) && (state->wrap & 4))
582
0
                CRC4(state->check, hold);
583
10.1k
            INITBITS();
584
10.1k
            state->mode = OS;
585
10.1k
            Z_FALLTHROUGH;
586
587
10.1k
        case OS:
588
10.1k
            NEEDBITS(16);
589
10.1k
            if (state->head != NULL) {
590
0
                state->head->xflags = (int)(hold & 0xff);
591
0
                state->head->os = (int)(hold >> 8);
592
0
            }
593
10.1k
            if ((state->flags & 0x0200) && (state->wrap & 4))
594
0
                CRC2(state->check, hold);
595
10.1k
            INITBITS();
596
10.1k
            state->mode = EXLEN;
597
10.1k
            Z_FALLTHROUGH;
598
599
10.1k
        case EXLEN:
600
10.1k
            if (state->flags & 0x0400) {
601
0
                NEEDBITS(16);
602
0
                state->length = (uint16_t)hold;
603
0
                if (state->head != NULL)
604
0
                    state->head->extra_len = (uint16_t)hold;
605
0
                if ((state->flags & 0x0200) && (state->wrap & 4))
606
0
                    CRC2(state->check, hold);
607
0
                INITBITS();
608
10.1k
            } else if (state->head != NULL) {
609
0
                state->head->extra = NULL;
610
0
            }
611
10.1k
            state->mode = EXTRA;
612
10.1k
            Z_FALLTHROUGH;
613
614
10.1k
        case EXTRA:
615
10.1k
            if (state->flags & 0x0400) {
616
0
                copy = state->length;
617
0
                if (copy > have)
618
0
                    copy = have;
619
0
                if (copy) {
620
0
                    if (state->head != NULL && state->head->extra != NULL) {
621
0
                        len = state->head->extra_len - state->length;
622
0
                        if (len < state->head->extra_max) {
623
0
                            memcpy(state->head->extra + len, next,
624
0
                                    len + copy > state->head->extra_max ?
625
0
                                    state->head->extra_max - len : copy);
626
0
                        }
627
0
                    }
628
0
                    if ((state->flags & 0x0200) && (state->wrap & 4)) {
629
0
                        state->check = crc32_small((uint32_t)state->check, next, copy);
630
0
                    }
631
0
                    have -= copy;
632
0
                    next += copy;
633
0
                    state->length -= copy;
634
0
                }
635
0
                if (state->length)
636
0
                    goto inf_leave;
637
0
            }
638
10.1k
            state->length = 0;
639
10.1k
            state->mode = NAME;
640
10.1k
            Z_FALLTHROUGH;
641
642
10.1k
        case NAME:
643
10.1k
            if (state->flags & 0x0800) {
644
0
                if (have == 0) goto inf_leave;
645
0
                copy = 0;
646
0
                do {
647
0
                    len = (unsigned)(next[copy++]);
648
0
                    if (state->head != NULL && state->head->name != NULL && state->length < state->head->name_max)
649
0
                        state->head->name[state->length++] = (unsigned char)len;
650
0
                } while (len && copy < have);
651
0
                if ((state->flags & 0x0200) && (state->wrap & 4))
652
0
                    state->check = crc32_small((uint32_t)state->check, next, copy);
653
0
                have -= copy;
654
0
                next += copy;
655
0
                if (len)
656
0
                    goto inf_leave;
657
10.1k
            } else if (state->head != NULL) {
658
0
                state->head->name = NULL;
659
0
            }
660
10.1k
            state->length = 0;
661
10.1k
            state->mode = COMMENT;
662
10.1k
            Z_FALLTHROUGH;
663
664
10.1k
        case COMMENT:
665
10.1k
            if (state->flags & 0x1000) {
666
0
                if (have == 0) goto inf_leave;
667
0
                copy = 0;
668
0
                do {
669
0
                    len = (unsigned)(next[copy++]);
670
0
                    if (state->head != NULL && state->head->comment != NULL
671
0
                        && state->length < state->head->comm_max)
672
0
                        state->head->comment[state->length++] = (unsigned char)len;
673
0
                } while (len && copy < have);
674
0
                if ((state->flags & 0x0200) && (state->wrap & 4))
675
0
                    state->check = crc32_small((uint32_t)state->check, next, copy);
676
0
                have -= copy;
677
0
                next += copy;
678
0
                if (len)
679
0
                    goto inf_leave;
680
10.1k
            } else if (state->head != NULL) {
681
0
                state->head->comment = NULL;
682
0
            }
683
10.1k
            state->mode = HCRC;
684
10.1k
            Z_FALLTHROUGH;
685
686
10.1k
        case HCRC:
687
10.1k
            if (state->flags & 0x0200) {
688
0
                NEEDBITS(16);
689
0
                if ((state->wrap & 4) && hold != (state->check & 0xffff)) {
690
0
                    SET_BAD("header crc mismatch");
691
0
                    break;
692
0
                }
693
0
                INITBITS();
694
0
            }
695
10.1k
            if (state->head != NULL) {
696
0
                state->head->hcrc = (int)((state->flags >> 9) & 1);
697
0
                state->head->done = 1;
698
0
            }
699
            /* compute crc32 checksum if not in raw mode */
700
10.1k
            if ((state->wrap & 4) && state->flags)
701
10.1k
                strm->adler = state->check = CRC32_INITIAL_VALUE;
702
10.1k
            state->mode = TYPE;
703
10.1k
            break;
704
0
#endif
705
12.4k
        case DICTID:
706
12.4k
            NEEDBITS(32);
707
12.4k
            strm->adler = state->check = ZSWAP32((unsigned)hold);
708
12.4k
            INITBITS();
709
12.4k
            state->mode = DICT;
710
12.4k
            Z_FALLTHROUGH;
711
712
24.8k
        case DICT:
713
24.8k
            if (state->havedict == 0) {
714
12.4k
                RESTORE();
715
12.4k
                return Z_NEED_DICT;
716
12.4k
            }
717
12.4k
            strm->adler = state->check = ADLER32_INITIAL_VALUE;
718
12.4k
            state->mode = TYPE;
719
12.4k
            Z_FALLTHROUGH;
720
721
12.1M
        case TYPE:
722
12.1M
            if (flush == Z_BLOCK || flush == Z_TREES)
723
0
                goto inf_leave;
724
12.1M
            Z_FALLTHROUGH;
725
726
12.1M
        case TYPEDO:
727
            /* determine and dispatch block type */
728
12.1M
            INFLATE_TYPEDO_HOOK(strm, flush);  /* hook for IBM Z DFLTCC */
729
12.1M
            if (state->last) {
730
56.6k
                BYTEBITS();
731
56.6k
                state->mode = CHECK;
732
56.6k
                break;
733
56.6k
            }
734
12.0M
            NEEDBITS(3);
735
12.0M
            state->last = BITS(1);
736
12.0M
            DROPBITS(1);
737
12.0M
            switch (BITS(2)) {
738
30.6k
            case 0:                             /* stored block */
739
30.6k
                Tracev((stderr, "inflate:     stored block%s\n", state->last ? " (last)" : ""));
740
30.6k
                state->mode = STORED;
741
30.6k
                break;
742
11.9M
            case 1:                             /* fixed block */
743
11.9M
                PREFIX(fixedtables)(state);
744
11.9M
                Tracev((stderr, "inflate:     fixed codes block%s\n", state->last ? " (last)" : ""));
745
11.9M
                state->mode = LEN_;             /* decode codes */
746
11.9M
                if (flush == Z_TREES) {
747
0
                    DROPBITS(2);
748
0
                    goto inf_leave;
749
0
                }
750
11.9M
                break;
751
11.9M
            case 2:                             /* dynamic block */
752
86.3k
                Tracev((stderr, "inflate:     dynamic codes block%s\n", state->last ? " (last)" : ""));
753
86.3k
                state->mode = TABLE;
754
86.3k
                break;
755
766
            default:
756
766
                SET_BAD("invalid block type");
757
12.0M
            }
758
12.0M
            DROPBITS(2);
759
12.0M
            break;
760
761
37.2k
        case STORED:
762
            /* get and verify stored block length */
763
37.2k
            BYTEBITS();                         /* go to byte boundary */
764
37.2k
            NEEDBITS(32);
765
30.6k
            if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
766
1.22k
                SET_BAD("invalid stored block lengths");
767
1.22k
                break;
768
1.22k
            }
769
29.4k
            state->length = (uint16_t)hold;
770
29.4k
            Tracev((stderr, "inflate:       stored length %u\n", state->length));
771
29.4k
            INITBITS();
772
29.4k
            state->mode = COPY_;
773
29.4k
            if (flush == Z_TREES)
774
0
                goto inf_leave;
775
29.4k
            Z_FALLTHROUGH;
776
777
29.4k
        case COPY_:
778
29.4k
            state->mode = COPY;
779
29.4k
            Z_FALLTHROUGH;
780
781
43.6M
        case COPY:
782
            /* copy stored block from input to output */
783
43.6M
            copy = state->length;
784
43.6M
            if (copy) {
785
43.6M
                copy = MIN(copy, have);
786
43.6M
                copy = MIN(copy, left);
787
43.6M
                if (copy == 0)
788
21.8M
                    goto inf_leave;
789
21.8M
                memcpy(put, next, copy);
790
21.8M
                have -= copy;
791
21.8M
                next += copy;
792
21.8M
                left -= copy;
793
21.8M
                put += copy;
794
21.8M
                state->length -= copy;
795
21.8M
                break;
796
43.6M
            }
797
29.2k
            Tracev((stderr, "inflate:       stored end\n"));
798
29.2k
            state->mode = TYPE;
799
29.2k
            break;
800
801
98.9k
        case TABLE:
802
            /* get dynamic table entries descriptor */
803
98.9k
            NEEDBITS(14);
804
86.3k
            state->nlen = BITS(5) + 257;
805
86.3k
            DROPBITS(5);
806
86.3k
            state->ndist = BITS(5) + 1;
807
86.3k
            DROPBITS(5);
808
86.3k
            state->ncode = BITS(4) + 4;
809
86.3k
            DROPBITS(4);
810
86.3k
#ifndef PKZIP_BUG_WORKAROUND
811
86.3k
            if (state->nlen > 286 || state->ndist > 30) {
812
82
                SET_BAD("too many length or distance symbols");
813
82
                break;
814
82
            }
815
86.2k
#endif
816
86.2k
            Tracev((stderr, "inflate:       table sizes ok\n"));
817
86.2k
            state->have = 0;
818
86.2k
            state->mode = LENLENS;
819
86.2k
            Z_FALLTHROUGH;
820
821
129k
        case LENLENS:
822
            /* get code length code lengths (not a typo) */
823
1.61M
            while (state->have < state->ncode) {
824
1.53M
                NEEDBITS(3);
825
1.48M
                state->lens[order[state->have++]] = (uint16_t)BITS(3);
826
1.48M
                DROPBITS(3);
827
1.48M
            }
828
235k
            while (state->have < 19)
829
149k
                state->lens[order[state->have++]] = 0;
830
86.2k
            state->next = state->codes;
831
86.2k
            state->lencode = state->distcode = (const code *)(state->next);
832
86.2k
            state->lenbits = 7;
833
86.2k
            ret = zng_inflate_table(CODES, state->lens, 19, &(state->next), &(state->lenbits), state->work);
834
86.2k
            if (ret) {
835
613
                SET_BAD("invalid code lengths set");
836
613
                break;
837
613
            }
838
85.6k
            Tracev((stderr, "inflate:       code lengths ok\n"));
839
85.6k
            state->have = 0;
840
85.6k
            state->mode = CODELENS;
841
85.6k
            Z_FALLTHROUGH;
842
843
484k
        case CODELENS:
844
            /* get length and distance code code lengths */
845
9.34M
            while (state->have < state->nlen + state->ndist) {
846
12.2M
                for (;;) {
847
12.2M
                    here = state->lencode[BITS(state->lenbits)];
848
12.2M
                    if (here.bits <= bits) break;
849
3.38M
                    PULLBYTE();
850
3.38M
                }
851
8.91M
                if (here.val < 16) {
852
7.26M
                    DROPBITS(here.bits);
853
7.26M
                    state->lens[state->have++] = here.val;
854
7.26M
                } else {
855
1.65M
                    if (here.val == 16) {
856
1.05M
                        NEEDBITS(here.bits + 2);
857
1.02M
                        DROPBITS(here.bits);
858
1.02M
                        if (state->have == 0) {
859
14
                            SET_BAD("invalid bit length repeat");
860
14
                            break;
861
14
                        }
862
1.02M
                        len = state->lens[state->have - 1];
863
1.02M
                        copy = 3 + BITS(2);
864
1.02M
                        DROPBITS(2);
865
1.02M
                    } else if (here.val == 17) {
866
366k
                        NEEDBITS(here.bits + 3);
867
356k
                        DROPBITS(here.bits);
868
356k
                        len = 0;
869
356k
                        copy = 3 + BITS(3);
870
356k
                        DROPBITS(3);
871
356k
                    } else {
872
229k
                        NEEDBITS(here.bits + 7);
873
220k
                        DROPBITS(here.bits);
874
220k
                        len = 0;
875
220k
                        copy = 11 + BITS(7);
876
220k
                        DROPBITS(7);
877
220k
                    }
878
1.59M
                    if (state->have + copy > state->nlen + state->ndist) {
879
102
                        SET_BAD("invalid bit length repeat");
880
102
                        break;
881
102
                    }
882
18.5M
                    while (copy) {
883
16.9M
                        --copy;
884
16.9M
                        state->lens[state->have++] = (uint16_t)len;
885
16.9M
                    }
886
1.59M
                }
887
8.91M
            }
888
889
            /* handle error breaks in while */
890
85.4k
            if (state->mode == BAD)
891
116
                break;
892
893
            /* check for end-of-block code (better have one) */
894
85.3k
            if (state->lens[256] == 0) {
895
38
                SET_BAD("invalid code -- missing end-of-block");
896
38
                break;
897
38
            }
898
899
            /* build code tables -- note: do not change the lenbits or distbits
900
               values here (MAX_LEN_ROOT_BITS and 9) without reading the comments
901
               in inftrees.h concerning the ENOUGH constants, which depend on
902
               those values, and the refill comments in inffast_tpl.h, which
903
               depend on lenbits never exceeding MAX_LEN_ROOT_BITS */
904
85.2k
            state->next = state->codes;
905
85.2k
            state->lencode = (const code *)(state->next);
906
85.2k
            state->lenbits = MAX_LEN_ROOT_BITS;
907
85.2k
            ret = zng_inflate_table(LENS, state->lens, state->nlen, &(state->next), &(state->lenbits), state->work);
908
85.2k
            if (ret) {
909
256
                SET_BAD("invalid literal/lengths set");
910
256
                break;
911
256
            }
912
85.0k
            state->distcode = (const code *)(state->next);
913
85.0k
            state->distbits = 9;
914
85.0k
            ret = zng_inflate_table(DISTS, state->lens + state->nlen, state->ndist,
915
85.0k
                            &(state->next), &(state->distbits), state->work);
916
85.0k
            if (ret) {
917
57
                SET_BAD("invalid distances set");
918
57
                break;
919
57
            }
920
84.9k
            Tracev((stderr, "inflate:       codes ok\n"));
921
84.9k
            state->mode = LEN_;
922
84.9k
            if (flush == Z_TREES)
923
0
                goto inf_leave;
924
84.9k
            Z_FALLTHROUGH;
925
926
12.0M
        case LEN_:
927
12.0M
            state->mode = LEN;
928
12.0M
            Z_FALLTHROUGH;
929
930
138M
        case LEN:
931
            /* use inflate_fast() if we have enough input and output */
932
138M
            if (have >= INFLATE_FAST_MIN_HAVE && left >= INFLATE_FAST_MIN_SAFE) {
933
12.0M
                RESTORE();
934
12.0M
                FUNCTABLE_CALL(inflate_fast)(strm, out, left < INFLATE_FAST_MIN_LEFT);
935
12.0M
                LOAD();
936
12.0M
                if (state->mode == TYPE)
937
11.9M
                    state->back = -1;
938
12.0M
                break;
939
12.0M
            }
940
126M
            state->back = 0;
941
942
            /* get a literal, length, or end-of-block code */
943
214M
            for (;;) {
944
214M
                here = state->lencode[BITS(state->lenbits)];
945
214M
                if (CODE_BITS(here) <= bits)
946
95.5M
                    break;
947
118M
                PULLBYTE();
948
118M
            }
949
95.5M
            if (here.op && (here.op & 0xf0) == 0) {
950
422k
                unsigned last_bits;
951
422k
                last = here;
952
422k
                last_bits = CODE_BITS(last);
953
499k
                for (;;) {
954
499k
                    here = state->lencode[last.val + (BITS(last_bits + (last.op & 15)) >> last_bits)];
955
499k
                    if (last_bits + CODE_BITS(here) <= bits)
956
348k
                        break;
957
150k
                    PULLBYTE();
958
150k
                }
959
348k
                DROPBITS(last_bits);
960
348k
                state->back += last_bits;
961
348k
            }
962
95.4M
            code_bits = CODE_BITS(here);
963
95.4M
            DROPBITS(code_bits);
964
95.4M
            state->back += code_bits;
965
95.4M
            state->length = here.val;
966
967
            /* process literal */
968
95.4M
            if ((int)(here.op) == 0) {
969
93.4M
                TRACE_LITERAL(here.val);
970
93.4M
                state->mode = LIT;
971
93.4M
                break;
972
93.4M
            }
973
974
            /* process end of block */
975
2.02M
            if (here.op & 32) {
976
61.0k
                TRACE_END_OF_BLOCK();
977
61.0k
                state->back = -1;
978
61.0k
                state->mode = TYPE;
979
61.0k
                break;
980
61.0k
            }
981
982
            /* invalid code */
983
1.96M
            if (here.op & 64) {
984
12
                SET_BAD("invalid literal/length code");
985
12
                break;
986
12
            }
987
988
            /* length code */
989
1.96M
            state->extra = CODE_EXTRA(here);
990
1.96M
            state->mode = LENEXT;
991
1.96M
            Z_FALLTHROUGH;
992
993
2.00M
        case LENEXT:
994
            /* get extra bits, if any */
995
2.00M
            if (state->extra) {
996
270k
                NEEDBITS(state->extra);
997
227k
                state->length += BITS(state->extra);
998
227k
                DROPBITS(state->extra);
999
227k
                state->back += state->extra;
1000
227k
            }
1001
1.96M
            TRACE_LENGTH(state->length);
1002
1.96M
            state->was = state->length;
1003
1.96M
            state->mode = DIST;
1004
1.96M
            Z_FALLTHROUGH;
1005
1006
2.38M
        case DIST:
1007
            /* get distance code */
1008
3.29M
            for (;;) {
1009
3.29M
                here = state->distcode[BITS(state->distbits)];
1010
3.29M
                if (CODE_BITS(here) <= bits)
1011
1.96M
                    break;
1012
1.33M
                PULLBYTE();
1013
1.33M
            }
1014
1.96M
            if ((here.op & 0xf0) == 0) {
1015
6.95k
                unsigned last_bits;
1016
6.95k
                last = here;
1017
6.95k
                last_bits = CODE_BITS(last);
1018
8.15k
                for (;;) {
1019
8.15k
                    here = state->distcode[last.val + (BITS(last_bits + (last.op & 15)) >> last_bits)];
1020
8.15k
                    if (last_bits + CODE_BITS(here) <= bits)
1021
5.82k
                        break;
1022
2.33k
                    PULLBYTE();
1023
2.33k
                }
1024
5.82k
                DROPBITS(last_bits);
1025
5.82k
                state->back += last_bits;
1026
5.82k
            }
1027
1.96M
            code_bits = CODE_BITS(here);
1028
1.96M
            DROPBITS(code_bits);
1029
1.96M
            state->back += code_bits;
1030
1.96M
            if (here.op & 64) {
1031
24
                SET_BAD("invalid distance code");
1032
24
                break;
1033
24
            }
1034
1.96M
            state->offset = here.val;
1035
1.96M
            state->extra = CODE_EXTRA(here);
1036
1.96M
            state->mode = DISTEXT;
1037
1.96M
            Z_FALLTHROUGH;
1038
1039
3.82M
        case DISTEXT:
1040
            /* get distance extra bits, if any */
1041
3.82M
            if (state->extra) {
1042
3.74M
                NEEDBITS(state->extra);
1043
1.88M
                state->offset += BITS(state->extra);
1044
1.88M
                DROPBITS(state->extra);
1045
1.88M
                state->back += state->extra;
1046
1.88M
            }
1047
#ifdef INFLATE_STRICT
1048
            if (state->offset > state->dmax) {
1049
                SET_BAD("invalid distance too far back");
1050
                break;
1051
            }
1052
#endif
1053
1.96M
            TRACE_DISTANCE(state->offset);
1054
1.96M
            state->mode = MATCH;
1055
1.96M
            Z_FALLTHROUGH;
1056
1057
62.2M
        case MATCH:
1058
            /* copy match from window to output */
1059
62.2M
            if (left == 0)
1060
30.2M
                goto inf_leave;
1061
31.9M
            copy = out - left;
1062
31.9M
            if (state->offset > copy) {         /* copy from window */
1063
31.9M
                copy = state->offset - copy;
1064
31.9M
                if (copy > state->whave) {
1065
#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
1066
                    if (state->sane) {
1067
                        SET_BAD("invalid distance too far back");
1068
                        break;
1069
                    }
1070
                    Trace((stderr, "inflate.c too far\n"));
1071
                    copy -= state->whave;
1072
                    copy = MIN(copy, state->length);
1073
                    copy = MIN(copy, left);
1074
                    left -= copy;
1075
                    state->length -= copy;
1076
                    do {
1077
                        *put++ = 0;
1078
                    } while (--copy);
1079
                    if (state->length == 0)
1080
                        state->mode = LEN;
1081
#else
1082
194
                    SET_BAD("invalid distance too far back");
1083
194
#endif
1084
194
                    break;
1085
194
                }
1086
31.9M
                if (copy > state->wnext) {
1087
5.80M
                    copy -= state->wnext;
1088
5.80M
                    from = state->window + (state->wsize - copy);
1089
26.1M
                } else {
1090
26.1M
                    from = state->window + (state->wnext - copy);
1091
26.1M
                }
1092
31.9M
                copy = MIN(copy, state->length);
1093
31.9M
                copy = MIN(copy, left);
1094
1095
31.9M
                put = chunkcopy_safe(put, from, copy, put + left);
1096
31.9M
            } else {
1097
50.5k
                copy = MIN(state->length, left);
1098
1099
50.5k
                put = FUNCTABLE_CALL(chunkmemset_safe)(put, put - state->offset, copy, left);
1100
50.5k
            }
1101
31.9M
            left -= copy;
1102
31.9M
            state->length -= copy;
1103
31.9M
            if (state->length == 0)
1104
1.96M
                state->mode = LEN;
1105
31.9M
            break;
1106
1107
155M
        case LIT:
1108
155M
            if (left == 0)
1109
62.3M
                goto inf_leave;
1110
93.4M
            *put++ = (unsigned char)(state->length);
1111
93.4M
            left--;
1112
93.4M
            state->mode = LEN;
1113
93.4M
            break;
1114
1115
56.6k
        case CHECK:
1116
56.6k
            if (state->wrap) {
1117
56.6k
                NEEDBITS(32);
1118
53.8k
                out -= left;
1119
53.8k
                strm->total_out += out;
1120
53.8k
                state->total += out;
1121
1122
                /* compute crc32 checksum if not in raw mode */
1123
53.8k
                if (INFLATE_NEED_CHECKSUM(strm) && state->wrap & 4) {
1124
49.1k
                    if (out) {
1125
48.9k
                        inf_chksum(strm, put - out, out);
1126
48.9k
                    }
1127
49.1k
                }
1128
53.8k
                out = left;
1129
53.8k
                if ((state->wrap & 4) && (
1130
49.1k
#ifdef GUNZIP
1131
49.1k
                     state->flags ? hold :
1132
49.1k
#endif
1133
49.1k
                     ZSWAP32((unsigned)hold)) != state->check) {
1134
499
                    SET_BAD("incorrect data check");
1135
499
                    break;
1136
499
                }
1137
53.3k
                INITBITS();
1138
53.3k
                Tracev((stderr, "inflate:   check matches trailer\n"));
1139
53.3k
            }
1140
53.3k
#ifdef GUNZIP
1141
53.3k
            state->mode = LENGTH;
1142
53.3k
            Z_FALLTHROUGH;
1143
1144
53.3k
        case LENGTH:
1145
53.3k
            if (state->wrap && state->flags) {
1146
10.2k
                NEEDBITS(32);
1147
10.1k
                if ((state->wrap & 4) && hold != (state->total & 0xffffffff)) {
1148
0
                    SET_BAD("incorrect length check");
1149
0
                    break;
1150
0
                }
1151
10.1k
                INITBITS();
1152
10.1k
                Tracev((stderr, "inflate:   length matches trailer\n"));
1153
10.1k
            }
1154
53.3k
#endif
1155
53.3k
            state->mode = DONE;
1156
53.3k
            Z_FALLTHROUGH;
1157
1158
53.3k
        case DONE:
1159
            /* inflate stream terminated properly */
1160
53.3k
            ret = Z_STREAM_END;
1161
53.3k
            goto inf_leave;
1162
1163
4.65k
        case BAD:
1164
4.65k
            ret = Z_DATA_ERROR;
1165
4.65k
            goto inf_leave;
1166
1167
0
        case SYNC:
1168
1169
0
        default:                 /* can't happen, but makes compilers happy */
1170
0
            return Z_STREAM_ERROR;
1171
413M
        }
1172
1173
    /*
1174
       Return from inflate(), updating the total counts and the check value.
1175
       If there was no progress during the inflate() call, return a buffer
1176
       error.  Call updatewindow() to create and/or update the window state.
1177
     */
1178
148M
  inf_leave:
1179
148M
    RESTORE();
1180
148M
    uint32_t check_bytes = out - strm->avail_out;
1181
148M
    if (INFLATE_NEED_UPDATEWINDOW(strm) &&
1182
148M
            (state->wsize || (out != strm->avail_out && state->mode < BAD &&
1183
148M
                 (state->mode < CHECK || flush != Z_FINISH)))) {
1184
        /* update sliding window with respective checksum if not in "raw" mode */
1185
148M
        updatewindow(strm, strm->next_out, check_bytes, state->wrap & 4);
1186
148M
    }
1187
148M
    in -= strm->avail_in;
1188
148M
    out -= strm->avail_out;
1189
148M
    strm->total_in += in;
1190
148M
    strm->total_out += out;
1191
148M
    state->total += out;
1192
1193
148M
    strm->data_type = (int)state->bits + (state->last ? 64 : 0) +
1194
148M
                      (state->mode == TYPE ? 128 : 0) + (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0);
1195
148M
    if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK) {
1196
        /* when no sliding window is used, hash the output bytes if no CHECK state */
1197
1.37k
        if (INFLATE_NEED_CHECKSUM(strm) && !state->wsize && flush == Z_FINISH) {
1198
0
            inf_chksum(strm, put - check_bytes, check_bytes);
1199
0
        }
1200
1.37k
        ret = Z_BUF_ERROR;
1201
1.37k
    }
1202
148M
    return ret;
1203
148M
}
1204
1205
63.1k
int32_t Z_EXPORT PREFIX(inflateEnd)(PREFIX3(stream) *strm) {
1206
63.1k
    if (inflateStateCheck(strm))
1207
0
        return Z_STREAM_ERROR;
1208
1209
    /* Free allocated buffers */
1210
63.1k
    free_inflate(strm);
1211
1212
63.1k
    Tracev((stderr, "inflate: end\n"));
1213
63.1k
    return Z_OK;
1214
63.1k
}
1215
1216
0
int32_t Z_EXPORT PREFIX(inflateGetDictionary)(PREFIX3(stream) *strm, uint8_t *dictionary, uint32_t *dictLength) {
1217
0
    struct inflate_state *state;
1218
1219
    /* check state */
1220
0
    if (inflateStateCheck(strm))
1221
0
        return Z_STREAM_ERROR;
1222
0
    state = (struct inflate_state *)strm->state;
1223
1224
0
    INFLATE_GET_DICTIONARY_HOOK(strm, dictionary, dictLength);  /* hook for IBM Z DFLTCC */
1225
1226
    /* copy dictionary */
1227
0
    if (state->whave && dictionary != NULL) {
1228
0
        memcpy(dictionary, state->window + state->wnext, state->whave - state->wnext);
1229
0
        memcpy(dictionary + state->whave - state->wnext, state->window, state->wnext);
1230
0
    }
1231
0
    if (dictLength != NULL)
1232
0
        *dictLength = state->whave;
1233
0
    return Z_OK;
1234
0
}
1235
1236
12.4k
int32_t Z_EXPORT PREFIX(inflateSetDictionary)(PREFIX3(stream) *strm, const uint8_t *dictionary, uint32_t dictLength) {
1237
12.4k
    struct inflate_state *state;
1238
12.4k
    unsigned long dictid;
1239
1240
    /* check state */
1241
12.4k
    if (inflateStateCheck(strm))
1242
0
        return Z_STREAM_ERROR;
1243
12.4k
    state = (struct inflate_state *)strm->state;
1244
12.4k
    if (state->wrap != 0 && state->mode != DICT)
1245
0
        return Z_STREAM_ERROR;
1246
1247
    /* check for correct dictionary identifier */
1248
12.4k
    if (state->mode == DICT) {
1249
12.4k
        dictid = FUNCTABLE_CALL(adler32)(ADLER32_INITIAL_VALUE, dictionary, dictLength);
1250
12.4k
        if (dictid != state->check)
1251
0
            return Z_DATA_ERROR;
1252
12.4k
    }
1253
1254
12.4k
    INFLATE_SET_DICTIONARY_HOOK(strm, dictionary, dictLength);  /* hook for IBM Z DFLTCC */
1255
1256
    /* copy dictionary to window using updatewindow(), which will amend the
1257
       existing dictionary if appropriate */
1258
12.4k
    updatewindow(strm, dictionary + dictLength, dictLength, 0);
1259
1260
12.4k
    state->havedict = 1;
1261
12.4k
    Tracev((stderr, "inflate:   dictionary set\n"));
1262
12.4k
    return Z_OK;
1263
12.4k
}
1264
1265
0
int32_t Z_EXPORT PREFIX(inflateGetHeader)(PREFIX3(stream) *strm, PREFIX(gz_headerp) head) {
1266
0
    struct inflate_state *state;
1267
1268
    /* check state */
1269
0
    if (inflateStateCheck(strm))
1270
0
        return Z_STREAM_ERROR;
1271
0
    state = (struct inflate_state *)strm->state;
1272
0
    if ((state->wrap & 2) == 0)
1273
0
        return Z_STREAM_ERROR;
1274
1275
    /* save header structure */
1276
0
    state->head = head;
1277
0
    head->done = 0;
1278
0
    return Z_OK;
1279
0
}
1280
1281
/*
1282
   Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff.  Return when found
1283
   or when out of input.  When called, *have is the number of pattern bytes
1284
   found in order so far, in 0..3.  On return *have is updated to the new
1285
   state.  If on return *have equals four, then the pattern was found and the
1286
   return value is how many bytes were read including the last byte of the
1287
   pattern.  If *have is less than four, then the pattern has not been found
1288
   yet and the return value is len.  In the latter case, syncsearch() can be
1289
   called again with more data and the *have state.  *have is initialized to
1290
   zero for the first call.
1291
 */
1292
9.32k
static uint32_t syncsearch(uint32_t *have, const uint8_t *buf, uint32_t len) {
1293
9.32k
    uint32_t got, next;
1294
1295
9.32k
    got = *have;
1296
9.32k
    next = 0;
1297
51.2k
    while (next < len && got < 4) {
1298
41.9k
        if ((int)(buf[next]) == (got < 2 ? 0 : 0xff))
1299
18.9k
            got++;
1300
23.0k
        else if (buf[next])
1301
17.0k
            got = 0;
1302
5.99k
        else
1303
5.99k
            got = 4 - got;
1304
41.9k
        next++;
1305
41.9k
    }
1306
9.32k
    *have = got;
1307
9.32k
    return next;
1308
9.32k
}
1309
1310
4.66k
int32_t Z_EXPORT PREFIX(inflateSync)(PREFIX3(stream) *strm) {
1311
4.66k
    struct inflate_state *state;
1312
4.66k
    size_t in, out;             /* temporary to save total_in and total_out */
1313
4.66k
    unsigned len;               /* number of bytes to look at or looked at */
1314
4.66k
    int flags;                  /* temporary to save header status */
1315
4.66k
    unsigned char buf[4];       /* to restore bit buffer to byte string */
1316
1317
    /* check parameters */
1318
4.66k
    if (inflateStateCheck(strm))
1319
0
        return Z_STREAM_ERROR;
1320
4.66k
    state = (struct inflate_state *)strm->state;
1321
4.66k
    if (strm->avail_in == 0 && state->bits < 8)
1322
0
        return Z_BUF_ERROR;
1323
1324
    /* if first time, start search in bit buffer */
1325
4.66k
    if (state->mode != SYNC) {
1326
4.66k
        state->mode = SYNC;
1327
4.66k
        state->hold >>= state->bits & 7;
1328
4.66k
        state->bits -= state->bits & 7;
1329
4.66k
        len = 0;
1330
4.66k
        while (state->bits >= 8) {
1331
0
            buf[len++] = (unsigned char)(state->hold);
1332
0
            state->hold >>= 8;
1333
0
            state->bits -= 8;
1334
0
        }
1335
4.66k
        state->have = 0;
1336
4.66k
        syncsearch(&(state->have), buf, len);
1337
4.66k
    }
1338
1339
    /* search available input */
1340
4.66k
    len = syncsearch(&(state->have), strm->next_in, strm->avail_in);
1341
4.66k
    strm->avail_in -= len;
1342
4.66k
    strm->next_in += len;
1343
4.66k
    strm->total_in += len;
1344
1345
    /* return no joy or set up to restart inflate() on a new block */
1346
4.66k
    if (state->have != 4)
1347
0
        return Z_DATA_ERROR;
1348
4.66k
    if (state->flags == -1)
1349
0
        state->wrap = 0;    /* if no header yet, treat as raw */
1350
4.66k
    else
1351
4.66k
        state->wrap &= ~4;  /* no point in computing a check value now */
1352
4.66k
    flags = state->flags;
1353
4.66k
    in = strm->total_in;
1354
4.66k
    out = strm->total_out;
1355
4.66k
    PREFIX(inflateReset)(strm);
1356
4.66k
    strm->total_in = (z_uintmax_t)in; /* Can't use z_size_t here as it will overflow on 64-bit Windows */
1357
4.66k
    strm->total_out = (z_uintmax_t)out;
1358
4.66k
    state->flags = flags;
1359
4.66k
    state->mode = TYPE;
1360
4.66k
    return Z_OK;
1361
4.66k
}
1362
1363
/*
1364
   Returns true if inflate is currently at the end of a block generated by
1365
   Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
1366
   implementation to provide an additional safety check. PPP uses
1367
   Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored
1368
   block. When decompressing, PPP checks that at the end of input packet,
1369
   inflate is waiting for these length bytes.
1370
 */
1371
0
int32_t Z_EXPORT PREFIX(inflateSyncPoint)(PREFIX3(stream) *strm) {
1372
0
    struct inflate_state *state;
1373
1374
0
    if (inflateStateCheck(strm))
1375
0
        return Z_STREAM_ERROR;
1376
0
    INFLATE_SYNC_POINT_HOOK(strm);
1377
0
    state = (struct inflate_state *)strm->state;
1378
0
    return state->mode == STORED && state->bits == 0;
1379
0
}
1380
1381
0
int32_t Z_EXPORT PREFIX(inflateCopy)(PREFIX3(stream) *dest, PREFIX3(stream) *source) {
1382
0
    struct inflate_state *state;
1383
0
    struct inflate_state *copy;
1384
1385
    /* check input */
1386
0
    if (inflateStateCheck(source) || dest == NULL)
1387
0
        return Z_STREAM_ERROR;
1388
0
    state = (struct inflate_state *)source->state;
1389
1390
    /* copy stream */
1391
0
    memcpy(dest, source, sizeof(PREFIX3(stream)));
1392
1393
    /* allocate space */
1394
0
    inflate_allocs *alloc_bufs = alloc_inflate(dest);
1395
0
    if (alloc_bufs == NULL)
1396
0
        return Z_MEM_ERROR;
1397
0
    copy = alloc_bufs->state;
1398
1399
    /* copy state */
1400
0
    memcpy(copy, state, sizeof(struct inflate_state));
1401
0
    copy->strm = dest;
1402
0
    if (state->lencode >= state->codes && state->lencode <= state->codes + ENOUGH - 1) {
1403
0
        copy->lencode = copy->codes + (state->lencode - state->codes);
1404
0
        copy->distcode = copy->codes + (state->distcode - state->codes);
1405
0
    }
1406
0
    copy->next = copy->codes + (state->next - state->codes);
1407
0
    copy->window = alloc_bufs->window;
1408
0
    copy->alloc_bufs = alloc_bufs;
1409
1410
    /* window */
1411
0
    memcpy(copy->window, state->window, INFLATE_ADJUST_WINDOW_SIZE((size_t)state->wsize));
1412
1413
0
    dest->state = (struct internal_state *)copy;
1414
0
    return Z_OK;
1415
0
}
1416
1417
0
int32_t Z_EXPORT PREFIX(inflateUndermine)(PREFIX3(stream) *strm, int32_t subvert) {
1418
#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
1419
    struct inflate_state *state;
1420
1421
    if (inflateStateCheck(strm))
1422
        return Z_STREAM_ERROR;
1423
    state = (struct inflate_state *)strm->state;
1424
    state->sane = !subvert;
1425
    return Z_OK;
1426
#else
1427
0
    Z_UNUSED(strm);
1428
0
    Z_UNUSED(subvert);
1429
0
    return Z_DATA_ERROR;
1430
0
#endif
1431
0
}
1432
1433
0
int32_t Z_EXPORT PREFIX(inflateValidate)(PREFIX3(stream) *strm, int32_t check) {
1434
0
    struct inflate_state *state;
1435
1436
0
    if (inflateStateCheck(strm))
1437
0
        return Z_STREAM_ERROR;
1438
0
    state = (struct inflate_state *)strm->state;
1439
0
    if (check && state->wrap)
1440
0
        state->wrap |= 4;
1441
0
    else
1442
0
        state->wrap &= ~4;
1443
0
    return Z_OK;
1444
0
}
1445
1446
0
long Z_EXPORT PREFIX(inflateMark)(PREFIX3(stream) *strm) {
1447
0
    struct inflate_state *state;
1448
1449
0
    if (inflateStateCheck(strm))
1450
0
        return -65536;
1451
0
    INFLATE_MARK_HOOK(strm);  /* hook for IBM Z DFLTCC */
1452
0
    state = (struct inflate_state *)strm->state;
1453
0
    return (long)(((unsigned long)((long)state->back)) << 16) +
1454
0
        (state->mode == COPY ? state->length :
1455
0
            (state->mode == MATCH ? state->was - state->length : 0));
1456
0
}
1457
1458
0
unsigned long Z_EXPORT PREFIX(inflateCodesUsed)(PREFIX3(stream) *strm) {
1459
0
    struct inflate_state *state;
1460
0
    if (strm == NULL || strm->state == NULL)
1461
0
        return (unsigned long)-1;
1462
0
    state = (struct inflate_state *)strm->state;
1463
0
    return (unsigned long)(state->next - state->codes);
1464
0
}