• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/rand_util.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/stringprintf.h"
15 #include "base/types/pass_key.h"
16 
17 namespace base {
18 
19 namespace {
20 
21 template <typename Char>
IsLowerHexDigit(Char c)22 constexpr bool IsLowerHexDigit(Char c) {
23   return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f');
24 }
25 
IsHyphenPosition(size_t i)26 constexpr bool IsHyphenPosition(size_t i) {
27   return i == 8 || i == 13 || i == 18 || i == 23;
28 }
29 
30 // Returns a canonical Uuid string given that `input` is validly formatted
31 // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, such that x is a hexadecimal digit.
32 // If `strict`, x must be a lower-case hexadecimal digit.
33 template <typename StringPieceType>
GetCanonicalUuidInternal(StringPieceType input,bool strict)34 std::string GetCanonicalUuidInternal(StringPieceType input, bool strict) {
35   using CharType = typename StringPieceType::value_type;
36 
37   constexpr size_t kUuidLength = 36;
38   if (input.length() != kUuidLength) {
39     return std::string();
40   }
41 
42   std::string lowercase_;
43   lowercase_.resize(kUuidLength);
44   for (size_t i = 0; i < input.length(); ++i) {
45     CharType current = input[i];
46     if (IsHyphenPosition(i)) {
47       if (current != '-') {
48         return std::string();
49       }
50       lowercase_[i] = '-';
51     } else {
52       if (strict ? !IsLowerHexDigit(current) : !IsHexDigit(current)) {
53         return std::string();
54       }
55       lowercase_[i] = static_cast<char>(ToLowerASCII(current));
56     }
57   }
58 
59   return lowercase_;
60 }
61 
62 }  // namespace
63 
GenerateUuid()64 std::string GenerateUuid() {
65   Uuid uuid = Uuid::GenerateRandomV4();
66   return uuid.AsLowercaseString();
67 }
68 
IsValidUuid(StringPiece input)69 bool IsValidUuid(StringPiece input) {
70   return !GetCanonicalUuidInternal(input, /*strict=*/false).empty();
71 }
72 
IsValidUuidOutputString(StringPiece input)73 bool IsValidUuidOutputString(StringPiece input) {
74   return !GetCanonicalUuidInternal(input, /*strict=*/true).empty();
75 }
76 
77 // static
GenerateRandomV4()78 Uuid Uuid::GenerateRandomV4() {
79   uint8_t sixteen_bytes[kGuidV4InputLength];
80   // Use base::RandBytes instead of crypto::RandBytes, because crypto calls the
81   // base version directly, and to prevent the dependency from base/ to crypto/.
82   RandBytes(&sixteen_bytes, sizeof(sixteen_bytes));
83   return FormatRandomDataAsV4Impl(sixteen_bytes);
84 }
85 
86 // static
FormatRandomDataAsV4(base::span<const uint8_t,16> input,base::PassKey<content::FileSystemAccessManagerImpl>)87 Uuid Uuid::FormatRandomDataAsV4(
88     base::span<const uint8_t, 16> input,
89     base::PassKey<content::FileSystemAccessManagerImpl> /*pass_key*/) {
90   return FormatRandomDataAsV4Impl(input);
91 }
92 
93 // static
FormatRandomDataAsV4ForTesting(base::span<const uint8_t,16> input)94 Uuid Uuid::FormatRandomDataAsV4ForTesting(base::span<const uint8_t, 16> input) {
95   return FormatRandomDataAsV4Impl(input);
96 }
97 
98 // static
FormatRandomDataAsV4Impl(base::span<const uint8_t,16> input)99 Uuid Uuid::FormatRandomDataAsV4Impl(base::span<const uint8_t, 16> input) {
100   DCHECK_EQ(input.size_bytes(), kGuidV4InputLength);
101 
102   uint64_t sixteen_bytes[2];
103   memcpy(&sixteen_bytes, input.data(), sizeof(sixteen_bytes));
104 
105   // Set the Uuid to version 4 as described in RFC 4122, section 4.4.
106   // The format of Uuid version 4 must be xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx,
107   // where y is one of [8, 9, a, b].
108 
109   // Clear the version bits and set the version to 4:
110   sixteen_bytes[0] &= 0xffffffff'ffff0fffULL;
111   sixteen_bytes[0] |= 0x00000000'00004000ULL;
112 
113   // Set the two most significant bits (bits 6 and 7) of the
114   // clock_seq_hi_and_reserved to zero and one, respectively:
115   sixteen_bytes[1] &= 0x3fffffff'ffffffffULL;
116   sixteen_bytes[1] |= 0x80000000'00000000ULL;
117 
118   Uuid uuid;
119   uuid.lowercase_ =
120       StringPrintf("%08x-%04x-%04x-%04x-%012llx",
121                    static_cast<uint32_t>(sixteen_bytes[0] >> 32),
122                    static_cast<uint32_t>((sixteen_bytes[0] >> 16) & 0x0000ffff),
123                    static_cast<uint32_t>(sixteen_bytes[0] & 0x0000ffff),
124                    static_cast<uint32_t>(sixteen_bytes[1] >> 48),
125                    sixteen_bytes[1] & 0x0000ffff'ffffffffULL);
126   return uuid;
127 }
128 
129 // static
ParseCaseInsensitive(StringPiece input)130 Uuid Uuid::ParseCaseInsensitive(StringPiece input) {
131   Uuid uuid;
132   uuid.lowercase_ = GetCanonicalUuidInternal(input, /*strict=*/false);
133   return uuid;
134 }
135 
136 // static
ParseCaseInsensitive(StringPiece16 input)137 Uuid Uuid::ParseCaseInsensitive(StringPiece16 input) {
138   Uuid uuid;
139   uuid.lowercase_ = GetCanonicalUuidInternal(input, /*strict=*/false);
140   return uuid;
141 }
142 
143 // static
ParseLowercase(StringPiece input)144 Uuid Uuid::ParseLowercase(StringPiece input) {
145   Uuid uuid;
146   uuid.lowercase_ = GetCanonicalUuidInternal(input, /*strict=*/true);
147   return uuid;
148 }
149 
150 // static
ParseLowercase(StringPiece16 input)151 Uuid Uuid::ParseLowercase(StringPiece16 input) {
152   Uuid uuid;
153   uuid.lowercase_ = GetCanonicalUuidInternal(input, /*strict=*/true);
154   return uuid;
155 }
156 
157 Uuid::Uuid() = default;
158 
159 Uuid::Uuid(const Uuid& other) = default;
160 
161 Uuid& Uuid::operator=(const Uuid& other) = default;
162 
163 Uuid::Uuid(Uuid&& other) = default;
164 
165 Uuid& Uuid::operator=(Uuid&& other) = default;
166 
AsLowercaseString() const167 const std::string& Uuid::AsLowercaseString() const {
168   return lowercase_;
169 }
170 
operator ==(const Uuid & other) const171 bool Uuid::operator==(const Uuid& other) const {
172   return AsLowercaseString() == other.AsLowercaseString();
173 }
174 
operator !=(const Uuid & other) const175 bool Uuid::operator!=(const Uuid& other) const {
176   return !(*this == other);
177 }
178 
operator <(const Uuid & other) const179 bool Uuid::operator<(const Uuid& other) const {
180   return AsLowercaseString() < other.AsLowercaseString();
181 }
182 
operator <=(const Uuid & other) const183 bool Uuid::operator<=(const Uuid& other) const {
184   return *this < other || *this == other;
185 }
186 
operator >(const Uuid & other) const187 bool Uuid::operator>(const Uuid& other) const {
188   return !(*this <= other);
189 }
190 
operator >=(const Uuid & other) const191 bool Uuid::operator>=(const Uuid& other) const {
192   return !(*this < other);
193 }
194 
operator <<(std::ostream & out,const Uuid & uuid)195 std::ostream& operator<<(std::ostream& out, const Uuid& uuid) {
196   return out << uuid.AsLowercaseString();
197 }
198 
GenerateGUID()199 std::string GenerateGUID() {
200   return GenerateUuid();
201 }
202 
IsValidGUID(StringPiece input)203 bool IsValidGUID(StringPiece input) {
204   return IsValidUuid(input);
205 }
206 
IsValidGUIDOutputString(StringPiece input)207 bool IsValidGUIDOutputString(StringPiece input) {
208   return IsValidUuidOutputString(input);
209 }
210 
211 }  // namespace base
212