[M73 Dev][Tizen] Fix compilation errors for TV profile
[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 "base/rand_util.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/stringprintf.h"
13
14 namespace base {
15
16 namespace {
17
18 template <typename Char>
19 bool IsLowerHexDigit(Char c) {
20   return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f');
21 }
22
23 template <typename StringPieceType>
24 bool IsValidGUIDInternal(StringPieceType guid, bool strict) {
25   using CharType = typename StringPieceType::value_type;
26
27   const size_t kGUIDLength = 36U;
28   if (guid.length() != kGUIDLength)
29     return false;
30
31   for (size_t i = 0; i < guid.length(); ++i) {
32     CharType current = guid[i];
33     if (i == 8 || i == 13 || i == 18 || i == 23) {
34       if (current != '-')
35         return false;
36     } else {
37       if (strict ? !IsLowerHexDigit(current) : !IsHexDigit(current))
38         return false;
39     }
40   }
41
42   return true;
43 }
44
45 }  // namespace
46
47 std::string GenerateGUID() {
48   uint64_t sixteen_bytes[2];
49   // Use base::RandBytes instead of crypto::RandBytes, because crypto calls the
50   // base version directly, and to prevent the dependency from base/ to crypto/.
51   base::RandBytes(&sixteen_bytes, sizeof(sixteen_bytes));
52
53   // Set the GUID to version 4 as described in RFC 4122, section 4.4.
54   // The format of GUID version 4 must be xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx,
55   // where y is one of [8, 9, A, B].
56
57   // Clear the version bits and set the version to 4:
58   sixteen_bytes[0] &= 0xffffffff'ffff0fffULL;
59   sixteen_bytes[0] |= 0x00000000'00004000ULL;
60
61   // Set the two most significant bits (bits 6 and 7) of the
62   // clock_seq_hi_and_reserved to zero and one, respectively:
63   sixteen_bytes[1] &= 0x3fffffff'ffffffffULL;
64   sixteen_bytes[1] |= 0x80000000'00000000ULL;
65
66   return RandomDataToGUIDString(sixteen_bytes);
67 }
68
69 bool IsValidGUID(base::StringPiece guid) {
70   return IsValidGUIDInternal(guid, false /* strict */);
71 }
72
73 bool IsValidGUID(base::StringPiece16 guid) {
74   return IsValidGUIDInternal(guid, false /* strict */);
75 }
76
77 bool IsValidGUIDOutputString(base::StringPiece guid) {
78   return IsValidGUIDInternal(guid, true /* strict */);
79 }
80
81 std::string RandomDataToGUIDString(const uint64_t bytes[2]) {
82   return StringPrintf("%08x-%04x-%04x-%04x-%012llx",
83                       static_cast<unsigned int>(bytes[0] >> 32),
84                       static_cast<unsigned int>((bytes[0] >> 16) & 0x0000ffff),
85                       static_cast<unsigned int>(bytes[0] & 0x0000ffff),
86                       static_cast<unsigned int>(bytes[1] >> 48),
87                       bytes[1] & 0x0000ffff'ffffffffULL);
88 }
89
90 }  // namespace base