1275793eaSopenharmony_ci/* infback.c -- inflate using a call-back interface
2275793eaSopenharmony_ci * Copyright (C) 1995-2022 Mark Adler
3275793eaSopenharmony_ci * For conditions of distribution and use, see copyright notice in zlib.h
4275793eaSopenharmony_ci */
5275793eaSopenharmony_ci
6275793eaSopenharmony_ci/*
7275793eaSopenharmony_ci   This code is largely copied from inflate.c.  Normally either infback.o or
8275793eaSopenharmony_ci   inflate.o would be linked into an application--not both.  The interface
9275793eaSopenharmony_ci   with inffast.c is retained so that optimized assembler-coded versions of
10275793eaSopenharmony_ci   inflate_fast() can be used with either inflate.c or infback.c.
11275793eaSopenharmony_ci */
12275793eaSopenharmony_ci
13275793eaSopenharmony_ci#include "zutil.h"
14275793eaSopenharmony_ci#include "inftrees.h"
15275793eaSopenharmony_ci#include "inflate.h"
16275793eaSopenharmony_ci#include "inffast.h"
17275793eaSopenharmony_ci
18275793eaSopenharmony_ci/*
19275793eaSopenharmony_ci   strm provides memory allocation functions in zalloc and zfree, or
20275793eaSopenharmony_ci   Z_NULL to use the library memory allocation functions.
21275793eaSopenharmony_ci
22275793eaSopenharmony_ci   windowBits is in the range 8..15, and window is a user-supplied
23275793eaSopenharmony_ci   window and output buffer that is 2**windowBits bytes.
24275793eaSopenharmony_ci */
25275793eaSopenharmony_ciint ZEXPORT inflateBackInit_(z_streamp strm, int windowBits,
26275793eaSopenharmony_ci                             unsigned char FAR *window, const char *version,
27275793eaSopenharmony_ci                             int stream_size) {
28275793eaSopenharmony_ci    struct inflate_state FAR *state;
29275793eaSopenharmony_ci
30275793eaSopenharmony_ci    if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
31275793eaSopenharmony_ci        stream_size != (int)(sizeof(z_stream)))
32275793eaSopenharmony_ci        return Z_VERSION_ERROR;
33275793eaSopenharmony_ci    if (strm == Z_NULL || window == Z_NULL ||
34275793eaSopenharmony_ci        windowBits < 8 || windowBits > 15)
35275793eaSopenharmony_ci        return Z_STREAM_ERROR;
36275793eaSopenharmony_ci    strm->msg = Z_NULL;                 /* in case we return an error */
37275793eaSopenharmony_ci    if (strm->zalloc == (alloc_func)0) {
38275793eaSopenharmony_ci#ifdef Z_SOLO
39275793eaSopenharmony_ci        return Z_STREAM_ERROR;
40275793eaSopenharmony_ci#else
41275793eaSopenharmony_ci        strm->zalloc = zcalloc;
42275793eaSopenharmony_ci        strm->opaque = (voidpf)0;
43275793eaSopenharmony_ci#endif
44275793eaSopenharmony_ci    }
45275793eaSopenharmony_ci    if (strm->zfree == (free_func)0)
46275793eaSopenharmony_ci#ifdef Z_SOLO
47275793eaSopenharmony_ci        return Z_STREAM_ERROR;
48275793eaSopenharmony_ci#else
49275793eaSopenharmony_ci    strm->zfree = zcfree;
50275793eaSopenharmony_ci#endif
51275793eaSopenharmony_ci    state = (struct inflate_state FAR *)ZALLOC(strm, 1,
52275793eaSopenharmony_ci                                               sizeof(struct inflate_state));
53275793eaSopenharmony_ci    if (state == Z_NULL) return Z_MEM_ERROR;
54275793eaSopenharmony_ci    Tracev((stderr, "inflate: allocated\n"));
55275793eaSopenharmony_ci    strm->state = (struct internal_state FAR *)state;
56275793eaSopenharmony_ci    state->dmax = 32768U;
57275793eaSopenharmony_ci    state->wbits = (uInt)windowBits;
58275793eaSopenharmony_ci    state->wsize = 1U << windowBits;
59275793eaSopenharmony_ci    state->window = window;
60275793eaSopenharmony_ci    state->wnext = 0;
61275793eaSopenharmony_ci    state->whave = 0;
62275793eaSopenharmony_ci    state->sane = 1;
63275793eaSopenharmony_ci    return Z_OK;
64275793eaSopenharmony_ci}
65275793eaSopenharmony_ci
66275793eaSopenharmony_ci/*
67275793eaSopenharmony_ci   Return state with length and distance decoding tables and index sizes set to
68275793eaSopenharmony_ci   fixed code decoding.  Normally this returns fixed tables from inffixed.h.
69275793eaSopenharmony_ci   If BUILDFIXED is defined, then instead this routine builds the tables the
70275793eaSopenharmony_ci   first time it's called, and returns those tables the first time and
71275793eaSopenharmony_ci   thereafter.  This reduces the size of the code by about 2K bytes, in
72275793eaSopenharmony_ci   exchange for a little execution time.  However, BUILDFIXED should not be
73275793eaSopenharmony_ci   used for threaded applications, since the rewriting of the tables and virgin
74275793eaSopenharmony_ci   may not be thread-safe.
75275793eaSopenharmony_ci */
76275793eaSopenharmony_cilocal void fixedtables(struct inflate_state FAR *state) {
77275793eaSopenharmony_ci#ifdef BUILDFIXED
78275793eaSopenharmony_ci    static int virgin = 1;
79275793eaSopenharmony_ci    static code *lenfix, *distfix;
80275793eaSopenharmony_ci    static code fixed[544];
81275793eaSopenharmony_ci
82275793eaSopenharmony_ci    /* build fixed huffman tables if first call (may not be thread safe) */
83275793eaSopenharmony_ci    if (virgin) {
84275793eaSopenharmony_ci        unsigned sym, bits;
85275793eaSopenharmony_ci        static code *next;
86275793eaSopenharmony_ci
87275793eaSopenharmony_ci        /* literal/length table */
88275793eaSopenharmony_ci        sym = 0;
89275793eaSopenharmony_ci        while (sym < 144) state->lens[sym++] = 8;
90275793eaSopenharmony_ci        while (sym < 256) state->lens[sym++] = 9;
91275793eaSopenharmony_ci        while (sym < 280) state->lens[sym++] = 7;
92275793eaSopenharmony_ci        while (sym < 288) state->lens[sym++] = 8;
93275793eaSopenharmony_ci        next = fixed;
94275793eaSopenharmony_ci        lenfix = next;
95275793eaSopenharmony_ci        bits = 9;
96275793eaSopenharmony_ci        inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
97275793eaSopenharmony_ci
98275793eaSopenharmony_ci        /* distance table */
99275793eaSopenharmony_ci        sym = 0;
100275793eaSopenharmony_ci        while (sym < 32) state->lens[sym++] = 5;
101275793eaSopenharmony_ci        distfix = next;
102275793eaSopenharmony_ci        bits = 5;
103275793eaSopenharmony_ci        inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
104275793eaSopenharmony_ci
105275793eaSopenharmony_ci        /* do this just once */
106275793eaSopenharmony_ci        virgin = 0;
107275793eaSopenharmony_ci    }
108275793eaSopenharmony_ci#else /* !BUILDFIXED */
109275793eaSopenharmony_ci#   include "inffixed.h"
110275793eaSopenharmony_ci#endif /* BUILDFIXED */
111275793eaSopenharmony_ci    state->lencode = lenfix;
112275793eaSopenharmony_ci    state->lenbits = 9;
113275793eaSopenharmony_ci    state->distcode = distfix;
114275793eaSopenharmony_ci    state->distbits = 5;
115275793eaSopenharmony_ci}
116275793eaSopenharmony_ci
117275793eaSopenharmony_ci/* Macros for inflateBack(): */
118275793eaSopenharmony_ci
119275793eaSopenharmony_ci/* Load returned state from inflate_fast() */
120275793eaSopenharmony_ci#define LOAD() \
121275793eaSopenharmony_ci    do { \
122275793eaSopenharmony_ci        put = strm->next_out; \
123275793eaSopenharmony_ci        left = strm->avail_out; \
124275793eaSopenharmony_ci        next = strm->next_in; \
125275793eaSopenharmony_ci        have = strm->avail_in; \
126275793eaSopenharmony_ci        hold = state->hold; \
127275793eaSopenharmony_ci        bits = state->bits; \
128275793eaSopenharmony_ci    } while (0)
129275793eaSopenharmony_ci
130275793eaSopenharmony_ci/* Set state from registers for inflate_fast() */
131275793eaSopenharmony_ci#define RESTORE() \
132275793eaSopenharmony_ci    do { \
133275793eaSopenharmony_ci        strm->next_out = put; \
134275793eaSopenharmony_ci        strm->avail_out = left; \
135275793eaSopenharmony_ci        strm->next_in = next; \
136275793eaSopenharmony_ci        strm->avail_in = have; \
137275793eaSopenharmony_ci        state->hold = hold; \
138275793eaSopenharmony_ci        state->bits = bits; \
139275793eaSopenharmony_ci    } while (0)
140275793eaSopenharmony_ci
141275793eaSopenharmony_ci/* Clear the input bit accumulator */
142275793eaSopenharmony_ci#define INITBITS() \
143275793eaSopenharmony_ci    do { \
144275793eaSopenharmony_ci        hold = 0; \
145275793eaSopenharmony_ci        bits = 0; \
146275793eaSopenharmony_ci    } while (0)
147275793eaSopenharmony_ci
148275793eaSopenharmony_ci/* Assure that some input is available.  If input is requested, but denied,
149275793eaSopenharmony_ci   then return a Z_BUF_ERROR from inflateBack(). */
150275793eaSopenharmony_ci#define PULL() \
151275793eaSopenharmony_ci    do { \
152275793eaSopenharmony_ci        if (have == 0) { \
153275793eaSopenharmony_ci            have = in(in_desc, &next); \
154275793eaSopenharmony_ci            if (have == 0) { \
155275793eaSopenharmony_ci                next = Z_NULL; \
156275793eaSopenharmony_ci                ret = Z_BUF_ERROR; \
157275793eaSopenharmony_ci                goto inf_leave; \
158275793eaSopenharmony_ci            } \
159275793eaSopenharmony_ci        } \
160275793eaSopenharmony_ci    } while (0)
161275793eaSopenharmony_ci
162275793eaSopenharmony_ci/* Get a byte of input into the bit accumulator, or return from inflateBack()
163275793eaSopenharmony_ci   with an error if there is no input available. */
164275793eaSopenharmony_ci#define PULLBYTE() \
165275793eaSopenharmony_ci    do { \
166275793eaSopenharmony_ci        PULL(); \
167275793eaSopenharmony_ci        have--; \
168275793eaSopenharmony_ci        hold += (unsigned long)(*next++) << bits; \
169275793eaSopenharmony_ci        bits += 8; \
170275793eaSopenharmony_ci    } while (0)
171275793eaSopenharmony_ci
172275793eaSopenharmony_ci/* Assure that there are at least n bits in the bit accumulator.  If there is
173275793eaSopenharmony_ci   not enough available input to do that, then return from inflateBack() with
174275793eaSopenharmony_ci   an error. */
175275793eaSopenharmony_ci#define NEEDBITS(n) \
176275793eaSopenharmony_ci    do { \
177275793eaSopenharmony_ci        while (bits < (unsigned)(n)) \
178275793eaSopenharmony_ci            PULLBYTE(); \
179275793eaSopenharmony_ci    } while (0)
180275793eaSopenharmony_ci
181275793eaSopenharmony_ci/* Return the low n bits of the bit accumulator (n < 16) */
182275793eaSopenharmony_ci#define BITS(n) \
183275793eaSopenharmony_ci    ((unsigned)hold & ((1U << (n)) - 1))
184275793eaSopenharmony_ci
185275793eaSopenharmony_ci/* Remove n bits from the bit accumulator */
186275793eaSopenharmony_ci#define DROPBITS(n) \
187275793eaSopenharmony_ci    do { \
188275793eaSopenharmony_ci        hold >>= (n); \
189275793eaSopenharmony_ci        bits -= (unsigned)(n); \
190275793eaSopenharmony_ci    } while (0)
191275793eaSopenharmony_ci
192275793eaSopenharmony_ci/* Remove zero to seven bits as needed to go to a byte boundary */
193275793eaSopenharmony_ci#define BYTEBITS() \
194275793eaSopenharmony_ci    do { \
195275793eaSopenharmony_ci        hold >>= bits & 7; \
196275793eaSopenharmony_ci        bits -= bits & 7; \
197275793eaSopenharmony_ci    } while (0)
198275793eaSopenharmony_ci
199275793eaSopenharmony_ci/* Assure that some output space is available, by writing out the window
200275793eaSopenharmony_ci   if it's full.  If the write fails, return from inflateBack() with a
201275793eaSopenharmony_ci   Z_BUF_ERROR. */
202275793eaSopenharmony_ci#define ROOM() \
203275793eaSopenharmony_ci    do { \
204275793eaSopenharmony_ci        if (left == 0) { \
205275793eaSopenharmony_ci            put = state->window; \
206275793eaSopenharmony_ci            left = state->wsize; \
207275793eaSopenharmony_ci            state->whave = left; \
208275793eaSopenharmony_ci            if (out(out_desc, put, left)) { \
209275793eaSopenharmony_ci                ret = Z_BUF_ERROR; \
210275793eaSopenharmony_ci                goto inf_leave; \
211275793eaSopenharmony_ci            } \
212275793eaSopenharmony_ci        } \
213275793eaSopenharmony_ci    } while (0)
214275793eaSopenharmony_ci
215275793eaSopenharmony_ci/*
216275793eaSopenharmony_ci   strm provides the memory allocation functions and window buffer on input,
217275793eaSopenharmony_ci   and provides information on the unused input on return.  For Z_DATA_ERROR
218275793eaSopenharmony_ci   returns, strm will also provide an error message.
219275793eaSopenharmony_ci
220275793eaSopenharmony_ci   in() and out() are the call-back input and output functions.  When
221275793eaSopenharmony_ci   inflateBack() needs more input, it calls in().  When inflateBack() has
222275793eaSopenharmony_ci   filled the window with output, or when it completes with data in the
223275793eaSopenharmony_ci   window, it calls out() to write out the data.  The application must not
224275793eaSopenharmony_ci   change the provided input until in() is called again or inflateBack()
225275793eaSopenharmony_ci   returns.  The application must not change the window/output buffer until
226275793eaSopenharmony_ci   inflateBack() returns.
227275793eaSopenharmony_ci
228275793eaSopenharmony_ci   in() and out() are called with a descriptor parameter provided in the
229275793eaSopenharmony_ci   inflateBack() call.  This parameter can be a structure that provides the
230275793eaSopenharmony_ci   information required to do the read or write, as well as accumulated
231275793eaSopenharmony_ci   information on the input and output such as totals and check values.
232275793eaSopenharmony_ci
233275793eaSopenharmony_ci   in() should return zero on failure.  out() should return non-zero on
234275793eaSopenharmony_ci   failure.  If either in() or out() fails, than inflateBack() returns a
235275793eaSopenharmony_ci   Z_BUF_ERROR.  strm->next_in can be checked for Z_NULL to see whether it
236275793eaSopenharmony_ci   was in() or out() that caused in the error.  Otherwise,  inflateBack()
237275793eaSopenharmony_ci   returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format
238275793eaSopenharmony_ci   error, or Z_MEM_ERROR if it could not allocate memory for the state.
239275793eaSopenharmony_ci   inflateBack() can also return Z_STREAM_ERROR if the input parameters
240275793eaSopenharmony_ci   are not correct, i.e. strm is Z_NULL or the state was not initialized.
241275793eaSopenharmony_ci */
242275793eaSopenharmony_ciint ZEXPORT inflateBack(z_streamp strm, in_func in, void FAR *in_desc,
243275793eaSopenharmony_ci                        out_func out, void FAR *out_desc) {
244275793eaSopenharmony_ci    struct inflate_state FAR *state;
245275793eaSopenharmony_ci    z_const unsigned char FAR *next;    /* next input */
246275793eaSopenharmony_ci    unsigned char FAR *put;     /* next output */
247275793eaSopenharmony_ci    unsigned have, left;        /* available input and output */
248275793eaSopenharmony_ci    unsigned long hold;         /* bit buffer */
249275793eaSopenharmony_ci    unsigned bits;              /* bits in bit buffer */
250275793eaSopenharmony_ci    unsigned copy;              /* number of stored or match bytes to copy */
251275793eaSopenharmony_ci    unsigned char FAR *from;    /* where to copy match bytes from */
252275793eaSopenharmony_ci    code here;                  /* current decoding table entry */
253275793eaSopenharmony_ci    code last;                  /* parent table entry */
254275793eaSopenharmony_ci    unsigned len;               /* length to copy for repeats, bits to drop */
255275793eaSopenharmony_ci    int ret;                    /* return code */
256275793eaSopenharmony_ci    static const unsigned short order[19] = /* permutation of code lengths */
257275793eaSopenharmony_ci        {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
258275793eaSopenharmony_ci
259275793eaSopenharmony_ci    /* Check that the strm exists and that the state was initialized */
260275793eaSopenharmony_ci    if (strm == Z_NULL || strm->state == Z_NULL)
261275793eaSopenharmony_ci        return Z_STREAM_ERROR;
262275793eaSopenharmony_ci    state = (struct inflate_state FAR *)strm->state;
263275793eaSopenharmony_ci
264275793eaSopenharmony_ci    /* Reset the state */
265275793eaSopenharmony_ci    strm->msg = Z_NULL;
266275793eaSopenharmony_ci    state->mode = TYPE;
267275793eaSopenharmony_ci    state->last = 0;
268275793eaSopenharmony_ci    state->whave = 0;
269275793eaSopenharmony_ci    next = strm->next_in;
270275793eaSopenharmony_ci    have = next != Z_NULL ? strm->avail_in : 0;
271275793eaSopenharmony_ci    hold = 0;
272275793eaSopenharmony_ci    bits = 0;
273275793eaSopenharmony_ci    put = state->window;
274275793eaSopenharmony_ci    left = state->wsize;
275275793eaSopenharmony_ci
276275793eaSopenharmony_ci    /* Inflate until end of block marked as last */
277275793eaSopenharmony_ci    for (;;)
278275793eaSopenharmony_ci        switch (state->mode) {
279275793eaSopenharmony_ci        case TYPE:
280275793eaSopenharmony_ci            /* determine and dispatch block type */
281275793eaSopenharmony_ci            if (state->last) {
282275793eaSopenharmony_ci                BYTEBITS();
283275793eaSopenharmony_ci                state->mode = DONE;
284275793eaSopenharmony_ci                break;
285275793eaSopenharmony_ci            }
286275793eaSopenharmony_ci            NEEDBITS(3);
287275793eaSopenharmony_ci            state->last = BITS(1);
288275793eaSopenharmony_ci            DROPBITS(1);
289275793eaSopenharmony_ci            switch (BITS(2)) {
290275793eaSopenharmony_ci            case 0:                             /* stored block */
291275793eaSopenharmony_ci                Tracev((stderr, "inflate:     stored block%s\n",
292275793eaSopenharmony_ci                        state->last ? " (last)" : ""));
293275793eaSopenharmony_ci                state->mode = STORED;
294275793eaSopenharmony_ci                break;
295275793eaSopenharmony_ci            case 1:                             /* fixed block */
296275793eaSopenharmony_ci                fixedtables(state);
297275793eaSopenharmony_ci                Tracev((stderr, "inflate:     fixed codes block%s\n",
298275793eaSopenharmony_ci                        state->last ? " (last)" : ""));
299275793eaSopenharmony_ci                state->mode = LEN;              /* decode codes */
300275793eaSopenharmony_ci                break;
301275793eaSopenharmony_ci            case 2:                             /* dynamic block */
302275793eaSopenharmony_ci                Tracev((stderr, "inflate:     dynamic codes block%s\n",
303275793eaSopenharmony_ci                        state->last ? " (last)" : ""));
304275793eaSopenharmony_ci                state->mode = TABLE;
305275793eaSopenharmony_ci                break;
306275793eaSopenharmony_ci            case 3:
307275793eaSopenharmony_ci                strm->msg = (char *)"invalid block type";
308275793eaSopenharmony_ci                state->mode = BAD;
309275793eaSopenharmony_ci            }
310275793eaSopenharmony_ci            DROPBITS(2);
311275793eaSopenharmony_ci            break;
312275793eaSopenharmony_ci
313275793eaSopenharmony_ci        case STORED:
314275793eaSopenharmony_ci            /* get and verify stored block length */
315275793eaSopenharmony_ci            BYTEBITS();                         /* go to byte boundary */
316275793eaSopenharmony_ci            NEEDBITS(32);
317275793eaSopenharmony_ci            if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
318275793eaSopenharmony_ci                strm->msg = (char *)"invalid stored block lengths";
319275793eaSopenharmony_ci                state->mode = BAD;
320275793eaSopenharmony_ci                break;
321275793eaSopenharmony_ci            }
322275793eaSopenharmony_ci            state->length = (unsigned)hold & 0xffff;
323275793eaSopenharmony_ci            Tracev((stderr, "inflate:       stored length %u\n",
324275793eaSopenharmony_ci                    state->length));
325275793eaSopenharmony_ci            INITBITS();
326275793eaSopenharmony_ci
327275793eaSopenharmony_ci            /* copy stored block from input to output */
328275793eaSopenharmony_ci            while (state->length != 0) {
329275793eaSopenharmony_ci                copy = state->length;
330275793eaSopenharmony_ci                PULL();
331275793eaSopenharmony_ci                ROOM();
332275793eaSopenharmony_ci                if (copy > have) copy = have;
333275793eaSopenharmony_ci                if (copy > left) copy = left;
334275793eaSopenharmony_ci                zmemcpy(put, next, copy);
335275793eaSopenharmony_ci                have -= copy;
336275793eaSopenharmony_ci                next += copy;
337275793eaSopenharmony_ci                left -= copy;
338275793eaSopenharmony_ci                put += copy;
339275793eaSopenharmony_ci                state->length -= copy;
340275793eaSopenharmony_ci            }
341275793eaSopenharmony_ci            Tracev((stderr, "inflate:       stored end\n"));
342275793eaSopenharmony_ci            state->mode = TYPE;
343275793eaSopenharmony_ci            break;
344275793eaSopenharmony_ci
345275793eaSopenharmony_ci        case TABLE:
346275793eaSopenharmony_ci            /* get dynamic table entries descriptor */
347275793eaSopenharmony_ci            NEEDBITS(14);
348275793eaSopenharmony_ci            state->nlen = BITS(5) + 257;
349275793eaSopenharmony_ci            DROPBITS(5);
350275793eaSopenharmony_ci            state->ndist = BITS(5) + 1;
351275793eaSopenharmony_ci            DROPBITS(5);
352275793eaSopenharmony_ci            state->ncode = BITS(4) + 4;
353275793eaSopenharmony_ci            DROPBITS(4);
354275793eaSopenharmony_ci#ifndef PKZIP_BUG_WORKAROUND
355275793eaSopenharmony_ci            if (state->nlen > 286 || state->ndist > 30) {
356275793eaSopenharmony_ci                strm->msg = (char *)"too many length or distance symbols";
357275793eaSopenharmony_ci                state->mode = BAD;
358275793eaSopenharmony_ci                break;
359275793eaSopenharmony_ci            }
360275793eaSopenharmony_ci#endif
361275793eaSopenharmony_ci            Tracev((stderr, "inflate:       table sizes ok\n"));
362275793eaSopenharmony_ci
363275793eaSopenharmony_ci            /* get code length code lengths (not a typo) */
364275793eaSopenharmony_ci            state->have = 0;
365275793eaSopenharmony_ci            while (state->have < state->ncode) {
366275793eaSopenharmony_ci                NEEDBITS(3);
367275793eaSopenharmony_ci                state->lens[order[state->have++]] = (unsigned short)BITS(3);
368275793eaSopenharmony_ci                DROPBITS(3);
369275793eaSopenharmony_ci            }
370275793eaSopenharmony_ci            while (state->have < 19)
371275793eaSopenharmony_ci                state->lens[order[state->have++]] = 0;
372275793eaSopenharmony_ci            state->next = state->codes;
373275793eaSopenharmony_ci            state->lencode = (code const FAR *)(state->next);
374275793eaSopenharmony_ci            state->lenbits = 7;
375275793eaSopenharmony_ci            ret = inflate_table(CODES, state->lens, 19, &(state->next),
376275793eaSopenharmony_ci                                &(state->lenbits), state->work);
377275793eaSopenharmony_ci            if (ret) {
378275793eaSopenharmony_ci                strm->msg = (char *)"invalid code lengths set";
379275793eaSopenharmony_ci                state->mode = BAD;
380275793eaSopenharmony_ci                break;
381275793eaSopenharmony_ci            }
382275793eaSopenharmony_ci            Tracev((stderr, "inflate:       code lengths ok\n"));
383275793eaSopenharmony_ci
384275793eaSopenharmony_ci            /* get length and distance code code lengths */
385275793eaSopenharmony_ci            state->have = 0;
386275793eaSopenharmony_ci            while (state->have < state->nlen + state->ndist) {
387275793eaSopenharmony_ci                for (;;) {
388275793eaSopenharmony_ci                    here = state->lencode[BITS(state->lenbits)];
389275793eaSopenharmony_ci                    if ((unsigned)(here.bits) <= bits) break;
390275793eaSopenharmony_ci                    PULLBYTE();
391275793eaSopenharmony_ci                }
392275793eaSopenharmony_ci                if (here.val < 16) {
393275793eaSopenharmony_ci                    DROPBITS(here.bits);
394275793eaSopenharmony_ci                    state->lens[state->have++] = here.val;
395275793eaSopenharmony_ci                }
396275793eaSopenharmony_ci                else {
397275793eaSopenharmony_ci                    if (here.val == 16) {
398275793eaSopenharmony_ci                        NEEDBITS(here.bits + 2);
399275793eaSopenharmony_ci                        DROPBITS(here.bits);
400275793eaSopenharmony_ci                        if (state->have == 0) {
401275793eaSopenharmony_ci                            strm->msg = (char *)"invalid bit length repeat";
402275793eaSopenharmony_ci                            state->mode = BAD;
403275793eaSopenharmony_ci                            break;
404275793eaSopenharmony_ci                        }
405275793eaSopenharmony_ci                        len = (unsigned)(state->lens[state->have - 1]);
406275793eaSopenharmony_ci                        copy = 3 + BITS(2);
407275793eaSopenharmony_ci                        DROPBITS(2);
408275793eaSopenharmony_ci                    }
409275793eaSopenharmony_ci                    else if (here.val == 17) {
410275793eaSopenharmony_ci                        NEEDBITS(here.bits + 3);
411275793eaSopenharmony_ci                        DROPBITS(here.bits);
412275793eaSopenharmony_ci                        len = 0;
413275793eaSopenharmony_ci                        copy = 3 + BITS(3);
414275793eaSopenharmony_ci                        DROPBITS(3);
415275793eaSopenharmony_ci                    }
416275793eaSopenharmony_ci                    else {
417275793eaSopenharmony_ci                        NEEDBITS(here.bits + 7);
418275793eaSopenharmony_ci                        DROPBITS(here.bits);
419275793eaSopenharmony_ci                        len = 0;
420275793eaSopenharmony_ci                        copy = 11 + BITS(7);
421275793eaSopenharmony_ci                        DROPBITS(7);
422275793eaSopenharmony_ci                    }
423275793eaSopenharmony_ci                    if (state->have + copy > state->nlen + state->ndist) {
424275793eaSopenharmony_ci                        strm->msg = (char *)"invalid bit length repeat";
425275793eaSopenharmony_ci                        state->mode = BAD;
426275793eaSopenharmony_ci                        break;
427275793eaSopenharmony_ci                    }
428275793eaSopenharmony_ci                    while (copy--)
429275793eaSopenharmony_ci                        state->lens[state->have++] = (unsigned short)len;
430275793eaSopenharmony_ci                }
431275793eaSopenharmony_ci            }
432275793eaSopenharmony_ci
433275793eaSopenharmony_ci            /* handle error breaks in while */
434275793eaSopenharmony_ci            if (state->mode == BAD) break;
435275793eaSopenharmony_ci
436275793eaSopenharmony_ci            /* check for end-of-block code (better have one) */
437275793eaSopenharmony_ci            if (state->lens[256] == 0) {
438275793eaSopenharmony_ci                strm->msg = (char *)"invalid code -- missing end-of-block";
439275793eaSopenharmony_ci                state->mode = BAD;
440275793eaSopenharmony_ci                break;
441275793eaSopenharmony_ci            }
442275793eaSopenharmony_ci
443275793eaSopenharmony_ci            /* build code tables -- note: do not change the lenbits or distbits
444275793eaSopenharmony_ci               values here (9 and 6) without reading the comments in inftrees.h
445275793eaSopenharmony_ci               concerning the ENOUGH constants, which depend on those values */
446275793eaSopenharmony_ci            state->next = state->codes;
447275793eaSopenharmony_ci            state->lencode = (code const FAR *)(state->next);
448275793eaSopenharmony_ci            state->lenbits = 9;
449275793eaSopenharmony_ci            ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
450275793eaSopenharmony_ci                                &(state->lenbits), state->work);
451275793eaSopenharmony_ci            if (ret) {
452275793eaSopenharmony_ci                strm->msg = (char *)"invalid literal/lengths set";
453275793eaSopenharmony_ci                state->mode = BAD;
454275793eaSopenharmony_ci                break;
455275793eaSopenharmony_ci            }
456275793eaSopenharmony_ci            state->distcode = (code const FAR *)(state->next);
457275793eaSopenharmony_ci            state->distbits = 6;
458275793eaSopenharmony_ci            ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
459275793eaSopenharmony_ci                            &(state->next), &(state->distbits), state->work);
460275793eaSopenharmony_ci            if (ret) {
461275793eaSopenharmony_ci                strm->msg = (char *)"invalid distances set";
462275793eaSopenharmony_ci                state->mode = BAD;
463275793eaSopenharmony_ci                break;
464275793eaSopenharmony_ci            }
465275793eaSopenharmony_ci            Tracev((stderr, "inflate:       codes ok\n"));
466275793eaSopenharmony_ci            state->mode = LEN;
467275793eaSopenharmony_ci                /* fallthrough */
468275793eaSopenharmony_ci
469275793eaSopenharmony_ci        case LEN:
470275793eaSopenharmony_ci            /* use inflate_fast() if we have enough input and output */
471275793eaSopenharmony_ci            if (have >= 6 && left >= 258) {
472275793eaSopenharmony_ci                RESTORE();
473275793eaSopenharmony_ci                if (state->whave < state->wsize)
474275793eaSopenharmony_ci                    state->whave = state->wsize - left;
475275793eaSopenharmony_ci                inflate_fast(strm, state->wsize);
476275793eaSopenharmony_ci                LOAD();
477275793eaSopenharmony_ci                break;
478275793eaSopenharmony_ci            }
479275793eaSopenharmony_ci
480275793eaSopenharmony_ci            /* get a literal, length, or end-of-block code */
481275793eaSopenharmony_ci            for (;;) {
482275793eaSopenharmony_ci                here = state->lencode[BITS(state->lenbits)];
483275793eaSopenharmony_ci                if ((unsigned)(here.bits) <= bits) break;
484275793eaSopenharmony_ci                PULLBYTE();
485275793eaSopenharmony_ci            }
486275793eaSopenharmony_ci            if (here.op && (here.op & 0xf0) == 0) {
487275793eaSopenharmony_ci                last = here;
488275793eaSopenharmony_ci                for (;;) {
489275793eaSopenharmony_ci                    here = state->lencode[last.val +
490275793eaSopenharmony_ci                            (BITS(last.bits + last.op) >> last.bits)];
491275793eaSopenharmony_ci                    if ((unsigned)(last.bits + here.bits) <= bits) break;
492275793eaSopenharmony_ci                    PULLBYTE();
493275793eaSopenharmony_ci                }
494275793eaSopenharmony_ci                DROPBITS(last.bits);
495275793eaSopenharmony_ci            }
496275793eaSopenharmony_ci            DROPBITS(here.bits);
497275793eaSopenharmony_ci            state->length = (unsigned)here.val;
498275793eaSopenharmony_ci
499275793eaSopenharmony_ci            /* process literal */
500275793eaSopenharmony_ci            if (here.op == 0) {
501275793eaSopenharmony_ci                Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
502275793eaSopenharmony_ci                        "inflate:         literal '%c'\n" :
503275793eaSopenharmony_ci                        "inflate:         literal 0x%02x\n", here.val));
504275793eaSopenharmony_ci                ROOM();
505275793eaSopenharmony_ci                *put++ = (unsigned char)(state->length);
506275793eaSopenharmony_ci                left--;
507275793eaSopenharmony_ci                state->mode = LEN;
508275793eaSopenharmony_ci                break;
509275793eaSopenharmony_ci            }
510275793eaSopenharmony_ci
511275793eaSopenharmony_ci            /* process end of block */
512275793eaSopenharmony_ci            if (here.op & 32) {
513275793eaSopenharmony_ci                Tracevv((stderr, "inflate:         end of block\n"));
514275793eaSopenharmony_ci                state->mode = TYPE;
515275793eaSopenharmony_ci                break;
516275793eaSopenharmony_ci            }
517275793eaSopenharmony_ci
518275793eaSopenharmony_ci            /* invalid code */
519275793eaSopenharmony_ci            if (here.op & 64) {
520275793eaSopenharmony_ci                strm->msg = (char *)"invalid literal/length code";
521275793eaSopenharmony_ci                state->mode = BAD;
522275793eaSopenharmony_ci                break;
523275793eaSopenharmony_ci            }
524275793eaSopenharmony_ci
525275793eaSopenharmony_ci            /* length code -- get extra bits, if any */
526275793eaSopenharmony_ci            state->extra = (unsigned)(here.op) & 15;
527275793eaSopenharmony_ci            if (state->extra != 0) {
528275793eaSopenharmony_ci                NEEDBITS(state->extra);
529275793eaSopenharmony_ci                state->length += BITS(state->extra);
530275793eaSopenharmony_ci                DROPBITS(state->extra);
531275793eaSopenharmony_ci            }
532275793eaSopenharmony_ci            Tracevv((stderr, "inflate:         length %u\n", state->length));
533275793eaSopenharmony_ci
534275793eaSopenharmony_ci            /* get distance code */
535275793eaSopenharmony_ci            for (;;) {
536275793eaSopenharmony_ci                here = state->distcode[BITS(state->distbits)];
537275793eaSopenharmony_ci                if ((unsigned)(here.bits) <= bits) break;
538275793eaSopenharmony_ci                PULLBYTE();
539275793eaSopenharmony_ci            }
540275793eaSopenharmony_ci            if ((here.op & 0xf0) == 0) {
541275793eaSopenharmony_ci                last = here;
542275793eaSopenharmony_ci                for (;;) {
543275793eaSopenharmony_ci                    here = state->distcode[last.val +
544275793eaSopenharmony_ci                            (BITS(last.bits + last.op) >> last.bits)];
545275793eaSopenharmony_ci                    if ((unsigned)(last.bits + here.bits) <= bits) break;
546275793eaSopenharmony_ci                    PULLBYTE();
547275793eaSopenharmony_ci                }
548275793eaSopenharmony_ci                DROPBITS(last.bits);
549275793eaSopenharmony_ci            }
550275793eaSopenharmony_ci            DROPBITS(here.bits);
551275793eaSopenharmony_ci            if (here.op & 64) {
552275793eaSopenharmony_ci                strm->msg = (char *)"invalid distance code";
553275793eaSopenharmony_ci                state->mode = BAD;
554275793eaSopenharmony_ci                break;
555275793eaSopenharmony_ci            }
556275793eaSopenharmony_ci            state->offset = (unsigned)here.val;
557275793eaSopenharmony_ci
558275793eaSopenharmony_ci            /* get distance extra bits, if any */
559275793eaSopenharmony_ci            state->extra = (unsigned)(here.op) & 15;
560275793eaSopenharmony_ci            if (state->extra != 0) {
561275793eaSopenharmony_ci                NEEDBITS(state->extra);
562275793eaSopenharmony_ci                state->offset += BITS(state->extra);
563275793eaSopenharmony_ci                DROPBITS(state->extra);
564275793eaSopenharmony_ci            }
565275793eaSopenharmony_ci            if (state->offset > state->wsize - (state->whave < state->wsize ?
566275793eaSopenharmony_ci                                                left : 0)) {
567275793eaSopenharmony_ci                strm->msg = (char *)"invalid distance too far back";
568275793eaSopenharmony_ci                state->mode = BAD;
569275793eaSopenharmony_ci                break;
570275793eaSopenharmony_ci            }
571275793eaSopenharmony_ci            Tracevv((stderr, "inflate:         distance %u\n", state->offset));
572275793eaSopenharmony_ci
573275793eaSopenharmony_ci            /* copy match from window to output */
574275793eaSopenharmony_ci            do {
575275793eaSopenharmony_ci                ROOM();
576275793eaSopenharmony_ci                copy = state->wsize - state->offset;
577275793eaSopenharmony_ci                if (copy < left) {
578275793eaSopenharmony_ci                    from = put + copy;
579275793eaSopenharmony_ci                    copy = left - copy;
580275793eaSopenharmony_ci                }
581275793eaSopenharmony_ci                else {
582275793eaSopenharmony_ci                    from = put - state->offset;
583275793eaSopenharmony_ci                    copy = left;
584275793eaSopenharmony_ci                }
585275793eaSopenharmony_ci                if (copy > state->length) copy = state->length;
586275793eaSopenharmony_ci                state->length -= copy;
587275793eaSopenharmony_ci                left -= copy;
588275793eaSopenharmony_ci                do {
589275793eaSopenharmony_ci                    *put++ = *from++;
590275793eaSopenharmony_ci                } while (--copy);
591275793eaSopenharmony_ci            } while (state->length != 0);
592275793eaSopenharmony_ci            break;
593275793eaSopenharmony_ci
594275793eaSopenharmony_ci        case DONE:
595275793eaSopenharmony_ci            /* inflate stream terminated properly */
596275793eaSopenharmony_ci            ret = Z_STREAM_END;
597275793eaSopenharmony_ci            goto inf_leave;
598275793eaSopenharmony_ci
599275793eaSopenharmony_ci        case BAD:
600275793eaSopenharmony_ci            ret = Z_DATA_ERROR;
601275793eaSopenharmony_ci            goto inf_leave;
602275793eaSopenharmony_ci
603275793eaSopenharmony_ci        default:
604275793eaSopenharmony_ci            /* can't happen, but makes compilers happy */
605275793eaSopenharmony_ci            ret = Z_STREAM_ERROR;
606275793eaSopenharmony_ci            goto inf_leave;
607275793eaSopenharmony_ci        }
608275793eaSopenharmony_ci
609275793eaSopenharmony_ci    /* Write leftover output and return unused input */
610275793eaSopenharmony_ci  inf_leave:
611275793eaSopenharmony_ci    if (left < state->wsize) {
612275793eaSopenharmony_ci        if (out(out_desc, state->window, state->wsize - left) &&
613275793eaSopenharmony_ci            ret == Z_STREAM_END)
614275793eaSopenharmony_ci            ret = Z_BUF_ERROR;
615275793eaSopenharmony_ci    }
616275793eaSopenharmony_ci    strm->next_in = next;
617275793eaSopenharmony_ci    strm->avail_in = have;
618275793eaSopenharmony_ci    return ret;
619275793eaSopenharmony_ci}
620275793eaSopenharmony_ci
621275793eaSopenharmony_ciint ZEXPORT inflateBackEnd(z_streamp strm) {
622275793eaSopenharmony_ci    if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
623275793eaSopenharmony_ci        return Z_STREAM_ERROR;
624275793eaSopenharmony_ci    ZFREE(strm, strm->state);
625275793eaSopenharmony_ci    strm->state = Z_NULL;
626275793eaSopenharmony_ci    Tracev((stderr, "inflate: end\n"));
627275793eaSopenharmony_ci    return Z_OK;
628275793eaSopenharmony_ci}
629