2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/Pl_Flate.hh>
|
2009-10-23 14:58:09 +00:00
|
|
|
#include <zlib.h>
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
#include <qpdf/QUtil.hh>
|
|
|
|
|
|
|
|
Pl_Flate::Pl_Flate(char const* identifier, Pipeline* next,
|
|
|
|
action_e action, int out_bufsize) :
|
|
|
|
Pipeline(identifier, next),
|
|
|
|
out_bufsize(out_bufsize),
|
|
|
|
action(action),
|
|
|
|
initialized(false)
|
|
|
|
{
|
|
|
|
this->outbuf = new unsigned char[out_bufsize];
|
2009-10-23 14:58:09 +00:00
|
|
|
// 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;
|
|
|
|
|
2013-02-24 02:46:21 +00:00
|
|
|
z_stream& zstream = *(static_cast<z_stream*>(this->zdata));
|
|
|
|
zstream.zalloc = 0;
|
|
|
|
zstream.zfree = 0;
|
|
|
|
zstream.opaque = 0;
|
2008-04-29 12:55:25 +00:00
|
|
|
zstream.next_in = 0;
|
|
|
|
zstream.avail_in = 0;
|
|
|
|
zstream.next_out = this->outbuf;
|
|
|
|
zstream.avail_out = out_bufsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
Pl_Flate::~Pl_Flate()
|
|
|
|
{
|
|
|
|
if (this->outbuf)
|
|
|
|
{
|
|
|
|
delete [] this->outbuf;
|
|
|
|
this->outbuf = 0;
|
|
|
|
}
|
2013-02-24 02:46:21 +00:00
|
|
|
delete static_cast<z_stream*>(this->zdata);
|
2009-10-23 14:58:09 +00:00
|
|
|
this->zdata = 0;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-06-20 15:20:57 +00:00
|
|
|
Pl_Flate::write(unsigned char* data, size_t len)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
if (this->outbuf == 0)
|
|
|
|
{
|
2009-09-26 18:36:04 +00:00
|
|
|
throw std::logic_error(
|
2008-04-29 12:55:25 +00:00
|
|
|
this->identifier +
|
|
|
|
": Pl_Flate: write() called after finish() called");
|
|
|
|
}
|
2012-06-20 15:20:57 +00:00
|
|
|
|
|
|
|
// Write in chunks in case len is too big to fit in an int.
|
|
|
|
// Assume int is at least 32 bits.
|
|
|
|
static size_t const max_bytes = 1 << 30;
|
|
|
|
size_t bytes_left = len;
|
|
|
|
unsigned char* buf = data;
|
|
|
|
while (bytes_left > 0)
|
|
|
|
{
|
|
|
|
size_t bytes = (bytes_left >= max_bytes ? max_bytes : bytes_left);
|
2013-02-24 02:46:21 +00:00
|
|
|
handleData(buf, bytes, Z_NO_FLUSH);
|
2012-06-20 15:20:57 +00:00
|
|
|
bytes_left -= bytes;
|
|
|
|
buf += bytes;
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Pl_Flate::handleData(unsigned char* data, int len, int flush)
|
|
|
|
{
|
2013-02-24 02:46:21 +00:00
|
|
|
z_stream& zstream = *(static_cast<z_stream*>(this->zdata));
|
2009-10-23 14:58:09 +00:00
|
|
|
zstream.next_in = data;
|
|
|
|
zstream.avail_in = len;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
if (! this->initialized)
|
|
|
|
{
|
|
|
|
int err = Z_OK;
|
2013-02-24 02:46:21 +00:00
|
|
|
|
|
|
|
// deflateInit and inflateInit are macros that use old-style
|
|
|
|
// casts.
|
|
|
|
#ifdef __GNUC__
|
2013-03-27 21:36:28 +00:00
|
|
|
# if ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406
|
2013-02-24 02:46:21 +00:00
|
|
|
# pragma GCC diagnostic push
|
|
|
|
# pragma GCC diagnostic ignored "-Wold-style-cast"
|
2013-03-27 21:36:28 +00:00
|
|
|
# endif
|
2013-02-24 02:46:21 +00:00
|
|
|
#endif
|
2008-04-29 12:55:25 +00:00
|
|
|
if (this->action == a_deflate)
|
|
|
|
{
|
2009-10-23 14:58:09 +00:00
|
|
|
err = deflateInit(&zstream, Z_DEFAULT_COMPRESSION);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-10-23 14:58:09 +00:00
|
|
|
err = inflateInit(&zstream);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2013-02-24 02:46:21 +00:00
|
|
|
#ifdef __GNUC__
|
2013-03-27 21:36:28 +00:00
|
|
|
# if ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406
|
2013-02-24 02:46:21 +00:00
|
|
|
# pragma GCC diagnostic pop
|
2013-03-27 21:36:28 +00:00
|
|
|
# endif
|
2013-02-24 02:46:21 +00:00
|
|
|
#endif
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
checkError("Init", err);
|
|
|
|
this->initialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int err = Z_OK;
|
|
|
|
|
|
|
|
bool done = false;
|
|
|
|
while (! done)
|
|
|
|
{
|
|
|
|
if (action == a_deflate)
|
|
|
|
{
|
2009-10-23 14:58:09 +00:00
|
|
|
err = deflate(&zstream, flush);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-10-23 14:58:09 +00:00
|
|
|
err = inflate(&zstream, flush);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
switch (err)
|
|
|
|
{
|
|
|
|
case Z_BUF_ERROR:
|
|
|
|
// Probably shouldn't be able to happen, but possible as a
|
|
|
|
// boundary condition: if the last call to inflate exactly
|
|
|
|
// filled the output buffer, it's possible that the next
|
|
|
|
// call to inflate could have nothing to do.
|
|
|
|
done = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Z_STREAM_END:
|
|
|
|
done = true;
|
|
|
|
// fall through
|
|
|
|
|
|
|
|
case Z_OK:
|
|
|
|
{
|
2009-10-23 14:58:09 +00:00
|
|
|
if ((zstream.avail_in == 0) &&
|
|
|
|
(zstream.avail_out > 0))
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
// There is nothing left to read, and there was
|
|
|
|
// sufficient buffer space to write everything we
|
|
|
|
// needed, so we're done for now.
|
|
|
|
done = true;
|
|
|
|
}
|
2009-10-23 14:58:09 +00:00
|
|
|
uLong ready = (this->out_bufsize - zstream.avail_out);
|
2008-04-29 12:55:25 +00:00
|
|
|
if (ready > 0)
|
|
|
|
{
|
|
|
|
this->getNext()->write(this->outbuf, ready);
|
2009-10-23 14:58:09 +00:00
|
|
|
zstream.next_out = this->outbuf;
|
|
|
|
zstream.avail_out = this->out_bufsize;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
this->checkError("data", err);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Pl_Flate::finish()
|
|
|
|
{
|
2017-07-29 16:07:19 +00:00
|
|
|
try
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2017-07-29 16:07:19 +00:00
|
|
|
if (this->outbuf)
|
|
|
|
{
|
|
|
|
if (this->initialized)
|
|
|
|
{
|
|
|
|
z_stream& zstream = *(static_cast<z_stream*>(this->zdata));
|
|
|
|
unsigned char buf[1];
|
|
|
|
buf[0] = '\0';
|
|
|
|
handleData(buf, 0, Z_FINISH);
|
|
|
|
int err = Z_OK;
|
|
|
|
if (action == a_deflate)
|
|
|
|
{
|
|
|
|
err = deflateEnd(&zstream);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
err = inflateEnd(&zstream);
|
|
|
|
}
|
|
|
|
checkError("End", err);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete [] this->outbuf;
|
|
|
|
this->outbuf = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (std::exception& e)
|
|
|
|
{
|
|
|
|
this->getNext()->finish();
|
|
|
|
throw e;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
this->getNext()->finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Pl_Flate::checkError(char const* prefix, int error_code)
|
|
|
|
{
|
2013-02-24 02:46:21 +00:00
|
|
|
z_stream& zstream = *(static_cast<z_stream*>(this->zdata));
|
2008-04-29 12:55:25 +00:00
|
|
|
if (error_code != Z_OK)
|
|
|
|
{
|
|
|
|
char const* action_str = (action == a_deflate ? "deflate" : "inflate");
|
|
|
|
std::string msg =
|
|
|
|
this->identifier + ": " + action_str + ": " + prefix + ": ";
|
|
|
|
|
2009-10-23 14:58:09 +00:00
|
|
|
if (zstream.msg)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2009-10-23 14:58:09 +00:00
|
|
|
msg += zstream.msg;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
switch (error_code)
|
|
|
|
{
|
|
|
|
case Z_ERRNO:
|
|
|
|
msg += "zlib system error";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Z_STREAM_ERROR:
|
|
|
|
msg += "zlib stream error";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Z_DATA_ERROR:
|
|
|
|
msg += "zlib data error";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Z_MEM_ERROR:
|
|
|
|
msg += "zlib memory error";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Z_BUF_ERROR:
|
|
|
|
msg += "zlib buffer error";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Z_VERSION_ERROR:
|
|
|
|
msg += "zlib version error";
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
msg += std::string("zlib unknown error (") +
|
|
|
|
QUtil::int_to_string(error_code) + ")";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-26 18:36:04 +00:00
|
|
|
throw std::runtime_error(msg);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|