aboutsummaryrefslogtreecommitdiff
path: root/gunzip.c
diff options
context:
space:
mode:
Diffstat (limited to 'gunzip.c')
-rw-r--r--gunzip.c170
1 files changed, 90 insertions, 80 deletions
diff --git a/gunzip.c b/gunzip.c
index 7aa896469..1a5cdba88 100644
--- a/gunzip.c
+++ b/gunzip.c
@@ -1,13 +1,33 @@
1/* vi: set sw=4 ts=4: */ 1/* vi: set sw=4 ts=4: */
2/* zcat : stripped version based on gzip sources 2/*
3 Sven Rudolph <sr1@inf.tu-dresden.de> 3 * Gzip implementation for busybox
4 */ 4 *
5 * Based on GNU gzip Copyright (C) 1992-1993 Jean-loup Gailly.
6 *
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 *
28 */
5 29
6#include "internal.h" 30#include "internal.h"
7#define bb_need_name_too_long
8#define BB_DECLARE_EXTERN
9#include "messages.c"
10
11static const char gunzip_usage[] = 31static const char gunzip_usage[] =
12 "gunzip [OPTION]... FILE\n\n" 32 "gunzip [OPTION]... FILE\n\n"
13 "Uncompress FILE (or standard input if FILE is '-').\n\n" 33 "Uncompress FILE (or standard input if FILE is '-').\n\n"
@@ -16,6 +36,18 @@ static const char gunzip_usage[] =
16 "\t-c\tWrite output to standard output\n" 36 "\t-c\tWrite output to standard output\n"
17 "\t-t\tTest compressed file integrity\n"; 37 "\t-t\tTest compressed file integrity\n";
18 38
39
40 /* These defines are very important for BusyBox. Without these,
41 * huge chunks of ram are pre-allocated making the BusyBox bss
42 * size Freaking Huge(tm), which is a bad thing.*/
43#define SMALL_MEM
44#define DYN_ALLOC
45
46#define bb_need_name_too_long
47#define BB_DECLARE_EXTERN
48#include "messages.c"
49
50
19/* gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface 51/* gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface
20 * Copyright (C) 1992-1993 Jean-loup Gailly 52 * Copyright (C) 1992-1993 Jean-loup Gailly
21 * The unzip code was written and put in the public domain by Mark Adler. 53 * The unzip code was written and put in the public domain by Mark Adler.
@@ -89,25 +121,6 @@ static char *license_msg[] = {
89#define get_char() get_byte() 121#define get_char() get_byte()
90#define put_char(c) put_byte(c) 122#define put_char(c) put_byte(c)
91 123
92/* #include "gzip.h" */
93
94/* gzip.h -- common declarations for all gzip modules
95 * Copyright (C) 1992-1993 Jean-loup Gailly.
96 * This is free software; you can redistribute it and/or modify it under the
97 * terms of the GNU General Public License, see the file COPYING.
98 */
99
100#if defined(__STDC__) || defined(PROTO)
101# define OF(args) args
102#else
103# define OF(args) ()
104#endif
105
106#ifdef __STDC__
107typedef void *voidp;
108#else
109typedef char *voidp;
110#endif
111 124
112/* I don't like nested includes, but the string and io functions are used 125/* I don't like nested includes, but the string and io functions are used
113 * too often 126 * too often
@@ -118,7 +131,7 @@ typedef char *voidp;
118# if !defined(STDC_HEADERS) && !defined(NO_MEMORY_H) && !defined(__GNUC__) 131# if !defined(STDC_HEADERS) && !defined(NO_MEMORY_H) && !defined(__GNUC__)
119# include <memory.h> 132# include <memory.h>
120# endif 133# endif
121# define memzero(s, n) memset ((voidp)(s), 0, (n)) 134# define memzero(s, n) memset ((void *)(s), 0, (n))
122#else 135#else
123# include <strings.h> 136# include <strings.h>
124# define strchr index 137# define strchr index
@@ -329,49 +342,46 @@ extern int save_orig_name; /* set if original name must be saved */
329#define WARN(msg) {fprintf msg ; \ 342#define WARN(msg) {fprintf msg ; \
330 if (exit_code == OK) exit_code = WARNING;} 343 if (exit_code == OK) exit_code = WARNING;}
331 344
332#define do_exit(c) exit(c)
333
334
335 /* in unzip.c */ 345 /* in unzip.c */
336extern int unzip OF((int in, int out)); 346extern int unzip (int in, int out);
337 347
338 /* in gzip.c */ 348 /* in gzip.c */
339RETSIGTYPE abort_gzip OF((void)); 349RETSIGTYPE abort_gzip (void);
340 350
341 /* in deflate.c */ 351 /* in deflate.c */
342void lm_init OF((int pack_level, ush * flags)); 352void lm_init (int pack_level, ush * flags);
343ulg deflate OF((void)); 353ulg deflate (void);
344 354
345 /* in trees.c */ 355 /* in trees.c */
346void ct_init OF((ush * attr, int *method)); 356void ct_init (ush * attr, int *method);
347int ct_tally OF((int dist, int lc)); 357int ct_tally (int dist, int lc);
348ulg flush_block OF((char *buf, ulg stored_len, int eof)); 358ulg flush_block (char *buf, ulg stored_len, int eof);
349 359
350 /* in bits.c */ 360 /* in bits.c */
351void bi_init OF((file_t zipfile)); 361void bi_init (file_t zipfile);
352void send_bits OF((int value, int length)); 362void send_bits (int value, int length);
353unsigned bi_reverse OF((unsigned value, int length)); 363unsigned bi_reverse (unsigned value, int length);
354void bi_windup OF((void)); 364void bi_windup (void);
355void copy_block OF((char *buf, unsigned len, int header)); 365void copy_block (char *buf, unsigned len, int header);
356extern int (*read_buf) OF((char *buf, unsigned size)); 366extern int (*read_buf) (char *buf, unsigned size);
357 367
358 /* in util.c: */ 368 /* in util.c: */
359extern int copy OF((int in, int out)); 369extern int copy (int in, int out);
360extern ulg updcrc OF((uch * s, unsigned n)); 370extern ulg updcrc (uch * s, unsigned n);
361extern void clear_bufs OF((void)); 371extern void clear_bufs (void);
362extern int fill_inbuf OF((int eof_ok)); 372extern int fill_inbuf (int eof_ok);
363extern void flush_outbuf OF((void)); 373extern void flush_outbuf (void);
364extern void flush_window OF((void)); 374extern void flush_window (void);
365extern void write_buf OF((int fd, voidp buf, unsigned cnt)); 375extern void write_buf (int fd, void * buf, unsigned cnt);
366 376
367#ifndef __linux__ 377#ifndef __linux__
368extern char *basename OF((char *fname)); 378extern char *basename (char *fname);
369#endif /* not __linux__ */ 379#endif /* not __linux__ */
370extern void read_error OF((void)); 380extern void read_error (void);
371extern void write_error OF((void)); 381extern void write_error (void);
372 382
373 /* in inflate.c */ 383 /* in inflate.c */
374extern int inflate OF((void)); 384extern int inflate (void);
375 385
376/* #include "lzw.h" */ 386/* #include "lzw.h" */
377 387
@@ -415,8 +425,8 @@ extern int inflate OF((void));
415extern int maxbits; /* max bits per code for LZW */ 425extern int maxbits; /* max bits per code for LZW */
416extern int block_mode; /* block compress mode -C compatible with 2.0 */ 426extern int block_mode; /* block compress mode -C compatible with 2.0 */
417 427
418extern int lzw OF((int in, int out)); 428extern int lzw (int in, int out);
419extern int unlzw OF((int in, int out)); 429extern int unlzw (int in, int out);
420 430
421 431
422/* #include "revision.h" */ 432/* #include "revision.h" */
@@ -605,7 +615,7 @@ typedef struct direct dir_type;
605#if !defined(S_ISREG) && defined(S_IFREG) 615#if !defined(S_ISREG) && defined(S_IFREG)
606# define S_ISREG(m) (((m) & S_IFMT) == S_IFREG) 616# define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
607#endif 617#endif
608typedef RETSIGTYPE(*sig_type) OF((int)); 618typedef RETSIGTYPE(*sig_type) (int);
609 619
610#ifndef O_BINARY 620#ifndef O_BINARY
611# define O_BINARY 0 /* creation mode for open() */ 621# define O_BINARY 0 /* creation mode for open() */
@@ -644,7 +654,7 @@ typedef RETSIGTYPE(*sig_type) OF((int));
644 654
645#ifdef NO_OFF_T 655#ifdef NO_OFF_T
646typedef long off_t; 656typedef long off_t;
647off_t lseek OF((int fd, off_t offset, int whence)); 657off_t lseek (int fd, off_t offset, int whence);
648#endif 658#endif
649 659
650 660
@@ -687,7 +697,7 @@ long header_bytes; /* number of bytes in gzip header */
687 697
688/* local functions */ 698/* local functions */
689 699
690local int get_method OF((int in)); 700local int get_method (int in);
691 701
692#define strequ(s1, s2) (strcmp((s1),(s2)) == 0) 702#define strequ(s1, s2) (strcmp((s1),(s2)) == 0)
693 703
@@ -773,7 +783,7 @@ int gunzip_main(int argc, char **argv)
773 usage(gunzip_usage); 783 usage(gunzip_usage);
774 if (strlen(*argv) > MAX_PATH_LEN) { 784 if (strlen(*argv) > MAX_PATH_LEN) {
775 fprintf(stderr, name_too_long, "gunzip"); 785 fprintf(stderr, name_too_long, "gunzip");
776 do_exit(WARNING); 786 exit(WARNING);
777 } 787 }
778 strcpy(ifname, *argv); 788 strcpy(ifname, *argv);
779 789
@@ -781,13 +791,13 @@ int gunzip_main(int argc, char **argv)
781 inFileNum = open(ifname, O_RDONLY); 791 inFileNum = open(ifname, O_RDONLY);
782 if (inFileNum < 0) { 792 if (inFileNum < 0) {
783 perror(ifname); 793 perror(ifname);
784 do_exit(WARNING); 794 exit(WARNING);
785 } 795 }
786 /* Get the time stamp on the input file. */ 796 /* Get the time stamp on the input file. */
787 result = stat(ifname, &statBuf); 797 result = stat(ifname, &statBuf);
788 if (result < 0) { 798 if (result < 0) {
789 perror(ifname); 799 perror(ifname);
790 do_exit(WARNING); 800 exit(WARNING);
791 } 801 }
792 ifile_size = statBuf.st_size; 802 ifile_size = statBuf.st_size;
793 } 803 }
@@ -812,7 +822,7 @@ int gunzip_main(int argc, char **argv)
812 /* And get to work */ 822 /* And get to work */
813 if (strlen(ifname) > MAX_PATH_LEN - 4) { 823 if (strlen(ifname) > MAX_PATH_LEN - 4) {
814 fprintf(stderr, name_too_long, "gunzip"); 824 fprintf(stderr, name_too_long, "gunzip");
815 do_exit(WARNING); 825 exit(WARNING);
816 } 826 }
817 strcpy(ofname, ifname); 827 strcpy(ofname, ifname);
818 pos = strstr(ofname, ".gz"); 828 pos = strstr(ofname, ".gz");
@@ -836,7 +846,7 @@ int gunzip_main(int argc, char **argv)
836#endif 846#endif
837 if (outFileNum < 0) { 847 if (outFileNum < 0) {
838 perror(ofname); 848 perror(ofname);
839 do_exit(WARNING); 849 exit(WARNING);
840 } 850 }
841 /* Set permissions on the file */ 851 /* Set permissions on the file */
842 fchmod(outFileNum, statBuf.st_mode); 852 fchmod(outFileNum, statBuf.st_mode);
@@ -860,7 +870,7 @@ int gunzip_main(int argc, char **argv)
860 exit(FALSE); 870 exit(FALSE);
861 } 871 }
862 } 872 }
863 do_exit(exit_code); 873 exit(exit_code);
864} 874}
865 875
866 876
@@ -953,7 +963,7 @@ int in; /* input file descriptor */
953 */ 963 */
954RETSIGTYPE abort_gzip() 964RETSIGTYPE abort_gzip()
955{ 965{
956 do_exit(ERROR); 966 exit(ERROR);
957} 967}
958 968
959/* unzip.c -- decompress files in gzip or pkzip format. 969/* unzip.c -- decompress files in gzip or pkzip format.
@@ -1027,7 +1037,7 @@ int in, out; /* input and output file descriptors */
1027 ofd = out; 1037 ofd = out;
1028 method = get_method(ifd); 1038 method = get_method(ifd);
1029 if (method < 0) { 1039 if (method < 0) {
1030 do_exit(exit_code); /* error message already emitted */ 1040 exit(exit_code); /* error message already emitted */
1031 } 1041 }
1032 1042
1033 updcrc(NULL, 0); /* initialize crc */ 1043 updcrc(NULL, 0); /* initialize crc */
@@ -1218,7 +1228,7 @@ void flush_window()
1218 */ 1228 */
1219void write_buf(fd, buf, cnt) 1229void write_buf(fd, buf, cnt)
1220int fd; 1230int fd;
1221voidp buf; 1231void * buf;
1222unsigned cnt; 1232unsigned cnt;
1223{ 1233{
1224 unsigned n; 1234 unsigned n;
@@ -1228,7 +1238,7 @@ unsigned cnt;
1228 write_error(); 1238 write_error();
1229 } 1239 }
1230 cnt -= n; 1240 cnt -= n;
1231 buf = (voidp) ((char *) buf + n); 1241 buf = (void *) ((char *) buf + n);
1232 } 1242 }
1233} 1243}
1234 1244
@@ -1240,8 +1250,8 @@ unsigned cnt;
1240# define const 1250# define const
1241# endif 1251# endif
1242 1252
1243int strspn OF((const char *s, const char *accept)); 1253int strspn (const char *s, const char *accept);
1244int strcspn OF((const char *s, const char *reject)); 1254int strcspn (const char *s, const char *reject);
1245 1255
1246/* ======================================================================== 1256/* ========================================================================
1247 * Return the length of the maximum initial segment 1257 * Return the length of the maximum initial segment
@@ -1493,15 +1503,15 @@ struct huft {
1493 1503
1494 1504
1495/* Function prototypes */ 1505/* Function prototypes */
1496int huft_build OF((unsigned *, unsigned, unsigned, ush *, ush *, 1506int huft_build (unsigned *, unsigned, unsigned, ush *, ush *,
1497 struct huft **, int *)); 1507 struct huft **, int *);
1498int huft_free OF((struct huft *)); 1508int huft_free (struct huft *);
1499int inflate_codes OF((struct huft *, struct huft *, int, int)); 1509int inflate_codes (struct huft *, struct huft *, int, int);
1500int inflate_stored OF((void)); 1510int inflate_stored (void);
1501int inflate_fixed OF((void)); 1511int inflate_fixed (void);
1502int inflate_dynamic OF((void)); 1512int inflate_dynamic (void);
1503int inflate_block OF((int *)); 1513int inflate_block (int *);
1504int inflate OF((void)); 1514int inflate (void);
1505 1515
1506 1516
1507/* The inflate algorithm uses a sliding 32K byte window on the uncompressed 1517/* The inflate algorithm uses a sliding 32K byte window on the uncompressed