Woops, my gunzip fix broke unzip, these cant be static
[oweals/busybox.git] / archival / libunarchive / decompress_unzip.c
index 7ebbd0ee75dafa1e39c1aaa591f20d446f41c313..3a7334ce9e10760bfd26a1609680a312f40785cc 100644 (file)
@@ -14,6 +14,8 @@
  * General cleanup to better adhere to the style guide and make use of standard
  * busybox functions by Glenn McGrath <bug1@optushome.com.au>
  * 
+ * read_gz interface + associated hacking by Laurence Anderson
+ *
  * 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
@@ -84,31 +86,28 @@ static int gunzip_src_fd;
 unsigned int gunzip_bytes_out; /* number of output bytes */
 static unsigned int gunzip_outbuf_count;       /* bytes in output buffer */
 
-/* This is used to sanify any unused bits from the bitbuffer 
- * so they arent skipped when reading trailers (trailing headers) */
-unsigned char gunzip_in_buffer_count;
-unsigned char *gunzip_in_buffer;
-
 /* gunzip_window size--must be a power of two, and
  *  at least 32K for zip's deflate method */
 static const int gunzip_wsize = 0x8000;
-static int output_buffer_size = 0x8000; // gunzip_wsize initially
-
 static unsigned char *gunzip_window;
+
 static unsigned int *gunzip_crc_table;
 unsigned int gunzip_crc;
 
-static unsigned char *output_buffer;
-static unsigned int output_buffer_len;
-
 /* If BMAX needs to be larger than 16, then h and x[] should be ulg. */
 #define BMAX 16        /* maximum bit length of any code (16 for explode) */
 #define N_MAX 288      /* maximum number of codes in any set */
 
-static unsigned int gunzip_hufts;      /* track memory usage */
+/* bitbuffer */
 static unsigned int gunzip_bb; /* bit buffer */
 static unsigned char gunzip_bk;        /* bits in bit buffer */
 
