source: mainline/uspace/lib/c/generic/uuid.c@ 5e801dc

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 5e801dc was a4ee8b3f, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 6 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.

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