Coverage Report

Created: 2026-02-14 07:07

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