Coverage Report

Created: 2025-07-12 06:30

/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
3.61k
#  define ZALLOC_STATE(strm, items, size) ZALLOC(strm, items, size)
21
3.61k
#  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
156
#  define ZALLOC_WINDOW(strm, items, size) ZALLOC(strm, items, size)
25
156
#  define ZFREE_WINDOW(strm, addr) ZFREE(strm, addr)
26
/* Invoked at the end of inflateResetKeep(). Useful for initializing arch-specific extension blocks. */
27
3.61k
#  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
11.8k
#  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
14.1k
#  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
7.54k
#  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
18.2k
static int inflateStateCheck(PREFIX3(stream) *strm) {
48
18.2k
    struct inflate_state *state;
49
18.2k
    if (strm == NULL || strm->zalloc == NULL || strm->zfree == NULL)
50
0
        return 1;
51
18.2k
    state = (struct inflate_state *)strm->state;
52
18.2k
    if (state == NULL || state->strm != strm || state->mode < HEAD || state->mode > SYNC)
53
0
        return 1;
54
18.2k
    return 0;
55
18.2k
}
56
57
3.61k
int32_t Z_EXPORT PREFIX(inflateResetKeep)(PREFIX3(stream) *strm) {
58
3.61k
    struct inflate_state *state;
59
60
3.61k
    if (inflateStateCheck(strm))
61
0
        return Z_STREAM_ERROR;
62
3.61k
    state = (struct inflate_state *)strm->state;
63
3.61k
    strm->total_in = strm->total_out = state->total = 0;
64
3.61k
    strm->msg = NULL;
65
3.61k
    if (state->wrap)        /* to support ill-conceived Java test suite */
66
3.61k
        strm->adler = state->wrap & 1;
67
3.61k
    state->mode = HEAD;
68
3.61k
    state->check = ADLER32_INITIAL_VALUE;
69
3.61k
    state->last = 0;
70
3.61k
    state->havedict = 0;
71
3.61k
    state->flags = -1;
72
3.61k
    state->dmax = 32768U;
73
3.61k
    state->head = NULL;
74
3.61k
    state->hold = 0;
75
3.61k
    state->bits = 0;
76
3.61k
    state->lencode = state->distcode = state->next = state->codes;
77
3.61k
    state->sane = 1;
78
3.61k
    state->back = -1;
79
3.61k
    INFLATE_RESET_KEEP_HOOK(strm);  /* hook for IBM Z DFLTCC */
80
3.61k
    Tracev((stderr, "inflate: reset\n"));
81
3.61k
    return Z_OK;
82
3.61k
}
83
84
3.61k
int32_t Z_EXPORT PREFIX(inflateReset)(PREFIX3(stream) *strm) {
85
3.61k
    struct inflate_state *state;
86
87
3.61k
    if (inflateStateCheck(strm))
88
0
        return Z_STREAM_ERROR;
89
3.61k
    state = (struct inflate_state *)strm->state;
90
3.61k
    state->wsize = 0;
91
3.61k
    state->whave = 0;
92
3.61k
    state->wnext = 0;
93
3.61k
    return PREFIX(inflateResetKeep)(strm);
94
3.61k
}
95
96
3.61k
int32_t Z_EXPORT PREFIX(inflateReset2)(PREFIX3(stream) *strm, int32_t windowBits) {
97
3.61k
    int wrap;
98
3.61k
    struct inflate_state *state;
99
100
    /* get the state */
101
3.61k
    if (inflateStateCheck(strm))
102
0
        return Z_STREAM_ERROR;
103
3.61k
    state = (struct inflate_state *)strm->state;
104
105
    /* extract wrap request from windowBits parameter */
106
3.61k
    if (windowBits < 0) {
107
0
        wrap = 0;
108
0
        if (windowBits < -15)
109
0
            return Z_STREAM_ERROR;
110
0
        windowBits = -windowBits;
111
3.61k
    } else {
112
3.61k
        wrap = (windowBits >> 4) + 5;
113
3.61k
#ifdef GUNZIP
114
3.61k
        if (windowBits < 48)
115
3.61k
            windowBits &= 15;
116
3.61k
#endif
117
3.61k
    }
118
119
    /* set number of window bits, free window if different */
120
3.61k
    if (windowBits && (windowBits < 8 || windowBits > 15))
121
0
        return Z_STREAM_ERROR;
122
3.61k
    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
3.61k
    state->wrap = wrap;
129
3.61k
    state->wbits = (unsigned)windowBits;
130
3.61k
    return PREFIX(inflateReset)(strm);
131
3.61k
}
132
133
3.61k
int32_t Z_EXPORT PREFIX(inflateInit2_)(PREFIX3(stream) *strm, int32_t windowBits, const char *version, int32_t stream_size) {
134
3.61k
    int32_t ret;
135
3.61k
    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
3.61k
    if (version == NULL || version[0] != PREFIX2(VERSION)[0] || stream_size != (int)(sizeof(PREFIX3(stream))))
144
0
        return Z_VERSION_ERROR;
145
3.61k
    if (strm == NULL)
146
0
        return Z_STREAM_ERROR;
147
3.61k
    strm->msg = NULL;                   /* in case we return an error */
148
3.61k
    if (strm->zalloc == NULL) {
149
3.61k
        strm->zalloc = zng_calloc;
150
3.61k
        strm->opaque = NULL;
151
3.61k
    }
152
3.61k
    if (strm->zfree == NULL)
153
3.61k
        strm->zfree = zng_cfree;
154
3.61k
    state = (struct inflate_state *) ZALLOC_STATE(strm, 1, sizeof(struct inflate_state));
155
3.61k
    if (state == NULL)
156
0
        return Z_MEM_ERROR;
157
3.61k
    Tracev((stderr, "inflate: allocated\n"));
158
3.61k
    strm->state = (struct internal_state *)state;
159
3.61k
    state->strm = strm;
160
3.61k
    state->window = NULL;
161
3.61k
    state->mode = HEAD;     /* to pass state test in inflateReset2() */
162
3.61k
    state->chunksize = functable.chunksize();
163
3.61k
    ret = PREFIX(inflateReset2)(strm, windowBits);
164
3.61k
    if (ret != Z_OK) {
165
0
        ZFREE_STATE(strm, state);
166
0
        strm->state = NULL;
167
0
    }
168
3.61k
    return ret;
169
3.61k
}
170
171
3.61k
int32_t Z_EXPORT PREFIX(inflateInit_)(PREFIX3(stream) *strm, const char *version, int32_t stream_size) {
172
3.61k
    return PREFIX(inflateInit2_)(strm, DEF_WBITS, version, stream_size);
173
3.61k
}
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
2.54k
void Z_INTERNAL fixedtables(struct inflate_state *state) {
201
2.54k
    state->lencode = lenfix;
202
2.54k
    state->lenbits = 9;
203
2.54k
    state->distcode = distfix;
204
2.54k
    state->distbits = 5;
205
2.54k
}
206
207
312
int Z_INTERNAL inflate_ensure_window(struct inflate_state *state) {
208
    /* if it hasn't been done already, allocate space for the window */
209
312
    if (state->window == NULL) {
210
156
        unsigned wsize = 1U << state->wbits;
211
156
        state->window = (unsigned char *) ZALLOC_WINDOW(state->strm, wsize + state->chunksize, sizeof(unsigned char));
212
156
        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
156
    }
221
222
    /* if window not in use yet, initialize */
223
312
    if (state->wsize == 0) {
224
156
        state->wsize = 1U << state->wbits;
225
156
        state->wnext = 0;
226
156
        state->whave = 0;
227
156
    }
228
229
312
    return 0;
230
312
}
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
312
static int32_t updatewindow(PREFIX3(stream) *strm, const uint8_t *end, uint32_t copy) {
247
312
    struct inflate_state *state;
248
312
    uint32_t dist;
249
250
312
    state = (struct inflate_state *)strm->state;
251
252
312
    if (inflate_ensure_window(state)) return 1;
253
254
    /* copy state->wsize or less output bytes into the circular window */
255
312
    if (copy >= state->wsize) {
256
12
        memcpy(state->window, end - state->wsize, state->wsize);
257
12
        state->wnext = 0;
258
12
        state->whave = state->wsize;
259
300
    } else {
260
300
        dist = state->wsize - state->wnext;
261
300
        if (dist > copy)
262
300
            dist = copy;
263
300
        memcpy(state->window + state->wnext, end - copy, dist);
264
300
        copy -= dist;
265
300
        if (copy) {
266
0
            memcpy(state->window, end - copy, copy);
267
0
            state->wnext = copy;
268
0
            state->whave = state->wsize;
269
300
        } else {
270
300
            state->wnext += dist;
271
300
            if (state->wnext == state->wsize)
272
0
                state->wnext = 0;
273
300
            if (state->whave < state->wsize)
274
288
                state->whave += dist;
275
300
        }
276
300
    }
277
312
    return 0;
278
312
}
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.09M
    do { \
289
1.09M
        if (have == 0) goto inf_leave; \
290
1.09M
        have--; \
291
1.09M
        hold += ((unsigned)(*next++) << bits); \
292
1.09M
        bits += 8; \
293
1.09M
    } 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
