OSDN Git Service

deleted ASSERTs and added many checks, also cleanup the codes
[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-12-31 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         ERROR("no memory");
98         return NULL;
99     }
100
101     uuid_generate_time(uu);
102     memcpy(uuid, uu, 16);
103
104     return (PTS_UUID *)uuid;
105 }
106
107 /**
108  * free UUID
109  */
110 void freeUuid(PTS_UUID *uuid) {
111     /* check */
112     if (uuid == NULL) {
113         ERROR("null input");
114         return;
115     }
116
117     xfree(uuid);
118 }
119
120
121 /**
122  * String -> UUID 
123  */
124 PTS_UUID *getUuidFromString(char *str) {
125     PTS_UUID *uuid;
126     uuid_t uu;
127     int rc;
128
129     /* check */
130     if (str == NULL) {
131         ERROR("null input");
132         return NULL;
133     }
134
135     rc = uuid_parse(str, uu);
136     if (rc != 0) {
137         ERROR("getUuidFromString() - uuid_parse fail, rc=%d, UUID='%s'",
138             rc, str);
139         return NULL;
140     }
141
142     uuid = xmalloc(sizeof(PTS_UUID));
143     if (uuid == NULL) {
144         ERROR("no memory");
145         return NULL;
146     }
147     memcpy(uuid, uu, 16);
148
149     return uuid;
150 }
151
152 /**
153  * UUID -> String 
154  */
155 char * getStringOfUuid(PTS_UUID *uuid) {
156     char *str_uuid;
157     uuid_t uu;
158
159     /* check */
160     if (uuid == NULL) {
161         ERROR("null input");
162         return NULL;
163     }
164
165     str_uuid = xmalloc(37);
166     if (str_uuid == NULL) {
167         ERROR("no memory");
168         return NULL;
169     }
170
171     memcpy(uu, uuid, 16);
172
173     uuid_unparse(uu, str_uuid);
174
175     return str_uuid;
176 }
177
178
179 /**
180  * get Time 
181  *
182 Linux
183 struct tm
184   int tm_sec;                    Seconds.     [0-60] (1 leap second)
185   int tm_min;                    Minutes.     [0-59] 
186   int tm_hour;                   Hours.       [0-23] 
187   int tm_mday;                   Day.         [1-31] 
188   int tm_mon;                    Month.       [0-11] 
189   int tm_year;                   Year - 1900.  
190   int tm_wday;                   Day of week. [0-6] 
191   int tm_yday;                   Days in year.[0-365] 
192   int tm_isdst;                  DST.         [-1/0/1]
193   long int __tm_gmtoff;          Seconds east of UTC.  
194   __const char *__tm_zone;       Timezone abbreviation.
195
196 PTS 
197 typedef struct {
198     PTS_UInt32 sec;     //
199     PTS_UInt32 min;             //
200     PTS_UInt32 hour;    //
201     PTS_UInt32 mday;    //
202     PTS_UInt32 mon;             //
203     PTS_UInt32 year;    //
204     PTS_UInt32 wday;    //
205     PTS_UInt32 yday;    //
206     PTS_Bool isDst;
207 } PTS_DateTime;
208
209  */
210 PTS_DateTime * getDateTimeOfUuid(PTS_UUID *uuid) {
211     uuid_t uu;
212     PTS_DateTime *pdt;
213     time_t t;
214     struct timeval tv;
215     struct tm time;
216
217     /* check */
218     if (uuid == NULL) {
219         ERROR("null input\n");
220         return NULL;
221     }
222
223     /* get time */
224     memcpy(uu, uuid, 16);
225     t = uuid_time(uu, &tv);
226     // TODO gmtime or local?
227     gmtime_r((const time_t *) &t, &time);
228
229     pdt = xmalloc(sizeof(PTS_DateTime));
230     if (pdt == NULL) {
231         ERROR("no memory");
232         return NULL;
233     }
234     memcpy(pdt, &time, (9*4));
235
236     return pdt;
237 }
238
239 /**
240  * get current time
241  */
242 PTS_DateTime * getDateTime() {
243     PTS_DateTime *pdt;
244     time_t t;
245     struct tm ttm;
246
247     /* get time */
248     time(&t);
249     // TODO gmtime or local?
250     gmtime_r((const time_t *) &t, &ttm);
251
252     pdt = xmalloc(sizeof(PTS_DateTime));
253     if (pdt == NULL) {
254         ERROR("no memory");
255         return NULL;
256     }
257     memcpy(pdt, &ttm, (9*4));
258
259     return pdt;
260 }
261
262