35 template <
typename State,
typename R, R (*Del)(State*)>
47 using in_char_type = std::remove_pointer_t<decltype(state_type::next_in)>;
48 using out_char_type = std::remove_pointer_t<decltype(state_type::next_out)>;
49 using deflate_state_type = std::unique_ptr<state_type, detail::end_deleter<state_type, int, ::BZ2_bzCompressEnd>>;
50 using inflate_state_type = std::unique_ptr<state_type, detail::end_deleter<state_type, int, ::BZ2_bzDecompressEnd>>;
55 auto ret = ::BZ2_bzCompress(x.get(), flush ? BZ_FLUSH : BZ_RUN);
57 return status_type::CAN_CONTINUE;
58 if (ret == BZ_FLUSH_OK)
59 return status_type::END;
60 return status_type::ERROR;
65 ::BZ2_bzDecompress(x.get());
66 return status_type::CAN_CONTINUE;
72 *state =
state_type{NULL, 0u, 0u, 0u, NULL, 0u, 0u, 0u, NULL, NULL, NULL, NULL};
73 ::BZ2_bzCompressInit(state.get(), 9, 0, 0);
80 *state =
state_type{NULL, 0u, 0u, 0u, NULL, 0u, 0u, 0u, NULL, NULL, NULL, NULL};
81 ::BZ2_bzDecompressInit(state.get(), 0, 0);
86 template <
int window = 15 + 16,
int compression = Z_DEFAULT_COMPRESSION>
89 using in_char_type = std::remove_pointer_t<decltype(state_type::next_in)>;
90 using out_char_type = std::remove_pointer_t<decltype(state_type::next_out)>;
91 using deflate_state_type = std::unique_ptr<state_type, detail::end_deleter<state_type, int, ::deflateEnd>>;
92 using inflate_state_type = std::unique_ptr<state_type, detail::end_deleter<state_type, int, ::inflateEnd>>;
97 auto ret =
::deflate(x.get(), flush ? Z_FINISH : Z_NO_FLUSH);
99 return status_type::CAN_CONTINUE;
100 if (ret == Z_STREAM_END)
101 return status_type::END;
102 return status_type::ERROR;
108 return status_type::CAN_CONTINUE;
114 *state =
state_type{Z_NULL, 0, 0, Z_NULL, 0, 0, NULL, NULL, Z_NULL, Z_NULL, Z_NULL, 0, 0ul, 0ul};
115 ::deflateInit(state.get(), compression);
122 *state =
state_type{Z_NULL, 0, 0, Z_NULL, 0, 0, NULL, NULL, Z_NULL, Z_NULL, Z_NULL, 0, 0ul, 0ul};
123 ::inflateInit2(state.get(), window);
128 template <u
int32_t flags = 0>
131 using in_char_type = std::remove_const_t<std::remove_pointer_t<decltype(state_type::next_in)>>;
132 using out_char_type = std::remove_pointer_t<decltype(state_type::next_out)>;
133 using deflate_state_type = std::unique_ptr<state_type, detail::end_deleter<state_type, void, ::lzma_end>>;
134 using inflate_state_type = std::unique_ptr<state_type, detail::end_deleter<state_type, void, ::lzma_end>>;
139 auto ret = ::lzma_code(x.get(), flush ? LZMA_FULL_FLUSH : LZMA_RUN);
141 return status_type::CAN_CONTINUE;
142 else if (ret == LZMA_STREAM_END)
143 return status_type::END;
145 return status_type::ERROR;
150 auto ret = ::lzma_code(x.get(), LZMA_RUN);
152 return status_type::CAN_CONTINUE;
153 else if (ret == LZMA_STREAM_END)
154 return status_type::END;
156 return status_type::ERROR;
162 *state = LZMA_STREAM_INIT;
163 auto ret = ::lzma_easy_encoder(state.get(), LZMA_PRESET_DEFAULT, LZMA_CHECK_CRC64);
164 assert(ret == LZMA_OK);
171 *state = LZMA_STREAM_INIT;
172 auto ret = ::lzma_stream_decoder(state.get(), std::numeric_limits<uint64_t>::max(), flags);
173 assert(ret == LZMA_OK);
179 template <
typename Tag,
typename StreamType = std::ifstream>
181 template <
typename IStrm>
182 class inf_streambuf :
public std::basic_streambuf<typename IStrm::char_type, std::char_traits<typename IStrm::char_type>>
185 using base_type = std::basic_streambuf<typename IStrm::char_type, std::char_traits<typename IStrm::char_type>>;
191 constexpr
static std::size_t
CHUNK = (1 << 16);
193 std::array<strm_in_buf_type, CHUNK>
in_buf;
195 typename Tag::inflate_state_type
strm = Tag::new_inflate_state();
196 typename std::add_pointer<IStrm>::type
src;
202 std::size_t
bytes_read()
const {
return strm->total_out - (this->egptr() - this->gptr()); }
209 std::unique_ptr<inf_streambuf<StreamType>>
buffer = std::make_unique<inf_streambuf<StreamType>>(
underlying.get());
215 std::istream inflated{
buffer.get()};
216 inflated.read(s, count);
218 eof_ = inflated.eof();
229 template <
typename T,
typename S>
230 template <
typename I>
233 std::array<
strm_out_buf_type, std::tuple_size<decltype(out_buf)>::value> uns_out_buf;
235 strm->avail_out = uns_out_buf.size();
236 strm->next_out = uns_out_buf.data();
239 if (strm->avail_in == 0) {
242 this->setg(this->out_buf.data(), this->out_buf.data(), this->out_buf.data());
243 return base_type::underflow();
247 std::array<
char_type, std::tuple_size<decltype(in_buf)>::value> sig_in_buf;
248 src->read(sig_in_buf.data(), sig_in_buf.size());
249 auto bytes_read = src->gcount();
250 assert(bytes_read >= 0);
251 std::memcpy(in_buf.data(), sig_in_buf.data(),
static_cast<std::size_t
>(src->gcount()));
254 strm->avail_in =
static_cast<unsigned>(src->gcount());
255 strm->next_in = in_buf.data();
258 if (strm->avail_in == 0) {
259 this->setg(this->out_buf.data(), this->out_buf.data(), this->out_buf.data());
260 return base_type::underflow();
265 auto result = T::inflate(strm);
266 assert(result == T::status_type::CAN_CONTINUE || result == T::status_type::END);
269 while (strm->avail_out == uns_out_buf.size());
272 std::memcpy(this->out_buf.data(), uns_out_buf.data(), uns_out_buf.size() - strm->avail_out);
274 auto bytes_remaining = std::size(uns_out_buf) - strm->avail_out;
275 assert(bytes_remaining <= std::numeric_limits<std::make_signed_t<decltype(bytes_remaining)>>::max());
276 this->setg(this->out_buf.data(), this->out_buf.data(),
277 std::next(this->out_buf.data(),
static_cast<std::make_signed_t<decltype(bytes_remaining)
>>(bytes_remaining)));
278 return base_type::traits_type::to_int_type(this->out_buf.front());
Definition: inf_stream.h:183
std::size_t bytes_read() const
Definition: inf_stream.h:202
int_type underflow() override
Definition: inf_stream.h:231
inf_streambuf(IStrm *in)
Definition: inf_stream.h:199
typename base_type::char_type char_type
Definition: inf_stream.h:187
inf_streambuf(Tag, IStrm *in)
Definition: inf_stream.h:200
typename Tag::out_char_type strm_out_buf_type
Definition: inf_stream.h:189
typename Tag::in_char_type strm_in_buf_type
Definition: inf_stream.h:188
Tag::inflate_state_type strm
Definition: inf_stream.h:195
std::array< char_type, CHUNK > out_buf
Definition: inf_stream.h:194
constexpr static std::size_t CHUNK
Definition: inf_stream.h:191
typename base_type::int_type int_type
Definition: inf_stream.h:186
std::basic_streambuf< typename IStrm::char_type, std::char_traits< typename IStrm::char_type > > base_type
Definition: inf_stream.h:185
std::add_pointer< IStrm >::type src
Definition: inf_stream.h:196
std::array< strm_in_buf_type, CHUNK > in_buf
Definition: inf_stream.h:193
Definition: champsim.h:24
Definition: inf_stream.h:180
inf_istream(StreamType &&str)
Definition: inf_stream.h:226
inf_istream(std::string s)
Definition: inf_stream.h:225
bool eof() const
Definition: inf_stream.h:222
std::unique_ptr< StreamType > underlying
Definition: inf_stream.h:208
std::streamsize gcount() const
Definition: inf_stream.h:223
std::streamsize gcount_
Definition: inf_stream.h:210
bool eof_
Definition: inf_stream.h:211
std::unique_ptr< inf_streambuf< StreamType > > buffer
Definition: inf_stream.h:209
inf_istream & read(char *s, std::streamsize count)
Definition: inf_stream.h:213