2
1
mirror of https://github.com/qpdf/qpdf.git synced 2025-02-02 11:58:25 +00:00

avoid having to include zlib.h in Pl_Flate.hh

git-svn-id: svn+q:///qpdf/trunk@882 71b93d88-0707-0410-a8cf-f5a4172ac649
This commit is contained in:
Jay Berkenbilt 2009-10-23 14:58:09 +00:00
parent b84b698a17
commit 3c84d496f0
2 changed files with 29 additions and 18 deletions

View File

@ -10,8 +10,6 @@
#include <qpdf/Pipeline.hh> #include <qpdf/Pipeline.hh>
#include <zlib.h>
class Pl_Flate: public Pipeline class Pl_Flate: public Pipeline
{ {
public: public:
@ -38,7 +36,7 @@ class Pl_Flate: public Pipeline
int out_bufsize; int out_bufsize;
action_e action; action_e action;
bool initialized; bool initialized;
z_stream zstream; void* zdata;
}; };
#endif // __PL_FLATE_HH__ #endif // __PL_FLATE_HH__

View File

@ -1,4 +1,5 @@
#include <qpdf/Pl_Flate.hh> #include <qpdf/Pl_Flate.hh>
#include <zlib.h>
#include <qpdf/QUtil.hh> #include <qpdf/QUtil.hh>
@ -10,7 +11,14 @@ Pl_Flate::Pl_Flate(char const* identifier, Pipeline* next,
initialized(false) initialized(false)
{ {
this->outbuf = new unsigned char[out_bufsize]; this->outbuf = new unsigned char[out_bufsize];
// Indirect through zdata to reach the z_stream so we don't have
// to include zlib.h in Pl_Flate.hh. This means people using
// shared library versions of qpdf don't have to have zlib
// development files available, which particularly helps in a
// Windows environment.
this->zdata = new z_stream;
z_stream& zstream = *((z_stream*) this->zdata);
zstream.zalloc = (alloc_func)0; zstream.zalloc = (alloc_func)0;
zstream.zfree = (free_func)0; zstream.zfree = (free_func)0;
zstream.opaque = (voidpf)0; zstream.opaque = (voidpf)0;
@ -27,6 +35,8 @@ Pl_Flate::~Pl_Flate()
delete [] this->outbuf; delete [] this->outbuf;
this->outbuf = 0; this->outbuf = 0;
} }
delete (z_stream*)this->zdata;
this->zdata = 0;
} }
void void
@ -44,19 +54,20 @@ Pl_Flate::write(unsigned char* data, int len)
void void
Pl_Flate::handleData(unsigned char* data, int len, int flush) Pl_Flate::handleData(unsigned char* data, int len, int flush)
{ {
this->zstream.next_in = data; z_stream& zstream = *((z_stream*) this->zdata);
this->zstream.avail_in = len; zstream.next_in = data;
zstream.avail_in = len;
if (! this->initialized) if (! this->initialized)
{ {
int err = Z_OK; int err = Z_OK;
if (this->action == a_deflate) if (this->action == a_deflate)
{ {
err = deflateInit(&this->zstream, Z_DEFAULT_COMPRESSION); err = deflateInit(&zstream, Z_DEFAULT_COMPRESSION);
} }
else else
{ {
err = inflateInit(&this->zstream); err = inflateInit(&zstream);
} }
checkError("Init", err); checkError("Init", err);
this->initialized = true; this->initialized = true;
@ -69,11 +80,11 @@ Pl_Flate::handleData(unsigned char* data, int len, int flush)
{ {
if (action == a_deflate) if (action == a_deflate)
{ {
err = deflate(&this->zstream, flush); err = deflate(&zstream, flush);
} }
else else
{ {
err = inflate(&this->zstream, flush); err = inflate(&zstream, flush);
} }
switch (err) switch (err)
{ {
@ -91,20 +102,20 @@ Pl_Flate::handleData(unsigned char* data, int len, int flush)
case Z_OK: case Z_OK:
{ {
if ((this->zstream.avail_in == 0) && if ((zstream.avail_in == 0) &&
(this->zstream.avail_out > 0)) (zstream.avail_out > 0))
{ {
// There is nothing left to read, and there was // There is nothing left to read, and there was
// sufficient buffer space to write everything we // sufficient buffer space to write everything we
// needed, so we're done for now. // needed, so we're done for now.
done = true; done = true;
} }
uLong ready = (this->out_bufsize - this->zstream.avail_out); uLong ready = (this->out_bufsize - zstream.avail_out);
if (ready > 0) if (ready > 0)
{ {
this->getNext()->write(this->outbuf, ready); this->getNext()->write(this->outbuf, ready);
this->zstream.next_out = this->outbuf; zstream.next_out = this->outbuf;
this->zstream.avail_out = this->out_bufsize; zstream.avail_out = this->out_bufsize;
} }
} }
break; break;
@ -123,17 +134,18 @@ Pl_Flate::finish()
{ {
if (this->initialized) if (this->initialized)
{ {
z_stream& zstream = *((z_stream*) this->zdata);
unsigned char buf[1]; unsigned char buf[1];
buf[0] = '\0'; buf[0] = '\0';
handleData(buf, 0, Z_FINISH); handleData(buf, 0, Z_FINISH);
int err = Z_OK; int err = Z_OK;
if (action == a_deflate) if (action == a_deflate)
{ {
err = deflateEnd(&this->zstream); err = deflateEnd(&zstream);
} }
else else
{ {
err = inflateEnd(&this->zstream); err = inflateEnd(&zstream);
} }
checkError("End", err); checkError("End", err);
} }
@ -147,15 +159,16 @@ Pl_Flate::finish()
void void
Pl_Flate::checkError(char const* prefix, int error_code) Pl_Flate::checkError(char const* prefix, int error_code)
{ {
z_stream& zstream = *((z_stream*) this->zdata);
if (error_code != Z_OK) if (error_code != Z_OK)
{ {
char const* action_str = (action == a_deflate ? "deflate" : "inflate"); char const* action_str = (action == a_deflate ? "deflate" : "inflate");
std::string msg = std::string msg =
this->identifier + ": " + action_str + ": " + prefix + ": "; this->identifier + ": " + action_str + ": " + prefix + ": ";
if (this->zstream.msg) if (zstream.msg)
{ {
msg += this->zstream.msg; msg += zstream.msg;
} }
else else
{ {