Piece.cc 6.4 KB

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