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, |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
13 // limitations under the License. | 13 // limitations under the License. |
14 | 14 |
15 #if !defined(__STDC_FORMAT_MACROS) | 15 #if !defined(__STDC_FORMAT_MACROS) |
16 #define __STDC_FORMAT_MACROS | 16 #define __STDC_FORMAT_MACROS |
17 #endif | 17 #endif |
18 | 18 |
19 #include "util/misc/uuid.h" | 19 #include "util/misc/uuid.h" |
20 | 20 |
21 #include <inttypes.h> | 21 #include <inttypes.h> |
22 #include <stdio.h> | 22 #include <stdio.h> |
23 #include <string.h> | 23 #include <string.h> |
24 | 24 |
25 #include "base/basictypes.h" | 25 #include "base/basictypes.h" |
| 26 #include "base/logging.h" |
26 #include "base/strings/stringprintf.h" | 27 #include "base/strings/stringprintf.h" |
27 #include "base/strings/utf_string_conversions.h" | 28 #include "base/strings/utf_string_conversions.h" |
28 #include "base/sys_byteorder.h" | 29 #include "base/sys_byteorder.h" |
29 #include "util/stdlib/cxx.h" | 30 #include "util/stdlib/cxx.h" |
30 | 31 |
| 32 #if defined(OS_MACOSX) |
| 33 #include <uuid/uuid.h> |
| 34 #endif // OS_MACOSX |
| 35 |
31 #if CXX_LIBRARY_VERSION >= 2011 | 36 #if CXX_LIBRARY_VERSION >= 2011 |
32 #include <type_traits> | 37 #include <type_traits> |
33 #endif | 38 #endif |
34 | 39 |
35 namespace crashpad { | 40 namespace crashpad { |
36 | 41 |
37 static_assert(sizeof(UUID) == 16, "UUID must be 16 bytes"); | 42 static_assert(sizeof(UUID) == 16, "UUID must be 16 bytes"); |
38 | 43 |
39 #if CXX_LIBRARY_VERSION >= 2011 | 44 #if CXX_LIBRARY_VERSION >= 2011 |
40 static_assert(std::is_standard_layout<UUID>::value, | 45 static_assert(std::is_standard_layout<UUID>::value, |
41 "UUID must be standard layout"); | 46 "UUID must be standard layout"); |
42 #endif | 47 #endif |
43 | 48 |
44 UUID::UUID() : data_1(0), data_2(0), data_3(0), data_4(), data_5() { | 49 UUID::UUID() : data_1(0), data_2(0), data_3(0), data_4(), data_5() { |
45 } | 50 } |
46 | 51 |
| 52 UUID::UUID(InitializeWithNewTag) { |
| 53 CHECK(InitializeWithNew()); |
| 54 } |
| 55 |
47 UUID::UUID(const uint8_t* bytes) { | 56 UUID::UUID(const uint8_t* bytes) { |
48 InitializeFromBytes(bytes); | 57 InitializeFromBytes(bytes); |
49 } | 58 } |
50 | 59 |
51 bool UUID::operator==(const UUID& that) const { | 60 bool UUID::operator==(const UUID& that) const { |
52 return memcmp(this, &that, sizeof(UUID)) == 0; | 61 return memcmp(this, &that, sizeof(UUID)) == 0; |
53 } | 62 } |
54 | 63 |
55 void UUID::InitializeFromBytes(const uint8_t* bytes) { | 64 void UUID::InitializeFromBytes(const uint8_t* bytes) { |
56 memcpy(this, bytes, sizeof(*this)); | 65 memcpy(this, bytes, sizeof(*this)); |
(...skipping 24 matching lines...) Expand all Loading... |
81 &temp.data_5[3], | 90 &temp.data_5[3], |
82 &temp.data_5[4], | 91 &temp.data_5[4], |
83 &temp.data_5[5]); | 92 &temp.data_5[5]); |
84 if (rv != 11) | 93 if (rv != 11) |
85 return false; | 94 return false; |
86 | 95 |
87 *this = temp; | 96 *this = temp; |
88 return true; | 97 return true; |
89 } | 98 } |
90 | 99 |
| 100 bool UUID::InitializeWithNew() { |
| 101 #if defined(OS_MACOSX) |
| 102 uuid_t uuid; |
| 103 uuid_generate(uuid); |
| 104 InitializeFromBytes(uuid); |
| 105 return true; |
| 106 #elif defined(OS_WIN) |
| 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 #else |
| 115 #error Port. |
| 116 #endif // OS_MACOSX |
| 117 } |
| 118 |
91 #if defined(OS_WIN) | 119 #if defined(OS_WIN) |
92 void UUID::InitializeFromSystemUUID(const ::UUID* system_uuid) { | 120 void UUID::InitializeFromSystemUUID(const ::UUID* system_uuid) { |
93 static_assert(sizeof(::UUID) == sizeof(UUID), | 121 static_assert(sizeof(::UUID) == sizeof(UUID), |
94 "unexpected system uuid size"); | 122 "unexpected system uuid size"); |
95 static_assert(offsetof(::UUID, Data1) == offsetof(UUID, data_1), | 123 static_assert(offsetof(::UUID, Data1) == offsetof(UUID, data_1), |
96 "unexpected system uuid layout"); | 124 "unexpected system uuid layout"); |
97 memcpy(this, system_uuid, sizeof(::UUID)); | 125 memcpy(this, system_uuid, sizeof(::UUID)); |
98 } | 126 } |
99 #endif // OS_WIN | 127 #endif // OS_WIN |
100 | 128 |
(...skipping 12 matching lines...) Expand all Loading... |
113 data_5[5]); | 141 data_5[5]); |
114 } | 142 } |
115 | 143 |
116 #if defined(OS_WIN) | 144 #if defined(OS_WIN) |
117 base::string16 UUID::ToString16() const { | 145 base::string16 UUID::ToString16() const { |
118 return base::UTF8ToUTF16(ToString()); | 146 return base::UTF8ToUTF16(ToString()); |
119 } | 147 } |
120 #endif // OS_WIN | 148 #endif // OS_WIN |
121 | 149 |
122 } // namespace crashpad | 150 } // namespace crashpad |
OLD | NEW |