xrpld
Loading...
Searching...
No Matches
Compression.h
1#pragma once
2
3#include <xrpl/basics/CompressionAlgorithms.h>
4#include <xrpl/basics/Log.h>
5#include <xrpl/beast/utility/instrumentation.h>
6
7#include <cstddef>
8#include <cstdint>
9
11
14
15// All values other than 'none' must have the high bit. The low order four bits
16// must be 0.
17enum class Algorithm : std::uint8_t { None = 0x00, LZ4 = 0x90 };
18
19enum class Compressed : std::uint8_t { On, Off };
20
30template <typename InputStream>
33 InputStream& in,
34 std::size_t inSize,
35 std::uint8_t* decompressed,
36 std::size_t decompressedSize,
37 Algorithm algorithm = Algorithm::LZ4)
38{
39 try
40 {
41 if (algorithm == Algorithm::LZ4)
42 {
44 in, inSize, decompressed, decompressedSize);
45 }
46
47 // LCOV_EXCL_START
48 JLOG(debugLog().warn()) << "decompress: invalid compression algorithm "
49 << static_cast<int>(algorithm);
50 UNREACHABLE(
51 "xrpl::compression::decompress : invalid compression "
52 "algorithm");
53 // LCOV_EXCL_STOP
54 }
55 catch (...) // NOLINT(bugprone-empty-catch)
56 {
57 }
58 return 0;
59}
60
71template <class BufferFactory>
74 void const* in,
75 std::size_t inSize,
76 BufferFactory&& bf,
77 Algorithm algorithm = Algorithm::LZ4)
78{
79 try
80 {
81 if (algorithm == Algorithm::LZ4)
82 {
84 in, inSize, std::forward<BufferFactory>(bf));
85 }
86
87 // LCOV_EXCL_START
88 JLOG(debugLog().warn()) << "compress: invalid compression algorithm"
89 << static_cast<int>(algorithm);
90 UNREACHABLE(
91 "xrpl::compression::compress : invalid compression "
92 "algorithm");
93 // LCOV_EXCL_STOP
94 }
95 catch (...) // NOLINT(bugprone-empty-catch)
96 {
97 }
98 return 0;
99}
100} // 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:73
constexpr std::size_t kHeaderBytesCompressed
Definition Compression.h:13
constexpr std::size_t kHeaderBytes
Definition Compression.h:12
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:32
beast::Journal debugLog()
Returns a debug journal.
Definition Log.cpp:399