317 lines
9.4 KiB
C++
317 lines
9.4 KiB
C++
#include "assets/rnc.hpp"
|
|||
|
|
#include "core/log.hpp"
|
||
|
|
|
||
|
|
#include <algorithm>
|
||
|
|
#include <array>
|
||
|
|
#include <cstring>
|
||
|
|
|
||
|
|
namespace top_octane::assets {
|
||
|
|
|
||
|
|
namespace {
|
||
|
|
|
||
|
|
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;
|
||
|
|
uint32_t bitbuf = 0;
|
||
|
|
int bitcnt = 0;
|
||
|
|
|
||
|
|
void init(const uint8_t* buf) {
|
||
|
|
src = buf;
|
||
|
|
pos = 0;
|
||
|
|
bitbuf = read_le16(src);
|
||
|
|
bitcnt = 16;
|
||
|
|
}
|
||
|
|
|
||
|
|
void advance(int n) {
|
||
|
|
bitbuf >>= n;
|
||
|
|
bitcnt -= n;
|
||
|
|
if (bitcnt < 16) {
|
||
|
|
pos += 2;
|
||
|
|
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() {
|
||
|
|
bitcnt -= 16;
|
||
|
|
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 (num == 0) return true;
|
||
|
|
std::array<uint8_t, 32> lens{};
|
||
|
|
for (uint32_t i = 0; i < num; ++i) {
|
||
|
|
lens[i] = static_cast<uint8_t>(bs.read(0x0F, 4));
|
||
|
|
}
|
||
|
|
|
||
|
|
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 true;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool read_huff(Rnc1BitStream& bs, const std::vector<HuffEntry>& tbl, uint32_t& out_val) {
|
||
|
|
for (const auto& e : tbl) {
|
||
|
|
if ((bs.bitbuf & e.mask) == e.code) {
|
||
|
|
bs.advance(e.bits);
|
||
|
|
if (e.sym < 2) {
|
||
|
|
out_val = e.sym;
|
||
|
|
} else {
|
||
|
|
int extra = e.sym - 1;
|
||
|
|
uint32_t mask = (1u << extra) - 1u;
|
||
|
|
out_val = bs.read(mask, extra) | (1u << extra);
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
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 (18u + packed_len > size) {
|
||
|
|
LOG_ERROR("RNC1 truncated: need %u, got %zu", 18u + packed_len, size);
|
||
|
|
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
|
||
|
|
std::vector<uint8_t> src(packed_len + 8, 0);
|
||
|
|
std::memcpy(src.data(), data + 18, packed_len);
|
||
|
|
|
||
|
|
Rnc1BitStream bs;
|
||
|
|
bs.init(src.data());
|
||
|
|
bs.read(3, 2); // lock & key bits
|
||
|
|
|
||
|
|
output.clear();
|
||
|
|
output.reserve(unpacked_len);
|
||
|
|
|
||
|
|
std::vector<HuffEntry> raw_t, dist_t, len_t;
|
||
|
|
for (uint8_t chunk = 0; chunk < pack_chunks; ++chunk) {
|
||
|
|
build_huff_table(bs, raw_t);
|
||
|
|
build_huff_table(bs, dist_t);
|
||
|
|
build_huff_table(bs, len_t);
|
||
|
|
int subchunks = static_cast<int>(bs.read(0xFFFF, 16));
|
||
|
|
|
||
|
|
while (true) {
|
||
|
|
uint32_t raw_len = 0;
|
||
|
|
if (!read_huff(bs, raw_t, raw_len)) {
|
||
|
|
LOG_ERROR("RNC1 raw huffman decode failure");
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
if (raw_len > 0) {
|
||
|
|
if (bs.pos + raw_len > packed_len + 4) {
|
||
|
|
LOG_ERROR("RNC1 raw copy OOB");
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
output.insert(output.end(), src.data() + bs.pos, src.data() + bs.pos + raw_len);
|
||
|
|
bs.pos += raw_len;
|
||
|
|
bs.fix();
|
||
|
|
}
|
||
|
|
--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");
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
dist += 1;
|
||
|
|
mlen += 2;
|
||
|
|
if (dist > output.size()) {
|
||
|
|
LOG_ERROR("RNC1 back-reference OOB: dist=%u size=%zu", dist, output.size());
|
||
|
|
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);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
if (rnc_crc16(output.data(), output.size()) != unpacked_crc) {
|
||
|
|
LOG_ERROR("RNC1 unpacked CRC16 mismatch");
|
||
|
|
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 (18u + packed_len > size) return false;
|
||
|
|
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;
|
||
|
|
auto getbit = [&]() -> int {
|
||
|
|
if (bitcnt == 0) {
|
||
|
|
bitbuf = (pos < packed_len) ? src[pos++] : 0;
|
||
|
|
bitcnt = 8;
|
||
|
|
}
|
||
|
|
int bit = (bitbuf >> 7) & 1;
|
||
|
|
bitbuf <<= 1;
|
||
|
|
--bitcnt;
|
||
|
|
return bit;
|
||
|
|
};
|
||
|
|
auto getbyte = [&]() -> uint8_t {
|
||
|
|
return (pos < packed_len) ? src[pos++] : 0;
|
||
|
|
};
|
||
|
|
|
||
|
|
output.clear();
|
||
|
|
output.reserve(unpacked_len);
|
||
|
|
getbit();
|
||
|
|
getbit();
|
||
|
|
|
||
|
|
while (true) {
|
||
|
|
while (getbit() == 0) {
|
||
|
|
output.push_back(getbyte());
|
||
|
|
}
|
||
|
|
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) {
|
||
|
|
// raw run of 12..72 bytes
|
||
|
|
if (getbit() == 0) {
|
||
|
|
len = 2;
|
||
|
|
int b0 = getbit(), b1 = getbit(), b2 = getbit();
|
||
|
|
(void)b0; (void)b1; (void)b2;
|
||
|
|
// fallback unused by Hi-Octane (which uses RNC1 exclusively)
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
if (dist > output.size()) return false;
|
||
|
|
for (uint32_t i = 0; i < len; ++i) {
|
||
|
|
output.push_back(output[output.size() - dist]);
|
||
|
|
}
|
||
|
|
if (output.size() >= unpacked_len) break;
|
||
|
|
}
|
||
|
|
return output.size() == unpacked_len && rnc_crc16(output.data(), output.size()) == unpacked_crc;
|
||
|
|
}
|
||
|
|
|
||
|
|
} // 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 (size >= 18 && data[0] == 'R' && data[1] == 'N' && data[2] == 'C') {
|
||
|
|
if (data[3] == 1) return unpack_rnc1(data, size, output);
|
||
|
|
if (data[3] == 2) return unpack_rnc2(data, size, output);
|
||
|
|
}
|
||
|
|
output.assign(data, data + size);
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace top_octane::assets
|