aboutsummaryrefslogtreecommitdiff
path: root/archival
diff options
context:
space:
mode:
Diffstat (limited to 'archival')
-rw-r--r--archival/libunarchive/decompress_unzip.c755
-rw-r--r--archival/libunarchive/unzip.c755
2 files changed, 764 insertions, 746 deletions
diff --git a/archival/libunarchive/decompress_unzip.c b/archival/libunarchive/decompress_unzip.c
index cde16d067..76fb86285 100644
--- a/archival/libunarchive/decompress_unzip.c
+++ b/archival/libunarchive/decompress_unzip.c
@@ -69,7 +69,7 @@ static char *license_msg[] = {
69#include "libbb.h" 69#include "libbb.h"
70 70
71#ifdef CONFIG_FEATURE_UNCOMPRESS 71#ifdef CONFIG_FEATURE_UNCOMPRESS
72int uncompress ( FILE *in, FILE *out ); 72int uncompress(FILE * in, FILE * out);
73#endif 73#endif
74 74
75static FILE *in_file, *out_file; 75static FILE *in_file, *out_file;
@@ -78,7 +78,7 @@ static FILE *in_file, *out_file;
78static unsigned char *window; 78static unsigned char *window;
79static unsigned long *crc_table; 79static unsigned long *crc_table;
80 80
81static unsigned long crc; /* shift register contents */ 81static unsigned long crc; /* shift register contents */
82 82
83/* Return codes from gzip */ 83/* Return codes from gzip */
84static const int ERROR = 1; 84static const int ERROR = 1;
@@ -90,21 +90,21 @@ static const int ERROR = 1;
90static const int WSIZE = 0x8000; 90static const int WSIZE = 0x8000;
91 91
92/* If BMAX needs to be larger than 16, then h and x[] should be ulg. */ 92/* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
93static const int BMAX = 16; /* maximum bit length of any code (16 for explode) */ 93static const int BMAX = 16; /* maximum bit length of any code (16 for explode) */
94static const int N_MAX = 288; /* maximum number of codes in any set */ 94static const int N_MAX = 288; /* maximum number of codes in any set */
95 95
96static long bytes_out; /* number of output bytes */ 96static long bytes_out; /* number of output bytes */
97static unsigned long outcnt; /* bytes in output buffer */ 97static unsigned long outcnt; /* bytes in output buffer */
98 98
99static unsigned hufts; /* track memory usage */ 99static unsigned hufts; /* track memory usage */
100static unsigned long bb; /* bit buffer */ 100static unsigned long bb; /* bit buffer */
101static unsigned bk; /* bits in bit buffer */ 101static unsigned bk; /* bits in bit buffer */
102 102
103typedef struct huft_s { 103typedef struct huft_s {
104 unsigned char e; /* number of extra bits or operation */ 104 unsigned char e; /* number of extra bits or operation */
105 unsigned char b; /* number of bits in this code or subcode */ 105 unsigned char b; /* number of bits in this code or subcode */
106 union { 106 union {
107 unsigned short n; /* literal, length base, or distance base */ 107 unsigned short n; /* literal, length base, or distance base */
108 struct huft_s *t; /* pointer to next level of table */ 108 struct huft_s *t; /* pointer to next level of table */
109 } v; 109 } v;
110} huft_t; 110} huft_t;
@@ -115,11 +115,11 @@ static const unsigned short mask_bits[] = {
115 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff 115 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
116}; 116};
117 117
118//static int error_number = 0; 118/* static int error_number = 0; */
119/* ======================================================================== 119/* ========================================================================
120 * Signal and error handler. 120 * Signal and error handler.
121 */ 121 */
122 122
123static void abort_gzip(void) 123static void abort_gzip(void)
124{ 124{
125 error_msg("gzip aborted\n"); 125 error_msg("gzip aborted\n");
@@ -128,26 +128,28 @@ static void abort_gzip(void)
128 128
129static void make_crc_table(void) 129static void make_crc_table(void)
130{ 130{
131 const unsigned long poly = 0xedb88320; /* polynomial exclusive-or pattern */ 131 const unsigned long poly = 0xedb88320; /* polynomial exclusive-or pattern */
132 unsigned short i; /* counter for all possible eight bit values */ 132 unsigned short i; /* counter for all possible eight bit values */
133 133
134 /* initial shift register value */ 134 /* initial shift register value */
135 crc = 0xffffffffL; 135 crc = 0xffffffffL;
136 crc_table = (unsigned long *) malloc(256 * sizeof(unsigned long)); 136 crc_table = (unsigned long *) malloc(256 * sizeof(unsigned long));
137 137
138 /* Compute and print table of CRC's, five per line */ 138 /* Compute and print table of CRC's, five per line */
139 for (i = 0; i < 256; i++) { 139 for (i = 0; i < 256; i++) {
140 unsigned long table_entry; /* crc shift register */ 140 unsigned long table_entry; /* crc shift register */
141 char k; /* byte being shifted into crc apparatus */ 141 char k; /* byte being shifted into crc apparatus */
142 142
143 table_entry = i; 143 table_entry = i;
144 /* The idea to initialize the register with the byte instead of 144 /* The idea to initialize the register with the byte instead of
145 * zero was stolen from Haruhiko Okumura's ar002 145 * zero was stolen from Haruhiko Okumura's ar002
146 */ 146 */
147 for (k = 8; k; k--) { 147 for (k = 8; k; k--) {
148 table_entry = table_entry & 1 ? (table_entry >> 1) ^ poly : table_entry >> 1; 148 table_entry =
149 table_entry & 1 ? (table_entry >> 1) ^ poly : table_entry >>
150 1;
149 } 151 }
150 crc_table[i]=table_entry; 152 crc_table[i] = table_entry;
151 } 153 }
152} 154}
153 155
@@ -179,7 +181,7 @@ static void flush_window(void)
179 * each table. 181 * each table.
180 * t: table to free 182 * t: table to free
181 */ 183 */
182static int huft_free(huft_t *t) 184static int huft_free(huft_t * t)
183{ 185{
184 huft_t *p, *q; 186 huft_t *p, *q;
185 187
@@ -209,36 +211,37 @@ typedef unsigned char extra_bits_t;
209 * t: result: starting table 211 * t: result: starting table
210 * m: maximum lookup bits, returns actual 212 * m: maximum lookup bits, returns actual
211 */ 213 */
212static int huft_build(unsigned int *b, const unsigned int n, const unsigned int s, 214static int huft_build(unsigned int *b, const unsigned int n,
213 const unsigned short *d, const extra_bits_t *e, huft_t **t, int *m) 215 const unsigned int s, const unsigned short *d,
216 const extra_bits_t * e, huft_t ** t, int *m)
214{ 217{
215 unsigned a; /* counter for codes of length k */ 218 unsigned a; /* counter for codes of length k */
216 unsigned c[BMAX + 1]; /* bit length count table */ 219 unsigned c[BMAX + 1]; /* bit length count table */
217 unsigned f; /* i repeats in table every f entries */ 220 unsigned f; /* i repeats in table every f entries */
218 int g; /* maximum code length */ 221 int g; /* maximum code length */
219 int h; /* table level */ 222 int h; /* table level */
220 register unsigned i; /* counter, current code */ 223 register unsigned i; /* counter, current code */
221 register unsigned j; /* counter */ 224 register unsigned j; /* counter */
222 register int k; /* number of bits in current code */ 225 register int k; /* number of bits in current code */
223 int l; /* bits per table (returned in m) */ 226 int l; /* bits per table (returned in m) */
224 register unsigned *p; /* pointer into c[], b[], or v[] */ 227 register unsigned *p; /* pointer into c[], b[], or v[] */
225 register huft_t *q; /* points to current table */ 228 register huft_t *q; /* points to current table */
226 huft_t r; /* table entry for structure assignment */ 229 huft_t r; /* table entry for structure assignment */
227 huft_t *u[BMAX]; /* table stack */ 230 huft_t *u[BMAX]; /* table stack */
228 unsigned v[N_MAX]; /* values in order of bit length */ 231 unsigned v[N_MAX]; /* values in order of bit length */
229 register int w; /* bits before this table == (l * h) */ 232 register int w; /* bits before this table == (l * h) */
230 unsigned x[BMAX + 1]; /* bit offsets, then code stack */ 233 unsigned x[BMAX + 1]; /* bit offsets, then code stack */
231 unsigned *xp; /* pointer into x */ 234 unsigned *xp; /* pointer into x */
232 int y; /* number of dummy codes added */ 235 int y; /* number of dummy codes added */
233 unsigned z; /* number of entries in current table */ 236 unsigned z; /* number of entries in current table */
234 237
235 /* Generate counts for each bit length */ 238 /* Generate counts for each bit length */
236 memset ((void *)(c), 0, sizeof(c)); 239 memset((void *) (c), 0, sizeof(c));
237 p = b; 240 p = b;
238 i = n; 241 i = n;
239 do { 242 do {
240 c[*p]++; /* assume all entries <= BMAX */ 243 c[*p]++; /* assume all entries <= BMAX */
241 p++; /* Can't combine with above line (Solaris bug) */ 244 p++; /* Can't combine with above line (Solaris bug) */
242 } while (--i); 245 } while (--i);
243 if (c[0] == n) { /* null input--all zero length codes */ 246 if (c[0] == n) { /* null input--all zero length codes */
244 *t = (huft_t *) NULL; 247 *t = (huft_t *) NULL;
@@ -274,7 +277,7 @@ static int huft_build(unsigned int *b, const unsigned int n, const unsigned int
274 x[1] = j = 0; 277 x[1] = j = 0;
275 p = c + 1; 278 p = c + 1;
276 xp = x + 2; 279 xp = x + 2;
277 while (--i) { /* note that i == g from above */ 280 while (--i) { /* note that i == g from above */
278 *xp++ = (j += *p++); 281 *xp++ = (j += *p++);
279 } 282 }
280 283
@@ -287,7 +290,7 @@ static int huft_build(unsigned int *b, const unsigned int n, const unsigned int
287 } while (++i < n); 290 } while (++i < n);
288 291
289 /* Generate the Huffman codes and for each, make the table entries */ 292 /* Generate the Huffman codes and for each, make the table entries */
290 x[0] = i = 0; /* first Huffman code is zero */ 293 x[0] = i = 0; /* first Huffman code is zero */
291 p = v; /* grab values in bit order */ 294 p = v; /* grab values in bit order */
292 h = -1; /* no tables yet--level -1 */ 295 h = -1; /* no tables yet--level -1 */
293 w = -l; /* bits decoded == (l * h) */ 296 w = -l; /* bits decoded == (l * h) */
@@ -303,7 +306,7 @@ static int huft_build(unsigned int *b, const unsigned int n, const unsigned int
303 /* make tables up to required level */ 306 /* make tables up to required level */
304 while (k > w + l) { 307 while (k > w + l) {
305 h++; 308 h++;
306 w += l; /* previous table always l bits */ 309 w += l; /* previous table always l bits */
307 310
308 /* compute minimum size table less than or equal to l bits */ 311 /* compute minimum size table less than or equal to l bits */
309 z = (z = g - w) > (unsigned) l ? l : z; /* upper limit on table size */ 312 z = (z = g - w) > (unsigned) l ? l : z; /* upper limit on table size */
@@ -316,15 +319,15 @@ static int huft_build(unsigned int *b, const unsigned int n, const unsigned int
316 f -= *xp; /* else deduct codes from patterns */ 319 f -= *xp; /* else deduct codes from patterns */
317 } 320 }
318 } 321 }
319 z = 1 << j; /* table entries for j-bit table */ 322 z = 1 << j; /* table entries for j-bit table */
320 323
321 /* allocate and link in new table */ 324 /* allocate and link in new table */
322 q = (huft_t *) xmalloc((z + 1) * sizeof(huft_t)); 325 q = (huft_t *) xmalloc((z + 1) * sizeof(huft_t));
323 326
324 hufts += z + 1; /* track memory usage */ 327 hufts += z + 1; /* track memory usage */
325 *t = q + 1; /* link to list for huft_free() */ 328 *t = q + 1; /* link to list for huft_free() */
326 *(t = &(q->v.t)) = NULL; 329 *(t = &(q->v.t)) = NULL;
327 u[h] = ++q; /* table starts after link */ 330 u[h] = ++q; /* table starts after link */
328 331
329 /* connect to last table, if there is one */ 332 /* connect to last table, if there is one */
330 if (h) { 333 if (h) {
@@ -340,11 +343,11 @@ static int huft_build(unsigned int *b, const unsigned int n, const unsigned int
340 /* set up table entry in r */ 343 /* set up table entry in r */
341 r.b = (unsigned char) (k - w); 344 r.b = (unsigned char) (k - w);
342 if (p >= v + n) 345 if (p >= v + n)
343 r.e = 99; /* out of values--invalid code */ 346 r.e = 99; /* out of values--invalid code */
344 else if (*p < s) { 347 else if (*p < s) {
345 r.e = (unsigned char) (*p < 256 ? 16 : 15); /* 256 is end-of-block code */ 348 r.e = (unsigned char) (*p < 256 ? 16 : 15); /* 256 is end-of-block code */
346 r.v.n = (unsigned short) (*p); /* simple code is just the value */ 349 r.v.n = (unsigned short) (*p); /* simple code is just the value */
347 p++; /* one compiler does not like *p++ */ 350 p++; /* one compiler does not like *p++ */
348 } else { 351 } else {
349 r.e = (unsigned char) e[*p - s]; /* non-simple--look up in lists */ 352 r.e = (unsigned char) e[*p - s]; /* non-simple--look up in lists */
350 r.v.n = d[*p++ - s]; 353 r.v.n = d[*p++ - s];
@@ -362,7 +365,7 @@ static int huft_build(unsigned int *b, const unsigned int n, const unsigned int
362 365
363 /* backup over finished tables */ 366 /* backup over finished tables */
364 while ((i & ((1 << w) - 1)) != x[h]) { 367 while ((i & ((1 << w) - 1)) != x[h]) {
365 h--; /* don't need to update q */ 368 h--; /* don't need to update q */
366 w -= l; 369 w -= l;
367 } 370 }
368 } 371 }
@@ -378,52 +381,52 @@ static int huft_build(unsigned int *b, const unsigned int n, const unsigned int
378 * tl, td: literal/length and distance decoder tables 381 * tl, td: literal/length and distance decoder tables
379 * bl, bd: number of bits decoded by tl[] and td[] 382 * bl, bd: number of bits decoded by tl[] and td[]
380 */ 383 */
381static int inflate_codes(huft_t *tl, huft_t *td, int bl, int bd) 384static int inflate_codes(huft_t * tl, huft_t * td, int bl, int bd)
382{ 385{
383 register unsigned long e; /* table entry flag/number of extra bits */ 386 register unsigned long e; /* table entry flag/number of extra bits */
384 unsigned long n, d; /* length and index for copy */ 387 unsigned long n, d; /* length and index for copy */
385 unsigned long w; /* current window position */ 388 unsigned long w; /* current window position */
386 huft_t *t; /* pointer to table entry */ 389 huft_t *t; /* pointer to table entry */
387 unsigned ml, md; /* masks for bl and bd bits */ 390 unsigned ml, md; /* masks for bl and bd bits */
388 register unsigned long b; /* bit buffer */ 391 register unsigned long b; /* bit buffer */
389 register unsigned k; /* number of bits in bit buffer */ 392 register unsigned k; /* number of bits in bit buffer */
390 393
391 /* make local copies of globals */ 394 /* make local copies of globals */
392 b = bb; /* initialize bit buffer */ 395 b = bb; /* initialize bit buffer */
393 k = bk; 396 k = bk;
394 w = outcnt; /* initialize window position */ 397 w = outcnt; /* initialize window position */
395 398
396 /* inflate the coded data */ 399 /* inflate the coded data */
397 ml = mask_bits[bl]; /* precompute masks for speed */ 400 ml = mask_bits[bl]; /* precompute masks for speed */
398 md = mask_bits[bd]; 401 md = mask_bits[bd];
399 for (;;) { /* do until end of block */ 402 for (;;) { /* do until end of block */
400 while (k < (unsigned) bl) { 403 while (k < (unsigned) bl) {
401 b |= ((unsigned long)fgetc(in_file)) << k; 404 b |= ((unsigned long) fgetc(in_file)) << k;
402 k += 8; 405 k += 8;
403 } 406 }
404 if ((e = (t = tl + ((unsigned) b & ml))->e) > 16) 407 if ((e = (t = tl + ((unsigned) b & ml))->e) > 16)
405 do { 408 do {
406 if (e == 99) { 409 if (e == 99) {
407 return 1; 410 return 1;
408 } 411 }
409 b >>= t->b; 412 b >>= t->b;
410 k -= t->b; 413 k -= t->b;
411 e -= 16; 414 e -= 16;
412 while (k < e) { 415 while (k < e) {
413 b |= ((unsigned long)fgetc(in_file)) << k; 416 b |= ((unsigned long) fgetc(in_file)) << k;
414 k += 8; 417 k += 8;
415 } 418 }
416 } while ((e = (t = t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16); 419 } while ((e =
420 (t = t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16);
417 b >>= t->b; 421 b >>= t->b;
418 k -= t->b; 422 k -= t->b;
419 if (e == 16) { /* then it's a literal */ 423 if (e == 16) { /* then it's a literal */
420 window[w++] = (unsigned char) t->v.n; 424 window[w++] = (unsigned char) t->v.n;
421 if (w == WSIZE) { 425 if (w == WSIZE) {
422 outcnt=(w), 426 outcnt = (w), flush_window();
423 flush_window();
424 w = 0; 427 w = 0;
425 } 428 }
426 } else { /* it's an EOB or a length */ 429 } else { /* it's an EOB or a length */
427 430
428 /* exit if end of block */ 431 /* exit if end of block */
429 if (e == 15) { 432 if (e == 15) {
@@ -432,7 +435,7 @@ static int inflate_codes(huft_t *tl, huft_t *td, int bl, int bd)
432 435
433 /* get length of block to copy */ 436 /* get length of block to copy */
434 while (k < e) { 437 while (k < e) {
435 b |= ((unsigned long)fgetc(in_file)) << k; 438 b |= ((unsigned long) fgetc(in_file)) << k;
436 k += 8; 439 k += 8;
437 } 440 }
438 n = t->v.n + ((unsigned) b & mask_bits[e]); 441 n = t->v.n + ((unsigned) b & mask_bits[e]);
@@ -441,7 +444,7 @@ static int inflate_codes(huft_t *tl, huft_t *td, int bl, int bd)
441 444
442 /* decode distance of block to copy */ 445 /* decode distance of block to copy */
443 while (k < (unsigned) bd) { 446 while (k < (unsigned) bd) {
444 b |= ((unsigned long)fgetc(in_file)) << k; 447 b |= ((unsigned long) fgetc(in_file)) << k;
445 k += 8; 448 k += 8;
446 } 449 }
447 450
@@ -453,14 +456,16 @@ static int inflate_codes(huft_t *tl, huft_t *td, int bl, int bd)
453 k -= t->b; 456 k -= t->b;
454 e -= 16; 457 e -= 16;
455 while (k < e) { 458 while (k < e) {
456 b |= ((unsigned long)fgetc(in_file)) << k; 459 b |= ((unsigned long) fgetc(in_file)) << k;
457 k += 8; 460 k += 8;
458 } 461 }
459 } while ((e = (t = t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16); 462 } while ((e =
463 (t =
464 t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16);
460 b >>= t->b; 465 b >>= t->b;
461 k -= t->b; 466 k -= t->b;
462 while (k < e) { 467 while (k < e) {
463 b |= ((unsigned long)fgetc(in_file)) << k; 468 b |= ((unsigned long) fgetc(in_file)) << k;
464 k += 8; 469 k += 8;
465 } 470 }
466 d = w - t->v.n - ((unsigned) b & mask_bits[e]); 471 d = w - t->v.n - ((unsigned) b & mask_bits[e]);
@@ -469,20 +474,21 @@ static int inflate_codes(huft_t *tl, huft_t *td, int bl, int bd)
469 474
470 /* do the copy */ 475 /* do the copy */
471 do { 476 do {
472 n -= (e = (e = WSIZE - ((d &= WSIZE - 1) > w ? d : w)) > n ? n : e); 477 n -= (e =
478 (e =
479 WSIZE - ((d &= WSIZE - 1) > w ? d : w)) > n ? n : e);
473#if !defined(NOMEMCPY) && !defined(DEBUG) 480#if !defined(NOMEMCPY) && !defined(DEBUG)
474 if (w - d >= e) { /* (this test assumes unsigned comparison) */ 481 if (w - d >= e) { /* (this test assumes unsigned comparison) */
475 memcpy(window + w, window + d, e); 482 memcpy(window + w, window + d, e);
476 w += e; 483 w += e;
477 d += e; 484 d += e;
478 } else /* do it slow to avoid memcpy() overlap */ 485 } else /* do it slow to avoid memcpy() overlap */
479#endif /* !NOMEMCPY */ 486#endif /* !NOMEMCPY */
480 do { 487 do {
481 window[w++] = window[d++]; 488 window[w++] = window[d++];
482 } while (--e); 489 } while (--e);
483 if (w == WSIZE) { 490 if (w == WSIZE) {
484 outcnt=(w), 491 outcnt = (w), flush_window();
485 flush_window();
486 w = 0; 492 w = 0;
487 } 493 }
488 } while (n); 494 } while (n);
@@ -498,28 +504,30 @@ static int inflate_codes(huft_t *tl, huft_t *td, int bl, int bd)
498 return 0; 504 return 0;
499} 505}
500 506
501static const unsigned short cplens[] = { /* Copy lengths for literal codes 257..285 */ 507static const unsigned short cplens[] = { /* Copy lengths for literal codes 257..285 */
502 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 508 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
503 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0 509 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
504}; 510};
511
505/* note: see note #13 above about the 258 in this list. */ 512/* note: see note #13 above about the 258 in this list. */
506static const extra_bits_t cplext[] = { /* Extra bits for literal codes 257..285 */ 513static const extra_bits_t cplext[] = { /* Extra bits for literal codes 257..285 */
507 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 514 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
508 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99 515 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99
509}; /* 99==invalid */ 516}; /* 99==invalid */
510static const unsigned short cpdist[] = { /* Copy offsets for distance codes 0..29 */ 517static const unsigned short cpdist[] = { /* Copy offsets for distance codes 0..29 */
511 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 518 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
512 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 519 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
513 8193, 12289, 16385, 24577 520 8193, 12289, 16385, 24577
514}; 521};
515static const extra_bits_t cpdext[] = { /* Extra bits for distance codes */ 522static const extra_bits_t cpdext[] = { /* Extra bits for distance codes */
516 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 523 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
517 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 524 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
518 12, 12, 13, 13 525 12, 12, 13, 13
519}; 526};
527
520/* Tables for deflate from PKZIP's appnote.txt. */ 528/* Tables for deflate from PKZIP's appnote.txt. */
521static const extra_bits_t border[] = { /* Order of the bit length code lengths */ 529static const extra_bits_t border[] = { /* Order of the bit length code lengths */
522 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 530 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
523}; 531};
524 532
525/* 533/*
@@ -531,8 +539,8 @@ static const extra_bits_t border[] = { /* Order of the bit length code lengths
531static int inflate_block(int *e) 539static int inflate_block(int *e)
532{ 540{
533 unsigned t; /* block type */ 541 unsigned t; /* block type */
534 register unsigned long b; /* bit buffer */ 542 register unsigned long b; /* bit buffer */
535 register unsigned k; /* number of bits in bit buffer */ 543 register unsigned k; /* number of bits in bit buffer */
536 544
537 /* make local bit buffer */ 545 /* make local bit buffer */
538 b = bb; 546 b = bb;
@@ -540,7 +548,7 @@ static int inflate_block(int *e)
540 548
541 /* read in last block bit */ 549 /* read in last block bit */
542 while (k < 1) { 550 while (k < 1) {
543 b |= ((unsigned long)fgetc(in_file)) << k; 551 b |= ((unsigned long) fgetc(in_file)) << k;
544 k += 8; 552 k += 8;
545 } 553 }
546 *e = (int) b & 1; 554 *e = (int) b & 1;
@@ -549,7 +557,7 @@ static int inflate_block(int *e)
549 557
550 /* read in block type */ 558 /* read in block type */
551 while (k < 2) { 559 while (k < 2) {
552 b |= ((unsigned long)fgetc(in_file)) << k; 560 b |= ((unsigned long) fgetc(in_file)) << k;
553 k += 8; 561 k += 8;
554 } 562 }
555 t = (unsigned) b & 3; 563 t = (unsigned) b & 3;
@@ -562,286 +570,287 @@ static int inflate_block(int *e)
562 570
563 /* inflate that block type */ 571 /* inflate that block type */
564 switch (t) { 572 switch (t) {
565 case 0: /* Inflate stored */ 573 case 0: /* Inflate stored */
566 { 574 {
567 unsigned long n; /* number of bytes in block */ 575 unsigned long n; /* number of bytes in block */
568 unsigned long w; /* current window position */ 576 unsigned long w; /* current window position */
569 register unsigned long b_stored; /* bit buffer */ 577 register unsigned long b_stored; /* bit buffer */
570 register unsigned long k_stored; /* number of bits in bit buffer */ 578 register unsigned long k_stored; /* number of bits in bit buffer */
571 579
572 /* make local copies of globals */ 580 /* make local copies of globals */
573 b_stored = bb; /* initialize bit buffer */ 581 b_stored = bb; /* initialize bit buffer */
574 k_stored = bk; 582 k_stored = bk;
575 w = outcnt; /* initialize window position */ 583 w = outcnt; /* initialize window position */
576 584
577 /* go to byte boundary */ 585 /* go to byte boundary */
578 n = k_stored & 7; 586 n = k_stored & 7;
579 b_stored >>= n; 587 b_stored >>= n;
580 k_stored -= n; 588 k_stored -= n;
581 589
582 /* get the length and its complement */ 590 /* get the length and its complement */
583 while (k_stored < 16) { 591 while (k_stored < 16) {
584 b_stored |= ((unsigned long)fgetc(in_file)) << k_stored; 592 b_stored |= ((unsigned long) fgetc(in_file)) << k_stored;
585 k_stored += 8; 593 k_stored += 8;
586 } 594 }
587 n = ((unsigned) b_stored & 0xffff); 595 n = ((unsigned) b_stored & 0xffff);
588 b_stored >>= 16; 596 b_stored >>= 16;
589 k_stored -= 16; 597 k_stored -= 16;
590 while (k_stored < 16) { 598 while (k_stored < 16) {
591 b_stored |= ((unsigned long)fgetc(in_file)) << k_stored; 599 b_stored |= ((unsigned long) fgetc(in_file)) << k_stored;
600 k_stored += 8;
601 }
602 if (n != (unsigned) ((~b_stored) & 0xffff)) {
603 return 1; /* error in compressed data */
604 }
605 b_stored >>= 16;
606 k_stored -= 16;
607
608 /* read and output the compressed data */
609 while (n--) {
610 while (k_stored < 8) {
611 b_stored |= ((unsigned long) fgetc(in_file)) << k_stored;
592 k_stored += 8; 612 k_stored += 8;
593 } 613 }
594 if (n != (unsigned) ((~b_stored) & 0xffff)) { 614 window[w++] = (unsigned char) b_stored;
595 return 1; /* error in compressed data */ 615 if (w == (unsigned long) WSIZE) {
596 } 616 outcnt = (w), flush_window();
597 b_stored >>= 16; 617 w = 0;
598 k_stored -= 16;
599
600 /* read and output the compressed data */
601 while (n--) {
602 while (k_stored < 8) {
603 b_stored |= ((unsigned long)fgetc(in_file)) << k_stored;
604 k_stored += 8;
605 }
606 window[w++] = (unsigned char) b_stored;
607 if (w == (unsigned long)WSIZE) {
608 outcnt=(w),
609 flush_window();
610 w = 0;
611 }
612 b_stored >>= 8;
613 k_stored -= 8;
614 } 618 }
619 b_stored >>= 8;
620 k_stored -= 8;
621 }
615 622
616 /* restore the globals from the locals */ 623 /* restore the globals from the locals */
617 outcnt = w; /* restore global window pointer */ 624 outcnt = w; /* restore global window pointer */
618 bb = b_stored; /* restore global bit buffer */ 625 bb = b_stored; /* restore global bit buffer */
619 bk = k_stored; 626 bk = k_stored;
620 return 0; 627 return 0;
628 }
629 case 1: /* Inflate fixed
630 * decompress an inflated type 1 (fixed Huffman codes) block. We should
631 * either replace this with a custom decoder, or at least precompute the
632 * Huffman tables.
633 */
634 {
635 int i; /* temporary variable */
636 huft_t *tl; /* literal/length code table */
637 huft_t *td; /* distance code table */
638 int bl; /* lookup bits for tl */
639 int bd; /* lookup bits for td */
640 unsigned int l[288]; /* length list for huft_build */
641
642 /* set up literal table */
643 for (i = 0; i < 144; i++) {
644 l[i] = 8;
645 }
646 for (; i < 256; i++) {
647 l[i] = 9;
648 }
649 for (; i < 280; i++) {
650 l[i] = 7;
651 }
652 for (; i < 288; i++) { /* make a complete, but wrong code set */
653 l[i] = 8;
654 }
655 bl = 7;
656 if ((i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl)) != 0) {
657 return i;
621 } 658 }
622 case 1: /* Inflate fixed
623 * decompress an inflated type 1 (fixed Huffman codes) block. We should
624 * either replace this with a custom decoder, or at least precompute the
625 * Huffman tables.
626 */
627 {
628 int i; /* temporary variable */
629 huft_t *tl; /* literal/length code table */
630 huft_t *td; /* distance code table */
631 int bl; /* lookup bits for tl */
632 int bd; /* lookup bits for td */
633 unsigned int l[288]; /* length list for huft_build */
634
635 /* set up literal table */
636 for (i = 0; i < 144; i++) {
637 l[i] = 8;
638 }
639 for (; i < 256; i++) {
640 l[i] = 9;
641 }
642 for (; i < 280; i++) {
643 l[i] = 7;
644 }
645 for (; i < 288; i++) { /* make a complete, but wrong code set */
646 l[i] = 8;
647 }
648 bl = 7;
649 if ((i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl)) != 0) {
650 return i;
651 }
652 659
653 /* set up distance table */ 660 /* set up distance table */
654 for (i = 0; i < 30; i++) { /* make an incomplete code set */ 661 for (i = 0; i < 30; i++) { /* make an incomplete code set */
655 l[i] = 5; 662 l[i] = 5;
656 } 663 }
657 bd = 5; 664 bd = 5;
658 if ((i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd)) > 1) { 665 if ((i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd)) > 1) {
659 huft_free(tl); 666 huft_free(tl);
660 return i; 667 return i;
661 } 668 }
662 669
663 /* decompress until an end-of-block code */ 670 /* decompress until an end-of-block code */
664 if (inflate_codes(tl, td, bl, bd)) 671 if (inflate_codes(tl, td, bl, bd))
665 return 1; 672 return 1;
666 673
667 /* free the decoding tables, return */ 674 /* free the decoding tables, return */
668 huft_free(tl); 675 huft_free(tl);
669 huft_free(td); 676 huft_free(td);
670 return 0; 677 return 0;
678 }
679 case 2: /* Inflate dynamic */
680 {
681 const int dbits = 6; /* bits in base distance lookup table */
682 const int lbits = 9; /* bits in base literal/length lookup table */
683
684 int i; /* temporary variables */
685 unsigned j;
686 unsigned l; /* last length */
687 unsigned m; /* mask for bit lengths table */
688 unsigned n; /* number of lengths to get */
689 huft_t *tl; /* literal/length code table */
690 huft_t *td; /* distance code table */
691 int bl; /* lookup bits for tl */
692 int bd; /* lookup bits for td */
693 unsigned nb; /* number of bit length codes */
694 unsigned nl; /* number of literal/length codes */
695 unsigned nd; /* number of distance codes */
696
697 unsigned ll[286 + 30]; /* literal/length and distance code lengths */
698 register unsigned long b_dynamic; /* bit buffer */
699 register unsigned k_dynamic; /* number of bits in bit buffer */
700
701 /* make local bit buffer */
702 b_dynamic = bb;
703 k_dynamic = bk;
704
705 /* read in table lengths */
706 while (k_dynamic < 5) {
707 b_dynamic |= ((unsigned long) fgetc(in_file)) << k_dynamic;
708 k_dynamic += 8;
709 }
710 nl = 257 + ((unsigned) b_dynamic & 0x1f); /* number of literal/length codes */
711 b_dynamic >>= 5;
712 k_dynamic -= 5;
713 while (k_dynamic < 5) {
714 b_dynamic |= ((unsigned long) fgetc(in_file)) << k_dynamic;
715 k_dynamic += 8;
716 }
717 nd = 1 + ((unsigned) b_dynamic & 0x1f); /* number of distance codes */
718 b_dynamic >>= 5;
719 k_dynamic -= 5;
720 while (k_dynamic < 4) {
721 b_dynamic |= ((unsigned long) fgetc(in_file)) << k_dynamic;
722 k_dynamic += 8;
723 }
724 nb = 4 + ((unsigned) b_dynamic & 0xf); /* number of bit length codes */
725 b_dynamic >>= 4;
726 k_dynamic -= 4;
727 if (nl > 286 || nd > 30) {
728 return 1; /* bad lengths */
671 } 729 }
672 case 2: /* Inflate dynamic */ 730
673 { 731 /* read in bit-length-code lengths */
674 const int dbits = 6; /* bits in base distance lookup table */ 732 for (j = 0; j < nb; j++) {
675 const int lbits = 9; /* bits in base literal/length lookup table */ 733 while (k_dynamic < 3) {
676 734 b_dynamic |= ((unsigned long) fgetc(in_file)) << k_dynamic;
677 int i; /* temporary variables */
678 unsigned j;
679 unsigned l; /* last length */
680 unsigned m; /* mask for bit lengths table */
681 unsigned n; /* number of lengths to get */
682 huft_t *tl; /* literal/length code table */
683 huft_t *td; /* distance code table */
684 int bl; /* lookup bits for tl */
685 int bd; /* lookup bits for td */
686 unsigned nb; /* number of bit length codes */
687 unsigned nl; /* number of literal/length codes */
688 unsigned nd; /* number of distance codes */
689
690 unsigned ll[286 + 30]; /* literal/length and distance code lengths */
691 register unsigned long b_dynamic; /* bit buffer */
692 register unsigned k_dynamic; /* number of bits in bit buffer */
693
694 /* make local bit buffer */
695 b_dynamic = bb;
696 k_dynamic = bk;
697
698 /* read in table lengths */
699 while (k_dynamic < 5) {
700 b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic;
701 k_dynamic += 8; 735 k_dynamic += 8;
702 } 736 }
703 nl = 257 + ((unsigned) b_dynamic & 0x1f); /* number of literal/length codes */ 737 ll[border[j]] = (unsigned) b_dynamic & 7;
704 b_dynamic >>= 5; 738 b_dynamic >>= 3;
705 k_dynamic -= 5; 739 k_dynamic -= 3;
706 while (k_dynamic < 5) { 740 }
707 b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic; 741 for (; j < 19; j++) {
708 k_dynamic += 8; 742 ll[border[j]] = 0;
743 }
744
745 /* build decoding table for trees--single level, 7 bit lookup */
746 bl = 7;
747 if ((i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl)) != 0) {
748 if (i == 1) {
749 huft_free(tl);
709 } 750 }
710 nd = 1 + ((unsigned) b_dynamic & 0x1f); /* number of distance codes */ 751 return i; /* incomplete code set */
711 b_dynamic >>= 5; 752 }
712 k_dynamic -= 5; 753
713 while (k_dynamic < 4) { 754 /* read in literal and distance code lengths */
714 b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic; 755 n = nl + nd;
756 m = mask_bits[bl];
757 i = l = 0;
758 while ((unsigned) i < n) {
759 while (k_dynamic < (unsigned) bl) {
760 b_dynamic |= ((unsigned long) fgetc(in_file)) << k_dynamic;
715 k_dynamic += 8; 761 k_dynamic += 8;
716 } 762 }
717 nb = 4 + ((unsigned) b_dynamic & 0xf); /* number of bit length codes */ 763 j = (td = tl + ((unsigned) b_dynamic & m))->b;
718 b_dynamic >>= 4; 764 b_dynamic >>= j;
719 k_dynamic -= 4; 765 k_dynamic -= j;
720 if (nl > 286 || nd > 30) { 766 j = td->v.n;
721 return 1; /* bad lengths */ 767 if (j < 16) { /* length of code in bits (0..15) */
722 } 768 ll[i++] = l = j; /* save last length in l */
723 769 } else if (j == 16) { /* repeat last length 3 to 6 times */
724 /* read in bit-length-code lengths */ 770 while (k_dynamic < 2) {
725 for (j = 0; j < nb; j++) { 771 b_dynamic |=
772 ((unsigned long) fgetc(in_file)) << k_dynamic;
773 k_dynamic += 8;
774 }
775 j = 3 + ((unsigned) b_dynamic & 3);
776 b_dynamic >>= 2;
777 k_dynamic -= 2;
778 if ((unsigned) i + j > n) {
779 return 1;
780 }
781 while (j--) {
782 ll[i++] = l;
783 }
784 } else if (j == 17) { /* 3 to 10 zero length codes */
726 while (k_dynamic < 3) { 785 while (k_dynamic < 3) {
727 b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic; 786 b_dynamic |=
787 ((unsigned long) fgetc(in_file)) << k_dynamic;
728 k_dynamic += 8; 788 k_dynamic += 8;
729 } 789 }
730 ll[border[j]] = (unsigned) b_dynamic & 7; 790 j = 3 + ((unsigned) b_dynamic & 7);
731 b_dynamic >>= 3; 791 b_dynamic >>= 3;
732 k_dynamic -= 3; 792 k_dynamic -= 3;
733 } 793 if ((unsigned) i + j > n) {
734 for (; j < 19; j++) { 794 return 1;
735 ll[border[j]] = 0;
736 }
737
738 /* build decoding table for trees--single level, 7 bit lookup */
739 bl = 7;
740 if ((i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl)) != 0) {
741 if (i == 1) {
742 huft_free(tl);
743 } 795 }
744 return i; /* incomplete code set */ 796 while (j--) {
745 } 797 ll[i++] = 0;
746 798 }
747 /* read in literal and distance code lengths */ 799 l = 0;
748 n = nl + nd; 800 } else { /* j == 18: 11 to 138 zero length codes */
749 m = mask_bits[bl]; 801 while (k_dynamic < 7) {
750 i = l = 0; 802 b_dynamic |=
751 while ((unsigned) i < n) { 803 ((unsigned long) fgetc(in_file)) << k_dynamic;
752 while (k_dynamic < (unsigned) bl) {
753 b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic;
754 k_dynamic += 8; 804 k_dynamic += 8;
755 } 805 }
756 j = (td = tl + ((unsigned) b_dynamic & m))->b; 806 j = 11 + ((unsigned) b_dynamic & 0x7f);
757 b_dynamic >>= j; 807 b_dynamic >>= 7;
758 k_dynamic -= j; 808 k_dynamic -= 7;
759 j = td->v.n; 809 if ((unsigned) i + j > n) {
760 if (j < 16) { /* length of code in bits (0..15) */ 810 return 1;
761 ll[i++] = l = j; /* save last length in l */
762 } 811 }
763 else if (j == 16) { /* repeat last length 3 to 6 times */ 812 while (j--) {
764 while (k_dynamic < 2) { 813 ll[i++] = 0;
765 b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic;
766 k_dynamic += 8;
767 }
768 j = 3 + ((unsigned) b_dynamic & 3);
769 b_dynamic >>= 2;
770 k_dynamic -= 2;
771 if ((unsigned) i + j > n) {
772 return 1;
773 }
774 while (j--) {
775 ll[i++] = l;
776 }
777 } else if (j == 17) { /* 3 to 10 zero length codes */
778 while (k_dynamic < 3) {
779 b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic;
780 k_dynamic += 8;
781 }
782 j = 3 + ((unsigned) b_dynamic & 7);
783 b_dynamic >>= 3;
784 k_dynamic -= 3;
785 if ((unsigned) i + j > n) {
786 return 1;
787 }
788 while (j--) {
789 ll[i++] = 0;
790 }
791 l = 0;
792 } else { /* j == 18: 11 to 138 zero length codes */
793 while (k_dynamic < 7) {
794 b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic;
795 k_dynamic += 8;
796 }
797 j = 11 + ((unsigned) b_dynamic & 0x7f);
798 b_dynamic >>= 7;
799 k_dynamic -= 7;
800 if ((unsigned) i + j > n) {
801 return 1;
802 }
803 while (j--) {
804 ll[i++] = 0;
805 }
806 l = 0;
807 } 814 }
815 l = 0;
808 } 816 }
817 }
809 818
810 /* free decoding table for trees */ 819 /* free decoding table for trees */
811 huft_free(tl); 820 huft_free(tl);
812 821
813 /* restore the global bit buffer */ 822 /* restore the global bit buffer */
814 bb = b_dynamic; 823 bb = b_dynamic;
815 bk = k_dynamic; 824 bk = k_dynamic;
816 825
817 /* build the decoding tables for literal/length and distance codes */ 826 /* build the decoding tables for literal/length and distance codes */
818 bl = lbits; 827 bl = lbits;
819 if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0) { 828 if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0) {
820 if (i == 1) { 829 if (i == 1) {
821 error_msg("Incomplete literal tree"); 830 error_msg("Incomplete literal tree");
822 huft_free(tl);
823 }
824 return i; /* incomplete code set */
825 }
826 bd = dbits;
827 if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0) {
828 if (i == 1) {
829 error_msg("incomplete distance tree");
830 huft_free(td);
831 }
832 huft_free(tl); 831 huft_free(tl);
833 return i; /* incomplete code set */
834 } 832 }
835 833 return i; /* incomplete code set */
836 /* decompress until an end-of-block code */ 834 }
837 if (inflate_codes(tl, td, bl, bd)) 835 bd = dbits;
838 return 1; 836 if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0) {
839 837 if (i == 1) {
840 /* free the decoding tables, return */ 838 error_msg("incomplete distance tree");
839 huft_free(td);
840 }
841 huft_free(tl); 841 huft_free(tl);
842 huft_free(td); 842 return i; /* incomplete code set */
843 return 0;
844 } 843 }
844
845 /* decompress until an end-of-block code */
846 if (inflate_codes(tl, td, bl, bd))
847 return 1;
848
849 /* free the decoding tables, return */
850 huft_free(tl);
851 huft_free(td);
852 return 0;
853 }
845 default: 854 default:
846 /* bad block type */ 855 /* bad block type */
847 return 2; 856 return 2;
@@ -853,7 +862,7 @@ static int inflate_block(int *e)
853 * 862 *
854 * GLOBAL VARIABLES: outcnt, bk, bb, hufts, inptr 863 * GLOBAL VARIABLES: outcnt, bk, bb, hufts, inptr
855 */ 864 */
856extern int inflate(FILE *in, FILE *out) 865extern int inflate(FILE * in, FILE * out)
857{ 866{
858 int e; /* last block flag */ 867 int e; /* last block flag */
859 int r; /* result code */ 868 int r; /* result code */
@@ -868,7 +877,7 @@ extern int inflate(FILE *in, FILE *out)
868 out_file = out; 877 out_file = out;
869 878
870 /* Allocate all global buffers (for DYN_ALLOC option) */ 879 /* Allocate all global buffers (for DYN_ALLOC option) */
871 window = xmalloc((size_t)(((2L*WSIZE)+1L)*sizeof(unsigned char))); 880 window = xmalloc((size_t) (((2L * WSIZE) + 1L) * sizeof(unsigned char)));
872 bytes_out = 0L; 881 bytes_out = 0L;
873 882
874 /* Create the crc table */ 883 /* Create the crc table */
@@ -910,40 +919,35 @@ extern int inflate(FILE *in, FILE *out)
910 * The magic header has already been checked. The output buffer is cleared. 919 * The magic header has already been checked. The output buffer is cleared.
911 * in, out: input and output file descriptors 920 * in, out: input and output file descriptors
912 */ 921 */
913extern int unzip(FILE *l_in_file, FILE *l_out_file) 922extern int unzip(FILE * l_in_file, FILE * l_out_file)
914{ 923{
915 unsigned char buf[8]; /* extended local header */ 924 unsigned char buf[8]; /* extended local header */
916 unsigned char flags; /* compression flags */ 925 unsigned char flags; /* compression flags */
917 typedef void (*sig_type) (int); 926 typedef void (*sig_type) (int);
918 unsigned short i; 927 unsigned short i;
919 unsigned char magic [2]; 928 unsigned char magic[2];
920 929
921 if (signal(SIGINT, SIG_IGN) != SIG_IGN) { 930 if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
922 (void) signal(SIGINT, (sig_type) abort_gzip); 931 (void) signal(SIGINT, (sig_type) abort_gzip);
923 } 932 }
924#ifdef SIGTERM
925// if (signal(SIGTERM, SIG_IGN) != SIG_IGN) {
926// (void) signal(SIGTERM, (sig_type) abort_gzip);
927// }
928#endif
929#ifdef SIGHUP 933#ifdef SIGHUP
930 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) { 934 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
931 (void) signal(SIGHUP, (sig_type) abort_gzip); 935 (void) signal(SIGHUP, (sig_type) abort_gzip);
932 } 936 }
933#endif 937#endif
934 938
935 magic [0] = fgetc(l_in_file); 939 magic[0] = fgetc(l_in_file);
936 magic [1] = fgetc(l_in_file); 940 magic[1] = fgetc(l_in_file);
937 941
938#ifdef CONFIG_FEATURE_UNCOMPRESS 942#ifdef CONFIG_FEATURE_UNCOMPRESS
939 /* Magic header for compress files, 1F 9d = \037\235 */ 943 /* Magic header for compress files, 1F 9d = \037\235 */
940 if (( magic [0] == 0x1F ) && ( magic [1] == 0x9d)) { 944 if ((magic[0] == 0x1F) && (magic[1] == 0x9d)) {
941 return uncompress ( l_in_file, l_out_file ); 945 return uncompress(l_in_file, l_out_file);
942 } 946 }
943#endif 947#endif
944 948
945 /* Magic header for gzip files, 1F 8B = \037\213 */ 949 /* Magic header for gzip files, 1F 8B = \037\213 */
946 if (( magic [0] != 0x1F ) || ( magic [1] != 0x8b)) { 950 if ((magic[0] != 0x1F) || (magic[1] != 0x8b)) {
947 error_msg("Invalid gzip magic"); 951 error_msg("Invalid gzip magic");
948 return EXIT_FAILURE; 952 return EXIT_FAILURE;
949 } 953 }
@@ -951,7 +955,7 @@ extern int unzip(FILE *l_in_file, FILE *l_out_file)
951 /* Check the compression method */ 955 /* Check the compression method */
952 if (fgetc(l_in_file) != 8) { 956 if (fgetc(l_in_file) != 8) {
953 error_msg("Unknown compression method"); 957 error_msg("Unknown compression method");
954 return(-1); 958 return (-1);
955 } 959 }
956 960
957 flags = (unsigned char) fgetc(l_in_file); 961 flags = (unsigned char) fgetc(l_in_file);
@@ -963,7 +967,8 @@ extern int unzip(FILE *l_in_file, FILE *l_out_file)
963 967
964 if (flags & 0x04) { 968 if (flags & 0x04) {
965 /* bit 2 set: extra field present */ 969 /* bit 2 set: extra field present */
966 const unsigned short extra = fgetc(l_in_file) + (fgetc(l_in_file) << 8); 970 const unsigned short extra =
971 fgetc(l_in_file) + (fgetc(l_in_file) << 8);
967 972
968 for (i = 0; i < extra; i++) { 973 for (i = 0; i < extra; i++) {
969 fgetc(l_in_file); 974 fgetc(l_in_file);
@@ -994,11 +999,14 @@ extern int unzip(FILE *l_in_file, FILE *l_out_file)
994 fread(buf, 1, 8, l_in_file); 999 fread(buf, 1, 8, l_in_file);
995 1000
996 /* Validate decompression - crc */ 1001 /* Validate decompression - crc */
997 if ((unsigned int)((buf[0] | (buf[1] << 8)) |((buf[2] | (buf[3] << 8)) << 16)) != (crc ^ 0xffffffffL)) { 1002 if ((unsigned int) ((buf[0] | (buf[1] << 8)) |
1003 ((buf[2] | (buf[3] << 8)) << 16)) !=
1004 (crc ^ 0xffffffffL)) {
998 error_msg("invalid compressed data--crc error"); 1005 error_msg("invalid compressed data--crc error");
999 } 1006 }
1000 /* Validate decompression - size */ 1007 /* Validate decompression - size */
1001 if (((buf[4] | (buf[5] << 8)) |((buf[6] | (buf[7] << 8)) << 16)) != (unsigned long) bytes_out) { 1008 if (((buf[4] | (buf[5] << 8)) | ((buf[6] | (buf[7] << 8)) << 16)) !=
1009 (unsigned long) bytes_out) {
1002 error_msg("invalid compressed data--length error"); 1010 error_msg("invalid compressed data--length error");
1003 } 1011 }
1004 1012
@@ -1011,7 +1019,8 @@ extern int unzip(FILE *l_in_file, FILE *l_out_file)
1011extern void gz_close(int gunzip_pid) 1019extern void gz_close(int gunzip_pid)
1012{ 1020{
1013 if (kill(gunzip_pid, SIGTERM) == -1) { 1021 if (kill(gunzip_pid, SIGTERM) == -1) {
1014 error_msg_and_die("*** Couldnt kill old gunzip process *** aborting"); 1022 error_msg_and_die
1023 ("*** Couldnt kill old gunzip process *** aborting");
1015 } 1024 }
1016 1025
1017 if (waitpid(gunzip_pid, NULL, 0) == -1) { 1026 if (waitpid(gunzip_pid, NULL, 0) == -1) {
diff --git a/archival/libunarchive/unzip.c b/archival/libunarchive/unzip.c
index cde16d067..76fb86285 100644
--- a/archival/libunarchive/unzip.c
+++ b/archival/libunarchive/unzip.c
@@ -69,7 +69,7 @@ static char *license_msg[] = {
69#include "libbb.h" 69#include "libbb.h"
70 70
71#ifdef CONFIG_FEATURE_UNCOMPRESS 71#ifdef CONFIG_FEATURE_UNCOMPRESS
72int uncompress ( FILE *in, FILE *out ); 72int uncompress(FILE * in, FILE * out);
73#endif 73#endif
74 74
75static FILE *in_file, *out_file; 75static FILE *in_file, *out_file;
@@ -78,7 +78,7 @@ static FILE *in_file, *out_file;
78static unsigned char *window; 78static unsigned char *window;
79static unsigned long *crc_table; 79static unsigned long *crc_table;
80 80
81static unsigned long crc; /* shift register contents */ 81static unsigned long crc; /* shift register contents */
82 82
83/* Return codes from gzip */ 83/* Return codes from gzip */
84static const int ERROR = 1; 84static const int ERROR = 1;
@@ -90,21 +90,21 @@ static const int ERROR = 1;
90static const int WSIZE = 0x8000; 90static const int WSIZE = 0x8000;
91 91
92/* If BMAX needs to be larger than 16, then h and x[] should be ulg. */ 92/* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
93static const int BMAX = 16; /* maximum bit length of any code (16 for explode) */ 93static const int BMAX = 16; /* maximum bit length of any code (16 for explode) */
94static const int N_MAX = 288; /* maximum number of codes in any set */ 94static const int N_MAX = 288; /* maximum number of codes in any set */
95 95
96static long bytes_out; /* number of output bytes */ 96static long bytes_out; /* number of output bytes */
97static unsigned long outcnt; /* bytes in output buffer */ 97static unsigned long outcnt; /* bytes in output buffer */
98 98
99static unsigned hufts; /* track memory usage */ 99static unsigned hufts; /* track memory usage */
100static unsigned long bb; /* bit buffer */ 100static unsigned long bb; /* bit buffer */
101static unsigned bk; /* bits in bit buffer */ 101static unsigned bk; /* bits in bit buffer */
102 102
103typedef struct huft_s { 103typedef struct huft_s {
104 unsigned char e; /* number of extra bits or operation */ 104 unsigned char e; /* number of extra bits or operation */
105 unsigned char b; /* number of bits in this code or subcode */ 105 unsigned char b; /* number of bits in this code or subcode */
106 union { 106 union {
107 unsigned short n; /* literal, length base, or distance base */ 107 unsigned short n; /* literal, length base, or distance base */
108 struct huft_s *t; /* pointer to next level of table */ 108 struct huft_s *t; /* pointer to next level of table */
109 } v; 109 } v;
110} huft_t; 110} huft_t;
@@ -115,11 +115,11 @@ static const unsigned short mask_bits[] = {
115 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff 115 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
116}; 116};
117 117
118//static int error_number = 0; 118/* static int error_number = 0; */
119/* ======================================================================== 119/* ========================================================================
120 * Signal and error handler. 120 * Signal and error handler.
121 */ 121 */
122 122
123static void abort_gzip(void) 123static void abort_gzip(void)
124{ 124{
125 error_msg("gzip aborted\n"); 125 error_msg("gzip aborted\n");
@@ -128,26 +128,28 @@ static void abort_gzip(void)
128 128
129static void make_crc_table(void) 129static void make_crc_table(void)
130{ 130{
131 const unsigned long poly = 0xedb88320; /* polynomial exclusive-or pattern */ 131 const unsigned long poly = 0xedb88320; /* polynomial exclusive-or pattern */
132 unsigned short i; /* counter for all possible eight bit values */ 132 unsigned short i; /* counter for all possible eight bit values */
133 133
134 /* initial shift register value */ 134 /* initial shift register value */
135 crc = 0xffffffffL; 135 crc = 0xffffffffL;
136 crc_table = (unsigned long *) malloc(256 * sizeof(unsigned long)); 136 crc_table = (unsigned long *) malloc(256 * sizeof(unsigned long));
137 137
138 /* Compute and print table of CRC's, five per line */ 138 /* Compute and print table of CRC's, five per line */
139 for (i = 0; i < 256; i++) { 139 for (i = 0; i < 256; i++) {
140 unsigned long table_entry; /* crc shift register */ 140 unsigned long table_entry; /* crc shift register */
141 char k; /* byte being shifted into crc apparatus */ 141 char k; /* byte being shifted into crc apparatus */
142 142
143 table_entry = i; 143 table_entry = i;
144 /* The idea to initialize the register with the byte instead of 144 /* The idea to initialize the register with the byte instead of
145 * zero was stolen from Haruhiko Okumura's ar002 145 * zero was stolen from Haruhiko Okumura's ar002
146 */ 146 */
147 for (k = 8; k; k--) { 147 for (k = 8; k; k--) {
148 table_entry = table_entry & 1 ? (table_entry >> 1) ^ poly : table_entry >> 1; 148 table_entry =
149 table_entry & 1 ? (table_entry >> 1) ^ poly : table_entry >>
150 1;
149 } 151 }
150 crc_table[i]=table_entry; 152 crc_table[i] = table_entry;
151 } 153 }
152} 154}
153 155
@@ -179,7 +181,7 @@ static void flush_window(void)
179 * each table. 181 * each table.
180 * t: table to free 182 * t: table to free
181 */ 183 */
182static int huft_free(huft_t *t) 184static int huft_free(huft_t * t)
183{ 185{
184 huft_t *p, *q; 186 huft_t *p, *q;
185 187
@@ -209,36 +211,37 @@ typedef unsigned char extra_bits_t;
209 * t: result: starting table 211 * t: result: starting table
210 * m: maximum lookup bits, returns actual 212 * m: maximum lookup bits, returns actual
211 */ 213 */
212static int huft_build(unsigned int *b, const unsigned int n, const unsigned int s, 214static int huft_build(unsigned int *b, const unsigned int n,
213 const unsigned short *d, const extra_bits_t *e, huft_t **t, int *m) 215 const unsigned int s, const unsigned short *d,
216 const extra_bits_t * e, huft_t ** t, int *m)
214{ 217{
215 unsigned a; /* counter for codes of length k */ 218 unsigned a; /* counter for codes of length k */
216 unsigned c[BMAX + 1]; /* bit length count table */ 219 unsigned c[BMAX + 1]; /* bit length count table */
217 unsigned f; /* i repeats in table every f entries */ 220 unsigned f; /* i repeats in table every f entries */
218 int g; /* maximum code length */ 221 int g; /* maximum code length */
219 int h; /* table level */ 222 int h; /* table level */
220 register unsigned i; /* counter, current code */ 223 register unsigned i; /* counter, current code */
221 register unsigned j; /* counter */ 224 register unsigned j; /* counter */
222 register int k; /* number of bits in current code */ 225 register int k; /* number of bits in current code */
223 int l; /* bits per table (returned in m) */ 226 int l; /* bits per table (returned in m) */
224 register unsigned *p; /* pointer into c[], b[], or v[] */ 227 register unsigned *p; /* pointer into c[], b[], or v[] */
225 register huft_t *q; /* points to current table */ 228 register huft_t *q; /* points to current table */
226 huft_t r; /* table entry for structure assignment */ 229 huft_t r; /* table entry for structure assignment */
227 huft_t *u[BMAX]; /* table stack */ 230 huft_t *u[BMAX]; /* table stack */
228 unsigned v[N_MAX]; /* values in order of bit length */ 231 unsigned v[N_MAX]; /* values in order of bit length */
229 register int w; /* bits before this table == (l * h) */ 232 register int w; /* bits before this table == (l * h) */
230 unsigned x[BMAX + 1]; /* bit offsets, then code stack */ 233 unsigned x[BMAX + 1]; /* bit offsets, then code stack */
231 unsigned *xp; /* pointer into x */ 234 unsigned *xp; /* pointer into x */
232 int y; /* number of dummy codes added */ 235 int y; /* number of dummy codes added */
233 unsigned z; /* number of entries in current table */ 236 unsigned z; /* number of entries in current table */
234 237
235 /* Generate counts for each bit length */ 238 /* Generate counts for each bit length */
236 memset ((void *)(c), 0, sizeof(c)); 239 memset((void *) (c), 0, sizeof(c));
237 p = b; 240 p = b;
238 i = n; 241 i = n;
239 do { 242 do {
240 c[*p]++; /* assume all entries <= BMAX */ 243 c[*p]++; /* assume all entries <= BMAX */
241 p++; /* Can't combine with above line (Solaris bug) */ 244 p++; /* Can't combine with above line (Solaris bug) */
242 } while (--i); 245 } while (--i);
243 if (c[0] == n) { /* null input--all zero length codes */ 246 if (c[0] == n) { /* null input--all zero length codes */
244 *t = (huft_t *) NULL; 247 *t = (huft_t *) NULL;
@@ -274,7 +277,7 @@ static int huft_build(unsigned int *b, const unsigned int n, const unsigned int
274 x[1] = j = 0; 277 x[1] = j = 0;
275 p = c + 1; 278 p = c + 1;
276 xp = x + 2; 279 xp = x + 2;
277 while (--i) { /* note that i == g from above */ 280 while (--i) { /* note that i == g from above */
278 *xp++ = (j += *p++); 281 *xp++ = (j += *p++);
279 } 282 }
280 283
@@ -287,7 +290,7 @@ static int huft_build(unsigned int *b, const unsigned int n, const unsigned int
287 } while (++i < n); 290 } while (++i < n);
288 291
289 /* Generate the Huffman codes and for each, make the table entries */ 292 /* Generate the Huffman codes and for each, make the table entries */
290 x[0] = i = 0; /* first Huffman code is zero */ 293 x[0] = i = 0; /* first Huffman code is zero */
291 p = v; /* grab values in bit order */ 294 p = v; /* grab values in bit order */
292 h = -1; /* no tables yet--level -1 */ 295 h = -1; /* no tables yet--level -1 */
293 w = -l; /* bits decoded == (l * h) */ 296 w = -l; /* bits decoded == (l * h) */
@@ -303,7 +306,7 @@ static int huft_build(unsigned int *b, const unsigned int n, const unsigned int
303 /* make tables up to required level */ 306 /* make tables up to required level */
304 while (k > w + l) { 307 while (k > w + l) {
305 h++; 308 h++;
306 w += l; /* previous table always l bits */ 309 w += l; /* previous table always l bits */
307 310
308 /* compute minimum size table less than or equal to l bits */ 311 /* compute minimum size table less than or equal to l bits */
309 z = (z = g - w) > (unsigned) l ? l : z; /* upper limit on table size */ 312 z = (z = g - w) > (unsigned) l ? l : z; /* upper limit on table size */
@@ -316,15 +319,15 @@ static int huft_build(unsigned int *b, const unsigned int n, const unsigned int
316 f -= *xp; /* else deduct codes from patterns */ 319 f -= *xp; /* else deduct codes from patterns */
317 } 320 }
318 } 321 }
319 z = 1 << j; /* table entries for j-bit table */ 322 z = 1 << j; /* table entries for j-bit table */
320 323
321 /* allocate and link in new table */ 324 /* allocate and link in new table */
322 q = (huft_t *) xmalloc((z + 1) * sizeof(huft_t)); 325 q = (huft_t *) xmalloc((z + 1) * sizeof(huft_t));
323 326
324 hufts += z + 1; /* track memory usage */ 327 hufts += z + 1; /* track memory usage */
325 *t = q + 1; /* link to list for huft_free() */ 328 *t = q + 1; /* link to list for huft_free() */
326 *(t = &(q->v.t)) = NULL; 329 *(t = &(q->v.t)) = NULL;
327 u[h] = ++q; /* table starts after link */ 330 u[h] = ++q; /* table starts after link */
328 331
329 /* connect to last table, if there is one */ 332 /* connect to last table, if there is one */
330 if (h) { 333 if (h) {
@@ -340,11 +343,11 @@ static int huft_build(unsigned int *b, const unsigned int n, const unsigned int
340 /* set up table entry in r */ 343 /* set up table entry in r */
341 r.b = (unsigned char) (k - w); 344 r.b = (unsigned char) (k - w);
342 if (p >= v + n) 345 if (p >= v + n)
343 r.e = 99; /* out of values--invalid code */ 346 r.e = 99; /* out of values--invalid code */
344 else if (*p < s) { 347 else if (*p < s) {
345 r.e = (unsigned char) (*p < 256 ? 16 : 15); /* 256 is end-of-block code */ 348 r.e = (unsigned char) (*p < 256 ? 16 : 15); /* 256 is end-of-block code */
346 r.v.n = (unsigned short) (*p); /* simple code is just the value */ 349 r.v.n = (unsigned short) (*p); /* simple code is just the value */
347 p++; /* one compiler does not like *p++ */ 350 p++; /* one compiler does not like *p++ */
348 } else { 351 } else {
349 r.e = (unsigned char) e[*p - s]; /* non-simple--look up in lists */ 352 r.e = (unsigned char) e[*p - s]; /* non-simple--look up in lists */
350 r.v.n = d[*p++ - s]; 353 r.v.n = d[*p++ - s];
@@ -362,7 +365,7 @@ static int huft_build(unsigned int *b, const unsigned int n, const unsigned int
362 365
363 /* backup over finished tables */ 366 /* backup over finished tables */
364 while ((i & ((1 << w) - 1)) != x[h]) { 367 while ((i & ((1 << w) - 1)) != x[h]) {
365 h--; /* don't need to update q */ 368 h--; /* don't need to update q */
366 w -= l; 369 w -= l;
367 } 370 }
368 } 371 }
@@ -378,52 +381,52 @@ static int huft_build(unsigned int *b, const unsigned int n, const unsigned int
378 * tl, td: literal/length and distance decoder tables 381 * tl, td: literal/length and distance decoder tables
379 * bl, bd: number of bits decoded by tl[] and td[] 382 * bl, bd: number of bits decoded by tl[] and td[]
380 */ 383 */
381static int inflate_codes(huft_t *tl, huft_t *td, int bl, int bd) 384static int inflate_codes(huft_t * tl, huft_t * td, int bl, int bd)
382{ 385{
383 register unsigned long e; /* table entry flag/number of extra bits */ 386 register unsigned long e; /* table entry flag/number of extra bits */
384 unsigned long n, d; /* length and index for copy */ 387 unsigned long n, d; /* length and index for copy */
385 unsigned long w; /* current window position */ 388 unsigned long w; /* current window position */
386 huft_t *t; /* pointer to table entry */ 389 huft_t *t; /* pointer to table entry */
387 unsigned ml, md; /* masks for bl and bd bits */ 390 unsigned ml, md; /* masks for bl and bd bits */
388 register unsigned long b; /* bit buffer */ 391 register unsigned long b; /* bit buffer */
389 register unsigned k; /* number of bits in bit buffer */ 392 register unsigned k; /* number of bits in bit buffer */
390 393
391 /* make local copies of globals */ 394 /* make local copies of globals */
392 b = bb; /* initialize bit buffer */ 395 b = bb; /* initialize bit buffer */
393 k = bk; 396 k = bk;
394 w = outcnt; /* initialize window position */ 397 w = outcnt; /* initialize window position */
395 398
396 /* inflate the coded data */ 399 /* inflate the coded data */
397 ml = mask_bits[bl]; /* precompute masks for speed */ 400 ml = mask_bits[bl]; /* precompute masks for speed */
398 md = mask_bits[bd]; 401 md = mask_bits[bd];
399 for (;;) { /* do until end of block */ 402 for (;;) { /* do until end of block */
400 while (k < (unsigned) bl) { 403 while (k < (unsigned) bl) {
401 b |= ((unsigned long)fgetc(in_file)) << k; 404 b |= ((unsigned long) fgetc(in_file)) << k;
402 k += 8; 405 k += 8;
403 } 406 }
404 if ((e = (t = tl + ((unsigned) b & ml))->e) > 16) 407 if ((e = (t = tl + ((unsigned) b & ml))->e) > 16)
405 do { 408 do {
406 if (e == 99) { 409 if (e == 99) {
407 return 1; 410 return 1;
408 } 411 }
409 b >>= t->b; 412 b >>= t->b;
410 k -= t->b; 413 k -= t->b;
411 e -= 16; 414 e -= 16;
412 while (k < e) { 415 while (k < e) {
413 b |= ((unsigned long)fgetc(in_file)) << k; 416 b |= ((unsigned long) fgetc(in_file)) << k;
414 k += 8; 417 k += 8;
415 } 418 }
416 } while ((e = (t = t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16); 419 } while ((e =
420 (t = t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16);
417 b >>= t->b; 421 b >>= t->b;
418 k -= t->b; 422 k -= t->b;
419 if (e == 16) { /* then it's a literal */ 423 if (e == 16) { /* then it's a literal */
420 window[w++] = (unsigned char) t->v.n; 424 window[w++] = (unsigned char) t->v.n;
421 if (w == WSIZE) { 425 if (w == WSIZE) {
422 outcnt=(w), 426 outcnt = (w), flush_window();
423 flush_window();
424 w = 0; 427 w = 0;
425 } 428 }
426 } else { /* it's an EOB or a length */ 429 } else { /* it's an EOB or a length */
427 430
428 /* exit if end of block */ 431 /* exit if end of block */
429 if (e == 15) { 432 if (e == 15) {
@@ -432,7 +435,7 @@ static int inflate_codes(huft_t *tl, huft_t *td, int bl, int bd)
432 435
433 /* get length of block to copy */ 436 /* get length of block to copy */
434 while (k < e) { 437 while (k < e) {
435 b |= ((unsigned long)fgetc(in_file)) << k; 438 b |= ((unsigned long) fgetc(in_file)) << k;
436 k += 8; 439 k += 8;
437 } 440 }
438 n = t->v.n + ((unsigned) b & mask_bits[e]); 441 n = t->v.n + ((unsigned) b & mask_bits[e]);
@@ -441,7 +444,7 @@ static int inflate_codes(huft_t *tl, huft_t *td, int bl, int bd)
441 444
442 /* decode distance of block to copy */ 445 /* decode distance of block to copy */
443 while (k < (unsigned) bd) { 446 while (k < (unsigned) bd) {
444 b |= ((unsigned long)fgetc(in_file)) << k; 447 b |= ((unsigned long) fgetc(in_file)) << k;
445 k += 8; 448 k += 8;
446 } 449 }
447 450
@@ -453,14 +456,16 @@ static int inflate_codes(huft_t *tl, huft_t *td, int bl, int bd)
453 k -= t->b; 456 k -= t->b;
454 e -= 16; 457 e -= 16;
455 while (k < e) { 458 while (k < e) {
456 b |= ((unsigned long)fgetc(in_file)) << k; 459 b |= ((unsigned long) fgetc(in_file)) << k;
457 k += 8; 460 k += 8;
458 } 461 }
459 } while ((e = (t = t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16); 462 } while ((e =
463 (t =
464 t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16);
460 b >>= t->b; 465 b >>= t->b;
461 k -= t->b; 466 k -= t->b;
462 while (k < e) { 467 while (k < e) {
463 b |= ((unsigned long)fgetc(in_file)) << k; 468 b |= ((unsigned long) fgetc(in_file)) << k;
464 k += 8; 469 k += 8;
465 } 470 }
466 d = w - t->v.n - ((unsigned) b & mask_bits[e]); 471 d = w - t->v.n - ((unsigned) b & mask_bits[e]);
@@ -469,20 +474,21 @@ static int inflate_codes(huft_t *tl, huft_t *td, int bl, int bd)
469 474
470 /* do the copy */ 475 /* do the copy */
471 do { 476 do {
472 n -= (e = (e = WSIZE - ((d &= WSIZE - 1) > w ? d : w)) > n ? n : e); 477 n -= (e =
478 (e =
479 WSIZE - ((d &= WSIZE - 1) > w ? d : w)) > n ? n : e);
473#if !defined(NOMEMCPY) && !defined(DEBUG) 480#if !defined(NOMEMCPY) && !defined(DEBUG)
474 if (w - d >= e) { /* (this test assumes unsigned comparison) */ 481 if (w - d >= e) { /* (this test assumes unsigned comparison) */
475 memcpy(window + w, window + d, e); 482 memcpy(window + w, window + d, e);
476 w += e; 483 w += e;
477 d += e; 484 d += e;
478 } else /* do it slow to avoid memcpy() overlap */ 485 } else /* do it slow to avoid memcpy() overlap */
479#endif /* !NOMEMCPY */ 486#endif /* !NOMEMCPY */
480 do { 487 do {
481 window[w++] = window[d++]; 488 window[w++] = window[d++];
482 } while (--e); 489 } while (--e);
483 if (w == WSIZE) { 490 if (w == WSIZE) {
484 outcnt=(w), 491 outcnt = (w), flush_window();
485 flush_window();
486 w = 0; 492 w = 0;
487 } 493 }
488 } while (n); 494 } while (n);
@@ -498,28 +504,30 @@ static int inflate_codes(huft_t *tl, huft_t *td, int bl, int bd)
498 return 0; 504 return 0;
499} 505}
500 506
501static const unsigned short cplens[] = { /* Copy lengths for literal codes 257..285 */ 507static const unsigned short cplens[] = { /* Copy lengths for literal codes 257..285 */
502 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 508 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
503 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0 509 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
504}; 510};
511
505/* note: see note #13 above about the 258 in this list. */ 512/* note: see note #13 above about the 258 in this list. */
506static const extra_bits_t cplext[] = { /* Extra bits for literal codes 257..285 */ 513static const extra_bits_t cplext[] = { /* Extra bits for literal codes 257..285 */
507 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 514 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
508 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99 515 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99
509}; /* 99==invalid */ 516}; /* 99==invalid */
510static const unsigned short cpdist[] = { /* Copy offsets for distance codes 0..29 */ 517static const unsigned short cpdist[] = { /* Copy offsets for distance codes 0..29 */
511 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 518 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
512 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 519 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
513 8193, 12289, 16385, 24577 520 8193, 12289, 16385, 24577
514}; 521};
515static const extra_bits_t cpdext[] = { /* Extra bits for distance codes */ 522static const extra_bits_t cpdext[] = { /* Extra bits for distance codes */
516 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 523 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
517 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 524 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
518 12, 12, 13, 13 525 12, 12, 13, 13
519}; 526};
527
520/* Tables for deflate from PKZIP's appnote.txt. */ 528/* Tables for deflate from PKZIP's appnote.txt. */
521static const extra_bits_t border[] = { /* Order of the bit length code lengths */ 529static const extra_bits_t border[] = { /* Order of the bit length code lengths */
522 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 530 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
523}; 531};
524 532
525/* 533/*
@@ -531,8 +539,8 @@ static const extra_bits_t border[] = { /* Order of the bit length code lengths
531static int inflate_block(int *e) 539static int inflate_block(int *e)
532{ 540{
533 unsigned t; /* block type */ 541 unsigned t; /* block type */
534 register unsigned long b; /* bit buffer */ 542 register unsigned long b; /* bit buffer */
535 register unsigned k; /* number of bits in bit buffer */ 543 register unsigned k; /* number of bits in bit buffer */
536 544
537 /* make local bit buffer */ 545 /* make local bit buffer */
538 b = bb; 546 b = bb;
@@ -540,7 +548,7 @@ static int inflate_block(int *e)
540 548
541 /* read in last block bit */ 549 /* read in last block bit */
542 while (k < 1) { 550 while (k < 1) {
543 b |= ((unsigned long)fgetc(in_file)) << k; 551 b |= ((unsigned long) fgetc(in_file)) << k;
544 k += 8; 552 k += 8;
545 } 553 }
546 *e = (int) b & 1; 554 *e = (int) b & 1;
@@ -549,7 +557,7 @@ static int inflate_block(int *e)
549 557
550 /* read in block type */ 558 /* read in block type */
551 while (k < 2) { 559 while (k < 2) {
552 b |= ((unsigned long)fgetc(in_file)) << k; 560 b |= ((unsigned long) fgetc(in_file)) << k;
553 k += 8; 561 k += 8;
554 } 562 }
555 t = (unsigned) b & 3; 563 t = (unsigned) b & 3;
@@ -562,286 +570,287 @@ static int inflate_block(int *e)
562 570
563 /* inflate that block type */ 571 /* inflate that block type */
564 switch (t) { 572 switch (t) {
565 case 0: /* Inflate stored */ 573 case 0: /* Inflate stored */
566 { 574 {
567 unsigned long n; /* number of bytes in block */ 575 unsigned long n; /* number of bytes in block */
568 unsigned long w; /* current window position */ 576 unsigned long w; /* current window position */
569 register unsigned long b_stored; /* bit buffer */ 577 register unsigned long b_stored; /* bit buffer */
570 register unsigned long k_stored; /* number of bits in bit buffer */ 578 register unsigned long k_stored; /* number of bits in bit buffer */
571 579
572 /* make local copies of globals */ 580 /* make local copies of globals */
573 b_stored = bb; /* initialize bit buffer */ 581 b_stored = bb; /* initialize bit buffer */
574 k_stored = bk; 582 k_stored = bk;
575 w = outcnt; /* initialize window position */ 583 w = outcnt; /* initialize window position */
576 584
577 /* go to byte boundary */ 585 /* go to byte boundary */
578 n = k_stored & 7; 586 n = k_stored & 7;
579 b_stored >>= n; 587 b_stored >>= n;
580 k_stored -= n; 588 k_stored -= n;
581 589
582 /* get the length and its complement */ 590 /* get the length and its complement */
583 while (k_stored < 16) { 591 while (k_stored < 16) {
584 b_stored |= ((unsigned long)fgetc(in_file)) << k_stored; 592 b_stored |= ((unsigned long) fgetc(in_file)) << k_stored;
585 k_stored += 8; 593 k_stored += 8;
586 } 594 }
587 n = ((unsigned) b_stored & 0xffff); 595 n = ((unsigned) b_stored & 0xffff);
588 b_stored >>= 16; 596 b_stored >>= 16;
589 k_stored -= 16; 597 k_stored -= 16;
590 while (k_stored < 16) { 598 while (k_stored < 16) {
591 b_stored |= ((unsigned long)fgetc(in_file)) << k_stored; 599 b_stored |= ((unsigned long) fgetc(in_file)) << k_stored;
600 k_stored += 8;
601 }
602 if (n != (unsigned) ((~b_stored) & 0xffff)) {
603 return 1; /* error in compressed data */
604 }
605 b_stored >>= 16;
606 k_stored -= 16;
607
608 /* read and output the compressed data */
609 while (n--) {
610 while (k_stored < 8) {
611 b_stored |= ((unsigned long) fgetc(in_file)) << k_stored;
592 k_stored += 8; 612 k_stored += 8;
593 } 613 }
594 if (n != (unsigned) ((~b_stored) & 0xffff)) { 614 window[w++] = (unsigned char) b_stored;
595 return 1; /* error in compressed data */ 615 if (w == (unsigned long) WSIZE) {
596 } 616 outcnt = (w), flush_window();
597 b_stored >>= 16; 617 w = 0;
598 k_stored -= 16;
599
600 /* read and output the compressed data */
601 while (n--) {
602 while (k_stored < 8) {
603 b_stored |= ((unsigned long)fgetc(in_file)) << k_stored;
604 k_stored += 8;
605 }
606 window[w++] = (unsigned char) b_stored;
607 if (w == (unsigned long)WSIZE) {
608 outcnt=(w),
609 flush_window();
610 w = 0;
611 }
612 b_stored >>= 8;
613 k_stored -= 8;
614 } 618 }
619 b_stored >>= 8;
620 k_stored -= 8;
621 }
615 622
616 /* restore the globals from the locals */ 623 /* restore the globals from the locals */
617 outcnt = w; /* restore global window pointer */ 624 outcnt = w; /* restore global window pointer */
618 bb = b_stored; /* restore global bit buffer */ 625 bb = b_stored; /* restore global bit buffer */
619 bk = k_stored; 626 bk = k_stored;
620 return 0; 627 return 0;
628 }
629 case 1: /* Inflate fixed
630 * decompress an inflated type 1 (fixed Huffman codes) block. We should
631 * either replace this with a custom decoder, or at least precompute the
632 * Huffman tables.
633 */
634 {
635 int i; /* temporary variable */
636 huft_t *tl; /* literal/length code table */
637 huft_t *td; /* distance code table */
638 int bl; /* lookup bits for tl */
639 int bd; /* lookup bits for td */
640 unsigned int l[288]; /* length list for huft_build */
641
642 /* set up literal table */
643 for (i = 0; i < 144; i++) {
644 l[i] = 8;
645 }
646 for (; i < 256; i++) {
647 l[i] = 9;
648 }
649 for (; i < 280; i++) {
650 l[i] = 7;
651 }
652 for (; i < 288; i++) { /* make a complete, but wrong code set */
653 l[i] = 8;
654 }
655 bl = 7;
656 if ((i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl)) != 0) {
657 return i;
621 } 658 }
622 case 1: /* Inflate fixed
623 * decompress an inflated type 1 (fixed Huffman codes) block. We should
624 * either replace this with a custom decoder, or at least precompute the
625 * Huffman tables.
626 */
627 {
628 int i; /* temporary variable */
629 huft_t *tl; /* literal/length code table */
630 huft_t *td; /* distance code table */
631 int bl; /* lookup bits for tl */
632 int bd; /* lookup bits for td */
633 unsigned int l[288]; /* length list for huft_build */
634
635 /* set up literal table */
636 for (i = 0; i < 144; i++) {
637 l[i] = 8;
638 }
639 for (; i < 256; i++) {
640 l[i] = 9;
641 }
642 for (; i < 280; i++) {
643 l[i] = 7;
644 }
645 for (; i < 288; i++) { /* make a complete, but wrong code set */
646 l[i] = 8;
647 }
648 bl = 7;
649 if ((i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl)) != 0) {
650 return i;
651 }
652 659
653 /* set up distance table */ 660 /* set up distance table */
654 for (i = 0; i < 30; i++) { /* make an incomplete code set */ 661 for (i = 0; i < 30; i++) { /* make an incomplete code set */
655 l[i] = 5; 662 l[i] = 5;
656 } 663 }
657 bd = 5; 664 bd = 5;
658 if ((i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd)) > 1) { 665 if ((i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd)) > 1) {
659 huft_free(tl); 666 huft_free(tl);
660 return i; 667 return i;
661 } 668 }
662 669
663 /* decompress until an end-of-block code */ 670 /* decompress until an end-of-block code */
664 if (inflate_codes(tl, td, bl, bd)) 671 if (inflate_codes(tl, td, bl, bd))
665 return 1; 672 return 1;
666 673
667 /* free the decoding tables, return */ 674 /* free the decoding tables, return */
668 huft_free(tl); 675 huft_free(tl);
669 huft_free(td); 676 huft_free(td);
670 return 0; 677 return 0;
678 }
679 case 2: /* Inflate dynamic */
680 {
681 const int dbits = 6; /* bits in base distance lookup table */
682 const int lbits = 9; /* bits in base literal/length lookup table */
683
684 int i; /* temporary variables */
685 unsigned j;
686 unsigned l; /* last length */
687 unsigned m; /* mask for bit lengths table */
688 unsigned n; /* number of lengths to get */
689 huft_t *tl; /* literal/length code table */
690 huft_t *td; /* distance code table */
691 int bl; /* lookup bits for tl */
692 int bd; /* lookup bits for td */
693 unsigned nb; /* number of bit length codes */
694 unsigned nl; /* number of literal/length codes */
695 unsigned nd; /* number of distance codes */
696
697 unsigned ll[286 + 30]; /* literal/length and distance code lengths */
698 register unsigned long b_dynamic; /* bit buffer */
699 register unsigned k_dynamic; /* number of bits in bit buffer */
700
701 /* make local bit buffer */
702 b_dynamic = bb;
703 k_dynamic = bk;
704
705 /* read in table lengths */
706 while (k_dynamic < 5) {
707 b_dynamic |= ((unsigned long) fgetc(in_file)) << k_dynamic;
708 k_dynamic += 8;
709 }
710 nl = 257 + ((unsigned) b_dynamic & 0x1f); /* number of literal/length codes */
711 b_dynamic >>= 5;
712 k_dynamic -= 5;
713 while (k_dynamic < 5) {
714 b_dynamic |= ((unsigned long) fgetc(in_file)) << k_dynamic;
715 k_dynamic += 8;
716 }
717 nd = 1 + ((unsigned) b_dynamic & 0x1f); /* number of distance codes */
718 b_dynamic >>= 5;
719 k_dynamic -= 5;
720 while (k_dynamic < 4) {
721 b_dynamic |= ((unsigned long) fgetc(in_file)) << k_dynamic;
722 k_dynamic += 8;
723 }
724 nb = 4 + ((unsigned) b_dynamic & 0xf); /* number of bit length codes */
725 b_dynamic >>= 4;
726 k_dynamic -= 4;
727 if (nl > 286 || nd > 30) {
728 return 1; /* bad lengths */
671 } 729 }
672 case 2: /* Inflate dynamic */ 730
673 { 731 /* read in bit-length-code lengths */
674 const int dbits = 6; /* bits in base distance lookup table */ 732 for (j = 0; j < nb; j++) {
675 const int lbits = 9; /* bits in base literal/length lookup table */ 733 while (k_dynamic < 3) {
676 734 b_dynamic |= ((unsigned long) fgetc(in_file)) << k_dynamic;
677 int i; /* temporary variables */
678 unsigned j;
679 unsigned l; /* last length */
680 unsigned m; /* mask for bit lengths table */
681 unsigned n; /* number of lengths to get */
682 huft_t *tl; /* literal/length code table */
683 huft_t *td; /* distance code table */
684 int bl; /* lookup bits for tl */
685 int bd; /* lookup bits for td */
686 unsigned nb; /* number of bit length codes */
687 unsigned nl; /* number of literal/length codes */
688 unsigned nd; /* number of distance codes */
689
690 unsigned ll[286 + 30]; /* literal/length and distance code lengths */
691 register unsigned long b_dynamic; /* bit buffer */
692 register unsigned k_dynamic; /* number of bits in bit buffer */
693
694 /* make local bit buffer */
695 b_dynamic = bb;
696 k_dynamic = bk;
697
698 /* read in table lengths */
699 while (k_dynamic < 5) {
700 b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic;
701 k_dynamic += 8; 735 k_dynamic += 8;
702 } 736 }
703 nl = 257 + ((unsigned) b_dynamic & 0x1f); /* number of literal/length codes */ 737 ll[border[j]] = (unsigned) b_dynamic & 7;
704 b_dynamic >>= 5; 738 b_dynamic >>= 3;
705 k_dynamic -= 5; 739 k_dynamic -= 3;
706 while (k_dynamic < 5) { 740 }
707 b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic; 741 for (; j < 19; j++) {
708 k_dynamic += 8; 742 ll[border[j]] = 0;
743 }
744
745 /* build decoding table for trees--single level, 7 bit lookup */
746 bl = 7;
747 if ((i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl)) != 0) {
748 if (i == 1) {
749 huft_free(tl);
709 } 750 }
710 nd = 1 + ((unsigned) b_dynamic & 0x1f); /* number of distance codes */ 751 return i; /* incomplete code set */
711 b_dynamic >>= 5; 752 }
712 k_dynamic -= 5; 753
713 while (k_dynamic < 4) { 754 /* read in literal and distance code lengths */
714 b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic; 755 n = nl + nd;
756 m = mask_bits[bl];
757 i = l = 0;
758 while ((unsigned) i < n) {
759 while (k_dynamic < (unsigned) bl) {
760 b_dynamic |= ((unsigned long) fgetc(in_file)) << k_dynamic;
715 k_dynamic += 8; 761 k_dynamic += 8;
716 } 762 }
717 nb = 4 + ((unsigned) b_dynamic & 0xf); /* number of bit length codes */ 763 j = (td = tl + ((unsigned) b_dynamic & m))->b;
718 b_dynamic >>= 4; 764 b_dynamic >>= j;
719 k_dynamic -= 4; 765 k_dynamic -= j;
720 if (nl > 286 || nd > 30) { 766 j = td->v.n;
721 return 1; /* bad lengths */ 767 if (j < 16) { /* length of code in bits (0..15) */
722 } 768 ll[i++] = l = j; /* save last length in l */
723 769 } else if (j == 16) { /* repeat last length 3 to 6 times */
724 /* read in bit-length-code lengths */ 770 while (k_dynamic < 2) {
725 for (j = 0; j < nb; j++) { 771 b_dynamic |=
772 ((unsigned long) fgetc(in_file)) << k_dynamic;
773 k_dynamic += 8;
774 }
775 j = 3 + ((unsigned) b_dynamic & 3);
776 b_dynamic >>= 2;
777 k_dynamic -= 2;
778 if ((unsigned) i + j > n) {
779 return 1;
780 }
781 while (j--) {
782 ll[i++] = l;
783 }
784 } else if (j == 17) { /* 3 to 10 zero length codes */
726 while (k_dynamic < 3) { 785 while (k_dynamic < 3) {
727 b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic; 786 b_dynamic |=
787 ((unsigned long) fgetc(in_file)) << k_dynamic;
728 k_dynamic += 8; 788 k_dynamic += 8;
729 } 789 }
730 ll[border[j]] = (unsigned) b_dynamic & 7; 790 j = 3 + ((unsigned) b_dynamic & 7);
731 b_dynamic >>= 3; 791 b_dynamic >>= 3;
732 k_dynamic -= 3; 792 k_dynamic -= 3;
733 } 793 if ((unsigned) i + j > n) {
734 for (; j < 19; j++) { 794 return 1;
735 ll[border[j]] = 0;
736 }
737
738 /* build decoding table for trees--single level, 7 bit lookup */
739 bl = 7;
740 if ((i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl)) != 0) {
741 if (i == 1) {
742 huft_free(tl);
743 } 795 }
744 return i; /* incomplete code set */ 796 while (j--) {
745 } 797 ll[i++] = 0;
746 798 }
747 /* read in literal and distance code lengths */ 799 l = 0;
748 n = nl + nd; 800 } else { /* j == 18: 11 to 138 zero length codes */
749 m = mask_bits[bl]; 801 while (k_dynamic < 7) {
750 i = l = 0; 802 b_dynamic |=
751 while ((unsigned) i < n) { 803 ((unsigned long) fgetc(in_file)) << k_dynamic;
752 while (k_dynamic < (unsigned) bl) {
753 b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic;
754 k_dynamic += 8; 804 k_dynamic += 8;
755 } 805 }
756 j = (td = tl + ((unsigned) b_dynamic & m))->b; 806 j = 11 + ((unsigned) b_dynamic & 0x7f);
757 b_dynamic >>= j; 807 b_dynamic >>= 7;
758 k_dynamic -= j; 808 k_dynamic -= 7;
759 j = td->v.n; 809 if ((unsigned) i + j > n) {
760 if (j < 16) { /* length of code in bits (0..15) */ 810 return 1;
761 ll[i++] = l = j; /* save last length in l */
762 } 811 }
763 else if (j == 16) { /* repeat last length 3 to 6 times */ 812 while (j--) {
764 while (k_dynamic < 2) { 813 ll[i++] = 0;
765 b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic;
766 k_dynamic += 8;
767 }
768 j = 3 + ((unsigned) b_dynamic & 3);
769 b_dynamic >>= 2;
770 k_dynamic -= 2;
771 if ((unsigned) i + j > n) {
772 return 1;
773 }
774 while (j--) {
775 ll[i++] = l;
776 }
777 } else if (j == 17) { /* 3 to 10 zero length codes */
778 while (k_dynamic < 3) {
779 b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic;
780 k_dynamic += 8;
781 }
782 j = 3 + ((unsigned) b_dynamic & 7);
783 b_dynamic >>= 3;
784 k_dynamic -= 3;
785 if ((unsigned) i + j > n) {
786 return 1;
787 }
788 while (j--) {
789 ll[i++] = 0;
790 }
791 l = 0;
792 } else { /* j == 18: 11 to 138 zero length codes */
793 while (k_dynamic < 7) {
794 b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic;
795 k_dynamic += 8;
796 }
797 j = 11 + ((unsigned) b_dynamic & 0x7f);
798 b_dynamic >>= 7;
799 k_dynamic -= 7;
800 if ((unsigned) i + j > n) {
801 return 1;
802 }
803 while (j--) {
804 ll[i++] = 0;
805 }
806 l = 0;
807 } 814 }
815 l = 0;
808 } 816 }
817 }
809 818
810 /* free decoding table for trees */ 819 /* free decoding table for trees */
811 huft_free(tl); 820 huft_free(tl);
812 821
813 /* restore the global bit buffer */ 822 /* restore the global bit buffer */
814 bb = b_dynamic; 823 bb = b_dynamic;
815 bk = k_dynamic; 824 bk = k_dynamic;
816 825
817 /* build the decoding tables for literal/length and distance codes */ 826 /* build the decoding tables for literal/length and distance codes */
818 bl = lbits; 827 bl = lbits;
819 if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0) { 828 if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0) {
820 if (i == 1) { 829 if (i == 1) {
821 error_msg("Incomplete literal tree"); 830 error_msg("Incomplete literal tree");
822 huft_free(tl);
823 }
824 return i; /* incomplete code set */
825 }
826 bd = dbits;
827 if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0) {
828 if (i == 1) {
829 error_msg("incomplete distance tree");
830 huft_free(td);
831 }
832 huft_free(tl); 831 huft_free(tl);
833 return i; /* incomplete code set */
834 } 832 }
835 833 return i; /* incomplete code set */
836 /* decompress until an end-of-block code */ 834 }
837 if (inflate_codes(tl, td, bl, bd)) 835 bd = dbits;
838 return 1; 836 if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0) {
839 837 if (i == 1) {
840 /* free the decoding tables, return */ 838 error_msg("incomplete distance tree");
839 huft_free(td);
840 }
841 huft_free(tl); 841 huft_free(tl);
842 huft_free(td); 842 return i; /* incomplete code set */
843 return 0;
844 } 843 }
844
845 /* decompress until an end-of-block code */
846 if (inflate_codes(tl, td, bl, bd))
847 return 1;
848
849 /* free the decoding tables, return */
850 huft_free(tl);
851 huft_free(td);
852 return 0;
853 }
845 default: 854 default:
846 /* bad block type */ 855 /* bad block type */
847 return 2; 856 return 2;
@@ -853,7 +862,7 @@ static int inflate_block(int *e)
853 * 862 *
854 * GLOBAL VARIABLES: outcnt, bk, bb, hufts, inptr 863 * GLOBAL VARIABLES: outcnt, bk, bb, hufts, inptr
855 */ 864 */
856extern int inflate(FILE *in, FILE *out) 865extern int inflate(FILE * in, FILE * out)
857{ 866{
858 int e; /* last block flag */ 867 int e; /* last block flag */
859 int r; /* result code */ 868 int r; /* result code */
@@ -868,7 +877,7 @@ extern int inflate(FILE *in, FILE *out)
868 out_file = out; 877 out_file = out;
869 878
870 /* Allocate all global buffers (for DYN_ALLOC option) */ 879 /* Allocate all global buffers (for DYN_ALLOC option) */
871 window = xmalloc((size_t)(((2L*WSIZE)+1L)*sizeof(unsigned char))); 880 window = xmalloc((size_t) (((2L * WSIZE) + 1L) * sizeof(unsigned char)));
872 bytes_out = 0L; 881 bytes_out = 0L;
873 882
874 /* Create the crc table */ 883 /* Create the crc table */
@@ -910,40 +919,35 @@ extern int inflate(FILE *in, FILE *out)
910 * The magic header has already been checked. The output buffer is cleared. 919 * The magic header has already been checked. The output buffer is cleared.
911 * in, out: input and output file descriptors 920 * in, out: input and output file descriptors
912 */ 921 */
913extern int unzip(FILE *l_in_file, FILE *l_out_file) 922extern int unzip(FILE * l_in_file, FILE * l_out_file)
914{ 923{
915 unsigned char buf[8]; /* extended local header */ 924 unsigned char buf[8]; /* extended local header */
916 unsigned char flags; /* compression flags */ 925 unsigned char flags; /* compression flags */
917 typedef void (*sig_type) (int); 926 typedef void (*sig_type) (int);
918 unsigned short i; 927 unsigned short i;
919 unsigned char magic [2]; 928 unsigned char magic[2];
920 929
921 if (signal(SIGINT, SIG_IGN) != SIG_IGN) { 930 if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
922 (void) signal(SIGINT, (sig_type) abort_gzip); 931 (void) signal(SIGINT, (sig_type) abort_gzip);
923 } 932 }
924#ifdef SIGTERM
925// if (signal(SIGTERM, SIG_IGN) != SIG_IGN) {
926// (void) signal(SIGTERM, (sig_type) abort_gzip);
927// }
928#endif
929#ifdef SIGHUP 933#ifdef SIGHUP
930 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) { 934 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
931 (void) signal(SIGHUP, (sig_type) abort_gzip); 935 (void) signal(SIGHUP, (sig_type) abort_gzip);
932 } 936 }
933#endif 937#endif
934 938
935 magic [0] = fgetc(l_in_file); 939 magic[0] = fgetc(l_in_file);
936 magic [1] = fgetc(l_in_file); 940 magic[1] = fgetc(l_in_file);
937 941
938#ifdef CONFIG_FEATURE_UNCOMPRESS 942#ifdef CONFIG_FEATURE_UNCOMPRESS
939 /* Magic header for compress files, 1F 9d = \037\235 */ 943 /* Magic header for compress files, 1F 9d = \037\235 */
940 if (( magic [0] == 0x1F ) && ( magic [1] == 0x9d)) { 944 if ((magic[0] == 0x1F) && (magic[1] == 0x9d)) {
941 return uncompress ( l_in_file, l_out_file ); 945 return uncompress(l_in_file, l_out_file);
942 } 946 }
943#endif 947#endif
944 948
945 /* Magic header for gzip files, 1F 8B = \037\213 */ 949 /* Magic header for gzip files, 1F 8B = \037\213 */
946 if (( magic [0] != 0x1F ) || ( magic [1] != 0x8b)) { 950 if ((magic[0] != 0x1F) || (magic[1] != 0x8b)) {
947 error_msg("Invalid gzip magic"); 951 error_msg("Invalid gzip magic");
948 return EXIT_FAILURE; 952 return EXIT_FAILURE;
949 } 953 }
@@ -951,7 +955,7 @@ extern int unzip(FILE *l_in_file, FILE *l_out_file)
951 /* Check the compression method */ 955 /* Check the compression method */
952 if (fgetc(l_in_file) != 8) { 956 if (fgetc(l_in_file) != 8) {
953 error_msg("Unknown compression method"); 957 error_msg("Unknown compression method");
954 return(-1); 958 return (-1);
955 } 959 }
956 960
957 flags = (unsigned char) fgetc(l_in_file); 961 flags = (unsigned char) fgetc(l_in_file);
@@ -963,7 +967,8 @@ extern int unzip(FILE *l_in_file, FILE *l_out_file)
963 967
964 if (flags & 0x04) { 968 if (flags & 0x04) {
965 /* bit 2 set: extra field present */ 969 /* bit 2 set: extra field present */
966 const unsigned short extra = fgetc(l_in_file) + (fgetc(l_in_file) << 8); 970 const unsigned short extra =
971 fgetc(l_in_file) + (fgetc(l_in_file) << 8);
967 972
968 for (i = 0; i < extra; i++) { 973 for (i = 0; i < extra; i++) {
969 fgetc(l_in_file); 974 fgetc(l_in_file);
@@ -994,11 +999,14 @@ extern int unzip(FILE *l_in_file, FILE *l_out_file)
994 fread(buf, 1, 8, l_in_file); 999 fread(buf, 1, 8, l_in_file);
995 1000
996 /* Validate decompression - crc */ 1001 /* Validate decompression - crc */
997 if ((unsigned int)((buf[0] | (buf[1] << 8)) |((buf[2] | (buf[3] << 8)) << 16)) != (crc ^ 0xffffffffL)) { 1002 if ((unsigned int) ((buf[0] | (buf[1] << 8)) |
1003 ((buf[2] | (buf[3] << 8)) << 16)) !=
1004 (crc ^ 0xffffffffL)) {
998 error_msg("invalid compressed data--crc error"); 1005 error_msg("invalid compressed data--crc error");
999 } 1006 }
1000 /* Validate decompression - size */ 1007 /* Validate decompression - size */
1001 if (((buf[4] | (buf[5] << 8)) |((buf[6] | (buf[7] << 8)) << 16)) != (unsigned long) bytes_out) { 1008 if (((buf[4] | (buf[5] << 8)) | ((buf[6] | (buf[7] << 8)) << 16)) !=
1009 (unsigned long) bytes_out) {
1002 error_msg("invalid compressed data--length error"); 1010 error_msg("invalid compressed data--length error");
1003 } 1011 }
1004 1012
@@ -1011,7 +1019,8 @@ extern int unzip(FILE *l_in_file, FILE *l_out_file)
1011extern void gz_close(int gunzip_pid) 1019extern void gz_close(int gunzip_pid)
1012{ 1020{
1013 if (kill(gunzip_pid, SIGTERM) == -1) { 1021 if (kill(gunzip_pid, SIGTERM) == -1) {
1014 error_msg_and_die("*** Couldnt kill old gunzip process *** aborting"); 1022 error_msg_and_die
1023 ("*** Couldnt kill old gunzip process *** aborting");
1015 } 1024 }
1016 1025
1017 if (waitpid(gunzip_pid, NULL, 0) == -1) { 1026 if (waitpid(gunzip_pid, NULL, 0) == -1) {