summaryrefslogtreecommitdiff
path: root/pkuuid.h
diff options
context:
space:
mode:
authorJonathan Bradley <jcb@pikum.xyz>2025-03-19 18:15:52 -0400
committerJonathan Bradley <jcb@pikum.xyz>2025-03-20 12:01:56 -0400
commit9cd55867de91013bdbfe0d73112df504eb7963ba (patch)
tree35c7d25805c10a61dd65bd80febedb81e82c50bf /pkuuid.h
parentbc7e78b1766e8d9c9af1d47c207d084e161db98d (diff)
pkuuid.h: first-pass + bump version to 0.4.1
Diffstat (limited to 'pkuuid.h')
-rw-r--r--pkuuid.h203
1 files changed, 203 insertions, 0 deletions
diff --git a/pkuuid.h b/pkuuid.h
new file mode 100644
index 0000000..7426f79
--- /dev/null
+++ b/pkuuid.h
@@ -0,0 +1,203 @@
+#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();
+
+#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);
+#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;
+}
+
+#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