X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=archival%2Flibunarchive%2Fdecompress_uncompress.c;h=2877c898190622bcc6f6583d7ad77f56738bbb22;hb=fe4ef36bcd660598cac4bdb5f428b3f0bdbc3fa3;hp=ff98254ffb948d70d0a511c6ef79ab3b4ed6c1bb;hpb=97a8dd3857aea9730382e2975a2ee2000fd23ebb;p=oweals%2Fbusybox.git diff --git a/archival/libunarchive/decompress_uncompress.c b/archival/libunarchive/decompress_uncompress.c index ff98254ff..2877c8981 100644 --- a/archival/libunarchive/decompress_uncompress.c +++ b/archival/libunarchive/decompress_uncompress.c @@ -1,13 +1,10 @@ /* vi: set sw=4 ts=4: */ -#include "libbb.h" - /* uncompress for busybox -- (c) 2002 Robert Griebl * * based on the original compress42.c source * (see disclaimer below) */ - /* (N)compress42.c - File compression ala IEEE Computer, Mar 1992. * * Authors: @@ -26,9 +23,10 @@ * [... History snipped ...] * */ -#include -#include -#include + +#include "libbb.h" +#include "unarchive.h" + /* Default input buffer size */ #define IBUFSIZ 2048 @@ -37,97 +35,101 @@ #define OBUFSIZ 2048 /* Defines for third byte of header */ -#define MAGIC_1 (char_type)'\037' /* First byte of compressed file */ -#define MAGIC_2 (char_type)'\235' /* Second byte of compressed file */ -#define BIT_MASK 0x1f /* Mask for 'number of compresssion bits' */ - /* Masks 0x20 and 0x40 are free. */ - /* I think 0x20 should mean that there is */ - /* a fourth header byte (for expansion). */ -#define BLOCK_MODE 0x80 /* Block compresssion if table is full and */ - /* compression rate is dropping flush tables */ - /* the next two codes should not be changed lightly, as they must not */ - /* lie within the contiguous general code space. */ -#define FIRST 257 /* first free entry */ -#define CLEAR 256 /* table clear output code */ - -#define INIT_BITS 9 /* initial number of bits/code */ - +#define BIT_MASK 0x1f /* Mask for 'number of compresssion bits' */ + /* Masks 0x20 and 0x40 are free. */ + /* I think 0x20 should mean that there is */ + /* a fourth header byte (for expansion). */ +#define BLOCK_MODE 0x80 /* Block compression if table is full and */ + /* compression rate is dropping flush tables */ + /* the next two codes should not be changed lightly, as they must not */ + /* lie within the contiguous general code space. */ +#define FIRST 257 /* first free entry */ +#define CLEAR 256 /* table clear output code */ -/* machine variants which require cc -Dmachine: pdp11, z8000, DOS */ -#define FAST - -#define HBITS 17 /* 50% occupancy */ -#define HSIZE (1< BITS) { - bb_error_msg("compressed with %d bits, can only handle %d bits", maxbits, - BITS); - return -1; + bb_error_msg("compressed with %d bits, can only handle " + BITS_STR" bits", maxbits); + goto err; } - maxcode = MAXCODE(n_bits = INIT_BITS) - 1; - bitmask = (1 << n_bits) - 1; + n_bits = INIT_BITS; + maxcode = MAXCODE(INIT_BITS) - 1; + bitmask = (1 << INIT_BITS) - 1; oldcode = -1; finchar = 0; outpos = 0; @@ -136,20 +138,21 @@ uncompress(int fd_in, int fd_out) free_ent = ((block_mode) ? FIRST : 256); /* As above, initialize the first 256 entries in the table. */ - clear_tab_prefixof(); + /*clear_tab_prefixof(); - done by xzalloc */ for (code = 255; code >= 0; --code) { tab_suffixof(code) = (unsigned char) code; } do { - resetbuf:; + resetbuf: { int i; int e; int o; - e = insize - (o = (posbits >> 3)); + o = posbits >> 3; + e = insize - o; for (i = 0; i < e; ++i) inbuf[i] = inbuf[i + o]; @@ -160,6 +163,7 @@ uncompress(int fd_in, int fd_out) if (insize < (int) (IBUFSIZ + 64) - IBUFSIZ) { rsize = safe_read(fd_in, inbuf + insize, IBUFSIZ); +//error check?? insize += rsize; } @@ -184,8 +188,8 @@ uncompress(int fd_in, int fd_out) { unsigned char *p = &inbuf[posbits >> 3]; - code = ((((long) (p[0])) | ((long) (p[1]) << 8) | - ((long) (p[2]) << 16)) >> (posbits & 0x7)) & bitmask; + code = ((((long) (p[0])) | ((long) (p[1]) << 8) | + ((long) (p[2]) << 16)) >> (posbits & 0x7)) & bitmask; } posbits += n_bits; @@ -204,8 +208,9 @@ uncompress(int fd_in, int fd_out) ((posbits - 1) + ((n_bits << 3) - (posbits - 1 + (n_bits << 3)) % (n_bits << 3))); - maxcode = MAXCODE(n_bits = INIT_BITS) - 1; - bitmask = (1 << n_bits) - 1; + n_bits = INIT_BITS; + maxcode = MAXCODE(INIT_BITS) - 1; + bitmask = (1 << INIT_BITS) - 1; goto resetbuf; } @@ -225,7 +230,7 @@ uncompress(int fd_in, int fd_out) insize, posbits, p[-1], p[0], p[1], p[2], p[3], (posbits & 07)); bb_error_msg("uncompress: corrupt input"); - return -1; + goto err; } *--stackp = (unsigned char) finchar; @@ -233,18 +238,20 @@ uncompress(int fd_in, int fd_out) } /* Generate output characters in reverse order */ - while ((long int) code >= (long int) 256) { + while ((long) code >= (long) 256) { *--stackp = tab_suffixof(code); code = tab_prefixof(code); } - *--stackp = (unsigned char) (finchar = tab_suffixof(code)); + finchar = tab_suffixof(code); + *--stackp = (unsigned char) finchar; /* And put them out in forward order */ { int i; - if (outpos + (i = (de_stack - stackp)) >= OBUFSIZ) { + i = de_stack - stackp; + if (outpos + i >= OBUFSIZ) { do { if (i > OBUFSIZ - outpos) { i = OBUFSIZ - outpos; @@ -256,12 +263,14 @@ uncompress(int fd_in, int fd_out) } if (outpos >= OBUFSIZ) { - write(fd_out, outbuf, outpos); - USE_DESKTOP(total_written += outpos;) + full_write(fd_out, outbuf, outpos); +//error check?? + IF_DESKTOP(total_written += outpos;) outpos = 0; } stackp += i; - } while ((i = (de_stack - stackp)) > 0); + i = de_stack - stackp; + } while (i > 0); } else { memcpy(outbuf + outpos, stackp, i); outpos += i; @@ -269,7 +278,8 @@ uncompress(int fd_in, int fd_out) } /* Generate the new entry. */ - if ((code = free_ent) < maxmaxcode) { + code = free_ent; + if (code < maxmaxcode) { tab_prefixof(code) = (unsigned short) oldcode; tab_suffixof(code) = (unsigned char) finchar; free_ent = code + 1; @@ -282,11 +292,16 @@ uncompress(int fd_in, int fd_out) } while (rsize > 0); if (outpos > 0) { - write(fd_out, outbuf, outpos); - USE_DESKTOP(total_written += outpos;) + full_write(fd_out, outbuf, outpos); +//error check?? + IF_DESKTOP(total_written += outpos;) } - RELEASE_CONFIG_BUFFER(inbuf); - RELEASE_CONFIG_BUFFER(outbuf); - return USE_DESKTOP(total_written) + 0; + retval = IF_DESKTOP(total_written) + 0; + err: + free(inbuf); + free(outbuf); + free(htab); + free(codetab); + return retval; }