2020-11-10 00:58:19 +01:00
|
|
|
#include "subfile.h"
|
|
|
|
#include "huffmantext.h"
|
|
|
|
|
2021-04-10 15:27:40 +02:00
|
|
|
using namespace IMG;
|
2020-11-10 00:58:19 +01:00
|
|
|
|
|
|
|
bool HuffmanText::fetch(const SubFile *file, SubFile::Handle &hdl,
|
|
|
|
quint32 &data, quint32 &bits, quint32 &usedBits, quint32 &usedData) const
|
|
|
|
{
|
|
|
|
quint32 rs, ls, old;
|
|
|
|
|
2021-10-23 22:53:10 +02:00
|
|
|
bits = _table.symBits() - bits;
|
2020-11-10 00:58:19 +01:00
|
|
|
|
|
|
|
if (usedBits < bits) {
|
2021-10-23 22:53:10 +02:00
|
|
|
old = usedBits ? usedData >> (32 - usedBits) : 0;
|
2020-11-10 00:58:19 +01:00
|
|
|
if (!file->readVUInt32SW(hdl, 4, usedData))
|
|
|
|
return false;
|
|
|
|
ls = bits - usedBits;
|
2021-10-23 22:53:10 +02:00
|
|
|
rs = 32 - (bits - usedBits);
|
2020-11-10 00:58:19 +01:00
|
|
|
old = usedData >> rs | old << ls;
|
|
|
|
} else {
|
|
|
|
ls = bits;
|
|
|
|
rs = usedBits - bits;
|
2021-10-23 22:53:10 +02:00
|
|
|
old = usedData >> (32 - bits);
|
2020-11-10 00:58:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
usedData = usedData << ls;
|
2021-10-23 22:53:10 +02:00
|
|
|
data = data | old << (32 - _table.symBits());
|
2020-11-10 00:58:19 +01:00
|
|
|
usedBits = rs;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HuffmanText::decode(const SubFile *file, SubFile::Handle &hdl,
|
|
|
|
QVector<quint8> &str) const
|
|
|
|
{
|
|
|
|
quint32 bits = 0;
|
|
|
|
quint32 data = 0;
|
|
|
|
quint32 usedBits = 0;
|
|
|
|
quint32 usedData = 0;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
if (!fetch(file, hdl, data, bits, usedBits, usedData))
|
|
|
|
return false;
|
|
|
|
|
2021-10-23 22:53:10 +02:00
|
|
|
quint8 size;
|
|
|
|
quint32 sym = _table.symbol(data, size);
|
2020-11-10 00:58:19 +01:00
|
|
|
|
2021-10-23 22:53:10 +02:00
|
|
|
if (_table.symBits() < size)
|
2020-11-10 00:58:19 +01:00
|
|
|
return false;
|
|
|
|
data = data << size;
|
2021-10-23 22:53:10 +02:00
|
|
|
bits = _table.symBits() - size;
|
2020-11-10 00:58:19 +01:00
|
|
|
|
2021-10-23 22:53:10 +02:00
|
|
|
if (!(_table.symbolBits() & 7)) {
|
|
|
|
for (quint32 i = 0; i < (_table.symbolBits() >> 3); i++) {
|
2020-11-10 00:58:19 +01:00
|
|
|
str.append((quint8)sym);
|
|
|
|
if (((quint8)sym == '\0'))
|
|
|
|
return true;
|
|
|
|
sym = sym >> 8;
|
|
|
|
}
|
|
|
|
} else {
|
2021-10-23 22:53:10 +02:00
|
|
|
Q_ASSERT(false);
|
|
|
|
return false;
|
2020-11-10 00:58:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|