Piece.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 "BitfieldManFactory.h"
  38. #include "BitfieldMan.h"
  39. #include "A2STR.h"
  40. #include "util.h"
  41. #include "a2functional.h"
  42. #ifdef ENABLE_MESSAGE_DIGEST
  43. # include "messageDigest.h"
  44. #endif // ENABLE_MESSAGE_DIGEST
  45. namespace aria2 {
  46. Piece::Piece():index(0), length(0), _blockLength(BLOCK_LENGTH), bitfield(0)
  47. #ifdef ENABLE_MESSAGE_DIGEST
  48. , _nextBegin(0)
  49. #endif // ENABLE_MESSAGE_DIGEST
  50. {}
  51. Piece::Piece(size_t index, size_t length, size_t blockLength):index(index), length(length), _blockLength(blockLength)
  52. #ifdef ENABLE_MESSAGE_DIGEST
  53. , _nextBegin(0)
  54. #endif // ENABLE_MESSAGE_DIGEST
  55. {
  56. bitfield =
  57. BitfieldManFactory::getFactoryInstance()->createBitfieldMan(_blockLength, length);
  58. }
  59. Piece::Piece(const Piece& piece) {
  60. index = piece.index;
  61. length = piece.length;
  62. _blockLength = piece._blockLength;
  63. if(piece.bitfield == 0) {
  64. bitfield = 0;
  65. } else {
  66. bitfield = new BitfieldMan(*piece.bitfield);
  67. }
  68. #ifdef ENABLE_MESSAGE_DIGEST
  69. _nextBegin = piece._nextBegin;
  70. // TODO Is this OK?
  71. _mdctx = piece._mdctx;
  72. #endif // ENABLE_MESSAGE_DIGEST
  73. }
  74. Piece::~Piece()
  75. {
  76. delete bitfield;
  77. }
  78. Piece& Piece::operator=(const Piece& piece)
  79. {
  80. if(this != &piece) {
  81. index = piece.index;
  82. length = piece.length;
  83. delete bitfield;
  84. if(piece.bitfield) {
  85. bitfield = new BitfieldMan(*piece.bitfield);
  86. } else {
  87. bitfield = 0;
  88. }
  89. }
  90. return *this;
  91. }
  92. void Piece::completeBlock(size_t blockIndex) {
  93. bitfield->setBit(blockIndex);
  94. bitfield->unsetUseBit(blockIndex);
  95. }
  96. void Piece::clearAllBlock() {
  97. bitfield->clearAllBit();
  98. bitfield->clearAllUseBit();
  99. }
  100. void Piece::setAllBlock() {
  101. bitfield->setAllBit();
  102. }
  103. bool Piece::pieceComplete() const {
  104. return bitfield->isAllBitSet();
  105. }
  106. size_t Piece::countBlock() const
  107. {
  108. return bitfield->countBlock();
  109. }
  110. size_t Piece::getBlockLength(size_t index) const
  111. {
  112. return bitfield->getBlockLength(index);
  113. }
  114. size_t Piece::getBlockLength() const
  115. {
  116. return bitfield->getBlockLength();
  117. }
  118. const unsigned char* Piece::getBitfield() const
  119. {
  120. return bitfield->getBitfield();
  121. }
  122. size_t Piece::getBitfieldLength() const
  123. {
  124. return bitfield->getBitfieldLength();
  125. }
  126. bool Piece::isBlockUsed(size_t index) const
  127. {
  128. return bitfield->isUseBitSet(index);
  129. }
  130. void Piece::cancelBlock(size_t blockIndex) {
  131. bitfield->unsetUseBit(blockIndex);
  132. }
  133. size_t Piece::countCompleteBlock() const
  134. {
  135. return bitfield->countBlock()-bitfield->countMissingBlock();
  136. }
  137. size_t Piece::countMissingBlock() const
  138. {
  139. return bitfield->countMissingBlock();
  140. }
  141. bool Piece::hasBlock(size_t blockIndex) const
  142. {
  143. return bitfield->isBitSet(blockIndex);
  144. }
  145. bool Piece::getMissingUnusedBlockIndex(size_t& index) const
  146. {
  147. if(bitfield->getFirstMissingUnusedIndex(index)) {
  148. bitfield->setUseBit(index);
  149. return true;
  150. } else {
  151. return false;
  152. }
  153. }
  154. size_t Piece::getMissingUnusedBlockIndex
  155. (std::vector<size_t>& indexes, size_t n) const
  156. {
  157. size_t num = bitfield->getFirstNMissingUnusedIndex(indexes, n);
  158. if(num) {
  159. for(std::vector<size_t>::const_iterator i = indexes.end()-num;
  160. i != indexes.end(); ++i) {
  161. bitfield->setUseBit(*i);
  162. }
  163. }
  164. return num;
  165. }
  166. bool Piece::getFirstMissingBlockIndexWithoutLock(size_t& index) const
  167. {
  168. return bitfield->getFirstMissingIndex(index);
  169. }
  170. bool Piece::getAllMissingBlockIndexes
  171. (unsigned char* misbitfield, size_t mislen) const
  172. {
  173. return bitfield->getAllMissingIndexes(misbitfield, mislen);
  174. }
  175. std::string Piece::toString() const {
  176. return strconcat("piece: index=", util::itos(index),
  177. ", length=", util::itos(length));
  178. }
  179. void Piece::reconfigure(size_t length)
  180. {
  181. delete bitfield;
  182. this->length = length;
  183. bitfield =
  184. BitfieldManFactory::getFactoryInstance()->createBitfieldMan(_blockLength, length);
  185. }
  186. void Piece::setBitfield(const unsigned char* bitfield, size_t len)
  187. {
  188. this->bitfield->setBitfield(bitfield, len);
  189. }
  190. size_t Piece::getCompletedLength()
  191. {
  192. return bitfield->getCompletedLength();
  193. }
  194. #ifdef ENABLE_MESSAGE_DIGEST
  195. void Piece::setHashAlgo(const std::string& algo)
  196. {
  197. _hashAlgo = algo;
  198. }
  199. bool Piece::updateHash(uint32_t begin, const unsigned char* data, size_t dataLength)
  200. {
  201. if(_hashAlgo.empty()) {
  202. return false;
  203. }
  204. if(begin == _nextBegin && _nextBegin+dataLength <= length) {
  205. if(_mdctx.isNull()) {
  206. _mdctx.reset(new MessageDigestContext());
  207. _mdctx->trySetAlgo(_hashAlgo);
  208. _mdctx->digestInit();
  209. }
  210. _mdctx->digestUpdate(data, dataLength);
  211. _nextBegin += dataLength;
  212. return true;
  213. } else {
  214. return false;
  215. }
  216. }
  217. bool Piece::isHashCalculated() const
  218. {
  219. return !_mdctx.isNull() && _nextBegin == length;
  220. }
  221. // TODO should be getHashString()
  222. std::string Piece::getHashString()
  223. {
  224. if(_mdctx.isNull()) {
  225. return A2STR::NIL;
  226. } else {
  227. return util::toHex(_mdctx->digestFinal());
  228. }
  229. }
  230. void Piece::destroyHashContext()
  231. {
  232. _mdctx.reset();
  233. _nextBegin = 0;
  234. }
  235. #endif // ENABLE_MESSAGE_DIGEST
  236. } // namespace aria2