From c5e1b1e7748d3ecb78c3913b5acec8afc6f10d8b Mon Sep 17 00:00:00 2001 From: inspectredc Date: Tue, 31 Dec 2024 00:18:49 +0000 Subject: Fix Crc after comptool --- src/preprocess/CompTool.cpp | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) (limited to 'src/preprocess/CompTool.cpp') diff --git a/src/preprocess/CompTool.cpp b/src/preprocess/CompTool.cpp index f453050..d310e35 100644 --- a/src/preprocess/CompTool.cpp +++ b/src/preprocess/CompTool.cpp @@ -4,6 +4,7 @@ #include "lib/binarytools/BinaryReader.h" #include #include +#include uint32_t CompTool::FindFileTable(std::vector& rom) { uint8_t query_one[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x50, 0x00, 0x00, 0x00, 0x00 }; @@ -22,6 +23,39 @@ uint32_t CompTool::FindFileTable(std::vector& rom) { throw std::runtime_error("Failed to find file table"); } +#define ROL(i, b) ((i << (b)) | (i >> (32 - (b)))) + +std::pair CompTool::CalculateCRCs(LUS::BinaryWriter& decompFile) +{ + uint32_t start = 0x1000; + uint32_t end = 0x101000; + LUS::BinaryReader readFile(decompFile.GetStream()); + uint32_t t1, t2, t3, t4, t5, t6; + + // Todo: Implement other bootcodes + t1 = t2 = t3 = t4 = t5 = t6 = sCrcSeed; + + readFile.SetEndianness(Torch::Endianness::Big); + readFile.Seek(start, LUS::SeekOffsetType::Start); + + for (size_t i = start; i < end; i += sizeof(uint32_t)) { + uint32_t d = readFile.ReadUInt32(); + uint32_t r = ROL(d, d & 0x1F); + + if ((t6 + d) < t6) { + t4 = (t4 + 1); + } + + t3 ^= d; + t6 += d; + t2 ^= ((t2 > d) ? r : (t6 ^ d)); + t5 += r; + t1 += (t5 ^ d); + } + + return std::make_pair((uint32_t)(t6 ^ t4 ^ t3), (uint32_t)(t5 ^ t2 ^ t1)); +} + std::vector CompTool::Decompress(std::vector rom){ LUS::BinaryReader basefile((char*) rom.data(), rom.size()); basefile.SetEndianness(Torch::Endianness::Big); @@ -78,9 +112,12 @@ std::vector CompTool::Decompress(std::vector rom){ count++; } + auto crcs = CompTool::CalculateCRCs(decompfile); + decompfile.Seek(0x10, LUS::SeekOffsetType::Start); - decompfile.Write(0xA7D5F194); // CRC1 - decompfile.Write(0xFE3DF761); // CRC2 + + decompfile.Write(crcs.first); // CRC1 + decompfile.Write(crcs.second); // CRC2 auto result = decompfile.ToVector(); return { (uint8_t*) result.data(), (uint8_t*) result.data() + result.size() }; -- cgit v1.2.3