From 1fe45d890cd93159db7a0b235ca832cc74be1ab5 Mon Sep 17 00:00:00 2001 From: Pengfei Date: Wed, 4 Aug 2021 11:25:39 +0800 Subject: [PATCH] More refactor going on --- src/core/CMakeLists.txt | 8 ++--- src/core/cia_builder.cpp | 2 +- src/core/cia_builder.h | 4 +-- ...quick_decryptor.cpp => file_decryptor.cpp} | 31 +++++++++---------- .../{quick_decryptor.h => file_decryptor.h} | 7 +++-- src/core/file_sys/data/extdata.cpp | 2 +- src/core/file_sys/ncch_container.cpp | 2 +- src/core/file_sys/ncch_container.h | 4 +-- src/core/importer.cpp | 29 ++++++++--------- src/core/importer.h | 2 +- .../{decryptor.cpp => sdmc_decryptor.cpp} | 12 +++---- src/core/{decryptor.h => sdmc_decryptor.h} | 4 +-- src/frontend/utilities.cpp | 2 +- 13 files changed, 55 insertions(+), 54 deletions(-) rename src/core/{quick_decryptor.cpp => file_decryptor.cpp} (83%) rename src/core/{quick_decryptor.h => file_decryptor.h} (96%) rename src/core/{decryptor.cpp => sdmc_decryptor.cpp} (93%) rename src/core/{decryptor.h => sdmc_decryptor.h} (97%) diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 6db6a4b..b2f3b09 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -7,8 +7,8 @@ add_library(core STATIC db/title_db.h db/title_keys_bin.cpp db/title_keys_bin.h - decryptor.cpp - decryptor.h + file_decryptor.cpp + file_decryptor.h file_sys/certificate.cpp file_sys/certificate.h file_sys/cia_common.h @@ -35,8 +35,8 @@ add_library(core STATIC key/arithmetic128.h key/key.cpp key/key.h - quick_decryptor.cpp - quick_decryptor.h + sdmc_decryptor.cpp + sdmc_decryptor.h ) target_link_libraries(core PRIVATE common cryptopp) diff --git a/src/core/cia_builder.cpp b/src/core/cia_builder.cpp index 0b3919e..552a21c 100644 --- a/src/core/cia_builder.cpp +++ b/src/core/cia_builder.cpp @@ -282,7 +282,7 @@ bool CIABuilder::AddContent(u16 content_id, NCCHContainer& ncch) { const bool is_encrypted = static_cast(tmd_chunk.type) & 0x01; // For encrypted content, the hashes are calculated before CIA/CDN encryption. - // So we have to add hash calculation to the CryptoFunc of the QuickDecryptor. + // So we have to add hash calculation to the CryptoFunc of the FileDecryptor. // For unencrypted content, we can just use HashedFile's hashing. std::shared_ptr crypto; if (is_encrypted) { diff --git a/src/core/cia_builder.h b/src/core/cia_builder.h index 599bcc6..80cd8ea 100644 --- a/src/core/cia_builder.h +++ b/src/core/cia_builder.h @@ -10,11 +10,11 @@ #include "common/file_util.h" #include "common/progress_callback.h" #include "common/swap.h" +#include "core/file_decryptor.h" #include "core/file_sys/cia_common.h" #include "core/file_sys/ncch_container.h" #include "core/file_sys/title_metadata.h" #include "core/key/key.h" -#include "core/quick_decryptor.h" namespace Core { @@ -118,7 +118,7 @@ private: std::mutex abort_ncch_mutex; NCCHContainer* abort_ncch{}; - QuickDecryptor decryptor; + FileDecryptor decryptor; }; } // namespace Core diff --git a/src/core/quick_decryptor.cpp b/src/core/file_decryptor.cpp similarity index 83% rename from src/core/quick_decryptor.cpp rename to src/core/file_decryptor.cpp index 1d2f6d5..d13eb4a 100644 --- a/src/core/quick_decryptor.cpp +++ b/src/core/file_decryptor.cpp @@ -11,22 +11,21 @@ #include "common/assert.h" #include "common/file_util.h" #include "common/string_util.h" -#include "core/decryptor.h" -#include "core/quick_decryptor.h" +#include "core/file_decryptor.h" namespace Core { -QuickDecryptor::QuickDecryptor() = default; +FileDecryptor::FileDecryptor() = default; -QuickDecryptor::~QuickDecryptor() = default; +FileDecryptor::~FileDecryptor() = default; -void QuickDecryptor::SetCrypto(std::shared_ptr crypto_) { +void FileDecryptor::SetCrypto(std::shared_ptr crypto_) { crypto = std::move(crypto_); } -bool QuickDecryptor::CryptAndWriteFile(std::shared_ptr source_, std::size_t size, - std::shared_ptr destination_, - const Common::ProgressCallback& callback_) { +bool FileDecryptor::CryptAndWriteFile(std::shared_ptr source_, std::size_t size, + std::shared_ptr destination_, + const Common::ProgressCallback& callback_) { if (is_running) { LOG_ERROR(Core, "Decryptor is running"); return false; @@ -55,10 +54,10 @@ bool QuickDecryptor::CryptAndWriteFile(std::shared_ptr source_ is_good = is_running = true; - read_thread = std::make_unique(&QuickDecryptor::DataReadLoop, this); - write_thread = std::make_unique(&QuickDecryptor::DataWriteLoop, this); + read_thread = std::make_unique(&FileDecryptor::DataReadLoop, this); + write_thread = std::make_unique(&FileDecryptor::DataWriteLoop, this); if (crypto) { - decrypt_thread = std::make_unique(&QuickDecryptor::DataDecryptLoop, this); + decrypt_thread = std::make_unique(&FileDecryptor::DataDecryptLoop, this); } completion_event.Wait(); @@ -79,7 +78,7 @@ bool QuickDecryptor::CryptAndWriteFile(std::shared_ptr source_ return ret; } -void QuickDecryptor::DataReadLoop() { +void FileDecryptor::DataReadLoop() { std::size_t current_buffer = 0; bool is_first_run = true; @@ -113,7 +112,7 @@ void QuickDecryptor::DataReadLoop() { } } -void QuickDecryptor::DataDecryptLoop() { +void FileDecryptor::DataDecryptLoop() { std::size_t current_buffer = 0; std::size_t file_size = current_total_size; @@ -130,7 +129,7 @@ void QuickDecryptor::DataDecryptLoop() { } } -void QuickDecryptor::DataWriteLoop() { +void FileDecryptor::DataWriteLoop() { std::size_t current_buffer = 0; if (!*destination) { @@ -174,14 +173,14 @@ void QuickDecryptor::DataWriteLoop() { completion_event.Set(); } -void QuickDecryptor::Abort() { +void FileDecryptor::Abort() { if (is_running.exchange(false)) { is_good = false; completion_event.Set(); } } -void QuickDecryptor::Reset(std::size_t total_size_) { +void FileDecryptor::Reset(std::size_t total_size_) { total_size = total_size_; imported_size = 0; } diff --git a/src/core/quick_decryptor.h b/src/core/file_decryptor.h similarity index 96% rename from src/core/quick_decryptor.h rename to src/core/file_decryptor.h index 81ecef7..b2afc83 100644 --- a/src/core/quick_decryptor.h +++ b/src/core/file_decryptor.h @@ -18,13 +18,14 @@ namespace Core { class CryptoFunc; /** + * Generalized file decryptor. * Helper that reads, decrypts and writes data. This uses three threads to process the data * and call progress callbacks occasionally. */ -class QuickDecryptor { +class FileDecryptor { public: - explicit QuickDecryptor(); - ~QuickDecryptor(); + explicit FileDecryptor(); + ~FileDecryptor(); /** * Set up the crypto to use. diff --git a/src/core/file_sys/data/extdata.cpp b/src/core/file_sys/data/extdata.cpp index 32927e2..669d535 100644 --- a/src/core/file_sys/data/extdata.cpp +++ b/src/core/file_sys/data/extdata.cpp @@ -2,9 +2,9 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. -#include "core/decryptor.h" #include "core/file_sys/data/data_container.h" #include "core/file_sys/data/extdata.h" +#include "core/sdmc_decryptor.h" namespace Core { diff --git a/src/core/file_sys/ncch_container.cpp b/src/core/file_sys/ncch_container.cpp index cfc5936..d96d576 100644 --- a/src/core/file_sys/ncch_container.cpp +++ b/src/core/file_sys/ncch_container.cpp @@ -442,7 +442,7 @@ bool NCCHContainer::DecryptToFile(std::shared_ptr dest_file, } if (!is_encrypted) { - // Simply copy everything. QuickDecryptor is used for progress reporting + // Simply copy everything. FileDecryptor is used for progress reporting file->Seek(0, SEEK_SET); const auto size = file->GetSize(); diff --git a/src/core/file_sys/ncch_container.h b/src/core/file_sys/ncch_container.h index 5ee97b5..4618baa 100644 --- a/src/core/file_sys/ncch_container.h +++ b/src/core/file_sys/ncch_container.h @@ -13,7 +13,7 @@ #include "common/file_util.h" #include "common/progress_callback.h" #include "common/swap.h" -#include "core/decryptor.h" +#include "core/sdmc_decryptor.h" namespace Core { @@ -305,7 +305,7 @@ private: std::shared_ptr exefs_file; // Used for DecryptToFile - QuickDecryptor decryptor; + FileDecryptor decryptor; std::atomic_bool aborted{false}; friend class CIABuilder; diff --git a/src/core/importer.cpp b/src/core/importer.cpp index 4bd723b..a18e409 100644 --- a/src/core/importer.cpp +++ b/src/core/importer.cpp @@ -11,7 +11,6 @@ #include "core/cia_builder.h" #include "core/db/seed_db.h" #include "core/db/title_db.h" -#include "core/decryptor.h" #include "core/file_sys/certificate.h" #include "core/file_sys/data/data_container.h" #include "core/file_sys/data/extdata.h" @@ -21,6 +20,7 @@ #include "core/file_sys/title_metadata.h" #include "core/importer.h" #include "core/key/key.h" +#include "core/sdmc_decryptor.h" namespace Core { @@ -60,11 +60,11 @@ bool SDMCImporter::Init() { Certs::Load(config.certs_db_path); } - decryptor = std::make_unique(config.sdmc_path); + sdmc_decryptor = std::make_unique(config.sdmc_path); // Load SDMC Title DB { - DataContainer container(decryptor->DecryptFile("/dbs/title.db")); + DataContainer container(sdmc_decryptor->DecryptFile("/dbs/title.db")); std::vector> data; if (container.IsGood() && container.GetIVFCLevel4Data(data)) { sdmc_title_db = std::make_unique(std::move(data[0])); @@ -93,7 +93,7 @@ bool SDMCImporter::IsGood() const { } void SDMCImporter::AbortImporting() { - decryptor->Abort(); + sdmc_decryptor->Abort(); } bool SDMCImporter::ImportContent(const ContentSpecifier& specifier, @@ -169,8 +169,9 @@ bool ImportTitleGeneric(Dec& decryptor, const std::string& base_path, bool SDMCImporter::ImportTitle(const ContentSpecifier& specifier, const Common::ProgressCallback& callback) { return ImportTitleGeneric( - *decryptor, config.sdmc_path, specifier, [this, &callback](const std::string& filepath) { - return decryptor->DecryptAndWriteFile( + *sdmc_decryptor, config.sdmc_path, specifier, + [this, &callback](const std::string& filepath) { + return sdmc_decryptor->DecryptAndWriteFile( filepath, FileUtil::GetUserPath(FileUtil::UserPath::SDMCDir) + "Nintendo " @@ -185,10 +186,10 @@ bool SDMCImporter::ImportNandTitle(const ContentSpecifier& specifier, const auto base_path = config.system_titles_path.substr(0, config.system_titles_path.size() - 6); - QuickDecryptor quick_decryptor; + FileDecryptor decryptor; return ImportTitleGeneric( - quick_decryptor, base_path, specifier, - [&base_path, &quick_decryptor, &callback](const std::string& filepath) { + decryptor, base_path, specifier, + [&base_path, &decryptor, &callback](const std::string& filepath) { const auto physical_path = base_path + filepath.substr(1); const auto citra_path = FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) + "00000000000000000000000000000000" + filepath; @@ -197,7 +198,7 @@ bool SDMCImporter::ImportNandTitle(const ContentSpecifier& specifier, return false; } // Crypto is not set: plain copy with progress. - return quick_decryptor.CryptAndWriteFile( + return decryptor.CryptAndWriteFile( std::make_shared(physical_path, "rb"), FileUtil::GetSize(physical_path), std::make_shared(citra_path, "wb"), callback); @@ -208,7 +209,7 @@ bool SDMCImporter::ImportSavegame(u64 id, [[maybe_unused]] const Common::ProgressCallback& callback) { const auto path = fmt::format("title/{:08x}/{:08x}/data/", (id >> 32), (id & 0xFFFFFFFF)); - DataContainer container(decryptor->DecryptFile(fmt::format("/{}00000001.sav", path))); + DataContainer container(sdmc_decryptor->DecryptFile(fmt::format("/{}00000001.sav", path))); if (!container.IsGood()) { return false; } @@ -258,7 +259,7 @@ bool SDMCImporter::ImportNandSavegame(u64 id, bool SDMCImporter::ImportExtdata(u64 id, [[maybe_unused]] const Common::ProgressCallback& callback) { const auto path = fmt::format("extdata/{:08x}/{:08x}/", (id >> 32), (id & 0xFFFFFFFF)); - Extdata extdata("/" + path, *decryptor); + Extdata extdata("/" + path, *sdmc_decryptor); if (!extdata.IsGood()) { return false; } @@ -508,7 +509,7 @@ bool SDMCImporter::LoadTMD(ContentType type, u64 id, TitleMetadata& out) const { } return out.Load(file.GetData()); } else { - return out.Load(decryptor->DecryptFile(tmd_path.substr(config.sdmc_path.size() - 1))); + return out.Load(sdmc_decryptor->DecryptFile(tmd_path.substr(config.sdmc_path.size() - 1))); } } @@ -849,7 +850,7 @@ void SDMCImporter::ListTitle(std::vector& out) const { // Savegames can be uninitialized. // TODO: Is there a better way of checking this other than performing the // decryption? (Very costy) - DataContainer container(decryptor->DecryptFile( + DataContainer container(sdmc_decryptor->DecryptFile( fmt::format("/title/{:08x}/{}/data/00000001.sav", high_id, virtual_name))); if (!container.IsGood()) { return true; diff --git a/src/core/importer.h b/src/core/importer.h index b60969b..6c44c06 100644 --- a/src/core/importer.h +++ b/src/core/importer.h @@ -205,7 +205,7 @@ private: bool is_good{}; Config config; - std::unique_ptr decryptor; + std::unique_ptr sdmc_decryptor; std::unique_ptr cia_builder; std::mutex cia_builder_mutex; diff --git a/src/core/decryptor.cpp b/src/core/sdmc_decryptor.cpp similarity index 93% rename from src/core/decryptor.cpp rename to src/core/sdmc_decryptor.cpp index b7a4952..b38e39d 100644 --- a/src/core/decryptor.cpp +++ b/src/core/sdmc_decryptor.cpp @@ -12,8 +12,8 @@ #include "common/assert.h" #include "common/file_util.h" #include "common/string_util.h" -#include "core/decryptor.h" #include "core/key/key.h" +#include "core/sdmc_decryptor.h" namespace Core { @@ -49,7 +49,7 @@ std::array GetFileCTR(const std::string& path) { } // namespace void SDMCDecryptor::Reset(std::size_t total_size) { - quick_decryptor.Reset(total_size); + file_decryptor.Reset(total_size); } bool SDMCDecryptor::DecryptAndWriteFile(const std::string& source, const std::string& destination, @@ -61,17 +61,17 @@ bool SDMCDecryptor::DecryptAndWriteFile(const std::string& source, const std::st auto key = Key::GetNormalKey(Key::SDKey); auto ctr = GetFileCTR(source); - quick_decryptor.SetCrypto(CreateCTRCrypto(key, ctr)); + file_decryptor.SetCrypto(CreateCTRCrypto(key, ctr)); auto source_file = std::make_shared(root_folder + source, "rb"); auto size = source_file->GetSize(); auto destination_file = std::make_shared(destination, "wb"); - return quick_decryptor.CryptAndWriteFile(std::move(source_file), size, - std::move(destination_file), callback); + return file_decryptor.CryptAndWriteFile(std::move(source_file), size, + std::move(destination_file), callback); } void SDMCDecryptor::Abort() { - quick_decryptor.Abort(); + file_decryptor.Abort(); } std::vector SDMCDecryptor::DecryptFile(const std::string& source) const { diff --git a/src/core/decryptor.h b/src/core/sdmc_decryptor.h similarity index 97% rename from src/core/decryptor.h rename to src/core/sdmc_decryptor.h index 807cf66..49d3203 100644 --- a/src/core/decryptor.h +++ b/src/core/sdmc_decryptor.h @@ -9,7 +9,7 @@ #include "common/common_types.h" #include "common/file_util.h" #include "common/logging/log.h" -#include "core/quick_decryptor.h" +#include "core/file_decryptor.h" namespace Core { @@ -53,7 +53,7 @@ public: private: std::string root_folder; - QuickDecryptor quick_decryptor; + FileDecryptor file_decryptor; }; /// Interface for reading an SDMC file like a normal IOFile. This is read-only. diff --git a/src/frontend/utilities.cpp b/src/frontend/utilities.cpp index eda9c60..e0d0985 100644 --- a/src/frontend/utilities.cpp +++ b/src/frontend/utilities.cpp @@ -8,12 +8,12 @@ #include #include #include -#include "core/decryptor.h" #include "core/file_sys/data/data_container.h" #include "core/file_sys/data/extdata.h" #include "core/file_sys/data/savegame.h" #include "core/file_sys/ncch_container.h" #include "core/key/key.h" +#include "core/sdmc_decryptor.h" #include "frontend/select_files_dialog.h" #include "frontend/utilities.h" #include "ui_utilities.h"