Piece.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "Piece.h"
  36. #include "util.h"
  37. #include "BitfieldMan.h"
  38. #include "A2STR.h"
  39. #include "util.h"
  40. #include "a2functional.h"
  41. #ifdef ENABLE_MESSAGE_DIGEST
  42. # include "MessageDigest.h"
  43. #endif // ENABLE_MESSAGE_DIGEST
  44. namespace aria2 {
  45. Piece::Piece():index_(0), length_(0), blockLength_(BLOCK_LENGTH), bitfield_(0)
  46. #ifdef ENABLE_MESSAGE_DIGEST
  47. , nextBegin_(0)
  48. #endif // ENABLE_MESSAGE_DIGEST
  49. {}
  50. Piece::Piece(size_t index, size_t length, size_t blockLength):
  51. index_(index), length_(length), blockLength_(blockLength),
  52. bitfield_(new BitfieldMan(blockLength_, length))
  53. #ifdef ENABLE_MESSAGE_DIGEST
  54. , nextBegin_(0)
  55. #endif // ENABLE_MESSAGE_DIGEST
  56. {}
  57. Piece::~Piece()
  58. {
  59. delete bitfield_;
  60. }
  61. void Piece::completeBlock(size_t blockIndex) {
  62. bitfield_->setBit(blockIndex);
  63. bitfield_->unsetUseBit(blockIndex);
  64. }
  65. void Piece::clearAllBlock() {
  66. bitfield_->clearAllBit();
  67. bitfield_->clearAllUseBit();
  68. }
  69. void Piece::setAllBlock() {
  70. bitfield_->setAllBit();
  71. }
  72. bool Piece::pieceComplete() const {
  73. return bitfield_->isAllBitSet();
  74. }
  75. size_t Piece::countBlock() const
  76. {
  77. return bitfield_->countBlock();
  78. }
  79. size_t Piece::getBlockLength(size_t index) const
  80. {
  81. return bitfield_->getBlockLength(index);
  82. }
  83. size_t Piece::getBlockLength() const
  84. {
  85. return bitfield_->getBlockLength();
  86. }
  87. const unsigned char* Piece::getBitfield() const
  88. {
  89. return bitfield_->getBitfield();
  90. }
  91. size_t Piece::getBitfieldLength() const
  92. {
  93. return bitfield_->getBitfieldLength();
  94. }
  95. bool Piece::isBlockUsed(size_t index) const
  96. {
  97. return bitfield_->isUseBitSet(index);
  98. }
  99. void Piece::cancelBlock(size_t blockIndex) {
  100. bitfield_->unsetUseBit(blockIndex);
  101. }
  102. size_t Piece::countCompleteBlock() const
  103. {
  104. return bitfield_->countBlock()-bitfield_->countMissingBlock();
  105. }
  106. size_t Piece::countMissingBlock() const
  107. {
  108. return bitfield_->countMissingBlock();
  109. }
  110. bool Piece::hasBlock(size_t blockIndex) const
  111. {
  112. return bitfield_->isBitSet(blockIndex);
  113. }
  114. bool Piece::getMissingUnusedBlockIndex(size_t& index) const
  115. {
  116. if(bitfield_->getFirstMissingUnusedIndex(index)) {
  117. bitfield_->setUseBit(index);
  118. return true;
  119. } else {
  120. return false;
  121. }
  122. }
  123. size_t Piece::getMissingUnusedBlockIndex
  124. (std::vector<size_t>& indexes, size_t n) const
  125. {
  126. size_t num = bitfield_->getFirstNMissingUnusedIndex(indexes, n);
  127. if(num) {
  128. for(std::vector<size_t>::const_iterator i = indexes.end()-num,
  129. eoi = indexes.end(); i != eoi; ++i) {
  130. bitfield_->setUseBit(*i);
  131. }
  132. }
  133. return num;
  134. }
  135. bool Piece::getFirstMissingBlockIndexWithoutLock(size_t& index) const
  136. {
  137. return bitfield_->getFirstMissingIndex(index);
  138. }
  139. bool Piece::getAllMissingBlockIndexes
  140. (unsigned char* misbitfield, size_t mislen) const
  141. {
  142. return bitfield_->getAllMissingIndexes(misbitfield, mislen);
  143. }
  144. std::string Piece::toString() const {
  145. return strconcat("piece: index=", util::itos(index_),
  146. ", length=", util::itos(length_));
  147. }
  148. void Piece::reconfigure(size_t length)
  149. {
  150. delete bitfield_;
  151. length_ = length;
  152. bitfield_ = new BitfieldMan(blockLength_, length_);
  153. }
  154. void Piece::setBitfield(const unsigned char* bitfield, size_t len)
  155. {
  156. bitfield_->setBitfield(bitfield, len);
  157. }
  158. size_t Piece::getCompletedLength()
  159. {
  160. return bitfield_->getCompletedLength();
  161. }
  162. #ifdef ENABLE_MESSAGE_DIGEST
  163. void Piece::setHashAlgo(const std::string& algo)
  164. {
  165. hashAlgo_ = algo;
  166. }
  167. bool Piece::updateHash
  168. (uint32_t begin, const unsigned char* data, size_t dataLength)
  169. {
  170. if(hashAlgo_.empty()) {
  171. return false;
  172. }
  173. if(begin == nextBegin_ && nextBegin_+dataLength <= length_) {
  174. if(mdctx_.isNull()) {
  175. mdctx_ = MessageDigest::create(hashAlgo_);
  176. }
  177. mdctx_->update(data, dataLength);
  178. nextBegin_ += dataLength;
  179. return true;
  180. } else {
  181. return false;
  182. }
  183. }
  184. bool Piece::isHashCalculated() const
  185. {
  186. return !mdctx_.isNull() && nextBegin_ == length_;
  187. }
  188. std::string Piece::getHashString()
  189. {
  190. if(mdctx_.isNull()) {
  191. return A2STR::NIL;
  192. } else {
  193. std::string hash = mdctx_->hexDigest();
  194. destroyHashContext();
  195. return hash;
  196. }
  197. }
  198. void Piece::destroyHashContext()
  199. {
  200. mdctx_.reset();
  201. nextBegin_ = 0;
  202. }
  203. #endif // ENABLE_MESSAGE_DIGEST
  204. } // namespace aria2