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-default-arguments,
-google-readability-casting, -google-readability-casting,
-hicpp-deprecated-headers, -hicpp-deprecated-headers,
-hicpp-no-array-decay,
-misc-const-correctness, -misc-const-correctness,
-misc-include-cleaner, -misc-include-cleaner,
-misc-non-private-member-variables-in-classes, -misc-non-private-member-variables-in-classes,

View file

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

View file

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

View file

@ -3,12 +3,11 @@
#include <QByteArray> #include <QByteArray>
#include <string> #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 ); std::string decompressLzma2( const char * bufptr, unsigned length, bool raw_decoder = false );
string decompressLzma2( const char * bufptr, unsigned length, bool raw_decoder = false );