[poppler] [PATCH] reimplement FlateStream using zlib

Jeff Muizelaar jeff at infidigm.net
Sat Apr 23 22:31:25 PDT 2005


Coments and any testing appreciated.

-Jeff


diff -X /home/jeff/rsrc/autoignore -urN cvs/configure.ac flate/configure.ac
--- cvs/configure.ac	2005-04-23 22:16:20.000000000 -0400
+++ flate/configure.ac	2005-04-24 00:48:21.000000000 -0400
@@ -82,6 +82,34 @@
   AC_DEFINE(HAVE_FSEEK64)
 fi
 
+dnl Test for zlib
+AC_ARG_ENABLE(zlib,
+	      AC_HELP_STRING([--disable-zlib],
+	                     [Don't build against zlib.]),
+              enable_zlib=$enableval,
+              enable_zlib="try")
+if test x$enable_zlib = xtry; then
+  AC_CHECK_LIB([z], [inflate],,
+	       AC_MSG_ERROR("*** zlib library not found ***"))
+  AC_CHECK_HEADERS([zlib.h],,
+		   AC_MSG_ERROR("*** zlib headers not found ***"))
+elif test x$enable_zlib = xyes; then
+  AC_CHECK_LIB([zlib], [inflate],
+               [enable_zlib="yes"],
+	       [enable_zlib="no"])
+  AC_CHECK_HEADERS([zlib.h],,
+		   [enable_zlib="no"])
+fi
+
+if test x$enable_zlib = xyes; then
+  ZLIB_LIBS="-lz"
+  AC_SUBST(ZLIB_LIBS)
+  AC_DEFINE(ENABLE_ZLIB)
+fi
+
+AM_CONDITIONAL(BUILD_ZLIB, test x$enable_zlib = xyes)
+AH_TEMPLATE([ENABLE_ZLIB],
+	    [Use zlib instead of builtin zlib decoder.])
 
 dnl Test for libjpeg
 AC_ARG_ENABLE(libjpeg,
@@ -314,3 +342,4 @@
 echo "  qt wrapper:    $enable_poppler_qt"
 echo "  glib wrapper:  $enable_poppler_glib"
 echo "  use libjpeg:   $enable_libjpeg"
+echo "  use zlib:      $enable_zlib"
diff -X /home/jeff/rsrc/autoignore -urN cvs/poppler/FlateStream.cc flate/poppler/FlateStream.cc
--- cvs/poppler/FlateStream.cc	1969-12-31 19:00:00.000000000 -0500
+++ flate/poppler/FlateStream.cc	2005-04-24 00:28:30.000000000 -0400
@@ -0,0 +1,115 @@
+//========================================================================
+//
+// FlateStream.cc
+//
+// Copyright 1996-2003 Glyph & Cog, LLC
+//
+//========================================================================
+#include "FlateStream.h"
+
+FlateStream::FlateStream(Stream *strA, int predictor, int columns, int colors, int bits) :
+  FilterStream(strA)
+{
+  if (predictor != 1) {
+    pred = new StreamPredictor(this, predictor, columns, colors, bits);
+  } else {
+    pred = NULL;
+  }
+  out_pos = 0;
+  eof = false;
+  d_stream.avail_out = sizeof(out_buf);
+  d_stream.next_out = out_buf;
+  memset(&d_stream, 0, sizeof(d_stream));
+}
+
+FlateStream::~FlateStream() {
+  inflateEnd(&d_stream);
+  delete str;
+}
+
+void FlateStream::reset() {
+  //FIXME: what are the semantics of reset?
+  //i.e. how much intialization has to happen in the constructor?
+  str->reset();
+  d_stream.avail_out = sizeof(out_buf);
+  d_stream.next_out = out_buf;
+  memset(&d_stream, 0, sizeof(d_stream));
+  inflateInit(&d_stream);
+  d_stream.avail_in = 0;
+  status = Z_OK;
+  out_pos = 0;
+  out_buf_len = 0;
+}
+
+int FlateStream::getRawChar() {
+  int ret;
+  
+  if (eof)
+    return EOF;
+  
+  fill_buffer();
+  ret = out_buf[out_pos++];
+  
+  if (out_pos >= out_buf_len) {
+    if (status == Z_STREAM_END)
+      eof = true;
+    d_stream.avail_out = sizeof(out_buf);
+    d_stream.next_out = out_buf;
+    out_pos = 0;
+  }
+  
+  return ret;
+}
+
+int FlateStream::getChar() {
+  if (pred)
+    return pred->getChar();
+  else
+    return getRawChar();
+}
+
+int FlateStream::lookChar() {
+  if (pred)
+    return pred->lookChar();
+  
+  if (eof)
+    return EOF;
+
+  if (out_pos == 0)
+    fill_buffer();
+
+  return out_buf[out_pos];
+}
+
+void FlateStream::fill_buffer() {
+  /* buffer is empty so we need to fill it */
+  if (d_stream.avail_in == 0) {
+    int c;
+    /* read from the source stream */
+    while (d_stream.avail_in < sizeof(in_buf) && (c = str->getChar()) != EOF) {
+      in_buf[d_stream.avail_in++] = c;
+    }
+    d_stream.next_in = in_buf;
+  }
+  while (d_stream.avail_out && d_stream.avail_in && (status == Z_OK || status == Z_BUF_ERROR)) {
+    status = inflate(&d_stream, Z_SYNC_FLUSH);
+  }
+  out_buf_len = sizeof(out_buf) - d_stream.avail_out;
+}
+
+GooString *FlateStream::getPSFilter(int psLevel, char *indent) {
+  GooString *s;
+
+  if (psLevel < 3 || pred) {
+    return NULL;
+  }
+  if (!(s = str->getPSFilter(psLevel, indent))) {
+    return NULL;
+  }
+  s->append(indent)->append("<< >> /FlateDecode filter\n");
+  return s;
+}
+
+GBool FlateStream::isBinary(GBool last) {
+  return str->isBinary(gTrue);
+}
diff -X /home/jeff/rsrc/autoignore -urN cvs/poppler/FlateStream.h flate/poppler/FlateStream.h
--- cvs/poppler/FlateStream.h	1969-12-31 19:00:00.000000000 -0500
+++ flate/poppler/FlateStream.h	2005-04-24 00:23:15.000000000 -0400
@@ -0,0 +1,69 @@
+//========================================================================
+//
+// FlateStream.h
+//
+// Copyright 1996-2003 Glyph & Cog, LLC
+//
+//========================================================================
+
+#ifndef FLATESTREAM_H
+#define FLATESTREAM_H
+#include <config.h>
+
+#ifdef USE_GCC_PRAGMAS
+#pragma interface
+#endif
+
+
+#ifdef USE_GCC_PRAGMAS
+#pragma implementation
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stddef.h>
+#ifndef WIN32
+#include <unistd.h>
+#endif
+#include <string.h>
+#include <ctype.h>
+#include "goo/gmem.h"
+#include "goo/gfile.h"
+#include "poppler-config.h"
+#include "Error.h"
+#include "Object.h"
+#ifndef NO_DECRYPTION
+#include "Decrypt.h"
+#endif
+#include "Stream.h"
+
+extern "C" {
+#include <zlib.h>
+}
+
+class FlateStream: public FilterStream {
+public:
+
+  FlateStream(Stream *strA, int predictor, int columns, int colors, int bits);
+  virtual ~FlateStream();
+  virtual StreamKind getKind() { return strFlate; }
+  virtual void reset();
+  virtual int getChar();
+  virtual int lookChar();
+  virtual int getRawChar();
+  virtual GooString *getPSFilter(int psLevel, char *indent);
+  virtual GBool isBinary(GBool last = gTrue);
+
+private:
+  void fill_buffer(void);
+  z_stream d_stream;
+  StreamPredictor *pred;
+  int status;
+  bool eof;
+  unsigned char in_buf[4096];
+  unsigned char out_buf[4096];
+  int out_pos;
+  int out_buf_len;
+};
+
+#endif
diff -X /home/jeff/rsrc/autoignore -urN cvs/poppler/Makefile.am flate/poppler/Makefile.am
--- cvs/poppler/Makefile.am	2005-04-23 22:16:20.000000000 -0400
+++ flate/poppler/Makefile.am	2005-04-23 23:35:44.000000000 -0400
@@ -49,6 +49,17 @@
 
 endif
 
+if BUILD_ZLIB
+
+zlib_sources =					\
+	FlateStream.h				\
+	FlateStream.cc
+
+zlib_libs = 					\
+	$(ZLIB_LIBS)
+
+endif
+
 INCLUDES =					\
 	-I$(top_srcdir)				\
 	$(splash_includes)			\
@@ -62,7 +73,8 @@
 	$(top_builddir)/fofi/libfofi.la		\
 	$(splash_libs)				\
 	$(cairo_libs)				\
-	$(libjpeg_libs)
+	$(libjpeg_libs)				\
+	$(zlib_libs)
 
 poppler_includedir = $(includedir)/poppler
 poppler_include_HEADERS =	\
@@ -119,6 +131,7 @@
 	$(splash_sources)	\
 	$(cairo_sources)	\
 	$(libjpeg_sources)	\
+	$(zlib_sources)		\
 	Annot.cc		\
 	Array.cc 		\
 	BuiltinFont.cc		\
@@ -156,4 +169,4 @@
 	PSOutputDev.cc		\
 	TextOutputDev.cc	\
 	PageLabelInfo.h		\
-	PageLabelInfo.cc
+	PageLabelInfo.cc	
diff -X /home/jeff/rsrc/autoignore -urN cvs/poppler/Stream.cc flate/poppler/Stream.cc
--- cvs/poppler/Stream.cc	2005-04-23 22:16:20.000000000 -0400
+++ flate/poppler/Stream.cc	2005-04-24 01:27:50.000000000 -0400
@@ -37,6 +37,10 @@
 #include "DCTStream.h"
 #endif
 
+#ifdef ENABLE_ZLIB
+#include "FlateStream.h"
+#endif
+
 #ifdef __DJGPP__
 static GBool setDJSYSFLAGS = gFalse;
 #endif
@@ -3185,6 +3189,7 @@
 
 #endif
 
+#ifndef ENABLE_ZLIB
 //------------------------------------------------------------------------
 // FlateStream
 //------------------------------------------------------------------------
@@ -3715,6 +3720,7 @@
   codeSize -= bits;
   return c;
 }
+#endif
 
 //------------------------------------------------------------------------
 // EOFStream
diff -X /home/jeff/rsrc/autoignore -urN cvs/poppler/Stream.h flate/poppler/Stream.h
--- cvs/poppler/Stream.h	2005-04-23 22:16:20.000000000 -0400
+++ flate/poppler/Stream.h	2005-04-24 01:25:20.000000000 -0400
@@ -635,6 +635,7 @@
 };
 #endif
 
+#ifndef ENABLE_ZLIB
 //------------------------------------------------------------------------
 // FlateStream
 //------------------------------------------------------------------------
@@ -709,6 +710,7 @@
   int getHuffmanCodeWord(FlateHuffmanTab *tab);
   int getCodeWord(int bits);
 };
+#endif
 
 //------------------------------------------------------------------------
 // EOFStream


More information about the poppler mailing list