1 --- a/arch/arm/boot/compressed/Makefile
2 +++ b/arch/arm/boot/compressed/Makefile
3 @@ -67,8 +67,15 @@ endif
5 SEDFLAGS = s/TEXT_START/$(ZTEXTADDR)/;s/BSS_START/$(ZBSSADDR)/
7 -targets := vmlinux vmlinux.lds piggy.gz piggy.o font.o font.c \
8 - head.o misc.o $(OBJS)
9 +suffix_$(CONFIG_KERNEL_GZIP) = gz
10 +suffix_$(CONFIG_KERNEL_BZIP2) = bz2
11 +suffix_$(CONFIG_KERNEL_LZMA) = lzma
13 +targets := vmlinux vmlinux.lds \
14 + piggy.gz piggy.gz.o \
15 + piggy.bz2 piggy.bz2.o \
16 + piggy.lzma piggy.lzma.o \
17 + font.o font.c head.o misc.o $(OBJS)
19 ifeq ($(CONFIG_FUNCTION_TRACER),y)
20 ORIG_CFLAGS := $(KBUILD_CFLAGS)
21 @@ -95,7 +102,7 @@ LDFLAGS_vmlinux += -p --no-undefined -X
22 # would otherwise mess up our GOT table
23 CFLAGS_misc.o := -Dstatic=
25 -$(obj)/vmlinux: $(obj)/vmlinux.lds $(obj)/$(HEAD) $(obj)/piggy.o \
26 +$(obj)/vmlinux: $(obj)/vmlinux.lds $(obj)/$(HEAD) $(obj)/piggy.$(suffix_y).o \
27 $(addprefix $(obj)/, $(OBJS)) FORCE
30 @@ -103,7 +110,17 @@ $(obj)/vmlinux: $(obj)/vmlinux.lds $(obj
31 $(obj)/piggy.gz: $(obj)/../Image FORCE
32 $(call if_changed,gzip)
34 -$(obj)/piggy.o: $(obj)/piggy.gz FORCE
35 +$(obj)/piggy.bz2: $(obj)/../Image FORCE
36 + $(call if_changed,bzip2)
38 +$(obj)/piggy.lzma: $(obj)/../Image FORCE
39 + $(call if_changed,lzma)
41 +$(obj)/piggy.gz.o: $(obj)/piggy.gz FORCE
43 +$(obj)/piggy.bz2.o: $(obj)/piggy.bz2 FORCE
45 +$(obj)/piggy.lzma.o: $(obj)/piggy.lzma FORCE
47 CFLAGS_font.o := -Dstatic=
49 --- a/arch/arm/boot/compressed/misc.c
50 +++ b/arch/arm/boot/compressed/misc.c
51 @@ -169,116 +169,34 @@ static inline __ptr_t memcpy(__ptr_t __d
55 -#define OF(args) args
58 -typedef unsigned char uch;
59 -typedef unsigned short ush;
60 typedef unsigned long ulg;
62 -#define WSIZE 0x8000 /* Window size must be at least 32k, */
63 - /* and a power of two */
65 -static uch *inbuf; /* input buffer */
66 -static uch window[WSIZE]; /* Sliding window buffer */
68 -static unsigned insize; /* valid bytes in inbuf */
69 -static unsigned inptr; /* index of next byte to be processed in inbuf */
70 -static unsigned outcnt; /* bytes in output buffer */
73 -#define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
74 -#define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
75 -#define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
76 -#define ORIG_NAME 0x08 /* bit 3 set: original file name present */
77 -#define COMMENT 0x10 /* bit 4 set: file comment present */
78 -#define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
79 -#define RESERVED 0xC0 /* bit 6,7: reserved */
81 -#define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
83 -/* Diagnostic functions */
85 -# define Assert(cond,msg) {if(!(cond)) error(msg);}
86 -# define Trace(x) fprintf x
87 -# define Tracev(x) {if (verbose) fprintf x ;}
88 -# define Tracevv(x) {if (verbose>1) fprintf x ;}
89 -# define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
90 -# define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
92 -# define Assert(cond,msg)
97 -# define Tracecv(c,x)
100 -static int fill_inbuf(void);
101 -static void flush_window(void);
102 -static void error(char *m);
104 extern char input_data[];
105 extern char input_data_end[];
107 -static uch *output_data;
108 -static ulg output_ptr;
109 -static ulg bytes_out;
111 static void error(char *m);
113 -static void putstr(const char *);
116 static ulg free_mem_ptr;
117 static ulg free_mem_end_ptr;
119 -#ifdef STANDALONE_DEBUG
120 -#define NO_INFLATE_MALLOC
123 #define ARCH_HAS_DECOMP_WDOG
126 +#ifdef CONFIG_KERNEL_GZIP
127 #include "../../../../lib/inflate.c"
130 -/* ===========================================================================
131 - * Fill the input buffer. This is called only when the buffer is empty
132 - * and at least one byte is really needed.
134 -int fill_inbuf(void)
137 - error("ran out of input data");
138 +#ifdef CONFIG_KERNEL_BZIP2
139 +#include "../../../../lib/decompress_bunzip2.c"
142 - inbuf = input_data;
143 - insize = &input_data_end[0] - &input_data[0];
144 +#ifdef CONFIG_KERNEL_LZMA
145 +#include "../../../../lib/decompress_unlzma.c"
152 -/* ===========================================================================
153 - * Write the output window window[0..outcnt-1] and update crc and bytes_out.
154 - * (Used for the decompressed data only.)
156 -void flush_window(void)
163 - out = &output_data[output_ptr];
164 - for (n = 0; n < outcnt; n++) {
165 - ch = *out++ = *in++;
166 - c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
169 - bytes_out += (ulg)outcnt;
170 - output_ptr += (ulg)outcnt;
176 #define arch_error(x)
177 @@ -301,16 +219,24 @@ ulg
178 decompress_kernel(ulg output_start, ulg free_mem_ptr_p, ulg free_mem_ptr_end_p,
181 - output_data = (uch *)output_start; /* Points to kernel start */
182 - free_mem_ptr = free_mem_ptr_p;
183 - free_mem_end_ptr = free_mem_ptr_end_p;
186 + size_t input_len = input_data_end - input_data;
189 __machine_arch_type = arch_id;
194 - putstr("Uncompressing Linux...");
196 + ptr = (ulg *) (((long)input_data_end) - 4);
197 + output_ptr = output_start + *ptr;
199 + free_mem_ptr = output_ptr;
200 + free_mem_end_ptr = output_ptr + 0x4000000;
202 + putstr("Decompressing Linux...");
203 + decompress(input_data, input_len,
204 + NULL, NULL, (unsigned char *) output_start, &pos, error);
205 putstr(" done, booting the kernel.\n");
208 @@ -320,11 +246,8 @@ char output_buffer[1500*1024];
212 - output_data = output_buffer;
215 putstr("Uncompressing Linux...");
217 + decompress(input_data, input_len, NULL, output_buffer, NULL);
221 --- a/arch/x86/boot/compressed/Makefile
222 +++ b/arch/x86/boot/compressed/Makefile
224 # create a compressed vmlinux image from the original vmlinux
227 -targets := vmlinux vmlinux.bin vmlinux.bin.gz head_$(BITS).o misc.o piggy.o
228 +targets := vmlinux vmlinux.bin vmlinux.bin.gz vmlinux.bin.bz2 vmlinux.bin.lzma head_$(BITS).o misc.o piggy.o
230 KBUILD_CFLAGS := -m$(BITS) -D__KERNEL__ $(LINUX_INCLUDE) -O2
231 KBUILD_CFLAGS += -fno-strict-aliasing -fPIC
232 @@ -47,9 +47,17 @@ ifeq ($(CONFIG_X86_32),y)
233 ifdef CONFIG_RELOCATABLE
234 $(obj)/vmlinux.bin.gz: $(obj)/vmlinux.bin.all FORCE
235 $(call if_changed,gzip)
236 +$(obj)/vmlinux.bin.bz2: $(obj)/vmlinux.bin.all FORCE
237 + $(call if_changed,bzip2)
238 +$(obj)/vmlinux.bin.lzma: $(obj)/vmlinux.bin.all FORCE
239 + $(call if_changed,lzma)
241 $(obj)/vmlinux.bin.gz: $(obj)/vmlinux.bin FORCE
242 $(call if_changed,gzip)
243 +$(obj)/vmlinux.bin.bz2: $(obj)/vmlinux.bin FORCE
244 + $(call if_changed,bzip2)
245 +$(obj)/vmlinux.bin.lzma: $(obj)/vmlinux.bin FORCE
246 + $(call if_changed,lzma)
248 LDFLAGS_piggy.o := -r --format binary --oformat elf32-i386 -T
250 @@ -60,5 +68,9 @@ $(obj)/vmlinux.bin.gz: $(obj)/vmlinux.bi
251 LDFLAGS_piggy.o := -r --format binary --oformat elf64-x86-64 -T
254 -$(obj)/piggy.o: $(obj)/vmlinux.scr $(obj)/vmlinux.bin.gz FORCE
255 +suffix_$(CONFIG_KERNEL_GZIP) = gz
256 +suffix_$(CONFIG_KERNEL_BZIP2) = bz2
257 +suffix_$(CONFIG_KERNEL_LZMA) = lzma
259 +$(obj)/piggy.o: $(obj)/vmlinux.scr $(obj)/vmlinux.bin.$(suffix_y) FORCE
260 $(call if_changed,ld)
261 --- a/arch/x86/boot/compressed/misc.c
262 +++ b/arch/x86/boot/compressed/misc.c
263 @@ -116,71 +116,13 @@
268 -#define OF(args) args
269 #define STATIC static
273 #define memzero(s, n) memset((s), 0, (n))
275 -typedef unsigned char uch;
276 -typedef unsigned short ush;
277 -typedef unsigned long ulg;
280 - * Window size must be at least 32k, and a power of two.
281 - * We don't actually have a window just a huge output buffer,
282 - * so we report a 2G window size, as that should always be
283 - * larger than our output buffer:
285 -#define WSIZE 0x80000000
288 -static unsigned char *inbuf;
290 -/* Sliding window buffer (and final output buffer): */
291 -static unsigned char *window;
293 -/* Valid bytes in inbuf: */
294 -static unsigned insize;
296 -/* Index of next byte to be processed in inbuf: */
297 -static unsigned inptr;
299 -/* Bytes in output buffer: */
300 -static unsigned outcnt;
302 -/* gzip flag byte */
303 -#define ASCII_FLAG 0x01 /* bit 0 set: file probably ASCII text */
304 -#define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gz file */
305 -#define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
306 -#define ORIG_NAM 0x08 /* bit 3 set: original file name present */
307 -#define COMMENT 0x10 /* bit 4 set: file comment present */
308 -#define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
309 -#define RESERVED 0xC0 /* bit 6, 7: reserved */
311 -#define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
313 -/* Diagnostic functions */
315 -# define Assert(cond, msg) do { if (!(cond)) error(msg); } while (0)
316 -# define Trace(x) do { fprintf x; } while (0)
317 -# define Tracev(x) do { if (verbose) fprintf x ; } while (0)
318 -# define Tracevv(x) do { if (verbose > 1) fprintf x ; } while (0)
319 -# define Tracec(c, x) do { if (verbose && (c)) fprintf x ; } while (0)
320 -# define Tracecv(c, x) do { if (verbose > 1 && (c)) fprintf x ; } while (0)
322 -# define Assert(cond, msg)
326 -# define Tracec(c, x)
327 -# define Tracecv(c, x)
330 -static int fill_inbuf(void);
331 -static void flush_window(void);
332 static void error(char *m);
335 @@ -189,11 +131,6 @@ static void error(char *m);
336 static struct boot_params *real_mode; /* Pointer to real-mode data */
339 -extern unsigned char input_data[];
340 -extern int input_len;
342 -static long bytes_out;
344 static void *memset(void *s, int c, unsigned n);
345 static void *memcpy(void *dest, const void *src, unsigned n);
347 @@ -213,7 +150,19 @@ static char *vidmem;
349 static int lines, cols;
353 +#ifdef CONFIG_KERNEL_GZIP
354 #include "../../../../lib/inflate.c"
357 +#ifdef CONFIG_KERNEL_BZIP2
358 +#include "../../../../lib/decompress_bunzip2.c"
361 +#ifdef CONFIG_KERNEL_LZMA
362 +#include "../../../../lib/decompress_unlzma.c"
365 static void scroll(void)
367 @@ -293,38 +242,6 @@ static void *memcpy(void *dest, const vo
371 -/* ===========================================================================
372 - * Fill the input buffer. This is called only when the buffer is empty
373 - * and at least one byte is really needed.
375 -static int fill_inbuf(void)
377 - error("ran out of input data");
381 -/* ===========================================================================
382 - * Write the output window window[0..outcnt-1] and update crc and bytes_out.
383 - * (Used for the decompressed data only.)
385 -static void flush_window(void)
387 - /* With my window equal to my output buffer
388 - * I only need to compute the crc here.
390 - unsigned long c = crc; /* temporary variable */
392 - unsigned char *in, ch;
395 - for (n = 0; n < outcnt; n++) {
397 - c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
400 - bytes_out += (unsigned long)outcnt;
404 static void error(char *x)
406 @@ -407,12 +324,8 @@ asmlinkage void decompress_kernel(void *
407 lines = real_mode->screen_info.orig_video_lines;
408 cols = real_mode->screen_info.orig_video_cols;
410 - window = output; /* Output buffer (Normally at 1M) */
411 free_mem_ptr = heap; /* Heap */
412 free_mem_end_ptr = heap + BOOT_HEAP_SIZE;
413 - inbuf = input_data; /* Input buffer */
414 - insize = input_len;
418 if ((unsigned long)output & (__KERNEL_ALIGN - 1))
419 @@ -430,10 +343,9 @@ asmlinkage void decompress_kernel(void *
425 putstr("\nDecompressing Linux... ");
427 + decompress(input_data, input_len, NULL, NULL, output, NULL, error);
430 putstr("done.\nBooting the kernel.\n");
431 --- a/arch/x86/include/asm/boot.h
432 +++ b/arch/x86/include/asm/boot.h
434 + (CONFIG_PHYSICAL_ALIGN - 1)) \
435 & ~(CONFIG_PHYSICAL_ALIGN - 1))
437 +#if (defined CONFIG_KERNEL_BZIP2)
438 +#define BOOT_HEAP_SIZE 0x400000
442 #define BOOT_HEAP_SIZE 0x7000
443 -#define BOOT_STACK_SIZE 0x4000
445 #define BOOT_HEAP_SIZE 0x4000
450 +#ifdef CONFIG_X86_64
451 +#define BOOT_STACK_SIZE 0x4000
453 #define BOOT_STACK_SIZE 0x1000
456 --- a/drivers/block/Kconfig
457 +++ b/drivers/block/Kconfig
458 @@ -358,6 +358,30 @@ config BLK_DEV_XIP
459 will prevent RAM block device backing store memory from being
460 allocated from highmem (only a problem for highmem systems).
463 + bool "Initial ramdisk compressed using bzip2"
465 + depends on BLK_DEV_INITRD=y
467 + Support loading of a bzip2 encoded initial ramdisk or cpio buffer
471 + bool "Initial ramdisk compressed using lzma"
473 + depends on BLK_DEV_INITRD=y
475 + Support loading of a lzma encoded initial ramdisk or cpio buffer
479 + bool "Initial ramdisk compressed using gzip"
481 + depends on BLK_DEV_INITRD=y
483 + Support loading of a gzip encoded initial ramdisk or cpio buffer.
487 tristate "Packet writing on CD/DVD media"
490 +++ b/include/linux/decompress/bunzip2.h
492 +#ifndef DECOMPRESS_BUNZIP2_H
493 +#define DECOMPRESS_BUNZIP2_H
495 +int bunzip2(unsigned char *inbuf, int len,
496 + int(*fill)(void*, unsigned int),
497 + int(*flush)(void*, unsigned int),
498 + unsigned char *output,
500 + void(*error)(char *x));
503 +++ b/include/linux/decompress/generic.h
505 +#ifndef DECOMPRESS_GENERIC_H
506 +#define DECOMPRESS_GENERIC_H
508 +/* Minimal chunksize to be read.
509 + *Bzip2 prefers at least 4096
510 + *Lzma prefers 0x10000 */
511 +#define COMPR_IOBUF_SIZE 4096
513 +typedef int (*decompress_fn) (unsigned char *inbuf, int len,
514 + int(*fill)(void*, unsigned int),
515 + int(*writebb)(void*, unsigned int),
516 + unsigned char *output,
518 + void(*error)(char *x));
520 +/* inbuf - input buffer
521 + *len - len of pre-read data in inbuf
522 + *fill - function to fill inbuf if empty
523 + *writebb - function to write out outbug
524 + *posp - if non-null, input position (number of bytes read) will be
527 + *If len != 0, the inbuf is initialized (with as much data), and fill
528 + *should not be called
529 + *If len = 0, the inbuf is allocated, but empty. Its size is IOBUF_SIZE
530 + *fill should be called (repeatedly...) to read data, at most IOBUF_SIZE
536 +++ b/include/linux/decompress/inflate.h
541 +/* Other housekeeping constants */
542 +#define INBUFSIZ 4096
544 +int gunzip(unsigned char *inbuf, int len,
545 + int(*fill)(void*, unsigned int),
546 + int(*flush)(void*, unsigned int),
547 + unsigned char *output,
549 + void(*error_fn)(char *x));
552 +++ b/include/linux/decompress/mm.h
557 + * Memory management for pre-boot and ramdisk uncompressors
559 + * Authors: Alain Knaff <alain@knaff.lu>
563 +#ifndef DECOMPR_MM_H
564 +#define DECOMPR_MM_H
568 +/* Code active when included from pre-boot environment: */
570 +/* A trivial malloc implementation, adapted from
571 + * malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994
573 +static unsigned long malloc_ptr;
574 +static int malloc_count;
576 +static void *malloc(int size)
581 + error("Malloc error");
583 + malloc_ptr = free_mem_ptr;
585 + malloc_ptr = (malloc_ptr + 3) & ~3; /* Align */
587 + p = (void *)malloc_ptr;
588 + malloc_ptr += size;
590 + if (free_mem_end_ptr && malloc_ptr >= free_mem_end_ptr)
591 + error("Out of memory");
597 +static void free(void *where)
601 + malloc_ptr = free_mem_ptr;
604 +#define large_malloc(a) malloc(a)
605 +#define large_free(a) free(a)
607 +#define set_error_fn(x)
614 +/* Code active when compiled standalone for use when loading ramdisk: */
616 +#include <linux/kernel.h>
617 +#include <linux/fs.h>
618 +#include <linux/string.h>
619 +#include <linux/vmalloc.h>
621 +/* Use defines rather than static inline in order to avoid spurious
622 + * warnings when not needed (indeed large_malloc / large_free are not
623 + * needed by inflate */
625 +#define malloc(a) kmalloc(a, GFP_KERNEL)
626 +#define free(a) kfree(a)
628 +#define large_malloc(a) vmalloc(a)
629 +#define large_free(a) vfree(a)
631 +static void(*error)(char *m);
632 +#define set_error_fn(x) error = x;
638 +#include <linux/init.h>
642 +#endif /* DECOMPR_MM_H */
644 +++ b/include/linux/decompress/unlzma.h
646 +#ifndef DECOMPRESS_UNLZMA_H
647 +#define DECOMPRESS_UNLZMA_H
649 +int unlzma(unsigned char *, int,
650 + int(*fill)(void*, unsigned int),
651 + int(*flush)(void*, unsigned int),
652 + unsigned char *output,
654 + void(*error)(char *x)
660 @@ -101,6 +101,56 @@ config LOCALVERSION_AUTO
662 which is done within the script "scripts/setlocalversion".)
665 + prompt "Kernel compression mode"
666 + default KERNEL_GZIP
668 + The linux kernel is a kind of self-extracting executable.
669 + Several compression algorithms are available, which differ
670 + in efficiency, compression and decompression speed.
671 + Compression speed is only relevant when building a kernel.
672 + Decompression speed is relevant at each boot.
674 + If you have any problems with bzip2 or lzma compressed
675 + kernels, mail me (Alain Knaff) <alain@knaff.lu>. (An older
676 + version of this functionality (bzip2 only), for 2.4, was
677 + supplied by Christian Ludwig)
679 + High compression options are mostly useful for users, who
680 + are low on disk space (embedded systems), but for whom ram
683 + If in doubt, select 'gzip'
688 + The old and tried gzip compression. Its compression ratio is
689 + the poorest among the 3 choices; however its speed (both
690 + compression and decompression) is the fastest.
695 + Its compression ratio and speed is intermediate.
696 + Decompression speed is slowest among the 3.
697 + The kernel size is about 10 per cent smaller with bzip2,
698 + in comparison to gzip.
699 + Bzip2 uses a large amount of memory. For modern kernels
700 + you will need at least 8MB RAM or more for booting.
705 + The most recent compression algorithm.
706 + Its ratio is best, decompression speed is between the other
707 + 2. Compression is slowest.
708 + The kernel size is about 33 per cent smaller with lzma,
709 + in comparison to gzip.
715 bool "Support for paging of anonymous memory (swap)"
716 depends on MMU && BLOCK
717 --- a/init/do_mounts_rd.c
718 +++ b/init/do_mounts_rd.c
721 #include "do_mounts.h"
723 +#include <linux/decompress/generic.h>
725 +#include <linux/decompress/bunzip2.h>
726 +#include <linux/decompress/unlzma.h>
727 +#include <linux/decompress/inflate.h>
729 int __initdata rd_prompt = 1;/* 1 = prompt for RAM disk, 0 = don't prompt */
731 static int __init prompt_ramdisk(char *str)
732 @@ -29,7 +35,7 @@ static int __init ramdisk_start_setup(ch
734 __setup("ramdisk_start=", ramdisk_start_setup);
736 -static int __init crd_load(int in_fd, int out_fd);
737 +static int __init crd_load(int in_fd, int out_fd, decompress_fn deco);
740 * This routine tries to find a RAM disk image to load, and returns the
741 @@ -46,7 +52,7 @@ static int __init crd_load(int in_fd, in
745 -identify_ramdisk_image(int fd, int start_block)
746 +identify_ramdisk_image(int fd, int start_block, decompress_fn *decompressor)
748 const int size = 512;
749 struct minix_super_block *minixsb;
750 @@ -74,6 +80,7 @@ identify_ramdisk_image(int fd, int start
751 sys_lseek(fd, start_block * BLOCK_SIZE, 0);
752 sys_read(fd, buf, size);
754 +#ifdef CONFIG_RD_GZIP
756 * If it matches the gzip magic numbers, return 0
758 @@ -81,9 +88,39 @@ identify_ramdisk_image(int fd, int start
760 "RAMDISK: Compressed image found at block %d\n",
762 + *decompressor = gunzip;
768 +#ifdef CONFIG_RD_BZIP2
770 + * If it matches the bzip2 magic numbers, return -1
772 + if (buf[0] == 0x42 && (buf[1] == 0x5a)) {
774 + "RAMDISK: Bzipped image found at block %d\n",
776 + *decompressor = bunzip2;
782 +#ifdef CONFIG_RD_LZMA
784 + * If it matches the lzma magic numbers, return -1
786 + if (buf[0] == 0x5d && (buf[1] == 0x00)) {
788 + "RAMDISK: Lzma image found at block %d\n",
790 + *decompressor = unlzma;
796 /* romfs is at block zero too */
797 if (romfsb->word0 == ROMSB_WORD0 &&
798 @@ -156,6 +193,7 @@ int __init rd_load_image(char *from)
799 int nblocks, i, disk;
801 unsigned short rotate = 0;
802 + decompress_fn decompressor = NULL;
803 #if !defined(CONFIG_S390) && !defined(CONFIG_PPC_ISERIES)
804 char rotator[4] = { '|' , '/' , '-' , '\\' };
806 @@ -168,12 +206,12 @@ int __init rd_load_image(char *from)
810 - nblocks = identify_ramdisk_image(in_fd, rd_image_start);
811 + nblocks = identify_ramdisk_image(in_fd, rd_image_start, &decompressor);
816 - if (crd_load(in_fd, out_fd) == 0)
817 + if (crd_load(in_fd, out_fd, decompressor) == 0)
818 goto successful_load;
821 @@ -272,138 +310,48 @@ int __init rd_load_disk(int n)
822 return rd_load_image("/dev/root");
826 - * gzip declarations
829 -#define OF(args) args
832 -#define memzero(s, n) memset ((s), 0, (n))
835 -typedef unsigned char uch;
836 -typedef unsigned short ush;
837 -typedef unsigned long ulg;
839 -#define INBUFSIZ 4096
840 -#define WSIZE 0x8000 /* window size--must be a power of two, and */
841 - /* at least 32K for zip's deflate method */
846 -static unsigned insize; /* valid bytes in inbuf */
847 -static unsigned inptr; /* index of next byte to be processed in inbuf */
848 -static unsigned outcnt; /* bytes in output buffer */
849 static int exit_code;
850 -static int unzip_error;
851 -static long bytes_out;
852 +static int decompress_error;
853 static int crd_infd, crd_outfd;
855 -#define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
857 -/* Diagnostic functions (stubbed out) */
858 -#define Assert(cond,msg)
863 -#define Tracecv(c,x)
865 -#define STATIC static
868 -static int __init fill_inbuf(void);
869 -static void __init flush_window(void);
870 -static void __init error(char *m);
872 -#define NO_INFLATE_MALLOC
874 -#include "../lib/inflate.c"
876 -/* ===========================================================================
877 - * Fill the input buffer. This is called only when the buffer is empty
878 - * and at least one byte is really needed.
879 - * Returning -1 does not guarantee that gunzip() will ever return.
881 -static int __init fill_inbuf(void)
882 +static int __init compr_fill(void *buf, unsigned int len)
884 - if (exit_code) return -1;
886 - insize = sys_read(crd_infd, inbuf, INBUFSIZ);
888 - error("RAMDISK: ran out of compressed data");
895 + int r = sys_read(crd_infd, buf, len);
897 + printk(KERN_ERR "RAMDISK: error while reading compressed data");
899 + printk(KERN_ERR "RAMDISK: EOF while reading compressed data");
903 -/* ===========================================================================
904 - * Write the output window window[0..outcnt-1] and update crc and bytes_out.
905 - * (Used for the decompressed data only.)
907 -static void __init flush_window(void)
908 +static int __init compr_flush(void *window, unsigned int outcnt)
910 - ulg c = crc; /* temporary variable */
911 - unsigned n, written;
914 - written = sys_write(crd_outfd, window, outcnt);
915 - if (written != outcnt && unzip_error == 0) {
916 - printk(KERN_ERR "RAMDISK: incomplete write (%d != %d) %ld\n",
917 - written, outcnt, bytes_out);
921 - for (n = 0; n < outcnt; n++) {
923 - c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
926 - bytes_out += (ulg)outcnt;
928 + int written = sys_write(crd_outfd, window, outcnt);
929 + if (written != outcnt) {
930 + if (decompress_error == 0)
932 + "RAMDISK: incomplete write (%d != %d)\n",
934 + decompress_error = 1;
940 static void __init error(char *x)
942 printk(KERN_ERR "%s\n", x);
945 + decompress_error = 1;
948 -static int __init crd_load(int in_fd, int out_fd)
949 +static int __init crd_load(int in_fd, int out_fd, decompress_fn deco)
953 - insize = 0; /* valid bytes in inbuf */
954 - inptr = 0; /* index of next byte to be processed in inbuf */
955 - outcnt = 0; /* bytes in output buffer */
958 - crc = (ulg)0xffffffffL; /* shift register contents */
962 - inbuf = kmalloc(INBUFSIZ, GFP_KERNEL);
964 - printk(KERN_ERR "RAMDISK: Couldn't allocate gzip buffer\n");
967 - window = kmalloc(WSIZE, GFP_KERNEL);
969 - printk(KERN_ERR "RAMDISK: Couldn't allocate gzip window\n");
976 + result = deco(NULL, 0, compr_fill, compr_flush, NULL, NULL, error);
977 + if (decompress_error)
983 --- a/init/initramfs.c
984 +++ b/init/initramfs.c
985 @@ -389,11 +389,14 @@ static int __init write_buffer(char *buf
989 -static void __init flush_buffer(char *buf, unsigned len)
991 +static int __init flush_buffer(void *bufv, unsigned len)
993 + char *buf = (char *) bufv;
999 while ((written = write_buffer(buf, len)) < len && !message) {
1000 char c = buf[written];
1002 @@ -407,73 +410,14 @@ static void __init flush_buffer(char *bu
1004 error("junk in compressed archive");
1010 - * gzip declarations
1013 -#define OF(args) args
1016 -#define memzero(s, n) memset ((s), 0, (n))
1018 +static unsigned my_inptr; /* index of next byte to be processed in inbuf */
1020 -typedef unsigned char uch;
1021 -typedef unsigned short ush;
1022 -typedef unsigned long ulg;
1024 -#define WSIZE 0x8000 /* window size--must be a power of two, and */
1025 - /* at least 32K for zip's deflate method */
1028 -static uch *window;
1030 -static unsigned insize; /* valid bytes in inbuf */
1031 -static unsigned inptr; /* index of next byte to be processed in inbuf */
1032 -static unsigned outcnt; /* bytes in output buffer */
1033 -static long bytes_out;
1035 -#define get_byte() (inptr < insize ? inbuf[inptr++] : -1)
1037 -/* Diagnostic functions (stubbed out) */
1038 -#define Assert(cond,msg)
1042 -#define Tracec(c,x)
1043 -#define Tracecv(c,x)
1045 -#define STATIC static
1046 -#define INIT __init
1048 -static void __init flush_window(void);
1049 -static void __init error(char *m);
1051 -#define NO_INFLATE_MALLOC
1053 -#include "../lib/inflate.c"
1055 -/* ===========================================================================
1056 - * Write the output window window[0..outcnt-1] and update crc and bytes_out.
1057 - * (Used for the decompressed data only.)
1059 -static void __init flush_window(void)
1061 - ulg c = crc; /* temporary variable */
1065 - flush_buffer(window, outcnt);
1067 - for (n = 0; n < outcnt; n++) {
1069 - c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
1072 - bytes_out += (ulg)outcnt;
1075 +#include <linux/decompress/bunzip2.h>
1076 +#include <linux/decompress/unlzma.h>
1077 +#include <linux/decompress/inflate.h>
1079 static char * __init unpack_to_rootfs(char *buf, unsigned len, int check_only)
1081 @@ -482,9 +426,10 @@ static char * __init unpack_to_rootfs(ch
1082 header_buf = kmalloc(110, GFP_KERNEL);
1083 symlink_buf = kmalloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1, GFP_KERNEL);
1084 name_buf = kmalloc(N_ALIGN(PATH_MAX), GFP_KERNEL);
1085 - window = kmalloc(WSIZE, GFP_KERNEL);
1086 - if (!window || !header_buf || !symlink_buf || !name_buf)
1088 + if (!header_buf || !symlink_buf || !name_buf)
1089 panic("can't allocate buffers");
1094 @@ -504,22 +449,38 @@ static char * __init unpack_to_rootfs(ch
1101 - outcnt = 0; /* bytes in output buffer */
1103 - crc = (ulg)0xffffffffL; /* shift register contents */
1106 + if (!gunzip(buf, len, NULL, flush_buffer, NULL,
1107 + &my_inptr, error) &&
1111 +#ifdef CONFIG_RD_BZIP2
1112 + message = NULL; /* Zero out message, or else cpio will think an error has already occured */
1113 + if (!bunzip2(buf, len, NULL, flush_buffer, NULL,
1114 + &my_inptr, error) < 0
1116 + message == NULL) {
1121 +#ifdef CONFIG_RD_LZMA
1122 + message = NULL; /* Zero out message, or else cpio will think an error has already occured */
1123 + if (!unlzma(buf, len, NULL, flush_buffer, NULL,
1124 + &my_inptr, error) < 0
1126 + message == NULL) {
1132 - error("junk in gzipped archive");
1133 - this_header = saved_offset + inptr;
1136 + error("junk in compressed archive");
1137 + this_header = saved_offset + my_inptr;
1148 @@ -11,7 +11,8 @@ lib-y := ctype.o string.o vsprintf.o cmd
1149 rbtree.o radix-tree.o dump_stack.o \
1150 idr.o int_sqrt.o extable.o prio_tree.o \
1151 sha1.o irq_regs.o reciprocal_div.o argv_split.o \
1152 - proportions.o prio_heap.o ratelimit.o show_mem.o
1153 + proportions.o prio_heap.o ratelimit.o show_mem.o \
1154 + inflate.o decompress_bunzip2.o decompress_unlzma.o
1156 lib-$(CONFIG_MMU) += ioremap.o
1157 lib-$(CONFIG_SMP) += cpumask.o
1159 +++ b/lib/decompress_bunzip2.c
1161 +/* vi: set sw = 4 ts = 4: */
1162 +/* Small bzip2 deflate implementation, by Rob Landley (rob@landley.net).
1164 + Based on bzip2 decompression code by Julian R Seward (jseward@acm.org),
1165 + which also acknowledges contributions by Mike Burrows, David Wheeler,
1166 + Peter Fenwick, Alistair Moffat, Radford Neal, Ian H. Witten,
1167 + Robert Sedgewick, and Jon L. Bentley.
1169 + This code is licensed under the LGPLv2:
1170 + LGPL (http://www.gnu.org/copyleft/lgpl.html
1174 + Size and speed optimizations by Manuel Novoa III (mjn3@codepoet.org).
1176 + More efficient reading of Huffman codes, a streamlined read_bunzip()
1177 + function, and various other tweaks. In (limited) tests, approximately
1178 + 20% faster than bzcat on x86 and about 10% faster on arm.
1180 + Note that about 2/3 of the time is spent in read_unzip() reversing
1181 + the Burrows-Wheeler transformation. Much of that time is delay
1182 + resulting from cache misses.
1184 + I would ask that anyone benefiting from this work, especially those
1185 + using it in commercial products, consider making a donation to my local
1186 + non-profit hospice organization in the name of the woman I loved, who
1187 + passed away Feb. 12, 2003.
1189 + In memory of Toni W. Hagan
1191 + Hospice of Acadiana, Inc.
1192 + 2600 Johnston St., Suite 200
1193 + Lafayette, LA 70503-3240
1195 + Phone (337) 232-1234 or 1-800-738-2226
1196 + Fax (337) 232-1297
1198 + http://www.hospiceacadiana.com/
1204 + Made it fit for running in Linux Kernel by Alain Knaff (alain@knaff.lu)
1209 +#include <linux/decompress/bunzip2.h>
1210 +#endif /* !STATIC */
1212 +#include <linux/decompress/mm.h>
1215 +#define INT_MAX 0x7fffffff
1218 +/* Constants for Huffman coding */
1219 +#define MAX_GROUPS 6
1220 +#define GROUP_SIZE 50 /* 64 would have been more efficient */
1221 +#define MAX_HUFCODE_BITS 20 /* Longest Huffman code allowed */
1222 +#define MAX_SYMBOLS 258 /* 256 literals + RUNA + RUNB */
1223 +#define SYMBOL_RUNA 0
1224 +#define SYMBOL_RUNB 1
1226 +/* Status return values */
1227 +#define RETVAL_OK 0
1228 +#define RETVAL_LAST_BLOCK (-1)
1229 +#define RETVAL_NOT_BZIP_DATA (-2)
1230 +#define RETVAL_UNEXPECTED_INPUT_EOF (-3)
1231 +#define RETVAL_UNEXPECTED_OUTPUT_EOF (-4)
1232 +#define RETVAL_DATA_ERROR (-5)
1233 +#define RETVAL_OUT_OF_MEMORY (-6)
1234 +#define RETVAL_OBSOLETE_INPUT (-7)
1236 +/* Other housekeeping constants */
1237 +#define BZIP2_IOBUF_SIZE 4096
1239 +/* This is what we know about each Huffman coding group */
1240 +struct group_data {
1241 + /* We have an extra slot at the end of limit[] for a sentinal value. */
1242 + int limit[MAX_HUFCODE_BITS+1];
1243 + int base[MAX_HUFCODE_BITS];
1244 + int permute[MAX_SYMBOLS];
1245 + int minLen, maxLen;
1248 +/* Structure holding all the housekeeping data, including IO buffers and
1249 + memory that persists between calls to bunzip */
1250 +struct bunzip_data {
1251 + /* State for interrupting output loop */
1252 + int writeCopies, writePos, writeRunCountdown, writeCount, writeCurrent;
1253 + /* I/O tracking data (file handles, buffers, positions, etc.) */
1254 + int (*fill)(void*, unsigned int);
1255 + int inbufCount, inbufPos /*, outbufPos*/;
1256 + unsigned char *inbuf /*,*outbuf*/;
1257 + unsigned int inbufBitCount, inbufBits;
1258 + /* The CRC values stored in the block header and calculated from the
1260 + unsigned int crc32Table[256], headerCRC, totalCRC, writeCRC;
1261 + /* Intermediate buffer and its size (in bytes) */
1262 + unsigned int *dbuf, dbufSize;
1263 + /* These things are a bit too big to go on the stack */
1264 + unsigned char selectors[32768]; /* nSelectors = 15 bits */
1265 + struct group_data groups[MAX_GROUPS]; /* Huffman coding tables */
1266 + int io_error; /* non-zero if we have IO error */
1270 +/* Return the next nnn bits of input. All reads from the compressed input
1271 + are done through this function. All reads are big endian */
1272 +static unsigned int INIT get_bits(struct bunzip_data *bd, char bits_wanted)
1274 + unsigned int bits = 0;
1276 + /* If we need to get more data from the byte buffer, do so.
1277 + (Loop getting one byte at a time to enforce endianness and avoid
1278 + unaligned access.) */
1279 + while (bd->inbufBitCount < bits_wanted) {
1280 + /* If we need to read more data from file into byte buffer, do
1282 + if (bd->inbufPos == bd->inbufCount) {
1285 + bd->inbufCount = bd->fill(bd->inbuf, BZIP2_IOBUF_SIZE);
1286 + if (bd->inbufCount <= 0) {
1287 + bd->io_error = RETVAL_UNEXPECTED_INPUT_EOF;
1292 + /* Avoid 32-bit overflow (dump bit buffer to top of output) */
1293 + if (bd->inbufBitCount >= 24) {
1294 + bits = bd->inbufBits&((1 << bd->inbufBitCount)-1);
1295 + bits_wanted -= bd->inbufBitCount;
1296 + bits <<= bits_wanted;
1297 + bd->inbufBitCount = 0;
1299 + /* Grab next 8 bits of input from buffer. */
1300 + bd->inbufBits = (bd->inbufBits << 8)|bd->inbuf[bd->inbufPos++];
1301 + bd->inbufBitCount += 8;
1303 + /* Calculate result */
1304 + bd->inbufBitCount -= bits_wanted;
1305 + bits |= (bd->inbufBits >> bd->inbufBitCount)&((1 << bits_wanted)-1);
1310 +/* Unpacks the next block and sets up for the inverse burrows-wheeler step. */
1312 +static int INIT get_next_block(struct bunzip_data *bd)
1314 + struct group_data *hufGroup = NULL;
1316 + int *limit = NULL;
1317 + int dbufCount, nextSym, dbufSize, groupCount, selector,
1318 + i, j, k, t, runPos, symCount, symTotal, nSelectors,
1320 + unsigned char uc, symToByte[256], mtfSymbol[256], *selectors;
1321 + unsigned int *dbuf, origPtr;
1324 + dbufSize = bd->dbufSize;
1325 + selectors = bd->selectors;
1327 + /* Read in header signature and CRC, then validate signature.
1328 + (last block signature means CRC is for whole file, return now) */
1329 + i = get_bits(bd, 24);
1330 + j = get_bits(bd, 24);
1331 + bd->headerCRC = get_bits(bd, 32);
1332 + if ((i == 0x177245) && (j == 0x385090))
1333 + return RETVAL_LAST_BLOCK;
1334 + if ((i != 0x314159) || (j != 0x265359))
1335 + return RETVAL_NOT_BZIP_DATA;
1336 + /* We can add support for blockRandomised if anybody complains.
1337 + There was some code for this in busybox 1.0.0-pre3, but nobody ever
1338 + noticed that it didn't actually work. */
1339 + if (get_bits(bd, 1))
1340 + return RETVAL_OBSOLETE_INPUT;
1341 + origPtr = get_bits(bd, 24);
1342 + if (origPtr > dbufSize)
1343 + return RETVAL_DATA_ERROR;
1344 + /* mapping table: if some byte values are never used (encoding things
1345 + like ascii text), the compression code removes the gaps to have fewer
1346 + symbols to deal with, and writes a sparse bitfield indicating which
1347 + values were present. We make a translation table to convert the
1348 + symbols back to the corresponding bytes. */
1349 + t = get_bits(bd, 16);
1351 + for (i = 0; i < 16; i++) {
1352 + if (t&(1 << (15-i))) {
1353 + k = get_bits(bd, 16);
1354 + for (j = 0; j < 16; j++)
1355 + if (k&(1 << (15-j)))
1356 + symToByte[symTotal++] = (16*i)+j;
1359 + /* How many different Huffman coding groups does this block use? */
1360 + groupCount = get_bits(bd, 3);
1361 + if (groupCount < 2 || groupCount > MAX_GROUPS)
1362 + return RETVAL_DATA_ERROR;
1363 + /* nSelectors: Every GROUP_SIZE many symbols we select a new
1364 + Huffman coding group. Read in the group selector list,
1365 + which is stored as MTF encoded bit runs. (MTF = Move To
1366 + Front, as each value is used it's moved to the start of the
1368 + nSelectors = get_bits(bd, 15);
1370 + return RETVAL_DATA_ERROR;
1371 + for (i = 0; i < groupCount; i++)
1373 + for (i = 0; i < nSelectors; i++) {
1374 + /* Get next value */
1375 + for (j = 0; get_bits(bd, 1); j++)
1376 + if (j >= groupCount)
1377 + return RETVAL_DATA_ERROR;
1378 + /* Decode MTF to get the next selector */
1379 + uc = mtfSymbol[j];
1381 + mtfSymbol[j] = mtfSymbol[j-1];
1382 + mtfSymbol[0] = selectors[i] = uc;
1384 + /* Read the Huffman coding tables for each group, which code
1385 + for symTotal literal symbols, plus two run symbols (RUNA,
1387 + symCount = symTotal+2;
1388 + for (j = 0; j < groupCount; j++) {
1389 + unsigned char length[MAX_SYMBOLS], temp[MAX_HUFCODE_BITS+1];
1390 + int minLen, maxLen, pp;
1391 + /* Read Huffman code lengths for each symbol. They're
1392 + stored in a way similar to mtf; record a starting
1393 + value for the first symbol, and an offset from the
1394 + previous value for everys symbol after that.
1395 + (Subtracting 1 before the loop and then adding it
1396 + back at the end is an optimization that makes the
1397 + test inside the loop simpler: symbol length 0
1398 + becomes negative, so an unsigned inequality catches
1400 + t = get_bits(bd, 5)-1;
1401 + for (i = 0; i < symCount; i++) {
1403 + if (((unsigned)t) > (MAX_HUFCODE_BITS-1))
1404 + return RETVAL_DATA_ERROR;
1406 + /* If first bit is 0, stop. Else
1407 + second bit indicates whether to
1408 + increment or decrement the value.
1409 + Optimization: grab 2 bits and unget
1410 + the second if the first was 0. */
1412 + k = get_bits(bd, 2);
1414 + bd->inbufBitCount++;
1417 + /* Add one if second bit 1, else
1418 + * subtract 1. Avoids if/else */
1419 + t += (((k+1)&2)-1);
1421 + /* Correct for the initial -1, to get the
1422 + * final symbol length */
1425 + /* Find largest and smallest lengths in this group */
1426 + minLen = maxLen = length[0];
1428 + for (i = 1; i < symCount; i++) {
1429 + if (length[i] > maxLen)
1430 + maxLen = length[i];
1431 + else if (length[i] < minLen)
1432 + minLen = length[i];
1435 + /* Calculate permute[], base[], and limit[] tables from
1438 + * permute[] is the lookup table for converting
1439 + * Huffman coded symbols into decoded symbols. base[]
1440 + * is the amount to subtract from the value of a
1441 + * Huffman symbol of a given length when using
1444 + * limit[] indicates the largest numerical value a
1445 + * symbol with a given number of bits can have. This
1446 + * is how the Huffman codes can vary in length: each
1447 + * code with a value > limit[length] needs another
1450 + hufGroup = bd->groups+j;
1451 + hufGroup->minLen = minLen;
1452 + hufGroup->maxLen = maxLen;
1453 + /* Note that minLen can't be smaller than 1, so we
1454 + adjust the base and limit array pointers so we're
1455 + not always wasting the first entry. We do this
1456 + again when using them (during symbol decoding).*/
1457 + base = hufGroup->base-1;
1458 + limit = hufGroup->limit-1;
1459 + /* Calculate permute[]. Concurently, initialize
1460 + * temp[] and limit[]. */
1462 + for (i = minLen; i <= maxLen; i++) {
1463 + temp[i] = limit[i] = 0;
1464 + for (t = 0; t < symCount; t++)
1465 + if (length[t] == i)
1466 + hufGroup->permute[pp++] = t;
1468 + /* Count symbols coded for at each bit length */
1469 + for (i = 0; i < symCount; i++)
1470 + temp[length[i]]++;
1471 + /* Calculate limit[] (the largest symbol-coding value
1472 + *at each bit length, which is (previous limit <<
1473 + *1)+symbols at this level), and base[] (number of
1474 + *symbols to ignore at each bit length, which is limit
1475 + *minus the cumulative count of symbols coded for
1478 + for (i = minLen; i < maxLen; i++) {
1480 + /* We read the largest possible symbol size
1481 + and then unget bits after determining how
1482 + many we need, and those extra bits could be
1483 + set to anything. (They're noise from
1484 + future symbols.) At each level we're
1485 + really only interested in the first few
1486 + bits, so here we set all the trailing
1487 + to-be-ignored bits to 1 so they don't
1488 + affect the value > limit[length]
1490 + limit[i] = (pp << (maxLen - i)) - 1;
1492 + base[i+1] = pp-(t += temp[i]);
1494 + limit[maxLen+1] = INT_MAX; /* Sentinal value for
1495 + * reading next sym. */
1496 + limit[maxLen] = pp+temp[maxLen]-1;
1499 + /* We've finished reading and digesting the block header. Now
1500 + read this block's Huffman coded symbols from the file and
1501 + undo the Huffman coding and run length encoding, saving the
1502 + result into dbuf[dbufCount++] = uc */
1504 + /* Initialize symbol occurrence counters and symbol Move To
1506 + for (i = 0; i < 256; i++) {
1508 + mtfSymbol[i] = (unsigned char)i;
1510 + /* Loop through compressed symbols. */
1511 + runPos = dbufCount = symCount = selector = 0;
1513 + /* Determine which Huffman coding group to use. */
1514 + if (!(symCount--)) {
1515 + symCount = GROUP_SIZE-1;
1516 + if (selector >= nSelectors)
1517 + return RETVAL_DATA_ERROR;
1518 + hufGroup = bd->groups+selectors[selector++];
1519 + base = hufGroup->base-1;
1520 + limit = hufGroup->limit-1;
1522 + /* Read next Huffman-coded symbol. */
1523 + /* Note: It is far cheaper to read maxLen bits and
1524 + back up than it is to read minLen bits and then an
1525 + additional bit at a time, testing as we go.
1526 + Because there is a trailing last block (with file
1527 + CRC), there is no danger of the overread causing an
1528 + unexpected EOF for a valid compressed file. As a
1529 + further optimization, we do the read inline
1530 + (falling back to a call to get_bits if the buffer
1531 + runs dry). The following (up to got_huff_bits:) is
1532 + equivalent to j = get_bits(bd, hufGroup->maxLen);
1534 + while (bd->inbufBitCount < hufGroup->maxLen) {
1535 + if (bd->inbufPos == bd->inbufCount) {
1536 + j = get_bits(bd, hufGroup->maxLen);
1537 + goto got_huff_bits;
1540 + (bd->inbufBits << 8)|bd->inbuf[bd->inbufPos++];
1541 + bd->inbufBitCount += 8;
1543 + bd->inbufBitCount -= hufGroup->maxLen;
1544 + j = (bd->inbufBits >> bd->inbufBitCount)&
1545 + ((1 << hufGroup->maxLen)-1);
1547 + /* Figure how how many bits are in next symbol and
1549 + i = hufGroup->minLen;
1550 + while (j > limit[i])
1552 + bd->inbufBitCount += (hufGroup->maxLen - i);
1553 + /* Huffman decode value to get nextSym (with bounds checking) */
1554 + if ((i > hufGroup->maxLen)
1555 + || (((unsigned)(j = (j>>(hufGroup->maxLen-i))-base[i]))
1557 + return RETVAL_DATA_ERROR;
1558 + nextSym = hufGroup->permute[j];
1559 + /* We have now decoded the symbol, which indicates
1560 + either a new literal byte, or a repeated run of the
1561 + most recent literal byte. First, check if nextSym
1562 + indicates a repeated run, and if so loop collecting
1563 + how many times to repeat the last literal. */
1564 + if (((unsigned)nextSym) <= SYMBOL_RUNB) { /* RUNA or RUNB */
1565 + /* If this is the start of a new run, zero out
1571 + /* Neat trick that saves 1 symbol: instead of
1572 + or-ing 0 or 1 at each bit position, add 1
1573 + or 2 instead. For example, 1011 is 1 << 0
1574 + + 1 << 1 + 2 << 2. 1010 is 2 << 0 + 2 << 1
1575 + + 1 << 2. You can make any bit pattern
1576 + that way using 1 less symbol than the basic
1577 + or 0/1 method (except all bits 0, which
1578 + would use no symbols, but a run of length 0
1579 + doesn't mean anything in this context).
1580 + Thus space is saved. */
1581 + t += (runPos << nextSym);
1582 + /* +runPos if RUNA; +2*runPos if RUNB */
1587 + /* When we hit the first non-run symbol after a run,
1588 + we now know how many times to repeat the last
1589 + literal, so append that many copies to our buffer
1590 + of decoded symbols (dbuf) now. (The last literal
1591 + used is the one at the head of the mtfSymbol
1595 + if (dbufCount+t >= dbufSize)
1596 + return RETVAL_DATA_ERROR;
1598 + uc = symToByte[mtfSymbol[0]];
1599 + byteCount[uc] += t;
1601 + dbuf[dbufCount++] = uc;
1603 + /* Is this the terminating symbol? */
1604 + if (nextSym > symTotal)
1606 + /* At this point, nextSym indicates a new literal
1607 + character. Subtract one to get the position in the
1608 + MTF array at which this literal is currently to be
1609 + found. (Note that the result can't be -1 or 0,
1610 + because 0 and 1 are RUNA and RUNB. But another
1611 + instance of the first symbol in the mtf array,
1612 + position 0, would have been handled as part of a
1613 + run above. Therefore 1 unused mtf position minus 2
1614 + non-literal nextSym values equals -1.) */
1615 + if (dbufCount >= dbufSize)
1616 + return RETVAL_DATA_ERROR;
1618 + uc = mtfSymbol[i];
1619 + /* Adjust the MTF array. Since we typically expect to
1620 + *move only a small number of symbols, and are bound
1621 + *by 256 in any case, using memmove here would
1622 + *typically be bigger and slower due to function call
1623 + *overhead and other assorted setup costs. */
1625 + mtfSymbol[i] = mtfSymbol[i-1];
1627 + mtfSymbol[0] = uc;
1628 + uc = symToByte[uc];
1629 + /* We have our literal byte. Save it into dbuf. */
1631 + dbuf[dbufCount++] = (unsigned int)uc;
1633 + /* At this point, we've read all the Huffman-coded symbols
1634 + (and repeated runs) for this block from the input stream,
1635 + and decoded them into the intermediate buffer. There are
1636 + dbufCount many decoded bytes in dbuf[]. Now undo the
1637 + Burrows-Wheeler transform on dbuf. See
1638 + http://dogma.net/markn/articles/bwt/bwt.htm
1640 + /* Turn byteCount into cumulative occurrence counts of 0 to n-1. */
1642 + for (i = 0; i < 256; i++) {
1643 + k = j+byteCount[i];
1647 + /* Figure out what order dbuf would be in if we sorted it. */
1648 + for (i = 0; i < dbufCount; i++) {
1649 + uc = (unsigned char)(dbuf[i] & 0xff);
1650 + dbuf[byteCount[uc]] |= (i << 8);
1653 + /* Decode first byte by hand to initialize "previous" byte.
1654 + Note that it doesn't get output, and if the first three
1655 + characters are identical it doesn't qualify as a run (hence
1656 + writeRunCountdown = 5). */
1658 + if (origPtr >= dbufCount)
1659 + return RETVAL_DATA_ERROR;
1660 + bd->writePos = dbuf[origPtr];
1661 + bd->writeCurrent = (unsigned char)(bd->writePos&0xff);
1662 + bd->writePos >>= 8;
1663 + bd->writeRunCountdown = 5;
1665 + bd->writeCount = dbufCount;
1670 +/* Undo burrows-wheeler transform on intermediate buffer to produce output.
1671 + If start_bunzip was initialized with out_fd =-1, then up to len bytes of
1672 + data are written to outbuf. Return value is number of bytes written or
1673 + error (all errors are negative numbers). If out_fd!=-1, outbuf and len
1674 + are ignored, data is written to out_fd and return is RETVAL_OK or error.
1677 +static int INIT read_bunzip(struct bunzip_data *bd, char *outbuf, int len)
1679 + const unsigned int *dbuf;
1680 + int pos, xcurrent, previous, gotcount;
1682 + /* If last read was short due to end of file, return last block now */
1683 + if (bd->writeCount < 0)
1684 + return bd->writeCount;
1688 + pos = bd->writePos;
1689 + xcurrent = bd->writeCurrent;
1691 + /* We will always have pending decoded data to write into the output
1692 + buffer unless this is the very first call (in which case we haven't
1693 + Huffman-decoded a block into the intermediate buffer yet). */
1695 + if (bd->writeCopies) {
1696 + /* Inside the loop, writeCopies means extra copies (beyond 1) */
1697 + --bd->writeCopies;
1698 + /* Loop outputting bytes */
1700 + /* If the output buffer is full, snapshot
1701 + * state and return */
1702 + if (gotcount >= len) {
1703 + bd->writePos = pos;
1704 + bd->writeCurrent = xcurrent;
1705 + bd->writeCopies++;
1708 + /* Write next byte into output buffer, updating CRC */
1709 + outbuf[gotcount++] = xcurrent;
1710 + bd->writeCRC = (((bd->writeCRC) << 8)
1711 + ^bd->crc32Table[((bd->writeCRC) >> 24)
1713 + /* Loop now if we're outputting multiple
1714 + * copies of this byte */
1715 + if (bd->writeCopies) {
1716 + --bd->writeCopies;
1720 + if (!bd->writeCount--)
1722 + /* Follow sequence vector to undo
1723 + * Burrows-Wheeler transform */
1724 + previous = xcurrent;
1726 + xcurrent = pos&0xff;
1728 + /* After 3 consecutive copies of the same
1729 + byte, the 4th is a repeat count. We count
1730 + down from 4 instead *of counting up because
1731 + testing for non-zero is faster */
1732 + if (--bd->writeRunCountdown) {
1733 + if (xcurrent != previous)
1734 + bd->writeRunCountdown = 4;
1736 + /* We have a repeated run, this byte
1737 + * indicates the count */
1738 + bd->writeCopies = xcurrent;
1739 + xcurrent = previous;
1740 + bd->writeRunCountdown = 5;
1741 + /* Sometimes there are just 3 bytes
1742 + * (run length 0) */
1743 + if (!bd->writeCopies)
1744 + goto decode_next_byte;
1745 + /* Subtract the 1 copy we'd output
1746 + * anyway to get extras */
1747 + --bd->writeCopies;
1750 + /* Decompression of this block completed successfully */
1751 + bd->writeCRC = ~bd->writeCRC;
1752 + bd->totalCRC = ((bd->totalCRC << 1) |
1753 + (bd->totalCRC >> 31)) ^ bd->writeCRC;
1754 + /* If this block had a CRC error, force file level CRC error. */
1755 + if (bd->writeCRC != bd->headerCRC) {
1756 + bd->totalCRC = bd->headerCRC+1;
1757 + return RETVAL_LAST_BLOCK;
1761 + /* Refill the intermediate buffer by Huffman-decoding next
1762 + * block of input */
1763 + /* (previous is just a convenient unused temp variable here) */
1764 + previous = get_next_block(bd);
1766 + bd->writeCount = previous;
1767 + return (previous != RETVAL_LAST_BLOCK) ? previous : gotcount;
1769 + bd->writeCRC = 0xffffffffUL;
1770 + pos = bd->writePos;
1771 + xcurrent = bd->writeCurrent;
1772 + goto decode_next_byte;
1775 +static int INIT nofill(void *buf, unsigned int len)
1780 +/* Allocate the structure, read file header. If in_fd ==-1, inbuf must contain
1781 + a complete bunzip file (len bytes long). If in_fd!=-1, inbuf and len are
1782 + ignored, and data is read from file handle into temporary buffer. */
1783 +static int INIT start_bunzip(struct bunzip_data **bdp, void *inbuf, int len,
1784 + int (*fill)(void*, unsigned int))
1786 + struct bunzip_data *bd;
1787 + unsigned int i, j, c;
1788 + const unsigned int BZh0 =
1789 + (((unsigned int)'B') << 24)+(((unsigned int)'Z') << 16)
1790 + +(((unsigned int)'h') << 8)+(unsigned int)'0';
1792 + /* Figure out how much data to allocate */
1793 + i = sizeof(struct bunzip_data);
1795 + /* Allocate bunzip_data. Most fields initialize to zero. */
1796 + bd = *bdp = malloc(i);
1797 + memset(bd, 0, sizeof(struct bunzip_data));
1798 + /* Setup input buffer */
1799 + bd->inbuf = inbuf;
1800 + bd->inbufCount = len;
1804 + bd->fill = nofill;
1806 + /* Init the CRC32 table (big endian) */
1807 + for (i = 0; i < 256; i++) {
1809 + for (j = 8; j; j--)
1810 + c = c&0x80000000 ? (c << 1)^0x04c11db7 : (c << 1);
1811 + bd->crc32Table[i] = c;
1814 + /* Ensure that file starts with "BZh['1'-'9']." */
1815 + i = get_bits(bd, 32);
1816 + if (((unsigned int)(i-BZh0-1)) >= 9)
1817 + return RETVAL_NOT_BZIP_DATA;
1819 + /* Fourth byte (ascii '1'-'9'), indicates block size in units of 100k of
1820 + uncompressed data. Allocate intermediate buffer for block. */
1821 + bd->dbufSize = 100000*(i-BZh0);
1823 + bd->dbuf = large_malloc(bd->dbufSize * sizeof(int));
1827 +/* Example usage: decompress src_fd to dst_fd. (Stops at end of bzip2 data,
1828 + not end of file.) */
1829 +STATIC int INIT bunzip2(unsigned char *buf, int len,
1830 + int(*fill)(void*, unsigned int),
1831 + int(*flush)(void*, unsigned int),
1832 + unsigned char *outbuf,
1834 + void(*error_fn)(char *x))
1836 + struct bunzip_data *bd;
1838 + unsigned char *inbuf;
1840 + set_error_fn(error_fn);
1842 + outbuf = malloc(BZIP2_IOBUF_SIZE);
1844 + len -= 4; /* Uncompressed size hack active in pre-boot
1847 + error("Could not allocate output bufer");
1853 + inbuf = malloc(BZIP2_IOBUF_SIZE);
1855 + error("Could not allocate input bufer");
1858 + i = start_bunzip(&bd, inbuf, len, fill);
1861 + i = read_bunzip(bd, outbuf, BZIP2_IOBUF_SIZE);
1867 + if (i != flush(outbuf, i)) {
1868 + i = RETVAL_UNEXPECTED_OUTPUT_EOF;
1873 + /* Check CRC and release memory */
1874 + if (i == RETVAL_LAST_BLOCK) {
1875 + if (bd->headerCRC != bd->totalCRC)
1876 + error("Data integrity error when decompressing.");
1879 + } else if (i == RETVAL_UNEXPECTED_OUTPUT_EOF) {
1880 + error("Compressed file ends unexpectedly");
1883 + large_free(bd->dbuf);
1885 + *pos = bd->inbufPos;
1895 +#define decompress bunzip2
1897 +++ b/lib/decompress_unlzma.c
1899 +/* Lzma decompressor for Linux kernel. Shamelessly snarfed
1900 + *from busybox 1.1.1
1902 + *Linux kernel adaptation
1903 + *Copyright (C) 2006 Alain < alain@knaff.lu >
1905 + *Based on small lzma deflate implementation/Small range coder
1906 + *implementation for lzma.
1907 + *Copyright (C) 2006 Aurelien Jacobs < aurel@gnuage.org >
1909 + *Based on LzmaDecode.c from the LZMA SDK 4.22 (http://www.7-zip.org/)
1910 + *Copyright (C) 1999-2005 Igor Pavlov
1912 + *Copyrights of the parts, see headers below.
1915 + *This program is free software; you can redistribute it and/or
1916 + *modify it under the terms of the GNU Lesser General Public
1917 + *License as published by the Free Software Foundation; either
1918 + *version 2.1 of the License, or (at your option) any later version.
1920 + *This program is distributed in the hope that it will be useful,
1921 + *but WITHOUT ANY WARRANTY; without even the implied warranty of
1922 + *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1923 + *Lesser General Public License for more details.
1925 + *You should have received a copy of the GNU Lesser General Public
1926 + *License along with this library; if not, write to the Free Software
1927 + *Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1931 +#include <linux/decompress/unlzma.h>
1932 +#endif /* STATIC */
1934 +#include <linux/decompress/mm.h>
1936 +#define MIN(a, b) (((a) < (b)) ? (a) : (b))
1938 +static long long INIT read_int(unsigned char *ptr, int size)
1941 + long long ret = 0;
1943 + for (i = 0; i < size; i++)
1944 + ret = (ret << 8) | ptr[size-i-1];
1948 +#define ENDIAN_CONVERT(x) \
1949 + x = (typeof(x))read_int((unsigned char *)&x, sizeof(x))
1952 +/* Small range coder implementation for lzma.
1953 + *Copyright (C) 2006 Aurelien Jacobs < aurel@gnuage.org >
1955 + *Based on LzmaDecode.c from the LZMA SDK 4.22 (http://www.7-zip.org/)
1956 + *Copyright (c) 1999-2005 Igor Pavlov
1959 +#include <linux/compiler.h>
1961 +#define LZMA_IOBUF_SIZE 0x10000
1964 + int (*fill)(void*, unsigned int);
1967 + uint8_t *buffer_end;
1975 +#define RC_TOP_BITS 24
1976 +#define RC_MOVE_BITS 5
1977 +#define RC_MODEL_TOTAL_BITS 11
1980 +/* Called twice: once at startup and once in rc_normalize() */
1981 +static void INIT rc_read(struct rc *rc)
1983 + rc->buffer_size = rc->fill((char *)rc->buffer, LZMA_IOBUF_SIZE);
1984 + if (rc->buffer_size <= 0)
1985 + error("unexpected EOF");
1986 + rc->ptr = rc->buffer;
1987 + rc->buffer_end = rc->buffer + rc->buffer_size;
1991 +static inline void INIT rc_init(struct rc *rc,
1992 + int (*fill)(void*, unsigned int),
1993 + char *buffer, int buffer_size)
1996 + rc->buffer = (uint8_t *)buffer;
1997 + rc->buffer_size = buffer_size;
1998 + rc->buffer_end = rc->buffer + rc->buffer_size;
1999 + rc->ptr = rc->buffer;
2002 + rc->range = 0xFFFFFFFF;
2005 +static inline void INIT rc_init_code(struct rc *rc)
2009 + for (i = 0; i < 5; i++) {
2010 + if (rc->ptr >= rc->buffer_end)
2012 + rc->code = (rc->code << 8) | *rc->ptr++;
2017 +/* Called once. TODO: bb_maybe_free() */
2018 +static inline void INIT rc_free(struct rc *rc)
2023 +/* Called twice, but one callsite is in inline'd rc_is_bit_0_helper() */
2024 +static void INIT rc_do_normalize(struct rc *rc)
2026 + if (rc->ptr >= rc->buffer_end)
2029 + rc->code = (rc->code << 8) | *rc->ptr++;
2031 +static inline void INIT rc_normalize(struct rc *rc)
2033 + if (rc->range < (1 << RC_TOP_BITS))
2034 + rc_do_normalize(rc);
2037 +/* Called 9 times */
2038 +/* Why rc_is_bit_0_helper exists?
2039 + *Because we want to always expose (rc->code < rc->bound) to optimizer
2041 +static inline uint32_t INIT rc_is_bit_0_helper(struct rc *rc, uint16_t *p)
2044 + rc->bound = *p * (rc->range >> RC_MODEL_TOTAL_BITS);
2047 +static inline int INIT rc_is_bit_0(struct rc *rc, uint16_t *p)
2049 + uint32_t t = rc_is_bit_0_helper(rc, p);
2050 + return rc->code < t;
2053 +/* Called ~10 times, but very small, thus inlined */
2054 +static inline void INIT rc_update_bit_0(struct rc *rc, uint16_t *p)
2056 + rc->range = rc->bound;
2057 + *p += ((1 << RC_MODEL_TOTAL_BITS) - *p) >> RC_MOVE_BITS;
2059 +static inline void rc_update_bit_1(struct rc *rc, uint16_t *p)
2061 + rc->range -= rc->bound;
2062 + rc->code -= rc->bound;
2063 + *p -= *p >> RC_MOVE_BITS;
2066 +/* Called 4 times in unlzma loop */
2067 +static int INIT rc_get_bit(struct rc *rc, uint16_t *p, int *symbol)
2069 + if (rc_is_bit_0(rc, p)) {
2070 + rc_update_bit_0(rc, p);
2074 + rc_update_bit_1(rc, p);
2075 + *symbol = *symbol * 2 + 1;
2081 +static inline int INIT rc_direct_bit(struct rc *rc)
2085 + if (rc->code >= rc->range) {
2086 + rc->code -= rc->range;
2093 +static inline void INIT
2094 +rc_bit_tree_decode(struct rc *rc, uint16_t *p, int num_levels, int *symbol)
2096 + int i = num_levels;
2100 + rc_get_bit(rc, p + *symbol, symbol);
2101 + *symbol -= 1 << num_levels;
2106 + * Small lzma deflate implementation.
2107 + * Copyright (C) 2006 Aurelien Jacobs < aurel@gnuage.org >
2109 + * Based on LzmaDecode.c from the LZMA SDK 4.22 (http://www.7-zip.org/)
2110 + * Copyright (C) 1999-2005 Igor Pavlov
2114 +struct lzma_header {
2116 + uint32_t dict_size;
2117 + uint64_t dst_size;
2118 +} __attribute__ ((packed)) ;
2121 +#define LZMA_BASE_SIZE 1846
2122 +#define LZMA_LIT_SIZE 768
2124 +#define LZMA_NUM_POS_BITS_MAX 4
2126 +#define LZMA_LEN_NUM_LOW_BITS 3
2127 +#define LZMA_LEN_NUM_MID_BITS 3
2128 +#define LZMA_LEN_NUM_HIGH_BITS 8
2130 +#define LZMA_LEN_CHOICE 0
2131 +#define LZMA_LEN_CHOICE_2 (LZMA_LEN_CHOICE + 1)
2132 +#define LZMA_LEN_LOW (LZMA_LEN_CHOICE_2 + 1)
2133 +#define LZMA_LEN_MID (LZMA_LEN_LOW \
2134 + + (1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_LOW_BITS)))
2135 +#define LZMA_LEN_HIGH (LZMA_LEN_MID \
2136 + +(1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_MID_BITS)))
2137 +#define LZMA_NUM_LEN_PROBS (LZMA_LEN_HIGH + (1 << LZMA_LEN_NUM_HIGH_BITS))
2139 +#define LZMA_NUM_STATES 12
2140 +#define LZMA_NUM_LIT_STATES 7
2142 +#define LZMA_START_POS_MODEL_INDEX 4
2143 +#define LZMA_END_POS_MODEL_INDEX 14
2144 +#define LZMA_NUM_FULL_DISTANCES (1 << (LZMA_END_POS_MODEL_INDEX >> 1))
2146 +#define LZMA_NUM_POS_SLOT_BITS 6
2147 +#define LZMA_NUM_LEN_TO_POS_STATES 4
2149 +#define LZMA_NUM_ALIGN_BITS 4
2151 +#define LZMA_MATCH_MIN_LEN 2
2153 +#define LZMA_IS_MATCH 0
2154 +#define LZMA_IS_REP (LZMA_IS_MATCH + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX))
2155 +#define LZMA_IS_REP_G0 (LZMA_IS_REP + LZMA_NUM_STATES)
2156 +#define LZMA_IS_REP_G1 (LZMA_IS_REP_G0 + LZMA_NUM_STATES)
2157 +#define LZMA_IS_REP_G2 (LZMA_IS_REP_G1 + LZMA_NUM_STATES)
2158 +#define LZMA_IS_REP_0_LONG (LZMA_IS_REP_G2 + LZMA_NUM_STATES)
2159 +#define LZMA_POS_SLOT (LZMA_IS_REP_0_LONG \
2160 + + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX))
2161 +#define LZMA_SPEC_POS (LZMA_POS_SLOT \
2162 + +(LZMA_NUM_LEN_TO_POS_STATES << LZMA_NUM_POS_SLOT_BITS))
2163 +#define LZMA_ALIGN (LZMA_SPEC_POS \
2164 + + LZMA_NUM_FULL_DISTANCES - LZMA_END_POS_MODEL_INDEX)
2165 +#define LZMA_LEN_CODER (LZMA_ALIGN + (1 << LZMA_NUM_ALIGN_BITS))
2166 +#define LZMA_REP_LEN_CODER (LZMA_LEN_CODER + LZMA_NUM_LEN_PROBS)
2167 +#define LZMA_LITERAL (LZMA_REP_LEN_CODER + LZMA_NUM_LEN_PROBS)
2172 + uint8_t previous_byte;
2173 + size_t buffer_pos;
2175 + size_t global_pos;
2176 + int(*flush)(void*, unsigned int);
2177 + struct lzma_header *header;
2182 + uint32_t rep0, rep1, rep2, rep3;
2185 +static inline size_t INIT get_pos(struct writer *wr)
2188 + wr->global_pos + wr->buffer_pos;
2191 +static inline uint8_t INIT peek_old_byte(struct writer *wr,
2196 + while (offs > wr->header->dict_size)
2197 + offs -= wr->header->dict_size;
2198 + pos = wr->buffer_pos - offs;
2199 + return wr->buffer[pos];
2201 + uint32_t pos = wr->buffer_pos - offs;
2202 + while (pos >= wr->header->dict_size)
2203 + pos += wr->header->dict_size;
2204 + return wr->buffer[pos];
2209 +static inline void INIT write_byte(struct writer *wr, uint8_t byte)
2211 + wr->buffer[wr->buffer_pos++] = wr->previous_byte = byte;
2212 + if (wr->flush && wr->buffer_pos == wr->header->dict_size) {
2213 + wr->buffer_pos = 0;
2214 + wr->global_pos += wr->header->dict_size;
2215 + wr->flush((char *)wr->buffer, wr->header->dict_size);
2220 +static inline void INIT copy_byte(struct writer *wr, uint32_t offs)
2222 + write_byte(wr, peek_old_byte(wr, offs));
2225 +static inline void INIT copy_bytes(struct writer *wr,
2226 + uint32_t rep0, int len)
2229 + copy_byte(wr, rep0);
2231 + } while (len != 0 && wr->buffer_pos < wr->header->dst_size);
2234 +static inline void INIT process_bit0(struct writer *wr, struct rc *rc,
2235 + struct cstate *cst, uint16_t *p,
2236 + int pos_state, uint16_t *prob,
2237 + int lc, uint32_t literal_pos_mask) {
2239 + rc_update_bit_0(rc, prob);
2240 + prob = (p + LZMA_LITERAL +
2242 + * (((get_pos(wr) & literal_pos_mask) << lc)
2243 + + (wr->previous_byte >> (8 - lc))))
2246 + if (cst->state >= LZMA_NUM_LIT_STATES) {
2247 + int match_byte = peek_old_byte(wr, cst->rep0);
2250 + uint16_t *prob_lit;
2253 + bit = match_byte & 0x100;
2254 + prob_lit = prob + 0x100 + bit + mi;
2255 + if (rc_get_bit(rc, prob_lit, &mi)) {
2262 + } while (mi < 0x100);
2264 + while (mi < 0x100) {
2265 + uint16_t *prob_lit = prob + mi;
2266 + rc_get_bit(rc, prob_lit, &mi);
2268 + write_byte(wr, mi);
2269 + if (cst->state < 4)
2271 + else if (cst->state < 10)
2277 +static inline void INIT process_bit1(struct writer *wr, struct rc *rc,
2278 + struct cstate *cst, uint16_t *p,
2279 + int pos_state, uint16_t *prob) {
2281 + uint16_t *prob_len;
2285 + rc_update_bit_1(rc, prob);
2286 + prob = p + LZMA_IS_REP + cst->state;
2287 + if (rc_is_bit_0(rc, prob)) {
2288 + rc_update_bit_0(rc, prob);
2289 + cst->rep3 = cst->rep2;
2290 + cst->rep2 = cst->rep1;
2291 + cst->rep1 = cst->rep0;
2292 + cst->state = cst->state < LZMA_NUM_LIT_STATES ? 0 : 3;
2293 + prob = p + LZMA_LEN_CODER;
2295 + rc_update_bit_1(rc, prob);
2296 + prob = p + LZMA_IS_REP_G0 + cst->state;
2297 + if (rc_is_bit_0(rc, prob)) {
2298 + rc_update_bit_0(rc, prob);
2299 + prob = (p + LZMA_IS_REP_0_LONG
2301 + LZMA_NUM_POS_BITS_MAX) +
2303 + if (rc_is_bit_0(rc, prob)) {
2304 + rc_update_bit_0(rc, prob);
2306 + cst->state = cst->state < LZMA_NUM_LIT_STATES ?
2308 + copy_byte(wr, cst->rep0);
2311 + rc_update_bit_1(rc, prob);
2314 + uint32_t distance;
2316 + rc_update_bit_1(rc, prob);
2317 + prob = p + LZMA_IS_REP_G1 + cst->state;
2318 + if (rc_is_bit_0(rc, prob)) {
2319 + rc_update_bit_0(rc, prob);
2320 + distance = cst->rep1;
2322 + rc_update_bit_1(rc, prob);
2323 + prob = p + LZMA_IS_REP_G2 + cst->state;
2324 + if (rc_is_bit_0(rc, prob)) {
2325 + rc_update_bit_0(rc, prob);
2326 + distance = cst->rep2;
2328 + rc_update_bit_1(rc, prob);
2329 + distance = cst->rep3;
2330 + cst->rep3 = cst->rep2;
2332 + cst->rep2 = cst->rep1;
2334 + cst->rep1 = cst->rep0;
2335 + cst->rep0 = distance;
2337 + cst->state = cst->state < LZMA_NUM_LIT_STATES ? 8 : 11;
2338 + prob = p + LZMA_REP_LEN_CODER;
2341 + prob_len = prob + LZMA_LEN_CHOICE;
2342 + if (rc_is_bit_0(rc, prob_len)) {
2343 + rc_update_bit_0(rc, prob_len);
2344 + prob_len = (prob + LZMA_LEN_LOW
2346 + LZMA_LEN_NUM_LOW_BITS));
2348 + num_bits = LZMA_LEN_NUM_LOW_BITS;
2350 + rc_update_bit_1(rc, prob_len);
2351 + prob_len = prob + LZMA_LEN_CHOICE_2;
2352 + if (rc_is_bit_0(rc, prob_len)) {
2353 + rc_update_bit_0(rc, prob_len);
2354 + prob_len = (prob + LZMA_LEN_MID
2356 + LZMA_LEN_NUM_MID_BITS));
2357 + offset = 1 << LZMA_LEN_NUM_LOW_BITS;
2358 + num_bits = LZMA_LEN_NUM_MID_BITS;
2360 + rc_update_bit_1(rc, prob_len);
2361 + prob_len = prob + LZMA_LEN_HIGH;
2362 + offset = ((1 << LZMA_LEN_NUM_LOW_BITS)
2363 + + (1 << LZMA_LEN_NUM_MID_BITS));
2364 + num_bits = LZMA_LEN_NUM_HIGH_BITS;
2368 + rc_bit_tree_decode(rc, prob_len, num_bits, &len);
2371 + if (cst->state < 4) {
2374 + cst->state += LZMA_NUM_LIT_STATES;
2376 + p + LZMA_POS_SLOT +
2378 + LZMA_NUM_LEN_TO_POS_STATES ? len :
2379 + LZMA_NUM_LEN_TO_POS_STATES - 1)
2380 + << LZMA_NUM_POS_SLOT_BITS);
2381 + rc_bit_tree_decode(rc, prob,
2382 + LZMA_NUM_POS_SLOT_BITS,
2384 + if (pos_slot >= LZMA_START_POS_MODEL_INDEX) {
2386 + num_bits = (pos_slot >> 1) - 1;
2387 + cst->rep0 = 2 | (pos_slot & 1);
2388 + if (pos_slot < LZMA_END_POS_MODEL_INDEX) {
2389 + cst->rep0 <<= num_bits;
2390 + prob = p + LZMA_SPEC_POS +
2391 + cst->rep0 - pos_slot - 1;
2393 + num_bits -= LZMA_NUM_ALIGN_BITS;
2394 + while (num_bits--)
2395 + cst->rep0 = (cst->rep0 << 1) |
2396 + rc_direct_bit(rc);
2397 + prob = p + LZMA_ALIGN;
2398 + cst->rep0 <<= LZMA_NUM_ALIGN_BITS;
2399 + num_bits = LZMA_NUM_ALIGN_BITS;
2403 + while (num_bits--) {
2404 + if (rc_get_bit(rc, prob + mi, &mi))
2409 + cst->rep0 = pos_slot;
2410 + if (++(cst->rep0) == 0)
2414 + len += LZMA_MATCH_MIN_LEN;
2416 + copy_bytes(wr, cst->rep0, len);
2421 +STATIC inline int INIT unlzma(unsigned char *buf, int in_len,
2422 + int(*fill)(void*, unsigned int),
2423 + int(*flush)(void*, unsigned int),
2424 + unsigned char *output,
2426 + void(*error_fn)(char *x)
2429 + struct lzma_header header;
2431 + uint32_t pos_state_mask;
2432 + uint32_t literal_pos_mask;
2438 + struct cstate cst;
2439 + unsigned char *inbuf;
2442 + set_error_fn(error_fn);
2444 + in_len -= 4; /* Uncompressed size hack active in pre-boot
2449 + inbuf = malloc(LZMA_IOBUF_SIZE);
2451 + error("Could not allocate input bufer");
2456 + cst.rep0 = cst.rep1 = cst.rep2 = cst.rep3 = 1;
2458 + wr.header = &header;
2460 + wr.global_pos = 0;
2461 + wr.previous_byte = 0;
2462 + wr.buffer_pos = 0;
2464 + rc_init(&rc, fill, inbuf, in_len);
2466 + for (i = 0; i < sizeof(header); i++) {
2467 + if (rc.ptr >= rc.buffer_end)
2469 + ((unsigned char *)&header)[i] = *rc.ptr++;
2472 + if (header.pos >= (9 * 5 * 5))
2473 + error("bad header");
2487 + pos_state_mask = (1 << pb) - 1;
2488 + literal_pos_mask = (1 << lp) - 1;
2490 + ENDIAN_CONVERT(header.dict_size);
2491 + ENDIAN_CONVERT(header.dst_size);
2493 + if (header.dict_size == 0)
2494 + header.dict_size = 1;
2497 + wr.buffer = output;
2499 + wr.bufsize = MIN(header.dst_size, header.dict_size);
2500 + wr.buffer = large_malloc(wr.bufsize);
2502 + if (wr.buffer == NULL)
2505 + num_probs = LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp));
2506 + p = (uint16_t *) large_malloc(num_probs * sizeof(*p));
2509 + num_probs = LZMA_LITERAL + (LZMA_LIT_SIZE << (lc + lp));
2510 + for (i = 0; i < num_probs; i++)
2511 + p[i] = (1 << RC_MODEL_TOTAL_BITS) >> 1;
2513 + rc_init_code(&rc);
2515 + while (get_pos(&wr) < header.dst_size) {
2516 + int pos_state = get_pos(&wr) & pos_state_mask;
2517 + uint16_t *prob = p + LZMA_IS_MATCH +
2518 + (cst.state << LZMA_NUM_POS_BITS_MAX) + pos_state;
2519 + if (rc_is_bit_0(&rc, prob))
2520 + process_bit0(&wr, &rc, &cst, p, pos_state, prob,
2521 + lc, literal_pos_mask);
2523 + process_bit1(&wr, &rc, &cst, p, pos_state, prob);
2524 + if (cst.rep0 == 0)
2530 + *posp = rc.ptr-rc.buffer;
2532 + wr.flush(wr.buffer, wr.buffer_pos);
2537 + large_free(wr.buffer);
2545 +#define decompress unlzma
2548 @@ -109,20 +109,78 @@ static char rcsid[] = "#Id: inflate.c,v
2552 +#include <linux/decompress/inflate.h>
2553 +#endif /* ! STATIC */
2555 -#if defined(STDC_HEADERS) || defined(HAVE_STDLIB_H)
2556 -# include <sys/types.h>
2557 -# include <stdlib.h>
2558 +#include <linux/decompress/mm.h>
2560 +#include <linux/string.h>
2563 +static int(*flush_cb)(void*, unsigned int);
2564 +static int(*fill_cb)(void*, unsigned int);
2566 +/* Begin stuff copied from initramfs */
2568 + * gzip declarations
2571 +#define OF(args) args
2574 +#define memzero(s, n) memset((s), 0, (n))
2579 -#endif /* !STATIC */
2580 +#define INBUFSIZ 4096
2582 +#define WSIZE 0x8000 /* window size--must be a power of two, and */
2583 + /* at least 32K for zip's deflate method */
2585 +static uint8_t *inbuf;
2586 +static uint8_t *window;
2588 +static unsigned insize; /* valid bytes in inbuf */
2589 +static unsigned outcnt; /* bytes in output buffer */
2590 +static long bytes_out;
2594 +static unsigned inptr; /* index of next byte to be processed in inbuf */
2598 +/* ===========================================================================
2599 + * Fill the input buffer. This is called only when the buffer is empty
2600 + * and at least one byte is really needed.
2601 + * Returning -1 does not guarantee that gunzip() will ever return.
2603 +static int INIT fill_inbuf(void)
2605 + insize = fill_cb(inbuf, INBUFSIZ);
2606 + if (insize <= 0) {
2607 + error("RAMDISK: ran out of compressed data");
2616 +#define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
2618 +/* Diagnostic functions (stubbed out) */
2619 +#define Assert(cond, msg)
2623 +#define Tracec(c, x)
2624 +#define Tracecv(c, x)
2628 +static void flush_window(void);
2629 +/* End stuff copied from initramfs */
2633 #define slide window
2635 /* Huffman code lookup table entry--this entry is four bytes for machines
2636 @@ -133,10 +191,10 @@ static char rcsid[] = "#Id: inflate.c,v
2637 an unused code. If a code with e == 99 is looked up, this implies an
2638 error in the data. */
2640 - uch e; /* number of extra bits or operation */
2641 - uch b; /* number of bits in this code or subcode */
2642 + uint8_t e; /* number of extra bits or operation */
2643 + uint8_t b; /* number of bits in this code or subcode */
2645 - ush n; /* literal, length base, or distance base */
2646 + uint16_t n; /* literal, length base, or distance base */
2647 struct huft *t; /* pointer to next level of table */
2650 @@ -144,7 +202,7 @@ struct huft {
2652 /* Function prototypes */
2653 STATIC int INIT huft_build OF((unsigned *, unsigned, unsigned,
2654 - const ush *, const ush *, struct huft **, int *));
2655 + const uint16_t *, const uint16_t *, struct huft **, int *));
2656 STATIC int INIT huft_free OF((struct huft *));
2657 STATIC int INIT inflate_codes OF((struct huft *, struct huft *, int, int));
2658 STATIC int INIT inflate_stored OF((void));
2659 @@ -159,28 +217,28 @@ STATIC int INIT inflate OF((void));
2660 circular buffer. The index is updated simply by incrementing and then
2661 ANDing with 0x7fff (32K-1). */
2662 /* It is left to other modules to supply the 32 K area. It is assumed
2663 - to be usable as if it were declared "uch slide[32768];" or as just
2664 - "uch *slide;" and then malloc'ed in the latter case. The definition
2665 + to be usable as if it were declared "uint8_t slide[32768];" or as just
2666 + "uint8_t *slide;" and then malloc'ed in the latter case. The definition
2667 must be in unzip.h, included above. */
2668 /* unsigned wp; current position in slide */
2670 #define flush_output(w) (wp=(w),flush_window())
2672 /* Tables for deflate from PKZIP's appnote.txt. */
2673 -static const unsigned border[] = { /* Order of the bit length code lengths */
2674 +static const unsigned border[] = { /* Order of the bit length code lengths */
2675 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
2676 -static const ush cplens[] = { /* Copy lengths for literal codes 257..285 */
2677 +static const uint16_t cplens[] = { /* Copy lengths for literal codes 257..285 */
2678 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
2679 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
2680 /* note: see note #13 above about the 258 in this list. */
2681 -static const ush cplext[] = { /* Extra bits for literal codes 257..285 */
2682 +static const uint16_t cplext[] = { /* Extra bits for literal codes 257..285 */
2683 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
2684 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99}; /* 99==invalid */
2685 -static const ush cpdist[] = { /* Copy offsets for distance codes 0..29 */
2686 +static const uint16_t cpdist[] = { /* Copy offsets for distance codes 0..29 */
2687 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
2688 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
2689 8193, 12289, 16385, 24577};
2690 -static const ush cpdext[] = { /* Extra bits for distance codes */
2691 +static const uint16_t cpdext[] = { /* Extra bits for distance codes */
2692 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
2693 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
2695 @@ -217,59 +275,21 @@ static const ush cpdext[] = { /*
2699 -STATIC ulg bb; /* bit buffer */
2700 +STATIC uint32_t bb; /* bit buffer */
2701 STATIC unsigned bk; /* bits in bit buffer */
2703 -STATIC const ush mask_bits[] = {
2704 +STATIC const uint16_t mask_bits[] = {
2706 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
2707 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
2710 -#define NEXTBYTE() ({ int v = get_byte(); if (v < 0) goto underrun; (uch)v; })
2711 -#define NEEDBITS(n) {while(k<(n)){b|=((ulg)NEXTBYTE())<<k;k+=8;}}
2712 +#define NEXTBYTE() ({ int v = get_byte(); if (v < 0) goto underrun; \
2714 +#define NEEDBITS(n) {while (k < (n)) \
2715 + {b |= ((uint32_t)NEXTBYTE())<<k; k += 8; } }
2716 #define DUMPBITS(n) {b>>=(n);k-=(n);}
2718 -#ifndef NO_INFLATE_MALLOC
2719 -/* A trivial malloc implementation, adapted from
2720 - * malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994
2723 -static unsigned long malloc_ptr;
2724 -static int malloc_count;
2726 -static void *malloc(int size)
2731 - error("Malloc error");
2733 - malloc_ptr = free_mem_ptr;
2735 - malloc_ptr = (malloc_ptr + 3) & ~3; /* Align */
2737 - p = (void *)malloc_ptr;
2738 - malloc_ptr += size;
2740 - if (free_mem_end_ptr && malloc_ptr >= free_mem_end_ptr)
2741 - error("Out of memory");
2747 -static void free(void *where)
2750 - if (!malloc_count)
2751 - malloc_ptr = free_mem_ptr;
2754 -#define malloc(a) kmalloc(a, GFP_KERNEL)
2755 -#define free(a) kfree(a)
2759 Huffman code decoding is performed using a multi-level table lookup.
2760 The fastest way to decode is to simply build a lookup table whose
2761 @@ -307,7 +327,7 @@ STATIC const int lbits = 9; /*
2762 STATIC const int dbits = 6; /* bits in base distance lookup table */
2765 -/* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
2766 +/* If BMAX needs to be larger than 16, then h and x[] should be uint32_t. */
2767 #define BMAX 16 /* maximum bit length of any code (16 for explode) */
2768 #define N_MAX 288 /* maximum number of codes in any set */
2770 @@ -319,8 +339,8 @@ STATIC int INIT huft_build(
2771 unsigned *b, /* code lengths in bits (all assumed <= BMAX) */
2772 unsigned n, /* number of codes (assumed <= N_MAX) */
2773 unsigned s, /* number of simple-valued codes (0..s-1) */
2774 - const ush *d, /* list of base values for non-simple codes */
2775 - const ush *e, /* list of extra bits for non-simple codes */
2776 + const uint16_t *d, /* list of base values for non-simple codes */
2777 + const uint16_t *e, /* list of extra bits for non-simple codes */
2778 struct huft **t, /* result: starting table */
2779 int *m /* maximum lookup bits, returns actual */
2781 @@ -500,8 +520,8 @@ DEBG1("5 ");
2784 x[h] = i; /* save pattern for backing up */
2785 - r.b = (uch)l; /* bits to dump before this table */
2786 - r.e = (uch)(16 + j); /* bits in this table */
2787 + r.b = (uint8_t)l; /* bits to dump before this table */
2788 + r.e = (uint8_t)(16 + j); /* bits in this table */
2789 r.v.t = q; /* pointer to this table */
2790 j = i >> (w - l); /* (get around Turbo C bug) */
2791 u[h-1][j] = r; /* connect to last table */
2792 @@ -511,18 +531,18 @@ DEBG1("6 ");
2795 /* set up table entry in r */
2796 - r.b = (uch)(k - w);
2797 + r.b = (uint8_t)(k - w);
2799 r.e = 99; /* out of values--invalid code */
2802 - r.e = (uch)(*p < 256 ? 16 : 15); /* 256 is end-of-block code */
2803 - r.v.n = (ush)(*p); /* simple code is just the value */
2804 + r.e = (uint8_t)(*p < 256 ? 16 : 15); /* 256 is end-of-block code */
2805 + r.v.n = (uint16_t)(*p); /* simple code is just the value */
2806 p++; /* one compiler does not like *p++ */
2810 - r.e = (uch)e[*p - s]; /* non-simple--look up in lists */
2811 + r.e = (uint8_t)e[*p - s]; /* non-simple--look up in lists */
2812 r.v.n = d[*p++ - s];
2815 @@ -592,11 +612,12 @@ STATIC int INIT inflate_codes(
2816 Return an error code or zero if it all goes ok. */
2818 register unsigned e; /* table entry flag/number of extra bits */
2819 - unsigned n, d; /* length and index for copy */
2821 + int d; /* source index for copy */
2822 unsigned w; /* current window position */
2823 struct huft *t; /* pointer to table entry */
2824 unsigned ml, md; /* masks for bl and bd bits */
2825 - register ulg b; /* bit buffer */
2826 + register uint32_t b; /* bit buffer */
2827 register unsigned k; /* number of bits in bit buffer */
2830 @@ -622,7 +643,7 @@ STATIC int INIT inflate_codes(
2832 if (e == 16) /* then it's a literal */
2834 - slide[w++] = (uch)t->v.n;
2835 + slide[w++] = (uint8_t)t->v.n;
2836 Tracevv((stderr, "%c", slide[w-1]));
2839 @@ -659,11 +680,25 @@ STATIC int INIT inflate_codes(
2843 - n -= (e = (e = WSIZE - ((d &= WSIZE-1) > w ? d : w)) > n ? n : e);
2847 + /* Sliding window emulated using circular buffer:
2848 + * manage wrap-around */
2849 + e = WSIZE - ((d &= WSIZE-1) > w ? d : w);
2859 #if !defined(NOMEMCPY) && !defined(DEBUG)
2860 if (w - d >= e) /* (this test assumes unsigned comparison) */
2862 - memcpy(slide + w, slide + d, e);
2863 + memcpy(slide + w, slide + d, e);
2867 @@ -673,9 +708,8 @@ STATIC int INIT inflate_codes(
2868 slide[w++] = slide[d++];
2869 Tracevv((stderr, "%c", slide[w-1]));
2879 @@ -702,7 +736,7 @@ STATIC int INIT inflate_stored(void)
2881 unsigned n; /* number of bytes in block */
2882 unsigned w; /* current window position */
2883 - register ulg b; /* bit buffer */
2884 + register uint32_t b; /* bit buffer */
2885 register unsigned k; /* number of bits in bit buffer */
2888 @@ -732,7 +766,7 @@ DEBG("<stor");
2892 - slide[w++] = (uch)b;
2893 + slide[w++] = (uint8_t)b;
2897 @@ -838,7 +872,7 @@ STATIC int noinline INIT inflate_dynamic
2898 unsigned nl; /* number of literal/length codes */
2899 unsigned nd; /* number of distance codes */
2900 unsigned *ll; /* literal/length and distance code lengths */
2901 - register ulg b; /* bit buffer */
2902 + register uint32_t b; /* bit buffer */
2903 register unsigned k; /* number of bits in bit buffer */
2906 @@ -1033,7 +1067,7 @@ STATIC int INIT inflate_block(
2907 /* decompress an inflated block */
2909 unsigned t; /* block type */
2910 - register ulg b; /* bit buffer */
2911 + register uint32_t b; /* bit buffer */
2912 register unsigned k; /* number of bits in bit buffer */
2915 @@ -1130,8 +1164,8 @@ STATIC int INIT inflate(void)
2917 **********************************************************************/
2919 -static ulg crc_32_tab[256];
2920 -static ulg crc; /* initialized in makecrc() so it'll reside in bss */
2921 +static uint32_t crc_32_tab[256];
2922 +static uint32_t crc; /* initialized in makecrc() so it'll reside in bss */
2923 #define CRC_VALUE (crc ^ 0xffffffffUL)
2926 @@ -1172,7 +1206,7 @@ makecrc(void)
2929 /* this is initialized here so this code could reside in ROM */
2930 - crc = (ulg)0xffffffffUL; /* shift register contents */
2931 + crc = (uint32_t)0xffffffffUL; /* shift register contents */
2934 /* gzip flag byte */
2935 @@ -1184,18 +1218,89 @@ makecrc(void)
2936 #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
2937 #define RESERVED 0xC0 /* bit 6,7: reserved */
2940 +/* ===========================================================================
2941 + * Write the output window window[0..outcnt-1] and update crc and bytes_out.
2942 + * (Used for the decompressed data only.)
2944 +static void INIT flush_window(void)
2946 + uint32_t c = crc; /* temporary variable */
2951 + for (n = 0; n < outcnt; n++) {
2953 + c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
2956 + bytes_out += (uint32_t)outcnt;
2957 + if (flush_cb != NULL)
2958 + flush_cb(window, outcnt); /* TODO: handle unzip_error */
2964 +static int empty_fill(void *buf, unsigned int len)
2972 * Do the uncompression!
2974 -static int INIT gunzip(void)
2975 +STATIC int INIT gunzip(
2977 + unsigned char *buf, int len,
2978 + int(*fill)(void*, unsigned int),
2979 + int(*flush)(void*, unsigned int),
2980 + unsigned char *output,
2982 + void(*error_fn)(char *x)
2990 unsigned char magic[2]; /* magic header */
2992 - ulg orig_crc = 0; /* original crc */
2993 - ulg orig_len = 0; /* original uncompressed length */
2994 + uint32_t orig_crc = 0; /* original crc */
2995 + uint32_t orig_len = 0; /* original uncompressed length */
2999 + set_error_fn(error_fn);
3001 + fill_cb = empty_fill;
3007 + window = malloc(0x8000);
3009 + panic("can't allocate buffers");
3017 + inbuf = malloc(INBUFSIZ);
3021 + outcnt = 0; /* bytes in output buffer */
3023 + crc = (uint32_t)0xffffffffL; /* shift register contents */
3026 magic[0] = NEXTBYTE();
3027 magic[1] = NEXTBYTE();
3028 method = NEXTBYTE();
3029 @@ -1212,7 +1317,7 @@ static int INIT gunzip(void)
3033 - flags = (uch)get_byte();
3034 + flags = (uint8_t)get_byte();
3035 if ((flags & ENCRYPTED) != 0) {
3036 error("Input is encrypted");
3038 @@ -1277,15 +1382,15 @@ static int INIT gunzip(void)
3039 /* crc32 (see algorithm.doc)
3040 * uncompressed input size modulo 2^32
3042 - orig_crc = (ulg) NEXTBYTE();
3043 - orig_crc |= (ulg) NEXTBYTE() << 8;
3044 - orig_crc |= (ulg) NEXTBYTE() << 16;
3045 - orig_crc |= (ulg) NEXTBYTE() << 24;
3046 + orig_crc = (uint32_t) NEXTBYTE();
3047 + orig_crc |= (uint32_t) NEXTBYTE() << 8;
3048 + orig_crc |= (uint32_t) NEXTBYTE() << 16;
3049 + orig_crc |= (uint32_t) NEXTBYTE() << 24;
3051 - orig_len = (ulg) NEXTBYTE();
3052 - orig_len |= (ulg) NEXTBYTE() << 8;
3053 - orig_len |= (ulg) NEXTBYTE() << 16;
3054 - orig_len |= (ulg) NEXTBYTE() << 24;
3055 + orig_len = (uint32_t) NEXTBYTE();
3056 + orig_len |= (uint32_t) NEXTBYTE() << 8;
3057 + orig_len |= (uint32_t) NEXTBYTE() << 16;
3058 + orig_len |= (uint32_t) NEXTBYTE() << 24;
3060 /* Validate decompression */
3061 if (orig_crc != CRC_VALUE) {
3062 @@ -1296,11 +1401,22 @@ static int INIT gunzip(void)
3063 error("length error");
3074 underrun: /* NEXTBYTE() goto's here if needed */
3080 error("out of input data");
3085 +#define decompress gunzip
3086 --- a/scripts/Makefile.lib
3087 +++ b/scripts/Makefile.lib
3088 @@ -183,3 +183,17 @@ quiet_cmd_gzip = GZIP $@
3089 cmd_gzip = gzip -f -9 < $< > $@
3093 +# ---------------------------------------------------------------------------
3095 +# Bzip2 does not include size in file... so we have to fake that
3096 +size_append=$(CONFIG_SHELL) $(srctree)/scripts/bin_size
3098 +quiet_cmd_bzip2 = BZIP2 $@
3099 +cmd_bzip2 = (bzip2 -9 < $< ; $(size_append) $<) > $@ || (rm -f $@ ; false)
3102 +# ---------------------------------------------------------------------------
3104 +quiet_cmd_lzma = LZMA $@
3105 +cmd_lzma = (/usr/bin/lzma -9 -c $< ; $(size_append) $<) >$@ || (rm -f $@ ; false)
3107 +++ b/scripts/bin_size
3111 +if [ $# = 0 ] ; then
3112 + echo Usage: $0 file
3115 +size_dec=`stat -c "%s" $1`
3116 +size_hex_echo_string=`printf "%08x" $size_dec |
3117 + sed 's/\(..\)\(..\)\(..\)\(..\)/\\\\x\4\\\\x\3\\\\x\2\\\\x\1/g'`
3118 +/bin/echo -ne $size_hex_echo_string