3.77k
int32_t Z_EXPORT PREFIX(inflate)(PREFIX3(stream) *strm, int32_t flush) {
378
3.77k
    struct inflate_state *state;
379
3.77k
    const unsigned char *next;  /* next input */
380
3.77k
    unsigned char *put;         /* next output */
381
3.77k
    unsigned have, left;        /* available input and output */
382
3.77k
    uint32_t hold;              /* bit buffer */
383
3.77k
    unsigned bits;              /* bits in bit buffer */
384
3.77k
    uint32_t in, out;           /* save starting available input and output */
385
3.77k
    unsigned copy;              /* number of stored or match bytes to copy */
386
3.77k
    unsigned char *from;        /* where to copy match bytes from */
387
3.77k
    code here;                  /* current decoding table entry */
388
3.77k
    code last;                  /* parent table entry */
389
3.77k
    unsigned len;               /* length to copy for repeats, bits to drop */
390
3.77k
    int32_t ret;                /* return code */
391
3.77k
#ifdef GUNZIP
392
3.77k
    unsigned char hbuf[4];      /* buffer for gzip header crc calculation */
393
3.77k
#endif
394
3.77k
    static const uint16_t order[19] = /* permutation of code lengths */
395
3.77k
        {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
396
397
3.77k
    if (inflateStateCheck(strm) || strm->next_out == NULL ||
398
3.77k
        (strm->next_in == NULL && strm->avail_in != 0))
399
0
        return Z_STREAM_ERROR;
400
401
3.77k
    state = (struct inflate_state *)strm->state;
402
3.77k
    if (state->mode == TYPE)      /* skip check */
403
1
        state->mode = TYPEDO;
404
3.77k
    LOAD();
405
3.77k
    in = have;
406
3.77k
    out = left;
407
3.77k
    ret = Z_OK;
408
3.77k
    for (;;)
409
1.53M
        switch (state->mode) {
410
3.61k
        case HEAD:
411
3.61k
            if (state->wrap == 0) {
412
0
                state->mode = TYPEDO;
413
0
                break;
414
0
            }
415
3.61k
            NEEDBITS(16);
416
3.60k
#ifdef GUNZIP
417
3.60k
            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
3.60k
            if (state->head != NULL)
427
0
                state->head->done = -1;
428
3.60k
            if (!(state->wrap & 1) ||   /* check if zlib header allowed */
429
#else
430
            if (
431
#endif
432
3.60k
                ((BITS(8) << 8) + (hold >> 8)) % 31) {
433
11
                SET_BAD("incorrect header check");
434
11
                break;
435
11
            }
436
3.59k
            if (BITS(4) != Z_DEFLATED) {
437
5
                SET_BAD("unknown compression method");
438
5
                break;
439
5
            }
440
3.58k
            DROPBITS(4);
441
3.58k
            len = BITS(4) + 8;
442
3.58k
            if (state->wbits == 0)
443
0
                state->wbits = len;
444
3.58k
            if (len > 15 || len > state->wbits) {
445
0
                SET_BAD("invalid window size");
446
0
                break;
447
0
            }
448
3.58k
            state->dmax = 1U << len;
449
3.58k
            state->flags = 0;               /* indicate zlib header */
450
3.58k
            Tracev((stderr, "inflate:   zlib header ok\n"));
451
3.58k
            strm->adler = state->check = ADLER32_INITIAL_VALUE;
452
3.58k
            state->mode = hold & 0x200 ? DICTID : TYPE;
453
3.58k
            INITBITS();
454
3.58k
            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
4
        case DICTID:
595
4
            NEEDBITS(32);
596
4
            strm->adler = state->check = ZSWAP32(hold);
597
4
            INITBITS();
598
4
            state->mode = DICT;
599
600
4
        case DICT:
601
4
            if (state->havedict == 0) {
602
4
                RESTORE();
603
4
                return Z_NEED_DICT;
604
4
            }
605
0
            strm->adler = state->check = ADLER32_INITIAL_VALUE;
606
0
            state->mode = TYPE;
607
608
11.8k
        case TYPE:
609
11.8k
            if (flush == Z_BLOCK || flush == Z_TREES)
610
0
                goto inf_leave;
611
612
11.8k
        case TYPEDO:
613
            /* determine and dispatch block type */
614
11.8k
            INFLATE_TYPEDO_HOOK(strm, flush);  /* hook for IBM Z DFLTCC */
615
11.8k
            if (state->last) {
616
3.33k
                BYTEBITS();
617
3.33k
                state->mode = CHECK;
618
3.33k
                break;
619
3.33k
            }
620
8.47k
            NEEDBITS(3);
621
8.46k
            state->last = BITS(1);
622
8.46k
            DROPBITS(1);
623
8.46k
            switch (BITS(2)) {
624
1.06k
            case 0:                             /* stored block */
625
1.06k
                Tracev((stderr, "inflate:     stored block%s\n", state->last ? " (last)" : ""));
626
1.06k
                state->mode = STORED;
627
1.06k
                break;
628
2.54k
            case 1:                             /* fixed block */
629
2.54k
                fixedtables(state);
630
2.54k
                Tracev((stderr, "inflate:     fixed codes block%s\n", state->last ? " (last)" : ""));
631
2.54k
                state->mode = LEN_;             /* decode codes */
632
2.54k
                if (flush == Z_TREES) {
633
0
                    DROPBITS(2);
634
0
                    goto inf_leave;
635
0
                }
636
2.54k
                break;
637
4.86k
            case 2:                             /* dynamic block */
638
4.86k
                Tracev((stderr, "inflate:     dynamic codes block%s\n", state->last ? " (last)" : ""));
639
4.86k
                state->mode = TABLE;
640
4.86k
                break;
641
2
            case 3:
642
2
                SET_BAD("invalid block type");
643
8.46k
            }
644
8.46k
            DROPBITS(2);
645
8.46k
            break;
646
647
1.06k
        case STORED:
648
            /* get and verify stored block length */
649
1.06k
            BYTEBITS();                         /* go to byte boundary */
650
1.06k
            NEEDBITS(32);
651
1.05k
            if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
652
8
                SET_BAD("invalid stored block lengths");
653
8
                break;
654
8
            }
655
1.04k
            state->length = (uint16_t)hold;
656
1.04k
            Tracev((stderr, "inflate:       stored length %u\n", state->length));
657
1.04k
            INITBITS();
658
1.04k
            state->mode = COPY_;
659
1.04k
            if (flush == Z_TREES)
660
0
                goto inf_leave;
661
662
1.04k
        case COPY_:
663
1.04k
            state->mode = COPY;
664
665
1.10k
        case COPY:
666
            /* copy stored block from input to output */
667
1.10k
            copy = state->length;
668
1.10k
            if (copy) {
669
66
                if (copy > have) copy = have;
670
66
                if (copy > left) copy = left;
671
66
                if (copy == 0) goto inf_leave;
672
44
                memcpy(put, next, copy);
673
44
                have -= copy;
674
44
                next += copy;
675
44
                left -= copy;
676
44
                put += copy;
677
44
                state->length -= copy;
678
44
                break;
679
66
            }
680
1.03k
            Tracev((stderr, "inflate:       stored end\n"));
681
1.03k
            state->mode = TYPE;
682
1.03k
            break;
683
684
4.86k
        case TABLE:
685
            /* get dynamic table entries descriptor */
686
4.86k
            NEEDBITS(14);
687
4.86k
            state->nlen = BITS(5) + 257;
688
4.86k
            DROPBITS(5);
689
4.86k
            state->ndist = BITS(5) + 1;
690
4.86k
            DROPBITS(5);
691
4.86k
            state->ncode = BITS(4) + 4;
692
4.86k
            DROPBITS(4);
693
4.86k
#ifndef PKZIP_BUG_WORKAROUND
694
4.86k
            if (state->nlen > 286 || state->ndist > 30) {
695
3
                SET_BAD("too many length or distance symbols");
696
3
                break;
697
3
            }
698
4.85k
#endif
699
4.85k
            Tracev((stderr, "inflate:       table sizes ok\n"));
700
4.85k
            state->have = 0;
701
4.85k
            state->mode = LENLENS;
702
703
4.86k
        case LENLENS:
704
            /* get code length code lengths (not a typo) */
705
82.3k
            while (state->have < state->ncode) {
706
77.4k
                NEEDBITS(3);
707
77.4k
                state->lens[order[state->have++]] = (uint16_t)BITS(3);
708
77.4k
                DROPBITS(3);
709
77.4k
            }
710
19.7k
            while (state->have < 19)
711
14.8k
                state->lens[order[state->have++]] = 0;
712
4.85k
            state->next = state->codes;
713
4.85k
            state->lencode = (const code *)(state->next);
714
4.85k
            state->lenbits = 7;
715
4.85k
            ret = zng_inflate_table(CODES, state->lens, 19, &(state->next), &(state->lenbits), state->work);
716
4.85k
            if (ret) {
717
6
                SET_BAD("invalid code lengths set");
718
6
                break;
719
6
            }
720
4.85k
            Tracev((stderr, "inflate:       code lengths ok\n"));
721
4.85k
            state->have = 0;
722
4.85k
            state->mode = CODELENS;
723
724
4.85k
        case CODELENS:
725
            /* get length and distance code code lengths */
726
904k
            while (state->have < state->nlen + state->ndist) {
727
1.14M
                for (;;) {
728
1.14M
                    here = state->lencode[BITS(state->lenbits)];
729
1.14M
                    if (here.bits <= bits) break;
730
248k
                    PULLBYTE();
731
248k
                }
732
899k
                if (here.val < 16) {
733
833k
                    DROPBITS(here.bits);
734
833k
                    state->lens[state->have++] = here.val;
735
833k
                } else {
736
65.5k
                    if (here.val == 16) {
737
46.9k
                        NEEDBITS(here.bits + 2);
738
46.9k
                        DROPBITS(here.bits);
739
46.9k
                        if (state->have == 0) {
740
1
                            SET_BAD("invalid bit length repeat");
741
1
                            break;
742
1
                        }
743
46.9k
                        len = state->lens[state->have - 1];
744
46.9k
                        copy = 3 + BITS(2);
745
46.9k
                        DROPBITS(2);
746
46.9k
                    } else if (here.val == 17) {
747
12.2k
                        NEEDBITS(here.bits + 3);
748
12.2k
                        DROPBITS(here.bits);
749
12.2k
                        len = 0;
750
12.2k
                        copy = 3 + BITS(3);
751
12.2k
                        DROPBITS(3);
752
12.2k
                    } else {
753
6.25k
                        NEEDBITS(here.bits + 7);
754
6.25k
                        DROPBITS(here.bits);
755
6.25k
                        len = 0;
756
6.25k
                        copy = 11 + BITS(7);
757
6.25k
                        DROPBITS(7);
758
6.25k
                    }
759
65.5k
                    if (state->have + copy > state->nlen + state->ndist) {
760
5
                        SET_BAD("invalid bit length repeat");
761
5
                        break;
762
5
                    }
763
624k
                    while (copy) {
764
558k
                        --copy;
765
558k
                        state->lens[state->have++] = (uint16_t)len;
766
558k
                    }
767
65.5k
                }
768
899k
            }
769
770
            /* handle error breaks in while */
771
4.84k
            if (state->mode == BAD)
772
6
                break;
773
774
            /* check for end-of-block code (better have one) */
775
4.84k
            if (state->lens[256] == 0) {
776
3
                SET_BAD("invalid code -- missing end-of-block");
777
3
                break;
778
3
            }
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
4.83k
            state->next = state->codes;
784
4.83k
            state->lencode = (const code *)(state->next);
785
4.83k
            state->lenbits = 9;
786
4.83k
            ret = zng_inflate_table(LENS, state->lens, state->nlen, &(state->next), &(state->lenbits), state->work);
787
4.83k
            if (ret) {
788
16
                SET_BAD("invalid literal/lengths set");
789
16
                break;
790
16
            }
791
4.82k
            state->distcode = (const code *)(state->next);
792
4.82k
            state->distbits = 6;
793
4.82k
            ret = zng_inflate_table(DISTS, state->lens + state->nlen, state->ndist,
794
4.82k
                            &(state->next), &(state->distbits), state->work);
795
4.82k
            if (ret) {
796
8
                SET_BAD("invalid distances set");
797
8
                break;
798
8
            }
799
4.81k
            Tracev((stderr, "inflate:       codes ok\n"));
800
4.81k
            state->mode = LEN_;
801
4.81k
            if (flush == Z_TREES)
802
0
                goto inf_leave;
803
804
7.35k
        case LEN_:
805
7.35k
            state->mode = LEN;
806
807
766k
        case LEN:
808
            /* use inflate_fast() if we have enough input and output */
809
766k
            if (have >= INFLATE_FAST_MIN_HAVE && left >= INFLATE_FAST_MIN_LEFT) {
810
6.93k
                RESTORE();
811
6.93k
                zng_inflate_fast(strm, out);
812
6.93k
                LOAD();
813
6.93k
                if (state->mode == TYPE)
814
3.66k
                    state->back = -1;
815
6.93k
                break;
816
6.93k
            }
817
759k
            state->back = 0;
818
819
            /* get a literal, length, or end-of-block code */
820
1.49M
            for (;;) {
821
1.49M
                here = state->lencode[BITS(state->lenbits)];
822
1.49M
                if (here.bits <= bits)
823
759k
                    break;
824
736k
                PULLBYTE();
825
736k
            }
826
759k
            if (here.op && (here.op & 0xf0) == 0) {
827
8.81k
                last = here;
828
10.7k
                for (;;) {
829
10.7k
                    here = state->lencode[last.val + (BITS(last.bits + last.op) >> last.bits)];
830
10.7k
                    if ((unsigned)last.bits + (unsigned)here.bits <= bits)
831
8.80k
                        break;
832
1.90k
                    PULLBYTE();
833
1.90k
                }
834
8.80k
                DROPBITS(last.bits);
835
8.80k
                state->back += last.bits;
836
8.80k
            }
837
759k
            DROPBITS(here.bits);
838
759k
            state->back += here.bits;
839
759k
            state->length = here.val;
840
841
            /* process literal */
842
759k
            if ((int)(here.op) == 0) {
843
745k
                Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
844
745k
                        "inflate:         literal '%c'\n" :
845
745k
                        "inflate:         literal 0x%02x\n", here.val));
846
745k
                state->mode = LIT;
847
745k
                break;
848
745k
            }
849
850
            /* process end of block */
851
14.2k
            if (here.op & 32) {
852
3.51k
                Tracevv((stderr, "inflate:         end of block\n"));
853
3.51k
                state->back = -1;
854
3.51k
                state->mode = TYPE;
855
3.51k
                break;
856
3.51k
            }
857
858
            /* invalid code */
859
10.7k
            if (here.op & 64) {
860
0
                SET_BAD("invalid literal/length code");
861
0
                break;
862
0
            }
863
864
            /* length code */
865
10.7k
            state->extra = (here.op & 15);
866
10.7k
            state->mode = LENEXT;
867
868
10.7k
        case LENEXT:
869
            /* get extra bits, if any */
870
10.7k
            if (state->extra) {
871
1.13k
                NEEDBITS(state->extra);
872
1.13k
                state->length += BITS(state->extra);
873
1.13k
                DROPBITS(state->extra);
874
1.13k
                state->back += state->extra;
875
1.13k
            }
876
10.7k
            Tracevv((stderr, "inflate:         length %u\n", state->length));
877
10.7k
            state->was = state->length;
878
10.7k
            state->mode = DIST;
879
880
10.7k
        case DIST:
881
            /* get distance code */
882
15.3k
            for (;;) {
883
15.3k
                here = state->distcode[BITS(state->distbits)];
884
15.3k
                if (here.bits <= bits)
885
10.7k
                    break;
886
4.60k
                PULLBYTE();
887
4.60k
            }
888
10.7k
            if ((here.op & 0xf0) == 0) {
889
169
                last = here;
890
278
                for (;;) {
891
278
                    here = state->distcode[last.val + (BITS(last.bits + last.op) >> last.bits)];
892
278
                    if ((unsigned)last.bits + (unsigned)here.bits <= bits)
893
169
                        break;
894
109
                    PULLBYTE();
895
109
                }
896
169
                DROPBITS(last.bits);
897
169
                state->back += last.bits;
898
169
            }
899
10.7k
            DROPBITS(here.bits);
900
10.7k
            state->back += here.bits;
901
10.7k
            if (here.op & 64) {
902
10
                SET_BAD("invalid distance code");
903
10
                break;
904
10
            }
905
10.7k
            state->offset = here.val;
906
10.7k
            state->extra = (here.op & 15);
907
10.7k
            state->mode = DISTEXT;
908
909
10.7k
        case DISTEXT:
910
            /* get distance extra bits, if any */
911
10.7k
            if (state->extra) {
912
8.95k
                NEEDBITS(state->extra);
913
8.95k
                state->offset += BITS(state->extra);
914
8.95k
                DROPBITS(state->extra);
915
8.95k
                state->back += state->extra;
916
8.95k
            }
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.7k
            Tracevv((stderr, "inflate:         distance %u\n", state->offset));
924
10.7k
            state->mode = MATCH;
925
926
10.8k
        case MATCH:
927
            /* copy match from window to output */
928
10.8k
            if (left == 0) goto inf_leave;
929
10.7k
            copy = out - left;
930
10.7k
            if (state->offset > copy) {         /* copy from window */
931
11
                copy = state->offset - copy;
932
11
                if (copy > state->whave) {
933
11
                    if (state->sane) {
934
11
                        SET_BAD("invalid distance too far back");
935
11
                        break;
936
11
                    }
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
11
                }
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.7k
            } else {                             /* copy from output */
967
10.7k
                copy = state->length;
968
10.7k
                if (copy > left)
969
73
                    copy = left;
970
971
10.7k
                put = functable.chunkmemset_safe(put, state->offset, copy, left);
972
10.7k
            }
973
10.7k
            left -= copy;
974
10.7k
            state->length -= copy;
975
10.7k
            if (state->length == 0)
976
10.6k
                state->mode = LEN;
977
10.7k
            break;
978
979
745k
        case LIT:
980
745k
            if (left == 0)
981
46
                goto inf_leave;
982
744k
            *put++ = (unsigned char)(state->length);
983
744k
            left--;
984
744k
            state->mode = LEN;
985
744k
            break;
986
987
3.33k
        case CHECK:
988
3.33k
            if (state->wrap) {
989
3.33k
                NEEDBITS(32);
990
3.32k
                out -= left;
991
3.32k
                strm->total_out += out;
992
3.32k
                state->total += out;
993
3.32k
                if (INFLATE_NEED_CHECKSUM(strm) && (state->wrap & 4) && out)
994
3.32k
                    strm->adler = state->check = UPDATE(state->check, put - out, out);
995
3.32k
                out = left;
996
3.32k
                if ((state->wrap & 4) && (
997
3.32k
#ifdef GUNZIP
998
3.32k
                     state->flags ? hold :
999
3.32k
#endif
1000
3.32k
                     ZSWAP32(hold)) != state->check) {
1001
29
                    SET_BAD("incorrect data check");
1002
29
                    break;
1003
29
                }
1004
3.30k
                INITBITS();
1005
3.30k
                Tracev((stderr, "inflate:   check matches trailer\n"));
1006
3.30k
            }
1007
3.30k
#ifdef GUNZIP
1008
3.30k
            state->mode = LENGTH;
1009
1010
3.30k
        case LENGTH:
1011
3.30k
            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
3.30k
#endif
1021
3.30k
            state->mode = DONE;
1022
1023
3.30k
        case DONE:
1024
            /* inflate stream terminated properly */
1025
3.30k
            ret = Z_STREAM_END;
1026
3.30k
            goto inf_leave;
1027
1028
142
        case BAD:
1029
142
            ret = Z_DATA_ERROR;
1030
142
            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.53M
        }
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
3.77k
  inf_leave:
