1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the core module of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
31
#include "qdatastream.h"
35
\brief The QUuid class stores a Universally Unique Identifier (UUID).
40
For objects or declarations that must be uniquely identified,
41
UUIDs (also known as GUIDs) are widely used in order to assign a
42
fixed and easy to compare value to the object or declaration. The
43
128-bit value of a UUID is generated by an algorithm that
44
guarantees that the value is unique.
46
In Qt, UUIDs are wrapped by the QUuid struct which provides
47
convenience functions for handling UUIDs. Most platforms provide
48
a tool to generate new UUIDs, for example, \c uuidgen and \c
51
UUIDs generated by QUuid, are based on the \c Random version of the
52
\c DCE (Distributed Computing Environment) standard.
54
UUIDs can be constructed from numeric values or from strings, or
55
using the static createUuid() function. They can be converted to a
56
string with toString(). UUIDs have a variant() and a version(),
57
and null UUIDs return true from isNull().
61
\fn QUuid::QUuid(const GUID &guid)
63
Casts a Windows \a guid to a Qt QUuid.
65
\warning This function is only for Windows platforms.
69
\fn QUuid &QUuid::operator=(const GUID &guid)
71
Assigns a Windows \a guid to a Qt QUuid.
73
\warning This function is only for Windows platforms.
77
\fn QUuid::operator GUID() const
79
Returns a Windows GUID from a QUuid.
81
\warning This function is only for Windows platforms.
87
Creates the null UUID {00000000-0000-0000-0000-000000000000}.
91
\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)
93
Creates a UUID with the value specified by the parameters, \a l,
94
\a w1, \a w2, \a b1, \a b2, \a b3, \a b4, \a b5, \a b6, \a b7, \a
99
// {67C8770B-44F1-410A-AB9A-F9B5446F13EE}
100
QUuid IID_MyInterface(0x67c8770b, 0x44f1, 0x410a, 0xab, 0x9a, 0xf9, 0xb5, 0x44, 0x6f, 0x13, 0xee)
104
#ifndef QT_NO_QUUID_STRING
106
Creates a QUuid object from the string \a text. The function can
107
only convert a string in the format
108
{HHHHHHHH-HHHH-HHHH-HHHH-HHHHHHHHHHHH} (where 'H' stands for a hex
109
digit). If the conversion fails a null UUID is created.
111
QUuid::QUuid(const QString &text)
114
if (text.isEmpty()) {
118
QString temp = text.toUpper();
119
if (temp[0] != QLatin1Char('{'))
120
temp = QLatin1Char('{') + text;
121
if (text[(int)text.length()-1] != QLatin1Char('}'))
122
temp += QLatin1Char('}');
124
data1 = temp.mid(1, 8).toULongLong(&ok, 16);
130
data2 = temp.mid(10, 4).toUInt(&ok, 16);
135
data3 = temp.mid(15, 4).toUInt(&ok, 16);
140
data4[0] = temp.mid(20, 2).toUInt(&ok, 16);
145
data4[1] = temp.mid(22, 2).toUInt(&ok, 16);
150
for (int i = 2; i<8; i++) {
151
data4[i] = temp.mid(25 + (i-2)*2, 2).toUShort(&ok, 16);
162
QUuid::QUuid(const char *text)
164
*this = QUuid(QString::fromLatin1(text));
169
\fn bool QUuid::operator==(const QUuid &other) const
171
Returns true if this QUuid and the \a other QUuid are identical;
172
otherwise returns false.
176
\fn bool QUuid::operator!=(const QUuid &other) const
178
Returns true if this QUuid and the \a other QUuid are different;
179
otherwise returns false.
181
#ifndef QT_NO_QUUID_STRING
183
\fn QUuid::operator QString() const
185
Returns the string representation of the uuid.
190
static QString uuidhex(uint data, int digits)
192
return QString::number(data, 16).rightJustified(digits, QLatin1Char('0'));
196
Returns the string representation of the uuid.
198
QString QUuid::toString() const
202
QChar dash = QLatin1Char('-');
203
result = QLatin1Char('{') + uuidhex(data1,8);
205
result += uuidhex(data2,4);
207
result += uuidhex(data3,4);
209
result += uuidhex(data4[0],2);
210
result += uuidhex(data4[1],2);
212
for (int i = 2; i < 8; i++)
213
result += uuidhex(data4[i],2);
215
return result + QLatin1Char('}');
219
#ifndef QT_NO_DATASTREAM
222
Writes the uuid \a id to the datastream \a s.
224
QDataStream &operator<<(QDataStream &s, const QUuid &id)
226
s << (quint32)id.data1;
227
s << (quint16)id.data2;
228
s << (quint16)id.data3;
229
for (int i = 0; i < 8; i++)
230
s << (quint8)id.data4[i];
236
Reads uuid from from the stream \a s into \a id.
238
QDataStream &operator>>(QDataStream &s, QUuid &id)
249
for (int i = 0; i < 8; i++) {
258
Returns true if this is the null UUID
259
{00000000-0000-0000-0000-000000000000}; otherwise returns false.
261
bool QUuid::isNull() const
263
return data4[0] == 0 && data4[1] == 0 && data4[2] == 0 && data4[3] == 0 &&
264
data4[4] == 0 && data4[5] == 0 && data4[6] == 0 && data4[7] == 0 &&
265
data1 == 0 && data2 == 0 && data3 == 0;
271
This enum defines the variant of the UUID, which is the scheme
272
which defines the layout of the 128-bits value.
274
\value VarUnknown Variant is unknown
275
\value NCS Reserved for NCS (Network Computing System) backward compatibility
276
\value DCE Distributed Computing Environment, the scheme used by QUuid
277
\value Microsoft Reserved for Microsoft backward compatibility (GUID)
278
\value Reserved Reserved for future definition
284
This enum defines the version of the UUID.
286
\value VerUnknown Version is unknown
287
\value Time Time-based, by using timestamp, clock sequence, and
288
MAC network card address (if available) for the node sections
289
\value EmbeddedPOSIX DCE Security version, with embedded POSIX UUIDs
290
\value Name Name-based, by using values from a name for all sections
291
\value Random Random-based, by using random numbers for all sections
295
\fn QUuid::Variant QUuid::variant() const
297
Returns the variant of the UUID.
298
The null UUID is considered to be of an unknown variant.
302
QUuid::Variant QUuid::variant() const
306
// Check the 3 MSB of data4[0]
307
if ((data4[0] & 0x80) == 0x00) return NCS;
308
else if ((data4[0] & 0xC0) == 0x80) return DCE;
309
else if ((data4[0] & 0xE0) == 0xC0) return Microsoft;
310
else if ((data4[0] & 0xE0) == 0xE0) return Reserved;
315
\fn QUuid::Version QUuid::version() const
317
Returns the version of the UUID, if the UUID is of the DCE
318
variant; otherwise returns VerUnknown.
322
QUuid::Version QUuid::version() const
324
// Check the 4 MSB of data3
325
Version ver = (Version)(data3>>12);
327
|| (variant() != DCE)
335
\fn bool QUuid::operator<(const QUuid &other) const
337
Returns true if this QUuid is of the same variant,
338
and lexicographically before the \a other QUuid;
339
otherwise returns false.
343
#define ISLESS(f1, f2) if (f1!=f2) return (f1<f2);
344
bool QUuid::operator<(const QUuid &other) const
346
if (variant() != other.variant())
349
ISLESS(data1, other.data1);
350
ISLESS(data2, other.data2);
351
ISLESS(data3, other.data3);
352
for (int n = 0; n < 8; n++) {
353
ISLESS(data4[n], other.data4[n]);
359
\fn bool QUuid::operator>(const QUuid &other) const
361
Returns true if this QUuid is of the same variant,
362
and lexicographically after the \a other QUuid;
363
otherwise returns false.
367
#define ISMORE(f1, f2) if (f1!=f2) return (f1>f2);
368
bool QUuid::operator>(const QUuid &other) const
370
if (variant() != other.variant())
373
ISMORE(data1, other.data1);
374
ISMORE(data2, other.data2);
375
ISMORE(data3, other.data3);
376
for (int n = 0; n < 8; n++) {
377
ISMORE(data4[n], other.data4[n]);
383
\fn QUuid QUuid::createUuid()
385
Returns a new UUID of \c DCE variant, and \c Random type. The
386
UUIDs generated are based on the platform specific pseudo-random
387
generator, which is usually not a cryptographic-quality random
388
number generator. Therefore, a UUID is not guaranteed to be unique
389
cross application instances.
391
On Windows, the new UUID is extremely likely to be unique on the
392
same or any other system, networked or not.
394
\sa variant(), version()
396
#if defined(Q_OS_WIN32)
397
#include <objbase.h> // For CoCreateGuid
398
QUuid QUuid::createUuid()
406
#include "qdatetime.h"
407
#include "stdlib.h" // For srand/rand
408
QUuid QUuid::createUuid()
410
static const int intbits = sizeof(int)*8;
411
static int randbits = 0;
414
do { ++randbits; } while ((max=max>>1));
415
srand((uint)QDateTime::currentDateTime().toTime_t());
416
rand(); // Skip first
420
uint *data = &(result.data1);
421
int chunks = 16 / sizeof(uint);
424
for (int filled = 0; filled < intbits; filled += randbits)
425
randNumber |= rand()<<filled;
426
*(data+chunks) = randNumber;
429
result.data4[0] = (result.data4[0] & 0x3F) | 0x80; // UV_DCE
430
result.data3 = (result.data3 & 0x0FFF) | 0x4000; // UV_Random
434
#endif // !Q_OS_WIN32
437
\fn bool QUuid::operator==(const GUID &guid) const
439
Returns true if this UUID is equal to the Windows GUID \a guid;
440
otherwise returns false.
444
\fn bool QUuid::operator!=(const GUID &guid) const
446
Returns true if this UUID is not equal to the Windows GUID \a
447
guid; otherwise returns false.