OSDN Git Service

update for v0.2.6
[openpts/openpts.git] / src / uuid_libuuid.c
1 /*
2  * This file is part of the OpenPTS project.
3  *
4  * The Initial Developer of the Original Code is International
5  * Business Machines Corporation. Portions created by IBM
6  * Corporation are Copyright (C) 2010 International Business
7  * Machines Corporation. All Rights Reserved.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the Common Public License as published by
11  * IBM Corporation; either version 1 of the License, or (at your option)
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * Common Public License for more details.
18  *
19  * You should have received a copy of the Common Public License
20  * along with this program; if not, a copy can be viewed at
21  * http://www.opensource.org/licenses/cpl1.0.php.
22  */
23
24 /**
25  * \file src/uuid_libuuid.c
26  * \brief UUID wrapper (libuuid part)
27  * @author Seiji Munetoh <munetoh@users.sourceforge.jp>
28  * @date 2010-11-29
29  * cleanup 2011-10-07 SM
30  *
31  * Linux uses libuuid
32  *
33  */
34
35 #include <stdio.h>
36 #include <string.h>
37 #include <time.h>
38 #include <sys/stat.h>
39
40 // DIR
41 #include <unistd.h>
42 #include <dirent.h>
43
44 #include <uuid.h>
45
46 #include <openpts.h>
47
48 #define SEP_LINE "------------------------------------------------------------------------------------"
49
50 #ifdef MACOS
51 #include <arpa/inet.h>
52
53 typedef struct {
54     uint32_t time_low;
55     uint16_t time_mid;
56     uint16_t time_hi_and_version;
57     uint8_t  clock_seq_hi_and_reserved;
58     uint8_t  clock_seq_low;
59     char     node[6];
60 } my_uuid_t;
61
62 time_t uuid_time(uuid_t uu, struct timeval *tv) {
63     my_uuid_t myUUID;
64     uint64_t clunks;
65
66     myUUID.time_low = ntohl(*((uint32_t*)&uu[0]));
67     myUUID.time_mid = ntohs(*((uint16_t*)&uu[4]));
68     myUUID.time_hi_and_version = ntohs(*((uint16_t*)&uu[6]));
69     myUUID.clock_seq_hi_and_reserved = uu[8];
70
71     if ((myUUID.clock_seq_hi_and_reserved & 0xc0) != 0x80) {
72         ERROR("uuid_time() - bad UUID variant (0x%02x) found, can't extract timestamp\n",
73             (myUUID.clock_seq_hi_and_reserved & 0xc0) >> 4);
74         return (time_t)-1;
75     }
76
77     clunks  = ((uint64_t)(myUUID.time_hi_and_version & 0x0fff)) << 48;
78     clunks += ((uint64_t)myUUID.time_mid) << 32;
79     clunks += myUUID.time_low;
80     return (clunks - 0x01B21DD213814000ULL) / 10000000;
81 }
82 #endif
83
84 /******************************/
85 /* PTS_UUID                   */
86 /******************************/
87
88 /**
89  * Create new UUID (DCE1.1 v1 time and node base)
90  */
91 PTS_UUID *newUuid() {
92     uuid_t uu;
93     PTS_UUID *uuid;
94
95     uuid = xmalloc(sizeof(PTS_UUID));  // BYTE[16]
96     if (uuid == NULL) {
97         return NULL;
98     }
99
100     uuid_generate_time(uu);
101     memcpy(uuid, uu, 16);
102
103     return (PTS_UUID *)uuid;
104 }
105
106 /**
107  * free UUID
108  */
109 void freeUuid(PTS_UUID *uuid) {
110     xfree(uuid);
111 }
112
113
114 /**
115  * String -> UUID 
116  */
117 PTS_UUID *getUuidFromString(char *str) {
118     PTS_UUID *uuid;
119     uuid_t uu;
120     int rc;
121
122     rc = uuid_parse(str, uu);
123     if (rc != 0) {
124         ERROR("getUuidFromString() - uuid_parse fail, rc=%d, UUID='%s'\n",
125             rc, str);
126         return NULL;
127     }
128
129     uuid = xmalloc(sizeof(PTS_UUID));
130     if (uuid == NULL) {
131         ERROR("\n");
132         return NULL;
133     }
134     memcpy(uuid, uu, 16);
135
136     return uuid;
137 }
138
139 /**
140  * UUID -> String 
141  */
142 char * getStringOfUuid(PTS_UUID *uuid) {
143     char *str_uuid;
144     uuid_t uu;
145
146     str_uuid = xmalloc(37);
147     if (str_uuid == NULL) {
148         return NULL;
149     }
150
151     memcpy(uu, uuid, 16);
152
153     uuid_unparse(uu, str_uuid);
154
155     return str_uuid;
156 }
157
158
159 /**
160  * get Time 
161  *
162 Linux
163 struct tm
164   int tm_sec;                    Seconds.     [0-60] (1 leap second)
165   int tm_min;                    Minutes.     [0-59] 
166   int tm_hour;                   Hours.       [0-23] 
167   int tm_mday;                   Day.         [1-31] 
168   int tm_mon;                    Month.       [0-11] 
169   int tm_year;                   Year - 1900.  
170   int tm_wday;                   Day of week. [0-6] 
171   int tm_yday;                   Days in year.[0-365] 
172   int tm_isdst;                  DST.         [-1/0/1]
173   long int __tm_gmtoff;          Seconds east of UTC.  
174   __const char *__tm_zone;       Timezone abbreviation.
175
176 PTS 
177 typedef struct {
178     PTS_UInt32 sec;     //
179     PTS_UInt32 min;             //
180     PTS_UInt32 hour;    //
181     PTS_UInt32 mday;    //
182     PTS_UInt32 mon;             //
183     PTS_UInt32 year;    //
184     PTS_UInt32 wday;    //
185     PTS_UInt32 yday;    //
186     PTS_Bool isDst;
187 } PTS_DateTime;
188
189  */
190 PTS_DateTime * getDateTimeOfUuid(PTS_UUID *uuid) {
191     uuid_t uu;
192     PTS_DateTime *pdt;
193     time_t t;
194     struct timeval tv;
195     struct tm time;
196
197     /* check */
198     if (uuid == NULL) {
199         ERROR("null input\n");
200         return NULL;
201     }
202
203     /* get time */
204     memcpy(uu, uuid, 16);
205     t = uuid_time(uu, &tv);
206     // TODO gmtime or local?
207     gmtime_r((const time_t *) &t, &time);
208
209     pdt = xmalloc(sizeof(PTS_DateTime));
210     if (pdt == NULL) {
211         return NULL;
212     }
213     memcpy(pdt, &time, (9*4));
214
215     return pdt;
216 }
217
218 /**
219  * get current time
220  */
221 PTS_DateTime * getDateTime() {
222     PTS_DateTime *pdt;
223     time_t t;
224     struct tm ttm;
225
226     /* get time */
227     time(&t);
228     // TODO gmtime or local?
229     gmtime_r((const time_t *) &t, &ttm);
230
231     pdt = xmalloc(sizeof(PTS_DateTime));
232     if (pdt == NULL) {
233         return NULL;
234     }
235     memcpy(pdt, &ttm, (9*4));
236
237     return pdt;
238 }
239
240