Unify UUID/GUID generation code across base and blink
We have at least three different UUID/GUID implementations in Chromium
and they all use slightly different UUID/GUID format criteria.
From code health (and consistent behavior) pov it'd be nice
to unify them.
- base/guid_posix.cc - returns mostly-correct Ver4 GUID string but in uppercase
- base/guid_win.cc - returns mostly-correct Ver4 GUID string but in uppercase
- third_party/WebKit/Source/platform/UUID.cpp - returns correct Ver4 GUID
This patch tries to unify them into one location in base/guid.cc.
BUG=561879
TEST=base_unittests:GUID*
TEST=blink_platform_unittests:UUID*
Review URL: https://codereview.chromium.org/1906723003
Cr-Commit-Position: refs/heads/master@{#390042}
diff --git a/base/guid.cc b/base/guid.cc
index 99b037b..57140733 100644
--- a/base/guid.cc
+++ b/base/guid.cc
@@ -5,12 +5,21 @@
#include "base/guid.h"
#include <stddef.h>
+#include <stdint.h>
+#include "base/rand_util.h"
#include "base/strings/string_util.h"
+#include "base/strings/stringprintf.h"
namespace base {
-bool IsValidGUID(const std::string& guid) {
+namespace {
+
+bool IsLowerHexDigit(char c) {
+ return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f');
+}
+
+bool IsValidGUIDInternal(const base::StringPiece& guid, bool strict) {
const size_t kGUIDLength = 36U;
if (guid.length() != kGUIDLength)
return false;
@@ -21,7 +30,7 @@
if (current != '-')
return false;
} else {
- if (!IsHexDigit(current))
+ if ((strict && !IsLowerHexDigit(current)) || !IsHexDigit(current))
return false;
}
}
@@ -29,4 +38,42 @@
return true;
}
+} // namespace
+
+std::string GenerateGUID() {
+ uint64_t sixteen_bytes[2] = {base::RandUint64(), base::RandUint64()};
+
+ // 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] &= 0xffffffffffff0fffULL;
+ sixteen_bytes[0] |= 0x0000000000004000ULL;
+
+ // 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] &= 0x3fffffffffffffffULL;
+ sixteen_bytes[1] |= 0x8000000000000000ULL;
+
+ return RandomDataToGUIDString(sixteen_bytes);
+}
+
+bool IsValidGUID(const base::StringPiece& guid) {
+ return IsValidGUIDInternal(guid, false /* strict */);
+}
+
+bool IsValidGUIDOutputString(const base::StringPiece& guid) {
+ return IsValidGUIDInternal(guid, true /* strict */);
+}
+
+std::string RandomDataToGUIDString(const uint64_t bytes[2]) {
+ return StringPrintf("%08x-%04x-%04x-%04x-%012llx",
+ static_cast<unsigned int>(bytes[0] >> 32),
+ static_cast<unsigned int>((bytes[0] >> 16) & 0x0000ffff),
+ static_cast<unsigned int>(bytes[0] & 0x0000ffff),
+ static_cast<unsigned int>(bytes[1] >> 48),
+ bytes[1] & 0x0000ffffffffffffULL);
+}
+
} // namespace base