Replace ZCodec::mbInit with sane enum
...and document how the member functions are supposed to be called from client code. Change-Id: Ia4847945e4a361c43a0ed001e3e78e901c9abcad
This commit is contained in:
parent
d4af55670f
commit
1c92fbf4fd
@ -27,10 +27,16 @@
|
|||||||
|
|
||||||
class SvStream;
|
class SvStream;
|
||||||
|
|
||||||
|
// The overall client call protocol is one of:
|
||||||
|
// * BeginCompression, Compress, EndCompression
|
||||||
|
// * BeginCompression, Decompress, EndCompression
|
||||||
|
// * BeginCompression, Write*, EndCompression
|
||||||
|
// * BeginCompression, Read*, EndCompression
|
||||||
|
// * BeginCompression, ReadAsynchron*, EndCompression
|
||||||
class TOOLS_DLLPUBLIC ZCodec
|
class TOOLS_DLLPUBLIC ZCodec
|
||||||
{
|
{
|
||||||
private:
|
enum State { STATE_INIT, STATE_DECOMPRESS, STATE_COMPRESS };
|
||||||
sal_uIntPtr mbInit;
|
State meState;
|
||||||
bool mbStatus;
|
bool mbStatus;
|
||||||
bool mbFinish;
|
bool mbFinish;
|
||||||
SvStream* mpIStm;
|
SvStream* mpIStm;
|
||||||
|
@ -38,7 +38,7 @@
|
|||||||
static const int gz_magic[2] = { 0x1f, 0x8b }; /* gzip magic header */
|
static const int gz_magic[2] = { 0x1f, 0x8b }; /* gzip magic header */
|
||||||
|
|
||||||
ZCodec::ZCodec( sal_uIntPtr nInBufSize, sal_uIntPtr nOutBufSize )
|
ZCodec::ZCodec( sal_uIntPtr nInBufSize, sal_uIntPtr nOutBufSize )
|
||||||
: mbInit(0)
|
: meState(STATE_INIT)
|
||||||
, mbStatus(false)
|
, mbStatus(false)
|
||||||
, mbFinish(false)
|
, mbFinish(false)
|
||||||
, mpIStm(NULL)
|
, mpIStm(NULL)
|
||||||
@ -63,7 +63,7 @@ ZCodec::~ZCodec()
|
|||||||
|
|
||||||
void ZCodec::BeginCompression( int nCompressLevel, bool updateCrc, bool gzLib )
|
void ZCodec::BeginCompression( int nCompressLevel, bool updateCrc, bool gzLib )
|
||||||
{
|
{
|
||||||
mbInit = 0;
|
assert(meState == STATE_INIT);
|
||||||
mbStatus = true;
|
mbStatus = true;
|
||||||
mbFinish = false;
|
mbFinish = false;
|
||||||
mpIStm = mpOStm = NULL;
|
mpIStm = mpOStm = NULL;
|
||||||
@ -83,9 +83,9 @@ long ZCodec::EndCompression()
|
|||||||
{
|
{
|
||||||
long retvalue = 0;
|
long retvalue = 0;
|
||||||
|
|
||||||
if ( mbInit != 0 )
|
if (meState != STATE_INIT)
|
||||||
{
|
{
|
||||||
if ( mbInit & 2 ) // 1->decompress, 3->compress
|
if (meState == STATE_COMPRESS)
|
||||||
{
|
{
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
@ -105,6 +105,7 @@ long ZCodec::EndCompression()
|
|||||||
}
|
}
|
||||||
delete[] mpOutBuf;
|
delete[] mpOutBuf;
|
||||||
delete[] mpInBuf;
|
delete[] mpInBuf;
|
||||||
|
meState = STATE_INIT;
|
||||||
}
|
}
|
||||||
return ( mbStatus ) ? retvalue : -1;
|
return ( mbStatus ) ? retvalue : -1;
|
||||||
}
|
}
|
||||||
@ -113,13 +114,11 @@ long ZCodec::Compress( SvStream& rIStm, SvStream& rOStm )
|
|||||||
{
|
{
|
||||||
long nOldTotal_In = PZSTREAM->total_in;
|
long nOldTotal_In = PZSTREAM->total_in;
|
||||||
|
|
||||||
if ( mbInit == 0 )
|
assert(meState == STATE_INIT);
|
||||||
{
|
mpIStm = &rIStm;
|
||||||
mpIStm = &rIStm;
|
mpOStm = &rOStm;
|
||||||
mpOStm = &rOStm;
|
ImplInitBuf( false );
|
||||||
ImplInitBuf( false );
|
mpInBuf = new sal_uInt8[ mnInBufSize ];
|
||||||
mpInBuf = new sal_uInt8[ mnInBufSize ];
|
|
||||||
}
|
|
||||||
while (( PZSTREAM->avail_in = mpIStm->Read( PZSTREAM->next_in = mpInBuf, mnInBufSize )) != 0 )
|
while (( PZSTREAM->avail_in = mpIStm->Read( PZSTREAM->next_in = mpInBuf, mnInBufSize )) != 0 )
|
||||||
{
|
{
|
||||||
if ( PZSTREAM->avail_out == 0 )
|
if ( PZSTREAM->avail_out == 0 )
|
||||||
@ -139,16 +138,11 @@ long ZCodec::Decompress( SvStream& rIStm, SvStream& rOStm )
|
|||||||
sal_uIntPtr nInToRead;
|
sal_uIntPtr nInToRead;
|
||||||
long nOldTotal_Out = PZSTREAM->total_out;
|
long nOldTotal_Out = PZSTREAM->total_out;
|
||||||
|
|
||||||
if ( mbFinish )
|
assert(meState == STATE_INIT);
|
||||||
return PZSTREAM->total_out - nOldTotal_Out;
|
mpIStm = &rIStm;
|
||||||
|
mpOStm = &rOStm;
|
||||||
if ( mbInit == 0 )
|
ImplInitBuf( true );
|
||||||
{
|
PZSTREAM->next_out = mpOutBuf = new sal_uInt8[ PZSTREAM->avail_out = mnOutBufSize ];
|
||||||
mpIStm = &rIStm;
|
|
||||||
mpOStm = &rOStm;
|
|
||||||
ImplInitBuf( true );
|
|
||||||
PZSTREAM->next_out = mpOutBuf = new sal_uInt8[ PZSTREAM->avail_out = mnOutBufSize ];
|
|
||||||
}
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
if ( PZSTREAM->avail_out == 0 ) ImplWriteBack();
|
if ( PZSTREAM->avail_out == 0 ) ImplWriteBack();
|
||||||
@ -173,14 +167,12 @@ long ZCodec::Decompress( SvStream& rIStm, SvStream& rOStm )
|
|||||||
while ( ( err != Z_STREAM_END) && ( PZSTREAM->avail_in || mnInToRead ) );
|
while ( ( err != Z_STREAM_END) && ( PZSTREAM->avail_in || mnInToRead ) );
|
||||||
ImplWriteBack();
|
ImplWriteBack();
|
||||||
|
|
||||||
if ( err == Z_STREAM_END )
|
|
||||||
mbFinish = true;
|
|
||||||
return ( mbStatus ) ? (long)(PZSTREAM->total_out - nOldTotal_Out) : -1;
|
return ( mbStatus ) ? (long)(PZSTREAM->total_out - nOldTotal_Out) : -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
long ZCodec::Write( SvStream& rOStm, const sal_uInt8* pData, sal_uIntPtr nSize )
|
long ZCodec::Write( SvStream& rOStm, const sal_uInt8* pData, sal_uIntPtr nSize )
|
||||||
{
|
{
|
||||||
if ( mbInit == 0 )
|
if (meState == STATE_INIT)
|
||||||
{
|
{
|
||||||
mpOStm = &rOStm;
|
mpOStm = &rOStm;
|
||||||
ImplInitBuf( false );
|
ImplInitBuf( false );
|
||||||
@ -212,7 +204,7 @@ long ZCodec::Read( SvStream& rIStm, sal_uInt8* pData, sal_uIntPtr nSize )
|
|||||||
return 0; // PZSTREAM->total_out;
|
return 0; // PZSTREAM->total_out;
|
||||||
|
|
||||||
mpIStm = &rIStm;
|
mpIStm = &rIStm;
|
||||||
if ( mbInit == 0 )
|
if (meState == STATE_INIT)
|
||||||
{
|
{
|
||||||
ImplInitBuf( true );
|
ImplInitBuf( true );
|
||||||
}
|
}
|
||||||
@ -256,7 +248,7 @@ long ZCodec::ReadAsynchron( SvStream& rIStm, sal_uInt8* pData, sal_uIntPtr nSize
|
|||||||
if ( mbFinish )
|
if ( mbFinish )
|
||||||
return 0; // PZSTREAM->total_out;
|
return 0; // PZSTREAM->total_out;
|
||||||
|
|
||||||
if ( mbInit == 0 )
|
if (meState == STATE_INIT)
|
||||||
{
|
{
|
||||||
mpIStm = &rIStm;
|
mpIStm = &rIStm;
|
||||||
ImplInitBuf( true );
|
ImplInitBuf( true );
|
||||||
@ -308,7 +300,7 @@ void ZCodec::ImplWriteBack()
|
|||||||
|
|
||||||
if ( nAvail )
|
if ( nAvail )
|
||||||
{
|
{
|
||||||
if ( mbInit & 2 && mbUpdateCrc )
|
if (meState == STATE_COMPRESS && mbUpdateCrc)
|
||||||
UpdateCRC( mpOutBuf, nAvail );
|
UpdateCRC( mpOutBuf, nAvail );
|
||||||
mpOStm->Write( PZSTREAM->next_out = mpOutBuf, nAvail );
|
mpOStm->Write( PZSTREAM->next_out = mpOutBuf, nAvail );
|
||||||
PZSTREAM->avail_out = mnOutBufSize;
|
PZSTREAM->avail_out = mnOutBufSize;
|
||||||
@ -337,74 +329,72 @@ sal_uIntPtr ZCodec::GetCRC()
|
|||||||
|
|
||||||
void ZCodec::ImplInitBuf ( bool nIOFlag )
|
void ZCodec::ImplInitBuf ( bool nIOFlag )
|
||||||
{
|
{
|
||||||
if ( mbInit == 0 )
|
assert(meState == STATE_INIT);
|
||||||
|
if ( nIOFlag )
|
||||||
{
|
{
|
||||||
if ( nIOFlag )
|
meState = STATE_DECOMPRESS;
|
||||||
|
if ( mbStatus && mbGzLib )
|
||||||
{
|
{
|
||||||
mbInit = 1;
|
sal_uInt8 n1, n2, j, nMethod, nFlags;
|
||||||
if ( mbStatus && mbGzLib )
|
for ( int i = 0; i < 2; i++ ) // gz - magic number
|
||||||
{
|
{
|
||||||
sal_uInt8 n1, n2, j, nMethod, nFlags;
|
mpIStm->ReadUChar( j );
|
||||||
for ( int i = 0; i < 2; i++ ) // gz - magic number
|
if ( j != gz_magic[ i ] )
|
||||||
|
mbStatus = false;
|
||||||
|
}
|
||||||
|
mpIStm->ReadUChar( nMethod );
|
||||||
|
mpIStm->ReadUChar( nFlags );
|
||||||
|
if ( nMethod != Z_DEFLATED )
|
||||||
|
mbStatus = false;
|
||||||
|
if ( ( nFlags & GZ_RESERVED ) != 0 )
|
||||||
|
mbStatus = false;
|
||||||
|
/* Discard time, xflags and OS code: */
|
||||||
|
mpIStm->SeekRel( 6 );
|
||||||
|
/* skip the extra field */
|
||||||
|
if ( nFlags & GZ_EXTRA_FIELD )
|
||||||
|
{
|
||||||
|
mpIStm->ReadUChar( n1 ).ReadUChar( n2 );
|
||||||
|
mpIStm->SeekRel( n1 + ( n2 << 8 ) );
|
||||||
|
}
|
||||||
|
/* skip the original file name */
|
||||||
|
if ( nFlags & GZ_ORIG_NAME)
|
||||||
|
{
|
||||||
|
do
|
||||||
{
|
{
|
||||||
mpIStm->ReadUChar( j );
|
mpIStm->ReadUChar( j );
|
||||||
if ( j != gz_magic[ i ] )
|
|
||||||
mbStatus = false;
|
|
||||||
}
|
}
|
||||||
mpIStm->ReadUChar( nMethod );
|
while ( j && !mpIStm->IsEof() );
|
||||||
mpIStm->ReadUChar( nFlags );
|
|
||||||
if ( nMethod != Z_DEFLATED )
|
|
||||||
mbStatus = false;
|
|
||||||
if ( ( nFlags & GZ_RESERVED ) != 0 )
|
|
||||||
mbStatus = false;
|
|
||||||
/* Discard time, xflags and OS code: */
|
|
||||||
mpIStm->SeekRel( 6 );
|
|
||||||
/* skip the extra field */
|
|
||||||
if ( nFlags & GZ_EXTRA_FIELD )
|
|
||||||
{
|
|
||||||
mpIStm->ReadUChar( n1 ).ReadUChar( n2 );
|
|
||||||
mpIStm->SeekRel( n1 + ( n2 << 8 ) );
|
|
||||||
}
|
|
||||||
/* skip the original file name */
|
|
||||||
if ( nFlags & GZ_ORIG_NAME)
|
|
||||||
{
|
|
||||||
do
|
|
||||||
{
|
|
||||||
mpIStm->ReadUChar( j );
|
|
||||||
}
|
|
||||||
while ( j && !mpIStm->IsEof() );
|
|
||||||
}
|
|
||||||
/* skip the .gz file comment */
|
|
||||||
if ( nFlags & GZ_COMMENT )
|
|
||||||
{
|
|
||||||
do
|
|
||||||
{
|
|
||||||
mpIStm->ReadUChar( j );
|
|
||||||
}
|
|
||||||
while ( j && !mpIStm->IsEof() );
|
|
||||||
}
|
|
||||||
/* skip the header crc */
|
|
||||||
if ( nFlags & GZ_HEAD_CRC )
|
|
||||||
mpIStm->SeekRel( 2 );
|
|
||||||
if ( mbStatus )
|
|
||||||
mbStatus = ( inflateInit2( PZSTREAM, -MAX_WBITS) != Z_OK ) ? false : true;
|
|
||||||
}
|
}
|
||||||
else
|
/* skip the .gz file comment */
|
||||||
|
if ( nFlags & GZ_COMMENT )
|
||||||
{
|
{
|
||||||
mbStatus = ( inflateInit( PZSTREAM ) >= 0 );
|
do
|
||||||
|
{
|
||||||
|
mpIStm->ReadUChar( j );
|
||||||
|
}
|
||||||
|
while ( j && !mpIStm->IsEof() );
|
||||||
}
|
}
|
||||||
mpInBuf = new sal_uInt8[ mnInBufSize ];
|
/* skip the header crc */
|
||||||
|
if ( nFlags & GZ_HEAD_CRC )
|
||||||
|
mpIStm->SeekRel( 2 );
|
||||||
|
if ( mbStatus )
|
||||||
|
mbStatus = ( inflateInit2( PZSTREAM, -MAX_WBITS) != Z_OK ) ? false : true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
mbInit = 3;
|
mbStatus = ( inflateInit( PZSTREAM ) >= 0 );
|
||||||
|
|
||||||
mbStatus = ( deflateInit2_( PZSTREAM, mnCompressLevel, Z_DEFLATED,
|
|
||||||
MAX_WBITS, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY,
|
|
||||||
ZLIB_VERSION, sizeof( z_stream ) ) >= 0 );
|
|
||||||
|
|
||||||
PZSTREAM->next_out = mpOutBuf = new sal_uInt8[ PZSTREAM->avail_out = mnOutBufSize ];
|
|
||||||
}
|
}
|
||||||
|
mpInBuf = new sal_uInt8[ mnInBufSize ];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
meState = STATE_COMPRESS;
|
||||||
|
|
||||||
|
mbStatus = ( deflateInit2_( PZSTREAM, mnCompressLevel, Z_DEFLATED,
|
||||||
|
MAX_WBITS, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY,
|
||||||
|
ZLIB_VERSION, sizeof( z_stream ) ) >= 0 );
|
||||||
|
|
||||||
|
PZSTREAM->next_out = mpOutBuf = new sal_uInt8[ PZSTREAM->avail_out = mnOutBufSize ];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user