Skip to content

Commit

Permalink
refactor FileImpl::verify()
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanA101a committed Mar 4, 2024
1 parent 3c8c5f2 commit 8863e95
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 22 deletions.
37 changes: 15 additions & 22 deletions src/fileimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -551,47 +551,40 @@ class Grouping
struct zim_MD5_CTX md5ctx;
zim_MD5Init(&md5ctx);

const int piece_size=1024;
unsigned char ch[piece_size];
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);


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

// It reads and updates the checksum with the remaining amount of file
// when we reach the end of the file
// It reads 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;
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 Down
2 changes: 2 additions & 0 deletions src/fileimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ namespace zim
const std::string& getMimeType(uint16_t idx) const;

std::string getChecksum();

#define CHUNK_SIZE 1024
bool verify();
bool is_multiPart() const;

Expand Down

0 comments on commit 8863e95

Please sign in to comment.