1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (C) 1996, 1997 Theodore Ts'o.
13 /* UUID packed form */
17 uint16_t time_hi_and_version;
22 static void uuid_pack(const struct uuid *uu, rte_uuid_t ptr)
28 out[3] = (uint8_t) tmp;
30 out[2] = (uint8_t) tmp;
32 out[1] = (uint8_t) tmp;
34 out[0] = (uint8_t) tmp;
37 out[5] = (uint8_t) tmp;
39 out[4] = (uint8_t) tmp;
41 tmp = uu->time_hi_and_version;
42 out[7] = (uint8_t) tmp;
44 out[6] = (uint8_t) tmp;
47 out[9] = (uint8_t) tmp;
49 out[8] = (uint8_t) tmp;
51 memcpy(out+10, uu->node, 6);
54 static void uuid_unpack(const rte_uuid_t in, struct uuid *uu)
56 const uint8_t *ptr = in;
60 tmp = (tmp << 8) | *ptr++;
61 tmp = (tmp << 8) | *ptr++;
62 tmp = (tmp << 8) | *ptr++;
66 tmp = (tmp << 8) | *ptr++;
70 tmp = (tmp << 8) | *ptr++;
71 uu->time_hi_and_version = tmp;
74 tmp = (tmp << 8) | *ptr++;
77 memcpy(uu->node, ptr, 6);
80 bool rte_uuid_is_null(const rte_uuid_t uu)
82 const uint8_t *cp = uu;
85 for (i = 0; i < 16; i++)
92 * rte_uuid_compare() - compare two UUIDs.
94 int rte_uuid_compare(const rte_uuid_t uu1, const rte_uuid_t uu2)
96 struct uuid uuid1, uuid2;
98 uuid_unpack(uu1, &uuid1);
99 uuid_unpack(uu2, &uuid2);
101 #define UUCMP(u1, u2) \
102 do { if (u1 != u2) return (u1 < u2) ? -1 : 1; } while (0)
104 UUCMP(uuid1.time_low, uuid2.time_low);
105 UUCMP(uuid1.time_mid, uuid2.time_mid);
106 UUCMP(uuid1.time_hi_and_version, uuid2.time_hi_and_version);
107 UUCMP(uuid1.clock_seq, uuid2.clock_seq);
110 return memcmp(uuid1.node, uuid2.node, 6);
113 int rte_uuid_parse(const char *in, rte_uuid_t uu)
120 if (strlen(in) != 36)
123 for (i = 0, cp = in; i <= 36; i++, cp++) {
124 if ((i == 8) || (i == 13) || (i == 18) ||
138 uuid.time_low = strtoul(in, NULL, 16);
139 uuid.time_mid = strtoul(in+9, NULL, 16);
140 uuid.time_hi_and_version = strtoul(in+14, NULL, 16);
141 uuid.clock_seq = strtoul(in+19, NULL, 16);
145 for (i = 0; i < 6; i++) {
148 uuid.node[i] = strtoul(buf, NULL, 16);
151 uuid_pack(&uuid, uu);
155 void rte_uuid_unparse(const rte_uuid_t uu, char *out, size_t len)
159 uuid_unpack(uu, &uuid);
162 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
163 uuid.time_low, uuid.time_mid, uuid.time_hi_and_version,
164 uuid.clock_seq >> 8, uuid.clock_seq & 0xFF,
165 uuid.node[0], uuid.node[1], uuid.node[2],
166 uuid.node[3], uuid.node[4], uuid.node[5]);