source: rtems/cpukit/libmisc/uuid/uuid_time.c

Last change on this file was ebdeaf6, checked in by Ralf Corsepius <ralf.corsepius@…>, on 03/27/10 at 04:54:19

Add HAVE_CONFIG_H guards around #include "config.h".

  • Property mode set to 100644
File size: 4.3 KB
Line 
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 HAVE_CONFIG_H
38#include "config.h"
39#endif
40
41#ifdef _WIN32
42#define _WIN32_WINNT 0x0500
43#include <windows.h>
44#define UUID MYUUID
45#endif
46
47#include <stdio.h>
48#ifdef HAVE_UNISTD_H
49#include <unistd.h>
50#endif
51#include <stdlib.h>
52#include <sys/types.h>
53#ifdef HAVE_SYS_TIME_H
54#include <sys/time.h>
55#endif
56#include <time.h>
57
58#include "uuidP.h"
59
60time_t uuid_time(const uuid_t uu, struct timeval *ret_tv)
61{
62        struct timeval          tv;
63        struct uuid             uuid;
64        uint32_t                high;
65        uint64_t                clock_reg;
66
67        uuid_unpack(uu, &uuid);
68
69        high = uuid.time_mid | ((uint32_t)(uuid.time_hi_and_version & 0xFFF) << 16);
70        clock_reg = uuid.time_low | ((uint64_t) high << 32);
71
72        clock_reg -= (((uint64_t) 0x01B21DD2) << 32) + 0x13814000;
73        tv.tv_sec = clock_reg / 10000000;
74        tv.tv_usec = (clock_reg % 10000000) / 10;
75
76        if (ret_tv)
77                *ret_tv = tv;
78
79        return tv.tv_sec;
80}
81
82int uuid_type(const uuid_t uu)
83{
84        struct uuid             uuid;
85
86        uuid_unpack(uu, &uuid);
87        return ((uuid.time_hi_and_version >> 12) & 0xF);
88}
89
90int uuid_variant(const uuid_t uu)
91{
92        struct uuid             uuid;
93        int                     var;
94
95        uuid_unpack(uu, &uuid);
96        var = uuid.clock_seq;
97
98        if ((var & 0x8000) == 0)
99                return UUID_VARIANT_NCS;
100        if ((var & 0x4000) == 0)
101                return UUID_VARIANT_DCE;
102        if ((var & 0x2000) == 0)
103                return UUID_VARIANT_MICROSOFT;
104        return UUID_VARIANT_OTHER;
105}
106
107#ifdef DEBUG
108static const char *variant_string(int variant)
109{
110        switch (variant) {
111        case UUID_VARIANT_NCS:
112                return "NCS";
113        case UUID_VARIANT_DCE:
114                return "DCE";
115        case UUID_VARIANT_MICROSOFT:
116                return "Microsoft";
117        default:
118                return "Other";
119        }
120}
121
122
123int
124main(int argc, char **argv)
125{
126        uuid_t          buf;
127        time_t          time_reg;
128        struct timeval  tv;
129        int             type, variant;
130
131        if (argc != 2) {
132                fprintf(stderr, "Usage: %s uuid\n", argv[0]);
133                exit(1);
134        }
135        if (uuid_parse(argv[1], buf)) {
136                fprintf(stderr, "Invalid UUID: %s\n", argv[1]);
137                exit(1);
138        }
139        variant = uuid_variant(buf);
140        type = uuid_type(buf);
141        time_reg = uuid_time(buf, &tv);
142
143        printf("UUID variant is %d (%s)\n", variant, variant_string(variant));
144        if (variant != UUID_VARIANT_DCE) {
145                printf("Warning: This program only knows how to interpret "
146                       "DCE UUIDs.\n\tThe rest of the output is likely "
147                       "to be incorrect!!\n");
148        }
149        printf("UUID type is %d", type);
150        switch (type) {
151        case 1:
152                printf(" (time based)\n");
153                break;
154        case 2:
155                printf(" (DCE)\n");
156                break;
157        case 3:
158                printf(" (name-based)\n");
159                break;
160        case 4:
161                printf(" (random)\n");
162                break;
163        default:
164                printf("\n");
165        }
166        if (type != 1) {
167                printf("Warning: not a time-based UUID, so UUID time "
168                       "decoding will likely not work!\n");
169        }
170        printf("UUID time is: (%ld, %ld): %s\n", tv.tv_sec, tv.tv_usec,
171               ctime(&time_reg));
172
173        return 0;
174}
175#endif
Note: See TracBrowser for help on using the repository browser.