Implement checksum verification for gunzip
[grub.git] / grub-core / io / gzio.c
1 /* gzio.c - decompression support for gzip */
2 /*
3  *  GRUB  --  GRand Unified Bootloader
4  *  Copyright (C) 1999,2005,2006,2007,2009  Free Software Foundation, Inc.
5  *
6  *  GRUB is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  GRUB is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
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 #include <grub/err.h>
38 #include <grub/types.h>
39 #include <grub/mm.h>
40 #include <grub/misc.h>
41 #include <grub/fs.h>
42 #include <grub/file.h>
43 #include <grub/dl.h>
44 #include <grub/deflate.h>
45 #include <grub/i18n.h>
46 #include <grub/crypto.h>
47
48 GRUB_MOD_LICENSE ("GPLv3+");
49
50 /*
51  *  Window Size
52  *
53  *  This must be a power of two, and at least 32K for zip's deflate method
54  */
55
56 #define WSIZE   0x8000
57
58
59 #define INBUFSIZ  0x2000
60
61 /* The state stored in filesystem-specific data.  */
62 struct grub_gzio
63 {
64   /* The underlying file object.  */
65   grub_file_t file;
66   /* If input is in memory following fields are used instead of file.  */
67   grub_size_t mem_input_size, mem_input_off;
68   grub_uint8_t *mem_input;
69   /* The offset at which the data starts in the underlying file.  */
70   grub_off_t data_offset;
71   /* The type of current block.  */
72   int block_type;
73   /* The length of current block.  */
74   int block_len;
75   /* The flag of the last block.  */
76   int last_block;
77   /* The flag of codes.  */
78   int code_state;
79   /* The length of a copy.  */
80   unsigned inflate_n;
81   /* The index of a copy.  */
82   unsigned inflate_d;
83   /* The input buffer.  */
84   grub_uint8_t inbuf[INBUFSIZ];
85   int inbuf_d;
86   /* The bit buffer.  */
87   unsigned long bb;
88   /* The bits in the bit buffer.  */
89   unsigned bk;
90   /* The sliding window in uncompressed data.  */
91   grub_uint8_t slide[WSIZE];
92   /* Current position in the slide.  */
93   unsigned wp;
94   /* The literal/length code table.  */
95   struct huft *tl;
96   /* The distance code table.  */
97   struct huft *td;
98   /* The checksum algorithm */
99   const gcry_md_spec_t *hdesc;
100   /* The wanted checksum */
101   grub_uint32_t orig_checksum;
102   /* The uncompressed length */
103   grub_size_t orig_len;
104   /* Context for checksum calculation */
105   grub_uint8_t *hcontext;
106   /* The lookup bits for the literal/length code table. */
107   int bl;
108   /* The lookup bits for the distance code table.  */
109   int bd;
110   /* The original offset value.  */
111   grub_off_t saved_offset;
112 };
113 typedef struct grub_gzio *grub_gzio_t;
114
115 /* Declare the filesystem structure for grub_gzio_open.  */
116 static struct grub_fs grub_gzio_fs;
117
118 /* Function prototypes */
119 static void initialize_tables (grub_gzio_t);
120
121 /* Eat variable-length header fields.  */
122 static int
123 eat_field (grub_file_t file, int len)
124 {
125   char ch = 1;
126   int not_retval = 1;
127
128   do
129     {
130       if (len >= 0)
131         {
132           if (! (len--))
133             break;
134         }
135       else
136         {
137           if (! ch)
138             break;
139         }
140     }
141   while ((not_retval = grub_file_read (file, &ch, 1)) == 1);
142
143   return ! not_retval;
144 }
145
146
147 /* Little-Endian defines for the 2-byte magic numbers for gzip files.  */
148 #define GZIP_MAGIC      grub_le_to_cpu16 (0x8B1F)
149 #define OLD_GZIP_MAGIC  grub_le_to_cpu16 (0x9E1F)
150
151 /* Compression methods (see algorithm.doc) */
152 #define GRUB_GZ_STORED      0
153 #define GRUB_GZ_COMPRESSED  1
154 #define GRUB_GZ_PACKED      2
155 #define GRUB_GZ_LZHED       3
156 /* methods 4 to 7 reserved */
157 #define GRUB_GZ_DEFLATED    8
158 #define GRUB_GZ_MAX_METHODS 9
159
160 /* gzip flag byte */
161 #define GRUB_GZ_ASCII_FLAG   0x01       /* bit 0 set: file probably ascii text */
162 #define GRUB_GZ_CONTINUATION 0x02       /* bit 1 set: continuation of multi-part gzip file */
163 #define GRUB_GZ_EXTRA_FIELD  0x04       /* bit 2 set: extra field present */
164 #define GRUB_GZ_ORIG_NAME    0x08       /* bit 3 set: original file name present */
165 #define GRUB_GZ_COMMENT      0x10       /* bit 4 set: file comment present */
166 #define GRUB_GZ_ENCRYPTED    0x20       /* bit 5 set: file is encrypted */
167 #define GRUB_GZ_RESERVED     0xC0       /* bit 6,7:   reserved */
168
169 #define GRUB_GZ_UNSUPPORTED_FLAGS       (GRUB_GZ_CONTINUATION | GRUB_GZ_ENCRYPTED | GRUB_GZ_RESERVED)
170
171 /* inflate block codes */
172 #define INFLATE_STORED  0
173 #define INFLATE_FIXED   1
174 #define INFLATE_DYNAMIC 2
175
176 typedef unsigned char uch;
177 typedef unsigned short ush;
178 typedef unsigned long ulg;
179
180 static int
181 test_gzip_header (grub_file_t file)
182 {
183   struct {
184     grub_uint16_t magic;
185     grub_uint8_t method;
186     grub_uint8_t flags;
187     grub_uint32_t timestamp;
188     grub_uint8_t extra_flags;
189     grub_uint8_t os_type;
190   } hdr;
191   grub_uint16_t extra_len;
192   grub_uint32_t crc32;
193   grub_gzio_t gzio = file->data;
194
195   if (grub_file_tell (gzio->file) != 0)
196     grub_file_seek (gzio->file, 0);
197
198   /*
199    *  This checks if the file is gzipped.  If a problem occurs here
200    *  (other than a real error with the disk) then we don't think it
201    *  is a compressed file, and simply mark it as such.
202    */
203   if (grub_file_read (gzio->file, &hdr, 10) != 10
204       || ((hdr.magic != GZIP_MAGIC)
205           && (hdr.magic != OLD_GZIP_MAGIC)))
206     return 0;
207
208   /*
209    *  This does consistency checking on the header data.  If a
210    *  problem occurs from here on, then we have corrupt or otherwise
211    *  bad data, and the error should be reported to the user.
212    */
213   if (hdr.method != GRUB_GZ_DEFLATED
214       || (hdr.flags & GRUB_GZ_UNSUPPORTED_FLAGS)
215       || ((hdr.flags & GRUB_GZ_EXTRA_FIELD)
216           && (grub_file_read (gzio->file, &extra_len, 2) != 2
217               || eat_field (gzio->file,
218                             grub_le_to_cpu16 (extra_len))))
219       || ((hdr.flags & GRUB_GZ_ORIG_NAME) && eat_field (gzio->file, -1))
220       || ((hdr.flags & GRUB_GZ_COMMENT) && eat_field (gzio->file, -1)))
221     return 0;
222
223   gzio->data_offset = grub_file_tell (gzio->file);
224
225   /* FIXME: don't do this on not easily seekable files.  */
226   {
227     grub_file_seek (gzio->file, grub_file_size (gzio->file) - 8);
228     if (grub_file_read (gzio->file, &crc32, 4) != 4)
229       return 0;
230     gzio->orig_checksum = grub_le_to_cpu32 (crc32);
231     if (grub_file_read (gzio->file, &gzio->orig_len, 4) != 4)
232       return 0;
233     /* FIXME: this does not handle files whose original size is over 4GB.
234        But how can we know the real original size?  */
235     file->size = grub_le_to_cpu32 (gzio->orig_len);
236   }
237
238   initialize_tables (gzio);
239
240   return 1;
241 }
242
243
244 /* Huffman code lookup table entry--this entry is four bytes for machines
245    that have 16-bit pointers (e.g. PC's in the small or medium model).
246    Valid extra bits are 0..13.  e == 15 is EOB (end of block), e == 16
247    means that v is a literal, 16 < e < 32 means that v is a pointer to
248    the next table, which codes e - 16 bits, and lastly e == 99 indicates
249    an unused code.  If a code with e == 99 is looked up, this implies an
250    error in the data. */
251 struct huft
252 {
253   uch e;                        /* number of extra bits or operation */
254   uch b;                        /* number of bits in this code or subcode */
255   union
256     {
257       ush n;                    /* literal, length base, or distance base */
258       struct huft *t;           /* pointer to next level of table */
259     }
260   v;
261 };
262
263
264 /* The inflate algorithm uses a sliding 32K byte window on the uncompressed
265    stream to find repeated byte strings.  This is implemented here as a
266    circular buffer.  The index is updated simply by incrementing and then
267    and'ing with 0x7fff (32K-1). */
268 /* It is left to other modules to supply the 32K area.  It is assumed
269    to be usable as if it were declared "uch slide[32768];" or as just
270    "uch *slide;" and then malloc'ed in the latter case.  The definition
271    must be in unzip.h, included above. */
272
273
274 /* Tables for deflate from PKZIP's appnote.txt. */
275 static unsigned bitorder[] =
276 {                               /* Order of the bit length code lengths */
277   16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
278 static ush cplens[] =
279 {                               /* Copy lengths for literal codes 257..285 */
280   3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
281   35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
282         /* note: see note #13 above about the 258 in this list. */
283 static ush cplext[] =
284 {                               /* Extra bits for literal codes 257..285 */
285   0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
286   3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99};       /* 99==invalid */
287 static ush cpdist[] =
288 {                               /* Copy offsets for distance codes 0..29 */
289   1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
290   257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
291   8193, 12289, 16385, 24577};
292 static ush cpdext[] =
293 {                               /* Extra bits for distance codes */
294   0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
295   7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
296   12, 12, 13, 13};
297
298
299 /*
300    Huffman code decoding is performed using a multi-level table lookup.
301    The fastest way to decode is to simply build a lookup table whose
302    size is determined by the longest code.  However, the time it takes
303    to build this table can also be a factor if the data being decoded
304    is not very long.  The most common codes are necessarily the
305    shortest codes, so those codes dominate the decoding time, and hence
306    the speed.  The idea is you can have a shorter table that decodes the
307    shorter, more probable codes, and then point to subsidiary tables for
308    the longer codes.  The time it costs to decode the longer codes is
309    then traded against the time it takes to make longer tables.
310
311    This results of this trade are in the variables lbits and dbits
312    below.  lbits is the number of bits the first level table for literal/
313    length codes can decode in one step, and dbits is the same thing for
314    the distance codes.  Subsequent tables are also less than or equal to
315    those sizes.  These values may be adjusted either when all of the
316    codes are shorter than that, in which case the longest code length in
317    bits is used, or when the shortest code is *longer* than the requested
318    table size, in which case the length of the shortest code in bits is
319    used.
320
321    There are two different values for the two tables, since they code a
322    different number of possibilities each.  The literal/length table
323    codes 286 possible values, or in a flat code, a little over eight
324    bits.  The distance table codes 30 possible values, or a little less
325    than five bits, flat.  The optimum values for speed end up being
326    about one bit more than those, so lbits is 8+1 and dbits is 5+1.
327    The optimum values may differ though from machine to machine, and
328    possibly even between compilers.  Your mileage may vary.
329  */
330
331
332 static int lbits = 9;           /* bits in base literal/length lookup table */
333 static int dbits = 6;           /* bits in base distance lookup table */
334
335
336 /* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
337 #define BMAX 16                 /* maximum bit length of any code (16 for explode) */
338 #define N_MAX 288               /* maximum number of codes in any set */
339
340
341 /* Macros for inflate() bit peeking and grabbing.
342    The usage is:
343
344         NEEDBITS(j)
345         x = b & mask_bits[j];
346         DUMPBITS(j)
347
348    where NEEDBITS makes sure that b has at least j bits in it, and
349    DUMPBITS removes the bits from b.  The macros use the variable k
350    for the number of bits in b.  Normally, b and k are register
351    variables for speed, and are initialized at the beginning of a
352    routine that uses these macros from a global bit buffer and count.
353
354    If we assume that EOB will be the longest code, then we will never
355    ask for bits with NEEDBITS that are beyond the end of the stream.
356    So, NEEDBITS should not read any more bytes than are needed to
357    meet the request.  Then no bytes need to be "returned" to the buffer
358    at the end of the last block.
359
360    However, this assumption is not true for fixed blocks--the EOB code
361    is 7 bits, but the other literal/length codes can be 8 or 9 bits.
362    (The EOB code is shorter than other codes because fixed blocks are
363    generally short.  So, while a block always has an EOB, many other
364    literal/length codes have a significantly lower probability of
365    showing up at all.)  However, by making the first table have a
366    lookup of seven bits, the EOB code will be found in that first
367    lookup, and so will not require that too many bits be pulled from
368    the stream.
369  */
370
371 static ush mask_bits[] =
372 {
373   0x0000,
374   0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
375   0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
376 };
377
378 #define NEEDBITS(n) do {while(k<(n)){b|=((ulg)get_byte(gzio))<<k;k+=8;}} while (0)
379 #define DUMPBITS(n) do {b>>=(n);k-=(n);} while (0)
380
381 static int
382 get_byte (grub_gzio_t gzio)
383 {
384   if (gzio->mem_input)
385     {
386       if (gzio->mem_input_off < gzio->mem_input_size)
387         return gzio->mem_input[gzio->mem_input_off++];
388       return 0;
389     }
390
391   if (gzio->file && (grub_file_tell (gzio->file)
392                      == (grub_off_t) gzio->data_offset
393                      || gzio->inbuf_d == INBUFSIZ))
394     {
395       gzio->inbuf_d = 0;
396       grub_file_read (gzio->file, gzio->inbuf, INBUFSIZ);
397     }
398
399   return gzio->inbuf[gzio->inbuf_d++];
400 }
401
402 static void
403 gzio_seek (grub_gzio_t gzio, grub_off_t off)
404 {
405   if (gzio->mem_input)
406     {
407       if (off > gzio->mem_input_size)
408         grub_error (GRUB_ERR_OUT_OF_RANGE,
409                     N_("attempt to seek outside of the file"));
410       else
411         gzio->mem_input_off = off;
412     }
413   else
414     grub_file_seek (gzio->file, off);
415 }
416
417 /* more function prototypes */
418 static int huft_build (unsigned *, unsigned, unsigned, ush *, ush *,
419                        struct huft **, int *);
420 static int huft_free (struct huft *);
421 static int inflate_codes_in_window (grub_gzio_t);
422
423
424 /* Given a list of code lengths and a maximum table size, make a set of
425    tables to decode that set of codes.  Return zero on success, one if
426    the given code set is incomplete (the tables are still built in this
427    case), two if the input is invalid (all zero length codes or an
428    oversubscribed set of lengths), and three if not enough memory. */
429
430 static int
431 huft_build (unsigned *b,        /* code lengths in bits (all assumed <= BMAX) */
432             unsigned n,         /* number of codes (assumed <= N_MAX) */
433             unsigned s,         /* number of simple-valued codes (0..s-1) */
434             ush * d,            /* list of base values for non-simple codes */
435             ush * e,            /* list of extra bits for non-simple codes */
436             struct huft **t,    /* result: starting table */
437             int *m)             /* maximum lookup bits, returns actual */
438 {
439   unsigned a;                   /* counter for codes of length k */
440   unsigned c[BMAX + 1];         /* bit length count table */
441   unsigned f;                   /* i repeats in table every f entries */
442   int g;                        /* maximum code length */
443   int h;                        /* table level */
444   register unsigned i;          /* counter, current code */
445   register unsigned j;          /* counter */
446   register int k;               /* number of bits in current code */
447   int l;                        /* bits per table (returned in m) */
448   register unsigned *p;         /* pointer into c[], b[], or v[] */
449   register struct huft *q;      /* points to current table */
450   struct huft r;                /* table entry for structure assignment */
451   struct huft *u[BMAX];         /* table stack */
452   unsigned v[N_MAX];            /* values in order of bit length */
453   register int w;               /* bits before this table == (l * h) */
454   unsigned x[BMAX + 1];         /* bit offsets, then code stack */
455   unsigned *xp;                 /* pointer into x */
456   int y;                        /* number of dummy codes added */
457   unsigned z;                   /* number of entries in current table */
458
459   /* Generate counts for each bit length */
460   grub_memset ((char *) c, 0, sizeof (c));
461   p = b;
462   i = n;
463   do
464     {
465       c[*p]++;                  /* assume all entries <= BMAX */
466       p++;                      /* Can't combine with above line (Solaris bug) */
467     }
468   while (--i);
469   if (c[0] == n)                /* null input--all zero length codes */
470     {
471       *t = (struct huft *) NULL;
472       *m = 0;
473       return 0;
474     }
475
476   /* Find minimum and maximum length, bound *m by those */
477   l = *m;
478   for (j = 1; j <= BMAX; j++)
479     if (c[j])
480       break;
481   k = j;                        /* minimum code length */
482   if ((unsigned) l < j)
483     l = j;
484   for (i = BMAX; i; i--)
485     if (c[i])
486       break;
487   g = i;                        /* maximum code length */
488   if ((unsigned) l > i)
489     l = i;
490   *m = l;
491
492   /* Adjust last length count to fill out codes, if needed */
493   for (y = 1 << j; j < i; j++, y <<= 1)
494     if ((y -= c[j]) < 0)
495       return 2;                 /* bad input: more codes than bits */
496   if ((y -= c[i]) < 0)
497     return 2;
498   c[i] += y;
499
500   /* Generate starting offsets into the value table for each length */
501   x[1] = j = 0;
502   p = c + 1;
503   xp = x + 2;
504   while (--i)
505     {                           /* note that i == g from above */
506       *xp++ = (j += *p++);
507     }
508
509   /* Make a table of values in order of bit lengths */
510   p = b;
511   i = 0;
512   do
513     {
514       if ((j = *p++) != 0)
515         v[x[j]++] = i;
516     }
517   while (++i < n);
518
519   /* Generate the Huffman codes and for each, make the table entries */
520   x[0] = i = 0;                 /* first Huffman code is zero */
521   p = v;                        /* grab values in bit order */
522   h = -1;                       /* no tables yet--level -1 */
523   w = -l;                       /* bits decoded == (l * h) */
524   u[0] = (struct huft *) NULL;  /* just to keep compilers happy */
525   q = (struct huft *) NULL;     /* ditto */
526   z = 0;                        /* ditto */
527
528   /* go through the bit lengths (k already is bits in shortest code) */
529   for (; k <= g; k++)
530     {
531       a = c[k];
532       while (a--)
533         {
534           /* here i is the Huffman code of length k bits for value *p */
535           /* make tables up to required level */
536           while (k > w + l)
537             {
538               h++;
539               w += l;           /* previous table always l bits */
540
541               /* compute minimum size table less than or equal to l bits */
542               z = (z = (unsigned) (g - w)) > (unsigned) l ? (unsigned) l : z;   /* upper limit on table size */
543               if ((f = 1 << (j = k - w)) > a + 1)       /* try a k-w bit table */
544                 {               /* too few codes for k-w bit table */
545                   f -= a + 1;   /* deduct codes from patterns left */
546                   xp = c + k;
547                   while (++j < z)       /* try smaller tables up to z bits */
548                     {
549                       if ((f <<= 1) <= *++xp)
550                         break;  /* enough codes to use up j bits */
551                       f -= *xp; /* else deduct codes from patterns */
552                     }
553                 }
554               z = 1 << j;       /* table entries for j-bit table */
555
556               /* allocate and link in new table */
557               q = (struct huft *) grub_zalloc ((z + 1) * sizeof (struct huft));
558               if (! q)
559                 {
560                   if (h)
561                     huft_free (u[0]);
562                   return 3;
563                 }
564
565               *t = q + 1;       /* link to list for huft_free() */
566               *(t = &(q->v.t)) = (struct huft *) NULL;
567               u[h] = ++q;       /* table starts after link */
568
569               /* connect to last table, if there is one */
570               if (h)
571                 {
572                   x[h] = i;     /* save pattern for backing up */
573                   r.b = (uch) l;        /* bits to dump before this table */
574                   r.e = (uch) (16 + j);         /* bits in this table */
575                   r.v.t = q;    /* pointer to this table */
576                   j = i >> (w - l);     /* (get around Turbo C bug) */
577                   u[h - 1][j] = r;      /* connect to last table */
578                 }
579             }
580
581           /* set up table entry in r */
582           r.b = (uch) (k - w);
583           if (p >= v + n)
584             r.e = 99;           /* out of values--invalid code */
585           else if (*p < s)
586             {
587               r.e = (uch) (*p < 256 ? 16 : 15);         /* 256 is end-of-block code */
588               r.v.n = (ush) (*p);       /* simple code is just the value */
589               p++;              /* one compiler does not like *p++ */
590             }
591           else
592             {
593               r.e = (uch) e[*p - s];    /* non-simple--look up in lists */
594               r.v.n = d[*p++ - s];
595             }
596
597           /* fill code-like entries with r */
598           f = 1 << (k - w);
599           for (j = i >> w; j < z; j += f)
600             q[j] = r;
601
602           /* backwards increment the k-bit code i */
603           for (j = 1 << (k - 1); i & j; j >>= 1)
604             i ^= j;
605           i ^= j;
606
607           /* backup over finished tables */
608           while ((i & ((1 << w) - 1)) != x[h])
609             {
610               h--;              /* don't need to update q */
611               w -= l;
612             }
613         }
614     }
615
616   /* Return true (1) if we were given an incomplete table */
617   return y != 0 && g != 1;
618 }
619
620
621 /* Free the malloc'ed tables built by huft_build(), which makes a linked
622    list of the tables it made, with the links in a dummy first entry of
623    each table.  */
624 static int
625 huft_free (struct huft *t)
626 {
627   register struct huft *p, *q;
628
629
630   /* Go through linked list, freeing from the malloced (t[-1]) address. */
631   p = t;
632   while (p != (struct huft *) NULL)
633     {
634       q = (--p)->v.t;
635       grub_free ((char *) p);
636       p = q;
637     }
638   return 0;
639 }
640
641
642 /*
643  *  inflate (decompress) the codes in a deflated (compressed) block.
644  *  Return an error code or zero if it all goes ok.
645  */
646
647 static int
648 inflate_codes_in_window (grub_gzio_t gzio)
649 {
650   register unsigned e;          /* table entry flag/number of extra bits */
651   unsigned n, d;                /* length and index for copy */
652   unsigned w;                   /* current window position */
653   struct huft *t;               /* pointer to table entry */
654   unsigned ml, md;              /* masks for bl and bd bits */
655   register ulg b;               /* bit buffer */
656   register unsigned k;          /* number of bits in bit buffer */
657
658   /* make local copies of globals */
659   d = gzio->inflate_d;
660   n = gzio->inflate_n;
661   b = gzio->bb;                 /* initialize bit buffer */
662   k = gzio->bk;
663   w = gzio->wp;                 /* initialize window position */
664
665   /* inflate the coded data */
666   ml = mask_bits[gzio->bl];             /* precompute masks for speed */
667   md = mask_bits[gzio->bd];
668   for (;;)                      /* do until end of block */
669     {
670       if (! gzio->code_state)
671         {
672           NEEDBITS ((unsigned) gzio->bl);
673           if ((e = (t = gzio->tl + ((unsigned) b & ml))->e) > 16)
674             do
675               {
676                 if (e == 99)
677                   {
678                     grub_error (GRUB_ERR_BAD_COMPRESSED_DATA,
679                                 "an unused code found");
680                     return 1;
681                   }
682                 DUMPBITS (t->b);
683                 e -= 16;
684                 NEEDBITS (e);
685               }
686             while ((e = (t = t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16);
687           DUMPBITS (t->b);
688
689           if (e == 16)          /* then it's a literal */
690             {
691               gzio->slide[w++] = (uch) t->v.n;
692               if (w == WSIZE)
693                 break;
694             }
695           else
696             /* it's an EOB or a length */
697             {
698               /* exit if end of block */
699               if (e == 15)
700                 {
701                   gzio->block_len = 0;
702                   break;
703                 }
704
705               /* get length of block to copy */
706               NEEDBITS (e);
707               n = t->v.n + ((unsigned) b & mask_bits[e]);
708               DUMPBITS (e);
709
710               /* decode distance of block to copy */
711               NEEDBITS ((unsigned) gzio->bd);
712               if ((e = (t = gzio->td + ((unsigned) b & md))->e) > 16)
713                 do
714                   {
715                     if (e == 99)
716                       {
717                         grub_error (GRUB_ERR_BAD_COMPRESSED_DATA,
718                                     "an unused code found");
719                         return 1;
720                       }
721                     DUMPBITS (t->b);
722                     e -= 16;
723                     NEEDBITS (e);
724                   }
725                 while ((e = (t = t->v.t + ((unsigned) b & mask_bits[e]))->e)
726                        > 16);
727               DUMPBITS (t->b);
728               NEEDBITS (e);
729               d = w - t->v.n - ((unsigned) b & mask_bits[e]);
730               DUMPBITS (e);
731               gzio->code_state++;
732             }
733         }
734
735       if (gzio->code_state)
736         {
737           /* do the copy */
738           do
739             {
740               n -= (e = (e = WSIZE - ((d &= WSIZE - 1) > w ? d : w)) > n ? n
741                     : e);
742
743               if (w - d >= e)
744                 {
745                   grub_memmove (gzio->slide + w, gzio->slide + d, e);
746                   w += e;
747                   d += e;
748                 }
749               else
750                 /* purposefully use the overlap for extra copies here!! */
751                 {
752                   while (e--)
753                     gzio->slide[w++] = gzio->slide[d++];
754                 }
755
756               if (w == WSIZE)
757                 break;
758             }
759           while (n);
760
761           if (! n)
762             gzio->code_state--;
763
764           /* did we break from the loop too soon? */
765           if (w == WSIZE)
766             break;
767         }
768     }
769
770   /* restore the globals from the locals */
771   gzio->inflate_d = d;
772   gzio->inflate_n = n;
773   gzio->wp = w;                 /* restore global window pointer */
774   gzio->bb = b;                 /* restore global bit buffer */
775   gzio->bk = k;
776
777   return ! gzio->block_len;
778 }
779
780
781 /* get header for an inflated type 0 (stored) block. */
782
783 static void
784 init_stored_block (grub_gzio_t gzio)
785 {
786   register ulg b;               /* bit buffer */
787   register unsigned k;          /* number of bits in bit buffer */
788
789   /* make local copies of globals */
790   b = gzio->bb;                 /* initialize bit buffer */
791   k = gzio->bk;
792
793   /* go to byte boundary */
794   DUMPBITS (k & 7);
795
796   /* get the length and its complement */
797   NEEDBITS (16);
798   gzio->block_len = ((unsigned) b & 0xffff);
799   DUMPBITS (16);
800   NEEDBITS (16);
801   if (gzio->block_len != (int) ((~b) & 0xffff))
802     grub_error (GRUB_ERR_BAD_COMPRESSED_DATA,
803                 "the length of a stored block does not match");
804   DUMPBITS (16);
805
806   /* restore global variables */
807   gzio->bb = b;
808   gzio->bk = k;
809 }
810
811
812 /* get header for an inflated type 1 (fixed Huffman codes) block.  We should
813    either replace this with a custom decoder, or at least precompute the
814    Huffman tables. */
815
816 static void
817 init_fixed_block (grub_gzio_t gzio)
818 {
819   int i;                        /* temporary variable */
820   unsigned l[288];              /* length list for huft_build */
821
822   /* set up literal table */
823   for (i = 0; i < 144; i++)
824     l[i] = 8;
825   for (; i < 256; i++)
826     l[i] = 9;
827   for (; i < 280; i++)
828     l[i] = 7;
829   for (; i < 288; i++)          /* make a complete, but wrong code set */
830     l[i] = 8;
831   gzio->bl = 7;
832   if (huft_build (l, 288, 257, cplens, cplext, &gzio->tl, &gzio->bl) != 0)
833     {
834       if (grub_errno == GRUB_ERR_NONE)
835         grub_error (GRUB_ERR_BAD_COMPRESSED_DATA,
836                     "failed in building a Huffman code table");
837       return;
838     }
839
840   /* set up distance table */
841   for (i = 0; i < 30; i++)      /* make an incomplete code set */
842     l[i] = 5;
843   gzio->bd = 5;
844   if (huft_build (l, 30, 0, cpdist, cpdext, &gzio->td, &gzio->bd) > 1)
845     {
846       if (grub_errno == GRUB_ERR_NONE)
847         grub_error (GRUB_ERR_BAD_COMPRESSED_DATA,
848                     "failed in building a Huffman code table");
849       huft_free (gzio->tl);
850       gzio->tl = 0;
851       return;
852     }
853
854   /* indicate we're now working on a block */
855   gzio->code_state = 0;
856   gzio->block_len++;
857 }
858
859
860 /* get header for an inflated type 2 (dynamic Huffman codes) block. */
861
862 static void
863 init_dynamic_block (grub_gzio_t gzio)
864 {
865   int i;                        /* temporary variables */
866   unsigned j;
867   unsigned l;                   /* last length */
868   unsigned m;                   /* mask for bit lengths table */
869   unsigned n;                   /* number of lengths to get */
870   unsigned nb;                  /* number of bit length codes */
871   unsigned nl;                  /* number of literal/length codes */
872   unsigned nd;                  /* number of distance codes */
873   unsigned ll[286 + 30];        /* literal/length and distance code lengths */
874   register ulg b;               /* bit buffer */
875   register unsigned k;          /* number of bits in bit buffer */
876
877   /* make local bit buffer */
878   b = gzio->bb;
879   k = gzio->bk;
880
881   /* read in table lengths */
882   NEEDBITS (5);
883   nl = 257 + ((unsigned) b & 0x1f);     /* number of literal/length codes */
884   DUMPBITS (5);
885   NEEDBITS (5);
886   nd = 1 + ((unsigned) b & 0x1f);       /* number of distance codes */
887   DUMPBITS (5);
888   NEEDBITS (4);
889   nb = 4 + ((unsigned) b & 0xf);        /* number of bit length codes */
890   DUMPBITS (4);
891   if (nl > 286 || nd > 30)
892     {
893       grub_error (GRUB_ERR_BAD_COMPRESSED_DATA, "too much data");
894       return;
895     }
896
897   /* read in bit-length-code lengths */
898   for (j = 0; j < nb; j++)
899     {
900       NEEDBITS (3);
901       ll[bitorder[j]] = (unsigned) b & 7;
902       DUMPBITS (3);
903     }
904   for (; j < 19; j++)
905     ll[bitorder[j]] = 0;
906
907   /* build decoding table for trees--single level, 7 bit lookup */
908   gzio->bl = 7;
909   if (huft_build (ll, 19, 19, NULL, NULL, &gzio->tl, &gzio->bl) != 0)
910     {
911       grub_error (GRUB_ERR_BAD_COMPRESSED_DATA,
912                   "failed in building a Huffman code table");
913       return;
914     }
915
916   /* read in literal and distance code lengths */
917   n = nl + nd;
918   m = mask_bits[gzio->bl];
919   i = l = 0;
920   while ((unsigned) i < n)
921     {
922       NEEDBITS ((unsigned) gzio->bl);
923       j = (gzio->td = gzio->tl + ((unsigned) b & m))->b;
924       DUMPBITS (j);
925       j = gzio->td->v.n;
926       if (j < 16)               /* length of code in bits (0..15) */
927         ll[i++] = l = j;        /* save last length in l */
928       else if (j == 16)         /* repeat last length 3 to 6 times */
929         {
930           NEEDBITS (2);
931           j = 3 + ((unsigned) b & 3);
932           DUMPBITS (2);
933           if ((unsigned) i + j > n)
934             {
935               grub_error (GRUB_ERR_BAD_COMPRESSED_DATA, "too many codes found");
936               return;
937             }
938           while (j--)
939             ll[i++] = l;
940         }
941       else if (j == 17)         /* 3 to 10 zero length codes */
942         {
943           NEEDBITS (3);
944           j = 3 + ((unsigned) b & 7);
945           DUMPBITS (3);
946           if ((unsigned) i + j > n)
947             {
948               grub_error (GRUB_ERR_BAD_COMPRESSED_DATA, "too many codes found");
949               return;
950             }
951           while (j--)
952             ll[i++] = 0;
953           l = 0;
954         }
955       else
956         /* j == 18: 11 to 138 zero length codes */
957         {
958           NEEDBITS (7);
959           j = 11 + ((unsigned) b & 0x7f);
960           DUMPBITS (7);
961           if ((unsigned) i + j > n)
962             {
963               grub_error (GRUB_ERR_BAD_COMPRESSED_DATA, "too many codes found");
964               return;
965             }
966           while (j--)
967             ll[i++] = 0;
968           l = 0;
969         }
970     }
971
972   /* free decoding table for trees */
973   huft_free (gzio->tl);
974   gzio->td = 0;
975   gzio->tl = 0;
976
977   /* restore the global bit buffer */
978   gzio->bb = b;
979   gzio->bk = k;
980
981   /* build the decoding tables for literal/length and distance codes */
982   gzio->bl = lbits;
983   if (huft_build (ll, nl, 257, cplens, cplext, &gzio->tl, &gzio->bl) != 0)
984     {
985       grub_error (GRUB_ERR_BAD_COMPRESSED_DATA,
986                   "failed in building a Huffman code table");
987       return;
988     }
989   gzio->bd = dbits;
990   if (huft_build (ll + nl, nd, 0, cpdist, cpdext, &gzio->td, &gzio->bd) != 0)
991     {
992       huft_free (gzio->tl);
993       gzio->tl = 0;
994       grub_error (GRUB_ERR_BAD_COMPRESSED_DATA,
995                   "failed in building a Huffman code table");
996       return;
997     }
998
999   /* indicate we're now working on a block */
1000   gzio->code_state = 0;
1001   gzio->block_len++;
1002 }
1003
1004
1005 static void
1006 get_new_block (grub_gzio_t gzio)
1007 {
1008   register ulg b;               /* bit buffer */
1009   register unsigned k;          /* number of bits in bit buffer */
1010
1011   /* make local bit buffer */
1012   b = gzio->bb;
1013   k = gzio->bk;
1014
1015   /* read in last block bit */
1016   NEEDBITS (1);
1017   gzio->last_block = (int) b & 1;
1018   DUMPBITS (1);
1019
1020   /* read in block type */
1021   NEEDBITS (2);
1022   gzio->block_type = (unsigned) b & 3;
1023   DUMPBITS (2);
1024
1025   /* restore the global bit buffer */
1026   gzio->bb = b;
1027   gzio->bk = k;
1028
1029   switch (gzio->block_type)
1030     {
1031     case INFLATE_STORED:
1032       init_stored_block (gzio);
1033       break;
1034     case INFLATE_FIXED:
1035       init_fixed_block (gzio);
1036       break;
1037     case INFLATE_DYNAMIC:
1038       init_dynamic_block (gzio);
1039       break;
1040     default:
1041       break;
1042     }
1043 }
1044
1045
1046 static void
1047 inflate_window (grub_gzio_t gzio)
1048 {
1049   /* initialize window */
1050   gzio->wp = 0;
1051
1052   /*
1053    *  Main decompression loop.
1054    */
1055
1056   while (gzio->wp < WSIZE && grub_errno == GRUB_ERR_NONE)
1057     {
1058       if (! gzio->block_len)
1059         {
1060           if (gzio->last_block)
1061             break;
1062
1063           get_new_block (gzio);
1064         }
1065
1066       if (gzio->block_type > INFLATE_DYNAMIC)
1067         grub_error (GRUB_ERR_BAD_COMPRESSED_DATA,
1068                     "unknown block type %d", gzio->block_type);
1069
1070       if (grub_errno != GRUB_ERR_NONE)
1071         return;
1072
1073       /*
1074        *  Expand stored block here.
1075        */
1076       if (gzio->block_type == INFLATE_STORED)
1077         {
1078           int w = gzio->wp;
1079
1080           /*
1081            *  This is basically a glorified pass-through
1082            */
1083
1084           while (gzio->block_len && w < WSIZE && grub_errno == GRUB_ERR_NONE)
1085             {
1086               gzio->slide[w++] = get_byte (gzio);
1087               gzio->block_len--;
1088             }
1089
1090           gzio->wp = w;
1091
1092           continue;
1093         }
1094
1095       /*
1096        *  Expand other kind of block.
1097        */
1098
1099       if (inflate_codes_in_window (gzio))
1100         {
1101           huft_free (gzio->tl);
1102           huft_free (gzio->td);
1103           gzio->tl = 0;
1104           gzio->td = 0;
1105         }
1106     }
1107
1108   gzio->saved_offset += gzio->wp;
1109
1110   if (gzio->hcontext)
1111     {
1112       gzio->hdesc->write (gzio->hcontext, gzio->slide, gzio->wp);
1113
1114       if (gzio->saved_offset == gzio->orig_len)
1115         {
1116           grub_uint32_t csum;
1117
1118           gzio->hdesc->final (gzio->hcontext);
1119           csum = *(grub_uint32_t *)gzio->hdesc->read (gzio->hcontext);
1120           csum = grub_be_to_cpu32 (csum);
1121           if (csum != gzio->orig_checksum)
1122             grub_error (GRUB_ERR_BAD_COMPRESSED_DATA,
1123                         "checksum mismatch %08x/%08x",
1124                         gzio->orig_checksum, csum);
1125         }
1126     }
1127 }
1128
1129
1130 static void
1131 initialize_tables (grub_gzio_t gzio)
1132 {
1133   gzio->saved_offset = 0;
1134   gzio_seek (gzio, gzio->data_offset);
1135
1136   /* Initialize the bit buffer.  */
1137   gzio->bk = 0;
1138   gzio->bb = 0;
1139
1140   /* Reset partial decompression code.  */
1141   gzio->last_block = 0;
1142   gzio->block_len = 0;
1143
1144   /* Reset memory allocation stuff.  */
1145   huft_free (gzio->tl);
1146   huft_free (gzio->td);
1147   gzio->tl = NULL;
1148   gzio->td = NULL;
1149
1150   if (gzio->hcontext)
1151     gzio->hdesc->init(gzio->hcontext);
1152 }
1153
1154
1155 /* Open a new decompressing object on the top of IO. If TRANSPARENT is true,
1156    even if IO does not contain data compressed by gzip, return a valid file
1157    object. Note that this function won't close IO, even if an error occurs.  */
1158 static grub_file_t
1159 grub_gzio_open (grub_file_t io, const char *name __attribute__ ((unused)))
1160 {
1161   grub_file_t file;
1162   grub_gzio_t gzio = 0;
1163
1164   file = (grub_file_t) grub_zalloc (sizeof (*file));
1165   if (! file)
1166     return 0;
1167
1168   gzio = grub_zalloc (sizeof (*gzio));
1169   if (! gzio)
1170     {
1171       grub_free (file);
1172       return 0;
1173     }
1174
1175   gzio->file = io;
1176
1177   gzio->hdesc = GRUB_MD_CRC32;
1178   gzio->hcontext = grub_malloc(gzio->hdesc->contextsize);
1179
1180   file->device = io->device;
1181   file->data = gzio;
1182   file->fs = &grub_gzio_fs;
1183   file->not_easily_seekable = 1;
1184
1185   if (! test_gzip_header (file))
1186     {
1187       grub_errno = GRUB_ERR_NONE;
1188       grub_free (gzio->hcontext);
1189       grub_free (gzio);
1190       grub_free (file);
1191       grub_file_seek (io, 0);
1192
1193       return io;
1194     }
1195
1196   return file;
1197 }
1198
1199 static grub_uint8_t
1200 mod_31 (grub_uint16_t v)
1201 {
1202   /* At most 2 iterations for any number that
1203      we can get here.
1204      In any case faster than real division.  */
1205   while (v > 0x1f)
1206     v = (v & 0x1f) + (v >> 5);
1207   if (v == 0x1f)
1208     return 0;
1209   return v;
1210 }
1211
1212 static int
1213 test_zlib_header (grub_gzio_t gzio)
1214 {
1215   grub_uint8_t cmf, flg;
1216   
1217   cmf = get_byte (gzio);
1218   flg = get_byte (gzio);
1219
1220   /* Check that compression method is DEFLATE.  */
1221   if ((cmf & 0xf) != GRUB_GZ_DEFLATED)
1222     {
1223       /* TRANSLATORS: It's about given file having some strange format, not
1224          complete lack of gzip support.  */
1225       grub_error (GRUB_ERR_BAD_COMPRESSED_DATA, N_("unsupported gzip format"));
1226       return 0;
1227     }
1228
1229   /* Usually it would be: (cmf * 256 + flg) % 31 != 0.  */
1230   /* But 256 == 8 (31).  */
1231   /* By multiplying by 4 and using 32 == 1 (31). We get our formula.  */
1232   if (mod_31 (cmf + flg * 4) != 0)
1233     {
1234       grub_error (GRUB_ERR_BAD_COMPRESSED_DATA, N_("unsupported gzip format"));
1235       return 0;
1236     }
1237
1238   /* Dictionary isn't supported.  */
1239   if (flg & 0x20)
1240     {
1241       grub_error (GRUB_ERR_BAD_COMPRESSED_DATA, N_("unsupported gzip format"));
1242       return 0;
1243     }
1244
1245   gzio->data_offset = 2;
1246   initialize_tables (gzio);
1247
1248   return 1;
1249 }
1250
1251 static grub_ssize_t
1252 grub_gzio_read_real (grub_gzio_t gzio, grub_off_t offset,
1253                      char *buf, grub_size_t len)
1254 {
1255   grub_ssize_t ret = 0;
1256
1257   /* Do we reset decompression to the beginning of the file?  */
1258   if (gzio->saved_offset > offset + WSIZE)
1259     initialize_tables (gzio);
1260
1261   /*
1262    *  This loop operates upon uncompressed data only.  The only
1263    *  special thing it does is to make sure the decompression
1264    *  window is within the range of data it needs.
1265    */
1266
1267   while (len > 0 && grub_errno == GRUB_ERR_NONE)
1268     {
1269       register grub_size_t size;
1270       register char *srcaddr;
1271
1272       while (offset >= gzio->saved_offset)
1273         {
1274           inflate_window (gzio);
1275           if (gzio->wp == 0)
1276             goto out;
1277         }
1278
1279       if (gzio->wp == 0)
1280         goto out;
1281
1282       srcaddr = (char *) ((offset & (WSIZE - 1)) + gzio->slide);
1283       size = gzio->saved_offset - offset;
1284       if (size > len)
1285         size = len;
1286
1287       grub_memmove (buf, srcaddr, size);
1288
1289       buf += size;
1290       len -= size;
1291       ret += size;
1292       offset += size;
1293     }
1294
1295  out:
1296   if (grub_errno != GRUB_ERR_NONE)
1297     ret = -1;
1298
1299   return ret;
1300 }
1301
1302 static grub_ssize_t
1303 grub_gzio_read (grub_file_t file, char *buf, grub_size_t len)
1304 {
1305   grub_ssize_t ret;
1306   ret = grub_gzio_read_real (file->data, file->offset, buf, len);
1307
1308   if (!grub_errno && ret != (grub_ssize_t) len)
1309     {
1310       grub_error (GRUB_ERR_BAD_COMPRESSED_DATA, "premature end of compressed");
1311       ret = -1;
1312     }
1313   return ret;
1314 }
1315
1316 /* Release everything, including the underlying file object.  */
1317 static grub_err_t
1318 grub_gzio_close (grub_file_t file)
1319 {
1320   grub_gzio_t gzio = file->data;
1321
1322   grub_file_close (gzio->file);
1323   huft_free (gzio->tl);
1324   huft_free (gzio->td);
1325   grub_free (gzio->hcontext);
1326   grub_free (gzio);
1327
1328   /* No need to close the same device twice.  */
1329   file->device = 0;
1330   file->name = 0;
1331
1332   return grub_errno;
1333 }
1334
1335 grub_ssize_t
1336 grub_zlib_decompress (char *inbuf, grub_size_t insize, grub_off_t off,
1337                       char *outbuf, grub_size_t outsize)
1338 {
1339   grub_gzio_t gzio = 0;
1340   grub_ssize_t ret;
1341
1342   gzio = grub_zalloc (sizeof (*gzio));
1343   if (! gzio)
1344     return -1;
1345   gzio->mem_input = (grub_uint8_t *) inbuf;
1346   gzio->mem_input_size = insize;
1347   gzio->mem_input_off = 0;
1348
1349   if (!test_zlib_header (gzio))
1350     {
1351       grub_free (gzio);
1352       return -1;
1353     }
1354
1355   ret = grub_gzio_read_real (gzio, off, outbuf, outsize);
1356   grub_free (gzio);
1357
1358   /* FIXME: Check Adler.  */
1359   return ret;
1360 }
1361
1362 grub_ssize_t
1363 grub_deflate_decompress (char *inbuf, grub_size_t insize, grub_off_t off,
1364                          char *outbuf, grub_size_t outsize)
1365 {
1366   grub_gzio_t gzio = 0;
1367   grub_ssize_t ret;
1368
1369   gzio = grub_zalloc (sizeof (*gzio));
1370   if (! gzio)
1371     return -1;
1372   gzio->mem_input = (grub_uint8_t *) inbuf;
1373   gzio->mem_input_size = insize;
1374   gzio->mem_input_off = 0;
1375
1376   initialize_tables (gzio);
1377
1378   ret = grub_gzio_read_real (gzio, off, outbuf, outsize);
1379   grub_free (gzio);
1380
1381   return ret;
1382 }
1383
1384 \f
1385
1386 static struct grub_fs grub_gzio_fs =
1387   {
1388     .name = "gzio",
1389     .dir = 0,
1390     .open = 0,
1391     .read = grub_gzio_read,
1392     .close = grub_gzio_close,
1393     .label = 0,
1394     .next = 0
1395   };
1396
1397 GRUB_MOD_INIT(gzio)
1398 {
1399   grub_file_filter_register (GRUB_FILE_FILTER_GZIO, grub_gzio_open);
1400 }
1401
1402 GRUB_MOD_FINI(gzio)
1403 {
1404   grub_file_filter_unregister (GRUB_FILE_FILTER_GZIO);
1405 }