wok-stable annotate linux/stuff/linux-lzma-2.6.24.2.u @ rev 521
Linux: split to linux, linux-acpi, linux-ieee1394, linux-sound.
Add: linux-reiserfs, linux-wireless
Fix modules post_install
Add: linux-reiserfs, linux-wireless
Fix modules post_install
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Wed Apr 09 08:49:36 2008 +0000 (2008-04-09) |
parents | a24feaeab23d |
children | 83af6305cb33 |
rev | line source |
---|---|
pascal@215 | 1 --- linux-2.6.24.2/arch/x86/boot/compressed/Makefile_32 |
pascal@215 | 2 +++ linux-2.6.24.2/arch/x86/boot/compressed/Makefile_32 |
pascal@215 | 3 @@ -4,7 +4,7 @@ |
pascal@215 | 4 # create a compressed vmlinux image from the original vmlinux |
pascal@215 | 5 # |
pascal@215 | 6 |
pascal@215 | 7 -targets := vmlinux vmlinux.bin vmlinux.bin.gz head_32.o misc_32.o piggy.o \ |
pascal@215 | 8 +targets := vmlinux vmlinux.bin vmlinux.bin.gz vmlinux.bin.bz2 vmlinux.bin.lzma head_32.o misc_32.o piggy.o \ |
pascal@215 | 9 vmlinux.bin.all vmlinux.relocs |
pascal@215 | 10 EXTRA_AFLAGS := -traditional |
pascal@215 | 11 |
pascal@215 | 12 @@ -44,7 +44,27 @@ $(obj)/vmlinux.bin.gz: $(obj)/vmlinux.bi |
pascal@215 | 13 $(call if_changed,gzip) |
pascal@215 | 14 endif |
pascal@215 | 15 |
pascal@215 | 16 +ifdef CONFIG_RELOCATABLE |
pascal@215 | 17 +$(obj)/vmlinux.bin.bz2: $(obj)/vmlinux.bin.all FORCE |
pascal@215 | 18 + $(call if_changed,bzip2) |
pascal@215 | 19 +else |
pascal@215 | 20 +$(obj)/vmlinux.bin.bz2: $(obj)/vmlinux.bin FORCE |
pascal@215 | 21 + $(call if_changed,bzip2) |
pascal@215 | 22 +endif |
pascal@215 | 23 + |
pascal@215 | 24 +ifdef CONFIG_RELOCATABLE |
pascal@215 | 25 +$(obj)/vmlinux.bin.lzma: $(obj)/vmlinux.bin.all FORCE |
pascal@215 | 26 + $(call if_changed,lzma) |
pascal@215 | 27 +else |
pascal@215 | 28 +$(obj)/vmlinux.bin.lzma: $(obj)/vmlinux.bin FORCE |
pascal@215 | 29 + $(call if_changed,lzma) |
pascal@215 | 30 +endif |
pascal@215 | 31 + |
pascal@215 | 32 LDFLAGS_piggy.o := -r --format binary --oformat elf32-i386 -T |
pascal@215 | 33 |
pascal@215 | 34 -$(obj)/piggy.o: $(src)/vmlinux_32.scr $(obj)/vmlinux.bin.gz FORCE |
pascal@215 | 35 +suffix_$(CONFIG_KERNEL_GZIP) = gz |
pascal@215 | 36 +suffix_$(CONFIG_KERNEL_BZIP2) = bz2 |
pascal@215 | 37 +suffix_$(CONFIG_KERNEL_LZMA) = lzma |
pascal@215 | 38 + |
pascal@215 | 39 +$(obj)/piggy.o: $(src)/vmlinux_32.scr $(obj)/vmlinux.bin.$(suffix_y) FORCE |
pascal@215 | 40 $(call if_changed,ld) |
pascal@243 | 41 |
pascal@215 | 42 --- linux-2.6.24.2/arch/x86/boot/compressed/Makefile_64 |
pascal@215 | 43 +++ linux-2.6.24.2/arch/x86/boot/compressed/Makefile_64 |
pascal@215 | 44 @@ -4,7 +4,7 @@ |
pascal@215 | 45 # create a compressed vmlinux image from the original vmlinux |
pascal@215 | 46 # |
pascal@215 | 47 |
pascal@215 | 48 -targets := vmlinux vmlinux.bin vmlinux.bin.gz head_64.o misc_64.o piggy.o |
pascal@215 | 49 +targets := vmlinux vmlinux.bin vmlinux.bin.gz vmlinux.bin.bz2 vmlinux.bin.lzma head_64.o misc_64.o piggy.o |
pascal@215 | 50 |
pascal@215 | 51 KBUILD_CFLAGS := -m64 -D__KERNEL__ $(LINUXINCLUDE) -O2 \ |
pascal@215 | 52 -fno-strict-aliasing -fPIC -mcmodel=small \ |
pascal@243 | 53 |
pascal@215 | 54 --- linux-2.6.24.2/arch/x86/boot/compressed/misc_32.c |
pascal@215 | 55 +++ linux-2.6.24.2/arch/x86/boot/compressed/misc_32.c |
pascal@215 | 56 @@ -121,9 +121,12 @@ typedef unsigned long ulg; |
pascal@215 | 57 * always be larger than our output buffer. |
pascal@215 | 58 */ |
pascal@215 | 59 |
pascal@215 | 60 +#ifdef CONFIG_KERNEL_GZIP |
pascal@215 | 61 static uch *inbuf; /* input buffer */ |
pascal@215 | 62 +#endif |
pascal@215 | 63 static uch *window; /* Sliding window buffer, (and final output buffer) */ |
pascal@215 | 64 |
pascal@215 | 65 +#ifdef CONFIG_KERNEL_GZIP |
pascal@215 | 66 static unsigned insize; /* valid bytes in inbuf */ |
pascal@215 | 67 static unsigned inptr; /* index of next byte to be processed in inbuf */ |
pascal@215 | 68 static unsigned outcnt; /* bytes in output buffer */ |
pascal@215 | 69 @@ -158,9 +161,14 @@ static unsigned outcnt; /* bytes in out |
pascal@215 | 70 |
pascal@215 | 71 static int fill_inbuf(void); |
pascal@215 | 72 static void flush_window(void); |
pascal@215 | 73 +#endif |
pascal@215 | 74 + |
pascal@215 | 75 static void error(char *m); |
pascal@215 | 76 + |
pascal@215 | 77 +#ifdef CONFIG_KERNEL_GZIP |
pascal@215 | 78 static void gzip_mark(void **); |
pascal@215 | 79 static void gzip_release(void **); |
pascal@215 | 80 +#endif |
pascal@215 | 81 |
pascal@215 | 82 /* |
pascal@215 | 83 * This is set up by the setup-routine at boot-time |
pascal@215 | 84 @@ -181,7 +189,9 @@ static long bytes_out = 0; |
pascal@215 | 85 static void *malloc(int size); |
pascal@215 | 86 static void free(void *where); |
pascal@215 | 87 |
pascal@215 | 88 +#if (defined CONFIG_KERNEL_GZIP || defined CONFIG_KERNEL_BZIP2) |
pascal@215 | 89 static void *memset(void *s, int c, unsigned n); |
pascal@215 | 90 +#endif |
pascal@215 | 91 static void *memcpy(void *dest, const void *src, unsigned n); |
pascal@215 | 92 |
pascal@215 | 93 static void putstr(const char *); |
pascal@215 | 94 @@ -189,7 +199,11 @@ static void putstr(const char *); |
pascal@215 | 95 static unsigned long free_mem_ptr; |
pascal@215 | 96 static unsigned long free_mem_end_ptr; |
pascal@215 | 97 |
pascal@215 | 98 +#if (defined CONFIG_KERNEL_BZIP2 || defined CONFIG_KERNEL_LZMA) |
pascal@215 | 99 +#define HEAP_SIZE 0x400000 |
pascal@215 | 100 +#else |
pascal@215 | 101 #define HEAP_SIZE 0x4000 |
pascal@215 | 102 +#endif |
pascal@215 | 103 |
pascal@215 | 104 static char *vidmem = (char *)0xb8000; |
pascal@215 | 105 static int vidport; |
pascal@215 | 106 @@ -199,7 +213,29 @@ static int lines, cols; |
pascal@215 | 107 void *xquad_portio; |
pascal@215 | 108 #endif |
pascal@215 | 109 |
pascal@215 | 110 +#if (defined CONFIG_KERNEL_BZIP2 || defined CONFIG_KERNEL_LZMA) |
pascal@215 | 111 + |
pascal@215 | 112 +#define large_malloc malloc |
pascal@215 | 113 +#define large_free free |
pascal@215 | 114 + |
pascal@215 | 115 +#ifdef current |
pascal@215 | 116 +#undef current |
pascal@215 | 117 +#endif |
pascal@215 | 118 + |
pascal@215 | 119 +#define INCLUDED |
pascal@215 | 120 +#endif |
pascal@215 | 121 + |
pascal@215 | 122 +#ifdef CONFIG_KERNEL_GZIP |
pascal@215 | 123 #include "../../../../lib/inflate.c" |
pascal@215 | 124 +#endif |
pascal@215 | 125 + |
pascal@215 | 126 +#ifdef CONFIG_KERNEL_BZIP2 |
pascal@215 | 127 +#include "../../../../lib/decompress_bunzip2.c" |
pascal@215 | 128 +#endif |
pascal@215 | 129 + |
pascal@215 | 130 +#ifdef CONFIG_KERNEL_LZMA |
pascal@215 | 131 +#include "../../../../lib/decompress_unlzma.c" |
pascal@215 | 132 +#endif |
pascal@215 | 133 |
pascal@215 | 134 static void *malloc(int size) |
pascal@215 | 135 { |
pascal@215 | 136 @@ -223,6 +259,7 @@ static void free(void *where) |
pascal@215 | 137 { /* Don't care */ |
pascal@215 | 138 } |
pascal@215 | 139 |
pascal@215 | 140 +#ifdef CONFIG_KERNEL_GZIP |
pascal@215 | 141 static void gzip_mark(void **ptr) |
pascal@215 | 142 { |
pascal@215 | 143 *ptr = (void *) free_mem_ptr; |
pascal@215 | 144 @@ -232,6 +269,7 @@ static void gzip_release(void **ptr) |
pascal@215 | 145 { |
pascal@215 | 146 free_mem_ptr = (unsigned long) *ptr; |
pascal@215 | 147 } |
pascal@215 | 148 +#endif |
pascal@215 | 149 |
pascal@215 | 150 static void scroll(void) |
pascal@215 | 151 { |
pascal@215 | 152 @@ -282,6 +320,7 @@ static void putstr(const char *s) |
pascal@215 | 153 outb_p(0xff & (pos >> 1), vidport+1); |
pascal@215 | 154 } |
pascal@215 | 155 |
pascal@215 | 156 +#if (defined CONFIG_KERNEL_GZIP || defined CONFIG_KERNEL_BZIP2) |
pascal@215 | 157 static void* memset(void* s, int c, unsigned n) |
pascal@215 | 158 { |
pascal@215 | 159 int i; |
pascal@215 | 160 @@ -290,6 +329,7 @@ static void* memset(void* s, int c, unsi |
pascal@215 | 161 for (i=0;i<n;i++) ss[i] = c; |
pascal@215 | 162 return s; |
pascal@215 | 163 } |
pascal@215 | 164 +#endif |
pascal@215 | 165 |
pascal@215 | 166 static void* memcpy(void* dest, const void* src, unsigned n) |
pascal@215 | 167 { |
pascal@215 | 168 @@ -300,6 +340,26 @@ static void* memcpy(void* dest, const vo |
pascal@215 | 169 return dest; |
pascal@215 | 170 } |
pascal@215 | 171 |
pascal@215 | 172 +#ifndef CONFIG_KERNEL_GZIP |
pascal@215 | 173 +/* =========================================================================== |
pascal@215 | 174 + * Write the output window window[0..outcnt-1] and update bytes_out. |
pascal@215 | 175 + * (Used for the decompressed data only.) |
pascal@215 | 176 + */ |
pascal@215 | 177 +static int compr_flush(char *data, unsigned int len) |
pascal@215 | 178 +{ |
pascal@215 | 179 + unsigned n; |
pascal@215 | 180 + uch *out; |
pascal@215 | 181 + |
pascal@215 | 182 + out = window; |
pascal@215 | 183 + for (n = 0; n < len; n++) { |
pascal@215 | 184 + *out++ = *data++; |
pascal@215 | 185 + } |
pascal@215 | 186 + bytes_out += (ulg)len; |
pascal@215 | 187 + window += (ulg)len; |
pascal@215 | 188 + return len; |
pascal@215 | 189 +} |
pascal@215 | 190 + |
pascal@215 | 191 +#else |
pascal@215 | 192 /* =========================================================================== |
pascal@215 | 193 * Fill the input buffer. This is called only when the buffer is empty |
pascal@215 | 194 * and at least one byte is really needed. |
pascal@215 | 195 @@ -332,6 +392,7 @@ static void flush_window(void) |
pascal@215 | 196 bytes_out += (ulg)outcnt; |
pascal@215 | 197 outcnt = 0; |
pascal@215 | 198 } |
pascal@215 | 199 +#endif |
pascal@215 | 200 |
pascal@215 | 201 static void error(char *x) |
pascal@215 | 202 { |
pascal@215 | 203 @@ -361,9 +422,11 @@ asmlinkage void decompress_kernel(void * |
pascal@215 | 204 window = output; /* Output buffer (Normally at 1M) */ |
pascal@215 | 205 free_mem_ptr = end; /* Heap */ |
pascal@215 | 206 free_mem_end_ptr = end + HEAP_SIZE; |
pascal@215 | 207 +#ifdef CONFIG_KERNEL_GZIP |
pascal@215 | 208 inbuf = input_data; /* Input buffer */ |
pascal@215 | 209 insize = input_len; |
pascal@215 | 210 inptr = 0; |
pascal@215 | 211 +#endif |
pascal@215 | 212 |
pascal@215 | 213 if ((u32)output & (CONFIG_PHYSICAL_ALIGN -1)) |
pascal@215 | 214 error("Destination address not CONFIG_PHYSICAL_ALIGN aligned"); |
pascal@215 | 215 @@ -374,9 +437,21 @@ asmlinkage void decompress_kernel(void * |
pascal@215 | 216 error("Wrong destination address"); |
pascal@215 | 217 #endif |
pascal@215 | 218 |
pascal@215 | 219 +#ifdef CONFIG_KERNEL_BZIP2 |
pascal@215 | 220 + putstr("Bunzipping Linux... "); |
pascal@215 | 221 + bunzip2(input_data, input_len-4, NULL, compr_flush, NULL); |
pascal@215 | 222 +#endif |
pascal@215 | 223 + |
pascal@215 | 224 +#ifdef CONFIG_KERNEL_LZMA |
pascal@215 | 225 + putstr("Unlzmaing Linux... "); |
pascal@215 | 226 + unlzma(input_data, input_len-4, NULL, compr_flush, NULL); |
pascal@215 | 227 +#endif |
pascal@215 | 228 + |
pascal@215 | 229 +#ifdef CONFIG_KERNEL_GZIP |
pascal@215 | 230 makecrc(); |
pascal@215 | 231 putstr("Uncompressing Linux... "); |
pascal@215 | 232 gunzip(); |
pascal@215 | 233 +#endif |
pascal@215 | 234 putstr("Ok, booting the kernel.\n"); |
pascal@215 | 235 return; |
pascal@215 | 236 } |
pascal@243 | 237 |
pascal@215 | 238 --- linux-2.6.24.2/arch/x86/boot/compressed/misc_64.c |
pascal@215 | 239 +++ linux-2.6.24.2/arch/x86/boot/compressed/misc_64.c |
pascal@215 | 240 @@ -121,9 +121,12 @@ typedef unsigned long ulg; |
pascal@215 | 241 * always be larger than our output buffer. |
pascal@215 | 242 */ |
pascal@215 | 243 |
pascal@215 | 244 +#ifdef CONFIG_KERNEL_GZIP |
pascal@215 | 245 static uch *inbuf; /* input buffer */ |
pascal@215 | 246 +#endif |
pascal@215 | 247 static uch *window; /* Sliding window buffer, (and final output buffer) */ |
pascal@215 | 248 |
pascal@215 | 249 +#ifdef CONFIG_KERNEL_GZIP |
pascal@215 | 250 static unsigned insize; /* valid bytes in inbuf */ |
pascal@215 | 251 static unsigned inptr; /* index of next byte to be processed in inbuf */ |
pascal@215 | 252 static unsigned outcnt; /* bytes in output buffer */ |
pascal@215 | 253 @@ -158,9 +161,14 @@ static unsigned outcnt; /* bytes in out |
pascal@215 | 254 |
pascal@215 | 255 static int fill_inbuf(void); |
pascal@215 | 256 static void flush_window(void); |
pascal@215 | 257 +#endif |
pascal@215 | 258 + |
pascal@215 | 259 static void error(char *m); |
pascal@215 | 260 + |
pascal@215 | 261 +#ifdef CONFIG_KERNEL_GZIP |
pascal@215 | 262 static void gzip_mark(void **); |
pascal@215 | 263 static void gzip_release(void **); |
pascal@215 | 264 +#endif |
pascal@215 | 265 |
pascal@215 | 266 /* |
pascal@215 | 267 * This is set up by the setup-routine at boot-time |
pascal@215 | 268 @@ -173,15 +181,19 @@ static unsigned char *real_mode; /* Poin |
pascal@215 | 269 #endif |
pascal@215 | 270 #define RM_SCREEN_INFO (*(struct screen_info *)(real_mode+0)) |
pascal@215 | 271 |
pascal@215 | 272 +#ifdef CONFIG_KERNEL_GZIP |
pascal@215 | 273 extern unsigned char input_data[]; |
pascal@215 | 274 extern int input_len; |
pascal@215 | 275 +#endif |
pascal@215 | 276 |
pascal@215 | 277 static long bytes_out = 0; |
pascal@215 | 278 |
pascal@215 | 279 static void *malloc(int size); |
pascal@215 | 280 static void free(void *where); |
pascal@215 | 281 |
pascal@215 | 282 +#if (defined CONFIG_KERNEL_GZIP || defined CONFIG_KERNEL_BZIP2) |
pascal@215 | 283 static void *memset(void *s, int c, unsigned n); |
pascal@215 | 284 +#endif |
pascal@215 | 285 static void *memcpy(void *dest, const void *src, unsigned n); |
pascal@215 | 286 |
pascal@215 | 287 static void putstr(const char *); |
pascal@215 | 288 @@ -189,13 +201,39 @@ static void putstr(const char *); |
pascal@215 | 289 static long free_mem_ptr; |
pascal@215 | 290 static long free_mem_end_ptr; |
pascal@215 | 291 |
pascal@215 | 292 +#if (defined CONFIG_KERNEL_BZIP2 || defined CONFIG_KERNEL_LZMA) |
pascal@215 | 293 +#define HEAP_SIZE 0x400000 |
pascal@215 | 294 +#else |
pascal@215 | 295 #define HEAP_SIZE 0x7000 |
pascal@215 | 296 +#endif |
pascal@215 | 297 |
pascal@215 | 298 static char *vidmem = (char *)0xb8000; |
pascal@215 | 299 static int vidport; |
pascal@215 | 300 static int lines, cols; |
pascal@215 | 301 |
pascal@215 | 302 +#if (defined CONFIG_KERNEL_BZIP2 || defined CONFIG_KERNEL_LZMA) |
pascal@215 | 303 + |
pascal@215 | 304 +#define large_malloc malloc |
pascal@215 | 305 +#define large_free free |
pascal@215 | 306 + |
pascal@215 | 307 +#ifdef current |
pascal@215 | 308 +#undef current |
pascal@215 | 309 +#endif |
pascal@215 | 310 + |
pascal@215 | 311 +#define INCLUDED |
pascal@215 | 312 +#endif |
pascal@215 | 313 + |
pascal@215 | 314 +#ifdef CONFIG_KERNEL_GZIP |
pascal@215 | 315 #include "../../../../lib/inflate.c" |
pascal@215 | 316 +#endif |
pascal@215 | 317 + |
pascal@215 | 318 +#ifdef CONFIG_KERNEL_BZIP2 |
pascal@215 | 319 +#include "../../../../lib/decompress_bunzip2.c" |
pascal@215 | 320 +#endif |
pascal@215 | 321 + |
pascal@215 | 322 +#ifdef CONFIG_KERNEL_LZMA |
pascal@215 | 323 +#include "../../../../lib/decompress_unlzma.c" |
pascal@215 | 324 +#endif |
pascal@215 | 325 |
pascal@215 | 326 static void *malloc(int size) |
pascal@215 | 327 { |
pascal@215 | 328 @@ -219,6 +257,7 @@ static void free(void *where) |
pascal@215 | 329 { /* Don't care */ |
pascal@215 | 330 } |
pascal@215 | 331 |
pascal@215 | 332 +#ifdef CONFIG_KERNEL_GZIP |
pascal@215 | 333 static void gzip_mark(void **ptr) |
pascal@215 | 334 { |
pascal@215 | 335 *ptr = (void *) free_mem_ptr; |
pascal@215 | 336 @@ -228,6 +267,7 @@ static void gzip_release(void **ptr) |
pascal@215 | 337 { |
pascal@215 | 338 free_mem_ptr = (long) *ptr; |
pascal@215 | 339 } |
pascal@215 | 340 +#endif |
pascal@215 | 341 |
pascal@215 | 342 static void scroll(void) |
pascal@215 | 343 { |
pascal@215 | 344 @@ -275,6 +315,7 @@ static void putstr(const char *s) |
pascal@215 | 345 outb_p(0xff & (pos >> 1), vidport+1); |
pascal@215 | 346 } |
pascal@215 | 347 |
pascal@215 | 348 +#if (defined CONFIG_KERNEL_GZIP || defined CONFIG_KERNEL_BZIP2) |
pascal@215 | 349 static void* memset(void* s, int c, unsigned n) |
pascal@215 | 350 { |
pascal@215 | 351 int i; |
pascal@215 | 352 @@ -283,6 +324,7 @@ static void* memset(void* s, int c, unsi |
pascal@215 | 353 for (i=0;i<n;i++) ss[i] = c; |
pascal@215 | 354 return s; |
pascal@215 | 355 } |
pascal@215 | 356 +#endif |
pascal@215 | 357 |
pascal@215 | 358 static void* memcpy(void* dest, const void* src, unsigned n) |
pascal@215 | 359 { |
pascal@215 | 360 @@ -293,6 +335,26 @@ static void* memcpy(void* dest, const vo |
pascal@215 | 361 return dest; |
pascal@215 | 362 } |
pascal@215 | 363 |
pascal@215 | 364 +#ifndef CONFIG_KERNEL_GZIP |
pascal@215 | 365 +/* =========================================================================== |
pascal@215 | 366 + * Write the output window window[0..outcnt-1] and update bytes_out. |
pascal@215 | 367 + * (Used for the decompressed data only.) |
pascal@215 | 368 + */ |
pascal@215 | 369 +static int compr_flush(char *data, unsigned int len) |
pascal@215 | 370 +{ |
pascal@215 | 371 + unsigned n; |
pascal@215 | 372 + uch *out; |
pascal@215 | 373 + |
pascal@215 | 374 + out = window; |
pascal@215 | 375 + for (n = 0; n < len; n++) { |
pascal@215 | 376 + *out++ = *data++; |
pascal@215 | 377 + } |
pascal@215 | 378 + bytes_out += (ulg)len; |
pascal@215 | 379 + window += (ulg)len; |
pascal@215 | 380 + return len; |
pascal@215 | 381 +} |
pascal@215 | 382 + |
pascal@215 | 383 +#else |
pascal@215 | 384 /* =========================================================================== |
pascal@215 | 385 * Fill the input buffer. This is called only when the buffer is empty |
pascal@215 | 386 * and at least one byte is really needed. |
pascal@215 | 387 @@ -325,6 +387,7 @@ static void flush_window(void) |
pascal@215 | 388 bytes_out += (ulg)outcnt; |
pascal@215 | 389 outcnt = 0; |
pascal@215 | 390 } |
pascal@215 | 391 +#endif |
pascal@215 | 392 |
pascal@215 | 393 static void error(char *x) |
pascal@215 | 394 { |
pascal@215 | 395 @@ -363,9 +426,21 @@ asmlinkage void decompress_kernel(void * |
pascal@215 | 396 if ((ulg)output >= 0xffffffffffUL) |
pascal@215 | 397 error("Destination address too large"); |
pascal@215 | 398 |
pascal@215 | 399 +#ifdef CONFIG_KERNEL_BZIP2 |
pascal@215 | 400 + putstr(".\nBunzipping Linux..."); |
pascal@215 | 401 + bunzip2(input_data, input_len-4, NULL, compr_flush, NULL); |
pascal@215 | 402 +#endif |
pascal@215 | 403 + |
pascal@215 | 404 +#ifdef CONFIG_KERNEL_LZMA |
pascal@215 | 405 + putstr(".\nUnlzmaing Linux..."); |
pascal@215 | 406 + unlzma(input_data, input_len-4, NULL, compr_flush, NULL); |
pascal@215 | 407 +#endif |
pascal@215 | 408 + |
pascal@215 | 409 +#ifdef CONFIG_KERNEL_GZIP |
pascal@215 | 410 makecrc(); |
pascal@215 | 411 putstr(".\nDecompressing Linux..."); |
pascal@215 | 412 gunzip(); |
pascal@215 | 413 +#endif |
pascal@215 | 414 putstr("done.\nBooting the kernel.\n"); |
pascal@215 | 415 return; |
pascal@215 | 416 } |
pascal@243 | 417 |
pascal@215 | 418 --- linux-2.6.24.2/drivers/block/Kconfig |
pascal@215 | 419 +++ linux-2.6.24.2/drivers/block/Kconfig |
pascal@215 | 420 @@ -356,6 +356,30 @@ config BLK_DEV_RAM_BLOCKSIZE |
pascal@215 | 421 setups function - apparently needed by the rd_load_image routine |
pascal@215 | 422 that supposes the filesystem in the image uses a 1024 blocksize. |
pascal@215 | 423 |
pascal@215 | 424 +config RD_BZIP2 |
pascal@215 | 425 + bool "Initial ramdisk compressed using bzip2" |
pascal@215 | 426 + default n |
pascal@215 | 427 + depends on BLK_DEV_INITRD=y |
pascal@215 | 428 + help |
pascal@215 | 429 + Support loading of a bzip2 encoded initial ramdisk or cpio buffer |
pascal@215 | 430 + If unsure, say N. |
pascal@215 | 431 + |
pascal@215 | 432 +config RD_LZMA |
pascal@215 | 433 + bool "Initial ramdisk compressed using lzma" |
pascal@215 | 434 + default n |
pascal@215 | 435 + depends on BLK_DEV_INITRD=y |
pascal@215 | 436 + help |
pascal@215 | 437 + Support loading of a lzma encoded initial ramdisk or cpio buffer |
pascal@215 | 438 + If unsure, say N. |
pascal@215 | 439 + |
pascal@215 | 440 +config RD_GZIP |
pascal@215 | 441 + bool "Initial ramdisk compressed using gzip" |
pascal@215 | 442 + default y |
pascal@215 | 443 + depends on BLK_DEV_INITRD=y |
pascal@215 | 444 + help |
pascal@215 | 445 + Support loading of a gzip encoded initial ramdisk or cpio buffer. |
pascal@215 | 446 + If unsure, say Y. |
pascal@215 | 447 + |
pascal@215 | 448 config CDROM_PKTCDVD |
pascal@215 | 449 tristate "Packet writing on CD/DVD media" |
pascal@215 | 450 depends on !UML |
pascal@243 | 451 |
pascal@215 | 452 --- linux-2.6.24.2/include/linux/decompress_bunzip2.h |
pascal@215 | 453 +++ linux-2.6.24.2/include/linux/decompress_bunzip2.h |
pascal@215 | 454 @@ -0,0 +1,16 @@ |
pascal@215 | 455 +#ifndef DECOMPRESS_BUNZIP2_H |
pascal@215 | 456 +#define DECOMPRESS_BUNZIP2_H |
pascal@215 | 457 + |
pascal@215 | 458 +/* Other housekeeping constants */ |
pascal@215 | 459 +#define BZIP2_IOBUF_SIZE 4096 |
pascal@215 | 460 + |
pascal@215 | 461 +#ifndef STATIC |
pascal@215 | 462 +#define STATIC /**/ |
pascal@215 | 463 +#endif |
pascal@215 | 464 + |
pascal@215 | 465 +STATIC int bunzip2(char *inbuf, int len, |
pascal@215 | 466 + int(*fill)(void*,unsigned int), |
pascal@215 | 467 + int(*writebb)(char*,unsigned int), |
pascal@215 | 468 + int *pos); |
pascal@215 | 469 + |
pascal@215 | 470 +#endif |
pascal@243 | 471 |
pascal@215 | 472 --- linux-2.6.24.2/include/linux/decompress_generic.h |
pascal@215 | 473 +++ linux-2.6.24.2/include/linux/decompress_generic.h |
pascal@215 | 474 @@ -0,0 +1,28 @@ |
pascal@215 | 475 +#ifndef DECOMPRESS_GENERIC_H |
pascal@215 | 476 +#define DECOMPRESS_GENERIC_H |
pascal@215 | 477 + |
pascal@215 | 478 +/* Minimal chunksize to be read. |
pascal@215 | 479 + * Bzip2 prefers at least 4096 |
pascal@215 | 480 + * Lzma prefers 0x10000 */ |
pascal@215 | 481 +#define COMPR_IOBUF_SIZE 4096 |
pascal@215 | 482 + |
pascal@215 | 483 +typedef int (*uncompress_fn) (char *inbuf, int len, |
pascal@215 | 484 + int(*fill)(char*,unsigned int), |
pascal@215 | 485 + int(*writebb)(char*,unsigned int), |
pascal@215 | 486 + int *posp); |
pascal@215 | 487 + |
pascal@215 | 488 +/* inbuf - input buffer |
pascal@215 | 489 + * len - len of pre-read data in inbuf |
pascal@215 | 490 + * fill - function to fill inbuf if empty |
pascal@215 | 491 + * writebb - function to write out outbug |
pascal@215 | 492 + * posp - if non-null, input position (number of bytes read) will be |
pascal@215 | 493 + * returned here |
pascal@215 | 494 + * |
pascal@215 | 495 + * If len != 0, the inbuf is initialized (with as much data), and fill |
pascal@215 | 496 + * should not be called |
pascal@215 | 497 + * If len = 0, the inbuf is allocated, but empty. Its size is IOBUF_SIZE |
pascal@215 | 498 + * fill should be called (repeatedly...) to read data, at most IOBUF_SIZE |
pascal@215 | 499 + */ |
pascal@215 | 500 + |
pascal@215 | 501 + |
pascal@215 | 502 +#endif |
pascal@243 | 503 |
pascal@215 | 504 --- linux-2.6.24.2/include/linux/decompress_unlzma.h |
pascal@215 | 505 +++ linux-2.6.24.2/include/linux/decompress_unlzma.h |
pascal@215 | 506 @@ -0,0 +1,15 @@ |
pascal@215 | 507 +#ifndef DECOMPRESS_UNLZMA_H |
pascal@215 | 508 +#define DECOMPRESS_UNLZMA_H |
pascal@215 | 509 + |
pascal@215 | 510 +#define LZMA_IOBUF_SIZE 0x10000 |
pascal@215 | 511 + |
pascal@215 | 512 +#ifndef STATIC |
pascal@215 | 513 +#define STATIC /**/ |
pascal@215 | 514 +#endif |
pascal@215 | 515 + |
pascal@215 | 516 +STATIC int unlzma(char *inbuf, int len, |
pascal@215 | 517 + int(*fill)(void*,unsigned int), |
pascal@215 | 518 + int(*writebb)(char*,unsigned int), |
pascal@215 | 519 + int *pos); |
pascal@215 | 520 + |
pascal@215 | 521 +#endif |
pascal@243 | 522 |
pascal@215 | 523 --- linux-2.6.24.2/init/do_mounts_rd.c |
pascal@215 | 524 +++ linux-2.6.24.2/init/do_mounts_rd.c |
pascal@215 | 525 @@ -8,6 +8,16 @@ |
pascal@215 | 526 #include <linux/initrd.h> |
pascal@215 | 527 #include <linux/string.h> |
pascal@215 | 528 |
pascal@215 | 529 +#ifdef CONFIG_RD_BZIP2 |
pascal@215 | 530 +#include <linux/decompress_bunzip2.h> |
pascal@215 | 531 +#undef STATIC |
pascal@215 | 532 +#endif |
pascal@215 | 533 + |
pascal@215 | 534 +#ifdef CONFIG_RD_LZMA |
pascal@215 | 535 +#include <linux/decompress_unlzma.h> |
pascal@215 | 536 +#undef STATIC |
pascal@215 | 537 +#endif |
pascal@215 | 538 + |
pascal@215 | 539 #include "do_mounts.h" |
pascal@215 | 540 |
pascal@215 | 541 #define BUILD_CRAMDISK |
pascal@215 | 542 @@ -30,7 +40,15 @@ static int __init ramdisk_start_setup(ch |
pascal@215 | 543 } |
pascal@215 | 544 __setup("ramdisk_start=", ramdisk_start_setup); |
pascal@215 | 545 |
pascal@215 | 546 +#ifdef CONFIG_RD_GZIP |
pascal@215 | 547 static int __init crd_load(int in_fd, int out_fd); |
pascal@215 | 548 +#endif |
pascal@215 | 549 +#ifdef CONFIG_RD_BZIP2 |
pascal@215 | 550 +static int __init crd_load_bzip2(int in_fd, int out_fd); |
pascal@215 | 551 +#endif |
pascal@215 | 552 +#ifdef CONFIG_RD_LZMA |
pascal@215 | 553 +static int __init crd_load_lzma(int in_fd, int out_fd); |
pascal@215 | 554 +#endif |
pascal@215 | 555 |
pascal@215 | 556 /* |
pascal@215 | 557 * This routine tries to find a RAM disk image to load, and returns the |
pascal@215 | 558 @@ -46,7 +64,7 @@ static int __init crd_load(int in_fd, in |
pascal@215 | 559 * gzip |
pascal@215 | 560 */ |
pascal@215 | 561 static int __init |
pascal@215 | 562 -identify_ramdisk_image(int fd, int start_block) |
pascal@215 | 563 +identify_ramdisk_image(int fd, int start_block, int *ztype) |
pascal@215 | 564 { |
pascal@215 | 565 const int size = 512; |
pascal@215 | 566 struct minix_super_block *minixsb; |
pascal@215 | 567 @@ -72,6 +90,7 @@ identify_ramdisk_image(int fd, int start |
pascal@215 | 568 sys_lseek(fd, start_block * BLOCK_SIZE, 0); |
pascal@215 | 569 sys_read(fd, buf, size); |
pascal@215 | 570 |
pascal@215 | 571 +#ifdef CONFIG_RD_GZIP |
pascal@215 | 572 /* |
pascal@215 | 573 * If it matches the gzip magic numbers, return -1 |
pascal@215 | 574 */ |
pascal@215 | 575 @@ -79,9 +98,40 @@ identify_ramdisk_image(int fd, int start |
pascal@215 | 576 printk(KERN_NOTICE |
pascal@215 | 577 "RAMDISK: Compressed image found at block %d\n", |
pascal@215 | 578 start_block); |
pascal@215 | 579 + *ztype = 0; |
pascal@215 | 580 + nblocks = 0; |
pascal@215 | 581 + goto done; |
pascal@215 | 582 + } |
pascal@215 | 583 +#endif |
pascal@215 | 584 + |
pascal@215 | 585 +#ifdef CONFIG_RD_BZIP2 |
pascal@215 | 586 + /* |
pascal@215 | 587 + * If it matches the bzip magic numbers, return -1 |
pascal@215 | 588 + */ |
pascal@215 | 589 + if (buf[0] == 0x42 && (buf[1] == 0x5a)) { |
pascal@215 | 590 + printk(KERN_NOTICE |
pascal@215 | 591 + "RAMDISK: Bzipped image found at block %d\n", |
pascal@215 | 592 + start_block); |
pascal@215 | 593 + *ztype = 1; |
pascal@215 | 594 + nblocks = 0; |
pascal@215 | 595 + goto done; |
pascal@215 | 596 + } |
pascal@215 | 597 +#endif |
pascal@215 | 598 + |
pascal@215 | 599 +#ifdef CONFIG_RD_LZMA |
pascal@215 | 600 + /* |
pascal@215 | 601 + * If it matches the bzip magic numbers, return -1 |
pascal@215 | 602 + */ |
pascal@215 | 603 + if (buf[0] == 0x5d && (buf[1] == 0x00)) { |
pascal@215 | 604 + printk(KERN_NOTICE |
pascal@215 | 605 + "RAMDISK: Lzma image found at block %d\n", |
pascal@215 | 606 + start_block); |
pascal@215 | 607 + *ztype = 2; |
pascal@215 | 608 nblocks = 0; |
pascal@215 | 609 goto done; |
pascal@215 | 610 } |
pascal@215 | 611 +#endif |
pascal@215 | 612 + |
pascal@215 | 613 |
pascal@215 | 614 /* romfs is at block zero too */ |
pascal@215 | 615 if (romfsb->word0 == ROMSB_WORD0 && |
pascal@215 | 616 @@ -145,6 +195,7 @@ int __init rd_load_image(char *from) |
pascal@215 | 617 int nblocks, i, disk; |
pascal@215 | 618 char *buf = NULL; |
pascal@215 | 619 unsigned short rotate = 0; |
pascal@215 | 620 + int ztype=-1; |
pascal@215 | 621 #if !defined(CONFIG_S390) && !defined(CONFIG_PPC_ISERIES) |
pascal@215 | 622 char rotator[4] = { '|' , '/' , '-' , '\\' }; |
pascal@215 | 623 #endif |
pascal@215 | 624 @@ -157,14 +208,38 @@ int __init rd_load_image(char *from) |
pascal@215 | 625 if (in_fd < 0) |
pascal@215 | 626 goto noclose_input; |
pascal@215 | 627 |
pascal@215 | 628 - nblocks = identify_ramdisk_image(in_fd, rd_image_start); |
pascal@215 | 629 + nblocks = identify_ramdisk_image(in_fd, rd_image_start, &ztype); |
pascal@215 | 630 if (nblocks < 0) |
pascal@215 | 631 goto done; |
pascal@215 | 632 |
pascal@215 | 633 if (nblocks == 0) { |
pascal@215 | 634 #ifdef BUILD_CRAMDISK |
pascal@215 | 635 - if (crd_load(in_fd, out_fd) == 0) |
pascal@215 | 636 - goto successful_load; |
pascal@215 | 637 + switch(ztype) { |
pascal@215 | 638 + |
pascal@215 | 639 +#ifdef CONFIG_RD_GZIP |
pascal@215 | 640 + case 0: |
pascal@215 | 641 + if (crd_load(in_fd, out_fd) == 0) |
pascal@215 | 642 + goto successful_load; |
pascal@215 | 643 + break; |
pascal@215 | 644 +#endif |
pascal@215 | 645 + |
pascal@215 | 646 +#ifdef CONFIG_RD_BZIP2 |
pascal@215 | 647 + case 1: |
pascal@215 | 648 + if (crd_load_bzip2(in_fd, out_fd) == 0) |
pascal@215 | 649 + goto successful_load; |
pascal@215 | 650 + break; |
pascal@215 | 651 +#endif |
pascal@215 | 652 + |
pascal@215 | 653 +#ifdef CONFIG_RD_LZMA |
pascal@215 | 654 + case 2: |
pascal@215 | 655 + if (crd_load_lzma(in_fd, out_fd) == 0) |
pascal@215 | 656 + goto successful_load; |
pascal@215 | 657 + break; |
pascal@215 | 658 +#endif |
pascal@215 | 659 + |
pascal@215 | 660 + default: |
pascal@215 | 661 + break; |
pascal@215 | 662 + } |
pascal@215 | 663 #else |
pascal@215 | 664 printk(KERN_NOTICE |
pascal@215 | 665 "RAMDISK: Kernel does not support compressed " |
pascal@215 | 666 @@ -269,6 +344,7 @@ int __init rd_load_disk(int n) |
pascal@215 | 667 |
pascal@215 | 668 #ifdef BUILD_CRAMDISK |
pascal@215 | 669 |
pascal@215 | 670 +#ifdef CONFIG_RD_GZIP |
pascal@215 | 671 /* |
pascal@215 | 672 * gzip declarations |
pascal@215 | 673 */ |
pascal@215 | 674 @@ -296,8 +372,11 @@ static unsigned outcnt; /* bytes in out |
pascal@215 | 675 static int exit_code; |
pascal@215 | 676 static int unzip_error; |
pascal@215 | 677 static long bytes_out; |
pascal@215 | 678 +#endif |
pascal@215 | 679 + |
pascal@215 | 680 static int crd_infd, crd_outfd; |
pascal@215 | 681 |
pascal@215 | 682 +#ifdef CONFIG_RD_GZIP |
pascal@215 | 683 #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf()) |
pascal@215 | 684 |
pascal@215 | 685 /* Diagnostic functions (stubbed out) */ |
pascal@215 | 686 @@ -359,7 +438,22 @@ static int __init fill_inbuf(void) |
pascal@215 | 687 |
pascal@215 | 688 return inbuf[0]; |
pascal@215 | 689 } |
pascal@215 | 690 +#endif |
pascal@215 | 691 + |
pascal@215 | 692 +#if (defined CONFIG_RD_BZIP2 || defined CONFIG_RD_LZMA) |
pascal@215 | 693 +static int __init compr_fill(void *buf, unsigned int len) |
pascal@215 | 694 +{ |
pascal@215 | 695 + int r = sys_read(crd_infd, buf, len); |
pascal@215 | 696 + if(r < 0) { |
pascal@215 | 697 + printk(KERN_ERR "RAMDISK: error while reading compressed data"); |
pascal@215 | 698 + } else if(r == 0) { |
pascal@215 | 699 + printk(KERN_ERR "RAMDISK: EOF while reading compressed data"); |
pascal@215 | 700 + } |
pascal@215 | 701 + return r; |
pascal@215 | 702 +} |
pascal@215 | 703 +#endif |
pascal@215 | 704 |
pascal@215 | 705 +#ifdef CONFIG_RD_GZIP |
pascal@215 | 706 /* =========================================================================== |
pascal@215 | 707 * Write the output window window[0..outcnt-1] and update crc and bytes_out. |
pascal@215 | 708 * (Used for the decompressed data only.) |
pascal@215 | 709 @@ -385,7 +479,24 @@ static void __init flush_window(void) |
pascal@215 | 710 bytes_out += (ulg)outcnt; |
pascal@215 | 711 outcnt = 0; |
pascal@215 | 712 } |
pascal@215 | 713 +#endif |
pascal@215 | 714 + |
pascal@215 | 715 +#if (defined CONFIG_RD_BZIP2 || defined CONFIG_RD_LZMA) |
pascal@215 | 716 +static int __init compr_flush(void *window, unsigned int outcnt) { |
pascal@215 | 717 + static int progressDots=0; |
pascal@215 | 718 + int written = sys_write(crd_outfd, window, outcnt); |
pascal@215 | 719 + if (written != outcnt) { |
pascal@215 | 720 + printk(KERN_ERR "RAMDISK: incomplete write (%d != %d)\n", |
pascal@215 | 721 + written, outcnt); |
pascal@215 | 722 + } |
pascal@215 | 723 + progressDots = (progressDots+1)%10; |
pascal@215 | 724 + if(!progressDots) |
pascal@215 | 725 + printk("."); |
pascal@215 | 726 + return outcnt; |
pascal@215 | 727 +} |
pascal@215 | 728 +#endif |
pascal@215 | 729 |
pascal@215 | 730 +#ifdef CONFIG_RD_GZIP |
pascal@215 | 731 static void __init error(char *x) |
pascal@215 | 732 { |
pascal@215 | 733 printk(KERN_ERR "%s\n", x); |
pascal@215 | 734 @@ -425,5 +536,43 @@ static int __init crd_load(int in_fd, in |
pascal@215 | 735 kfree(window); |
pascal@215 | 736 return result; |
pascal@215 | 737 } |
pascal@215 | 738 +#endif |
pascal@215 | 739 + |
pascal@215 | 740 +#if (defined CONFIG_RD_BZIP2 || defined CONFIG_RD_LZMA) |
pascal@215 | 741 +static int __init crd_load_compr(int in_fd, int out_fd, int size, |
pascal@215 | 742 + int (*deco)(char *,int, |
pascal@215 | 743 + int(*fill)(void*,unsigned int), |
pascal@215 | 744 + int(*flush)(void*,unsigned int), |
pascal@215 | 745 + int *)) |
pascal@215 | 746 +{ |
pascal@215 | 747 + int result; |
pascal@215 | 748 + char *inbuf = kmalloc(size, GFP_KERNEL); |
pascal@215 | 749 + crd_infd = in_fd; |
pascal@215 | 750 + crd_outfd = out_fd; |
pascal@215 | 751 + if (inbuf == 0) { |
pascal@215 | 752 + printk(KERN_ERR "RAMDISK: Couldn't allocate decompression buffer\n"); |
pascal@215 | 753 + return -1; |
pascal@215 | 754 + } |
pascal@215 | 755 + result=deco(inbuf, 0, compr_fill, compr_flush, NULL); |
pascal@215 | 756 + kfree(inbuf); |
pascal@215 | 757 + printk("\n"); |
pascal@215 | 758 + return result; |
pascal@215 | 759 +} |
pascal@215 | 760 +#endif |
pascal@215 | 761 + |
pascal@215 | 762 +#ifdef CONFIG_RD_BZIP2 |
pascal@215 | 763 +static int __init crd_load_bzip2(int in_fd, int out_fd) |
pascal@215 | 764 +{ |
pascal@215 | 765 + return crd_load_compr(in_fd, out_fd, BZIP2_IOBUF_SIZE, bunzip2); |
pascal@215 | 766 +} |
pascal@215 | 767 +#endif |
pascal@215 | 768 + |
pascal@215 | 769 +#ifdef CONFIG_RD_LZMA |
pascal@215 | 770 +static int __init crd_load_lzma(int in_fd, int out_fd) |
pascal@215 | 771 +{ |
pascal@215 | 772 + return crd_load_compr(in_fd, out_fd, LZMA_IOBUF_SIZE, unlzma); |
pascal@215 | 773 +} |
pascal@215 | 774 + |
pascal@215 | 775 +#endif |
pascal@215 | 776 |
pascal@215 | 777 #endif /* BUILD_CRAMDISK */ |
pascal@243 | 778 |
pascal@215 | 779 --- linux-2.6.24.2/init/initramfs.c |
pascal@215 | 780 +++ linux-2.6.24.2/init/initramfs.c |
pascal@215 | 781 @@ -7,6 +7,15 @@ |
pascal@215 | 782 #include <linux/string.h> |
pascal@215 | 783 #include <linux/syscalls.h> |
pascal@215 | 784 |
pascal@215 | 785 +/* We need to enable RD_GZIP unconditionnally, as the built-in |
pascal@215 | 786 + * initramfs is gzip-compressed, alas! |
pascal@215 | 787 + * We can only wonder why, though, as the whole kernel (which contains |
pascal@215 | 788 + * built-in initramfs) is gzip (or bzip) compressed anyways afterwards... |
pascal@215 | 789 + */ |
pascal@215 | 790 +#ifndef CONFIG_RD_GZIP |
pascal@215 | 791 +#define CONFIG_RD_GZIP |
pascal@215 | 792 +#endif |
pascal@215 | 793 + |
pascal@215 | 794 static __initdata char *message; |
pascal@215 | 795 static void __init error(char *x) |
pascal@215 | 796 { |
pascal@215 | 797 @@ -347,11 +356,13 @@ static int __init write_buffer(char *buf |
pascal@215 | 798 return len - count; |
pascal@215 | 799 } |
pascal@215 | 800 |
pascal@215 | 801 -static void __init flush_buffer(char *buf, unsigned len) |
pascal@215 | 802 + |
pascal@215 | 803 +static int __init flush_buffer(char *buf, unsigned len) |
pascal@215 | 804 { |
pascal@215 | 805 int written; |
pascal@215 | 806 + int origLen = len; |
pascal@215 | 807 if (message) |
pascal@215 | 808 - return; |
pascal@215 | 809 + return -1; |
pascal@215 | 810 while ((written = write_buffer(buf, len)) < len && !message) { |
pascal@215 | 811 char c = buf[written]; |
pascal@215 | 812 if (c == '0') { |
pascal@215 | 813 @@ -365,8 +376,24 @@ static void __init flush_buffer(char *bu |
pascal@215 | 814 } else |
pascal@215 | 815 error("junk in compressed archive"); |
pascal@215 | 816 } |
pascal@215 | 817 + return origLen; |
pascal@215 | 818 } |
pascal@215 | 819 |
pascal@215 | 820 +#ifdef CONFIG_RD_BZIP2 |
pascal@215 | 821 +#include <linux/decompress_bunzip2.h> |
pascal@215 | 822 +#undef STATIC |
pascal@215 | 823 + |
pascal@215 | 824 +#endif |
pascal@215 | 825 + |
pascal@215 | 826 +#ifdef CONFIG_RD_LZMA |
pascal@215 | 827 +#include <linux/decompress_unlzma.h> |
pascal@215 | 828 +#undef STATIC |
pascal@215 | 829 + |
pascal@215 | 830 +#endif |
pascal@215 | 831 + |
pascal@215 | 832 +static unsigned inptr; /* index of next byte to be processed in inbuf */ |
pascal@215 | 833 + |
pascal@215 | 834 +#ifdef CONFIG_RD_GZIP |
pascal@215 | 835 /* |
pascal@215 | 836 * gzip declarations |
pascal@215 | 837 */ |
pascal@215 | 838 @@ -388,7 +415,6 @@ static uch *inbuf; |
pascal@215 | 839 static uch *window; |
pascal@215 | 840 |
pascal@215 | 841 static unsigned insize; /* valid bytes in inbuf */ |
pascal@215 | 842 -static unsigned inptr; /* index of next byte to be processed in inbuf */ |
pascal@215 | 843 static unsigned outcnt; /* bytes in output buffer */ |
pascal@215 | 844 static long bytes_out; |
pascal@215 | 845 |
pascal@215 | 846 @@ -440,6 +466,7 @@ static void __init flush_window(void) |
pascal@215 | 847 bytes_out += (ulg)outcnt; |
pascal@215 | 848 outcnt = 0; |
pascal@215 | 849 } |
pascal@215 | 850 +#endif |
pascal@215 | 851 |
pascal@215 | 852 static char * __init unpack_to_rootfs(char *buf, unsigned len, int check_only) |
pascal@215 | 853 { |
pascal@215 | 854 @@ -448,9 +475,11 @@ static char * __init unpack_to_rootfs(ch |
pascal@215 | 855 header_buf = malloc(110); |
pascal@215 | 856 symlink_buf = malloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1); |
pascal@215 | 857 name_buf = malloc(N_ALIGN(PATH_MAX)); |
pascal@215 | 858 +#ifdef CONFIG_RD_GZIP |
pascal@215 | 859 window = malloc(WSIZE); |
pascal@215 | 860 if (!window || !header_buf || !symlink_buf || !name_buf) |
pascal@215 | 861 panic("can't allocate buffers"); |
pascal@215 | 862 +#endif |
pascal@215 | 863 state = Start; |
pascal@215 | 864 this_header = 0; |
pascal@215 | 865 message = NULL; |
pascal@215 | 866 @@ -470,6 +499,7 @@ static char * __init unpack_to_rootfs(ch |
pascal@215 | 867 continue; |
pascal@215 | 868 } |
pascal@215 | 869 this_header = 0; |
pascal@215 | 870 +#ifdef CONFIG_RD_GZIP |
pascal@215 | 871 insize = len; |
pascal@215 | 872 inbuf = buf; |
pascal@215 | 873 inptr = 0; |
pascal@215 | 874 @@ -477,14 +507,38 @@ static char * __init unpack_to_rootfs(ch |
pascal@215 | 875 bytes_out = 0; |
pascal@215 | 876 crc = (ulg)0xffffffffL; /* shift register contents */ |
pascal@215 | 877 makecrc(); |
pascal@215 | 878 - gunzip(); |
pascal@215 | 879 + if(!gunzip() && message == NULL) |
pascal@215 | 880 + goto ok; |
pascal@215 | 881 +#endif |
pascal@215 | 882 + |
pascal@215 | 883 +#ifdef CONFIG_RD_BZIP2 |
pascal@215 | 884 + message = NULL; /* Zero out message, or else cpio will |
pascal@215 | 885 + think an error has already occured */ |
pascal@215 | 886 + if(!bunzip2(buf, len, NULL, flush_buffer, &inptr) < 0 && |
pascal@215 | 887 + message == NULL) { |
pascal@215 | 888 + goto ok; |
pascal@215 | 889 + } |
pascal@215 | 890 +#endif |
pascal@215 | 891 + |
pascal@215 | 892 +#ifdef CONFIG_RD_LZMA |
pascal@215 | 893 + message = NULL; /* Zero out message, or else cpio will |
pascal@215 | 894 + think an error has already occured */ |
pascal@215 | 895 + if(!unlzma(buf, len, NULL, flush_buffer, &inptr) < 0 && |
pascal@215 | 896 + message == NULL) { |
pascal@215 | 897 + goto ok; |
pascal@215 | 898 + } |
pascal@215 | 899 +#endif |
pascal@215 | 900 + ok: |
pascal@215 | 901 + |
pascal@215 | 902 if (state != Reset) |
pascal@215 | 903 - error("junk in gzipped archive"); |
pascal@215 | 904 + error("junk in compressed archive"); |
pascal@215 | 905 this_header = saved_offset + inptr; |
pascal@215 | 906 buf += inptr; |
pascal@215 | 907 len -= inptr; |
pascal@215 | 908 } |
pascal@215 | 909 +#ifdef CONFIG_RD_GZIP |
pascal@215 | 910 free(window); |
pascal@215 | 911 +#endif |
pascal@215 | 912 free(name_buf); |
pascal@215 | 913 free(symlink_buf); |
pascal@215 | 914 free(header_buf); |
pascal@243 | 915 |
pascal@215 | 916 --- linux-2.6.24.2/init/Kconfig |
pascal@215 | 917 +++ linux-2.6.24.2/init/Kconfig |
pascal@215 | 918 @@ -92,6 +92,56 @@ config LOCALVERSION_AUTO |
pascal@215 | 919 |
pascal@215 | 920 which is done within the script "scripts/setlocalversion".) |
pascal@215 | 921 |
pascal@215 | 922 +choice |
pascal@215 | 923 + prompt "Kernel compression mode" |
pascal@215 | 924 + default KERNEL_GZIP |
pascal@215 | 925 + help |
pascal@215 | 926 + The linux kernel is a kind of self-extracting executable. |
pascal@215 | 927 + Several compression algorithms are available, which differ |
pascal@215 | 928 + in efficiency, compression and decompression speed. |
pascal@215 | 929 + Compression speed is only relevant when building a kernel. |
pascal@215 | 930 + Decompression speed is relevant at each boot. |
pascal@215 | 931 + |
pascal@215 | 932 + If you have any problems with bzip2 or lzma compressed |
pascal@215 | 933 + kernels, mail me (Alain Knaff) <alain@knaff.lu>. (An older |
pascal@215 | 934 + version of this functionality (bzip2 only), for 2.4, was |
pascal@215 | 935 + supplied by Christian Ludwig) |
pascal@215 | 936 + |
pascal@215 | 937 + High compression options are mostly useful for users, who |
pascal@215 | 938 + are low on disk space (embedded systems), but for whom ram |
pascal@215 | 939 + size matters less. |
pascal@215 | 940 + |
pascal@215 | 941 + If in doubt, select 'gzip' |
pascal@215 | 942 + |
pascal@215 | 943 +config KERNEL_GZIP |
pascal@215 | 944 + bool "Gzip" |
pascal@215 | 945 + help |
pascal@215 | 946 + The old and tries gzip compression. Its compression ratio is |
pascal@215 | 947 + the poorest among the 3 choices; however its speed (both |
pascal@215 | 948 + compression and decompression) is the fastest. |
pascal@215 | 949 + |
pascal@215 | 950 +config KERNEL_BZIP2 |
pascal@215 | 951 + bool "Bzip2" |
pascal@215 | 952 + help |
pascal@215 | 953 + Its compression ratio and speed is intermediate. |
pascal@215 | 954 + Decompression speed is slowest among the 3. |
pascal@215 | 955 + The kernel size is about 10 per cent smaller with bzip2, |
pascal@215 | 956 + in comparison to gzip. |
pascal@215 | 957 + Bzip2 uses a large amount of memory. For modern kernels |
pascal@215 | 958 + you will need at least 8MB RAM or more for booting. |
pascal@215 | 959 + |
pascal@215 | 960 +config KERNEL_LZMA |
pascal@215 | 961 + bool "LZMA" |
pascal@215 | 962 + help |
pascal@215 | 963 + The most recent compression algorithm. |
pascal@215 | 964 + Its ratio is best, decompression speed is between the other |
pascal@215 | 965 + 2. Compression is slowest. |
pascal@215 | 966 + The kernel size is about 33 per cent smaller with lzma, |
pascal@215 | 967 + in comparison to gzip. |
pascal@215 | 968 + |
pascal@215 | 969 +endchoice |
pascal@215 | 970 + |
pascal@215 | 971 + |
pascal@215 | 972 config SWAP |
pascal@215 | 973 bool "Support for paging of anonymous memory (swap)" |
pascal@215 | 974 depends on MMU && BLOCK |
pascal@243 | 975 |
pascal@215 | 976 --- linux-2.6.24.2/lib/decompress_bunzip2.c |
pascal@215 | 977 +++ linux-2.6.24.2/lib/decompress_bunzip2.c |
pascal@215 | 978 @@ -0,0 +1,645 @@ |
pascal@215 | 979 +/* vi: set sw=4 ts=4: */ |
pascal@215 | 980 +/* Small bzip2 deflate implementation, by Rob Landley (rob@landley.net). |
pascal@215 | 981 + |
pascal@215 | 982 + Based on bzip2 decompression code by Julian R Seward (jseward@acm.org), |
pascal@215 | 983 + which also acknowledges contributions by Mike Burrows, David Wheeler, |
pascal@215 | 984 + Peter Fenwick, Alistair Moffat, Radford Neal, Ian H. Witten, |
pascal@215 | 985 + Robert Sedgewick, and Jon L. Bentley. |
pascal@215 | 986 + |
pascal@215 | 987 + This code is licensed under the LGPLv2: |
pascal@215 | 988 + LGPL (http://www.gnu.org/copyleft/lgpl.html |
pascal@215 | 989 +*/ |
pascal@215 | 990 + |
pascal@215 | 991 +/* |
pascal@215 | 992 + Size and speed optimizations by Manuel Novoa III (mjn3@codepoet.org). |
pascal@215 | 993 + |
pascal@215 | 994 + More efficient reading of Huffman codes, a streamlined read_bunzip() |
pascal@215 | 995 + function, and various other tweaks. In (limited) tests, approximately |
pascal@215 | 996 + 20% faster than bzcat on x86 and about 10% faster on arm. |
pascal@215 | 997 + |
pascal@215 | 998 + Note that about 2/3 of the time is spent in read_unzip() reversing |
pascal@215 | 999 + the Burrows-Wheeler transformation. Much of that time is delay |
pascal@215 | 1000 + resulting from cache misses. |
pascal@215 | 1001 + |
pascal@215 | 1002 + I would ask that anyone benefiting from this work, especially those |
pascal@215 | 1003 + using it in commercial products, consider making a donation to my local |
pascal@215 | 1004 + non-profit hospice organization in the name of the woman I loved, who |
pascal@215 | 1005 + passed away Feb. 12, 2003. |
pascal@215 | 1006 + |
pascal@215 | 1007 + In memory of Toni W. Hagan |
pascal@215 | 1008 + |
pascal@215 | 1009 + Hospice of Acadiana, Inc. |
pascal@215 | 1010 + 2600 Johnston St., Suite 200 |
pascal@215 | 1011 + Lafayette, LA 70503-3240 |
pascal@215 | 1012 + |
pascal@215 | 1013 + Phone (337) 232-1234 or 1-800-738-2226 |
pascal@215 | 1014 + Fax (337) 232-1297 |
pascal@215 | 1015 + |
pascal@215 | 1016 + http://www.hospiceacadiana.com/ |
pascal@215 | 1017 + |
pascal@215 | 1018 + Manuel |
pascal@215 | 1019 + */ |
pascal@215 | 1020 + |
pascal@215 | 1021 +/* |
pascal@215 | 1022 + Made it fit for running in Linux Kernel by Alain Knaff (alain@knaff.lu) |
pascal@215 | 1023 +*/ |
pascal@215 | 1024 + |
pascal@215 | 1025 + |
pascal@215 | 1026 +#ifndef STATIC |
pascal@215 | 1027 + |
pascal@215 | 1028 +#include <linux/kernel.h> |
pascal@215 | 1029 +#include <linux/fs.h> |
pascal@215 | 1030 +#include <linux/string.h> |
pascal@215 | 1031 + |
pascal@215 | 1032 +#ifdef TEST |
pascal@215 | 1033 +#include "test.h" |
pascal@215 | 1034 +#else |
pascal@215 | 1035 +#include <linux/vmalloc.h> |
pascal@215 | 1036 +#endif |
pascal@215 | 1037 + |
pascal@215 | 1038 +static void __init *large_malloc(size_t size) |
pascal@215 | 1039 +{ |
pascal@215 | 1040 + return vmalloc(size); |
pascal@215 | 1041 +} |
pascal@215 | 1042 + |
pascal@215 | 1043 +static void __init large_free(void *where) |
pascal@215 | 1044 +{ |
pascal@215 | 1045 + vfree(where); |
pascal@215 | 1046 +} |
pascal@215 | 1047 + |
pascal@215 | 1048 +#ifndef TEST |
pascal@215 | 1049 +static void __init *malloc(size_t size) |
pascal@215 | 1050 +{ |
pascal@215 | 1051 + return kmalloc(size, GFP_KERNEL); |
pascal@215 | 1052 +} |
pascal@215 | 1053 + |
pascal@215 | 1054 +static void __init free(void *where) |
pascal@215 | 1055 +{ |
pascal@215 | 1056 + kfree(where); |
pascal@215 | 1057 +} |
pascal@215 | 1058 + |
pascal@215 | 1059 +static void __init error(char *x) |
pascal@215 | 1060 +{ |
pascal@215 | 1061 + printk(KERN_ERR "%s\n", x); |
pascal@215 | 1062 +} |
pascal@215 | 1063 +#endif |
pascal@215 | 1064 + |
pascal@215 | 1065 +#define STATIC /**/ |
pascal@215 | 1066 + |
pascal@215 | 1067 +#endif |
pascal@215 | 1068 + |
pascal@215 | 1069 +#include <linux/decompress_bunzip2.h> |
pascal@215 | 1070 + |
pascal@215 | 1071 + |
pascal@215 | 1072 +/* Constants for Huffman coding */ |
pascal@215 | 1073 +#define MAX_GROUPS 6 |
pascal@215 | 1074 +#define GROUP_SIZE 50 /* 64 would have been more efficient */ |
pascal@215 | 1075 +#define MAX_HUFCODE_BITS 20 /* Longest Huffman code allowed */ |
pascal@215 | 1076 +#define MAX_SYMBOLS 258 /* 256 literals + RUNA + RUNB */ |
pascal@215 | 1077 +#define SYMBOL_RUNA 0 |
pascal@215 | 1078 +#define SYMBOL_RUNB 1 |
pascal@215 | 1079 + |
pascal@215 | 1080 +/* Status return values */ |
pascal@215 | 1081 +#define RETVAL_OK 0 |
pascal@215 | 1082 +#define RETVAL_LAST_BLOCK (-1) |
pascal@215 | 1083 +#define RETVAL_NOT_BZIP_DATA (-2) |
pascal@215 | 1084 +#define RETVAL_UNEXPECTED_INPUT_EOF (-3) |
pascal@215 | 1085 +#define RETVAL_UNEXPECTED_OUTPUT_EOF (-4) |
pascal@215 | 1086 +#define RETVAL_DATA_ERROR (-5) |
pascal@215 | 1087 +#define RETVAL_OUT_OF_MEMORY (-6) |
pascal@215 | 1088 +#define RETVAL_OBSOLETE_INPUT (-7) |
pascal@215 | 1089 + |
pascal@215 | 1090 + |
pascal@215 | 1091 +/* This is what we know about each Huffman coding group */ |
pascal@215 | 1092 +struct group_data { |
pascal@215 | 1093 + /* We have an extra slot at the end of limit[] for a sentinal value. */ |
pascal@215 | 1094 + int limit[MAX_HUFCODE_BITS+1],base[MAX_HUFCODE_BITS],permute[MAX_SYMBOLS]; |
pascal@215 | 1095 + int minLen, maxLen; |
pascal@215 | 1096 +}; |
pascal@215 | 1097 + |
pascal@215 | 1098 +/* Structure holding all the housekeeping data, including IO buffers and |
pascal@215 | 1099 + memory that persists between calls to bunzip */ |
pascal@215 | 1100 +typedef struct { |
pascal@215 | 1101 + /* State for interrupting output loop */ |
pascal@215 | 1102 + int writeCopies,writePos,writeRunCountdown,writeCount,writeCurrent; |
pascal@215 | 1103 + /* I/O tracking data (file handles, buffers, positions, etc.) */ |
pascal@215 | 1104 + int (*fill)(void*,unsigned int); |
pascal@215 | 1105 + int inbufCount,inbufPos /*,outbufPos*/; |
pascal@215 | 1106 + unsigned char *inbuf /*,*outbuf*/; |
pascal@215 | 1107 + unsigned int inbufBitCount, inbufBits; |
pascal@215 | 1108 + /* The CRC values stored in the block header and calculated from the data */ |
pascal@215 | 1109 + unsigned int crc32Table[256],headerCRC, totalCRC, writeCRC; |
pascal@215 | 1110 + /* Intermediate buffer and its size (in bytes) */ |
pascal@215 | 1111 + unsigned int *dbuf, dbufSize; |
pascal@215 | 1112 + /* These things are a bit too big to go on the stack */ |
pascal@215 | 1113 + unsigned char selectors[32768]; /* nSelectors=15 bits */ |
pascal@215 | 1114 + struct group_data groups[MAX_GROUPS]; /* Huffman coding tables */ |
pascal@215 | 1115 + int io_error; /* non-zero if we have IO error */ |
pascal@215 | 1116 +} bunzip_data; |
pascal@215 | 1117 + |
pascal@215 | 1118 + |
pascal@215 | 1119 +/* Return the next nnn bits of input. All reads from the compressed input |
pascal@215 | 1120 + are done through this function. All reads are big endian */ |
pascal@215 | 1121 +static unsigned int get_bits(bunzip_data *bd, char bits_wanted) |
pascal@215 | 1122 +{ |
pascal@215 | 1123 + unsigned int bits=0; |
pascal@215 | 1124 + |
pascal@215 | 1125 + /* If we need to get more data from the byte buffer, do so. (Loop getting |
pascal@215 | 1126 + one byte at a time to enforce endianness and avoid unaligned access.) */ |
pascal@215 | 1127 + while (bd->inbufBitCount<bits_wanted) { |
pascal@215 | 1128 + /* If we need to read more data from file into byte buffer, do so */ |
pascal@215 | 1129 + if(bd->inbufPos==bd->inbufCount) { |
pascal@215 | 1130 + if(bd->io_error) |
pascal@215 | 1131 + return 0; |
pascal@215 | 1132 + if((bd->inbufCount = bd->fill(bd->inbuf, BZIP2_IOBUF_SIZE)) <= 0) { |
pascal@215 | 1133 + bd->io_error=RETVAL_UNEXPECTED_INPUT_EOF; |
pascal@215 | 1134 + return 0; |
pascal@215 | 1135 + } |
pascal@215 | 1136 + bd->inbufPos=0; |
pascal@215 | 1137 + } |
pascal@215 | 1138 + /* Avoid 32-bit overflow (dump bit buffer to top of output) */ |
pascal@215 | 1139 + if(bd->inbufBitCount>=24) { |
pascal@215 | 1140 + bits=bd->inbufBits&((1<<bd->inbufBitCount)-1); |
pascal@215 | 1141 + bits_wanted-=bd->inbufBitCount; |
pascal@215 | 1142 + bits<<=bits_wanted; |
pascal@215 | 1143 + bd->inbufBitCount=0; |
pascal@215 | 1144 + } |
pascal@215 | 1145 + /* Grab next 8 bits of input from buffer. */ |
pascal@215 | 1146 + bd->inbufBits=(bd->inbufBits<<8)|bd->inbuf[bd->inbufPos++]; |
pascal@215 | 1147 + bd->inbufBitCount+=8; |
pascal@215 | 1148 + } |
pascal@215 | 1149 + /* Calculate result */ |
pascal@215 | 1150 + bd->inbufBitCount-=bits_wanted; |
pascal@215 | 1151 + bits|=(bd->inbufBits>>bd->inbufBitCount)&((1<<bits_wanted)-1); |
pascal@215 | 1152 + |
pascal@215 | 1153 + return bits; |
pascal@215 | 1154 +} |
pascal@215 | 1155 + |
pascal@215 | 1156 +/* Unpacks the next block and sets up for the inverse burrows-wheeler step. */ |
pascal@215 | 1157 + |
pascal@215 | 1158 +static int get_next_block(bunzip_data *bd) |
pascal@215 | 1159 +{ |
pascal@215 | 1160 + struct group_data *hufGroup=NULL; |
pascal@215 | 1161 + int *base=NULL; |
pascal@215 | 1162 + int *limit=NULL; |
pascal@215 | 1163 + int dbufCount,nextSym,dbufSize,groupCount,selector, |
pascal@215 | 1164 + i,j,k,t,runPos,symCount,symTotal,nSelectors,byteCount[256]; |
pascal@215 | 1165 + unsigned char uc, symToByte[256], mtfSymbol[256], *selectors; |
pascal@215 | 1166 + unsigned int *dbuf,origPtr; |
pascal@215 | 1167 + |
pascal@215 | 1168 + dbuf=bd->dbuf; |
pascal@215 | 1169 + dbufSize=bd->dbufSize; |
pascal@215 | 1170 + selectors=bd->selectors; |
pascal@215 | 1171 + |
pascal@215 | 1172 + /* Read in header signature and CRC, then validate signature. |
pascal@215 | 1173 + (last block signature means CRC is for whole file, return now) */ |
pascal@215 | 1174 + i = get_bits(bd,24); |
pascal@215 | 1175 + j = get_bits(bd,24); |
pascal@215 | 1176 + bd->headerCRC=get_bits(bd,32); |
pascal@215 | 1177 + if ((i == 0x177245) && (j == 0x385090)) return RETVAL_LAST_BLOCK; |
pascal@215 | 1178 + if ((i != 0x314159) || (j != 0x265359)) return RETVAL_NOT_BZIP_DATA; |
pascal@215 | 1179 + /* We can add support for blockRandomised if anybody complains. There was |
pascal@215 | 1180 + some code for this in busybox 1.0.0-pre3, but nobody ever noticed that |
pascal@215 | 1181 + it didn't actually work. */ |
pascal@215 | 1182 + if(get_bits(bd,1)) return RETVAL_OBSOLETE_INPUT; |
pascal@215 | 1183 + if((origPtr=get_bits(bd,24)) > dbufSize) return RETVAL_DATA_ERROR; |
pascal@215 | 1184 + /* mapping table: if some byte values are never used (encoding things |
pascal@215 | 1185 + like ascii text), the compression code removes the gaps to have fewer |
pascal@215 | 1186 + symbols to deal with, and writes a sparse bitfield indicating which |
pascal@215 | 1187 + values were present. We make a translation table to convert the symbols |
pascal@215 | 1188 + back to the corresponding bytes. */ |
pascal@215 | 1189 + t=get_bits(bd, 16); |
pascal@215 | 1190 + symTotal=0; |
pascal@215 | 1191 + for (i=0;i<16;i++) { |
pascal@215 | 1192 + if(t&(1<<(15-i))) { |
pascal@215 | 1193 + k=get_bits(bd,16); |
pascal@215 | 1194 + for(j=0;j<16;j++) |
pascal@215 | 1195 + if(k&(1<<(15-j))) symToByte[symTotal++]=(16*i)+j; |
pascal@215 | 1196 + } |
pascal@215 | 1197 + } |
pascal@215 | 1198 + /* How many different Huffman coding groups does this block use? */ |
pascal@215 | 1199 + groupCount=get_bits(bd,3); |
pascal@215 | 1200 + if (groupCount<2 || groupCount>MAX_GROUPS) return RETVAL_DATA_ERROR; |
pascal@215 | 1201 + /* nSelectors: Every GROUP_SIZE many symbols we select a new Huffman coding |
pascal@215 | 1202 + group. Read in the group selector list, which is stored as MTF encoded |
pascal@215 | 1203 + bit runs. (MTF=Move To Front, as each value is used it's moved to the |
pascal@215 | 1204 + start of the list.) */ |
pascal@215 | 1205 + if(!(nSelectors=get_bits(bd, 15))) return RETVAL_DATA_ERROR; |
pascal@215 | 1206 + for(i=0; i<groupCount; i++) mtfSymbol[i] = i; |
pascal@215 | 1207 + for(i=0; i<nSelectors; i++) { |
pascal@215 | 1208 + /* Get next value */ |
pascal@215 | 1209 + for(j=0;get_bits(bd,1);j++) if (j>=groupCount) return RETVAL_DATA_ERROR; |
pascal@215 | 1210 + /* Decode MTF to get the next selector */ |
pascal@215 | 1211 + uc = mtfSymbol[j]; |
pascal@215 | 1212 + for(;j;j--) mtfSymbol[j] = mtfSymbol[j-1]; |
pascal@215 | 1213 + mtfSymbol[0]=selectors[i]=uc; |
pascal@215 | 1214 + } |
pascal@215 | 1215 + /* Read the Huffman coding tables for each group, which code for symTotal |
pascal@215 | 1216 + literal symbols, plus two run symbols (RUNA, RUNB) */ |
pascal@215 | 1217 + symCount=symTotal+2; |
pascal@215 | 1218 + for (j=0; j<groupCount; j++) { |
pascal@215 | 1219 + unsigned char length[MAX_SYMBOLS],temp[MAX_HUFCODE_BITS+1]; |
pascal@215 | 1220 + int minLen, maxLen, pp; |
pascal@215 | 1221 + /* Read Huffman code lengths for each symbol. They're stored in |
pascal@215 | 1222 + a way similar to mtf; record a starting value for the first symbol, |
pascal@215 | 1223 + and an offset from the previous value for everys symbol after that. |
pascal@215 | 1224 + (Subtracting 1 before the loop and then adding it back at the end is |
pascal@215 | 1225 + an optimization that makes the test inside the loop simpler: symbol |
pascal@215 | 1226 + length 0 becomes negative, so an unsigned inequality catches it.) */ |
pascal@215 | 1227 + t=get_bits(bd, 5)-1; |
pascal@215 | 1228 + for (i = 0; i < symCount; i++) { |
pascal@215 | 1229 + for(;;) { |
pascal@215 | 1230 + if (((unsigned)t) > (MAX_HUFCODE_BITS-1)) |
pascal@215 | 1231 + return RETVAL_DATA_ERROR; |
pascal@215 | 1232 + /* If first bit is 0, stop. Else second bit indicates whether |
pascal@215 | 1233 + to increment or decrement the value. Optimization: grab 2 |
pascal@215 | 1234 + bits and unget the second if the first was 0. */ |
pascal@215 | 1235 + k = get_bits(bd,2); |
pascal@215 | 1236 + if (k < 2) { |
pascal@215 | 1237 + bd->inbufBitCount++; |
pascal@215 | 1238 + break; |
pascal@215 | 1239 + } |
pascal@215 | 1240 + /* Add one if second bit 1, else subtract 1. Avoids if/else */ |
pascal@215 | 1241 + t+=(((k+1)&2)-1); |
pascal@215 | 1242 + } |
pascal@215 | 1243 + /* Correct for the initial -1, to get the final symbol length */ |
pascal@215 | 1244 + length[i]=t+1; |
pascal@215 | 1245 + } |
pascal@215 | 1246 + /* Find largest and smallest lengths in this group */ |
pascal@215 | 1247 + minLen=maxLen=length[0]; |
pascal@215 | 1248 + for(i = 1; i < symCount; i++) { |
pascal@215 | 1249 + if(length[i] > maxLen) maxLen = length[i]; |
pascal@215 | 1250 + else if(length[i] < minLen) minLen = length[i]; |
pascal@215 | 1251 + } |
pascal@215 | 1252 + /* Calculate permute[], base[], and limit[] tables from length[]. |
pascal@215 | 1253 + * |
pascal@215 | 1254 + * permute[] is the lookup table for converting Huffman coded symbols |
pascal@215 | 1255 + * into decoded symbols. base[] is the amount to subtract from the |
pascal@215 | 1256 + * value of a Huffman symbol of a given length when using permute[]. |
pascal@215 | 1257 + * |
pascal@215 | 1258 + * limit[] indicates the largest numerical value a symbol with a given |
pascal@215 | 1259 + * number of bits can have. This is how the Huffman codes can vary in |
pascal@215 | 1260 + * length: each code with a value>limit[length] needs another bit. |
pascal@215 | 1261 + */ |
pascal@215 | 1262 + hufGroup=bd->groups+j; |
pascal@215 | 1263 + hufGroup->minLen = minLen; |
pascal@215 | 1264 + hufGroup->maxLen = maxLen; |
pascal@215 | 1265 + /* Note that minLen can't be smaller than 1, so we adjust the base |
pascal@215 | 1266 + and limit array pointers so we're not always wasting the first |
pascal@215 | 1267 + entry. We do this again when using them (during symbol decoding).*/ |
pascal@215 | 1268 + base=hufGroup->base-1; |
pascal@215 | 1269 + limit=hufGroup->limit-1; |
pascal@215 | 1270 + /* Calculate permute[]. Concurently, initialize temp[] and limit[]. */ |
pascal@215 | 1271 + pp=0; |
pascal@215 | 1272 + for(i=minLen;i<=maxLen;i++) { |
pascal@215 | 1273 + temp[i]=limit[i]=0; |
pascal@215 | 1274 + for(t=0;t<symCount;t++) |
pascal@215 | 1275 + if(length[t]==i) hufGroup->permute[pp++] = t; |
pascal@215 | 1276 + } |
pascal@215 | 1277 + /* Count symbols coded for at each bit length */ |
pascal@215 | 1278 + for (i=0;i<symCount;i++) temp[length[i]]++; |
pascal@215 | 1279 + /* Calculate limit[] (the largest symbol-coding value at each bit |
pascal@215 | 1280 + * length, which is (previous limit<<1)+symbols at this level), and |
pascal@215 | 1281 + * base[] (number of symbols to ignore at each bit length, which is |
pascal@215 | 1282 + * limit minus the cumulative count of symbols coded for already). */ |
pascal@215 | 1283 + pp=t=0; |
pascal@215 | 1284 + for (i=minLen; i<maxLen; i++) { |
pascal@215 | 1285 + pp+=temp[i]; |
pascal@215 | 1286 + /* We read the largest possible symbol size and then unget bits |
pascal@215 | 1287 + after determining how many we need, and those extra bits could |
pascal@215 | 1288 + be set to anything. (They're noise from future symbols.) At |
pascal@215 | 1289 + each level we're really only interested in the first few bits, |
pascal@215 | 1290 + so here we set all the trailing to-be-ignored bits to 1 so they |
pascal@215 | 1291 + don't affect the value>limit[length] comparison. */ |
pascal@215 | 1292 + limit[i]= (pp << (maxLen - i)) - 1; |
pascal@215 | 1293 + pp<<=1; |
pascal@215 | 1294 + base[i+1]=pp-(t+=temp[i]); |
pascal@215 | 1295 + } |
pascal@215 | 1296 + limit[maxLen+1] = INT_MAX; /* Sentinal value for reading next sym. */ |
pascal@215 | 1297 + limit[maxLen]=pp+temp[maxLen]-1; |
pascal@215 | 1298 + base[minLen]=0; |
pascal@215 | 1299 + } |
pascal@215 | 1300 + /* We've finished reading and digesting the block header. Now read this |
pascal@215 | 1301 + block's Huffman coded symbols from the file and undo the Huffman coding |
pascal@215 | 1302 + and run length encoding, saving the result into dbuf[dbufCount++]=uc */ |
pascal@215 | 1303 + |
pascal@215 | 1304 + /* Initialize symbol occurrence counters and symbol Move To Front table */ |
pascal@215 | 1305 + for(i=0;i<256;i++) { |
pascal@215 | 1306 + byteCount[i] = 0; |
pascal@215 | 1307 + mtfSymbol[i]=(unsigned char)i; |
pascal@215 | 1308 + } |
pascal@215 | 1309 + /* Loop through compressed symbols. */ |
pascal@215 | 1310 + runPos=dbufCount=symCount=selector=0; |
pascal@215 | 1311 + for(;;) { |
pascal@215 | 1312 + /* Determine which Huffman coding group to use. */ |
pascal@215 | 1313 + if(!(symCount--)) { |
pascal@215 | 1314 + symCount=GROUP_SIZE-1; |
pascal@215 | 1315 + if(selector>=nSelectors) return RETVAL_DATA_ERROR; |
pascal@215 | 1316 + hufGroup=bd->groups+selectors[selector++]; |
pascal@215 | 1317 + base=hufGroup->base-1; |
pascal@215 | 1318 + limit=hufGroup->limit-1; |
pascal@215 | 1319 + } |
pascal@215 | 1320 + /* Read next Huffman-coded symbol. */ |
pascal@215 | 1321 + /* Note: It is far cheaper to read maxLen bits and back up than it is |
pascal@215 | 1322 + to read minLen bits and then an additional bit at a time, testing |
pascal@215 | 1323 + as we go. Because there is a trailing last block (with file CRC), |
pascal@215 | 1324 + there is no danger of the overread causing an unexpected EOF for a |
pascal@215 | 1325 + valid compressed file. As a further optimization, we do the read |
pascal@215 | 1326 + inline (falling back to a call to get_bits if the buffer runs |
pascal@215 | 1327 + dry). The following (up to got_huff_bits:) is equivalent to |
pascal@215 | 1328 + j=get_bits(bd,hufGroup->maxLen); |
pascal@215 | 1329 + */ |
pascal@215 | 1330 + while (bd->inbufBitCount<hufGroup->maxLen) { |
pascal@215 | 1331 + if(bd->inbufPos==bd->inbufCount) { |
pascal@215 | 1332 + j = get_bits(bd,hufGroup->maxLen); |
pascal@215 | 1333 + goto got_huff_bits; |
pascal@215 | 1334 + } |
pascal@215 | 1335 + bd->inbufBits=(bd->inbufBits<<8)|bd->inbuf[bd->inbufPos++]; |
pascal@215 | 1336 + bd->inbufBitCount+=8; |
pascal@215 | 1337 + }; |
pascal@215 | 1338 + bd->inbufBitCount-=hufGroup->maxLen; |
pascal@215 | 1339 + j = (bd->inbufBits>>bd->inbufBitCount)&((1<<hufGroup->maxLen)-1); |
pascal@215 | 1340 +got_huff_bits: |
pascal@215 | 1341 + /* Figure how how many bits are in next symbol and unget extras */ |
pascal@215 | 1342 + i=hufGroup->minLen; |
pascal@215 | 1343 + while(j>limit[i]) ++i; |
pascal@215 | 1344 + bd->inbufBitCount += (hufGroup->maxLen - i); |
pascal@215 | 1345 + /* Huffman decode value to get nextSym (with bounds checking) */ |
pascal@215 | 1346 + if ((i > hufGroup->maxLen) |
pascal@215 | 1347 + || (((unsigned)(j=(j>>(hufGroup->maxLen-i))-base[i])) |
pascal@215 | 1348 + >= MAX_SYMBOLS)) |
pascal@215 | 1349 + return RETVAL_DATA_ERROR; |
pascal@215 | 1350 + nextSym = hufGroup->permute[j]; |
pascal@215 | 1351 + /* We have now decoded the symbol, which indicates either a new literal |
pascal@215 | 1352 + byte, or a repeated run of the most recent literal byte. First, |
pascal@215 | 1353 + check if nextSym indicates a repeated run, and if so loop collecting |
pascal@215 | 1354 + how many times to repeat the last literal. */ |
pascal@215 | 1355 + if (((unsigned)nextSym) <= SYMBOL_RUNB) { /* RUNA or RUNB */ |
pascal@215 | 1356 + /* If this is the start of a new run, zero out counter */ |
pascal@215 | 1357 + if(!runPos) { |
pascal@215 | 1358 + runPos = 1; |
pascal@215 | 1359 + t = 0; |
pascal@215 | 1360 + } |
pascal@215 | 1361 + /* Neat trick that saves 1 symbol: instead of or-ing 0 or 1 at |
pascal@215 | 1362 + each bit position, add 1 or 2 instead. For example, |
pascal@215 | 1363 + 1011 is 1<<0 + 1<<1 + 2<<2. 1010 is 2<<0 + 2<<1 + 1<<2. |
pascal@215 | 1364 + You can make any bit pattern that way using 1 less symbol than |
pascal@215 | 1365 + the basic or 0/1 method (except all bits 0, which would use no |
pascal@215 | 1366 + symbols, but a run of length 0 doesn't mean anything in this |
pascal@215 | 1367 + context). Thus space is saved. */ |
pascal@215 | 1368 + t += (runPos << nextSym); /* +runPos if RUNA; +2*runPos if RUNB */ |
pascal@215 | 1369 + runPos <<= 1; |
pascal@215 | 1370 + continue; |
pascal@215 | 1371 + } |
pascal@215 | 1372 + /* When we hit the first non-run symbol after a run, we now know |
pascal@215 | 1373 + how many times to repeat the last literal, so append that many |
pascal@215 | 1374 + copies to our buffer of decoded symbols (dbuf) now. (The last |
pascal@215 | 1375 + literal used is the one at the head of the mtfSymbol array.) */ |
pascal@215 | 1376 + if(runPos) { |
pascal@215 | 1377 + runPos=0; |
pascal@215 | 1378 + if(dbufCount+t>=dbufSize) return RETVAL_DATA_ERROR; |
pascal@215 | 1379 + |
pascal@215 | 1380 + uc = symToByte[mtfSymbol[0]]; |
pascal@215 | 1381 + byteCount[uc] += t; |
pascal@215 | 1382 + while(t--) dbuf[dbufCount++]=uc; |
pascal@215 | 1383 + } |
pascal@215 | 1384 + /* Is this the terminating symbol? */ |
pascal@215 | 1385 + if(nextSym>symTotal) break; |
pascal@215 | 1386 + /* At this point, nextSym indicates a new literal character. Subtract |
pascal@215 | 1387 + one to get the position in the MTF array at which this literal is |
pascal@215 | 1388 + currently to be found. (Note that the result can't be -1 or 0, |
pascal@215 | 1389 + because 0 and 1 are RUNA and RUNB. But another instance of the |
pascal@215 | 1390 + first symbol in the mtf array, position 0, would have been handled |
pascal@215 | 1391 + as part of a run above. Therefore 1 unused mtf position minus |
pascal@215 | 1392 + 2 non-literal nextSym values equals -1.) */ |
pascal@215 | 1393 + if(dbufCount>=dbufSize) return RETVAL_DATA_ERROR; |
pascal@215 | 1394 + i = nextSym - 1; |
pascal@215 | 1395 + uc = mtfSymbol[i]; |
pascal@215 | 1396 + /* Adjust the MTF array. Since we typically expect to move only a |
pascal@215 | 1397 + * small number of symbols, and are bound by 256 in any case, using |
pascal@215 | 1398 + * memmove here would typically be bigger and slower due to function |
pascal@215 | 1399 + * call overhead and other assorted setup costs. */ |
pascal@215 | 1400 + do { |
pascal@215 | 1401 + mtfSymbol[i] = mtfSymbol[i-1]; |
pascal@215 | 1402 + } while (--i); |
pascal@215 | 1403 + mtfSymbol[0] = uc; |
pascal@215 | 1404 + uc=symToByte[uc]; |
pascal@215 | 1405 + /* We have our literal byte. Save it into dbuf. */ |
pascal@215 | 1406 + byteCount[uc]++; |
pascal@215 | 1407 + dbuf[dbufCount++] = (unsigned int)uc; |
pascal@215 | 1408 + } |
pascal@215 | 1409 + /* At this point, we've read all the Huffman-coded symbols (and repeated |
pascal@215 | 1410 + runs) for this block from the input stream, and decoded them into the |
pascal@215 | 1411 + intermediate buffer. There are dbufCount many decoded bytes in dbuf[]. |
pascal@215 | 1412 + Now undo the Burrows-Wheeler transform on dbuf. |
pascal@215 | 1413 + See http://dogma.net/markn/articles/bwt/bwt.htm |
pascal@215 | 1414 + */ |
pascal@215 | 1415 + /* Turn byteCount into cumulative occurrence counts of 0 to n-1. */ |
pascal@215 | 1416 + j=0; |
pascal@215 | 1417 + for(i=0;i<256;i++) { |
pascal@215 | 1418 + k=j+byteCount[i]; |
pascal@215 | 1419 + byteCount[i] = j; |
pascal@215 | 1420 + j=k; |
pascal@215 | 1421 + } |
pascal@215 | 1422 + /* Figure out what order dbuf would be in if we sorted it. */ |
pascal@215 | 1423 + for (i=0;i<dbufCount;i++) { |
pascal@215 | 1424 + uc=(unsigned char)(dbuf[i] & 0xff); |
pascal@215 | 1425 + dbuf[byteCount[uc]] |= (i << 8); |
pascal@215 | 1426 + byteCount[uc]++; |
pascal@215 | 1427 + } |
pascal@215 | 1428 + /* Decode first byte by hand to initialize "previous" byte. Note that it |
pascal@215 | 1429 + doesn't get output, and if the first three characters are identical |
pascal@215 | 1430 + it doesn't qualify as a run (hence writeRunCountdown=5). */ |
pascal@215 | 1431 + if(dbufCount) { |
pascal@215 | 1432 + if(origPtr>=dbufCount) return RETVAL_DATA_ERROR; |
pascal@215 | 1433 + bd->writePos=dbuf[origPtr]; |
pascal@215 | 1434 + bd->writeCurrent=(unsigned char)(bd->writePos&0xff); |
pascal@215 | 1435 + bd->writePos>>=8; |
pascal@215 | 1436 + bd->writeRunCountdown=5; |
pascal@215 | 1437 + } |
pascal@215 | 1438 + bd->writeCount=dbufCount; |
pascal@215 | 1439 + |
pascal@215 | 1440 + return RETVAL_OK; |
pascal@215 | 1441 +} |
pascal@215 | 1442 + |
pascal@215 | 1443 +/* Undo burrows-wheeler transform on intermediate buffer to produce output. |
pascal@215 | 1444 + If start_bunzip was initialized with out_fd=-1, then up to len bytes of |
pascal@215 | 1445 + data are written to outbuf. Return value is number of bytes written or |
pascal@215 | 1446 + error (all errors are negative numbers). If out_fd!=-1, outbuf and len |
pascal@215 | 1447 + are ignored, data is written to out_fd and return is RETVAL_OK or error. |
pascal@215 | 1448 +*/ |
pascal@215 | 1449 + |
pascal@215 | 1450 +static int read_bunzip(bunzip_data *bd, char *outbuf, int len) |
pascal@215 | 1451 +{ |
pascal@215 | 1452 + const unsigned int *dbuf; |
pascal@215 | 1453 + int pos,xcurrent,previous,gotcount; |
pascal@215 | 1454 + |
pascal@215 | 1455 + /* If last read was short due to end of file, return last block now */ |
pascal@215 | 1456 + if(bd->writeCount<0) return bd->writeCount; |
pascal@215 | 1457 + |
pascal@215 | 1458 + gotcount = 0; |
pascal@215 | 1459 + dbuf=bd->dbuf; |
pascal@215 | 1460 + pos=bd->writePos; |
pascal@215 | 1461 + xcurrent=bd->writeCurrent; |
pascal@215 | 1462 + |
pascal@215 | 1463 + /* We will always have pending decoded data to write into the output |
pascal@215 | 1464 + buffer unless this is the very first call (in which case we haven't |
pascal@215 | 1465 + Huffman-decoded a block into the intermediate buffer yet). */ |
pascal@215 | 1466 + |
pascal@215 | 1467 + if (bd->writeCopies) { |
pascal@215 | 1468 + /* Inside the loop, writeCopies means extra copies (beyond 1) */ |
pascal@215 | 1469 + --bd->writeCopies; |
pascal@215 | 1470 + /* Loop outputting bytes */ |
pascal@215 | 1471 + for(;;) { |
pascal@215 | 1472 + /* If the output buffer is full, snapshot state and return */ |
pascal@215 | 1473 + if(gotcount >= len) { |
pascal@215 | 1474 + bd->writePos=pos; |
pascal@215 | 1475 + bd->writeCurrent=xcurrent; |
pascal@215 | 1476 + bd->writeCopies++; |
pascal@215 | 1477 + return len; |
pascal@215 | 1478 + } |
pascal@215 | 1479 + /* Write next byte into output buffer, updating CRC */ |
pascal@215 | 1480 + outbuf[gotcount++] = xcurrent; |
pascal@215 | 1481 + bd->writeCRC=(((bd->writeCRC)<<8) |
pascal@215 | 1482 + ^bd->crc32Table[((bd->writeCRC)>>24)^xcurrent]); |
pascal@215 | 1483 + /* Loop now if we're outputting multiple copies of this byte */ |
pascal@215 | 1484 + if (bd->writeCopies) { |
pascal@215 | 1485 + --bd->writeCopies; |
pascal@215 | 1486 + continue; |
pascal@215 | 1487 + } |
pascal@215 | 1488 +decode_next_byte: |
pascal@215 | 1489 + if (!bd->writeCount--) break; |
pascal@215 | 1490 + /* Follow sequence vector to undo Burrows-Wheeler transform */ |
pascal@215 | 1491 + previous=xcurrent; |
pascal@215 | 1492 + pos=dbuf[pos]; |
pascal@215 | 1493 + xcurrent=pos&0xff; |
pascal@215 | 1494 + pos>>=8; |
pascal@215 | 1495 + /* After 3 consecutive copies of the same byte, the 4th is a repeat |
pascal@215 | 1496 + count. We count down from 4 instead |
pascal@215 | 1497 + * of counting up because testing for non-zero is faster */ |
pascal@215 | 1498 + if(--bd->writeRunCountdown) { |
pascal@215 | 1499 + if(xcurrent!=previous) bd->writeRunCountdown=4; |
pascal@215 | 1500 + } else { |
pascal@215 | 1501 + /* We have a repeated run, this byte indicates the count */ |
pascal@215 | 1502 + bd->writeCopies=xcurrent; |
pascal@215 | 1503 + xcurrent=previous; |
pascal@215 | 1504 + bd->writeRunCountdown=5; |
pascal@215 | 1505 + /* Sometimes there are just 3 bytes (run length 0) */ |
pascal@215 | 1506 + if(!bd->writeCopies) goto decode_next_byte; |
pascal@215 | 1507 + /* Subtract the 1 copy we'd output anyway to get extras */ |
pascal@215 | 1508 + --bd->writeCopies; |
pascal@215 | 1509 + } |
pascal@215 | 1510 + } |
pascal@215 | 1511 + /* Decompression of this block completed successfully */ |
pascal@215 | 1512 + bd->writeCRC=~bd->writeCRC; |
pascal@215 | 1513 + bd->totalCRC=((bd->totalCRC<<1) | (bd->totalCRC>>31)) ^ bd->writeCRC; |
pascal@215 | 1514 + /* If this block had a CRC error, force file level CRC error. */ |
pascal@215 | 1515 + if(bd->writeCRC!=bd->headerCRC) { |
pascal@215 | 1516 + bd->totalCRC=bd->headerCRC+1; |
pascal@215 | 1517 + return RETVAL_LAST_BLOCK; |
pascal@215 | 1518 + } |
pascal@215 | 1519 + } |
pascal@215 | 1520 + |
pascal@215 | 1521 + /* Refill the intermediate buffer by Huffman-decoding next block of input */ |
pascal@215 | 1522 + /* (previous is just a convenient unused temp variable here) */ |
pascal@215 | 1523 + previous=get_next_block(bd); |
pascal@215 | 1524 + if(previous) { |
pascal@215 | 1525 + bd->writeCount=previous; |
pascal@215 | 1526 + return (previous!=RETVAL_LAST_BLOCK) ? previous : gotcount; |
pascal@215 | 1527 + } |
pascal@215 | 1528 + bd->writeCRC=0xffffffffUL; |
pascal@215 | 1529 + pos=bd->writePos; |
pascal@215 | 1530 + xcurrent=bd->writeCurrent; |
pascal@215 | 1531 + goto decode_next_byte; |
pascal@215 | 1532 +} |
pascal@215 | 1533 + |
pascal@215 | 1534 +static int nofill(void *buf,unsigned int len) { |
pascal@215 | 1535 + return -1; |
pascal@215 | 1536 +} |
pascal@215 | 1537 + |
pascal@215 | 1538 +/* Allocate the structure, read file header. If in_fd==-1, inbuf must contain |
pascal@215 | 1539 + a complete bunzip file (len bytes long). If in_fd!=-1, inbuf and len are |
pascal@215 | 1540 + ignored, and data is read from file handle into temporary buffer. */ |
pascal@215 | 1541 +static int start_bunzip(bunzip_data **bdp, void *inbuf, int len, |
pascal@215 | 1542 + int (*fill)(void*,unsigned int)) |
pascal@215 | 1543 +{ |
pascal@215 | 1544 + bunzip_data *bd; |
pascal@215 | 1545 + unsigned int i,j,c; |
pascal@215 | 1546 + const unsigned int BZh0=(((unsigned int)'B')<<24)+(((unsigned int)'Z')<<16) |
pascal@215 | 1547 + +(((unsigned int)'h')<<8)+(unsigned int)'0'; |
pascal@215 | 1548 + |
pascal@215 | 1549 + /* Figure out how much data to allocate */ |
pascal@215 | 1550 + i=sizeof(bunzip_data); |
pascal@215 | 1551 + |
pascal@215 | 1552 + /* Allocate bunzip_data. Most fields initialize to zero. */ |
pascal@215 | 1553 + bd=*bdp=malloc(i); |
pascal@215 | 1554 + memset(bd,0,sizeof(bunzip_data)); |
pascal@215 | 1555 + /* Setup input buffer */ |
pascal@215 | 1556 + bd->inbuf=inbuf; |
pascal@215 | 1557 + bd->inbufCount=len; |
pascal@215 | 1558 + if(fill != NULL) |
pascal@215 | 1559 + bd->fill=fill; |
pascal@215 | 1560 + else |
pascal@215 | 1561 + bd->fill=nofill; |
pascal@215 | 1562 + |
pascal@215 | 1563 + /* Init the CRC32 table (big endian) */ |
pascal@215 | 1564 + for(i=0;i<256;i++) { |
pascal@215 | 1565 + c=i<<24; |
pascal@215 | 1566 + for(j=8;j;j--) |
pascal@215 | 1567 + c=c&0x80000000 ? (c<<1)^0x04c11db7 : (c<<1); |
pascal@215 | 1568 + bd->crc32Table[i]=c; |
pascal@215 | 1569 + } |
pascal@215 | 1570 + |
pascal@215 | 1571 + /* Ensure that file starts with "BZh['1'-'9']." */ |
pascal@215 | 1572 + i = get_bits(bd,32); |
pascal@215 | 1573 + if (((unsigned int)(i-BZh0-1)) >= 9) return RETVAL_NOT_BZIP_DATA; |
pascal@215 | 1574 + |
pascal@215 | 1575 + /* Fourth byte (ascii '1'-'9'), indicates block size in units of 100k of |
pascal@215 | 1576 + uncompressed data. Allocate intermediate buffer for block. */ |
pascal@215 | 1577 + bd->dbufSize=100000*(i-BZh0); |
pascal@215 | 1578 + |
pascal@215 | 1579 + bd->dbuf=large_malloc(bd->dbufSize * sizeof(int)); |
pascal@215 | 1580 + return RETVAL_OK; |
pascal@215 | 1581 +} |
pascal@215 | 1582 + |
pascal@215 | 1583 +/* Example usage: decompress src_fd to dst_fd. (Stops at end of bzip data, |
pascal@215 | 1584 + not end of file.) */ |
pascal@215 | 1585 +STATIC int bunzip2(char *inbuf, int len, |
pascal@215 | 1586 + int(*fill)(void*,unsigned int), |
pascal@215 | 1587 + int(*writebb)(char*,unsigned int), |
pascal@215 | 1588 + int *pos) |
pascal@215 | 1589 +{ |
pascal@215 | 1590 + char *outbuf; |
pascal@215 | 1591 + bunzip_data *bd; |
pascal@215 | 1592 + int i; |
pascal@215 | 1593 + |
pascal@215 | 1594 + outbuf=malloc(BZIP2_IOBUF_SIZE); |
pascal@215 | 1595 + if(!(i=start_bunzip(&bd,inbuf,len,fill))) { |
pascal@215 | 1596 + for(;;) { |
pascal@215 | 1597 + if((i=read_bunzip(bd,outbuf,BZIP2_IOBUF_SIZE)) <= 0) break; |
pascal@215 | 1598 + if(i!=writebb(outbuf,i)) { |
pascal@215 | 1599 + i=RETVAL_UNEXPECTED_OUTPUT_EOF; |
pascal@215 | 1600 + break; |
pascal@215 | 1601 + } |
pascal@215 | 1602 + } |
pascal@215 | 1603 + } |
pascal@215 | 1604 + /* Check CRC and release memory */ |
pascal@215 | 1605 + if(i==RETVAL_LAST_BLOCK) { |
pascal@215 | 1606 + if (bd->headerCRC!=bd->totalCRC) { |
pascal@215 | 1607 + error("Data integrity error when decompressing."); |
pascal@215 | 1608 + } else { |
pascal@215 | 1609 + i=RETVAL_OK; |
pascal@215 | 1610 + } |
pascal@215 | 1611 + } |
pascal@215 | 1612 + else if (i==RETVAL_UNEXPECTED_OUTPUT_EOF) { |
pascal@215 | 1613 + error("Compressed file ends unexpectedly"); |
pascal@215 | 1614 + } |
pascal@215 | 1615 + if(bd->dbuf) large_free(bd->dbuf); |
pascal@215 | 1616 + if(pos) |
pascal@215 | 1617 + *pos = bd->inbufPos; |
pascal@215 | 1618 + free(bd); |
pascal@215 | 1619 + free(outbuf); |
pascal@215 | 1620 + |
pascal@215 | 1621 + return i; |
pascal@215 | 1622 +} |
pascal@215 | 1623 + |
pascal@243 | 1624 |
pascal@215 | 1625 --- linux-2.6.24.2/lib/decompress_unlzma.c |
pascal@215 | 1626 +++ linux-2.6.24.2/lib/decompress_unlzma.c |
pascal@215 | 1627 @@ -0,0 +1,605 @@ |
pascal@215 | 1628 +/* Lzma decompressor for Linux kernel. Shamelessly snarfed |
pascal@215 | 1629 + * from busybox 1.1.1 |
pascal@215 | 1630 + * |
pascal@215 | 1631 + * Linux kernel adaptation |
pascal@215 | 1632 + * Copyright (C) 2006 Alain <alain@knaff.lu> |
pascal@215 | 1633 + * |
pascal@215 | 1634 + * Based on small lzma deflate implementation/Small range coder |
pascal@215 | 1635 + * implementation for lzma. |
pascal@215 | 1636 + * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org> |
pascal@215 | 1637 + * |
pascal@215 | 1638 + * Based on LzmaDecode.c from the LZMA SDK 4.22 (http://www.7-zip.org/) |
pascal@215 | 1639 + * Copyright (C) 1999-2005 Igor Pavlov |
pascal@215 | 1640 + * |
pascal@215 | 1641 + * Copyrights of the parts, see headers below. |
pascal@215 | 1642 + * |
pascal@215 | 1643 + * |
pascal@215 | 1644 + * This program is free software; you can redistribute it and/or |
pascal@215 | 1645 + * modify it under the terms of the GNU Lesser General Public |
pascal@215 | 1646 + * License as published by the Free Software Foundation; either |
pascal@215 | 1647 + * version 2.1 of the License, or (at your option) any later version. |
pascal@215 | 1648 + * |
pascal@215 | 1649 + * This program is distributed in the hope that it will be useful, |
pascal@215 | 1650 + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
pascal@215 | 1651 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
pascal@215 | 1652 + * Lesser General Public License for more details. |
pascal@215 | 1653 + * |
pascal@215 | 1654 + * You should have received a copy of the GNU Lesser General Public |
pascal@215 | 1655 + * License along with this library; if not, write to the Free Software |
pascal@215 | 1656 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
pascal@215 | 1657 + */ |
pascal@215 | 1658 + |
pascal@215 | 1659 +#ifndef STATIC |
pascal@215 | 1660 + |
pascal@215 | 1661 +#include <linux/kernel.h> |
pascal@215 | 1662 +#include <linux/fs.h> |
pascal@215 | 1663 +#include <linux/string.h> |
pascal@215 | 1664 + |
pascal@215 | 1665 +#ifdef TEST |
pascal@215 | 1666 +#include "test.h" |
pascal@215 | 1667 +#else |
pascal@215 | 1668 +#include <linux/vmalloc.h> |
pascal@215 | 1669 +#endif |
pascal@215 | 1670 + |
pascal@215 | 1671 +static void __init *large_malloc(size_t size) |
pascal@215 | 1672 +{ |
pascal@215 | 1673 + return vmalloc(size); |
pascal@215 | 1674 +} |
pascal@215 | 1675 + |
pascal@215 | 1676 +static void __init large_free(void *where) |
pascal@215 | 1677 +{ |
pascal@215 | 1678 + vfree(where); |
pascal@215 | 1679 +} |
pascal@215 | 1680 + |
pascal@215 | 1681 +#ifndef TEST |
pascal@215 | 1682 +static void __init *malloc(size_t size) |
pascal@215 | 1683 +{ |
pascal@215 | 1684 + return kmalloc(size, GFP_KERNEL); |
pascal@215 | 1685 +} |
pascal@215 | 1686 + |
pascal@215 | 1687 +static void __init free(void *where) |
pascal@215 | 1688 +{ |
pascal@215 | 1689 + kfree(where); |
pascal@215 | 1690 +} |
pascal@215 | 1691 + |
pascal@215 | 1692 +static void __init error(char *x) |
pascal@215 | 1693 +{ |
pascal@215 | 1694 + printk(KERN_ERR "%s\n", x); |
pascal@215 | 1695 +} |
pascal@215 | 1696 + |
pascal@215 | 1697 +#endif |
pascal@215 | 1698 + |
pascal@215 | 1699 +#define STATIC /**/ |
pascal@215 | 1700 + |
pascal@215 | 1701 +#endif |
pascal@215 | 1702 + |
pascal@215 | 1703 +#include <linux/decompress_unlzma.h> |
pascal@215 | 1704 + |
pascal@215 | 1705 +#define MIN(a,b) (((a)<(b))?(a):(b)) |
pascal@215 | 1706 + |
pascal@215 | 1707 +static long long read_int(unsigned char *ptr, int size) |
pascal@215 | 1708 +{ |
pascal@215 | 1709 + int i; |
pascal@215 | 1710 + long long ret=0; |
pascal@215 | 1711 + |
pascal@215 | 1712 + for(i=0; i<size; i++) { |
pascal@215 | 1713 + ret = (ret << 8) | ptr[size-i-1]; |
pascal@215 | 1714 + } |
pascal@215 | 1715 + return ret; |
pascal@215 | 1716 +} |
pascal@215 | 1717 + |
pascal@215 | 1718 +#define ENDIAN_CONVERT(x) x=(typeof(x))read_int((unsigned char*)&x,sizeof(x)) |
pascal@215 | 1719 + |
pascal@215 | 1720 + |
pascal@215 | 1721 +/* Small range coder implementation for lzma. |
pascal@215 | 1722 + * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org> |
pascal@215 | 1723 + * |
pascal@215 | 1724 + * Based on LzmaDecode.c from the LZMA SDK 4.22 (http://www.7-zip.org/) |
pascal@215 | 1725 + * Copyright (c) 1999-2005 Igor Pavlov |
pascal@215 | 1726 + */ |
pascal@215 | 1727 + |
pascal@215 | 1728 +#ifndef always_inline |
pascal@215 | 1729 +# if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >0) |
pascal@215 | 1730 +# define always_inline __attribute__((always_inline)) inline |
pascal@215 | 1731 +# else |
pascal@215 | 1732 +# define always_inline inline |
pascal@215 | 1733 +# endif |
pascal@215 | 1734 +#endif |
pascal@215 | 1735 + |
pascal@215 | 1736 +#ifdef CONFIG_FEATURE_LZMA_FAST |
pascal@215 | 1737 +# define speed_inline always_inline |
pascal@215 | 1738 +#else |
pascal@215 | 1739 +# define speed_inline |
pascal@215 | 1740 +#endif |
pascal@215 | 1741 + |
pascal@215 | 1742 + |
pascal@215 | 1743 +typedef struct { |
pascal@215 | 1744 + int (*fill)(void*,unsigned int); |
pascal@215 | 1745 + uint8_t *ptr; |
pascal@215 | 1746 + uint8_t *buffer; |
pascal@215 | 1747 + uint8_t *buffer_end; |
pascal@215 | 1748 + int buffer_size; |
pascal@215 | 1749 + uint32_t code; |
pascal@215 | 1750 + uint32_t range; |
pascal@215 | 1751 + uint32_t bound; |
pascal@215 | 1752 +} rc_t; |
pascal@215 | 1753 + |
pascal@215 | 1754 + |
pascal@215 | 1755 +#define RC_TOP_BITS 24 |
pascal@215 | 1756 +#define RC_MOVE_BITS 5 |
pascal@215 | 1757 +#define RC_MODEL_TOTAL_BITS 11 |
pascal@215 | 1758 + |
pascal@215 | 1759 + |
pascal@215 | 1760 +/* Called twice: once at startup and once in rc_normalize() */ |
pascal@215 | 1761 +static void rc_read(rc_t * rc) |
pascal@215 | 1762 +{ |
pascal@215 | 1763 + rc->buffer_size = rc->fill((char*)rc->buffer, LZMA_IOBUF_SIZE); |
pascal@215 | 1764 + if (rc->buffer_size <= 0) |
pascal@215 | 1765 + error("unexpected EOF"); |
pascal@215 | 1766 + rc->ptr = rc->buffer; |
pascal@215 | 1767 + rc->buffer_end = rc->buffer + rc->buffer_size; |
pascal@215 | 1768 +} |
pascal@215 | 1769 + |
pascal@215 | 1770 +/* Called once */ |
pascal@215 | 1771 +static always_inline void rc_init(rc_t * rc, int (*fill)(void*,unsigned int), |
pascal@215 | 1772 + char *buffer, int buffer_size) |
pascal@215 | 1773 +{ |
pascal@215 | 1774 + rc->fill = fill; |
pascal@215 | 1775 + rc->buffer = (uint8_t *)buffer; |
pascal@215 | 1776 + rc->buffer_size = buffer_size; |
pascal@215 | 1777 + rc->buffer_end = rc->buffer + rc->buffer_size; |
pascal@215 | 1778 + rc->ptr = rc->buffer; |
pascal@215 | 1779 + |
pascal@215 | 1780 + rc->code = 0; |
pascal@215 | 1781 + rc->range = 0xFFFFFFFF; |
pascal@215 | 1782 +} |
pascal@215 | 1783 + |
pascal@215 | 1784 +static always_inline void rc_init_code(rc_t * rc) |
pascal@215 | 1785 +{ |
pascal@215 | 1786 + int i; |
pascal@215 | 1787 + |
pascal@215 | 1788 + for (i = 0; i < 5; i++) { |
pascal@215 | 1789 + if (rc->ptr >= rc->buffer_end) |
pascal@215 | 1790 + rc_read(rc); |
pascal@215 | 1791 + rc->code = (rc->code << 8) | *rc->ptr++; |
pascal@215 | 1792 + } |
pascal@215 | 1793 +} |
pascal@215 | 1794 + |
pascal@215 | 1795 + |
pascal@215 | 1796 +/* Called once. TODO: bb_maybe_free() */ |
pascal@215 | 1797 +static always_inline void rc_free(rc_t * rc) |
pascal@215 | 1798 +{ |
pascal@215 | 1799 + free(rc->buffer); |
pascal@215 | 1800 +} |
pascal@215 | 1801 + |
pascal@215 | 1802 +/* Called twice, but one callsite is in speed_inline'd rc_is_bit_0_helper() */ |
pascal@215 | 1803 +static void rc_do_normalize(rc_t * rc) |
pascal@215 | 1804 +{ |
pascal@215 | 1805 + if (rc->ptr >= rc->buffer_end) |
pascal@215 | 1806 + rc_read(rc); |
pascal@215 | 1807 + rc->range <<= 8; |
pascal@215 | 1808 + rc->code = (rc->code << 8) | *rc->ptr++; |
pascal@215 | 1809 +} |
pascal@215 | 1810 +static always_inline void rc_normalize(rc_t * rc) |
pascal@215 | 1811 +{ |
pascal@215 | 1812 + if (rc->range < (1 << RC_TOP_BITS)) { |
pascal@215 | 1813 + rc_do_normalize(rc); |
pascal@215 | 1814 + } |
pascal@215 | 1815 +} |
pascal@215 | 1816 + |
pascal@215 | 1817 +/* Called 9 times */ |
pascal@215 | 1818 +/* Why rc_is_bit_0_helper exists? |
pascal@215 | 1819 + * Because we want to always expose (rc->code < rc->bound) to optimizer |
pascal@215 | 1820 + */ |
pascal@215 | 1821 +static speed_inline uint32_t rc_is_bit_0_helper(rc_t * rc, uint16_t * p) |
pascal@215 | 1822 +{ |
pascal@215 | 1823 + rc_normalize(rc); |
pascal@215 | 1824 + rc->bound = *p * (rc->range >> RC_MODEL_TOTAL_BITS); |
pascal@215 | 1825 + return rc->bound; |
pascal@215 | 1826 +} |
pascal@215 | 1827 +static always_inline int rc_is_bit_0(rc_t * rc, uint16_t * p) |
pascal@215 | 1828 +{ |
pascal@215 | 1829 + uint32_t t = rc_is_bit_0_helper(rc, p); |
pascal@215 | 1830 + return rc->code < t; |
pascal@215 | 1831 +} |
pascal@215 | 1832 + |
pascal@215 | 1833 +/* Called ~10 times, but very small, thus inlined */ |
pascal@215 | 1834 +static speed_inline void rc_update_bit_0(rc_t * rc, uint16_t * p) |
pascal@215 | 1835 +{ |
pascal@215 | 1836 + rc->range = rc->bound; |
pascal@215 | 1837 + *p += ((1 << RC_MODEL_TOTAL_BITS) - *p) >> RC_MOVE_BITS; |
pascal@215 | 1838 +} |
pascal@215 | 1839 +static speed_inline void rc_update_bit_1(rc_t * rc, uint16_t * p) |
pascal@215 | 1840 +{ |
pascal@215 | 1841 + rc->range -= rc->bound; |
pascal@215 | 1842 + rc->code -= rc->bound; |
pascal@215 | 1843 + *p -= *p >> RC_MOVE_BITS; |
pascal@215 | 1844 +} |
pascal@215 | 1845 + |
pascal@215 | 1846 +/* Called 4 times in unlzma loop */ |
pascal@215 | 1847 +static int rc_get_bit(rc_t * rc, uint16_t * p, int *symbol) |
pascal@215 | 1848 +{ |
pascal@215 | 1849 + if (rc_is_bit_0(rc, p)) { |
pascal@215 | 1850 + rc_update_bit_0(rc, p); |
pascal@215 | 1851 + *symbol *= 2; |
pascal@215 | 1852 + return 0; |
pascal@215 | 1853 + } else { |
pascal@215 | 1854 + rc_update_bit_1(rc, p); |
pascal@215 | 1855 + *symbol = *symbol * 2 + 1; |
pascal@215 | 1856 + return 1; |
pascal@215 | 1857 + } |
pascal@215 | 1858 +} |
pascal@215 | 1859 + |
pascal@215 | 1860 +/* Called once */ |
pascal@215 | 1861 +static always_inline int rc_direct_bit(rc_t * rc) |
pascal@215 | 1862 +{ |
pascal@215 | 1863 + rc_normalize(rc); |
pascal@215 | 1864 + rc->range >>= 1; |
pascal@215 | 1865 + if (rc->code >= rc->range) { |
pascal@215 | 1866 + rc->code -= rc->range; |
pascal@215 | 1867 + return 1; |
pascal@215 | 1868 + } |
pascal@215 | 1869 + return 0; |
pascal@215 | 1870 +} |
pascal@215 | 1871 + |
pascal@215 | 1872 +/* Called twice */ |
pascal@215 | 1873 +static speed_inline void |
pascal@215 | 1874 +rc_bit_tree_decode(rc_t * rc, uint16_t * p, int num_levels, int *symbol) |
pascal@215 | 1875 +{ |
pascal@215 | 1876 + int i = num_levels; |
pascal@215 | 1877 + |
pascal@215 | 1878 + *symbol = 1; |
pascal@215 | 1879 + while (i--) |
pascal@215 | 1880 + rc_get_bit(rc, p + *symbol, symbol); |
pascal@215 | 1881 + *symbol -= 1 << num_levels; |
pascal@215 | 1882 +} |
pascal@215 | 1883 + |
pascal@215 | 1884 + |
pascal@215 | 1885 +/* |
pascal@215 | 1886 + * Small lzma deflate implementation. |
pascal@215 | 1887 + * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org> |
pascal@215 | 1888 + * |
pascal@215 | 1889 + * Based on LzmaDecode.c from the LZMA SDK 4.22 (http://www.7-zip.org/) |
pascal@215 | 1890 + * Copyright (C) 1999-2005 Igor Pavlov |
pascal@215 | 1891 + */ |
pascal@215 | 1892 + |
pascal@215 | 1893 + |
pascal@215 | 1894 +typedef struct { |
pascal@215 | 1895 + uint8_t pos; |
pascal@215 | 1896 + uint32_t dict_size; |
pascal@215 | 1897 + uint64_t dst_size; |
pascal@215 | 1898 +} __attribute__ ((packed)) lzma_header_t; |
pascal@215 | 1899 + |
pascal@215 | 1900 + |
pascal@215 | 1901 +#define LZMA_BASE_SIZE 1846 |
pascal@215 | 1902 +#define LZMA_LIT_SIZE 768 |
pascal@215 | 1903 + |
pascal@215 | 1904 +#define LZMA_NUM_POS_BITS_MAX 4 |
pascal@215 | 1905 + |
pascal@215 | 1906 +#define LZMA_LEN_NUM_LOW_BITS 3 |
pascal@215 | 1907 +#define LZMA_LEN_NUM_MID_BITS 3 |
pascal@215 | 1908 +#define LZMA_LEN_NUM_HIGH_BITS 8 |
pascal@215 | 1909 + |
pascal@215 | 1910 +#define LZMA_LEN_CHOICE 0 |
pascal@215 | 1911 +#define LZMA_LEN_CHOICE_2 (LZMA_LEN_CHOICE + 1) |
pascal@215 | 1912 +#define LZMA_LEN_LOW (LZMA_LEN_CHOICE_2 + 1) |
pascal@215 | 1913 +#define LZMA_LEN_MID (LZMA_LEN_LOW \ |
pascal@215 | 1914 + + (1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_LOW_BITS))) |
pascal@215 | 1915 +#define LZMA_LEN_HIGH (LZMA_LEN_MID \ |
pascal@215 | 1916 + +(1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_MID_BITS))) |
pascal@215 | 1917 +#define LZMA_NUM_LEN_PROBS (LZMA_LEN_HIGH + (1 << LZMA_LEN_NUM_HIGH_BITS)) |
pascal@215 | 1918 + |
pascal@215 | 1919 +#define LZMA_NUM_STATES 12 |
pascal@215 | 1920 +#define LZMA_NUM_LIT_STATES 7 |
pascal@215 | 1921 + |
pascal@215 | 1922 +#define LZMA_START_POS_MODEL_INDEX 4 |
pascal@215 | 1923 +#define LZMA_END_POS_MODEL_INDEX 14 |
pascal@215 | 1924 +#define LZMA_NUM_FULL_DISTANCES (1 << (LZMA_END_POS_MODEL_INDEX >> 1)) |
pascal@215 | 1925 + |
pascal@215 | 1926 +#define LZMA_NUM_POS_SLOT_BITS 6 |
pascal@215 | 1927 +#define LZMA_NUM_LEN_TO_POS_STATES 4 |
pascal@215 | 1928 + |
pascal@215 | 1929 +#define LZMA_NUM_ALIGN_BITS 4 |
pascal@215 | 1930 + |
pascal@215 | 1931 +#define LZMA_MATCH_MIN_LEN 2 |
pascal@215 | 1932 + |
pascal@215 | 1933 +#define LZMA_IS_MATCH 0 |
pascal@215 | 1934 +#define LZMA_IS_REP (LZMA_IS_MATCH + (LZMA_NUM_STATES <<LZMA_NUM_POS_BITS_MAX)) |
pascal@215 | 1935 +#define LZMA_IS_REP_G0 (LZMA_IS_REP + LZMA_NUM_STATES) |
pascal@215 | 1936 +#define LZMA_IS_REP_G1 (LZMA_IS_REP_G0 + LZMA_NUM_STATES) |
pascal@215 | 1937 +#define LZMA_IS_REP_G2 (LZMA_IS_REP_G1 + LZMA_NUM_STATES) |
pascal@215 | 1938 +#define LZMA_IS_REP_0_LONG (LZMA_IS_REP_G2 + LZMA_NUM_STATES) |
pascal@215 | 1939 +#define LZMA_POS_SLOT (LZMA_IS_REP_0_LONG \ |
pascal@215 | 1940 + + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX)) |
pascal@215 | 1941 +#define LZMA_SPEC_POS (LZMA_POS_SLOT \ |
pascal@215 | 1942 + +(LZMA_NUM_LEN_TO_POS_STATES << LZMA_NUM_POS_SLOT_BITS)) |
pascal@215 | 1943 +#define LZMA_ALIGN (LZMA_SPEC_POS \ |
pascal@215 | 1944 + + LZMA_NUM_FULL_DISTANCES - LZMA_END_POS_MODEL_INDEX) |
pascal@215 | 1945 +#define LZMA_LEN_CODER (LZMA_ALIGN + (1 << LZMA_NUM_ALIGN_BITS)) |
pascal@215 | 1946 +#define LZMA_REP_LEN_CODER (LZMA_LEN_CODER + LZMA_NUM_LEN_PROBS) |
pascal@215 | 1947 +#define LZMA_LITERAL (LZMA_REP_LEN_CODER + LZMA_NUM_LEN_PROBS) |
pascal@215 | 1948 + |
pascal@215 | 1949 + |
pascal@215 | 1950 +STATIC int unlzma(char *inbuf, int in_len, |
pascal@215 | 1951 + int(*fill)(void*,unsigned int), |
pascal@215 | 1952 + int(*writebb)(char*,unsigned int), |
pascal@215 | 1953 + int *posp) |
pascal@215 | 1954 +{ |
pascal@215 | 1955 + lzma_header_t header; |
pascal@215 | 1956 + int lc, pb, lp; |
pascal@215 | 1957 + uint32_t pos_state_mask; |
pascal@215 | 1958 + uint32_t literal_pos_mask; |
pascal@215 | 1959 + uint32_t pos; |
pascal@215 | 1960 + uint16_t *p; |
pascal@215 | 1961 + uint16_t *prob; |
pascal@215 | 1962 + uint16_t *prob_lit; |
pascal@215 | 1963 + int num_bits; |
pascal@215 | 1964 + int num_probs; |
pascal@215 | 1965 + rc_t rc; |
pascal@215 | 1966 + int i, mi; |
pascal@215 | 1967 + uint8_t *buffer; |
pascal@215 | 1968 + uint8_t previous_byte = 0; |
pascal@215 | 1969 + size_t buffer_pos = 0, global_pos = 0; |
pascal@215 | 1970 + int len = 0; |
pascal@215 | 1971 + int state = 0; |
pascal@215 | 1972 + int bufsize; |
pascal@215 | 1973 + uint32_t rep0 = 1, rep1 = 1, rep2 = 1, rep3 = 1; |
pascal@215 | 1974 + |
pascal@215 | 1975 + rc_init(&rc, fill, inbuf, in_len); |
pascal@215 | 1976 + |
pascal@215 | 1977 + for (i = 0; i < sizeof(header); i++) { |
pascal@215 | 1978 + if (rc.ptr >= rc.buffer_end) |
pascal@215 | 1979 + rc_read(&rc); |
pascal@215 | 1980 + ((unsigned char *)&header)[i] = *rc.ptr++; |
pascal@215 | 1981 + } |
pascal@215 | 1982 + |
pascal@215 | 1983 + if (header.pos >= (9 * 5 * 5)) |
pascal@215 | 1984 + error("bad header"); |
pascal@215 | 1985 + |
pascal@215 | 1986 + mi = header.pos / 9; |
pascal@215 | 1987 + lc = header.pos % 9; |
pascal@215 | 1988 + pb = mi / 5; |
pascal@215 | 1989 + lp = mi % 5; |
pascal@215 | 1990 + pos_state_mask = (1 << pb) - 1; |
pascal@215 | 1991 + literal_pos_mask = (1 << lp) - 1; |
pascal@215 | 1992 + |
pascal@215 | 1993 + ENDIAN_CONVERT(header.dict_size); |
pascal@215 | 1994 + ENDIAN_CONVERT(header.dst_size); |
pascal@215 | 1995 + |
pascal@215 | 1996 + if (header.dict_size == 0) |
pascal@215 | 1997 + header.dict_size = 1; |
pascal@215 | 1998 + |
pascal@215 | 1999 + bufsize = MIN(header.dst_size, header.dict_size); |
pascal@215 | 2000 + buffer = large_malloc(bufsize); |
pascal@215 | 2001 + if(buffer == NULL) |
pascal@215 | 2002 + return -1; |
pascal@215 | 2003 + |
pascal@215 | 2004 + num_probs = LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp)); |
pascal@215 | 2005 + p = large_malloc(num_probs * sizeof(*p)); |
pascal@215 | 2006 + num_probs = LZMA_LITERAL + (LZMA_LIT_SIZE << (lc + lp)); |
pascal@215 | 2007 + for (i = 0; i < num_probs; i++) |
pascal@215 | 2008 + p[i] = (1 << RC_MODEL_TOTAL_BITS) >> 1; |
pascal@215 | 2009 + |
pascal@215 | 2010 + rc_init_code(&rc); |
pascal@215 | 2011 + |
pascal@215 | 2012 + while (global_pos + buffer_pos < header.dst_size) { |
pascal@215 | 2013 + int pos_state = (buffer_pos + global_pos) & pos_state_mask; |
pascal@215 | 2014 + |
pascal@215 | 2015 + prob = |
pascal@215 | 2016 + p + LZMA_IS_MATCH + (state << LZMA_NUM_POS_BITS_MAX) + pos_state; |
pascal@215 | 2017 + if (rc_is_bit_0(&rc, prob)) { |
pascal@215 | 2018 + mi = 1; |
pascal@215 | 2019 + rc_update_bit_0(&rc, prob); |
pascal@215 | 2020 + prob = (p + LZMA_LITERAL + (LZMA_LIT_SIZE |
pascal@215 | 2021 + * ((((buffer_pos + global_pos) & literal_pos_mask) << lc) |
pascal@215 | 2022 + + (previous_byte >> (8 - lc))))); |
pascal@215 | 2023 + |
pascal@215 | 2024 + if (state >= LZMA_NUM_LIT_STATES) { |
pascal@215 | 2025 + int match_byte; |
pascal@215 | 2026 + |
pascal@215 | 2027 + pos = buffer_pos - rep0; |
pascal@215 | 2028 + while (pos >= header.dict_size) |
pascal@215 | 2029 + pos += header.dict_size; |
pascal@215 | 2030 + if(pos >= bufsize) { |
pascal@215 | 2031 + goto fail; |
pascal@215 | 2032 + } |
pascal@215 | 2033 + match_byte = buffer[pos]; |
pascal@215 | 2034 + do { |
pascal@215 | 2035 + int bit; |
pascal@215 | 2036 + |
pascal@215 | 2037 + match_byte <<= 1; |
pascal@215 | 2038 + bit = match_byte & 0x100; |
pascal@215 | 2039 + prob_lit = prob + 0x100 + bit + mi; |
pascal@215 | 2040 + if (rc_get_bit(&rc, prob_lit, &mi)) { |
pascal@215 | 2041 + if (!bit) |
pascal@215 | 2042 + break; |
pascal@215 | 2043 + } else { |
pascal@215 | 2044 + if (bit) |
pascal@215 | 2045 + break; |
pascal@215 | 2046 + } |
pascal@215 | 2047 + } while (mi < 0x100); |
pascal@215 | 2048 + } |
pascal@215 | 2049 + while (mi < 0x100) { |
pascal@215 | 2050 + prob_lit = prob + mi; |
pascal@215 | 2051 + rc_get_bit(&rc, prob_lit, &mi); |
pascal@215 | 2052 + } |
pascal@215 | 2053 + previous_byte = (uint8_t) mi; |
pascal@215 | 2054 + |
pascal@215 | 2055 + buffer[buffer_pos++] = previous_byte; |
pascal@215 | 2056 + if (buffer_pos == header.dict_size) { |
pascal@215 | 2057 + buffer_pos = 0; |
pascal@215 | 2058 + global_pos += header.dict_size; |
pascal@215 | 2059 + writebb((char*)buffer, header.dict_size); |
pascal@215 | 2060 + } |
pascal@215 | 2061 + if (state < 4) |
pascal@215 | 2062 + state = 0; |
pascal@215 | 2063 + else if (state < 10) |
pascal@215 | 2064 + state -= 3; |
pascal@215 | 2065 + else |
pascal@215 | 2066 + state -= 6; |
pascal@215 | 2067 + } else { |
pascal@215 | 2068 + int offset; |
pascal@215 | 2069 + uint16_t *prob_len; |
pascal@215 | 2070 + |
pascal@215 | 2071 + rc_update_bit_1(&rc, prob); |
pascal@215 | 2072 + prob = p + LZMA_IS_REP + state; |
pascal@215 | 2073 + if (rc_is_bit_0(&rc, prob)) { |
pascal@215 | 2074 + rc_update_bit_0(&rc, prob); |
pascal@215 | 2075 + rep3 = rep2; |
pascal@215 | 2076 + rep2 = rep1; |
pascal@215 | 2077 + rep1 = rep0; |
pascal@215 | 2078 + state = state < LZMA_NUM_LIT_STATES ? 0 : 3; |
pascal@215 | 2079 + prob = p + LZMA_LEN_CODER; |
pascal@215 | 2080 + } else { |
pascal@215 | 2081 + rc_update_bit_1(&rc, prob); |
pascal@215 | 2082 + prob = p + LZMA_IS_REP_G0 + state; |
pascal@215 | 2083 + if (rc_is_bit_0(&rc, prob)) { |
pascal@215 | 2084 + rc_update_bit_0(&rc, prob); |
pascal@215 | 2085 + prob = (p + LZMA_IS_REP_0_LONG |
pascal@215 | 2086 + + (state << LZMA_NUM_POS_BITS_MAX) + pos_state); |
pascal@215 | 2087 + if (rc_is_bit_0(&rc, prob)) { |
pascal@215 | 2088 + rc_update_bit_0(&rc, prob); |
pascal@215 | 2089 + |
pascal@215 | 2090 + state = state < LZMA_NUM_LIT_STATES ? 9 : 11; |
pascal@215 | 2091 + pos = buffer_pos - rep0; |
pascal@215 | 2092 + while (pos >= header.dict_size) |
pascal@215 | 2093 + pos += header.dict_size; |
pascal@215 | 2094 + if(pos >= bufsize) { |
pascal@215 | 2095 + goto fail; |
pascal@215 | 2096 + } |
pascal@215 | 2097 + previous_byte = buffer[pos]; |
pascal@215 | 2098 + buffer[buffer_pos++] = previous_byte; |
pascal@215 | 2099 + if (buffer_pos == header.dict_size) { |
pascal@215 | 2100 + buffer_pos = 0; |
pascal@215 | 2101 + global_pos += header.dict_size; |
pascal@215 | 2102 + writebb((char*)buffer, header.dict_size); |
pascal@215 | 2103 + } |
pascal@215 | 2104 + continue; |
pascal@215 | 2105 + } else { |
pascal@215 | 2106 + rc_update_bit_1(&rc, prob); |
pascal@215 | 2107 + } |
pascal@215 | 2108 + } else { |
pascal@215 | 2109 + uint32_t distance; |
pascal@215 | 2110 + |
pascal@215 | 2111 + rc_update_bit_1(&rc, prob); |
pascal@215 | 2112 + prob = p + LZMA_IS_REP_G1 + state; |
pascal@215 | 2113 + if (rc_is_bit_0(&rc, prob)) { |
pascal@215 | 2114 + rc_update_bit_0(&rc, prob); |
pascal@215 | 2115 + distance = rep1; |
pascal@215 | 2116 + } else { |
pascal@215 | 2117 + rc_update_bit_1(&rc, prob); |
pascal@215 | 2118 + prob = p + LZMA_IS_REP_G2 + state; |
pascal@215 | 2119 + if (rc_is_bit_0(&rc, prob)) { |
pascal@215 | 2120 + rc_update_bit_0(&rc, prob); |
pascal@215 | 2121 + distance = rep2; |
pascal@215 | 2122 + } else { |
pascal@215 | 2123 + rc_update_bit_1(&rc, prob); |
pascal@215 | 2124 + distance = rep3; |
pascal@215 | 2125 + rep3 = rep2; |
pascal@215 | 2126 + } |
pascal@215 | 2127 + rep2 = rep1; |
pascal@215 | 2128 + } |
pascal@215 | 2129 + rep1 = rep0; |
pascal@215 | 2130 + rep0 = distance; |
pascal@215 | 2131 + } |
pascal@215 | 2132 + state = state < LZMA_NUM_LIT_STATES ? 8 : 11; |
pascal@215 | 2133 + prob = p + LZMA_REP_LEN_CODER; |
pascal@215 | 2134 + } |
pascal@215 | 2135 + |
pascal@215 | 2136 + prob_len = prob + LZMA_LEN_CHOICE; |
pascal@215 | 2137 + if (rc_is_bit_0(&rc, prob_len)) { |
pascal@215 | 2138 + rc_update_bit_0(&rc, prob_len); |
pascal@215 | 2139 + prob_len = (prob + LZMA_LEN_LOW |
pascal@215 | 2140 + + (pos_state << LZMA_LEN_NUM_LOW_BITS)); |
pascal@215 | 2141 + offset = 0; |
pascal@215 | 2142 + num_bits = LZMA_LEN_NUM_LOW_BITS; |
pascal@215 | 2143 + } else { |
pascal@215 | 2144 + rc_update_bit_1(&rc, prob_len); |
pascal@215 | 2145 + prob_len = prob + LZMA_LEN_CHOICE_2; |
pascal@215 | 2146 + if (rc_is_bit_0(&rc, prob_len)) { |
pascal@215 | 2147 + rc_update_bit_0(&rc, prob_len); |
pascal@215 | 2148 + prob_len = (prob + LZMA_LEN_MID |
pascal@215 | 2149 + + (pos_state << LZMA_LEN_NUM_MID_BITS)); |
pascal@215 | 2150 + offset = 1 << LZMA_LEN_NUM_LOW_BITS; |
pascal@215 | 2151 + num_bits = LZMA_LEN_NUM_MID_BITS; |
pascal@215 | 2152 + } else { |
pascal@215 | 2153 + rc_update_bit_1(&rc, prob_len); |
pascal@215 | 2154 + prob_len = prob + LZMA_LEN_HIGH; |
pascal@215 | 2155 + offset = ((1 << LZMA_LEN_NUM_LOW_BITS) |
pascal@215 | 2156 + + (1 << LZMA_LEN_NUM_MID_BITS)); |
pascal@215 | 2157 + num_bits = LZMA_LEN_NUM_HIGH_BITS; |
pascal@215 | 2158 + } |
pascal@215 | 2159 + } |
pascal@215 | 2160 + rc_bit_tree_decode(&rc, prob_len, num_bits, &len); |
pascal@215 | 2161 + len += offset; |
pascal@215 | 2162 + |
pascal@215 | 2163 + if (state < 4) { |
pascal@215 | 2164 + int pos_slot; |
pascal@215 | 2165 + |
pascal@215 | 2166 + state += LZMA_NUM_LIT_STATES; |
pascal@215 | 2167 + prob = |
pascal@215 | 2168 + p + LZMA_POS_SLOT + |
pascal@215 | 2169 + ((len < |
pascal@215 | 2170 + LZMA_NUM_LEN_TO_POS_STATES ? len : |
pascal@215 | 2171 + LZMA_NUM_LEN_TO_POS_STATES - 1) |
pascal@215 | 2172 + << LZMA_NUM_POS_SLOT_BITS); |
pascal@215 | 2173 + rc_bit_tree_decode(&rc, prob, LZMA_NUM_POS_SLOT_BITS, |
pascal@215 | 2174 + &pos_slot); |
pascal@215 | 2175 + if (pos_slot >= LZMA_START_POS_MODEL_INDEX) { |
pascal@215 | 2176 + num_bits = (pos_slot >> 1) - 1; |
pascal@215 | 2177 + rep0 = 2 | (pos_slot & 1); |
pascal@215 | 2178 + if (pos_slot < LZMA_END_POS_MODEL_INDEX) { |
pascal@215 | 2179 + rep0 <<= num_bits; |
pascal@215 | 2180 + prob = p + LZMA_SPEC_POS + rep0 - pos_slot - 1; |
pascal@215 | 2181 + } else { |
pascal@215 | 2182 + num_bits -= LZMA_NUM_ALIGN_BITS; |
pascal@215 | 2183 + while (num_bits--) |
pascal@215 | 2184 + rep0 = (rep0 << 1) | rc_direct_bit(&rc); |
pascal@215 | 2185 + prob = p + LZMA_ALIGN; |
pascal@215 | 2186 + rep0 <<= LZMA_NUM_ALIGN_BITS; |
pascal@215 | 2187 + num_bits = LZMA_NUM_ALIGN_BITS; |
pascal@215 | 2188 + } |
pascal@215 | 2189 + i = 1; |
pascal@215 | 2190 + mi = 1; |
pascal@215 | 2191 + while (num_bits--) { |
pascal@215 | 2192 + if (rc_get_bit(&rc, prob + mi, &mi)) |
pascal@215 | 2193 + rep0 |= i; |
pascal@215 | 2194 + i <<= 1; |
pascal@215 | 2195 + } |
pascal@215 | 2196 + } else |
pascal@215 | 2197 + rep0 = pos_slot; |
pascal@215 | 2198 + if (++rep0 == 0) |
pascal@215 | 2199 + break; |
pascal@215 | 2200 + } |
pascal@215 | 2201 + |
pascal@215 | 2202 + len += LZMA_MATCH_MIN_LEN; |
pascal@215 | 2203 + |
pascal@215 | 2204 + do { |
pascal@215 | 2205 + pos = buffer_pos - rep0; |
pascal@215 | 2206 + while (pos >= header.dict_size) |
pascal@215 | 2207 + pos += header.dict_size; |
pascal@215 | 2208 + if(pos >= bufsize) { |
pascal@215 | 2209 + goto fail; |
pascal@215 | 2210 + } |
pascal@215 | 2211 + previous_byte = buffer[pos]; |
pascal@215 | 2212 + buffer[buffer_pos++] = previous_byte; |
pascal@215 | 2213 + if (buffer_pos == header.dict_size) { |
pascal@215 | 2214 + buffer_pos = 0; |
pascal@215 | 2215 + global_pos += header.dict_size; |
pascal@215 | 2216 + writebb((char*)buffer, header.dict_size); |
pascal@215 | 2217 + } |
pascal@215 | 2218 + len--; |
pascal@215 | 2219 + } while (len != 0 && buffer_pos < header.dst_size); |
pascal@215 | 2220 + } |
pascal@215 | 2221 + } |
pascal@215 | 2222 + |
pascal@215 | 2223 + writebb((char*)buffer, buffer_pos); |
pascal@215 | 2224 + if(posp) { |
pascal@215 | 2225 + *posp = rc.ptr-rc.buffer; |
pascal@215 | 2226 + } |
pascal@215 | 2227 + large_free(buffer); |
pascal@215 | 2228 + return 0; |
pascal@215 | 2229 + fail: |
pascal@215 | 2230 + large_free(buffer); |
pascal@215 | 2231 + return -1; |
pascal@215 | 2232 +} |
pascal@243 | 2233 |
pascal@215 | 2234 --- linux-2.6.24.2/lib/Makefile |
pascal@215 | 2235 +++ linux-2.6.24.2/lib/Makefile |
pascal@215 | 2236 @@ -50,6 +50,10 @@ obj-$(CONFIG_CRC7) += crc7.o |
pascal@215 | 2237 obj-$(CONFIG_LIBCRC32C) += libcrc32c.o |
pascal@215 | 2238 obj-$(CONFIG_GENERIC_ALLOCATOR) += genalloc.o |
pascal@215 | 2239 |
pascal@215 | 2240 +obj-$(CONFIG_RD_BZIP2) += decompress_bunzip2.o |
pascal@215 | 2241 +obj-$(CONFIG_RD_LZMA) += decompress_unlzma.o |
pascal@215 | 2242 + |
pascal@215 | 2243 + |
pascal@215 | 2244 obj-$(CONFIG_ZLIB_INFLATE) += zlib_inflate/ |
pascal@215 | 2245 obj-$(CONFIG_ZLIB_DEFLATE) += zlib_deflate/ |
pascal@215 | 2246 obj-$(CONFIG_REED_SOLOMON) += reed_solomon/ |
pascal@243 | 2247 |
pascal@215 | 2248 --- linux-2.6.24.2/scripts/Makefile.lib |
pascal@215 | 2249 +++ linux-2.6.24.2/scripts/Makefile.lib |
pascal@215 | 2250 @@ -166,4 +166,17 @@ cmd_objcopy = $(OBJCOPY) $(OBJCOPYFLAGS) |
pascal@215 | 2251 quiet_cmd_gzip = GZIP $@ |
pascal@215 | 2252 cmd_gzip = gzip -f -9 < $< > $@ |
pascal@215 | 2253 |
pascal@215 | 2254 +# Append size |
pascal@215 | 2255 +size_append=perl -e 'print(pack("i",(stat($$ARGV[0]))[7]));' |
pascal@215 | 2256 |
pascal@215 | 2257 +# Bzip2 |
pascal@215 | 2258 +# --------------------------------------------------------------------------- |
pascal@215 | 2259 + |
pascal@215 | 2260 +quiet_cmd_bzip2 = BZIP2 $@ |
pascal@215 | 2261 +cmd_bzip2 = (bzip2 -9 < $< ; $(size_append) $<) > $@ |
pascal@215 | 2262 + |
pascal@215 | 2263 +# Lzma |
pascal@215 | 2264 +# --------------------------------------------------------------------------- |
pascal@215 | 2265 + |
pascal@215 | 2266 +quiet_cmd_lzma = LZMA $@ |
pascal@215 | 2267 +cmd_lzma = (lzma e $< -so ; $(size_append) $<) >$@ |