source: mainline/uspace/lib/c/generic/uuid.c@ e3272101

Last change on this file since e3272101 was f52ce72, checked in by Matthieu Riolo <matthieu.riolo@…>, 7 years ago

Make sure rndgen_destroy() always gets called

The original implementation would not call
rndgen_destroy if the uuid was successfully
generated. Which could create a potential
memory leak.

Conflicts:

uspace/lib/c/generic/uuid.c

  • Property mode set to 100644
File size: 5.3 KB
Line 
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/** Generate UUID.
44 *
45 * @param uuid Place to store generated UUID
46 * @return EOK on success or an error code
47 */
48errno_t uuid_generate(uuid_t *uuid)
49{
50 int i;
51 rndgen_t *rndgen;
52 errno_t rc;
53
54 rc = rndgen_create(&rndgen);
55 if (rc != EOK)
56 return EIO;
57
58 for (i = 0; i < uuid_bytes; i++) {
59 rc = rndgen_uint8(rndgen, &uuid->b[i]);
60 if (rc != EOK) {
61 rc = EIO;
62 goto error;
63 }
64 }
65
66 /* Version 4 UUID from random or pseudo-random numbers */
67 uuid->b[6] = (uuid->b[6] & 0x4F) | 0x40;
68 uuid->b[8] = (uuid->b[8] & 0xBF) | 0xB0;
69
70 rc = EOK;
71error:
72 rndgen_destroy(rndgen);
73 return rc;
74}
75
76/** Encode UUID into binary form per RFC 4122.
77 *
78 * @param uuid UUID
79 * @param buf 16-byte destination buffer
80 */
81void uuid_encode(uuid_t *uuid, uint8_t *buf)
82{
83 int i;
84
85 for (i = 0; i < uuid_bytes; i++)
86 buf[i] = uuid->b[i];
87}
88
89/** Decode UUID from binary form per RFC 4122.
90 *
91 * @param buf 16-byte source buffer
92 * @param uuid Place to store UUID
93 */
94void uuid_decode(uint8_t *buf, uuid_t *uuid)
95{
96 int i;
97
98 for (i = 0; i < uuid_bytes; i++)
99 uuid->b[i] = buf[i];
100}
101
102/** Parse string UUID.
103 *
104 * If @a endptr is not NULL, it is set to point to the first character
105 * following the UUID. If @a endptr is NULL, the string must not contain
106 * any characters following the UUID, otherwise an error is returned.
107 *
108 * @param str String beginning with UUID string representation
109 * @param uuid Place to store UUID
110 * @param endptr Place to store pointer to end of UUID or @c NULL
111 *
112 * @return EOK on success or an error code
113 */
114errno_t uuid_parse(const char *str, uuid_t *uuid, const char **endptr)
115{
116 errno_t rc;
117 const char *eptr;
118 uint32_t time_low;
119 uint16_t time_mid;
120 uint16_t time_ver;
121 uint16_t clock;
122 uint64_t node;
123 int i;
124
125 rc = str_uint32_t(str, &eptr, 16, false, &time_low);
126 if (rc != EOK || eptr != str + 8 || *eptr != '-')
127 return EINVAL;
128
129 rc = str_uint16_t(str + 9, &eptr, 16, false, &time_mid);
130 if (rc != EOK || eptr != str + 13 || *eptr != '-')
131 return EINVAL;
132
133 rc = str_uint16_t(str + 14, &eptr, 16, false, &time_ver);
134 if (rc != EOK || eptr != str + 18 || *eptr != '-')
135 return EINVAL;
136
137 rc = str_uint16_t(str + 19, &eptr, 16, false, &clock);
138 if (rc != EOK || eptr != str + 23 || *eptr != '-')
139 return EINVAL;
140
141 rc = str_uint64_t(str + 24, &eptr, 16, false, &node);
142 if (rc != EOK || eptr != str + 36)
143 return EINVAL;
144
145 uuid->b[0] = time_low >> 24;
146 uuid->b[1] = (time_low >> 16) & 0xff;
147 uuid->b[2] = (time_low >> 8) & 0xff;
148 uuid->b[3] = time_low & 0xff;
149
150 uuid->b[4] = time_mid >> 8;
151 uuid->b[5] = time_mid & 0xff;
152
153 uuid->b[6] = time_ver >> 8;
154 uuid->b[7] = time_ver & 0xff;
155
156 uuid->b[8] = clock >> 8;
157 uuid->b[9] = clock & 0xff;
158
159 for (i = 0; i < 6; i++)
160 uuid->b[10 + i] = (node >> 8 * (5 - i)) & 0xff;
161
162 if (endptr != NULL) {
163 *endptr = str + 36;
164 } else {
165 if (*(str + 36) != '\0')
166 return EINVAL;
167 }
168
169 return EOK;
170}
171
172/** Format UUID into string representation.
173 *
174 * @param uuid UUID
175 * @param rstr Place to store pointer to newly allocated string
176 *
177 * @return EOK on success, ENOMEM if out of memory
178 */
179errno_t uuid_format(uuid_t *uuid, char **rstr, bool uppercase)
180{
181 size_t size = 37;
182 char *str = malloc(sizeof(char) * size);
183 if (str == NULL)
184 return ENOMEM;
185
186 const char *format = "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x";
187 if (uppercase)
188 format = "%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X";
189
190 int ret = snprintf(str, size, format, uuid->b[0], uuid->b[1], uuid->b[2], uuid->b[3], uuid->b[4], uuid->b[5], uuid->b[6], uuid->b[7], uuid->b[8], uuid->b[9], uuid->b[10], uuid->b[11], uuid->b[12], uuid->b[13], uuid->b[14], uuid->b[15]);
191
192 if (ret != 36)
193 return EINVAL;
194
195 *rstr = str;
196 return EOK;
197}
198
199/** @}
200 */
Note: See TracBrowser for help on using the repository browser.