Remove unistd.h
[helenos.git] / uspace / lib / c / generic / uuid.c
blob4a62c28d4e84689dd5fd893361db61ca56e7b29f
1 /*
2 * Copyright (c) 2015 Jiri Svoboda
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
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.
29 /** @addtogroup libc
30 * @{
32 /** @file Universaly Unique Identifier (see RFC 4122)
35 #include <errno.h>
36 #include <uuid.h>
37 #include <stdlib.h>
38 #include <stddef.h>
39 #include <time.h>
40 #include <str.h>
42 /** Generate UUID.
44 * @param uuid Place to store generated UUID
45 * @return EOK on success or negative error code
47 int uuid_generate(uuid_t *uuid)
49 int i;
50 struct timeval tv;
52 /* XXX This is a rather poor way of generating random numbers */
53 gettimeofday(&tv, NULL);
54 srandom(tv.tv_sec ^ tv.tv_usec);
56 for (i = 0; i < uuid_bytes; i++)
57 uuid->b[i] = random();
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;
63 return EOK;
66 /** Encode UUID into binary form per RFC 4122.
68 * @param uuid UUID
69 * @param buf 16-byte destination buffer
71 void uuid_encode(uuid_t *uuid, uint8_t *buf)
73 int i;
75 for (i = 0; i < uuid_bytes; i++)
76 buf[i] = uuid->b[i];
79 /** Decode UUID from binary form per RFC 4122.
81 * @param buf 16-byte source buffer
82 * @param uuid Place to store UUID
84 void uuid_decode(uint8_t *buf, uuid_t *uuid)
86 int i;
88 for (i = 0; i < uuid_bytes; i++)
89 uuid->b[i] = buf[i];
92 /** Parse string UUID.
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.
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
102 * @return EOK on success or negative error code
104 int uuid_parse(const char *str, uuid_t *uuid, const char **endptr)
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;
115 rc = str_uint32_t(str, &eptr, 16, false, &time_low);
116 if (rc != EOK || eptr != str + 8 || *eptr != '-')
117 return EINVAL;
119 rc = str_uint16_t(str + 9, &eptr, 16, false, &time_mid);
120 if (rc != EOK || eptr != str + 13 || *eptr != '-')
121 return EINVAL;
123 rc = str_uint16_t(str + 14, &eptr, 16, false, &time_ver);
124 if (rc != EOK || eptr != str + 18 || *eptr != '-')
125 return EINVAL;
127 rc = str_uint16_t(str + 19, &eptr, 16, false, &clock);
128 if (rc != EOK || eptr != str + 23 || *eptr != '-')
129 return EINVAL;
131 rc = str_uint64_t(str + 24, &eptr, 16, false, &node);
132 if (rc != EOK || eptr != str + 36 || *eptr != '\0')
133 return EINVAL;
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;
140 uuid->b[4] = time_mid >> 8;
141 uuid->b[5] = time_mid & 0xff;
143 uuid->b[6] = time_ver >> 8;
144 uuid->b[7] = time_ver & 0xff;
146 uuid->b[8] = clock >> 8;
147 uuid->b[9] = clock & 0xff;
149 for (i = 0; i < 6; i++)
150 uuid->b[10 + i] = (node >> 8 * (5 - i)) & 0xff;
152 if (endptr != NULL) {
153 *endptr = str + 36;
154 } else {
155 if (*(str + 36) != '\0')
156 return EINVAL;
159 return EOK;
162 /** Format UUID into string representation.
164 * @param uuid UUID
165 * @param rstr Place to store pointer to newly allocated string
167 * @return EOK on success, ENOMEM if out of memory
169 int uuid_format(uuid_t *uuid, char **rstr)
171 return ENOTSUP;
175 /** @}