[70815a24] | 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>
|
---|
[87337dc5] | 37 | #include <rndgen.h>
|
---|
[70815a24] | 38 | #include <stdlib.h>
|
---|
[582a0b8] | 39 | #include <stddef.h>
|
---|
[f57ccb5] | 40 | #include <str.h>
|
---|
[6c707e4] | 41 | #include <stdio.h>
|
---|
[70815a24] | 42 |
|
---|
[3887aab2] | 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 |
|
---|
[70815a24] | 52 | /** Generate UUID.
|
---|
| 53 | *
|
---|
| 54 | * @param uuid Place to store generated UUID
|
---|
[cde999a] | 55 | * @return EOK on success or an error code
|
---|
[70815a24] | 56 | */
|
---|
[b7fd2a0] | 57 | errno_t uuid_generate(uuid_t *uuid)
|
---|
[70815a24] | 58 | {
|
---|
| 59 | int i;
|
---|
[87337dc5] | 60 | rndgen_t *rndgen;
|
---|
| 61 | errno_t rc;
|
---|
[70815a24] | 62 |
|
---|
[87337dc5] | 63 | rc = rndgen_create(&rndgen);
|
---|
| 64 | if (rc != EOK)
|
---|
| 65 | return EIO;
|
---|
[70815a24] | 66 |
|
---|
[3887aab2] | 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]);
|
---|
[87337dc5] | 99 | if (rc != EOK) {
|
---|
| 100 | rc = EIO;
|
---|
| 101 | goto error;
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
[70815a24] | 104 |
|
---|
| 105 | /* Version 4 UUID from random or pseudo-random numbers */
|
---|
[3887aab2] | 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;
|
---|
[70815a24] | 108 |
|
---|
[87337dc5] | 109 | error:
|
---|
| 110 | rndgen_destroy(rndgen);
|
---|
| 111 | return rc;
|
---|
[70815a24] | 112 | }
|
---|
| 113 |
|
---|
| 114 | /** Encode UUID into binary form per RFC 4122.
|
---|
| 115 | *
|
---|
| 116 | * @param uuid UUID
|
---|
| 117 | * @param buf 16-byte destination buffer
|
---|
| 118 | */
|
---|
| 119 | void uuid_encode(uuid_t *uuid, uint8_t *buf)
|
---|
| 120 | {
|
---|
| 121 | int i;
|
---|
| 122 |
|
---|
[3887aab2] | 123 | encode32_be(buf, uuid->time_low);
|
---|
| 124 | encode16_be(buf + 4, uuid->time_mid);
|
---|
| 125 | encode16_be(buf + 6, uuid->time_hi_and_version);
|
---|
| 126 | buf[8] = uuid->clock_seq_hi_and_reserved;
|
---|
| 127 | buf[9] = uuid->clock_seq_low;
|
---|
| 128 |
|
---|
| 129 | for (i = 0; i < _UUID_NODE_LEN; i++)
|
---|
| 130 | buf[10 + i] = uuid->node[i];
|
---|
[70815a24] | 131 | }
|
---|
| 132 |
|
---|
| 133 | /** Decode UUID from binary form per RFC 4122.
|
---|
| 134 | *
|
---|
| 135 | * @param buf 16-byte source buffer
|
---|
| 136 | * @param uuid Place to store UUID
|
---|
| 137 | */
|
---|
| 138 | void uuid_decode(uint8_t *buf, uuid_t *uuid)
|
---|
| 139 | {
|
---|
| 140 | int i;
|
---|
| 141 |
|
---|
[3887aab2] | 142 | uuid->time_low = decode32_be(buf);
|
---|
| 143 | uuid->time_mid = decode16_be(buf + 4);
|
---|
| 144 | uuid->time_hi_and_version = decode16_be(buf + 6);
|
---|
| 145 | uuid->clock_seq_hi_and_reserved = buf[8];
|
---|
| 146 | uuid->clock_seq_low = buf[9];
|
---|
| 147 |
|
---|
| 148 | for (i = 0; i < _UUID_NODE_LEN; i++)
|
---|
| 149 | uuid->node[i] = buf[10 + i];
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | /** Encode UUID into little-endian (GPT) binary form.
|
---|
| 153 | *
|
---|
| 154 | * @param uuid UUID
|
---|
| 155 | * @param buf 16-byte destination buffer
|
---|
| 156 | */
|
---|
| 157 | void uuid_encode_le(uuid_t *uuid, uint8_t *buf)
|
---|
| 158 | {
|
---|
| 159 | int i;
|
---|
| 160 |
|
---|
| 161 | encode32_le(buf, uuid->time_low);
|
---|
| 162 | encode16_le(buf + 4, uuid->time_mid);
|
---|
| 163 | encode16_le(buf + 6, uuid->time_hi_and_version);
|
---|
| 164 | buf[8] = uuid->clock_seq_hi_and_reserved;
|
---|
| 165 | buf[9] = uuid->clock_seq_low;
|
---|
| 166 |
|
---|
| 167 | for (i = 0; i < _UUID_NODE_LEN; i++)
|
---|
| 168 | buf[10 + i] = uuid->node[i];
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | /** Decode UUID from little-endian (GPT) binary form.
|
---|
| 172 | *
|
---|
| 173 | * @param buf 16-byte source buffer
|
---|
| 174 | * @param uuid Place to store UUID
|
---|
| 175 | */
|
---|
| 176 | void uuid_decode_le(uint8_t *buf, uuid_t *uuid)
|
---|
| 177 | {
|
---|
| 178 | int i;
|
---|
| 179 |
|
---|
| 180 | uuid->time_low = decode32_le(buf);
|
---|
| 181 | uuid->time_mid = decode16_le(buf + 4);
|
---|
| 182 | uuid->time_hi_and_version = decode16_le(buf + 6);
|
---|
| 183 | uuid->clock_seq_hi_and_reserved = buf[8];
|
---|
| 184 | uuid->clock_seq_low = buf[9];
|
---|
| 185 |
|
---|
| 186 | for (i = 0; i < _UUID_NODE_LEN; i++)
|
---|
| 187 | uuid->node[i] = buf[10 + i];
|
---|
[70815a24] | 188 | }
|
---|
| 189 |
|
---|
[f57ccb5] | 190 | /** Parse string UUID.
|
---|
| 191 | *
|
---|
| 192 | * If @a endptr is not NULL, it is set to point to the first character
|
---|
| 193 | * following the UUID. If @a endptr is NULL, the string must not contain
|
---|
| 194 | * any characters following the UUID, otherwise an error is returned.
|
---|
| 195 | *
|
---|
| 196 | * @param str String beginning with UUID string representation
|
---|
| 197 | * @param uuid Place to store UUID
|
---|
| 198 | * @param endptr Place to store pointer to end of UUID or @c NULL
|
---|
| 199 | *
|
---|
[cde999a] | 200 | * @return EOK on success or an error code
|
---|
[f57ccb5] | 201 | */
|
---|
[b7fd2a0] | 202 | errno_t uuid_parse(const char *str, uuid_t *uuid, const char **endptr)
|
---|
[f57ccb5] | 203 | {
|
---|
[b7fd2a0] | 204 | errno_t rc;
|
---|
[f57ccb5] | 205 | const char *eptr;
|
---|
| 206 | uint32_t time_low;
|
---|
| 207 | uint16_t time_mid;
|
---|
| 208 | uint16_t time_ver;
|
---|
| 209 | uint16_t clock;
|
---|
| 210 | uint64_t node;
|
---|
| 211 | int i;
|
---|
| 212 |
|
---|
| 213 | rc = str_uint32_t(str, &eptr, 16, false, &time_low);
|
---|
| 214 | if (rc != EOK || eptr != str + 8 || *eptr != '-')
|
---|
| 215 | return EINVAL;
|
---|
| 216 |
|
---|
| 217 | rc = str_uint16_t(str + 9, &eptr, 16, false, &time_mid);
|
---|
| 218 | if (rc != EOK || eptr != str + 13 || *eptr != '-')
|
---|
| 219 | return EINVAL;
|
---|
| 220 |
|
---|
| 221 | rc = str_uint16_t(str + 14, &eptr, 16, false, &time_ver);
|
---|
| 222 | if (rc != EOK || eptr != str + 18 || *eptr != '-')
|
---|
| 223 | return EINVAL;
|
---|
| 224 |
|
---|
| 225 | rc = str_uint16_t(str + 19, &eptr, 16, false, &clock);
|
---|
| 226 | if (rc != EOK || eptr != str + 23 || *eptr != '-')
|
---|
| 227 | return EINVAL;
|
---|
| 228 |
|
---|
| 229 | rc = str_uint64_t(str + 24, &eptr, 16, false, &node);
|
---|
[ab7d85a] | 230 | if (rc != EOK || eptr != str + 36)
|
---|
[f57ccb5] | 231 | return EINVAL;
|
---|
| 232 |
|
---|
[3887aab2] | 233 | uuid->time_low = time_low;
|
---|
[f57ccb5] | 234 |
|
---|
[3887aab2] | 235 | uuid->time_mid = time_mid;
|
---|
[f57ccb5] | 236 |
|
---|
[3887aab2] | 237 | uuid->time_hi_and_version = time_ver;
|
---|
[f57ccb5] | 238 |
|
---|
[3887aab2] | 239 | uuid->clock_seq_hi_and_reserved = clock >> 8;
|
---|
[f57ccb5] | 240 |
|
---|
[3887aab2] | 241 | uuid->clock_seq_low = clock & 0xff;
|
---|
| 242 |
|
---|
| 243 | for (i = 0; i < _UUID_NODE_LEN; i++)
|
---|
| 244 | uuid->node[i] = (node >> 8 * (5 - i)) & 0xff;
|
---|
[f57ccb5] | 245 |
|
---|
| 246 | if (endptr != NULL) {
|
---|
| 247 | *endptr = str + 36;
|
---|
| 248 | } else {
|
---|
| 249 | if (*(str + 36) != '\0')
|
---|
| 250 | return EINVAL;
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 | return EOK;
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | /** Format UUID into string representation.
|
---|
| 257 | *
|
---|
| 258 | * @param uuid UUID
|
---|
| 259 | * @param rstr Place to store pointer to newly allocated string
|
---|
| 260 | *
|
---|
| 261 | * @return EOK on success, ENOMEM if out of memory
|
---|
| 262 | */
|
---|
[6c707e4] | 263 | errno_t uuid_format(uuid_t *uuid, char **rstr, bool uppercase)
|
---|
[f57ccb5] | 264 | {
|
---|
[6c707e4] | 265 | size_t size = 37;
|
---|
| 266 | char *str = malloc(sizeof(char) * size);
|
---|
| 267 | if (str == NULL)
|
---|
| 268 | return ENOMEM;
|
---|
| 269 |
|
---|
[3887aab2] | 270 | const char *format = "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x";
|
---|
[6c707e4] | 271 | if (uppercase)
|
---|
[3887aab2] | 272 | format = "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X";
|
---|
[6c707e4] | 273 |
|
---|
[3887aab2] | 274 | int ret = snprintf(str, size, format, uuid->time_low, uuid->time_mid,
|
---|
| 275 | uuid->time_hi_and_version, uuid->clock_seq_hi_and_reserved,
|
---|
| 276 | uuid->clock_seq_low, uuid->node[0], uuid->node[1], uuid->node[2],
|
---|
| 277 | uuid->node[3], uuid->node[4], uuid->node[5]);
|
---|
[6c707e4] | 278 |
|
---|
[11e4856] | 279 | if (ret != 36) {
|
---|
| 280 | free(str);
|
---|
[6c707e4] | 281 | return EINVAL;
|
---|
[11e4856] | 282 | }
|
---|
[6c707e4] | 283 |
|
---|
| 284 | *rstr = str;
|
---|
| 285 | return EOK;
|
---|
[f57ccb5] | 286 | }
|
---|
| 287 |
|
---|
[3887aab2] | 288 | static void encode16_be(uint8_t *buf, uint16_t value)
|
---|
| 289 | {
|
---|
| 290 | buf[0] = (value >> 8) & 0xff;
|
---|
| 291 | buf[1] = value & 0xff;
|
---|
| 292 | }
|
---|
| 293 |
|
---|
| 294 | static void encode16_le(uint8_t *buf, uint16_t value)
|
---|
| 295 | {
|
---|
| 296 | buf[0] = value & 0xff;
|
---|
| 297 | buf[1] = (value >> 8) & 0xff;
|
---|
| 298 | }
|
---|
| 299 |
|
---|
| 300 | static void encode32_be(uint8_t *buf, uint32_t value)
|
---|
| 301 | {
|
---|
| 302 | buf[0] = (value >> 24) & 0xff;
|
---|
| 303 | buf[1] = (value >> 16) & 0xff;
|
---|
| 304 | buf[2] = (value >> 8) & 0xff;
|
---|
| 305 | buf[3] = value & 0xff;
|
---|
| 306 | }
|
---|
| 307 |
|
---|
| 308 | static void encode32_le(uint8_t *buf, uint32_t value)
|
---|
| 309 | {
|
---|
| 310 | buf[0] = value & 0xff;
|
---|
| 311 | buf[1] = (value >> 8) & 0xff;
|
---|
| 312 | buf[2] = (value >> 16) & 0xff;
|
---|
| 313 | buf[3] = (value >> 24) & 0xff;
|
---|
| 314 | }
|
---|
| 315 |
|
---|
| 316 | static uint16_t decode16_be(uint8_t *buf)
|
---|
| 317 | {
|
---|
| 318 | return ((buf[0] << 8) | buf[1]);
|
---|
| 319 | }
|
---|
| 320 |
|
---|
| 321 | static uint16_t decode16_le(uint8_t *buf)
|
---|
| 322 | {
|
---|
| 323 | return ((buf[1] << 8) | buf[0]);
|
---|
| 324 | }
|
---|
| 325 |
|
---|
| 326 | static uint32_t decode32_be(uint8_t *buf)
|
---|
| 327 | {
|
---|
| 328 | return ((buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]);
|
---|
| 329 | }
|
---|
| 330 |
|
---|
| 331 | static uint32_t decode32_le(uint8_t *buf)
|
---|
| 332 | {
|
---|
| 333 | return ((buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0]);
|
---|
| 334 | }
|
---|
| 335 |
|
---|
[70815a24] | 336 | /** @}
|
---|
| 337 | */
|
---|