diff options
| author | 2022-02-04 23:44:02 -0500 | |
|---|---|---|
| committer | 2022-02-05 13:18:31 -0500 | |
| commit | 3271099fea41d5adc0003c02c8481b334772296a (patch) | |
| tree | 10006c106d3a317639d330b45369973fa29af562 /src | |
| parent | Merge pull request #7842 from german77/vibration_test (diff) | |
| download | yuzu-3271099fea41d5adc0003c02c8481b334772296a.tar.gz yuzu-3271099fea41d5adc0003c02c8481b334772296a.tar.xz yuzu-3271099fea41d5adc0003c02c8481b334772296a.zip | |
common: Implement NewUUID
This is a fixed and revised implementation of UUID that uses an array of bytes as its internal representation of a UUID instead of a u128 (which was an array of 2 u64s).
In addition to this, the generation of RFC 4122 Version 4 compliant UUIDs is also implemented.
Diffstat (limited to '')
| -rw-r--r-- | src/common/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/common/new_uuid.cpp | 182 | ||||
| -rw-r--r-- | src/common/new_uuid.h | 138 |
3 files changed, 322 insertions, 0 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index adf70eb8b..3dd460191 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt | |||
| @@ -99,6 +99,8 @@ add_library(common STATIC | |||
| 99 | microprofile.cpp | 99 | microprofile.cpp |
| 100 | microprofile.h | 100 | microprofile.h |
| 101 | microprofileui.h | 101 | microprofileui.h |
| 102 | new_uuid.cpp | ||
| 103 | new_uuid.h | ||
| 102 | nvidia_flags.cpp | 104 | nvidia_flags.cpp |
| 103 | nvidia_flags.h | 105 | nvidia_flags.h |
| 104 | page_table.cpp | 106 | page_table.cpp |
diff --git a/src/common/new_uuid.cpp b/src/common/new_uuid.cpp new file mode 100644 index 000000000..0442681c8 --- /dev/null +++ b/src/common/new_uuid.cpp | |||
| @@ -0,0 +1,182 @@ | |||
| 1 | // Copyright 2022 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <bit> | ||
| 6 | #include <random> | ||
| 7 | |||
| 8 | #include <fmt/format.h> | ||
| 9 | |||
| 10 | #include "common/assert.h" | ||
| 11 | #include "common/new_uuid.h" | ||
| 12 | #include "common/tiny_mt.h" | ||
| 13 | |||
| 14 | namespace Common { | ||
| 15 | |||
| 16 | namespace { | ||
| 17 | |||
| 18 | constexpr size_t RawStringSize = sizeof(NewUUID) * 2; | ||
| 19 | constexpr size_t FormattedStringSize = RawStringSize + 4; | ||
| 20 | |||
| 21 | u8 HexCharToByte(char c) { | ||
| 22 | if (c >= '0' && c <= '9') { | ||
| 23 | return static_cast<u8>(c - '0'); | ||
| 24 | } | ||
| 25 | if (c >= 'a' && c <= 'f') { | ||
| 26 | return static_cast<u8>(c - 'a' + 10); | ||
| 27 | } | ||
| 28 | if (c >= 'A' && c <= 'F') { | ||
| 29 | return static_cast<u8>(c - 'A' + 10); | ||
| 30 | } | ||
| 31 | ASSERT_MSG(false, "{} is not a hexadecimal digit!", c); | ||
| 32 | return u8{0}; | ||
| 33 | } | ||
| 34 | |||
| 35 | std::array<u8, 0x10> ConstructFromRawString(std::string_view raw_string) { | ||
| 36 | std::array<u8, 0x10> uuid; | ||
| 37 | |||
| 38 | for (size_t i = 0; i < RawStringSize; i += 2) { | ||
| 39 | uuid[i / 2] = | ||
| 40 | static_cast<u8>((HexCharToByte(raw_string[i]) << 4) | HexCharToByte(raw_string[i + 1])); | ||
| 41 | } | ||
| 42 | |||
| 43 | return uuid; | ||
| 44 | } | ||
| 45 | |||
| 46 | std::array<u8, 0x10> ConstructFromFormattedString(std::string_view formatted_string) { | ||
| 47 | std::array<u8, 0x10> uuid; | ||
| 48 | |||
| 49 | size_t i = 0; | ||
| 50 | |||
| 51 | // Process the first 8 characters. | ||
| 52 | const auto* str = formatted_string.data(); | ||
| 53 | |||
| 54 | for (; i < 4; ++i) { | ||
| 55 | uuid[i] = static_cast<u8>((HexCharToByte(*(str++)) << 4)); | ||
| 56 | uuid[i] |= HexCharToByte(*(str++)); | ||
| 57 | } | ||
| 58 | |||
| 59 | // Process the next 4 characters. | ||
| 60 | ++str; | ||
| 61 | |||
| 62 | for (; i < 6; ++i) { | ||
| 63 | uuid[i] = static_cast<u8>((HexCharToByte(*(str++)) << 4)); | ||
| 64 | uuid[i] |= HexCharToByte(*(str++)); | ||
| 65 | } | ||
| 66 | |||
| 67 | // Process the next 4 characters. | ||
| 68 | ++str; | ||
| 69 | |||
| 70 | for (; i < 8; ++i) { | ||
| 71 | uuid[i] = static_cast<u8>((HexCharToByte(*(str++)) << 4)); | ||
| 72 | uuid[i] |= HexCharToByte(*(str++)); | ||
| 73 | } | ||
| 74 | |||
| 75 | // Process the next 4 characters. | ||
| 76 | ++str; | ||
| 77 | |||
| 78 | for (; i < 10; ++i) { | ||
| 79 | uuid[i] = static_cast<u8>((HexCharToByte(*(str++)) << 4)); | ||
| 80 | uuid[i] |= HexCharToByte(*(str++)); | ||
| 81 | } | ||
| 82 | |||
| 83 | // Process the last 12 characters. | ||
| 84 | ++str; | ||
| 85 | |||
| 86 | for (; i < 16; ++i) { | ||
| 87 | uuid[i] = static_cast<u8>((HexCharToByte(*(str++)) << 4)); | ||
| 88 | uuid[i] |= HexCharToByte(*(str++)); | ||
| 89 | } | ||
| 90 | |||
| 91 | return uuid; | ||
| 92 | } | ||
| 93 | |||
| 94 | std::array<u8, 0x10> ConstructUUID(std::string_view uuid_string) { | ||
| 95 | const auto length = uuid_string.length(); | ||
| 96 | |||
| 97 | if (length == 0) { | ||
| 98 | return {}; | ||
| 99 | } | ||
| 100 | |||
| 101 | // Check if the input string contains 32 hexadecimal characters. | ||
| 102 | if (length == RawStringSize) { | ||
| 103 | return ConstructFromRawString(uuid_string); | ||
| 104 | } | ||
| 105 | |||
| 106 | // Check if the input string has the length of a RFC 4122 formatted UUID string. | ||
| 107 | if (length == FormattedStringSize) { | ||
| 108 | return ConstructFromFormattedString(uuid_string); | ||
| 109 | } | ||
| 110 | |||
| 111 | ASSERT_MSG(false, "UUID string has an invalid length of {} characters!", length); | ||
| 112 | |||
| 113 | return {}; | ||
| 114 | } | ||
| 115 | |||
| 116 | } // Anonymous namespace | ||
| 117 | |||
| 118 | NewUUID::NewUUID(std::string_view uuid_string) : uuid{ConstructUUID(uuid_string)} {} | ||
| 119 | |||
| 120 | std::string NewUUID::RawString() const { | ||
| 121 | return fmt::format("{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}" | ||
| 122 | "{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}", | ||
| 123 | uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5], uuid[6], uuid[7], | ||
| 124 | uuid[8], uuid[9], uuid[10], uuid[11], uuid[12], uuid[13], uuid[14], | ||
| 125 | uuid[15]); | ||
| 126 | } | ||
| 127 | |||
| 128 | std::string NewUUID::FormattedString() const { | ||
| 129 | return fmt::format("{:02x}{:02x}{:02x}{:02x}" | ||
| 130 | "-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-" | ||
| 131 | "{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}", | ||
| 132 | uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5], uuid[6], uuid[7], | ||
| 133 | uuid[8], uuid[9], uuid[10], uuid[11], uuid[12], uuid[13], uuid[14], | ||
| 134 | uuid[15]); | ||
| 135 | } | ||
| 136 | |||
| 137 | size_t NewUUID::Hash() const noexcept { | ||
| 138 | u64 hash; | ||
| 139 | u64 temp; | ||
| 140 | |||
| 141 | std::memcpy(&hash, uuid.data(), sizeof(u64)); | ||
| 142 | std::memcpy(&temp, uuid.data() + 8, sizeof(u64)); | ||
| 143 | |||
| 144 | return hash ^ std::rotl(temp, 1); | ||
| 145 | } | ||
| 146 | |||
| 147 | NewUUID NewUUID::MakeRandom() { | ||
| 148 | std::random_device device; | ||
| 149 | |||
| 150 | return MakeRandomWithSeed(device()); | ||
| 151 | } | ||
| 152 | |||
| 153 | NewUUID NewUUID::MakeRandomWithSeed(u32 seed) { | ||
| 154 | // Create and initialize our RNG. | ||
| 155 | TinyMT rng; | ||
| 156 | rng.Initialize(seed); | ||
| 157 | |||
| 158 | NewUUID uuid; | ||
| 159 | |||
| 160 | // Populate the UUID with random bytes. | ||
| 161 | rng.GenerateRandomBytes(uuid.uuid.data(), sizeof(NewUUID)); | ||
| 162 | |||
| 163 | return uuid; | ||
| 164 | } | ||
| 165 | |||
| 166 | NewUUID NewUUID::MakeRandomRFC4122V4() { | ||
| 167 | auto uuid = MakeRandom(); | ||
| 168 | |||
| 169 | // According to Proposed Standard RFC 4122 Section 4.4, we must: | ||
| 170 | |||
| 171 | // 1. Set the two most significant bits (bits 6 and 7) of the | ||
| 172 | // clock_seq_hi_and_reserved to zero and one, respectively. | ||
| 173 | uuid.uuid[8] = 0x80 | (uuid.uuid[8] & 0x3F); | ||
| 174 | |||
| 175 | // 2. Set the four most significant bits (bits 12 through 15) of the | ||
| 176 | // time_hi_and_version field to the 4-bit version number from Section 4.1.3. | ||
| 177 | uuid.uuid[6] = 0x40 | (uuid.uuid[6] & 0xF); | ||
| 178 | |||
| 179 | return uuid; | ||
| 180 | } | ||
| 181 | |||
| 182 | } // namespace Common | ||
diff --git a/src/common/new_uuid.h b/src/common/new_uuid.h new file mode 100644 index 000000000..bd4468ad2 --- /dev/null +++ b/src/common/new_uuid.h | |||
| @@ -0,0 +1,138 @@ | |||
| 1 | // Copyright 2022 yuzu 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 <functional> | ||
| 9 | #include <string> | ||
| 10 | #include <string_view> | ||
| 11 | |||
| 12 | #include "common/common_types.h" | ||
| 13 | |||
| 14 | namespace Common { | ||
| 15 | |||
| 16 | struct NewUUID { | ||
| 17 | std::array<u8, 0x10> uuid{}; | ||
| 18 | |||
| 19 | /// Constructs an invalid UUID. | ||
| 20 | constexpr NewUUID() = default; | ||
| 21 | |||
| 22 | /// Constructs a UUID from a reference to a 128 bit array. | ||
| 23 | constexpr explicit NewUUID(const std::array<u8, 16>& uuid_) : uuid{uuid_} {} | ||
| 24 | |||
| 25 | /** | ||
| 26 | * Constructs a UUID from either: | ||
| 27 | * 1. A 32 hexadecimal character string representing the bytes of the UUID | ||
| 28 | * 2. A RFC 4122 formatted UUID string, in the format xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx | ||
| 29 | * | ||
| 30 | * The input string may contain uppercase or lowercase characters, but they must: | ||
| 31 | * 1. Contain valid hexadecimal characters (0-9, a-f, A-F) | ||
| 32 | * 2. Not contain the "0x" hexadecimal prefix | ||
| 33 | * | ||
| 34 | * Should the input string not meet the above requirements, | ||
| 35 | * an assert will be triggered and an invalid UUID is set instead. | ||
| 36 | */ | ||
| 37 | explicit NewUUID(std::string_view uuid_string); | ||
| 38 | |||
| 39 | ~NewUUID() = default; | ||
| 40 | |||
| 41 | constexpr NewUUID(const NewUUID&) noexcept = default; | ||
| 42 | constexpr NewUUID(NewUUID&&) noexcept = default; | ||
| 43 | |||
| 44 | constexpr NewUUID& operator=(const NewUUID&) noexcept = default; | ||
| 45 | constexpr NewUUID& operator=(NewUUID&&) noexcept = default; | ||
| 46 | |||
| 47 | /** | ||
| 48 | * Returns whether the stored UUID is valid or not. | ||
| 49 | * | ||
| 50 | * @returns True if the stored UUID is valid, false otherwise. | ||
| 51 | */ | ||
| 52 | constexpr bool IsValid() const { | ||
| 53 | return uuid != std::array<u8, 0x10>{}; | ||
| 54 | } | ||
| 55 | |||
| 56 | /** | ||
| 57 | * Returns whether the stored UUID is invalid or not. | ||
| 58 | * | ||
| 59 | * @returns True if the stored UUID is invalid, false otherwise. | ||
| 60 | */ | ||
| 61 | constexpr bool IsInvalid() const { | ||
| 62 | return !IsValid(); | ||
| 63 | } | ||
| 64 | |||
| 65 | /** | ||
| 66 | * Returns a 32 hexadecimal character string representing the bytes of the UUID. | ||
| 67 | * | ||
| 68 | * @returns A 32 hexadecimal character string of the UUID. | ||
| 69 | */ | ||
| 70 | std::string RawString() const; | ||
| 71 | |||
| 72 | /** | ||
| 73 | * Returns a RFC 4122 formatted UUID string in the format xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx. | ||
| 74 | * | ||
| 75 | * @returns A RFC 4122 formatted UUID string. | ||
| 76 | */ | ||
| 77 | std::string FormattedString() const; | ||
| 78 | |||
| 79 | /** | ||
| 80 | * Returns a 64-bit hash of the UUID for use in hash table data structures. | ||
| 81 | * | ||
| 82 | * @returns A 64-bit hash of the UUID. | ||
| 83 | */ | ||
| 84 | size_t Hash() const noexcept; | ||
| 85 | |||
| 86 | /** | ||
| 87 | * Creates a default UUID "yuzu Default UID". | ||
| 88 | * | ||
| 89 | * @returns A UUID with its bytes set to the ASCII values of "yuzu Default UID". | ||
| 90 | */ | ||
| 91 | static constexpr NewUUID MakeDefault() { | ||
| 92 | return NewUUID{ | ||
| 93 | {'y', 'u', 'z', 'u', ' ', 'D', 'e', 'f', 'a', 'u', 'l', 't', ' ', 'U', 'I', 'D'}, | ||
| 94 | }; | ||
| 95 | } | ||
| 96 | |||
| 97 | /** | ||
| 98 | * Creates a random UUID. | ||
| 99 | * | ||
| 100 | * @returns A random UUID. | ||
| 101 | */ | ||
| 102 | static NewUUID MakeRandom(); | ||
| 103 | |||
| 104 | /** | ||
| 105 | * Creates a random UUID with a seed. | ||
| 106 | * | ||
| 107 | * @param seed A seed to initialize the Mersenne-Twister RNG | ||
| 108 | * | ||
| 109 | * @returns A random UUID. | ||
| 110 | */ | ||
| 111 | static NewUUID MakeRandomWithSeed(u32 seed); | ||
| 112 | |||
| 113 | /** | ||
| 114 | * Creates a random UUID. The generated UUID is RFC 4122 Version 4 compliant. | ||
| 115 | * | ||
| 116 | * @returns A random UUID that is RFC 4122 Version 4 compliant. | ||
| 117 | */ | ||
| 118 | static NewUUID MakeRandomRFC4122V4(); | ||
| 119 | |||
| 120 | friend constexpr bool operator==(const NewUUID& lhs, const NewUUID& rhs) = default; | ||
| 121 | }; | ||
| 122 | static_assert(sizeof(NewUUID) == 0x10, "UUID has incorrect size."); | ||
| 123 | |||
| 124 | /// An invalid UUID. This UUID has all its bytes set to 0. | ||
| 125 | constexpr NewUUID InvalidUUID = {}; | ||
| 126 | |||
| 127 | } // namespace Common | ||
| 128 | |||
| 129 | namespace std { | ||
| 130 | |||
| 131 | template <> | ||
| 132 | struct hash<Common::NewUUID> { | ||
| 133 | size_t operator()(const Common::NewUUID& uuid) const noexcept { | ||
| 134 | return uuid.Hash(); | ||
| 135 | } | ||
| 136 | }; | ||
| 137 | |||
| 138 | } // namespace std | ||