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 10, 2024
1 parent 65bf7a8 commit ea2cdbe
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions src/fileimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*
*/

#define CHUNK_SIZE 1024
#include "fileimpl.h"
#include <zim/error.h>
#include "_dirent.h"
Expand Down Expand Up @@ -550,28 +551,45 @@ class Grouping

struct zim_MD5_CTX md5ctx;
zim_MD5Init(&md5ctx);


unsigned char ch[CHUNK_SIZE];
offset_type checksumPos = header.getChecksumPos();
offset_type currentPos = 0;
offset_type toRead = checksumPos;

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(toRead>=CHUNK_SIZE && stream.read(reinterpret_cast<char*>(ch),CHUNK_SIZE).good()) {
zim_MD5Update(&md5ctx, ch, CHUNK_SIZE);
toRead-=stream.gcount();
}

// Previous read was good, so we have exited the previous `while` because
// `toRead<CHUNK_SIZE`. Let's try to read `toRead` chars and process them later.
// Else, the previous `while` exited because we didn't succeed to read
// `CHUNK_SIZE`, and we still have some data to process before changing part.
// It reads the remaining amount of part when we reach the end of the file
if(stream.good()){
stream.read(reinterpret_cast<char*>(ch),toRead);
}

// It updates the checksum with the remaining amount of data when we
// reach the end of the file or part
zim_MD5Update(&md5ctx, ch, stream.gcount());
toRead-=stream.gcount();

if (stream.bad()) {
perror("error while reading file");
return false;
}
if (currentPos == checksumPos) {
if (!toRead) {
break;
}
}

if (currentPos != checksumPos) {
if (toRead) {
return false;
}

Expand All @@ -583,7 +601,6 @@ class Grouping
{
return false;
}

return true;
}

Expand Down

0 comments on commit ea2cdbe

Please sign in to comment.