More refactor going on

This commit is contained in:
Pengfei
2021-08-04 11:25:39 +08:00
parent 0abddb7e79
commit 1fe45d890c
13 changed files with 55 additions and 54 deletions
+4 -4
View File
@@ -7,8 +7,8 @@ add_library(core STATIC
db/title_db.h db/title_db.h
db/title_keys_bin.cpp db/title_keys_bin.cpp
db/title_keys_bin.h db/title_keys_bin.h
decryptor.cpp file_decryptor.cpp
decryptor.h file_decryptor.h
file_sys/certificate.cpp file_sys/certificate.cpp
file_sys/certificate.h file_sys/certificate.h
file_sys/cia_common.h file_sys/cia_common.h
@@ -35,8 +35,8 @@ add_library(core STATIC
key/arithmetic128.h key/arithmetic128.h
key/key.cpp key/key.cpp
key/key.h key/key.h
quick_decryptor.cpp sdmc_decryptor.cpp
quick_decryptor.h sdmc_decryptor.h
) )
target_link_libraries(core PRIVATE common cryptopp) target_link_libraries(core PRIVATE common cryptopp)
+1 -1
View File
@@ -282,7 +282,7 @@ bool CIABuilder::AddContent(u16 content_id, NCCHContainer& ncch) {
const bool is_encrypted = static_cast<u16>(tmd_chunk.type) & 0x01; const bool is_encrypted = static_cast<u16>(tmd_chunk.type) & 0x01;
// For encrypted content, the hashes are calculated before CIA/CDN encryption. // 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. // For unencrypted content, we can just use HashedFile's hashing.
std::shared_ptr<CIAEncryptAndHash> crypto; std::shared_ptr<CIAEncryptAndHash> crypto;
if (is_encrypted) { if (is_encrypted) {
+2 -2
View File
@@ -10,11 +10,11 @@
#include "common/file_util.h" #include "common/file_util.h"
#include "common/progress_callback.h" #include "common/progress_callback.h"
#include "common/swap.h" #include "common/swap.h"
#include "core/file_decryptor.h"
#include "core/file_sys/cia_common.h" #include "core/file_sys/cia_common.h"
#include "core/file_sys/ncch_container.h" #include "core/file_sys/ncch_container.h"
#include "core/file_sys/title_metadata.h" #include "core/file_sys/title_metadata.h"
#include "core/key/key.h" #include "core/key/key.h"
#include "core/quick_decryptor.h"
namespace Core { namespace Core {
@@ -118,7 +118,7 @@ private:
std::mutex abort_ncch_mutex; std::mutex abort_ncch_mutex;
NCCHContainer* abort_ncch{}; NCCHContainer* abort_ncch{};
QuickDecryptor decryptor; FileDecryptor decryptor;
}; };
} // namespace Core } // namespace Core
@@ -11,22 +11,21 @@
#include "common/assert.h" #include "common/assert.h"
#include "common/file_util.h" #include "common/file_util.h"
#include "common/string_util.h" #include "common/string_util.h"
#include "core/decryptor.h" #include "core/file_decryptor.h"
#include "core/quick_decryptor.h"
namespace Core { namespace Core {
QuickDecryptor::QuickDecryptor() = default; FileDecryptor::FileDecryptor() = default;
QuickDecryptor::~QuickDecryptor() = default; FileDecryptor::~FileDecryptor() = default;
void QuickDecryptor::SetCrypto(std::shared_ptr<CryptoFunc> crypto_) { void FileDecryptor::SetCrypto(std::shared_ptr<CryptoFunc> crypto_) {
crypto = std::move(crypto_); crypto = std::move(crypto_);
} }
bool QuickDecryptor::CryptAndWriteFile(std::shared_ptr<FileUtil::IOFile> source_, std::size_t size, bool FileDecryptor::CryptAndWriteFile(std::shared_ptr<FileUtil::IOFile> source_, std::size_t size,
std::shared_ptr<FileUtil::IOFile> destination_, std::shared_ptr<FileUtil::IOFile> destination_,
const Common::ProgressCallback& callback_) { const Common::ProgressCallback& callback_) {
if (is_running) { if (is_running) {
LOG_ERROR(Core, "Decryptor is running"); LOG_ERROR(Core, "Decryptor is running");
return false; return false;
@@ -55,10 +54,10 @@ bool QuickDecryptor::CryptAndWriteFile(std::shared_ptr<FileUtil::IOFile> source_
is_good = is_running = true; is_good = is_running = true;
read_thread = std::make_unique<std::thread>(&QuickDecryptor::DataReadLoop, this); read_thread = std::make_unique<std::thread>(&FileDecryptor::DataReadLoop, this);
write_thread = std::make_unique<std::thread>(&QuickDecryptor::DataWriteLoop, this); write_thread = std::make_unique<std::thread>(&FileDecryptor::DataWriteLoop, this);
if (crypto) { if (crypto) {
decrypt_thread = std::make_unique<std::thread>(&QuickDecryptor::DataDecryptLoop, this); decrypt_thread = std::make_unique<std::thread>(&FileDecryptor::DataDecryptLoop, this);
} }
completion_event.Wait(); completion_event.Wait();
@@ -79,7 +78,7 @@ bool QuickDecryptor::CryptAndWriteFile(std::shared_ptr<FileUtil::IOFile> source_
return ret; return ret;
} }
void QuickDecryptor::DataReadLoop() { void FileDecryptor::DataReadLoop() {
std::size_t current_buffer = 0; std::size_t current_buffer = 0;
bool is_first_run = true; 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 current_buffer = 0;
std::size_t file_size = current_total_size; 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; std::size_t current_buffer = 0;
if (!*destination) { if (!*destination) {
@@ -174,14 +173,14 @@ void QuickDecryptor::DataWriteLoop() {
completion_event.Set(); completion_event.Set();
} }
void QuickDecryptor::Abort() { void FileDecryptor::Abort() {
if (is_running.exchange(false)) { if (is_running.exchange(false)) {
is_good = false; is_good = false;
completion_event.Set(); completion_event.Set();
} }
} }
void QuickDecryptor::Reset(std::size_t total_size_) { void FileDecryptor::Reset(std::size_t total_size_) {
total_size = total_size_; total_size = total_size_;
imported_size = 0; imported_size = 0;
} }
@@ -18,13 +18,14 @@ namespace Core {
class CryptoFunc; class CryptoFunc;
/** /**
* Generalized file decryptor.
* Helper that reads, decrypts and writes data. This uses three threads to process the data * Helper that reads, decrypts and writes data. This uses three threads to process the data
* and call progress callbacks occasionally. * and call progress callbacks occasionally.
*/ */
class QuickDecryptor { class FileDecryptor {
public: public:
explicit QuickDecryptor(); explicit FileDecryptor();
~QuickDecryptor(); ~FileDecryptor();
/** /**
* Set up the crypto to use. * Set up the crypto to use.
+1 -1
View File
@@ -2,9 +2,9 @@
// Licensed under GPLv2 or any later version // Licensed under GPLv2 or any later version
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include "core/decryptor.h"
#include "core/file_sys/data/data_container.h" #include "core/file_sys/data/data_container.h"
#include "core/file_sys/data/extdata.h" #include "core/file_sys/data/extdata.h"
#include "core/sdmc_decryptor.h"
namespace Core { namespace Core {
+1 -1
View File
@@ -442,7 +442,7 @@ bool NCCHContainer::DecryptToFile(std::shared_ptr<FileUtil::IOFile> dest_file,
} }
if (!is_encrypted) { 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); file->Seek(0, SEEK_SET);
const auto size = file->GetSize(); const auto size = file->GetSize();
+2 -2
View File
@@ -13,7 +13,7 @@
#include "common/file_util.h" #include "common/file_util.h"
#include "common/progress_callback.h" #include "common/progress_callback.h"
#include "common/swap.h" #include "common/swap.h"
#include "core/decryptor.h" #include "core/sdmc_decryptor.h"
namespace Core { namespace Core {
@@ -305,7 +305,7 @@ private:
std::shared_ptr<FileUtil::IOFile> exefs_file; std::shared_ptr<FileUtil::IOFile> exefs_file;
// Used for DecryptToFile // Used for DecryptToFile
QuickDecryptor decryptor; FileDecryptor decryptor;
std::atomic_bool aborted{false}; std::atomic_bool aborted{false};
friend class CIABuilder; friend class CIABuilder;
+15 -14
View File
@@ -11,7 +11,6 @@
#include "core/cia_builder.h" #include "core/cia_builder.h"
#include "core/db/seed_db.h" #include "core/db/seed_db.h"
#include "core/db/title_db.h" #include "core/db/title_db.h"
#include "core/decryptor.h"
#include "core/file_sys/certificate.h" #include "core/file_sys/certificate.h"
#include "core/file_sys/data/data_container.h" #include "core/file_sys/data/data_container.h"
#include "core/file_sys/data/extdata.h" #include "core/file_sys/data/extdata.h"
@@ -21,6 +20,7 @@
#include "core/file_sys/title_metadata.h" #include "core/file_sys/title_metadata.h"
#include "core/importer.h" #include "core/importer.h"
#include "core/key/key.h" #include "core/key/key.h"
#include "core/sdmc_decryptor.h"
namespace Core { namespace Core {
@@ -60,11 +60,11 @@ bool SDMCImporter::Init() {
Certs::Load(config.certs_db_path); Certs::Load(config.certs_db_path);
} }
decryptor = std::make_unique<SDMCDecryptor>(config.sdmc_path); sdmc_decryptor = std::make_unique<SDMCDecryptor>(config.sdmc_path);
// Load SDMC Title DB // Load SDMC Title DB
{ {
DataContainer container(decryptor->DecryptFile("/dbs/title.db")); DataContainer container(sdmc_decryptor->DecryptFile("/dbs/title.db"));
std::vector<std::vector<u8>> data; std::vector<std::vector<u8>> data;
if (container.IsGood() && container.GetIVFCLevel4Data(data)) { if (container.IsGood() && container.GetIVFCLevel4Data(data)) {
sdmc_title_db = std::make_unique<TitleDB>(std::move(data[0])); sdmc_title_db = std::make_unique<TitleDB>(std::move(data[0]));
@@ -93,7 +93,7 @@ bool SDMCImporter::IsGood() const {
} }
void SDMCImporter::AbortImporting() { void SDMCImporter::AbortImporting() {
decryptor->Abort(); sdmc_decryptor->Abort();
} }
bool SDMCImporter::ImportContent(const ContentSpecifier& specifier, 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, bool SDMCImporter::ImportTitle(const ContentSpecifier& specifier,
const Common::ProgressCallback& callback) { const Common::ProgressCallback& callback) {
return ImportTitleGeneric( return ImportTitleGeneric(
*decryptor, config.sdmc_path, specifier, [this, &callback](const std::string& filepath) { *sdmc_decryptor, config.sdmc_path, specifier,
return decryptor->DecryptAndWriteFile( [this, &callback](const std::string& filepath) {
return sdmc_decryptor->DecryptAndWriteFile(
filepath, filepath,
FileUtil::GetUserPath(FileUtil::UserPath::SDMCDir) + FileUtil::GetUserPath(FileUtil::UserPath::SDMCDir) +
"Nintendo " "Nintendo "
@@ -185,10 +186,10 @@ bool SDMCImporter::ImportNandTitle(const ContentSpecifier& specifier,
const auto base_path = const auto base_path =
config.system_titles_path.substr(0, config.system_titles_path.size() - 6); config.system_titles_path.substr(0, config.system_titles_path.size() - 6);
QuickDecryptor quick_decryptor; FileDecryptor decryptor;
return ImportTitleGeneric( return ImportTitleGeneric(
quick_decryptor, base_path, specifier, decryptor, base_path, specifier,
[&base_path, &quick_decryptor, &callback](const std::string& filepath) { [&base_path, &decryptor, &callback](const std::string& filepath) {
const auto physical_path = base_path + filepath.substr(1); const auto physical_path = base_path + filepath.substr(1);
const auto citra_path = FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) + const auto citra_path = FileUtil::GetUserPath(FileUtil::UserPath::NANDDir) +
"00000000000000000000000000000000" + filepath; "00000000000000000000000000000000" + filepath;
@@ -197,7 +198,7 @@ bool SDMCImporter::ImportNandTitle(const ContentSpecifier& specifier,
return false; return false;
} }
// Crypto is not set: plain copy with progress. // Crypto is not set: plain copy with progress.
return quick_decryptor.CryptAndWriteFile( return decryptor.CryptAndWriteFile(
std::make_shared<FileUtil::IOFile>(physical_path, "rb"), std::make_shared<FileUtil::IOFile>(physical_path, "rb"),
FileUtil::GetSize(physical_path), FileUtil::GetSize(physical_path),
std::make_shared<FileUtil::IOFile>(citra_path, "wb"), callback); std::make_shared<FileUtil::IOFile>(citra_path, "wb"), callback);
@@ -208,7 +209,7 @@ bool SDMCImporter::ImportSavegame(u64 id,
[[maybe_unused]] const Common::ProgressCallback& callback) { [[maybe_unused]] const Common::ProgressCallback& callback) {
const auto path = fmt::format("title/{:08x}/{:08x}/data/", (id >> 32), (id & 0xFFFFFFFF)); 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()) { if (!container.IsGood()) {
return false; return false;
} }
@@ -258,7 +259,7 @@ bool SDMCImporter::ImportNandSavegame(u64 id,
bool SDMCImporter::ImportExtdata(u64 id, bool SDMCImporter::ImportExtdata(u64 id,
[[maybe_unused]] const Common::ProgressCallback& callback) { [[maybe_unused]] const Common::ProgressCallback& callback) {
const auto path = fmt::format("extdata/{:08x}/{:08x}/", (id >> 32), (id & 0xFFFFFFFF)); const auto path = fmt::format("extdata/{:08x}/{:08x}/", (id >> 32), (id & 0xFFFFFFFF));
Extdata extdata("/" + path, *decryptor); Extdata extdata("/" + path, *sdmc_decryptor);
if (!extdata.IsGood()) { if (!extdata.IsGood()) {
return false; return false;
} }
@@ -508,7 +509,7 @@ bool SDMCImporter::LoadTMD(ContentType type, u64 id, TitleMetadata& out) const {
} }
return out.Load(file.GetData()); return out.Load(file.GetData());
} else { } 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<ContentSpecifier>& out) const {
// Savegames can be uninitialized. // Savegames can be uninitialized.
// TODO: Is there a better way of checking this other than performing the // TODO: Is there a better way of checking this other than performing the
// decryption? (Very costy) // decryption? (Very costy)
DataContainer container(decryptor->DecryptFile( DataContainer container(sdmc_decryptor->DecryptFile(
fmt::format("/title/{:08x}/{}/data/00000001.sav", high_id, virtual_name))); fmt::format("/title/{:08x}/{}/data/00000001.sav", high_id, virtual_name)));
if (!container.IsGood()) { if (!container.IsGood()) {
return true; return true;
+1 -1
View File
@@ -205,7 +205,7 @@ private:
bool is_good{}; bool is_good{};
Config config; Config config;
std::unique_ptr<SDMCDecryptor> decryptor; std::unique_ptr<SDMCDecryptor> sdmc_decryptor;
std::unique_ptr<CIABuilder> cia_builder; std::unique_ptr<CIABuilder> cia_builder;
std::mutex cia_builder_mutex; std::mutex cia_builder_mutex;
@@ -12,8 +12,8 @@
#include "common/assert.h" #include "common/assert.h"
#include "common/file_util.h" #include "common/file_util.h"
#include "common/string_util.h" #include "common/string_util.h"
#include "core/decryptor.h"
#include "core/key/key.h" #include "core/key/key.h"
#include "core/sdmc_decryptor.h"
namespace Core { namespace Core {
@@ -49,7 +49,7 @@ std::array<u8, 16> GetFileCTR(const std::string& path) {
} // namespace } // namespace
void SDMCDecryptor::Reset(std::size_t total_size) { 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, 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 key = Key::GetNormalKey(Key::SDKey);
auto ctr = GetFileCTR(source); auto ctr = GetFileCTR(source);
quick_decryptor.SetCrypto(CreateCTRCrypto(key, ctr)); file_decryptor.SetCrypto(CreateCTRCrypto(key, ctr));
auto source_file = std::make_shared<FileUtil::IOFile>(root_folder + source, "rb"); auto source_file = std::make_shared<FileUtil::IOFile>(root_folder + source, "rb");
auto size = source_file->GetSize(); auto size = source_file->GetSize();
auto destination_file = std::make_shared<FileUtil::IOFile>(destination, "wb"); auto destination_file = std::make_shared<FileUtil::IOFile>(destination, "wb");
return quick_decryptor.CryptAndWriteFile(std::move(source_file), size, return file_decryptor.CryptAndWriteFile(std::move(source_file), size,
std::move(destination_file), callback); std::move(destination_file), callback);
} }
void SDMCDecryptor::Abort() { void SDMCDecryptor::Abort() {
quick_decryptor.Abort(); file_decryptor.Abort();
} }
std::vector<u8> SDMCDecryptor::DecryptFile(const std::string& source) const { std::vector<u8> SDMCDecryptor::DecryptFile(const std::string& source) const {
@@ -9,7 +9,7 @@
#include "common/common_types.h" #include "common/common_types.h"
#include "common/file_util.h" #include "common/file_util.h"
#include "common/logging/log.h" #include "common/logging/log.h"
#include "core/quick_decryptor.h" #include "core/file_decryptor.h"
namespace Core { namespace Core {
@@ -53,7 +53,7 @@ public:
private: private:
std::string root_folder; std::string root_folder;
QuickDecryptor quick_decryptor; FileDecryptor file_decryptor;
}; };
/// Interface for reading an SDMC file like a normal IOFile. This is read-only. /// Interface for reading an SDMC file like a normal IOFile. This is read-only.
+1 -1
View File
@@ -8,12 +8,12 @@
#include <QMessageBox> #include <QMessageBox>
#include <QProgressDialog> #include <QProgressDialog>
#include <QtConcurrent/QtConcurrentRun> #include <QtConcurrent/QtConcurrentRun>
#include "core/decryptor.h"
#include "core/file_sys/data/data_container.h" #include "core/file_sys/data/data_container.h"
#include "core/file_sys/data/extdata.h" #include "core/file_sys/data/extdata.h"
#include "core/file_sys/data/savegame.h" #include "core/file_sys/data/savegame.h"
#include "core/file_sys/ncch_container.h" #include "core/file_sys/ncch_container.h"
#include "core/key/key.h" #include "core/key/key.h"
#include "core/sdmc_decryptor.h"
#include "frontend/select_files_dialog.h" #include "frontend/select_files_dialog.h"
#include "frontend/utilities.h" #include "frontend/utilities.h"
#include "ui_utilities.h" #include "ui_utilities.h"