[brcm63xx] register GPIO-connected LEDs for known boards
[openwrt.git] / target / linux / rdc / patches-2.6.28 / 006-bzip2_lzma_x86.patch
1 --- a/arch/arm/boot/compressed/Makefile
2 +++ b/arch/arm/boot/compressed/Makefile
3 @@ -67,8 +67,15 @@ endif
4
5 SEDFLAGS = s/TEXT_START/$(ZTEXTADDR)/;s/BSS_START/$(ZBSSADDR)/
6
7 -targets := vmlinux vmlinux.lds piggy.gz piggy.o font.o font.c \
8 - head.o misc.o $(OBJS)
9 +suffix_$(CONFIG_KERNEL_GZIP) = gz
10 +suffix_$(CONFIG_KERNEL_BZIP2) = bz2
11 +suffix_$(CONFIG_KERNEL_LZMA) = lzma
12 +
13 +targets := vmlinux vmlinux.lds \
14 + piggy.gz piggy.gz.o \
15 + piggy.bz2 piggy.bz2.o \
16 + piggy.lzma piggy.lzma.o \
17 + font.o font.c head.o misc.o $(OBJS)
18
19 ifeq ($(CONFIG_FUNCTION_TRACER),y)
20 ORIG_CFLAGS := $(KBUILD_CFLAGS)
21 @@ -95,7 +102,7 @@ LDFLAGS_vmlinux += -p --no-undefined -X
22 # would otherwise mess up our GOT table
23 CFLAGS_misc.o := -Dstatic=
24
25 -$(obj)/vmlinux: $(obj)/vmlinux.lds $(obj)/$(HEAD) $(obj)/piggy.o \
26 +$(obj)/vmlinux: $(obj)/vmlinux.lds $(obj)/$(HEAD) $(obj)/piggy.$(suffix_y).o \
27 $(addprefix $(obj)/, $(OBJS)) FORCE
28 $(call if_changed,ld)
29 @:
30 @@ -103,7 +110,17 @@ $(obj)/vmlinux: $(obj)/vmlinux.lds $(obj
31 $(obj)/piggy.gz: $(obj)/../Image FORCE
32 $(call if_changed,gzip)
33
34 -$(obj)/piggy.o: $(obj)/piggy.gz FORCE
35 +$(obj)/piggy.bz2: $(obj)/../Image FORCE
36 + $(call if_changed,bzip2)
37 +
38 +$(obj)/piggy.lzma: $(obj)/../Image FORCE
39 + $(call if_changed,lzma)
40 +
41 +$(obj)/piggy.gz.o: $(obj)/piggy.gz FORCE
42 +
43 +$(obj)/piggy.bz2.o: $(obj)/piggy.bz2 FORCE
44 +
45 +$(obj)/piggy.lzma.o: $(obj)/piggy.lzma FORCE
46
47 CFLAGS_font.o := -Dstatic=
48
49 --- a/arch/arm/boot/compressed/misc.c
50 +++ b/arch/arm/boot/compressed/misc.c
51 @@ -169,116 +169,34 @@ static inline __ptr_t memcpy(__ptr_t __d
52 /*
53 * gzip delarations
54 */
55 -#define OF(args) args
56 #define STATIC static
57
58 -typedef unsigned char uch;
59 -typedef unsigned short ush;
60 typedef unsigned long ulg;
61
62 -#define WSIZE 0x8000 /* Window size must be at least 32k, */
63 - /* and a power of two */
64 -
65 -static uch *inbuf; /* input buffer */
66 -static uch window[WSIZE]; /* Sliding window buffer */
67 -
68 -static unsigned insize; /* valid bytes in inbuf */
69 -static unsigned inptr; /* index of next byte to be processed in inbuf */
70 -static unsigned outcnt; /* bytes in output buffer */
71 -
72 -/* gzip flag byte */
73 -#define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
74 -#define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
75 -#define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
76 -#define ORIG_NAME 0x08 /* bit 3 set: original file name present */
77 -#define COMMENT 0x10 /* bit 4 set: file comment present */
78 -#define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
79 -#define RESERVED 0xC0 /* bit 6,7: reserved */
80 -
81 -#define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
82 -
83 -/* Diagnostic functions */
84 -#ifdef DEBUG
85 -# define Assert(cond,msg) {if(!(cond)) error(msg);}
86 -# define Trace(x) fprintf x
87 -# define Tracev(x) {if (verbose) fprintf x ;}
88 -# define Tracevv(x) {if (verbose>1) fprintf x ;}
89 -# define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
90 -# define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
91 -#else
92 -# define Assert(cond,msg)
93 -# define Trace(x)
94 -# define Tracev(x)
95 -# define Tracevv(x)
96 -# define Tracec(c,x)
97 -# define Tracecv(c,x)
98 -#endif
99 -
100 -static int fill_inbuf(void);
101 -static void flush_window(void);
102 -static void error(char *m);
103 -
104 extern char input_data[];
105 extern char input_data_end[];
106
107 -static uch *output_data;
108 -static ulg output_ptr;
109 -static ulg bytes_out;
110 -
111 static void error(char *m);
112
113 -static void putstr(const char *);
114 -
115 -extern int end;
116 static ulg free_mem_ptr;
117 static ulg free_mem_end_ptr;
118
119 -#ifdef STANDALONE_DEBUG
120 -#define NO_INFLATE_MALLOC
121 -#endif
122 -
123 #define ARCH_HAS_DECOMP_WDOG
124 +#define NEW_CODE
125
126 +#ifdef CONFIG_KERNEL_GZIP
127 #include "../../../../lib/inflate.c"
128 +#endif
129
130 -/* ===========================================================================
131 - * Fill the input buffer. This is called only when the buffer is empty
132 - * and at least one byte is really needed.
133 - */
134 -int fill_inbuf(void)
135 -{
136 - if (insize != 0)
137 - error("ran out of input data");
138 +#ifdef CONFIG_KERNEL_BZIP2
139 +#include "../../../../lib/decompress_bunzip2.c"
140 +#endif
141
142 - inbuf = input_data;
143 - insize = &input_data_end[0] - &input_data[0];
144 +#ifdef CONFIG_KERNEL_LZMA
145 +#include "../../../../lib/decompress_unlzma.c"
146 +#endif
147
148 - inptr = 1;
149 - return inbuf[0];
150 -}
151
152 -/* ===========================================================================
153 - * Write the output window window[0..outcnt-1] and update crc and bytes_out.
154 - * (Used for the decompressed data only.)
155 - */
156 -void flush_window(void)
157 -{
158 - ulg c = crc;
159 - unsigned n;
160 - uch *in, *out, ch;
161 -
162 - in = window;
163 - out = &output_data[output_ptr];
164 - for (n = 0; n < outcnt; n++) {
165 - ch = *out++ = *in++;
166 - c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
167 - }
168 - crc = c;
169 - bytes_out += (ulg)outcnt;
170 - output_ptr += (ulg)outcnt;
171 - outcnt = 0;
172 - putstr(".");
173 -}
174
175 #ifndef arch_error
176 #define arch_error(x)
177 @@ -301,16 +219,24 @@ ulg
178 decompress_kernel(ulg output_start, ulg free_mem_ptr_p, ulg free_mem_ptr_end_p,
179 int arch_id)
180 {
181 - output_data = (uch *)output_start; /* Points to kernel start */
182 - free_mem_ptr = free_mem_ptr_p;
183 - free_mem_end_ptr = free_mem_ptr_end_p;
184 + ulg output_ptr;
185 + ulg *ptr;
186 + size_t input_len = input_data_end - input_data;
187 + size_t pos = 0;
188 +
189 __machine_arch_type = arch_id;
190
191 arch_decomp_setup();
192
193 - makecrc();
194 - putstr("Uncompressing Linux...");
195 - gunzip();
196 + ptr = (ulg *) (((long)input_data_end) - 4);
197 + output_ptr = output_start + *ptr;
198 +
199 + free_mem_ptr = output_ptr;
200 + free_mem_end_ptr = output_ptr + 0x4000000;
201 +
202 + putstr("Decompressing Linux...");
203 + decompress(input_data, input_len,
204 + NULL, NULL, (unsigned char *) output_start, &pos, error);
205 putstr(" done, booting the kernel.\n");
206 return output_ptr;
207 }
208 @@ -320,11 +246,8 @@ char output_buffer[1500*1024];
209
210 int main()
211 {
212 - output_data = output_buffer;
213 -
214 - makecrc();
215 putstr("Uncompressing Linux...");
216 - gunzip();
217 + decompress(input_data, input_len, NULL, output_buffer, NULL);
218 putstr("done.\n");
219 return 0;
220 }
221 --- a/arch/x86/boot/compressed/Makefile
222 +++ b/arch/x86/boot/compressed/Makefile
223 @@ -4,7 +4,7 @@
224 # create a compressed vmlinux image from the original vmlinux
225 #
226
227 -targets := vmlinux vmlinux.bin vmlinux.bin.gz head_$(BITS).o misc.o piggy.o
228 +targets := vmlinux vmlinux.bin vmlinux.bin.gz vmlinux.bin.bz2 vmlinux.bin.lzma head_$(BITS).o misc.o piggy.o
229
230 KBUILD_CFLAGS := -m$(BITS) -D__KERNEL__ $(LINUX_INCLUDE) -O2
231 KBUILD_CFLAGS += -fno-strict-aliasing -fPIC
232 @@ -47,9 +47,17 @@ ifeq ($(CONFIG_X86_32),y)
233 ifdef CONFIG_RELOCATABLE
234 $(obj)/vmlinux.bin.gz: $(obj)/vmlinux.bin.all FORCE
235 $(call if_changed,gzip)
236 +$(obj)/vmlinux.bin.bz2: $(obj)/vmlinux.bin.all FORCE
237 + $(call if_changed,bzip2)
238 +$(obj)/vmlinux.bin.lzma: $(obj)/vmlinux.bin.all FORCE
239 + $(call if_changed,lzma)
240 else
241 $(obj)/vmlinux.bin.gz: $(obj)/vmlinux.bin FORCE
242 $(call if_changed,gzip)
243 +$(obj)/vmlinux.bin.bz2: $(obj)/vmlinux.bin FORCE
244 + $(call if_changed,bzip2)
245 +$(obj)/vmlinux.bin.lzma: $(obj)/vmlinux.bin FORCE
246 + $(call if_changed,lzma)
247 endif
248 LDFLAGS_piggy.o := -r --format binary --oformat elf32-i386 -T
249
250 @@ -60,5 +68,9 @@ $(obj)/vmlinux.bin.gz: $(obj)/vmlinux.bi
251 LDFLAGS_piggy.o := -r --format binary --oformat elf64-x86-64 -T
252 endif
253
254 -$(obj)/piggy.o: $(obj)/vmlinux.scr $(obj)/vmlinux.bin.gz FORCE
255 +suffix_$(CONFIG_KERNEL_GZIP) = gz
256 +suffix_$(CONFIG_KERNEL_BZIP2) = bz2
257 +suffix_$(CONFIG_KERNEL_LZMA) = lzma
258 +
259 +$(obj)/piggy.o: $(obj)/vmlinux.scr $(obj)/vmlinux.bin.$(suffix_y) FORCE
260 $(call if_changed,ld)
261 --- a/arch/x86/boot/compressed/misc.c
262 +++ b/arch/x86/boot/compressed/misc.c
263 @@ -116,71 +116,13 @@
264 /*
265 * gzip declarations
266 */
267 -
268 -#define OF(args) args
269 #define STATIC static
270
271 #undef memset
272 #undef memcpy
273 #define memzero(s, n) memset((s), 0, (n))
274
275 -typedef unsigned char uch;
276 -typedef unsigned short ush;
277 -typedef unsigned long ulg;
278 -
279 -/*
280 - * Window size must be at least 32k, and a power of two.
281 - * We don't actually have a window just a huge output buffer,
282 - * so we report a 2G window size, as that should always be
283 - * larger than our output buffer:
284 - */
285 -#define WSIZE 0x80000000
286
287 -/* Input buffer: */
288 -static unsigned char *inbuf;
289 -
290 -/* Sliding window buffer (and final output buffer): */
291 -static unsigned char *window;
292 -
293 -/* Valid bytes in inbuf: */
294 -static unsigned insize;
295 -
296 -/* Index of next byte to be processed in inbuf: */
297 -static unsigned inptr;
298 -
299 -/* Bytes in output buffer: */
300 -static unsigned outcnt;
301 -
302 -/* gzip flag byte */
303 -#define ASCII_FLAG 0x01 /* bit 0 set: file probably ASCII text */
304 -#define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gz file */
305 -#define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
306 -#define ORIG_NAM 0x08 /* bit 3 set: original file name present */
307 -#define COMMENT 0x10 /* bit 4 set: file comment present */
308 -#define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
309 -#define RESERVED 0xC0 /* bit 6, 7: reserved */
310 -
311 -#define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
312 -
313 -/* Diagnostic functions */
314 -#ifdef DEBUG
315 -# define Assert(cond, msg) do { if (!(cond)) error(msg); } while (0)
316 -# define Trace(x) do { fprintf x; } while (0)
317 -# define Tracev(x) do { if (verbose) fprintf x ; } while (0)
318 -# define Tracevv(x) do { if (verbose > 1) fprintf x ; } while (0)
319 -# define Tracec(c, x) do { if (verbose && (c)) fprintf x ; } while (0)
320 -# define Tracecv(c, x) do { if (verbose > 1 && (c)) fprintf x ; } while (0)
321 -#else
322 -# define Assert(cond, msg)
323 -# define Trace(x)
324 -# define Tracev(x)
325 -# define Tracevv(x)
326 -# define Tracec(c, x)
327 -# define Tracecv(c, x)
328 -#endif
329 -
330 -static int fill_inbuf(void);
331 -static void flush_window(void);
332 static void error(char *m);
333
334 /*
335 @@ -189,11 +131,6 @@ static void error(char *m);
336 static struct boot_params *real_mode; /* Pointer to real-mode data */
337 static int quiet;
338
339 -extern unsigned char input_data[];
340 -extern int input_len;
341 -
342 -static long bytes_out;
343 -
344 static void *memset(void *s, int c, unsigned n);
345 static void *memcpy(void *dest, const void *src, unsigned n);
346
347 @@ -213,7 +150,19 @@ static char *vidmem;
348 static int vidport;
349 static int lines, cols;
350
351 +#define NEW_CODE
352 +
353 +#ifdef CONFIG_KERNEL_GZIP
354 #include "../../../../lib/inflate.c"
355 +#endif
356 +
357 +#ifdef CONFIG_KERNEL_BZIP2
358 +#include "../../../../lib/decompress_bunzip2.c"
359 +#endif
360 +
361 +#ifdef CONFIG_KERNEL_LZMA
362 +#include "../../../../lib/decompress_unlzma.c"
363 +#endif
364
365 static void scroll(void)
366 {
367 @@ -293,38 +242,6 @@ static void *memcpy(void *dest, const vo
368 return dest;
369 }
370
371 -/* ===========================================================================
372 - * Fill the input buffer. This is called only when the buffer is empty
373 - * and at least one byte is really needed.
374 - */
375 -static int fill_inbuf(void)
376 -{
377 - error("ran out of input data");
378 - return 0;
379 -}
380 -
381 -/* ===========================================================================
382 - * Write the output window window[0..outcnt-1] and update crc and bytes_out.
383 - * (Used for the decompressed data only.)
384 - */
385 -static void flush_window(void)
386 -{
387 - /* With my window equal to my output buffer
388 - * I only need to compute the crc here.
389 - */
390 - unsigned long c = crc; /* temporary variable */
391 - unsigned n;
392 - unsigned char *in, ch;
393 -
394 - in = window;
395 - for (n = 0; n < outcnt; n++) {
396 - ch = *in++;
397 - c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
398 - }
399 - crc = c;
400 - bytes_out += (unsigned long)outcnt;
401 - outcnt = 0;
402 -}
403
404 static void error(char *x)
405 {
406 @@ -407,12 +324,8 @@ asmlinkage void decompress_kernel(void *
407 lines = real_mode->screen_info.orig_video_lines;
408 cols = real_mode->screen_info.orig_video_cols;
409
410 - window = output; /* Output buffer (Normally at 1M) */
411 free_mem_ptr = heap; /* Heap */
412 free_mem_end_ptr = heap + BOOT_HEAP_SIZE;
413 - inbuf = input_data; /* Input buffer */
414 - insize = input_len;
415 - inptr = 0;
416
417 #ifdef CONFIG_X86_64
418 if ((unsigned long)output & (__KERNEL_ALIGN - 1))
419 @@ -430,10 +343,9 @@ asmlinkage void decompress_kernel(void *
420 #endif
421 #endif
422
423 - makecrc();
424 if (!quiet)
425 putstr("\nDecompressing Linux... ");
426 - gunzip();
427 + decompress(input_data, input_len, NULL, NULL, output, NULL, error);
428 parse_elf(output);
429 if (!quiet)
430 putstr("done.\nBooting the kernel.\n");
431 --- a/arch/x86/include/asm/boot.h
432 +++ b/arch/x86/include/asm/boot.h
433 @@ -15,11 +15,21 @@
434 + (CONFIG_PHYSICAL_ALIGN - 1)) \
435 & ~(CONFIG_PHYSICAL_ALIGN - 1))
436
437 +#if (defined CONFIG_KERNEL_BZIP2)
438 +#define BOOT_HEAP_SIZE 0x400000
439 +#else
440 +
441 #ifdef CONFIG_X86_64
442 #define BOOT_HEAP_SIZE 0x7000
443 -#define BOOT_STACK_SIZE 0x4000
444 #else
445 #define BOOT_HEAP_SIZE 0x4000
446 +#endif
447 +
448 +#endif
449 +
450 +#ifdef CONFIG_X86_64
451 +#define BOOT_STACK_SIZE 0x4000
452 +#else
453 #define BOOT_STACK_SIZE 0x1000
454 #endif
455
456 --- a/drivers/block/Kconfig
457 +++ b/drivers/block/Kconfig
458 @@ -358,6 +358,30 @@ config BLK_DEV_XIP
459 will prevent RAM block device backing store memory from being
460 allocated from highmem (only a problem for highmem systems).
461
462 +config RD_BZIP2
463 + bool "Initial ramdisk compressed using bzip2"
464 + default n
465 + depends on BLK_DEV_INITRD=y
466 + help
467 + Support loading of a bzip2 encoded initial ramdisk or cpio buffer
468 + If unsure, say N.
469 +
470 +config RD_LZMA
471 + bool "Initial ramdisk compressed using lzma"
472 + default n
473 + depends on BLK_DEV_INITRD=y
474 + help
475 + Support loading of a lzma encoded initial ramdisk or cpio buffer
476 + If unsure, say N.
477 +
478 +config RD_GZIP
479 + bool "Initial ramdisk compressed using gzip"
480 + default y
481 + depends on BLK_DEV_INITRD=y
482 + help
483 + Support loading of a gzip encoded initial ramdisk or cpio buffer.
484 + If unsure, say Y.
485 +
486 config CDROM_PKTCDVD
487 tristate "Packet writing on CD/DVD media"
488 depends on !UML
489 --- /dev/null
490 +++ b/include/linux/decompress/bunzip2.h
491 @@ -0,0 +1,10 @@
492 +#ifndef DECOMPRESS_BUNZIP2_H
493 +#define DECOMPRESS_BUNZIP2_H
494 +
495 +int bunzip2(unsigned char *inbuf, int len,
496 + int(*fill)(void*, unsigned int),
497 + int(*flush)(void*, unsigned int),
498 + unsigned char *output,
499 + int *pos,
500 + void(*error)(char *x));
501 +#endif
502 --- /dev/null
503 +++ b/include/linux/decompress/generic.h
504 @@ -0,0 +1,30 @@
505 +#ifndef DECOMPRESS_GENERIC_H
506 +#define DECOMPRESS_GENERIC_H
507 +
508 +/* Minimal chunksize to be read.
509 + *Bzip2 prefers at least 4096
510 + *Lzma prefers 0x10000 */
511 +#define COMPR_IOBUF_SIZE 4096
512 +
513 +typedef int (*decompress_fn) (unsigned char *inbuf, int len,
514 + int(*fill)(void*, unsigned int),
515 + int(*writebb)(void*, unsigned int),
516 + unsigned char *output,
517 + int *posp,
518 + void(*error)(char *x));
519 +
520 +/* inbuf - input buffer
521 + *len - len of pre-read data in inbuf
522 + *fill - function to fill inbuf if empty
523 + *writebb - function to write out outbug
524 + *posp - if non-null, input position (number of bytes read) will be
525 + * returned here
526 + *
527 + *If len != 0, the inbuf is initialized (with as much data), and fill
528 + *should not be called
529 + *If len = 0, the inbuf is allocated, but empty. Its size is IOBUF_SIZE
530 + *fill should be called (repeatedly...) to read data, at most IOBUF_SIZE
531 + */
532 +
533 +
534 +#endif
535 --- /dev/null
536 +++ b/include/linux/decompress/inflate.h
537 @@ -0,0 +1,13 @@
538 +#ifndef INFLATE_H
539 +#define INFLATE_H
540 +
541 +/* Other housekeeping constants */
542 +#define INBUFSIZ 4096
543 +
544 +int gunzip(unsigned char *inbuf, int len,
545 + int(*fill)(void*, unsigned int),
546 + int(*flush)(void*, unsigned int),
547 + unsigned char *output,
548 + int *pos,
549 + void(*error_fn)(char *x));
550 +#endif
551 --- /dev/null
552 +++ b/include/linux/decompress/mm.h
553 @@ -0,0 +1,89 @@
554 +/*
555 + * linux/compr_mm.h
556 + *
557 + * Memory management for pre-boot and ramdisk uncompressors
558 + *
559 + * Authors: Alain Knaff <alain@knaff.lu>
560 + *
561 + */
562 +
563 +#ifndef DECOMPR_MM_H
564 +#define DECOMPR_MM_H
565 +
566 +#ifdef STATIC
567 +
568 +/* Code active when included from pre-boot environment: */
569 +
570 +/* A trivial malloc implementation, adapted from
571 + * malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994
572 + */
573 +static unsigned long malloc_ptr;
574 +static int malloc_count;
575 +
576 +static void *malloc(int size)
577 +{
578 + void *p;
579 +
580 + if (size < 0)
581 + error("Malloc error");
582 + if (!malloc_ptr)
583 + malloc_ptr = free_mem_ptr;
584 +
585 + malloc_ptr = (malloc_ptr + 3) & ~3; /* Align */
586 +
587 + p = (void *)malloc_ptr;
588 + malloc_ptr += size;
589 +
590 + if (free_mem_end_ptr && malloc_ptr >= free_mem_end_ptr)
591 + error("Out of memory");
592 +
593 + malloc_count++;
594 + return p;
595 +}
596 +
597 +static void free(void *where)
598 +{
599 + malloc_count--;
600 + if (!malloc_count)
601 + malloc_ptr = free_mem_ptr;
602 +}
603 +
604 +#define large_malloc(a) malloc(a)
605 +#define large_free(a) free(a)
606 +
607 +#define set_error_fn(x)
608 +#define panic error
609 +
610 +#define INIT
611 +
612 +#else /* STATIC */
613 +
614 +/* Code active when compiled standalone for use when loading ramdisk: */
615 +
616 +#include <linux/kernel.h>
617 +#include <linux/fs.h>
618 +#include <linux/string.h>
619 +#include <linux/vmalloc.h>
620 +
621 +/* Use defines rather than static inline in order to avoid spurious
622 + * warnings when not needed (indeed large_malloc / large_free are not
623 + * needed by inflate */
624 +
625 +#define malloc(a) kmalloc(a, GFP_KERNEL)
626 +#define free(a) kfree(a)
627 +
628 +#define large_malloc(a) vmalloc(a)
629 +#define large_free(a) vfree(a)
630 +
631 +static void(*error)(char *m);
632 +#define set_error_fn(x) error = x;
633 +#define NEW_CODE
634 +
635 +#define INIT __init
636 +#define STATIC
637 +
638 +#include <linux/init.h>
639 +
640 +#endif /* STATIC */
641 +
642 +#endif /* DECOMPR_MM_H */
643 --- /dev/null
644 +++ b/include/linux/decompress/unlzma.h
645 @@ -0,0 +1,12 @@
646 +#ifndef DECOMPRESS_UNLZMA_H
647 +#define DECOMPRESS_UNLZMA_H
648 +
649 +int unlzma(unsigned char *, int,
650 + int(*fill)(void*, unsigned int),
651 + int(*flush)(void*, unsigned int),
652 + unsigned char *output,
653 + int *posp,
654 + void(*error)(char *x)
655 + );
656 +
657 +#endif
658 --- a/init/Kconfig
659 +++ b/init/Kconfig
660 @@ -101,6 +101,56 @@ config LOCALVERSION_AUTO
661
662 which is done within the script "scripts/setlocalversion".)
663
664 +choice
665 + prompt "Kernel compression mode"
666 + default KERNEL_GZIP
667 + help
668 + The linux kernel is a kind of self-extracting executable.
669 + Several compression algorithms are available, which differ
670 + in efficiency, compression and decompression speed.
671 + Compression speed is only relevant when building a kernel.
672 + Decompression speed is relevant at each boot.
673 +
674 + If you have any problems with bzip2 or lzma compressed
675 + kernels, mail me (Alain Knaff) <alain@knaff.lu>. (An older
676 + version of this functionality (bzip2 only), for 2.4, was
677 + supplied by Christian Ludwig)
678 +
679 + High compression options are mostly useful for users, who
680 + are low on disk space (embedded systems), but for whom ram
681 + size matters less.
682 +
683 + If in doubt, select 'gzip'
684 +
685 +config KERNEL_GZIP
686 + bool "Gzip"
687 + help
688 + The old and tried gzip compression. Its compression ratio is
689 + the poorest among the 3 choices; however its speed (both
690 + compression and decompression) is the fastest.
691 +
692 +config KERNEL_BZIP2
693 + bool "Bzip2"
694 + help
695 + Its compression ratio and speed is intermediate.
696 + Decompression speed is slowest among the 3.
697 + The kernel size is about 10 per cent smaller with bzip2,
698 + in comparison to gzip.
699 + Bzip2 uses a large amount of memory. For modern kernels
700 + you will need at least 8MB RAM or more for booting.
701 +
702 +config KERNEL_LZMA
703 + bool "LZMA"
704 + help
705 + The most recent compression algorithm.
706 + Its ratio is best, decompression speed is between the other
707 + 2. Compression is slowest.
708 + The kernel size is about 33 per cent smaller with lzma,
709 + in comparison to gzip.
710 +
711 +endchoice
712 +
713 +
714 config SWAP
715 bool "Support for paging of anonymous memory (swap)"
716 depends on MMU && BLOCK
717 --- a/init/do_mounts_rd.c
718 +++ b/init/do_mounts_rd.c
719 @@ -11,6 +11,12 @@
720
721 #include "do_mounts.h"
722
723 +#include <linux/decompress/generic.h>
724 +
725 +#include <linux/decompress/bunzip2.h>
726 +#include <linux/decompress/unlzma.h>
727 +#include <linux/decompress/inflate.h>
728 +
729 int __initdata rd_prompt = 1;/* 1 = prompt for RAM disk, 0 = don't prompt */
730
731 static int __init prompt_ramdisk(char *str)
732 @@ -29,7 +35,7 @@ static int __init ramdisk_start_setup(ch
733 }
734 __setup("ramdisk_start=", ramdisk_start_setup);
735
736 -static int __init crd_load(int in_fd, int out_fd);
737 +static int __init crd_load(int in_fd, int out_fd, decompress_fn deco);
738
739 /*
740 * This routine tries to find a RAM disk image to load, and returns the
741 @@ -46,7 +52,7 @@ static int __init crd_load(int in_fd, in
742 * gzip
743 */
744 static int __init
745 -identify_ramdisk_image(int fd, int start_block)
746 +identify_ramdisk_image(int fd, int start_block, decompress_fn *decompressor)
747 {
748 const int size = 512;
749 struct minix_super_block *minixsb;
750 @@ -74,6 +80,7 @@ identify_ramdisk_image(int fd, int start
751 sys_lseek(fd, start_block * BLOCK_SIZE, 0);
752 sys_read(fd, buf, size);
753
754 +#ifdef CONFIG_RD_GZIP
755 /*
756 * If it matches the gzip magic numbers, return 0
757 */
758 @@ -81,9 +88,39 @@ identify_ramdisk_image(int fd, int start
759 printk(KERN_NOTICE
760 "RAMDISK: Compressed image found at block %d\n",
761 start_block);
762 + *decompressor = gunzip;
763 + nblocks = 0;
764 + goto done;
765 + }
766 +#endif
767 +
768 +#ifdef CONFIG_RD_BZIP2
769 + /*
770 + * If it matches the bzip2 magic numbers, return -1
771 + */
772 + if (buf[0] == 0x42 && (buf[1] == 0x5a)) {
773 + printk(KERN_NOTICE
774 + "RAMDISK: Bzipped image found at block %d\n",
775 + start_block);
776 + *decompressor = bunzip2;
777 nblocks = 0;
778 goto done;
779 }
780 +#endif
781 +
782 +#ifdef CONFIG_RD_LZMA
783 + /*
784 + * If it matches the lzma magic numbers, return -1
785 + */
786 + if (buf[0] == 0x5d && (buf[1] == 0x00)) {
787 + printk(KERN_NOTICE
788 + "RAMDISK: Lzma image found at block %d\n",
789 + start_block);
790 + *decompressor = unlzma;
791 + nblocks = 0;
792 + goto done;
793 + }
794 +#endif
795
796 /* romfs is at block zero too */
797 if (romfsb->word0 == ROMSB_WORD0 &&
798 @@ -156,6 +193,7 @@ int __init rd_load_image(char *from)
799 int nblocks, i, disk;
800 char *buf = NULL;
801 unsigned short rotate = 0;
802 + decompress_fn decompressor = NULL;
803 #if !defined(CONFIG_S390) && !defined(CONFIG_PPC_ISERIES)
804 char rotator[4] = { '|' , '/' , '-' , '\\' };
805 #endif
806 @@ -168,12 +206,12 @@ int __init rd_load_image(char *from)
807 if (in_fd < 0)
808 goto noclose_input;
809
810 - nblocks = identify_ramdisk_image(in_fd, rd_image_start);
811 + nblocks = identify_ramdisk_image(in_fd, rd_image_start, &decompressor);
812 if (nblocks < 0)
813 goto done;
814
815 if (nblocks == 0) {
816 - if (crd_load(in_fd, out_fd) == 0)
817 + if (crd_load(in_fd, out_fd, decompressor) == 0)
818 goto successful_load;
819 goto done;
820 }
821 @@ -272,138 +310,48 @@ int __init rd_load_disk(int n)
822 return rd_load_image("/dev/root");
823 }
824
825 -/*
826 - * gzip declarations
827 - */
828 -
829 -#define OF(args) args
830 -
831 -#ifndef memzero
832 -#define memzero(s, n) memset ((s), 0, (n))
833 -#endif
834 -
835 -typedef unsigned char uch;
836 -typedef unsigned short ush;
837 -typedef unsigned long ulg;
838 -
839 -#define INBUFSIZ 4096
840 -#define WSIZE 0x8000 /* window size--must be a power of two, and */
841 - /* at least 32K for zip's deflate method */
842 -
843 -static uch *inbuf;
844 -static uch *window;
845 -
846 -static unsigned insize; /* valid bytes in inbuf */
847 -static unsigned inptr; /* index of next byte to be processed in inbuf */
848 -static unsigned outcnt; /* bytes in output buffer */
849 static int exit_code;
850 -static int unzip_error;
851 -static long bytes_out;
852 +static int decompress_error;
853 static int crd_infd, crd_outfd;
854
855 -#define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
856 -
857 -/* Diagnostic functions (stubbed out) */
858 -#define Assert(cond,msg)
859 -#define Trace(x)
860 -#define Tracev(x)
861 -#define Tracevv(x)
862 -#define Tracec(c,x)
863 -#define Tracecv(c,x)
864 -
865 -#define STATIC static
866 -#define INIT __init
867 -
868 -static int __init fill_inbuf(void);
869 -static void __init flush_window(void);
870 -static void __init error(char *m);
871 -
872 -#define NO_INFLATE_MALLOC
873 -
874 -#include "../lib/inflate.c"
875 -
876 -/* ===========================================================================
877 - * Fill the input buffer. This is called only when the buffer is empty
878 - * and at least one byte is really needed.
879 - * Returning -1 does not guarantee that gunzip() will ever return.
880 - */
881 -static int __init fill_inbuf(void)
882 +static int __init compr_fill(void *buf, unsigned int len)
883 {
884 - if (exit_code) return -1;
885 -
886 - insize = sys_read(crd_infd, inbuf, INBUFSIZ);
887 - if (insize == 0) {
888 - error("RAMDISK: ran out of compressed data");
889 - return -1;
890 - }
891 -
892 - inptr = 1;
893 -
894 - return inbuf[0];
895 + int r = sys_read(crd_infd, buf, len);
896 + if (r < 0)
897 + printk(KERN_ERR "RAMDISK: error while reading compressed data");
898 + else if (r == 0)
899 + printk(KERN_ERR "RAMDISK: EOF while reading compressed data");
900 + return r;
901 }
902
903 -/* ===========================================================================
904 - * Write the output window window[0..outcnt-1] and update crc and bytes_out.
905 - * (Used for the decompressed data only.)
906 - */
907 -static void __init flush_window(void)
908 +static int __init compr_flush(void *window, unsigned int outcnt)
909 {
910 - ulg c = crc; /* temporary variable */
911 - unsigned n, written;
912 - uch *in, ch;
913 -
914 - written = sys_write(crd_outfd, window, outcnt);
915 - if (written != outcnt && unzip_error == 0) {
916 - printk(KERN_ERR "RAMDISK: incomplete write (%d != %d) %ld\n",
917 - written, outcnt, bytes_out);
918 - unzip_error = 1;
919 - }
920 - in = window;
921 - for (n = 0; n < outcnt; n++) {
922 - ch = *in++;
923 - c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
924 - }
925 - crc = c;
926 - bytes_out += (ulg)outcnt;
927 - outcnt = 0;
928 + int written = sys_write(crd_outfd, window, outcnt);
929 + if (written != outcnt) {
930 + if (decompress_error == 0)
931 + printk(KERN_ERR
932 + "RAMDISK: incomplete write (%d != %d)\n",
933 + written, outcnt);
934 + decompress_error = 1;
935 + return -1;
936 + }
937 + return outcnt;
938 }
939
940 static void __init error(char *x)
941 {
942 printk(KERN_ERR "%s\n", x);
943 exit_code = 1;
944 - unzip_error = 1;
945 + decompress_error = 1;
946 }
947
948 -static int __init crd_load(int in_fd, int out_fd)
949 +static int __init crd_load(int in_fd, int out_fd, decompress_fn deco)
950 {
951 int result;
952 -
953 - insize = 0; /* valid bytes in inbuf */
954 - inptr = 0; /* index of next byte to be processed in inbuf */
955 - outcnt = 0; /* bytes in output buffer */
956 - exit_code = 0;
957 - bytes_out = 0;
958 - crc = (ulg)0xffffffffL; /* shift register contents */
959 -
960 crd_infd = in_fd;
961 crd_outfd = out_fd;
962 - inbuf = kmalloc(INBUFSIZ, GFP_KERNEL);
963 - if (!inbuf) {
964 - printk(KERN_ERR "RAMDISK: Couldn't allocate gzip buffer\n");
965 - return -1;
966 - }
967 - window = kmalloc(WSIZE, GFP_KERNEL);
968 - if (!window) {
969 - printk(KERN_ERR "RAMDISK: Couldn't allocate gzip window\n");
970 - kfree(inbuf);
971 - return -1;
972 - }
973 - makecrc();
974 - result = gunzip();
975 - if (unzip_error)
976 + result = deco(NULL, 0, compr_fill, compr_flush, NULL, NULL, error);
977 + if (decompress_error)
978 result = 1;
979 - kfree(inbuf);
980 - kfree(window);
981 return result;
982 }
983 --- a/init/initramfs.c
984 +++ b/init/initramfs.c
985 @@ -389,11 +389,14 @@ static int __init write_buffer(char *buf
986 return len - count;
987 }
988
989 -static void __init flush_buffer(char *buf, unsigned len)
990 +
991 +static int __init flush_buffer(void *bufv, unsigned len)
992 {
993 + char *buf = (char *) bufv;
994 int written;
995 + int origLen = len;
996 if (message)
997 - return;
998 + return -1;
999 while ((written = write_buffer(buf, len)) < len && !message) {
1000 char c = buf[written];
1001 if (c == '0') {
1002 @@ -407,73 +410,14 @@ static void __init flush_buffer(char *bu
1003 } else
1004 error("junk in compressed archive");
1005 }
1006 + return origLen;
1007 }
1008
1009 -/*
1010 - * gzip declarations
1011 - */
1012 -
1013 -#define OF(args) args
1014 -
1015 -#ifndef memzero
1016 -#define memzero(s, n) memset ((s), 0, (n))
1017 -#endif
1018 +static unsigned my_inptr; /* index of next byte to be processed in inbuf */
1019
1020 -typedef unsigned char uch;
1021 -typedef unsigned short ush;
1022 -typedef unsigned long ulg;
1023 -
1024 -#define WSIZE 0x8000 /* window size--must be a power of two, and */
1025 - /* at least 32K for zip's deflate method */
1026 -
1027 -static uch *inbuf;
1028 -static uch *window;
1029 -
1030 -static unsigned insize; /* valid bytes in inbuf */
1031 -static unsigned inptr; /* index of next byte to be processed in inbuf */
1032 -static unsigned outcnt; /* bytes in output buffer */
1033 -static long bytes_out;
1034 -
1035 -#define get_byte() (inptr < insize ? inbuf[inptr++] : -1)
1036 -
1037 -/* Diagnostic functions (stubbed out) */
1038 -#define Assert(cond,msg)
1039 -#define Trace(x)
1040 -#define Tracev(x)
1041 -#define Tracevv(x)
1042 -#define Tracec(c,x)
1043 -#define Tracecv(c,x)
1044 -
1045 -#define STATIC static
1046 -#define INIT __init
1047 -
1048 -static void __init flush_window(void);
1049 -static void __init error(char *m);
1050 -
1051 -#define NO_INFLATE_MALLOC
1052 -
1053 -#include "../lib/inflate.c"
1054 -
1055 -/* ===========================================================================
1056 - * Write the output window window[0..outcnt-1] and update crc and bytes_out.
1057 - * (Used for the decompressed data only.)
1058 - */
1059 -static void __init flush_window(void)
1060 -{
1061 - ulg c = crc; /* temporary variable */
1062 - unsigned n;
1063 - uch *in, ch;
1064 -
1065 - flush_buffer(window, outcnt);
1066 - in = window;
1067 - for (n = 0; n < outcnt; n++) {
1068 - ch = *in++;
1069 - c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
1070 - }
1071 - crc = c;
1072 - bytes_out += (ulg)outcnt;
1073 - outcnt = 0;
1074 -}
1075 +#include <linux/decompress/bunzip2.h>
1076 +#include <linux/decompress/unlzma.h>
1077 +#include <linux/decompress/inflate.h>
1078
1079 static char * __init unpack_to_rootfs(char *buf, unsigned len, int check_only)
1080 {
1081 @@ -482,9 +426,10 @@ static char * __init unpack_to_rootfs(ch
1082 header_buf = kmalloc(110, GFP_KERNEL);
1083 symlink_buf = kmalloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1, GFP_KERNEL);
1084 name_buf = kmalloc(N_ALIGN(PATH_MAX), GFP_KERNEL);
1085 - window = kmalloc(WSIZE, GFP_KERNEL);
1086 - if (!window || !header_buf || !symlink_buf || !name_buf)
1087 +
1088 + if (!header_buf || !symlink_buf || !name_buf)
1089 panic("can't allocate buffers");
1090 +
1091 state = Start;
1092 this_header = 0;
1093 message = NULL;
1094 @@ -504,22 +449,38 @@ static char * __init unpack_to_rootfs(ch
1095 continue;
1096 }
1097 this_header = 0;
1098 - insize = len;
1099 - inbuf = buf;
1100 - inptr = 0;
1101 - outcnt = 0; /* bytes in output buffer */
1102 - bytes_out = 0;
1103 - crc = (ulg)0xffffffffL; /* shift register contents */
1104 - makecrc();
1105 - gunzip();
1106 + if (!gunzip(buf, len, NULL, flush_buffer, NULL,
1107 + &my_inptr, error) &&
1108 + message == NULL)
1109 + goto ok;
1110 +
1111 +#ifdef CONFIG_RD_BZIP2
1112 + message = NULL; /* Zero out message, or else cpio will think an error has already occured */
1113 + if (!bunzip2(buf, len, NULL, flush_buffer, NULL,
1114 + &my_inptr, error) < 0
1115 + &&
1116 + message == NULL) {
1117 + goto ok;
1118 + }
1119 +#endif
1120 +
1121 +#ifdef CONFIG_RD_LZMA
1122 + message = NULL; /* Zero out message, or else cpio will think an error has already occured */
1123 + if (!unlzma(buf, len, NULL, flush_buffer, NULL,
1124 + &my_inptr, error) < 0
1125 + &&
1126 + message == NULL) {
1127 + goto ok;
1128 + }
1129 +#endif
1130 +ok:
1131 if (state != Reset)
1132 - error("junk in gzipped archive");
1133 - this_header = saved_offset + inptr;
1134 - buf += inptr;
1135 - len -= inptr;
1136 + error("junk in compressed archive");
1137 + this_header = saved_offset + my_inptr;
1138 + buf += my_inptr;
1139 + len -= my_inptr;
1140 }
1141 dir_utime();
1142 - kfree(window);
1143 kfree(name_buf);
1144 kfree(symlink_buf);
1145 kfree(header_buf);
1146 --- a/lib/Makefile
1147 +++ b/lib/Makefile
1148 @@ -11,7 +11,8 @@ lib-y := ctype.o string.o vsprintf.o cmd
1149 rbtree.o radix-tree.o dump_stack.o \
1150 idr.o int_sqrt.o extable.o prio_tree.o \
1151 sha1.o irq_regs.o reciprocal_div.o argv_split.o \
1152 - proportions.o prio_heap.o ratelimit.o show_mem.o
1153 + proportions.o prio_heap.o ratelimit.o show_mem.o \
1154 + inflate.o decompress_bunzip2.o decompress_unlzma.o
1155
1156 lib-$(CONFIG_MMU) += ioremap.o
1157 lib-$(CONFIG_SMP) += cpumask.o
1158 --- /dev/null
1159 +++ b/lib/decompress_bunzip2.c
1160 @@ -0,0 +1,735 @@
1161 +/* vi: set sw = 4 ts = 4: */
1162 +/* Small bzip2 deflate implementation, by Rob Landley (rob@landley.net).
1163 +
1164 + Based on bzip2 decompression code by Julian R Seward (jseward@acm.org),
1165 + which also acknowledges contributions by Mike Burrows, David Wheeler,
1166 + Peter Fenwick, Alistair Moffat, Radford Neal, Ian H. Witten,
1167 + Robert Sedgewick, and Jon L. Bentley.
1168 +
1169 + This code is licensed under the LGPLv2:
1170 + LGPL (http://www.gnu.org/copyleft/lgpl.html
1171 +*/
1172 +
1173 +/*
1174 + Size and speed optimizations by Manuel Novoa III (mjn3@codepoet.org).
1175 +
1176 + More efficient reading of Huffman codes, a streamlined read_bunzip()
1177 + function, and various other tweaks. In (limited) tests, approximately
1178 + 20% faster than bzcat on x86 and about 10% faster on arm.
1179 +
1180 + Note that about 2/3 of the time is spent in read_unzip() reversing
1181 + the Burrows-Wheeler transformation. Much of that time is delay
1182 + resulting from cache misses.
1183 +
1184 + I would ask that anyone benefiting from this work, especially those
1185 + using it in commercial products, consider making a donation to my local
1186 + non-profit hospice organization in the name of the woman I loved, who
1187 + passed away Feb. 12, 2003.
1188 +
1189 + In memory of Toni W. Hagan
1190 +
1191 + Hospice of Acadiana, Inc.
1192 + 2600 Johnston St., Suite 200
1193 + Lafayette, LA 70503-3240
1194 +
1195 + Phone (337) 232-1234 or 1-800-738-2226
1196 + Fax (337) 232-1297
1197 +
1198 + http://www.hospiceacadiana.com/
1199 +
1200 + Manuel
1201 + */
1202 +
1203 +/*
1204 + Made it fit for running in Linux Kernel by Alain Knaff (alain@knaff.lu)
1205 +*/
1206 +
1207 +
1208 +#ifndef STATIC
1209 +#include <linux/decompress/bunzip2.h>
1210 +#endif /* !STATIC */
1211 +
1212 +#include <linux/decompress/mm.h>
1213 +
1214 +#ifndef INT_MAX
1215 +#define INT_MAX 0x7fffffff
1216 +#endif
1217 +
1218 +/* Constants for Huffman coding */
1219 +#define MAX_GROUPS 6
1220 +#define GROUP_SIZE 50 /* 64 would have been more efficient */
1221 +#define MAX_HUFCODE_BITS 20 /* Longest Huffman code allowed */
1222 +#define MAX_SYMBOLS 258 /* 256 literals + RUNA + RUNB */
1223 +#define SYMBOL_RUNA 0
1224 +#define SYMBOL_RUNB 1
1225 +
1226 +/* Status return values */
1227 +#define RETVAL_OK 0
1228 +#define RETVAL_LAST_BLOCK (-1)
1229 +#define RETVAL_NOT_BZIP_DATA (-2)
1230 +#define RETVAL_UNEXPECTED_INPUT_EOF (-3)
1231 +#define RETVAL_UNEXPECTED_OUTPUT_EOF (-4)
1232 +#define RETVAL_DATA_ERROR (-5)
1233 +#define RETVAL_OUT_OF_MEMORY (-6)
1234 +#define RETVAL_OBSOLETE_INPUT (-7)
1235 +
1236 +/* Other housekeeping constants */
1237 +#define BZIP2_IOBUF_SIZE 4096
1238 +
1239 +/* This is what we know about each Huffman coding group */
1240 +struct group_data {
1241 + /* We have an extra slot at the end of limit[] for a sentinal value. */
1242 + int limit[MAX_HUFCODE_BITS+1];
1243 + int base[MAX_HUFCODE_BITS];
1244 + int permute[MAX_SYMBOLS];
1245 + int minLen, maxLen;
1246 +};
1247 +
1248 +/* Structure holding all the housekeeping data, including IO buffers and
1249 + memory that persists between calls to bunzip */
1250 +struct bunzip_data {
1251 + /* State for interrupting output loop */
1252 + int writeCopies, writePos, writeRunCountdown, writeCount, writeCurrent;
1253 + /* I/O tracking data (file handles, buffers, positions, etc.) */
1254 + int (*fill)(void*, unsigned int);
1255 + int inbufCount, inbufPos /*, outbufPos*/;
1256 + unsigned char *inbuf /*,*outbuf*/;
1257 + unsigned int inbufBitCount, inbufBits;
1258 + /* The CRC values stored in the block header and calculated from the
1259 + data */
1260 + unsigned int crc32Table[256], headerCRC, totalCRC, writeCRC;
1261 + /* Intermediate buffer and its size (in bytes) */
1262 + unsigned int *dbuf, dbufSize;
1263 + /* These things are a bit too big to go on the stack */
1264 + unsigned char selectors[32768]; /* nSelectors = 15 bits */
1265 + struct group_data groups[MAX_GROUPS]; /* Huffman coding tables */
1266 + int io_error; /* non-zero if we have IO error */
1267 +};
1268 +
1269 +
1270 +/* Return the next nnn bits of input. All reads from the compressed input
1271 + are done through this function. All reads are big endian */
1272 +static unsigned int INIT get_bits(struct bunzip_data *bd, char bits_wanted)
1273 +{
1274 + unsigned int bits = 0;
1275 +
1276 + /* If we need to get more data from the byte buffer, do so.
1277 + (Loop getting one byte at a time to enforce endianness and avoid
1278 + unaligned access.) */
1279 + while (bd->inbufBitCount < bits_wanted) {
1280 + /* If we need to read more data from file into byte buffer, do
1281 + so */
1282 + if (bd->inbufPos == bd->inbufCount) {
1283 + if (bd->io_error)
1284 + return 0;
1285 + bd->inbufCount = bd->fill(bd->inbuf, BZIP2_IOBUF_SIZE);
1286 + if (bd->inbufCount <= 0) {
1287 + bd->io_error = RETVAL_UNEXPECTED_INPUT_EOF;
1288 + return 0;
1289 + }
1290 + bd->inbufPos = 0;
1291 + }
1292 + /* Avoid 32-bit overflow (dump bit buffer to top of output) */
1293 + if (bd->inbufBitCount >= 24) {
1294 + bits = bd->inbufBits&((1 << bd->inbufBitCount)-1);
1295 + bits_wanted -= bd->inbufBitCount;
1296 + bits <<= bits_wanted;
1297 + bd->inbufBitCount = 0;
1298 + }
1299 + /* Grab next 8 bits of input from buffer. */
1300 + bd->inbufBits = (bd->inbufBits << 8)|bd->inbuf[bd->inbufPos++];
1301 + bd->inbufBitCount += 8;
1302 + }
1303 + /* Calculate result */
1304 + bd->inbufBitCount -= bits_wanted;
1305 + bits |= (bd->inbufBits >> bd->inbufBitCount)&((1 << bits_wanted)-1);
1306 +
1307 + return bits;
1308 +}
1309 +
1310 +/* Unpacks the next block and sets up for the inverse burrows-wheeler step. */
1311 +
1312 +static int INIT get_next_block(struct bunzip_data *bd)
1313 +{
1314 + struct group_data *hufGroup = NULL;
1315 + int *base = NULL;
1316 + int *limit = NULL;
1317 + int dbufCount, nextSym, dbufSize, groupCount, selector,
1318 + i, j, k, t, runPos, symCount, symTotal, nSelectors,
1319 + byteCount[256];
1320 + unsigned char uc, symToByte[256], mtfSymbol[256], *selectors;
1321 + unsigned int *dbuf, origPtr;
1322 +
1323 + dbuf = bd->dbuf;
1324 + dbufSize = bd->dbufSize;
1325 + selectors = bd->selectors;
1326 +
1327 + /* Read in header signature and CRC, then validate signature.
1328 + (last block signature means CRC is for whole file, return now) */
1329 + i = get_bits(bd, 24);
1330 + j = get_bits(bd, 24);
1331 + bd->headerCRC = get_bits(bd, 32);
1332 + if ((i == 0x177245) && (j == 0x385090))
1333 + return RETVAL_LAST_BLOCK;
1334 + if ((i != 0x314159) || (j != 0x265359))
1335 + return RETVAL_NOT_BZIP_DATA;
1336 + /* We can add support for blockRandomised if anybody complains.
1337 + There was some code for this in busybox 1.0.0-pre3, but nobody ever
1338 + noticed that it didn't actually work. */
1339 + if (get_bits(bd, 1))
1340 + return RETVAL_OBSOLETE_INPUT;
1341 + origPtr = get_bits(bd, 24);
1342 + if (origPtr > dbufSize)
1343 + return RETVAL_DATA_ERROR;
1344 + /* mapping table: if some byte values are never used (encoding things
1345 + like ascii text), the compression code removes the gaps to have fewer
1346 + symbols to deal with, and writes a sparse bitfield indicating which
1347 + values were present. We make a translation table to convert the
1348 + symbols back to the corresponding bytes. */
1349 + t = get_bits(bd, 16);
1350 + symTotal = 0;
1351 + for (i = 0; i < 16; i++) {
1352 + if (t&(1 << (15-i))) {
1353 + k = get_bits(bd, 16);
1354 + for (j = 0; j < 16; j++)
1355 + if (k&(1 << (15-j)))
1356 + symToByte[symTotal++] = (16*i)+j;
1357 + }
1358 + }
1359 + /* How many different Huffman coding groups does this block use? */
1360 + groupCount = get_bits(bd, 3);
1361 + if (groupCount < 2 || groupCount > MAX_GROUPS)
1362 + return RETVAL_DATA_ERROR;
1363 + /* nSelectors: Every GROUP_SIZE many symbols we select a new
1364 + Huffman coding group. Read in the group selector list,
1365 + which is stored as MTF encoded bit runs. (MTF = Move To
1366 + Front, as each value is used it's moved to the start of the
1367 + list.) */
1368 + nSelectors = get_bits(bd, 15);
1369 + if (!nSelectors)
1370 + return RETVAL_DATA_ERROR;
1371 + for (i = 0; i < groupCount; i++)
1372 + mtfSymbol[i] = i;
1373 + for (i = 0; i < nSelectors; i++) {
1374 + /* Get next value */
1375 + for (j = 0; get_bits(bd, 1); j++)
1376 + if (j >= groupCount)
1377 + return RETVAL_DATA_ERROR;
1378 + /* Decode MTF to get the next selector */
1379 + uc = mtfSymbol[j];
1380 + for (; j; j--)
1381 + mtfSymbol[j] = mtfSymbol[j-1];
1382 + mtfSymbol[0] = selectors[i] = uc;
1383 + }
1384 + /* Read the Huffman coding tables for each group, which code
1385 + for symTotal literal symbols, plus two run symbols (RUNA,
1386 + RUNB) */
1387 + symCount = symTotal+2;
1388 + for (j = 0; j < groupCount; j++) {
1389 + unsigned char length[MAX_SYMBOLS], temp[MAX_HUFCODE_BITS+1];
1390 + int minLen, maxLen, pp;
1391 + /* Read Huffman code lengths for each symbol. They're
1392 + stored in a way similar to mtf; record a starting
1393 + value for the first symbol, and an offset from the
1394 + previous value for everys symbol after that.
1395 + (Subtracting 1 before the loop and then adding it
1396 + back at the end is an optimization that makes the
1397 + test inside the loop simpler: symbol length 0
1398 + becomes negative, so an unsigned inequality catches
1399 + it.) */
1400 + t = get_bits(bd, 5)-1;
1401 + for (i = 0; i < symCount; i++) {
1402 + for (;;) {
1403 + if (((unsigned)t) > (MAX_HUFCODE_BITS-1))
1404 + return RETVAL_DATA_ERROR;
1405 +
1406 + /* If first bit is 0, stop. Else
1407 + second bit indicates whether to
1408 + increment or decrement the value.
1409 + Optimization: grab 2 bits and unget
1410 + the second if the first was 0. */
1411 +
1412 + k = get_bits(bd, 2);
1413 + if (k < 2) {
1414 + bd->inbufBitCount++;
1415 + break;
1416 + }
1417 + /* Add one if second bit 1, else
1418 + * subtract 1. Avoids if/else */
1419 + t += (((k+1)&2)-1);
1420 + }
1421 + /* Correct for the initial -1, to get the
1422 + * final symbol length */
1423 + length[i] = t+1;
1424 + }
1425 + /* Find largest and smallest lengths in this group */
1426 + minLen = maxLen = length[0];
1427 +
1428 + for (i = 1; i < symCount; i++) {
1429 + if (length[i] > maxLen)
1430 + maxLen = length[i];
1431 + else if (length[i] < minLen)
1432 + minLen = length[i];
1433 + }
1434 +
1435 + /* Calculate permute[], base[], and limit[] tables from
1436 + * length[].
1437 + *
1438 + * permute[] is the lookup table for converting
1439 + * Huffman coded symbols into decoded symbols. base[]
1440 + * is the amount to subtract from the value of a
1441 + * Huffman symbol of a given length when using
1442 + * permute[].
1443 + *
1444 + * limit[] indicates the largest numerical value a
1445 + * symbol with a given number of bits can have. This
1446 + * is how the Huffman codes can vary in length: each
1447 + * code with a value > limit[length] needs another
1448 + * bit.
1449 + */
1450 + hufGroup = bd->groups+j;
1451 + hufGroup->minLen = minLen;
1452 + hufGroup->maxLen = maxLen;
1453 + /* Note that minLen can't be smaller than 1, so we
1454 + adjust the base and limit array pointers so we're
1455 + not always wasting the first entry. We do this
1456 + again when using them (during symbol decoding).*/
1457 + base = hufGroup->base-1;
1458 + limit = hufGroup->limit-1;
1459 + /* Calculate permute[]. Concurently, initialize
1460 + * temp[] and limit[]. */
1461 + pp = 0;
1462 + for (i = minLen; i <= maxLen; i++) {
1463 + temp[i] = limit[i] = 0;
1464 + for (t = 0; t < symCount; t++)
1465 + if (length[t] == i)
1466 + hufGroup->permute[pp++] = t;
1467 + }
1468 + /* Count symbols coded for at each bit length */
1469 + for (i = 0; i < symCount; i++)
1470 + temp[length[i]]++;
1471 + /* Calculate limit[] (the largest symbol-coding value
1472 + *at each bit length, which is (previous limit <<
1473 + *1)+symbols at this level), and base[] (number of
1474 + *symbols to ignore at each bit length, which is limit
1475 + *minus the cumulative count of symbols coded for
1476 + *already). */
1477 + pp = t = 0;
1478 + for (i = minLen; i < maxLen; i++) {
1479 + pp += temp[i];
1480 + /* We read the largest possible symbol size
1481 + and then unget bits after determining how
1482 + many we need, and those extra bits could be
1483 + set to anything. (They're noise from
1484 + future symbols.) At each level we're
1485 + really only interested in the first few
1486 + bits, so here we set all the trailing
1487 + to-be-ignored bits to 1 so they don't
1488 + affect the value > limit[length]
1489 + comparison. */
1490 + limit[i] = (pp << (maxLen - i)) - 1;
1491 + pp <<= 1;
1492 + base[i+1] = pp-(t += temp[i]);
1493 + }
1494 + limit[maxLen+1] = INT_MAX; /* Sentinal value for
1495 + * reading next sym. */
1496 + limit[maxLen] = pp+temp[maxLen]-1;
1497 + base[minLen] = 0;
1498 + }
1499 + /* We've finished reading and digesting the block header. Now
1500 + read this block's Huffman coded symbols from the file and
1501 + undo the Huffman coding and run length encoding, saving the
1502 + result into dbuf[dbufCount++] = uc */
1503 +
1504 + /* Initialize symbol occurrence counters and symbol Move To
1505 + * Front table */
1506 + for (i = 0; i < 256; i++) {
1507 + byteCount[i] = 0;
1508 + mtfSymbol[i] = (unsigned char)i;
1509 + }
1510 + /* Loop through compressed symbols. */
1511 + runPos = dbufCount = symCount = selector = 0;
1512 + for (;;) {
1513 + /* Determine which Huffman coding group to use. */
1514 + if (!(symCount--)) {
1515 + symCount = GROUP_SIZE-1;
1516 + if (selector >= nSelectors)
1517 + return RETVAL_DATA_ERROR;
1518 + hufGroup = bd->groups+selectors[selector++];
1519 + base = hufGroup->base-1;
1520 + limit = hufGroup->limit-1;
1521 + }
1522 + /* Read next Huffman-coded symbol. */
1523 + /* Note: It is far cheaper to read maxLen bits and
1524 + back up than it is to read minLen bits and then an
1525 + additional bit at a time, testing as we go.
1526 + Because there is a trailing last block (with file
1527 + CRC), there is no danger of the overread causing an
1528 + unexpected EOF for a valid compressed file. As a
1529 + further optimization, we do the read inline
1530 + (falling back to a call to get_bits if the buffer
1531 + runs dry). The following (up to got_huff_bits:) is
1532 + equivalent to j = get_bits(bd, hufGroup->maxLen);
1533 + */
1534 + while (bd->inbufBitCount < hufGroup->maxLen) {
1535 + if (bd->inbufPos == bd->inbufCount) {
1536 + j = get_bits(bd, hufGroup->maxLen);
1537 + goto got_huff_bits;
1538 + }
1539 + bd->inbufBits =
1540 + (bd->inbufBits << 8)|bd->inbuf[bd->inbufPos++];
1541 + bd->inbufBitCount += 8;
1542 + };
1543 + bd->inbufBitCount -= hufGroup->maxLen;
1544 + j = (bd->inbufBits >> bd->inbufBitCount)&
1545 + ((1 << hufGroup->maxLen)-1);
1546 +got_huff_bits:
1547 + /* Figure how how many bits are in next symbol and
1548 + * unget extras */
1549 + i = hufGroup->minLen;
1550 + while (j > limit[i])
1551 + ++i;
1552 + bd->inbufBitCount += (hufGroup->maxLen - i);
1553 + /* Huffman decode value to get nextSym (with bounds checking) */
1554 + if ((i > hufGroup->maxLen)
1555 + || (((unsigned)(j = (j>>(hufGroup->maxLen-i))-base[i]))
1556 + >= MAX_SYMBOLS))
1557 + return RETVAL_DATA_ERROR;
1558 + nextSym = hufGroup->permute[j];
1559 + /* We have now decoded the symbol, which indicates
1560 + either a new literal byte, or a repeated run of the
1561 + most recent literal byte. First, check if nextSym
1562 + indicates a repeated run, and if so loop collecting
1563 + how many times to repeat the last literal. */
1564 + if (((unsigned)nextSym) <= SYMBOL_RUNB) { /* RUNA or RUNB */
1565 + /* If this is the start of a new run, zero out
1566 + * counter */
1567 + if (!runPos) {
1568 + runPos = 1;
1569 + t = 0;
1570 + }
1571 + /* Neat trick that saves 1 symbol: instead of
1572 + or-ing 0 or 1 at each bit position, add 1
1573 + or 2 instead. For example, 1011 is 1 << 0
1574 + + 1 << 1 + 2 << 2. 1010 is 2 << 0 + 2 << 1
1575 + + 1 << 2. You can make any bit pattern
1576 + that way using 1 less symbol than the basic
1577 + or 0/1 method (except all bits 0, which
1578 + would use no symbols, but a run of length 0
1579 + doesn't mean anything in this context).
1580 + Thus space is saved. */
1581 + t += (runPos << nextSym);
1582 + /* +runPos if RUNA; +2*runPos if RUNB */
1583 +
1584 + runPos <<= 1;
1585 + continue;
1586 + }
1587 + /* When we hit the first non-run symbol after a run,
1588 + we now know how many times to repeat the last
1589 + literal, so append that many copies to our buffer
1590 + of decoded symbols (dbuf) now. (The last literal
1591 + used is the one at the head of the mtfSymbol
1592 + array.) */
1593 + if (runPos) {
1594 + runPos = 0;
1595 + if (dbufCount+t >= dbufSize)
1596 + return RETVAL_DATA_ERROR;
1597 +
1598 + uc = symToByte[mtfSymbol[0]];
1599 + byteCount[uc] += t;
1600 + while (t--)
1601 + dbuf[dbufCount++] = uc;
1602 + }
1603 + /* Is this the terminating symbol? */
1604 + if (nextSym > symTotal)
1605 + break;
1606 + /* At this point, nextSym indicates a new literal
1607 + character. Subtract one to get the position in the
1608 + MTF array at which this literal is currently to be
1609 + found. (Note that the result can't be -1 or 0,
1610 + because 0 and 1 are RUNA and RUNB. But another
1611 + instance of the first symbol in the mtf array,
1612 + position 0, would have been handled as part of a
1613 + run above. Therefore 1 unused mtf position minus 2
1614 + non-literal nextSym values equals -1.) */
1615 + if (dbufCount >= dbufSize)
1616 + return RETVAL_DATA_ERROR;
1617 + i = nextSym - 1;
1618 + uc = mtfSymbol[i];
1619 + /* Adjust the MTF array. Since we typically expect to
1620 + *move only a small number of symbols, and are bound
1621 + *by 256 in any case, using memmove here would
1622 + *typically be bigger and slower due to function call
1623 + *overhead and other assorted setup costs. */
1624 + do {
1625 + mtfSymbol[i] = mtfSymbol[i-1];
1626 + } while (--i);
1627 + mtfSymbol[0] = uc;
1628 + uc = symToByte[uc];
1629 + /* We have our literal byte. Save it into dbuf. */
1630 + byteCount[uc]++;
1631 + dbuf[dbufCount++] = (unsigned int)uc;
1632 + }
1633 + /* At this point, we've read all the Huffman-coded symbols
1634 + (and repeated runs) for this block from the input stream,
1635 + and decoded them into the intermediate buffer. There are
1636 + dbufCount many decoded bytes in dbuf[]. Now undo the
1637 + Burrows-Wheeler transform on dbuf. See
1638 + http://dogma.net/markn/articles/bwt/bwt.htm
1639 + */
1640 + /* Turn byteCount into cumulative occurrence counts of 0 to n-1. */
1641 + j = 0;
1642 + for (i = 0; i < 256; i++) {
1643 + k = j+byteCount[i];
1644 + byteCount[i] = j;
1645 + j = k;
1646 + }
1647 + /* Figure out what order dbuf would be in if we sorted it. */
1648 + for (i = 0; i < dbufCount; i++) {
1649 + uc = (unsigned char)(dbuf[i] & 0xff);
1650 + dbuf[byteCount[uc]] |= (i << 8);
1651 + byteCount[uc]++;
1652 + }
1653 + /* Decode first byte by hand to initialize "previous" byte.
1654 + Note that it doesn't get output, and if the first three
1655 + characters are identical it doesn't qualify as a run (hence
1656 + writeRunCountdown = 5). */
1657 + if (dbufCount) {
1658 + if (origPtr >= dbufCount)
1659 + return RETVAL_DATA_ERROR;
1660 + bd->writePos = dbuf[origPtr];
1661 + bd->writeCurrent = (unsigned char)(bd->writePos&0xff);
1662 + bd->writePos >>= 8;
1663 + bd->writeRunCountdown = 5;
1664 + }
1665 + bd->writeCount = dbufCount;
1666 +
1667 + return RETVAL_OK;
1668 +}
1669 +
1670 +/* Undo burrows-wheeler transform on intermediate buffer to produce output.
1671 + If start_bunzip was initialized with out_fd =-1, then up to len bytes of
1672 + data are written to outbuf. Return value is number of bytes written or
1673 + error (all errors are negative numbers). If out_fd!=-1, outbuf and len
1674 + are ignored, data is written to out_fd and return is RETVAL_OK or error.
1675 +*/
1676 +
1677 +static int INIT read_bunzip(struct bunzip_data *bd, char *outbuf, int len)
1678 +{
1679 + const unsigned int *dbuf;
1680 + int pos, xcurrent, previous, gotcount;
1681 +
1682 + /* If last read was short due to end of file, return last block now */
1683 + if (bd->writeCount < 0)
1684 + return bd->writeCount;
1685 +
1686 + gotcount = 0;
1687 + dbuf = bd->dbuf;
1688 + pos = bd->writePos;
1689 + xcurrent = bd->writeCurrent;
1690 +
1691 + /* We will always have pending decoded data to write into the output
1692 + buffer unless this is the very first call (in which case we haven't
1693 + Huffman-decoded a block into the intermediate buffer yet). */
1694 +
1695 + if (bd->writeCopies) {
1696 + /* Inside the loop, writeCopies means extra copies (beyond 1) */
1697 + --bd->writeCopies;
1698 + /* Loop outputting bytes */
1699 + for (;;) {
1700 + /* If the output buffer is full, snapshot
1701 + * state and return */
1702 + if (gotcount >= len) {
1703 + bd->writePos = pos;
1704 + bd->writeCurrent = xcurrent;
1705 + bd->writeCopies++;
1706 + return len;
1707 + }
1708 + /* Write next byte into output buffer, updating CRC */
1709 + outbuf[gotcount++] = xcurrent;
1710 + bd->writeCRC = (((bd->writeCRC) << 8)
1711 + ^bd->crc32Table[((bd->writeCRC) >> 24)
1712 + ^xcurrent]);
1713 + /* Loop now if we're outputting multiple
1714 + * copies of this byte */
1715 + if (bd->writeCopies) {
1716 + --bd->writeCopies;
1717 + continue;
1718 + }
1719 +decode_next_byte:
1720 + if (!bd->writeCount--)
1721 + break;
1722 + /* Follow sequence vector to undo
1723 + * Burrows-Wheeler transform */
1724 + previous = xcurrent;
1725 + pos = dbuf[pos];
1726 + xcurrent = pos&0xff;
1727 + pos >>= 8;
1728 + /* After 3 consecutive copies of the same
1729 + byte, the 4th is a repeat count. We count
1730 + down from 4 instead *of counting up because
1731 + testing for non-zero is faster */
1732 + if (--bd->writeRunCountdown) {
1733 + if (xcurrent != previous)
1734 + bd->writeRunCountdown = 4;
1735 + } else {
1736 + /* We have a repeated run, this byte
1737 + * indicates the count */
1738 + bd->writeCopies = xcurrent;
1739 + xcurrent = previous;
1740 + bd->writeRunCountdown = 5;
1741 + /* Sometimes there are just 3 bytes
1742 + * (run length 0) */
1743 + if (!bd->writeCopies)
1744 + goto decode_next_byte;
1745 + /* Subtract the 1 copy we'd output
1746 + * anyway to get extras */
1747 + --bd->writeCopies;
1748 + }
1749 + }
1750 + /* Decompression of this block completed successfully */
1751 + bd->writeCRC = ~bd->writeCRC;
1752 + bd->totalCRC = ((bd->totalCRC << 1) |
1753 + (bd->totalCRC >> 31)) ^ bd->writeCRC;
1754 + /* If this block had a CRC error, force file level CRC error. */
1755 + if (bd->writeCRC != bd->headerCRC) {
1756 + bd->totalCRC = bd->headerCRC+1;
1757 + return RETVAL_LAST_BLOCK;
1758 + }
1759 + }
1760 +
1761 + /* Refill the intermediate buffer by Huffman-decoding next
1762 + * block of input */
1763 + /* (previous is just a convenient unused temp variable here) */
1764 + previous = get_next_block(bd);
1765 + if (previous) {
1766 + bd->writeCount = previous;
1767 + return (previous != RETVAL_LAST_BLOCK) ? previous : gotcount;
1768 + }
1769 + bd->writeCRC = 0xffffffffUL;
1770 + pos = bd->writePos;
1771 + xcurrent = bd->writeCurrent;
1772 + goto decode_next_byte;
1773 +}
1774 +
1775 +static int INIT nofill(void *buf, unsigned int len)
1776 +{
1777 + return -1;
1778 +}
1779 +
1780 +/* Allocate the structure, read file header. If in_fd ==-1, inbuf must contain
1781 + a complete bunzip file (len bytes long). If in_fd!=-1, inbuf and len are
1782 + ignored, and data is read from file handle into temporary buffer. */
1783 +static int INIT start_bunzip(struct bunzip_data **bdp, void *inbuf, int len,
1784 + int (*fill)(void*, unsigned int))
1785 +{
1786 + struct bunzip_data *bd;
1787 + unsigned int i, j, c;
1788 + const unsigned int BZh0 =
1789 + (((unsigned int)'B') << 24)+(((unsigned int)'Z') << 16)
1790 + +(((unsigned int)'h') << 8)+(unsigned int)'0';
1791 +
1792 + /* Figure out how much data to allocate */
1793 + i = sizeof(struct bunzip_data);
1794 +
1795 + /* Allocate bunzip_data. Most fields initialize to zero. */
1796 + bd = *bdp = malloc(i);
1797 + memset(bd, 0, sizeof(struct bunzip_data));
1798 + /* Setup input buffer */
1799 + bd->inbuf = inbuf;
1800 + bd->inbufCount = len;
1801 + if (fill != NULL)
1802 + bd->fill = fill;
1803 + else
1804 + bd->fill = nofill;
1805 +
1806 + /* Init the CRC32 table (big endian) */
1807 + for (i = 0; i < 256; i++) {
1808 + c = i << 24;
1809 + for (j = 8; j; j--)
1810 + c = c&0x80000000 ? (c << 1)^0x04c11db7 : (c << 1);
1811 + bd->crc32Table[i] = c;
1812 + }
1813 +
1814 + /* Ensure that file starts with "BZh['1'-'9']." */
1815 + i = get_bits(bd, 32);
1816 + if (((unsigned int)(i-BZh0-1)) >= 9)
1817 + return RETVAL_NOT_BZIP_DATA;
1818 +
1819 + /* Fourth byte (ascii '1'-'9'), indicates block size in units of 100k of
1820 + uncompressed data. Allocate intermediate buffer for block. */
1821 + bd->dbufSize = 100000*(i-BZh0);
1822 +
1823 + bd->dbuf = large_malloc(bd->dbufSize * sizeof(int));
1824 + return RETVAL_OK;
1825 +}
1826 +
1827 +/* Example usage: decompress src_fd to dst_fd. (Stops at end of bzip2 data,
1828 + not end of file.) */
1829 +STATIC int INIT bunzip2(unsigned char *buf, int len,
1830 + int(*fill)(void*, unsigned int),
1831 + int(*flush)(void*, unsigned int),
1832 + unsigned char *outbuf,
1833 + int *pos,
1834 + void(*error_fn)(char *x))
1835 +{
1836 + struct bunzip_data *bd;
1837 + int i = -1;
1838 + unsigned char *inbuf;
1839 +
1840 + set_error_fn(error_fn);
1841 + if (flush)
1842 + outbuf = malloc(BZIP2_IOBUF_SIZE);
1843 + else
1844 + len -= 4; /* Uncompressed size hack active in pre-boot
1845 + environment */
1846 + if (!outbuf) {
1847 + error("Could not allocate output bufer");
1848 + return -1;
1849 + }
1850 + if (buf)
1851 + inbuf = buf;
1852 + else
1853 + inbuf = malloc(BZIP2_IOBUF_SIZE);
1854 + if (!inbuf) {
1855 + error("Could not allocate input bufer");
1856 + goto exit_0;
1857 + }
1858 + i = start_bunzip(&bd, inbuf, len, fill);
1859 + if (!i) {
1860 + for (;;) {
1861 + i = read_bunzip(bd, outbuf, BZIP2_IOBUF_SIZE);
1862 + if (i <= 0)
1863 + break;
1864 + if (!flush)
1865 + outbuf += i;
1866 + else
1867 + if (i != flush(outbuf, i)) {
1868 + i = RETVAL_UNEXPECTED_OUTPUT_EOF;
1869 + break;
1870 + }
1871 + }
1872 + }
1873 + /* Check CRC and release memory */
1874 + if (i == RETVAL_LAST_BLOCK) {
1875 + if (bd->headerCRC != bd->totalCRC)
1876 + error("Data integrity error when decompressing.");
1877 + else
1878 + i = RETVAL_OK;
1879 + } else if (i == RETVAL_UNEXPECTED_OUTPUT_EOF) {
1880 + error("Compressed file ends unexpectedly");
1881 + }
1882 + if (bd->dbuf)
1883 + large_free(bd->dbuf);
1884 + if (pos)
1885 + *pos = bd->inbufPos;
1886 + free(bd);
1887 + if (!buf)
1888 + free(inbuf);
1889 +exit_0:
1890 + if (flush)
1891 + free(outbuf);
1892 + return i;
1893 +}
1894 +
1895 +#define decompress bunzip2
1896 --- /dev/null
1897 +++ b/lib/decompress_unlzma.c
1898 @@ -0,0 +1,647 @@
1899 +/* Lzma decompressor for Linux kernel. Shamelessly snarfed
1900 + *from busybox 1.1.1
1901 + *
1902 + *Linux kernel adaptation
1903 + *Copyright (C) 2006 Alain < alain@knaff.lu >
1904 + *
1905 + *Based on small lzma deflate implementation/Small range coder
1906 + *implementation for lzma.
1907 + *Copyright (C) 2006 Aurelien Jacobs < aurel@gnuage.org >
1908 + *
1909 + *Based on LzmaDecode.c from the LZMA SDK 4.22 (http://www.7-zip.org/)
1910 + *Copyright (C) 1999-2005 Igor Pavlov
1911 + *
1912 + *Copyrights of the parts, see headers below.
1913 + *
1914 + *
1915 + *This program is free software; you can redistribute it and/or
1916 + *modify it under the terms of the GNU Lesser General Public
1917 + *License as published by the Free Software Foundation; either
1918 + *version 2.1 of the License, or (at your option) any later version.
1919 + *
1920 + *This program is distributed in the hope that it will be useful,
1921 + *but WITHOUT ANY WARRANTY; without even the implied warranty of
1922 + *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1923 + *Lesser General Public License for more details.
1924 + *
1925 + *You should have received a copy of the GNU Lesser General Public
1926 + *License along with this library; if not, write to the Free Software
1927 + *Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1928 + */
1929 +
1930 +#ifndef STATIC
1931 +#include <linux/decompress/unlzma.h>
1932 +#endif /* STATIC */
1933 +
1934 +#include <linux/decompress/mm.h>
1935 +
1936 +#define MIN(a, b) (((a) < (b)) ? (a) : (b))
1937 +
1938 +static long long INIT read_int(unsigned char *ptr, int size)
1939 +{
1940 + int i;
1941 + long long ret = 0;
1942 +
1943 + for (i = 0; i < size; i++)
1944 + ret = (ret << 8) | ptr[size-i-1];
1945 + return ret;
1946 +}
1947 +
1948 +#define ENDIAN_CONVERT(x) \
1949 + x = (typeof(x))read_int((unsigned char *)&x, sizeof(x))
1950 +
1951 +
1952 +/* Small range coder implementation for lzma.
1953 + *Copyright (C) 2006 Aurelien Jacobs < aurel@gnuage.org >
1954 + *
1955 + *Based on LzmaDecode.c from the LZMA SDK 4.22 (http://www.7-zip.org/)
1956 + *Copyright (c) 1999-2005 Igor Pavlov
1957 + */
1958 +
1959 +#include <linux/compiler.h>
1960 +
1961 +#define LZMA_IOBUF_SIZE 0x10000
1962 +
1963 +struct rc {
1964 + int (*fill)(void*, unsigned int);
1965 + uint8_t *ptr;
1966 + uint8_t *buffer;
1967 + uint8_t *buffer_end;
1968 + int buffer_size;
1969 + uint32_t code;
1970 + uint32_t range;
1971 + uint32_t bound;
1972 +};
1973 +
1974 +
1975 +#define RC_TOP_BITS 24
1976 +#define RC_MOVE_BITS 5
1977 +#define RC_MODEL_TOTAL_BITS 11
1978 +
1979 +
1980 +/* Called twice: once at startup and once in rc_normalize() */
1981 +static void INIT rc_read(struct rc *rc)
1982 +{
1983 + rc->buffer_size = rc->fill((char *)rc->buffer, LZMA_IOBUF_SIZE);
1984 + if (rc->buffer_size <= 0)
1985 + error("unexpected EOF");
1986 + rc->ptr = rc->buffer;
1987 + rc->buffer_end = rc->buffer + rc->buffer_size;
1988 +}
1989 +
1990 +/* Called once */
1991 +static inline void INIT rc_init(struct rc *rc,
1992 + int (*fill)(void*, unsigned int),
1993 + char *buffer, int buffer_size)
1994 +{
1995 + rc->fill = fill;
1996 + rc->buffer = (uint8_t *)buffer;
1997 + rc->buffer_size = buffer_size;
1998 + rc->buffer_end = rc->buffer + rc->buffer_size;
1999 + rc->ptr = rc->buffer;
2000 +
2001 + rc->code = 0;
2002 + rc->range = 0xFFFFFFFF;
2003 +}
2004 +
2005 +static inline void INIT rc_init_code(struct rc *rc)
2006 +{
2007 + int i;
2008 +
2009 + for (i = 0; i < 5; i++) {
2010 + if (rc->ptr >= rc->buffer_end)
2011 + rc_read(rc);
2012 + rc->code = (rc->code << 8) | *rc->ptr++;
2013 + }
2014 +}
2015 +
2016 +
2017 +/* Called once. TODO: bb_maybe_free() */
2018 +static inline void INIT rc_free(struct rc *rc)
2019 +{
2020 + free(rc->buffer);
2021 +}
2022 +
2023 +/* Called twice, but one callsite is in inline'd rc_is_bit_0_helper() */
2024 +static void INIT rc_do_normalize(struct rc *rc)
2025 +{
2026 + if (rc->ptr >= rc->buffer_end)
2027 + rc_read(rc);
2028 + rc->range <<= 8;
2029 + rc->code = (rc->code << 8) | *rc->ptr++;
2030 +}
2031 +static inline void INIT rc_normalize(struct rc *rc)
2032 +{
2033 + if (rc->range < (1 << RC_TOP_BITS))
2034 + rc_do_normalize(rc);
2035 +}
2036 +
2037 +/* Called 9 times */
2038 +/* Why rc_is_bit_0_helper exists?
2039 + *Because we want to always expose (rc->code < rc->bound) to optimizer
2040 + */
2041 +static inline uint32_t INIT rc_is_bit_0_helper(struct rc *rc, uint16_t *p)
2042 +{
2043 + rc_normalize(rc);
2044 + rc->bound = *p * (rc->range >> RC_MODEL_TOTAL_BITS);
2045 + return rc->bound;
2046 +}
2047 +static inline int INIT rc_is_bit_0(struct rc *rc, uint16_t *p)
2048 +{
2049 + uint32_t t = rc_is_bit_0_helper(rc, p);
2050 + return rc->code < t;
2051 +}
2052 +
2053 +/* Called ~10 times, but very small, thus inlined */
2054 +static inline void INIT rc_update_bit_0(struct rc *rc, uint16_t *p)
2055 +{
2056 + rc->range = rc->bound;
2057 + *p += ((1 << RC_MODEL_TOTAL_BITS) - *p) >> RC_MOVE_BITS;
2058 +}
2059 +static inline void rc_update_bit_1(struct rc *rc, uint16_t *p)
2060 +{
2061 + rc->range -= rc->bound;
2062 + rc->code -= rc->bound;
2063 + *p -= *p >> RC_MOVE_BITS;
2064 +}
2065 +
2066 +/* Called 4 times in unlzma loop */
2067 +static int INIT rc_get_bit(struct rc *rc, uint16_t *p, int *symbol)
2068 +{
2069 + if (rc_is_bit_0(rc, p)) {
2070 + rc_update_bit_0(rc, p);
2071 + *symbol *= 2;
2072 + return 0;
2073 + } else {
2074 + rc_update_bit_1(rc, p);
2075 + *symbol = *symbol * 2 + 1;
2076 + return 1;
2077 + }
2078 +}
2079 +
2080 +/* Called once */
2081 +static inline int INIT rc_direct_bit(struct rc *rc)
2082 +{
2083 + rc_normalize(rc);
2084 + rc->range >>= 1;
2085 + if (rc->code >= rc->range) {
2086 + rc->code -= rc->range;
2087 + return 1;
2088 + }
2089 + return 0;
2090 +}
2091 +
2092 +/* Called twice */
2093 +static inline void INIT
2094 +rc_bit_tree_decode(struct rc *rc, uint16_t *p, int num_levels, int *symbol)
2095 +{
2096 + int i = num_levels;
2097 +
2098 + *symbol = 1;
2099 + while (i--)
2100 + rc_get_bit(rc, p + *symbol, symbol);
2101 + *symbol -= 1 << num_levels;
2102 +}
2103 +
2104 +
2105 +/*
2106 + * Small lzma deflate implementation.
2107 + * Copyright (C) 2006 Aurelien Jacobs < aurel@gnuage.org >
2108 + *
2109 + * Based on LzmaDecode.c from the LZMA SDK 4.22 (http://www.7-zip.org/)
2110 + * Copyright (C) 1999-2005 Igor Pavlov
2111 + */
2112 +
2113 +
2114 +struct lzma_header {
2115 + uint8_t pos;
2116 + uint32_t dict_size;
2117 + uint64_t dst_size;
2118 +} __attribute__ ((packed)) ;
2119 +
2120 +
2121 +#define LZMA_BASE_SIZE 1846
2122 +#define LZMA_LIT_SIZE 768
2123 +
2124 +#define LZMA_NUM_POS_BITS_MAX 4
2125 +
2126 +#define LZMA_LEN_NUM_LOW_BITS 3
2127 +#define LZMA_LEN_NUM_MID_BITS 3
2128 +#define LZMA_LEN_NUM_HIGH_BITS 8
2129 +
2130 +#define LZMA_LEN_CHOICE 0
2131 +#define LZMA_LEN_CHOICE_2 (LZMA_LEN_CHOICE + 1)
2132 +#define LZMA_LEN_LOW (LZMA_LEN_CHOICE_2 + 1)
2133 +#define LZMA_LEN_MID (LZMA_LEN_LOW \
2134 + + (1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_LOW_BITS)))
2135 +#define LZMA_LEN_HIGH (LZMA_LEN_MID \
2136 + +(1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_MID_BITS)))
2137 +#define LZMA_NUM_LEN_PROBS (LZMA_LEN_HIGH + (1 << LZMA_LEN_NUM_HIGH_BITS))
2138 +
2139 +#define LZMA_NUM_STATES 12
2140 +#define LZMA_NUM_LIT_STATES 7
2141 +
2142 +#define LZMA_START_POS_MODEL_INDEX 4
2143 +#define LZMA_END_POS_MODEL_INDEX 14
2144 +#define LZMA_NUM_FULL_DISTANCES (1 << (LZMA_END_POS_MODEL_INDEX >> 1))
2145 +
2146 +#define LZMA_NUM_POS_SLOT_BITS 6
2147 +#define LZMA_NUM_LEN_TO_POS_STATES 4
2148 +
2149 +#define LZMA_NUM_ALIGN_BITS 4
2150 +
2151 +#define LZMA_MATCH_MIN_LEN 2
2152 +
2153 +#define LZMA_IS_MATCH 0
2154 +#define LZMA_IS_REP (LZMA_IS_MATCH + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX))
2155 +#define LZMA_IS_REP_G0 (LZMA_IS_REP + LZMA_NUM_STATES)
2156 +#define LZMA_IS_REP_G1 (LZMA_IS_REP_G0 + LZMA_NUM_STATES)
2157 +#define LZMA_IS_REP_G2 (LZMA_IS_REP_G1 + LZMA_NUM_STATES)
2158 +#define LZMA_IS_REP_0_LONG (LZMA_IS_REP_G2 + LZMA_NUM_STATES)
2159 +#define LZMA_POS_SLOT (LZMA_IS_REP_0_LONG \
2160 + + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX))
2161 +#define LZMA_SPEC_POS (LZMA_POS_SLOT \
2162 + +(LZMA_NUM_LEN_TO_POS_STATES << LZMA_NUM_POS_SLOT_BITS))
2163 +#define LZMA_ALIGN (LZMA_SPEC_POS \
2164 + + LZMA_NUM_FULL_DISTANCES - LZMA_END_POS_MODEL_INDEX)
2165 +#define LZMA_LEN_CODER (LZMA_ALIGN + (1 << LZMA_NUM_ALIGN_BITS))
2166 +#define LZMA_REP_LEN_CODER (LZMA_LEN_CODER + LZMA_NUM_LEN_PROBS)
2167 +#define LZMA_LITERAL (LZMA_REP_LEN_CODER + LZMA_NUM_LEN_PROBS)
2168 +
2169 +
2170 +struct writer {
2171 + uint8_t *buffer;
2172 + uint8_t previous_byte;
2173 + size_t buffer_pos;
2174 + int bufsize;
2175 + size_t global_pos;
2176 + int(*flush)(void*, unsigned int);
2177 + struct lzma_header *header;
2178 +};
2179 +
2180 +struct cstate {
2181 + int state;
2182 + uint32_t rep0, rep1, rep2, rep3;
2183 +};
2184 +
2185 +static inline size_t INIT get_pos(struct writer *wr)
2186 +{
2187 + return
2188 + wr->global_pos + wr->buffer_pos;
2189 +}
2190 +
2191 +static inline uint8_t INIT peek_old_byte(struct writer *wr,
2192 + uint32_t offs)
2193 +{
2194 + if (!wr->flush) {
2195 + int32_t pos;
2196 + while (offs > wr->header->dict_size)
2197 + offs -= wr->header->dict_size;
2198 + pos = wr->buffer_pos - offs;
2199 + return wr->buffer[pos];
2200 + } else {
2201 + uint32_t pos = wr->buffer_pos - offs;
2202 + while (pos >= wr->header->dict_size)
2203 + pos += wr->header->dict_size;
2204 + return wr->buffer[pos];
2205 + }
2206 +
2207 +}
2208 +
2209 +static inline void INIT write_byte(struct writer *wr, uint8_t byte)
2210 +{
2211 + wr->buffer[wr->buffer_pos++] = wr->previous_byte = byte;
2212 + if (wr->flush && wr->buffer_pos == wr->header->dict_size) {
2213 + wr->buffer_pos = 0;
2214 + wr->global_pos += wr->header->dict_size;
2215 + wr->flush((char *)wr->buffer, wr->header->dict_size);
2216 + }
2217 +}
2218 +
2219 +
2220 +static inline void INIT copy_byte(struct writer *wr, uint32_t offs)
2221 +{
2222 + write_byte(wr, peek_old_byte(wr, offs));
2223 +}
2224 +
2225 +static inline void INIT copy_bytes(struct writer *wr,
2226 + uint32_t rep0, int len)
2227 +{
2228 + do {
2229 + copy_byte(wr, rep0);
2230 + len--;
2231 + } while (len != 0 && wr->buffer_pos < wr->header->dst_size);
2232 +}
2233 +
2234 +static inline void INIT process_bit0(struct writer *wr, struct rc *rc,
2235 + struct cstate *cst, uint16_t *p,
2236 + int pos_state, uint16_t *prob,
2237 + int lc, uint32_t literal_pos_mask) {
2238 + int mi = 1;
2239 + rc_update_bit_0(rc, prob);
2240 + prob = (p + LZMA_LITERAL +
2241 + (LZMA_LIT_SIZE
2242 + * (((get_pos(wr) & literal_pos_mask) << lc)
2243 + + (wr->previous_byte >> (8 - lc))))
2244 + );
2245 +
2246 + if (cst->state >= LZMA_NUM_LIT_STATES) {
2247 + int match_byte = peek_old_byte(wr, cst->rep0);
2248 + do {
2249 + int bit;
2250 + uint16_t *prob_lit;
2251 +
2252 + match_byte <<= 1;
2253 + bit = match_byte & 0x100;
2254 + prob_lit = prob + 0x100 + bit + mi;
2255 + if (rc_get_bit(rc, prob_lit, &mi)) {
2256 + if (!bit)
2257 + break;
2258 + } else {
2259 + if (bit)
2260 + break;
2261 + }
2262 + } while (mi < 0x100);
2263 + }
2264 + while (mi < 0x100) {
2265 + uint16_t *prob_lit = prob + mi;
2266 + rc_get_bit(rc, prob_lit, &mi);
2267 + }
2268 + write_byte(wr, mi);
2269 + if (cst->state < 4)
2270 + cst->state = 0;
2271 + else if (cst->state < 10)
2272 + cst->state -= 3;
2273 + else
2274 + cst->state -= 6;
2275 +}
2276 +
2277 +static inline void INIT process_bit1(struct writer *wr, struct rc *rc,
2278 + struct cstate *cst, uint16_t *p,
2279 + int pos_state, uint16_t *prob) {
2280 + int offset;
2281 + uint16_t *prob_len;
2282 + int num_bits;
2283 + int len;
2284 +
2285 + rc_update_bit_1(rc, prob);
2286 + prob = p + LZMA_IS_REP + cst->state;
2287 + if (rc_is_bit_0(rc, prob)) {
2288 + rc_update_bit_0(rc, prob);
2289 + cst->rep3 = cst->rep2;
2290 + cst->rep2 = cst->rep1;
2291 + cst->rep1 = cst->rep0;
2292 + cst->state = cst->state < LZMA_NUM_LIT_STATES ? 0 : 3;
2293 + prob = p + LZMA_LEN_CODER;
2294 + } else {
2295 + rc_update_bit_1(rc, prob);
2296 + prob = p + LZMA_IS_REP_G0 + cst->state;
2297 + if (rc_is_bit_0(rc, prob)) {
2298 + rc_update_bit_0(rc, prob);
2299 + prob = (p + LZMA_IS_REP_0_LONG
2300 + + (cst->state <<
2301 + LZMA_NUM_POS_BITS_MAX) +
2302 + pos_state);
2303 + if (rc_is_bit_0(rc, prob)) {
2304 + rc_update_bit_0(rc, prob);
2305 +
2306 + cst->state = cst->state < LZMA_NUM_LIT_STATES ?
2307 + 9 : 11;
2308 + copy_byte(wr, cst->rep0);
2309 + return;
2310 + } else {
2311 + rc_update_bit_1(rc, prob);
2312 + }
2313 + } else {
2314 + uint32_t distance;
2315 +
2316 + rc_update_bit_1(rc, prob);
2317 + prob = p + LZMA_IS_REP_G1 + cst->state;
2318 + if (rc_is_bit_0(rc, prob)) {
2319 + rc_update_bit_0(rc, prob);
2320 + distance = cst->rep1;
2321 + } else {
2322 + rc_update_bit_1(rc, prob);
2323 + prob = p + LZMA_IS_REP_G2 + cst->state;
2324 + if (rc_is_bit_0(rc, prob)) {
2325 + rc_update_bit_0(rc, prob);
2326 + distance = cst->rep2;
2327 + } else {
2328 + rc_update_bit_1(rc, prob);
2329 + distance = cst->rep3;
2330 + cst->rep3 = cst->rep2;
2331 + }
2332 + cst->rep2 = cst->rep1;
2333 + }
2334 + cst->rep1 = cst->rep0;
2335 + cst->rep0 = distance;
2336 + }
2337 + cst->state = cst->state < LZMA_NUM_LIT_STATES ? 8 : 11;
2338 + prob = p + LZMA_REP_LEN_CODER;
2339 + }
2340 +
2341 + prob_len = prob + LZMA_LEN_CHOICE;
2342 + if (rc_is_bit_0(rc, prob_len)) {
2343 + rc_update_bit_0(rc, prob_len);
2344 + prob_len = (prob + LZMA_LEN_LOW
2345 + + (pos_state <<
2346 + LZMA_LEN_NUM_LOW_BITS));
2347 + offset = 0;
2348 + num_bits = LZMA_LEN_NUM_LOW_BITS;
2349 + } else {
2350 + rc_update_bit_1(rc, prob_len);
2351 + prob_len = prob + LZMA_LEN_CHOICE_2;
2352 + if (rc_is_bit_0(rc, prob_len)) {
2353 + rc_update_bit_0(rc, prob_len);
2354 + prob_len = (prob + LZMA_LEN_MID
2355 + + (pos_state <<
2356 + LZMA_LEN_NUM_MID_BITS));
2357 + offset = 1 << LZMA_LEN_NUM_LOW_BITS;
2358 + num_bits = LZMA_LEN_NUM_MID_BITS;
2359 + } else {
2360 + rc_update_bit_1(rc, prob_len);
2361 + prob_len = prob + LZMA_LEN_HIGH;
2362 + offset = ((1 << LZMA_LEN_NUM_LOW_BITS)
2363 + + (1 << LZMA_LEN_NUM_MID_BITS));
2364 + num_bits = LZMA_LEN_NUM_HIGH_BITS;
2365 + }
2366 + }
2367 +
2368 + rc_bit_tree_decode(rc, prob_len, num_bits, &len);
2369 + len += offset;
2370 +
2371 + if (cst->state < 4) {
2372 + int pos_slot;
2373 +
2374 + cst->state += LZMA_NUM_LIT_STATES;
2375 + prob =
2376 + p + LZMA_POS_SLOT +
2377 + ((len <
2378 + LZMA_NUM_LEN_TO_POS_STATES ? len :
2379 + LZMA_NUM_LEN_TO_POS_STATES - 1)
2380 + << LZMA_NUM_POS_SLOT_BITS);
2381 + rc_bit_tree_decode(rc, prob,
2382 + LZMA_NUM_POS_SLOT_BITS,
2383 + &pos_slot);
2384 + if (pos_slot >= LZMA_START_POS_MODEL_INDEX) {
2385 + int i, mi;
2386 + num_bits = (pos_slot >> 1) - 1;
2387 + cst->rep0 = 2 | (pos_slot & 1);
2388 + if (pos_slot < LZMA_END_POS_MODEL_INDEX) {
2389 + cst->rep0 <<= num_bits;
2390 + prob = p + LZMA_SPEC_POS +
2391 + cst->rep0 - pos_slot - 1;
2392 + } else {
2393 + num_bits -= LZMA_NUM_ALIGN_BITS;
2394 + while (num_bits--)
2395 + cst->rep0 = (cst->rep0 << 1) |
2396 + rc_direct_bit(rc);
2397 + prob = p + LZMA_ALIGN;
2398 + cst->rep0 <<= LZMA_NUM_ALIGN_BITS;
2399 + num_bits = LZMA_NUM_ALIGN_BITS;
2400 + }
2401 + i = 1;
2402 + mi = 1;
2403 + while (num_bits--) {
2404 + if (rc_get_bit(rc, prob + mi, &mi))
2405 + cst->rep0 |= i;
2406 + i <<= 1;
2407 + }
2408 + } else
2409 + cst->rep0 = pos_slot;
2410 + if (++(cst->rep0) == 0)
2411 + return;
2412 + }
2413 +
2414 + len += LZMA_MATCH_MIN_LEN;
2415 +
2416 + copy_bytes(wr, cst->rep0, len);
2417 +}
2418 +
2419 +
2420 +
2421 +STATIC inline int INIT unlzma(unsigned char *buf, int in_len,
2422 + int(*fill)(void*, unsigned int),
2423 + int(*flush)(void*, unsigned int),
2424 + unsigned char *output,
2425 + int *posp,
2426 + void(*error_fn)(char *x)
2427 + )
2428 +{
2429 + struct lzma_header header;
2430 + int lc, pb, lp;
2431 + uint32_t pos_state_mask;
2432 + uint32_t literal_pos_mask;
2433 + uint16_t *p;
2434 + int num_probs;
2435 + struct rc rc;
2436 + int i, mi;
2437 + struct writer wr;
2438 + struct cstate cst;
2439 + unsigned char *inbuf;
2440 + int ret = -1;
2441 +
2442 + set_error_fn(error_fn);
2443 + if (!flush)
2444 + in_len -= 4; /* Uncompressed size hack active in pre-boot
2445 + environment */
2446 + if (buf)
2447 + inbuf = buf;
2448 + else
2449 + inbuf = malloc(LZMA_IOBUF_SIZE);
2450 + if (!inbuf) {
2451 + error("Could not allocate input bufer");
2452 + goto exit_0;
2453 + }
2454 +
2455 + cst.state = 0;
2456 + cst.rep0 = cst.rep1 = cst.rep2 = cst.rep3 = 1;
2457 +
2458 + wr.header = &header;
2459 + wr.flush = flush;
2460 + wr.global_pos = 0;
2461 + wr.previous_byte = 0;
2462 + wr.buffer_pos = 0;
2463 +
2464 + rc_init(&rc, fill, inbuf, in_len);
2465 +
2466 + for (i = 0; i < sizeof(header); i++) {
2467 + if (rc.ptr >= rc.buffer_end)
2468 + rc_read(&rc);
2469 + ((unsigned char *)&header)[i] = *rc.ptr++;
2470 + }
2471 +
2472 + if (header.pos >= (9 * 5 * 5))
2473 + error("bad header");
2474 +
2475 + mi = 0;
2476 + lc = header.pos;
2477 + while (lc >= 9) {
2478 + mi++;
2479 + lc -= 9;
2480 + }
2481 + pb = 0;
2482 + lp = mi;
2483 + while (lp >= 5) {
2484 + pb++;
2485 + lp -= 5;
2486 + }
2487 + pos_state_mask = (1 << pb) - 1;
2488 + literal_pos_mask = (1 << lp) - 1;
2489 +
2490 + ENDIAN_CONVERT(header.dict_size);
2491 + ENDIAN_CONVERT(header.dst_size);
2492 +
2493 + if (header.dict_size == 0)
2494 + header.dict_size = 1;
2495 +
2496 + if (output)
2497 + wr.buffer = output;
2498 + else {
2499 + wr.bufsize = MIN(header.dst_size, header.dict_size);
2500 + wr.buffer = large_malloc(wr.bufsize);
2501 + }
2502 + if (wr.buffer == NULL)
2503 + goto exit_1;
2504 +
2505 + num_probs = LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp));
2506 + p = (uint16_t *) large_malloc(num_probs * sizeof(*p));
2507 + if (p == 0)
2508 + goto exit_2;
2509 + num_probs = LZMA_LITERAL + (LZMA_LIT_SIZE << (lc + lp));
2510 + for (i = 0; i < num_probs; i++)
2511 + p[i] = (1 << RC_MODEL_TOTAL_BITS) >> 1;
2512 +
2513 + rc_init_code(&rc);
2514 +
2515 + while (get_pos(&wr) < header.dst_size) {
2516 + int pos_state = get_pos(&wr) & pos_state_mask;
2517 + uint16_t *prob = p + LZMA_IS_MATCH +
2518 + (cst.state << LZMA_NUM_POS_BITS_MAX) + pos_state;
2519 + if (rc_is_bit_0(&rc, prob))
2520 + process_bit0(&wr, &rc, &cst, p, pos_state, prob,
2521 + lc, literal_pos_mask);
2522 + else {
2523 + process_bit1(&wr, &rc, &cst, p, pos_state, prob);
2524 + if (cst.rep0 == 0)
2525 + break;
2526 + }
2527 + }
2528 +
2529 + if (posp)
2530 + *posp = rc.ptr-rc.buffer;
2531 + if (wr.flush)
2532 + wr.flush(wr.buffer, wr.buffer_pos);
2533 + ret = 0;
2534 + large_free(p);
2535 +exit_2:
2536 + if (!output)
2537 + large_free(wr.buffer);
2538 +exit_1:
2539 + if (!buf)
2540 + free(inbuf);
2541 +exit_0:
2542 + return ret;
2543 +}
2544 +
2545 +#define decompress unlzma
2546 --- a/lib/inflate.c
2547 +++ b/lib/inflate.c
2548 @@ -109,20 +109,78 @@ static char rcsid[] = "#Id: inflate.c,v
2549 #endif
2550
2551 #ifndef STATIC
2552 +#include <linux/decompress/inflate.h>
2553 +#endif /* ! STATIC */
2554
2555 -#if defined(STDC_HEADERS) || defined(HAVE_STDLIB_H)
2556 -# include <sys/types.h>
2557 -# include <stdlib.h>
2558 +#include <linux/decompress/mm.h>
2559 +
2560 +#include <linux/string.h>
2561 +
2562 +#ifdef NEW_CODE
2563 +static int(*flush_cb)(void*, unsigned int);
2564 +static int(*fill_cb)(void*, unsigned int);
2565 +
2566 +/* Begin stuff copied from initramfs */
2567 +/*
2568 + * gzip declarations
2569 + */
2570 +
2571 +#define OF(args) args
2572 +
2573 +#ifndef memzero
2574 +#define memzero(s, n) memset((s), 0, (n))
2575 #endif
2576
2577 -#include "gzip.h"
2578 -#define STATIC
2579 -#endif /* !STATIC */
2580 +#define INBUFSIZ 4096
2581 +
2582 +#define WSIZE 0x8000 /* window size--must be a power of two, and */
2583 + /* at least 32K for zip's deflate method */
2584 +
2585 +static uint8_t *inbuf;
2586 +static uint8_t *window;
2587 +
2588 +static unsigned insize; /* valid bytes in inbuf */
2589 +static unsigned outcnt; /* bytes in output buffer */
2590 +static long bytes_out;
2591 +
2592 +/* --- */
2593 +
2594 +static unsigned inptr; /* index of next byte to be processed in inbuf */
2595 +
2596 +/* --- */
2597 +
2598 +/* ===========================================================================
2599 + * Fill the input buffer. This is called only when the buffer is empty
2600 + * and at least one byte is really needed.
2601 + * Returning -1 does not guarantee that gunzip() will ever return.
2602 + */
2603 +static int INIT fill_inbuf(void)
2604 +{
2605 + insize = fill_cb(inbuf, INBUFSIZ);
2606 + if (insize <= 0) {
2607 + error("RAMDISK: ran out of compressed data");
2608 + return -1;
2609 + }
2610 +
2611 + inptr = 1;
2612 +
2613 + return inbuf[0];
2614 +}
2615 +
2616 +#define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
2617 +
2618 +/* Diagnostic functions (stubbed out) */
2619 +#define Assert(cond, msg)
2620 +#define Trace(x)
2621 +#define Tracev(x)
2622 +#define Tracevv(x)
2623 +#define Tracec(c, x)
2624 +#define Tracecv(c, x)
2625
2626 -#ifndef INIT
2627 -#define INIT
2628 +static void flush_window(void);
2629 +/* End stuff copied from initramfs */
2630 #endif
2631 -
2632 +
2633 #define slide window
2634
2635 /* Huffman code lookup table entry--this entry is four bytes for machines
2636 @@ -133,10 +191,10 @@ static char rcsid[] = "#Id: inflate.c,v
2637 an unused code. If a code with e == 99 is looked up, this implies an
2638 error in the data. */
2639 struct huft {
2640 - uch e; /* number of extra bits or operation */
2641 - uch b; /* number of bits in this code or subcode */
2642 + uint8_t e; /* number of extra bits or operation */
2643 + uint8_t b; /* number of bits in this code or subcode */
2644 union {
2645 - ush n; /* literal, length base, or distance base */
2646 + uint16_t n; /* literal, length base, or distance base */
2647 struct huft *t; /* pointer to next level of table */
2648 } v;
2649 };
2650 @@ -144,7 +202,7 @@ struct huft {
2651
2652 /* Function prototypes */
2653 STATIC int INIT huft_build OF((unsigned *, unsigned, unsigned,
2654 - const ush *, const ush *, struct huft **, int *));
2655 + const uint16_t *, const uint16_t *, struct huft **, int *));
2656 STATIC int INIT huft_free OF((struct huft *));
2657 STATIC int INIT inflate_codes OF((struct huft *, struct huft *, int, int));
2658 STATIC int INIT inflate_stored OF((void));
2659 @@ -159,28 +217,28 @@ STATIC int INIT inflate OF((void));
2660 circular buffer. The index is updated simply by incrementing and then
2661 ANDing with 0x7fff (32K-1). */
2662 /* It is left to other modules to supply the 32 K area. It is assumed
2663 - to be usable as if it were declared "uch slide[32768];" or as just
2664 - "uch *slide;" and then malloc'ed in the latter case. The definition
2665 + to be usable as if it were declared "uint8_t slide[32768];" or as just
2666 + "uint8_t *slide;" and then malloc'ed in the latter case. The definition
2667 must be in unzip.h, included above. */
2668 /* unsigned wp; current position in slide */
2669 #define wp outcnt
2670 #define flush_output(w) (wp=(w),flush_window())
2671
2672 /* Tables for deflate from PKZIP's appnote.txt. */
2673 -static const unsigned border[] = { /* Order of the bit length code lengths */
2674 +static const unsigned border[] = { /* Order of the bit length code lengths */
2675 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
2676 -static const ush cplens[] = { /* Copy lengths for literal codes 257..285 */
2677 +static const uint16_t cplens[] = { /* Copy lengths for literal codes 257..285 */
2678 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
2679 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
2680 /* note: see note #13 above about the 258 in this list. */
2681 -static const ush cplext[] = { /* Extra bits for literal codes 257..285 */
2682 +static const uint16_t cplext[] = { /* Extra bits for literal codes 257..285 */
2683 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
2684 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99}; /* 99==invalid */
2685 -static const ush cpdist[] = { /* Copy offsets for distance codes 0..29 */
2686 +static const uint16_t cpdist[] = { /* Copy offsets for distance codes 0..29 */
2687 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
2688 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
2689 8193, 12289, 16385, 24577};
2690 -static const ush cpdext[] = { /* Extra bits for distance codes */
2691 +static const uint16_t cpdext[] = { /* Extra bits for distance codes */
2692 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
2693 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
2694 12, 12, 13, 13};
2695 @@ -217,59 +275,21 @@ static const ush cpdext[] = { /*
2696 the stream.
2697 */
2698
2699 -STATIC ulg bb; /* bit buffer */
2700 +STATIC uint32_t bb; /* bit buffer */
2701 STATIC unsigned bk; /* bits in bit buffer */
2702
2703 -STATIC const ush mask_bits[] = {
2704 +STATIC const uint16_t mask_bits[] = {
2705 0x0000,
2706 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
2707 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
2708 };
2709
2710 -#define NEXTBYTE() ({ int v = get_byte(); if (v < 0) goto underrun; (uch)v; })
2711 -#define NEEDBITS(n) {while(k<(n)){b|=((ulg)NEXTBYTE())<<k;k+=8;}}
2712 +#define NEXTBYTE() ({ int v = get_byte(); if (v < 0) goto underrun; \
2713 + (uint8_t)v; })
2714 +#define NEEDBITS(n) {while (k < (n)) \
2715 + {b |= ((uint32_t)NEXTBYTE())<<k; k += 8; } }
2716 #define DUMPBITS(n) {b>>=(n);k-=(n);}
2717
2718 -#ifndef NO_INFLATE_MALLOC
2719 -/* A trivial malloc implementation, adapted from
2720 - * malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994
2721 - */
2722 -
2723 -static unsigned long malloc_ptr;
2724 -static int malloc_count;
2725 -
2726 -static void *malloc(int size)
2727 -{
2728 - void *p;
2729 -
2730 - if (size < 0)
2731 - error("Malloc error");
2732 - if (!malloc_ptr)
2733 - malloc_ptr = free_mem_ptr;
2734 -
2735 - malloc_ptr = (malloc_ptr + 3) & ~3; /* Align */
2736 -
2737 - p = (void *)malloc_ptr;
2738 - malloc_ptr += size;
2739 -
2740 - if (free_mem_end_ptr && malloc_ptr >= free_mem_end_ptr)
2741 - error("Out of memory");
2742 -
2743 - malloc_count++;
2744 - return p;
2745 -}
2746 -
2747 -static void free(void *where)
2748 -{
2749 - malloc_count--;
2750 - if (!malloc_count)
2751 - malloc_ptr = free_mem_ptr;
2752 -}
2753 -#else
2754 -#define malloc(a) kmalloc(a, GFP_KERNEL)
2755 -#define free(a) kfree(a)
2756 -#endif
2757 -
2758 /*
2759 Huffman code decoding is performed using a multi-level table lookup.
2760 The fastest way to decode is to simply build a lookup table whose
2761 @@ -307,7 +327,7 @@ STATIC const int lbits = 9; /*
2762 STATIC const int dbits = 6; /* bits in base distance lookup table */
2763
2764
2765 -/* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
2766 +/* If BMAX needs to be larger than 16, then h and x[] should be uint32_t. */
2767 #define BMAX 16 /* maximum bit length of any code (16 for explode) */
2768 #define N_MAX 288 /* maximum number of codes in any set */
2769
2770 @@ -319,8 +339,8 @@ STATIC int INIT huft_build(
2771 unsigned *b, /* code lengths in bits (all assumed <= BMAX) */
2772 unsigned n, /* number of codes (assumed <= N_MAX) */
2773 unsigned s, /* number of simple-valued codes (0..s-1) */
2774 - const ush *d, /* list of base values for non-simple codes */
2775 - const ush *e, /* list of extra bits for non-simple codes */
2776 + const uint16_t *d, /* list of base values for non-simple codes */
2777 + const uint16_t *e, /* list of extra bits for non-simple codes */
2778 struct huft **t, /* result: starting table */
2779 int *m /* maximum lookup bits, returns actual */
2780 )
2781 @@ -500,8 +520,8 @@ DEBG1("5 ");
2782 if (h)
2783 {
2784 x[h] = i; /* save pattern for backing up */
2785 - r.b = (uch)l; /* bits to dump before this table */
2786 - r.e = (uch)(16 + j); /* bits in this table */
2787 + r.b = (uint8_t)l; /* bits to dump before this table */
2788 + r.e = (uint8_t)(16 + j); /* bits in this table */
2789 r.v.t = q; /* pointer to this table */
2790 j = i >> (w - l); /* (get around Turbo C bug) */
2791 u[h-1][j] = r; /* connect to last table */
2792 @@ -511,18 +531,18 @@ DEBG1("6 ");
2793 DEBG("h6c ");
2794
2795 /* set up table entry in r */
2796 - r.b = (uch)(k - w);
2797 + r.b = (uint8_t)(k - w);
2798 if (p >= v + n)
2799 r.e = 99; /* out of values--invalid code */
2800 else if (*p < s)
2801 {
2802 - r.e = (uch)(*p < 256 ? 16 : 15); /* 256 is end-of-block code */
2803 - r.v.n = (ush)(*p); /* simple code is just the value */
2804 + r.e = (uint8_t)(*p < 256 ? 16 : 15); /* 256 is end-of-block code */
2805 + r.v.n = (uint16_t)(*p); /* simple code is just the value */
2806 p++; /* one compiler does not like *p++ */
2807 }
2808 else
2809 {
2810 - r.e = (uch)e[*p - s]; /* non-simple--look up in lists */
2811 + r.e = (uint8_t)e[*p - s]; /* non-simple--look up in lists */
2812 r.v.n = d[*p++ - s];
2813 }
2814 DEBG("h6d ");
2815 @@ -592,11 +612,12 @@ STATIC int INIT inflate_codes(
2816 Return an error code or zero if it all goes ok. */
2817 {
2818 register unsigned e; /* table entry flag/number of extra bits */
2819 - unsigned n, d; /* length and index for copy */
2820 + unsigned n;
2821 + int d; /* source index for copy */
2822 unsigned w; /* current window position */
2823 struct huft *t; /* pointer to table entry */
2824 unsigned ml, md; /* masks for bl and bd bits */
2825 - register ulg b; /* bit buffer */
2826 + register uint32_t b; /* bit buffer */
2827 register unsigned k; /* number of bits in bit buffer */
2828
2829
2830 @@ -622,7 +643,7 @@ STATIC int INIT inflate_codes(
2831 DUMPBITS(t->b)
2832 if (e == 16) /* then it's a literal */
2833 {
2834 - slide[w++] = (uch)t->v.n;
2835 + slide[w++] = (uint8_t)t->v.n;
2836 Tracevv((stderr, "%c", slide[w-1]));
2837 if (w == WSIZE)
2838 {
2839 @@ -659,11 +680,25 @@ STATIC int INIT inflate_codes(
2840
2841 /* do the copy */
2842 do {
2843 - n -= (e = (e = WSIZE - ((d &= WSIZE-1) > w ? d : w)) > n ? n : e);
2844 +#ifdef NEW_CODE
2845 + if (flush_cb) {
2846 +#endif
2847 + /* Sliding window emulated using circular buffer:
2848 + * manage wrap-around */
2849 + e = WSIZE - ((d &= WSIZE-1) > w ? d : w);
2850 + if (e > n)
2851 + e = n;
2852 +#ifdef NEW_CODE
2853 + } else {
2854 + e = n;
2855 + }
2856 +#endif
2857 + n -= e;
2858 +
2859 #if !defined(NOMEMCPY) && !defined(DEBUG)
2860 if (w - d >= e) /* (this test assumes unsigned comparison) */
2861 {
2862 - memcpy(slide + w, slide + d, e);
2863 + memcpy(slide + w, slide + d, e);
2864 w += e;
2865 d += e;
2866 }
2867 @@ -673,9 +708,8 @@ STATIC int INIT inflate_codes(
2868 slide[w++] = slide[d++];
2869 Tracevv((stderr, "%c", slide[w-1]));
2870 } while (--e);
2871 - if (w == WSIZE)
2872 - {
2873 - flush_output(w);
2874 + if (w == WSIZE) {
2875 + flush_output(w);
2876 w = 0;
2877 }
2878 } while (n);
2879 @@ -702,7 +736,7 @@ STATIC int INIT inflate_stored(void)
2880 {
2881 unsigned n; /* number of bytes in block */
2882 unsigned w; /* current window position */
2883 - register ulg b; /* bit buffer */
2884 + register uint32_t b; /* bit buffer */
2885 register unsigned k; /* number of bits in bit buffer */
2886
2887 DEBG("<stor");
2888 @@ -732,7 +766,7 @@ DEBG("<stor");
2889 while (n--)
2890 {
2891 NEEDBITS(8)
2892 - slide[w++] = (uch)b;
2893 + slide[w++] = (uint8_t)b;
2894 if (w == WSIZE)
2895 {
2896 flush_output(w);
2897 @@ -838,7 +872,7 @@ STATIC int noinline INIT inflate_dynamic
2898 unsigned nl; /* number of literal/length codes */
2899 unsigned nd; /* number of distance codes */
2900 unsigned *ll; /* literal/length and distance code lengths */
2901 - register ulg b; /* bit buffer */
2902 + register uint32_t b; /* bit buffer */
2903 register unsigned k; /* number of bits in bit buffer */
2904 int ret;
2905
2906 @@ -1033,7 +1067,7 @@ STATIC int INIT inflate_block(
2907 /* decompress an inflated block */
2908 {
2909 unsigned t; /* block type */
2910 - register ulg b; /* bit buffer */
2911 + register uint32_t b; /* bit buffer */
2912 register unsigned k; /* number of bits in bit buffer */
2913
2914 DEBG("<blk");
2915 @@ -1130,8 +1164,8 @@ STATIC int INIT inflate(void)
2916 *
2917 **********************************************************************/
2918
2919 -static ulg crc_32_tab[256];
2920 -static ulg crc; /* initialized in makecrc() so it'll reside in bss */
2921 +static uint32_t crc_32_tab[256];
2922 +static uint32_t crc; /* initialized in makecrc() so it'll reside in bss */
2923 #define CRC_VALUE (crc ^ 0xffffffffUL)
2924
2925 /*
2926 @@ -1172,7 +1206,7 @@ makecrc(void)
2927 }
2928
2929 /* this is initialized here so this code could reside in ROM */
2930 - crc = (ulg)0xffffffffUL; /* shift register contents */
2931 + crc = (uint32_t)0xffffffffUL; /* shift register contents */
2932 }
2933
2934 /* gzip flag byte */
2935 @@ -1184,18 +1218,89 @@ makecrc(void)
2936 #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
2937 #define RESERVED 0xC0 /* bit 6,7: reserved */
2938
2939 +#ifdef NEW_CODE
2940 +/* ===========================================================================
2941 + * Write the output window window[0..outcnt-1] and update crc and bytes_out.
2942 + * (Used for the decompressed data only.)
2943 + */
2944 +static void INIT flush_window(void)
2945 +{
2946 + uint32_t c = crc; /* temporary variable */
2947 + unsigned n;
2948 + uint8_t *in, ch;
2949 +
2950 + in = window;
2951 + for (n = 0; n < outcnt; n++) {
2952 + ch = *in++;
2953 + c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
2954 + }
2955 + crc = c;
2956 + bytes_out += (uint32_t)outcnt;
2957 + if (flush_cb != NULL)
2958 + flush_cb(window, outcnt); /* TODO: handle unzip_error */
2959 + else
2960 + window += outcnt;
2961 + outcnt = 0;
2962 +}
2963 +
2964 +static int empty_fill(void *buf, unsigned int len)
2965 +{
2966 + return 0;
2967 +}
2968 +#endif
2969 +
2970 +
2971 /*
2972 * Do the uncompression!
2973 */
2974 -static int INIT gunzip(void)
2975 +STATIC int INIT gunzip(
2976 +#ifdef NEW_CODE
2977 + unsigned char *buf, int len,
2978 + int(*fill)(void*, unsigned int),
2979 + int(*flush)(void*, unsigned int),
2980 + unsigned char *output,
2981 + int *posp,
2982 + void(*error_fn)(char *x)
2983 +#else
2984 + void
2985 +#endif
2986 + )
2987 {
2988 - uch flags;
2989 + uint8_t flags;
2990 unsigned char magic[2]; /* magic header */
2991 char method;
2992 - ulg orig_crc = 0; /* original crc */
2993 - ulg orig_len = 0; /* original uncompressed length */
2994 + uint32_t orig_crc = 0; /* original crc */
2995 + uint32_t orig_len = 0; /* original uncompressed length */
2996 int res;
2997
2998 +#ifdef NEW_CODE
2999 + set_error_fn(error_fn);
3000 + if (fill == NULL)
3001 + fill_cb = empty_fill;
3002 + else
3003 + fill_cb = fill;
3004 + if (output)
3005 + window = output;
3006 + else {
3007 + window = malloc(0x8000);
3008 + if (!window)
3009 + panic("can't allocate buffers");
3010 + flush_cb = flush;
3011 + }
3012 +
3013 + insize = len;
3014 + if (buf)
3015 + inbuf = buf;
3016 + else
3017 + inbuf = malloc(INBUFSIZ);
3018 +#endif
3019 +
3020 + inptr = 0;
3021 + outcnt = 0; /* bytes in output buffer */
3022 + bytes_out = 0;
3023 + crc = (uint32_t)0xffffffffL; /* shift register contents */
3024 + makecrc();
3025 +
3026 magic[0] = NEXTBYTE();
3027 magic[1] = NEXTBYTE();
3028 method = NEXTBYTE();
3029 @@ -1212,7 +1317,7 @@ static int INIT gunzip(void)
3030 return -1;
3031 }
3032
3033 - flags = (uch)get_byte();
3034 + flags = (uint8_t)get_byte();
3035 if ((flags & ENCRYPTED) != 0) {
3036 error("Input is encrypted");
3037 return -1;
3038 @@ -1277,15 +1382,15 @@ static int INIT gunzip(void)
3039 /* crc32 (see algorithm.doc)
3040 * uncompressed input size modulo 2^32
3041 */
3042 - orig_crc = (ulg) NEXTBYTE();
3043 - orig_crc |= (ulg) NEXTBYTE() << 8;
3044 - orig_crc |= (ulg) NEXTBYTE() << 16;
3045 - orig_crc |= (ulg) NEXTBYTE() << 24;
3046 + orig_crc = (uint32_t) NEXTBYTE();
3047 + orig_crc |= (uint32_t) NEXTBYTE() << 8;
3048 + orig_crc |= (uint32_t) NEXTBYTE() << 16;
3049 + orig_crc |= (uint32_t) NEXTBYTE() << 24;
3050
3051 - orig_len = (ulg) NEXTBYTE();
3052 - orig_len |= (ulg) NEXTBYTE() << 8;
3053 - orig_len |= (ulg) NEXTBYTE() << 16;
3054 - orig_len |= (ulg) NEXTBYTE() << 24;
3055 + orig_len = (uint32_t) NEXTBYTE();
3056 + orig_len |= (uint32_t) NEXTBYTE() << 8;
3057 + orig_len |= (uint32_t) NEXTBYTE() << 16;
3058 + orig_len |= (uint32_t) NEXTBYTE() << 24;
3059
3060 /* Validate decompression */
3061 if (orig_crc != CRC_VALUE) {
3062 @@ -1296,11 +1401,22 @@ static int INIT gunzip(void)
3063 error("length error");
3064 return -1;
3065 }
3066 +#ifdef NEW_CODE
3067 + if (!output)
3068 + free(window);
3069 + if (posp)
3070 + *posp = inptr;
3071 +#endif
3072 return 0;
3073
3074 underrun: /* NEXTBYTE() goto's here if needed */
3075 + free(window);
3076 +#ifdef NEW_CODE
3077 + if (!buf)
3078 + free(inbuf);
3079 +#endif
3080 error("out of input data");
3081 return -1;
3082 }
3083
3084 -
3085 +#define decompress gunzip
3086 --- a/scripts/Makefile.lib
3087 +++ b/scripts/Makefile.lib
3088 @@ -183,3 +183,17 @@ quiet_cmd_gzip = GZIP $@
3089 cmd_gzip = gzip -f -9 < $< > $@
3090
3091
3092 +# Bzip2
3093 +# ---------------------------------------------------------------------------
3094 +
3095 +# Bzip2 does not include size in file... so we have to fake that
3096 +size_append=$(CONFIG_SHELL) $(srctree)/scripts/bin_size
3097 +
3098 +quiet_cmd_bzip2 = BZIP2 $@
3099 +cmd_bzip2 = (bzip2 -9 < $< ; $(size_append) $<) > $@ || (rm -f $@ ; false)
3100 +
3101 +# Lzma
3102 +# ---------------------------------------------------------------------------
3103 +
3104 +quiet_cmd_lzma = LZMA $@
3105 +cmd_lzma = (/usr/bin/lzma -9 -c $< ; $(size_append) $<) >$@ || (rm -f $@ ; false)
3106 --- /dev/null
3107 +++ b/scripts/bin_size
3108 @@ -0,0 +1,10 @@
3109 +#!/bin/sh
3110 +
3111 +if [ $# = 0 ] ; then
3112 + echo Usage: $0 file
3113 +fi
3114 +
3115 +size_dec=`stat -c "%s" $1`
3116 +size_hex_echo_string=`printf "%08x" $size_dec |
3117 + sed 's/\(..\)\(..\)\(..\)\(..\)/\\\\x\4\\\\x\3\\\\x\2\\\\x\1/g'`
3118 +/bin/echo -ne $size_hex_echo_string
This page took 0.168698 seconds and 5 git commands to generate.