Make serializeVarintCommon SCARY

Move the part that doesn't depend on template arugment to a
serializeVarintCommonImpl function. Also it makes sense to make it
out of line.

Use the impl function but not the template one when serializing
the size of length delimited fields. We trust out selves that this
type is compatible with serializeVarintCommon.

Pick-to: 6.8 6.9
Task-number: QTBUG-128812
Change-Id: Ifcda7ebf7b2dee209c258d64724908022cf66de9
Reviewed-by: Dennis Oberst <dennis.oberst@qt.io>
This commit is contained in:
Alexey Edelev 2024-10-01 23:19:28 +02:00
parent a938e362c8
commit e2ef159b2e
2 changed files with 21 additions and 17 deletions

View File

@ -5,6 +5,23 @@
QT_BEGIN_NAMESPACE
QByteArray ProtobufScalarSerializers::serializeVarintCommonImpl(quint64 value)
{
if (value == 0)
return { 1, char(0) };
QByteArray result;
while (value != 0) {
// Put 7 bits to result buffer and mark as "not last" (0b10000000)
result.append((value & 0b01111111) | 0b10000000);
// Divide values to chunks of 7 bits and move to next chunk
value >>= 7;
}
result.back() &= ~0b10000000;
return result;
}
/*
Gets length of a byte-array and prepends to it its serialized length value
using the appropriate serialization algorithm
@ -13,7 +30,7 @@ QT_BEGIN_NAMESPACE
*/
QByteArray ProtobufScalarSerializers::prependLengthDelimitedSize(const QByteArray &data)
{
return serializeVarintCommon<uint32_t>(data.size()) + data;
return serializeVarintCommonImpl(data.size()) + data;
}
std::optional<QByteArray>

View File

@ -139,25 +139,12 @@ using if_non_packed_compatible = std::enable_if_t<is_non_packed_compatible<V>::v
// Serializers
// ###########################################################################
[[nodiscard]] QByteArray serializeVarintCommonImpl(quint64 value);
template <typename V, if_unsigned_int<V> = true>
[[nodiscard]] QByteArray serializeVarintCommon(const V &value)
{
if (value == 0)
return QByteArray(1, char(0));
quint64 varint = value;
QByteArray result;
while (varint != 0) {
// Put 7 bits to result buffer and mark as "not last" (0b10000000)
result.append((varint & 0b01111111) | 0b10000000);
// Divide values to chunks of 7 bits and move to next chunk
varint >>= 7;
}
result.data()[result.size() - 1] &= ~0b10000000;
return result;
return serializeVarintCommonImpl(quint64(value));
}
//---------------Integral and floating point types serializers---------------