1 /* vi: set sw=4 ts=4: */
3 * gunzip implementation for busybox
5 * Based on GNU gzip v1.2.4 Copyright (C) 1992-1993 Jean-loup Gailly.
7 * Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de>
8 * based on gzip sources
10 * Adjusted further by Erik Andersen <andersee@debian.org> to support
11 * files as well as stdin/stdout, and to generally behave itself wrt
12 * command line handling.
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>
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.
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.
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
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.
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.
44 static 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.",
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.",
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.",
63 #include <sys/types.h>
70 static FILE *in_file, *out_file;
72 /* these are freed by gz_close */
73 static unsigned char *window;
74 static unsigned long *crc_table;
76 static unsigned long crc = 0xffffffffL; /* shift register contents */
78 /* Return codes from gzip */
79 static const int ERROR = 1;
82 * window size--must be a power of two, and
83 * at least 32K for zip's deflate method
85 static const int WSIZE = 0x8000;
87 /* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
88 static const int BMAX = 16; /* maximum bit length of any code (16 for explode) */
89 static const int N_MAX = 288; /* maximum number of codes in any set */
91 static long bytes_out; /* number of output bytes */
92 static unsigned long outcnt; /* bytes in output buffer */
94 static unsigned hufts; /* track memory usage */
95 static unsigned long bb; /* bit buffer */
96 static unsigned bk; /* bits in bit buffer */
98 typedef 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 */
102 unsigned short n; /* literal, length base, or distance base */
103 struct huft_s *t; /* pointer to next level of table */
107 static const unsigned short mask_bits[] = {
109 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
110 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
113 //static int error_number = 0;
114 /* ========================================================================
115 * Signal and error handler.
118 static void abort_gzip(void)
120 error_msg("gzip aborted\n");
124 static void make_crc_table(void)
126 const unsigned long poly = 0xedb88320; /* polynomial exclusive-or pattern */
127 unsigned short i; /* counter for all possible eight bit values */
129 crc_table = (unsigned long *) malloc(256 * sizeof(unsigned long));
131 /* Compute and print table of CRC's, five per line */
132 for (i = 0; i < 256; i++) {
133 unsigned long table_entry; /* crc shift register */
134 char k; /* byte being shifted into crc apparatus */
137 /* The idea to initialize the register with the byte instead of
138 * zero was stolen from Haruhiko Okumura's ar002
140 for (k = 8; k; k--) {
141 table_entry = table_entry & 1 ? (table_entry >> 1) ^ poly : table_entry >> 1;
143 crc_table[i]=table_entry;
147 /* ===========================================================================
148 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
149 * (Used for the decompressed data only.)
151 static void flush_window(void)
158 for (n = 0; n < outcnt; n++) {
159 crc = crc_table[((int) crc ^ (window[n])) & 0xff] ^ (crc >> 8);
162 if (fwrite(window, 1, outcnt, out_file) != outcnt) {
163 error_msg_and_die("Couldnt write");
165 bytes_out += (unsigned long) outcnt;
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
175 static int huft_free(huft_t *t)
179 /* Go through linked list, freeing from the malloced (t[-1]) address. */
181 while (p != (huft_t *) NULL) {
189 typedef unsigned char extra_bits_t;
191 /* 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.
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
205 static int huft_build(unsigned int *b, const unsigned int n, const unsigned int s,
206 const unsigned short *d, const extra_bits_t *e, huft_t **t, int *m)
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 */
228 /* Generate counts for each bit length */
229 memset ((void *)(c), 0, sizeof(c));
233 c[*p]++; /* assume all entries <= BMAX */
234 p++; /* Can't combine with above line (Solaris bug) */
236 if (c[0] == n) { /* null input--all zero length codes */
237 *t = (huft_t *) NULL;
242 /* Find minimum and maximum length, bound *m by those */
244 for (j = 1; j <= BMAX; j++)
247 k = j; /* minimum code length */
248 if ((unsigned) l < j)
250 for (i = BMAX; i; i--)
253 g = i; /* maximum code length */
254 if ((unsigned) l > i)
258 /* Adjust last length count to fill out codes, if needed */
259 for (y = 1 << j; j < i; j++, y <<= 1)
261 return 2; /* bad input: more codes than bits */
266 /* Generate starting offsets into the value table for each length */
270 while (--i) { /* note that i == g from above */
274 /* Make a table of values in order of bit lengths */
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 */
291 /* go through the bit lengths (k already is bits in shortest code) */
292 for (; k <= g; k++) {
295 /* here i is the Huffman code of length k bits for value *p */
296 /* make tables up to required level */
299 w += l; /* previous table always l bits */
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 */
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 */
312 z = 1 << j; /* table entries for j-bit table */
314 /* allocate and link in new table */
315 q = (huft_t *) xmalloc((z + 1) * sizeof(huft_t));
317 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 */
322 /* connect to last table, if there is one */
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 */
333 /* set up table entry in r */
334 r.b = (unsigned char) (k - w);
336 r.e = 99; /* out of values--invalid code */
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++ */
342 r.e = (unsigned char) e[*p - s]; /* non-simple--look up in lists */
346 /* fill code-like entries with r */
348 for (j = i >> w; j < z; j += f)
351 /* backwards increment the k-bit code i */
352 for (j = 1 << (k - 1); i & j; j >>= 1)
356 /* backup over finished tables */
357 while ((i & ((1 << w) - 1)) != x[h]) {
358 h--; /* don't need to update q */
363 /* Return true (1) if we were given an incomplete table */
364 return y != 0 && g != 1;
368 * inflate (decompress) the codes in a deflated (compressed) block.
369 * Return an error code or zero if it all goes ok.
371 * tl, td: literal/length and distance decoder tables
372 * bl, bd: number of bits decoded by tl[] and td[]
374 static int inflate_codes(huft_t *tl, huft_t *td, int bl, int bd)
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 */
384 /* make local copies of globals */
385 b = bb; /* initialize bit buffer */
387 w = outcnt; /* initialize window position */
389 /* inflate the coded data */
390 ml = mask_bits[bl]; /* precompute masks for speed */
392 for (;;) { /* do until end of block */
393 while (k < (unsigned) bl) {
394 b |= ((unsigned long)fgetc(in_file)) << k;
397 if ((e = (t = tl + ((unsigned) b & ml))->e) > 16)
406 b |= ((unsigned long)fgetc(in_file)) << k;
409 } while ((e = (t = t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16);
412 if (e == 16) { /* then it's a literal */
413 window[w++] = (unsigned char) t->v.n;
419 } else { /* it's an EOB or a length */
421 /* exit if end of block */
426 /* get length of block to copy */
428 b |= ((unsigned long)fgetc(in_file)) << k;
431 n = t->v.n + ((unsigned) b & mask_bits[e]);
435 /* decode distance of block to copy */
436 while (k < (unsigned) bd) {
437 b |= ((unsigned long)fgetc(in_file)) << k;
441 if ((e = (t = td + ((unsigned) b & md))->e) > 16)
449 b |= ((unsigned long)fgetc(in_file)) << k;
452 } while ((e = (t = t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16);
456 b |= ((unsigned long)fgetc(in_file)) << k;
459 d = w - t->v.n - ((unsigned) b & mask_bits[e]);
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);
471 } else /* do it slow to avoid memcpy() overlap */
472 #endif /* !NOMEMCPY */
474 window[w++] = window[d++];
485 /* restore the globals from the locals */
486 outcnt = w; /* restore global window pointer */
487 bb = b; /* restore global bit buffer */
494 static 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
498 /* note: see note #13 above about the 258 in this list. */
499 static 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
503 static 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
508 static 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,
513 /* Tables for deflate from PKZIP's appnote.txt. */
514 static 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
519 * decompress an inflated block
522 * GLOBAL VARIABLES: bb, kk,
524 static int inflate_block(int *e)
526 unsigned t; /* block type */
527 register unsigned long b; /* bit buffer */
528 register unsigned k; /* number of bits in bit buffer */
530 /* make local bit buffer */
534 /* read in last block bit */
536 b |= ((unsigned long)fgetc(in_file)) << k;
543 /* read in block type */
545 b |= ((unsigned long)fgetc(in_file)) << k;
548 t = (unsigned) b & 3;
552 /* restore the global bit buffer */
556 /* inflate that block type */
558 case 0: /* Inflate stored */
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 */
565 /* make local copies of globals */
566 b_stored = bb; /* initialize bit buffer */
568 w = outcnt; /* initialize window position */
570 /* go to byte boundary */
575 /* get the length and its complement */
576 while (k_stored < 16) {
577 b_stored |= ((unsigned long)fgetc(in_file)) << k_stored;
580 n = ((unsigned) b_stored & 0xffff);
583 while (k_stored < 16) {
584 b_stored |= ((unsigned long)fgetc(in_file)) << k_stored;
587 if (n != (unsigned) ((~b_stored) & 0xffff)) {
588 return 1; /* error in compressed data */
593 /* read and output the compressed data */
595 while (k_stored < 8) {
596 b_stored |= ((unsigned long)fgetc(in_file)) << k_stored;
599 window[w++] = (unsigned char) b_stored;
600 if (w == (unsigned long)WSIZE) {
609 /* restore the globals from the locals */
610 outcnt = w; /* restore global window pointer */
611 bb = b_stored; /* restore global bit buffer */
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
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 */
628 /* set up literal table */
629 for (i = 0; i < 144; i++) {
632 for (; i < 256; i++) {
635 for (; i < 280; i++) {
638 for (; i < 288; i++) { /* make a complete, but wrong code set */
642 if ((i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl)) != 0) {
646 /* set up distance table */
647 for (i = 0; i < 30; i++) { /* make an incomplete code set */
651 if ((i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd)) > 1) {
656 /* decompress until an end-of-block code */
657 if (inflate_codes(tl, td, bl, bd))
660 /* free the decoding tables, return */
665 case 2: /* Inflate dynamic */
667 const int dbits = 6; /* bits in base distance lookup table */
668 const int lbits = 9; /* bits in base literal/length lookup table */
670 int i; /* temporary variables */
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 */
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 */
687 /* make local bit buffer */
691 /* read in table lengths */
692 while (k_dynamic < 5) {
693 b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic;
696 nl = 257 + ((unsigned) b_dynamic & 0x1f); /* number of literal/length codes */
699 while (k_dynamic < 5) {
700 b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic;
703 nd = 1 + ((unsigned) b_dynamic & 0x1f); /* number of distance codes */
706 while (k_dynamic < 4) {
707 b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic;
710 nb = 4 + ((unsigned) b_dynamic & 0xf); /* number of bit length codes */
713 if (nl > 286 || nd > 30) {
714 return 1; /* bad lengths */
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;
723 ll[border[j]] = (unsigned) b_dynamic & 7;
727 for (; j < 19; j++) {
731 /* build decoding table for trees--single level, 7 bit lookup */
733 if ((i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl)) != 0) {
737 return i; /* incomplete code set */
740 /* read in literal and distance code lengths */
744 while ((unsigned) i < n) {
745 while (k_dynamic < (unsigned) bl) {
746 b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic;
749 j = (td = tl + ((unsigned) b_dynamic & m))->b;
753 if (j < 16) { /* length of code in bits (0..15) */
754 ll[i++] = l = j; /* save last length in l */
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;
761 j = 3 + ((unsigned) b_dynamic & 3);
764 if ((unsigned) i + j > n) {
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;
775 j = 3 + ((unsigned) b_dynamic & 7);
778 if ((unsigned) i + j > n) {
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;
790 j = 11 + ((unsigned) b_dynamic & 0x7f);
793 if ((unsigned) i + j > n) {
803 /* free decoding table for trees */
806 /* restore the global bit buffer */
810 /* build the decoding tables for literal/length and distance codes */
812 if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0) {
814 error_msg("Incomplete literal tree");
817 return i; /* incomplete code set */
820 if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0) {
822 error_msg("incomplete distance tree");
826 return i; /* incomplete code set */
829 /* decompress until an end-of-block code */
830 if (inflate_codes(tl, td, bl, bd))
833 /* free the decoding tables, return */
845 * decompress an inflated entry
847 * GLOBAL VARIABLES: outcnt, bk, bb, hufts, inptr
849 extern int inflate(FILE *in, FILE *out)
851 int e; /* last block flag */
852 int r; /* result code */
853 unsigned h = 0; /* maximum struct huft's malloc'ed */
855 /* initialize window, bit buffer */
863 /* Allocate all global buffers (for DYN_ALLOC option) */
864 window = xmalloc((size_t)(((2L*WSIZE)+1L)*sizeof(unsigned char)));
867 /* Create the crc table */
870 /* decompress until the last block */
873 if ((r = inflate_block(&e)) != 0) {
881 /* Undo too much lookahead. The next read will be byte aligned so we
882 * can discard unused bits in the last meaningful byte.
886 ungetc((bb << bk), in_file);
889 /* flush out window */
898 /* ===========================================================================
899 * Unzip in to out. This routine works on gzip files only.
901 * IN assertions: the buffer inbuf contains already the beginning of
902 * the compressed data, from offsets inptr to insize-1 included.
903 * The magic header has already been checked. The output buffer is cleared.
904 * in, out: input and output file descriptors
906 extern int unzip(FILE *l_in_file, FILE *l_out_file)
908 unsigned char buf[8]; /* extended local header */
909 unsigned char flags; /* compression flags */
910 typedef void (*sig_type) (int);
913 if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
914 (void) signal(SIGINT, (sig_type) abort_gzip);
917 // if (signal(SIGTERM, SIG_IGN) != SIG_IGN) {
918 // (void) signal(SIGTERM, (sig_type) abort_gzip);
922 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
923 (void) signal(SIGHUP, (sig_type) abort_gzip);
927 /* Magic header for gzip files, 1F 8B = \037\213 */
928 if ((fgetc(l_in_file) != 0x1F) || (fgetc(l_in_file) != 0x8b)) {
929 error_msg("Invalid gzip magic");
933 /* Check the compression method */
934 if (fgetc(l_in_file) != 8) {
935 error_msg("Unknown compression method");
939 flags = (unsigned char) fgetc(l_in_file);
941 /* Ignore time stamp(4), extra flags(1), OS type(1) */
942 for (i = 0; i < 6; i++) {
947 /* bit 2 set: extra field present */
948 const unsigned short extra = fgetc(l_in_file) + (fgetc(l_in_file) << 8);
950 for (i = 0; i < extra; i++) {
955 /* Discard original name if any */
957 /* bit 3 set: original file name present */
958 while (fgetc(l_in_file) != 0); /* null */
961 /* Discard file comment if any */
963 /* bit 4 set: file comment present */
964 while (fgetc(l_in_file) != 0); /* null */
968 if (inflate(l_in_file, l_out_file) != 0) {
969 error_msg("invalid compressed data--format violated");
972 /* Get the crc and original length
973 * crc32 (see algorithm.doc)
974 * uncompressed input size modulo 2^32
976 fread(buf, 1, 8, l_in_file);
978 /* Validate decompression - crc */
979 if ((unsigned int)((buf[0] | (buf[1] << 8)) |((buf[2] | (buf[3] << 8)) << 16)) != (crc ^ 0xffffffffL)) {
980 error_msg("invalid compressed data--crc error");
982 /* Validate decompression - size */
983 if (((buf[4] | (buf[5] << 8)) |((buf[6] | (buf[7] << 8)) << 16)) != (unsigned long) bytes_out) {
984 error_msg("invalid compressed data--length error");
991 * This needs access to global variables window and crc_table, so its not in its own file.
993 extern void gz_close(int gunzip_pid)
995 if (kill(gunzip_pid, SIGTERM) == -1) {
996 error_msg_and_die("*** Couldnt kill old gunzip process *** aborting");
999 if (waitpid(gunzip_pid, NULL, 0) == -1) {
1000 printf("Couldnt wait ?");