blob: 81764a47fb952931324cf38ee049cdb814aaf9c7 [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
Glenn L McGrathfedbfe42002-11-28 09:09:47 +000073/* Input buffer */
Eric Andersen14f5c8d2005-04-16 19:39:00 +000074static unsigned char inbuf[IBUFSIZ + 64];
Glenn L McGrathfedbfe42002-11-28 09:09:47 +000075
76/* Output buffer */
Eric Andersen14f5c8d2005-04-16 19:39:00 +000077static unsigned char outbuf[OBUFSIZ + 2048];
Robert Griebl94a6a952002-05-19 19:00:14 +000078
79
Rob Landley2ad1e7c2006-05-11 15:23:23 +000080static unsigned char htab[HSIZE];
Eric Andersen14f5c8d2005-04-16 19:39:00 +000081static unsigned short codetab[HSIZE];
Robert Griebl94a6a952002-05-19 19:00:14 +000082
83#define htabof(i) htab[i]
84#define codetabof(i) codetab[i]
85#define tab_prefixof(i) codetabof(i)
Glenn L McGrathfedbfe42002-11-28 09:09:47 +000086#define tab_suffixof(i) ((unsigned char *)(htab))[i]
87#define de_stack ((unsigned char *)&(htab[HSIZE-1]))
Robert Griebl94a6a952002-05-19 19:00:14 +000088#define clear_htab() memset(htab, -1, sizeof(htab))
89#define clear_tab_prefixof() memset(codetab, 0, 256);
90
91
Robert Griebl94a6a952002-05-19 19:00:14 +000092/*
93 * Decompress stdin to stdout. This routine adapts to the codes in the
94 * file building the "string" table on-the-fly; requiring no table to
95 * be stored in the compressed file. The tables used herein are shared
96 * with those of the compress() routine. See the definitions above.
97 */
98
Rob Landleydfba7412006-03-06 20:47:33 +000099int uncompress(int fd_in, int fd_out)
Robert Griebl94a6a952002-05-19 19:00:14 +0000100{
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000101 unsigned char *stackp;
102 long int code;
103 int finchar;
104 long int oldcode;
105 long int incode;
106 int inbits;
107 int posbits;
108 int outpos;
109 int insize;
110 int bitmask;
111 long int free_ent;
112 long int maxcode;
113 long int maxmaxcode;
114 int n_bits;
115 int rsize = 0;
Robert Griebl94a6a952002-05-19 19:00:14 +0000116
117 insize = 0;
118
Manuel Novoa III cad53642003-03-19 09:13:01 +0000119 inbuf[0] = bb_xread_char(fd_in);
Robert Griebl94a6a952002-05-19 19:00:14 +0000120
121 maxbits = inbuf[0] & BIT_MASK;
122 block_mode = inbuf[0] & BLOCK_MODE;
123 maxmaxcode = MAXCODE(maxbits);
124
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000125 if (maxbits > BITS) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000126 bb_error_msg("compressed with %d bits, can only handle %d bits", maxbits,
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000127 BITS);
Robert Griebl94a6a952002-05-19 19:00:14 +0000128 return -1;
129 }
130
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000131 maxcode = MAXCODE(n_bits = INIT_BITS) - 1;
132 bitmask = (1 << n_bits) - 1;
Robert Griebl94a6a952002-05-19 19:00:14 +0000133 oldcode = -1;
134 finchar = 0;
135 outpos = 0;
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000136 posbits = 0 << 3;
Robert Griebl94a6a952002-05-19 19:00:14 +0000137
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000138 free_ent = ((block_mode) ? FIRST : 256);
Robert Griebl94a6a952002-05-19 19:00:14 +0000139
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000140 /* As above, initialize the first 256 entries in the table. */
141 clear_tab_prefixof();
Robert Griebl94a6a952002-05-19 19:00:14 +0000142
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000143 for (code = 255; code >= 0; --code) {
144 tab_suffixof(code) = (unsigned char) code;
145 }
Robert Griebl94a6a952002-05-19 19:00:14 +0000146
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000147 do {
148 resetbuf:;
Robert Griebl94a6a952002-05-19 19:00:14 +0000149 {
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000150 int i;
151 int e;
152 int o;
Robert Griebl94a6a952002-05-19 19:00:14 +0000153
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000154 e = insize - (o = (posbits >> 3));
Robert Griebl94a6a952002-05-19 19:00:14 +0000155
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000156 for (i = 0; i < e; ++i)
157 inbuf[i] = inbuf[i + o];
Robert Griebl94a6a952002-05-19 19:00:14 +0000158
159 insize = e;
160 posbits = 0;
161 }
162
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000163 if (insize < (int) sizeof(inbuf) - IBUFSIZ) {
Glenn L McGrath1a2d75f2003-11-21 22:17:28 +0000164 rsize = safe_read(fd_in, inbuf + insize, IBUFSIZ);
Robert Griebl94a6a952002-05-19 19:00:14 +0000165 insize += rsize;
166 }
167
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000168 inbits = ((rsize > 0) ? (insize - insize % n_bits) << 3 :
169 (insize << 3) - (n_bits - 1));
Robert Griebl94a6a952002-05-19 19:00:14 +0000170
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000171 while (inbits > posbits) {
172 if (free_ent > maxcode) {
173 posbits =
174 ((posbits - 1) +
175 ((n_bits << 3) -
176 (posbits - 1 + (n_bits << 3)) % (n_bits << 3)));
Robert Griebl94a6a952002-05-19 19:00:14 +0000177 ++n_bits;
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000178 if (n_bits == maxbits) {
Robert Griebl94a6a952002-05-19 19:00:14 +0000179 maxcode = maxmaxcode;
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000180 } else {
181 maxcode = MAXCODE(n_bits) - 1;
182 }
183 bitmask = (1 << n_bits) - 1;
Robert Griebl94a6a952002-05-19 19:00:14 +0000184 goto resetbuf;
185 }
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000186 {
187 unsigned char *p = &inbuf[posbits >> 3];
Robert Griebl94a6a952002-05-19 19:00:14 +0000188
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000189 code =
190 ((((long) (p[0])) | ((long) (p[1]) << 8) |
191 ((long) (p[2]) << 16)) >> (posbits & 0x7)) & bitmask;
Robert Griebl94a6a952002-05-19 19:00:14 +0000192 }
193 posbits += n_bits;
Robert Griebl94a6a952002-05-19 19:00:14 +0000194
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000195
196 if (oldcode == -1) {
197 outbuf[outpos++] = (unsigned char) (finchar =
198 (int) (oldcode = code));
Robert Griebl94a6a952002-05-19 19:00:14 +0000199 continue;
200 }
201
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000202 if (code == CLEAR && block_mode) {
Robert Griebl94a6a952002-05-19 19:00:14 +0000203 clear_tab_prefixof();
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000204 free_ent = FIRST - 1;
205 posbits =
206 ((posbits - 1) +
207 ((n_bits << 3) -
208 (posbits - 1 + (n_bits << 3)) % (n_bits << 3)));
209 maxcode = MAXCODE(n_bits = INIT_BITS) - 1;
210 bitmask = (1 << n_bits) - 1;
Robert Griebl94a6a952002-05-19 19:00:14 +0000211 goto resetbuf;
212 }
213
214 incode = code;
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000215 stackp = de_stack;
Robert Griebl94a6a952002-05-19 19:00:14 +0000216
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000217 /* Special case for KwKwK string. */
218 if (code >= free_ent) {
219 if (code > free_ent) {
220 unsigned char *p;
Robert Griebl94a6a952002-05-19 19:00:14 +0000221
222 posbits -= n_bits;
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000223 p = &inbuf[posbits >> 3];
Robert Griebl94a6a952002-05-19 19:00:14 +0000224
Manuel Novoa III cad53642003-03-19 09:13:01 +0000225 bb_error_msg
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000226 ("insize:%d posbits:%d inbuf:%02X %02X %02X %02X %02X (%d)",
227 insize, posbits, p[-1], p[0], p[1], p[2], p[3],
228 (posbits & 07));
Manuel Novoa III cad53642003-03-19 09:13:01 +0000229 bb_error_msg("uncompress: corrupt input");
Robert Griebl94a6a952002-05-19 19:00:14 +0000230 return -1;
231 }
232
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000233 *--stackp = (unsigned char) finchar;
234 code = oldcode;
Robert Griebl94a6a952002-05-19 19:00:14 +0000235 }
236
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000237 /* Generate output characters in reverse order */
238 while ((long int) code >= (long int) 256) {
239 *--stackp = tab_suffixof(code);
240 code = tab_prefixof(code);
Robert Griebl94a6a952002-05-19 19:00:14 +0000241 }
242
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000243 *--stackp = (unsigned char) (finchar = tab_suffixof(code));
Robert Griebl94a6a952002-05-19 19:00:14 +0000244
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000245 /* And put them out in forward order */
Robert Griebl94a6a952002-05-19 19:00:14 +0000246 {
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000247 int i;
Robert Griebl94a6a952002-05-19 19:00:14 +0000248
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000249 if (outpos + (i = (de_stack - stackp)) >= OBUFSIZ) {
250 do {
251 if (i > OBUFSIZ - outpos) {
252 i = OBUFSIZ - outpos;
253 }
Robert Griebl94a6a952002-05-19 19:00:14 +0000254
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000255 if (i > 0) {
256 memcpy(outbuf + outpos, stackp, i);
Robert Griebl94a6a952002-05-19 19:00:14 +0000257 outpos += i;
258 }
259
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000260 if (outpos >= OBUFSIZ) {
Glenn L McGrath2e41d0c2002-09-27 06:46:02 +0000261 write(fd_out, outbuf, outpos);
Robert Griebl94a6a952002-05-19 19:00:14 +0000262 outpos = 0;
263 }
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000264 stackp += i;
265 } while ((i = (de_stack - stackp)) > 0);
266 } else {
267 memcpy(outbuf + outpos, stackp, i);
Robert Griebl94a6a952002-05-19 19:00:14 +0000268 outpos += i;
269 }
270 }
271
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000272 /* Generate the new entry. */
273 if ((code = free_ent) < maxmaxcode) {
274 tab_prefixof(code) = (unsigned short) oldcode;
275 tab_suffixof(code) = (unsigned char) finchar;
276 free_ent = code + 1;
277 }
Robert Griebl94a6a952002-05-19 19:00:14 +0000278
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000279 /* Remember previous code. */
280 oldcode = incode;
Robert Griebl94a6a952002-05-19 19:00:14 +0000281 }
282
Glenn L McGrathfedbfe42002-11-28 09:09:47 +0000283 } while (rsize > 0);
Robert Griebl94a6a952002-05-19 19:00:14 +0000284
Glenn L McGrath2e41d0c2002-09-27 06:46:02 +0000285 if (outpos > 0) {
286 write(fd_out, outbuf, outpos);
287 }
Robert Griebl94a6a952002-05-19 19:00:14 +0000288
289 return 0;
290}