2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/Pl_ASCII85Decoder.hh>
|
2022-02-04 21:31:31 +00:00
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/QTC.hh>
|
2023-05-20 11:22:32 +00:00
|
|
|
#include <cstring>
|
2009-09-26 18:36:04 +00:00
|
|
|
#include <stdexcept>
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
Pl_ASCII85Decoder::Pl_ASCII85Decoder(char const* identifier, Pipeline* next) :
|
|
|
|
Pipeline(identifier, next),
|
|
|
|
pos(0),
|
|
|
|
eod(0)
|
|
|
|
{
|
|
|
|
memset(this->inbuf, 117, 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-05-03 21:43:07 +00:00
|
|
|
Pl_ASCII85Decoder::write(unsigned char const* buf, size_t len)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
if (eod > 1) {
|
2022-02-08 14:18:08 +00:00
|
|
|
return;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2012-06-20 15:20:57 +00:00
|
|
|
for (size_t i = 0; i < len; ++i) {
|
2022-02-08 14:18:08 +00:00
|
|
|
if (eod > 1) {
|
|
|
|
break;
|
|
|
|
} else if (eod == 1) {
|
|
|
|
if (buf[i] == '>') {
|
|
|
|
flush();
|
|
|
|
eod = 2;
|
|
|
|
} else {
|
|
|
|
throw std::runtime_error("broken end-of-data sequence in base 85 data");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (buf[i]) {
|
|
|
|
case ' ':
|
|
|
|
case '\f':
|
|
|
|
case '\v':
|
|
|
|
case '\t':
|
|
|
|
case '\r':
|
|
|
|
case '\n':
|
|
|
|
QTC::TC("libtests", "Pl_ASCII85Decoder ignore space");
|
|
|
|
// ignore whitespace
|
|
|
|
break;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-02-08 14:18:08 +00:00
|
|
|
case '~':
|
|
|
|
eod = 1;
|
|
|
|
break;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-02-08 14:18:08 +00:00
|
|
|
case 'z':
|
|
|
|
if (pos != 0) {
|
|
|
|
throw std::runtime_error("unexpected z during base 85 decode");
|
|
|
|
} else {
|
|
|
|
QTC::TC("libtests", "Pl_ASCII85Decoder read z");
|
2013-02-24 02:46:21 +00:00
|
|
|
unsigned char zeroes[4];
|
|
|
|
memset(zeroes, '\0', 4);
|
2022-02-08 14:18:08 +00:00
|
|
|
getNext()->write(zeroes, 4);
|
|
|
|
}
|
|
|
|
break;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-02-08 14:18:08 +00:00
|
|
|
default:
|
|
|
|
if ((buf[i] < 33) || (buf[i] > 117)) {
|
|
|
|
throw std::runtime_error("character out of range during base 85 decode");
|
|
|
|
} else {
|
|
|
|
this->inbuf[this->pos++] = buf[i];
|
|
|
|
if (pos == 5) {
|
|
|
|
flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Pl_ASCII85Decoder::flush()
|
|
|
|
{
|
|
|
|
if (this->pos == 0) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("libtests", "Pl_ASCII85Decoder no-op flush");
|
|
|
|
return;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
unsigned long lval = 0;
|
|
|
|
for (int i = 0; i < 5; ++i) {
|
2022-02-08 14:18:08 +00:00
|
|
|
lval *= 85;
|
|
|
|
lval += (this->inbuf[i] - 33U);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char outbuf[4];
|
|
|
|
memset(outbuf, 0, 4);
|
|
|
|
for (int i = 3; i >= 0; --i) {
|
2022-02-08 14:18:08 +00:00
|
|
|
outbuf[i] = lval & 0xff;
|
|
|
|
lval >>= 8;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QTC::TC("libtests", "Pl_ASCII85Decoder partial flush", (this->pos == 5) ? 0 : 1);
|
2021-01-04 16:55:28 +00:00
|
|
|
// Reset before calling getNext()->write in case that throws an exception.
|
|
|
|
auto t = this->pos - 1;
|
2008-04-29 12:55:25 +00:00
|
|
|
this->pos = 0;
|
|
|
|
memset(this->inbuf, 117, 5);
|
2021-01-04 16:55:28 +00:00
|
|
|
|
|
|
|
getNext()->write(outbuf, t);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Pl_ASCII85Decoder::finish()
|
|
|
|
{
|
|
|
|
flush();
|
|
|
|
getNext()->finish();
|
|
|
|
}
|