2 * Copyright (c) 2015 Jiri Svoboda
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
32 /** @file Universaly Unique Identifier (see RFC 4122)
44 * @param uuid Place to store generated UUID
45 * @return EOK on success or negative error code
47 int uuid_generate(uuid_t
*uuid
)
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;
66 /** Encode UUID into binary form per RFC 4122.
69 * @param buf 16-byte destination buffer
71 void uuid_encode(uuid_t
*uuid
, uint8_t *buf
)
75 for (i
= 0; i
< uuid_bytes
; 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
)
88 for (i
= 0; i
< uuid_bytes
; 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
)
115 rc
= str_uint32_t(str
, &eptr
, 16, false, &time_low
);
116 if (rc
!= EOK
|| eptr
!= str
+ 8 || *eptr
!= '-')
119 rc
= str_uint16_t(str
+ 9, &eptr
, 16, false, &time_mid
);
120 if (rc
!= EOK
|| eptr
!= str
+ 13 || *eptr
!= '-')
123 rc
= str_uint16_t(str
+ 14, &eptr
, 16, false, &time_ver
);
124 if (rc
!= EOK
|| eptr
!= str
+ 18 || *eptr
!= '-')
127 rc
= str_uint16_t(str
+ 19, &eptr
, 16, false, &clock
);
128 if (rc
!= EOK
|| eptr
!= str
+ 23 || *eptr
!= '-')
131 rc
= str_uint64_t(str
+ 24, &eptr
, 16, false, &node
);
132 if (rc
!= EOK
|| eptr
!= str
+ 36 || *eptr
!= '\0')
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
) {
155 if (*(str
+ 36) != '\0')
162 /** Format UUID into string representation.
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
)