1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (C) 1996, 1997 Theodore Ts'o.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, and the entire permission notice in its entirety,
9 * including the disclaimer of warranties.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote
14 * products derived from this software without specific prior
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
20 * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
23 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
27 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
39 /* UUID packed form */
43 uint16_t time_hi_and_version;
48 static void uuid_pack(const struct uuid *uu, rte_uuid_t ptr)
54 out[3] = (uint8_t) tmp;
56 out[2] = (uint8_t) tmp;
58 out[1] = (uint8_t) tmp;
60 out[0] = (uint8_t) tmp;
63 out[5] = (uint8_t) tmp;
65 out[4] = (uint8_t) tmp;
67 tmp = uu->time_hi_and_version;
68 out[7] = (uint8_t) tmp;
70 out[6] = (uint8_t) tmp;
73 out[9] = (uint8_t) tmp;
75 out[8] = (uint8_t) tmp;
77 memcpy(out+10, uu->node, 6);
80 static void uuid_unpack(const rte_uuid_t in, struct uuid *uu)
82 const uint8_t *ptr = in;
86 tmp = (tmp << 8) | *ptr++;
87 tmp = (tmp << 8) | *ptr++;
88 tmp = (tmp << 8) | *ptr++;
92 tmp = (tmp << 8) | *ptr++;
96 tmp = (tmp << 8) | *ptr++;
97 uu->time_hi_and_version = tmp;
100 tmp = (tmp << 8) | *ptr++;
103 memcpy(uu->node, ptr, 6);
106 bool rte_uuid_is_null(const rte_uuid_t uu)
108 const uint8_t *cp = uu;
111 for (i = 0; i < 16; i++)
118 * rte_uuid_compare() - compare two UUIDs.
120 int rte_uuid_compare(const rte_uuid_t uu1, const rte_uuid_t uu2)
122 struct uuid uuid1, uuid2;
124 uuid_unpack(uu1, &uuid1);
125 uuid_unpack(uu2, &uuid2);
127 #define UUCMP(u1, u2) \
128 do { if (u1 != u2) return (u1 < u2) ? -1 : 1; } while (0)
130 UUCMP(uuid1.time_low, uuid2.time_low);
131 UUCMP(uuid1.time_mid, uuid2.time_mid);
132 UUCMP(uuid1.time_hi_and_version, uuid2.time_hi_and_version);
133 UUCMP(uuid1.clock_seq, uuid2.clock_seq);
136 return memcmp(uuid1.node, uuid2.node, 6);
139 int rte_uuid_parse(const char *in, rte_uuid_t uu)
146 if (strlen(in) != 36)
149 for (i = 0, cp = in; i <= 36; i++, cp++) {
150 if ((i == 8) || (i == 13) || (i == 18) ||
164 uuid.time_low = strtoul(in, NULL, 16);
165 uuid.time_mid = strtoul(in+9, NULL, 16);
166 uuid.time_hi_and_version = strtoul(in+14, NULL, 16);
167 uuid.clock_seq = strtoul(in+19, NULL, 16);
171 for (i = 0; i < 6; i++) {
174 uuid.node[i] = strtoul(buf, NULL, 16);
177 uuid_pack(&uuid, uu);
181 void rte_uuid_unparse(const rte_uuid_t uu, char *out, size_t len)
185 uuid_unpack(uu, &uuid);
188 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
189 uuid.time_low, uuid.time_mid, uuid.time_hi_and_version,
190 uuid.clock_seq >> 8, uuid.clock_seq & 0xFF,
191 uuid.node[0], uuid.node[1], uuid.node[2],
192 uuid.node[3], uuid.node[4], uuid.node[5]);