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