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.
4
5 #include "base/uuid.h"
6
7 #include <stddef.h>
8 #include <stdint.h>
9
10 #include <ostream>
11
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"
17
18 namespace base {
19
20 namespace {
21
22 template <typename Char>
IsLowerHexDigit(Char c)23 constexpr bool IsLowerHexDigit(Char c) {
24 return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f');
25 }
26
IsHyphenPosition(size_t i)27 constexpr bool IsHyphenPosition(size_t i) {
28 return i == 8 || i == 13 || i == 18 || i == 23;
29 }
30
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>
GetCanonicalUuidInternal(StringPieceType input,bool strict)35 std::string GetCanonicalUuidInternal(StringPieceType input, bool strict) {
36 using CharType = typename StringPieceType::value_type;
37
38 constexpr size_t kUuidLength = 36;
39 if (input.length() != kUuidLength) {
40 return std::string();
41 }
42
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)) {
48 if (current != '-') {
49 return std::string();
50 }
51 lowercase_[i] = '-';
52 } else {
53 if (strict ? !IsLowerHexDigit(current) : !IsHexDigit(current)) {
54 return std::string();
55 }
56 lowercase_[i] = static_cast<char>(ToLowerASCII(current));
57 }
58 }
59
60 return lowercase_;
61 }
62
63 } // namespace
64
65 // static
GenerateRandomV4()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);
72 }
73
74 // static
FormatRandomDataAsV4(base::span<const uint8_t,16> input,base::PassKey<content::FileSystemAccessManagerImpl>)75 Uuid Uuid::FormatRandomDataAsV4(
76 base::span<const uint8_t, 16> input,
77 base::PassKey<content::FileSystemAccessManagerImpl> /*pass_key*/) {
78 return FormatRandomDataAsV4Impl(input);
79 }
80
81 // static
FormatRandomDataAsV4ForTesting(base::span<const uint8_t,16> input)82 Uuid Uuid::FormatRandomDataAsV4ForTesting(base::span<const uint8_t, 16> input) {
83 return FormatRandomDataAsV4Impl(input);
84 }
85
86 // static
FormatRandomDataAsV4Impl(base::span<const uint8_t,16> input)87 Uuid Uuid::FormatRandomDataAsV4Impl(base::span<const uint8_t, 16> input) {
88 DCHECK_EQ(input.size_bytes(), kGuidV4InputLength);
89
90 uint64_t sixteen_bytes[2];
91 memcpy(&sixteen_bytes, input.data(), sizeof(sixteen_bytes));
92
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].
96
97 // Clear the version bits and set the version to 4:
98 sixteen_bytes[0] &= 0xffffffff'ffff0fffULL;
99 sixteen_bytes[0] |= 0x00000000'00004000ULL;
100
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;
105
106 Uuid uuid;
107 uuid.lowercase_ =
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);
114 return uuid;
115 }
116
117 // static
ParseCaseInsensitive(StringPiece input)118 Uuid Uuid::ParseCaseInsensitive(StringPiece input) {
119 Uuid uuid;
120 uuid.lowercase_ = GetCanonicalUuidInternal(input, /*strict=*/false);
121 return uuid;
122 }
123
124 // static
ParseCaseInsensitive(StringPiece16 input)125 Uuid Uuid::ParseCaseInsensitive(StringPiece16 input) {
126 Uuid uuid;
127 uuid.lowercase_ = GetCanonicalUuidInternal(input, /*strict=*/false);
128 return uuid;
129 }
130
131 // static
ParseLowercase(StringPiece input)132 Uuid Uuid::ParseLowercase(StringPiece input) {
133 Uuid uuid;
134 uuid.lowercase_ = GetCanonicalUuidInternal(input, /*strict=*/true);
135 return uuid;
136 }
137
138 // static
ParseLowercase(StringPiece16 input)139 Uuid Uuid::ParseLowercase(StringPiece16 input) {
140 Uuid uuid;
141 uuid.lowercase_ = GetCanonicalUuidInternal(input, /*strict=*/true);
142 return uuid;
143 }
144
145 Uuid::Uuid() = default;
146
147 Uuid::Uuid(const Uuid& other) = default;
148
149 Uuid& Uuid::operator=(const Uuid& other) = default;
150
151 Uuid::Uuid(Uuid&& other) = default;
152
153 Uuid& Uuid::operator=(Uuid&& other) = default;
154
AsLowercaseString() const155 const std::string& Uuid::AsLowercaseString() const {
156 return lowercase_;
157 }
158
operator <<(std::ostream & out,const Uuid & uuid)159 std::ostream& operator<<(std::ostream& out, const Uuid& uuid) {
160 return out << uuid.AsLowercaseString();
161 }
162
operator ()(const Uuid & uuid) const163 size_t UuidHash::operator()(const Uuid& uuid) const {
164 // TODO(crbug.com/1026195): Avoid converting to string to take the hash when
165 // the internal type is migrated to a non-string type.
166 return FastHash(uuid.AsLowercaseString());
167 }
168
169 } // namespace base
170