1 /* unlzw.c -- decompress files in LZW format.
2 * The code in this file is directly derived from the public domain 'compress'
3 * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
4 * Ken Turkowski, Dave Mack and Peter Jannesen.
6 * This is a temporary version which will be rewritten in some future version
7 * to accommodate in-memory decompression.
11 static char rcsid[] = "$Id: unlzw.c,v 1.5 2006/12/11 18:54:39 eggert Exp $";
27 typedef unsigned char char_type;
28 typedef long code_int;
29 typedef unsigned long count_int;
30 typedef unsigned short count_short;
31 typedef unsigned long cmp_code_int;
33 #define MAXCODE(n) (1L << (n))
56 # define REG1 register
60 # define REG2 register
64 # define REG3 register
68 # define REG4 register
72 # define REG5 register
76 # define REG6 register
80 # define REG7 register
84 # define REG8 register
88 # define REG9 register
92 # define REG10 register
96 # define REG11 register
100 # define REG12 register
104 # define REG13 register
108 # define REG14 register
112 # define REG15 register
116 # define REG16 register
120 # define BYTEORDER 0000
131 #if BYTEORDER == 4321
137 #if BYTEORDER == 1234
150 #if BYTEORDER == 4321 && NOALLIGN == 1
151 # define input(b,o,c,n,m){ \
152 (c) = (*(long *)(&(b)[(o)>>3])>>((o)&0x7))&(m); \
156 # define input(b,o,c,n,m){ \
157 REG1 char_type *p = &(b)[(o)>>3]; \
158 (c) = ((((long)(p[0]))|((long)(p[1])<<8)| \
159 ((long)(p[2])<<16))>>((o)&0x7))&(m); \
165 /* DECLARE(ush, tab_prefix, (1<<BITS)); -- prefix code */
166 # define tab_prefixof(i) tab_prefix[i]
167 # define clear_tab_prefixof() memzero(tab_prefix, 256);
169 /* DECLARE(ush, tab_prefix0, (1<<(BITS-1)); -- prefix for even codes */
170 /* DECLARE(ush, tab_prefix1, (1<<(BITS-1)); -- prefix for odd codes */
172 # define tab_prefixof(i) tab_prefix[(i)&1][(i)>>1]
173 # define clear_tab_prefixof() \
174 memzero(tab_prefix0, 128), \
175 memzero(tab_prefix1, 128);
177 #define de_stack ((char_type *)(&d_buf[DIST_BUFSIZE-1]))
178 #define tab_suffixof(i) tab_suffix[i]
180 int block_mode = BLOCK_MODE; /* block compress mode -C compatible with 2.0 */
182 /* ============================================================================
183 * Decompress in to out. This routine adapts to the codes in the
184 * file building the "string" table on-the-fly; requiring no table to
185 * be stored in the compressed file.
186 * IN assertions: the buffer inbuf contains already the beginning of
187 * the compressed data, from offsets iptr to insize-1 included.
188 * The magic header has already been checked and skipped.
189 * bytes_in and bytes_out have been initialized.
192 int in, out; /* input and output file descriptors */
194 REG2 char_type *stackp;
197 REG5 code_int oldcode;
198 REG6 code_int incode;
202 /* REG10 int insize; (global) */
203 REG11 unsigned bitmask;
204 REG12 code_int free_ent;
205 REG13 code_int maxcode;
206 REG14 code_int maxmaxcode;
211 tab_prefix[0] = tab_prefix0;
212 tab_prefix[1] = tab_prefix1;
214 maxbits = get_byte();
215 block_mode = maxbits & BLOCK_MODE;
216 if ((maxbits & LZW_RESERVED) != 0) {
217 WARN((stderr, "\n%s: %s: warning, unknown flags 0x%x\n",
218 program_name, ifname, maxbits & LZW_RESERVED));
221 maxmaxcode = MAXCODE(maxbits);
223 if (maxbits > BITS) {
225 "\n%s: %s: compressed with %d bits, can only handle %d bits\n",
226 program_name, ifname, maxbits, BITS);
231 maxcode = MAXCODE(n_bits = INIT_BITS)-1;
232 bitmask = (1<<n_bits)-1;
238 free_ent = ((block_mode) ? FIRST : 256);
240 clear_tab_prefixof(); /* Initialize the first 256 entries in the table. */
242 for (code = 255 ; code >= 0 ; --code) {
243 tab_suffixof(code) = (char_type)code;
251 e = insize-(o = (posbits>>3));
253 for (i = 0 ; i < e ; ++i) {
254 inbuf[i] = inbuf[i+o];
259 if (insize < INBUF_EXTRA) {
260 rsize = read_buffer (in, (char *) inbuf + insize, INBUFSIZ);
265 bytes_in += (off_t)rsize;
267 inbits = ((rsize != 0) ? ((long)insize - insize%n_bits)<<3 :
268 ((long)insize<<3)-(n_bits-1));
270 while (inbits > posbits) {
271 if (free_ent > maxcode) {
272 posbits = ((posbits-1) +
273 ((n_bits<<3)-(posbits-1+(n_bits<<3))%(n_bits<<3)));
275 if (n_bits == maxbits) {
276 maxcode = maxmaxcode;
278 maxcode = MAXCODE(n_bits)-1;
280 bitmask = (1<<n_bits)-1;
283 input(inbuf,posbits,code,n_bits,bitmask);
284 Tracev((stderr, "%d ", code));
288 gzip_error ("corrupt input.");
289 outbuf[outpos++] = (char_type)(finchar = (int)(oldcode=code));
292 if (code == CLEAR && block_mode) {
293 clear_tab_prefixof();
294 free_ent = FIRST - 1;
295 posbits = ((posbits-1) +
296 ((n_bits<<3)-(posbits-1+(n_bits<<3))%(n_bits<<3)));
297 maxcode = MAXCODE(n_bits = INIT_BITS)-1;
298 bitmask = (1<<n_bits)-1;
304 if (code >= free_ent) { /* Special case for KwKwK string. */
305 if (code > free_ent) {
310 p = &inbuf[posbits>>3];
312 "code:%ld free_ent:%ld n_bits:%d insize:%u\n",
313 code, free_ent, n_bits, insize);
315 "posbits:%ld inbuf:%02X %02X %02X %02X %02X\n",
316 posbits, p[-1],p[0],p[1],p[2],p[3]);
318 if (!test && outpos > 0) {
319 write_buf(out, (char*)outbuf, outpos);
320 bytes_out += (off_t)outpos;
322 gzip_error (to_stdout
324 : "corrupt input. Use zcat to recover some data.");
326 *--stackp = (char_type)finchar;
330 while ((cmp_code_int)code >= (cmp_code_int)256) {
331 /* Generate output characters in reverse order */
332 *--stackp = tab_suffixof(code);
333 code = tab_prefixof(code);
335 *--stackp = (char_type)(finchar = tab_suffixof(code));
337 /* And put them out in forward order */
341 if (outpos+(i = (de_stack-stackp)) >= OUTBUFSIZ) {
343 if (i > OUTBUFSIZ-outpos) i = OUTBUFSIZ-outpos;
346 memcpy(outbuf+outpos, stackp, i);
349 if (outpos >= OUTBUFSIZ) {
351 write_buf(out, (char*)outbuf, outpos);
352 bytes_out += (off_t)outpos;
357 } while ((i = (de_stack-stackp)) > 0);
359 memcpy(outbuf+outpos, stackp, i);
364 if ((code = free_ent) < maxmaxcode) { /* Generate the new entry. */
366 tab_prefixof(code) = (unsigned short)oldcode;
367 tab_suffixof(code) = (char_type)finchar;
370 oldcode = incode; /* Remember previous code. */
372 } while (rsize != 0);
374 if (!test && outpos > 0) {
375 write_buf(out, (char*)outbuf, outpos);
376 bytes_out += (off_t)outpos;