ar71xx: remove duplicated include
[openwrt.git] / target / linux / rdc / patches-2.6.28 / 006-bzip2_lzma_x86.patch
1 --- a/arch/x86/boot/compressed/Makefile
2 +++ b/arch/x86/boot/compressed/Makefile
3 @@ -4,7 +4,7 @@
4 # create a compressed vmlinux image from the original vmlinux
5 #
6
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
9
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)
20 else
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)
27 endif
28 LDFLAGS_piggy.o := -r --format binary --oformat elf32-i386 -T
29
30 @@ -60,5 +68,9 @@ $(obj)/vmlinux.bin.gz: $(obj)/vmlinux.bi
31 LDFLAGS_piggy.o := -r --format binary --oformat elf64-x86-64 -T
32 endif
33
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
38 +
39 +$(obj)/piggy.o: $(obj)/vmlinux.scr $(obj)/vmlinux.bin.$(suffix_y) FORCE
40 $(call if_changed,ld)
41 --- a/arch/x86/boot/compressed/misc.c
42 +++ b/arch/x86/boot/compressed/misc.c
43 @@ -116,71 +116,13 @@
44 /*
45 * gzip declarations
46 */
47 -
48 -#define OF(args) args
49 #define STATIC static
50
51 #undef memset
52 #undef memcpy
53 #define memzero(s, n) memset((s), 0, (n))
54
55 -typedef unsigned char uch;
56 -typedef unsigned short ush;
57 -typedef unsigned long ulg;
58 -
59 -/*
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:
64 - */
65 -#define WSIZE 0x80000000
66
67 -/* Input buffer: */
68 -static unsigned char *inbuf;
69 -
70 -/* Sliding window buffer (and final output buffer): */
71 -static unsigned char *window;
72 -
73 -/* Valid bytes in inbuf: */
74 -static unsigned insize;
75 -
76 -/* Index of next byte to be processed in inbuf: */
77 -static unsigned inptr;
78 -
79 -/* Bytes in output buffer: */
80 -static unsigned outcnt;
81 -
82 -/* gzip flag byte */
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 */
90 -
91 -#define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
92 -
93 -/* Diagnostic functions */
94 -#ifdef DEBUG
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)
101 -#else
102 -# define Assert(cond, msg)
103 -# define Trace(x)
104 -# define Tracev(x)
105 -# define Tracevv(x)
106 -# define Tracec(c, x)
107 -# define Tracecv(c, x)
108 -#endif
109 -
110 -static int fill_inbuf(void);
111 -static void flush_window(void);
112 static void error(char *m);
113
114 /*
115 @@ -189,11 +131,6 @@ static void error(char *m);
116 static struct boot_params *real_mode; /* Pointer to real-mode data */
117 static int quiet;
118
119 -extern unsigned char input_data[];
120 -extern int input_len;
121 -
122 -static long bytes_out;
123 -
124 static void *memset(void *s, int c, unsigned n);
125 static void *memcpy(void *dest, const void *src, unsigned n);
126
127 @@ -213,7 +150,19 @@ static char *vidmem;
128 static int vidport;
129 static int lines, cols;
130
131 +#define NEW_CODE
132 +
133 +#ifdef CONFIG_KERNEL_GZIP
134 #include "../../../../lib/inflate.c"
135 +#endif
136 +
137 +#ifdef CONFIG_KERNEL_BZIP2
138 +#include "../../../../lib/decompress_bunzip2.c"
139 +#endif
140 +
141 +#ifdef CONFIG_KERNEL_LZMA
142 +#include "../../../../lib/decompress_unlzma.c"
143 +#endif
144
145 static void scroll(void)
146 {
147 @@ -293,38 +242,6 @@ static void *memcpy(void *dest, const vo
148 return dest;
149 }
150
151 -/* ===========================================================================
152 - * Fill the input buffer. This is called only when the buffer is empty
153 - * and at least one byte is really needed.
154 - */
155 -static int fill_inbuf(void)
156 -{
157 - error("ran out of input data");
158 - return 0;
159 -}
160 -
161 -/* ===========================================================================
162 - * Write the output window window[0..outcnt-1] and update crc and bytes_out.
163 - * (Used for the decompressed data only.)
164 - */
165 -static void flush_window(void)
166 -{
167 - /* With my window equal to my output buffer
168 - * I only need to compute the crc here.
169 - */
170 - unsigned long c = crc; /* temporary variable */
171 - unsigned n;
172 - unsigned char *in, ch;
173 -
174 - in = window;
175 - for (n = 0; n < outcnt; n++) {
176 - ch = *in++;
177 - c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
178 - }
179 - crc = c;
180 - bytes_out += (unsigned long)outcnt;
181 - outcnt = 0;
182 -}
183
184 static void error(char *x)
185 {
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;
189
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;
195 - inptr = 0;
196
197 #ifdef CONFIG_X86_64
198 if ((unsigned long)output & (__KERNEL_ALIGN - 1))
199 @@ -430,10 +343,9 @@ asmlinkage void decompress_kernel(void *
200 #endif
201 #endif
202
203 - makecrc();
204 if (!quiet)
205 putstr("\nDecompressing Linux... ");
206 - gunzip();
207 + decompress(input_data, input_len, NULL, NULL, output, NULL, error);
208 parse_elf(output);
209 if (!quiet)
210 putstr("done.\nBooting the kernel.\n");
211 --- a/arch/x86/include/asm/boot.h
212 +++ b/arch/x86/include/asm/boot.h
213 @@ -15,11 +15,21 @@
214 + (CONFIG_PHYSICAL_ALIGN - 1)) \
215 & ~(CONFIG_PHYSICAL_ALIGN - 1))
216
217 +#if (defined CONFIG_KERNEL_BZIP2)
218 +#define BOOT_HEAP_SIZE 0x400000
219 +#else
220 +
221 #ifdef CONFIG_X86_64
222 #define BOOT_HEAP_SIZE 0x7000
223 -#define BOOT_STACK_SIZE 0x4000
224 #else
225 #define BOOT_HEAP_SIZE 0x4000
226 +#endif
227 +
228 +#endif
229 +
230 +#ifdef CONFIG_X86_64
231 +#define BOOT_STACK_SIZE 0x4000
232 +#else
233 #define BOOT_STACK_SIZE 0x1000
234 #endif
235
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).
241
242 +config RD_BZIP2
243 + bool "Initial ramdisk compressed using bzip2"
244 + default n
245 + depends on BLK_DEV_INITRD=y
246 + help
247 + Support loading of a bzip2 encoded initial ramdisk or cpio buffer
248 + If unsure, say N.
249 +
250 +config RD_LZMA
251 + bool "Initial ramdisk compressed using lzma"
252 + default n
253 + depends on BLK_DEV_INITRD=y
254 + help
255 + Support loading of a lzma encoded initial ramdisk or cpio buffer
256 + If unsure, say N.
257 +
258 +config RD_GZIP
259 + bool "Initial ramdisk compressed using gzip"
260 + default y
261 + depends on BLK_DEV_INITRD=y
262 + help
263 + Support loading of a gzip encoded initial ramdisk or cpio buffer.
264 + If unsure, say Y.
265 +
266 config CDROM_PKTCDVD
267 tristate "Packet writing on CD/DVD media"
268 depends on !UML
269 --- /dev/null
270 +++ b/include/linux/decompress/bunzip2.h
271 @@ -0,0 +1,10 @@
272 +#ifndef DECOMPRESS_BUNZIP2_H
273 +#define DECOMPRESS_BUNZIP2_H
274 +
275 +int bunzip2(unsigned char *inbuf, int len,
276 + int(*fill)(void*, unsigned int),
277 + int(*flush)(void*, unsigned int),
278 + unsigned char *output,
279 + int *pos,
280 + void(*error)(char *x));
281 +#endif
282 --- /dev/null
283 +++ b/include/linux/decompress/generic.h
284 @@ -0,0 +1,30 @@
285 +#ifndef DECOMPRESS_GENERIC_H
286 +#define DECOMPRESS_GENERIC_H
287 +
288 +/* Minimal chunksize to be read.
289 + *Bzip2 prefers at least 4096
290 + *Lzma prefers 0x10000 */
291 +#define COMPR_IOBUF_SIZE 4096
292 +
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,
297 + int *posp,
298 + void(*error)(char *x));
299 +
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
305 + * returned here
306 + *
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
311 + */
312 +
313 +
314 +#endif
315 --- /dev/null
316 +++ b/include/linux/decompress/inflate.h
317 @@ -0,0 +1,13 @@
318 +#ifndef INFLATE_H
319 +#define INFLATE_H
320 +
321 +/* Other housekeeping constants */
322 +#define INBUFSIZ 4096
323 +
324 +int gunzip(unsigned char *inbuf, int len,
325 + int(*fill)(void*, unsigned int),
326 + int(*flush)(void*, unsigned int),
327 + unsigned char *output,
328 + int *pos,
329 + void(*error_fn)(char *x));
330 +#endif
331 --- /dev/null
332 +++ b/include/linux/decompress/mm.h
333 @@ -0,0 +1,89 @@
334 +/*
335 + * linux/compr_mm.h
336 + *
337 + * Memory management for pre-boot and ramdisk uncompressors
338 + *
339 + * Authors: Alain Knaff <alain@knaff.lu>
340 + *
341 + */
342 +
343 +#ifndef DECOMPR_MM_H
344 +#define DECOMPR_MM_H
345 +
346 +#ifdef STATIC
347 +
348 +/* Code active when included from pre-boot environment: */
349 +
350 +/* A trivial malloc implementation, adapted from
351 + * malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994
352 + */
353 +static unsigned long malloc_ptr;
354 +static int malloc_count;
355 +
356 +static void *malloc(int size)
357 +{
358 + void *p;
359 +
360 + if (size < 0)
361 + error("Malloc error");
362 + if (!malloc_ptr)
363 + malloc_ptr = free_mem_ptr;
364 +
365 + malloc_ptr = (malloc_ptr + 3) & ~3; /* Align */
366 +
367 + p = (void *)malloc_ptr;
368 + malloc_ptr += size;
369 +
370 + if (free_mem_end_ptr && malloc_ptr >= free_mem_end_ptr)
371 + error("Out of memory");
372 +
373 + malloc_count++;
374 + return p;
375 +}
376 +
377 +static void free(void *where)
378 +{
379 + malloc_count--;
380 + if (!malloc_count)
381 + malloc_ptr = free_mem_ptr;
382 +}
383 +
384 +#define large_malloc(a) malloc(a)
385 +#define large_free(a) free(a)
386 +
387 +#define set_error_fn(x)
388 +#define panic error
389 +
390 +#define INIT
391 +
392 +#else /* STATIC */
393 +
394 +/* Code active when compiled standalone for use when loading ramdisk: */
395 +
396 +#include <linux/kernel.h>
397 +#include <linux/fs.h>
398 +#include <linux/string.h>
399 +#include <linux/vmalloc.h>
400 +
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 */
404 +
405 +#define malloc(a) kmalloc(a, GFP_KERNEL)
406 +#define free(a) kfree(a)
407 +
408 +#define large_malloc(a) vmalloc(a)
409 +#define large_free(a) vfree(a)
410 +
411 +static void(*error)(char *m);
412 +#define set_error_fn(x) error = x;
413 +#define NEW_CODE
414 +
415 +#define INIT __init
416 +#define STATIC
417 +
418 +#include <linux/init.h>
419 +
420 +#endif /* STATIC */
421 +
422 +#endif /* DECOMPR_MM_H */
423 --- /dev/null
424 +++ b/include/linux/decompress/unlzma.h
425 @@ -0,0 +1,12 @@
426 +#ifndef DECOMPRESS_UNLZMA_H
427 +#define DECOMPRESS_UNLZMA_H
428 +
429 +int unlzma(unsigned char *, int,
430 + int(*fill)(void*, unsigned int),
431 + int(*flush)(void*, unsigned int),
432 + unsigned char *output,
433 + int *posp,
434 + void(*error)(char *x)
435 + );
436 +
437 +#endif
438 --- a/init/Kconfig
439 +++ b/init/Kconfig
440 @@ -101,6 +101,56 @@ config LOCALVERSION_AUTO
441
442 which is done within the script "scripts/setlocalversion".)
443
444 +choice
445 + prompt "Kernel compression mode"
446 + default KERNEL_GZIP
447 + help
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.
453 +
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)
458 +
459 + High compression options are mostly useful for users, who
460 + are low on disk space (embedded systems), but for whom ram
461 + size matters less.
462 +
463 + If in doubt, select 'gzip'
464 +
465 +config KERNEL_GZIP
466 + bool "Gzip"
467 + help
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.
471 +
472 +config KERNEL_BZIP2
473 + bool "Bzip2"
474 + help
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.
481 +
482 +config KERNEL_LZMA
483 + bool "LZMA"
484 + help
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.
490 +
491 +endchoice
492 +
493 +
494 config SWAP
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
499 @@ -11,6 +11,12 @@
500
501 #include "do_mounts.h"
502
503 +#include <linux/decompress/generic.h>
504 +
505 +#include <linux/decompress/bunzip2.h>
506 +#include <linux/decompress/unlzma.h>
507 +#include <linux/decompress/inflate.h>
508 +
509 int __initdata rd_prompt = 1;/* 1 = prompt for RAM disk, 0 = don't prompt */
510
511 static int __init prompt_ramdisk(char *str)
512 @@ -29,7 +35,7 @@ static int __init ramdisk_start_setup(ch
513 }
514 __setup("ramdisk_start=", ramdisk_start_setup);
515
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);
518
519 /*
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
522 * gzip
523 */
524 static int __init
525 -identify_ramdisk_image(int fd, int start_block)
526 +identify_ramdisk_image(int fd, int start_block, decompress_fn *decompressor)
527 {
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);
533
534 +#ifdef CONFIG_RD_GZIP
535 /*
536 * If it matches the gzip magic numbers, return 0
537 */
538 @@ -81,9 +88,39 @@ identify_ramdisk_image(int fd, int start
539 printk(KERN_NOTICE
540 "RAMDISK: Compressed image found at block %d\n",
541 start_block);
542 + *decompressor = gunzip;
543 + nblocks = 0;
544 + goto done;
545 + }
546 +#endif
547 +
548 +#ifdef CONFIG_RD_BZIP2
549 + /*
550 + * If it matches the bzip2 magic numbers, return -1
551 + */
552 + if (buf[0] == 0x42 && (buf[1] == 0x5a)) {
553 + printk(KERN_NOTICE
554 + "RAMDISK: Bzipped image found at block %d\n",
555 + start_block);
556 + *decompressor = bunzip2;
557 nblocks = 0;
558 goto done;
559 }
560 +#endif
561 +
562 +#ifdef CONFIG_RD_LZMA
563 + /*
564 + * If it matches the lzma magic numbers, return -1
565 + */
566 + if (buf[0] == 0x5d && (buf[1] == 0x00)) {
567 + printk(KERN_NOTICE
568 + "RAMDISK: Lzma image found at block %d\n",
569 + start_block);
570 + *decompressor = unlzma;
571 + nblocks = 0;
572 + goto done;
573 + }
574 +#endif
575
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;
580 char *buf = NULL;
581 unsigned short rotate = 0;
582 + decompress_fn decompressor = NULL;
583 #if !defined(CONFIG_S390) && !defined(CONFIG_PPC_ISERIES)
584 char rotator[4] = { '|' , '/' , '-' , '\\' };
585 #endif
586 @@ -168,12 +206,12 @@ int __init rd_load_image(char *from)
587 if (in_fd < 0)
588 goto noclose_input;
589
590 - nblocks = identify_ramdisk_image(in_fd, rd_image_start);
591 + nblocks = identify_ramdisk_image(in_fd, rd_image_start, &decompressor);
592 if (nblocks < 0)
593 goto done;
594
595 if (nblocks == 0) {
596 - if (crd_load(in_fd, out_fd) == 0)
597 + if (crd_load(in_fd, out_fd, decompressor) == 0)
598 goto successful_load;
599 goto done;
600 }
601 @@ -272,138 +310,48 @@ int __init rd_load_disk(int n)
602 return rd_load_image("/dev/root");
603 }
604
605 -/*
606 - * gzip declarations
607 - */
608 -
609 -#define OF(args) args
610 -
611 -#ifndef memzero
612 -#define memzero(s, n) memset ((s), 0, (n))
613 -#endif
614 -
615 -typedef unsigned char uch;
616 -typedef unsigned short ush;
617 -typedef unsigned long ulg;
618 -
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 */
622 -
623 -static uch *inbuf;
624 -static uch *window;
625 -
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;
634
635 -#define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
636 -
637 -/* Diagnostic functions (stubbed out) */
638 -#define Assert(cond,msg)
639 -#define Trace(x)
640 -#define Tracev(x)
641 -#define Tracevv(x)
642 -#define Tracec(c,x)
643 -#define Tracecv(c,x)
644 -
645 -#define STATIC static
646 -#define INIT __init
647 -
648 -static int __init fill_inbuf(void);
649 -static void __init flush_window(void);
650 -static void __init error(char *m);
651 -
652 -#define NO_INFLATE_MALLOC
653 -
654 -#include "../lib/inflate.c"
655 -
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.
660 - */
661 -static int __init fill_inbuf(void)
662 +static int __init compr_fill(void *buf, unsigned int len)
663 {
664 - if (exit_code) return -1;
665 -
666 - insize = sys_read(crd_infd, inbuf, INBUFSIZ);
667 - if (insize == 0) {
668 - error("RAMDISK: ran out of compressed data");
669 - return -1;
670 - }
671 -
672 - inptr = 1;
673 -
674 - return inbuf[0];
675 + int r = sys_read(crd_infd, buf, len);
676 + if (r < 0)
677 + printk(KERN_ERR "RAMDISK: error while reading compressed data");
678 + else if (r == 0)
679 + printk(KERN_ERR "RAMDISK: EOF while reading compressed data");
680 + return r;
681 }
682
683 -/* ===========================================================================
684 - * Write the output window window[0..outcnt-1] and update crc and bytes_out.
685 - * (Used for the decompressed data only.)
686 - */
687 -static void __init flush_window(void)
688 +static int __init compr_flush(void *window, unsigned int outcnt)
689 {
690 - ulg c = crc; /* temporary variable */
691 - unsigned n, written;
692 - uch *in, ch;
693 -
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);
698 - unzip_error = 1;
699 - }
700 - in = window;
701 - for (n = 0; n < outcnt; n++) {
702 - ch = *in++;
703 - c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
704 - }
705 - crc = c;
706 - bytes_out += (ulg)outcnt;
707 - outcnt = 0;
708 + int written = sys_write(crd_outfd, window, outcnt);
709 + if (written != outcnt) {
710 + if (decompress_error == 0)
711 + printk(KERN_ERR
712 + "RAMDISK: incomplete write (%d != %d)\n",
713 + written, outcnt);
714 + decompress_error = 1;
715 + return -1;
716 + }
717 + return outcnt;
718 }
719
720 static void __init error(char *x)
721 {
722 printk(KERN_ERR "%s\n", x);
723 exit_code = 1;
724 - unzip_error = 1;
725 + decompress_error = 1;
726 }
727
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)
730 {
731 int result;
732 -
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 */
736 - exit_code = 0;
737 - bytes_out = 0;
738 - crc = (ulg)0xffffffffL; /* shift register contents */
739 -
740 crd_infd = in_fd;
741 crd_outfd = out_fd;
742 - inbuf = kmalloc(INBUFSIZ, GFP_KERNEL);
743 - if (!inbuf) {
744 - printk(KERN_ERR "RAMDISK: Couldn't allocate gzip buffer\n");
745 - return -1;
746 - }
747 - window = kmalloc(WSIZE, GFP_KERNEL);
748 - if (!window) {
749 - printk(KERN_ERR "RAMDISK: Couldn't allocate gzip window\n");
750 - kfree(inbuf);
751 - return -1;
752 - }
753 - makecrc();
754 - result = gunzip();
755 - if (unzip_error)
756 + result = deco(NULL, 0, compr_fill, compr_flush, NULL, NULL, error);
757 + if (decompress_error)
758 result = 1;
759 - kfree(inbuf);
760 - kfree(window);
761 return result;
762 }
763 --- a/init/initramfs.c
764 +++ b/init/initramfs.c
765 @@ -389,11 +389,14 @@ static int __init write_buffer(char *buf
766 return len - count;
767 }
768
769 -static void __init flush_buffer(char *buf, unsigned len)
770 +
771 +static int __init flush_buffer(void *bufv, unsigned len)
772 {
773 + char *buf = (char *) bufv;
774 int written;
775 + int origLen = len;
776 if (message)
777 - return;
778 + return -1;
779 while ((written = write_buffer(buf, len)) < len && !message) {
780 char c = buf[written];
781 if (c == '0') {
782 @@ -407,73 +410,14 @@ static void __init flush_buffer(char *bu
783 } else
784 error("junk in compressed archive");
785 }
786 + return origLen;
787 }
788
789 -/*
790 - * gzip declarations
791 - */
792 -
793 -#define OF(args) args
794 -
795 -#ifndef memzero
796 -#define memzero(s, n) memset ((s), 0, (n))
797 -#endif
798 +static unsigned my_inptr; /* index of next byte to be processed in inbuf */
799
800 -typedef unsigned char uch;
801 -typedef unsigned short ush;
802 -typedef unsigned long ulg;
803 -
804 -#define WSIZE 0x8000 /* window size--must be a power of two, and */
805 - /* at least 32K for zip's deflate method */
806 -
807 -static uch *inbuf;
808 -static uch *window;
809 -
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;
814 -
815 -#define get_byte() (inptr < insize ? inbuf[inptr++] : -1)
816 -
817 -/* Diagnostic functions (stubbed out) */
818 -#define Assert(cond,msg)
819 -#define Trace(x)
820 -#define Tracev(x)
821 -#define Tracevv(x)
822 -#define Tracec(c,x)
823 -#define Tracecv(c,x)
824 -
825 -#define STATIC static
826 -#define INIT __init
827 -
828 -static void __init flush_window(void);
829 -static void __init error(char *m);
830 -
831 -#define NO_INFLATE_MALLOC
832 -
833 -#include "../lib/inflate.c"
834 -
835 -/* ===========================================================================
836 - * Write the output window window[0..outcnt-1] and update crc and bytes_out.
837 - * (Used for the decompressed data only.)
838 - */
839 -static void __init flush_window(void)
840 -{
841 - ulg c = crc; /* temporary variable */
842 - unsigned n;
843 - uch *in, ch;
844 -
845 - flush_buffer(window, outcnt);
846 - in = window;
847 - for (n = 0; n < outcnt; n++) {
848 - ch = *in++;
849 - c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
850 - }
851 - crc = c;
852 - bytes_out += (ulg)outcnt;
853 - outcnt = 0;
854 -}
855 +#include <linux/decompress/bunzip2.h>
856 +#include <linux/decompress/unlzma.h>
857 +#include <linux/decompress/inflate.h>
858
859 static char * __init unpack_to_rootfs(char *buf, unsigned len, int check_only)
860 {
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)
867 +
868 + if (!header_buf || !symlink_buf || !name_buf)
869 panic("can't allocate buffers");
870 +
871 state = Start;
872 this_header = 0;
873 message = NULL;
874 @@ -504,22 +449,38 @@ static char * __init unpack_to_rootfs(ch
875 continue;
876 }
877 this_header = 0;
878 - insize = len;
879 - inbuf = buf;
880 - inptr = 0;
881 - outcnt = 0; /* bytes in output buffer */
882 - bytes_out = 0;
883 - crc = (ulg)0xffffffffL; /* shift register contents */
884 - makecrc();
885 - gunzip();
886 + if (!gunzip(buf, len, NULL, flush_buffer, NULL,
887 + &my_inptr, error) &&
888 + message == NULL)
889 + goto ok;
890 +
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
895 + &&
896 + message == NULL) {
897 + goto ok;
898 + }
899 +#endif
900 +
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
905 + &&
906 + message == NULL) {
907 + goto ok;
908 + }
909 +#endif
910 +ok:
911 if (state != Reset)
912 - error("junk in gzipped archive");
913 - this_header = saved_offset + inptr;
914 - buf += inptr;
915 - len -= inptr;
916 + error("junk in compressed archive");
917 + this_header = saved_offset + my_inptr;
918 + buf += my_inptr;
919 + len -= my_inptr;
920 }
921 dir_utime();
922 - kfree(window);
923 kfree(name_buf);
924 kfree(symlink_buf);
925 kfree(header_buf);
926 --- a/lib/Makefile
927 +++ b/lib/Makefile
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
935
936 lib-$(CONFIG_MMU) += ioremap.o
937 lib-$(CONFIG_SMP) += cpumask.o
938 --- /dev/null
939 +++ b/lib/decompress_bunzip2.c
940 @@ -0,0 +1,735 @@
941 +/* vi: set sw = 4 ts = 4: */
942 +/* Small bzip2 deflate implementation, by Rob Landley (rob@landley.net).
943 +
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.
948 +
949 + This code is licensed under the LGPLv2:
950 + LGPL (http://www.gnu.org/copyleft/lgpl.html
951 +*/
952 +
953 +/*
954 + Size and speed optimizations by Manuel Novoa III (mjn3@codepoet.org).
955 +
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.
959 +
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.
963 +
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.
968 +
969 + In memory of Toni W. Hagan
970 +
971 + Hospice of Acadiana, Inc.
972 + 2600 Johnston St., Suite 200
973 + Lafayette, LA 70503-3240
974 +
975 + Phone (337) 232-1234 or 1-800-738-2226
976 + Fax (337) 232-1297
977 +
978 + http://www.hospiceacadiana.com/
979 +
980 + Manuel
981 + */
982 +
983 +/*
984 + Made it fit for running in Linux Kernel by Alain Knaff (alain@knaff.lu)
985 +*/
986 +
987 +
988 +#ifndef STATIC
989 +#include <linux/decompress/bunzip2.h>
990 +#endif /* !STATIC */
991 +
992 +#include <linux/decompress/mm.h>
993 +
994 +#ifndef INT_MAX
995 +#define INT_MAX 0x7fffffff
996 +#endif
997 +
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
1005 +
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)
1015 +
1016 +/* Other housekeeping constants */
1017 +#define BZIP2_IOBUF_SIZE 4096
1018 +
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;
1026 +};
1027 +
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
1039 + data */
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 */
1047 +};
1048 +
1049 +
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)
1053 +{
1054 + unsigned int bits = 0;
1055 +
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
1061 + so */
1062 + if (bd->inbufPos == bd->inbufCount) {
1063 + if (bd->io_error)
1064 + return 0;
1065 + bd->inbufCount = bd->fill(bd->inbuf, BZIP2_IOBUF_SIZE);
1066 + if (bd->inbufCount <= 0) {
1067 + bd->io_error = RETVAL_UNEXPECTED_INPUT_EOF;
1068 + return 0;
1069 + }
1070 + bd->inbufPos = 0;
1071 + }
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;
1078 + }
1079 + /* Grab next 8 bits of input from buffer. */
1080 + bd->inbufBits = (bd->inbufBits << 8)|bd->inbuf[bd->inbufPos++];
1081 + bd->inbufBitCount += 8;
1082 + }
1083 + /* Calculate result */
1084 + bd->inbufBitCount -= bits_wanted;
1085 + bits |= (bd->inbufBits >> bd->inbufBitCount)&((1 << bits_wanted)-1);
1086 +
1087 + return bits;
1088 +}
1089 +
1090 +/* Unpacks the next block and sets up for the inverse burrows-wheeler step. */
1091 +
1092 +static int INIT get_next_block(struct bunzip_data *bd)
1093 +{
1094 + struct group_data *hufGroup = NULL;
1095 + int *base = NULL;
1096 + int *limit = NULL;
1097 + int dbufCount, nextSym, dbufSize, groupCount, selector,
1098 + i, j, k, t, runPos, symCount, symTotal, nSelectors,
1099 + byteCount[256];
1100 + unsigned char uc, symToByte[256], mtfSymbol[256], *selectors;
1101 + unsigned int *dbuf, origPtr;
1102 +
1103 + dbuf = bd->dbuf;
1104 + dbufSize = bd->dbufSize;
1105 + selectors = bd->selectors;
1106 +
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);
1130 + symTotal = 0;
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;
1137 + }
1138 + }
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
1147 + list.) */
1148 + nSelectors = get_bits(bd, 15);
1149 + if (!nSelectors)
1150 + return RETVAL_DATA_ERROR;
1151 + for (i = 0; i < groupCount; i++)
1152 + mtfSymbol[i] = 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];
1160 + for (; j; j--)
1161 + mtfSymbol[j] = mtfSymbol[j-1];
1162 + mtfSymbol[0] = selectors[i] = uc;
1163 + }
1164 + /* Read the Huffman coding tables for each group, which code
1165 + for symTotal literal symbols, plus two run symbols (RUNA,
1166 + RUNB) */
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
1179 + it.) */
1180 + t = get_bits(bd, 5)-1;
1181 + for (i = 0; i < symCount; i++) {
1182 + for (;;) {
1183 + if (((unsigned)t) > (MAX_HUFCODE_BITS-1))
1184 + return RETVAL_DATA_ERROR;
1185 +
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. */
1191 +
1192 + k = get_bits(bd, 2);
1193 + if (k < 2) {
1194 + bd->inbufBitCount++;
1195 + break;
1196 + }
1197 + /* Add one if second bit 1, else
1198 + * subtract 1. Avoids if/else */
1199 + t += (((k+1)&2)-1);
1200 + }
1201 + /* Correct for the initial -1, to get the
1202 + * final symbol length */
1203 + length[i] = t+1;
1204 + }
1205 + /* Find largest and smallest lengths in this group */
1206 + minLen = maxLen = length[0];
1207 +
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];
1213 + }
1214 +
1215 + /* Calculate permute[], base[], and limit[] tables from
1216 + * length[].
1217 + *
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
1222 + * permute[].
1223 + *
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
1228 + * bit.
1229 + */
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[]. */
1241 + pp = 0;
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;
1247 + }
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
1256 + *already). */
1257 + pp = t = 0;
1258 + for (i = minLen; i < maxLen; i++) {
1259 + pp += temp[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]
1269 + comparison. */
1270 + limit[i] = (pp << (maxLen - i)) - 1;
1271 + pp <<= 1;
1272 + base[i+1] = pp-(t += temp[i]);
1273 + }
1274 + limit[maxLen+1] = INT_MAX; /* Sentinal value for
1275 + * reading next sym. */
1276 + limit[maxLen] = pp+temp[maxLen]-1;
1277 + base[minLen] = 0;
1278 + }
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 */
1283 +
1284 + /* Initialize symbol occurrence counters and symbol Move To
1285 + * Front table */
1286 + for (i = 0; i < 256; i++) {
1287 + byteCount[i] = 0;
1288 + mtfSymbol[i] = (unsigned char)i;
1289 + }
1290 + /* Loop through compressed symbols. */
1291 + runPos = dbufCount = symCount = selector = 0;
1292 + for (;;) {
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;
1301 + }
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);
1313 + */
1314 + while (bd->inbufBitCount < hufGroup->maxLen) {
1315 + if (bd->inbufPos == bd->inbufCount) {
1316 + j = get_bits(bd, hufGroup->maxLen);
1317 + goto got_huff_bits;
1318 + }
1319 + bd->inbufBits =
1320 + (bd->inbufBits << 8)|bd->inbuf[bd->inbufPos++];
1321 + bd->inbufBitCount += 8;
1322 + };
1323 + bd->inbufBitCount -= hufGroup->maxLen;
1324 + j = (bd->inbufBits >> bd->inbufBitCount)&
1325 + ((1 << hufGroup->maxLen)-1);
1326 +got_huff_bits:
1327 + /* Figure how how many bits are in next symbol and
1328 + * unget extras */
1329 + i = hufGroup->minLen;
1330 + while (j > limit[i])
1331 + ++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]))
1336 + >= MAX_SYMBOLS))
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
1346 + * counter */
1347 + if (!runPos) {
1348 + runPos = 1;
1349 + t = 0;
1350 + }
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 */
1363 +
1364 + runPos <<= 1;
1365 + continue;
1366 + }
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
1372 + array.) */
1373 + if (runPos) {
1374 + runPos = 0;
1375 + if (dbufCount+t >= dbufSize)
1376 + return RETVAL_DATA_ERROR;
1377 +
1378 + uc = symToByte[mtfSymbol[0]];
1379 + byteCount[uc] += t;
1380 + while (t--)
1381 + dbuf[dbufCount++] = uc;
1382 + }
1383 + /* Is this the terminating symbol? */
1384 + if (nextSym > symTotal)
1385 + break;
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;
1397 + i = nextSym - 1;
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. */
1404 + do {
1405 + mtfSymbol[i] = mtfSymbol[i-1];
1406 + } while (--i);
1407 + mtfSymbol[0] = uc;
1408 + uc = symToByte[uc];
1409 + /* We have our literal byte. Save it into dbuf. */
1410 + byteCount[uc]++;
1411 + dbuf[dbufCount++] = (unsigned int)uc;
1412 + }
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
1419 + */
1420 + /* Turn byteCount into cumulative occurrence counts of 0 to n-1. */
1421 + j = 0;
1422 + for (i = 0; i < 256; i++) {
1423 + k = j+byteCount[i];
1424 + byteCount[i] = j;
1425 + j = k;
1426 + }
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);
1431 + byteCount[uc]++;
1432 + }
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). */
1437 + if (dbufCount) {
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;
1444 + }
1445 + bd->writeCount = dbufCount;
1446 +
1447 + return RETVAL_OK;
1448 +}
1449 +
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.
1455 +*/
1456 +
1457 +static int INIT read_bunzip(struct bunzip_data *bd, char *outbuf, int len)
1458 +{
1459 + const unsigned int *dbuf;
1460 + int pos, xcurrent, previous, gotcount;
1461 +
1462 + /* If last read was short due to end of file, return last block now */
1463 + if (bd->writeCount < 0)
1464 + return bd->writeCount;
1465 +
1466 + gotcount = 0;
1467 + dbuf = bd->dbuf;
1468 + pos = bd->writePos;
1469 + xcurrent = bd->writeCurrent;
1470 +
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). */
1474 +
1475 + if (bd->writeCopies) {
1476 + /* Inside the loop, writeCopies means extra copies (beyond 1) */
1477 + --bd->writeCopies;
1478 + /* Loop outputting bytes */
1479 + for (;;) {
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++;
1486 + return len;
1487 + }
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)
1492 + ^xcurrent]);
1493 + /* Loop now if we're outputting multiple
1494 + * copies of this byte */
1495 + if (bd->writeCopies) {
1496 + --bd->writeCopies;
1497 + continue;
1498 + }
1499 +decode_next_byte:
1500 + if (!bd->writeCount--)
1501 + break;
1502 + /* Follow sequence vector to undo
1503 + * Burrows-Wheeler transform */
1504 + previous = xcurrent;
1505 + pos = dbuf[pos];
1506 + xcurrent = pos&0xff;
1507 + pos >>= 8;
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;
1515 + } else {
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;
1528 + }
1529 + }
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;
1538 + }
1539 + }
1540 +
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);
1545 + if (previous) {
1546 + bd->writeCount = previous;
1547 + return (previous != RETVAL_LAST_BLOCK) ? previous : gotcount;
1548 + }
1549 + bd->writeCRC = 0xffffffffUL;
1550 + pos = bd->writePos;
1551 + xcurrent = bd->writeCurrent;
1552 + goto decode_next_byte;
1553 +}
1554 +
1555 +static int INIT nofill(void *buf, unsigned int len)
1556 +{
1557 + return -1;
1558 +}
1559 +
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))
1565 +{
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';
1571 +
1572 + /* Figure out how much data to allocate */
1573 + i = sizeof(struct bunzip_data);
1574 +
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;
1581 + if (fill != NULL)
1582 + bd->fill = fill;
1583 + else
1584 + bd->fill = nofill;
1585 +
1586 + /* Init the CRC32 table (big endian) */
1587 + for (i = 0; i < 256; i++) {
1588 + c = i << 24;
1589 + for (j = 8; j; j--)
1590 + c = c&0x80000000 ? (c << 1)^0x04c11db7 : (c << 1);
1591 + bd->crc32Table[i] = c;
1592 + }
1593 +
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;
1598 +
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);
1602 +
1603 + bd->dbuf = large_malloc(bd->dbufSize * sizeof(int));
1604 + return RETVAL_OK;
1605 +}
1606 +
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,
1613 + int *pos,
1614 + void(*error_fn)(char *x))
1615 +{
1616 + struct bunzip_data *bd;
1617 + int i = -1;
1618 + unsigned char *inbuf;
1619 +
1620 + set_error_fn(error_fn);
1621 + if (flush)
1622 + outbuf = malloc(BZIP2_IOBUF_SIZE);
1623 + else
1624 + len -= 4; /* Uncompressed size hack active in pre-boot
1625 + environment */
1626 + if (!outbuf) {
1627 + error("Could not allocate output bufer");
1628 + return -1;
1629 + }
1630 + if (buf)
1631 + inbuf = buf;
1632 + else
1633 + inbuf = malloc(BZIP2_IOBUF_SIZE);
1634 + if (!inbuf) {
1635 + error("Could not allocate input bufer");
1636 + goto exit_0;
1637 + }
1638 + i = start_bunzip(&bd, inbuf, len, fill);
1639 + if (!i) {
1640 + for (;;) {
1641 + i = read_bunzip(bd, outbuf, BZIP2_IOBUF_SIZE);
1642 + if (i <= 0)
1643 + break;
1644 + if (!flush)
1645 + outbuf += i;
1646 + else
1647 + if (i != flush(outbuf, i)) {
1648 + i = RETVAL_UNEXPECTED_OUTPUT_EOF;
1649 + break;
1650 + }
1651 + }
1652 + }
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.");
1657 + else
1658 + i = RETVAL_OK;
1659 + } else if (i == RETVAL_UNEXPECTED_OUTPUT_EOF) {
1660 + error("Compressed file ends unexpectedly");
1661 + }
1662 + if (bd->dbuf)
1663 + large_free(bd->dbuf);
1664 + if (pos)
1665 + *pos = bd->inbufPos;
1666 + free(bd);
1667 + if (!buf)
1668 + free(inbuf);
1669 +exit_0:
1670 + if (flush)
1671 + free(outbuf);
1672 + return i;
1673 +}
1674 +
1675 +#define decompress bunzip2
1676 --- /dev/null
1677 +++ b/lib/decompress_unlzma.c
1678 @@ -0,0 +1,647 @@
1679 +/* Lzma decompressor for Linux kernel. Shamelessly snarfed
1680 + *from busybox 1.1.1
1681 + *
1682 + *Linux kernel adaptation
1683 + *Copyright (C) 2006 Alain < alain@knaff.lu >
1684 + *
1685 + *Based on small lzma deflate implementation/Small range coder
1686 + *implementation for lzma.
1687 + *Copyright (C) 2006 Aurelien Jacobs < aurel@gnuage.org >
1688 + *
1689 + *Based on LzmaDecode.c from the LZMA SDK 4.22 (http://www.7-zip.org/)
1690 + *Copyright (C) 1999-2005 Igor Pavlov
1691 + *
1692 + *Copyrights of the parts, see headers below.
1693 + *
1694 + *
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.
1699 + *
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.
1704 + *
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
1708 + */
1709 +
1710 +#ifndef STATIC
1711 +#include <linux/decompress/unlzma.h>
1712 +#endif /* STATIC */
1713 +
1714 +#include <linux/decompress/mm.h>
1715 +
1716 +#define MIN(a, b) (((a) < (b)) ? (a) : (b))
1717 +
1718 +static long long INIT read_int(unsigned char *ptr, int size)
1719 +{
1720 + int i;
1721 + long long ret = 0;
1722 +
1723 + for (i = 0; i < size; i++)
1724 + ret = (ret << 8) | ptr[size-i-1];
1725 + return ret;
1726 +}
1727 +
1728 +#define ENDIAN_CONVERT(x) \
1729 + x = (typeof(x))read_int((unsigned char *)&x, sizeof(x))
1730 +
1731 +
1732 +/* Small range coder implementation for lzma.
1733 + *Copyright (C) 2006 Aurelien Jacobs < aurel@gnuage.org >
1734 + *
1735 + *Based on LzmaDecode.c from the LZMA SDK 4.22 (http://www.7-zip.org/)
1736 + *Copyright (c) 1999-2005 Igor Pavlov
1737 + */
1738 +
1739 +#include <linux/compiler.h>
1740 +
1741 +#define LZMA_IOBUF_SIZE 0x10000
1742 +
1743 +struct rc {
1744 + int (*fill)(void*, unsigned int);
1745 + uint8_t *ptr;
1746 + uint8_t *buffer;
1747 + uint8_t *buffer_end;
1748 + int buffer_size;
1749 + uint32_t code;
1750 + uint32_t range;
1751 + uint32_t bound;
1752 +};
1753 +
1754 +
1755 +#define RC_TOP_BITS 24
1756 +#define RC_MOVE_BITS 5
1757 +#define RC_MODEL_TOTAL_BITS 11
1758 +
1759 +
1760 +/* Called twice: once at startup and once in rc_normalize() */
1761 +static void INIT rc_read(struct rc *rc)
1762 +{
1763 + rc->buffer_size = rc->fill((char *)rc->buffer, LZMA_IOBUF_SIZE);
1764 + if (rc->buffer_size <= 0)
1765 + error("unexpected EOF");
1766 + rc->ptr = rc->buffer;
1767 + rc->buffer_end = rc->buffer + rc->buffer_size;
1768 +}
1769 +
1770 +/* Called once */
1771 +static inline void INIT rc_init(struct rc *rc,
1772 + int (*fill)(void*, unsigned int),
1773 + char *buffer, int buffer_size)
1774 +{
1775 + rc->fill = fill;
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;
1780 +
1781 + rc->code = 0;
1782 + rc->range = 0xFFFFFFFF;
1783 +}
1784 +
1785 +static inline void INIT rc_init_code(struct rc *rc)
1786 +{
1787 + int i;
1788 +
1789 + for (i = 0; i < 5; i++) {
1790 + if (rc->ptr >= rc->buffer_end)
1791 + rc_read(rc);
1792 + rc->code = (rc->code << 8) | *rc->ptr++;
1793 + }
1794 +}
1795 +
1796 +
1797 +/* Called once. TODO: bb_maybe_free() */
1798 +static inline void INIT rc_free(struct rc *rc)
1799 +{
1800 + free(rc->buffer);
1801 +}
1802 +
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)
1805 +{
1806 + if (rc->ptr >= rc->buffer_end)
1807 + rc_read(rc);
1808 + rc->range <<= 8;
1809 + rc->code = (rc->code << 8) | *rc->ptr++;
1810 +}
1811 +static inline void INIT rc_normalize(struct rc *rc)
1812 +{
1813 + if (rc->range < (1 << RC_TOP_BITS))
1814 + rc_do_normalize(rc);
1815 +}
1816 +
1817 +/* Called 9 times */
1818 +/* Why rc_is_bit_0_helper exists?
1819 + *Because we want to always expose (rc->code < rc->bound) to optimizer
1820 + */
1821 +static inline uint32_t INIT rc_is_bit_0_helper(struct rc *rc, uint16_t *p)
1822 +{
1823 + rc_normalize(rc);
1824 + rc->bound = *p * (rc->range >> RC_MODEL_TOTAL_BITS);
1825 + return rc->bound;
1826 +}
1827 +static inline int INIT rc_is_bit_0(struct rc *rc, uint16_t *p)
1828 +{
1829 + uint32_t t = rc_is_bit_0_helper(rc, p);
1830 + return rc->code < t;
1831 +}
1832 +
1833 +/* Called ~10 times, but very small, thus inlined */
1834 +static inline void INIT rc_update_bit_0(struct rc *rc, uint16_t *p)
1835 +{
1836 + rc->range = rc->bound;
1837 + *p += ((1 << RC_MODEL_TOTAL_BITS) - *p) >> RC_MOVE_BITS;
1838 +}
1839 +static inline void rc_update_bit_1(struct rc *rc, uint16_t *p)
1840 +{
1841 + rc->range -= rc->bound;
1842 + rc->code -= rc->bound;
1843 + *p -= *p >> RC_MOVE_BITS;
1844 +}
1845 +
1846 +/* Called 4 times in unlzma loop */
1847 +static int INIT rc_get_bit(struct rc *rc, uint16_t *p, int *symbol)
1848 +{
1849 + if (rc_is_bit_0(rc, p)) {
1850 + rc_update_bit_0(rc, p);
1851 + *symbol *= 2;
1852 + return 0;
1853 + } else {
1854 + rc_update_bit_1(rc, p);
1855 + *symbol = *symbol * 2 + 1;
1856 + return 1;
1857 + }
1858 +}
1859 +
1860 +/* Called once */
1861 +static inline int INIT rc_direct_bit(struct rc *rc)
1862 +{
1863 + rc_normalize(rc);
1864 + rc->range >>= 1;
1865 + if (rc->code >= rc->range) {
1866 + rc->code -= rc->range;
1867 + return 1;
1868 + }
1869 + return 0;
1870 +}
1871 +
1872 +/* Called twice */
1873 +static inline void INIT
1874 +rc_bit_tree_decode(struct rc *rc, uint16_t *p, int num_levels, int *symbol)
1875 +{
1876 + int i = num_levels;
1877 +
1878 + *symbol = 1;
1879 + while (i--)
1880 + rc_get_bit(rc, p + *symbol, symbol);
1881 + *symbol -= 1 << num_levels;
1882 +}
1883 +
1884 +
1885 +/*
1886 + * Small lzma deflate implementation.
1887 + * Copyright (C) 2006 Aurelien Jacobs < aurel@gnuage.org >
1888 + *
1889 + * Based on LzmaDecode.c from the LZMA SDK 4.22 (http://www.7-zip.org/)
1890 + * Copyright (C) 1999-2005 Igor Pavlov
1891 + */
1892 +
1893 +
1894 +struct lzma_header {
1895 + uint8_t pos;
1896 + uint32_t dict_size;
1897 + uint64_t dst_size;
1898 +} __attribute__ ((packed)) ;
1899 +
1900 +
1901 +#define LZMA_BASE_SIZE 1846
1902 +#define LZMA_LIT_SIZE 768
1903 +
1904 +#define LZMA_NUM_POS_BITS_MAX 4
1905 +
1906 +#define LZMA_LEN_NUM_LOW_BITS 3
1907 +#define LZMA_LEN_NUM_MID_BITS 3
1908 +#define LZMA_LEN_NUM_HIGH_BITS 8
1909 +
1910 +#define LZMA_LEN_CHOICE 0
1911 +#define LZMA_LEN_CHOICE_2 (LZMA_LEN_CHOICE + 1)
1912 +#define LZMA_LEN_LOW (LZMA_LEN_CHOICE_2 + 1)
1913 +#define LZMA_LEN_MID (LZMA_LEN_LOW \
1914 + + (1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_LOW_BITS)))
1915 +#define LZMA_LEN_HIGH (LZMA_LEN_MID \
1916 + +(1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_MID_BITS)))
1917 +#define LZMA_NUM_LEN_PROBS (LZMA_LEN_HIGH + (1 << LZMA_LEN_NUM_HIGH_BITS))
1918 +
1919 +#define LZMA_NUM_STATES 12
1920 +#define LZMA_NUM_LIT_STATES 7
1921 +
1922 +#define LZMA_START_POS_MODEL_INDEX 4
1923 +#define LZMA_END_POS_MODEL_INDEX 14
1924 +#define LZMA_NUM_FULL_DISTANCES (1 << (LZMA_END_POS_MODEL_INDEX >> 1))
1925 +
1926 +#define LZMA_NUM_POS_SLOT_BITS 6
1927 +#define LZMA_NUM_LEN_TO_POS_STATES 4
1928 +
1929 +#define LZMA_NUM_ALIGN_BITS 4
1930 +
1931 +#define LZMA_MATCH_MIN_LEN 2
1932 +
1933 +#define LZMA_IS_MATCH 0
1934 +#define LZMA_IS_REP (LZMA_IS_MATCH + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX))
1935 +#define LZMA_IS_REP_G0 (LZMA_IS_REP + LZMA_NUM_STATES)
1936 +#define LZMA_IS_REP_G1 (LZMA_IS_REP_G0 + LZMA_NUM_STATES)
1937 +#define LZMA_IS_REP_G2 (LZMA_IS_REP_G1 + LZMA_NUM_STATES)
1938 +#define LZMA_IS_REP_0_LONG (LZMA_IS_REP_G2 + LZMA_NUM_STATES)
1939 +#define LZMA_POS_SLOT (LZMA_IS_REP_0_LONG \
1940 + + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX))
1941 +#define LZMA_SPEC_POS (LZMA_POS_SLOT \
1942 + +(LZMA_NUM_LEN_TO_POS_STATES << LZMA_NUM_POS_SLOT_BITS))
1943 +#define LZMA_ALIGN (LZMA_SPEC_POS \
1944 + + LZMA_NUM_FULL_DISTANCES - LZMA_END_POS_MODEL_INDEX)
1945 +#define LZMA_LEN_CODER (LZMA_ALIGN + (1 << LZMA_NUM_ALIGN_BITS))
1946 +#define LZMA_REP_LEN_CODER (LZMA_LEN_CODER + LZMA_NUM_LEN_PROBS)
1947 +#define LZMA_LITERAL (LZMA_REP_LEN_CODER + LZMA_NUM_LEN_PROBS)
1948 +
1949 +
1950 +struct writer {
1951 + uint8_t *buffer;
1952 + uint8_t previous_byte;
1953 + size_t buffer_pos;
1954 + int bufsize;
1955 + size_t global_pos;
1956 + int(*flush)(void*, unsigned int);
1957 + struct lzma_header *header;
1958 +};
1959 +
1960 +struct cstate {
1961 + int state;
1962 + uint32_t rep0, rep1, rep2, rep3;
1963 +};
1964 +
1965 +static inline size_t INIT get_pos(struct writer *wr)
1966 +{
1967 + return
1968 + wr->global_pos + wr->buffer_pos;
1969 +}
1970 +
1971 +static inline uint8_t INIT peek_old_byte(struct writer *wr,
1972 + uint32_t offs)
1973 +{
1974 + if (!wr->flush) {
1975 + int32_t pos;
1976 + while (offs > wr->header->dict_size)
1977 + offs -= wr->header->dict_size;
1978 + pos = wr->buffer_pos - offs;
1979 + return wr->buffer[pos];
1980 + } else {
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];
1985 + }
1986 +
1987 +}
1988 +
1989 +static inline void INIT write_byte(struct writer *wr, uint8_t byte)
1990 +{
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);
1996 + }
1997 +}
1998 +
1999 +
2000 +static inline void INIT copy_byte(struct writer *wr, uint32_t offs)
2001 +{
2002 + write_byte(wr, peek_old_byte(wr, offs));
2003 +}
2004 +
2005 +static inline void INIT copy_bytes(struct writer *wr,
2006 + uint32_t rep0, int len)
2007 +{
2008 + do {
2009 + copy_byte(wr, rep0);
2010 + len--;
2011 + } while (len != 0 && wr->buffer_pos < wr->header->dst_size);
2012 +}
2013 +
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) {
2018 + int mi = 1;
2019 + rc_update_bit_0(rc, prob);
2020 + prob = (p + LZMA_LITERAL +
2021 + (LZMA_LIT_SIZE
2022 + * (((get_pos(wr) & literal_pos_mask) << lc)
2023 + + (wr->previous_byte >> (8 - lc))))
2024 + );
2025 +
2026 + if (cst->state >= LZMA_NUM_LIT_STATES) {
2027 + int match_byte = peek_old_byte(wr, cst->rep0);
2028 + do {
2029 + int bit;
2030 + uint16_t *prob_lit;
2031 +
2032 + match_byte <<= 1;
2033 + bit = match_byte & 0x100;
2034 + prob_lit = prob + 0x100 + bit + mi;
2035 + if (rc_get_bit(rc, prob_lit, &mi)) {
2036 + if (!bit)
2037 + break;
2038 + } else {
2039 + if (bit)
2040 + break;
2041 + }
2042 + } while (mi < 0x100);
2043 + }
2044 + while (mi < 0x100) {
2045 + uint16_t *prob_lit = prob + mi;
2046 + rc_get_bit(rc, prob_lit, &mi);
2047 + }
2048 + write_byte(wr, mi);
2049 + if (cst->state < 4)
2050 + cst->state = 0;
2051 + else if (cst->state < 10)
2052 + cst->state -= 3;
2053 + else
2054 + cst->state -= 6;
2055 +}
2056 +
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) {
2060 + int offset;
2061 + uint16_t *prob_len;
2062 + int num_bits;
2063 + int len;
2064 +
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;
2074 + } else {
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
2080 + + (cst->state <<
2081 + LZMA_NUM_POS_BITS_MAX) +
2082 + pos_state);
2083 + if (rc_is_bit_0(rc, prob)) {
2084 + rc_update_bit_0(rc, prob);
2085 +
2086 + cst->state = cst->state < LZMA_NUM_LIT_STATES ?
2087 + 9 : 11;
2088 + copy_byte(wr, cst->rep0);
2089 + return;
2090 + } else {
2091 + rc_update_bit_1(rc, prob);
2092 + }
2093 + } else {
2094 + uint32_t distance;
2095 +
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;
2101 + } else {
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;
2107 + } else {
2108 + rc_update_bit_1(rc, prob);
2109 + distance = cst->rep3;
2110 + cst->rep3 = cst->rep2;
2111 + }
2112 + cst->rep2 = cst->rep1;
2113 + }
2114 + cst->rep1 = cst->rep0;
2115 + cst->rep0 = distance;
2116 + }
2117 + cst->state = cst->state < LZMA_NUM_LIT_STATES ? 8 : 11;
2118 + prob = p + LZMA_REP_LEN_CODER;
2119 + }
2120 +
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
2125 + + (pos_state <<
2126 + LZMA_LEN_NUM_LOW_BITS));
2127 + offset = 0;
2128 + num_bits = LZMA_LEN_NUM_LOW_BITS;
2129 + } else {
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
2135 + + (pos_state <<
2136 + LZMA_LEN_NUM_MID_BITS));
2137 + offset = 1 << LZMA_LEN_NUM_LOW_BITS;
2138 + num_bits = LZMA_LEN_NUM_MID_BITS;
2139 + } else {
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;
2145 + }
2146 + }
2147 +
2148 + rc_bit_tree_decode(rc, prob_len, num_bits, &len);
2149 + len += offset;
2150 +
2151 + if (cst->state < 4) {
2152 + int pos_slot;
2153 +
2154 + cst->state += LZMA_NUM_LIT_STATES;
2155 + prob =
2156 + p + LZMA_POS_SLOT +
2157 + ((len <
2158 + LZMA_NUM_LEN_TO_POS_STATES ? len :
2159 + LZMA_NUM_LEN_TO_POS_STATES - 1)
2160 + << LZMA_NUM_POS_SLOT_BITS);
2161 + rc_bit_tree_decode(rc, prob,
2162 + LZMA_NUM_POS_SLOT_BITS,
2163 + &pos_slot);
2164 + if (pos_slot >= LZMA_START_POS_MODEL_INDEX) {
2165 + int i, mi;
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;
2172 + } else {
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;
2180 + }
2181 + i = 1;
2182 + mi = 1;
2183 + while (num_bits--) {
2184 + if (rc_get_bit(rc, prob + mi, &mi))
2185 + cst->rep0 |= i;
2186 + i <<= 1;
2187 + }
2188 + } else
2189 + cst->rep0 = pos_slot;
2190 + if (++(cst->rep0) == 0)
2191 + return;
2192 + }
2193 +
2194 + len += LZMA_MATCH_MIN_LEN;
2195 +
2196 + copy_bytes(wr, cst->rep0, len);
2197 +}
2198 +
2199 +
2200 +
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,
2205 + int *posp,
2206 + void(*error_fn)(char *x)
2207 + )
2208 +{
2209 + struct lzma_header header;
2210 + int lc, pb, lp;
2211 + uint32_t pos_state_mask;
2212 + uint32_t literal_pos_mask;
2213 + uint16_t *p;
2214 + int num_probs;
2215 + struct rc rc;
2216 + int i, mi;
2217 + struct writer wr;
2218 + struct cstate cst;
2219 + unsigned char *inbuf;
2220 + int ret = -1;
2221 +
2222 + set_error_fn(error_fn);
2223 + if (!flush)
2224 + in_len -= 4; /* Uncompressed size hack active in pre-boot
2225 + environment */
2226 + if (buf)
2227 + inbuf = buf;
2228 + else
2229 + inbuf = malloc(LZMA_IOBUF_SIZE);
2230 + if (!inbuf) {
2231 + error("Could not allocate input bufer");
2232 + goto exit_0;
2233 + }
2234 +
2235 + cst.state = 0;
2236 + cst.rep0 = cst.rep1 = cst.rep2 = cst.rep3 = 1;
2237 +
2238 + wr.header = &header;
2239 + wr.flush = flush;
2240 + wr.global_pos = 0;
2241 + wr.previous_byte = 0;
2242 + wr.buffer_pos = 0;
2243 +
2244 + rc_init(&rc, fill, inbuf, in_len);
2245 +
2246 + for (i = 0; i < sizeof(header); i++) {
2247 + if (rc.ptr >= rc.buffer_end)
2248 + rc_read(&rc);
2249 + ((unsigned char *)&header)[i] = *rc.ptr++;
2250 + }
2251 +
2252 + if (header.pos >= (9 * 5 * 5))
2253 + error("bad header");
2254 +
2255 + mi = 0;
2256 + lc = header.pos;
2257 + while (lc >= 9) {
2258 + mi++;
2259 + lc -= 9;
2260 + }
2261 + pb = 0;
2262 + lp = mi;
2263 + while (lp >= 5) {
2264 + pb++;
2265 + lp -= 5;
2266 + }
2267 + pos_state_mask = (1 << pb) - 1;
2268 + literal_pos_mask = (1 << lp) - 1;
2269 +
2270 + ENDIAN_CONVERT(header.dict_size);
2271 + ENDIAN_CONVERT(header.dst_size);
2272 +
2273 + if (header.dict_size == 0)
2274 + header.dict_size = 1;
2275 +
2276 + if (output)
2277 + wr.buffer = output;
2278 + else {
2279 + wr.bufsize = MIN(header.dst_size, header.dict_size);
2280 + wr.buffer = large_malloc(wr.bufsize);
2281 + }
2282 + if (wr.buffer == NULL)
2283 + goto exit_1;
2284 +
2285 + num_probs = LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp));
2286 + p = (uint16_t *) large_malloc(num_probs * sizeof(*p));
2287 + if (p == 0)
2288 + goto exit_2;
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;
2292 +
2293 + rc_init_code(&rc);
2294 +
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);
2302 + else {
2303 + process_bit1(&wr, &rc, &cst, p, pos_state, prob);
2304 + if (cst.rep0 == 0)
2305 + break;
2306 + }
2307 + }
2308 +
2309 + if (posp)
2310 + *posp = rc.ptr-rc.buffer;
2311 + if (wr.flush)
2312 + wr.flush(wr.buffer, wr.buffer_pos);
2313 + ret = 0;
2314 + large_free(p);
2315 +exit_2:
2316 + if (!output)
2317 + large_free(wr.buffer);
2318 +exit_1:
2319 + if (!buf)
2320 + free(inbuf);
2321 +exit_0:
2322 + return ret;
2323 +}
2324 +
2325 +#define decompress unlzma
2326 --- a/lib/inflate.c
2327 +++ b/lib/inflate.c
2328 @@ -109,20 +109,78 @@ static char rcsid[] = "#Id: inflate.c,v
2329 #endif
2330
2331 #ifndef STATIC
2332 +#include <linux/decompress/inflate.h>
2333 +#endif /* ! STATIC */
2334
2335 -#if defined(STDC_HEADERS) || defined(HAVE_STDLIB_H)
2336 -# include <sys/types.h>
2337 -# include <stdlib.h>
2338 +#include <linux/decompress/mm.h>
2339 +
2340 +#include <linux/string.h>
2341 +
2342 +#ifdef NEW_CODE
2343 +static int(*flush_cb)(void*, unsigned int);
2344 +static int(*fill_cb)(void*, unsigned int);
2345 +
2346 +/* Begin stuff copied from initramfs */
2347 +/*
2348 + * gzip declarations
2349 + */
2350 +
2351 +#define OF(args) args
2352 +
2353 +#ifndef memzero
2354 +#define memzero(s, n) memset((s), 0, (n))
2355 #endif
2356
2357 -#include "gzip.h"
2358 -#define STATIC
2359 -#endif /* !STATIC */
2360 +#define INBUFSIZ 4096
2361 +
2362 +#define WSIZE 0x8000 /* window size--must be a power of two, and */
2363 + /* at least 32K for zip's deflate method */
2364 +
2365 +static uint8_t *inbuf;
2366 +static uint8_t *window;
2367 +
2368 +static unsigned insize; /* valid bytes in inbuf */
2369 +static unsigned outcnt; /* bytes in output buffer */
2370 +static long bytes_out;
2371 +
2372 +/* --- */
2373 +
2374 +static unsigned inptr; /* index of next byte to be processed in inbuf */
2375 +
2376 +/* --- */
2377 +
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.
2382 + */
2383 +static int INIT fill_inbuf(void)
2384 +{
2385 + insize = fill_cb(inbuf, INBUFSIZ);
2386 + if (insize <= 0) {
2387 + error("RAMDISK: ran out of compressed data");
2388 + return -1;
2389 + }
2390 +
2391 + inptr = 1;
2392 +
2393 + return inbuf[0];
2394 +}
2395 +
2396 +#define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
2397 +
2398 +/* Diagnostic functions (stubbed out) */
2399 +#define Assert(cond, msg)
2400 +#define Trace(x)
2401 +#define Tracev(x)
2402 +#define Tracevv(x)
2403 +#define Tracec(c, x)
2404 +#define Tracecv(c, x)
2405
2406 -#ifndef INIT
2407 -#define INIT
2408 +static void flush_window(void);
2409 +/* End stuff copied from initramfs */
2410 #endif
2411 -
2412 +
2413 #define slide window
2414
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. */
2419 struct huft {
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 */
2424 union {
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 */
2428 } v;
2429 };
2430 @@ -144,7 +202,7 @@ struct huft {
2431
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 */
2449 #define wp outcnt
2450 #define flush_output(w) (wp=(w),flush_window())
2451
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,
2474 12, 12, 13, 13};
2475 @@ -217,59 +275,21 @@ static const ush cpdext[] = { /*
2476 the stream.
2477 */
2478
2479 -STATIC ulg bb; /* bit buffer */
2480 +STATIC uint32_t bb; /* bit buffer */
2481 STATIC unsigned bk; /* bits in bit buffer */
2482
2483 -STATIC const ush mask_bits[] = {
2484 +STATIC const uint16_t mask_bits[] = {
2485 0x0000,
2486 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
2487 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
2488 };
2489
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; \
2493 + (uint8_t)v; })
2494 +#define NEEDBITS(n) {while (k < (n)) \
2495 + {b |= ((uint32_t)NEXTBYTE())<<k; k += 8; } }
2496 #define DUMPBITS(n) {b>>=(n);k-=(n);}
2497
2498 -#ifndef NO_INFLATE_MALLOC
2499 -/* A trivial malloc implementation, adapted from
2500 - * malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994
2501 - */
2502 -
2503 -static unsigned long malloc_ptr;
2504 -static int malloc_count;
2505 -
2506 -static void *malloc(int size)
2507 -{
2508 - void *p;
2509 -
2510 - if (size < 0)
2511 - error("Malloc error");
2512 - if (!malloc_ptr)
2513 - malloc_ptr = free_mem_ptr;
2514 -
2515 - malloc_ptr = (malloc_ptr + 3) & ~3; /* Align */
2516 -
2517 - p = (void *)malloc_ptr;
2518 - malloc_ptr += size;
2519 -
2520 - if (free_mem_end_ptr && malloc_ptr >= free_mem_end_ptr)
2521 - error("Out of memory");
2522 -
2523 - malloc_count++;
2524 - return p;
2525 -}
2526 -
2527 -static void free(void *where)
2528 -{
2529 - malloc_count--;
2530 - if (!malloc_count)
2531 - malloc_ptr = free_mem_ptr;
2532 -}
2533 -#else
2534 -#define malloc(a) kmalloc(a, GFP_KERNEL)
2535 -#define free(a) kfree(a)
2536 -#endif
2537 -
2538 /*
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 */
2543
2544
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 */
2549
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 */
2560 )
2561 @@ -500,8 +520,8 @@ DEBG1("5 ");
2562 if (h)
2563 {
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 ");
2573 DEBG("h6c ");
2574
2575 /* set up table entry in r */
2576 - r.b = (uch)(k - w);
2577 + r.b = (uint8_t)(k - w);
2578 if (p >= v + n)
2579 r.e = 99; /* out of values--invalid code */
2580 else if (*p < s)
2581 {
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++ */
2587 }
2588 else
2589 {
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];
2593 }
2594 DEBG("h6d ");
2595 @@ -592,11 +612,12 @@ STATIC int INIT inflate_codes(
2596 Return an error code or zero if it all goes ok. */
2597 {
2598 register unsigned e; /* table entry flag/number of extra bits */
2599 - unsigned n, d; /* length and index for copy */
2600 + unsigned n;
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 */
2608
2609
2610 @@ -622,7 +643,7 @@ STATIC int INIT inflate_codes(
2611 DUMPBITS(t->b)
2612 if (e == 16) /* then it's a literal */
2613 {
2614 - slide[w++] = (uch)t->v.n;
2615 + slide[w++] = (uint8_t)t->v.n;
2616 Tracevv((stderr, "%c", slide[w-1]));
2617 if (w == WSIZE)
2618 {
2619 @@ -659,11 +680,25 @@ STATIC int INIT inflate_codes(
2620
2621 /* do the copy */
2622 do {
2623 - n -= (e = (e = WSIZE - ((d &= WSIZE-1) > w ? d : w)) > n ? n : e);
2624 +#ifdef NEW_CODE
2625 + if (flush_cb) {
2626 +#endif
2627 + /* Sliding window emulated using circular buffer:
2628 + * manage wrap-around */
2629 + e = WSIZE - ((d &= WSIZE-1) > w ? d : w);
2630 + if (e > n)
2631 + e = n;
2632 +#ifdef NEW_CODE
2633 + } else {
2634 + e = n;
2635 + }
2636 +#endif
2637 + n -= e;
2638 +
2639 #if !defined(NOMEMCPY) && !defined(DEBUG)
2640 if (w - d >= e) /* (this test assumes unsigned comparison) */
2641 {
2642 - memcpy(slide + w, slide + d, e);
2643 + memcpy(slide + w, slide + d, e);
2644 w += e;
2645 d += e;
2646 }
2647 @@ -673,9 +708,8 @@ STATIC int INIT inflate_codes(
2648 slide[w++] = slide[d++];
2649 Tracevv((stderr, "%c", slide[w-1]));
2650 } while (--e);
2651 - if (w == WSIZE)
2652 - {
2653 - flush_output(w);
2654 + if (w == WSIZE) {
2655 + flush_output(w);
2656 w = 0;
2657 }
2658 } while (n);
2659 @@ -702,7 +736,7 @@ STATIC int INIT inflate_stored(void)
2660 {
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 */
2666
2667 DEBG("<stor");
2668 @@ -732,7 +766,7 @@ DEBG("<stor");
2669 while (n--)
2670 {
2671 NEEDBITS(8)
2672 - slide[w++] = (uch)b;
2673 + slide[w++] = (uint8_t)b;
2674 if (w == WSIZE)
2675 {
2676 flush_output(w);
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 */
2684 int ret;
2685
2686 @@ -1033,7 +1067,7 @@ STATIC int INIT inflate_block(
2687 /* decompress an inflated block */
2688 {
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 */
2693
2694 DEBG("<blk");
2695 @@ -1130,8 +1164,8 @@ STATIC int INIT inflate(void)
2696 *
2697 **********************************************************************/
2698
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)
2704
2705 /*
2706 @@ -1172,7 +1206,7 @@ makecrc(void)
2707 }
2708
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 */
2712 }
2713
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 */
2718
2719 +#ifdef NEW_CODE
2720 +/* ===========================================================================
2721 + * Write the output window window[0..outcnt-1] and update crc and bytes_out.
2722 + * (Used for the decompressed data only.)
2723 + */
2724 +static void INIT flush_window(void)
2725 +{
2726 + uint32_t c = crc; /* temporary variable */
2727 + unsigned n;
2728 + uint8_t *in, ch;
2729 +
2730 + in = window;
2731 + for (n = 0; n < outcnt; n++) {
2732 + ch = *in++;
2733 + c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
2734 + }
2735 + crc = c;
2736 + bytes_out += (uint32_t)outcnt;
2737 + if (flush_cb != NULL)
2738 + flush_cb(window, outcnt); /* TODO: handle unzip_error */
2739 + else
2740 + window += outcnt;
2741 + outcnt = 0;
2742 +}
2743 +
2744 +static int empty_fill(void *buf, unsigned int len)
2745 +{
2746 + return 0;
2747 +}
2748 +#endif
2749 +
2750 +
2751 /*
2752 * Do the uncompression!
2753 */
2754 -static int INIT gunzip(void)
2755 +STATIC int INIT gunzip(
2756 +#ifdef NEW_CODE
2757 + unsigned char *buf, int len,
2758 + int(*fill)(void*, unsigned int),
2759 + int(*flush)(void*, unsigned int),
2760 + unsigned char *output,
2761 + int *posp,
2762 + void(*error_fn)(char *x)
2763 +#else
2764 + void
2765 +#endif
2766 + )
2767 {
2768 - uch flags;
2769 + uint8_t flags;
2770 unsigned char magic[2]; /* magic header */
2771 char method;
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 */
2776 int res;
2777
2778 +#ifdef NEW_CODE
2779 + set_error_fn(error_fn);
2780 + if (fill == NULL)
2781 + fill_cb = empty_fill;
2782 + else
2783 + fill_cb = fill;
2784 + if (output)
2785 + window = output;
2786 + else {
2787 + window = malloc(0x8000);
2788 + if (!window)
2789 + panic("can't allocate buffers");
2790 + flush_cb = flush;
2791 + }
2792 +
2793 + insize = len;
2794 + if (buf)
2795 + inbuf = buf;
2796 + else
2797 + inbuf = malloc(INBUFSIZ);
2798 +#endif
2799 +
2800 + inptr = 0;
2801 + outcnt = 0; /* bytes in output buffer */
2802 + bytes_out = 0;
2803 + crc = (uint32_t)0xffffffffL; /* shift register contents */
2804 + makecrc();
2805 +
2806 magic[0] = NEXTBYTE();
2807 magic[1] = NEXTBYTE();
2808 method = NEXTBYTE();
2809 @@ -1212,7 +1317,7 @@ static int INIT gunzip(void)
2810 return -1;
2811 }
2812
2813 - flags = (uch)get_byte();
2814 + flags = (uint8_t)get_byte();
2815 if ((flags & ENCRYPTED) != 0) {
2816 error("Input is encrypted");
2817 return -1;
2818 @@ -1277,15 +1382,15 @@ static int INIT gunzip(void)
2819 /* crc32 (see algorithm.doc)
2820 * uncompressed input size modulo 2^32
2821 */
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;
2830
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;
2839
2840 /* Validate decompression */
2841 if (orig_crc != CRC_VALUE) {
2842 @@ -1296,11 +1401,22 @@ static int INIT gunzip(void)
2843 error("length error");
2844 return -1;
2845 }
2846 +#ifdef NEW_CODE
2847 + if (!output)
2848 + free(window);
2849 + if (posp)
2850 + *posp = inptr;
2851 +#endif
2852 return 0;
2853
2854 underrun: /* NEXTBYTE() goto's here if needed */
2855 + free(window);
2856 +#ifdef NEW_CODE
2857 + if (!buf)
2858 + free(inbuf);
2859 +#endif
2860 error("out of input data");
2861 return -1;
2862 }
2863
2864 -
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 < $< > $@
2870
2871
2872 +# Bzip2
2873 +# ---------------------------------------------------------------------------
2874 +
2875 +# Bzip2 does not include size in file... so we have to fake that
2876 +size_append=$(CONFIG_SHELL) $(srctree)/scripts/bin_size
2877 +
2878 +quiet_cmd_bzip2 = BZIP2 $@
2879 +cmd_bzip2 = (bzip2 -9 < $< ; $(size_append) $<) > $@ || (rm -f $@ ; false)
2880 +
2881 +# Lzma
2882 +# ---------------------------------------------------------------------------
2883 +
2884 +quiet_cmd_lzma = LZMA $@
2885 +cmd_lzma = (/usr/bin/lzma -9 -c $< ; $(size_append) $<) >$@ || (rm -f $@ ; false)
2886 --- /dev/null
2887 +++ b/scripts/bin_size
2888 @@ -0,0 +1,10 @@
2889 +#!/bin/sh
2890 +
2891 +if [ $# = 0 ] ; then
2892 + echo Usage: $0 file
2893 +fi
2894 +
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
This page took 0.233266 seconds and 5 git commands to generate.