From a62088539ed02a8569814601b3b99b713c5d8a34 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Wed, 12 Jun 2019 17:27:06 -0400 Subject: common/hex_util: Combine HexVectorToString() and HexArrayToString() These can be generified together by using a concept type to designate them. This also has the benefit of not making copies of potentially very large arrays. --- src/common/hex_util.cpp | 7 ------- src/common/hex_util.h | 11 +++++++---- src/core/crypto/key_manager.cpp | 4 ++-- src/core/file_sys/ips_layer.cpp | 2 +- src/core/file_sys/patch_manager.cpp | 8 ++++---- src/core/file_sys/registered_cache.cpp | 14 ++++++++------ src/core/file_sys/submission_package.cpp | 13 ++++++++----- src/core/file_sys/xts_archive.cpp | 2 +- src/core/hle/service/am/applets/general_backend.cpp | 12 ++++++------ src/core/hle/service/ldr/ldr.cpp | 2 +- src/core/loader/nso.cpp | 4 ++-- 11 files changed, 40 insertions(+), 39 deletions(-) (limited to 'src') diff --git a/src/common/hex_util.cpp b/src/common/hex_util.cpp index 5b63f9e81..c2f6cf0f6 100644 --- a/src/common/hex_util.cpp +++ b/src/common/hex_util.cpp @@ -30,13 +30,6 @@ std::vector HexStringToVector(std::string_view str, bool little_endian) { return out; } -std::string HexVectorToString(const std::vector& vector, bool upper) { - std::string out; - for (u8 c : vector) - out += fmt::format(upper ? "{:02X}" : "{:02x}", c); - return out; -} - std::array operator""_array16(const char* str, std::size_t len) { if (len != 32) { LOG_ERROR(Common, diff --git a/src/common/hex_util.h b/src/common/hex_util.h index 68f003cb6..a64c9b485 100644 --- a/src/common/hex_util.h +++ b/src/common/hex_util.h @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include "common/common_types.h" @@ -30,13 +31,15 @@ std::array HexStringToArray(std::string_view str) { return out; } -std::string HexVectorToString(const std::vector& vector, bool upper = true); +template +std::string HexToString(const ContiguousContainer& data, bool upper = true) { + static_assert(std::is_same_v, + "Underlying type within the contiguous container must be u8."); -template -std::string HexArrayToString(std::array array, bool upper = true) { std::string out; - for (u8 c : array) + for (const u8 c : data) { out += fmt::format(upper ? "{:02X}" : "{:02x}", c); + } return out; } diff --git a/src/core/crypto/key_manager.cpp b/src/core/crypto/key_manager.cpp index dc006e2bb..6dd633363 100644 --- a/src/core/crypto/key_manager.cpp +++ b/src/core/crypto/key_manager.cpp @@ -572,7 +572,7 @@ void KeyManager::WriteKeyToFile(KeyCategory category, std::string_view keyname, << "# If you are experiencing issues involving keys, it may help to delete this file\n"; } - file << fmt::format("\n{} = {}", keyname, Common::HexArrayToString(key)); + file << fmt::format("\n{} = {}", keyname, Common::HexToString(key)); AttemptLoadKeyFile(yuzu_keys_dir, yuzu_keys_dir, filename, category == KeyCategory::Title); } @@ -583,7 +583,7 @@ void KeyManager::SetKey(S128KeyType id, Key128 key, u64 field1, u64 field2) { Key128 rights_id; std::memcpy(rights_id.data(), &field2, sizeof(u64)); std::memcpy(rights_id.data() + sizeof(u64), &field1, sizeof(u64)); - WriteKeyToFile(KeyCategory::Title, Common::HexArrayToString(rights_id), key); + WriteKeyToFile(KeyCategory::Title, Common::HexToString(rights_id), key); } auto category = KeyCategory::Standard; diff --git a/src/core/file_sys/ips_layer.cpp b/src/core/file_sys/ips_layer.cpp index 485c4913a..fcf8dc49b 100644 --- a/src/core/file_sys/ips_layer.cpp +++ b/src/core/file_sys/ips_layer.cpp @@ -295,7 +295,7 @@ void IPSwitchCompiler::Parse() { LOG_INFO(Loader, "[IPSwitchCompiler ('{}')] - Patching value at offset 0x{:08X} " "with byte string '{}'", - patch_text->GetName(), offset, Common::HexVectorToString(replace)); + patch_text->GetName(), offset, Common::HexToString(replace)); } patch.records.insert_or_assign(offset, std::move(replace)); diff --git a/src/core/file_sys/patch_manager.cpp b/src/core/file_sys/patch_manager.cpp index 78dbadee3..da823c37b 100644 --- a/src/core/file_sys/patch_manager.cpp +++ b/src/core/file_sys/patch_manager.cpp @@ -142,7 +142,7 @@ std::vector PatchManager::CollectPatches(const std::vector PatchManager::PatchNSO(const std::vector& nso, const std::st return nso; } - const auto build_id_raw = Common::HexArrayToString(header.build_id); + const auto build_id_raw = Common::HexToString(header.build_id); const auto build_id = build_id_raw.substr(0, build_id_raw.find_last_not_of('0') + 1); if (Settings::values.dump_nso) { @@ -219,7 +219,7 @@ std::vector PatchManager::PatchNSO(const std::vector& nso, const std::st } bool PatchManager::HasNSOPatch(const std::array& build_id_) const { - const auto build_id_raw = Common::HexArrayToString(build_id_); + const auto build_id_raw = Common::HexToString(build_id_); const auto build_id = build_id_raw.substr(0, build_id_raw.find_last_not_of('0') + 1); LOG_INFO(Loader, "Querying NSO patch existence for build_id={}", build_id); @@ -235,7 +235,7 @@ bool PatchManager::HasNSOPatch(const std::array& build_id_) const { static std::optional ReadCheatFileFromFolder(const Core::System& system, u64 title_id, const std::array& build_id_, const VirtualDir& base_path, bool upper) { - const auto build_id_raw = Common::HexArrayToString(build_id_, upper); + const auto build_id_raw = Common::HexToString(build_id_, upper); const auto build_id = build_id_raw.substr(0, sizeof(u64) * 2); const auto file = base_path->GetFile(fmt::format("{}.txt", build_id)); diff --git a/src/core/file_sys/registered_cache.cpp b/src/core/file_sys/registered_cache.cpp index 3946ff871..58917e094 100644 --- a/src/core/file_sys/registered_cache.cpp +++ b/src/core/file_sys/registered_cache.cpp @@ -53,13 +53,14 @@ static bool FollowsNcaIdFormat(std::string_view name) { static std::string GetRelativePathFromNcaID(const std::array& nca_id, bool second_hex_upper, bool within_two_digit) { - if (!within_two_digit) - return fmt::format("/{}.nca", Common::HexArrayToString(nca_id, second_hex_upper)); + if (!within_two_digit) { + return fmt::format("/{}.nca", Common::HexToString(nca_id, second_hex_upper)); + } Core::Crypto::SHA256Hash hash{}; mbedtls_sha256(nca_id.data(), nca_id.size(), hash.data(), 0); return fmt::format("/000000{:02X}/{}.nca", hash[0], - Common::HexArrayToString(nca_id, second_hex_upper)); + Common::HexToString(nca_id, second_hex_upper)); } static std::string GetCNMTName(TitleType type, u64 title_id) { @@ -376,10 +377,11 @@ std::vector RegisteredCache::ListEntriesFilter( } static std::shared_ptr GetNCAFromNSPForID(const NSP& nsp, const NcaID& id) { - const auto file = nsp.GetFile(fmt::format("{}.nca", Common::HexArrayToString(id, false))); - if (file == nullptr) + auto file = nsp.GetFile(fmt::format("{}.nca", Common::HexToString(id, false))); + if (file == nullptr) { return nullptr; - return std::make_shared(file); + } + return std::make_shared(std::move(file)); } InstallResult RegisteredCache::InstallEntry(const XCI& xci, bool overwrite_if_exists, diff --git a/src/core/file_sys/submission_package.cpp b/src/core/file_sys/submission_package.cpp index c69caae0f..d0428a457 100644 --- a/src/core/file_sys/submission_package.cpp +++ b/src/core/file_sys/submission_package.cpp @@ -235,16 +235,18 @@ void NSP::ReadNCAs(const std::vector& files) { const auto section0 = nca->GetSubdirectories()[0]; for (const auto& inner_file : section0->GetFiles()) { - if (inner_file->GetExtension() != "cnmt") + if (inner_file->GetExtension() != "cnmt") { continue; + } const CNMT cnmt(inner_file); auto& ncas_title = ncas[cnmt.GetTitleID()]; ncas_title[{cnmt.GetType(), ContentRecordType::Meta}] = nca; for (const auto& rec : cnmt.GetContentRecords()) { - const auto id_string = Common::HexArrayToString(rec.nca_id, false); - const auto next_file = pfs->GetFile(fmt::format("{}.nca", id_string)); + const auto id_string = Common::HexToString(rec.nca_id, false); + auto next_file = pfs->GetFile(fmt::format("{}.nca", id_string)); + if (next_file == nullptr) { LOG_WARNING(Service_FS, "NCA with ID {}.nca is listed in content metadata, but cannot " @@ -253,9 +255,10 @@ void NSP::ReadNCAs(const std::vector& files) { continue; } - auto next_nca = std::make_shared(next_file, nullptr, 0, keys); - if (next_nca->GetType() == NCAContentType::Program) + auto next_nca = std::make_shared(std::move(next_file), nullptr, 0, keys); + if (next_nca->GetType() == NCAContentType::Program) { program_status[cnmt.GetTitleID()] = next_nca->GetStatus(); + } if (next_nca->GetStatus() == Loader::ResultStatus::Success || (next_nca->GetStatus() == Loader::ResultStatus::ErrorMissingBKTRBaseRomFS && (cnmt.GetTitleID() & 0x800) != 0)) { diff --git a/src/core/file_sys/xts_archive.cpp b/src/core/file_sys/xts_archive.cpp index eec51c64e..4bc5cb2ee 100644 --- a/src/core/file_sys/xts_archive.cpp +++ b/src/core/file_sys/xts_archive.cpp @@ -66,7 +66,7 @@ NAX::NAX(VirtualFile file_, std::array nca_id) Core::Crypto::SHA256Hash hash{}; mbedtls_sha256(nca_id.data(), nca_id.size(), hash.data(), 0); status = Parse(fmt::format("/registered/000000{:02X}/{}.nca", hash[0], - Common::HexArrayToString(nca_id, false))); + Common::HexToString(nca_id, false))); } NAX::~NAX() = default; diff --git a/src/core/hle/service/am/applets/general_backend.cpp b/src/core/hle/service/am/applets/general_backend.cpp index c591b9ac2..76fc8906d 100644 --- a/src/core/hle/service/am/applets/general_backend.cpp +++ b/src/core/hle/service/am/applets/general_backend.cpp @@ -2,7 +2,7 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. -#include +#include #include "common/assert.h" #include "common/hex_util.h" @@ -16,21 +16,21 @@ namespace Service::AM::Applets { -static void LogCurrentStorage(AppletDataBroker& broker, std::string prefix) { +static void LogCurrentStorage(AppletDataBroker& broker, std::string_view prefix) { std::unique_ptr storage = broker.PopNormalDataToApplet(); for (; storage != nullptr; storage = broker.PopNormalDataToApplet()) { const auto data = storage->GetData(); LOG_INFO(Service_AM, - "called (STUBBED), during {} recieved normal data with size={:08X}, data={}", - prefix, data.size(), Common::HexVectorToString(data)); + "called (STUBBED), during {} received normal data with size={:08X}, data={}", + prefix, data.size(), Common::HexToString(data)); } storage = broker.PopInteractiveDataToApplet(); for (; storage != nullptr; storage = broker.PopInteractiveDataToApplet()) { const auto data = storage->GetData(); LOG_INFO(Service_AM, - "called (STUBBED), during {} recieved interactive data with size={:08X}, data={}", - prefix, data.size(), Common::HexVectorToString(data)); + "called (STUBBED), during {} received interactive data with size={:08X}, data={}", + prefix, data.size(), Common::HexToString(data)); } } diff --git a/src/core/hle/service/ldr/ldr.cpp b/src/core/hle/service/ldr/ldr.cpp index 5af925515..b839303ac 100644 --- a/src/core/hle/service/ldr/ldr.cpp +++ b/src/core/hle/service/ldr/ldr.cpp @@ -310,7 +310,7 @@ public: if (!IsValidNROHash(hash)) { LOG_ERROR(Service_LDR, "NRO hash is not present in any currently loaded NRRs (hash={})!", - Common::HexArrayToString(hash)); + Common::HexToString(hash)); IPC::ResponseBuilder rb{ctx, 2}; rb.Push(ERROR_MISSING_NRR_HASH); return; diff --git a/src/core/loader/nso.cpp b/src/core/loader/nso.cpp index 62c090353..80090b792 100644 --- a/src/core/loader/nso.cpp +++ b/src/core/loader/nso.cpp @@ -152,8 +152,8 @@ std::optional AppLoader_NSO::LoadModule(Kernel::Process& process, auto& system = Core::System::GetInstance(); const auto cheats = pm->CreateCheatList(system, nso_header.build_id); if (!cheats.empty()) { - system.RegisterCheatList(cheats, Common::HexArrayToString(nso_header.build_id), - load_base, load_base + program_image.size()); + system.RegisterCheatList(cheats, Common::HexToString(nso_header.build_id), load_base, + load_base + program_image.size()); } } -- cgit v1.2.3 From 969cd6dc1d60acd98c89815dd53c11bf4dac2518 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Wed, 12 Jun 2019 17:47:05 -0400 Subject: common/hex_util: Reserve std::string memory ahead of time Avoids potentially performing multiple reallocations (depending on the size of the input data) by reserving the necessary amount of memory ahead of time. This is trivially doable, so there's no harm in it. --- src/common/hex_util.h | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src') diff --git a/src/common/hex_util.h b/src/common/hex_util.h index a64c9b485..bb4736f96 100644 --- a/src/common/hex_util.h +++ b/src/common/hex_util.h @@ -36,10 +36,15 @@ std::string HexToString(const ContiguousContainer& data, bool upper = true) { static_assert(std::is_same_v, "Underlying type within the contiguous container must be u8."); + constexpr std::size_t pad_width = 2; + std::string out; + out.reserve(std::size(data) * pad_width); + for (const u8 c : data) { out += fmt::format(upper ? "{:02X}" : "{:02x}", c); } + return out; } -- cgit v1.2.3