summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/CMakeLists.txt2
-rw-r--r--src/common/file_util.cpp6
-rw-r--r--src/common/file_util.h2
-rw-r--r--src/common/hex_util.cpp27
-rw-r--r--src/common/hex_util.h37
5 files changed, 74 insertions, 0 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt
index 939b8a7d3..d9424ea91 100644
--- a/src/common/CMakeLists.txt
+++ b/src/common/CMakeLists.txt
@@ -38,6 +38,8 @@ add_library(common STATIC
38 file_util.cpp 38 file_util.cpp
39 file_util.h 39 file_util.h
40 hash.h 40 hash.h
41 hex_util.cpp
42 hex_util.h
41 logging/backend.cpp 43 logging/backend.cpp
42 logging/backend.h 44 logging/backend.h
43 logging/filter.cpp 45 logging/filter.cpp
diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp
index 3ce590062..b30a67ff9 100644
--- a/src/common/file_util.cpp
+++ b/src/common/file_util.cpp
@@ -750,6 +750,12 @@ std::string GetHactoolConfigurationPath() {
750#endif 750#endif
751} 751}
752 752
753std::string GetNANDRegistrationDir(bool system) {
754 if (system)
755 return GetUserPath(UserPath::NANDDir) + "system/Contents/registered/";
756 return GetUserPath(UserPath::NANDDir) + "user/Contents/registered/";
757}
758
753size_t WriteStringToFile(bool text_file, const std::string& str, const char* filename) { 759size_t WriteStringToFile(bool text_file, const std::string& str, const char* filename) {
754 return FileUtil::IOFile(filename, text_file ? "w" : "wb").WriteBytes(str.data(), str.size()); 760 return FileUtil::IOFile(filename, text_file ? "w" : "wb").WriteBytes(str.data(), str.size());
755} 761}
diff --git a/src/common/file_util.h b/src/common/file_util.h
index 2711872ae..2f13d0b6b 100644
--- a/src/common/file_util.h
+++ b/src/common/file_util.h
@@ -129,6 +129,8 @@ const std::string& GetUserPath(UserPath path, const std::string& new_path = "");
129 129
130std::string GetHactoolConfigurationPath(); 130std::string GetHactoolConfigurationPath();
131 131
132std::string GetNANDRegistrationDir(bool system = false);
133
132// Returns the path to where the sys file are 134// Returns the path to where the sys file are
133std::string GetSysDirectory(); 135std::string GetSysDirectory();
134 136
diff --git a/src/common/hex_util.cpp b/src/common/hex_util.cpp
new file mode 100644
index 000000000..ae17c89d4
--- /dev/null
+++ b/src/common/hex_util.cpp
@@ -0,0 +1,27 @@
1// Copyright 2013 Dolphin Emulator Project / 2014 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include "common/hex_util.h"
6
7u8 ToHexNibble(char c1) {
8 if (c1 >= 65 && c1 <= 70)
9 return c1 - 55;
10 if (c1 >= 97 && c1 <= 102)
11 return c1 - 87;
12 if (c1 >= 48 && c1 <= 57)
13 return c1 - 48;
14 throw std::logic_error("Invalid hex digit");
15}
16
17std::array<u8, 16> operator""_array16(const char* str, size_t len) {
18 if (len != 32)
19 throw std::logic_error("Not of correct size.");
20 return HexStringToArray<16>(str);
21}
22
23std::array<u8, 32> operator""_array32(const char* str, size_t len) {
24 if (len != 64)
25 throw std::logic_error("Not of correct size.");
26 return HexStringToArray<32>(str);
27}
diff --git a/src/common/hex_util.h b/src/common/hex_util.h
new file mode 100644
index 000000000..13d586015
--- /dev/null
+++ b/src/common/hex_util.h
@@ -0,0 +1,37 @@
1// Copyright 2013 Dolphin Emulator Project / 2014 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <array>
8#include <cstddef>
9#include <string>
10#include <fmt/format.h>
11#include "common/common_types.h"
12
13u8 ToHexNibble(char c1);
14
15template <size_t Size, bool le = false>
16std::array<u8, Size> HexStringToArray(std::string_view str) {
17 std::array<u8, Size> out{};
18 if constexpr (le) {
19 for (size_t i = 2 * Size - 2; i <= 2 * Size; i -= 2)
20 out[i / 2] = (ToHexNibble(str[i]) << 4) | ToHexNibble(str[i + 1]);
21 } else {
22 for (size_t i = 0; i < 2 * Size; i += 2)
23 out[i / 2] = (ToHexNibble(str[i]) << 4) | ToHexNibble(str[i + 1]);
24 }
25 return out;
26}
27
28template <size_t Size>
29std::string HexArrayToString(std::array<u8, Size> array, bool upper = true) {
30 std::string out;
31 for (u8 c : array)
32 out += fmt::format(upper ? "{:02X}" : "{:02x}", c);
33 return out;
34}
35
36std::array<u8, 0x10> operator"" _array16(const char* str, size_t len);
37std::array<u8, 0x20> operator"" _array32(const char* str, size_t len);