Coverage Report

Created: 2026-10-17 07:13

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