]> git.saurik.com Git - apple/xnu.git/blame - libkern/uuid/uuid.c
xnu-792.6.56.tar.gz
[apple/xnu.git] / libkern / uuid / uuid.c
CommitLineData
91447636
A
1/*
2 * Copyright (c) 2004 Apple Computer, 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
47UUID_DEFINE(UUID_NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
48
49static void
50read_node(uint8_t *node)
51{
52 struct ifnet *ifp;
53 struct ifaddr *ifa;
54 struct sockaddr_dl *sdl;
55
56 ifnet_head_lock_shared();
57 TAILQ_FOREACH(ifp, &ifnet_head, if_link) {
58 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
59 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
60 if (sdl && sdl->sdl_family == AF_LINK && sdl->sdl_type == IFT_ETHER) {
61 memcpy(node, LLADDR(sdl), 6);
62 ifnet_head_done();
63 return;
64 }
65 }
66 }
67 ifnet_head_done();
68
69 read_random(node, 6);
70 node[0] |= 0x01;
71}
72
73static uint64_t
74read_time(void)
75{
76 struct timespec tv;
77
78 nanotime(&tv);
79
80 return (tv.tv_sec * 10000000ULL) + (tv.tv_nsec / 100ULL) + 0x01B21DD213814000ULL;
81}
82
83void
84uuid_clear(uuid_t uu)
85{
86 memset(uu, 0, sizeof(uuid_t));
87}
88
89int
90uuid_compare(const uuid_t uu1, const uuid_t uu2)
91{
92 return memcmp(uu1, uu2, sizeof(uuid_t));
93}
94
95void
96uuid_copy(uuid_t dst, const uuid_t src)
97{
98 memcpy(dst, src, sizeof(uuid_t));
99}
100
101void
102uuid_generate_random(uuid_t out)
103{
104 read_random(out, sizeof(uuid_t));
105
106 out[6] = (out[6] & 0x0F) | 0x40;
107 out[8] = (out[8] & 0x3F) | 0x80;
108}
109
110void
111uuid_generate_time(uuid_t out)
112{
113 uint64_t time;
114
115 read_node(&out[10]);
116 read_random(&out[8], 2);
117
118 time = read_time();
119 out[0] = (uint8_t)(time >> 24);
120 out[1] = (uint8_t)(time >> 16);
121 out[2] = (uint8_t)(time >> 8);
122 out[3] = (uint8_t)time;
123 out[4] = (uint8_t)(time >> 40);
124 out[5] = (uint8_t)(time >> 32);
125 out[6] = (uint8_t)(time >> 56);
126 out[7] = (uint8_t)(time >> 48);
127
128 out[6] = (out[6] & 0x0F) | 0x10;
129 out[8] = (out[8] & 0x3F) | 0x80;
130}
131
132void
133uuid_generate(uuid_t out)
134{
135 uuid_generate_random(out);
136}
137
138int
139uuid_is_null(const uuid_t uu)
140{
141 return !memcmp(uu, UUID_NULL, sizeof(uuid_t));
142}
143
144int
145uuid_parse(const char *in, uuid_t uu)
146{
147 int n = 0;
148
149 sscanf(in,
150 "%hh2x%hh2x%hh2x%hh2x-"
151 "%hh2x%hh2x-"
152 "%hh2x%hh2x-"
153 "%hh2x%hh2x-"
154 "%hh2x%hh2x%hh2x%hh2x%hh2x%hh2x%n",
155 &uu[0], &uu[1], &uu[2], &uu[3],
156 &uu[4], &uu[5],
157 &uu[6], &uu[7],
158 &uu[8], &uu[9],
159 &uu[10], &uu[11], &uu[12], &uu[13], &uu[14], &uu[15], &n);
160
161 return (n != 36 || in[n] != '\0' ? -1 : 0);
162}
163
164void
165uuid_unparse_lower(const uuid_t uu, char *out)
166{
167 sprintf(out,
168 "%02x%02x%02x%02x-"
169 "%02x%02x-"
170 "%02x%02x-"
171 "%02x%02x-"
172 "%02x%02x%02x%02x%02x%02x",
173 uu[0], uu[1], uu[2], uu[3],
174 uu[4], uu[5],
175 uu[6], uu[7],
176 uu[8], uu[9],
177 uu[10], uu[11], uu[12], uu[13], uu[14], uu[15]);
178}
179
180void
181uuid_unparse_upper(const uuid_t uu, char *out)
182{
183 sprintf(out,
184 "%02X%02X%02X%02X-"
185 "%02X%02X-"
186 "%02X%02X-"
187 "%02X%02X-"
188 "%02X%02X%02X%02X%02X%02X",
189 uu[0], uu[1], uu[2], uu[3],
190 uu[4], uu[5],
191 uu[6], uu[7],
192 uu[8], uu[9],
193 uu[10], uu[11], uu[12], uu[13], uu[14], uu[15]);
194}
195
196void
197uuid_unparse(const uuid_t uu, char *out)
198{
199 uuid_unparse_upper(uu, out);
200}