2017-08-16 10:26:31 +00:00
|
|
|
#include <qpdf/Pl_RunLength.hh>
|
|
|
|
|
|
|
|
#include <qpdf/QTC.hh>
|
|
|
|
#include <qpdf/QUtil.hh>
|
|
|
|
|
2019-06-22 01:32:47 +00:00
|
|
|
Pl_RunLength::Members::Members(action_e action) :
|
2017-08-16 10:26:31 +00:00
|
|
|
action(action),
|
|
|
|
state(st_top),
|
|
|
|
length(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-06-22 01:32:47 +00:00
|
|
|
Pl_RunLength::Pl_RunLength(char const* identifier, Pipeline* next, action_e action) :
|
|
|
|
Pipeline(identifier, next),
|
|
|
|
m(new Members(action))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-08-16 10:26:31 +00:00
|
|
|
Pl_RunLength::~Pl_RunLength()
|
|
|
|
{
|
2022-04-15 23:44:07 +00:00
|
|
|
// Must be explicit and not inline -- see QPDF_DLL_CLASS in README-maintainer
|
2017-08-16 10:26:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-05-03 21:43:07 +00:00
|
|
|
Pl_RunLength::write(unsigned char const* data, size_t len)
|
2017-08-16 10:26:31 +00:00
|
|
|
{
|
2019-06-22 01:32:47 +00:00
|
|
|
if (m->action == a_encode) {
|
2017-08-16 10:26:31 +00:00
|
|
|
encode(data, len);
|
|
|
|
} else {
|
|
|
|
decode(data, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-05-03 21:43:07 +00:00
|
|
|
Pl_RunLength::encode(unsigned char const* data, size_t len)
|
2017-08-16 10:26:31 +00:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < len; ++i) {
|
2019-06-22 01:32:47 +00:00
|
|
|
if ((m->state == st_top) != (m->length <= 1)) {
|
2017-08-16 10:26:31 +00:00
|
|
|
throw std::logic_error("Pl_RunLength::encode: state/length inconsistency");
|
|
|
|
}
|
|
|
|
unsigned char ch = data[i];
|
2019-06-22 01:32:47 +00:00
|
|
|
if ((m->length > 0) && ((m->state == st_copying) || (m->length < 128)) &&
|
|
|
|
(ch == m->buf[m->length - 1])) {
|
2017-08-16 10:26:31 +00:00
|
|
|
QTC::TC("libtests", "Pl_RunLength: switch to run", (m->length == 128) ? 0 : 1);
|
2019-06-22 01:32:47 +00:00
|
|
|
if (m->state == st_copying) {
|
|
|
|
--m->length;
|
2017-08-16 10:26:31 +00:00
|
|
|
flush_encode();
|
2019-06-22 01:32:47 +00:00
|
|
|
m->buf[0] = ch;
|
|
|
|
m->length = 1;
|
2017-08-16 10:26:31 +00:00
|
|
|
}
|
2019-06-22 01:32:47 +00:00
|
|
|
m->state = st_run;
|
|
|
|
m->buf[m->length] = ch;
|
|
|
|
++m->length;
|
2017-08-16 10:26:31 +00:00
|
|
|
} else {
|
2019-06-22 01:32:47 +00:00
|
|
|
if ((m->length == 128) || (m->state == st_run)) {
|
2017-08-16 10:26:31 +00:00
|
|
|
flush_encode();
|
2019-06-22 01:32:47 +00:00
|
|
|
} else if (m->length > 0) {
|
|
|
|
m->state = st_copying;
|
2017-08-16 10:26:31 +00:00
|
|
|
}
|
2019-06-22 01:32:47 +00:00
|
|
|
m->buf[m->length] = ch;
|
|
|
|
++m->length;
|
2017-08-16 10:26:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-05-03 21:43:07 +00:00
|
|
|
Pl_RunLength::decode(unsigned char const* data, size_t len)
|
2017-08-16 10:26:31 +00:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < len; ++i) {
|
|
|
|
unsigned char ch = data[i];
|
2019-06-22 01:32:47 +00:00
|
|
|
switch (m->state) {
|
2017-08-16 10:26:31 +00:00
|
|
|
case st_top:
|
|
|
|
if (ch < 128) {
|
|
|
|
// length represents remaining number of bytes to copy
|
2019-06-22 01:32:47 +00:00
|
|
|
m->length = 1U + ch;
|
|
|
|
m->state = st_copying;
|
2017-08-16 10:26:31 +00:00
|
|
|
} else if (ch > 128) {
|
|
|
|
// length represents number of copies of next byte
|
2019-06-22 01:32:47 +00:00
|
|
|
m->length = 257U - ch;
|
|
|
|
m->state = st_run;
|
2017-08-16 10:26:31 +00:00
|
|
|
} else // ch == 128
|
|
|
|
{
|
|
|
|
// EOD; stay in this state
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case st_copying:
|
|
|
|
this->getNext()->write(&ch, 1);
|
2019-06-22 01:32:47 +00:00
|
|
|
if (--m->length == 0) {
|
|
|
|
m->state = st_top;
|
2017-08-16 10:26:31 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case st_run:
|
2019-06-22 01:32:47 +00:00
|
|
|
for (unsigned int j = 0; j < m->length; ++j) {
|
2017-08-16 10:26:31 +00:00
|
|
|
this->getNext()->write(&ch, 1);
|
|
|
|
}
|
2019-06-22 01:32:47 +00:00
|
|
|
m->state = st_top;
|
2017-08-16 10:26:31 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Pl_RunLength::flush_encode()
|
|
|
|
{
|
2019-06-22 01:32:47 +00:00
|
|
|
if (m->length == 128) {
|
2017-08-16 10:26:31 +00:00
|
|
|
QTC::TC(
|
|
|
|
"libtests",
|
|
|
|
"Pl_RunLength flush full buffer",
|
2019-06-22 01:32:47 +00:00
|
|
|
(m->state == st_copying ? 0
|
|
|
|
: m->state == st_run ? 1
|
2017-08-16 10:26:31 +00:00
|
|
|
: -1));
|
|
|
|
}
|
2019-06-22 01:32:47 +00:00
|
|
|
if (m->length == 0) {
|
2017-08-16 10:26:31 +00:00
|
|
|
QTC::TC("libtests", "Pl_RunLength flush empty buffer");
|
|
|
|
}
|
2019-06-22 01:32:47 +00:00
|
|
|
if (m->state == st_run) {
|
|
|
|
if ((m->length < 2) || (m->length > 128)) {
|
2017-08-16 10:26:31 +00:00
|
|
|
throw std::logic_error("Pl_RunLength: invalid length in flush_encode for run");
|
|
|
|
}
|
2023-05-20 12:34:53 +00:00
|
|
|
auto ch = static_cast<unsigned char>(257 - m->length);
|
2017-08-16 10:26:31 +00:00
|
|
|
this->getNext()->write(&ch, 1);
|
2019-06-22 01:32:47 +00:00
|
|
|
this->getNext()->write(&m->buf[0], 1);
|
|
|
|
} else if (m->length > 0) {
|
2023-05-20 12:34:53 +00:00
|
|
|
auto ch = static_cast<unsigned char>(m->length - 1);
|
2017-08-16 10:26:31 +00:00
|
|
|
this->getNext()->write(&ch, 1);
|
2019-06-22 01:32:47 +00:00
|
|
|
this->getNext()->write(m->buf, m->length);
|
2017-08-16 10:26:31 +00:00
|
|
|
}
|
2019-06-22 01:32:47 +00:00
|
|
|
m->state = st_top;
|
|
|
|
m->length = 0;
|
2017-08-16 10:26:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Pl_RunLength::finish()
|
|
|
|
{
|
|
|
|
// When decoding, we might have read a length byte not followed by data, which means the stream
|
|
|
|
// was terminated early, but we will just ignore this case since this is the only sensible thing
|
|
|
|
// to do.
|
2019-06-22 01:32:47 +00:00
|
|
|
if (m->action == a_encode) {
|
2017-08-16 10:26:31 +00:00
|
|
|
flush_encode();
|
|
|
|
unsigned char ch = 128;
|
|
|
|
this->getNext()->write(&ch, 1);
|
|
|
|
}
|
|
|
|
this->getNext()->finish();
|
|
|
|
}
|