1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun #define DEBG(x)
3*4882a593Smuzhiyun #define DEBG1(x)
4*4882a593Smuzhiyun /* inflate.c -- Not copyrighted 1992 by Mark Adler
5*4882a593Smuzhiyun version c10p1, 10 January 1993 */
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun /*
8*4882a593Smuzhiyun * Adapted for booting Linux by Hannu Savolainen 1993
9*4882a593Smuzhiyun * based on gzip-1.0.3
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * Nicolas Pitre <nico@fluxnic.net>, 1999/04/14 :
12*4882a593Smuzhiyun * Little mods for all variable to reside either into rodata or bss segments
13*4882a593Smuzhiyun * by marking constant variables with 'const' and initializing all the others
14*4882a593Smuzhiyun * at run-time only. This allows for the kernel uncompressor to run
15*4882a593Smuzhiyun * directly from Flash or ROM memory on embedded systems.
16*4882a593Smuzhiyun */
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun /*
19*4882a593Smuzhiyun Inflate deflated (PKZIP's method 8 compressed) data. The compression
20*4882a593Smuzhiyun method searches for as much of the current string of bytes (up to a
21*4882a593Smuzhiyun length of 258) in the previous 32 K bytes. If it doesn't find any
22*4882a593Smuzhiyun matches (of at least length 3), it codes the next byte. Otherwise, it
23*4882a593Smuzhiyun codes the length of the matched string and its distance backwards from
24*4882a593Smuzhiyun the current position. There is a single Huffman code that codes both
25*4882a593Smuzhiyun single bytes (called "literals") and match lengths. A second Huffman
26*4882a593Smuzhiyun code codes the distance information, which follows a length code. Each
27*4882a593Smuzhiyun length or distance code actually represents a base value and a number
28*4882a593Smuzhiyun of "extra" (sometimes zero) bits to get to add to the base value. At
29*4882a593Smuzhiyun the end of each deflated block is a special end-of-block (EOB) literal/
30*4882a593Smuzhiyun length code. The decoding process is basically: get a literal/length
31*4882a593Smuzhiyun code; if EOB then done; if a literal, emit the decoded byte; if a
32*4882a593Smuzhiyun length then get the distance and emit the referred-to bytes from the
33*4882a593Smuzhiyun sliding window of previously emitted data.
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun There are (currently) three kinds of inflate blocks: stored, fixed, and
36*4882a593Smuzhiyun dynamic. The compressor deals with some chunk of data at a time, and
37*4882a593Smuzhiyun decides which method to use on a chunk-by-chunk basis. A chunk might
38*4882a593Smuzhiyun typically be 32 K or 64 K. If the chunk is incompressible, then the
39*4882a593Smuzhiyun "stored" method is used. In this case, the bytes are simply stored as
40*4882a593Smuzhiyun is, eight bits per byte, with none of the above coding. The bytes are
41*4882a593Smuzhiyun preceded by a count, since there is no longer an EOB code.
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun If the data is compressible, then either the fixed or dynamic methods
44*4882a593Smuzhiyun are used. In the dynamic method, the compressed data is preceded by
45*4882a593Smuzhiyun an encoding of the literal/length and distance Huffman codes that are
46*4882a593Smuzhiyun to be used to decode this block. The representation is itself Huffman
47*4882a593Smuzhiyun coded, and so is preceded by a description of that code. These code
48*4882a593Smuzhiyun descriptions take up a little space, and so for small blocks, there is
49*4882a593Smuzhiyun a predefined set of codes, called the fixed codes. The fixed method is
50*4882a593Smuzhiyun used if the block codes up smaller that way (usually for quite small
51*4882a593Smuzhiyun chunks), otherwise the dynamic method is used. In the latter case, the
52*4882a593Smuzhiyun codes are customized to the probabilities in the current block, and so
53*4882a593Smuzhiyun can code it much better than the pre-determined fixed codes.
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun The Huffman codes themselves are decoded using a multi-level table
56*4882a593Smuzhiyun lookup, in order to maximize the speed of decoding plus the speed of
57*4882a593Smuzhiyun building the decoding tables. See the comments below that precede the
58*4882a593Smuzhiyun lbits and dbits tuning parameters.
59*4882a593Smuzhiyun */
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun /*
63*4882a593Smuzhiyun Notes beyond the 1.93a appnote.txt:
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun 1. Distance pointers never point before the beginning of the output
66*4882a593Smuzhiyun stream.
67*4882a593Smuzhiyun 2. Distance pointers can point back across blocks, up to 32k away.
68*4882a593Smuzhiyun 3. There is an implied maximum of 7 bits for the bit length table and
69*4882a593Smuzhiyun 15 bits for the actual data.
70*4882a593Smuzhiyun 4. If only one code exists, then it is encoded using one bit. (Zero
71*4882a593Smuzhiyun would be more efficient, but perhaps a little confusing.) If two
72*4882a593Smuzhiyun codes exist, they are coded using one bit each (0 and 1).
73*4882a593Smuzhiyun 5. There is no way of sending zero distance codes--a dummy must be
74*4882a593Smuzhiyun sent if there are none. (History: a pre 2.0 version of PKZIP would
75*4882a593Smuzhiyun store blocks with no distance codes, but this was discovered to be
76*4882a593Smuzhiyun too harsh a criterion.) Valid only for 1.93a. 2.04c does allow
77*4882a593Smuzhiyun zero distance codes, which is sent as one code of zero bits in
78*4882a593Smuzhiyun length.
79*4882a593Smuzhiyun 6. There are up to 286 literal/length codes. Code 256 represents the
80*4882a593Smuzhiyun end-of-block. Note however that the static length tree defines
81*4882a593Smuzhiyun 288 codes just to fill out the Huffman codes. Codes 286 and 287
82*4882a593Smuzhiyun cannot be used though, since there is no length base or extra bits
83*4882a593Smuzhiyun defined for them. Similarly, there are up to 30 distance codes.
84*4882a593Smuzhiyun However, static trees define 32 codes (all 5 bits) to fill out the
85*4882a593Smuzhiyun Huffman codes, but the last two had better not show up in the data.
86*4882a593Smuzhiyun 7. Unzip can check dynamic Huffman blocks for complete code sets.
87*4882a593Smuzhiyun The exception is that a single code would not be complete (see #4).
88*4882a593Smuzhiyun 8. The five bits following the block type is really the number of
89*4882a593Smuzhiyun literal codes sent minus 257.
90*4882a593Smuzhiyun 9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
91*4882a593Smuzhiyun (1+6+6). Therefore, to output three times the length, you output
92*4882a593Smuzhiyun three codes (1+1+1), whereas to output four times the same length,
93*4882a593Smuzhiyun you only need two codes (1+3). Hmm.
94*4882a593Smuzhiyun 10. In the tree reconstruction algorithm, Code = Code + Increment
95*4882a593Smuzhiyun only if BitLength(i) is not zero. (Pretty obvious.)
96*4882a593Smuzhiyun 11. Correction: 4 Bits: # of Bit Length codes - 4 (4 - 19)
97*4882a593Smuzhiyun 12. Note: length code 284 can represent 227-258, but length code 285
98*4882a593Smuzhiyun really is 258. The last length deserves its own, short code
99*4882a593Smuzhiyun since it gets used a lot in very redundant files. The length
100*4882a593Smuzhiyun 258 is special since 258 - 3 (the min match length) is 255.
101*4882a593Smuzhiyun 13. The literal/length and distance code bit lengths are read as a
102*4882a593Smuzhiyun single stream of lengths. It is possible (and advantageous) for
103*4882a593Smuzhiyun a repeat code (16, 17, or 18) to go across the boundary between
104*4882a593Smuzhiyun the two sets of lengths.
105*4882a593Smuzhiyun */
106*4882a593Smuzhiyun #include <linux/compiler.h>
107*4882a593Smuzhiyun #ifdef NO_INFLATE_MALLOC
108*4882a593Smuzhiyun #include <linux/slab.h>
109*4882a593Smuzhiyun #endif
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun #ifdef RCSID
112*4882a593Smuzhiyun static char rcsid[] = "#Id: inflate.c,v 0.14 1993/06/10 13:27:04 jloup Exp #";
113*4882a593Smuzhiyun #endif
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun #ifndef STATIC
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun #if defined(STDC_HEADERS) || defined(HAVE_STDLIB_H)
118*4882a593Smuzhiyun # include <sys/types.h>
119*4882a593Smuzhiyun # include <stdlib.h>
120*4882a593Smuzhiyun #endif
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun #include "gzip.h"
123*4882a593Smuzhiyun #define STATIC
124*4882a593Smuzhiyun #endif /* !STATIC */
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun #ifndef INIT
127*4882a593Smuzhiyun #define INIT
128*4882a593Smuzhiyun #endif
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun #define slide window
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun /* Huffman code lookup table entry--this entry is four bytes for machines
133*4882a593Smuzhiyun that have 16-bit pointers (e.g. PC's in the small or medium model).
134*4882a593Smuzhiyun Valid extra bits are 0..13. e == 15 is EOB (end of block), e == 16
135*4882a593Smuzhiyun means that v is a literal, 16 < e < 32 means that v is a pointer to
136*4882a593Smuzhiyun the next table, which codes e - 16 bits, and lastly e == 99 indicates
137*4882a593Smuzhiyun an unused code. If a code with e == 99 is looked up, this implies an
138*4882a593Smuzhiyun error in the data. */
139*4882a593Smuzhiyun struct huft {
140*4882a593Smuzhiyun uch e; /* number of extra bits or operation */
141*4882a593Smuzhiyun uch b; /* number of bits in this code or subcode */
142*4882a593Smuzhiyun union {
143*4882a593Smuzhiyun ush n; /* literal, length base, or distance base */
144*4882a593Smuzhiyun struct huft *t; /* pointer to next level of table */
145*4882a593Smuzhiyun } v;
146*4882a593Smuzhiyun };
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun /* Function prototypes */
150*4882a593Smuzhiyun STATIC int INIT huft_build OF((unsigned *, unsigned, unsigned,
151*4882a593Smuzhiyun const ush *, const ush *, struct huft **, int *));
152*4882a593Smuzhiyun STATIC int INIT huft_free OF((struct huft *));
153*4882a593Smuzhiyun STATIC int INIT inflate_codes OF((struct huft *, struct huft *, int, int));
154*4882a593Smuzhiyun STATIC int INIT inflate_stored OF((void));
155*4882a593Smuzhiyun STATIC int INIT inflate_fixed OF((void));
156*4882a593Smuzhiyun STATIC int INIT inflate_dynamic OF((void));
157*4882a593Smuzhiyun STATIC int INIT inflate_block OF((int *));
158*4882a593Smuzhiyun STATIC int INIT inflate OF((void));
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun /* The inflate algorithm uses a sliding 32 K byte window on the uncompressed
162*4882a593Smuzhiyun stream to find repeated byte strings. This is implemented here as a
163*4882a593Smuzhiyun circular buffer. The index is updated simply by incrementing and then
164*4882a593Smuzhiyun ANDing with 0x7fff (32K-1). */
165*4882a593Smuzhiyun /* It is left to other modules to supply the 32 K area. It is assumed
166*4882a593Smuzhiyun to be usable as if it were declared "uch slide[32768];" or as just
167*4882a593Smuzhiyun "uch *slide;" and then malloc'ed in the latter case. The definition
168*4882a593Smuzhiyun must be in unzip.h, included above. */
169*4882a593Smuzhiyun /* unsigned wp; current position in slide */
170*4882a593Smuzhiyun #define wp outcnt
171*4882a593Smuzhiyun #define flush_output(w) (wp=(w),flush_window())
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun /* Tables for deflate from PKZIP's appnote.txt. */
174*4882a593Smuzhiyun static const unsigned border[] = { /* Order of the bit length code lengths */
175*4882a593Smuzhiyun 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
176*4882a593Smuzhiyun static const ush cplens[] = { /* Copy lengths for literal codes 257..285 */
177*4882a593Smuzhiyun 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
178*4882a593Smuzhiyun 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
179*4882a593Smuzhiyun /* note: see note #13 above about the 258 in this list. */
180*4882a593Smuzhiyun static const ush cplext[] = { /* Extra bits for literal codes 257..285 */
181*4882a593Smuzhiyun 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
182*4882a593Smuzhiyun 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99}; /* 99==invalid */
183*4882a593Smuzhiyun static const ush cpdist[] = { /* Copy offsets for distance codes 0..29 */
184*4882a593Smuzhiyun 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
185*4882a593Smuzhiyun 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
186*4882a593Smuzhiyun 8193, 12289, 16385, 24577};
187*4882a593Smuzhiyun static const ush cpdext[] = { /* Extra bits for distance codes */
188*4882a593Smuzhiyun 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
189*4882a593Smuzhiyun 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
190*4882a593Smuzhiyun 12, 12, 13, 13};
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun /* Macros for inflate() bit peeking and grabbing.
195*4882a593Smuzhiyun The usage is:
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun NEEDBITS(j)
198*4882a593Smuzhiyun x = b & mask_bits[j];
199*4882a593Smuzhiyun DUMPBITS(j)
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun where NEEDBITS makes sure that b has at least j bits in it, and
202*4882a593Smuzhiyun DUMPBITS removes the bits from b. The macros use the variable k
203*4882a593Smuzhiyun for the number of bits in b. Normally, b and k are register
204*4882a593Smuzhiyun variables for speed, and are initialized at the beginning of a
205*4882a593Smuzhiyun routine that uses these macros from a global bit buffer and count.
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun If we assume that EOB will be the longest code, then we will never
208*4882a593Smuzhiyun ask for bits with NEEDBITS that are beyond the end of the stream.
209*4882a593Smuzhiyun So, NEEDBITS should not read any more bytes than are needed to
210*4882a593Smuzhiyun meet the request. Then no bytes need to be "returned" to the buffer
211*4882a593Smuzhiyun at the end of the last block.
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun However, this assumption is not true for fixed blocks--the EOB code
214*4882a593Smuzhiyun is 7 bits, but the other literal/length codes can be 8 or 9 bits.
215*4882a593Smuzhiyun (The EOB code is shorter than other codes because fixed blocks are
216*4882a593Smuzhiyun generally short. So, while a block always has an EOB, many other
217*4882a593Smuzhiyun literal/length codes have a significantly lower probability of
218*4882a593Smuzhiyun showing up at all.) However, by making the first table have a
219*4882a593Smuzhiyun lookup of seven bits, the EOB code will be found in that first
220*4882a593Smuzhiyun lookup, and so will not require that too many bits be pulled from
221*4882a593Smuzhiyun the stream.
222*4882a593Smuzhiyun */
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun STATIC ulg bb; /* bit buffer */
225*4882a593Smuzhiyun STATIC unsigned bk; /* bits in bit buffer */
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun STATIC const ush mask_bits[] = {
228*4882a593Smuzhiyun 0x0000,
229*4882a593Smuzhiyun 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
230*4882a593Smuzhiyun 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
231*4882a593Smuzhiyun };
232*4882a593Smuzhiyun
233*4882a593Smuzhiyun #define NEXTBYTE() ({ int v = get_byte(); if (v < 0) goto underrun; (uch)v; })
234*4882a593Smuzhiyun #define NEEDBITS(n) {while(k<(n)){b|=((ulg)NEXTBYTE())<<k;k+=8;}}
235*4882a593Smuzhiyun #define DUMPBITS(n) {b>>=(n);k-=(n);}
236*4882a593Smuzhiyun
237*4882a593Smuzhiyun #ifndef NO_INFLATE_MALLOC
238*4882a593Smuzhiyun /* A trivial malloc implementation, adapted from
239*4882a593Smuzhiyun * malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994
240*4882a593Smuzhiyun */
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun static unsigned long malloc_ptr;
243*4882a593Smuzhiyun static int malloc_count;
244*4882a593Smuzhiyun
malloc(int size)245*4882a593Smuzhiyun static void *malloc(int size)
246*4882a593Smuzhiyun {
247*4882a593Smuzhiyun void *p;
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun if (size < 0)
250*4882a593Smuzhiyun error("Malloc error");
251*4882a593Smuzhiyun if (!malloc_ptr)
252*4882a593Smuzhiyun malloc_ptr = free_mem_ptr;
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun malloc_ptr = (malloc_ptr + 3) & ~3; /* Align */
255*4882a593Smuzhiyun
256*4882a593Smuzhiyun p = (void *)malloc_ptr;
257*4882a593Smuzhiyun malloc_ptr += size;
258*4882a593Smuzhiyun
259*4882a593Smuzhiyun if (free_mem_end_ptr && malloc_ptr >= free_mem_end_ptr)
260*4882a593Smuzhiyun error("Out of memory");
261*4882a593Smuzhiyun
262*4882a593Smuzhiyun malloc_count++;
263*4882a593Smuzhiyun return p;
264*4882a593Smuzhiyun }
265*4882a593Smuzhiyun
free(void * where)266*4882a593Smuzhiyun static void free(void *where)
267*4882a593Smuzhiyun {
268*4882a593Smuzhiyun malloc_count--;
269*4882a593Smuzhiyun if (!malloc_count)
270*4882a593Smuzhiyun malloc_ptr = free_mem_ptr;
271*4882a593Smuzhiyun }
272*4882a593Smuzhiyun #else
273*4882a593Smuzhiyun #define malloc(a) kmalloc(a, GFP_KERNEL)
274*4882a593Smuzhiyun #define free(a) kfree(a)
275*4882a593Smuzhiyun #endif
276*4882a593Smuzhiyun
277*4882a593Smuzhiyun /*
278*4882a593Smuzhiyun Huffman code decoding is performed using a multi-level table lookup.
279*4882a593Smuzhiyun The fastest way to decode is to simply build a lookup table whose
280*4882a593Smuzhiyun size is determined by the longest code. However, the time it takes
281*4882a593Smuzhiyun to build this table can also be a factor if the data being decoded
282*4882a593Smuzhiyun is not very long. The most common codes are necessarily the
283*4882a593Smuzhiyun shortest codes, so those codes dominate the decoding time, and hence
284*4882a593Smuzhiyun the speed. The idea is you can have a shorter table that decodes the
285*4882a593Smuzhiyun shorter, more probable codes, and then point to subsidiary tables for
286*4882a593Smuzhiyun the longer codes. The time it costs to decode the longer codes is
287*4882a593Smuzhiyun then traded against the time it takes to make longer tables.
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun This results of this trade are in the variables lbits and dbits
290*4882a593Smuzhiyun below. lbits is the number of bits the first level table for literal/
291*4882a593Smuzhiyun length codes can decode in one step, and dbits is the same thing for
292*4882a593Smuzhiyun the distance codes. Subsequent tables are also less than or equal to
293*4882a593Smuzhiyun those sizes. These values may be adjusted either when all of the
294*4882a593Smuzhiyun codes are shorter than that, in which case the longest code length in
295*4882a593Smuzhiyun bits is used, or when the shortest code is *longer* than the requested
296*4882a593Smuzhiyun table size, in which case the length of the shortest code in bits is
297*4882a593Smuzhiyun used.
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun There are two different values for the two tables, since they code a
300*4882a593Smuzhiyun different number of possibilities each. The literal/length table
301*4882a593Smuzhiyun codes 286 possible values, or in a flat code, a little over eight
302*4882a593Smuzhiyun bits. The distance table codes 30 possible values, or a little less
303*4882a593Smuzhiyun than five bits, flat. The optimum values for speed end up being
304*4882a593Smuzhiyun about one bit more than those, so lbits is 8+1 and dbits is 5+1.
305*4882a593Smuzhiyun The optimum values may differ though from machine to machine, and
306*4882a593Smuzhiyun possibly even between compilers. Your mileage may vary.
307*4882a593Smuzhiyun */
308*4882a593Smuzhiyun
309*4882a593Smuzhiyun
310*4882a593Smuzhiyun STATIC const int lbits = 9; /* bits in base literal/length lookup table */
311*4882a593Smuzhiyun STATIC const int dbits = 6; /* bits in base distance lookup table */
312*4882a593Smuzhiyun
313*4882a593Smuzhiyun
314*4882a593Smuzhiyun /* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
315*4882a593Smuzhiyun #define BMAX 16 /* maximum bit length of any code (16 for explode) */
316*4882a593Smuzhiyun #define N_MAX 288 /* maximum number of codes in any set */
317*4882a593Smuzhiyun
318*4882a593Smuzhiyun
319*4882a593Smuzhiyun STATIC unsigned hufts; /* track memory usage */
320*4882a593Smuzhiyun
321*4882a593Smuzhiyun
huft_build(unsigned * b,unsigned n,unsigned s,const ush * d,const ush * e,struct huft ** t,int * m)322*4882a593Smuzhiyun STATIC int INIT huft_build(
323*4882a593Smuzhiyun unsigned *b, /* code lengths in bits (all assumed <= BMAX) */
324*4882a593Smuzhiyun unsigned n, /* number of codes (assumed <= N_MAX) */
325*4882a593Smuzhiyun unsigned s, /* number of simple-valued codes (0..s-1) */
326*4882a593Smuzhiyun const ush *d, /* list of base values for non-simple codes */
327*4882a593Smuzhiyun const ush *e, /* list of extra bits for non-simple codes */
328*4882a593Smuzhiyun struct huft **t, /* result: starting table */
329*4882a593Smuzhiyun int *m /* maximum lookup bits, returns actual */
330*4882a593Smuzhiyun )
331*4882a593Smuzhiyun /* Given a list of code lengths and a maximum table size, make a set of
332*4882a593Smuzhiyun tables to decode that set of codes. Return zero on success, one if
333*4882a593Smuzhiyun the given code set is incomplete (the tables are still built in this
334*4882a593Smuzhiyun case), two if the input is invalid (all zero length codes or an
335*4882a593Smuzhiyun oversubscribed set of lengths), and three if not enough memory. */
336*4882a593Smuzhiyun {
337*4882a593Smuzhiyun unsigned a; /* counter for codes of length k */
338*4882a593Smuzhiyun unsigned f; /* i repeats in table every f entries */
339*4882a593Smuzhiyun int g; /* maximum code length */
340*4882a593Smuzhiyun int h; /* table level */
341*4882a593Smuzhiyun register unsigned i; /* counter, current code */
342*4882a593Smuzhiyun register unsigned j; /* counter */
343*4882a593Smuzhiyun register int k; /* number of bits in current code */
344*4882a593Smuzhiyun int l; /* bits per table (returned in m) */
345*4882a593Smuzhiyun register unsigned *p; /* pointer into c[], b[], or v[] */
346*4882a593Smuzhiyun register struct huft *q; /* points to current table */
347*4882a593Smuzhiyun struct huft r; /* table entry for structure assignment */
348*4882a593Smuzhiyun register int w; /* bits before this table == (l * h) */
349*4882a593Smuzhiyun unsigned *xp; /* pointer into x */
350*4882a593Smuzhiyun int y; /* number of dummy codes added */
351*4882a593Smuzhiyun unsigned z; /* number of entries in current table */
352*4882a593Smuzhiyun struct {
353*4882a593Smuzhiyun unsigned c[BMAX+1]; /* bit length count table */
354*4882a593Smuzhiyun struct huft *u[BMAX]; /* table stack */
355*4882a593Smuzhiyun unsigned v[N_MAX]; /* values in order of bit length */
356*4882a593Smuzhiyun unsigned x[BMAX+1]; /* bit offsets, then code stack */
357*4882a593Smuzhiyun } *stk;
358*4882a593Smuzhiyun unsigned *c, *v, *x;
359*4882a593Smuzhiyun struct huft **u;
360*4882a593Smuzhiyun int ret;
361*4882a593Smuzhiyun
362*4882a593Smuzhiyun DEBG("huft1 ");
363*4882a593Smuzhiyun
364*4882a593Smuzhiyun stk = malloc(sizeof(*stk));
365*4882a593Smuzhiyun if (stk == NULL)
366*4882a593Smuzhiyun return 3; /* out of memory */
367*4882a593Smuzhiyun
368*4882a593Smuzhiyun c = stk->c;
369*4882a593Smuzhiyun v = stk->v;
370*4882a593Smuzhiyun x = stk->x;
371*4882a593Smuzhiyun u = stk->u;
372*4882a593Smuzhiyun
373*4882a593Smuzhiyun /* Generate counts for each bit length */
374*4882a593Smuzhiyun memzero(stk->c, sizeof(stk->c));
375*4882a593Smuzhiyun p = b; i = n;
376*4882a593Smuzhiyun do {
377*4882a593Smuzhiyun Tracecv(*p, (stderr, (n-i >= ' ' && n-i <= '~' ? "%c %d\n" : "0x%x %d\n"),
378*4882a593Smuzhiyun n-i, *p));
379*4882a593Smuzhiyun c[*p]++; /* assume all entries <= BMAX */
380*4882a593Smuzhiyun p++; /* Can't combine with above line (Solaris bug) */
381*4882a593Smuzhiyun } while (--i);
382*4882a593Smuzhiyun if (c[0] == n) /* null input--all zero length codes */
383*4882a593Smuzhiyun {
384*4882a593Smuzhiyun *t = (struct huft *)NULL;
385*4882a593Smuzhiyun *m = 0;
386*4882a593Smuzhiyun ret = 2;
387*4882a593Smuzhiyun goto out;
388*4882a593Smuzhiyun }
389*4882a593Smuzhiyun
390*4882a593Smuzhiyun DEBG("huft2 ");
391*4882a593Smuzhiyun
392*4882a593Smuzhiyun /* Find minimum and maximum length, bound *m by those */
393*4882a593Smuzhiyun l = *m;
394*4882a593Smuzhiyun for (j = 1; j <= BMAX; j++)
395*4882a593Smuzhiyun if (c[j])
396*4882a593Smuzhiyun break;
397*4882a593Smuzhiyun k = j; /* minimum code length */
398*4882a593Smuzhiyun if ((unsigned)l < j)
399*4882a593Smuzhiyun l = j;
400*4882a593Smuzhiyun for (i = BMAX; i; i--)
401*4882a593Smuzhiyun if (c[i])
402*4882a593Smuzhiyun break;
403*4882a593Smuzhiyun g = i; /* maximum code length */
404*4882a593Smuzhiyun if ((unsigned)l > i)
405*4882a593Smuzhiyun l = i;
406*4882a593Smuzhiyun *m = l;
407*4882a593Smuzhiyun
408*4882a593Smuzhiyun DEBG("huft3 ");
409*4882a593Smuzhiyun
410*4882a593Smuzhiyun /* Adjust last length count to fill out codes, if needed */
411*4882a593Smuzhiyun for (y = 1 << j; j < i; j++, y <<= 1)
412*4882a593Smuzhiyun if ((y -= c[j]) < 0) {
413*4882a593Smuzhiyun ret = 2; /* bad input: more codes than bits */
414*4882a593Smuzhiyun goto out;
415*4882a593Smuzhiyun }
416*4882a593Smuzhiyun if ((y -= c[i]) < 0) {
417*4882a593Smuzhiyun ret = 2;
418*4882a593Smuzhiyun goto out;
419*4882a593Smuzhiyun }
420*4882a593Smuzhiyun c[i] += y;
421*4882a593Smuzhiyun
422*4882a593Smuzhiyun DEBG("huft4 ");
423*4882a593Smuzhiyun
424*4882a593Smuzhiyun /* Generate starting offsets into the value table for each length */
425*4882a593Smuzhiyun x[1] = j = 0;
426*4882a593Smuzhiyun p = c + 1; xp = x + 2;
427*4882a593Smuzhiyun while (--i) { /* note that i == g from above */
428*4882a593Smuzhiyun *xp++ = (j += *p++);
429*4882a593Smuzhiyun }
430*4882a593Smuzhiyun
431*4882a593Smuzhiyun DEBG("huft5 ");
432*4882a593Smuzhiyun
433*4882a593Smuzhiyun /* Make a table of values in order of bit lengths */
434*4882a593Smuzhiyun p = b; i = 0;
435*4882a593Smuzhiyun do {
436*4882a593Smuzhiyun if ((j = *p++) != 0)
437*4882a593Smuzhiyun v[x[j]++] = i;
438*4882a593Smuzhiyun } while (++i < n);
439*4882a593Smuzhiyun n = x[g]; /* set n to length of v */
440*4882a593Smuzhiyun
441*4882a593Smuzhiyun DEBG("h6 ");
442*4882a593Smuzhiyun
443*4882a593Smuzhiyun /* Generate the Huffman codes and for each, make the table entries */
444*4882a593Smuzhiyun x[0] = i = 0; /* first Huffman code is zero */
445*4882a593Smuzhiyun p = v; /* grab values in bit order */
446*4882a593Smuzhiyun h = -1; /* no tables yet--level -1 */
447*4882a593Smuzhiyun w = -l; /* bits decoded == (l * h) */
448*4882a593Smuzhiyun u[0] = (struct huft *)NULL; /* just to keep compilers happy */
449*4882a593Smuzhiyun q = (struct huft *)NULL; /* ditto */
450*4882a593Smuzhiyun z = 0; /* ditto */
451*4882a593Smuzhiyun DEBG("h6a ");
452*4882a593Smuzhiyun
453*4882a593Smuzhiyun /* go through the bit lengths (k already is bits in shortest code) */
454*4882a593Smuzhiyun for (; k <= g; k++)
455*4882a593Smuzhiyun {
456*4882a593Smuzhiyun DEBG("h6b ");
457*4882a593Smuzhiyun a = c[k];
458*4882a593Smuzhiyun while (a--)
459*4882a593Smuzhiyun {
460*4882a593Smuzhiyun DEBG("h6b1 ");
461*4882a593Smuzhiyun /* here i is the Huffman code of length k bits for value *p */
462*4882a593Smuzhiyun /* make tables up to required level */
463*4882a593Smuzhiyun while (k > w + l)
464*4882a593Smuzhiyun {
465*4882a593Smuzhiyun DEBG1("1 ");
466*4882a593Smuzhiyun h++;
467*4882a593Smuzhiyun w += l; /* previous table always l bits */
468*4882a593Smuzhiyun
469*4882a593Smuzhiyun /* compute minimum size table less than or equal to l bits */
470*4882a593Smuzhiyun z = (z = g - w) > (unsigned)l ? l : z; /* upper limit on table size */
471*4882a593Smuzhiyun if ((f = 1 << (j = k - w)) > a + 1) /* try a k-w bit table */
472*4882a593Smuzhiyun { /* too few codes for k-w bit table */
473*4882a593Smuzhiyun DEBG1("2 ");
474*4882a593Smuzhiyun f -= a + 1; /* deduct codes from patterns left */
475*4882a593Smuzhiyun xp = c + k;
476*4882a593Smuzhiyun if (j < z)
477*4882a593Smuzhiyun while (++j < z) /* try smaller tables up to z bits */
478*4882a593Smuzhiyun {
479*4882a593Smuzhiyun if ((f <<= 1) <= *++xp)
480*4882a593Smuzhiyun break; /* enough codes to use up j bits */
481*4882a593Smuzhiyun f -= *xp; /* else deduct codes from patterns */
482*4882a593Smuzhiyun }
483*4882a593Smuzhiyun }
484*4882a593Smuzhiyun DEBG1("3 ");
485*4882a593Smuzhiyun z = 1 << j; /* table entries for j-bit table */
486*4882a593Smuzhiyun
487*4882a593Smuzhiyun /* allocate and link in new table */
488*4882a593Smuzhiyun if ((q = (struct huft *)malloc((z + 1)*sizeof(struct huft))) ==
489*4882a593Smuzhiyun (struct huft *)NULL)
490*4882a593Smuzhiyun {
491*4882a593Smuzhiyun if (h)
492*4882a593Smuzhiyun huft_free(u[0]);
493*4882a593Smuzhiyun ret = 3; /* not enough memory */
494*4882a593Smuzhiyun goto out;
495*4882a593Smuzhiyun }
496*4882a593Smuzhiyun DEBG1("4 ");
497*4882a593Smuzhiyun hufts += z + 1; /* track memory usage */
498*4882a593Smuzhiyun *t = q + 1; /* link to list for huft_free() */
499*4882a593Smuzhiyun *(t = &(q->v.t)) = (struct huft *)NULL;
500*4882a593Smuzhiyun u[h] = ++q; /* table starts after link */
501*4882a593Smuzhiyun
502*4882a593Smuzhiyun DEBG1("5 ");
503*4882a593Smuzhiyun /* connect to last table, if there is one */
504*4882a593Smuzhiyun if (h)
505*4882a593Smuzhiyun {
506*4882a593Smuzhiyun x[h] = i; /* save pattern for backing up */
507*4882a593Smuzhiyun r.b = (uch)l; /* bits to dump before this table */
508*4882a593Smuzhiyun r.e = (uch)(16 + j); /* bits in this table */
509*4882a593Smuzhiyun r.v.t = q; /* pointer to this table */
510*4882a593Smuzhiyun j = i >> (w - l); /* (get around Turbo C bug) */
511*4882a593Smuzhiyun u[h-1][j] = r; /* connect to last table */
512*4882a593Smuzhiyun }
513*4882a593Smuzhiyun DEBG1("6 ");
514*4882a593Smuzhiyun }
515*4882a593Smuzhiyun DEBG("h6c ");
516*4882a593Smuzhiyun
517*4882a593Smuzhiyun /* set up table entry in r */
518*4882a593Smuzhiyun r.b = (uch)(k - w);
519*4882a593Smuzhiyun if (p >= v + n)
520*4882a593Smuzhiyun r.e = 99; /* out of values--invalid code */
521*4882a593Smuzhiyun else if (*p < s)
522*4882a593Smuzhiyun {
523*4882a593Smuzhiyun r.e = (uch)(*p < 256 ? 16 : 15); /* 256 is end-of-block code */
524*4882a593Smuzhiyun r.v.n = (ush)(*p); /* simple code is just the value */
525*4882a593Smuzhiyun p++; /* one compiler does not like *p++ */
526*4882a593Smuzhiyun }
527*4882a593Smuzhiyun else
528*4882a593Smuzhiyun {
529*4882a593Smuzhiyun r.e = (uch)e[*p - s]; /* non-simple--look up in lists */
530*4882a593Smuzhiyun r.v.n = d[*p++ - s];
531*4882a593Smuzhiyun }
532*4882a593Smuzhiyun DEBG("h6d ");
533*4882a593Smuzhiyun
534*4882a593Smuzhiyun /* fill code-like entries with r */
535*4882a593Smuzhiyun f = 1 << (k - w);
536*4882a593Smuzhiyun for (j = i >> w; j < z; j += f)
537*4882a593Smuzhiyun q[j] = r;
538*4882a593Smuzhiyun
539*4882a593Smuzhiyun /* backwards increment the k-bit code i */
540*4882a593Smuzhiyun for (j = 1 << (k - 1); i & j; j >>= 1)
541*4882a593Smuzhiyun i ^= j;
542*4882a593Smuzhiyun i ^= j;
543*4882a593Smuzhiyun
544*4882a593Smuzhiyun /* backup over finished tables */
545*4882a593Smuzhiyun while ((i & ((1 << w) - 1)) != x[h])
546*4882a593Smuzhiyun {
547*4882a593Smuzhiyun h--; /* don't need to update q */
548*4882a593Smuzhiyun w -= l;
549*4882a593Smuzhiyun }
550*4882a593Smuzhiyun DEBG("h6e ");
551*4882a593Smuzhiyun }
552*4882a593Smuzhiyun DEBG("h6f ");
553*4882a593Smuzhiyun }
554*4882a593Smuzhiyun
555*4882a593Smuzhiyun DEBG("huft7 ");
556*4882a593Smuzhiyun
557*4882a593Smuzhiyun /* Return true (1) if we were given an incomplete table */
558*4882a593Smuzhiyun ret = y != 0 && g != 1;
559*4882a593Smuzhiyun
560*4882a593Smuzhiyun out:
561*4882a593Smuzhiyun free(stk);
562*4882a593Smuzhiyun return ret;
563*4882a593Smuzhiyun }
564*4882a593Smuzhiyun
565*4882a593Smuzhiyun
566*4882a593Smuzhiyun
huft_free(struct huft * t)567*4882a593Smuzhiyun STATIC int INIT huft_free(
568*4882a593Smuzhiyun struct huft *t /* table to free */
569*4882a593Smuzhiyun )
570*4882a593Smuzhiyun /* Free the malloc'ed tables built by huft_build(), which makes a linked
571*4882a593Smuzhiyun list of the tables it made, with the links in a dummy first entry of
572*4882a593Smuzhiyun each table. */
573*4882a593Smuzhiyun {
574*4882a593Smuzhiyun register struct huft *p, *q;
575*4882a593Smuzhiyun
576*4882a593Smuzhiyun
577*4882a593Smuzhiyun /* Go through linked list, freeing from the malloced (t[-1]) address. */
578*4882a593Smuzhiyun p = t;
579*4882a593Smuzhiyun while (p != (struct huft *)NULL)
580*4882a593Smuzhiyun {
581*4882a593Smuzhiyun q = (--p)->v.t;
582*4882a593Smuzhiyun free((char*)p);
583*4882a593Smuzhiyun p = q;
584*4882a593Smuzhiyun }
585*4882a593Smuzhiyun return 0;
586*4882a593Smuzhiyun }
587*4882a593Smuzhiyun
588*4882a593Smuzhiyun
inflate_codes(struct huft * tl,struct huft * td,int bl,int bd)589*4882a593Smuzhiyun STATIC int INIT inflate_codes(
590*4882a593Smuzhiyun struct huft *tl, /* literal/length decoder tables */
591*4882a593Smuzhiyun struct huft *td, /* distance decoder tables */
592*4882a593Smuzhiyun int bl, /* number of bits decoded by tl[] */
593*4882a593Smuzhiyun int bd /* number of bits decoded by td[] */
594*4882a593Smuzhiyun )
595*4882a593Smuzhiyun /* inflate (decompress) the codes in a deflated (compressed) block.
596*4882a593Smuzhiyun Return an error code or zero if it all goes ok. */
597*4882a593Smuzhiyun {
598*4882a593Smuzhiyun register unsigned e; /* table entry flag/number of extra bits */
599*4882a593Smuzhiyun unsigned n, d; /* length and index for copy */
600*4882a593Smuzhiyun unsigned w; /* current window position */
601*4882a593Smuzhiyun struct huft *t; /* pointer to table entry */
602*4882a593Smuzhiyun unsigned ml, md; /* masks for bl and bd bits */
603*4882a593Smuzhiyun register ulg b; /* bit buffer */
604*4882a593Smuzhiyun register unsigned k; /* number of bits in bit buffer */
605*4882a593Smuzhiyun
606*4882a593Smuzhiyun
607*4882a593Smuzhiyun /* make local copies of globals */
608*4882a593Smuzhiyun b = bb; /* initialize bit buffer */
609*4882a593Smuzhiyun k = bk;
610*4882a593Smuzhiyun w = wp; /* initialize window position */
611*4882a593Smuzhiyun
612*4882a593Smuzhiyun /* inflate the coded data */
613*4882a593Smuzhiyun ml = mask_bits[bl]; /* precompute masks for speed */
614*4882a593Smuzhiyun md = mask_bits[bd];
615*4882a593Smuzhiyun for (;;) /* do until end of block */
616*4882a593Smuzhiyun {
617*4882a593Smuzhiyun NEEDBITS((unsigned)bl)
618*4882a593Smuzhiyun if ((e = (t = tl + ((unsigned)b & ml))->e) > 16)
619*4882a593Smuzhiyun do {
620*4882a593Smuzhiyun if (e == 99)
621*4882a593Smuzhiyun return 1;
622*4882a593Smuzhiyun DUMPBITS(t->b)
623*4882a593Smuzhiyun e -= 16;
624*4882a593Smuzhiyun NEEDBITS(e)
625*4882a593Smuzhiyun } while ((e = (t = t->v.t + ((unsigned)b & mask_bits[e]))->e) > 16);
626*4882a593Smuzhiyun DUMPBITS(t->b)
627*4882a593Smuzhiyun if (e == 16) /* then it's a literal */
628*4882a593Smuzhiyun {
629*4882a593Smuzhiyun slide[w++] = (uch)t->v.n;
630*4882a593Smuzhiyun Tracevv((stderr, "%c", slide[w-1]));
631*4882a593Smuzhiyun if (w == WSIZE)
632*4882a593Smuzhiyun {
633*4882a593Smuzhiyun flush_output(w);
634*4882a593Smuzhiyun w = 0;
635*4882a593Smuzhiyun }
636*4882a593Smuzhiyun }
637*4882a593Smuzhiyun else /* it's an EOB or a length */
638*4882a593Smuzhiyun {
639*4882a593Smuzhiyun /* exit if end of block */
640*4882a593Smuzhiyun if (e == 15)
641*4882a593Smuzhiyun break;
642*4882a593Smuzhiyun
643*4882a593Smuzhiyun /* get length of block to copy */
644*4882a593Smuzhiyun NEEDBITS(e)
645*4882a593Smuzhiyun n = t->v.n + ((unsigned)b & mask_bits[e]);
646*4882a593Smuzhiyun DUMPBITS(e);
647*4882a593Smuzhiyun
648*4882a593Smuzhiyun /* decode distance of block to copy */
649*4882a593Smuzhiyun NEEDBITS((unsigned)bd)
650*4882a593Smuzhiyun if ((e = (t = td + ((unsigned)b & md))->e) > 16)
651*4882a593Smuzhiyun do {
652*4882a593Smuzhiyun if (e == 99)
653*4882a593Smuzhiyun return 1;
654*4882a593Smuzhiyun DUMPBITS(t->b)
655*4882a593Smuzhiyun e -= 16;
656*4882a593Smuzhiyun NEEDBITS(e)
657*4882a593Smuzhiyun } while ((e = (t = t->v.t + ((unsigned)b & mask_bits[e]))->e) > 16);
658*4882a593Smuzhiyun DUMPBITS(t->b)
659*4882a593Smuzhiyun NEEDBITS(e)
660*4882a593Smuzhiyun d = w - t->v.n - ((unsigned)b & mask_bits[e]);
661*4882a593Smuzhiyun DUMPBITS(e)
662*4882a593Smuzhiyun Tracevv((stderr,"\\[%d,%d]", w-d, n));
663*4882a593Smuzhiyun
664*4882a593Smuzhiyun /* do the copy */
665*4882a593Smuzhiyun do {
666*4882a593Smuzhiyun n -= (e = (e = WSIZE - ((d &= WSIZE-1) > w ? d : w)) > n ? n : e);
667*4882a593Smuzhiyun #if !defined(NOMEMCPY) && !defined(DEBUG)
668*4882a593Smuzhiyun if (w - d >= e) /* (this test assumes unsigned comparison) */
669*4882a593Smuzhiyun {
670*4882a593Smuzhiyun memcpy(slide + w, slide + d, e);
671*4882a593Smuzhiyun w += e;
672*4882a593Smuzhiyun d += e;
673*4882a593Smuzhiyun }
674*4882a593Smuzhiyun else /* do it slow to avoid memcpy() overlap */
675*4882a593Smuzhiyun #endif /* !NOMEMCPY */
676*4882a593Smuzhiyun do {
677*4882a593Smuzhiyun slide[w++] = slide[d++];
678*4882a593Smuzhiyun Tracevv((stderr, "%c", slide[w-1]));
679*4882a593Smuzhiyun } while (--e);
680*4882a593Smuzhiyun if (w == WSIZE)
681*4882a593Smuzhiyun {
682*4882a593Smuzhiyun flush_output(w);
683*4882a593Smuzhiyun w = 0;
684*4882a593Smuzhiyun }
685*4882a593Smuzhiyun } while (n);
686*4882a593Smuzhiyun }
687*4882a593Smuzhiyun }
688*4882a593Smuzhiyun
689*4882a593Smuzhiyun
690*4882a593Smuzhiyun /* restore the globals from the locals */
691*4882a593Smuzhiyun wp = w; /* restore global window pointer */
692*4882a593Smuzhiyun bb = b; /* restore global bit buffer */
693*4882a593Smuzhiyun bk = k;
694*4882a593Smuzhiyun
695*4882a593Smuzhiyun /* done */
696*4882a593Smuzhiyun return 0;
697*4882a593Smuzhiyun
698*4882a593Smuzhiyun underrun:
699*4882a593Smuzhiyun return 4; /* Input underrun */
700*4882a593Smuzhiyun }
701*4882a593Smuzhiyun
702*4882a593Smuzhiyun
703*4882a593Smuzhiyun
inflate_stored(void)704*4882a593Smuzhiyun STATIC int INIT inflate_stored(void)
705*4882a593Smuzhiyun /* "decompress" an inflated type 0 (stored) block. */
706*4882a593Smuzhiyun {
707*4882a593Smuzhiyun unsigned n; /* number of bytes in block */
708*4882a593Smuzhiyun unsigned w; /* current window position */
709*4882a593Smuzhiyun register ulg b; /* bit buffer */
710*4882a593Smuzhiyun register unsigned k; /* number of bits in bit buffer */
711*4882a593Smuzhiyun
712*4882a593Smuzhiyun DEBG("<stor");
713*4882a593Smuzhiyun
714*4882a593Smuzhiyun /* make local copies of globals */
715*4882a593Smuzhiyun b = bb; /* initialize bit buffer */
716*4882a593Smuzhiyun k = bk;
717*4882a593Smuzhiyun w = wp; /* initialize window position */
718*4882a593Smuzhiyun
719*4882a593Smuzhiyun
720*4882a593Smuzhiyun /* go to byte boundary */
721*4882a593Smuzhiyun n = k & 7;
722*4882a593Smuzhiyun DUMPBITS(n);
723*4882a593Smuzhiyun
724*4882a593Smuzhiyun
725*4882a593Smuzhiyun /* get the length and its complement */
726*4882a593Smuzhiyun NEEDBITS(16)
727*4882a593Smuzhiyun n = ((unsigned)b & 0xffff);
728*4882a593Smuzhiyun DUMPBITS(16)
729*4882a593Smuzhiyun NEEDBITS(16)
730*4882a593Smuzhiyun if (n != (unsigned)((~b) & 0xffff))
731*4882a593Smuzhiyun return 1; /* error in compressed data */
732*4882a593Smuzhiyun DUMPBITS(16)
733*4882a593Smuzhiyun
734*4882a593Smuzhiyun
735*4882a593Smuzhiyun /* read and output the compressed data */
736*4882a593Smuzhiyun while (n--)
737*4882a593Smuzhiyun {
738*4882a593Smuzhiyun NEEDBITS(8)
739*4882a593Smuzhiyun slide[w++] = (uch)b;
740*4882a593Smuzhiyun if (w == WSIZE)
741*4882a593Smuzhiyun {
742*4882a593Smuzhiyun flush_output(w);
743*4882a593Smuzhiyun w = 0;
744*4882a593Smuzhiyun }
745*4882a593Smuzhiyun DUMPBITS(8)
746*4882a593Smuzhiyun }
747*4882a593Smuzhiyun
748*4882a593Smuzhiyun
749*4882a593Smuzhiyun /* restore the globals from the locals */
750*4882a593Smuzhiyun wp = w; /* restore global window pointer */
751*4882a593Smuzhiyun bb = b; /* restore global bit buffer */
752*4882a593Smuzhiyun bk = k;
753*4882a593Smuzhiyun
754*4882a593Smuzhiyun DEBG(">");
755*4882a593Smuzhiyun return 0;
756*4882a593Smuzhiyun
757*4882a593Smuzhiyun underrun:
758*4882a593Smuzhiyun return 4; /* Input underrun */
759*4882a593Smuzhiyun }
760*4882a593Smuzhiyun
761*4882a593Smuzhiyun
762*4882a593Smuzhiyun /*
763*4882a593Smuzhiyun * We use `noinline' here to prevent gcc-3.5 from using too much stack space
764*4882a593Smuzhiyun */
inflate_fixed(void)765*4882a593Smuzhiyun STATIC int noinline INIT inflate_fixed(void)
766*4882a593Smuzhiyun /* decompress an inflated type 1 (fixed Huffman codes) block. We should
767*4882a593Smuzhiyun either replace this with a custom decoder, or at least precompute the
768*4882a593Smuzhiyun Huffman tables. */
769*4882a593Smuzhiyun {
770*4882a593Smuzhiyun int i; /* temporary variable */
771*4882a593Smuzhiyun struct huft *tl; /* literal/length code table */
772*4882a593Smuzhiyun struct huft *td; /* distance code table */
773*4882a593Smuzhiyun int bl; /* lookup bits for tl */
774*4882a593Smuzhiyun int bd; /* lookup bits for td */
775*4882a593Smuzhiyun unsigned *l; /* length list for huft_build */
776*4882a593Smuzhiyun
777*4882a593Smuzhiyun DEBG("<fix");
778*4882a593Smuzhiyun
779*4882a593Smuzhiyun l = malloc(sizeof(*l) * 288);
780*4882a593Smuzhiyun if (l == NULL)
781*4882a593Smuzhiyun return 3; /* out of memory */
782*4882a593Smuzhiyun
783*4882a593Smuzhiyun /* set up literal table */
784*4882a593Smuzhiyun for (i = 0; i < 144; i++)
785*4882a593Smuzhiyun l[i] = 8;
786*4882a593Smuzhiyun for (; i < 256; i++)
787*4882a593Smuzhiyun l[i] = 9;
788*4882a593Smuzhiyun for (; i < 280; i++)
789*4882a593Smuzhiyun l[i] = 7;
790*4882a593Smuzhiyun for (; i < 288; i++) /* make a complete, but wrong code set */
791*4882a593Smuzhiyun l[i] = 8;
792*4882a593Smuzhiyun bl = 7;
793*4882a593Smuzhiyun if ((i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl)) != 0) {
794*4882a593Smuzhiyun free(l);
795*4882a593Smuzhiyun return i;
796*4882a593Smuzhiyun }
797*4882a593Smuzhiyun
798*4882a593Smuzhiyun /* set up distance table */
799*4882a593Smuzhiyun for (i = 0; i < 30; i++) /* make an incomplete code set */
800*4882a593Smuzhiyun l[i] = 5;
801*4882a593Smuzhiyun bd = 5;
802*4882a593Smuzhiyun if ((i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd)) > 1)
803*4882a593Smuzhiyun {
804*4882a593Smuzhiyun huft_free(tl);
805*4882a593Smuzhiyun free(l);
806*4882a593Smuzhiyun
807*4882a593Smuzhiyun DEBG(">");
808*4882a593Smuzhiyun return i;
809*4882a593Smuzhiyun }
810*4882a593Smuzhiyun
811*4882a593Smuzhiyun
812*4882a593Smuzhiyun /* decompress until an end-of-block code */
813*4882a593Smuzhiyun if (inflate_codes(tl, td, bl, bd)) {
814*4882a593Smuzhiyun free(l);
815*4882a593Smuzhiyun return 1;
816*4882a593Smuzhiyun }
817*4882a593Smuzhiyun
818*4882a593Smuzhiyun /* free the decoding tables, return */
819*4882a593Smuzhiyun free(l);
820*4882a593Smuzhiyun huft_free(tl);
821*4882a593Smuzhiyun huft_free(td);
822*4882a593Smuzhiyun return 0;
823*4882a593Smuzhiyun }
824*4882a593Smuzhiyun
825*4882a593Smuzhiyun
826*4882a593Smuzhiyun /*
827*4882a593Smuzhiyun * We use `noinline' here to prevent gcc-3.5 from using too much stack space
828*4882a593Smuzhiyun */
inflate_dynamic(void)829*4882a593Smuzhiyun STATIC int noinline INIT inflate_dynamic(void)
830*4882a593Smuzhiyun /* decompress an inflated type 2 (dynamic Huffman codes) block. */
831*4882a593Smuzhiyun {
832*4882a593Smuzhiyun int i; /* temporary variables */
833*4882a593Smuzhiyun unsigned j;
834*4882a593Smuzhiyun unsigned l; /* last length */
835*4882a593Smuzhiyun unsigned m; /* mask for bit lengths table */
836*4882a593Smuzhiyun unsigned n; /* number of lengths to get */
837*4882a593Smuzhiyun struct huft *tl; /* literal/length code table */
838*4882a593Smuzhiyun struct huft *td; /* distance code table */
839*4882a593Smuzhiyun int bl; /* lookup bits for tl */
840*4882a593Smuzhiyun int bd; /* lookup bits for td */
841*4882a593Smuzhiyun unsigned nb; /* number of bit length codes */
842*4882a593Smuzhiyun unsigned nl; /* number of literal/length codes */
843*4882a593Smuzhiyun unsigned nd; /* number of distance codes */
844*4882a593Smuzhiyun unsigned *ll; /* literal/length and distance code lengths */
845*4882a593Smuzhiyun register ulg b; /* bit buffer */
846*4882a593Smuzhiyun register unsigned k; /* number of bits in bit buffer */
847*4882a593Smuzhiyun int ret;
848*4882a593Smuzhiyun
849*4882a593Smuzhiyun DEBG("<dyn");
850*4882a593Smuzhiyun
851*4882a593Smuzhiyun #ifdef PKZIP_BUG_WORKAROUND
852*4882a593Smuzhiyun ll = malloc(sizeof(*ll) * (288+32)); /* literal/length and distance code lengths */
853*4882a593Smuzhiyun #else
854*4882a593Smuzhiyun ll = malloc(sizeof(*ll) * (286+30)); /* literal/length and distance code lengths */
855*4882a593Smuzhiyun #endif
856*4882a593Smuzhiyun
857*4882a593Smuzhiyun if (ll == NULL)
858*4882a593Smuzhiyun return 1;
859*4882a593Smuzhiyun
860*4882a593Smuzhiyun /* make local bit buffer */
861*4882a593Smuzhiyun b = bb;
862*4882a593Smuzhiyun k = bk;
863*4882a593Smuzhiyun
864*4882a593Smuzhiyun
865*4882a593Smuzhiyun /* read in table lengths */
866*4882a593Smuzhiyun NEEDBITS(5)
867*4882a593Smuzhiyun nl = 257 + ((unsigned)b & 0x1f); /* number of literal/length codes */
868*4882a593Smuzhiyun DUMPBITS(5)
869*4882a593Smuzhiyun NEEDBITS(5)
870*4882a593Smuzhiyun nd = 1 + ((unsigned)b & 0x1f); /* number of distance codes */
871*4882a593Smuzhiyun DUMPBITS(5)
872*4882a593Smuzhiyun NEEDBITS(4)
873*4882a593Smuzhiyun nb = 4 + ((unsigned)b & 0xf); /* number of bit length codes */
874*4882a593Smuzhiyun DUMPBITS(4)
875*4882a593Smuzhiyun #ifdef PKZIP_BUG_WORKAROUND
876*4882a593Smuzhiyun if (nl > 288 || nd > 32)
877*4882a593Smuzhiyun #else
878*4882a593Smuzhiyun if (nl > 286 || nd > 30)
879*4882a593Smuzhiyun #endif
880*4882a593Smuzhiyun {
881*4882a593Smuzhiyun ret = 1; /* bad lengths */
882*4882a593Smuzhiyun goto out;
883*4882a593Smuzhiyun }
884*4882a593Smuzhiyun
885*4882a593Smuzhiyun DEBG("dyn1 ");
886*4882a593Smuzhiyun
887*4882a593Smuzhiyun /* read in bit-length-code lengths */
888*4882a593Smuzhiyun for (j = 0; j < nb; j++)
889*4882a593Smuzhiyun {
890*4882a593Smuzhiyun NEEDBITS(3)
891*4882a593Smuzhiyun ll[border[j]] = (unsigned)b & 7;
892*4882a593Smuzhiyun DUMPBITS(3)
893*4882a593Smuzhiyun }
894*4882a593Smuzhiyun for (; j < 19; j++)
895*4882a593Smuzhiyun ll[border[j]] = 0;
896*4882a593Smuzhiyun
897*4882a593Smuzhiyun DEBG("dyn2 ");
898*4882a593Smuzhiyun
899*4882a593Smuzhiyun /* build decoding table for trees--single level, 7 bit lookup */
900*4882a593Smuzhiyun bl = 7;
901*4882a593Smuzhiyun if ((i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl)) != 0)
902*4882a593Smuzhiyun {
903*4882a593Smuzhiyun if (i == 1)
904*4882a593Smuzhiyun huft_free(tl);
905*4882a593Smuzhiyun ret = i; /* incomplete code set */
906*4882a593Smuzhiyun goto out;
907*4882a593Smuzhiyun }
908*4882a593Smuzhiyun
909*4882a593Smuzhiyun DEBG("dyn3 ");
910*4882a593Smuzhiyun
911*4882a593Smuzhiyun /* read in literal and distance code lengths */
912*4882a593Smuzhiyun n = nl + nd;
913*4882a593Smuzhiyun m = mask_bits[bl];
914*4882a593Smuzhiyun i = l = 0;
915*4882a593Smuzhiyun while ((unsigned)i < n)
916*4882a593Smuzhiyun {
917*4882a593Smuzhiyun NEEDBITS((unsigned)bl)
918*4882a593Smuzhiyun j = (td = tl + ((unsigned)b & m))->b;
919*4882a593Smuzhiyun DUMPBITS(j)
920*4882a593Smuzhiyun j = td->v.n;
921*4882a593Smuzhiyun if (j < 16) /* length of code in bits (0..15) */
922*4882a593Smuzhiyun ll[i++] = l = j; /* save last length in l */
923*4882a593Smuzhiyun else if (j == 16) /* repeat last length 3 to 6 times */
924*4882a593Smuzhiyun {
925*4882a593Smuzhiyun NEEDBITS(2)
926*4882a593Smuzhiyun j = 3 + ((unsigned)b & 3);
927*4882a593Smuzhiyun DUMPBITS(2)
928*4882a593Smuzhiyun if ((unsigned)i + j > n) {
929*4882a593Smuzhiyun ret = 1;
930*4882a593Smuzhiyun goto out;
931*4882a593Smuzhiyun }
932*4882a593Smuzhiyun while (j--)
933*4882a593Smuzhiyun ll[i++] = l;
934*4882a593Smuzhiyun }
935*4882a593Smuzhiyun else if (j == 17) /* 3 to 10 zero length codes */
936*4882a593Smuzhiyun {
937*4882a593Smuzhiyun NEEDBITS(3)
938*4882a593Smuzhiyun j = 3 + ((unsigned)b & 7);
939*4882a593Smuzhiyun DUMPBITS(3)
940*4882a593Smuzhiyun if ((unsigned)i + j > n) {
941*4882a593Smuzhiyun ret = 1;
942*4882a593Smuzhiyun goto out;
943*4882a593Smuzhiyun }
944*4882a593Smuzhiyun while (j--)
945*4882a593Smuzhiyun ll[i++] = 0;
946*4882a593Smuzhiyun l = 0;
947*4882a593Smuzhiyun }
948*4882a593Smuzhiyun else /* j == 18: 11 to 138 zero length codes */
949*4882a593Smuzhiyun {
950*4882a593Smuzhiyun NEEDBITS(7)
951*4882a593Smuzhiyun j = 11 + ((unsigned)b & 0x7f);
952*4882a593Smuzhiyun DUMPBITS(7)
953*4882a593Smuzhiyun if ((unsigned)i + j > n) {
954*4882a593Smuzhiyun ret = 1;
955*4882a593Smuzhiyun goto out;
956*4882a593Smuzhiyun }
957*4882a593Smuzhiyun while (j--)
958*4882a593Smuzhiyun ll[i++] = 0;
959*4882a593Smuzhiyun l = 0;
960*4882a593Smuzhiyun }
961*4882a593Smuzhiyun }
962*4882a593Smuzhiyun
963*4882a593Smuzhiyun DEBG("dyn4 ");
964*4882a593Smuzhiyun
965*4882a593Smuzhiyun /* free decoding table for trees */
966*4882a593Smuzhiyun huft_free(tl);
967*4882a593Smuzhiyun
968*4882a593Smuzhiyun DEBG("dyn5 ");
969*4882a593Smuzhiyun
970*4882a593Smuzhiyun /* restore the global bit buffer */
971*4882a593Smuzhiyun bb = b;
972*4882a593Smuzhiyun bk = k;
973*4882a593Smuzhiyun
974*4882a593Smuzhiyun DEBG("dyn5a ");
975*4882a593Smuzhiyun
976*4882a593Smuzhiyun /* build the decoding tables for literal/length and distance codes */
977*4882a593Smuzhiyun bl = lbits;
978*4882a593Smuzhiyun if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0)
979*4882a593Smuzhiyun {
980*4882a593Smuzhiyun DEBG("dyn5b ");
981*4882a593Smuzhiyun if (i == 1) {
982*4882a593Smuzhiyun error("incomplete literal tree");
983*4882a593Smuzhiyun huft_free(tl);
984*4882a593Smuzhiyun }
985*4882a593Smuzhiyun ret = i; /* incomplete code set */
986*4882a593Smuzhiyun goto out;
987*4882a593Smuzhiyun }
988*4882a593Smuzhiyun DEBG("dyn5c ");
989*4882a593Smuzhiyun bd = dbits;
990*4882a593Smuzhiyun if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0)
991*4882a593Smuzhiyun {
992*4882a593Smuzhiyun DEBG("dyn5d ");
993*4882a593Smuzhiyun if (i == 1) {
994*4882a593Smuzhiyun error("incomplete distance tree");
995*4882a593Smuzhiyun #ifdef PKZIP_BUG_WORKAROUND
996*4882a593Smuzhiyun i = 0;
997*4882a593Smuzhiyun }
998*4882a593Smuzhiyun #else
999*4882a593Smuzhiyun huft_free(td);
1000*4882a593Smuzhiyun }
1001*4882a593Smuzhiyun huft_free(tl);
1002*4882a593Smuzhiyun ret = i; /* incomplete code set */
1003*4882a593Smuzhiyun goto out;
1004*4882a593Smuzhiyun #endif
1005*4882a593Smuzhiyun }
1006*4882a593Smuzhiyun
1007*4882a593Smuzhiyun DEBG("dyn6 ");
1008*4882a593Smuzhiyun
1009*4882a593Smuzhiyun /* decompress until an end-of-block code */
1010*4882a593Smuzhiyun if (inflate_codes(tl, td, bl, bd)) {
1011*4882a593Smuzhiyun ret = 1;
1012*4882a593Smuzhiyun goto out;
1013*4882a593Smuzhiyun }
1014*4882a593Smuzhiyun
1015*4882a593Smuzhiyun DEBG("dyn7 ");
1016*4882a593Smuzhiyun
1017*4882a593Smuzhiyun /* free the decoding tables, return */
1018*4882a593Smuzhiyun huft_free(tl);
1019*4882a593Smuzhiyun huft_free(td);
1020*4882a593Smuzhiyun
1021*4882a593Smuzhiyun DEBG(">");
1022*4882a593Smuzhiyun ret = 0;
1023*4882a593Smuzhiyun out:
1024*4882a593Smuzhiyun free(ll);
1025*4882a593Smuzhiyun return ret;
1026*4882a593Smuzhiyun
1027*4882a593Smuzhiyun underrun:
1028*4882a593Smuzhiyun ret = 4; /* Input underrun */
1029*4882a593Smuzhiyun goto out;
1030*4882a593Smuzhiyun }
1031*4882a593Smuzhiyun
1032*4882a593Smuzhiyun
1033*4882a593Smuzhiyun
inflate_block(int * e)1034*4882a593Smuzhiyun STATIC int INIT inflate_block(
1035*4882a593Smuzhiyun int *e /* last block flag */
1036*4882a593Smuzhiyun )
1037*4882a593Smuzhiyun /* decompress an inflated block */
1038*4882a593Smuzhiyun {
1039*4882a593Smuzhiyun unsigned t; /* block type */
1040*4882a593Smuzhiyun register ulg b; /* bit buffer */
1041*4882a593Smuzhiyun register unsigned k; /* number of bits in bit buffer */
1042*4882a593Smuzhiyun
1043*4882a593Smuzhiyun DEBG("<blk");
1044*4882a593Smuzhiyun
1045*4882a593Smuzhiyun /* make local bit buffer */
1046*4882a593Smuzhiyun b = bb;
1047*4882a593Smuzhiyun k = bk;
1048*4882a593Smuzhiyun
1049*4882a593Smuzhiyun
1050*4882a593Smuzhiyun /* read in last block bit */
1051*4882a593Smuzhiyun NEEDBITS(1)
1052*4882a593Smuzhiyun *e = (int)b & 1;
1053*4882a593Smuzhiyun DUMPBITS(1)
1054*4882a593Smuzhiyun
1055*4882a593Smuzhiyun
1056*4882a593Smuzhiyun /* read in block type */
1057*4882a593Smuzhiyun NEEDBITS(2)
1058*4882a593Smuzhiyun t = (unsigned)b & 3;
1059*4882a593Smuzhiyun DUMPBITS(2)
1060*4882a593Smuzhiyun
1061*4882a593Smuzhiyun
1062*4882a593Smuzhiyun /* restore the global bit buffer */
1063*4882a593Smuzhiyun bb = b;
1064*4882a593Smuzhiyun bk = k;
1065*4882a593Smuzhiyun
1066*4882a593Smuzhiyun /* inflate that block type */
1067*4882a593Smuzhiyun if (t == 2)
1068*4882a593Smuzhiyun return inflate_dynamic();
1069*4882a593Smuzhiyun if (t == 0)
1070*4882a593Smuzhiyun return inflate_stored();
1071*4882a593Smuzhiyun if (t == 1)
1072*4882a593Smuzhiyun return inflate_fixed();
1073*4882a593Smuzhiyun
1074*4882a593Smuzhiyun DEBG(">");
1075*4882a593Smuzhiyun
1076*4882a593Smuzhiyun /* bad block type */
1077*4882a593Smuzhiyun return 2;
1078*4882a593Smuzhiyun
1079*4882a593Smuzhiyun underrun:
1080*4882a593Smuzhiyun return 4; /* Input underrun */
1081*4882a593Smuzhiyun }
1082*4882a593Smuzhiyun
1083*4882a593Smuzhiyun
1084*4882a593Smuzhiyun
inflate(void)1085*4882a593Smuzhiyun STATIC int INIT inflate(void)
1086*4882a593Smuzhiyun /* decompress an inflated entry */
1087*4882a593Smuzhiyun {
1088*4882a593Smuzhiyun int e; /* last block flag */
1089*4882a593Smuzhiyun int r; /* result code */
1090*4882a593Smuzhiyun unsigned h; /* maximum struct huft's malloc'ed */
1091*4882a593Smuzhiyun
1092*4882a593Smuzhiyun /* initialize window, bit buffer */
1093*4882a593Smuzhiyun wp = 0;
1094*4882a593Smuzhiyun bk = 0;
1095*4882a593Smuzhiyun bb = 0;
1096*4882a593Smuzhiyun
1097*4882a593Smuzhiyun
1098*4882a593Smuzhiyun /* decompress until the last block */
1099*4882a593Smuzhiyun h = 0;
1100*4882a593Smuzhiyun do {
1101*4882a593Smuzhiyun hufts = 0;
1102*4882a593Smuzhiyun #ifdef ARCH_HAS_DECOMP_WDOG
1103*4882a593Smuzhiyun arch_decomp_wdog();
1104*4882a593Smuzhiyun #endif
1105*4882a593Smuzhiyun r = inflate_block(&e);
1106*4882a593Smuzhiyun if (r)
1107*4882a593Smuzhiyun return r;
1108*4882a593Smuzhiyun if (hufts > h)
1109*4882a593Smuzhiyun h = hufts;
1110*4882a593Smuzhiyun } while (!e);
1111*4882a593Smuzhiyun
1112*4882a593Smuzhiyun /* Undo too much lookahead. The next read will be byte aligned so we
1113*4882a593Smuzhiyun * can discard unused bits in the last meaningful byte.
1114*4882a593Smuzhiyun */
1115*4882a593Smuzhiyun while (bk >= 8) {
1116*4882a593Smuzhiyun bk -= 8;
1117*4882a593Smuzhiyun inptr--;
1118*4882a593Smuzhiyun }
1119*4882a593Smuzhiyun
1120*4882a593Smuzhiyun /* flush out slide */
1121*4882a593Smuzhiyun flush_output(wp);
1122*4882a593Smuzhiyun
1123*4882a593Smuzhiyun
1124*4882a593Smuzhiyun /* return success */
1125*4882a593Smuzhiyun #ifdef DEBUG
1126*4882a593Smuzhiyun fprintf(stderr, "<%u> ", h);
1127*4882a593Smuzhiyun #endif /* DEBUG */
1128*4882a593Smuzhiyun return 0;
1129*4882a593Smuzhiyun }
1130*4882a593Smuzhiyun
1131*4882a593Smuzhiyun /**********************************************************************
1132*4882a593Smuzhiyun *
1133*4882a593Smuzhiyun * The following are support routines for inflate.c
1134*4882a593Smuzhiyun *
1135*4882a593Smuzhiyun **********************************************************************/
1136*4882a593Smuzhiyun
1137*4882a593Smuzhiyun static ulg crc_32_tab[256];
1138*4882a593Smuzhiyun static ulg crc; /* initialized in makecrc() so it'll reside in bss */
1139*4882a593Smuzhiyun #define CRC_VALUE (crc ^ 0xffffffffUL)
1140*4882a593Smuzhiyun
1141*4882a593Smuzhiyun /*
1142*4882a593Smuzhiyun * Code to compute the CRC-32 table. Borrowed from
1143*4882a593Smuzhiyun * gzip-1.0.3/makecrc.c.
1144*4882a593Smuzhiyun */
1145*4882a593Smuzhiyun
1146*4882a593Smuzhiyun static void INIT
makecrc(void)1147*4882a593Smuzhiyun makecrc(void)
1148*4882a593Smuzhiyun {
1149*4882a593Smuzhiyun /* Not copyrighted 1990 Mark Adler */
1150*4882a593Smuzhiyun
1151*4882a593Smuzhiyun unsigned long c; /* crc shift register */
1152*4882a593Smuzhiyun unsigned long e; /* polynomial exclusive-or pattern */
1153*4882a593Smuzhiyun int i; /* counter for all possible eight bit values */
1154*4882a593Smuzhiyun int k; /* byte being shifted into crc apparatus */
1155*4882a593Smuzhiyun
1156*4882a593Smuzhiyun /* terms of polynomial defining this crc (except x^32): */
1157*4882a593Smuzhiyun static const int p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
1158*4882a593Smuzhiyun
1159*4882a593Smuzhiyun /* Make exclusive-or pattern from polynomial */
1160*4882a593Smuzhiyun e = 0;
1161*4882a593Smuzhiyun for (i = 0; i < sizeof(p)/sizeof(int); i++)
1162*4882a593Smuzhiyun e |= 1L << (31 - p[i]);
1163*4882a593Smuzhiyun
1164*4882a593Smuzhiyun crc_32_tab[0] = 0;
1165*4882a593Smuzhiyun
1166*4882a593Smuzhiyun for (i = 1; i < 256; i++)
1167*4882a593Smuzhiyun {
1168*4882a593Smuzhiyun c = 0;
1169*4882a593Smuzhiyun for (k = i | 256; k != 1; k >>= 1)
1170*4882a593Smuzhiyun {
1171*4882a593Smuzhiyun c = c & 1 ? (c >> 1) ^ e : c >> 1;
1172*4882a593Smuzhiyun if (k & 1)
1173*4882a593Smuzhiyun c ^= e;
1174*4882a593Smuzhiyun }
1175*4882a593Smuzhiyun crc_32_tab[i] = c;
1176*4882a593Smuzhiyun }
1177*4882a593Smuzhiyun
1178*4882a593Smuzhiyun /* this is initialized here so this code could reside in ROM */
1179*4882a593Smuzhiyun crc = (ulg)0xffffffffUL; /* shift register contents */
1180*4882a593Smuzhiyun }
1181*4882a593Smuzhiyun
1182*4882a593Smuzhiyun /* gzip flag byte */
1183*4882a593Smuzhiyun #define ASCII_FLAG 0x01 /* bit 0 set: file probably ASCII text */
1184*4882a593Smuzhiyun #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
1185*4882a593Smuzhiyun #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
1186*4882a593Smuzhiyun #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
1187*4882a593Smuzhiyun #define COMMENT 0x10 /* bit 4 set: file comment present */
1188*4882a593Smuzhiyun #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
1189*4882a593Smuzhiyun #define RESERVED 0xC0 /* bit 6,7: reserved */
1190*4882a593Smuzhiyun
1191*4882a593Smuzhiyun /*
1192*4882a593Smuzhiyun * Do the uncompression!
1193*4882a593Smuzhiyun */
gunzip(void)1194*4882a593Smuzhiyun static int INIT gunzip(void)
1195*4882a593Smuzhiyun {
1196*4882a593Smuzhiyun uch flags;
1197*4882a593Smuzhiyun unsigned char magic[2]; /* magic header */
1198*4882a593Smuzhiyun char method;
1199*4882a593Smuzhiyun ulg orig_crc = 0; /* original crc */
1200*4882a593Smuzhiyun ulg orig_len = 0; /* original uncompressed length */
1201*4882a593Smuzhiyun int res;
1202*4882a593Smuzhiyun
1203*4882a593Smuzhiyun magic[0] = NEXTBYTE();
1204*4882a593Smuzhiyun magic[1] = NEXTBYTE();
1205*4882a593Smuzhiyun method = NEXTBYTE();
1206*4882a593Smuzhiyun
1207*4882a593Smuzhiyun if (magic[0] != 037 ||
1208*4882a593Smuzhiyun ((magic[1] != 0213) && (magic[1] != 0236))) {
1209*4882a593Smuzhiyun error("bad gzip magic numbers");
1210*4882a593Smuzhiyun return -1;
1211*4882a593Smuzhiyun }
1212*4882a593Smuzhiyun
1213*4882a593Smuzhiyun /* We only support method #8, DEFLATED */
1214*4882a593Smuzhiyun if (method != 8) {
1215*4882a593Smuzhiyun error("internal error, invalid method");
1216*4882a593Smuzhiyun return -1;
1217*4882a593Smuzhiyun }
1218*4882a593Smuzhiyun
1219*4882a593Smuzhiyun flags = (uch)get_byte();
1220*4882a593Smuzhiyun if ((flags & ENCRYPTED) != 0) {
1221*4882a593Smuzhiyun error("Input is encrypted");
1222*4882a593Smuzhiyun return -1;
1223*4882a593Smuzhiyun }
1224*4882a593Smuzhiyun if ((flags & CONTINUATION) != 0) {
1225*4882a593Smuzhiyun error("Multi part input");
1226*4882a593Smuzhiyun return -1;
1227*4882a593Smuzhiyun }
1228*4882a593Smuzhiyun if ((flags & RESERVED) != 0) {
1229*4882a593Smuzhiyun error("Input has invalid flags");
1230*4882a593Smuzhiyun return -1;
1231*4882a593Smuzhiyun }
1232*4882a593Smuzhiyun NEXTBYTE(); /* Get timestamp */
1233*4882a593Smuzhiyun NEXTBYTE();
1234*4882a593Smuzhiyun NEXTBYTE();
1235*4882a593Smuzhiyun NEXTBYTE();
1236*4882a593Smuzhiyun
1237*4882a593Smuzhiyun (void)NEXTBYTE(); /* Ignore extra flags for the moment */
1238*4882a593Smuzhiyun (void)NEXTBYTE(); /* Ignore OS type for the moment */
1239*4882a593Smuzhiyun
1240*4882a593Smuzhiyun if ((flags & EXTRA_FIELD) != 0) {
1241*4882a593Smuzhiyun unsigned len = (unsigned)NEXTBYTE();
1242*4882a593Smuzhiyun len |= ((unsigned)NEXTBYTE())<<8;
1243*4882a593Smuzhiyun while (len--) (void)NEXTBYTE();
1244*4882a593Smuzhiyun }
1245*4882a593Smuzhiyun
1246*4882a593Smuzhiyun /* Get original file name if it was truncated */
1247*4882a593Smuzhiyun if ((flags & ORIG_NAME) != 0) {
1248*4882a593Smuzhiyun /* Discard the old name */
1249*4882a593Smuzhiyun while (NEXTBYTE() != 0) /* null */ ;
1250*4882a593Smuzhiyun }
1251*4882a593Smuzhiyun
1252*4882a593Smuzhiyun /* Discard file comment if any */
1253*4882a593Smuzhiyun if ((flags & COMMENT) != 0) {
1254*4882a593Smuzhiyun while (NEXTBYTE() != 0) /* null */ ;
1255*4882a593Smuzhiyun }
1256*4882a593Smuzhiyun
1257*4882a593Smuzhiyun /* Decompress */
1258*4882a593Smuzhiyun if ((res = inflate())) {
1259*4882a593Smuzhiyun switch (res) {
1260*4882a593Smuzhiyun case 0:
1261*4882a593Smuzhiyun break;
1262*4882a593Smuzhiyun case 1:
1263*4882a593Smuzhiyun error("invalid compressed format (err=1)");
1264*4882a593Smuzhiyun break;
1265*4882a593Smuzhiyun case 2:
1266*4882a593Smuzhiyun error("invalid compressed format (err=2)");
1267*4882a593Smuzhiyun break;
1268*4882a593Smuzhiyun case 3:
1269*4882a593Smuzhiyun error("out of memory");
1270*4882a593Smuzhiyun break;
1271*4882a593Smuzhiyun case 4:
1272*4882a593Smuzhiyun error("out of input data");
1273*4882a593Smuzhiyun break;
1274*4882a593Smuzhiyun default:
1275*4882a593Smuzhiyun error("invalid compressed format (other)");
1276*4882a593Smuzhiyun }
1277*4882a593Smuzhiyun return -1;
1278*4882a593Smuzhiyun }
1279*4882a593Smuzhiyun
1280*4882a593Smuzhiyun /* Get the crc and original length */
1281*4882a593Smuzhiyun /* crc32 (see algorithm.doc)
1282*4882a593Smuzhiyun * uncompressed input size modulo 2^32
1283*4882a593Smuzhiyun */
1284*4882a593Smuzhiyun orig_crc = (ulg) NEXTBYTE();
1285*4882a593Smuzhiyun orig_crc |= (ulg) NEXTBYTE() << 8;
1286*4882a593Smuzhiyun orig_crc |= (ulg) NEXTBYTE() << 16;
1287*4882a593Smuzhiyun orig_crc |= (ulg) NEXTBYTE() << 24;
1288*4882a593Smuzhiyun
1289*4882a593Smuzhiyun orig_len = (ulg) NEXTBYTE();
1290*4882a593Smuzhiyun orig_len |= (ulg) NEXTBYTE() << 8;
1291*4882a593Smuzhiyun orig_len |= (ulg) NEXTBYTE() << 16;
1292*4882a593Smuzhiyun orig_len |= (ulg) NEXTBYTE() << 24;
1293*4882a593Smuzhiyun
1294*4882a593Smuzhiyun /* Validate decompression */
1295*4882a593Smuzhiyun if (orig_crc != CRC_VALUE) {
1296*4882a593Smuzhiyun error("crc error");
1297*4882a593Smuzhiyun return -1;
1298*4882a593Smuzhiyun }
1299*4882a593Smuzhiyun if (orig_len != bytes_out) {
1300*4882a593Smuzhiyun error("length error");
1301*4882a593Smuzhiyun return -1;
1302*4882a593Smuzhiyun }
1303*4882a593Smuzhiyun return 0;
1304*4882a593Smuzhiyun
1305*4882a593Smuzhiyun underrun: /* NEXTBYTE() goto's here if needed */
1306*4882a593Smuzhiyun error("out of input data");
1307*4882a593Smuzhiyun return -1;
1308*4882a593Smuzhiyun }
1309*4882a593Smuzhiyun
1310*4882a593Smuzhiyun
1311