diff --git a/src/fileimpl.cpp b/src/fileimpl.cpp index 61d43c7e..229da4b6 100644 --- a/src/fileimpl.cpp +++ b/src/fileimpl.cpp @@ -19,6 +19,7 @@ * */ +#define CHUNK_SIZE 1024 #include "fileimpl.h" #include #include @@ -547,28 +548,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(&ch), 1); + while(toRead>=CHUNK_SIZE && stream.read(reinterpret_cast(ch),CHUNK_SIZE).good()) { + zim_MD5Update(&md5ctx, ch, CHUNK_SIZE); + toRead-=CHUNK_SIZE; + } + + // Previous read was good, so we have exited the previous `while` because + // `toRead(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; } @@ -580,7 +598,6 @@ class Grouping { return false; } - return true; }