Skip to content

Commit

Permalink
the last headword in the block has incorrect data length
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoyifang committed Jun 23, 2022
1 parent 4a3905c commit b617323
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
34 changes: 28 additions & 6 deletions mdictparser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -595,10 +595,12 @@ MdictParser::HeadWordIndex MdictParser::splitHeadWordBlock( QByteArray const & b
}

bool MdictParser::readRecordBlock( MdictParser::HeadWordIndex & headWordIndex,
MdictParser::RecordHandler & recordHandler )
MdictParser::RecordHandler & recordHandler,
bool cross_block_read )
{
// cache the index, the headWordIndex is already sorted
size_t idx = 0;
bool readNextBlock = false;

for ( HeadWordIndex::const_iterator i = headWordIndex.begin(); i != headWordIndex.end(); ++i )
{
Expand All @@ -611,21 +613,41 @@ bool MdictParser::readRecordBlock( MdictParser::HeadWordIndex & headWordIndex,
RecordIndex const & recordIndex = recordBlockInfos_[idx];
HeadWordIndex::const_iterator iNext = i + 1;
qint64 recordSize;
if ( iNext == headWordIndex.end() )
recordSize = recordIndex.shadowEndPos - i->first;
auto current = *i;

if( iNext == headWordIndex.end() )
{
qint64 lastWordSize = recordIndex.shadowEndPos - current.first;

readNextBlock = cross_block_read && readNextHeadWordIndex( headWordIndex );
if(readNextBlock)
{
recordSize = qMin(lastWordSize, headWordIndex.begin()->first - current.first);
}
else
{
recordSize = lastWordSize;
}
}
else
recordSize = iNext->first - i->first;
recordSize = iNext->first - current.first;

RecordInfo recordInfo;
recordInfo.compressedBlockPos = recordPos_ + recordIndex.startPos;
recordInfo.recordOffset = i->first - recordIndex.shadowStartPos;
recordInfo.recordOffset = current.first - recordIndex.shadowStartPos;
recordInfo.decompressedBlockSize = recordIndex.decompressedSize;
recordInfo.compressedBlockSize = recordIndex.compressedSize;
recordInfo.recordSize = recordSize;

recordHandler.handleRecord( i->second, recordInfo );
recordHandler.handleRecord( current.second, recordInfo );

if( readNextBlock )
break;
}

if( readNextBlock )
readRecordBlock( headWordIndex, recordHandler, cross_block_read );

return true;
}

Expand Down
2 changes: 1 addition & 1 deletion mdictparser.hh
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public:

bool open( const char * filename );
bool readNextHeadWordIndex( HeadWordIndex & headWordIndex );
bool readRecordBlock( HeadWordIndex & headWordIndex, RecordHandler & recordHandler );
bool readRecordBlock( HeadWordIndex & headWordIndex, RecordHandler & recordHandler, bool cross_block_read=false );

// helpers
static QString toUtf16( const char * fromCode, const char * from, size_t fromSize );
Expand Down
4 changes: 2 additions & 2 deletions mdx.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1440,9 +1440,9 @@ vector< sptr< Dictionary::Class > > makeDictionaries( vector< string > const & f
MdictParser::HeadWordIndex headWordIndex;

// enumerating word and its definition
while ( parser.readNextHeadWordIndex( headWordIndex ) )
if ( parser.readNextHeadWordIndex( headWordIndex ) )
{
parser.readRecordBlock( headWordIndex, articleHandler );
parser.readRecordBlock( headWordIndex, articleHandler, true);
}

// enumerating resources if there's any
Expand Down

0 comments on commit b617323

Please sign in to comment.