2006-03-21 14:12:51 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
|
|
|
* aria2 - a simple utility for downloading files faster
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
/* copyright --> */
|
|
|
|
#include "BitfieldMan.h"
|
2006-05-24 15:18:58 +00:00
|
|
|
#include "Util.h"
|
2006-03-21 14:12:51 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
BitfieldMan::BitfieldMan(int blockLength, long long int totalLength)
|
2006-04-06 12:52:16 +00:00
|
|
|
:blockLength(blockLength), totalLength(totalLength), filterBitfield(0),
|
|
|
|
filterEnabled(false) {
|
2006-03-21 14:12:51 +00:00
|
|
|
if(blockLength > 0 && totalLength > 0) {
|
|
|
|
blocks = totalLength/blockLength+(totalLength%blockLength ? 1 : 0);
|
|
|
|
bitfieldLength = blocks/8+(blocks%8 ? 1 : 0);
|
|
|
|
bitfield = new unsigned char[bitfieldLength];
|
|
|
|
useBitfield = new unsigned char[bitfieldLength];
|
|
|
|
memset(bitfield, 0, bitfieldLength);
|
|
|
|
memset(useBitfield, 0, bitfieldLength);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BitfieldMan::BitfieldMan(const BitfieldMan& bitfieldMan) {
|
|
|
|
blockLength = bitfieldMan.blockLength;
|
|
|
|
totalLength = bitfieldMan.totalLength;
|
|
|
|
blocks = bitfieldMan.blocks;
|
|
|
|
bitfieldLength = bitfieldMan.bitfieldLength;
|
|
|
|
bitfield = new unsigned char[bitfieldLength];
|
|
|
|
useBitfield = new unsigned char[bitfieldLength];
|
|
|
|
memcpy(bitfield, bitfieldMan.bitfield, bitfieldLength);
|
|
|
|
memcpy(useBitfield, bitfieldMan.useBitfield, bitfieldLength);
|
2006-04-06 12:52:16 +00:00
|
|
|
filterEnabled = bitfieldMan.filterEnabled;
|
|
|
|
if(bitfieldMan.filterBitfield) {
|
|
|
|
filterBitfield = new unsigned char[bitfieldLength];
|
|
|
|
memcpy(filterBitfield, bitfieldMan.filterBitfield, bitfieldLength);
|
|
|
|
} else {
|
|
|
|
filterBitfield = 0;
|
|
|
|
}
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BitfieldMan::~BitfieldMan() {
|
|
|
|
delete [] bitfield;
|
|
|
|
delete [] useBitfield;
|
2006-04-06 12:52:16 +00:00
|
|
|
if(filterBitfield) {
|
|
|
|
delete [] filterBitfield;
|
|
|
|
}
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int BitfieldMan::countSetBit(const unsigned char* bitfield, int len) const {
|
|
|
|
int count = 0;
|
2006-05-24 15:18:58 +00:00
|
|
|
int size = sizeof(unsigned int);
|
|
|
|
for(int i = 0; i < len/size; i++) {
|
|
|
|
count += Util::countBit(*(unsigned int*)&bitfield[i*size]);
|
|
|
|
}
|
|
|
|
for(int i = len-len%size; i < len; i++) {
|
|
|
|
count += Util::countBit((unsigned int)bitfield[i]);
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2006-07-30 12:58:27 +00:00
|
|
|
int BitfieldMan::getNthBitIndex(const unsigned char bitfield, int nth) const {
|
2006-03-21 14:12:51 +00:00
|
|
|
int index = -1;
|
2006-07-30 12:58:27 +00:00
|
|
|
for(int bs = 7; bs >= 0; bs--) {
|
|
|
|
unsigned char mask = 1 << bs;
|
|
|
|
if(bitfield & mask) {
|
|
|
|
nth--;
|
|
|
|
if(nth == 0) {
|
|
|
|
index = 7-bs;
|
|
|
|
break;
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2006-07-30 12:58:27 +00:00
|
|
|
int
|
|
|
|
BitfieldMan::getMissingIndexRandomly(const unsigned char* bitfield,
|
|
|
|
int bitfieldLength) const
|
|
|
|
{
|
|
|
|
int byte = (int)(((double)bitfieldLength)*random()/(RAND_MAX+1.0));
|
2006-05-24 15:18:58 +00:00
|
|
|
|
2006-07-30 12:58:27 +00:00
|
|
|
for(int i = 0; i < bitfieldLength; i++) {
|
|
|
|
unsigned char mask = 0xff;
|
|
|
|
if(bitfield[byte]&mask) {
|
|
|
|
int index = byte*8+getNthBitIndex(bitfield[byte], 1);
|
|
|
|
return index;
|
2006-05-24 15:18:58 +00:00
|
|
|
}
|
2006-07-30 12:58:27 +00:00
|
|
|
byte++;
|
|
|
|
if(byte == bitfieldLength) {
|
|
|
|
byte = 0;
|
2006-05-24 15:18:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BitfieldMan::hasMissingPiece(const unsigned char* peerBitfield, int length) const {
|
|
|
|
if(bitfieldLength != length) {
|
|
|
|
return false;
|
|
|
|
}
|
2006-07-30 12:58:27 +00:00
|
|
|
bool retval = false;
|
2006-05-24 15:18:58 +00:00
|
|
|
for(int i = 0; i < bitfieldLength; i++) {
|
2006-07-30 12:58:27 +00:00
|
|
|
unsigned char temp = peerBitfield[i] & ~bitfield[i];
|
2006-05-24 15:18:58 +00:00
|
|
|
if(filterEnabled) {
|
2006-07-30 12:58:27 +00:00
|
|
|
temp &= filterBitfield[i];
|
2006-05-24 15:18:58 +00:00
|
|
|
}
|
2006-07-30 12:58:27 +00:00
|
|
|
if(temp&0xff) {
|
2006-05-24 15:18:58 +00:00
|
|
|
retval = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2006-03-21 14:12:51 +00:00
|
|
|
int BitfieldMan::getMissingIndex(const unsigned char* peerBitfield, int length) const {
|
|
|
|
if(bitfieldLength != length) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
unsigned char* tempBitfield = new unsigned char[bitfieldLength];
|
|
|
|
for(int i = 0; i < bitfieldLength; i++) {
|
|
|
|
tempBitfield[i] = peerBitfield[i] & ~bitfield[i];
|
2006-04-06 12:52:16 +00:00
|
|
|
if(filterEnabled) {
|
|
|
|
tempBitfield[i] &= filterBitfield[i];
|
|
|
|
}
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
2006-07-30 12:58:27 +00:00
|
|
|
int index = getMissingIndexRandomly(tempBitfield, bitfieldLength);
|
2006-03-23 10:47:25 +00:00
|
|
|
delete [] tempBitfield;
|
2006-03-21 14:12:51 +00:00
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
int BitfieldMan::getMissingUnusedIndex(const unsigned char* peerBitfield, int length) const {
|
|
|
|
if(bitfieldLength != length) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
unsigned char* tempBitfield = new unsigned char[bitfieldLength];
|
|
|
|
for(int i = 0; i < bitfieldLength; i++) {
|
|
|
|
tempBitfield[i] = peerBitfield[i] & ~bitfield[i] & ~useBitfield[i];
|
2006-04-06 12:52:16 +00:00
|
|
|
if(filterEnabled) {
|
|
|
|
tempBitfield[i] &= filterBitfield[i];
|
|
|
|
}
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
2006-07-30 12:58:27 +00:00
|
|
|
int index = getMissingIndexRandomly(tempBitfield, bitfieldLength);
|
2006-03-23 10:47:25 +00:00
|
|
|
delete [] tempBitfield;
|
2006-03-21 14:12:51 +00:00
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
int BitfieldMan::getFirstMissingUnusedIndex(const unsigned char* peerBitfield, int length) const {
|
|
|
|
if(bitfieldLength != length) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
for(int i = 0; i < bitfieldLength; i++) {
|
|
|
|
unsigned char bit = peerBitfield[i] & ~bitfield[i] & ~useBitfield[i];
|
2006-04-06 12:52:16 +00:00
|
|
|
if(filterEnabled) {
|
|
|
|
bit &= filterBitfield[i];
|
|
|
|
}
|
2006-03-21 14:12:51 +00:00
|
|
|
for(int bs = 7; bs >= 0 && i*8+7-bs < blocks; bs--) {
|
|
|
|
unsigned char mask = 1 << bs;
|
|
|
|
if(bit & mask) {
|
|
|
|
return i*8+7-bs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int BitfieldMan::getFirstMissingUnusedIndex() const {
|
|
|
|
for(int i = 0; i < bitfieldLength; i++) {
|
|
|
|
unsigned char bit = ~bitfield[i] & ~useBitfield[i];
|
2006-04-06 12:52:16 +00:00
|
|
|
if(filterEnabled) {
|
|
|
|
bit &= filterBitfield[i];
|
|
|
|
}
|
2006-03-21 14:12:51 +00:00
|
|
|
for(int bs = 7; bs >= 0 && i*8+7-bs < blocks; bs--) {
|
|
|
|
unsigned char mask = 1 << bs;
|
|
|
|
if(bit & mask) {
|
|
|
|
return i*8+7-bs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-05-21 16:19:17 +00:00
|
|
|
int BitfieldMan::getMissingIndex() const {
|
|
|
|
unsigned char* tempBitfield = new unsigned char[bitfieldLength];
|
|
|
|
for(int i = 0; i < bitfieldLength; i++) {
|
|
|
|
tempBitfield[i] = ~bitfield[i];
|
|
|
|
if(filterEnabled) {
|
|
|
|
tempBitfield[i] &= filterBitfield[i];
|
|
|
|
}
|
|
|
|
}
|
2006-07-30 12:58:27 +00:00
|
|
|
int index = getMissingIndexRandomly(tempBitfield, bitfieldLength);
|
2006-05-21 16:19:17 +00:00
|
|
|
delete [] tempBitfield;
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2006-03-22 16:21:11 +00:00
|
|
|
BlockIndexes BitfieldMan::getAllMissingIndexes() const {
|
|
|
|
BlockIndexes missingIndexes;
|
2006-03-21 14:12:51 +00:00
|
|
|
for(int i = 0; i < bitfieldLength; i++) {
|
|
|
|
unsigned char bit = ~bitfield[i];
|
2006-04-06 12:52:16 +00:00
|
|
|
if(filterEnabled) {
|
|
|
|
bit &= filterBitfield[i];
|
|
|
|
}
|
2006-03-21 14:12:51 +00:00
|
|
|
for(int bs = 7; bs >= 0 && i*8+7-bs < blocks; bs--) {
|
|
|
|
unsigned char mask = 1 << bs;
|
|
|
|
if(bit & mask) {
|
|
|
|
missingIndexes.push_back(i*8+7-bs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return missingIndexes;
|
|
|
|
}
|
|
|
|
|
2006-05-24 15:18:58 +00:00
|
|
|
BlockIndexes BitfieldMan::getAllMissingIndexes(const unsigned char* peerBitfield, int peerBitfieldLength) const {
|
|
|
|
BlockIndexes missingIndexes;
|
|
|
|
if(bitfieldLength != peerBitfieldLength) {
|
|
|
|
return missingIndexes;
|
|
|
|
}
|
|
|
|
for(int i = 0; i < bitfieldLength; i++) {
|
|
|
|
unsigned char bit = peerBitfield[i] & ~bitfield[i];
|
|
|
|
if(filterEnabled) {
|
|
|
|
bit &= filterBitfield[i];
|
|
|
|
}
|
|
|
|
for(int bs = 7; bs >= 0 && i*8+7-bs < blocks; bs--) {
|
|
|
|
unsigned char mask = 1 << bs;
|
|
|
|
if(bit & mask) {
|
|
|
|
missingIndexes.push_back(i*8+7-bs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return missingIndexes;
|
|
|
|
}
|
|
|
|
|
2006-03-21 14:12:51 +00:00
|
|
|
int BitfieldMan::countMissingBlock() const {
|
2006-04-06 12:52:16 +00:00
|
|
|
if(filterEnabled) {
|
|
|
|
unsigned char* temp = new unsigned char[bitfieldLength];
|
|
|
|
for(int i = 0; i < bitfieldLength; i++) {
|
|
|
|
temp[i] = bitfield[i]&filterBitfield[i];
|
|
|
|
}
|
|
|
|
int count = countSetBit(filterBitfield, bitfieldLength)-
|
|
|
|
countSetBit(temp, bitfieldLength);
|
|
|
|
delete [] temp;
|
|
|
|
return count;
|
|
|
|
} else {
|
|
|
|
return blocks-countSetBit(bitfield, bitfieldLength);
|
|
|
|
}
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
|
|
|
|
2006-04-06 12:52:16 +00:00
|
|
|
int BitfieldMan::countBlock() const {
|
|
|
|
if(filterEnabled) {
|
|
|
|
return countSetBit(filterBitfield, bitfieldLength);
|
|
|
|
} else {
|
|
|
|
return blocks;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BitfieldMan::setBitInternal(unsigned char* bitfield, int index, bool on) {
|
2006-03-21 14:12:51 +00:00
|
|
|
if(blocks <= index) { return false; }
|
|
|
|
unsigned char mask = 128 >> index%8;
|
2006-04-06 12:52:16 +00:00
|
|
|
if(on) {
|
|
|
|
bitfield[index/8] |= mask;
|
|
|
|
} else {
|
|
|
|
bitfield[index/8] &= ~mask;
|
|
|
|
}
|
2006-03-21 14:12:51 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-04-06 12:52:16 +00:00
|
|
|
bool BitfieldMan::setUseBit(int index) {
|
|
|
|
return setBitInternal(useBitfield, index, true);
|
|
|
|
}
|
|
|
|
|
2006-03-21 14:12:51 +00:00
|
|
|
bool BitfieldMan::unsetUseBit(int index) {
|
2006-04-06 12:52:16 +00:00
|
|
|
return setBitInternal(useBitfield, index, false);
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool BitfieldMan::setBit(int index) {
|
2006-04-06 12:52:16 +00:00
|
|
|
return setBitInternal(bitfield, index, true);
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
2006-04-06 12:52:16 +00:00
|
|
|
|
2006-03-21 14:12:51 +00:00
|
|
|
bool BitfieldMan::unsetBit(int index) {
|
2006-04-06 12:52:16 +00:00
|
|
|
return setBitInternal(bitfield, index, false);
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool BitfieldMan::isAllBitSet() const {
|
2006-04-06 12:52:16 +00:00
|
|
|
if(filterEnabled) {
|
2006-04-16 14:38:19 +00:00
|
|
|
for(int i = 0; i < bitfieldLength; i++) {
|
2006-04-06 12:52:16 +00:00
|
|
|
if((bitfield[i]&filterBitfield[i]) != filterBitfield[i]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
for(int i = 0; i < bitfieldLength-1; i++) {
|
|
|
|
if(bitfield[i] != 0xff) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unsigned char b = ~((128 >> (blocks-1)%8)-1);
|
|
|
|
if(bitfield[bitfieldLength-1] != b) {
|
2006-03-21 14:12:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
2006-04-06 12:52:16 +00:00
|
|
|
return true;
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BitfieldMan::isBitSetInternal(const unsigned char* bitfield, int index) const {
|
|
|
|
if(blocks <= index) { return false; }
|
|
|
|
unsigned char mask = 128 >> index%8;
|
|
|
|
return (bitfield[index/8] & mask) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BitfieldMan::isBitSet(int index) const {
|
|
|
|
return isBitSetInternal(bitfield, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BitfieldMan::isUseBitSet(int index) const {
|
|
|
|
return isBitSetInternal(useBitfield, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BitfieldMan::setBitfield(const unsigned char* bitfield, int bitfieldLength) {
|
|
|
|
if(this->bitfieldLength != bitfieldLength) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
memcpy(this->bitfield, bitfield, this->bitfieldLength);
|
|
|
|
memset(this->useBitfield, 0, this->bitfieldLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BitfieldMan::clearAllBit() {
|
|
|
|
memset(this->bitfield, 0, this->bitfieldLength);
|
|
|
|
memset(this->useBitfield, 0, this->bitfieldLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BitfieldMan::setAllBit() {
|
|
|
|
for(int i = 0; i < blocks; i++) {
|
|
|
|
setBit(i);
|
|
|
|
}
|
|
|
|
}
|
2006-04-06 12:52:16 +00:00
|
|
|
|
|
|
|
bool BitfieldMan::setFilterBit(int index) {
|
|
|
|
return setBitInternal(filterBitfield, index, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BitfieldMan::addFilter(long long int offset, long long int length) {
|
|
|
|
if(!filterBitfield) {
|
|
|
|
filterBitfield = new unsigned char[bitfieldLength];
|
|
|
|
memset(filterBitfield, 0, bitfieldLength);
|
|
|
|
}
|
|
|
|
int startBlock = offset/blockLength;
|
|
|
|
int endBlock = (offset+length-1)/blockLength;
|
|
|
|
for(int i = startBlock; i <= endBlock && i < blocks; i++) {
|
|
|
|
setFilterBit(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BitfieldMan::enableFilter() {
|
|
|
|
filterEnabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BitfieldMan::disableFilter() {
|
|
|
|
filterEnabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BitfieldMan::clearFilter() {
|
|
|
|
if(filterBitfield) {
|
|
|
|
delete [] filterBitfield;
|
|
|
|
filterBitfield = 0;
|
|
|
|
}
|
|
|
|
filterEnabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BitfieldMan::isFilterEnabled() const {
|
|
|
|
return filterEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
long long int BitfieldMan::getFilteredTotalLength() const {
|
|
|
|
if(!filterBitfield) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int filteredBlocks = countSetBit(filterBitfield, bitfieldLength);
|
|
|
|
if(filteredBlocks == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(isBitSetInternal(filterBitfield, blocks-1)) {
|
|
|
|
return ((long long int)filteredBlocks-1)*blockLength+getLastBlockLength();
|
|
|
|
} else {
|
|
|
|
return ((long long int)filteredBlocks)*blockLength;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
long long int BitfieldMan::getCompletedLength() const {
|
|
|
|
unsigned char* temp = new unsigned char[bitfieldLength];
|
|
|
|
for(int i = 0; i < bitfieldLength; i++) {
|
|
|
|
temp[i] = bitfield[i];
|
|
|
|
if(filterEnabled) {
|
|
|
|
temp[i] &= filterBitfield[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int completedBlocks = countSetBit(temp, bitfieldLength);
|
|
|
|
long long int completedLength = 0;
|
|
|
|
if(completedBlocks == 0) {
|
|
|
|
completedLength = 0;
|
|
|
|
} else {
|
|
|
|
if(isBitSetInternal(temp, blocks-1)) {
|
|
|
|
completedLength = ((long long int)completedBlocks-1)*blockLength+getLastBlockLength();
|
|
|
|
} else {
|
|
|
|
completedLength = ((long long int)completedBlocks)*blockLength;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete [] temp;
|
|
|
|
return completedLength;
|
|
|
|
}
|