Fix Crc after comptool

This commit is contained in:
inspectredc
2024-12-30 19:53:17 -06:00
committed by Lywx
parent 608b06ba52
commit c5e1b1e774
2 changed files with 42 additions and 2 deletions
+39 -2
View File
@@ -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() };
+3
View File
@@ -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;
};