File Coverage

/usr/local/lib/perl5/site_perl/5.42.0/x86_64-linux/Horus/include/horus_format.h
Criterion Covered Total %
statement 27 60 45.0
branch n/a
condition n/a
subroutine n/a
pod n/a
total 27 60 45.0


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 61           static inline void horus_format_hyphenated(char *dst, const unsigned char *uuid,
40             const uint16_t *lut) {
41             /* bytes 0-3 */
42 61           horus_hex_byte(dst + 0, uuid[0], lut);
43 61           horus_hex_byte(dst + 2, uuid[1], lut);
44 61           horus_hex_byte(dst + 4, uuid[2], lut);
45 61           horus_hex_byte(dst + 6, uuid[3], lut);
46 61           dst[8] = '-';
47             /* bytes 4-5 */
48 61           horus_hex_byte(dst + 9, uuid[4], lut);
49 61           horus_hex_byte(dst + 11, uuid[5], lut);
50 61           dst[13] = '-';
51             /* bytes 6-7 */
52 61           horus_hex_byte(dst + 14, uuid[6], lut);
53 61           horus_hex_byte(dst + 16, uuid[7], lut);
54 61           dst[18] = '-';
55             /* bytes 8-9 */
56 61           horus_hex_byte(dst + 19, uuid[8], lut);
57 61           horus_hex_byte(dst + 21, uuid[9], lut);
58 61           dst[23] = '-';
59             /* bytes 10-15 */
60 61           horus_hex_byte(dst + 24, uuid[10], lut);
61 61           horus_hex_byte(dst + 26, uuid[11], lut);
62 61           horus_hex_byte(dst + 28, uuid[12], lut);
63 61           horus_hex_byte(dst + 30, uuid[13], lut);
64 61           horus_hex_byte(dst + 32, uuid[14], lut);
65 61           horus_hex_byte(dst + 34, uuid[15], lut);
66 61           }
67              
68             /* ── Master format dispatch ─────────────────────────────────────── */
69              
70             /* Returns the output length for a given format (excluding NUL) */
71             static inline int horus_format_length(horus_format_t fmt) {
72             switch (fmt) {
73             case HORUS_FMT_STR: return HORUS_FMT_STR_LEN;
74             case HORUS_FMT_HEX: return HORUS_FMT_HEX_LEN;
75             case HORUS_FMT_BRACES: return HORUS_FMT_BRACES_LEN;
76             case HORUS_FMT_URN: return HORUS_FMT_URN_LEN;
77             case HORUS_FMT_BASE64: return HORUS_FMT_BASE64_LEN;
78             case HORUS_FMT_BASE32: return HORUS_FMT_BASE32_LEN;
79             case HORUS_FMT_CROCKFORD: return HORUS_FMT_CROCKFORD_LEN;
80             case HORUS_FMT_BINARY: return HORUS_FMT_BINARY_LEN;
81             case HORUS_FMT_UPPER_STR: return HORUS_FMT_STR_LEN;
82             case HORUS_FMT_UPPER_HEX: return HORUS_FMT_HEX_LEN;
83             default: return HORUS_FMT_STR_LEN;
84             }
85             }
86              
87             /* Format a 16-byte UUID into the given buffer. Returns bytes written. */
88 61           static inline int horus_format_uuid(char *dst, const unsigned char *uuid,
89             horus_format_t fmt) {
90 61           switch (fmt) {
91 61           case HORUS_FMT_STR:
92 61           horus_format_hyphenated(dst, uuid, horus_hex_lut);
93 61           return HORUS_FMT_STR_LEN;
94              
95 0           case HORUS_FMT_UPPER_STR:
96 0           horus_format_hyphenated(dst, uuid, horus_hex_lut_upper);
97 0           return HORUS_FMT_STR_LEN;
98              
99 0           case HORUS_FMT_HEX:
100 0           horus_hex_encode(dst, uuid, horus_hex_lut);
101 0           return HORUS_FMT_HEX_LEN;
102              
103 0           case HORUS_FMT_UPPER_HEX:
104 0           horus_hex_encode(dst, uuid, horus_hex_lut_upper);
105 0           return HORUS_FMT_HEX_LEN;
106              
107 0           case HORUS_FMT_BRACES:
108 0           dst[0] = '{';
109 0           horus_format_hyphenated(dst + 1, uuid, horus_hex_lut);
110 0           dst[37] = '}';
111 0           return HORUS_FMT_BRACES_LEN;
112              
113 0           case HORUS_FMT_URN:
114 0           memcpy(dst, "urn:uuid:", 9);
115 0           horus_format_hyphenated(dst + 9, uuid, horus_hex_lut);
116 0           return HORUS_FMT_URN_LEN;
117              
118 0           case HORUS_FMT_BASE64:
119 0           horus_base64_encode(dst, uuid);
120 0           return HORUS_FMT_BASE64_LEN;
121              
122 0           case HORUS_FMT_BASE32:
123 0           horus_base32_encode(dst, uuid);
124 0           return HORUS_FMT_BASE32_LEN;
125              
126 0           case HORUS_FMT_CROCKFORD:
127 0           horus_crockford_encode(dst, uuid);
128 0           return HORUS_FMT_CROCKFORD_LEN;
129              
130 0           case HORUS_FMT_BINARY:
131 0           memcpy(dst, uuid, 16);
132 0           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 */