]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/bootstrap/server/src/gunzip.c
832ff9af52d21935f2131b888799548535427d35
[l4.git] / l4 / pkg / bootstrap / server / src / gunzip.c
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 <stdlib.h>
129 #include <string.h>
130 #include <stdio.h>
131
132 #include "gunzip.h"
133
134 unsigned int filepos;
135 unsigned int filemax;
136 unsigned int fsmax;     /* max size of fs/readable media */
137
138 grub_error_t errnum;
139
140 /* whether to show decompression progress or not */
141 enum { do_show_progress = 1 };
142
143 /* so we can disable decompression  */
144 int no_decompression = 0;
145
146 /* used to tell if "read" should be redirected to "gunzip_read" */
147 unsigned int compressed_file;
148
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;
156
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;
162
163
164 /* Function prototypes */
165 static void initialize_tables (void);
166
167 static void show_progress(int done, int len)
168 {
169   int r = printf("%d%%", (done * 100) / len);
170   while (r-- > 0)
171     putchar('\b');
172   fflush(NULL);
173 }
174
175 /*
176  *  Linear allocator.
177  */
178
179 static unsigned long linalloc_topaddr;
180
181 static void *
182 linalloc (int size)
183 {
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;
190 }
191
192 static void
193 reset_linalloc (void)
194 {
195   linalloc_topaddr = RAW_ADDR (UPPER_MEM_LINALLOC);
196 }
197
198
199 /* internal variable swap function */
200 static void
201 gunzip_swap_values (void)
202 {
203   register int itmp;
204
205   /* swap filepos */
206   itmp = filepos;
207   filepos = gzip_filepos;
208   gzip_filepos = itmp;
209
210   /* swap filemax */
211   itmp = filemax;
212   filemax = gzip_filemax;
213   gzip_filemax = itmp;
214
215   /* swap fsmax */
216   itmp = fsmax;
217   fsmax = gzip_fsmax;
218   gzip_fsmax = itmp;
219 }
220
221
222 /* internal function for eating variable-length header fields */
223 static int
224 bad_field (int len)
225 {
226   unsigned char ch = 1;
227   int not_retval = 1;
228
229   do
230     {
231       if (len >= 0)
232         {
233           if (!(len--))
234             break;
235         }
236       else
237         {
238           if (!ch)
239             break;
240         }
241     }
242   while ((not_retval = grub_read (&ch, 1)) == 1);
243
244   return (!not_retval);
245 }
246
247
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
251
252 /* Compression methods (see algorithm.doc) */
253 #define STORED      0
254 #define COMPRESSED  1
255 #define PACKED      2
256 #define LZHED       3
257 /* methods 4 to 7 reserved */
258 #define DEFLATED    8
259 #define MAX_METHODS 9
260
261 /* gzip flag byte */
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 */
269
270 #define UNSUPP_FLAGS (CONTINUATION|ENCRYPTED|RESERVED)
271
272 /* inflate block codes */
273 #define INFLATE_STORED    0
274 #define INFLATE_FIXED     1
275 #define INFLATE_DYNAMIC   2
276
277 typedef unsigned char uch;
278 typedef unsigned short ush;
279 typedef unsigned long ulg;
280
281 /*
282  *  Window Size
283  *
284  *  This must be a power of two, and at least 32K for zip's deflate method
285  */
286
287 #define WSIZE 0x8000
288
289
290 int
291 gunzip_test_header (void)
292 {
293   unsigned char buf[10] __attribute__((aligned(4)));
294   
295   /* "compressed_file" is already reset to zero by this point */
296
297   /*
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.
301    */
302   if (no_decompression
303       || grub_read (buf, 10) != 10
304       || ((*((unsigned short *) buf) != GZIP_HDR_LE)
305           && (*((unsigned short *) buf) != OLD_GZIP_HDR_LE)))
306     {
307       filepos = 0;
308       return ! errnum;
309     }
310
311   /*
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.
315    */
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)))
323     {
324       if (! errnum)
325         errnum = ERR_BAD_GZIP_HEADER;
326       
327       return 0;
328     }
329
330   gzip_data_offset = filepos;
331   
332   filepos = filemax - 8;
333   
334   if (grub_read (buf, 8) != 8)
335     {
336       if (! errnum)
337         errnum = ERR_BAD_GZIP_HEADER;
338       
339       return 0;
340     }
341
342   gzip_crc = *((unsigned long *) buf);
343   gzip_fsmax = gzip_filemax = *((unsigned long *) (buf + 4));
344
345   initialize_tables ();
346
347   compressed_file = 1;
348   gunzip_swap_values ();
349   /*
350    *  Now "gzip_*" values refer to the compressed data.
351    */
352
353   filepos = 0;
354
355   return 1;
356 }
357
358
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. */
366 struct huft
367 {
368   uch e;                        /* number of extra bits or operation */
369   uch b;                        /* number of bits in this code or subcode */
370   union
371     {
372       ush n;                    /* literal, length base, or distance base */
373       struct huft *t;           /* pointer to next level of table */
374     }
375   v;
376 };
377
378
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. */
387
388
389 /* sliding window in uncompressed data */
390 static uch slide[WSIZE];
391
392 /* current position in slide */
393 static unsigned wp;
394
395
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,
418   12, 12, 13, 13};
419
420
421 /*
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.
432
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
441    used.
442
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.
451  */
452
453
454 static int lbits = 9;           /* bits in base literal/length lookup table */
455 static int dbits = 6;           /* bits in base distance lookup table */
456
457
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 */
461
462
463 static unsigned hufts;          /* track memory usage */
464
465
466 /* Macros for inflate() bit peeking and grabbing.
467    The usage is:
468
469         NEEDBITS(j)
470         x = b & mask_bits[j];
471         DUMPBITS(j)
472
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.
478
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.
484
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
493    the stream.
494  */
495
496 static ulg bb;                  /* bit buffer */
497 static unsigned bk;             /* bits in bit buffer */
498
499 static ush mask_bits[] =
500 {
501   0x0000,
502   0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
503   0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
504 };
505
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)
508
509 #define INBUFSIZ  0x2000
510
511 static unsigned char inbuf[INBUFSIZ];
512 static int bufloc;
513
514 static int
515 get_byte (void)
516 {
517   if (filepos == gzip_data_offset || bufloc == INBUFSIZ)
518     {
519       bufloc = 0;
520       grub_read (inbuf, INBUFSIZ);
521     }
522
523   return inbuf[bufloc++];
524 }
525
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 */
531
532
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);
537
538
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. */
544
545 static int
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 */
553 {
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 */
573
574   /* Generate counts for each bit length */
575   memset ((char *) c, 0, sizeof (c));
576   p = b;
577   i = n;
578   do
579     {
580       c[*p]++;                  /* assume all entries <= BMAX */
581       p++;                      /* Can't combine with above line (Solaris bug) */
582     }
583   while (--i);
584   if (c[0] == n)                /* null input--all zero length codes */
585     {
586       *t = (struct huft *) NULL;
587       *m = 0;
588       return 0;
589     }
590
591   /* Find minimum and maximum length, bound *m by those */
592   l = *m;
593   for (j = 1; j <= BMAX; j++)
594     if (c[j])
595       break;
596   k = j;                        /* minimum code length */
597   if ((unsigned) l < j)
598     l = j;
599   for (i = BMAX; i; i--)
600     if (c[i])
601       break;
602   g = i;                        /* maximum code length */
603   if ((unsigned) l > i)
604     l = i;
605   *m = l;
606
607   /* Adjust last length count to fill out codes, if needed */
608   for (y = 1 << j; j < i; j++, y <<= 1)
609     if ((y -= c[j]) < 0)
610       return 2;                 /* bad input: more codes than bits */
611   if ((y -= c[i]) < 0)
612     return 2;
613   c[i] += y;
614
615   /* Generate starting offsets into the value table for each length */
616   x[1] = j = 0;
617   p = c + 1;
618   xp = x + 2;
619   while (--i)
620     {                           /* note that i == g from above */
621       *xp++ = (j += *p++);
622     }
623
624   /* Make a table of values in order of bit lengths */
625   p = b;
626   i = 0;
627   do
628     {
629       if ((j = *p++) != 0)
630         v[x[j]++] = i;
631     }
632   while (++i < n);
633
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 */
641   z = 0;                        /* ditto */
642
643   /* go through the bit lengths (k already is bits in shortest code) */
644   for (; k <= g; k++)
645     {
646       a = c[k];
647       while (a--)
648         {
649           /* here i is the Huffman code of length k bits for value *p */
650           /* make tables up to required level */
651           while (k > w + l)
652             {
653               h++;
654               w += l;           /* previous table always l bits */
655
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 */
661                   xp = c + k;
662                   while (++j < z)       /* try smaller tables up to z bits */
663                     {
664                       if ((f <<= 1) <= *++xp)
665                         break;  /* enough codes to use up j bits */
666                       f -= *xp; /* else deduct codes from patterns */
667                     }
668                 }
669               z = 1 << j;       /* table entries for j-bit table */
670
671               /* allocate and link in new table */
672               q = (struct huft *) linalloc ((z + 1) * sizeof (struct huft));
673
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 */
678
679               /* connect to last table, if there is one */
680               if (h)
681                 {
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 */
688                 }
689             }
690
691           /* set up table entry in r */
692           r.b = (uch) (k - w);
693           if (p >= v + n)
694             r.e = 99;           /* out of values--invalid code */
695           else if (*p < s)
696             {
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++ */
700             }
701           else
702             {
703               r.e = (uch) e[*p - s];    /* non-simple--look up in lists */
704               r.v.n = d[*p++ - s];
705             }
706
707           /* fill code-like entries with r */
708           f = 1 << (k - w);
709           for (j = i >> w; j < z; j += f)
710             q[j] = r;
711
712           /* backwards increment the k-bit code i */
713           for (j = 1 << (k - 1); i & j; j >>= 1)
714             i ^= j;
715           i ^= j;
716
717           /* backup over finished tables */
718           while ((i & ((1 << w) - 1)) != x[h])
719             {
720               h--;              /* don't need to update q */
721               w -= l;
722             }
723         }
724     }
725
726   /* Return true (1) if we were given an incomplete table */
727   return y != 0 && g != 1;
728 }
729
730
731 /*
732  *  inflate (decompress) the codes in a deflated (compressed) block.
733  *  Return an error code or zero if it all goes ok.
734  */
735
736 static unsigned inflate_n, inflate_d;
737
738 static int
739 inflate_codes_in_window (void)
740 {
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 */
748
749   /* make local copies of globals */
750   d = inflate_d;
751   n = inflate_n;
752   b = bb;                       /* initialize bit buffer */
753   k = bk;
754   w = wp;                       /* initialize window position */
755
756   /* inflate the coded data */
757   ml = mask_bits[bl];           /* precompute masks for speed */
758   md = mask_bits[bd];
759   for (;;)                      /* do until end of block */
760     {
761       if (!code_state)
762         {
763           NEEDBITS ((unsigned) bl);
764           if ((e = (t = tl + ((unsigned) b & ml))->e) > 16)
765             do
766               {
767                 if (e == 99)
768                   {
769                     errnum = ERR_BAD_GZIP_DATA;
770                     return 0;
771                   }
772                 DUMPBITS (t->b);
773                 e -= 16;
774                 NEEDBITS (e);
775               }
776             while ((e = (t = t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16);
777           DUMPBITS (t->b);
778
779           if (e == 16)          /* then it's a literal */
780             {
781               slide[w++] = (uch) t->v.n;
782               if (w == WSIZE)
783                 break;
784             }
785           else
786             /* it's an EOB or a length */
787             {
788               /* exit if end of block */
789               if (e == 15)
790                 {
791                   block_len = 0;
792                   break;
793                 }
794
795               /* get length of block to copy */
796               NEEDBITS (e);
797               n = t->v.n + ((unsigned) b & mask_bits[e]);
798               DUMPBITS (e);
799
800               /* decode distance of block to copy */
801               NEEDBITS ((unsigned) bd);
802               if ((e = (t = td + ((unsigned) b & md))->e) > 16)
803                 do
804                   {
805                     if (e == 99)
806                       {
807                         errnum = ERR_BAD_GZIP_DATA;
808                         return 0;
809                       }
810                     DUMPBITS (t->b);
811                     e -= 16;
812                     NEEDBITS (e);
813                   }
814                 while ((e = (t = t->v.t + ((unsigned) b & mask_bits[e]))->e)
815                        > 16);
816               DUMPBITS (t->b);
817               NEEDBITS (e);
818               d = w - t->v.n - ((unsigned) b & mask_bits[e]);
819               DUMPBITS (e);
820               code_state++;
821             }
822         }
823
824       if (code_state)
825         {
826           /* do the copy */
827           do
828             {
829               n -= (e = (e = WSIZE - ((d &= WSIZE - 1) > w ? d : w)) > n ? n
830                     : e);
831               if (w - d >= e)
832                 {
833                   memmove (slide + w, slide + d, e);
834                   w += e;
835                   d += e;
836                 }
837               else
838                 /* purposefully use the overlap for extra copies here!! */
839                 {
840                   while (e--)
841                     slide[w++] = slide[d++];
842                 }
843               if (w == WSIZE)
844                 break;
845             }
846           while (n);
847
848           if (!n)
849             code_state--;
850
851           /* did we break from the loop too soon? */
852           if (w == WSIZE)
853             break;
854         }
855     }
856
857   /* restore the globals from the locals */
858   inflate_d = d;
859   inflate_n = n;
860   wp = w;                       /* restore global window pointer */
861   bb = b;                       /* restore global bit buffer */
862   bk = k;
863
864   return !block_len;
865 }
866
867
868 /* get header for an inflated type 0 (stored) block. */
869
870 static void
871 init_stored_block (void)
872 {
873   register ulg b;               /* bit buffer */
874   register unsigned k;          /* number of bits in bit buffer */
875
876   /* make local copies of globals */
877   b = bb;                       /* initialize bit buffer */
878   k = bk;
879
880   /* go to byte boundary */
881   DUMPBITS (k & 7);
882
883   /* get the length and its complement */
884   NEEDBITS (16);
885   block_len = ((unsigned) b & 0xffff);
886   DUMPBITS (16);
887   NEEDBITS (16);
888   if (block_len != (unsigned) ((~b) & 0xffff))
889     errnum = ERR_BAD_GZIP_DATA;
890   DUMPBITS (16);
891
892   /* restore global variables */
893   bb = b;
894   bk = k;
895 }
896
897
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
900    Huffman tables. */
901
902 static void
903 init_fixed_block (void)
904 {
905   int i;                        /* temporary variable */
906   unsigned l[288];              /* length list for huft_build */
907
908   /* set up literal table */
909   for (i = 0; i < 144; i++)
910     l[i] = 8;
911   for (; i < 256; i++)
912     l[i] = 9;
913   for (; i < 280; i++)
914     l[i] = 7;
915   for (; i < 288; i++)          /* make a complete, but wrong code set */
916     l[i] = 8;
917   bl = 7;
918   if ((i = huft_build (l, 288, 257, cplens, cplext, &tl, &bl)) != 0)
919     {
920       errnum = ERR_BAD_GZIP_DATA;
921       return;
922     }
923
924   /* set up distance table */
925   for (i = 0; i < 30; i++)      /* make an incomplete code set */
926     l[i] = 5;
927   bd = 5;
928   if ((i = huft_build (l, 30, 0, cpdist, cpdext, &td, &bd)) > 1)
929     {
930       errnum = ERR_BAD_GZIP_DATA;
931       return;
932     }
933
934   /* indicate we're now working on a block */
935   code_state = 0;
936   block_len++;
937 }
938
939
940 /* get header for an inflated type 2 (dynamic Huffman codes) block. */
941
942 static void
943 init_dynamic_block (void)
944 {
945   int i;                        /* temporary variables */
946   unsigned j;
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 */
956
957   /* make local bit buffer */
958   b = bb;
959   k = bk;
960
961   /* read in table lengths */
962   NEEDBITS (5);
963   nl = 257 + ((unsigned) b & 0x1f);     /* number of literal/length codes */
964   DUMPBITS (5);
965   NEEDBITS (5);
966   nd = 1 + ((unsigned) b & 0x1f);       /* number of distance codes */
967   DUMPBITS (5);
968   NEEDBITS (4);
969   nb = 4 + ((unsigned) b & 0xf);        /* number of bit length codes */
970   DUMPBITS (4);
971   if (nl > 286 || nd > 30)
972     {
973       errnum = ERR_BAD_GZIP_DATA;
974       return;
975     }
976
977   /* read in bit-length-code lengths */
978   for (j = 0; j < nb; j++)
979     {
980       NEEDBITS (3);
981       ll[bitorder[j]] = (unsigned) b & 7;
982       DUMPBITS (3);
983     }
984   for (; j < 19; j++)
985     ll[bitorder[j]] = 0;
986
987   /* build decoding table for trees--single level, 7 bit lookup */
988   bl = 7;
989   if ((i = huft_build (ll, 19, 19, NULL, NULL, &tl, &bl)) != 0)
990     {
991       errnum = ERR_BAD_GZIP_DATA;
992       return;
993     }
994
995   /* read in literal and distance code lengths */
996   n = nl + nd;
997   m = mask_bits[bl];
998   i = l = 0;
999   while ((unsigned) i < n)
1000     {
1001       NEEDBITS ((unsigned) bl);
1002       j = (td = tl + ((unsigned) b & m))->b;
1003       DUMPBITS (j);
1004       j = td->v.n;
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 */
1008         {
1009           NEEDBITS (2);
1010           j = 3 + ((unsigned) b & 3);
1011           DUMPBITS (2);
1012           if ((unsigned) i + j > n)
1013             {
1014               errnum = ERR_BAD_GZIP_DATA;
1015               return;
1016             }
1017           while (j--)
1018             ll[i++] = l;
1019         }
1020       else if (j == 17)         /* 3 to 10 zero length codes */
1021         {
1022           NEEDBITS (3);
1023           j = 3 + ((unsigned) b & 7);
1024           DUMPBITS (3);
1025           if ((unsigned) i + j > n)
1026             {
1027               errnum = ERR_BAD_GZIP_DATA;
1028               return;
1029             }
1030           while (j--)
1031             ll[i++] = 0;
1032           l = 0;
1033         }
1034       else
1035         /* j == 18: 11 to 138 zero length codes */
1036         {
1037           NEEDBITS (7);
1038           j = 11 + ((unsigned) b & 0x7f);
1039           DUMPBITS (7);
1040           if ((unsigned) i + j > n)
1041             {
1042               errnum = ERR_BAD_GZIP_DATA;
1043               return;
1044             }
1045           while (j--)
1046             ll[i++] = 0;
1047           l = 0;
1048         }
1049     }
1050
1051   /* free decoding table for trees */
1052   reset_linalloc ();
1053
1054   /* restore the global bit buffer */
1055   bb = b;
1056   bk = k;
1057
1058   /* build the decoding tables for literal/length and distance codes */
1059   bl = lbits;
1060   if ((i = huft_build (ll, nl, 257, cplens, cplext, &tl, &bl)) != 0)
1061     {
1062 #if 0
1063       if (i == 1)
1064         printf ("gunzip: incomplete literal tree\n");
1065 #endif
1066
1067       errnum = ERR_BAD_GZIP_DATA;
1068       return;
1069     }
1070   bd = dbits;
1071   if ((i = huft_build (ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0)
1072     {
1073 #if 0
1074       if (i == 1)
1075         printf ("gunzip: incomplete distance tree\n");
1076 #endif
1077
1078       errnum = ERR_BAD_GZIP_DATA;
1079       return;
1080     }
1081
1082   /* indicate we're now working on a block */
1083   code_state = 0;
1084   block_len++;
1085 }
1086
1087
1088 static void
1089 get_new_block (void)
1090 {
1091   register ulg b;               /* bit buffer */
1092   register unsigned k;          /* number of bits in bit buffer */
1093
1094   hufts = 0;
1095
1096   /* make local bit buffer */
1097   b = bb;
1098   k = bk;
1099
1100   /* read in last block bit */
1101   NEEDBITS (1);
1102   last_block = (int) b & 1;
1103   DUMPBITS (1);
1104
1105   /* read in block type */
1106   NEEDBITS (2);
1107   block_type = (unsigned) b & 3;
1108   DUMPBITS (2);
1109
1110   /* restore the global bit buffer */
1111   bb = b;
1112   bk = k;
1113
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 ();
1120 }
1121
1122
1123 static void
1124 inflate_window (void)
1125 {
1126   /* initialize window */
1127   wp = 0;
1128
1129   /*
1130    *  Main decompression loop.
1131    */
1132
1133   while (wp < WSIZE && !errnum)
1134     {
1135       if (!block_len)
1136         {
1137           if (last_block)
1138             break;
1139
1140           get_new_block ();
1141         }
1142
1143       if (block_type > INFLATE_DYNAMIC)
1144         errnum = ERR_BAD_GZIP_DATA;
1145
1146       if (errnum)
1147         return;
1148
1149       /*
1150        *  Expand stored block here.
1151        */
1152       if (block_type == INFLATE_STORED)
1153         {
1154           int w = wp;
1155
1156           /*
1157            *  This is basically a glorified pass-through
1158            */
1159
1160           while (block_len && w < WSIZE && !errnum)
1161             {
1162               slide[w++] = get_byte ();
1163               block_len--;
1164             }
1165
1166           wp = w;
1167
1168           continue;
1169         }
1170
1171       /*
1172        *  Expand other kind of block.
1173        */
1174
1175       if (inflate_codes_in_window ())
1176         reset_linalloc ();
1177     }
1178
1179   saved_filepos += WSIZE;
1180
1181   /* XXX do CRC calculation here! */
1182 }
1183
1184
1185 static void
1186 initialize_tables (void)
1187 {
1188   saved_filepos = 0;
1189   filepos = gzip_data_offset;
1190
1191   /* initialize window, bit buffer */
1192   bk = 0;
1193   bb = 0;
1194
1195   /* reset partial decompression code */
1196   last_block = 0;
1197   block_len = 0;
1198
1199   /* reset memory allocation stuff */
1200   reset_linalloc ();
1201 }
1202
1203
1204 int
1205 gunzip_read (unsigned char *buf, int len)
1206 {
1207   int ret = 0;
1208   int real_len = len;
1209
1210   compressed_file = 0;
1211   gunzip_swap_values ();
1212   /*
1213    *  Now "gzip_*" values refer to the uncompressed data.
1214    */
1215
1216   /* do we reset decompression to the beginning of the file? */
1217   if (saved_filepos > gzip_filepos + WSIZE)
1218     initialize_tables ();
1219
1220   /*
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.
1224    */
1225
1226   while (len > 0 && !errnum)
1227     {
1228       register int size;
1229       register char *srcaddr;
1230
1231       while (gzip_filepos >= saved_filepos)
1232         inflate_window ();
1233
1234       srcaddr = (char *) ((gzip_filepos & (WSIZE - 1)) + slide);
1235       size = saved_filepos - gzip_filepos;
1236       if (size > len)
1237         size = len;
1238
1239       memmove (buf, srcaddr, size);
1240
1241       buf += size;
1242       len -= size;
1243       gzip_filepos += size;
1244       ret += size;
1245
1246       if (do_show_progress)
1247         show_progress(ret, real_len);
1248     }
1249
1250   compressed_file = 1;
1251   gunzip_swap_values ();
1252   /*
1253    *  Now "gzip_*" values refer to the compressed data.
1254    */
1255
1256   if (errnum)
1257     ret = 0;
1258
1259   return ret;
1260 }
1261
1262 #endif /* ! NO_DECOMPRESSION */