162306a36Sopenharmony_ci/* inflate.c -- zlib decompression
262306a36Sopenharmony_ci * Copyright (C) 1995-2005 Mark Adler
362306a36Sopenharmony_ci * For conditions of distribution and use, see copyright notice in zlib.h
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Based on zlib 1.2.3 but modified for the Linux Kernel by
662306a36Sopenharmony_ci * Richard Purdie <richard@openedhand.com>
762306a36Sopenharmony_ci *
862306a36Sopenharmony_ci * Changes mainly for static instead of dynamic memory allocation
962306a36Sopenharmony_ci *
1062306a36Sopenharmony_ci */
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci#include <linux/zutil.h>
1362306a36Sopenharmony_ci#include "inftrees.h"
1462306a36Sopenharmony_ci#include "inflate.h"
1562306a36Sopenharmony_ci#include "inffast.h"
1662306a36Sopenharmony_ci#include "infutil.h"
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ci/* architecture-specific bits */
1962306a36Sopenharmony_ci#ifdef CONFIG_ZLIB_DFLTCC
2062306a36Sopenharmony_ci#  include "../zlib_dfltcc/dfltcc_inflate.h"
2162306a36Sopenharmony_ci#else
2262306a36Sopenharmony_ci#define INFLATE_RESET_HOOK(strm) do {} while (0)
2362306a36Sopenharmony_ci#define INFLATE_TYPEDO_HOOK(strm, flush) do {} while (0)
2462306a36Sopenharmony_ci#define INFLATE_NEED_UPDATEWINDOW(strm) 1
2562306a36Sopenharmony_ci#define INFLATE_NEED_CHECKSUM(strm) 1
2662306a36Sopenharmony_ci#endif
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ciint zlib_inflate_workspacesize(void)
2962306a36Sopenharmony_ci{
3062306a36Sopenharmony_ci    return sizeof(struct inflate_workspace);
3162306a36Sopenharmony_ci}
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ciint zlib_inflateReset(z_streamp strm)
3462306a36Sopenharmony_ci{
3562306a36Sopenharmony_ci    struct inflate_state *state;
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci    if (strm == NULL || strm->state == NULL) return Z_STREAM_ERROR;
3862306a36Sopenharmony_ci    state = (struct inflate_state *)strm->state;
3962306a36Sopenharmony_ci    strm->total_in = strm->total_out = state->total = 0;
4062306a36Sopenharmony_ci    strm->msg = NULL;
4162306a36Sopenharmony_ci    strm->adler = 1;        /* to support ill-conceived Java test suite */
4262306a36Sopenharmony_ci    state->mode = HEAD;
4362306a36Sopenharmony_ci    state->last = 0;
4462306a36Sopenharmony_ci    state->havedict = 0;
4562306a36Sopenharmony_ci    state->dmax = 32768U;
4662306a36Sopenharmony_ci    state->hold = 0;
4762306a36Sopenharmony_ci    state->bits = 0;
4862306a36Sopenharmony_ci    state->lencode = state->distcode = state->next = state->codes;
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci    /* Initialise Window */
5162306a36Sopenharmony_ci    state->wsize = 1U << state->wbits;
5262306a36Sopenharmony_ci    state->write = 0;
5362306a36Sopenharmony_ci    state->whave = 0;
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci    INFLATE_RESET_HOOK(strm);
5662306a36Sopenharmony_ci    return Z_OK;
5762306a36Sopenharmony_ci}
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ciint zlib_inflateInit2(z_streamp strm, int windowBits)
6062306a36Sopenharmony_ci{
6162306a36Sopenharmony_ci    struct inflate_state *state;
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci    if (strm == NULL) return Z_STREAM_ERROR;
6462306a36Sopenharmony_ci    strm->msg = NULL;                 /* in case we return an error */
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci    state = &WS(strm)->inflate_state;
6762306a36Sopenharmony_ci    strm->state = (struct internal_state *)state;
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ci    if (windowBits < 0) {
7062306a36Sopenharmony_ci        state->wrap = 0;
7162306a36Sopenharmony_ci        windowBits = -windowBits;
7262306a36Sopenharmony_ci    }
7362306a36Sopenharmony_ci    else {
7462306a36Sopenharmony_ci        state->wrap = (windowBits >> 4) + 1;
7562306a36Sopenharmony_ci    }
7662306a36Sopenharmony_ci    if (windowBits < 8 || windowBits > 15) {
7762306a36Sopenharmony_ci        return Z_STREAM_ERROR;
7862306a36Sopenharmony_ci    }
7962306a36Sopenharmony_ci    state->wbits = (unsigned)windowBits;
8062306a36Sopenharmony_ci#ifdef CONFIG_ZLIB_DFLTCC
8162306a36Sopenharmony_ci    /*
8262306a36Sopenharmony_ci     * DFLTCC requires the window to be page aligned.
8362306a36Sopenharmony_ci     * Thus, we overallocate and take the aligned portion of the buffer.
8462306a36Sopenharmony_ci     */
8562306a36Sopenharmony_ci    state->window = PTR_ALIGN(&WS(strm)->working_window[0], PAGE_SIZE);
8662306a36Sopenharmony_ci#else
8762306a36Sopenharmony_ci    state->window = &WS(strm)->working_window[0];
8862306a36Sopenharmony_ci#endif
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci    return zlib_inflateReset(strm);
9162306a36Sopenharmony_ci}
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci/*
9462306a36Sopenharmony_ci   Return state with length and distance decoding tables and index sizes set to
9562306a36Sopenharmony_ci   fixed code decoding.  This returns fixed tables from inffixed.h.
9662306a36Sopenharmony_ci */
9762306a36Sopenharmony_cistatic void zlib_fixedtables(struct inflate_state *state)
9862306a36Sopenharmony_ci{
9962306a36Sopenharmony_ci#   include "inffixed.h"
10062306a36Sopenharmony_ci    state->lencode = lenfix;
10162306a36Sopenharmony_ci    state->lenbits = 9;
10262306a36Sopenharmony_ci    state->distcode = distfix;
10362306a36Sopenharmony_ci    state->distbits = 5;
10462306a36Sopenharmony_ci}
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ci/*
10862306a36Sopenharmony_ci   Update the window with the last wsize (normally 32K) bytes written before
10962306a36Sopenharmony_ci   returning. This is only called when a window is already in use, or when
11062306a36Sopenharmony_ci   output has been written during this inflate call, but the end of the deflate
11162306a36Sopenharmony_ci   stream has not been reached yet. It is also called to window dictionary data
11262306a36Sopenharmony_ci   when a dictionary is loaded.
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci   Providing output buffers larger than 32K to inflate() should provide a speed
11562306a36Sopenharmony_ci   advantage, since only the last 32K of output is copied to the sliding window
11662306a36Sopenharmony_ci   upon return from inflate(), and since all distances after the first 32K of
11762306a36Sopenharmony_ci   output will fall in the output data, making match copies simpler and faster.
11862306a36Sopenharmony_ci   The advantage may be dependent on the size of the processor's data caches.
11962306a36Sopenharmony_ci */
12062306a36Sopenharmony_cistatic void zlib_updatewindow(z_streamp strm, unsigned out)
12162306a36Sopenharmony_ci{
12262306a36Sopenharmony_ci    struct inflate_state *state;
12362306a36Sopenharmony_ci    unsigned copy, dist;
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci    state = (struct inflate_state *)strm->state;
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ci    /* copy state->wsize or less output bytes into the circular window */
12862306a36Sopenharmony_ci    copy = out - strm->avail_out;
12962306a36Sopenharmony_ci    if (copy >= state->wsize) {
13062306a36Sopenharmony_ci        memcpy(state->window, strm->next_out - state->wsize, state->wsize);
13162306a36Sopenharmony_ci        state->write = 0;
13262306a36Sopenharmony_ci        state->whave = state->wsize;
13362306a36Sopenharmony_ci    }
13462306a36Sopenharmony_ci    else {
13562306a36Sopenharmony_ci        dist = state->wsize - state->write;
13662306a36Sopenharmony_ci        if (dist > copy) dist = copy;
13762306a36Sopenharmony_ci        memcpy(state->window + state->write, strm->next_out - copy, dist);
13862306a36Sopenharmony_ci        copy -= dist;
13962306a36Sopenharmony_ci        if (copy) {
14062306a36Sopenharmony_ci            memcpy(state->window, strm->next_out - copy, copy);
14162306a36Sopenharmony_ci            state->write = copy;
14262306a36Sopenharmony_ci            state->whave = state->wsize;
14362306a36Sopenharmony_ci        }
14462306a36Sopenharmony_ci        else {
14562306a36Sopenharmony_ci            state->write += dist;
14662306a36Sopenharmony_ci            if (state->write == state->wsize) state->write = 0;
14762306a36Sopenharmony_ci            if (state->whave < state->wsize) state->whave += dist;
14862306a36Sopenharmony_ci        }
14962306a36Sopenharmony_ci    }
15062306a36Sopenharmony_ci}
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ci/*
15462306a36Sopenharmony_ci * At the end of a Deflate-compressed PPP packet, we expect to have seen
15562306a36Sopenharmony_ci * a `stored' block type value but not the (zero) length bytes.
15662306a36Sopenharmony_ci */
15762306a36Sopenharmony_ci/*
15862306a36Sopenharmony_ci   Returns true if inflate is currently at the end of a block generated by
15962306a36Sopenharmony_ci   Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
16062306a36Sopenharmony_ci   implementation to provide an additional safety check. PPP uses
16162306a36Sopenharmony_ci   Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored
16262306a36Sopenharmony_ci   block. When decompressing, PPP checks that at the end of input packet,
16362306a36Sopenharmony_ci   inflate is waiting for these length bytes.
16462306a36Sopenharmony_ci */
16562306a36Sopenharmony_cistatic int zlib_inflateSyncPacket(z_streamp strm)
16662306a36Sopenharmony_ci{
16762306a36Sopenharmony_ci    struct inflate_state *state;
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ci    if (strm == NULL || strm->state == NULL) return Z_STREAM_ERROR;
17062306a36Sopenharmony_ci    state = (struct inflate_state *)strm->state;
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ci    if (state->mode == STORED && state->bits == 0) {
17362306a36Sopenharmony_ci	state->mode = TYPE;
17462306a36Sopenharmony_ci        return Z_OK;
17562306a36Sopenharmony_ci    }
17662306a36Sopenharmony_ci    return Z_DATA_ERROR;
17762306a36Sopenharmony_ci}
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ci/* Macros for inflate(): */
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_ci/* check function to use adler32() for zlib or crc32() for gzip */
18262306a36Sopenharmony_ci#define UPDATE(check, buf, len) zlib_adler32(check, buf, len)
18362306a36Sopenharmony_ci
18462306a36Sopenharmony_ci/* Load registers with state in inflate() for speed */
18562306a36Sopenharmony_ci#define LOAD() \
18662306a36Sopenharmony_ci    do { \
18762306a36Sopenharmony_ci        put = strm->next_out; \
18862306a36Sopenharmony_ci        left = strm->avail_out; \
18962306a36Sopenharmony_ci        next = strm->next_in; \
19062306a36Sopenharmony_ci        have = strm->avail_in; \
19162306a36Sopenharmony_ci        hold = state->hold; \
19262306a36Sopenharmony_ci        bits = state->bits; \
19362306a36Sopenharmony_ci    } while (0)
19462306a36Sopenharmony_ci
19562306a36Sopenharmony_ci/* Restore state from registers in inflate() */
19662306a36Sopenharmony_ci#define RESTORE() \
19762306a36Sopenharmony_ci    do { \
19862306a36Sopenharmony_ci        strm->next_out = put; \
19962306a36Sopenharmony_ci        strm->avail_out = left; \
20062306a36Sopenharmony_ci        strm->next_in = next; \
20162306a36Sopenharmony_ci        strm->avail_in = have; \
20262306a36Sopenharmony_ci        state->hold = hold; \
20362306a36Sopenharmony_ci        state->bits = bits; \
20462306a36Sopenharmony_ci    } while (0)
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_ci/* Clear the input bit accumulator */
20762306a36Sopenharmony_ci#define INITBITS() \
20862306a36Sopenharmony_ci    do { \
20962306a36Sopenharmony_ci        hold = 0; \
21062306a36Sopenharmony_ci        bits = 0; \
21162306a36Sopenharmony_ci    } while (0)
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ci/* Get a byte of input into the bit accumulator, or return from inflate()
21462306a36Sopenharmony_ci   if there is no input available. */
21562306a36Sopenharmony_ci#define PULLBYTE() \
21662306a36Sopenharmony_ci    do { \
21762306a36Sopenharmony_ci        if (have == 0) goto inf_leave; \
21862306a36Sopenharmony_ci        have--; \
21962306a36Sopenharmony_ci        hold += (unsigned long)(*next++) << bits; \
22062306a36Sopenharmony_ci        bits += 8; \
22162306a36Sopenharmony_ci    } while (0)
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_ci/* Assure that there are at least n bits in the bit accumulator.  If there is
22462306a36Sopenharmony_ci   not enough available input to do that, then return from inflate(). */
22562306a36Sopenharmony_ci#define NEEDBITS(n) \
22662306a36Sopenharmony_ci    do { \
22762306a36Sopenharmony_ci        while (bits < (unsigned)(n)) \
22862306a36Sopenharmony_ci            PULLBYTE(); \
22962306a36Sopenharmony_ci    } while (0)
23062306a36Sopenharmony_ci
23162306a36Sopenharmony_ci/* Return the low n bits of the bit accumulator (n < 16) */
23262306a36Sopenharmony_ci#define BITS(n) \
23362306a36Sopenharmony_ci    ((unsigned)hold & ((1U << (n)) - 1))
23462306a36Sopenharmony_ci
23562306a36Sopenharmony_ci/* Remove n bits from the bit accumulator */
23662306a36Sopenharmony_ci#define DROPBITS(n) \
23762306a36Sopenharmony_ci    do { \
23862306a36Sopenharmony_ci        hold >>= (n); \
23962306a36Sopenharmony_ci        bits -= (unsigned)(n); \
24062306a36Sopenharmony_ci    } while (0)
24162306a36Sopenharmony_ci
24262306a36Sopenharmony_ci/* Remove zero to seven bits as needed to go to a byte boundary */
24362306a36Sopenharmony_ci#define BYTEBITS() \
24462306a36Sopenharmony_ci    do { \
24562306a36Sopenharmony_ci        hold >>= bits & 7; \
24662306a36Sopenharmony_ci        bits -= bits & 7; \
24762306a36Sopenharmony_ci    } while (0)
24862306a36Sopenharmony_ci
24962306a36Sopenharmony_ci/*
25062306a36Sopenharmony_ci   inflate() uses a state machine to process as much input data and generate as
25162306a36Sopenharmony_ci   much output data as possible before returning.  The state machine is
25262306a36Sopenharmony_ci   structured roughly as follows:
25362306a36Sopenharmony_ci
25462306a36Sopenharmony_ci    for (;;) switch (state) {
25562306a36Sopenharmony_ci    ...
25662306a36Sopenharmony_ci    case STATEn:
25762306a36Sopenharmony_ci        if (not enough input data or output space to make progress)
25862306a36Sopenharmony_ci            return;
25962306a36Sopenharmony_ci        ... make progress ...
26062306a36Sopenharmony_ci        state = STATEm;
26162306a36Sopenharmony_ci        break;
26262306a36Sopenharmony_ci    ...
26362306a36Sopenharmony_ci    }
26462306a36Sopenharmony_ci
26562306a36Sopenharmony_ci   so when inflate() is called again, the same case is attempted again, and
26662306a36Sopenharmony_ci   if the appropriate resources are provided, the machine proceeds to the
26762306a36Sopenharmony_ci   next state.  The NEEDBITS() macro is usually the way the state evaluates
26862306a36Sopenharmony_ci   whether it can proceed or should return.  NEEDBITS() does the return if
26962306a36Sopenharmony_ci   the requested bits are not available.  The typical use of the BITS macros
27062306a36Sopenharmony_ci   is:
27162306a36Sopenharmony_ci
27262306a36Sopenharmony_ci        NEEDBITS(n);
27362306a36Sopenharmony_ci        ... do something with BITS(n) ...
27462306a36Sopenharmony_ci        DROPBITS(n);
27562306a36Sopenharmony_ci
27662306a36Sopenharmony_ci   where NEEDBITS(n) either returns from inflate() if there isn't enough
27762306a36Sopenharmony_ci   input left to load n bits into the accumulator, or it continues.  BITS(n)
27862306a36Sopenharmony_ci   gives the low n bits in the accumulator.  When done, DROPBITS(n) drops
27962306a36Sopenharmony_ci   the low n bits off the accumulator.  INITBITS() clears the accumulator
28062306a36Sopenharmony_ci   and sets the number of available bits to zero.  BYTEBITS() discards just
28162306a36Sopenharmony_ci   enough bits to put the accumulator on a byte boundary.  After BYTEBITS()
28262306a36Sopenharmony_ci   and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
28362306a36Sopenharmony_ci
28462306a36Sopenharmony_ci   NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
28562306a36Sopenharmony_ci   if there is no input available.  The decoding of variable length codes uses
28662306a36Sopenharmony_ci   PULLBYTE() directly in order to pull just enough bytes to decode the next
28762306a36Sopenharmony_ci   code, and no more.
28862306a36Sopenharmony_ci
28962306a36Sopenharmony_ci   Some states loop until they get enough input, making sure that enough
29062306a36Sopenharmony_ci   state information is maintained to continue the loop where it left off
29162306a36Sopenharmony_ci   if NEEDBITS() returns in the loop.  For example, want, need, and keep
29262306a36Sopenharmony_ci   would all have to actually be part of the saved state in case NEEDBITS()
29362306a36Sopenharmony_ci   returns:
29462306a36Sopenharmony_ci
29562306a36Sopenharmony_ci    case STATEw:
29662306a36Sopenharmony_ci        while (want < need) {
29762306a36Sopenharmony_ci            NEEDBITS(n);
29862306a36Sopenharmony_ci            keep[want++] = BITS(n);
29962306a36Sopenharmony_ci            DROPBITS(n);
30062306a36Sopenharmony_ci        }
30162306a36Sopenharmony_ci        state = STATEx;
30262306a36Sopenharmony_ci    case STATEx:
30362306a36Sopenharmony_ci
30462306a36Sopenharmony_ci   As shown above, if the next state is also the next case, then the break
30562306a36Sopenharmony_ci   is omitted.
30662306a36Sopenharmony_ci
30762306a36Sopenharmony_ci   A state may also return if there is not enough output space available to
30862306a36Sopenharmony_ci   complete that state.  Those states are copying stored data, writing a
30962306a36Sopenharmony_ci   literal byte, and copying a matching string.
31062306a36Sopenharmony_ci
31162306a36Sopenharmony_ci   When returning, a "goto inf_leave" is used to update the total counters,
31262306a36Sopenharmony_ci   update the check value, and determine whether any progress has been made
31362306a36Sopenharmony_ci   during that inflate() call in order to return the proper return code.
31462306a36Sopenharmony_ci   Progress is defined as a change in either strm->avail_in or strm->avail_out.
31562306a36Sopenharmony_ci   When there is a window, goto inf_leave will update the window with the last
31662306a36Sopenharmony_ci   output written.  If a goto inf_leave occurs in the middle of decompression
31762306a36Sopenharmony_ci   and there is no window currently, goto inf_leave will create one and copy
31862306a36Sopenharmony_ci   output to the window for the next call of inflate().
31962306a36Sopenharmony_ci
32062306a36Sopenharmony_ci   In this implementation, the flush parameter of inflate() only affects the
32162306a36Sopenharmony_ci   return code (per zlib.h).  inflate() always writes as much as possible to
32262306a36Sopenharmony_ci   strm->next_out, given the space available and the provided input--the effect
32362306a36Sopenharmony_ci   documented in zlib.h of Z_SYNC_FLUSH.  Furthermore, inflate() always defers
32462306a36Sopenharmony_ci   the allocation of and copying into a sliding window until necessary, which
32562306a36Sopenharmony_ci   provides the effect documented in zlib.h for Z_FINISH when the entire input
32662306a36Sopenharmony_ci   stream available.  So the only thing the flush parameter actually does is:
32762306a36Sopenharmony_ci   when flush is set to Z_FINISH, inflate() cannot return Z_OK.  Instead it
32862306a36Sopenharmony_ci   will return Z_BUF_ERROR if it has not reached the end of the stream.
32962306a36Sopenharmony_ci */
33062306a36Sopenharmony_ci
33162306a36Sopenharmony_ciint zlib_inflate(z_streamp strm, int flush)
33262306a36Sopenharmony_ci{
33362306a36Sopenharmony_ci    struct inflate_state *state;
33462306a36Sopenharmony_ci    const unsigned char *next;  /* next input */
33562306a36Sopenharmony_ci    unsigned char *put;         /* next output */
33662306a36Sopenharmony_ci    unsigned have, left;        /* available input and output */
33762306a36Sopenharmony_ci    unsigned long hold;         /* bit buffer */
33862306a36Sopenharmony_ci    unsigned bits;              /* bits in bit buffer */
33962306a36Sopenharmony_ci    unsigned in, out;           /* save starting available input and output */
34062306a36Sopenharmony_ci    unsigned copy;              /* number of stored or match bytes to copy */
34162306a36Sopenharmony_ci    unsigned char *from;        /* where to copy match bytes from */
34262306a36Sopenharmony_ci    code this;                  /* current decoding table entry */
34362306a36Sopenharmony_ci    code last;                  /* parent table entry */
34462306a36Sopenharmony_ci    unsigned len;               /* length to copy for repeats, bits to drop */
34562306a36Sopenharmony_ci    int ret;                    /* return code */
34662306a36Sopenharmony_ci    static const unsigned short order[19] = /* permutation of code lengths */
34762306a36Sopenharmony_ci        {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
34862306a36Sopenharmony_ci
34962306a36Sopenharmony_ci    /* Do not check for strm->next_out == NULL here as ppc zImage
35062306a36Sopenharmony_ci       inflates to strm->next_out = 0 */
35162306a36Sopenharmony_ci
35262306a36Sopenharmony_ci    if (strm == NULL || strm->state == NULL ||
35362306a36Sopenharmony_ci        (strm->next_in == NULL && strm->avail_in != 0))
35462306a36Sopenharmony_ci        return Z_STREAM_ERROR;
35562306a36Sopenharmony_ci
35662306a36Sopenharmony_ci    state = (struct inflate_state *)strm->state;
35762306a36Sopenharmony_ci
35862306a36Sopenharmony_ci    if (state->mode == TYPE) state->mode = TYPEDO;      /* skip check */
35962306a36Sopenharmony_ci    LOAD();
36062306a36Sopenharmony_ci    in = have;
36162306a36Sopenharmony_ci    out = left;
36262306a36Sopenharmony_ci    ret = Z_OK;
36362306a36Sopenharmony_ci    for (;;)
36462306a36Sopenharmony_ci        switch (state->mode) {
36562306a36Sopenharmony_ci        case HEAD:
36662306a36Sopenharmony_ci            if (state->wrap == 0) {
36762306a36Sopenharmony_ci                state->mode = TYPEDO;
36862306a36Sopenharmony_ci                break;
36962306a36Sopenharmony_ci            }
37062306a36Sopenharmony_ci            NEEDBITS(16);
37162306a36Sopenharmony_ci            if (
37262306a36Sopenharmony_ci                ((BITS(8) << 8) + (hold >> 8)) % 31) {
37362306a36Sopenharmony_ci                strm->msg = (char *)"incorrect header check";
37462306a36Sopenharmony_ci                state->mode = BAD;
37562306a36Sopenharmony_ci                break;
37662306a36Sopenharmony_ci            }
37762306a36Sopenharmony_ci            if (BITS(4) != Z_DEFLATED) {
37862306a36Sopenharmony_ci                strm->msg = (char *)"unknown compression method";
37962306a36Sopenharmony_ci                state->mode = BAD;
38062306a36Sopenharmony_ci                break;
38162306a36Sopenharmony_ci            }
38262306a36Sopenharmony_ci            DROPBITS(4);
38362306a36Sopenharmony_ci            len = BITS(4) + 8;
38462306a36Sopenharmony_ci            if (len > state->wbits) {
38562306a36Sopenharmony_ci                strm->msg = (char *)"invalid window size";
38662306a36Sopenharmony_ci                state->mode = BAD;
38762306a36Sopenharmony_ci                break;
38862306a36Sopenharmony_ci            }
38962306a36Sopenharmony_ci            state->dmax = 1U << len;
39062306a36Sopenharmony_ci            strm->adler = state->check = zlib_adler32(0L, NULL, 0);
39162306a36Sopenharmony_ci            state->mode = hold & 0x200 ? DICTID : TYPE;
39262306a36Sopenharmony_ci            INITBITS();
39362306a36Sopenharmony_ci            break;
39462306a36Sopenharmony_ci        case DICTID:
39562306a36Sopenharmony_ci            NEEDBITS(32);
39662306a36Sopenharmony_ci            strm->adler = state->check = REVERSE(hold);
39762306a36Sopenharmony_ci            INITBITS();
39862306a36Sopenharmony_ci            state->mode = DICT;
39962306a36Sopenharmony_ci	    fallthrough;
40062306a36Sopenharmony_ci        case DICT:
40162306a36Sopenharmony_ci            if (state->havedict == 0) {
40262306a36Sopenharmony_ci                RESTORE();
40362306a36Sopenharmony_ci                return Z_NEED_DICT;
40462306a36Sopenharmony_ci            }
40562306a36Sopenharmony_ci            strm->adler = state->check = zlib_adler32(0L, NULL, 0);
40662306a36Sopenharmony_ci            state->mode = TYPE;
40762306a36Sopenharmony_ci	    fallthrough;
40862306a36Sopenharmony_ci        case TYPE:
40962306a36Sopenharmony_ci            if (flush == Z_BLOCK) goto inf_leave;
41062306a36Sopenharmony_ci	    fallthrough;
41162306a36Sopenharmony_ci        case TYPEDO:
41262306a36Sopenharmony_ci            INFLATE_TYPEDO_HOOK(strm, flush);
41362306a36Sopenharmony_ci            if (state->last) {
41462306a36Sopenharmony_ci                BYTEBITS();
41562306a36Sopenharmony_ci                state->mode = CHECK;
41662306a36Sopenharmony_ci                break;
41762306a36Sopenharmony_ci            }
41862306a36Sopenharmony_ci            NEEDBITS(3);
41962306a36Sopenharmony_ci            state->last = BITS(1);
42062306a36Sopenharmony_ci            DROPBITS(1);
42162306a36Sopenharmony_ci            switch (BITS(2)) {
42262306a36Sopenharmony_ci            case 0:                             /* stored block */
42362306a36Sopenharmony_ci                state->mode = STORED;
42462306a36Sopenharmony_ci                break;
42562306a36Sopenharmony_ci            case 1:                             /* fixed block */
42662306a36Sopenharmony_ci                zlib_fixedtables(state);
42762306a36Sopenharmony_ci                state->mode = LEN;              /* decode codes */
42862306a36Sopenharmony_ci                break;
42962306a36Sopenharmony_ci            case 2:                             /* dynamic block */
43062306a36Sopenharmony_ci                state->mode = TABLE;
43162306a36Sopenharmony_ci                break;
43262306a36Sopenharmony_ci            case 3:
43362306a36Sopenharmony_ci                strm->msg = (char *)"invalid block type";
43462306a36Sopenharmony_ci                state->mode = BAD;
43562306a36Sopenharmony_ci            }
43662306a36Sopenharmony_ci            DROPBITS(2);
43762306a36Sopenharmony_ci            break;
43862306a36Sopenharmony_ci        case STORED:
43962306a36Sopenharmony_ci            BYTEBITS();                         /* go to byte boundary */
44062306a36Sopenharmony_ci            NEEDBITS(32);
44162306a36Sopenharmony_ci            if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
44262306a36Sopenharmony_ci                strm->msg = (char *)"invalid stored block lengths";
44362306a36Sopenharmony_ci                state->mode = BAD;
44462306a36Sopenharmony_ci                break;
44562306a36Sopenharmony_ci            }
44662306a36Sopenharmony_ci            state->length = (unsigned)hold & 0xffff;
44762306a36Sopenharmony_ci            INITBITS();
44862306a36Sopenharmony_ci            state->mode = COPY;
44962306a36Sopenharmony_ci	    fallthrough;
45062306a36Sopenharmony_ci        case COPY:
45162306a36Sopenharmony_ci            copy = state->length;
45262306a36Sopenharmony_ci            if (copy) {
45362306a36Sopenharmony_ci                if (copy > have) copy = have;
45462306a36Sopenharmony_ci                if (copy > left) copy = left;
45562306a36Sopenharmony_ci                if (copy == 0) goto inf_leave;
45662306a36Sopenharmony_ci                memcpy(put, next, copy);
45762306a36Sopenharmony_ci                have -= copy;
45862306a36Sopenharmony_ci                next += copy;
45962306a36Sopenharmony_ci                left -= copy;
46062306a36Sopenharmony_ci                put += copy;
46162306a36Sopenharmony_ci                state->length -= copy;
46262306a36Sopenharmony_ci                break;
46362306a36Sopenharmony_ci            }
46462306a36Sopenharmony_ci            state->mode = TYPE;
46562306a36Sopenharmony_ci            break;
46662306a36Sopenharmony_ci        case TABLE:
46762306a36Sopenharmony_ci            NEEDBITS(14);
46862306a36Sopenharmony_ci            state->nlen = BITS(5) + 257;
46962306a36Sopenharmony_ci            DROPBITS(5);
47062306a36Sopenharmony_ci            state->ndist = BITS(5) + 1;
47162306a36Sopenharmony_ci            DROPBITS(5);
47262306a36Sopenharmony_ci            state->ncode = BITS(4) + 4;
47362306a36Sopenharmony_ci            DROPBITS(4);
47462306a36Sopenharmony_ci#ifndef PKZIP_BUG_WORKAROUND
47562306a36Sopenharmony_ci            if (state->nlen > 286 || state->ndist > 30) {
47662306a36Sopenharmony_ci                strm->msg = (char *)"too many length or distance symbols";
47762306a36Sopenharmony_ci                state->mode = BAD;
47862306a36Sopenharmony_ci                break;
47962306a36Sopenharmony_ci            }
48062306a36Sopenharmony_ci#endif
48162306a36Sopenharmony_ci            state->have = 0;
48262306a36Sopenharmony_ci            state->mode = LENLENS;
48362306a36Sopenharmony_ci	    fallthrough;
48462306a36Sopenharmony_ci        case LENLENS:
48562306a36Sopenharmony_ci            while (state->have < state->ncode) {
48662306a36Sopenharmony_ci                NEEDBITS(3);
48762306a36Sopenharmony_ci                state->lens[order[state->have++]] = (unsigned short)BITS(3);
48862306a36Sopenharmony_ci                DROPBITS(3);
48962306a36Sopenharmony_ci            }
49062306a36Sopenharmony_ci            while (state->have < 19)
49162306a36Sopenharmony_ci                state->lens[order[state->have++]] = 0;
49262306a36Sopenharmony_ci            state->next = state->codes;
49362306a36Sopenharmony_ci            state->lencode = (code const *)(state->next);
49462306a36Sopenharmony_ci            state->lenbits = 7;
49562306a36Sopenharmony_ci            ret = zlib_inflate_table(CODES, state->lens, 19, &(state->next),
49662306a36Sopenharmony_ci                                &(state->lenbits), state->work);
49762306a36Sopenharmony_ci            if (ret) {
49862306a36Sopenharmony_ci                strm->msg = (char *)"invalid code lengths set";
49962306a36Sopenharmony_ci                state->mode = BAD;
50062306a36Sopenharmony_ci                break;
50162306a36Sopenharmony_ci            }
50262306a36Sopenharmony_ci            state->have = 0;
50362306a36Sopenharmony_ci            state->mode = CODELENS;
50462306a36Sopenharmony_ci	    fallthrough;
50562306a36Sopenharmony_ci        case CODELENS:
50662306a36Sopenharmony_ci            while (state->have < state->nlen + state->ndist) {
50762306a36Sopenharmony_ci                for (;;) {
50862306a36Sopenharmony_ci                    this = state->lencode[BITS(state->lenbits)];
50962306a36Sopenharmony_ci                    if ((unsigned)(this.bits) <= bits) break;
51062306a36Sopenharmony_ci                    PULLBYTE();
51162306a36Sopenharmony_ci                }
51262306a36Sopenharmony_ci                if (this.val < 16) {
51362306a36Sopenharmony_ci                    NEEDBITS(this.bits);
51462306a36Sopenharmony_ci                    DROPBITS(this.bits);
51562306a36Sopenharmony_ci                    state->lens[state->have++] = this.val;
51662306a36Sopenharmony_ci                }
51762306a36Sopenharmony_ci                else {
51862306a36Sopenharmony_ci                    if (this.val == 16) {
51962306a36Sopenharmony_ci                        NEEDBITS(this.bits + 2);
52062306a36Sopenharmony_ci                        DROPBITS(this.bits);
52162306a36Sopenharmony_ci                        if (state->have == 0) {
52262306a36Sopenharmony_ci                            strm->msg = (char *)"invalid bit length repeat";
52362306a36Sopenharmony_ci                            state->mode = BAD;
52462306a36Sopenharmony_ci                            break;
52562306a36Sopenharmony_ci                        }
52662306a36Sopenharmony_ci                        len = state->lens[state->have - 1];
52762306a36Sopenharmony_ci                        copy = 3 + BITS(2);
52862306a36Sopenharmony_ci                        DROPBITS(2);
52962306a36Sopenharmony_ci                    }
53062306a36Sopenharmony_ci                    else if (this.val == 17) {
53162306a36Sopenharmony_ci                        NEEDBITS(this.bits + 3);
53262306a36Sopenharmony_ci                        DROPBITS(this.bits);
53362306a36Sopenharmony_ci                        len = 0;
53462306a36Sopenharmony_ci                        copy = 3 + BITS(3);
53562306a36Sopenharmony_ci                        DROPBITS(3);
53662306a36Sopenharmony_ci                    }
53762306a36Sopenharmony_ci                    else {
53862306a36Sopenharmony_ci                        NEEDBITS(this.bits + 7);
53962306a36Sopenharmony_ci                        DROPBITS(this.bits);
54062306a36Sopenharmony_ci                        len = 0;
54162306a36Sopenharmony_ci                        copy = 11 + BITS(7);
54262306a36Sopenharmony_ci                        DROPBITS(7);
54362306a36Sopenharmony_ci                    }
54462306a36Sopenharmony_ci                    if (state->have + copy > state->nlen + state->ndist) {
54562306a36Sopenharmony_ci                        strm->msg = (char *)"invalid bit length repeat";
54662306a36Sopenharmony_ci                        state->mode = BAD;
54762306a36Sopenharmony_ci                        break;
54862306a36Sopenharmony_ci                    }
54962306a36Sopenharmony_ci                    while (copy--)
55062306a36Sopenharmony_ci                        state->lens[state->have++] = (unsigned short)len;
55162306a36Sopenharmony_ci                }
55262306a36Sopenharmony_ci            }
55362306a36Sopenharmony_ci
55462306a36Sopenharmony_ci            /* handle error breaks in while */
55562306a36Sopenharmony_ci            if (state->mode == BAD) break;
55662306a36Sopenharmony_ci
55762306a36Sopenharmony_ci            /* build code tables */
55862306a36Sopenharmony_ci            state->next = state->codes;
55962306a36Sopenharmony_ci            state->lencode = (code const *)(state->next);
56062306a36Sopenharmony_ci            state->lenbits = 9;
56162306a36Sopenharmony_ci            ret = zlib_inflate_table(LENS, state->lens, state->nlen, &(state->next),
56262306a36Sopenharmony_ci                                &(state->lenbits), state->work);
56362306a36Sopenharmony_ci            if (ret) {
56462306a36Sopenharmony_ci                strm->msg = (char *)"invalid literal/lengths set";
56562306a36Sopenharmony_ci                state->mode = BAD;
56662306a36Sopenharmony_ci                break;
56762306a36Sopenharmony_ci            }
56862306a36Sopenharmony_ci            state->distcode = (code const *)(state->next);
56962306a36Sopenharmony_ci            state->distbits = 6;
57062306a36Sopenharmony_ci            ret = zlib_inflate_table(DISTS, state->lens + state->nlen, state->ndist,
57162306a36Sopenharmony_ci                            &(state->next), &(state->distbits), state->work);
57262306a36Sopenharmony_ci            if (ret) {
57362306a36Sopenharmony_ci                strm->msg = (char *)"invalid distances set";
57462306a36Sopenharmony_ci                state->mode = BAD;
57562306a36Sopenharmony_ci                break;
57662306a36Sopenharmony_ci            }
57762306a36Sopenharmony_ci            state->mode = LEN;
57862306a36Sopenharmony_ci	    fallthrough;
57962306a36Sopenharmony_ci        case LEN:
58062306a36Sopenharmony_ci            if (have >= 6 && left >= 258) {
58162306a36Sopenharmony_ci                RESTORE();
58262306a36Sopenharmony_ci                inflate_fast(strm, out);
58362306a36Sopenharmony_ci                LOAD();
58462306a36Sopenharmony_ci                break;
58562306a36Sopenharmony_ci            }
58662306a36Sopenharmony_ci            for (;;) {
58762306a36Sopenharmony_ci                this = state->lencode[BITS(state->lenbits)];
58862306a36Sopenharmony_ci                if ((unsigned)(this.bits) <= bits) break;
58962306a36Sopenharmony_ci                PULLBYTE();
59062306a36Sopenharmony_ci            }
59162306a36Sopenharmony_ci            if (this.op && (this.op & 0xf0) == 0) {
59262306a36Sopenharmony_ci                last = this;
59362306a36Sopenharmony_ci                for (;;) {
59462306a36Sopenharmony_ci                    this = state->lencode[last.val +
59562306a36Sopenharmony_ci                            (BITS(last.bits + last.op) >> last.bits)];
59662306a36Sopenharmony_ci                    if ((unsigned)(last.bits + this.bits) <= bits) break;
59762306a36Sopenharmony_ci                    PULLBYTE();
59862306a36Sopenharmony_ci                }
59962306a36Sopenharmony_ci                DROPBITS(last.bits);
60062306a36Sopenharmony_ci            }
60162306a36Sopenharmony_ci            DROPBITS(this.bits);
60262306a36Sopenharmony_ci            state->length = (unsigned)this.val;
60362306a36Sopenharmony_ci            if ((int)(this.op) == 0) {
60462306a36Sopenharmony_ci                state->mode = LIT;
60562306a36Sopenharmony_ci                break;
60662306a36Sopenharmony_ci            }
60762306a36Sopenharmony_ci            if (this.op & 32) {
60862306a36Sopenharmony_ci                state->mode = TYPE;
60962306a36Sopenharmony_ci                break;
61062306a36Sopenharmony_ci            }
61162306a36Sopenharmony_ci            if (this.op & 64) {
61262306a36Sopenharmony_ci                strm->msg = (char *)"invalid literal/length code";
61362306a36Sopenharmony_ci                state->mode = BAD;
61462306a36Sopenharmony_ci                break;
61562306a36Sopenharmony_ci            }
61662306a36Sopenharmony_ci            state->extra = (unsigned)(this.op) & 15;
61762306a36Sopenharmony_ci            state->mode = LENEXT;
61862306a36Sopenharmony_ci	    fallthrough;
61962306a36Sopenharmony_ci        case LENEXT:
62062306a36Sopenharmony_ci            if (state->extra) {
62162306a36Sopenharmony_ci                NEEDBITS(state->extra);
62262306a36Sopenharmony_ci                state->length += BITS(state->extra);
62362306a36Sopenharmony_ci                DROPBITS(state->extra);
62462306a36Sopenharmony_ci            }
62562306a36Sopenharmony_ci            state->mode = DIST;
62662306a36Sopenharmony_ci	    fallthrough;
62762306a36Sopenharmony_ci        case DIST:
62862306a36Sopenharmony_ci            for (;;) {
62962306a36Sopenharmony_ci                this = state->distcode[BITS(state->distbits)];
63062306a36Sopenharmony_ci                if ((unsigned)(this.bits) <= bits) break;
63162306a36Sopenharmony_ci                PULLBYTE();
63262306a36Sopenharmony_ci            }
63362306a36Sopenharmony_ci            if ((this.op & 0xf0) == 0) {
63462306a36Sopenharmony_ci                last = this;
63562306a36Sopenharmony_ci                for (;;) {
63662306a36Sopenharmony_ci                    this = state->distcode[last.val +
63762306a36Sopenharmony_ci                            (BITS(last.bits + last.op) >> last.bits)];
63862306a36Sopenharmony_ci                    if ((unsigned)(last.bits + this.bits) <= bits) break;
63962306a36Sopenharmony_ci                    PULLBYTE();
64062306a36Sopenharmony_ci                }
64162306a36Sopenharmony_ci                DROPBITS(last.bits);
64262306a36Sopenharmony_ci            }
64362306a36Sopenharmony_ci            DROPBITS(this.bits);
64462306a36Sopenharmony_ci            if (this.op & 64) {
64562306a36Sopenharmony_ci                strm->msg = (char *)"invalid distance code";
64662306a36Sopenharmony_ci                state->mode = BAD;
64762306a36Sopenharmony_ci                break;
64862306a36Sopenharmony_ci            }
64962306a36Sopenharmony_ci            state->offset = (unsigned)this.val;
65062306a36Sopenharmony_ci            state->extra = (unsigned)(this.op) & 15;
65162306a36Sopenharmony_ci            state->mode = DISTEXT;
65262306a36Sopenharmony_ci	    fallthrough;
65362306a36Sopenharmony_ci        case DISTEXT:
65462306a36Sopenharmony_ci            if (state->extra) {
65562306a36Sopenharmony_ci                NEEDBITS(state->extra);
65662306a36Sopenharmony_ci                state->offset += BITS(state->extra);
65762306a36Sopenharmony_ci                DROPBITS(state->extra);
65862306a36Sopenharmony_ci            }
65962306a36Sopenharmony_ci#ifdef INFLATE_STRICT
66062306a36Sopenharmony_ci            if (state->offset > state->dmax) {
66162306a36Sopenharmony_ci                strm->msg = (char *)"invalid distance too far back";
66262306a36Sopenharmony_ci                state->mode = BAD;
66362306a36Sopenharmony_ci                break;
66462306a36Sopenharmony_ci            }
66562306a36Sopenharmony_ci#endif
66662306a36Sopenharmony_ci            if (state->offset > state->whave + out - left) {
66762306a36Sopenharmony_ci                strm->msg = (char *)"invalid distance too far back";
66862306a36Sopenharmony_ci                state->mode = BAD;
66962306a36Sopenharmony_ci                break;
67062306a36Sopenharmony_ci            }
67162306a36Sopenharmony_ci            state->mode = MATCH;
67262306a36Sopenharmony_ci	    fallthrough;
67362306a36Sopenharmony_ci        case MATCH:
67462306a36Sopenharmony_ci            if (left == 0) goto inf_leave;
67562306a36Sopenharmony_ci            copy = out - left;
67662306a36Sopenharmony_ci            if (state->offset > copy) {         /* copy from window */
67762306a36Sopenharmony_ci                copy = state->offset - copy;
67862306a36Sopenharmony_ci                if (copy > state->write) {
67962306a36Sopenharmony_ci                    copy -= state->write;
68062306a36Sopenharmony_ci                    from = state->window + (state->wsize - copy);
68162306a36Sopenharmony_ci                }
68262306a36Sopenharmony_ci                else
68362306a36Sopenharmony_ci                    from = state->window + (state->write - copy);
68462306a36Sopenharmony_ci                if (copy > state->length) copy = state->length;
68562306a36Sopenharmony_ci            }
68662306a36Sopenharmony_ci            else {                              /* copy from output */
68762306a36Sopenharmony_ci                from = put - state->offset;
68862306a36Sopenharmony_ci                copy = state->length;
68962306a36Sopenharmony_ci            }
69062306a36Sopenharmony_ci            if (copy > left) copy = left;
69162306a36Sopenharmony_ci            left -= copy;
69262306a36Sopenharmony_ci            state->length -= copy;
69362306a36Sopenharmony_ci            do {
69462306a36Sopenharmony_ci                *put++ = *from++;
69562306a36Sopenharmony_ci            } while (--copy);
69662306a36Sopenharmony_ci            if (state->length == 0) state->mode = LEN;
69762306a36Sopenharmony_ci            break;
69862306a36Sopenharmony_ci        case LIT:
69962306a36Sopenharmony_ci            if (left == 0) goto inf_leave;
70062306a36Sopenharmony_ci            *put++ = (unsigned char)(state->length);
70162306a36Sopenharmony_ci            left--;
70262306a36Sopenharmony_ci            state->mode = LEN;
70362306a36Sopenharmony_ci            break;
70462306a36Sopenharmony_ci        case CHECK:
70562306a36Sopenharmony_ci            if (state->wrap) {
70662306a36Sopenharmony_ci                NEEDBITS(32);
70762306a36Sopenharmony_ci                out -= left;
70862306a36Sopenharmony_ci                strm->total_out += out;
70962306a36Sopenharmony_ci                state->total += out;
71062306a36Sopenharmony_ci                if (INFLATE_NEED_CHECKSUM(strm) && out)
71162306a36Sopenharmony_ci                    strm->adler = state->check =
71262306a36Sopenharmony_ci                        UPDATE(state->check, put - out, out);
71362306a36Sopenharmony_ci                out = left;
71462306a36Sopenharmony_ci                if ((
71562306a36Sopenharmony_ci                     REVERSE(hold)) != state->check) {
71662306a36Sopenharmony_ci                    strm->msg = (char *)"incorrect data check";
71762306a36Sopenharmony_ci                    state->mode = BAD;
71862306a36Sopenharmony_ci                    break;
71962306a36Sopenharmony_ci                }
72062306a36Sopenharmony_ci                INITBITS();
72162306a36Sopenharmony_ci            }
72262306a36Sopenharmony_ci            state->mode = DONE;
72362306a36Sopenharmony_ci	    fallthrough;
72462306a36Sopenharmony_ci        case DONE:
72562306a36Sopenharmony_ci            ret = Z_STREAM_END;
72662306a36Sopenharmony_ci            goto inf_leave;
72762306a36Sopenharmony_ci        case BAD:
72862306a36Sopenharmony_ci            ret = Z_DATA_ERROR;
72962306a36Sopenharmony_ci            goto inf_leave;
73062306a36Sopenharmony_ci        case MEM:
73162306a36Sopenharmony_ci            return Z_MEM_ERROR;
73262306a36Sopenharmony_ci        case SYNC:
73362306a36Sopenharmony_ci        default:
73462306a36Sopenharmony_ci            return Z_STREAM_ERROR;
73562306a36Sopenharmony_ci        }
73662306a36Sopenharmony_ci
73762306a36Sopenharmony_ci    /*
73862306a36Sopenharmony_ci       Return from inflate(), updating the total counts and the check value.
73962306a36Sopenharmony_ci       If there was no progress during the inflate() call, return a buffer
74062306a36Sopenharmony_ci       error.  Call zlib_updatewindow() to create and/or update the window state.
74162306a36Sopenharmony_ci     */
74262306a36Sopenharmony_ci  inf_leave:
74362306a36Sopenharmony_ci    RESTORE();
74462306a36Sopenharmony_ci    if (INFLATE_NEED_UPDATEWINDOW(strm) &&
74562306a36Sopenharmony_ci            (state->wsize || (state->mode < CHECK && out != strm->avail_out)))
74662306a36Sopenharmony_ci        zlib_updatewindow(strm, out);
74762306a36Sopenharmony_ci
74862306a36Sopenharmony_ci    in -= strm->avail_in;
74962306a36Sopenharmony_ci    out -= strm->avail_out;
75062306a36Sopenharmony_ci    strm->total_in += in;
75162306a36Sopenharmony_ci    strm->total_out += out;
75262306a36Sopenharmony_ci    state->total += out;
75362306a36Sopenharmony_ci    if (INFLATE_NEED_CHECKSUM(strm) && state->wrap && out)
75462306a36Sopenharmony_ci        strm->adler = state->check =
75562306a36Sopenharmony_ci            UPDATE(state->check, strm->next_out - out, out);
75662306a36Sopenharmony_ci
75762306a36Sopenharmony_ci    strm->data_type = state->bits + (state->last ? 64 : 0) +
75862306a36Sopenharmony_ci                      (state->mode == TYPE ? 128 : 0);
75962306a36Sopenharmony_ci
76062306a36Sopenharmony_ci    if (flush == Z_PACKET_FLUSH && ret == Z_OK &&
76162306a36Sopenharmony_ci            strm->avail_out != 0 && strm->avail_in == 0)
76262306a36Sopenharmony_ci		return zlib_inflateSyncPacket(strm);
76362306a36Sopenharmony_ci
76462306a36Sopenharmony_ci    if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
76562306a36Sopenharmony_ci        ret = Z_BUF_ERROR;
76662306a36Sopenharmony_ci
76762306a36Sopenharmony_ci    return ret;
76862306a36Sopenharmony_ci}
76962306a36Sopenharmony_ci
77062306a36Sopenharmony_ciint zlib_inflateEnd(z_streamp strm)
77162306a36Sopenharmony_ci{
77262306a36Sopenharmony_ci    if (strm == NULL || strm->state == NULL)
77362306a36Sopenharmony_ci        return Z_STREAM_ERROR;
77462306a36Sopenharmony_ci    return Z_OK;
77562306a36Sopenharmony_ci}
77662306a36Sopenharmony_ci
77762306a36Sopenharmony_ci/*
77862306a36Sopenharmony_ci * This subroutine adds the data at next_in/avail_in to the output history
77962306a36Sopenharmony_ci * without performing any output.  The output buffer must be "caught up";
78062306a36Sopenharmony_ci * i.e. no pending output but this should always be the case. The state must
78162306a36Sopenharmony_ci * be waiting on the start of a block (i.e. mode == TYPE or HEAD).  On exit,
78262306a36Sopenharmony_ci * the output will also be caught up, and the checksum will have been updated
78362306a36Sopenharmony_ci * if need be.
78462306a36Sopenharmony_ci */
78562306a36Sopenharmony_ciint zlib_inflateIncomp(z_stream *z)
78662306a36Sopenharmony_ci{
78762306a36Sopenharmony_ci    struct inflate_state *state = (struct inflate_state *)z->state;
78862306a36Sopenharmony_ci    Byte *saved_no = z->next_out;
78962306a36Sopenharmony_ci    uInt saved_ao = z->avail_out;
79062306a36Sopenharmony_ci
79162306a36Sopenharmony_ci    if (state->mode != TYPE && state->mode != HEAD)
79262306a36Sopenharmony_ci	return Z_DATA_ERROR;
79362306a36Sopenharmony_ci
79462306a36Sopenharmony_ci    /* Setup some variables to allow misuse of updateWindow */
79562306a36Sopenharmony_ci    z->avail_out = 0;
79662306a36Sopenharmony_ci    z->next_out = (unsigned char*)z->next_in + z->avail_in;
79762306a36Sopenharmony_ci
79862306a36Sopenharmony_ci    zlib_updatewindow(z, z->avail_in);
79962306a36Sopenharmony_ci
80062306a36Sopenharmony_ci    /* Restore saved variables */
80162306a36Sopenharmony_ci    z->avail_out = saved_ao;
80262306a36Sopenharmony_ci    z->next_out = saved_no;
80362306a36Sopenharmony_ci
80462306a36Sopenharmony_ci    z->adler = state->check =
80562306a36Sopenharmony_ci        UPDATE(state->check, z->next_in, z->avail_in);
80662306a36Sopenharmony_ci
80762306a36Sopenharmony_ci    z->total_out += z->avail_in;
80862306a36Sopenharmony_ci    z->total_in += z->avail_in;
80962306a36Sopenharmony_ci    z->next_in += z->avail_in;
81062306a36Sopenharmony_ci    state->total += z->avail_in;
81162306a36Sopenharmony_ci    z->avail_in = 0;
81262306a36Sopenharmony_ci
81362306a36Sopenharmony_ci    return Z_OK;
81462306a36Sopenharmony_ci}
815