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