Blender V5.3
uuid.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_assert.hh"
10#include "BLI_string.hh"
11#include "BLI_uuid.hh"
12
13#include <cstdio>
14#include <cstring>
15#include <ctime>
16#include <random>
17#include <sstream>
18#include <string>
19#include <tuple>
20
21namespace blender {
22
23/* Ensure the UUID struct doesn't have any padding, to be compatible with memcmp(). */
24static_assert(sizeof(UUID) == 16, "expect UUIDs to be 128 bit exactly");
25
27{
28 static std::mt19937_64 rng = []() {
29 std::mt19937_64 rng;
30
31 /* Ensure the RNG really can output 64-bit values. */
32 static_assert(std::mt19937_64::min() == 0LL);
33 static_assert(std::mt19937_64::max() == 0xffffffffffffffffLL);
34
35 timespec ts;
36 timespec_get(&ts, TIME_UTC);
37 /* XOR the nanosecond and second fields, just in case the clock only has seconds resolution. */
38 uint64_t seed = ts.tv_nsec;
39 seed ^= ts.tv_sec;
40 rng.seed(seed);
41
42 return rng;
43 }();
44
45 bUUID uuid;
46
47 /* RFC4122 suggests setting certain bits to a fixed value, and then randomizing the remaining
48 * bits. The opposite is easier to implement, though, so that's what's done here. */
49
50 /* Read two 64-bit numbers to randomize all 128 bits of the UUID. */
51 uint64_t *uuid_as_int64 = reinterpret_cast<uint64_t *>(&uuid);
52 uuid_as_int64[0] = rng();
53 uuid_as_int64[1] = rng();
54
55 /* Set the most significant four bits to 0b0100 to indicate version 4 (random UUID). */
56 uuid.time_hi_and_version &= ~0xF000;
57 uuid.time_hi_and_version |= 0x4000;
58
59 /* Set the most significant two bits to 0b10 to indicate compatibility with RFC4122. */
60 uuid.clock_seq_hi_and_reserved &= ~0x40;
61 uuid.clock_seq_hi_and_reserved |= 0x80;
62
63 return uuid;
64}
65
67{
68 const bUUID nil = {0, 0, 0, 0, 0, {0}};
69 return nil;
70}
71
73{
74 return BLI_uuid_equal(BLI_uuid_nil(), uuid);
75}
76
77bool BLI_uuid_equal(const bUUID uuid1, const bUUID uuid2)
78{
79 return std::memcmp(&uuid1, &uuid2, sizeof(uuid1)) == 0;
80}
81
82void BLI_uuid_format(char *buffer, const bUUID uuid)
83{
84 const size_t buffer_len_unclamped = BLI_snprintf(
85 buffer,
87 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
88 uuid.time_low,
89 uuid.time_mid,
92 uuid.clock_seq_low,
93 uuid.node[0],
94 uuid.node[1],
95 uuid.node[2],
96 uuid.node[3],
97 uuid.node[4],
98 uuid.node[5]);
99
100 /* Assert the string length is not clamped. */
101 BLI_assert(buffer_len_unclamped == UUID_STRING_SIZE - 1);
102 UNUSED_VARS_NDEBUG(buffer_len_unclamped);
103}
104
105bool BLI_uuid_parse_string(bUUID *uuid, const char *buffer)
106{
107 const int fields_parsed_num = std::sscanf(
108 buffer,
109 "%8x-%4hx-%4hx-%2hhx%2hhx-%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx",
110 &uuid->time_low,
111 &uuid->time_mid,
112 &uuid->time_hi_and_version,
114 &uuid->clock_seq_low,
115 &uuid->node[0],
116 &uuid->node[1],
117 &uuid->node[2],
118 &uuid->node[3],
119 &uuid->node[4],
120 &uuid->node[5]);
121 return fields_parsed_num == 11;
122}
123
124std::ostream &operator<<(std::ostream &stream, bUUID uuid)
125{
126 std::string buffer(36, '\0');
127 BLI_uuid_format(buffer.data(), uuid);
128 stream << buffer;
129 return stream;
130}
131
132UUID::UUID(const std::initializer_list<uint32_t> field_values)
133{
134 BLI_assert_msg(field_values.size() == 11, "UUID requires 5 regular fields + 6 `node` values");
135
136 const auto *field_iter = field_values.begin();
137
138 this->time_low = *field_iter++;
139 this->time_mid = uint16_t(*field_iter++);
140 this->time_hi_and_version = uint16_t(*field_iter++);
141 this->clock_seq_hi_and_reserved = uint8_t(*field_iter++);
142 this->clock_seq_low = uint8_t(*field_iter++);
143
144 std::copy(field_iter, field_values.end(), this->node);
145}
146
147UUID::UUID(const StringRefNull string_formatted_uuid)
148{
149 const bool parsed_ok = BLI_uuid_parse_string(this, string_formatted_uuid.c_str());
150 if (!parsed_ok) {
151 std::stringstream ss;
152 ss << "invalid UUID string " << string_formatted_uuid;
153 throw std::runtime_error(ss.str());
154 }
155}
156
157UUID::UUID(const bUUID &struct_uuid)
158{
159 *(static_cast<bUUID *>(this)) = struct_uuid;
160}
161
162std::string UUID::str() const
163{
164 std::string string(36, '\0');
165 BLI_uuid_format(string.data(), *this);
166 return string;
167}
168
170{
171 /* Convert the struct into two 64-bit numbers, and XOR them to get the hash. */
172 const uint64_t *uuid_as_int64 = reinterpret_cast<const uint64_t *>(this);
173 return uuid_as_int64[0] ^ uuid_as_int64[1];
174}
175
176bool operator==(const UUID uuid1, const UUID uuid2)
177{
178 return BLI_uuid_equal(uuid1, uuid2);
179}
180
181bool operator!=(const UUID uuid1, const UUID uuid2)
182{
183 return !(uuid1 == uuid2);
184}
185
186bool operator<(const UUID uuid1, const UUID uuid2)
187{
188 auto simple_fields1 = std::tie(uuid1.time_low,
189 uuid1.time_mid,
192 uuid1.clock_seq_low);
193 auto simple_fields2 = std::tie(uuid2.time_low,
194 uuid2.time_mid,
197 uuid2.clock_seq_low);
198 if (simple_fields1 == simple_fields2) {
199 return std::memcmp(uuid1.node, uuid2.node, sizeof(uuid1.node)) < 0;
200 }
201 return simple_fields1 < simple_fields2;
202}
203
204} // namespace blender
#define UUID_STRING_SIZE
#define UNUSED_VARS_NDEBUG(...)
#define BLI_assert_msg(a, msg)
constexpr const char * c_str() const
UUID()=default
uint64_t hash() const
Definition uuid.cc:169
std::string str() const
Definition uuid.cc:162
std::ostream & operator<<(std::ostream &stream, const AssetCatalogPath &path_to_append)
bool operator==(const Error &left, const Error &right)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned __int64 uint64_t
Definition BKE_text.h:91
size_t BLI_snprintf(char *__restrict dst, size_t dst_maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
bool BLI_uuid_is_nil(bUUID uuid)
Definition uuid.cc:72
bool BLI_uuid_equal(bUUID uuid1, bUUID uuid2)
Definition uuid.cc:77
bool operator!=(const CPPType &a, const CPPType &b)
unsigned short uint16_t
Definition BKE_text.h:80
BMesh const char void * data
unsigned char uint8_t
Definition BKE_text.h:79
constexpr bool operator<(StringRef a, StringRef b)
bUUID BLI_uuid_generate_random()
Definition uuid.cc:26
bool BLI_uuid_parse_string(bUUID *uuid, const char *buffer) ATTR_NONNULL()
Definition uuid.cc:105
void BLI_uuid_format(char *buffer, bUUID uuid) ATTR_NONNULL()
Definition uuid.cc:82
bUUID BLI_uuid_nil()
Definition uuid.cc:66
Universally Unique Identifier according to RFC4122.
uint16_t time_hi_and_version
uint8_t node[6]
uint8_t clock_seq_hi_and_reserved
uint8_t clock_seq_low