1*99a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
2*99a2dd95SBruce Richardson * Copyright (C) 1996, 1997 Theodore Ts'o.
3*99a2dd95SBruce Richardson */
4*99a2dd95SBruce Richardson
5*99a2dd95SBruce Richardson #include <stdio.h>
6*99a2dd95SBruce Richardson #include <string.h>
7*99a2dd95SBruce Richardson #include <stdint.h>
8*99a2dd95SBruce Richardson #include <stdlib.h>
9*99a2dd95SBruce Richardson #include <ctype.h>
10*99a2dd95SBruce Richardson
11*99a2dd95SBruce Richardson #include <rte_uuid.h>
12*99a2dd95SBruce Richardson
13*99a2dd95SBruce Richardson /* UUID packed form */
14*99a2dd95SBruce Richardson struct uuid {
15*99a2dd95SBruce Richardson uint32_t time_low;
16*99a2dd95SBruce Richardson uint16_t time_mid;
17*99a2dd95SBruce Richardson uint16_t time_hi_and_version;
18*99a2dd95SBruce Richardson uint16_t clock_seq;
19*99a2dd95SBruce Richardson uint8_t node[6];
20*99a2dd95SBruce Richardson };
21*99a2dd95SBruce Richardson
uuid_pack(const struct uuid * uu,rte_uuid_t ptr)22*99a2dd95SBruce Richardson static void uuid_pack(const struct uuid *uu, rte_uuid_t ptr)
23*99a2dd95SBruce Richardson {
24*99a2dd95SBruce Richardson uint32_t tmp;
25*99a2dd95SBruce Richardson uint8_t *out = ptr;
26*99a2dd95SBruce Richardson
27*99a2dd95SBruce Richardson tmp = uu->time_low;
28*99a2dd95SBruce Richardson out[3] = (uint8_t) tmp;
29*99a2dd95SBruce Richardson tmp >>= 8;
30*99a2dd95SBruce Richardson out[2] = (uint8_t) tmp;
31*99a2dd95SBruce Richardson tmp >>= 8;
32*99a2dd95SBruce Richardson out[1] = (uint8_t) tmp;
33*99a2dd95SBruce Richardson tmp >>= 8;
34*99a2dd95SBruce Richardson out[0] = (uint8_t) tmp;
35*99a2dd95SBruce Richardson
36*99a2dd95SBruce Richardson tmp = uu->time_mid;
37*99a2dd95SBruce Richardson out[5] = (uint8_t) tmp;
38*99a2dd95SBruce Richardson tmp >>= 8;
39*99a2dd95SBruce Richardson out[4] = (uint8_t) tmp;
40*99a2dd95SBruce Richardson
41*99a2dd95SBruce Richardson tmp = uu->time_hi_and_version;
42*99a2dd95SBruce Richardson out[7] = (uint8_t) tmp;
43*99a2dd95SBruce Richardson tmp >>= 8;
44*99a2dd95SBruce Richardson out[6] = (uint8_t) tmp;
45*99a2dd95SBruce Richardson
46*99a2dd95SBruce Richardson tmp = uu->clock_seq;
47*99a2dd95SBruce Richardson out[9] = (uint8_t) tmp;
48*99a2dd95SBruce Richardson tmp >>= 8;
49*99a2dd95SBruce Richardson out[8] = (uint8_t) tmp;
50*99a2dd95SBruce Richardson
51*99a2dd95SBruce Richardson memcpy(out+10, uu->node, 6);
52*99a2dd95SBruce Richardson }
53*99a2dd95SBruce Richardson
uuid_unpack(const rte_uuid_t in,struct uuid * uu)54*99a2dd95SBruce Richardson static void uuid_unpack(const rte_uuid_t in, struct uuid *uu)
55*99a2dd95SBruce Richardson {
56*99a2dd95SBruce Richardson const uint8_t *ptr = in;
57*99a2dd95SBruce Richardson uint32_t tmp;
58*99a2dd95SBruce Richardson
59*99a2dd95SBruce Richardson tmp = *ptr++;
60*99a2dd95SBruce Richardson tmp = (tmp << 8) | *ptr++;
61*99a2dd95SBruce Richardson tmp = (tmp << 8) | *ptr++;
62*99a2dd95SBruce Richardson tmp = (tmp << 8) | *ptr++;
63*99a2dd95SBruce Richardson uu->time_low = tmp;
64*99a2dd95SBruce Richardson
65*99a2dd95SBruce Richardson tmp = *ptr++;
66*99a2dd95SBruce Richardson tmp = (tmp << 8) | *ptr++;
67*99a2dd95SBruce Richardson uu->time_mid = tmp;
68*99a2dd95SBruce Richardson
69*99a2dd95SBruce Richardson tmp = *ptr++;
70*99a2dd95SBruce Richardson tmp = (tmp << 8) | *ptr++;
71*99a2dd95SBruce Richardson uu->time_hi_and_version = tmp;
72*99a2dd95SBruce Richardson
73*99a2dd95SBruce Richardson tmp = *ptr++;
74*99a2dd95SBruce Richardson tmp = (tmp << 8) | *ptr++;
75*99a2dd95SBruce Richardson uu->clock_seq = tmp;
76*99a2dd95SBruce Richardson
77*99a2dd95SBruce Richardson memcpy(uu->node, ptr, 6);
78*99a2dd95SBruce Richardson }
79*99a2dd95SBruce Richardson
rte_uuid_is_null(const rte_uuid_t uu)80*99a2dd95SBruce Richardson bool rte_uuid_is_null(const rte_uuid_t uu)
81*99a2dd95SBruce Richardson {
82*99a2dd95SBruce Richardson const uint8_t *cp = uu;
83*99a2dd95SBruce Richardson int i;
84*99a2dd95SBruce Richardson
85*99a2dd95SBruce Richardson for (i = 0; i < 16; i++)
86*99a2dd95SBruce Richardson if (*cp++)
87*99a2dd95SBruce Richardson return false;
88*99a2dd95SBruce Richardson return true;
89*99a2dd95SBruce Richardson }
90*99a2dd95SBruce Richardson
91*99a2dd95SBruce Richardson /*
92*99a2dd95SBruce Richardson * rte_uuid_compare() - compare two UUIDs.
93*99a2dd95SBruce Richardson */
rte_uuid_compare(const rte_uuid_t uu1,const rte_uuid_t uu2)94*99a2dd95SBruce Richardson int rte_uuid_compare(const rte_uuid_t uu1, const rte_uuid_t uu2)
95*99a2dd95SBruce Richardson {
96*99a2dd95SBruce Richardson struct uuid uuid1, uuid2;
97*99a2dd95SBruce Richardson
98*99a2dd95SBruce Richardson uuid_unpack(uu1, &uuid1);
99*99a2dd95SBruce Richardson uuid_unpack(uu2, &uuid2);
100*99a2dd95SBruce Richardson
101*99a2dd95SBruce Richardson #define UUCMP(u1, u2) \
102*99a2dd95SBruce Richardson do { if (u1 != u2) return (u1 < u2) ? -1 : 1; } while (0)
103*99a2dd95SBruce Richardson
104*99a2dd95SBruce Richardson UUCMP(uuid1.time_low, uuid2.time_low);
105*99a2dd95SBruce Richardson UUCMP(uuid1.time_mid, uuid2.time_mid);
106*99a2dd95SBruce Richardson UUCMP(uuid1.time_hi_and_version, uuid2.time_hi_and_version);
107*99a2dd95SBruce Richardson UUCMP(uuid1.clock_seq, uuid2.clock_seq);
108*99a2dd95SBruce Richardson #undef UUCMP
109*99a2dd95SBruce Richardson
110*99a2dd95SBruce Richardson return memcmp(uuid1.node, uuid2.node, 6);
111*99a2dd95SBruce Richardson }
112*99a2dd95SBruce Richardson
rte_uuid_parse(const char * in,rte_uuid_t uu)113*99a2dd95SBruce Richardson int rte_uuid_parse(const char *in, rte_uuid_t uu)
114*99a2dd95SBruce Richardson {
115*99a2dd95SBruce Richardson struct uuid uuid;
116*99a2dd95SBruce Richardson int i;
117*99a2dd95SBruce Richardson const char *cp;
118*99a2dd95SBruce Richardson char buf[3];
119*99a2dd95SBruce Richardson
120*99a2dd95SBruce Richardson if (strlen(in) != 36)
121*99a2dd95SBruce Richardson return -1;
122*99a2dd95SBruce Richardson
123*99a2dd95SBruce Richardson for (i = 0, cp = in; i <= 36; i++, cp++) {
124*99a2dd95SBruce Richardson if ((i == 8) || (i == 13) || (i == 18) ||
125*99a2dd95SBruce Richardson (i == 23)) {
126*99a2dd95SBruce Richardson if (*cp == '-')
127*99a2dd95SBruce Richardson continue;
128*99a2dd95SBruce Richardson else
129*99a2dd95SBruce Richardson return -1;
130*99a2dd95SBruce Richardson }
131*99a2dd95SBruce Richardson if (i == 36)
132*99a2dd95SBruce Richardson if (*cp == 0)
133*99a2dd95SBruce Richardson continue;
134*99a2dd95SBruce Richardson if (!isxdigit(*cp))
135*99a2dd95SBruce Richardson return -1;
136*99a2dd95SBruce Richardson }
137*99a2dd95SBruce Richardson
138*99a2dd95SBruce Richardson uuid.time_low = strtoul(in, NULL, 16);
139*99a2dd95SBruce Richardson uuid.time_mid = strtoul(in+9, NULL, 16);
140*99a2dd95SBruce Richardson uuid.time_hi_and_version = strtoul(in+14, NULL, 16);
141*99a2dd95SBruce Richardson uuid.clock_seq = strtoul(in+19, NULL, 16);
142*99a2dd95SBruce Richardson cp = in+24;
143*99a2dd95SBruce Richardson buf[2] = 0;
144*99a2dd95SBruce Richardson
145*99a2dd95SBruce Richardson for (i = 0; i < 6; i++) {
146*99a2dd95SBruce Richardson buf[0] = *cp++;
147*99a2dd95SBruce Richardson buf[1] = *cp++;
148*99a2dd95SBruce Richardson uuid.node[i] = strtoul(buf, NULL, 16);
149*99a2dd95SBruce Richardson }
150*99a2dd95SBruce Richardson
151*99a2dd95SBruce Richardson uuid_pack(&uuid, uu);
152*99a2dd95SBruce Richardson return 0;
153*99a2dd95SBruce Richardson }
154*99a2dd95SBruce Richardson
rte_uuid_unparse(const rte_uuid_t uu,char * out,size_t len)155*99a2dd95SBruce Richardson void rte_uuid_unparse(const rte_uuid_t uu, char *out, size_t len)
156*99a2dd95SBruce Richardson {
157*99a2dd95SBruce Richardson struct uuid uuid;
158*99a2dd95SBruce Richardson
159*99a2dd95SBruce Richardson uuid_unpack(uu, &uuid);
160*99a2dd95SBruce Richardson
161*99a2dd95SBruce Richardson snprintf(out, len,
162*99a2dd95SBruce Richardson "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
163*99a2dd95SBruce Richardson uuid.time_low, uuid.time_mid, uuid.time_hi_and_version,
164*99a2dd95SBruce Richardson uuid.clock_seq >> 8, uuid.clock_seq & 0xFF,
165*99a2dd95SBruce Richardson uuid.node[0], uuid.node[1], uuid.node[2],
166*99a2dd95SBruce Richardson uuid.node[3], uuid.node[4], uuid.node[5]);
167*99a2dd95SBruce Richardson }
168