wok-6.x annotate linux/stuff/linux-lzma-2.6.22.9.u @ rev 2202

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