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 <cstdint>
26 #include <lz4.h>
27 #include <stdexcept>
28 #include <vector>
29 
30 namespace ripple {
31 
32 namespace compression_algorithms {
33 
42 template <typename BufferFactory>
44 lz4Compress(void const* in, std::size_t inSize, BufferFactory&& bf)
45 {
46  if (inSize > UINT32_MAX)
47  Throw<std::runtime_error>("lz4 compress: invalid size");
48 
49  auto const outCapacity = LZ4_compressBound(inSize);
50 
51  // Request the caller to allocate and return the buffer to hold compressed
52  // data
53  auto compressed = bf(outCapacity);
54 
55  auto compressedSize = LZ4_compress_default(
56  reinterpret_cast<const char*>(in),
57  reinterpret_cast<char*>(compressed),
58  inSize,
59  outCapacity);
60  if (compressedSize == 0)
61  Throw<std::runtime_error>("lz4 compress: failed");
62 
63  return compressedSize;
64 }
65 
73 inline std::size_t
75  std::uint8_t const* in,
76  std::size_t inSize,
77  std::uint8_t* decompressed,
78  std::size_t decompressedSize)
79 {
80  auto ret = LZ4_decompress_safe(
81  reinterpret_cast<const char*>(in),
82  reinterpret_cast<char*>(decompressed),
83  inSize,
84  decompressedSize);
85 
86  if (ret <= 0 || ret != decompressedSize)
87  Throw<std::runtime_error>("lz4 decompress: failed");
88 
89  return decompressedSize;
90 }
91 
100 template <typename InputStream>
103  InputStream& in,
104  std::size_t inSize,
105  std::uint8_t* decompressed,
106  std::size_t decompressedSize)
107 {
108  std::vector<std::uint8_t> compressed;
109  std::uint8_t const* chunk = nullptr;
110  int chunkSize = 0;
111  int copiedInSize = 0;
112  auto const currentBytes = in.ByteCount();
113 
114  // Use the first chunk if it is >= inSize bytes of the compressed message.
115  // Otherwise copy inSize bytes of chunks into compressed buffer and
116  // use the buffer to decompress.
117  while (in.Next(reinterpret_cast<void const**>(&chunk), &chunkSize))
118  {
119  if (copiedInSize == 0)
120  {
121  if (chunkSize >= inSize)
122  {
123  copiedInSize = inSize;
124  break;
125  }
126  compressed.resize(inSize);
127  }
128 
129  chunkSize = chunkSize < (inSize - copiedInSize)
130  ? chunkSize
131  : (inSize - copiedInSize);
132 
133  std::copy(chunk, chunk + chunkSize, compressed.data() + copiedInSize);
134 
135  copiedInSize += chunkSize;
136 
137  if (copiedInSize == inSize)
138  {
139  chunk = compressed.data();
140  break;
141  }
142  }
143 
144  // Put back unused bytes
145  if (in.ByteCount() > (currentBytes + copiedInSize))
146  in.BackUp(in.ByteCount() - currentBytes - copiedInSize);
147 
148  if ((copiedInSize == 0 && chunkSize < inSize) ||
149  (copiedInSize > 0 && copiedInSize != inSize))
150  Throw<std::runtime_error>("lz4 decompress: insufficient input size");
151 
152  return lz4Decompress(chunk, inSize, decompressed, decompressedSize);
153 }
154 
155 } // namespace compression_algorithms
156 
157 } // namespace ripple
158 
159 #endif // RIPPLED_COMPRESSIONALGORITHMS_H_INCLUDED
std::vector::resize
T resize(T... args)
vector
ripple::QualityDirection::in
@ in
algorithm
stdexcept
cstdint
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:74
std::uint8_t
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
std::vector::data
T data(T... args)