2 #include "zipios++/zipios-config.h"
4 #include "zipios++/meta-iostreams.h"
21 _zs_initialized ( false ),
23 _invec ( _invecsize ),
25 _outvec ( _outvecsize )
37 if ( user_init && ! init() )
38 cerr <<
"DeflateOutputStreambuf::reset() failed!\n" ;
51 bool DeflateOutputStreambuf::init(
int comp_level ) {
52 static const int default_mem_level = 8 ;
56 _zs.next_in =
reinterpret_cast< unsigned char *
>( &( _invec[ 0 ] ) ) ;
59 _zs.next_out =
reinterpret_cast< unsigned char *
>( &( _outvec[ 0 ] ) ) ;
60 _zs.avail_out = _outvecsize ;
63 if( _zs_initialized ) {
65 err = deflateReset( &_zs ) ;
68 err = deflateInit2( &_zs, comp_level, Z_DEFLATED, -MAX_WBITS,
69 default_mem_level, Z_DEFAULT_STRATEGY ) ;
72 _zs_initialized = true ;
76 setp( &( _invec[ 0 ] ), &( _invec[ 0 ] ) + _invecsize ) ;
78 _crc32 = crc32( 0, Z_NULL, 0 ) ;
79 _overflown_bytes = 0 ;
88 bool DeflateOutputStreambuf::closeStream() {
90 if( _zs_initialized ) {
92 err = deflateEnd( &_zs ) ;
93 _zs_initialized = false ;
99 cerr <<
"DeflateOutputStreambuf::closeStream(): deflateEnd failed" ;
101 cerr <<
": " << zError( err ) ;
109 int DeflateOutputStreambuf::overflow(
int c ) {
110 _zs.avail_in = pptr() - pbase() ;
111 _zs.next_in =
reinterpret_cast< unsigned char *
>( &( _invec[ 0 ] ) ) ;
113 _crc32 = crc32( _crc32, _zs.next_in, _zs.avail_in ) ;
114 _overflown_bytes += _zs.avail_in ;
116 _zs.next_out =
reinterpret_cast< unsigned char *
>( &( _outvec[ 0 ] ) ) ;
117 _zs.avail_out = _outvecsize ;
121 while ( ( _zs.avail_in > 0 || _zs.avail_out == 0 ) && err == Z_OK ) {
122 if ( _zs.avail_out == 0 )
125 err = deflate( &_zs, Z_NO_FLUSH ) ;
131 setp( &( _invec[ 0 ] ), &( _invec[ 0 ] ) + _invecsize ) ;
133 if( err != Z_OK && err != Z_STREAM_END ) {
134 #if defined (HAVE_STD_IOSTREAM) && defined (USE_STD_IOSTREAM)
136 OutputStringStream msgs ;
137 msgs <<
"Deflation failed" ;
139 msgs <<
": " << zError( err ) ;
141 throw IOException( msgs.str() ) ;
143 cerr <<
"Deflation failed\n" ;
155 int DeflateOutputStreambuf::sync() {
163 int deflated_bytes = _outvecsize - _zs.avail_out ;
164 int bc = _outbuf->sputn( &( _outvec[ 0 ] ), deflated_bytes ) ;
166 _zs.next_out =
reinterpret_cast< unsigned char *
>( &( _outvec[ 0 ] ) ) ;
167 _zs.avail_out = _outvecsize ;
169 return deflated_bytes == bc ;
176 _zs.next_out =
reinterpret_cast< unsigned char *
>( &( _outvec[ 0 ] ) ) ;
177 _zs.avail_out = _outvecsize ;
182 while ( err == Z_OK ) {
183 if ( _zs.avail_out == 0 )
186 err = deflate( &_zs, Z_FINISH ) ;
191 if ( err != Z_STREAM_END ) {
192 cerr <<
"DeflateOutputStreambuf::endDeflation(): deflation failed:\n" ;
194 cerr <<
": " << zError( err ) ;