X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=archival%2Flibunarchive%2Fdecompress_unzip.c;h=621d84c2d51ff97ef20fd07538fa9ffc60ed0582;hb=714701c890b5f03253c5ecdb7367c4258ce78715;hp=3215697aa70ed0aa6af4a667a9c681766c6793d2;hpb=7ab5f4d03d4541b96ccd42957eb74ff9f7728ebf;p=oweals%2Fbusybox.git diff --git a/archival/libunarchive/decompress_unzip.c b/archival/libunarchive/decompress_unzip.c index 3215697aa..621d84c2d 100644 --- a/archival/libunarchive/decompress_unzip.c +++ b/archival/libunarchive/decompress_unzip.c @@ -21,21 +21,6 @@ * to free leaked bytebuffer memory (used in unzip.c), and some minor style * guide cleanups by Ed Clark * - * 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 - * - * * gzip (GNU zip) -- compress files with zip algorithm and 'compress' interface * Copyright (C) 1992-1993 Jean-loup Gailly * The unzip code was written and put in the public domain by Mark Adler. @@ -43,37 +28,11 @@ * 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 "libbb.h" #include "unarchive.h" @@ -95,8 +54,8 @@ static unsigned int gunzip_outbuf_count; /* bytes in output buffer */ 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) */ @@ -155,9 +114,8 @@ static unsigned int fill_bitbuffer(unsigned int bitbuffer, unsigned int *current /* 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 (!(bytebuffer_size = bb_xread(gunzip_src_fd, &bytebuffer[4], bytebuffer_max - 8))) { + 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; } @@ -165,36 +123,7 @@ static unsigned int fill_bitbuffer(unsigned int bitbuffer, unsigned int *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 *) xmalloc(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; } /* @@ -242,17 +171,17 @@ int huft_build(unsigned int *b, const unsigned int n, 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 h; /* table level */ - register unsigned i; /* counter, current code */ - register unsigned j; /* counter */ - register int k; /* number of bits in current code */ - register unsigned *p; /* pointer into c[], b[], or v[] */ - register huft_t *q; /* points to current table */ + 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 */ - register int w; /* bits decoded */ + 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 */ @@ -313,7 +242,7 @@ int huft_build(unsigned int *b, const unsigned int 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 */ + 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 */ @@ -325,8 +254,8 @@ 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 > ws[h + 1]) { - w = ws[++h]; + 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 */ @@ -343,22 +272,22 @@ int huft_build(unsigned int *b, const unsigned int n, } 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[h+1] = w + j; /* set bits decoded in stack */ + 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) (w - ws[h - 1]); /* bits to dump before this 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[h - 1]; - u[h - 1][j] = r; /* connect to last table */ + j = (i & ((1 << w) - 1)) >> ws[htl - 1]; + u[htl - 1][j] = r; /* connect to last table */ } } @@ -387,8 +316,8 @@ int huft_build(unsigned int *b, const unsigned int n, i ^= j; /* backup over finished tables */ - while ((i & ((1 << w) - 1)) != x[h]) { - w = ws[--h]; + while ((i & ((1 << w) - 1)) != x[htl]) { + w = ws[--htl]; } } } @@ -582,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 */ @@ -816,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,7 +770,7 @@ static int inflate_block(int *e) } 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); } } @@ -877,11 +806,11 @@ static int inflate_get_next_window(void) } 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) { @@ -906,8 +835,10 @@ void inflate_cleanup(void) free(bytebuffer); } -int inflate_unzip(int in, int out) +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); @@ -922,18 +853,20 @@ int inflate_unzip(int in, int out) 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 = bb_full_write(out, gunzip_window, gunzip_outbuf_count); + 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; } @@ -950,20 +883,22 @@ int inflate_unzip(int in, int out) gunzip_bb >>= 8; gunzip_bk -= 8; } - return 0; + return USE_DESKTOP(total) + 0; } -int inflate_gunzip(int in, int out) +USE_DESKTOP(long long) int +inflate_gunzip(int in, int out) { - unsigned int stored_crc = 0; + uint32_t stored_crc = 0; unsigned int count; + USE_DESKTOP(long long total = )inflate_unzip(in, out); - inflate_unzip(in, out); + 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(in, &bytebuffer[bytebuffer_size], 8 - count); + xread(in, &bytebuffer[bytebuffer_size], 8 - count); bytebuffer_size += 8 - count; } for (count = 0; count != 4; count++) { @@ -972,7 +907,7 @@ int inflate_gunzip(int in, int out) } /* Validate decompression - crc */ - if (stored_crc != (gunzip_crc ^ 0xffffffffL)) { + if (stored_crc != (~gunzip_crc)) { bb_error_msg("crc error"); return -1; } @@ -981,9 +916,9 @@ int inflate_gunzip(int in, int out) 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("Incorrect length"); + bb_error_msg("incorrect length"); return -1; } - return 0; + return USE_DESKTOP(total) + 0; }