2N/A/* gzio.c - decompression support for gzip */
2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 1999,2005,2006,2007,2009 Free Software Foundation, Inc.
2N/A * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
2N/A *
2N/A * GRUB is free software: you can redistribute it and/or modify
2N/A * it under the terms of the GNU General Public License as published by
2N/A * the Free Software Foundation, either version 3 of the License, or
2N/A * (at your option) any later version.
2N/A *
2N/A * GRUB is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU General Public License
2N/A * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
2N/A */
2N/A
2N/A/*
2N/A * Most of this file was originally the source file "inflate.c", written
2N/A * by Mark Adler. It has been very heavily modified. In particular, the
2N/A * original would run through the whole file at once, and this version can
2N/A * be stopped and restarted on any boundary during the decompression process.
2N/A *
2N/A * The license and header comments that file are included here.
2N/A */
2N/A
2N/A/* inflate.c -- Not copyrighted 1992 by Mark Adler
2N/A version c10p1, 10 January 1993 */
2N/A
2N/A/* You can do whatever you like with this source file, though I would
2N/A prefer that if you modify it and redistribute it that you include
2N/A comments to that effect with your name and the date. Thank you.
2N/A */
2N/A
2N/A#include <grub/err.h>
2N/A#include <grub/types.h>
2N/A#include <grub/mm.h>
2N/A#include <grub/misc.h>
2N/A#include <grub/fs.h>
2N/A#include <grub/file.h>
2N/A#include <grub/dl.h>
2N/A#include <grub/deflate.h>
2N/A
2N/AGRUB_MOD_LICENSE ("GPLv3+");
2N/A
2N/A/*
2N/A * Window Size
2N/A *
2N/A * This must be a power of two, and at least 32K for zip's deflate method
2N/A */
2N/A
2N/A#define WSIZE 0x8000
2N/A
2N/A
2N/A#define INBUFSIZ 0x2000
2N/A
2N/A/* The state stored in filesystem-specific data. */
2N/Astruct grub_gzio
2N/A{
2N/A /* The underlying file object. */
2N/A grub_file_t file;
2N/A /* If input is in memory following fields are used instead of file. */
2N/A grub_size_t mem_input_size, mem_input_off;
2N/A grub_uint8_t *mem_input;
2N/A /* The offset at which the data starts in the underlying file. */
2N/A grub_off_t data_offset;
2N/A /* The type of current block. */
2N/A int block_type;
2N/A /* The length of current block. */
2N/A int block_len;
2N/A /* The flag of the last block. */
2N/A int last_block;
2N/A /* The flag of codes. */
2N/A int code_state;
2N/A /* The length of a copy. */
2N/A unsigned inflate_n;
2N/A /* The index of a copy. */
2N/A unsigned inflate_d;
2N/A /* The input buffer. */
2N/A grub_uint8_t inbuf[INBUFSIZ];
2N/A int inbuf_d;
2N/A /* The bit buffer. */
2N/A unsigned long bb;
2N/A /* The bits in the bit buffer. */
2N/A unsigned bk;
2N/A /* The sliding window in uncompressed data. */
2N/A grub_uint8_t slide[WSIZE];
2N/A /* Current position in the slide. */
2N/A unsigned wp;
2N/A /* The literal/length code table. */
2N/A struct huft *tl;
2N/A /* The distance code table. */
2N/A struct huft *td;
2N/A /* The lookup bits for the literal/length code table. */
2N/A int bl;
2N/A /* The lookup bits for the distance code table. */
2N/A int bd;
2N/A /* The original offset value. */
2N/A grub_off_t saved_offset;
2N/A};
2N/Atypedef struct grub_gzio *grub_gzio_t;
2N/A
2N/A/* Declare the filesystem structure for grub_gzio_open. */
2N/Astatic struct grub_fs grub_gzio_fs;
2N/A
2N/A/* Function prototypes */
2N/Astatic void initialize_tables (grub_gzio_t);
2N/A
2N/A/* Eat variable-length header fields. */
2N/Astatic int
2N/Aeat_field (grub_file_t file, int len)
2N/A{
2N/A char ch = 1;
2N/A int not_retval = 1;
2N/A
2N/A do
2N/A {
2N/A if (len >= 0)
2N/A {
2N/A if (! (len--))
2N/A break;
2N/A }
2N/A else
2N/A {
2N/A if (! ch)
2N/A break;
2N/A }
2N/A }
2N/A while ((not_retval = grub_file_read (file, &ch, 1)) == 1);
2N/A
2N/A return ! not_retval;
2N/A}
2N/A
2N/A
2N/A/* Little-Endian defines for the 2-byte magic numbers for gzip files. */
2N/A#define GZIP_MAGIC grub_le_to_cpu16 (0x8B1F)
2N/A#define OLD_GZIP_MAGIC grub_le_to_cpu16 (0x9E1F)
2N/A
2N/A/* Compression methods (see algorithm.doc) */
2N/A#define STORED 0
2N/A#define COMPRESSED 1
2N/A#define PACKED 2
2N/A#define LZHED 3
2N/A/* methods 4 to 7 reserved */
2N/A#define DEFLATED 8
2N/A#define MAX_METHODS 9
2N/A
2N/A/* gzip flag byte */
2N/A#define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
2N/A#define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
2N/A#define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
2N/A#define ORIG_NAME 0x08 /* bit 3 set: original file name present */
2N/A#define COMMENT 0x10 /* bit 4 set: file comment present */
2N/A#define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
2N/A#define RESERVED 0xC0 /* bit 6,7: reserved */
2N/A
2N/A#define UNSUPPORTED_FLAGS (CONTINUATION | ENCRYPTED | RESERVED)
2N/A
2N/A/* inflate block codes */
2N/A#define INFLATE_STORED 0
2N/A#define INFLATE_FIXED 1
2N/A#define INFLATE_DYNAMIC 2
2N/A
2N/Atypedef unsigned char uch;
2N/Atypedef unsigned short ush;
2N/Atypedef unsigned long ulg;
2N/A
2N/Astatic int
2N/Atest_gzip_header (grub_file_t file)
2N/A{
2N/A struct {
2N/A grub_uint16_t magic;
2N/A grub_uint8_t method;
2N/A grub_uint8_t flags;
2N/A grub_uint32_t timestamp;
2N/A grub_uint8_t extra_flags;
2N/A grub_uint8_t os_type;
2N/A } hdr;
2N/A grub_uint16_t extra_len;
2N/A grub_uint32_t orig_len;
2N/A grub_gzio_t gzio = file->data;
2N/A
2N/A if (grub_file_tell (gzio->file) != 0)
2N/A grub_file_seek (gzio->file, 0);
2N/A
2N/A /*
2N/A * This checks if the file is gzipped. If a problem occurs here
2N/A * (other than a real error with the disk) then we don't think it
2N/A * is a compressed file, and simply mark it as such.
2N/A */
2N/A if (grub_file_read (gzio->file, &hdr, 10) != 10
2N/A || ((hdr.magic != GZIP_MAGIC)
2N/A && (hdr.magic != OLD_GZIP_MAGIC)))
2N/A {
2N/A grub_error (GRUB_ERR_BAD_FILE_TYPE, "no gzip magic found");
2N/A return 0;
2N/A }
2N/A
2N/A /*
2N/A * This does consistency checking on the header data. If a
2N/A * problem occurs from here on, then we have corrupt or otherwise
2N/A * bad data, and the error should be reported to the user.
2N/A */
2N/A if (hdr.method != DEFLATED
2N/A || (hdr.flags & UNSUPPORTED_FLAGS)
2N/A || ((hdr.flags & EXTRA_FIELD)
2N/A && (grub_file_read (gzio->file, &extra_len, 2) != 2
2N/A || eat_field (gzio->file,
2N/A grub_le_to_cpu16 (extra_len))))
2N/A || ((hdr.flags & ORIG_NAME) && eat_field (gzio->file, -1))
2N/A || ((hdr.flags & COMMENT) && eat_field (gzio->file, -1)))
2N/A {
2N/A grub_error (GRUB_ERR_BAD_COMPRESSED_DATA, "unsupported gzip format");
2N/A return 0;
2N/A }
2N/A
2N/A gzio->data_offset = grub_file_tell (gzio->file);
2N/A
2N/A if (gzio->file->not_easily_seekable == 0 ||
2N/A !grub_file_force_not_easily_seekable)
2N/A /* FIXME: don't do this on not easily seekable files. */
2N/A {
2N/A grub_file_seek (gzio->file, grub_file_size (gzio->file) - 4);
2N/A if (grub_file_read (gzio->file, &orig_len, 4) != 4)
2N/A {
2N/A grub_error (GRUB_ERR_BAD_FILE_TYPE, "unsupported gzip format");
2N/A return 0;
2N/A }
2N/A /* FIXME: this does not handle files whose original size is over 4GB.
2N/A But how can we know the real original size? */
2N/A grub_dprintf("gzio", "Original file size = %u\n", orig_len);
2N/A grub_dprintf("gzio", "backing file size = %u\n", (unsigned)gzio->file->size);
2N/A file->size = grub_le_to_cpu32 (orig_len);
2N/A } else {
2N/A /* File size is AT LEAST this amount */
2N/A file->size = grub_file_size (gzio->file);
2N/A grub_dprintf("gzio", "backing file size = %llu\n",
2N/A (unsigned long long)file->size);
2N/A file->is_size_approximate = 1;
2N/A }
2N/A
2N/A initialize_tables (gzio);
2N/A
2N/A return 1;
2N/A}
2N/A
2N/A
2N/A/* Huffman code lookup table entry--this entry is four bytes for machines
2N/A that have 16-bit pointers (e.g. PC's in the small or medium model).
2N/A Valid extra bits are 0..13. e == 15 is EOB (end of block), e == 16
2N/A means that v is a literal, 16 < e < 32 means that v is a pointer to
2N/A the next table, which codes e - 16 bits, and lastly e == 99 indicates
2N/A an unused code. If a code with e == 99 is looked up, this implies an
2N/A error in the data. */
2N/Astruct huft
2N/A{
2N/A uch e; /* number of extra bits or operation */
2N/A uch b; /* number of bits in this code or subcode */
2N/A union
2N/A {
2N/A ush n; /* literal, length base, or distance base */
2N/A struct huft *t; /* pointer to next level of table */
2N/A }
2N/A v;
2N/A};
2N/A
2N/A
2N/A/* The inflate algorithm uses a sliding 32K byte window on the uncompressed
2N/A stream to find repeated byte strings. This is implemented here as a
2N/A circular buffer. The index is updated simply by incrementing and then
2N/A and'ing with 0x7fff (32K-1). */
2N/A/* It is left to other modules to supply the 32K area. It is assumed
2N/A to be usable as if it were declared "uch slide[32768];" or as just
2N/A "uch *slide;" and then malloc'ed in the latter case. The definition
2N/A must be in unzip.h, included above. */
2N/A
2N/A
2N/A/* Tables for deflate from PKZIP's appnote.txt. */
2N/Astatic unsigned bitorder[] =
2N/A{ /* Order of the bit length code lengths */
2N/A 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
2N/Astatic ush cplens[] =
2N/A{ /* Copy lengths for literal codes 257..285 */
2N/A 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
2N/A 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
2N/A /* note: see note #13 above about the 258 in this list. */
2N/Astatic ush cplext[] =
2N/A{ /* Extra bits for literal codes 257..285 */
2N/A 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
2N/A 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99}; /* 99==invalid */
2N/Astatic ush cpdist[] =
2N/A{ /* Copy offsets for distance codes 0..29 */
2N/A 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
2N/A 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
2N/A 8193, 12289, 16385, 24577};
2N/Astatic ush cpdext[] =
2N/A{ /* Extra bits for distance codes */
2N/A 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
2N/A 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
2N/A 12, 12, 13, 13};
2N/A
2N/A
2N/A/*
2N/A Huffman code decoding is performed using a multi-level table lookup.
2N/A The fastest way to decode is to simply build a lookup table whose
2N/A size is determined by the longest code. However, the time it takes
2N/A to build this table can also be a factor if the data being decoded
2N/A is not very long. The most common codes are necessarily the
2N/A shortest codes, so those codes dominate the decoding time, and hence
2N/A the speed. The idea is you can have a shorter table that decodes the
2N/A shorter, more probable codes, and then point to subsidiary tables for
2N/A the longer codes. The time it costs to decode the longer codes is
2N/A then traded against the time it takes to make longer tables.
2N/A
2N/A This results of this trade are in the variables lbits and dbits
2N/A below. lbits is the number of bits the first level table for literal/
2N/A length codes can decode in one step, and dbits is the same thing for
2N/A the distance codes. Subsequent tables are also less than or equal to
2N/A those sizes. These values may be adjusted either when all of the
2N/A codes are shorter than that, in which case the longest code length in
2N/A bits is used, or when the shortest code is *longer* than the requested
2N/A table size, in which case the length of the shortest code in bits is
2N/A used.
2N/A
2N/A There are two different values for the two tables, since they code a
2N/A different number of possibilities each. The literal/length table
2N/A codes 286 possible values, or in a flat code, a little over eight
2N/A bits. The distance table codes 30 possible values, or a little less
2N/A than five bits, flat. The optimum values for speed end up being
2N/A about one bit more than those, so lbits is 8+1 and dbits is 5+1.
2N/A The optimum values may differ though from machine to machine, and
2N/A possibly even between compilers. Your mileage may vary.
2N/A */
2N/A
2N/A
2N/Astatic int lbits = 9; /* bits in base literal/length lookup table */
2N/Astatic int dbits = 6; /* bits in base distance lookup table */
2N/A
2N/A
2N/A/* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
2N/A#define BMAX 16 /* maximum bit length of any code (16 for explode) */
2N/A#define N_MAX 288 /* maximum number of codes in any set */
2N/A
2N/A
2N/A/* Macros for inflate() bit peeking and grabbing.
2N/A The usage is:
2N/A
2N/A NEEDBITS(j)
2N/A x = b & mask_bits[j];
2N/A DUMPBITS(j)
2N/A
2N/A where NEEDBITS makes sure that b has at least j bits in it, and
2N/A DUMPBITS removes the bits from b. The macros use the variable k
2N/A for the number of bits in b. Normally, b and k are register
2N/A variables for speed, and are initialized at the beginning of a
2N/A routine that uses these macros from a global bit buffer and count.
2N/A
2N/A If we assume that EOB will be the longest code, then we will never
2N/A ask for bits with NEEDBITS that are beyond the end of the stream.
2N/A So, NEEDBITS should not read any more bytes than are needed to
2N/A meet the request. Then no bytes need to be "returned" to the buffer
2N/A at the end of the last block.
2N/A
2N/A However, this assumption is not true for fixed blocks--the EOB code
2N/A is 7 bits, but the other literal/length codes can be 8 or 9 bits.
2N/A (The EOB code is shorter than other codes because fixed blocks are
2N/A generally short. So, while a block always has an EOB, many other
2N/A literal/length codes have a significantly lower probability of
2N/A showing up at all.) However, by making the first table have a
2N/A lookup of seven bits, the EOB code will be found in that first
2N/A lookup, and so will not require that too many bits be pulled from
2N/A the stream.
2N/A */
2N/A
2N/Astatic ush mask_bits[] =
2N/A{
2N/A 0x0000,
2N/A 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
2N/A 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
2N/A};
2N/A
2N/A#define NEEDBITS(n) do {while(k<(n)){b|=((ulg)get_byte(gzio))<<k;k+=8;}} while (0)
2N/A#define DUMPBITS(n) do {b>>=(n);k-=(n);} while (0)
2N/A
2N/Astatic int
2N/Aget_byte (grub_gzio_t gzio)
2N/A{
2N/A if (gzio->mem_input)
2N/A {
2N/A if (gzio->mem_input_off < gzio->mem_input_size)
2N/A return gzio->mem_input[gzio->mem_input_off++];
2N/A return 0;
2N/A }
2N/A
2N/A if (gzio->file && (grub_file_tell (gzio->file)
2N/A == (grub_off_t) gzio->data_offset
2N/A || gzio->inbuf_d == INBUFSIZ))
2N/A {
2N/A gzio->inbuf_d = 0;
2N/A grub_file_read (gzio->file, gzio->inbuf, INBUFSIZ);
2N/A }
2N/A
2N/A return gzio->inbuf[gzio->inbuf_d++];
2N/A}
2N/A
2N/Astatic void
2N/Agzio_seek (grub_gzio_t gzio, grub_off_t off)
2N/A{
2N/A if (gzio->mem_input)
2N/A {
2N/A if (off > gzio->mem_input_size)
2N/A grub_error (GRUB_ERR_OUT_OF_RANGE,
2N/A "attempt to seek outside of the file");
2N/A else
2N/A gzio->mem_input_off = off;
2N/A }
2N/A else
2N/A grub_file_seek (gzio->file, off);
2N/A}
2N/A
2N/A/* more function prototypes */
2N/Astatic int huft_build (unsigned *, unsigned, unsigned, ush *, ush *,
2N/A struct huft **, int *);
2N/Astatic int huft_free (struct huft *);
2N/Astatic int inflate_codes_in_window (grub_gzio_t);
2N/A
2N/A
2N/A/* Given a list of code lengths and a maximum table size, make a set of
2N/A tables to decode that set of codes. Return zero on success, one if
2N/A the given code set is incomplete (the tables are still built in this
2N/A case), two if the input is invalid (all zero length codes or an
2N/A oversubscribed set of lengths), and three if not enough memory. */
2N/A
2N/Astatic int
2N/Ahuft_build (unsigned *b, /* code lengths in bits (all assumed <= BMAX) */
2N/A unsigned n, /* number of codes (assumed <= N_MAX) */
2N/A unsigned s, /* number of simple-valued codes (0..s-1) */
2N/A ush * d, /* list of base values for non-simple codes */
2N/A ush * e, /* list of extra bits for non-simple codes */
2N/A struct huft **t, /* result: starting table */
2N/A int *m) /* maximum lookup bits, returns actual */
2N/A{
2N/A unsigned a; /* counter for codes of length k */
2N/A unsigned c[BMAX + 1]; /* bit length count table */
2N/A unsigned f; /* i repeats in table every f entries */
2N/A int g; /* maximum code length */
2N/A int h; /* table level */
2N/A register unsigned i; /* counter, current code */
2N/A register unsigned j; /* counter */
2N/A register int k; /* number of bits in current code */
2N/A int l; /* bits per table (returned in m) */
2N/A register unsigned *p; /* pointer into c[], b[], or v[] */
2N/A register struct huft *q; /* points to current table */
2N/A struct huft r; /* table entry for structure assignment */
2N/A struct huft *u[BMAX]; /* table stack */
2N/A unsigned v[N_MAX]; /* values in order of bit length */
2N/A register int w; /* bits before this table == (l * h) */
2N/A unsigned x[BMAX + 1]; /* bit offsets, then code stack */
2N/A unsigned *xp; /* pointer into x */
2N/A int y; /* number of dummy codes added */
2N/A unsigned z; /* number of entries in current table */
2N/A
2N/A /* Generate counts for each bit length */
2N/A grub_memset ((char *) c, 0, sizeof (c));
2N/A p = b;
2N/A i = n;
2N/A do
2N/A {
2N/A c[*p]++; /* assume all entries <= BMAX */
2N/A p++; /* Can't combine with above line (Solaris bug) */
2N/A }
2N/A while (--i);
2N/A if (c[0] == n) /* null input--all zero length codes */
2N/A {
2N/A *t = (struct huft *) NULL;
2N/A *m = 0;
2N/A return 0;
2N/A }
2N/A
2N/A /* Find minimum and maximum length, bound *m by those */
2N/A l = *m;
2N/A for (j = 1; j <= BMAX; j++)
2N/A if (c[j])
2N/A break;
2N/A k = j; /* minimum code length */
2N/A if ((unsigned) l < j)
2N/A l = j;
2N/A for (i = BMAX; i; i--)
2N/A if (c[i])
2N/A break;
2N/A g = i; /* maximum code length */
2N/A if ((unsigned) l > i)
2N/A l = i;
2N/A *m = l;
2N/A
2N/A /* Adjust last length count to fill out codes, if needed */
2N/A for (y = 1 << j; j < i; j++, y <<= 1)
2N/A if ((y -= c[j]) < 0)
2N/A return 2; /* bad input: more codes than bits */
2N/A if ((y -= c[i]) < 0)
2N/A return 2;
2N/A c[i] += y;
2N/A
2N/A /* Generate starting offsets into the value table for each length */
2N/A x[1] = j = 0;
2N/A p = c + 1;
2N/A xp = x + 2;
2N/A while (--i)
2N/A { /* note that i == g from above */
2N/A *xp++ = (j += *p++);
2N/A }
2N/A
2N/A /* Make a table of values in order of bit lengths */
2N/A p = b;
2N/A i = 0;
2N/A do
2N/A {
2N/A if ((j = *p++) != 0)
2N/A v[x[j]++] = i;
2N/A }
2N/A while (++i < n);
2N/A
2N/A /* Generate the Huffman codes and for each, make the table entries */
2N/A x[0] = i = 0; /* first Huffman code is zero */
2N/A p = v; /* grab values in bit order */
2N/A h = -1; /* no tables yet--level -1 */
2N/A w = -l; /* bits decoded == (l * h) */
2N/A u[0] = (struct huft *) NULL; /* just to keep compilers happy */
2N/A q = (struct huft *) NULL; /* ditto */
2N/A z = 0; /* ditto */
2N/A
2N/A /* go through the bit lengths (k already is bits in shortest code) */
2N/A for (; k <= g; k++)
2N/A {
2N/A a = c[k];
2N/A while (a--)
2N/A {
2N/A /* here i is the Huffman code of length k bits for value *p */
2N/A /* make tables up to required level */
2N/A while (k > w + l)
2N/A {
2N/A h++;
2N/A w += l; /* previous table always l bits */
2N/A
2N/A /* compute minimum size table less than or equal to l bits */
2N/A z = (z = (unsigned) (g - w)) > (unsigned) l ? (unsigned) l : z; /* upper limit on table size */
2N/A if ((f = 1 << (j = k - w)) > a + 1) /* try a k-w bit table */
2N/A { /* too few codes for k-w bit table */
2N/A f -= a + 1; /* deduct codes from patterns left */
2N/A xp = c + k;
2N/A while (++j < z) /* try smaller tables up to z bits */
2N/A {
2N/A if ((f <<= 1) <= *++xp)
2N/A break; /* enough codes to use up j bits */
2N/A f -= *xp; /* else deduct codes from patterns */
2N/A }
2N/A }
2N/A z = 1 << j; /* table entries for j-bit table */
2N/A
2N/A /* allocate and link in new table */
2N/A q = (struct huft *) grub_malloc ((z + 1) * sizeof (struct huft));
2N/A if (! q)
2N/A {
2N/A if (h)
2N/A huft_free (u[0]);
2N/A return 3;
2N/A }
2N/A
2N/A *t = q + 1; /* link to list for huft_free() */
2N/A *(t = &(q->v.t)) = (struct huft *) NULL;
2N/A u[h] = ++q; /* table starts after link */
2N/A
2N/A /* connect to last table, if there is one */
2N/A if (h)
2N/A {
2N/A x[h] = i; /* save pattern for backing up */
2N/A r.b = (uch) l; /* bits to dump before this table */
2N/A r.e = (uch) (16 + j); /* bits in this table */
2N/A r.v.t = q; /* pointer to this table */
2N/A j = i >> (w - l); /* (get around Turbo C bug) */
2N/A u[h - 1][j] = r; /* connect to last table */
2N/A }
2N/A }
2N/A
2N/A /* set up table entry in r */
2N/A r.b = (uch) (k - w);
2N/A if (p >= v + n)
2N/A r.e = 99; /* out of values--invalid code */
2N/A else if (*p < s)
2N/A {
2N/A r.e = (uch) (*p < 256 ? 16 : 15); /* 256 is end-of-block code */
2N/A r.v.n = (ush) (*p); /* simple code is just the value */
2N/A p++; /* one compiler does not like *p++ */
2N/A }
2N/A else
2N/A {
2N/A r.e = (uch) e[*p - s]; /* non-simple--look up in lists */
2N/A r.v.n = d[*p++ - s];
2N/A }
2N/A
2N/A /* fill code-like entries with r */
2N/A f = 1 << (k - w);
2N/A for (j = i >> w; j < z; j += f)
2N/A q[j] = r;
2N/A
2N/A /* backwards increment the k-bit code i */
2N/A for (j = 1 << (k - 1); i & j; j >>= 1)
2N/A i ^= j;
2N/A i ^= j;
2N/A
2N/A /* backup over finished tables */
2N/A while ((i & ((1 << w) - 1)) != x[h])
2N/A {
2N/A h--; /* don't need to update q */
2N/A w -= l;
2N/A }
2N/A }
2N/A }
2N/A
2N/A /* Return true (1) if we were given an incomplete table */
2N/A return y != 0 && g != 1;
2N/A}
2N/A
2N/A
2N/A/* Free the malloc'ed tables built by huft_build(), which makes a linked
2N/A list of the tables it made, with the links in a dummy first entry of
2N/A each table. */
2N/Astatic int
2N/Ahuft_free (struct huft *t)
2N/A{
2N/A register struct huft *p, *q;
2N/A
2N/A
2N/A /* Go through linked list, freeing from the malloced (t[-1]) address. */
2N/A p = t;
2N/A while (p != (struct huft *) NULL)
2N/A {
2N/A q = (--p)->v.t;
2N/A grub_free ((char *) p);
2N/A p = q;
2N/A }
2N/A return 0;
2N/A}
2N/A
2N/A
2N/A/*
2N/A * inflate (decompress) the codes in a deflated (compressed) block.
2N/A * Return an error code or zero if it all goes ok.
2N/A */
2N/A
2N/Astatic int
2N/Ainflate_codes_in_window (grub_gzio_t gzio)
2N/A{
2N/A register unsigned e; /* table entry flag/number of extra bits */
2N/A unsigned n, d; /* length and index for copy */
2N/A unsigned w; /* current window position */
2N/A struct huft *t; /* pointer to table entry */
2N/A unsigned ml, md; /* masks for bl and bd bits */
2N/A register ulg b; /* bit buffer */
2N/A register unsigned k; /* number of bits in bit buffer */
2N/A
2N/A /* make local copies of globals */
2N/A d = gzio->inflate_d;
2N/A n = gzio->inflate_n;
2N/A b = gzio->bb; /* initialize bit buffer */
2N/A k = gzio->bk;
2N/A w = gzio->wp; /* initialize window position */
2N/A
2N/A /* inflate the coded data */
2N/A ml = mask_bits[gzio->bl]; /* precompute masks for speed */
2N/A md = mask_bits[gzio->bd];
2N/A for (;;) /* do until end of block */
2N/A {
2N/A if (! gzio->code_state)
2N/A {
2N/A NEEDBITS ((unsigned) gzio->bl);
2N/A if ((e = (t = gzio->tl + ((unsigned) b & ml))->e) > 16)
2N/A do
2N/A {
2N/A if (e == 99)
2N/A {
2N/A grub_error (GRUB_ERR_BAD_COMPRESSED_DATA,
2N/A "an unused code found");
2N/A return 1;
2N/A }
2N/A DUMPBITS (t->b);
2N/A e -= 16;
2N/A NEEDBITS (e);
2N/A }
2N/A while ((e = (t = t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16);
2N/A DUMPBITS (t->b);
2N/A
2N/A if (e == 16) /* then it's a literal */
2N/A {
2N/A gzio->slide[w++] = (uch) t->v.n;
2N/A if (w == WSIZE)
2N/A break;
2N/A }
2N/A else
2N/A /* it's an EOB or a length */
2N/A {
2N/A /* exit if end of block */
2N/A if (e == 15)
2N/A {
2N/A gzio->block_len = 0;
2N/A break;
2N/A }
2N/A
2N/A /* get length of block to copy */
2N/A NEEDBITS (e);
2N/A n = t->v.n + ((unsigned) b & mask_bits[e]);
2N/A DUMPBITS (e);
2N/A
2N/A /* decode distance of block to copy */
2N/A NEEDBITS ((unsigned) gzio->bd);
2N/A if ((e = (t = gzio->td + ((unsigned) b & md))->e) > 16)
2N/A do
2N/A {
2N/A if (e == 99)
2N/A {
2N/A grub_error (GRUB_ERR_BAD_COMPRESSED_DATA,
2N/A "an unused code found");
2N/A return 1;
2N/A }
2N/A DUMPBITS (t->b);
2N/A e -= 16;
2N/A NEEDBITS (e);
2N/A }
2N/A while ((e = (t = t->v.t + ((unsigned) b & mask_bits[e]))->e)
2N/A > 16);
2N/A DUMPBITS (t->b);
2N/A NEEDBITS (e);
2N/A d = w - t->v.n - ((unsigned) b & mask_bits[e]);
2N/A DUMPBITS (e);
2N/A gzio->code_state++;
2N/A }
2N/A }
2N/A
2N/A if (gzio->code_state)
2N/A {
2N/A /* do the copy */
2N/A do
2N/A {
2N/A n -= (e = (e = WSIZE - ((d &= WSIZE - 1) > w ? d : w)) > n ? n
2N/A : e);
2N/A
2N/A if (w - d >= e)
2N/A {
2N/A grub_memmove (gzio->slide + w, gzio->slide + d, e);
2N/A w += e;
2N/A d += e;
2N/A }
2N/A else
2N/A /* purposefully use the overlap for extra copies here!! */
2N/A {
2N/A while (e--)
2N/A gzio->slide[w++] = gzio->slide[d++];
2N/A }
2N/A
2N/A if (w == WSIZE)
2N/A break;
2N/A }
2N/A while (n);
2N/A
2N/A if (! n)
2N/A gzio->code_state--;
2N/A
2N/A /* did we break from the loop too soon? */
2N/A if (w == WSIZE)
2N/A break;
2N/A }
2N/A }
2N/A
2N/A /* restore the globals from the locals */
2N/A gzio->inflate_d = d;
2N/A gzio->inflate_n = n;
2N/A gzio->wp = w; /* restore global window pointer */
2N/A gzio->bb = b; /* restore global bit buffer */
2N/A gzio->bk = k;
2N/A
2N/A return ! gzio->block_len;
2N/A}
2N/A
2N/A
2N/A/* get header for an inflated type 0 (stored) block. */
2N/A
2N/Astatic void
2N/Ainit_stored_block (grub_gzio_t gzio)
2N/A{
2N/A register ulg b; /* bit buffer */
2N/A register unsigned k; /* number of bits in bit buffer */
2N/A
2N/A /* make local copies of globals */
2N/A b = gzio->bb; /* initialize bit buffer */
2N/A k = gzio->bk;
2N/A
2N/A /* go to byte boundary */
2N/A DUMPBITS (k & 7);
2N/A
2N/A /* get the length and its complement */
2N/A NEEDBITS (16);
2N/A gzio->block_len = ((unsigned) b & 0xffff);
2N/A DUMPBITS (16);
2N/A NEEDBITS (16);
2N/A if (gzio->block_len != (int) ((~b) & 0xffff))
2N/A grub_error (GRUB_ERR_BAD_COMPRESSED_DATA,
2N/A "the length of a stored block does not match");
2N/A DUMPBITS (16);
2N/A
2N/A /* restore global variables */
2N/A gzio->bb = b;
2N/A gzio->bk = k;
2N/A}
2N/A
2N/A
2N/A/* get header for an inflated type 1 (fixed Huffman codes) block. We should
2N/A either replace this with a custom decoder, or at least precompute the
2N/A Huffman tables. */
2N/A
2N/Astatic void
2N/Ainit_fixed_block (grub_gzio_t gzio)
2N/A{
2N/A int i; /* temporary variable */
2N/A unsigned l[288]; /* length list for huft_build */
2N/A
2N/A /* set up literal table */
2N/A for (i = 0; i < 144; i++)
2N/A l[i] = 8;
2N/A for (; i < 256; i++)
2N/A l[i] = 9;
2N/A for (; i < 280; i++)
2N/A l[i] = 7;
2N/A for (; i < 288; i++) /* make a complete, but wrong code set */
2N/A l[i] = 8;
2N/A gzio->bl = 7;
2N/A if (huft_build (l, 288, 257, cplens, cplext, &gzio->tl, &gzio->bl) != 0)
2N/A {
2N/A if (grub_errno == GRUB_ERR_NONE)
2N/A grub_error (GRUB_ERR_BAD_COMPRESSED_DATA,
2N/A "failed in building a Huffman code table");
2N/A return;
2N/A }
2N/A
2N/A /* set up distance table */
2N/A for (i = 0; i < 30; i++) /* make an incomplete code set */
2N/A l[i] = 5;
2N/A gzio->bd = 5;
2N/A if (huft_build (l, 30, 0, cpdist, cpdext, &gzio->td, &gzio->bd) > 1)
2N/A {
2N/A if (grub_errno == GRUB_ERR_NONE)
2N/A grub_error (GRUB_ERR_BAD_COMPRESSED_DATA,
2N/A "failed in building a Huffman code table");
2N/A huft_free (gzio->tl);
2N/A gzio->tl = 0;
2N/A return;
2N/A }
2N/A
2N/A /* indicate we're now working on a block */
2N/A gzio->code_state = 0;
2N/A gzio->block_len++;
2N/A}
2N/A
2N/A
2N/A/* get header for an inflated type 2 (dynamic Huffman codes) block. */
2N/A
2N/Astatic void
2N/Ainit_dynamic_block (grub_gzio_t gzio)
2N/A{
2N/A int i; /* temporary variables */
2N/A unsigned j;
2N/A unsigned l; /* last length */
2N/A unsigned m; /* mask for bit lengths table */
2N/A unsigned n; /* number of lengths to get */
2N/A unsigned nb; /* number of bit length codes */
2N/A unsigned nl; /* number of literal/length codes */
2N/A unsigned nd; /* number of distance codes */
2N/A unsigned ll[286 + 30]; /* literal/length and distance code lengths */
2N/A register ulg b; /* bit buffer */
2N/A register unsigned k; /* number of bits in bit buffer */
2N/A
2N/A /* make local bit buffer */
2N/A b = gzio->bb;
2N/A k = gzio->bk;
2N/A
2N/A /* read in table lengths */
2N/A NEEDBITS (5);
2N/A nl = 257 + ((unsigned) b & 0x1f); /* number of literal/length codes */
2N/A DUMPBITS (5);
2N/A NEEDBITS (5);
2N/A nd = 1 + ((unsigned) b & 0x1f); /* number of distance codes */
2N/A DUMPBITS (5);
2N/A NEEDBITS (4);
2N/A nb = 4 + ((unsigned) b & 0xf); /* number of bit length codes */
2N/A DUMPBITS (4);
2N/A if (nl > 286 || nd > 30)
2N/A {
2N/A grub_error (GRUB_ERR_BAD_COMPRESSED_DATA, "too much data");
2N/A return;
2N/A }
2N/A
2N/A /* read in bit-length-code lengths */
2N/A for (j = 0; j < nb; j++)
2N/A {
2N/A NEEDBITS (3);
2N/A ll[bitorder[j]] = (unsigned) b & 7;
2N/A DUMPBITS (3);
2N/A }
2N/A for (; j < 19; j++)
2N/A ll[bitorder[j]] = 0;
2N/A
2N/A /* build decoding table for trees--single level, 7 bit lookup */
2N/A gzio->bl = 7;
2N/A if (huft_build (ll, 19, 19, NULL, NULL, &gzio->tl, &gzio->bl) != 0)
2N/A {
2N/A grub_error (GRUB_ERR_BAD_COMPRESSED_DATA,
2N/A "failed in building a Huffman code table");
2N/A return;
2N/A }
2N/A
2N/A /* read in literal and distance code lengths */
2N/A n = nl + nd;
2N/A m = mask_bits[gzio->bl];
2N/A i = l = 0;
2N/A while ((unsigned) i < n)
2N/A {
2N/A NEEDBITS ((unsigned) gzio->bl);
2N/A j = (gzio->td = gzio->tl + ((unsigned) b & m))->b;
2N/A DUMPBITS (j);
2N/A j = gzio->td->v.n;
2N/A if (j < 16) /* length of code in bits (0..15) */
2N/A ll[i++] = l = j; /* save last length in l */
2N/A else if (j == 16) /* repeat last length 3 to 6 times */
2N/A {
2N/A NEEDBITS (2);
2N/A j = 3 + ((unsigned) b & 3);
2N/A DUMPBITS (2);
2N/A if ((unsigned) i + j > n)
2N/A {
2N/A grub_error (GRUB_ERR_BAD_COMPRESSED_DATA, "too many codes found");
2N/A return;
2N/A }
2N/A while (j--)
2N/A ll[i++] = l;
2N/A }
2N/A else if (j == 17) /* 3 to 10 zero length codes */
2N/A {
2N/A NEEDBITS (3);
2N/A j = 3 + ((unsigned) b & 7);
2N/A DUMPBITS (3);
2N/A if ((unsigned) i + j > n)
2N/A {
2N/A grub_error (GRUB_ERR_BAD_COMPRESSED_DATA, "too many codes found");
2N/A return;
2N/A }
2N/A while (j--)
2N/A ll[i++] = 0;
2N/A l = 0;
2N/A }
2N/A else
2N/A /* j == 18: 11 to 138 zero length codes */
2N/A {
2N/A NEEDBITS (7);
2N/A j = 11 + ((unsigned) b & 0x7f);
2N/A DUMPBITS (7);
2N/A if ((unsigned) i + j > n)
2N/A {
2N/A grub_error (GRUB_ERR_BAD_COMPRESSED_DATA, "too many codes found");
2N/A return;
2N/A }
2N/A while (j--)
2N/A ll[i++] = 0;
2N/A l = 0;
2N/A }
2N/A }
2N/A
2N/A /* free decoding table for trees */
2N/A huft_free (gzio->tl);
2N/A gzio->td = 0;
2N/A gzio->tl = 0;
2N/A
2N/A /* restore the global bit buffer */
2N/A gzio->bb = b;
2N/A gzio->bk = k;
2N/A
2N/A /* build the decoding tables for literal/length and distance codes */
2N/A gzio->bl = lbits;
2N/A if (huft_build (ll, nl, 257, cplens, cplext, &gzio->tl, &gzio->bl) != 0)
2N/A {
2N/A grub_error (GRUB_ERR_BAD_COMPRESSED_DATA,
2N/A "failed in building a Huffman code table");
2N/A return;
2N/A }
2N/A gzio->bd = dbits;
2N/A if (huft_build (ll + nl, nd, 0, cpdist, cpdext, &gzio->td, &gzio->bd) != 0)
2N/A {
2N/A huft_free (gzio->tl);
2N/A gzio->tl = 0;
2N/A grub_error (GRUB_ERR_BAD_COMPRESSED_DATA,
2N/A "failed in building a Huffman code table");
2N/A return;
2N/A }
2N/A
2N/A /* indicate we're now working on a block */
2N/A gzio->code_state = 0;
2N/A gzio->block_len++;
2N/A}
2N/A
2N/A
2N/Astatic void
2N/Aget_new_block (grub_gzio_t gzio)
2N/A{
2N/A register ulg b; /* bit buffer */
2N/A register unsigned k; /* number of bits in bit buffer */
2N/A
2N/A /* make local bit buffer */
2N/A b = gzio->bb;
2N/A k = gzio->bk;
2N/A
2N/A /* read in last block bit */
2N/A NEEDBITS (1);
2N/A gzio->last_block = (int) b & 1;
2N/A DUMPBITS (1);
2N/A
2N/A /* read in block type */
2N/A NEEDBITS (2);
2N/A gzio->block_type = (unsigned) b & 3;
2N/A DUMPBITS (2);
2N/A
2N/A /* restore the global bit buffer */
2N/A gzio->bb = b;
2N/A gzio->bk = k;
2N/A
2N/A switch (gzio->block_type)
2N/A {
2N/A case INFLATE_STORED:
2N/A init_stored_block (gzio);
2N/A break;
2N/A case INFLATE_FIXED:
2N/A init_fixed_block (gzio);
2N/A break;
2N/A case INFLATE_DYNAMIC:
2N/A init_dynamic_block (gzio);
2N/A break;
2N/A default:
2N/A break;
2N/A }
2N/A}
2N/A
2N/A
2N/Astatic void
2N/Ainflate_window (grub_gzio_t gzio)
2N/A{
2N/A /* initialize window */
2N/A gzio->wp = 0;
2N/A
2N/A /*
2N/A * Main decompression loop.
2N/A */
2N/A
2N/A while (gzio->wp < WSIZE && grub_errno == GRUB_ERR_NONE)
2N/A {
2N/A if (! gzio->block_len)
2N/A {
2N/A if (gzio->last_block)
2N/A break;
2N/A
2N/A get_new_block (gzio);
2N/A }
2N/A
2N/A if (gzio->block_type > INFLATE_DYNAMIC)
2N/A grub_error (GRUB_ERR_BAD_COMPRESSED_DATA,
2N/A "unknown block type %d", gzio->block_type);
2N/A
2N/A if (grub_errno != GRUB_ERR_NONE)
2N/A return;
2N/A
2N/A /*
2N/A * Expand stored block here.
2N/A */
2N/A if (gzio->block_type == INFLATE_STORED)
2N/A {
2N/A int w = gzio->wp;
2N/A
2N/A /*
2N/A * This is basically a glorified pass-through
2N/A */
2N/A
2N/A while (gzio->block_len && w < WSIZE && grub_errno == GRUB_ERR_NONE)
2N/A {
2N/A gzio->slide[w++] = get_byte (gzio);
2N/A gzio->block_len--;
2N/A }
2N/A
2N/A gzio->wp = w;
2N/A
2N/A continue;
2N/A }
2N/A
2N/A /*
2N/A * Expand other kind of block.
2N/A */
2N/A
2N/A if (inflate_codes_in_window (gzio))
2N/A {
2N/A huft_free (gzio->tl);
2N/A huft_free (gzio->td);
2N/A gzio->tl = 0;
2N/A gzio->td = 0;
2N/A }
2N/A }
2N/A
2N/A gzio->saved_offset += WSIZE;
2N/A
2N/A /* XXX do CRC calculation here! */
2N/A}
2N/A
2N/A
2N/Astatic void
2N/Ainitialize_tables (grub_gzio_t gzio)
2N/A{
2N/A gzio->saved_offset = 0;
2N/A gzio_seek (gzio, gzio->data_offset);
2N/A
2N/A /* Initialize the bit buffer. */
2N/A gzio->bk = 0;
2N/A gzio->bb = 0;
2N/A
2N/A /* Reset partial decompression code. */
2N/A gzio->last_block = 0;
2N/A gzio->block_len = 0;
2N/A
2N/A /* Reset memory allocation stuff. */
2N/A huft_free (gzio->tl);
2N/A huft_free (gzio->td);
2N/A}
2N/A
2N/A
2N/A/* Open a new decompressing object on the top of IO. If TRANSPARENT is true,
2N/A even if IO does not contain data compressed by gzip, return a valid file
2N/A object. Note that this function won't close IO, even if an error occurs. */
2N/Astatic grub_file_t
2N/Agrub_gzio_open (grub_file_t io)
2N/A{
2N/A grub_file_t file;
2N/A grub_gzio_t gzio = 0;
2N/A
2N/A file = (grub_file_t) grub_zalloc (sizeof (*file));
2N/A if (! file)
2N/A return 0;
2N/A
2N/A gzio = grub_zalloc (sizeof (*gzio));
2N/A if (! gzio)
2N/A {
2N/A grub_free (file);
2N/A return 0;
2N/A }
2N/A
2N/A gzio->file = io;
2N/A
2N/A file->device = io->device;
2N/A file->offset = 0;
2N/A file->data = gzio;
2N/A file->read_hook = 0;
2N/A file->fs = &grub_gzio_fs;
2N/A file->not_easily_seekable = 1;
2N/A
2N/A if (! test_gzip_header (file))
2N/A {
2N/A grub_free (gzio);
2N/A grub_free (file);
2N/A grub_file_seek (io, 0);
2N/A grub_errno = GRUB_ERR_NONE;
2N/A
2N/A return io;
2N/A }
2N/A
2N/A return file;
2N/A}
2N/A
2N/Astatic int
2N/Atest_zlib_header (grub_gzio_t gzio)
2N/A{
2N/A grub_uint8_t cmf, flg;
2N/A
2N/A cmf = get_byte (gzio);
2N/A flg = get_byte (gzio);
2N/A
2N/A /* Check that compression method is DEFLATE. */
2N/A if ((cmf & 0xf) != DEFLATED)
2N/A {
2N/A grub_error (GRUB_ERR_BAD_COMPRESSED_DATA, "unsupported gzip format");
2N/A return 0;
2N/A }
2N/A
2N/A if ((cmf * 256 + flg) % 31)
2N/A {
2N/A grub_error (GRUB_ERR_BAD_COMPRESSED_DATA, "unsupported gzip format");
2N/A return 0;
2N/A }
2N/A
2N/A /* Dictionary isn't supported. */
2N/A if (flg & 0x20)
2N/A {
2N/A grub_error (GRUB_ERR_BAD_COMPRESSED_DATA, "unsupported gzip format");
2N/A return 0;
2N/A }
2N/A
2N/A gzio->data_offset = 2;
2N/A initialize_tables (gzio);
2N/A
2N/A return 1;
2N/A}
2N/A
2N/Astatic grub_ssize_t
2N/Agrub_gzio_read_real (grub_gzio_t gzio, grub_off_t offset,
2N/A char *buf, grub_size_t len)
2N/A{
2N/A grub_ssize_t ret = 0;
2N/A
2N/A /* Do we reset decompression to the beginning of the file? */
2N/A if (gzio->saved_offset > offset + WSIZE)
2N/A initialize_tables (gzio);
2N/A
2N/A /*
2N/A * This loop operates upon uncompressed data only. The only
2N/A * special thing it does is to make sure the decompression
2N/A * window is within the range of data it needs.
2N/A */
2N/A
2N/A while ((!gzio->last_block || gzio->block_len > 0) &&
2N/A len > 0 && grub_errno == GRUB_ERR_NONE)
2N/A {
2N/A register grub_size_t size;
2N/A register char *srcaddr;
2N/A
2N/A while (offset >= gzio->saved_offset)
2N/A inflate_window (gzio);
2N/A
2N/A srcaddr = (char *) ((offset & (WSIZE - 1)) + gzio->slide);
2N/A size = gzio->saved_offset - offset;
2N/A if (gzio->last_block && gzio->block_len == 0 && size > gzio->wp)
2N/A size = gzio->wp;
2N/A
2N/A if (size > len)
2N/A size = len;
2N/A
2N/A grub_memmove (buf, srcaddr, size);
2N/A
2N/A buf += size;
2N/A len -= size;
2N/A ret += size;
2N/A offset += size;
2N/A }
2N/A
2N/A if (grub_errno != GRUB_ERR_NONE)
2N/A ret = -1;
2N/A
2N/A return ret;
2N/A}
2N/A
2N/Astatic grub_ssize_t
2N/Agrub_gzio_read (grub_file_t file, char *buf, grub_size_t len)
2N/A{
2N/A return grub_gzio_read_real (file->data, file->offset, buf, len);
2N/A}
2N/A
2N/A/* Release everything, including the underlying file object. */
2N/Astatic grub_err_t
2N/Agrub_gzio_close (grub_file_t file)
2N/A{
2N/A grub_gzio_t gzio = file->data;
2N/A
2N/A grub_file_close (gzio->file);
2N/A huft_free (gzio->tl);
2N/A huft_free (gzio->td);
2N/A grub_free (gzio);
2N/A
2N/A /* No need to close the same device twice. */
2N/A file->device = 0;
2N/A
2N/A return grub_errno;
2N/A}
2N/A
2N/Agrub_ssize_t
2N/Agrub_zlib_decompress (char *inbuf, grub_size_t insize, grub_off_t off,
2N/A char *outbuf, grub_size_t outsize)
2N/A{
2N/A grub_gzio_t gzio = 0;
2N/A grub_ssize_t ret;
2N/A
2N/A gzio = grub_zalloc (sizeof (*gzio));
2N/A if (! gzio)
2N/A return -1;
2N/A gzio->mem_input = (grub_uint8_t *) inbuf;
2N/A gzio->mem_input_size = insize;
2N/A gzio->mem_input_off = 0;
2N/A
2N/A if (!test_zlib_header (gzio))
2N/A {
2N/A grub_free (gzio);
2N/A return -1;
2N/A }
2N/A
2N/A ret = grub_gzio_read_real (gzio, off, outbuf, outsize);
2N/A grub_free (gzio);
2N/A
2N/A /* FIXME: Check Adler. */
2N/A return ret;
2N/A}
2N/A
2N/A
2N/A
2N/Astatic struct grub_fs grub_gzio_fs =
2N/A {
2N/A .name = "gzio",
2N/A .dir = 0,
2N/A .open = 0,
2N/A .read = grub_gzio_read,
2N/A .close = grub_gzio_close,
2N/A .label = 0,
2N/A .next = 0
2N/A };
2N/A
2N/AGRUB_MOD_INIT(gzio)
2N/A{
2N/A grub_file_filter_register (GRUB_FILE_FILTER_GZIO, grub_gzio_open);
2N/A}
2N/A
2N/AGRUB_MOD_FINI(gzio)
2N/A{
2N/A grub_file_filter_unregister (GRUB_FILE_FILTER_GZIO);
2N/A}