[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>
|
---|
[70815a24] | 41 |
|
---|
| 42 | /** Generate UUID.
|
---|
| 43 | *
|
---|
| 44 | * @param uuid Place to store generated UUID
|
---|
[cde999a] | 45 | * @return EOK on success or an error code
|
---|
[70815a24] | 46 | */
|
---|
[b7fd2a0] | 47 | errno_t uuid_generate(uuid_t *uuid)
|
---|
[70815a24] | 48 | {
|
---|
| 49 | int i;
|
---|
[87337dc5] | 50 | rndgen_t *rndgen;
|
---|
| 51 | errno_t rc;
|
---|
[70815a24] | 52 |
|
---|
[87337dc5] | 53 | rc = rndgen_create(&rndgen);
|
---|
| 54 | if (rc != EOK)
|
---|
| 55 | return EIO;
|
---|
[70815a24] | 56 |
|
---|
[87337dc5] | 57 | for (i = 0; i < uuid_bytes; i++) {
|
---|
| 58 | rc = rndgen_uint8(rndgen, &uuid->b[i]);
|
---|
| 59 | if (rc != EOK) {
|
---|
| 60 | rc = EIO;
|
---|
| 61 | goto error;
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
[70815a24] | 64 |
|
---|
| 65 | /* Version 4 UUID from random or pseudo-random numbers */
|
---|
| 66 | uuid->b[8] = (uuid->b[8] & ~0xc0) | 0x40;
|
---|
| 67 | uuid->b[6] = (uuid->b[6] & 0xf0) | 0x40;
|
---|
| 68 |
|
---|
| 69 | return EOK;
|
---|
[87337dc5] | 70 | error:
|
---|
| 71 | rndgen_destroy(rndgen);
|
---|
| 72 | return rc;
|
---|
[70815a24] | 73 | }
|
---|
| 74 |
|
---|
| 75 | /** Encode UUID into binary form per RFC 4122.
|
---|
| 76 | *
|
---|
| 77 | * @param uuid UUID
|
---|
| 78 | * @param buf 16-byte destination buffer
|
---|
| 79 | */
|
---|
| 80 | void 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 | */
|
---|
| 93 | void 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 |
|
---|
[f57ccb5] | 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 | *
|
---|
[cde999a] | 111 | * @return EOK on success or an error code
|
---|
[f57ccb5] | 112 | */
|
---|
[b7fd2a0] | 113 | errno_t uuid_parse(const char *str, uuid_t *uuid, const char **endptr)
|
---|
[f57ccb5] | 114 | {
|
---|
[b7fd2a0] | 115 | errno_t rc;
|
---|
[f57ccb5] | 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 || *eptr != '\0')
|
---|
| 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 | */
|
---|
[b7fd2a0] | 178 | errno_t uuid_format(uuid_t *uuid, char **rstr)
|
---|
[f57ccb5] | 179 | {
|
---|
| 180 | return ENOTSUP;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 |
|
---|
[70815a24] | 184 | /** @}
|
---|
| 185 | */
|
---|