--- unlzw.c Thu May 20 20:56:19 1999 +++ unlzw/unlzw.cc Wed Nov 17 21:18:23 1999 @@ -7,23 +7,17 @@ * to accommodate in-memory decompression. */ -#ifdef RCSID -static char rcsid[] = "$Id: unlzw.diffs,v 1.1 2000/02/26 17:40:39 nobody Exp $"; -#endif - -#include +/* + * Modified for integration into Apdf by E. Lesueur. + */ -#include "tailor.h" +#define IMPL -#ifdef HAVE_UNISTD_H -# include -#endif -#ifndef NO_FCNTL_H -# include -#endif +#include +#include "../xpdf-0.90/xpdf/UnLZW.h" -#include "gzip.h" -#include "lzw.h" +#define memzero(s, n) memset((s), 0, (n)) +#define Tracev(x) typedef unsigned char char_type; typedef long code_int; @@ -31,6 +25,24 @@ typedef unsigned short count_short; typedef unsigned long cmp_code_int; +static const int INIT_BITS = 9; +static const code_int CLEAR = 256; +static const code_int FIRST = 257; +static const int INBUFSIZ = 0x8000; +static const int INBUF_EXTRA = 64; +static const int OUTBUFSIZ = 16384; +static const int OUTBUF_EXTRA = 2048; +static const int DIST_BUFSIZE = 0x8000; +static const int test = 0; +static const int maxbits = 0x0c; +static const int block_mode = 1; + +static unsigned short tab_prefix[1<<16]; +static unsigned char tab_suffix[1<<16]; +static unsigned char inbuf[INBUFSIZ+INBUF_EXTRA]; +static unsigned char outbuf[OUTBUFSIZ+OUTBUF_EXTRA]; +static unsigned short d_buf[DIST_BUFSIZE]; + #define MAXCODE(n) (1L << (n)) #ifndef REGISTERS @@ -126,28 +138,6 @@ #endif -union bytes { - long word; - struct { -#if BYTEORDER == 4321 - char_type b1; - char_type b2; - char_type b3; - char_type b4; -#else -#if BYTEORDER == 1234 - char_type b4; - char_type b3; - char_type b2; - char_type b1; -#else -# undef BYTEORDER - int dummy; -#endif -#endif - } bytes; -}; - #if BYTEORDER == 4321 && NOALLIGN == 1 # define input(b,o,c,n,m){ \ (c) = (*(long *)(&(b)[(o)>>3])>>((o)&0x7))&(m); \ @@ -178,7 +168,6 @@ #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 @@ -189,9 +178,8 @@ * The magic header has already been checked and skipped. * bytes_in and bytes_out have been initialized. */ -int unlzw(in, out) - int in, out; /* input and output file descriptors */ -{ + +extern "C" const char* unlzw(Pipe* in,Pipe* out,const char*) { REG2 char_type *stackp; REG3 code_int code; REG4 int finchar; @@ -200,7 +188,7 @@ REG7 long inbits; REG8 long posbits; REG9 int outpos; -/* REG10 int insize; (global) */ + REG10 int insize; REG11 unsigned bitmask; REG12 code_int free_ent; REG13 code_int maxcode; @@ -212,29 +200,17 @@ tab_prefix[0] = tab_prefix0; tab_prefix[1] = tab_prefix1; #endif - maxbits = get_byte(); - 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)); - } - maxbits &= BIT_MASK; maxmaxcode = MAXCODE(maxbits); - if (maxbits > BITS) { - fprintf(stderr, - "\n%s: %s: compressed with %d bits, can only handle %d bits\n", - progname, ifname, maxbits, BITS); - exit_code = ERROR; - return ERROR; - } - rsize = insize; + if ((rsize = insize = in->read(inbuf, INBUFSIZ)) == EOF) + return "unexpected end of file."; + maxcode = MAXCODE(n_bits = INIT_BITS)-1; bitmask = (1<read((char*)inbuf+insize, INBUFSIZ)) == EOF) + return "unexpected end of file."; insize += rsize; - bytes_in += (ulg)rsize; } inbits = ((rsize != 0) ? ((long)insize - insize%n_bits)<<3 : ((long)insize<<3)-(n_bits-1)); @@ -284,7 +258,8 @@ Tracev((stderr, "%d ", code)); if (oldcode == -1) { - if (code >= 256) error("corrupt input."); + if (code >= 256) + return "corrupt input."; outbuf[outpos++] = (char_type)(finchar = (int)(oldcode=code)); continue; } @@ -315,11 +290,9 @@ posbits, p[-1],p[0],p[1],p[2],p[3]); #endif if (!test && outpos > 0) { - write_buf(out, (char*)outbuf, outpos); - bytes_out += (ulg)outpos; + out->write(outbuf, outpos); } - error(to_stdout ? "corrupt input." : - "corrupt input. Use zcat to recover some data."); + return "corrupt input."; } *--stackp = (char_type)finchar; code = oldcode; @@ -346,8 +319,7 @@ } if (outpos >= OUTBUFSIZ) { if (!test) { - write_buf(out, (char*)outbuf, outpos); - bytes_out += (ulg)outpos; + out->write(outbuf, outpos); } outpos = 0; } @@ -370,8 +342,9 @@ } while (rsize != 0); if (!test && outpos > 0) { - write_buf(out, (char*)outbuf, outpos); - bytes_out += (ulg)outpos; + out->write(outbuf, outpos); } - return OK; + + return NULL; } +