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