1 /* vi: set sw=4 ts=4: */
2 /* uncompress for busybox -- (c) 2002 Robert Griebl
4 * based on the original compress42.c source
5 * (see disclaimer below)
8 /* (N)compress42.c - File compression ala IEEE Computer, Mar 1992.
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
21 * marc@suse.de : a small security fix for a buffer overflow
23 * [... History snipped ...]
28 #include "bb_archive.h"
31 /* Default input buffer size */
34 /* Default output buffer size */
37 /* Defines for third byte of header */
38 #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 */
49 #define INIT_BITS 9 /* initial number of bits/code */
52 /* machine variants which require cc -Dmachine: pdp11, z8000, DOS */
53 #define HBITS 17 /* 50% occupancy */
54 #define HSIZE (1<<HBITS)
55 #define HMASK (HSIZE-1) /* unused */
56 #define HPRIME 9941 /* unused */
59 #undef MAXSEG_64K /* unused */
60 #define MAXCODE(n) (1L << (n))
62 #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)
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
72 * be stored in the compressed file.
75 IF_DESKTOP(long long) int FAST_FUNC
76 unpack_Z_stream(transformer_aux_data_t
*aux
, int src_fd
, int dst_fd
)
78 IF_DESKTOP(long long total_written
= 0;)
79 IF_DESKTOP(long long) int retval
= -1;
80 unsigned char *stackp
;
95 unsigned char *inbuf
; /* were eating insane amounts of stack - */
96 unsigned char *outbuf
; /* bad for some embedded targets */
98 unsigned short *codetab
;
100 /* Hmm, these were statics - why?! */
101 /* user settable max # bits/code */
102 int maxbits
; /* = BITS; */
103 /* block compress mode -C compatible with 2.0 */
104 int block_mode
; /* = BLOCK_MODE; */
106 if (check_signature16(aux
, src_fd
, COMPRESS_MAGIC
))
109 inbuf
= xzalloc(IBUFSIZ
+ 64);
110 outbuf
= xzalloc(OBUFSIZ
+ 2048);
111 htab
= xzalloc(HSIZE
); /* wasn't zeroed out before, maybe can xmalloc? */
112 codetab
= xzalloc(HSIZE
* sizeof(codetab
[0]));
116 /* xread isn't good here, we have to return - caller may want
117 * to do some cleanup (e.g. delete incomplete unpacked file etc) */
118 if (full_read(src_fd
, inbuf
, 1) != 1) {
119 bb_error_msg("short read");
123 maxbits
= inbuf
[0] & BIT_MASK
;
124 block_mode
= inbuf
[0] & BLOCK_MODE
;
125 maxmaxcode
= MAXCODE(maxbits
);
127 if (maxbits
> BITS
) {
128 bb_error_msg("compressed with %d bits, can only handle "
129 BITS_STR
" bits", maxbits
);
134 maxcode
= MAXCODE(INIT_BITS
) - 1;
135 bitmask
= (1 << INIT_BITS
) - 1;
141 free_ent
= ((block_mode
) ? FIRST
: 256);
143 /* As above, initialize the first 256 entries in the table. */
144 /*clear_tab_prefixof(); - done by xzalloc */
146 for (code
= 255; code
>= 0; --code
) {
147 tab_suffixof(code
) = (unsigned char) code
;
160 for (i
= 0; i
< e
; ++i
)
161 inbuf
[i
] = inbuf
[i
+ o
];
167 if (insize
< (int) (IBUFSIZ
+ 64) - IBUFSIZ
) {
168 rsize
= safe_read(src_fd
, inbuf
+ insize
, IBUFSIZ
);
170 bb_error_msg_and_die(bb_msg_read_error
);
174 inbits
= ((rsize
> 0) ? (insize
- insize
% n_bits
) << 3 :
175 (insize
<< 3) - (n_bits
- 1));
177 while (inbits
> posbits
) {
178 if (free_ent
> maxcode
) {
182 (posbits
- 1 + (n_bits
<< 3)) % (n_bits
<< 3)));
184 if (n_bits
== maxbits
) {
185 maxcode
= maxmaxcode
;
187 maxcode
= MAXCODE(n_bits
) - 1;
189 bitmask
= (1 << n_bits
) - 1;
193 unsigned char *p
= &inbuf
[posbits
>> 3];
195 code
= ((((long) (p
[0])) | ((long) (p
[1]) << 8) |
196 ((long) (p
[2]) << 16)) >> (posbits
& 0x7)) & bitmask
;
203 bb_error_msg_and_die("corrupted data"); /* %ld", code); */
205 finchar
= (int) oldcode
;
206 outbuf
[outpos
++] = (unsigned char) finchar
;
210 if (code
== CLEAR
&& block_mode
) {
211 clear_tab_prefixof();
212 free_ent
= FIRST
- 1;
216 (posbits
- 1 + (n_bits
<< 3)) % (n_bits
<< 3)));
218 maxcode
= MAXCODE(INIT_BITS
) - 1;
219 bitmask
= (1 << INIT_BITS
) - 1;
226 /* Special case for KwKwK string. */
227 if (code
>= free_ent
) {
228 if (code
> free_ent
) {
232 p
= &inbuf
[posbits
>> 3];
235 ("insize:%d posbits:%d inbuf:%02X %02X %02X %02X %02X (%d)",
236 insize
, posbits
, p
[-1], p
[0], p
[1], p
[2], p
[3],
238 bb_error_msg("corrupted data");
242 *--stackp
= (unsigned char) finchar
;
246 /* Generate output characters in reverse order */
247 while ((long) code
>= (long) 256) {
248 if (stackp
<= &htabof(0))
249 bb_error_msg_and_die("corrupted data");
250 *--stackp
= tab_suffixof(code
);
251 code
= tab_prefixof(code
);
254 finchar
= tab_suffixof(code
);
255 *--stackp
= (unsigned char) finchar
;
257 /* And put them out in forward order */
261 i
= de_stack
- stackp
;
262 if (outpos
+ i
>= OBUFSIZ
) {
264 if (i
> OBUFSIZ
- outpos
) {
265 i
= OBUFSIZ
- outpos
;
269 memcpy(outbuf
+ outpos
, stackp
, i
);
273 if (outpos
>= OBUFSIZ
) {
274 xwrite(dst_fd
, outbuf
, outpos
);
275 IF_DESKTOP(total_written
+= outpos
;)
279 i
= de_stack
- stackp
;
282 memcpy(outbuf
+ outpos
, stackp
, i
);
287 /* Generate the new entry. */
289 if (code
< maxmaxcode
) {
290 tab_prefixof(code
) = (unsigned short) oldcode
;
291 tab_suffixof(code
) = (unsigned char) finchar
;
295 /* Remember previous code. */
302 xwrite(dst_fd
, outbuf
, outpos
);
303 IF_DESKTOP(total_written
+= outpos
;)
306 retval
= IF_DESKTOP(total_written
) + 0;