From 837072f80f79040bf9750a5cec9c565798bd0811 Mon Sep 17 00:00:00 2001 From: louist103 <35883445+louist103@users.noreply.github.com> Date: Thu, 26 Oct 2023 20:39:26 -0400 Subject: [PATCH] Check for compressed files in the extractor (#3292) * Check for zip/rar header in the extractor. * 7z --- soh/soh/Extractor/Extract.cpp | 26 ++++++++++++++++++++++++++ soh/soh/Extractor/Extract.h | 2 ++ 2 files changed, 28 insertions(+) diff --git a/soh/soh/Extractor/Extract.cpp b/soh/soh/Extractor/Extract.cpp index d283bc3cc..c79d26dad 100644 --- a/soh/soh/Extractor/Extract.cpp +++ b/soh/soh/Extractor/Extract.cpp @@ -109,6 +109,10 @@ void Extractor::ShowCrcErrorBox() const { ShowErrorBox("Rom CRC invalid", "Rom CRC did not match the list of known good roms. Please find another."); } +void Extractor::ShowCompressedErrorBox() const { + ShowErrorBox("File is Compressed", "The selected file appears to be compressed. Please extract before using."); +} + int Extractor::ShowRomPickBox(uint32_t verCrc) const { std::unique_ptr boxBuffer = std::make_unique(mCurrentRomPath.size() + 100); SDL_MessageBoxData boxData = { 0 }; @@ -328,6 +332,24 @@ bool Extractor::ValidateAndFixRom() { return false; } +// The file box will only allow selecting an n64 rom but typing in the file name will allow selecting anything. +bool Extractor::ValidateNotCompressed() const { + // ZIP file header + if (mRomData[0] == 'P' && mRomData[1] == 'K' && mRomData[2] == 0x03 && mRomData[3] == 0x04) { + return false; + } + // RAR file header. Only the first 4 bytes. + if (mRomData[0] == 'R' && mRomData[1] == 'a' && mRomData[2] == 'r' && mRomData[3] == 0x21) { + return false; + } + // 7z file header. 37 7A BC AF 27 1C + if (mRomData[0] == '7' && mRomData[1] == 'z' && mRomData[2] == 0xBC && mRomData[3] == 0xAF && mRomData[4] == 0x27 && mRomData[5] == 0x1C) { + return false; + } + + return true; +} + bool Extractor::ValidateRomSize() const { if (mCurRomSize != MB32 && mCurRomSize != MB54 && mCurRomSize != MB64) { return false; @@ -336,6 +358,10 @@ bool Extractor::ValidateRomSize() const { } bool Extractor::ValidateRom(bool skipCrcTextBox) { + if (!ValidateNotCompressed()) { + ShowCompressedErrorBox(); + return false; + } if (!ValidateRomSize()) { ShowSizeErrorBox(); return false; diff --git a/soh/soh/Extractor/Extract.h b/soh/soh/Extractor/Extract.h index 6c9b4a078..4ff7cb92f 100644 --- a/soh/soh/Extractor/Extract.h +++ b/soh/soh/Extractor/Extract.h @@ -38,6 +38,7 @@ class Extractor { bool ValidateRomSize() const; bool ValidateRom(bool skipCrcBox = false); + bool ValidateNotCompressed() const; const char* GetZapdVerStr() const; void SetRomInfo(const std::string& path); @@ -46,6 +47,7 @@ class Extractor { void GetRoms(std::vector& roms); void ShowSizeErrorBox() const; void ShowCrcErrorBox() const; + void ShowCompressedErrorBox() const; int ShowRomPickBox(uint32_t verCrc) const; bool ManuallySearchForRom(); bool ManuallySearchForRomMatchingType(RomSearchMode searchMode);