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