Files
top-octane/src/assets/rnc.cpp
T

413 lines
12 KiB
C++
Raw Normal View History

2026-11-30 22:31:51 +02:00
#include "assets/rnc.hpp"
#include "core/log.hpp"
#include <algorithm>
#include <array>
#include <cstring>
namespace top_octane::assets {
namespace {
constexpr uint32_t kMaxRncUnpackedBytes = 32u * 1024u * 1024u;
2026-11-30 22:31:51 +02:00
inline uint16_t read_be16(const uint8_t* p) {
return static_cast<uint16_t>((static_cast<uint16_t>(p[0]) << 8) | p[1]);
}
inline uint32_t read_be32(const uint8_t* p) {
return (static_cast<uint32_t>(p[0]) << 24) |
(static_cast<uint32_t>(p[1]) << 16) |
(static_cast<uint32_t>(p[2]) << 8) |
static_cast<uint32_t>(p[3]);
}
inline uint16_t read_le16(const uint8_t* p) {
return static_cast<uint16_t>(p[0] | (static_cast<uint16_t>(p[1]) << 8));
}
struct HuffEntry {
uint16_t code;
uint16_t mask;
uint8_t bits;
uint8_t sym;
};
struct Rnc1BitStream {
const uint8_t* src = nullptr;
size_t pos = 0;
size_t size = 0;
2026-11-30 22:31:51 +02:00
uint32_t bitbuf = 0;
int bitcnt = 0;
bool error = false;
2026-11-30 22:31:51 +02:00
void init(const uint8_t* buf, size_t buf_size) {
2026-11-30 22:31:51 +02:00
src = buf;
pos = 0;
size = buf_size;
error = (!src || size < 2);
bitbuf = error ? 0u : read_le16(src);
2026-11-30 22:31:51 +02:00
bitcnt = 16;
}
void advance(int n) {
if (error || n < 0 || n > 16) {
error = true;
return;
}
2026-11-30 22:31:51 +02:00
bitbuf >>= n;
bitcnt -= n;
if (bitcnt < 16) {
pos += 2;
if (pos + 2 > size) {
error = true;
return;
}
2026-11-30 22:31:51 +02:00
bitbuf |= static_cast<uint32_t>(read_le16(src + pos)) << bitcnt;
bitcnt += 16;
}
}
uint32_t read(uint32_t mask, int n) {
uint32_t v = bitbuf & mask;
advance(n);
return v;
}
void fix() {
if (error || pos + 2 > size) {
error = true;
return;
}
2026-11-30 22:31:51 +02:00
bitcnt -= 16;
if (bitcnt < 0) bitcnt = 0;
2026-11-30 22:31:51 +02:00
uint32_t keep_mask = (bitcnt > 0) ? ((1u << bitcnt) - 1u) : 0u;
bitbuf &= keep_mask;
bitbuf |= static_cast<uint32_t>(read_le16(src + pos)) << bitcnt;
bitcnt += 16;
}
};
bool build_huff_table(Rnc1BitStream& bs, std::vector<HuffEntry>& out) {
out.clear();
uint32_t num = bs.read(0x1F, 5);
if (bs.error) return false;
2026-11-30 22:31:51 +02:00
if (num == 0) return true;
if (num > 32) return false;
2026-11-30 22:31:51 +02:00
std::array<uint8_t, 32> lens{};
for (uint32_t i = 0; i < num; ++i) {
lens[i] = static_cast<uint8_t>(bs.read(0x0F, 4));
if (bs.error) return false;
2026-11-30 22:31:51 +02:00
}
uint32_t code = 0;
for (int bl = 1; bl <= 16; ++bl) {
for (uint32_t idx = 0; idx < num; ++idx) {
if (lens[idx] == bl) {
uint16_t rev = 0;
for (int k = 0; k < bl; ++k) {
if (code & (1u << (bl - 1 - k))) {
rev |= static_cast<uint16_t>(1u << k);
}
}
HuffEntry e;
e.code = rev;
e.mask = static_cast<uint16_t>((1u << bl) - 1u);
e.bits = static_cast<uint8_t>(bl);
e.sym = static_cast<uint8_t>(idx);
out.push_back(e);
++code;
}
}
code <<= 1;
}
return !bs.error;
2026-11-30 22:31:51 +02:00
}
bool read_huff(Rnc1BitStream& bs, const std::vector<HuffEntry>& tbl, uint32_t& out_val) {
if (bs.error || tbl.empty()) return false;
2026-11-30 22:31:51 +02:00
for (const auto& e : tbl) {
if ((bs.bitbuf & e.mask) == e.code) {
bs.advance(e.bits);
if (bs.error) return false;
2026-11-30 22:31:51 +02:00
if (e.sym < 2) {
out_val = e.sym;
} else if (e.sym <= 17) {
2026-11-30 22:31:51 +02:00
int extra = e.sym - 1;
uint32_t high_bit = 1u << extra;
uint32_t mask = high_bit - 1u;
out_val = bs.read(mask, extra) | high_bit;
} else {
return false;
2026-11-30 22:31:51 +02:00
}
return !bs.error;
2026-11-30 22:31:51 +02:00
}
}
return false;
}
bool unpack_rnc1(const uint8_t* data, size_t size, std::vector<uint8_t>& output) {
if (size < 18) return false;
uint32_t unpacked_len = read_be32(data + 4);
uint32_t packed_len = read_be32(data + 8);
uint16_t unpacked_crc = read_be16(data + 12);
uint16_t packed_crc = read_be16(data + 14);
uint8_t pack_chunks = data[17];
if (unpacked_len > kMaxRncUnpackedBytes) {
LOG_ERROR("RNC1 unpacked size too large: %u", unpacked_len);
return false;
}
if (static_cast<size_t>(packed_len) > size || size - static_cast<size_t>(packed_len) < 18u) {
LOG_ERROR("RNC1 truncated: need 18+%u, got %zu", packed_len, size);
2026-11-30 22:31:51 +02:00
return false;
}
if (rnc_crc16(data + 18, packed_len) != packed_crc) {
LOG_ERROR("RNC1 packed CRC16 mismatch");
return false;
}
// Pad with trailing zero bytes so 16-bit prefetch reads safely at end-of-stream
const size_t src_limit = static_cast<size_t>(packed_len) + 4u;
std::vector<uint8_t> src(static_cast<size_t>(packed_len) + 8u, 0);
if (packed_len > 0) {
std::memcpy(src.data(), data + 18, packed_len);
}
2026-11-30 22:31:51 +02:00
Rnc1BitStream bs;
bs.init(src.data(), src.size());
2026-11-30 22:31:51 +02:00
bs.read(3, 2); // lock & key bits
if (bs.error) return false;
2026-11-30 22:31:51 +02:00
output.clear();
output.reserve(unpacked_len);
std::vector<HuffEntry> raw_t, dist_t, len_t;
for (uint8_t chunk = 0; chunk < pack_chunks; ++chunk) {
if (!build_huff_table(bs, raw_t) ||
!build_huff_table(bs, dist_t) ||
!build_huff_table(bs, len_t)) {
output.clear();
return false;
}
2026-11-30 22:31:51 +02:00
int subchunks = static_cast<int>(bs.read(0xFFFF, 16));
if (bs.error) {
output.clear();
return false;
}
2026-11-30 22:31:51 +02:00
while (true) {
uint32_t raw_len = 0;
if (!read_huff(bs, raw_t, raw_len)) {
LOG_ERROR("RNC1 raw huffman decode failure");
output.clear();
2026-11-30 22:31:51 +02:00
return false;
}
if (raw_len > 0) {
if (bs.pos > src_limit ||
static_cast<size_t>(raw_len) > src_limit - bs.pos ||
static_cast<size_t>(raw_len) > static_cast<size_t>(unpacked_len) - output.size()) {
2026-11-30 22:31:51 +02:00
LOG_ERROR("RNC1 raw copy OOB");
output.clear();
2026-11-30 22:31:51 +02:00
return false;
}
output.insert(output.end(), src.data() + bs.pos, src.data() + bs.pos + raw_len);
bs.pos += raw_len;
bs.fix();
if (bs.error) {
output.clear();
return false;
}
2026-11-30 22:31:51 +02:00
}
--subchunks;
if (subchunks <= 0) break;
uint32_t dist = 0, mlen = 0;
if (!read_huff(bs, dist_t, dist) || !read_huff(bs, len_t, mlen)) {
LOG_ERROR("RNC1 match huffman decode failure");
output.clear();
2026-11-30 22:31:51 +02:00
return false;
}
dist += 1u;
mlen += 2u;
if (dist > output.size() ||
static_cast<size_t>(mlen) > static_cast<size_t>(unpacked_len) - output.size()) {
LOG_ERROR("RNC1 back-reference OOB: dist=%u mlen=%u size=%zu", dist, mlen, output.size());
output.clear();
2026-11-30 22:31:51 +02:00
return false;
}
for (uint32_t k = 0; k < mlen; ++k) {
output.push_back(output[output.size() - dist]);
}
}
}
if (output.size() != unpacked_len) {
LOG_ERROR("RNC1 length mismatch: got %zu expected %u", output.size(), unpacked_len);
output.clear();
2026-11-30 22:31:51 +02:00
return false;
}
if (rnc_crc16(output.data(), output.size()) != unpacked_crc) {
LOG_ERROR("RNC1 unpacked CRC16 mismatch");
output.clear();
2026-11-30 22:31:51 +02:00
return false;
}
return true;
}
// RNC Method 2 decoder (MSB bitstream) supported defensively
bool unpack_rnc2(const uint8_t* data, size_t size, std::vector<uint8_t>& output) {
if (size < 18) return false;
uint32_t unpacked_len = read_be32(data + 4);
uint32_t packed_len = read_be32(data + 8);
uint16_t unpacked_crc = read_be16(data + 12);
uint16_t packed_crc = read_be16(data + 14);
if (unpacked_len > kMaxRncUnpackedBytes) return false;
if (static_cast<size_t>(packed_len) > size || size - static_cast<size_t>(packed_len) < 18u) {
return false;
}
2026-11-30 22:31:51 +02:00
if (rnc_crc16(data + 18, packed_len) != packed_crc) return false;
const uint8_t* src = data + 18;
size_t pos = 0;
uint16_t bitbuf = 0;
int bitcnt = 0;
bool eof_err = false;
2026-11-30 22:31:51 +02:00
auto getbit = [&]() -> int {
if (eof_err) return 1;
2026-11-30 22:31:51 +02:00
if (bitcnt == 0) {
if (pos >= packed_len) {
eof_err = true;
return 1;
}
bitbuf = src[pos++];
2026-11-30 22:31:51 +02:00
bitcnt = 8;
}
int bit = (bitbuf >> 7) & 1;
bitbuf <<= 1;
--bitcnt;
return bit;
};
auto getbyte = [&]() -> uint8_t {
if (eof_err || pos >= packed_len) {
eof_err = true;
return 0;
}
return src[pos++];
2026-11-30 22:31:51 +02:00
};
output.clear();
output.reserve(unpacked_len);
getbit();
getbit();
if (eof_err) return false;
2026-11-30 22:31:51 +02:00
while (!eof_err) {
while (!eof_err && getbit() == 0) {
uint8_t b = getbyte();
if (eof_err || output.size() >= unpacked_len) {
output.clear();
return false;
}
output.push_back(b);
}
if (eof_err) {
output.clear();
return false;
2026-11-30 22:31:51 +02:00
}
uint32_t len = 2;
uint32_t dist = 0;
if (getbit() == 0) {
len = 2;
if (getbit() == 1) {
len += 2;
if (getbit() == 1) len += 1;
}
dist = getbyte() + 1u;
} else {
len = 3;
if (getbit() == 0) {
if (getbit() == 0) {
len = 2;
int b0 = getbit(), b1 = getbit(), b2 = getbit();
(void)b0; (void)b1; (void)b2;
output.clear();
2026-11-30 22:31:51 +02:00
return false;
}
}
output.clear();
return false;
}
if (eof_err || dist == 0 || dist > output.size() ||
static_cast<size_t>(len) > static_cast<size_t>(unpacked_len) - output.size()) {
output.clear();
2026-11-30 22:31:51 +02:00
return false;
}
for (uint32_t i = 0; i < len; ++i) {
output.push_back(output[output.size() - dist]);
}
if (output.size() >= unpacked_len) break;
}
if (eof_err || output.size() != unpacked_len ||
rnc_crc16(output.data(), output.size()) != unpacked_crc) {
output.clear();
return false;
}
return true;
2026-11-30 22:31:51 +02:00
}
} // namespace
uint16_t rnc_crc16(const uint8_t* data, size_t len) {
static const auto table = []() {
std::array<uint16_t, 256> t{};
for (uint16_t i = 0; i < 256; ++i) {
uint16_t v = i;
for (int b = 0; b < 8; ++b) {
v = (v & 1) ? static_cast<uint16_t>((v >> 1) ^ 0xA001u) : static_cast<uint16_t>(v >> 1);
}
t[i] = v;
}
return t;
}();
uint16_t crc = 0;
for (size_t i = 0; i < len; ++i) {
crc ^= data[i];
crc = static_cast<uint16_t>((crc >> 8) ^ table[crc & 0xFFu]);
}
return crc;
}
bool is_rnc_compressed(const std::vector<uint8_t>& input) {
return input.size() >= 18 && input[0] == 'R' && input[1] == 'N' && input[2] == 'C' &&
(input[3] == 1 || input[3] == 2);
}
bool rnc_unpack(const std::vector<uint8_t>& input, std::vector<uint8_t>& output) {
return rnc_unpack_bytes(input.data(), input.size(), output);
}
bool rnc_unpack_bytes(const uint8_t* data, size_t size, std::vector<uint8_t>& output) {
if (!data || size == 0) {
output.clear();
return false;
}
if (!output.empty() && data >= output.data() && data < output.data() + output.size()) {
std::vector<uint8_t> copy(data, data + size);
return rnc_unpack_bytes(copy.data(), copy.size(), output);
}
if (size >= 4 && data[0] == 'R' && data[1] == 'N' && data[2] == 'C' &&
(data[3] == 1 || data[3] == 2)) {
if (size < 18) {
output.clear();
return false;
}
2026-11-30 22:31:51 +02:00
if (data[3] == 1) return unpack_rnc1(data, size, output);
return unpack_rnc2(data, size, output);
2026-11-30 22:31:51 +02:00
}
output.assign(data, data + size);
return true;
}
} // namespace top_octane::assets