30 const size_t headroom,
31 const size_t tailroom)
34 if (src.
size() > LZ4_MAX_INPUT_SIZE)
35 OPENVPN_THROW(lz4_error,
"compress buffer size=" << src.
size() <<
" exceeds LZ4_MAX_INPUT_SIZE=" << LZ4_MAX_INPUT_SIZE);
39 dest->init_headroom(headroom);
43 const std::uint32_t size = htonl(clamp_to_typerange<uint32_t>(src.
size()));
44 dest->write(&size,
sizeof(size));
48 const int comp_size = ::LZ4_compress_default((
const char *)src.
c_data(),
49 (
char *)dest->data_end(),
51 (
int)dest->remaining(tailroom));
53 OPENVPN_THROW(lz4_error,
"LZ4_compress_default returned error status=" << comp_size);
54 dest->inc_size(comp_size);
59 const size_t headroom,
60 const size_t tailroom,
61 size_t max_decompressed_size = LZ4_MAX_INPUT_SIZE)
65 if (src.
size() <
sizeof(std::uint32_t))
66 OPENVPN_THROW(lz4_error,
"decompress buffer size=" << src.
size() <<
" is too small");
68 src.
read(&size,
sizeof(size));
70 if (max_decompressed_size > LZ4_MAX_INPUT_SIZE)
71 max_decompressed_size = LZ4_MAX_INPUT_SIZE;
72 if (max_decompressed_size && size > max_decompressed_size)
73 OPENVPN_THROW(lz4_error,
"decompress expansion size=" << size <<
" is too large (must be <= " << max_decompressed_size <<
')');
77 dest->init_headroom(headroom);
80 const int decomp_size = LZ4_decompress_safe((
const char *)src.
c_data(),
85 OPENVPN_THROW(lz4_error,
"LZ4_decompress_safe returned error status=" << decomp_size);
86 if (
static_cast<unsigned int>(decomp_size) != size)
87 OPENVPN_THROW(lz4_error,
"decompress size inconsistency expected_size=" << size <<
" actual_size=" << decomp_size);
88 dest->inc_size(decomp_size);
const T * c_data() const
Returns a const pointer to the start of the buffer.
size_t size() const
Returns the size of the buffer in T objects.
void read(NCT *data, const size_t size)
Read data from the buffer into the specified memory location.
static Ptr Create(ArgsT &&...args)
Creates a new instance of RcEnable with the given arguments.
#define OPENVPN_EXCEPTION(C)
#define OPENVPN_THROW(exc, stuff)
BufferPtr compress(const ConstBuffer &src, const size_t headroom, const size_t tailroom)
BufferPtr decompress(const ConstBuffer &source, const size_t headroom, const size_t tailroom, size_t max_decompressed_size=LZ4_MAX_INPUT_SIZE)
OutT clamp_to_typerange(InT inVal)
Clamps the input value to the legal range for the output type.