00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include <stdio.h>
00038 #include <unistd.h>
00039 #include <stdlib.h>
00040 #include <sys/types.h>
00041 #include <sys/time.h>
00042 #include <time.h>
00043
00044 #include "uuidP.h"
00045
00046 time_t uuid_time(const uuid_t uu, struct timeval *ret_tv)
00047 {
00048 struct uuid uuid;
00049 uint32_t high;
00050 struct timeval tv;
00051 unsigned long long clock_reg;
00052
00053 uuid_unpack(uu, &uuid);
00054
00055 high = uuid.time_mid | ((uuid.time_hi_and_version & 0xFFF) << 16);
00056 clock_reg = uuid.time_low | ((unsigned long long) high << 32);
00057
00058 clock_reg -= (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000;
00059 tv.tv_sec = clock_reg / 10000000;
00060 tv.tv_usec = (clock_reg % 10000000) / 10;
00061
00062 if (ret_tv)
00063 *ret_tv = tv;
00064
00065 return tv.tv_sec;
00066 }
00067
00068 int uuid_type(const uuid_t uu)
00069 {
00070 struct uuid uuid;
00071
00072 uuid_unpack(uu, &uuid);
00073 return ((uuid.time_hi_and_version >> 12) & 0xF);
00074 }
00075
00076 int uuid_variant(const uuid_t uu)
00077 {
00078 struct uuid uuid;
00079 int var;
00080
00081 uuid_unpack(uu, &uuid);
00082 var = uuid.clock_seq;
00083
00084 if ((var & 0x8000) == 0)
00085 return UUID_VARIANT_NCS;
00086 if ((var & 0x4000) == 0)
00087 return UUID_VARIANT_DCE;
00088 if ((var & 0x2000) == 0)
00089 return UUID_VARIANT_MICROSOFT;
00090 return UUID_VARIANT_OTHER;
00091 }
00092
00093 #if 0
00094 #ifdef DEBUG
00095 static const char *variant_string(int variant)
00096 {
00097 switch (variant) {
00098 case UUID_VARIANT_NCS:
00099 return "NCS";
00100 case UUID_VARIANT_DCE:
00101 return "DCE";
00102 case UUID_VARIANT_MICROSOFT:
00103 return "Microsoft";
00104 default:
00105 return "Other";
00106 }
00107 }
00108
00109
00110 int
00111 main(int argc, char **argv)
00112 {
00113 uuid_t buf;
00114 time_t time_reg;
00115 struct timeval tv;
00116 int type, variant;
00117
00118 if (argc != 2) {
00119 fprintf(stderr, "Usage: %s uuid\n", argv[0]);
00120 exit(1);
00121 }
00122 if (uuid_parse(argv[1], buf)) {
00123 fprintf(stderr, "Invalid UUID: %s\n", argv[1]);
00124 exit(1);
00125 }
00126 variant = uuid_variant(buf);
00127 type = uuid_type(buf);
00128 time_reg = uuid_time(buf, &tv);
00129
00130 printf("UUID variant is %d (%s)\n", variant, variant_string(variant));
00131 if (variant != UUID_VARIANT_DCE) {
00132 printf("Warning: This program only knows how to interpret "
00133 "DCE UUIDs.\n\tThe rest of the output is likely "
00134 "to be incorrect!!\n");
00135 }
00136 printf("UUID type is %d", type);
00137 switch (type) {
00138 case 1:
00139 printf(" (time based)\n");
00140 break;
00141 case 2:
00142 printf(" (DCE)\n");
00143 break;
00144 case 3:
00145 printf(" (name-based)\n");
00146 break;
00147 case 4:
00148 printf(" (random)\n");
00149 break;
00150 default:
00151 printf("\n");
00152 }
00153 if (type != 1) {
00154 printf("Warning: not a time-based UUID, so UUID time "
00155 "decoding will likely not work!\n");
00156 }
00157 printf("UUID time is: (%ld, %ld): %s\n", tv.tv_sec, tv.tv_usec,
00158 ctime(&time_reg));
00159
00160 return 0;
00161 }
00162 #endif
00163 #endif