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 "config.h"
38
39 #include <stdio.h>
40 #ifdef HAVE_UNISTD_H
41 #include <unistd.h>
42 #endif
43 #include <stdlib.h>
44 #include <sys/types.h>
45 #ifdef HAVE_SYS_TIME_H
46 #include <sys/time.h>
47 #endif
48 #include <time.h>
49
50 #include "uuidP.h"
51
uuid_time(const uuid_t uu,struct timeval * ret_tv)52 time_t uuid_time(const uuid_t uu, struct timeval *ret_tv)
53 {
54 struct timeval tv;
55 struct uuid uuid;
56 uint32_t high;
57 uint64_t clock_reg;
58
59 uuid_unpack(uu, &uuid);
60
61 high = uuid.time_mid | ((uuid.time_hi_and_version & 0xFFF) << 16);
62 clock_reg = uuid.time_low | ((uint64_t) high << 32);
63
64 clock_reg -= (((uint64_t) 0x01B21DD2) << 32) + 0x13814000;
65 tv.tv_sec = clock_reg / 10000000;
66 tv.tv_usec = (clock_reg % 10000000) / 10;
67
68 if (ret_tv)
69 *ret_tv = tv;
70
71 return tv.tv_sec;
72 }
73
uuid_type(const uuid_t uu)74 int uuid_type(const uuid_t uu)
75 {
76 struct uuid uuid;
77
78 uuid_unpack(uu, &uuid);
79 return ((uuid.time_hi_and_version >> 12) & 0xF);
80 }
81
uuid_variant(const uuid_t uu)82 int uuid_variant(const uuid_t uu)
83 {
84 struct uuid uuid;
85 int var;
86
87 uuid_unpack(uu, &uuid);
88 var = uuid.clock_seq;
89
90 if ((var & 0x8000) == 0)
91 return UUID_VARIANT_NCS;
92 if ((var & 0x4000) == 0)
93 return UUID_VARIANT_DCE;
94 if ((var & 0x2000) == 0)
95 return UUID_VARIANT_MICROSOFT;
96 return UUID_VARIANT_OTHER;
97 }
98
99 #ifdef DEBUG
variant_string(int variant)100 static const char *variant_string(int variant)
101 {
102 switch (variant) {
103 case UUID_VARIANT_NCS:
104 return "NCS";
105 case UUID_VARIANT_DCE:
106 return "DCE";
107 case UUID_VARIANT_MICROSOFT:
108 return "Microsoft";
109 default:
110 return "Other";
111 }
112 }
113
114
115 int
main(int argc,char ** argv)116 main(int argc, char **argv)
117 {
118 uuid_t buf;
119 time_t time_reg;
120 struct timeval tv;
121 int type, variant;
122
123 if (argc != 2) {
124 fprintf(stderr, "Usage: %s uuid\n", argv[0]);
125 exit(1);
126 }
127 if (uuid_parse(argv[1], buf)) {
128 fprintf(stderr, "Invalid UUID: %s\n", argv[1]);
129 exit(1);
130 }
131 variant = uuid_variant(buf);
132 type = uuid_type(buf);
133 time_reg = uuid_time(buf, &tv);
134
135 printf("UUID variant is %d (%s)\n", variant, variant_string(variant));
136 if (variant != UUID_VARIANT_DCE) {
137 printf("Warning: This program only knows how to interpret "
138 "DCE UUIDs.\n\tThe rest of the output is likely "
139 "to be incorrect!!\n");
140 }
141 printf("UUID type is %d", type);
142 switch (type) {
143 case 1:
144 printf(" (time based)\n");
145 break;
146 case 2:
147 printf(" (DCE)\n");
148 break;
149 case 3:
150 printf(" (name-based)\n");
151 break;
152 case 4:
153 printf(" (random)\n");
154 break;
155 default:
156 printf("\n");
157 }
158 if (type != 1) {
159 printf("Warning: not a time-based UUID, so UUID time "
160 "decoding will likely not work!\n");
161 }
162 printf("UUID time is: (%ld, %ld): %s\n", (long)tv.tv_sec, (long)tv.tv_usec,
163 ctime(&time_reg));
164
165 return 0;
166 }
167 #endif
168