24 #ifndef STATICLIB_COMPRESS_INFLATE_SOURCE_HPP
25 #define STATICLIB_COMPRESS_INFLATE_SOURCE_HPP
31 #include <type_traits>
35 #include "staticlib/config.hpp"
36 #include "staticlib/io.hpp"
47 template <
typename Source, std::
size_t buf_size = 4096>
56 std::array<char, buf_size> buf;
72 bool exhausted =
false;
83 z_stream* stream =
static_cast<z_stream*
> (std::malloc(
sizeof(z_stream)));
85 "Error creating inflate stream: 'malloc' failed"));
86 std::memset(stream, 0,
sizeof (z_stream));
87 auto err = inflateInit2(stream, -MAX_WBITS);
89 "Error initializing inflate stream: [" + ::zError(err) +
"]"));
94 if (
nullptr == strm)
return;
120 src(std::move(other.src)),
121 buf(std::move(other.buf)),
125 exhausted(other.exhausted) {
126 other.strm =
nullptr;
136 src = std::move(other.src);
137 buf = std::move(other.buf);
139 other.strm =
nullptr;
142 exhausted = other.exhausted;
152 std::streamsize
read(sl::io::span<char> span) {
156 avail = sl::io::read_all(src, {buf.data(), buf.size()});
160 strm->next_in =
reinterpret_cast<unsigned char*
> (buf.data() + pos);
161 strm->avail_in =
static_cast<uInt
> (avail);
162 strm->next_out =
reinterpret_cast<unsigned char*
> (span.data());
163 strm->avail_out =
static_cast<uInt
> (span.size());
165 auto err = ::inflate(strm, Z_FINISH);
166 if (Z_OK == err || Z_STREAM_END == err || Z_BUF_ERROR == err) {
167 std::streamsize
read = avail - strm->avail_in;
168 std::streamsize written = span.size_signed() - strm->avail_out;
169 size_t uread =
static_cast<size_t> (
read);
172 if (written > 0 || Z_STREAM_END != err) {
176 return std::char_traits<char>::eof();
178 +
"Inflate error: [" + ::zError(err) +
"]"));
180 return std::char_traits<char>::eof();
202 template <
typename Source,
203 class =
typename std::enable_if<!std::is_lvalue_reference<Source>::value>::type>
215 template <
typename Source>
218 sl::io::make_reference_source(source));