+/* These control the size of the bytebuffer */
+#define BYTEBUFFER_MAX 0x8000
+static unsigned char *bytebuffer = NULL;
+static unsigned int bytebuffer_offset = 0;
+static unsigned int bytebuffer_size = 0;
+
 static const unsigned short mask_bits[] = {
        0x0000, 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
        0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
@@ -145,13 +144,25 @@ static const unsigned char border[] = {
        16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
 };
 
+static void fill_bytebuffer(void)
+{
+       if (bytebuffer_offset >= bytebuffer_size) {
+               /* Leave the first 4 bytes empty so we can always unwind the bitbuffer 
+                * to the front of the bytebuffer, leave 4 bytes free at end of tail
+                * so we can easily top up buffer in check_trailer_gzip() */
+               bytebuffer_size = 4 + xread(gunzip_src_fd, &bytebuffer[4], BYTEBUFFER_MAX - 8);
+               bytebuffer_offset = 4;
+       }
+}
+
 static unsigned int fill_bitbuffer(unsigned int bitbuffer, unsigned int *current, const unsigned int required)
 {
        while (*current < required) {
-               bitbuffer |= ((unsigned int) xread_char(gunzip_src_fd)) << *current;
+               fill_bytebuffer();
+               bitbuffer |= ((unsigned int) bytebuffer[bytebuffer_offset]) << *current;
+               bytebuffer_offset++;
                *current += 8;
        }
-
        return(bitbuffer);
 }
 
@@ -343,7 +354,6 @@ static int huft_build(unsigned int *b, const unsigned int n,
                                /* allocate and link in new table */
                                q = (huft_t *) xmalloc((z + 1) * sizeof(huft_t));
 
-                               gunzip_hufts += z + 1;  /* track memory usage */
                                *t = q + 1;     /* link to list for huft_free() */
                                *(t = &(q->v.t)) = NULL;
                                u[h] = ++q;     /* table starts after link */
@@ -395,36 +405,6 @@ static int huft_build(unsigned int *b, const unsigned int n,
        return y != 0 && g != 1;
 }
 
-/* ===========================================================================
- * Write the output gunzip_window gunzip_window[0..gunzip_outbuf_count-1] and update crc and gunzip_bytes_out.
- * (Used for the decompressed data only.)
- */
-static void flush_gunzip_window(void)
-{
-       int n;
-
-       for (n = 0; n < gunzip_outbuf_count; n++) {
-               gunzip_crc = gunzip_crc_table[((int) gunzip_crc ^ (gunzip_window[n])) & 0xff] ^ (gunzip_crc >> 8);
-       }
-
-       if (output_buffer_len == 0) { // Our buffer is empty -> straight memcpy
-               memcpy(output_buffer, gunzip_window, gunzip_outbuf_count);
-               output_buffer_len = gunzip_outbuf_count;
-       } else { // Bit more complicated, append to end of output_buffer, realloc as necessary
-               int newlen = output_buffer_len + gunzip_outbuf_count;
-               if (newlen > output_buffer_size) {
-                       output_buffer = xrealloc(output_buffer, newlen); // Could free later, but as we now have the memory...
-                       //printf("Using %d byte output buffer\n", newlen);
-                       output_buffer_size = newlen;
-               }
-               memcpy(output_buffer + output_buffer_len, gunzip_window, gunzip_outbuf_count);
-               output_buffer_len += gunzip_outbuf_count;
-       }
-       
-       gunzip_bytes_out += gunzip_outbuf_count;
-       gunzip_outbuf_count = 0;
-}
-
 /*
  * inflate (decompress) the codes in a deflated (compressed) block.
  * Return an error code or zero if it all goes ok.
@@ -468,7 +448,7 @@ static int inflate_codes(huft_t * my_tl, huft_t * my_td, const unsigned int my_b
                if ((e = (t = tl + ((unsigned) b & ml))->e) > 16)
                        do {
                                if (e == 99) {
-                                       return 1;
+                                       error_msg_and_die("inflate_codes error 1");;
                                }
                                b >>= t->b;
                                k -= t->b;
@@ -484,7 +464,7 @@ static int inflate_codes(huft_t * my_tl, huft_t * my_td, const unsigned int my_b
                                gunzip_outbuf_count = (w);
                                //flush_gunzip_window();
                                w = 0;
-                               return -1;
+                               return 1; // We have a block to read
                        }
                } else {                /* it's an EOB or a length */
 
@@ -504,7 +484,7 @@ static int inflate_codes(huft_t * my_tl, huft_t * my_td, const unsigned int my_b
                        if ((e = (t = td + ((unsigned) b & md))->e) > 16)
                                do {
                                        if (e == 99)
-                                               return 1;
+                                               error_msg_and_die("inflate_codes error 2");;
                                        b >>= t->b;
                                        k -= t->b;
                                        e -= 16;
@@ -542,7 +522,7 @@ do_copy:            do {
                                        else resumeCopy = 0;
                                        //flush_gunzip_window();
                                        w = 0;
-                                       return -1;
+                                       return 1;
                                }
                        } while (n);
                        resumeCopy = 0;
@@ -563,7 +543,7 @@ do_copy:            do {
        return 0;
 }
 
-int inflate_stored(int my_n, int my_b_stored, int my_k_stored, int setup)
+static int inflate_stored(int my_n, int my_b_stored, int my_k_stored, int setup)
 {
        static int n, b_stored, k_stored, w;
        if (setup) {
@@ -584,7 +564,7 @@ int inflate_stored(int my_n, int my_b_stored, int my_k_stored, int setup)
                        w = 0;
                        b_stored >>= 8;
                        k_stored -= 8;
-                       return -1; // Means more stuff 2do
+                       return 1; // We have a block
                }
                b_stored >>= 8;
                k_stored -= 8;
@@ -866,51 +846,89 @@ static int inflate_block(int *e)
        }
        default:
                /* bad block type */
-               error_msg("bad block type %d\n", t);
-               return 2;
+               error_msg_and_die("bad block type %d\n", t);
+       }
+}
+
+static void calculate_gunzip_crc(void)
+{
+       int n;
+       for (n = 0; n < gunzip_outbuf_count; n++) {
+               gunzip_crc = gunzip_crc_table[((int) gunzip_crc ^ (gunzip_window[n])) & 0xff] ^ (gunzip_crc >> 8);
        }
+       gunzip_bytes_out += gunzip_outbuf_count;
+}
+
+static int inflate_get_next_window(void)
+{
+       static int needAnotherBlock = 1;
+       static int method = -1; // Method == -1 for stored, -2 for codes
+       static int e = 0;
+       
+       gunzip_outbuf_count = 0;
+
+       while(1) {
+               int ret;
+       
+               if (needAnotherBlock) {
+                       if(e) {
+                               calculate_gunzip_crc();
+                               return 0;
+                       } // Last block
+                       method = inflate_block(&e);
+                       needAnotherBlock = 0;
+               }
+       
+               switch (method) {
+                       case -1:        ret = inflate_stored(0,0,0,0);
+                                       break;
+                       case -2:        ret = inflate_codes(0,0,0,0,0);
+                                       break;
+                       default:        error_msg_and_die("inflate error %d", method);
+               }
+
+               if (ret == 1) {
+                       calculate_gunzip_crc();
+                       return 1; // More data left
+               } else needAnotherBlock = 1; // End of that block
+       }
+       /* Doesnt get here */
 }
 
 /*
- * decompress an inflated entry
+ * User functions
  *
- * GLOBAL VARIABLES: gunzip_outbuf_count, bk, gunzip_bb, hufts, inptr
+ * read_gz, GZ_gzReadOpen, GZ_gzReadClose, inflate
  */
 
 extern ssize_t read_gz(int fd, void *buf, size_t count)
 {
-       static int e = 0;                               /* last block flag */
-       int r;                          /* result code */
-       unsigned h = 0;         /* maximum struct huft's malloc'ed */
-       ssize_t written = count;
-       static char *output_buffer_ptr = 0;
-
-       while (output_buffer_len == 0) { // We need more data
-               if (e) return 0; // No more data here!
-               gunzip_hufts = 0;
-               r = inflate_block(&e);
-               if (r == -1) { // Call inflate_stored while returning -1
-                       while(inflate_stored(0,0,0,0) == -1) flush_gunzip_window();
-               } else if (r == -2) { // Call inflate_codes while returning -1
-                       while(inflate_codes(0,0,0,0,0) == -1) flush_gunzip_window();
-               } else {
-                       error_msg_and_die("inflate error %d", r);
-                       return -1;
-               }
-               if (gunzip_hufts > h) {
-                       h = gunzip_hufts;
-               }
-               if (e) { // Ok finished uncompressing, get any buffered uncompressed data
-                       flush_gunzip_window();
-               }
-               output_buffer_ptr = output_buffer;
+       static int morebytes = 0, finished = 0;
+       
+       if (morebytes) {
+               int bytesRead = morebytes > count ? count : morebytes;
+               memcpy(buf, gunzip_window + (gunzip_outbuf_count - morebytes), bytesRead);
+               morebytes -= bytesRead;
+               return bytesRead;
+       } else if (finished) {
+               return 0;
+       } else if (count >= 0x8000) { // We can decompress direcly to the buffer, 32k at a time
+               // Could decompress to larger buffer, but it must be a power of 2, and calculating that is probably more expensive than the benefit
+               unsigned char *old_gunzip_window = gunzip_window; // Save old window
+               gunzip_window = buf;
+               if (inflate_get_next_window() == 0) finished = 1;
+               gunzip_window = old_gunzip_window; // Restore old window
+               return gunzip_outbuf_count;
+       } else { // Oh well, need to split up the gunzip_window
+               int bytesRead;
+               if (inflate_get_next_window() == 0) finished = 1;
+               morebytes = gunzip_outbuf_count;
+               bytesRead = morebytes > count ? count : morebytes;
+               memcpy(buf, gunzip_window, bytesRead);
+               morebytes -= bytesRead;
+               return bytesRead;
        }
-       if (count > output_buffer_len) written = output_buffer_len; // We're only giving them as much as we have!
-       memcpy(buf, output_buffer_ptr, written);
-       output_buffer_ptr += written;
-       output_buffer_len -= written;
-
-       return written;
+       
 }
 
 extern void GZ_gzReadOpen(int fd, void *unused, int nUnused)
@@ -919,12 +937,12 @@ extern void GZ_gzReadOpen(int fd, void *unused, int nUnused)
 
        /* Allocate all global buffers (for DYN_ALLOC option) */
        gunzip_window = xmalloc(gunzip_wsize);
-       output_buffer = xmalloc(gunzip_wsize);
        gunzip_outbuf_count = 0;
        gunzip_bytes_out = 0;
        gunzip_src_fd = fd;
 
-       gunzip_in_buffer = malloc(8);
+       /* Input buffer */
+       bytebuffer = xmalloc(BYTEBUFFER_MAX);
 
        /* initialize gunzip_window, bit buffer */
        gunzip_bk = 0;
@@ -938,26 +956,23 @@ extern void GZ_gzReadClose(void)
 {
        /* Cleanup */
        free(gunzip_window);
-       free(output_buffer);
        free(gunzip_crc_table);
 
        /* Store unused bytes in a global buffer so calling applets can access it */
-       gunzip_in_buffer_count = 0;
        if (gunzip_bk >= 8) {
                /* Undo too much lookahead. The next read will be byte aligned
                 * so we can discard unused bits in the last meaningful byte. */
-               gunzip_in_buffer[gunzip_in_buffer_count] = gunzip_bb & 0xff;
-               gunzip_in_buffer_count++;
+               bytebuffer_offset--;
+               bytebuffer[bytebuffer_offset] = gunzip_bb & 0xff;
                gunzip_bb >>= 8;
                gunzip_bk -= 8;
        }
 }
 
-extern int inflate(int in, int out)
+/*extern int inflate(int in, int out) // Useful for testing read_gz
 {
        char buf[8192];
        ssize_t nread, nwrote;
-       ssize_t total = 0;
 
        GZ_gzReadOpen(in, 0, 0);
        while(1) { // Robbed from copyfd.c
@@ -972,8 +987,54 @@ extern int inflate(int in, int out)
                        perror_msg("write");
                        return -1;
                }
-               total += nwrote;
        }
        GZ_gzReadClose();
        return 0;
+}*/
+
+extern int inflate(int in, int out)
+{
+       ssize_t nwrote;
+       GZ_gzReadOpen(in, 0, 0);
+       while(1) {
+               int ret = inflate_get_next_window();
+               nwrote = full_write(out, gunzip_window, gunzip_outbuf_count);
+               if (nwrote == -1) {
+                       perror_msg("write");
+                       return -1;
+               }
+               if (ret == 0) break;
+       }
+       GZ_gzReadClose();
+       return 0;
+}
+
+extern void check_trailer_gzip(int src_fd)
+{
+       unsigned int stored_crc = 0;
+       unsigned char count;
+
+       /* top up the input buffer with the rest of the trailer */
+       count = bytebuffer_size - bytebuffer_offset;
+       if (count < 8) {
+               xread_all(src_fd, &bytebuffer[bytebuffer_size], 8 - count);
+               bytebuffer_size += 8 - count;
+       }
+       for (count = 0; count != 4; count++) {
+               stored_crc |= (bytebuffer[bytebuffer_offset] << (count * 8));
+               bytebuffer_offset++;
+       }
+
+       /* Validate decompression - crc */
+       if (stored_crc != (gunzip_crc ^ 0xffffffffL)) {
+               error_msg_and_die("crc error");
+       }
+
+       /* Validate decompression - size */
+       if (gunzip_bytes_out !=
+               (bytebuffer[bytebuffer_offset] | (bytebuffer[bytebuffer_offset+1] << 8) |
+               (bytebuffer[bytebuffer_offset+2] << 16) | (bytebuffer[bytebuffer_offset+3] << 24))) {
+               error_msg_and_die("Incorrect length, but crc is correct");
+       }
+
 }