Piece.cc 6.4 KB

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