[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>
|
---|
| 37 | #include <stdlib.h>
|
---|
[582a0b8] | 38 | #include <stddef.h>
|
---|
| 39 | #include <time.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 | */
|
---|
| 47 | int uuid_generate(uuid_t *uuid)
|
---|
| 48 | {
|
---|
| 49 | int i;
|
---|
| 50 | struct timeval tv;
|
---|
| 51 |
|
---|
| 52 | /* XXX This is a rather poor way of generating random numbers */
|
---|
| 53 | gettimeofday(&tv, NULL);
|
---|
| 54 | srandom(tv.tv_sec ^ tv.tv_usec);
|
---|
| 55 |
|
---|
| 56 | for (i = 0; i < uuid_bytes; i++)
|
---|
| 57 | uuid->b[i] = random();
|
---|
| 58 |
|
---|
| 59 | /* Version 4 UUID from random or pseudo-random numbers */
|
---|
| 60 | uuid->b[8] = (uuid->b[8] & ~0xc0) | 0x40;
|
---|
| 61 | uuid->b[6] = (uuid->b[6] & 0xf0) | 0x40;
|
---|
| 62 |
|
---|
| 63 | return EOK;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | /** Encode UUID into binary form per RFC 4122.
|
---|
| 67 | *
|
---|
| 68 | * @param uuid UUID
|
---|
| 69 | * @param buf 16-byte destination buffer
|
---|
| 70 | */
|
---|
| 71 | void uuid_encode(uuid_t *uuid, uint8_t *buf)
|
---|
| 72 | {
|
---|
| 73 | int i;
|
---|
| 74 |
|
---|
| 75 | for (i = 0; i < uuid_bytes; i++)
|
---|
| 76 | buf[i] = uuid->b[i];
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | /** Decode UUID from binary form per RFC 4122.
|
---|
| 80 | *
|
---|
| 81 | * @param buf 16-byte source buffer
|
---|
| 82 | * @param uuid Place to store UUID
|
---|
| 83 | */
|
---|
| 84 | void uuid_decode(uint8_t *buf, uuid_t *uuid)
|
---|
| 85 | {
|
---|
| 86 | int i;
|
---|
| 87 |
|
---|
| 88 | for (i = 0; i < uuid_bytes; i++)
|
---|
| 89 | uuid->b[i] = buf[i];
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[f57ccb5] | 92 | /** Parse string UUID.
|
---|
| 93 | *
|
---|
| 94 | * If @a endptr is not NULL, it is set to point to the first character
|
---|
| 95 | * following the UUID. If @a endptr is NULL, the string must not contain
|
---|
| 96 | * any characters following the UUID, otherwise an error is returned.
|
---|
| 97 | *
|
---|
| 98 | * @param str String beginning with UUID string representation
|
---|
| 99 | * @param uuid Place to store UUID
|
---|
| 100 | * @param endptr Place to store pointer to end of UUID or @c NULL
|
---|
| 101 | *
|
---|
[cde999a] | 102 | * @return EOK on success or an error code
|
---|
[f57ccb5] | 103 | */
|
---|
| 104 | int uuid_parse(const char *str, uuid_t *uuid, const char **endptr)
|
---|
| 105 | {
|
---|
| 106 | int rc;
|
---|
| 107 | const char *eptr;
|
---|
| 108 | uint32_t time_low;
|
---|
| 109 | uint16_t time_mid;
|
---|
| 110 | uint16_t time_ver;
|
---|
| 111 | uint16_t clock;
|
---|
| 112 | uint64_t node;
|
---|
| 113 | int i;
|
---|
| 114 |
|
---|
| 115 | rc = str_uint32_t(str, &eptr, 16, false, &time_low);
|
---|
| 116 | if (rc != EOK || eptr != str + 8 || *eptr != '-')
|
---|
| 117 | return EINVAL;
|
---|
| 118 |
|
---|
| 119 | rc = str_uint16_t(str + 9, &eptr, 16, false, &time_mid);
|
---|
| 120 | if (rc != EOK || eptr != str + 13 || *eptr != '-')
|
---|
| 121 | return EINVAL;
|
---|
| 122 |
|
---|
| 123 | rc = str_uint16_t(str + 14, &eptr, 16, false, &time_ver);
|
---|
| 124 | if (rc != EOK || eptr != str + 18 || *eptr != '-')
|
---|
| 125 | return EINVAL;
|
---|
| 126 |
|
---|
| 127 | rc = str_uint16_t(str + 19, &eptr, 16, false, &clock);
|
---|
| 128 | if (rc != EOK || eptr != str + 23 || *eptr != '-')
|
---|
| 129 | return EINVAL;
|
---|
| 130 |
|
---|
| 131 | rc = str_uint64_t(str + 24, &eptr, 16, false, &node);
|
---|
| 132 | if (rc != EOK || eptr != str + 36 || *eptr != '\0')
|
---|
| 133 | return EINVAL;
|
---|
| 134 |
|
---|
| 135 | uuid->b[0] = time_low >> 24;
|
---|
| 136 | uuid->b[1] = (time_low >> 16) & 0xff;
|
---|
| 137 | uuid->b[2] = (time_low >> 8) & 0xff;
|
---|
| 138 | uuid->b[3] = time_low & 0xff;
|
---|
| 139 |
|
---|
| 140 | uuid->b[4] = time_mid >> 8;
|
---|
| 141 | uuid->b[5] = time_mid & 0xff;
|
---|
| 142 |
|
---|
| 143 | uuid->b[6] = time_ver >> 8;
|
---|
| 144 | uuid->b[7] = time_ver & 0xff;
|
---|
| 145 |
|
---|
| 146 | uuid->b[8] = clock >> 8;
|
---|
| 147 | uuid->b[9] = clock & 0xff;
|
---|
| 148 |
|
---|
| 149 | for (i = 0; i < 6; i++)
|
---|
| 150 | uuid->b[10 + i] = (node >> 8 * (5 - i)) & 0xff;
|
---|
| 151 |
|
---|
| 152 | if (endptr != NULL) {
|
---|
| 153 | *endptr = str + 36;
|
---|
| 154 | } else {
|
---|
| 155 | if (*(str + 36) != '\0')
|
---|
| 156 | return EINVAL;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | return EOK;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | /** Format UUID into string representation.
|
---|
| 163 | *
|
---|
| 164 | * @param uuid UUID
|
---|
| 165 | * @param rstr Place to store pointer to newly allocated string
|
---|
| 166 | *
|
---|
| 167 | * @return EOK on success, ENOMEM if out of memory
|
---|
| 168 | */
|
---|
| 169 | int uuid_format(uuid_t *uuid, char **rstr)
|
---|
| 170 | {
|
---|
| 171 | return ENOTSUP;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 |
|
---|
[70815a24] | 175 | /** @}
|
---|
| 176 | */
|
---|