Make ctrff usable again

This commit is contained in:
2025-12-06 21:34:19 +01:00
parent b021609c4c
commit bfa6c9e92a
15 changed files with 97 additions and 91 deletions

View File

@@ -5,14 +5,14 @@
namespace ctrff {
namespace LZ11 {
PD::u32 GetOccurenceLength(const PD::u8* new_ptr, size_t new_len,
const PD::u8* old_ptr, size_t old_len,
PD::u32& disp) {
ctrff::u32 GetOccurenceLength(const ctrff::u8* new_ptr, size_t new_len,
const ctrff::u8* old_ptr, size_t old_len,
ctrff::u32& disp) {
disp = 0;
if (new_len == 0) {
return 0;
}
PD::u32 res = 0;
ctrff::u32 res = 0;
if (old_len > 0) {
for (size_t i = 0; i < old_len - 1; i++) {
auto ref = old_ptr + i;
@@ -35,20 +35,20 @@ PD::u32 GetOccurenceLength(const PD::u8* new_ptr, size_t new_len,
return res;
}
CTRFF_API std::vector<PD::u8> Compress(const std::vector<PD::u8>& in) {
CTRFF_API std::vector<ctrff::u8> Compress(const std::vector<ctrff::u8>& in) {
if (in.size() > 0xFFFFFF) {
std::cout << "ERROR: LZ11 input is too large!" << std::endl;
return std::vector<PD::u8>();
return std::vector<ctrff::u8>();
}
std::stringstream s;
// SETUP HEADER //
s << static_cast<PD::u8>(0x11);
s << static_cast<PD::u8>(in.size() & 0xFF);
s << static_cast<PD::u8>((in.size() >> 8) & 0xFF);
s << static_cast<PD::u8>((in.size() >> 16) & 0xFF);
s << static_cast<ctrff::u8>(0x11);
s << static_cast<ctrff::u8>(in.size() & 0xFF);
s << static_cast<ctrff::u8>((in.size() >> 8) & 0xFF);
s << static_cast<ctrff::u8>((in.size() >> 16) & 0xFF);
size_t res_len = 4; // 4-byte header
PD::u8 out_buf[0x21]; // 33 bytes
size_t res_len = 4; // 4-byte header
ctrff::u8 out_buf[0x21]; // 33 bytes
out_buf[0] = 0;
size_t obl = 1; // out_buf_len
size_t buf_blocks = 0;
@@ -63,7 +63,7 @@ CTRFF_API std::vector<PD::u8> Compress(const std::vector<PD::u8>& in) {
buf_blocks = 0;
}
PD::u32 disp = 0;
ctrff::u32 disp = 0;
size_t old_len = std::min(rb, static_cast<size_t>(0x1000));
size_t len = LZ11::GetOccurenceLength(
in.data() + rb, std::min(in.size() - rb, static_cast<size_t>(0x10110)),
@@ -73,21 +73,22 @@ CTRFF_API std::vector<PD::u8> Compress(const std::vector<PD::u8>& in) {
out_buf[obl++] = in[rb++];
} else {
rb += len;
out_buf[0] |= static_cast<PD::u8>(1 << (7 - buf_blocks));
out_buf[0] |= static_cast<ctrff::u8>(1 << (7 - buf_blocks));
if (len > 0x110) {
out_buf[obl++] =
0x10 | static_cast<PD::u8>(((len - 0x111) >> 12) & 0x0F);
out_buf[obl++] = static_cast<PD::u8>(((len - 0x111) >> 4) & 0xFF);
out_buf[obl] = static_cast<PD::u8>(((len - 0x111) << 4) & 0xF0);
0x10 | static_cast<ctrff::u8>(((len - 0x111) >> 12) & 0x0F);
out_buf[obl++] = static_cast<ctrff::u8>(((len - 0x111) >> 4) & 0xFF);
out_buf[obl] = static_cast<ctrff::u8>(((len - 0x111) << 4) & 0xF0);
} else if (len > 0x10) {
out_buf[obl++] = 0x00 | static_cast<PD::u8>(((len - 0x11) >> 4) & 0x0F);
out_buf[obl] = static_cast<PD::u8>(((len - 0x11) << 4) & 0xF0);
out_buf[obl++] =
0x00 | static_cast<ctrff::u8>(((len - 0x11) >> 4) & 0x0F);
out_buf[obl] = static_cast<ctrff::u8>(((len - 0x11) << 4) & 0xF0);
} else {
out_buf[obl] |= static_cast<PD::u8>(((len - 1) << 4) & 0xF0);
out_buf[obl] |= static_cast<ctrff::u8>(((len - 1) << 4) & 0xF0);
}
obl++;
out_buf[obl++] = static_cast<PD::u8>(((disp - 1) >> 8) & 0x0F);
out_buf[obl++] = static_cast<PD::u8>((disp - 1) & 0xFF);
out_buf[obl++] = static_cast<ctrff::u8>(((disp - 1) >> 8) & 0x0F);
out_buf[obl++] = static_cast<ctrff::u8>((disp - 1) & 0xFF);
}
buf_blocks++;
}
@@ -98,13 +99,13 @@ CTRFF_API std::vector<PD::u8> Compress(const std::vector<PD::u8>& in) {
}
if (res_len % 4 != 0) {
PD::u32 pad_len = 4 - (res_len % 4);
PD::u8 pad[4] = {0};
ctrff::u32 pad_len = 4 - (res_len % 4);
ctrff::u8 pad[4] = {0};
s.write(reinterpret_cast<const char*>(pad), pad_len);
res_len += pad_len;
}
std::vector<PD::u8> res(res_len);
std::vector<ctrff::u8> res(res_len);
s.read(reinterpret_cast<char*>(res.data()), res.size());
return res;
}