1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
#include "zipios++/zipios-config.h"
#include "zipios++/meta-iostreams.h"
#include <zlib.h>
#include "zipios++/fcollexceptions.h"
#include "zipios++/deflateoutputstreambuf.h"
#include "outputstringstream.h"
namespace zipios {
using std::cerr ;
using std::endl ;
DeflateOutputStreambuf::DeflateOutputStreambuf( streambuf *outbuf, bool user_init,
bool del_outbuf )
: FilterOutputStreambuf( outbuf, del_outbuf ),
_zs_initialized ( false ),
_invecsize ( 1000 ),
_invec ( _invecsize ),
_outvecsize ( 1000 ),
_outvec ( _outvecsize )
{
// NOTICE: It is important that this constructor and the methods it
// calls doesn't do anything with the output streambuf _outbuf The
// reason is that this class can be subclassed, and the subclass
// should get a chance to write to the buffer first
// zlib init:
_zs.zalloc = Z_NULL ;
_zs.zfree = Z_NULL ;
_zs.opaque = Z_NULL ;
if ( user_init && ! init() )
cerr << "DeflateOutputStreambuf::reset() failed!\n" ; // FIXME: throw something
}
DeflateOutputStreambuf::~DeflateOutputStreambuf() {
closeStream() ;
}
// This method is called in the constructor, so it must not write
// anything to the output streambuf _outbuf (see notice in
// constructor)
bool DeflateOutputStreambuf::init( int comp_level ) {
static const int default_mem_level = 8 ;
// _zs.next_in and avail_in must be set according to
// zlib.h (inline doc).
_zs.next_in = reinterpret_cast< unsigned char * >( &( _invec[ 0 ] ) ) ;
_zs.avail_in = 0 ;
_zs.next_out = reinterpret_cast< unsigned char * >( &( _outvec[ 0 ] ) ) ;
_zs.avail_out = _outvecsize ;
int err ;
if( _zs_initialized ) { // just reset it
endDeflation() ;
err = deflateReset( &_zs ) ;
// FIXME: bug, for deflateReset we do not update the compression level
} else { // init it
err = deflateInit2( &_zs, comp_level, Z_DEFLATED, -MAX_WBITS,
default_mem_level, Z_DEFAULT_STRATEGY ) ;
/* windowBits is passed < 0 to tell that no zlib header should be
written. */
_zs_initialized = true ;
}
// streambuf init:
setp( &( _invec[ 0 ] ), &( _invec[ 0 ] ) + _invecsize ) ;
_crc32 = crc32( 0, Z_NULL, 0 ) ;
_overflown_bytes = 0 ;
if ( err == Z_OK )
return true ;
else
return false ;
}
bool DeflateOutputStreambuf::closeStream() {
int err = Z_OK ;
if( _zs_initialized ) {
endDeflation() ;
err = deflateEnd( &_zs ) ;
_zs_initialized = false ;
}
if ( err == Z_OK )
return true ;
else {
cerr << "DeflateOutputStreambuf::closeStream(): deflateEnd failed" ;
#ifdef HAVE_ZERROR
cerr << ": " << zError( err ) ;
#endif
cerr << endl ;
return false ;
}
}
int DeflateOutputStreambuf::overflow( int c ) {
_zs.avail_in = pptr() - pbase() ;
_zs.next_in = reinterpret_cast< unsigned char * >( &( _invec[ 0 ] ) ) ;
_crc32 = crc32( _crc32, _zs.next_in, _zs.avail_in ) ; // update crc32
_overflown_bytes += _zs.avail_in ;
_zs.next_out = reinterpret_cast< unsigned char * >( &( _outvec[ 0 ] ) ) ;
_zs.avail_out = _outvecsize ;
// Deflate until _invec is empty.
int err = Z_OK ;
while ( ( _zs.avail_in > 0 || _zs.avail_out == 0 ) && err == Z_OK ) {
if ( _zs.avail_out == 0 )
flushOutvec() ;
err = deflate( &_zs, Z_NO_FLUSH ) ;
}
flushOutvec() ;
// Update 'put' pointers
setp( &( _invec[ 0 ] ), &( _invec[ 0 ] ) + _invecsize ) ;
if( err != Z_OK && err != Z_STREAM_END ) {
#if defined (HAVE_STD_IOSTREAM) && defined (USE_STD_IOSTREAM)
// Throw an exception to make istream set badbit
OutputStringStream msgs ;
msgs << "Deflation failed" ;
#ifdef HAVE_ZERROR
msgs << ": " << zError( err ) ;
#endif
throw IOException( msgs.str() ) ;
#endif
cerr << "Deflation failed\n" ;
return EOF ;
}
if ( c != EOF ) {
*pptr() = c ;
pbump( 1 ) ;
}
return 0 ;
}
int DeflateOutputStreambuf::sync() {
// FIXME: Do something
// return overflow() ;
return 0 ;
}
bool DeflateOutputStreambuf::flushOutvec() {
int deflated_bytes = _outvecsize - _zs.avail_out ;
int bc = _outbuf->sputn( &( _outvec[ 0 ] ), deflated_bytes ) ;
_zs.next_out = reinterpret_cast< unsigned char * >( &( _outvec[ 0 ] ) ) ;
_zs.avail_out = _outvecsize ;
return deflated_bytes == bc ;
}
void DeflateOutputStreambuf::endDeflation() {
overflow() ;
_zs.next_out = reinterpret_cast< unsigned char * >( &( _outvec[ 0 ] ) ) ;
_zs.avail_out = _outvecsize ;
// Deflate until _invec is empty.
int err = Z_OK ;
while ( err == Z_OK ) {
if ( _zs.avail_out == 0 )
flushOutvec() ;
err = deflate( &_zs, Z_FINISH ) ;
}
flushOutvec() ;
if ( err != Z_STREAM_END ) {
cerr << "DeflateOutputStreambuf::endDeflation(): deflation failed:\n" ;
#ifdef HAVE_ZERROR
cerr << ": " << zError( err ) ;
#endif
cerr << endl ;
}
}
} // namespace
/** \file
Implementation of DeflateOutputStreambuf.
*/
/*
Zipios++ - a small C++ library that provides easy access to .zip files.
Copyright (C) 2000 Thomas Sndergaard
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
|