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>
71 #ifdef CONFIG_FEATURE_UNCOMPRESS
72 int uncompress(FILE * in, FILE * out);
75 static FILE *in_file, *out_file;
77 /* these are freed by gz_close */
78 static unsigned char *window;
79 static unsigned long *crc_table;
81 static unsigned long crc; /* shift register contents */
83 /* Return codes from gzip */
87 * window size--must be a power of two, and
88 * at least 32K for zip's deflate method
92 /* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
93 #define BMAX 16 /* maximum bit length of any code (16 for explode) */
94 #define N_MAX 288 /* maximum number of codes in any set */
96 static long bytes_out; /* number of output bytes */
97 static unsigned long outcnt; /* bytes in output buffer */
99 static unsigned hufts; /* track memory usage */
100 static unsigned long bb; /* bit buffer */
101 static unsigned bk; /* bits in bit buffer */
103 typedef struct huft_s {
104 unsigned char e; /* number of extra bits or operation */
105 unsigned char b; /* number of bits in this code or subcode */
107 unsigned short n; /* literal, length base, or distance base */
108 struct huft_s *t; /* pointer to next level of table */
112 static const unsigned short mask_bits[] = {
114 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
115 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
118 /* static int error_number = 0; */
119 /* ========================================================================
120 * Signal and error handler.
123 static void abort_gzip(void)
125 error_msg("gzip aborted\n");
129 static void make_crc_table(void)
131 const unsigned long poly = 0xedb88320; /* polynomial exclusive-or pattern */
132 unsigned short i; /* counter for all possible eight bit values */
134 /* initial shift register value */
136 crc_table = (unsigned long *) malloc(256 * sizeof(unsigned long));
138 /* Compute and print table of CRC's, five per line */
139 for (i = 0; i < 256; i++) {
140 unsigned long table_entry; /* crc shift register */
141 char k; /* byte being shifted into crc apparatus */
144 /* The idea to initialize the register with the byte instead of
145 * zero was stolen from Haruhiko Okumura's ar002
147 for (k = 8; k; k--) {
149 table_entry & 1 ? (table_entry >> 1) ^ poly : table_entry >>
152 crc_table[i] = table_entry;
156 /* ===========================================================================
157 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
158 * (Used for the decompressed data only.)
160 static void flush_window(void)
167 for (n = 0; n < outcnt; n++) {
168 crc = crc_table[((int) crc ^ (window[n])) & 0xff] ^ (crc >> 8);
171 if (fwrite(window, 1, outcnt, out_file) != outcnt) {
172 error_msg_and_die("Couldnt write");
174 bytes_out += (unsigned long) outcnt;
179 * Free the malloc'ed tables built by huft_build(), which makes a linked
180 * list of the tables it made, with the links in a dummy first entry of
184 static int huft_free(huft_t * t)
188 /* Go through linked list, freeing from the malloced (t[-1]) address. */
190 while (p != (huft_t *) NULL) {
198 typedef unsigned char extra_bits_t;
200 /* Given a list of code lengths and a maximum table size, make a set of
201 * tables to decode that set of codes. Return zero on success, one if
202 * the given code set is incomplete (the tables are still built in this
203 * case), two if the input is invalid (all zero length codes or an
204 * oversubscribed set of lengths), and three if not enough memory.
206 * b: code lengths in bits (all assumed <= BMAX)
207 * n: number of codes (assumed <= N_MAX)
208 * s: number of simple-valued codes (0..s-1)
209 * d: list of base values for non-simple codes
210 * e: list of extra bits for non-simple codes
211 * t: result: starting table
212 * m: maximum lookup bits, returns actual
214 static int huft_build(unsigned int *b, const unsigned int n,
215 const unsigned int s, const unsigned short *d,
216 const extra_bits_t * e, huft_t ** t, int *m)
218 unsigned a; /* counter for codes of length k */
219 unsigned c[BMAX + 1]; /* bit length count table */
220 unsigned f; /* i repeats in table every f entries */
221 int g; /* maximum code length */
222 int h; /* table level */
223 register unsigned i; /* counter, current code */
224 register unsigned j; /* counter */
225 register int k; /* number of bits in current code */
226 int l; /* bits per table (returned in m) */
227 register unsigned *p; /* pointer into c[], b[], or v[] */
228 register huft_t *q; /* points to current table */
229 huft_t r; /* table entry for structure assignment */
230 huft_t *u[BMAX]; /* table stack */
231 unsigned v[N_MAX]; /* values in order of bit length */
232 register int w; /* bits before this table == (l * h) */
233 unsigned x[BMAX + 1]; /* bit offsets, then code stack */
234 unsigned *xp; /* pointer into x */
235 int y; /* number of dummy codes added */
236 unsigned z; /* number of entries in current table */
238 /* Generate counts for each bit length */
239 memset((void *) (c), 0, sizeof(c));
243 c[*p]++; /* assume all entries <= BMAX */
244 p++; /* Can't combine with above line (Solaris bug) */
246 if (c[0] == n) { /* null input--all zero length codes */
247 *t = (huft_t *) NULL;
252 /* Find minimum and maximum length, bound *m by those */
254 for (j = 1; j <= BMAX; j++)
257 k = j; /* minimum code length */
258 if ((unsigned) l < j)
260 for (i = BMAX; i; i--)
263 g = i; /* maximum code length */
264 if ((unsigned) l > i)
268 /* Adjust last length count to fill out codes, if needed */
269 for (y = 1 << j; j < i; j++, y <<= 1)
271 return 2; /* bad input: more codes than bits */
276 /* Generate starting offsets into the value table for each length */
280 while (--i) { /* note that i == g from above */
284 /* Make a table of values in order of bit lengths */
292 /* Generate the Huffman codes and for each, make the table entries */
293 x[0] = i = 0; /* first Huffman code is zero */
294 p = v; /* grab values in bit order */
295 h = -1; /* no tables yet--level -1 */
296 w = -l; /* bits decoded == (l * h) */
297 u[0] = (huft_t *) NULL; /* just to keep compilers happy */
298 q = (huft_t *) NULL; /* ditto */
301 /* go through the bit lengths (k already is bits in shortest code) */
302 for (; k <= g; k++) {
305 /* here i is the Huffman code of length k bits for value *p */
306 /* make tables up to required level */
309 w += l; /* previous table always l bits */
311 /* compute minimum size table less than or equal to l bits */
312 z = (z = g - w) > (unsigned) l ? l : z; /* upper limit on table size */
313 if ((f = 1 << (j = k - w)) > a + 1) { /* try a k-w bit table *//* too few codes for k-w bit table */
314 f -= a + 1; /* deduct codes from patterns left */
316 while (++j < z) { /* try smaller tables up to z bits */
317 if ((f <<= 1) <= *++xp)
318 break; /* enough codes to use up j bits */
319 f -= *xp; /* else deduct codes from patterns */
322 z = 1 << j; /* table entries for j-bit table */
324 /* allocate and link in new table */
325 q = (huft_t *) xmalloc((z + 1) * sizeof(huft_t));
327 hufts += z + 1; /* track memory usage */
328 *t = q + 1; /* link to list for huft_free() */
329 *(t = &(q->v.t)) = NULL;
330 u[h] = ++q; /* table starts after link */
332 /* connect to last table, if there is one */
334 x[h] = i; /* save pattern for backing up */
335 r.b = (unsigned char) l; /* bits to dump before this table */
336 r.e = (unsigned char) (16 + j); /* bits in this table */
337 r.v.t = q; /* pointer to this table */
338 j = i >> (w - l); /* (get around Turbo C bug) */
339 u[h - 1][j] = r; /* connect to last table */
343 /* set up table entry in r */
344 r.b = (unsigned char) (k - w);
346 r.e = 99; /* out of values--invalid code */
348 r.e = (unsigned char) (*p < 256 ? 16 : 15); /* 256 is end-of-block code */
349 r.v.n = (unsigned short) (*p); /* simple code is just the value */
350 p++; /* one compiler does not like *p++ */
352 r.e = (unsigned char) e[*p - s]; /* non-simple--look up in lists */
356 /* fill code-like entries with r */
358 for (j = i >> w; j < z; j += f)
361 /* backwards increment the k-bit code i */
362 for (j = 1 << (k - 1); i & j; j >>= 1)
366 /* backup over finished tables */
367 while ((i & ((1 << w) - 1)) != x[h]) {
368 h--; /* don't need to update q */
373 /* Return true (1) if we were given an incomplete table */
374 return y != 0 && g != 1;
378 * inflate (decompress) the codes in a deflated (compressed) block.
379 * Return an error code or zero if it all goes ok.
381 * tl, td: literal/length and distance decoder tables
382 * bl, bd: number of bits decoded by tl[] and td[]
384 static int inflate_codes(huft_t * tl, huft_t * td, int bl, int bd)
386 register unsigned long e; /* table entry flag/number of extra bits */
387 unsigned long n, d; /* length and index for copy */
388 unsigned long w; /* current window position */
389 huft_t *t; /* pointer to table entry */
390 unsigned ml, md; /* masks for bl and bd bits */
391 register unsigned long b; /* bit buffer */
392 register unsigned k; /* number of bits in bit buffer */
393 register int input_char;
395 /* make local copies of globals */
396 b = bb; /* initialize bit buffer */
398 w = outcnt; /* initialize window position */
400 /* inflate the coded data */
401 ml = mask_bits[bl]; /* precompute masks for speed */
403 for (;;) { /* do until end of block */
404 while (k < (unsigned) bl) {
405 input_char = fgetc(in_file);
406 if (input_char == EOF) return 1;
407 b |= ((unsigned long)input_char) << k;
410 if ((e = (t = tl + ((unsigned) b & ml))->e) > 16)
419 input_char = fgetc(in_file);
420 if (input_char == EOF) return 1;
421 b |= ((unsigned long)input_char) << k;
425 (t = t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16);
428 if (e == 16) { /* then it's a literal */
429 window[w++] = (unsigned char) t->v.n;
431 outcnt = (w), flush_window();
434 } else { /* it's an EOB or a length */
436 /* exit if end of block */
441 /* get length of block to copy */
443 input_char = fgetc(in_file);
444 if (input_char == EOF) return 1;
445 b |= ((unsigned long)input_char) << k;
448 n = t->v.n + ((unsigned) b & mask_bits[e]);
452 /* decode distance of block to copy */
453 while (k < (unsigned) bd) {
454 input_char = fgetc(in_file);
455 if (input_char == EOF) return 1;
456 b |= ((unsigned long)input_char) << k;
460 if ((e = (t = td + ((unsigned) b & md))->e) > 16)
468 input_char = fgetc(in_file);
469 if (input_char == EOF) return 1;
470 b |= ((unsigned long)input_char) << k;
475 t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16);
479 input_char = fgetc(in_file);
480 if (input_char == EOF) return 1;
481 b |= ((unsigned long)input_char) << k;
484 d = w - t->v.n - ((unsigned) b & mask_bits[e]);
492 WSIZE - ((d &= WSIZE - 1) > w ? d : w)) > n ? n : e);
493 #if !defined(NOMEMCPY) && !defined(DEBUG)
494 if (w - d >= e) { /* (this test assumes unsigned comparison) */
495 memcpy(window + w, window + d, e);
498 } else /* do it slow to avoid memcpy() overlap */
499 #endif /* !NOMEMCPY */
501 window[w++] = window[d++];
504 outcnt = (w), flush_window();
511 /* restore the globals from the locals */
512 outcnt = w; /* restore global window pointer */
513 bb = b; /* restore global bit buffer */
520 static const unsigned short cplens[] = { /* Copy lengths for literal codes 257..285 */
521 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
522 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
525 /* note: see note #13 above about the 258 in this list. */
526 static const extra_bits_t cplext[] = { /* Extra bits for literal codes 257..285 */
527 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
528 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99
530 static const unsigned short cpdist[] = { /* Copy offsets for distance codes 0..29 */
531 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
532 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
533 8193, 12289, 16385, 24577
535 static const extra_bits_t cpdext[] = { /* Extra bits for distance codes */
536 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
537 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
541 /* Tables for deflate from PKZIP's appnote.txt. */
542 static const extra_bits_t border[] = { /* Order of the bit length code lengths */
543 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
547 * decompress an inflated block
550 * GLOBAL VARIABLES: bb, kk,
552 static int inflate_block(int *e)
554 unsigned t; /* block type */
555 register unsigned long b; /* bit buffer */
556 register unsigned k; /* number of bits in bit buffer */
559 /* make local bit buffer */
563 /* read in last block bit */
565 input_char = fgetc(in_file);
566 if (input_char == EOF) return 1;
567 b |= ((unsigned long)input_char) << k;
574 /* read in block type */
576 input_char = fgetc(in_file);
577 if (input_char == EOF) return 1;
578 b |= ((unsigned long)input_char) << k;
581 t = (unsigned) b & 3;
585 /* restore the global bit buffer */
589 /* inflate that block type */
591 case 0: /* Inflate stored */
593 unsigned long n; /* number of bytes in block */
594 unsigned long w; /* current window position */
595 register unsigned long b_stored; /* bit buffer */
596 register unsigned long k_stored; /* number of bits in bit buffer */
598 /* make local copies of globals */
599 b_stored = bb; /* initialize bit buffer */
601 w = outcnt; /* initialize window position */
603 /* go to byte boundary */
608 /* get the length and its complement */
609 while (k_stored < 16) {
610 input_char = fgetc(in_file);
611 if (input_char == EOF) return 1;
612 b_stored |= ((unsigned long)input_char) << k_stored;
615 n = ((unsigned) b_stored & 0xffff);
618 while (k_stored < 16) {
619 input_char = fgetc(in_file);
620 if (input_char == EOF) return 1;
621 b_stored |= ((unsigned long)input_char) << k_stored;
624 if (n != (unsigned) ((~b_stored) & 0xffff)) {
625 return 1; /* error in compressed data */
630 /* read and output the compressed data */
632 while (k_stored < 8) {
633 input_char = fgetc(in_file);
634 if (input_char == EOF) return 1;
635 b_stored |= ((unsigned long)input_char) << k_stored;
638 window[w++] = (unsigned char) b_stored;
639 if (w == (unsigned long) WSIZE) {
640 outcnt = (w), flush_window();
647 /* restore the globals from the locals */
648 outcnt = w; /* restore global window pointer */
649 bb = b_stored; /* restore global bit buffer */
653 case 1: /* Inflate fixed
654 * decompress an inflated type 1 (fixed Huffman codes) block. We should
655 * either replace this with a custom decoder, or at least precompute the
659 int i; /* temporary variable */
660 huft_t *tl; /* literal/length code table */
661 huft_t *td; /* distance code table */
662 int bl; /* lookup bits for tl */
663 int bd; /* lookup bits for td */
664 unsigned int l[288]; /* length list for huft_build */
666 /* set up literal table */
667 for (i = 0; i < 144; i++) {
670 for (; i < 256; i++) {
673 for (; i < 280; i++) {
676 for (; i < 288; i++) { /* make a complete, but wrong code set */
680 if ((i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl)) != 0) {
684 /* set up distance table */
685 for (i = 0; i < 30; i++) { /* make an incomplete code set */
689 if ((i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd)) > 1) {
694 /* decompress until an end-of-block code */
695 if (inflate_codes(tl, td, bl, bd))
698 /* free the decoding tables, return */
703 case 2: /* Inflate dynamic */
705 const int dbits = 6; /* bits in base distance lookup table */
706 const int lbits = 9; /* bits in base literal/length lookup table */
708 int i; /* temporary variables */
710 unsigned l; /* last length */
711 unsigned m; /* mask for bit lengths table */
712 unsigned n; /* number of lengths to get */
713 huft_t *tl; /* literal/length code table */
714 huft_t *td; /* distance code table */
715 int bl; /* lookup bits for tl */
716 int bd; /* lookup bits for td */
717 unsigned nb; /* number of bit length codes */
718 unsigned nl; /* number of literal/length codes */
719 unsigned nd; /* number of distance codes */
721 unsigned ll[286 + 30]; /* literal/length and distance code lengths */
722 register unsigned long b_dynamic; /* bit buffer */
723 register unsigned k_dynamic; /* number of bits in bit buffer */
725 /* make local bit buffer */
729 /* read in table lengths */
730 while (k_dynamic < 5) {
731 input_char = fgetc(in_file);
732 if (input_char == EOF) return 1;
733 b_dynamic |= ((unsigned long)input_char) << k_dynamic;
736 nl = 257 + ((unsigned) b_dynamic & 0x1f); /* number of literal/length codes */
739 while (k_dynamic < 5) {
740 input_char = fgetc(in_file);
741 if (input_char == EOF) return 1;
742 b_dynamic |= ((unsigned long)input_char) << k_dynamic;
745 nd = 1 + ((unsigned) b_dynamic & 0x1f); /* number of distance codes */
748 while (k_dynamic < 4) {
749 input_char = fgetc(in_file);
750 if (input_char == EOF) return 1;
751 b_dynamic |= ((unsigned long)input_char) << k_dynamic;
754 nb = 4 + ((unsigned) b_dynamic & 0xf); /* number of bit length codes */
757 if (nl > 286 || nd > 30) {
758 return 1; /* bad lengths */
761 /* read in bit-length-code lengths */
762 for (j = 0; j < nb; j++) {
763 while (k_dynamic < 3) {
764 input_char = fgetc(in_file);
765 if (input_char == EOF) return 1;
766 b_dynamic |= ((unsigned long)input_char) << k_dynamic;
769 ll[border[j]] = (unsigned) b_dynamic & 7;
773 for (; j < 19; j++) {
777 /* build decoding table for trees--single level, 7 bit lookup */
779 if ((i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl)) != 0) {
783 return i; /* incomplete code set */
786 /* read in literal and distance code lengths */
790 while ((unsigned) i < n) {
791 while (k_dynamic < (unsigned) bl) {
792 input_char = fgetc(in_file);
793 if (input_char == EOF) return 1;
794 b_dynamic |= ((unsigned long)input_char) << k_dynamic;
797 j = (td = tl + ((unsigned) b_dynamic & m))->b;
801 if (j < 16) { /* length of code in bits (0..15) */
802 ll[i++] = l = j; /* save last length in l */
803 } else if (j == 16) { /* repeat last length 3 to 6 times */
804 while (k_dynamic < 2) {
805 input_char = fgetc(in_file);
806 if (input_char == EOF) return 1;
807 b_dynamic |= ((unsigned long)input_char) << k_dynamic;
810 j = 3 + ((unsigned) b_dynamic & 3);
813 if ((unsigned) i + j > n) {
819 } else if (j == 17) { /* 3 to 10 zero length codes */
820 while (k_dynamic < 3) {
821 input_char = fgetc(in_file);
822 if (input_char == EOF) return 1;
823 b_dynamic |= ((unsigned long)input_char) << k_dynamic;
826 j = 3 + ((unsigned) b_dynamic & 7);
829 if ((unsigned) i + j > n) {
836 } else { /* j == 18: 11 to 138 zero length codes */
837 while (k_dynamic < 7) {
838 input_char = fgetc(in_file);
839 if (input_char == EOF) return 1;
840 b_dynamic |= ((unsigned long)input_char) << k_dynamic;
843 j = 11 + ((unsigned) b_dynamic & 0x7f);
846 if ((unsigned) i + j > n) {
856 /* free decoding table for trees */
859 /* restore the global bit buffer */
863 /* build the decoding tables for literal/length and distance codes */
865 if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0) {
867 error_msg("Incomplete literal tree");
870 return i; /* incomplete code set */
873 if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0) {
875 error_msg("incomplete distance tree");
879 return i; /* incomplete code set */
882 /* decompress until an end-of-block code */
883 if (inflate_codes(tl, td, bl, bd))
886 /* free the decoding tables, return */
898 * decompress an inflated entry
900 * GLOBAL VARIABLES: outcnt, bk, bb, hufts, inptr
902 extern int inflate(FILE * in, FILE * out)
904 int e; /* last block flag */
905 int r; /* result code */
906 unsigned h = 0; /* maximum struct huft's malloc'ed */
908 /* initialize window, bit buffer */
916 /* Allocate all global buffers (for DYN_ALLOC option) */
917 window = xmalloc((size_t) (((2L * WSIZE) + 1L) * sizeof(unsigned char)));
920 /* Create the crc table */
923 /* decompress until the last block */
926 if ((r = inflate_block(&e)) != 0) {
934 /* Undo too much lookahead. The next read will be byte aligned so we
935 * can discard unused bits in the last meaningful byte.
939 ungetc((bb << bk), in_file);
942 /* flush out window */
951 /* ===========================================================================
952 * Unzip in to out. This routine works on gzip files only.
954 * IN assertions: the buffer inbuf contains already the beginning of
955 * the compressed data, from offsets inptr to insize-1 included.
956 * The magic header has already been checked. The output buffer is cleared.
957 * in, out: input and output file descriptors
959 extern int unzip(FILE * l_in_file, FILE * l_out_file)
961 unsigned char buf[8]; /* extended local header */
962 unsigned char flags; /* compression flags */
963 typedef void (*sig_type) (int);
965 unsigned char magic[2];
967 if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
968 (void) signal(SIGINT, (sig_type) abort_gzip);
971 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
972 (void) signal(SIGHUP, (sig_type) abort_gzip);
976 magic[0] = fgetc(l_in_file);
977 magic[1] = fgetc(l_in_file);
979 #ifdef CONFIG_FEATURE_UNCOMPRESS
980 /* Magic header for compress files, 1F 9d = \037\235 */
981 if ((magic[0] == 0x1F) && (magic[1] == 0x9d)) {
982 return uncompress(l_in_file, l_out_file);
986 /* Magic header for gzip files, 1F 8B = \037\213 */
987 if ((magic[0] != 0x1F) || (magic[1] != 0x8b)) {
988 error_msg("Invalid gzip magic");
992 /* Check the compression method */
993 if (fgetc(l_in_file) != 8) /* also catches EOF */ {
994 error_msg("Unknown compression method");
998 flags = (unsigned char) fgetc(l_in_file);
1000 /* Ignore time stamp(4), extra flags(1), OS type(1) */
1001 for (i = 0; i < 6; i++) {
1006 /* bit 2 set: extra field present */
1007 const unsigned short extra =
1008 fgetc(l_in_file) + (fgetc(l_in_file) << 8);
1009 if (feof(in_file)) return 1;
1010 for (i = 0; i < extra; i++) {
1015 /* Discard original name if any */
1017 /* bit 3 set: original file name present */
1018 while (fgetc(l_in_file) != 0 && !feof(l_in_file)); /* null */
1021 /* Discard file comment if any */
1023 /* bit 4 set: file comment present */
1024 while (fgetc(l_in_file) != 0 && !feof(l_in_file)); /* null */
1028 if (inflate(l_in_file, l_out_file) != 0) {
1029 error_msg("invalid compressed data--format violated");
1032 /* Get the crc and original length
1033 * crc32 (see algorithm.doc)
1034 * uncompressed input size modulo 2^32
1036 fread(buf, 1, 8, l_in_file);
1038 /* Validate decompression - crc */
1039 if ((unsigned int) ((buf[0] | (buf[1] << 8)) |
1040 ((buf[2] | (buf[3] << 8)) << 16)) !=
1041 (crc ^ 0xffffffffL)) {
1042 error_msg("invalid compressed data--crc error");
1044 /* Validate decompression - size */
1045 if (((buf[4] | (buf[5] << 8)) | ((buf[6] | (buf[7] << 8)) << 16)) !=
1046 (unsigned long) bytes_out) {
1047 error_msg("invalid compressed data--length error");
1054 * This needs access to global variables window and crc_table, so its not in its own file.
1056 extern void gz_close(int gunzip_pid)
1058 if (kill(gunzip_pid, SIGTERM) == -1) {
1060 ("*** Couldnt kill old gunzip process *** aborting");
1063 if (waitpid(gunzip_pid, NULL, 0) == -1) {
1064 printf("Couldnt wait ?");