paul@181 | 1 | /* |
paul@181 | 2 | * uuid_time.c --- Interpret the time field from a uuid. This program |
paul@181 | 3 | * violates the UUID abstraction barrier by reaching into the guts |
paul@181 | 4 | * of a UUID and interpreting it. |
paul@181 | 5 | * |
paul@181 | 6 | * Copyright (C) 1998, 1999 Theodore Ts'o. |
paul@181 | 7 | * |
paul@181 | 8 | * %Begin-Header% |
paul@181 | 9 | * Redistribution and use in source and binary forms, with or without |
paul@181 | 10 | * modification, are permitted provided that the following conditions |
paul@181 | 11 | * are met: |
paul@181 | 12 | * 1. Redistributions of source code must retain the above copyright |
paul@181 | 13 | * notice, and the entire permission notice in its entirety, |
paul@181 | 14 | * including the disclaimer of warranties. |
paul@181 | 15 | * 2. Redistributions in binary form must reproduce the above copyright |
paul@181 | 16 | * notice, this list of conditions and the following disclaimer in the |
paul@181 | 17 | * documentation and/or other materials provided with the distribution. |
paul@181 | 18 | * 3. The name of the author may not be used to endorse or promote |
paul@181 | 19 | * products derived from this software without specific prior |
paul@181 | 20 | * written permission. |
paul@181 | 21 | * |
paul@181 | 22 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED |
paul@181 | 23 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
paul@181 | 24 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF |
paul@181 | 25 | * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE |
paul@181 | 26 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
paul@181 | 27 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
paul@181 | 28 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
paul@181 | 29 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
paul@181 | 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
paul@181 | 31 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
paul@181 | 32 | * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH |
paul@181 | 33 | * DAMAGE. |
paul@181 | 34 | * %End-Header% |
paul@181 | 35 | */ |
paul@181 | 36 | |
paul@181 | 37 | #include "config.h" |
paul@181 | 38 | |
paul@181 | 39 | #ifdef _WIN32 |
paul@181 | 40 | #define _WIN32_WINNT 0x0500 |
paul@181 | 41 | #include <windows.h> |
paul@181 | 42 | #define UUID MYUUID |
paul@181 | 43 | #endif |
paul@181 | 44 | |
paul@181 | 45 | #include <stdio.h> |
paul@181 | 46 | #ifdef HAVE_UNISTD_H |
paul@181 | 47 | #include <unistd.h> |
paul@181 | 48 | #endif |
paul@181 | 49 | #include <stdlib.h> |
paul@181 | 50 | #include <sys/types.h> |
paul@181 | 51 | #ifdef HAVE_SYS_TIME_H |
paul@181 | 52 | #include <sys/time.h> |
paul@181 | 53 | #endif |
paul@181 | 54 | #include <time.h> |
paul@181 | 55 | |
paul@181 | 56 | #include "uuidP.h" |
paul@181 | 57 | |
paul@181 | 58 | time_t uuid_time(const uuid_t uu, struct timeval *ret_tv) |
paul@181 | 59 | { |
paul@181 | 60 | struct timeval tv; |
paul@181 | 61 | struct uuid uuid; |
paul@181 | 62 | uint32_t high; |
paul@181 | 63 | uint64_t clock_reg; |
paul@181 | 64 | |
paul@181 | 65 | uuid_unpack(uu, &uuid); |
paul@181 | 66 | |
paul@181 | 67 | high = uuid.time_mid | ((uuid.time_hi_and_version & 0xFFF) << 16); |
paul@181 | 68 | clock_reg = uuid.time_low | ((uint64_t) high << 32); |
paul@181 | 69 | |
paul@181 | 70 | clock_reg -= (((uint64_t) 0x01B21DD2) << 32) + 0x13814000; |
paul@181 | 71 | tv.tv_sec = clock_reg / 10000000; |
paul@181 | 72 | tv.tv_usec = (clock_reg % 10000000) / 10; |
paul@181 | 73 | |
paul@181 | 74 | if (ret_tv) |
paul@181 | 75 | *ret_tv = tv; |
paul@181 | 76 | |
paul@181 | 77 | return tv.tv_sec; |
paul@181 | 78 | } |
paul@181 | 79 | |
paul@181 | 80 | int uuid_type(const uuid_t uu) |
paul@181 | 81 | { |
paul@181 | 82 | struct uuid uuid; |
paul@181 | 83 | |
paul@181 | 84 | uuid_unpack(uu, &uuid); |
paul@181 | 85 | return ((uuid.time_hi_and_version >> 12) & 0xF); |
paul@181 | 86 | } |
paul@181 | 87 | |
paul@181 | 88 | int uuid_variant(const uuid_t uu) |
paul@181 | 89 | { |
paul@181 | 90 | struct uuid uuid; |
paul@181 | 91 | int var; |
paul@181 | 92 | |
paul@181 | 93 | uuid_unpack(uu, &uuid); |
paul@181 | 94 | var = uuid.clock_seq; |
paul@181 | 95 | |
paul@181 | 96 | if ((var & 0x8000) == 0) |
paul@181 | 97 | return UUID_VARIANT_NCS; |
paul@181 | 98 | if ((var & 0x4000) == 0) |
paul@181 | 99 | return UUID_VARIANT_DCE; |
paul@181 | 100 | if ((var & 0x2000) == 0) |
paul@181 | 101 | return UUID_VARIANT_MICROSOFT; |
paul@181 | 102 | return UUID_VARIANT_OTHER; |
paul@181 | 103 | } |
paul@181 | 104 | |
paul@181 | 105 | #ifdef DEBUG |
paul@181 | 106 | static const char *variant_string(int variant) |
paul@181 | 107 | { |
paul@181 | 108 | switch (variant) { |
paul@181 | 109 | case UUID_VARIANT_NCS: |
paul@181 | 110 | return "NCS"; |
paul@181 | 111 | case UUID_VARIANT_DCE: |
paul@181 | 112 | return "DCE"; |
paul@181 | 113 | case UUID_VARIANT_MICROSOFT: |
paul@181 | 114 | return "Microsoft"; |
paul@181 | 115 | default: |
paul@181 | 116 | return "Other"; |
paul@181 | 117 | } |
paul@181 | 118 | } |
paul@181 | 119 | |
paul@181 | 120 | |
paul@181 | 121 | int |
paul@181 | 122 | main(int argc, char **argv) |
paul@181 | 123 | { |
paul@181 | 124 | uuid_t buf; |
paul@181 | 125 | time_t time_reg; |
paul@181 | 126 | struct timeval tv; |
paul@181 | 127 | int type, variant; |
paul@181 | 128 | |
paul@181 | 129 | if (argc != 2) { |
paul@181 | 130 | fprintf(stderr, "Usage: %s uuid\n", argv[0]); |
paul@181 | 131 | exit(1); |
paul@181 | 132 | } |
paul@181 | 133 | if (uuid_parse(argv[1], buf)) { |
paul@181 | 134 | fprintf(stderr, "Invalid UUID: %s\n", argv[1]); |
paul@181 | 135 | exit(1); |
paul@181 | 136 | } |
paul@181 | 137 | variant = uuid_variant(buf); |
paul@181 | 138 | type = uuid_type(buf); |
paul@181 | 139 | time_reg = uuid_time(buf, &tv); |
paul@181 | 140 | |
paul@181 | 141 | printf("UUID variant is %d (%s)\n", variant, variant_string(variant)); |
paul@181 | 142 | if (variant != UUID_VARIANT_DCE) { |
paul@181 | 143 | printf("Warning: This program only knows how to interpret " |
paul@181 | 144 | "DCE UUIDs.\n\tThe rest of the output is likely " |
paul@181 | 145 | "to be incorrect!!\n"); |
paul@181 | 146 | } |
paul@181 | 147 | printf("UUID type is %d", type); |
paul@181 | 148 | switch (type) { |
paul@181 | 149 | case 1: |
paul@181 | 150 | printf(" (time based)\n"); |
paul@181 | 151 | break; |
paul@181 | 152 | case 2: |
paul@181 | 153 | printf(" (DCE)\n"); |
paul@181 | 154 | break; |
paul@181 | 155 | case 3: |
paul@181 | 156 | printf(" (name-based)\n"); |
paul@181 | 157 | break; |
paul@181 | 158 | case 4: |
paul@181 | 159 | printf(" (random)\n"); |
paul@181 | 160 | break; |
paul@181 | 161 | default: |
paul@181 | 162 | printf("\n"); |
paul@181 | 163 | } |
paul@181 | 164 | if (type != 1) { |
paul@181 | 165 | printf("Warning: not a time-based UUID, so UUID time " |
paul@181 | 166 | "decoding will likely not work!\n"); |
paul@181 | 167 | } |
paul@181 | 168 | printf("UUID time is: (%ld, %ld): %s\n", tv.tv_sec, (long)tv.tv_usec, |
paul@181 | 169 | ctime(&time_reg)); |
paul@181 | 170 | |
paul@181 | 171 | return 0; |
paul@181 | 172 | } |
paul@181 | 173 | #endif |