xrpld
Loading...
Searching...
No Matches
Compression.h
1#pragma once
2
3#include <xrpl/basics/CompressionAlgorithms.h>
4#include <xrpl/basics/Log.h>
5
7
10
11// All values other than 'none' must have the high bit. The low order four bits
12// must be 0.
13enum class Algorithm : std::uint8_t { None = 0x00, LZ4 = 0x90 };
14
15enum class Compressed : std::uint8_t { On, Off };
16
25template <typename InputStream>
28 InputStream& in,
29 std::size_t inSize,
30 std::uint8_t* decompressed,
31 std::size_t decompressedSize,
32 Algorithm algorithm = Algorithm::LZ4)
33{
34 try
35 {
36 if (algorithm == Algorithm::LZ4)
37 {
39 in, inSize, decompressed, decompressedSize);
40 }
41
42 // LCOV_EXCL_START
43 JLOG(debugLog().warn()) << "decompress: invalid compression algorithm "
44 << static_cast<int>(algorithm);
45 UNREACHABLE(
46 "xrpl::compression::decompress : invalid compression "
47 "algorithm");
48 // LCOV_EXCL_STOP
49 }
50 catch (...) // NOLINT(bugprone-empty-catch)
51 {
52 }
53 return 0;
54}
55
65template <class BufferFactory>
68 void const* in,
69 std::size_t inSize,
70 BufferFactory&& bf,
71 Algorithm algorithm = Algorithm::LZ4)
72{
73 try
74 {
75 if (algorithm == Algorithm::LZ4)
76 {
78 in, inSize, std::forward<BufferFactory>(bf));
79 }
80
81 // LCOV_EXCL_START
82 JLOG(debugLog().warn()) << "compress: invalid compression algorithm"
83 << static_cast<int>(algorithm);
84 UNREACHABLE(
85 "xrpl::compression::compress : invalid compression "
86 "algorithm");
87 // LCOV_EXCL_STOP
88 }
89 catch (...) // NOLINT(bugprone-empty-catch)
90 {
91 }
92 return 0;
93}
94} // namespace xrpl::compression
T forward(T... args)
std::size_t lz4Decompress(std::uint8_t const *in, std::size_t inSizeUnchecked, std::uint8_t *decompressed, std::size_t decompressedSizeUnchecked)
std::size_t lz4Compress(void const *in, std::size_t inSize, BufferFactory &&bf)
LZ4 block compression.
std::size_t compress(void const *in, std::size_t inSize, BufferFactory &&bf, Algorithm algorithm=Algorithm::LZ4)
Compress input data.
Definition Compression.h:67
constexpr std::size_t kHeaderBytesCompressed
Definition Compression.h:9
constexpr std::size_t kHeaderBytes
Definition Compression.h:8
std::size_t decompress(InputStream &in, std::size_t inSize, std::uint8_t *decompressed, std::size_t decompressedSize, Algorithm algorithm=Algorithm::LZ4)
Decompress input stream.
Definition Compression.h:27
beast::Journal debugLog()
Returns a debug journal.
Definition Log.cpp:399