blob: eeff9774ad3c1f8022c4c5b68065b56febcfaa07 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Erik Andersen61677fe2000-04-13 01:18:56 +00002/*
3 * Gzip implementation for busybox
4 *
Glenn L McGrathc2bf5ca2000-09-29 06:46:59 +00005 * Based on GNU gzip v1.2.4 Copyright (C) 1992-1993 Jean-loup Gailly.
Erik Andersen61677fe2000-04-13 01:18:56 +00006 *
7 * Originally adjusted for busybox by Sven Rudolph <sr1@inf.tu-dresden.de>
8 * based on gzip sources
9 *
10 * Adjusted further by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
11 * to support files as well as stdin/stdout, and to generally behave itself wrt
12 * command line handling.
13 *
14 * 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.
18 *
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.
23 *
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
27 *
Glenn L McGrathc2bf5ca2000-09-29 06:46:59 +000028 *
29 * gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
Eric Andersenb052b471999-11-18 00:21:59 +000030 * 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.
35 *
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.
38 */
39
40#if 0
Erik Andersene49d5ec2000-02-08 19:58:47 +000041static 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.",
47 "",
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.",
52 "",
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.",
56 0
57};
Eric Andersenb052b471999-11-18 00:21:59 +000058#endif
59
Glenn L McGrathc2bf5ca2000-09-29 06:46:59 +000060#include "busybox.h"
61#include <getopt.h>
Eric Andersenb052b471999-11-18 00:21:59 +000062#include <ctype.h>
63#include <sys/types.h>
64#include <signal.h>
Eric Andersenb052b471999-11-18 00:21:59 +000065#include <errno.h>
Glenn L McGrathc2bf5ca2000-09-29 06:46:59 +000066#include <stdio.h>
67#include <string.h>
68#include <memory.h>
69#include <unistd.h>
70#include <fcntl.h>
71#include <stdlib.h>
72#include <time.h>
73#include <dirent.h>
74#define BB_DECLARE_EXTERN
75#define bb_need_memory_exhausted
76#define bb_need_name_too_long
77#include "messages.c"
Eric Andersenb052b471999-11-18 00:21:59 +000078
79#define RECORD_IO 0
80
Eric Andersenb052b471999-11-18 00:21:59 +000081/* Return codes from gzip */
82#define OK 0
83#define ERROR 1
84#define WARNING 2
85
Glenn L McGrathc2bf5ca2000-09-29 06:46:59 +000086#define DEFLATED 8
87#define INBUFSIZ 0x2000 /* input buffer size */
88#define INBUF_EXTRA 64 /* required by unlzw() */
89#define OUTBUFSIZ 8192 /* output buffer size */
90#define OUTBUF_EXTRA 2048 /* required by unlzw() */
91#define DIST_BUFSIZE 0x2000 /* buffer for distances, see trees.c */
Eric Andersenb052b471999-11-18 00:21:59 +000092
Glenn L McGrathc2bf5ca2000-09-29 06:46:59 +000093#define GZIP_MAGIC "\037\213" /* Magic header for gzip files, 1F 8B */
Eric Andersenb052b471999-11-18 00:21:59 +000094
95/* gzip flag byte */
Glenn L McGrathc2bf5ca2000-09-29 06:46:59 +000096#define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
97#define ORIG_NAME 0x08 /* bit 3 set: original file name present */
98#define COMMENT 0x10 /* bit 4 set: file comment present */
99#define WSIZE 0x8000 /* window size--must be a power of two, and */
100 /* at least 32K for zip's deflate method */
Eric Andersenb052b471999-11-18 00:21:59 +0000101
Glenn L McGrathc2bf5ca2000-09-29 06:46:59 +0000102/* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
103#define BMAX 16 /* maximum bit length of any code (16 for explode) */
104#define N_MAX 288 /* maximum number of codes in any set */
Eric Andersenb052b471999-11-18 00:21:59 +0000105
Glenn L McGrathc2bf5ca2000-09-29 06:46:59 +0000106/* PKZIP header definitions */
107#define LOCSIG 0x04034b50L /* four-byte lead-in (lsb first) */
108#define LOCCRC 14 /* offset of crc */
109#define LOCLEN 22 /* offset of uncompressed length */
110#define EXTHDR 16 /* size of extended local header, inc sig */
Eric Andersenb052b471999-11-18 00:21:59 +0000111
Glenn L McGrathc2bf5ca2000-09-29 06:46:59 +0000112#define BITS 16
Eric Andersenb052b471999-11-18 00:21:59 +0000113
114/* Diagnostic functions */
115#ifdef DEBUG
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000116# define Assert(cond,msg) {if(!(cond)) errorMsg(msg);}
Eric Andersenb052b471999-11-18 00:21:59 +0000117# 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 ;}
122#else
123# define Assert(cond,msg)
124# define Trace(x)
125# define Tracev(x)
126# define Tracevv(x)
127# define Tracec(c,x)
128# define Tracecv(c,x)
129#endif
130
Erik Andersene49d5ec2000-02-08 19:58:47 +0000131#ifndef MAX_PATH_LEN /* max pathname length */
Erik Andersen4f3f7572000-04-28 00:18:56 +0000132# ifdef BUFSIZ
133# define MAX_PATH_LEN BUFSIZ
Erik Andersenfac10d72000-02-07 05:29:42 +0000134# else
135# define MAX_PATH_LEN 1024
136# endif
Eric Andersenb052b471999-11-18 00:21:59 +0000137#endif
138
Glenn L McGrathc2bf5ca2000-09-29 06:46:59 +0000139#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);}
Eric Andersenb052b471999-11-18 00:21:59 +0000142
Glenn L McGrathc2bf5ca2000-09-29 06:46:59 +0000143#define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf(0))
144
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))
148
149 /* in gzip.c */
150void abort_gzip (void);
151typedef void (*sig_type) (int);
152
153typedef unsigned char uch;
154typedef unsigned short ush;
155typedef unsigned long ulg;
156typedef int file_t; /* Do not use stdio */
157
Eric Andersen63a86222000-11-07 06:52:13 +0000158static uch *inbuf;
159static uch *outbuf;
160static ush *d_buf;
161static uch *window;
162static ush *tab_prefix0;
163static ush *tab_prefix1;
Glenn L McGrathc2bf5ca2000-09-29 06:46:59 +0000164
165/* local variables */
Eric Andersen63a86222000-11-07 06:52:13 +0000166static int test_mode = 0; /* check file integrity option */
167static int foreground; /* set if program run in foreground */
168static int method = DEFLATED; /* compression method */
169static int exit_code = OK; /* program exit code */
170static int last_member; /* set for .zip and .Z files */
171static int part_nb; /* number of parts in .gz file */
172static long ifile_size; /* input file size, -1 for devices (debug only) */
173static long bytes_in; /* number of input bytes */
174static long bytes_out; /* number of output bytes */
175static int ifd; /* input file descriptor */
176static int ofd; /* output file descriptor */
177static unsigned insize; /* valid bytes in inbuf */
178static unsigned inptr; /* index of next byte to be processed in inbuf */
179static unsigned outcnt; /* bytes in output buffer */
Glenn L McGrathc2bf5ca2000-09-29 06:46:59 +0000180
181unsigned hufts; /* track memory usage */
182ulg bb; /* bit buffer */
183unsigned bk; /* bits in bit buffer */
184int crc_table_empty = 1;
185
186struct huft {
187 uch e; /* number of extra bits or operation */
188 uch b; /* number of bits in this code or subcode */
189 union {
190 ush n; /* literal, length base, or distance base */
191 struct huft *t; /* pointer to next level of table */
192 } v;
193};
194
195/* Tables for deflate from PKZIP's appnote.txt. */
196static 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
198};
199static 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
202};
203
204/* note: see note #13 above about the 258 in this list. */
205static 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
208}; /* 99==invalid */
209
210static 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
214};
215
216static 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,
219 12, 12, 13, 13
220};
221
222ush mask_bits[] = {
223 0x0000,
224 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
225 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
226};
227
228/* ========================================================================
229 * Error handlers.
230 */
231void read_error_msg()
232{
233 fprintf(stderr, "\n");
234 if (errno != 0) {
235 perror("");
236 } else {
237 fprintf(stderr, "unexpected end of file\n");
238 }
239 abort_gzip();
240}
241
242/* ===========================================================================
243 * Fill the input buffer. This is called only when the buffer is empty.
244 */
245int fill_inbuf(eof_ok)
246int eof_ok; /* set if EOF acceptable as a result */
247{
248 int len;
249
250 /* Read as much as possible */
251 insize = 0;
252 errno = 0;
253 do {
254 len = read(ifd, (char *) inbuf + insize, INBUFSIZ - insize);
255 if (len == 0 || len == EOF)
256 break;
257 insize += len;
258 } while (insize < INBUFSIZ);
259
260 if (insize == 0) {
261 if (eof_ok)
262 return EOF;
263 read_error_msg();
264 }
265 bytes_in += (ulg) insize;
266 inptr = 1;
267 return inbuf[0];
268}
Eric Andersenb052b471999-11-18 00:21:59 +0000269
270
Glenn L McGrathc2bf5ca2000-09-29 06:46:59 +0000271/* ========================================================================
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.
281 */
282static int get_method(in)
283int in; /* input file descriptor */
284{
285 uch flags; /* compression flags */
286 char magic[2]; /* magic header */
287 long header_bytes = 0; /* number of bytes in gzip header */
Eric Andersenb052b471999-11-18 00:21:59 +0000288
Glenn L McGrathc2bf5ca2000-09-29 06:46:59 +0000289 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 */
295
296 if (memcmp(magic, GZIP_MAGIC, 2) == 0) {
297
298 method = (int) get_byte();
299 if (method != DEFLATED) {
300 errorMsg("unknown method %d -- get newer version of gzip\n", method);
301 exit_code = ERROR;
302 return -1;
303 }
304 flags = (uch) get_byte();
305
306 (ulg) get_byte(); /* Ignore time stamp */
307 (ulg) get_byte();
308 (ulg) get_byte();
309 (ulg) get_byte();
310
311 (void) get_byte(); /* Ignore extra flags for the moment */
312 (void) get_byte(); /* Ignore OS type for the moment */
313
314 if ((flags & EXTRA_FIELD) != 0) {
315 unsigned len = (unsigned) get_byte();
316 len |= ((unsigned) get_byte()) << 8;
317 while (len--)
318 (void) get_byte();
319 }
320
321 /* Discard original name if any */
322 if ((flags & ORIG_NAME) != 0) {
323 while (get_byte() != 0); /* null */
324 }
325
326 /* Discard file comment if any */
327 if ((flags & COMMENT) != 0) {
328 while (get_byte() != 0) /* null */
329 ;
330 }
331 if (part_nb == 1) {
332 header_bytes = inptr + 2 * sizeof(long); /* include crc and size */
333 }
334
335 }
336
337 if (method >= 0)
338 return method;
339
340 if (part_nb == 1) {
341 fprintf(stderr, "\nnot in gzip format\n");
342 exit_code = ERROR;
343 return -1;
344 } else {
345 fprintf(stderr, "\ndecompression OK, trailing garbage ignored\n");
346 if (exit_code == OK)
347 exit_code = WARNING;
348 return -2;
349 }
350}
351
352/* ========================================================================
353 * Signal and error handler.
354 */
355void abort_gzip()
356{
357 exit(ERROR);
358}
359
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.
364 */
365ulg updcrc(s, n)
366uch *s; /* pointer to bytes to pump through */
367unsigned n; /* number of bytes in s[] */
368{
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 */
377
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};
380
381 /* Make exclusive-or pattern from polynomial (0xedb88320) */
382 e = 0;
383 for (i = 0; i < sizeof(p)/sizeof(int); i++)
384 e |= 1L << (31 - p[i]);
385
386 /* Compute and print table of CRC's, five per line */
387 crc_32_tab[0] = 0x00000000L;
388 for (i = 1; i < 256; i++) {
389 c = i;
390 /* The idea to initialize the register with the byte instead of
391 * zero was stolen from Haruhiko Okumura's ar002
392 */
393 for (k = 8; k; k--)
394 c = c & 1 ? (c >> 1) ^ e : c >> 1;
395 crc_32_tab[i]=c;
396 }
397 }
398
399 if (s == NULL) {
400 c = 0xffffffffL;
401 } else {
402 c = crc;
403 if (n)
404 do {
405 c = crc_32_tab[((int) c ^ (*s++)) & 0xff] ^ (c >> 8);
406 } while (--n);
407 }
408 crc = c;
409 return c ^ 0xffffffffL; /* (instead of ~c for 64-bit machines) */
410}
411
412void write_error_msg()
413{
414 fprintf(stderr, "\n");
415 perror("");
416 abort_gzip();
417}
418
419/* ===========================================================================
420 * Does the same as write(), but also handles partial pipe writes and checks
421 * for error return.
422 */
423void write_buf(fd, buf, cnt)
424int fd;
425void * buf;
426unsigned cnt;
427{
428 unsigned n;
429
430 while ((n = write(fd, buf, cnt)) != cnt) {
431 if (n == (unsigned) (-1)) {
432 write_error_msg();
433 }
434 cnt -= n;
435 buf = (void *) ((char *) buf + n);
436 }
437}
438
439/* ===========================================================================
440 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
441 * (Used for the decompressed data only.)
442 */
443void flush_window()
444{
445 if (outcnt == 0)
446 return;
447 updcrc(window, outcnt);
448
449 if (!test_mode)
450 write_buf(ofd, (char *) window, outcnt);
451 bytes_out += (ulg) outcnt;
452 outcnt = 0;
453}
454
455int inflate_stored()
456/* "decompress" an inflated type 0 (stored) block. */
457{
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 */
462
463 /* make local copies of globals */
464 b = bb; /* initialize bit buffer */
465 k = bk;
466 w = outcnt; /* initialize window position */
467
468 /* go to byte boundary */
469 n = k & 7;
470 DUMPBITS(n);
471
472 /* get the length and its complement */
473 NEEDBITS(16)
474 n = ((unsigned) b & 0xffff);
475 DUMPBITS(16)
476 NEEDBITS(16)
477 if (n != (unsigned) ((~b) & 0xffff))
478 return 1; /* error in compressed data */
479 DUMPBITS(16)
480
481 /* read and output the compressed data */
482 while (n--) {
483 NEEDBITS(8)
484 window[w++] = (uch) b;
485 if (w == WSIZE) {
486// flush_output(w);
487 outcnt=(w),
488 flush_window();
489 w = 0;
490 }
491 DUMPBITS(8)
492 }
493
494 /* restore the globals from the locals */
495 outcnt = w; /* restore global window pointer */
496 bb = b; /* restore global bit buffer */
497 bk = k;
498 return 0;
499}
500
501int huft_free(t)
502struct huft *t; /* table to free */
503
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
506 each table. */
507{
508 register struct huft *p, *q;
509
510 /* Go through linked list, freeing from the malloced (t[-1]) address. */
511 p = t;
512 while (p != (struct huft *) NULL) {
513 q = (--p)->v.t;
514 free((char *) p);
515 p = q;
516 }
517 return 0;
518}
519
520
521int huft_build(b, n, s, d, e, t, m)
522unsigned *b; /* code lengths in bits (all assumed <= BMAX) */
523unsigned n; /* number of codes (assumed <= N_MAX) */
524unsigned s; /* number of simple-valued codes (0..s-1) */
525ush *d; /* list of base values for non-simple codes */
526ush *e; /* list of extra bits for non-simple codes */
527struct huft **t; /* result: starting table */
528int *m; /* maximum lookup bits, returns actual */
529
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. */
535{
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 */
555
556 /* Generate counts for each bit length */
557 memset ((void *)(c), 0, sizeof(c));
558 p = b;
559 i = n;
560 do {
561 Tracecv(*p,(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) */
564 } while (--i);
565 if (c[0] == n) { /* null input--all zero length codes */
566 *t = (struct huft *) NULL;
567 *m = 0;
568 return 0;
569 }
570
571 /* Find minimum and maximum length, bound *m by those */
572 l = *m;
573 for (j = 1; j <= BMAX; j++)
574 if (c[j])
575 break;
576 k = j; /* minimum code length */
577 if ((unsigned) l < j)
578 l = j;
579 for (i = BMAX; i; i--)
580 if (c[i])
581 break;
582 g = i; /* maximum code length */
583 if ((unsigned) l > i)
584 l = i;
585 *m = l;
586
587 /* Adjust last length count to fill out codes, if needed */
588 for (y = 1 << j; j < i; j++, y <<= 1)
589 if ((y -= c[j]) < 0)
590 return 2; /* bad input: more codes than bits */
591 if ((y -= c[i]) < 0)
592 return 2;
593 c[i] += y;
594
595 /* Generate starting offsets into the value table for each length */
596 x[1] = j = 0;
597 p = c + 1;
598 xp = x + 2;
599 while (--i) { /* note that i == g from above */
600 *xp++ = (j += *p++);
601 }
602
603 /* Make a table of values in order of bit lengths */
604 p = b;
605 i = 0;
606 do {
607 if ((j = *p++) != 0)
608 v[x[j]++] = i;
609 } while (++i < n);
610
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 */
618 z = 0; /* ditto */
619
620 /* go through the bit lengths (k already is bits in shortest code) */
621 for (; k <= g; k++) {
622 a = c[k];
623 while (a--) {
624 /* here i is the Huffman code of length k bits for value *p */
625 /* make tables up to required level */
626 while (k > w + l) {
627 h++;
628 w += l; /* previous table always l bits */
629
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 */
634 xp = c + k;
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 */
639 }
640 }
641 z = 1 << j; /* table entries for j-bit table */
642
643 /* allocate and link in new table */
644 if (
645 (q =
646 (struct huft *) malloc((z + 1) *
647 sizeof(struct huft))) ==
648 (struct huft *) NULL) {
649 if (h)
650 huft_free(u[0]);
651 return 3; /* not enough memory */
652 }
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 */
657
658 /* connect to last table, if there is one */
659 if (h) {
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 */
666 }
667 }
668
669 /* set up table entry in r */
670 r.b = (uch) (k - w);
671 if (p >= v + n)
672 r.e = 99; /* out of values--invalid code */
673 else if (*p < s) {
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++ */
677 } else {
678 r.e = (uch) e[*p - s]; /* non-simple--look up in lists */
679 r.v.n = d[*p++ - s];
680 }
681
682 /* fill code-like entries with r */
683 f = 1 << (k - w);
684 for (j = i >> w; j < z; j += f)
685 q[j] = r;
686
687 /* backwards increment the k-bit code i */
688 for (j = 1 << (k - 1); i & j; j >>= 1)
689 i ^= j;
690 i ^= j;
691
692 /* backup over finished tables */
693 while ((i & ((1 << w) - 1)) != x[h]) {
694 h--; /* don't need to update q */
695 w -= l;
696 }
697 }
698 }
699 /* Return true (1) if we were given an incomplete table */
700 return y != 0 && g != 1;
701}
702
703
704int inflate_codes(tl, td, bl, bd)
705struct huft *tl, *td; /* literal/length and distance decoder tables */
706int bl, bd; /* number of bits decoded by tl[] and td[] */
707
708/* inflate (decompress) the codes in a deflated (compressed) block.
709 Return an error code or zero if it all goes ok. */
710{
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 */
718
719 /* make local copies of globals */
720 b = bb; /* initialize bit buffer */
721 k = bk;
722 w = outcnt; /* initialize window position */
723
724 /* inflate the coded data */
725 ml = mask_bits[bl]; /* precompute masks for speed */
726 md = mask_bits[bd];
727 for (;;) { /* do until end of block */
728 NEEDBITS((unsigned) bl)
729 if ((e = (t = tl + ((unsigned) b & ml))->e) > 16)
730 do {
731 if (e == 99)
732 return 1;
733 DUMPBITS(t->b)
734 e -= 16;
735 NEEDBITS(e)
736 } while ((e = (t = t->v.t + ((unsigned) b & mask_bits[e]))->e)
737 > 16);
738 DUMPBITS(t->b)
739 if (e == 16) { /* then it's a literal */
740 window[w++] = (uch) t->v.n;
741 Tracevv((stderr, "%c", window[w - 1]));
742 if (w == WSIZE) {
743// flush_output(w);
744 outcnt=(w),
745 flush_window();
746 w = 0;
747 }
748 } else { /* it's an EOB or a length */
749
750 /* exit if end of block */
751 if (e == 15)
752 break;
753
754 /* get length of block to copy */
755 NEEDBITS(e)
756 n = t->v.n + ((unsigned) b & mask_bits[e]);
757 DUMPBITS(e);
758
759 /* decode distance of block to copy */
760 NEEDBITS((unsigned) bd)
761 if ((e = (t = td + ((unsigned) b & md))->e) > 16)
762 do {
763 if (e == 99)
764 return 1;
765 DUMPBITS(t->b)
766 e -= 16;
767 NEEDBITS(e)
768 }
769 while (
770 (e =
771 (t =
772 t->v.t + ((unsigned) b & mask_bits[e]))->e) >
773 16);
774 DUMPBITS(t->b)
775 NEEDBITS(e)
776 d = w - t->v.n - ((unsigned) b & mask_bits[e]);
777 DUMPBITS(e)
778 Tracevv((stderr, "\\[%d,%d]", w - d, n));
779
780 /* do the copy */
781 do {
782 n -= (e =
783 (e =
784 WSIZE - ((d &= WSIZE - 1) > w ? d : w)) >
785 n ? n : e);
786#if !defined(NOMEMCPY) && !defined(DEBUG)
787 if (w - d >= e) { /* (this test assumes unsigned comparison) */
788 memcpy(window + w, window + d, e);
789 w += e;
790 d += e;
791 } else /* do it slow to avoid memcpy() overlap */
792#endif /* !NOMEMCPY */
793 do {
794 window[w++] = window[d++];
795 Tracevv((stderr, "%c", window[w - 1]));
796 } while (--e);
797 if (w == WSIZE) {
798// flush_output(w);
799 outcnt=(w),
800 flush_window();
801 w = 0;
802 }
803 } while (n);
804 }
805 }
806
807 /* restore the globals from the locals */
808 outcnt = w; /* restore global window pointer */
809 bb = b; /* restore global bit buffer */
810 bk = k;
811
812 /* done */
813 return 0;
814}
815
816int inflate_fixed()
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
819 Huffman tables. */
820{
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 */
827
828 /* set up literal table */
829 for (i = 0; i < 144; i++)
830 l[i] = 8;
831 for (; i < 256; i++)
832 l[i] = 9;
833 for (; i < 280; i++)
834 l[i] = 7;
835 for (; i < 288; i++) /* make a complete, but wrong code set */
836 l[i] = 8;
837 bl = 7;
838 if ((i = huft_build(l, 288, 257, cplens, cplext, &tl, &bl)) != 0)
839 return i;
840
841 /* set up distance table */
842 for (i = 0; i < 30; i++) /* make an incomplete code set */
843 l[i] = 5;
844 bd = 5;
845 if ((i = huft_build(l, 30, 0, cpdist, cpdext, &td, &bd)) > 1) {
846 huft_free(tl);
847 return i;
848 }
849
850 /* decompress until an end-of-block code */
851 if (inflate_codes(tl, td, bl, bd))
852 return 1;
853
854 /* free the decoding tables, return */
855 huft_free(tl);
856 huft_free(td);
857 return 0;
858}
859
860int inflate_dynamic()
861/* decompress an inflated type 2 (dynamic Huffman codes) block. */
862{
863 int dbits = 6; /* bits in base distance lookup table */
864 int lbits = 9; /* bits in base literal/length lookup table */
865
866 int i; /* temporary variables */
867 unsigned j;
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 */
878
879#ifdef PKZIP_BUG_WORKAROUND
880 unsigned ll[288 + 32]; /* literal/length and distance code lengths */
Eric Andersenb052b471999-11-18 00:21:59 +0000881#else
Glenn L McGrathc2bf5ca2000-09-29 06:46:59 +0000882 unsigned ll[286 + 30]; /* literal/length and distance code lengths */
Eric Andersenb052b471999-11-18 00:21:59 +0000883#endif
Glenn L McGrathc2bf5ca2000-09-29 06:46:59 +0000884 register ulg b; /* bit buffer */
885 register unsigned k; /* number of bits in bit buffer */
Eric Andersenb052b471999-11-18 00:21:59 +0000886
Glenn L McGrathc2bf5ca2000-09-29 06:46:59 +0000887 /* make local bit buffer */
888 b = bb;
889 k = bk;
Eric Andersenb052b471999-11-18 00:21:59 +0000890
Glenn L McGrathc2bf5ca2000-09-29 06:46:59 +0000891 /* read in table lengths */
892 NEEDBITS(5)
893 nl = 257 + ((unsigned) b & 0x1f); /* number of literal/length codes */
894 DUMPBITS(5)
895 NEEDBITS(5)
896 nd = 1 + ((unsigned) b & 0x1f); /* number of distance codes */
897 DUMPBITS(5)
898 NEEDBITS(4)
899 nb = 4 + ((unsigned) b & 0xf); /* number of bit length codes */
900 DUMPBITS(4)
901#ifdef PKZIP_BUG_WORKAROUND
902 if (nl > 288 || nd > 32)
903#else
904 if (nl > 286 || nd > 30)
905#endif
906 return 1; /* bad lengths */
Eric Andersenb052b471999-11-18 00:21:59 +0000907
Glenn L McGrathc2bf5ca2000-09-29 06:46:59 +0000908 /* read in bit-length-code lengths */
909 for (j = 0; j < nb; j++) {
910 NEEDBITS(3)
911 ll[border[j]] = (unsigned) b & 7;
912 DUMPBITS(3)
913 }
914 for (; j < 19; j++)
915 ll[border[j]] = 0;
Eric Andersenb052b471999-11-18 00:21:59 +0000916
Glenn L McGrathc2bf5ca2000-09-29 06:46:59 +0000917 /* build decoding table for trees--single level, 7 bit lookup */
918 bl = 7;
919 if ((i = huft_build(ll, 19, 19, NULL, NULL, &tl, &bl)) != 0) {
920 if (i == 1)
921 huft_free(tl);
922 return i; /* incomplete code set */
923 }
Eric Andersenb052b471999-11-18 00:21:59 +0000924
Glenn L McGrathc2bf5ca2000-09-29 06:46:59 +0000925 /* read in literal and distance code lengths */
926 n = nl + nd;
927 m = mask_bits[bl];
928 i = l = 0;
929 while ((unsigned) i < n) {
930 NEEDBITS((unsigned) bl)
931 j = (td = tl + ((unsigned) b & m))->b;
932 DUMPBITS(j)
933 j = td->v.n;
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 */
937 NEEDBITS(2)
938 j = 3 + ((unsigned) b & 3);
939 DUMPBITS(2)
940 if ((unsigned) i + j > n)
941 return 1;
942 while (j--)
943 ll[i++] = l;
944 } else if (j == 17) { /* 3 to 10 zero length codes */
945 NEEDBITS(3)
946 j = 3 + ((unsigned) b & 7);
947 DUMPBITS(3)
948 if ((unsigned) i + j > n)
949 return 1;
950 while (j--)
951 ll[i++] = 0;
952 l = 0;
953 } else { /* j == 18: 11 to 138 zero length codes */
Eric Andersenb052b471999-11-18 00:21:59 +0000954
Glenn L McGrathc2bf5ca2000-09-29 06:46:59 +0000955 NEEDBITS(7)
956 j = 11 + ((unsigned) b & 0x7f);
957 DUMPBITS(7)
958 if ((unsigned) i + j > n)
959 return 1;
960 while (j--)
961 ll[i++] = 0;
962 l = 0;
963 }
964 }
Eric Andersenb052b471999-11-18 00:21:59 +0000965
Glenn L McGrathc2bf5ca2000-09-29 06:46:59 +0000966 /* free decoding table for trees */
967 huft_free(tl);
968
969 /* restore the global bit buffer */
970 bb = b;
971 bk = k;
972
973 /* build the decoding tables for literal/length and distance codes */
974 bl = lbits;
975 if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0) {
976 if (i == 1) {
977 fprintf(stderr, " incomplete literal tree\n");
978 huft_free(tl);
979 }
980 return i; /* incomplete code set */
981 }
982 bd = dbits;
983 if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0) {
984 if (i == 1) {
985 fprintf(stderr, " incomplete distance tree\n");
986 huft_free(td);
987 }
988 huft_free(tl);
989 return i; /* incomplete code set */
990 }
991
992 /* decompress until an end-of-block code */
993 if (inflate_codes(tl, td, bl, bd))
994 return 1;
995
996 /* free the decoding tables, return */
997 huft_free(tl);
998 huft_free(td);
999 return 0;
1000}
1001
1002/* decompress an inflated block */
1003int inflate_block(e)
1004int *e; /* last block flag */
1005{
1006 unsigned t; /* block type */
1007 register ulg b; /* bit buffer */
1008 register unsigned k; /* number of bits in bit buffer */
1009
1010 /* make local bit buffer */
1011 b = bb;
1012 k = bk;
1013
1014 /* read in last block bit */
1015 NEEDBITS(1)
1016 * e = (int) b & 1;
1017 DUMPBITS(1)
1018
1019 /* read in block type */
1020 NEEDBITS(2)
1021 t = (unsigned) b & 3;
1022 DUMPBITS(2)
1023
1024 /* restore the global bit buffer */
1025 bb = b;
1026 bk = k;
1027
1028 /* inflate that block type */
1029 if (t == 2)
1030 return inflate_dynamic();
1031 if (t == 0)
1032 return inflate_stored();
1033 if (t == 1)
1034 return inflate_fixed();
1035
1036 /* bad block type */
1037 return 2;
1038}
1039
1040int inflate()
1041/* decompress an inflated entry */
1042{
1043 int e; /* last block flag */
1044 int r; /* result code */
1045 unsigned h; /* maximum struct huft's malloc'ed */
1046
1047 /* initialize window, bit buffer */
1048 outcnt = 0;
1049 bk = 0;
1050 bb = 0;
1051
1052 /* decompress until the last block */
1053 h = 0;
1054 do {
1055 hufts = 0;
1056 if ((r = inflate_block(&e)) != 0)
1057 return r;
1058 if (hufts > h)
1059 h = hufts;
1060 } while (!e);
1061
1062 /* Undo too much lookahead. The next read will be byte aligned so we
1063 * can discard unused bits in the last meaningful byte.
1064 */
1065 while (bk >= 8) {
1066 bk -= 8;
1067 inptr--;
1068 }
1069
1070 /* flush out window */
1071 outcnt=(outcnt),
1072 flush_window();
1073 /* return success */
1074#ifdef DEBUG
1075 fprintf(stderr, "<%u> ", h);
1076#endif /* DEBUG */
1077 return 0;
1078}
1079
1080/* ===========================================================================
1081 * Unzip in to out. This routine works on both gzip and pkzip files.
1082 *
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.
1086 */
1087int unzip(in, out)
1088int in, out; /* input and output file descriptors */
1089{
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 */
1094 int n;
1095 uch buf[EXTHDR]; /* extended local header */
1096
1097 ifd = in;
1098 ofd = out;
1099 method = get_method(ifd);
1100 if (method < 0) {
1101 exit(exit_code); /* error message already emitted */
1102 }
1103
1104 updcrc(NULL, 0); /* initialize crc */
1105
1106 if (pkzip && !ext_header) { /* crc and length at the end otherwise */
1107 orig_crc = LG(inbuf + LOCCRC);
1108 orig_len = LG(inbuf + LOCLEN);
1109 }
1110
1111 /* Decompress */
1112 if (method == DEFLATED) {
1113
1114 int res = inflate();
1115
1116 if (res == 3) {
1117 errorMsg(memory_exhausted);
1118 } else if (res != 0) {
Matt Kraai207061a2000-10-23 18:03:46 +00001119 errorMsg("invalid compressed data--format violated\n");
Glenn L McGrathc2bf5ca2000-09-29 06:46:59 +00001120 }
1121
1122 } else {
Matt Kraai207061a2000-10-23 18:03:46 +00001123 errorMsg("internal error, invalid method\n");
Glenn L McGrathc2bf5ca2000-09-29 06:46:59 +00001124 }
1125
1126 /* Get the crc and original length */
1127 if (!pkzip) {
1128 /* crc32 (see algorithm.doc)
1129 * uncompressed input size modulo 2^32
1130 */
1131 for (n = 0; n < 8; n++) {
1132 buf[n] = (uch) get_byte(); /* may cause an error if EOF */
1133 }
1134 orig_crc = LG(buf);
1135 orig_len = LG(buf + 4);
1136
1137 } else if (ext_header) { /* If extended header, check it */
1138 /* signature - 4bytes: 0x50 0x4b 0x07 0x08
1139 * CRC-32 value
1140 * compressed size 4-bytes
1141 * uncompressed size 4-bytes
1142 */
1143 for (n = 0; n < EXTHDR; n++) {
1144 buf[n] = (uch) get_byte(); /* may cause an error if EOF */
1145 }
1146 orig_crc = LG(buf + 4);
1147 orig_len = LG(buf + 12);
1148 }
1149
1150 /* Validate decompression */
1151 if (orig_crc != updcrc(outbuf, 0)) {
Matt Kraai207061a2000-10-23 18:03:46 +00001152 errorMsg("invalid compressed data--crc error\n");
Glenn L McGrathc2bf5ca2000-09-29 06:46:59 +00001153 }
1154 if (orig_len != (ulg) bytes_out) {
Matt Kraai207061a2000-10-23 18:03:46 +00001155 errorMsg("invalid compressed data--length error\n");
Glenn L McGrathc2bf5ca2000-09-29 06:46:59 +00001156 }
1157
1158 /* Check if there are more entries in a pkzip file */
1159 if (pkzip && inptr + 4 < insize && LG(inbuf + inptr) == LOCSIG) {
1160 fprintf(stderr, "has more than one entry--rest ignored\n");
1161 if (exit_code == OK)
1162 exit_code = WARNING;
1163 }
1164 ext_header = pkzip = 0; /* for next file */
1165 return OK;
1166}
1167
1168
1169/* ===========================================================================
1170 * Clear input and output buffers
1171 */
1172void clear_bufs(void)
1173{
1174 outcnt = 0;
1175 insize = inptr = 0;
1176 bytes_in = bytes_out = 0L;
1177}
1178
Eric Andersenb052b471999-11-18 00:21:59 +00001179/* ======================================================================== */
Erik Andersene49d5ec2000-02-08 19:58:47 +00001180int gunzip_main(int argc, char **argv)
Eric Andersenb052b471999-11-18 00:21:59 +00001181{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001182 int file_count; /* number of files to precess */
Eric Andersenea824fb2000-07-21 22:17:39 +00001183 int tostdout = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001184 int fromstdin = 0;
1185 int result;
1186 int inFileNum;
1187 int outFileNum;
1188 int delInputFile = 0;
Eric Andersenea824fb2000-07-21 22:17:39 +00001189 int force = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001190 struct stat statBuf;
1191 char *delFileName;
1192 char ifname[MAX_PATH_LEN + 1]; /* input file name */
1193 char ofname[MAX_PATH_LEN + 1]; /* output file name */
Eric Andersenb052b471999-11-18 00:21:59 +00001194
Matt Kraaie58771e2000-07-12 15:38:49 +00001195 if (strcmp(applet_name, "zcat") == 0) {
Eric Andersene99674a2000-09-01 00:41:10 +00001196 force = 1;
Eric Andersen73bdf132000-09-01 16:25:33 +00001197 tostdout = 1;
Erik Andersen59b9e872000-05-10 05:05:45 +00001198 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001199
1200 /* Parse any options */
1201 while (--argc > 0 && **(++argv) == '-') {
1202 if (*((*argv) + 1) == '\0') {
Eric Andersenea824fb2000-07-21 22:17:39 +00001203 tostdout = 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001204 }
1205 while (*(++(*argv))) {
1206 switch (**argv) {
1207 case 'c':
Eric Andersenea824fb2000-07-21 22:17:39 +00001208 tostdout = 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001209 break;
1210 case 't':
1211 test_mode = 1;
1212 break;
Eric Andersenea824fb2000-07-21 22:17:39 +00001213 case 'f':
1214 force = 1;
1215 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001216 default:
1217 usage(gunzip_usage);
1218 }
1219 }
1220 }
Eric Andersene99674a2000-09-01 00:41:10 +00001221
1222 if (argc <= 0) {
1223 tostdout = 1;
Eric Andersenea824fb2000-07-21 22:17:39 +00001224 fromstdin = 1;
Eric Andersene99674a2000-09-01 00:41:10 +00001225 }
Eric Andersenea824fb2000-07-21 22:17:39 +00001226
1227 if (isatty(fileno(stdin)) && fromstdin==1 && force==0)
1228 fatalError( "data not read from terminal. Use -f to force it.\n");
1229 if (isatty(fileno(stdout)) && tostdout==1 && force==0)
1230 fatalError( "data not written to terminal. Use -f to force it.\n");
1231
Erik Andersene49d5ec2000-02-08 19:58:47 +00001232
1233 foreground = signal(SIGINT, SIG_IGN) != SIG_IGN;
1234 if (foreground) {
1235 (void) signal(SIGINT, (sig_type) abort_gzip);
1236 }
Eric Andersenb052b471999-11-18 00:21:59 +00001237#ifdef SIGTERM
Erik Andersene49d5ec2000-02-08 19:58:47 +00001238 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) {
1239 (void) signal(SIGTERM, (sig_type) abort_gzip);
1240 }
Eric Andersenb052b471999-11-18 00:21:59 +00001241#endif
1242#ifdef SIGHUP
Erik Andersene49d5ec2000-02-08 19:58:47 +00001243 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
1244 (void) signal(SIGHUP, (sig_type) abort_gzip);
1245 }
Eric Andersenb052b471999-11-18 00:21:59 +00001246#endif
1247
Erik Andersene49d5ec2000-02-08 19:58:47 +00001248 file_count = argc - optind;
Eric Andersenb052b471999-11-18 00:21:59 +00001249
Erik Andersene49d5ec2000-02-08 19:58:47 +00001250 /* Allocate all global buffers (for DYN_ALLOC option) */
Glenn L McGrathc2bf5ca2000-09-29 06:46:59 +00001251 inbuf = xmalloc((size_t)((INBUFSIZ+INBUF_EXTRA+1L)*sizeof(uch)));
1252 outbuf = xmalloc((size_t)((OUTBUFSIZ+OUTBUF_EXTRA+1L)*sizeof(uch)));
1253 d_buf = xmalloc((size_t)((DIST_BUFSIZE+1L)*sizeof(ush)));
1254 window = xmalloc((size_t)(((2L*WSIZE)+1L)*sizeof(uch)));
1255 tab_prefix0 = xmalloc((size_t)(((1L<<(BITS-1))+1L)*sizeof(ush)));
1256 tab_prefix1 = xmalloc((size_t)(((1L<<(BITS-1))+1L)*sizeof(ush)));
Eric Andersenb052b471999-11-18 00:21:59 +00001257
Erik Andersene49d5ec2000-02-08 19:58:47 +00001258 if (fromstdin == 1) {
1259 strcpy(ofname, "stdin");
Eric Andersenb052b471999-11-18 00:21:59 +00001260
Erik Andersene49d5ec2000-02-08 19:58:47 +00001261 inFileNum = fileno(stdin);
1262 ifile_size = -1L; /* convention for unknown size */
Eric Andersenb052b471999-11-18 00:21:59 +00001263 } else {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001264 /* Open up the input file */
Eric Andersenea824fb2000-07-21 22:17:39 +00001265 if (argc <= 0)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001266 usage(gunzip_usage);
1267 if (strlen(*argv) > MAX_PATH_LEN) {
Matt Kraaid537a952000-07-14 01:51:25 +00001268 errorMsg(name_too_long);
Erik Andersen61677fe2000-04-13 01:18:56 +00001269 exit(WARNING);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001270 }
1271 strcpy(ifname, *argv);
1272
1273 /* Open input fille */
1274 inFileNum = open(ifname, O_RDONLY);
1275 if (inFileNum < 0) {
1276 perror(ifname);
Erik Andersen61677fe2000-04-13 01:18:56 +00001277 exit(WARNING);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001278 }
1279 /* Get the time stamp on the input file. */
1280 result = stat(ifname, &statBuf);
1281 if (result < 0) {
1282 perror(ifname);
Erik Andersen61677fe2000-04-13 01:18:56 +00001283 exit(WARNING);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001284 }
1285 ifile_size = statBuf.st_size;
Eric Andersenb052b471999-11-18 00:21:59 +00001286 }
1287
Eric Andersenea824fb2000-07-21 22:17:39 +00001288 if (tostdout == 1) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001289 /* And get to work */
1290 strcpy(ofname, "stdout");
1291 outFileNum = fileno(stdout);
1292
1293 clear_bufs(); /* clear input and output buffers */
1294 part_nb = 0;
1295
1296 /* Actually do the compression/decompression. */
1297 unzip(inFileNum, outFileNum);
1298
1299 } else if (test_mode) {
1300 /* Actually do the compression/decompression. */
1301 unzip(inFileNum, 2);
1302 } else {
1303 char *pos;
1304
1305 /* And get to work */
1306 if (strlen(ifname) > MAX_PATH_LEN - 4) {
Matt Kraaid537a952000-07-14 01:51:25 +00001307 errorMsg(name_too_long);
Erik Andersen61677fe2000-04-13 01:18:56 +00001308 exit(WARNING);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001309 }
1310 strcpy(ofname, ifname);
1311 pos = strstr(ofname, ".gz");
1312 if (pos != NULL) {
1313 *pos = '\0';
1314 delInputFile = 1;
1315 } else {
1316 pos = strstr(ofname, ".tgz");
1317 if (pos != NULL) {
1318 *pos = '\0';
1319 strcat(pos, ".tar");
1320 delInputFile = 1;
1321 }
1322 }
1323
1324 /* Open output fille */
Erik Andersen4d1d0111999-12-17 18:44:15 +00001325#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001326 outFileNum = open(ofname, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW);
Erik Andersen4d1d0111999-12-17 18:44:15 +00001327#else
Erik Andersene49d5ec2000-02-08 19:58:47 +00001328 outFileNum = open(ofname, O_RDWR | O_CREAT | O_EXCL);
Erik Andersen4d1d0111999-12-17 18:44:15 +00001329#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +00001330 if (outFileNum < 0) {
1331 perror(ofname);
Erik Andersen61677fe2000-04-13 01:18:56 +00001332 exit(WARNING);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001333 }
1334 /* Set permissions on the file */
1335 fchmod(outFileNum, statBuf.st_mode);
1336
1337 clear_bufs(); /* clear input and output buffers */
1338 part_nb = 0;
1339
1340 /* Actually do the compression/decompression. */
1341 result = unzip(inFileNum, outFileNum);
1342
1343 close(outFileNum);
1344 close(inFileNum);
1345 /* Delete the original file */
1346 if (result == OK)
1347 delFileName = ifname;
1348 else
1349 delFileName = ofname;
1350
1351 if (delInputFile == 1 && unlink(delFileName) < 0) {
1352 perror(delFileName);
Matt Kraai3e856ce2000-12-01 02:55:13 +00001353 return EXIT_FAILURE;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001354 }
Eric Andersenb052b471999-11-18 00:21:59 +00001355 }
Eric Andersenb6106152000-06-19 17:25:40 +00001356 return(exit_code);
Eric Andersenb052b471999-11-18 00:21:59 +00001357}