wok diff linux/stuff/linux-lzma-2.6.22.9.u @ rev 2804

update qt-4.4.1 to qt-4.5.0
author Allan Pinto <allan316@gmail.com>
date Wed Apr 29 09:28:07 2009 +0000 (2009-04-29)
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/linux/stuff/linux-lzma-2.6.22.9.u	Wed Apr 29 09:28:07 2009 +0000
     1.3 @@ -0,0 +1,2075 @@
     1.4 +--- linux-2.6.22.9/arch/i386/boot/compressed/Makefile
     1.5 ++++ linux-2.6.22.9/arch/i386/boot/compressed/Makefile
     1.6 +@@ -4,7 +4,7 @@
     1.7 + # create a compressed vmlinux image from the original vmlinux
     1.8 + #
     1.9 + 
    1.10 +-targets		:= vmlinux vmlinux.bin vmlinux.bin.gz head.o misc.o piggy.o \
    1.11 ++targets		:= vmlinux vmlinux.bin vmlinux.bin.gz vmlinux.bin.bz2 vmlinux.bin.lzma head.o misc.o piggy.o \
    1.12 + 			vmlinux.bin.all vmlinux.relocs
    1.13 + EXTRA_AFLAGS	:= -traditional
    1.14 + 
    1.15 +@@ -39,7 +39,27 @@ $(obj)/vmlinux.bin.gz: $(obj)/vmlinux.bi
    1.16 + 	$(call if_changed,gzip)
    1.17 + endif
    1.18 + 
    1.19 ++ifdef CONFIG_RELOCATABLE
    1.20 ++$(obj)/vmlinux.bin.bz2: $(obj)/vmlinux.bin.all FORCE
    1.21 ++	$(call if_changed,bzip2)
    1.22 ++else
    1.23 ++$(obj)/vmlinux.bin.bz2: $(obj)/vmlinux.bin FORCE
    1.24 ++	$(call if_changed,bzip2)
    1.25 ++endif
    1.26 ++
    1.27 ++ifdef CONFIG_RELOCATABLE
    1.28 ++$(obj)/vmlinux.bin.lzma: $(obj)/vmlinux.bin.all FORCE
    1.29 ++	$(call if_changed,lzma)
    1.30 ++else
    1.31 ++$(obj)/vmlinux.bin.lzma: $(obj)/vmlinux.bin FORCE
    1.32 ++	$(call if_changed,lzma)
    1.33 ++endif
    1.34 ++
    1.35 + LDFLAGS_piggy.o := -r --format binary --oformat elf32-i386 -T
    1.36 + 
    1.37 +-$(obj)/piggy.o: $(src)/vmlinux.scr $(obj)/vmlinux.bin.gz FORCE
    1.38 ++suffix_$(CONFIG_KERNEL_GZIP)  = gz
    1.39 ++suffix_$(CONFIG_KERNEL_BZIP2) = bz2
    1.40 ++suffix_$(CONFIG_KERNEL_LZMA)  = lzma
    1.41 ++
    1.42 ++$(obj)/piggy.o: $(src)/vmlinux.scr $(obj)/vmlinux.bin.$(suffix_y) FORCE
    1.43 + 	$(call if_changed,ld)
    1.44 +
    1.45 +--- linux-2.6.22.9/arch/i386/boot/compressed/misc.c
    1.46 ++++ linux-2.6.22.9/arch/i386/boot/compressed/misc.c
    1.47 +@@ -121,9 +121,12 @@
    1.48 + 				 * always be larger than our output buffer.
    1.49 + 				 */
    1.50 + 
    1.51 ++#ifdef CONFIG_KERNEL_GZIP
    1.52 + static uch *inbuf;	/* input buffer */
    1.53 ++#endif
    1.54 + static uch *window;	/* Sliding window buffer, (and final output buffer) */
    1.55 + 
    1.56 ++#ifdef CONFIG_KERNEL_GZIP
    1.57 + static unsigned insize;  /* valid bytes in inbuf */
    1.58 + static unsigned inptr;   /* index of next byte to be processed in inbuf */
    1.59 + static unsigned outcnt;  /* bytes in output buffer */
    1.60 +@@ -158,9 +161,14 @@ static unsigned outcnt;  /* bytes in out
    1.61 + 
    1.62 + static int  fill_inbuf(void);
    1.63 + static void flush_window(void);
    1.64 ++#endif
    1.65 ++
    1.66 + static void error(char *m);
    1.67 ++
    1.68 ++#ifdef CONFIG_KERNEL_GZIP
    1.69 + static void gzip_mark(void **);
    1.70 + static void gzip_release(void **);
    1.71 ++#endif
    1.72 +   
    1.73 + /*
    1.74 +  * This is set up by the setup-routine at boot-time
    1.75 +@@ -181,7 +189,9 @@ static long bytes_out = 0;
    1.76 + static void *malloc(int size);
    1.77 + static void free(void *where);
    1.78 + 
    1.79 ++#if (defined CONFIG_KERNEL_GZIP || defined CONFIG_KERNEL_BZIP2)
    1.80 + static void *memset(void *s, int c, unsigned n);
    1.81 ++#endif
    1.82 + static void *memcpy(void *dest, const void *src, unsigned n);
    1.83 + 
    1.84 + static void putstr(const char *);
    1.85 +@@ -189,7 +199,11 @@ static void putstr(const char *);
    1.86 + static unsigned long free_mem_ptr;
    1.87 + static unsigned long free_mem_end_ptr;
    1.88 + 
    1.89 ++#if (defined CONFIG_KERNEL_BZIP2 || defined CONFIG_KERNEL_LZMA)
    1.90 ++#define HEAP_SIZE             0x400000
    1.91 ++#else
    1.92 + #define HEAP_SIZE             0x4000
    1.93 ++#endif
    1.94 + 
    1.95 + static char *vidmem = (char *)0xb8000;
    1.96 + static int vidport;
    1.97 +@@ -199,7 +213,29 @@ static int lines, cols;
    1.98 + void *xquad_portio;
    1.99 + #endif
   1.100 + 
   1.101 ++#if (defined CONFIG_KERNEL_BZIP2 || defined CONFIG_KERNEL_LZMA)
   1.102 ++
   1.103 ++#define large_malloc malloc
   1.104 ++#define large_free free
   1.105 ++
   1.106 ++#ifdef current
   1.107 ++#undef current
   1.108 ++#endif
   1.109 ++
   1.110 ++#define INCLUDED
   1.111 ++#endif
   1.112 ++
   1.113 ++#ifdef CONFIG_KERNEL_GZIP
   1.114 + #include "../../../../lib/inflate.c"
   1.115 ++#endif
   1.116 ++
   1.117 ++#ifdef CONFIG_KERNEL_BZIP2
   1.118 ++#include "../../../../lib/decompress_bunzip2.c"
   1.119 ++#endif
   1.120 ++
   1.121 ++#ifdef CONFIG_KERNEL_LZMA
   1.122 ++#include "../../../../lib/decompress_unlzma.c"
   1.123 ++#endif
   1.124 + 
   1.125 + static void *malloc(int size)
   1.126 + {
   1.127 +@@ -223,6 +259,7 @@ static void free(void *where)
   1.128 + {	/* Don't care */
   1.129 + }
   1.130 + 
   1.131 ++#ifdef CONFIG_KERNEL_GZIP
   1.132 + static void gzip_mark(void **ptr)
   1.133 + {
   1.134 + 	*ptr = (void *) free_mem_ptr;
   1.135 +@@ -232,6 +269,7 @@ static void gzip_release(void **ptr)
   1.136 + {
   1.137 + 	free_mem_ptr = (unsigned long) *ptr;
   1.138 + }
   1.139 ++#endif
   1.140 +  
   1.141 + static void scroll(void)
   1.142 + {
   1.143 +@@ -279,6 +317,7 @@ static void putstr(const char *s)
   1.144 + 	outb_p(0xff & (pos >> 1), vidport+1);
   1.145 + }
   1.146 + 
   1.147 ++#if (defined CONFIG_KERNEL_GZIP || defined CONFIG_KERNEL_BZIP2)
   1.148 + static void* memset(void* s, int c, unsigned n)
   1.149 + {
   1.150 + 	int i;
   1.151 +@@ -287,6 +326,7 @@ static void* memset(void* s, int c, unsi
   1.152 + 	for (i=0;i<n;i++) ss[i] = c;
   1.153 + 	return s;
   1.154 + }
   1.155 ++#endif
   1.156 + 
   1.157 + static void* memcpy(void* dest, const void* src, unsigned n)
   1.158 + {
   1.159 +@@ -297,6 +337,26 @@ static void* memcpy(void* dest, const vo
   1.160 + 	return dest;
   1.161 + }
   1.162 + 
   1.163 ++#ifndef CONFIG_KERNEL_GZIP
   1.164 ++/* ===========================================================================
   1.165 ++ * Write the output window window[0..outcnt-1] and update bytes_out.
   1.166 ++ * (Used for the decompressed data only.)
   1.167 ++ */
   1.168 ++static int compr_flush(char *data, unsigned int len)
   1.169 ++{
   1.170 ++    unsigned n;
   1.171 ++    uch *out;
   1.172 ++    
   1.173 ++    out = window; 
   1.174 ++    for (n = 0; n < len; n++) {
   1.175 ++	    *out++ = *data++;
   1.176 ++    }
   1.177 ++    bytes_out += (ulg)len;
   1.178 ++    window += (ulg)len;
   1.179 ++    return len;
   1.180 ++}
   1.181 ++
   1.182 ++#else
   1.183 + /* ===========================================================================
   1.184 +  * Fill the input buffer. This is called only when the buffer is empty
   1.185 +  * and at least one byte is really needed.
   1.186 +@@ -329,6 +389,7 @@ static void flush_window(void)
   1.187 + 	bytes_out += (ulg)outcnt;
   1.188 + 	outcnt = 0;
   1.189 + }
   1.190 ++#endif
   1.191 + 
   1.192 + static void error(char *x)
   1.193 + {
   1.194 +@@ -358,9 +419,11 @@ asmlinkage void decompress_kernel(void *
   1.195 + 	window = output;  	/* Output buffer (Normally at 1M) */
   1.196 + 	free_mem_ptr     = end;	/* Heap  */
   1.197 + 	free_mem_end_ptr = end + HEAP_SIZE;
   1.198 ++#ifdef CONFIG_KERNEL_GZIP
   1.199 + 	inbuf  = input_data;	/* Input buffer */
   1.200 + 	insize = input_len;
   1.201 + 	inptr  = 0;
   1.202 ++#endif
   1.203 + 
   1.204 + 	if ((u32)output & (CONFIG_PHYSICAL_ALIGN -1))
   1.205 + 		error("Destination address not CONFIG_PHYSICAL_ALIGN aligned");
   1.206 +@@ -371,9 +434,21 @@ asmlinkage void decompress_kernel(void *
   1.207 + 		error("Wrong destination address");
   1.208 + #endif
   1.209 + 
   1.210 ++#ifdef CONFIG_KERNEL_BZIP2
   1.211 ++	putstr("Bunzipping Linux... ");
   1.212 ++	bunzip2(input_data, input_len-4, NULL, compr_flush, NULL);
   1.213 ++#endif
   1.214 ++
   1.215 ++#ifdef CONFIG_KERNEL_LZMA
   1.216 ++	putstr("Unlzmaing Linux... ");
   1.217 ++	unlzma(input_data, input_len-4, NULL, compr_flush, NULL);
   1.218 ++#endif
   1.219 ++
   1.220 ++#ifdef CONFIG_KERNEL_GZIP
   1.221 + 	makecrc();
   1.222 + 	putstr("Uncompressing Linux... ");
   1.223 + 	gunzip();
   1.224 ++#endif
   1.225 + 	putstr("Ok, booting the kernel.\n");
   1.226 + 	return;
   1.227 + }
   1.228 +
   1.229 +--- linux-2.6.22.9/drivers/block/Kconfig
   1.230 ++++ linux-2.6.22.9/drivers/block/Kconfig
   1.231 +@@ -406,6 +406,30 @@
   1.232 + 	  setups function - apparently needed by the rd_load_image routine
   1.233 + 	  that supposes the filesystem in the image uses a 1024 blocksize.
   1.234 + 
   1.235 ++config RD_BZIP2
   1.236 ++	bool "Initial ramdisk compressed using bzip2"
   1.237 ++	default n
   1.238 ++	depends on BLK_DEV_INITRD=y
   1.239 ++	help
   1.240 ++	  Support loading of a bzip2 encoded initial ramdisk or cpio buffer
   1.241 ++	  If unsure, say N.
   1.242 ++
   1.243 ++config RD_LZMA
   1.244 ++	bool "Initial ramdisk compressed using lzma"
   1.245 ++	default n
   1.246 ++	depends on BLK_DEV_INITRD=y
   1.247 ++	help
   1.248 ++	  Support loading of a lzma encoded initial ramdisk or cpio buffer
   1.249 ++	  If unsure, say N.
   1.250 ++
   1.251 ++config RD_GZIP
   1.252 ++	bool "Initial ramdisk compressed using gzip"
   1.253 ++	default y
   1.254 ++	depends on BLK_DEV_INITRD=y
   1.255 ++	help
   1.256 ++	  Support loading of a gzip encoded initial ramdisk or cpio buffer.
   1.257 ++	  If unsure, say Y.
   1.258 ++
   1.259 + config CDROM_PKTCDVD
   1.260 + 	tristate "Packet writing on CD/DVD media"
   1.261 + 	depends on !UML
   1.262 +
   1.263 +--- linux-2.6.22.9/include/linux/decompress_bunzip2.h
   1.264 ++++ linux-2.6.22.9/include/linux/decompress_bunzip2.h
   1.265 +@@ -0,0 +1,16 @@
   1.266 ++#ifndef DECOMPRESS_BUNZIP2_H
   1.267 ++#define DECOMPRESS_BUNZIP2_H
   1.268 ++
   1.269 ++/* Other housekeeping constants */
   1.270 ++#define BZIP2_IOBUF_SIZE		4096
   1.271 ++
   1.272 ++#ifndef STATIC
   1.273 ++#define STATIC /**/
   1.274 ++#endif
   1.275 ++
   1.276 ++STATIC int bunzip2(char *inbuf, int len, 
   1.277 ++		   int(*fill)(void*,unsigned int),
   1.278 ++		   int(*writebb)(char*,unsigned int),
   1.279 ++		   int *pos);
   1.280 ++
   1.281 ++#endif
   1.282 +
   1.283 +--- linux-2.6.22.9/include/linux/decompress_generic.h
   1.284 ++++ linux-2.6.22.9/include/linux/decompress_generic.h
   1.285 +@@ -0,0 +1,28 @@
   1.286 ++#ifndef DECOMPRESS_GENERIC_H
   1.287 ++#define DECOMPRESS_GENERIC_H
   1.288 ++
   1.289 ++/* Minimal chunksize to be read.
   1.290 ++ * Bzip2 prefers at least 4096
   1.291 ++ * Lzma prefers 0x10000 */
   1.292 ++#define COMPR_IOBUF_SIZE	4096
   1.293 ++
   1.294 ++typedef int (*uncompress_fn) (char *inbuf, int len, 
   1.295 ++			      int(*fill)(char*,unsigned int),
   1.296 ++			      int(*writebb)(char*,unsigned int),
   1.297 ++			      int *posp);
   1.298 ++
   1.299 ++/* inbuf   - input buffer
   1.300 ++ * len     - len of pre-read data in inbuf
   1.301 ++ * fill    - function to fill inbuf if empty
   1.302 ++ * writebb - function to write out outbug
   1.303 ++ * posp    - if non-null, input position (number of bytes read) will be
   1.304 ++ *           returned here
   1.305 ++ *
   1.306 ++ * If len != 0, the inbuf is initialized (with as much data), and fill
   1.307 ++ * should not be called
   1.308 ++ * If len = 0, the inbuf is allocated, but empty. Its size is IOBUF_SIZE
   1.309 ++ * fill should be called (repeatedly...) to read data, at most IOBUF_SIZE
   1.310 ++ */
   1.311 ++ 
   1.312 ++
   1.313 ++#endif
   1.314 +
   1.315 +--- linux-2.6.22.9/include/linux/decompress_unlzma.h
   1.316 ++++ linux-2.6.22.9/include/linux/decompress_unlzma.h
   1.317 +@@ -0,0 +1,15 @@
   1.318 ++#ifndef DECOMPRESS_UNLZMA_H
   1.319 ++#define DECOMPRESS_UNLZMA_H
   1.320 ++
   1.321 ++#define LZMA_IOBUF_SIZE	0x10000
   1.322 ++
   1.323 ++#ifndef STATIC
   1.324 ++#define STATIC /**/
   1.325 ++#endif
   1.326 ++
   1.327 ++STATIC int unlzma(char *inbuf, int len, 
   1.328 ++		  int(*fill)(void*,unsigned int),
   1.329 ++		  int(*writebb)(char*,unsigned int),
   1.330 ++		  int *pos);
   1.331 ++
   1.332 ++#endif
   1.333 +
   1.334 +--- linux-2.6.22.9/init/do_mounts_rd.c
   1.335 ++++ linux-2.6.22.9/init/do_mounts_rd.c
   1.336 +@@ -8,6 +8,16 @@
   1.337 + #include <linux/initrd.h>
   1.338 + #include <linux/string.h>
   1.339 + 
   1.340 ++#ifdef CONFIG_RD_BZIP2
   1.341 ++#include <linux/decompress_bunzip2.h>
   1.342 ++#undef STATIC
   1.343 ++#endif
   1.344 ++
   1.345 ++#ifdef CONFIG_RD_LZMA
   1.346 ++#include <linux/decompress_unlzma.h>
   1.347 ++#undef STATIC
   1.348 ++#endif
   1.349 ++
   1.350 + #include "do_mounts.h"
   1.351 + 
   1.352 + #define BUILD_CRAMDISK
   1.353 +@@ -30,7 +40,15 @@ static int __init ramdisk_start_setup(ch
   1.354 + }
   1.355 + __setup("ramdisk_start=", ramdisk_start_setup);
   1.356 + 
   1.357 ++#ifdef CONFIG_RD_GZIP
   1.358 + static int __init crd_load(int in_fd, int out_fd);
   1.359 ++#endif
   1.360 ++#ifdef CONFIG_RD_BZIP2
   1.361 ++static int __init crd_load_bzip2(int in_fd, int out_fd);
   1.362 ++#endif
   1.363 ++#ifdef CONFIG_RD_LZMA
   1.364 ++static int __init crd_load_lzma(int in_fd, int out_fd);
   1.365 ++#endif
   1.366 + 
   1.367 + /*
   1.368 +  * This routine tries to find a RAM disk image to load, and returns the
   1.369 +@@ -46,7 +64,7 @@ static int __init crd_load(int in_fd, in
   1.370 +  * 	gzip
   1.371 +  */
   1.372 + static int __init 
   1.373 +-identify_ramdisk_image(int fd, int start_block)
   1.374 ++identify_ramdisk_image(int fd, int start_block, int *ztype)
   1.375 + {
   1.376 + 	const int size = 512;
   1.377 + 	struct minix_super_block *minixsb;
   1.378 +@@ -72,6 +90,7 @@ identify_ramdisk_image(int fd, int start
   1.379 + 	sys_lseek(fd, start_block * BLOCK_SIZE, 0);
   1.380 + 	sys_read(fd, buf, size);
   1.381 + 
   1.382 ++#ifdef CONFIG_RD_GZIP
   1.383 + 	/*
   1.384 + 	 * If it matches the gzip magic numbers, return -1
   1.385 + 	 */
   1.386 +@@ -79,9 +98,40 @@ identify_ramdisk_image(int fd, int start
   1.387 + 		printk(KERN_NOTICE
   1.388 + 		       "RAMDISK: Compressed image found at block %d\n",
   1.389 + 		       start_block);
   1.390 ++		*ztype = 0;
   1.391 ++		nblocks = 0;
   1.392 ++		goto done;
   1.393 ++	}
   1.394 ++#endif
   1.395 ++
   1.396 ++#ifdef CONFIG_RD_BZIP2
   1.397 ++	/*
   1.398 ++	 * If it matches the bzip magic numbers, return -1
   1.399 ++	 */
   1.400 ++	if (buf[0] == 0x42 && (buf[1] == 0x5a)) {
   1.401 ++		printk(KERN_NOTICE
   1.402 ++		       "RAMDISK: Bzipped image found at block %d\n",
   1.403 ++		       start_block);
   1.404 ++		*ztype = 1;
   1.405 + 		nblocks = 0;
   1.406 + 		goto done;
   1.407 + 	}
   1.408 ++#endif
   1.409 ++
   1.410 ++#ifdef CONFIG_RD_LZMA
   1.411 ++	/*
   1.412 ++	 * If it matches the bzip magic numbers, return -1
   1.413 ++	 */
   1.414 ++	if (buf[0] == 0x5d && (buf[1] == 0x00)) {
   1.415 ++		printk(KERN_NOTICE
   1.416 ++		       "RAMDISK: Lzma image found at block %d\n",
   1.417 ++		       start_block);
   1.418 ++		*ztype = 2;
   1.419 ++		nblocks = 0;
   1.420 ++		goto done;
   1.421 ++	}
   1.422 ++#endif
   1.423 ++
   1.424 + 
   1.425 + 	/* romfs is at block zero too */
   1.426 + 	if (romfsb->word0 == ROMSB_WORD0 &&
   1.427 +@@ -145,6 +195,7 @@ int __init rd_load_image(char *from)
   1.428 + 	int nblocks, i, disk;
   1.429 + 	char *buf = NULL;
   1.430 + 	unsigned short rotate = 0;
   1.431 ++	int ztype=-1;
   1.432 + #if !defined(CONFIG_S390) && !defined(CONFIG_PPC_ISERIES)
   1.433 + 	char rotator[4] = { '|' , '/' , '-' , '\\' };
   1.434 + #endif
   1.435 +@@ -157,14 +208,38 @@ int __init rd_load_image(char *from)
   1.436 + 	if (in_fd < 0)
   1.437 + 		goto noclose_input;
   1.438 + 
   1.439 +-	nblocks = identify_ramdisk_image(in_fd, rd_image_start);
   1.440 ++	nblocks = identify_ramdisk_image(in_fd, rd_image_start, &ztype);
   1.441 + 	if (nblocks < 0)
   1.442 + 		goto done;
   1.443 + 
   1.444 + 	if (nblocks == 0) {
   1.445 + #ifdef BUILD_CRAMDISK
   1.446 +-		if (crd_load(in_fd, out_fd) == 0)
   1.447 +-			goto successful_load;
   1.448 ++		switch(ztype) {
   1.449 ++
   1.450 ++#ifdef CONFIG_RD_GZIP
   1.451 ++			case 0:
   1.452 ++				if (crd_load(in_fd, out_fd) == 0)
   1.453 ++					goto successful_load;
   1.454 ++				break;
   1.455 ++#endif
   1.456 ++
   1.457 ++#ifdef CONFIG_RD_BZIP2
   1.458 ++			case 1:
   1.459 ++				if (crd_load_bzip2(in_fd, out_fd) == 0)
   1.460 ++					goto successful_load;
   1.461 ++				break;
   1.462 ++#endif
   1.463 ++
   1.464 ++#ifdef CONFIG_RD_LZMA
   1.465 ++			case 2:
   1.466 ++				if (crd_load_lzma(in_fd, out_fd) == 0)
   1.467 ++					goto successful_load;
   1.468 ++				break;
   1.469 ++#endif
   1.470 ++
   1.471 ++			default:
   1.472 ++				break;
   1.473 ++		}
   1.474 + #else
   1.475 + 		printk(KERN_NOTICE
   1.476 + 		       "RAMDISK: Kernel does not support compressed "
   1.477 +@@ -269,6 +344,7 @@ int __init rd_load_disk(int n)
   1.478 + 
   1.479 + #ifdef BUILD_CRAMDISK
   1.480 + 
   1.481 ++#ifdef CONFIG_RD_GZIP
   1.482 + /*
   1.483 +  * gzip declarations
   1.484 +  */
   1.485 +@@ -296,8 +372,11 @@ static unsigned outcnt;  /* bytes in out
   1.486 + static int exit_code;
   1.487 + static int unzip_error;
   1.488 + static long bytes_out;
   1.489 ++#endif
   1.490 ++
   1.491 + static int crd_infd, crd_outfd;
   1.492 + 
   1.493 ++#ifdef CONFIG_RD_GZIP
   1.494 + #define get_byte()  (inptr < insize ? inbuf[inptr++] : fill_inbuf())
   1.495 + 		
   1.496 + /* Diagnostic functions (stubbed out) */
   1.497 +@@ -359,7 +438,22 @@ static int __init fill_inbuf(void)
   1.498 + 
   1.499 + 	return inbuf[0];
   1.500 + }
   1.501 ++#endif
   1.502 + 
   1.503 ++#if (defined CONFIG_RD_BZIP2 || defined CONFIG_RD_LZMA)
   1.504 ++static int __init compr_fill(void *buf, unsigned int len)
   1.505 ++{
   1.506 ++	int r = sys_read(crd_infd, buf, len);
   1.507 ++	if(r < 0) {
   1.508 ++		printk(KERN_ERR "RAMDISK: error while reading compressed data");
   1.509 ++	} else if(r == 0) {
   1.510 ++		printk(KERN_ERR "RAMDISK: EOF while reading compressed data");
   1.511 ++	}
   1.512 ++	return r;
   1.513 ++}
   1.514 ++#endif
   1.515 ++
   1.516 ++#ifdef CONFIG_RD_GZIP
   1.517 + /* ===========================================================================
   1.518 +  * Write the output window window[0..outcnt-1] and update crc and bytes_out.
   1.519 +  * (Used for the decompressed data only.)
   1.520 +@@ -385,7 +479,24 @@ static void __init flush_window(void)
   1.521 +     bytes_out += (ulg)outcnt;
   1.522 +     outcnt = 0;
   1.523 + }
   1.524 ++#endif
   1.525 + 
   1.526 ++#if (defined CONFIG_RD_BZIP2 || defined CONFIG_RD_LZMA)
   1.527 ++static int __init compr_flush(void *window, unsigned int outcnt) {
   1.528 ++	static int progressDots=0;
   1.529 ++	int written = sys_write(crd_outfd, window, outcnt);
   1.530 ++	if (written != outcnt) {
   1.531 ++		printk(KERN_ERR "RAMDISK: incomplete write (%d != %d)\n",
   1.532 ++		       written, outcnt);
   1.533 ++	}
   1.534 ++	progressDots = (progressDots+1)%10;
   1.535 ++	if(!progressDots)
   1.536 ++		printk(".");
   1.537 ++	return outcnt;
   1.538 ++}
   1.539 ++#endif
   1.540 ++
   1.541 ++#ifdef CONFIG_RD_GZIP
   1.542 + static void __init error(char *x)
   1.543 + {
   1.544 + 	printk(KERN_ERR "%s\n", x);
   1.545 +@@ -425,5 +536,43 @@ static int __init crd_load(int in_fd, in
   1.546 + 	kfree(window);
   1.547 + 	return result;
   1.548 + }
   1.549 ++#endif
   1.550 ++
   1.551 ++#if (defined CONFIG_RD_BZIP2 || defined CONFIG_RD_LZMA)
   1.552 ++static int __init crd_load_compr(int in_fd, int out_fd, int size,
   1.553 ++				 int (*deco)(char *,int,
   1.554 ++					     int(*fill)(void*,unsigned int),
   1.555 ++					     int(*flush)(void*,unsigned int),
   1.556 ++					     int *))
   1.557 ++{
   1.558 ++	int result;
   1.559 ++	char *inbuf = kmalloc(size, GFP_KERNEL);
   1.560 ++	crd_infd = in_fd;
   1.561 ++	crd_outfd = out_fd;
   1.562 ++	if (inbuf == 0) {
   1.563 ++		printk(KERN_ERR "RAMDISK: Couldn't allocate decompression buffer\n");
   1.564 ++		return -1;
   1.565 ++	}
   1.566 ++	result=deco(inbuf, 0, compr_fill, compr_flush, NULL);
   1.567 ++	kfree(inbuf);
   1.568 ++	printk("\n");
   1.569 ++	return result;
   1.570 ++}
   1.571 ++#endif
   1.572 ++
   1.573 ++#ifdef CONFIG_RD_BZIP2
   1.574 ++static int __init crd_load_bzip2(int in_fd, int out_fd)
   1.575 ++{
   1.576 ++	return crd_load_compr(in_fd, out_fd, BZIP2_IOBUF_SIZE, bunzip2);
   1.577 ++}
   1.578 ++#endif
   1.579 ++
   1.580 ++#ifdef CONFIG_RD_LZMA
   1.581 ++static int __init crd_load_lzma(int in_fd, int out_fd)
   1.582 ++{
   1.583 ++	return crd_load_compr(in_fd, out_fd, LZMA_IOBUF_SIZE, unlzma);
   1.584 ++}
   1.585 ++
   1.586 ++#endif
   1.587 + 
   1.588 + #endif  /* BUILD_CRAMDISK */
   1.589 +
   1.590 +--- linux-2.6.22.9/init/initramfs.c
   1.591 ++++ linux-2.6.22.9/init/initramfs.c
   1.592 +@@ -7,6 +7,15 @@
   1.593 + #include <linux/string.h>
   1.594 + #include <linux/syscalls.h>
   1.595 + 
   1.596 ++/* We need to enable RD_GZIP unconditionnally, as the built-in
   1.597 ++ * initramfs is gzip-compressed, alas!
   1.598 ++ * We can only wonder why, though, as the whole kernel (which contains
   1.599 ++ * built-in initramfs) is gzip (or bzip) compressed anyways afterwards...
   1.600 ++ */
   1.601 ++#ifndef CONFIG_RD_GZIP
   1.602 ++#define CONFIG_RD_GZIP
   1.603 ++#endif
   1.604 ++
   1.605 + static __initdata char *message;
   1.606 + static void __init error(char *x)
   1.607 + {
   1.608 +@@ -347,11 +356,13 @@ static int __init write_buffer(char *buf
   1.609 + 	return len - count;
   1.610 + }
   1.611 + 
   1.612 +-static void __init flush_buffer(char *buf, unsigned len)
   1.613 ++
   1.614 ++static int __init flush_buffer(char *buf, unsigned len)
   1.615 + {
   1.616 + 	int written;
   1.617 ++	int origLen = len;
   1.618 + 	if (message)
   1.619 +-		return;
   1.620 ++		return -1;
   1.621 + 	while ((written = write_buffer(buf, len)) < len && !message) {
   1.622 + 		char c = buf[written];
   1.623 + 		if (c == '0') {
   1.624 +@@ -365,8 +376,24 @@ static void __init flush_buffer(char *bu
   1.625 + 		} else
   1.626 + 			error("junk in compressed archive");
   1.627 + 	}
   1.628 ++	return origLen;
   1.629 + }
   1.630 + 
   1.631 ++#ifdef CONFIG_RD_BZIP2
   1.632 ++#include <linux/decompress_bunzip2.h>
   1.633 ++#undef STATIC
   1.634 ++
   1.635 ++#endif
   1.636 ++
   1.637 ++#ifdef CONFIG_RD_LZMA
   1.638 ++#include <linux/decompress_unlzma.h>
   1.639 ++#undef STATIC
   1.640 ++
   1.641 ++#endif
   1.642 ++
   1.643 ++static unsigned inptr;   /* index of next byte to be processed in inbuf */
   1.644 ++
   1.645 ++#ifdef CONFIG_RD_GZIP
   1.646 + /*
   1.647 +  * gzip declarations
   1.648 +  */
   1.649 +@@ -388,7 +415,6 @@ static uch *inbuf;
   1.650 + static uch *window;
   1.651 + 
   1.652 + static unsigned insize;  /* valid bytes in inbuf */
   1.653 +-static unsigned inptr;   /* index of next byte to be processed in inbuf */
   1.654 + static unsigned outcnt;  /* bytes in output buffer */
   1.655 + static long bytes_out;
   1.656 + 
   1.657 +@@ -440,6 +466,7 @@ static void __init flush_window(void)
   1.658 + 	bytes_out += (ulg)outcnt;
   1.659 + 	outcnt = 0;
   1.660 + }
   1.661 ++#endif
   1.662 + 
   1.663 + static char * __init unpack_to_rootfs(char *buf, unsigned len, int check_only)
   1.664 + {
   1.665 +@@ -448,9 +475,11 @@ static char * __init unpack_to_rootfs(ch
   1.666 + 	header_buf = malloc(110);
   1.667 + 	symlink_buf = malloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1);
   1.668 + 	name_buf = malloc(N_ALIGN(PATH_MAX));
   1.669 ++#ifdef CONFIG_RD_GZIP
   1.670 + 	window = malloc(WSIZE);
   1.671 + 	if (!window || !header_buf || !symlink_buf || !name_buf)
   1.672 + 		panic("can't allocate buffers");
   1.673 ++#endif
   1.674 + 	state = Start;
   1.675 + 	this_header = 0;
   1.676 + 	message = NULL;
   1.677 +@@ -470,6 +499,7 @@ static char * __init unpack_to_rootfs(ch
   1.678 + 			continue;
   1.679 + 		}
   1.680 + 		this_header = 0;
   1.681 ++#ifdef CONFIG_RD_GZIP
   1.682 + 		insize = len;
   1.683 + 		inbuf = buf;
   1.684 + 		inptr = 0;
   1.685 +@@ -477,14 +507,38 @@ static char * __init unpack_to_rootfs(ch
   1.686 + 		bytes_out = 0;
   1.687 + 		crc = (ulg)0xffffffffL; /* shift register contents */
   1.688 + 		makecrc();
   1.689 +-		gunzip();
   1.690 ++		if(!gunzip() && message == NULL)
   1.691 ++			goto ok;
   1.692 ++#endif
   1.693 ++
   1.694 ++#ifdef CONFIG_RD_BZIP2
   1.695 ++		message = NULL; /* Zero out message, or else cpio will
   1.696 ++				   think an error has already occured */
   1.697 ++		if(!bunzip2(buf, len, NULL, flush_buffer, &inptr) < 0 &&
   1.698 ++		   message == NULL) {
   1.699 ++			goto ok;
   1.700 ++		}
   1.701 ++#endif
   1.702 ++
   1.703 ++#ifdef CONFIG_RD_LZMA
   1.704 ++		message = NULL; /* Zero out message, or else cpio will
   1.705 ++				   think an error has already occured */
   1.706 ++		if(!unlzma(buf, len, NULL, flush_buffer, &inptr) < 0 &&
   1.707 ++		   message == NULL) {
   1.708 ++			goto ok;
   1.709 ++		}
   1.710 ++#endif
   1.711 ++    ok:
   1.712 ++
   1.713 + 		if (state != Reset)
   1.714 +-			error("junk in gzipped archive");
   1.715 ++			error("junk in compressed archive");
   1.716 + 		this_header = saved_offset + inptr;
   1.717 + 		buf += inptr;
   1.718 + 		len -= inptr;
   1.719 + 	}
   1.720 ++#ifdef CONFIG_RD_GZIP
   1.721 + 	free(window);
   1.722 ++#endif
   1.723 + 	free(name_buf);
   1.724 + 	free(symlink_buf);
   1.725 + 	free(header_buf);
   1.726 +
   1.727 +--- linux-2.6.22.9/init/Kconfig
   1.728 ++++ linux-2.6.22.9/init/Kconfig
   1.729 +@@ -95,6 +95,56 @@
   1.730 + 
   1.731 + 	  which is done within the script "scripts/setlocalversion".)
   1.732 + 
   1.733 ++choice
   1.734 ++        prompt "Kernel compression mode"
   1.735 ++        default KERNEL_GZIP
   1.736 ++        help
   1.737 ++	  The linux kernel is a kind of self-extracting executable.
   1.738 ++	  Several compression algorithms are available, which differ
   1.739 ++	  in efficiency, compression and decompression speed.
   1.740 ++	  Compression speed is only relevant when building a kernel.
   1.741 ++	  Decompression speed is relevant at each boot.
   1.742 ++
   1.743 ++	  If you have any problems with bzip2 or lzma compressed
   1.744 ++	  kernels, mail me (Alain Knaff) <alain@knaff.lu>. (An older
   1.745 ++	  version of this functionality (bzip2 only), for 2.4, was
   1.746 ++	  supplied by Christian Ludwig)
   1.747 ++
   1.748 ++	  High compression options are mostly useful for users, who
   1.749 ++	  are low on disk space (embedded systems), but for whom ram
   1.750 ++	  size matters less.
   1.751 ++
   1.752 ++	  If in doubt, select 'gzip'
   1.753 ++
   1.754 ++config KERNEL_GZIP
   1.755 ++       bool "Gzip"
   1.756 ++       help
   1.757 ++         The old and tries gzip compression. Its compression ratio is
   1.758 ++	 the poorest among the 3 choices; however its speed (both
   1.759 ++	 compression and decompression) is the fastest.
   1.760 ++
   1.761 ++config KERNEL_BZIP2
   1.762 ++	bool "Bzip2"
   1.763 ++	help
   1.764 ++	  Its compression ratio and speed is intermediate.
   1.765 ++	  Decompression speed is slowest among the 3.
   1.766 ++	  The kernel size is about 10 per cent smaller with bzip2,
   1.767 ++	  in comparison to gzip.
   1.768 ++	  Bzip2 uses a large amount of memory. For modern kernels
   1.769 ++	  you will need at least 8MB RAM or more for booting.
   1.770 ++
   1.771 ++config KERNEL_LZMA
   1.772 ++       bool "LZMA"
   1.773 ++       help
   1.774 ++         The most recent compression algorithm.
   1.775 ++	 Its ratio is best, decompression speed is between the other
   1.776 ++	 2. Compression is slowest.
   1.777 ++	 The kernel size is about 33 per cent smaller with lzma,
   1.778 ++	 in comparison to gzip.
   1.779 ++
   1.780 ++endchoice
   1.781 ++
   1.782 ++
   1.783 + config SWAP
   1.784 + 	bool "Support for paging of anonymous memory (swap)"
   1.785 + 	depends on MMU && BLOCK
   1.786 +
   1.787 +--- linux-2.6.22.9/lib/decompress_bunzip2.c
   1.788 ++++ linux-2.6.22.9/lib/decompress_bunzip2.c
   1.789 +@@ -0,0 +1,645 @@
   1.790 ++/* vi: set sw=4 ts=4: */
   1.791 ++/*	Small bzip2 deflate implementation, by Rob Landley (rob@landley.net).
   1.792 ++
   1.793 ++	Based on bzip2 decompression code by Julian R Seward (jseward@acm.org),
   1.794 ++	which also acknowledges contributions by Mike Burrows, David Wheeler,
   1.795 ++	Peter Fenwick, Alistair Moffat, Radford Neal, Ian H. Witten,
   1.796 ++	Robert Sedgewick, and Jon L. Bentley.
   1.797 ++
   1.798 ++	This code is licensed under the LGPLv2:
   1.799 ++		LGPL (http://www.gnu.org/copyleft/lgpl.html
   1.800 ++*/
   1.801 ++
   1.802 ++/*
   1.803 ++	Size and speed optimizations by Manuel Novoa III  (mjn3@codepoet.org).
   1.804 ++
   1.805 ++	More efficient reading of Huffman codes, a streamlined read_bunzip()
   1.806 ++	function, and various other tweaks.  In (limited) tests, approximately
   1.807 ++	20% faster than bzcat on x86 and about 10% faster on arm.
   1.808 ++
   1.809 ++	Note that about 2/3 of the time is spent in read_unzip() reversing
   1.810 ++	the Burrows-Wheeler transformation.  Much of that time is delay
   1.811 ++	resulting from cache misses.
   1.812 ++
   1.813 ++	I would ask that anyone benefiting from this work, especially those
   1.814 ++	using it in commercial products, consider making a donation to my local
   1.815 ++	non-profit hospice organization in the name of the woman I loved, who
   1.816 ++	passed away Feb. 12, 2003.
   1.817 ++
   1.818 ++		In memory of Toni W. Hagan
   1.819 ++
   1.820 ++		Hospice of Acadiana, Inc.
   1.821 ++		2600 Johnston St., Suite 200
   1.822 ++		Lafayette, LA 70503-3240
   1.823 ++
   1.824 ++		Phone (337) 232-1234 or 1-800-738-2226
   1.825 ++		Fax   (337) 232-1297
   1.826 ++
   1.827 ++		http://www.hospiceacadiana.com/
   1.828 ++
   1.829 ++	Manuel
   1.830 ++ */
   1.831 ++
   1.832 ++/*
   1.833 ++	Made it fit for running in Linux Kernel by Alain Knaff (alain@knaff.lu)
   1.834 ++*/
   1.835 ++
   1.836 ++
   1.837 ++#ifndef STATIC
   1.838 ++
   1.839 ++#include <linux/kernel.h>
   1.840 ++#include <linux/fs.h>
   1.841 ++#include <linux/string.h>
   1.842 ++
   1.843 ++#ifdef TEST
   1.844 ++#include "test.h"
   1.845 ++#else
   1.846 ++#include <linux/vmalloc.h>
   1.847 ++#endif
   1.848 ++
   1.849 ++static void __init *large_malloc(size_t size)
   1.850 ++{
   1.851 ++	return vmalloc(size);
   1.852 ++}
   1.853 ++
   1.854 ++static void __init large_free(void *where)
   1.855 ++{
   1.856 ++	vfree(where);
   1.857 ++}
   1.858 ++
   1.859 ++#ifndef TEST
   1.860 ++static void __init *malloc(size_t size)
   1.861 ++{
   1.862 ++	return kmalloc(size, GFP_KERNEL);
   1.863 ++}
   1.864 ++
   1.865 ++static void __init free(void *where)
   1.866 ++{
   1.867 ++	kfree(where);
   1.868 ++}
   1.869 ++
   1.870 ++static void __init error(char *x)
   1.871 ++{
   1.872 ++	printk(KERN_ERR "%s\n", x);
   1.873 ++}
   1.874 ++#endif
   1.875 ++
   1.876 ++#define STATIC /**/
   1.877 ++
   1.878 ++#endif
   1.879 ++
   1.880 ++#include <linux/decompress_bunzip2.h>
   1.881 ++
   1.882 ++
   1.883 ++/* Constants for Huffman coding */
   1.884 ++#define MAX_GROUPS			6
   1.885 ++#define GROUP_SIZE   		50		/* 64 would have been more efficient */
   1.886 ++#define MAX_HUFCODE_BITS 	20		/* Longest Huffman code allowed */
   1.887 ++#define MAX_SYMBOLS 		258		/* 256 literals + RUNA + RUNB */
   1.888 ++#define SYMBOL_RUNA			0
   1.889 ++#define SYMBOL_RUNB			1
   1.890 ++
   1.891 ++/* Status return values */
   1.892 ++#define RETVAL_OK						0
   1.893 ++#define RETVAL_LAST_BLOCK				(-1)
   1.894 ++#define RETVAL_NOT_BZIP_DATA			(-2)
   1.895 ++#define RETVAL_UNEXPECTED_INPUT_EOF		(-3)
   1.896 ++#define RETVAL_UNEXPECTED_OUTPUT_EOF	(-4)
   1.897 ++#define RETVAL_DATA_ERROR				(-5)
   1.898 ++#define RETVAL_OUT_OF_MEMORY			(-6)
   1.899 ++#define RETVAL_OBSOLETE_INPUT			(-7)
   1.900 ++
   1.901 ++
   1.902 ++/* This is what we know about each Huffman coding group */
   1.903 ++struct group_data {
   1.904 ++	/* We have an extra slot at the end of limit[] for a sentinal value. */
   1.905 ++	int limit[MAX_HUFCODE_BITS+1],base[MAX_HUFCODE_BITS],permute[MAX_SYMBOLS];
   1.906 ++	int minLen, maxLen;
   1.907 ++};
   1.908 ++
   1.909 ++/* Structure holding all the housekeeping data, including IO buffers and
   1.910 ++   memory that persists between calls to bunzip */
   1.911 ++typedef struct {
   1.912 ++	/* State for interrupting output loop */
   1.913 ++	int writeCopies,writePos,writeRunCountdown,writeCount,writeCurrent;
   1.914 ++	/* I/O tracking data (file handles, buffers, positions, etc.) */
   1.915 ++	int (*fill)(void*,unsigned int);
   1.916 ++	int inbufCount,inbufPos /*,outbufPos*/;
   1.917 ++	unsigned char *inbuf /*,*outbuf*/;
   1.918 ++	unsigned int inbufBitCount, inbufBits;
   1.919 ++	/* The CRC values stored in the block header and calculated from the data */
   1.920 ++	unsigned int crc32Table[256],headerCRC, totalCRC, writeCRC;
   1.921 ++	/* Intermediate buffer and its size (in bytes) */
   1.922 ++	unsigned int *dbuf, dbufSize;
   1.923 ++	/* These things are a bit too big to go on the stack */
   1.924 ++	unsigned char selectors[32768];			/* nSelectors=15 bits */
   1.925 ++	struct group_data groups[MAX_GROUPS];	/* Huffman coding tables */
   1.926 ++	int io_error;			/* non-zero if we have IO error */
   1.927 ++} bunzip_data;
   1.928 ++	
   1.929 ++
   1.930 ++/* Return the next nnn bits of input.  All reads from the compressed input
   1.931 ++   are done through this function.  All reads are big endian */
   1.932 ++static unsigned int get_bits(bunzip_data *bd, char bits_wanted)
   1.933 ++{
   1.934 ++	unsigned int bits=0;
   1.935 ++
   1.936 ++	/* If we need to get more data from the byte buffer, do so.  (Loop getting
   1.937 ++	   one byte at a time to enforce endianness and avoid unaligned access.) */
   1.938 ++	while (bd->inbufBitCount<bits_wanted) {
   1.939 ++		/* If we need to read more data from file into byte buffer, do so */
   1.940 ++		if(bd->inbufPos==bd->inbufCount) {
   1.941 ++			if(bd->io_error)
   1.942 ++				return 0;
   1.943 ++			if((bd->inbufCount = bd->fill(bd->inbuf, BZIP2_IOBUF_SIZE)) <= 0) {
   1.944 ++				bd->io_error=RETVAL_UNEXPECTED_INPUT_EOF;
   1.945 ++				return 0;
   1.946 ++			}
   1.947 ++			bd->inbufPos=0;
   1.948 ++		}
   1.949 ++		/* Avoid 32-bit overflow (dump bit buffer to top of output) */
   1.950 ++		if(bd->inbufBitCount>=24) {
   1.951 ++			bits=bd->inbufBits&((1<<bd->inbufBitCount)-1);
   1.952 ++			bits_wanted-=bd->inbufBitCount;
   1.953 ++			bits<<=bits_wanted;
   1.954 ++			bd->inbufBitCount=0;
   1.955 ++		}
   1.956 ++		/* Grab next 8 bits of input from buffer. */
   1.957 ++		bd->inbufBits=(bd->inbufBits<<8)|bd->inbuf[bd->inbufPos++];
   1.958 ++		bd->inbufBitCount+=8;
   1.959 ++	}
   1.960 ++	/* Calculate result */
   1.961 ++	bd->inbufBitCount-=bits_wanted;
   1.962 ++	bits|=(bd->inbufBits>>bd->inbufBitCount)&((1<<bits_wanted)-1);
   1.963 ++
   1.964 ++	return bits;
   1.965 ++}
   1.966 ++
   1.967 ++/* Unpacks the next block and sets up for the inverse burrows-wheeler step. */
   1.968 ++
   1.969 ++static int get_next_block(bunzip_data *bd)
   1.970 ++{
   1.971 ++	struct group_data *hufGroup=NULL;
   1.972 ++	int *base=NULL;
   1.973 ++	int *limit=NULL;
   1.974 ++	int dbufCount,nextSym,dbufSize,groupCount,selector,
   1.975 ++		i,j,k,t,runPos,symCount,symTotal,nSelectors,byteCount[256];
   1.976 ++	unsigned char uc, symToByte[256], mtfSymbol[256], *selectors;
   1.977 ++	unsigned int *dbuf,origPtr;
   1.978 ++
   1.979 ++	dbuf=bd->dbuf;
   1.980 ++	dbufSize=bd->dbufSize;
   1.981 ++	selectors=bd->selectors;
   1.982 ++
   1.983 ++	/* Read in header signature and CRC, then validate signature.
   1.984 ++	   (last block signature means CRC is for whole file, return now) */
   1.985 ++	i = get_bits(bd,24);
   1.986 ++	j = get_bits(bd,24);
   1.987 ++	bd->headerCRC=get_bits(bd,32);
   1.988 ++	if ((i == 0x177245) && (j == 0x385090)) return RETVAL_LAST_BLOCK;
   1.989 ++	if ((i != 0x314159) || (j != 0x265359)) return RETVAL_NOT_BZIP_DATA;
   1.990 ++	/* We can add support for blockRandomised if anybody complains.  There was
   1.991 ++	   some code for this in busybox 1.0.0-pre3, but nobody ever noticed that
   1.992 ++	   it didn't actually work. */
   1.993 ++	if(get_bits(bd,1)) return RETVAL_OBSOLETE_INPUT;
   1.994 ++	if((origPtr=get_bits(bd,24)) > dbufSize) return RETVAL_DATA_ERROR;
   1.995 ++	/* mapping table: if some byte values are never used (encoding things
   1.996 ++	   like ascii text), the compression code removes the gaps to have fewer
   1.997 ++	   symbols to deal with, and writes a sparse bitfield indicating which
   1.998 ++	   values were present.  We make a translation table to convert the symbols
   1.999 ++	   back to the corresponding bytes. */
  1.1000 ++	t=get_bits(bd, 16);
  1.1001 ++	symTotal=0;
  1.1002 ++	for (i=0;i<16;i++) {
  1.1003 ++		if(t&(1<<(15-i))) {
  1.1004 ++			k=get_bits(bd,16);
  1.1005 ++			for(j=0;j<16;j++)
  1.1006 ++				if(k&(1<<(15-j))) symToByte[symTotal++]=(16*i)+j;
  1.1007 ++		}
  1.1008 ++	}
  1.1009 ++	/* How many different Huffman coding groups does this block use? */
  1.1010 ++	groupCount=get_bits(bd,3);
  1.1011 ++	if (groupCount<2 || groupCount>MAX_GROUPS) return RETVAL_DATA_ERROR;
  1.1012 ++	/* nSelectors: Every GROUP_SIZE many symbols we select a new Huffman coding
  1.1013 ++	   group.  Read in the group selector list, which is stored as MTF encoded
  1.1014 ++	   bit runs.  (MTF=Move To Front, as each value is used it's moved to the
  1.1015 ++	   start of the list.) */
  1.1016 ++	if(!(nSelectors=get_bits(bd, 15))) return RETVAL_DATA_ERROR;
  1.1017 ++	for(i=0; i<groupCount; i++) mtfSymbol[i] = i;
  1.1018 ++	for(i=0; i<nSelectors; i++) {
  1.1019 ++		/* Get next value */
  1.1020 ++		for(j=0;get_bits(bd,1);j++) if (j>=groupCount) return RETVAL_DATA_ERROR;
  1.1021 ++		/* Decode MTF to get the next selector */
  1.1022 ++		uc = mtfSymbol[j];
  1.1023 ++		for(;j;j--) mtfSymbol[j] = mtfSymbol[j-1];
  1.1024 ++		mtfSymbol[0]=selectors[i]=uc;
  1.1025 ++	}
  1.1026 ++	/* Read the Huffman coding tables for each group, which code for symTotal
  1.1027 ++	   literal symbols, plus two run symbols (RUNA, RUNB) */
  1.1028 ++	symCount=symTotal+2;
  1.1029 ++	for (j=0; j<groupCount; j++) {
  1.1030 ++		unsigned char length[MAX_SYMBOLS],temp[MAX_HUFCODE_BITS+1];
  1.1031 ++		int	minLen,	maxLen, pp;
  1.1032 ++		/* Read Huffman code lengths for each symbol.  They're stored in
  1.1033 ++		   a way similar to mtf; record a starting value for the first symbol,
  1.1034 ++		   and an offset from the previous value for everys symbol after that.
  1.1035 ++		   (Subtracting 1 before the loop and then adding it back at the end is
  1.1036 ++		   an optimization that makes the test inside the loop simpler: symbol
  1.1037 ++		   length 0 becomes negative, so an unsigned inequality catches it.) */
  1.1038 ++		t=get_bits(bd, 5)-1;
  1.1039 ++		for (i = 0; i < symCount; i++) {
  1.1040 ++			for(;;) {
  1.1041 ++				if (((unsigned)t) > (MAX_HUFCODE_BITS-1))
  1.1042 ++					return RETVAL_DATA_ERROR;
  1.1043 ++				/* If first bit is 0, stop.  Else second bit indicates whether
  1.1044 ++				   to increment or decrement the value.  Optimization: grab 2
  1.1045 ++				   bits and unget the second if the first was 0. */
  1.1046 ++				k = get_bits(bd,2);
  1.1047 ++				if (k < 2) {
  1.1048 ++					bd->inbufBitCount++;
  1.1049 ++					break;
  1.1050 ++				}
  1.1051 ++				/* Add one if second bit 1, else subtract 1.  Avoids if/else */
  1.1052 ++				t+=(((k+1)&2)-1);
  1.1053 ++			}
  1.1054 ++			/* Correct for the initial -1, to get the final symbol length */
  1.1055 ++			length[i]=t+1;
  1.1056 ++		}
  1.1057 ++		/* Find largest and smallest lengths in this group */
  1.1058 ++		minLen=maxLen=length[0];
  1.1059 ++		for(i = 1; i < symCount; i++) {
  1.1060 ++			if(length[i] > maxLen) maxLen = length[i];
  1.1061 ++			else if(length[i] < minLen) minLen = length[i];
  1.1062 ++		}
  1.1063 ++		/* Calculate permute[], base[], and limit[] tables from length[].
  1.1064 ++		 *
  1.1065 ++		 * permute[] is the lookup table for converting Huffman coded symbols
  1.1066 ++		 * into decoded symbols.  base[] is the amount to subtract from the
  1.1067 ++		 * value of a Huffman symbol of a given length when using permute[].
  1.1068 ++		 *
  1.1069 ++		 * limit[] indicates the largest numerical value a symbol with a given
  1.1070 ++		 * number of bits can have.  This is how the Huffman codes can vary in
  1.1071 ++		 * length: each code with a value>limit[length] needs another bit.
  1.1072 ++		 */
  1.1073 ++		hufGroup=bd->groups+j;
  1.1074 ++		hufGroup->minLen = minLen;
  1.1075 ++		hufGroup->maxLen = maxLen;
  1.1076 ++		/* Note that minLen can't be smaller than 1, so we adjust the base
  1.1077 ++		   and limit array pointers so we're not always wasting the first
  1.1078 ++		   entry.  We do this again when using them (during symbol decoding).*/
  1.1079 ++		base=hufGroup->base-1;
  1.1080 ++		limit=hufGroup->limit-1;
  1.1081 ++		/* Calculate permute[].  Concurently, initialize temp[] and limit[]. */
  1.1082 ++		pp=0;
  1.1083 ++		for(i=minLen;i<=maxLen;i++) {
  1.1084 ++			temp[i]=limit[i]=0;
  1.1085 ++			for(t=0;t<symCount;t++)
  1.1086 ++				if(length[t]==i) hufGroup->permute[pp++] = t;
  1.1087 ++		}
  1.1088 ++		/* Count symbols coded for at each bit length */
  1.1089 ++		for (i=0;i<symCount;i++) temp[length[i]]++;
  1.1090 ++		/* Calculate limit[] (the largest symbol-coding value at each bit
  1.1091 ++		 * length, which is (previous limit<<1)+symbols at this level), and
  1.1092 ++		 * base[] (number of symbols to ignore at each bit length, which is
  1.1093 ++		 * limit minus the cumulative count of symbols coded for already). */
  1.1094 ++		pp=t=0;
  1.1095 ++		for (i=minLen; i<maxLen; i++) {
  1.1096 ++			pp+=temp[i];
  1.1097 ++			/* We read the largest possible symbol size and then unget bits
  1.1098 ++			   after determining how many we need, and those extra bits could
  1.1099 ++			   be set to anything.  (They're noise from future symbols.)  At
  1.1100 ++			   each level we're really only interested in the first few bits,
  1.1101 ++			   so here we set all the trailing to-be-ignored bits to 1 so they
  1.1102 ++			   don't affect the value>limit[length] comparison. */
  1.1103 ++			limit[i]= (pp << (maxLen - i)) - 1;
  1.1104 ++			pp<<=1;
  1.1105 ++			base[i+1]=pp-(t+=temp[i]);
  1.1106 ++		}
  1.1107 ++		limit[maxLen+1] = INT_MAX; /* Sentinal value for reading next sym. */
  1.1108 ++		limit[maxLen]=pp+temp[maxLen]-1;
  1.1109 ++		base[minLen]=0;
  1.1110 ++	}
  1.1111 ++	/* We've finished reading and digesting the block header.  Now read this
  1.1112 ++	   block's Huffman coded symbols from the file and undo the Huffman coding
  1.1113 ++	   and run length encoding, saving the result into dbuf[dbufCount++]=uc */
  1.1114 ++
  1.1115 ++	/* Initialize symbol occurrence counters and symbol Move To Front table */
  1.1116 ++	for(i=0;i<256;i++) {
  1.1117 ++		byteCount[i] = 0;
  1.1118 ++		mtfSymbol[i]=(unsigned char)i;
  1.1119 ++	}
  1.1120 ++	/* Loop through compressed symbols. */
  1.1121 ++	runPos=dbufCount=symCount=selector=0;
  1.1122 ++	for(;;) {
  1.1123 ++		/* Determine which Huffman coding group to use. */
  1.1124 ++		if(!(symCount--)) {
  1.1125 ++			symCount=GROUP_SIZE-1;
  1.1126 ++			if(selector>=nSelectors) return RETVAL_DATA_ERROR;
  1.1127 ++			hufGroup=bd->groups+selectors[selector++];
  1.1128 ++			base=hufGroup->base-1;
  1.1129 ++			limit=hufGroup->limit-1;
  1.1130 ++		}
  1.1131 ++		/* Read next Huffman-coded symbol. */
  1.1132 ++		/* Note: It is far cheaper to read maxLen bits and back up than it is
  1.1133 ++		   to read minLen bits and then an additional bit at a time, testing
  1.1134 ++		   as we go.  Because there is a trailing last block (with file CRC),
  1.1135 ++		   there is no danger of the overread causing an unexpected EOF for a
  1.1136 ++		   valid compressed file.  As a further optimization, we do the read
  1.1137 ++		   inline (falling back to a call to get_bits if the buffer runs
  1.1138 ++		   dry).  The following (up to got_huff_bits:) is equivalent to
  1.1139 ++		   j=get_bits(bd,hufGroup->maxLen);
  1.1140 ++		 */
  1.1141 ++		while (bd->inbufBitCount<hufGroup->maxLen) {
  1.1142 ++			if(bd->inbufPos==bd->inbufCount) {
  1.1143 ++				j = get_bits(bd,hufGroup->maxLen);
  1.1144 ++				goto got_huff_bits;
  1.1145 ++			}
  1.1146 ++			bd->inbufBits=(bd->inbufBits<<8)|bd->inbuf[bd->inbufPos++];
  1.1147 ++			bd->inbufBitCount+=8;
  1.1148 ++		};
  1.1149 ++		bd->inbufBitCount-=hufGroup->maxLen;
  1.1150 ++		j = (bd->inbufBits>>bd->inbufBitCount)&((1<<hufGroup->maxLen)-1);
  1.1151 ++got_huff_bits:
  1.1152 ++		/* Figure how how many bits are in next symbol and unget extras */
  1.1153 ++		i=hufGroup->minLen;
  1.1154 ++		while(j>limit[i]) ++i;
  1.1155 ++		bd->inbufBitCount += (hufGroup->maxLen - i);
  1.1156 ++		/* Huffman decode value to get nextSym (with bounds checking) */
  1.1157 ++		if ((i > hufGroup->maxLen)
  1.1158 ++			|| (((unsigned)(j=(j>>(hufGroup->maxLen-i))-base[i]))
  1.1159 ++				>= MAX_SYMBOLS))
  1.1160 ++			return RETVAL_DATA_ERROR;
  1.1161 ++		nextSym = hufGroup->permute[j];
  1.1162 ++		/* We have now decoded the symbol, which indicates either a new literal
  1.1163 ++		   byte, or a repeated run of the most recent literal byte.  First,
  1.1164 ++		   check if nextSym indicates a repeated run, and if so loop collecting
  1.1165 ++		   how many times to repeat the last literal. */
  1.1166 ++		if (((unsigned)nextSym) <= SYMBOL_RUNB) { /* RUNA or RUNB */
  1.1167 ++			/* If this is the start of a new run, zero out counter */
  1.1168 ++			if(!runPos) {
  1.1169 ++				runPos = 1;
  1.1170 ++				t = 0;
  1.1171 ++			}
  1.1172 ++			/* Neat trick that saves 1 symbol: instead of or-ing 0 or 1 at
  1.1173 ++			   each bit position, add 1 or 2 instead.  For example,
  1.1174 ++			   1011 is 1<<0 + 1<<1 + 2<<2.  1010 is 2<<0 + 2<<1 + 1<<2.
  1.1175 ++			   You can make any bit pattern that way using 1 less symbol than
  1.1176 ++			   the basic or 0/1 method (except all bits 0, which would use no
  1.1177 ++			   symbols, but a run of length 0 doesn't mean anything in this
  1.1178 ++			   context).  Thus space is saved. */
  1.1179 ++			t += (runPos << nextSym); /* +runPos if RUNA; +2*runPos if RUNB */
  1.1180 ++			runPos <<= 1;
  1.1181 ++			continue;
  1.1182 ++		}
  1.1183 ++		/* When we hit the first non-run symbol after a run, we now know
  1.1184 ++		   how many times to repeat the last literal, so append that many
  1.1185 ++		   copies to our buffer of decoded symbols (dbuf) now.  (The last
  1.1186 ++		   literal used is the one at the head of the mtfSymbol array.) */
  1.1187 ++		if(runPos) {
  1.1188 ++			runPos=0;
  1.1189 ++			if(dbufCount+t>=dbufSize) return RETVAL_DATA_ERROR;
  1.1190 ++
  1.1191 ++			uc = symToByte[mtfSymbol[0]];
  1.1192 ++			byteCount[uc] += t;
  1.1193 ++			while(t--) dbuf[dbufCount++]=uc;
  1.1194 ++		}
  1.1195 ++		/* Is this the terminating symbol? */
  1.1196 ++		if(nextSym>symTotal) break;
  1.1197 ++		/* At this point, nextSym indicates a new literal character.  Subtract
  1.1198 ++		   one to get the position in the MTF array at which this literal is
  1.1199 ++		   currently to be found.  (Note that the result can't be -1 or 0,
  1.1200 ++		   because 0 and 1 are RUNA and RUNB.  But another instance of the
  1.1201 ++		   first symbol in the mtf array, position 0, would have been handled
  1.1202 ++		   as part of a run above.  Therefore 1 unused mtf position minus
  1.1203 ++		   2 non-literal nextSym values equals -1.) */
  1.1204 ++		if(dbufCount>=dbufSize) return RETVAL_DATA_ERROR;
  1.1205 ++		i = nextSym - 1;
  1.1206 ++		uc = mtfSymbol[i];
  1.1207 ++		/* Adjust the MTF array.  Since we typically expect to move only a
  1.1208 ++		 * small number of symbols, and are bound by 256 in any case, using
  1.1209 ++		 * memmove here would typically be bigger and slower due to function
  1.1210 ++		 * call overhead and other assorted setup costs. */
  1.1211 ++		do {
  1.1212 ++			mtfSymbol[i] = mtfSymbol[i-1];
  1.1213 ++		} while (--i);
  1.1214 ++		mtfSymbol[0] = uc;
  1.1215 ++		uc=symToByte[uc];
  1.1216 ++		/* We have our literal byte.  Save it into dbuf. */
  1.1217 ++		byteCount[uc]++;
  1.1218 ++		dbuf[dbufCount++] = (unsigned int)uc;
  1.1219 ++	}
  1.1220 ++	/* At this point, we've read all the Huffman-coded symbols (and repeated
  1.1221 ++       runs) for this block from the input stream, and decoded them into the
  1.1222 ++	   intermediate buffer.  There are dbufCount many decoded bytes in dbuf[].
  1.1223 ++	   Now undo the Burrows-Wheeler transform on dbuf.
  1.1224 ++	   See http://dogma.net/markn/articles/bwt/bwt.htm
  1.1225 ++	 */
  1.1226 ++	/* Turn byteCount into cumulative occurrence counts of 0 to n-1. */
  1.1227 ++	j=0;
  1.1228 ++	for(i=0;i<256;i++) {
  1.1229 ++		k=j+byteCount[i];
  1.1230 ++		byteCount[i] = j;
  1.1231 ++		j=k;
  1.1232 ++	}
  1.1233 ++	/* Figure out what order dbuf would be in if we sorted it. */
  1.1234 ++	for (i=0;i<dbufCount;i++) {
  1.1235 ++		uc=(unsigned char)(dbuf[i] & 0xff);
  1.1236 ++		dbuf[byteCount[uc]] |= (i << 8);
  1.1237 ++		byteCount[uc]++;
  1.1238 ++	}
  1.1239 ++	/* Decode first byte by hand to initialize "previous" byte.  Note that it
  1.1240 ++	   doesn't get output, and if the first three characters are identical
  1.1241 ++	   it doesn't qualify as a run (hence writeRunCountdown=5). */
  1.1242 ++	if(dbufCount) {
  1.1243 ++		if(origPtr>=dbufCount) return RETVAL_DATA_ERROR;
  1.1244 ++		bd->writePos=dbuf[origPtr];
  1.1245 ++	    bd->writeCurrent=(unsigned char)(bd->writePos&0xff);
  1.1246 ++		bd->writePos>>=8;
  1.1247 ++		bd->writeRunCountdown=5;
  1.1248 ++	}
  1.1249 ++	bd->writeCount=dbufCount;
  1.1250 ++
  1.1251 ++	return RETVAL_OK;
  1.1252 ++}
  1.1253 ++
  1.1254 ++/* Undo burrows-wheeler transform on intermediate buffer to produce output.
  1.1255 ++   If start_bunzip was initialized with out_fd=-1, then up to len bytes of
  1.1256 ++   data are written to outbuf.  Return value is number of bytes written or
  1.1257 ++   error (all errors are negative numbers).  If out_fd!=-1, outbuf and len
  1.1258 ++   are ignored, data is written to out_fd and return is RETVAL_OK or error.
  1.1259 ++*/
  1.1260 ++
  1.1261 ++static int read_bunzip(bunzip_data *bd, char *outbuf, int len)
  1.1262 ++{
  1.1263 ++	const unsigned int *dbuf;
  1.1264 ++	int pos,xcurrent,previous,gotcount;
  1.1265 ++
  1.1266 ++	/* If last read was short due to end of file, return last block now */
  1.1267 ++	if(bd->writeCount<0) return bd->writeCount;
  1.1268 ++
  1.1269 ++	gotcount = 0;
  1.1270 ++	dbuf=bd->dbuf;
  1.1271 ++	pos=bd->writePos;
  1.1272 ++	xcurrent=bd->writeCurrent;
  1.1273 ++
  1.1274 ++	/* We will always have pending decoded data to write into the output
  1.1275 ++	   buffer unless this is the very first call (in which case we haven't
  1.1276 ++	   Huffman-decoded a block into the intermediate buffer yet). */
  1.1277 ++
  1.1278 ++	if (bd->writeCopies) {
  1.1279 ++		/* Inside the loop, writeCopies means extra copies (beyond 1) */
  1.1280 ++		--bd->writeCopies;
  1.1281 ++		/* Loop outputting bytes */
  1.1282 ++		for(;;) {
  1.1283 ++			/* If the output buffer is full, snapshot state and return */
  1.1284 ++			if(gotcount >= len) {
  1.1285 ++				bd->writePos=pos;
  1.1286 ++				bd->writeCurrent=xcurrent;
  1.1287 ++				bd->writeCopies++;
  1.1288 ++				return len;
  1.1289 ++			}
  1.1290 ++			/* Write next byte into output buffer, updating CRC */
  1.1291 ++			outbuf[gotcount++] = xcurrent;
  1.1292 ++			bd->writeCRC=(((bd->writeCRC)<<8)
  1.1293 ++						  ^bd->crc32Table[((bd->writeCRC)>>24)^xcurrent]);
  1.1294 ++			/* Loop now if we're outputting multiple copies of this byte */
  1.1295 ++			if (bd->writeCopies) {
  1.1296 ++				--bd->writeCopies;
  1.1297 ++				continue;
  1.1298 ++			}
  1.1299 ++decode_next_byte:
  1.1300 ++			if (!bd->writeCount--) break;
  1.1301 ++			/* Follow sequence vector to undo Burrows-Wheeler transform */
  1.1302 ++			previous=xcurrent;
  1.1303 ++			pos=dbuf[pos];
  1.1304 ++			xcurrent=pos&0xff;
  1.1305 ++			pos>>=8;
  1.1306 ++			/* After 3 consecutive copies of the same byte, the 4th is a repeat
  1.1307 ++			   count.  We count down from 4 instead
  1.1308 ++			 * of counting up because testing for non-zero is faster */
  1.1309 ++			if(--bd->writeRunCountdown) {
  1.1310 ++				if(xcurrent!=previous) bd->writeRunCountdown=4;
  1.1311 ++			} else {
  1.1312 ++				/* We have a repeated run, this byte indicates the count */
  1.1313 ++				bd->writeCopies=xcurrent;
  1.1314 ++				xcurrent=previous;
  1.1315 ++				bd->writeRunCountdown=5;
  1.1316 ++				/* Sometimes there are just 3 bytes (run length 0) */
  1.1317 ++				if(!bd->writeCopies) goto decode_next_byte;
  1.1318 ++				/* Subtract the 1 copy we'd output anyway to get extras */
  1.1319 ++				--bd->writeCopies;
  1.1320 ++			}
  1.1321 ++		}
  1.1322 ++		/* Decompression of this block completed successfully */
  1.1323 ++		bd->writeCRC=~bd->writeCRC;
  1.1324 ++		bd->totalCRC=((bd->totalCRC<<1) | (bd->totalCRC>>31)) ^ bd->writeCRC;
  1.1325 ++		/* If this block had a CRC error, force file level CRC error. */
  1.1326 ++		if(bd->writeCRC!=bd->headerCRC) {
  1.1327 ++			bd->totalCRC=bd->headerCRC+1;
  1.1328 ++			return RETVAL_LAST_BLOCK;
  1.1329 ++		}
  1.1330 ++	}
  1.1331 ++
  1.1332 ++	/* Refill the intermediate buffer by Huffman-decoding next block of input */
  1.1333 ++	/* (previous is just a convenient unused temp variable here) */
  1.1334 ++	previous=get_next_block(bd);
  1.1335 ++	if(previous) {
  1.1336 ++		bd->writeCount=previous;
  1.1337 ++		return (previous!=RETVAL_LAST_BLOCK) ? previous : gotcount;
  1.1338 ++	}
  1.1339 ++	bd->writeCRC=0xffffffffUL;
  1.1340 ++	pos=bd->writePos;
  1.1341 ++	xcurrent=bd->writeCurrent;
  1.1342 ++	goto decode_next_byte;
  1.1343 ++}
  1.1344 ++
  1.1345 ++static int nofill(void *buf,unsigned int len) {
  1.1346 ++	return -1;
  1.1347 ++}
  1.1348 ++
  1.1349 ++/* Allocate the structure, read file header.  If in_fd==-1, inbuf must contain
  1.1350 ++   a complete bunzip file (len bytes long).  If in_fd!=-1, inbuf and len are
  1.1351 ++   ignored, and data is read from file handle into temporary buffer. */
  1.1352 ++static int start_bunzip(bunzip_data **bdp, void *inbuf, int len,
  1.1353 ++			int (*fill)(void*,unsigned int))
  1.1354 ++{
  1.1355 ++	bunzip_data *bd;
  1.1356 ++	unsigned int i,j,c;
  1.1357 ++	const unsigned int BZh0=(((unsigned int)'B')<<24)+(((unsigned int)'Z')<<16)
  1.1358 ++							+(((unsigned int)'h')<<8)+(unsigned int)'0';
  1.1359 ++
  1.1360 ++	/* Figure out how much data to allocate */
  1.1361 ++	i=sizeof(bunzip_data);
  1.1362 ++
  1.1363 ++	/* Allocate bunzip_data.  Most fields initialize to zero. */
  1.1364 ++	bd=*bdp=malloc(i);
  1.1365 ++	memset(bd,0,sizeof(bunzip_data));
  1.1366 ++	/* Setup input buffer */
  1.1367 ++	bd->inbuf=inbuf;
  1.1368 ++	bd->inbufCount=len;
  1.1369 ++	if(fill != NULL)
  1.1370 ++		bd->fill=fill;
  1.1371 ++	else
  1.1372 ++		bd->fill=nofill;
  1.1373 ++
  1.1374 ++	/* Init the CRC32 table (big endian) */
  1.1375 ++	for(i=0;i<256;i++) {
  1.1376 ++		c=i<<24;
  1.1377 ++		for(j=8;j;j--)
  1.1378 ++			c=c&0x80000000 ? (c<<1)^0x04c11db7 : (c<<1);
  1.1379 ++		bd->crc32Table[i]=c;
  1.1380 ++	}
  1.1381 ++
  1.1382 ++	/* Ensure that file starts with "BZh['1'-'9']." */
  1.1383 ++	i = get_bits(bd,32);
  1.1384 ++	if (((unsigned int)(i-BZh0-1)) >= 9) return RETVAL_NOT_BZIP_DATA;
  1.1385 ++
  1.1386 ++	/* Fourth byte (ascii '1'-'9'), indicates block size in units of 100k of
  1.1387 ++	   uncompressed data.  Allocate intermediate buffer for block. */
  1.1388 ++	bd->dbufSize=100000*(i-BZh0);
  1.1389 ++
  1.1390 ++	bd->dbuf=large_malloc(bd->dbufSize * sizeof(int));
  1.1391 ++	return RETVAL_OK;
  1.1392 ++}
  1.1393 ++
  1.1394 ++/* Example usage: decompress src_fd to dst_fd.  (Stops at end of bzip data,
  1.1395 ++   not end of file.) */
  1.1396 ++STATIC int bunzip2(char *inbuf, int len, 
  1.1397 ++		   int(*fill)(void*,unsigned int),
  1.1398 ++		   int(*writebb)(char*,unsigned int),
  1.1399 ++		   int *pos)
  1.1400 ++{
  1.1401 ++	char *outbuf;
  1.1402 ++	bunzip_data *bd;
  1.1403 ++	int i;
  1.1404 ++
  1.1405 ++	outbuf=malloc(BZIP2_IOBUF_SIZE);
  1.1406 ++	if(!(i=start_bunzip(&bd,inbuf,len,fill))) {
  1.1407 ++		for(;;) {
  1.1408 ++			if((i=read_bunzip(bd,outbuf,BZIP2_IOBUF_SIZE)) <= 0) break;
  1.1409 ++			if(i!=writebb(outbuf,i)) {
  1.1410 ++				i=RETVAL_UNEXPECTED_OUTPUT_EOF;
  1.1411 ++				break;
  1.1412 ++			}
  1.1413 ++		}
  1.1414 ++	}
  1.1415 ++	/* Check CRC and release memory */
  1.1416 ++	if(i==RETVAL_LAST_BLOCK) {
  1.1417 ++		if (bd->headerCRC!=bd->totalCRC) {
  1.1418 ++			error("Data integrity error when decompressing.");
  1.1419 ++		} else {
  1.1420 ++			i=RETVAL_OK;
  1.1421 ++		}
  1.1422 ++	}
  1.1423 ++	else if (i==RETVAL_UNEXPECTED_OUTPUT_EOF) {
  1.1424 ++		error("Compressed file ends unexpectedly");
  1.1425 ++	}
  1.1426 ++	if(bd->dbuf) large_free(bd->dbuf);
  1.1427 ++	if(pos)
  1.1428 ++		*pos = bd->inbufPos;
  1.1429 ++	free(bd);
  1.1430 ++	free(outbuf);
  1.1431 ++
  1.1432 ++	return i;
  1.1433 ++}
  1.1434 ++
  1.1435 +
  1.1436 +--- linux-2.6.22.9/lib/decompress_unlzma.c
  1.1437 ++++ linux-2.6.22.9/lib/decompress_unlzma.c
  1.1438 +@@ -0,0 +1,605 @@
  1.1439 ++/* Lzma decompressor for Linux kernel. Shamelessly snarfed
  1.1440 ++ * from busybox 1.1.1
  1.1441 ++ *
  1.1442 ++ * Linux kernel adaptation
  1.1443 ++ * Copyright (C) 2006  Alain <alain@knaff.lu>
  1.1444 ++ *
  1.1445 ++ * Based on small lzma deflate implementation/Small range coder 
  1.1446 ++ * implementation for lzma.
  1.1447 ++ * Copyright (C) 2006  Aurelien Jacobs <aurel@gnuage.org>
  1.1448 ++ *
  1.1449 ++ * Based on LzmaDecode.c from the LZMA SDK 4.22 (http://www.7-zip.org/)
  1.1450 ++ * Copyright (C) 1999-2005  Igor Pavlov
  1.1451 ++ *
  1.1452 ++ * Copyrights of the parts, see headers below.
  1.1453 ++ *
  1.1454 ++ *
  1.1455 ++ * This program is free software; you can redistribute it and/or
  1.1456 ++ * modify it under the terms of the GNU Lesser General Public
  1.1457 ++ * License as published by the Free Software Foundation; either
  1.1458 ++ * version 2.1 of the License, or (at your option) any later version.
  1.1459 ++ *
  1.1460 ++ * This program is distributed in the hope that it will be useful,
  1.1461 ++ * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1.1462 ++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  1.1463 ++ * Lesser General Public License for more details.
  1.1464 ++ *
  1.1465 ++ * You should have received a copy of the GNU Lesser General Public
  1.1466 ++ * License along with this library; if not, write to the Free Software
  1.1467 ++ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  1.1468 ++ */
  1.1469 ++
  1.1470 ++#ifndef STATIC
  1.1471 ++
  1.1472 ++#include <linux/kernel.h>
  1.1473 ++#include <linux/fs.h>
  1.1474 ++#include <linux/string.h>
  1.1475 ++
  1.1476 ++#ifdef TEST
  1.1477 ++#include "test.h"
  1.1478 ++#else
  1.1479 ++#include <linux/vmalloc.h>
  1.1480 ++#endif
  1.1481 ++
  1.1482 ++static void __init *large_malloc(size_t size)
  1.1483 ++{
  1.1484 ++	return vmalloc(size);
  1.1485 ++}
  1.1486 ++
  1.1487 ++static void __init large_free(void *where)
  1.1488 ++{
  1.1489 ++	vfree(where);
  1.1490 ++}
  1.1491 ++
  1.1492 ++#ifndef TEST
  1.1493 ++static void __init *malloc(size_t size)
  1.1494 ++{
  1.1495 ++	return kmalloc(size, GFP_KERNEL);
  1.1496 ++}
  1.1497 ++
  1.1498 ++static void __init free(void *where)
  1.1499 ++{
  1.1500 ++	kfree(where);
  1.1501 ++}
  1.1502 ++
  1.1503 ++static void __init error(char *x)
  1.1504 ++{
  1.1505 ++	printk(KERN_ERR "%s\n", x);
  1.1506 ++}
  1.1507 ++
  1.1508 ++#endif
  1.1509 ++
  1.1510 ++#define STATIC /**/
  1.1511 ++
  1.1512 ++#endif
  1.1513 ++
  1.1514 ++#include <linux/decompress_unlzma.h>
  1.1515 ++
  1.1516 ++#define	MIN(a,b) (((a)<(b))?(a):(b))
  1.1517 ++
  1.1518 ++static long long read_int(unsigned char *ptr, int size)
  1.1519 ++{
  1.1520 ++	int i;
  1.1521 ++	long long ret=0;
  1.1522 ++
  1.1523 ++	for(i=0; i<size; i++) {
  1.1524 ++		ret = (ret << 8) | ptr[size-i-1];
  1.1525 ++	}
  1.1526 ++	return ret;
  1.1527 ++}
  1.1528 ++
  1.1529 ++#define ENDIAN_CONVERT(x) x=(typeof(x))read_int((unsigned char*)&x,sizeof(x))
  1.1530 ++
  1.1531 ++
  1.1532 ++/* Small range coder implementation for lzma.
  1.1533 ++ * Copyright (C) 2006  Aurelien Jacobs <aurel@gnuage.org>
  1.1534 ++ *
  1.1535 ++ * Based on LzmaDecode.c from the LZMA SDK 4.22 (http://www.7-zip.org/)
  1.1536 ++ * Copyright (c) 1999-2005  Igor Pavlov
  1.1537 ++ */
  1.1538 ++
  1.1539 ++#ifndef always_inline
  1.1540 ++#  if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >0)
  1.1541 ++#    define always_inline __attribute__((always_inline)) inline
  1.1542 ++#  else
  1.1543 ++#    define always_inline inline
  1.1544 ++#  endif
  1.1545 ++#endif
  1.1546 ++
  1.1547 ++#ifdef CONFIG_FEATURE_LZMA_FAST
  1.1548 ++#  define speed_inline always_inline
  1.1549 ++#else
  1.1550 ++#  define speed_inline
  1.1551 ++#endif
  1.1552 ++
  1.1553 ++
  1.1554 ++typedef struct {
  1.1555 ++	int (*fill)(void*,unsigned int);
  1.1556 ++	uint8_t *ptr;
  1.1557 ++	uint8_t *buffer;
  1.1558 ++	uint8_t *buffer_end;
  1.1559 ++	int buffer_size;
  1.1560 ++	uint32_t code;
  1.1561 ++	uint32_t range;
  1.1562 ++	uint32_t bound;
  1.1563 ++} rc_t;
  1.1564 ++
  1.1565 ++
  1.1566 ++#define RC_TOP_BITS 24
  1.1567 ++#define RC_MOVE_BITS 5
  1.1568 ++#define RC_MODEL_TOTAL_BITS 11
  1.1569 ++
  1.1570 ++
  1.1571 ++/* Called twice: once at startup and once in rc_normalize() */
  1.1572 ++static void rc_read(rc_t * rc)
  1.1573 ++{
  1.1574 ++	rc->buffer_size = rc->fill((char*)rc->buffer, LZMA_IOBUF_SIZE);
  1.1575 ++	if (rc->buffer_size <= 0)
  1.1576 ++		error("unexpected EOF");
  1.1577 ++	rc->ptr = rc->buffer;
  1.1578 ++	rc->buffer_end = rc->buffer + rc->buffer_size;
  1.1579 ++}
  1.1580 ++
  1.1581 ++/* Called once */
  1.1582 ++static always_inline void rc_init(rc_t * rc, int (*fill)(void*,unsigned int),
  1.1583 ++				  char *buffer, int buffer_size)
  1.1584 ++{
  1.1585 ++	rc->fill = fill;
  1.1586 ++	rc->buffer = (uint8_t *)buffer;
  1.1587 ++	rc->buffer_size = buffer_size;
  1.1588 ++	rc->buffer_end = rc->buffer + rc->buffer_size;
  1.1589 ++	rc->ptr = rc->buffer;
  1.1590 ++
  1.1591 ++	rc->code = 0;
  1.1592 ++	rc->range = 0xFFFFFFFF;
  1.1593 ++}
  1.1594 ++
  1.1595 ++static always_inline void rc_init_code(rc_t * rc)
  1.1596 ++{
  1.1597 ++	int i;
  1.1598 ++
  1.1599 ++	for (i = 0; i < 5; i++) {
  1.1600 ++		if (rc->ptr >= rc->buffer_end)
  1.1601 ++			rc_read(rc);
  1.1602 ++		rc->code = (rc->code << 8) | *rc->ptr++;
  1.1603 ++	}
  1.1604 ++}
  1.1605 ++
  1.1606 ++
  1.1607 ++/* Called once. TODO: bb_maybe_free() */
  1.1608 ++static always_inline void rc_free(rc_t * rc)
  1.1609 ++{
  1.1610 ++	free(rc->buffer);
  1.1611 ++}
  1.1612 ++
  1.1613 ++/* Called twice, but one callsite is in speed_inline'd rc_is_bit_0_helper() */
  1.1614 ++static void rc_do_normalize(rc_t * rc)
  1.1615 ++{
  1.1616 ++	if (rc->ptr >= rc->buffer_end)
  1.1617 ++		rc_read(rc);
  1.1618 ++	rc->range <<= 8;
  1.1619 ++	rc->code = (rc->code << 8) | *rc->ptr++;
  1.1620 ++}
  1.1621 ++static always_inline void rc_normalize(rc_t * rc)
  1.1622 ++{
  1.1623 ++	if (rc->range < (1 << RC_TOP_BITS)) {
  1.1624 ++		rc_do_normalize(rc);
  1.1625 ++	}
  1.1626 ++}
  1.1627 ++
  1.1628 ++/* Called 9 times */
  1.1629 ++/* Why rc_is_bit_0_helper exists?
  1.1630 ++ * Because we want to always expose (rc->code < rc->bound) to optimizer
  1.1631 ++ */
  1.1632 ++static speed_inline uint32_t rc_is_bit_0_helper(rc_t * rc, uint16_t * p)
  1.1633 ++{
  1.1634 ++	rc_normalize(rc);
  1.1635 ++	rc->bound = *p * (rc->range >> RC_MODEL_TOTAL_BITS);
  1.1636 ++	return rc->bound;
  1.1637 ++}
  1.1638 ++static always_inline int rc_is_bit_0(rc_t * rc, uint16_t * p)
  1.1639 ++{
  1.1640 ++	uint32_t t = rc_is_bit_0_helper(rc, p);
  1.1641 ++	return rc->code < t;
  1.1642 ++}
  1.1643 ++
  1.1644 ++/* Called ~10 times, but very small, thus inlined */
  1.1645 ++static speed_inline void rc_update_bit_0(rc_t * rc, uint16_t * p)
  1.1646 ++{
  1.1647 ++	rc->range = rc->bound;
  1.1648 ++	*p += ((1 << RC_MODEL_TOTAL_BITS) - *p) >> RC_MOVE_BITS;
  1.1649 ++}
  1.1650 ++static speed_inline void rc_update_bit_1(rc_t * rc, uint16_t * p)
  1.1651 ++{
  1.1652 ++	rc->range -= rc->bound;
  1.1653 ++	rc->code -= rc->bound;
  1.1654 ++	*p -= *p >> RC_MOVE_BITS;
  1.1655 ++}
  1.1656 ++
  1.1657 ++/* Called 4 times in unlzma loop */
  1.1658 ++static int rc_get_bit(rc_t * rc, uint16_t * p, int *symbol)
  1.1659 ++{
  1.1660 ++	if (rc_is_bit_0(rc, p)) {
  1.1661 ++		rc_update_bit_0(rc, p);
  1.1662 ++		*symbol *= 2;
  1.1663 ++		return 0;
  1.1664 ++	} else {
  1.1665 ++		rc_update_bit_1(rc, p);
  1.1666 ++		*symbol = *symbol * 2 + 1;
  1.1667 ++		return 1;
  1.1668 ++	}
  1.1669 ++}
  1.1670 ++
  1.1671 ++/* Called once */
  1.1672 ++static always_inline int rc_direct_bit(rc_t * rc)
  1.1673 ++{
  1.1674 ++	rc_normalize(rc);
  1.1675 ++	rc->range >>= 1;
  1.1676 ++	if (rc->code >= rc->range) {
  1.1677 ++		rc->code -= rc->range;
  1.1678 ++		return 1;
  1.1679 ++	}
  1.1680 ++	return 0;
  1.1681 ++}
  1.1682 ++
  1.1683 ++/* Called twice */
  1.1684 ++static speed_inline void
  1.1685 ++rc_bit_tree_decode(rc_t * rc, uint16_t * p, int num_levels, int *symbol)
  1.1686 ++{
  1.1687 ++	int i = num_levels;
  1.1688 ++
  1.1689 ++	*symbol = 1;
  1.1690 ++	while (i--)
  1.1691 ++		rc_get_bit(rc, p + *symbol, symbol);
  1.1692 ++	*symbol -= 1 << num_levels;
  1.1693 ++}
  1.1694 ++
  1.1695 ++
  1.1696 ++/*
  1.1697 ++ * Small lzma deflate implementation.
  1.1698 ++ * Copyright (C) 2006  Aurelien Jacobs <aurel@gnuage.org>
  1.1699 ++ *
  1.1700 ++ * Based on LzmaDecode.c from the LZMA SDK 4.22 (http://www.7-zip.org/)
  1.1701 ++ * Copyright (C) 1999-2005  Igor Pavlov
  1.1702 ++ */
  1.1703 ++
  1.1704 ++
  1.1705 ++typedef struct {
  1.1706 ++	uint8_t pos;
  1.1707 ++	uint32_t dict_size;
  1.1708 ++	uint64_t dst_size;
  1.1709 ++} __attribute__ ((packed)) lzma_header_t;
  1.1710 ++
  1.1711 ++
  1.1712 ++#define LZMA_BASE_SIZE 1846
  1.1713 ++#define LZMA_LIT_SIZE 768
  1.1714 ++
  1.1715 ++#define LZMA_NUM_POS_BITS_MAX 4
  1.1716 ++
  1.1717 ++#define LZMA_LEN_NUM_LOW_BITS 3
  1.1718 ++#define LZMA_LEN_NUM_MID_BITS 3
  1.1719 ++#define LZMA_LEN_NUM_HIGH_BITS 8
  1.1720 ++
  1.1721 ++#define LZMA_LEN_CHOICE 0
  1.1722 ++#define LZMA_LEN_CHOICE_2 (LZMA_LEN_CHOICE + 1)
  1.1723 ++#define LZMA_LEN_LOW (LZMA_LEN_CHOICE_2 + 1)
  1.1724 ++#define LZMA_LEN_MID (LZMA_LEN_LOW \
  1.1725 ++		      + (1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_LOW_BITS)))
  1.1726 ++#define LZMA_LEN_HIGH (LZMA_LEN_MID \
  1.1727 ++		       +(1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_MID_BITS)))
  1.1728 ++#define LZMA_NUM_LEN_PROBS (LZMA_LEN_HIGH + (1 << LZMA_LEN_NUM_HIGH_BITS))
  1.1729 ++
  1.1730 ++#define LZMA_NUM_STATES 12
  1.1731 ++#define LZMA_NUM_LIT_STATES 7
  1.1732 ++
  1.1733 ++#define LZMA_START_POS_MODEL_INDEX 4
  1.1734 ++#define LZMA_END_POS_MODEL_INDEX 14
  1.1735 ++#define LZMA_NUM_FULL_DISTANCES (1 << (LZMA_END_POS_MODEL_INDEX >> 1))
  1.1736 ++
  1.1737 ++#define LZMA_NUM_POS_SLOT_BITS 6
  1.1738 ++#define LZMA_NUM_LEN_TO_POS_STATES 4
  1.1739 ++
  1.1740 ++#define LZMA_NUM_ALIGN_BITS 4
  1.1741 ++
  1.1742 ++#define LZMA_MATCH_MIN_LEN 2
  1.1743 ++
  1.1744 ++#define LZMA_IS_MATCH 0
  1.1745 ++#define LZMA_IS_REP (LZMA_IS_MATCH + (LZMA_NUM_STATES <<LZMA_NUM_POS_BITS_MAX))
  1.1746 ++#define LZMA_IS_REP_G0 (LZMA_IS_REP + LZMA_NUM_STATES)
  1.1747 ++#define LZMA_IS_REP_G1 (LZMA_IS_REP_G0 + LZMA_NUM_STATES)
  1.1748 ++#define LZMA_IS_REP_G2 (LZMA_IS_REP_G1 + LZMA_NUM_STATES)
  1.1749 ++#define LZMA_IS_REP_0_LONG (LZMA_IS_REP_G2 + LZMA_NUM_STATES)
  1.1750 ++#define LZMA_POS_SLOT (LZMA_IS_REP_0_LONG \
  1.1751 ++		       + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX))
  1.1752 ++#define LZMA_SPEC_POS (LZMA_POS_SLOT \
  1.1753 ++		       +(LZMA_NUM_LEN_TO_POS_STATES << LZMA_NUM_POS_SLOT_BITS))
  1.1754 ++#define LZMA_ALIGN (LZMA_SPEC_POS \
  1.1755 ++		    + LZMA_NUM_FULL_DISTANCES - LZMA_END_POS_MODEL_INDEX)
  1.1756 ++#define LZMA_LEN_CODER (LZMA_ALIGN + (1 << LZMA_NUM_ALIGN_BITS))
  1.1757 ++#define LZMA_REP_LEN_CODER (LZMA_LEN_CODER + LZMA_NUM_LEN_PROBS)
  1.1758 ++#define LZMA_LITERAL (LZMA_REP_LEN_CODER + LZMA_NUM_LEN_PROBS)
  1.1759 ++
  1.1760 ++
  1.1761 ++STATIC int unlzma(char *inbuf, int in_len, 
  1.1762 ++		   int(*fill)(void*,unsigned int),
  1.1763 ++		   int(*writebb)(char*,unsigned int),
  1.1764 ++		   int *posp)
  1.1765 ++{
  1.1766 ++	lzma_header_t header;
  1.1767 ++	int lc, pb, lp;
  1.1768 ++	uint32_t pos_state_mask;
  1.1769 ++	uint32_t literal_pos_mask;
  1.1770 ++	uint32_t pos;
  1.1771 ++	uint16_t *p;
  1.1772 ++	uint16_t *prob;
  1.1773 ++	uint16_t *prob_lit;
  1.1774 ++	int num_bits;
  1.1775 ++	int num_probs;
  1.1776 ++	rc_t rc;
  1.1777 ++	int i, mi;
  1.1778 ++	uint8_t *buffer;
  1.1779 ++	uint8_t previous_byte = 0;
  1.1780 ++	size_t buffer_pos = 0, global_pos = 0;
  1.1781 ++	int len = 0;
  1.1782 ++	int state = 0;
  1.1783 ++	int bufsize;
  1.1784 ++	uint32_t rep0 = 1, rep1 = 1, rep2 = 1, rep3 = 1;
  1.1785 ++
  1.1786 ++	rc_init(&rc, fill, inbuf, in_len);
  1.1787 ++
  1.1788 ++	for (i = 0; i < sizeof(header); i++) {
  1.1789 ++		if (rc.ptr >= rc.buffer_end)
  1.1790 ++			rc_read(&rc);
  1.1791 ++		((unsigned char *)&header)[i] = *rc.ptr++;
  1.1792 ++	}
  1.1793 ++
  1.1794 ++	if (header.pos >= (9 * 5 * 5))
  1.1795 ++		error("bad header");
  1.1796 ++
  1.1797 ++	mi = header.pos / 9;
  1.1798 ++	lc = header.pos % 9;
  1.1799 ++	pb = mi / 5;
  1.1800 ++	lp = mi % 5;
  1.1801 ++	pos_state_mask = (1 << pb) - 1;
  1.1802 ++	literal_pos_mask = (1 << lp) - 1;
  1.1803 ++
  1.1804 ++	ENDIAN_CONVERT(header.dict_size);
  1.1805 ++	ENDIAN_CONVERT(header.dst_size);
  1.1806 ++
  1.1807 ++	if (header.dict_size == 0)
  1.1808 ++		header.dict_size = 1;
  1.1809 ++
  1.1810 ++	bufsize = MIN(header.dst_size, header.dict_size);
  1.1811 ++	buffer = large_malloc(bufsize);
  1.1812 ++	if(buffer == NULL)
  1.1813 ++		return -1;
  1.1814 ++
  1.1815 ++	num_probs = LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp));
  1.1816 ++	p = large_malloc(num_probs * sizeof(*p));
  1.1817 ++	num_probs = LZMA_LITERAL + (LZMA_LIT_SIZE << (lc + lp));
  1.1818 ++	for (i = 0; i < num_probs; i++)
  1.1819 ++		p[i] = (1 << RC_MODEL_TOTAL_BITS) >> 1;
  1.1820 ++
  1.1821 ++	rc_init_code(&rc);
  1.1822 ++
  1.1823 ++	while (global_pos + buffer_pos < header.dst_size) {
  1.1824 ++		int pos_state = (buffer_pos + global_pos) & pos_state_mask;
  1.1825 ++
  1.1826 ++		prob =
  1.1827 ++			p + LZMA_IS_MATCH + (state << LZMA_NUM_POS_BITS_MAX) + pos_state;
  1.1828 ++		if (rc_is_bit_0(&rc, prob)) {
  1.1829 ++			mi = 1;
  1.1830 ++			rc_update_bit_0(&rc, prob);
  1.1831 ++			prob = (p + LZMA_LITERAL + (LZMA_LIT_SIZE
  1.1832 ++					* ((((buffer_pos + global_pos) & literal_pos_mask) << lc)
  1.1833 ++					+ (previous_byte >> (8 - lc)))));
  1.1834 ++
  1.1835 ++			if (state >= LZMA_NUM_LIT_STATES) {
  1.1836 ++				int match_byte;
  1.1837 ++
  1.1838 ++				pos = buffer_pos - rep0;
  1.1839 ++				while (pos >= header.dict_size)
  1.1840 ++					pos += header.dict_size;
  1.1841 ++				if(pos >= bufsize) {
  1.1842 ++					goto fail;
  1.1843 ++				}
  1.1844 ++				match_byte = buffer[pos];
  1.1845 ++				do {
  1.1846 ++					int bit;
  1.1847 ++
  1.1848 ++					match_byte <<= 1;
  1.1849 ++					bit = match_byte & 0x100;
  1.1850 ++					prob_lit = prob + 0x100 + bit + mi;
  1.1851 ++					if (rc_get_bit(&rc, prob_lit, &mi)) {
  1.1852 ++						if (!bit)
  1.1853 ++							break;
  1.1854 ++					} else {
  1.1855 ++						if (bit)
  1.1856 ++							break;
  1.1857 ++					}
  1.1858 ++				} while (mi < 0x100);
  1.1859 ++			}
  1.1860 ++			while (mi < 0x100) {
  1.1861 ++				prob_lit = prob + mi;
  1.1862 ++				rc_get_bit(&rc, prob_lit, &mi);
  1.1863 ++			}
  1.1864 ++			previous_byte = (uint8_t) mi;
  1.1865 ++
  1.1866 ++			buffer[buffer_pos++] = previous_byte;
  1.1867 ++			if (buffer_pos == header.dict_size) {
  1.1868 ++				buffer_pos = 0;
  1.1869 ++				global_pos += header.dict_size;
  1.1870 ++				writebb((char*)buffer, header.dict_size);
  1.1871 ++			}
  1.1872 ++			if (state < 4)
  1.1873 ++				state = 0;
  1.1874 ++			else if (state < 10)
  1.1875 ++				state -= 3;
  1.1876 ++			else
  1.1877 ++				state -= 6;
  1.1878 ++		} else {
  1.1879 ++			int offset;
  1.1880 ++			uint16_t *prob_len;
  1.1881 ++
  1.1882 ++			rc_update_bit_1(&rc, prob);
  1.1883 ++			prob = p + LZMA_IS_REP + state;
  1.1884 ++			if (rc_is_bit_0(&rc, prob)) {
  1.1885 ++				rc_update_bit_0(&rc, prob);
  1.1886 ++				rep3 = rep2;
  1.1887 ++				rep2 = rep1;
  1.1888 ++				rep1 = rep0;
  1.1889 ++				state = state < LZMA_NUM_LIT_STATES ? 0 : 3;
  1.1890 ++				prob = p + LZMA_LEN_CODER;
  1.1891 ++			} else {
  1.1892 ++				rc_update_bit_1(&rc, prob);
  1.1893 ++				prob = p + LZMA_IS_REP_G0 + state;
  1.1894 ++				if (rc_is_bit_0(&rc, prob)) {
  1.1895 ++					rc_update_bit_0(&rc, prob);
  1.1896 ++					prob = (p + LZMA_IS_REP_0_LONG
  1.1897 ++							+ (state << LZMA_NUM_POS_BITS_MAX) + pos_state);
  1.1898 ++					if (rc_is_bit_0(&rc, prob)) {
  1.1899 ++						rc_update_bit_0(&rc, prob);
  1.1900 ++
  1.1901 ++						state = state < LZMA_NUM_LIT_STATES ? 9 : 11;
  1.1902 ++						pos = buffer_pos - rep0;
  1.1903 ++						while (pos >= header.dict_size)
  1.1904 ++							pos += header.dict_size;
  1.1905 ++						if(pos >= bufsize) {
  1.1906 ++							goto fail;
  1.1907 ++						}
  1.1908 ++						previous_byte = buffer[pos];
  1.1909 ++						buffer[buffer_pos++] = previous_byte;
  1.1910 ++						if (buffer_pos == header.dict_size) {
  1.1911 ++							buffer_pos = 0;
  1.1912 ++							global_pos += header.dict_size;
  1.1913 ++							writebb((char*)buffer, header.dict_size);
  1.1914 ++						}
  1.1915 ++						continue;
  1.1916 ++					} else {
  1.1917 ++						rc_update_bit_1(&rc, prob);
  1.1918 ++					}
  1.1919 ++				} else {
  1.1920 ++					uint32_t distance;
  1.1921 ++
  1.1922 ++					rc_update_bit_1(&rc, prob);
  1.1923 ++					prob = p + LZMA_IS_REP_G1 + state;
  1.1924 ++					if (rc_is_bit_0(&rc, prob)) {
  1.1925 ++						rc_update_bit_0(&rc, prob);
  1.1926 ++						distance = rep1;
  1.1927 ++					} else {
  1.1928 ++						rc_update_bit_1(&rc, prob);
  1.1929 ++						prob = p + LZMA_IS_REP_G2 + state;
  1.1930 ++						if (rc_is_bit_0(&rc, prob)) {
  1.1931 ++							rc_update_bit_0(&rc, prob);
  1.1932 ++							distance = rep2;
  1.1933 ++						} else {
  1.1934 ++							rc_update_bit_1(&rc, prob);
  1.1935 ++							distance = rep3;
  1.1936 ++							rep3 = rep2;
  1.1937 ++						}
  1.1938 ++						rep2 = rep1;
  1.1939 ++					}
  1.1940 ++					rep1 = rep0;
  1.1941 ++					rep0 = distance;
  1.1942 ++				}
  1.1943 ++				state = state < LZMA_NUM_LIT_STATES ? 8 : 11;
  1.1944 ++				prob = p + LZMA_REP_LEN_CODER;
  1.1945 ++			}
  1.1946 ++
  1.1947 ++			prob_len = prob + LZMA_LEN_CHOICE;
  1.1948 ++			if (rc_is_bit_0(&rc, prob_len)) {
  1.1949 ++				rc_update_bit_0(&rc, prob_len);
  1.1950 ++				prob_len = (prob + LZMA_LEN_LOW
  1.1951 ++							+ (pos_state << LZMA_LEN_NUM_LOW_BITS));
  1.1952 ++				offset = 0;
  1.1953 ++				num_bits = LZMA_LEN_NUM_LOW_BITS;
  1.1954 ++			} else {
  1.1955 ++				rc_update_bit_1(&rc, prob_len);
  1.1956 ++				prob_len = prob + LZMA_LEN_CHOICE_2;
  1.1957 ++				if (rc_is_bit_0(&rc, prob_len)) {
  1.1958 ++					rc_update_bit_0(&rc, prob_len);
  1.1959 ++					prob_len = (prob + LZMA_LEN_MID
  1.1960 ++								+ (pos_state << LZMA_LEN_NUM_MID_BITS));
  1.1961 ++					offset = 1 << LZMA_LEN_NUM_LOW_BITS;
  1.1962 ++					num_bits = LZMA_LEN_NUM_MID_BITS;
  1.1963 ++				} else {
  1.1964 ++					rc_update_bit_1(&rc, prob_len);
  1.1965 ++					prob_len = prob + LZMA_LEN_HIGH;
  1.1966 ++					offset = ((1 << LZMA_LEN_NUM_LOW_BITS)
  1.1967 ++							  + (1 << LZMA_LEN_NUM_MID_BITS));
  1.1968 ++					num_bits = LZMA_LEN_NUM_HIGH_BITS;
  1.1969 ++				}
  1.1970 ++			}
  1.1971 ++			rc_bit_tree_decode(&rc, prob_len, num_bits, &len);
  1.1972 ++			len += offset;
  1.1973 ++
  1.1974 ++			if (state < 4) {
  1.1975 ++				int pos_slot;
  1.1976 ++
  1.1977 ++				state += LZMA_NUM_LIT_STATES;
  1.1978 ++				prob =
  1.1979 ++					p + LZMA_POS_SLOT +
  1.1980 ++					((len <
  1.1981 ++					  LZMA_NUM_LEN_TO_POS_STATES ? len :
  1.1982 ++					  LZMA_NUM_LEN_TO_POS_STATES - 1)
  1.1983 ++					 << LZMA_NUM_POS_SLOT_BITS);
  1.1984 ++				rc_bit_tree_decode(&rc, prob, LZMA_NUM_POS_SLOT_BITS,
  1.1985 ++								   &pos_slot);
  1.1986 ++				if (pos_slot >= LZMA_START_POS_MODEL_INDEX) {
  1.1987 ++					num_bits = (pos_slot >> 1) - 1;
  1.1988 ++					rep0 = 2 | (pos_slot & 1);
  1.1989 ++					if (pos_slot < LZMA_END_POS_MODEL_INDEX) {
  1.1990 ++						rep0 <<= num_bits;
  1.1991 ++						prob = p + LZMA_SPEC_POS + rep0 - pos_slot - 1;
  1.1992 ++					} else {
  1.1993 ++						num_bits -= LZMA_NUM_ALIGN_BITS;
  1.1994 ++						while (num_bits--)
  1.1995 ++							rep0 = (rep0 << 1) | rc_direct_bit(&rc);
  1.1996 ++						prob = p + LZMA_ALIGN;
  1.1997 ++						rep0 <<= LZMA_NUM_ALIGN_BITS;
  1.1998 ++						num_bits = LZMA_NUM_ALIGN_BITS;
  1.1999 ++					}
  1.2000 ++					i = 1;
  1.2001 ++					mi = 1;
  1.2002 ++					while (num_bits--) {
  1.2003 ++						if (rc_get_bit(&rc, prob + mi, &mi))
  1.2004 ++							rep0 |= i;
  1.2005 ++						i <<= 1;
  1.2006 ++					}
  1.2007 ++				} else
  1.2008 ++					rep0 = pos_slot;
  1.2009 ++				if (++rep0 == 0)
  1.2010 ++					break;
  1.2011 ++			}
  1.2012 ++
  1.2013 ++			len += LZMA_MATCH_MIN_LEN;
  1.2014 ++
  1.2015 ++			do {
  1.2016 ++				pos = buffer_pos - rep0;
  1.2017 ++				while (pos >= header.dict_size)
  1.2018 ++					pos += header.dict_size;
  1.2019 ++				if(pos >= bufsize) {
  1.2020 ++					goto fail;
  1.2021 ++				}
  1.2022 ++				previous_byte = buffer[pos];
  1.2023 ++				buffer[buffer_pos++] = previous_byte;
  1.2024 ++				if (buffer_pos == header.dict_size) {
  1.2025 ++					buffer_pos = 0;
  1.2026 ++					global_pos += header.dict_size;
  1.2027 ++					writebb((char*)buffer, header.dict_size);
  1.2028 ++				}
  1.2029 ++				len--;
  1.2030 ++			} while (len != 0 && buffer_pos < header.dst_size);
  1.2031 ++		}
  1.2032 ++	}
  1.2033 ++
  1.2034 ++	writebb((char*)buffer, buffer_pos);
  1.2035 ++	if(posp) {
  1.2036 ++		*posp = rc.ptr-rc.buffer;
  1.2037 ++	}
  1.2038 ++	large_free(buffer);
  1.2039 ++	return 0;
  1.2040 ++ fail:
  1.2041 ++	large_free(buffer);
  1.2042 ++	return -1;
  1.2043 ++}
  1.2044 +
  1.2045 +--- linux-2.6.22.9/lib/Makefile
  1.2046 ++++ linux-2.6.22.9/lib/Makefile
  1.2047 +@@ -46,6 +46,10 @@
  1.2048 + obj-$(CONFIG_LIBCRC32C)	+= libcrc32c.o
  1.2049 + obj-$(CONFIG_GENERIC_ALLOCATOR) += genalloc.o
  1.2050 + 
  1.2051 ++obj-$(CONFIG_RD_BZIP2)	+= decompress_bunzip2.o
  1.2052 ++obj-$(CONFIG_RD_LZMA)	+= decompress_unlzma.o
  1.2053 ++
  1.2054 ++
  1.2055 + obj-$(CONFIG_ZLIB_INFLATE) += zlib_inflate/
  1.2056 + obj-$(CONFIG_ZLIB_DEFLATE) += zlib_deflate/
  1.2057 + obj-$(CONFIG_REED_SOLOMON) += reed_solomon/
  1.2058 +
  1.2059 +--- linux-2.6.22.9/scripts/Makefile.lib
  1.2060 ++++ linux-2.6.22.9/scripts/Makefile.lib
  1.2061 +@@ -162,4 +162,17 @@
  1.2062 + quiet_cmd_gzip = GZIP    $@
  1.2063 + cmd_gzip = gzip -f -9 < $< > $@
  1.2064 + 
  1.2065 ++# Append size
  1.2066 ++size_append=perl -e 'print(pack("i",(stat($$ARGV[0]))[7]));'
  1.2067 + 
  1.2068 ++# Bzip2
  1.2069 ++# ---------------------------------------------------------------------------
  1.2070 ++
  1.2071 ++quiet_cmd_bzip2 = BZIP2    $@
  1.2072 ++cmd_bzip2 = (bzip2 -9 < $< ; $(size_append) $<) > $@
  1.2073 ++
  1.2074 ++# Lzma
  1.2075 ++# ---------------------------------------------------------------------------
  1.2076 ++
  1.2077 ++quiet_cmd_lzma = LZMA    $@
  1.2078 ++cmd_lzma = (lzma e $< -so ; $(size_append) $<) >$@