1 | /*
|
---|
2 | * Copyright (c) 2015 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup libc
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file Universaly Unique Identifier (see RFC 4122)
|
---|
33 | */
|
---|
34 |
|
---|
35 | #include <errno.h>
|
---|
36 | #include <uuid.h>
|
---|
37 | #include <rndgen.h>
|
---|
38 | #include <stdlib.h>
|
---|
39 | #include <stddef.h>
|
---|
40 | #include <str.h>
|
---|
41 | #include <stdio.h>
|
---|
42 |
|
---|
43 | static void encode16_be(uint8_t *, uint16_t);
|
---|
44 | static void encode16_le(uint8_t *, uint16_t);
|
---|
45 | static void encode32_be(uint8_t *, uint32_t);
|
---|
46 | static void encode32_le(uint8_t *, uint32_t);
|
---|
47 | static uint16_t decode16_be(uint8_t *);
|
---|
48 | static uint16_t decode16_le(uint8_t *);
|
---|
49 | static uint32_t decode32_be(uint8_t *);
|
---|
50 | static uint32_t decode32_le(uint8_t *);
|
---|
51 |
|
---|
52 | /** Generate UUID.
|
---|
53 | *
|
---|
54 | * @param uuid Place to store generated UUID
|
---|
55 | * @return EOK on success or an error code
|
---|
56 | */
|
---|
57 | errno_t uuid_generate(uuid_t *uuid)
|
---|
58 | {
|
---|
59 | int i;
|
---|
60 | rndgen_t *rndgen;
|
---|
61 | errno_t rc;
|
---|
62 |
|
---|
63 | rc = rndgen_create(&rndgen);
|
---|
64 | if (rc != EOK)
|
---|
65 | return EIO;
|
---|
66 |
|
---|
67 | rc = rndgen_uint32(rndgen, &uuid->time_low);
|
---|
68 | if (rc != EOK) {
|
---|
69 | rc = EIO;
|
---|
70 | goto error;
|
---|
71 | }
|
---|
72 |
|
---|
73 | rc = rndgen_uint16(rndgen, &uuid->time_mid);
|
---|
74 | if (rc != EOK) {
|
---|
75 | rc = EIO;
|
---|
76 | goto error;
|
---|
77 | }
|
---|
78 |
|
---|
79 | rc = rndgen_uint16(rndgen, &uuid->time_hi_and_version);
|
---|
80 | if (rc != EOK) {
|
---|
81 | rc = EIO;
|
---|
82 | goto error;
|
---|
83 | }
|
---|
84 |
|
---|
85 | rc = rndgen_uint8(rndgen, &uuid->clock_seq_hi_and_reserved);
|
---|
86 | if (rc != EOK) {
|
---|
87 | rc = EIO;
|
---|
88 | goto error;
|
---|
89 | }
|
---|
90 |
|
---|
91 | rc = rndgen_uint8(rndgen, &uuid->clock_seq_low);
|
---|
92 | if (rc != EOK) {
|
---|
93 | rc = EIO;
|
---|
94 | goto error;
|
---|
95 | }
|
---|
96 |
|
---|
97 | for (i = 0; i < _UUID_NODE_LEN; i++) {
|
---|
98 | rc = rndgen_uint8(rndgen, &uuid->node[i]);
|
---|
99 | if (rc != EOK) {
|
---|
100 | rc = EIO;
|
---|
101 | goto error;
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | /* Version 4 UUID from random or pseudo-random numbers */
|
---|
106 | uuid->time_hi_and_version = (uuid->time_hi_and_version & 0x0fff) | 0x4000;
|
---|
107 | uuid->clock_seq_hi_and_reserved = (uuid->clock_seq_hi_and_reserved & 0x3f) | 0x80;
|
---|
108 |
|
---|
109 | error:
|
---|
110 | rndgen_destroy(rndgen);
|
---|
111 | return rc;
|
---|
112 | }
|
---|
113 |
|
---|
114 |
|
---|
115 | /** Encode UUID into binary form per RFC 4122.
|
---|
116 | *
|
---|
117 | * @param uuid UUID
|
---|
118 | * @param buf 16-byte destination buffer
|
---|
119 | */
|
---|
120 | void uuid_encode(uuid_t *uuid, uint8_t *buf)
|
---|
121 | {
|
---|
122 | int i;
|
---|
123 |
|
---|
124 | encode32_be(buf, uuid->time_low);
|
---|
125 | encode16_be(buf + 4, uuid->time_mid);
|
---|
126 | encode16_be(buf + 6, uuid->time_hi_and_version);
|
---|
127 | buf[8] = uuid->clock_seq_hi_and_reserved;
|
---|
128 | buf[9] = uuid->clock_seq_low;
|
---|
129 |
|
---|
130 | for (i = 0; i < _UUID_NODE_LEN; i++)
|
---|
131 | buf[10 + i] = uuid->node[i];
|
---|
132 | }
|
---|
133 |
|
---|
134 | /** Decode UUID from binary form per RFC 4122.
|
---|
135 | *
|
---|
136 | * @param buf 16-byte source buffer
|
---|
137 | * @param uuid Place to store UUID
|
---|
138 | */
|
---|
139 | void uuid_decode(uint8_t *buf, uuid_t *uuid)
|
---|
140 | {
|
---|
141 | int i;
|
---|
142 |
|
---|
143 | uuid->time_low = decode32_be(buf);
|
---|
144 | uuid->time_mid = decode16_be(buf + 4);
|
---|
145 | uuid->time_hi_and_version = decode16_be(buf + 6);
|
---|
146 | uuid->clock_seq_hi_and_reserved = buf[8];
|
---|
147 | uuid->clock_seq_low = buf[9];
|
---|
148 |
|
---|
149 | for (i = 0; i < _UUID_NODE_LEN; i++)
|
---|
150 | uuid->node[i] = buf[10 + i];
|
---|
151 | }
|
---|
152 |
|
---|
153 | /** Encode UUID into little-endian (GPT) binary form.
|
---|
154 | *
|
---|
155 | * @param uuid UUID
|
---|
156 | * @param buf 16-byte destination buffer
|
---|
157 | */
|
---|
158 | void uuid_encode_le(uuid_t *uuid, uint8_t *buf)
|
---|
159 | {
|
---|
160 | int i;
|
---|
161 |
|
---|
162 | encode32_le(buf, uuid->time_low);
|
---|
163 | encode16_le(buf + 4, uuid->time_mid);
|
---|
164 | encode16_le(buf + 6, uuid->time_hi_and_version);
|
---|
165 | buf[8] = uuid->clock_seq_hi_and_reserved;
|
---|
166 | buf[9] = uuid->clock_seq_low;
|
---|
167 |
|
---|
168 | for (i = 0; i < _UUID_NODE_LEN; i++)
|
---|
169 | buf[10 + i] = uuid->node[i];
|
---|
170 | }
|
---|
171 |
|
---|
172 | /** Decode UUID from little-endian (GPT) binary form.
|
---|
173 | *
|
---|
174 | * @param buf 16-byte source buffer
|
---|
175 | * @param uuid Place to store UUID
|
---|
176 | */
|
---|
177 | void uuid_decode_le(uint8_t *buf, uuid_t *uuid)
|
---|
178 | {
|
---|
179 | int i;
|
---|
180 |
|
---|
181 | uuid->time_low = decode32_le(buf);
|
---|
182 | uuid->time_mid = decode16_le(buf + 4);
|
---|
183 | uuid->time_hi_and_version = decode16_le(buf + 6);
|
---|
184 | uuid->clock_seq_hi_and_reserved = buf[8];
|
---|
185 | uuid->clock_seq_low = buf[9];
|
---|
186 |
|
---|
187 | for (i = 0; i < _UUID_NODE_LEN; i++)
|
---|
188 | uuid->node[i] = buf[10 + i];
|
---|
189 | }
|
---|
190 |
|
---|
191 | /** Parse string UUID.
|
---|
192 | *
|
---|
193 | * If @a endptr is not NULL, it is set to point to the first character
|
---|
194 | * following the UUID. If @a endptr is NULL, the string must not contain
|
---|
195 | * any characters following the UUID, otherwise an error is returned.
|
---|
196 | *
|
---|
197 | * @param str String beginning with UUID string representation
|
---|
198 | * @param uuid Place to store UUID
|
---|
199 | * @param endptr Place to store pointer to end of UUID or @c NULL
|
---|
200 | *
|
---|
201 | * @return EOK on success or an error code
|
---|
202 | */
|
---|
203 | errno_t uuid_parse(const char *str, uuid_t *uuid, const char **endptr)
|
---|
204 | {
|
---|
205 | errno_t rc;
|
---|
206 | const char *eptr;
|
---|
207 | uint32_t time_low;
|
---|
208 | uint16_t time_mid;
|
---|
209 | uint16_t time_ver;
|
---|
210 | uint16_t clock;
|
---|
211 | uint64_t node;
|
---|
212 | int i;
|
---|
213 |
|
---|
214 | rc = str_uint32_t(str, &eptr, 16, false, &time_low);
|
---|
215 | if (rc != EOK || eptr != str + 8 || *eptr != '-')
|
---|
216 | return EINVAL;
|
---|
217 |
|
---|
218 | rc = str_uint16_t(str + 9, &eptr, 16, false, &time_mid);
|
---|
219 | if (rc != EOK || eptr != str + 13 || *eptr != '-')
|
---|
220 | return EINVAL;
|
---|
221 |
|
---|
222 | rc = str_uint16_t(str + 14, &eptr, 16, false, &time_ver);
|
---|
223 | if (rc != EOK || eptr != str + 18 || *eptr != '-')
|
---|
224 | return EINVAL;
|
---|
225 |
|
---|
226 | rc = str_uint16_t(str + 19, &eptr, 16, false, &clock);
|
---|
227 | if (rc != EOK || eptr != str + 23 || *eptr != '-')
|
---|
228 | return EINVAL;
|
---|
229 |
|
---|
230 | rc = str_uint64_t(str + 24, &eptr, 16, false, &node);
|
---|
231 | if (rc != EOK || eptr != str + 36)
|
---|
232 | return EINVAL;
|
---|
233 |
|
---|
234 | uuid->time_low = time_low;
|
---|
235 |
|
---|
236 | uuid->time_mid = time_mid;
|
---|
237 |
|
---|
238 | uuid->time_hi_and_version = time_ver;
|
---|
239 |
|
---|
240 | uuid->clock_seq_hi_and_reserved = clock >> 8;
|
---|
241 |
|
---|
242 | uuid->clock_seq_low = clock & 0xff;
|
---|
243 |
|
---|
244 | for (i = 0; i < _UUID_NODE_LEN; i++)
|
---|
245 | uuid->node[i] = (node >> 8 * (5 - i)) & 0xff;
|
---|
246 |
|
---|
247 | if (endptr != NULL) {
|
---|
248 | *endptr = str + 36;
|
---|
249 | } else {
|
---|
250 | if (*(str + 36) != '\0')
|
---|
251 | return EINVAL;
|
---|
252 | }
|
---|
253 |
|
---|
254 | return EOK;
|
---|
255 | }
|
---|
256 |
|
---|
257 | /** Format UUID into string representation.
|
---|
258 | *
|
---|
259 | * @param uuid UUID
|
---|
260 | * @param rstr Place to store pointer to newly allocated string
|
---|
261 | *
|
---|
262 | * @return EOK on success, ENOMEM if out of memory
|
---|
263 | */
|
---|
264 | errno_t uuid_format(uuid_t *uuid, char **rstr, bool uppercase)
|
---|
265 | {
|
---|
266 | size_t size = 37;
|
---|
267 | char *str = malloc(sizeof(char) * size);
|
---|
268 | if (str == NULL)
|
---|
269 | return ENOMEM;
|
---|
270 |
|
---|
271 | const char *format = "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x";
|
---|
272 | if (uppercase)
|
---|
273 | format = "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X";
|
---|
274 |
|
---|
275 | int ret = snprintf(str, size, format, uuid->time_low, uuid->time_mid,
|
---|
276 | uuid->time_hi_and_version, uuid->clock_seq_hi_and_reserved,
|
---|
277 | uuid->clock_seq_low, uuid->node[0], uuid->node[1], uuid->node[2],
|
---|
278 | uuid->node[3], uuid->node[4], uuid->node[5]);
|
---|
279 |
|
---|
280 | if (ret != 36) {
|
---|
281 | free(str);
|
---|
282 | return EINVAL;
|
---|
283 | }
|
---|
284 |
|
---|
285 | *rstr = str;
|
---|
286 | return EOK;
|
---|
287 | }
|
---|
288 |
|
---|
289 | static void encode16_be(uint8_t *buf, uint16_t value)
|
---|
290 | {
|
---|
291 | buf[0] = (value >> 8) & 0xff;
|
---|
292 | buf[1] = value & 0xff;
|
---|
293 | }
|
---|
294 |
|
---|
295 | static void encode16_le(uint8_t *buf, uint16_t value)
|
---|
296 | {
|
---|
297 | buf[0] = value & 0xff;
|
---|
298 | buf[1] = (value >> 8) & 0xff;
|
---|
299 | }
|
---|
300 |
|
---|
301 | static void encode32_be(uint8_t *buf, uint32_t value)
|
---|
302 | {
|
---|
303 | buf[0] = (value >> 24) & 0xff;
|
---|
304 | buf[1] = (value >> 16) & 0xff;
|
---|
305 | buf[2] = (value >> 8) & 0xff;
|
---|
306 | buf[3] = value & 0xff;
|
---|
307 | }
|
---|
308 |
|
---|
309 | static void encode32_le(uint8_t *buf, uint32_t value)
|
---|
310 | {
|
---|
311 | buf[0] = value & 0xff;
|
---|
312 | buf[1] = (value >> 8) & 0xff;
|
---|
313 | buf[2] = (value >> 16) & 0xff;
|
---|
314 | buf[3] = (value >> 24) & 0xff;
|
---|
315 | }
|
---|
316 |
|
---|
317 | static uint16_t decode16_be(uint8_t *buf)
|
---|
318 | {
|
---|
319 | return ((buf[0] << 8) | buf[1]);
|
---|
320 | }
|
---|
321 |
|
---|
322 | static uint16_t decode16_le(uint8_t *buf)
|
---|
323 | {
|
---|
324 | return ((buf[1] << 8) | buf[0]);
|
---|
325 | }
|
---|
326 |
|
---|
327 | static uint32_t decode32_be(uint8_t *buf)
|
---|
328 | {
|
---|
329 | return ((buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]);
|
---|
330 | }
|
---|
331 |
|
---|
332 | static uint32_t decode32_le(uint8_t *buf)
|
---|
333 | {
|
---|
334 | return ((buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0]);
|
---|
335 | }
|
---|
336 |
|
---|
337 | /** @}
|
---|
338 | */
|
---|