summaryrefslogtreecommitdiff
path: root/pkuuid.h
blob: 30d4a2412c3f32e99c56e0f53a595a838973dc21 (plain)
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
#ifndef PK_UUID_H
#define PK_UUID_H

#include "stddef.h"
#include <time.h>

struct pk_uuid {
	alignas(max_align_t) unsigned char uuid[16];
};

const struct pk_uuid pk_uuid_zed = { .uuid = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } };
const struct pk_uuid pk_uuid_max = { .uuid = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF } };

#define pk_uuid_printf_format PK_Q(%.2x%.2x%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x%.2x%.2x%.2x%.2x)
#define pk_uuid_printf_var(id) id.uuid[0], id.uuid[1], id.uuid[2], id.uuid[3], id.uuid[4], id.uuid[5], id.uuid[6], id.uuid[7], id.uuid[8], id.uuid[9], id.uuid[10], id.uuid[11], id.uuid[12], id.uuid[13], id.uuid[14], id.uuid[15]

void pk_uuid_init(time_t srand_seed);
void pk_uuid_teardown();

struct pk_uuid pk_uuid_new_v7();
bool pk_uuid_equals(struct pk_uuid lhs, struct pk_uuid rhs);

#if defined(__cplusplus)
#include <ostream>
#include <iomanip>
std::ostream& operator<<(std::ostream &o, const struct pk_uuid& uuid);
std::istream& operator>>(std::istream &i, struct pk_uuid& uuid);
bool operator==(const pk_uuid &lhs, const pk_uuid &rhs) { return pk_uuid_equals(lhs, rhs); }
bool operator!=(const pk_uuid &lhs, const pk_uuid &rhs) { return !pk_uuid_equals(lhs, rhs); }
#endif

#endif /* PK_UUID_H */

#ifdef PK_IMPL_UUID

#include <stdatomic.h>
#include <stdlib.h>
#include <stdint.h>

// TODO JCB - 2025-03-19
// This should have platform-specific defines
#ifndef PK_UUID_CLOCK
	#ifdef CLOCK_TAI
		#define PK_UUID_CLOCK CLOCK_TAI
	#else
		#define PK_UUID_CLOCK CLOCK_REALTIME
	#endif
#endif

void
pk_uuid_init(time_t srand_seed)
{
	// TODO 2025-03-19 - JCB
	// pk.h should NOT be setting srand.
	// Replace dependency on rand/srand with a sufficient rand() implementation.
	// I would prefer if generating a UUID did not advance a global random.
	// Consider creating a pkrand.h to resolve this.
	srand(srand_seed);
}

void
pk_uuid_teardown()
{
}

