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