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