blob: 53c27080f21cd7337523066e7a315a0686ebc7b4 [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/* uncompress for busybox -- (c) 2002 Robert Griebl
3 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00004 * based on the original compress42.c source
Robert Griebl94a6a952002-05-19 19:00:14 +00005 * (see disclaimer below)
6 */
7
Robert Griebl94a6a952002-05-19 19:00:14 +00008/* (N)compress42.c - File compression ala IEEE Computer, Mar 1992.
9 *
10 * Authors:
11 * Spencer W. Thomas (decvax!harpo!utah-cs!utah-gr!thomas)
12 * Jim McKie (decvax!mcvax!jim)
13 * Steve Davies (decvax!vax135!petsd!peora!srd)
14 * Ken Turkowski (decvax!decwrl!turtlevax!ken)
15 * James A. Woods (decvax!ihnp4!ames!jaw)
16 * Joe Orost (decvax!vax135!petsd!joe)
17 * Dave Mack (csu@alembic.acs.com)
18 * Peter Jannesen, Network Communication Systems
19 * (peter@ncs.nl)
20 *
21 * marc@suse.de : a small security fix for a buffer overflow
22 *
23 * [... History snipped ...]
24 *
25 */
Robert Griebl94a6a952002-05-19 19:00:14 +000026
Denis Vlasenkoe9ad84d2008-08-05 13:10:34 +000027#include "libbb.h"
Denys Vlasenkod184a722011-09-22 12:45:14 +020028#include "bb_archive.h"
Denis Vlasenkoe9ad84d2008-08-05 13:10:34 +000029
30
Eric Andersenaff114c2004-04-14 17:51:38 +000031/* Default input buffer size */
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020032#define IBUFSIZ 2048
Robert Griebl94a6a952002-05-19 19:00:14 +000033
Glenn L McGrathfedbfe42002-11-28 09:09:47 +000034/* Default output buffer size */
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020035#define OBUFSIZ 2048
Robert Griebl94a6a952002-05-19 19:00:14 +000036
Glenn L McGrathfedbfe42002-11-28 09:09:47 +000037/* Defines for third byte of header */
Denis Vlasenkoe8a07882007-06-10 15:08:44 +000038#define BIT_MASK 0x1f /* Mask for 'number of compresssion bits' */
39 /* Masks 0x20 and 0x40 are free. */
40 /* I think 0x20 should mean that there is */
41 /* a fourth header byte (for expansion). */
42#define BLOCK_MODE 0x80 /* Block compression if table is full and */
43 /* compression rate is dropping flush tables */
44 /* the next two codes should not be changed lightly, as they must not */
45 /* lie within the contiguous general code space. */
46#define FIRST 257 /* first free entry */
47#define CLEAR 256 /* table clear output code */
Robert Griebl94a6a952002-05-19 19:00:14 +000048
Denis Vlasenkoe8a07882007-06-10 15:08:44 +000049#define INIT_BITS 9 /* initial number of bits/code */
Robert Griebl94a6a952002-05-19 19:00:14 +000050
51
Glenn L McGrathfedbfe42002-11-28 09:09:47 +000052/* machine variants which require cc -Dmachine: pdp11, z8000, DOS */
Denis Vlasenkoe8a07882007-06-10 15:08:44 +000053#define HBITS 17 /* 50% occupancy */
54#define HSIZE (1<<HBITS)
55#define HMASK (HSIZE-1) /* unused */
56#define HPRIME 9941 /* unused */
57#define BITS 16
58#define BITS_STR "16"
59#undef MAXSEG_64K /* unused */
60#define MAXCODE(n) (1L << (n))
Robert Griebl94a6a952002-05-19 19:00:14 +000061
Denis Vlasenkoe8a07882007-06-10 15:08:44 +000062#define htabof(i) htab[i]
63#define codetabof(i) codetab[i]
64#define tab_prefixof(i) codetabof(i)
65#define tab_suffixof(i) ((unsigned char *)(htab))[i]
66#define de_stack ((unsigned char *)&(htab[HSIZE-1]))
67#define clear_tab_prefixof() memset(codetab, 0, 256)
Robert Griebl94a6a952002-05-19 19:00:14 +000068
Robert Griebl94a6a952002-05-19 19:00:14 +000069/*
70 * Decompress stdin to stdout. This routine adapts to the codes in the
71 * file building the "string" table on-the-fly; requiring no table to
Denis Vlasenkoe8a07882007-06-10 15:08:44 +000072 * be stored in the compressed file.
Robert Griebl94a6a952002-05-19 19:00:14 +000073 */
74
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000075IF_DESKTOP(long long) int FAST_FUNC
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +010076unpack_Z_stream(transformer_aux_data_t *aux, int src_fd, int dst_fd)
Robert Griebl94a6a952002-05-19 19:00:14 +000077{
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000078 IF_DESKTOP(long long total_written = 0;)
79 IF_DESKTOP(long long) int retval = -1;
Glenn L McGrathfedbfe42002-11-28 09:09:47 +000080 unsigned char *stackp;
Glenn L McGrathfedbfe42002-11-28 09:09:47 +000081 int finchar;
Denis Vlasenko87468852007-04-13 23:22:00 +000082 long oldcode;
83 long incode;
Glenn L McGrathfedbfe42002-11-28 09:09:47 +000084 int inbits;
85 int posbits;
86 int outpos;
87 int insize;
88 int bitmask;
Denis Vlasenko87468852007-04-13 23:22:00 +000089 long free_ent;
90 long maxcode;
91 long maxmaxcode;
Glenn L McGrathfedbfe42002-11-28 09:09:47 +000092 int n_bits;
93 int rsize = 0;
Denis Vlasenkoe8a07882007-06-10 15:08:44 +000094 unsigned char *inbuf; /* were eating insane amounts of stack - */
95 unsigned char *outbuf; /* bad for some embedded targets */
96 unsigned char *htab;
97 unsigned short *codetab;
Denis Vlasenko447b5432007-01-05 19:49:02 +000098
99 /* Hmm, these were statics - why?! */
100 /* user settable max # bits/code */
101 int maxbits; /* = BITS; */
102 /* block compress mode -C compatible with 2.0 */
103 int block_mode; /* = BLOCK_MODE; */
104
Denys Vlasenko8a6a2f92012-03-06 16:27:48 +0100105 if (check_signature16(aux, src_fd, COMPRESS_MAGIC))
106 return -1;
107
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000108 inbuf = xzalloc(IBUFSIZ + 64);
109 outbuf = xzalloc(OBUFSIZ + 2048);
Denys Vlasenko59655072012-03-06 16:23:50 +0100110 htab = xzalloc(HSIZE); /* wasn't zeroed out before, maybe can xmalloc? */
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000111 codetab = xzalloc(HSIZE * sizeof(codetab[0]));
Robert Griebl94a6a952002-05-19 19:00:14 +0000112
113 insize = 0;
114
Denis Vlasenko447b5432007-01-05 19:49:02 +0000115 /* xread isn't good here, we have to return - caller may want
116 * to do some cleanup (e.g. delete incomplete unpacked file etc) */
Denys Vlasenko59655072012-03-06 16:23:50 +0100117 if (full_read(src_fd, inbuf, 1) != 1) {
Denis Vlasenko447b5432007-01-05 19:49:02 +0000118 bb_error_msg("short read");
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000119 goto err;
Denis Vlasenko447b5432007-01-05 19:49:02 +0000120 }
Robert Griebl94a6a952002-05-19 19:00:14 +0000121
122 maxbits = inbuf[0] & BIT_MASK;
123 block_mode = inbuf[0] & BLOCK_MODE;
124 maxmaxcode = MAXCODE(maxbits);
125
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000126 if (maxbits > BITS) {
Denis Vlasenko447b5432007-01-05 19:49:02 +0000127 bb_error_msg("compressed with %d bits, can only handle "
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000128 BITS_STR" bits", maxbits);
129 goto err;
Robert Griebl94a6a952002-05-19 19:00:14 +0000130 }
131
Denis Vlasenko447b5432007-01-05 19:49:02 +0000132 n_bits = INIT_BITS;
133 maxcode = MAXCODE(INIT_BITS) - 1;
134 bitmask = (1 << INIT_BITS) - 1;
Robert Griebl94a6a952002-05-19 19:00:14 +0000135 oldcode = -1;
136 finchar = 0;
137 outpos = 0;
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000138 posbits = 0 << 3;
Robert Griebl94a6a952002-05-19 19:00:14 +0000139
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000140 free_ent = ((block_mode) ? FIRST : 256);
Robert Griebl94a6a952002-05-19 19:00:14 +0000141
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000142 /* As above, initialize the first 256 entries in the table. */
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000143 /*clear_tab_prefixof(); - done by xzalloc */
Robert Griebl94a6a952002-05-19 19:00:14 +0000144
Denys Vlasenkoc71547c2012-12-05 01:04:54 +0100145 {
146 int i;
147 for (i = 255; i >= 0; --i)
148 tab_suffixof(i) = (unsigned char) i;
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000149 }
Robert Griebl94a6a952002-05-19 19:00:14 +0000150
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000151 do {
Denis Vlasenko447b5432007-01-05 19:49:02 +0000152 resetbuf:
Robert Griebl94a6a952002-05-19 19:00:14 +0000153 {
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000154 int i;
155 int e;
156 int o;
Robert Griebl94a6a952002-05-19 19:00:14 +0000157
Denis Vlasenko447b5432007-01-05 19:49:02 +0000158 o = posbits >> 3;
159 e = insize - o;
Robert Griebl94a6a952002-05-19 19:00:14 +0000160
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000161 for (i = 0; i < e; ++i)
162 inbuf[i] = inbuf[i + o];
Robert Griebl94a6a952002-05-19 19:00:14 +0000163
164 insize = e;
165 posbits = 0;
166 }
167
Bernhard Reutner-Fischer5d725462006-09-06 15:28:32 +0000168 if (insize < (int) (IBUFSIZ + 64) - IBUFSIZ) {
Denys Vlasenko59655072012-03-06 16:23:50 +0100169 rsize = safe_read(src_fd, inbuf + insize, IBUFSIZ);
Denys Vlasenko251fc702011-08-18 14:29:41 +0200170 if (rsize < 0)
Denys Vlasenkoa04e4c22011-08-22 04:59:41 +0200171 bb_error_msg_and_die(bb_msg_read_error);
Robert Griebl94a6a952002-05-19 19:00:14 +0000172 insize += rsize;
173 }
174
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000175 inbits = ((rsize > 0) ? (insize - insize % n_bits) << 3 :
176 (insize << 3) - (n_bits - 1));
Robert Griebl94a6a952002-05-19 19:00:14 +0000177
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000178 while (inbits > posbits) {
Denys Vlasenkoc71547c2012-12-05 01:04:54 +0100179 long code;
180
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000181 if (free_ent > maxcode) {
182 posbits =
183 ((posbits - 1) +
184 ((n_bits << 3) -
185 (posbits - 1 + (n_bits << 3)) % (n_bits << 3)));
Robert Griebl94a6a952002-05-19 19:00:14 +0000186 ++n_bits;
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000187 if (n_bits == maxbits) {
Robert Griebl94a6a952002-05-19 19:00:14 +0000188 maxcode = maxmaxcode;
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000189 } else {
190 maxcode = MAXCODE(n_bits) - 1;
191 }
192 bitmask = (1 << n_bits) - 1;
Robert Griebl94a6a952002-05-19 19:00:14 +0000193 goto resetbuf;
194 }
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000195 {
196 unsigned char *p = &inbuf[posbits >> 3];
Denys Vlasenkoc71547c2012-12-05 01:04:54 +0100197 code = ((p[0]
198 | ((long) (p[1]) << 8)
199 | ((long) (p[2]) << 16)) >> (posbits & 0x7)) & bitmask;
Robert Griebl94a6a952002-05-19 19:00:14 +0000200 }
201 posbits += n_bits;
Robert Griebl94a6a952002-05-19 19:00:14 +0000202
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000203 if (oldcode == -1) {
Denys Vlasenko251fc702011-08-18 14:29:41 +0200204 if (code >= 256)
205 bb_error_msg_and_die("corrupted data"); /* %ld", code); */
Denis Vlasenko97a8dd32006-10-01 15:55:11 +0000206 oldcode = code;
207 finchar = (int) oldcode;
208 outbuf[outpos++] = (unsigned char) finchar;
Robert Griebl94a6a952002-05-19 19:00:14 +0000209 continue;
210 }
211
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000212 if (code == CLEAR && block_mode) {
Robert Griebl94a6a952002-05-19 19:00:14 +0000213 clear_tab_prefixof();
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000214 free_ent = FIRST - 1;
215 posbits =
216 ((posbits - 1) +
217 ((n_bits << 3) -
218 (posbits - 1 + (n_bits << 3)) % (n_bits << 3)));
Denis Vlasenko447b5432007-01-05 19:49:02 +0000219 n_bits = INIT_BITS;
220 maxcode = MAXCODE(INIT_BITS) - 1;
221 bitmask = (1 << INIT_BITS) - 1;
Robert Griebl94a6a952002-05-19 19:00:14 +0000222 goto resetbuf;
223 }
224
225 incode = code;
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000226 stackp = de_stack;
Robert Griebl94a6a952002-05-19 19:00:14 +0000227
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000228 /* Special case for KwKwK string. */
229 if (code >= free_ent) {
230 if (code > free_ent) {
Denys Vlasenko4d5955e2012-12-05 11:08:30 +0100231/*
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000232 unsigned char *p;
Robert Griebl94a6a952002-05-19 19:00:14 +0000233
234 posbits -= n_bits;
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000235 p = &inbuf[posbits >> 3];
Manuel Novoa III cad53642003-03-19 09:13:01 +0000236 bb_error_msg
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000237 ("insize:%d posbits:%d inbuf:%02X %02X %02X %02X %02X (%d)",
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100238 insize, posbits, p[-1], p[0], p[1], p[2], p[3],
239 (posbits & 07));
Denys Vlasenkoa8461172012-12-05 01:06:05 +0100240*/
Denys Vlasenkoccb88a62010-05-27 02:22:54 +0200241 bb_error_msg("corrupted data");
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000242 goto err;
Robert Griebl94a6a952002-05-19 19:00:14 +0000243 }
244
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000245 *--stackp = (unsigned char) finchar;
246 code = oldcode;
Robert Griebl94a6a952002-05-19 19:00:14 +0000247 }
248
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000249 /* Generate output characters in reverse order */
Denys Vlasenkoc71547c2012-12-05 01:04:54 +0100250 while (code >= 256) {
Denys Vlasenko251fc702011-08-18 14:29:41 +0200251 if (stackp <= &htabof(0))
252 bb_error_msg_and_die("corrupted data");
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000253 *--stackp = tab_suffixof(code);
254 code = tab_prefixof(code);
Robert Griebl94a6a952002-05-19 19:00:14 +0000255 }
256
Denis Vlasenko447b5432007-01-05 19:49:02 +0000257 finchar = tab_suffixof(code);
258 *--stackp = (unsigned char) finchar;
Robert Griebl94a6a952002-05-19 19:00:14 +0000259
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000260 /* And put them out in forward order */
Robert Griebl94a6a952002-05-19 19:00:14 +0000261 {
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000262 int i;
Robert Griebl94a6a952002-05-19 19:00:14 +0000263
Denis Vlasenko447b5432007-01-05 19:49:02 +0000264 i = de_stack - stackp;
265 if (outpos + i >= OBUFSIZ) {
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000266 do {
267 if (i > OBUFSIZ - outpos) {
268 i = OBUFSIZ - outpos;
269 }
Robert Griebl94a6a952002-05-19 19:00:14 +0000270
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000271 if (i > 0) {
272 memcpy(outbuf + outpos, stackp, i);
Robert Griebl94a6a952002-05-19 19:00:14 +0000273 outpos += i;
274 }
275
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000276 if (outpos >= OBUFSIZ) {
Denys Vlasenko59655072012-03-06 16:23:50 +0100277 xwrite(dst_fd, outbuf, outpos);
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000278 IF_DESKTOP(total_written += outpos;)
Robert Griebl94a6a952002-05-19 19:00:14 +0000279 outpos = 0;
280 }
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000281 stackp += i;
Denis Vlasenko447b5432007-01-05 19:49:02 +0000282 i = de_stack - stackp;
283 } while (i > 0);
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000284 } else {
285 memcpy(outbuf + outpos, stackp, i);
Robert Griebl94a6a952002-05-19 19:00:14 +0000286 outpos += i;
287 }
288 }
289
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000290 /* Generate the new entry. */
Denys Vlasenkoc71547c2012-12-05 01:04:54 +0100291 if (free_ent < maxmaxcode) {
292 tab_prefixof(free_ent) = (unsigned short) oldcode;
293 tab_suffixof(free_ent) = (unsigned char) finchar;
294 free_ent++;
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000295 }
Robert Griebl94a6a952002-05-19 19:00:14 +0000296
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000297 /* Remember previous code. */
298 oldcode = incode;
Robert Griebl94a6a952002-05-19 19:00:14 +0000299 }
300
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000301 } while (rsize > 0);
Robert Griebl94a6a952002-05-19 19:00:14 +0000302
Glenn L McGrath2e41d0c2002-09-27 06:46:02 +0000303 if (outpos > 0) {
Denys Vlasenko59655072012-03-06 16:23:50 +0100304 xwrite(dst_fd, outbuf, outpos);
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000305 IF_DESKTOP(total_written += outpos;)
Glenn L McGrath2e41d0c2002-09-27 06:46:02 +0000306 }
Robert Griebl94a6a952002-05-19 19:00:14 +0000307
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000308 retval = IF_DESKTOP(total_written) + 0;
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000309 err:
310 free(inbuf);
311 free(outbuf);
312 free(htab);
313 free(codetab);
314 return retval;
Robert Griebl94a6a952002-05-19 19:00:14 +0000315}