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