OLD | NEW |
1 // Copyright 2014 The Crashpad Authors. All rights reserved. | 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
(...skipping 24 matching lines...) Expand all Loading... |
35 | 35 |
36 #if CXX_LIBRARY_VERSION >= 2011 | 36 #if CXX_LIBRARY_VERSION >= 2011 |
37 #include <type_traits> | 37 #include <type_traits> |
38 #endif | 38 #endif |
39 | 39 |
40 namespace crashpad { | 40 namespace crashpad { |
41 | 41 |
42 static_assert(sizeof(UUID) == 16, "UUID must be 16 bytes"); | 42 static_assert(sizeof(UUID) == 16, "UUID must be 16 bytes"); |
43 | 43 |
44 #if CXX_LIBRARY_VERSION >= 2011 | 44 #if CXX_LIBRARY_VERSION >= 2011 |
45 static_assert(std::is_standard_layout<UUID>::value, | 45 static_assert(std::is_pod<UUID>::value, "UUID must be POD"); |
46 "UUID must be standard layout"); | |
47 #endif | 46 #endif |
48 | 47 |
49 UUID::UUID() : data_1(0), data_2(0), data_3(0), data_4(), data_5() { | 48 bool UUID::operator==(const UUID& that) const { |
| 49 return memcmp(this, &that, sizeof(*this)) == 0; |
50 } | 50 } |
51 | 51 |
52 UUID::UUID(InitializeWithNewTag) { | 52 void UUID::InitializeToZero() { |
53 CHECK(InitializeWithNew()); | 53 memset(this, 0, sizeof(*this)); |
54 } | |
55 | |
56 UUID::UUID(const uint8_t* bytes) { | |
57 InitializeFromBytes(bytes); | |
58 } | |
59 | |
60 bool UUID::operator==(const UUID& that) const { | |
61 return memcmp(this, &that, sizeof(UUID)) == 0; | |
62 } | 54 } |
63 | 55 |
64 void UUID::InitializeFromBytes(const uint8_t* bytes) { | 56 void UUID::InitializeFromBytes(const uint8_t* bytes) { |
65 memcpy(this, bytes, sizeof(*this)); | 57 memcpy(this, bytes, sizeof(*this)); |
66 data_1 = base::NetToHost32(data_1); | 58 data_1 = base::NetToHost32(data_1); |
67 data_2 = base::NetToHost16(data_2); | 59 data_2 = base::NetToHost16(data_2); |
68 data_3 = base::NetToHost16(data_3); | 60 data_3 = base::NetToHost16(data_3); |
69 } | 61 } |
70 | 62 |
71 bool UUID::InitializeFromString(const base::StringPiece& string) { | 63 bool UUID::InitializeFromString(const base::StringPiece& string) { |
(...skipping 24 matching lines...) Expand all Loading... |
96 *this = temp; | 88 *this = temp; |
97 return true; | 89 return true; |
98 } | 90 } |
99 | 91 |
100 bool UUID::InitializeWithNew() { | 92 bool UUID::InitializeWithNew() { |
101 #if defined(OS_MACOSX) | 93 #if defined(OS_MACOSX) |
102 uuid_t uuid; | 94 uuid_t uuid; |
103 uuid_generate(uuid); | 95 uuid_generate(uuid); |
104 InitializeFromBytes(uuid); | 96 InitializeFromBytes(uuid); |
105 return true; | 97 return true; |
106 #elif defined(OS_WIN) | 98 #elif defined(OS_WIN) || defined(OS_LINUX) || defined(OS_ANDROID) |
107 ::UUID system_uuid; | |
108 if (UuidCreate(&system_uuid) != RPC_S_OK) { | |
109 LOG(ERROR) << "UuidCreate"; | |
110 return false; | |
111 } | |
112 InitializeFromSystemUUID(&system_uuid); | |
113 return true; | |
114 #elif defined(OS_LINUX) || defined(OS_ANDROID) | |
115 // Linux does not provide a UUID generator in a widely-available system | 99 // Linux does not provide a UUID generator in a widely-available system |
116 // library. uuid_generate() from libuuid is not available everywhere. | 100 // library. uuid_generate() from libuuid is not available everywhere. |
| 101 // On Windows, do not use UuidCreate() to avoid a dependency on rpcrt4, so |
| 102 // that this function is usable early in DllMain(). |
117 base::RandBytes(this, sizeof(*this)); | 103 base::RandBytes(this, sizeof(*this)); |
118 | 104 |
119 // Set six bits per RFC 4122 §4.4 to identify this as a pseudo-random UUID. | 105 // Set six bits per RFC 4122 §4.4 to identify this as a pseudo-random UUID. |
120 data_3 = (4 << 12) | (data_3 & 0x0fff); // §4.1.3 | 106 data_3 = (4 << 12) | (data_3 & 0x0fff); // §4.1.3 |
121 data_4[0] = 0x80 | (data_4[0] & 0x3f); // §4.1.1 | 107 data_4[0] = 0x80 | (data_4[0] & 0x3f); // §4.1.1 |
122 | 108 |
123 return true; | 109 return true; |
124 #else | 110 #else |
125 #error Port. | 111 #error Port. |
126 #endif // OS_MACOSX | 112 #endif // OS_MACOSX |
127 } | 113 } |
128 | 114 |
129 #if defined(OS_WIN) | 115 #if defined(OS_WIN) |
130 void UUID::InitializeFromSystemUUID(const ::UUID* system_uuid) { | 116 void UUID::InitializeFromSystemUUID(const ::UUID* system_uuid) { |
131 static_assert(sizeof(::UUID) == sizeof(UUID), | 117 static_assert(sizeof(::UUID) == sizeof(UUID), |
132 "unexpected system uuid size"); | 118 "unexpected system uuid size"); |
133 static_assert(offsetof(::UUID, Data1) == offsetof(UUID, data_1), | 119 static_assert(offsetof(::UUID, Data1) == offsetof(UUID, data_1), |
134 "unexpected system uuid layout"); | 120 "unexpected system uuid layout"); |
135 memcpy(this, system_uuid, sizeof(::UUID)); | 121 memcpy(this, system_uuid, sizeof(*this)); |
136 } | 122 } |
137 #endif // OS_WIN | 123 #endif // OS_WIN |
138 | 124 |
139 std::string UUID::ToString() const { | 125 std::string UUID::ToString() const { |
140 return base::StringPrintf("%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x", | 126 return base::StringPrintf("%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x", |
141 data_1, | 127 data_1, |
142 data_2, | 128 data_2, |
143 data_3, | 129 data_3, |
144 data_4[0], | 130 data_4[0], |
145 data_4[1], | 131 data_4[1], |
146 data_5[0], | 132 data_5[0], |
147 data_5[1], | 133 data_5[1], |
148 data_5[2], | 134 data_5[2], |
149 data_5[3], | 135 data_5[3], |
150 data_5[4], | 136 data_5[4], |
151 data_5[5]); | 137 data_5[5]); |
152 } | 138 } |
153 | 139 |
154 #if defined(OS_WIN) | 140 #if defined(OS_WIN) |
155 base::string16 UUID::ToString16() const { | 141 base::string16 UUID::ToString16() const { |
156 return base::UTF8ToUTF16(ToString()); | 142 return base::UTF8ToUTF16(ToString()); |
157 } | 143 } |
158 #endif // OS_WIN | 144 #endif // OS_WIN |
159 | 145 |
160 } // namespace crashpad | 146 } // namespace crashpad |
OLD | NEW |