uuid_time.c

00001 /*
00002  * uuid_time.c --- Interpret the time field from a uuid.  This program
00003  *      violates the UUID abstraction barrier by reaching into the guts
00004  *      of a UUID and interpreting it.
00005  * 
00006  * Copyright (C) 1998, 1999 Theodore Ts'o.
00007  *
00008  * %Begin-Header%
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted provided that the following conditions
00011  * are met:
00012  * 1. Redistributions of source code must retain the above copyright
00013  *    notice, and the entire permission notice in its entirety,
00014  *    including the disclaimer of warranties.
00015  * 2. Redistributions in binary form must reproduce the above copyright
00016  *    notice, this list of conditions and the following disclaimer in the
00017  *    documentation and/or other materials provided with the distribution.
00018  * 3. The name of the author may not be used to endorse or promote
00019  *    products derived from this software without specific prior
00020  *    written permission.
00021  * 
00022  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
00023  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00024  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
00025  * WHICH ARE HEREBY DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE
00026  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00027  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
00028  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
00029  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00030  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00031  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00032  * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
00033  * DAMAGE.
00034  * %End-Header%
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

Generated on Sun Sep 24 20:10:49 2006 for WvStreams by  doxygen 1.4.7