Coverage Report

Created: 2025-12-11 06:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/c-blosc2/internal-complibs/zlib-ng-2.0.7/inflate.c
Line
Count
Source
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
0
#  define ZALLOC_STATE(strm, items, size) ZALLOC(strm, items, size)
21
0
#  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
0
#  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
0
#  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
0
#  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
0
#  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
0
static int inflateStateCheck(PREFIX3(stream) *strm) {
48
0
    struct inflate_state *state;
49
0
    if (strm == NULL || strm->zalloc == NULL || strm->zfree == NULL)
50
0
        return 1;
51
0
    state = (struct inflate_state *)strm->state;
52
0
    if (state == NULL || state->strm != strm || state->mode < HEAD || state->mode > SYNC)
53
0
        return 1;
54
0
    return 0;
55
0
}
56
57
0
int32_t Z_EXPORT PREFIX(inflateResetKeep)(PREFIX3(stream) *strm) {
58
0
    struct inflate_state *state;
59
60
0
    if (inflateStateCheck(strm))
61
0
        return Z_STREAM_ERROR;
62
0
    state = (struct inflate_state *)strm->state;
63
0
    strm->total_in = strm->total_out = state->total = 0;
64
0
    strm->msg = NULL;
65
0
    if (state->wrap)        /* to support ill-conceived Java test suite */
66
0
        strm->adler = state->wrap & 1;
67
0
    state->mode = HEAD;
68
0
    state->check = ADLER32_INITIAL_VALUE;
69
0
    state->last = 0;
70
0
    state->havedict = 0;
71
0
    state->flags = -1;
72
0
    state->dmax = 32768U;
73
0
    state->head = NULL;
74
0
    state->hold = 0;
75
0
    state->bits = 0;
76
0
    state->lencode = state->distcode = state->next = state->codes;
77
0
    state->sane = 1;
78
0
    state->back = -1;
79
0
    INFLATE_RESET_KEEP_HOOK(strm);  /* hook for IBM Z DFLTCC */
80
0
    Tracev((stderr, "inflate: reset\n"));
81
0
    return Z_OK;
82
0
}
83
84
0
int32_t Z_EXPORT PREFIX(inflateReset)(PREFIX3(stream) *strm) {
85
0
    struct inflate_state *state;
86
87
0
    if (inflateStateCheck(strm))
88
0
        return Z_STREAM_ERROR;
89
0
    state = (struct inflate_state *)strm->state;
90
0
    state->wsize = 0;
91
0
    state->whave = 0;
92
0
    state->wnext = 0;
93
0
    return PREFIX(inflateResetKeep)(strm);
94
0
}
95
96
0
int32_t Z_EXPORT PREFIX(inflateReset2)(PREFIX3(stream) *strm, int32_t windowBits) {
97
0
    int wrap;
98
0
    struct inflate_state *state;
99
100
    /* get the state */
101
0
    if (inflateStateCheck(strm))
102
0
        return Z_STREAM_ERROR;
103
0
    state = (struct inflate_state *)strm->state;
104
105
    /* extract wrap request from windowBits parameter */
106
0
    if (windowBits < 0) {
107
0
        wrap = 0;
108
0
        if (windowBits < -15)
109
0
            return Z_STREAM_ERROR;
110
0
        windowBits = -windowBits;
111
0
    } else {
112
0
        wrap = (windowBits >> 4) + 5;
113
0
#ifdef GUNZIP
114
0
        if (windowBits < 48)
115
0
            windowBits &= 15;
116
0
#endif
117
0
    }
118
119
    /* set number of window bits, free window if different */
120
0
    if (windowBits && (windowBits < 8 || windowBits > 15))
121
0
        return Z_STREAM_ERROR;
122
0
    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
0
    state->wrap = wrap;
129
0
    state->wbits = (unsigned)windowBits;
130
0
    return PREFIX(inflateReset)(strm);
131
0
}
132
133
0
int32_t Z_EXPORT PREFIX(inflateInit2_)(PREFIX3(stream) *strm, int32_t windowBits, const char *version, int32_t stream_size) {
134
0
    int32_t ret;
135
0
    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
0
    if (version == NULL || version[0] != PREFIX2(VERSION)[0] || stream_size != (int)(sizeof(PREFIX3(stream))))
144
0
        return Z_VERSION_ERROR;
145
0
    if (strm == NULL)
146
0
        return Z_STREAM_ERROR;
147
0
    strm->msg = NULL;                   /* in case we return an error */
148
0
    if (strm->zalloc == NULL) {
149
0
        strm->zalloc = zng_calloc;
150
0
        strm->opaque = NULL;
151
0
    }
152
0
    if (strm->zfree == NULL)
153
0
        strm->zfree = zng_cfree;
154
0
    state = (struct inflate_state *) ZALLOC_STATE(strm, 1, sizeof(struct inflate_state));
155
0
    if (state == NULL)
156
0
        return Z_MEM_ERROR;
157
0
    Tracev((stderr, "inflate: allocated\n"));
158
0
    strm->state = (struct internal_state *)state;
159
0
    state->strm = strm;
160
0
    state->window = NULL;
161
0
    state->mode = HEAD;     /* to pass state test in inflateReset2() */
162
0
    state->chunksize = functable.chunksize();
163
0
    ret = PREFIX(inflateReset2)(strm, windowBits);
164
0
    if (ret != Z_OK) {
165
0
        ZFREE_STATE(strm, state);
166
0
        strm->state = NULL;
167
0
    }
168
0
    return ret;
169
0
}
170
171
0
int32_t Z_EXPORT PREFIX(inflateInit_)(PREFIX3(stream) *strm, const char *version, int32_t stream_size) {
172
0
    return PREFIX(inflateInit2_)(strm, DEF_WBITS, version, stream_size);
173
0
}
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
0
void Z_INTERNAL fixedtables(struct inflate_state *state) {
201
0
    state->lencode = lenfix;
202
0
    state->lenbits = 9;
203
0
    state->distcode = distfix;
204
0
    state->distbits = 5;
205
0
}
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
0
    do { \
289
0
        if (have == 0) goto inf_leave; \
290
0
        have--; \
291
0
        hold += ((unsigned)(*next++) << bits); \
292
0
        bits += 8; \
293
0
    } 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
