Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(313)

Side by Side Diff: base/guid.cc

Issue 1906723003: Unify UUID/GUID generation code across base and blink (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Use uppercase GUID for push messaging for crbug.com/461867 Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/guid.h" 5 #include "base/guid.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h>
8 9
10 #include "base/rand_util.h"
9 #include "base/strings/string_util.h" 11 #include "base/strings/string_util.h"
12 #include "base/strings/stringprintf.h"
10 13
11 namespace base { 14 namespace base {
12 15
13 bool IsValidGUID(const std::string& guid) { 16 namespace {
17
18 bool IsLowerHexDigit(char c) {
19 return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f');
20 }
21
22 bool IsValidGUIDInternal(const std::string& guid, bool strict) {
14 const size_t kGUIDLength = 36U; 23 const size_t kGUIDLength = 36U;
15 if (guid.length() != kGUIDLength) 24 if (guid.length() != kGUIDLength)
16 return false; 25 return false;
17 26
18 for (size_t i = 0; i < guid.length(); ++i) { 27 for (size_t i = 0; i < guid.length(); ++i) {
19 char current = guid[i]; 28 char current = guid[i];
20 if (i == 8 || i == 13 || i == 18 || i == 23) { 29 if (i == 8 || i == 13 || i == 18 || i == 23) {
21 if (current != '-') 30 if (current != '-')
22 return false; 31 return false;
23 } else { 32 } else {
24 if (!IsHexDigit(current)) 33 if ((strict && !IsLowerHexDigit(current)) || !IsHexDigit(current))
25 return false; 34 return false;
26 } 35 }
27 } 36 }
28 37
29 return true; 38 return true;
30 } 39 }
31 40
41 } // namespace
42
43 std::string GenerateGUID() {
44 uint64_t sixteen_bytes[2] = {base::RandUint64(), base::RandUint64()};
45
46 // Set the GUID to version 4 as described in RFC 4122, section 4.4.
47 // The format of GUID version 4 must be xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx,
48 // where y is one of [8, 9, A, B].
49
50 // Clear the version bits and set the version to 4:
51 sixteen_bytes[0] &= 0xffffffffffff0fffULL;
52 sixteen_bytes[0] |= 0x0000000000004000ULL;
53
54 // Set the two most significant bits (bits 6 and 7) of the
55 // clock_seq_hi_and_reserved to zero and one, respectively:
56 sixteen_bytes[1] &= 0x3fffffffffffffffULL;
57 sixteen_bytes[1] |= 0x8000000000000000ULL;
58
59 return RandomDataToGUIDString(sixteen_bytes);
60 }
61
62 bool IsValidGUID(const std::string& guid) {
63 return IsValidGUIDInternal(guid, false /* strict */);
64 }
65
66 bool IsValidVersion4GUID(const std::string& guid) {
67 return IsValidGUIDInternal(guid, true /* strict */);
68 }
69
70 std::string RandomDataToGUIDString(const uint64_t bytes[2]) {
71 return StringPrintf("%08x-%04x-%04x-%01X%03x-%012llx",
Mark Mentovai 2016/04/22 13:01:46 There’s a capital X in here that should be lowerca
kinuko 2016/04/25 14:24:37 Done.
72 static_cast<unsigned int>(bytes[0] >> 32),
73 static_cast<unsigned int>((bytes[0] >> 16) & 0x0000ffff),
74 static_cast<unsigned int>(bytes[0] & 0x0000ffff),
75 static_cast<unsigned int>(bytes[1] >> 60),
Mark Mentovai 2016/04/22 13:01:46 I don’t see the reason to have bytes[1] >> 60 and
kinuko 2016/04/25 14:24:37 Oops, you're right, done.
76 static_cast<unsigned int>((bytes[1] >> 48) & 0x0fff),
77 bytes[1] & 0x0000ffffffffffffULL);
78 }
79
32 } // namespace base 80 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698