opt: mdx -> avoid duplicated Adler-32 checksum in zlib decompression

This commit is contained in:
shenleban tongying 2024-11-10 21:04:59 -05:00
parent 160402e1d0
commit d4db51f278
4 changed files with 18 additions and 21 deletions

View file

@ -26,6 +26,7 @@ Checks: >
-google-default-arguments,
-google-readability-casting,
-hicpp-deprecated-headers,
-hicpp-no-array-decay,
-misc-const-correctness,
-misc-include-cleaner,
-misc-non-private-member-variables-in-classes,

View file

@ -264,14 +264,12 @@ bool MdictParser::parseCompressedBlock( qint64 compressedBlockSize,
case 0x02000000:
// zlib compression
decompressedBlock = zlibDecompress( buf, size );
if ( !checkAdler32( decompressedBlock.constData(), decompressedBlock.size(), checksum ) ) {
gdWarning( "MDict: parseCompressedBlock: zlib: checksum does not match" );
decompressedBlock = zlibDecompress( buf, size, checksum );
if ( decompressedBlock.isEmpty() ) {
gdWarning( "MDict: parseCompressedBlock: zlib: failed to decompress or checksum does not match" );
return false;
}
break;
default:
gdWarning( "MDict: parseCompressedBlock: unknown type" );
return false;

View file

@ -3,20 +3,21 @@
#include <bzlib.h>
#include <lzma.h>
#define CHUNK_SIZE 2048
using std::string;
QByteArray zlibDecompress( const char * bufptr, unsigned length )
static constexpr qsizetype CHUNK_SIZE = 2048;
QByteArray zlibDecompress( const char * bufptr, unsigned length, uLong adler32_checksum )
{
z_stream zs;
char buf[ CHUNK_SIZE ];
z_stream zs{};
QByteArray str;
int res;
memset( &zs, 0, sizeof( zs ) );
int res = Z_OK;
zs.next_in = (Bytef *)bufptr;
zs.avail_in = length;
res = inflateInit( &zs );
if ( res == Z_OK ) {
char buf[ CHUNK_SIZE ];
while ( res != Z_STREAM_END ) {
zs.next_out = (Bytef *)buf;
zs.avail_out = CHUNK_SIZE;
@ -27,9 +28,7 @@ QByteArray zlibDecompress( const char * bufptr, unsigned length )
}
}
}
inflateEnd( &zs );
if ( res != Z_STREAM_END ) {
if ( inflateEnd( &zs ) != Z_OK || res != Z_STREAM_END || ( adler32_checksum != 0 && zs.adler != adler32_checksum ) ) {
str.clear();
}
return str;
@ -37,7 +36,7 @@ QByteArray zlibDecompress( const char * bufptr, unsigned length )
string decompressZlib( const char * bufptr, unsigned length )
{
QByteArray b = zlibDecompress( bufptr, length );
QByteArray b = zlibDecompress( bufptr, length, 0 );
return string( b.constData(), b.size() );
}

View file

@ -3,12 +3,11 @@
#include <QByteArray>
#include <string>
using std::string;
/// @param adler32_checksum 0 to skip checksum
QByteArray zlibDecompress( const char * bufptr, unsigned length, unsigned long adler32_checksum );
QByteArray zlibDecompress( const char * bufptr, unsigned length );
std::string decompressZlib( const char * bufptr, unsigned length );
string decompressZlib( const char * bufptr, unsigned length );
std::string decompressBzip2( const char * bufptr, unsigned length );
string decompressBzip2( const char * bufptr, unsigned length );
string decompressLzma2( const char * bufptr, unsigned length, bool raw_decoder = false );
std::string decompressLzma2( const char * bufptr, unsigned length, bool raw_decoder = false );