blob: ee746216d018ff0ba761bd8504da77886e706742 [file] [log] [blame]
Glenn L McGrath7fd92942001-04-11 03:11:33 +00001/* vi: set sw=4 ts=4: */
2/*
3 * gunzip implementation for busybox
4 *
5 * Based on GNU gzip v1.2.4 Copyright (C) 1992-1993 Jean-loup Gailly.
6 *
7 * Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de>
8 * based on gzip sources
9 *
10 * Adjusted further by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
11 * to support files as well as stdin/stdout, and to generally behave itself wrt
12 * command line handling.
13 *
14 * General cleanup to better adhere to the style guide and make use of standard
15 * busybox functions by Glenn McGrath <bug1@optushome.com.au>
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 *
31 *
32 * gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
33 * Copyright (C) 1992-1993 Jean-loup Gailly
34 * The unzip code was written and put in the public domain by Mark Adler.
35 * Portions of the lzw code are derived from the public domain 'compress'
36 * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
37 * Ken Turkowski, Dave Mack and Peter Jannesen.
38 *
39 * See the license_msg below and the file COPYING for the software license.
40 * See the file algorithm.doc for the compression algorithms and file formats.
41 */
42
43#if 0
44static char *license_msg[] = {
45 " Copyright (C) 1992-1993 Jean-loup Gailly",
46 " This program is free software; you can redistribute it and/or modify",
47 " it under the terms of the GNU General Public License as published by",
48 " the Free Software Foundation; either version 2, or (at your option)",
49 " any later version.",
50 "",
51 " This program is distributed in the hope that it will be useful,",
52 " but WITHOUT ANY WARRANTY; without even the implied warranty of",
53 " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the",
54 " GNU General Public License for more details.",
55 "",
56 " You should have received a copy of the GNU General Public License",
57 " along with this program; if not, write to the Free Software",
58 " Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.",
59 0
60};
61#endif
62
63#include <sys/types.h>
64#include <sys/wait.h>
65#include <signal.h>
66#include <stdlib.h>
Manuel Novoa III a2949aa2001-06-29 18:59:32 +000067#include <string.h>
Glenn L McGrath7fd92942001-04-11 03:11:33 +000068#include "libbb.h"
Glenn L McGrath7fd92942001-04-11 03:11:33 +000069
Eric Andersen044228d2001-07-17 01:12:36 +000070static FILE *in_file, *out_file;
Glenn L McGrath7fd92942001-04-11 03:11:33 +000071
72/* these are freed by gz_close */
73static unsigned char *window;
74static unsigned long *crc_table;
75
76static unsigned long crc = 0xffffffffL; /* shift register contents */
77
78/* Return codes from gzip */
79static const int ERROR = 1;
80
81/*
82 * window size--must be a power of two, and
83 * at least 32K for zip's deflate method
84 */
85static const int WSIZE = 0x8000;
86
87/* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
88static const int BMAX = 16; /* maximum bit length of any code (16 for explode) */
89static const int N_MAX = 288; /* maximum number of codes in any set */
90
91static long bytes_out; /* number of output bytes */
92static unsigned long outcnt; /* bytes in output buffer */
93
Eric Andersen044228d2001-07-17 01:12:36 +000094static unsigned hufts; /* track memory usage */
95static unsigned long bb; /* bit buffer */
96static unsigned bk; /* bits in bit buffer */
Glenn L McGrath7fd92942001-04-11 03:11:33 +000097
98typedef struct huft_s {
99 unsigned char e; /* number of extra bits or operation */
100 unsigned char b; /* number of bits in this code or subcode */
101 union {
102 unsigned short n; /* literal, length base, or distance base */
103 struct huft_s *t; /* pointer to next level of table */
104 } v;
105} huft_t;
106
Eric Andersen044228d2001-07-17 01:12:36 +0000107static const unsigned short mask_bits[] = {
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000108 0x0000,
109 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
110 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
111};
112
113//static int error_number = 0;
114/* ========================================================================
115 * Signal and error handler.
116 */
117
118static void abort_gzip()
119{
120 error_msg("gzip aborted\n");
121 exit(ERROR);
122}
123
124static void make_crc_table()
125{
126 unsigned long table_entry; /* crc shift register */
127 unsigned long poly = 0; /* polynomial exclusive-or pattern */
128 int i; /* counter for all possible eight bit values */
129 int k; /* byte being shifted into crc apparatus */
130
131 /* terms of polynomial defining this crc (except x^32): */
132 static int p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
133
134 crc_table = (unsigned long *) malloc(256 * sizeof(unsigned long));
135
136 /* Make exclusive-or pattern from polynomial (0xedb88320) */
137 for (i = 0; i < sizeof(p)/sizeof(int); i++)
138 poly |= 1L << (31 - p[i]);
139
140 /* Compute and print table of CRC's, five per line */
141 for (i = 0; i < 256; i++) {
142 table_entry = i;
143 /* The idea to initialize the register with the byte instead of
144 * zero was stolen from Haruhiko Okumura's ar002
145 */
146 for (k = 8; k; k--) {
147 table_entry = table_entry & 1 ? (table_entry >> 1) ^ poly : table_entry >> 1;
148 }
149 crc_table[i]=table_entry;
150 }
151}
152
153/* ===========================================================================
154 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
155 * (Used for the decompressed data only.)
156 */
Eric Andersen044228d2001-07-17 01:12:36 +0000157static void flush_window(void)
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000158{
159 int n;
160
161 if (outcnt == 0)
162 return;
163
164 for (n = 0; n < outcnt; n++) {
165 crc = crc_table[((int) crc ^ (window[n])) & 0xff] ^ (crc >> 8);
166 }
167
168 if (fwrite(window, 1, outcnt, out_file) != outcnt) {
169 error_msg_and_die("Couldnt write");
170 }
171 bytes_out += (unsigned long) outcnt;
172 outcnt = 0;
173}
174
175/*
176 * Free the malloc'ed tables built by huft_build(), which makes a linked
177 * list of the tables it made, with the links in a dummy first entry of
178 * each table.
179 * t: table to free
180 */
181static int huft_free(huft_t *t)
182{
183 huft_t *p, *q;
184
185 /* Go through linked list, freeing from the malloced (t[-1]) address. */
186 p = t;
187 while (p != (huft_t *) NULL) {
188 q = (--p)->v.t;
189 free((char *) p);
190 p = q;
191 }
192 return 0;
193}
194
195/* Given a list of code lengths and a maximum table size, make a set of
196 * tables to decode that set of codes. Return zero on success, one if
197 * the given code set is incomplete (the tables are still built in this
198 * case), two if the input is invalid (all zero length codes or an
199 * oversubscribed set of lengths), and three if not enough memory.
200 *
201 * b: code lengths in bits (all assumed <= BMAX)
202 * n: number of codes (assumed <= N_MAX)
203 * s: number of simple-valued codes (0..s-1)
204 * d: list of base values for non-simple codes
205 * e: list of extra bits for non-simple codes
206 * t: result: starting table
207 * m: maximum lookup bits, returns actual
208 */
209static int huft_build(unsigned int *b, const unsigned int n, const unsigned int s,
210 const unsigned short *d, const unsigned short *e, huft_t **t, int *m)
211{
212 unsigned a; /* counter for codes of length k */
213 unsigned c[BMAX + 1]; /* bit length count table */
214 unsigned f; /* i repeats in table every f entries */
215 int g; /* maximum code length */
216 int h; /* table level */
217 register unsigned i; /* counter, current code */
218 register unsigned j; /* counter */
219 register int k; /* number of bits in current code */
220 int l; /* bits per table (returned in m) */
221 register unsigned *p; /* pointer into c[], b[], or v[] */
222 register huft_t *q; /* points to current table */
223 huft_t r; /* table entry for structure assignment */
224 huft_t *u[BMAX]; /* table stack */
225 unsigned v[N_MAX]; /* values in order of bit length */
226 register int w; /* bits before this table == (l * h) */
227 unsigned x[BMAX + 1]; /* bit offsets, then code stack */
228 unsigned *xp; /* pointer into x */
229 int y; /* number of dummy codes added */
230 unsigned z; /* number of entries in current table */
231
232 /* Generate counts for each bit length */
233 memset ((void *)(c), 0, sizeof(c));
234 p = b;
235 i = n;
236 do {
237 c[*p]++; /* assume all entries <= BMAX */
238 p++; /* Can't combine with above line (Solaris bug) */
239 } while (--i);
240 if (c[0] == n) { /* null input--all zero length codes */
241 *t = (huft_t *) NULL;
242 *m = 0;
243 return 0;
244 }
245
246 /* Find minimum and maximum length, bound *m by those */
247 l = *m;
248 for (j = 1; j <= BMAX; j++)
249 if (c[j])
250 break;
251 k = j; /* minimum code length */
252 if ((unsigned) l < j)
253 l = j;
254 for (i = BMAX; i; i--)
255 if (c[i])
256 break;
257 g = i; /* maximum code length */
258 if ((unsigned) l > i)
259 l = i;
260 *m = l;
261
262 /* Adjust last length count to fill out codes, if needed */
263 for (y = 1 << j; j < i; j++, y <<= 1)
264 if ((y -= c[j]) < 0)
265 return 2; /* bad input: more codes than bits */
266 if ((y -= c[i]) < 0)
267 return 2;
268 c[i] += y;
269
270 /* Generate starting offsets into the value table for each length */
271 x[1] = j = 0;
272 p = c + 1;
273 xp = x + 2;
274 while (--i) { /* note that i == g from above */
275 *xp++ = (j += *p++);
276 }
277
278 /* Make a table of values in order of bit lengths */
279 p = b;
280 i = 0;
281 do {
282 if ((j = *p++) != 0)
283 v[x[j]++] = i;
284 } while (++i < n);
285
286 /* Generate the Huffman codes and for each, make the table entries */
287 x[0] = i = 0; /* first Huffman code is zero */
288 p = v; /* grab values in bit order */
289 h = -1; /* no tables yet--level -1 */
290 w = -l; /* bits decoded == (l * h) */
291 u[0] = (huft_t *) NULL; /* just to keep compilers happy */
292 q = (huft_t *) NULL; /* ditto */
293 z = 0; /* ditto */
294
295 /* go through the bit lengths (k already is bits in shortest code) */
296 for (; k <= g; k++) {
297 a = c[k];
298 while (a--) {
299 /* here i is the Huffman code of length k bits for value *p */
300 /* make tables up to required level */
301 while (k > w + l) {
302 h++;
303 w += l; /* previous table always l bits */
304
305 /* compute minimum size table less than or equal to l bits */
306 z = (z = g - w) > (unsigned) l ? l : z; /* upper limit on table size */
307 if ((f = 1 << (j = k - w)) > a + 1) { /* try a k-w bit table *//* too few codes for k-w bit table */
308 f -= a + 1; /* deduct codes from patterns left */
309 xp = c + k;
310 while (++j < z) { /* try smaller tables up to z bits */
311 if ((f <<= 1) <= *++xp)
312 break; /* enough codes to use up j bits */
313 f -= *xp; /* else deduct codes from patterns */
314 }
315 }
316 z = 1 << j; /* table entries for j-bit table */
317
318 /* allocate and link in new table */
319 if ((q = (huft_t *) xmalloc((z + 1) * sizeof(huft_t))) == NULL) {
320 if (h) {
321 huft_free(u[0]);
322 }
323 return 3; /* not enough memory */
324 }
325 hufts += z + 1; /* track memory usage */
326 *t = q + 1; /* link to list for huft_free() */
327 *(t = &(q->v.t)) = NULL;
328 u[h] = ++q; /* table starts after link */
329
330 /* connect to last table, if there is one */
331 if (h) {
332 x[h] = i; /* save pattern for backing up */
333 r.b = (unsigned char) l; /* bits to dump before this table */
334 r.e = (unsigned char) (16 + j); /* bits in this table */
335 r.v.t = q; /* pointer to this table */
336 j = i >> (w - l); /* (get around Turbo C bug) */
337 u[h - 1][j] = r; /* connect to last table */
338 }
339 }
340
341 /* set up table entry in r */
342 r.b = (unsigned char) (k - w);
343 if (p >= v + n)
344 r.e = 99; /* out of values--invalid code */
345 else if (*p < s) {
346 r.e = (unsigned char) (*p < 256 ? 16 : 15); /* 256 is end-of-block code */
347 r.v.n = (unsigned short) (*p); /* simple code is just the value */
348 p++; /* one compiler does not like *p++ */
349 } else {
350 r.e = (unsigned char) e[*p - s]; /* non-simple--look up in lists */
351 r.v.n = d[*p++ - s];
352 }
353
354 /* fill code-like entries with r */
355 f = 1 << (k - w);
356 for (j = i >> w; j < z; j += f)
357 q[j] = r;
358
359 /* backwards increment the k-bit code i */
360 for (j = 1 << (k - 1); i & j; j >>= 1)
361 i ^= j;
362 i ^= j;
363
364 /* backup over finished tables */
365 while ((i & ((1 << w) - 1)) != x[h]) {
366 h--; /* don't need to update q */
367 w -= l;
368 }
369 }
370 }
371 /* Return true (1) if we were given an incomplete table */
372 return y != 0 && g != 1;
373}
374
375/*
376 * inflate (decompress) the codes in a deflated (compressed) block.
377 * Return an error code or zero if it all goes ok.
378 *
379 * tl, td: literal/length and distance decoder tables
380 * bl, bd: number of bits decoded by tl[] and td[]
381 */
382static int inflate_codes(huft_t *tl, huft_t *td, int bl, int bd)
383{
384 register unsigned long e; /* table entry flag/number of extra bits */
385 unsigned long n, d; /* length and index for copy */
386 unsigned long w; /* current window position */
387 huft_t *t; /* pointer to table entry */
388 unsigned ml, md; /* masks for bl and bd bits */
389 register unsigned long b; /* bit buffer */
390 register unsigned k; /* number of bits in bit buffer */
391
392 /* make local copies of globals */
393 b = bb; /* initialize bit buffer */
394 k = bk;
395 w = outcnt; /* initialize window position */
396
397 /* inflate the coded data */
398 ml = mask_bits[bl]; /* precompute masks for speed */
399 md = mask_bits[bd];
400 for (;;) { /* do until end of block */
401 while (k < (unsigned) bl) {
402 b |= ((unsigned long)fgetc(in_file)) << k;
403 k += 8;
404 }
405 if ((e = (t = tl + ((unsigned) b & ml))->e) > 16)
406 do {
407 if (e == 99) {
408 return 1;
409 }
410 b >>= t->b;
411 k -= t->b;
412 e -= 16;
413 while (k < e) {
414 b |= ((unsigned long)fgetc(in_file)) << k;
415 k += 8;
416 }
417 } while ((e = (t = t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16);
418 b >>= t->b;
419 k -= t->b;
420 if (e == 16) { /* then it's a literal */
421 window[w++] = (unsigned char) t->v.n;
422 if (w == WSIZE) {
423 outcnt=(w),
424 flush_window();
425 w = 0;
426 }
427 } else { /* it's an EOB or a length */
428
429 /* exit if end of block */
430 if (e == 15) {
431 break;
432 }
433
434 /* get length of block to copy */
435 while (k < e) {
436 b |= ((unsigned long)fgetc(in_file)) << k;
437 k += 8;
438 }
439 n = t->v.n + ((unsigned) b & mask_bits[e]);
440 b >>= e;
441 k -= e;
442
443 /* decode distance of block to copy */
444 while (k < (unsigned) bd) {
445 b |= ((unsigned long)fgetc(in_file)) << k;
446 k += 8;
447 }
448
449 if ((e = (t = td + ((unsigned) b & md))->e) > 16)
450 do {
451 if (e == 99)
452 return 1;
453 b >>= t->b;
454 k -= t->b;
455 e -= 16;
456 while (k < e) {
457 b |= ((unsigned long)fgetc(in_file)) << k;
458 k += 8;
459 }
460 } while ((e = (t = t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16);
461 b >>= t->b;
462 k -= t->b;
463 while (k < e) {
464 b |= ((unsigned long)fgetc(in_file)) << k;
465 k += 8;
466 }
467 d = w - t->v.n - ((unsigned) b & mask_bits[e]);
468 b >>= e;
469 k -= e;
470
471 /* do the copy */
472 do {
473 n -= (e = (e = WSIZE - ((d &= WSIZE - 1) > w ? d : w)) > n ? n : e);
474#if !defined(NOMEMCPY) && !defined(DEBUG)
475 if (w - d >= e) { /* (this test assumes unsigned comparison) */
476 memcpy(window + w, window + d, e);
477 w += e;
478 d += e;
479 } else /* do it slow to avoid memcpy() overlap */
480#endif /* !NOMEMCPY */
481 do {
482 window[w++] = window[d++];
483 } while (--e);
484 if (w == WSIZE) {
485 outcnt=(w),
486 flush_window();
487 w = 0;
488 }
489 } while (n);
490 }
491 }
492
493 /* restore the globals from the locals */
494 outcnt = w; /* restore global window pointer */
495 bb = b; /* restore global bit buffer */
496 bk = k;
497
498 /* done */
499 return 0;
500}
501
502/*
503 * decompress an inflated block
504 * e: last block flag
505 *
506 * GLOBAL VARIABLES: bb, kk,
507 */
508static int inflate_block(int *e)
509{
510 unsigned t; /* block type */
511 register unsigned long b; /* bit buffer */
512 register unsigned k; /* number of bits in bit buffer */
513 static unsigned short cplens[] = { /* Copy lengths for literal codes 257..285 */
514 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
515 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
516 };
517 /* note: see note #13 above about the 258 in this list. */
518 static unsigned short cplext[] = { /* Extra bits for literal codes 257..285 */
519 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
520 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99
521 }; /* 99==invalid */
522 static unsigned short cpdist[] = { /* Copy offsets for distance codes 0..29 */
523 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
524 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
525 8193, 12289, 16385, 24577
526 };
527 static unsigned short cpdext[] = { /* Extra bits for distance codes */
528 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
529 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
530 12, 12, 13, 13
531 };
532
533 /* make local bit buffer */
534 b = bb;
535 k = bk;
536
537 /* read in last block bit */
538 while (k < 1) {
539 b |= ((unsigned long)fgetc(in_file)) << k;
540 k += 8;
541 }
542 *e = (int) b & 1;
543 b >>= 1;
544 k -= 1;
545
546 /* read in block type */
547 while (k < 2) {
548 b |= ((unsigned long)fgetc(in_file)) << k;
549 k += 8;
550 }
551 t = (unsigned) b & 3;
552 b >>= 2;
553 k -= 2;
554
555 /* restore the global bit buffer */
556 bb = b;
557 bk = k;
558
559 /* inflate that block type */
560 switch (t) {
561 case 0: /* Inflate stored */
562 {
563 unsigned long n; /* number of bytes in block */
564 unsigned long w; /* current window position */
565 register unsigned long b_stored; /* bit buffer */
566 register unsigned long k_stored; /* number of bits in bit buffer */
567
568 /* make local copies of globals */
569 b_stored = bb; /* initialize bit buffer */
570 k_stored = bk;
571 w = outcnt; /* initialize window position */
572
573 /* go to byte boundary */
574 n = k_stored & 7;
575 b_stored >>= n;
576 k_stored -= n;
577
578 /* get the length and its complement */
579 while (k_stored < 16) {
580 b_stored |= ((unsigned long)fgetc(in_file)) << k_stored;
581 k_stored += 8;
582 }
583 n = ((unsigned) b_stored & 0xffff);
584 b_stored >>= 16;
585 k_stored -= 16;
586 while (k_stored < 16) {
587 b_stored |= ((unsigned long)fgetc(in_file)) << k_stored;
588 k_stored += 8;
589 }
590 if (n != (unsigned) ((~b_stored) & 0xffff)) {
591 return 1; /* error in compressed data */
592 }
593 b_stored >>= 16;
594 k_stored -= 16;
595
596 /* read and output the compressed data */
597 while (n--) {
598 while (k_stored < 8) {
599 b_stored |= ((unsigned long)fgetc(in_file)) << k_stored;
600 k_stored += 8;
601 }
602 window[w++] = (unsigned char) b_stored;
603 if (w == (unsigned long)WSIZE) {
604 outcnt=(w),
605 flush_window();
606 w = 0;
607 }
608 b_stored >>= 8;
609 k_stored -= 8;
610 }
611
612 /* restore the globals from the locals */
613 outcnt = w; /* restore global window pointer */
614 bb = b_stored; /* restore global bit buffer */
615 bk = k_stored;
616 return 0;
617 }
618 case 1: /* Inflate fixed
619 * decompress an inflated type 1 (fixed Huffman codes) block. We should
620 * either replace this with a custom decoder, or at least precompute the
621 * Huffman tables.
622 */
623 {
624 int i; /* temporary variable */
625 huft_t *tl; /* literal/length code table */
626 huft_t *td; /* distance code table */
627 int bl; /* lookup bits for tl */
628 int bd; /* lookup bits for td */
629 unsigned int l[288]; /* length list for huft_build */
630
631 /* set up literal table */
632 for (i = 0; i < 144; i++) {
633 l[i] = 8;
634 }
635 for (; i < 256; i++) {
636 l[i] = 9;
637 }
638 for (; i < 280; i++) {
639 l[i] = 7;
640 }
641 for (; i < 288; i++) { /* make a complete, but wrong code set */
642 l[i] = 8;
643 }
644 bl = 7;
645 if ((i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl)) != 0) {
646 return i;
647 }
648
649 /* set up distance table */
650 for (i = 0; i < 30; i++) { /* make an incomplete code set */
651 l[i] = 5;
652 }
653 bd = 5;
654 if ((i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd)) > 1) {
655 huft_free(tl);
656 return i;
657 }
658
659 /* decompress until an end-of-block code */
660 if (inflate_codes(tl, td, bl, bd))
661 return 1;
662
663 /* free the decoding tables, return */
664 huft_free(tl);
665 huft_free(td);
666 return 0;
667 }
668 case 2: /* Inflate dynamic */
669 {
670 /* Tables for deflate from PKZIP's appnote.txt. */
671 static unsigned border[] = { /* Order of the bit length code lengths */
672 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
673 };
674 int dbits = 6; /* bits in base distance lookup table */
675 int lbits = 9; /* bits in base literal/length lookup table */
676
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;
702 }
703 nl = 257 + ((unsigned) b_dynamic & 0x1f); /* number of literal/length codes */
704 b_dynamic >>= 5;
705 k_dynamic -= 5;
706 while (k_dynamic < 5) {
707 b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic;
708 k_dynamic += 8;
709 }
710 nd = 1 + ((unsigned) b_dynamic & 0x1f); /* number of distance codes */
711 b_dynamic >>= 5;
712 k_dynamic -= 5;
713 while (k_dynamic < 4) {
714 b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic;
715 k_dynamic += 8;
716 }
717 nb = 4 + ((unsigned) b_dynamic & 0xf); /* number of bit length codes */
718 b_dynamic >>= 4;
719 k_dynamic -= 4;
720 if (nl > 286 || nd > 30) {
721 return 1; /* bad lengths */
722 }
723
724 /* read in bit-length-code lengths */
725 for (j = 0; j < nb; j++) {
726 while (k_dynamic < 3) {
727 b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic;
728 k_dynamic += 8;
729 }
730 ll[border[j]] = (unsigned) b_dynamic & 7;
731 b_dynamic >>= 3;
732 k_dynamic -= 3;
733 }
734 for (; j < 19; j++) {
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 }
744 return i; /* incomplete code set */
745 }
746
747 /* read in literal and distance code lengths */
748 n = nl + nd;
749 m = mask_bits[bl];
750 i = l = 0;
751 while ((unsigned) i < n) {
752 while (k_dynamic < (unsigned) bl) {
753 b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic;
754 k_dynamic += 8;
755 }
756 j = (td = tl + ((unsigned) b_dynamic & m))->b;
757 b_dynamic >>= j;
758 k_dynamic -= j;
759 j = td->v.n;
760 if (j < 16) { /* length of code in bits (0..15) */
761 ll[i++] = l = j; /* save last length in l */
762 }
763 else if (j == 16) { /* repeat last length 3 to 6 times */
764 while (k_dynamic < 2) {
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 }
808 }
809
810 /* free decoding table for trees */
811 huft_free(tl);
812
813 /* restore the global bit buffer */
814 bb = b_dynamic;
815 bk = k_dynamic;
816
817 /* build the decoding tables for literal/length and distance codes */
818 bl = lbits;
819 if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0) {
820 if (i == 1) {
821 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);
833 return i; /* incomplete code set */
834 }
835
836 /* decompress until an end-of-block code */
837 if (inflate_codes(tl, td, bl, bd))
838 return 1;
839
840 /* free the decoding tables, return */
841 huft_free(tl);
842 huft_free(td);
843 return 0;
844 }
845 default:
846 /* bad block type */
847 return 2;
848 }
849}
850
851/*
852 * decompress an inflated entry
853 *
854 * GLOBAL VARIABLES: outcnt, bk, bb, hufts, inptr
855 */
856static int inflate()
857{
858 int e; /* last block flag */
859 int r; /* result code */
860 unsigned h = 0; /* maximum struct huft's malloc'ed */
861
862 /* initialize window, bit buffer */
863 outcnt = 0;
864 bk = 0;
865 bb = 0;
866
867 /* decompress until the last block */
868 do {
869 hufts = 0;
870 if ((r = inflate_block(&e)) != 0) {
871 return r;
872 }
873 if (hufts > h) {
874 h = hufts;
875 }
876 } while (!e);
877
878 /* flush out window */
879 flush_window();
880
881 /* return success */
882 return 0;
883}
884
885/* ===========================================================================
886 * Unzip in to out. This routine works on both gzip and pkzip files.
887 *
888 * IN assertions: the buffer inbuf contains already the beginning of
889 * the compressed data, from offsets inptr to insize-1 included.
890 * The magic header has already been checked. The output buffer is cleared.
891 * in, out: input and output file descriptors
892 */
893extern int unzip(FILE *l_in_file, FILE *l_out_file)
894{
895 const int extra_field = 0x04; /* bit 2 set: extra field present */
896 const int orig_name = 0x08; /* bit 3 set: original file name present */
897 const int comment = 0x10; /* bit 4 set: file comment present */
898 unsigned char buf[8]; /* extended local header */
899 unsigned char flags; /* compression flags */
900 char magic[2]; /* magic header */
901 int method;
902 typedef void (*sig_type) (int);
903 int exit_code=0; /* program exit code */
Matt Kraaib1810562001-04-18 14:49:55 +0000904 int i;
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000905
906 in_file = l_in_file;
907 out_file = l_out_file;
908
909 if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
910 (void) signal(SIGINT, (sig_type) abort_gzip);
911 }
912#ifdef SIGTERM
Glenn L McGrathf70f6ce2001-04-11 15:09:30 +0000913// if (signal(SIGTERM, SIG_IGN) != SIG_IGN) {
914// (void) signal(SIGTERM, (sig_type) abort_gzip);
915// }
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000916#endif
917#ifdef SIGHUP
918 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
919 (void) signal(SIGHUP, (sig_type) abort_gzip);
920 }
921#endif
922
923 /* Allocate all global buffers (for DYN_ALLOC option) */
924 window = xmalloc((size_t)(((2L*WSIZE)+1L)*sizeof(unsigned char)));
925 outcnt = 0;
926 bytes_out = 0L;
927
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000928 magic[0] = fgetc(in_file);
929 magic[1] = fgetc(in_file);
930
931 /* Magic header for gzip files, 1F 8B = \037\213 */
932 if (memcmp(magic, "\037\213", 2) != 0) {
933 error_msg("Invalid gzip magic");
934 return EXIT_FAILURE;
935 }
936
937 method = (int) fgetc(in_file);
938 if (method != 8) {
939 error_msg("unknown method %d -- get newer version of gzip", method);
940 exit_code = 1;
941 return -1;
942 }
943
944 flags = (unsigned char) fgetc(in_file);
945
946 /* Ignore time stamp(4), extra flags(1), OS type(1) */
Matt Kraaib1810562001-04-18 14:49:55 +0000947 for (i = 0; i < 6; i++)
948 fgetc(in_file);
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000949
950 if ((flags & extra_field) != 0) {
Matt Kraaib1810562001-04-18 14:49:55 +0000951 size_t extra;
952 extra = fgetc(in_file);
953 extra += fgetc(in_file) << 8;
954
955 for (i = 0; i < extra; i++)
956 fgetc(in_file);
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000957 }
958
959 /* Discard original name if any */
960 if ((flags & orig_name) != 0) {
961 while (fgetc(in_file) != 0); /* null */
962 }
963
964 /* Discard file comment if any */
965 if ((flags & comment) != 0) {
966 while (fgetc(in_file) != 0); /* null */
967 }
968
969 if (method < 0) {
970 printf("it failed\n");
971 return(exit_code); /* error message already emitted */
972 }
973
974 make_crc_table();
975
976 /* Decompress */
977 if (method == 8) {
978
979 int res = inflate();
980
981 if (res == 3) {
982 error_msg(memory_exhausted);
983 } else if (res != 0) {
984 error_msg("invalid compressed data--format violated");
985 }
986
987 } else {
988 error_msg("internal error, invalid method");
989 }
990
991 /* Get the crc and original length
992 * crc32 (see algorithm.doc)
993 * uncompressed input size modulo 2^32
994 */
995 fread(buf, 1, 8, in_file);
996
997 /* Validate decompression - crc */
Eric Andersen0d8cc162001-06-27 06:15:50 +0000998 if ((unsigned int)((buf[0] | (buf[1] << 8)) |((buf[2] | (buf[3] << 8)) << 16)) != (crc ^ 0xffffffffL)) {
Glenn L McGrath7fd92942001-04-11 03:11:33 +0000999 error_msg("invalid compressed data--crc error");
1000 }
1001 /* Validate decompression - size */
1002 if (((buf[4] | (buf[5] << 8)) |((buf[6] | (buf[7] << 8)) << 16)) != (unsigned long) bytes_out) {
1003 error_msg("invalid compressed data--length error");
1004 }
1005
1006 free(window);
1007 free(crc_table);
1008
1009 return 0;
1010}
1011
1012/*
1013 * This needs access to global variables wondow and crc_table, so its not in its own file.
1014 */
1015extern void gz_close(int gunzip_pid)
1016{
1017 if (kill(gunzip_pid, SIGTERM) == -1) {
1018 error_msg_and_die("*** Couldnt kill old gunzip process *** aborting");
1019 }
1020
1021 if (waitpid(gunzip_pid, NULL, 0) == -1) {
1022 printf("Couldnt wait ?");
1023 }
Glenn L McGrath93febe62001-07-11 07:25:01 +00001024 free(window);
Glenn L McGrath93febe62001-07-11 07:25:01 +00001025 free(crc_table);
Glenn L McGrath7fd92942001-04-11 03:11:33 +00001026}