Skip to content

Commit

Permalink
Put back unused bytes after decompression
Browse files Browse the repository at this point in the history
  • Loading branch information
gregtatcam committed Mar 26, 2020
1 parent 0bed489 commit 377b17c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
21 changes: 16 additions & 5 deletions src/ripple/basics/CompressionAlgorithms.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,24 +110,35 @@ lz4Decompress(InputStream& in, std::size_t inSize,
// Use the first chunk if it is >= inSize bytes of the compressed message.
// Otherwise copy inSize bytes of chunks into compressed buffer and
// use the buffer to decompress.
while (copiedInSize < inSize && in.Next(reinterpret_cast<void const**>(&chunk), &chunkSize))
while (in.Next(reinterpret_cast<void const**>(&chunk), &chunkSize))
{
if (copiedInSize == 0)
{
if (chunkSize >= inSize)
{
copiedInSize = inSize;
break;
}
compressed.resize(inSize);
}

auto size = chunkSize < (inSize - copiedInSize) ? chunkSize : (inSize - copiedInSize);
chunkSize = chunkSize < (inSize - copiedInSize) ? chunkSize : (inSize - copiedInSize);

std::copy(chunk, chunk + size, compressed.data() + copiedInSize);
std::copy(chunk, chunk + chunkSize, compressed.data() + copiedInSize);

copiedInSize += size;
copiedInSize += chunkSize;

chunk = compressed.data();
if (copiedInSize == inSize)
{
chunk = compressed.data();
break;
}
}

// Put back unused bytes
if (in.ByteCount() > copiedInSize)
in.BackUp(in.ByteCount() - copiedInSize);

if ((copiedInSize == 0 && chunkSize < inSize) || (copiedInSize > 0 && copiedInSize != inSize))
doThrow("lz4 decompress: insufficient input size");

Expand Down
2 changes: 1 addition & 1 deletion src/ripple/overlay/impl/Message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ Message::setHeader(std::uint8_t* in, std::uint32_t payloadBytes, int type,
auto h = in;

auto pack = [](std::uint8_t*& in, std::uint32_t size) {
*in++ = static_cast<std::uint8_t>((size >> 24) & 0x0F); // leftmost 4 are ompression bits
*in++ = static_cast<std::uint8_t>((size >> 24) & 0x0F); // leftmost 4 are compression bits
*in++ = static_cast<std::uint8_t>((size >> 16) & 0xFF);
*in++ = static_cast<std::uint8_t>((size >> 8) & 0xFF);
*in++ = static_cast<std::uint8_t>(size & 0xFF);
Expand Down

0 comments on commit 377b17c

Please sign in to comment.