From 8863e95fb4ba39c43188bb0a2cb2cc7cf110a67c Mon Sep 17 00:00:00 2001 From: Aryan Arora Date: Tue, 5 Mar 2024 00:19:54 +0530 Subject: [PATCH] refactor FileImpl::verify() --- src/fileimpl.cpp | 37 +++++++++++++++---------------------- src/fileimpl.h | 2 ++ 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/src/fileimpl.cpp b/src/fileimpl.cpp index 746298769..a19f6d0d3 100644 --- a/src/fileimpl.cpp +++ b/src/fileimpl.cpp @@ -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(ch),piece_size).good()) { - zim_MD5Update(&md5ctx, ch, piece_size); - currentPos+=stream.gcount(); + while(toRead>=CHUNK_SIZE && stream.read(reinterpret_cast(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(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(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; } diff --git a/src/fileimpl.h b/src/fileimpl.h index b07d2c7f1..25ea19006 100644 --- a/src/fileimpl.h +++ b/src/fileimpl.h @@ -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;