-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuuid.cpp
201 lines (168 loc) · 5.72 KB
/
uuid.cpp
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
#include "uuid.hpp"
// std
#include <bit>
#include <cstring>
#include <iostream>
#include <limits>
#include <random>
// local
#include "hex.hpp"
#include "literals.hpp"
#include "traits.hpp"
namespace {
static std::random_device& rd()
{
static std::random_device static_rd;
return static_rd;
}
static std::mt19937& gen()
{
static std::mt19937 static_gen( rd()() );
return static_gen;
}
template <
typename T,
typename = std::enable_if_t<
util::is_one_of_v<T, uint8_t, uint16_t, uint32_t, uint64_t>>
>
T generate_rand()
{
static std::uniform_int_distribution<T> distribution(
std::numeric_limits<T>::min(),
std::numeric_limits<T>::max()
);
return distribution( gen() );
}
/// utility to swap endian-ness of a value
template <typename T>
constexpr T swap_endian(T v);
template <>
constexpr uint8_t swap_endian(uint8_t v) { return v; }
template <>
constexpr uint16_t swap_endian(uint16_t v) { return (v >> 8) | (v << 8); }
template <>
constexpr uint32_t swap_endian(uint32_t v)
{
uint32_t result = swap_endian<uint16_t>(v >> 16);
result |= (uint32_t)swap_endian<uint16_t>(v & 0xffff) << 16;
return result;
}
template <>
constexpr uint64_t swap_endian(uint64_t v)
{
uint64_t result = swap_endian<uint32_t>(v >> 32);
result |= (uint64_t)swap_endian<uint32_t>(v & 0xffffffff) << 32;
return result;
}
/// convert to big endian from native byte order or vice versa
/// - in memory data is BE
/// - if e.g. we have a non-standard field width we have to adjust before converting endian
/// - e.g. in case of 6 bytes representing 0x010203040506:
/// - uint8_t memory[] = { 1, 2, 3, 4, 5, 6 };
/// - memcpy 6 bytes to uint64_t value -> value = { 1, 2, 3, 4, 5, 6, 0, 0 };
/// - shift by sizeof(value) - count: value = { 0, 0, 1, 2, 3, 4, 5, 6 };
/// - endian convert: value = { 6, 5, 4, 3, 2, 1, 0, 0 };
template<
typename T,
typename = std::enable_if_t<
util::is_one_of_v<T, uint8_t, uint16_t, uint32_t, uint64_t>>
>
constexpr T be_convert(T v, size_t count = sizeof(T))
{
if constexpr ( __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ )
{
// nothing to do
return v;
}
v <<= ( (sizeof(T) - count) << 3 );
return swap_endian(v);
}
/// copy n bytes from \param data to return type
template <
typename T,
typename = std::enable_if_t<
util::is_one_of_v<T, uint8_t, uint16_t, uint32_t, uint64_t>>
>
T copy_bytes_to_value(const char* data, size_t count)
{
T result {};
memcpy(&result, data, count);
return be_convert(result, count);
}
// copy \param value into an array of \param data
template <
typename T,
typename = std::enable_if_t<
util::is_one_of_v<T, uint8_t, uint16_t, uint32_t, uint64_t>>
>
void copy_value_to_bytes(char* data, size_t count, T value)
{
value = be_convert(value);
memcpy(data, &value, count);
}
}
namespace util {
/// following rfc4122:
/// 0 1 2 3
/// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// | time_low |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// | time_mid | time_hi_and_version |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// |clk_seq_hi_res | clk_seq_low | node (0-1) |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// | node (2-5) |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
uint32_t uuid::time_low() const
{
return copy_bytes_to_value<uint32_t>( &data[0], 4 );
}
uint16_t uuid::time_mid() const
{
return copy_bytes_to_value<uint16_t>( &data[4], 2 );
}
uint16_t uuid::time_hi_version() const
{
return copy_bytes_to_value<uint16_t>( &data[6], 2 );
}
uint8_t uuid::clock_seq_hi_reserved() const
{
return copy_bytes_to_value<uint8_t>( &data[8], 1 );
}
uint8_t uuid::clock_seq_low() const
{
return copy_bytes_to_value<uint8_t>( &data[9], 1 );
}
uint64_t uuid::node() const
{
return copy_bytes_to_value<uint64_t>( &data[10], 6 );
}
uuid uuid::create_v4()
{
uuid result;
copy_value_to_bytes(&result.data[0], 4, generate_rand<uint32_t>());
copy_value_to_bytes(&result.data[4], 2, generate_rand<uint16_t>());
copy_value_to_bytes(&result.data[6], 2, (uint16_t)((generate_rand<uint16_t>() & 0x0fff) | 0x4000));
copy_value_to_bytes(&result.data[8], 2, (uint16_t)(generate_rand<uint16_t>() & 0x3fff + 0x8000));
copy_value_to_bytes(&result.data[10], 6, generate_rand<uint64_t>());
return result;
}
std::string uuid::to_string() const
{
// aaaaaaaa-bbbb-cccc-ddee-ffffgggggggg - 4 spacers + 32 chars
std::string result(36, 0);
char* p = &result[0];
for ( size_t i=0; i<16; ++i )
{
p += to_hex(p, data[i]);
if (i == 3 || i == 5 || i == 7 || i == 9)
*p++ = '-';
}
return result;
}
}
std::ostream& operator<<(std::ostream& os, const util::uuid& uuid)
{
return os << uuid.to_string();
}