aboutsummaryrefslogtreecommitdiff
path: root/e2fsprogs/uuid
diff options
context:
space:
mode:
authorvapier <vapier@69ca8d6d-28ef-0310-b511-8ec308f3f277>2005-05-09 22:13:22 +0000
committervapier <vapier@69ca8d6d-28ef-0310-b511-8ec308f3f277>2005-05-09 22:13:22 +0000
commit11830e85185f7b859954515eed52fa78d2aa4679 (patch)
treeb86c6e23c1baa56577715cdeef6551d10a4ead2c /e2fsprogs/uuid
parent602f3364c07401359a4768ca1aa0d788367932c6 (diff)
downloadbusybox-w32-11830e85185f7b859954515eed52fa78d2aa4679.tar.gz
busybox-w32-11830e85185f7b859954515eed52fa78d2aa4679.tar.bz2
busybox-w32-11830e85185f7b859954515eed52fa78d2aa4679.zip
import more libs to prep for new e2fsprogs
git-svn-id: svn://busybox.net/trunk/busybox@10281 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'e2fsprogs/uuid')
-rw-r--r--e2fsprogs/uuid/clear.c43
-rw-r--r--e2fsprogs/uuid/compare.c55
-rw-r--r--e2fsprogs/uuid/copy.c45
-rw-r--r--e2fsprogs/uuid/gen_uuid.c310
-rw-r--r--e2fsprogs/uuid/isnull.c48
-rw-r--r--e2fsprogs/uuid/pack.c69
-rw-r--r--e2fsprogs/uuid/parse.c79
-rw-r--r--e2fsprogs/uuid/unpack.c63
-rw-r--r--e2fsprogs/uuid/unparse.c76
-rw-r--r--e2fsprogs/uuid/uuid.h101
-rw-r--r--e2fsprogs/uuid/uuidP.h63
-rw-r--r--e2fsprogs/uuid/uuid_time.c161
12 files changed, 1113 insertions, 0 deletions
diff --git a/e2fsprogs/uuid/clear.c b/e2fsprogs/uuid/clear.c
new file mode 100644
index 000000000..f3a1005b3
--- /dev/null
+++ b/e2fsprogs/uuid/clear.c
@@ -0,0 +1,43 @@
1/*
2 * clear.c -- Clear a UUID
3 *
4 * Copyright (C) 1996, 1997 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, and the entire permission notice in its entirety,
12 * including the disclaimer of warranties.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote
17 * products derived from this software without specific prior
18 * written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
21 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
23 * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
30 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
31 * DAMAGE.
32 * %End-Header%
33 */
34
35#include "string.h"
36
37#include "uuidP.h"
38
39void uuid_clear(uuid_t uu)
40{
41 memset(uu, 0, 16);
42}
43
diff --git a/e2fsprogs/uuid/compare.c b/e2fsprogs/uuid/compare.c
new file mode 100644
index 000000000..a9c505c74
--- /dev/null
+++ b/e2fsprogs/uuid/compare.c
@@ -0,0 +1,55 @@
1/*
2 * compare.c --- compare whether or not two UUID's are the same
3 *
4 * Returns 0 if the two UUID's are different, and 1 if they are the same.
5 *
6 * Copyright (C) 1996, 1997 Theodore Ts'o.
7 *
8 * %Begin-Header%
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, and the entire permission notice in its entirety,
14 * including the disclaimer of warranties.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The name of the author may not be used to endorse or promote
19 * products derived from this software without specific prior
20 * written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
25 * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
28 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
29 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
33 * DAMAGE.
34 * %End-Header%
35 */
36
37#include "uuidP.h"
38#include <string.h>
39
40#define UUCMP(u1,u2) if (u1 != u2) return((u1 < u2) ? -1 : 1);
41
42int uuid_compare(const uuid_t uu1, const uuid_t uu2)
43{
44 struct uuid uuid1, uuid2;
45
46 uuid_unpack(uu1, &uuid1);
47 uuid_unpack(uu2, &uuid2);
48
49 UUCMP(uuid1.time_low, uuid2.time_low);
50 UUCMP(uuid1.time_mid, uuid2.time_mid);
51 UUCMP(uuid1.time_hi_and_version, uuid2.time_hi_and_version);
52 UUCMP(uuid1.clock_seq, uuid2.clock_seq);
53 return memcmp(uuid1.node, uuid2.node, 6);
54}
55
diff --git a/e2fsprogs/uuid/copy.c b/e2fsprogs/uuid/copy.c
new file mode 100644
index 000000000..963bc818d
--- /dev/null
+++ b/e2fsprogs/uuid/copy.c
@@ -0,0 +1,45 @@
1/*
2 * copy.c --- copy UUIDs
3 *
4 * Copyright (C) 1996, 1997 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, and the entire permission notice in its entirety,
12 * including the disclaimer of warranties.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote
17 * products derived from this software without specific prior
18 * written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
21 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
23 * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
30 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
31 * DAMAGE.
32 * %End-Header%
33 */
34
35#include "uuidP.h"
36
37void uuid_copy(uuid_t dst, const uuid_t src)
38{
39 unsigned char *cp1;
40 const unsigned char *cp2;
41 int i;
42
43 for (i=0, cp1 = dst, cp2 = src; i < 16; i++)
44 *cp1++ = *cp2++;
45}
diff --git a/e2fsprogs/uuid/gen_uuid.c b/e2fsprogs/uuid/gen_uuid.c
new file mode 100644
index 000000000..489cdd2b9
--- /dev/null
+++ b/e2fsprogs/uuid/gen_uuid.c
@@ -0,0 +1,310 @@
1/*
2 * gen_uuid.c --- generate a DCE-compatible uuid
3 *
4 * Copyright (C) 1996, 1997, 1998, 1999 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, and the entire permission notice in its entirety,
12 * including the disclaimer of warranties.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote
17 * products derived from this software without specific prior
18 * written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
21 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
23 * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
30 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
31 * DAMAGE.
32 * %End-Header%
33 */
34
35#ifdef HAVE_UNISTD_H
36#include <unistd.h>
37#endif
38#ifdef HAVE_STDLIB_H
39#include <stdlib.h>
40#endif
41#include <string.h>
42#include <fcntl.h>
43#include <errno.h>
44#include <sys/types.h>
45#include <sys/time.h>
46#include <sys/stat.h>
47#include <sys/file.h>
48#ifdef HAVE_SYS_IOCTL_H
49#include <sys/ioctl.h>
50#endif
51#ifdef HAVE_SYS_SOCKET_H
52#include <sys/socket.h>
53#endif
54#ifdef HAVE_SYS_SOCKIO_H
55#include <sys/sockio.h>
56#endif
57#ifdef HAVE_NET_IF_H
58#include <net/if.h>
59#endif
60#ifdef HAVE_NETINET_IN_H
61#include <netinet/in.h>
62#endif
63#ifdef HAVE_NET_IF_DL_H
64#include <net/if_dl.h>
65#endif
66
67#include "uuidP.h"
68
69#ifdef HAVE_SRANDOM
70#define srand(x) srandom(x)
71#define rand() random()
72#endif
73
74static int get_random_fd(void)
75{
76 struct timeval tv;
77 static int fd = -2;
78 int i;
79
80 if (fd == -2) {
81 gettimeofday(&tv, 0);
82 fd = open("/dev/urandom", O_RDONLY);
83 if (fd == -1)
84 fd = open("/dev/random", O_RDONLY | O_NONBLOCK);
85 srand((getpid() << 16) ^ getuid() ^ tv.tv_sec ^ tv.tv_usec);
86 }
87 /* Crank the random number generator a few times */
88 gettimeofday(&tv, 0);
89 for (i = (tv.tv_sec ^ tv.tv_usec) & 0x1F; i > 0; i--)
90 rand();
91 return fd;
92}
93
94
95/*
96 * Generate a series of random bytes. Use /dev/urandom if possible,
97 * and if not, use srandom/random.
98 */
99static void get_random_bytes(void *buf, int nbytes)
100{
101 int i, n = nbytes, fd = get_random_fd();
102 int lose_counter = 0;
103 unsigned char *cp = (unsigned char *) buf;
104
105 if (fd >= 0) {
106 while (n > 0) {
107 i = read(fd, cp, n);
108 if (i <= 0) {
109 if (lose_counter++ > 16)
110 break;
111 continue;
112 }
113 n -= i;
114 cp += i;
115 lose_counter = 0;
116 }
117 }
118
119 /*
120 * We do this all the time, but this is the only source of
121 * randomness if /dev/random/urandom is out to lunch.
122 */
123 for (cp = buf, i = 0; i < nbytes; i++)
124 *cp++ ^= (rand() >> 7) & 0xFF;
125 return;
126}
127
128/*
129 * Get the ethernet hardware address, if we can find it...
130 */
131static int get_node_id(unsigned char *node_id)
132{
133#ifdef HAVE_NET_IF_H
134 int sd;
135 struct ifreq ifr, *ifrp;
136 struct ifconf ifc;
137 char buf[1024];
138 int n, i;
139 unsigned char *a;
140#ifdef HAVE_NET_IF_DL_H
141 struct sockaddr_dl *sdlp;
142#endif
143
144/*
145 * BSD 4.4 defines the size of an ifreq to be
146 * max(sizeof(ifreq), sizeof(ifreq.ifr_name)+ifreq.ifr_addr.sa_len
147 * However, under earlier systems, sa_len isn't present, so the size is
148 * just sizeof(struct ifreq)
149 */
150#ifdef HAVE_SA_LEN
151#ifndef max
152#define max(a,b) ((a) > (b) ? (a) : (b))
153#endif
154#define ifreq_size(i) max(sizeof(struct ifreq),\
155 sizeof((i).ifr_name)+(i).ifr_addr.sa_len)
156#else
157#define ifreq_size(i) sizeof(struct ifreq)
158#endif /* HAVE_SA_LEN*/
159
160 sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
161 if (sd < 0) {
162 return -1;
163 }
164 memset(buf, 0, sizeof(buf));
165 ifc.ifc_len = sizeof(buf);
166 ifc.ifc_buf = buf;
167 if (ioctl (sd, SIOCGIFCONF, (char *)&ifc) < 0) {
168 close(sd);
169 return -1;
170 }
171 n = ifc.ifc_len;
172 for (i = 0; i < n; i+= ifreq_size(*ifrp) ) {
173 ifrp = (struct ifreq *)((char *) ifc.ifc_buf+i);
174 strncpy(ifr.ifr_name, ifrp->ifr_name, IFNAMSIZ);
175#ifdef SIOCGIFHWADDR
176 if (ioctl(sd, SIOCGIFHWADDR, &ifr) < 0)
177 continue;
178 a = (unsigned char *) &ifr.ifr_hwaddr.sa_data;
179#else
180#ifdef SIOCGENADDR
181 if (ioctl(sd, SIOCGENADDR, &ifr) < 0)
182 continue;
183 a = (unsigned char *) ifr.ifr_enaddr;
184#else
185#ifdef HAVE_NET_IF_DL_H
186 sdlp = (struct sockaddr_dl *) &ifrp->ifr_addr;
187 if ((sdlp->sdl_family != AF_LINK) || (sdlp->sdl_alen != 6))
188 continue;
189 a = (unsigned char *) &sdlp->sdl_data[sdlp->sdl_nlen];
190#else
191 /*
192 * XXX we don't have a way of getting the hardware
193 * address
194 */
195 close(sd);
196 return 0;
197#endif /* HAVE_NET_IF_DL_H */
198#endif /* SIOCGENADDR */
199#endif /* SIOCGIFHWADDR */
200 if (!a[0] && !a[1] && !a[2] && !a[3] && !a[4] && !a[5])
201 continue;
202 if (node_id) {
203 memcpy(node_id, a, 6);
204 close(sd);
205 return 1;
206 }
207 }
208 close(sd);
209#endif
210 return 0;
211}
212
213/* Assume that the gettimeofday() has microsecond granularity */
214#define MAX_ADJUSTMENT 10
215
216static int get_clock(uint32_t *clock_high, uint32_t *clock_low, uint16_t *ret_clock_seq)
217{
218 static int adjustment = 0;
219 static struct timeval last = {0, 0};
220 static uint16_t clock_seq;
221 struct timeval tv;
222 unsigned long long clock_reg;
223
224try_again:
225 gettimeofday(&tv, 0);
226 if ((last.tv_sec == 0) && (last.tv_usec == 0)) {
227 get_random_bytes(&clock_seq, sizeof(clock_seq));
228 clock_seq &= 0x3FFF;
229 last = tv;
230 last.tv_sec--;
231 }
232 if ((tv.tv_sec < last.tv_sec) ||
233 ((tv.tv_sec == last.tv_sec) &&
234 (tv.tv_usec < last.tv_usec))) {
235 clock_seq = (clock_seq+1) & 0x3FFF;
236 adjustment = 0;
237 last = tv;
238 } else if ((tv.tv_sec == last.tv_sec) &&
239 (tv.tv_usec == last.tv_usec)) {
240 if (adjustment >= MAX_ADJUSTMENT)
241 goto try_again;
242 adjustment++;
243 } else {
244 adjustment = 0;
245 last = tv;
246 }
247
248 clock_reg = tv.tv_usec*10 + adjustment;
249 clock_reg += ((unsigned long long) tv.tv_sec)*10000000;
250 clock_reg += (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000;
251
252 *clock_high = clock_reg >> 32;
253 *clock_low = clock_reg;
254 *ret_clock_seq = clock_seq;
255 return 0;
256}
257
258void uuid_generate_time(uuid_t out)
259{
260 static unsigned char node_id[6];
261 static int has_init = 0;
262 struct uuid uu;
263 uint32_t clock_mid;
264
265 if (!has_init) {
266 if (get_node_id(node_id) <= 0) {
267 get_random_bytes(node_id, 6);
268 /*
269 * Set multicast bit, to prevent conflicts
270 * with IEEE 802 addresses obtained from
271 * network cards
272 */
273 node_id[0] |= 0x01;
274 }
275 has_init = 1;
276 }
277 get_clock(&clock_mid, &uu.time_low, &uu.clock_seq);
278 uu.clock_seq |= 0x8000;
279 uu.time_mid = (uint16_t) clock_mid;
280 uu.time_hi_and_version = ((clock_mid >> 16) & 0x0FFF) | 0x1000;
281 memcpy(uu.node, node_id, 6);
282 uuid_pack(&uu, out);
283}
284
285void uuid_generate_random(uuid_t out)
286{
287 uuid_t buf;
288 struct uuid uu;
289
290 get_random_bytes(buf, sizeof(buf));
291 uuid_unpack(buf, &uu);
292
293 uu.clock_seq = (uu.clock_seq & 0x3FFF) | 0x8000;
294 uu.time_hi_and_version = (uu.time_hi_and_version & 0x0FFF) | 0x4000;
295 uuid_pack(&uu, out);
296}
297
298/*
299 * This is the generic front-end to uuid_generate_random and
300 * uuid_generate_time. It uses uuid_generate_random only if
301 * /dev/urandom is available, since otherwise we won't have
302 * high-quality randomness.
303 */
304void uuid_generate(uuid_t out)
305{
306 if (get_random_fd() >= 0)
307 uuid_generate_random(out);
308 else
309 uuid_generate_time(out);
310}
diff --git a/e2fsprogs/uuid/isnull.c b/e2fsprogs/uuid/isnull.c
new file mode 100644
index 000000000..54a830038
--- /dev/null
+++ b/e2fsprogs/uuid/isnull.c
@@ -0,0 +1,48 @@
1/*
2 * isnull.c --- Check whether or not the UUID is null
3 *
4 * Copyright (C) 1996, 1997 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, and the entire permission notice in its entirety,
12 * including the disclaimer of warranties.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote
17 * products derived from this software without specific prior
18 * written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
21 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
23 * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
30 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
31 * DAMAGE.
32 * %End-Header%
33 */
34
35#include "uuidP.h"
36
37/* Returns 1 if the uuid is the NULL uuid */
38int uuid_is_null(const uuid_t uu)
39{
40 const unsigned char *cp;
41 int i;
42
43 for (i=0, cp = uu; i < 16; i++)
44 if (*cp++)
45 return 0;
46 return 1;
47}
48
diff --git a/e2fsprogs/uuid/pack.c b/e2fsprogs/uuid/pack.c
new file mode 100644
index 000000000..348d43213
--- /dev/null
+++ b/e2fsprogs/uuid/pack.c
@@ -0,0 +1,69 @@
1/*
2 * Internal routine for packing UUID's
3 *
4 * Copyright (C) 1996, 1997 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, and the entire permission notice in its entirety,
12 * including the disclaimer of warranties.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote
17 * products derived from this software without specific prior
18 * written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
21 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
23 * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
30 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
31 * DAMAGE.
32 * %End-Header%
33 */
34
35#include <string.h>
36#include "uuidP.h"
37
38void uuid_pack(const struct uuid *uu, uuid_t ptr)
39{
40 uint32_t tmp;
41 unsigned char *out = ptr;
42
43 tmp = uu->time_low;
44 out[3] = (unsigned char) tmp;
45 tmp >>= 8;
46 out[2] = (unsigned char) tmp;
47 tmp >>= 8;
48 out[1] = (unsigned char) tmp;
49 tmp >>= 8;
50 out[0] = (unsigned char) tmp;
51
52 tmp = uu->time_mid;
53 out[5] = (unsigned char) tmp;
54 tmp >>= 8;
55 out[4] = (unsigned char) tmp;
56
57 tmp = uu->time_hi_and_version;
58 out[7] = (unsigned char) tmp;
59 tmp >>= 8;
60 out[6] = (unsigned char) tmp;
61
62 tmp = uu->clock_seq;
63 out[9] = (unsigned char) tmp;
64 tmp >>= 8;
65 out[8] = (unsigned char) tmp;
66
67 memcpy(out+10, uu->node, 6);
68}
69
diff --git a/e2fsprogs/uuid/parse.c b/e2fsprogs/uuid/parse.c
new file mode 100644
index 000000000..07b894d11
--- /dev/null
+++ b/e2fsprogs/uuid/parse.c
@@ -0,0 +1,79 @@
1/*
2 * parse.c --- UUID parsing
3 *
4 * Copyright (C) 1996, 1997 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, and the entire permission notice in its entirety,
12 * including the disclaimer of warranties.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote
17 * products derived from this software without specific prior
18 * written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
21 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
23 * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
30 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
31 * DAMAGE.
32 * %End-Header%
33 */
34
35#include <stdlib.h>
36#include <stdio.h>
37#include <ctype.h>
38#include <string.h>
39
40#include "uuidP.h"
41
42int uuid_parse(const char *in, uuid_t uu)
43{
44 struct uuid uuid;
45 int i;
46 const char *cp;
47 char buf[3];
48
49 if (strlen(in) != 36)
50 return -1;
51 for (i=0, cp = in; i <= 36; i++,cp++) {
52 if ((i == 8) || (i == 13) || (i == 18) ||
53 (i == 23)) {
54 if (*cp == '-')
55 continue;
56 else
57 return -1;
58 }
59 if (i== 36)
60 if (*cp == 0)
61 continue;
62 if (!isxdigit(*cp))
63 return -1;
64 }
65 uuid.time_low = strtoul(in, NULL, 16);
66 uuid.time_mid = strtoul(in+9, NULL, 16);
67 uuid.time_hi_and_version = strtoul(in+14, NULL, 16);
68 uuid.clock_seq = strtoul(in+19, NULL, 16);
69 cp = in+24;
70 buf[2] = 0;
71 for (i=0; i < 6; i++) {
72 buf[0] = *cp++;
73 buf[1] = *cp++;
74 uuid.node[i] = strtoul(buf, NULL, 16);
75 }
76
77 uuid_pack(&uuid, uu);
78 return 0;
79}
diff --git a/e2fsprogs/uuid/unpack.c b/e2fsprogs/uuid/unpack.c
new file mode 100644
index 000000000..9502fc2a6
--- /dev/null
+++ b/e2fsprogs/uuid/unpack.c
@@ -0,0 +1,63 @@
1/*
2 * Internal routine for unpacking UUID
3 *
4 * Copyright (C) 1996, 1997 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, and the entire permission notice in its entirety,
12 * including the disclaimer of warranties.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote
17 * products derived from this software without specific prior
18 * written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
21 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
23 * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
30 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
31 * DAMAGE.
32 * %End-Header%
33 */
34
35#include <string.h>
36#include "uuidP.h"
37
38void uuid_unpack(const uuid_t in, struct uuid *uu)
39{
40 const uint8_t *ptr = in;
41 uint32_t tmp;
42
43 tmp = *ptr++;
44 tmp = (tmp << 8) | *ptr++;
45 tmp = (tmp << 8) | *ptr++;
46 tmp = (tmp << 8) | *ptr++;
47 uu->time_low = tmp;
48
49 tmp = *ptr++;
50 tmp = (tmp << 8) | *ptr++;
51 uu->time_mid = tmp;
52
53 tmp = *ptr++;
54 tmp = (tmp << 8) | *ptr++;
55 uu->time_hi_and_version = tmp;
56
57 tmp = *ptr++;
58 tmp = (tmp << 8) | *ptr++;
59 uu->clock_seq = tmp;
60
61 memcpy(uu->node, ptr, 6);
62}
63
diff --git a/e2fsprogs/uuid/unparse.c b/e2fsprogs/uuid/unparse.c
new file mode 100644
index 000000000..c0e08ef49
--- /dev/null
+++ b/e2fsprogs/uuid/unparse.c
@@ -0,0 +1,76 @@
1/*
2 * unparse.c -- convert a UUID to string
3 *
4 * Copyright (C) 1996, 1997 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, and the entire permission notice in its entirety,
12 * including the disclaimer of warranties.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote
17 * products derived from this software without specific prior
18 * written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
21 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
23 * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
30 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
31 * DAMAGE.
32 * %End-Header%
33 */
34
35#include <stdio.h>
36
37#include "uuidP.h"
38
39static const char *fmt_lower =
40 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x";
41
42static const char *fmt_upper =
43 "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X";
44
45#ifdef UUID_UNPARSE_DEFAULT_UPPER
46#define FMT_DEFAULT fmt_upper
47#else
48#define FMT_DEFAULT fmt_lower
49#endif
50
51static void uuid_unparse_x(const uuid_t uu, char *out, const char *fmt)
52{
53 struct uuid uuid;
54
55 uuid_unpack(uu, &uuid);
56 sprintf(out, fmt,
57 uuid.time_low, uuid.time_mid, uuid.time_hi_and_version,
58 uuid.clock_seq >> 8, uuid.clock_seq & 0xFF,
59 uuid.node[0], uuid.node[1], uuid.node[2],
60 uuid.node[3], uuid.node[4], uuid.node[5]);
61}
62
63void uuid_unparse_lower(const uuid_t uu, char *out)
64{
65 uuid_unparse_x(uu, out, fmt_lower);
66}
67
68void uuid_unparse_upper(const uuid_t uu, char *out)
69{
70 uuid_unparse_x(uu, out, fmt_upper);
71}
72
73void uuid_unparse(const uuid_t uu, char *out)
74{
75 uuid_unparse_x(uu, out, FMT_DEFAULT);
76}
diff --git a/e2fsprogs/uuid/uuid.h b/e2fsprogs/uuid/uuid.h
new file mode 100644
index 000000000..e9cf889ae
--- /dev/null
+++ b/e2fsprogs/uuid/uuid.h
@@ -0,0 +1,101 @@
1/*
2 * Public include file for the UUID library
3 *
4 * Copyright (C) 1996, 1997, 1998 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, and the entire permission notice in its entirety,
12 * including the disclaimer of warranties.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote
17 * products derived from this software without specific prior
18 * written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
21 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
23 * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
30 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
31 * DAMAGE.
32 * %End-Header%
33 */
34
35#ifndef _UUID_UUID_H
36#define _UUID_UUID_H
37
38#include <sys/types.h>
39#include <sys/time.h>
40#include <time.h>
41
42typedef unsigned char uuid_t[16];
43
44/* UUID Variant definitions */
45#define UUID_VARIANT_NCS 0
46#define UUID_VARIANT_DCE 1
47#define UUID_VARIANT_MICROSOFT 2
48#define UUID_VARIANT_OTHER 3
49
50/* UUID Type definitions */
51#define UUID_TYPE_DCE_TIME 1
52#define UUID_TYPE_DCE_RANDOM 4
53
54/* Allow UUID constants to be defined */
55#ifdef __GNUC__
56#define UUID_DEFINE(name,u0,u1,u2,u3,u4,u5,u6,u7,u8,u9,u10,u11,u12,u13,u14,u15) \
57 static const uuid_t name __attribute__ ((unused)) = {u0,u1,u2,u3,u4,u5,u6,u7,u8,u9,u10,u11,u12,u13,u14,u15}
58#else
59#define UUID_DEFINE(name,u0,u1,u2,u3,u4,u5,u6,u7,u8,u9,u10,u11,u12,u13,u14,u15) \
60 static const uuid_t name = {u0,u1,u2,u3,u4,u5,u6,u7,u8,u9,u10,u11,u12,u13,u14,u15}
61#endif
62
63#ifdef __cplusplus
64extern "C" {
65#endif
66
67/* clear.c */
68void uuid_clear(uuid_t uu);
69
70/* compare.c */
71int uuid_compare(const uuid_t uu1, const uuid_t uu2);
72
73/* copy.c */
74void uuid_copy(uuid_t dst, const uuid_t src);
75
76/* gen_uuid.c */
77void uuid_generate(uuid_t out);
78void uuid_generate_random(uuid_t out);
79void uuid_generate_time(uuid_t out);
80
81/* isnull.c */
82int uuid_is_null(const uuid_t uu);
83
84/* parse.c */
85int uuid_parse(const char *in, uuid_t uu);
86
87/* unparse.c */
88void uuid_unparse(const uuid_t uu, char *out);
89void uuid_unparse_lower(const uuid_t uu, char *out);
90void uuid_unparse_upper(const uuid_t uu, char *out);
91
92/* uuid_time.c */
93time_t uuid_time(const uuid_t uu, struct timeval *ret_tv);
94int uuid_type(const uuid_t uu);
95int uuid_variant(const uuid_t uu);
96
97#ifdef __cplusplus
98}
99#endif
100
101#endif /* _UUID_UUID_H */
diff --git a/e2fsprogs/uuid/uuidP.h b/e2fsprogs/uuid/uuidP.h
new file mode 100644
index 000000000..adf233da9
--- /dev/null
+++ b/e2fsprogs/uuid/uuidP.h
@@ -0,0 +1,63 @@
1/*
2 * uuid.h -- private header file for uuids
3 *
4 * Copyright (C) 1996, 1997 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, and the entire permission notice in its entirety,
12 * including the disclaimer of warranties.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote
17 * products derived from this software without specific prior
18 * written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
21 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
23 * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
30 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
31 * DAMAGE.
32 * %End-Header%
33 */
34
35#ifdef HAVE_INTTYPES_H
36#include <inttypes.h>
37#else
38#include <uuid/uuid_types.h>
39#endif
40#include <sys/types.h>
41
42#include "uuid.h"
43
44/*
45 * Offset between 15-Oct-1582 and 1-Jan-70
46 */
47#define TIME_OFFSET_HIGH 0x01B21DD2
48#define TIME_OFFSET_LOW 0x13814000
49
50struct uuid {
51 uint32_t time_low;
52 uint16_t time_mid;
53 uint16_t time_hi_and_version;
54 uint16_t clock_seq;
55 uint8_t node[6];
56};
57
58
59/*
60 * prototypes
61 */
62void uuid_pack(const struct uuid *uu, uuid_t ptr);
63void uuid_unpack(const uuid_t in, struct uuid *uu);
diff --git a/e2fsprogs/uuid/uuid_time.c b/e2fsprogs/uuid/uuid_time.c
new file mode 100644
index 000000000..d5f992b39
--- /dev/null
+++ b/e2fsprogs/uuid/uuid_time.c
@@ -0,0 +1,161 @@
1/*
2 * uuid_time.c --- Interpret the time field from a uuid. This program
3 * violates the UUID abstraction barrier by reaching into the guts
4 * of a UUID and interpreting it.
5 *
6 * Copyright (C) 1998, 1999 Theodore Ts'o.
7 *
8 * %Begin-Header%
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, and the entire permission notice in its entirety,
14 * including the disclaimer of warranties.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The name of the author may not be used to endorse or promote
19 * products derived from this software without specific prior
20 * written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
25 * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
28 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
29 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
33 * DAMAGE.
34 * %End-Header%
35 */
36
37#include <stdio.h>
38#include <unistd.h>
39#include <stdlib.h>
40#include <sys/types.h>
41#include <sys/time.h>
42#include <time.h>
43
44#include "uuidP.h"
45
46time_t uuid_time(const uuid_t uu, struct timeval *ret_tv)
47{
48 struct uuid uuid;
49 uint32_t high;
50 struct timeval tv;
51 unsigned long long clock_reg;
52
53 uuid_unpack(uu, &uuid);
54
55 high = uuid.time_mid | ((uuid.time_hi_and_version & 0xFFF) << 16);
56 clock_reg = uuid.time_low | ((unsigned long long) high << 32);
57
58 clock_reg -= (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000;
59 tv.tv_sec = clock_reg / 10000000;
60 tv.tv_usec = (clock_reg % 10000000) / 10;
61
62 if (ret_tv)
63 *ret_tv = tv;
64
65 return tv.tv_sec;
66}
67
68int uuid_type(const uuid_t uu)
69{
70 struct uuid uuid;
71
72 uuid_unpack(uu, &uuid);
73 return ((uuid.time_hi_and_version >> 12) & 0xF);
74}
75
76int uuid_variant(const uuid_t uu)
77{
78 struct uuid uuid;
79 int var;
80
81 uuid_unpack(uu, &uuid);
82 var = uuid.clock_seq;
83
84 if ((var & 0x8000) == 0)
85 return UUID_VARIANT_NCS;
86 if ((var & 0x4000) == 0)
87 return UUID_VARIANT_DCE;
88 if ((var & 0x2000) == 0)
89 return UUID_VARIANT_MICROSOFT;
90 return UUID_VARIANT_OTHER;
91}
92
93#ifdef DEBUG
94static const char *variant_string(int variant)
95{
96 switch (variant) {
97 case UUID_VARIANT_NCS:
98 return "NCS";
99 case UUID_VARIANT_DCE:
100 return "DCE";
101 case UUID_VARIANT_MICROSOFT:
102 return "Microsoft";
103 default:
104 return "Other";
105 }
106}
107
108
109int
110main(int argc, char **argv)
111{
112 uuid_t buf;
113 time_t time_reg;
114 struct timeval tv;
115 int type, variant;
116
117 if (argc != 2) {
118 fprintf(stderr, "Usage: %s uuid\n", argv[0]);
119 exit(1);
120 }
121 if (uuid_parse(argv[1], buf)) {
122 fprintf(stderr, "Invalid UUID: %s\n", argv[1]);
123 exit(1);
124 }
125 variant = uuid_variant(buf);
126 type = uuid_type(buf);
127 time_reg = uuid_time(buf, &tv);
128
129 printf("UUID variant is %d (%s)\n", variant, variant_string(variant));
130 if (variant != UUID_VARIANT_DCE) {
131 printf("Warning: This program only knows how to interpret "
132 "DCE UUIDs.\n\tThe rest of the output is likely "
133 "to be incorrect!!\n");
134 }
135 printf("UUID type is %d", type);
136 switch (type) {
137 case 1:
138 printf(" (time based)\n");
139 break;
140 case 2:
141 printf(" (DCE)\n");
142 break;
143 case 3:
144 printf(" (name-based)\n");
145 break;
146 case 4:
147 printf(" (random)\n");
148 break;
149 default:
150 printf("\n");
151 }
152 if (type != 1) {
153 printf("Warning: not a time-based UUID, so UUID time "
154 "decoding will likely not work!\n");
155 }
156 printf("UUID time is: (%ld, %ld): %s\n", tv.tv_sec, tv.tv_usec,
157 ctime(&time_reg));
158
159 return 0;
160}
161#endif