20#ifndef RIPPLED_COMPRESSIONALGORITHMS_H_INCLUDED
21#define RIPPLED_COMPRESSIONALGORITHMS_H_INCLUDED
23#include <xrpl/basics/contract.h>
34namespace compression_algorithms {
44template <
typename BufferFactory>
48 if (inSize > UINT32_MAX)
49 Throw<std::runtime_error>(
"lz4 compress: invalid size");
51 auto const outCapacity = LZ4_compressBound(inSize);
55 auto compressed = bf(outCapacity);
57 auto compressedSize = LZ4_compress_default(
58 reinterpret_cast<char const*
>(
in),
59 reinterpret_cast<char*
>(compressed),
62 if (compressedSize == 0)
63 Throw<std::runtime_error>(
"lz4 compress: failed");
65 return compressedSize;
82 int const inSize =
static_cast<int>(inSizeUnchecked);
83 int const decompressedSize =
static_cast<int>(decompressedSizeUnchecked);
86 Throw<std::runtime_error>(
"lz4Decompress: integer overflow (input)");
88 if (decompressedSize <= 0)
89 Throw<std::runtime_error>(
"lz4Decompress: integer overflow (output)");
91 if (LZ4_decompress_safe(
92 reinterpret_cast<char const*
>(
in),
93 reinterpret_cast<char*
>(decompressed),
95 decompressedSize) != decompressedSize)
96 Throw<std::runtime_error>(
"lz4Decompress: failed");
98 return decompressedSize;
109template <
typename InputStream>
120 int copiedInSize = 0;
121 auto const currentBytes =
in.ByteCount();
126 while (
in.Next(
reinterpret_cast<void const**
>(&chunk), &chunkSize))
128 if (copiedInSize == 0)
130 if (chunkSize >= inSize)
132 copiedInSize = inSize;
135 compressed.
resize(inSize);
138 chunkSize = chunkSize < (inSize - copiedInSize)
140 : (inSize - copiedInSize);
142 std::copy(chunk, chunk + chunkSize, compressed.
data() + copiedInSize);
144 copiedInSize += chunkSize;
146 if (copiedInSize == inSize)
148 chunk = compressed.
data();
154 if (
in.ByteCount() > (currentBytes + copiedInSize))
155 in.BackUp(
in.ByteCount() - currentBytes - copiedInSize);
157 if ((copiedInSize == 0 && chunkSize < inSize) ||
158 (copiedInSize > 0 && copiedInSize != inSize))
159 Throw<std::runtime_error>(
"lz4 decompress: insufficient input size");
161 return lz4Decompress(chunk, inSize, decompressed, decompressedSize);
std::size_t lz4Compress(void const *in, std::size_t inSize, BufferFactory &&bf)
LZ4 block compression.
std::size_t lz4Decompress(std::uint8_t const *in, std::size_t inSizeUnchecked, std::uint8_t *decompressed, std::size_t decompressedSizeUnchecked)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.