0
int32_t Z_EXPORT PREFIX(inflate)(PREFIX3(stream) *strm, int32_t flush) {
378
0
    struct inflate_state *state;
379
0
    const unsigned char *next;  /* next input */
380
0
    unsigned char *put;         /* next output */
381
0
    unsigned have, left;        /* available input and output */
382
0
    uint32_t hold;              /* bit buffer */
383
0
    unsigned bits;              /* bits in bit buffer */
384
0
    uint32_t in, out;           /* save starting available input and output */
385
0
    unsigned copy;              /* number of stored or match bytes to copy */
386
0
    unsigned char *from;        /* where to copy match bytes from */
387
0
    code here;                  /* current decoding table entry */
388
0
    code last;                  /* parent table entry */
389
0
    unsigned len;               /* length to copy for repeats, bits to drop */
390
0
    int32_t ret;                /* return code */
391
0
#ifdef GUNZIP
392
0
    unsigned char hbuf[4];      /* buffer for gzip header crc calculation */
393
0
#endif
394
0
    static const uint16_t order[19] = /* permutation of code lengths */
395
0
        {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
396
397
0
    if (inflateStateCheck(strm) || strm->next_out == NULL ||
398
0
        (strm->next_in == NULL && strm->avail_in != 0))
399
0
        return Z_STREAM_ERROR;
400
401
0
    state = (struct inflate_state *)strm->state;
402
0
    if (state->mode == TYPE)      /* skip check */
403
0
        state->mode = TYPEDO;
404
0
    LOAD();
405
0
    in = have;
406
0
    out = left;
407
0
    ret = Z_OK;
408
0
    for (;;)
409
0
        switch (state->mode) {
410
0
        case HEAD:
411
0
            if (state->wrap == 0) {
412
0
                state->mode = TYPEDO;
413
0
                break;
414
0
            }
415
0
            NEEDBITS(16);
416
0
#ifdef GUNZIP
417
0
            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
0
            if (state->head != NULL)
427
0
                state->head->done = -1;
428
0
            if (!(state->wrap & 1) ||   /* check if zlib header allowed */
429
#else
430
            if (
431
#endif
432
0
                ((BITS(8) << 8) + (hold >> 8)) % 31) {
433
0
                SET_BAD("incorrect header check");
434
0
                break;
435
0
            }
436
0
            if (BITS(4) != Z_DEFLATED) {
437
0
                SET_BAD("unknown compression method");
438
0
                break;
439
0
            }
440
0
            DROPBITS(4);
441
0
            len = BITS(4) + 8;
442
0
            if (state->wbits == 0)
443
0
                state->wbits = len;
444
0
            if (len > 15 || len > state->wbits) {
445
0
                SET_BAD("invalid window size");
446
0
                break;
447
0
            }
448
0
            state->dmax = 1U << len;
449
0
            state->flags = 0;               /* indicate zlib header */
450
0
            Tracev((stderr, "inflate:   zlib header ok\n"));
451
0
            strm->adler = state->check = ADLER32_INITIAL_VALUE;
452
0
            state->mode = hold & 0x200 ? DICTID : TYPE;
453
0
            INITBITS();
454
0
            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
0
        case TYPE:
609
0
            if (flush == Z_BLOCK || flush == Z_TREES)
610
0
                goto inf_leave;
611
612
0
        case TYPEDO:
613
            /* determine and dispatch block type */
614
0
            INFLATE_TYPEDO_HOOK(strm, flush);  /* hook for IBM Z DFLTCC */
615
0
            if (state->last) {
616
0
                BYTEBITS();
617
0
                state->mode = CHECK;
618
0
                break;
619
0
            }
620
0
            NEEDBITS(3);
621
0
            state->last = BITS(1);
622
0
            DROPBITS(1);
623
0
            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
0
            case 1:                             /* fixed block */
629
0
                fixedtables(state);
630
0
                Tracev((stderr, "inflate:     fixed codes block%s\n", state->last ? " (last)" : ""));
631
0
                state->mode = LEN_;             /* decode codes */
632
0
                if (flush == Z_TREES) {
633
0
                    DROPBITS(2);
634
0
                    goto inf_leave;
635
0
                }
636
0
                break;
637
0
            case 2:                             /* dynamic block */
638
0
                Tracev((stderr, "inflate:     dynamic codes block%s\n", state->last ? " (last)" : ""));
639
0
                state->mode = TABLE;
640
0
                break;
641
0
            case 3:
642
0
                SET_BAD("invalid block type");
643
0
            }
644
0
            DROPBITS(2);
645
0
            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
0
        case TABLE:
685
            /* get dynamic table entries descriptor */
686
0
            NEEDBITS(14);
687
0
            state->nlen = BITS(5) + 257;
688
0
            DROPBITS(5);
689
0
            state->ndist = BITS(5) + 1;
690
0
            DROPBITS(5);
691
0
            state->ncode = BITS(4) + 4;
692
0
            DROPBITS(4);
693
0
#ifndef PKZIP_BUG_WORKAROUND
694
0
            if (state->nlen > 286 || state->ndist > 30) {
695
0
                SET_BAD("too many length or distance symbols");
696
0
                break;
697
0
            }
698
0
#endif
699
0
            Tracev((stderr, "inflate:       table sizes ok\n"));
700
0
            state->have = 0;
701
0
            state->mode = LENLENS;
702
703
0
        case LENLENS:
704
            /* get code length code lengths (not a typo) */
705
0
            while (state->have < state->ncode) {
706
0
                NEEDBITS(3);
707
0
                state->lens[order[state->have++]] = (uint16_t)BITS(3);
708
0
                DROPBITS(3);
709
0
            }
710
0
            while (state->have < 19)
711
0
                state->lens[order[state->have++]] = 0;
712
0
            state->next = state->codes;
713
0
            state->lencode = (const code *)(state->next);
714
0
            state->lenbits = 7;
715
0
            ret = zng_inflate_table(CODES, state->lens, 19, &(state->next), &(state->lenbits), state->work);
716
0
            if (ret) {
717
0
                SET_BAD("invalid code lengths set");
718
0
                break;
719
0
            }
720
0
            Tracev((stderr, "inflate:       code lengths ok\n"));
721
0
            state->have = 0;
722
0
            state->mode = CODELENS;
723
724
0
        case CODELENS:
725
            /* get length and distance code code lengths */
726
0
            while (state->have < state->nlen + state->ndist) {
727
0
                for (;;) {
728
0
                    here = state->lencode[BITS(state->lenbits)];
729
0
                    if (here.bits <= bits) break;
730
0
                    PULLBYTE();
731
0
                }
732
0
                if (here.val < 16) {
733
0
                    DROPBITS(here.bits);
734
0
                    state->lens[state->have++] = here.val;
735
0
                } else {
736
0
                    if (here.val == 16) {
737
0
                        NEEDBITS(here.bits + 2);
738
0
                        DROPBITS(here.bits);
739
0
                        if (state->have == 0) {
740
0
                            SET_BAD("invalid bit length repeat");
741
0
                            break;
742
0
                        }
743
0
                        len = state->lens[state->have - 1];
744
0
                        copy = 3 + BITS(2);
745
0
                        DROPBITS(2);
746
0
                    } else if (here.val == 17) {
747
0
                        NEEDBITS(here.bits + 3);
748
0
                        DROPBITS(here.bits);
749
0
                        len = 0;
750
0
                        copy = 3 + BITS(3);
751
0
                        DROPBITS(3);
752
0
                    } else {
753
0
                        NEEDBITS(here.bits + 7);
754
0
                        DROPBITS(here.bits);
755
0
                        len = 0;
756
0
                        copy = 11 + BITS(7);
757
0
                        DROPBITS(7);
758
0
                    }
759
0
                    if (state->have + copy > state->nlen + state->ndist) {
760
0
                        SET_BAD("invalid bit length repeat");
761
0
                        break;
762
0
                    }
763
0
                    while (copy) {
764
0
                        --copy;
765
0
                        state->lens[state->have++] = (uint16_t)len;
766
0
                    }
767
0
                }
768
0
            }
769
770
            /* handle error breaks in while */
771
0
            if (state->mode == BAD)
772
0
                break;
773
774
            /* check for end-of-block code (better have one) */
775
0
            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
0
            state->next = state->codes;
784
0
            state->lencode = (const code *)(state->next);
785
0
            state->lenbits = 9;
786
0
            ret = zng_inflate_table(LENS, state->lens, state->nlen, &(state->next), &(state->lenbits), state->work);
787
0
            if (ret) {
788
0
                SET_BAD("invalid literal/lengths set");
789
0
                break;
790
0
            }
791
0
            state->distcode = (const code *)(state->next);
792
0
            state->distbits = 6;
793
0
            ret = zng_inflate_table(DISTS, state->lens + state->nlen, state->ndist,
794
0
                            &(state->next), &(state->distbits), state->work);
795
0
            if (ret) {
796
0
                SET_BAD("invalid distances set");
797
0
                break;
798
0
            }
799
0
            Tracev((stderr, "inflate:       codes ok\n"));
800
0
            state->mode = LEN_;
801
0
            if (flush == Z_TREES)
802
0
                goto inf_leave;
803
804
0
        case LEN_:
805
0
            state->mode = LEN;
806
807
0
        case LEN:
808
            /* use inflate_fast() if we have enough input and output */
809
0
            if (have >= INFLATE_FAST_MIN_HAVE && left >= INFLATE_FAST_MIN_LEFT) {
810
0
                RESTORE();
811
0
                zng_inflate_fast(strm, out);
812
0
                LOAD();
813
0
                if (state->mode == TYPE)
814
0
                    state->back = -1;
815
0
                break;
816
0
            }
817
0
            state->back = 0;
818
819
            /* get a literal, length, or end-of-block code */
820
0
            for (;;) {
821
0
                here = state->lencode[BITS(state->lenbits)];
822
0
                if (here.bits <= bits)
823
0
                    break;
824
0
                PULLBYTE();
825
0
            }
826
0
            if (here.op && (here.op & 0xf0) == 0) {
827
0
                last = here;
828
0
                for (;;) {
829
0
                    here = state->lencode[last.val + (BITS(last.bits + last.op) >> last.bits)];
830
0
                    if ((unsigned)last.bits + (unsigned)here.bits <= bits)
831
0
                        break;
832
0
                    PULLBYTE();
833
0
                }
834
0
                DROPBITS(last.bits);
835
0
                state->back += last.bits;
836
0
            }
837
0
            DROPBITS(here.bits);
838
0
            state->back += here.bits;
839
0
            state->length = here.val;
840
841
            /* process literal */
842
0
            if ((int)(here.op) == 0) {
843
0
                Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
844
0
                        "inflate:         literal '%c'\n" :
845
0
                        "inflate:         literal 0x%02x\n", here.val));
846
0
                state->mode = LIT;
847
0
                break;
848
0
            }
849
850
            /* process end of block */
851
0
            if (here.op & 32) {
852
0
                Tracevv((stderr, "inflate:         end of block\n"));
853
0
                state->back = -1;
854
0
                state->mode = TYPE;
855
0
                break;
856
0
            }
857
858
            /* invalid code */
859
0
            if (here.op & 64) {
860
0
                SET_BAD("invalid literal/length code");
861
0
                break;
862
0
            }
863
864
            /* length code */
865
0
            state->extra = (here.op & 15);
866
0
            state->mode = LENEXT;
867
868
0
        case LENEXT:
869
            /* get extra bits, if any */
870
0
            if (state->extra) {
871
0
                NEEDBITS(state->extra);
872
0
                state->length += BITS(state->extra);
873
0
                DROPBITS(state->extra);
874
0
                state->back += state->extra;
875
0
            }
876
0
            Tracevv((stderr, "inflate:         length %u\n", state->length));
877
0
            state->was = state->length;
878
0
            state->mode = DIST;
879
880
0
        case DIST:
881
            /* get distance code */
882
0
            for (;;) {
883
0
                here = state->distcode[BITS(state->distbits)];
884
0
                if (here.bits <= bits)
885
0
                    break;
886
0
                PULLBYTE();
887
0
            }
888
0
            if ((here.op & 0xf0) == 0) {
889
0
                last = here;
890
0
                for (;;) {
891
0
                    here = state->distcode[last.val + (BITS(last.bits + last.op) >> last.bits)];
892
0
                    if ((unsigned)last.bits + (unsigned)here.bits <= bits)
893
0
                        break;
894
0
                    PULLBYTE();
895
0
                }
896
0
                DROPBITS(last.bits);
897
0
                state->back += last.bits;
898
0
            }
899
0
            DROPBITS(here.bits);
900
0
            state->back += here.bits;
901
0
            if (here.op & 64) {
902
0
                SET_BAD("invalid distance code");
903
0
                break;
904
0
            }
905
0
            state->offset = here.val;
906
0
            state->extra = (here.op & 15);
907
0
            state->mode = DISTEXT;
908
909
0
        case DISTEXT:
910
            /* get distance extra bits, if any */
911
0
            if (state->extra) {
912
0
                NEEDBITS(state->extra);
913
0
                state->offset += BITS(state->extra);
914
0
                DROPBITS(state->extra);
915
0
                state->back += state->extra;
916
0
            }
917
#ifdef INFLATE_STRICT
918
            if (state->offset > state->dmax) {
919
                SET_BAD("invalid distance too far back");
920
                break;
921
            }
922
#endif
923
0
            Tracevv((stderr, "inflate:         distance %u\n", state->offset));
924
0
            state->mode = MATCH;
925
926
0
        case MATCH:
927
            /* copy match from window to output */
928
0
            if (left == 0) goto inf_leave;
929
0
            copy = out - left;
930
0
            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
0
            } else {                             /* copy from output */
967
0
                copy = state->length;
968
0
                if (copy > left)
969
0
                    copy = left;
970
971
0
                put = functable.chunkmemset_safe(put, state->offset, copy, left);
972
0
            }
