4 #ifdef CONFIG_FEATURE_UNCOMPRESS
6 /* uncompress for busybox -- (c) 2002 Robert Griebl
8 * based on the original compress42.c source
9 * (see disclaimer below)
13 /* (N)compress42.c - File compression ala IEEE Computer, Mar 1992.
16 * Spencer W. Thomas (decvax!harpo!utah-cs!utah-gr!thomas)
17 * Jim McKie (decvax!mcvax!jim)
18 * Steve Davies (decvax!vax135!petsd!peora!srd)
19 * Ken Turkowski (decvax!decwrl!turtlevax!ken)
20 * James A. Woods (decvax!ihnp4!ames!jaw)
21 * Joe Orost (decvax!vax135!petsd!joe)
22 * Dave Mack (csu@alembic.acs.com)
23 * Peter Jannesen, Network Communication Systems
26 * marc@suse.de : a small security fix for a buffer overflow
28 * [... History snipped ...]
34 #define IBUFSIZ 2048 /* Defailt input buffer size */
35 #define OBUFSIZ 2048 /* Default output buffer size */
37 /* Defines for third byte of header */
38 #define MAGIC_1 (char_type)'\037'/* First byte of compressed file */
39 #define MAGIC_2 (char_type)'\235'/* Second byte of compressed file */
40 #define BIT_MASK 0x1f /* Mask for 'number of compresssion bits' */
41 /* Masks 0x20 and 0x40 are free. */
42 /* I think 0x20 should mean that there is */
43 /* a fourth header byte (for expansion). */
44 #define BLOCK_MODE 0x80 /* Block compresssion if table is full and */
45 /* compression rate is dropping flush tables */
47 /* the next two codes should not be changed lightly, as they must not */
48 /* lie within the contiguous general code space. */
49 #define FIRST 257 /* first free entry */
50 #define CLEAR 256 /* table clear output code */
52 #define INIT_BITS 9 /* initial number of bits/code */
56 * machine variants which require cc -Dmachine: pdp11, z8000, DOS
60 #define HBITS 17 /* 50% occupancy */
61 #define HSIZE (1<<HBITS)
62 #define HMASK (HSIZE-1)
67 typedef long int code_int;
69 typedef long int count_int;
70 typedef long int cmp_code_int;
72 typedef unsigned char char_type;
74 #define MAXCODE(n) (1L << (n))
78 int block_mode = BLOCK_MODE;/* Block compress mode -C compatible with 2.0*/
79 int maxbits = BITS; /* user settable max # bits/code */
80 int exit_code = -1; /* Exitcode of compress (-1 no file compressed) */
82 char_type inbuf[IBUFSIZ+64]; /* Input buffer */
83 char_type outbuf[OBUFSIZ+2048];/* Output buffer */
86 count_int htab[HSIZE];
87 unsigned short codetab[HSIZE];
89 #define htabof(i) htab[i]
90 #define codetabof(i) codetab[i]
91 #define tab_prefixof(i) codetabof(i)
92 #define tab_suffixof(i) ((char_type *)(htab))[i]
93 #define de_stack ((char_type *)&(htab[HSIZE-1]))
94 #define clear_htab() memset(htab, -1, sizeof(htab))
95 #define clear_tab_prefixof() memset(codetab, 0, 256);
98 extern int uncompress ( FILE *, FILE * );
102 * Decompress stdin to stdout. This routine adapts to the codes in the
103 * file building the "string" table on-the-fly; requiring no table to
104 * be stored in the compressed file. The tables used herein are shared
105 * with those of the compress() routine. See the definitions above.
108 int uncompress ( FILE * fdin, FILE * fdout )
128 inbuf [0] = fgetc(fdin);
130 maxbits = inbuf[0] & BIT_MASK;
131 block_mode = inbuf[0] & BLOCK_MODE;
132 maxmaxcode = MAXCODE(maxbits);
136 fprintf(stderr, "compressed with %d bits, can only handle %d bits\n", maxbits, BITS);
140 //fprintf(stderr, "Bits: %d, block_mode: %d\n", maxbits, block_mode );
142 maxcode = MAXCODE(n_bits = INIT_BITS)-1;
143 bitmask = (1<<n_bits)-1;
149 free_ent = ((block_mode) ? FIRST : 256);
151 clear_tab_prefixof(); /* As above, initialize the first
152 256 entries in the table. */
154 for (code = 255 ; code >= 0 ; --code)
155 tab_suffixof(code) = (char_type)code;
165 e = insize-(o = (posbits>>3));
167 for (i = 0 ; i < e ; ++i)
168 inbuf[i] = inbuf[i+o];
174 if (insize < (int) sizeof(inbuf)-IBUFSIZ)
176 rsize = fread(inbuf+insize, 1,IBUFSIZ,fdin);
178 if ( !rsize && ferror(fdin))
184 inbits = ((rsize > 0) ? (insize - insize%n_bits)<<3 :
185 (insize<<3)-(n_bits-1));
187 while (inbits > posbits)
189 if (free_ent > maxcode)
191 posbits = ((posbits-1) + ((n_bits<<3) -
192 (posbits-1+(n_bits<<3))%(n_bits<<3)));
195 if (n_bits == maxbits)
196 maxcode = maxmaxcode;
198 maxcode = MAXCODE(n_bits)-1;
200 bitmask = (1<<n_bits)-1;
206 char_type *p = &inbuf[posbits>>3];
207 code = ((((long)(p[0]))|((long)(p[1])<<8)|((long)(p[2])<<16))>>(posbits&0x7))&bitmask;
214 outbuf[outpos++] = (char_type)(finchar = (int)(oldcode = code));
218 if (code == CLEAR && block_mode)
220 clear_tab_prefixof();
221 free_ent = FIRST - 1;
222 posbits = ((posbits-1) + ((n_bits<<3) -
223 (posbits-1+(n_bits<<3))%(n_bits<<3)));
224 maxcode = MAXCODE(n_bits = INIT_BITS)-1;
225 bitmask = (1<<n_bits)-1;
232 if (code >= free_ent) /* Special case for KwKwK string. */
239 p = &inbuf[posbits>>3];
241 fprintf(stderr, "insize:%d posbits:%d inbuf:%02X %02X %02X %02X %02X (%d)\n", insize, posbits,
242 p[-1],p[0],p[1],p[2],p[3], (posbits&07));
243 fprintf(stderr, "uncompress: corrupt input\n");
247 *--stackp = (char_type)finchar;
251 while ((cmp_code_int)code >= (cmp_code_int)256)
252 { /* Generate output characters in reverse order */
253 *--stackp = tab_suffixof(code);
254 code = tab_prefixof(code);
257 *--stackp = (char_type)(finchar = tab_suffixof(code));
259 /* And put them out in forward order */
264 if (outpos+(i = (de_stack-stackp)) >= OBUFSIZ)
268 if (i > OBUFSIZ-outpos) i = OBUFSIZ-outpos;
272 memcpy(outbuf+outpos, stackp, i);
276 if (outpos >= OBUFSIZ)
278 fwrite(outbuf, 1,outpos,fdout);
284 while ((i = (de_stack-stackp)) > 0);
288 memcpy(outbuf+outpos, stackp, i);
293 if ((code = free_ent) < maxmaxcode) /* Generate the new entry. */
295 tab_prefixof(code) = (unsigned short)oldcode;
296 tab_suffixof(code) = (char_type)finchar;
300 oldcode = incode; /* Remember previous code. */
307 fwrite(outbuf, outpos,1, fdout);