2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/Pl_Flate.hh>
|
2009-10-23 14:58:09 +00:00
|
|
|
#include <zlib.h>
|
2018-03-03 16:35:01 +00:00
|
|
|
#include <string.h>
|
2019-06-21 04:01:36 +00:00
|
|
|
#include <limits.h>
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
#include <qpdf/QUtil.hh>
|
2019-06-21 04:01:36 +00:00
|
|
|
#include <qpdf/QIntC.hh>
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2019-08-23 23:54:08 +00:00
|
|
|
int Pl_Flate::compression_level = Z_DEFAULT_COMPRESSION;
|
|
|
|
|
2019-06-22 01:32:47 +00:00
|
|
|
Pl_Flate::Members::Members(size_t out_bufsize,
|
|
|
|
action_e action) :
|
|
|
|
out_bufsize(out_bufsize),
|
2008-04-29 12:55:25 +00:00
|
|
|
action(action),
|
2019-06-22 01:32:47 +00:00
|
|
|
initialized(false),
|
|
|
|
zdata(0)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2019-06-22 18:24:49 +00:00
|
|
|
this->outbuf = PointerHolder<unsigned char>(
|
|
|
|
true, 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;
|
|
|
|
|
2019-06-21 04:01:36 +00:00
|
|
|
if (out_bufsize > UINT_MAX)
|
|
|
|
{
|
|
|
|
throw std::runtime_error(
|
|
|
|
"Pl_Flate: zlib doesn't support buffer"
|
|
|
|
" sizes larger than unsigned int");
|
|
|
|
}
|
|
|
|
|
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;
|
2022-02-04 15:10:19 +00:00
|
|
|
zstream.next_out = this->outbuf.get();
|
2019-06-21 04:01:36 +00:00
|
|
|
zstream.avail_out = QIntC::to_uint(out_bufsize);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
2019-06-22 01:32:47 +00:00
|
|
|
Pl_Flate::Members::~Members()
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2017-08-26 02:26:53 +00:00
|
|
|
if (this->initialized)
|
|
|
|
{
|
|
|
|
z_stream& zstream = *(static_cast<z_stream*>(this->zdata));
|
|
|
|
if (action == a_deflate)
|
|
|
|
{
|
|
|
|
deflateEnd(&zstream);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
inflateEnd(&zstream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-06-22 01:32:47 +00:00
|
|
|
Pl_Flate::Pl_Flate(char const* identifier, Pipeline* next,
|
|
|
|
action_e action, unsigned int out_bufsize_int) :
|
|
|
|
Pipeline(identifier, next),
|
|
|
|
m(new Members(QIntC::to_size(out_bufsize_int), action))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Pl_Flate::~Pl_Flate()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-11-02 21:54:10 +00:00
|
|
|
void
|
|
|
|
Pl_Flate::setWarnCallback(std::function<void(char const*, int)> callback)
|
|
|
|
{
|
|
|
|
this->m->callback = callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Pl_Flate::warn(char const* msg, int code)
|
|
|
|
{
|
|
|
|
if (this->m->callback != nullptr)
|
|
|
|
{
|
|
|
|
this->m->callback(msg, code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2022-02-04 15:10:19 +00:00
|
|
|
if (this->m->outbuf.get() == 0)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
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);
|
2018-03-03 16:35:01 +00:00
|
|
|
handleData(buf, bytes,
|
2019-06-22 01:32:47 +00:00
|
|
|
(this->m->action == a_inflate ? Z_SYNC_FLUSH : 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
|
2019-06-21 04:01:36 +00:00
|
|
|
Pl_Flate::handleData(unsigned char* data, size_t len, int flush)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2019-06-21 04:01:36 +00:00
|
|
|
if (len > UINT_MAX)
|
|
|
|
{
|
|
|
|
throw std::runtime_error(
|
|
|
|
"Pl_Flate: zlib doesn't support data"
|
|
|
|
" blocks larger than int");
|
|
|
|
}
|
2019-06-22 01:32:47 +00:00
|
|
|
z_stream& zstream = *(static_cast<z_stream*>(this->m->zdata));
|
2009-10-23 14:58:09 +00:00
|
|
|
zstream.next_in = data;
|
2019-06-21 04:01:36 +00:00
|
|
|
zstream.avail_in = QIntC::to_uint(len);
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2019-06-22 01:32:47 +00:00
|
|
|
if (! this->m->initialized)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
int err = Z_OK;
|
2013-02-24 02:46:21 +00:00
|
|
|
|
|
|
|
// deflateInit and inflateInit are macros that use old-style
|
|
|
|
// casts.
|
2017-08-22 11:20:55 +00:00
|
|
|
#if ((defined(__GNUC__) && ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406) || \
|
|
|
|
defined(__clang__))
|
2013-02-24 02:46:21 +00:00
|
|
|
# pragma GCC diagnostic push
|
|
|
|
# pragma GCC diagnostic ignored "-Wold-style-cast"
|
|
|
|
#endif
|
2019-06-22 01:32:47 +00:00
|
|
|
if (this->m->action == a_deflate)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2019-08-23 23:54:08 +00:00
|
|
|
err = deflateInit(&zstream, compression_level);
|
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
|
|
|
}
|
2017-08-22 11:20:55 +00:00
|
|
|
#if ((defined(__GNUC__) && ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406) || \
|
|
|
|
defined(__clang__))
|
2013-02-24 02:46:21 +00:00
|
|
|
# pragma GCC diagnostic pop
|
|
|
|
#endif
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
checkError("Init", err);
|
2019-06-22 01:32:47 +00:00
|
|
|
this->m->initialized = true;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int err = Z_OK;
|
|
|
|
|
|
|
|
bool done = false;
|
|
|
|
while (! done)
|
|
|
|
{
|
2019-06-22 01:32:47 +00:00
|
|
|
if (this->m->action == a_deflate)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
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
|
|
|
}
|
2019-06-22 01:32:47 +00:00
|
|
|
if ((this->m->action == a_inflate) && (err != Z_OK) && zstream.msg &&
|
2018-03-03 16:35:01 +00:00
|
|
|
(strcmp(zstream.msg, "incorrect data check") == 0))
|
|
|
|
{
|
|
|
|
// Other PDF readers ignore this specific error. Combining
|
|
|
|
// this with Z_SYNC_FLUSH enables qpdf to handle some
|
|
|
|
// broken zlib streams without losing data.
|
|
|
|
err = Z_STREAM_END;
|
|
|
|
}
|
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
|
2021-11-02 21:54:10 +00:00
|
|
|
// call to inflate could have nothing to do. There are PDF
|
|
|
|
// files in the wild that have this error (including at
|
|
|
|
// least one in qpdf's test suite). In some cases, we want
|
|
|
|
// to know about this, because it indicates incorrect
|
|
|
|
// compression, so call a callback if provided.
|
|
|
|
this->warn(
|
|
|
|
"input stream is complete but output may still be valid",
|
|
|
|
err);
|
2008-04-29 12:55:25 +00:00
|
|
|
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;
|
|
|
|
}
|
2019-06-21 04:01:36 +00:00
|
|
|
uLong ready =
|
2019-06-22 01:32:47 +00:00
|
|
|
QIntC::to_ulong(this->m->out_bufsize - zstream.avail_out);
|
2008-04-29 12:55:25 +00:00
|
|
|
if (ready > 0)
|
|
|
|
{
|
2022-02-04 15:10:19 +00:00
|
|
|
this->getNext()->write(this->m->outbuf.get(), ready);
|
|
|
|
zstream.next_out = this->m->outbuf.get();
|
2019-06-22 01:32:47 +00:00
|
|
|
zstream.avail_out = QIntC::to_uint(this->m->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
|
|
|
{
|
2022-02-04 15:10:19 +00:00
|
|
|
if (this->m->outbuf.get())
|
2017-07-29 16:07:19 +00:00
|
|
|
{
|
2019-06-22 01:32:47 +00:00
|
|
|
if (this->m->initialized)
|
2017-07-29 16:07:19 +00:00
|
|
|
{
|
2019-06-22 01:32:47 +00:00
|
|
|
z_stream& zstream = *(static_cast<z_stream*>(this->m->zdata));
|
2017-07-29 16:07:19 +00:00
|
|
|
unsigned char buf[1];
|
|
|
|
buf[0] = '\0';
|
|
|
|
handleData(buf, 0, Z_FINISH);
|
|
|
|
int err = Z_OK;
|
2019-06-22 01:32:47 +00:00
|
|
|
if (this->m->action == a_deflate)
|
2017-07-29 16:07:19 +00:00
|
|
|
{
|
|
|
|
err = deflateEnd(&zstream);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
err = inflateEnd(&zstream);
|
|
|
|
}
|
2019-06-22 01:32:47 +00:00
|
|
|
this->m->initialized = false;
|
2017-07-29 16:07:19 +00:00
|
|
|
checkError("End", err);
|
|
|
|
}
|
|
|
|
|
2019-06-22 01:32:47 +00:00
|
|
|
this->m->outbuf = 0;
|
2017-07-29 16:07:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (std::exception& e)
|
|
|
|
{
|
2021-11-02 21:54:10 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
this->getNext()->finish();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
// ignore secondary exception
|
|
|
|
}
|
|
|
|
throw std::runtime_error(e.what());
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
this->getNext()->finish();
|
|
|
|
}
|
|
|
|
|
2019-08-23 23:54:08 +00:00
|
|
|
void
|
|
|
|
Pl_Flate::setCompressionLevel(int level)
|
|
|
|
{
|
|
|
|
compression_level = level;
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
void
|
|
|
|
Pl_Flate::checkError(char const* prefix, int error_code)
|
|
|
|
{
|
2019-06-22 01:32:47 +00:00
|
|
|
z_stream& zstream = *(static_cast<z_stream*>(this->m->zdata));
|
2008-04-29 12:55:25 +00:00
|
|
|
if (error_code != Z_OK)
|
|
|
|
{
|
2019-06-22 01:32:47 +00:00
|
|
|
char const* action_str =
|
|
|
|
(this->m->action == a_deflate ? "deflate" : "inflate");
|
2008-04-29 12:55:25 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|