license headers in libnetxms changed to LGPL
[public/netxms.git] / src / libnetxms / uuid.c
1 /*
2 ** libuuid integrated into NetXMS project
3 ** Copyright (C) 1996, 1997 Theodore Ts'o.
4 ** Integrated into NetXMS by Victor Kirhenshtein
5 **
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU Lesser General Public License as published
8 ** by the Free Software Foundation; either version 3 of the License, or
9 ** (at your option) any later version.
10 **
11 ** This program is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU General Public License for more details.
15 **
16 ** You should have received a copy of the GNU Lesser General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 **
20 ** File: uuid.cpp
21 **
22 **/
23
24 #include "libnetxms.h"
25 #include "uuidP.h"
26
27
28 //
29 // Clear a UUID
30 //
31
32 void LIBNETXMS_EXPORTABLE uuid_clear(uuid_t uu)
33 {
34 memset(uu, 0, 16);
35 }
36
37 #define UUCMP(u1,u2) if (u1 != u2) return((u1 < u2) ? -1 : 1);
38
39
40 //
41 // compare whether or not two UUID's are the same
42 //
43 // Returns 0 if the two UUID's are different, and 1 if they are the same.
44 //
45
46 int LIBNETXMS_EXPORTABLE uuid_compare(uuid_t uu1, uuid_t uu2)
47 {
48 struct uuid uuid1, uuid2;
49
50 uuid_unpack(uu1, &uuid1);
51 uuid_unpack(uu2, &uuid2);
52
53 UUCMP(uuid1.time_low, uuid2.time_low);
54 UUCMP(uuid1.time_mid, uuid2.time_mid);
55 UUCMP(uuid1.time_hi_and_version, uuid2.time_hi_and_version);
56 UUCMP(uuid1.clock_seq, uuid2.clock_seq);
57 return memcmp(uuid1.node, uuid2.node, 6);
58 }
59
60
61 //
62 // isnull.c --- Check whether or not the UUID is null
63 // Returns 1 if the uuid is the NULL uuid
64 //
65
66 int LIBNETXMS_EXPORTABLE uuid_is_null(uuid_t uu)
67 {
68 unsigned char *cp;
69 int i;
70
71 for (i=0, cp = uu; i < 16; i++)
72 if (*cp++)
73 return 0;
74 return 1;
75 }
76
77
78 //
79 // Internal routine for packing UUID's
80 //
81
82 void uuid_pack(struct uuid *uu, uuid_t ptr)
83 {
84 unsigned int tmp;
85 unsigned char *out = ptr;
86
87 tmp = uu->time_low;
88 out[3] = (unsigned char) tmp;
89 tmp >>= 8;
90 out[2] = (unsigned char) tmp;
91 tmp >>= 8;
92 out[1] = (unsigned char) tmp;
93 tmp >>= 8;
94 out[0] = (unsigned char) tmp;
95
96 tmp = uu->time_mid;
97 out[5] = (unsigned char) tmp;
98 tmp >>= 8;
99 out[4] = (unsigned char) tmp;
100
101 tmp = uu->time_hi_and_version;
102 out[7] = (unsigned char) tmp;
103 tmp >>= 8;
104 out[6] = (unsigned char) tmp;
105
106 tmp = uu->clock_seq;
107 out[9] = (unsigned char) tmp;
108 tmp >>= 8;
109 out[8] = (unsigned char) tmp;
110
111 memcpy(out+10, uu->node, 6);
112 }
113
114
115 //
116 // Internal routine for unpacking UUID
117 //
118
119 void uuid_unpack(uuid_t in, struct uuid *uu)
120 {
121 unsigned char *ptr = in;
122 unsigned int tmp;
123
124 tmp = *ptr++;
125 tmp = (tmp << 8) | *ptr++;
126 tmp = (tmp << 8) | *ptr++;
127 tmp = (tmp << 8) | *ptr++;
128 uu->time_low = tmp;
129
130 tmp = *ptr++;
131 tmp = (tmp << 8) | *ptr++;
132 uu->time_mid = tmp;
133
134 tmp = *ptr++;
135 tmp = (tmp << 8) | *ptr++;
136 uu->time_hi_and_version = tmp;
137
138 tmp = *ptr++;
139 tmp = (tmp << 8) | *ptr++;
140 uu->clock_seq = tmp;
141
142 memcpy(uu->node, ptr, 6);
143 }
144
145
146 //
147 // Parse UUID
148 //
149
150 int LIBNETXMS_EXPORTABLE uuid_parse(TCHAR *in, uuid_t uu)
151 {
152 struct uuid uuid;
153 int i;
154 TCHAR *cp, buf[3];
155
156 if (_tcslen(in) != 36)
157 return -1;
158 for (i=0, cp = in; i <= 36; i++,cp++) {
159 if ((i == 8) || (i == 13) || (i == 18) ||
160 (i == 23))
161 if (*cp == _T('-'))
162 continue;
163 if (i == 36)
164 if (*cp == 0)
165 continue;
166 if (!_istxdigit(*cp))
167 return -1;
168 }
169 uuid.time_low = _tcstoul(in, NULL, 16);
170 uuid.time_mid = (WORD)_tcstoul(in + 9, NULL, 16);
171 uuid.time_hi_and_version = (WORD)_tcstoul(in + 14, NULL, 16);
172 uuid.clock_seq = (WORD)_tcstoul(in + 19, NULL, 16);
173 cp = in + 24;
174 buf[2] = 0;
175 for(i = 0; i < 6; i++)
176 {
177 buf[0] = *cp++;
178 buf[1] = *cp++;
179 uuid.node[i] = (BYTE)_tcstoul(buf, NULL, 16);
180 }
181
182 uuid_pack(&uuid, uu);
183 return 0;
184 }
185
186
187 //
188 // Convert packed UUID to string
189 //
190
191 TCHAR LIBNETXMS_EXPORTABLE *uuid_to_string(uuid_t uu, TCHAR *out)
192 {
193 struct uuid uuid;
194
195 uuid_unpack(uu, &uuid);
196 _sntprintf(out, 64,
197 _T("%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x"),
198 uuid.time_low, uuid.time_mid, uuid.time_hi_and_version,
199 uuid.clock_seq >> 8, uuid.clock_seq & 0xFF,
200 uuid.node[0], uuid.node[1], uuid.node[2],
201 uuid.node[3], uuid.node[4], uuid.node[5]);
202 return out;
203 }