rippled
Loading...
Searching...
No Matches
Compression.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2020 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLED_COMPRESSION_H_INCLUDED
21#define RIPPLED_COMPRESSION_H_INCLUDED
22
23#include <xrpl/basics/CompressionAlgorithms.h>
24#include <xrpl/basics/Log.h>
25
26namespace ripple {
27
28namespace compression {
29
32
33// All values other than 'none' must have the high bit. The low order four bits
34// must be 0.
35enum class Algorithm : std::uint8_t { None = 0x00, LZ4 = 0x90 };
36
37enum class Compressed : std::uint8_t { On, Off };
38
47template <typename InputStream>
50 InputStream& in,
51 std::size_t inSize,
52 std::uint8_t* decompressed,
53 std::size_t decompressedSize,
54 Algorithm algorithm = Algorithm::LZ4)
55{
56 try
57 {
58 if (algorithm == Algorithm::LZ4)
60 in, inSize, decompressed, decompressedSize);
61 else
62 {
63 JLOG(debugLog().warn())
64 << "decompress: invalid compression algorithm "
65 << static_cast<int>(algorithm);
66 UNREACHABLE(
67 "ripple::compression::decompress : invalid compression "
68 "algorithm");
69 }
70 }
71 catch (...)
72 {
73 }
74 return 0;
75}
76
86template <class BufferFactory>
89 void const* in,
90 std::size_t inSize,
91 BufferFactory&& bf,
92 Algorithm algorithm = Algorithm::LZ4)
93{
94 try
95 {
96 if (algorithm == Algorithm::LZ4)
98 in, inSize, std::forward<BufferFactory>(bf));
99 else
100 {
101 JLOG(debugLog().warn()) << "compress: invalid compression algorithm"
102 << static_cast<int>(algorithm);
103 UNREACHABLE(
104 "ripple::compression::compress : invalid compression "
105 "algorithm");
106 }
107 }
108 catch (...)
109 {
110 }
111 return 0;
112}
113} // namespace compression
114
115} // namespace ripple
116
117#endif // RIPPLED_COMPRESSION_H_INCLUDED
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:30
std::size_t compress(void const *in, std::size_t inSize, BufferFactory &&bf, Algorithm algorithm=Algorithm::LZ4)
Compress input data.
Definition: Compression.h:88
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:49
std::size_t constexpr headerBytesCompressed
Definition: Compression.h:31
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
beast::Journal debugLog()
Returns a debug journal.
Definition: Log.cpp:468