10 #include <libdeflate.h>
19 return {
reinterpret_cast<const char *
>(View.data()), View.size() };
31 std::visit([](
const auto & Buffer) ->
const std::byte *
33 using Variant = std::decay_t<decltype(Buffer)>;
35 if constexpr (std::is_same_v<Variant, Compression::Result::Static>)
53 m_Handle = libdeflate_alloc_compressor(CompressionFactor);
55 if (m_Handle ==
nullptr)
57 throw std::bad_alloc();
67 libdeflate_free_compressor(m_Handle);
74 template <auto Algorithm>
80 const auto BytesWrittenOut = Algorithm(m_Handle, Input, Size, Buffer.data(), Buffer.size());
82 if (BytesWrittenOut != 0)
84 return { Buffer, BytesWrittenOut };
94 auto Dynamic = cpp20::make_unique_for_overwrite<Result::Dynamic::element_type[]>(DynamicCapacity);
95 const auto BytesWrittenOut = Algorithm(m_Handle, Input, Size, Dynamic.get(), DynamicCapacity);
97 if (BytesWrittenOut != 0)
99 return { std::move(Dynamic), BytesWrittenOut };
102 DynamicCapacity *= 2;
112 return Compress<&libdeflate_gzip_compress>(Input.data(), Input.size());
121 return Compress<&libdeflate_zlib_compress>(Input.data(), Input.size());
130 return Compress<&libdeflate_zlib_compress>(Input, Size);
139 m_Handle = libdeflate_alloc_decompressor();
141 if (m_Handle ==
nullptr)
143 throw std::bad_alloc();
153 libdeflate_free_decompressor(m_Handle);
162 return Extract<&libdeflate_gzip_decompress>(Input);
171 return Extract<&libdeflate_zlib_decompress>(Input);
180 return Extract<&libdeflate_zlib_decompress>(Input, UncompressedSize);
187 template <auto Algorithm>
193 size_t BytesWrittenOut;
195 switch (Algorithm(m_Handle, Input.data(), Input.size(), Buffer.data(), Buffer.size(), &BytesWrittenOut))
197 case LIBDEFLATE_SUCCESS:
return { Buffer, BytesWrittenOut };
198 case LIBDEFLATE_INSUFFICIENT_SPACE:
break;
199 default:
throw std::runtime_error(
"Data extraction failed.");
208 size_t BytesWrittenOut;
209 auto Dynamic = cpp20::make_unique_for_overwrite<Result::Dynamic::element_type[]>(DynamicCapacity);
211 switch (Algorithm(m_Handle, Input.data(), Input.size(), Dynamic.get(), DynamicCapacity, &BytesWrittenOut))
213 case libdeflate_result::LIBDEFLATE_SUCCESS:
return { std::move(Dynamic), BytesWrittenOut };
214 case libdeflate_result::LIBDEFLATE_INSUFFICIENT_SPACE:
216 DynamicCapacity *= 2;
219 default:
throw std::runtime_error(
"Data extraction failed.");
228 template <auto Algorithm>
236 Algorithm(m_Handle, Input.data(), Input.size(), Buffer.data(), UncompressedSize,
nullptr) == libdeflate_result::LIBDEFLATE_SUCCESS
239 return { Buffer, UncompressedSize };
244 Algorithm(m_Handle, Input.data(), Input.size(), Dynamic.get(), UncompressedSize,
nullptr) == libdeflate_result::LIBDEFLATE_SUCCESS
247 return { std::move(Dynamic), UncompressedSize };
250 throw std::runtime_error(
"Data extraction failed.");
std::basic_string_view< std::byte > ContiguousByteBufferView
std::enable_if_t< std::is_array_v< T > &&(std::extent_v< T >==0), std::unique_ptr< T > > make_unique_for_overwrite(std::size_t a_Size)
Contains the result of a compression or extraction operation.
static constexpr size_t StaticCapacity
ContiguousByteBufferView GetView() const
Returns a view (of type std::byte) of the internal store.
std::string_view GetStringView() const
Returns a view (of type char) of the internal store.
std::array< std::byte, 128 KiB > Static
Result Compress(const void *Input, size_t Size)
Result CompressGZip(ContiguousByteBufferView Input)
Compressor(int CompressionFactor=6)
Creates a new compressor instance with a compression factor [0-12].
Result CompressZLib(ContiguousByteBufferView Input)
Result ExtractZLib(ContiguousByteBufferView Input)
Result ExtractGZip(ContiguousByteBufferView Input)
Result Extract(ContiguousByteBufferView Input)
Extractor()
Creates a new extractor instance.