New upstream version 18.08
[deb_dpdk.git] / lib / librte_eal / common / eal_common_uuid.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (C) 1996, 1997 Theodore Ts'o.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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
15  *    written permission.
16  *
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
28  * DAMAGE.
29  */
30
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdint.h>
34 #include <stdlib.h>
35 #include <ctype.h>
36
37 #include <rte_uuid.h>
38
39 /* UUID packed form */
40 struct uuid {
41         uint32_t        time_low;
42         uint16_t        time_mid;
43         uint16_t        time_hi_and_version;
44         uint16_t        clock_seq;
45         uint8_t         node[6];
46 };
47
48 static void uuid_pack(const struct uuid *uu, rte_uuid_t ptr)
49 {
50         uint32_t tmp;
51         uint8_t *out = ptr;
52
53         tmp = uu->time_low;
54         out[3] = (uint8_t) tmp;
55         tmp >>= 8;
56         out[2] = (uint8_t) tmp;
57         tmp >>= 8;
58         out[1] = (uint8_t) tmp;
59         tmp >>= 8;
60         out[0] = (uint8_t) tmp;
61
62         tmp = uu->time_mid;
63         out[5] = (uint8_t) tmp;
64         tmp >>= 8;
65         out[4] = (uint8_t) tmp;
66
67         tmp = uu->time_hi_and_version;
68         out[7] = (uint8_t) tmp;
69         tmp >>= 8;
70         out[6] = (uint8_t) tmp;
71
72         tmp = uu->clock_seq;
73         out[9] = (uint8_t) tmp;
74         tmp >>= 8;
75         out[8] = (uint8_t) tmp;
76
77         memcpy(out+10, uu->node, 6);
78 }
79
80 static void uuid_unpack(const rte_uuid_t in, struct uuid *uu)
81 {
82         const uint8_t *ptr = in;
83         uint32_t tmp;
84
85         tmp = *ptr++;
86         tmp = (tmp << 8) | *ptr++;
87         tmp = (tmp << 8) | *ptr++;
88         tmp = (tmp << 8) | *ptr++;
89         uu->time_low = tmp;
90
91         tmp = *ptr++;
92         tmp = (tmp << 8) | *ptr++;
93         uu->time_mid = tmp;
94
95         tmp = *ptr++;
96         tmp = (tmp << 8) | *ptr++;
97         uu->time_hi_and_version = tmp;
98
99         tmp = *ptr++;
100         tmp = (tmp << 8) | *ptr++;
101         uu->clock_seq = tmp;
102
103         memcpy(uu->node, ptr, 6);
104 }
105
106 bool rte_uuid_is_null(const rte_uuid_t uu)
107 {
108         const uint8_t *cp = uu;
109         int i;
110
111         for (i = 0; i < 16; i++)
112                 if (*cp++)
113                         return false;
114         return true;
115 }
116
117 /*
118  * rte_uuid_compare() - compare two UUIDs.
119  */
120 int rte_uuid_compare(const rte_uuid_t uu1, const rte_uuid_t uu2)
121 {
122         struct uuid     uuid1, uuid2;
123
124         uuid_unpack(uu1, &uuid1);
125         uuid_unpack(uu2, &uuid2);
126
127 #define UUCMP(u1, u2) \
128         do { if (u1 != u2) return (u1 < u2) ? -1 : 1; } while (0)
129
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);
134 #undef UUCMP
135
136         return memcmp(uuid1.node, uuid2.node, 6);
137 }
138
139 int rte_uuid_parse(const char *in, rte_uuid_t uu)
140 {
141         struct uuid     uuid;
142         int             i;
143         const char      *cp;
144         char            buf[3];
145
146         if (strlen(in) != 36)
147                 return -1;
148
149         for (i = 0, cp = in; i <= 36; i++, cp++) {
150                 if ((i == 8) || (i == 13) || (i == 18) ||
151                     (i == 23)) {
152                         if (*cp == '-')
153                                 continue;
154                         else
155                                 return -1;
156                 }
157                 if (i == 36)
158                         if (*cp == 0)
159                                 continue;
160                 if (!isxdigit(*cp))
161                         return -1;
162         }
163
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);
168         cp = in+24;
169         buf[2] = 0;
170
171         for (i = 0; i < 6; i++) {
172                 buf[0] = *cp++;
173                 buf[1] = *cp++;
174                 uuid.node[i] = strtoul(buf, NULL, 16);
175         }
176
177         uuid_pack(&uuid, uu);
178         return 0;
179 }
180
181 void rte_uuid_unparse(const rte_uuid_t uu, char *out, size_t len)
182 {
183         struct uuid uuid;
184
185         uuid_unpack(uu, &uuid);
186
187         snprintf(out, len,
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]);
193 }