2 * This file is part of the OpenPTS project.
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.
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)
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.
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.
25 * \file src/uuid_libuuid.c
26 * \brief UUID wrapper (libuuid part)
27 * @author Seiji Munetoh <munetoh@users.sourceforge.jp>
29 * cleanup 2011-12-31 SM
48 #define SEP_LINE "------------------------------------------------------------------------------------"
51 #include <arpa/inet.h>
56 uint16_t time_hi_and_version;
57 uint8_t clock_seq_hi_and_reserved;
58 uint8_t clock_seq_low;
62 time_t uuid_time(uuid_t uu, struct timeval *tv) {
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];
71 if ((myUUID.clock_seq_hi_and_reserved & 0xc0) != 0x80) {
72 LOG(LOG_ERR, "uuid_time() - bad UUID variant (0x%02x) found, can't extract timestamp\n",
73 (myUUID.clock_seq_hi_and_reserved & 0xc0) >> 4);
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;
84 /******************************/
86 /******************************/
89 * Create new UUID (DCE1.1 v1 time and node base)
95 uuid = xmalloc(sizeof(PTS_UUID)); // BYTE[16]
97 LOG(LOG_ERR, "no memory");
101 uuid_generate_time(uu);
102 memcpy(uuid, uu, 16);
104 return (PTS_UUID *)uuid;
110 void freeUuid(PTS_UUID *uuid) {
113 LOG(LOG_ERR, "null input");
124 PTS_UUID *getUuidFromString(char *str) {
131 LOG(LOG_ERR, "null input");
135 rc = uuid_parse(str, uu);
137 LOG(LOG_ERR, "getUuidFromString() - uuid_parse fail, rc=%d, UUID='%s'",
142 uuid = xmalloc(sizeof(PTS_UUID));
144 LOG(LOG_ERR, "no memory");
147 memcpy(uuid, uu, 16);
155 char * getStringOfUuid(PTS_UUID *uuid) {
161 LOG(LOG_ERR, "null input");
165 str_uuid = xmalloc(37);
166 if (str_uuid == NULL) {
167 LOG(LOG_ERR, "no memory");
171 memcpy(uu, uuid, 16);
173 uuid_unparse(uu, str_uuid);
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.
210 PTS_DateTime * getDateTimeOfUuid(PTS_UUID *uuid) {
219 LOG(LOG_ERR, "null input\n");
224 memcpy(uu, uuid, 16);
225 t = uuid_time(uu, &tv);
226 // TODO gmtime or local?
227 gmtime_r((const time_t *) &t, &time);
229 pdt = xmalloc(sizeof(PTS_DateTime));
231 LOG(LOG_ERR, "no memory");
234 memcpy(pdt, &time, (9*4));
242 PTS_DateTime * getDateTime() {
249 // TODO gmtime or local?
250 gmtime_r((const time_t *) &t, &ttm);
252 pdt = xmalloc(sizeof(PTS_DateTime));
254 LOG(LOG_ERR, "no memory");
257 memcpy(pdt, &ttm, (9*4));