/* vi: set sw=4 ts=4: */
-/* gzip.c -- this is a stripped down version of gzip I put into busybox, it does
- * only standard in to standard out with -9 compression. It also requires the
- * zcat module for some important functions.
+/*
+ * Gzip implementation for busybox
+ *
+ * Based on GNU gzip Copyright (C) 1992-1993 Jean-loup Gailly.
+ *
+ * Originally adjusted for busybox by Charles P. Wright <cpw@unix.asb.com>
+ * "this is a stripped down version of gzip I put into busybox, it does
+ * only standard in to standard out with -9 compression. It also requires
+ * the zcat module for some important functions."
+ *
+ * Adjusted further by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
+ * to support files as well as stdin/stdout, and to generally behave itself wrt
+ * command line handling.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
- * Charles P. Wright <cpw@unix.asb.com>
- */
-#include "internal.h"
-#ifdef BB_GZIP
-
-//#ifndef BB_ZCAT
-//#error you need zcat to have gzip support!
-//#endif
-
-static const char gzip_usage[] =
- "gzip [OPTION]... FILE\n\n"
- "Compress FILE with maximum compression.\n"
- "When FILE is -, reads standard input. Implies -c.\n\n"
-
- "Options:\n"
- "\t-c\tWrite output to standard output instead of FILE.gz\n";
-
-
-/* gzip.h -- common declarations for all gzip modules
- * Copyright (C) 1992-1993 Jean-loup Gailly.
- * This is free software; you can redistribute it and/or modify it under the
- * terms of the GNU General Public License, see the file COPYING.
*/
-#if defined(__STDC__) || defined(PROTO)
-# define OF(args) args
-#else
-# define OF(args) ()
-#endif
-
-#ifdef __STDC__
-typedef void *voidp;
-#else
-typedef char *voidp;
-#endif
+/* These defines are very important for BusyBox. Without these,
+ * huge chunks of ram are pre-allocated making the BusyBox bss
+ * size Freaking Huge(tm), which is a bad thing.*/
+#define SMALL_MEM
+#define DYN_ALLOC
/* I don't like nested includes, but the string and io functions are used
* too often
*/
+#include <stdlib.h>
#include <stdio.h>
-#if !defined(NO_STRING_H) || defined(STDC_HEADERS)
-# include <string.h>
-# if !defined(STDC_HEADERS) && !defined(NO_MEMORY_H) && !defined(__GNUC__)
-# include <memory.h>
-# endif
-# define memzero(s, n) memset ((voidp)(s), 0, (n))
-#else
-# include <strings.h>
-# define strchr index
-# define strrchr rindex
-# define memcpy(d, s, n) bcopy((s), (d), (n))
-# define memcmp(s1, s2, n) bcmp((s1), (s2), (n))
-# define memzero(s, n) bzero((s), (n))
-#endif
+#include <string.h>
+#include <unistd.h>
+#include "busybox.h"
+#define BB_DECLARE_EXTERN
+#define bb_need_memory_exhausted
+#include "messages.c"
+
+#define memzero(s, n) memset ((void *)(s), 0, (n))
#ifndef RETSIGTYPE
# define RETSIGTYPE void
#endif
#ifdef DYN_ALLOC
-# define EXTERN(type, array) extern type * near array
-# define DECLARE(type, array, size) type * near array
+# define EXTERN(type, array) extern type * array
+# define DECLARE(type, array, size) type * array
# define ALLOC(type, array, size) { \
- array = (type*)fcalloc((size_t)(((size)+1L)/2), 2*sizeof(type)); \
- if (array == NULL) error("insufficient memory"); \
+ array = (type*)calloc((size_t)(((size)+1L)/2), 2*sizeof(type)); \
+ if (array == NULL) error_msg(memory_exhausted); \
}
-# define FREE(array) {if (array != NULL) fcfree(array), array=NULL;}
+# define FREE(array) {if (array != NULL) free(array), array=NULL;}
#else
# define EXTERN(type, array) extern type array[]
# define DECLARE(type, array, size) type array[size]
/* Diagnostic functions */
#ifdef DEBUG
-# define Assert(cond,msg) {if(!(cond)) error(msg);}
+# define Assert(cond,msg) {if(!(cond)) error_msg(msg);}
# define Trace(x) fprintf x
# define Tracev(x) {if (verbose) fprintf x ;}
# define Tracevv(x) {if (verbose>1) fprintf x ;}
#define WARN(msg) {if (!quiet) fprintf msg ; \
if (exit_code == OK) exit_code = WARNING;}
-#define do_exit(c) exit(c)
-
/* in zip.c: */
-extern int zip OF((int in, int out));
-extern int file_read OF((char *buf, unsigned size));
+extern int zip (int in, int out);
+extern int file_read (char *buf, unsigned size);
/* in unzip.c */
-extern int unzip OF((int in, int out));
-extern int check_zipfile OF((int in));
+extern int unzip (int in, int out);
+extern int check_zipfile (int in);
/* in unpack.c */
-extern int unpack OF((int in, int out));
+extern int unpack (int in, int out);
/* in unlzh.c */
-extern int unlzh OF((int in, int out));
+extern int unlzh (int in, int out);
/* in gzip.c */
-RETSIGTYPE abort_gzip OF((void));
+RETSIGTYPE abort_gzip (void);
/* in deflate.c */
-void lm_init OF((ush * flags));
-ulg deflate OF((void));
+void lm_init (ush * flags);
+ulg deflate (void);
/* in trees.c */
-void ct_init OF((ush * attr, int *method));
-int ct_tally OF((int dist, int lc));
-ulg flush_block OF((char *buf, ulg stored_len, int eof));
+void ct_init (ush * attr, int *method);
+int ct_tally (int dist, int lc);
+ulg flush_block (char *buf, ulg stored_len, int eof);
/* in bits.c */
-void bi_init OF((file_t zipfile));
-void send_bits OF((int value, int length));
-unsigned bi_reverse OF((unsigned value, int length));
-void bi_windup OF((void));
-void copy_block OF((char *buf, unsigned len, int header));
-extern int (*read_buf) OF((char *buf, unsigned size));
+void bi_init (file_t zipfile);
+void send_bits (int value, int length);
+unsigned bi_reverse (unsigned value, int length);
+void bi_windup (void);
+void copy_block (char *buf, unsigned len, int header);
+extern int (*read_buf) (char *buf, unsigned size);
/* in util.c: */
-extern int copy OF((int in, int out));
-extern ulg updcrc OF((uch * s, unsigned n));
-extern void clear_bufs OF((void));
-extern int fill_inbuf OF((int eof_ok));
-extern void flush_outbuf OF((void));
-extern void flush_window OF((void));
-extern void write_buf OF((int fd, voidp buf, unsigned cnt));
-extern char *strlwr OF((char *s));
-extern char *add_envopt OF((int *argcp, char ***argvp, char *env));
-extern void error OF((char *m));
-extern void warn OF((char *a, char *b));
-extern void read_error OF((void));
-extern void write_error OF((void));
-extern void display_ratio OF((long num, long den, FILE * file));
+extern int copy (int in, int out);
+extern ulg updcrc (uch * s, unsigned n);
+extern void clear_bufs (void);
+extern int fill_inbuf (int eof_ok);
+extern void flush_outbuf (void);
+extern void flush_window (void);
+extern void write_buf (int fd, void * buf, unsigned cnt);
+extern char *strlwr (char *s);
+extern char *add_envopt (int *argcp, char ***argvp, char *env);
+extern void read_error_msg (void);
+extern void write_error_msg (void);
+extern void display_ratio (long num, long den, FILE * file);
/* in inflate.c */
-extern int inflate OF((void));
+extern int inflate (void);
/* lzw.h -- define the lzw functions.
* Copyright (C) 1992-1993 Jean-loup Gailly.
* are always zero.
*/
-int (*read_buf) OF((char *buf, unsigned size));
+int (*read_buf) (char *buf, unsigned size);
/* Current input function. Set to mem_read for in-memory compression */
/* ===========================================================================
* Prototypes for local functions.
*/
-local void fill_window OF((void));
+local void fill_window (void);
-int longest_match OF((IPos cur_match));
+int longest_match (IPos cur_match);
#ifdef ASMV
-void match_init OF((void)); /* asm code initialization */
+void match_init (void); /* asm code initialization */
#endif
#ifdef DEBUG
-local void check_match OF((IPos start, IPos match, int length));
+local void check_match (IPos start, IPos match, int length);
#endif
/* ===========================================================================
(char *) window + start, length) != EQUAL) {
fprintf(stderr,
" start %d, match %d, length %d\n", start, match, length);
- error("invalid match");
+ error_msg("invalid match");
}
if (verbose > 1) {
fprintf(stderr, "\\[%d,%d]", start - match, length);
#include <ctype.h>
#include <sys/types.h>
#include <signal.h>
-#include <sys/stat.h>
#include <errno.h>
/* configuration */
# include <unistd.h>
#endif
-#if defined(STDC_HEADERS) || !defined(NO_STDLIB_H)
-# include <stdlib.h>
-#else
-extern int errno;
-#endif
-
#if defined(DIRENT)
# include <dirent.h>
typedef struct dirent dir_type;
# define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
#endif
-typedef RETSIGTYPE(*sig_type) OF((int));
+typedef RETSIGTYPE(*sig_type) (int);
#ifndef O_BINARY
# define O_BINARY 0 /* creation mode for open() */
#ifdef NO_OFF_T
typedef long off_t;
-off_t lseek OF((int fd, off_t offset, int whence));
+off_t lseek (int fd, off_t offset, int whence);
#endif
/* Separator for file name parts (see shorten_name()) */
/* local variables */
-int ascii = 0; /* convert end-of-lines to local OS conventions */
-int decompress = 0; /* decompress (-d) */
-int no_name = -1; /* don't save or restore the original file name */
-int no_time = -1; /* don't save or restore the original file time */
-int foreground; /* set if program run in foreground */
-char *progname; /* program name */
+static int foreground; /* set if program run in foreground */
static int method = DEFLATED; /* compression method */
static int exit_code = OK; /* program exit code */
-int save_orig_name; /* set if original name must be saved */
-int last_member; /* set for .zip and .Z files */
-int part_nb; /* number of parts in .gz file */
-long time_stamp; /* original time stamp (modification time) */
-long ifile_size; /* input file size, -1 for devices (debug only) */
-char *env; /* contents of GZIP env variable */
-char **args = NULL; /* argv pointer if GZIP env variable defined */
-char z_suffix[MAX_SUFFIX + 1]; /* default suffix (can be set with --suffix) */
-int z_len; /* strlen(z_suffix) */
-
-long bytes_in; /* number of input bytes */
-long bytes_out; /* number of output bytes */
-char ifname[MAX_PATH_LEN]; /* input file name */
-char ofname[MAX_PATH_LEN]; /* output file name */
-int remove_ofname = 0; /* remove output file on error */
-struct stat istat; /* status for input file */
-int ifd; /* input file descriptor */
-int ofd; /* output file descriptor */
-unsigned insize; /* valid bytes in inbuf */
-unsigned inptr; /* index of next byte to be processed in inbuf */
-unsigned outcnt; /* bytes in output buffer */
+static int part_nb; /* number of parts in .gz file */
+static long time_stamp; /* original time stamp (modification time) */
+static long ifile_size; /* input file size, -1 for devices (debug only) */
+static char z_suffix[MAX_SUFFIX + 1]; /* default suffix (can be set with --suffix) */
+static int z_len; /* strlen(z_suffix) */
+
+static long bytes_in; /* number of input bytes */
+static long bytes_out; /* number of output bytes */
+static char ifname[MAX_PATH_LEN]; /* input file name */
+static char ofname[MAX_PATH_LEN]; /* output file name */
+static int ifd; /* input file descriptor */
+static int ofd; /* output file descriptor */
+static unsigned insize; /* valid bytes in inbuf */
+static unsigned outcnt; /* bytes in output buffer */
/* local functions */
char *delFileName;
int tostdout = 0;
int fromstdin = 0;
-
- if (argc == 1)
- usage(gzip_usage);
+ int force = 0;
/* Parse any options */
while (--argc > 0 && **(++argv) == '-') {
if (*((*argv) + 1) == '\0') {
- fromstdin = 1;
tostdout = 1;
}
while (*(++(*argv))) {
case 'c':
tostdout = 1;
break;
+ case 'f':
+ force = 1;
+ break;
+ /* Ignore 1-9 (compression level) options */
+ case '1': case '2': case '3': case '4': case '5':
+ case '6': case '7': case '8': case '9':
+ break;
+ case 'd':
+ exit(gunzip_main(argc, argv));
default:
- usage(gzip_usage);
+ show_usage();
}
}
}
+ if (argc <= 0 ) {
+ fromstdin = 1;
+ tostdout = 1;
+ }
+
+ if (isatty(fileno(stdin)) && fromstdin==1 && force==0)
+ error_msg_and_die( "data not read from terminal. Use -f to force it.");
+ if (isatty(fileno(stdout)) && tostdout==1 && force==0)
+ error_msg_and_die( "data not written to terminal. Use -f to force it.");
foreground = signal(SIGINT, SIG_IGN) != SIG_IGN;
if (foreground) {
ifile_size = -1L; /* convention for unknown size */
} else {
/* Open up the input file */
- if (*argv == '\0')
- usage(gzip_usage);
+ if (argc <= 0)
+ show_usage();
strncpy(ifname, *argv, MAX_PATH_LEN);
- /* Open input fille */
+ /* Open input file */
inFileNum = open(ifname, O_RDONLY);
- if (inFileNum < 0) {
- perror(ifname);
- do_exit(WARNING);
- }
+ if (inFileNum < 0)
+ perror_msg_and_die("%s", ifname);
/* Get the time stamp on the input file. */
- result = stat(ifname, &statBuf);
- if (result < 0) {
- perror(ifname);
- do_exit(WARNING);
- }
+ if (stat(ifname, &statBuf) < 0)
+ perror_msg_and_die("%s", ifname);
time_stamp = statBuf.st_ctime;
ifile_size = statBuf.st_size;
}
#else
outFileNum = open(ofname, O_RDWR | O_CREAT | O_EXCL);
#endif
- if (outFileNum < 0) {
- perror(ofname);
- do_exit(WARNING);
- }
+ if (outFileNum < 0)
+ perror_msg_and_die("%s", ofname);
SET_BINARY_MODE(outFileNum);
/* Set permissions on the file */
fchmod(outFileNum, statBuf.st_mode);
else
delFileName = ofname;
- if (unlink(delFileName) < 0) {
- perror(delFileName);
- exit(FALSE);
- }
+ if (unlink(delFileName) < 0)
+ perror_msg_and_die("%s", delFileName);
}
- do_exit(exit_code);
+ return(exit_code);
}
/* trees.c -- output deflated data using Huffman coding
* Local (static) routines in this file.
*/
-local void init_block OF((void));
-local void pqdownheap OF((ct_data near * tree, int k));
-local void gen_bitlen OF((tree_desc near * desc));
-local void gen_codes OF((ct_data near * tree, int max_code));
-local void build_tree OF((tree_desc near * desc));
-local void scan_tree OF((ct_data near * tree, int max_code));
-local void send_tree OF((ct_data near * tree, int max_code));
-local int build_bl_tree OF((void));
-local void send_all_trees OF((int lcodes, int dcodes, int blcodes));
-local void compress_block OF((ct_data near * ltree, ct_data near * dtree));
-local void set_file_type OF((void));
+local void init_block (void);
+local void pqdownheap (ct_data near * tree, int k);
+local void gen_bitlen (tree_desc near * desc);
+local void gen_codes (ct_data near * tree, int max_code);
+local void build_tree (tree_desc near * desc);
+local void scan_tree (ct_data near * tree, int max_code);
+local void send_tree (ct_data near * tree, int max_code);
+local int build_bl_tree (void);
+local void send_all_trees (int lcodes, int dcodes, int blcodes);
+local void compress_block (ct_data near * ltree, ct_data near * dtree);
+local void set_file_type (void);
#ifndef DEBUG
* used.
*/
-#define MAX(a,b) (a >= b ? a : b)
/* the arguments must not have side effects */
/* ===========================================================================
#endif
/* Since LIT_BUFSIZE <= 2*WSIZE, the input data must be there: */
if (buf == (char *) 0)
- error("block vanished");
+ error_msg("block vanished");
copy_block(buf, (unsigned) stored_len, 0); /* without header */
compressed_len = stored_len << 3;
bin_freq += dyn_ltree[n++].Freq;
*file_type = bin_freq > (ascii_freq >> 2) ? BINARY : ASCII;
if (*file_type == BINARY && translate_eol) {
- warn("-l used on binary file", "");
+ error_msg("-l used on binary file");
}
}
# include <fcntl.h>
#endif
-#if defined(STDC_HEADERS) || !defined(NO_STDLIB_H)
-# include <stdlib.h>
-#else
-extern int errno;
-#endif
-
/* ===========================================================================
* Copy input to output unchanged: zcat == cat with --force.
* IN assertion: insize bytes have already been read in inbuf.
insize = read(in, (char *) inbuf, INBUFSIZ);
}
if ((int) insize == EOF && errno != 0) {
- read_error();
+ read_error_msg();
}
bytes_in = bytes_out;
return OK;
/* Provide missing strspn and strcspn functions. */
-# ifndef __STDC__
-# define const
-# endif
-
-int strspn OF((const char *s, const char *accept));
-int strcspn OF((const char *s, const char *reject));
+int strspn (const char *s, const char *accept);
+int strcspn (const char *s, const char *reject);
/* ========================================================================
* Return the length of the maximum initial segment
nargv = (char **) calloc(*argcp + 1, sizeof(char *));
if (nargv == NULL)
- error("out of memory");
+ error_msg(memory_exhausted);
oargv = *argvp;
*argvp = nargv;
/* Copy the program name first */
if (oargc-- < 0)
- error("argc<=0");
+ error_msg("argc<=0");
*(nargv++) = *(oargv++);
/* Then copy the environment args */
isize += (ulg) len;
return (int) len;
}
-#endif
+
+/* ===========================================================================
+ * Write the output buffer outbuf[0..outcnt-1] and update bytes_out.
+ * (used for the compressed data only)
+ */
+void flush_outbuf()
+{
+ if (outcnt == 0)
+ return;
+
+ write_buf(ofd, (char *) outbuf, outcnt);
+ bytes_out += (ulg) outcnt;
+ outcnt = 0;
+}