From a45315ee749e4b33ed26ebcbafd5c955da52ca0b Mon Sep 17 00:00:00 2001 From: bibo38 Date: Sun, 10 Jan 2016 12:51:16 +0100 Subject: Fixed not working packet decompression A compressed packed contains always the uncompressed size, but in the code this size was threatened as the compressed data size. This created a failure, when the client compressed a packet. See: http://wiki.vg/Protocol#With_compression Fixes #2841 Fixes #2467 --- src/Protocol/Protocol18x.cpp | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/Protocol/Protocol18x.cpp b/src/Protocol/Protocol18x.cpp index 9b716ff92..d80e9d034 100644 --- a/src/Protocol/Protocol18x.cpp +++ b/src/Protocol/Protocol18x.cpp @@ -1834,39 +1834,44 @@ void cProtocol180::AddReceivedData(const char * a_Data, size_t a_Size) } // Check packet for compression: - UInt32 CompressedSize = 0; + UInt32 UncompressedSize = 0; AString UncompressedData; if (m_State == 3) { UInt32 NumBytesRead = static_cast(m_ReceivedData.GetReadableSpace()); - m_ReceivedData.ReadVarInt(CompressedSize); - if (CompressedSize > PacketLen) + + if (!m_ReceivedData.ReadVarInt(UncompressedSize)) { - m_Client->Kick("Bad compression"); + m_Client->Kick("Compression packet incomplete"); return; } - if (CompressedSize > 0) + + NumBytesRead -= static_cast(m_ReceivedData.GetReadableSpace()); // How many bytes has the UncompressedSize taken up? + ASSERT(PacketLen > NumBytesRead); + PacketLen -= NumBytesRead; + + if (UncompressedSize > 0) { // Decompress the data: AString CompressedData; - if (!m_ReceivedData.ReadString(CompressedData, CompressedSize) || (InflateString(CompressedData.data(), CompressedSize, UncompressedData) != Z_OK)) + VERIFY(m_ReceivedData.ReadString(CompressedData, PacketLen)); + if (InflateString(CompressedData.data(), PacketLen, UncompressedData) != Z_OK) { m_Client->Kick("Compression failure"); return; } PacketLen = static_cast(UncompressedData.size()); - } - else - { - NumBytesRead -= static_cast(m_ReceivedData.GetReadableSpace()); // How many bytes has the CompressedSize taken up? - ASSERT(PacketLen > NumBytesRead); - PacketLen -= NumBytesRead; + if (PacketLen != UncompressedSize) + { + m_Client->Kick("Wrong uncompressed packet size given"); + return; + } } } // Move the packet payload to a separate cByteBuffer, bb: cByteBuffer bb(PacketLen + 1); - if (CompressedSize == 0) + if (UncompressedSize == 0) { // No compression was used, move directly VERIFY(m_ReceivedData.ReadToByteBuffer(bb, static_cast(PacketLen))); -- cgit v1.2.3