18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci#define DEBG(x)
38c2ecf20Sopenharmony_ci#define DEBG1(x)
48c2ecf20Sopenharmony_ci/* inflate.c -- Not copyrighted 1992 by Mark Adler
58c2ecf20Sopenharmony_ci   version c10p1, 10 January 1993 */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci/*
88c2ecf20Sopenharmony_ci * Adapted for booting Linux by Hannu Savolainen 1993
98c2ecf20Sopenharmony_ci * based on gzip-1.0.3
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * Nicolas Pitre <nico@fluxnic.net>, 1999/04/14 :
128c2ecf20Sopenharmony_ci *   Little mods for all variable to reside either into rodata or bss segments
138c2ecf20Sopenharmony_ci *   by marking constant variables with 'const' and initializing all the others
148c2ecf20Sopenharmony_ci *   at run-time only.  This allows for the kernel uncompressor to run
158c2ecf20Sopenharmony_ci *   directly from Flash or ROM memory on embedded systems.
168c2ecf20Sopenharmony_ci */
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci/*
198c2ecf20Sopenharmony_ci   Inflate deflated (PKZIP's method 8 compressed) data.  The compression
208c2ecf20Sopenharmony_ci   method searches for as much of the current string of bytes (up to a
218c2ecf20Sopenharmony_ci   length of 258) in the previous 32 K bytes.  If it doesn't find any
228c2ecf20Sopenharmony_ci   matches (of at least length 3), it codes the next byte.  Otherwise, it
238c2ecf20Sopenharmony_ci   codes the length of the matched string and its distance backwards from
248c2ecf20Sopenharmony_ci   the current position.  There is a single Huffman code that codes both
258c2ecf20Sopenharmony_ci   single bytes (called "literals") and match lengths.  A second Huffman
268c2ecf20Sopenharmony_ci   code codes the distance information, which follows a length code.  Each
278c2ecf20Sopenharmony_ci   length or distance code actually represents a base value and a number
288c2ecf20Sopenharmony_ci   of "extra" (sometimes zero) bits to get to add to the base value.  At
298c2ecf20Sopenharmony_ci   the end of each deflated block is a special end-of-block (EOB) literal/
308c2ecf20Sopenharmony_ci   length code.  The decoding process is basically: get a literal/length
318c2ecf20Sopenharmony_ci   code; if EOB then done; if a literal, emit the decoded byte; if a
328c2ecf20Sopenharmony_ci   length then get the distance and emit the referred-to bytes from the
338c2ecf20Sopenharmony_ci   sliding window of previously emitted data.
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci   There are (currently) three kinds of inflate blocks: stored, fixed, and
368c2ecf20Sopenharmony_ci   dynamic.  The compressor deals with some chunk of data at a time, and
378c2ecf20Sopenharmony_ci   decides which method to use on a chunk-by-chunk basis.  A chunk might
388c2ecf20Sopenharmony_ci   typically be 32 K or 64 K.  If the chunk is incompressible, then the
398c2ecf20Sopenharmony_ci   "stored" method is used.  In this case, the bytes are simply stored as
408c2ecf20Sopenharmony_ci   is, eight bits per byte, with none of the above coding.  The bytes are
418c2ecf20Sopenharmony_ci   preceded by a count, since there is no longer an EOB code.
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci   If the data is compressible, then either the fixed or dynamic methods
448c2ecf20Sopenharmony_ci   are used.  In the dynamic method, the compressed data is preceded by
458c2ecf20Sopenharmony_ci   an encoding of the literal/length and distance Huffman codes that are
468c2ecf20Sopenharmony_ci   to be used to decode this block.  The representation is itself Huffman
478c2ecf20Sopenharmony_ci   coded, and so is preceded by a description of that code.  These code
488c2ecf20Sopenharmony_ci   descriptions take up a little space, and so for small blocks, there is
498c2ecf20Sopenharmony_ci   a predefined set of codes, called the fixed codes.  The fixed method is
508c2ecf20Sopenharmony_ci   used if the block codes up smaller that way (usually for quite small
518c2ecf20Sopenharmony_ci   chunks), otherwise the dynamic method is used.  In the latter case, the
528c2ecf20Sopenharmony_ci   codes are customized to the probabilities in the current block, and so
538c2ecf20Sopenharmony_ci   can code it much better than the pre-determined fixed codes.
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci   The Huffman codes themselves are decoded using a multi-level table
568c2ecf20Sopenharmony_ci   lookup, in order to maximize the speed of decoding plus the speed of
578c2ecf20Sopenharmony_ci   building the decoding tables.  See the comments below that precede the
588c2ecf20Sopenharmony_ci   lbits and dbits tuning parameters.
598c2ecf20Sopenharmony_ci */
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci/*
638c2ecf20Sopenharmony_ci   Notes beyond the 1.93a appnote.txt:
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci   1. Distance pointers never point before the beginning of the output
668c2ecf20Sopenharmony_ci      stream.
678c2ecf20Sopenharmony_ci   2. Distance pointers can point back across blocks, up to 32k away.
688c2ecf20Sopenharmony_ci   3. There is an implied maximum of 7 bits for the bit length table and
698c2ecf20Sopenharmony_ci      15 bits for the actual data.
708c2ecf20Sopenharmony_ci   4. If only one code exists, then it is encoded using one bit.  (Zero
718c2ecf20Sopenharmony_ci      would be more efficient, but perhaps a little confusing.)  If two
728c2ecf20Sopenharmony_ci      codes exist, they are coded using one bit each (0 and 1).
738c2ecf20Sopenharmony_ci   5. There is no way of sending zero distance codes--a dummy must be
748c2ecf20Sopenharmony_ci      sent if there are none.  (History: a pre 2.0 version of PKZIP would
758c2ecf20Sopenharmony_ci      store blocks with no distance codes, but this was discovered to be
768c2ecf20Sopenharmony_ci      too harsh a criterion.)  Valid only for 1.93a.  2.04c does allow
778c2ecf20Sopenharmony_ci      zero distance codes, which is sent as one code of zero bits in
788c2ecf20Sopenharmony_ci      length.
798c2ecf20Sopenharmony_ci   6. There are up to 286 literal/length codes.  Code 256 represents the
808c2ecf20Sopenharmony_ci      end-of-block.  Note however that the static length tree defines
818c2ecf20Sopenharmony_ci      288 codes just to fill out the Huffman codes.  Codes 286 and 287
828c2ecf20Sopenharmony_ci      cannot be used though, since there is no length base or extra bits
838c2ecf20Sopenharmony_ci      defined for them.  Similarly, there are up to 30 distance codes.
848c2ecf20Sopenharmony_ci      However, static trees define 32 codes (all 5 bits) to fill out the
858c2ecf20Sopenharmony_ci      Huffman codes, but the last two had better not show up in the data.
868c2ecf20Sopenharmony_ci   7. Unzip can check dynamic Huffman blocks for complete code sets.
878c2ecf20Sopenharmony_ci      The exception is that a single code would not be complete (see #4).
888c2ecf20Sopenharmony_ci   8. The five bits following the block type is really the number of
898c2ecf20Sopenharmony_ci      literal codes sent minus 257.
908c2ecf20Sopenharmony_ci   9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
918c2ecf20Sopenharmony_ci      (1+6+6).  Therefore, to output three times the length, you output
928c2ecf20Sopenharmony_ci      three codes (1+1+1), whereas to output four times the same length,
938c2ecf20Sopenharmony_ci      you only need two codes (1+3).  Hmm.
948c2ecf20Sopenharmony_ci  10. In the tree reconstruction algorithm, Code = Code + Increment
958c2ecf20Sopenharmony_ci      only if BitLength(i) is not zero.  (Pretty obvious.)
968c2ecf20Sopenharmony_ci  11. Correction: 4 Bits: # of Bit Length codes - 4     (4 - 19)
978c2ecf20Sopenharmony_ci  12. Note: length code 284 can represent 227-258, but length code 285
988c2ecf20Sopenharmony_ci      really is 258.  The last length deserves its own, short code
998c2ecf20Sopenharmony_ci      since it gets used a lot in very redundant files.  The length
1008c2ecf20Sopenharmony_ci      258 is special since 258 - 3 (the min match length) is 255.
1018c2ecf20Sopenharmony_ci  13. The literal/length and distance code bit lengths are read as a
1028c2ecf20Sopenharmony_ci      single stream of lengths.  It is possible (and advantageous) for
1038c2ecf20Sopenharmony_ci      a repeat code (16, 17, or 18) to go across the boundary between
1048c2ecf20Sopenharmony_ci      the two sets of lengths.
1058c2ecf20Sopenharmony_ci */
1068c2ecf20Sopenharmony_ci#include <linux/compiler.h>
1078c2ecf20Sopenharmony_ci#ifdef NO_INFLATE_MALLOC
1088c2ecf20Sopenharmony_ci#include <linux/slab.h>
1098c2ecf20Sopenharmony_ci#endif
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci#ifdef RCSID
1128c2ecf20Sopenharmony_cistatic char rcsid[] = "#Id: inflate.c,v 0.14 1993/06/10 13:27:04 jloup Exp #";
1138c2ecf20Sopenharmony_ci#endif
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci#ifndef STATIC
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci#if defined(STDC_HEADERS) || defined(HAVE_STDLIB_H)
1188c2ecf20Sopenharmony_ci#  include <sys/types.h>
1198c2ecf20Sopenharmony_ci#  include <stdlib.h>
1208c2ecf20Sopenharmony_ci#endif
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci#include "gzip.h"
1238c2ecf20Sopenharmony_ci#define STATIC
1248c2ecf20Sopenharmony_ci#endif /* !STATIC */
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci#ifndef INIT
1278c2ecf20Sopenharmony_ci#define INIT
1288c2ecf20Sopenharmony_ci#endif
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci#define slide window
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci/* Huffman code lookup table entry--this entry is four bytes for machines
1338c2ecf20Sopenharmony_ci   that have 16-bit pointers (e.g. PC's in the small or medium model).
1348c2ecf20Sopenharmony_ci   Valid extra bits are 0..13.  e == 15 is EOB (end of block), e == 16
1358c2ecf20Sopenharmony_ci   means that v is a literal, 16 < e < 32 means that v is a pointer to
1368c2ecf20Sopenharmony_ci   the next table, which codes e - 16 bits, and lastly e == 99 indicates
1378c2ecf20Sopenharmony_ci   an unused code.  If a code with e == 99 is looked up, this implies an
1388c2ecf20Sopenharmony_ci   error in the data. */
1398c2ecf20Sopenharmony_cistruct huft {
1408c2ecf20Sopenharmony_ci  uch e;                /* number of extra bits or operation */
1418c2ecf20Sopenharmony_ci  uch b;                /* number of bits in this code or subcode */
1428c2ecf20Sopenharmony_ci  union {
1438c2ecf20Sopenharmony_ci    ush n;              /* literal, length base, or distance base */
1448c2ecf20Sopenharmony_ci    struct huft *t;     /* pointer to next level of table */
1458c2ecf20Sopenharmony_ci  } v;
1468c2ecf20Sopenharmony_ci};
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci/* Function prototypes */
1508c2ecf20Sopenharmony_ciSTATIC int INIT huft_build OF((unsigned *, unsigned, unsigned,
1518c2ecf20Sopenharmony_ci		const ush *, const ush *, struct huft **, int *));
1528c2ecf20Sopenharmony_ciSTATIC int INIT huft_free OF((struct huft *));
1538c2ecf20Sopenharmony_ciSTATIC int INIT inflate_codes OF((struct huft *, struct huft *, int, int));
1548c2ecf20Sopenharmony_ciSTATIC int INIT inflate_stored OF((void));
1558c2ecf20Sopenharmony_ciSTATIC int INIT inflate_fixed OF((void));
1568c2ecf20Sopenharmony_ciSTATIC int INIT inflate_dynamic OF((void));
1578c2ecf20Sopenharmony_ciSTATIC int INIT inflate_block OF((int *));
1588c2ecf20Sopenharmony_ciSTATIC int INIT inflate OF((void));
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci/* The inflate algorithm uses a sliding 32 K byte window on the uncompressed
1628c2ecf20Sopenharmony_ci   stream to find repeated byte strings.  This is implemented here as a
1638c2ecf20Sopenharmony_ci   circular buffer.  The index is updated simply by incrementing and then
1648c2ecf20Sopenharmony_ci   ANDing with 0x7fff (32K-1). */
1658c2ecf20Sopenharmony_ci/* It is left to other modules to supply the 32 K area.  It is assumed
1668c2ecf20Sopenharmony_ci   to be usable as if it were declared "uch slide[32768];" or as just
1678c2ecf20Sopenharmony_ci   "uch *slide;" and then malloc'ed in the latter case.  The definition
1688c2ecf20Sopenharmony_ci   must be in unzip.h, included above. */
1698c2ecf20Sopenharmony_ci/* unsigned wp;             current position in slide */
1708c2ecf20Sopenharmony_ci#define wp outcnt
1718c2ecf20Sopenharmony_ci#define flush_output(w) (wp=(w),flush_window())
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci/* Tables for deflate from PKZIP's appnote.txt. */
1748c2ecf20Sopenharmony_cistatic const unsigned border[] = {    /* Order of the bit length code lengths */
1758c2ecf20Sopenharmony_ci        16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
1768c2ecf20Sopenharmony_cistatic const ush cplens[] = {         /* Copy lengths for literal codes 257..285 */
1778c2ecf20Sopenharmony_ci        3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
1788c2ecf20Sopenharmony_ci        35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
1798c2ecf20Sopenharmony_ci        /* note: see note #13 above about the 258 in this list. */
1808c2ecf20Sopenharmony_cistatic const ush cplext[] = {         /* Extra bits for literal codes 257..285 */
1818c2ecf20Sopenharmony_ci        0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
1828c2ecf20Sopenharmony_ci        3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99}; /* 99==invalid */
1838c2ecf20Sopenharmony_cistatic const ush cpdist[] = {         /* Copy offsets for distance codes 0..29 */
1848c2ecf20Sopenharmony_ci        1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
1858c2ecf20Sopenharmony_ci        257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
1868c2ecf20Sopenharmony_ci        8193, 12289, 16385, 24577};
1878c2ecf20Sopenharmony_cistatic const ush cpdext[] = {         /* Extra bits for distance codes */
1888c2ecf20Sopenharmony_ci        0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
1898c2ecf20Sopenharmony_ci        7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
1908c2ecf20Sopenharmony_ci        12, 12, 13, 13};
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci/* Macros for inflate() bit peeking and grabbing.
1958c2ecf20Sopenharmony_ci   The usage is:
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci        NEEDBITS(j)
1988c2ecf20Sopenharmony_ci        x = b & mask_bits[j];
1998c2ecf20Sopenharmony_ci        DUMPBITS(j)
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci   where NEEDBITS makes sure that b has at least j bits in it, and
2028c2ecf20Sopenharmony_ci   DUMPBITS removes the bits from b.  The macros use the variable k
2038c2ecf20Sopenharmony_ci   for the number of bits in b.  Normally, b and k are register
2048c2ecf20Sopenharmony_ci   variables for speed, and are initialized at the beginning of a
2058c2ecf20Sopenharmony_ci   routine that uses these macros from a global bit buffer and count.
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci   If we assume that EOB will be the longest code, then we will never
2088c2ecf20Sopenharmony_ci   ask for bits with NEEDBITS that are beyond the end of the stream.
2098c2ecf20Sopenharmony_ci   So, NEEDBITS should not read any more bytes than are needed to
2108c2ecf20Sopenharmony_ci   meet the request.  Then no bytes need to be "returned" to the buffer
2118c2ecf20Sopenharmony_ci   at the end of the last block.
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci   However, this assumption is not true for fixed blocks--the EOB code
2148c2ecf20Sopenharmony_ci   is 7 bits, but the other literal/length codes can be 8 or 9 bits.
2158c2ecf20Sopenharmony_ci   (The EOB code is shorter than other codes because fixed blocks are
2168c2ecf20Sopenharmony_ci   generally short.  So, while a block always has an EOB, many other
2178c2ecf20Sopenharmony_ci   literal/length codes have a significantly lower probability of
2188c2ecf20Sopenharmony_ci   showing up at all.)  However, by making the first table have a
2198c2ecf20Sopenharmony_ci   lookup of seven bits, the EOB code will be found in that first
2208c2ecf20Sopenharmony_ci   lookup, and so will not require that too many bits be pulled from
2218c2ecf20Sopenharmony_ci   the stream.
2228c2ecf20Sopenharmony_ci */
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ciSTATIC ulg bb;                         /* bit buffer */
2258c2ecf20Sopenharmony_ciSTATIC unsigned bk;                    /* bits in bit buffer */
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ciSTATIC const ush mask_bits[] = {
2288c2ecf20Sopenharmony_ci    0x0000,
2298c2ecf20Sopenharmony_ci    0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
2308c2ecf20Sopenharmony_ci    0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
2318c2ecf20Sopenharmony_ci};
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci#define NEXTBYTE()  ({ int v = get_byte(); if (v < 0) goto underrun; (uch)v; })
2348c2ecf20Sopenharmony_ci#define NEEDBITS(n) {while(k<(n)){b|=((ulg)NEXTBYTE())<<k;k+=8;}}
2358c2ecf20Sopenharmony_ci#define DUMPBITS(n) {b>>=(n);k-=(n);}
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci#ifndef NO_INFLATE_MALLOC
2388c2ecf20Sopenharmony_ci/* A trivial malloc implementation, adapted from
2398c2ecf20Sopenharmony_ci *  malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994
2408c2ecf20Sopenharmony_ci */
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_cistatic unsigned long malloc_ptr;
2438c2ecf20Sopenharmony_cistatic int malloc_count;
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_cistatic void *malloc(int size)
2468c2ecf20Sopenharmony_ci{
2478c2ecf20Sopenharmony_ci       void *p;
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci       if (size < 0)
2508c2ecf20Sopenharmony_ci		error("Malloc error");
2518c2ecf20Sopenharmony_ci       if (!malloc_ptr)
2528c2ecf20Sopenharmony_ci		malloc_ptr = free_mem_ptr;
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci       malloc_ptr = (malloc_ptr + 3) & ~3;     /* Align */
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci       p = (void *)malloc_ptr;
2578c2ecf20Sopenharmony_ci       malloc_ptr += size;
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci       if (free_mem_end_ptr && malloc_ptr >= free_mem_end_ptr)
2608c2ecf20Sopenharmony_ci		error("Out of memory");
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci       malloc_count++;
2638c2ecf20Sopenharmony_ci       return p;
2648c2ecf20Sopenharmony_ci}
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_cistatic void free(void *where)
2678c2ecf20Sopenharmony_ci{
2688c2ecf20Sopenharmony_ci       malloc_count--;
2698c2ecf20Sopenharmony_ci       if (!malloc_count)
2708c2ecf20Sopenharmony_ci		malloc_ptr = free_mem_ptr;
2718c2ecf20Sopenharmony_ci}
2728c2ecf20Sopenharmony_ci#else
2738c2ecf20Sopenharmony_ci#define malloc(a) kmalloc(a, GFP_KERNEL)
2748c2ecf20Sopenharmony_ci#define free(a) kfree(a)
2758c2ecf20Sopenharmony_ci#endif
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci/*
2788c2ecf20Sopenharmony_ci   Huffman code decoding is performed using a multi-level table lookup.
2798c2ecf20Sopenharmony_ci   The fastest way to decode is to simply build a lookup table whose
2808c2ecf20Sopenharmony_ci   size is determined by the longest code.  However, the time it takes
2818c2ecf20Sopenharmony_ci   to build this table can also be a factor if the data being decoded
2828c2ecf20Sopenharmony_ci   is not very long.  The most common codes are necessarily the
2838c2ecf20Sopenharmony_ci   shortest codes, so those codes dominate the decoding time, and hence
2848c2ecf20Sopenharmony_ci   the speed.  The idea is you can have a shorter table that decodes the
2858c2ecf20Sopenharmony_ci   shorter, more probable codes, and then point to subsidiary tables for
2868c2ecf20Sopenharmony_ci   the longer codes.  The time it costs to decode the longer codes is
2878c2ecf20Sopenharmony_ci   then traded against the time it takes to make longer tables.
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci   This results of this trade are in the variables lbits and dbits
2908c2ecf20Sopenharmony_ci   below.  lbits is the number of bits the first level table for literal/
2918c2ecf20Sopenharmony_ci   length codes can decode in one step, and dbits is the same thing for
2928c2ecf20Sopenharmony_ci   the distance codes.  Subsequent tables are also less than or equal to
2938c2ecf20Sopenharmony_ci   those sizes.  These values may be adjusted either when all of the
2948c2ecf20Sopenharmony_ci   codes are shorter than that, in which case the longest code length in
2958c2ecf20Sopenharmony_ci   bits is used, or when the shortest code is *longer* than the requested
2968c2ecf20Sopenharmony_ci   table size, in which case the length of the shortest code in bits is
2978c2ecf20Sopenharmony_ci   used.
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci   There are two different values for the two tables, since they code a
3008c2ecf20Sopenharmony_ci   different number of possibilities each.  The literal/length table
3018c2ecf20Sopenharmony_ci   codes 286 possible values, or in a flat code, a little over eight
3028c2ecf20Sopenharmony_ci   bits.  The distance table codes 30 possible values, or a little less
3038c2ecf20Sopenharmony_ci   than five bits, flat.  The optimum values for speed end up being
3048c2ecf20Sopenharmony_ci   about one bit more than those, so lbits is 8+1 and dbits is 5+1.
3058c2ecf20Sopenharmony_ci   The optimum values may differ though from machine to machine, and
3068c2ecf20Sopenharmony_ci   possibly even between compilers.  Your mileage may vary.
3078c2ecf20Sopenharmony_ci */
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ciSTATIC const int lbits = 9;          /* bits in base literal/length lookup table */
3118c2ecf20Sopenharmony_ciSTATIC const int dbits = 6;          /* bits in base distance lookup table */
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci/* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
3158c2ecf20Sopenharmony_ci#define BMAX 16         /* maximum bit length of any code (16 for explode) */
3168c2ecf20Sopenharmony_ci#define N_MAX 288       /* maximum number of codes in any set */
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ciSTATIC unsigned hufts;         /* track memory usage */
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ciSTATIC int INIT huft_build(
3238c2ecf20Sopenharmony_ci	unsigned *b,            /* code lengths in bits (all assumed <= BMAX) */
3248c2ecf20Sopenharmony_ci	unsigned n,             /* number of codes (assumed <= N_MAX) */
3258c2ecf20Sopenharmony_ci	unsigned s,             /* number of simple-valued codes (0..s-1) */
3268c2ecf20Sopenharmony_ci	const ush *d,           /* list of base values for non-simple codes */
3278c2ecf20Sopenharmony_ci	const ush *e,           /* list of extra bits for non-simple codes */
3288c2ecf20Sopenharmony_ci	struct huft **t,        /* result: starting table */
3298c2ecf20Sopenharmony_ci	int *m                  /* maximum lookup bits, returns actual */
3308c2ecf20Sopenharmony_ci	)
3318c2ecf20Sopenharmony_ci/* Given a list of code lengths and a maximum table size, make a set of
3328c2ecf20Sopenharmony_ci   tables to decode that set of codes.  Return zero on success, one if
3338c2ecf20Sopenharmony_ci   the given code set is incomplete (the tables are still built in this
3348c2ecf20Sopenharmony_ci   case), two if the input is invalid (all zero length codes or an
3358c2ecf20Sopenharmony_ci   oversubscribed set of lengths), and three if not enough memory. */
3368c2ecf20Sopenharmony_ci{
3378c2ecf20Sopenharmony_ci  unsigned a;                   /* counter for codes of length k */
3388c2ecf20Sopenharmony_ci  unsigned f;                   /* i repeats in table every f entries */
3398c2ecf20Sopenharmony_ci  int g;                        /* maximum code length */
3408c2ecf20Sopenharmony_ci  int h;                        /* table level */
3418c2ecf20Sopenharmony_ci  register unsigned i;          /* counter, current code */
3428c2ecf20Sopenharmony_ci  register unsigned j;          /* counter */
3438c2ecf20Sopenharmony_ci  register int k;               /* number of bits in current code */
3448c2ecf20Sopenharmony_ci  int l;                        /* bits per table (returned in m) */
3458c2ecf20Sopenharmony_ci  register unsigned *p;         /* pointer into c[], b[], or v[] */
3468c2ecf20Sopenharmony_ci  register struct huft *q;      /* points to current table */
3478c2ecf20Sopenharmony_ci  struct huft r;                /* table entry for structure assignment */
3488c2ecf20Sopenharmony_ci  register int w;               /* bits before this table == (l * h) */
3498c2ecf20Sopenharmony_ci  unsigned *xp;                 /* pointer into x */
3508c2ecf20Sopenharmony_ci  int y;                        /* number of dummy codes added */
3518c2ecf20Sopenharmony_ci  unsigned z;                   /* number of entries in current table */
3528c2ecf20Sopenharmony_ci  struct {
3538c2ecf20Sopenharmony_ci    unsigned c[BMAX+1];           /* bit length count table */
3548c2ecf20Sopenharmony_ci    struct huft *u[BMAX];         /* table stack */
3558c2ecf20Sopenharmony_ci    unsigned v[N_MAX];            /* values in order of bit length */
3568c2ecf20Sopenharmony_ci    unsigned x[BMAX+1];           /* bit offsets, then code stack */
3578c2ecf20Sopenharmony_ci  } *stk;
3588c2ecf20Sopenharmony_ci  unsigned *c, *v, *x;
3598c2ecf20Sopenharmony_ci  struct huft **u;
3608c2ecf20Sopenharmony_ci  int ret;
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ciDEBG("huft1 ");
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci  stk = malloc(sizeof(*stk));
3658c2ecf20Sopenharmony_ci  if (stk == NULL)
3668c2ecf20Sopenharmony_ci    return 3;			/* out of memory */
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci  c = stk->c;
3698c2ecf20Sopenharmony_ci  v = stk->v;
3708c2ecf20Sopenharmony_ci  x = stk->x;
3718c2ecf20Sopenharmony_ci  u = stk->u;
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci  /* Generate counts for each bit length */
3748c2ecf20Sopenharmony_ci  memzero(stk->c, sizeof(stk->c));
3758c2ecf20Sopenharmony_ci  p = b;  i = n;
3768c2ecf20Sopenharmony_ci  do {
3778c2ecf20Sopenharmony_ci    Tracecv(*p, (stderr, (n-i >= ' ' && n-i <= '~' ? "%c %d\n" : "0x%x %d\n"),
3788c2ecf20Sopenharmony_ci	    n-i, *p));
3798c2ecf20Sopenharmony_ci    c[*p]++;                    /* assume all entries <= BMAX */
3808c2ecf20Sopenharmony_ci    p++;                      /* Can't combine with above line (Solaris bug) */
3818c2ecf20Sopenharmony_ci  } while (--i);
3828c2ecf20Sopenharmony_ci  if (c[0] == n)                /* null input--all zero length codes */
3838c2ecf20Sopenharmony_ci  {
3848c2ecf20Sopenharmony_ci    *t = (struct huft *)NULL;
3858c2ecf20Sopenharmony_ci    *m = 0;
3868c2ecf20Sopenharmony_ci    ret = 2;
3878c2ecf20Sopenharmony_ci    goto out;
3888c2ecf20Sopenharmony_ci  }
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ciDEBG("huft2 ");
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci  /* Find minimum and maximum length, bound *m by those */
3938c2ecf20Sopenharmony_ci  l = *m;
3948c2ecf20Sopenharmony_ci  for (j = 1; j <= BMAX; j++)
3958c2ecf20Sopenharmony_ci    if (c[j])
3968c2ecf20Sopenharmony_ci      break;
3978c2ecf20Sopenharmony_ci  k = j;                        /* minimum code length */
3988c2ecf20Sopenharmony_ci  if ((unsigned)l < j)
3998c2ecf20Sopenharmony_ci    l = j;
4008c2ecf20Sopenharmony_ci  for (i = BMAX; i; i--)
4018c2ecf20Sopenharmony_ci    if (c[i])
4028c2ecf20Sopenharmony_ci      break;
4038c2ecf20Sopenharmony_ci  g = i;                        /* maximum code length */
4048c2ecf20Sopenharmony_ci  if ((unsigned)l > i)
4058c2ecf20Sopenharmony_ci    l = i;
4068c2ecf20Sopenharmony_ci  *m = l;
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ciDEBG("huft3 ");
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci  /* Adjust last length count to fill out codes, if needed */
4118c2ecf20Sopenharmony_ci  for (y = 1 << j; j < i; j++, y <<= 1)
4128c2ecf20Sopenharmony_ci    if ((y -= c[j]) < 0) {
4138c2ecf20Sopenharmony_ci      ret = 2;                 /* bad input: more codes than bits */
4148c2ecf20Sopenharmony_ci      goto out;
4158c2ecf20Sopenharmony_ci    }
4168c2ecf20Sopenharmony_ci  if ((y -= c[i]) < 0) {
4178c2ecf20Sopenharmony_ci    ret = 2;
4188c2ecf20Sopenharmony_ci    goto out;
4198c2ecf20Sopenharmony_ci  }
4208c2ecf20Sopenharmony_ci  c[i] += y;
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ciDEBG("huft4 ");
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ci  /* Generate starting offsets into the value table for each length */
4258c2ecf20Sopenharmony_ci  x[1] = j = 0;
4268c2ecf20Sopenharmony_ci  p = c + 1;  xp = x + 2;
4278c2ecf20Sopenharmony_ci  while (--i) {                 /* note that i == g from above */
4288c2ecf20Sopenharmony_ci    *xp++ = (j += *p++);
4298c2ecf20Sopenharmony_ci  }
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ciDEBG("huft5 ");
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci  /* Make a table of values in order of bit lengths */
4348c2ecf20Sopenharmony_ci  p = b;  i = 0;
4358c2ecf20Sopenharmony_ci  do {
4368c2ecf20Sopenharmony_ci    if ((j = *p++) != 0)
4378c2ecf20Sopenharmony_ci      v[x[j]++] = i;
4388c2ecf20Sopenharmony_ci  } while (++i < n);
4398c2ecf20Sopenharmony_ci  n = x[g];                   /* set n to length of v */
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ciDEBG("h6 ");
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci  /* Generate the Huffman codes and for each, make the table entries */
4448c2ecf20Sopenharmony_ci  x[0] = i = 0;                 /* first Huffman code is zero */
4458c2ecf20Sopenharmony_ci  p = v;                        /* grab values in bit order */
4468c2ecf20Sopenharmony_ci  h = -1;                       /* no tables yet--level -1 */
4478c2ecf20Sopenharmony_ci  w = -l;                       /* bits decoded == (l * h) */
4488c2ecf20Sopenharmony_ci  u[0] = (struct huft *)NULL;   /* just to keep compilers happy */
4498c2ecf20Sopenharmony_ci  q = (struct huft *)NULL;      /* ditto */
4508c2ecf20Sopenharmony_ci  z = 0;                        /* ditto */
4518c2ecf20Sopenharmony_ciDEBG("h6a ");
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ci  /* go through the bit lengths (k already is bits in shortest code) */
4548c2ecf20Sopenharmony_ci  for (; k <= g; k++)
4558c2ecf20Sopenharmony_ci  {
4568c2ecf20Sopenharmony_ciDEBG("h6b ");
4578c2ecf20Sopenharmony_ci    a = c[k];
4588c2ecf20Sopenharmony_ci    while (a--)
4598c2ecf20Sopenharmony_ci    {
4608c2ecf20Sopenharmony_ciDEBG("h6b1 ");
4618c2ecf20Sopenharmony_ci      /* here i is the Huffman code of length k bits for value *p */
4628c2ecf20Sopenharmony_ci      /* make tables up to required level */
4638c2ecf20Sopenharmony_ci      while (k > w + l)
4648c2ecf20Sopenharmony_ci      {
4658c2ecf20Sopenharmony_ciDEBG1("1 ");
4668c2ecf20Sopenharmony_ci        h++;
4678c2ecf20Sopenharmony_ci        w += l;                 /* previous table always l bits */
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci        /* compute minimum size table less than or equal to l bits */
4708c2ecf20Sopenharmony_ci        z = (z = g - w) > (unsigned)l ? l : z;  /* upper limit on table size */
4718c2ecf20Sopenharmony_ci        if ((f = 1 << (j = k - w)) > a + 1)     /* try a k-w bit table */
4728c2ecf20Sopenharmony_ci        {                       /* too few codes for k-w bit table */
4738c2ecf20Sopenharmony_ciDEBG1("2 ");
4748c2ecf20Sopenharmony_ci          f -= a + 1;           /* deduct codes from patterns left */
4758c2ecf20Sopenharmony_ci          xp = c + k;
4768c2ecf20Sopenharmony_ci          if (j < z)
4778c2ecf20Sopenharmony_ci            while (++j < z)       /* try smaller tables up to z bits */
4788c2ecf20Sopenharmony_ci            {
4798c2ecf20Sopenharmony_ci              if ((f <<= 1) <= *++xp)
4808c2ecf20Sopenharmony_ci                break;            /* enough codes to use up j bits */
4818c2ecf20Sopenharmony_ci              f -= *xp;           /* else deduct codes from patterns */
4828c2ecf20Sopenharmony_ci            }
4838c2ecf20Sopenharmony_ci        }
4848c2ecf20Sopenharmony_ciDEBG1("3 ");
4858c2ecf20Sopenharmony_ci        z = 1 << j;             /* table entries for j-bit table */
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci        /* allocate and link in new table */
4888c2ecf20Sopenharmony_ci        if ((q = (struct huft *)malloc((z + 1)*sizeof(struct huft))) ==
4898c2ecf20Sopenharmony_ci            (struct huft *)NULL)
4908c2ecf20Sopenharmony_ci        {
4918c2ecf20Sopenharmony_ci          if (h)
4928c2ecf20Sopenharmony_ci            huft_free(u[0]);
4938c2ecf20Sopenharmony_ci          ret = 3;             /* not enough memory */
4948c2ecf20Sopenharmony_ci	  goto out;
4958c2ecf20Sopenharmony_ci        }
4968c2ecf20Sopenharmony_ciDEBG1("4 ");
4978c2ecf20Sopenharmony_ci        hufts += z + 1;         /* track memory usage */
4988c2ecf20Sopenharmony_ci        *t = q + 1;             /* link to list for huft_free() */
4998c2ecf20Sopenharmony_ci        *(t = &(q->v.t)) = (struct huft *)NULL;
5008c2ecf20Sopenharmony_ci        u[h] = ++q;             /* table starts after link */
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_ciDEBG1("5 ");
5038c2ecf20Sopenharmony_ci        /* connect to last table, if there is one */
5048c2ecf20Sopenharmony_ci        if (h)
5058c2ecf20Sopenharmony_ci        {
5068c2ecf20Sopenharmony_ci          x[h] = i;             /* save pattern for backing up */
5078c2ecf20Sopenharmony_ci          r.b = (uch)l;         /* bits to dump before this table */
5088c2ecf20Sopenharmony_ci          r.e = (uch)(16 + j);  /* bits in this table */
5098c2ecf20Sopenharmony_ci          r.v.t = q;            /* pointer to this table */
5108c2ecf20Sopenharmony_ci          j = i >> (w - l);     /* (get around Turbo C bug) */
5118c2ecf20Sopenharmony_ci          u[h-1][j] = r;        /* connect to last table */
5128c2ecf20Sopenharmony_ci        }
5138c2ecf20Sopenharmony_ciDEBG1("6 ");
5148c2ecf20Sopenharmony_ci      }
5158c2ecf20Sopenharmony_ciDEBG("h6c ");
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_ci      /* set up table entry in r */
5188c2ecf20Sopenharmony_ci      r.b = (uch)(k - w);
5198c2ecf20Sopenharmony_ci      if (p >= v + n)
5208c2ecf20Sopenharmony_ci        r.e = 99;               /* out of values--invalid code */
5218c2ecf20Sopenharmony_ci      else if (*p < s)
5228c2ecf20Sopenharmony_ci      {
5238c2ecf20Sopenharmony_ci        r.e = (uch)(*p < 256 ? 16 : 15);    /* 256 is end-of-block code */
5248c2ecf20Sopenharmony_ci        r.v.n = (ush)(*p);             /* simple code is just the value */
5258c2ecf20Sopenharmony_ci	p++;                           /* one compiler does not like *p++ */
5268c2ecf20Sopenharmony_ci      }
5278c2ecf20Sopenharmony_ci      else
5288c2ecf20Sopenharmony_ci      {
5298c2ecf20Sopenharmony_ci        r.e = (uch)e[*p - s];   /* non-simple--look up in lists */
5308c2ecf20Sopenharmony_ci        r.v.n = d[*p++ - s];
5318c2ecf20Sopenharmony_ci      }
5328c2ecf20Sopenharmony_ciDEBG("h6d ");
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_ci      /* fill code-like entries with r */
5358c2ecf20Sopenharmony_ci      f = 1 << (k - w);
5368c2ecf20Sopenharmony_ci      for (j = i >> w; j < z; j += f)
5378c2ecf20Sopenharmony_ci        q[j] = r;
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_ci      /* backwards increment the k-bit code i */
5408c2ecf20Sopenharmony_ci      for (j = 1 << (k - 1); i & j; j >>= 1)
5418c2ecf20Sopenharmony_ci        i ^= j;
5428c2ecf20Sopenharmony_ci      i ^= j;
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_ci      /* backup over finished tables */
5458c2ecf20Sopenharmony_ci      while ((i & ((1 << w) - 1)) != x[h])
5468c2ecf20Sopenharmony_ci      {
5478c2ecf20Sopenharmony_ci        h--;                    /* don't need to update q */
5488c2ecf20Sopenharmony_ci        w -= l;
5498c2ecf20Sopenharmony_ci      }
5508c2ecf20Sopenharmony_ciDEBG("h6e ");
5518c2ecf20Sopenharmony_ci    }
5528c2ecf20Sopenharmony_ciDEBG("h6f ");
5538c2ecf20Sopenharmony_ci  }
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_ciDEBG("huft7 ");
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_ci  /* Return true (1) if we were given an incomplete table */
5588c2ecf20Sopenharmony_ci  ret = y != 0 && g != 1;
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci  out:
5618c2ecf20Sopenharmony_ci  free(stk);
5628c2ecf20Sopenharmony_ci  return ret;
5638c2ecf20Sopenharmony_ci}
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_ci
5678c2ecf20Sopenharmony_ciSTATIC int INIT huft_free(
5688c2ecf20Sopenharmony_ci	struct huft *t         /* table to free */
5698c2ecf20Sopenharmony_ci	)
5708c2ecf20Sopenharmony_ci/* Free the malloc'ed tables built by huft_build(), which makes a linked
5718c2ecf20Sopenharmony_ci   list of the tables it made, with the links in a dummy first entry of
5728c2ecf20Sopenharmony_ci   each table. */
5738c2ecf20Sopenharmony_ci{
5748c2ecf20Sopenharmony_ci  register struct huft *p, *q;
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_ci
5778c2ecf20Sopenharmony_ci  /* Go through linked list, freeing from the malloced (t[-1]) address. */
5788c2ecf20Sopenharmony_ci  p = t;
5798c2ecf20Sopenharmony_ci  while (p != (struct huft *)NULL)
5808c2ecf20Sopenharmony_ci  {
5818c2ecf20Sopenharmony_ci    q = (--p)->v.t;
5828c2ecf20Sopenharmony_ci    free((char*)p);
5838c2ecf20Sopenharmony_ci    p = q;
5848c2ecf20Sopenharmony_ci  }
5858c2ecf20Sopenharmony_ci  return 0;
5868c2ecf20Sopenharmony_ci}
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_ciSTATIC int INIT inflate_codes(
5908c2ecf20Sopenharmony_ci	struct huft *tl,    /* literal/length decoder tables */
5918c2ecf20Sopenharmony_ci	struct huft *td,    /* distance decoder tables */
5928c2ecf20Sopenharmony_ci	int bl,             /* number of bits decoded by tl[] */
5938c2ecf20Sopenharmony_ci	int bd              /* number of bits decoded by td[] */
5948c2ecf20Sopenharmony_ci	)
5958c2ecf20Sopenharmony_ci/* inflate (decompress) the codes in a deflated (compressed) block.
5968c2ecf20Sopenharmony_ci   Return an error code or zero if it all goes ok. */
5978c2ecf20Sopenharmony_ci{
5988c2ecf20Sopenharmony_ci  register unsigned e;  /* table entry flag/number of extra bits */
5998c2ecf20Sopenharmony_ci  unsigned n, d;        /* length and index for copy */
6008c2ecf20Sopenharmony_ci  unsigned w;           /* current window position */
6018c2ecf20Sopenharmony_ci  struct huft *t;       /* pointer to table entry */
6028c2ecf20Sopenharmony_ci  unsigned ml, md;      /* masks for bl and bd bits */
6038c2ecf20Sopenharmony_ci  register ulg b;       /* bit buffer */
6048c2ecf20Sopenharmony_ci  register unsigned k;  /* number of bits in bit buffer */
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_ci
6078c2ecf20Sopenharmony_ci  /* make local copies of globals */
6088c2ecf20Sopenharmony_ci  b = bb;                       /* initialize bit buffer */
6098c2ecf20Sopenharmony_ci  k = bk;
6108c2ecf20Sopenharmony_ci  w = wp;                       /* initialize window position */
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_ci  /* inflate the coded data */
6138c2ecf20Sopenharmony_ci  ml = mask_bits[bl];           /* precompute masks for speed */
6148c2ecf20Sopenharmony_ci  md = mask_bits[bd];
6158c2ecf20Sopenharmony_ci  for (;;)                      /* do until end of block */
6168c2ecf20Sopenharmony_ci  {
6178c2ecf20Sopenharmony_ci    NEEDBITS((unsigned)bl)
6188c2ecf20Sopenharmony_ci    if ((e = (t = tl + ((unsigned)b & ml))->e) > 16)
6198c2ecf20Sopenharmony_ci      do {
6208c2ecf20Sopenharmony_ci        if (e == 99)
6218c2ecf20Sopenharmony_ci          return 1;
6228c2ecf20Sopenharmony_ci        DUMPBITS(t->b)
6238c2ecf20Sopenharmony_ci        e -= 16;
6248c2ecf20Sopenharmony_ci        NEEDBITS(e)
6258c2ecf20Sopenharmony_ci      } while ((e = (t = t->v.t + ((unsigned)b & mask_bits[e]))->e) > 16);
6268c2ecf20Sopenharmony_ci    DUMPBITS(t->b)
6278c2ecf20Sopenharmony_ci    if (e == 16)                /* then it's a literal */
6288c2ecf20Sopenharmony_ci    {
6298c2ecf20Sopenharmony_ci      slide[w++] = (uch)t->v.n;
6308c2ecf20Sopenharmony_ci      Tracevv((stderr, "%c", slide[w-1]));
6318c2ecf20Sopenharmony_ci      if (w == WSIZE)
6328c2ecf20Sopenharmony_ci      {
6338c2ecf20Sopenharmony_ci        flush_output(w);
6348c2ecf20Sopenharmony_ci        w = 0;
6358c2ecf20Sopenharmony_ci      }
6368c2ecf20Sopenharmony_ci    }
6378c2ecf20Sopenharmony_ci    else                        /* it's an EOB or a length */
6388c2ecf20Sopenharmony_ci    {
6398c2ecf20Sopenharmony_ci      /* exit if end of block */
6408c2ecf20Sopenharmony_ci      if (e == 15)
6418c2ecf20Sopenharmony_ci        break;
6428c2ecf20Sopenharmony_ci
6438c2ecf20Sopenharmony_ci      /* get length of block to copy */
6448c2ecf20Sopenharmony_ci      NEEDBITS(e)
6458c2ecf20Sopenharmony_ci      n = t->v.n + ((unsigned)b & mask_bits[e]);
6468c2ecf20Sopenharmony_ci      DUMPBITS(e);
6478c2ecf20Sopenharmony_ci
6488c2ecf20Sopenharmony_ci      /* decode distance of block to copy */
6498c2ecf20Sopenharmony_ci      NEEDBITS((unsigned)bd)
6508c2ecf20Sopenharmony_ci      if ((e = (t = td + ((unsigned)b & md))->e) > 16)
6518c2ecf20Sopenharmony_ci        do {
6528c2ecf20Sopenharmony_ci          if (e == 99)
6538c2ecf20Sopenharmony_ci            return 1;
6548c2ecf20Sopenharmony_ci          DUMPBITS(t->b)
6558c2ecf20Sopenharmony_ci          e -= 16;
6568c2ecf20Sopenharmony_ci          NEEDBITS(e)
6578c2ecf20Sopenharmony_ci        } while ((e = (t = t->v.t + ((unsigned)b & mask_bits[e]))->e) > 16);
6588c2ecf20Sopenharmony_ci      DUMPBITS(t->b)
6598c2ecf20Sopenharmony_ci      NEEDBITS(e)
6608c2ecf20Sopenharmony_ci      d = w - t->v.n - ((unsigned)b & mask_bits[e]);
6618c2ecf20Sopenharmony_ci      DUMPBITS(e)
6628c2ecf20Sopenharmony_ci      Tracevv((stderr,"\\[%d,%d]", w-d, n));
6638c2ecf20Sopenharmony_ci
6648c2ecf20Sopenharmony_ci      /* do the copy */
6658c2ecf20Sopenharmony_ci      do {
6668c2ecf20Sopenharmony_ci        n -= (e = (e = WSIZE - ((d &= WSIZE-1) > w ? d : w)) > n ? n : e);
6678c2ecf20Sopenharmony_ci#if !defined(NOMEMCPY) && !defined(DEBUG)
6688c2ecf20Sopenharmony_ci        if (w - d >= e)         /* (this test assumes unsigned comparison) */
6698c2ecf20Sopenharmony_ci        {
6708c2ecf20Sopenharmony_ci          memcpy(slide + w, slide + d, e);
6718c2ecf20Sopenharmony_ci          w += e;
6728c2ecf20Sopenharmony_ci          d += e;
6738c2ecf20Sopenharmony_ci        }
6748c2ecf20Sopenharmony_ci        else                      /* do it slow to avoid memcpy() overlap */
6758c2ecf20Sopenharmony_ci#endif /* !NOMEMCPY */
6768c2ecf20Sopenharmony_ci          do {
6778c2ecf20Sopenharmony_ci            slide[w++] = slide[d++];
6788c2ecf20Sopenharmony_ci	    Tracevv((stderr, "%c", slide[w-1]));
6798c2ecf20Sopenharmony_ci          } while (--e);
6808c2ecf20Sopenharmony_ci        if (w == WSIZE)
6818c2ecf20Sopenharmony_ci        {
6828c2ecf20Sopenharmony_ci          flush_output(w);
6838c2ecf20Sopenharmony_ci          w = 0;
6848c2ecf20Sopenharmony_ci        }
6858c2ecf20Sopenharmony_ci      } while (n);
6868c2ecf20Sopenharmony_ci    }
6878c2ecf20Sopenharmony_ci  }
6888c2ecf20Sopenharmony_ci
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_ci  /* restore the globals from the locals */
6918c2ecf20Sopenharmony_ci  wp = w;                       /* restore global window pointer */
6928c2ecf20Sopenharmony_ci  bb = b;                       /* restore global bit buffer */
6938c2ecf20Sopenharmony_ci  bk = k;
6948c2ecf20Sopenharmony_ci
6958c2ecf20Sopenharmony_ci  /* done */
6968c2ecf20Sopenharmony_ci  return 0;
6978c2ecf20Sopenharmony_ci
6988c2ecf20Sopenharmony_ci underrun:
6998c2ecf20Sopenharmony_ci  return 4;			/* Input underrun */
7008c2ecf20Sopenharmony_ci}
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_ci
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_ciSTATIC int INIT inflate_stored(void)
7058c2ecf20Sopenharmony_ci/* "decompress" an inflated type 0 (stored) block. */
7068c2ecf20Sopenharmony_ci{
7078c2ecf20Sopenharmony_ci  unsigned n;           /* number of bytes in block */
7088c2ecf20Sopenharmony_ci  unsigned w;           /* current window position */
7098c2ecf20Sopenharmony_ci  register ulg b;       /* bit buffer */
7108c2ecf20Sopenharmony_ci  register unsigned k;  /* number of bits in bit buffer */
7118c2ecf20Sopenharmony_ci
7128c2ecf20Sopenharmony_ciDEBG("<stor");
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_ci  /* make local copies of globals */
7158c2ecf20Sopenharmony_ci  b = bb;                       /* initialize bit buffer */
7168c2ecf20Sopenharmony_ci  k = bk;
7178c2ecf20Sopenharmony_ci  w = wp;                       /* initialize window position */
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_ci
7208c2ecf20Sopenharmony_ci  /* go to byte boundary */
7218c2ecf20Sopenharmony_ci  n = k & 7;
7228c2ecf20Sopenharmony_ci  DUMPBITS(n);
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_ci
7258c2ecf20Sopenharmony_ci  /* get the length and its complement */
7268c2ecf20Sopenharmony_ci  NEEDBITS(16)
7278c2ecf20Sopenharmony_ci  n = ((unsigned)b & 0xffff);
7288c2ecf20Sopenharmony_ci  DUMPBITS(16)
7298c2ecf20Sopenharmony_ci  NEEDBITS(16)
7308c2ecf20Sopenharmony_ci  if (n != (unsigned)((~b) & 0xffff))
7318c2ecf20Sopenharmony_ci    return 1;                   /* error in compressed data */
7328c2ecf20Sopenharmony_ci  DUMPBITS(16)
7338c2ecf20Sopenharmony_ci
7348c2ecf20Sopenharmony_ci
7358c2ecf20Sopenharmony_ci  /* read and output the compressed data */
7368c2ecf20Sopenharmony_ci  while (n--)
7378c2ecf20Sopenharmony_ci  {
7388c2ecf20Sopenharmony_ci    NEEDBITS(8)
7398c2ecf20Sopenharmony_ci    slide[w++] = (uch)b;
7408c2ecf20Sopenharmony_ci    if (w == WSIZE)
7418c2ecf20Sopenharmony_ci    {
7428c2ecf20Sopenharmony_ci      flush_output(w);
7438c2ecf20Sopenharmony_ci      w = 0;
7448c2ecf20Sopenharmony_ci    }
7458c2ecf20Sopenharmony_ci    DUMPBITS(8)
7468c2ecf20Sopenharmony_ci  }
7478c2ecf20Sopenharmony_ci
7488c2ecf20Sopenharmony_ci
7498c2ecf20Sopenharmony_ci  /* restore the globals from the locals */
7508c2ecf20Sopenharmony_ci  wp = w;                       /* restore global window pointer */
7518c2ecf20Sopenharmony_ci  bb = b;                       /* restore global bit buffer */
7528c2ecf20Sopenharmony_ci  bk = k;
7538c2ecf20Sopenharmony_ci
7548c2ecf20Sopenharmony_ci  DEBG(">");
7558c2ecf20Sopenharmony_ci  return 0;
7568c2ecf20Sopenharmony_ci
7578c2ecf20Sopenharmony_ci underrun:
7588c2ecf20Sopenharmony_ci  return 4;			/* Input underrun */
7598c2ecf20Sopenharmony_ci}
7608c2ecf20Sopenharmony_ci
7618c2ecf20Sopenharmony_ci
7628c2ecf20Sopenharmony_ci/*
7638c2ecf20Sopenharmony_ci * We use `noinline' here to prevent gcc-3.5 from using too much stack space
7648c2ecf20Sopenharmony_ci */
7658c2ecf20Sopenharmony_ciSTATIC int noinline INIT inflate_fixed(void)
7668c2ecf20Sopenharmony_ci/* decompress an inflated type 1 (fixed Huffman codes) block.  We should
7678c2ecf20Sopenharmony_ci   either replace this with a custom decoder, or at least precompute the
7688c2ecf20Sopenharmony_ci   Huffman tables. */
7698c2ecf20Sopenharmony_ci{
7708c2ecf20Sopenharmony_ci  int i;                /* temporary variable */
7718c2ecf20Sopenharmony_ci  struct huft *tl;      /* literal/length code table */
7728c2ecf20Sopenharmony_ci  struct huft *td;      /* distance code table */
7738c2ecf20Sopenharmony_ci  int bl;               /* lookup bits for tl */
7748c2ecf20Sopenharmony_ci  int bd;               /* lookup bits for td */
7758c2ecf20Sopenharmony_ci  unsigned *l;          /* length list for huft_build */
7768c2ecf20Sopenharmony_ci
7778c2ecf20Sopenharmony_ciDEBG("<fix");
7788c2ecf20Sopenharmony_ci
7798c2ecf20Sopenharmony_ci  l = malloc(sizeof(*l) * 288);
7808c2ecf20Sopenharmony_ci  if (l == NULL)
7818c2ecf20Sopenharmony_ci    return 3;			/* out of memory */
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_ci  /* set up literal table */
7848c2ecf20Sopenharmony_ci  for (i = 0; i < 144; i++)
7858c2ecf20Sopenharmony_ci    l[i] = 8;
7868c2ecf20Sopenharmony_ci  for (; i < 256; i++)
7878c2ecf20Sopenharmony_ci    l[i] = 9;
7888c2ecf20Sopenharmony_ci  for (; i < 280; i++)
7898c2ecf20Sopenharmony_ci    l[i] = 7;
7908c2ecf20Sopenharmony_ci  for (; i < 288; i++)          /* make a complete, but wrong code set */
7918c2ecf20Sopenharmony_ci    l[i] = 8;
7928c2ecf20Sopenharmony_ci  bl = 7;
7938c2ecf20Sopenharmony_ci  if ((i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl)) != 0) {
7948c2ecf20Sopenharmony_ci    free(l);
7958c2ecf20Sopenharmony_ci    return i;
7968c2ecf20Sopenharmony_ci  }
7978c2ecf20Sopenharmony_ci
7988c2ecf20Sopenharmony_ci  /* set up distance table */
7998c2ecf20Sopenharmony_ci  for (i = 0; i < 30; i++)      /* make an incomplete code set */
8008c2ecf20Sopenharmony_ci    l[i] = 5;
8018c2ecf20Sopenharmony_ci  bd = 5;
8028c2ecf20Sopenharmony_ci  if ((i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd)) > 1)
8038c2ecf20Sopenharmony_ci  {
8048c2ecf20Sopenharmony_ci    huft_free(tl);
8058c2ecf20Sopenharmony_ci    free(l);
8068c2ecf20Sopenharmony_ci
8078c2ecf20Sopenharmony_ci    DEBG(">");
8088c2ecf20Sopenharmony_ci    return i;
8098c2ecf20Sopenharmony_ci  }
8108c2ecf20Sopenharmony_ci
8118c2ecf20Sopenharmony_ci
8128c2ecf20Sopenharmony_ci  /* decompress until an end-of-block code */
8138c2ecf20Sopenharmony_ci  if (inflate_codes(tl, td, bl, bd)) {
8148c2ecf20Sopenharmony_ci    free(l);
8158c2ecf20Sopenharmony_ci    return 1;
8168c2ecf20Sopenharmony_ci  }
8178c2ecf20Sopenharmony_ci
8188c2ecf20Sopenharmony_ci  /* free the decoding tables, return */
8198c2ecf20Sopenharmony_ci  free(l);
8208c2ecf20Sopenharmony_ci  huft_free(tl);
8218c2ecf20Sopenharmony_ci  huft_free(td);
8228c2ecf20Sopenharmony_ci  return 0;
8238c2ecf20Sopenharmony_ci}
8248c2ecf20Sopenharmony_ci
8258c2ecf20Sopenharmony_ci
8268c2ecf20Sopenharmony_ci/*
8278c2ecf20Sopenharmony_ci * We use `noinline' here to prevent gcc-3.5 from using too much stack space
8288c2ecf20Sopenharmony_ci */
8298c2ecf20Sopenharmony_ciSTATIC int noinline INIT inflate_dynamic(void)
8308c2ecf20Sopenharmony_ci/* decompress an inflated type 2 (dynamic Huffman codes) block. */
8318c2ecf20Sopenharmony_ci{
8328c2ecf20Sopenharmony_ci  int i;                /* temporary variables */
8338c2ecf20Sopenharmony_ci  unsigned j;
8348c2ecf20Sopenharmony_ci  unsigned l;           /* last length */
8358c2ecf20Sopenharmony_ci  unsigned m;           /* mask for bit lengths table */
8368c2ecf20Sopenharmony_ci  unsigned n;           /* number of lengths to get */
8378c2ecf20Sopenharmony_ci  struct huft *tl;      /* literal/length code table */
8388c2ecf20Sopenharmony_ci  struct huft *td;      /* distance code table */
8398c2ecf20Sopenharmony_ci  int bl;               /* lookup bits for tl */
8408c2ecf20Sopenharmony_ci  int bd;               /* lookup bits for td */
8418c2ecf20Sopenharmony_ci  unsigned nb;          /* number of bit length codes */
8428c2ecf20Sopenharmony_ci  unsigned nl;          /* number of literal/length codes */
8438c2ecf20Sopenharmony_ci  unsigned nd;          /* number of distance codes */
8448c2ecf20Sopenharmony_ci  unsigned *ll;         /* literal/length and distance code lengths */
8458c2ecf20Sopenharmony_ci  register ulg b;       /* bit buffer */
8468c2ecf20Sopenharmony_ci  register unsigned k;  /* number of bits in bit buffer */
8478c2ecf20Sopenharmony_ci  int ret;
8488c2ecf20Sopenharmony_ci
8498c2ecf20Sopenharmony_ciDEBG("<dyn");
8508c2ecf20Sopenharmony_ci
8518c2ecf20Sopenharmony_ci#ifdef PKZIP_BUG_WORKAROUND
8528c2ecf20Sopenharmony_ci  ll = malloc(sizeof(*ll) * (288+32));  /* literal/length and distance code lengths */
8538c2ecf20Sopenharmony_ci#else
8548c2ecf20Sopenharmony_ci  ll = malloc(sizeof(*ll) * (286+30));  /* literal/length and distance code lengths */
8558c2ecf20Sopenharmony_ci#endif
8568c2ecf20Sopenharmony_ci
8578c2ecf20Sopenharmony_ci  if (ll == NULL)
8588c2ecf20Sopenharmony_ci    return 1;
8598c2ecf20Sopenharmony_ci
8608c2ecf20Sopenharmony_ci  /* make local bit buffer */
8618c2ecf20Sopenharmony_ci  b = bb;
8628c2ecf20Sopenharmony_ci  k = bk;
8638c2ecf20Sopenharmony_ci
8648c2ecf20Sopenharmony_ci
8658c2ecf20Sopenharmony_ci  /* read in table lengths */
8668c2ecf20Sopenharmony_ci  NEEDBITS(5)
8678c2ecf20Sopenharmony_ci  nl = 257 + ((unsigned)b & 0x1f);      /* number of literal/length codes */
8688c2ecf20Sopenharmony_ci  DUMPBITS(5)
8698c2ecf20Sopenharmony_ci  NEEDBITS(5)
8708c2ecf20Sopenharmony_ci  nd = 1 + ((unsigned)b & 0x1f);        /* number of distance codes */
8718c2ecf20Sopenharmony_ci  DUMPBITS(5)
8728c2ecf20Sopenharmony_ci  NEEDBITS(4)
8738c2ecf20Sopenharmony_ci  nb = 4 + ((unsigned)b & 0xf);         /* number of bit length codes */
8748c2ecf20Sopenharmony_ci  DUMPBITS(4)
8758c2ecf20Sopenharmony_ci#ifdef PKZIP_BUG_WORKAROUND
8768c2ecf20Sopenharmony_ci  if (nl > 288 || nd > 32)
8778c2ecf20Sopenharmony_ci#else
8788c2ecf20Sopenharmony_ci  if (nl > 286 || nd > 30)
8798c2ecf20Sopenharmony_ci#endif
8808c2ecf20Sopenharmony_ci  {
8818c2ecf20Sopenharmony_ci    ret = 1;             /* bad lengths */
8828c2ecf20Sopenharmony_ci    goto out;
8838c2ecf20Sopenharmony_ci  }
8848c2ecf20Sopenharmony_ci
8858c2ecf20Sopenharmony_ciDEBG("dyn1 ");
8868c2ecf20Sopenharmony_ci
8878c2ecf20Sopenharmony_ci  /* read in bit-length-code lengths */
8888c2ecf20Sopenharmony_ci  for (j = 0; j < nb; j++)
8898c2ecf20Sopenharmony_ci  {
8908c2ecf20Sopenharmony_ci    NEEDBITS(3)
8918c2ecf20Sopenharmony_ci    ll[border[j]] = (unsigned)b & 7;
8928c2ecf20Sopenharmony_ci    DUMPBITS(3)
8938c2ecf20Sopenharmony_ci  }
8948c2ecf20Sopenharmony_ci  for (; j < 19; j++)
8958c2ecf20Sopenharmony_ci    ll[border[j]] = 0;
8968c2ecf20Sopenharmony_ci
8978c2ecf20Sopenharmony_ciDEBG("dyn2 ");
8988c2ecf20Sopenharmony_ci
8998c2ecf20Sopenharmony_ci  /* build decoding table for trees--single level, 7 bit lookup */
9008c2ecf20Sopenharmony_ci  bl = 7;
9018c2ecf20Sopenharmony_ci  if ((i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl)) != 0)
9028c2ecf20Sopenharmony_ci  {
9038c2ecf20Sopenharmony_ci    if (i == 1)
9048c2ecf20Sopenharmony_ci      huft_free(tl);
9058c2ecf20Sopenharmony_ci    ret = i;                   /* incomplete code set */
9068c2ecf20Sopenharmony_ci    goto out;
9078c2ecf20Sopenharmony_ci  }
9088c2ecf20Sopenharmony_ci
9098c2ecf20Sopenharmony_ciDEBG("dyn3 ");
9108c2ecf20Sopenharmony_ci
9118c2ecf20Sopenharmony_ci  /* read in literal and distance code lengths */
9128c2ecf20Sopenharmony_ci  n = nl + nd;
9138c2ecf20Sopenharmony_ci  m = mask_bits[bl];
9148c2ecf20Sopenharmony_ci  i = l = 0;
9158c2ecf20Sopenharmony_ci  while ((unsigned)i < n)
9168c2ecf20Sopenharmony_ci  {
9178c2ecf20Sopenharmony_ci    NEEDBITS((unsigned)bl)
9188c2ecf20Sopenharmony_ci    j = (td = tl + ((unsigned)b & m))->b;
9198c2ecf20Sopenharmony_ci    DUMPBITS(j)
9208c2ecf20Sopenharmony_ci    j = td->v.n;
9218c2ecf20Sopenharmony_ci    if (j < 16)                 /* length of code in bits (0..15) */
9228c2ecf20Sopenharmony_ci      ll[i++] = l = j;          /* save last length in l */
9238c2ecf20Sopenharmony_ci    else if (j == 16)           /* repeat last length 3 to 6 times */
9248c2ecf20Sopenharmony_ci    {
9258c2ecf20Sopenharmony_ci      NEEDBITS(2)
9268c2ecf20Sopenharmony_ci      j = 3 + ((unsigned)b & 3);
9278c2ecf20Sopenharmony_ci      DUMPBITS(2)
9288c2ecf20Sopenharmony_ci      if ((unsigned)i + j > n) {
9298c2ecf20Sopenharmony_ci        ret = 1;
9308c2ecf20Sopenharmony_ci	goto out;
9318c2ecf20Sopenharmony_ci      }
9328c2ecf20Sopenharmony_ci      while (j--)
9338c2ecf20Sopenharmony_ci        ll[i++] = l;
9348c2ecf20Sopenharmony_ci    }
9358c2ecf20Sopenharmony_ci    else if (j == 17)           /* 3 to 10 zero length codes */
9368c2ecf20Sopenharmony_ci    {
9378c2ecf20Sopenharmony_ci      NEEDBITS(3)
9388c2ecf20Sopenharmony_ci      j = 3 + ((unsigned)b & 7);
9398c2ecf20Sopenharmony_ci      DUMPBITS(3)
9408c2ecf20Sopenharmony_ci      if ((unsigned)i + j > n) {
9418c2ecf20Sopenharmony_ci        ret = 1;
9428c2ecf20Sopenharmony_ci	goto out;
9438c2ecf20Sopenharmony_ci      }
9448c2ecf20Sopenharmony_ci      while (j--)
9458c2ecf20Sopenharmony_ci        ll[i++] = 0;
9468c2ecf20Sopenharmony_ci      l = 0;
9478c2ecf20Sopenharmony_ci    }
9488c2ecf20Sopenharmony_ci    else                        /* j == 18: 11 to 138 zero length codes */
9498c2ecf20Sopenharmony_ci    {
9508c2ecf20Sopenharmony_ci      NEEDBITS(7)
9518c2ecf20Sopenharmony_ci      j = 11 + ((unsigned)b & 0x7f);
9528c2ecf20Sopenharmony_ci      DUMPBITS(7)
9538c2ecf20Sopenharmony_ci      if ((unsigned)i + j > n) {
9548c2ecf20Sopenharmony_ci        ret = 1;
9558c2ecf20Sopenharmony_ci	goto out;
9568c2ecf20Sopenharmony_ci      }
9578c2ecf20Sopenharmony_ci      while (j--)
9588c2ecf20Sopenharmony_ci        ll[i++] = 0;
9598c2ecf20Sopenharmony_ci      l = 0;
9608c2ecf20Sopenharmony_ci    }
9618c2ecf20Sopenharmony_ci  }
9628c2ecf20Sopenharmony_ci
9638c2ecf20Sopenharmony_ciDEBG("dyn4 ");
9648c2ecf20Sopenharmony_ci
9658c2ecf20Sopenharmony_ci  /* free decoding table for trees */
9668c2ecf20Sopenharmony_ci  huft_free(tl);
9678c2ecf20Sopenharmony_ci
9688c2ecf20Sopenharmony_ciDEBG("dyn5 ");
9698c2ecf20Sopenharmony_ci
9708c2ecf20Sopenharmony_ci  /* restore the global bit buffer */
9718c2ecf20Sopenharmony_ci  bb = b;
9728c2ecf20Sopenharmony_ci  bk = k;
9738c2ecf20Sopenharmony_ci
9748c2ecf20Sopenharmony_ciDEBG("dyn5a ");
9758c2ecf20Sopenharmony_ci
9768c2ecf20Sopenharmony_ci  /* build the decoding tables for literal/length and distance codes */
9778c2ecf20Sopenharmony_ci  bl = lbits;
9788c2ecf20Sopenharmony_ci  if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0)
9798c2ecf20Sopenharmony_ci  {
9808c2ecf20Sopenharmony_ciDEBG("dyn5b ");
9818c2ecf20Sopenharmony_ci    if (i == 1) {
9828c2ecf20Sopenharmony_ci      error("incomplete literal tree");
9838c2ecf20Sopenharmony_ci      huft_free(tl);
9848c2ecf20Sopenharmony_ci    }
9858c2ecf20Sopenharmony_ci    ret = i;                   /* incomplete code set */
9868c2ecf20Sopenharmony_ci    goto out;
9878c2ecf20Sopenharmony_ci  }
9888c2ecf20Sopenharmony_ciDEBG("dyn5c ");
9898c2ecf20Sopenharmony_ci  bd = dbits;
9908c2ecf20Sopenharmony_ci  if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0)
9918c2ecf20Sopenharmony_ci  {
9928c2ecf20Sopenharmony_ciDEBG("dyn5d ");
9938c2ecf20Sopenharmony_ci    if (i == 1) {
9948c2ecf20Sopenharmony_ci      error("incomplete distance tree");
9958c2ecf20Sopenharmony_ci#ifdef PKZIP_BUG_WORKAROUND
9968c2ecf20Sopenharmony_ci      i = 0;
9978c2ecf20Sopenharmony_ci    }
9988c2ecf20Sopenharmony_ci#else
9998c2ecf20Sopenharmony_ci      huft_free(td);
10008c2ecf20Sopenharmony_ci    }
10018c2ecf20Sopenharmony_ci    huft_free(tl);
10028c2ecf20Sopenharmony_ci    ret = i;                   /* incomplete code set */
10038c2ecf20Sopenharmony_ci    goto out;
10048c2ecf20Sopenharmony_ci#endif
10058c2ecf20Sopenharmony_ci  }
10068c2ecf20Sopenharmony_ci
10078c2ecf20Sopenharmony_ciDEBG("dyn6 ");
10088c2ecf20Sopenharmony_ci
10098c2ecf20Sopenharmony_ci  /* decompress until an end-of-block code */
10108c2ecf20Sopenharmony_ci  if (inflate_codes(tl, td, bl, bd)) {
10118c2ecf20Sopenharmony_ci    ret = 1;
10128c2ecf20Sopenharmony_ci    goto out;
10138c2ecf20Sopenharmony_ci  }
10148c2ecf20Sopenharmony_ci
10158c2ecf20Sopenharmony_ciDEBG("dyn7 ");
10168c2ecf20Sopenharmony_ci
10178c2ecf20Sopenharmony_ci  /* free the decoding tables, return */
10188c2ecf20Sopenharmony_ci  huft_free(tl);
10198c2ecf20Sopenharmony_ci  huft_free(td);
10208c2ecf20Sopenharmony_ci
10218c2ecf20Sopenharmony_ci  DEBG(">");
10228c2ecf20Sopenharmony_ci  ret = 0;
10238c2ecf20Sopenharmony_ciout:
10248c2ecf20Sopenharmony_ci  free(ll);
10258c2ecf20Sopenharmony_ci  return ret;
10268c2ecf20Sopenharmony_ci
10278c2ecf20Sopenharmony_ciunderrun:
10288c2ecf20Sopenharmony_ci  ret = 4;			/* Input underrun */
10298c2ecf20Sopenharmony_ci  goto out;
10308c2ecf20Sopenharmony_ci}
10318c2ecf20Sopenharmony_ci
10328c2ecf20Sopenharmony_ci
10338c2ecf20Sopenharmony_ci
10348c2ecf20Sopenharmony_ciSTATIC int INIT inflate_block(
10358c2ecf20Sopenharmony_ci	int *e                  /* last block flag */
10368c2ecf20Sopenharmony_ci	)
10378c2ecf20Sopenharmony_ci/* decompress an inflated block */
10388c2ecf20Sopenharmony_ci{
10398c2ecf20Sopenharmony_ci  unsigned t;           /* block type */
10408c2ecf20Sopenharmony_ci  register ulg b;       /* bit buffer */
10418c2ecf20Sopenharmony_ci  register unsigned k;  /* number of bits in bit buffer */
10428c2ecf20Sopenharmony_ci
10438c2ecf20Sopenharmony_ci  DEBG("<blk");
10448c2ecf20Sopenharmony_ci
10458c2ecf20Sopenharmony_ci  /* make local bit buffer */
10468c2ecf20Sopenharmony_ci  b = bb;
10478c2ecf20Sopenharmony_ci  k = bk;
10488c2ecf20Sopenharmony_ci
10498c2ecf20Sopenharmony_ci
10508c2ecf20Sopenharmony_ci  /* read in last block bit */
10518c2ecf20Sopenharmony_ci  NEEDBITS(1)
10528c2ecf20Sopenharmony_ci  *e = (int)b & 1;
10538c2ecf20Sopenharmony_ci  DUMPBITS(1)
10548c2ecf20Sopenharmony_ci
10558c2ecf20Sopenharmony_ci
10568c2ecf20Sopenharmony_ci  /* read in block type */
10578c2ecf20Sopenharmony_ci  NEEDBITS(2)
10588c2ecf20Sopenharmony_ci  t = (unsigned)b & 3;
10598c2ecf20Sopenharmony_ci  DUMPBITS(2)
10608c2ecf20Sopenharmony_ci
10618c2ecf20Sopenharmony_ci
10628c2ecf20Sopenharmony_ci  /* restore the global bit buffer */
10638c2ecf20Sopenharmony_ci  bb = b;
10648c2ecf20Sopenharmony_ci  bk = k;
10658c2ecf20Sopenharmony_ci
10668c2ecf20Sopenharmony_ci  /* inflate that block type */
10678c2ecf20Sopenharmony_ci  if (t == 2)
10688c2ecf20Sopenharmony_ci    return inflate_dynamic();
10698c2ecf20Sopenharmony_ci  if (t == 0)
10708c2ecf20Sopenharmony_ci    return inflate_stored();
10718c2ecf20Sopenharmony_ci  if (t == 1)
10728c2ecf20Sopenharmony_ci    return inflate_fixed();
10738c2ecf20Sopenharmony_ci
10748c2ecf20Sopenharmony_ci  DEBG(">");
10758c2ecf20Sopenharmony_ci
10768c2ecf20Sopenharmony_ci  /* bad block type */
10778c2ecf20Sopenharmony_ci  return 2;
10788c2ecf20Sopenharmony_ci
10798c2ecf20Sopenharmony_ci underrun:
10808c2ecf20Sopenharmony_ci  return 4;			/* Input underrun */
10818c2ecf20Sopenharmony_ci}
10828c2ecf20Sopenharmony_ci
10838c2ecf20Sopenharmony_ci
10848c2ecf20Sopenharmony_ci
10858c2ecf20Sopenharmony_ciSTATIC int INIT inflate(void)
10868c2ecf20Sopenharmony_ci/* decompress an inflated entry */
10878c2ecf20Sopenharmony_ci{
10888c2ecf20Sopenharmony_ci  int e;                /* last block flag */
10898c2ecf20Sopenharmony_ci  int r;                /* result code */
10908c2ecf20Sopenharmony_ci  unsigned h;           /* maximum struct huft's malloc'ed */
10918c2ecf20Sopenharmony_ci
10928c2ecf20Sopenharmony_ci  /* initialize window, bit buffer */
10938c2ecf20Sopenharmony_ci  wp = 0;
10948c2ecf20Sopenharmony_ci  bk = 0;
10958c2ecf20Sopenharmony_ci  bb = 0;
10968c2ecf20Sopenharmony_ci
10978c2ecf20Sopenharmony_ci
10988c2ecf20Sopenharmony_ci  /* decompress until the last block */
10998c2ecf20Sopenharmony_ci  h = 0;
11008c2ecf20Sopenharmony_ci  do {
11018c2ecf20Sopenharmony_ci    hufts = 0;
11028c2ecf20Sopenharmony_ci#ifdef ARCH_HAS_DECOMP_WDOG
11038c2ecf20Sopenharmony_ci    arch_decomp_wdog();
11048c2ecf20Sopenharmony_ci#endif
11058c2ecf20Sopenharmony_ci    r = inflate_block(&e);
11068c2ecf20Sopenharmony_ci    if (r)
11078c2ecf20Sopenharmony_ci	    return r;
11088c2ecf20Sopenharmony_ci    if (hufts > h)
11098c2ecf20Sopenharmony_ci      h = hufts;
11108c2ecf20Sopenharmony_ci  } while (!e);
11118c2ecf20Sopenharmony_ci
11128c2ecf20Sopenharmony_ci  /* Undo too much lookahead. The next read will be byte aligned so we
11138c2ecf20Sopenharmony_ci   * can discard unused bits in the last meaningful byte.
11148c2ecf20Sopenharmony_ci   */
11158c2ecf20Sopenharmony_ci  while (bk >= 8) {
11168c2ecf20Sopenharmony_ci    bk -= 8;
11178c2ecf20Sopenharmony_ci    inptr--;
11188c2ecf20Sopenharmony_ci  }
11198c2ecf20Sopenharmony_ci
11208c2ecf20Sopenharmony_ci  /* flush out slide */
11218c2ecf20Sopenharmony_ci  flush_output(wp);
11228c2ecf20Sopenharmony_ci
11238c2ecf20Sopenharmony_ci
11248c2ecf20Sopenharmony_ci  /* return success */
11258c2ecf20Sopenharmony_ci#ifdef DEBUG
11268c2ecf20Sopenharmony_ci  fprintf(stderr, "<%u> ", h);
11278c2ecf20Sopenharmony_ci#endif /* DEBUG */
11288c2ecf20Sopenharmony_ci  return 0;
11298c2ecf20Sopenharmony_ci}
11308c2ecf20Sopenharmony_ci
11318c2ecf20Sopenharmony_ci/**********************************************************************
11328c2ecf20Sopenharmony_ci *
11338c2ecf20Sopenharmony_ci * The following are support routines for inflate.c
11348c2ecf20Sopenharmony_ci *
11358c2ecf20Sopenharmony_ci **********************************************************************/
11368c2ecf20Sopenharmony_ci
11378c2ecf20Sopenharmony_cistatic ulg crc_32_tab[256];
11388c2ecf20Sopenharmony_cistatic ulg crc;		/* initialized in makecrc() so it'll reside in bss */
11398c2ecf20Sopenharmony_ci#define CRC_VALUE (crc ^ 0xffffffffUL)
11408c2ecf20Sopenharmony_ci
11418c2ecf20Sopenharmony_ci/*
11428c2ecf20Sopenharmony_ci * Code to compute the CRC-32 table. Borrowed from
11438c2ecf20Sopenharmony_ci * gzip-1.0.3/makecrc.c.
11448c2ecf20Sopenharmony_ci */
11458c2ecf20Sopenharmony_ci
11468c2ecf20Sopenharmony_cistatic void INIT
11478c2ecf20Sopenharmony_cimakecrc(void)
11488c2ecf20Sopenharmony_ci{
11498c2ecf20Sopenharmony_ci/* Not copyrighted 1990 Mark Adler	*/
11508c2ecf20Sopenharmony_ci
11518c2ecf20Sopenharmony_ci  unsigned long c;      /* crc shift register */
11528c2ecf20Sopenharmony_ci  unsigned long e;      /* polynomial exclusive-or pattern */
11538c2ecf20Sopenharmony_ci  int i;                /* counter for all possible eight bit values */
11548c2ecf20Sopenharmony_ci  int k;                /* byte being shifted into crc apparatus */
11558c2ecf20Sopenharmony_ci
11568c2ecf20Sopenharmony_ci  /* terms of polynomial defining this crc (except x^32): */
11578c2ecf20Sopenharmony_ci  static const int p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
11588c2ecf20Sopenharmony_ci
11598c2ecf20Sopenharmony_ci  /* Make exclusive-or pattern from polynomial */
11608c2ecf20Sopenharmony_ci  e = 0;
11618c2ecf20Sopenharmony_ci  for (i = 0; i < sizeof(p)/sizeof(int); i++)
11628c2ecf20Sopenharmony_ci    e |= 1L << (31 - p[i]);
11638c2ecf20Sopenharmony_ci
11648c2ecf20Sopenharmony_ci  crc_32_tab[0] = 0;
11658c2ecf20Sopenharmony_ci
11668c2ecf20Sopenharmony_ci  for (i = 1; i < 256; i++)
11678c2ecf20Sopenharmony_ci  {
11688c2ecf20Sopenharmony_ci    c = 0;
11698c2ecf20Sopenharmony_ci    for (k = i | 256; k != 1; k >>= 1)
11708c2ecf20Sopenharmony_ci    {
11718c2ecf20Sopenharmony_ci      c = c & 1 ? (c >> 1) ^ e : c >> 1;
11728c2ecf20Sopenharmony_ci      if (k & 1)
11738c2ecf20Sopenharmony_ci        c ^= e;
11748c2ecf20Sopenharmony_ci    }
11758c2ecf20Sopenharmony_ci    crc_32_tab[i] = c;
11768c2ecf20Sopenharmony_ci  }
11778c2ecf20Sopenharmony_ci
11788c2ecf20Sopenharmony_ci  /* this is initialized here so this code could reside in ROM */
11798c2ecf20Sopenharmony_ci  crc = (ulg)0xffffffffUL; /* shift register contents */
11808c2ecf20Sopenharmony_ci}
11818c2ecf20Sopenharmony_ci
11828c2ecf20Sopenharmony_ci/* gzip flag byte */
11838c2ecf20Sopenharmony_ci#define ASCII_FLAG   0x01 /* bit 0 set: file probably ASCII text */
11848c2ecf20Sopenharmony_ci#define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
11858c2ecf20Sopenharmony_ci#define EXTRA_FIELD  0x04 /* bit 2 set: extra field present */
11868c2ecf20Sopenharmony_ci#define ORIG_NAME    0x08 /* bit 3 set: original file name present */
11878c2ecf20Sopenharmony_ci#define COMMENT      0x10 /* bit 4 set: file comment present */
11888c2ecf20Sopenharmony_ci#define ENCRYPTED    0x20 /* bit 5 set: file is encrypted */
11898c2ecf20Sopenharmony_ci#define RESERVED     0xC0 /* bit 6,7:   reserved */
11908c2ecf20Sopenharmony_ci
11918c2ecf20Sopenharmony_ci/*
11928c2ecf20Sopenharmony_ci * Do the uncompression!
11938c2ecf20Sopenharmony_ci */
11948c2ecf20Sopenharmony_cistatic int INIT gunzip(void)
11958c2ecf20Sopenharmony_ci{
11968c2ecf20Sopenharmony_ci    uch flags;
11978c2ecf20Sopenharmony_ci    unsigned char magic[2]; /* magic header */
11988c2ecf20Sopenharmony_ci    char method;
11998c2ecf20Sopenharmony_ci    ulg orig_crc = 0;       /* original crc */
12008c2ecf20Sopenharmony_ci    ulg orig_len = 0;       /* original uncompressed length */
12018c2ecf20Sopenharmony_ci    int res;
12028c2ecf20Sopenharmony_ci
12038c2ecf20Sopenharmony_ci    magic[0] = NEXTBYTE();
12048c2ecf20Sopenharmony_ci    magic[1] = NEXTBYTE();
12058c2ecf20Sopenharmony_ci    method   = NEXTBYTE();
12068c2ecf20Sopenharmony_ci
12078c2ecf20Sopenharmony_ci    if (magic[0] != 037 ||
12088c2ecf20Sopenharmony_ci	((magic[1] != 0213) && (magic[1] != 0236))) {
12098c2ecf20Sopenharmony_ci	    error("bad gzip magic numbers");
12108c2ecf20Sopenharmony_ci	    return -1;
12118c2ecf20Sopenharmony_ci    }
12128c2ecf20Sopenharmony_ci
12138c2ecf20Sopenharmony_ci    /* We only support method #8, DEFLATED */
12148c2ecf20Sopenharmony_ci    if (method != 8)  {
12158c2ecf20Sopenharmony_ci	    error("internal error, invalid method");
12168c2ecf20Sopenharmony_ci	    return -1;
12178c2ecf20Sopenharmony_ci    }
12188c2ecf20Sopenharmony_ci
12198c2ecf20Sopenharmony_ci    flags  = (uch)get_byte();
12208c2ecf20Sopenharmony_ci    if ((flags & ENCRYPTED) != 0) {
12218c2ecf20Sopenharmony_ci	    error("Input is encrypted");
12228c2ecf20Sopenharmony_ci	    return -1;
12238c2ecf20Sopenharmony_ci    }
12248c2ecf20Sopenharmony_ci    if ((flags & CONTINUATION) != 0) {
12258c2ecf20Sopenharmony_ci	    error("Multi part input");
12268c2ecf20Sopenharmony_ci	    return -1;
12278c2ecf20Sopenharmony_ci    }
12288c2ecf20Sopenharmony_ci    if ((flags & RESERVED) != 0) {
12298c2ecf20Sopenharmony_ci	    error("Input has invalid flags");
12308c2ecf20Sopenharmony_ci	    return -1;
12318c2ecf20Sopenharmony_ci    }
12328c2ecf20Sopenharmony_ci    NEXTBYTE();	/* Get timestamp */
12338c2ecf20Sopenharmony_ci    NEXTBYTE();
12348c2ecf20Sopenharmony_ci    NEXTBYTE();
12358c2ecf20Sopenharmony_ci    NEXTBYTE();
12368c2ecf20Sopenharmony_ci
12378c2ecf20Sopenharmony_ci    (void)NEXTBYTE();  /* Ignore extra flags for the moment */
12388c2ecf20Sopenharmony_ci    (void)NEXTBYTE();  /* Ignore OS type for the moment */
12398c2ecf20Sopenharmony_ci
12408c2ecf20Sopenharmony_ci    if ((flags & EXTRA_FIELD) != 0) {
12418c2ecf20Sopenharmony_ci	    unsigned len = (unsigned)NEXTBYTE();
12428c2ecf20Sopenharmony_ci	    len |= ((unsigned)NEXTBYTE())<<8;
12438c2ecf20Sopenharmony_ci	    while (len--) (void)NEXTBYTE();
12448c2ecf20Sopenharmony_ci    }
12458c2ecf20Sopenharmony_ci
12468c2ecf20Sopenharmony_ci    /* Get original file name if it was truncated */
12478c2ecf20Sopenharmony_ci    if ((flags & ORIG_NAME) != 0) {
12488c2ecf20Sopenharmony_ci	    /* Discard the old name */
12498c2ecf20Sopenharmony_ci	    while (NEXTBYTE() != 0) /* null */ ;
12508c2ecf20Sopenharmony_ci    }
12518c2ecf20Sopenharmony_ci
12528c2ecf20Sopenharmony_ci    /* Discard file comment if any */
12538c2ecf20Sopenharmony_ci    if ((flags & COMMENT) != 0) {
12548c2ecf20Sopenharmony_ci	    while (NEXTBYTE() != 0) /* null */ ;
12558c2ecf20Sopenharmony_ci    }
12568c2ecf20Sopenharmony_ci
12578c2ecf20Sopenharmony_ci    /* Decompress */
12588c2ecf20Sopenharmony_ci    if ((res = inflate())) {
12598c2ecf20Sopenharmony_ci	    switch (res) {
12608c2ecf20Sopenharmony_ci	    case 0:
12618c2ecf20Sopenharmony_ci		    break;
12628c2ecf20Sopenharmony_ci	    case 1:
12638c2ecf20Sopenharmony_ci		    error("invalid compressed format (err=1)");
12648c2ecf20Sopenharmony_ci		    break;
12658c2ecf20Sopenharmony_ci	    case 2:
12668c2ecf20Sopenharmony_ci		    error("invalid compressed format (err=2)");
12678c2ecf20Sopenharmony_ci		    break;
12688c2ecf20Sopenharmony_ci	    case 3:
12698c2ecf20Sopenharmony_ci		    error("out of memory");
12708c2ecf20Sopenharmony_ci		    break;
12718c2ecf20Sopenharmony_ci	    case 4:
12728c2ecf20Sopenharmony_ci		    error("out of input data");
12738c2ecf20Sopenharmony_ci		    break;
12748c2ecf20Sopenharmony_ci	    default:
12758c2ecf20Sopenharmony_ci		    error("invalid compressed format (other)");
12768c2ecf20Sopenharmony_ci	    }
12778c2ecf20Sopenharmony_ci	    return -1;
12788c2ecf20Sopenharmony_ci    }
12798c2ecf20Sopenharmony_ci
12808c2ecf20Sopenharmony_ci    /* Get the crc and original length */
12818c2ecf20Sopenharmony_ci    /* crc32  (see algorithm.doc)
12828c2ecf20Sopenharmony_ci     * uncompressed input size modulo 2^32
12838c2ecf20Sopenharmony_ci     */
12848c2ecf20Sopenharmony_ci    orig_crc = (ulg) NEXTBYTE();
12858c2ecf20Sopenharmony_ci    orig_crc |= (ulg) NEXTBYTE() << 8;
12868c2ecf20Sopenharmony_ci    orig_crc |= (ulg) NEXTBYTE() << 16;
12878c2ecf20Sopenharmony_ci    orig_crc |= (ulg) NEXTBYTE() << 24;
12888c2ecf20Sopenharmony_ci
12898c2ecf20Sopenharmony_ci    orig_len = (ulg) NEXTBYTE();
12908c2ecf20Sopenharmony_ci    orig_len |= (ulg) NEXTBYTE() << 8;
12918c2ecf20Sopenharmony_ci    orig_len |= (ulg) NEXTBYTE() << 16;
12928c2ecf20Sopenharmony_ci    orig_len |= (ulg) NEXTBYTE() << 24;
12938c2ecf20Sopenharmony_ci
12948c2ecf20Sopenharmony_ci    /* Validate decompression */
12958c2ecf20Sopenharmony_ci    if (orig_crc != CRC_VALUE) {
12968c2ecf20Sopenharmony_ci	    error("crc error");
12978c2ecf20Sopenharmony_ci	    return -1;
12988c2ecf20Sopenharmony_ci    }
12998c2ecf20Sopenharmony_ci    if (orig_len != bytes_out) {
13008c2ecf20Sopenharmony_ci	    error("length error");
13018c2ecf20Sopenharmony_ci	    return -1;
13028c2ecf20Sopenharmony_ci    }
13038c2ecf20Sopenharmony_ci    return 0;
13048c2ecf20Sopenharmony_ci
13058c2ecf20Sopenharmony_ci underrun:			/* NEXTBYTE() goto's here if needed */
13068c2ecf20Sopenharmony_ci    error("out of input data");
13078c2ecf20Sopenharmony_ci    return -1;
13088c2ecf20Sopenharmony_ci}
13098c2ecf20Sopenharmony_ci
13108c2ecf20Sopenharmony_ci
1311