2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 1999 Free Software Foundation, Inc.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * Most of this file was originally the source file "inflate.c", written
22 * by Mark Adler. It has been very heavily modified. In particular, the
23 * original would run through the whole file at once, and this version can
24 * be stopped and restarted on any boundary during the decompression process.
26 * The license and header comments that file are included here.
29 /* inflate.c -- Not copyrighted 1992 by Mark Adler
30 version c10p1, 10 January 1993 */
32 /* You can do whatever you like with this source file, though I would
33 prefer that if you modify it and redistribute it that you include
34 comments to that effect with your name and the date. Thank you.
38 Inflate deflated (PKZIP's method 8 compressed) data. The compression
39 method searches for as much of the current string of bytes (up to a
40 length of 258) in the previous 32K bytes. If it doesn't find any
41 matches (of at least length 3), it codes the next byte. Otherwise, it
42 codes the length of the matched string and its distance backwards from
43 the current position. There is a single Huffman code that codes both
44 single bytes (called "literals") and match lengths. A second Huffman
45 code codes the distance information, which follows a length code. Each
46 length or distance code actually represents a base value and a number
47 of "extra" (sometimes zero) bits to get to add to the base value. At
48 the end of each deflated block is a special end-of-block (EOB) literal/
49 length code. The decoding process is basically: get a literal/length
50 code; if EOB then done; if a literal, emit the decoded byte; if a
51 length then get the distance and emit the referred-to bytes from the
52 sliding window of previously emitted data.
54 There are (currently) three kinds of inflate blocks: stored, fixed, and
55 dynamic. The compressor deals with some chunk of data at a time, and
56 decides which method to use on a chunk-by-chunk basis. A chunk might
57 typically be 32K or 64K. If the chunk is uncompressible, then the
58 "stored" method is used. In this case, the bytes are simply stored as
59 is, eight bits per byte, with none of the above coding. The bytes are
60 preceded by a count, since there is no longer an EOB code.
62 If the data is compressible, then either the fixed or dynamic methods
63 are used. In the dynamic method, the compressed data is preceded by
64 an encoding of the literal/length and distance Huffman codes that are
65 to be used to decode this block. The representation is itself Huffman
66 coded, and so is preceded by a description of that code. These code
67 descriptions take up a little space, and so for small blocks, there is
68 a predefined set of codes, called the fixed codes. The fixed method is
69 used if the block codes up smaller that way (usually for quite small
70 chunks), otherwise the dynamic method is used. In the latter case, the
71 codes are customized to the probabilities in the current block, and so
72 can code it much better than the pre-determined fixed codes.
74 The Huffman codes themselves are decoded using a mutli-level table
75 lookup, in order to maximize the speed of decoding plus the speed of
76 building the decoding tables. See the comments below that precede the
77 lbits and dbits tuning parameters.
82 Notes beyond the 1.93a appnote.txt:
84 1. Distance pointers never point before the beginning of the output
86 2. Distance pointers can point back across blocks, up to 32k away.
87 3. There is an implied maximum of 7 bits for the bit length table and
88 15 bits for the actual data.
89 4. If only one code exists, then it is encoded using one bit. (Zero
90 would be more efficient, but perhaps a little confusing.) If two
91 codes exist, they are coded using one bit each (0 and 1).
92 5. There is no way of sending zero distance codes--a dummy must be
93 sent if there are none. (History: a pre 2.0 version of PKZIP would
94 store blocks with no distance codes, but this was discovered to be
95 too harsh a criterion.) Valid only for 1.93a. 2.04c does allow
96 zero distance codes, which is sent as one code of zero bits in
98 6. There are up to 286 literal/length codes. Code 256 represents the
99 end-of-block. Note however that the static length tree defines
100 288 codes just to fill out the Huffman codes. Codes 286 and 287
101 cannot be used though, since there is no length base or extra bits
102 defined for them. Similarly, there are up to 30 distance codes.
103 However, static trees define 32 codes (all 5 bits) to fill out the
104 Huffman codes, but the last two had better not show up in the data.
105 7. Unzip can check dynamic Huffman blocks for complete code sets.
106 The exception is that a single code would not be complete (see #4).
107 8. The five bits following the block type is really the number of
108 literal codes sent minus 257.
109 9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
110 (1+6+6). Therefore, to output three times the length, you output
111 three codes (1+1+1), whereas to output four times the same length,
112 you only need two codes (1+3). Hmm.
113 10. In the tree reconstruction algorithm, Code = Code + Increment
114 only if BitLength(i) is not zero. (Pretty obvious.)
115 11. Correction: 4 Bits: # of Bit Length codes - 4 (4 - 19)
116 12. Note: length code 284 can represent 227-258, but length code 285
117 really is 258. The last length deserves its own, short code
118 since it gets used a lot in very redundant files. The length
119 258 is special since 258 - 3 (the min match length) is 255.
120 13. The literal/length and distance code bit lengths are read as a
121 single stream of lengths. It is possible (and advantageous) for
122 a repeat code (16, 17, or 18) to go across the boundary between
123 the two sets of lengths.
126 #ifndef NO_DECOMPRESSION
134 unsigned int filepos;
135 unsigned int filemax;
136 unsigned int fsmax; /* max size of fs/readable media */
140 /* whether to show decompression progress or not */
141 enum { do_show_progress = 1 };
143 /* so we can disable decompression */
144 int no_decompression = 0;
146 /* used to tell if "read" should be redirected to "gunzip_read" */
147 unsigned int compressed_file;
149 /* internal variables only */
150 static unsigned int gzip_data_offset;
151 static int gzip_filepos;
152 static int gzip_filemax;
153 static int gzip_fsmax;
154 static int saved_filepos;
155 static unsigned long gzip_crc;
157 /* internal extra variables for use of inflate code */
158 static int block_type;
159 static unsigned int block_len;
160 static int last_block;
161 static int code_state;
164 /* Function prototypes */
165 static void initialize_tables (void);
167 static void show_progress(int done, int len)
169 int r = printf("%d%%", (done * 100) / len);
179 static unsigned long linalloc_topaddr;
184 extern void *lin_alloc_buffer;
185 unsigned long newaddr = (linalloc_topaddr - size) & ~3;
186 if (newaddr < (unsigned long)lin_alloc_buffer)
187 panic("Out of memory while uncompressing");
188 linalloc_topaddr = newaddr;
189 return (void *) linalloc_topaddr;
193 reset_linalloc (void)
195 linalloc_topaddr = RAW_ADDR (UPPER_MEM_LINALLOC);
199 /* internal variable swap function */
201 gunzip_swap_values (void)
207 filepos = gzip_filepos;
212 filemax = gzip_filemax;
222 /* internal function for eating variable-length header fields */
226 unsigned char ch = 1;
242 while ((not_retval = grub_read (&ch, 1)) == 1);
244 return (!not_retval);
248 /* Little-Endian defines for the 2-byte magic number for gzip files */
249 #define GZIP_HDR_LE 0x8B1F
250 #define OLD_GZIP_HDR_LE 0x9E1F
252 /* Compression methods (see algorithm.doc) */
257 /* methods 4 to 7 reserved */
259 #define MAX_METHODS 9
262 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
263 #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
264 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
265 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
266 #define COMMENT 0x10 /* bit 4 set: file comment present */
267 #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
268 #define RESERVED 0xC0 /* bit 6,7: reserved */
270 #define UNSUPP_FLAGS (CONTINUATION|ENCRYPTED|RESERVED)
272 /* inflate block codes */
273 #define INFLATE_STORED 0
274 #define INFLATE_FIXED 1
275 #define INFLATE_DYNAMIC 2
277 typedef unsigned char uch;
278 typedef unsigned short ush;
279 typedef unsigned long ulg;
284 * This must be a power of two, and at least 32K for zip's deflate method
291 gunzip_test_header (void)
293 unsigned char buf[10] __attribute__((aligned(4)));
295 /* "compressed_file" is already reset to zero by this point */
298 * This checks if the file is gzipped. If a problem occurs here
299 * (other than a real error with the disk) then we don't think it
300 * is a compressed file, and simply mark it as such.
303 || grub_read (buf, 10) != 10
304 || ((*((unsigned short *) buf) != GZIP_HDR_LE)
305 && (*((unsigned short *) buf) != OLD_GZIP_HDR_LE)))
312 * This does consistency checking on the header data. If a
313 * problem occurs from here on, then we have corrupt or otherwise
314 * bad data, and the error should be reported to the user.
316 if (buf[2] != DEFLATED
317 || (buf[3] & UNSUPP_FLAGS)
318 || ((buf[3] & EXTRA_FIELD)
319 && (grub_read (buf, 2) != 2
320 || bad_field (*((unsigned short *) buf))))
321 || ((buf[3] & ORIG_NAME) && bad_field (-1))
322 || ((buf[3] & COMMENT) && bad_field (-1)))
325 errnum = ERR_BAD_GZIP_HEADER;
330 gzip_data_offset = filepos;
332 filepos = filemax - 8;
334 if (grub_read (buf, 8) != 8)
337 errnum = ERR_BAD_GZIP_HEADER;
342 gzip_crc = *((unsigned long *) buf);
343 gzip_fsmax = gzip_filemax = *((unsigned long *) (buf + 4));
345 initialize_tables ();
348 gunzip_swap_values ();
350 * Now "gzip_*" values refer to the compressed data.
359 /* Huffman code lookup table entry--this entry is four bytes for machines
360 that have 16-bit pointers (e.g. PC's in the small or medium model).
361 Valid extra bits are 0..13. e == 15 is EOB (end of block), e == 16
362 means that v is a literal, 16 < e < 32 means that v is a pointer to
363 the next table, which codes e - 16 bits, and lastly e == 99 indicates
364 an unused code. If a code with e == 99 is looked up, this implies an
365 error in the data. */
368 uch e; /* number of extra bits or operation */
369 uch b; /* number of bits in this code or subcode */
372 ush n; /* literal, length base, or distance base */
373 struct huft *t; /* pointer to next level of table */
379 /* The inflate algorithm uses a sliding 32K byte window on the uncompressed
380 stream to find repeated byte strings. This is implemented here as a
381 circular buffer. The index is updated simply by incrementing and then
382 and'ing with 0x7fff (32K-1). */
383 /* It is left to other modules to supply the 32K area. It is assumed
384 to be usable as if it were declared "uch slide[32768];" or as just
385 "uch *slide;" and then malloc'ed in the latter case. The definition
386 must be in unzip.h, included above. */
389 /* sliding window in uncompressed data */
390 static uch slide[WSIZE];
392 /* current position in slide */
396 /* Tables for deflate from PKZIP's appnote.txt. */
397 static unsigned bitorder[] =
398 { /* Order of the bit length code lengths */
399 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
400 static ush cplens[] __attribute__((aligned(4))) =
401 { /* Copy lengths for literal codes 257..285 */
402 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
403 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
404 /* note: see note #13 above about the 258 in this list. */
405 static ush cplext[] __attribute__((aligned(4))) =
406 { /* Extra bits for literal codes 257..285 */
407 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
408 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99}; /* 99==invalid */
409 static ush cpdist[] __attribute__((aligned(4))) =
410 { /* Copy offsets for distance codes 0..29 */
411 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
412 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
413 8193, 12289, 16385, 24577};
414 static ush cpdext[] __attribute__((aligned(4))) =
415 { /* Extra bits for distance codes */
416 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
417 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
422 Huffman code decoding is performed using a multi-level table lookup.
423 The fastest way to decode is to simply build a lookup table whose
424 size is determined by the longest code. However, the time it takes
425 to build this table can also be a factor if the data being decoded
426 is not very long. The most common codes are necessarily the
427 shortest codes, so those codes dominate the decoding time, and hence
428 the speed. The idea is you can have a shorter table that decodes the
429 shorter, more probable codes, and then point to subsidiary tables for
430 the longer codes. The time it costs to decode the longer codes is
431 then traded against the time it takes to make longer tables.
433 This results of this trade are in the variables lbits and dbits
434 below. lbits is the number of bits the first level table for literal/
435 length codes can decode in one step, and dbits is the same thing for
436 the distance codes. Subsequent tables are also less than or equal to
437 those sizes. These values may be adjusted either when all of the
438 codes are shorter than that, in which case the longest code length in
439 bits is used, or when the shortest code is *longer* than the requested
440 table size, in which case the length of the shortest code in bits is
443 There are two different values for the two tables, since they code a
444 different number of possibilities each. The literal/length table
445 codes 286 possible values, or in a flat code, a little over eight
446 bits. The distance table codes 30 possible values, or a little less
447 than five bits, flat. The optimum values for speed end up being
448 about one bit more than those, so lbits is 8+1 and dbits is 5+1.
449 The optimum values may differ though from machine to machine, and
450 possibly even between compilers. Your mileage may vary.
454 static int lbits = 9; /* bits in base literal/length lookup table */
455 static int dbits = 6; /* bits in base distance lookup table */
458 /* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
459 #define BMAX 16 /* maximum bit length of any code (16 for explode) */
460 #define N_MAX 288 /* maximum number of codes in any set */
463 static unsigned hufts; /* track memory usage */
466 /* Macros for inflate() bit peeking and grabbing.
470 x = b & mask_bits[j];
473 where NEEDBITS makes sure that b has at least j bits in it, and
474 DUMPBITS removes the bits from b. The macros use the variable k
475 for the number of bits in b. Normally, b and k are register
476 variables for speed, and are initialized at the beginning of a
477 routine that uses these macros from a global bit buffer and count.
479 If we assume that EOB will be the longest code, then we will never
480 ask for bits with NEEDBITS that are beyond the end of the stream.
481 So, NEEDBITS should not read any more bytes than are needed to
482 meet the request. Then no bytes need to be "returned" to the buffer
483 at the end of the last block.
485 However, this assumption is not true for fixed blocks--the EOB code
486 is 7 bits, but the other literal/length codes can be 8 or 9 bits.
487 (The EOB code is shorter than other codes because fixed blocks are
488 generally short. So, while a block always has an EOB, many other
489 literal/length codes have a significantly lower probability of
490 showing up at all.) However, by making the first table have a
491 lookup of seven bits, the EOB code will be found in that first
492 lookup, and so will not require that too many bits be pulled from
496 static ulg bb; /* bit buffer */
497 static unsigned bk; /* bits in bit buffer */
499 static ush mask_bits[] =
502 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
503 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
506 #define NEEDBITS(n) do {while(k<(n)){b|=((ulg)get_byte())<<k;k+=8;}} while (0)
507 #define DUMPBITS(n) do {b>>=(n);k-=(n);} while (0)
509 #define INBUFSIZ 0x2000
511 static unsigned char inbuf[INBUFSIZ];
517 if (filepos == gzip_data_offset || bufloc == INBUFSIZ)
520 grub_read (inbuf, INBUFSIZ);
523 return inbuf[bufloc++];
526 /* decompression global pointers */
527 static struct huft *tl; /* literal/length code table */
528 static struct huft *td; /* distance code table */
529 static int bl; /* lookup bits for tl */
530 static int bd; /* lookup bits for td */
533 /* more function prototypes */
534 static int huft_build (unsigned *, unsigned, unsigned, ush *, ush *,
535 struct huft **, int *);
536 static int inflate_codes_in_window (void);
539 /* Given a list of code lengths and a maximum table size, make a set of
540 tables to decode that set of codes. Return zero on success, one if
541 the given code set is incomplete (the tables are still built in this
542 case), two if the input is invalid (all zero length codes or an
543 oversubscribed set of lengths), and three if not enough memory. */
546 huft_build (unsigned *b, /* code lengths in bits (all assumed <= BMAX) */
547 unsigned n, /* number of codes (assumed <= N_MAX) */
548 unsigned s, /* number of simple-valued codes (0..s-1) */
549 ush * d, /* list of base values for non-simple codes */
550 ush * e, /* list of extra bits for non-simple codes */
551 struct huft **t, /* result: starting table */
552 int *m) /* maximum lookup bits, returns actual */
554 unsigned a; /* counter for codes of length k */
555 unsigned c[BMAX + 1]; /* bit length count table */
556 unsigned f; /* i repeats in table every f entries */
557 int g; /* maximum code length */
558 int h; /* table level */
559 register unsigned i; /* counter, current code */
560 register unsigned j; /* counter */
561 register int k; /* number of bits in current code */
562 int l; /* bits per table (returned in m) */
563 register unsigned *p; /* pointer into c[], b[], or v[] */
564 register struct huft *q; /* points to current table */
565 struct huft r; /* table entry for structure assignment */
566 struct huft *u[BMAX]; /* table stack */
567 unsigned v[N_MAX]; /* values in order of bit length */
568 register int w; /* bits before this table == (l * h) */
569 unsigned x[BMAX + 1]; /* bit offsets, then code stack */
570 unsigned *xp; /* pointer into x */
571 int y; /* number of dummy codes added */
572 unsigned z; /* number of entries in current table */
574 /* Generate counts for each bit length */
575 memset ((char *) c, 0, sizeof (c));
580 c[*p]++; /* assume all entries <= BMAX */
581 p++; /* Can't combine with above line (Solaris bug) */
584 if (c[0] == n) /* null input--all zero length codes */
586 *t = (struct huft *) NULL;
591 /* Find minimum and maximum length, bound *m by those */
593 for (j = 1; j <= BMAX; j++)
596 k = j; /* minimum code length */
597 if ((unsigned) l < j)
599 for (i = BMAX; i; i--)
602 g = i; /* maximum code length */
603 if ((unsigned) l > i)
607 /* Adjust last length count to fill out codes, if needed */
608 for (y = 1 << j; j < i; j++, y <<= 1)
610 return 2; /* bad input: more codes than bits */
615 /* Generate starting offsets into the value table for each length */
620 { /* note that i == g from above */
624 /* Make a table of values in order of bit lengths */
634 /* Generate the Huffman codes and for each, make the table entries */
635 x[0] = i = 0; /* first Huffman code is zero */
636 p = v; /* grab values in bit order */
637 h = -1; /* no tables yet--level -1 */
638 w = -l; /* bits decoded == (l * h) */
639 u[0] = (struct huft *) NULL; /* just to keep compilers happy */
640 q = (struct huft *) NULL; /* ditto */
643 /* go through the bit lengths (k already is bits in shortest code) */
649 /* here i is the Huffman code of length k bits for value *p */
650 /* make tables up to required level */
654 w += l; /* previous table always l bits */
656 /* compute minimum size table less than or equal to l bits */
657 z = (z = g - w) > (unsigned) l ? l : z; /* upper limit on table size */
658 if ((f = 1 << (j = k - w)) > a + 1) /* try a k-w bit table */
659 { /* too few codes for k-w bit table */
660 f -= a + 1; /* deduct codes from patterns left */
662 while (++j < z) /* try smaller tables up to z bits */
664 if ((f <<= 1) <= *++xp)
665 break; /* enough codes to use up j bits */
666 f -= *xp; /* else deduct codes from patterns */
669 z = 1 << j; /* table entries for j-bit table */
671 /* allocate and link in new table */
672 q = (struct huft *) linalloc ((z + 1) * sizeof (struct huft));
674 hufts += z + 1; /* track memory usage */
675 *t = q + 1; /* link to list for huft_free() */
676 *(t = &(q->v.t)) = (struct huft *) NULL;
677 u[h] = ++q; /* table starts after link */
679 /* connect to last table, if there is one */
682 x[h] = i; /* save pattern for backing up */
683 r.b = (uch) l; /* bits to dump before this table */
684 r.e = (uch) (16 + j); /* bits in this table */
685 r.v.t = q; /* pointer to this table */
686 j = i >> (w - l); /* (get around Turbo C bug) */
687 u[h - 1][j] = r; /* connect to last table */
691 /* set up table entry in r */
694 r.e = 99; /* out of values--invalid code */
697 r.e = (uch) (*p < 256 ? 16 : 15); /* 256 is end-of-block code */
698 r.v.n = (ush) (*p); /* simple code is just the value */
699 p++; /* one compiler does not like *p++ */
703 r.e = (uch) e[*p - s]; /* non-simple--look up in lists */
707 /* fill code-like entries with r */
709 for (j = i >> w; j < z; j += f)
712 /* backwards increment the k-bit code i */
713 for (j = 1 << (k - 1); i & j; j >>= 1)
717 /* backup over finished tables */
718 while ((i & ((1 << w) - 1)) != x[h])
720 h--; /* don't need to update q */
726 /* Return true (1) if we were given an incomplete table */
727 return y != 0 && g != 1;
732 * inflate (decompress) the codes in a deflated (compressed) block.
733 * Return an error code or zero if it all goes ok.
736 static unsigned inflate_n, inflate_d;
739 inflate_codes_in_window (void)
741 register unsigned e; /* table entry flag/number of extra bits */
742 unsigned n, d; /* length and index for copy */
743 unsigned w; /* current window position */
744 struct huft *t; /* pointer to table entry */
745 unsigned ml, md; /* masks for bl and bd bits */
746 register ulg b; /* bit buffer */
747 register unsigned k; /* number of bits in bit buffer */
749 /* make local copies of globals */
752 b = bb; /* initialize bit buffer */
754 w = wp; /* initialize window position */
756 /* inflate the coded data */
757 ml = mask_bits[bl]; /* precompute masks for speed */
759 for (;;) /* do until end of block */
763 NEEDBITS ((unsigned) bl);
764 if ((e = (t = tl + ((unsigned) b & ml))->e) > 16)
769 errnum = ERR_BAD_GZIP_DATA;
776 while ((e = (t = t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16);
779 if (e == 16) /* then it's a literal */
781 slide[w++] = (uch) t->v.n;
786 /* it's an EOB or a length */
788 /* exit if end of block */
795 /* get length of block to copy */
797 n = t->v.n + ((unsigned) b & mask_bits[e]);
800 /* decode distance of block to copy */
801 NEEDBITS ((unsigned) bd);
802 if ((e = (t = td + ((unsigned) b & md))->e) > 16)
807 errnum = ERR_BAD_GZIP_DATA;
814 while ((e = (t = t->v.t + ((unsigned) b & mask_bits[e]))->e)
818 d = w - t->v.n - ((unsigned) b & mask_bits[e]);
829 n -= (e = (e = WSIZE - ((d &= WSIZE - 1) > w ? d : w)) > n ? n
833 memmove (slide + w, slide + d, e);
838 /* purposefully use the overlap for extra copies here!! */
841 slide[w++] = slide[d++];
851 /* did we break from the loop too soon? */
857 /* restore the globals from the locals */
860 wp = w; /* restore global window pointer */
861 bb = b; /* restore global bit buffer */
868 /* get header for an inflated type 0 (stored) block. */
871 init_stored_block (void)
873 register ulg b; /* bit buffer */
874 register unsigned k; /* number of bits in bit buffer */
876 /* make local copies of globals */
877 b = bb; /* initialize bit buffer */
880 /* go to byte boundary */
883 /* get the length and its complement */
885 block_len = ((unsigned) b & 0xffff);
888 if (block_len != (unsigned) ((~b) & 0xffff))
889 errnum = ERR_BAD_GZIP_DATA;
892 /* restore global variables */
898 /* get header for an inflated type 1 (fixed Huffman codes) block. We should
899 either replace this with a custom decoder, or at least precompute the
903 init_fixed_block (void)
905 int i; /* temporary variable */
906 unsigned l[288]; /* length list for huft_build */
908 /* set up literal table */
909 for (i = 0; i < 144; i++)
915 for (; i < 288; i++) /* make a complete, but wrong code set */
918 if ((i = huft_build (l, 288, 257, cplens, cplext, &tl, &bl)) != 0)
920 errnum = ERR_BAD_GZIP_DATA;
924 /* set up distance table */
925 for (i = 0; i < 30; i++) /* make an incomplete code set */
928 if ((i = huft_build (l, 30, 0, cpdist, cpdext, &td, &bd)) > 1)
930 errnum = ERR_BAD_GZIP_DATA;
934 /* indicate we're now working on a block */
940 /* get header for an inflated type 2 (dynamic Huffman codes) block. */
943 init_dynamic_block (void)
945 int i; /* temporary variables */
947 unsigned l; /* last length */
948 unsigned m; /* mask for bit lengths table */
949 unsigned n; /* number of lengths to get */
950 unsigned nb; /* number of bit length codes */
951 unsigned nl; /* number of literal/length codes */
952 unsigned nd; /* number of distance codes */
953 unsigned ll[286 + 30]; /* literal/length and distance code lengths */
954 register ulg b; /* bit buffer */
955 register unsigned k; /* number of bits in bit buffer */
957 /* make local bit buffer */
961 /* read in table lengths */
963 nl = 257 + ((unsigned) b & 0x1f); /* number of literal/length codes */
966 nd = 1 + ((unsigned) b & 0x1f); /* number of distance codes */
969 nb = 4 + ((unsigned) b & 0xf); /* number of bit length codes */
971 if (nl > 286 || nd > 30)
973 errnum = ERR_BAD_GZIP_DATA;
977 /* read in bit-length-code lengths */
978 for (j = 0; j < nb; j++)
981 ll[bitorder[j]] = (unsigned) b & 7;
987 /* build decoding table for trees--single level, 7 bit lookup */
989 if ((i = huft_build (ll, 19, 19, NULL, NULL, &tl, &bl)) != 0)
991 errnum = ERR_BAD_GZIP_DATA;
995 /* read in literal and distance code lengths */
999 while ((unsigned) i < n)
1001 NEEDBITS ((unsigned) bl);
1002 j = (td = tl + ((unsigned) b & m))->b;
1005 if (j < 16) /* length of code in bits (0..15) */
1006 ll[i++] = l = j; /* save last length in l */
1007 else if (j == 16) /* repeat last length 3 to 6 times */
1010 j = 3 + ((unsigned) b & 3);
1012 if ((unsigned) i + j > n)
1014 errnum = ERR_BAD_GZIP_DATA;
1020 else if (j == 17) /* 3 to 10 zero length codes */
1023 j = 3 + ((unsigned) b & 7);
1025 if ((unsigned) i + j > n)
1027 errnum = ERR_BAD_GZIP_DATA;
1035 /* j == 18: 11 to 138 zero length codes */
1038 j = 11 + ((unsigned) b & 0x7f);
1040 if ((unsigned) i + j > n)
1042 errnum = ERR_BAD_GZIP_DATA;
1051 /* free decoding table for trees */
1054 /* restore the global bit buffer */
1058 /* build the decoding tables for literal/length and distance codes */
1060 if ((i = huft_build (ll, nl, 257, cplens, cplext, &tl, &bl)) != 0)
1064 printf ("gunzip: incomplete literal tree\n");
1067 errnum = ERR_BAD_GZIP_DATA;
1071 if ((i = huft_build (ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0)
1075 printf ("gunzip: incomplete distance tree\n");
1078 errnum = ERR_BAD_GZIP_DATA;
1082 /* indicate we're now working on a block */
1089 get_new_block (void)
1091 register ulg b; /* bit buffer */
1092 register unsigned k; /* number of bits in bit buffer */
1096 /* make local bit buffer */
1100 /* read in last block bit */
1102 last_block = (int) b & 1;
1105 /* read in block type */
1107 block_type = (unsigned) b & 3;
1110 /* restore the global bit buffer */
1114 if (block_type == INFLATE_STORED)
1115 init_stored_block ();
1116 if (block_type == INFLATE_FIXED)
1117 init_fixed_block ();
1118 if (block_type == INFLATE_DYNAMIC)
1119 init_dynamic_block ();
1124 inflate_window (void)
1126 /* initialize window */
1130 * Main decompression loop.
1133 while (wp < WSIZE && !errnum)
1143 if (block_type > INFLATE_DYNAMIC)
1144 errnum = ERR_BAD_GZIP_DATA;
1150 * Expand stored block here.
1152 if (block_type == INFLATE_STORED)
1157 * This is basically a glorified pass-through
1160 while (block_len && w < WSIZE && !errnum)
1162 slide[w++] = get_byte ();
1172 * Expand other kind of block.
1175 if (inflate_codes_in_window ())
1179 saved_filepos += WSIZE;
1181 /* XXX do CRC calculation here! */
1186 initialize_tables (void)
1189 filepos = gzip_data_offset;
1191 /* initialize window, bit buffer */
1195 /* reset partial decompression code */
1199 /* reset memory allocation stuff */
1205 gunzip_read (unsigned char *buf, int len)
1210 compressed_file = 0;
1211 gunzip_swap_values ();
1213 * Now "gzip_*" values refer to the uncompressed data.
1216 /* do we reset decompression to the beginning of the file? */
1217 if (saved_filepos > gzip_filepos + WSIZE)
1218 initialize_tables ();
1221 * This loop operates upon uncompressed data only. The only
1222 * special thing it does is to make sure the decompression
1223 * window is within the range of data it needs.
1226 while (len > 0 && !errnum)
1229 register char *srcaddr;
1231 while (gzip_filepos >= saved_filepos)
1234 srcaddr = (char *) ((gzip_filepos & (WSIZE - 1)) + slide);
1235 size = saved_filepos - gzip_filepos;
1239 memmove (buf, srcaddr, size);
1243 gzip_filepos += size;
1246 if (do_show_progress)
1247 show_progress(ret, real_len);
1250 compressed_file = 1;
1251 gunzip_swap_values ();
1253 * Now "gzip_*" values refer to the compressed data.
1262 #endif /* ! NO_DECOMPRESSION */