Fix a segfault in lash, hush, and cmdedit. Each of these used
[oweals/busybox.git] / gzip.c
diff --git a/gzip.c b/gzip.c
index 19ad1a72900ec28045b72c499ab4dca1002b02e4..c4c59272903bc3eb27ac1d6dff47f844a228103e 100644 (file)
--- a/gzip.c
+++ b/gzip.c
  *
  */
 
-#include "internal.h"
-#define BB_DECLARE_EXTERN
-#define bb_need_memory_exhausted
-#include "messages.c"
-
 /* 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.*/
 /* I don't like nested includes, but the string and io functions are used
  * too often
  */
+#include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
+#include <unistd.h>
+#include <errno.h>
+#include "busybox.h"
+
 #define memzero(s, n)     memset ((void *)(s), 0, (n))
 
 #ifndef RETSIGTYPE
@@ -70,7 +70,7 @@ typedef unsigned long ulg;
 /* methods 4 to 7 reserved */
 #define DEFLATED    8
 #define MAX_METHODS 9
-extern int method;                             /* compression method */
+static int method;                             /* compression method */
 
 /* To save memory for 16 bit systems, some arrays are overlaid between
  * the various modules:
@@ -114,7 +114,7 @@ extern int method;                          /* compression method */
 #  define DECLARE(type, array, size)  type * array
 #  define ALLOC(type, array, size) { \
       array = (type*)calloc((size_t)(((size)+1L)/2), 2*sizeof(type)); \
-      if (array == NULL) errorMsg(memory_exhausted); \
+      if (array == NULL) error_msg(memory_exhausted); \
    }
 #  define FREE(array) {if (array != NULL) free(array), array=NULL;}
 #else
@@ -141,7 +141,7 @@ EXTERN(ush, tab_prefix1);           /* prefix for odd  codes */
 #endif
 
 extern unsigned insize;                        /* valid bytes in inbuf */
-extern unsigned inptr;                 /* index of next byte to be processed in inbuf */
+static unsigned inptr;                 /* index of next byte to be processed in inbuf */
 extern unsigned outcnt;                        /* bytes in output buffer */
 
 extern long bytes_in;                  /* number of input bytes */
@@ -251,7 +251,7 @@ extern int save_orig_name;          /* set if original name must be saved */
 
 /* Diagnostic functions */
 #ifdef DEBUG
-#  define Assert(cond,msg) {if(!(cond)) errorMsg(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 ;}
@@ -275,7 +275,6 @@ extern int zip (int in, int out);
 extern int file_read (char *buf, unsigned size);
 
        /* in unzip.c */
-extern int unzip (int in, int out);
 extern int check_zipfile (int in);
 
        /* in unpack.c */
@@ -292,7 +291,7 @@ void lm_init (ush * flags);
 ulg deflate (void);
 
                /* in trees.c */
-void ct_init (ush * attr, int *method);
+void ct_init (ush * attr, int *methodp);
 int ct_tally (int dist, int lc);
 ulg flush_block (char *buf, ulg stored_len, int eof);
 
@@ -306,16 +305,16 @@ extern int (*read_buf) (char *buf, unsigned size);
 
        /* in util.c: */
 extern int copy (int in, int out);
-extern ulg updcrc (uch * s, unsigned n);
-extern void clear_bufs (void);
+//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 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 read_error_msg (void);
+//extern void write_error_msg (void);
 extern void display_ratio (long num, long den, FILE * file);
 
        /* in inflate.c */
@@ -702,6 +701,120 @@ extern void _expand_args(int *argc, char ***argv);
 #ifndef put_char
 #  define put_char(c) put_byte(c)
 #endif
