diff options
| author | Linus Jahn <lnj@kaidan.im> | 2022-09-14 20:16:59 +0200 |
|---|---|---|
| committer | Linus Jahn <lnj@kaidan.im> | 2022-09-14 22:28:32 +0200 |
| commit | f48aedd89fc7654a8435367aa50a124724dfcd0c (patch) | |
| tree | e30432f1f0a5625eb89b0bb3b32cbe8a9031fa81 /src | |
| parent | 50be94233ad615ef8061e344f4123745714339c7 (diff) | |
| download | qxmpp-f48aedd89fc7654a8435367aa50a124724dfcd0c.tar.gz | |
BitsOfBinaryData: Add fromByteArray() utility function
It automatically hashes the data and creates a content ID.
Diffstat (limited to 'src')
| -rw-r--r-- | src/base/QXmppBitsOfBinaryData.cpp | 23 | ||||
| -rw-r--r-- | src/base/QXmppBitsOfBinaryData.h | 2 |
2 files changed, 25 insertions, 0 deletions
diff --git a/src/base/QXmppBitsOfBinaryData.cpp b/src/base/QXmppBitsOfBinaryData.cpp index 1592a2e5..6f9226de 100644 --- a/src/base/QXmppBitsOfBinaryData.cpp +++ b/src/base/QXmppBitsOfBinaryData.cpp @@ -41,6 +41,29 @@ QXmppBitsOfBinaryDataPrivate::QXmppBitsOfBinaryDataPrivate() /// /// +/// Creates bits of binary data from a QByteArray. +/// +/// This hashes the data to generate a content ID. The MIME type is not set. +/// +/// \note This blocks while hashing the data. You may want to run this via QtConcurrent or a +/// QThreadPool to run this on for large amounts of data. +/// +/// \since QXmpp 1.5 +/// +QXmppBitsOfBinaryData QXmppBitsOfBinaryData::fromByteArray(QByteArray data) +{ + QXmppBitsOfBinaryContentId cid; + cid.setHash(QCryptographicHash::hash(data, QCryptographicHash::Sha1)); + cid.setAlgorithm(QCryptographicHash::Sha1); + + QXmppBitsOfBinaryData bobData; + bobData.d->cid = std::move(cid); + bobData.d->data = std::move(data); + + return bobData; +} + +/// /// Default constructor /// QXmppBitsOfBinaryData::QXmppBitsOfBinaryData() diff --git a/src/base/QXmppBitsOfBinaryData.h b/src/base/QXmppBitsOfBinaryData.h index 3e5ad20b..e0a0226c 100644 --- a/src/base/QXmppBitsOfBinaryData.h +++ b/src/base/QXmppBitsOfBinaryData.h @@ -18,6 +18,8 @@ class QXmppBitsOfBinaryContentId; class QXMPP_EXPORT QXmppBitsOfBinaryData { public: + static QXmppBitsOfBinaryData fromByteArray(QByteArray data); + QXmppBitsOfBinaryData(); QXmppBitsOfBinaryData(const QXmppBitsOfBinaryData &); QXmppBitsOfBinaryData(QXmppBitsOfBinaryData &&); |
