diff options
author | Morph <39850852+Morph1984@users.noreply.github.com> | 2022-02-05 19:07:51 +0100 |
---|---|---|
committer | Morph <39850852+Morph1984@users.noreply.github.com> | 2022-02-05 19:56:21 +0100 |
commit | ec4d7f71fedb1cc117b0fb6c33cbffb699b98555 (patch) | |
tree | a76907fd120a1f963835f888b6f974618ef84456 | |
parent | general: Rename NewUUID to UUID, and remove the previous UUID impl (diff) | |
download | yuzu-ec4d7f71fedb1cc117b0fb6c33cbffb699b98555.tar yuzu-ec4d7f71fedb1cc117b0fb6c33cbffb699b98555.tar.gz yuzu-ec4d7f71fedb1cc117b0fb6c33cbffb699b98555.tar.bz2 yuzu-ec4d7f71fedb1cc117b0fb6c33cbffb699b98555.tar.lz yuzu-ec4d7f71fedb1cc117b0fb6c33cbffb699b98555.tar.xz yuzu-ec4d7f71fedb1cc117b0fb6c33cbffb699b98555.tar.zst yuzu-ec4d7f71fedb1cc117b0fb6c33cbffb699b98555.zip |
-rw-r--r-- | src/common/uuid.cpp | 53 |
1 files changed, 39 insertions, 14 deletions
diff --git a/src/common/uuid.cpp b/src/common/uuid.cpp index 10a1b86e0..4aab10e08 100644 --- a/src/common/uuid.cpp +++ b/src/common/uuid.cpp @@ -3,6 +3,7 @@ // Refer to the license.txt file included. #include <bit> +#include <optional> #include <random> #include <fmt/format.h> @@ -18,7 +19,7 @@ namespace { constexpr size_t RawStringSize = sizeof(UUID) * 2; constexpr size_t FormattedStringSize = RawStringSize + 4; -u8 HexCharToByte(char c) { +std::optional<u8> HexCharToByte(char c) { if (c >= '0' && c <= '9') { return static_cast<u8>(c - '0'); } @@ -29,15 +30,19 @@ u8 HexCharToByte(char c) { return static_cast<u8>(c - 'A' + 10); } ASSERT_MSG(false, "{} is not a hexadecimal digit!", c); - return u8{0}; + return std::nullopt; } std::array<u8, 0x10> ConstructFromRawString(std::string_view raw_string) { std::array<u8, 0x10> uuid; for (size_t i = 0; i < RawStringSize; i += 2) { - uuid[i / 2] = - static_cast<u8>((HexCharToByte(raw_string[i]) << 4) | HexCharToByte(raw_string[i + 1])); + const auto upper = HexCharToByte(raw_string[i]); + const auto lower = HexCharToByte(raw_string[i + 1]); + if (!upper || !lower) { + return {}; + } + uuid[i / 2] = static_cast<u8>((*upper << 4) | *lower); } return uuid; @@ -52,40 +57,60 @@ std::array<u8, 0x10> ConstructFromFormattedString(std::string_view formatted_str const auto* str = formatted_string.data(); for (; i < 4; ++i) { - uuid[i] = static_cast<u8>((HexCharToByte(*(str++)) << 4)); - uuid[i] |= HexCharToByte(*(str++)); + const auto upper = HexCharToByte(*(str++)); + const auto lower = HexCharToByte(*(str++)); + if (!upper || !lower) { + return {}; + } + uuid[i] = static_cast<u8>((*upper << 4) | *lower); } // Process the next 4 characters. ++str; for (; i < 6; ++i) { - uuid[i] = static_cast<u8>((HexCharToByte(*(str++)) << 4)); - uuid[i] |= HexCharToByte(*(str++)); + const auto upper = HexCharToByte(*(str++)); + const auto lower = HexCharToByte(*(str++)); + if (!upper || !lower) { + return {}; + } + uuid[i] = static_cast<u8>((*upper << 4) | *lower); } // Process the next 4 characters. ++str; for (; i < 8; ++i) { - uuid[i] = static_cast<u8>((HexCharToByte(*(str++)) << 4)); - uuid[i] |= HexCharToByte(*(str++)); + const auto upper = HexCharToByte(*(str++)); + const auto lower = HexCharToByte(*(str++)); + if (!upper || !lower) { + return {}; + } + uuid[i] = static_cast<u8>((*upper << 4) | *lower); } // Process the next 4 characters. ++str; for (; i < 10; ++i) { - uuid[i] = static_cast<u8>((HexCharToByte(*(str++)) << 4)); - uuid[i] |= HexCharToByte(*(str++)); + const auto upper = HexCharToByte(*(str++)); + const auto lower = HexCharToByte(*(str++)); + if (!upper || !lower) { + return {}; + } + uuid[i] = static_cast<u8>((*upper << 4) | *lower); } // Process the last 12 characters. ++str; for (; i < 16; ++i) { - uuid[i] = static_cast<u8>((HexCharToByte(*(str++)) << 4)); - uuid[i] |= HexCharToByte(*(str++)); + const auto upper = HexCharToByte(*(str++)); + const auto lower = HexCharToByte(*(str++)); + if (!upper || !lower) { + return {}; + } + uuid[i] = static_cast<u8>((*upper << 4) | *lower); } return uuid; |