ia64/xen-unstable

changeset 19076:9b0289a165eb

x86: Support booting a bzImage format domain 0 kernel.

This requires a bzImage v2.08 or later kernel.

xen/common/inflate.c is taken unmodified from Linux v2.6.28.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Thu Jan 22 18:00:48 2009 +0000 (2009-01-22)
parents fe2957a376fe
children ced1c3069ada
files xen/arch/x86/Makefile xen/arch/x86/bzimage.c xen/arch/x86/domain_build.c xen/arch/x86/setup.c xen/common/inflate.c xen/include/xen/sched.h
line diff
     1.1 --- a/xen/arch/x86/Makefile	Thu Jan 22 17:41:13 2009 +0000
     1.2 +++ b/xen/arch/x86/Makefile	Thu Jan 22 18:00:48 2009 +0000
     1.3 @@ -53,6 +53,7 @@ obj-y += machine_kexec.o
     1.4  obj-y += crash.o
     1.5  obj-y += tboot.o
     1.6  obj-y += hpet.o
     1.7 +obj-y += bzimage.o
     1.8  
     1.9  obj-$(crash_debug) += gdbstub.o
    1.10  
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/xen/arch/x86/bzimage.c	Thu Jan 22 18:00:48 2009 +0000
     2.3 @@ -0,0 +1,242 @@
     2.4 +#include <xen/cache.h>
     2.5 +#include <xen/errno.h>
     2.6 +#include <xen/lib.h>
     2.7 +#include <xen/mm.h>
     2.8 +#include <xen/string.h>
     2.9 +#include <xen/types.h>
    2.10 +
    2.11 +#define HEAPORDER 3
    2.12 +
    2.13 +static unsigned char *window;
    2.14 +#define memptr long
    2.15 +static memptr free_mem_ptr;
    2.16 +static memptr free_mem_end_ptr;
    2.17 +
    2.18 +#define WSIZE           0x80000000
    2.19 +
    2.20 +static unsigned char    *inbuf;
    2.21 +static unsigned         insize;
    2.22 +
    2.23 +/* Index of next byte to be processed in inbuf: */
    2.24 +static unsigned         inptr;
    2.25 +
    2.26 +/* Bytes in output buffer: */
    2.27 +static unsigned         outcnt;
    2.28 +
    2.29 +#define OF(args)        args
    2.30 +#define STATIC          static
    2.31 +
    2.32 +#define memzero(s, n)   memset((s), 0, (n))
    2.33 +
    2.34 +typedef unsigned char   uch;
    2.35 +typedef unsigned short  ush;
    2.36 +typedef unsigned long   ulg;
    2.37 +
    2.38 +#define INIT __init
    2.39 +
    2.40 +#define get_byte()      (inptr < insize ? inbuf[inptr++] : fill_inbuf())
    2.41 +
    2.42 +/* Diagnostic functions */
    2.43 +#ifdef DEBUG
    2.44 +#  define Assert(cond, msg) do { if (!(cond)) error(msg); } while (0)
    2.45 +#  define Trace(x)      do { fprintf x; } while (0)
    2.46 +#  define Tracev(x)     do { if (verbose) fprintf x ; } while (0)
    2.47 +#  define Tracevv(x)    do { if (verbose > 1) fprintf x ; } while (0)
    2.48 +#  define Tracec(c, x)  do { if (verbose && (c)) fprintf x ; } while (0)
    2.49 +#  define Tracecv(c, x) do { if (verbose > 1 && (c)) fprintf x ; } while (0)
    2.50 +#else
    2.51 +#  define Assert(cond, msg)
    2.52 +#  define Trace(x)
    2.53 +#  define Tracev(x)
    2.54 +#  define Tracevv(x)
    2.55 +#  define Tracec(c, x)
    2.56 +#  define Tracecv(c, x)
    2.57 +#endif
    2.58 +
    2.59 +static long bytes_out;
    2.60 +static void flush_window(void);
    2.61 +
    2.62 +static __init void error(char *x)
    2.63 +{
    2.64 +    printk("%s\n", x);
    2.65 +    BUG();
    2.66 +}
    2.67 +
    2.68 +static __init int fill_inbuf(void)
    2.69 +{
    2.70 +        error("ran out of input data");
    2.71 +        return 0;
    2.72 +}
    2.73 +
    2.74 +
    2.75 +#include "../../common/inflate.c"
    2.76 +
    2.77 +static __init void flush_window(void)
    2.78 +{
    2.79 +    /*
    2.80 +     * The window is equal to the output buffer therefore only need to
    2.81 +     * compute the crc.
    2.82 +     */
    2.83 +    unsigned long c = crc;
    2.84 +    unsigned n;
    2.85 +    unsigned char *in, ch;
    2.86 +
    2.87 +    in = window;
    2.88 +    for ( n = 0; n < outcnt; n++ )
    2.89 +    {
    2.90 +        ch = *in++;
    2.91 +        c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
    2.92 +    }
    2.93 +    crc = c;
    2.94 +
    2.95 +    bytes_out += (unsigned long)outcnt;
    2.96 +    outcnt = 0;
    2.97 +}
    2.98 +
    2.99 +static __init int gzip_length(char *image, unsigned long image_len)
   2.100 +{
   2.101 +    return *(uint32_t *)&image[image_len - 4];
   2.102 +}
   2.103 +
   2.104 +static  __init int perform_gunzip(char *output, char **_image_start, unsigned long *image_len)
   2.105 +{
   2.106 +    char *image = *_image_start;
   2.107 +    int rc;
   2.108 +    unsigned char magic0 = (unsigned char)image[0];
   2.109 +    unsigned char magic1 = (unsigned char)image[1];
   2.110 +
   2.111 +    if ( magic0 != 0x1f || ( (magic1 != 0x8b) && (magic1 != 0x9e) ) )
   2.112 +        return 0;
   2.113 +
   2.114 +    window = (unsigned char *)output;
   2.115 +
   2.116 +    free_mem_ptr = (unsigned long)alloc_xenheap_pages(HEAPORDER);
   2.117 +    free_mem_end_ptr = free_mem_ptr + (PAGE_SIZE << HEAPORDER);
   2.118 +
   2.119 +    inbuf = (unsigned char *)image;
   2.120 +    insize = *image_len;
   2.121 +    inptr = 0;
   2.122 +
   2.123 +    makecrc();
   2.124 +
   2.125 +    if ( gunzip() < 0 )
   2.126 +    {
   2.127 +        rc = -EINVAL;
   2.128 +    }
   2.129 +    else
   2.130 +    {
   2.131 +        *_image_start = (char *)window;
   2.132 +        *image_len = gzip_length(image, *image_len);
   2.133 +        rc = 0;
   2.134 +    }
   2.135 +
   2.136 +    free_xenheap_pages((void *)free_mem_ptr, HEAPORDER);
   2.137 +
   2.138 +    return rc;
   2.139 +}
   2.140 +
   2.141 +struct setup_header {
   2.142 +        uint8_t         _pad0[0x1f1];           /* skip uninteresting stuff */
   2.143 +        uint8_t         setup_sects;
   2.144 +        uint16_t        root_flags;
   2.145 +        uint32_t        syssize;
   2.146 +        uint16_t        ram_size;
   2.147 +        uint16_t        vid_mode;
   2.148 +        uint16_t        root_dev;
   2.149 +        uint16_t        boot_flag;
   2.150 +        uint16_t        jump;
   2.151 +        uint32_t        header;
   2.152 +#define HDR_MAGIC               "HdrS"
   2.153 +#define HDR_MAGIC_SZ    4
   2.154 +        uint16_t        version;
   2.155 +#define VERSION(h,l)    (((h)<<8) | (l))
   2.156 +        uint32_t        realmode_swtch;
   2.157 +        uint16_t        start_sys;
   2.158 +        uint16_t        kernel_version;
   2.159 +        uint8_t         type_of_loader;
   2.160 +        uint8_t         loadflags;
   2.161 +        uint16_t        setup_move_size;
   2.162 +        uint32_t        code32_start;
   2.163 +        uint32_t        ramdisk_image;
   2.164 +        uint32_t        ramdisk_size;
   2.165 +        uint32_t        bootsect_kludge;
   2.166 +        uint16_t        heap_end_ptr;
   2.167 +        uint16_t        _pad1;
   2.168 +        uint32_t        cmd_line_ptr;
   2.169 +        uint32_t        initrd_addr_max;
   2.170 +        uint32_t        kernel_alignment;
   2.171 +        uint8_t         relocatable_kernel;
   2.172 +        uint8_t         _pad2[3];
   2.173 +        uint32_t        cmdline_size;
   2.174 +        uint32_t        hardware_subarch;
   2.175 +        uint64_t        hardware_subarch_data;
   2.176 +        uint32_t        payload_offset;
   2.177 +        uint32_t        payload_length;
   2.178 +    } __attribute__((packed));
   2.179 +
   2.180 +static __init int bzimage_check(struct setup_header *hdr, unsigned long len)
   2.181 +{
   2.182 +    if ( len < sizeof(struct setup_header) )
   2.183 +        return 0;
   2.184 +
   2.185 +    if ( memcmp(&hdr->header, HDR_MAGIC, HDR_MAGIC_SZ) != 0 )
   2.186 +        return 0;
   2.187 +
   2.188 +    if ( hdr->version < VERSION(2,8) ) {
   2.189 +        printk("Cannot load bzImage v%d.%02d at least v2.08 is required\n",
   2.190 +           hdr->version >> 8, hdr->version & 0xff);
   2.191 +        return -EINVAL;
   2.192 +    }
   2.193 +    return 1;
   2.194 +}
   2.195 +
   2.196 +int __init bzimage_headroom(char *image_start, unsigned long image_length)
   2.197 +{
   2.198 +    struct setup_header *hdr = (struct setup_header *)image_start;
   2.199 +    char *img;
   2.200 +    int err, headroom;
   2.201 +
   2.202 +    err = bzimage_check(hdr, image_length);
   2.203 +    if (err < 1)
   2.204 +        return err;
   2.205 +
   2.206 +    img = image_start + (hdr->setup_sects+1) * 512;
   2.207 +    img += hdr->payload_offset;
   2.208 +
   2.209 +    headroom = gzip_length(img, hdr->payload_length);
   2.210 +    headroom += headroom >> 12; /* Add 8 bytes for every 32K input block */
   2.211 +    headroom += (32768 + 18); /* Add 32K + 18 bytes of extra headroom */
   2.212 +    headroom = (headroom + 4095) & ~4095;
   2.213 +
   2.214 +    return headroom;
   2.215 +}
   2.216 +
   2.217 +int __init bzimage_parse(char *image_base, char **image_start, unsigned long *image_len)
   2.218 +{
   2.219 +    struct setup_header *hdr = (struct setup_header *)(*image_start);
   2.220 +    int err = bzimage_check(hdr, *image_len);
   2.221 +
   2.222 +    if (err < 1)
   2.223 +        return err;
   2.224 +
   2.225 +    BUG_ON(!(image_base < *image_start));
   2.226 +
   2.227 +    *image_start += (hdr->setup_sects+1) * 512;
   2.228 +    *image_start += hdr->payload_offset;
   2.229 +    *image_len = hdr->payload_length;
   2.230 +
   2.231 +    if ( (err = perform_gunzip(image_base, image_start, image_len)) < 0 )
   2.232 +        return err;
   2.233 +
   2.234 +    return 0;
   2.235 +}
   2.236 +
   2.237 +/*
   2.238 + * Local variables:
   2.239 + * mode: C
   2.240 + * c-set-style: "BSD"
   2.241 + * c-basic-offset: 4
   2.242 + * tab-width: 4
   2.243 + * indent-tabs-mode: nil
   2.244 + * End:
   2.245 + */
     3.1 --- a/xen/arch/x86/domain_build.c	Thu Jan 22 17:41:13 2009 +0000
     3.2 +++ b/xen/arch/x86/domain_build.c	Thu Jan 22 18:00:48 2009 +0000
     3.3 @@ -32,6 +32,9 @@
     3.4  
     3.5  #include <public/version.h>
     3.6  
     3.7 +int __init bzimage_parse(
     3.8 +    char *output, char **image_start, unsigned long *image_len);
     3.9 +
    3.10  extern unsigned long initial_images_nrpages(void);
    3.11  extern void discard_initial_images(void);
    3.12  
    3.13 @@ -196,7 +199,8 @@ static void __init process_dom0_ioports_
    3.14  
    3.15  int __init construct_dom0(
    3.16      struct domain *d,
    3.17 -    unsigned long _image_start, unsigned long image_len, 
    3.18 +    unsigned long _image_base,
    3.19 +    unsigned long _image_start, unsigned long image_len,
    3.20      unsigned long _initrd_start, unsigned long initrd_len,
    3.21      char *cmdline)
    3.22  {
    3.23 @@ -213,9 +217,11 @@ int __init construct_dom0(
    3.24      struct vcpu *v = d->vcpu[0];
    3.25      unsigned long long value;
    3.26  #if defined(__i386__)
    3.27 +    char *image_base   = (char *)_image_base;   /* use lowmem mappings */
    3.28      char *image_start  = (char *)_image_start;  /* use lowmem mappings */
    3.29      char *initrd_start = (char *)_initrd_start; /* use lowmem mappings */
    3.30  #elif defined(__x86_64__)
    3.31 +    char *image_base   = __va(_image_base);
    3.32      char *image_start  = __va(_image_start);
    3.33      char *initrd_start = __va(_initrd_start);
    3.34  #endif
    3.35 @@ -262,6 +268,9 @@ int __init construct_dom0(
    3.36  
    3.37      nr_pages = compute_dom0_nr_pages();
    3.38  
    3.39 +    if ( (rc = bzimage_parse(image_base, &image_start, &image_len)) != 0 )
    3.40 +        return rc;
    3.41 +
    3.42      if ( (rc = elf_init(&elf, image_start, image_len)) != 0 )
    3.43          return rc;
    3.44  #ifdef VERBOSE
     4.1 --- a/xen/arch/x86/setup.c	Thu Jan 22 17:41:13 2009 +0000
     4.2 +++ b/xen/arch/x86/setup.c	Thu Jan 22 18:00:48 2009 +0000
     4.3 @@ -39,6 +39,8 @@
     4.4  #include <xsm/xsm.h>
     4.5  #include <asm/tboot.h>
     4.6  
     4.7 +int __init bzimage_headroom(char *image_start, unsigned long image_length);
     4.8 +
     4.9  #if defined(CONFIG_X86_64)
    4.10  #define BOOTSTRAP_DIRECTMAP_END (1UL << 32) /* 4GB */
    4.11  #define maddr_to_bootstrap_virt(m) maddr_to_virt(m)
    4.12 @@ -171,19 +173,21 @@ static void __init do_initcalls(void)
    4.13      for ( ; ; ) halt();                         \
    4.14  } while (0)
    4.15  
    4.16 -static unsigned long __initdata initial_images_start, initial_images_end;
    4.17 +static unsigned long __initdata initial_images_base;
    4.18 +static unsigned long __initdata initial_images_start;
    4.19 +static unsigned long __initdata initial_images_end;
    4.20  
    4.21  unsigned long __init initial_images_nrpages(void)
    4.22  {
    4.23 -    ASSERT(!(initial_images_start & ~PAGE_MASK));
    4.24 +    ASSERT(!(initial_images_base & ~PAGE_MASK));
    4.25      ASSERT(!(initial_images_end   & ~PAGE_MASK));
    4.26      return ((initial_images_end >> PAGE_SHIFT) -
    4.27 -            (initial_images_start >> PAGE_SHIFT));
    4.28 +            (initial_images_base >> PAGE_SHIFT));
    4.29  }
    4.30  
    4.31  void __init discard_initial_images(void)
    4.32  {
    4.33 -    init_domheap_pages(initial_images_start, initial_images_end);
    4.34 +    init_domheap_pages(initial_images_base, initial_images_end);
    4.35  }
    4.36  
    4.37  extern char __per_cpu_start[], __per_cpu_data_end[], __per_cpu_end[];
    4.38 @@ -413,7 +417,7 @@ void __init __start_xen(unsigned long mb
    4.39      unsigned int initrdidx = 1;
    4.40      multiboot_info_t *mbi = __va(mbi_p);
    4.41      module_t *mod = (module_t *)__va(mbi->mods_addr);
    4.42 -    unsigned long nr_pages, modules_length;
    4.43 +    unsigned long nr_pages, modules_length, modules_headroom;
    4.44      unsigned long allocator_bitmap_end;
    4.45      int i, e820_warn = 0, bytes = 0;
    4.46      struct ns16550_defaults ns16550 = {
    4.47 @@ -613,6 +617,10 @@ void __init __start_xen(unsigned long mb
    4.48       * x86/64, we relocate Xen to higher memory.
    4.49       */
    4.50      modules_length = mod[mbi->mods_count-1].mod_end - mod[0].mod_start;
    4.51 +    modules_headroom = bzimage_headroom(
    4.52 +        (char *)(unsigned long)mod[0].mod_start,
    4.53 +        (unsigned long)(mod[0].mod_end - mod[0].mod_start));
    4.54 +
    4.55      for ( i = boot_e820.nr_map-1; i >= 0; i-- )
    4.56      {
    4.57          uint64_t s, e, mask = (1UL << L2_PAGETABLE_SHIFT) - 1;
    4.58 @@ -717,12 +725,15 @@ void __init __start_xen(unsigned long mb
    4.59  #endif
    4.60  
    4.61          /* Is the region suitable for relocating the multiboot modules? */
    4.62 -        if ( !initial_images_start && (s < e) && ((e-s) >= modules_length) )
    4.63 +        if ( !initial_images_start && (s < e) &&
    4.64 +             ((e-s) >= (modules_length+modules_headroom)) )
    4.65          {
    4.66              initial_images_end = e;
    4.67              e = (e - modules_length) & PAGE_MASK;
    4.68              initial_images_start = e;
    4.69 -            move_memory(initial_images_start, 
    4.70 +            e -= modules_headroom;
    4.71 +            initial_images_base = e;
    4.72 +            move_memory(initial_images_start,
    4.73                          mod[0].mod_start, mod[mbi->mods_count-1].mod_end);
    4.74          }
    4.75  
    4.76 @@ -736,7 +747,7 @@ void __init __start_xen(unsigned long mb
    4.77  
    4.78      if ( !initial_images_start )
    4.79          EARLY_FAIL("Not enough memory to relocate the dom0 kernel image.\n");
    4.80 -    reserve_e820_ram(&boot_e820, initial_images_start, initial_images_end);
    4.81 +    reserve_e820_ram(&boot_e820, initial_images_base, initial_images_end);
    4.82  
    4.83      /* Initialise boot heap. */
    4.84      allocator_bitmap_end = init_boot_allocator(__pa(&_end));
    4.85 @@ -1027,7 +1038,8 @@ void __init __start_xen(unsigned long mb
    4.86       * above our heap. The second module, if present, is an initrd ramdisk.
    4.87       */
    4.88      if ( construct_dom0(dom0,
    4.89 -                        initial_images_start, 
    4.90 +                        initial_images_base,
    4.91 +                        initial_images_start,
    4.92                          mod[0].mod_end-mod[0].mod_start,
    4.93                          _initrd_start,
    4.94                          _initrd_len,
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/xen/common/inflate.c	Thu Jan 22 18:00:48 2009 +0000
     5.3 @@ -0,0 +1,1303 @@
     5.4 +#define DEBG(x)
     5.5 +#define DEBG1(x)
     5.6 +/* inflate.c -- Not copyrighted 1992 by Mark Adler
     5.7 +   version c10p1, 10 January 1993 */
     5.8 +
     5.9 +/* 
    5.10 + * Adapted for booting Linux by Hannu Savolainen 1993
    5.11 + * based on gzip-1.0.3 
    5.12 + *
    5.13 + * Nicolas Pitre <nico@cam.org>, 1999/04/14 :
    5.14 + *   Little mods for all variable to reside either into rodata or bss segments
    5.15 + *   by marking constant variables with 'const' and initializing all the others
    5.16 + *   at run-time only.  This allows for the kernel uncompressor to run
    5.17 + *   directly from Flash or ROM memory on embedded systems.
    5.18 + */
    5.19 +
    5.20 +/*
    5.21 +   Inflate deflated (PKZIP's method 8 compressed) data.  The compression
    5.22 +   method searches for as much of the current string of bytes (up to a
    5.23 +   length of 258) in the previous 32 K bytes.  If it doesn't find any
    5.24 +   matches (of at least length 3), it codes the next byte.  Otherwise, it
    5.25 +   codes the length of the matched string and its distance backwards from
    5.26 +   the current position.  There is a single Huffman code that codes both
    5.27 +   single bytes (called "literals") and match lengths.  A second Huffman
    5.28 +   code codes the distance information, which follows a length code.  Each
    5.29 +   length or distance code actually represents a base value and a number
    5.30 +   of "extra" (sometimes zero) bits to get to add to the base value.  At
    5.31 +   the end of each deflated block is a special end-of-block (EOB) literal/
    5.32 +   length code.  The decoding process is basically: get a literal/length
    5.33 +   code; if EOB then done; if a literal, emit the decoded byte; if a
    5.34 +   length then get the distance and emit the referred-to bytes from the
    5.35 +   sliding window of previously emitted data.
    5.36 +
    5.37 +   There are (currently) three kinds of inflate blocks: stored, fixed, and
    5.38 +   dynamic.  The compressor deals with some chunk of data at a time, and
    5.39 +   decides which method to use on a chunk-by-chunk basis.  A chunk might
    5.40 +   typically be 32 K or 64 K.  If the chunk is incompressible, then the
    5.41 +   "stored" method is used.  In this case, the bytes are simply stored as
    5.42 +   is, eight bits per byte, with none of the above coding.  The bytes are
    5.43 +   preceded by a count, since there is no longer an EOB code.
    5.44 +
    5.45 +   If the data is compressible, then either the fixed or dynamic methods
    5.46 +   are used.  In the dynamic method, the compressed data is preceded by
    5.47 +   an encoding of the literal/length and distance Huffman codes that are
    5.48 +   to be used to decode this block.  The representation is itself Huffman
    5.49 +   coded, and so is preceded by a description of that code.  These code
    5.50 +   descriptions take up a little space, and so for small blocks, there is
    5.51 +   a predefined set of codes, called the fixed codes.  The fixed method is
    5.52 +   used if the block codes up smaller that way (usually for quite small
    5.53 +   chunks), otherwise the dynamic method is used.  In the latter case, the
    5.54 +   codes are customized to the probabilities in the current block, and so
    5.55 +   can code it much better than the pre-determined fixed codes.
    5.56 + 
    5.57 +   The Huffman codes themselves are decoded using a multi-level table
    5.58 +   lookup, in order to maximize the speed of decoding plus the speed of
    5.59 +   building the decoding tables.  See the comments below that precede the
    5.60 +   lbits and dbits tuning parameters.
    5.61 + */
    5.62 +
    5.63 +
    5.64 +/*
    5.65 +   Notes beyond the 1.93a appnote.txt:
    5.66 +
    5.67 +   1. Distance pointers never point before the beginning of the output
    5.68 +      stream.
    5.69 +   2. Distance pointers can point back across blocks, up to 32k away.
    5.70 +   3. There is an implied maximum of 7 bits for the bit length table and
    5.71 +      15 bits for the actual data.
    5.72 +   4. If only one code exists, then it is encoded using one bit.  (Zero
    5.73 +      would be more efficient, but perhaps a little confusing.)  If two
    5.74 +      codes exist, they are coded using one bit each (0 and 1).
    5.75 +   5. There is no way of sending zero distance codes--a dummy must be
    5.76 +      sent if there are none.  (History: a pre 2.0 version of PKZIP would
    5.77 +      store blocks with no distance codes, but this was discovered to be
    5.78 +      too harsh a criterion.)  Valid only for 1.93a.  2.04c does allow
    5.79 +      zero distance codes, which is sent as one code of zero bits in
    5.80 +      length.
    5.81 +   6. There are up to 286 literal/length codes.  Code 256 represents the
    5.82 +      end-of-block.  Note however that the static length tree defines
    5.83 +      288 codes just to fill out the Huffman codes.  Codes 286 and 287
    5.84 +      cannot be used though, since there is no length base or extra bits
    5.85 +      defined for them.  Similarly, there are up to 30 distance codes.
    5.86 +      However, static trees define 32 codes (all 5 bits) to fill out the
    5.87 +      Huffman codes, but the last two had better not show up in the data.
    5.88 +   7. Unzip can check dynamic Huffman blocks for complete code sets.
    5.89 +      The exception is that a single code would not be complete (see #4).
    5.90 +   8. The five bits following the block type is really the number of
    5.91 +      literal codes sent minus 257.
    5.92 +   9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
    5.93 +      (1+6+6).  Therefore, to output three times the length, you output
    5.94 +      three codes (1+1+1), whereas to output four times the same length,
    5.95 +      you only need two codes (1+3).  Hmm.
    5.96 +  10. In the tree reconstruction algorithm, Code = Code + Increment
    5.97 +      only if BitLength(i) is not zero.  (Pretty obvious.)
    5.98 +  11. Correction: 4 Bits: # of Bit Length codes - 4     (4 - 19)
    5.99 +  12. Note: length code 284 can represent 227-258, but length code 285
   5.100 +      really is 258.  The last length deserves its own, short code
   5.101 +      since it gets used a lot in very redundant files.  The length
   5.102 +      258 is special since 258 - 3 (the min match length) is 255.
   5.103 +  13. The literal/length and distance code bit lengths are read as a
   5.104 +      single stream of lengths.  It is possible (and advantageous) for
   5.105 +      a repeat code (16, 17, or 18) to go across the boundary between
   5.106 +      the two sets of lengths.
   5.107 + */
   5.108 +
   5.109 +#ifdef RCSID
   5.110 +static char rcsid[] = "#Id: inflate.c,v 0.14 1993/06/10 13:27:04 jloup Exp #";
   5.111 +#endif
   5.112 +
   5.113 +#ifndef STATIC
   5.114 +
   5.115 +#if defined(STDC_HEADERS) || defined(HAVE_STDLIB_H)
   5.116 +#  include <sys/types.h>
   5.117 +#  include <stdlib.h>
   5.118 +#endif
   5.119 +
   5.120 +#include "gzip.h"
   5.121 +#define STATIC
   5.122 +#endif /* !STATIC */
   5.123 +
   5.124 +#ifndef INIT
   5.125 +#define INIT
   5.126 +#endif
   5.127 + 
   5.128 +#define slide window
   5.129 +
   5.130 +/* Huffman code lookup table entry--this entry is four bytes for machines
   5.131 +   that have 16-bit pointers (e.g. PC's in the small or medium model).
   5.132 +   Valid extra bits are 0..13.  e == 15 is EOB (end of block), e == 16
   5.133 +   means that v is a literal, 16 < e < 32 means that v is a pointer to
   5.134 +   the next table, which codes e - 16 bits, and lastly e == 99 indicates
   5.135 +   an unused code.  If a code with e == 99 is looked up, this implies an
   5.136 +   error in the data. */
   5.137 +struct huft {
   5.138 +    uch e;                /* number of extra bits or operation */
   5.139 +    uch b;                /* number of bits in this code or subcode */
   5.140 +    union {
   5.141 +        ush n;              /* literal, length base, or distance base */
   5.142 +        struct huft *t;     /* pointer to next level of table */
   5.143 +    } v;
   5.144 +};
   5.145 +
   5.146 +
   5.147 +/* Function prototypes */
   5.148 +STATIC int INIT huft_build OF((unsigned *, unsigned, unsigned, 
   5.149 +                               const ush *, const ush *, struct huft **, int *));
   5.150 +STATIC int INIT huft_free OF((struct huft *));
   5.151 +STATIC int INIT inflate_codes OF((struct huft *, struct huft *, int, int));
   5.152 +STATIC int INIT inflate_stored OF((void));
   5.153 +STATIC int INIT inflate_fixed OF((void));
   5.154 +STATIC int INIT inflate_dynamic OF((void));
   5.155 +STATIC int INIT inflate_block OF((int *));
   5.156 +STATIC int INIT inflate OF((void));
   5.157 +
   5.158 +
   5.159 +/* The inflate algorithm uses a sliding 32 K byte window on the uncompressed
   5.160 +   stream to find repeated byte strings.  This is implemented here as a
   5.161 +   circular buffer.  The index is updated simply by incrementing and then
   5.162 +   ANDing with 0x7fff (32K-1). */
   5.163 +/* It is left to other modules to supply the 32 K area.  It is assumed
   5.164 +   to be usable as if it were declared "uch slide[32768];" or as just
   5.165 +   "uch *slide;" and then malloc'ed in the latter case.  The definition
   5.166 +   must be in unzip.h, included above. */
   5.167 +/* unsigned wp;             current position in slide */
   5.168 +#define wp outcnt
   5.169 +#define flush_output(w) (wp=(w),flush_window())
   5.170 +
   5.171 +/* Tables for deflate from PKZIP's appnote.txt. */
   5.172 +static const unsigned border[] = {    /* Order of the bit length code lengths */
   5.173 +    16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
   5.174 +static const ush cplens[] = {         /* Copy lengths for literal codes 257..285 */
   5.175 +    3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
   5.176 +    35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
   5.177 +/* note: see note #13 above about the 258 in this list. */
   5.178 +static const ush cplext[] = {         /* Extra bits for literal codes 257..285 */
   5.179 +    0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
   5.180 +    3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99}; /* 99==invalid */
   5.181 +static const ush cpdist[] = {         /* Copy offsets for distance codes 0..29 */
   5.182 +    1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
   5.183 +    257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
   5.184 +    8193, 12289, 16385, 24577};
   5.185 +static const ush cpdext[] = {         /* Extra bits for distance codes */
   5.186 +    0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
   5.187 +    7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
   5.188 +    12, 12, 13, 13};
   5.189 +
   5.190 +
   5.191 +
   5.192 +/* Macros for inflate() bit peeking and grabbing.
   5.193 +   The usage is:
   5.194 +   
   5.195 +        NEEDBITS(j)
   5.196 +        x = b & mask_bits[j];
   5.197 +        DUMPBITS(j)
   5.198 +
   5.199 +   where NEEDBITS makes sure that b has at least j bits in it, and
   5.200 +   DUMPBITS removes the bits from b.  The macros use the variable k
   5.201 +   for the number of bits in b.  Normally, b and k are register
   5.202 +   variables for speed, and are initialized at the beginning of a
   5.203 +   routine that uses these macros from a global bit buffer and count.
   5.204 +
   5.205 +   If we assume that EOB will be the longest code, then we will never
   5.206 +   ask for bits with NEEDBITS that are beyond the end of the stream.
   5.207 +   So, NEEDBITS should not read any more bytes than are needed to
   5.208 +   meet the request.  Then no bytes need to be "returned" to the buffer
   5.209 +   at the end of the last block.
   5.210 +
   5.211 +   However, this assumption is not true for fixed blocks--the EOB code
   5.212 +   is 7 bits, but the other literal/length codes can be 8 or 9 bits.
   5.213 +   (The EOB code is shorter than other codes because fixed blocks are
   5.214 +   generally short.  So, while a block always has an EOB, many other
   5.215 +   literal/length codes have a significantly lower probability of
   5.216 +   showing up at all.)  However, by making the first table have a
   5.217 +   lookup of seven bits, the EOB code will be found in that first
   5.218 +   lookup, and so will not require that too many bits be pulled from
   5.219 +   the stream.
   5.220 + */
   5.221 +
   5.222 +STATIC ulg bb;                         /* bit buffer */
   5.223 +STATIC unsigned bk;                    /* bits in bit buffer */
   5.224 +
   5.225 +STATIC const ush mask_bits[] = {
   5.226 +    0x0000,
   5.227 +    0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
   5.228 +    0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
   5.229 +};
   5.230 +
   5.231 +#define NEXTBYTE()  ({ int v = get_byte(); if (v < 0) goto underrun; (uch)v; })
   5.232 +#define NEEDBITS(n) {while(k<(n)){b|=((ulg)NEXTBYTE())<<k;k+=8;}}
   5.233 +#define DUMPBITS(n) {b>>=(n);k-=(n);}
   5.234 +
   5.235 +#ifndef NO_INFLATE_MALLOC
   5.236 +/* A trivial malloc implementation, adapted from
   5.237 + *  malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994
   5.238 + */
   5.239 +
   5.240 +static unsigned long malloc_ptr;
   5.241 +static int malloc_count;
   5.242 +
   5.243 +static void *malloc(int size)
   5.244 +{
   5.245 +    void *p;
   5.246 +
   5.247 +    if (size < 0)
   5.248 +        error("Malloc error");
   5.249 +    if (!malloc_ptr)
   5.250 +        malloc_ptr = free_mem_ptr;
   5.251 +
   5.252 +    malloc_ptr = (malloc_ptr + 3) & ~3;     /* Align */
   5.253 +
   5.254 +    p = (void *)malloc_ptr;
   5.255 +    malloc_ptr += size;
   5.256 +
   5.257 +    if (free_mem_end_ptr && malloc_ptr >= free_mem_end_ptr)
   5.258 +        error("Out of memory");
   5.259 +
   5.260 +    malloc_count++;
   5.261 +    return p;
   5.262 +}
   5.263 +
   5.264 +static void free(void *where)
   5.265 +{
   5.266 +    malloc_count--;
   5.267 +    if (!malloc_count)
   5.268 +        malloc_ptr = free_mem_ptr;
   5.269 +}
   5.270 +#else
   5.271 +#define malloc(a) kmalloc(a, GFP_KERNEL)
   5.272 +#define free(a) kfree(a)
   5.273 +#endif
   5.274 +
   5.275 +/*
   5.276 +   Huffman code decoding is performed using a multi-level table lookup.
   5.277 +   The fastest way to decode is to simply build a lookup table whose
   5.278 +   size is determined by the longest code.  However, the time it takes
   5.279 +   to build this table can also be a factor if the data being decoded
   5.280 +   is not very long.  The most common codes are necessarily the
   5.281 +   shortest codes, so those codes dominate the decoding time, and hence
   5.282 +   the speed.  The idea is you can have a shorter table that decodes the
   5.283 +   shorter, more probable codes, and then point to subsidiary tables for
   5.284 +   the longer codes.  The time it costs to decode the longer codes is
   5.285 +   then traded against the time it takes to make longer tables.
   5.286 +
   5.287 +   This results of this trade are in the variables lbits and dbits
   5.288 +   below.  lbits is the number of bits the first level table for literal/
   5.289 +   length codes can decode in one step, and dbits is the same thing for
   5.290 +   the distance codes.  Subsequent tables are also less than or equal to
   5.291 +   those sizes.  These values may be adjusted either when all of the
   5.292 +   codes are shorter than that, in which case the longest code length in
   5.293 +   bits is used, or when the shortest code is *longer* than the requested
   5.294 +   table size, in which case the length of the shortest code in bits is
   5.295 +   used.
   5.296 +
   5.297 +   There are two different values for the two tables, since they code a
   5.298 +   different number of possibilities each.  The literal/length table
   5.299 +   codes 286 possible values, or in a flat code, a little over eight
   5.300 +   bits.  The distance table codes 30 possible values, or a little less
   5.301 +   than five bits, flat.  The optimum values for speed end up being
   5.302 +   about one bit more than those, so lbits is 8+1 and dbits is 5+1.
   5.303 +   The optimum values may differ though from machine to machine, and
   5.304 +   possibly even between compilers.  Your mileage may vary.
   5.305 + */
   5.306 +
   5.307 +
   5.308 +STATIC const int lbits = 9;          /* bits in base literal/length lookup table */
   5.309 +STATIC const int dbits = 6;          /* bits in base distance lookup table */
   5.310 +
   5.311 +
   5.312 +/* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
   5.313 +#define BMAX 16         /* maximum bit length of any code (16 for explode) */
   5.314 +#define N_MAX 288       /* maximum number of codes in any set */
   5.315 +
   5.316 +
   5.317 +STATIC unsigned hufts;         /* track memory usage */
   5.318 +
   5.319 +
   5.320 +STATIC int INIT huft_build(
   5.321 +    unsigned *b,            /* code lengths in bits (all assumed <= BMAX) */
   5.322 +    unsigned n,             /* number of codes (assumed <= N_MAX) */
   5.323 +    unsigned s,             /* number of simple-valued codes (0..s-1) */
   5.324 +    const ush *d,           /* list of base values for non-simple codes */
   5.325 +    const ush *e,           /* list of extra bits for non-simple codes */
   5.326 +    struct huft **t,        /* result: starting table */
   5.327 +    int *m                  /* maximum lookup bits, returns actual */
   5.328 +    )
   5.329 +/* Given a list of code lengths and a maximum table size, make a set of
   5.330 +   tables to decode that set of codes.  Return zero on success, one if
   5.331 +   the given code set is incomplete (the tables are still built in this
   5.332 +   case), two if the input is invalid (all zero length codes or an
   5.333 +   oversubscribed set of lengths), and three if not enough memory. */
   5.334 +{
   5.335 +    unsigned a;                   /* counter for codes of length k */
   5.336 +    unsigned f;                   /* i repeats in table every f entries */
   5.337 +    int g;                        /* maximum code length */
   5.338 +    int h;                        /* table level */
   5.339 +    register unsigned i;          /* counter, current code */
   5.340 +    register unsigned j;          /* counter */
   5.341 +    register int k;               /* number of bits in current code */
   5.342 +    int l;                        /* bits per table (returned in m) */
   5.343 +    register unsigned *p;         /* pointer into c[], b[], or v[] */
   5.344 +    register struct huft *q;      /* points to current table */
   5.345 +    struct huft r;                /* table entry for structure assignment */
   5.346 +    register int w;               /* bits before this table == (l * h) */
   5.347 +    unsigned *xp;                 /* pointer into x */
   5.348 +    int y;                        /* number of dummy codes added */
   5.349 +    unsigned z;                   /* number of entries in current table */
   5.350 +    struct {
   5.351 +        unsigned c[BMAX+1];           /* bit length count table */
   5.352 +        struct huft *u[BMAX];         /* table stack */
   5.353 +        unsigned v[N_MAX];            /* values in order of bit length */
   5.354 +        unsigned x[BMAX+1];           /* bit offsets, then code stack */
   5.355 +    } *stk;
   5.356 +    unsigned *c, *v, *x;
   5.357 +    struct huft **u;
   5.358 +    int ret;
   5.359 +
   5.360 +    DEBG("huft1 ");
   5.361 +
   5.362 +    stk = malloc(sizeof(*stk));
   5.363 +    if (stk == NULL)
   5.364 +        return 3;   /* out of memory */
   5.365 +
   5.366 +    c = stk->c;
   5.367 +    v = stk->v;
   5.368 +    x = stk->x;
   5.369 +    u = stk->u;
   5.370 +
   5.371 +    /* Generate counts for each bit length */
   5.372 +    memzero(stk->c, sizeof(stk->c));
   5.373 +    p = b;  i = n;
   5.374 +    do {
   5.375 +        Tracecv(*p, (stderr, (n-i >= ' ' && n-i <= '~' ? "%c %d\n" : "0x%x %d\n"), 
   5.376 +                     n-i, *p));
   5.377 +        c[*p]++;                    /* assume all entries <= BMAX */
   5.378 +        p++;                      /* Can't combine with above line (Solaris bug) */
   5.379 +    } while (--i);
   5.380 +    if (c[0] == n)                /* null input--all zero length codes */
   5.381 +    {
   5.382 +        *t = (struct huft *)NULL;
   5.383 +        *m = 0;
   5.384 +        ret = 2;
   5.385 +        goto out;
   5.386 +    }
   5.387 +
   5.388 +    DEBG("huft2 ");
   5.389 +
   5.390 +    /* Find minimum and maximum length, bound *m by those */
   5.391 +    l = *m;
   5.392 +    for (j = 1; j <= BMAX; j++)
   5.393 +        if (c[j])
   5.394 +            break;
   5.395 +    k = j;                        /* minimum code length */
   5.396 +    if ((unsigned)l < j)
   5.397 +        l = j;
   5.398 +    for (i = BMAX; i; i--)
   5.399 +        if (c[i])
   5.400 +            break;
   5.401 +    g = i;                        /* maximum code length */
   5.402 +    if ((unsigned)l > i)
   5.403 +        l = i;
   5.404 +    *m = l;
   5.405 +
   5.406 +    DEBG("huft3 ");
   5.407 +
   5.408 +    /* Adjust last length count to fill out codes, if needed */
   5.409 +    for (y = 1 << j; j < i; j++, y <<= 1)
   5.410 +        if ((y -= c[j]) < 0) {
   5.411 +            ret = 2;                 /* bad input: more codes than bits */
   5.412 +            goto out;
   5.413 +        }
   5.414 +    if ((y -= c[i]) < 0) {
   5.415 +        ret = 2;
   5.416 +        goto out;
   5.417 +    }
   5.418 +    c[i] += y;
   5.419 +
   5.420 +    DEBG("huft4 ");
   5.421 +
   5.422 +    /* Generate starting offsets into the value table for each length */
   5.423 +    x[1] = j = 0;
   5.424 +    p = c + 1;  xp = x + 2;
   5.425 +    while (--i) {                 /* note that i == g from above */
   5.426 +        *xp++ = (j += *p++);
   5.427 +    }
   5.428 +
   5.429 +    DEBG("huft5 ");
   5.430 +
   5.431 +    /* Make a table of values in order of bit lengths */
   5.432 +    p = b;  i = 0;
   5.433 +    do {
   5.434 +        if ((j = *p++) != 0)
   5.435 +            v[x[j]++] = i;
   5.436 +    } while (++i < n);
   5.437 +    n = x[g];                   /* set n to length of v */
   5.438 +
   5.439 +    DEBG("h6 ");
   5.440 +
   5.441 +    /* Generate the Huffman codes and for each, make the table entries */
   5.442 +    x[0] = i = 0;                 /* first Huffman code is zero */
   5.443 +    p = v;                        /* grab values in bit order */
   5.444 +    h = -1;                       /* no tables yet--level -1 */
   5.445 +    w = -l;                       /* bits decoded == (l * h) */
   5.446 +    u[0] = (struct huft *)NULL;   /* just to keep compilers happy */
   5.447 +    q = (struct huft *)NULL;      /* ditto */
   5.448 +    z = 0;                        /* ditto */
   5.449 +    DEBG("h6a ");
   5.450 +
   5.451 +    /* go through the bit lengths (k already is bits in shortest code) */
   5.452 +    for (; k <= g; k++)
   5.453 +    {
   5.454 +        DEBG("h6b ");
   5.455 +        a = c[k];
   5.456 +        while (a--)
   5.457 +        {
   5.458 +            DEBG("h6b1 ");
   5.459 +            /* here i is the Huffman code of length k bits for value *p */
   5.460 +            /* make tables up to required level */
   5.461 +            while (k > w + l)
   5.462 +            {
   5.463 +                DEBG1("1 ");
   5.464 +                h++;
   5.465 +                w += l;                 /* previous table always l bits */
   5.466 +
   5.467 +                /* compute minimum size table less than or equal to l bits */
   5.468 +                z = (z = g - w) > (unsigned)l ? l : z;  /* upper limit on table size */
   5.469 +                if ((f = 1 << (j = k - w)) > a + 1)     /* try a k-w bit table */
   5.470 +                {                       /* too few codes for k-w bit table */
   5.471 +                    DEBG1("2 ");
   5.472 +                    f -= a + 1;           /* deduct codes from patterns left */
   5.473 +                    xp = c + k;
   5.474 +                    if (j < z)
   5.475 +                        while (++j < z)       /* try smaller tables up to z bits */
   5.476 +                        {
   5.477 +                            if ((f <<= 1) <= *++xp)
   5.478 +                                break;            /* enough codes to use up j bits */
   5.479 +                            f -= *xp;           /* else deduct codes from patterns */
   5.480 +                        }
   5.481 +                }
   5.482 +                DEBG1("3 ");
   5.483 +                z = 1 << j;             /* table entries for j-bit table */
   5.484 +
   5.485 +                /* allocate and link in new table */
   5.486 +                if ((q = (struct huft *)malloc((z + 1)*sizeof(struct huft))) ==
   5.487 +                    (struct huft *)NULL)
   5.488 +                {
   5.489 +                    if (h)
   5.490 +                        huft_free(u[0]);
   5.491 +                    ret = 3;             /* not enough memory */
   5.492 +                    goto out;
   5.493 +                }
   5.494 +                DEBG1("4 ");
   5.495 +                hufts += z + 1;         /* track memory usage */
   5.496 +                *t = q + 1;             /* link to list for huft_free() */
   5.497 +                *(t = &(q->v.t)) = (struct huft *)NULL;
   5.498 +                u[h] = ++q;             /* table starts after link */
   5.499 +
   5.500 +                DEBG1("5 ");
   5.501 +                /* connect to last table, if there is one */
   5.502 +                if (h)
   5.503 +                {
   5.504 +                    x[h] = i;             /* save pattern for backing up */
   5.505 +                    r.b = (uch)l;         /* bits to dump before this table */
   5.506 +                    r.e = (uch)(16 + j);  /* bits in this table */
   5.507 +                    r.v.t = q;            /* pointer to this table */
   5.508 +                    j = i >> (w - l);     /* (get around Turbo C bug) */
   5.509 +                    u[h-1][j] = r;        /* connect to last table */
   5.510 +                }
   5.511 +                DEBG1("6 ");
   5.512 +            }
   5.513 +            DEBG("h6c ");
   5.514 +
   5.515 +            /* set up table entry in r */
   5.516 +            r.b = (uch)(k - w);
   5.517 +            if (p >= v + n)
   5.518 +                r.e = 99;               /* out of values--invalid code */
   5.519 +            else if (*p < s)
   5.520 +            {
   5.521 +                r.e = (uch)(*p < 256 ? 16 : 15);    /* 256 is end-of-block code */
   5.522 +                r.v.n = (ush)(*p);             /* simple code is just the value */
   5.523 +                p++;                           /* one compiler does not like *p++ */
   5.524 +            }
   5.525 +            else
   5.526 +            {
   5.527 +                r.e = (uch)e[*p - s];   /* non-simple--look up in lists */
   5.528 +                r.v.n = d[*p++ - s];
   5.529 +            }
   5.530 +            DEBG("h6d ");
   5.531 +
   5.532 +            /* fill code-like entries with r */
   5.533 +            f = 1 << (k - w);
   5.534 +            for (j = i >> w; j < z; j += f)
   5.535 +                q[j] = r;
   5.536 +
   5.537 +            /* backwards increment the k-bit code i */
   5.538 +            for (j = 1 << (k - 1); i & j; j >>= 1)
   5.539 +                i ^= j;
   5.540 +            i ^= j;
   5.541 +
   5.542 +            /* backup over finished tables */
   5.543 +            while ((i & ((1 << w) - 1)) != x[h])
   5.544 +            {
   5.545 +                h--;                    /* don't need to update q */
   5.546 +                w -= l;
   5.547 +            }
   5.548 +            DEBG("h6e ");
   5.549 +        }
   5.550 +        DEBG("h6f ");
   5.551 +    }
   5.552 +
   5.553 +    DEBG("huft7 ");
   5.554 +
   5.555 +    /* Return true (1) if we were given an incomplete table */
   5.556 +    ret = y != 0 && g != 1;
   5.557 +
   5.558 + out:
   5.559 +    free(stk);
   5.560 +    return ret;
   5.561 +}
   5.562 +
   5.563 +
   5.564 +
   5.565 +STATIC int INIT huft_free(
   5.566 +    struct huft *t         /* table to free */
   5.567 +    )
   5.568 +/* Free the malloc'ed tables built by huft_build(), which makes a linked
   5.569 +   list of the tables it made, with the links in a dummy first entry of
   5.570 +   each table. */
   5.571 +{
   5.572 +    register struct huft *p, *q;
   5.573 +
   5.574 +
   5.575 +    /* Go through linked list, freeing from the malloced (t[-1]) address. */
   5.576 +    p = t;
   5.577 +    while (p != (struct huft *)NULL)
   5.578 +    {
   5.579 +        q = (--p)->v.t;
   5.580 +        free((char*)p);
   5.581 +        p = q;
   5.582 +    } 
   5.583 +    return 0;
   5.584 +}
   5.585 +
   5.586 +
   5.587 +STATIC int INIT inflate_codes(
   5.588 +    struct huft *tl,    /* literal/length decoder tables */
   5.589 +    struct huft *td,    /* distance decoder tables */
   5.590 +    int bl,             /* number of bits decoded by tl[] */
   5.591 +    int bd              /* number of bits decoded by td[] */
   5.592 +    )
   5.593 +/* inflate (decompress) the codes in a deflated (compressed) block.
   5.594 +   Return an error code or zero if it all goes ok. */
   5.595 +{
   5.596 +    register unsigned e;  /* table entry flag/number of extra bits */
   5.597 +    unsigned n, d;        /* length and index for copy */
   5.598 +    unsigned w;           /* current window position */
   5.599 +    struct huft *t;       /* pointer to table entry */
   5.600 +    unsigned ml, md;      /* masks for bl and bd bits */
   5.601 +    register ulg b;       /* bit buffer */
   5.602 +    register unsigned k;  /* number of bits in bit buffer */
   5.603 +
   5.604 +
   5.605 +    /* make local copies of globals */
   5.606 +    b = bb;                       /* initialize bit buffer */
   5.607 +    k = bk;
   5.608 +    w = wp;                       /* initialize window position */
   5.609 +
   5.610 +    /* inflate the coded data */
   5.611 +    ml = mask_bits[bl];           /* precompute masks for speed */
   5.612 +    md = mask_bits[bd];
   5.613 +    for (;;)                      /* do until end of block */
   5.614 +    {
   5.615 +        NEEDBITS((unsigned)bl)
   5.616 +            if ((e = (t = tl + ((unsigned)b & ml))->e) > 16)
   5.617 +                do {
   5.618 +                    if (e == 99)
   5.619 +                        return 1;
   5.620 +                    DUMPBITS(t->b)
   5.621 +                        e -= 16;
   5.622 +                    NEEDBITS(e)
   5.623 +                        } while ((e = (t = t->v.t + ((unsigned)b & mask_bits[e]))->e) > 16);
   5.624 +        DUMPBITS(t->b)
   5.625 +            if (e == 16)                /* then it's a literal */
   5.626 +            {
   5.627 +                slide[w++] = (uch)t->v.n;
   5.628 +                Tracevv((stderr, "%c", slide[w-1]));
   5.629 +                if (w == WSIZE)
   5.630 +                {
   5.631 +                    flush_output(w);
   5.632 +                    w = 0;
   5.633 +                }
   5.634 +            }
   5.635 +            else                        /* it's an EOB or a length */
   5.636 +            {
   5.637 +                /* exit if end of block */
   5.638 +                if (e == 15)
   5.639 +                    break;
   5.640 +
   5.641 +                /* get length of block to copy */
   5.642 +                NEEDBITS(e)
   5.643 +                    n = t->v.n + ((unsigned)b & mask_bits[e]);
   5.644 +                DUMPBITS(e);
   5.645 +
   5.646 +                /* decode distance of block to copy */
   5.647 +                NEEDBITS((unsigned)bd)
   5.648 +                    if ((e = (t = td + ((unsigned)b & md))->e) > 16)
   5.649 +                        do {
   5.650 +                            if (e == 99)
   5.651 +                                return 1;
   5.652 +                            DUMPBITS(t->b)
   5.653 +                                e -= 16;
   5.654 +                            NEEDBITS(e)
   5.655 +                                } while ((e = (t = t->v.t + ((unsigned)b & mask_bits[e]))->e) > 16);
   5.656 +                DUMPBITS(t->b)
   5.657 +                    NEEDBITS(e)
   5.658 +                    d = w - t->v.n - ((unsigned)b & mask_bits[e]);
   5.659 +                DUMPBITS(e)
   5.660 +                    Tracevv((stderr,"\\[%d,%d]", w-d, n));
   5.661 +
   5.662 +                /* do the copy */
   5.663 +                do {
   5.664 +                    n -= (e = (e = WSIZE - ((d &= WSIZE-1) > w ? d : w)) > n ? n : e);
   5.665 +#if !defined(NOMEMCPY) && !defined(DEBUG)
   5.666 +                    if (w - d >= e)         /* (this test assumes unsigned comparison) */
   5.667 +                    {
   5.668 +                        memcpy(slide + w, slide + d, e);
   5.669 +                        w += e;
   5.670 +                        d += e;
   5.671 +                    }
   5.672 +                    else                      /* do it slow to avoid memcpy() overlap */
   5.673 +#endif /* !NOMEMCPY */
   5.674 +                        do {
   5.675 +                            slide[w++] = slide[d++];
   5.676 +                            Tracevv((stderr, "%c", slide[w-1]));
   5.677 +                        } while (--e);
   5.678 +                    if (w == WSIZE)
   5.679 +                    {
   5.680 +                        flush_output(w);
   5.681 +                        w = 0;
   5.682 +                    }
   5.683 +                } while (n);
   5.684 +            }
   5.685 +    }
   5.686 +
   5.687 +
   5.688 +    /* restore the globals from the locals */
   5.689 +    wp = w;                       /* restore global window pointer */
   5.690 +    bb = b;                       /* restore global bit buffer */
   5.691 +    bk = k;
   5.692 +
   5.693 +    /* done */
   5.694 +    return 0;
   5.695 +
   5.696 + underrun:
   5.697 +    return 4;   /* Input underrun */
   5.698 +}
   5.699 +
   5.700 +
   5.701 +
   5.702 +STATIC int INIT inflate_stored(void)
   5.703 +/* "decompress" an inflated type 0 (stored) block. */
   5.704 +{
   5.705 +    unsigned n;           /* number of bytes in block */
   5.706 +    unsigned w;           /* current window position */
   5.707 +    register ulg b;       /* bit buffer */
   5.708 +    register unsigned k;  /* number of bits in bit buffer */
   5.709 +
   5.710 +    DEBG("<stor");
   5.711 +
   5.712 +    /* make local copies of globals */
   5.713 +    b = bb;                       /* initialize bit buffer */
   5.714 +    k = bk;
   5.715 +    w = wp;                       /* initialize window position */
   5.716 +
   5.717 +
   5.718 +    /* go to byte boundary */
   5.719 +    n = k & 7;
   5.720 +    DUMPBITS(n);
   5.721 +
   5.722 +
   5.723 +    /* get the length and its complement */
   5.724 +    NEEDBITS(16)
   5.725 +        n = ((unsigned)b & 0xffff);
   5.726 +    DUMPBITS(16)
   5.727 +        NEEDBITS(16)
   5.728 +        if (n != (unsigned)((~b) & 0xffff))
   5.729 +            return 1;                   /* error in compressed data */
   5.730 +    DUMPBITS(16)
   5.731 +
   5.732 +
   5.733 +        /* read and output the compressed data */
   5.734 +        while (n--)
   5.735 +        {
   5.736 +            NEEDBITS(8)
   5.737 +                slide[w++] = (uch)b;
   5.738 +            if (w == WSIZE)
   5.739 +            {
   5.740 +                flush_output(w);
   5.741 +                w = 0;
   5.742 +            }
   5.743 +            DUMPBITS(8)
   5.744 +                }
   5.745 +
   5.746 +
   5.747 +    /* restore the globals from the locals */
   5.748 +    wp = w;                       /* restore global window pointer */
   5.749 +    bb = b;                       /* restore global bit buffer */
   5.750 +    bk = k;
   5.751 +
   5.752 +    DEBG(">");
   5.753 +    return 0;
   5.754 +
   5.755 + underrun:
   5.756 +    return 4;   /* Input underrun */
   5.757 +}
   5.758 +
   5.759 +
   5.760 +/*
   5.761 + * We use `noinline' here to prevent gcc-3.5 from using too much stack space
   5.762 + */
   5.763 +STATIC int noinline INIT inflate_fixed(void)
   5.764 +/* decompress an inflated type 1 (fixed Huffman codes) block.  We should
   5.765 +   either replace this with a custom decoder, or at least precompute the
   5.766 +   Huffman tables. */
   5.767 +{
   5.768 +    int i;                /* temporary variable */
   5.769 +    struct huft *tl;      /* literal/length code table */
   5.770 +    struct huft *td;      /* distance code table */
   5.771 +    int bl;               /* lookup bits for tl */
   5.772 +    int bd;               /* lookup bits for td */
   5.773 +    unsigned *l;          /* length list for huft_build */
   5.774 +
   5.775 +    DEBG("<fix");
   5.776 +
   5.777 +    l = malloc(sizeof(*l) * 288);
   5.778 +    if (l == NULL)
   5.779 +        return 3;   /* out of memory */
   5.780 +
   5.781 +    /* set up literal table */
   5.782 +    for (i = 0; i < 144; i++)
   5.783 +        l[i] = 8;
   5.784 +    for (; i < 256; i++)
   5.785 +        l[i] = 9;
   5.786 +    for (; i < 280; i++)
   5.787 +        l[i] = 7;
   5.788 +    for (; i < 288; i++)          /* make a complete, but wrong code set */
   5.789 +        l[i] = 8;
   5.790 +    bl = 7;
   5.791 +    if ((i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl)) != 0) {
   5.792 +        free(l);
   5.793 +        return i;
   5.794 +    }
   5.795 +
   5.796 +    /* set up distance table */
   5.797 +    for (i = 0; i < 30; i++)      /* make an incomplete code set */
   5.798 +        l[i] = 5;
   5.799 +    bd = 5;
   5.800 +    if ((i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd)) > 1)
   5.801 +    {
   5.802 +        huft_free(tl);
   5.803 +        free(l);
   5.804 +
   5.805 +        DEBG(">");
   5.806 +        return i;
   5.807 +    }
   5.808 +
   5.809 +
   5.810 +    /* decompress until an end-of-block code */
   5.811 +    if (inflate_codes(tl, td, bl, bd)) {
   5.812 +        free(l);
   5.813 +        return 1;
   5.814 +    }
   5.815 +
   5.816 +    /* free the decoding tables, return */
   5.817 +    free(l);
   5.818 +    huft_free(tl);
   5.819 +    huft_free(td);
   5.820 +    return 0;
   5.821 +}
   5.822 +
   5.823 +
   5.824 +/*
   5.825 + * We use `noinline' here to prevent gcc-3.5 from using too much stack space
   5.826 + */
   5.827 +STATIC int noinline INIT inflate_dynamic(void)
   5.828 +/* decompress an inflated type 2 (dynamic Huffman codes) block. */
   5.829 +{
   5.830 +    int i;                /* temporary variables */
   5.831 +    unsigned j;
   5.832 +    unsigned l;           /* last length */
   5.833 +    unsigned m;           /* mask for bit lengths table */
   5.834 +    unsigned n;           /* number of lengths to get */
   5.835 +    struct huft *tl;      /* literal/length code table */
   5.836 +    struct huft *td;      /* distance code table */
   5.837 +    int bl;               /* lookup bits for tl */
   5.838 +    int bd;               /* lookup bits for td */
   5.839 +    unsigned nb;          /* number of bit length codes */
   5.840 +    unsigned nl;          /* number of literal/length codes */
   5.841 +    unsigned nd;          /* number of distance codes */
   5.842 +    unsigned *ll;         /* literal/length and distance code lengths */
   5.843 +    register ulg b;       /* bit buffer */
   5.844 +    register unsigned k;  /* number of bits in bit buffer */
   5.845 +    int ret;
   5.846 +
   5.847 +    DEBG("<dyn");
   5.848 +
   5.849 +#ifdef PKZIP_BUG_WORKAROUND
   5.850 +    ll = malloc(sizeof(*ll) * (288+32));  /* literal/length and distance code lengths */
   5.851 +#else
   5.852 +    ll = malloc(sizeof(*ll) * (286+30));  /* literal/length and distance code lengths */
   5.853 +#endif
   5.854 +
   5.855 +    if (ll == NULL)
   5.856 +        return 1;
   5.857 +
   5.858 +    /* make local bit buffer */
   5.859 +    b = bb;
   5.860 +    k = bk;
   5.861 +
   5.862 +
   5.863 +    /* read in table lengths */
   5.864 +    NEEDBITS(5)
   5.865 +        nl = 257 + ((unsigned)b & 0x1f);      /* number of literal/length codes */
   5.866 +    DUMPBITS(5)
   5.867 +        NEEDBITS(5)
   5.868 +        nd = 1 + ((unsigned)b & 0x1f);        /* number of distance codes */
   5.869 +    DUMPBITS(5)
   5.870 +        NEEDBITS(4)
   5.871 +        nb = 4 + ((unsigned)b & 0xf);         /* number of bit length codes */
   5.872 +    DUMPBITS(4)
   5.873 +#ifdef PKZIP_BUG_WORKAROUND
   5.874 +        if (nl > 288 || nd > 32)
   5.875 +#else
   5.876 +            if (nl > 286 || nd > 30)
   5.877 +#endif
   5.878 +            {
   5.879 +                ret = 1;             /* bad lengths */
   5.880 +                goto out;
   5.881 +            }
   5.882 +
   5.883 +    DEBG("dyn1 ");
   5.884 +
   5.885 +    /* read in bit-length-code lengths */
   5.886 +    for (j = 0; j < nb; j++)
   5.887 +    {
   5.888 +        NEEDBITS(3)
   5.889 +            ll[border[j]] = (unsigned)b & 7;
   5.890 +        DUMPBITS(3)
   5.891 +            }
   5.892 +    for (; j < 19; j++)
   5.893 +        ll[border[j]] = 0;
   5.894 +
   5.895 +    DEBG("dyn2 ");
   5.896 +
   5.897 +    /* build decoding table for trees--single level, 7 bit lookup */
   5.898 +    bl = 7;
   5.899 +    if ((i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl)) != 0)
   5.900 +    {
   5.901 +        if (i == 1)
   5.902 +            huft_free(tl);
   5.903 +        ret = i;                   /* incomplete code set */
   5.904 +        goto out;
   5.905 +    }
   5.906 +
   5.907 +    DEBG("dyn3 ");
   5.908 +
   5.909 +    /* read in literal and distance code lengths */
   5.910 +    n = nl + nd;
   5.911 +    m = mask_bits[bl];
   5.912 +    i = l = 0;
   5.913 +    while ((unsigned)i < n)
   5.914 +    {
   5.915 +        NEEDBITS((unsigned)bl)
   5.916 +            j = (td = tl + ((unsigned)b & m))->b;
   5.917 +        DUMPBITS(j)
   5.918 +            j = td->v.n;
   5.919 +        if (j < 16)                 /* length of code in bits (0..15) */
   5.920 +            ll[i++] = l = j;          /* save last length in l */
   5.921 +        else if (j == 16)           /* repeat last length 3 to 6 times */
   5.922 +        {
   5.923 +            NEEDBITS(2)
   5.924 +                j = 3 + ((unsigned)b & 3);
   5.925 +            DUMPBITS(2)
   5.926 +                if ((unsigned)i + j > n) {
   5.927 +                    ret = 1;
   5.928 +                    goto out;
   5.929 +                }
   5.930 +            while (j--)
   5.931 +                ll[i++] = l;
   5.932 +        }
   5.933 +        else if (j == 17)           /* 3 to 10 zero length codes */
   5.934 +        {
   5.935 +            NEEDBITS(3)
   5.936 +                j = 3 + ((unsigned)b & 7);
   5.937 +            DUMPBITS(3)
   5.938 +                if ((unsigned)i + j > n) {
   5.939 +                    ret = 1;
   5.940 +                    goto out;
   5.941 +                }
   5.942 +            while (j--)
   5.943 +                ll[i++] = 0;
   5.944 +            l = 0;
   5.945 +        }
   5.946 +        else                        /* j == 18: 11 to 138 zero length codes */
   5.947 +        {
   5.948 +            NEEDBITS(7)
   5.949 +                j = 11 + ((unsigned)b & 0x7f);
   5.950 +            DUMPBITS(7)
   5.951 +                if ((unsigned)i + j > n) {
   5.952 +                    ret = 1;
   5.953 +                    goto out;
   5.954 +                }
   5.955 +            while (j--)
   5.956 +                ll[i++] = 0;
   5.957 +            l = 0;
   5.958 +        }
   5.959 +    }
   5.960 +
   5.961 +    DEBG("dyn4 ");
   5.962 +
   5.963 +    /* free decoding table for trees */
   5.964 +    huft_free(tl);
   5.965 +
   5.966 +    DEBG("dyn5 ");
   5.967 +
   5.968 +    /* restore the global bit buffer */
   5.969 +    bb = b;
   5.970 +    bk = k;
   5.971 +
   5.972 +    DEBG("dyn5a ");
   5.973 +
   5.974 +    /* build the decoding tables for literal/length and distance codes */
   5.975 +    bl = lbits;
   5.976 +    if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0)
   5.977 +    {
   5.978 +        DEBG("dyn5b ");
   5.979 +        if (i == 1) {
   5.980 +            error("incomplete literal tree");
   5.981 +            huft_free(tl);
   5.982 +        }
   5.983 +        ret = i;                   /* incomplete code set */
   5.984 +        goto out;
   5.985 +    }
   5.986 +    DEBG("dyn5c ");
   5.987 +    bd = dbits;
   5.988 +    if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0)
   5.989 +    {
   5.990 +        DEBG("dyn5d ");
   5.991 +        if (i == 1) {
   5.992 +            error("incomplete distance tree");
   5.993 +#ifdef PKZIP_BUG_WORKAROUND
   5.994 +            i = 0;
   5.995 +        }
   5.996 +#else
   5.997 +        huft_free(td);
   5.998 +    }
   5.999 +    huft_free(tl);
  5.1000 +    ret = i;                   /* incomplete code set */
  5.1001 +    goto out;
  5.1002 +#endif
  5.1003 +}
  5.1004 +
  5.1005 +DEBG("dyn6 ");
  5.1006 +
  5.1007 +  /* decompress until an end-of-block code */
  5.1008 +if (inflate_codes(tl, td, bl, bd)) {
  5.1009 +    ret = 1;
  5.1010 +    goto out;
  5.1011 +}
  5.1012 +
  5.1013 +DEBG("dyn7 ");
  5.1014 +
  5.1015 +  /* free the decoding tables, return */
  5.1016 +huft_free(tl);
  5.1017 +huft_free(td);
  5.1018 +
  5.1019 +DEBG(">");
  5.1020 +ret = 0;
  5.1021 +out:
  5.1022 +free(ll);
  5.1023 +return ret;
  5.1024 +
  5.1025 +underrun:
  5.1026 +ret = 4;   /* Input underrun */
  5.1027 +goto out;
  5.1028 +}
  5.1029 +
  5.1030 +
  5.1031 +
  5.1032 +STATIC int INIT inflate_block(
  5.1033 +int *e                  /* last block flag */
  5.1034 +)
  5.1035 +/* decompress an inflated block */
  5.1036 +{
  5.1037 +unsigned t;           /* block type */
  5.1038 +register ulg b;       /* bit buffer */
  5.1039 +register unsigned k;  /* number of bits in bit buffer */
  5.1040 +
  5.1041 +DEBG("<blk");
  5.1042 +
  5.1043 +/* make local bit buffer */
  5.1044 +b = bb;
  5.1045 +k = bk;
  5.1046 +
  5.1047 +
  5.1048 +/* read in last block bit */
  5.1049 +NEEDBITS(1)
  5.1050 +    *e = (int)b & 1;
  5.1051 +    DUMPBITS(1)
  5.1052 +
  5.1053 +
  5.1054 +    /* read in block type */
  5.1055 +    NEEDBITS(2)
  5.1056 +    t = (unsigned)b & 3;
  5.1057 +    DUMPBITS(2)
  5.1058 +
  5.1059 +
  5.1060 +    /* restore the global bit buffer */
  5.1061 +    bb = b;
  5.1062 +    bk = k;
  5.1063 +
  5.1064 +    /* inflate that block type */
  5.1065 +    if (t == 2)
  5.1066 +    return inflate_dynamic();
  5.1067 +    if (t == 0)
  5.1068 +    return inflate_stored();
  5.1069 +    if (t == 1)
  5.1070 +    return inflate_fixed();
  5.1071 +
  5.1072 +    DEBG(">");
  5.1073 +
  5.1074 +    /* bad block type */
  5.1075 +    return 2;
  5.1076 +
  5.1077 +    underrun:
  5.1078 +    return 4;   /* Input underrun */
  5.1079 +}
  5.1080 +
  5.1081 +
  5.1082 +
  5.1083 +STATIC int INIT inflate(void)
  5.1084 +/* decompress an inflated entry */
  5.1085 +{
  5.1086 +    int e;                /* last block flag */
  5.1087 +    int r;                /* result code */
  5.1088 +    unsigned h;           /* maximum struct huft's malloc'ed */
  5.1089 +
  5.1090 +    /* initialize window, bit buffer */
  5.1091 +    wp = 0;
  5.1092 +    bk = 0;
  5.1093 +    bb = 0;
  5.1094 +
  5.1095 +
  5.1096 +    /* decompress until the last block */
  5.1097 +    h = 0;
  5.1098 +    do {
  5.1099 +        hufts = 0;
  5.1100 +#ifdef ARCH_HAS_DECOMP_WDOG
  5.1101 +        arch_decomp_wdog();
  5.1102 +#endif
  5.1103 +        r = inflate_block(&e);
  5.1104 +        if (r)
  5.1105 +            return r;
  5.1106 +        if (hufts > h)
  5.1107 +            h = hufts;
  5.1108 +    } while (!e);
  5.1109 +
  5.1110 +    /* Undo too much lookahead. The next read will be byte aligned so we
  5.1111 +     * can discard unused bits in the last meaningful byte.
  5.1112 +     */
  5.1113 +    while (bk >= 8) {
  5.1114 +        bk -= 8;
  5.1115 +        inptr--;
  5.1116 +    }
  5.1117 +
  5.1118 +    /* flush out slide */
  5.1119 +    flush_output(wp);
  5.1120 +
  5.1121 +
  5.1122 +    /* return success */
  5.1123 +#ifdef DEBUG
  5.1124 +    fprintf(stderr, "<%u> ", h);
  5.1125 +#endif /* DEBUG */
  5.1126 +    return 0;
  5.1127 +}
  5.1128 +
  5.1129 +/**********************************************************************
  5.1130 + *
  5.1131 + * The following are support routines for inflate.c
  5.1132 + *
  5.1133 + **********************************************************************/
  5.1134 +
  5.1135 +static ulg crc_32_tab[256];
  5.1136 +static ulg crc;  /* initialized in makecrc() so it'll reside in bss */
  5.1137 +#define CRC_VALUE (crc ^ 0xffffffffUL)
  5.1138 +
  5.1139 +/*
  5.1140 + * Code to compute the CRC-32 table. Borrowed from 
  5.1141 + * gzip-1.0.3/makecrc.c.
  5.1142 + */
  5.1143 +
  5.1144 +static void INIT
  5.1145 +makecrc(void)
  5.1146 +{
  5.1147 +/* Not copyrighted 1990 Mark Adler */
  5.1148 +
  5.1149 +    unsigned long c;      /* crc shift register */
  5.1150 +    unsigned long e;      /* polynomial exclusive-or pattern */
  5.1151 +    int i;                /* counter for all possible eight bit values */
  5.1152 +    int k;                /* byte being shifted into crc apparatus */
  5.1153 +
  5.1154 +    /* terms of polynomial defining this crc (except x^32): */
  5.1155 +    static const int p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
  5.1156 +
  5.1157 +    /* Make exclusive-or pattern from polynomial */
  5.1158 +    e = 0;
  5.1159 +    for (i = 0; i < sizeof(p)/sizeof(int); i++)
  5.1160 +        e |= 1L << (31 - p[i]);
  5.1161 +
  5.1162 +    crc_32_tab[0] = 0;
  5.1163 +
  5.1164 +    for (i = 1; i < 256; i++)
  5.1165 +    {
  5.1166 +        c = 0;
  5.1167 +        for (k = i | 256; k != 1; k >>= 1)
  5.1168 +        {
  5.1169 +            c = c & 1 ? (c >> 1) ^ e : c >> 1;
  5.1170 +            if (k & 1)
  5.1171 +                c ^= e;
  5.1172 +        }
  5.1173 +        crc_32_tab[i] = c;
  5.1174 +    }
  5.1175 +
  5.1176 +    /* this is initialized here so this code could reside in ROM */
  5.1177 +    crc = (ulg)0xffffffffUL; /* shift register contents */
  5.1178 +}
  5.1179 +
  5.1180 +/* gzip flag byte */
  5.1181 +#define ASCII_FLAG   0x01 /* bit 0 set: file probably ASCII text */
  5.1182 +#define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
  5.1183 +#define EXTRA_FIELD  0x04 /* bit 2 set: extra field present */
  5.1184 +#define ORIG_NAME    0x08 /* bit 3 set: original file name present */
  5.1185 +#define COMMENT      0x10 /* bit 4 set: file comment present */
  5.1186 +#define ENCRYPTED    0x20 /* bit 5 set: file is encrypted */
  5.1187 +#define RESERVED     0xC0 /* bit 6,7:   reserved */
  5.1188 +
  5.1189 +/*
  5.1190 + * Do the uncompression!
  5.1191 + */
  5.1192 +static int INIT gunzip(void)
  5.1193 +{
  5.1194 +    uch flags;
  5.1195 +    unsigned char magic[2]; /* magic header */
  5.1196 +    char method;
  5.1197 +    ulg orig_crc = 0;       /* original crc */
  5.1198 +    ulg orig_len = 0;       /* original uncompressed length */
  5.1199 +    int res;
  5.1200 +
  5.1201 +    magic[0] = NEXTBYTE();
  5.1202 +    magic[1] = NEXTBYTE();
  5.1203 +    method   = NEXTBYTE();
  5.1204 +
  5.1205 +    if (magic[0] != 037 ||
  5.1206 +        ((magic[1] != 0213) && (magic[1] != 0236))) {
  5.1207 +        error("bad gzip magic numbers");
  5.1208 +        return -1;
  5.1209 +    }
  5.1210 +
  5.1211 +    /* We only support method #8, DEFLATED */
  5.1212 +    if (method != 8)  {
  5.1213 +        error("internal error, invalid method");
  5.1214 +        return -1;
  5.1215 +    }
  5.1216 +
  5.1217 +    flags  = (uch)get_byte();
  5.1218 +    if ((flags & ENCRYPTED) != 0) {
  5.1219 +        error("Input is encrypted");
  5.1220 +        return -1;
  5.1221 +    }
  5.1222 +    if ((flags & CONTINUATION) != 0) {
  5.1223 +        error("Multi part input");
  5.1224 +        return -1;
  5.1225 +    }
  5.1226 +    if ((flags & RESERVED) != 0) {
  5.1227 +        error("Input has invalid flags");
  5.1228 +        return -1;
  5.1229 +    }
  5.1230 +    NEXTBYTE(); /* Get timestamp */
  5.1231 +    NEXTBYTE();
  5.1232 +    NEXTBYTE();
  5.1233 +    NEXTBYTE();
  5.1234 +
  5.1235 +    (void)NEXTBYTE();  /* Ignore extra flags for the moment */
  5.1236 +    (void)NEXTBYTE();  /* Ignore OS type for the moment */
  5.1237 +
  5.1238 +    if ((flags & EXTRA_FIELD) != 0) {
  5.1239 +        unsigned len = (unsigned)NEXTBYTE();
  5.1240 +        len |= ((unsigned)NEXTBYTE())<<8;
  5.1241 +        while (len--) (void)NEXTBYTE();
  5.1242 +    }
  5.1243 +
  5.1244 +    /* Get original file name if it was truncated */
  5.1245 +    if ((flags & ORIG_NAME) != 0) {
  5.1246 +        /* Discard the old name */
  5.1247 +        while (NEXTBYTE() != 0) /* null */ ;
  5.1248 +    } 
  5.1249 +
  5.1250 +    /* Discard file comment if any */
  5.1251 +    if ((flags & COMMENT) != 0) {
  5.1252 +        while (NEXTBYTE() != 0) /* null */ ;
  5.1253 +    }
  5.1254 +
  5.1255 +    /* Decompress */
  5.1256 +    if ((res = inflate())) {
  5.1257 +        switch (res) {
  5.1258 +        case 0:
  5.1259 +            break;
  5.1260 +        case 1:
  5.1261 +            error("invalid compressed format (err=1)");
  5.1262 +            break;
  5.1263 +        case 2:
  5.1264 +            error("invalid compressed format (err=2)");
  5.1265 +            break;
  5.1266 +        case 3:
  5.1267 +            error("out of memory");
  5.1268 +            break;
  5.1269 +        case 4:
  5.1270 +            error("out of input data");
  5.1271 +            break;
  5.1272 +        default:
  5.1273 +            error("invalid compressed format (other)");
  5.1274 +        }
  5.1275 +        return -1;
  5.1276 +    }
  5.1277 +     
  5.1278 +    /* Get the crc and original length */
  5.1279 +    /* crc32  (see algorithm.doc)
  5.1280 +     * uncompressed input size modulo 2^32
  5.1281 +     */
  5.1282 +    orig_crc = (ulg) NEXTBYTE();
  5.1283 +    orig_crc |= (ulg) NEXTBYTE() << 8;
  5.1284 +    orig_crc |= (ulg) NEXTBYTE() << 16;
  5.1285 +    orig_crc |= (ulg) NEXTBYTE() << 24;
  5.1286 +    
  5.1287 +    orig_len = (ulg) NEXTBYTE();
  5.1288 +    orig_len |= (ulg) NEXTBYTE() << 8;
  5.1289 +    orig_len |= (ulg) NEXTBYTE() << 16;
  5.1290 +    orig_len |= (ulg) NEXTBYTE() << 24;
  5.1291 +    
  5.1292 +    /* Validate decompression */
  5.1293 +    if (orig_crc != CRC_VALUE) {
  5.1294 +        error("crc error");
  5.1295 +        return -1;
  5.1296 +    }
  5.1297 +    if (orig_len != bytes_out) {
  5.1298 +        error("length error");
  5.1299 +        return -1;
  5.1300 +    }
  5.1301 +    return 0;
  5.1302 +
  5.1303 + underrun:   /* NEXTBYTE() goto's here if needed */
  5.1304 +    error("out of input data");
  5.1305 +    return -1;
  5.1306 +}
     6.1 --- a/xen/include/xen/sched.h	Thu Jan 22 17:41:13 2009 +0000
     6.2 +++ b/xen/include/xen/sched.h	Thu Jan 22 18:00:48 2009 +0000
     6.3 @@ -351,7 +351,8 @@ struct domain *domain_create(
     6.4  
     6.5  int construct_dom0(
     6.6      struct domain *d,
     6.7 -    unsigned long image_start, unsigned long image_len, 
     6.8 +    unsigned long image_base,
     6.9 +    unsigned long image_start, unsigned long image_len,
    6.10      unsigned long initrd_start, unsigned long initrd_len,
    6.11      char *cmdline);
    6.12