From c83c652b3761e5300841984c0bc333884a3680f1 Mon Sep 17 00:00:00 2001 From: Abdelrahman Said Date: Sat, 22 Mar 2025 02:27:59 +0000 Subject: [PATCH] Add UUIDv4 generator --- src/uuid/uuid.c | 59 ++++++++++++++++++++++++++++++++++++++++++++ src/uuid/uuid.h | 34 +++++++++++++++++++++++++ src/uuid/wapp_uuid.c | 8 ++++++ src/uuid/wapp_uuid.h | 9 +++++++ src/wapp.c | 1 + src/wapp.h | 1 + 6 files changed, 112 insertions(+) create mode 100644 src/uuid/uuid.c create mode 100644 src/uuid/uuid.h create mode 100644 src/uuid/wapp_uuid.c create mode 100644 src/uuid/wapp_uuid.h diff --git a/src/uuid/uuid.c b/src/uuid/uuid.c new file mode 100644 index 0000000..431615d --- /dev/null +++ b/src/uuid/uuid.c @@ -0,0 +1,59 @@ +#include "uuid.h" +#include "../common/aliases/aliases.h" +#include "../core/strings/str8/str8.h" +#include "../prng/xorshift/xorshift.h" +#include +#include + +#define UUID_STR_FORMAT ("%.8" PRIx64 "-%.4" PRIx64 "-%.4" PRIx64 "-%.4" PRIx64 "-%.12" PRIx64) + +typedef struct uuid4 UUID4; +struct uuid4 { + u64 high; + u64 low; +}; + +internal UUID4 generate_uuid4(void); +internal void uuid4_to_uuid(const UUID4* uuid4, UUID *uuid); + +UUID *wapp_uuid_init_uuid4(UUID *uuid) { + if (!uuid) { + goto RETURN_INIT_UUID4; + } + + UUID4 uuid4 = generate_uuid4(); + uuid4_to_uuid(&uuid4, uuid); + +RETURN_INIT_UUID4: + return uuid; +} + +internal UUID4 generate_uuid4(void) { + persistent XOR256State state = {0}; + persistent bool initialised = false; + + if (!initialised) { + initialised = true; + state = wapp_prng_xorshift_init_state(); + } + + UUID4 uuid = (UUID4){ + .high = wapp_prng_xorshift_256(&state), + .low = wapp_prng_xorshift_256(&state), + }; + + uuid.high = (uuid.high & 0xffffffffffff0fff) | 0x0000000000004000; + uuid.low = (uuid.low & 0x3fffffffffffffff) | 0x8000000000000000; + + return uuid; +} + +internal void uuid4_to_uuid(const UUID4* uuid4, UUID *uuid) { + u64 grp1 = uuid4->high >> 32; + u64 grp2 = (uuid4->high << 32) >> 48; + u64 grp3 = (uuid4->high << 48) >> 48; + u64 grp4 = uuid4->low >> 48; + u64 grp5 = (uuid4->low << 16) >> 16; + + wapp_str8_format(&(uuid->uuid), UUID_STR_FORMAT, grp1, grp2, grp3, grp4, grp5); +} diff --git a/src/uuid/uuid.h b/src/uuid/uuid.h new file mode 100644 index 0000000..bfbec98 --- /dev/null +++ b/src/uuid/uuid.h @@ -0,0 +1,34 @@ +#ifndef UUID_H +#define UUID_H + +#include "../common/aliases/aliases.h" +#include "../core/strings/str8/str8.h" + +#ifdef __cplusplus +BEGIN_C_LINKAGE +#endif // __cplusplus + +#define UUID_BUF_LENGTH 48 +#define WAPP_UUID_SPEC WAPP_STR8_SPEC +#define wapp_uuid_varg(UUID) wapp_str8_varg((UUID).uuid) + +typedef struct uuid UUID; +struct uuid { + Str8 uuid; +}; + +#define wapp_uuid_gen_uuid4() *(wapp_uuid_init_uuid4(&wapp_uuid_create())) + +/* Low level UUID API */ + +#define wapp_uuid_create() ((UUID){.uuid = wapp_str8_buf(UUID_BUF_LENGTH)}) + +// Just returns the same pointer that was passed in with the UUID initialised. +// If the pointer is NULL, it skips initialisation. +UUID *wapp_uuid_init_uuid4(UUID *uuid); + +#ifdef __cplusplus +END_C_LINKAGE +#endif // __cplusplus + +#endif // !UUID_H diff --git a/src/uuid/wapp_uuid.c b/src/uuid/wapp_uuid.c new file mode 100644 index 0000000..7fa7ed4 --- /dev/null +++ b/src/uuid/wapp_uuid.c @@ -0,0 +1,8 @@ +#ifndef WAPP_UUID_C +#define WAPP_UUID_C + +#include "uuid.c" +#include "../core/wapp_core.c" +#include "../prng/wapp_prng.c" + +#endif // !WAPP_UUID_C diff --git a/src/uuid/wapp_uuid.h b/src/uuid/wapp_uuid.h new file mode 100644 index 0000000..094ccd2 --- /dev/null +++ b/src/uuid/wapp_uuid.h @@ -0,0 +1,9 @@ +#ifndef WAPP_UUID_H +#define WAPP_UUID_H + +#include "uuid.h" +#include "../common/wapp_common.h" +#include "../core/wapp_core.h" +#include "../prng/wapp_prng.h" + +#endif // !WAPP_UUID_H diff --git a/src/wapp.c b/src/wapp.c index 16d148e..1459e5d 100644 --- a/src/wapp.c +++ b/src/wapp.c @@ -4,6 +4,7 @@ #include "wapp.h" #include "core/wapp_core.c" #include "prng/wapp_prng.c" +#include "uuid/uuid.c" #include "testing/wapp_testing.c" #endif // !WAPP_C diff --git a/src/wapp.h b/src/wapp.h index cddb106..68094dc 100644 --- a/src/wapp.h +++ b/src/wapp.h @@ -4,6 +4,7 @@ #include "common/wapp_common.h" #include "core/wapp_core.h" #include "prng/wapp_prng.h" +#include "uuid/wapp_uuid.h" #include "testing/wapp_testing.h" #endif // !WAPP_H