X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=archival%2Flibunarchive%2Fdecompress_unzip.c;h=621d84c2d51ff97ef20fd07538fa9ffc60ed0582;hb=714701c890b5f03253c5ecdb7367c4258ce78715;hp=2b16db3c3f1bd252cefac954899ce0d2a6250f00;hpb=cb81e6484d1f50ec2761f6294722407b14add525;p=oweals%2Fbusybox.git diff --git a/archival/libunarchive/decompress_unzip.c b/archival/libunarchive/decompress_unzip.c index 2b16db3c3..621d84c2d 100644 --- a/archival/libunarchive/decompress_unzip.c +++ b/archival/libunarchive/decompress_unzip.c @@ -12,24 +12,14 @@ * command line handling. * * General cleanup to better adhere to the style guide and make use of standard - * busybox functions by Glenn McGrath - * - * 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 - * (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. + * busybox functions by Glenn McGrath * - * 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 + * read_gz interface + associated hacking by Laurence Anderson * + * Fixed huft_build() so decoding end-of-block code does not grab more bits + * than necessary (this is required by unzip applet), added inflate_cleanup() + * to free leaked bytebuffer memory (used in unzip.c), and some minor style + * guide cleanups by Ed Clark * * gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface * Copyright (C) 1992-1993 Jean-loup Gailly @@ -38,39 +28,12 @@ * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies, * Ken Turkowski, Dave Mack and Peter Jannesen. * - * See the license_msg below and the file COPYING for the software license. * See the file algorithm.doc for the compression algorithms and file formats. + * + * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. */ -#if 0 -static char *license_msg[] = { - " Copyright (C) 1992-1993 Jean-loup Gailly", - " 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, 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., 675 Mass Ave, Cambridge, MA 02139, USA.", - 0 -}; -#endif - -#include -#include -#include -#include -#include -#include -#include -#include "config.h" -#include "busybox.h" +#include "libbb.h" #include "unarchive.h" typedef struct huft_s { @@ -88,11 +51,11 @@ static unsigned int gunzip_outbuf_count; /* bytes in output 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; +enum { gunzip_wsize = 0x8000 }; static unsigned char *gunzip_window; -static unsigned int *gunzip_crc_table; -unsigned int gunzip_crc; +static uint32_t *gunzip_crc_table; +uint32_t gunzip_crc; /* 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) */ @@ -103,7 +66,7 @@ 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 int bytebuffer_max = 0x8000; static unsigned char *bytebuffer = NULL; static unsigned int bytebuffer_offset = 0; static unsigned int bytebuffer_size = 0; @@ -116,26 +79,26 @@ static const unsigned short mask_bits[] = { /* Copy lengths for literal codes 257..285 */ static const unsigned short cplens[] = { 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, - 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0 + 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0 }; /* note: see note #13 above about the 258 in this list. */ /* Extra bits for literal codes 257..285 */ static const unsigned char cplext[] = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, - 5, 5, 5, 0, 99, 99 + 5, 5, 5, 0, 99, 99 }; /* 99==invalid */ /* Copy offsets for distance codes 0..29 */ static const unsigned short cpdist[] = { 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, - 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577 + 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577 }; /* Extra bits for distance codes */ static const unsigned char cpdext[] = { 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, - 11, 11, 12, 12, 13, 13 + 11, 11, 12, 12, 13, 13 }; /* Tables for deflate from PKZIP's appnote.txt. */ @@ -144,61 +107,29 @@ 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 + bb_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) { - fill_bytebuffer(); + 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() */ + if (1 > (bytebuffer_size = safe_read(gunzip_src_fd, &bytebuffer[4], bytebuffer_max - 8))) + bb_error_msg_and_die("unexpected end of file"); + bytebuffer_size += 4; + bytebuffer_offset = 4; + } bitbuffer |= ((unsigned int) bytebuffer[bytebuffer_offset]) << *current; bytebuffer_offset++; *current += 8; } - return(bitbuffer); -} - -static void make_gunzip_crc_table(void) -{ - const unsigned int poly = 0xedb88320; /* polynomial exclusive-or pattern */ - unsigned short i; /* counter for all possible eight bit values */ - - /* initial shift register value */ - gunzip_crc = 0xffffffffL; - gunzip_crc_table = (unsigned int *) malloc(256 * sizeof(unsigned int)); - - /* Compute and print table of CRC's, five per line */ - for (i = 0; i < 256; i++) { - unsigned int table_entry; /* crc shift register */ - unsigned 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 - */ - for (k = 8; k; k--) { - if (table_entry & 1) { - table_entry = (table_entry >> 1) ^ poly; - } else { - table_entry >>= 1; - } - } - gunzip_crc_table[i] = table_entry; - } + return bitbuffer; } /* * Free the malloc'ed tables built by huft_build(), which makes a linked * list of the tables it made, with the links in a dummy first entry of - * each table. + * each table. * t: table to free */ static int huft_free(huft_t * t) @@ -230,70 +161,60 @@ static int huft_free(huft_t * t) * t: result: starting table * m: maximum lookup bits, returns actual */ -static int huft_build(unsigned int *b, const unsigned int n, - const unsigned int s, const unsigned short *d, - const unsigned char *e, huft_t ** t, int *m) +static +int huft_build(unsigned int *b, const unsigned int n, + const unsigned int s, const unsigned short *d, + const unsigned char *e, huft_t ** t, unsigned int *m) { - unsigned a; /* counter for codes of length k */ + unsigned a; /* counter for codes of length k */ unsigned c[BMAX + 1]; /* bit length count table */ - unsigned f; /* i repeats in table every f entries */ - int g; /* maximum code length */ - int h; /* table level */ - register unsigned i; /* counter, current code */ - register unsigned j; /* counter */ - register int k; /* number of bits in current code */ - int l; /* bits per table (returned in m) */ - register unsigned *p; /* pointer into c[], b[], or v[] */ - register huft_t *q; /* points to current table */ - huft_t r; /* table entry for structure assignment */ - huft_t *u[BMAX]; /* table stack */ - unsigned v[N_MAX]; /* values in order of bit length */ - register int w; /* bits before this table == (l * h) */ + unsigned eob_len; /* length of end-of-block code (value 256) */ + unsigned f; /* i repeats in table every f entries */ + int g; /* maximum code length */ + int htl; /* table level */ + unsigned i; /* counter, current code */ + unsigned j; /* counter */ + int k; /* number of bits in current code */ + unsigned *p; /* pointer into c[], b[], or v[] */ + huft_t *q; /* points to current table */ + huft_t r; /* table entry for structure assignment */ + huft_t *u[BMAX]; /* table stack */ + unsigned v[N_MAX]; /* values in order of bit length */ + int ws[BMAX+1]; /* bits decoded stack */ + int w; /* bits decoded */ unsigned x[BMAX + 1]; /* bit offsets, then code stack */ - unsigned *xp; /* pointer into x */ - int y; /* number of dummy codes added */ - unsigned z; /* number of entries in current table */ + unsigned *xp; /* pointer into x */ + int y; /* number of dummy codes added */ + unsigned z; /* number of entries in current table */ + + /* Length of EOB code, if any */ + eob_len = n > 256 ? b[256] : BMAX; /* Generate counts for each bit length */ - memset((void *) (c), 0, sizeof(c)); + memset((void *)c, 0, sizeof(c)); p = b; i = n; do { - c[*p]++; /* assume all entries <= BMAX */ - p++; /* Can't combine with above line (Solaris bug) */ + c[*p]++; /* assume all entries <= BMAX */ + p++; /* Can't combine with above line (Solaris bug) */ } while (--i); - if (c[0] == n) { /* null input--all zero length codes */ + if (c[0] == n) { /* null input--all zero length codes */ *t = (huft_t *) NULL; *m = 0; - return 0; + return 2; } /* Find minimum and maximum length, bound *m by those */ - l = *m; - for (j = 1; j <= BMAX; j++) { - if (c[j]) { - break; - } - } - k = j; /* minimum code length */ - if ((unsigned) l < j) { - l = j; - } - for (i = BMAX; i; i--) { - if (c[i]) { - break; - } - } - g = i; /* maximum code length */ - if ((unsigned) l > i) { - l = i; - } - *m = l; + for (j = 1; (c[j] == 0) && (j <= BMAX); j++); + k = j; /* minimum code length */ + for (i = BMAX; (c[i] == 0) && i; i--); + g = i; /* maximum code length */ + *m = (*m < j) ? j : ((*m > i) ? i : *m); /* Adjust last length count to fill out codes, if needed */ for (y = 1 << j; j < i; j++, y <<= 1) { if ((y -= c[j]) < 0) { - return 2; /* bad input: more codes than bits */ + return 2; /* bad input: more codes than bits */ } } if ((y -= c[i]) < 0) { @@ -305,7 +226,7 @@ static int huft_build(unsigned int *b, const unsigned int n, x[1] = j = 0; p = c + 1; xp = x + 2; - while (--i) { /* note that i == g from above */ + while (--i) { /* note that i == g from above */ *xp++ = (j += *p++); } @@ -319,13 +240,13 @@ static int huft_build(unsigned int *b, const unsigned int n, } while (++i < n); /* Generate the Huffman codes and for each, make the table entries */ - x[0] = i = 0; /* first Huffman code is zero */ - p = v; /* grab values in bit order */ - h = -1; /* no tables yet--level -1 */ - w = -l; /* bits decoded == (l * h) */ + x[0] = i = 0; /* first Huffman code is zero */ + p = v; /* grab values in bit order */ + htl = -1; /* no tables yet--level -1 */ + w = ws[0] = 0; /* bits decoded */ u[0] = (huft_t *) NULL; /* just to keep compilers happy */ q = (huft_t *) NULL; /* ditto */ - z = 0; /* ditto */ + z = 0; /* ditto */ /* go through the bit lengths (k already is bits in shortest code) */ for (; k <= g; k++) { @@ -333,52 +254,52 @@ static int huft_build(unsigned int *b, const unsigned int n, while (a--) { /* here i is the Huffman code of length k bits for value *p */ /* make tables up to required level */ - while (k > w + l) { - h++; - w += l; /* previous table always l bits */ - - /* compute minimum size table less than or equal to l bits */ - z = (z = g - w) > (unsigned) l ? l : z; /* upper limit on table size */ - if ((f = 1 << (j = k - w)) > a + 1) { /* try a k-w bit table *//* too few codes for k-w bit table */ - f -= a + 1; /* deduct codes from patterns left */ + while (k > ws[htl + 1]) { + w = ws[++htl]; + + /* compute minimum size table less than or equal to *m bits */ + z = (z = g - w) > *m ? *m : z; /* upper limit on table size */ + if ((f = 1 << (j = k - w)) > a + 1) { /* try a k-w bit table */ + /* too few codes for k-w bit table */ + f -= a + 1; /* deduct codes from patterns left */ xp = c + k; - while (++j < z) { /* try smaller tables up to z bits */ + while (++j < z) { /* try smaller tables up to z bits */ if ((f <<= 1) <= *++xp) { - break; /* enough codes to use up j bits */ + break; /* enough codes to use up j bits */ } - f -= *xp; /* else deduct codes from patterns */ + f -= *xp; /* else deduct codes from patterns */ } } + j = (w + j > eob_len && w < eob_len) ? eob_len - w : j; /* make EOB code end at table */ z = 1 << j; /* table entries for j-bit table */ + ws[htl+1] = w + j; /* set bits decoded in stack */ /* allocate and link in new table */ - q = (huft_t *) xmalloc((z + 1) * sizeof(huft_t)); - + q = (huft_t *) xzalloc((z + 1) * sizeof(huft_t)); *t = q + 1; /* link to list for huft_free() */ - *(t = &(q->v.t)) = NULL; - u[h] = ++q; /* table starts after link */ + t = &(q->v.t); + u[htl] = ++q; /* table starts after link */ /* connect to last table, if there is one */ - if (h) { - x[h] = i; /* save pattern for backing up */ - r.b = (unsigned char) l; /* bits to dump before this table */ - r.e = (unsigned char) (16 + j); /* bits in this table */ - r.v.t = q; /* pointer to this table */ - j = i >> (w - l); /* (get around Turbo C bug) */ - u[h - 1][j] = r; /* connect to last table */ + if (htl) { + x[htl] = i; /* save pattern for backing up */ + r.b = (unsigned char) (w - ws[htl - 1]); /* bits to dump before this table */ + r.e = (unsigned char) (16 + j); /* bits in this table */ + r.v.t = q; /* pointer to this table */ + j = (i & ((1 << w) - 1)) >> ws[htl - 1]; + u[htl - 1][j] = r; /* connect to last table */ } } /* set up table entry in r */ r.b = (unsigned char) (k - w); if (p >= v + n) { - r.e = 99; /* out of values--invalid code */ + r.e = 99; /* out of values--invalid code */ } else if (*p < s) { - r.e = (unsigned char) (*p < 256 ? 16 : 15); /* 256 is end-of-block code */ - r.v.n = (unsigned short) (*p); /* simple code is just the value */ - p++; /* one compiler does not like *p++ */ + r.e = (unsigned char) (*p < 256 ? 16 : 15); /* 256 is EOB code */ + r.v.n = (unsigned short) (*p++); /* simple code is just the value */ } else { - r.e = (unsigned char) e[*p - s]; /* non-simple--look up in lists */ + r.e = (unsigned char) e[*p - s]; /* non-simple--look up in lists */ r.v.n = d[*p++ - s]; } @@ -395,12 +316,15 @@ static int huft_build(unsigned int *b, const unsigned int n, i ^= j; /* backup over finished tables */ - while ((i & ((1 << w) - 1)) != x[h]) { - h--; /* don't need to update q */ - w -= l; + while ((i & ((1 << w) - 1)) != x[htl]) { + w = ws[--htl]; } } } + + /* return actual size of base table */ + *m = ws[1]; + /* Return true (1) if we were given an incomplete table */ return y != 0 && g != 1; } @@ -440,15 +364,15 @@ static int inflate_codes(huft_t * my_tl, huft_t * my_td, const unsigned int my_b md = mask_bits[bd]; return 0; // Don't actually do anything the first time } - + if (resumeCopy) goto do_copy; - + while (1) { /* do until end of block */ b = fill_bitbuffer(b, &k, bl); if ((e = (t = tl + ((unsigned) b & ml))->e) > 16) do { if (e == 99) { - bb_error_msg_and_die("inflate_codes error 1");; + bb_error_msg_and_die("inflate_codes error 1"); } b >>= t->b; k -= t->b; @@ -484,7 +408,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) - bb_error_msg_and_die("inflate_codes error 2");; + bb_error_msg_and_die("inflate_codes error 2"); b >>= t->b; k -= t->b; e -= 16; @@ -538,14 +462,14 @@ do_copy: do { /* free the decoding tables, return */ huft_free(tl); huft_free(td); - + /* done */ return 0; } 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; + static unsigned int n, b_stored, k_stored, w; if (setup) { n = my_n; b_stored = my_b_stored; @@ -553,12 +477,12 @@ static int inflate_stored(int my_n, int my_b_stored, int my_k_stored, int setup) w = gunzip_outbuf_count; /* initialize gunzip_window position */ return 0; // Don't do anything first time } - + /* read and output the compressed data */ while (n--) { b_stored = fill_bitbuffer(b_stored, &k_stored, 8); gunzip_window[w++] = (unsigned char) b_stored; - if (w == (unsigned int) gunzip_wsize) { + if (w == gunzip_wsize) { gunzip_outbuf_count = (w); //flush_gunzip_window(); w = 0; @@ -587,7 +511,7 @@ static int inflate_stored(int my_n, int my_b_stored, int my_k_stored, int setup) static int inflate_block(int *e) { unsigned t; /* block type */ - register unsigned int b; /* bit buffer */ + unsigned int b; /* bit buffer */ unsigned int k; /* number of bits in bit buffer */ /* make local bit buffer */ @@ -644,7 +568,7 @@ static int inflate_block(int *e) inflate_stored(n, b_stored, k_stored, 1); // Setup inflate_stored return -1; } - case 1: /* Inflate fixed + case 1: /* Inflate fixed * decompress an inflated type 1 (fixed Huffman codes) block. We should * either replace this with a custom decoder, or at least precompute the * Huffman tables. @@ -687,9 +611,9 @@ static int inflate_block(int *e) /* decompress until an end-of-block code */ inflate_codes(tl, td, bl, bd, 1); // Setup inflate_codes - + /* huft_free code moved into inflate_codes */ - + return -2; } case 2: /* Inflate dynamic */ @@ -821,7 +745,7 @@ static int inflate_block(int *e) if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0) { if (i == 1) { - bb_error_msg_and_die("Incomplete literal tree"); + bb_error_msg_and_die("incomplete literal tree"); huft_free(tl); } return i; /* incomplete code set */ @@ -841,12 +765,12 @@ static int inflate_block(int *e) inflate_codes(tl, td, bl, bd, 1); // Setup inflate_codes /* huft_free code moved into inflate_codes */ - + return -2; } default: /* bad block type */ - bb_error_msg_and_die("bad block type %d\n", t); + bb_error_msg_and_die("bad block type %d", t); } } @@ -861,30 +785,32 @@ static void calculate_gunzip_crc(void) 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; - + static int needAnotherBlock = 1; + gunzip_outbuf_count = 0; while(1) { int ret; - + if (needAnotherBlock) { if(e) { calculate_gunzip_crc(); + e = 0; + needAnotherBlock = 1; 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: bb_error_msg_and_die("inflate error %d", method); + case -1: ret = inflate_stored(0,0,0,0); + break; + case -2: ret = inflate_codes(0,0,0,0,0); + break; + default: bb_error_msg_and_die("inflate error %d", method); } if (ret == 1) { @@ -895,65 +821,55 @@ static int inflate_get_next_window(void) /* Doesnt get here */ } -/* - * User functions - * - * read_gz, GZ_gzReadOpen, GZ_gzReadClose, inflate - */ +/* Initialise bytebuffer, be careful not to overfill the buffer */ +void inflate_init(unsigned int bufsize) +{ + /* Set the bytebuffer size, default is same as gunzip_wsize */ + bytebuffer_max = bufsize + 8; + bytebuffer_offset = 4; + bytebuffer_size = 0; +} -extern ssize_t read_gz(int fd, void *buf, size_t count) +void inflate_cleanup(void) { - 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; - } - + free(bytebuffer); } -extern void GZ_gzReadOpen(int fd, void *unused, int nUnused) +USE_DESKTOP(long long) int +inflate_unzip(int in, int out) { + USE_DESKTOP(long long total = 0;) + ssize_t nwrote; typedef void (*sig_type) (int); /* Allocate all global buffers (for DYN_ALLOC option) */ gunzip_window = xmalloc(gunzip_wsize); gunzip_outbuf_count = 0; gunzip_bytes_out = 0; - gunzip_src_fd = fd; - - /* Input buffer */ - bytebuffer = xmalloc(BYTEBUFFER_MAX); + gunzip_src_fd = in; /* initialize gunzip_window, bit buffer */ gunzip_bk = 0; gunzip_bb = 0; /* Create the crc table */ - make_gunzip_crc_table(); -} + gunzip_crc_table = crc32_filltable(0); + gunzip_crc = ~0; + + /* Allocate space for buffer */ + bytebuffer = xmalloc(bytebuffer_max); + + while(1) { + int ret = inflate_get_next_window(); + nwrote = full_write(out, gunzip_window, gunzip_outbuf_count); + if (nwrote == -1) { + bb_perror_msg("write"); + return -1; + } + USE_DESKTOP(total += nwrote;) + if (ret == 0) break; + } -extern void GZ_gzReadClose(void) -{ /* Cleanup */ free(gunzip_window); free(gunzip_crc_table); @@ -967,57 +883,22 @@ extern void GZ_gzReadClose(void) gunzip_bb >>= 8; gunzip_bk -= 8; } + return USE_DESKTOP(total) + 0; } -/*extern int inflate(int in, int out) // Useful for testing read_gz +USE_DESKTOP(long long) int +inflate_gunzip(int in, int out) { - char buf[8192]; - ssize_t nread, nwrote; - - GZ_gzReadOpen(in, 0, 0); - while(1) { // Robbed from bb_copyfd.c - nread = read_gz(in, buf, sizeof(buf)); - if (nread == 0) break; // no data to write - else if (nread == -1) { - bb_perror_msg("read"); - return -1; - } - nwrote = bb_full_write(out, buf, nread); - if (nwrote == -1) { - bb_perror_msg("write"); - return -1; - } - } - GZ_gzReadClose(); - return 0; -}*/ + uint32_t stored_crc = 0; + unsigned int count; + USE_DESKTOP(long long total = )inflate_unzip(in, out); -extern int inflate(int in, int out) -{ - ssize_t nwrote; - GZ_gzReadOpen(in, 0, 0); - while(1) { - int ret = inflate_get_next_window(); - nwrote = bb_full_write(out, gunzip_window, gunzip_outbuf_count); - if (nwrote == -1) { - bb_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; + USE_DESKTOP(if (total < 0) return total;) /* top up the input buffer with the rest of the trailer */ count = bytebuffer_size - bytebuffer_offset; if (count < 8) { - bb_xread_all(src_fd, &bytebuffer[bytebuffer_size], 8 - count); + xread(in, &bytebuffer[bytebuffer_size], 8 - count); bytebuffer_size += 8 - count; } for (count = 0; count != 4; count++) { @@ -1026,15 +907,18 @@ extern void check_trailer_gzip(int src_fd) } /* Validate decompression - crc */ - if (stored_crc != (gunzip_crc ^ 0xffffffffL)) { - bb_error_msg_and_die("crc error"); + if (stored_crc != (~gunzip_crc)) { + bb_error_msg("crc error"); + return -1; } /* 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))) { - bb_error_msg_and_die("Incorrect length, but crc is correct"); + bb_error_msg("incorrect length"); + return -1; } + return USE_DESKTOP(total) + 0; }