Coverage Report

Created: 2026-07-16 06:59

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