diff --git a/src/fileimpl.cpp b/src/fileimpl.cpp index 7882d381b..fdd28b3d1 100644 --- a/src/fileimpl.cpp +++ b/src/fileimpl.cpp @@ -19,6 +19,7 @@ * */ +#define CHUNK_SIZE 1024 #include "fileimpl.h" #include #include "_dirent.h" @@ -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(&ch), 1); + while(toRead>=CHUNK_SIZE && stream.read(reinterpret_cast(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(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; } @@ -583,7 +601,6 @@ class Grouping { return false; } - return true; }