159 lines
4.0 KiB
C++
159 lines
4.0 KiB
C++
#include "echoion/protocol/QuickLz.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <stdexcept>
|
|
|
|
namespace Echoion::Protocol
|
|
{
|
|
namespace
|
|
{
|
|
constexpr int TABLE_SIZE = 4096;
|
|
constexpr uint32_t MAX_DECOMPRESSED_SIZE = 1024 * 1024;
|
|
|
|
uint32_t readU32LE(ByteView b, size_t off)
|
|
{
|
|
return static_cast<uint32_t>(b[off]) |
|
|
(static_cast<uint32_t>(b[off + 1]) << 8) |
|
|
(static_cast<uint32_t>(b[off + 2]) << 16) |
|
|
(static_cast<uint32_t>(b[off + 3]) << 24);
|
|
}
|
|
|
|
int32_t readI32LE(ByteView b, size_t off)
|
|
{
|
|
return static_cast<int32_t>(readU32LE(b, off));
|
|
}
|
|
|
|
uint32_t read24(const Bytes& b, size_t off)
|
|
{
|
|
return static_cast<uint32_t>(b[off]) |
|
|
(static_cast<uint32_t>(b[off + 1]) << 8) |
|
|
(static_cast<uint32_t>(b[off + 2]) << 16);
|
|
}
|
|
|
|
int hashVal(uint32_t value)
|
|
{
|
|
return static_cast<int>(((value >> 12) ^ value) & 0xfff);
|
|
}
|
|
} // namespace
|
|
|
|
uint32_t qlzDecompressedSize(ByteView data)
|
|
{
|
|
return (data[0] & 0x02) != 0
|
|
? static_cast<uint32_t>(readI32LE(data, 5))
|
|
: data[2];
|
|
}
|
|
|
|
uint32_t qlzCompressedSize(ByteView data)
|
|
{
|
|
return (data[0] & 0x02) != 0
|
|
? static_cast<uint32_t>(readI32LE(data, 1))
|
|
: data[1];
|
|
}
|
|
|
|
Bytes qlzDecompress(ByteView data)
|
|
{
|
|
if (data.empty()) {
|
|
throw std::runtime_error("QuickLZ: empty input");
|
|
}
|
|
const uint8_t flags = data[0];
|
|
const int level = (flags >> 2) & 0x03;
|
|
if (level != 1) {
|
|
throw std::runtime_error("QuickLZ level not supported");
|
|
}
|
|
|
|
const size_t headerLen = (flags & 0x02) != 0 ? 9 : 3;
|
|
const uint32_t decompressedSize = qlzDecompressedSize(data);
|
|
if (decompressedSize > MAX_DECOMPRESSED_SIZE) {
|
|
throw std::runtime_error("QuickLZ: decompressed size exceeds max");
|
|
}
|
|
|
|
Bytes dest(decompressedSize, 0);
|
|
|
|
if ((flags & 0x01) == 0) {
|
|
// Stored, not compressed.
|
|
if (headerLen + decompressedSize > data.size()) {
|
|
throw std::runtime_error("QuickLZ: truncated stored block");
|
|
}
|
|
std::copy_n(data.begin() + headerLen, decompressedSize,
|
|
dest.begin());
|
|
return dest;
|
|
}
|
|
|
|
std::vector<int32_t> hashtable(TABLE_SIZE, 0);
|
|
uint32_t control = 1;
|
|
size_t sourcePos = headerLen;
|
|
size_t destPos = 0;
|
|
size_t nextHashed = 0;
|
|
|
|
while (true) {
|
|
if (control == 1) {
|
|
control = readU32LE(data, sourcePos);
|
|
sourcePos += 4;
|
|
}
|
|
|
|
if ((control & 1) != 0) {
|
|
control >>= 1;
|
|
const uint8_t next = data[sourcePos++];
|
|
const int hashIdx = (next >> 4) | (data[sourcePos++] << 4);
|
|
|
|
uint32_t matchLen = next & 0x0f;
|
|
if (matchLen != 0) {
|
|
matchLen += 2;
|
|
} else {
|
|
matchLen = data[sourcePos++];
|
|
}
|
|
|
|
const int32_t offset = hashtable[hashIdx];
|
|
|
|
dest[destPos] = dest[offset];
|
|
dest[destPos + 1] = dest[offset + 1];
|
|
dest[destPos + 2] = dest[offset + 2];
|
|
for (uint32_t i = 3; i < matchLen; ++i) {
|
|
dest[destPos + i] = dest[offset + i];
|
|
}
|
|
destPos += matchLen;
|
|
|
|
const size_t end = destPos + 1 - matchLen;
|
|
if (nextHashed < end) {
|
|
uint32_t nxt = read24(dest, nextHashed);
|
|
hashtable[hashVal(nxt)] = static_cast<int32_t>(nextHashed);
|
|
for (size_t i = nextHashed + 1; i < end; ++i) {
|
|
nxt = (nxt >> 8) |
|
|
(static_cast<uint32_t>(dest[i + 2]) << 16);
|
|
hashtable[hashVal(nxt)] = static_cast<int32_t>(i);
|
|
}
|
|
}
|
|
nextHashed = destPos;
|
|
} else if (destPos >=
|
|
std::max<size_t>(decompressedSize, 10) - 10) {
|
|
while (destPos < decompressedSize) {
|
|
if (control == 1) {
|
|
sourcePos += 4;
|
|
}
|
|
control >>= 1;
|
|
dest[destPos++] = data[sourcePos++];
|
|
}
|
|
break;
|
|
} else {
|
|
dest[destPos++] = data[sourcePos++];
|
|
control >>= 1;
|
|
|
|
const size_t end = destPos >= 2 ? destPos - 2 : 0;
|
|
if (nextHashed < end) {
|
|
uint32_t nxt = read24(dest, nextHashed);
|
|
hashtable[hashVal(nxt)] = static_cast<int32_t>(nextHashed);
|
|
for (size_t i = nextHashed + 1; i < end; ++i) {
|
|
nxt = (nxt >> 8) |
|
|
(static_cast<uint32_t>(dest[i + 2]) << 16);
|
|
hashtable[hashVal(nxt)] = static_cast<int32_t>(i);
|
|
}
|
|
}
|
|
nextHashed = std::max(nextHashed, end);
|
|
}
|
|
}
|
|
|
|
return dest;
|
|
}
|
|
|
|
} // namespace Echoion::Protocol
|