Coverage Report

Created: 2026-11-23 06:53

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