struct pk_uuid
pk_uuid_new_v7()
{
	const int n = 1;
	uint32_t r;
	// https://www.rfc-editor.org/rfc/rfc9562.html#name-uuid-version-7
	struct pk_uuid ret;
	struct timespec t;
	clock_gettime(PK_UUID_CLOCK, &t);
	uint32_t sec = (uint32_t)t.tv_sec;
	uint32_t nsec = (uint32_t)t.tv_nsec;
	// [000-047] (6 bytes) big-endian unix epoch
	// TODO test this on a big-endian machine, I don't think this is correct.
	// This `if` determines if we are big or little endian.
	// A return value of 1 says we are little endian, so swap the bits.
	if (*(char *)&n == 1) {
		ret.uuid[0] = (uint8_t)((sec  & 0xFF000000) >> 24);
		ret.uuid[1] = (uint8_t)((sec  & 0x00FF0000) >> 16);
		ret.uuid[2] = (uint8_t)((sec  & 0x0000FF00) >> 8);
		ret.uuid[3] = (uint8_t)((sec  & 0x000000FF) >> 0);
		ret.uuid[4] = (uint8_t)((nsec & 0x0000FF00) >> 8);
		ret.uuid[5] = (uint8_t)((nsec & 0x000000FF) >> 0);
	} else {
		ret.uuid[0] = (uint8_t)((sec  & 0xFF000000) >> 0);
		ret.uuid[1] = (uint8_t)((sec  & 0x00FF0000) >> 8);
		ret.uuid[2] = (uint8_t)((sec  & 0x0000FF00) >> 16);
		ret.uuid[3] = (uint8_t)((sec  & 0x000000FF) >> 24);
		ret.uuid[4] = (uint8_t)((nsec & 0xFF000000) >> 0);
		ret.uuid[5] = (uint8_t)((nsec & 0x00FF0000) >> 8);
	}
	// [052-127] random
	r = (uint32_t)rand();
	if (*(char *)&n == 1) {
		ret.uuid[8]  = (uint8_t)((r & 0xFF000000) >> 24);
		ret.uuid[9]  = (uint8_t)((r & 0x00FF0000) >> 16);
		ret.uuid[10] = (uint8_t)((r & 0x0000FF00) >> 8);
		ret.uuid[11] = (uint8_t)((r & 0x000000FF) >> 0);
	} else {
		ret.uuid[8]  = (uint8_t)((r & 0xFF000000) >> 0);
		ret.uuid[9]  = (uint8_t)((r & 0x00FF0000) >> 8);
		ret.uuid[10] = (uint8_t)((r & 0x0000FF00) >> 16);
		ret.uuid[11] = (uint8_t)((r & 0x000000FF) >> 24);
	}
	r = rand();
	if (*(char *)&n == 1) {
		ret.uuid[12] = (uint8_t)((r & 0xFF000000) >> 24);
		ret.uuid[13] = (uint8_t)((r & 0x00FF0000) >> 16);
		ret.uuid[14] = (uint8_t)((r & 0x0000FF00) >> 8);
		ret.uuid[15] = (uint8_t)((r & 0x000000FF) >> 0);
	} else {
		ret.uuid[12] = (uint8_t)((r & 0xFF000000) >> 0);
		ret.uuid[13] = (uint8_t)((r & 0x00FF0000) >> 8);
		ret.uuid[14] = (uint8_t)((r & 0x0000FF00) >> 16);
		ret.uuid[15] = (uint8_t)((r & 0x000000FF) >> 24);
	}
	ret.uuid[6]  = ret.uuid[9]  ^ ret.uuid[12];
	ret.uuid[7]  = ret.uuid[10] ^ ret.uuid[15];

	// [048-051] v7 nibble
	// version must be 0x7_
	// 0x70 is 0b01110000
	// 0x7F is 0b01111111
	ret.uuid[6] |= 0x70;
	ret.uuid[6] &= 0x7F;

	// [064-065] 2-bit variant field
	// variant must be 0b10
	// 0x80 is 0b10000000
	// 0xBF is 0b10111111
	ret.uuid[8] |= 0x80;
	ret.uuid[8] &= 0xBF;

	return ret;
}

bool pk_uuid_equals(struct pk_uuid lhs, struct pk_uuid rhs)
{
	int i;
	for (i = 0; i < 16; ++i) {
		if (lhs.uuid[i] != rhs.uuid[i]) return false;
	}
	return true;
}

#if defined(__cplusplus)
#include "./pkstn.h" /* deleteme */
std::ostream&
operator<<(std::ostream &o, const struct pk_uuid& uuid)
{
	int i;
	o << std::hex;
	for (i = 0; i < 4; ++i) {
		o << std::setw(2) << std::setfill('0');
		o << (uint16_t)uuid.uuid[i];
	}
	o << "-";
	for (i = 4; i < 6; ++i) {
		o << std::setw(2) << std::setfill('0');
		o << (uint16_t)uuid.uuid[i];
	}
	o << "-";
	for (i = 6; i < 8; ++i) {
		o << std::setw(2) << std::setfill('0');
		o << (uint16_t)uuid.uuid[i];
	}
	o << "-";
	for (i = 8; i < 10; ++i) {
		o << std::setw(2) << std::setfill('0');
		o << (uint16_t)uuid.uuid[i];
	}
	o << "-";
	for (i = 10; i < 16; ++i) {
		o << std::setw(2) << std::setfill('0');
		o << (uint16_t)uuid.uuid[i];
	}
	return o;
}

std::istream&
operator>>(std::istream &i, struct pk_uuid& uuid)
{
	char c[3];
	char k = 0;
	char offset = 0;
	c[2] = '\0';
	for (k = 0; k < 20; ++k) {
		if (k == 4 || k == 7 || k == 10 || k == 13) {
			offset += 1;
			c[0] = i.peek();
			if (c[0] != '-') {
				goto err_out;
			}
			i.get(); // burn
			continue;
		}
		i.get(c[0]);
		i.get(c[1]);
		if (pk_stn_uint8_t(&uuid.uuid[k - offset], c, 16) != PK_STN_RES_SUCCESS) {
			goto err_out;
		}
	}
	return i;
err_out:
	i.seekg(-(((k + 1) * 2) + offset), std::ios_base::cur);
	uuid = pk_uuid_zed;
	return i;
}
#endif

#endif