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 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 * gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
30 * Copyright (C) 1992-1993 Jean-loup Gailly
31 * The unzip code was written and put in the public domain by Mark Adler.
32 * Portions of the lzw code are derived from the public domain 'compress'
33 * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
34 * Ken Turkowski, Dave Mack and Peter Jannesen.
36 * See the license_msg below and the file COPYING for the software license.
37 * See the file algorithm.doc for the compression algorithms and file formats.
41 static char *license_msg[] = {
42 " Copyright (C) 1992-1993 Jean-loup Gailly",
43 " This program is free software; you can redistribute it and/or modify",
44 " it under the terms of the GNU General Public License as published by",
45 " the Free Software Foundation; either version 2, or (at your option)",
46 " any later version.",
48 " This program is distributed in the hope that it will be useful,",
49 " but WITHOUT ANY WARRANTY; without even the implied warranty of",
50 " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the",
51 " GNU General Public License for more details.",
53 " You should have received a copy of the GNU General Public License",
54 " along with this program; if not, write to the Free Software",
55 " Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.",
62 #include <sys/types.h>
74 #define BB_DECLARE_EXTERN
75 #define bb_need_memory_exhausted
76 #define bb_need_name_too_long
79 static const int RECORD_IO = 0;
81 /* Return codes from gzip */
82 static const int OK = 0;
83 static const int ERROR = 1;
84 static const int WARNING = 2;
86 static const int DEFLATED = 8;
87 static const int INBUFSIZ = 0x2000; /* input buffer size */
88 static const int INBUF_EXTRA = 64; /* required by unlzw() */
89 static const int OUTBUFSIZ = 8192; /* output buffer size */
90 static const int OUTBUF_EXTRA = 2048; /* required by unlzw() */
91 static const int DIST_BUFSIZE = 0x2000; /* buffer for distances, see trees.c */
93 #define GZIP_MAGIC "\037\213" /* Magic header for gzip files, 1F 8B */
96 static const int EXTRA_FIELD = 0x04; /* bit 2 set: extra field present */
97 static const int ORIG_NAME = 0x08; /* bit 3 set: original file name present */
98 static const int COMMENT = 0x10; /* bit 4 set: file comment present */
99 static const int WSIZE = 0x8000; /* window size--must be a power of two, and */
100 /* at least 32K for zip's deflate method */
102 /* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
103 static const int BMAX = 16; /* maximum bit length of any code (16 for explode) */
104 static const int N_MAX = 288; /* maximum number of codes in any set */
106 /* PKZIP header definitions */
107 static const int LOCSIG = 0x04034b50L; /* four-byte lead-in (lsb first) */
108 static const int LOCCRC = 14; /* offset of crc */
109 static const int LOCLEN = 22; /* offset of uncompressed length */
110 static const int EXTHDR = 16; /* size of extended local header, inc sig */
112 static const int BITS = 16;
114 /* Diagnostic functions */
116 # define Assert(cond,msg) {if(!(cond)) error_msg(msg);}
117 # define Trace(x) fprintf x
118 # define Tracev(x) {if (verbose) fprintf x ;}
119 # define Tracevv(x) {if (verbose>1) fprintf x ;}
120 # define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
121 # define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
123 # define Assert(cond,msg)
128 # define Tracecv(c,x)
131 #ifndef MAX_PATH_LEN /* max pathname length */
133 # define MAX_PATH_LEN BUFSIZ
135 static const int MAX_PATH_LEN = 1024;
139 #define NEXTBYTE() (uch)get_byte()
140 #define NEEDBITS(n) {while(k<(n)){b|=((ulg)NEXTBYTE())<<k;k+=8;}}
141 #define DUMPBITS(n) {b>>=(n);k-=(n);}
143 #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf(0))
145 /* Macros for getting two-byte and four-byte header values */
146 #define SH(p) ((ush)(uch)((p)[0]) | ((ush)(uch)((p)[1]) << 8))
147 #define LG(p) ((ulg)(SH(p)) | ((ulg)(SH((p)+2)) << 16))
150 void abort_gzip (void);
151 typedef void (*sig_type) (int);
153 typedef unsigned char uch;
154 typedef unsigned short ush;
155 typedef unsigned long ulg;
156 typedef int file_t; /* Do not use stdio */
162 static ush *tab_prefix0;
163 static ush *tab_prefix1;
165 /* local variables */
166 static int test_mode = 0; /* check file integrity option */
167 static int foreground; /* set if program run in foreground */
168 static int method; /* compression method */
169 static int exit_code; /* program exit code */
170 static int last_member; /* set for .zip and .Z files */
171 static int part_nb; /* number of parts in .gz file */
172 static long ifile_size; /* input file size, -1 for devices (debug only) */
173 static long bytes_in; /* number of input bytes */
174 static long bytes_out; /* number of output bytes */
175 static int ifd; /* input file descriptor */
176 static int ofd; /* output file descriptor */
177 static unsigned insize; /* valid bytes in inbuf */
178 static unsigned inptr; /* index of next byte to be processed in inbuf */
179 static unsigned outcnt; /* bytes in output buffer */
181 unsigned hufts; /* track memory usage */
182 ulg bb; /* bit buffer */
183 unsigned bk; /* bits in bit buffer */
184 int crc_table_empty = 1;
187 uch e; /* number of extra bits or operation */
188 uch b; /* number of bits in this code or subcode */
190 ush n; /* literal, length base, or distance base */
191 struct huft *t; /* pointer to next level of table */
195 /* Tables for deflate from PKZIP's appnote.txt. */
196 static unsigned border[] = { /* Order of the bit length code lengths */
197 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
199 static ush cplens[] = { /* Copy lengths for literal codes 257..285 */
200 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
201 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
204 /* note: see note #13 above about the 258 in this list. */
205 static ush cplext[] = { /* Extra bits for literal codes 257..285 */
206 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
207 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99
210 static ush cpdist[] = { /* Copy offsets for distance codes 0..29 */
211 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
212 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
213 8193, 12289, 16385, 24577
216 static ush cpdext[] = { /* Extra bits for distance codes */
217 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
218 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
224 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
225 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
228 /* ========================================================================
231 void read_error_msg()
233 fprintf(stderr, "\n");
237 fprintf(stderr, "unexpected end of file\n");
242 /* ===========================================================================
243 * Fill the input buffer. This is called only when the buffer is empty.
245 int fill_inbuf(eof_ok)
246 int eof_ok; /* set if EOF acceptable as a result */
250 /* Read as much as possible */
254 len = read(ifd, (char *) inbuf + insize, INBUFSIZ - insize);
255 if (len == 0 || len == EOF)
258 } while (insize < INBUFSIZ);
265 bytes_in += (ulg) insize;
271 /* ========================================================================
272 * Check the magic number of the input file and update ofname if an
273 * original name was given and tostdout is not set.
274 * Return the compression method, -1 for error, -2 for warning.
275 * Set inptr to the offset of the next byte to be processed.
276 * Updates time_stamp if there is one and --no-time is not used.
277 * This function may be called repeatedly for an input file consisting
278 * of several contiguous gzip'ed members.
279 * IN assertions: there is at least one remaining compressed member.
280 * If the member is a zip file, it must be the only one.
282 static int get_method(in)
283 int in; /* input file descriptor */
285 uch flags; /* compression flags */
286 char magic[2]; /* magic header */
287 long header_bytes = 0; /* number of bytes in gzip header */
289 magic[0] = (char) get_byte();
290 magic[1] = (char) get_byte();
291 method = -1; /* unknown yet */
292 part_nb++; /* number of parts in gzip file */
293 last_member = RECORD_IO;
294 /* assume multiple members in gzip file except for record oriented I/O */
296 if (memcmp(magic, GZIP_MAGIC, 2) == 0) {
298 method = (int) get_byte();
299 if (method != DEFLATED) {
300 error_msg("unknown method %d -- get newer version of gzip", method);
304 flags = (uch) get_byte();
306 (ulg) get_byte(); /* Ignore time stamp */
311 (void) get_byte(); /* Ignore extra flags for the moment */
312 (void) get_byte(); /* Ignore OS type for the moment */
314 if ((flags & EXTRA_FIELD) != 0) {
315 unsigned len = (unsigned) get_byte();
316 len |= ((unsigned) get_byte()) << 8;
321 /* Discard original name if any */
322 if ((flags & ORIG_NAME) != 0) {
323 while (get_byte() != 0); /* null */
326 /* Discard file comment if any */
327 if ((flags & COMMENT) != 0) {
328 while (get_byte() != 0) /* null */
332 header_bytes = inptr + 2 * sizeof(long); /* include crc and size */
341 fprintf(stderr, "\nnot in gzip format\n");
345 fprintf(stderr, "\ndecompression OK, trailing garbage ignored\n");
352 /* ========================================================================
353 * Signal and error handler.
360 /* ===========================================================================
361 * Run a set of bytes through the crc shift register. If s is a NULL
362 * pointer, then initialize the crc shift register contents instead.
363 * Return the current crc in either case.
366 uch *s; /* pointer to bytes to pump through */
367 unsigned n; /* number of bytes in s[] */
369 static ulg crc = (ulg) 0xffffffffL; /* shift register contents */
370 register ulg c; /* temporary variable */
371 static unsigned long crc_32_tab[256];
372 if (crc_table_empty) {
373 unsigned long c; /* crc shift register */
374 unsigned long e; /* polynomial exclusive-or pattern */
375 int i; /* counter for all possible eight bit values */
376 int k; /* byte being shifted into crc apparatus */
378 /* terms of polynomial defining this crc (except x^32): */
379 static int p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
381 /* Make exclusive-or pattern from polynomial (0xedb88320) */
383 for (i = 0; i < sizeof(p)/sizeof(int); i++)
384 e |= 1L << (31 - p[i]);
386 /* Compute and print table of CRC's, five per line */
387 crc_32_tab[0] = 0x00000000L;
388 for (i = 1; i < 256; i++) {
390 /* The idea to initialize the register with the byte instead of
391 * zero was stolen from Haruhiko Okumura's ar002
394 c = c & 1 ? (c >> 1) ^ e : c >> 1;
405 c = crc_32_tab[((int) c ^ (*s++)) & 0xff] ^ (c >> 8);
409 return c ^ 0xffffffffL; /* (instead of ~c for 64-bit machines) */
412 void write_error_msg()
414 fprintf(stderr, "\n");
419 /* ===========================================================================
420 * Does the same as write(), but also handles partial pipe writes and checks
423 void write_buf(fd, buf, cnt)
430 while ((n = write(fd, buf, cnt)) != cnt) {
431 if (n == (unsigned) (-1)) {
435 buf = (void *) ((char *) buf + n);
439 /* ===========================================================================
440 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
441 * (Used for the decompressed data only.)
447 updcrc(window, outcnt);
450 write_buf(ofd, (char *) window, outcnt);
451 bytes_out += (ulg) outcnt;
456 /* "decompress" an inflated type 0 (stored) block. */
458 unsigned n; /* number of bytes in block */
459 unsigned w; /* current window position */
460 register ulg b; /* bit buffer */
461 register unsigned k; /* number of bits in bit buffer */
463 /* make local copies of globals */
464 b = bb; /* initialize bit buffer */
466 w = outcnt; /* initialize window position */
468 /* go to byte boundary */
472 /* get the length and its complement */
474 n = ((unsigned) b & 0xffff);
477 if (n != (unsigned) ((~b) & 0xffff))
478 return 1; /* error in compressed data */
481 /* read and output the compressed data */
484 window[w++] = (uch) b;
494 /* restore the globals from the locals */
495 outcnt = w; /* restore global window pointer */
496 bb = b; /* restore global bit buffer */
502 struct huft *t; /* table to free */
504 /* Free the malloc'ed tables built by huft_build(), which makes a linked
505 list of the tables it made, with the links in a dummy first entry of
508 register struct huft *p, *q;
510 /* Go through linked list, freeing from the malloced (t[-1]) address. */
512 while (p != (struct huft *) NULL) {
521 int huft_build(b, n, s, d, e, t, m)
522 unsigned *b; /* code lengths in bits (all assumed <= BMAX) */
523 unsigned n; /* number of codes (assumed <= N_MAX) */
524 unsigned s; /* number of simple-valued codes (0..s-1) */
525 ush *d; /* list of base values for non-simple codes */
526 ush *e; /* list of extra bits for non-simple codes */
527 struct huft **t; /* result: starting table */
528 int *m; /* maximum lookup bits, returns actual */
530 /* Given a list of code lengths and a maximum table size, make a set of
531 tables to decode that set of codes. Return zero on success, one if
532 the given code set is incomplete (the tables are still built in this
533 case), two if the input is invalid (all zero length codes or an
534 oversubscribed set of lengths), and three if not enough memory. */
536 unsigned a; /* counter for codes of length k */
537 unsigned c[BMAX + 1]; /* bit length count table */
538 unsigned f; /* i repeats in table every f entries */
539 int g; /* maximum code length */
540 int h; /* table level */
541 register unsigned i; /* counter, current code */
542 register unsigned j; /* counter */
543 register int k; /* number of bits in current code */
544 int l; /* bits per table (returned in m) */
545 register unsigned *p; /* pointer into c[], b[], or v[] */
546 register struct huft *q; /* points to current table */
547 struct huft r; /* table entry for structure assignment */
548 struct huft *u[BMAX]; /* table stack */
549 unsigned v[N_MAX]; /* values in order of bit length */
550 register int w; /* bits before this table == (l * h) */
551 unsigned x[BMAX + 1]; /* bit offsets, then code stack */
552 unsigned *xp; /* pointer into x */
553 int y; /* number of dummy codes added */
554 unsigned z; /* number of entries in current table */
556 /* Generate counts for each bit length */
557 memset ((void *)(c), 0, sizeof(c));
561 Tracecv(*p,
\e(stderr, (n - i >= ' ' && n - i <= '~' ? "%c %d\n" : "0x%x %d\n"), n - i, *p));
562 c[*p]++; /* assume all entries <= BMAX */
563 p++; /* Can't combine with above line (Solaris bug) */
565 if (c[0] == n) { /* null input--all zero length codes */
566 *t = (struct huft *) NULL;
571 /* Find minimum and maximum length, bound *m by those */
573 for (j = 1; j <= BMAX; j++)
576 k = j; /* minimum code length */
577 if ((unsigned) l < j)
579 for (i = BMAX; i; i--)
582 g = i; /* maximum code length */
583 if ((unsigned) l > i)
587 /* Adjust last length count to fill out codes, if needed */
588 for (y = 1 << j; j < i; j++, y <<= 1)
590 return 2; /* bad input: more codes than bits */
595 /* Generate starting offsets into the value table for each length */
599 while (--i) { /* note that i == g from above */
603 /* Make a table of values in order of bit lengths */
611 /* Generate the Huffman codes and for each, make the table entries */
612 x[0] = i = 0; /* first Huffman code is zero */
613 p = v; /* grab values in bit order */
614 h = -1; /* no tables yet--level -1 */
615 w = -l; /* bits decoded == (l * h) */
616 u[0] = (struct huft *) NULL; /* just to keep compilers happy */
617 q = (struct huft *) NULL; /* ditto */
620 /* go through the bit lengths (k already is bits in shortest code) */
621 for (; k <= g; k++) {
624 /* here i is the Huffman code of length k bits for value *p */
625 /* make tables up to required level */
628 w += l; /* previous table always l bits */
630 /* compute minimum size table less than or equal to l bits */
631 z = (z = g - w) > (unsigned) l ? l : z; /* upper limit on table size */
632 if ((f = 1 << (j = k - w)) > a + 1) { /* try a k-w bit table *//* too few codes for k-w bit table */
633 f -= a + 1; /* deduct codes from patterns left */
635 while (++j < z) { /* try smaller tables up to z bits */
636 if ((f <<= 1) <= *++xp)
637 break; /* enough codes to use up j bits */
638 f -= *xp; /* else deduct codes from patterns */
641 z = 1 << j; /* table entries for j-bit table */
643 /* allocate and link in new table */
646 (struct huft *) malloc((z + 1) *
647 sizeof(struct huft))) ==
648 (struct huft *) NULL) {
651 return 3; /* not enough memory */
653 hufts += z + 1; /* track memory usage */
654 *t = q + 1; /* link to list for huft_free() */
655 *(t = &(q->v.t)) = (struct huft *) NULL;
656 u[h] = ++q; /* table starts after link */
658 /* connect to last table, if there is one */
660 x[h] = i; /* save pattern for backing up */
661 r.b = (uch) l; /* bits to dump before this table */
662 r.e = (uch) (16 + j); /* bits in this table */
663 r.v.t = q; /* pointer to this table */
664 j = i >> (w - l); /* (get around Turbo C bug) */
665 u[h - 1][j] = r; /* connect to last table */
669 /* set up table entry in r */
672 r.e = 99; /* out of values--invalid code */
674 r.e = (uch) (*p < 256 ? 16 : 15); /* 256 is end-of-block code */
675 r.v.n = (ush) (*p); /* simple code is just the value */
676 p++; /* one compiler does not like *p++ */
678 r.e = (uch) e[*p - s]; /* non-simple--look up in lists */
682 /* fill code-like entries with r */
684 for (j = i >> w; j < z; j += f)
687 /* backwards increment the k-bit code i */
688 for (j = 1 << (k - 1); i & j; j >>= 1)
692 /* backup over finished tables */
693 while ((i & ((1 << w) - 1)) != x[h]) {
694 h--; /* don't need to update q */
699 /* Return true (1) if we were given an incomplete table */
700 return y != 0 && g != 1;
704 int inflate_codes(tl, td, bl, bd)
705 struct huft *tl, *td; /* literal/length and distance decoder tables */
706 int bl, bd; /* number of bits decoded by tl[] and td[] */
708 /* inflate (decompress) the codes in a deflated (compressed) block.
709 Return an error code or zero if it all goes ok. */
711 register unsigned e; /* table entry flag/number of extra bits */
712 unsigned n, d; /* length and index for copy */
713 unsigned w; /* current window position */
714 struct huft *t; /* pointer to table entry */
715 unsigned ml, md; /* masks for bl and bd bits */
716 register ulg b; /* bit buffer */
717 register unsigned k; /* number of bits in bit buffer */
719 /* make local copies of globals */
720 b = bb; /* initialize bit buffer */
722 w = outcnt; /* initialize window position */
724 /* inflate the coded data */
725 ml = mask_bits[bl]; /* precompute masks for speed */
727 for (;;) { /* do until end of block */
728 NEEDBITS((unsigned) bl)
729 if ((e = (t = tl + ((unsigned) b & ml))->e) > 16)
736 } while ((e = (t = t->v.t + ((unsigned) b & mask_bits[e]))->e)
739 if (e == 16) { /* then it's a literal */
740 window[w++] = (uch) t->v.n;
741 Tracevv((stderr, "%c", window[w - 1]));
748 } else { /* it's an EOB or a length */
750 /* exit if end of block */
754 /* get length of block to copy */
756 n = t->v.n + ((unsigned) b & mask_bits[e]);
759 /* decode distance of block to copy */
760 NEEDBITS((unsigned) bd)
761 if ((e = (t = td + ((unsigned) b & md))->e) > 16)
772 t->v.t + ((unsigned) b & mask_bits[e]))->e) >
776 d = w - t->v.n - ((unsigned) b & mask_bits[e]);
778 Tracevv((stderr, "\\[%d,%d]", w - d, n));
784 WSIZE - ((d &= WSIZE - 1) > w ? d : w)) >
786 #if !defined(NOMEMCPY) && !defined(DEBUG)
787 if (w - d >= e) { /* (this test assumes unsigned comparison) */
788 memcpy(window + w, window + d, e);
791 } else /* do it slow to avoid memcpy() overlap */
792 #endif /* !NOMEMCPY */
794 window[w++] = window[d++];
795 Tracevv((stderr, "%c", window[w - 1]));
807 /* restore the globals from the locals */
808 outcnt = w; /* restore global window pointer */
809 bb = b; /* restore global bit buffer */
817 /* decompress an inflated type 1 (fixed Huffman codes) block. We should
818 either replace this with a custom decoder, or at least precompute the
821 int i; /* temporary variable */
822 struct huft *tl; /* literal/length code table */
823 struct huft *td; /* distance code table */
824 int bl; /* lookup bits for tl */
825 int bd; /* lookup bits for td */
826 unsigned l[288]; /* length list for huft_build */
828 /* set up literal table */
829 for (i = 0; i < 144; i++)
835 for (; i < 288; i++) /* make a complete, but wrong code set */
838 if ((i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl)) != 0)
841 /* set up distance table */
842 for (i = 0; i < 30; i++) /* make an incomplete code set */
845 if ((i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd)) > 1) {
850 /* decompress until an end-of-block code */
851 if (inflate_codes(tl, td, bl, bd))
854 /* free the decoding tables, return */
860 int inflate_dynamic()
861 /* decompress an inflated type 2 (dynamic Huffman codes) block. */
863 int dbits = 6; /* bits in base distance lookup table */
864 int lbits = 9; /* bits in base literal/length lookup table */
866 int i; /* temporary variables */
868 unsigned l; /* last length */
869 unsigned m; /* mask for bit lengths table */
870 unsigned n; /* number of lengths to get */
871 struct huft *tl; /* literal/length code table */
872 struct huft *td; /* distance code table */
873 int bl; /* lookup bits for tl */
874 int bd; /* lookup bits for td */
875 unsigned nb; /* number of bit length codes */
876 unsigned nl; /* number of literal/length codes */
877 unsigned nd; /* number of distance codes */
879 #ifdef PKZIP_BUG_WORKAROUND
880 unsigned ll[288 + 32]; /* literal/length and distance code lengths */
882 unsigned ll[286 + 30]; /* literal/length and distance code lengths */
884 register ulg b; /* bit buffer */
885 register unsigned k; /* number of bits in bit buffer */
887 /* make local bit buffer */
891 /* read in table lengths */
893 nl = 257 + ((unsigned) b & 0x1f); /* number of literal/length codes */
896 nd = 1 + ((unsigned) b & 0x1f); /* number of distance codes */
899 nb = 4 + ((unsigned) b & 0xf); /* number of bit length codes */
901 #ifdef PKZIP_BUG_WORKAROUND
902 if (nl > 288 || nd > 32)
904 if (nl > 286 || nd > 30)
906 return 1; /* bad lengths */
908 /* read in bit-length-code lengths */
909 for (j = 0; j < nb; j++) {
911 ll[border[j]] = (unsigned) b & 7;
917 /* build decoding table for trees--single level, 7 bit lookup */
919 if ((i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl)) != 0) {
922 return i; /* incomplete code set */
925 /* read in literal and distance code lengths */
929 while ((unsigned) i < n) {
930 NEEDBITS((unsigned) bl)
931 j = (td = tl + ((unsigned) b & m))->b;
934 if (j < 16) /* length of code in bits (0..15) */
935 ll[i++] = l = j; /* save last length in l */
936 else if (j == 16) { /* repeat last length 3 to 6 times */
938 j = 3 + ((unsigned) b & 3);
940 if ((unsigned) i + j > n)
944 } else if (j == 17) { /* 3 to 10 zero length codes */
946 j = 3 + ((unsigned) b & 7);
948 if ((unsigned) i + j > n)
953 } else { /* j == 18: 11 to 138 zero length codes */
956 j = 11 + ((unsigned) b & 0x7f);
958 if ((unsigned) i + j > n)
966 /* free decoding table for trees */
969 /* restore the global bit buffer */
973 /* build the decoding tables for literal/length and distance codes */
975 if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0) {
977 fprintf(stderr, " incomplete literal tree\n");
980 return i; /* incomplete code set */
983 if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0) {
985 fprintf(stderr, " incomplete distance tree\n");
989 return i; /* incomplete code set */
992 /* decompress until an end-of-block code */
993 if (inflate_codes(tl, td, bl, bd))
996 /* free the decoding tables, return */
1002 /* decompress an inflated block */
1003 int inflate_block(e)
1004 int *e; /* last block flag */
1006 unsigned t; /* block type */
1007 register ulg b; /* bit buffer */
1008 register unsigned k; /* number of bits in bit buffer */
1010 /* make local bit buffer */
1014 /* read in last block bit */
1019 /* read in block type */
1021 t = (unsigned) b & 3;
1024 /* restore the global bit buffer */
1028 /* inflate that block type */
1030 return inflate_dynamic();
1032 return inflate_stored();
1034 return inflate_fixed();
1036 /* bad block type */
1041 /* decompress an inflated entry */
1043 int e; /* last block flag */
1044 int r; /* result code */
1045 unsigned h; /* maximum struct huft's malloc'ed */
1047 /* initialize window, bit buffer */
1052 /* decompress until the last block */
1056 if ((r = inflate_block(&e)) != 0)
1062 /* Undo too much lookahead. The next read will be byte aligned so we
1063 * can discard unused bits in the last meaningful byte.
1070 /* flush out window */
1073 /* return success */
1075 fprintf(stderr, "<%u> ", h);
1080 /* ===========================================================================
1081 * Unzip in to out. This routine works on both gzip and pkzip files.
1083 * IN assertions: the buffer inbuf contains already the beginning of
1084 * the compressed data, from offsets inptr to insize-1 included.
1085 * The magic header has already been checked. The output buffer is cleared.
1087 extern int unzip(in, out)
1088 int in, out; /* input and output file descriptors */
1090 int ext_header = 0; /* set if extended local header */
1091 int pkzip = 0; /* set for a pkzip file */
1092 ulg orig_crc = 0; /* original crc */
1093 ulg orig_len = 0; /* original uncompressed length */
1095 uch buf[EXTHDR]; /* extended local header */
1099 method = get_method(ifd);
1101 exit(exit_code); /* error message already emitted */
1104 updcrc(NULL, 0); /* initialize crc */
1106 if (pkzip && !ext_header) { /* crc and length at the end otherwise */
1107 orig_crc = LG(inbuf + LOCCRC);
1108 orig_len = LG(inbuf + LOCLEN);
1112 if (method == DEFLATED) {
1114 int res = inflate();
1117 error_msg(memory_exhausted);
1118 } else if (res != 0) {
1119 error_msg("invalid compressed data--format violated");
1123 error_msg("internal error, invalid method");
1126 /* Get the crc and original length */
1128 /* crc32 (see algorithm.doc)
1129 * uncompressed input size modulo 2^32
1131 for (n = 0; n < 8; n++) {
1132 buf[n] = (uch) get_byte(); /* may cause an error if EOF */
1135 orig_len = LG(buf + 4);
1137 } else if (ext_header) { /* If extended header, check it */
1138 /* signature - 4bytes: 0x50 0x4b 0x07 0x08
1140 * compressed size 4-bytes
1141 * uncompressed size 4-bytes
1143 for (n = 0; n < EXTHDR; n++) {
1144 buf[n] = (uch) get_byte(); /* may cause an error if EOF */
1146 orig_crc = LG(buf + 4);
1147 orig_len = LG(buf + 12);
1150 /* Validate decompression */
1151 if (orig_crc != updcrc(outbuf, 0)) {
1152 error_msg("invalid compressed data--crc error");
1154 if (orig_len != (ulg) bytes_out) {
1155 error_msg("invalid compressed data--length error");
1158 /* Check if there are more entries in a pkzip file */
1159 if (pkzip && inptr + 4 < insize && LG(inbuf + inptr) == LOCSIG) {
1160 error_msg("has more than one entry--rest ignored");
1161 if (exit_code == OK)
1162 exit_code = WARNING;
1164 ext_header = pkzip = 0; /* for next file */
1169 /* ===========================================================================
1170 * Clear input and output buffers
1172 void clear_bufs(void)
1176 bytes_in = bytes_out = 0L;
1179 /* ===========================================================================
1180 * Initialize gunzip buffers and signals
1182 extern int gunzip_init()
1184 foreground = signal(SIGINT, SIG_IGN) != SIG_IGN;
1186 (void) signal(SIGINT, (sig_type) abort_gzip);
1189 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) {
1190 (void) signal(SIGTERM, (sig_type) abort_gzip);
1194 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
1195 (void) signal(SIGHUP, (sig_type) abort_gzip);
1199 /* Allocate all global buffers (for DYN_ALLOC option) */
1200 inbuf = xmalloc((size_t)((INBUFSIZ+INBUF_EXTRA+1L)*sizeof(uch)));
1201 outbuf = xmalloc((size_t)((OUTBUFSIZ+OUTBUF_EXTRA+1L)*sizeof(uch)));
1202 d_buf = xmalloc((size_t)((DIST_BUFSIZE+1L)*sizeof(ush)));
1203 window = xmalloc((size_t)(((2L*WSIZE)+1L)*sizeof(uch)));
1204 tab_prefix0 = xmalloc((size_t)(((1L<<(BITS-1))+1L)*sizeof(ush)));
1205 tab_prefix1 = xmalloc((size_t)(((1L<<(BITS-1))+1L)*sizeof(ush)));
1207 clear_bufs(); /* clear input and output buffers */
1209 return(EXIT_SUCCESS);
1213 /* ======================================================================== */
1214 int gunzip_main(int argc, char **argv)
1221 int delInputFile = 0;
1223 struct stat statBuf;
1225 RESERVE_BB_BUFFER(ifname, MAX_PATH_LEN+1); /* input file name */
1226 RESERVE_BB_BUFFER(ofname, MAX_PATH_LEN+1); /* output file name */
1228 method = DEFLATED; /* default compression method */
1229 exit_code = OK; /* let's go out on a limb and assume everything will run fine (wink wink) */
1231 if (strcmp(applet_name, "zcat") == 0) {
1236 /* Parse any options */
1237 while (--argc > 0 && **(++argv) == '-') {
1238 if (*((*argv) + 1) == '\0') {
1241 while (*(++(*argv))) {
1263 if (isatty(fileno(stdin)) && fromstdin==1 && force==0)
1264 error_msg_and_die( "data not read from terminal. Use -f to force it.");
1265 if (isatty(fileno(stdout)) && tostdout==1 && force==0)
1266 error_msg_and_die( "data not written to terminal. Use -f to force it.");
1270 if (fromstdin == 1) {
1271 strcpy(ofname, "stdin");
1273 inFileNum = fileno(stdin);
1274 ifile_size = -1L; /* convention for unknown size */
1276 /* Open up the input file */
1279 if (strlen(*argv) > MAX_PATH_LEN) {
1280 error_msg(name_too_long);
1283 strcpy(ifname, *argv);
1285 /* Open input fille */
1286 inFileNum = open(ifname, O_RDONLY);
1287 if (inFileNum < 0) {
1291 /* Get the time stamp on the input file. */
1292 result = stat(ifname, &statBuf);
1297 ifile_size = statBuf.st_size;
1300 if (tostdout == 1) {
1301 /* And get to work */
1302 strcpy(ofname, "stdout");
1303 outFileNum = fileno(stdout);
1305 /* Actually do the compression/decompression. */
1306 unzip(inFileNum, outFileNum);
1308 } else if (test_mode) {
1309 /* Actually do the compression/decompression. */
1310 unzip(inFileNum, 2);
1314 /* And get to work */
1315 if (strlen(ifname) > MAX_PATH_LEN - 4) {
1316 error_msg(name_too_long);
1319 strcpy(ofname, ifname);
1320 pos = strstr(ofname, ".gz");
1325 pos = strstr(ofname, ".tgz");
1328 strcat(pos, ".tar");
1333 /* Open output fille */
1334 #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
1335 outFileNum = open(ofname, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW);
1337 outFileNum = open(ofname, O_RDWR | O_CREAT | O_EXCL);
1339 if (outFileNum < 0) {
1343 /* Set permissions on the file */
1344 fchmod(outFileNum, statBuf.st_mode);
1346 /* Actually do the compression/decompression. */
1347 result = unzip(inFileNum, outFileNum);
1351 /* Delete the original file */
1353 delFileName = ifname;
1355 delFileName = ofname;
1357 if (delInputFile == 1 && unlink(delFileName) < 0) {
1358 perror(delFileName);
1359 return EXIT_FAILURE;