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 <lz4.h>
25 #include <algorithm>
26 
27 namespace ripple {
28 
29 namespace compression_algorithms {
30 
34 inline void doThrow(const char *message)
35 {
36  Throw<std::runtime_error>(message);
37 }
38 
47 template<typename BufferFactory>
49 lz4Compress(void const* in,
50  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 data
58  auto compressed = bf(outCapacity);
59 
60  auto compressedSize = LZ4_compress_default(
61  reinterpret_cast<const char*>(in),
62  reinterpret_cast<char*>(compressed),
63  inSize,
64  outCapacity);
65  if (compressedSize == 0)
66  doThrow("lz4 compress: failed");
67 
68  return compressedSize;
69 }
70 
78 inline
81  std::uint8_t* decompressed, std::size_t decompressedSize)
82 {
83  auto ret = LZ4_decompress_safe(reinterpret_cast<const char*>(in),
84  reinterpret_cast<char*>(decompressed), inSize, decompressedSize);
85 
86  if (ret <= 0 || ret != decompressedSize)
87  doThrow("lz4 decompress: failed");
88 
89  return decompressedSize;
90 }
91 
100 template<typename InputStream>
102 lz4Decompress(InputStream& in, std::size_t inSize,
103  std::uint8_t* decompressed, std::size_t decompressedSize)
104 {
105  std::vector<std::uint8_t> compressed;
106  std::uint8_t const* chunk = nullptr;
107  int chunkSize = 0;
108  int copiedInSize = 0;
109  auto const currentBytes = in.ByteCount();
110 
111  // Use the first chunk if it is >= inSize bytes of the compressed message.
112  // Otherwise copy inSize bytes of chunks into compressed buffer and
113  // use the buffer to decompress.
114  while (in.Next(reinterpret_cast<void const**>(&chunk), &chunkSize))
115  {
116  if (copiedInSize == 0)
117  {
118  if (chunkSize >= inSize)
119  {
120  copiedInSize = inSize;
121  break;
122  }
123  compressed.resize(inSize);
124  }
125 
126  chunkSize = chunkSize < (inSize - copiedInSize) ? chunkSize : (inSize - copiedInSize);
127 
128  std::copy(chunk, chunk + chunkSize, compressed.data() + copiedInSize);
129 
130  copiedInSize += chunkSize;
131 
132  if (copiedInSize == inSize)
133  {
134  chunk = compressed.data();
135  break;
136  }
137  }
138 
139  // Put back unused bytes
140  if (in.ByteCount() > (currentBytes + copiedInSize))
141  in.BackUp(in.ByteCount() - currentBytes - copiedInSize);
142 
143  if ((copiedInSize == 0 && chunkSize < inSize) || (copiedInSize > 0 && copiedInSize != inSize))
144  doThrow("lz4 decompress: insufficient input size");
145 
146  return lz4Decompress(chunk, inSize, decompressed, decompressedSize);
147 }
148 
149 } // compression
150 
151 } // ripple
152 
153 #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:34
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:49
std::size_t
std::vector::data
T data(T... args)