1 | // |
---|
2 | // UUID.cpp |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/src/UUID.cpp#5 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: UUID |
---|
8 | // Module: UUID |
---|
9 | // |
---|
10 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
11 | // and Contributors. |
---|
12 | // |
---|
13 | // Permission is hereby granted, free of charge, to any person or organization |
---|
14 | // obtaining a copy of the software and accompanying documentation covered by |
---|
15 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
16 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
17 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
18 | // do so, all subject to the following: |
---|
19 | // |
---|
20 | // The copyright notices in the Software and this entire statement, including |
---|
21 | // the above license grant, this restriction and the following disclaimer, |
---|
22 | // must be included in all copies of the Software, in whole or in part, and |
---|
23 | // all derivative works of the Software, unless such copies or derivative |
---|
24 | // works are solely in the form of machine-executable object code generated by |
---|
25 | // a source language processor. |
---|
26 | // |
---|
27 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
28 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
29 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
30 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
31 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
32 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
33 | // DEALINGS IN THE SOFTWARE. |
---|
34 | // |
---|
35 | |
---|
36 | |
---|
37 | #include "Poco/UUID.h" |
---|
38 | #include "Poco/ByteOrder.h" |
---|
39 | #include "Poco/Exception.h" |
---|
40 | #include <algorithm> |
---|
41 | #include <cstring> |
---|
42 | |
---|
43 | |
---|
44 | namespace Poco { |
---|
45 | |
---|
46 | |
---|
47 | UUID::UUID(): |
---|
48 | _timeLow(0), |
---|
49 | _timeMid(0), |
---|
50 | _timeHiAndVersion(0), |
---|
51 | _clockSeq(0) |
---|
52 | { |
---|
53 | std::memset(_node, 0, sizeof(_node)); |
---|
54 | } |
---|
55 | |
---|
56 | |
---|
57 | UUID::UUID(const UUID& uuid): |
---|
58 | _timeLow(uuid._timeLow), |
---|
59 | _timeMid(uuid._timeMid), |
---|
60 | _timeHiAndVersion(uuid._timeHiAndVersion), |
---|
61 | _clockSeq(uuid._clockSeq) |
---|
62 | { |
---|
63 | std::memcpy(_node, uuid._node, sizeof(_node)); |
---|
64 | } |
---|
65 | |
---|
66 | |
---|
67 | UUID::UUID(const std::string& uuid) |
---|
68 | { |
---|
69 | parse(uuid); |
---|
70 | } |
---|
71 | |
---|
72 | |
---|
73 | UUID::UUID(const char* uuid) |
---|
74 | { |
---|
75 | poco_check_ptr (uuid); |
---|
76 | parse(std::string(uuid)); |
---|
77 | } |
---|
78 | |
---|
79 | |
---|
80 | UUID::UUID(UInt32 timeLow, UInt32 timeMid, UInt32 timeHiAndVersion, UInt16 clockSeq, UInt8 node[]): |
---|
81 | _timeLow(timeLow), |
---|
82 | _timeMid(timeMid), |
---|
83 | _timeHiAndVersion(timeHiAndVersion), |
---|
84 | _clockSeq(clockSeq) |
---|
85 | { |
---|
86 | std::memcpy(_node, node, sizeof(_node)); |
---|
87 | } |
---|
88 | |
---|
89 | |
---|
90 | UUID::UUID(const char* bytes, Version version) |
---|
91 | { |
---|
92 | UInt32 i32; |
---|
93 | UInt16 i16; |
---|
94 | std::memcpy(&i32, bytes, sizeof(i32)); |
---|
95 | _timeLow = ByteOrder::fromNetwork(i32); |
---|
96 | bytes += sizeof(i32); |
---|
97 | std::memcpy(&i16, bytes, sizeof(i16)); |
---|
98 | _timeMid = ByteOrder::fromNetwork(i16); |
---|
99 | bytes += sizeof(i16); |
---|
100 | std::memcpy(&i16, bytes, sizeof(i16)); |
---|
101 | _timeHiAndVersion = ByteOrder::fromNetwork(i16); |
---|
102 | bytes += sizeof(i16); |
---|
103 | std::memcpy(&i16, bytes, sizeof(i16)); |
---|
104 | _clockSeq = ByteOrder::fromNetwork(i16); |
---|
105 | bytes += sizeof(i16); |
---|
106 | std::memcpy(_node, bytes, sizeof(_node)); |
---|
107 | |
---|
108 | _timeHiAndVersion &= 0x0FFF; |
---|
109 | _timeHiAndVersion |= (version << 12); |
---|
110 | _clockSeq &= 0x3FFF; |
---|
111 | _clockSeq |= 0x8000; |
---|
112 | } |
---|
113 | |
---|
114 | |
---|
115 | UUID::~UUID() |
---|
116 | { |
---|
117 | } |
---|
118 | |
---|
119 | |
---|
120 | UUID& UUID::operator = (const UUID& uuid) |
---|
121 | { |
---|
122 | if (&uuid != this) |
---|
123 | { |
---|
124 | _timeLow = uuid._timeLow; |
---|
125 | _timeMid = uuid._timeMid; |
---|
126 | _timeHiAndVersion = uuid._timeHiAndVersion; |
---|
127 | _clockSeq = uuid._clockSeq; |
---|
128 | std::memcpy(_node, uuid._node, sizeof(_node)); |
---|
129 | } |
---|
130 | return *this; |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | void UUID::swap(UUID& uuid) |
---|
135 | { |
---|
136 | std::swap(_timeLow, uuid._timeLow); |
---|
137 | std::swap(_timeMid, uuid._timeMid); |
---|
138 | std::swap(_timeHiAndVersion, uuid._timeHiAndVersion); |
---|
139 | std::swap(_clockSeq, uuid._clockSeq); |
---|
140 | std::swap_ranges(_node, _node + 6, &uuid._node[0]); |
---|
141 | } |
---|
142 | |
---|
143 | |
---|
144 | void UUID::parse(const std::string& uuid) |
---|
145 | { |
---|
146 | if (uuid.size() < 36) |
---|
147 | throw SyntaxException(uuid); |
---|
148 | |
---|
149 | if (uuid[8] != '-'|| uuid[13] != '-' || uuid[18] != '-' || uuid[23] != '-') |
---|
150 | throw SyntaxException(uuid); |
---|
151 | |
---|
152 | std::string::const_iterator it = uuid.begin(); |
---|
153 | _timeLow = 0; |
---|
154 | for (int i = 0; i < 8; ++i) |
---|
155 | { |
---|
156 | _timeLow = (_timeLow << 4) | nibble(*it++); |
---|
157 | } |
---|
158 | ++it; |
---|
159 | _timeMid = 0; |
---|
160 | for (int i = 0; i < 4; ++i) |
---|
161 | { |
---|
162 | _timeMid = (_timeMid << 4) | nibble(*it++); |
---|
163 | } |
---|
164 | ++it; |
---|
165 | _timeHiAndVersion = 0; |
---|
166 | for (int i = 0; i < 4; ++i) |
---|
167 | { |
---|
168 | _timeHiAndVersion = (_timeHiAndVersion << 4) | nibble(*it++); |
---|
169 | } |
---|
170 | ++it; |
---|
171 | _clockSeq = 0; |
---|
172 | for (int i = 0; i < 4; ++i) |
---|
173 | { |
---|
174 | _clockSeq = (_clockSeq << 4) | nibble(*it++); |
---|
175 | } |
---|
176 | ++it; |
---|
177 | for (int i = 0; i < 6; ++i) |
---|
178 | { |
---|
179 | _node[i] = (nibble(*it++) << 4) | nibble(*it++) ; |
---|
180 | } |
---|
181 | } |
---|
182 | |
---|
183 | |
---|
184 | std::string UUID::toString() const |
---|
185 | { |
---|
186 | std::string result; |
---|
187 | result.reserve(36); |
---|
188 | appendHex(result, _timeLow); |
---|
189 | result += '-'; |
---|
190 | appendHex(result, _timeMid); |
---|
191 | result += '-'; |
---|
192 | appendHex(result, _timeHiAndVersion); |
---|
193 | result += '-'; |
---|
194 | appendHex(result, _clockSeq); |
---|
195 | result += '-'; |
---|
196 | for (int i = 0; i < sizeof(_node); ++i) |
---|
197 | appendHex(result, _node[i]); |
---|
198 | return result; |
---|
199 | } |
---|
200 | |
---|
201 | |
---|
202 | void UUID::copyFrom(const char* buffer) |
---|
203 | { |
---|
204 | UInt32 i32; |
---|
205 | UInt16 i16; |
---|
206 | std::memcpy(&i32, buffer, sizeof(i32)); |
---|
207 | _timeLow = ByteOrder::fromNetwork(i32); |
---|
208 | buffer += sizeof(i32); |
---|
209 | std::memcpy(&i16, buffer, sizeof(i16)); |
---|
210 | _timeMid = ByteOrder::fromNetwork(i16); |
---|
211 | buffer += sizeof(i16); |
---|
212 | std::memcpy(&i16, buffer, sizeof(i16)); |
---|
213 | _timeHiAndVersion = ByteOrder::fromNetwork(i16); |
---|
214 | buffer += sizeof(i16); |
---|
215 | std::memcpy(&i16, buffer, sizeof(i16)); |
---|
216 | _clockSeq = ByteOrder::fromNetwork(i16); |
---|
217 | buffer += sizeof(i16); |
---|
218 | std::memcpy(_node, buffer, sizeof(_node)); |
---|
219 | } |
---|
220 | |
---|
221 | |
---|
222 | void UUID::copyTo(char* buffer) const |
---|
223 | { |
---|
224 | UInt32 i32 = ByteOrder::toNetwork(_timeLow); |
---|
225 | std::memcpy(buffer, &i32, sizeof(i32)); |
---|
226 | buffer += sizeof(i32); |
---|
227 | UInt16 i16 = ByteOrder::toNetwork(_timeMid); |
---|
228 | std::memcpy(buffer, &i16, sizeof(i16)); |
---|
229 | buffer += sizeof(i16); |
---|
230 | i16 = ByteOrder::toNetwork(_timeHiAndVersion); |
---|
231 | std::memcpy(buffer, &i16, sizeof(i16)); |
---|
232 | buffer += sizeof(i16); |
---|
233 | i16 = ByteOrder::toNetwork(_clockSeq); |
---|
234 | std::memcpy(buffer, &i16, sizeof(i16)); |
---|
235 | buffer += sizeof(i16); |
---|
236 | std::memcpy(buffer, _node, sizeof(_node)); |
---|
237 | } |
---|
238 | |
---|
239 | |
---|
240 | int UUID::variant() const |
---|
241 | { |
---|
242 | int v = _clockSeq >> 13; |
---|
243 | if ((v & 6) == 6) |
---|
244 | return v; |
---|
245 | else if (v & 4) |
---|
246 | return 2; |
---|
247 | else |
---|
248 | return 0; |
---|
249 | } |
---|
250 | |
---|
251 | |
---|
252 | int UUID::compare(const UUID& uuid) const |
---|
253 | { |
---|
254 | if (_timeLow != uuid._timeLow) return _timeLow < uuid._timeLow ? -1 : 1; |
---|
255 | if (_timeMid != uuid._timeMid) return _timeMid < uuid._timeMid ? -1 : 1; |
---|
256 | if (_timeHiAndVersion != uuid._timeHiAndVersion) return _timeHiAndVersion < uuid._timeHiAndVersion ? -1 : 1; |
---|
257 | if (_clockSeq != uuid._clockSeq) return _clockSeq < uuid._clockSeq ? -1 : 1; |
---|
258 | for (int i = 0; i < sizeof(_node); ++i) |
---|
259 | { |
---|
260 | if (_node[i] < uuid._node[i]) |
---|
261 | return -1; |
---|
262 | else if (_node[i] > uuid._node[i]) |
---|
263 | return 1; |
---|
264 | } |
---|
265 | return 0; |
---|
266 | } |
---|
267 | |
---|
268 | |
---|
269 | void UUID::appendHex(std::string& str, UInt8 n) |
---|
270 | { |
---|
271 | static const char* digits = "0123456789abcdef"; |
---|
272 | str += digits[(n >> 4) & 0xF]; |
---|
273 | str += digits[n & 0xF]; |
---|
274 | } |
---|
275 | |
---|
276 | |
---|
277 | void UUID::appendHex(std::string& str, UInt16 n) |
---|
278 | { |
---|
279 | appendHex(str, UInt8(n >> 8)); |
---|
280 | appendHex(str, UInt8(n & 0xFF)); |
---|
281 | } |
---|
282 | |
---|
283 | |
---|
284 | void UUID::appendHex(std::string& str, UInt32 n) |
---|
285 | { |
---|
286 | appendHex(str, UInt16(n >> 16)); |
---|
287 | appendHex(str, UInt16(n & 0xFFFF)); |
---|
288 | } |
---|
289 | |
---|
290 | |
---|
291 | UInt8 UUID::nibble(char hex) |
---|
292 | { |
---|
293 | if (hex >= 'a' && hex <= 'f') |
---|
294 | return UInt8(hex - 'a' + 10); |
---|
295 | else if (hex >= 'A' && hex <= 'F') |
---|
296 | return UInt8(hex - 'A' + 10); |
---|
297 | else if (hex >= '0' && hex <= '9') |
---|
298 | return UInt8(hex - '0'); |
---|
299 | else |
---|
300 | return UInt8(0); |
---|
301 | } |
---|
302 | |
---|
303 | |
---|
304 | void UUID::fromNetwork() |
---|
305 | { |
---|
306 | _timeLow = ByteOrder::fromNetwork(_timeLow); |
---|
307 | _timeMid = ByteOrder::fromNetwork(_timeMid); |
---|
308 | _timeHiAndVersion = ByteOrder::fromNetwork(_timeHiAndVersion); |
---|
309 | _clockSeq = ByteOrder::fromNetwork(_clockSeq); |
---|
310 | } |
---|
311 | |
---|
312 | |
---|
313 | void UUID::toNetwork() |
---|
314 | { |
---|
315 | _timeLow = ByteOrder::toNetwork(_timeLow); |
---|
316 | _timeMid = ByteOrder::toNetwork(_timeMid); |
---|
317 | _timeHiAndVersion = ByteOrder::toNetwork(_timeHiAndVersion); |
---|
318 | _clockSeq = ByteOrder::toNetwork(_clockSeq); |
---|
319 | } |
---|
320 | |
---|
321 | |
---|
322 | const UUID& UUID::nil() |
---|
323 | { |
---|
324 | static UUID nil; |
---|
325 | return nil; |
---|
326 | } |
---|
327 | |
---|
328 | |
---|
329 | const UUID& UUID::dns() |
---|
330 | { |
---|
331 | static UUID uuidDNS("6ba7b810-9dad-11d1-80b4-00c04fd430c8"); |
---|
332 | return uuidDNS; |
---|
333 | } |
---|
334 | |
---|
335 | |
---|
336 | const UUID& UUID::uri() |
---|
337 | { |
---|
338 | static UUID uuidURI("6ba7b811-9dad-11d1-80b4-00c04fd430c8"); |
---|
339 | return uuidURI; |
---|
340 | } |
---|
341 | |
---|
342 | |
---|
343 | const UUID& UUID::oid() |
---|
344 | { |
---|
345 | static UUID uuidOID("6ba7b812-9dad-11d1-80b4-00c04fd430c8"); |
---|
346 | return uuidOID; |
---|
347 | } |
---|
348 | |
---|
349 | |
---|
350 | const UUID& UUID::x500() |
---|
351 | { |
---|
352 | static UUID uuidX500("6ba7b814-9dad-11d1-80b4-00c04fd430c8"); |
---|
353 | return uuidX500; |
---|
354 | } |
---|
355 | |
---|
356 | |
---|
357 | } // namespace Poco |
---|