When I attempt to decompress data of a size greater than 2048 the zlib uncompress call returns Z_OK. So to clarify if I decompress data of size 2980 it will decompress upto 2048 (Two loops) and then return Z_OK. What am i missing?
Bytes is a vector< unsigned char >;
Bytes uncompressIt( const Bytes& data )
{
size_t buffer_length = 1024;
Byte* buffer = nullptr;
int status = 0;
do
{
buffer = ( Byte* ) calloc( buffer_length + 1, sizeof( Byte ) );
int status = uncompress( buffer, &buffer_length, &data[ 0 ], data.size( ) );
if ( status == Z_OK )
{
break;
}
else if ( status == Z_MEM_ERROR )
{
throw runtime_error( "GZip decompress ran out of memory." );
}
else if ( status == Z_DATA_ERROR )
{
throw runtime_error( "GZip decompress input data was corrupted or incomplete." );
}
else //if ( status == Z_BUF_ERROR )
{
free( buffer );
buffer_length *= 2;
}
} while ( status == Z_BUF_ERROR ); //then the output buffer wasn't large enough
Bytes result;
for( size_t index = 0; index != buffer_length; index++ )
{
result.push_back( buffer[ index ] );
}
return result;
}
EDIT:
Thanks @Michael for catching the realloc. I've been mucking around with the implementation and missed it; still no excuse before posting it.