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:
Stephan Bergmann 2014-05-21 17:03:41 +02:00
parent d4af55670f
commit 1c92fbf4fd
2 changed files with 80 additions and 84 deletions

View File

@ -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;

View File

@ -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;
if ( mbInit == 0 )
{
mpIStm = &rIStm; mpIStm = &rIStm;
mpOStm = &rOStm; mpOStm = &rOStm;
ImplInitBuf( true ); ImplInitBuf( true );
PZSTREAM->next_out = mpOutBuf = new sal_uInt8[ PZSTREAM->avail_out = mnOutBufSize ]; 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,11 +329,10 @@ 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 )
{ {
mbInit = 1; meState = STATE_DECOMPRESS;
if ( mbStatus && mbGzLib ) if ( mbStatus && mbGzLib )
{ {
sal_uInt8 n1, n2, j, nMethod, nFlags; sal_uInt8 n1, n2, j, nMethod, nFlags;
@ -397,7 +388,7 @@ void ZCodec::ImplInitBuf ( bool nIOFlag )
} }
else else
{ {
mbInit = 3; meState = STATE_COMPRESS;
mbStatus = ( deflateInit2_( PZSTREAM, mnCompressLevel, Z_DEFLATED, mbStatus = ( deflateInit2_( PZSTREAM, mnCompressLevel, Z_DEFLATED,
MAX_WBITS, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY, MAX_WBITS, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY,
@ -406,7 +397,6 @@ void ZCodec::ImplInitBuf ( bool nIOFlag )
PZSTREAM->next_out = mpOutBuf = new sal_uInt8[ PZSTREAM->avail_out = mnOutBufSize ]; PZSTREAM->next_out = mpOutBuf = new sal_uInt8[ PZSTREAM->avail_out = mnOutBufSize ];
} }
} }
}
void ZCodec::UpdateCRC ( sal_uInt8* pSource, long nDatSize) void ZCodec::UpdateCRC ( sal_uInt8* pSource, long nDatSize)
{ {