wok-6.x annotate linux/stuff/linux-lzma-2.6.25.5.u @ rev 936

Linux: lzma catch eof
author Pascal Bellard <pascal.bellard@slitaz.org>
date Fri Jun 20 21:49:23 2008 +0000 (2008-06-20)
parents 9e2bae22918d
children edda77c45d23
rev   line source
pascal@886 1 --- linux-2.6.25.5/arch/x86/boot/compressed/Makefile
pascal@886 2 +++ linux-2.6.25.5/arch/x86/boot/compressed/Makefile
pascal@886 3 @@ -4,7 +4,7 @@
pascal@886 4 # create a compressed vmlinux image from the original vmlinux
pascal@886 5 #
pascal@886 6
pascal@886 7 -targets := vmlinux vmlinux.bin vmlinux.bin.gz head_$(BITS).o misc.o piggy.o
pascal@886 8 +targets := vmlinux vmlinux.bin vmlinux.bin.gz vmlinux.bin.bz2 vmlinux.bin.lzma head_$(BITS).o misc.o piggy.o
pascal@886 9
pascal@886 10 KBUILD_CFLAGS := -m$(BITS) -D__KERNEL__ $(LINUX_INCLUDE) -O2
pascal@886 11 KBUILD_CFLAGS += -fno-strict-aliasing -fPIC
pascal@886 12 @@ -50,15 +50,41 @@
pascal@886 13 $(obj)/vmlinux.bin.gz: $(obj)/vmlinux.bin FORCE
pascal@886 14 $(call if_changed,gzip)
pascal@886 15 endif
pascal@886 16 +
pascal@886 17 +ifdef CONFIG_RELOCATABLE
pascal@886 18 +$(obj)/vmlinux.bin.bz2: $(obj)/vmlinux.bin.all FORCE
pascal@886 19 + $(call if_changed,bzip2)
pascal@886 20 +else
pascal@886 21 +$(obj)/vmlinux.bin.bz2: $(obj)/vmlinux.bin FORCE
pascal@886 22 + $(call if_changed,bzip2)
pascal@886 23 +endif
pascal@886 24 +
pascal@886 25 +ifdef CONFIG_RELOCATABLE
pascal@886 26 +$(obj)/vmlinux.bin.lzma: $(obj)/vmlinux.bin.all FORCE
pascal@886 27 + $(call if_changed,lzma)
pascal@886 28 +else
pascal@886 29 +$(obj)/vmlinux.bin.lzma: $(obj)/vmlinux.bin FORCE
pascal@886 30 + $(call if_changed,lzma)
pascal@886 31 +endif
pascal@886 32 +
pascal@886 33 LDFLAGS_piggy.o := -r --format binary --oformat elf32-i386 -T
pascal@886 34
pascal@886 35 else
pascal@886 36 +$(obj)/vmlinux.bin.bz2: $(obj)/vmlinux.bin FORCE
pascal@886 37 + $(call if_changed,bzip2)
pascal@886 38 +
pascal@886 39 +$(obj)/vmlinux.bin.lzma: $(obj)/vmlinux.bin FORCE
pascal@886 40 + $(call if_changed,lzma)
pascal@886 41 +
pascal@886 42 $(obj)/vmlinux.bin.gz: $(obj)/vmlinux.bin FORCE
pascal@886 43 $(call if_changed,gzip)
pascal@886 44
pascal@886 45 LDFLAGS_piggy.o := -r --format binary --oformat elf64-x86-64 -T
pascal@886 46 endif
pascal@886 47
pascal@886 48 +suffix_$(CONFIG_KERNEL_GZIP) = gz
pascal@886 49 +suffix_$(CONFIG_KERNEL_BZIP2) = bz2
pascal@886 50 +suffix_$(CONFIG_KERNEL_LZMA) = lzma
pascal@886 51
pascal@886 52 -$(obj)/piggy.o: $(obj)/vmlinux.scr $(obj)/vmlinux.bin.gz FORCE
pascal@886 53 +$(obj)/piggy.o: $(src)/vmlinux.scr $(obj)/vmlinux.bin.$(suffix_y) FORCE
pascal@886 54 $(call if_changed,ld)
pascal@886 55
pascal@886 56 --- linux-2.6.25.5/arch/x86/boot/compressed/misc.c
pascal@886 57 +++ linux-2.6.25.5/arch/x86/boot/compressed/misc.c
pascal@886 58 @@ -130,9 +130,12 @@
pascal@886 59 * always be larger than our output buffer.
pascal@886 60 */
pascal@886 61
pascal@886 62 +#ifdef CONFIG_KERNEL_GZIP
pascal@886 63 static uch *inbuf; /* input buffer */
pascal@886 64 +#endif
pascal@886 65 static uch *window; /* Sliding window buffer, (and final output buffer) */
pascal@886 66
pascal@886 67 +#ifdef CONFIG_KERNEL_GZIP
pascal@886 68 static unsigned insize; /* valid bytes in inbuf */
pascal@886 69 static unsigned inptr; /* index of next byte to be processed in inbuf */
pascal@886 70 static unsigned outcnt; /* bytes in output buffer */
pascal@886 71 @@ -167,9 +170,12 @@
pascal@886 72
pascal@886 73 static int fill_inbuf(void);
pascal@886 74 static void flush_window(void);
pascal@886 75 +#endif
pascal@886 76 static void error(char *m);
pascal@886 77 +#ifdef CONFIG_KERNEL_GZIP
pascal@886 78 static void gzip_mark(void **);
pascal@886 79 static void gzip_release(void **);
pascal@886 80 +#endif
pascal@886 81
pascal@886 82 /*
pascal@886 83 * This is set up by the setup-routine at boot-time
pascal@886 84 @@ -185,12 +191,12 @@
pascal@886 85 extern unsigned char input_data[];
pascal@886 86 extern int input_len;
pascal@886 87
pascal@886 88 -static long bytes_out = 0;
pascal@886 89 -
pascal@886 90 static void *malloc(int size);
pascal@886 91 static void free(void *where);
pascal@886 92
pascal@886 93 +#if (defined CONFIG_KERNEL_GZIP || defined CONFIG_KERNEL_BZIP2)
pascal@886 94 static void *memset(void *s, int c, unsigned n);
pascal@886 95 +#endif
pascal@886 96 static void *memcpy(void *dest, const void *src, unsigned n);
pascal@886 97
pascal@886 98 static void putstr(const char *);
pascal@886 99 @@ -204,11 +210,15 @@
pascal@886 100 static memptr free_mem_ptr;
pascal@886 101 static memptr free_mem_end_ptr;
pascal@886 102
pascal@886 103 +#if (defined CONFIG_KERNEL_BZIP2 || defined CONFIG_KERNEL_LZMA)
pascal@886 104 +#define HEAP_SIZE 0x400000
pascal@886 105 +#else
pascal@886 106 #ifdef CONFIG_X86_64
pascal@886 107 #define HEAP_SIZE 0x7000
pascal@886 108 #else
pascal@886 109 #define HEAP_SIZE 0x4000
pascal@886 110 #endif
pascal@886 111 +#endif
pascal@886 112
pascal@886 113 static char *vidmem = (char *)0xb8000;
pascal@886 114 static int vidport;
pascal@886 115 @@ -218,7 +228,29 @@
pascal@886 116 void *xquad_portio;
pascal@886 117 #endif
pascal@886 118
pascal@886 119 +#if (defined CONFIG_KERNEL_BZIP2 || defined CONFIG_KERNEL_LZMA)
pascal@886 120 +
pascal@886 121 +#define large_malloc malloc
pascal@886 122 +#define large_free free
pascal@886 123 +
pascal@886 124 +#ifdef current
pascal@886 125 +#undef current
pascal@886 126 +#endif
pascal@886 127 +
pascal@886 128 +#define INCLUDED
pascal@886 129 +#endif
pascal@886 130 +
pascal@886 131 +#ifdef CONFIG_KERNEL_GZIP
pascal@886 132 #include "../../../../lib/inflate.c"
pascal@886 133 +#endif
pascal@886 134 +
pascal@886 135 +#ifdef CONFIG_KERNEL_BZIP2
pascal@886 136 +#include "../../../../lib/decompress_bunzip2.c"
pascal@886 137 +#endif
pascal@886 138 +
pascal@886 139 +#ifdef CONFIG_KERNEL_LZMA
pascal@886 140 +#include "../../../../lib/decompress_unlzma.c"
pascal@886 141 +#endif
pascal@886 142
pascal@886 143 static void *malloc(int size)
pascal@886 144 {
pascal@886 145 @@ -242,6 +274,7 @@
pascal@886 146 { /* Don't care */
pascal@886 147 }
pascal@886 148
pascal@886 149 +#ifdef CONFIG_KERNEL_GZIP
pascal@886 150 static void gzip_mark(void **ptr)
pascal@886 151 {
pascal@886 152 *ptr = (void *) free_mem_ptr;
pascal@886 153 @@ -251,6 +284,7 @@
pascal@886 154 {
pascal@886 155 free_mem_ptr = (memptr) *ptr;
pascal@886 156 }
pascal@886 157 +#endif
pascal@886 158
pascal@886 159 static void scroll(void)
pascal@886 160 {
pascal@886 161 @@ -303,6 +337,7 @@
pascal@886 162 outb(0xff & (pos >> 1), vidport+1);
pascal@886 163 }
pascal@886 164
pascal@886 165 +#if (defined CONFIG_KERNEL_GZIP || defined CONFIG_KERNEL_BZIP2)
pascal@886 166 static void* memset(void* s, int c, unsigned n)
pascal@886 167 {
pascal@886 168 int i;
pascal@886 169 @@ -311,6 +346,7 @@
pascal@886 170 for (i=0;i<n;i++) ss[i] = c;
pascal@886 171 return s;
pascal@886 172 }
pascal@886 173 +#endif
pascal@886 174
pascal@886 175 static void* memcpy(void* dest, const void* src, unsigned n)
pascal@886 176 {
pascal@886 177 @@ -322,6 +358,26 @@
pascal@886 178 return dest;
pascal@886 179 }
pascal@886 180
pascal@886 181 +#ifdef CONFIG_KERNEL_BZIP2
pascal@886 182 +/* ===========================================================================
pascal@886 183 + * Write the output window window[0..outcnt-1].
pascal@886 184 + * (Used for the decompressed data only.)
pascal@886 185 + */
pascal@886 186 +static int compr_flush(char *data, unsigned int len)
pascal@886 187 +{
pascal@886 188 + unsigned n;
pascal@886 189 + uch *out;
pascal@886 190 +
pascal@886 191 + out = window;
pascal@886 192 + for (n = 0; n < len; n++) {
pascal@886 193 + *out++ = *data++;
pascal@886 194 + }
pascal@886 195 + window += (ulg)len;
pascal@886 196 + return len;
pascal@886 197 +}
pascal@886 198 +
pascal@886 199 +#endif
pascal@886 200 +#ifdef CONFIG_KERNEL_GZIP
pascal@886 201 /* ===========================================================================
pascal@886 202 * Fill the input buffer. This is called only when the buffer is empty
pascal@886 203 * and at least one byte is really needed.
pascal@886 204 @@ -333,7 +389,7 @@
pascal@886 205 }
pascal@886 206
pascal@886 207 /* ===========================================================================
pascal@886 208 - * Write the output window window[0..outcnt-1] and update crc and bytes_out.
pascal@886 209 + * Write the output window window[0..outcnt-1] and update crc.
pascal@886 210 * (Used for the decompressed data only.)
pascal@886 211 */
pascal@886 212 static void flush_window(void)
pascal@886 213 @@ -351,9 +407,9 @@
pascal@886 214 c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
pascal@886 215 }
pascal@886 216 crc = c;
pascal@886 217 - bytes_out += (ulg)outcnt;
pascal@886 218 outcnt = 0;
pascal@886 219 }
pascal@886 220 +#endif
pascal@886 221
pascal@886 222 static void error(char *x)
pascal@886 223 {
pascal@886 224 @@ -385,9 +441,11 @@
pascal@886 225 window = output; /* Output buffer (Normally at 1M) */
pascal@886 226 free_mem_ptr = heap; /* Heap */
pascal@886 227 free_mem_end_ptr = heap + HEAP_SIZE;
pascal@886 228 +#ifdef CONFIG_KERNEL_GZIP
pascal@886 229 inbuf = input_data; /* Input buffer */
pascal@886 230 insize = input_len;
pascal@886 231 inptr = 0;
pascal@886 232 +#endif
pascal@886 233
pascal@886 234 #ifdef CONFIG_X86_64
pascal@886 235 if ((ulg)output & (__KERNEL_ALIGN - 1))
pascal@886 236 @@ -405,9 +463,21 @@
pascal@886 237 #endif
pascal@886 238 #endif
pascal@886 239
pascal@886 240 +#ifdef CONFIG_KERNEL_BZIP2
pascal@886 241 + putstr("\nBunzipping Linux... ");
pascal@886 242 + bunzip2(input_data, input_len-4, NULL, compr_flush, NULL);
pascal@886 243 +#endif
pascal@886 244 +
pascal@886 245 +#ifdef CONFIG_KERNEL_LZMA
pascal@886 246 + putstr("\nUnlzmaing Linux... ");
pascal@886 247 + unlzma(input_data, input_len-4, NULL, NULL, window);
pascal@886 248 +#endif
pascal@886 249 +
pascal@886 250 +#ifdef CONFIG_KERNEL_GZIP
pascal@886 251 makecrc();
pascal@886 252 putstr("\nDecompressing Linux... ");
pascal@886 253 gunzip();
pascal@886 254 +#endif
pascal@886 255 putstr("done.\nBooting the kernel.\n");
pascal@886 256 return;
pascal@886 257 }
pascal@886 258
pascal@886 259 --- linux-2.6.25.5/drivers/block/Kconfig
pascal@886 260 +++ linux-2.6.25.5/drivers/block/Kconfig
pascal@886 261 @@ -357,6 +357,30 @@
pascal@886 262 will prevent RAM block device backing store memory from being
pascal@886 263 allocated from highmem (only a problem for highmem systems).
pascal@886 264
pascal@886 265 +config RD_BZIP2
pascal@886 266 + bool "Initial ramdisk compressed using bzip2"
pascal@886 267 + default n
pascal@886 268 + depends on BLK_DEV_INITRD=y
pascal@886 269 + help
pascal@886 270 + Support loading of a bzip2 encoded initial ramdisk or cpio buffer
pascal@886 271 + If unsure, say N.
pascal@886 272 +
pascal@886 273 +config RD_LZMA
pascal@886 274 + bool "Initial ramdisk compressed using lzma"
pascal@886 275 + default n
pascal@886 276 + depends on BLK_DEV_INITRD=y
pascal@886 277 + help
pascal@886 278 + Support loading of a lzma encoded initial ramdisk or cpio buffer
pascal@886 279 + If unsure, say N.
pascal@886 280 +
pascal@886 281 +config RD_GZIP
pascal@886 282 + bool "Initial ramdisk compressed using gzip"
pascal@886 283 + default y
pascal@886 284 + depends on BLK_DEV_INITRD=y
pascal@886 285 + help
pascal@886 286 + Support loading of a gzip encoded initial ramdisk or cpio buffer.
pascal@886 287 + If unsure, say Y.
pascal@886 288 +
pascal@886 289 config CDROM_PKTCDVD
pascal@886 290 tristate "Packet writing on CD/DVD media"
pascal@886 291 depends on !UML
pascal@886 292
pascal@886 293 --- linux-2.6.25.5/include/linux/decompress_bunzip2.h
pascal@886 294 +++ linux-2.6.25.5/include/linux/decompress_bunzip2.h
pascal@886 295 @@ -0,0 +1,16 @@
pascal@886 296 +#ifndef DECOMPRESS_BUNZIP2_H
pascal@886 297 +#define DECOMPRESS_BUNZIP2_H
pascal@886 298 +
pascal@886 299 +/* Other housekeeping constants */
pascal@886 300 +#define BZIP2_IOBUF_SIZE 4096
pascal@886 301 +
pascal@886 302 +#ifndef STATIC
pascal@886 303 +#define STATIC /**/
pascal@886 304 +#endif
pascal@886 305 +
pascal@886 306 +STATIC int bunzip2(char *inbuf, int len,
pascal@886 307 + int(*fill)(void*,unsigned int),
pascal@886 308 + int(*writebb)(char*,unsigned int),
pascal@886 309 + int *pos);
pascal@886 310 +
pascal@886 311 +#endif
pascal@886 312
pascal@886 313 --- linux-2.6.25.5/include/linux/decompress_generic.h
pascal@886 314 +++ linux-2.6.25.5/include/linux/decompress_generic.h
pascal@886 315 @@ -0,0 +1,28 @@
pascal@886 316 +#ifndef DECOMPRESS_GENERIC_H
pascal@886 317 +#define DECOMPRESS_GENERIC_H
pascal@886 318 +
pascal@886 319 +/* Minimal chunksize to be read.
pascal@886 320 + * Bzip2 prefers at least 4096
pascal@886 321 + * Lzma prefers 0x10000 */
pascal@886 322 +#define COMPR_IOBUF_SIZE 4096
pascal@886 323 +
pascal@886 324 +typedef int (*uncompress_fn) (char *inbuf, int len,
pascal@886 325 + int(*fill)(char*,unsigned int),
pascal@886 326 + int(*writebb)(char*,unsigned int),
pascal@886 327 + int *posp);
pascal@886 328 +
pascal@886 329 +/* inbuf - input buffer
pascal@886 330 + * len - len of pre-read data in inbuf
pascal@886 331 + * fill - function to fill inbuf if empty
pascal@886 332 + * writebb - function to write out outbug
pascal@886 333 + * posp - if non-null, input position (number of bytes read) will be
pascal@886 334 + * returned here
pascal@886 335 + *
pascal@886 336 + * If len != 0, the inbuf is initialized (with as much data), and fill
pascal@886 337 + * should not be called
pascal@886 338 + * If len = 0, the inbuf is allocated, but empty. Its size is IOBUF_SIZE
pascal@886 339 + * fill should be called (repeatedly...) to read data, at most IOBUF_SIZE
pascal@886 340 + */
pascal@886 341 +
pascal@886 342 +
pascal@886 343 +#endif
pascal@886 344
pascal@886 345 --- linux-2.6.25.5/include/linux/decompress_unlzma.h
pascal@886 346 +++ linux-2.6.25.5/include/linux/decompress_unlzma.h
pascal@886 347 @@ -0,0 +1,15 @@
pascal@886 348 +#ifndef DECOMPRESS_UNLZMA_H
pascal@886 349 +#define DECOMPRESS_UNLZMA_H
pascal@886 350 +
pascal@886 351 +#define LZMA_IOBUF_SIZE 0x10000
pascal@886 352 +
pascal@886 353 +#ifndef STATIC
pascal@886 354 +#define STATIC /**/
pascal@886 355 +#endif
pascal@886 356 +
pascal@886 357 +STATIC int unlzma(char *inbuf, int len,
pascal@886 358 + int(*fill)(void*,unsigned int),
pascal@886 359 + int(*writebb)(char*,unsigned int),
pascal@886 360 + int *pos);
pascal@886 361 +
pascal@886 362 +#endif
pascal@886 363
pascal@886 364 --- linux-2.6.25.5/init/do_mounts_rd.c
pascal@886 365 +++ linux-2.6.25.5/init/do_mounts_rd.c
pascal@886 366 @@ -8,6 +8,16 @@
pascal@886 367 #include <linux/initrd.h>
pascal@886 368 #include <linux/string.h>
pascal@886 369
pascal@886 370 +#ifdef CONFIG_RD_BZIP2
pascal@886 371 +#include <linux/decompress_bunzip2.h>
pascal@886 372 +#undef STATIC
pascal@886 373 +#endif
pascal@886 374 +
pascal@886 375 +#ifdef CONFIG_RD_LZMA
pascal@886 376 +#include <linux/decompress_unlzma.h>
pascal@886 377 +#undef STATIC
pascal@886 378 +#endif
pascal@886 379 +
pascal@886 380 #include "do_mounts.h"
pascal@886 381
pascal@886 382 #define BUILD_CRAMDISK
pascal@886 383 @@ -30,7 +40,15 @@ static int __init ramdisk_start_setup(ch
pascal@886 384 }
pascal@886 385 __setup("ramdisk_start=", ramdisk_start_setup);
pascal@886 386
pascal@886 387 +#ifdef CONFIG_RD_GZIP
pascal@886 388 static int __init crd_load(int in_fd, int out_fd);
pascal@886 389 +#endif
pascal@886 390 +#ifdef CONFIG_RD_BZIP2
pascal@886 391 +static int __init crd_load_bzip2(int in_fd, int out_fd);
pascal@886 392 +#endif
pascal@886 393 +#ifdef CONFIG_RD_LZMA
pascal@886 394 +static int __init crd_load_lzma(int in_fd, int out_fd);
pascal@886 395 +#endif
pascal@886 396
pascal@886 397 /*
pascal@886 398 * This routine tries to find a RAM disk image to load, and returns the
pascal@886 399 @@ -46,7 +64,7 @@ static int __init crd_load(int in_fd, in
pascal@886 400 * gzip
pascal@886 401 */
pascal@886 402 static int __init
pascal@886 403 -identify_ramdisk_image(int fd, int start_block)
pascal@886 404 +identify_ramdisk_image(int fd, int start_block, int *ztype)
pascal@886 405 {
pascal@886 406 const int size = 512;
pascal@886 407 struct minix_super_block *minixsb;
pascal@886 408 @@ -72,6 +90,7 @@ identify_ramdisk_image(int fd, int start
pascal@886 409 sys_lseek(fd, start_block * BLOCK_SIZE, 0);
pascal@886 410 sys_read(fd, buf, size);
pascal@886 411
pascal@886 412 +#ifdef CONFIG_RD_GZIP
pascal@886 413 /*
pascal@886 414 * If it matches the gzip magic numbers, return -1
pascal@886 415 */
pascal@886 416 @@ -79,9 +98,40 @@ identify_ramdisk_image(int fd, int start
pascal@886 417 printk(KERN_NOTICE
pascal@886 418 "RAMDISK: Compressed image found at block %d\n",
pascal@886 419 start_block);
pascal@886 420 + *ztype = 0;
pascal@886 421 + nblocks = 0;
pascal@886 422 + goto done;
pascal@886 423 + }
pascal@886 424 +#endif
pascal@886 425 +
pascal@886 426 +#ifdef CONFIG_RD_BZIP2
pascal@886 427 + /*
pascal@886 428 + * If it matches the bzip magic numbers, return -1
pascal@886 429 + */
pascal@886 430 + if (buf[0] == 0x42 && (buf[1] == 0x5a)) {
pascal@886 431 + printk(KERN_NOTICE
pascal@886 432 + "RAMDISK: Bzipped image found at block %d\n",
pascal@886 433 + start_block);
pascal@886 434 + *ztype = 1;
pascal@886 435 + nblocks = 0;
pascal@886 436 + goto done;
pascal@886 437 + }
pascal@886 438 +#endif
pascal@886 439 +
pascal@886 440 +#ifdef CONFIG_RD_LZMA
pascal@886 441 + /*
pascal@886 442 + * If it matches the bzip magic numbers, return -1
pascal@886 443 + */
pascal@886 444 + if (buf[0] == 0x5d && (buf[1] == 0x00)) {
pascal@886 445 + printk(KERN_NOTICE
pascal@886 446 + "RAMDISK: Lzma image found at block %d\n",
pascal@886 447 + start_block);
pascal@886 448 + *ztype = 2;
pascal@886 449 nblocks = 0;
pascal@886 450 goto done;
pascal@886 451 }
pascal@886 452 +#endif
pascal@886 453 +
pascal@886 454
pascal@886 455 /* romfs is at block zero too */
pascal@886 456 if (romfsb->word0 == ROMSB_WORD0 &&
pascal@886 457 @@ -145,6 +195,7 @@ int __init rd_load_image(char *from)
pascal@886 458 int nblocks, i, disk;
pascal@886 459 char *buf = NULL;
pascal@886 460 unsigned short rotate = 0;
pascal@886 461 + int ztype=-1;
pascal@886 462 #if !defined(CONFIG_S390) && !defined(CONFIG_PPC_ISERIES)
pascal@886 463 char rotator[4] = { '|' , '/' , '-' , '\\' };
pascal@886 464 #endif
pascal@886 465 @@ -157,14 +208,38 @@ int __init rd_load_image(char *from)
pascal@886 466 if (in_fd < 0)
pascal@886 467 goto noclose_input;
pascal@886 468
pascal@886 469 - nblocks = identify_ramdisk_image(in_fd, rd_image_start);
pascal@886 470 + nblocks = identify_ramdisk_image(in_fd, rd_image_start, &ztype);
pascal@886 471 if (nblocks < 0)
pascal@886 472 goto done;
pascal@886 473
pascal@886 474 if (nblocks == 0) {
pascal@886 475 #ifdef BUILD_CRAMDISK
pascal@886 476 - if (crd_load(in_fd, out_fd) == 0)
pascal@886 477 - goto successful_load;
pascal@886 478 + switch(ztype) {
pascal@886 479 +
pascal@886 480 +#ifdef CONFIG_RD_GZIP
pascal@886 481 + case 0:
pascal@886 482 + if (crd_load(in_fd, out_fd) == 0)
pascal@886 483 + goto successful_load;
pascal@886 484 + break;
pascal@886 485 +#endif
pascal@886 486 +
pascal@886 487 +#ifdef CONFIG_RD_BZIP2
pascal@886 488 + case 1:
pascal@886 489 + if (crd_load_bzip2(in_fd, out_fd) == 0)
pascal@886 490 + goto successful_load;
pascal@886 491 + break;
pascal@886 492 +#endif
pascal@886 493 +
pascal@886 494 +#ifdef CONFIG_RD_LZMA
pascal@886 495 + case 2:
pascal@886 496 + if (crd_load_lzma(in_fd, out_fd) == 0)
pascal@886 497 + goto successful_load;
pascal@886 498 + break;
pascal@886 499 +#endif
pascal@886 500 +
pascal@886 501 + default:
pascal@886 502 + break;
pascal@886 503 + }
pascal@886 504 #else
pascal@886 505 printk(KERN_NOTICE
pascal@886 506 "RAMDISK: Kernel does not support compressed "
pascal@886 507 @@ -269,6 +344,7 @@ int __init rd_load_disk(int n)
pascal@886 508
pascal@886 509 #ifdef BUILD_CRAMDISK
pascal@886 510
pascal@886 511 +#ifdef CONFIG_RD_GZIP
pascal@886 512 /*
pascal@886 513 * gzip declarations
pascal@886 514 */
pascal@886 515 @@ -296,8 +372,11 @@ static unsigned outcnt; /* bytes in out
pascal@886 516 static int exit_code;
pascal@886 517 static int unzip_error;
pascal@886 518 static long bytes_out;
pascal@886 519 +#endif
pascal@886 520 +
pascal@886 521 static int crd_infd, crd_outfd;
pascal@886 522
pascal@886 523 +#ifdef CONFIG_RD_GZIP
pascal@886 524 #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
pascal@886 525
pascal@886 526 /* Diagnostic functions (stubbed out) */
pascal@886 527 @@ -359,7 +438,22 @@ static int __init fill_inbuf(void)
pascal@886 528
pascal@886 529 return inbuf[0];
pascal@886 530 }
pascal@886 531 +#endif
pascal@886 532 +
pascal@886 533 +#if (defined CONFIG_RD_BZIP2 || defined CONFIG_RD_LZMA)
pascal@886 534 +static int __init compr_fill(void *buf, unsigned int len)
pascal@886 535 +{
pascal@886 536 + int r = sys_read(crd_infd, buf, len);
pascal@886 537 + if(r < 0) {
pascal@886 538 + printk(KERN_ERR "RAMDISK: error while reading compressed data");
pascal@886 539 + } else if(r == 0) {
pascal@886 540 + printk(KERN_ERR "RAMDISK: EOF while reading compressed data");
pascal@886 541 + }
pascal@886 542 + return r;
pascal@886 543 +}
pascal@886 544 +#endif
pascal@886 545
pascal@886 546 +#ifdef CONFIG_RD_GZIP
pascal@886 547 /* ===========================================================================
pascal@886 548 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
pascal@886 549 * (Used for the decompressed data only.)
pascal@886 550 @@ -385,7 +479,24 @@ static void __init flush_window(void)
pascal@886 551 bytes_out += (ulg)outcnt;
pascal@886 552 outcnt = 0;
pascal@886 553 }
pascal@886 554 +#endif
pascal@886 555 +
pascal@886 556 +#if (defined CONFIG_RD_BZIP2 || defined CONFIG_RD_LZMA)
pascal@886 557 +static int __init compr_flush(void *window, unsigned int outcnt) {
pascal@886 558 + static int progressDots=0;
pascal@886 559 + int written = sys_write(crd_outfd, window, outcnt);
pascal@886 560 + if (written != outcnt) {
pascal@886 561 + printk(KERN_ERR "RAMDISK: incomplete write (%d != %d)\n",
pascal@886 562 + written, outcnt);
pascal@886 563 + }
pascal@886 564 + progressDots = (progressDots+1)%10;
pascal@886 565 + if(!progressDots)
pascal@886 566 + printk(".");
pascal@886 567 + return outcnt;
pascal@886 568 +}
pascal@886 569 +#endif
pascal@886 570
pascal@886 571 +#ifdef CONFIG_RD_GZIP
pascal@886 572 static void __init error(char *x)
pascal@886 573 {
pascal@886 574 printk(KERN_ERR "%s\n", x);
pascal@886 575 @@ -425,5 +536,43 @@ static int __init crd_load(int in_fd, in
pascal@886 576 kfree(window);
pascal@886 577 return result;
pascal@886 578 }
pascal@886 579 +#endif
pascal@886 580 +
pascal@886 581 +#if (defined CONFIG_RD_BZIP2 || defined CONFIG_RD_LZMA)
pascal@886 582 +static int __init crd_load_compr(int in_fd, int out_fd, int size,
pascal@886 583 + int (*deco)(char *,int,
pascal@886 584 + int(*fill)(void*,unsigned int),
pascal@886 585 + int(*flush)(void*,unsigned int),
pascal@886 586 + int *))
pascal@886 587 +{
pascal@886 588 + int result;
pascal@886 589 + char *inbuf = kmalloc(size, GFP_KERNEL);
pascal@886 590 + crd_infd = in_fd;
pascal@886 591 + crd_outfd = out_fd;
pascal@886 592 + if (inbuf == 0) {
pascal@886 593 + printk(KERN_ERR "RAMDISK: Couldn't allocate decompression buffer\n");
pascal@886 594 + return -1;
pascal@886 595 + }
pascal@886 596 + result=deco(inbuf, 0, compr_fill, compr_flush, NULL);
pascal@886 597 + kfree(inbuf);
pascal@886 598 + printk("\n");
pascal@886 599 + return result;
pascal@886 600 +}
pascal@886 601 +#endif
pascal@886 602 +
pascal@886 603 +#ifdef CONFIG_RD_BZIP2
pascal@886 604 +static int __init crd_load_bzip2(int in_fd, int out_fd)
pascal@886 605 +{
pascal@886 606 + return crd_load_compr(in_fd, out_fd, BZIP2_IOBUF_SIZE, bunzip2);
pascal@886 607 +}
pascal@886 608 +#endif
pascal@886 609 +
pascal@886 610 +#ifdef CONFIG_RD_LZMA
pascal@886 611 +static int __init crd_load_lzma(int in_fd, int out_fd)
pascal@886 612 +{
pascal@886 613 + return crd_load_compr(in_fd, out_fd, LZMA_IOBUF_SIZE, unlzma);
pascal@886 614 +}
pascal@886 615 +
pascal@886 616 +#endif
pascal@886 617
pascal@886 618 #endif /* BUILD_CRAMDISK */
pascal@886 619
pascal@886 620 --- linux-2.6.25.5/init/initramfs.c
pascal@886 621 +++ linux-2.6.25.5/init/initramfs.c
pascal@886 622 @@ -367,6 +367,18 @@
pascal@886 623 }
pascal@886 624 }
pascal@886 625
pascal@886 626 +#ifdef CONFIG_RD_BZIP2
pascal@886 627 +#include <linux/decompress_bunzip2.h>
pascal@886 628 +#undef STATIC
pascal@886 629 +
pascal@886 630 +#endif
pascal@886 631 +
pascal@886 632 +#ifdef CONFIG_RD_LZMA
pascal@886 633 +#include <linux/decompress_unlzma.h>
pascal@886 634 +#undef STATIC
pascal@886 635 +
pascal@886 636 +#endif
pascal@886 637 +
pascal@886 638 /*
pascal@886 639 * gzip declarations
pascal@886 640 */
pascal@886 641 @@ -441,6 +453,29 @@
pascal@886 642 outcnt = 0;
pascal@886 643 }
pascal@886 644
pascal@886 645 +#include <linux/initrd.h>
pascal@886 646 +#ifdef CONFIG_RD_LZMA
pascal@886 647 +#define INITRD_PAGE ((PAGE_SIZE > 1024*1024) ? PAGE_SIZE : 1024*1024)
pascal@886 648 +static int fill_offset, fill_total;
pascal@886 649 +static int fill_buffer(void *buffer, unsigned size)
pascal@886 650 +{
pascal@886 651 + int max = initrd_end - initrd_start - fill_offset;
pascal@886 652 + if (size < max) max = size;
pascal@886 653 + memcpy(buffer, (void *)(initrd_start + fill_offset), max);
pascal@886 654 + fill_offset += max;
pascal@886 655 + fill_total += max;
pascal@886 656 + if (fill_offset >= INITRD_PAGE) {
pascal@886 657 + unsigned rem = fill_offset % INITRD_PAGE;
pascal@886 658 + unsigned end = initrd_start + fill_offset - rem;
pascal@886 659 + free_initrd_mem(initrd_start, end);
pascal@886 660 + printk(".");
pascal@886 661 + initrd_start = end;
pascal@886 662 + fill_offset = rem;
pascal@886 663 + }
pascal@886 664 + return max;
pascal@886 665 +}
pascal@886 666 +#endif
pascal@886 667 +
pascal@886 668 static char * __init unpack_to_rootfs(char *buf, unsigned len, int check_only)
pascal@886 669 {
pascal@886 670 int written;
pascal@886 671 @@ -455,6 +490,9 @@
pascal@886 672 this_header = 0;
pascal@886 673 message = NULL;
pascal@886 674 while (!message && len) {
pascal@886 675 +#ifdef CONFIG_RD_LZMA
pascal@886 676 + int status;
pascal@886 677 +#endif
pascal@886 678 loff_t saved_offset = this_header;
pascal@886 679 if (*buf == '0' && !(this_header & 3)) {
pascal@886 680 state = Start;
pascal@886 681 @@ -477,9 +515,42 @@
pascal@886 682 bytes_out = 0;
pascal@886 683 crc = (ulg)0xffffffffL; /* shift register contents */
pascal@886 684 makecrc();
pascal@886 685 - gunzip();
pascal@886 686 + if(!gunzip() && message == NULL)
pascal@886 687 + goto ok;
pascal@886 688 +
pascal@886 689 +#ifdef CONFIG_RD_BZIP2
pascal@886 690 + message = NULL; /* Zero out message, or else cpio will
pascal@886 691 + think an error has already occured */
pascal@886 692 + if(!bunzip2(buf, len, NULL, flush_buffer, &inptr) < 0 &&
pascal@886 693 + message == NULL) {
pascal@886 694 + goto ok;
pascal@886 695 + }
pascal@886 696 +#endif
pascal@886 697 +
pascal@886 698 +#ifdef CONFIG_RD_LZMA
pascal@886 699 + message = NULL; /* Zero out message, or else cpio will
pascal@886 700 + think an error has already occured */
pascal@886 701 + status = -1;
pascal@886 702 + if(buf == (char *) initrd_start) {
pascal@886 703 + char *work_buffer = malloc(LZMA_IOBUF_SIZE);
pascal@886 704 + if (work_buffer) {
pascal@886 705 + fill_total = fill_offset = 0;
pascal@886 706 + fill_buffer(work_buffer, LZMA_IOBUF_SIZE);
pascal@886 707 + status = unlzma(work_buffer, LZMA_IOBUF_SIZE,
pascal@886 708 + fill_buffer, flush_buffer, NULL);
pascal@886 709 + inptr = fill_total;
pascal@886 710 + free(work_buffer);
pascal@886 711 + }
pascal@886 712 + }
pascal@886 713 + else status = unlzma(buf,len, NULL, flush_buffer, &inptr);
pascal@886 714 + if (status == 0 && message == NULL) {
pascal@886 715 + goto ok;
pascal@886 716 + }
pascal@886 717 +#endif
pascal@886 718 + ok:
pascal@886 719 +
pascal@886 720 if (state != Reset)
pascal@886 721 - error("junk in gzipped archive");
pascal@886 722 + error("junk in compressed archive");
pascal@886 723 this_header = saved_offset + inptr;
pascal@886 724 buf += inptr;
pascal@886 725 len -= inptr;
pascal@886 726 @@ -545,7 +616,7 @@
pascal@886 727 if (err)
pascal@886 728 panic(err);
pascal@886 729 if (initrd_start) {
pascal@886 730 -#ifdef CONFIG_BLK_DEV_RAM
pascal@886 731 +#ifdef NOT_IN_SLITAZ_CONFIG_BLK_DEV_RAM
pascal@886 732 int fd;
pascal@886 733 printk(KERN_INFO "checking if image is initramfs...");
pascal@886 734 err = unpack_to_rootfs((char *)initrd_start,
pascal@886 735
pascal@886 736 --- linux-2.6.25.5/init/Kconfig
pascal@886 737 +++ linux-2.6.25.5/init/Kconfig
pascal@886 738 @@ -100,6 +100,56 @@
pascal@886 739
pascal@886 740 which is done within the script "scripts/setlocalversion".)
pascal@886 741
pascal@886 742 +choice
pascal@886 743 + prompt "Kernel compression mode"
pascal@886 744 + default KERNEL_GZIP
pascal@886 745 + help
pascal@886 746 + The linux kernel is a kind of self-extracting executable.
pascal@886 747 + Several compression algorithms are available, which differ
pascal@886 748 + in efficiency, compression and decompression speed.
pascal@886 749 + Compression speed is only relevant when building a kernel.
pascal@886 750 + Decompression speed is relevant at each boot.
pascal@886 751 +
pascal@886 752 + If you have any problems with bzip2 or lzma compressed
pascal@886 753 + kernels, mail me (Alain Knaff) <alain@knaff.lu>. (An older
pascal@886 754 + version of this functionality (bzip2 only), for 2.4, was
pascal@886 755 + supplied by Christian Ludwig)
pascal@886 756 +
pascal@886 757 + High compression options are mostly useful for users, who
pascal@886 758 + are low on disk space (embedded systems), but for whom ram
pascal@886 759 + size matters less.
pascal@886 760 +
pascal@886 761 + If in doubt, select 'gzip'
pascal@886 762 +
pascal@886 763 +config KERNEL_GZIP
pascal@886 764 + bool "Gzip"
pascal@886 765 + help
pascal@886 766 + The old and tries gzip compression. Its compression ratio is
pascal@886 767 + the poorest among the 3 choices; however its speed (both
pascal@886 768 + compression and decompression) is the fastest.
pascal@886 769 +
pascal@886 770 +config KERNEL_BZIP2
pascal@886 771 + bool "Bzip2"
pascal@886 772 + help
pascal@886 773 + Its compression ratio and speed is intermediate.
pascal@886 774 + Decompression speed is slowest among the 3.
pascal@886 775 + The kernel size is about 10 per cent smaller with bzip2,
pascal@886 776 + in comparison to gzip.
pascal@886 777 + Bzip2 uses a large amount of memory. For modern kernels
pascal@886 778 + you will need at least 8MB RAM or more for booting.
pascal@886 779 +
pascal@886 780 +config KERNEL_LZMA
pascal@886 781 + bool "LZMA"
pascal@886 782 + help
pascal@886 783 + The most recent compression algorithm.
pascal@886 784 + Its ratio is best, decompression speed is between the other
pascal@886 785 + 2. Compression is slowest.
pascal@886 786 + The kernel size is about 33 per cent smaller with lzma,
pascal@886 787 + in comparison to gzip.
pascal@886 788 +
pascal@886 789 +endchoice
pascal@886 790 +
pascal@886 791 +
pascal@886 792 config SWAP
pascal@886 793 bool "Support for paging of anonymous memory (swap)"
pascal@886 794 depends on MMU && BLOCK
pascal@886 795
pascal@886 796 --- linux-2.6.25.5/lib/decompress_bunzip2.c
pascal@886 797 +++ linux-2.6.25.5/lib/decompress_bunzip2.c
pascal@886 798 @@ -0,0 +1,645 @@
pascal@886 799 +/* vi: set sw=4 ts=4: */
pascal@886 800 +/* Small bzip2 deflate implementation, by Rob Landley (rob@landley.net).
pascal@886 801 +
pascal@886 802 + Based on bzip2 decompression code by Julian R Seward (jseward@acm.org),
pascal@886 803 + which also acknowledges contributions by Mike Burrows, David Wheeler,
pascal@886 804 + Peter Fenwick, Alistair Moffat, Radford Neal, Ian H. Witten,
pascal@886 805 + Robert Sedgewick, and Jon L. Bentley.
pascal@886 806 +
pascal@886 807 + This code is licensed under the LGPLv2:
pascal@886 808 + LGPL (http://www.gnu.org/copyleft/lgpl.html
pascal@886 809 +*/
pascal@886 810 +
pascal@886 811 +/*
pascal@886 812 + Size and speed optimizations by Manuel Novoa III (mjn3@codepoet.org).
pascal@886 813 +
pascal@886 814 + More efficient reading of Huffman codes, a streamlined read_bunzip()
pascal@886 815 + function, and various other tweaks. In (limited) tests, approximately
pascal@886 816 + 20% faster than bzcat on x86 and about 10% faster on arm.
pascal@886 817 +
pascal@886 818 + Note that about 2/3 of the time is spent in read_unzip() reversing
pascal@886 819 + the Burrows-Wheeler transformation. Much of that time is delay
pascal@886 820 + resulting from cache misses.
pascal@886 821 +
pascal@886 822 + I would ask that anyone benefiting from this work, especially those
pascal@886 823 + using it in commercial products, consider making a donation to my local
pascal@886 824 + non-profit hospice organization in the name of the woman I loved, who
pascal@886 825 + passed away Feb. 12, 2003.
pascal@886 826 +
pascal@886 827 + In memory of Toni W. Hagan
pascal@886 828 +
pascal@886 829 + Hospice of Acadiana, Inc.
pascal@886 830 + 2600 Johnston St., Suite 200
pascal@886 831 + Lafayette, LA 70503-3240
pascal@886 832 +
pascal@886 833 + Phone (337) 232-1234 or 1-800-738-2226
pascal@886 834 + Fax (337) 232-1297
pascal@886 835 +
pascal@886 836 + http://www.hospiceacadiana.com/
pascal@886 837 +
pascal@886 838 + Manuel
pascal@886 839 + */
pascal@886 840 +
pascal@886 841 +/*
pascal@886 842 + Made it fit for running in Linux Kernel by Alain Knaff (alain@knaff.lu)
pascal@886 843 +*/
pascal@886 844 +
pascal@886 845 +
pascal@886 846 +#ifndef STATIC
pascal@886 847 +
pascal@886 848 +#include <linux/kernel.h>
pascal@886 849 +#include <linux/fs.h>
pascal@886 850 +#include <linux/string.h>
pascal@886 851 +
pascal@886 852 +#ifdef TEST
pascal@886 853 +#include "test.h"
pascal@886 854 +#else
pascal@886 855 +#include <linux/vmalloc.h>
pascal@886 856 +#endif
pascal@886 857 +
pascal@886 858 +static void __init *large_malloc(size_t size)
pascal@886 859 +{
pascal@886 860 + return vmalloc(size);
pascal@886 861 +}
pascal@886 862 +
pascal@886 863 +static void __init large_free(void *where)
pascal@886 864 +{
pascal@886 865 + vfree(where);
pascal@886 866 +}
pascal@886 867 +
pascal@886 868 +#ifndef TEST
pascal@886 869 +static void __init *malloc(size_t size)
pascal@886 870 +{
pascal@886 871 + return kmalloc(size, GFP_KERNEL);
pascal@886 872 +}
pascal@886 873 +
pascal@886 874 +static void __init free(void *where)
pascal@886 875 +{
pascal@886 876 + kfree(where);
pascal@886 877 +}
pascal@886 878 +
pascal@886 879 +static void __init error(char *x)
pascal@886 880 +{
pascal@886 881 + printk(KERN_ERR "%s\n", x);
pascal@886 882 +}
pascal@886 883 +#endif
pascal@886 884 +
pascal@886 885 +#define STATIC /**/
pascal@886 886 +
pascal@886 887 +#endif
pascal@886 888 +
pascal@886 889 +#include <linux/decompress_bunzip2.h>
pascal@886 890 +
pascal@886 891 +
pascal@886 892 +/* Constants for Huffman coding */
pascal@886 893 +#define MAX_GROUPS 6
pascal@886 894 +#define GROUP_SIZE 50 /* 64 would have been more efficient */
pascal@886 895 +#define MAX_HUFCODE_BITS 20 /* Longest Huffman code allowed */
pascal@886 896 +#define MAX_SYMBOLS 258 /* 256 literals + RUNA + RUNB */
pascal@886 897 +#define SYMBOL_RUNA 0
pascal@886 898 +#define SYMBOL_RUNB 1
pascal@886 899 +
pascal@886 900 +/* Status return values */
pascal@886 901 +#define RETVAL_OK 0
pascal@886 902 +#define RETVAL_LAST_BLOCK (-1)
pascal@886 903 +#define RETVAL_NOT_BZIP_DATA (-2)
pascal@886 904 +#define RETVAL_UNEXPECTED_INPUT_EOF (-3)
pascal@886 905 +#define RETVAL_UNEXPECTED_OUTPUT_EOF (-4)
pascal@886 906 +#define RETVAL_DATA_ERROR (-5)
pascal@886 907 +#define RETVAL_OUT_OF_MEMORY (-6)
pascal@886 908 +#define RETVAL_OBSOLETE_INPUT (-7)
pascal@886 909 +
pascal@886 910 +
pascal@886 911 +/* This is what we know about each Huffman coding group */
pascal@886 912 +struct group_data {
pascal@886 913 + /* We have an extra slot at the end of limit[] for a sentinal value. */
pascal@886 914 + int limit[MAX_HUFCODE_BITS+1],base[MAX_HUFCODE_BITS],permute[MAX_SYMBOLS];
pascal@886 915 + int minLen, maxLen;
pascal@886 916 +};
pascal@886 917 +
pascal@886 918 +/* Structure holding all the housekeeping data, including IO buffers and
pascal@886 919 + memory that persists between calls to bunzip */
pascal@886 920 +typedef struct {
pascal@886 921 + /* State for interrupting output loop */
pascal@886 922 + int writeCopies,writePos,writeRunCountdown,writeCount,writeCurrent;
pascal@886 923 + /* I/O tracking data (file handles, buffers, positions, etc.) */
pascal@886 924 + int (*fill)(void*,unsigned int);
pascal@886 925 + int inbufCount,inbufPos /*,outbufPos*/;
pascal@886 926 + unsigned char *inbuf /*,*outbuf*/;
pascal@886 927 + unsigned int inbufBitCount, inbufBits;
pascal@886 928 + /* The CRC values stored in the block header and calculated from the data */
pascal@886 929 + unsigned int crc32Table[256],headerCRC, totalCRC, writeCRC;
pascal@886 930 + /* Intermediate buffer and its size (in bytes) */
pascal@886 931 + unsigned int *dbuf, dbufSize;
pascal@886 932 + /* These things are a bit too big to go on the stack */
pascal@886 933 + unsigned char selectors[32768]; /* nSelectors=15 bits */
pascal@886 934 + struct group_data groups[MAX_GROUPS]; /* Huffman coding tables */
pascal@886 935 + int io_error; /* non-zero if we have IO error */
pascal@886 936 +} bunzip_data;
pascal@886 937 +
pascal@886 938 +
pascal@886 939 +/* Return the next nnn bits of input. All reads from the compressed input
pascal@886 940 + are done through this function. All reads are big endian */
pascal@886 941 +static unsigned int get_bits(bunzip_data *bd, char bits_wanted)
pascal@886 942 +{
pascal@886 943 + unsigned int bits=0;
pascal@886 944 +
pascal@886 945 + /* If we need to get more data from the byte buffer, do so. (Loop getting
pascal@886 946 + one byte at a time to enforce endianness and avoid unaligned access.) */
pascal@886 947 + while (bd->inbufBitCount<bits_wanted) {
pascal@886 948 + /* If we need to read more data from file into byte buffer, do so */
pascal@886 949 + if(bd->inbufPos==bd->inbufCount) {
pascal@886 950 + if(bd->io_error)
pascal@886 951 + return 0;
pascal@886 952 + if((bd->inbufCount = bd->fill(bd->inbuf, BZIP2_IOBUF_SIZE)) <= 0) {
pascal@886 953 + bd->io_error=RETVAL_UNEXPECTED_INPUT_EOF;
pascal@886 954 + return 0;
pascal@886 955 + }
pascal@886 956 + bd->inbufPos=0;
pascal@886 957 + }
pascal@886 958 + /* Avoid 32-bit overflow (dump bit buffer to top of output) */
pascal@886 959 + if(bd->inbufBitCount>=24) {
pascal@886 960 + bits=bd->inbufBits&((1<<bd->inbufBitCount)-1);
pascal@886 961 + bits_wanted-=bd->inbufBitCount;
pascal@886 962 + bits<<=bits_wanted;
pascal@886 963 + bd->inbufBitCount=0;
pascal@886 964 + }
pascal@886 965 + /* Grab next 8 bits of input from buffer. */
pascal@886 966 + bd->inbufBits=(bd->inbufBits<<8)|bd->inbuf[bd->inbufPos++];
pascal@886 967 + bd->inbufBitCount+=8;
pascal@886 968 + }
pascal@886 969 + /* Calculate result */
pascal@886 970 + bd->inbufBitCount-=bits_wanted;
pascal@886 971 + bits|=(bd->inbufBits>>bd->inbufBitCount)&((1<<bits_wanted)-1);
pascal@886 972 +
pascal@886 973 + return bits;
pascal@886 974 +}
pascal@886 975 +
pascal@886 976 +/* Unpacks the next block and sets up for the inverse burrows-wheeler step. */
pascal@886 977 +
pascal@886 978 +static int get_next_block(bunzip_data *bd)
pascal@886 979 +{
pascal@886 980 + struct group_data *hufGroup=NULL;
pascal@886 981 + int *base=NULL;
pascal@886 982 + int *limit=NULL;
pascal@886 983 + int dbufCount,nextSym,dbufSize,groupCount,selector,
pascal@886 984 + i,j,k,t,runPos,symCount,symTotal,nSelectors,byteCount[256];
pascal@886 985 + unsigned char uc, symToByte[256], mtfSymbol[256], *selectors;
pascal@886 986 + unsigned int *dbuf,origPtr;
pascal@886 987 +
pascal@886 988 + dbuf=bd->dbuf;
pascal@886 989 + dbufSize=bd->dbufSize;
pascal@886 990 + selectors=bd->selectors;
pascal@886 991 +
pascal@886 992 + /* Read in header signature and CRC, then validate signature.
pascal@886 993 + (last block signature means CRC is for whole file, return now) */
pascal@886 994 + i = get_bits(bd,24);
pascal@886 995 + j = get_bits(bd,24);
pascal@886 996 + bd->headerCRC=get_bits(bd,32);
pascal@886 997 + if ((i == 0x177245) && (j == 0x385090)) return RETVAL_LAST_BLOCK;
pascal@886 998 + if ((i != 0x314159) || (j != 0x265359)) return RETVAL_NOT_BZIP_DATA;
pascal@886 999 + /* We can add support for blockRandomised if anybody complains. There was
pascal@886 1000 + some code for this in busybox 1.0.0-pre3, but nobody ever noticed that
pascal@886 1001 + it didn't actually work. */
pascal@886 1002 + if(get_bits(bd,1)) return RETVAL_OBSOLETE_INPUT;
pascal@886 1003 + if((origPtr=get_bits(bd,24)) > dbufSize) return RETVAL_DATA_ERROR;
pascal@886 1004 + /* mapping table: if some byte values are never used (encoding things
pascal@886 1005 + like ascii text), the compression code removes the gaps to have fewer
pascal@886 1006 + symbols to deal with, and writes a sparse bitfield indicating which
pascal@886 1007 + values were present. We make a translation table to convert the symbols
pascal@886 1008 + back to the corresponding bytes. */
pascal@886 1009 + t=get_bits(bd, 16);
pascal@886 1010 + symTotal=0;
pascal@886 1011 + for (i=0;i<16;i++) {
pascal@886 1012 + if(t&(1<<(15-i))) {
pascal@886 1013 + k=get_bits(bd,16);
pascal@886 1014 + for(j=0;j<16;j++)
pascal@886 1015 + if(k&(1<<(15-j))) symToByte[symTotal++]=(16*i)+j;
pascal@886 1016 + }
pascal@886 1017 + }
pascal@886 1018 + /* How many different Huffman coding groups does this block use? */
pascal@886 1019 + groupCount=get_bits(bd,3);
pascal@886 1020 + if (groupCount<2 || groupCount>MAX_GROUPS) return RETVAL_DATA_ERROR;
pascal@886 1021 + /* nSelectors: Every GROUP_SIZE many symbols we select a new Huffman coding
pascal@886 1022 + group. Read in the group selector list, which is stored as MTF encoded
pascal@886 1023 + bit runs. (MTF=Move To Front, as each value is used it's moved to the
pascal@886 1024 + start of the list.) */
pascal@886 1025 + if(!(nSelectors=get_bits(bd, 15))) return RETVAL_DATA_ERROR;
pascal@886 1026 + for(i=0; i<groupCount; i++) mtfSymbol[i] = i;
pascal@886 1027 + for(i=0; i<nSelectors; i++) {
pascal@886 1028 + /* Get next value */
pascal@886 1029 + for(j=0;get_bits(bd,1);j++) if (j>=groupCount) return RETVAL_DATA_ERROR;
pascal@886 1030 + /* Decode MTF to get the next selector */
pascal@886 1031 + uc = mtfSymbol[j];
pascal@886 1032 + for(;j;j--) mtfSymbol[j] = mtfSymbol[j-1];
pascal@886 1033 + mtfSymbol[0]=selectors[i]=uc;
pascal@886 1034 + }
pascal@886 1035 + /* Read the Huffman coding tables for each group, which code for symTotal
pascal@886 1036 + literal symbols, plus two run symbols (RUNA, RUNB) */
pascal@886 1037 + symCount=symTotal+2;
pascal@886 1038 + for (j=0; j<groupCount; j++) {
pascal@886 1039 + unsigned char length[MAX_SYMBOLS],temp[MAX_HUFCODE_BITS+1];
pascal@886 1040 + int minLen, maxLen, pp;
pascal@886 1041 + /* Read Huffman code lengths for each symbol. They're stored in
pascal@886 1042 + a way similar to mtf; record a starting value for the first symbol,
pascal@886 1043 + and an offset from the previous value for everys symbol after that.
pascal@886 1044 + (Subtracting 1 before the loop and then adding it back at the end is
pascal@886 1045 + an optimization that makes the test inside the loop simpler: symbol
pascal@886 1046 + length 0 becomes negative, so an unsigned inequality catches it.) */
pascal@886 1047 + t=get_bits(bd, 5)-1;
pascal@886 1048 + for (i = 0; i < symCount; i++) {
pascal@886 1049 + for(;;) {
pascal@886 1050 + if (((unsigned)t) > (MAX_HUFCODE_BITS-1))
pascal@886 1051 + return RETVAL_DATA_ERROR;
pascal@886 1052 + /* If first bit is 0, stop. Else second bit indicates whether
pascal@886 1053 + to increment or decrement the value. Optimization: grab 2
pascal@886 1054 + bits and unget the second if the first was 0. */
pascal@886 1055 + k = get_bits(bd,2);
pascal@886 1056 + if (k < 2) {
pascal@886 1057 + bd->inbufBitCount++;
pascal@886 1058 + break;
pascal@886 1059 + }
pascal@886 1060 + /* Add one if second bit 1, else subtract 1. Avoids if/else */
pascal@886 1061 + t+=(((k+1)&2)-1);
pascal@886 1062 + }
pascal@886 1063 + /* Correct for the initial -1, to get the final symbol length */
pascal@886 1064 + length[i]=t+1;
pascal@886 1065 + }
pascal@886 1066 + /* Find largest and smallest lengths in this group */
pascal@886 1067 + minLen=maxLen=length[0];
pascal@886 1068 + for(i = 1; i < symCount; i++) {
pascal@886 1069 + if(length[i] > maxLen) maxLen = length[i];
pascal@886 1070 + else if(length[i] < minLen) minLen = length[i];
pascal@886 1071 + }
pascal@886 1072 + /* Calculate permute[], base[], and limit[] tables from length[].
pascal@886 1073 + *
pascal@886 1074 + * permute[] is the lookup table for converting Huffman coded symbols
pascal@886 1075 + * into decoded symbols. base[] is the amount to subtract from the
pascal@886 1076 + * value of a Huffman symbol of a given length when using permute[].
pascal@886 1077 + *
pascal@886 1078 + * limit[] indicates the largest numerical value a symbol with a given
pascal@886 1079 + * number of bits can have. This is how the Huffman codes can vary in
pascal@886 1080 + * length: each code with a value>limit[length] needs another bit.
pascal@886 1081 + */
pascal@886 1082 + hufGroup=bd->groups+j;
pascal@886 1083 + hufGroup->minLen = minLen;
pascal@886 1084 + hufGroup->maxLen = maxLen;
pascal@886 1085 + /* Note that minLen can't be smaller than 1, so we adjust the base
pascal@886 1086 + and limit array pointers so we're not always wasting the first
pascal@886 1087 + entry. We do this again when using them (during symbol decoding).*/
pascal@886 1088 + base=hufGroup->base-1;
pascal@886 1089 + limit=hufGroup->limit-1;
pascal@886 1090 + /* Calculate permute[]. Concurently, initialize temp[] and limit[]. */
pascal@886 1091 + pp=0;
pascal@886 1092 + for(i=minLen;i<=maxLen;i++) {
pascal@886 1093 + temp[i]=limit[i]=0;
pascal@886 1094 + for(t=0;t<symCount;t++)
pascal@886 1095 + if(length[t]==i) hufGroup->permute[pp++] = t;
pascal@886 1096 + }
pascal@886 1097 + /* Count symbols coded for at each bit length */
pascal@886 1098 + for (i=0;i<symCount;i++) temp[length[i]]++;
pascal@886 1099 + /* Calculate limit[] (the largest symbol-coding value at each bit
pascal@886 1100 + * length, which is (previous limit<<1)+symbols at this level), and
pascal@886 1101 + * base[] (number of symbols to ignore at each bit length, which is
pascal@886 1102 + * limit minus the cumulative count of symbols coded for already). */
pascal@886 1103 + pp=t=0;
pascal@886 1104 + for (i=minLen; i<maxLen; i++) {
pascal@886 1105 + pp+=temp[i];
pascal@886 1106 + /* We read the largest possible symbol size and then unget bits
pascal@886 1107 + after determining how many we need, and those extra bits could
pascal@886 1108 + be set to anything. (They're noise from future symbols.) At
pascal@886 1109 + each level we're really only interested in the first few bits,
pascal@886 1110 + so here we set all the trailing to-be-ignored bits to 1 so they
pascal@886 1111 + don't affect the value>limit[length] comparison. */
pascal@886 1112 + limit[i]= (pp << (maxLen - i)) - 1;
pascal@886 1113 + pp<<=1;
pascal@886 1114 + base[i+1]=pp-(t+=temp[i]);
pascal@886 1115 + }
pascal@886 1116 + limit[maxLen+1] = INT_MAX; /* Sentinal value for reading next sym. */
pascal@886 1117 + limit[maxLen]=pp+temp[maxLen]-1;
pascal@886 1118 + base[minLen]=0;
pascal@886 1119 + }
pascal@886 1120 + /* We've finished reading and digesting the block header. Now read this
pascal@886 1121 + block's Huffman coded symbols from the file and undo the Huffman coding
pascal@886 1122 + and run length encoding, saving the result into dbuf[dbufCount++]=uc */
pascal@886 1123 +
pascal@886 1124 + /* Initialize symbol occurrence counters and symbol Move To Front table */
pascal@886 1125 + for(i=0;i<256;i++) {
pascal@886 1126 + byteCount[i] = 0;
pascal@886 1127 + mtfSymbol[i]=(unsigned char)i;
pascal@886 1128 + }
pascal@886 1129 + /* Loop through compressed symbols. */
pascal@886 1130 + runPos=dbufCount=symCount=selector=0;
pascal@886 1131 + for(;;) {
pascal@886 1132 + /* Determine which Huffman coding group to use. */
pascal@886 1133 + if(!(symCount--)) {
pascal@886 1134 + symCount=GROUP_SIZE-1;
pascal@886 1135 + if(selector>=nSelectors) return RETVAL_DATA_ERROR;
pascal@886 1136 + hufGroup=bd->groups+selectors[selector++];
pascal@886 1137 + base=hufGroup->base-1;
pascal@886 1138 + limit=hufGroup->limit-1;
pascal@886 1139 + }
pascal@886 1140 + /* Read next Huffman-coded symbol. */
pascal@886 1141 + /* Note: It is far cheaper to read maxLen bits and back up than it is
pascal@886 1142 + to read minLen bits and then an additional bit at a time, testing
pascal@886 1143 + as we go. Because there is a trailing last block (with file CRC),
pascal@886 1144 + there is no danger of the overread causing an unexpected EOF for a
pascal@886 1145 + valid compressed file. As a further optimization, we do the read
pascal@886 1146 + inline (falling back to a call to get_bits if the buffer runs
pascal@886 1147 + dry). The following (up to got_huff_bits:) is equivalent to
pascal@886 1148 + j=get_bits(bd,hufGroup->maxLen);
pascal@886 1149 + */
pascal@886 1150 + while (bd->inbufBitCount<hufGroup->maxLen) {
pascal@886 1151 + if(bd->inbufPos==bd->inbufCount) {
pascal@886 1152 + j = get_bits(bd,hufGroup->maxLen);
pascal@886 1153 + goto got_huff_bits;
pascal@886 1154 + }
pascal@886 1155 + bd->inbufBits=(bd->inbufBits<<8)|bd->inbuf[bd->inbufPos++];
pascal@886 1156 + bd->inbufBitCount+=8;
pascal@886 1157 + };
pascal@886 1158 + bd->inbufBitCount-=hufGroup->maxLen;
pascal@886 1159 + j = (bd->inbufBits>>bd->inbufBitCount)&((1<<hufGroup->maxLen)-1);
pascal@886 1160 +got_huff_bits:
pascal@886 1161 + /* Figure how how many bits are in next symbol and unget extras */
pascal@886 1162 + i=hufGroup->minLen;
pascal@886 1163 + while(j>limit[i]) ++i;
pascal@886 1164 + bd->inbufBitCount += (hufGroup->maxLen - i);
pascal@886 1165 + /* Huffman decode value to get nextSym (with bounds checking) */
pascal@886 1166 + if ((i > hufGroup->maxLen)
pascal@886 1167 + || (((unsigned)(j=(j>>(hufGroup->maxLen-i))-base[i]))
pascal@886 1168 + >= MAX_SYMBOLS))
pascal@886 1169 + return RETVAL_DATA_ERROR;
pascal@886 1170 + nextSym = hufGroup->permute[j];
pascal@886 1171 + /* We have now decoded the symbol, which indicates either a new literal
pascal@886 1172 + byte, or a repeated run of the most recent literal byte. First,
pascal@886 1173 + check if nextSym indicates a repeated run, and if so loop collecting
pascal@886 1174 + how many times to repeat the last literal. */
pascal@886 1175 + if (((unsigned)nextSym) <= SYMBOL_RUNB) { /* RUNA or RUNB */
pascal@886 1176 + /* If this is the start of a new run, zero out counter */
pascal@886 1177 + if(!runPos) {
pascal@886 1178 + runPos = 1;
pascal@886 1179 + t = 0;
pascal@886 1180 + }
pascal@886 1181 + /* Neat trick that saves 1 symbol: instead of or-ing 0 or 1 at
pascal@886 1182 + each bit position, add 1 or 2 instead. For example,
pascal@886 1183 + 1011 is 1<<0 + 1<<1 + 2<<2. 1010 is 2<<0 + 2<<1 + 1<<2.
pascal@886 1184 + You can make any bit pattern that way using 1 less symbol than
pascal@886 1185 + the basic or 0/1 method (except all bits 0, which would use no
pascal@886 1186 + symbols, but a run of length 0 doesn't mean anything in this
pascal@886 1187 + context). Thus space is saved. */
pascal@886 1188 + t += (runPos << nextSym); /* +runPos if RUNA; +2*runPos if RUNB */
pascal@886 1189 + runPos <<= 1;
pascal@886 1190 + continue;
pascal@886 1191 + }
pascal@886 1192 + /* When we hit the first non-run symbol after a run, we now know
pascal@886 1193 + how many times to repeat the last literal, so append that many
pascal@886 1194 + copies to our buffer of decoded symbols (dbuf) now. (The last
pascal@886 1195 + literal used is the one at the head of the mtfSymbol array.) */
pascal@886 1196 + if(runPos) {
pascal@886 1197 + runPos=0;
pascal@886 1198 + if(dbufCount+t>=dbufSize) return RETVAL_DATA_ERROR;
pascal@886 1199 +
pascal@886 1200 + uc = symToByte[mtfSymbol[0]];
pascal@886 1201 + byteCount[uc] += t;
pascal@886 1202 + while(t--) dbuf[dbufCount++]=uc;
pascal@886 1203 + }
pascal@886 1204 + /* Is this the terminating symbol? */
pascal@886 1205 + if(nextSym>symTotal) break;
pascal@886 1206 + /* At this point, nextSym indicates a new literal character. Subtract
pascal@886 1207 + one to get the position in the MTF array at which this literal is
pascal@886 1208 + currently to be found. (Note that the result can't be -1 or 0,
pascal@886 1209 + because 0 and 1 are RUNA and RUNB. But another instance of the
pascal@886 1210 + first symbol in the mtf array, position 0, would have been handled
pascal@886 1211 + as part of a run above. Therefore 1 unused mtf position minus
pascal@886 1212 + 2 non-literal nextSym values equals -1.) */
pascal@886 1213 + if(dbufCount>=dbufSize) return RETVAL_DATA_ERROR;
pascal@886 1214 + i = nextSym - 1;
pascal@886 1215 + uc = mtfSymbol[i];
pascal@886 1216 + /* Adjust the MTF array. Since we typically expect to move only a
pascal@886 1217 + * small number of symbols, and are bound by 256 in any case, using
pascal@886 1218 + * memmove here would typically be bigger and slower due to function
pascal@886 1219 + * call overhead and other assorted setup costs. */
pascal@886 1220 + do {
pascal@886 1221 + mtfSymbol[i] = mtfSymbol[i-1];
pascal@886 1222 + } while (--i);
pascal@886 1223 + mtfSymbol[0] = uc;
pascal@886 1224 + uc=symToByte[uc];
pascal@886 1225 + /* We have our literal byte. Save it into dbuf. */
pascal@886 1226 + byteCount[uc]++;
pascal@886 1227 + dbuf[dbufCount++] = (unsigned int)uc;
pascal@886 1228 + }
pascal@886 1229 + /* At this point, we've read all the Huffman-coded symbols (and repeated
pascal@886 1230 + runs) for this block from the input stream, and decoded them into the
pascal@886 1231 + intermediate buffer. There are dbufCount many decoded bytes in dbuf[].
pascal@886 1232 + Now undo the Burrows-Wheeler transform on dbuf.
pascal@886 1233 + See http://dogma.net/markn/articles/bwt/bwt.htm
pascal@886 1234 + */
pascal@886 1235 + /* Turn byteCount into cumulative occurrence counts of 0 to n-1. */
pascal@886 1236 + j=0;
pascal@886 1237 + for(i=0;i<256;i++) {
pascal@886 1238 + k=j+byteCount[i];
pascal@886 1239 + byteCount[i] = j;
pascal@886 1240 + j=k;
pascal@886 1241 + }
pascal@886 1242 + /* Figure out what order dbuf would be in if we sorted it. */
pascal@886 1243 + for (i=0;i<dbufCount;i++) {
pascal@886 1244 + uc=(unsigned char)(dbuf[i] & 0xff);
pascal@886 1245 + dbuf[byteCount[uc]] |= (i << 8);
pascal@886 1246 + byteCount[uc]++;
pascal@886 1247 + }
pascal@886 1248 + /* Decode first byte by hand to initialize "previous" byte. Note that it
pascal@886 1249 + doesn't get output, and if the first three characters are identical
pascal@886 1250 + it doesn't qualify as a run (hence writeRunCountdown=5). */
pascal@886 1251 + if(dbufCount) {
pascal@886 1252 + if(origPtr>=dbufCount) return RETVAL_DATA_ERROR;
pascal@886 1253 + bd->writePos=dbuf[origPtr];
pascal@886 1254 + bd->writeCurrent=(unsigned char)(bd->writePos&0xff);
pascal@886 1255 + bd->writePos>>=8;
pascal@886 1256 + bd->writeRunCountdown=5;
pascal@886 1257 + }
pascal@886 1258 + bd->writeCount=dbufCount;
pascal@886 1259 +
pascal@886 1260 + return RETVAL_OK;
pascal@886 1261 +}
pascal@886 1262 +
pascal@886 1263 +/* Undo burrows-wheeler transform on intermediate buffer to produce output.
pascal@886 1264 + If start_bunzip was initialized with out_fd=-1, then up to len bytes of
pascal@886 1265 + data are written to outbuf. Return value is number of bytes written or
pascal@886 1266 + error (all errors are negative numbers). If out_fd!=-1, outbuf and len
pascal@886 1267 + are ignored, data is written to out_fd and return is RETVAL_OK or error.
pascal@886 1268 +*/
pascal@886 1269 +
pascal@886 1270 +static int read_bunzip(bunzip_data *bd, char *outbuf, int len)
pascal@886 1271 +{
pascal@886 1272 + const unsigned int *dbuf;
pascal@886 1273 + int pos,xcurrent,previous,gotcount;
pascal@886 1274 +
pascal@886 1275 + /* If last read was short due to end of file, return last block now */
pascal@886 1276 + if(bd->writeCount<0) return bd->writeCount;
pascal@886 1277 +
pascal@886 1278 + gotcount = 0;
pascal@886 1279 + dbuf=bd->dbuf;
pascal@886 1280 + pos=bd->writePos;
pascal@886 1281 + xcurrent=bd->writeCurrent;
pascal@886 1282 +
pascal@886 1283 + /* We will always have pending decoded data to write into the output
pascal@886 1284 + buffer unless this is the very first call (in which case we haven't
pascal@886 1285 + Huffman-decoded a block into the intermediate buffer yet). */
pascal@886 1286 +
pascal@886 1287 + if (bd->writeCopies) {
pascal@886 1288 + /* Inside the loop, writeCopies means extra copies (beyond 1) */
pascal@886 1289 + --bd->writeCopies;
pascal@886 1290 + /* Loop outputting bytes */
pascal@886 1291 + for(;;) {
pascal@886 1292 + /* If the output buffer is full, snapshot state and return */
pascal@886 1293 + if(gotcount >= len) {
pascal@886 1294 + bd->writePos=pos;
pascal@886 1295 + bd->writeCurrent=xcurrent;
pascal@886 1296 + bd->writeCopies++;
pascal@886 1297 + return len;
pascal@886 1298 + }
pascal@886 1299 + /* Write next byte into output buffer, updating CRC */
pascal@886 1300 + outbuf[gotcount++] = xcurrent;
pascal@886 1301 + bd->writeCRC=(((bd->writeCRC)<<8)
pascal@886 1302 + ^bd->crc32Table[((bd->writeCRC)>>24)^xcurrent]);
pascal@886 1303 + /* Loop now if we're outputting multiple copies of this byte */
pascal@886 1304 + if (bd->writeCopies) {
pascal@886 1305 + --bd->writeCopies;
pascal@886 1306 + continue;
pascal@886 1307 + }
pascal@886 1308 +decode_next_byte:
pascal@886 1309 + if (!bd->writeCount--) break;
pascal@886 1310 + /* Follow sequence vector to undo Burrows-Wheeler transform */
pascal@886 1311 + previous=xcurrent;
pascal@886 1312 + pos=dbuf[pos];
pascal@886 1313 + xcurrent=pos&0xff;
pascal@886 1314 + pos>>=8;
pascal@886 1315 + /* After 3 consecutive copies of the same byte, the 4th is a repeat
pascal@886 1316 + count. We count down from 4 instead
pascal@886 1317 + * of counting up because testing for non-zero is faster */
pascal@886 1318 + if(--bd->writeRunCountdown) {
pascal@886 1319 + if(xcurrent!=previous) bd->writeRunCountdown=4;
pascal@886 1320 + } else {
pascal@886 1321 + /* We have a repeated run, this byte indicates the count */
pascal@886 1322 + bd->writeCopies=xcurrent;
pascal@886 1323 + xcurrent=previous;
pascal@886 1324 + bd->writeRunCountdown=5;
pascal@886 1325 + /* Sometimes there are just 3 bytes (run length 0) */
pascal@886 1326 + if(!bd->writeCopies) goto decode_next_byte;
pascal@886 1327 + /* Subtract the 1 copy we'd output anyway to get extras */
pascal@886 1328 + --bd->writeCopies;
pascal@886 1329 + }
pascal@886 1330 + }
pascal@886 1331 + /* Decompression of this block completed successfully */
pascal@886 1332 + bd->writeCRC=~bd->writeCRC;
pascal@886 1333 + bd->totalCRC=((bd->totalCRC<<1) | (bd->totalCRC>>31)) ^ bd->writeCRC;
pascal@886 1334 + /* If this block had a CRC error, force file level CRC error. */
pascal@886 1335 + if(bd->writeCRC!=bd->headerCRC) {
pascal@886 1336 + bd->totalCRC=bd->headerCRC+1;
pascal@886 1337 + return RETVAL_LAST_BLOCK;
pascal@886 1338 + }
pascal@886 1339 + }
pascal@886 1340 +
pascal@886 1341 + /* Refill the intermediate buffer by Huffman-decoding next block of input */
pascal@886 1342 + /* (previous is just a convenient unused temp variable here) */
pascal@886 1343 + previous=get_next_block(bd);
pascal@886 1344 + if(previous) {
pascal@886 1345 + bd->writeCount=previous;
pascal@886 1346 + return (previous!=RETVAL_LAST_BLOCK) ? previous : gotcount;
pascal@886 1347 + }
pascal@886 1348 + bd->writeCRC=0xffffffffUL;
pascal@886 1349 + pos=bd->writePos;
pascal@886 1350 + xcurrent=bd->writeCurrent;
pascal@886 1351 + goto decode_next_byte;
pascal@886 1352 +}
pascal@886 1353 +
pascal@886 1354 +static int nofill(void *buf,unsigned int len) {
pascal@886 1355 + return -1;
pascal@886 1356 +}
pascal@886 1357 +
pascal@886 1358 +/* Allocate the structure, read file header. If in_fd==-1, inbuf must contain
pascal@886 1359 + a complete bunzip file (len bytes long). If in_fd!=-1, inbuf and len are
pascal@886 1360 + ignored, and data is read from file handle into temporary buffer. */
pascal@886 1361 +static int start_bunzip(bunzip_data **bdp, void *inbuf, int len,
pascal@886 1362 + int (*fill)(void*,unsigned int))
pascal@886 1363 +{
pascal@886 1364 + bunzip_data *bd;
pascal@886 1365 + unsigned int i,j,c;
pascal@886 1366 + const unsigned int BZh0=(((unsigned int)'B')<<24)+(((unsigned int)'Z')<<16)
pascal@886 1367 + +(((unsigned int)'h')<<8)+(unsigned int)'0';
pascal@886 1368 +
pascal@886 1369 + /* Figure out how much data to allocate */
pascal@886 1370 + i=sizeof(bunzip_data);
pascal@886 1371 +
pascal@886 1372 + /* Allocate bunzip_data. Most fields initialize to zero. */
pascal@886 1373 + bd=*bdp=malloc(i);
pascal@886 1374 + memset(bd,0,sizeof(bunzip_data));
pascal@886 1375 + /* Setup input buffer */
pascal@886 1376 + bd->inbuf=inbuf;
pascal@886 1377 + bd->inbufCount=len;
pascal@886 1378 + if(fill != NULL)
pascal@886 1379 + bd->fill=fill;
pascal@886 1380 + else
pascal@886 1381 + bd->fill=nofill;
pascal@886 1382 +
pascal@886 1383 + /* Init the CRC32 table (big endian) */
pascal@886 1384 + for(i=0;i<256;i++) {
pascal@886 1385 + c=i<<24;
pascal@886 1386 + for(j=8;j;j--)
pascal@886 1387 + c=c&0x80000000 ? (c<<1)^0x04c11db7 : (c<<1);
pascal@886 1388 + bd->crc32Table[i]=c;
pascal@886 1389 + }
pascal@886 1390 +
pascal@886 1391 + /* Ensure that file starts with "BZh['1'-'9']." */
pascal@886 1392 + i = get_bits(bd,32);
pascal@886 1393 + if (((unsigned int)(i-BZh0-1)) >= 9) return RETVAL_NOT_BZIP_DATA;
pascal@886 1394 +
pascal@886 1395 + /* Fourth byte (ascii '1'-'9'), indicates block size in units of 100k of
pascal@886 1396 + uncompressed data. Allocate intermediate buffer for block. */
pascal@886 1397 + bd->dbufSize=100000*(i-BZh0);
pascal@886 1398 +
pascal@886 1399 + bd->dbuf=large_malloc(bd->dbufSize * sizeof(int));
pascal@886 1400 + return RETVAL_OK;
pascal@886 1401 +}
pascal@886 1402 +
pascal@886 1403 +/* Example usage: decompress src_fd to dst_fd. (Stops at end of bzip data,
pascal@886 1404 + not end of file.) */
pascal@886 1405 +STATIC int bunzip2(char *inbuf, int len,
pascal@886 1406 + int(*fill)(void*,unsigned int),
pascal@886 1407 + int(*writebb)(char*,unsigned int),
pascal@886 1408 + int *pos)
pascal@886 1409 +{
pascal@886 1410 + char *outbuf;
pascal@886 1411 + bunzip_data *bd;
pascal@886 1412 + int i;
pascal@886 1413 +
pascal@886 1414 + outbuf=malloc(BZIP2_IOBUF_SIZE);
pascal@886 1415 + if(!(i=start_bunzip(&bd,inbuf,len,fill))) {
pascal@886 1416 + for(;;) {
pascal@886 1417 + if((i=read_bunzip(bd,outbuf,BZIP2_IOBUF_SIZE)) <= 0) break;
pascal@886 1418 + if(i!=writebb(outbuf,i)) {
pascal@886 1419 + i=RETVAL_UNEXPECTED_OUTPUT_EOF;
pascal@886 1420 + break;
pascal@886 1421 + }
pascal@886 1422 + }
pascal@886 1423 + }
pascal@886 1424 + /* Check CRC and release memory */
pascal@886 1425 + if(i==RETVAL_LAST_BLOCK) {
pascal@886 1426 + if (bd->headerCRC!=bd->totalCRC) {
pascal@886 1427 + error("Data integrity error when decompressing.");
pascal@886 1428 + } else {
pascal@886 1429 + i=RETVAL_OK;
pascal@886 1430 + }
pascal@886 1431 + }
pascal@886 1432 + else if (i==RETVAL_UNEXPECTED_OUTPUT_EOF) {
pascal@886 1433 + error("Compressed file ends unexpectedly");
pascal@886 1434 + }
pascal@886 1435 + if(bd->dbuf) large_free(bd->dbuf);
pascal@886 1436 + if(pos)
pascal@886 1437 + *pos = bd->inbufPos;
pascal@886 1438 + free(bd);
pascal@886 1439 + free(outbuf);
pascal@886 1440 +
pascal@886 1441 + return i;
pascal@886 1442 +}
pascal@886 1443 +
pascal@886 1444
pascal@886 1445 --- linux-2.6.25.5/lib/decompress_unlzma.c
pascal@886 1446 +++ linux-2.6.25.5/lib/decompress_unlzma.c
pascal@936 1447 @@ -0,0 +1,616 @@
pascal@886 1448 +/* Lzma decompressor for Linux kernel. Shamelessly snarfed
pascal@886 1449 + * from busybox 1.1.1
pascal@886 1450 + *
pascal@886 1451 + * Linux kernel adaptation
pascal@886 1452 + * Copyright (C) 2006 Alain <alain@knaff.lu>
pascal@886 1453 + *
pascal@886 1454 + * Based on small lzma deflate implementation/Small range coder
pascal@886 1455 + * implementation for lzma.
pascal@886 1456 + * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
pascal@886 1457 + *
pascal@886 1458 + * Based on LzmaDecode.c from the LZMA SDK 4.22 (http://www.7-zip.org/)
pascal@886 1459 + * Copyright (C) 1999-2005 Igor Pavlov
pascal@886 1460 + *
pascal@886 1461 + * Copyrights of the parts, see headers below.
pascal@886 1462 + *
pascal@886 1463 + *
pascal@886 1464 + * This program is free software; you can redistribute it and/or
pascal@886 1465 + * modify it under the terms of the GNU Lesser General Public
pascal@886 1466 + * License as published by the Free Software Foundation; either
pascal@886 1467 + * version 2.1 of the License, or (at your option) any later version.
pascal@886 1468 + *
pascal@886 1469 + * This program is distributed in the hope that it will be useful,
pascal@886 1470 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
pascal@886 1471 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
pascal@886 1472 + * Lesser General Public License for more details.
pascal@886 1473 + *
pascal@886 1474 + * You should have received a copy of the GNU Lesser General Public
pascal@886 1475 + * License along with this library; if not, write to the Free Software
pascal@886 1476 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
pascal@886 1477 + */
pascal@886 1478 +
pascal@886 1479 +#ifndef STATIC
pascal@886 1480 +
pascal@886 1481 +#include <linux/kernel.h>
pascal@886 1482 +#include <linux/fs.h>
pascal@886 1483 +#include <linux/string.h>
pascal@886 1484 +
pascal@886 1485 +#ifdef TEST
pascal@886 1486 +#include "test.h"
pascal@886 1487 +#else
pascal@886 1488 +#include <linux/vmalloc.h>
pascal@886 1489 +#endif
pascal@886 1490 +
pascal@886 1491 +static void __init *large_malloc(size_t size)
pascal@886 1492 +{
pascal@886 1493 + return vmalloc(size);
pascal@886 1494 +}
pascal@886 1495 +
pascal@886 1496 +static void __init large_free(void *where)
pascal@886 1497 +{
pascal@886 1498 + vfree(where);
pascal@886 1499 +}
pascal@886 1500 +
pascal@886 1501 +#ifndef TEST
pascal@886 1502 +static void __init error(char *x)
pascal@886 1503 +{
pascal@886 1504 + printk(KERN_ERR "%s\n", x);
pascal@886 1505 +}
pascal@886 1506 +
pascal@886 1507 +#endif
pascal@886 1508 +
pascal@886 1509 +#define STATIC /**/
pascal@886 1510 +
pascal@886 1511 +#endif
pascal@886 1512 +
pascal@886 1513 +#define CONFIG_FEATURE_LZMA_FAST
pascal@886 1514 +#include <linux/decompress_unlzma.h>
pascal@886 1515 +
pascal@886 1516 +#define MIN(a,b) (((a)<(b))?(a):(b))
pascal@886 1517 +
pascal@886 1518 +static long long read_int(unsigned char *ptr, int size)
pascal@886 1519 +{
pascal@886 1520 + int i;
pascal@886 1521 + long long ret=0;
pascal@886 1522 +
pascal@886 1523 + for(i=0; i<size; i++) {
pascal@886 1524 + ret = (ret << 8) | ptr[size-i-1];
pascal@886 1525 + }
pascal@886 1526 + return ret;
pascal@886 1527 +}
pascal@886 1528 +
pascal@886 1529 +#define ENDIAN_CONVERT(x) x=(typeof(x))read_int((unsigned char*)&x,sizeof(x))
pascal@886 1530 +
pascal@886 1531 +
pascal@886 1532 +/* Small range coder implementation for lzma.
pascal@886 1533 + * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
pascal@886 1534 + *
pascal@886 1535 + * Based on LzmaDecode.c from the LZMA SDK 4.22 (http://www.7-zip.org/)
pascal@886 1536 + * Copyright (c) 1999-2005 Igor Pavlov
pascal@886 1537 + */
pascal@886 1538 +
pascal@886 1539 +#ifndef always_inline
pascal@886 1540 +# if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >0)
pascal@886 1541 +# define always_inline __attribute__((always_inline)) inline
pascal@886 1542 +# else
pascal@886 1543 +# define always_inline inline
pascal@886 1544 +# endif
pascal@886 1545 +#endif
pascal@886 1546 +
pascal@886 1547 +#ifdef CONFIG_FEATURE_LZMA_FAST
pascal@886 1548 +# define speed_inline always_inline
pascal@886 1549 +#else
pascal@886 1550 +# define speed_inline
pascal@886 1551 +#endif
pascal@886 1552 +
pascal@886 1553 +
pascal@886 1554 +typedef struct {
pascal@886 1555 + int (*fill)(void*,unsigned int);
pascal@886 1556 + uint8_t *ptr;
pascal@886 1557 + uint8_t *buffer;
pascal@886 1558 + uint8_t *buffer_end;
pascal@886 1559 + int buffer_size;
pascal@886 1560 + uint32_t code;
pascal@886 1561 + uint32_t range;
pascal@886 1562 + uint32_t bound;
pascal@886 1563 +} rc_t;
pascal@886 1564 +
pascal@886 1565 +
pascal@886 1566 +#define RC_TOP_BITS 24
pascal@886 1567 +#define RC_MOVE_BITS 5
pascal@886 1568 +#define RC_MODEL_TOTAL_BITS 11
pascal@886 1569 +
pascal@886 1570 +
pascal@886 1571 +/* Called twice: once at startup and once in rc_normalize() */
pascal@886 1572 +static void rc_read(rc_t * rc)
pascal@886 1573 +{
pascal@936 1574 + if (!rc->buffer_size) return;
pascal@936 1575 + if (rc->fill) {
pascal@936 1576 + rc->buffer_size = rc->fill((char*)rc->buffer, LZMA_IOBUF_SIZE);
pascal@936 1577 + rc->ptr = rc->buffer;
pascal@936 1578 + rc->buffer_end = rc->buffer + rc->buffer_size;
pascal@936 1579 + if (rc->buffer_size > 0) return;
pascal@936 1580 + }
pascal@936 1581 + error("unexpected EOF");
pascal@936 1582 + rc->buffer_size = 0;
pascal@886 1583 +}
pascal@886 1584 +
pascal@886 1585 +/* Called once */
pascal@886 1586 +static always_inline void rc_init(rc_t * rc, int (*fill)(void*,unsigned int),
pascal@886 1587 + char *buffer, int buffer_size)
pascal@886 1588 +{
pascal@886 1589 + rc->fill = fill;
pascal@886 1590 + rc->buffer = (uint8_t *)buffer;
pascal@886 1591 + rc->buffer_size = buffer_size;
pascal@886 1592 + rc->buffer_end = rc->buffer + rc->buffer_size;
pascal@886 1593 + rc->ptr = rc->buffer;
pascal@886 1594 +
pascal@886 1595 + rc->code = 0;
pascal@886 1596 + rc->range = 0xFFFFFFFF;
pascal@886 1597 +}
pascal@886 1598 +
pascal@886 1599 +static always_inline void rc_init_code(rc_t * rc)
pascal@886 1600 +{
pascal@886 1601 + int i;
pascal@886 1602 +
pascal@886 1603 + for (i = 0; i < 5; i++) {
pascal@886 1604 + if (rc->ptr >= rc->buffer_end)
pascal@886 1605 + rc_read(rc);
pascal@886 1606 + rc->code = (rc->code << 8) | *rc->ptr++;
pascal@886 1607 + }
pascal@886 1608 +}
pascal@886 1609 +
pascal@886 1610 +/* Called twice, but one callsite is in speed_inline'd rc_is_bit_0_helper() */
pascal@886 1611 +static void rc_do_normalize(rc_t * rc)
pascal@886 1612 +{
pascal@886 1613 + if (rc->ptr >= rc->buffer_end)
pascal@886 1614 + rc_read(rc);
pascal@886 1615 + rc->range <<= 8;
pascal@886 1616 + rc->code = (rc->code << 8) | *rc->ptr++;
pascal@886 1617 +}
pascal@886 1618 +static always_inline void rc_normalize(rc_t * rc)
pascal@886 1619 +{
pascal@886 1620 + if (rc->range < (1 << RC_TOP_BITS)) {
pascal@886 1621 + rc_do_normalize(rc);
pascal@886 1622 + }
pascal@886 1623 +}
pascal@886 1624 +
pascal@886 1625 +/* Called 9 times */
pascal@886 1626 +/* Why rc_is_bit_0_helper exists?
pascal@886 1627 + * Because we want to always expose (rc->code < rc->bound) to optimizer
pascal@886 1628 + */
pascal@886 1629 +static speed_inline uint32_t rc_is_bit_0_helper(rc_t * rc, uint16_t * p)
pascal@886 1630 +{
pascal@886 1631 + rc_normalize(rc);
pascal@886 1632 + rc->bound = *p * (rc->range >> RC_MODEL_TOTAL_BITS);
pascal@886 1633 + return rc->bound;
pascal@886 1634 +}
pascal@886 1635 +static always_inline int rc_is_bit_0(rc_t * rc, uint16_t * p)
pascal@886 1636 +{
pascal@886 1637 + uint32_t t = rc_is_bit_0_helper(rc, p);
pascal@886 1638 + return rc->code < t;
pascal@886 1639 +}
pascal@886 1640 +
pascal@886 1641 +/* Called ~10 times, but very small, thus inlined */
pascal@886 1642 +static speed_inline void rc_update_bit_0(rc_t * rc, uint16_t * p)
pascal@886 1643 +{
pascal@886 1644 + rc->range = rc->bound;
pascal@886 1645 + *p += ((1 << RC_MODEL_TOTAL_BITS) - *p) >> RC_MOVE_BITS;
pascal@886 1646 +}
pascal@886 1647 +static speed_inline void rc_update_bit_1(rc_t * rc, uint16_t * p)
pascal@886 1648 +{
pascal@886 1649 + rc->range -= rc->bound;
pascal@886 1650 + rc->code -= rc->bound;
pascal@886 1651 + *p -= *p >> RC_MOVE_BITS;
pascal@886 1652 +}
pascal@886 1653 +
pascal@886 1654 +/* Called 4 times in unlzma loop */
pascal@886 1655 +static int rc_get_bit(rc_t * rc, uint16_t * p, int *symbol)
pascal@886 1656 +{
pascal@886 1657 + if (rc_is_bit_0(rc, p)) {
pascal@886 1658 + rc_update_bit_0(rc, p);
pascal@886 1659 + *symbol *= 2;
pascal@886 1660 + return 0;
pascal@886 1661 + } else {
pascal@886 1662 + rc_update_bit_1(rc, p);
pascal@886 1663 + *symbol = *symbol * 2 + 1;
pascal@886 1664 + return 1;
pascal@886 1665 + }
pascal@886 1666 +}
pascal@886 1667 +
pascal@886 1668 +/* Called once */
pascal@886 1669 +static always_inline int rc_direct_bit(rc_t * rc)
pascal@886 1670 +{
pascal@886 1671 + rc_normalize(rc);
pascal@886 1672 + rc->range >>= 1;
pascal@886 1673 + if (rc->code >= rc->range) {
pascal@886 1674 + rc->code -= rc->range;
pascal@886 1675 + return 1;
pascal@886 1676 + }
pascal@886 1677 + return 0;
pascal@886 1678 +}
pascal@886 1679 +
pascal@886 1680 +/* Called twice */
pascal@886 1681 +static speed_inline void
pascal@886 1682 +rc_bit_tree_decode(rc_t * rc, uint16_t * p, int num_levels, int *symbol)
pascal@886 1683 +{
pascal@886 1684 + int i = num_levels;
pascal@886 1685 +
pascal@886 1686 + *symbol = 1;
pascal@886 1687 + while (i--)
pascal@886 1688 + rc_get_bit(rc, p + *symbol, symbol);
pascal@886 1689 + *symbol -= 1 << num_levels;
pascal@886 1690 +}
pascal@886 1691 +
pascal@886 1692 +
pascal@886 1693 +/*
pascal@886 1694 + * Small lzma deflate implementation.
pascal@886 1695 + * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
pascal@886 1696 + *
pascal@886 1697 + * Based on LzmaDecode.c from the LZMA SDK 4.22 (http://www.7-zip.org/)
pascal@886 1698 + * Copyright (C) 1999-2005 Igor Pavlov
pascal@886 1699 + */
pascal@886 1700 +
pascal@886 1701 +
pascal@886 1702 +typedef struct {
pascal@886 1703 + uint8_t pos;
pascal@886 1704 + uint32_t dict_size;
pascal@886 1705 + uint64_t dst_size;
pascal@886 1706 +} __attribute__ ((packed)) lzma_header_t;
pascal@886 1707 +
pascal@886 1708 +
pascal@886 1709 +#define LZMA_BASE_SIZE 1846
pascal@886 1710 +#define LZMA_LIT_SIZE 768
pascal@886 1711 +
pascal@886 1712 +#define LZMA_NUM_POS_BITS_MAX 4
pascal@886 1713 +
pascal@886 1714 +#define LZMA_LEN_NUM_LOW_BITS 3
pascal@886 1715 +#define LZMA_LEN_NUM_MID_BITS 3
pascal@886 1716 +#define LZMA_LEN_NUM_HIGH_BITS 8
pascal@886 1717 +
pascal@886 1718 +#define LZMA_LEN_CHOICE 0
pascal@886 1719 +#define LZMA_LEN_CHOICE_2 (LZMA_LEN_CHOICE + 1)
pascal@886 1720 +#define LZMA_LEN_LOW (LZMA_LEN_CHOICE_2 + 1)
pascal@886 1721 +#define LZMA_LEN_MID (LZMA_LEN_LOW \
pascal@886 1722 + + (1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_LOW_BITS)))
pascal@886 1723 +#define LZMA_LEN_HIGH (LZMA_LEN_MID \
pascal@886 1724 + +(1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_MID_BITS)))
pascal@886 1725 +#define LZMA_NUM_LEN_PROBS (LZMA_LEN_HIGH + (1 << LZMA_LEN_NUM_HIGH_BITS))
pascal@886 1726 +
pascal@886 1727 +#define LZMA_NUM_STATES 12
pascal@886 1728 +#define LZMA_NUM_LIT_STATES 7
pascal@886 1729 +
pascal@886 1730 +#define LZMA_START_POS_MODEL_INDEX 4
pascal@886 1731 +#define LZMA_END_POS_MODEL_INDEX 14
pascal@886 1732 +#define LZMA_NUM_FULL_DISTANCES (1 << (LZMA_END_POS_MODEL_INDEX >> 1))
pascal@886 1733 +
pascal@886 1734 +#define LZMA_NUM_POS_SLOT_BITS 6
pascal@886 1735 +#define LZMA_NUM_LEN_TO_POS_STATES 4
pascal@886 1736 +
pascal@886 1737 +#define LZMA_NUM_ALIGN_BITS 4
pascal@886 1738 +
pascal@886 1739 +#define LZMA_MATCH_MIN_LEN 2
pascal@886 1740 +
pascal@886 1741 +#define LZMA_IS_MATCH 0
pascal@886 1742 +#define LZMA_IS_REP (LZMA_IS_MATCH + (LZMA_NUM_STATES <<LZMA_NUM_POS_BITS_MAX))
pascal@886 1743 +#define LZMA_IS_REP_G0 (LZMA_IS_REP + LZMA_NUM_STATES)
pascal@886 1744 +#define LZMA_IS_REP_G1 (LZMA_IS_REP_G0 + LZMA_NUM_STATES)
pascal@886 1745 +#define LZMA_IS_REP_G2 (LZMA_IS_REP_G1 + LZMA_NUM_STATES)
pascal@886 1746 +#define LZMA_IS_REP_0_LONG (LZMA_IS_REP_G2 + LZMA_NUM_STATES)
pascal@886 1747 +#define LZMA_POS_SLOT (LZMA_IS_REP_0_LONG \
pascal@886 1748 + + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX))
pascal@886 1749 +#define LZMA_SPEC_POS (LZMA_POS_SLOT \
pascal@886 1750 + +(LZMA_NUM_LEN_TO_POS_STATES << LZMA_NUM_POS_SLOT_BITS))
pascal@886 1751 +#define LZMA_ALIGN (LZMA_SPEC_POS \
pascal@886 1752 + + LZMA_NUM_FULL_DISTANCES - LZMA_END_POS_MODEL_INDEX)
pascal@886 1753 +#define LZMA_LEN_CODER (LZMA_ALIGN + (1 << LZMA_NUM_ALIGN_BITS))
pascal@886 1754 +#define LZMA_REP_LEN_CODER (LZMA_LEN_CODER + LZMA_NUM_LEN_PROBS)
pascal@886 1755 +#define LZMA_LITERAL (LZMA_REP_LEN_CODER + LZMA_NUM_LEN_PROBS)
pascal@886 1756 +
pascal@886 1757 +
pascal@886 1758 +STATIC int unlzma(char *inbuf, int in_len,
pascal@886 1759 + int(*fill)(void*,unsigned int),
pascal@886 1760 + int(*writebb)(char*,unsigned int),
pascal@886 1761 + int *posp)
pascal@886 1762 +{
pascal@886 1763 + lzma_header_t header;
pascal@886 1764 + int lc, pb, lp;
pascal@886 1765 + uint32_t pos_state_mask;
pascal@886 1766 + uint32_t literal_pos_mask;
pascal@886 1767 + uint32_t pos;
pascal@886 1768 + uint16_t *p;
pascal@886 1769 + uint16_t *prob;
pascal@886 1770 + uint16_t *prob_lit;
pascal@886 1771 + int num_bits;
pascal@886 1772 + int num_probs;
pascal@886 1773 + rc_t rc;
pascal@886 1774 + int i, mi;
pascal@886 1775 + uint8_t *buffer;
pascal@886 1776 + uint8_t previous_byte = 0;
pascal@886 1777 + size_t buffer_pos = 0, global_pos = 0;
pascal@886 1778 + int len = 0;
pascal@886 1779 + int state = 0;
pascal@886 1780 + int bufsize;
pascal@886 1781 + uint32_t rep0 = 1, rep1 = 1, rep2 = 1, rep3 = 1;
pascal@886 1782 +
pascal@886 1783 + rc_init(&rc, fill, inbuf, in_len);
pascal@886 1784 +
pascal@933 1785 + header.dict_size = (uint32_t) -1L;
pascal@933 1786 + header.dst_size = (uint64_t) -1LL;
pascal@886 1787 + if (inbuf && in_len > 0 && inbuf[0] == 0) {
pascal@886 1788 + const int LZMA_LC = 3, LZMA_LP = 0, LZMA_PB = 2;
pascal@886 1789 + header.pos = (LZMA_PB * 45) + (LZMA_LP * 5) + LZMA_LC;
pascal@886 1790 + rc.ptr++;
pascal@886 1791 + }
pascal@886 1792 + else {
pascal@886 1793 + int hdrsize = sizeof(header);
pascal@886 1794 + if (inbuf && in_len > 12 &&
pascal@886 1795 + (1 + * (unsigned long *) &inbuf[9]) > 1U)
pascal@886 1796 + hdrsize = 5;
pascal@886 1797 + for (i = 0; i < hdrsize; i++) {
pascal@886 1798 + if (rc.ptr >= rc.buffer_end)
pascal@886 1799 + rc_read(&rc);
pascal@886 1800 + ((unsigned char *)&header)[i] = *rc.ptr++;
pascal@886 1801 + }
pascal@886 1802 + }
pascal@886 1803 +
pascal@936 1804 + if (header.pos >= (9 * 5 * 5)) {
pascal@886 1805 + error("bad header");
pascal@936 1806 + return -1;
pascal@936 1807 + }
pascal@886 1808 +
pascal@886 1809 + mi = header.pos / 9;
pascal@886 1810 + lc = header.pos % 9;
pascal@886 1811 + pb = mi / 5;
pascal@886 1812 + lp = mi % 5;
pascal@886 1813 + pos_state_mask = (1 << pb) - 1;
pascal@886 1814 + literal_pos_mask = (1 << lp) - 1;
pascal@886 1815 +
pascal@886 1816 + ENDIAN_CONVERT(header.dict_size);
pascal@886 1817 + ENDIAN_CONVERT(header.dst_size);
pascal@886 1818 +
pascal@886 1819 + if (header.dict_size == 0)
pascal@886 1820 + header.dict_size = 1;
pascal@886 1821 +
pascal@886 1822 + bufsize = MIN(header.dst_size, header.dict_size);
pascal@886 1823 + buffer = (uint8_t *) posp;
pascal@886 1824 + if (writebb) buffer = large_malloc(bufsize);
pascal@886 1825 + if(buffer == NULL)
pascal@886 1826 + return -1;
pascal@886 1827 +
pascal@886 1828 + num_probs = LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp));
pascal@886 1829 + p = large_malloc(num_probs * sizeof(*p));
pascal@886 1830 + num_probs = LZMA_LITERAL + (LZMA_LIT_SIZE << (lc + lp));
pascal@886 1831 + for (i = 0; i < num_probs; i++)
pascal@886 1832 + p[i] = (1 << RC_MODEL_TOTAL_BITS) >> 1;
pascal@886 1833 +
pascal@886 1834 + rc_init_code(&rc);
pascal@886 1835 +
pascal@886 1836 + while (global_pos + buffer_pos < header.dst_size) {
pascal@886 1837 + int pos_state = (buffer_pos + global_pos) & pos_state_mask;
pascal@886 1838 +
pascal@886 1839 + prob =
pascal@886 1840 + p + LZMA_IS_MATCH + (state << LZMA_NUM_POS_BITS_MAX) + pos_state;
pascal@886 1841 + if (rc_is_bit_0(&rc, prob)) {
pascal@886 1842 + mi = 1;
pascal@886 1843 + rc_update_bit_0(&rc, prob);
pascal@886 1844 + prob = (p + LZMA_LITERAL + (LZMA_LIT_SIZE
pascal@886 1845 + * ((((buffer_pos + global_pos) & literal_pos_mask) << lc)
pascal@886 1846 + + (previous_byte >> (8 - lc)))));
pascal@886 1847 +
pascal@886 1848 + if (state >= LZMA_NUM_LIT_STATES) {
pascal@886 1849 + int match_byte;
pascal@886 1850 +
pascal@886 1851 + pos = buffer_pos - rep0;
pascal@936 1852 +
pascal@936 1853 + while (pos >= header.dict_size)
pascal@936 1854 + pos += header.dict_size;
pascal@936 1855 + if(pos >= bufsize) {
pascal@936 1856 + goto fail;
pascal@886 1857 + }
pascal@936 1858 +
pascal@886 1859 + match_byte = buffer[pos];
pascal@886 1860 + do {
pascal@886 1861 + int bit;
pascal@886 1862 +
pascal@886 1863 + match_byte <<= 1;
pascal@886 1864 + bit = match_byte & 0x100;
pascal@886 1865 + prob_lit = prob + 0x100 + bit + mi;
pascal@886 1866 + if (rc_get_bit(&rc, prob_lit, &mi)) {
pascal@886 1867 + if (!bit)
pascal@886 1868 + break;
pascal@886 1869 + } else {
pascal@886 1870 + if (bit)
pascal@886 1871 + break;
pascal@886 1872 + }
pascal@886 1873 + } while (mi < 0x100);
pascal@886 1874 + }
pascal@886 1875 + while (mi < 0x100) {
pascal@886 1876 + prob_lit = prob + mi;
pascal@886 1877 + rc_get_bit(&rc, prob_lit, &mi);
pascal@886 1878 + }
pascal@886 1879 + previous_byte = (uint8_t) mi;
pascal@886 1880 + if (state < 4)
pascal@886 1881 + state = 0;
pascal@886 1882 + else if (state < 10)
pascal@886 1883 + state -= 3;
pascal@886 1884 + else
pascal@886 1885 + state -= 6;
pascal@886 1886 + goto store_previous_byte;
pascal@886 1887 + } else {
pascal@886 1888 + int offset;
pascal@886 1889 + uint16_t *prob_len;
pascal@886 1890 +
pascal@886 1891 + rc_update_bit_1(&rc, prob);
pascal@886 1892 + prob = p + LZMA_IS_REP + state;
pascal@886 1893 + if (rc_is_bit_0(&rc, prob)) {
pascal@886 1894 + rc_update_bit_0(&rc, prob);
pascal@886 1895 + rep3 = rep2;
pascal@886 1896 + rep2 = rep1;
pascal@886 1897 + rep1 = rep0;
pascal@886 1898 + state = state < LZMA_NUM_LIT_STATES ? 0 : 3;
pascal@886 1899 + prob = p + LZMA_LEN_CODER;
pascal@886 1900 + } else {
pascal@886 1901 + rc_update_bit_1(&rc, prob);
pascal@886 1902 + prob = p + LZMA_IS_REP_G0 + state;
pascal@886 1903 + if (rc_is_bit_0(&rc, prob)) {
pascal@886 1904 + rc_update_bit_0(&rc, prob);
pascal@886 1905 + prob = (p + LZMA_IS_REP_0_LONG
pascal@886 1906 + + (state << LZMA_NUM_POS_BITS_MAX) + pos_state);
pascal@886 1907 + if (rc_is_bit_0(&rc, prob)) {
pascal@886 1908 + rc_update_bit_0(&rc, prob);
pascal@886 1909 +
pascal@886 1910 + state = state < LZMA_NUM_LIT_STATES ? 9 : 11;
pascal@886 1911 + pos = buffer_pos - rep0;
pascal@936 1912 +
pascal@936 1913 + while (pos >= header.dict_size)
pascal@936 1914 + pos += header.dict_size;
pascal@936 1915 + if(pos >= bufsize) {
pascal@936 1916 + goto fail;
pascal@886 1917 + }
pascal@936 1918 +
pascal@886 1919 + previous_byte = buffer[pos];
pascal@886 1920 + store_previous_byte:
pascal@936 1921 + if (!rc.buffer_size) goto eof;
pascal@886 1922 + buffer[buffer_pos++] = previous_byte;
pascal@886 1923 + if (writebb && buffer_pos == header.dict_size) {
pascal@886 1924 + buffer_pos = 0;
pascal@886 1925 + global_pos += header.dict_size;
pascal@886 1926 + writebb((char*)buffer, header.dict_size);
pascal@886 1927 + }
pascal@886 1928 + continue;
pascal@886 1929 + } else {
pascal@886 1930 + rc_update_bit_1(&rc, prob);
pascal@886 1931 + }
pascal@886 1932 + } else {
pascal@886 1933 + uint32_t distance;
pascal@886 1934 +
pascal@886 1935 + rc_update_bit_1(&rc, prob);
pascal@886 1936 + prob = p + LZMA_IS_REP_G1 + state;
pascal@886 1937 + if (rc_is_bit_0(&rc, prob)) {
pascal@886 1938 + rc_update_bit_0(&rc, prob);
pascal@886 1939 + distance = rep1;
pascal@886 1940 + } else {
pascal@886 1941 + rc_update_bit_1(&rc, prob);
pascal@886 1942 + prob = p + LZMA_IS_REP_G2 + state;
pascal@886 1943 + if (rc_is_bit_0(&rc, prob)) {
pascal@886 1944 + rc_update_bit_0(&rc, prob);
pascal@886 1945 + distance = rep2;
pascal@886 1946 + } else {
pascal@886 1947 + rc_update_bit_1(&rc, prob);
pascal@886 1948 + distance = rep3;
pascal@886 1949 + rep3 = rep2;
pascal@886 1950 + }
pascal@886 1951 + rep2 = rep1;
pascal@886 1952 + }
pascal@886 1953 + rep1 = rep0;
pascal@886 1954 + rep0 = distance;
pascal@886 1955 + }
pascal@886 1956 + state = state < LZMA_NUM_LIT_STATES ? 8 : 11;
pascal@886 1957 + prob = p + LZMA_REP_LEN_CODER;
pascal@886 1958 + }
pascal@886 1959 +
pascal@886 1960 + prob_len = prob + LZMA_LEN_CHOICE;
pascal@886 1961 + if (rc_is_bit_0(&rc, prob_len)) {
pascal@886 1962 + rc_update_bit_0(&rc, prob_len);
pascal@886 1963 + prob_len = (prob + LZMA_LEN_LOW
pascal@886 1964 + + (pos_state << LZMA_LEN_NUM_LOW_BITS));
pascal@886 1965 + offset = 0;
pascal@886 1966 + num_bits = LZMA_LEN_NUM_LOW_BITS;
pascal@886 1967 + } else {
pascal@886 1968 + rc_update_bit_1(&rc, prob_len);
pascal@886 1969 + prob_len = prob + LZMA_LEN_CHOICE_2;
pascal@886 1970 + if (rc_is_bit_0(&rc, prob_len)) {
pascal@886 1971 + rc_update_bit_0(&rc, prob_len);
pascal@886 1972 + prob_len = (prob + LZMA_LEN_MID
pascal@886 1973 + + (pos_state << LZMA_LEN_NUM_MID_BITS));
pascal@886 1974 + offset = 1 << LZMA_LEN_NUM_LOW_BITS;
pascal@886 1975 + num_bits = LZMA_LEN_NUM_MID_BITS;
pascal@886 1976 + } else {
pascal@886 1977 + rc_update_bit_1(&rc, prob_len);
pascal@886 1978 + prob_len = prob + LZMA_LEN_HIGH;
pascal@886 1979 + offset = ((1 << LZMA_LEN_NUM_LOW_BITS)
pascal@886 1980 + + (1 << LZMA_LEN_NUM_MID_BITS));
pascal@886 1981 + num_bits = LZMA_LEN_NUM_HIGH_BITS;
pascal@886 1982 + }
pascal@886 1983 + }
pascal@886 1984 + rc_bit_tree_decode(&rc, prob_len, num_bits, &len);
pascal@886 1985 + len += offset;
pascal@886 1986 +
pascal@886 1987 + if (state < 4) {
pascal@886 1988 + int pos_slot;
pascal@886 1989 +
pascal@886 1990 + state += LZMA_NUM_LIT_STATES;
pascal@886 1991 + prob =
pascal@886 1992 + p + LZMA_POS_SLOT +
pascal@886 1993 + ((len <
pascal@886 1994 + LZMA_NUM_LEN_TO_POS_STATES ? len :
pascal@886 1995 + LZMA_NUM_LEN_TO_POS_STATES - 1)
pascal@886 1996 + << LZMA_NUM_POS_SLOT_BITS);
pascal@886 1997 + rc_bit_tree_decode(&rc, prob, LZMA_NUM_POS_SLOT_BITS,
pascal@886 1998 + &pos_slot);
pascal@886 1999 + if (pos_slot >= LZMA_START_POS_MODEL_INDEX) {
pascal@886 2000 + num_bits = (pos_slot >> 1) - 1;
pascal@886 2001 + rep0 = 2 | (pos_slot & 1);
pascal@886 2002 + if (pos_slot < LZMA_END_POS_MODEL_INDEX) {
pascal@886 2003 + rep0 <<= num_bits;
pascal@886 2004 + prob = p + LZMA_SPEC_POS + rep0 - pos_slot - 1;
pascal@886 2005 + } else {
pascal@886 2006 + num_bits -= LZMA_NUM_ALIGN_BITS;
pascal@886 2007 + while (num_bits--)
pascal@886 2008 + rep0 = (rep0 << 1) | rc_direct_bit(&rc);
pascal@886 2009 + prob = p + LZMA_ALIGN;
pascal@886 2010 + rep0 <<= LZMA_NUM_ALIGN_BITS;
pascal@886 2011 + num_bits = LZMA_NUM_ALIGN_BITS;
pascal@886 2012 + }
pascal@886 2013 + i = 1;
pascal@886 2014 + mi = 1;
pascal@886 2015 + while (num_bits--) {
pascal@886 2016 + if (rc_get_bit(&rc, prob + mi, &mi))
pascal@886 2017 + rep0 |= i;
pascal@886 2018 + i <<= 1;
pascal@886 2019 + }
pascal@886 2020 + } else
pascal@886 2021 + rep0 = pos_slot;
pascal@886 2022 + if (++rep0 == 0)
pascal@886 2023 + break;
pascal@886 2024 + }
pascal@886 2025 +
pascal@886 2026 + len += LZMA_MATCH_MIN_LEN;
pascal@886 2027 +
pascal@936 2028 + if (!rc.buffer_size) goto eof;
pascal@886 2029 + do {
pascal@886 2030 + pos = buffer_pos - rep0;
pascal@936 2031 +
pascal@936 2032 + while (pos >= header.dict_size)
pascal@936 2033 + pos += header.dict_size;
pascal@936 2034 + if(pos >= bufsize) {
pascal@936 2035 + goto fail;
pascal@886 2036 + }
pascal@936 2037 +
pascal@886 2038 + previous_byte = buffer[pos];
pascal@886 2039 + buffer[buffer_pos++] = previous_byte;
pascal@886 2040 + if (writebb && buffer_pos == header.dict_size) {
pascal@886 2041 + buffer_pos = 0;
pascal@886 2042 + global_pos += header.dict_size;
pascal@886 2043 + writebb((char*)buffer, header.dict_size);
pascal@886 2044 + }
pascal@886 2045 + len--;
pascal@933 2046 + } while (len != 0 && (global_pos + buffer_pos) < header.dst_size);
pascal@886 2047 + }
pascal@886 2048 + }
pascal@936 2049 + eof:
pascal@886 2050 + if (writebb) {
pascal@886 2051 + writebb((char*)buffer, buffer_pos);
pascal@886 2052 + if(posp) {
pascal@886 2053 + *posp = rc.ptr-rc.buffer;
pascal@886 2054 + }
pascal@886 2055 + large_free(buffer);
pascal@886 2056 + }
pascal@886 2057 + large_free(p);
pascal@886 2058 + return 0;
pascal@886 2059 + fail:
pascal@886 2060 + if (writebb) large_free(buffer);
pascal@886 2061 + large_free(p);
pascal@886 2062 + return -1;
pascal@886 2063 +}
pascal@886 2064
pascal@886 2065 --- linux-2.6.25.5/lib/unlzma_syms.c
pascal@886 2066 +++ linux-2.6.25.5/lib/unlzma_syms.c
pascal@886 2067 @@ -0,0 +1,14 @@
pascal@886 2068 +/*
pascal@886 2069 + * linux/lib/unlzma_syms.c
pascal@886 2070 + *
pascal@886 2071 + * Exported symbols for the unlzma functionality.
pascal@886 2072 + *
pascal@886 2073 + */
pascal@886 2074 +
pascal@886 2075 +#include <linux/module.h>
pascal@886 2076 +#include <linux/init.h>
pascal@886 2077 +
pascal@886 2078 +#include <linux/decompress_unlzma.h>
pascal@886 2079 +
pascal@886 2080 +EXPORT_SYMBOL(unlzma);
pascal@886 2081 +MODULE_LICENSE("GPL");
pascal@886 2082
pascal@886 2083 --- linux-2.6.25.5/lib/Makefile
pascal@886 2084 +++ linux-2.6.25.5/lib/Makefile
pascal@886 2085 @@ -50,6 +50,9 @@ obj-$(CONFIG_CRC7) += crc7.o
pascal@886 2086 obj-$(CONFIG_LIBCRC32C) += libcrc32c.o
pascal@886 2087 obj-$(CONFIG_GENERIC_ALLOCATOR) += genalloc.o
pascal@886 2088
pascal@886 2089 +obj-$(CONFIG_RD_BZIP2) += decompress_bunzip2.o
pascal@886 2090 +obj-$(CONFIG_RD_LZMA) += decompress_unlzma.o unlzma_syms.o
pascal@886 2091 +
pascal@886 2092 obj-$(CONFIG_ZLIB_INFLATE) += zlib_inflate/
pascal@886 2093 obj-$(CONFIG_ZLIB_DEFLATE) += zlib_deflate/
pascal@886 2094 obj-$(CONFIG_REED_SOLOMON) += reed_solomon/
pascal@886 2095
pascal@886 2096 --- linux-2.6.25.5/scripts/Makefile.lib
pascal@886 2097 +++ linux-2.6.25.5/scripts/Makefile.lib
pascal@886 2098 @@ -172,4 +172,17 @@
pascal@886 2099 quiet_cmd_gzip = GZIP $@
pascal@886 2100 cmd_gzip = gzip -f -9 < $< > $@
pascal@886 2101
pascal@886 2102 +# Append size
pascal@886 2103 +size_append=perl -e 'print(pack("i",(stat($$ARGV[0]))[7]));'
pascal@886 2104
pascal@886 2105 +# Bzip2
pascal@886 2106 +# ---------------------------------------------------------------------------
pascal@886 2107 +
pascal@886 2108 +quiet_cmd_bzip2 = BZIP2 $@
pascal@886 2109 +cmd_bzip2 = (bzip2 -9 < $< ; $(size_append) $<) > $@
pascal@886 2110 +
pascal@886 2111 +# Lzma
pascal@886 2112 +# ---------------------------------------------------------------------------
pascal@886 2113 +
pascal@886 2114 +quiet_cmd_lzma = LZMA $@
pascal@886 2115 +cmd_lzma = (lzma e $< -so ; $(size_append) $<) >$@
pascal@886 2116
pascal@886 2117 --- linux-2.6.25.5/arch/x86/mm/init_32.c
pascal@886 2118 +++ linux-2.6.25.5/arch/x86/mm/init_32.c
pascal@886 2119 @@ -788,7 +788,8 @@
pascal@886 2120 free_page(addr);
pascal@886 2121 totalram_pages++;
pascal@886 2122 }
pascal@886 2123 - printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10);
pascal@886 2124 + if (what)
pascal@886 2125 + printk(KERN_INFO "Freeing %s: %luk freed\n", what, (end - begin) >> 10);
pascal@886 2126 #endif
pascal@886 2127 }
pascal@886 2128
pascal@886 2129 @@ -802,6 +803,6 @@
pascal@886 2130 #ifdef CONFIG_BLK_DEV_INITRD
pascal@886 2131 void free_initrd_mem(unsigned long start, unsigned long end)
pascal@886 2132 {
pascal@886 2133 - free_init_pages("initrd memory", start, end);
pascal@886 2134 + free_init_pages(NULL, start, end);
pascal@886 2135 }
pascal@886 2136 #endif