rippled
CompressionAlgorithms.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_COMPRESSIONALGORITHMS_H_INCLUDED
21 #define RIPPLED_COMPRESSIONALGORITHMS_H_INCLUDED
22 
23 #include <ripple/basics/contract.h>
24 #include <algorithm>
25 #include <lz4.h>
26 
27 namespace ripple {
28 
29 namespace compression_algorithms {
30 
34 inline void
35 doThrow(const char* message)
36 {
37  Throw<std::runtime_error>(message);
38 }
39 
48 template <typename BufferFactory>
50 lz4Compress(void const* in, std::size_t inSize, BufferFactory&& bf)
51 {
52  if (inSize > UINT32_MAX)
53  doThrow("lz4 compress: invalid size");
54 
55  auto const outCapacity = LZ4_compressBound(inSize);
56 
57  // Request the caller to allocate and return the buffer to hold compressed
58  // data
59  auto compressed = bf(outCapacity);
60 
61  auto compressedSize = LZ4_compress_default(
62  reinterpret_cast<const char*>(in),
63  reinterpret_cast<char*>(compressed),
64  inSize,
65  outCapacity);
66  if (compressedSize == 0)
67  doThrow("lz4 compress: failed");
68 
69  return compressedSize;
70 }
71 
79 inline std::size_t
81  std::uint8_t const* in,
82  std::size_t inSize,
83  std::uint8_t* decompressed,
84  std::size_t decompressedSize)
85 {
86  auto ret = LZ4_decompress_safe(
87  reinterpret_cast<const char*>(in),
88  reinterpret_cast<char*>(decompressed),
89  inSize,
90  decompressedSize);
91 
92  if (ret <= 0 || ret != decompressedSize)
93  doThrow("lz4 decompress: failed");
94 
95  return decompressedSize;
96 }
97 
106 template <typename InputStream>
109  InputStream& in,
110  std::size_t inSize,
111  std::uint8_t* decompressed,
112  std::size_t decompressedSize)
113 {
114  std::vector<std::uint8_t> compressed;
115  std::uint8_t const* chunk = nullptr;
116  int chunkSize = 0;
117  int copiedInSize = 0;
118  auto const currentBytes = in.ByteCount();
119 
120  // Use the first chunk if it is >= inSize bytes of the compressed message.
121  // Otherwise copy inSize bytes of chunks into compressed buffer and
122  // use the buffer to decompress.
123  while (in.Next(reinterpret_cast<void const**>(&chunk), &chunkSize))
124  {
125  if (copiedInSize == 0)
126  {
127  if (chunkSize >= inSize)
128  {
129  copiedInSize = inSize;
130  break;
131  }
132  compressed.resize(inSize);
133  }
134 
135  chunkSize = chunkSize < (inSize - copiedInSize)
136  ? chunkSize
137  : (inSize - copiedInSize);
138 
139  std::copy(chunk, chunk + chunkSize, compressed.data() + copiedInSize);
140 
141  copiedInSize += chunkSize;
142 
143  if (copiedInSize == inSize)
144  {
145  chunk = compressed.data();
146  break;
147  }
148  }
149 
150  // Put back unused bytes
151  if (in.ByteCount() > (currentBytes + copiedInSize))
152  in.BackUp(in.ByteCount() - currentBytes - copiedInSize);
153 
154  if ((copiedInSize == 0 && chunkSize < inSize) ||
155  (copiedInSize > 0 && copiedInSize != inSize))
156  doThrow("lz4 decompress: insufficient input size");
157 
158  return lz4Decompress(chunk, inSize, decompressed, decompressedSize);
159 }
160 
161 } // namespace compression_algorithms
162 
163 } // namespace ripple
164 
165 #endif // RIPPLED_COMPRESSIONALGORITHMS_H_INCLUDED
std::vector::resize
T resize(T... args)
std::vector
STL class.
ripple::QualityDirection::in
@ in
algorithm
std::copy
T copy(T... args)
ripple::compression_algorithms::lz4Decompress
std::size_t lz4Decompress(std::uint8_t const *in, std::size_t inSize, std::uint8_t *decompressed, std::size_t decompressedSize)
Definition: CompressionAlgorithms.h:80
std::uint8_t
ripple::compression_algorithms::doThrow
void doThrow(const char *message)
Convenience wrapper for Throw.
Definition: CompressionAlgorithms.h:35
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:50
std::size_t
std::vector::data
T data(T... args)