From ec6968fcb4fcd989e49328f5afa958a891ad66f9 Mon Sep 17 00:00:00 2001 From: tobid7 Date: Mon, 29 Dec 2025 14:02:46 +0100 Subject: [PATCH] FIx issues with lz11 compression (Still WIP) also fixed compiling on mingw for now Readded palladium for the desktop tools --- .gitmodules | 3 ++ CMakeLists.txt | 3 +- README.md | 2 +- include/ctrff/types.hpp | 6 ++-- source/lz11.cpp | 76 ++++++++++++++++++++++++----------------- vendor/palladium | 1 + 6 files changed, 54 insertions(+), 37 deletions(-) create mode 160000 vendor/palladium diff --git a/.gitmodules b/.gitmodules index 8debfd6..f4115d5 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "vendor/cli-fancy"] path = vendor/cli-fancy url = https://dev.npid7.de/tobid7/cli-fancy +[submodule "vendor/palladium"] + path = vendor/palladium + url = https://github.com/tobid7/palladium diff --git a/CMakeLists.txt b/CMakeLists.txt index 5f1af06..cf7cc3e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,9 +33,10 @@ add_library(ctrff STATIC target_include_directories(ctrff PUBLIC include) if(${CTRFF_DESKTOP}) + add_subdirectory(vendor/palladium) add_executable(ctrff-cli tool/main.cpp) target_include_directories(ctrff-cli PUBLIC include vendor/stb vendor/cli-fancy/include) - target_link_libraries(ctrff-cli PUBLIC ctrff) + target_link_libraries(ctrff-cli PUBLIC ctrff palladium) add_executable(test tool/test.cpp) target_include_directories(test PUBLIC include vendor/stb vendor/cli-fancy/include) target_link_libraries(test PUBLIC ctrff) diff --git a/README.md b/README.md index 3f4562f..6efa807 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ Not all Planned formates are listed here yet | bcstm | Loading of almost every Data | Fully done and playable by BCSTM-Player | | bcwav | Basic Loading (not tested yet) | Not finished yet | | bclim | Nothing done yet (Started creating header) | | -| lz11 | Encoder done, Decoder missing | Files are bit diffrent to the ones bannertool generates (don't know why) | +| lz11 | Encoder done, Decoder missing | WIP | | romfs | Nothing Done yet (Started creating header) | | | smdh | Almost done | missing safetey checks | | cbmd | Nothing done yet | | diff --git a/include/ctrff/types.hpp b/include/ctrff/types.hpp index 35ac3a7..29ee1d4 100644 --- a/include/ctrff/types.hpp +++ b/include/ctrff/types.hpp @@ -23,7 +23,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ - +/* #ifdef _WIN32 // Windows (MSVC Tested) #ifdef CTRFF_BUILD_SHARED #define CTRFF_API __declspec(dllexport) @@ -45,9 +45,9 @@ SOFTWARE. #elif defined(__3DS__) // 3ds Specific // Only Static supported #define CTRFF_API -#else +#else*/ #define CTRFF_API -#endif +//#endif #include #include diff --git a/source/lz11.cpp b/source/lz11.cpp index 6ee954c..27851b2 100644 --- a/source/lz11.cpp +++ b/source/lz11.cpp @@ -5,8 +5,8 @@ namespace ctrff { namespace LZ11 { -ctrff::u32 GetOccurenceLength(const ctrff::u8* new_ptr, size_t new_len, - const ctrff::u8* old_ptr, size_t old_len, +ctrff::u32 GetOccurenceLength(const ctrff::u8* new_ptr, u32 new_len, + const ctrff::u8* old_ptr, u32 old_len, ctrff::u32& disp) { disp = 0; if (new_len == 0) { @@ -14,11 +14,11 @@ ctrff::u32 GetOccurenceLength(const ctrff::u8* new_ptr, size_t new_len, } ctrff::u32 res = 0; if (old_len > 0) { - for (size_t i = 0; i < old_len - 1; i++) { + for (u32 i = 0; i < old_len - 1; i++) { auto ref = old_ptr + i; - size_t len = 0; - for (size_t j = 0; j < new_len; j++) { - if (*(ref + j) != (*new_ptr + j)) { + u32 len = 0; + for (u32 j = 0; j < new_len; j++) { + if (*(ref + j) != *(new_ptr + j)) { break; } len++; @@ -41,32 +41,41 @@ CTRFF_API std::vector Compress(const std::vector& in) { return std::vector(); } std::stringstream s; + struct header { + u8 Magic; + u8 Size1; + u8 Size2; + u8 Size3; + }; + header hdr; + hdr.Magic = 0x11; + hdr.Size1 = in.size() & 0xFF; + hdr.Size2 = (in.size() >> 8) & 0xFF; + hdr.Size3 = (in.size() >> 16) & 0xFF; // SETUP HEADER // - s << static_cast(0x11); - s << static_cast(in.size() & 0xFF); - s << static_cast((in.size() >> 8) & 0xFF); - s << static_cast((in.size() >> 16) & 0xFF); + s.write(reinterpret_cast(&hdr), sizeof(hdr)); - size_t res_len = 4; // 4-byte header + u32 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; - size_t rb = 0; + memset(out_buf, 0x0, sizeof(out_buf)); + u32 obl = 1; // out_buf_len + u32 buf_blocks = 0; + u32 rb = 0; while (rb < in.size()) { - if (buf_blocks == 8 || obl >= sizeof(out_buf) - 3) { + if (buf_blocks == 8) { s.write(reinterpret_cast(out_buf), obl); res_len += obl; - out_buf[0] = 0; + memset(out_buf, 0x0, sizeof(out_buf)); obl = 1; buf_blocks = 0; } ctrff::u32 disp = 0; - size_t old_len = std::min(rb, static_cast(0x1000)); - size_t len = LZ11::GetOccurenceLength( - in.data() + rb, std::min(in.size() - rb, static_cast(0x10110)), + u32 old_len = std::min(rb, static_cast(0x1000)); + u32 len = LZ11::GetOccurenceLength( + in.data() + rb, + std::min((u32)in.size() - rb, static_cast(0x10110)), in.data() + rb - old_len, old_len, disp); if (len < 3) { @@ -75,20 +84,24 @@ CTRFF_API std::vector Compress(const std::vector& in) { rb += len; out_buf[0] |= static_cast(1 << (7 - buf_blocks)); if (len > 0x110) { - out_buf[obl++] = - 0x10 | static_cast(((len - 0x111) >> 12) & 0x0F); - out_buf[obl++] = static_cast(((len - 0x111) >> 4) & 0xFF); + out_buf[obl] = 0x10; + out_buf[obl] |= static_cast(((len - 0x111) >> 12) & 0x0F); + obl++; + out_buf[obl] = static_cast(((len - 0x111) >> 4) & 0xFF); + obl++; out_buf[obl] = static_cast(((len - 0x111) << 4) & 0xF0); } else if (len > 0x10) { - out_buf[obl++] = - 0x00 | static_cast(((len - 0x11) >> 4) & 0x0F); - out_buf[obl] = static_cast(((len - 0x11) << 4) & 0xF0); + out_buf[obl] = 0x00; + out_buf[obl] |= static_cast(((len - 0x111) >> 4) & 0x0F); + obl++; + out_buf[obl] = static_cast(((len - 0x111) << 4) & 0xF0); } else { - out_buf[obl] |= static_cast(((len - 1) << 4) & 0xF0); + out_buf[obl] = static_cast(((len - 1) << 4) & 0xF0); } + out_buf[obl] |= static_cast(((disp - 1) >> 8) & 0x0F); + obl++; + out_buf[obl] = static_cast((disp - 1) & 0xFF); obl++; - out_buf[obl++] = static_cast(((disp - 1) >> 8) & 0x0F); - out_buf[obl++] = static_cast((disp - 1) & 0xFF); } buf_blocks++; } @@ -105,9 +118,8 @@ CTRFF_API std::vector Compress(const std::vector& in) { res_len += pad_len; } - std::vector res(res_len); - s.read(reinterpret_cast(res.data()), res.size()); - return res; + std::string tmp = s.str(); + return std::vector(tmp.begin(), tmp.end()); } } // namespace LZ11 } // namespace ctrff \ No newline at end of file diff --git a/vendor/palladium b/vendor/palladium new file mode 160000 index 0000000..3575a67 --- /dev/null +++ b/vendor/palladium @@ -0,0 +1 @@ +Subproject commit 3575a6787d85115ab88e4f159d0bba9cfbba5ad8