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