/* unlzw.c -- decompress files in LZW format.
* The code in this file is directly derived from the public domain 'compress'
* written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
* Ken Turkowski, Dave Mack and Peter Jannesen.
* This is a temporary version which will be rewritten in some future version
* to accommodate in-memory decompression.
static char rcsid
[] = "$Id: unlzw.c,v 0.15 1993/06/10 13:28:35 jloup Exp $";
typedef unsigned char char_type
;
typedef unsigned long count_int
;
typedef unsigned short count_short
;
typedef unsigned long cmp_code_int
;
#define MAXCODE(n) (1L << (n))
#if BYTEORDER == 4321 && NOALLIGN == 1
# define input(b,o,c,n,m){ \
(c) = (*(long *)(&(b)[(o)>>3])>>((o)&0x7))&(m); \
# define input(b,o,c,n,m){ \
REG1 char_type *p = &(b)[(o)>>3]; \
(c) = ((((long)(p[0]))|((long)(p[1])<<8)| \
((long)(p[2])<<16))>>((o)&0x7))&(m); \
/* DECLARE(ush, tab_prefix, (1<<BITS)); -- prefix code */
# define tab_prefixof(i) tab_prefix[i]
# define clear_tab_prefixof() memzero(tab_prefix, 256);
/* DECLARE(ush, tab_prefix0, (1<<(BITS-1)); -- prefix for even codes */
/* DECLARE(ush, tab_prefix1, (1<<(BITS-1)); -- prefix for odd codes */
# define tab_prefixof(i) tab_prefix[(i)&1][(i)>>1]
# define clear_tab_prefixof() \
memzero(tab_prefix0, 128), \
memzero(tab_prefix1, 128);
#define de_stack ((char_type *)(&d_buf[DIST_BUFSIZE-1]))
#define tab_suffixof(i) tab_suffix[i]
int block_mode
= BLOCK_MODE
; /* block compress mode -C compatible with 2.0 */
/* ============================================================================
* Decompress in to out. This routine adapts to the codes in the
* file building the "string" table on-the-fly; requiring no table to
* be stored in the compressed file.
* IN assertions: the buffer inbuf contains already the beginning of
* the compressed data, from offsets iptr to insize-1 included.
* The magic header has already been checked and skipped.
* bytes_in and bytes_out have been initialized.
int in
, out
; /* input and output file descriptors */
/* REG10 int insize; (global) */
REG14 code_int maxmaxcode
;
tab_prefix
[0] = tab_prefix0
;
tab_prefix
[1] = tab_prefix1
;
block_mode
= maxbits
& BLOCK_MODE
;
if ((maxbits
& LZW_RESERVED
) != 0) {
WARN((stderr
, "\n%s: %s: warning, unknown flags 0x%x\n",
progname
, ifname
, maxbits
& LZW_RESERVED
));
maxmaxcode
= MAXCODE(maxbits
);
"\n%s: %s: compressed with %d bits, can only handle %d bits\n",
progname
, ifname
, maxbits
, BITS
);
maxcode
= MAXCODE(n_bits
= INIT_BITS
)-1;
free_ent
= ((block_mode
) ? FIRST
: 256);
clear_tab_prefixof(); /* Initialize the first 256 entries in the table. */
for (code
= 255 ; code
>= 0 ; --code
) {
tab_suffixof(code
) = (char_type
)code
;
e
= insize
-(o
= (posbits
>>3));
for (i
= 0 ; i
< e
; ++i
) {
if (insize
< INBUF_EXTRA
) {
if ((rsize
= read(in
, (char*)inbuf
+insize
, INBUFSIZ
)) == EOF
) {
inbits
= ((rsize
!= 0) ? ((long)insize
- insize
%n_bits
)<<3 :
((long)insize
<<3)-(n_bits
-1));
while (inbits
> posbits
) {
if (free_ent
> maxcode
) {
((n_bits
<<3)-(posbits
-1+(n_bits
<<3))%(n_bits
<<3)));
maxcode
= MAXCODE(n_bits
)-1;
input(inbuf
,posbits
,code
,n_bits
,bitmask
);
outbuf
[outpos
++] = (char_type
)(finchar
= (int)(oldcode
=code
));
if (code
== CLEAR
&& block_mode
) {
((n_bits
<<3)-(posbits
-1+(n_bits
<<3))%(n_bits
<<3)));
maxcode
= MAXCODE(n_bits
= INIT_BITS
)-1;
if (code
>= free_ent
) { /* Special case for KwKwK string. */
"code:%ld free_ent:%ld n_bits:%d insize:%u\n",
code
, free_ent
, n_bits
, insize
);
"posbits:%ld inbuf:%02X %02X %02X %02X %02X\n",
posbits
, p
[-1],p
[0],p
[1],p
[2],p
[3]);
if (!test
&& outpos
> 0) {
write_buf(out
, (char*)outbuf
, outpos
);
bytes_out
+= (ulg
)outpos
;
error("corrupt input. Use zcat to recover some data.");
*--stackp
= (char_type
)finchar
;
while ((cmp_code_int
)code
>= (cmp_code_int
)256) {
/* Generate output characters in reverse order */
*--stackp
= tab_suffixof(code
);
code
= tab_prefixof(code
);
*--stackp
= (char_type
)(finchar
= tab_suffixof(code
));
/* And put them out in forward order */
if (outpos
+(i
= (de_stack
-stackp
)) >= OUTBUFSIZ
) {
if (i
> OUTBUFSIZ
-outpos
) i
= OUTBUFSIZ
-outpos
;
memcpy(outbuf
+outpos
, stackp
, i
);
if (outpos
>= OUTBUFSIZ
) {
write_buf(out
, (char*)outbuf
, outpos
);
bytes_out
+= (ulg
)outpos
;
} while ((i
= (de_stack
-stackp
)) > 0);
memcpy(outbuf
+outpos
, stackp
, i
);
if ((code
= free_ent
) < maxmaxcode
) { /* Generate the new entry. */
tab_prefixof(code
) = (unsigned short)oldcode
;
tab_suffixof(code
) = (char_type
)finchar
;
oldcode
= incode
; /* Remember previous code. */
if (!test
&& outpos
> 0) {
write_buf(out
, (char*)outbuf
, outpos
);
bytes_out
+= (ulg
)outpos
;