From f0de8da515d93bc9ef6da263136c16b984c33de5 Mon Sep 17 00:00:00 2001 From: zhupengfei Date: Thu, 30 Apr 2020 18:54:10 +0800 Subject: [PATCH] core: Makes it possible to skip decryption part in QuickDecryptor Bad name, huh? --- src/core/decryptor.cpp | 4 ++-- src/core/quick_decryptor.cpp | 19 ++++++++++++++----- src/core/quick_decryptor.h | 8 +++++--- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/core/decryptor.cpp b/src/core/decryptor.cpp index 103fc1e..2af126f 100644 --- a/src/core/decryptor.cpp +++ b/src/core/decryptor.cpp @@ -65,8 +65,8 @@ bool SDMCDecryptor::DecryptAndWriteFile(const std::string& source, const std::st auto key = Key::GetNormalKey(Key::SDKey); auto ctr = GetFileCTR(source); return quick_decryptor.DecryptAndWriteFile(std::move(source_file), size, - std::move(destination_file), std::move(key), - std::move(ctr), callback); + std::move(destination_file), callback, true, + std::move(key), std::move(ctr)); } void SDMCDecryptor::Abort() { diff --git a/src/core/quick_decryptor.cpp b/src/core/quick_decryptor.cpp index bd993c7..b7585b9 100644 --- a/src/core/quick_decryptor.cpp +++ b/src/core/quick_decryptor.cpp @@ -25,8 +25,8 @@ QuickDecryptor::~QuickDecryptor() = default; template bool QuickDecryptor::DecryptAndWriteFile(std::unique_ptr source_, std::size_t size, std::unique_ptr destination_, - Core::Key::AESKey key_, Core::Key::AESKey ctr_, - const ProgressCallback& callback_) { + const ProgressCallback& callback_, bool decrypt_, + Core::Key::AESKey key_, Core::Key::AESKey ctr_) { if (is_running) { LOG_ERROR(Core, "Decryptor is running"); return false; @@ -45,6 +45,7 @@ bool QuickDecryptor::DecryptAndWriteFile(std::unique_ptr source_, s source = std::move(source_); destination = std::move(destination_); + decrypt = decrypt_; key = std::move(key_); ctr = std::move(ctr_); callback = callback_; @@ -54,15 +55,19 @@ bool QuickDecryptor::DecryptAndWriteFile(std::unique_ptr source_, s is_good = is_running = true; read_thread = std::make_unique(&QuickDecryptor::DataReadLoop, this); - decrypt_thread = std::make_unique(&QuickDecryptor::DataDecryptLoop, this); write_thread = std::make_unique(&QuickDecryptor::DataWriteLoop, this); + if (decrypt) { + decrypt_thread = std::make_unique(&QuickDecryptor::DataDecryptLoop, this); + } completion_event.Wait(); is_running = false; read_thread->join(); - decrypt_thread->join(); write_thread->join(); + if (decrypt) { + decrypt_thread->join(); + } // Release the files source.reset(); @@ -152,7 +157,11 @@ void QuickDecryptor::DataWriteLoop() { iteration++; - data_decrypted_event[current_buffer].Wait(); + if (decrypt) { + data_decrypted_event[current_buffer].Wait(); + } else { + data_read_event[current_buffer].Wait(); + } const auto bytes_to_write = std::min(BufferSize, file_size); if (destination->WriteBytes(buffers[current_buffer].data(), bytes_to_write) != diff --git a/src/core/quick_decryptor.h b/src/core/quick_decryptor.h index 5f7735d..5346b07 100644 --- a/src/core/quick_decryptor.h +++ b/src/core/quick_decryptor.h @@ -34,9 +34,10 @@ public: ~QuickDecryptor(); bool DecryptAndWriteFile(std::unique_ptr source, std::size_t size, - std::unique_ptr destination, Core::Key::AESKey key, - Core::Key::AESKey ctr, - const ProgressCallback& callback = [](std::size_t, std::size_t) {}); + std::unique_ptr destination, + const ProgressCallback& callback = [](std::size_t, std::size_t) {}, + bool decrypt = false, Core::Key::AESKey key = {}, + Core::Key::AESKey ctr = {}); void DataReadLoop(); void DataDecryptLoop(); @@ -52,6 +53,7 @@ private: std::unique_ptr source; std::unique_ptr destination; + bool decrypt{}; Core::Key::AESKey key; Core::Key::AESKey ctr;