973
0
            left -= copy;
974
0
            state->length -= copy;
975
0
            if (state->length == 0)
976
0
                state->mode = LEN;
977
0
            break;
978
979
0
        case LIT:
980
0
            if (left == 0)
981
0
                goto inf_leave;
982
0
            *put++ = (unsigned char)(state->length);
983
0
            left--;
984
0
            state->mode = LEN;
985
0
            break;
986
987
0
        case CHECK:
988
0
            if (state->wrap) {
989
0
                NEEDBITS(32);
990
0
                out -= left;
991
0
                strm->total_out += out;
992
0
                state->total += out;
993
0
                if (INFLATE_NEED_CHECKSUM(strm) && (state->wrap & 4) && out)
994
0
                    strm->adler = state->check = UPDATE(state->check, put - out, out);
995
0
                out = left;
996
0
                if ((state->wrap & 4) && (
997
0
#ifdef GUNZIP
998
0
                     state->flags ? hold :
999
0
#endif
1000
0
                     ZSWAP32(hold)) != state->check) {
1001
0
                    SET_BAD("incorrect data check");
1002
0
                    break;
1003
0
                }
1004
0
                INITBITS();
1005
0
                Tracev((stderr, "inflate:   check matches trailer\n"));
1006
0
            }
1007
0
#ifdef GUNZIP
1008
0
            state->mode = LENGTH;
