• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  GRUB  --  GRand Unified Bootloader
3  *  Copyright (C) 1999  Free Software Foundation, Inc.
4  *
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.
9  *
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.
14  *
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.
18  */
19 
20 /*
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.
25  *
26  * The license and header comments that file are included here.
27  */
28 
29 /* inflate.c -- Not copyrighted 1992 by Mark Adler
30    version c10p1, 10 January 1993 */
31 
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.
35  */
36 
37 /*
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.
53 
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.
61 
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.
73 
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.
78  */
79 
80 
81 /*
82    Notes beyond the 1.93a appnote.txt:
83 
84    1. Distance pointers never point before the beginning of the output
85       stream.
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
97       length.
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.
124  */
125 
126 #ifndef NO_DECOMPRESSION
127 
128 #include "shared.h"
129 
130 #include "filesys.h"
131 
132 /* so we can disable decompression  */
133 int no_decompression = 0;
134 
135 /* used to tell if "read" should be redirected to "gunzip_read" */
136 int compressed_file;
137 
138 /* internal variables only */
139 static int gzip_data_offset;
140 static int gzip_filepos;
141 static int gzip_filemax;
142 static int gzip_fsmax;
143 static int saved_filepos;
144 static unsigned long gzip_crc;
145 
146 /* internal extra variables for use of inflate code */
147 static int block_type;
148 static int block_len;
149 static int last_block;
150 static int code_state;
151 
152 
153 /* Function prototypes */
154 static void initialize_tables (void);
155 
156 /*
157  *  Linear allocator.
158  */
159 
160 static unsigned long linalloc_topaddr;
161 
162 static void *
linalloc(int size)163 linalloc (int size)
164 {
165   linalloc_topaddr = (linalloc_topaddr - size) & ~3;
166   return (void *) linalloc_topaddr;
167 }
168 
169 static void
reset_linalloc(void)170 reset_linalloc (void)
171 {
172   linalloc_topaddr = RAW_ADDR ((mbi.mem_upper << 10) + 0x100000);
173 }
174 
175 
176 /* internal variable swap function */
177 static void
gunzip_swap_values(void)178 gunzip_swap_values (void)
179 {
180   register int itmp;
181 
182   /* swap filepos */
183   itmp = filepos;
184   filepos = gzip_filepos;
185   gzip_filepos = itmp;
186 
187   /* swap filemax */
188   itmp = filemax;
189   filemax = gzip_filemax;
190   gzip_filemax = itmp;
191 
192   /* swap fsmax */
193   itmp = fsmax;
194   fsmax = gzip_fsmax;
195   gzip_fsmax = itmp;
196 }
197 
198 
199 /* internal function for eating variable-length header fields */
200 static int
bad_field(int len)201 bad_field (int len)
202 {
203   char ch = 1;
204   int not_retval = 1;
205 
206   do
207     {
208       if (len >= 0)
209 	{
210 	  if (!(len--))
211 	    break;
212 	}
213       else
214 	{
215 	  if (!ch)
216 	    break;
217 	}
218     }
219   while ((not_retval = grub_read (&ch, 1)) == 1);
220 
221   return (!not_retval);
222 }
223 
224 
225 /* Little-Endian defines for the 2-byte magic number for gzip files */
226 #define GZIP_HDR_LE      0x8B1F
227 #define OLD_GZIP_HDR_LE  0x9E1F
228 
229 /* Compression methods (see algorithm.doc) */
230 #define STORED      0
231 #define COMPRESSED  1
232 #define PACKED      2
233 #define LZHED       3
234 /* methods 4 to 7 reserved */
235 #define DEFLATED    8
236 #define MAX_METHODS 9
237 
238 /* gzip flag byte */
239 #define ASCII_FLAG   0x01	/* bit 0 set: file probably ascii text */
240 #define CONTINUATION 0x02	/* bit 1 set: continuation of multi-part gzip file */
241 #define EXTRA_FIELD  0x04	/* bit 2 set: extra field present */
242 #define ORIG_NAME    0x08	/* bit 3 set: original file name present */
243 #define COMMENT      0x10	/* bit 4 set: file comment present */
244 #define ENCRYPTED    0x20	/* bit 5 set: file is encrypted */
245 #define RESERVED     0xC0	/* bit 6,7:   reserved */
246 
247 #define UNSUPP_FLAGS (CONTINUATION|ENCRYPTED|RESERVED)
248 
249 /* inflate block codes */
250 #define INFLATE_STORED    0
251 #define INFLATE_FIXED     1
252 #define INFLATE_DYNAMIC   2
253 
254 typedef unsigned char uch;
255 typedef unsigned short ush;
256 typedef unsigned long ulg;
257 
258 /*
259  *  Window Size
260  *
261  *  This must be a power of two, and at least 32K for zip's deflate method
262  */
263 
264 #define WSIZE 0x8000
265 
266 
267 int
gunzip_test_header(void)268 gunzip_test_header (void)
269 {
270   unsigned char buf[10];
271 
272   /* "compressed_file" is already reset to zero by this point */
273 
274   /*
275    *  This checks if the file is gzipped.  If a problem occurs here
276    *  (other than a real error with the disk) then we don't think it
277    *  is a compressed file, and simply mark it as such.
278    */
279   if (no_decompression
280       || grub_read (buf, 10) != 10
281       || ((*((unsigned short *) buf) != GZIP_HDR_LE)
282 	  && (*((unsigned short *) buf) != OLD_GZIP_HDR_LE)))
283     {
284       filepos = 0;
285       return ! errnum;
286     }
287 
288   /*
289    *  This does consistency checking on the header data.  If a
290    *  problem occurs from here on, then we have corrupt or otherwise
291    *  bad data, and the error should be reported to the user.
292    */
293   if (buf[2] != DEFLATED
294       || (buf[3] & UNSUPP_FLAGS)
295       || ((buf[3] & EXTRA_FIELD)
296 	  && (grub_read (buf, 2) != 2
297 	      || bad_field (*((unsigned short *) buf))))
298       || ((buf[3] & ORIG_NAME) && bad_field (-1))
299       || ((buf[3] & COMMENT) && bad_field (-1)))
300     {
301       if (! errnum)
302 	errnum = ERR_BAD_GZIP_HEADER;
303 
304       return 0;
305     }
306 
307   gzip_data_offset = filepos;
308 
309   filepos = filemax - 8;
310 
311   if (grub_read (buf, 8) != 8)
312     {
313       if (! errnum)
314 	errnum = ERR_BAD_GZIP_HEADER;
315 
316       return 0;
317     }
318 
319   gzip_crc = *((unsigned long *) buf);
320   gzip_fsmax = gzip_filemax = *((unsigned long *) (buf + 4));
321 
322   initialize_tables ();
323 
324   compressed_file = 1;
325   gunzip_swap_values ();
326   /*
327    *  Now "gzip_*" values refer to the compressed data.
328    */
329 
330   filepos = 0;
331 
332   return 1;
333 }
334 
335 
336 /* Huffman code lookup table entry--this entry is four bytes for machines
337    that have 16-bit pointers (e.g. PC's in the small or medium model).
338    Valid extra bits are 0..13.  e == 15 is EOB (end of block), e == 16
339    means that v is a literal, 16 < e < 32 means that v is a pointer to
340    the next table, which codes e - 16 bits, and lastly e == 99 indicates
341    an unused code.  If a code with e == 99 is looked up, this implies an
342    error in the data. */
343 struct huft
344 {
345   uch e;			/* number of extra bits or operation */
346   uch b;			/* number of bits in this code or subcode */
347   union
348     {
349       ush n;			/* literal, length base, or distance base */
350       struct huft *t;		/* pointer to next level of table */
351     }
352   v;
353 };
354 
355 
356 /* The inflate algorithm uses a sliding 32K byte window on the uncompressed
357    stream to find repeated byte strings.  This is implemented here as a
358    circular buffer.  The index is updated simply by incrementing and then
359    and'ing with 0x7fff (32K-1). */
360 /* It is left to other modules to supply the 32K area.  It is assumed
361    to be usable as if it were declared "uch slide[32768];" or as just
362    "uch *slide;" and then malloc'ed in the latter case.  The definition
363    must be in unzip.h, included above. */
364 
365 
366 /* sliding window in uncompressed data */
367 static uch slide[WSIZE];
368 
369 /* current position in slide */
370 static unsigned wp;
371 
372 
373 /* Tables for deflate from PKZIP's appnote.txt. */
374 static unsigned bitorder[] =
375 {				/* Order of the bit length code lengths */
376   16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
377 static ush cplens[] =
378 {				/* Copy lengths for literal codes 257..285 */
379   3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
380   35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
381 	/* note: see note #13 above about the 258 in this list. */
382 static ush cplext[] =
383 {				/* Extra bits for literal codes 257..285 */
384   0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
385   3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99};	/* 99==invalid */
386 static ush cpdist[] =
387 {				/* Copy offsets for distance codes 0..29 */
388   1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
389   257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
390   8193, 12289, 16385, 24577};
391 static ush cpdext[] =
392 {				/* Extra bits for distance codes */
393   0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
394   7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
395   12, 12, 13, 13};
396 
397 
398 /*
399    Huffman code decoding is performed using a multi-level table lookup.
400    The fastest way to decode is to simply build a lookup table whose
401    size is determined by the longest code.  However, the time it takes
402    to build this table can also be a factor if the data being decoded
403    is not very long.  The most common codes are necessarily the
404    shortest codes, so those codes dominate the decoding time, and hence
405    the speed.  The idea is you can have a shorter table that decodes the
406    shorter, more probable codes, and then point to subsidiary tables for
407    the longer codes.  The time it costs to decode the longer codes is
408    then traded against the time it takes to make longer tables.
409 
410    This results of this trade are in the variables lbits and dbits
411    below.  lbits is the number of bits the first level table for literal/
412    length codes can decode in one step, and dbits is the same thing for
413    the distance codes.  Subsequent tables are also less than or equal to
414    those sizes.  These values may be adjusted either when all of the
415    codes are shorter than that, in which case the longest code length in
416    bits is used, or when the shortest code is *longer* than the requested
417    table size, in which case the length of the shortest code in bits is
418    used.
419 
420    There are two different values for the two tables, since they code a
421    different number of possibilities each.  The literal/length table
422    codes 286 possible values, or in a flat code, a little over eight
423    bits.  The distance table codes 30 possible values, or a little less
424    than five bits, flat.  The optimum values for speed end up being
425    about one bit more than those, so lbits is 8+1 and dbits is 5+1.
426    The optimum values may differ though from machine to machine, and
427    possibly even between compilers.  Your mileage may vary.
428  */
429 
430 
431 static int lbits = 9;		/* bits in base literal/length lookup table */
432 static int dbits = 6;		/* bits in base distance lookup table */
433 
434 
435 /* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
436 #define BMAX 16			/* maximum bit length of any code (16 for explode) */
437 #define N_MAX 288		/* maximum number of codes in any set */
438 
439 
440 static unsigned hufts;		/* track memory usage */
441 
442 
443 /* Macros for inflate() bit peeking and grabbing.
444    The usage is:
445 
446         NEEDBITS(j)
447         x = b & mask_bits[j];
448         DUMPBITS(j)
449 
450    where NEEDBITS makes sure that b has at least j bits in it, and
451    DUMPBITS removes the bits from b.  The macros use the variable k
452    for the number of bits in b.  Normally, b and k are register
453    variables for speed, and are initialized at the beginning of a
454    routine that uses these macros from a global bit buffer and count.
455 
456    If we assume that EOB will be the longest code, then we will never
457    ask for bits with NEEDBITS that are beyond the end of the stream.
458    So, NEEDBITS should not read any more bytes than are needed to
459    meet the request.  Then no bytes need to be "returned" to the buffer
460    at the end of the last block.
461 
462    However, this assumption is not true for fixed blocks--the EOB code
463    is 7 bits, but the other literal/length codes can be 8 or 9 bits.
464    (The EOB code is shorter than other codes because fixed blocks are
465    generally short.  So, while a block always has an EOB, many other
466    literal/length codes have a significantly lower probability of
467    showing up at all.)  However, by making the first table have a
468    lookup of seven bits, the EOB code will be found in that first
469    lookup, and so will not require that too many bits be pulled from
470    the stream.
471  */
472 
473 static ulg bb;			/* bit buffer */
474 static unsigned bk;		/* bits in bit buffer */
475 
476 static ush mask_bits[] =
477 {
478   0x0000,
479   0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
480   0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
481 };
482 
483 #define NEEDBITS(n) do {while(k<(n)){b|=((ulg)get_byte())<<k;k+=8;}} while (0)
484 #define DUMPBITS(n) do {b>>=(n);k-=(n);} while (0)
485 
486 #define INBUFSIZ  0x2000
487 
488 static uch inbuf[INBUFSIZ];
489 static int bufloc;
490 
491 static int
get_byte(void)492 get_byte (void)
493 {
494   if (filepos == gzip_data_offset || bufloc == INBUFSIZ)
495     {
496       bufloc = 0;
497       grub_read (inbuf, INBUFSIZ);
498     }
499 
500   return inbuf[bufloc++];
501 }
502 
503 /* decompression global pointers */
504 static struct huft *tl;		/* literal/length code table */
505 static struct huft *td;		/* distance code table */
506 static int bl;			/* lookup bits for tl */
507 static int bd;			/* lookup bits for td */
508 
509 
510 /* more function prototypes */
511 static int huft_build (unsigned *, unsigned, unsigned, ush *, ush *,
512 		       struct huft **, int *);
513 static int inflate_codes_in_window (void);
514 
515 
516 /* Given a list of code lengths and a maximum table size, make a set of
517    tables to decode that set of codes.  Return zero on success, one if
518    the given code set is incomplete (the tables are still built in this
519    case), two if the input is invalid (all zero length codes or an
520    oversubscribed set of lengths), and three if not enough memory. */
521 
522 static int
huft_build(unsigned * b,unsigned n,unsigned s,ush * d,ush * e,struct huft ** t,int * m)523 huft_build (unsigned *b,	/* code lengths in bits (all assumed <= BMAX) */
524 	    unsigned n,		/* number of codes (assumed <= N_MAX) */
525 	    unsigned s,		/* number of simple-valued codes (0..s-1) */
526 	    ush * d,		/* list of base values for non-simple codes */
527 	    ush * e,		/* list of extra bits for non-simple codes */
528 	    struct huft **t,	/* result: starting table */
529 	    int *m)		/* maximum lookup bits, returns actual */
530 {
531   unsigned a;			/* counter for codes of length k */
532   unsigned c[BMAX + 1];		/* bit length count table */
533   unsigned f;			/* i repeats in table every f entries */
534   int g;			/* maximum code length */
535   int h;			/* table level */
536   register unsigned i;		/* counter, current code */
537   register unsigned j;		/* counter */
538   register int k;		/* number of bits in current code */
539   int l;			/* bits per table (returned in m) */
540   register unsigned *p;		/* pointer into c[], b[], or v[] */
541   register struct huft *q;	/* points to current table */
542   struct huft r;		/* table entry for structure assignment */
543   struct huft *u[BMAX];		/* table stack */
544   unsigned v[N_MAX];		/* values in order of bit length */
545   register int w;		/* bits before this table == (l * h) */
546   unsigned x[BMAX + 1];		/* bit offsets, then code stack */
547   unsigned *xp;			/* pointer into x */
548   int y;			/* number of dummy codes added */
549   unsigned z;			/* number of entries in current table */
550 
551   /* Generate counts for each bit length */
552   memset ((char *) c, 0, sizeof (c));
553   p = b;
554   i = n;
555   do
556     {
557       c[*p]++;			/* assume all entries <= BMAX */
558       p++;			/* Can't combine with above line (Solaris bug) */
559     }
560   while (--i);
561   if (c[0] == n)		/* null input--all zero length codes */
562     {
563       *t = (struct huft *) NULL;
564       *m = 0;
565       return 0;
566     }
567 
568   /* Find minimum and maximum length, bound *m by those */
569   l = *m;
570   for (j = 1; j <= BMAX; j++)
571     if (c[j])
572       break;
573   k = j;			/* minimum code length */
574   if ((unsigned) l < j)
575     l = j;
576   for (i = BMAX; i; i--)
577     if (c[i])
578       break;
579   g = i;			/* maximum code length */
580   if ((unsigned) l > i)
581     l = i;
582   *m = l;
583 
584   /* Adjust last length count to fill out codes, if needed */
585   for (y = 1 << j; j < i; j++, y <<= 1)
586     if ((y -= c[j]) < 0)
587       return 2;			/* bad input: more codes than bits */
588   if ((y -= c[i]) < 0)
589     return 2;
590   c[i] += y;
591 
592   /* Generate starting offsets into the value table for each length */
593   x[1] = j = 0;
594   p = c + 1;
595   xp = x + 2;
596   while (--i)
597     {				/* note that i == g from above */
598       *xp++ = (j += *p++);
599     }
600 
601   /* Make a table of values in order of bit lengths */
602   p = b;
603   i = 0;
604   do
605     {
606       if ((j = *p++) != 0)
607 	v[x[j]++] = i;
608     }
609   while (++i < n);
610 
611   /* Generate the Huffman codes and for each, make the table entries */
612   x[0] = i = 0;			/* first Huffman code is zero */
613   p = v;			/* grab values in bit order */
614   h = -1;			/* no tables yet--level -1 */
615   w = -l;			/* bits decoded == (l * h) */
616   u[0] = (struct huft *) NULL;	/* just to keep compilers happy */
617   q = (struct huft *) NULL;	/* ditto */
618   z = 0;			/* ditto */
619 
620   /* go through the bit lengths (k already is bits in shortest code) */
621   for (; k <= g; k++)
622     {
623       a = c[k];
624       while (a--)
625 	{
626 	  /* here i is the Huffman code of length k bits for value *p */
627 	  /* make tables up to required level */
628 	  while (k > w + l)
629 	    {
630 	      h++;
631 	      w += l;		/* previous table always l bits */
632 
633 	      /* compute minimum size table less than or equal to l bits */
634 	      z = (z = g - w) > (unsigned) l ? l : z;	/* upper limit on table size */
635 	      if ((f = 1 << (j = k - w)) > a + 1)	/* try a k-w bit table */
636 		{		/* too few codes for k-w bit table */
637 		  f -= a + 1;	/* deduct codes from patterns left */
638 		  xp = c + k;
639 		  while (++j < z)	/* try smaller tables up to z bits */
640 		    {
641 		      if ((f <<= 1) <= *++xp)
642 			break;	/* enough codes to use up j bits */
643 		      f -= *xp;	/* else deduct codes from patterns */
644 		    }
645 		}
646 	      z = 1 << j;	/* table entries for j-bit table */
647 
648 	      /* allocate and link in new table */
649 	      q = (struct huft *) linalloc ((z + 1) * sizeof (struct huft));
650 
651 	      hufts += z + 1;	/* track memory usage */
652 	      *t = q + 1;	/* link to list for huft_free() */
653 	      *(t = &(q->v.t)) = (struct huft *) NULL;
654 	      u[h] = ++q;	/* table starts after link */
655 
656 	      /* connect to last table, if there is one */
657 	      if (h)
658 		{
659 		  x[h] = i;	/* save pattern for backing up */
660 		  r.b = (uch) l;	/* bits to dump before this table */
661 		  r.e = (uch) (16 + j);		/* bits in this table */
662 		  r.v.t = q;	/* pointer to this table */
663 		  j = i >> (w - l);	/* (get around Turbo C bug) */
664 		  u[h - 1][j] = r;	/* connect to last table */
665 		}
666 	    }
667 
668 	  /* set up table entry in r */
669 	  r.b = (uch) (k - w);
670 	  if (p >= v + n)
671 	    r.e = 99;		/* out of values--invalid code */
672 	  else if (*p < s)
673 	    {
674 	      r.e = (uch) (*p < 256 ? 16 : 15);		/* 256 is end-of-block code */
675 	      r.v.n = (ush) (*p);	/* simple code is just the value */
676 	      p++;		/* one compiler does not like *p++ */
677 	    }
678 	  else
679 	    {
680 	      r.e = (uch) e[*p - s];	/* non-simple--look up in lists */
681 	      r.v.n = d[*p++ - s];
682 	    }
683 
684 	  /* fill code-like entries with r */
685 	  f = 1 << (k - w);
686 	  for (j = i >> w; j < z; j += f)
687 	    q[j] = r;
688 
689 	  /* backwards increment the k-bit code i */
690 	  for (j = 1 << (k - 1); i & j; j >>= 1)
691 	    i ^= j;
692 	  i ^= j;
693 
694 	  /* backup over finished tables */
695 	  while ((i & ((1 << w) - 1)) != x[h])
696 	    {
697 	      h--;		/* don't need to update q */
698 	      w -= l;
699 	    }
700 	}
701     }
702 
703   /* Return true (1) if we were given an incomplete table */
704   return y != 0 && g != 1;
705 }
706 
707 
708 /*
709  *  inflate (decompress) the codes in a deflated (compressed) block.
710  *  Return an error code or zero if it all goes ok.
711  */
712 
713 static unsigned inflate_n, inflate_d;
714 
715 static int
inflate_codes_in_window(void)716 inflate_codes_in_window (void)
717 {
718   register unsigned e;		/* table entry flag/number of extra bits */
719   unsigned n, d;		/* length and index for copy */
720   unsigned w;			/* current window position */
721   struct huft *t;		/* pointer to table entry */
722   unsigned ml, md;		/* masks for bl and bd bits */
723   register ulg b;		/* bit buffer */
724   register unsigned k;		/* number of bits in bit buffer */
725 
726   /* make local copies of globals */
727   d = inflate_d;
728   n = inflate_n;
729   b = bb;			/* initialize bit buffer */
730   k = bk;
731   w = wp;			/* initialize window position */
732 
733   /* inflate the coded data */
734   ml = mask_bits[bl];		/* precompute masks for speed */
735   md = mask_bits[bd];
736   for (;;)			/* do until end of block */
737     {
738       if (!code_state)
739 	{
740 	  NEEDBITS ((unsigned) bl);
741 	  if ((e = (t = tl + ((unsigned) b & ml))->e) > 16)
742 	    do
743 	      {
744 		if (e == 99)
745 		  {
746 		    errnum = ERR_BAD_GZIP_DATA;
747 		    return 0;
748 		  }
749 		DUMPBITS (t->b);
750 		e -= 16;
751 		NEEDBITS (e);
752 	      }
753 	    while ((e = (t = t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16);
754 	  DUMPBITS (t->b);
755 
756 	  if (e == 16)		/* then it's a literal */
757 	    {
758 	      slide[w++] = (uch) t->v.n;
759 	      if (w == WSIZE)
760 		break;
761 	    }
762 	  else
763 	    /* it's an EOB or a length */
764 	    {
765 	      /* exit if end of block */
766 	      if (e == 15)
767 		{
768 		  block_len = 0;
769 		  break;
770 		}
771 
772 	      /* get length of block to copy */
773 	      NEEDBITS (e);
774 	      n = t->v.n + ((unsigned) b & mask_bits[e]);
775 	      DUMPBITS (e);
776 
777 	      /* decode distance of block to copy */
778 	      NEEDBITS ((unsigned) bd);
779 	      if ((e = (t = td + ((unsigned) b & md))->e) > 16)
780 		do
781 		  {
782 		    if (e == 99)
783 		      {
784 			errnum = ERR_BAD_GZIP_DATA;
785 			return 0;
786 		      }
787 		    DUMPBITS (t->b);
788 		    e -= 16;
789 		    NEEDBITS (e);
790 		  }
791 		while ((e = (t = t->v.t + ((unsigned) b & mask_bits[e]))->e)
792 		       > 16);
793 	      DUMPBITS (t->b);
794 	      NEEDBITS (e);
795 	      d = w - t->v.n - ((unsigned) b & mask_bits[e]);
796 	      DUMPBITS (e);
797 	      code_state++;
798 	    }
799 	}
800 
801       if (code_state)
802 	{
803 	  /* do the copy */
804 	  do
805 	    {
806 	      n -= (e = (e = WSIZE - ((d &= WSIZE - 1) > w ? d : w)) > n ? n
807 		    : e);
808 	      if (w - d >= e)
809 		{
810 		  memmove (slide + w, slide + d, e);
811 		  w += e;
812 		  d += e;
813 		}
814 	      else
815 		/* purposefully use the overlap for extra copies here!! */
816 		{
817 		  while (e--)
818 		    slide[w++] = slide[d++];
819 		}
820 	      if (w == WSIZE)
821 		break;
822 	    }
823 	  while (n);
824 
825 	  if (!n)
826 	    code_state--;
827 
828 	  /* did we break from the loop too soon? */
829 	  if (w == WSIZE)
830 	    break;
831 	}
832     }
833 
834   /* restore the globals from the locals */
835   inflate_d = d;
836   inflate_n = n;
837   wp = w;			/* restore global window pointer */
838   bb = b;			/* restore global bit buffer */
839   bk = k;
840 
841   return !block_len;
842 }
843 
844 
845 /* get header for an inflated type 0 (stored) block. */
846 
847 static void
init_stored_block(void)848 init_stored_block (void)
849 {
850   register ulg b;		/* bit buffer */
851   register unsigned k;		/* number of bits in bit buffer */
852 
853   /* make local copies of globals */
854   b = bb;			/* initialize bit buffer */
855   k = bk;
856 
857   /* go to byte boundary */
858   DUMPBITS (k & 7);
859 
860   /* get the length and its complement */
861   NEEDBITS (16);
862   block_len = ((unsigned) b & 0xffff);
863   DUMPBITS (16);
864   NEEDBITS (16);
865   if (block_len != (unsigned) ((~b) & 0xffff))
866     errnum = ERR_BAD_GZIP_DATA;
867   DUMPBITS (16);
868 
869   /* restore global variables */
870   bb = b;
871   bk = k;
872 }
873 
874 
875 /* get header for an inflated type 1 (fixed Huffman codes) block.  We should
876    either replace this with a custom decoder, or at least precompute the
877    Huffman tables. */
878 
879 static void
init_fixed_block()880 init_fixed_block ()
881 {
882   int i;			/* temporary variable */
883   unsigned l[288];		/* length list for huft_build */
884 
885   /* set up literal table */
886   for (i = 0; i < 144; i++)
887     l[i] = 8;
888   for (; i < 256; i++)
889     l[i] = 9;
890   for (; i < 280; i++)
891     l[i] = 7;
892   for (; i < 288; i++)		/* make a complete, but wrong code set */
893     l[i] = 8;
894   bl = 7;
895   if ((i = huft_build (l, 288, 257, cplens, cplext, &tl, &bl)) != 0)
896     {
897       errnum = ERR_BAD_GZIP_DATA;
898       return;
899     }
900 
901   /* set up distance table */
902   for (i = 0; i < 30; i++)	/* make an incomplete code set */
903     l[i] = 5;
904   bd = 5;
905   if ((i = huft_build (l, 30, 0, cpdist, cpdext, &td, &bd)) > 1)
906     {
907       errnum = ERR_BAD_GZIP_DATA;
908       return;
909     }
910 
911   /* indicate we're now working on a block */
912   code_state = 0;
913   block_len++;
914 }
915 
916 
917 /* get header for an inflated type 2 (dynamic Huffman codes) block. */
918 
919 static void
init_dynamic_block(void)920 init_dynamic_block (void)
921 {
922   int i;			/* temporary variables */
923   unsigned j;
924   unsigned l;			/* last length */
925   unsigned m;			/* mask for bit lengths table */
926   unsigned n;			/* number of lengths to get */
927   unsigned nb;			/* number of bit length codes */
928   unsigned nl;			/* number of literal/length codes */
929   unsigned nd;			/* number of distance codes */
930   unsigned ll[286 + 30];	/* literal/length and distance code lengths */
931   register ulg b;		/* bit buffer */
932   register unsigned k;		/* number of bits in bit buffer */
933 
934   /* make local bit buffer */
935   b = bb;
936   k = bk;
937 
938   /* read in table lengths */
939   NEEDBITS (5);
940   nl = 257 + ((unsigned) b & 0x1f);	/* number of literal/length codes */
941   DUMPBITS (5);
942   NEEDBITS (5);
943   nd = 1 + ((unsigned) b & 0x1f);	/* number of distance codes */
944   DUMPBITS (5);
945   NEEDBITS (4);
946   nb = 4 + ((unsigned) b & 0xf);	/* number of bit length codes */
947   DUMPBITS (4);
948   if (nl > 286 || nd > 30)
949     {
950       errnum = ERR_BAD_GZIP_DATA;
951       return;
952     }
953 
954   /* read in bit-length-code lengths */
955   for (j = 0; j < nb; j++)
956     {
957       NEEDBITS (3);
958       ll[bitorder[j]] = (unsigned) b & 7;
959       DUMPBITS (3);
960     }
961   for (; j < 19; j++)
962     ll[bitorder[j]] = 0;
963 
964   /* build decoding table for trees--single level, 7 bit lookup */
965   bl = 7;
966   if ((i = huft_build (ll, 19, 19, NULL, NULL, &tl, &bl)) != 0)
967     {
968       errnum = ERR_BAD_GZIP_DATA;
969       return;
970     }
971 
972   /* read in literal and distance code lengths */
973   n = nl + nd;
974   m = mask_bits[bl];
975   i = l = 0;
976   while ((unsigned) i < n)
977     {
978       NEEDBITS ((unsigned) bl);
979       j = (td = tl + ((unsigned) b & m))->b;
980       DUMPBITS (j);
981       j = td->v.n;
982       if (j < 16)		/* length of code in bits (0..15) */
983 	ll[i++] = l = j;	/* save last length in l */
984       else if (j == 16)		/* repeat last length 3 to 6 times */
985 	{
986 	  NEEDBITS (2);
987 	  j = 3 + ((unsigned) b & 3);
988 	  DUMPBITS (2);
989 	  if ((unsigned) i + j > n)
990 	    {
991 	      errnum = ERR_BAD_GZIP_DATA;
992 	      return;
993 	    }
994 	  while (j--)
995 	    ll[i++] = l;
996 	}
997       else if (j == 17)		/* 3 to 10 zero length codes */
998 	{
999 	  NEEDBITS (3);
1000 	  j = 3 + ((unsigned) b & 7);
1001 	  DUMPBITS (3);
1002 	  if ((unsigned) i + j > n)
1003 	    {
1004 	      errnum = ERR_BAD_GZIP_DATA;
1005 	      return;
1006 	    }
1007 	  while (j--)
1008 	    ll[i++] = 0;
1009 	  l = 0;
1010 	}
1011       else
1012 	/* j == 18: 11 to 138 zero length codes */
1013 	{
1014 	  NEEDBITS (7);
1015 	  j = 11 + ((unsigned) b & 0x7f);
1016 	  DUMPBITS (7);
1017 	  if ((unsigned) i + j > n)
1018 	    {
1019 	      errnum = ERR_BAD_GZIP_DATA;
1020 	      return;
1021 	    }
1022 	  while (j--)
1023 	    ll[i++] = 0;
1024 	  l = 0;
1025 	}
1026     }
1027 
1028   /* free decoding table for trees */
1029   reset_linalloc ();
1030 
1031   /* restore the global bit buffer */
1032   bb = b;
1033   bk = k;
1034 
1035   /* build the decoding tables for literal/length and distance codes */
1036   bl = lbits;
1037   if ((i = huft_build (ll, nl, 257, cplens, cplext, &tl, &bl)) != 0)
1038     {
1039 #if 0
1040       if (i == 1)
1041 	printf ("gunzip: incomplete literal tree\n");
1042 #endif
1043 
1044       errnum = ERR_BAD_GZIP_DATA;
1045       return;
1046     }
1047   bd = dbits;
1048   if ((i = huft_build (ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0)
1049     {
1050 #if 0
1051       if (i == 1)
1052 	printf ("gunzip: incomplete distance tree\n");
1053 #endif
1054 
1055       errnum = ERR_BAD_GZIP_DATA;
1056       return;
1057     }
1058 
1059   /* indicate we're now working on a block */
1060   code_state = 0;
1061   block_len++;
1062 }
1063 
1064 
1065 static void
get_new_block(void)1066 get_new_block (void)
1067 {
1068   register ulg b;		/* bit buffer */
1069   register unsigned k;		/* number of bits in bit buffer */
1070 
1071   hufts = 0;
1072 
1073   /* make local bit buffer */
1074   b = bb;
1075   k = bk;
1076 
1077   /* read in last block bit */
1078   NEEDBITS (1);
1079   last_block = (int) b & 1;
1080   DUMPBITS (1);
1081 
1082   /* read in block type */
1083   NEEDBITS (2);
1084   block_type = (unsigned) b & 3;
1085   DUMPBITS (2);
1086 
1087   /* restore the global bit buffer */
1088   bb = b;
1089   bk = k;
1090 
1091   if (block_type == INFLATE_STORED)
1092     init_stored_block ();
1093   if (block_type == INFLATE_FIXED)
1094     init_fixed_block ();
1095   if (block_type == INFLATE_DYNAMIC)
1096     init_dynamic_block ();
1097 }
1098 
1099 
1100 static void
inflate_window(void)1101 inflate_window (void)
1102 {
1103   /* initialize window */
1104   wp = 0;
1105 
1106   /*
1107    *  Main decompression loop.
1108    */
1109 
1110   while (wp < WSIZE && !errnum)
1111     {
1112       if (!block_len)
1113 	{
1114 	  if (last_block)
1115 	    break;
1116 
1117 	  get_new_block ();
1118 	}
1119 
1120       if (block_type > INFLATE_DYNAMIC)
1121 	errnum = ERR_BAD_GZIP_DATA;
1122 
1123       if (errnum)
1124 	return;
1125 
1126       /*
1127        *  Expand stored block here.
1128        */
1129       if (block_type == INFLATE_STORED)
1130 	{
1131 	  int w = wp;
1132 
1133 	  /*
1134 	   *  This is basically a glorified pass-through
1135 	   */
1136 
1137 	  while (block_len && w < WSIZE && !errnum)
1138 	    {
1139 	      slide[w++] = get_byte ();
1140 	      block_len--;
1141 	    }
1142 
1143 	  wp = w;
1144 
1145 	  continue;
1146 	}
1147 
1148       /*
1149        *  Expand other kind of block.
1150        */
1151 
1152       if (inflate_codes_in_window ())
1153 	reset_linalloc ();
1154     }
1155 
1156   saved_filepos += WSIZE;
1157 
1158   /* XXX do CRC calculation here! */
1159 }
1160 
1161 
1162 static void
initialize_tables(void)1163 initialize_tables (void)
1164 {
1165   saved_filepos = 0;
1166   filepos = gzip_data_offset;
1167 
1168   /* initialize window, bit buffer */
1169   bk = 0;
1170   bb = 0;
1171 
1172   /* reset partial decompression code */
1173   last_block = 0;
1174   block_len = 0;
1175 
1176   /* reset memory allocation stuff */
1177   reset_linalloc ();
1178 }
1179 
1180 
1181 int
gunzip_read(char * buf,int len)1182 gunzip_read (char *buf, int len)
1183 {
1184   int ret = 0;
1185 
1186   compressed_file = 0;
1187   gunzip_swap_values ();
1188   /*
1189    *  Now "gzip_*" values refer to the uncompressed data.
1190    */
1191 
1192   /* do we reset decompression to the beginning of the file? */
1193   if (saved_filepos > gzip_filepos + WSIZE)
1194     initialize_tables ();
1195 
1196   /*
1197    *  This loop operates upon uncompressed data only.  The only
1198    *  special thing it does is to make sure the decompression
1199    *  window is within the range of data it needs.
1200    */
1201 
1202   while (len > 0 && !errnum)
1203     {
1204       register int size;
1205       register char *srcaddr;
1206 
1207       while (gzip_filepos >= saved_filepos)
1208 	inflate_window ();
1209 
1210       srcaddr = (char *) ((gzip_filepos & (WSIZE - 1)) + slide);
1211       size = saved_filepos - gzip_filepos;
1212       if (size > len)
1213 	size = len;
1214 
1215       memmove (buf, srcaddr, size);
1216 
1217       buf += size;
1218       len -= size;
1219       gzip_filepos += size;
1220       ret += size;
1221     }
1222 
1223   compressed_file = 1;
1224   gunzip_swap_values ();
1225   /*
1226    *  Now "gzip_*" values refer to the compressed data.
1227    */
1228 
1229   if (errnum)
1230     ret = 0;
1231 
1232   return ret;
1233 }
1234 
1235 #endif /* ! NO_DECOMPRESSION */
1236