Skip to content

Commit

Permalink
Optimize checksum calculation on verify
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanA101a committed Mar 2, 2024
1 parent 65bf7a8 commit 3c8c5f2
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions src/fileimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -551,16 +551,36 @@ class Grouping
struct zim_MD5_CTX md5ctx;
zim_MD5Init(&md5ctx);

const int piece_size=1024;
unsigned char ch[piece_size];
offset_type checksumPos = header.getChecksumPos();
offset_type currentPos = 0;

for(auto part = zimFile->begin();
part != zimFile->end();
part++) {
std::ifstream stream(part->second->filename(), std::ios_base::in|std::ios_base::binary);

char ch;
for(/*NOTHING*/ ; currentPos < checksumPos && stream.get(ch).good(); currentPos++) {
zim_MD5Update(&md5ctx, reinterpret_cast<const uint8_t*>(&ch), 1);

while(currentPos < checksumPos-piece_size && stream.read(reinterpret_cast<char*>(ch),piece_size).good()) {
zim_MD5Update(&md5ctx, ch, piece_size);
currentPos+=stream.gcount();
}

// It reads and updates the checksum with the remaining amount of file
// when we reach the end of the file
if(stream.good()){
int remaining=checksumPos-currentPos;
stream.read(reinterpret_cast<char*>(ch),remaining);
zim_MD5Update(&md5ctx, ch, remaining);
currentPos+=stream.gcount();
}
// It updates the checksum with the remaining amount of part when we
// reach the end of the part or filesize is less than piece_size
else if(stream.gcount()>0){
int remaining=checksumPos<piece_size?checksumPos:stream.gcount();
zim_MD5Update(&md5ctx, ch, remaining);
currentPos+=remaining;
}
if (stream.bad()) {
perror("error while reading file");
Expand All @@ -583,7 +603,6 @@ class Grouping
{
return false;
}

return true;
}

Expand Down

0 comments on commit 3c8c5f2

Please sign in to comment.