guid.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/guid.h"
  5. #include <stddef.h>
  6. #include <stdint.h>
  7. #include <ostream>
  8. #include "base/rand_util.h"
  9. #include "base/strings/string_util.h"
  10. #include "base/strings/stringprintf.h"
  11. namespace base {
  12. namespace {
  13. template <typename Char>
  14. constexpr bool IsLowerHexDigit(Char c) {
  15. return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f');
  16. }
  17. constexpr bool IsHyphenPosition(size_t i) {
  18. return i == 8 || i == 13 || i == 18 || i == 23;
  19. }
  20. // Returns a canonical GUID string given that `input` is validly formatted
  21. // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, such that x is a hexadecimal digit.
  22. // If `strict`, x must be a lower-case hexadecimal digit.
  23. template <typename StringPieceType>
  24. std::string GetCanonicalGUIDInternal(StringPieceType input, bool strict) {
  25. using CharType = typename StringPieceType::value_type;
  26. constexpr size_t kGUIDLength = 36;
  27. if (input.length() != kGUIDLength)
  28. return std::string();
  29. std::string lowercase_;
  30. lowercase_.resize(kGUIDLength);
  31. for (size_t i = 0; i < input.length(); ++i) {
  32. CharType current = input[i];
  33. if (IsHyphenPosition(i)) {
  34. if (current != '-')
  35. return std::string();
  36. lowercase_[i] = '-';
  37. } else {
  38. if (strict ? !IsLowerHexDigit(current) : !IsHexDigit(current))
  39. return std::string();
  40. lowercase_[i] = static_cast<char>(ToLowerASCII(current));
  41. }
  42. }
  43. return lowercase_;
  44. }
  45. } // namespace
  46. std::string GenerateGUID() {
  47. GUID guid = GUID::GenerateRandomV4();
  48. return guid.AsLowercaseString();
  49. }
  50. bool IsValidGUID(StringPiece input) {
  51. return !GetCanonicalGUIDInternal(input, /*strict=*/false).empty();
  52. }
  53. bool IsValidGUID(StringPiece16 input) {
  54. return !GetCanonicalGUIDInternal(input, /*strict=*/false).empty();
  55. }
  56. bool IsValidGUIDOutputString(StringPiece input) {
  57. return !GetCanonicalGUIDInternal(input, /*strict=*/true).empty();
  58. }
  59. std::string RandomDataToGUIDString(const uint64_t bytes[2]) {
  60. return StringPrintf(
  61. "%08x-%04x-%04x-%04x-%012llx", static_cast<uint32_t>(bytes[0] >> 32),
  62. static_cast<uint32_t>((bytes[0] >> 16) & 0x0000ffff),
  63. static_cast<uint32_t>(bytes[0] & 0x0000ffff),
  64. static_cast<uint32_t>(bytes[1] >> 48), bytes[1] & 0x0000ffff'ffffffffULL);
  65. }
  66. // static
  67. GUID GUID::GenerateRandomV4() {
  68. uint64_t sixteen_bytes[2];
  69. // Use base::RandBytes instead of crypto::RandBytes, because crypto calls the
  70. // base version directly, and to prevent the dependency from base/ to crypto/.
  71. RandBytes(&sixteen_bytes, sizeof(sixteen_bytes));
  72. // Set the GUID to version 4 as described in RFC 4122, section 4.4.
  73. // The format of GUID version 4 must be xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx,
  74. // where y is one of [8, 9, a, b].
  75. // Clear the version bits and set the version to 4:
  76. sixteen_bytes[0] &= 0xffffffff'ffff0fffULL;
  77. sixteen_bytes[0] |= 0x00000000'00004000ULL;
  78. // Set the two most significant bits (bits 6 and 7) of the
  79. // clock_seq_hi_and_reserved to zero and one, respectively:
  80. sixteen_bytes[1] &= 0x3fffffff'ffffffffULL;
  81. sixteen_bytes[1] |= 0x80000000'00000000ULL;
  82. GUID guid;
  83. guid.lowercase_ = RandomDataToGUIDString(sixteen_bytes);
  84. return guid;
  85. }
  86. // static
  87. GUID GUID::ParseCaseInsensitive(StringPiece input) {
  88. GUID guid;
  89. guid.lowercase_ = GetCanonicalGUIDInternal(input, /*strict=*/false);
  90. return guid;
  91. }
  92. // static
  93. GUID GUID::ParseCaseInsensitive(StringPiece16 input) {
  94. GUID guid;
  95. guid.lowercase_ = GetCanonicalGUIDInternal(input, /*strict=*/false);
  96. return guid;
  97. }
  98. // static
  99. GUID GUID::ParseLowercase(StringPiece input) {
  100. GUID guid;
  101. guid.lowercase_ = GetCanonicalGUIDInternal(input, /*strict=*/true);
  102. return guid;
  103. }
  104. // static
  105. GUID GUID::ParseLowercase(StringPiece16 input) {
  106. GUID guid;
  107. guid.lowercase_ = GetCanonicalGUIDInternal(input, /*strict=*/true);
  108. return guid;
  109. }
  110. GUID::GUID() = default;
  111. GUID::GUID(const GUID& other) = default;
  112. GUID& GUID::operator=(const GUID& other) = default;
  113. GUID::GUID(GUID&& other) = default;
  114. GUID& GUID::operator=(GUID&& other) = default;
  115. const std::string& GUID::AsLowercaseString() const {
  116. return lowercase_;
  117. }
  118. bool GUID::operator==(const GUID& other) const {
  119. return AsLowercaseString() == other.AsLowercaseString();
  120. }
  121. bool GUID::operator!=(const GUID& other) const {
  122. return !(*this == other);
  123. }
  124. bool GUID::operator<(const GUID& other) const {
  125. return AsLowercaseString() < other.AsLowercaseString();
  126. }
  127. bool GUID::operator<=(const GUID& other) const {
  128. return *this < other || *this == other;
  129. }
  130. bool GUID::operator>(const GUID& other) const {
  131. return !(*this <= other);
  132. }
  133. bool GUID::operator>=(const GUID& other) const {
  134. return !(*this < other);
  135. }
  136. std::ostream& operator<<(std::ostream& out, const GUID& guid) {
  137. return out << guid.AsLowercaseString();
  138. }
  139. } // namespace base