Coverage Report

Created: 2025-07-18 06:49

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