diff options
| -rw-r--r-- | src/common/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/common/hex_util.cpp | 27 | ||||
| -rw-r--r-- | src/common/hex_util.h | 35 |
3 files changed, 64 insertions, 0 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index d5d4f6f82..2ad456864 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt | |||
| @@ -40,6 +40,8 @@ add_library(common STATIC | |||
| 40 | file_util.cpp | 40 | file_util.cpp |
| 41 | file_util.h | 41 | file_util.h |
| 42 | hash.h | 42 | hash.h |
| 43 | hex_util.cpp | ||
| 44 | hex_util.h | ||
| 43 | logging/backend.cpp | 45 | logging/backend.cpp |
| 44 | logging/backend.h | 46 | logging/backend.h |
| 45 | logging/filter.cpp | 47 | logging/filter.cpp |
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 | |||
| 7 | u8 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 | |||
| 17 | std::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 | |||
| 23 | std::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..f16c58aab --- /dev/null +++ b/src/common/hex_util.h | |||
| @@ -0,0 +1,35 @@ | |||
| 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 <fmt/format.h> | ||
| 9 | #include "common/common_types.h" | ||
| 10 | |||
| 11 | u8 ToHexNibble(char c1); | ||
| 12 | |||
| 13 | template <size_t Size, bool le = false> | ||
| 14 | std::array<u8, Size> HexStringToArray(std::string_view str) { | ||
| 15 | std::array<u8, Size> out{}; | ||
| 16 | if constexpr (le) { | ||
| 17 | for (size_t i = 2 * Size - 2; i <= 2 * Size; i -= 2) | ||
| 18 | out[i / 2] = (ToHexNibble(str[i]) << 4) | ToHexNibble(str[i + 1]); | ||
| 19 | } else { | ||
| 20 | for (size_t i = 0; i < 2 * Size; i += 2) | ||
| 21 | out[i / 2] = (ToHexNibble(str[i]) << 4) | ToHexNibble(str[i + 1]); | ||
| 22 | } | ||
| 23 | return out; | ||
| 24 | } | ||
| 25 | |||
| 26 | template <size_t Size> | ||
| 27 | std::string HexArrayToString(std::array<u8, Size> array, bool upper = true) { | ||
| 28 | std::string out; | ||
| 29 | for (u8 c : array) | ||
| 30 | out += fmt::format(upper ? "{:02X}" : "{:02x}", c); | ||
| 31 | return out; | ||
| 32 | } | ||
| 33 | |||
| 34 | std::array<u8, 0x10> operator"" _array16(const char* str, size_t len); | ||
| 35 | std::array<u8, 0x20> operator"" _array32(const char* str, size_t len); | ||