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$";
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 if ((rsize
= read(in
, (char*)inbuf
+insize
, INBUFSIZ
)) == -1) {
264 bytes_in
+= (off_t
)rsize
;
266 inbits
= ((rsize
!= 0) ? ((long)insize
- insize
%n_bits
)<<3 :
267 ((long)insize
<<3)-(n_bits
-1));
269 while (inbits
> posbits
) {
270 if (free_ent
> maxcode
) {
271 posbits
= ((posbits
-1) +
272 ((n_bits
<<3)-(posbits
-1+(n_bits
<<3))%(n_bits
<<3)));
274 if (n_bits
== maxbits
) {
275 maxcode
= maxmaxcode
;
277 maxcode
= MAXCODE(n_bits
)-1;
279 bitmask
= (1<<n_bits
)-1;
282 input(inbuf
,posbits
,code
,n_bits
,bitmask
);
283 Tracev((stderr
, "%d ", code
));
287 gzip_error ("corrupt input.");
288 outbuf
[outpos
++] = (char_type
)(finchar
= (int)(oldcode
=code
));
291 if (code
== CLEAR
&& block_mode
) {
292 clear_tab_prefixof();
293 free_ent
= FIRST
- 1;
294 posbits
= ((posbits
-1) +
295 ((n_bits
<<3)-(posbits
-1+(n_bits
<<3))%(n_bits
<<3)));
296 maxcode
= MAXCODE(n_bits
= INIT_BITS
)-1;
297 bitmask
= (1<<n_bits
)-1;
303 if (code
>= free_ent
) { /* Special case for KwKwK string. */
304 if (code
> free_ent
) {
309 p
= &inbuf
[posbits
>>3];
311 "code:%ld free_ent:%ld n_bits:%d insize:%u\n",
312 code
, free_ent
, n_bits
, insize
);
314 "posbits:%ld inbuf:%02X %02X %02X %02X %02X\n",
315 posbits
, p
[-1],p
[0],p
[1],p
[2],p
[3]);
317 if (!test
&& outpos
> 0) {
318 write_buf(out
, (char*)outbuf
, outpos
);
319 bytes_out
+= (off_t
)outpos
;
321 gzip_error (to_stdout
323 : "corrupt input. Use zcat to recover some data.");
325 *--stackp
= (char_type
)finchar
;
329 while ((cmp_code_int
)code
>= (cmp_code_int
)256) {
330 /* Generate output characters in reverse order */
331 *--stackp
= tab_suffixof(code
);
332 code
= tab_prefixof(code
);
334 *--stackp
= (char_type
)(finchar
= tab_suffixof(code
));
336 /* And put them out in forward order */
340 if (outpos
+(i
= (de_stack
-stackp
)) >= OUTBUFSIZ
) {
342 if (i
> OUTBUFSIZ
-outpos
) i
= OUTBUFSIZ
-outpos
;
345 memcpy(outbuf
+outpos
, stackp
, i
);
348 if (outpos
>= OUTBUFSIZ
) {
350 write_buf(out
, (char*)outbuf
, outpos
);
351 bytes_out
+= (off_t
)outpos
;
356 } while ((i
= (de_stack
-stackp
)) > 0);
358 memcpy(outbuf
+outpos
, stackp
, i
);
363 if ((code
= free_ent
) < maxmaxcode
) { /* Generate the new entry. */
365 tab_prefixof(code
) = (unsigned short)oldcode
;
366 tab_suffixof(code
) = (char_type
)finchar
;
369 oldcode
= incode
; /* Remember previous code. */
371 } while (rsize
!= 0);
373 if (!test
&& outpos
> 0) {
374 write_buf(out
, (char*)outbuf
, outpos
);
375 bytes_out
+= (off_t
)outpos
;