File Coverage

include/horus_format.h
Criterion Covered Total %
statement 69 73 94.5
branch n/a
condition n/a
subroutine n/a
pod n/a
total 69 73 94.5


line stmt bran cond sub pod time code
1             #ifndef HORUS_FORMAT_H
2             #define HORUS_FORMAT_H
3              
4             /*
5             * horus_format.h - Format 16-byte UUID binary into output strings
6             *
7             * All formatters write into a pre-sized buffer. Caller is responsible
8             * for allocating enough space (use HORUS_FMT_*_LEN constants).
9             */
10              
11             #include "horus_encode.h"
12              
13             /* Output lengths (excluding NUL terminator) */
14             #define HORUS_FMT_STR_LEN 36
15             #define HORUS_FMT_HEX_LEN 32
16             #define HORUS_FMT_BRACES_LEN 38
17             #define HORUS_FMT_URN_LEN 45
18             #define HORUS_FMT_BASE64_LEN 22
19             #define HORUS_FMT_BASE32_LEN 26
20             #define HORUS_FMT_CROCKFORD_LEN 26
21             #define HORUS_FMT_BINARY_LEN 16
22              
23             /* Format enum */
24             typedef enum {
25             HORUS_FMT_STR = 0, /* 550e8400-e29b-41d4-a716-446655440000 */
26             HORUS_FMT_HEX = 1, /* 550e8400e29b41d4a716446655440000 */
27             HORUS_FMT_BRACES = 2, /* {550e8400-e29b-41d4-a716-446655440000} */
28             HORUS_FMT_URN = 3, /* urn:uuid:550e8400-e29b-41d4-a716-446655440000 */
29             HORUS_FMT_BASE64 = 4, /* 22-char base64, no padding */
30             HORUS_FMT_BASE32 = 5, /* 26-char RFC 4648 */
31             HORUS_FMT_CROCKFORD = 6, /* 26-char Crockford base32 */
32             HORUS_FMT_BINARY = 7, /* raw 16 bytes */
33             HORUS_FMT_UPPER_STR = 8, /* uppercase hyphenated */
34             HORUS_FMT_UPPER_HEX = 9 /* uppercase no hyphens */
35             } horus_format_t;
36              
37             /* ── Hyphenated format (8-4-4-4-12) ────────────────────────────── */
38              
39 5009           static inline void horus_format_hyphenated(char *dst, const unsigned char *uuid,
40             const uint16_t *lut) {
41             /* bytes 0-3 */
42 5009           horus_hex_byte(dst + 0, uuid[0], lut);
43 5009           horus_hex_byte(dst + 2, uuid[1], lut);
44 5009           horus_hex_byte(dst + 4, uuid[2], lut);
45 5009           horus_hex_byte(dst + 6, uuid[3], lut);
46 5009           dst[8] = '-';
47             /* bytes 4-5 */
48 5009           horus_hex_byte(dst + 9, uuid[4], lut);
49 5009           horus_hex_byte(dst + 11, uuid[5], lut);
50 5009           dst[13] = '-';
51             /* bytes 6-7 */
52 5009           horus_hex_byte(dst + 14, uuid[6], lut);
53 5009           horus_hex_byte(dst + 16, uuid[7], lut);
54 5009           dst[18] = '-';
55             /* bytes 8-9 */
56 5009           horus_hex_byte(dst + 19, uuid[8], lut);
57 5009           horus_hex_byte(dst + 21, uuid[9], lut);
58 5009           dst[23] = '-';
59             /* bytes 10-15 */
60 5009           horus_hex_byte(dst + 24, uuid[10], lut);
61 5009           horus_hex_byte(dst + 26, uuid[11], lut);
62 5009           horus_hex_byte(dst + 28, uuid[12], lut);
63 5009           horus_hex_byte(dst + 30, uuid[13], lut);
64 5009           horus_hex_byte(dst + 32, uuid[14], lut);
65 5009           horus_hex_byte(dst + 34, uuid[15], lut);
66 5009           }
67              
68             /* ── Master format dispatch ─────────────────────────────────────── */
69              
70             /* Returns the output length for a given format (excluding NUL) */
71 5126           static inline int horus_format_length(horus_format_t fmt) {
72 5126           switch (fmt) {
73 4969           case HORUS_FMT_STR: return HORUS_FMT_STR_LEN;
74 76           case HORUS_FMT_HEX: return HORUS_FMT_HEX_LEN;
75 13           case HORUS_FMT_BRACES: return HORUS_FMT_BRACES_LEN;
76 13           case HORUS_FMT_URN: return HORUS_FMT_URN_LEN;
77 13           case HORUS_FMT_BASE64: return HORUS_FMT_BASE64_LEN;
78 1           case HORUS_FMT_BASE32: return HORUS_FMT_BASE32_LEN;
79 1           case HORUS_FMT_CROCKFORD: return HORUS_FMT_CROCKFORD_LEN;
80 14           case HORUS_FMT_BINARY: return HORUS_FMT_BINARY_LEN;
81 14           case HORUS_FMT_UPPER_STR: return HORUS_FMT_STR_LEN;
82 12           case HORUS_FMT_UPPER_HEX: return HORUS_FMT_HEX_LEN;
83 0           default: return HORUS_FMT_STR_LEN;
84             }
85             }
86              
87             /* Format a 16-byte UUID into the given buffer. Returns bytes written. */
88 5126           static inline int horus_format_uuid(char *dst, const unsigned char *uuid,
89             horus_format_t fmt) {
90 5126           switch (fmt) {
91 4969           case HORUS_FMT_STR:
92 4969           horus_format_hyphenated(dst, uuid, horus_hex_lut);
93 4969           return HORUS_FMT_STR_LEN;
94              
95 14           case HORUS_FMT_UPPER_STR:
96 14           horus_format_hyphenated(dst, uuid, horus_hex_lut_upper);
97 14           return HORUS_FMT_STR_LEN;
98              
99 76           case HORUS_FMT_HEX:
100 76           horus_hex_encode(dst, uuid, horus_hex_lut);
101 76           return HORUS_FMT_HEX_LEN;
102              
103 12           case HORUS_FMT_UPPER_HEX:
104 12           horus_hex_encode(dst, uuid, horus_hex_lut_upper);
105 12           return HORUS_FMT_HEX_LEN;
106              
107 13           case HORUS_FMT_BRACES:
108 13           dst[0] = '{';
109 13           horus_format_hyphenated(dst + 1, uuid, horus_hex_lut);
110 13           dst[37] = '}';
111 13           return HORUS_FMT_BRACES_LEN;
112              
113 13           case HORUS_FMT_URN:
114 13           memcpy(dst, "urn:uuid:", 9);
115 13           horus_format_hyphenated(dst + 9, uuid, horus_hex_lut);
116 13           return HORUS_FMT_URN_LEN;
117              
118 13           case HORUS_FMT_BASE64:
119 13           horus_base64_encode(dst, uuid);
120 13           return HORUS_FMT_BASE64_LEN;
121              
122 1           case HORUS_FMT_BASE32:
123 1           horus_base32_encode(dst, uuid);
124 1           return HORUS_FMT_BASE32_LEN;
125              
126 1           case HORUS_FMT_CROCKFORD:
127 1           horus_crockford_encode(dst, uuid);
128 1           return HORUS_FMT_CROCKFORD_LEN;
129              
130 14           case HORUS_FMT_BINARY:
131 14           memcpy(dst, uuid, 16);
132 14           return HORUS_FMT_BINARY_LEN;
133              
134 0           default:
135 0           horus_format_hyphenated(dst, uuid, horus_hex_lut);
136 0           return HORUS_FMT_STR_LEN;
137             }
138             }
139              
140             #endif /* HORUS_FORMAT_H */