Skip to content

Commit

Permalink
Merge pull request #861 from aryanA101a/main
Browse files Browse the repository at this point in the history
  • Loading branch information
mgautierfr authored Mar 25, 2024
2 parents 8d978e1 + 5e5c0b8 commit 96afb38
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 <zim/tools.h>
Expand Down Expand Up @@ -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<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-=CHUNK_SIZE;
}

// 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 @@ -580,7 +598,6 @@ class Grouping
{
return false;
}

return true;
}

Expand Down

0 comments on commit 96afb38

Please sign in to comment.