1009
1010
0
        case LENGTH:
1011
0
            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
0
#endif
1021
0
            state->mode = DONE;
1022
1023
0
        case DONE:
1024
            /* inflate stream terminated properly */
1025
0
            ret = Z_STREAM_END;
1026
0
            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
0
        }
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
0
  inf_leave:
1048
0
    RESTORE();
1049
0
    if (INFLATE_NEED_UPDATEWINDOW(strm) &&
1050
0
            (state->wsize || (out != strm->avail_out && state->mode < BAD &&
1051
0
                 (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
0
    in -= strm->avail_in;
1058
0
    out -= strm->avail_out;
1059
0
    strm->total_in += in;
1060
0
    strm->total_out += out;
1061
0
    state->total += out;
1062
0
    if (INFLATE_NEED_CHECKSUM(strm) && (state->wrap & 4) && out)
1063
0
        strm->adler = state->check = UPDATE(state->check, strm->next_out - out, out);
1064
0
    strm->data_type = (int)state->bits + (state->last ? 64 : 0) +
1065
0
                      (state->mode == TYPE ? 128 : 0) + (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0);
1066
0
    if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
1067
0
        ret = Z_BUF_ERROR;
1068
0
    return ret;
1069
0
}
1070
1071
0
int32_t Z_EXPORT PREFIX(inflateEnd)(PREFIX3(stream) *strm) {
1072
0
    struct inflate_state *state;
1073
0
    if (inflateStateCheck(strm))
1074
0
        return Z_STREAM_ERROR;
1075
0
    state = (struct inflate_state *)strm->state;
1076
0
    if (state->window != NULL)
1077
0
        ZFREE_WINDOW(strm, state->window);
1078
0
    ZFREE_STATE(strm, strm->state);
1079
0
    strm->state = NULL;
1080
0
    Tracev((stderr, "inflate: end\n"));
1081
0
    return Z_OK;
1082
0
}
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
}