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.
12 #include "base/rand_util.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/stringprintf.h"
20 template <typename Char>
21 constexpr bool IsLowerHexDigit(Char c) {
22 return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f');
25 constexpr bool IsHyphenPosition(size_t i) {
26 return i == 8 || i == 13 || i == 18 || i == 23;
29 // Returns a canonical GUID string given that `input` is validly formatted
30 // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, such that x is a hexadecimal digit.
31 // If `strict`, x must be a lower-case hexadecimal digit.
32 template <typename StringPieceType>
33 std::string GetCanonicalGUIDInternal(StringPieceType input, bool strict) {
34 using CharType = typename StringPieceType::value_type;
36 constexpr size_t kGUIDLength = 36;
37 if (input.length() != kGUIDLength)
40 std::string lowercase_;
41 lowercase_.resize(kGUIDLength);
42 for (size_t i = 0; i < input.length(); ++i) {
43 CharType current = input[i];
44 if (IsHyphenPosition(i)) {
49 if (strict ? !IsLowerHexDigit(current) : !IsHexDigit(current))
51 lowercase_[i] = ToLowerASCII(current);
60 std::string GenerateGUID() {
61 GUID guid = GUID::GenerateRandomV4();
62 return guid.AsLowercaseString();
65 bool IsValidGUID(StringPiece input) {
66 return !GetCanonicalGUIDInternal(input, /*strict=*/false).empty();
69 bool IsValidGUID(StringPiece16 input) {
70 return !GetCanonicalGUIDInternal(input, /*strict=*/false).empty();
73 bool IsValidGUIDOutputString(StringPiece input) {
74 return !GetCanonicalGUIDInternal(input, /*strict=*/true).empty();
77 std::string RandomDataToGUIDString(const uint64_t bytes[2]) {
79 "%08x-%04x-%04x-%04x-%012llx", static_cast<uint32_t>(bytes[0] >> 32),
80 static_cast<uint32_t>((bytes[0] >> 16) & 0x0000ffff),
81 static_cast<uint32_t>(bytes[0] & 0x0000ffff),
82 static_cast<uint32_t>(bytes[1] >> 48), bytes[1] & 0x0000ffff'ffffffffULL);
86 GUID GUID::GenerateRandomV4() {
87 uint64_t sixteen_bytes[2];
88 // Use base::RandBytes instead of crypto::RandBytes, because crypto calls the
89 // base version directly, and to prevent the dependency from base/ to crypto/.
90 RandBytes(&sixteen_bytes, sizeof(sixteen_bytes));
92 // Set the GUID to version 4 as described in RFC 4122, section 4.4.
93 // The format of GUID version 4 must be xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx,
94 // where y is one of [8, 9, a, b].
96 // Clear the version bits and set the version to 4:
97 sixteen_bytes[0] &= 0xffffffff'ffff0fffULL;
98 sixteen_bytes[0] |= 0x00000000'00004000ULL;
100 // Set the two most significant bits (bits 6 and 7) of the
101 // clock_seq_hi_and_reserved to zero and one, respectively:
102 sixteen_bytes[1] &= 0x3fffffff'ffffffffULL;
103 sixteen_bytes[1] |= 0x80000000'00000000ULL;
106 guid.lowercase_ = RandomDataToGUIDString(sixteen_bytes);
111 GUID GUID::ParseCaseInsensitive(StringPiece input) {
113 guid.lowercase_ = GetCanonicalGUIDInternal(input, /*strict=*/false);
118 GUID GUID::ParseCaseInsensitive(StringPiece16 input) {
120 guid.lowercase_ = GetCanonicalGUIDInternal(input, /*strict=*/false);
125 GUID GUID::ParseLowercase(StringPiece input) {
127 guid.lowercase_ = GetCanonicalGUIDInternal(input, /*strict=*/true);
132 GUID GUID::ParseLowercase(StringPiece16 input) {
134 guid.lowercase_ = GetCanonicalGUIDInternal(input, /*strict=*/true);
138 GUID::GUID() = default;
140 GUID::GUID(const GUID& other) = default;
142 GUID& GUID::operator=(const GUID& other) = default;
144 const std::string& GUID::AsLowercaseString() const {
148 bool GUID::operator==(const GUID& other) const {
149 return AsLowercaseString() == other.AsLowercaseString();
152 bool GUID::operator!=(const GUID& other) const {
153 return !(*this == other);
156 bool GUID::operator<(const GUID& other) const {
157 return AsLowercaseString() < other.AsLowercaseString();
160 bool GUID::operator<=(const GUID& other) const {
161 return *this < other || *this == other;
164 bool GUID::operator>(const GUID& other) const {
165 return !(*this <= other);
168 bool GUID::operator>=(const GUID& other) const {
169 return !(*this < other);
172 std::ostream& operator<<(std::ostream& out, const GUID& guid) {
173 return out << guid.AsLowercaseString();