]>
git.saurik.com Git - apple/xnu.git/blob - libkern/uuid/uuid.c
2 * Copyright (c) 2004-2011 Apple Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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
33 #include <uuid/uuid.h>
38 #include <sys/random.h>
39 #include <sys/socket.h>
40 #include <sys/systm.h>
43 extern int uuid_get_ethernet(u_int8_t
*);
45 UUID_DEFINE(UUID_NULL
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
48 read_node(uint8_t *node
)
51 if (uuid_get_ethernet(node
) == 0)
53 #endif /* NETWORKING */
66 return (tv
.tv_sec
* 10000000ULL) + (tv
.tv_nsec
/ 100ULL) + 0x01B21DD213814000ULL
;
72 memset(uu
, 0, sizeof(uuid_t
));
76 uuid_compare(const uuid_t uu1
, const uuid_t uu2
)
78 return memcmp(uu1
, uu2
, sizeof(uuid_t
));
82 uuid_copy(uuid_t dst
, const uuid_t src
)
84 memcpy(dst
, src
, sizeof(uuid_t
));
88 uuid_generate_random(uuid_t out
)
90 read_random(out
, sizeof(uuid_t
));
92 out
[6] = (out
[6] & 0x0F) | 0x40;
93 out
[8] = (out
[8] & 0x3F) | 0x80;
97 uuid_generate_time(uuid_t out
)
102 read_random(&out
[8], 2);
105 out
[0] = (uint8_t)(time
>> 24);
106 out
[1] = (uint8_t)(time
>> 16);
107 out
[2] = (uint8_t)(time
>> 8);
108 out
[3] = (uint8_t)time
;
109 out
[4] = (uint8_t)(time
>> 40);
110 out
[5] = (uint8_t)(time
>> 32);
111 out
[6] = (uint8_t)(time
>> 56);
112 out
[7] = (uint8_t)(time
>> 48);
114 out
[6] = (out
[6] & 0x0F) | 0x10;
115 out
[8] = (out
[8] & 0x3F) | 0x80;
119 uuid_generate(uuid_t out
)
121 uuid_generate_random(out
);
125 uuid_is_null(const uuid_t uu
)
127 return !memcmp(uu
, UUID_NULL
, sizeof(uuid_t
));
131 uuid_parse(const uuid_string_t in
, uuid_t uu
)
136 "%2hhx%2hhx%2hhx%2hhx-"
140 "%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%n",
141 &uu
[0], &uu
[1], &uu
[2], &uu
[3],
145 &uu
[10], &uu
[11], &uu
[12], &uu
[13], &uu
[14], &uu
[15], &n
);
147 return (n
!= 36 || in
[n
] != '\0' ? -1 : 0);
151 uuid_unparse_lower(const uuid_t uu
, uuid_string_t out
)
154 sizeof(uuid_string_t
),
159 "%02x%02x%02x%02x%02x%02x",
160 uu
[0], uu
[1], uu
[2], uu
[3],
164 uu
[10], uu
[11], uu
[12], uu
[13], uu
[14], uu
[15]);
168 uuid_unparse_upper(const uuid_t uu
, uuid_string_t out
)
171 sizeof(uuid_string_t
),
176 "%02X%02X%02X%02X%02X%02X",
177 uu
[0], uu
[1], uu
[2], uu
[3],
181 uu
[10], uu
[11], uu
[12], uu
[13], uu
[14], uu
[15]);
185 uuid_unparse(const uuid_t uu
, uuid_string_t out
)
187 uuid_unparse_upper(uu
, out
);