| 1 |
|
| #ifndef HORUS_UTIL_H |
| 2 |
|
| #define HORUS_UTIL_H |
| 3 |
|
| |
| 4 |
|
| /* |
| 5 |
|
| * horus_util.h - UUID utility functions: validate, compare, extract |
| 6 |
|
| */ |
| 7 |
|
| |
| 8 |
|
| #include <string.h> |
| 9 |
|
| #include <stdint.h> |
| 10 |
|
| |
| 11 |
|
| /* ── Extract version from binary UUID ───────────────────────────── */ |
| 12 |
|
| |
| 13 |
21 |
| static inline int horus_uuid_version_bin(const unsigned char *uuid) { |
| 14 |
21 |
| return (uuid[6] >> 4) & 0x0F; |
| 15 |
|
| } |
| 16 |
|
| |
| 17 |
|
| /* ── Extract variant from binary UUID ───────────────────────────── */ |
| 18 |
|
| |
| 19 |
18 |
| static inline int horus_uuid_variant_bin(const unsigned char *uuid) { |
| 20 |
18 |
| unsigned char v = uuid[8]; |
| 21 |
18 |
▶ | if ((v & 0x80) == 0) return 0; /* NCS backward compatibility */ |
Branch: 1 branch
| Branch | True | False | Total |
| if ((v & 0x80) == 0) return 0; /* NCS backward compatibility */ |
0 |
18 |
18 |
|
| 22 |
18 |
▶ | if ((v & 0xC0) == 0x80) return 1; /* RFC 9562 (standard) */ |
Branch: 1 branch
| Branch | True | False | Total |
| if ((v & 0xC0) == 0x80) return 1; /* RFC 9562 (standard) */ |
18 |
0 |
18 |
|
| 23 |
0 |
▶ | if ((v & 0xE0) == 0xC0) return 2; /* Microsoft */ |
Branch: 1 branch
| Branch | True | False | Total |
| if ((v & 0xE0) == 0xC0) return 2; /* Microsoft */ |
0 |
0 |
0 |
|
| 24 |
0 |
| return 3; /* Future/reserved */ |
| 25 |
|
| } |
| 26 |
|
| |
| 27 |
|
| /* ── Compare two binary UUIDs ───────────────────────────────────── */ |
| 28 |
|
| |
| 29 |
5 |
| static inline int horus_uuid_cmp_bin(const unsigned char *a, const unsigned char *b) { |
| 30 |
5 |
| return memcmp(a, b, 16); |
| 31 |
|
| } |
| 32 |
|
| |
| 33 |
|
| /* ── Check if NIL ───────────────────────────────────────────────── */ |
| 34 |
|
| |
| 35 |
16 |
| static inline int horus_uuid_is_nil_bin(const unsigned char *uuid) { |
| 36 |
|
| /* Optimise with 64-bit comparison */ |
| 37 |
16 |
| const uint64_t *p = (const uint64_t *)uuid; |
| 38 |
16 |
▶ | return (p[0] == 0) && (p[1] == 0); |
Branch: 2 branches
| Branch | True | False | Total |
| return (p[0] == 0) && (p[1] == 0); |
2 |
14 |
16 |
| return (p[0] == 0) && (p[1] == 0); |
2 |
0 |
2 |
|
| 39 |
|
| } |
| 40 |
|
| |
| 41 |
|
| /* ── Check if MAX ───────────────────────────────────────────────── */ |
| 42 |
|
| |
| 43 |
15 |
| static inline int horus_uuid_is_max_bin(const unsigned char *uuid) { |
| 44 |
15 |
| const uint64_t *p = (const uint64_t *)uuid; |
| 45 |
15 |
| return (p[0] == UINT64_C(0xFFFFFFFFFFFFFFFF)) |
| 46 |
15 |
▶ | && (p[1] == UINT64_C(0xFFFFFFFFFFFFFFFF)); |
Branch: 2 branches
| Branch | True | False | Total |
| && (p[1] == UINT64_C(0xFFFFFFFFFFFFFFFF)); |
2 |
13 |
15 |
| && (p[1] == UINT64_C(0xFFFFFFFFFFFFFFFF)); |
2 |
0 |
2 |
|
| 47 |
|
| } |
| 48 |
|
| |
| 49 |
|
| /* ── Validate a UUID string (any format) ────────────────────────── */ |
| 50 |
|
| |
| 51 |
17 |
| static inline int horus_uuid_validate(const char *input, size_t len) { |
| 52 |
|
| unsigned char tmp[16]; |
| 53 |
17 |
▶ | if (horus_parse_uuid(tmp, input, len) != HORUS_PARSE_OK) |
Branch: 1 branch
| Branch | True | False | Total |
| if (horus_parse_uuid(tmp, input, len) != HORUS_PARSE_OK) |
5 |
12 |
17 |
|
| 54 |
5 |
| return 0; |
| 55 |
|
| /* Check that variant is RFC 9562 or it's NIL/MAX */ |
| 56 |
12 |
▶ | if (horus_uuid_is_nil_bin(tmp) || horus_uuid_is_max_bin(tmp)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (horus_uuid_is_nil_bin(tmp) || horus_uuid_is_max_bin(tmp)) |
11 |
1 |
12 |
| if (horus_uuid_is_nil_bin(tmp) || horus_uuid_is_max_bin(tmp)) |
1 |
10 |
11 |
|
| 57 |
2 |
| return 1; |
| 58 |
10 |
| return (horus_uuid_variant_bin(tmp) == 1) ? 1 : 0; |
| 59 |
|
| } |
| 60 |
|
| |
| 61 |
|
| /* ── Extract timestamp as Unix epoch seconds (NV) ───────────────── */ |
| 62 |
|
| |
| 63 |
6 |
| static inline double horus_uuid_extract_time(const unsigned char *uuid) { |
| 64 |
6 |
| int version = horus_uuid_version_bin(uuid); |
| 65 |
6 |
| switch (version) { |
| 66 |
2 |
| case 1: |
| 67 |
2 |
| return horus_gregorian_to_unix(horus_extract_time_v1(uuid)); |
| 68 |
1 |
| case 6: |
| 69 |
1 |
| return horus_gregorian_to_unix(horus_extract_time_v6(uuid)); |
| 70 |
2 |
| case 7: |
| 71 |
2 |
| return horus_ms_to_unix(horus_extract_time_v7(uuid)); |
| 72 |
1 |
| default: |
| 73 |
1 |
| return 0.0; /* No timestamp for other versions */ |
| 74 |
|
| } |
| 75 |
|
| } |
| 76 |
|
| |
| 77 |
|
| #endif /* HORUS_UTIL_H */ |