Debian Codegraph @

libuuid/src/uuid_time.c

raw
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 #ifdef _WIN32
38 #define _WIN32_WINNT 0x0500
39 #include <windows.h>
40 #define UUID MYUUID
41 #endif
42
43 #include <errno.h>
44 #include <stdio.h>
45 #ifdef HAVE_UNISTD_H
46 #include <unistd.h>
47 #endif
48 #include <stdlib.h>
49 #include <sys/types.h>
50 #ifdef HAVE_SYS_TIME_H
51 #include <sys/time.h>
52 #endif
53 #include <time.h>
54
55 #include "uuidP.h"
56 #include "timeutils.h"
57
58 #undef uuid_time
59
60 /* prototype to make compiler happy */
61 time_t __uuid_time(const uuid_t uu, struct timeval *ret_tv);
62
63 static void gregorian_to_unix(uint64_t ts, struct timeval *tv)
64 {
65 const uint64_t offset = 0x01B21DD213814000ULL;
66 int64_t clock_reg = (int64_t)(ts - offset);
67 tv->tv_sec = clock_reg / 10000000;
68 tv->tv_usec = (clock_reg % 10000000) / 10;
69 }
70
71 static void uuid_time_v1(const struct uuid *uuid, struct timeval *tv)
72 {
73 uint32_t high;
74 uint64_t clock_reg;
75
76 high = uuid->time_mid | ((uuid->time_hi_and_version & 0xFFF) << 16);
77 clock_reg = uuid->time_low | ((uint64_t) high << 32);
78
79 gregorian_to_unix(clock_reg, tv);
80 }
81
82 static void uuid_time_v6(const struct uuid *uuid, struct timeval *tv)
83 {
84 uint64_t clock_reg;
85
86 clock_reg = uuid->time_low;
87 clock_reg <<= 16;
88 clock_reg |= uuid->time_mid;
89 clock_reg <<= 12;
90 clock_reg |= uuid->time_hi_and_version & 0xFFF;
91
92 gregorian_to_unix(clock_reg, tv);
93 }
94
95 static void uuid_time_v7(const struct uuid *uuid, struct timeval *tv)
96 {
97 uint64_t clock_reg;
98
99 clock_reg = uuid->time_low;
100 clock_reg <<= 16;
101 clock_reg |= uuid->time_mid;
102
103 tv->tv_sec = clock_reg / MSEC_PER_SEC;
104 tv->tv_usec = (clock_reg % MSEC_PER_SEC) * USEC_PER_MSEC;
105 }
106
107 static uint8_t __uuid_type(const struct uuid *uuid)
108 {
109 return (uuid->time_hi_and_version >> 12) & 0xF;
110 }
111
112 /* this function could be 32bit time_t and 32bit timeval or 64bit,
113 depending on compiler flags and architecture. */
114 time_t __uuid_time(const uuid_t uu, struct timeval *ret_tv)
115 {
116 struct timeval tv;
117 struct uuid uuid;
118 uint8_t type;
119
120 uuid_unpack(uu, &uuid);
121 type = __uuid_type(&uuid);
122
123 switch (type) {
124 case UUID_TYPE_DCE_TIME:
125 uuid_time_v1(&uuid, &tv);
126 break;
127 case UUID_TYPE_DCE_TIME_V6:
128 uuid_time_v6(&uuid, &tv);
129 break;
130 case UUID_TYPE_DCE_TIME_V7:
131 uuid_time_v7(&uuid, &tv);
132 break;
133 default:
134 tv.tv_sec = -1;
135 tv.tv_usec = -1;
136 break;
137 }
138
139 if (ret_tv)
140 *ret_tv = tv;
141
142 return tv.tv_sec;
143 }
144 #if defined(__USE_TIME_BITS64) && defined(__GLIBC__)
145 extern time_t uuid_time64(const uuid_t uu, struct timeval *ret_tv) __attribute__((weak, alias("__uuid_time")));
146 #elif defined(__clang__) && defined(__APPLE__)
147 __asm__(".globl _uuid_time");
148 __asm__(".set _uuid_time, ___uuid_time");
149 extern time_t uuid_time(const uuid_t uu, struct timeval *ret_tv);
150 #else
151 extern time_t uuid_time(const uuid_t uu, struct timeval *ret_tv) __attribute__((weak, alias("__uuid_time")));
152 #endif
153
154 #if defined(__USE_TIME_BITS64) && defined(__GLIBC__)
155 struct timeval32
156 {
157 int32_t tv_sec;
158 int32_t tv_usec;
159 };
160
161 /* prototype to make compiler happy */
162 int32_t __uuid_time32(const uuid_t uu, struct timeval32 *ret_tv);
163
164 /* Check whether time fits in 32bit time_t. */
165 static inline int
166 in_time32_t_range(time_t t)
167 {
168 int32_t s;
169
170 s = t;
171
172 return s == t;
173 }
174
175 int32_t __uuid_time32(const uuid_t uu, struct timeval32 *ret_tv)
176 {
177 struct timeval tv;
178 time_t ret_time = __uuid_time(uu, &tv);
179
180 if (! in_time32_t_range(ret_time)) {
181 ret_tv->tv_sec = -1;
182 ret_tv->tv_usec = -1;
183 errno = EOVERFLOW;
184 return -1;
185 }
186
187 if (ret_tv) {
188 ret_tv->tv_sec = tv.tv_sec;
189 ret_tv->tv_usec = tv.tv_usec;
190 }
191
192 return tv.tv_sec;
193 }
194 extern int32_t uuid_time(const uuid_t uu, struct timeval32 *ret_tv) __attribute__((weak, alias("__uuid_time32")));
195 #endif
196
197 int uuid_type(const uuid_t uu)
198 {
199 struct uuid uuid;
200
201 uuid_unpack(uu, &uuid);
202 return __uuid_type(&uuid);
203 }
204
205 int uuid_variant(const uuid_t uu)
206 {
207 struct uuid uuid;
208 int var;
209
210 uuid_unpack(uu, &uuid);
211 var = uuid.clock_seq;
212
213 if ((var & 0x8000) == 0)
214 return UUID_VARIANT_NCS;
215 if ((var & 0x4000) == 0)
216 return UUID_VARIANT_DCE;
217 if ((var & 0x2000) == 0)
218 return UUID_VARIANT_MICROSOFT;
219 return UUID_VARIANT_OTHER;
220 }
221
222 #ifdef DEBUG
223 static const char *variant_string(int variant)
224 {
225 switch (variant) {
226 case UUID_VARIANT_NCS:
227 return "NCS";
228 case UUID_VARIANT_DCE:
229 return "DCE";
230 case UUID_VARIANT_MICROSOFT:
231 return "Microsoft";
232 default:
233 return "Other";
234 }
235 }
236
237
238 int
239 main(int argc, char **argv)
240 {
241 uuid_t buf;
242 time_t time_reg;
243 struct timeval tv;
244 int type, variant;
245
246 if (argc != 2) {
247 fprintf(stderr, "Usage: %s uuid\n", argv[0]);
248 exit(1);
249 }
250 if (uuid_parse(argv[1], buf)) {
251 fprintf(stderr, "Invalid UUID: %s\n", argv[1]);
252 exit(1);
253 }
254 variant = uuid_variant(buf);
255 type = uuid_type(buf);
256 time_reg = uuid_time(buf, &tv);
257
258 printf("UUID variant is %d (%s)\n", variant, variant_string(variant));
259 if (variant != UUID_VARIANT_DCE) {
260 printf("Warning: This program only knows how to interpret "
261 "DCE UUIDs.\n\tThe rest of the output is likely "
262 "to be incorrect!!\n");
263 }
264 printf("UUID type is %d", type);
265 switch (type) {
266 case 1:
267 printf(" (time based)\n");
268 break;
269 case 2:
270 printf(" (DCE)\n");
271 break;
272 case 3:
273 printf(" (name-based)\n");
274 break;
275 case 4:
276 printf(" (random)\n");
277 break;
278 default:
279 printf("\n");
280 }
281 if (type != 1) {
282 printf("Warning: not a time-based UUID, so UUID time "
283 "decoding will likely not work!\n");
284 }
285 printf("UUID time is: (%"PRId64", %"PRId64"): %s\n",
286 (int64_t)tv.tv_sec, (int64_t)tv.tv_usec, ctime(&time_reg));
287
288 return 0;
289 }
290 #endif