diff options
Diffstat (limited to 'src/common/new_uuid.cpp')
| -rw-r--r-- | src/common/new_uuid.cpp | 188 |
1 files changed, 0 insertions, 188 deletions
diff --git a/src/common/new_uuid.cpp b/src/common/new_uuid.cpp deleted file mode 100644 index f2f0077ae..000000000 --- a/src/common/new_uuid.cpp +++ /dev/null | |||
| @@ -1,188 +0,0 @@ | |||
| 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 | u128 NewUUID::AsU128() const { | ||
| 148 | u128 uuid_old; | ||
| 149 | std::memcpy(&uuid_old, uuid.data(), sizeof(NewUUID)); | ||
| 150 | return uuid_old; | ||
| 151 | } | ||
| 152 | |||
| 153 | NewUUID NewUUID::MakeRandom() { | ||
| 154 | std::random_device device; | ||
| 155 | |||
| 156 | return MakeRandomWithSeed(device()); | ||
| 157 | } | ||
| 158 | |||
| 159 | NewUUID NewUUID::MakeRandomWithSeed(u32 seed) { | ||
| 160 | // Create and initialize our RNG. | ||
| 161 | TinyMT rng; | ||
| 162 | rng.Initialize(seed); | ||
| 163 | |||
| 164 | NewUUID uuid; | ||
| 165 | |||
| 166 | // Populate the UUID with random bytes. | ||
| 167 | rng.GenerateRandomBytes(uuid.uuid.data(), sizeof(NewUUID)); | ||
| 168 | |||
| 169 | return uuid; | ||
| 170 | } | ||
| 171 | |||
| 172 | NewUUID NewUUID::MakeRandomRFC4122V4() { | ||
| 173 | auto uuid = MakeRandom(); | ||
| 174 | |||
| 175 | // According to Proposed Standard RFC 4122 Section 4.4, we must: | ||
| 176 | |||
| 177 | // 1. Set the two most significant bits (bits 6 and 7) of the | ||
| 178 | // clock_seq_hi_and_reserved to zero and one, respectively. | ||
| 179 | uuid.uuid[8] = 0x80 | (uuid.uuid[8] & 0x3F); | ||
| 180 | |||
| 181 | // 2. Set the four most significant bits (bits 12 through 15) of the | ||
| 182 | // time_hi_and_version field to the 4-bit version number from Section 4.1.3. | ||
| 183 | uuid.uuid[6] = 0x40 | (uuid.uuid[6] & 0xF); | ||
| 184 | |||
| 185 | return uuid; | ||
| 186 | } | ||
| 187 | |||
| 188 | } // namespace Common | ||