1 /* vi: set sw=4 ts=4: */
3 * Gzip 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 <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.
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.",
65 #include <sys/types.h>
78 #define BB_DECLARE_EXTERN
79 #define bb_need_memory_exhausted
80 #define bb_need_name_too_long
83 FILE *in_file, *out_file;
85 static unsigned long *crc_table;
86 static unsigned long crc = 0xffffffffL; /* shift register contents */
88 /* Return codes from gzip */
89 static const int ERROR = 1;
92 * window size--must be a power of two, and
93 * at least 32K for zip's deflate method
95 static const int WSIZE = 0x8000;
97 /* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
98 static const int BMAX = 16; /* maximum bit length of any code (16 for explode) */
99 static const int N_MAX = 288; /* maximum number of codes in any set */
101 static unsigned char *window;
103 static long bytes_out; /* number of output bytes */
104 static unsigned long outcnt; /* bytes in output buffer */
106 unsigned hufts; /* track memory usage */
107 unsigned long bb; /* bit buffer */
108 unsigned bk; /* bits in bit buffer */
110 typedef struct huft_s {
111 unsigned char e; /* number of extra bits or operation */
112 unsigned char b; /* number of bits in this code or subcode */
114 unsigned short n; /* literal, length base, or distance base */
115 struct huft_s *t; /* pointer to next level of table */
119 unsigned short mask_bits[] = {
121 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
122 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
125 //static int error_number = 0;
126 /* ========================================================================
127 * Signal and error handler.
129 static void abort_gzip()
131 error_msg("gzip aborted\n");
137 static void make_crc_table()
139 unsigned long table_entry; /* crc shift register */
140 unsigned long poly = 0; /* polynomial exclusive-or pattern */
141 int i; /* counter for all possible eight bit values */
142 int k; /* byte being shifted into crc apparatus */
144 /* terms of polynomial defining this crc (except x^32): */
145 static int p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
147 crc_table = (unsigned long *) malloc(256 * sizeof(unsigned long));
149 /* Make exclusive-or pattern from polynomial (0xedb88320) */
150 for (i = 0; i < sizeof(p)/sizeof(int); i++)
151 poly |= 1L << (31 - p[i]);
153 /* Compute and print table of CRC's, five per line */
154 for (i = 0; i < 256; i++) {
156 /* The idea to initialize the register with the byte instead of
157 * zero was stolen from Haruhiko Okumura's ar002
159 for (k = 8; k; k--) {
160 table_entry = table_entry & 1 ? (table_entry >> 1) ^ poly : table_entry >> 1;
162 crc_table[i]=table_entry;
166 /* ===========================================================================
167 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
168 * (Used for the decompressed data only.)
177 for (n = 0; n < outcnt; n++) {
178 crc = crc_table[((int) crc ^ (window[n])) & 0xff] ^ (crc >> 8);
181 if (fwrite(window, 1, outcnt, out_file) != outcnt) {
182 error_msg_and_die("Couldnt write");
184 bytes_out += (unsigned long) outcnt;
189 * Free the malloc'ed tables built by huft_build(), which makes a linked
190 * list of the tables it made, with the links in a dummy first entry of
194 static int huft_free(huft_t *t)
198 /* Go through linked list, freeing from the malloced (t[-1]) address. */
200 while (p != (huft_t *) NULL) {
208 /* Given a list of code lengths and a maximum table size, make a set of
209 * tables to decode that set of codes. Return zero on success, one if
210 * the given code set is incomplete (the tables are still built in this
211 * case), two if the input is invalid (all zero length codes or an
212 * oversubscribed set of lengths), and three if not enough memory.
214 * b: code lengths in bits (all assumed <= BMAX)
215 * n: number of codes (assumed <= N_MAX)
216 * s: number of simple-valued codes (0..s-1)
217 * d: list of base values for non-simple codes
218 * e: list of extra bits for non-simple codes
219 * t: result: starting table
220 * m: maximum lookup bits, returns actual
222 static int huft_build(unsigned int *b, const unsigned int n, const unsigned int s,
223 const unsigned short *d, const unsigned short *e, huft_t **t, int *m)
225 unsigned a; /* counter for codes of length k */
226 unsigned c[BMAX + 1]; /* bit length count table */
227 unsigned f; /* i repeats in table every f entries */
228 int g; /* maximum code length */
229 int h; /* table level */
230 register unsigned i; /* counter, current code */
231 register unsigned j; /* counter */
232 register int k; /* number of bits in current code */
233 int l; /* bits per table (returned in m) */
234 register unsigned *p; /* pointer into c[], b[], or v[] */
235 register huft_t *q; /* points to current table */
236 huft_t r; /* table entry for structure assignment */
237 huft_t *u[BMAX]; /* table stack */
238 unsigned v[N_MAX]; /* values in order of bit length */
239 register int w; /* bits before this table == (l * h) */
240 unsigned x[BMAX + 1]; /* bit offsets, then code stack */
241 unsigned *xp; /* pointer into x */
242 int y; /* number of dummy codes added */
243 unsigned z; /* number of entries in current table */
245 /* Generate counts for each bit length */
246 memset ((void *)(c), 0, sizeof(c));
250 c[*p]++; /* assume all entries <= BMAX */
251 p++; /* Can't combine with above line (Solaris bug) */
253 if (c[0] == n) { /* null input--all zero length codes */
254 *t = (huft_t *) NULL;
259 /* Find minimum and maximum length, bound *m by those */
261 for (j = 1; j <= BMAX; j++)
264 k = j; /* minimum code length */
265 if ((unsigned) l < j)
267 for (i = BMAX; i; i--)
270 g = i; /* maximum code length */
271 if ((unsigned) l > i)
275 /* Adjust last length count to fill out codes, if needed */
276 for (y = 1 << j; j < i; j++, y <<= 1)
278 return 2; /* bad input: more codes than bits */
283 /* Generate starting offsets into the value table for each length */
287 while (--i) { /* note that i == g from above */
291 /* Make a table of values in order of bit lengths */
299 /* Generate the Huffman codes and for each, make the table entries */
300 x[0] = i = 0; /* first Huffman code is zero */
301 p = v; /* grab values in bit order */
302 h = -1; /* no tables yet--level -1 */
303 w = -l; /* bits decoded == (l * h) */
304 u[0] = (huft_t *) NULL; /* just to keep compilers happy */
305 q = (huft_t *) NULL; /* ditto */
308 /* go through the bit lengths (k already is bits in shortest code) */
309 for (; k <= g; k++) {
312 /* here i is the Huffman code of length k bits for value *p */
313 /* make tables up to required level */
316 w += l; /* previous table always l bits */
318 /* compute minimum size table less than or equal to l bits */
319 z = (z = g - w) > (unsigned) l ? l : z; /* upper limit on table size */
320 if ((f = 1 << (j = k - w)) > a + 1) { /* try a k-w bit table *//* too few codes for k-w bit table */
321 f -= a + 1; /* deduct codes from patterns left */
323 while (++j < z) { /* try smaller tables up to z bits */
324 if ((f <<= 1) <= *++xp)
325 break; /* enough codes to use up j bits */
326 f -= *xp; /* else deduct codes from patterns */
329 z = 1 << j; /* table entries for j-bit table */
331 /* allocate and link in new table */
332 if ((q = (huft_t *) xmalloc((z + 1) * sizeof(huft_t))) == NULL) {
336 return 3; /* not enough memory */
338 hufts += z + 1; /* track memory usage */
339 *t = q + 1; /* link to list for huft_free() */
340 *(t = &(q->v.t)) = NULL;
341 u[h] = ++q; /* table starts after link */
343 /* connect to last table, if there is one */
345 x[h] = i; /* save pattern for backing up */
346 r.b = (unsigned char) l; /* bits to dump before this table */
347 r.e = (unsigned char) (16 + j); /* bits in this table */
348 r.v.t = q; /* pointer to this table */
349 j = i >> (w - l); /* (get around Turbo C bug) */
350 u[h - 1][j] = r; /* connect to last table */
354 /* set up table entry in r */
355 r.b = (unsigned char) (k - w);
357 r.e = 99; /* out of values--invalid code */
359 r.e = (unsigned char) (*p < 256 ? 16 : 15); /* 256 is end-of-block code */
360 r.v.n = (unsigned short) (*p); /* simple code is just the value */
361 p++; /* one compiler does not like *p++ */
363 r.e = (unsigned char) e[*p - s]; /* non-simple--look up in lists */
367 /* fill code-like entries with r */
369 for (j = i >> w; j < z; j += f)
372 /* backwards increment the k-bit code i */
373 for (j = 1 << (k - 1); i & j; j >>= 1)
377 /* backup over finished tables */
378 while ((i & ((1 << w) - 1)) != x[h]) {
379 h--; /* don't need to update q */
384 /* Return true (1) if we were given an incomplete table */
385 return y != 0 && g != 1;
389 * inflate (decompress) the codes in a deflated (compressed) block.
390 * Return an error code or zero if it all goes ok.
392 * tl, td: literal/length and distance decoder tables
393 * bl, bd: number of bits decoded by tl[] and td[]
395 static int inflate_codes(huft_t *tl, huft_t *td, int bl, int bd)
397 register unsigned long e; /* table entry flag/number of extra bits */
398 unsigned long n, d; /* length and index for copy */
399 unsigned long w; /* current window position */
400 huft_t *t; /* pointer to table entry */
401 unsigned ml, md; /* masks for bl and bd bits */
402 register unsigned long b; /* bit buffer */
403 register unsigned k; /* number of bits in bit buffer */
405 /* make local copies of globals */
406 b = bb; /* initialize bit buffer */
408 w = outcnt; /* initialize window position */
410 /* inflate the coded data */
411 ml = mask_bits[bl]; /* precompute masks for speed */
413 for (;;) { /* do until end of block */
414 while (k < (unsigned) bl) {
415 b |= ((unsigned long)fgetc(in_file)) << k;
418 if ((e = (t = tl + ((unsigned) b & ml))->e) > 16)
427 b |= ((unsigned long)fgetc(in_file)) << k;
430 } while ((e = (t = t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16);
433 if (e == 16) { /* then it's a literal */
434 window[w++] = (unsigned char) t->v.n;
440 } else { /* it's an EOB or a length */
442 /* exit if end of block */
447 /* get length of block to copy */
449 b |= ((unsigned long)fgetc(in_file)) << k;
452 n = t->v.n + ((unsigned) b & mask_bits[e]);
456 /* decode distance of block to copy */
457 while (k < (unsigned) bd) {
458 b |= ((unsigned long)fgetc(in_file)) << k;
462 if ((e = (t = td + ((unsigned) b & md))->e) > 16)
470 b |= ((unsigned long)fgetc(in_file)) << k;
473 } while ((e = (t = t->v.t + ((unsigned) b & mask_bits[e]))->e) > 16);
477 b |= ((unsigned long)fgetc(in_file)) << k;
480 d = w - t->v.n - ((unsigned) b & mask_bits[e]);
486 n -= (e = (e = WSIZE - ((d &= WSIZE - 1) > w ? d : w)) > n ? n : e);
487 #if !defined(NOMEMCPY) && !defined(DEBUG)
488 if (w - d >= e) { /* (this test assumes unsigned comparison) */
489 memcpy(window + w, window + d, e);
492 } else /* do it slow to avoid memcpy() overlap */
493 #endif /* !NOMEMCPY */
495 window[w++] = window[d++];
506 /* restore the globals from the locals */
507 outcnt = w; /* restore global window pointer */
508 bb = b; /* restore global bit buffer */
516 * decompress an inflated block
519 * GLOBAL VARIABLES: bb, kk,
521 static int inflate_block(int *e)
523 unsigned t; /* block type */
524 register unsigned long b; /* bit buffer */
525 register unsigned k; /* number of bits in bit buffer */
526 static unsigned short cplens[] = { /* Copy lengths for literal codes 257..285 */
527 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
528 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
530 /* note: see note #13 above about the 258 in this list. */
531 static unsigned short cplext[] = { /* Extra bits for literal codes 257..285 */
532 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
533 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99
535 static unsigned short cpdist[] = { /* Copy offsets for distance codes 0..29 */
536 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
537 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
538 8193, 12289, 16385, 24577
540 static unsigned short cpdext[] = { /* Extra bits for distance codes */
541 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
542 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
546 /* make local bit buffer */
550 /* read in last block bit */
552 b |= ((unsigned long)fgetc(in_file)) << k;
559 /* read in block type */
561 b |= ((unsigned long)fgetc(in_file)) << k;
564 t = (unsigned) b & 3;
568 /* restore the global bit buffer */
572 /* inflate that block type */
574 case 0: /* Inflate stored */
576 unsigned long n; /* number of bytes in block */
577 unsigned long w; /* current window position */
578 register unsigned long b_stored; /* bit buffer */
579 register unsigned long k_stored; /* number of bits in bit buffer */
581 /* make local copies of globals */
582 b_stored = bb; /* initialize bit buffer */
584 w = outcnt; /* initialize window position */
586 /* go to byte boundary */
591 /* get the length and its complement */
592 while (k_stored < 16) {
593 b_stored |= ((unsigned long)fgetc(in_file)) << k_stored;
596 n = ((unsigned) b_stored & 0xffff);
599 while (k_stored < 16) {
600 b_stored |= ((unsigned long)fgetc(in_file)) << k_stored;
603 if (n != (unsigned) ((~b_stored) & 0xffff)) {
604 return 1; /* error in compressed data */
609 /* read and output the compressed data */
611 while (k_stored < 8) {
612 b_stored |= ((unsigned long)fgetc(in_file)) << k_stored;
615 window[w++] = (unsigned char) b_stored;
616 if (w == (unsigned long)WSIZE) {
625 /* restore the globals from the locals */
626 outcnt = w; /* restore global window pointer */
627 bb = b_stored; /* restore global bit buffer */
631 case 1: /* Inflate fixed
632 * decompress an inflated type 1 (fixed Huffman codes) block. We should
633 * either replace this with a custom decoder, or at least precompute the
637 int i; /* temporary variable */
638 huft_t *tl; /* literal/length code table */
639 huft_t *td; /* distance code table */
640 int bl; /* lookup bits for tl */
641 int bd; /* lookup bits for td */
642 unsigned int l[288]; /* length list for huft_build */
644 /* set up literal table */
645 for (i = 0; i < 144; i++) {
648 for (; i < 256; i++) {
651 for (; i < 280; i++) {
654 for (; i < 288; i++) { /* make a complete, but wrong code set */
658 if ((i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl)) != 0) {
662 /* set up distance table */
663 for (i = 0; i < 30; i++) { /* make an incomplete code set */
667 if ((i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd)) > 1) {
672 /* decompress until an end-of-block code */
673 if (inflate_codes(tl, td, bl, bd))
676 /* free the decoding tables, return */
681 case 2: /* Inflate dynamic */
683 /* Tables for deflate from PKZIP's appnote.txt. */
684 static unsigned border[] = { /* Order of the bit length code lengths */
685 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
687 int dbits = 6; /* bits in base distance lookup table */
688 int lbits = 9; /* bits in base literal/length lookup table */
690 int i; /* temporary variables */
692 unsigned l; /* last length */
693 unsigned m; /* mask for bit lengths table */
694 unsigned n; /* number of lengths to get */
695 huft_t *tl; /* literal/length code table */
696 huft_t *td; /* distance code table */
697 int bl; /* lookup bits for tl */
698 int bd; /* lookup bits for td */
699 unsigned nb; /* number of bit length codes */
700 unsigned nl; /* number of literal/length codes */
701 unsigned nd; /* number of distance codes */
703 unsigned ll[286 + 30]; /* literal/length and distance code lengths */
704 register unsigned long b_dynamic; /* bit buffer */
705 register unsigned k_dynamic; /* number of bits in bit buffer */
707 /* make local bit buffer */
711 /* read in table lengths */
712 while (k_dynamic < 5) {
713 b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic;
716 nl = 257 + ((unsigned) b_dynamic & 0x1f); /* number of literal/length codes */
719 while (k_dynamic < 5) {
720 b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic;
723 nd = 1 + ((unsigned) b_dynamic & 0x1f); /* number of distance codes */
726 while (k_dynamic < 4) {
727 b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic;
730 nb = 4 + ((unsigned) b_dynamic & 0xf); /* number of bit length codes */
733 if (nl > 286 || nd > 30) {
734 return 1; /* bad lengths */
737 /* read in bit-length-code lengths */
738 for (j = 0; j < nb; j++) {
739 while (k_dynamic < 3) {
740 b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic;
743 ll[border[j]] = (unsigned) b_dynamic & 7;
747 for (; j < 19; j++) {
751 /* build decoding table for trees--single level, 7 bit lookup */
753 if ((i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl)) != 0) {
757 return i; /* incomplete code set */
760 /* read in literal and distance code lengths */
764 while ((unsigned) i < n) {
765 while (k_dynamic < (unsigned) bl) {
766 b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic;
769 j = (td = tl + ((unsigned) b_dynamic & m))->b;
773 if (j < 16) { /* length of code in bits (0..15) */
774 ll[i++] = l = j; /* save last length in l */
776 else if (j == 16) { /* repeat last length 3 to 6 times */
777 while (k_dynamic < 2) {
778 b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic;
781 j = 3 + ((unsigned) b_dynamic & 3);
784 if ((unsigned) i + j > n) {
790 } else if (j == 17) { /* 3 to 10 zero length codes */
791 while (k_dynamic < 3) {
792 b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic;
795 j = 3 + ((unsigned) b_dynamic & 7);
798 if ((unsigned) i + j > n) {
805 } else { /* j == 18: 11 to 138 zero length codes */
806 while (k_dynamic < 7) {
807 b_dynamic |= ((unsigned long)fgetc(in_file)) << k_dynamic;
810 j = 11 + ((unsigned) b_dynamic & 0x7f);
813 if ((unsigned) i + j > n) {
823 /* free decoding table for trees */
826 /* restore the global bit buffer */
830 /* build the decoding tables for literal/length and distance codes */
832 if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0) {
834 error_msg("Incomplete literal tree");
837 return i; /* incomplete code set */
840 if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0) {
842 error_msg("incomplete distance tree");
846 return i; /* incomplete code set */
849 /* decompress until an end-of-block code */
850 if (inflate_codes(tl, td, bl, bd))
853 /* free the decoding tables, return */
865 * decompress an inflated entry
867 * GLOBAL VARIABLES: outcnt, bk, bb, hufts, inptr
871 int e; /* last block flag */
872 int r; /* result code */
873 unsigned h = 0; /* maximum struct huft's malloc'ed */
875 /* initialize window, bit buffer */
880 /* decompress until the last block */
883 if ((r = inflate_block(&e)) != 0) {
891 /* flush out window */
898 /* ===========================================================================
899 * Unzip in to out. This routine works on both gzip and pkzip files.
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 const int extra_field = 0x04; /* bit 2 set: extra field present */
909 const int orig_name = 0x08; /* bit 3 set: original file name present */
910 const int comment = 0x10; /* bit 4 set: file comment present */
911 unsigned char buf[8]; /* extended local header */
912 unsigned char flags; /* compression flags */
913 char magic[2]; /* magic header */
915 typedef void (*sig_type) (int);
916 int exit_code=0; /* program exit code */
919 out_file = l_out_file;
921 /* if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
922 (void) signal(SIGINT, (sig_type) abort_gzip);
925 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) {
926 (void) signal(SIGTERM, (sig_type) abort_gzip);
930 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
931 (void) signal(SIGHUP, (sig_type) abort_gzip);
935 /* Allocate all global buffers (for DYN_ALLOC option) */
936 window = xmalloc((size_t)(((2L*WSIZE)+1L)*sizeof(unsigned char)));
940 /* set the buffer size */
941 setvbuf(in_file, NULL, _IOFBF, 0x8000);
943 magic[0] = fgetc(in_file);
944 magic[1] = fgetc(in_file);
946 /* Magic header for gzip files, 1F 8B = \037\213 */
947 if (memcmp(magic, "\037\213", 2) != 0) {
948 error_msg("Invalid gzip magic");
952 method = (int) fgetc(in_file);
954 error_msg("unknown method %d -- get newer version of gzip", method);
959 flags = (unsigned char) fgetc(in_file);
961 /* Ignore time stamp(4), extra flags(1), OS type(1) */
962 fseek(in_file, 6, SEEK_CUR);
964 if ((flags & extra_field) != 0) {
965 fseek(in_file, (size_t) fgetc(in_file) + ((size_t)fgetc(in_file) << 8), SEEK_CUR);
968 /* Discard original name if any */
969 if ((flags & orig_name) != 0) {
970 while (fgetc(in_file) != 0); /* null */
973 /* Discard file comment if any */
974 if ((flags & comment) != 0) {
975 while (fgetc(in_file) != 0); /* null */
979 printf("it failed\n");
980 return(exit_code); /* error message already emitted */
991 error_msg(memory_exhausted);
992 } else if (res != 0) {
993 error_msg("invalid compressed data--format violated");
997 error_msg("internal error, invalid method");
1000 /* Get the crc and original length
1001 * crc32 (see algorithm.doc)
1002 * uncompressed input size modulo 2^32
1004 fread(buf, 1, 8, in_file);
1006 /* Validate decompression - crc */
1007 if (((buf[0] | (buf[1] << 8)) |((buf[2] | (buf[3] << 8)) << 16)) != (crc ^ 0xffffffffL)) {
1008 error_msg("invalid compressed data--crc error");
1010 /* Validate decompression - size */
1011 if (((buf[4] | (buf[5] << 8)) |((buf[6] | (buf[7] << 8)) << 16)) != (unsigned long) bytes_out) {
1012 error_msg("invalid compressed data--length error");
1021 extern int gz_open(FILE *compressed_file, int *pid)
1025 // signal(SIGCHLD, abort_gzip);
1026 if (pipe(unzip_pipe)!=0) {
1027 error_msg("pipe error");
1028 return(EXIT_FAILURE);
1030 if ((*pid = fork()) == -1) {
1031 error_msg("fork failured");
1032 return(EXIT_FAILURE);
1036 close(unzip_pipe[0]);
1037 unzip(compressed_file, fdopen(unzip_pipe[1], "w"));
1038 printf("finished unzipping\n");
1040 fclose(compressed_file);
1041 close(unzip_pipe[1]);
1045 close(unzip_pipe[1]);
1046 return(unzip_pipe[0]);
1049 extern void gz_close(int gunzip_pid)
1051 if (kill(gunzip_pid, SIGTERM) == -1) {
1052 error_msg_and_die("*** Couldnt kill old gunzip process *** aborting");
1055 if (waitpid(gunzip_pid, NULL, 0) == -1) {
1056 printf("Couldnt wait ?");
1062 extern int gunzip_main(int argc, char **argv)
1064 struct stat stat_buf;
1066 char *if_name = NULL;
1067 char *of_name = NULL;
1068 char *delete_file_name = NULL;
1070 const int gunzip_to_stdout = 1;
1071 const int gunzip_from_stdin = 2;
1072 const int gunzip_force = 4;
1073 const int gunzip_test = 8;
1077 int delete_old_file = FALSE;
1080 /* if called as zcat */
1081 if (strcmp(applet_name, "zcat") == 0) {
1085 flags |= gunzip_force;
1086 flags |= gunzip_to_stdout;
1090 /* workout flags as regular gunzip */
1091 /* set default flags */
1093 flags |= (gunzip_from_stdin | gunzip_to_stdout);
1096 /* Parse any options */
1097 while ((opt = getopt(argc, argv, "ctfh")) != -1) {
1100 flags |= gunzip_to_stdout;
1103 flags |= gunzip_force;
1106 flags |= gunzip_test;
1110 show_usage(); /* exit's inside usage */
1115 /* Set input filename and number */
1116 if (flags & gunzip_from_stdin) {
1118 if ((flags & gunzip_force) == 0) {
1119 error_msg_and_die("data not written to terminal. Use -f to force it.");
1121 strcpy(if_name, "stdin");
1123 if_name = strdup(argv[optind]);
1124 /* Open input file */
1125 in_file = xfopen(if_name, "r");
1127 /* Get the time stamp on the input file. */
1128 if (stat(if_name, &stat_buf) < 0) {
1129 error_msg_and_die("Couldnt stat file %s",if_name);
1133 /* Set output filename and number */
1134 if (flags & gunzip_to_stdout) {
1136 /* whats the best way to do this with streams ? */
1137 if (isatty(fileno(out_file)) && ((flags & gunzip_force) == 0)) {
1138 error_msg_and_die("data not written to terminal. Use -f to force it.");
1141 strcpy(of_name, "stdout");
1142 } else if (flags & gunzip_test) {
1143 out_file = xfopen("/dev/null", "w"); /* why does test use filenum 2 ? */
1146 int length = strlen(if_name);
1148 delete_old_file = TRUE;
1149 extension = strrchr(if_name, '.');
1150 if (strcmp(extension, ".gz") == 0) {
1152 } else if (strcmp(extension, ".tgz") == 0) {
1155 error_msg_and_die("Invalid extension");
1157 of_name = (char *) xcalloc(sizeof(char), length + 1);
1158 strncpy(of_name, if_name, length);
1160 /* Open output file */
1161 out_file = xfopen(of_name, "w");
1163 /* Set permissions on the file */
1164 chmod(of_name, stat_buf.st_mode);
1167 /* do the decompression, and cleanup */
1168 if (unzip(in_file, out_file) == 0) {
1169 /* Success, remove .gz file */
1170 delete_file_name = if_name;
1172 /* remove failed attempt */
1173 delete_file_name = of_name;
1179 if (delete_old_file == TRUE) {
1180 if (unlink(delete_file_name) < 0) {
1181 error_msg_and_die("Couldnt remove %s", delete_file_name);
1187 return(EXIT_SUCCESS);