1 --- a/arch/x86/boot/compressed/Makefile
2 +++ b/arch/x86/boot/compressed/Makefile
4 # create a compressed vmlinux image from the original vmlinux
7 -targets := vmlinux vmlinux.bin vmlinux.bin.gz head_$(BITS).o misc.o piggy.o
8 +targets := vmlinux vmlinux.bin vmlinux.bin.gz vmlinux.bin.bz2 vmlinux.bin.lzma head_$(BITS).o misc.o piggy.o
10 KBUILD_CFLAGS := -m$(BITS) -D__KERNEL__ $(LINUX_INCLUDE) -O2
11 KBUILD_CFLAGS += -fno-strict-aliasing -fPIC
12 @@ -47,9 +47,17 @@ ifeq ($(CONFIG_X86_32),y)
13 ifdef CONFIG_RELOCATABLE
14 $(obj)/vmlinux.bin.gz: $(obj)/vmlinux.bin.all FORCE
15 $(call if_changed,gzip)
16 +$(obj)/vmlinux.bin.bz2: $(obj)/vmlinux.bin.all FORCE
17 + $(call if_changed,bzip2)
18 +$(obj)/vmlinux.bin.lzma: $(obj)/vmlinux.bin.all FORCE
19 + $(call if_changed,lzma)
21 $(obj)/vmlinux.bin.gz: $(obj)/vmlinux.bin FORCE
22 $(call if_changed,gzip)
23 +$(obj)/vmlinux.bin.bz2: $(obj)/vmlinux.bin FORCE
24 + $(call if_changed,bzip2)
25 +$(obj)/vmlinux.bin.lzma: $(obj)/vmlinux.bin FORCE
26 + $(call if_changed,lzma)
28 LDFLAGS_piggy.o := -r --format binary --oformat elf32-i386 -T
30 @@ -60,5 +68,9 @@ $(obj)/vmlinux.bin.gz: $(obj)/vmlinux.bi
31 LDFLAGS_piggy.o := -r --format binary --oformat elf64-x86-64 -T
34 -$(obj)/piggy.o: $(obj)/vmlinux.scr $(obj)/vmlinux.bin.gz FORCE
35 +suffix_$(CONFIG_KERNEL_GZIP) = gz
36 +suffix_$(CONFIG_KERNEL_BZIP2) = bz2
37 +suffix_$(CONFIG_KERNEL_LZMA) = lzma
39 +$(obj)/piggy.o: $(obj)/vmlinux.scr $(obj)/vmlinux.bin.$(suffix_y) FORCE
41 --- a/arch/x86/boot/compressed/misc.c
42 +++ b/arch/x86/boot/compressed/misc.c
48 -#define OF(args) args
53 #define memzero(s, n) memset((s), 0, (n))
55 -typedef unsigned char uch;
56 -typedef unsigned short ush;
57 -typedef unsigned long ulg;
60 - * Window size must be at least 32k, and a power of two.
61 - * We don't actually have a window just a huge output buffer,
62 - * so we report a 2G window size, as that should always be
63 - * larger than our output buffer:
65 -#define WSIZE 0x80000000
68 -static unsigned char *inbuf;
70 -/* Sliding window buffer (and final output buffer): */
71 -static unsigned char *window;
73 -/* Valid bytes in inbuf: */
74 -static unsigned insize;
76 -/* Index of next byte to be processed in inbuf: */
77 -static unsigned inptr;
79 -/* Bytes in output buffer: */
80 -static unsigned outcnt;
83 -#define ASCII_FLAG 0x01 /* bit 0 set: file probably ASCII text */
84 -#define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gz file */
85 -#define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
86 -#define ORIG_NAM 0x08 /* bit 3 set: original file name present */
87 -#define COMMENT 0x10 /* bit 4 set: file comment present */
88 -#define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
89 -#define RESERVED 0xC0 /* bit 6, 7: reserved */
91 -#define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
93 -/* Diagnostic functions */
95 -# define Assert(cond, msg) do { if (!(cond)) error(msg); } while (0)
96 -# define Trace(x) do { fprintf x; } while (0)
97 -# define Tracev(x) do { if (verbose) fprintf x ; } while (0)
98 -# define Tracevv(x) do { if (verbose > 1) fprintf x ; } while (0)
99 -# define Tracec(c, x) do { if (verbose && (c)) fprintf x ; } while (0)
100 -# define Tracecv(c, x) do { if (verbose > 1 && (c)) fprintf x ; } while (0)
102 -# define Assert(cond, msg)
106 -# define Tracec(c, x)
107 -# define Tracecv(c, x)
110 -static int fill_inbuf(void);
111 -static void flush_window(void);
112 static void error(char *m);
115 @@ -189,11 +131,6 @@ static void error(char *m);
116 static struct boot_params *real_mode; /* Pointer to real-mode data */
119 -extern unsigned char input_data[];
120 -extern int input_len;
122 -static long bytes_out;
124 static void *memset(void *s, int c, unsigned n);
125 static void *memcpy(void *dest, const void *src, unsigned n);
127 @@ -213,7 +150,19 @@ static char *vidmem;
129 static int lines, cols;
133 +#ifdef CONFIG_KERNEL_GZIP
134 #include "../../../../lib/inflate.c"
137 +#ifdef CONFIG_KERNEL_BZIP2
138 +#include "../../../../lib/decompress_bunzip2.c"
141 +#ifdef CONFIG_KERNEL_LZMA
142 +#include "../../../../lib/decompress_unlzma.c"
145 static void scroll(void)
147 @@ -293,38 +242,6 @@ static void *memcpy(void *dest, const vo
151 -/* ===========================================================================
152 - * Fill the input buffer. This is called only when the buffer is empty
153 - * and at least one byte is really needed.
155 -static int fill_inbuf(void)
157 - error("ran out of input data");
161 -/* ===========================================================================
162 - * Write the output window window[0..outcnt-1] and update crc and bytes_out.
163 - * (Used for the decompressed data only.)
165 -static void flush_window(void)
167 - /* With my window equal to my output buffer
168 - * I only need to compute the crc here.
170 - unsigned long c = crc; /* temporary variable */
172 - unsigned char *in, ch;
175 - for (n = 0; n < outcnt; n++) {
177 - c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
180 - bytes_out += (unsigned long)outcnt;
184 static void error(char *x)
186 @@ -407,12 +324,8 @@ asmlinkage void decompress_kernel(void *
187 lines = real_mode->screen_info.orig_video_lines;
188 cols = real_mode->screen_info.orig_video_cols;
190 - window = output; /* Output buffer (Normally at 1M) */
191 free_mem_ptr = heap; /* Heap */
192 free_mem_end_ptr = heap + BOOT_HEAP_SIZE;
193 - inbuf = input_data; /* Input buffer */
194 - insize = input_len;
198 if ((unsigned long)output & (__KERNEL_ALIGN - 1))
199 @@ -430,10 +343,9 @@ asmlinkage void decompress_kernel(void *
205 putstr("\nDecompressing Linux... ");
207 + decompress(input_data, input_len, NULL, NULL, output, NULL, error);
210 putstr("done.\nBooting the kernel.\n");
211 --- a/arch/x86/include/asm/boot.h
212 +++ b/arch/x86/include/asm/boot.h
214 + (CONFIG_PHYSICAL_ALIGN - 1)) \
215 & ~(CONFIG_PHYSICAL_ALIGN - 1))
217 +#if (defined CONFIG_KERNEL_BZIP2)
218 +#define BOOT_HEAP_SIZE 0x400000
222 #define BOOT_HEAP_SIZE 0x7000
223 -#define BOOT_STACK_SIZE 0x4000
225 #define BOOT_HEAP_SIZE 0x4000
230 +#ifdef CONFIG_X86_64
231 +#define BOOT_STACK_SIZE 0x4000
233 #define BOOT_STACK_SIZE 0x1000
236 --- a/drivers/block/Kconfig
237 +++ b/drivers/block/Kconfig
238 @@ -358,6 +358,30 @@ config BLK_DEV_XIP
239 will prevent RAM block device backing store memory from being
240 allocated from highmem (only a problem for highmem systems).
243 + bool "Initial ramdisk compressed using bzip2"
245 + depends on BLK_DEV_INITRD=y
247 + Support loading of a bzip2 encoded initial ramdisk or cpio buffer
251 + bool "Initial ramdisk compressed using lzma"
253 + depends on BLK_DEV_INITRD=y
255 + Support loading of a lzma encoded initial ramdisk or cpio buffer
259 + bool "Initial ramdisk compressed using gzip"
261 + depends on BLK_DEV_INITRD=y
263 + Support loading of a gzip encoded initial ramdisk or cpio buffer.
267 tristate "Packet writing on CD/DVD media"
270 +++ b/include/linux/decompress/bunzip2.h
272 +#ifndef DECOMPRESS_BUNZIP2_H
273 +#define DECOMPRESS_BUNZIP2_H
275 +int bunzip2(unsigned char *inbuf, int len,
276 + int(*fill)(void*, unsigned int),
277 + int(*flush)(void*, unsigned int),
278 + unsigned char *output,
280 + void(*error)(char *x));
283 +++ b/include/linux/decompress/generic.h
285 +#ifndef DECOMPRESS_GENERIC_H
286 +#define DECOMPRESS_GENERIC_H
288 +/* Minimal chunksize to be read.
289 + *Bzip2 prefers at least 4096
290 + *Lzma prefers 0x10000 */
291 +#define COMPR_IOBUF_SIZE 4096
293 +typedef int (*decompress_fn) (unsigned char *inbuf, int len,
294 + int(*fill)(void*, unsigned int),
295 + int(*writebb)(void*, unsigned int),
296 + unsigned char *output,
298 + void(*error)(char *x));
300 +/* inbuf - input buffer
301 + *len - len of pre-read data in inbuf
302 + *fill - function to fill inbuf if empty
303 + *writebb - function to write out outbug
304 + *posp - if non-null, input position (number of bytes read) will be
307 + *If len != 0, the inbuf is initialized (with as much data), and fill
308 + *should not be called
309 + *If len = 0, the inbuf is allocated, but empty. Its size is IOBUF_SIZE
310 + *fill should be called (repeatedly...) to read data, at most IOBUF_SIZE
316 +++ b/include/linux/decompress/inflate.h
321 +/* Other housekeeping constants */
322 +#define INBUFSIZ 4096
324 +int gunzip(unsigned char *inbuf, int len,
325 + int(*fill)(void*, unsigned int),
326 + int(*flush)(void*, unsigned int),
327 + unsigned char *output,
329 + void(*error_fn)(char *x));
332 +++ b/include/linux/decompress/mm.h
337 + * Memory management for pre-boot and ramdisk uncompressors
339 + * Authors: Alain Knaff <alain@knaff.lu>
343 +#ifndef DECOMPR_MM_H
344 +#define DECOMPR_MM_H
348 +/* Code active when included from pre-boot environment: */
350 +/* A trivial malloc implementation, adapted from
351 + * malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994
353 +static unsigned long malloc_ptr;
354 +static int malloc_count;
356 +static void *malloc(int size)
361 + error("Malloc error");
363 + malloc_ptr = free_mem_ptr;
365 + malloc_ptr = (malloc_ptr + 3) & ~3; /* Align */
367 + p = (void *)malloc_ptr;
368 + malloc_ptr += size;
370 + if (free_mem_end_ptr && malloc_ptr >= free_mem_end_ptr)
371 + error("Out of memory");
377 +static void free(void *where)
381 + malloc_ptr = free_mem_ptr;
384 +#define large_malloc(a) malloc(a)
385 +#define large_free(a) free(a)
387 +#define set_error_fn(x)
394 +/* Code active when compiled standalone for use when loading ramdisk: */
396 +#include <linux/kernel.h>
397 +#include <linux/fs.h>
398 +#include <linux/string.h>
399 +#include <linux/vmalloc.h>
401 +/* Use defines rather than static inline in order to avoid spurious
402 + * warnings when not needed (indeed large_malloc / large_free are not
403 + * needed by inflate */
405 +#define malloc(a) kmalloc(a, GFP_KERNEL)
406 +#define free(a) kfree(a)
408 +#define large_malloc(a) vmalloc(a)
409 +#define large_free(a) vfree(a)
411 +static void(*error)(char *m);
412 +#define set_error_fn(x) error = x;
418 +#include <linux/init.h>
422 +#endif /* DECOMPR_MM_H */
424 +++ b/include/linux/decompress/unlzma.h
426 +#ifndef DECOMPRESS_UNLZMA_H
427 +#define DECOMPRESS_UNLZMA_H
429 +int unlzma(unsigned char *, int,
430 + int(*fill)(void*, unsigned int),
431 + int(*flush)(void*, unsigned int),
432 + unsigned char *output,
434 + void(*error)(char *x)
440 @@ -101,6 +101,56 @@ config LOCALVERSION_AUTO
442 which is done within the script "scripts/setlocalversion".)
445 + prompt "Kernel compression mode"
446 + default KERNEL_GZIP
448 + The linux kernel is a kind of self-extracting executable.
449 + Several compression algorithms are available, which differ
450 + in efficiency, compression and decompression speed.
451 + Compression speed is only relevant when building a kernel.
452 + Decompression speed is relevant at each boot.
454 + If you have any problems with bzip2 or lzma compressed
455 + kernels, mail me (Alain Knaff) <alain@knaff.lu>. (An older
456 + version of this functionality (bzip2 only), for 2.4, was
457 + supplied by Christian Ludwig)
459 + High compression options are mostly useful for users, who
460 + are low on disk space (embedded systems), but for whom ram
463 + If in doubt, select 'gzip'
468 + The old and tried gzip compression. Its compression ratio is
469 + the poorest among the 3 choices; however its speed (both
470 + compression and decompression) is the fastest.
475 + Its compression ratio and speed is intermediate.
476 + Decompression speed is slowest among the 3.
477 + The kernel size is about 10 per cent smaller with bzip2,
478 + in comparison to gzip.
479 + Bzip2 uses a large amount of memory. For modern kernels
480 + you will need at least 8MB RAM or more for booting.
485 + The most recent compression algorithm.
486 + Its ratio is best, decompression speed is between the other
487 + 2. Compression is slowest.
488 + The kernel size is about 33 per cent smaller with lzma,
489 + in comparison to gzip.
495 bool "Support for paging of anonymous memory (swap)"
496 depends on MMU && BLOCK
497 --- a/init/do_mounts_rd.c
498 +++ b/init/do_mounts_rd.c
501 #include "do_mounts.h"
503 +#include <linux/decompress/generic.h>
505 +#include <linux/decompress/bunzip2.h>
506 +#include <linux/decompress/unlzma.h>
507 +#include <linux/decompress/inflate.h>
509 int __initdata rd_prompt = 1;/* 1 = prompt for RAM disk, 0 = don't prompt */
511 static int __init prompt_ramdisk(char *str)
512 @@ -29,7 +35,7 @@ static int __init ramdisk_start_setup(ch
514 __setup("ramdisk_start=", ramdisk_start_setup);
516 -static int __init crd_load(int in_fd, int out_fd);
517 +static int __init crd_load(int in_fd, int out_fd, decompress_fn deco);
520 * This routine tries to find a RAM disk image to load, and returns the
521 @@ -46,7 +52,7 @@ static int __init crd_load(int in_fd, in
525 -identify_ramdisk_image(int fd, int start_block)
526 +identify_ramdisk_image(int fd, int start_block, decompress_fn *decompressor)
528 const int size = 512;
529 struct minix_super_block *minixsb;
530 @@ -74,6 +80,7 @@ identify_ramdisk_image(int fd, int start
531 sys_lseek(fd, start_block * BLOCK_SIZE, 0);
532 sys_read(fd, buf, size);
534 +#ifdef CONFIG_RD_GZIP
536 * If it matches the gzip magic numbers, return 0
538 @@ -81,9 +88,39 @@ identify_ramdisk_image(int fd, int start
540 "RAMDISK: Compressed image found at block %d\n",
542 + *decompressor = gunzip;
548 +#ifdef CONFIG_RD_BZIP2
550 + * If it matches the bzip2 magic numbers, return -1
552 + if (buf[0] == 0x42 && (buf[1] == 0x5a)) {
554 + "RAMDISK: Bzipped image found at block %d\n",
556 + *decompressor = bunzip2;
562 +#ifdef CONFIG_RD_LZMA
564 + * If it matches the lzma magic numbers, return -1
566 + if (buf[0] == 0x5d && (buf[1] == 0x00)) {
568 + "RAMDISK: Lzma image found at block %d\n",
570 + *decompressor = unlzma;
576 /* romfs is at block zero too */
577 if (romfsb->word0 == ROMSB_WORD0 &&
578 @@ -156,6 +193,7 @@ int __init rd_load_image(char *from)
579 int nblocks, i, disk;
581 unsigned short rotate = 0;
582 + decompress_fn decompressor = NULL;
583 #if !defined(CONFIG_S390) && !defined(CONFIG_PPC_ISERIES)
584 char rotator[4] = { '|' , '/' , '-' , '\\' };
586 @@ -168,12 +206,12 @@ int __init rd_load_image(char *from)
590 - nblocks = identify_ramdisk_image(in_fd, rd_image_start);
591 + nblocks = identify_ramdisk_image(in_fd, rd_image_start, &decompressor);
596 - if (crd_load(in_fd, out_fd) == 0)
597 + if (crd_load(in_fd, out_fd, decompressor) == 0)
598 goto successful_load;
601 @@ -272,138 +310,48 @@ int __init rd_load_disk(int n)
602 return rd_load_image("/dev/root");
606 - * gzip declarations
609 -#define OF(args) args
612 -#define memzero(s, n) memset ((s), 0, (n))
615 -typedef unsigned char uch;
616 -typedef unsigned short ush;
617 -typedef unsigned long ulg;
619 -#define INBUFSIZ 4096
620 -#define WSIZE 0x8000 /* window size--must be a power of two, and */
621 - /* at least 32K for zip's deflate method */
626 -static unsigned insize; /* valid bytes in inbuf */
627 -static unsigned inptr; /* index of next byte to be processed in inbuf */
628 -static unsigned outcnt; /* bytes in output buffer */
629 static int exit_code;
630 -static int unzip_error;
631 -static long bytes_out;
632 +static int decompress_error;
633 static int crd_infd, crd_outfd;
635 -#define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
637 -/* Diagnostic functions (stubbed out) */
638 -#define Assert(cond,msg)
643 -#define Tracecv(c,x)
645 -#define STATIC static
648 -static int __init fill_inbuf(void);
649 -static void __init flush_window(void);
650 -static void __init error(char *m);
652 -#define NO_INFLATE_MALLOC
654 -#include "../lib/inflate.c"
656 -/* ===========================================================================
657 - * Fill the input buffer. This is called only when the buffer is empty
658 - * and at least one byte is really needed.
659 - * Returning -1 does not guarantee that gunzip() will ever return.
661 -static int __init fill_inbuf(void)
662 +static int __init compr_fill(void *buf, unsigned int len)
664 - if (exit_code) return -1;
666 - insize = sys_read(crd_infd, inbuf, INBUFSIZ);
668 - error("RAMDISK: ran out of compressed data");
675 + int r = sys_read(crd_infd, buf, len);
677 + printk(KERN_ERR "RAMDISK: error while reading compressed data");
679 + printk(KERN_ERR "RAMDISK: EOF while reading compressed data");
683 -/* ===========================================================================
684 - * Write the output window window[0..outcnt-1] and update crc and bytes_out.
685 - * (Used for the decompressed data only.)
687 -static void __init flush_window(void)
688 +static int __init compr_flush(void *window, unsigned int outcnt)
690 - ulg c = crc; /* temporary variable */
691 - unsigned n, written;
694 - written = sys_write(crd_outfd, window, outcnt);
695 - if (written != outcnt && unzip_error == 0) {
696 - printk(KERN_ERR "RAMDISK: incomplete write (%d != %d) %ld\n",
697 - written, outcnt, bytes_out);
701 - for (n = 0; n < outcnt; n++) {
703 - c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
706 - bytes_out += (ulg)outcnt;
708 + int written = sys_write(crd_outfd, window, outcnt);
709 + if (written != outcnt) {
710 + if (decompress_error == 0)
712 + "RAMDISK: incomplete write (%d != %d)\n",
714 + decompress_error = 1;
720 static void __init error(char *x)
722 printk(KERN_ERR "%s\n", x);
725 + decompress_error = 1;
728 -static int __init crd_load(int in_fd, int out_fd)
729 +static int __init crd_load(int in_fd, int out_fd, decompress_fn deco)
733 - insize = 0; /* valid bytes in inbuf */
734 - inptr = 0; /* index of next byte to be processed in inbuf */
735 - outcnt = 0; /* bytes in output buffer */
738 - crc = (ulg)0xffffffffL; /* shift register contents */
742 - inbuf = kmalloc(INBUFSIZ, GFP_KERNEL);
744 - printk(KERN_ERR "RAMDISK: Couldn't allocate gzip buffer\n");
747 - window = kmalloc(WSIZE, GFP_KERNEL);
749 - printk(KERN_ERR "RAMDISK: Couldn't allocate gzip window\n");
756 + result = deco(NULL, 0, compr_fill, compr_flush, NULL, NULL, error);
757 + if (decompress_error)
763 --- a/init/initramfs.c
764 +++ b/init/initramfs.c
765 @@ -389,11 +389,14 @@ static int __init write_buffer(char *buf
769 -static void __init flush_buffer(char *buf, unsigned len)
771 +static int __init flush_buffer(void *bufv, unsigned len)
773 + char *buf = (char *) bufv;
779 while ((written = write_buffer(buf, len)) < len && !message) {
780 char c = buf[written];
782 @@ -407,73 +410,14 @@ static void __init flush_buffer(char *bu
784 error("junk in compressed archive");
790 - * gzip declarations
793 -#define OF(args) args
796 -#define memzero(s, n) memset ((s), 0, (n))
798 +static unsigned my_inptr; /* index of next byte to be processed in inbuf */
800 -typedef unsigned char uch;
801 -typedef unsigned short ush;
802 -typedef unsigned long ulg;
804 -#define WSIZE 0x8000 /* window size--must be a power of two, and */
805 - /* at least 32K for zip's deflate method */
810 -static unsigned insize; /* valid bytes in inbuf */
811 -static unsigned inptr; /* index of next byte to be processed in inbuf */
812 -static unsigned outcnt; /* bytes in output buffer */
813 -static long bytes_out;
815 -#define get_byte() (inptr < insize ? inbuf[inptr++] : -1)
817 -/* Diagnostic functions (stubbed out) */
818 -#define Assert(cond,msg)
823 -#define Tracecv(c,x)
825 -#define STATIC static
828 -static void __init flush_window(void);
829 -static void __init error(char *m);
831 -#define NO_INFLATE_MALLOC
833 -#include "../lib/inflate.c"
835 -/* ===========================================================================
836 - * Write the output window window[0..outcnt-1] and update crc and bytes_out.
837 - * (Used for the decompressed data only.)
839 -static void __init flush_window(void)
841 - ulg c = crc; /* temporary variable */
845 - flush_buffer(window, outcnt);
847 - for (n = 0; n < outcnt; n++) {
849 - c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
852 - bytes_out += (ulg)outcnt;
855 +#include <linux/decompress/bunzip2.h>
856 +#include <linux/decompress/unlzma.h>
857 +#include <linux/decompress/inflate.h>
859 static char * __init unpack_to_rootfs(char *buf, unsigned len, int check_only)
861 @@ -482,9 +426,10 @@ static char * __init unpack_to_rootfs(ch
862 header_buf = kmalloc(110, GFP_KERNEL);
863 symlink_buf = kmalloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1, GFP_KERNEL);
864 name_buf = kmalloc(N_ALIGN(PATH_MAX), GFP_KERNEL);
865 - window = kmalloc(WSIZE, GFP_KERNEL);
866 - if (!window || !header_buf || !symlink_buf || !name_buf)
868 + if (!header_buf || !symlink_buf || !name_buf)
869 panic("can't allocate buffers");
874 @@ -504,22 +449,38 @@ static char * __init unpack_to_rootfs(ch
881 - outcnt = 0; /* bytes in output buffer */
883 - crc = (ulg)0xffffffffL; /* shift register contents */
886 + if (!gunzip(buf, len, NULL, flush_buffer, NULL,
887 + &my_inptr, error) &&
891 +#ifdef CONFIG_RD_BZIP2
892 + message = NULL; /* Zero out message, or else cpio will think an error has already occured */
893 + if (!bunzip2(buf, len, NULL, flush_buffer, NULL,
894 + &my_inptr, error) < 0
901 +#ifdef CONFIG_RD_LZMA
902 + message = NULL; /* Zero out message, or else cpio will think an error has already occured */
903 + if (!unlzma(buf, len, NULL, flush_buffer, NULL,
904 + &my_inptr, error) < 0
912 - error("junk in gzipped archive");
913 - this_header = saved_offset + inptr;
916 + error("junk in compressed archive");
917 + this_header = saved_offset + my_inptr;
928 @@ -11,7 +11,8 @@ lib-y := ctype.o string.o vsprintf.o cmd
929 rbtree.o radix-tree.o dump_stack.o \
930 idr.o int_sqrt.o extable.o prio_tree.o \
931 sha1.o irq_regs.o reciprocal_div.o argv_split.o \
932 - proportions.o prio_heap.o ratelimit.o show_mem.o
933 + proportions.o prio_heap.o ratelimit.o show_mem.o \
934 + inflate.o decompress_bunzip2.o decompress_unlzma.o
936 lib-$(CONFIG_MMU) += ioremap.o
937 lib-$(CONFIG_SMP) += cpumask.o
939 +++ b/lib/decompress_bunzip2.c
941 +/* vi: set sw = 4 ts = 4: */
942 +/* Small bzip2 deflate implementation, by Rob Landley (rob@landley.net).
944 + Based on bzip2 decompression code by Julian R Seward (jseward@acm.org),
945 + which also acknowledges contributions by Mike Burrows, David Wheeler,
946 + Peter Fenwick, Alistair Moffat, Radford Neal, Ian H. Witten,
947 + Robert Sedgewick, and Jon L. Bentley.
949 + This code is licensed under the LGPLv2:
950 + LGPL (http://www.gnu.org/copyleft/lgpl.html
954 + Size and speed optimizations by Manuel Novoa III (mjn3@codepoet.org).
956 + More efficient reading of Huffman codes, a streamlined read_bunzip()
957 + function, and various other tweaks. In (limited) tests, approximately
958 + 20% faster than bzcat on x86 and about 10% faster on arm.
960 + Note that about 2/3 of the time is spent in read_unzip() reversing
961 + the Burrows-Wheeler transformation. Much of that time is delay
962 + resulting from cache misses.
964 + I would ask that anyone benefiting from this work, especially those
965 + using it in commercial products, consider making a donation to my local
966 + non-profit hospice organization in the name of the woman I loved, who
967 + passed away Feb. 12, 2003.
969 + In memory of Toni W. Hagan
971 + Hospice of Acadiana, Inc.
972 + 2600 Johnston St., Suite 200
973 + Lafayette, LA 70503-3240
975 + Phone (337) 232-1234 or 1-800-738-2226
978 + http://www.hospiceacadiana.com/
984 + Made it fit for running in Linux Kernel by Alain Knaff (alain@knaff.lu)
989 +#include <linux/decompress/bunzip2.h>
990 +#endif /* !STATIC */
992 +#include <linux/decompress/mm.h>
995 +#define INT_MAX 0x7fffffff
998 +/* Constants for Huffman coding */
999 +#define MAX_GROUPS 6
1000 +#define GROUP_SIZE 50 /* 64 would have been more efficient */
1001 +#define MAX_HUFCODE_BITS 20 /* Longest Huffman code allowed */
1002 +#define MAX_SYMBOLS 258 /* 256 literals + RUNA + RUNB */
1003 +#define SYMBOL_RUNA 0
1004 +#define SYMBOL_RUNB 1
1006 +/* Status return values */
1007 +#define RETVAL_OK 0
1008 +#define RETVAL_LAST_BLOCK (-1)
1009 +#define RETVAL_NOT_BZIP_DATA (-2)
1010 +#define RETVAL_UNEXPECTED_INPUT_EOF (-3)
1011 +#define RETVAL_UNEXPECTED_OUTPUT_EOF (-4)
1012 +#define RETVAL_DATA_ERROR (-5)
1013 +#define RETVAL_OUT_OF_MEMORY (-6)
1014 +#define RETVAL_OBSOLETE_INPUT (-7)
1016 +/* Other housekeeping constants */
1017 +#define BZIP2_IOBUF_SIZE 4096
1019 +/* This is what we know about each Huffman coding group */
1020 +struct group_data {
1021 + /* We have an extra slot at the end of limit[] for a sentinal value. */
1022 + int limit[MAX_HUFCODE_BITS+1];
1023 + int base[MAX_HUFCODE_BITS];
1024 + int permute[MAX_SYMBOLS];
1025 + int minLen, maxLen;
1028 +/* Structure holding all the housekeeping data, including IO buffers and
1029 + memory that persists between calls to bunzip */
1030 +struct bunzip_data {
1031 + /* State for interrupting output loop */
1032 + int writeCopies, writePos, writeRunCountdown, writeCount, writeCurrent;
1033 + /* I/O tracking data (file handles, buffers, positions, etc.) */
1034 + int (*fill)(void*, unsigned int);
1035 + int inbufCount, inbufPos /*, outbufPos*/;
1036 + unsigned char *inbuf /*,*outbuf*/;
1037 + unsigned int inbufBitCount, inbufBits;
1038 + /* The CRC values stored in the block header and calculated from the
1040 + unsigned int crc32Table[256], headerCRC, totalCRC, writeCRC;
1041 + /* Intermediate buffer and its size (in bytes) */
1042 + unsigned int *dbuf, dbufSize;
1043 + /* These things are a bit too big to go on the stack */
1044 + unsigned char selectors[32768]; /* nSelectors = 15 bits */
1045 + struct group_data groups[MAX_GROUPS]; /* Huffman coding tables */
1046 + int io_error; /* non-zero if we have IO error */
1050 +/* Return the next nnn bits of input. All reads from the compressed input
1051 + are done through this function. All reads are big endian */
1052 +static unsigned int INIT get_bits(struct bunzip_data *bd, char bits_wanted)
1054 + unsigned int bits = 0;
1056 + /* If we need to get more data from the byte buffer, do so.
1057 + (Loop getting one byte at a time to enforce endianness and avoid
1058 + unaligned access.) */
1059 + while (bd->inbufBitCount < bits_wanted) {
1060 + /* If we need to read more data from file into byte buffer, do
1062 + if (bd->inbufPos == bd->inbufCount) {
1065 + bd->inbufCount = bd->fill(bd->inbuf, BZIP2_IOBUF_SIZE);
1066 + if (bd->inbufCount <= 0) {
1067 + bd->io_error = RETVAL_UNEXPECTED_INPUT_EOF;
1072 + /* Avoid 32-bit overflow (dump bit buffer to top of output) */
1073 + if (bd->inbufBitCount >= 24) {
1074 + bits = bd->inbufBits&((1 << bd->inbufBitCount)-1);
1075 + bits_wanted -= bd->inbufBitCount;
1076 + bits <<= bits_wanted;
1077 + bd->inbufBitCount = 0;
1079 + /* Grab next 8 bits of input from buffer. */
1080 + bd->inbufBits = (bd->inbufBits << 8)|bd->inbuf[bd->inbufPos++];
1081 + bd->inbufBitCount += 8;
1083 + /* Calculate result */
1084 + bd->inbufBitCount -= bits_wanted;
1085 + bits |= (bd->inbufBits >> bd->inbufBitCount)&((1 << bits_wanted)-1);
1090 +/* Unpacks the next block and sets up for the inverse burrows-wheeler step. */
1092 +static int INIT get_next_block(struct bunzip_data *bd)
1094 + struct group_data *hufGroup = NULL;
1096 + int *limit = NULL;
1097 + int dbufCount, nextSym, dbufSize, groupCount, selector,
1098 + i, j, k, t, runPos, symCount, symTotal, nSelectors,
1100 + unsigned char uc, symToByte[256], mtfSymbol[256], *selectors;
1101 + unsigned int *dbuf, origPtr;
1104 + dbufSize = bd->dbufSize;
1105 + selectors = bd->selectors;
1107 + /* Read in header signature and CRC, then validate signature.
1108 + (last block signature means CRC is for whole file, return now) */
1109 + i = get_bits(bd, 24);
1110 + j = get_bits(bd, 24);
1111 + bd->headerCRC = get_bits(bd, 32);
1112 + if ((i == 0x177245) && (j == 0x385090))
1113 + return RETVAL_LAST_BLOCK;
1114 + if ((i != 0x314159) || (j != 0x265359))
1115 + return RETVAL_NOT_BZIP_DATA;
1116 + /* We can add support for blockRandomised if anybody complains.
1117 + There was some code for this in busybox 1.0.0-pre3, but nobody ever
1118 + noticed that it didn't actually work. */
1119 + if (get_bits(bd, 1))
1120 + return RETVAL_OBSOLETE_INPUT;
1121 + origPtr = get_bits(bd, 24);
1122 + if (origPtr > dbufSize)
1123 + return RETVAL_DATA_ERROR;
1124 + /* mapping table: if some byte values are never used (encoding things
1125 + like ascii text), the compression code removes the gaps to have fewer
1126 + symbols to deal with, and writes a sparse bitfield indicating which
1127 + values were present. We make a translation table to convert the
1128 + symbols back to the corresponding bytes. */
1129 + t = get_bits(bd, 16);
1131 + for (i = 0; i < 16; i++) {
1132 + if (t&(1 << (15-i))) {
1133 + k = get_bits(bd, 16);
1134 + for (j = 0; j < 16; j++)
1135 + if (k&(1 << (15-j)))
1136 + symToByte[symTotal++] = (16*i)+j;
1139 + /* How many different Huffman coding groups does this block use? */
1140 + groupCount = get_bits(bd, 3);
1141 + if (groupCount < 2 || groupCount > MAX_GROUPS)
1142 + return RETVAL_DATA_ERROR;
1143 + /* nSelectors: Every GROUP_SIZE many symbols we select a new
1144 + Huffman coding group. Read in the group selector list,
1145 + which is stored as MTF encoded bit runs. (MTF = Move To
1146 + Front, as each value is used it's moved to the start of the
1148 + nSelectors = get_bits(bd, 15);
1150 + return RETVAL_DATA_ERROR;
1151 + for (i = 0; i < groupCount; i++)
1153 + for (i = 0; i < nSelectors; i++) {
1154 + /* Get next value */
1155 + for (j = 0; get_bits(bd, 1); j++)
1156 + if (j >= groupCount)
1157 + return RETVAL_DATA_ERROR;
1158 + /* Decode MTF to get the next selector */
1159 + uc = mtfSymbol[j];
1161 + mtfSymbol[j] = mtfSymbol[j-1];
1162 + mtfSymbol[0] = selectors[i] = uc;
1164 + /* Read the Huffman coding tables for each group, which code
1165 + for symTotal literal symbols, plus two run symbols (RUNA,
1167 + symCount = symTotal+2;
1168 + for (j = 0; j < groupCount; j++) {
1169 + unsigned char length[MAX_SYMBOLS], temp[MAX_HUFCODE_BITS+1];
1170 + int minLen, maxLen, pp;
1171 + /* Read Huffman code lengths for each symbol. They're
1172 + stored in a way similar to mtf; record a starting
1173 + value for the first symbol, and an offset from the
1174 + previous value for everys symbol after that.
1175 + (Subtracting 1 before the loop and then adding it
1176 + back at the end is an optimization that makes the
1177 + test inside the loop simpler: symbol length 0
1178 + becomes negative, so an unsigned inequality catches
1180 + t = get_bits(bd, 5)-1;
1181 + for (i = 0; i < symCount; i++) {
1183 + if (((unsigned)t) > (MAX_HUFCODE_BITS-1))
1184 + return RETVAL_DATA_ERROR;
1186 + /* If first bit is 0, stop. Else
1187 + second bit indicates whether to
1188 + increment or decrement the value.
1189 + Optimization: grab 2 bits and unget
1190 + the second if the first was 0. */
1192 + k = get_bits(bd, 2);
1194 + bd->inbufBitCount++;
1197 + /* Add one if second bit 1, else
1198 + * subtract 1. Avoids if/else */
1199 + t += (((k+1)&2)-1);
1201 + /* Correct for the initial -1, to get the
1202 + * final symbol length */
1205 + /* Find largest and smallest lengths in this group */
1206 + minLen = maxLen = length[0];
1208 + for (i = 1; i < symCount; i++) {
1209 + if (length[i] > maxLen)
1210 + maxLen = length[i];
1211 + else if (length[i] < minLen)
1212 + minLen = length[i];
1215 + /* Calculate permute[], base[], and limit[] tables from
1218 + * permute[] is the lookup table for converting
1219 + * Huffman coded symbols into decoded symbols. base[]
1220 + * is the amount to subtract from the value of a
1221 + * Huffman symbol of a given length when using
1224 + * limit[] indicates the largest numerical value a
1225 + * symbol with a given number of bits can have. This
1226 + * is how the Huffman codes can vary in length: each
1227 + * code with a value > limit[length] needs another
1230 + hufGroup = bd->groups+j;
1231 + hufGroup->minLen = minLen;
1232 + hufGroup->maxLen = maxLen;
1233 + /* Note that minLen can't be smaller than 1, so we
1234 + adjust the base and limit array pointers so we're
1235 + not always wasting the first entry. We do this
1236 + again when using them (during symbol decoding).*/
1237 + base = hufGroup->base-1;
1238 + limit = hufGroup->limit-1;
1239 + /* Calculate permute[]. Concurently, initialize
1240 + * temp[] and limit[]. */
1242 + for (i = minLen; i <= maxLen; i++) {
1243 + temp[i] = limit[i] = 0;
1244 + for (t = 0; t < symCount; t++)
1245 + if (length[t] == i)
1246 + hufGroup->permute[pp++] = t;
1248 + /* Count symbols coded for at each bit length */
1249 + for (i = 0; i < symCount; i++)
1250 + temp[length[i]]++;
1251 + /* Calculate limit[] (the largest symbol-coding value
1252 + *at each bit length, which is (previous limit <<
1253 + *1)+symbols at this level), and base[] (number of
1254 + *symbols to ignore at each bit length, which is limit
1255 + *minus the cumulative count of symbols coded for
1258 + for (i = minLen; i < maxLen; i++) {
1260 + /* We read the largest possible symbol size
1261 + and then unget bits after determining how
1262 + many we need, and those extra bits could be
1263 + set to anything. (They're noise from
1264 + future symbols.) At each level we're
1265 + really only interested in the first few
1266 + bits, so here we set all the trailing
1267 + to-be-ignored bits to 1 so they don't
1268 + affect the value > limit[length]
1270 + limit[i] = (pp << (maxLen - i)) - 1;
1272 + base[i+1] = pp-(t += temp[i]);
1274 + limit[maxLen+1] = INT_MAX; /* Sentinal value for
1275 + * reading next sym. */
1276 + limit[maxLen] = pp+temp[maxLen]-1;
1279 + /* We've finished reading and digesting the block header. Now
1280 + read this block's Huffman coded symbols from the file and
1281 + undo the Huffman coding and run length encoding, saving the
1282 + result into dbuf[dbufCount++] = uc */
1284 + /* Initialize symbol occurrence counters and symbol Move To
1286 + for (i = 0; i < 256; i++) {
1288 + mtfSymbol[i] = (unsigned char)i;
1290 + /* Loop through compressed symbols. */
1291 + runPos = dbufCount = symCount = selector = 0;
1293 + /* Determine which Huffman coding group to use. */
1294 + if (!(symCount--)) {
1295 + symCount = GROUP_SIZE-1;
1296 + if (selector >= nSelectors)
1297 + return RETVAL_DATA_ERROR;
1298 + hufGroup = bd->groups+selectors[selector++];
1299 + base = hufGroup->base-1;
1300 + limit = hufGroup->limit-1;
1302 + /* Read next Huffman-coded symbol. */
1303 + /* Note: It is far cheaper to read maxLen bits and
1304 + back up than it is to read minLen bits and then an
1305 + additional bit at a time, testing as we go.
1306 + Because there is a trailing last block (with file
1307 + CRC), there is no danger of the overread causing an
1308 + unexpected EOF for a valid compressed file. As a
1309 + further optimization, we do the read inline
1310 + (falling back to a call to get_bits if the buffer
1311 + runs dry). The following (up to got_huff_bits:) is
1312 + equivalent to j = get_bits(bd, hufGroup->maxLen);
1314 + while (bd->inbufBitCount < hufGroup->maxLen) {
1315 + if (bd->inbufPos == bd->inbufCount) {
1316 + j = get_bits(bd, hufGroup->maxLen);
1317 + goto got_huff_bits;
1320 + (bd->inbufBits << 8)|bd->inbuf[bd->inbufPos++];
1321 + bd->inbufBitCount += 8;
1323 + bd->inbufBitCount -= hufGroup->maxLen;
1324 + j = (bd->inbufBits >> bd->inbufBitCount)&
1325 + ((1 << hufGroup->maxLen)-1);
1327 + /* Figure how how many bits are in next symbol and
1329 + i = hufGroup->minLen;
1330 + while (j > limit[i])
1332 + bd->inbufBitCount += (hufGroup->maxLen - i);
1333 + /* Huffman decode value to get nextSym (with bounds checking) */
1334 + if ((i > hufGroup->maxLen)
1335 + || (((unsigned)(j = (j>>(hufGroup->maxLen-i))-base[i]))
1337 + return RETVAL_DATA_ERROR;
1338 + nextSym = hufGroup->permute[j];
1339 + /* We have now decoded the symbol, which indicates
1340 + either a new literal byte, or a repeated run of the
1341 + most recent literal byte. First, check if nextSym
1342 + 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
1351 + /* Neat trick that saves 1 symbol: instead of
1352 + or-ing 0 or 1 at each bit position, add 1
1353 + or 2 instead. For example, 1011 is 1 << 0
1354 + + 1 << 1 + 2 << 2. 1010 is 2 << 0 + 2 << 1
1355 + + 1 << 2. You can make any bit pattern
1356 + that way using 1 less symbol than the basic
1357 + or 0/1 method (except all bits 0, which
1358 + would use no symbols, but a run of length 0
1359 + doesn't mean anything in this context).
1360 + Thus space is saved. */
1361 + t += (runPos << nextSym);
1362 + /* +runPos if RUNA; +2*runPos if RUNB */
1367 + /* When we hit the first non-run symbol after a run,
1368 + we now know how many times to repeat the last
1369 + literal, so append that many copies to our buffer
1370 + of decoded symbols (dbuf) now. (The last literal
1371 + used is the one at the head of the mtfSymbol
1375 + if (dbufCount+t >= dbufSize)
1376 + return RETVAL_DATA_ERROR;
1378 + uc = symToByte[mtfSymbol[0]];
1379 + byteCount[uc] += t;
1381 + dbuf[dbufCount++] = uc;
1383 + /* Is this the terminating symbol? */
1384 + if (nextSym > symTotal)
1386 + /* At this point, nextSym indicates a new literal
1387 + character. Subtract one to get the position in the
1388 + MTF array at which this literal is currently to be
1389 + found. (Note that the result can't be -1 or 0,
1390 + because 0 and 1 are RUNA and RUNB. But another
1391 + instance of the first symbol in the mtf array,
1392 + position 0, would have been handled as part of a
1393 + run above. Therefore 1 unused mtf position minus 2
1394 + non-literal nextSym values equals -1.) */
1395 + if (dbufCount >= dbufSize)
1396 + return RETVAL_DATA_ERROR;
1398 + uc = mtfSymbol[i];
1399 + /* Adjust the MTF array. Since we typically expect to
1400 + *move only a small number of symbols, and are bound
1401 + *by 256 in any case, using memmove here would
1402 + *typically be bigger and slower due to function call
1403 + *overhead and other assorted setup costs. */
1405 + mtfSymbol[i] = mtfSymbol[i-1];
1407 + mtfSymbol[0] = uc;
1408 + uc = symToByte[uc];
1409 + /* We have our literal byte. Save it into dbuf. */
1411 + dbuf[dbufCount++] = (unsigned int)uc;
1413 + /* At this point, we've read all the Huffman-coded symbols
1414 + (and repeated runs) for this block from the input stream,
1415 + and decoded them into the intermediate buffer. There are
1416 + dbufCount many decoded bytes in dbuf[]. Now undo the
1417 + Burrows-Wheeler transform on dbuf. See
1418 + http://dogma.net/markn/articles/bwt/bwt.htm
1420 + /* Turn byteCount into cumulative occurrence counts of 0 to n-1. */
1422 + for (i = 0; i < 256; i++) {
1423 + k = j+byteCount[i];
1427 + /* Figure out what order dbuf would be in if we sorted it. */
1428 + for (i = 0; i < dbufCount; i++) {
1429 + uc = (unsigned char)(dbuf[i] & 0xff);
1430 + dbuf[byteCount[uc]] |= (i << 8);
1433 + /* Decode first byte by hand to initialize "previous" byte.
1434 + Note that it doesn't get output, and if the first three
1435 + characters are identical it doesn't qualify as a run (hence
1436 + writeRunCountdown = 5). */
1438 + if (origPtr >= dbufCount)
1439 + return RETVAL_DATA_ERROR;
1440 + bd->writePos = dbuf[origPtr];
1441 + bd->writeCurrent = (unsigned char)(bd->writePos&0xff);
1442 + bd->writePos >>= 8;
1443 + bd->writeRunCountdown = 5;
1445 + bd->writeCount = dbufCount;
1450 +/* Undo burrows-wheeler transform on intermediate buffer to produce output.
1451 + If start_bunzip was initialized with out_fd =-1, then up to len bytes of
1452 + data are written to outbuf. Return value is number of bytes written or
1453 + error (all errors are negative numbers). If out_fd!=-1, outbuf and len
1454 + are ignored, data is written to out_fd and return is RETVAL_OK or error.
1457 +static int INIT read_bunzip(struct bunzip_data *bd, char *outbuf, int len)
1459 + const unsigned int *dbuf;
1460 + int pos, xcurrent, previous, gotcount;
1462 + /* If last read was short due to end of file, return last block now */
1463 + if (bd->writeCount < 0)
1464 + return bd->writeCount;
1468 + pos = bd->writePos;
1469 + xcurrent = bd->writeCurrent;
1471 + /* We will always have pending decoded data to write into the output
1472 + buffer unless this is the very first call (in which case we haven't
1473 + Huffman-decoded a block into the intermediate buffer yet). */
1475 + if (bd->writeCopies) {
1476 + /* Inside the loop, writeCopies means extra copies (beyond 1) */
1477 + --bd->writeCopies;
1478 + /* Loop outputting bytes */
1480 + /* If the output buffer is full, snapshot
1481 + * state and return */
1482 + if (gotcount >= len) {
1483 + bd->writePos = pos;
1484 + bd->writeCurrent = xcurrent;
1485 + bd->writeCopies++;
1488 + /* Write next byte into output buffer, updating CRC */
1489 + outbuf[gotcount++] = xcurrent;
1490 + bd->writeCRC = (((bd->writeCRC) << 8)
1491 + ^bd->crc32Table[((bd->writeCRC) >> 24)
1493 + /* Loop now if we're outputting multiple
1494 + * copies of this byte */
1495 + if (bd->writeCopies) {
1496 + --bd->writeCopies;
1500 + if (!bd->writeCount--)
1502 + /* Follow sequence vector to undo
1503 + * Burrows-Wheeler transform */
1504 + previous = xcurrent;
1506 + xcurrent = pos&0xff;
1508 + /* After 3 consecutive copies of the same
1509 + byte, the 4th is a repeat count. We count
1510 + down from 4 instead *of counting up because
1511 + testing for non-zero is faster */
1512 + if (--bd->writeRunCountdown) {
1513 + if (xcurrent != previous)
1514 + bd->writeRunCountdown = 4;
1516 + /* We have a repeated run, this byte
1517 + * indicates the count */
1518 + bd->writeCopies = xcurrent;
1519 + xcurrent = previous;
1520 + bd->writeRunCountdown = 5;
1521 + /* Sometimes there are just 3 bytes
1522 + * (run length 0) */
1523 + if (!bd->writeCopies)
1524 + goto decode_next_byte;
1525 + /* Subtract the 1 copy we'd output
1526 + * anyway to get extras */
1527 + --bd->writeCopies;
1530 + /* Decompression of this block completed successfully */
1531 + bd->writeCRC = ~bd->writeCRC;
1532 + bd->totalCRC = ((bd->totalCRC << 1) |
1533 + (bd->totalCRC >> 31)) ^ bd->writeCRC;
1534 + /* If this block had a CRC error, force file level CRC error. */
1535 + if (bd->writeCRC != bd->headerCRC) {
1536 + bd->totalCRC = bd->headerCRC+1;
1537 + return RETVAL_LAST_BLOCK;
1541 + /* Refill the intermediate buffer by Huffman-decoding next
1542 + * block of input */
1543 + /* (previous is just a convenient unused temp variable here) */
1544 + previous = get_next_block(bd);
1546 + bd->writeCount = previous;
1547 + return (previous != RETVAL_LAST_BLOCK) ? previous : gotcount;
1549 + bd->writeCRC = 0xffffffffUL;
1550 + pos = bd->writePos;
1551 + xcurrent = bd->writeCurrent;
1552 + goto decode_next_byte;
1555 +static int INIT nofill(void *buf, unsigned int len)
1560 +/* Allocate the structure, read file header. If in_fd ==-1, inbuf must contain
1561 + a complete bunzip file (len bytes long). If in_fd!=-1, inbuf and len are
1562 + ignored, and data is read from file handle into temporary buffer. */
1563 +static int INIT start_bunzip(struct bunzip_data **bdp, void *inbuf, int len,
1564 + int (*fill)(void*, unsigned int))
1566 + struct bunzip_data *bd;
1567 + unsigned int i, j, c;
1568 + const unsigned int BZh0 =
1569 + (((unsigned int)'B') << 24)+(((unsigned int)'Z') << 16)
1570 + +(((unsigned int)'h') << 8)+(unsigned int)'0';
1572 + /* Figure out how much data to allocate */
1573 + i = sizeof(struct bunzip_data);
1575 + /* Allocate bunzip_data. Most fields initialize to zero. */
1576 + bd = *bdp = malloc(i);
1577 + memset(bd, 0, sizeof(struct bunzip_data));
1578 + /* Setup input buffer */
1579 + bd->inbuf = inbuf;
1580 + bd->inbufCount = len;
1584 + bd->fill = nofill;
1586 + /* Init the CRC32 table (big endian) */
1587 + for (i = 0; i < 256; i++) {
1589 + for (j = 8; j; j--)
1590 + c = c&0x80000000 ? (c << 1)^0x04c11db7 : (c << 1);
1591 + bd->crc32Table[i] = c;
1594 + /* Ensure that file starts with "BZh['1'-'9']." */
1595 + i = get_bits(bd, 32);
1596 + if (((unsigned int)(i-BZh0-1)) >= 9)
1597 + return RETVAL_NOT_BZIP_DATA;
1599 + /* Fourth byte (ascii '1'-'9'), indicates block size in units of 100k of
1600 + uncompressed data. Allocate intermediate buffer for block. */
1601 + bd->dbufSize = 100000*(i-BZh0);
1603 + bd->dbuf = large_malloc(bd->dbufSize * sizeof(int));
1607 +/* Example usage: decompress src_fd to dst_fd. (Stops at end of bzip2 data,
1608 + not end of file.) */
1609 +STATIC int INIT bunzip2(unsigned char *buf, int len,
1610 + int(*fill)(void*, unsigned int),
1611 + int(*flush)(void*, unsigned int),
1612 + unsigned char *outbuf,
1614 + void(*error_fn)(char *x))
1616 + struct bunzip_data *bd;
1618 + unsigned char *inbuf;
1620 + set_error_fn(error_fn);
1622 + outbuf = malloc(BZIP2_IOBUF_SIZE);
1624 + len -= 4; /* Uncompressed size hack active in pre-boot
1627 + error("Could not allocate output bufer");
1633 + inbuf = malloc(BZIP2_IOBUF_SIZE);
1635 + error("Could not allocate input bufer");
1638 + i = start_bunzip(&bd, inbuf, len, fill);
1641 + i = read_bunzip(bd, outbuf, BZIP2_IOBUF_SIZE);
1647 + if (i != flush(outbuf, i)) {
1648 + i = RETVAL_UNEXPECTED_OUTPUT_EOF;
1653 + /* Check CRC and release memory */
1654 + if (i == RETVAL_LAST_BLOCK) {
1655 + if (bd->headerCRC != bd->totalCRC)
1656 + error("Data integrity error when decompressing.");
1659 + } else if (i == RETVAL_UNEXPECTED_OUTPUT_EOF) {
1660 + error("Compressed file ends unexpectedly");
1663 + large_free(bd->dbuf);
1665 + *pos = bd->inbufPos;
1675 +#define decompress bunzip2
1677 +++ b/lib/decompress_unlzma.c
1679 +/* Lzma decompressor for Linux kernel. Shamelessly snarfed
1680 + *from busybox 1.1.1
1682 + *Linux kernel adaptation
1683 + *Copyright (C) 2006 Alain < alain@knaff.lu >
1685 + *Based on small lzma deflate implementation/Small range coder
1686 + *implementation for lzma.
1687 + *Copyright (C) 2006 Aurelien Jacobs < aurel@gnuage.org >
1689 + *Based on LzmaDecode.c from the LZMA SDK 4.22 (http://www.7-zip.org/)
1690 + *Copyright (C) 1999-2005 Igor Pavlov
1692 + *Copyrights of the parts, see headers below.
1695 + *This program is free software; you can redistribute it and/or
1696 + *modify it under the terms of the GNU Lesser General Public
1697 + *License as published by the Free Software Foundation; either
1698 + *version 2.1 of the License, or (at your option) any later version.
1700 + *This program is distributed in the hope that it will be useful,
1701 + *but WITHOUT ANY WARRANTY; without even the implied warranty of
1702 + *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1703 + *Lesser General Public License for more details.
1705 + *You should have received a copy of the GNU Lesser General Public
1706 + *License along with this library; if not, write to the Free Software
1707 + *Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1711 +#include <linux/decompress/unlzma.h>
1712 +#endif /* STATIC */
1714 +#include <linux/decompress/mm.h>
1716 +#define MIN(a, b) (((a) < (b)) ? (a) : (b))
1718 +static long long INIT read_int(unsigned char *ptr, int size)
1721 + long long ret = 0;
1723 + for (i = 0; i < size; i++)
1724 + ret = (ret << 8) | ptr[size-i-1];
1728 +#define ENDIAN_CONVERT(x) \
1729 + x = (typeof(x))read_int((unsigned char *)&x, sizeof(x))
1732 +/* Small range coder implementation for lzma.
1733 + *Copyright (C) 2006 Aurelien Jacobs < aurel@gnuage.org >
1735 + *Based on LzmaDecode.c from the LZMA SDK 4.22 (http://www.7-zip.org/)
1736 + *Copyright (c) 1999-2005 Igor Pavlov
1739 +#include <linux/compiler.h>
1741 +#define LZMA_IOBUF_SIZE 0x10000
1744 + int (*fill)(void*, unsigned int);
1747 + uint8_t *buffer_end;
1755 +#define RC_TOP_BITS 24
1756 +#define RC_MOVE_BITS 5
1757 +#define RC_MODEL_TOTAL_BITS 11
1760 +/* Called twice: once at startup and once in rc_normalize() */
1761 +static void INIT rc_read(struct rc *rc)
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;
1771 +static inline void INIT rc_init(struct rc *rc,
1772 + int (*fill)(void*, unsigned int),
1773 + char *buffer, int buffer_size)
1776 + rc->buffer = (uint8_t *)buffer;
1777 + rc->buffer_size = buffer_size;
1778 + rc->buffer_end = rc->buffer + rc->buffer_size;
1779 + rc->ptr = rc->buffer;
1782 + rc->range = 0xFFFFFFFF;
1785 +static inline void INIT rc_init_code(struct rc *rc)
1789 + for (i = 0; i < 5; i++) {
1790 + if (rc->ptr >= rc->buffer_end)
1792 + rc->code = (rc->code << 8) | *rc->ptr++;
1797 +/* Called once. TODO: bb_maybe_free() */
1798 +static inline void INIT rc_free(struct rc *rc)
1803 +/* Called twice, but one callsite is in inline'd rc_is_bit_0_helper() */
1804 +static void INIT rc_do_normalize(struct rc *rc)
1806 + if (rc->ptr >= rc->buffer_end)
1809 + rc->code = (rc->code << 8) | *rc->ptr++;
1811 +static inline void INIT rc_normalize(struct rc *rc)
1813 + if (rc->range < (1 << RC_TOP_BITS))
1814 + rc_do_normalize(rc);
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
1821 +static inline uint32_t INIT rc_is_bit_0_helper(struct rc *rc, uint16_t *p)
1824 + rc->bound = *p * (rc->range >> RC_MODEL_TOTAL_BITS);
1827 +static inline int INIT rc_is_bit_0(struct rc *rc, uint16_t *p)
1829 + uint32_t t = rc_is_bit_0_helper(rc, p);
1830 + return rc->code < t;
1833 +/* Called ~10 times, but very small, thus inlined */
1834 +static inline void INIT rc_update_bit_0(struct rc *rc, uint16_t *p)
1836 + rc->range = rc->bound;
1837 + *p += ((1 << RC_MODEL_TOTAL_BITS) - *p) >> RC_MOVE_BITS;
1839 +static inline void rc_update_bit_1(struct rc *rc, uint16_t *p)
1841 + rc->range -= rc->bound;
1842 + rc->code -= rc->bound;
1843 + *p -= *p >> RC_MOVE_BITS;
1846 +/* Called 4 times in unlzma loop */
1847 +static int INIT rc_get_bit(struct rc *rc, uint16_t *p, int *symbol)
1849 + if (rc_is_bit_0(rc, p)) {
1850 + rc_update_bit_0(rc, p);
1854 + rc_update_bit_1(rc, p);
1855 + *symbol = *symbol * 2 + 1;
1861 +static inline int INIT rc_direct_bit(struct rc *rc)
1865 + if (rc->code >= rc->range) {
1866 + rc->code -= rc->range;
1873 +static inline void INIT
1874 +rc_bit_tree_decode(struct rc *rc, uint16_t *p, int num_levels, int *symbol)
1876 + int i = num_levels;
1880 + rc_get_bit(rc, p + *symbol, symbol);
1881 + *symbol -= 1 << num_levels;
1886 + * Small lzma deflate implementation.
1887 + * Copyright (C) 2006 Aurelien Jacobs < aurel@gnuage.org >
1889 + * Based on LzmaDecode.c from the LZMA SDK 4.22 (http://www.7-zip.org/)
1890 + * Copyright (C) 1999-2005 Igor Pavlov
1894 +struct lzma_header {
1896 + uint32_t dict_size;
1897 + uint64_t dst_size;
1898 +} __attribute__ ((packed)) ;
1901 +#define LZMA_BASE_SIZE 1846
1902 +#define LZMA_LIT_SIZE 768
1904 +#define LZMA_NUM_POS_BITS_MAX 4
1906 +#define LZMA_LEN_NUM_LOW_BITS 3
1907 +#define LZMA_LEN_NUM_MID_BITS 3
1908 +#define LZMA_LEN_NUM_HIGH_BITS 8
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))
1919 +#define LZMA_NUM_STATES 12
1920 +#define LZMA_NUM_LIT_STATES 7
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))
1926 +#define LZMA_NUM_POS_SLOT_BITS 6
1927 +#define LZMA_NUM_LEN_TO_POS_STATES 4
1929 +#define LZMA_NUM_ALIGN_BITS 4
1931 +#define LZMA_MATCH_MIN_LEN 2
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)
1952 + uint8_t previous_byte;
1953 + size_t buffer_pos;
1955 + size_t global_pos;
1956 + int(*flush)(void*, unsigned int);
1957 + struct lzma_header *header;
1962 + uint32_t rep0, rep1, rep2, rep3;
1965 +static inline size_t INIT get_pos(struct writer *wr)
1968 + wr->global_pos + wr->buffer_pos;
1971 +static inline uint8_t INIT peek_old_byte(struct writer *wr,
1976 + while (offs > wr->header->dict_size)
1977 + offs -= wr->header->dict_size;
1978 + pos = wr->buffer_pos - offs;
1979 + return wr->buffer[pos];
1981 + uint32_t pos = wr->buffer_pos - offs;
1982 + while (pos >= wr->header->dict_size)
1983 + pos += wr->header->dict_size;
1984 + return wr->buffer[pos];
1989 +static inline void INIT write_byte(struct writer *wr, uint8_t byte)
1991 + wr->buffer[wr->buffer_pos++] = wr->previous_byte = byte;
1992 + if (wr->flush && wr->buffer_pos == wr->header->dict_size) {
1993 + wr->buffer_pos = 0;
1994 + wr->global_pos += wr->header->dict_size;
1995 + wr->flush((char *)wr->buffer, wr->header->dict_size);
2000 +static inline void INIT copy_byte(struct writer *wr, uint32_t offs)
2002 + write_byte(wr, peek_old_byte(wr, offs));
2005 +static inline void INIT copy_bytes(struct writer *wr,
2006 + uint32_t rep0, int len)
2009 + copy_byte(wr, rep0);
2011 + } while (len != 0 && wr->buffer_pos < wr->header->dst_size);
2014 +static inline void INIT process_bit0(struct writer *wr, struct rc *rc,
2015 + struct cstate *cst, uint16_t *p,
2016 + int pos_state, uint16_t *prob,
2017 + int lc, uint32_t literal_pos_mask) {
2019 + rc_update_bit_0(rc, prob);
2020 + prob = (p + LZMA_LITERAL +
2022 + * (((get_pos(wr) & literal_pos_mask) << lc)
2023 + + (wr->previous_byte >> (8 - lc))))
2026 + if (cst->state >= LZMA_NUM_LIT_STATES) {
2027 + int match_byte = peek_old_byte(wr, cst->rep0);
2030 + uint16_t *prob_lit;
2033 + bit = match_byte & 0x100;
2034 + prob_lit = prob + 0x100 + bit + mi;
2035 + if (rc_get_bit(rc, prob_lit, &mi)) {
2042 + } while (mi < 0x100);
2044 + while (mi < 0x100) {
2045 + uint16_t *prob_lit = prob + mi;
2046 + rc_get_bit(rc, prob_lit, &mi);
2048 + write_byte(wr, mi);
2049 + if (cst->state < 4)
2051 + else if (cst->state < 10)
2057 +static inline void INIT process_bit1(struct writer *wr, struct rc *rc,
2058 + struct cstate *cst, uint16_t *p,
2059 + int pos_state, uint16_t *prob) {
2061 + uint16_t *prob_len;
2065 + rc_update_bit_1(rc, prob);
2066 + prob = p + LZMA_IS_REP + cst->state;
2067 + if (rc_is_bit_0(rc, prob)) {
2068 + rc_update_bit_0(rc, prob);
2069 + cst->rep3 = cst->rep2;
2070 + cst->rep2 = cst->rep1;
2071 + cst->rep1 = cst->rep0;
2072 + cst->state = cst->state < LZMA_NUM_LIT_STATES ? 0 : 3;
2073 + prob = p + LZMA_LEN_CODER;
2075 + rc_update_bit_1(rc, prob);
2076 + prob = p + LZMA_IS_REP_G0 + cst->state;
2077 + if (rc_is_bit_0(rc, prob)) {
2078 + rc_update_bit_0(rc, prob);
2079 + prob = (p + LZMA_IS_REP_0_LONG
2081 + LZMA_NUM_POS_BITS_MAX) +
2083 + if (rc_is_bit_0(rc, prob)) {
2084 + rc_update_bit_0(rc, prob);
2086 + cst->state = cst->state < LZMA_NUM_LIT_STATES ?
2088 + copy_byte(wr, cst->rep0);
2091 + rc_update_bit_1(rc, prob);
2094 + uint32_t distance;
2096 + rc_update_bit_1(rc, prob);
2097 + prob = p + LZMA_IS_REP_G1 + cst->state;
2098 + if (rc_is_bit_0(rc, prob)) {
2099 + rc_update_bit_0(rc, prob);
2100 + distance = cst->rep1;
2102 + rc_update_bit_1(rc, prob);
2103 + prob = p + LZMA_IS_REP_G2 + cst->state;
2104 + if (rc_is_bit_0(rc, prob)) {
2105 + rc_update_bit_0(rc, prob);
2106 + distance = cst->rep2;
2108 + rc_update_bit_1(rc, prob);
2109 + distance = cst->rep3;
2110 + cst->rep3 = cst->rep2;
2112 + cst->rep2 = cst->rep1;
2114 + cst->rep1 = cst->rep0;
2115 + cst->rep0 = distance;
2117 + cst->state = cst->state < LZMA_NUM_LIT_STATES ? 8 : 11;
2118 + prob = p + LZMA_REP_LEN_CODER;
2121 + prob_len = prob + LZMA_LEN_CHOICE;
2122 + if (rc_is_bit_0(rc, prob_len)) {
2123 + rc_update_bit_0(rc, prob_len);
2124 + prob_len = (prob + LZMA_LEN_LOW
2126 + LZMA_LEN_NUM_LOW_BITS));
2128 + num_bits = LZMA_LEN_NUM_LOW_BITS;
2130 + rc_update_bit_1(rc, prob_len);
2131 + prob_len = prob + LZMA_LEN_CHOICE_2;
2132 + if (rc_is_bit_0(rc, prob_len)) {
2133 + rc_update_bit_0(rc, prob_len);
2134 + prob_len = (prob + LZMA_LEN_MID
2136 + LZMA_LEN_NUM_MID_BITS));
2137 + offset = 1 << LZMA_LEN_NUM_LOW_BITS;
2138 + num_bits = LZMA_LEN_NUM_MID_BITS;
2140 + rc_update_bit_1(rc, prob_len);
2141 + prob_len = prob + LZMA_LEN_HIGH;
2142 + offset = ((1 << LZMA_LEN_NUM_LOW_BITS)
2143 + + (1 << LZMA_LEN_NUM_MID_BITS));
2144 + num_bits = LZMA_LEN_NUM_HIGH_BITS;
2148 + rc_bit_tree_decode(rc, prob_len, num_bits, &len);
2151 + if (cst->state < 4) {
2154 + cst->state += LZMA_NUM_LIT_STATES;
2156 + p + LZMA_POS_SLOT +
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,
2162 + LZMA_NUM_POS_SLOT_BITS,
2164 + if (pos_slot >= LZMA_START_POS_MODEL_INDEX) {
2166 + num_bits = (pos_slot >> 1) - 1;
2167 + cst->rep0 = 2 | (pos_slot & 1);
2168 + if (pos_slot < LZMA_END_POS_MODEL_INDEX) {
2169 + cst->rep0 <<= num_bits;
2170 + prob = p + LZMA_SPEC_POS +
2171 + cst->rep0 - pos_slot - 1;
2173 + num_bits -= LZMA_NUM_ALIGN_BITS;
2174 + while (num_bits--)
2175 + cst->rep0 = (cst->rep0 << 1) |
2176 + rc_direct_bit(rc);
2177 + prob = p + LZMA_ALIGN;
2178 + cst->rep0 <<= LZMA_NUM_ALIGN_BITS;
2179 + num_bits = LZMA_NUM_ALIGN_BITS;
2183 + while (num_bits--) {
2184 + if (rc_get_bit(rc, prob + mi, &mi))
2189 + cst->rep0 = pos_slot;
2190 + if (++(cst->rep0) == 0)
2194 + len += LZMA_MATCH_MIN_LEN;
2196 + copy_bytes(wr, cst->rep0, len);
2201 +STATIC inline int INIT unlzma(unsigned char *buf, int in_len,
2202 + int(*fill)(void*, unsigned int),
2203 + int(*flush)(void*, unsigned int),
2204 + unsigned char *output,
2206 + void(*error_fn)(char *x)
2209 + struct lzma_header header;
2211 + uint32_t pos_state_mask;
2212 + uint32_t literal_pos_mask;
2218 + struct cstate cst;
2219 + unsigned char *inbuf;
2222 + set_error_fn(error_fn);
2224 + in_len -= 4; /* Uncompressed size hack active in pre-boot
2229 + inbuf = malloc(LZMA_IOBUF_SIZE);
2231 + error("Could not allocate input bufer");
2236 + cst.rep0 = cst.rep1 = cst.rep2 = cst.rep3 = 1;
2238 + wr.header = &header;
2240 + wr.global_pos = 0;
2241 + wr.previous_byte = 0;
2242 + wr.buffer_pos = 0;
2244 + rc_init(&rc, fill, inbuf, in_len);
2246 + for (i = 0; i < sizeof(header); i++) {
2247 + if (rc.ptr >= rc.buffer_end)
2249 + ((unsigned char *)&header)[i] = *rc.ptr++;
2252 + if (header.pos >= (9 * 5 * 5))
2253 + error("bad header");
2267 + pos_state_mask = (1 << pb) - 1;
2268 + literal_pos_mask = (1 << lp) - 1;
2270 + ENDIAN_CONVERT(header.dict_size);
2271 + ENDIAN_CONVERT(header.dst_size);
2273 + if (header.dict_size == 0)
2274 + header.dict_size = 1;
2277 + wr.buffer = output;
2279 + wr.bufsize = MIN(header.dst_size, header.dict_size);
2280 + wr.buffer = large_malloc(wr.bufsize);
2282 + if (wr.buffer == NULL)
2285 + num_probs = LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp));
2286 + p = (uint16_t *) large_malloc(num_probs * sizeof(*p));
2289 + num_probs = LZMA_LITERAL + (LZMA_LIT_SIZE << (lc + lp));
2290 + for (i = 0; i < num_probs; i++)
2291 + p[i] = (1 << RC_MODEL_TOTAL_BITS) >> 1;
2293 + rc_init_code(&rc);
2295 + while (get_pos(&wr) < header.dst_size) {
2296 + int pos_state = get_pos(&wr) & pos_state_mask;
2297 + uint16_t *prob = p + LZMA_IS_MATCH +
2298 + (cst.state << LZMA_NUM_POS_BITS_MAX) + pos_state;
2299 + if (rc_is_bit_0(&rc, prob))
2300 + process_bit0(&wr, &rc, &cst, p, pos_state, prob,
2301 + lc, literal_pos_mask);
2303 + process_bit1(&wr, &rc, &cst, p, pos_state, prob);
2304 + if (cst.rep0 == 0)
2310 + *posp = rc.ptr-rc.buffer;
2312 + wr.flush(wr.buffer, wr.buffer_pos);
2317 + large_free(wr.buffer);
2325 +#define decompress unlzma
2328 @@ -109,20 +109,78 @@ static char rcsid[] = "#Id: inflate.c,v
2332 +#include <linux/decompress/inflate.h>
2333 +#endif /* ! STATIC */
2335 -#if defined(STDC_HEADERS) || defined(HAVE_STDLIB_H)
2336 -# include <sys/types.h>
2337 -# include <stdlib.h>
2338 +#include <linux/decompress/mm.h>
2340 +#include <linux/string.h>
2343 +static int(*flush_cb)(void*, unsigned int);
2344 +static int(*fill_cb)(void*, unsigned int);
2346 +/* Begin stuff copied from initramfs */
2348 + * gzip declarations
2351 +#define OF(args) args
2354 +#define memzero(s, n) memset((s), 0, (n))
2359 -#endif /* !STATIC */
2360 +#define INBUFSIZ 4096
2362 +#define WSIZE 0x8000 /* window size--must be a power of two, and */
2363 + /* at least 32K for zip's deflate method */
2365 +static uint8_t *inbuf;
2366 +static uint8_t *window;
2368 +static unsigned insize; /* valid bytes in inbuf */
2369 +static unsigned outcnt; /* bytes in output buffer */
2370 +static long bytes_out;
2374 +static unsigned inptr; /* index of next byte to be processed in inbuf */
2378 +/* ===========================================================================
2379 + * Fill the input buffer. This is called only when the buffer is empty
2380 + * and at least one byte is really needed.
2381 + * Returning -1 does not guarantee that gunzip() will ever return.
2383 +static int INIT fill_inbuf(void)
2385 + insize = fill_cb(inbuf, INBUFSIZ);
2386 + if (insize <= 0) {
2387 + error("RAMDISK: ran out of compressed data");
2396 +#define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
2398 +/* Diagnostic functions (stubbed out) */
2399 +#define Assert(cond, msg)
2403 +#define Tracec(c, x)
2404 +#define Tracecv(c, x)
2408 +static void flush_window(void);
2409 +/* End stuff copied from initramfs */
2413 #define slide window
2415 /* Huffman code lookup table entry--this entry is four bytes for machines
2416 @@ -133,10 +191,10 @@ static char rcsid[] = "#Id: inflate.c,v
2417 an unused code. If a code with e == 99 is looked up, this implies an
2418 error in the data. */
2420 - uch e; /* number of extra bits or operation */
2421 - uch b; /* number of bits in this code or subcode */
2422 + uint8_t e; /* number of extra bits or operation */
2423 + uint8_t b; /* number of bits in this code or subcode */
2425 - ush n; /* literal, length base, or distance base */
2426 + uint16_t n; /* literal, length base, or distance base */
2427 struct huft *t; /* pointer to next level of table */
2430 @@ -144,7 +202,7 @@ struct huft {
2432 /* Function prototypes */
2433 STATIC int INIT huft_build OF((unsigned *, unsigned, unsigned,
2434 - const ush *, const ush *, struct huft **, int *));
2435 + const uint16_t *, const uint16_t *, struct huft **, int *));
2436 STATIC int INIT huft_free OF((struct huft *));
2437 STATIC int INIT inflate_codes OF((struct huft *, struct huft *, int, int));
2438 STATIC int INIT inflate_stored OF((void));
2439 @@ -159,28 +217,28 @@ STATIC int INIT inflate OF((void));
2440 circular buffer. The index is updated simply by incrementing and then
2441 ANDing with 0x7fff (32K-1). */
2442 /* It is left to other modules to supply the 32 K area. It is assumed
2443 - to be usable as if it were declared "uch slide[32768];" or as just
2444 - "uch *slide;" and then malloc'ed in the latter case. The definition
2445 + to be usable as if it were declared "uint8_t slide[32768];" or as just
2446 + "uint8_t *slide;" and then malloc'ed in the latter case. The definition
2447 must be in unzip.h, included above. */
2448 /* unsigned wp; current position in slide */
2450 #define flush_output(w) (wp=(w),flush_window())
2452 /* Tables for deflate from PKZIP's appnote.txt. */
2453 -static const unsigned border[] = { /* Order of the bit length code lengths */
2454 +static const unsigned border[] = { /* Order of the bit length code lengths */
2455 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
2456 -static const ush cplens[] = { /* Copy lengths for literal codes 257..285 */
2457 +static const uint16_t cplens[] = { /* Copy lengths for literal codes 257..285 */
2458 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
2459 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
2460 /* note: see note #13 above about the 258 in this list. */
2461 -static const ush cplext[] = { /* Extra bits for literal codes 257..285 */
2462 +static const uint16_t cplext[] = { /* Extra bits for literal codes 257..285 */
2463 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
2464 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99}; /* 99==invalid */
2465 -static const ush cpdist[] = { /* Copy offsets for distance codes 0..29 */
2466 +static const uint16_t cpdist[] = { /* Copy offsets for distance codes 0..29 */
2467 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
2468 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
2469 8193, 12289, 16385, 24577};
2470 -static const ush cpdext[] = { /* Extra bits for distance codes */
2471 +static const uint16_t cpdext[] = { /* Extra bits for distance codes */
2472 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
2473 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
2475 @@ -217,59 +275,21 @@ static const ush cpdext[] = { /*
2479 -STATIC ulg bb; /* bit buffer */
2480 +STATIC uint32_t bb; /* bit buffer */
2481 STATIC unsigned bk; /* bits in bit buffer */
2483 -STATIC const ush mask_bits[] = {
2484 +STATIC const uint16_t mask_bits[] = {
2486 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
2487 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
2490 -#define NEXTBYTE() ({ int v = get_byte(); if (v < 0) goto underrun; (uch)v; })
2491 -#define NEEDBITS(n) {while(k<(n)){b|=((ulg)NEXTBYTE())<<k;k+=8;}}
2492 +#define NEXTBYTE() ({ int v = get_byte(); if (v < 0) goto underrun; \
2494 +#define NEEDBITS(n) {while (k < (n)) \
2495 + {b |= ((uint32_t)NEXTBYTE())<<k; k += 8; } }
2496 #define DUMPBITS(n) {b>>=(n);k-=(n);}
2498 -#ifndef NO_INFLATE_MALLOC
2499 -/* A trivial malloc implementation, adapted from
2500 - * malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994
2503 -static unsigned long malloc_ptr;
2504 -static int malloc_count;
2506 -static void *malloc(int size)
2511 - error("Malloc error");
2513 - malloc_ptr = free_mem_ptr;
2515 - malloc_ptr = (malloc_ptr + 3) & ~3; /* Align */
2517 - p = (void *)malloc_ptr;
2518 - malloc_ptr += size;
2520 - if (free_mem_end_ptr && malloc_ptr >= free_mem_end_ptr)
2521 - error("Out of memory");
2527 -static void free(void *where)
2530 - if (!malloc_count)
2531 - malloc_ptr = free_mem_ptr;
2534 -#define malloc(a) kmalloc(a, GFP_KERNEL)
2535 -#define free(a) kfree(a)
2539 Huffman code decoding is performed using a multi-level table lookup.
2540 The fastest way to decode is to simply build a lookup table whose
2541 @@ -307,7 +327,7 @@ STATIC const int lbits = 9; /*
2542 STATIC const int dbits = 6; /* bits in base distance lookup table */
2545 -/* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
2546 +/* If BMAX needs to be larger than 16, then h and x[] should be uint32_t. */
2547 #define BMAX 16 /* maximum bit length of any code (16 for explode) */
2548 #define N_MAX 288 /* maximum number of codes in any set */
2550 @@ -319,8 +339,8 @@ STATIC int INIT huft_build(
2551 unsigned *b, /* code lengths in bits (all assumed <= BMAX) */
2552 unsigned n, /* number of codes (assumed <= N_MAX) */
2553 unsigned s, /* number of simple-valued codes (0..s-1) */
2554 - const ush *d, /* list of base values for non-simple codes */
2555 - const ush *e, /* list of extra bits for non-simple codes */
2556 + const uint16_t *d, /* list of base values for non-simple codes */
2557 + const uint16_t *e, /* list of extra bits for non-simple codes */
2558 struct huft **t, /* result: starting table */
2559 int *m /* maximum lookup bits, returns actual */
2561 @@ -500,8 +520,8 @@ DEBG1("5 ");
2564 x[h] = i; /* save pattern for backing up */
2565 - r.b = (uch)l; /* bits to dump before this table */
2566 - r.e = (uch)(16 + j); /* bits in this table */
2567 + r.b = (uint8_t)l; /* bits to dump before this table */
2568 + r.e = (uint8_t)(16 + j); /* bits in this table */
2569 r.v.t = q; /* pointer to this table */
2570 j = i >> (w - l); /* (get around Turbo C bug) */
2571 u[h-1][j] = r; /* connect to last table */
2572 @@ -511,18 +531,18 @@ DEBG1("6 ");
2575 /* set up table entry in r */
2576 - r.b = (uch)(k - w);
2577 + r.b = (uint8_t)(k - w);
2579 r.e = 99; /* out of values--invalid code */
2582 - r.e = (uch)(*p < 256 ? 16 : 15); /* 256 is end-of-block code */
2583 - r.v.n = (ush)(*p); /* simple code is just the value */
2584 + r.e = (uint8_t)(*p < 256 ? 16 : 15); /* 256 is end-of-block code */
2585 + r.v.n = (uint16_t)(*p); /* simple code is just the value */
2586 p++; /* one compiler does not like *p++ */
2590 - r.e = (uch)e[*p - s]; /* non-simple--look up in lists */
2591 + r.e = (uint8_t)e[*p - s]; /* non-simple--look up in lists */
2592 r.v.n = d[*p++ - s];
2595 @@ -592,11 +612,12 @@ STATIC int INIT inflate_codes(
2596 Return an error code or zero if it all goes ok. */
2598 register unsigned e; /* table entry flag/number of extra bits */
2599 - unsigned n, d; /* length and index for copy */
2601 + int d; /* source index for copy */
2602 unsigned w; /* current window position */
2603 struct huft *t; /* pointer to table entry */
2604 unsigned ml, md; /* masks for bl and bd bits */
2605 - register ulg b; /* bit buffer */
2606 + register uint32_t b; /* bit buffer */
2607 register unsigned k; /* number of bits in bit buffer */
2610 @@ -622,7 +643,7 @@ STATIC int INIT inflate_codes(
2612 if (e == 16) /* then it's a literal */
2614 - slide[w++] = (uch)t->v.n;
2615 + slide[w++] = (uint8_t)t->v.n;
2616 Tracevv((stderr, "%c", slide[w-1]));
2619 @@ -659,11 +680,25 @@ STATIC int INIT inflate_codes(
2623 - n -= (e = (e = WSIZE - ((d &= WSIZE-1) > w ? d : w)) > n ? n : e);
2627 + /* Sliding window emulated using circular buffer:
2628 + * manage wrap-around */
2629 + e = WSIZE - ((d &= WSIZE-1) > w ? d : w);
2639 #if !defined(NOMEMCPY) && !defined(DEBUG)
2640 if (w - d >= e) /* (this test assumes unsigned comparison) */
2642 - memcpy(slide + w, slide + d, e);
2643 + memcpy(slide + w, slide + d, e);
2647 @@ -673,9 +708,8 @@ STATIC int INIT inflate_codes(
2648 slide[w++] = slide[d++];
2649 Tracevv((stderr, "%c", slide[w-1]));
2659 @@ -702,7 +736,7 @@ STATIC int INIT inflate_stored(void)
2661 unsigned n; /* number of bytes in block */
2662 unsigned w; /* current window position */
2663 - register ulg b; /* bit buffer */
2664 + register uint32_t b; /* bit buffer */
2665 register unsigned k; /* number of bits in bit buffer */
2668 @@ -732,7 +766,7 @@ DEBG("<stor");
2672 - slide[w++] = (uch)b;
2673 + slide[w++] = (uint8_t)b;
2677 @@ -838,7 +872,7 @@ STATIC int noinline INIT inflate_dynamic
2678 unsigned nl; /* number of literal/length codes */
2679 unsigned nd; /* number of distance codes */
2680 unsigned *ll; /* literal/length and distance code lengths */
2681 - register ulg b; /* bit buffer */
2682 + register uint32_t b; /* bit buffer */
2683 register unsigned k; /* number of bits in bit buffer */
2686 @@ -1033,7 +1067,7 @@ STATIC int INIT inflate_block(
2687 /* decompress an inflated block */
2689 unsigned t; /* block type */
2690 - register ulg b; /* bit buffer */
2691 + register uint32_t b; /* bit buffer */
2692 register unsigned k; /* number of bits in bit buffer */
2695 @@ -1130,8 +1164,8 @@ STATIC int INIT inflate(void)
2697 **********************************************************************/
2699 -static ulg crc_32_tab[256];
2700 -static ulg crc; /* initialized in makecrc() so it'll reside in bss */
2701 +static uint32_t crc_32_tab[256];
2702 +static uint32_t crc; /* initialized in makecrc() so it'll reside in bss */
2703 #define CRC_VALUE (crc ^ 0xffffffffUL)
2706 @@ -1172,7 +1206,7 @@ makecrc(void)
2709 /* this is initialized here so this code could reside in ROM */
2710 - crc = (ulg)0xffffffffUL; /* shift register contents */
2711 + crc = (uint32_t)0xffffffffUL; /* shift register contents */
2714 /* gzip flag byte */
2715 @@ -1184,18 +1218,89 @@ makecrc(void)
2716 #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
2717 #define RESERVED 0xC0 /* bit 6,7: reserved */
2720 +/* ===========================================================================
2721 + * Write the output window window[0..outcnt-1] and update crc and bytes_out.
2722 + * (Used for the decompressed data only.)
2724 +static void INIT flush_window(void)
2726 + uint32_t c = crc; /* temporary variable */
2731 + for (n = 0; n < outcnt; n++) {
2733 + c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
2736 + bytes_out += (uint32_t)outcnt;
2737 + if (flush_cb != NULL)
2738 + flush_cb(window, outcnt); /* TODO: handle unzip_error */
2744 +static int empty_fill(void *buf, unsigned int len)
2752 * Do the uncompression!
2754 -static int INIT gunzip(void)
2755 +STATIC int INIT gunzip(
2757 + unsigned char *buf, int len,
2758 + int(*fill)(void*, unsigned int),
2759 + int(*flush)(void*, unsigned int),
2760 + unsigned char *output,
2762 + void(*error_fn)(char *x)
2770 unsigned char magic[2]; /* magic header */
2772 - ulg orig_crc = 0; /* original crc */
2773 - ulg orig_len = 0; /* original uncompressed length */
2774 + uint32_t orig_crc = 0; /* original crc */
2775 + uint32_t orig_len = 0; /* original uncompressed length */
2779 + set_error_fn(error_fn);
2781 + fill_cb = empty_fill;
2787 + window = malloc(0x8000);
2789 + panic("can't allocate buffers");
2797 + inbuf = malloc(INBUFSIZ);
2801 + outcnt = 0; /* bytes in output buffer */
2803 + crc = (uint32_t)0xffffffffL; /* shift register contents */
2806 magic[0] = NEXTBYTE();
2807 magic[1] = NEXTBYTE();
2808 method = NEXTBYTE();
2809 @@ -1212,7 +1317,7 @@ static int INIT gunzip(void)
2813 - flags = (uch)get_byte();
2814 + flags = (uint8_t)get_byte();
2815 if ((flags & ENCRYPTED) != 0) {
2816 error("Input is encrypted");
2818 @@ -1277,15 +1382,15 @@ static int INIT gunzip(void)
2819 /* crc32 (see algorithm.doc)
2820 * uncompressed input size modulo 2^32
2822 - orig_crc = (ulg) NEXTBYTE();
2823 - orig_crc |= (ulg) NEXTBYTE() << 8;
2824 - orig_crc |= (ulg) NEXTBYTE() << 16;
2825 - orig_crc |= (ulg) NEXTBYTE() << 24;
2826 + orig_crc = (uint32_t) NEXTBYTE();
2827 + orig_crc |= (uint32_t) NEXTBYTE() << 8;
2828 + orig_crc |= (uint32_t) NEXTBYTE() << 16;
2829 + orig_crc |= (uint32_t) NEXTBYTE() << 24;
2831 - orig_len = (ulg) NEXTBYTE();
2832 - orig_len |= (ulg) NEXTBYTE() << 8;
2833 - orig_len |= (ulg) NEXTBYTE() << 16;
2834 - orig_len |= (ulg) NEXTBYTE() << 24;
2835 + orig_len = (uint32_t) NEXTBYTE();
2836 + orig_len |= (uint32_t) NEXTBYTE() << 8;
2837 + orig_len |= (uint32_t) NEXTBYTE() << 16;
2838 + orig_len |= (uint32_t) NEXTBYTE() << 24;
2840 /* Validate decompression */
2841 if (orig_crc != CRC_VALUE) {
2842 @@ -1296,11 +1401,22 @@ static int INIT gunzip(void)
2843 error("length error");
2854 underrun: /* NEXTBYTE() goto's here if needed */
2860 error("out of input data");
2865 +#define decompress gunzip
2866 --- a/scripts/Makefile.lib
2867 +++ b/scripts/Makefile.lib
2868 @@ -183,3 +183,17 @@ quiet_cmd_gzip = GZIP $@
2869 cmd_gzip = gzip -f -9 < $< > $@
2873 +# ---------------------------------------------------------------------------
2875 +# Bzip2 does not include size in file... so we have to fake that
2876 +size_append=$(CONFIG_SHELL) $(srctree)/scripts/bin_size
2878 +quiet_cmd_bzip2 = BZIP2 $@
2879 +cmd_bzip2 = (bzip2 -9 < $< ; $(size_append) $<) > $@ || (rm -f $@ ; false)
2882 +# ---------------------------------------------------------------------------
2884 +quiet_cmd_lzma = LZMA $@
2885 +cmd_lzma = (/usr/bin/lzma -9 -c $< ; $(size_append) $<) >$@ || (rm -f $@ ; false)
2887 +++ b/scripts/bin_size
2891 +if [ $# = 0 ] ; then
2892 + echo Usage: $0 file
2895 +size_dec=`stat -c "%s" $1`
2896 +size_hex_echo_string=`printf "%08x" $size_dec |
2897 + sed 's/\(..\)\(..\)\(..\)\(..\)/\\\\x\4\\\\x\3\\\\x\2\\\\x\1/g'`
2898 +/bin/echo -ne $size_hex_echo_string