2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/Pl_LZWDecoder.hh>
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <qpdf/QIntC.hh>
|
2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/QTC.hh>
|
2013-10-05 20:37:27 +00:00
|
|
|
#include <qpdf/QUtil.hh>
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <assert.h>
|
2009-09-26 18:36:04 +00:00
|
|
|
#include <stdexcept>
|
2008-04-29 12:55:25 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
Pl_LZWDecoder::Pl_LZWDecoder(
|
|
|
|
char const* identifier, Pipeline* next, bool early_code_change) :
|
2008-04-29 12:55:25 +00:00
|
|
|
Pipeline(identifier, next),
|
|
|
|
code_size(9),
|
|
|
|
next(0),
|
|
|
|
byte_pos(0),
|
|
|
|
bit_pos(0),
|
|
|
|
bits_available(0),
|
|
|
|
code_change_delta(early_code_change ? 1 : 0),
|
|
|
|
eod(false),
|
|
|
|
last_code(256)
|
|
|
|
{
|
|
|
|
memset(buf, 0, 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-06-20 15:20:57 +00:00
|
|
|
Pl_LZWDecoder::write(unsigned char* bytes, size_t len)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
for (size_t i = 0; i < len; ++i) {
|
2022-02-08 14:18:08 +00:00
|
|
|
this->buf[next++] = bytes[i];
|
2022-04-02 21:14:10 +00:00
|
|
|
if (this->next == 3) {
|
2022-02-08 14:18:08 +00:00
|
|
|
this->next = 0;
|
|
|
|
}
|
|
|
|
this->bits_available += 8;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (this->bits_available >= this->code_size) {
|
2022-02-08 14:18:08 +00:00
|
|
|
sendNextCode();
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Pl_LZWDecoder::finish()
|
|
|
|
{
|
|
|
|
getNext()->finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Pl_LZWDecoder::sendNextCode()
|
|
|
|
{
|
2019-06-21 03:35:23 +00:00
|
|
|
unsigned int high = this->byte_pos;
|
|
|
|
unsigned int med = (this->byte_pos + 1) % 3;
|
|
|
|
unsigned int low = (this->byte_pos + 2) % 3;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2019-06-21 03:35:23 +00:00
|
|
|
unsigned int bits_from_high = 8 - this->bit_pos;
|
|
|
|
unsigned int bits_from_med = this->code_size - bits_from_high;
|
|
|
|
unsigned int bits_from_low = 0;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (bits_from_med > 8) {
|
2022-02-08 14:18:08 +00:00
|
|
|
bits_from_low = bits_from_med - 8;
|
|
|
|
bits_from_med = 8;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2019-06-21 03:35:23 +00:00
|
|
|
unsigned int high_mask = (1U << bits_from_high) - 1U;
|
|
|
|
unsigned int med_mask = 0xff - ((1U << (8 - bits_from_med)) - 1U);
|
|
|
|
unsigned int low_mask = 0xff - ((1U << (8 - bits_from_low)) - 1U);
|
|
|
|
unsigned int code = 0;
|
2008-04-29 12:55:25 +00:00
|
|
|
code += (this->buf[high] & high_mask) << bits_from_med;
|
|
|
|
code += ((this->buf[med] & med_mask) >> (8 - bits_from_med));
|
2022-04-02 21:14:10 +00:00
|
|
|
if (bits_from_low) {
|
2022-02-08 14:18:08 +00:00
|
|
|
code <<= bits_from_low;
|
|
|
|
code += ((this->buf[low] & low_mask) >> (8 - bits_from_low));
|
|
|
|
this->byte_pos = low;
|
|
|
|
this->bit_pos = bits_from_low;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
this->byte_pos = med;
|
|
|
|
this->bit_pos = bits_from_med;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (this->bit_pos == 8) {
|
2022-02-08 14:18:08 +00:00
|
|
|
this->bit_pos = 0;
|
|
|
|
++this->byte_pos;
|
|
|
|
this->byte_pos %= 3;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
this->bits_available -= this->code_size;
|
|
|
|
|
|
|
|
handleCode(code);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char
|
2019-06-21 03:35:23 +00:00
|
|
|
Pl_LZWDecoder::getFirstChar(unsigned int code)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
unsigned char result = '\0';
|
2022-04-02 21:14:10 +00:00
|
|
|
if (code < 256) {
|
2022-02-08 14:18:08 +00:00
|
|
|
result = static_cast<unsigned char>(code);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (code > 257) {
|
2022-02-08 14:18:08 +00:00
|
|
|
unsigned int idx = code - 258;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (idx >= table.size()) {
|
2019-08-26 01:23:19 +00:00
|
|
|
throw std::runtime_error(
|
2013-10-05 20:37:27 +00:00
|
|
|
"Pl_LZWDecoder::getFirstChar: table overflow");
|
|
|
|
}
|
2022-02-08 14:18:08 +00:00
|
|
|
Buffer& b = table.at(idx);
|
|
|
|
result = b.getBuffer()[0];
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2019-08-26 01:23:19 +00:00
|
|
|
throw std::runtime_error(
|
2013-10-05 20:37:27 +00:00
|
|
|
"Pl_LZWDecoder::getFirstChar called with invalid code (" +
|
|
|
|
QUtil::int_to_string(code) + ")");
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Pl_LZWDecoder::addToTable(unsigned char next)
|
|
|
|
{
|
|
|
|
unsigned int last_size = 0;
|
|
|
|
unsigned char const* last_data = 0;
|
|
|
|
unsigned char tmp[1];
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (this->last_code < 256) {
|
2022-02-08 14:18:08 +00:00
|
|
|
tmp[0] = static_cast<unsigned char>(this->last_code);
|
|
|
|
last_data = tmp;
|
|
|
|
last_size = 1;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (this->last_code > 257) {
|
2022-02-08 14:18:08 +00:00
|
|
|
unsigned int idx = this->last_code - 258;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (idx >= table.size()) {
|
2019-08-26 01:23:19 +00:00
|
|
|
throw std::runtime_error(
|
2013-10-05 20:37:27 +00:00
|
|
|
"Pl_LZWDecoder::addToTable: table overflow");
|
|
|
|
}
|
2022-02-08 14:18:08 +00:00
|
|
|
Buffer& b = table.at(idx);
|
|
|
|
last_data = b.getBuffer();
|
|
|
|
last_size = QIntC::to_uint(b.getSize());
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2019-08-26 01:23:19 +00:00
|
|
|
throw std::runtime_error(
|
2013-10-05 20:37:27 +00:00
|
|
|
"Pl_LZWDecoder::addToTable called with invalid code (" +
|
|
|
|
QUtil::int_to_string(this->last_code) + ")");
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
Buffer entry(1 + last_size);
|
|
|
|
unsigned char* new_data = entry.getBuffer();
|
|
|
|
memcpy(new_data, last_data, last_size);
|
|
|
|
new_data[last_size] = next;
|
|
|
|
this->table.push_back(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-06-21 03:35:23 +00:00
|
|
|
Pl_LZWDecoder::handleCode(unsigned int code)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (this->eod) {
|
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
|
|
|
if (code == 256) {
|
|
|
|
if (!this->table.empty()) {
|
2022-02-08 14:18:08 +00:00
|
|
|
QTC::TC("libtests", "Pl_LZWDecoder intermediate reset");
|
|
|
|
}
|
|
|
|
this->table.clear();
|
|
|
|
this->code_size = 9;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (code == 257) {
|
2022-02-08 14:18:08 +00:00
|
|
|
this->eod = true;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
|
|
|
if (this->last_code != 256) {
|
2022-02-08 14:18:08 +00:00
|
|
|
// Add to the table from last time. New table entry would
|
|
|
|
// be what we read last plus the first character of what
|
|
|
|
// we're reading now.
|
|
|
|
unsigned char next = '\0';
|
|
|
|
unsigned int table_size = QIntC::to_uint(table.size());
|
2022-04-02 21:14:10 +00:00
|
|
|
if (code < 256) {
|
2022-02-08 14:18:08 +00:00
|
|
|
// just read < 256; last time's next was code
|
|
|
|
next = static_cast<unsigned char>(code);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (code > 257) {
|
2022-02-08 14:18:08 +00:00
|
|
|
size_t idx = code - 258;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (idx > table_size) {
|
2022-02-08 14:18:08 +00:00
|
|
|
throw std::runtime_error("LZWDecoder: bad code received");
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (idx == table_size) {
|
2022-02-08 14:18:08 +00:00
|
|
|
// The encoder would have just created this entry,
|
|
|
|
// so the first character of this entry would have
|
|
|
|
// been the same as the first character of the
|
|
|
|
// last entry.
|
|
|
|
QTC::TC("libtests", "Pl_LZWDecoder last was table size");
|
|
|
|
next = getFirstChar(this->last_code);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
next = getFirstChar(code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unsigned int new_idx = 258 + table_size;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (new_idx == 4096) {
|
2022-02-08 14:18:08 +00:00
|
|
|
throw std::runtime_error("LZWDecoder: table full");
|
|
|
|
}
|
|
|
|
addToTable(next);
|
|
|
|
unsigned int change_idx = new_idx + code_change_delta;
|
2022-04-02 21:14:10 +00:00
|
|
|
if ((change_idx == 511) || (change_idx == 1023) ||
|
|
|
|
(change_idx == 2047)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
++this->code_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (code < 256) {
|
2022-02-08 14:18:08 +00:00
|
|
|
unsigned char ch = static_cast<unsigned char>(code);
|
|
|
|
getNext()->write(&ch, 1);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2019-08-26 01:23:19 +00:00
|
|
|
unsigned int idx = code - 258;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (idx >= table.size()) {
|
2019-08-26 01:23:19 +00:00
|
|
|
throw std::runtime_error(
|
|
|
|
"Pl_LZWDecoder::handleCode: table overflow");
|
|
|
|
}
|
2022-02-08 14:18:08 +00:00
|
|
|
Buffer& b = table.at(idx);
|
|
|
|
getNext()->write(b.getBuffer(), b.getSize());
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this->last_code = code;
|
|
|
|
}
|