Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-04 09:23:09

0001 // Copyright (C) 2020 The Qt Company Ltd.
0002 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
0003 // Qt-Security score:significant reason:default
0004 
0005 #ifndef QUUID_H
0006 #define QUUID_H
0007 
0008 #include <QtCore/qcompare.h>
0009 #include <QtCore/qendian.h>
0010 #include <QtCore/qstring.h>
0011 #include <QtCore/qsystemdetection.h>
0012 
0013 #if defined(Q_OS_WIN) || defined(Q_QDOC)
0014 #ifndef GUID_DEFINED
0015 #define GUID_DEFINED
0016 typedef struct _GUID
0017 {
0018     ulong   Data1;
0019     ushort  Data2;
0020     ushort  Data3;
0021     uchar   Data4[8];
0022 } GUID, *REFGUID, *LPGUID;
0023 #endif
0024 #endif
0025 
0026 #if defined(Q_OS_DARWIN) || defined(Q_QDOC)
0027 Q_FORWARD_DECLARE_CF_TYPE(CFUUID);
0028 Q_FORWARD_DECLARE_OBJC_CLASS(NSUUID);
0029 #endif
0030 
0031 QT_BEGIN_NAMESPACE
0032 
0033 class Q_CORE_EXPORT QUuid
0034 {
0035     QUuid(Qt::Initialization) {}
0036 public:
0037     enum Variant {
0038         VarUnknown        =-1,
0039         NCS                = 0, // 0 - -
0040         DCE                = 2, // 1 0 -
0041         Microsoft        = 6, // 1 1 0
0042         Reserved        = 7  // 1 1 1
0043     };
0044 
0045     enum Version {
0046         VerUnknown        =-1,
0047         Time                = 1, // 0 0 0 1
0048         EmbeddedPOSIX        = 2, // 0 0 1 0
0049         Md5                 = 3, // 0 0 1 1
0050         Name = Md5,
0051         Random                = 4,  // 0 1 0 0
0052         Sha1            = 5, // 0 1 0 1
0053         UnixEpoch       = 7, // 0 1 1 1
0054     };
0055 
0056     enum StringFormat {
0057         WithBraces      = 0,
0058         WithoutBraces   = 1,
0059         Id128           = 3
0060     };
0061 
0062     union alignas(16) Id128Bytes {
0063         quint8 data[16];
0064         quint16 data16[8];
0065         quint32 data32[4];
0066         quint64 data64[2];
0067 #if defined(QT_COMPILER_SUPPORTS_INT128)
0068 QT_WARNING_PUSH
0069 QT_WARNING_DISABLE_GCC("-Wpedantic")    // ISO C++ does not support ‘__int128’ for ‘data128’
0070         unsigned __int128 data128[1];
0071 QT_WARNING_POP
0072 #elif defined(QT_SUPPORTS_INT128)
0073 #  error "struct QUuid::Id128Bytes should not depend on QT_SUPPORTS_INT128 for ABI reasons."
0074 #  error "Adjust the declaration of the `data128` member above so it is always defined if it's " \
0075         "supported by the current compiler/architecture in any configuration."
0076 #endif
0077 
0078         constexpr explicit operator QByteArrayView() const noexcept
0079         {
0080             return QByteArrayView(data, sizeof(data));
0081         }
0082 
0083         friend constexpr Id128Bytes qbswap(Id128Bytes b) noexcept
0084         {
0085             // 128-bit byte swap
0086             auto b0 = qbswap(b.data64[0]);
0087             auto b1 = qbswap(b.data64[1]);
0088             b.data64[0] = b1;
0089             b.data64[1] = b0;
0090             return b;
0091         }
0092     };
0093 
0094     constexpr QUuid() noexcept : data1(0), data2(0), data3(0), data4{0,0,0,0,0,0,0,0} {}
0095 
0096     constexpr QUuid(uint l, ushort w1, ushort w2, uchar b1, uchar b2, uchar b3,
0097                            uchar b4, uchar b5, uchar b6, uchar b7, uchar b8) noexcept
0098         : data1(l), data2(w1), data3(w2), data4{b1, b2, b3, b4, b5, b6, b7, b8} {}
0099     explicit inline QUuid(Id128Bytes id128, QSysInfo::Endian order = QSysInfo::BigEndian) noexcept;
0100 
0101     explicit QUuid(QAnyStringView string) noexcept
0102         : QUuid{fromString(string)} {}
0103     static QUuid fromString(QAnyStringView string) noexcept;
0104 #if QT_CORE_REMOVED_SINCE(6, 3)
0105     explicit QUuid(const QString &);
0106     static QUuid fromString(QStringView string) noexcept;
0107     static QUuid fromString(QLatin1StringView string) noexcept;
0108     explicit QUuid(const char *);
0109     explicit QUuid(const QByteArray &);
0110 #endif
0111     QString toString(StringFormat mode = WithBraces) const;
0112     QByteArray toByteArray(StringFormat mode = WithBraces) const;
0113     inline Id128Bytes toBytes(QSysInfo::Endian order = QSysInfo::BigEndian) const noexcept;
0114     QByteArray toRfc4122() const;
0115 
0116     static inline QUuid fromBytes(const void *bytes, QSysInfo::Endian order = QSysInfo::BigEndian);
0117 #if QT_CORE_REMOVED_SINCE(6, 3)
0118     static QUuid fromRfc4122(const QByteArray &);
0119 #endif
0120     static QUuid fromRfc4122(QByteArrayView) noexcept;
0121 
0122 #if QT_CORE_REMOVED_SINCE(6, 9)
0123     bool isNull() const noexcept;
0124 #endif
0125     constexpr bool isNull(QT6_DECL_NEW_OVERLOAD) const noexcept
0126     {
0127 #if defined(__cpp_lib_bit_cast) && defined(QT_SUPPORTS_INT128)
0128         return std::bit_cast<quint128>(*this) == 0;
0129 #else
0130         // QNX fails to compile
0131         // data4[0] == 0 && data4[1] == 0 && ...
0132         // in constexpr context, so rewrite it using a loop. This way we have
0133         // only single data4[i] != 0 check at each iteration
0134         for (size_t i = 0; i < 8; ++i) {
0135             if (data4[i] != 0)
0136                 return false;
0137         }
0138         return data1 == 0 && data2 == 0 && data3 == 0;
0139 #endif
0140     }
0141 
0142 #ifdef QT_SUPPORTS_INT128
0143     static constexpr QUuid fromUInt128(quint128 uuid, QSysInfo::Endian order = QSysInfo::BigEndian) noexcept;
0144     constexpr quint128 toUInt128(QSysInfo::Endian order = QSysInfo::BigEndian) const noexcept;
0145 #endif
0146 
0147 private:
0148     friend constexpr bool comparesEqual(const QUuid &lhs, const QUuid &rhs) noexcept
0149     {
0150         return is_eq(compareThreeWay_helper(lhs, rhs));
0151     }
0152     static constexpr Qt::strong_ordering
0153     compareThreeWay_helper(const QUuid &lhs, const QUuid &rhs) noexcept
0154     {
0155 #if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) && !defined(QT_BOOTSTRAPPED)
0156         if (const auto c = Qt::compareThreeWay(lhs.data1, rhs.data1); !is_eq(c))
0157             return c;
0158         if (const auto c = Qt::compareThreeWay(lhs.data2, rhs.data2); !is_eq(c))
0159             return c;
0160         if (const auto c = Qt::compareThreeWay(lhs.data3, rhs.data3); !is_eq(c))
0161             return c;
0162 #elif defined(__cpp_lib_bit_cast) && defined(QT_SUPPORTS_INT128)
0163         quint128 lu = qFromBigEndian(std::bit_cast<quint128>(lhs));
0164         quint128 ru = qFromBigEndian(std::bit_cast<quint128>(rhs));
0165         return Qt::compareThreeWay(lu, ru);
0166 #else
0167         auto make_int = [](const QUuid &u) {
0168             quint64 result = quint64(u.data3) << 48;
0169             result |= quint64(u.data2) << 32;
0170             return qFromBigEndian(result | u.data1);
0171         };
0172         if (const auto c = Qt::compareThreeWay(make_int(lhs), make_int(rhs)); !is_eq(c))
0173             return c;
0174 #endif
0175         for (unsigned i = 0; i < sizeof(lhs.data4); ++i) {
0176             if (const auto c = Qt::compareThreeWay(lhs.data4[i], rhs.data4[i]); !is_eq(c))
0177                 return c;
0178         }
0179         return Qt::strong_ordering::equal;
0180     }
0181     friend constexpr Qt::strong_ordering compareThreeWay(const QUuid &lhs, const QUuid &rhs) noexcept
0182     {
0183 #if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) && !defined(QT_BOOTSTRAPPED)
0184         // Keep the old sorting order from before Qt 6.8, which sorted first on
0185         // variant(). We don't need the exact algorithm to achieve same results.
0186         auto fastVariant = [](const QUuid &uuid) {
0187             quint8 v = uuid.data4[0];
0188             // i.e.: return v >= Microsoft ? v : v >= DCE ? DCE : NCS;
0189             return v >= 0xC0 ? v & 0xE0 : v >= 0x80 ? 0x80 : 0;
0190         };
0191         if (const auto c = Qt::compareThreeWay(fastVariant(lhs), fastVariant(rhs)); !is_eq(c))
0192             return c;
0193 #endif
0194         return compareThreeWay_helper(lhs, rhs);
0195     }
0196 
0197 public:
0198 /*  To prevent a meta-type creation ambiguity on Windows, we put comparison
0199     macros under NOT QT_CORE_REMOVED_SINCE(6, 8) part. */
0200 #if QT_CORE_REMOVED_SINCE(6, 8)
0201     constexpr bool operator==(const QUuid &orig) const noexcept
0202     {
0203         return comparesEqual(*this, orig);
0204     }
0205 
0206     constexpr bool operator!=(const QUuid &orig) const noexcept
0207     {
0208         return !operator==(orig);
0209     }
0210 
0211     bool operator<(const QUuid &other) const noexcept;
0212     bool operator>(const QUuid &other) const noexcept;
0213 #else
0214     Q_DECLARE_STRONGLY_ORDERED_LITERAL_TYPE(QUuid)
0215 #endif // QT_CORE_REMOVED_SINCE(6, 8)
0216 #if defined(Q_OS_WIN) || defined(Q_QDOC)
0217     // On Windows we have a type GUID that is used by the platform API, so we
0218     // provide convenience operators to cast from and to this type.
0219     constexpr QUuid(const GUID &guid) noexcept
0220         : data1(guid.Data1), data2(guid.Data2), data3(guid.Data3),
0221           data4{guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3],
0222                 guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]} {}
0223 
0224     constexpr QUuid &operator=(const GUID &guid) noexcept
0225     {
0226         *this = QUuid(guid);
0227         return *this;
0228     }
0229 
0230     constexpr operator GUID() const noexcept
0231     {
0232         GUID guid = { data1, data2, data3, { data4[0], data4[1], data4[2], data4[3], data4[4], data4[5], data4[6], data4[7] } };
0233         return guid;
0234     }
0235 private:
0236     friend constexpr bool comparesEqual(const QUuid &lhs, const GUID &rhs) noexcept
0237     {
0238         return comparesEqual(lhs, QUuid(rhs));
0239     }
0240 public:
0241 /*  To prevent a meta-type creation ambiguity on Windows, we put comparison
0242     macros under NOT QT_CORE_REMOVED_SINCE(6, 8) part. */
0243 #if QT_CORE_REMOVED_SINCE(6, 8)
0244     constexpr bool operator==(const GUID &guid) const noexcept
0245     {
0246         return comparesEqual(*this, QUuid(guid));
0247     }
0248 
0249     constexpr bool operator!=(const GUID &guid) const noexcept
0250     {
0251         return !operator==(guid);
0252     }
0253 #else
0254     Q_DECLARE_EQUALITY_COMPARABLE_LITERAL_TYPE(QUuid, GUID)
0255 #endif // !QT_CORE_REMOVED_SINCE(6, 8)
0256 #endif
0257 public:
0258     static QUuid createUuid();
0259 #if QT_CORE_REMOVED_SINCE(6, 8)
0260     static QUuid createUuidV3(const QUuid &ns, const QByteArray &baseData) noexcept;
0261     static QUuid createUuidV5(const QUuid &ns, const QByteArray &baseData) noexcept;
0262 #endif
0263     static QUuid createUuidV5(QUuid ns, QByteArrayView baseData) noexcept;
0264 #ifndef QT_BOOTSTRAPPED
0265     static QUuid createUuidV3(QUuid ns, QByteArrayView baseData) noexcept;
0266 #if !QT_CORE_REMOVED_SINCE(6, 8)
0267     Q_WEAK_OVERLOAD
0268 #endif
0269     static inline QUuid createUuidV3(const QUuid &ns, const QString &baseData)
0270     {
0271         return QUuid::createUuidV3(ns, qToByteArrayViewIgnoringNull(baseData.toUtf8()));
0272     }
0273 #endif
0274 #if !QT_CORE_REMOVED_SINCE(6, 8)
0275     Q_WEAK_OVERLOAD
0276 #endif
0277     static inline QUuid createUuidV5(const QUuid &ns, const QString &baseData)
0278     {
0279         return QUuid::createUuidV5(ns, qToByteArrayViewIgnoringNull(baseData.toUtf8()));
0280     }
0281 
0282     static QUuid createUuidV7();
0283 
0284 private:
0285     static constexpr bool isKnownVersion(Version v) noexcept
0286     {
0287         switch (v) {
0288         case VerUnknown:
0289             return false;
0290         case Time:
0291         case EmbeddedPOSIX:
0292         case Md5:
0293         case Random:
0294         case Sha1:
0295         case UnixEpoch:
0296             return true;
0297         }
0298         return false;
0299     }
0300 
0301 public:
0302 #if QT_CORE_REMOVED_SINCE(6, 9)
0303     QUuid::Variant variant() const noexcept;
0304     QUuid::Version version() const noexcept;
0305 #endif
0306     constexpr Variant variant(QT6_DECL_NEW_OVERLOAD) const noexcept
0307     {
0308         // Check the 3 MSB of data4[0]
0309         const quint8 var = data4[0] & 0xE0;
0310         if (var < 0x80)
0311             return isNull(QT6_CALL_NEW_OVERLOAD) ? VarUnknown : NCS;
0312         if (var < 0xC0)
0313             return DCE;
0314         return Variant(var >> 5); // Microsoft or Reserved
0315     }
0316     constexpr Version version(QT6_DECL_NEW_OVERLOAD) const noexcept
0317     {
0318         // Check the 4 MSB of data3
0319         const Version ver = Version(data3 >> 12);
0320         // Check that variant() == DCE and version is in a valid range
0321         if (isKnownVersion(ver) && (data4[0] & 0xC0) == 0x80)
0322             return ver;
0323         return VerUnknown;
0324     }
0325 
0326 #if defined(Q_OS_DARWIN) || defined(Q_QDOC)
0327     static QUuid fromCFUUID(CFUUIDRef uuid);
0328     CFUUIDRef toCFUUID() const Q_DECL_CF_RETURNS_RETAINED;
0329     static QUuid fromNSUUID(const NSUUID *uuid);
0330     NSUUID *toNSUUID() const Q_DECL_NS_RETURNS_AUTORELEASED;
0331 #endif
0332 
0333     uint    data1;
0334     ushort  data2;
0335     ushort  data3;
0336     uchar   data4[8];
0337 };
0338 
0339 Q_DECLARE_TYPEINFO(QUuid, Q_PRIMITIVE_TYPE);
0340 
0341 #ifndef QT_NO_DATASTREAM
0342 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QUuid &);
0343 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QUuid &);
0344 #endif
0345 
0346 #ifndef QT_NO_DEBUG_STREAM
0347 Q_CORE_EXPORT QDebug operator<<(QDebug, const QUuid &);
0348 #endif
0349 
0350 Q_CORE_EXPORT size_t qHash(const QUuid &uuid, size_t seed = 0) noexcept;
0351 
0352 QUuid::QUuid(Id128Bytes uuid, QSysInfo::Endian order) noexcept
0353 {
0354     char bytes[sizeof uuid];
0355     if (order == QSysInfo::LittleEndian)
0356         qbswap(uuid, bytes);
0357     else
0358         memcpy(bytes, &uuid, sizeof bytes);
0359     data1 = qFromBigEndian<quint32>(&bytes[0]);
0360     data2 = qFromBigEndian<quint16>(&bytes[4]);
0361     data3 = qFromBigEndian<quint16>(&bytes[6]);
0362     memcpy(data4, &bytes[8], sizeof(data4));
0363 }
0364 
0365 QUuid::Id128Bytes QUuid::toBytes(QSysInfo::Endian order) const noexcept
0366 {
0367     Id128Bytes result = {};
0368     qToBigEndian(data1, &result.data[0]);
0369     qToBigEndian(data2, &result.data[4]);
0370     qToBigEndian(data3, &result.data[6]);
0371     memcpy(&result.data[8], data4, sizeof(data4));
0372     if (order == QSysInfo::LittleEndian)
0373         return qbswap(result);
0374     return result;
0375 }
0376 
0377 QUuid QUuid::fromBytes(const void *bytes, QSysInfo::Endian order)
0378 {
0379     Id128Bytes result = {};
0380     memcpy(result.data, bytes, sizeof(result));
0381     return QUuid(result, order);
0382 }
0383 
0384 #ifdef QT_SUPPORTS_INT128
0385 constexpr QUuid QUuid::fromUInt128(quint128 uuid, QSysInfo::Endian order) noexcept
0386 {
0387     QUuid result = {};
0388     if (order == QSysInfo::BigEndian) {
0389         result.data1 = qFromBigEndian<quint32>(int(uuid));
0390         result.data2 = qFromBigEndian<quint16>(ushort(uuid >> 32));
0391         result.data3 = qFromBigEndian<quint16>(ushort(uuid >> 48));
0392         for (int i = 0; i < 8; ++i)
0393             result.data4[i] = uchar(uuid >> (64 + i * 8));
0394     } else {
0395         result.data1 = qFromLittleEndian<quint32>(uint(uuid >> 96));
0396         result.data2 = qFromLittleEndian<quint16>(ushort(uuid >> 80));
0397         result.data3 = qFromLittleEndian<quint16>(ushort(uuid >> 64));
0398         for (int i = 0; i < 8; ++i)
0399             result.data4[i] = uchar(uuid >> (56 - i * 8));
0400     }
0401     return result;
0402 }
0403 
0404 constexpr quint128 QUuid::toUInt128(QSysInfo::Endian order) const noexcept
0405 {
0406     quint128 result = {};
0407     if (order == QSysInfo::BigEndian) {
0408         for (int i = 0; i < 8; ++i)
0409             result |= quint64(data4[i]) << (i * 8);
0410         result = result << 64;
0411         result |= quint64(qToBigEndian<quint16>(data3)) << 48;
0412         result |= quint64(qToBigEndian<quint16>(data2)) << 32;
0413         result |= qToBigEndian<quint32>(data1);
0414     } else {
0415         result = qToLittleEndian<quint32>(data1);
0416         result = result << 32;
0417         result |= quint64(qToLittleEndian<quint16>(data2)) << 16;
0418         result |= quint64(qToLittleEndian<quint16>(data3));
0419         result = result << 64;
0420         for (int i = 0; i < 8; ++i)
0421             result |= quint64(data4[i]) << (56 - i * 8);
0422     }
0423     return result;
0424 }
0425 #endif
0426 
0427 #if defined(Q_QDOC)
0428 // provide fake declarations of qXXXEndian() functions, so that qDoc could
0429 // distinguish them from the general template
0430 QUuid::Id128Bytes qFromBigEndian(QUuid::Id128Bytes src);
0431 QUuid::Id128Bytes qFromLittleEndian(QUuid::Id128Bytes src);
0432 QUuid::Id128Bytes qToBigEndian(QUuid::Id128Bytes src);
0433 QUuid::Id128Bytes qToLittleEndian(QUuid::Id128Bytes src);
0434 #endif
0435 
0436 QT_END_NAMESPACE
0437 
0438 #endif // QUUID_H