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>
|
2009-09-26 18:36:04 +00:00
|
|
|
#include <stdexcept>
|
2008-04-29 12:55:25 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (eod > 1) {
|
2022-02-08 14:18:08 +00:00
|
|
|
return;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
for (size_t i = 0; i < len; ++i) {
|
|
|
|
if (eod > 1) {
|
2022-02-08 14:18:08 +00:00
|
|
|
break;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (eod == 1) {
|
|
|
|
if (buf[i] == '>') {
|
2022-02-08 14:18:08 +00:00
|
|
|
flush();
|
|
|
|
eod = 2;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
throw std::runtime_error(
|
|
|
|
"broken end-of-data sequence in base 85 data");
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
|
|
|
switch (buf[i]) {
|
|
|
|
case ' ':
|
|
|
|
case '\f':
|
|
|
|
case '\v':
|
|
|
|
case '\t':
|
|
|
|
case '\r':
|
|
|
|
case '\n':
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("libtests", "Pl_ASCII85Decoder ignore space");
|
|
|
|
// ignore whitespace
|
|
|
|
break;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case '~':
|
2022-02-08 14:18:08 +00:00
|
|
|
eod = 1;
|
|
|
|
break;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case 'z':
|
|
|
|
if (pos != 0) {
|
2022-02-08 14:18:08 +00:00
|
|
|
throw std::runtime_error(
|
|
|
|
"unexpected z during base 85 decode");
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
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-04-02 21:14:10 +00:00
|
|
|
default:
|
|
|
|
if ((buf[i] < 33) || (buf[i] > 117)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
throw std::runtime_error(
|
|
|
|
"character out of range during base 85 decode");
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
this->inbuf[this->pos++] = buf[i];
|
2022-04-02 21:14:10 +00:00
|
|
|
if (pos == 5) {
|
2022-02-08 14:18:08 +00:00
|
|
|
flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Pl_ASCII85Decoder::flush()
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
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;
|
2022-04-02 21:14:10 +00:00
|
|
|
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);
|
2022-04-02 21:14:10 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +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();
|
|
|
|
}
|