1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
12 #include "base/hash/hash.h"
13 #include "base/rand_util.h"
14 #include "base/strings/string_util.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/types/pass_key.h"
22 template <typename Char>
23 constexpr bool IsLowerHexDigit(Char c) {
24 return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f');
27 constexpr bool IsHyphenPosition(size_t i) {
28 return i == 8 || i == 13 || i == 18 || i == 23;
31 // Returns a canonical Uuid string given that `input` is validly formatted
32 // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, such that x is a hexadecimal digit.
33 // If `strict`, x must be a lower-case hexadecimal digit.
34 template <typename StringPieceType>
35 std::string GetCanonicalUuidInternal(StringPieceType input, bool strict) {
36 using CharType = typename StringPieceType::value_type;
38 constexpr size_t kUuidLength = 36;
39 if (input.length() != kUuidLength) {
43 std::string lowercase_;
44 lowercase_.resize(kUuidLength);
45 for (size_t i = 0; i < input.length(); ++i) {
46 CharType current = input[i];
47 if (IsHyphenPosition(i)) {
53 if (strict ? !IsLowerHexDigit(current) : !IsHexDigit(current)) {
56 lowercase_[i] = static_cast<char>(ToLowerASCII(current));
66 Uuid Uuid::GenerateRandomV4() {
67 uint8_t sixteen_bytes[kGuidV4InputLength];
68 // Use base::RandBytes instead of crypto::RandBytes, because crypto calls the
69 // base version directly, and to prevent the dependency from base/ to crypto/.
70 RandBytes(&sixteen_bytes, sizeof(sixteen_bytes));
71 return FormatRandomDataAsV4Impl(sixteen_bytes);
75 Uuid Uuid::FormatRandomDataAsV4(
76 base::span<const uint8_t, 16> input,
77 base::PassKey<content::FileSystemAccessManagerImpl> /*pass_key*/) {
78 return FormatRandomDataAsV4Impl(input);
82 Uuid Uuid::FormatRandomDataAsV4ForTesting(base::span<const uint8_t, 16> input) {
83 return FormatRandomDataAsV4Impl(input);
87 Uuid Uuid::FormatRandomDataAsV4Impl(base::span<const uint8_t, 16> input) {
88 DCHECK_EQ(input.size_bytes(), kGuidV4InputLength);
90 uint64_t sixteen_bytes[2];
91 memcpy(&sixteen_bytes, input.data(), sizeof(sixteen_bytes));
93 // Set the Uuid to version 4 as described in RFC 4122, section 4.4.
94 // The format of Uuid version 4 must be xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx,
95 // where y is one of [8, 9, a, b].
97 // Clear the version bits and set the version to 4:
98 sixteen_bytes[0] &= 0xffffffff'ffff0fffULL;
99 sixteen_bytes[0] |= 0x00000000'00004000ULL;
101 // Set the two most significant bits (bits 6 and 7) of the
102 // clock_seq_hi_and_reserved to zero and one, respectively:
103 sixteen_bytes[1] &= 0x3fffffff'ffffffffULL;
104 sixteen_bytes[1] |= 0x80000000'00000000ULL;
108 StringPrintf("%08x-%04x-%04x-%04x-%012llx",
109 static_cast<uint32_t>(sixteen_bytes[0] >> 32),
110 static_cast<uint32_t>((sixteen_bytes[0] >> 16) & 0x0000ffff),
111 static_cast<uint32_t>(sixteen_bytes[0] & 0x0000ffff),
112 static_cast<uint32_t>(sixteen_bytes[1] >> 48),
113 sixteen_bytes[1] & 0x0000ffff'ffffffffULL);
118 Uuid Uuid::ParseCaseInsensitive(StringPiece input) {
120 uuid.lowercase_ = GetCanonicalUuidInternal(input, /*strict=*/false);
125 Uuid Uuid::ParseCaseInsensitive(StringPiece16 input) {
127 uuid.lowercase_ = GetCanonicalUuidInternal(input, /*strict=*/false);
132 Uuid Uuid::ParseLowercase(StringPiece input) {
134 uuid.lowercase_ = GetCanonicalUuidInternal(input, /*strict=*/true);
139 Uuid Uuid::ParseLowercase(StringPiece16 input) {
141 uuid.lowercase_ = GetCanonicalUuidInternal(input, /*strict=*/true);
145 Uuid::Uuid() = default;
147 Uuid::Uuid(const Uuid& other) = default;
149 Uuid& Uuid::operator=(const Uuid& other) = default;
151 Uuid::Uuid(Uuid&& other) = default;
153 Uuid& Uuid::operator=(Uuid&& other) = default;
155 const std::string& Uuid::AsLowercaseString() const {
159 bool Uuid::operator==(const Uuid& other) const {
160 return AsLowercaseString() == other.AsLowercaseString();
163 bool Uuid::operator!=(const Uuid& other) const {
164 return !(*this == other);
167 bool Uuid::operator<(const Uuid& other) const {
168 return AsLowercaseString() < other.AsLowercaseString();
171 bool Uuid::operator<=(const Uuid& other) const {
172 return *this < other || *this == other;
175 bool Uuid::operator>(const Uuid& other) const {
176 return !(*this <= other);
179 bool Uuid::operator>=(const Uuid& other) const {
180 return !(*this < other);
183 std::ostream& operator<<(std::ostream& out, const Uuid& uuid) {
184 return out << uuid.AsLowercaseString();
187 size_t UuidHash::operator()(const Uuid& uuid) const {
188 // TODO(crbug.com/1026195): Avoid converting to string to take the hash when
189 // the internal type is migrated to a non-string type.
190 return FastHash(uuid.AsLowercaseString());