From 5c595a8046aa8084ae998e90b1aa738b60b2f568 Mon Sep 17 00:00:00 2001 From: zhupengfei Date: Sat, 28 Nov 2020 20:12:24 +0800 Subject: [PATCH] Add IOFile::GetData --- src/common/file_util.cpp | 19 +++++++++++++++++++ src/common/file_util.h | 2 ++ src/core/decryptor.cpp | 15 ++++----------- src/core/importer.cpp | 36 +++++++++--------------------------- src/core/inner_fat.cpp | 12 +----------- src/frontend/utilities.cpp | 10 ++++------ 6 files changed, 39 insertions(+), 55 deletions(-) diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index f52dee2..c5e22cc 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp @@ -873,6 +873,25 @@ std::size_t IOFile::Write(const char* data, std::size_t length) { return items_written; } +std::vector IOFile::GetData() { + if (!IsOpen()) { + m_good = false; + LOG_ERROR(Common, "File is not open"); + return {}; + } + if (!Seek(0, SEEK_SET)) { + LOG_ERROR(Common, "Failed to seek file"); + return {}; + } + + std::vector data(GetSize()); + if (Read(reinterpret_cast(data.data()), data.size()) != data.size()) { + LOG_ERROR(Common, "Failed to read from file"); + return {}; + } + return data; +} + u64 IOFile::GetSize() const { if (IsOpen()) return FileUtil::GetSize(m_file); diff --git a/src/common/file_util.h b/src/common/file_util.h index 3f5cb62..0bdaeea 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h @@ -220,6 +220,8 @@ public: virtual std::size_t Read(char* data, std::size_t length); virtual std::size_t Write(const char* data, std::size_t length); + std::vector GetData(); + bool IsOpen() const { return nullptr != m_file; } diff --git a/src/core/decryptor.cpp b/src/core/decryptor.cpp index fe01631..0ae746d 100644 --- a/src/core/decryptor.cpp +++ b/src/core/decryptor.cpp @@ -80,20 +80,13 @@ std::vector SDMCDecryptor::DecryptFile(const std::string& source) const { aes.SetKeyWithIV(key.data(), key.size(), ctr.data()); FileUtil::IOFile file(root_folder + source, "rb"); - if (!file) { - LOG_ERROR(Core, "Could not open {}", root_folder + source); + std::vector encrypted_data = file.GetData(); + if (encrypted_data.empty()) { + LOG_ERROR(Core, "Failed to read from {}", root_folder + source); return {}; } - auto size = file.GetSize(); - - std::vector encrypted_data(size); - if (file.ReadBytes(encrypted_data.data(), size) != size) { - LOG_ERROR(Core, "Could not read file {}", root_folder + source); - return {}; - } - - std::vector data(size); + std::vector data(file.GetSize()); aes.ProcessData(data.data(), encrypted_data.data(), encrypted_data.size()); return data; } diff --git a/src/core/importer.cpp b/src/core/importer.cpp index bd198e9..48fda18 100644 --- a/src/core/importer.cpp +++ b/src/core/importer.cpp @@ -194,13 +194,8 @@ bool SDMCImporter::ImportNandSavegame(u64 id, [[maybe_unused]] const ProgressCal const auto path = fmt::format("sysdata/{:08x}/00000000", (id & 0xFFFFFFFF)); FileUtil::IOFile file(config.nand_data_path + path, "rb"); - if (!file) { - LOG_ERROR(Core, "Could not open file {}", path); - return false; - } - - std::vector data(file.GetSize()); - if (file.ReadBytes(data.data(), data.size()) != data.size()) { + std::vector data = file.GetData(); + if (data.empty()) { LOG_ERROR(Core, "Failed to read from {}", path); return false; } @@ -248,13 +243,8 @@ bool SDMCImporter::ImportSystemArchive(u64 id, [[maybe_unused]] const ProgressCa const auto path = fmt::format("{}{:08x}/{:08x}.app", config.system_archives_path, (id >> 32), (id & 0xFFFFFFFF)); FileUtil::IOFile file(path, "rb"); - if (!file) { - LOG_ERROR(Core, "Could not open {}", path); - return false; - } - - std::vector data(file.GetSize()); - if (file.ReadBytes(data.data(), data.size()) != data.size()) { + std::vector data = file.GetData(); + if (data.empty()) { LOG_ERROR(Core, "Failed to read from {}", path); return false; } @@ -373,12 +363,8 @@ bool SDMCImporter::ImportSysdata(u64 id, [[maybe_unused]] const ProgressCallback } case 5: { // Config savegame FileUtil::IOFile file(config.config_savegame_path, "rb"); - if (!file) { - return false; - } - - std::vector data(file.GetSize()); - if (file.ReadBytes(data.data(), data.size()) != data.size()) { + std::vector data = file.GetData(); + if (data.empty()) { return false; } @@ -808,16 +794,12 @@ void SDMCImporter::ListNandSavegame(std::vector& out) const { // Read the file to test. FileUtil::IOFile file(path, "rb"); - if (!file) { - LOG_ERROR(Core, "Could not open {}", path); - return false; - } - - std::vector data(file.GetSize()); - if (file.ReadBytes(data.data(), data.size()) != data.size()) { + std::vector data = file.GetData(); + if (data.empty()) { LOG_ERROR(Core, "Could not read from {}", path); return false; } + DataContainer container(std::move(data)); if (!container.IsGood()) { return true; diff --git a/src/core/inner_fat.cpp b/src/core/inner_fat.cpp index 0e32fd6..4c94816 100644 --- a/src/core/inner_fat.cpp +++ b/src/core/inner_fat.cpp @@ -279,17 +279,7 @@ std::vector SDExtdata::ReadFile(const std::string& path) const { return decryptor->DecryptFile(path); } else { FileUtil::IOFile file(path, "rb"); - if (!file) { - LOG_ERROR(Core, "Failed to open {}", path); - return {}; - } - - std::vector data(file.GetSize()); - if (file.ReadBytes(data.data(), data.size()) != data.size()) { - LOG_ERROR(Core, "Failed to read from {}", path); - return {}; - } - return data; + return file.GetData(); } } diff --git a/src/frontend/utilities.cpp b/src/frontend/utilities.cpp index 31efe69..b879ace 100644 --- a/src/frontend/utilities.cpp +++ b/src/frontend/utilities.cpp @@ -217,10 +217,9 @@ void UtilitiesDialog::SaveDataExtractionTool() { } else { // TODO: Add Progress reporting ShowProgressDialog([source = source, destination = destination] { - const auto size = FileUtil::GetSize(source.toStdString()); - std::vector data(size); FileUtil::IOFile file(source.toStdString(), "rb"); - if (file.ReadBytes(data.data(), size) != size) { + std::vector data = file.GetData(); + if (data.empty()) { return false; } @@ -277,10 +276,9 @@ void UtilitiesDialog::RomFSExtractionTool() { } ShowProgressDialog([source = source, destination = destination] { - const auto size = FileUtil::GetSize(source.toStdString()); - std::vector data(size); FileUtil::IOFile src_file(source.toStdString(), "rb"); - if (src_file.ReadBytes(data.data(), size) != size) { + std::vector data = src_file.GetData(); + if (data.empty()) { return false; }