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