1048
3.77k
    RESTORE();
1049
3.77k
    if (INFLATE_NEED_UPDATEWINDOW(strm) &&
1050
3.77k
            (state->wsize || (out != strm->avail_out && state->mode < BAD &&
1051
3.61k
                 (state->mode < CHECK || flush != Z_FINISH)))) {
1052
312
        if (updatewindow(strm, strm->next_out, out - strm->avail_out)) {
1053
0
            state->mode = MEM;
1054
0
            return Z_MEM_ERROR;
1055
0
        }
1056
312
    }
1057
3.77k
    in -= strm->avail_in;
1058
3.77k
    out -= strm->avail_out;
1059
3.77k
    strm->total_in += in;
1060
3.77k
    strm->total_out += out;
1061
3.77k
    state->total += out;
1062
3.77k
    if (INFLATE_NEED_CHECKSUM(strm) && (state->wrap & 4) && out)
1063
234
        strm->adler = state->check = UPDATE(state->check, strm->next_out - out, out);
1064
3.77k
    strm->data_type = (int)state->bits + (state->last ? 64 : 0) +
1065
3.77k
                      (state->mode == TYPE ? 128 : 0) + (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0);
1066
3.77k
    if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
1067
164
        ret = Z_BUF_ERROR;
1068
3.77k
    return ret;
1069
3.77k
}
1070
1071
3.61k
int32_t Z_EXPORT PREFIX(inflateEnd)(PREFIX3(stream) *strm) {
1072
3.61k
    struct inflate_state *state;
1073
3.61k
    if (inflateStateCheck(strm))
1074
0
        return Z_STREAM_ERROR;
1075
3.61k
    state = (struct inflate_state *)strm->state;
1076
3.61k
    if (state->window != NULL)
1077
3.61k
        ZFREE_WINDOW(strm, state->window);
1078
3.61k
    ZFREE_STATE(strm, strm->state);
1079
3.61k
    strm->state = NULL;
1080
3.61k
    Tracev((stderr, "inflate: end\n"));
1081
3.61k
    return Z_OK;
1082
3.61k
}
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
}