ia64/xen-unstable

view xen/common/inflate.c @ 19835:edfdeb150f27

Fix buildsystem to detect udev > version 124

udev removed the udevinfo symlink from versions higher than 123 and
xen's build-system could not detect if udev is in place and has the
required version.

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