wok view linux/stuff/linux-lzma-2.6.24.2.u @ rev 224

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