+
+int crc_table_empty = 1;
+
+/* ========================================================================
+ * Signal and error handler.
+ */
+void abort_gzip()
+{
+       exit(ERROR);
+}
+
+/* ===========================================================================
+ * Clear input and output buffers
+ */
+static void clear_bufs(void)
+{
+       outcnt = 0;
+       insize = inptr = 0;
+       bytes_in = bytes_out = 0L;
+}
+
+static void write_error_msg()
+{
+       fprintf(stderr, "\n");
+       perror("");
+       abort_gzip();
+}
+
+/* ===========================================================================
+ * Does the same as write(), but also handles partial pipe writes and checks
+ * for error return.
+ */
+static void write_buf(fd, buf, cnt)
+int fd;
+void * buf;
+unsigned cnt;
+{
+       unsigned n;
+
+       while ((n = write(fd, buf, cnt)) != cnt) {
+               if (n == (unsigned) (-1)) {
+                       write_error_msg();
+               }
+               cnt -= n;
+               buf = (void *) ((char *) buf + n);
+       }
+}
+
+/* ========================================================================
+ * Error handlers.
+ */
+static void read_error_msg()
+{
+       fprintf(stderr, "\n");
+       if (errno != 0) {
+               perror("");
+       } else {
+               fprintf(stderr, "unexpected end of file\n");
+       }
+       abort_gzip();
+}
+
+/* ===========================================================================
+ * Run a set of bytes through the crc shift register.  If s is a NULL
+ * pointer, then initialize the crc shift register contents instead.
+ * Return the current crc in either case.
+ */
+static ulg updcrc(s, n)
+uch *s;                                        /* pointer to bytes to pump through */
+unsigned n;                            /* number of bytes in s[] */
+{
+       static ulg crc = (ulg) 0xffffffffL;     /* shift register contents */
+       register ulg c;                         /* temporary variable */
+       static unsigned long crc_32_tab[256];
+       if (crc_table_empty) {
+               unsigned long csr;      /* crc shift register */
+               unsigned long e;      /* polynomial exclusive-or pattern */
+               int i;                /* counter for all possible eight bit values */
+               int k;                /* byte being shifted into crc apparatus */
+
+               /* terms of polynomial defining this crc (except x^32): */
+               static int p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
+
+               /* Make exclusive-or pattern from polynomial (0xedb88320) */
+               e = 0;
+               for (i = 0; i < sizeof(p)/sizeof(int); i++)
+                       e |= 1L << (31 - p[i]);
+
+               /* Compute and print table of CRC's, five per line */
+               crc_32_tab[0] = 0x00000000L;
+               for (i = 1; i < 256; i++) {
+                       csr = i;
+                  /* The idea to initialize the register with the byte instead of
+                    * zero was stolen from Haruhiko Okumura's ar002
+                    */
+                       for (k = 8; k; k--)
+                               csr = csr & 1 ? (csr >> 1) ^ e : csr >> 1;
+                       crc_32_tab[i]=csr;
+               }
+       }
+
+       if (s == NULL) {
+               c = 0xffffffffL;
+       } else {
+               c = crc;
+               if (n)
+                       do {
+                               c = crc_32_tab[((int) c ^ (*s++)) & 0xff] ^ (c >> 8);
+                       } while (--n);
+       }
+       crc = c;
+       return c ^ 0xffffffffL;         /* (instead of ~c for 64-bit machines) */
+}
+
 /* bits.c -- output variable-length bit strings
  * Copyright (C) 1992-1993 Jean-loup Gailly
  * This is free software; you can redistribute it and/or modify it under the
@@ -1381,7 +1494,7 @@ int length;
                           (char *) window + start, length) != EQUAL) {
                fprintf(stderr,
                                " start %d, match %d, length %d\n", start, match, length);
-               errorMsg("invalid match");
+               error_msg("invalid match");
        }
        if (verbose > 1) {
                fprintf(stderr, "\\[%d,%d]", start - match, length);
@@ -1609,7 +1722,6 @@ ulg deflate()
 #include <ctype.h>
 #include <sys/types.h>
 #include <signal.h>
-#include <sys/stat.h>
 #include <errno.h>
 
                /* configuration */
@@ -1628,12 +1740,6 @@ ulg deflate()
 #  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;
@@ -1755,34 +1861,23 @@ DECLARE(ush, tab_prefix1, 1L << (BITS - 1));
 
                /* 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 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 */
 
@@ -1802,39 +1897,38 @@ int gzip_main(int argc, char **argv)
        int tostdout = 0;
        int fromstdin = 0;
        int force = 0;
+       int opt;
 
