diff --git a/src/DefaultBtProgressInfoFile.cc b/src/DefaultBtProgressInfoFile.cc index 5677ff26..ada93585 100644 --- a/src/DefaultBtProgressInfoFile.cc +++ b/src/DefaultBtProgressInfoFile.cc @@ -390,7 +390,7 @@ void DefaultBtProgressInfoFile::load() #ifdef ENABLE_MESSAGE_DIGEST - piece->setHashAlgo(dctx_->getPieceHashAlgo()); + piece->setHashAlgo(dctx_->getPieceHashType()); #endif // ENABLE_MESSAGE_DIGEST diff --git a/src/DefaultPieceStorage.cc b/src/DefaultPieceStorage.cc index 88d28d4b..8c683af5 100644 --- a/src/DefaultPieceStorage.cc +++ b/src/DefaultPieceStorage.cc @@ -106,7 +106,7 @@ SharedHandle DefaultPieceStorage::checkOutPiece piece.reset(new Piece(index, bitfieldMan_->getBlockLength(index))); #ifdef ENABLE_MESSAGE_DIGEST - piece->setHashAlgo(downloadContext_->getPieceHashAlgo()); + piece->setHashAlgo(downloadContext_->getPieceHashType()); #endif // ENABLE_MESSAGE_DIGEST @@ -735,7 +735,7 @@ void DefaultPieceStorage::markPiecesDone(uint64_t length) #ifdef ENABLE_MESSAGE_DIGEST - p->setHashAlgo(downloadContext_->getPieceHashAlgo()); + p->setHashAlgo(downloadContext_->getPieceHashType()); #endif // ENABLE_MESSAGE_DIGEST diff --git a/src/DownloadCommand.cc b/src/DownloadCommand.cc index 9e2066f0..f4a76267 100644 --- a/src/DownloadCommand.cc +++ b/src/DownloadCommand.cc @@ -92,7 +92,7 @@ DownloadCommand::DownloadCommand #ifdef ENABLE_MESSAGE_DIGEST { if(getOption()->getAsBool(PREF_REALTIME_CHUNK_CHECKSUM)) { - const std::string& algo = getDownloadContext()->getPieceHashAlgo(); + const std::string& algo = getDownloadContext()->getPieceHashType(); if(MessageDigest::supports(algo)) { messageDigest_ = MessageDigest::create(algo); pieceHashValidationEnabled_ = true; @@ -297,7 +297,7 @@ bool DownloadCommand::prepareForNextSegment() { } } #ifdef ENABLE_MESSAGE_DIGEST - if(getDownloadContext()->getPieceHashAlgo().empty()) { + if(getDownloadContext()->getPieceHashType().empty()) { SharedHandle entry (new ChecksumCheckIntegrityEntry(getRequestGroup())); if(entry->isValidationReady()) { diff --git a/src/DownloadContext.cc b/src/DownloadContext.cc index 56e2e7e2..beabd090 100644 --- a/src/DownloadContext.cc +++ b/src/DownloadContext.cc @@ -239,18 +239,18 @@ size_t DownloadContext::countRequestedFileEntry() const bool DownloadContext::isChecksumVerificationNeeded() const { - return pieceHashAlgo_.empty() && - !checksum_.empty() && !checksumHashAlgo_.empty() && !checksumVerified_; + return pieceHashType_.empty() && + !digest_.empty() && !hashType_.empty() && !checksumVerified_; } bool DownloadContext::isChecksumVerificationAvailable() const { - return !checksum_.empty() && !checksumHashAlgo_.empty(); + return !digest_.empty() && !hashType_.empty(); } bool DownloadContext::isPieceHashVerificationAvailable() const { - return !pieceHashAlgo_.empty() && + return !pieceHashType_.empty() && pieceHashes_.size() > 0 && pieceHashes_.size() == getNumPieces(); } @@ -263,19 +263,12 @@ const std::string& DownloadContext::getPieceHash(size_t index) const } } -void DownloadContext::setPieceHashAlgo(const std::string& algo) +void DownloadContext::setDigest +(const std::string& hashType, + const std::string& digest) { - pieceHashAlgo_ = algo; -} - -void DownloadContext::setChecksum(const std::string& checksum) -{ - checksum_ = checksum; -} - -void DownloadContext::setChecksumHashAlgo(const std::string& algo) -{ - checksumHashAlgo_ = algo; + hashType_ = hashType; + digest_ = digest; } void DownloadContext::setBasePath(const std::string& basePath) diff --git a/src/DownloadContext.h b/src/DownloadContext.h index 868ef621..f4126cdd 100644 --- a/src/DownloadContext.h +++ b/src/DownloadContext.h @@ -63,11 +63,11 @@ private: size_t pieceLength_; - std::string pieceHashAlgo_; + std::string pieceHashType_; - std::string checksum_; + std::string digest_; - std::string checksumHashAlgo_; + std::string hashType_; bool checksumVerified_; @@ -106,8 +106,11 @@ public: } template - void setPieceHashes(InputIterator first, InputIterator last) + void setPieceHashes + (const std::string& hashType, + InputIterator first, InputIterator last) { + pieceHashType_ = hashType; pieceHashes_.assign(first, last); } @@ -149,17 +152,13 @@ public: size_t getNumPieces() const; - const std::string& getPieceHashAlgo() const { return pieceHashAlgo_; } + const std::string& getPieceHashType() const { return pieceHashType_; } - void setPieceHashAlgo(const std::string& algo); + const std::string& getDigest() const { return digest_; } - const std::string& getChecksum() const { return checksum_; } + const std::string& getHashType() const { return hashType_; } - void setChecksum(const std::string& checksum); - - const std::string& getChecksumHashAlgo() const { return checksumHashAlgo_; } - - void setChecksumHashAlgo(const std::string& algo); + void setDigest(const std::string& hashType, const std::string& digest); // The representative path name for this context. It is used as a // part of .aria2 control file. If basePath_ is set, returns diff --git a/src/HttpResponseCommand.cc b/src/HttpResponseCommand.cc index 60a393e3..0f25f757 100644 --- a/src/HttpResponseCommand.cc +++ b/src/HttpResponseCommand.cc @@ -213,12 +213,11 @@ bool HttpResponseCommand::executeInternal() httpResponse->getDigest(checksums); for(std::vector::iterator i = checksums.begin(), eoi = checksums.end(); i != eoi; ++i) { - if(getDownloadContext()->getChecksumHashAlgo().empty()) { + if(getDownloadContext()->getHashType().empty()) { A2_LOG_DEBUG(fmt("Setting digest: type=%s, digest=%s", (*i).getHashType().c_str(), (*i).getDigest().c_str())); - getDownloadContext()->setChecksumHashAlgo((*i).getHashType()); - getDownloadContext()->setChecksum((*i).getDigest()); + getDownloadContext()->setDigest((*i).getHashType(), (*i).getDigest()); break; } else { if(checkChecksum(getDownloadContext(), *i)) { @@ -282,7 +281,7 @@ bool HttpResponseCommand::executeInternal() } } else { #ifdef ENABLE_MESSAGE_DIGEST - if(!getDownloadContext()->getChecksumHashAlgo().empty() && + if(!getDownloadContext()->getHashType().empty() && httpHeader->defined(HttpHeader::DIGEST)) { std::vector checksums; httpResponse->getDigest(checksums); @@ -559,8 +558,8 @@ bool HttpResponseCommand::checkChecksum (const SharedHandle& dctx, const Checksum& checksum) { - if(dctx->getChecksumHashAlgo() == checksum.getHashType()) { - if(dctx->getChecksum() == checksum.getDigest()) { + if(dctx->getHashType() == checksum.getHashType()) { + if(dctx->getDigest() == checksum.getDigest()) { A2_LOG_INFO("Valid hash found in Digest header field."); return true; } else { diff --git a/src/IteratableChecksumValidator.cc b/src/IteratableChecksumValidator.cc index 7437683a..be4137c4 100644 --- a/src/IteratableChecksumValidator.cc +++ b/src/IteratableChecksumValidator.cc @@ -73,8 +73,8 @@ void IteratableChecksumValidator::validateChunk() ctx_->update(buffer_, length); currentOffset_ += length; if(finished()) { - std::string actualChecksum = ctx_->hexDigest(); - if(dctx_->getChecksum() == actualChecksum) { + std::string actualDigest = ctx_->hexDigest(); + if(dctx_->getDigest() == actualDigest) { pieceStorage_->markAllPiecesDone(); } else { BitfieldMan bitfield(dctx_->getPieceLength(), dctx_->getTotalLength()); @@ -103,7 +103,7 @@ void IteratableChecksumValidator::init() delete [] buffer_; buffer_ = new unsigned char[BUFSIZE]; currentOffset_ = 0; - ctx_ = MessageDigest::create(dctx_->getChecksumHashAlgo()); + ctx_ = MessageDigest::create(dctx_->getHashType()); } } // namespace aria2 diff --git a/src/IteratableChunkChecksumValidator.cc b/src/IteratableChunkChecksumValidator.cc index 1382f37a..475e3b06 100644 --- a/src/IteratableChunkChecksumValidator.cc +++ b/src/IteratableChunkChecksumValidator.cc @@ -122,7 +122,7 @@ void IteratableChunkChecksumValidator::init() { delete [] buffer_; buffer_ = new unsigned char[BUFSIZE]; - ctx_ = MessageDigest::create(dctx_->getPieceHashAlgo()); + ctx_ = MessageDigest::create(dctx_->getPieceHashType()); bitfield_->clearAllBit(); currentIndex_ = 0; } diff --git a/src/Metalink2RequestGroup.cc b/src/Metalink2RequestGroup.cc index 579d2362..07291dda 100644 --- a/src/Metalink2RequestGroup.cc +++ b/src/Metalink2RequestGroup.cc @@ -263,13 +263,13 @@ Metalink2RequestGroup::createRequestGroup } #ifdef ENABLE_MESSAGE_DIGEST if(entry->checksum) { - dctx->setChecksum(entry->checksum->getDigest()); - dctx->setChecksumHashAlgo(entry->checksum->getHashType()); + dctx->setDigest(entry->checksum->getHashType(), + entry->checksum->getDigest()); } if(entry->chunkChecksum) { - dctx->setPieceHashes(entry->chunkChecksum->getPieceHashes().begin(), + dctx->setPieceHashes(entry->chunkChecksum->getHashType(), + entry->chunkChecksum->getPieceHashes().begin(), entry->chunkChecksum->getPieceHashes().end()); - dctx->setPieceHashAlgo(entry->chunkChecksum->getHashType()); } #endif // ENABLE_MESSAGE_DIGEST dctx->setSignature(entry->getSignature()); diff --git a/src/bittorrent_helper.cc b/src/bittorrent_helper.cc index c1efcfb7..790ae82d 100644 --- a/src/bittorrent_helper.cc +++ b/src/bittorrent_helper.cc @@ -122,8 +122,7 @@ void extractPieceHash(const SharedHandle& ctx, pieceHashes.push_back(util::toHex(hashData.data()+i*hashLength, hashLength)); } - ctx->setPieceHashes(pieceHashes.begin(), pieceHashes.end()); - ctx->setPieceHashAlgo("sha-1"); + ctx->setPieceHashes("sha-1", pieceHashes.begin(), pieceHashes.end()); } } // namespace diff --git a/test/BittorrentHelperTest.cc b/test/BittorrentHelperTest.cc index 34f51223..c83ab309 100644 --- a/test/BittorrentHelperTest.cc +++ b/test/BittorrentHelperTest.cc @@ -157,7 +157,7 @@ void BittorrentHelperTest::testGetPieceHash() { CPPUNIT_ASSERT_EQUAL(std::string(""), dctx->getPieceHash(3)); - CPPUNIT_ASSERT_EQUAL(std::string("sha-1"), dctx->getPieceHashAlgo()); + CPPUNIT_ASSERT_EQUAL(std::string("sha-1"), dctx->getPieceHashType()); } void BittorrentHelperTest::testGetFileEntries() { diff --git a/test/DownloadContextTest.cc b/test/DownloadContextTest.cc index 04aaf5cb..384264a3 100644 --- a/test/DownloadContextTest.cc +++ b/test/DownloadContextTest.cc @@ -54,7 +54,7 @@ void DownloadContextTest::testGetPieceHash() { DownloadContext ctx; const std::string pieceHashes[] = { "hash1","hash2","shash3" }; - ctx.setPieceHashes(&pieceHashes[0], &pieceHashes[3]); + ctx.setPieceHashes("sha-1", &pieceHashes[0], &pieceHashes[3]); CPPUNIT_ASSERT_EQUAL(std::string("hash1"), ctx.getPieceHash(0)); CPPUNIT_ASSERT_EQUAL(std::string(""), ctx.getPieceHash(3)); } diff --git a/test/IteratableChecksumValidatorTest.cc b/test/IteratableChecksumValidatorTest.cc index af2c3c7d..4729e32b 100644 --- a/test/IteratableChecksumValidatorTest.cc +++ b/test/IteratableChecksumValidatorTest.cc @@ -34,8 +34,7 @@ void IteratableChecksumValidatorTest::testValidate() { Option option; SharedHandle dctx (new DownloadContext(100, 250, A2_TEST_DIR"/chunkChecksumTestFile250.txt")); - dctx->setChecksum("898a81b8e0181280ae2ee1b81e269196d91e869a"); - dctx->setChecksumHashAlgo("sha-1"); + dctx->setDigest("sha-1", "898a81b8e0181280ae2ee1b81e269196d91e869a"); SharedHandle ps(new DefaultPieceStorage(dctx, &option)); ps->initStorage(); ps->getDiskAdaptor()->enableReadOnly(); @@ -54,8 +53,7 @@ void IteratableChecksumValidatorTest::testValidate_fail() { Option option; SharedHandle dctx (new DownloadContext(100, 250, A2_TEST_DIR"/chunkChecksumTestFile250.txt")); - dctx->setChecksum(std::string(40, '0')); // set wrong checksum - dctx->setChecksumHashAlgo("sha-1"); + dctx->setDigest("sha-1", std::string(40, '0')); // set wrong checksum SharedHandle ps(new DefaultPieceStorage(dctx, &option)); ps->initStorage(); ps->getDiskAdaptor()->enableReadOnly(); diff --git a/test/IteratableChunkChecksumValidatorTest.cc b/test/IteratableChunkChecksumValidatorTest.cc index bf39a78d..63328013 100644 --- a/test/IteratableChunkChecksumValidatorTest.cc +++ b/test/IteratableChunkChecksumValidatorTest.cc @@ -39,8 +39,7 @@ void IteratableChunkChecksumValidatorTest::testValidate() { Option option; SharedHandle dctx (new DownloadContext(100, 250, A2_TEST_DIR"/chunkChecksumTestFile250.txt")); - dctx->setPieceHashes(&csArray[0], &csArray[3]); - dctx->setPieceHashAlgo("sha-1"); + dctx->setPieceHashes("sha-1", &csArray[0], &csArray[3]); SharedHandle ps (new DefaultPieceStorage(dctx, &option)); ps->initStorage(); @@ -61,7 +60,7 @@ void IteratableChunkChecksumValidatorTest::testValidate() { // make the test fail std::deque badHashes(&csArray[0], &csArray[3]); badHashes[1] = "ffffffffffffffffffffffffffffffffffffffff"; - dctx->setPieceHashes(badHashes.begin(), badHashes.end()); + dctx->setPieceHashes("sha-1", badHashes.begin(), badHashes.end()); validator.init(); @@ -80,8 +79,7 @@ void IteratableChunkChecksumValidatorTest::testValidate_readError() { std::deque hashes(&csArray[0], &csArray[3]); hashes.push_back("ffffffffffffffffffffffffffffffffffffffff"); hashes.push_back("ffffffffffffffffffffffffffffffffffffffff"); - dctx->setPieceHashes(hashes.begin(), hashes.end()); - dctx->setPieceHashAlgo("sha-1"); + dctx->setPieceHashes("sha-1", hashes.begin(), hashes.end()); SharedHandle ps(new DefaultPieceStorage(dctx, &option)); ps->initStorage(); ps->getDiskAdaptor()->enableReadOnly(); diff --git a/test/Metalink2RequestGroupTest.cc b/test/Metalink2RequestGroupTest.cc index 2e64283a..089c49ec 100644 --- a/test/Metalink2RequestGroupTest.cc +++ b/test/Metalink2RequestGroupTest.cc @@ -60,10 +60,10 @@ void Metalink2RequestGroupTest::testGenerate() CPPUNIT_ASSERT(dctx); CPPUNIT_ASSERT_EQUAL((uint64_t)0ULL, dctx->getTotalLength()); #ifdef ENABLE_MESSAGE_DIGEST - CPPUNIT_ASSERT_EQUAL(std::string("sha-1"), dctx->getChecksumHashAlgo()); + CPPUNIT_ASSERT_EQUAL(std::string("sha-1"), dctx->getHashType()); CPPUNIT_ASSERT_EQUAL (std::string("a96cf3f0266b91d87d5124cf94326422800b627d"), - dctx->getChecksum()); + dctx->getDigest()); #endif // ENABLE_MESSAGE_DIGEST CPPUNIT_ASSERT(dctx->getSignature()); CPPUNIT_ASSERT_EQUAL(std::string("pgp"), dctx->getSignature()->getType()); @@ -79,13 +79,13 @@ void Metalink2RequestGroupTest::testGenerate() CPPUNIT_ASSERT(dctx); #ifdef ENABLE_MESSAGE_DIGEST - CPPUNIT_ASSERT_EQUAL(std::string("sha-1"), dctx->getPieceHashAlgo()); + CPPUNIT_ASSERT_EQUAL(std::string("sha-1"), dctx->getPieceHashType()); CPPUNIT_ASSERT_EQUAL((size_t)2, dctx->getPieceHashes().size()); CPPUNIT_ASSERT_EQUAL((size_t)262144, dctx->getPieceLength()); - CPPUNIT_ASSERT_EQUAL(std::string("sha-1"), dctx->getChecksumHashAlgo()); + CPPUNIT_ASSERT_EQUAL(std::string("sha-1"), dctx->getHashType()); CPPUNIT_ASSERT_EQUAL (std::string("4c255b0ed130f5ea880f0aa061c3da0487e251cc"), - dctx->getChecksum()); + dctx->getDigest()); #endif // ENABLE_MESSAGE_DIGEST CPPUNIT_ASSERT(!dctx->getSignature()); }