rippled
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 <ripple/basics/CompressionAlgorithms.h>
24 #include <ripple/basics/Log.h>
25 #include <lz4frame.h>
26 
27 namespace ripple {
28 
29 namespace compression {
30 
31 std::size_t constexpr headerBytes = 6;
33 
34 // All values other than 'none' must have the high bit. The low order four bits
35 // must be 0.
36 enum class Algorithm : std::uint8_t { None = 0x00, LZ4 = 0x90 };
37 
38 enum class Compressed : std::uint8_t { On, Off };
39 
48 template <typename InputStream>
51  InputStream& in,
52  std::size_t inSize,
53  std::uint8_t* decompressed,
54  std::size_t decompressedSize,
55  Algorithm algorithm = Algorithm::LZ4)
56 {
57  try
58  {
59  if (algorithm == Algorithm::LZ4)
61  in, inSize, decompressed, decompressedSize);
62  else
63  {
64  JLOG(debugLog().warn())
65  << "decompress: invalid compression algorithm "
66  << static_cast<int>(algorithm);
67  assert(0);
68  }
69  }
70  catch (...)
71  {
72  }
73  return 0;
74 }
75 
85 template <class BufferFactory>
88  void const* in,
89  std::size_t inSize,
90  BufferFactory&& bf,
91  Algorithm algorithm = Algorithm::LZ4)
92 {
93  try
94  {
95  if (algorithm == Algorithm::LZ4)
97  in, inSize, std::forward<BufferFactory>(bf));
98  else
99  {
100  JLOG(debugLog().warn()) << "compress: invalid compression algorithm"
101  << static_cast<int>(algorithm);
102  assert(0);
103  }
104  }
105  catch (...)
106  {
107  }
108  return 0;
109 }
110 } // namespace compression
111 
112 } // namespace ripple
113 
114 #endif // RIPPLED_COMPRESSION_H_INCLUDED
ripple::compression::Compressed::On
@ On
ripple::compression::headerBytes
constexpr std::size_t headerBytes
Definition: Compression.h:31
ripple::QualityDirection::in
@ in
ripple::compression::Algorithm::LZ4
@ LZ4
ripple::debugLog
beast::Journal debugLog()
Returns a debug journal.
Definition: Log.cpp:452
ripple::compression::compress
std::size_t compress(void const *in, std::size_t inSize, BufferFactory &&bf, Algorithm algorithm=Algorithm::LZ4)
Compress input data.
Definition: Compression.h:87
ripple::compression::Compressed::Off
@ Off
ripple::compression::Compressed
Compressed
Definition: Compression.h:38
std::uint8_t
ripple::compression::Algorithm
Algorithm
Definition: Compression.h:36
ripple::compression_algorithms::lz4Decompress
std::size_t lz4Decompress(std::uint8_t const *in, std::size_t inSizeUnchecked, std::uint8_t *decompressed, std::size_t decompressedSizeUnchecked)
Definition: CompressionAlgorithms.h:74
ripple::compression::decompress
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:50
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::compression_algorithms::lz4Compress
std::size_t lz4Compress(void const *in, std::size_t inSize, BufferFactory &&bf)
LZ4 block compression.
Definition: CompressionAlgorithms.h:44
std::size_t
ripple::compression::headerBytesCompressed
constexpr std::size_t headerBytesCompressed
Definition: Compression.h:32
ripple::compression::Algorithm::None
@ None