summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorinspectredc <inspectredc@gmail.com>2024-12-31 00:18:49 +0000
committerLywx <kiritodev01@gmail.com>2024-12-30 19:53:17 -0600
commitc5e1b1e7748d3ecb78c3913b5acec8afc6f10d8b (patch)
treeebf7cc26d2e3a9b796504a0f54356fc3e93fe79a
parent608b06ba52dae7219f651b299f195c5dbd7c8313 (diff)
Fix Crc after comptool
-rw-r--r--src/preprocess/CompTool.cpp41
-rw-r--r--src/preprocess/CompTool.h3
2 files changed, 42 insertions, 2 deletions
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 <fstream>
#include <cstring>
+#include <stdexcept>
uint32_t CompTool::FindFileTable(std::vector<uint8_t>& 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<uint8_t>& rom) {
throw std::runtime_error("Failed to find file table");
}
+#define ROL(i, b) ((i << (b)) | (i >> (32 - (b))))
+
+std::pair<uint32_t, uint32_t> 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<uint8_t> CompTool::Decompress(std::vector<uint8_t> rom){
LUS::BinaryReader basefile((char*) rom.data(), rom.size());
basefile.SetEndianness(Torch::Endianness::Big);
@@ -78,9 +112,12 @@ std::vector<uint8_t> CompTool::Decompress(std::vector<uint8_t> 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() };
diff --git a/src/preprocess/CompTool.h b/src/preprocess/CompTool.h
index 15b7db5..73052bb 100644
--- a/src/preprocess/CompTool.h
+++ b/src/preprocess/CompTool.h
@@ -1,5 +1,6 @@
#pragma once
+#include "lib/binarytools/BinaryWriter.h"
#include <cstdint>
#include <string>
#include <vector>
@@ -15,4 +16,6 @@ public:
static std::vector<uint8_t> Decompress(std::vector<uint8_t> rom);
private:
static uint32_t FindFileTable(std::vector<uint8_t>& rom);
+ static std::pair<uint32_t, uint32_t> CalculateCRCs(LUS::BinaryWriter& decompFile);
+ static inline const uint32_t sCrcSeed = 0xF8CA4DDC;
}; \ No newline at end of file