diff -u -b -B -r -N -w xpdf-0.80-decrypt/build020/xpdf/Makefile xpdf-0.80-decrypt-gzip/build020/xpdf/Makefile --- xpdf-0.80-decrypt/build020/xpdf/Makefile Fri Jun 25 19:49:24 1999 +++ xpdf-0.80-decrypt-gzip/build020/xpdf/Makefile Fri Jun 25 19:25:56 1999 @@ -52,6 +52,7 @@ $(srcdir)/PDFDoc.cc \ $(srcdir)/PSOutputDev.cc \ $(srcdir)/Stream.cc \ + $(srcdir)/unlzw.cc \ $(srcdir)/TextOutputDev.cc \ $(srcdir)/XRef.cc \ $(srcdir)/Apdf.c \ @@ -72,7 +73,7 @@ #------------------------------------------------------------------------ APDF_OBJS = Array.o Catalog.o Dict.o Error.o Gfx.o GfxFont.o \ - TextOutputDev.o AOutputDev.o XRef.o AGfx.o \ + TextOutputDev.o AOutputDev.o XRef.o AGfx.o unlzw.o \ GfxState.o Lexer.o Link.o Object.o OutputDev.o mystdio.o \ Page.o Parser.o PDFDoc.o PSOutputDev.o Stream.o \ Apdfcommon.o Apdf.o ImageOutputDev.o poly.o FontOutputDev.o \ @@ -199,6 +200,11 @@ ../../xpdf/mystdio.h ../../xpdf/../goo/gtypes.h \ ../../xpdf/../goo/GString.h ../../xpdf/Array.h ../../xpdf/Dict.h \ ../../xpdf/Stream.h ../../xpdf/rc4.h ../../xpdf/Stream-CCITT.h +unlzw.o: ../../xpdf/unlzw.cc ../../xpdf/Object.h ../../xpdf/mystdio.h \ + ../../xpdf/../goo/gtypes.h ../../xpdf/../goo/gmem.h \ + ../../xpdf/../goo/GString.h ../../xpdf/Array.h ../../xpdf/Dict.h \ + ../../xpdf/Stream.h ../../xpdf/Error.h ../../xpdf/config.h \ + ../../xpdf/rc4.h TextOutputDev.o: ../../xpdf/TextOutputDev.cc \ ../../xpdf/../goo/GString.h ../../xpdf/../goo/gmem.h \ ../../xpdf/config.h ../../xpdf/Error.h ../../xpdf/GfxState.h \ diff -u -b -B -r -N -w xpdf-0.80-decrypt/build040/goo/Makefile xpdf-0.80-decrypt-gzip/build040/goo/Makefile --- xpdf-0.80-decrypt/build040/goo/Makefile Fri Jun 25 19:41:17 1999 +++ xpdf-0.80-decrypt-gzip/build040/goo/Makefile Fri Jun 25 19:22:32 1999 @@ -64,10 +64,3 @@ $(CC) $(CFLAGS) -MM $(C_SRC) >>Makefile #----- dependences ----- -gmem.o: ../../goo/gmem.cc ../../goo/gmem.h -GString.o: ../../goo/GString.cc ../../goo/GString.h -gmempp.o: ../../goo/gmempp.cc ../../goo/gmem.h -gfile.o: ../../goo/gfile.cc ../../goo/GString.h ../../goo/gfile.h \ - ../../goo/gtypes.h -parseargs.o: ../../goo/parseargs.c ../../goo/parseargs.h \ - ../../goo/gtypes.h diff -u -b -B -r -N -w xpdf-0.80-decrypt/build040/xpdf/Makefile xpdf-0.80-decrypt-gzip/build040/xpdf/Makefile --- xpdf-0.80-decrypt/build040/xpdf/Makefile Fri Jun 25 19:47:55 1999 +++ xpdf-0.80-decrypt-gzip/build040/xpdf/Makefile Fri Jun 25 19:25:29 1999 @@ -52,6 +52,7 @@ $(srcdir)/PDFDoc.cc \ $(srcdir)/PSOutputDev.cc \ $(srcdir)/Stream.cc \ + $(srcdir)/unlzw.cc \ $(srcdir)/TextOutputDev.cc \ $(srcdir)/XRef.cc \ $(srcdir)/Apdf.c \ @@ -72,7 +73,7 @@ #------------------------------------------------------------------------ APDF_OBJS = Array.o Catalog.o Dict.o Error.o Gfx.o GfxFont.o \ - TextOutputDev.o AOutputDev.o XRef.o AGfx.o \ + TextOutputDev.o AOutputDev.o XRef.o AGfx.o unlzw.o \ GfxState.o Lexer.o Link.o Object.o OutputDev.o mystdio.o \ Page.o Parser.o PDFDoc.o PSOutputDev.o Stream.o \ Apdfcommon.o Apdf.o ImageOutputDev.o poly.o FontOutputDev.o \ @@ -199,6 +200,11 @@ ../../xpdf/mystdio.h ../../xpdf/../goo/gtypes.h \ ../../xpdf/../goo/GString.h ../../xpdf/Array.h ../../xpdf/Dict.h \ ../../xpdf/Stream.h ../../xpdf/rc4.h ../../xpdf/Stream-CCITT.h +unlzw.o: ../../xpdf/unlzw.cc ../../xpdf/Object.h ../../xpdf/mystdio.h \ + ../../xpdf/../goo/gtypes.h ../../xpdf/../goo/gmem.h \ + ../../xpdf/../goo/GString.h ../../xpdf/Array.h ../../xpdf/Dict.h \ + ../../xpdf/Stream.h ../../xpdf/Error.h ../../xpdf/config.h \ + ../../xpdf/rc4.h TextOutputDev.o: ../../xpdf/TextOutputDev.cc \ ../../xpdf/../goo/GString.h ../../xpdf/../goo/gmem.h \ ../../xpdf/config.h ../../xpdf/Error.h ../../xpdf/GfxState.h \ diff -u -b -B -r -N -w xpdf-0.80-decrypt/build060/xpdf/Makefile xpdf-0.80-decrypt-gzip/build060/xpdf/Makefile --- xpdf-0.80-decrypt/build060/xpdf/Makefile Fri Jun 25 19:49:06 1999 +++ xpdf-0.80-decrypt-gzip/build060/xpdf/Makefile Fri Jun 25 19:22:27 1999 @@ -52,6 +52,7 @@ $(srcdir)/PDFDoc.cc \ $(srcdir)/PSOutputDev.cc \ $(srcdir)/Stream.cc \ + $(srcdir)/unlzw.cc \ $(srcdir)/TextOutputDev.cc \ $(srcdir)/XRef.cc \ $(srcdir)/Apdf.c \ @@ -73,7 +74,7 @@ #------------------------------------------------------------------------ APDF_OBJS = Array.o Catalog.o Dict.o Error.o Gfx.o GfxFont.o \ - TextOutputDev.o AOutputDev.o XRef.o AGfx.o \ + TextOutputDev.o AOutputDev.o XRef.o AGfx.o unlzw.o \ GfxState.o Lexer.o Link.o Object.o OutputDev.o mystdio.o \ Page.o Parser.o PDFDoc.o PSOutputDev.o Stream.o \ Apdfcommon.o Apdf.o ImageOutputDev.o poly.o FontOutputDev.o \ @@ -200,6 +201,11 @@ ../../xpdf/mystdio.h ../../xpdf/../goo/gtypes.h \ ../../xpdf/../goo/GString.h ../../xpdf/Array.h ../../xpdf/Dict.h \ ../../xpdf/Stream.h ../../xpdf/rc4.h ../../xpdf/Stream-CCITT.h +unlzw.o: ../../xpdf/unlzw.cc ../../xpdf/Object.h ../../xpdf/mystdio.h \ + ../../xpdf/../goo/gtypes.h ../../xpdf/../goo/gmem.h \ + ../../xpdf/../goo/GString.h ../../xpdf/Array.h ../../xpdf/Dict.h \ + ../../xpdf/Stream.h ../../xpdf/Error.h ../../xpdf/config.h \ + ../../xpdf/rc4.h TextOutputDev.o: ../../xpdf/TextOutputDev.cc \ ../../xpdf/../goo/GString.h ../../xpdf/../goo/gmem.h \ ../../xpdf/config.h ../../xpdf/Error.h ../../xpdf/GfxState.h \ diff -u -b -B -r -N -w xpdf-0.80-decrypt/buildnofpu/xpdf/Makefile xpdf-0.80-decrypt-gzip/buildnofpu/xpdf/Makefile --- xpdf-0.80-decrypt/buildnofpu/xpdf/Makefile Fri Jun 25 19:49:41 1999 +++ xpdf-0.80-decrypt-gzip/buildnofpu/xpdf/Makefile Fri Jun 25 19:26:20 1999 @@ -52,6 +52,7 @@ $(srcdir)/PDFDoc.cc \ $(srcdir)/PSOutputDev.cc \ $(srcdir)/Stream.cc \ + $(srcdir)/unlzw.cc \ $(srcdir)/TextOutputDev.cc \ $(srcdir)/XRef.cc \ $(srcdir)/Apdf.c \ @@ -73,7 +74,7 @@ #------------------------------------------------------------------------ APDF_OBJS = Array.o Catalog.o Dict.o Error.o Gfx.o GfxFont.o \ - TextOutputDev.o AOutputDev.o XRef.o AGfx.o \ + TextOutputDev.o AOutputDev.o XRef.o AGfx.o unlzw.o \ GfxState.o Lexer.o Link.o Object.o OutputDev.o mystdio.o \ Page.o Parser.o PDFDoc.o PSOutputDev.o Stream.o \ Apdfcommon.o Apdf.o ImageOutputDev.o poly.o FontOutputDev.o \ @@ -200,6 +201,11 @@ ../../xpdf/mystdio.h ../../xpdf/../goo/gtypes.h \ ../../xpdf/../goo/GString.h ../../xpdf/Array.h ../../xpdf/Dict.h \ ../../xpdf/Stream.h ../../xpdf/rc4.h ../../xpdf/Stream-CCITT.h +unlzw.o: ../../xpdf/unlzw.cc ../../xpdf/Object.h ../../xpdf/mystdio.h \ + ../../xpdf/../goo/gtypes.h ../../xpdf/../goo/gmem.h \ + ../../xpdf/../goo/GString.h ../../xpdf/Array.h ../../xpdf/Dict.h \ + ../../xpdf/Stream.h ../../xpdf/Error.h ../../xpdf/config.h \ + ../../xpdf/rc4.h TextOutputDev.o: ../../xpdf/TextOutputDev.cc \ ../../xpdf/../goo/GString.h ../../xpdf/../goo/gmem.h \ ../../xpdf/config.h ../../xpdf/Error.h ../../xpdf/GfxState.h \ diff -u -b -B -r -N -w xpdf-0.80-decrypt/buildppc/xpdf/Makefile xpdf-0.80-decrypt-gzip/buildppc/xpdf/Makefile --- xpdf-0.80-decrypt/buildppc/xpdf/Makefile Fri Jun 25 19:39:49 1999 +++ xpdf-0.80-decrypt-gzip/buildppc/xpdf/Makefile Fri Jun 25 19:15:26 1999 @@ -59,6 +59,7 @@ $(srcdir)/PDFDoc.cc \ $(srcdir)/PSOutputDev.cc \ $(srcdir)/Stream.cc \ + $(srcdir)/unlzw.cc \ $(srcdir)/TextOutputDev.cc \ $(srcdir)/XRef.cc \ $(srcdir)/Apdf.c \ @@ -87,7 +88,7 @@ # /sc/c/sc link define POWERUP define USE_GFX_PIPE addsym debug=full to apdf //xpdf/Apdf.c APDF_OBJS = stub.o Array.o Catalog.o Dict.o Error.o Gfx.o GfxFont.o \ - TextOutputDev.o AOutputDev.o XRef.o apdfppcserv.o AGfx.o \ + TextOutputDev.o AOutputDev.o XRef.o apdfppcserv.o AGfx.o unlzw.o \ GfxState.o Lexer.o Link.o Object.o OutputDev.o mystdio.o \ Page.o Parser.o PDFDoc.o PSOutputDev.o Stream.o \ apdfcommon.o ImageOutputDev.o poly.o FontOutputDev.o \ @@ -218,6 +219,11 @@ ../../xpdf/mystdio.h ../../xpdf/../goo/gtypes.h \ ../../xpdf/../goo/GString.h ../../xpdf/Array.h ../../xpdf/Dict.h \ ../../xpdf/Stream.h ../../xpdf/rc4.h ../../xpdf/Stream-CCITT.h +unlzw.o: ../../xpdf/unlzw.cc ../../xpdf/Object.h ../../xpdf/mystdio.h \ + ../../xpdf/../goo/gtypes.h ../../xpdf/../goo/gmem.h \ + ../../xpdf/../goo/GString.h ../../xpdf/Array.h ../../xpdf/Dict.h \ + ../../xpdf/Stream.h ../../xpdf/Error.h ../../xpdf/config.h \ + ../../xpdf/rc4.h TextOutputDev.o: ../../xpdf/TextOutputDev.cc \ ../../xpdf/../goo/GString.h ../../xpdf/../goo/gmem.h \ ../../xpdf/config.h ../../xpdf/Error.h ../../xpdf/GfxState.h \ diff -u -b -B -r -N -w xpdf-0.80-decrypt/buildsas/xpdf/SMakefile xpdf-0.80-decrypt-gzip/buildsas/xpdf/SMakefile --- xpdf-0.80-decrypt/buildsas/xpdf/SMakefile Fri Jun 25 19:50:21 1999 +++ xpdf-0.80-decrypt-gzip/buildsas/xpdf/SMakefile Fri Jun 25 19:33:02 1999 @@ -59,6 +59,7 @@ $(srcdir)/PDFDoc.cc \ $(srcdir)/PSOutputDev.cc \ $(srcdir)/Stream.cc \ + $(srcdir)/unlzw.cc \ $(srcdir)/TextOutputDev.cc \ $(srcdir)/XRef.cc \ $(srcdir)/Apdf.c \ @@ -81,7 +82,7 @@ #------------------------------------------------------------------------ APDF_OBJS = Array.o Catalog.o Dict.o Error.o Gfx.o GfxFont.o \ - TextOutputDev.o AOutputDev.o XRef.o AGfx.o \ + TextOutputDev.o AOutputDev.o XRef.o AGfx.o unlzw.o \ GfxState.o Lexer.o Link.o Object.o OutputDev.o mystdio.o \ Page.o Parser.o PDFDoc.o PSOutputDev.o Stream.o \ Apdfcommon.o Apdf.o ImageOutputDev.o poly.o stub.o \ @@ -215,6 +216,11 @@ //xpdf/mystdio.h //goo/gtypes.h \ //goo/GString.h //xpdf/Array.h //xpdf/Dict.h \ //xpdf/Stream.h //xpdf/rc4.h //xpdf/Stream-CCITT.h +unlzw.o: //xpdf/unlzw.cc //xpdf/Object.h //xpdf/mystdio.h \ + //goo/gtypes.h //goo/gmem.h \ + //goo/GString.h //xpdf/Array.h //xpdf/Dict.h \ + //xpdf/Stream.h //xpdf/Error.h //xpdf/config.h \ + //xpdf/rc4.h TextOutputDev.o: //xpdf/TextOutputDev.cc \ //goo/GString.h //goo/gmem.h \ //xpdf/config.h //xpdf/Error.h //xpdf/GfxState.h \ diff -u -b -B -r -N -w xpdf-0.80-decrypt/xpdf/Stream.cc xpdf-0.80-decrypt-gzip/xpdf/Stream.cc --- xpdf-0.80-decrypt/xpdf/Stream.cc Fri Jun 25 18:43:44 1999 +++ xpdf-0.80-decrypt-gzip/xpdf/Stream.cc Fri Jun 25 19:07:41 1999 @@ -682,6 +682,113 @@ // LZWStream //------------------------------------------------------------------------ +static const int pipebufsize=100000; + +LZWStream::Pipe::Pipe() : wsize(pipebufsize),rsize(0) { + unsigned char* p=new unsigned char [sizeof(chunk)+pipebufsize]; + chunks=wchunk=(chunk*)p; + wp=rp=p+sizeof(chunk); + chunks->next=NULL; +} + +LZWStream::Pipe::~Pipe() { + chunk* p=chunks; + while(p) { + chunk* q=p->next; + delete [] (unsigned char*)p; + p=q; + } +} + +void LZWStream::Pipe::enlarge() { + if(chunks==wchunk) + rsize=wp-rp; + unsigned char* q=new unsigned char [sizeof(chunk)+pipebufsize]; + wchunk->next=(chunk*)q; + wchunk=wchunk->next; + wp=q+sizeof(chunk); + chunks->next=NULL; + wsize=pipebufsize; +} + +void LZWStream::Pipe::write(const void* p,size_t sz) { + while(sz>=wsize) { + memcpy(wp,p,wsize); + sz-=wsize; + wp+=wsize; + p=(const void*)((const char*)p+wsize); + enlarge(); + } + if(sz) { + memcpy(wp,p,sz); + wsize-=sz; + wp+=sz; + } +} + +bool LZWStream::Pipe::next_chunk() { + if(chunks==wchunk) { + if(rpnext; + delete [] (unsigned char*)chunks; + chunks=q; + if(q) { + rp=(unsigned char*)(q+1); + rsize=pipebufsize; + if(q==wchunk) + rsize-=wsize; + return true; + } else { + rsize=0; + rp=NULL; + return false; + } + } else + return false; +} + +int LZWStream::Pipe::read(void* p,size_t sz) { + int sz2=0; + while(sz>rsize) { + if(rsize==0 && !next_chunk()) + return sz2; + memcpy(p,rp,rsize); + sz-=rsize; + sz2+=rsize; + rp+=rsize; + rsize=0; + p=(void*)((char*)p+rsize); + } + if(sz) { + memcpy(p,rp,sz); + rsize-=sz; + sz2+=sz; + rp+=sz; + } + return sz2; +} + +void LZWStream::Pipe::reset() { + chunk* p=chunks->next; + while(p) { + chunk* q=p->next; + delete (unsigned char*)p; + p=q; + } + chunks->next=NULL; + wchunk=chunks; + rp=wp=(unsigned char*)(chunks+1); + rsize=0; + wsize=pipebufsize; +} + + LZWStream::LZWStream(Stream *str1, int predictor1, int columns1, int colors1, int bits1, int early1) { str = str1; @@ -692,82 +799,46 @@ nBits = bits1; } early = early1; - zPipe = NULL; bufPtr = bufEnd = buf; } LZWStream::~LZWStream() { - if (zPipe) { -#ifdef HAVE_POPEN - pclose(zPipe); -#else - myfclose(zPipe); -#endif - zPipe = NULL; - unlink(zName); - } delete str; } int LZWStream::getChar() { - return (bufPtr >= bufEnd && !fillBuf()) ? EOF : (*bufPtr++ & 0xff); + return pipe.get(); } int LZWStream::lookChar() { - return (bufPtr >= bufEnd && !fillBuf()) ? EOF : (*bufPtr & 0xff); + return pipe.lookc(); } -void LZWStream::reset() { - FILE *f; +inline int LZWStream::getCode() { + int c; + int code; - str->reset(); - bufPtr = bufEnd = buf; - if (zPipe) { -#ifdef HAVE_POPEN - pclose(zPipe); -#else - myfclose(zPipe); -#endif - zPipe = NULL; - unlink(zName); - } - strcpy(zCmd, uncompressCmd); - strcat(zCmd, " "); - zName = zCmd + strlen(zCmd); - tmpnam(zName); - strcat(zName, ".Z"); - if (!(f = fopen(zName, "wb"))) { - error(getPos(), "Couldn't open temporary file '%s'", zName); - return; - } - dumpFile(f); - fclose(f); -#ifdef HAVE_POPEN - if (!(zPipe = popen(zCmd, "r"))) { - error(getPos(), "Couldn't popen '%s'", zCmd); - unlink(zName); - return; - } -#else -#ifdef VMS - if (!system(zCmd)) { -#else - if (system(zCmd)) { -#endif - error(getPos(), "Couldn't execute '%s'", zCmd); - unlink(zName); - return; - } - zName[strlen(zName) - 2] = '\0'; - if (!(zPipe = myfopen(zName, "rb"))) { - error(getPos(), "Couldn't open uncompress file '%s'", zName); - unlink(zName); - return; + while (inputBits < inCodeBits) { + if ((c = str->getChar()) == EOF) + return EOF; + inputBuf = (inputBuf << 8) | (c & 0xff); + inputBits += 8; } -#endif + code = (inputBuf >> (inputBits - inCodeBits)) & ((1 << inCodeBits) - 1); + inputBits -= inCodeBits; + return code; } -void LZWStream::dumpFile(FILE *f) { +void LZWStream::reset() { + + str->reset(); + pipe.reset(); + + Pipe tmppipe; + + // fill tmppipe with the contents of the stream converted to + // 'compress' format (without header). + int outCodeBits; // size of output code int outBits; // max output code int outBuf[8]; // output buffer @@ -779,13 +850,6 @@ GBool first; // indicates first code word after clear int i, j; - // magic number - fputc(0x1f, f); - fputc(0x9d, f); - - // max code length, block mode flag - fputc(0x8c, f); - // init input side inCodeBits = 9; inputBuf = 0; @@ -864,7 +928,7 @@ outData = outData | (outBuf[j++] << outBits); outBits += outCodeBits; } - fputc(outData & 0xff, f); + tmppipe.put(char(outData)); outData >>= 8; outBits -= 8; } @@ -886,40 +950,10 @@ clear = gFalse; } } while (!eof); -} - -int LZWStream::getCode() { - int c; - int code; - - while (inputBits < inCodeBits) { - if ((c = str->getChar()) == EOF) - return EOF; - inputBuf = (inputBuf << 8) | (c & 0xff); - inputBits += 8; - } - code = (inputBuf >> (inputBits - inCodeBits)) & ((1 << inCodeBits) - 1); - inputBits -= inCodeBits; - return code; -} -GBool LZWStream::fillBuf() { - int n; + // uncompress tmppipe to pipe - if (!zPipe) - return gFalse; - if ((n = myfread(buf, 1, 256, zPipe)) < 256) { -#ifdef HAVE_POPEN - pclose(zPipe); -#else - myfclose(zPipe); -#endif - zPipe = NULL; - unlink(zName); - } - bufPtr = buf; - bufEnd = buf + n; - return n > 0; + unlzw(tmppipe, pipe); } GString *LZWStream::getPSFilter(char *indent) { diff -u -b -B -r -N -w xpdf-0.80-decrypt/xpdf/Stream.h xpdf-0.80-decrypt-gzip/xpdf/Stream.h --- xpdf-0.80-decrypt/xpdf/Stream.h Fri Jun 25 18:46:28 1999 +++ xpdf-0.80-decrypt-gzip/xpdf/Stream.h Fri Jun 25 19:08:58 1999 @@ -266,6 +266,32 @@ //------------------------------------------------------------------------ class LZWStream: public Stream { + class Pipe { + struct chunk { + chunk* next; + }; + public: + Pipe(); + ~Pipe(); + void write(const void*,size_t); + void put(char c) { if(wsize==0) enlarge(); *wp++=c; --wsize; } + int read(void*,size_t); + int get() { return rsize==0 && !next_chunk()?EOF:(--rsize,*rp++); } + int lookc() { return rsize==0 && !next_chunk()?EOF:*rp; } + void reset(); + private: + Pipe(const Pipe&); + Pipe& operator = (const Pipe&); + void enlarge(); + bool next_chunk(); + chunk* chunks; + chunk* wchunk; + unsigned char* wp; + int wsize; + unsigned char* rp; + int rsize; + }; + public: LZWStream(Stream *str1, int predictor1, int columns1, int colors1, @@ -286,9 +312,7 @@ Stream *str; // stream int early; // early parameter - char zCmd[256]; // uncompress command - myFILE *zPipe; // uncompress pipe - char *zName; // .Z file name (in zCmd) + Pipe pipe; int inputBuf; // input buffer int inputBits; // number of bits in input buffer int inCodeBits; // size of input code @@ -296,9 +320,8 @@ char *bufPtr; // next char to read char *bufEnd; // end of buffer - void dumpFile(FILE *f); int getCode(); - GBool fillBuf(); + void unlzw(Pipe&,Pipe&); }; //------------------------------------------------------------------------ diff -u -b -B -r -N -w xpdf-0.80-decrypt/xpdf/unlzw.cc xpdf-0.80-decrypt-gzip/xpdf/unlzw.cc --- xpdf-0.80-decrypt/xpdf/unlzw.cc Thu May 20 20:56:27 1999 +++ xpdf-0.80-decrypt-gzip/xpdf/unlzw.cc Fri Jun 25 18:58:10 1999 @@ -7,23 +7,18 @@ * to accommodate in-memory decompression. */ -#ifdef RCSID -static char rcsid[] = "$Id: unlzw.c,v 0.15 1993/06/10 13:28:35 jloup Exp $"; -#endif - -#include +/* + * Modified for integration into Xpdf by E. Lesueur. + */ -#include "tailor.h" +#include +#include +#include "Object.h" +#include "Stream.h" +#include "Error.h" -#ifdef HAVE_UNISTD_H -# include -#endif -#ifndef NO_FCNTL_H -# include -#endif - -#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 +26,48 @@ 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; +static unsigned char* tab_suffix; +static unsigned char* inbuf; +static unsigned char* outbuf; +static unsigned short* d_buf; + +class Init { + public: + Init() { + tab_prefix = new unsigned short [1<<16]; + tab_suffix = new unsigned char [1<<16]; + inbuf = new unsigned char [INBUFSIZ+INBUF_EXTRA]; + outbuf = new unsigned char [OUTBUFSIZ+OUTBUF_EXTRA]; + d_buf = new unsigned short [DIST_BUFSIZE]; + } + + ~Init() { + delete [] tab_suffix; + delete [] tab_prefix; + delete [] inbuf; + delete [] outbuf; + delete [] d_buf; + } + + static Init init; +}; + +Init Init::init; + + #define MAXCODE(n) (1L << (n)) #ifndef REGISTERS @@ -178,7 +215,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,8 +225,7 @@ * 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 */ +void LZWStream::unlzw(Pipe& in,Pipe& out) { REG2 char_type *stackp; REG3 code_int code; @@ -200,7 +235,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 +247,19 @@ 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; + if ((rsize = insize = in.read(inbuf, INBUFSIZ)) == EOF) { + error(getPos(), "unexpected end od file."); + return; } - rsize = insize; + maxcode = MAXCODE(n_bits = INIT_BITS)-1; bitmask = (1<= 256) error("corrupt input."); + if (code >= 256) { + error(getPos(),"corrupt input."); + return; + } outbuf[outpos++] = (char_type)(finchar = (int)(oldcode=code)); continue; } @@ -315,11 +343,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."); + error(getPos(),"corrupt input." ); } *--stackp = (char_type)finchar; code = oldcode; @@ -346,8 +372,7 @@ } if (outpos >= OUTBUFSIZ) { if (!test) { - write_buf(out, (char*)outbuf, outpos); - bytes_out += (ulg)outpos; + out.write(outbuf, outpos); } outpos = 0; } @@ -370,8 +395,7 @@ } while (rsize != 0); if (!test && outpos > 0) { - write_buf(out, (char*)outbuf, outpos); - bytes_out += (ulg)outpos; + out.write(outbuf, outpos); } - return OK; } +