Theodore Ts'o | 1e0a221 | 1998-12-04 08:13:52 +0000 | [diff] [blame] | 1 | /* |
Theodore Ts'o | b19d1a9 | 1999-06-18 00:32:03 +0000 | [diff] [blame] | 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. |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 5 | * |
Theodore Ts'o | b19d1a9 | 1999-06-18 00:32:03 +0000 | [diff] [blame] | 6 | * Copyright (C) 1998, 1999 Theodore Ts'o. |
Theodore Ts'o | 1e0a221 | 1998-12-04 08:13:52 +0000 | [diff] [blame] | 7 | * |
| 8 | * %Begin-Header% |
Theodore Ts'o | 1bbfec6 | 2004-03-20 14:02:24 -0500 | [diff] [blame] | 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. |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 21 | * |
Theodore Ts'o | 1bbfec6 | 2004-03-20 14:02:24 -0500 | [diff] [blame] | 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. |
Theodore Ts'o | 1e0a221 | 1998-12-04 08:13:52 +0000 | [diff] [blame] | 34 | * %End-Header% |
| 35 | */ |
| 36 | |
Theodore Ts'o | d1154eb | 2011-09-18 17:34:37 -0400 | [diff] [blame] | 37 | #include "config.h" |
| 38 | |
Theodore Ts'o | 6ec9ef1 | 2008-08-22 12:15:56 -0400 | [diff] [blame] | 39 | #ifdef _WIN32 |
| 40 | #define _WIN32_WINNT 0x0500 |
| 41 | #include <windows.h> |
| 42 | #define UUID MYUUID |
| 43 | #endif |
| 44 | |
Theodore Ts'o | 1e0a221 | 1998-12-04 08:13:52 +0000 | [diff] [blame] | 45 | #include <stdio.h> |
Theodore Ts'o | 6ec9ef1 | 2008-08-22 12:15:56 -0400 | [diff] [blame] | 46 | #ifdef HAVE_UNISTD_H |
Theodore Ts'o | 1e0a221 | 1998-12-04 08:13:52 +0000 | [diff] [blame] | 47 | #include <unistd.h> |
Theodore Ts'o | 6ec9ef1 | 2008-08-22 12:15:56 -0400 | [diff] [blame] | 48 | #endif |
Theodore Ts'o | 1e0a221 | 1998-12-04 08:13:52 +0000 | [diff] [blame] | 49 | #include <stdlib.h> |
| 50 | #include <sys/types.h> |
Theodore Ts'o | 6ec9ef1 | 2008-08-22 12:15:56 -0400 | [diff] [blame] | 51 | #ifdef HAVE_SYS_TIME_H |
Theodore Ts'o | 1e0a221 | 1998-12-04 08:13:52 +0000 | [diff] [blame] | 52 | #include <sys/time.h> |
Theodore Ts'o | 6ec9ef1 | 2008-08-22 12:15:56 -0400 | [diff] [blame] | 53 | #endif |
Theodore Ts'o | 1e0a221 | 1998-12-04 08:13:52 +0000 | [diff] [blame] | 54 | #include <time.h> |
Theodore Ts'o | 1e0a221 | 1998-12-04 08:13:52 +0000 | [diff] [blame] | 55 | |
| 56 | #include "uuidP.h" |
| 57 | |
Theodore Ts'o | ce2722f | 2001-09-10 20:30:09 -0400 | [diff] [blame] | 58 | time_t uuid_time(const uuid_t uu, struct timeval *ret_tv) |
Theodore Ts'o | 1e0a221 | 1998-12-04 08:13:52 +0000 | [diff] [blame] | 59 | { |
Theodore Ts'o | 1e0a221 | 1998-12-04 08:13:52 +0000 | [diff] [blame] | 60 | struct timeval tv; |
Theodore Ts'o | 6ec9ef1 | 2008-08-22 12:15:56 -0400 | [diff] [blame] | 61 | struct uuid uuid; |
| 62 | uint32_t high; |
| 63 | uint64_t clock_reg; |
Theodore Ts'o | 1e0a221 | 1998-12-04 08:13:52 +0000 | [diff] [blame] | 64 | |
| 65 | uuid_unpack(uu, &uuid); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 66 | |
Theodore Ts'o | 1e0a221 | 1998-12-04 08:13:52 +0000 | [diff] [blame] | 67 | high = uuid.time_mid | ((uuid.time_hi_and_version & 0xFFF) << 16); |
Theodore Ts'o | 6ec9ef1 | 2008-08-22 12:15:56 -0400 | [diff] [blame] | 68 | clock_reg = uuid.time_low | ((uint64_t) high << 32); |
Theodore Ts'o | 1e0a221 | 1998-12-04 08:13:52 +0000 | [diff] [blame] | 69 | |
Theodore Ts'o | 6ec9ef1 | 2008-08-22 12:15:56 -0400 | [diff] [blame] | 70 | clock_reg -= (((uint64_t) 0x01B21DD2) << 32) + 0x13814000; |
Theodore Ts'o | 1e0a221 | 1998-12-04 08:13:52 +0000 | [diff] [blame] | 71 | tv.tv_sec = clock_reg / 10000000; |
| 72 | tv.tv_usec = (clock_reg % 10000000) / 10; |
| 73 | |
| 74 | if (ret_tv) |
| 75 | *ret_tv = tv; |
| 76 | |
| 77 | return tv.tv_sec; |
| 78 | } |
| 79 | |
Theodore Ts'o | ce2722f | 2001-09-10 20:30:09 -0400 | [diff] [blame] | 80 | int uuid_type(const uuid_t uu) |
Theodore Ts'o | b19d1a9 | 1999-06-18 00:32:03 +0000 | [diff] [blame] | 81 | { |
| 82 | struct uuid uuid; |
| 83 | |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 84 | uuid_unpack(uu, &uuid); |
Theodore Ts'o | b19d1a9 | 1999-06-18 00:32:03 +0000 | [diff] [blame] | 85 | return ((uuid.time_hi_and_version >> 12) & 0xF); |
| 86 | } |
| 87 | |
Theodore Ts'o | ce2722f | 2001-09-10 20:30:09 -0400 | [diff] [blame] | 88 | int uuid_variant(const uuid_t uu) |
Theodore Ts'o | b19d1a9 | 1999-06-18 00:32:03 +0000 | [diff] [blame] | 89 | { |
| 90 | struct uuid uuid; |
| 91 | int var; |
| 92 | |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 93 | uuid_unpack(uu, &uuid); |
Theodore Ts'o | b19d1a9 | 1999-06-18 00:32:03 +0000 | [diff] [blame] | 94 | var = uuid.clock_seq; |
| 95 | |
| 96 | if ((var & 0x8000) == 0) |
| 97 | return UUID_VARIANT_NCS; |
| 98 | if ((var & 0x4000) == 0) |
| 99 | return UUID_VARIANT_DCE; |
| 100 | if ((var & 0x2000) == 0) |
| 101 | return UUID_VARIANT_MICROSOFT; |
| 102 | return UUID_VARIANT_OTHER; |
| 103 | } |
| 104 | |
Theodore Ts'o | 1e0a221 | 1998-12-04 08:13:52 +0000 | [diff] [blame] | 105 | #ifdef DEBUG |
Theodore Ts'o | 36caf25 | 1999-10-26 14:29:22 +0000 | [diff] [blame] | 106 | static const char *variant_string(int variant) |
Theodore Ts'o | b19d1a9 | 1999-06-18 00:32:03 +0000 | [diff] [blame] | 107 | { |
| 108 | switch (variant) { |
| 109 | case UUID_VARIANT_NCS: |
| 110 | return "NCS"; |
| 111 | case UUID_VARIANT_DCE: |
| 112 | return "DCE"; |
| 113 | case UUID_VARIANT_MICROSOFT: |
| 114 | return "Microsoft"; |
| 115 | default: |
| 116 | return "Other"; |
| 117 | } |
| 118 | } |
| 119 | |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 120 | |
Theodore Ts'o | 1e0a221 | 1998-12-04 08:13:52 +0000 | [diff] [blame] | 121 | int |
| 122 | main(int argc, char **argv) |
| 123 | { |
| 124 | uuid_t buf; |
| 125 | time_t time_reg; |
| 126 | struct timeval tv; |
Theodore Ts'o | b19d1a9 | 1999-06-18 00:32:03 +0000 | [diff] [blame] | 127 | int type, variant; |
Theodore Ts'o | 1e0a221 | 1998-12-04 08:13:52 +0000 | [diff] [blame] | 128 | |
| 129 | if (argc != 2) { |
| 130 | fprintf(stderr, "Usage: %s uuid\n", argv[0]); |
| 131 | exit(1); |
| 132 | } |
| 133 | if (uuid_parse(argv[1], buf)) { |
| 134 | fprintf(stderr, "Invalid UUID: %s\n", argv[1]); |
| 135 | exit(1); |
| 136 | } |
Theodore Ts'o | b19d1a9 | 1999-06-18 00:32:03 +0000 | [diff] [blame] | 137 | variant = uuid_variant(buf); |
| 138 | type = uuid_type(buf); |
Theodore Ts'o | 1e0a221 | 1998-12-04 08:13:52 +0000 | [diff] [blame] | 139 | time_reg = uuid_time(buf, &tv); |
| 140 | |
Theodore Ts'o | b19d1a9 | 1999-06-18 00:32:03 +0000 | [diff] [blame] | 141 | printf("UUID variant is %d (%s)\n", variant, variant_string(variant)); |
| 142 | if (variant != UUID_VARIANT_DCE) { |
| 143 | printf("Warning: This program only knows how to interpret " |
| 144 | "DCE UUIDs.\n\tThe rest of the output is likely " |
| 145 | "to be incorrect!!\n"); |
| 146 | } |
| 147 | printf("UUID type is %d", type); |
| 148 | switch (type) { |
| 149 | case 1: |
| 150 | printf(" (time based)\n"); |
| 151 | break; |
| 152 | case 2: |
| 153 | printf(" (DCE)\n"); |
| 154 | break; |
| 155 | case 3: |
| 156 | printf(" (name-based)\n"); |
| 157 | break; |
| 158 | case 4: |
| 159 | printf(" (random)\n"); |
| 160 | break; |
| 161 | default: |
| 162 | printf("\n"); |
| 163 | } |
| 164 | if (type != 1) { |
| 165 | printf("Warning: not a time-based UUID, so UUID time " |
| 166 | "decoding will likely not work!\n"); |
| 167 | } |
Theodore Ts'o | 96394d1 | 2001-01-12 18:30:54 +0000 | [diff] [blame] | 168 | printf("UUID time is: (%ld, %ld): %s\n", tv.tv_sec, tv.tv_usec, |
Theodore Ts'o | 1e0a221 | 1998-12-04 08:13:52 +0000 | [diff] [blame] | 169 | ctime(&time_reg)); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 170 | |
Theodore Ts'o | 1e0a221 | 1998-12-04 08:13:52 +0000 | [diff] [blame] | 171 | return 0; |
| 172 | } |
| 173 | #endif |