forked from GPUOpen-Drivers/pal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalUuid.h
401 lines (357 loc) · 12.4 KB
/
palUuid.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
***********************************************************************************************************************
*
* Copyright (c) 2021-2024 Advanced Micro Devices, Inc. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
**********************************************************************************************************************/
/**
***********************************************************************************************************************
* @file palUuid.h
* @brief Header for namespace Util::UUID. A collection of UUID generation functions
***********************************************************************************************************************
*/
#pragma once
#include "palUtil.h"
#include <string.h>
namespace Util
{
/**
***********************************************************************************************************************
* @brief Platform-agnostic UUID generation functions
***********************************************************************************************************************
*/
namespace Uuid
{
/// @brief Possible Version/Types of UUID
enum class Version : uint32
{
Invalid = 0, /// Not a recognized UUID
Version1 = 1, /// UUID is based on node and timestamp
Version3 = 3, /// UUID is a name local to a namespace (MD5)
Version4 = 4, /// UUID is random
Version5 = 5, /// UUID is a name local to a namespace (SHA1)
};
/// @brief Possible Variants of UUID
enum class Variant : uint32
{
Invalid, /// Not a recognized UUID
Rfc4122, /// UUID is stored in network byte order
MsCompatible /// UUID is stored in host byte order
};
/// @brief UUID 48-bit node sequence
struct Node
{
uint8 raw[6];
};
// Static asserts
static_assert((sizeof(Node) == 6), "Node must be 6 bytes in length");
/// @brief UUID 60-bit timestamp (stored in 64-bit register)
using Timestamp = uint64;
/// @brief UUID data storage structure
///
/// @note Byte order of data is determined by Variant:
/// * `Variant::Rfc4122` denotes network byte order (big-endian)
/// * `Variant::MsCompatible` denotes host byte order (mixed-endian)
///
/// @note `variantAndSequence` and `node` are always network byte order
struct UuidData
{
uint32 timeLow; /// low 32-bits of timestamp
uint16 timeMid; /// middle 16-bits of timestamp
uint16 timeHighAndVersion; /// 4-bit version and high 12-bits of timestamp
uint16 variantAndSequence; /// 1 to 3-bit variant and 13 to 15-bit sequence id
Node node; /// 48-bit node ID
};
// Static asserts
static_assert((sizeof(UuidData) == 16), "UuidData must be tightly packed");
static_assert((offsetof(UuidData, node) == 10), "UuidData::node must be at 10 byte offset");
/// @brief An accessor union of UUID in both raw bytes and UuidData formats
union Uuid
{
uint8 raw[sizeof(UuidData)]; /// raw digits of the UUID as byte array
uint64 raw64[2]; /// raw data as two 64-bit registers
UuidData data; /// helper accessor for UUID components
};
// Static asserts
static_assert((sizeof(Uuid) == 16), "UUID union must be 128-bit to fit in a register");
/// @brief Compare two UUIDs
///
/// @param left UUID to be compared
/// @param right UUID to be compared
///
/// @return integer value similar to ::std::memcmp
PAL_FORCE_INLINE int Compare(
const Uuid& left,
const Uuid& right)
{
return memcmp(left.raw, right.raw, sizeof(Uuid));
}
/// @brief Get the variant type of the UUID
///
/// @param uuid UUID to be checked
///
/// @return Variant type of the given UUID
PAL_FORCE_INLINE Variant GetVariant(
const Uuid& uuid) noexcept
{
constexpr int VariantByte = 8;
constexpr int VariantMask = 0b11000000;
constexpr int VariantShift = 6;
constexpr int VariantBitsRfc4122 = 0b10;
constexpr int VariantBitsMsCompatible = 0b11;
const int variantBits = (uuid.raw[VariantByte] & VariantMask) >> VariantShift;
return (variantBits == VariantBitsRfc4122)
? Variant::Rfc4122
: ((variantBits == VariantBitsMsCompatible)
? Variant::MsCompatible
: Variant::Invalid);
}
/// @brief Get the version (creation method) of the UUID
///
/// @param uuid UUID to be checked
///
/// @return Version/Type of the give UUID
PAL_FORCE_INLINE Version GetVersion(
const Uuid& uuid) noexcept
{
constexpr int VersionMask = 0xF0;
constexpr int VersionShift = 4;
constexpr int MaxVersion = 5;
constexpr int VersionByteRfc4122 = 6;
constexpr int VersionByteMsCompatible = 7;
const int versionByte = (GetVariant(uuid) != Variant::MsCompatible)
? VersionByteRfc4122
: VersionByteMsCompatible;
const int version = (uuid.raw[versionByte] & VersionMask) >> VersionShift;
return (version <= MaxVersion)
? static_cast<Version>(version)
: Version::Invalid;
}
/// @brief Get the version (creation method) of the UUID
///
/// @param uuid UUID to be checked
///
/// @return True if the UUID has a valid Version and Variant, False otherwise
PAL_FORCE_INLINE bool IsValid(
const Uuid& uuid) noexcept
{
return (GetVariant(uuid) != Variant::Invalid) &&
(GetVersion(uuid) != Version::Invalid);
}
/// @brief Convert a UUID to a string in 8-4-4-4-12 notation
///
/// @param uuid UUID to be converted
/// @param pOutString A buffer of at least 36 characters
///
/// @note This function will write exactly 36 characters into the buffer. No null terminator
/// will be written.
void ToString(
const Uuid& uuid,
char* pOutString);
/// @brief Convert a string in 8-4-4-4-12 notation to a UUID
///
/// @param pUuidString A buffer of at least 36 characters containing a UUID
///
/// @return A valid UUID on success, UuidNil on failure
///
/// @note This function will read exactly 36 characters from the buffer
Uuid FromString(
const char* pUuidString);
/// @brief Get the node ID of the machine
///
/// @return The 48-bit sequence identifying the current machine with the multicast bit set.
///
/// @note This ID may be local to the user and may not be portable between users
const Node& GetLocalNode();
/// @brief Get the current UUID timestamp
///
/// @return A 60-bit timestamp of 100-nanoseconds since epoch
Timestamp GetCurrentTimestamp();
/// @brief Get the UUID of the global namespace
///
/// @return A valid Uuid defining the global (portable) namespace
Uuid GetGlobalNamespace();
/// @brief Get the UUID of the local namespace
///
/// @return A valid Uuid defining the local (non-portable) namespace
Uuid GetLocalNamespace();
/// @brief Create a UUID Version 1 (node and time)
///
/// @param node A 48-bit node ID
/// @param timestamp A 60-bit timestamp
///
/// @return A valid Uuid that varies based on node and time
Uuid Uuid1(
const Node& node = GetLocalNode(),
Timestamp timestamp = GetCurrentTimestamp());
/// @brief Create a UUID Version 3 (MD5)
///
/// @param scope a valid Uuid defining a namespace
/// @param data a data buffer to be used as object name for hashing
/// @param dataSize size of data buffer in bytes
///
/// @return A valid Uuid defining an object within a namepace using MD5
Uuid Uuid3(
const Uuid& scope,
const void* data,
size_t dataSize);
/// @brief Create a UUID Version 3 from a known-length string (MD5)
///
/// @param scope a valid Uuid defining a namespace
/// @param name an object name with a known length
///
/// @return A valid Uuid defining an object within a namepace using MD5
template<size_t N>
Uuid Uuid3(
const Uuid& scope,
const char (&name)[N])
{
return Uuid3(scope, name, N);
}
/// @brief Create a UUID Version 4 (random)
///
/// @return A valid random Uuid
Uuid Uuid4();
/// @brief Create a UUID Version 5 (SHA1)
///
/// @param scope a valid Uuid defining a namespace
/// @param name a null-terminated string of the object name
/// @param data a data buffer to be used as object name for hashing
/// @param dataSize size of data buffer in bytes
///
/// @return A valid Uuid defining an object within a namepace using SHA1
Uuid Uuid5(
const Uuid& scope,
const void* data,
size_t dataSize);
/// @brief Create a UUID Version 3 from a known-length string (MD5)
///
/// @param scope a valid Uuid defining a namespace
/// @param name an object name with a known length
///
/// @return A valid Uuid defining an object within a namepace using MD5
template<size_t N>
Uuid Uuid5(
const Uuid& scope,
const char (&name)[N])
{
return Uuid5(scope, name, N);
}
/// @brief Create a UUID Version 5 (SHA1) with HMAC secret
///
/// @param scope a valid Uuid defining a namespace
/// @param data a data buffer to be used as object name for hashing
/// @param dataSize size of data buffer in bytes
/// @param secret a data buffer containing an HMAC shared secret
/// @param secretSize a data buffer containing an HMAC shared secret
///
/// @return A valid Uuid defining an object within a namepace using SHA1
Uuid Uuid5Hmac(
const Uuid& scope,
const void* data,
size_t dataSize,
const void* secret,
size_t secretSize);
/// {@
/// @note The following operators are provided for code clarity, and use with standard algorithms
/// @brief Equality operator for Node structure
///
/// @param left Node to be compared
/// @param right Node to be compared
///
/// @return true if the Node values are identical
constexpr inline bool operator==(
const Node& left,
const Node& right) noexcept
{
return (
(left.raw[0] == right.raw[0]) &&
(left.raw[1] == right.raw[1]) &&
(left.raw[2] == right.raw[2]) &&
(left.raw[3] == right.raw[3]) &&
(left.raw[4] == right.raw[4]) &&
(left.raw[5] == right.raw[5])
);
}
/// @brief Inequality operator for Node structure
///
/// @param left Node to be compared
/// @param right Node to be compared
///
/// @return true if the Node values are different
constexpr inline bool operator!=(
const Node& left,
const Node& right) noexcept
{
return (
(left.raw[0] != right.raw[0]) ||
(left.raw[1] != right.raw[1]) ||
(left.raw[2] != right.raw[2]) ||
(left.raw[3] != right.raw[3]) ||
(left.raw[4] != right.raw[4]) ||
(left.raw[5] != right.raw[5])
);
}
/// @brief Equality operator for Uuid structure
///
/// @param left Uuid to be compared
/// @param right Uuid to be compared
///
/// @return true if the Uuid values are identical
constexpr inline bool operator==(
const Uuid& left,
const Uuid& right) noexcept
{
return (
(left.raw64[0] == right.raw64[0]) &&
(left.raw64[1] == right.raw64[1])
);
}
/// @brief Inequality operator for Uuid structure
///
/// @param left Uuid to be compared
/// @param right Uuid to be compared
///
/// @return true if the Uuid values are different
constexpr inline bool operator!=(
const Uuid& left,
const Uuid& right) noexcept
{
return (
(left.raw64[0] != right.raw64[0]) ||
(left.raw64[1] != right.raw64[1])
);
}
/// @brief less-than operator for Uuid structure
///
/// @param left Uuid to be compared
/// @param right Uuid to be compared
///
/// @return true if the left Uuid is less than the right
inline bool operator<(
const Uuid& left,
const Uuid& right) noexcept
{
return Compare(left, right) < 0;
}
/// @}
} // namespace Uuid
} // namespace Util