From 73151a57536d7ce7e0b11904b7ab5467cd1fe466 Mon Sep 17 00:00:00 2001 From: Starsam80 Date: Wed, 29 Dec 2021 18:28:54 -0700 Subject: [PATCH] NANDImporter: Don't pass paths if we don't need to --- Source/Core/DiscIO/NANDImporter.cpp | 35 ++++++++++++++++++----------- Source/Core/DiscIO/NANDImporter.h | 3 +-- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/Source/Core/DiscIO/NANDImporter.cpp b/Source/Core/DiscIO/NANDImporter.cpp index 322dd548d7..1691ed5410 100644 --- a/Source/Core/DiscIO/NANDImporter.cpp +++ b/Source/Core/DiscIO/NANDImporter.cpp @@ -140,44 +140,53 @@ void NANDImporter::ProcessEntry(u16 entry_number, const std::string& parent_path Type type = static_cast(entry.mode & 3); if (type == Type::File) - ProcessFile(entry, path); + { + std::vector data = GetEntryData(entry); + File::IOFile file(m_nand_root + path, "wb"); + file.WriteBytes(data.data(), data.size()); + } else if (type == Type::Directory) - ProcessDirectory(entry, path); + { + File::CreateDir(m_nand_root + path); + ProcessEntry(entry.sub, path); + } else + { ERROR_LOG_FMT(DISCIO, "Ignoring unknown entry type for {}", entry); + } entry_number = entry.sib; } } -void NANDImporter::ProcessDirectory(const NANDFSTEntry& entry, const std::string& path) -{ - File::CreateDir(m_nand_root + path); - ProcessEntry(entry.sub, path); -} - -void NANDImporter::ProcessFile(const NANDFSTEntry& entry, const std::string& path) +std::vector NANDImporter::GetEntryData(const NANDFSTEntry& entry) { constexpr size_t NAND_AES_KEY_OFFSET = 0x158; constexpr size_t NAND_FAT_BLOCK_SIZE = 0x4000; - File::IOFile file(m_nand_root + path, "wb"); std::array key{}; std::copy(&m_nand_keys[NAND_AES_KEY_OFFSET], &m_nand_keys[NAND_AES_KEY_OFFSET + key.size()], key.begin()); + u16 sub = entry.sub; - u32 remaining_bytes = entry.size; + size_t remaining_bytes = entry.size; + std::vector data{}; + data.reserve(remaining_bytes); while (remaining_bytes > 0) { std::array iv{}; std::vector block = Common::AES::Decrypt( key.data(), iv.data(), &m_nand[NAND_FAT_BLOCK_SIZE * sub], NAND_FAT_BLOCK_SIZE); - u32 size = remaining_bytes < NAND_FAT_BLOCK_SIZE ? remaining_bytes : NAND_FAT_BLOCK_SIZE; - file.WriteBytes(block.data(), size); + + size_t size = std::min(remaining_bytes, block.size()); + data.insert(data.end(), block.begin(), block.begin() + size); remaining_bytes -= size; + sub = Common::swap16(&m_nand[m_nand_fat_offset + 2 * sub]); } + + return data; } bool NANDImporter::ExtractCertificates() diff --git a/Source/Core/DiscIO/NANDImporter.h b/Source/Core/DiscIO/NANDImporter.h index eeb12c87bc..2ebbbe1463 100644 --- a/Source/Core/DiscIO/NANDImporter.h +++ b/Source/Core/DiscIO/NANDImporter.h @@ -55,8 +55,7 @@ private: std::string GetPath(const NANDFSTEntry& entry, const std::string& parent_path); std::string FormatDebugString(const NANDFSTEntry& entry); void ProcessEntry(u16 entry_number, const std::string& parent_path); - void ProcessFile(const NANDFSTEntry& entry, const std::string& path); - void ProcessDirectory(const NANDFSTEntry& entry, const std::string& path); + std::vector GetEntryData(const NANDFSTEntry& entry); void ExportKeys(); std::string m_nand_root;