rippled
Loading...
Searching...
No Matches
Compression.h
1#ifndef XRPL_COMPRESSION_H_INCLUDED
2#define XRPL_COMPRESSION_H_INCLUDED
3
4#include <xrpl/basics/CompressionAlgorithms.h>
5#include <xrpl/basics/Log.h>
6
7namespace ripple {
8
9namespace compression {
10
13
14// All values other than 'none' must have the high bit. The low order four bits
15// must be 0.
16enum class Algorithm : std::uint8_t { None = 0x00, LZ4 = 0x90 };
17
18enum class Compressed : std::uint8_t { On, Off };
19
28template <typename InputStream>
31 InputStream& in,
32 std::size_t inSize,
33 std::uint8_t* decompressed,
34 std::size_t decompressedSize,
35 Algorithm algorithm = Algorithm::LZ4)
36{
37 try
38 {
39 if (algorithm == Algorithm::LZ4)
41 in, inSize, decompressed, decompressedSize);
42 else
43 {
44 // LCOV_EXCL_START
45 JLOG(debugLog().warn())
46 << "decompress: invalid compression algorithm "
47 << static_cast<int>(algorithm);
48 UNREACHABLE(
49 "ripple::compression::decompress : invalid compression "
50 "algorithm");
51 // LCOV_EXCL_STOP
52 }
53 }
54 catch (...)
55 {
56 }
57 return 0;
58}
59
69template <class BufferFactory>
72 void const* in,
73 std::size_t inSize,
74 BufferFactory&& bf,
75 Algorithm algorithm = Algorithm::LZ4)
76{
77 try
78 {
79 if (algorithm == Algorithm::LZ4)
81 in, inSize, std::forward<BufferFactory>(bf));
82 else
83 {
84 // LCOV_EXCL_START
85 JLOG(debugLog().warn()) << "compress: invalid compression algorithm"
86 << static_cast<int>(algorithm);
87 UNREACHABLE(
88 "ripple::compression::compress : invalid compression "
89 "algorithm");
90 // LCOV_EXCL_STOP
91 }
92 }
93 catch (...)
94 {
95 }
96 return 0;
97}
98} // namespace compression
99
100} // namespace ripple
101
102#endif // XRPL_COMPRESSION_H_INCLUDED
T is_same_v
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)
std::size_t constexpr headerBytes
Definition Compression.h:11
std::size_t compress(void const *in, std::size_t inSize, BufferFactory &&bf, Algorithm algorithm=Algorithm::LZ4)
Compress input data.
Definition Compression.h:71
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:30
std::size_t constexpr headerBytesCompressed
Definition Compression.h:12
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
beast::Journal debugLog()
Returns a debug journal.
Definition Log.cpp:457