From 9cb2695ad318dcda83a353b03c21b4fd71d0f9d6 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 24 Oct 2011 16:30:39 +0000 Subject: Made the encoder state opaque to the caller in Deflate and BZip2 to match Lzma. That means that when you use Bu::Deflate, Bu::Bzip2, or Bu::Lzma you don't get any of the respective libraries' header files. --- src/bzip2.cpp | 95 ++++++++++++++++++++++++-------------------- src/bzip2.h | 3 +- src/deflate.cpp | 120 ++++++++++++++++++++++++++++++-------------------------- src/deflate.h | 3 +- 4 files changed, 120 insertions(+), 101 deletions(-) diff --git a/src/bzip2.cpp b/src/bzip2.cpp index 0ff5444..ca007b0 100644 --- a/src/bzip2.cpp +++ b/src/bzip2.cpp @@ -8,10 +8,15 @@ #include "bu/bzip2.h" #include "bu/trace.h" +#include + +#define pState ((bz_stream *)prState) + using namespace Bu; Bu::BZip2::BZip2( Bu::Stream &rNext, int nCompression ) : Bu::Filter( rNext ), + prState( NULL ), nCompression( nCompression ), sTotalOut( 0 ) { @@ -28,10 +33,12 @@ Bu::BZip2::~BZip2() void Bu::BZip2::start() { TRACE(); - bzState.state = NULL; - bzState.bzalloc = NULL; - bzState.bzfree = NULL; - bzState.opaque = NULL; + + prState = new bz_stream; + pState->state = NULL; + pState->bzalloc = NULL; + pState->bzfree = NULL; + pState->opaque = NULL; nBufSize = 64*1024; pBuf = new char[nBufSize]; @@ -40,13 +47,15 @@ void Bu::BZip2::start() Bu::size Bu::BZip2::stop() { TRACE(); - if( bzState.state ) + if( pState->state ) { if( bReading ) { - BZ2_bzDecompressEnd( &bzState ); + BZ2_bzDecompressEnd( pState ); delete[] pBuf; pBuf = NULL; + delete pState; + prState = NULL; return 0; } else @@ -54,21 +63,23 @@ Bu::size Bu::BZip2::stop() // Bu::size sTotal = 0; for(;;) { - bzState.next_in = NULL; - bzState.avail_in = 0; - bzState.avail_out = nBufSize; - bzState.next_out = pBuf; - int res = BZ2_bzCompress( &bzState, BZ_FINISH ); - if( bzState.avail_out < nBufSize ) + pState->next_in = NULL; + pState->avail_in = 0; + pState->avail_out = nBufSize; + pState->next_out = pBuf; + int res = BZ2_bzCompress( pState, BZ_FINISH ); + if( pState->avail_out < nBufSize ) { - sTotalOut += rNext.write( pBuf, nBufSize-bzState.avail_out ); + sTotalOut += rNext.write( pBuf, nBufSize-pState->avail_out ); } if( res == BZ_STREAM_END ) break; } - BZ2_bzCompressEnd( &bzState ); + BZ2_bzCompressEnd( pState ); delete[] pBuf; pBuf = NULL; + delete pState; + prState = NULL; return sTotalOut; } } @@ -122,42 +133,42 @@ void Bu::BZip2::bzError( int code ) Bu::size Bu::BZip2::read( void *pData, Bu::size nBytes ) { TRACE( pData, nBytes ); - if( !bzState.state ) + if( !pState->state ) { bReading = true; - BZ2_bzDecompressInit( &bzState, 0, 0 ); - bzState.next_in = pBuf; - bzState.avail_in = 0; + BZ2_bzDecompressInit( pState, 0, 0 ); + pState->next_in = pBuf; + pState->avail_in = 0; } if( bReading == false ) throw ExceptionBase("This bzip2 filter is in writing mode, you can't read."); int nRead = 0; - int nReadTotal = bzState.total_out_lo32; - bzState.next_out = (char *)pData; - bzState.avail_out = nBytes; + int nReadTotal = pState->total_out_lo32; + pState->next_out = (char *)pData; + pState->avail_out = nBytes; for(;;) { - int ret = BZ2_bzDecompress( &bzState ); + int ret = BZ2_bzDecompress( pState ); - nReadTotal += nRead-bzState.avail_out; + nReadTotal += nRead-pState->avail_out; if( ret == BZ_STREAM_END ) { - if( bzState.avail_in > 0 ) + if( pState->avail_in > 0 ) { if( rNext.isSeekable() ) { - rNext.seek( -bzState.avail_in ); + rNext.seek( -pState->avail_in ); } } - return nBytes-bzState.avail_out; + return nBytes-pState->avail_out; } bzError( ret ); - if( bzState.avail_out ) + if( pState->avail_out ) { - if( bzState.avail_in == 0 ) + if( pState->avail_in == 0 ) { nRead = rNext.read( pBuf, nBufSize ); if( nRead == 0 && rNext.isEos() ) @@ -165,13 +176,13 @@ Bu::size Bu::BZip2::read( void *pData, Bu::size nBytes ) throw Bu::ExceptionBase("Premature end of underlying " "stream found reading bzip2 stream."); } - bzState.next_in = pBuf; - bzState.avail_in = nRead; + pState->next_in = pBuf; + pState->avail_in = nRead; } } else { - return nBytes-bzState.avail_out; + return nBytes-pState->avail_out; } } return 0; @@ -180,29 +191,29 @@ Bu::size Bu::BZip2::read( void *pData, Bu::size nBytes ) Bu::size Bu::BZip2::write( const void *pData, Bu::size nBytes ) { TRACE( pData, nBytes ); - if( !bzState.state ) + if( !pState->state ) { bReading = false; - BZ2_bzCompressInit( &bzState, nCompression, 0, 30 ); + BZ2_bzCompressInit( pState, nCompression, 0, 30 ); } if( bReading == true ) throw ExceptionBase("This bzip2 filter is in reading mode, you can't write."); // Bu::size sTotalOut = 0; - bzState.next_in = (char *)pData; - bzState.avail_in = nBytes; + pState->next_in = (char *)pData; + pState->avail_in = nBytes; for(;;) { - bzState.avail_out = nBufSize; - bzState.next_out = pBuf; + pState->avail_out = nBufSize; + pState->next_out = pBuf; - bzError( BZ2_bzCompress( &bzState, BZ_RUN ) ); + bzError( BZ2_bzCompress( pState, BZ_RUN ) ); - if( bzState.avail_out < nBufSize ) + if( pState->avail_out < nBufSize ) { - sTotalOut += rNext.write( pBuf, nBufSize-bzState.avail_out ); + sTotalOut += rNext.write( pBuf, nBufSize-pState->avail_out ); } - if( bzState.avail_in == 0 ) + if( pState->avail_in == 0 ) break; } @@ -212,7 +223,7 @@ Bu::size Bu::BZip2::write( const void *pData, Bu::size nBytes ) bool Bu::BZip2::isOpen() { TRACE(); - return (bzState.state != NULL); + return (pState->state != NULL); } Bu::size Bu::BZip2::getCompressedSize() diff --git a/src/bzip2.h b/src/bzip2.h index 6da3dff..0b5140d 100644 --- a/src/bzip2.h +++ b/src/bzip2.h @@ -9,7 +9,6 @@ #define BU_BZIP2_H #include -#include #include "bu/filter.h" @@ -36,7 +35,7 @@ namespace Bu private: void bzError( int code ); - bz_stream bzState; + void *prState; bool bReading; int nCompression; char *pBuf; diff --git a/src/deflate.cpp b/src/deflate.cpp index aec2a18..10a9c5f 100644 --- a/src/deflate.cpp +++ b/src/deflate.cpp @@ -8,10 +8,15 @@ #include "bu/deflate.h" #include "bu/trace.h" +#include + +#define pState ((z_stream *)prState) + using namespace Bu; Bu::Deflate::Deflate( Bu::Stream &rNext, int nCompression, Format eFmt ) : Bu::Filter( rNext ), + prState( NULL ), nCompression( nCompression ), sTotalOut( 0 ), eFmt( eFmt ), @@ -30,10 +35,11 @@ Bu::Deflate::~Deflate() void Bu::Deflate::start() { TRACE(); - zState.zalloc = NULL; - zState.zfree = NULL; - zState.opaque = NULL; - zState.state = NULL; + prState = new z_stream; + pState->zalloc = NULL; + pState->zfree = NULL; + pState->opaque = NULL; + pState->state = NULL; nBufSize = 64*1024; pBuf = new char[nBufSize]; @@ -42,34 +48,38 @@ void Bu::Deflate::start() Bu::size Bu::Deflate::stop() { TRACE(); - if( zState.state ) + if( pState && pState->state ) { if( bReading ) { - inflateEnd( &zState ); + inflateEnd( pState ); delete[] pBuf; pBuf = NULL; + delete pState; + prState = NULL; return 0; } else { for(;;) { - zState.next_in = NULL; - zState.avail_in = 0; - zState.avail_out = nBufSize; - zState.next_out = (Bytef *)pBuf; - int res = deflate( &zState, Z_FINISH ); - if( zState.avail_out < nBufSize ) + pState->next_in = NULL; + pState->avail_in = 0; + pState->avail_out = nBufSize; + pState->next_out = (Bytef *)pBuf; + int res = deflate( pState, Z_FINISH ); + if( pState->avail_out < nBufSize ) { - sTotalOut += rNext.write( pBuf, nBufSize-zState.avail_out ); + sTotalOut += rNext.write( pBuf, nBufSize-pState->avail_out ); } if( res == Z_STREAM_END ) break; } - deflateEnd( &zState ); + deflateEnd( pState ); delete[] pBuf; pBuf = NULL; + delete pState; + prState = NULL; return sTotalOut; } } @@ -87,25 +97,25 @@ void Bu::Deflate::zError( int code ) return; case Z_ERRNO: - throw ExceptionBase("Deflate: Errno - %s", zState.msg ); + throw ExceptionBase("Deflate: Errno - %s", pState->msg ); case Z_STREAM_ERROR: - throw ExceptionBase("Deflate: Stream Error - %s", zState.msg ); + throw ExceptionBase("Deflate: Stream Error - %s", pState->msg ); case Z_DATA_ERROR: - throw ExceptionBase("Deflate: Data Error - %s", zState.msg ); + throw ExceptionBase("Deflate: Data Error - %s", pState->msg ); case Z_MEM_ERROR: - throw ExceptionBase("Deflate: Mem Error - %s", zState.msg ); + throw ExceptionBase("Deflate: Mem Error - %s", pState->msg ); case Z_BUF_ERROR: - throw ExceptionBase("Deflate: Buf Error - %s", zState.msg ); + throw ExceptionBase("Deflate: Buf Error - %s", pState->msg ); case Z_VERSION_ERROR: - throw ExceptionBase("Deflate: Version Error - %s", zState.msg ); + throw ExceptionBase("Deflate: Version Error - %s", pState->msg ); default: - throw ExceptionBase("Deflate: Unknown error encountered - %s.", zState.msg ); + throw ExceptionBase("Deflate: Unknown error encountered - %s.", pState->msg ); } } @@ -113,55 +123,55 @@ void Bu::Deflate::zError( int code ) Bu::size Bu::Deflate::read( void *pData, Bu::size nBytes ) { TRACE( pData, nBytes ); - if( !zState.state ) + if( !pState->state ) { bReading = true; if( eFmt&AutoDetect ) - inflateInit2( &zState, 32+15 ); // Auto-detect, large window + inflateInit2( pState, 32+15 ); // Auto-detect, large window else if( eFmt == Raw ) - inflateInit2( &zState, -15 ); // Raw + inflateInit2( pState, -15 ); // Raw else if( eFmt == Zlib ) - inflateInit2( &zState, 15 ); // Zlib + inflateInit2( pState, 15 ); // Zlib else if( eFmt == Gzip ) - inflateInit2( &zState, 16+15 ); // GZip + inflateInit2( pState, 16+15 ); // GZip else throw Bu::ExceptionBase("Format mode for deflate read."); - zState.next_in = (Bytef *)pBuf; - zState.avail_in = 0; + pState->next_in = (Bytef *)pBuf; + pState->avail_in = 0; } if( bReading == false ) throw ExceptionBase("This deflate filter is in writing mode, you can't read."); int nRead = 0; - int nReadTotal = zState.total_out; - zState.next_out = (Bytef *)pData; - zState.avail_out = nBytes; + int nReadTotal = pState->total_out; + pState->next_out = (Bytef *)pData; + pState->avail_out = nBytes; for(;;) { - int ret = inflate( &zState, Z_NO_FLUSH ); + int ret = inflate( pState, Z_NO_FLUSH ); printf("inflate returned %d; avail in=%d, out=%d\n", ret, - zState.avail_in, zState.avail_out ); + pState->avail_in, pState->avail_out ); - nReadTotal += nRead-zState.avail_out; + nReadTotal += nRead-pState->avail_out; if( ret == Z_STREAM_END ) { bEos = true; - if( zState.avail_in > 0 ) + if( pState->avail_in > 0 ) { if( rNext.isSeekable() ) { - rNext.seek( -zState.avail_in ); + rNext.seek( -pState->avail_in ); } } - return nBytes-zState.avail_out; + return nBytes-pState->avail_out; } if( ret != Z_BUF_ERROR ) zError( ret ); - if( zState.avail_out ) + if( pState->avail_out ) { - if( zState.avail_in == 0 ) + if( pState->avail_in == 0 ) { nRead = rNext.read( pBuf, nBufSize ); if( nRead == 0 && rNext.isEos() ) @@ -169,13 +179,13 @@ Bu::size Bu::Deflate::read( void *pData, Bu::size nBytes ) throw Bu::ExceptionBase("Premature end of underlying " "stream found reading deflate stream."); } - zState.next_in = (Bytef *)pBuf; - zState.avail_in = nRead; + pState->next_in = (Bytef *)pBuf; + pState->avail_in = nRead; } } else { - return nBytes-zState.avail_out; + return nBytes-pState->avail_out; } } return 0; @@ -184,18 +194,18 @@ Bu::size Bu::Deflate::read( void *pData, Bu::size nBytes ) Bu::size Bu::Deflate::write( const void *pData, Bu::size nBytes ) { TRACE( pData, nBytes ); - if( !zState.state ) + if( !pState->state ) { bReading = false; int iFmt = eFmt&Gzip; if( iFmt == Raw ) - deflateInit2( &zState, nCompression, Z_DEFLATED, -15, 9, + deflateInit2( pState, nCompression, Z_DEFLATED, -15, 9, Z_DEFAULT_STRATEGY ); else if( iFmt == Zlib ) - deflateInit2( &zState, nCompression, Z_DEFLATED, 15, 9, + deflateInit2( pState, nCompression, Z_DEFLATED, 15, 9, Z_DEFAULT_STRATEGY ); else if( iFmt == Gzip ) - deflateInit2( &zState, nCompression, Z_DEFLATED, 16+15, 9, + deflateInit2( pState, nCompression, Z_DEFLATED, 16+15, 9, Z_DEFAULT_STRATEGY ); else throw Bu::ExceptionBase("Invalid format for deflate."); @@ -203,20 +213,20 @@ Bu::size Bu::Deflate::write( const void *pData, Bu::size nBytes ) if( bReading == true ) throw ExceptionBase("This deflate filter is in reading mode, you can't write."); - zState.next_in = (Bytef *)pData; - zState.avail_in = nBytes; + pState->next_in = (Bytef *)pData; + pState->avail_in = nBytes; for(;;) { - zState.avail_out = nBufSize; - zState.next_out = (Bytef *)pBuf; + pState->avail_out = nBufSize; + pState->next_out = (Bytef *)pBuf; - zError( deflate( &zState, Z_NO_FLUSH ) ); + zError( deflate( pState, Z_NO_FLUSH ) ); - if( zState.avail_out < nBufSize ) + if( pState->avail_out < nBufSize ) { - sTotalOut += rNext.write( pBuf, nBufSize-zState.avail_out ); + sTotalOut += rNext.write( pBuf, nBufSize-pState->avail_out ); } - if( zState.avail_in == 0 ) + if( pState->avail_in == 0 ) break; } @@ -226,7 +236,7 @@ Bu::size Bu::Deflate::write( const void *pData, Bu::size nBytes ) bool Bu::Deflate::isOpen() { TRACE(); - return (zState.state != NULL); + return (pState != NULL && pState->state != NULL); } bool Bu::Deflate::isEos() diff --git a/src/deflate.h b/src/deflate.h index 8ce283b..20d609a 100644 --- a/src/deflate.h +++ b/src/deflate.h @@ -9,7 +9,6 @@ #define BU_DEFLATE_H #include -#include #include "bu/filter.h" @@ -49,7 +48,7 @@ namespace Bu private: void zError( int code ); - z_stream zState; + void *prState; bool bReading; int nCompression; char *pBuf; -- cgit v1.2.3