Debian Codegraph @

src/mpt/uuid/uuid.hpp

raw
1 /* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
2
3 #ifndef MPT_UUID_UUID_HPP
4 #define MPT_UUID_UUID_HPP
5
6
7
8 #include "mpt/base/constexpr_throw.hpp"
9 #include "mpt/base/macros.hpp"
10 #include "mpt/base/integer.hpp"
11 #include "mpt/base/memory.hpp"
12 #include "mpt/base/namespace.hpp"
13 #include "mpt/endian/integer.hpp"
14 #include "mpt/format/default_formatter.hpp"
15 #include "mpt/format/simple.hpp"
16 #include "mpt/parse/parse.hpp"
17 #include "mpt/random/random.hpp"
18 #include "mpt/string/types.hpp"
19 #include "mpt/string/utility.hpp"
20
21 #if MPT_OS_WINDOWS
22 #if MPT_WINNT_AT_LEAST(MPT_WIN_VISTA)
23 #include <guiddef.h>
24 #endif // MPT_WIN_VISTA
25 #include <objbase.h>
26 #include <rpc.h>
27 #endif // MPT_OS_WINDOWS
28
29
30
31 namespace mpt {
32 inline namespace MPT_INLINE_NS {
33
34
35 // Microsoft on-disk layout
36 struct GUIDms {
37 uint32le Data1;
38 uint16le Data2;
39 uint16le Data3;
40 uint64be Data4; // yes, big endian here
41 };
42 constexpr bool declare_binary_safe(const GUIDms &) {
43 return true;
44 }
45 static_assert(mpt::check_binary_size<GUIDms>(16));
46
47 // RFC binary format
48 struct UUIDbin {
49 uint32be Data1;
50 uint16be Data2;
51 uint16be Data3;
52 uint64be Data4;
53 };
54 constexpr bool declare_binary_safe(const UUIDbin &) {
55 return true;
56 }
57 static_assert(mpt::check_binary_size<UUIDbin>(16));
58
59
60
61 struct UUID {
62 private:
63 uint32 Data1;
64 uint16 Data2;
65 uint16 Data3;
66 uint64 Data4;
67
68 public:
69 MPT_CONSTEXPRINLINE uint32 GetData1() const noexcept {
70 return Data1;
71 }
72 MPT_CONSTEXPRINLINE uint16 GetData2() const noexcept {
73 return Data2;
74 }
75 MPT_CONSTEXPRINLINE uint16 GetData3() const noexcept {
76 return Data3;
77 }
78 MPT_CONSTEXPRINLINE uint64 GetData4() const noexcept {
79 return Data4;
80 }
81
82 public:
83 MPT_CONSTEXPRINLINE uint64 GetData64_1() const noexcept {
84 return (static_cast<uint64>(Data1) << 32) | (static_cast<uint64>(Data2) << 16) | (static_cast<uint64>(Data3) << 0);
85 }
86 MPT_CONSTEXPRINLINE uint64 GetData64_2() const noexcept {
87 return Data4;
88 }
89
90 public:
91 // xxxxxxxx-xxxx-Mmxx-Nnxx-xxxxxxxxxxxx
92 // <--32-->-<16>-<16>-<-------64------>
93 MPT_CONSTEXPRINLINE bool IsNil() const noexcept {
94 return (Data1 == 0) && (Data2 == 0) && (Data3 == 0) && (Data4 == 0);
95 }
96 MPT_CONSTEXPRINLINE bool IsValid() const noexcept {
97 return (Data1 != 0) || (Data2 != 0) || (Data3 != 0) || (Data4 != 0);
98 }
99 MPT_CONSTEXPRINLINE uint8 Variant() const noexcept {
100 return Nn() >> 4u;
101 }
102 MPT_CONSTEXPRINLINE uint8 Version() const noexcept {
103 return Mm() >> 4u;
104 }
105 MPT_CONSTEXPRINLINE bool IsRFC4122() const noexcept {
106 return (Variant() & 0xcu) == 0x8u;
107 }
108
109 private:
110 MPT_CONSTEXPRINLINE uint8 Mm() const noexcept {
111 return static_cast<uint8>((Data3 >> 8) & 0xffu);
112 }
113 MPT_CONSTEXPRINLINE uint8 Nn() const noexcept {
114 return static_cast<uint8>((Data4 >> 56) & 0xffu);
115 }
116 #if MPT_COMPILER_GCC
117 #pragma GCC diagnostic push
118 #pragma GCC diagnostic ignored "-Wconversion"
119 #endif // MPT_COMPILER_GCC
120 void MakeRFC4122(uint8 version) noexcept {
121 // variant
122 uint8 Nn = static_cast<uint8>((Data4 >> 56) & 0xffu);
123 Data4 &= 0x00ffffffffffffffull;
124 Nn &= ~(0xc0u);
125 Nn |= 0x80u;
126 Data4 |= static_cast<uint64>(Nn) << 56;
127 // version
128 version &= 0x0fu;
129 uint8 Mm = static_cast<uint8>((Data3 >> 8) & 0xffu);
130 Data3 &= 0x00ffu;
131 Mm &= ~(0xf0u);
132 Mm |= (version << 4u);
133 Data3 |= static_cast<uint16>(Mm) << 8;
134 }
135 #if MPT_COMPILER_GCC
136 #pragma GCC diagnostic pop
137 #endif // MPT_COMPILER_GCC
138 #if MPT_OS_WINDOWS
139 private:
140 static mpt::UUID UUIDFromWin32(::UUID uuid) {
141 return mpt::UUID(uuid.Data1, uuid.Data2, uuid.Data3, (static_cast<uint64>(0) | (static_cast<uint64>(uuid.Data4[0]) << 56) | (static_cast<uint64>(uuid.Data4[1]) << 48) | (static_cast<uint64>(uuid.Data4[2]) << 40) | (static_cast<uint64>(uuid.Data4[3]) << 32) | (static_cast<uint64>(uuid.Data4[4]) << 24) | (static_cast<uint64>(uuid.Data4[5]) << 16) | (static_cast<uint64>(uuid.Data4[6]) << 8) | (static_cast<uint64>(uuid.Data4[7]) << 0)));
142 }
143 static ::UUID UUIDToWin32(mpt::UUID uuid) {
144 ::UUID result = ::UUID();
145 result.Data1 = uuid.GetData1();
146 result.Data2 = uuid.GetData2();
147 result.Data3 = uuid.GetData3();
148 result.Data4[0] = static_cast<uint8>(uuid.GetData4() >> 56);
149 result.Data4[1] = static_cast<uint8>(uuid.GetData4() >> 48);
150 result.Data4[2] = static_cast<uint8>(uuid.GetData4() >> 40);
151 result.Data4[3] = static_cast<uint8>(uuid.GetData4() >> 32);
152 result.Data4[4] = static_cast<uint8>(uuid.GetData4() >> 24);
153 result.Data4[5] = static_cast<uint8>(uuid.GetData4() >> 16);
154 result.Data4[6] = static_cast<uint8>(uuid.GetData4() >> 8);
155 result.Data4[7] = static_cast<uint8>(uuid.GetData4() >> 0);
156 return result;
157 }
158
159 public:
160 explicit UUID(::UUID uuid) {
161 *this = UUIDFromWin32(uuid);
162 }
163 operator ::UUID() const {
164 return UUIDToWin32(*this);
165 }
166 #endif // MPT_OS_WINDOWS
167 private:
168 static MPT_CONSTEXPRINLINE uint8 NibbleFromChar(char x) {
169 return ('0' <= x && x <= '9') ? static_cast<uint8>(x - '0' + 0) : ('a' <= x && x <= 'z') ? static_cast<uint8>(x - 'a' + 10)
170 : ('A' <= x && x <= 'Z') ? static_cast<uint8>(x - 'A' + 10)
171 : mpt::constexpr_throw<uint8>(std::domain_error(""));
172 }
173 static MPT_CONSTEXPRINLINE uint8 ByteFromHex(char x, char y) {
174 return static_cast<uint8>(uint8(0) | (NibbleFromChar(x) << 4) | (NibbleFromChar(y) << 0));
175 }
176 static MPT_CONSTEXPRINLINE uint16 ParseHex16(const char * str) {
177 return static_cast<uint16>(uint16(0) | (static_cast<uint16>(ByteFromHex(str[0], str[1])) << 8) | (static_cast<uint16>(ByteFromHex(str[2], str[3])) << 0));
178 }
179 static MPT_CONSTEXPRINLINE uint32 ParseHex32(const char * str) {
180 return static_cast<uint32>(uint32(0) | (static_cast<uint32>(ByteFromHex(str[0], str[1])) << 24) | (static_cast<uint32>(ByteFromHex(str[2], str[3])) << 16) | (static_cast<uint32>(ByteFromHex(str[4], str[5])) << 8) | (static_cast<uint32>(ByteFromHex(str[6], str[7])) << 0));
181 }
182
183 public:
184 static MPT_CONSTEXPRINLINE UUID ParseLiteral(const char * str, std::size_t len) {
185 return (len == 36 && str[8] == '-' && str[13] == '-' && str[18] == '-' && str[23] == '-') ? mpt::UUID(
186 ParseHex32(str + 0),
187 ParseHex16(str + 9),
188 ParseHex16(str + 14),
189 uint64(0)
190 | (static_cast<uint64>(ParseHex16(str + 19)) << 48)
191 | (static_cast<uint64>(ParseHex16(str + 24)) << 32)
192 | (static_cast<uint64>(ParseHex32(str + 28)) << 0))
193 : mpt::constexpr_throw<mpt::UUID>(std::domain_error(""));
194 }
195
196 public:
197 MPT_CONSTEXPRINLINE UUID() noexcept
198 : Data1(0)
199 , Data2(0)
200 , Data3(0)
201 , Data4(0) {
202 return;
203 }
204 MPT_CONSTEXPRINLINE explicit UUID(uint32 Data1, uint16 Data2, uint16 Data3, uint64 Data4) noexcept
205 : Data1(Data1)
206 , Data2(Data2)
207 , Data3(Data3)
208 , Data4(Data4) {
209 return;
210 }
211 explicit UUID(UUIDbin uuid) {
212 Data1 = uuid.Data1.get();
213 Data2 = uuid.Data2.get();
214 Data3 = uuid.Data3.get();
215 Data4 = uuid.Data4.get();
216 }
217 explicit UUID(GUIDms guid) {
218 Data1 = guid.Data1.get();
219 Data2 = guid.Data2.get();
220 Data3 = guid.Data3.get();
221 Data4 = guid.Data4.get();
222 }
223 operator UUIDbin() const {
224 UUIDbin result{};
225 result.Data1 = GetData1();
226 result.Data2 = GetData2();
227 result.Data3 = GetData3();
228 result.Data4 = GetData4();
229 return result;
230 }
231 operator GUIDms() const {
232 GUIDms result{};
233 result.Data1 = GetData1();
234 result.Data2 = GetData2();
235 result.Data3 = GetData3();
236 result.Data4 = GetData4();
237 return result;
238 }
239
240 public:
241 // Create a UUID
242 template <typename Trng>
243 static UUID Generate(Trng & rng) {
244 #if MPT_WINRT_AT_LEAST(MPT_WIN_8)
245 ::GUID guid = ::GUID();
246 HRESULT result = CoCreateGuid(&guid);
247 if (result != S_OK) {
248 return mpt::UUID::RFC4122Random(rng);
249 }
250 return mpt::UUID::UUIDFromWin32(guid);
251 #elif MPT_WINRT_BEFORE(MPT_WIN_8)
252 return mpt::UUID::RFC4122Random(rng);
253 #elif MPT_OS_WINDOWS
254 ::UUID uuid = ::UUID();
255 RPC_STATUS status = ::UuidCreate(&uuid);
256 if (status != RPC_S_OK && status != RPC_S_UUID_LOCAL_ONLY) {
257 return mpt::UUID::RFC4122Random(rng);
258 }
259 status = RPC_S_OK;
260 if (UuidIsNil(&uuid, &status) != FALSE) {
261 return mpt::UUID::RFC4122Random(rng);
262 }
263 if (status != RPC_S_OK) {
264 return mpt::UUID::RFC4122Random(rng);
265 }
266 return mpt::UUID::UUIDFromWin32(uuid);
267 #else
268 return mpt::UUID::RFC4122Random(rng);
269 #endif
270 }
271 // Create a UUID that contains local, traceable information.
272 // Safe for local use. May be faster.
273 template <typename Trng>
274 static UUID GenerateLocalUseOnly(Trng & rng) {
275 #if MPT_WINRT_AT_LEAST(MPT_WIN_8)
276 ::GUID guid = ::GUID();
277 HRESULT result = CoCreateGuid(&guid);
278 if (result != S_OK) {
279 return mpt::UUID::RFC4122Random(rng);
280 }
281 return mpt::UUID::UUIDFromWin32(guid);
282 #elif MPT_WINRT_BEFORE(MPT_WIN_8)
283 return mpt::UUID::RFC4122Random(rng);
284 #elif MPT_WINNT_AT_LEAST(MPT_WIN_XP)
285 // Available since Win2000, but we check for WinXP in order to not use this
286 // function in Win32old builds. It is not available on some non-fully
287 // patched Win98SE installs in the wild.
288 ::UUID uuid = ::UUID();
289 RPC_STATUS status = ::UuidCreateSequential(&uuid);
290 if (status != RPC_S_OK && status != RPC_S_UUID_LOCAL_ONLY) {
291 return Generate(rng);
292 }
293 status = RPC_S_OK;
294 if (UuidIsNil(&uuid, &status) != FALSE) {
295 return mpt::UUID::RFC4122Random(rng);
296 }
297 if (status != RPC_S_OK) {
298 return mpt::UUID::RFC4122Random(rng);
299 }
300 return mpt::UUID::UUIDFromWin32(uuid);
301 #elif MPT_OS_WINDOWS
302 // Fallback to ::UuidCreate is safe as ::UuidCreateSequential is only a
303 // tiny performance optimization.
304 return Generate(rng);
305 #else
306 return RFC4122Random(rng);
307 #endif
308 }
309 // Create a RFC4122 Random UUID.
310 template <typename Trng>
311 static UUID RFC4122Random(Trng & prng) {
312 UUID result;
313 result.Data1 = mpt::random<uint32>(prng);
314 result.Data2 = mpt::random<uint16>(prng);
315 result.Data3 = mpt::random<uint16>(prng);
316 result.Data4 = mpt::random<uint64>(prng);
317 result.MakeRFC4122(4);
318 return result;
319 }
320 friend UUID UUIDRFC4122NamespaceV3(const UUID & ns, const mpt::ustring & name);
321 friend UUID UUIDRFC4122NamespaceV5(const UUID & ns, const mpt::ustring & name);
322
323 public:
324 // General UUID<->string conversion.
325 // The string must/will be in standard UUID format: 4f9a455d-e7ef-4367-b2f0-0c83a38a5c72
326 static UUID FromString(const mpt::ustring & str) {
327 std::vector<mpt::ustring> segments = mpt::split<mpt::ustring>(str, MPT_ULITERAL("-"));
328 if (segments.size() != 5) {
329 return UUID();
330 }
331 if (segments[0].length() != 8) {
332 return UUID();
333 }
334 if (segments[1].length() != 4) {
335 return UUID();
336 }
337 if (segments[2].length() != 4) {
338 return UUID();
339 }
340 if (segments[3].length() != 4) {
341 return UUID();
342 }
343 if (segments[4].length() != 12) {
344 return UUID();
345 }
346 UUID result;
347 result.Data1 = mpt::parse_hex<uint32>(segments[0]);
348 result.Data2 = mpt::parse_hex<uint16>(segments[1]);
349 result.Data3 = mpt::parse_hex<uint16>(segments[2]);
350 result.Data4 = mpt::parse_hex<uint64>(segments[3] + segments[4]);
351 return result;
352 }
353 mpt::ustring ToUString() const {
354 return mpt::ustring()
355 + mpt::format<mpt::ustring>::hex0<8>(GetData1())
356 + MPT_USTRING("-")
357 + mpt::format<mpt::ustring>::hex0<4>(GetData2())
358 + MPT_USTRING("-")
359 + mpt::format<mpt::ustring>::hex0<4>(GetData3())
360 + MPT_USTRING("-")
361 + mpt::format<mpt::ustring>::hex0<4>(static_cast<uint16>(GetData4() >> 48))
362 + MPT_USTRING("-")
363 + mpt::format<mpt::ustring>::hex0<4>(static_cast<uint16>(GetData4() >> 32))
364 + mpt::format<mpt::ustring>::hex0<8>(static_cast<uint32>(GetData4() >> 0));
365 }
366 };
367
368 MPT_CONSTEXPRINLINE bool operator==(const mpt::UUID & a, const mpt::UUID & b) noexcept {
369 return (a.GetData1() == b.GetData1()) && (a.GetData2() == b.GetData2()) && (a.GetData3() == b.GetData3()) && (a.GetData4() == b.GetData4());
370 }
371
372 MPT_CONSTEXPRINLINE bool operator!=(const mpt::UUID & a, const mpt::UUID & b) noexcept {
373 return (a.GetData1() != b.GetData1()) || (a.GetData2() != b.GetData2()) || (a.GetData3() != b.GetData3()) || (a.GetData4() != b.GetData4());
374 }
375
376
377 namespace uuid_literals {
378
379 MPT_CONSTEVAL mpt::UUID operator""_uuid(const char * str, std::size_t len) {
380 return mpt::UUID::ParseLiteral(str, len);
381 }
382
383 } // namespace uuid_literals
384
385
386
387
388 } // namespace MPT_INLINE_NS
389 } // namespace mpt
390
391
392
393 #endif // MPT_UUID_UUID_HPP