[M94 Dev][Tizen] Fix for errors for generating ninja files
[platform/framework/web/chromium-efl.git] / base / guid.cc
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.
4
5 #include "base/guid.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
16 namespace base {
17
18 namespace {
19
20 template <typename Char>
21 constexpr bool IsLowerHexDigit(Char c) {
22   return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f');
23 }
24
25 constexpr bool IsHyphenPosition(size_t i) {
26   return i == 8 || i == 13 || i == 18 || i == 23;
27 }
28
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;
35
36   constexpr size_t kGUIDLength = 36;
37   if (input.length() != kGUIDLength)
38     return std::string();
39
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)) {
45       if (current != '-')
46         return std::string();
47       lowercase_[i] = '-';
48     } else {
49       if (strict ? !IsLowerHexDigit(current) : !IsHexDigit(current))
50         return std::string();
51       lowercase_[i] = ToLowerASCII(current);
52     }
53   }
54
55   return lowercase_;
56 }
57
58 }  // namespace
59
60 std::string GenerateGUID() {
61   GUID guid = GUID::GenerateRandomV4();
62   return guid.AsLowercaseString();
63 }
64
65 bool IsValidGUID(StringPiece input) {
66   return !GetCanonicalGUIDInternal(input, /*strict=*/false).empty();
67 }
68
69 bool IsValidGUID(StringPiece16 input) {
70   return !GetCanonicalGUIDInternal(input, /*strict=*/false).empty();
71 }
72
73 bool IsValidGUIDOutputString(StringPiece input) {
74   return !GetCanonicalGUIDInternal(input, /*strict=*/true).empty();
75 }
76
77 std::string RandomDataToGUIDString(const uint64_t bytes[2]) {
78   return StringPrintf(
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);
83 }
84
85 // static
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));
91
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].
95
96   // Clear the version bits and set the version to 4:
97   sixteen_bytes[0] &= 0xffffffff'ffff0fffULL;
98   sixteen_bytes[0] |= 0x00000000'00004000ULL;
99
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;
104
105   GUID guid;
106   guid.lowercase_ = RandomDataToGUIDString(sixteen_bytes);
107   return guid;
108 }
109
110 // static
111 GUID GUID::ParseCaseInsensitive(StringPiece input) {
112   GUID guid;
113   guid.lowercase_ = GetCanonicalGUIDInternal(input, /*strict=*/false);
114   return guid;
115 }
116
117 // static
118 GUID GUID::ParseCaseInsensitive(StringPiece16 input) {
119   GUID guid;
120   guid.lowercase_ = GetCanonicalGUIDInternal(input, /*strict=*/false);
121   return guid;
122 }
123
124 // static
125 GUID GUID::ParseLowercase(StringPiece input) {
126   GUID guid;
127   guid.lowercase_ = GetCanonicalGUIDInternal(input, /*strict=*/true);
128   return guid;
129 }
130
131 // static
132 GUID GUID::ParseLowercase(StringPiece16 input) {
133   GUID guid;
134   guid.lowercase_ = GetCanonicalGUIDInternal(input, /*strict=*/true);
135   return guid;
136 }
137
138 GUID::GUID() = default;
139
140 GUID::GUID(const GUID& other) = default;
141
142 GUID& GUID::operator=(const GUID& other) = default;
143
144 const std::string& GUID::AsLowercaseString() const {
145   return lowercase_;
146 }
147
148 bool GUID::operator==(const GUID& other) const {
149   return AsLowercaseString() == other.AsLowercaseString();
150 }
151
152 bool GUID::operator!=(const GUID& other) const {
153   return !(*this == other);
154 }
155
156 bool GUID::operator<(const GUID& other) const {
157   return AsLowercaseString() < other.AsLowercaseString();
158 }
159
160 bool GUID::operator<=(const GUID& other) const {
161   return *this < other || *this == other;
162 }
163
164 bool GUID::operator>(const GUID& other) const {
165   return !(*this <= other);
166 }
167
168 bool GUID::operator>=(const GUID& other) const {
169   return !(*this < other);
170 }
171
172 std::ostream& operator<<(std::ostream& out, const GUID& guid) {
173   return out << guid.AsLowercaseString();
174 }
175
176 }  // namespace base