1
/* gzio.c - decompression support for gzip */
3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 1999,2005 Free Software Foundation, Inc.
6
* This program 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 2 of the License, or
9
* (at your option) any later version.
11
* This program 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.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22
* Most of this file was originally the source file "inflate.c", written
23
* by Mark Adler. It has been very heavily modified. In particular, the
24
* original would run through the whole file at once, and this version can
25
* be stopped and restarted on any boundary during the decompression process.
27
* The license and header comments that file are included here.
30
/* inflate.c -- Not copyrighted 1992 by Mark Adler
31
version c10p1, 10 January 1993 */
33
/* You can do whatever you like with this source file, though I would
34
prefer that if you modify it and redistribute it that you include
35
comments to that effect with your name and the date. Thank you.
39
#include <grub/types.h>
41
#include <grub/misc.h>
43
#include <grub/file.h>
44
#include <grub/gzio.h>
49
* This must be a power of two, and at least 32K for zip's deflate method
55
#define INBUFSIZ 0x2000
57
/* The state stored in filesystem-specific data. */
60
/* The underlyding file object. */
62
/* The offset at which the data starts in the underlyding file. */
63
grub_ssize_t data_offset;
64
/* The type of current block. */
66
/* The length of current block. */
68
/* The flag of the last block. */
70
/* The flag of codes. */
72
/* The length of a copy. */
74
/* The index of a copy. */
76
/* The input buffer. */
77
grub_uint8_t inbuf[INBUFSIZ];
81
/* The bits in the bit buffer. */
83
/* Ths sliding window in uncompressed data. */
84
grub_uint8_t slide[WSIZE];
85
/* Current position in the slide. */
87
/* The literal/length code table. */
89
/* The distance code table. */
91
/* The lookup bits for the literal/length code table. */
93
/* The lookup bits for the distance code table. */
95
/* The original offset value. */
96
grub_ssize_t saved_offset;
98
typedef struct grub_gzio *grub_gzio_t;
100
/* Declare the filesystem structure for grub_gzio_open. */
101
static struct grub_fs grub_gzio_fs;
103
/* Function prototypes */
104
static void initialize_tables (grub_file_t file);
106
/* Eat variable-length header fields. */
108
eat_field (grub_file_t file, int len)
126
while ((not_retval = grub_file_read (file, &ch, 1)) == 1);
132
/* Little-Endian defines for the 2-byte magic numbers for gzip files. */
133
#define GZIP_MAGIC grub_le_to_cpu16 (0x8B1F)
134
#define OLD_GZIP_MAGIC grub_le_to_cpu16 (0x9E1F)
136
/* Compression methods (see algorithm.doc) */
141
/* methods 4 to 7 reserved */
143
#define MAX_METHODS 9
146
#define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
147
#define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
148
#define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
149
#define ORIG_NAME 0x08 /* bit 3 set: original file name present */
150
#define COMMENT 0x10 /* bit 4 set: file comment present */
151
#define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
152
#define RESERVED 0xC0 /* bit 6,7: reserved */
154
#define UNSUPPORTED_FLAGS (CONTINUATION | ENCRYPTED | RESERVED)
156
/* inflate block codes */
157
#define INFLATE_STORED 0
158
#define INFLATE_FIXED 1
159
#define INFLATE_DYNAMIC 2
161
typedef unsigned char uch;
162
typedef unsigned short ush;
163
typedef unsigned long ulg;
166
test_header (grub_file_t file)
168
unsigned char buf[10] __attribute__ ((aligned));
169
grub_gzio_t gzio = file->data;
171
if (grub_file_tell (gzio->file) != 0)
172
grub_file_seek (gzio->file, 0);
175
* This checks if the file is gzipped. If a problem occurs here
176
* (other than a real error with the disk) then we don't think it
177
* is a compressed file, and simply mark it as such.
179
if (grub_file_read (gzio->file, buf, 10) != 10
180
|| ((*((grub_uint16_t *) buf) != GZIP_MAGIC)
181
&& (*((grub_uint16_t *) buf) != OLD_GZIP_MAGIC)))
183
grub_error (GRUB_ERR_BAD_FILE_TYPE, "no gzip magic found");
188
* This does consistency checking on the header data. If a
189
* problem occurs from here on, then we have corrupt or otherwise
190
* bad data, and the error should be reported to the user.
192
if (buf[2] != DEFLATED
193
|| (buf[3] & UNSUPPORTED_FLAGS)
194
|| ((buf[3] & EXTRA_FIELD)
195
&& (grub_file_read (gzio->file, buf, 2) != 2
196
|| eat_field (gzio->file,
197
grub_le_to_cpu16 (*((grub_uint16_t *) buf)))))
198
|| ((buf[3] & ORIG_NAME) && eat_field (gzio->file, -1))
199
|| ((buf[3] & COMMENT) && eat_field (gzio->file, -1)))
201
grub_error (GRUB_ERR_BAD_GZIP_DATA, "unsupported gzip format");
205
gzio->data_offset = grub_file_tell (gzio->file);
207
grub_file_seek (gzio->file, grub_file_size (gzio->file) - 8);
209
if (grub_file_read (gzio->file, buf, 8) != 8)
211
grub_error (GRUB_ERR_BAD_FILE_TYPE, "unsupported gzip format");
215
file->size = grub_le_to_cpu32 (*((grub_uint32_t *) (buf + 4)));
217
initialize_tables (file);
223
/* Huffman code lookup table entry--this entry is four bytes for machines
224
that have 16-bit pointers (e.g. PC's in the small or medium model).
225
Valid extra bits are 0..13. e == 15 is EOB (end of block), e == 16
226
means that v is a literal, 16 < e < 32 means that v is a pointer to
227
the next table, which codes e - 16 bits, and lastly e == 99 indicates
228
an unused code. If a code with e == 99 is looked up, this implies an
229
error in the data. */
232
uch e; /* number of extra bits or operation */
233
uch b; /* number of bits in this code or subcode */
236
ush n; /* literal, length base, or distance base */
237
struct huft *t; /* pointer to next level of table */
243
/* The inflate algorithm uses a sliding 32K byte window on the uncompressed
244
stream to find repeated byte strings. This is implemented here as a
245
circular buffer. The index is updated simply by incrementing and then
246
and'ing with 0x7fff (32K-1). */
247
/* It is left to other modules to supply the 32K area. It is assumed
248
to be usable as if it were declared "uch slide[32768];" or as just
249
"uch *slide;" and then malloc'ed in the latter case. The definition
250
must be in unzip.h, included above. */
253
/* Tables for deflate from PKZIP's appnote.txt. */
254
static unsigned bitorder[] =
255
{ /* Order of the bit length code lengths */
256
16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
257
static ush cplens[] =
258
{ /* Copy lengths for literal codes 257..285 */
259
3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
260
35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
261
/* note: see note #13 above about the 258 in this list. */
262
static ush cplext[] =
263
{ /* Extra bits for literal codes 257..285 */
264
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
265
3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99}; /* 99==invalid */
266
static ush cpdist[] =
267
{ /* Copy offsets for distance codes 0..29 */
268
1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
269
257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
270
8193, 12289, 16385, 24577};
271
static ush cpdext[] =
272
{ /* Extra bits for distance codes */
273
0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
274
7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
279
Huffman code decoding is performed using a multi-level table lookup.
280
The fastest way to decode is to simply build a lookup table whose
281
size is determined by the longest code. However, the time it takes
282
to build this table can also be a factor if the data being decoded
283
is not very long. The most common codes are necessarily the
284
shortest codes, so those codes dominate the decoding time, and hence
285
the speed. The idea is you can have a shorter table that decodes the
286
shorter, more probable codes, and then point to subsidiary tables for
287
the longer codes. The time it costs to decode the longer codes is
288
then traded against the time it takes to make longer tables.
290
This results of this trade are in the variables lbits and dbits
291
below. lbits is the number of bits the first level table for literal/
292
length codes can decode in one step, and dbits is the same thing for
293
the distance codes. Subsequent tables are also less than or equal to
294
those sizes. These values may be adjusted either when all of the
295
codes are shorter than that, in which case the longest code length in
296
bits is used, or when the shortest code is *longer* than the requested
297
table size, in which case the length of the shortest code in bits is
300
There are two different values for the two tables, since they code a
301
different number of possibilities each. The literal/length table
302
codes 286 possible values, or in a flat code, a little over eight
303
bits. The distance table codes 30 possible values, or a little less
304
than five bits, flat. The optimum values for speed end up being
305
about one bit more than those, so lbits is 8+1 and dbits is 5+1.
306
The optimum values may differ though from machine to machine, and
307
possibly even between compilers. Your mileage may vary.
311
static int lbits = 9; /* bits in base literal/length lookup table */
312
static int dbits = 6; /* bits in base distance lookup table */
315
/* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
316
#define BMAX 16 /* maximum bit length of any code (16 for explode) */
317
#define N_MAX 288 /* maximum number of codes in any set */
320
/* Macros for inflate() bit peeking and grabbing.
324
x = b & mask_bits[j];
327
where NEEDBITS makes sure that b has at least j bits in it, and
328
DUMPBITS removes the bits from b. The macros use the variable k
329
for the number of bits in b. Normally, b and k are register
330
variables for speed, and are initialized at the beginning of a
331
routine that uses these macros from a global bit buffer and count.
333
If we assume that EOB will be the longest code, then we will never
334
ask for bits with NEEDBITS that are beyond the end of the stream.
335
So, NEEDBITS should not read any more bytes than are needed to
336
meet the request. Then no bytes need to be "returned" to the buffer
337
at the end of the last block.
339
However, this assumption is not true for fixed blocks--the EOB code
340
is 7 bits, but the other literal/length codes can be 8 or 9 bits.
341
(The EOB code is shorter than other codes because fixed blocks are
342
generally short. So, while a block always has an EOB, many other
343
literal/length codes have a significantly lower probability of
344
showing up at all.) However, by making the first table have a
345
lookup of seven bits, the EOB code will be found in that first
346
lookup, and so will not require that too many bits be pulled from
350
static ush mask_bits[] =
353
0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
354
0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
357
#define NEEDBITS(n) do {while(k<(n)){b|=((ulg)get_byte(file))<<k;k+=8;}} while (0)
358
#define DUMPBITS(n) do {b>>=(n);k-=(n);} while (0)
361
get_byte (grub_file_t file)
363
grub_gzio_t gzio = file->data;
365
if (grub_file_tell (gzio->file) == gzio->data_offset
366
|| gzio->inbuf_d == INBUFSIZ)
369
grub_file_read (gzio->file, gzio->inbuf, INBUFSIZ);
372
return gzio->inbuf[gzio->inbuf_d++];
375
/* more function prototypes */
376
static int huft_build (unsigned *, unsigned, unsigned, ush *, ush *,
377
struct huft **, int *);
378
static int huft_free (struct huft *);
379
static int inflate_codes_in_window (grub_file_t);
382
/* Given a list of code lengths and a maximum table size, make a set of
383
tables to decode that set of codes. Return zero on success, one if
384
the given code set is incomplete (the tables are still built in this
385
case), two if the input is invalid (all zero length codes or an
386
oversubscribed set of lengths), and three if not enough memory. */
389
huft_build (unsigned *b, /* code lengths in bits (all assumed <= BMAX) */
390
unsigned n, /* number of codes (assumed <= N_MAX) */
391
unsigned s, /* number of simple-valued codes (0..s-1) */
392
ush * d, /* list of base values for non-simple codes */
393
ush * e, /* list of extra bits for non-simple codes */
394
struct huft **t, /* result: starting table */
395
int *m) /* maximum lookup bits, returns actual */
397
unsigned a; /* counter for codes of length k */
398
unsigned c[BMAX + 1]; /* bit length count table */
399
unsigned f; /* i repeats in table every f entries */
400
int g; /* maximum code length */
401
int h; /* table level */
402
register unsigned i; /* counter, current code */
403
register unsigned j; /* counter */
404
register int k; /* number of bits in current code */
405
int l; /* bits per table (returned in m) */
406
register unsigned *p; /* pointer into c[], b[], or v[] */
407
register struct huft *q; /* points to current table */
408
struct huft r; /* table entry for structure assignment */
409
struct huft *u[BMAX]; /* table stack */
410
unsigned v[N_MAX]; /* values in order of bit length */
411
register int w; /* bits before this table == (l * h) */
412
unsigned x[BMAX + 1]; /* bit offsets, then code stack */
413
unsigned *xp; /* pointer into x */
414
int y; /* number of dummy codes added */
415
unsigned z; /* number of entries in current table */
417
/* Generate counts for each bit length */
418
grub_memset ((char *) c, 0, sizeof (c));
423
c[*p]++; /* assume all entries <= BMAX */
424
p++; /* Can't combine with above line (Solaris bug) */
427
if (c[0] == n) /* null input--all zero length codes */
429
*t = (struct huft *) NULL;
434
/* Find minimum and maximum length, bound *m by those */
436
for (j = 1; j <= BMAX; j++)
439
k = j; /* minimum code length */
440
if ((unsigned) l < j)
442
for (i = BMAX; i; i--)
445
g = i; /* maximum code length */
446
if ((unsigned) l > i)
450
/* Adjust last length count to fill out codes, if needed */
451
for (y = 1 << j; j < i; j++, y <<= 1)
453
return 2; /* bad input: more codes than bits */
458
/* Generate starting offsets into the value table for each length */
463
{ /* note that i == g from above */
467
/* Make a table of values in order of bit lengths */
477
/* Generate the Huffman codes and for each, make the table entries */
478
x[0] = i = 0; /* first Huffman code is zero */
479
p = v; /* grab values in bit order */
480
h = -1; /* no tables yet--level -1 */
481
w = -l; /* bits decoded == (l * h) */
482
u[0] = (struct huft *) NULL; /* just to keep compilers happy */
483
q = (struct huft *) NULL; /* ditto */
486
/* go through the bit lengths (k already is bits in shortest code) */
492
/* here i is the Huffman code of length k bits for value *p */
493
/* make tables up to required level */
497
w += l; /* previous table always l bits */
499
/* compute minimum size table less than or equal to l bits */
500
z = (z = (unsigned) (g - w)) > (unsigned) l ? (unsigned) l : z; /* upper limit on table size */
501
if ((f = 1 << (j = k - w)) > a + 1) /* try a k-w bit table */
502
{ /* too few codes for k-w bit table */
503
f -= a + 1; /* deduct codes from patterns left */
505
while (++j < z) /* try smaller tables up to z bits */
507
if ((f <<= 1) <= *++xp)
508
break; /* enough codes to use up j bits */
509
f -= *xp; /* else deduct codes from patterns */
512
z = 1 << j; /* table entries for j-bit table */
514
/* allocate and link in new table */
515
q = (struct huft *) grub_malloc ((z + 1) * sizeof (struct huft));
523
*t = q + 1; /* link to list for huft_free() */
524
*(t = &(q->v.t)) = (struct huft *) NULL;
525
u[h] = ++q; /* table starts after link */
527
/* connect to last table, if there is one */
530
x[h] = i; /* save pattern for backing up */
531
r.b = (uch) l; /* bits to dump before this table */
532
r.e = (uch) (16 + j); /* bits in this table */
533
r.v.t = q; /* pointer to this table */
534
j = i >> (w - l); /* (get around Turbo C bug) */
535
u[h - 1][j] = r; /* connect to last table */
539
/* set up table entry in r */
542
r.e = 99; /* out of values--invalid code */
545
r.e = (uch) (*p < 256 ? 16 : 15); /* 256 is end-of-block code */
546
r.v.n = (ush) (*p); /* simple code is just the value */
547
p++; /* one compiler does not like *p++ */
551
r.e = (uch) e[*p - s]; /* non-simple--look up in lists */
555
/* fill code-like entries with r */
557
for (j = i >> w; j < z; j += f)
560
/* backwards increment the k-bit code i */
561
for (j = 1 << (k - 1); i & j; j >>= 1)
565
/* backup over finished tables */
566
while ((i & ((1 << w) - 1)) != x[h])
568
h--; /* don't need to update q */
574
/* Return true (1) if we were given an incomplete table */
575
return y != 0 && g != 1;
579
/* Free the malloc'ed tables built by huft_build(), which makes a linked
580
list of the tables it made, with the links in a dummy first entry of
583
huft_free (struct huft *t)
585
register struct huft *p, *q;
588
/* Go through linked list, freeing from the malloced (t[-1]) address. */
590
while (p != (struct huft *) NULL)
593
grub_free ((char *) p);
601
* inflate (decompress) the codes in a deflated (compressed) block.
602
* Return an error code or zero if it all goes ok.
606
inflate_codes_in_window (grub_file_t file)
608
register unsigned e; /* table entry flag/number of extra bits */
609
unsigned n, d; /* length and index for copy */
610
unsigned w; /* current window position */
611
struct huft *t; /* pointer to table entry */
612
unsigned ml, md; /* masks for bl and bd bits */
613
register ulg b; /* bit buffer */
614
register unsigned k; /* number of bits in bit buffer */
615
grub_gzio_t gzio = file->data;
617
/* make local copies of globals */
620
b = gzio->bb; /* initialize bit buffer */
622
w = gzio->wp; /* initialize window position */
624
/* inflate the coded data */
625
ml = mask_bits[gzio->bl]; /* precompute masks for speed */
626
md = mask_bits[gzio->bd];
627
for (;;) /* do until end of block */
629
if (! gzio->code_state)
631
NEEDBITS ((unsigned) gzio->bl);
632
if ((e = (t = gzio->tl + ((unsigned) b & ml))->e) > 16)
637
grub_error (GRUB_ERR_BAD_GZIP_DATA,
638
"an unused code found");
645
while ((e = (t = t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16);
648
if (e == 16) /* then it's a literal */
650
gzio->slide[w++] = (uch) t->v.n;
655
/* it's an EOB or a length */
657
/* exit if end of block */
664
/* get length of block to copy */
666
n = t->v.n + ((unsigned) b & mask_bits[e]);
669
/* decode distance of block to copy */
670
NEEDBITS ((unsigned) gzio->bd);
671
if ((e = (t = gzio->td + ((unsigned) b & md))->e) > 16)
676
grub_error (GRUB_ERR_BAD_GZIP_DATA,
677
"an unused code found");
684
while ((e = (t = t->v.t + ((unsigned) b & mask_bits[e]))->e)
688
d = w - t->v.n - ((unsigned) b & mask_bits[e]);
694
if (gzio->code_state)
699
n -= (e = (e = WSIZE - ((d &= WSIZE - 1) > w ? d : w)) > n ? n
704
grub_memmove (gzio->slide + w, gzio->slide + d, e);
709
/* purposefully use the overlap for extra copies here!! */
712
gzio->slide[w++] = gzio->slide[d++];
723
/* did we break from the loop too soon? */
729
/* restore the globals from the locals */
732
gzio->wp = w; /* restore global window pointer */
733
gzio->bb = b; /* restore global bit buffer */
736
return ! gzio->block_len;
740
/* get header for an inflated type 0 (stored) block. */
743
init_stored_block (grub_file_t file)
745
register ulg b; /* bit buffer */
746
register unsigned k; /* number of bits in bit buffer */
747
grub_gzio_t gzio = file->data;
749
/* make local copies of globals */
750
b = gzio->bb; /* initialize bit buffer */
753
/* go to byte boundary */
756
/* get the length and its complement */
758
gzio->block_len = ((unsigned) b & 0xffff);
761
if (gzio->block_len != (int) ((~b) & 0xffff))
762
grub_error (GRUB_ERR_BAD_GZIP_DATA,
763
"the length of a stored block does not match");
766
/* restore global variables */
772
/* get header for an inflated type 1 (fixed Huffman codes) block. We should
773
either replace this with a custom decoder, or at least precompute the
777
init_fixed_block (grub_file_t file)
779
int i; /* temporary variable */
780
unsigned l[288]; /* length list for huft_build */
781
grub_gzio_t gzio = file->data;
783
/* set up literal table */
784
for (i = 0; i < 144; i++)
790
for (; i < 288; i++) /* make a complete, but wrong code set */
793
if (huft_build (l, 288, 257, cplens, cplext, &gzio->tl, &gzio->bl) != 0)
795
if (grub_errno == GRUB_ERR_NONE)
796
grub_error (GRUB_ERR_BAD_GZIP_DATA,
797
"failed in building a Huffman code table");
801
/* set up distance table */
802
for (i = 0; i < 30; i++) /* make an incomplete code set */
805
if (huft_build (l, 30, 0, cpdist, cpdext, &gzio->td, &gzio->bd) > 1)
807
if (grub_errno == GRUB_ERR_NONE)
808
grub_error (GRUB_ERR_BAD_GZIP_DATA,
809
"failed in building a Huffman code table");
810
huft_free (gzio->tl);
815
/* indicate we're now working on a block */
816
gzio->code_state = 0;
821
/* get header for an inflated type 2 (dynamic Huffman codes) block. */
824
init_dynamic_block (grub_file_t file)
826
int i; /* temporary variables */
828
unsigned l; /* last length */
829
unsigned m; /* mask for bit lengths table */
830
unsigned n; /* number of lengths to get */
831
unsigned nb; /* number of bit length codes */
832
unsigned nl; /* number of literal/length codes */
833
unsigned nd; /* number of distance codes */
834
unsigned ll[286 + 30]; /* literal/length and distance code lengths */
835
register ulg b; /* bit buffer */
836
register unsigned k; /* number of bits in bit buffer */
837
grub_gzio_t gzio = file->data;
839
/* make local bit buffer */
843
/* read in table lengths */
845
nl = 257 + ((unsigned) b & 0x1f); /* number of literal/length codes */
848
nd = 1 + ((unsigned) b & 0x1f); /* number of distance codes */
851
nb = 4 + ((unsigned) b & 0xf); /* number of bit length codes */
853
if (nl > 286 || nd > 30)
855
grub_error (GRUB_ERR_BAD_GZIP_DATA, "too much data");
859
/* read in bit-length-code lengths */
860
for (j = 0; j < nb; j++)
863
ll[bitorder[j]] = (unsigned) b & 7;
869
/* build decoding table for trees--single level, 7 bit lookup */
871
if (huft_build (ll, 19, 19, NULL, NULL, &gzio->tl, &gzio->bl) != 0)
873
grub_error (GRUB_ERR_BAD_GZIP_DATA,
874
"failed in building a Huffman code table");
878
/* read in literal and distance code lengths */
880
m = mask_bits[gzio->bl];
882
while ((unsigned) i < n)
884
NEEDBITS ((unsigned) gzio->bl);
885
j = (gzio->td = gzio->tl + ((unsigned) b & m))->b;
888
if (j < 16) /* length of code in bits (0..15) */
889
ll[i++] = l = j; /* save last length in l */
890
else if (j == 16) /* repeat last length 3 to 6 times */
893
j = 3 + ((unsigned) b & 3);
895
if ((unsigned) i + j > n)
897
grub_error (GRUB_ERR_BAD_GZIP_DATA, "too many codes found");
903
else if (j == 17) /* 3 to 10 zero length codes */
906
j = 3 + ((unsigned) b & 7);
908
if ((unsigned) i + j > n)
910
grub_error (GRUB_ERR_BAD_GZIP_DATA, "too many codes found");
918
/* j == 18: 11 to 138 zero length codes */
921
j = 11 + ((unsigned) b & 0x7f);
923
if ((unsigned) i + j > n)
925
grub_error (GRUB_ERR_BAD_GZIP_DATA, "too many codes found");
934
/* free decoding table for trees */
935
huft_free (gzio->tl);
939
/* restore the global bit buffer */
943
/* build the decoding tables for literal/length and distance codes */
945
if (huft_build (ll, nl, 257, cplens, cplext, &gzio->tl, &gzio->bl) != 0)
947
grub_error (GRUB_ERR_BAD_GZIP_DATA,
948
"failed in building a Huffman code table");
952
if (huft_build (ll + nl, nd, 0, cpdist, cpdext, &gzio->td, &gzio->bd) != 0)
954
huft_free (gzio->tl);
956
grub_error (GRUB_ERR_BAD_GZIP_DATA,
957
"failed in building a Huffman code table");
961
/* indicate we're now working on a block */
962
gzio->code_state = 0;
968
get_new_block (grub_file_t file)
970
register ulg b; /* bit buffer */
971
register unsigned k; /* number of bits in bit buffer */
972
grub_gzio_t gzio = file->data;
974
/* make local bit buffer */
978
/* read in last block bit */
980
gzio->last_block = (int) b & 1;
983
/* read in block type */
985
gzio->block_type = (unsigned) b & 3;
988
/* restore the global bit buffer */
992
switch (gzio->block_type)
995
init_stored_block (file);
998
init_fixed_block (file);
1000
case INFLATE_DYNAMIC:
1001
init_dynamic_block (file);
1010
inflate_window (grub_file_t file)
1012
grub_gzio_t gzio = file->data;
1014
/* initialize window */
1018
* Main decompression loop.
1021
while (gzio->wp < WSIZE && grub_errno == GRUB_ERR_NONE)
1023
if (! gzio->block_len)
1025
if (gzio->last_block)
1028
get_new_block (file);
1031
if (gzio->block_type > INFLATE_DYNAMIC)
1032
grub_error (GRUB_ERR_BAD_GZIP_DATA,
1033
"unknown block type %d", gzio->block_type);
1035
if (grub_errno != GRUB_ERR_NONE)
1039
* Expand stored block here.
1041
if (gzio->block_type == INFLATE_STORED)
1046
* This is basically a glorified pass-through
1049
while (gzio->block_len && w < WSIZE && grub_errno == GRUB_ERR_NONE)
1051
gzio->slide[w++] = get_byte (file);
1061
* Expand other kind of block.
1064
if (inflate_codes_in_window (file))
1066
huft_free (gzio->tl);
1067
huft_free (gzio->td);
1073
gzio->saved_offset += WSIZE;
1075
/* XXX do CRC calculation here! */
1080
initialize_tables (grub_file_t file)
1082
grub_gzio_t gzio = file->data;
1084
gzio->saved_offset = 0;
1085
grub_file_seek (gzio->file, gzio->data_offset);
1087
/* Initialize the bit buffer. */
1091
/* Reset partial decompression code. */
1092
gzio->last_block = 0;
1093
gzio->block_len = 0;
1095
/* Reset memory allocation stuff. */
1096
huft_free (gzio->tl);
1097
huft_free (gzio->td);
1101
/* Open a new decompressing object on the top of IO. If TRANSPARENT is true,
1102
even if IO does not contain data compressed by gzip, return a valid file
1103
object. Note that this function won't close IO, even if an error occurs. */
1105
grub_gzio_open (grub_file_t io, int transparent)
1108
grub_gzio_t gzio = 0;
1110
file = (grub_file_t) grub_malloc (sizeof (*file));
1114
gzio = grub_malloc (sizeof (*gzio));
1121
grub_memset (gzio, 0, sizeof (*gzio));
1124
file->device = io->device;
1127
file->read_hook = 0;
1128
file->fs = &grub_gzio_fs;
1130
if (! test_header (file))
1134
grub_file_seek (io, 0);
1136
if (grub_errno == GRUB_ERR_BAD_FILE_TYPE && transparent)
1138
grub_errno = GRUB_ERR_NONE;
1148
/* This is similar to grub_gzio_open, but takes a file name as an argument. */
1150
grub_gzfile_open (const char *name, int transparent)
1152
grub_file_t io, file;
1154
io = grub_file_open (name);
1158
file = grub_gzio_open (io, transparent);
1161
grub_file_close (io);
1169
grub_gzio_read (grub_file_t file, char *buf, grub_ssize_t len)
1171
grub_ssize_t ret = 0;
1172
grub_gzio_t gzio = file->data;
1173
grub_ssize_t offset;
1175
/* Do we reset decompression to the beginning of the file? */
1176
if (gzio->saved_offset > file->offset + WSIZE)
1177
initialize_tables (file);
1180
* This loop operates upon uncompressed data only. The only
1181
* special thing it does is to make sure the decompression
1182
* window is within the range of data it needs.
1185
offset = file->offset;
1187
while (len > 0 && grub_errno == GRUB_ERR_NONE)
1189
register grub_ssize_t size;
1190
register char *srcaddr;
1192
while (offset >= gzio->saved_offset)
1193
inflate_window (file);
1195
srcaddr = (char *) ((offset & (WSIZE - 1)) + gzio->slide);
1196
size = gzio->saved_offset - offset;
1200
grub_memmove (buf, srcaddr, size);
1208
if (grub_errno != GRUB_ERR_NONE)
1214
/* Release everything, including the underlying file object. */
1216
grub_gzio_close (grub_file_t file)
1218
grub_gzio_t gzio = file->data;
1220
grub_file_close (gzio->file);
1221
huft_free (gzio->tl);
1222
huft_free (gzio->td);
1225
/* No need to close the same device twice. */
1233
static struct grub_fs grub_gzio_fs =
1238
.read = grub_gzio_read,
1239
.close = grub_gzio_close,