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.
19 typedef unsigned char char_type
;
20 typedef long code_int
;
21 typedef unsigned long count_int
;
22 typedef unsigned short count_short
;
23 typedef unsigned long cmp_code_int
;
25 #define MAXCODE(n) (1L << (n))
28 # define BYTEORDER 0000
58 #if BYTEORDER == 4321 && NOALLIGN == 1
59 # define input(b,o,c,n,m){ \
60 (c) = (*(long *)(&(b)[(o)>>3])>>((o)&0x7))&(m); \
64 # define input(b,o,c,n,m){ \
65 char_type *p = &(b)[(o)>>3]; \
66 (c) = ((((long)(p[0]))|((long)(p[1])<<8)| \
67 ((long)(p[2])<<16))>>((o)&0x7))&(m); \
73 /* DECLARE(ush, tab_prefix, (1<<BITS)); -- prefix code */
74 # define tab_prefixof(i) tab_prefix[i]
75 # define clear_tab_prefixof() memzero(tab_prefix, 256);
77 /* DECLARE(ush, tab_prefix0, (1<<(BITS-1)); -- prefix for even codes */
78 /* DECLARE(ush, tab_prefix1, (1<<(BITS-1)); -- prefix for odd codes */
80 # define tab_prefixof(i) tab_prefix[(i)&1][(i)>>1]
81 # define clear_tab_prefixof() \
82 memzero(tab_prefix0, 128), \
83 memzero(tab_prefix1, 128);
85 #define de_stack ((char_type *)(&d_buf[DIST_BUFSIZE-1]))
86 #define tab_suffixof(i) tab_suffix[i]
88 /* block compress mode -C compatible with 2.0 */
89 static int block_mode
= BLOCK_MODE
;
91 /* ============================================================================
92 * Decompress in to out. This routine adapts to the codes in the
93 * file building the "string" table on-the-fly; requiring no table to
94 * be stored in the compressed file.
95 * IN assertions: the buffer inbuf contains already the beginning of
96 * the compressed data, from offsets iptr to insize-1 included.
97 * The magic header has already been checked and skipped.
98 * bytes_in and bytes_out have been initialized.
99 * 'in' and 'out' are the input and output file descriptors.
102 unlzw (int in
, int out
)
112 /* int insize; (global) */
121 tab_prefix
[0] = tab_prefix0
;
122 tab_prefix
[1] = tab_prefix1
;
124 maxbits
= get_byte();
125 block_mode
= maxbits
& BLOCK_MODE
;
126 if ((maxbits
& LZW_RESERVED
) != 0) {
127 WARN((stderr
, "\n%s: %s: warning, unknown flags 0x%x\n",
128 program_name
, ifname
, (unsigned int) maxbits
& LZW_RESERVED
));
131 maxmaxcode
= MAXCODE(maxbits
);
133 if (maxbits
> BITS
) {
135 "\n%s: %s: compressed with %d bits, can only handle %d bits\n",
136 program_name
, ifname
, maxbits
, BITS
);
141 maxcode
= MAXCODE(n_bits
= INIT_BITS
)-1;
142 bitmask
= (1<<n_bits
)-1;
148 free_ent
= ((block_mode
) ? FIRST
: 256);
150 clear_tab_prefixof(); /* Initialize the first 256 entries in the table. */
152 for (code
= 255 ; code
>= 0 ; --code
) {
153 tab_suffixof(code
) = (char_type
)code
;
162 e
= o
<= insize
? insize
- o
: 0;
164 for (i
= 0 ; i
< e
; ++i
) {
165 inbuf
[i
] = inbuf
[i
+o
];
170 if (insize
< INBUF_EXTRA
) {
171 rsize
= read_buffer (in
, (char *) inbuf
+ insize
, INBUFSIZ
);
176 bytes_in
+= (off_t
)rsize
;
178 inbits
= ((rsize
!= 0) ? ((long)insize
- insize
%n_bits
)<<3 :
179 ((long)insize
<<3)-(n_bits
-1));
181 while (inbits
> posbits
) {
182 if (free_ent
> maxcode
) {
183 posbits
= ((posbits
-1) +
184 ((n_bits
<<3)-(posbits
-1+(n_bits
<<3))%(n_bits
<<3)));
186 if (n_bits
== maxbits
) {
187 maxcode
= maxmaxcode
;
189 maxcode
= MAXCODE(n_bits
)-1;
191 bitmask
= (1<<n_bits
)-1;
194 input(inbuf
,posbits
,code
,n_bits
,bitmask
);
195 Tracev((stderr
, "%ld ", code
));
199 gzip_error ("corrupt input.");
200 outbuf
[outpos
++] = (char_type
)(finchar
= (int)(oldcode
=code
));
203 if (code
== CLEAR
&& block_mode
) {
204 clear_tab_prefixof();
205 free_ent
= FIRST
- 1;
206 posbits
= ((posbits
-1) +
207 ((n_bits
<<3)-(posbits
-1+(n_bits
<<3))%(n_bits
<<3)));
208 maxcode
= MAXCODE(n_bits
= INIT_BITS
)-1;
209 bitmask
= (1<<n_bits
)-1;
215 if (code
>= free_ent
) { /* Special case for KwKwK string. */
216 if (code
> free_ent
) {
221 p
= &inbuf
[posbits
>>3];
223 "code:%ld free_ent:%ld n_bits:%d insize:%u\n",
224 code
, free_ent
, n_bits
, insize
);
226 "posbits:%ld inbuf:%02X %02X %02X %02X %02X\n",
227 posbits
, p
[-1],p
[0],p
[1],p
[2],p
[3]);
230 write_buf (out
, outbuf
, outpos
);
231 gzip_error (to_stdout
233 : "corrupt input. Use zcat to recover some data.");
235 *--stackp
= (char_type
)finchar
;
239 while ((cmp_code_int
)code
>= (cmp_code_int
)256) {
240 /* Generate output characters in reverse order */
241 *--stackp
= tab_suffixof(code
);
242 code
= tab_prefixof(code
);
244 *--stackp
= (char_type
)(finchar
= tab_suffixof(code
));
246 /* And put them out in forward order */
250 if (outpos
+(i
= (de_stack
-stackp
)) >= OUTBUFSIZ
) {
252 if (i
> OUTBUFSIZ
-outpos
) i
= OUTBUFSIZ
-outpos
;
255 memcpy(outbuf
+outpos
, stackp
, i
);
258 if (outpos
>= OUTBUFSIZ
) {
259 write_buf (out
, outbuf
, outpos
);
263 } while ((i
= (de_stack
-stackp
)) > 0);
265 memcpy(outbuf
+outpos
, stackp
, i
);
270 if ((code
= free_ent
) < maxmaxcode
) { /* Generate the new entry. */
272 tab_prefixof(code
) = (unsigned short)oldcode
;
273 tab_suffixof(code
) = (char_type
)finchar
;
276 oldcode
= incode
; /* Remember previous code. */
278 } while (rsize
!= 0);
281 write_buf (out
, outbuf
, outpos
);