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