[821] | 1 | #include "libbb.h"
|
---|
| 2 |
|
---|
| 3 | /* uncompress for busybox -- (c) 2002 Robert Griebl
|
---|
| 4 | *
|
---|
| 5 | * based on the original compress42.c source
|
---|
| 6 | * (see disclaimer below)
|
---|
| 7 | */
|
---|
| 8 |
|
---|
| 9 |
|
---|
| 10 | /* (N)compress42.c - File compression ala IEEE Computer, Mar 1992.
|
---|
| 11 | *
|
---|
| 12 | * Authors:
|
---|
| 13 | * Spencer W. Thomas (decvax!harpo!utah-cs!utah-gr!thomas)
|
---|
| 14 | * Jim McKie (decvax!mcvax!jim)
|
---|
| 15 | * Steve Davies (decvax!vax135!petsd!peora!srd)
|
---|
| 16 | * Ken Turkowski (decvax!decwrl!turtlevax!ken)
|
---|
| 17 | * James A. Woods (decvax!ihnp4!ames!jaw)
|
---|
| 18 | * Joe Orost (decvax!vax135!petsd!joe)
|
---|
| 19 | * Dave Mack (csu@alembic.acs.com)
|
---|
| 20 | * Peter Jannesen, Network Communication Systems
|
---|
| 21 | * (peter@ncs.nl)
|
---|
| 22 | *
|
---|
| 23 | * marc@suse.de : a small security fix for a buffer overflow
|
---|
| 24 | *
|
---|
| 25 | * [... History snipped ...]
|
---|
| 26 | *
|
---|
| 27 | */
|
---|
| 28 | #include <stdio.h>
|
---|
| 29 | #include <string.h>
|
---|
| 30 | #include <unistd.h>
|
---|
| 31 |
|
---|
| 32 | /* Default input buffer size */
|
---|
| 33 | #define IBUFSIZ 2048
|
---|
| 34 |
|
---|
| 35 | /* Default output buffer size */
|
---|
| 36 | #define OBUFSIZ 2048
|
---|
| 37 |
|
---|
| 38 | /* Defines for third byte of header */
|
---|
| 39 | #define MAGIC_1 (char_type)'\037' /* First byte of compressed file */
|
---|
| 40 | #define MAGIC_2 (char_type)'\235' /* Second byte of compressed file */
|
---|
| 41 | #define BIT_MASK 0x1f /* Mask for 'number of compresssion bits' */
|
---|
| 42 | /* Masks 0x20 and 0x40 are free. */
|
---|
| 43 | /* I think 0x20 should mean that there is */
|
---|
| 44 | /* a fourth header byte (for expansion). */
|
---|
| 45 | #define BLOCK_MODE 0x80 /* Block compresssion if table is full and */
|
---|
| 46 | /* compression rate is dropping flush tables */
|
---|
| 47 | /* the next two codes should not be changed lightly, as they must not */
|
---|
| 48 | /* lie within the contiguous general code space. */
|
---|
| 49 | #define FIRST 257 /* first free entry */
|
---|
| 50 | #define CLEAR 256 /* table clear output code */
|
---|
| 51 |
|
---|
| 52 | #define INIT_BITS 9 /* initial number of bits/code */
|
---|
| 53 |
|
---|
| 54 |
|
---|
| 55 | /* machine variants which require cc -Dmachine: pdp11, z8000, DOS */
|
---|
| 56 | #define FAST
|
---|
| 57 |
|
---|
| 58 | #define HBITS 17 /* 50% occupancy */
|
---|
| 59 | #define HSIZE (1<<HBITS)
|
---|
| 60 | #define HMASK (HSIZE-1)
|
---|
| 61 | #define HPRIME 9941
|
---|
| 62 | #define BITS 16
|
---|
| 63 | #undef MAXSEG_64K
|
---|
| 64 | #define MAXCODE(n) (1L << (n))
|
---|
| 65 |
|
---|
| 66 | /* Block compress mode -C compatible with 2.0 */
|
---|
| 67 | static int block_mode = BLOCK_MODE;
|
---|
| 68 |
|
---|
| 69 | /* user settable max # bits/code */
|
---|
| 70 | static int maxbits = BITS;
|
---|
| 71 |
|
---|
| 72 | #define htabof(i) htab[i]
|
---|
| 73 | #define codetabof(i) codetab[i]
|
---|
| 74 | #define tab_prefixof(i) codetabof(i)
|
---|
| 75 | #define tab_suffixof(i) ((unsigned char *)(htab))[i]
|
---|
| 76 | #define de_stack ((unsigned char *)&(htab[HSIZE-1]))
|
---|
[902] | 77 | #define clear_htab() memset(htab, -1, HSIZE)
|
---|
[821] | 78 | #define clear_tab_prefixof() memset(codetab, 0, 256);
|
---|
| 79 |
|
---|
| 80 |
|
---|
| 81 | /*
|
---|
| 82 | * Decompress stdin to stdout. This routine adapts to the codes in the
|
---|
| 83 | * file building the "string" table on-the-fly; requiring no table to
|
---|
| 84 | * be stored in the compressed file. The tables used herein are shared
|
---|
| 85 | * with those of the compress() routine. See the definitions above.
|
---|
| 86 | */
|
---|
| 87 |
|
---|
| 88 | int uncompress(int fd_in, int fd_out)
|
---|
| 89 | {
|
---|
| 90 | unsigned char *stackp;
|
---|
| 91 | long int code;
|
---|
| 92 | int finchar;
|
---|
| 93 | long int oldcode;
|
---|
| 94 | long int incode;
|
---|
| 95 | int inbits;
|
---|
| 96 | int posbits;
|
---|
| 97 | int outpos;
|
---|
| 98 | int insize;
|
---|
| 99 | int bitmask;
|
---|
| 100 | long int free_ent;
|
---|
| 101 | long int maxcode;
|
---|
| 102 | long int maxmaxcode;
|
---|
| 103 | int n_bits;
|
---|
| 104 | int rsize = 0;
|
---|
[902] | 105 | RESERVE_CONFIG_UBUFFER(inbuf, IBUFSIZ + 64);
|
---|
| 106 | RESERVE_CONFIG_UBUFFER(outbuf, OBUFSIZ + 2048);
|
---|
| 107 | unsigned char htab[HSIZE];
|
---|
| 108 | unsigned short codetab[HSIZE];
|
---|
| 109 | memset(inbuf, 0, IBUFSIZ + 64);
|
---|
| 110 | memset(outbuf, 0, OBUFSIZ + 2048);
|
---|
[821] | 111 |
|
---|
| 112 | insize = 0;
|
---|
| 113 |
|
---|
| 114 | inbuf[0] = bb_xread_char(fd_in);
|
---|
| 115 |
|
---|
| 116 | maxbits = inbuf[0] & BIT_MASK;
|
---|
| 117 | block_mode = inbuf[0] & BLOCK_MODE;
|
---|
| 118 | maxmaxcode = MAXCODE(maxbits);
|
---|
| 119 |
|
---|
| 120 | if (maxbits > BITS) {
|
---|
| 121 | bb_error_msg("compressed with %d bits, can only handle %d bits", maxbits,
|
---|
| 122 | BITS);
|
---|
| 123 | return -1;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | maxcode = MAXCODE(n_bits = INIT_BITS) - 1;
|
---|
| 127 | bitmask = (1 << n_bits) - 1;
|
---|
| 128 | oldcode = -1;
|
---|
| 129 | finchar = 0;
|
---|
| 130 | outpos = 0;
|
---|
| 131 | posbits = 0 << 3;
|
---|
| 132 |
|
---|
| 133 | free_ent = ((block_mode) ? FIRST : 256);
|
---|
| 134 |
|
---|
| 135 | /* As above, initialize the first 256 entries in the table. */
|
---|
| 136 | clear_tab_prefixof();
|
---|
| 137 |
|
---|
| 138 | for (code = 255; code >= 0; --code) {
|
---|
| 139 | tab_suffixof(code) = (unsigned char) code;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | do {
|
---|
| 143 | resetbuf:;
|
---|
| 144 | {
|
---|
| 145 | int i;
|
---|
| 146 | int e;
|
---|
| 147 | int o;
|
---|
| 148 |
|
---|
| 149 | e = insize - (o = (posbits >> 3));
|
---|
| 150 |
|
---|
| 151 | for (i = 0; i < e; ++i)
|
---|
| 152 | inbuf[i] = inbuf[i + o];
|
---|
| 153 |
|
---|
| 154 | insize = e;
|
---|
| 155 | posbits = 0;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
[902] | 158 | if (insize < (int) (IBUFSIZ + 64) - IBUFSIZ) {
|
---|
[821] | 159 | rsize = safe_read(fd_in, inbuf + insize, IBUFSIZ);
|
---|
| 160 | insize += rsize;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | inbits = ((rsize > 0) ? (insize - insize % n_bits) << 3 :
|
---|
| 164 | (insize << 3) - (n_bits - 1));
|
---|
| 165 |
|
---|
| 166 | while (inbits > posbits) {
|
---|
| 167 | if (free_ent > maxcode) {
|
---|
| 168 | posbits =
|
---|
| 169 | ((posbits - 1) +
|
---|
| 170 | ((n_bits << 3) -
|
---|
| 171 | (posbits - 1 + (n_bits << 3)) % (n_bits << 3)));
|
---|
| 172 | ++n_bits;
|
---|
| 173 | if (n_bits == maxbits) {
|
---|
| 174 | maxcode = maxmaxcode;
|
---|
| 175 | } else {
|
---|
| 176 | maxcode = MAXCODE(n_bits) - 1;
|
---|
| 177 | }
|
---|
| 178 | bitmask = (1 << n_bits) - 1;
|
---|
| 179 | goto resetbuf;
|
---|
| 180 | }
|
---|
| 181 | {
|
---|
| 182 | unsigned char *p = &inbuf[posbits >> 3];
|
---|
| 183 |
|
---|
| 184 | code =
|
---|
| 185 | ((((long) (p[0])) | ((long) (p[1]) << 8) |
|
---|
| 186 | ((long) (p[2]) << 16)) >> (posbits & 0x7)) & bitmask;
|
---|
| 187 | }
|
---|
| 188 | posbits += n_bits;
|
---|
| 189 |
|
---|
| 190 |
|
---|
| 191 | if (oldcode == -1) {
|
---|
| 192 | outbuf[outpos++] = (unsigned char) (finchar =
|
---|
| 193 | (int) (oldcode = code));
|
---|
| 194 | continue;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | if (code == CLEAR && block_mode) {
|
---|
| 198 | clear_tab_prefixof();
|
---|
| 199 | free_ent = FIRST - 1;
|
---|
| 200 | posbits =
|
---|
| 201 | ((posbits - 1) +
|
---|
| 202 | ((n_bits << 3) -
|
---|
| 203 | (posbits - 1 + (n_bits << 3)) % (n_bits << 3)));
|
---|
| 204 | maxcode = MAXCODE(n_bits = INIT_BITS) - 1;
|
---|
| 205 | bitmask = (1 << n_bits) - 1;
|
---|
| 206 | goto resetbuf;
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | incode = code;
|
---|
| 210 | stackp = de_stack;
|
---|
| 211 |
|
---|
| 212 | /* Special case for KwKwK string. */
|
---|
| 213 | if (code >= free_ent) {
|
---|
| 214 | if (code > free_ent) {
|
---|
| 215 | unsigned char *p;
|
---|
| 216 |
|
---|
| 217 | posbits -= n_bits;
|
---|
| 218 | p = &inbuf[posbits >> 3];
|
---|
| 219 |
|
---|
| 220 | bb_error_msg
|
---|
| 221 | ("insize:%d posbits:%d inbuf:%02X %02X %02X %02X %02X (%d)",
|
---|
| 222 | insize, posbits, p[-1], p[0], p[1], p[2], p[3],
|
---|
| 223 | (posbits & 07));
|
---|
| 224 | bb_error_msg("uncompress: corrupt input");
|
---|
| 225 | return -1;
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | *--stackp = (unsigned char) finchar;
|
---|
| 229 | code = oldcode;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | /* Generate output characters in reverse order */
|
---|
| 233 | while ((long int) code >= (long int) 256) {
|
---|
| 234 | *--stackp = tab_suffixof(code);
|
---|
| 235 | code = tab_prefixof(code);
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | *--stackp = (unsigned char) (finchar = tab_suffixof(code));
|
---|
| 239 |
|
---|
| 240 | /* And put them out in forward order */
|
---|
| 241 | {
|
---|
| 242 | int i;
|
---|
| 243 |
|
---|
| 244 | if (outpos + (i = (de_stack - stackp)) >= OBUFSIZ) {
|
---|
| 245 | do {
|
---|
| 246 | if (i > OBUFSIZ - outpos) {
|
---|
| 247 | i = OBUFSIZ - outpos;
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | if (i > 0) {
|
---|
| 251 | memcpy(outbuf + outpos, stackp, i);
|
---|
| 252 | outpos += i;
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | if (outpos >= OBUFSIZ) {
|
---|
| 256 | write(fd_out, outbuf, outpos);
|
---|
| 257 | outpos = 0;
|
---|
| 258 | }
|
---|
| 259 | stackp += i;
|
---|
| 260 | } while ((i = (de_stack - stackp)) > 0);
|
---|
| 261 | } else {
|
---|
| 262 | memcpy(outbuf + outpos, stackp, i);
|
---|
| 263 | outpos += i;
|
---|
| 264 | }
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 | /* Generate the new entry. */
|
---|
| 268 | if ((code = free_ent) < maxmaxcode) {
|
---|
| 269 | tab_prefixof(code) = (unsigned short) oldcode;
|
---|
| 270 | tab_suffixof(code) = (unsigned char) finchar;
|
---|
| 271 | free_ent = code + 1;
|
---|
| 272 | }
|
---|
| 273 |
|
---|
| 274 | /* Remember previous code. */
|
---|
| 275 | oldcode = incode;
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | } while (rsize > 0);
|
---|
| 279 |
|
---|
| 280 | if (outpos > 0) {
|
---|
| 281 | write(fd_out, outbuf, outpos);
|
---|
| 282 | }
|
---|
| 283 |
|
---|
[902] | 284 | RELEASE_CONFIG_BUFFER(inbuf);
|
---|
| 285 | RELEASE_CONFIG_BUFFER(outbuf);
|
---|
[821] | 286 | return 0;
|
---|
| 287 | }
|
---|