summaryrefslogtreecommitdiffstats
path: root/src/corelib/plugin/quuid.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/plugin/quuid.cpp')
-rw-r--r--src/corelib/plugin/quuid.cpp168
1 files changed, 104 insertions, 64 deletions
diff --git a/src/corelib/plugin/quuid.cpp b/src/corelib/plugin/quuid.cpp
index dcf2529ef1..b3fad47ee6 100644
--- a/src/corelib/plugin/quuid.cpp
+++ b/src/corelib/plugin/quuid.cpp
@@ -116,12 +116,12 @@ static QUuid _q_uuidFromHex(const char *src)
return QUuid();
}
-static QUuid createFromName(const QUuid &ns, const QByteArray &baseData, QCryptographicHash::Algorithm algorithm, int version)
+static QUuid createFromName(QUuid ns, QByteArrayView baseData, QCryptographicHash::Algorithm algorithm, int version) noexcept
{
- QCryptographicHash hash(algorithm);
- hash.addData(ns.toRfc4122());
- hash.addData(baseData);
- QByteArrayView hashResult = hash.resultView();
+ std::byte buffer[20];
+ Q_ASSERT(sizeof buffer >= size_t(QCryptographicHash::hashLength(algorithm)));
+ QByteArrayView hashResult
+ = QCryptographicHash::hashInto(buffer, {QByteArrayView{ns.toBytes()}, baseData}, algorithm);
Q_ASSERT(hashResult.size() >= 16);
hashResult.truncate(16); // Sha1 will be too long
@@ -142,6 +142,11 @@ static QUuid createFromName(const QUuid &ns, const QByteArray &baseData, QCrypto
\reentrant
+ \compares strong
+ \compareswith strong GUID
+ \note Comparison with GUID is Windows-only.
+ \endcompareswith
+
Using \e{U}niversally \e{U}nique \e{ID}entifiers (UUID) is a
standard way to uniquely identify entities in a distributed
computing environment. A UUID is a 16-byte (128-bit) number
@@ -301,21 +306,69 @@ static QUuid createFromName(const QUuid &ns, const QByteArray &baseData, QCrypto
*/
/*!
+ \fn QUuid::Id128Bytes qFromBigEndian(QUuid::Id128Bytes src)
+ \since 6.6
+ \relates QUuid::Id128Bytes
+ \overload
+
+ Converts \a src from big-endian byte order and returns the struct holding
+ the binary representation of UUID in host byte order.
+
+ \sa <QtEndian>
+*/
+
+/*!
+ \fn QUuid::Id128Bytes qFromLittleEndian(QUuid::Id128Bytes src)
+ \since 6.6
+ \relates QUuid::Id128Bytes
+ \overload
+
+ Converts \a src from little-endian byte order and returns the struct holding
+ the binary representation of UUID in host byte order.
+
+ \sa <QtEndian>
+*/
+
+/*!
+ \fn QUuid::Id128Bytes qToBigEndian(QUuid::Id128Bytes src)
+ \since 6.6
+ \relates QUuid::Id128Bytes
+ \overload
+
+ Converts \a src from host byte order and returns the struct holding the
+ binary representation of UUID in big-endian byte order.
+
+ \sa <QtEndian>
+*/
+
+/*!
+ \fn QUuid::Id128Bytes qToLittleEndian(QUuid::Id128Bytes src)
+ \since 6.6
+ \relates QUuid::Id128Bytes
+ \overload
+
+ Converts \a src from host byte order and returns the struct holding the
+ binary representation of UUID in little-endian byte order.
+
+ \sa <QtEndian>
+*/
+
+/*!
\fn QUuid::QUuid(Id128Bytes id128, QSysInfo::Endian order) noexcept
\since 6.6
- Creates a QUuid based on the integral \a id128 parameter and respecting the
- byte order \a order.
+ Creates a QUuid based on the integral \a id128 parameter. The input
+ \a id128 parameter is considered to have byte order \a order.
\sa fromBytes(), toBytes(), toRfc4122(), toUInt128()
*/
/*!
- \fn QUuid::QUuid(quint128 uuid, QSysInfo::Endian order) noexcept
+ \fn QUuid::fromUInt128(quint128 uuid, QSysInfo::Endian order) noexcept
\since 6.6
- Creates a QUuid based on the integral \a uuid parameter and respecting the
- byte order \a order.
+ Creates a QUuid based on the integral \a uuid parameter. The input \a uuid
+ parameter is considered to have byte order \a order.
\note This function is only present on platforms that offer a 128-bit
integer type.
@@ -335,7 +388,7 @@ static QUuid createFromName(const QUuid &ns, const QByteArray &baseData, QCrypto
\note This function is only present on platforms that offer a 128-bit
integer type.
- \sa toRfc4122(), toBytes(), fromBytes(), QUuid()
+ \sa toRfc4122(), fromUInt128(), toBytes(), fromBytes(), QUuid()
*/
/*!
@@ -479,11 +532,14 @@ QUuid QUuid::fromString(QAnyStringView text) noexcept
/*!
\since 5.0
- \fn QUuid QUuid::createUuidV3(const QUuid &ns, const QByteArray &baseData);
+ \fn QUuid QUuid::createUuidV3(QUuid ns, QByteArrayView baseData);
This function returns a new UUID with variant QUuid::DCE and version QUuid::Md5.
\a ns is the namespace and \a baseData is the basic data as described by RFC 4122.
+ \note In Qt versions prior to 6.8, this function took QByteArray, not
+ QByteArrayView.
+
\sa variant(), version(), createUuidV5()
*/
@@ -499,11 +555,14 @@ QUuid QUuid::fromString(QAnyStringView text) noexcept
/*!
\since 5.0
- \fn QUuid QUuid::createUuidV5(const QUuid &ns, const QByteArray &baseData);
+ \fn QUuid QUuid::createUuidV5(QUuid ns, QByteArrayView baseData);
This function returns a new UUID with variant QUuid::DCE and version QUuid::Sha1.
\a ns is the namespace and \a baseData is the basic data as described by RFC 4122.
+ \note In Qt versions prior to 6.8, this function took QByteArray, not
+ QByteArrayView.
+
\sa variant(), version(), createUuidV3()
*/
@@ -517,13 +576,13 @@ QUuid QUuid::fromString(QAnyStringView text) noexcept
\sa variant(), version(), createUuidV3()
*/
#ifndef QT_BOOTSTRAPPED
-QUuid QUuid::createUuidV3(const QUuid &ns, const QByteArray &baseData)
+QUuid QUuid::createUuidV3(QUuid ns, QByteArrayView baseData) noexcept
{
return createFromName(ns, baseData, QCryptographicHash::Md5, 3);
}
#endif
-QUuid QUuid::createUuidV5(const QUuid &ns, const QByteArray &baseData)
+QUuid QUuid::createUuidV5(QUuid ns, QByteArrayView baseData) noexcept
{
return createFromName(ns, baseData, QCryptographicHash::Sha1, 5);
}
@@ -552,16 +611,16 @@ QUuid QUuid::fromRfc4122(QByteArrayView bytes) noexcept
}
/*!
- \fn bool QUuid::operator==(const QUuid &other) const
+ \fn bool QUuid::operator==(const QUuid &lhs, const QUuid &rhs)
- Returns \c true if this QUuid and the \a other QUuid are identical;
+ Returns \c true if \a lhs QUuid and the \a rhs QUuid are identical;
otherwise returns \c false.
*/
/*!
- \fn bool QUuid::operator!=(const QUuid &other) const
+ \fn bool QUuid::operator!=(const QUuid &lhs, const QUuid &rhs)
- Returns \c true if this QUuid and the \a other QUuid are different;
+ Returns \c true if \a lhs QUuid and the \a rhs QUuid are different;
otherwise returns \c false.
*/
@@ -697,13 +756,15 @@ QByteArray QUuid::toRfc4122() const
*/
QDataStream &operator<<(QDataStream &s, const QUuid &id)
{
- QByteArray bytes;
+ constexpr int NumBytes = sizeof(QUuid);
+ static_assert(NumBytes == 16, "Change the serialization format when this ever hits");
+ char bytes[NumBytes];
if (s.byteOrder() == QDataStream::BigEndian) {
- bytes = id.toRfc4122();
+ const auto id128 = id.toBytes();
+ static_assert(sizeof(id128) == NumBytes);
+ memcpy(bytes, &id128, NumBytes);
} else {
- // we know how many bytes a UUID has, I hope :)
- bytes = QByteArray(16, Qt::Uninitialized);
- uchar *data = reinterpret_cast<uchar *>(bytes.data());
+ auto *data = bytes;
// for historical reasons, our little-endian serialization format
// stores each of the UUID fields in little endian, instead of storing
@@ -721,9 +782,9 @@ QDataStream &operator<<(QDataStream &s, const QUuid &id)
}
}
- if (s.writeRawData(bytes.data(), 16) != 16) {
+ if (s.writeRawData(bytes, NumBytes) != NumBytes)
s.setStatus(QDataStream::WriteFailed);
- }
+
return s;
}
@@ -848,51 +909,31 @@ QUuid::Version QUuid::version() const noexcept
}
/*!
- \fn bool QUuid::operator<(const QUuid &other) const
+ \fn bool QUuid::operator<(const QUuid &lhs, const QUuid &rhs)
- Returns \c true if this QUuid has the same \l{Variant field}
- {variant field} as the \a other QUuid and is lexicographically
- \e{before} the \a other QUuid. If the \a other QUuid has a
+ Returns \c true if \a lhs QUuid has the same \l{Variant field}
+ {variant field} as the \a rhs QUuid and is lexicographically
+ \e{before} the \a rhs QUuid. If the \a rhs QUuid has a
different variant field, the return value is determined by
comparing the two \l{QUuid::Variant} {variants}.
\sa variant()
*/
-bool QUuid::operator<(const QUuid &other) const noexcept
-{
- if (variant() != other.variant())
- return variant() < other.variant();
-
-#define ISLESS(f1, f2) if (f1!=f2) return (f1<f2);
- ISLESS(data1, other.data1);
- ISLESS(data2, other.data2);
- ISLESS(data3, other.data3);
- for (int n = 0; n < 8; n++) {
- ISLESS(data4[n], other.data4[n]);
- }
-#undef ISLESS
- return false;
-}
/*!
- \fn bool QUuid::operator>(const QUuid &other) const
+ \fn bool QUuid::operator>(const QUuid &lhs, const QUuid &rhs)
- Returns \c true if this QUuid has the same \l{Variant field}
- {variant field} as the \a other QUuid and is lexicographically
- \e{after} the \a other QUuid. If the \a other QUuid has a
+ Returns \c true if \a lhs QUuid has the same \l{Variant field}
+ {variant field} as the \a rhs QUuid and is lexicographically
+ \e{after} the \a rhs QUuid. If the \a rhs QUuid has a
different variant field, the return value is determined by
comparing the two \l{QUuid::Variant} {variants}.
\sa variant()
*/
-bool QUuid::operator>(const QUuid &other) const noexcept
-{
- return other < *this;
-}
/*!
- \fn bool operator<=(const QUuid &lhs, const QUuid &rhs)
- \relates QUuid
+ \fn bool QUuid::operator<=(const QUuid &lhs, const QUuid &rhs)
\since 5.5
Returns \c true if \a lhs has the same \l{Variant field}
@@ -905,8 +946,7 @@ bool QUuid::operator>(const QUuid &other) const noexcept
*/
/*!
- \fn bool operator>=(const QUuid &lhs, const QUuid &rhs)
- \relates QUuid
+ \fn bool QUuid::operator>=(const QUuid &lhs, const QUuid &rhs)
\since 5.5
Returns \c true if \a lhs has the same \l{Variant field}
@@ -942,7 +982,7 @@ QUuid QUuid::createUuid()
return result;
}
-#else // Q_OS_WIN
+#elif !defined(QT_BOOTSTRAPPED)
QUuid QUuid::createUuid()
{
@@ -956,20 +996,20 @@ QUuid QUuid::createUuid()
return result;
}
-#endif // !Q_OS_WIN
+#endif // !Q_OS_WIN && !QT_BOOTSTRAPPED
/*!
- \fn bool QUuid::operator==(const GUID &guid) const
+ \fn bool QUuid::operator==(const QUuid &lhs, const GUID &rhs)
- Returns \c true if this UUID is equal to the Windows GUID \a guid;
+ Returns \c true if \a lhs UUID is equal to the Windows GUID \a rhs;
otherwise returns \c false.
*/
/*!
- \fn bool QUuid::operator!=(const GUID &guid) const
+ \fn bool QUuid::operator!=(const QUuid &lhs, const GUID &rhs)
- Returns \c true if this UUID is not equal to the Windows GUID \a
- guid; otherwise returns \c false.
+ Returns \c true if \a lhs UUID is not equal to the Windows GUID \a rhs;
+ otherwise returns \c false.
*/
#ifndef QT_NO_DEBUG_STREAM