blob: 571407330c2bdf5459f9a6fc1e4b42bcb046557f [file] [log] [blame]
[email protected]7e49ad32012-06-14 14:22:071// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]d3d728e92010-10-20 03:24:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]7e49ad32012-06-14 14:22:075#include "base/guid.h"
[email protected]d3d728e92010-10-20 03:24:556
avi9b6f42932015-12-26 22:15:147#include <stddef.h>
kinuko4ad1f552016-04-27 11:00:208#include <stdint.h>
avi9b6f42932015-12-26 22:15:149
kinuko4ad1f552016-04-27 11:00:2010#include "base/rand_util.h"
benchan57d121ac2014-09-05 05:08:3211#include "base/strings/string_util.h"
kinuko4ad1f552016-04-27 11:00:2012#include "base/strings/stringprintf.h"
benchan57d121ac2014-09-05 05:08:3213
[email protected]7e49ad32012-06-14 14:22:0714namespace base {
[email protected]d3d728e92010-10-20 03:24:5515
kinuko4ad1f552016-04-27 11:00:2016namespace {
17
18bool IsLowerHexDigit(char c) {
19 return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f');
20}
21
22bool IsValidGUIDInternal(const base::StringPiece& guid, bool strict) {
[email protected]d3d728e92010-10-20 03:24:5523 const size_t kGUIDLength = 36U;
24 if (guid.length() != kGUIDLength)
25 return false;
26
benchan57d121ac2014-09-05 05:08:3227 for (size_t i = 0; i < guid.length(); ++i) {
[email protected]d3d728e92010-10-20 03:24:5528 char current = guid[i];
29 if (i == 8 || i == 13 || i == 18 || i == 23) {
30 if (current != '-')
31 return false;
32 } else {
kinuko4ad1f552016-04-27 11:00:2033 if ((strict && !IsLowerHexDigit(current)) || !IsHexDigit(current))
[email protected]d3d728e92010-10-20 03:24:5534 return false;
35 }
36 }
37
38 return true;
39}
40
kinuko4ad1f552016-04-27 11:00:2041} // namespace
42
43std::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
62bool IsValidGUID(const base::StringPiece& guid) {
63 return IsValidGUIDInternal(guid, false /* strict */);
64}
65
66bool IsValidGUIDOutputString(const base::StringPiece& guid) {
67 return IsValidGUIDInternal(guid, true /* strict */);
68}
69
70std::string RandomDataToGUIDString(const uint64_t bytes[2]) {
71 return StringPrintf("%08x-%04x-%04x-%04x-%012llx",
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] >> 48),
76 bytes[1] & 0x0000ffffffffffffULL);
77}
78
[email protected]4461e9ad2013-09-27 08:52:2979} // namespace base