Simplify CRC table generation
[oweals/busybox.git] / libbb / unzip.c
index 3599571967658e89479f8e8ad94daa4697cd3bd3..c28ca836c2b58899a84c0aadf9cd6d2cf5afe2c7 100644 (file)
@@ -67,7 +67,7 @@ static char *license_msg[] = {
 #include <string.h>
 #include "libbb.h"
 
-FILE *in_file, *out_file;
+static FILE *in_file, *out_file;
 
 /* these are freed by gz_close */
 static unsigned char *window;
@@ -91,9 +91,9 @@ static const int N_MAX = 288;         /* maximum number of codes in any set */
 static long bytes_out;         /* number of output bytes */
 static unsigned long outcnt;   /* bytes in output buffer */
 
-unsigned hufts;                /* track memory usage */
-unsigned long bb;                      /* bit buffer */
-unsigned bk;           /* bits in bit buffer */
+static unsigned hufts;         /* track memory usage */
+static unsigned long bb;                       /* bit buffer */
+static unsigned bk;            /* bits in bit buffer */
 
 typedef struct huft_s {
        unsigned char e;                /* number of extra bits or operation */
@@ -104,7 +104,7 @@ typedef struct huft_s {
        } v;
 } huft_t;
 
-unsigned short mask_bits[] = {
+static const unsigned short mask_bits[] = {
        0x0000,
        0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
        0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
@@ -115,30 +115,24 @@ unsigned short mask_bits[] = {
  * Signal and error handler.
  */
  
-static void abort_gzip()
+static void abort_gzip(void)
 {
        error_msg("gzip aborted\n");
        exit(ERROR);
 }
 
-static void make_crc_table()
+static void make_crc_table(void)
 {
-       unsigned long table_entry;      /* crc shift register */
-       unsigned long poly = 0;      /* 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};
+       const unsigned long poly = 0xedb88320;      /* polynomial exclusive-or pattern */
+       unsigned short i;                /* counter for all possible eight bit values */
 
        crc_table = (unsigned long *) malloc(256 * sizeof(unsigned long));
 
-       /* Make exclusive-or pattern from polynomial (0xedb88320) */
-       for (i = 0; i < sizeof(p)/sizeof(int); i++)
-               poly |= 1L << (31 - p[i]);
-
        /* Compute and print table of CRC's, five per line */
        for (i = 0; i < 256; i++) {
+               unsigned long table_entry;      /* crc shift register */
+               char k; /* byte being shifted into crc apparatus */
+
                table_entry = i;
           /* The idea to initialize the register with the byte instead of
             * zero was stolen from Haruhiko Okumura's ar002
@@ -154,7 +148,7 @@ static void make_crc_table()
  * Write the output window window[0..outcnt-1] and update crc and bytes_out.
  * (Used for the decompressed data only.)
  */
-void flush_window()
+static void flush_window(void)
 {
        int n;
 
@@ -853,7 +847,7 @@ static int inflate_block(int *e)
  *
  * GLOBAL VARIABLES: outcnt, bk, bb, hufts, inptr
  */
-static int inflate()
+static int inflate(void)
 {
        int e;                          /* last block flag */
        int r;                          /* result code */
@@ -875,6 +869,14 @@ static int inflate()
                }
        } while (!e);
 
+       /* Undo too much lookahead. The next read will be byte aligned so we
+        * can discard unused bits in the last meaningful byte.
+        */
+       while (bk >= 8) {
+               bk -= 8;
+               ungetc((bb << bk), in_file);
+       }
+
        /* flush out window */
        flush_window();
 
@@ -1021,10 +1023,6 @@ extern void gz_close(int gunzip_pid)
        if (waitpid(gunzip_pid, NULL, 0) == -1) {
                printf("Couldnt wait ?");
        }
-       if (window) {
                free(window);
-       }
-       if (crc_table) {
                free(crc_table);
-       }
 }