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 xrpl {
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)
40 return xrpl::compression_algorithms::lz4Decompress(in, inSize, decompressed, decompressedSize);
41 else
42 {
43 // LCOV_EXCL_START
44 JLOG(debugLog().warn()) << "decompress: invalid compression algorithm " << static_cast<int>(algorithm);
45 UNREACHABLE(
46 "xrpl::compression::decompress : invalid compression "
47 "algorithm");
48 // LCOV_EXCL_STOP
49 }
50 }
51 catch (...)
52 {
53 }
54 return 0;
55}
56
66template <class BufferFactory>
68compress(void const* in, std::size_t inSize, BufferFactory&& bf, Algorithm algorithm = Algorithm::LZ4)
69{
70 try
71 {
72 if (algorithm == Algorithm::LZ4)
74 else
75 {
76 // LCOV_EXCL_START
77 JLOG(debugLog().warn()) << "compress: invalid compression algorithm" << static_cast<int>(algorithm);
78 UNREACHABLE(
79 "xrpl::compression::compress : invalid compression "
80 "algorithm");
81 // LCOV_EXCL_STOP
82 }
83 }
84 catch (...)
85 {
86 }
87 return 0;
88}
89} // namespace compression
90
91} // namespace xrpl
92
93#endif // XRPL_COMPRESSION_H_INCLUDED
T is_same_v
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:68
std::size_t constexpr headerBytes
Definition Compression.h:11
std::size_t constexpr headerBytesCompressed
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:30
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:445