rippled
Loading...
Searching...
No Matches
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 <xrpl/basics/contract.h>
24#include <algorithm>
25#include <cstdint>
26#include <lz4.h>
27#include <stdexcept>
28#include <vector>
29
30namespace ripple {
31
32namespace compression_algorithms {
33
42template <typename BufferFactory>
44lz4Compress(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
73inline std::size_t
75 std::uint8_t const* in,
76 std::size_t inSizeUnchecked,
77 std::uint8_t* decompressed,
78 std::size_t decompressedSizeUnchecked)
79{
80 int const inSize = static_cast<int>(inSizeUnchecked);
81 int const decompressedSize = static_cast<int>(decompressedSizeUnchecked);
82
83 if (inSize <= 0)
84 Throw<std::runtime_error>("lz4Decompress: integer overflow (input)");
85
86 if (decompressedSize <= 0)
87 Throw<std::runtime_error>("lz4Decompress: integer overflow (output)");
88
89 if (LZ4_decompress_safe(
90 reinterpret_cast<const char*>(in),
91 reinterpret_cast<char*>(decompressed),
92 inSize,
93 decompressedSize) != decompressedSize)
94 Throw<std::runtime_error>("lz4Decompress: failed");
95
96 return decompressedSize;
97}
98
107template <typename InputStream>
110 InputStream& in,
111 std::size_t inSize,
112 std::uint8_t* decompressed,
113 std::size_t decompressedSize)
114{
115 std::vector<std::uint8_t> compressed;
116 std::uint8_t const* chunk = nullptr;
117 int chunkSize = 0;
118 int copiedInSize = 0;
119 auto const currentBytes = in.ByteCount();
120
121 // Use the first chunk if it is >= inSize bytes of the compressed message.
122 // Otherwise copy inSize bytes of chunks into compressed buffer and
123 // use the buffer to decompress.
124 while (in.Next(reinterpret_cast<void const**>(&chunk), &chunkSize))
125 {
126 if (copiedInSize == 0)
127 {
128 if (chunkSize >= inSize)
129 {
130 copiedInSize = inSize;
131 break;
132 }
133 compressed.resize(inSize);
134 }
135
136 chunkSize = chunkSize < (inSize - copiedInSize)
137 ? chunkSize
138 : (inSize - copiedInSize);
139
140 std::copy(chunk, chunk + chunkSize, compressed.data() + copiedInSize);
141
142 copiedInSize += chunkSize;
143
144 if (copiedInSize == inSize)
145 {
146 chunk = compressed.data();
147 break;
148 }
149 }
150
151 // Put back unused bytes
152 if (in.ByteCount() > (currentBytes + copiedInSize))
153 in.BackUp(in.ByteCount() - currentBytes - copiedInSize);
154
155 if ((copiedInSize == 0 && chunkSize < inSize) ||
156 (copiedInSize > 0 && copiedInSize != inSize))
157 Throw<std::runtime_error>("lz4 decompress: insufficient input size");
158
159 return lz4Decompress(chunk, inSize, decompressed, decompressedSize);
160}
161
162} // namespace compression_algorithms
163
164} // namespace ripple
165
166#endif // RIPPLED_COMPRESSIONALGORITHMS_H_INCLUDED
T copy(T... args)
T data(T... args)
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)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
T resize(T... args)