summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Lioncash2019-06-12 17:27:06 -0400
committerGravatar Lioncash2019-06-12 17:54:05 -0400
commita62088539ed02a8569814601b3b99b713c5d8a34 (patch)
tree03fe279e7651c55291f702f5a8b518cb07e35812
parentMerge pull request #2578 from lioncash/cnmt (diff)
downloadyuzu-a62088539ed02a8569814601b3b99b713c5d8a34.tar.gz
yuzu-a62088539ed02a8569814601b3b99b713c5d8a34.tar.xz
yuzu-a62088539ed02a8569814601b3b99b713c5d8a34.zip
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.
Diffstat (limited to '')
-rw-r--r--src/common/hex_util.cpp7
-rw-r--r--src/common/hex_util.h11
-rw-r--r--src/core/crypto/key_manager.cpp4
-rw-r--r--src/core/file_sys/ips_layer.cpp2
-rw-r--r--src/core/file_sys/patch_manager.cpp8
-rw-r--r--src/core/file_sys/registered_cache.cpp14
-rw-r--r--src/core/file_sys/submission_package.cpp13
-rw-r--r--src/core/file_sys/xts_archive.cpp2
-rw-r--r--src/core/hle/service/am/applets/general_backend.cpp12
-rw-r--r--src/core/hle/service/ldr/ldr.cpp2
-rw-r--r--src/core/loader/nso.cpp4
11 files changed, 40 insertions, 39 deletions
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<u8> HexStringToVector(std::string_view str, bool little_endian) {
30 return out; 30 return out;
31} 31}
32 32
33std::string HexVectorToString(const std::vector<u8>& vector, bool upper) {
34 std::string out;
35 for (u8 c : vector)
36 out += fmt::format(upper ? "{:02X}" : "{:02x}", c);
37 return out;
38}
39
40std::array<u8, 16> operator""_array16(const char* str, std::size_t len) { 33std::array<u8, 16> operator""_array16(const char* str, std::size_t len) {
41 if (len != 32) { 34 if (len != 32) {
42 LOG_ERROR(Common, 35 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 @@
7#include <array> 7#include <array>
8#include <cstddef> 8#include <cstddef>
9#include <string> 9#include <string>
10#include <type_traits>
10#include <vector> 11#include <vector>
11#include <fmt/format.h> 12#include <fmt/format.h>
12#include "common/common_types.h" 13#include "common/common_types.h"
@@ -30,13 +31,15 @@ std::array<u8, Size> HexStringToArray(std::string_view str) {
30 return out; 31 return out;
31} 32}
32 33
33std::string HexVectorToString(const std::vector<u8>& vector, bool upper = true); 34template <typename ContiguousContainer>
35std::string HexToString(const ContiguousContainer& data, bool upper = true) {
36 static_assert(std::is_same_v<typename ContiguousContainer::value_type, u8>,
37 "Underlying type within the contiguous container must be u8.");
34 38
35template <std::size_t Size>
36std::string HexArrayToString(std::array<u8, Size> array, bool upper = true) {
37 std::string out; 39 std::string out;
38 for (u8 c : array) 40 for (const u8 c : data) {
39 out += fmt::format(upper ? "{:02X}" : "{:02x}", c); 41 out += fmt::format(upper ? "{:02X}" : "{:02x}", c);
42 }
40 return out; 43 return out;
41} 44}
42 45
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,
572 << "# If you are experiencing issues involving keys, it may help to delete this file\n"; 572 << "# If you are experiencing issues involving keys, it may help to delete this file\n";
573 } 573 }
574 574
575 file << fmt::format("\n{} = {}", keyname, Common::HexArrayToString(key)); 575 file << fmt::format("\n{} = {}", keyname, Common::HexToString(key));
576 AttemptLoadKeyFile(yuzu_keys_dir, yuzu_keys_dir, filename, category == KeyCategory::Title); 576 AttemptLoadKeyFile(yuzu_keys_dir, yuzu_keys_dir, filename, category == KeyCategory::Title);
577} 577}
578 578
@@ -583,7 +583,7 @@ void KeyManager::SetKey(S128KeyType id, Key128 key, u64 field1, u64 field2) {
583 Key128 rights_id; 583 Key128 rights_id;
584 std::memcpy(rights_id.data(), &field2, sizeof(u64)); 584 std::memcpy(rights_id.data(), &field2, sizeof(u64));
585 std::memcpy(rights_id.data() + sizeof(u64), &field1, sizeof(u64)); 585 std::memcpy(rights_id.data() + sizeof(u64), &field1, sizeof(u64));
586 WriteKeyToFile(KeyCategory::Title, Common::HexArrayToString(rights_id), key); 586 WriteKeyToFile(KeyCategory::Title, Common::HexToString(rights_id), key);
587 } 587 }
588 588
589 auto category = KeyCategory::Standard; 589 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() {
295 LOG_INFO(Loader, 295 LOG_INFO(Loader,
296 "[IPSwitchCompiler ('{}')] - Patching value at offset 0x{:08X} " 296 "[IPSwitchCompiler ('{}')] - Patching value at offset 0x{:08X} "
297 "with byte string '{}'", 297 "with byte string '{}'",
298 patch_text->GetName(), offset, Common::HexVectorToString(replace)); 298 patch_text->GetName(), offset, Common::HexToString(replace));
299 } 299 }
300 300
301 patch.records.insert_or_assign(offset, std::move(replace)); 301 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<VirtualFile> PatchManager::CollectPatches(const std::vector<VirtualD
142 if (!compiler.IsValid()) 142 if (!compiler.IsValid())
143 continue; 143 continue;
144 144
145 auto this_build_id = Common::HexArrayToString(compiler.GetBuildID()); 145 auto this_build_id = Common::HexToString(compiler.GetBuildID());
146 this_build_id = 146 this_build_id =
147 this_build_id.substr(0, this_build_id.find_last_not_of('0') + 1); 147 this_build_id.substr(0, this_build_id.find_last_not_of('0') + 1);
148 148
@@ -168,7 +168,7 @@ std::vector<u8> PatchManager::PatchNSO(const std::vector<u8>& nso, const std::st
168 return nso; 168 return nso;
169 } 169 }
170 170
171 const auto build_id_raw = Common::HexArrayToString(header.build_id); 171 const auto build_id_raw = Common::HexToString(header.build_id);
172 const auto build_id = build_id_raw.substr(0, build_id_raw.find_last_not_of('0') + 1); 172 const auto build_id = build_id_raw.substr(0, build_id_raw.find_last_not_of('0') + 1);
173 173
174 if (Settings::values.dump_nso) { 174 if (Settings::values.dump_nso) {
@@ -219,7 +219,7 @@ std::vector<u8> PatchManager::PatchNSO(const std::vector<u8>& nso, const std::st
219} 219}
220 220
221bool PatchManager::HasNSOPatch(const std::array<u8, 32>& build_id_) const { 221bool PatchManager::HasNSOPatch(const std::array<u8, 32>& build_id_) const {
222 const auto build_id_raw = Common::HexArrayToString(build_id_); 222 const auto build_id_raw = Common::HexToString(build_id_);
223 const auto build_id = build_id_raw.substr(0, build_id_raw.find_last_not_of('0') + 1); 223 const auto build_id = build_id_raw.substr(0, build_id_raw.find_last_not_of('0') + 1);
224 224
225 LOG_INFO(Loader, "Querying NSO patch existence for build_id={}", build_id); 225 LOG_INFO(Loader, "Querying NSO patch existence for build_id={}", build_id);
@@ -235,7 +235,7 @@ bool PatchManager::HasNSOPatch(const std::array<u8, 32>& build_id_) const {
235static std::optional<CheatList> ReadCheatFileFromFolder(const Core::System& system, u64 title_id, 235static std::optional<CheatList> ReadCheatFileFromFolder(const Core::System& system, u64 title_id,
236 const std::array<u8, 0x20>& build_id_, 236 const std::array<u8, 0x20>& build_id_,
237 const VirtualDir& base_path, bool upper) { 237 const VirtualDir& base_path, bool upper) {
238 const auto build_id_raw = Common::HexArrayToString(build_id_, upper); 238 const auto build_id_raw = Common::HexToString(build_id_, upper);
239 const auto build_id = build_id_raw.substr(0, sizeof(u64) * 2); 239 const auto build_id = build_id_raw.substr(0, sizeof(u64) * 2);
240 const auto file = base_path->GetFile(fmt::format("{}.txt", build_id)); 240 const auto file = base_path->GetFile(fmt::format("{}.txt", build_id));
241 241
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) {
53 53
54static std::string GetRelativePathFromNcaID(const std::array<u8, 16>& nca_id, bool second_hex_upper, 54static std::string GetRelativePathFromNcaID(const std::array<u8, 16>& nca_id, bool second_hex_upper,
55 bool within_two_digit) { 55 bool within_two_digit) {
56 if (!within_two_digit) 56 if (!within_two_digit) {
57 return fmt::format("/{}.nca", Common::HexArrayToString(nca_id, second_hex_upper)); 57 return fmt::format("/{}.nca", Common::HexToString(nca_id, second_hex_upper));
58 }
58 59
59 Core::Crypto::SHA256Hash hash{}; 60 Core::Crypto::SHA256Hash hash{};
60 mbedtls_sha256(nca_id.data(), nca_id.size(), hash.data(), 0); 61 mbedtls_sha256(nca_id.data(), nca_id.size(), hash.data(), 0);
61 return fmt::format("/000000{:02X}/{}.nca", hash[0], 62 return fmt::format("/000000{:02X}/{}.nca", hash[0],
62 Common::HexArrayToString(nca_id, second_hex_upper)); 63 Common::HexToString(nca_id, second_hex_upper));
63} 64}
64 65
65static std::string GetCNMTName(TitleType type, u64 title_id) { 66static std::string GetCNMTName(TitleType type, u64 title_id) {
@@ -376,10 +377,11 @@ std::vector<ContentProviderEntry> RegisteredCache::ListEntriesFilter(
376} 377}
377 378
378static std::shared_ptr<NCA> GetNCAFromNSPForID(const NSP& nsp, const NcaID& id) { 379static std::shared_ptr<NCA> GetNCAFromNSPForID(const NSP& nsp, const NcaID& id) {
379 const auto file = nsp.GetFile(fmt::format("{}.nca", Common::HexArrayToString(id, false))); 380 auto file = nsp.GetFile(fmt::format("{}.nca", Common::HexToString(id, false)));
380 if (file == nullptr) 381 if (file == nullptr) {
381 return nullptr; 382 return nullptr;
382 return std::make_shared<NCA>(file); 383 }
384 return std::make_shared<NCA>(std::move(file));
383} 385}
384 386
385InstallResult RegisteredCache::InstallEntry(const XCI& xci, bool overwrite_if_exists, 387InstallResult 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<VirtualFile>& files) {
235 const auto section0 = nca->GetSubdirectories()[0]; 235 const auto section0 = nca->GetSubdirectories()[0];
236 236
237 for (const auto& inner_file : section0->GetFiles()) { 237 for (const auto& inner_file : section0->GetFiles()) {
238 if (inner_file->GetExtension() != "cnmt") 238 if (inner_file->GetExtension() != "cnmt") {
239 continue; 239 continue;
240 }
240 241
241 const CNMT cnmt(inner_file); 242 const CNMT cnmt(inner_file);
242 auto& ncas_title = ncas[cnmt.GetTitleID()]; 243 auto& ncas_title = ncas[cnmt.GetTitleID()];
243 244
244 ncas_title[{cnmt.GetType(), ContentRecordType::Meta}] = nca; 245 ncas_title[{cnmt.GetType(), ContentRecordType::Meta}] = nca;
245 for (const auto& rec : cnmt.GetContentRecords()) { 246 for (const auto& rec : cnmt.GetContentRecords()) {
246 const auto id_string = Common::HexArrayToString(rec.nca_id, false); 247 const auto id_string = Common::HexToString(rec.nca_id, false);
247 const auto next_file = pfs->GetFile(fmt::format("{}.nca", id_string)); 248 auto next_file = pfs->GetFile(fmt::format("{}.nca", id_string));
249
248 if (next_file == nullptr) { 250 if (next_file == nullptr) {
249 LOG_WARNING(Service_FS, 251 LOG_WARNING(Service_FS,
250 "NCA with ID {}.nca is listed in content metadata, but cannot " 252 "NCA with ID {}.nca is listed in content metadata, but cannot "
@@ -253,9 +255,10 @@ void NSP::ReadNCAs(const std::vector<VirtualFile>& files) {
253 continue; 255 continue;
254 } 256 }
255 257
256 auto next_nca = std::make_shared<NCA>(next_file, nullptr, 0, keys); 258 auto next_nca = std::make_shared<NCA>(std::move(next_file), nullptr, 0, keys);
257 if (next_nca->GetType() == NCAContentType::Program) 259 if (next_nca->GetType() == NCAContentType::Program) {
258 program_status[cnmt.GetTitleID()] = next_nca->GetStatus(); 260 program_status[cnmt.GetTitleID()] = next_nca->GetStatus();
261 }
259 if (next_nca->GetStatus() == Loader::ResultStatus::Success || 262 if (next_nca->GetStatus() == Loader::ResultStatus::Success ||
260 (next_nca->GetStatus() == Loader::ResultStatus::ErrorMissingBKTRBaseRomFS && 263 (next_nca->GetStatus() == Loader::ResultStatus::ErrorMissingBKTRBaseRomFS &&
261 (cnmt.GetTitleID() & 0x800) != 0)) { 264 (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<u8, 0x10> nca_id)
66 Core::Crypto::SHA256Hash hash{}; 66 Core::Crypto::SHA256Hash hash{};
67 mbedtls_sha256(nca_id.data(), nca_id.size(), hash.data(), 0); 67 mbedtls_sha256(nca_id.data(), nca_id.size(), hash.data(), 0);
68 status = Parse(fmt::format("/registered/000000{:02X}/{}.nca", hash[0], 68 status = Parse(fmt::format("/registered/000000{:02X}/{}.nca", hash[0],
69 Common::HexArrayToString(nca_id, false))); 69 Common::HexToString(nca_id, false)));
70} 70}
71 71
72NAX::~NAX() = default; 72NAX::~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 @@
2// Licensed under GPLv2 or any later version 2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include <string> 5#include <string_view>
6 6
7#include "common/assert.h" 7#include "common/assert.h"
8#include "common/hex_util.h" 8#include "common/hex_util.h"
@@ -16,21 +16,21 @@
16 16
17namespace Service::AM::Applets { 17namespace Service::AM::Applets {
18 18
19static void LogCurrentStorage(AppletDataBroker& broker, std::string prefix) { 19static void LogCurrentStorage(AppletDataBroker& broker, std::string_view prefix) {
20 std::unique_ptr<IStorage> storage = broker.PopNormalDataToApplet(); 20 std::unique_ptr<IStorage> storage = broker.PopNormalDataToApplet();
21 for (; storage != nullptr; storage = broker.PopNormalDataToApplet()) { 21 for (; storage != nullptr; storage = broker.PopNormalDataToApplet()) {
22 const auto data = storage->GetData(); 22 const auto data = storage->GetData();
23 LOG_INFO(Service_AM, 23 LOG_INFO(Service_AM,
24 "called (STUBBED), during {} recieved normal data with size={:08X}, data={}", 24 "called (STUBBED), during {} received normal data with size={:08X}, data={}",
25 prefix, data.size(), Common::HexVectorToString(data)); 25 prefix, data.size(), Common::HexToString(data));
26 } 26 }
27 27
28 storage = broker.PopInteractiveDataToApplet(); 28 storage = broker.PopInteractiveDataToApplet();
29 for (; storage != nullptr; storage = broker.PopInteractiveDataToApplet()) { 29 for (; storage != nullptr; storage = broker.PopInteractiveDataToApplet()) {
30 const auto data = storage->GetData(); 30 const auto data = storage->GetData();
31 LOG_INFO(Service_AM, 31 LOG_INFO(Service_AM,
32 "called (STUBBED), during {} recieved interactive data with size={:08X}, data={}", 32 "called (STUBBED), during {} received interactive data with size={:08X}, data={}",
33 prefix, data.size(), Common::HexVectorToString(data)); 33 prefix, data.size(), Common::HexToString(data));
34 } 34 }
35} 35}
36 36
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:
310 if (!IsValidNROHash(hash)) { 310 if (!IsValidNROHash(hash)) {
311 LOG_ERROR(Service_LDR, 311 LOG_ERROR(Service_LDR,
312 "NRO hash is not present in any currently loaded NRRs (hash={})!", 312 "NRO hash is not present in any currently loaded NRRs (hash={})!",
313 Common::HexArrayToString(hash)); 313 Common::HexToString(hash));
314 IPC::ResponseBuilder rb{ctx, 2}; 314 IPC::ResponseBuilder rb{ctx, 2};
315 rb.Push(ERROR_MISSING_NRR_HASH); 315 rb.Push(ERROR_MISSING_NRR_HASH);
316 return; 316 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<VAddr> AppLoader_NSO::LoadModule(Kernel::Process& process,
152 auto& system = Core::System::GetInstance(); 152 auto& system = Core::System::GetInstance();
153 const auto cheats = pm->CreateCheatList(system, nso_header.build_id); 153 const auto cheats = pm->CreateCheatList(system, nso_header.build_id);
154 if (!cheats.empty()) { 154 if (!cheats.empty()) {
155 system.RegisterCheatList(cheats, Common::HexArrayToString(nso_header.build_id), 155 system.RegisterCheatList(cheats, Common::HexToString(nso_header.build_id), load_base,
156 load_base, load_base + program_image.size()); 156 load_base + program_image.size());
157 } 157 }
158 } 158 }
159 159