xref: /netbsd-src/external/gpl3/binutils/dist/zlib/infback.c (revision 4f645668ed707e1f969c546666f8c8e45e6f8888)
19573673dSchristos /* infback.c -- inflate using a call-back interface
2*4f645668Schristos  * Copyright (C) 1995-2022 Mark Adler
39573673dSchristos  * For conditions of distribution and use, see copyright notice in zlib.h
49573673dSchristos  */
59573673dSchristos 
69573673dSchristos /*
79573673dSchristos    This code is largely copied from inflate.c.  Normally either infback.o or
89573673dSchristos    inflate.o would be linked into an application--not both.  The interface
99573673dSchristos    with inffast.c is retained so that optimized assembler-coded versions of
109573673dSchristos    inflate_fast() can be used with either inflate.c or infback.c.
119573673dSchristos  */
129573673dSchristos 
139573673dSchristos #include "zutil.h"
149573673dSchristos #include "inftrees.h"
159573673dSchristos #include "inflate.h"
169573673dSchristos #include "inffast.h"
179573673dSchristos 
189573673dSchristos /* function prototypes */
199573673dSchristos local void fixedtables OF((struct inflate_state FAR *state));
209573673dSchristos 
219573673dSchristos /*
229573673dSchristos    strm provides memory allocation functions in zalloc and zfree, or
239573673dSchristos    Z_NULL to use the library memory allocation functions.
249573673dSchristos 
259573673dSchristos    windowBits is in the range 8..15, and window is a user-supplied
269573673dSchristos    window and output buffer that is 2**windowBits bytes.
279573673dSchristos  */
inflateBackInit_(strm,windowBits,window,version,stream_size)289573673dSchristos int ZEXPORT inflateBackInit_(strm, windowBits, window, version, stream_size)
299573673dSchristos z_streamp strm;
309573673dSchristos int windowBits;
319573673dSchristos unsigned char FAR *window;
329573673dSchristos const char *version;
339573673dSchristos int stream_size;
349573673dSchristos {
359573673dSchristos     struct inflate_state FAR *state;
369573673dSchristos 
379573673dSchristos     if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
389573673dSchristos         stream_size != (int)(sizeof(z_stream)))
399573673dSchristos         return Z_VERSION_ERROR;
409573673dSchristos     if (strm == Z_NULL || window == Z_NULL ||
419573673dSchristos         windowBits < 8 || windowBits > 15)
429573673dSchristos         return Z_STREAM_ERROR;
439573673dSchristos     strm->msg = Z_NULL;                 /* in case we return an error */
449573673dSchristos     if (strm->zalloc == (alloc_func)0) {
459573673dSchristos #ifdef Z_SOLO
469573673dSchristos         return Z_STREAM_ERROR;
479573673dSchristos #else
489573673dSchristos         strm->zalloc = zcalloc;
499573673dSchristos         strm->opaque = (voidpf)0;
509573673dSchristos #endif
519573673dSchristos     }
529573673dSchristos     if (strm->zfree == (free_func)0)
539573673dSchristos #ifdef Z_SOLO
549573673dSchristos         return Z_STREAM_ERROR;
559573673dSchristos #else
569573673dSchristos     strm->zfree = zcfree;
579573673dSchristos #endif
589573673dSchristos     state = (struct inflate_state FAR *)ZALLOC(strm, 1,
599573673dSchristos                                                sizeof(struct inflate_state));
609573673dSchristos     if (state == Z_NULL) return Z_MEM_ERROR;
619573673dSchristos     Tracev((stderr, "inflate: allocated\n"));
629573673dSchristos     strm->state = (struct internal_state FAR *)state;
639573673dSchristos     state->dmax = 32768U;
64fc4f4269Schristos     state->wbits = (uInt)windowBits;
659573673dSchristos     state->wsize = 1U << windowBits;
669573673dSchristos     state->window = window;
679573673dSchristos     state->wnext = 0;
689573673dSchristos     state->whave = 0;
699573673dSchristos     return Z_OK;
709573673dSchristos }
719573673dSchristos 
729573673dSchristos /*
739573673dSchristos    Return state with length and distance decoding tables and index sizes set to
749573673dSchristos    fixed code decoding.  Normally this returns fixed tables from inffixed.h.
759573673dSchristos    If BUILDFIXED is defined, then instead this routine builds the tables the
769573673dSchristos    first time it's called, and returns those tables the first time and
779573673dSchristos    thereafter.  This reduces the size of the code by about 2K bytes, in
789573673dSchristos    exchange for a little execution time.  However, BUILDFIXED should not be
799573673dSchristos    used for threaded applications, since the rewriting of the tables and virgin
809573673dSchristos    may not be thread-safe.
819573673dSchristos  */
fixedtables(state)829573673dSchristos local void fixedtables(state)
839573673dSchristos struct inflate_state FAR *state;
849573673dSchristos {
859573673dSchristos #ifdef BUILDFIXED
869573673dSchristos     static int virgin = 1;
879573673dSchristos     static code *lenfix, *distfix;
889573673dSchristos     static code fixed[544];
899573673dSchristos 
909573673dSchristos     /* build fixed huffman tables if first call (may not be thread safe) */
919573673dSchristos     if (virgin) {
929573673dSchristos         unsigned sym, bits;
939573673dSchristos         static code *next;
949573673dSchristos 
959573673dSchristos         /* literal/length table */
969573673dSchristos         sym = 0;
979573673dSchristos         while (sym < 144) state->lens[sym++] = 8;
989573673dSchristos         while (sym < 256) state->lens[sym++] = 9;
999573673dSchristos         while (sym < 280) state->lens[sym++] = 7;
1009573673dSchristos         while (sym < 288) state->lens[sym++] = 8;
1019573673dSchristos         next = fixed;
1029573673dSchristos         lenfix = next;
1039573673dSchristos         bits = 9;
1049573673dSchristos         inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
1059573673dSchristos 
1069573673dSchristos         /* distance table */
1079573673dSchristos         sym = 0;
1089573673dSchristos         while (sym < 32) state->lens[sym++] = 5;
1099573673dSchristos         distfix = next;
1109573673dSchristos         bits = 5;
1119573673dSchristos         inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
1129573673dSchristos 
1139573673dSchristos         /* do this just once */
1149573673dSchristos         virgin = 0;
1159573673dSchristos     }
1169573673dSchristos #else /* !BUILDFIXED */
1179573673dSchristos #   include "inffixed.h"
1189573673dSchristos #endif /* BUILDFIXED */
1199573673dSchristos     state->lencode = lenfix;
1209573673dSchristos     state->lenbits = 9;
1219573673dSchristos     state->distcode = distfix;
1229573673dSchristos     state->distbits = 5;
1239573673dSchristos }
1249573673dSchristos 
1259573673dSchristos /* Macros for inflateBack(): */
1269573673dSchristos 
1279573673dSchristos /* Load returned state from inflate_fast() */
1289573673dSchristos #define LOAD() \
1299573673dSchristos     do { \
1309573673dSchristos         put = strm->next_out; \
1319573673dSchristos         left = strm->avail_out; \
1329573673dSchristos         next = strm->next_in; \
1339573673dSchristos         have = strm->avail_in; \
1349573673dSchristos         hold = state->hold; \
1359573673dSchristos         bits = state->bits; \
1369573673dSchristos     } while (0)
1379573673dSchristos 
1389573673dSchristos /* Set state from registers for inflate_fast() */
1399573673dSchristos #define RESTORE() \
1409573673dSchristos     do { \
1419573673dSchristos         strm->next_out = put; \
1429573673dSchristos         strm->avail_out = left; \
1439573673dSchristos         strm->next_in = next; \
1449573673dSchristos         strm->avail_in = have; \
1459573673dSchristos         state->hold = hold; \
1469573673dSchristos         state->bits = bits; \
1479573673dSchristos     } while (0)
1489573673dSchristos 
1499573673dSchristos /* Clear the input bit accumulator */
1509573673dSchristos #define INITBITS() \
1519573673dSchristos     do { \
1529573673dSchristos         hold = 0; \
1539573673dSchristos         bits = 0; \
1549573673dSchristos     } while (0)
1559573673dSchristos 
1569573673dSchristos /* Assure that some input is available.  If input is requested, but denied,
1579573673dSchristos    then return a Z_BUF_ERROR from inflateBack(). */
1589573673dSchristos #define PULL() \
1599573673dSchristos     do { \
1609573673dSchristos         if (have == 0) { \
1619573673dSchristos             have = in(in_desc, &next); \
1629573673dSchristos             if (have == 0) { \
1639573673dSchristos                 next = Z_NULL; \
1649573673dSchristos                 ret = Z_BUF_ERROR; \
1659573673dSchristos                 goto inf_leave; \
1669573673dSchristos             } \
1679573673dSchristos         } \
1689573673dSchristos     } while (0)
1699573673dSchristos 
1709573673dSchristos /* Get a byte of input into the bit accumulator, or return from inflateBack()
1719573673dSchristos    with an error if there is no input available. */
1729573673dSchristos #define PULLBYTE() \
1739573673dSchristos     do { \
1749573673dSchristos         PULL(); \
1759573673dSchristos         have--; \
1769573673dSchristos         hold += (unsigned long)(*next++) << bits; \
1779573673dSchristos         bits += 8; \
1789573673dSchristos     } while (0)
1799573673dSchristos 
1809573673dSchristos /* Assure that there are at least n bits in the bit accumulator.  If there is
1819573673dSchristos    not enough available input to do that, then return from inflateBack() with
1829573673dSchristos    an error. */
1839573673dSchristos #define NEEDBITS(n) \
1849573673dSchristos     do { \
1859573673dSchristos         while (bits < (unsigned)(n)) \
1869573673dSchristos             PULLBYTE(); \
1879573673dSchristos     } while (0)
1889573673dSchristos 
1899573673dSchristos /* Return the low n bits of the bit accumulator (n < 16) */
1909573673dSchristos #define BITS(n) \
1919573673dSchristos     ((unsigned)hold & ((1U << (n)) - 1))
1929573673dSchristos 
1939573673dSchristos /* Remove n bits from the bit accumulator */
1949573673dSchristos #define DROPBITS(n) \
1959573673dSchristos     do { \
1969573673dSchristos         hold >>= (n); \
1979573673dSchristos         bits -= (unsigned)(n); \
1989573673dSchristos     } while (0)
1999573673dSchristos 
2009573673dSchristos /* Remove zero to seven bits as needed to go to a byte boundary */
2019573673dSchristos #define BYTEBITS() \
2029573673dSchristos     do { \
2039573673dSchristos         hold >>= bits & 7; \
2049573673dSchristos         bits -= bits & 7; \
2059573673dSchristos     } while (0)
2069573673dSchristos 
2079573673dSchristos /* Assure that some output space is available, by writing out the window
2089573673dSchristos    if it's full.  If the write fails, return from inflateBack() with a
2099573673dSchristos    Z_BUF_ERROR. */
2109573673dSchristos #define ROOM() \
2119573673dSchristos     do { \
2129573673dSchristos         if (left == 0) { \
2139573673dSchristos             put = state->window; \
2149573673dSchristos             left = state->wsize; \
2159573673dSchristos             state->whave = left; \
2169573673dSchristos             if (out(out_desc, put, left)) { \
2179573673dSchristos                 ret = Z_BUF_ERROR; \
2189573673dSchristos                 goto inf_leave; \
2199573673dSchristos             } \
2209573673dSchristos         } \
2219573673dSchristos     } while (0)
2229573673dSchristos 
2239573673dSchristos /*
2249573673dSchristos    strm provides the memory allocation functions and window buffer on input,
2259573673dSchristos    and provides information on the unused input on return.  For Z_DATA_ERROR
2269573673dSchristos    returns, strm will also provide an error message.
2279573673dSchristos 
2289573673dSchristos    in() and out() are the call-back input and output functions.  When
2299573673dSchristos    inflateBack() needs more input, it calls in().  When inflateBack() has
2309573673dSchristos    filled the window with output, or when it completes with data in the
2319573673dSchristos    window, it calls out() to write out the data.  The application must not
2329573673dSchristos    change the provided input until in() is called again or inflateBack()
2339573673dSchristos    returns.  The application must not change the window/output buffer until
2349573673dSchristos    inflateBack() returns.
2359573673dSchristos 
2369573673dSchristos    in() and out() are called with a descriptor parameter provided in the
2379573673dSchristos    inflateBack() call.  This parameter can be a structure that provides the
2389573673dSchristos    information required to do the read or write, as well as accumulated
2399573673dSchristos    information on the input and output such as totals and check values.
2409573673dSchristos 
2419573673dSchristos    in() should return zero on failure.  out() should return non-zero on
2429573673dSchristos    failure.  If either in() or out() fails, than inflateBack() returns a
2439573673dSchristos    Z_BUF_ERROR.  strm->next_in can be checked for Z_NULL to see whether it
2449573673dSchristos    was in() or out() that caused in the error.  Otherwise,  inflateBack()
2459573673dSchristos    returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format
2469573673dSchristos    error, or Z_MEM_ERROR if it could not allocate memory for the state.
2479573673dSchristos    inflateBack() can also return Z_STREAM_ERROR if the input parameters
2489573673dSchristos    are not correct, i.e. strm is Z_NULL or the state was not initialized.
2499573673dSchristos  */
inflateBack(strm,in,in_desc,out,out_desc)2509573673dSchristos int ZEXPORT inflateBack(strm, in, in_desc, out, out_desc)
2519573673dSchristos z_streamp strm;
2529573673dSchristos in_func in;
2539573673dSchristos void FAR *in_desc;
2549573673dSchristos out_func out;
2559573673dSchristos void FAR *out_desc;
2569573673dSchristos {
2579573673dSchristos     struct inflate_state FAR *state;
2588cbf5cb7Schristos     z_const unsigned char FAR *next;    /* next input */
2599573673dSchristos     unsigned char FAR *put;     /* next output */
2609573673dSchristos     unsigned have, left;        /* available input and output */
2619573673dSchristos     unsigned long hold;         /* bit buffer */
2629573673dSchristos     unsigned bits;              /* bits in bit buffer */
2639573673dSchristos     unsigned copy;              /* number of stored or match bytes to copy */
2649573673dSchristos     unsigned char FAR *from;    /* where to copy match bytes from */
2659573673dSchristos     code here;                  /* current decoding table entry */
2669573673dSchristos     code last;                  /* parent table entry */
2679573673dSchristos     unsigned len;               /* length to copy for repeats, bits to drop */
2689573673dSchristos     int ret;                    /* return code */
2699573673dSchristos     static const unsigned short order[19] = /* permutation of code lengths */
2709573673dSchristos         {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
2719573673dSchristos 
2729573673dSchristos     /* Check that the strm exists and that the state was initialized */
2739573673dSchristos     if (strm == Z_NULL || strm->state == Z_NULL)
2749573673dSchristos         return Z_STREAM_ERROR;
2759573673dSchristos     state = (struct inflate_state FAR *)strm->state;
2769573673dSchristos 
2779573673dSchristos     /* Reset the state */
2789573673dSchristos     strm->msg = Z_NULL;
2799573673dSchristos     state->mode = TYPE;
2809573673dSchristos     state->last = 0;
2819573673dSchristos     state->whave = 0;
2829573673dSchristos     next = strm->next_in;
2839573673dSchristos     have = next != Z_NULL ? strm->avail_in : 0;
2849573673dSchristos     hold = 0;
2859573673dSchristos     bits = 0;
2869573673dSchristos     put = state->window;
2879573673dSchristos     left = state->wsize;
2889573673dSchristos 
2899573673dSchristos     /* Inflate until end of block marked as last */
2909573673dSchristos     for (;;)
2919573673dSchristos         switch (state->mode) {
2929573673dSchristos         case TYPE:
2939573673dSchristos             /* determine and dispatch block type */
2949573673dSchristos             if (state->last) {
2959573673dSchristos                 BYTEBITS();
2969573673dSchristos                 state->mode = DONE;
2979573673dSchristos                 break;
2989573673dSchristos             }
2999573673dSchristos             NEEDBITS(3);
3009573673dSchristos             state->last = BITS(1);
3019573673dSchristos             DROPBITS(1);
3029573673dSchristos             switch (BITS(2)) {
3039573673dSchristos             case 0:                             /* stored block */
3049573673dSchristos                 Tracev((stderr, "inflate:     stored block%s\n",
3059573673dSchristos                         state->last ? " (last)" : ""));
3069573673dSchristos                 state->mode = STORED;
3079573673dSchristos                 break;
3089573673dSchristos             case 1:                             /* fixed block */
3099573673dSchristos                 fixedtables(state);
3109573673dSchristos                 Tracev((stderr, "inflate:     fixed codes block%s\n",
3119573673dSchristos                         state->last ? " (last)" : ""));
3129573673dSchristos                 state->mode = LEN;              /* decode codes */
3139573673dSchristos                 break;
3149573673dSchristos             case 2:                             /* dynamic block */
3159573673dSchristos                 Tracev((stderr, "inflate:     dynamic codes block%s\n",
3169573673dSchristos                         state->last ? " (last)" : ""));
3179573673dSchristos                 state->mode = TABLE;
3189573673dSchristos                 break;
3199573673dSchristos             case 3:
3209573673dSchristos                 strm->msg = (char *)"invalid block type";
3219573673dSchristos                 state->mode = BAD;
3229573673dSchristos             }
3239573673dSchristos             DROPBITS(2);
3249573673dSchristos             break;
3259573673dSchristos 
3269573673dSchristos         case STORED:
3279573673dSchristos             /* get and verify stored block length */
3289573673dSchristos             BYTEBITS();                         /* go to byte boundary */
3299573673dSchristos             NEEDBITS(32);
3309573673dSchristos             if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
3319573673dSchristos                 strm->msg = (char *)"invalid stored block lengths";
3329573673dSchristos                 state->mode = BAD;
3339573673dSchristos                 break;
3349573673dSchristos             }
3359573673dSchristos             state->length = (unsigned)hold & 0xffff;
3369573673dSchristos             Tracev((stderr, "inflate:       stored length %u\n",
3379573673dSchristos                     state->length));
3389573673dSchristos             INITBITS();
3399573673dSchristos 
3409573673dSchristos             /* copy stored block from input to output */
3419573673dSchristos             while (state->length != 0) {
3429573673dSchristos                 copy = state->length;
3439573673dSchristos                 PULL();
3449573673dSchristos                 ROOM();
3459573673dSchristos                 if (copy > have) copy = have;
3469573673dSchristos                 if (copy > left) copy = left;
3479573673dSchristos                 zmemcpy(put, next, copy);
3489573673dSchristos                 have -= copy;
3499573673dSchristos                 next += copy;
3509573673dSchristos                 left -= copy;
3519573673dSchristos                 put += copy;
3529573673dSchristos                 state->length -= copy;
3539573673dSchristos             }
3549573673dSchristos             Tracev((stderr, "inflate:       stored end\n"));
3559573673dSchristos             state->mode = TYPE;
3569573673dSchristos             break;
3579573673dSchristos 
3589573673dSchristos         case TABLE:
3599573673dSchristos             /* get dynamic table entries descriptor */
3609573673dSchristos             NEEDBITS(14);
3619573673dSchristos             state->nlen = BITS(5) + 257;
3629573673dSchristos             DROPBITS(5);
3639573673dSchristos             state->ndist = BITS(5) + 1;
3649573673dSchristos             DROPBITS(5);
3659573673dSchristos             state->ncode = BITS(4) + 4;
3669573673dSchristos             DROPBITS(4);
3679573673dSchristos #ifndef PKZIP_BUG_WORKAROUND
3689573673dSchristos             if (state->nlen > 286 || state->ndist > 30) {
3699573673dSchristos                 strm->msg = (char *)"too many length or distance symbols";
3709573673dSchristos                 state->mode = BAD;
3719573673dSchristos                 break;
3729573673dSchristos             }
3739573673dSchristos #endif
3749573673dSchristos             Tracev((stderr, "inflate:       table sizes ok\n"));
3759573673dSchristos 
3769573673dSchristos             /* get code length code lengths (not a typo) */
3779573673dSchristos             state->have = 0;
3789573673dSchristos             while (state->have < state->ncode) {
3799573673dSchristos                 NEEDBITS(3);
3809573673dSchristos                 state->lens[order[state->have++]] = (unsigned short)BITS(3);
3819573673dSchristos                 DROPBITS(3);
3829573673dSchristos             }
3839573673dSchristos             while (state->have < 19)
3849573673dSchristos                 state->lens[order[state->have++]] = 0;
3859573673dSchristos             state->next = state->codes;
3869573673dSchristos             state->lencode = (code const FAR *)(state->next);
3879573673dSchristos             state->lenbits = 7;
3889573673dSchristos             ret = inflate_table(CODES, state->lens, 19, &(state->next),
3899573673dSchristos                                 &(state->lenbits), state->work);
3909573673dSchristos             if (ret) {
3919573673dSchristos                 strm->msg = (char *)"invalid code lengths set";
3929573673dSchristos                 state->mode = BAD;
3939573673dSchristos                 break;
3949573673dSchristos             }
3959573673dSchristos             Tracev((stderr, "inflate:       code lengths ok\n"));
3969573673dSchristos 
3979573673dSchristos             /* get length and distance code code lengths */
3989573673dSchristos             state->have = 0;
3999573673dSchristos             while (state->have < state->nlen + state->ndist) {
4009573673dSchristos                 for (;;) {
4019573673dSchristos                     here = state->lencode[BITS(state->lenbits)];
4029573673dSchristos                     if ((unsigned)(here.bits) <= bits) break;
4039573673dSchristos                     PULLBYTE();
4049573673dSchristos                 }
4059573673dSchristos                 if (here.val < 16) {
4069573673dSchristos                     DROPBITS(here.bits);
4079573673dSchristos                     state->lens[state->have++] = here.val;
4089573673dSchristos                 }
4099573673dSchristos                 else {
4109573673dSchristos                     if (here.val == 16) {
4119573673dSchristos                         NEEDBITS(here.bits + 2);
4129573673dSchristos                         DROPBITS(here.bits);
4139573673dSchristos                         if (state->have == 0) {
4149573673dSchristos                             strm->msg = (char *)"invalid bit length repeat";
4159573673dSchristos                             state->mode = BAD;
4169573673dSchristos                             break;
4179573673dSchristos                         }
4189573673dSchristos                         len = (unsigned)(state->lens[state->have - 1]);
4199573673dSchristos                         copy = 3 + BITS(2);
4209573673dSchristos                         DROPBITS(2);
4219573673dSchristos                     }
4229573673dSchristos                     else if (here.val == 17) {
4239573673dSchristos                         NEEDBITS(here.bits + 3);
4249573673dSchristos                         DROPBITS(here.bits);
4259573673dSchristos                         len = 0;
4269573673dSchristos                         copy = 3 + BITS(3);
4279573673dSchristos                         DROPBITS(3);
4289573673dSchristos                     }
4299573673dSchristos                     else {
4309573673dSchristos                         NEEDBITS(here.bits + 7);
4319573673dSchristos                         DROPBITS(here.bits);
4329573673dSchristos                         len = 0;
4339573673dSchristos                         copy = 11 + BITS(7);
4349573673dSchristos                         DROPBITS(7);
4359573673dSchristos                     }
4369573673dSchristos                     if (state->have + copy > state->nlen + state->ndist) {
4379573673dSchristos                         strm->msg = (char *)"invalid bit length repeat";
4389573673dSchristos                         state->mode = BAD;
4399573673dSchristos                         break;
4409573673dSchristos                     }
4419573673dSchristos                     while (copy--)
4429573673dSchristos                         state->lens[state->have++] = (unsigned short)len;
4439573673dSchristos                 }
4449573673dSchristos             }
4459573673dSchristos 
4469573673dSchristos             /* handle error breaks in while */
4479573673dSchristos             if (state->mode == BAD) break;
4489573673dSchristos 
4499573673dSchristos             /* check for end-of-block code (better have one) */
4509573673dSchristos             if (state->lens[256] == 0) {
4519573673dSchristos                 strm->msg = (char *)"invalid code -- missing end-of-block";
4529573673dSchristos                 state->mode = BAD;
4539573673dSchristos                 break;
4549573673dSchristos             }
4559573673dSchristos 
4569573673dSchristos             /* build code tables -- note: do not change the lenbits or distbits
4579573673dSchristos                values here (9 and 6) without reading the comments in inftrees.h
4589573673dSchristos                concerning the ENOUGH constants, which depend on those values */
4599573673dSchristos             state->next = state->codes;
4609573673dSchristos             state->lencode = (code const FAR *)(state->next);
4619573673dSchristos             state->lenbits = 9;
4629573673dSchristos             ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
4639573673dSchristos                                 &(state->lenbits), state->work);
4649573673dSchristos             if (ret) {
4659573673dSchristos                 strm->msg = (char *)"invalid literal/lengths set";
4669573673dSchristos                 state->mode = BAD;
4679573673dSchristos                 break;
4689573673dSchristos             }
4699573673dSchristos             state->distcode = (code const FAR *)(state->next);
4709573673dSchristos             state->distbits = 6;
4719573673dSchristos             ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
4729573673dSchristos                             &(state->next), &(state->distbits), state->work);
4739573673dSchristos             if (ret) {
4749573673dSchristos                 strm->msg = (char *)"invalid distances set";
4759573673dSchristos                 state->mode = BAD;
4769573673dSchristos                 break;
4779573673dSchristos             }
4789573673dSchristos             Tracev((stderr, "inflate:       codes ok\n"));
4799573673dSchristos             state->mode = LEN;
480*4f645668Schristos                 /* fallthrough */
4819573673dSchristos 
4829573673dSchristos         case LEN:
4839573673dSchristos             /* use inflate_fast() if we have enough input and output */
4849573673dSchristos             if (have >= 6 && left >= 258) {
4859573673dSchristos                 RESTORE();
4869573673dSchristos                 if (state->whave < state->wsize)
4879573673dSchristos                     state->whave = state->wsize - left;
4889573673dSchristos                 inflate_fast(strm, state->wsize);
4899573673dSchristos                 LOAD();
4909573673dSchristos                 break;
4919573673dSchristos             }
4929573673dSchristos 
4939573673dSchristos             /* get a literal, length, or end-of-block code */
4949573673dSchristos             for (;;) {
4959573673dSchristos                 here = state->lencode[BITS(state->lenbits)];
4969573673dSchristos                 if ((unsigned)(here.bits) <= bits) break;
4979573673dSchristos                 PULLBYTE();
4989573673dSchristos             }
4999573673dSchristos             if (here.op && (here.op & 0xf0) == 0) {
5009573673dSchristos                 last = here;
5019573673dSchristos                 for (;;) {
5029573673dSchristos                     here = state->lencode[last.val +
5039573673dSchristos                             (BITS(last.bits + last.op) >> last.bits)];
5049573673dSchristos                     if ((unsigned)(last.bits + here.bits) <= bits) break;
5059573673dSchristos                     PULLBYTE();
5069573673dSchristos                 }
5079573673dSchristos                 DROPBITS(last.bits);
5089573673dSchristos             }
5099573673dSchristos             DROPBITS(here.bits);
5109573673dSchristos             state->length = (unsigned)here.val;
5119573673dSchristos 
5129573673dSchristos             /* process literal */
5139573673dSchristos             if (here.op == 0) {
5149573673dSchristos                 Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
5159573673dSchristos                         "inflate:         literal '%c'\n" :
5169573673dSchristos                         "inflate:         literal 0x%02x\n", here.val));
5179573673dSchristos                 ROOM();
5189573673dSchristos                 *put++ = (unsigned char)(state->length);
5199573673dSchristos                 left--;
5209573673dSchristos                 state->mode = LEN;
5219573673dSchristos                 break;
5229573673dSchristos             }
5239573673dSchristos 
5249573673dSchristos             /* process end of block */
5259573673dSchristos             if (here.op & 32) {
5269573673dSchristos                 Tracevv((stderr, "inflate:         end of block\n"));
5279573673dSchristos                 state->mode = TYPE;
5289573673dSchristos                 break;
5299573673dSchristos             }
5309573673dSchristos 
5319573673dSchristos             /* invalid code */
5329573673dSchristos             if (here.op & 64) {
5339573673dSchristos                 strm->msg = (char *)"invalid literal/length code";
5349573673dSchristos                 state->mode = BAD;
5359573673dSchristos                 break;
5369573673dSchristos             }
5379573673dSchristos 
5389573673dSchristos             /* length code -- get extra bits, if any */
5399573673dSchristos             state->extra = (unsigned)(here.op) & 15;
5409573673dSchristos             if (state->extra != 0) {
5419573673dSchristos                 NEEDBITS(state->extra);
5429573673dSchristos                 state->length += BITS(state->extra);
5439573673dSchristos                 DROPBITS(state->extra);
5449573673dSchristos             }
5459573673dSchristos             Tracevv((stderr, "inflate:         length %u\n", state->length));
5469573673dSchristos 
5479573673dSchristos             /* get distance code */
5489573673dSchristos             for (;;) {
5499573673dSchristos                 here = state->distcode[BITS(state->distbits)];
5509573673dSchristos                 if ((unsigned)(here.bits) <= bits) break;
5519573673dSchristos                 PULLBYTE();
5529573673dSchristos             }
5539573673dSchristos             if ((here.op & 0xf0) == 0) {
5549573673dSchristos                 last = here;
5559573673dSchristos                 for (;;) {
5569573673dSchristos                     here = state->distcode[last.val +
5579573673dSchristos                             (BITS(last.bits + last.op) >> last.bits)];
5589573673dSchristos                     if ((unsigned)(last.bits + here.bits) <= bits) break;
5599573673dSchristos                     PULLBYTE();
5609573673dSchristos                 }
5619573673dSchristos                 DROPBITS(last.bits);
5629573673dSchristos             }
5639573673dSchristos             DROPBITS(here.bits);
5649573673dSchristos             if (here.op & 64) {
5659573673dSchristos                 strm->msg = (char *)"invalid distance code";
5669573673dSchristos                 state->mode = BAD;
5679573673dSchristos                 break;
5689573673dSchristos             }
5699573673dSchristos             state->offset = (unsigned)here.val;
5709573673dSchristos 
5719573673dSchristos             /* get distance extra bits, if any */
5729573673dSchristos             state->extra = (unsigned)(here.op) & 15;
5739573673dSchristos             if (state->extra != 0) {
5749573673dSchristos                 NEEDBITS(state->extra);
5759573673dSchristos                 state->offset += BITS(state->extra);
5769573673dSchristos                 DROPBITS(state->extra);
5779573673dSchristos             }
5789573673dSchristos             if (state->offset > state->wsize - (state->whave < state->wsize ?
5799573673dSchristos                                                 left : 0)) {
5809573673dSchristos                 strm->msg = (char *)"invalid distance too far back";
5819573673dSchristos                 state->mode = BAD;
5829573673dSchristos                 break;
5839573673dSchristos             }
5849573673dSchristos             Tracevv((stderr, "inflate:         distance %u\n", state->offset));
5859573673dSchristos 
5869573673dSchristos             /* copy match from window to output */
5879573673dSchristos             do {
5889573673dSchristos                 ROOM();
5899573673dSchristos                 copy = state->wsize - state->offset;
5909573673dSchristos                 if (copy < left) {
5919573673dSchristos                     from = put + copy;
5929573673dSchristos                     copy = left - copy;
5939573673dSchristos                 }
5949573673dSchristos                 else {
5959573673dSchristos                     from = put - state->offset;
5969573673dSchristos                     copy = left;
5979573673dSchristos                 }
5989573673dSchristos                 if (copy > state->length) copy = state->length;
5999573673dSchristos                 state->length -= copy;
6009573673dSchristos                 left -= copy;
6019573673dSchristos                 do {
6029573673dSchristos                     *put++ = *from++;
6039573673dSchristos                 } while (--copy);
6049573673dSchristos             } while (state->length != 0);
6059573673dSchristos             break;
6069573673dSchristos 
6079573673dSchristos         case DONE:
6089573673dSchristos             /* inflate stream terminated properly -- write leftover output */
6099573673dSchristos             ret = Z_STREAM_END;
6109573673dSchristos             if (left < state->wsize) {
6119573673dSchristos                 if (out(out_desc, state->window, state->wsize - left))
6129573673dSchristos                     ret = Z_BUF_ERROR;
6139573673dSchristos             }
6149573673dSchristos             goto inf_leave;
6159573673dSchristos 
6169573673dSchristos         case BAD:
6179573673dSchristos             ret = Z_DATA_ERROR;
6189573673dSchristos             goto inf_leave;
6199573673dSchristos 
6209573673dSchristos         default:                /* can't happen, but makes compilers happy */
6219573673dSchristos             ret = Z_STREAM_ERROR;
6229573673dSchristos             goto inf_leave;
6239573673dSchristos         }
6249573673dSchristos 
6259573673dSchristos     /* Return unused input */
6269573673dSchristos   inf_leave:
6279573673dSchristos     strm->next_in = next;
6289573673dSchristos     strm->avail_in = have;
6299573673dSchristos     return ret;
6309573673dSchristos }
6319573673dSchristos 
inflateBackEnd(strm)6329573673dSchristos int ZEXPORT inflateBackEnd(strm)
6339573673dSchristos z_streamp strm;
6349573673dSchristos {
6359573673dSchristos     if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
6369573673dSchristos         return Z_STREAM_ERROR;
6379573673dSchristos     ZFREE(strm, strm->state);
6389573673dSchristos     strm->state = Z_NULL;
6399573673dSchristos     Tracev((stderr, "inflate: end\n"));
6409573673dSchristos     return Z_OK;
6419573673dSchristos }
642