1 /* vi: set sw=4 ts=4: */
4 /* uncompress for busybox -- (c) 2002 Robert Griebl
6 * based on the original compress42.c source
7 * (see disclaimer below)
10 /* (N)compress42.c - File compression ala IEEE Computer, Mar 1992.
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
23 * marc@suse.de : a small security fix for a buffer overflow
25 * [... History snipped ...]
29 /* Default input buffer size */
32 /* Default output buffer size */
35 /* Defines for third byte of header */
36 #define BIT_MASK 0x1f /* Mask for 'number of compresssion bits' */
37 /* Masks 0x20 and 0x40 are free. */
38 /* I think 0x20 should mean that there is */
39 /* a fourth header byte (for expansion). */
40 #define BLOCK_MODE 0x80 /* Block compression if table is full and */
41 /* compression rate is dropping flush tables */
42 /* the next two codes should not be changed lightly, as they must not */
43 /* lie within the contiguous general code space. */
44 #define FIRST 257 /* first free entry */
45 #define CLEAR 256 /* table clear output code */
47 #define INIT_BITS 9 /* initial number of bits/code */
50 /* machine variants which require cc -Dmachine: pdp11, z8000, DOS */
51 #define HBITS 17 /* 50% occupancy */
52 #define HSIZE (1<<HBITS)
53 #define HMASK (HSIZE-1) /* unused */
54 #define HPRIME 9941 /* unused */
57 #undef MAXSEG_64K /* unused */
58 #define MAXCODE(n) (1L << (n))
60 #define htabof(i) htab[i]
61 #define codetabof(i) codetab[i]
62 #define tab_prefixof(i) codetabof(i)
63 #define tab_suffixof(i) ((unsigned char *)(htab))[i]
64 #define de_stack ((unsigned char *)&(htab[HSIZE-1]))
65 #define clear_tab_prefixof() memset(codetab, 0, 256)
68 * Decompress stdin to stdout. This routine adapts to the codes in the
69 * file building the "string" table on-the-fly; requiring no table to
70 * be stored in the compressed file.
73 USE_DESKTOP(long long) int FAST_FUNC
74 uncompress(int fd_in, int fd_out)
76 USE_DESKTOP(long long total_written = 0;)
77 USE_DESKTOP(long long) int retval = -1;
78 unsigned char *stackp;
93 unsigned char *inbuf; /* were eating insane amounts of stack - */
94 unsigned char *outbuf; /* bad for some embedded targets */
96 unsigned short *codetab;
98 /* Hmm, these were statics - why?! */
99 /* user settable max # bits/code */
100 int maxbits; /* = BITS; */
101 /* block compress mode -C compatible with 2.0 */
102 int block_mode; /* = BLOCK_MODE; */
104 inbuf = xzalloc(IBUFSIZ + 64);
105 outbuf = xzalloc(OBUFSIZ + 2048);
106 htab = xzalloc(HSIZE); /* wsn't zeroed out before, maybe can xmalloc? */
107 codetab = xzalloc(HSIZE * sizeof(codetab[0]));
111 /* xread isn't good here, we have to return - caller may want
112 * to do some cleanup (e.g. delete incomplete unpacked file etc) */
113 if (full_read(fd_in, inbuf, 1) != 1) {
114 bb_error_msg("short read");
118 maxbits = inbuf[0] & BIT_MASK;
119 block_mode = inbuf[0] & BLOCK_MODE;
120 maxmaxcode = MAXCODE(maxbits);
122 if (maxbits > BITS) {
123 bb_error_msg("compressed with %d bits, can only handle "
124 BITS_STR" bits", maxbits);
129 maxcode = MAXCODE(INIT_BITS) - 1;
130 bitmask = (1 << INIT_BITS) - 1;
136 free_ent = ((block_mode) ? FIRST : 256);
138 /* As above, initialize the first 256 entries in the table. */
139 /*clear_tab_prefixof(); - done by xzalloc */
141 for (code = 255; code >= 0; --code) {
142 tab_suffixof(code) = (unsigned char) code;
155 for (i = 0; i < e; ++i)
156 inbuf[i] = inbuf[i + o];
162 if (insize < (int) (IBUFSIZ + 64) - IBUFSIZ) {
163 rsize = safe_read(fd_in, inbuf + insize, IBUFSIZ);
168 inbits = ((rsize > 0) ? (insize - insize % n_bits) << 3 :
169 (insize << 3) - (n_bits - 1));
171 while (inbits > posbits) {
172 if (free_ent > maxcode) {
176 (posbits - 1 + (n_bits << 3)) % (n_bits << 3)));
178 if (n_bits == maxbits) {
179 maxcode = maxmaxcode;
181 maxcode = MAXCODE(n_bits) - 1;
183 bitmask = (1 << n_bits) - 1;
187 unsigned char *p = &inbuf[posbits >> 3];
189 code = ((((long) (p[0])) | ((long) (p[1]) << 8) |
190 ((long) (p[2]) << 16)) >> (posbits & 0x7)) & bitmask;
197 finchar = (int) oldcode;
198 outbuf[outpos++] = (unsigned char) finchar;
202 if (code == CLEAR && block_mode) {
203 clear_tab_prefixof();
204 free_ent = FIRST - 1;
208 (posbits - 1 + (n_bits << 3)) % (n_bits << 3)));
210 maxcode = MAXCODE(INIT_BITS) - 1;
211 bitmask = (1 << INIT_BITS) - 1;
218 /* Special case for KwKwK string. */
219 if (code >= free_ent) {
220 if (code > free_ent) {
224 p = &inbuf[posbits >> 3];
227 ("insize:%d posbits:%d inbuf:%02X %02X %02X %02X %02X (%d)",
228 insize, posbits, p[-1], p[0], p[1], p[2], p[3],
230 bb_error_msg("uncompress: corrupt input");
234 *--stackp = (unsigned char) finchar;
238 /* Generate output characters in reverse order */
239 while ((long) code >= (long) 256) {
240 *--stackp = tab_suffixof(code);
241 code = tab_prefixof(code);
244 finchar = tab_suffixof(code);
245 *--stackp = (unsigned char) finchar;
247 /* And put them out in forward order */
251 i = de_stack - stackp;
252 if (outpos + i >= OBUFSIZ) {
254 if (i > OBUFSIZ - outpos) {
255 i = OBUFSIZ - outpos;
259 memcpy(outbuf + outpos, stackp, i);
263 if (outpos >= OBUFSIZ) {
264 full_write(fd_out, outbuf, outpos);
266 USE_DESKTOP(total_written += outpos;)
270 i = de_stack - stackp;
273 memcpy(outbuf + outpos, stackp, i);
278 /* Generate the new entry. */
280 if (code < maxmaxcode) {
281 tab_prefixof(code) = (unsigned short) oldcode;
282 tab_suffixof(code) = (unsigned char) finchar;
286 /* Remember previous code. */
293 full_write(fd_out, outbuf, outpos);
295 USE_DESKTOP(total_written += outpos;)
298 retval = USE_DESKTOP(total_written) + 0;