blob: e2941438ca09ca2fd1ee5fa019c6561520f103ea [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Robert Griebl94a6a952002-05-19 19:00:14 +00002#include "libbb.h"
3
Robert Griebl94a6a952002-05-19 19:00:14 +00004/* uncompress for busybox -- (c) 2002 Robert Griebl
5 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00006 * based on the original compress42.c source
Robert Griebl94a6a952002-05-19 19:00:14 +00007 * (see disclaimer below)
8 */
9
10
11/* (N)compress42.c - File compression ala IEEE Computer, Mar 1992.
12 *
13 * Authors:
14 * Spencer W. Thomas (decvax!harpo!utah-cs!utah-gr!thomas)
15 * Jim McKie (decvax!mcvax!jim)
16 * Steve Davies (decvax!vax135!petsd!peora!srd)
17 * Ken Turkowski (decvax!decwrl!turtlevax!ken)
18 * James A. Woods (decvax!ihnp4!ames!jaw)
19 * Joe Orost (decvax!vax135!petsd!joe)
20 * Dave Mack (csu@alembic.acs.com)
21 * Peter Jannesen, Network Communication Systems
22 * (peter@ncs.nl)
23 *
24 * marc@suse.de : a small security fix for a buffer overflow
25 *
26 * [... History snipped ...]
27 *
28 */
Glenn L McGrath2e41d0c2002-09-27 06:46:02 +000029#include <stdio.h>
30#include <string.h>
31#include <unistd.h>
Robert Griebl94a6a952002-05-19 19:00:14 +000032
Eric Andersenaff114c2004-04-14 17:51:38 +000033/* Default input buffer size */
Glenn L McGrathfedbfe42002-11-28 09:09:47 +000034#define IBUFSIZ 2048
Robert Griebl94a6a952002-05-19 19:00:14 +000035
Glenn L McGrathfedbfe42002-11-28 09:09:47 +000036/* Default output buffer size */
37#define OBUFSIZ 2048
Robert Griebl94a6a952002-05-19 19:00:14 +000038
Glenn L McGrathfedbfe42002-11-28 09:09:47 +000039/* Defines for third byte of header */
40#define MAGIC_1 (char_type)'\037' /* First byte of compressed file */
41#define MAGIC_2 (char_type)'\235' /* Second byte of compressed file */
42#define BIT_MASK 0x1f /* Mask for 'number of compresssion bits' */
43 /* Masks 0x20 and 0x40 are free. */
44 /* I think 0x20 should mean that there is */
45 /* a fourth header byte (for expansion). */
46#define BLOCK_MODE 0x80 /* Block compresssion if table is full and */
47 /* compression rate is dropping flush tables */
48 /* the next two codes should not be changed lightly, as they must not */
49 /* lie within the contiguous general code space. */
50#define FIRST 257 /* first free entry */
51#define CLEAR 256 /* table clear output code */
Robert Griebl94a6a952002-05-19 19:00:14 +000052
Glenn L McGrathfedbfe42002-11-28 09:09:47 +000053#define INIT_BITS 9 /* initial number of bits/code */
Robert Griebl94a6a952002-05-19 19:00:14 +000054
55
Glenn L McGrathfedbfe42002-11-28 09:09:47 +000056/* machine variants which require cc -Dmachine: pdp11, z8000, DOS */
Robert Griebl94a6a952002-05-19 19:00:14 +000057#define FAST
58
Glenn L McGrathfedbfe42002-11-28 09:09:47 +000059#define HBITS 17 /* 50% occupancy */
Robert Griebl94a6a952002-05-19 19:00:14 +000060#define HSIZE (1<<HBITS)
61#define HMASK (HSIZE-1)
62#define HPRIME 9941
63#define BITS 16
64#undef MAXSEG_64K
Robert Griebl94a6a952002-05-19 19:00:14 +000065#define MAXCODE(n) (1L << (n))
66
Glenn L McGrathfedbfe42002-11-28 09:09:47 +000067/* Block compress mode -C compatible with 2.0 */
Eric Andersen14f5c8d2005-04-16 19:39:00 +000068static int block_mode = BLOCK_MODE;
Glenn L McGrathfedbfe42002-11-28 09:09:47 +000069
70/* user settable max # bits/code */
Eric Andersen14f5c8d2005-04-16 19:39:00 +000071static int maxbits = BITS;
Glenn L McGrathfedbfe42002-11-28 09:09:47 +000072
Robert Griebl94a6a952002-05-19 19:00:14 +000073#define htabof(i) htab[i]
74#define codetabof(i) codetab[i]
75#define tab_prefixof(i) codetabof(i)
Glenn L McGrathfedbfe42002-11-28 09:09:47 +000076#define tab_suffixof(i) ((unsigned char *)(htab))[i]
77#define de_stack ((unsigned char *)&(htab[HSIZE-1]))
Bernhard Reutner-Fischer5d725462006-09-06 15:28:32 +000078#define clear_htab() memset(htab, -1, HSIZE)
Robert Griebl94a6a952002-05-19 19:00:14 +000079#define clear_tab_prefixof() memset(codetab, 0, 256);
80
81
Robert Griebl94a6a952002-05-19 19:00:14 +000082/*
83 * Decompress stdin to stdout. This routine adapts to the codes in the
84 * file building the "string" table on-the-fly; requiring no table to
85 * be stored in the compressed file. The tables used herein are shared
86 * with those of the compress() routine. See the definitions above.
87 */
88
Rob Landleydfba7412006-03-06 20:47:33 +000089int uncompress(int fd_in, int fd_out)
Robert Griebl94a6a952002-05-19 19:00:14 +000090{
Glenn L McGrathfedbfe42002-11-28 09:09:47 +000091 unsigned char *stackp;
92 long int code;
93 int finchar;
94 long int oldcode;
95 long int incode;
96 int inbits;
97 int posbits;
98 int outpos;
99 int insize;
100 int bitmask;
101 long int free_ent;
102 long int maxcode;
103 long int maxmaxcode;
104 int n_bits;
105 int rsize = 0;
Bernhard Reutner-Fischer5d725462006-09-06 15:28:32 +0000106 RESERVE_CONFIG_UBUFFER(inbuf, IBUFSIZ + 64);
107 RESERVE_CONFIG_UBUFFER(outbuf, OBUFSIZ + 2048);
108 unsigned char htab[HSIZE];
109 unsigned short codetab[HSIZE];
110 memset(inbuf, 0, IBUFSIZ + 64);
111 memset(outbuf, 0, OBUFSIZ + 2048);
Robert Griebl94a6a952002-05-19 19:00:14 +0000112
113 insize = 0;
114
Rob Landley53437472006-07-16 08:14:35 +0000115 inbuf[0] = xread_char(fd_in);
Robert Griebl94a6a952002-05-19 19:00:14 +0000116
117 maxbits = inbuf[0] & BIT_MASK;
118 block_mode = inbuf[0] & BLOCK_MODE;
119 maxmaxcode = MAXCODE(maxbits);
120
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000121 if (maxbits > BITS) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000122 bb_error_msg("compressed with %d bits, can only handle %d bits", maxbits,
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000123 BITS);
Robert Griebl94a6a952002-05-19 19:00:14 +0000124 return -1;
125 }
126
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000127 maxcode = MAXCODE(n_bits = INIT_BITS) - 1;
128 bitmask = (1 << n_bits) - 1;
Robert Griebl94a6a952002-05-19 19:00:14 +0000129 oldcode = -1;
130 finchar = 0;
131 outpos = 0;
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000132 posbits = 0 << 3;
Robert Griebl94a6a952002-05-19 19:00:14 +0000133
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000134 free_ent = ((block_mode) ? FIRST : 256);
Robert Griebl94a6a952002-05-19 19:00:14 +0000135
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000136 /* As above, initialize the first 256 entries in the table. */
137 clear_tab_prefixof();
Robert Griebl94a6a952002-05-19 19:00:14 +0000138
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000139 for (code = 255; code >= 0; --code) {
140 tab_suffixof(code) = (unsigned char) code;
141 }
Robert Griebl94a6a952002-05-19 19:00:14 +0000142
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000143 do {
144 resetbuf:;
Robert Griebl94a6a952002-05-19 19:00:14 +0000145 {
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000146 int i;
147 int e;
148 int o;
Robert Griebl94a6a952002-05-19 19:00:14 +0000149
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000150 e = insize - (o = (posbits >> 3));
Robert Griebl94a6a952002-05-19 19:00:14 +0000151
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000152 for (i = 0; i < e; ++i)
153 inbuf[i] = inbuf[i + o];
Robert Griebl94a6a952002-05-19 19:00:14 +0000154
155 insize = e;
156 posbits = 0;
157 }
158
Bernhard Reutner-Fischer5d725462006-09-06 15:28:32 +0000159 if (insize < (int) (IBUFSIZ + 64) - IBUFSIZ) {
Glenn L McGrath1a2d75f2003-11-21 22:17:28 +0000160 rsize = safe_read(fd_in, inbuf + insize, IBUFSIZ);
Robert Griebl94a6a952002-05-19 19:00:14 +0000161 insize += rsize;
162 }
163
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000164 inbits = ((rsize > 0) ? (insize - insize % n_bits) << 3 :
165 (insize << 3) - (n_bits - 1));
Robert Griebl94a6a952002-05-19 19:00:14 +0000166
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000167 while (inbits > posbits) {
168 if (free_ent > maxcode) {
169 posbits =
170 ((posbits - 1) +
171 ((n_bits << 3) -
172 (posbits - 1 + (n_bits << 3)) % (n_bits << 3)));
Robert Griebl94a6a952002-05-19 19:00:14 +0000173 ++n_bits;
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000174 if (n_bits == maxbits) {
Robert Griebl94a6a952002-05-19 19:00:14 +0000175 maxcode = maxmaxcode;
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000176 } else {
177 maxcode = MAXCODE(n_bits) - 1;
178 }
179 bitmask = (1 << n_bits) - 1;
Robert Griebl94a6a952002-05-19 19:00:14 +0000180 goto resetbuf;
181 }
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000182 {
183 unsigned char *p = &inbuf[posbits >> 3];
Robert Griebl94a6a952002-05-19 19:00:14 +0000184
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000185 code =
186 ((((long) (p[0])) | ((long) (p[1]) << 8) |
187 ((long) (p[2]) << 16)) >> (posbits & 0x7)) & bitmask;
Robert Griebl94a6a952002-05-19 19:00:14 +0000188 }
189 posbits += n_bits;
Robert Griebl94a6a952002-05-19 19:00:14 +0000190
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000191
192 if (oldcode == -1) {
193 outbuf[outpos++] = (unsigned char) (finchar =
194 (int) (oldcode = code));
Robert Griebl94a6a952002-05-19 19:00:14 +0000195 continue;
196 }
197
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000198 if (code == CLEAR && block_mode) {
Robert Griebl94a6a952002-05-19 19:00:14 +0000199 clear_tab_prefixof();
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000200 free_ent = FIRST - 1;
201 posbits =
202 ((posbits - 1) +
203 ((n_bits << 3) -
204 (posbits - 1 + (n_bits << 3)) % (n_bits << 3)));
205 maxcode = MAXCODE(n_bits = INIT_BITS) - 1;
206 bitmask = (1 << n_bits) - 1;
Robert Griebl94a6a952002-05-19 19:00:14 +0000207 goto resetbuf;
208 }
209
210 incode = code;
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000211 stackp = de_stack;
Robert Griebl94a6a952002-05-19 19:00:14 +0000212
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000213 /* Special case for KwKwK string. */
214 if (code >= free_ent) {
215 if (code > free_ent) {
216 unsigned char *p;
Robert Griebl94a6a952002-05-19 19:00:14 +0000217
218 posbits -= n_bits;
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000219 p = &inbuf[posbits >> 3];
Robert Griebl94a6a952002-05-19 19:00:14 +0000220
Manuel Novoa III cad53642003-03-19 09:13:01 +0000221 bb_error_msg
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000222 ("insize:%d posbits:%d inbuf:%02X %02X %02X %02X %02X (%d)",
223 insize, posbits, p[-1], p[0], p[1], p[2], p[3],
224 (posbits & 07));
Manuel Novoa III cad53642003-03-19 09:13:01 +0000225 bb_error_msg("uncompress: corrupt input");
Robert Griebl94a6a952002-05-19 19:00:14 +0000226 return -1;
227 }
228
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000229 *--stackp = (unsigned char) finchar;
230 code = oldcode;
Robert Griebl94a6a952002-05-19 19:00:14 +0000231 }
232
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000233 /* Generate output characters in reverse order */
234 while ((long int) code >= (long int) 256) {
235 *--stackp = tab_suffixof(code);
236 code = tab_prefixof(code);
Robert Griebl94a6a952002-05-19 19:00:14 +0000237 }
238
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000239 *--stackp = (unsigned char) (finchar = tab_suffixof(code));
Robert Griebl94a6a952002-05-19 19:00:14 +0000240
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000241 /* And put them out in forward order */
Robert Griebl94a6a952002-05-19 19:00:14 +0000242 {
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000243 int i;
Robert Griebl94a6a952002-05-19 19:00:14 +0000244
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000245 if (outpos + (i = (de_stack - stackp)) >= OBUFSIZ) {
246 do {
247 if (i > OBUFSIZ - outpos) {
248 i = OBUFSIZ - outpos;
249 }
Robert Griebl94a6a952002-05-19 19:00:14 +0000250
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000251 if (i > 0) {
252 memcpy(outbuf + outpos, stackp, i);
Robert Griebl94a6a952002-05-19 19:00:14 +0000253 outpos += i;
254 }
255
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000256 if (outpos >= OBUFSIZ) {
Glenn L McGrath2e41d0c2002-09-27 06:46:02 +0000257 write(fd_out, outbuf, outpos);
Robert Griebl94a6a952002-05-19 19:00:14 +0000258 outpos = 0;
259 }
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000260 stackp += i;
261 } while ((i = (de_stack - stackp)) > 0);
262 } else {
263 memcpy(outbuf + outpos, stackp, i);
Robert Griebl94a6a952002-05-19 19:00:14 +0000264 outpos += i;
265 }
266 }
267
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000268 /* Generate the new entry. */
269 if ((code = free_ent) < maxmaxcode) {
270 tab_prefixof(code) = (unsigned short) oldcode;
271 tab_suffixof(code) = (unsigned char) finchar;
272 free_ent = code + 1;
273 }
Robert Griebl94a6a952002-05-19 19:00:14 +0000274
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000275 /* Remember previous code. */
276 oldcode = incode;
Robert Griebl94a6a952002-05-19 19:00:14 +0000277 }
278
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000279 } while (rsize > 0);
Robert Griebl94a6a952002-05-19 19:00:14 +0000280
Glenn L McGrath2e41d0c2002-09-27 06:46:02 +0000281 if (outpos > 0) {
282 write(fd_out, outbuf, outpos);
283 }
Robert Griebl94a6a952002-05-19 19:00:14 +0000284
Bernhard Reutner-Fischer5d725462006-09-06 15:28:32 +0000285 RELEASE_CONFIG_BUFFER(inbuf);
286 RELEASE_CONFIG_BUFFER(outbuf);
Robert Griebl94a6a952002-05-19 19:00:14 +0000287 return 0;
288}