45 # if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__) 50 #endif // GENESIS_ZLIB 64 struct GzipInputSource::ZlibData
71 char in_buf_[ BlockLength ];
81 struct GzipInputSource::ZlibData
86 #endif // GENESIS_ZLIB 95 std::shared_ptr<BaseInputSource> input_source,
98 : input_source_( input_source )
99 , format_name_( translate_format_( format ))
102 []( ZlibData *impl ) {
delete impl; }
106 auto& z_stream_ = zlib_data_->z_stream_;
107 z_stream_.zalloc = Z_NULL;
108 z_stream_.zfree = Z_NULL;
109 z_stream_.opaque = Z_NULL;
110 z_stream_.avail_in = 0;
111 z_stream_.next_in = Z_NULL;
114 auto ret = inflateInit2( &z_stream_, get_format_( format ));
124 inflateEnd( &zlib_data_->z_stream_ );
127 size_t GzipInputSource::read_(
char* buffer,
size_t size )
130 auto& z_stream_ = zlib_data_->z_stream_;
131 auto& in_buf_ = zlib_data_->in_buf_;
132 auto& in_pos_ = zlib_data_->in_pos_;
133 auto& in_end_ = zlib_data_->in_end_;
138 size_t const out_end = size;
139 char* out_buf = buffer;
142 while( out_pos < out_end ) {
146 if( in_pos_ >= in_end_ ) {
148 in_end_ = input_source_->read( in_buf_, BlockLength );
150 assert( in_end_ >= in_pos_ );
151 assert( out_end >= out_pos );
157 z_stream_.avail_in =
static_cast<unsigned int>( in_end_ - in_pos_ );
158 z_stream_.next_in =
reinterpret_cast<Bytef*
>( in_buf_ ) + in_pos_;
161 z_stream_.avail_out =
static_cast<unsigned int>( out_end - out_pos );
162 z_stream_.next_out =
reinterpret_cast<Bytef*
>( out_buf ) + out_pos;
165 auto ret = inflate( &z_stream_, Z_NO_FLUSH );
168 assert( ret != Z_STREAM_ERROR );
169 if( ret == Z_NEED_DICT ) {
179 in_pos_ = in_end_ - z_stream_.avail_in;
180 out_pos = out_end - z_stream_.avail_out;
183 if( ret == Z_STREAM_END ) {
187 assert( in_pos_ == in_end_ );
195 assert( out_pos == out_end || in_pos_ == in_end_ );
201 std::string GzipInputSource::source_string_()
const 204 auto const bn =
file_basename( input_source_->source_string() );
208 if( ex ==
"gz" || ex ==
"gzip" || ex ==
"zlib" ) {
211 return input_source_->source_string();
222 return MAX_WBITS | 32;
224 return MAX_WBITS | 16;
256 #else // GENESIS_ZLIB 272 std::shared_ptr<BaseInputSource>,
277 , zlib_data_(
nullptr, []( ZlibData* ){} )
280 throw std::runtime_error(
"zlib: Genesis was not compiled with zlib support." );
283 size_t GzipInputSource::read_(
char*,
size_t )
288 std::string GzipInputSource::source_string_()
const 303 #endif // GENESIS_ZLIB
std::string file_extension(std::string const &filename)
Return the extension name of a file.
std::string file_filename(std::string const &filename)
Remove extension if present.
Container namespace for all symbols of genesis in order to keep them separate when used as a library...
std::string file_basename(std::string const &filename)
Remove directory name from file name if present.
Provides functions for accessing the file system.
Exception class thrown by failed gzip/zlib operations.