diff options
| author | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2011-10-13 21:40:07 +0900 |
|---|---|---|
| committer | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2011-10-13 21:40:07 +0900 |
| commit | 310e58bc621803917179c80b399013cf8b80e245 (patch) | |
| tree | 9ef55317e75ba1f3d378e70a50a191659e73bc38 /src | |
| parent | c2eb9d791a06baa4d28046813c25f4da137faa77 (diff) | |
Stored hash value as raw byte string, instead of hex digest.
Diffstat (limited to 'src')
| -rw-r--r-- | src/BtPieceMessage.cc | 5 | ||||
| -rw-r--r-- | src/Checksum.h | 2 | ||||
| -rw-r--r-- | src/DownloadCommand.cc | 16 | ||||
| -rw-r--r-- | src/GrowSegment.cc | 2 | ||||
| -rw-r--r-- | src/GrowSegment.h | 2 | ||||
| -rw-r--r-- | src/HttpResponse.cc | 7 | ||||
| -rw-r--r-- | src/IteratableChecksumValidator.cc | 5 | ||||
| -rw-r--r-- | src/IteratableChunkChecksumValidator.cc | 13 | ||||
| -rw-r--r-- | src/MessageDigest.cc | 4 | ||||
| -rw-r--r-- | src/MessageDigest.h | 6 | ||||
| -rw-r--r-- | src/MetalinkParserController.cc | 6 | ||||
| -rw-r--r-- | src/Piece.cc | 4 | ||||
| -rw-r--r-- | src/Piece.h | 4 | ||||
| -rw-r--r-- | src/PiecedSegment.cc | 4 | ||||
| -rw-r--r-- | src/PiecedSegment.h | 2 | ||||
| -rw-r--r-- | src/Segment.h | 2 | ||||
| -rw-r--r-- | src/bittorrent_helper.cc | 4 | ||||
| -rw-r--r-- | src/download_helper.cc | 2 | ||||
| -rw-r--r-- | src/message_digest_helper.cc | 8 | ||||
| -rw-r--r-- | src/message_digest_helper.h | 6 |
20 files changed, 53 insertions, 51 deletions
diff --git a/src/BtPieceMessage.cc b/src/BtPieceMessage.cc index 800f0751..b58ab647 100644 --- a/src/BtPieceMessage.cc +++ b/src/BtPieceMessage.cc @@ -223,11 +223,10 @@ bool BtPieceMessage::checkPieceHash(const SharedHandle<Piece>& piece) A2_LOG_DEBUG(fmt("Hash is available!! index=%lu", static_cast<unsigned long>(piece->getIndex()))); return - piece->getHashString()==downloadContext_->getPieceHash(piece->getIndex()); + piece->getDigest() == downloadContext_->getPieceHash(piece->getIndex()); } else { off_t offset = (off_t)piece->getIndex()*downloadContext_->getPieceLength(); - - return message_digest::staticSHA1DigestHexDigest + return message_digest::staticSHA1Digest (getPieceStorage()->getDiskAdaptor(), offset, piece->getLength()) == downloadContext_->getPieceHash(piece->getIndex()); } diff --git a/src/Checksum.h b/src/Checksum.h index f95d9c68..5fcf3182 100644 --- a/src/Checksum.h +++ b/src/Checksum.h @@ -46,7 +46,7 @@ private: std::string hashType_; std::string digest_; public: - // messageDigest_ is ascii hexadecimal notation. + // digest_ is raw byte array of hash value, not ascii hexadecimal notation. Checksum(const std::string& hashType, const std::string& digest); Checksum(); ~Checksum(); diff --git a/src/DownloadCommand.cc b/src/DownloadCommand.cc index 498309e0..d6beb2fe 100644 --- a/src/DownloadCommand.cc +++ b/src/DownloadCommand.cc @@ -231,12 +231,12 @@ bool DownloadCommand::executeInternal() { A2_LOG_DEBUG(fmt("Hash is available! index=%lu", static_cast<unsigned long>(segment->getIndex()))); validatePieceHash - (segment, expectedPieceHash, segment->getHashString()); + (segment, expectedPieceHash, segment->getDigest()); } else { messageDigest_->reset(); validatePieceHash (segment, expectedPieceHash, - message_digest::hexDigest + message_digest::digest (messageDigest_, getPieceStorage()->getDiskAdaptor(), segment->getPosition(), @@ -350,18 +350,18 @@ bool DownloadCommand::prepareForNextSegment() { #ifdef ENABLE_MESSAGE_DIGEST void DownloadCommand::validatePieceHash(const SharedHandle<Segment>& segment, - const std::string& expectedPieceHash, - const std::string& actualPieceHash) + const std::string& expectedHash, + const std::string& actualHash) { - if(actualPieceHash == expectedPieceHash) { - A2_LOG_INFO(fmt(MSG_GOOD_CHUNK_CHECKSUM, actualPieceHash.c_str())); + if(actualHash == expectedHash) { + A2_LOG_INFO(fmt(MSG_GOOD_CHUNK_CHECKSUM, util::toHex(actualHash).c_str())); getSegmentMan()->completeSegment(getCuid(), segment); } else { A2_LOG_INFO(fmt(EX_INVALID_CHUNK_CHECKSUM, static_cast<unsigned long>(segment->getIndex()), util::itos(segment->getPosition(), true).c_str(), - expectedPieceHash.c_str(), - actualPieceHash.c_str())); + util::toHex(expectedHash).c_str(), + util::toHex(actualHash).c_str())); segment->clear(); getSegmentMan()->cancelSegment(getCuid()); throw DL_RETRY_EX diff --git a/src/GrowSegment.cc b/src/GrowSegment.cc index 266d05f1..fcbdbb22 100644 --- a/src/GrowSegment.cc +++ b/src/GrowSegment.cc @@ -52,7 +52,7 @@ void GrowSegment::updateWrittenLength(size_t bytes) #ifdef ENABLE_MESSAGE_DIGEST -std::string GrowSegment::getHashString() +std::string GrowSegment::getDigest() { return A2STR::NIL; } diff --git a/src/GrowSegment.h b/src/GrowSegment.h index 382567b3..0e553c15 100644 --- a/src/GrowSegment.h +++ b/src/GrowSegment.h @@ -98,7 +98,7 @@ public: return false; } - virtual std::string getHashString(); + virtual std::string getDigest(); #endif // ENABLE_MESSAGE_DIGEST diff --git a/src/HttpResponse.cc b/src/HttpResponse.cc index e1381ad1..0c879291 100644 --- a/src/HttpResponse.cc +++ b/src/HttpResponse.cc @@ -408,11 +408,12 @@ void HttpResponse::getDigest(std::vector<Checksum>& result) const break; } util::lowercase(hashType); - std::string hexDigest = util::toHex(Base64::decode(digest)); - if(!MessageDigest::isValidHash(hashType, hexDigest)) { + digest = Base64::decode(digest); + if(!MessageDigest::supports(hashType) || + MessageDigest::getDigestLength(hashType) != digest.size()) { continue; } - result.push_back(Checksum(hashType, hexDigest)); + result.push_back(Checksum(hashType, digest)); } } std::sort(result.begin(), result.end(), HashTypeStronger()); diff --git a/src/IteratableChecksumValidator.cc b/src/IteratableChecksumValidator.cc index 36cd7518..20b1b5cc 100644 --- a/src/IteratableChecksumValidator.cc +++ b/src/IteratableChecksumValidator.cc @@ -75,12 +75,13 @@ void IteratableChecksumValidator::validateChunk() ctx_->update(buffer_, length); currentOffset_ += length; if(finished()) { - std::string actualDigest = ctx_->hexDigest(); + std::string actualDigest = ctx_->digest(); if(dctx_->getDigest() == actualDigest) { pieceStorage_->markAllPiecesDone(); } else { A2_LOG_INFO(fmt("Checksum validation failed. expected=%s, actual=%s", - dctx_->getDigest().c_str(), actualDigest.c_str())); + util::toHex(dctx_->getDigest()).c_str(), + util::toHex(actualDigest).c_str())); BitfieldMan bitfield(dctx_->getPieceLength(), dctx_->getTotalLength()); pieceStorage_->setBitfield(bitfield.getBitfield(), bitfield.getBitfieldLength()); } diff --git a/src/IteratableChunkChecksumValidator.cc b/src/IteratableChunkChecksumValidator.cc index 475e3b06..81f236d6 100644 --- a/src/IteratableChunkChecksumValidator.cc +++ b/src/IteratableChunkChecksumValidator.cc @@ -82,11 +82,12 @@ void IteratableChunkChecksumValidator::validateChunk() if(actualChecksum == dctx_->getPieceHashes()[currentIndex_]) { bitfield_->setBit(currentIndex_); } else { - A2_LOG_INFO(fmt(EX_INVALID_CHUNK_CHECKSUM, - static_cast<unsigned long>(currentIndex_), - util::itos(getCurrentOffset(), true).c_str(), - dctx_->getPieceHashes()[currentIndex_].c_str(), - actualChecksum.c_str())); + A2_LOG_INFO + (fmt(EX_INVALID_CHUNK_CHECKSUM, + static_cast<unsigned long>(currentIndex_), + util::itos(getCurrentOffset(), true).c_str(), + util::toHex(dctx_->getPieceHashes()[currentIndex_]).c_str(), + util::toHex(actualChecksum).c_str())); bitfield_->unsetBit(currentIndex_); } } catch(RecoverableException& ex) { @@ -156,7 +157,7 @@ std::string IteratableChunkChecksumValidator::digest(off_t offset, size_t length curoffset += r; woffset = 0; } - return ctx_->hexDigest(); + return ctx_->digest(); } diff --git a/src/MessageDigest.cc b/src/MessageDigest.cc index 4c2ea17e..87010f78 100644 --- a/src/MessageDigest.cc +++ b/src/MessageDigest.cc @@ -172,12 +172,12 @@ void MessageDigest::digest(unsigned char* md) pImpl_->digest(md); } -std::string MessageDigest::hexDigest() +std::string MessageDigest::digest() { size_t length = pImpl_->getDigestLength(); array_ptr<unsigned char> buf(new unsigned char[length]); pImpl_->digest(buf); - std::string hd = util::toHex(buf, length); + std::string hd(&buf[0], &buf[length]); return hd; } diff --git a/src/MessageDigest.h b/src/MessageDigest.h index 7f24a1db..6441e748 100644 --- a/src/MessageDigest.h +++ b/src/MessageDigest.h @@ -101,9 +101,9 @@ public: // reset(). void digest(unsigned char* md); - // Returns hex digest. This call can only be called once. To reuse - // this object, call reset(). - std::string hexDigest(); + // Returns raw digest, not hex digest. This call can only be called + // once. To reuse this object, call reset(). + std::string digest(); }; } // namespace aria2 diff --git a/src/MetalinkParserController.cc b/src/MetalinkParserController.cc index 0b2add57..0d886a49 100644 --- a/src/MetalinkParserController.cc +++ b/src/MetalinkParserController.cc @@ -284,7 +284,7 @@ void MetalinkParserController::setHashOfChecksum(const std::string& md) return; } if(MessageDigest::isValidHash(tChecksum_->getHashType(), md)) { - tChecksum_->setDigest(md); + tChecksum_->setDigest(util::fromHex(md)); } else { cancelChecksumTransaction(); } @@ -360,7 +360,7 @@ void MetalinkParserController::addHashOfChunkChecksumV4(const std::string& md) return; } if(MessageDigest::isValidHash(tChunkChecksumV4_->getHashType(), md)) { - tempChunkChecksumsV4_.push_back(md); + tempChunkChecksumsV4_.push_back(util::fromHex(md)); } else { cancelChunkChecksumTransactionV4(); } @@ -463,7 +463,7 @@ void MetalinkParserController::setMessageDigestOfChunkChecksum(const std::string return; } if(MessageDigest::isValidHash(tChunkChecksum_->getHashType(), md)) { - tempHashPair_.second = md; + tempHashPair_.second = util::fromHex(md); } else { cancelChunkChecksumTransaction(); } diff --git a/src/Piece.cc b/src/Piece.cc index f6f8c63a..a0698c3e 100644 --- a/src/Piece.cc +++ b/src/Piece.cc @@ -218,12 +218,12 @@ bool Piece::isHashCalculated() const return mdctx_ && nextBegin_ == length_; } -std::string Piece::getHashString() +std::string Piece::getDigest() { if(!mdctx_) { return A2STR::NIL; } else { - std::string hash = mdctx_->hexDigest(); + std::string hash = mdctx_->digest(); destroyHashContext(); return hash; } diff --git a/src/Piece.h b/src/Piece.h index 128c83a1..ae3b108a 100644 --- a/src/Piece.h +++ b/src/Piece.h @@ -164,11 +164,11 @@ public: bool isHashCalculated() const; - // Returns hash value in ASCII hexadecimal form, which is calculated + // Returns raw hash value, not hex digest, which is calculated // by updateHash(). Please note that this function returns hash // value only once. Second invocation without updateHash() returns // empty string. - std::string getHashString(); + std::string getDigest(); void destroyHashContext(); diff --git a/src/PiecedSegment.cc b/src/PiecedSegment.cc index bf6dd2b1..7cfd6fa9 100644 --- a/src/PiecedSegment.cc +++ b/src/PiecedSegment.cc @@ -102,9 +102,9 @@ bool PiecedSegment::isHashCalculated() const return piece_->isHashCalculated(); } -std::string PiecedSegment::getHashString() +std::string PiecedSegment::getDigest() { - return piece_->getHashString(); + return piece_->getDigest(); } #endif // ENABLE_MESSAGE_DIGEST diff --git a/src/PiecedSegment.h b/src/PiecedSegment.h index 00d3a386..85461358 100644 --- a/src/PiecedSegment.h +++ b/src/PiecedSegment.h @@ -84,7 +84,7 @@ public: virtual bool isHashCalculated() const; - virtual std::string getHashString(); + virtual std::string getDigest(); #endif // ENABLE_MESSAGE_DIGEST diff --git a/src/Segment.h b/src/Segment.h index b8f38c9f..153288d3 100644 --- a/src/Segment.h +++ b/src/Segment.h @@ -74,7 +74,7 @@ public: virtual bool isHashCalculated() const = 0; - virtual std::string getHashString() = 0; + virtual std::string getDigest() = 0; #endif // ENABLE_MESSAGE_DIGEST diff --git a/src/bittorrent_helper.cc b/src/bittorrent_helper.cc index 9d7f4505..050ba342 100644 --- a/src/bittorrent_helper.cc +++ b/src/bittorrent_helper.cc @@ -119,8 +119,8 @@ void extractPieceHash(const SharedHandle<DownloadContext>& ctx, std::vector<std::string> pieceHashes; pieceHashes.reserve(numPieces); for(size_t i = 0; i < numPieces; ++i) { - pieceHashes.push_back(util::toHex(hashData.data()+i*hashLength, - hashLength)); + const char* p = hashData.data()+i*hashLength; + pieceHashes.push_back(std::string(p, p+hashLength)); } ctx->setPieceHashes("sha-1", pieceHashes.begin(), pieceHashes.end()); } diff --git a/src/download_helper.cc b/src/download_helper.cc index 8201ac58..bfecf467 100644 --- a/src/download_helper.cc +++ b/src/download_helper.cc @@ -238,7 +238,7 @@ SharedHandle<RequestGroup> createRequestGroup util::divide(p, checksum, '='); util::lowercase(p.first); util::lowercase(p.second); - dctx->setDigest(p.first, p.second); + dctx->setDigest(p.first, util::fromHex(p.second)); } #endif // ENABLE_MESSAGE_DIGEST rg->setDownloadContext(dctx); diff --git a/src/message_digest_helper.cc b/src/message_digest_helper.cc index b30d2179..b0fa8d3d 100644 --- a/src/message_digest_helper.cc +++ b/src/message_digest_helper.cc @@ -65,14 +65,14 @@ void staticSHA1DigestFree() sha1Ctx_.reset(); } -std::string staticSHA1DigestHexDigest +std::string staticSHA1Digest (const BinaryStreamHandle& bs, off_t offset, uint64_t length) { sha1Ctx_->reset(); - return hexDigest(sha1Ctx_, bs, offset, length); + return digest(sha1Ctx_, bs, offset, length); } -std::string hexDigest +std::string digest (const SharedHandle<MessageDigest>& ctx, const SharedHandle<BinaryStream>& bs, off_t offset, uint64_t length) @@ -97,7 +97,7 @@ std::string hexDigest } ctx->update(BUF, readLength); } - return ctx->hexDigest(); + return ctx->digest(); } void digest diff --git a/src/message_digest_helper.h b/src/message_digest_helper.h index 27f9ed81..4bf5d582 100644 --- a/src/message_digest_helper.h +++ b/src/message_digest_helper.h @@ -62,14 +62,14 @@ void staticSHA1DigestInit(); */ void staticSHA1DigestFree(); -std::string staticSHA1DigestHexDigest +std::string staticSHA1Digest (const SharedHandle<BinaryStream>& bs, off_t offset, uint64_t length); /** * ctx must be initialized or reseted before calling this function. - * Returns hex digest string, not *raw* digest + * Returns raw digest string, not hex digest */ -std::string hexDigest +std::string digest (const SharedHandle<MessageDigest>& ctx, const SharedHandle<BinaryStream>& bs, off_t offset, uint64_t length); |