-       if (argc == 1)
-               usage(gzip_usage);
-
-       /* Parse any options */
-       while (--argc > 0 && **(++argv) == '-') {
-               if (*((*argv) + 1) == '\0') {
+       while ((opt = getopt(argc, argv, "cf123456789dq")) != -1) {
+               switch (opt) {
+               case 'c':
                        tostdout = 1;
-               }
-               while (*(++(*argv))) {
-                       switch (**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;
-                       default:
-                               usage(gzip_usage);
-                       }
+                       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 'q':
+                       break;
+#ifdef BB_GUNZIP
+               case 'd':
+                       optind = 1;
+                       return gunzip_main(argc, argv);
+#endif
+               default:
+                       show_usage();
                }
        }
-       if (argc <= 0)
+       if (optind == argc) {
                fromstdin = 1;
+               tostdout = 1;
+       }
 
-       if (isatty(fileno(stdin)) && fromstdin==1 && force==0)
-               fatalError( "data not read from terminal. Use -f to force it.\n");
        if (isatty(fileno(stdout)) && tostdout==1 && force==0)
-               fatalError( "data not written to terminal. Use -f to force it.\n");
+               error_msg_and_die( "compressed data not written to terminal. Use -f to force it.");
 
        foreground = signal(SIGINT, SIG_IGN) != SIG_IGN;
        if (foreground) {
@@ -1874,22 +1968,15 @@ int gzip_main(int argc, char **argv)
                ifile_size = -1L;               /* convention for unknown size */
        } else {
                /* Open up the input file */
-               if (argc <= 0)
-                       usage(gzip_usage);
-               strncpy(ifname, *argv, MAX_PATH_LEN);
+               strncpy(ifname, argv[optind], MAX_PATH_LEN);
 
-               /* Open input fille */
+               /* Open input file */
                inFileNum = open(ifname, O_RDONLY);
-               if (inFileNum < 0) {
-                       perror(ifname);
-                       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);
-                       exit(WARNING);
-               }
+               if (stat(ifname, &statBuf) < 0)
+                       perror_msg_and_die("%s", ifname);
                time_stamp = statBuf.st_ctime;
                ifile_size = statBuf.st_size;
        }
@@ -1920,10 +2007,8 @@ int gzip_main(int argc, char **argv)
 #else
                outFileNum = open(ofname, O_RDWR | O_CREAT | O_EXCL);
 #endif
-               if (outFileNum < 0) {
-                       perror(ofname);
-                       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);
@@ -1941,10 +2026,8 @@ int gzip_main(int argc, char **argv)
                else
                        delFileName = ofname;
 
-               if (unlink(delFileName) < 0) {
-                       perror(delFileName);
-                       exit(FALSE);
-               }
+               if (unlink(delFileName) < 0)
+                       perror_msg_and_die("%s", delFileName);
        }
 
        return(exit_code);
@@ -2913,7 +2996,7 @@ int eof;                                          /* true if this is the last block for a file */
 #endif
                /* Since LIT_BUFSIZE <= 2*WSIZE, the input data must be there: */
                if (buf == (char *) 0)
-                       errorMsg("block vanished");
+                       error_msg("block vanished");
 
                copy_block(buf, (unsigned) stored_len, 0);      /* without header */
                compressed_len = stored_len << 3;
@@ -3096,7 +3179,7 @@ local void set_file_type()
                bin_freq += dyn_ltree[n++].Freq;
        *file_type = bin_freq > (ascii_freq >> 2) ? BINARY : ASCII;
        if (*file_type == BINARY && translate_eol) {
-               errorMsg("-l used on binary file");
+               error_msg("-l used on binary file");
        }
 }
 
@@ -3117,12 +3200,6 @@ local void set_file_type()
 #  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.
@@ -3252,13 +3329,13 @@ char *env;                                              /* name of environment variable */
        nargv = (char **) calloc(*argcp + 1, sizeof(char *));
 
        if (nargv == NULL)
-               errorMsg(memory_exhausted);
+               error_msg(memory_exhausted);
        oargv = *argvp;
        *argvp = nargv;
 
        /* Copy the program name first */
        if (oargc-- < 0)
-               errorMsg("argc<=0");
+               error_msg("argc<=0");
        *(nargv++) = *(oargv++);
 
        /* Then copy the environment args */
@@ -3329,7 +3406,7 @@ long header_bytes;                                /* number of bytes in gzip header */
 int zip(in, out)
 int in, out;                                   /* input and output file descriptors */
 {
-       uch flags = 0;                          /* general purpose bit flags */
+       uch my_flags = 0;                               /* general purpose bit flags */
        ush attr = 0;                           /* ascii/binary flag */
        ush deflate_flags = 0;          /* pkzip -es, -en or -ex equivalent */
 
@@ -3345,7 +3422,7 @@ int in, out;                                      /* input and output file descriptors */
        put_byte(GZIP_MAGIC[1]);
        put_byte(DEFLATED);                     /* compression method */
 
-       put_byte(flags);                        /* general flags */
+       put_byte(my_flags);                     /* general flags */
        put_long(time_stamp);
 
        /* Write deflated file to zip file */
@@ -3393,3 +3470,17 @@ unsigned size;
        isize += (ulg) len;
        return (int) len;
 }
+
+/* ===========================================================================
+ * 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;
+}