From 9634f2737ba27bedea07ca9d395c9b8d4bd6df5d Mon Sep 17 00:00:00 2001 From: Simon Pribylski Date: Thu, 2 Nov 2023 23:43:50 +0100 Subject: [PATCH] use of VarLong for Packet size --- src/blitz/misc/Compression.cpp | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/blitz/misc/Compression.cpp b/src/blitz/misc/Compression.cpp index 95b9576..ca6138f 100644 --- a/src/blitz/misc/Compression.cpp +++ b/src/blitz/misc/Compression.cpp @@ -1,5 +1,6 @@ #include "blitz/misc/Compression.h" +#include "blitz/VarInt.h" #include #include @@ -34,8 +35,8 @@ DataBuffer Compress(const DataBuffer& buffer) { if (buffer.GetSize() < COMPRESSION_THRESHOLD) { // Don't compress since it's a small packet - std::uint64_t compressedDataLength = 0; - std::uint64_t packetLength = buffer.GetSize() + sizeof(compressedDataLength); + VarLong compressedDataLength = 0; + std::uint64_t packetLength = compressedDataLength.GetSerializedLength() + buffer.GetSize(); packet << packetLength; packet << compressedDataLength; @@ -45,22 +46,22 @@ DataBuffer Compress(const DataBuffer& buffer) { DataBuffer compressedData = Deflate(buffer.data(), buffer.GetSize()); - std::uint64_t compressedDataLength = buffer.GetSize(); - std::uint64_t packetLength = compressedData.GetSize() + sizeof(compressedDataLength); + VarLong uncompressedDataLength = buffer.GetSize(); + std::uint64_t packetLength = uncompressedDataLength.GetSerializedLength() + compressedData.GetSize(); packet << packetLength; - packet << compressedDataLength; + packet << uncompressedDataLength; packet.WriteSome(compressedData.data(), compressedData.GetSize()); return packet; } DataBuffer Decompress(DataBuffer& buffer, std::uint64_t packetLength) { - std::uint64_t uncompressedLength; + VarLong uncompressedLength; buffer >> uncompressedLength; - std::uint64_t compressedLength = packetLength - sizeof(uncompressedLength); + std::uint64_t compressedLength = packetLength - uncompressedLength.GetSerializedLength(); - if (uncompressedLength == 0) { + if (uncompressedLength.GetValue() == 0) { // Data already uncompressed. Nothing to do DataBuffer ret; buffer.ReadSome(ret, compressedLength); @@ -69,7 +70,7 @@ DataBuffer Decompress(DataBuffer& buffer, std::uint64_t packetLength) { assert(buffer.GetReadOffset() + compressedLength <= buffer.GetSize()); - return Inflate(buffer.data() + buffer.GetReadOffset(), compressedLength, uncompressedLength); + return Inflate(buffer.data() + buffer.GetReadOffset(), compressedLength, uncompressedLength.GetValue()); } DataBuffer Decompress(DataBuffer& buffer) {