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