Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/plugin/quuid.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||
---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||
2 | ** | - | ||||||
3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||
4 | ** Contact: https://www.qt.io/licensing/ | - | ||||||
5 | ** | - | ||||||
6 | ** This file is part of the QtCore module of the Qt Toolkit. | - | ||||||
7 | ** | - | ||||||
8 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||
9 | ** Commercial License Usage | - | ||||||
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||
11 | ** accordance with the commercial license agreement provided with the | - | ||||||
12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||
13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||
14 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||
15 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||
16 | ** | - | ||||||
17 | ** GNU Lesser General Public License Usage | - | ||||||
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||
19 | ** General Public License version 3 as published by the Free Software | - | ||||||
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||
21 | ** packaging of this file. Please review the following information to | - | ||||||
22 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||
24 | ** | - | ||||||
25 | ** GNU General Public License Usage | - | ||||||
26 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||
27 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||
28 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||
29 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||
31 | ** included in the packaging of this file. Please review the following | - | ||||||
32 | ** information to ensure the GNU General Public License requirements will | - | ||||||
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||
35 | ** | - | ||||||
36 | ** $QT_END_LICENSE$ | - | ||||||
37 | ** | - | ||||||
38 | ****************************************************************************/ | - | ||||||
39 | - | |||||||
40 | #include "quuid.h" | - | ||||||
41 | - | |||||||
42 | #include "qdatastream.h" | - | ||||||
43 | #include "qendian.h" | - | ||||||
44 | #include "qdebug.h" | - | ||||||
45 | #include "private/qtools_p.h" | - | ||||||
46 | - | |||||||
47 | #ifndef QT_BOOTSTRAPPED | - | ||||||
48 | #include "qcryptographichash.h" | - | ||||||
49 | #endif | - | ||||||
50 | QT_BEGIN_NAMESPACE | - | ||||||
51 | - | |||||||
52 | template <class Char, class Integral> | - | ||||||
53 | void _q_toHex(Char *&dst, Integral value) | - | ||||||
54 | { | - | ||||||
55 | value = qToBigEndian(value); | - | ||||||
56 | - | |||||||
57 | const char* p = reinterpret_cast<const char*>(&value); | - | ||||||
58 | - | |||||||
59 | for (uint i = 0; i < sizeof(Integral); ++i, dst += 2) { | - | ||||||
60 | dst[0] = Char(QtMiscUtils::toHexLower((p[i] >> 4) & 0xf)); | - | ||||||
61 | dst[1] = Char(QtMiscUtils::toHexLower(p[i] & 0xf)); | - | ||||||
62 | } | - | ||||||
63 | } | - | ||||||
64 | - | |||||||
65 | template <class Char, class Integral> | - | ||||||
66 | bool _q_fromHex(const Char *&src, Integral &value) | - | ||||||
67 | { | - | ||||||
68 | value = 0; | - | ||||||
69 | - | |||||||
70 | for (uint i = 0; i < sizeof(Integral) * 2; ++i) { | - | ||||||
71 | uint ch = *src++; | - | ||||||
72 | int tmp = QtMiscUtils::fromHex(ch); | - | ||||||
73 | if (tmp == -1) | - | ||||||
74 | return false; | - | ||||||
75 | - | |||||||
76 | value = value * 16 + tmp; | - | ||||||
77 | } | - | ||||||
78 | - | |||||||
79 | return true; | - | ||||||
80 | } | - | ||||||
81 | - | |||||||
82 | template <class Char> | - | ||||||
83 | void _q_uuidToHex(Char *&dst, const uint &d1, const ushort &d2, const ushort &d3, const uchar (&d4)[8]) | - | ||||||
84 | { | - | ||||||
85 | *dst++ = Char('{'); | - | ||||||
86 | _q_toHex(dst, d1); | - | ||||||
87 | *dst++ = Char('-'); | - | ||||||
88 | _q_toHex(dst, d2); | - | ||||||
89 | *dst++ = Char('-'); | - | ||||||
90 | _q_toHex(dst, d3); | - | ||||||
91 | *dst++ = Char('-'); | - | ||||||
92 | for (int i = 0; i < 2; i++) | - | ||||||
93 | _q_toHex(dst, d4[i]); | - | ||||||
94 | *dst++ = Char('-'); | - | ||||||
95 | for (int i = 2; i < 8; i++) | - | ||||||
96 | _q_toHex(dst, d4[i]); | - | ||||||
97 | *dst = Char('}'); | - | ||||||
98 | } | - | ||||||
99 | - | |||||||
100 | template <class Char> | - | ||||||
101 | bool _q_uuidFromHex(const Char *&src, uint &d1, ushort &d2, ushort &d3, uchar (&d4)[8]) | - | ||||||
102 | { | - | ||||||
103 | if (*src == Char('{')) | - | ||||||
104 | src++; | - | ||||||
105 | if (!_q_fromHex(src, d1) | - | ||||||
106 | || *src++ != Char('-') | - | ||||||
107 | || !_q_fromHex(src, d2) | - | ||||||
108 | || *src++ != Char('-') | - | ||||||
109 | || !_q_fromHex(src, d3) | - | ||||||
110 | || *src++ != Char('-') | - | ||||||
111 | || !_q_fromHex(src, d4[0]) | - | ||||||
112 | || !_q_fromHex(src, d4[1]) | - | ||||||
113 | || *src++ != Char('-') | - | ||||||
114 | || !_q_fromHex(src, d4[2]) | - | ||||||
115 | || !_q_fromHex(src, d4[3]) | - | ||||||
116 | || !_q_fromHex(src, d4[4]) | - | ||||||
117 | || !_q_fromHex(src, d4[5]) | - | ||||||
118 | || !_q_fromHex(src, d4[6]) | - | ||||||
119 | || !_q_fromHex(src, d4[7])) { | - | ||||||
120 | return false; | - | ||||||
121 | } | - | ||||||
122 | - | |||||||
123 | return true; | - | ||||||
124 | } | - | ||||||
125 | - | |||||||
126 | #ifndef QT_BOOTSTRAPPED | - | ||||||
127 | static QUuid createFromName(const QUuid &ns, const QByteArray &baseData, QCryptographicHash::Algorithm algorithm, int version) | - | ||||||
128 | { | - | ||||||
129 | QByteArray hashResult; | - | ||||||
130 | - | |||||||
131 | // create a scope so later resize won't reallocate | - | ||||||
132 | { | - | ||||||
133 | QCryptographicHash hash(algorithm); | - | ||||||
134 | hash.addData(ns.toRfc4122()); | - | ||||||
135 | hash.addData(baseData); | - | ||||||
136 | hashResult = hash.result(); | - | ||||||
137 | } | - | ||||||
138 | hashResult.resize(16); // Sha1 will be too long | - | ||||||
139 | - | |||||||
140 | QUuid result = QUuid::fromRfc4122(hashResult); | - | ||||||
141 | - | |||||||
142 | result.data3 &= 0x0FFF; | - | ||||||
143 | result.data3 |= (version << 12); | - | ||||||
144 | result.data4[0] &= 0x3F; | - | ||||||
145 | result.data4[0] |= 0x80; | - | ||||||
146 | - | |||||||
147 | return result; | - | ||||||
148 | } | - | ||||||
149 | #endif | - | ||||||
150 | - | |||||||
151 | /*! | - | ||||||
152 | \class QUuid | - | ||||||
153 | \inmodule QtCore | - | ||||||
154 | \brief The QUuid class stores a Universally Unique Identifier (UUID). | - | ||||||
155 | - | |||||||
156 | \reentrant | - | ||||||
157 | - | |||||||
158 | Using \e{U}niversally \e{U}nique \e{ID}entifiers (UUID) is a | - | ||||||
159 | standard way to uniquely identify entities in a distributed | - | ||||||
160 | computing environment. A UUID is a 16-byte (128-bit) number | - | ||||||
161 | generated by some algorithm that is meant to guarantee that the | - | ||||||
162 | UUID will be unique in the distributed computing environment where | - | ||||||
163 | it is used. The acronym GUID is often used instead, \e{G}lobally | - | ||||||
164 | \e{U}nique \e{ID}entifiers, but it refers to the same thing. | - | ||||||
165 | - | |||||||
166 | \target Variant field | - | ||||||
167 | Actually, the GUID is one \e{variant} of UUID. Multiple variants | - | ||||||
168 | are in use. Each UUID contains a bit field that specifies which | - | ||||||
169 | type (variant) of UUID it is. Call variant() to discover which | - | ||||||
170 | type of UUID an instance of QUuid contains. It extracts the three | - | ||||||
171 | most significant bits of byte 8 of the 16 bytes. In QUuid, byte 8 | - | ||||||
172 | is \c{QUuid::data4[0]}. If you create instances of QUuid using the | - | ||||||
173 | constructor that accepts all the numeric values as parameters, use | - | ||||||
174 | the following table to set the three most significant bits of | - | ||||||
175 | parameter \c{b1}, which becomes \c{QUuid::data4[0]} and contains | - | ||||||
176 | the variant field in its three most significant bits. In the | - | ||||||
177 | table, 'x' means \e {don't care}. | - | ||||||
178 | - | |||||||
179 | \table | - | ||||||
180 | \header | - | ||||||
181 | \li msb0 | - | ||||||
182 | \li msb1 | - | ||||||
183 | \li msb2 | - | ||||||
184 | \li Variant | - | ||||||
185 | - | |||||||
186 | \row | - | ||||||
187 | \li 0 | - | ||||||
188 | \li x | - | ||||||
189 | \li x | - | ||||||
190 | \li NCS (Network Computing System) | - | ||||||
191 | - | |||||||
192 | \row | - | ||||||
193 | \li 1 | - | ||||||
194 | \li 0 | - | ||||||
195 | \li x | - | ||||||
196 | \li DCE (Distributed Computing Environment) | - | ||||||
197 | - | |||||||
198 | \row | - | ||||||
199 | \li 1 | - | ||||||
200 | \li 1 | - | ||||||
201 | \li 0 | - | ||||||
202 | \li Microsoft (GUID) | - | ||||||
203 | - | |||||||
204 | \row | - | ||||||
205 | \li 1 | - | ||||||
206 | \li 1 | - | ||||||
207 | \li 1 | - | ||||||
208 | \li Reserved for future expansion | - | ||||||
209 | - | |||||||
210 | \endtable | - | ||||||
211 | - | |||||||
212 | \target Version field | - | ||||||
213 | If variant() returns QUuid::DCE, the UUID also contains a | - | ||||||
214 | \e{version} field in the four most significant bits of | - | ||||||
215 | \c{QUuid::data3}, and you can call version() to discover which | - | ||||||
216 | version your QUuid contains. If you create instances of QUuid | - | ||||||
217 | using the constructor that accepts all the numeric values as | - | ||||||
218 | parameters, use the following table to set the four most | - | ||||||
219 | significant bits of parameter \c{w2}, which becomes | - | ||||||
220 | \c{QUuid::data3} and contains the version field in its four most | - | ||||||
221 | significant bits. | - | ||||||
222 | - | |||||||
223 | \table | - | ||||||
224 | \header | - | ||||||
225 | \li msb0 | - | ||||||
226 | \li msb1 | - | ||||||
227 | \li msb2 | - | ||||||
228 | \li msb3 | - | ||||||
229 | \li Version | - | ||||||
230 | - | |||||||
231 | \row | - | ||||||
232 | \li 0 | - | ||||||
233 | \li 0 | - | ||||||
234 | \li 0 | - | ||||||
235 | \li 1 | - | ||||||
236 | \li Time | - | ||||||
237 | - | |||||||
238 | \row | - | ||||||
239 | \li 0 | - | ||||||
240 | \li 0 | - | ||||||
241 | \li 1 | - | ||||||
242 | \li 0 | - | ||||||
243 | \li Embedded POSIX | - | ||||||
244 | - | |||||||
245 | \row | - | ||||||
246 | \li 0 | - | ||||||
247 | \li 0 | - | ||||||
248 | \li 1 | - | ||||||
249 | \li 1 | - | ||||||
250 | \li Md5(Name) | - | ||||||
251 | - | |||||||
252 | \row | - | ||||||
253 | \li 0 | - | ||||||
254 | \li 1 | - | ||||||
255 | \li 0 | - | ||||||
256 | \li 0 | - | ||||||
257 | \li Random | - | ||||||
258 | - | |||||||
259 | \row | - | ||||||
260 | \li 0 | - | ||||||
261 | \li 1 | - | ||||||
262 | \li 0 | - | ||||||
263 | \li 1 | - | ||||||
264 | \li Sha1 | - | ||||||
265 | - | |||||||
266 | \endtable | - | ||||||
267 | - | |||||||
268 | The field layouts for the DCE versions listed in the table above | - | ||||||
269 | are specified in the \l{http://www.ietf.org/rfc/rfc4122.txt} | - | ||||||
270 | {Network Working Group UUID Specification}. | - | ||||||
271 | - | |||||||
272 | Most platforms provide a tool for generating new UUIDs, e.g. \c | - | ||||||
273 | uuidgen and \c guidgen. You can also use createUuid(). UUIDs | - | ||||||
274 | generated by createUuid() are of the random type. Their | - | ||||||
275 | QUuid::Version bits are set to QUuid::Random, and their | - | ||||||
276 | QUuid::Variant bits are set to QUuid::DCE. The rest of the UUID is | - | ||||||
277 | composed of random numbers. Theoretically, this means there is a | - | ||||||
278 | small chance that a UUID generated by createUuid() will not be | - | ||||||
279 | unique. But it is | - | ||||||
280 | \l{http://en.wikipedia.org/wiki/Universally_Unique_Identifier#Random_UUID_probability_of_duplicates} | - | ||||||
281 | {a \e{very} small chance}. | - | ||||||
282 | - | |||||||
283 | UUIDs can be constructed from numeric values or from strings, or | - | ||||||
284 | using the static createUuid() function. They can be converted to a | - | ||||||
285 | string with toString(). UUIDs have a variant() and a version(), | - | ||||||
286 | and null UUIDs return true from isNull(). | - | ||||||
287 | */ | - | ||||||
288 | - | |||||||
289 | /*! | - | ||||||
290 | \fn QUuid::QUuid(const GUID &guid) | - | ||||||
291 | - | |||||||
292 | Casts a Windows \a guid to a Qt QUuid. | - | ||||||
293 | - | |||||||
294 | \warning This function is only for Windows platforms. | - | ||||||
295 | */ | - | ||||||
296 | - | |||||||
297 | /*! | - | ||||||
298 | \fn QUuid &QUuid::operator=(const GUID &guid) | - | ||||||
299 | - | |||||||
300 | Assigns a Windows \a guid to a Qt QUuid. | - | ||||||
301 | - | |||||||
302 | \warning This function is only for Windows platforms. | - | ||||||
303 | */ | - | ||||||
304 | - | |||||||
305 | /*! | - | ||||||
306 | \fn QUuid::operator GUID() const | - | ||||||
307 | - | |||||||
308 | Returns a Windows GUID from a QUuid. | - | ||||||
309 | - | |||||||
310 | \warning This function is only for Windows platforms. | - | ||||||
311 | */ | - | ||||||
312 | - | |||||||
313 | /*! | - | ||||||
314 | \fn QUuid::QUuid() | - | ||||||
315 | - | |||||||
316 | Creates the null UUID. toString() will output the null UUID | - | ||||||
317 | as "{00000000-0000-0000-0000-000000000000}". | - | ||||||
318 | */ | - | ||||||
319 | - | |||||||
320 | /*! | - | ||||||
321 | \fn QUuid::QUuid(uint l, ushort w1, ushort w2, uchar b1, uchar b2, uchar b3, uchar b4, uchar b5, uchar b6, uchar b7, uchar b8) | - | ||||||
322 | - | |||||||
323 | Creates a UUID with the value specified by the parameters, \a l, | - | ||||||
324 | \a w1, \a w2, \a b1, \a b2, \a b3, \a b4, \a b5, \a b6, \a b7, \a | - | ||||||
325 | b8. | - | ||||||
326 | - | |||||||
327 | Example: | - | ||||||
328 | \snippet code/src_corelib_plugin_quuid.cpp 0 | - | ||||||
329 | */ | - | ||||||
330 | - | |||||||
331 | /*! | - | ||||||
332 | Creates a QUuid object from the string \a text, which must be | - | ||||||
333 | formatted as five hex fields separated by '-', e.g., | - | ||||||
334 | "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}" where 'x' is a hex | - | ||||||
335 | digit. The curly braces shown here are optional, but it is normal to | - | ||||||
336 | include them. If the conversion fails, a null UUID is created. See | - | ||||||
337 | toString() for an explanation of how the five hex fields map to the | - | ||||||
338 | public data members in QUuid. | - | ||||||
339 | - | |||||||
340 | \sa toString(), QUuid() | - | ||||||
341 | */ | - | ||||||
342 | QUuid::QUuid(const QString &text) | - | ||||||
343 | { | - | ||||||
344 | if (text.length() < 36) { | - | ||||||
345 | *this = QUuid(); | - | ||||||
346 | return; | - | ||||||
347 | } | - | ||||||
348 | - | |||||||
349 | const ushort *data = reinterpret_cast<const ushort *>(text.unicode()); | - | ||||||
350 | - | |||||||
351 | if (*data == '{' && text.length() < 37) { | - | ||||||
352 | *this = QUuid(); | - | ||||||
353 | return; | - | ||||||
354 | } | - | ||||||
355 | - | |||||||
356 | if (!_q_uuidFromHex(data, data1, data2, data3, data4)) { | - | ||||||
357 | *this = QUuid(); | - | ||||||
358 | return; | - | ||||||
359 | } | - | ||||||
360 | } | - | ||||||
361 | - | |||||||
362 | /*! | - | ||||||
363 | \internal | - | ||||||
364 | */ | - | ||||||
365 | QUuid::QUuid(const char *text) | - | ||||||
366 | { | - | ||||||
367 | if (!text) { | - | ||||||
368 | *this = QUuid(); | - | ||||||
369 | return; | - | ||||||
370 | } | - | ||||||
371 | - | |||||||
372 | if (!_q_uuidFromHex(text, data1, data2, data3, data4)) { | - | ||||||
373 | *this = QUuid(); | - | ||||||
374 | return; | - | ||||||
375 | } | - | ||||||
376 | } | - | ||||||
377 | - | |||||||
378 | /*! | - | ||||||
379 | Creates a QUuid object from the QByteArray \a text, which must be | - | ||||||
380 | formatted as five hex fields separated by '-', e.g., | - | ||||||
381 | "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}" where 'x' is a hex | - | ||||||
382 | digit. The curly braces shown here are optional, but it is normal to | - | ||||||
383 | include them. If the conversion fails, a null UUID is created. See | - | ||||||
384 | toByteArray() for an explanation of how the five hex fields map to the | - | ||||||
385 | public data members in QUuid. | - | ||||||
386 | - | |||||||
387 | \since 4.8 | - | ||||||
388 | - | |||||||
389 | \sa toByteArray(), QUuid() | - | ||||||
390 | */ | - | ||||||
391 | QUuid::QUuid(const QByteArray &text) | - | ||||||
392 | { | - | ||||||
393 | if (text.length() < 36) { | - | ||||||
394 | *this = QUuid(); | - | ||||||
395 | return; | - | ||||||
396 | } | - | ||||||
397 | - | |||||||
398 | const char *data = text.constData(); | - | ||||||
399 | - | |||||||
400 | if (*data == '{' && text.length() < 37) { | - | ||||||
401 | *this = QUuid(); | - | ||||||
402 | return; | - | ||||||
403 | } | - | ||||||
404 | - | |||||||
405 | if (!_q_uuidFromHex(data, data1, data2, data3, data4)) { | - | ||||||
406 | *this = QUuid(); | - | ||||||
407 | return; | - | ||||||
408 | } | - | ||||||
409 | } | - | ||||||
410 | - | |||||||
411 | /*! | - | ||||||
412 | \since 5.0 | - | ||||||
413 | \fn QUuid QUuid::createUuidV3(const QUuid &ns, const QByteArray &baseData); | - | ||||||
414 | - | |||||||
415 | This function returns a new UUID with variant QUuid::DCE and version QUuid::Md5. | - | ||||||
416 | \a ns is the namespace and \a baseData is the basic data as described by RFC 4122. | - | ||||||
417 | - | |||||||
418 | \sa variant(), version(), createUuidV5() | - | ||||||
419 | */ | - | ||||||
420 | - | |||||||
421 | /*! | - | ||||||
422 | \since 5.0 | - | ||||||
423 | \fn QUuid QUuid::createUuidV3(const QUuid &ns, const QString &baseData); | - | ||||||
424 | - | |||||||
425 | This function returns a new UUID with variant QUuid::DCE and version QUuid::Md5. | - | ||||||
426 | \a ns is the namespace and \a baseData is the basic data as described by RFC 4122. | - | ||||||
427 | - | |||||||
428 | \sa variant(), version(), createUuidV5() | - | ||||||
429 | */ | - | ||||||
430 | - | |||||||
431 | /*! | - | ||||||
432 | \since 5.0 | - | ||||||
433 | \fn QUuid QUuid::createUuidV5(const QUuid &ns, const QByteArray &baseData); | - | ||||||
434 | - | |||||||
435 | This function returns a new UUID with variant QUuid::DCE and version QUuid::Sha1. | - | ||||||
436 | \a ns is the namespace and \a baseData is the basic data as described by RFC 4122. | - | ||||||
437 | - | |||||||
438 | \sa variant(), version(), createUuidV3() | - | ||||||
439 | */ | - | ||||||
440 | - | |||||||
441 | /*! | - | ||||||
442 | \since 5.0 | - | ||||||
443 | \fn QUuid QUuid::createUuidV5(const QUuid &ns, const QString &baseData); | - | ||||||
444 | - | |||||||
445 | This function returns a new UUID with variant QUuid::DCE and version QUuid::Sha1. | - | ||||||
446 | \a ns is the namespace and \a baseData is the basic data as described by RFC 4122. | - | ||||||
447 | - | |||||||
448 | \sa variant(), version(), createUuidV3() | - | ||||||
449 | */ | - | ||||||
450 | #ifndef QT_BOOTSTRAPPED | - | ||||||
451 | QUuid QUuid::createUuidV3(const QUuid &ns, const QByteArray &baseData) | - | ||||||
452 | { | - | ||||||
453 | return createFromName(ns, baseData, QCryptographicHash::Md5, 3); | - | ||||||
454 | } | - | ||||||
455 | - | |||||||
456 | QUuid QUuid::createUuidV5(const QUuid &ns, const QByteArray &baseData) | - | ||||||
457 | { | - | ||||||
458 | return createFromName(ns, baseData, QCryptographicHash::Sha1, 5); | - | ||||||
459 | } | - | ||||||
460 | #endif | - | ||||||
461 | - | |||||||
462 | /*! | - | ||||||
463 | Creates a QUuid object from the binary representation of the UUID, as | - | ||||||
464 | specified by RFC 4122 section 4.1.2. See toRfc4122() for a further | - | ||||||
465 | explanation of the order of \a bytes required. | - | ||||||
466 | - | |||||||
467 | The byte array accepted is NOT a human readable format. | - | ||||||
468 | - | |||||||
469 | If the conversion fails, a null UUID is created. | - | ||||||
470 | - | |||||||
471 | \since 4.8 | - | ||||||
472 | - | |||||||
473 | \sa toRfc4122(), QUuid() | - | ||||||
474 | */ | - | ||||||
475 | QUuid QUuid::fromRfc4122(const QByteArray &bytes) | - | ||||||
476 | { | - | ||||||
477 | if (bytes.isEmpty() || bytes.length() != 16) | - | ||||||
478 | return QUuid(); | - | ||||||
479 | - | |||||||
480 | uint d1; | - | ||||||
481 | ushort d2, d3; | - | ||||||
482 | uchar d4[8]; | - | ||||||
483 | - | |||||||
484 | const uchar *data = reinterpret_cast<const uchar *>(bytes.constData()); | - | ||||||
485 | - | |||||||
486 | d1 = qFromBigEndian<quint32>(data); | - | ||||||
487 | data += sizeof(quint32); | - | ||||||
488 | d2 = qFromBigEndian<quint16>(data); | - | ||||||
489 | data += sizeof(quint16); | - | ||||||
490 | d3 = qFromBigEndian<quint16>(data); | - | ||||||
491 | data += sizeof(quint16); | - | ||||||
492 | - | |||||||
493 | for (int i = 0; i < 8; ++i) { | - | ||||||
494 | d4[i] = *(data); | - | ||||||
495 | data++; | - | ||||||
496 | } | - | ||||||
497 | - | |||||||
498 | return QUuid(d1, d2, d3, d4[0], d4[1], d4[2], d4[3], d4[4], d4[5], d4[6], d4[7]); | - | ||||||
499 | } | - | ||||||
500 | - | |||||||
501 | /*! | - | ||||||
502 | \fn bool QUuid::operator==(const QUuid &other) const | - | ||||||
503 | - | |||||||
504 | Returns \c true if this QUuid and the \a other QUuid are identical; | - | ||||||
505 | otherwise returns \c false. | - | ||||||
506 | */ | - | ||||||
507 | - | |||||||
508 | /*! | - | ||||||
509 | \fn bool QUuid::operator!=(const QUuid &other) const | - | ||||||
510 | - | |||||||
511 | Returns \c true if this QUuid and the \a other QUuid are different; | - | ||||||
512 | otherwise returns \c false. | - | ||||||
513 | */ | - | ||||||
514 | - | |||||||
515 | /*! | - | ||||||
516 | Returns the string representation of this QUuid. The string is | - | ||||||
517 | formatted as five hex fields separated by '-' and enclosed in | - | ||||||
518 | curly braces, i.e., "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}" where | - | ||||||
519 | 'x' is a hex digit. From left to right, the five hex fields are | - | ||||||
520 | obtained from the four public data members in QUuid as follows: | - | ||||||
521 | - | |||||||
522 | \table | - | ||||||
523 | \header | - | ||||||
524 | \li Field # | - | ||||||
525 | \li Source | - | ||||||
526 | - | |||||||
527 | \row | - | ||||||
528 | \li 1 | - | ||||||
529 | \li data1 | - | ||||||
530 | - | |||||||
531 | \row | - | ||||||
532 | \li 2 | - | ||||||
533 | \li data2 | - | ||||||
534 | - | |||||||
535 | \row | - | ||||||
536 | \li 3 | - | ||||||
537 | \li data3 | - | ||||||
538 | - | |||||||
539 | \row | - | ||||||
540 | \li 4 | - | ||||||
541 | \li data4[0] .. data4[1] | - | ||||||
542 | - | |||||||
543 | \row | - | ||||||
544 | \li 5 | - | ||||||
545 | \li data4[2] .. data4[7] | - | ||||||
546 | - | |||||||
547 | \endtable | - | ||||||
548 | */ | - | ||||||
549 | QString QUuid::toString() const | - | ||||||
550 | { | - | ||||||
551 | QString result(38, Qt::Uninitialized); | - | ||||||
552 | ushort *data = (ushort *)result.data(); | - | ||||||
553 | - | |||||||
554 | _q_uuidToHex(data, data1, data2, data3, data4); | - | ||||||
555 | - | |||||||
556 | return result; | - | ||||||
557 | } | - | ||||||
558 | - | |||||||
559 | /*! | - | ||||||
560 | Returns the binary representation of this QUuid. The byte array is | - | ||||||
561 | formatted as five hex fields separated by '-' and enclosed in | - | ||||||
562 | curly braces, i.e., "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}" where | - | ||||||
563 | 'x' is a hex digit. From left to right, the five hex fields are | - | ||||||
564 | obtained from the four public data members in QUuid as follows: | - | ||||||
565 | - | |||||||
566 | \table | - | ||||||
567 | \header | - | ||||||
568 | \li Field # | - | ||||||
569 | \li Source | - | ||||||
570 | - | |||||||
571 | \row | - | ||||||
572 | \li 1 | - | ||||||
573 | \li data1 | - | ||||||
574 | - | |||||||
575 | \row | - | ||||||
576 | \li 2 | - | ||||||
577 | \li data2 | - | ||||||
578 | - | |||||||
579 | \row | - | ||||||
580 | \li 3 | - | ||||||
581 | \li data3 | - | ||||||
582 | - | |||||||
583 | \row | - | ||||||
584 | \li 4 | - | ||||||
585 | \li data4[0] .. data4[1] | - | ||||||
586 | - | |||||||
587 | \row | - | ||||||
588 | \li 5 | - | ||||||
589 | \li data4[2] .. data4[7] | - | ||||||
590 | - | |||||||
591 | \endtable | - | ||||||
592 | - | |||||||
593 | \since 4.8 | - | ||||||
594 | */ | - | ||||||
595 | QByteArray QUuid::toByteArray() const | - | ||||||
596 | { | - | ||||||
597 | QByteArray result(38, Qt::Uninitialized); | - | ||||||
598 | char *data = result.data(); | - | ||||||
599 | - | |||||||
600 | _q_uuidToHex(data, data1, data2, data3, data4); | - | ||||||
601 | - | |||||||
602 | return result; | - | ||||||
603 | } | - | ||||||
604 | - | |||||||
605 | /*! | - | ||||||
606 | Returns the binary representation of this QUuid. The byte array is in big | - | ||||||
607 | endian format, and formatted according to RFC 4122, section 4.1.2 - | - | ||||||
608 | "Layout and byte order". | - | ||||||
609 | - | |||||||
610 | The order is as follows: | - | ||||||
611 | - | |||||||
612 | \table | - | ||||||
613 | \header | - | ||||||
614 | \li Field # | - | ||||||
615 | \li Source | - | ||||||
616 | - | |||||||
617 | \row | - | ||||||
618 | \li 1 | - | ||||||
619 | \li data1 | - | ||||||
620 | - | |||||||
621 | \row | - | ||||||
622 | \li 2 | - | ||||||
623 | \li data2 | - | ||||||
624 | - | |||||||
625 | \row | - | ||||||
626 | \li 3 | - | ||||||
627 | \li data3 | - | ||||||
628 | - | |||||||
629 | \row | - | ||||||
630 | \li 4 | - | ||||||
631 | \li data4[0] .. data4[7] | - | ||||||
632 | - | |||||||
633 | \endtable | - | ||||||
634 | - | |||||||
635 | \since 4.8 | - | ||||||
636 | */ | - | ||||||
637 | QByteArray QUuid::toRfc4122() const | - | ||||||
638 | { | - | ||||||
639 | // we know how many bytes a UUID has, I hope :) | - | ||||||
640 | QByteArray bytes(16, Qt::Uninitialized); | - | ||||||
641 | uchar *data = reinterpret_cast<uchar*>(bytes.data()); | - | ||||||
642 | - | |||||||
643 | qToBigEndian(data1, data); | - | ||||||
644 | data += sizeof(quint32); | - | ||||||
645 | qToBigEndian(data2, data); | - | ||||||
646 | data += sizeof(quint16); | - | ||||||
647 | qToBigEndian(data3, data); | - | ||||||
648 | data += sizeof(quint16); | - | ||||||
649 | - | |||||||
650 | for (int i = 0; i < 8; ++i) { | - | ||||||
651 | *(data) = data4[i]; | - | ||||||
652 | data++; | - | ||||||
653 | } | - | ||||||
654 | - | |||||||
655 | return bytes; | - | ||||||
656 | } | - | ||||||
657 | - | |||||||
658 | #ifndef QT_NO_DATASTREAM | - | ||||||
659 | /*! | - | ||||||
660 | \relates QUuid | - | ||||||
661 | Writes the UUID \a id to the data stream \a s. | - | ||||||
662 | */ | - | ||||||
663 | QDataStream &operator<<(QDataStream &s, const QUuid &id) | - | ||||||
664 | { | - | ||||||
665 | QByteArray bytes; | - | ||||||
666 | if (s.byteOrder() == QDataStream::BigEndian) { | - | ||||||
667 | bytes = id.toRfc4122(); | - | ||||||
668 | } else { | - | ||||||
669 | // we know how many bytes a UUID has, I hope :) | - | ||||||
670 | bytes = QByteArray(16, Qt::Uninitialized); | - | ||||||
671 | uchar *data = reinterpret_cast<uchar*>(bytes.data()); | - | ||||||
672 | - | |||||||
673 | qToLittleEndian(id.data1, data); | - | ||||||
674 | data += sizeof(quint32); | - | ||||||
675 | qToLittleEndian(id.data2, data); | - | ||||||
676 | data += sizeof(quint16); | - | ||||||
677 | qToLittleEndian(id.data3, data); | - | ||||||
678 | data += sizeof(quint16); | - | ||||||
679 | - | |||||||
680 | for (int i = 0; i < 8; ++i) { | - | ||||||
681 | *(data) = id.data4[i]; | - | ||||||
682 | data++; | - | ||||||
683 | } | - | ||||||
684 | } | - | ||||||
685 | - | |||||||
686 | if (s.writeRawData(bytes.data(), 16) != 16) { | - | ||||||
687 | s.setStatus(QDataStream::WriteFailed); | - | ||||||
688 | } | - | ||||||
689 | return s; | - | ||||||
690 | } | - | ||||||
691 | - | |||||||
692 | /*! | - | ||||||
693 | \relates QUuid | - | ||||||
694 | Reads a UUID from the stream \a s into \a id. | - | ||||||
695 | */ | - | ||||||
696 | QDataStream &operator>>(QDataStream &s, QUuid &id) | - | ||||||
697 | { | - | ||||||
698 | QByteArray bytes(16, Qt::Uninitialized); | - | ||||||
699 | if (s.readRawData(bytes.data(), 16) != 16) { | - | ||||||
700 | s.setStatus(QDataStream::ReadPastEnd); | - | ||||||
701 | return s; | - | ||||||
702 | } | - | ||||||
703 | - | |||||||
704 | if (s.byteOrder() == QDataStream::BigEndian) { | - | ||||||
705 | id = QUuid::fromRfc4122(bytes); | - | ||||||
706 | } else { | - | ||||||
707 | const uchar *data = reinterpret_cast<const uchar *>(bytes.constData()); | - | ||||||
708 | - | |||||||
709 | id.data1 = qFromLittleEndian<quint32>(data); | - | ||||||
710 | data += sizeof(quint32); | - | ||||||
711 | id.data2 = qFromLittleEndian<quint16>(data); | - | ||||||
712 | data += sizeof(quint16); | - | ||||||
713 | id.data3 = qFromLittleEndian<quint16>(data); | - | ||||||
714 | data += sizeof(quint16); | - | ||||||
715 | - | |||||||
716 | for (int i = 0; i < 8; ++i) { | - | ||||||
717 | id.data4[i] = *(data); | - | ||||||
718 | data++; | - | ||||||
719 | } | - | ||||||
720 | } | - | ||||||
721 | - | |||||||
722 | return s; | - | ||||||
723 | } | - | ||||||
724 | #endif // QT_NO_DATASTREAM | - | ||||||
725 | - | |||||||
726 | /*! | - | ||||||
727 | Returns \c true if this is the null UUID | - | ||||||
728 | {00000000-0000-0000-0000-000000000000}; otherwise returns \c false. | - | ||||||
729 | */ | - | ||||||
730 | bool QUuid::isNull() const Q_DECL_NOTHROW | - | ||||||
731 | { | - | ||||||
732 | return data4[0] == 0 && data4[1] == 0 && data4[2] == 0 && data4[3] == 0 && executed 58 times by 2 tests: return data4[0] == 0 && data4[1] == 0 && data4[2] == 0 && data4[3] == 0 && data4[4] == 0 && data4[5] == 0 && data4[6] == 0 && data4[7] == 0 && data1 == 0 && data2 == 0 && data3 == 0; Executed by:
| 58 | ||||||
733 | data4[4] == 0 && data4[5] == 0 && data4[6] == 0 && data4[7] == 0 && executed 58 times by 2 tests: return data4[0] == 0 && data4[1] == 0 && data4[2] == 0 && data4[3] == 0 && data4[4] == 0 && data4[5] == 0 && data4[6] == 0 && data4[7] == 0 && data1 == 0 && data2 == 0 && data3 == 0; Executed by:
| 58 | ||||||
734 | data1 == 0 && data2 == 0 && data3 == 0; executed 58 times by 2 tests: return data4[0] == 0 && data4[1] == 0 && data4[2] == 0 && data4[3] == 0 && data4[4] == 0 && data4[5] == 0 && data4[6] == 0 && data4[7] == 0 && data1 == 0 && data2 == 0 && data3 == 0; Executed by:
| 58 | ||||||
735 | } | - | ||||||
736 | - | |||||||
737 | /*! | - | ||||||
738 | \enum QUuid::Variant | - | ||||||
739 | - | |||||||
740 | This enum defines the values used in the \l{Variant field} | - | ||||||
741 | {variant field} of the UUID. The value in the variant field | - | ||||||
742 | determines the layout of the 128-bit value. | - | ||||||
743 | - | |||||||
744 | \value VarUnknown Variant is unknown | - | ||||||
745 | \value NCS Reserved for NCS (Network Computing System) backward compatibility | - | ||||||
746 | \value DCE Distributed Computing Environment, the scheme used by QUuid | - | ||||||
747 | \value Microsoft Reserved for Microsoft backward compatibility (GUID) | - | ||||||
748 | \value Reserved Reserved for future definition | - | ||||||
749 | */ | - | ||||||
750 | - | |||||||
751 | /*! | - | ||||||
752 | \enum QUuid::Version | - | ||||||
753 | - | |||||||
754 | This enum defines the values used in the \l{Version field} | - | ||||||
755 | {version field} of the UUID. The version field is meaningful | - | ||||||
756 | only if the value in the \l{Variant field} {variant field} | - | ||||||
757 | is QUuid::DCE. | - | ||||||
758 | - | |||||||
759 | \value VerUnknown Version is unknown | - | ||||||
760 | \value Time Time-based, by using timestamp, clock sequence, and | - | ||||||
761 | MAC network card address (if available) for the node sections | - | ||||||
762 | \value EmbeddedPOSIX DCE Security version, with embedded POSIX UUIDs | - | ||||||
763 | \value Name Name-based, by using values from a name for all sections | - | ||||||
764 | \value Md5 Alias for Name | - | ||||||
765 | \value Random Random-based, by using random numbers for all sections | - | ||||||
766 | \value Sha1 | - | ||||||
767 | */ | - | ||||||
768 | - | |||||||
769 | /*! | - | ||||||
770 | \fn QUuid::Variant QUuid::variant() const | - | ||||||
771 | - | |||||||
772 | Returns the value in the \l{Variant field} {variant field} of the | - | ||||||
773 | UUID. If the return value is QUuid::DCE, call version() to see | - | ||||||
774 | which layout it uses. The null UUID is considered to be of an | - | ||||||
775 | unknown variant. | - | ||||||
776 | - | |||||||
777 | \sa version() | - | ||||||
778 | */ | - | ||||||
779 | QUuid::Variant QUuid::variant() const Q_DECL_NOTHROW | - | ||||||
780 | { | - | ||||||
781 | if (isNull())
| 12-35 | ||||||
782 | return VarUnknown; executed 12 times by 1 test: return VarUnknown; Executed by:
| 12 | ||||||
783 | // Check the 3 MSB of data4[0] | - | ||||||
784 | if ((data4[0] & 0x80) == 0x00) return NCS; executed 2 times by 1 test: return NCS; Executed by:
| 2-33 | ||||||
785 | else if ((data4[0] & 0xC0) == 0x80) return DCE; executed 33 times by 1 test: return DCE; Executed by:
| 0-33 | ||||||
786 | else if ((data4[0] & 0xE0) == 0xC0) return Microsoft; never executed: return Microsoft;
| 0 | ||||||
787 | else if ((data4[0] & 0xE0) == 0xE0) return Reserved; never executed: return Reserved;
| 0 | ||||||
788 | return VarUnknown; never executed: return VarUnknown; | 0 | ||||||
789 | } | - | ||||||
790 | - | |||||||
791 | /*! | - | ||||||
792 | \fn QUuid::Version QUuid::version() const | - | ||||||
793 | - | |||||||
794 | Returns the \l{Version field} {version field} of the UUID, if the | - | ||||||
795 | UUID's \l{Variant field} {variant field} is QUuid::DCE. Otherwise | - | ||||||
796 | it returns QUuid::VerUnknown. | - | ||||||
797 | - | |||||||
798 | \sa variant() | - | ||||||
799 | */ | - | ||||||
800 | QUuid::Version QUuid::version() const Q_DECL_NOTHROW | - | ||||||
801 | { | - | ||||||
802 | // Check the 4 MSB of data3 | - | ||||||
803 | Version ver = (Version)(data3>>12); | - | ||||||
804 | if (isNull()
| 0-4 | ||||||
805 | || (variant() != DCE)
| 1-3 | ||||||
806 | || ver < Time
| 0-3 | ||||||
807 | || ver > Sha1)
| 0-3 | ||||||
808 | return VerUnknown; executed 1 time by 1 test: return VerUnknown; Executed by:
| 1 | ||||||
809 | return ver; executed 3 times by 1 test: return ver; Executed by:
| 3 | ||||||
810 | } | - | ||||||
811 | - | |||||||
812 | /*! \fn QUuid QUuid::fromCFUUID(CFUUIDRef uuid) | - | ||||||
813 | \since 5.7 | - | ||||||
814 | - | |||||||
815 | Constructs a new QUuid containing a copy of the \a uuid CFUUID. | - | ||||||
816 | - | |||||||
817 | \note this function is only available on Apple platforms. | - | ||||||
818 | */ | - | ||||||
819 | - | |||||||
820 | /*! \fn CFUUIDRef QUuid::toCFUUID() const | - | ||||||
821 | \since 5.7 | - | ||||||
822 | - | |||||||
823 | Creates a CFUUID from a QUuid. The caller owns the CFUUID and is | - | ||||||
824 | responsible for releasing it. | - | ||||||
825 | - | |||||||
826 | \note this function is only available on Apple platforms. | - | ||||||
827 | */ | - | ||||||
828 | - | |||||||
829 | /*! \fn QUuid QUuid::fromNSUUID(const NSUUID *uuid) | - | ||||||
830 | \since 5.7 | - | ||||||
831 | - | |||||||
832 | Constructs a new QUuid containing a copy of the \a uuid NSUUID. | - | ||||||
833 | - | |||||||
834 | \note this function is only available on Apple platforms. | - | ||||||
835 | */ | - | ||||||
836 | - | |||||||
837 | /*! \fn NSUUID QUuid::toNSUUID() const | - | ||||||
838 | \since 5.7 | - | ||||||
839 | - | |||||||
840 | Creates a NSUUID from a QUuid. The NSUUID is autoreleased. | - | ||||||
841 | - | |||||||
842 | \note this function is only available on Apple platforms. | - | ||||||
843 | */ | - | ||||||
844 | - | |||||||
845 | /*! | - | ||||||
846 | \fn bool QUuid::operator<(const QUuid &other) const | - | ||||||
847 | - | |||||||
848 | Returns \c true if this QUuid has the same \l{Variant field} | - | ||||||
849 | {variant field} as the \a other QUuid and is lexicographically | - | ||||||
850 | \e{before} the \a other QUuid. If the \a other QUuid has a | - | ||||||
851 | different variant field, the return value is determined by | - | ||||||
852 | comparing the two \l{QUuid::Variant} {variants}. | - | ||||||
853 | - | |||||||
854 | \sa variant() | - | ||||||
855 | */ | - | ||||||
856 | bool QUuid::operator<(const QUuid &other) const Q_DECL_NOTHROW | - | ||||||
857 | { | - | ||||||
858 | if (variant() != other.variant())
| 4-12 | ||||||
859 | return variant() < other.variant(); executed 4 times by 1 test: return variant() < other.variant(); Executed by:
| 4 | ||||||
860 | - | |||||||
861 | #define ISLESS(f1, f2) if (f1!=f2) return (f1<f2); | - | ||||||
862 | ISLESS(data1, other.data1); executed 8 times by 1 test: return (data1<other.data1); Executed by:
| 4-8 | ||||||
863 | ISLESS(data2, other.data2); never executed: return (data2<other.data2);
| 0-4 | ||||||
864 | ISLESS(data3, other.data3); never executed: return (data3<other.data3);
| 0-4 | ||||||
865 | for (int n = 0; n < 8; n++) {
| 4-32 | ||||||
866 | ISLESS(data4[n], other.data4[n]); never executed: return (data4[n]<other.data4[n]);
| 0-32 | ||||||
867 | } executed 32 times by 1 test: end of block Executed by:
| 32 | ||||||
868 | #undef ISLESS | - | ||||||
869 | return false; executed 4 times by 1 test: return false; Executed by:
| 4 | ||||||
870 | } | - | ||||||
871 | - | |||||||
872 | /*! | - | ||||||
873 | \fn bool QUuid::operator>(const QUuid &other) const | - | ||||||
874 | - | |||||||
875 | Returns \c true if this QUuid has the same \l{Variant field} | - | ||||||
876 | {variant field} as the \a other QUuid and is lexicographically | - | ||||||
877 | \e{after} the \a other QUuid. If the \a other QUuid has a | - | ||||||
878 | different variant field, the return value is determined by | - | ||||||
879 | comparing the two \l{QUuid::Variant} {variants}. | - | ||||||
880 | - | |||||||
881 | \sa variant() | - | ||||||
882 | */ | - | ||||||
883 | bool QUuid::operator>(const QUuid &other) const Q_DECL_NOTHROW | - | ||||||
884 | { | - | ||||||
885 | return other < *this; executed 3 times by 1 test: return other < *this; Executed by:
| 3 | ||||||
886 | } | - | ||||||
887 | - | |||||||
888 | /*! | - | ||||||
889 | \fn bool operator<=(const QUuid &lhs, const QUuid &rhs) | - | ||||||
890 | \relates QUuid | - | ||||||
891 | \since 5.5 | - | ||||||
892 | - | |||||||
893 | Returns \c true if \a lhs has the same \l{Variant field} | - | ||||||
894 | {variant field} as \a rhs and is lexicographically | - | ||||||
895 | \e{not after} \a rhs. If \a rhs has a | - | ||||||
896 | different variant field, the return value is determined by | - | ||||||
897 | comparing the two \l{QUuid::Variant} {variants}. | - | ||||||
898 | - | |||||||
899 | \sa {QUuid::}{variant()} | - | ||||||
900 | */ | - | ||||||
901 | - | |||||||
902 | /*! | - | ||||||
903 | \fn bool operator>=(const QUuid &lhs, const QUuid &rhs) | - | ||||||
904 | \relates QUuid | - | ||||||
905 | \since 5.5 | - | ||||||
906 | - | |||||||
907 | Returns \c true if \a lhs has the same \l{Variant field} | - | ||||||
908 | {variant field} as \a rhs and is lexicographically | - | ||||||
909 | \e{not before} \a rhs. If \a rhs has a | - | ||||||
910 | different variant field, the return value is determined by | - | ||||||
911 | comparing the two \l{QUuid::Variant} {variants}. | - | ||||||
912 | - | |||||||
913 | \sa {QUuid::}{variant()} | - | ||||||
914 | */ | - | ||||||
915 | - | |||||||
916 | /*! | - | ||||||
917 | \fn QUuid QUuid::createUuid() | - | ||||||
918 | - | |||||||
919 | On any platform other than Windows, this function returns a new | - | ||||||
920 | UUID with variant QUuid::DCE and version QUuid::Random. If | - | ||||||
921 | the /dev/urandom device exists, then the numbers used to construct | - | ||||||
922 | the UUID will be of cryptographic quality, which will make the UUID | - | ||||||
923 | unique. Otherwise, the numbers of the UUID will be obtained from | - | ||||||
924 | the local pseudo-random number generator (qrand(), which is seeded | - | ||||||
925 | by qsrand()) which is usually not of cryptograhic quality, which | - | ||||||
926 | means that the UUID can't be guaranteed to be unique. | - | ||||||
927 | - | |||||||
928 | On a Windows platform, a GUID is generated, which almost certainly | - | ||||||
929 | \e{will} be unique, on this or any other system, networked or not. | - | ||||||
930 | - | |||||||
931 | \sa variant(), version() | - | ||||||
932 | */ | - | ||||||
933 | #if defined(Q_OS_WIN) | - | ||||||
934 | - | |||||||
935 | QT_BEGIN_INCLUDE_NAMESPACE | - | ||||||
936 | #include <objbase.h> // For CoCreateGuid | - | ||||||
937 | QT_END_INCLUDE_NAMESPACE | - | ||||||
938 | - | |||||||
939 | QUuid QUuid::createUuid() | - | ||||||
940 | { | - | ||||||
941 | GUID guid; | - | ||||||
942 | CoCreateGuid(&guid); | - | ||||||
943 | QUuid result = guid; | - | ||||||
944 | return result; | - | ||||||
945 | } | - | ||||||
946 | - | |||||||
947 | #else // Q_OS_WIN | - | ||||||
948 | - | |||||||
949 | QT_BEGIN_INCLUDE_NAMESPACE | - | ||||||
950 | #include "qdatetime.h" | - | ||||||
951 | #include "qfile.h" | - | ||||||
952 | #include "qthreadstorage.h" | - | ||||||
953 | #include <stdlib.h> // for RAND_MAX | - | ||||||
954 | QT_END_INCLUDE_NAMESPACE | - | ||||||
955 | - | |||||||
956 | #if !defined(QT_BOOTSTRAPPED) && defined(Q_OS_UNIX) | - | ||||||
957 | Q_GLOBAL_STATIC(QThreadStorage<QFile *>, devUrandomStorage); | - | ||||||
958 | #endif | - | ||||||
959 | - | |||||||
960 | QUuid QUuid::createUuid() | - | ||||||
961 | { | - | ||||||
962 | QUuid result; | - | ||||||
963 | uint *data = &(result.data1); | - | ||||||
964 | - | |||||||
965 | #if defined(Q_OS_UNIX) | - | ||||||
966 | QFile *devUrandom; | - | ||||||
967 | # if !defined(QT_BOOTSTRAPPED) | - | ||||||
968 | devUrandom = devUrandomStorage()->localData(); | - | ||||||
969 | if (!devUrandom) { | - | ||||||
970 | devUrandom = new QFile(QLatin1String("/dev/urandom")); | - | ||||||
971 | devUrandom->open(QIODevice::ReadOnly | QIODevice::Unbuffered); | - | ||||||
972 | devUrandomStorage()->setLocalData(devUrandom); | - | ||||||
973 | } | - | ||||||
974 | # else | - | ||||||
975 | QFile file(QLatin1String("/dev/urandom")); | - | ||||||
976 | devUrandom = &file; | - | ||||||
977 | devUrandom->open(QIODevice::ReadOnly | QIODevice::Unbuffered); | - | ||||||
978 | # endif | - | ||||||
979 | enum { AmountToRead = 4 * sizeof(uint) }; | - | ||||||
980 | if (devUrandom->isOpen() | - | ||||||
981 | && devUrandom->read((char *) data, AmountToRead) == AmountToRead) { | - | ||||||
982 | // we got what we wanted, nothing more to do | - | ||||||
983 | ; | - | ||||||
984 | } else | - | ||||||
985 | #endif | - | ||||||
986 | { | - | ||||||
987 | static const int intbits = sizeof(int)*8; | - | ||||||
988 | static int randbits = 0; | - | ||||||
989 | if (!randbits) { | - | ||||||
990 | int r = 0; | - | ||||||
991 | int max = RAND_MAX; | - | ||||||
992 | do { ++r; } while ((max=max>>1)); | - | ||||||
993 | randbits = r; | - | ||||||
994 | } | - | ||||||
995 | - | |||||||
996 | // Seed the PRNG once per thread with a combination of current time, a | - | ||||||
997 | // stack address and a serial counter (since thread stack addresses are | - | ||||||
998 | // re-used). | - | ||||||
999 | #ifndef QT_BOOTSTRAPPED | - | ||||||
1000 | static QThreadStorage<int *> uuidseed; | - | ||||||
1001 | if (!uuidseed.hasLocalData()) | - | ||||||
1002 | { | - | ||||||
1003 | int *pseed = new int; | - | ||||||
1004 | static QBasicAtomicInt serial = Q_BASIC_ATOMIC_INITIALIZER(2); | - | ||||||
1005 | qsrand(*pseed = QDateTime::currentDateTimeUtc().toTime_t() | - | ||||||
1006 | + quintptr(&pseed) | - | ||||||
1007 | + serial.fetchAndAddRelaxed(1)); | - | ||||||
1008 | uuidseed.setLocalData(pseed); | - | ||||||
1009 | } | - | ||||||
1010 | #else | - | ||||||
1011 | static bool seeded = false; | - | ||||||
1012 | if (!seeded) | - | ||||||
1013 | qsrand(QDateTime::currentDateTimeUtc().toTime_t() | - | ||||||
1014 | + quintptr(&seeded)); | - | ||||||
1015 | #endif | - | ||||||
1016 | - | |||||||
1017 | int chunks = 16 / sizeof(uint); | - | ||||||
1018 | while (chunks--) { | - | ||||||
1019 | uint randNumber = 0; | - | ||||||
1020 | for (int filled = 0; filled < intbits; filled += randbits) | - | ||||||
1021 | randNumber |= qrand()<<filled; | - | ||||||
1022 | *(data+chunks) = randNumber; | - | ||||||
1023 | } | - | ||||||
1024 | } | - | ||||||
1025 | - | |||||||
1026 | result.data4[0] = (result.data4[0] & 0x3F) | 0x80; // UV_DCE | - | ||||||
1027 | result.data3 = (result.data3 & 0x0FFF) | 0x4000; // UV_Random | - | ||||||
1028 | - | |||||||
1029 | return result; | - | ||||||
1030 | } | - | ||||||
1031 | #endif // !Q_OS_WIN | - | ||||||
1032 | - | |||||||
1033 | /*! | - | ||||||
1034 | \fn bool QUuid::operator==(const GUID &guid) const | - | ||||||
1035 | - | |||||||
1036 | Returns \c true if this UUID is equal to the Windows GUID \a guid; | - | ||||||
1037 | otherwise returns \c false. | - | ||||||
1038 | */ | - | ||||||
1039 | - | |||||||
1040 | /*! | - | ||||||
1041 | \fn bool QUuid::operator!=(const GUID &guid) const | - | ||||||
1042 | - | |||||||
1043 | Returns \c true if this UUID is not equal to the Windows GUID \a | - | ||||||
1044 | guid; otherwise returns \c false. | - | ||||||
1045 | */ | - | ||||||
1046 | - | |||||||
1047 | #ifndef QT_NO_DEBUG_STREAM | - | ||||||
1048 | /*! | - | ||||||
1049 | \relates QUuid | - | ||||||
1050 | Writes the UUID \a id to the output stream for debugging information \a dbg. | - | ||||||
1051 | */ | - | ||||||
1052 | QDebug operator<<(QDebug dbg, const QUuid &id) | - | ||||||
1053 | { | - | ||||||
1054 | QDebugStateSaver saver(dbg); | - | ||||||
1055 | dbg.nospace() << "QUuid(" << id.toString() << ')'; | - | ||||||
1056 | return dbg; | - | ||||||
1057 | } | - | ||||||
1058 | #endif | - | ||||||
1059 | - | |||||||
1060 | /*! | - | ||||||
1061 | \since 5.0 | - | ||||||
1062 | \relates QUuid | - | ||||||
1063 | Returns a hash of the UUID \a uuid, using \a seed to seed the calculation. | - | ||||||
1064 | */ | - | ||||||
1065 | uint qHash(const QUuid &uuid, uint seed) Q_DECL_NOTHROW | - | ||||||
1066 | { | - | ||||||
1067 | return uuid.data1 ^ uuid.data2 ^ (uuid.data3 << 16) executed 3 times by 1 test: return uuid.data1 ^ uuid.data2 ^ (uuid.data3 << 16) ^ ((uuid.data4[0] << 24) | (uuid.data4[1] << 16) | (uuid.data4[2] << 8) | uuid.data4[3]) ^ ((uuid.data4[4] << 24) | (uuid.data4[5] << 16) | (uuid.data4[6] << 8) | uuid.data4[7]) ^ seed; Executed by:
| 3 | ||||||
1068 | ^ ((uuid.data4[0] << 24) | (uuid.data4[1] << 16) | (uuid.data4[2] << 8) | uuid.data4[3]) executed 3 times by 1 test: return uuid.data1 ^ uuid.data2 ^ (uuid.data3 << 16) ^ ((uuid.data4[0] << 24) | (uuid.data4[1] << 16) | (uuid.data4[2] << 8) | uuid.data4[3]) ^ ((uuid.data4[4] << 24) | (uuid.data4[5] << 16) | (uuid.data4[6] << 8) | uuid.data4[7]) ^ seed; Executed by:
| 3 | ||||||
1069 | ^ ((uuid.data4[4] << 24) | (uuid.data4[5] << 16) | (uuid.data4[6] << 8) | uuid.data4[7]) executed 3 times by 1 test: return uuid.data1 ^ uuid.data2 ^ (uuid.data3 << 16) ^ ((uuid.data4[0] << 24) | (uuid.data4[1] << 16) | (uuid.data4[2] << 8) | uuid.data4[3]) ^ ((uuid.data4[4] << 24) | (uuid.data4[5] << 16) | (uuid.data4[6] << 8) | uuid.data4[7]) ^ seed; Executed by:
| 3 | ||||||
1070 | ^ seed; executed 3 times by 1 test: return uuid.data1 ^ uuid.data2 ^ (uuid.data3 << 16) ^ ((uuid.data4[0] << 24) | (uuid.data4[1] << 16) | (uuid.data4[2] << 8) | uuid.data4[3]) ^ ((uuid.data4[4] << 24) | (uuid.data4[5] << 16) | (uuid.data4[6] << 8) | uuid.data4[7]) ^ seed; Executed by:
| 3 | ||||||
1071 | } | - | ||||||
1072 | - | |||||||
1073 | - | |||||||
1074 | QT_END_NAMESPACE | - | ||||||
Source code | Switch to Preprocessed file |