QCryptographicHash: move SmallByteArray out of Private

Rename it to QSmallByteArray, and make the maximum size a template
argument. Initialize m_size to 0, to avoid a partially-formed
default-constructed objects (default-constructed containers should
always be in the empty state).

As a drive-by, fix placement of some {'s.

Don't move it into a header of its own, yet, as it lacks a lot of
features expected of a generally-reusable class. Maybe one day.

This is in preparation of re-using the class to hold the key in
QMessageAuthenticationCode.

Pick-to: 6.5
Change-Id: Iee77e03e50afdf2ebc5889feeead344bef8ab3e8
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Marc Mutz 2023-02-22 07:53:29 +01:00
parent 9a18ca59ff
commit 29050fa65b

View File

@ -121,6 +121,28 @@ static inline int SHA384_512AddLength(SHA512Context *context, unsigned int lengt
QT_BEGIN_NAMESPACE
template <size_t N>
class QSmallByteArray
{
std::array<quint8, N> m_data;
static_assert(N <= std::numeric_limits<std::uint8_t>::max());
quint8 m_size = 0;
public:
// all SMFs are ok!
quint8 *data() noexcept { return m_data.data(); }
qsizetype size() const noexcept { return qsizetype{m_size}; }
bool isEmpty() const noexcept { return size() == 0; }
void clear() noexcept { m_size = 0; }
void resizeForOverwrite(qsizetype s)
{
Q_ASSERT(s >= 0);
Q_ASSERT(size_t(s) <= N);
m_size = std::uint8_t(s);
}
QByteArrayView toByteArrayView() const noexcept
{ return QByteArrayView{m_data.data(), size()}; }
};
static constexpr qsizetype MaxHashLength = 64;
static constexpr int hashLengthInternal(QCryptographicHash::Algorithm method) noexcept
@ -273,26 +295,9 @@ public:
void sha3Finish(int bitCount, Sha3Variant sha3Variant);
#endif
#endif
class SmallByteArray {
std::array<quint8, MaxHashLength> m_data;
static_assert(MaxHashLength <= std::numeric_limits<std::uint8_t>::max());
quint8 m_size;
public:
quint8 *data() noexcept { return m_data.data(); }
qsizetype size() const noexcept { return qsizetype{m_size}; }
bool isEmpty() const noexcept { return size() == 0; }
void clear() noexcept { m_size = 0; }
void resizeForOverwrite(qsizetype s) {
Q_ASSERT(s >= 0);
Q_ASSERT(s <= MaxHashLength);
m_size = std::uint8_t(s);
}
QByteArrayView toByteArrayView() const noexcept
{ return QByteArrayView{m_data.data(), size()}; }
};
// protects result in finalize()
QBasicMutex finalizeMutex;
SmallByteArray result;
QSmallByteArray<MaxHashLength> result;
const QCryptographicHash::Algorithm method;
};