]> git.saurik.com Git - apple/xnu.git/blob - libkern/uuid/uuid.c
xnu-1699.32.7.tar.gz
[apple/xnu.git] / libkern / uuid / uuid.c
1 /*
2 * Copyright (c) 2004-2010 Apple Inc. All rights reserved.
3 *
4 * %Begin-Header%
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 * 1. Redistributions of source code must retain the above copyright
9 * notice, and the entire permission notice in its entirety,
10 * including the disclaimer of warranties.
11 * 2. 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 * 3. The name of the author may not be used to endorse or promote
15 * products derived from this software without specific prior
16 * written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
19 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
21 * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
24 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
25 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
28 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
29 * DAMAGE.
30 * %End-Header%
31 */
32
33 #include <uuid/uuid.h>
34
35 #include <stdint.h>
36 #include <string.h>
37
38 #include <sys/random.h>
39 #include <sys/socket.h>
40 #include <sys/systm.h>
41 #include <sys/time.h>
42
43 #include <net/if.h>
44 #include <net/if_dl.h>
45 #include <net/if_types.h>
46
47 UUID_DEFINE(UUID_NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
48
49 static void
50 read_node(uint8_t *node)
51 {
52 #if NETWORKING
53 struct ifnet *ifp;
54 struct sockaddr_dl *sdl;
55
56 ifnet_head_lock_shared();
57 TAILQ_FOREACH(ifp, &ifnet_head, if_link) {
58 ifnet_lock_shared(ifp);
59 IFA_LOCK_SPIN(ifp->if_lladdr);
60 sdl = (struct sockaddr_dl *)ifp->if_lladdr->ifa_addr;
61 if (sdl->sdl_type == IFT_ETHER) {
62 memcpy(node, LLADDR(sdl), 6);
63 IFA_UNLOCK(ifp->if_lladdr);
64 ifnet_lock_done(ifp);
65 ifnet_head_done();
66 return;
67 }
68 IFA_UNLOCK(ifp->if_lladdr);
69 ifnet_lock_done(ifp);
70 }
71 ifnet_head_done();
72 #endif /* NETWORKING */
73
74 read_random(node, 6);
75 node[0] |= 0x01;
76 }
77
78 static uint64_t
79 read_time(void)
80 {
81 struct timespec tv;
82
83 nanotime(&tv);
84
85 return (tv.tv_sec * 10000000ULL) + (tv.tv_nsec / 100ULL) + 0x01B21DD213814000ULL;
86 }
87
88 void
89 uuid_clear(uuid_t uu)
90 {
91 memset(uu, 0, sizeof(uuid_t));
92 }
93
94 int
95 uuid_compare(const uuid_t uu1, const uuid_t uu2)
96 {
97 return memcmp(uu1, uu2, sizeof(uuid_t));
98 }
99
100 void
101 uuid_copy(uuid_t dst, const uuid_t src)
102 {
103 memcpy(dst, src, sizeof(uuid_t));
104 }
105
106 void
107 uuid_generate_random(uuid_t out)
108 {
109 read_random(out, sizeof(uuid_t));
110
111 out[6] = (out[6] & 0x0F) | 0x40;
112 out[8] = (out[8] & 0x3F) | 0x80;
113 }
114
115 void
116 uuid_generate_time(uuid_t out)
117 {
118 uint64_t time;
119
120 read_node(&out[10]);
121 read_random(&out[8], 2);
122
123 time = read_time();
124 out[0] = (uint8_t)(time >> 24);
125 out[1] = (uint8_t)(time >> 16);
126 out[2] = (uint8_t)(time >> 8);
127 out[3] = (uint8_t)time;
128 out[4] = (uint8_t)(time >> 40);
129 out[5] = (uint8_t)(time >> 32);
130 out[6] = (uint8_t)(time >> 56);
131 out[7] = (uint8_t)(time >> 48);
132
133 out[6] = (out[6] & 0x0F) | 0x10;
134 out[8] = (out[8] & 0x3F) | 0x80;
135 }
136
137 void
138 uuid_generate(uuid_t out)
139 {
140 uuid_generate_random(out);
141 }
142
143 int
144 uuid_is_null(const uuid_t uu)
145 {
146 return !memcmp(uu, UUID_NULL, sizeof(uuid_t));
147 }
148
149 int
150 uuid_parse(const uuid_string_t in, uuid_t uu)
151 {
152 int n = 0;
153
154 sscanf(in,
155 "%2hhx%2hhx%2hhx%2hhx-"
156 "%2hhx%2hhx-"
157 "%2hhx%2hhx-"
158 "%2hhx%2hhx-"
159 "%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%n",
160 &uu[0], &uu[1], &uu[2], &uu[3],
161 &uu[4], &uu[5],
162 &uu[6], &uu[7],
163 &uu[8], &uu[9],
164 &uu[10], &uu[11], &uu[12], &uu[13], &uu[14], &uu[15], &n);
165
166 return (n != 36 || in[n] != '\0' ? -1 : 0);
167 }
168
169 void
170 uuid_unparse_lower(const uuid_t uu, uuid_string_t out)
171 {
172 snprintf(out,
173 sizeof(uuid_string_t),
174 "%02x%02x%02x%02x-"
175 "%02x%02x-"
176 "%02x%02x-"
177 "%02x%02x-"
178 "%02x%02x%02x%02x%02x%02x",
179 uu[0], uu[1], uu[2], uu[3],
180 uu[4], uu[5],
181 uu[6], uu[7],
182 uu[8], uu[9],
183 uu[10], uu[11], uu[12], uu[13], uu[14], uu[15]);
184 }
185
186 void
187 uuid_unparse_upper(const uuid_t uu, uuid_string_t out)
188 {
189 snprintf(out,
190 sizeof(uuid_string_t),
191 "%02X%02X%02X%02X-"
192 "%02X%02X-"
193 "%02X%02X-"
194 "%02X%02X-"
195 "%02X%02X%02X%02X%02X%02X",
196 uu[0], uu[1], uu[2], uu[3],
197 uu[4], uu[5],
198 uu[6], uu[7],
199 uu[8], uu[9],
200 uu[10], uu[11], uu[12], uu[13], uu[14], uu[15]);
201 }
202
203 void
204 uuid_unparse(const uuid_t uu, uuid_string_t out)
205 {
206 uuid_unparse_upper(uu, out);
207 }