2008-03-07 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>

Use unsigned char for data store.
pull/1/head
Tatsuhiro Tsujikawa 2008-03-07 12:05:50 +00:00
parent 7578d5fb22
commit fc0fa4203a
86 changed files with 378 additions and 312 deletions

View File

@ -1,3 +1,7 @@
2008-03-07 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Use unsigned char for data store.
2008-03-07 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Bump up version number to 0.13.0+1

View File

@ -91,7 +91,7 @@ public:
/**
* Processes the repsponse from the tracker.
*/
virtual void processAnnounceResponse(const char* trackerResponse,
virtual void processAnnounceResponse(const unsigned char* trackerResponse,
size_t trackerResponseLength) = 0;
/**

View File

@ -69,7 +69,7 @@ bool BtDependency::resolve()
DiskAdaptorHandle diskAdaptor = dependee->getPieceStorage()->getDiskAdaptor();
diskAdaptor->openExistingFile();
std::string content = Util::toString(diskAdaptor);
btContext->loadFromMemory(content.c_str(), content.size(),
btContext->loadFromMemory(content,
File(dependee->getFilePath()).getBasename());
if(_option->defined(PREF_PEER_ID_PREFIX)) {
btContext->setPeerIdPrefix(_option->get(PREF_PEER_ID_PREFIX));

View File

@ -97,12 +97,12 @@ std::string BtExtendedMessage::toString() const {
BtExtendedMessageHandle
BtExtendedMessage::create(const BtContextHandle& btContext,
const PeerHandle& peer,
const char* data, size_t dataLength)
const unsigned char* data, size_t dataLength)
{
if(dataLength < 2) {
throw new DlAbortEx(MSG_TOO_SMALL_PAYLOAD_SIZE, "extended", dataLength);
}
int8_t id = PeerMessageUtil::getId((const unsigned char*)data);
int8_t id = PeerMessageUtil::getId(data);
if(id != ID) {
throw new DlAbortEx(EX_INVALID_BT_MESSAGE_ID, id, "extended", ID);
}

View File

@ -60,7 +60,7 @@ public:
static BtExtendedMessageHandle create(const SharedHandle<BtContext>& btContext,
const SharedHandle<Peer>& peer,
const char* data,
const unsigned char* data,
size_t dataLength);
virtual int8_t getId() { return ID; }

View File

@ -73,8 +73,7 @@ RequestGroups BtPostDownloadHandler::getNextRequestGroups(RequestGroup* requestG
throw;
}
DefaultBtContextHandle btContext = new DefaultBtContext();
btContext->loadFromMemory(content.c_str(), content.size(),
File(requestGroup->getFilePath()).getBasename());
btContext->loadFromMemory(content, File(requestGroup->getFilePath()).getBasename());
if(op->defined(PREF_PEER_ID_PREFIX)) {
btContext->setPeerIdPrefix(op->get(PREF_PEER_ID_PREFIX));
}

View File

@ -44,7 +44,7 @@ namespace aria2 {
ChunkedEncoding::ChunkedEncoding() {
strbufSize = 4096;
strbuf = new char[strbufSize];
strbuf = new unsigned char[strbufSize];
strbufTail = strbuf;
state = READ_SIZE;
chunkSize = 0;
@ -63,9 +63,10 @@ bool ChunkedEncoding::finished() {
void ChunkedEncoding::end() {}
void ChunkedEncoding::inflate(char* outbuf, int32_t& outlen, const char* inbuf, int32_t inlen) {
void ChunkedEncoding::inflate(unsigned char* outbuf, int32_t& outlen,
const unsigned char* inbuf, int32_t inlen) {
addBuffer(inbuf, inlen);
char* p = strbuf;
unsigned char* p = strbuf;
int32_t clen = 0;
while(1) {
if(state == READ_SIZE) {
@ -98,7 +99,7 @@ void ChunkedEncoding::inflate(char* outbuf, int32_t& outlen, const char* inbuf,
} else {
// copy string between [p, strbufTail]
int32_t unreadSize = strbufTail-p;
char* temp = new char[strbufSize];
unsigned char* temp = new unsigned char[strbufSize];
memcpy(temp, p, unreadSize);
delete [] strbuf;
strbuf = temp;
@ -107,7 +108,10 @@ void ChunkedEncoding::inflate(char* outbuf, int32_t& outlen, const char* inbuf,
outlen = clen;
}
int32_t ChunkedEncoding::readData(char** pp, char* buf, int32_t& len, int32_t maxlen) {
int32_t ChunkedEncoding::readData(unsigned char** pp,
unsigned char* buf, int32_t& len,
int32_t maxlen)
{
if(buf+len == buf+maxlen) {
return -1;
}
@ -131,9 +135,9 @@ int32_t ChunkedEncoding::readData(char** pp, char* buf, int32_t& len, int32_t ma
}
}
int32_t ChunkedEncoding::readDataEOL(char** pp) {
char* np = (char*)memchr(*pp, '\n', strbufTail-*pp);
char* rp = (char*)memchr(*pp, '\r', strbufTail-*pp);
int32_t ChunkedEncoding::readDataEOL(unsigned char** pp) {
unsigned char* np = reinterpret_cast<unsigned char*>(memchr(*pp, '\n', strbufTail-*pp));
unsigned char* rp = reinterpret_cast<unsigned char*>(memchr(*pp, '\r', strbufTail-*pp));
if(np != NULL && rp != NULL && np-rp == 1 && *pp == rp) {
*pp += 2;
return 0;
@ -144,18 +148,18 @@ int32_t ChunkedEncoding::readDataEOL(char** pp) {
}
}
int32_t ChunkedEncoding::readChunkSize(char** pp) {
int32_t ChunkedEncoding::readChunkSize(unsigned char** pp) {
// we read chunk-size from *pp
char* p;
char* np = (char*)memchr(*pp, '\n', strbufTail-*pp);
char* rp = (char*)memchr(*pp, '\r', strbufTail-*pp);
unsigned char* p;
unsigned char* np = reinterpret_cast<unsigned char*>(memchr(*pp, '\n', strbufTail-*pp));
unsigned char* rp = reinterpret_cast<unsigned char*>(memchr(*pp, '\r', strbufTail-*pp));
if(np == NULL || rp == NULL || np-rp != 1) {
// \r\n is not found. Return -1
return -1;
}
p = rp;
// We ignore chunk-extension
char* exsp = (char*)memchr(*pp, ';', strbufTail-*pp);
unsigned char* exsp = reinterpret_cast<unsigned char*>(memchr(*pp, ';', strbufTail-*pp));
if(exsp == 0 || p < exsp) {
exsp = p;
}
@ -168,14 +172,14 @@ int32_t ChunkedEncoding::readChunkSize(char** pp) {
return 0;
}
void ChunkedEncoding::addBuffer(const char* inbuf, int32_t inlen) {
void ChunkedEncoding::addBuffer(const unsigned char* inbuf, int32_t inlen) {
int32_t realbufSize = strbufTail-strbuf;
if(realbufSize+inlen >= strbufSize) {
if(realbufSize+inlen > MAX_BUFSIZE) {
throw new DlAbortEx(EX_TOO_LARGE_CHUNK, realbufSize+inlen);
}
strbufSize = realbufSize+inlen;
char* temp = new char[strbufSize];
unsigned char* temp = new unsigned char[strbufSize];
memcpy(temp, strbuf, realbufSize);
delete [] strbuf;
strbuf = temp;

View File

@ -48,18 +48,19 @@ private:
};
int32_t chunkSize;
int32_t state;
char* strbuf;
unsigned char* strbuf;
int32_t strbufSize;
char* strbufTail;
unsigned char* strbufTail;
/**
* Returns 0 if the size of chunk is retrieved successfully,
* otherwise returns non-zero value.
*/
int32_t readChunkSize(char** pp);
int32_t readData(char** pp, char* buf, int32_t& len, int32_t maxlen);
void addBuffer(const char* inbuf, int32_t inlen);
int32_t readDataEOL(char** pp);
int32_t readChunkSize(unsigned char** pp);
int32_t readData(unsigned char** pp, unsigned char* buf, int32_t& len,
int32_t maxlen);
void addBuffer(const unsigned char* inbuf, int32_t inlen);
int32_t readDataEOL(unsigned char** pp);
public:
@ -67,7 +68,8 @@ public:
~ChunkedEncoding();
void init();
void inflate(char* outbuf, int32_t& outlen, const char* inbuf, int32_t inlen);
void inflate(unsigned char* outbuf, int32_t& outlen,
const unsigned char* inbuf, int32_t inlen);
bool finished();
void end();
};

View File

@ -66,7 +66,7 @@ std::string DHTAbstractMessage::getBencodedMessage()
void DHTAbstractMessage::send()
{
std::string message = getBencodedMessage();
_connection->sendMessage(message.c_str(),
_connection->sendMessage(reinterpret_cast<const unsigned char*>(message.c_str()),
message.size(),
_remoteNode->getIPAddress(),
_remoteNode->getPort());

View File

@ -44,9 +44,9 @@ class DHTConnection {
public:
virtual ~DHTConnection() {}
virtual ssize_t receiveMessage(char* data, size_t len, std::string& host, uint16_t& port) = 0;
virtual ssize_t receiveMessage(unsigned char* data, size_t len, std::string& host, uint16_t& port) = 0;
virtual void sendMessage(const char* data, size_t len, const std::string& host, uint16_t port) = 0;
virtual void sendMessage(const unsigned char* data, size_t len, const std::string& host, uint16_t port) = 0;
};
} // namespace aria2

View File

@ -73,7 +73,7 @@ uint16_t DHTConnectionImpl::bind(uint16_t port)
return 0;
}
ssize_t DHTConnectionImpl::receiveMessage(char* data, size_t len, std::string& host, uint16_t& port)
ssize_t DHTConnectionImpl::receiveMessage(unsigned char* data, size_t len, std::string& host, uint16_t& port)
{
if(_socket->isReadable(0)) {
std::pair<std::string, uint16_t> remoteHost;
@ -86,7 +86,7 @@ ssize_t DHTConnectionImpl::receiveMessage(char* data, size_t len, std::string& h
}
}
void DHTConnectionImpl::sendMessage(const char* data, size_t len, const std::string& host, uint16_t port)
void DHTConnectionImpl::sendMessage(const unsigned char* data, size_t len, const std::string& host, uint16_t port)
{
_socket->writeData(data, len, host, port);
}

View File

@ -58,9 +58,9 @@ public:
uint16_t bind(uint16_t port);
virtual ssize_t receiveMessage(char* data, size_t len, std::string& host, uint16_t& port);
virtual ssize_t receiveMessage(unsigned char* data, size_t len, std::string& host, uint16_t& port);
virtual void sendMessage(const char* data, size_t len, const std::string& host, uint16_t port);
virtual void sendMessage(const unsigned char* data, size_t len, const std::string& host, uint16_t port);
SharedHandle<SocketCore> getSocket() const;
};

View File

@ -66,10 +66,10 @@ void DHTFindNodeReplyMessage::doReceivedAction()
Dictionary* DHTFindNodeReplyMessage::getResponse()
{
Dictionary* a = new Dictionary();
a->put("id", new Data(reinterpret_cast<const char*>(_localNode->getID()),
DHT_ID_LENGTH));
a->put("id", new Data(_localNode->getID(), DHT_ID_LENGTH));
size_t offset = 0;
char buffer[DHTBucket::K*26];
unsigned char buffer[DHTBucket::K*26];
// TODO if _closestKNodes.size() > DHTBucket::K ??
for(std::deque<SharedHandle<DHTNode> >::const_iterator i = _closestKNodes.begin(); i != _closestKNodes.end(); ++i) {
SharedHandle<DHTNode> node = *i;
memcpy(buffer+offset, node->getID(), DHT_ID_LENGTH);

View File

@ -67,22 +67,21 @@ void DHTGetPeersReplyMessage::doReceivedAction()
Dictionary* DHTGetPeersReplyMessage::getResponse()
{
Dictionary* r = new Dictionary();
r->put("id", new Data(reinterpret_cast<const char*>(_localNode->getID()),
DHT_ID_LENGTH));
r->put("id", new Data(_localNode->getID(), DHT_ID_LENGTH));
r->put("token", new Data(_token));
if(_values.size()) {
List* valuesList = new List();
r->put("values", valuesList);
for(std::deque<SharedHandle<Peer> >::const_iterator i = _values.begin(); i != _values.end(); ++i) {
const SharedHandle<Peer>& peer = *i;
char buffer[6];
unsigned char buffer[6];
if(PeerMessageUtil::createcompact(buffer, peer->ipaddr, peer->port)) {
valuesList->add(new Data(buffer, sizeof(buffer)));
}
}
} else {
size_t offset = 0;
char buffer[DHTBucket::K*26];
unsigned char buffer[DHTBucket::K*26];
for(std::deque<SharedHandle<DHTNode> >::const_iterator i = _closestKNodes.begin(); i != _closestKNodes.end(); ++i) {
SharedHandle<DHTNode> node = *i;
memcpy(buffer+offset, node->getID(), DHT_ID_LENGTH);

View File

@ -52,7 +52,7 @@ DHTMessage::~DHTMessage() {}
void DHTMessage::generateTransactionID()
{
char tid[DHT_TRANSACTION_ID_LENGTH];
unsigned char tid[DHT_TRANSACTION_ID_LENGTH];
DHTUtil::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
_transactionID = std::string(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);
}

View File

@ -108,8 +108,8 @@ public:
const std::string& transactionID) = 0;
virtual SharedHandle<DHTMessage>
createUnknownMessage(const char* data, size_t length, const std::string& ipaddr,
uint16_t port) = 0;
createUnknownMessage(const unsigned char* data, size_t length,
const std::string& ipaddr, uint16_t port) = 0;
};
} // namespace aria2

View File

@ -160,7 +160,7 @@ SharedHandle<DHTMessage> DHTMessageFactoryImpl::createQueryMessage(const Diction
}
const Data* id = getData(getDictionary(d, "a"), "id");
validateID(id);
SharedHandle<DHTNode> remoteNode = getRemoteNode((const unsigned char*)id->toString().c_str(), ipaddr, port);
SharedHandle<DHTNode> remoteNode = getRemoteNode(id->getData(), ipaddr, port);
std::string messageType = q->toString();
std::string transactionID = t->toString();
if(messageType == "ping") {
@ -168,34 +168,31 @@ SharedHandle<DHTMessage> DHTMessageFactoryImpl::createQueryMessage(const Diction
} else if(messageType == "find_node") {
const Data* targetNodeID = getData(a, "target");
validateID(targetNodeID);
return createFindNodeMessage(remoteNode,
(const unsigned char*)targetNodeID->getData(),
return createFindNodeMessage(remoteNode, targetNodeID->getData(),
transactionID);
} else if(messageType == "get_peers") {
const Data* infoHash = getData(a, "info_hash");
validateID(infoHash);
return createGetPeersMessage(remoteNode,
(const unsigned char*)infoHash->getData(),
transactionID);
infoHash->getData(), transactionID);
} else if(messageType == "announce_peer") {
const Data* infoHash = getData(a, "info_hash");
validateID(infoHash);
const Data* port = getData(a, "port");
validatePort(port);
const Data* token = getData(a, "token");
return createAnnouncePeerMessage(remoteNode,
(const unsigned char*)infoHash->getData(),
(uint16_t)port->toInt(),
token->toString(),
transactionID);
return createAnnouncePeerMessage(remoteNode, infoHash->getData(),
static_cast<uint16_t>(port->toInt()),
token->toString(), transactionID);
} else {
throw new DlAbortEx("Unsupported message type: %s", messageType.c_str());
}
}
SharedHandle<DHTMessage> DHTMessageFactoryImpl::createResponseMessage(const std::string& messageType,
const Dictionary* d,
const SharedHandle<DHTNode>& remoteNode)
SharedHandle<DHTMessage>
DHTMessageFactoryImpl::createResponseMessage(const std::string& messageType,
const Dictionary* d,
const SharedHandle<DHTNode>& remoteNode)
{
const Data* t = getData(d, "t");
const Data* y = getData(d, "y");
@ -217,11 +214,11 @@ SharedHandle<DHTMessage> DHTMessageFactoryImpl::createResponseMessage(const std:
const Dictionary* r = getDictionary(d, "r");
const Data* id = getData(r, "id");
validateID(id);
validateIDMatch(remoteNode->getID(),
(const unsigned char*)id->toString().c_str());
validateIDMatch(remoteNode->getID(), id->getData());
std::string transactionID = t->toString();
if(messageType == "ping") {
return createPingReplyMessage(remoteNode, (const unsigned char*)id->getData(), transactionID);
return createPingReplyMessage(remoteNode,
id->getData(), transactionID);
} else if(messageType == "find_node") {
return createFindNodeReplyMessage(remoteNode, d, transactionID);
} else if(messageType == "get_peers") {
@ -289,14 +286,15 @@ DHTMessageFactoryImpl::createFindNodeReplyMessage(const SharedHandle<DHTNode>& r
return m;
}
std::deque<SharedHandle<DHTNode> > DHTMessageFactoryImpl::extractNodes(const char* src, size_t length)
std::deque<SharedHandle<DHTNode> >
DHTMessageFactoryImpl::extractNodes(const unsigned char* src, size_t length)
{
if(length%26 != 0) {
throw new DlAbortEx("Nodes length is not multiple of 26");
}
std::deque<SharedHandle<DHTNode> > nodes;
for(size_t offset = 0; offset < length; offset += 26) {
SharedHandle<DHTNode> node = new DHTNode(reinterpret_cast<const unsigned char*>(src+offset));
SharedHandle<DHTNode> node = new DHTNode(src+offset);
std::pair<std::string, uint16_t> addr =
PeerMessageUtil::unpackcompact(src+offset+DHT_ID_LENGTH);
if(addr.first.empty()) {
@ -419,7 +417,7 @@ DHTMessageFactoryImpl::createAnnouncePeerReplyMessage(const SharedHandle<DHTNode
}
SharedHandle<DHTMessage>
DHTMessageFactoryImpl::createUnknownMessage(const char* data, size_t length,
DHTMessageFactoryImpl::createUnknownMessage(const unsigned char* data, size_t length,
const std::string& ipaddr, uint16_t port)
{

View File

@ -74,7 +74,7 @@ private:
void validatePort(const Data* i) const;
std::deque<SharedHandle<DHTNode> > extractNodes(const char* src, size_t length);
std::deque<SharedHandle<DHTNode> > extractNodes(const unsigned char* src, size_t length);
void setCommonProperty(const SharedHandle<DHTAbstractMessage>& m);
@ -156,8 +156,8 @@ public:
const std::string& transactionID);
virtual SharedHandle<DHTMessage>
createUnknownMessage(const char* data, size_t length, const std::string& ipaddr,
uint16_t port);
createUnknownMessage(const unsigned char* data, size_t length,
const std::string& ipaddr, uint16_t port);
void setRoutingTable(const WeakHandle<DHTRoutingTable>& routingTable);

View File

@ -65,7 +65,7 @@ SharedHandle<DHTMessage> DHTMessageReceiver::receiveMessage()
{
std::string remoteAddr;
uint16_t remotePort;
char data[64*1024];
unsigned char data[64*1024];
ssize_t length = _connection->receiveMessage(data, sizeof(data),
remoteAddr,
remotePort);
@ -126,7 +126,8 @@ void DHTMessageReceiver::handleTimeout()
}
SharedHandle<DHTMessage>
DHTMessageReceiver::handleUnknownMessage(const char* data, size_t length,
DHTMessageReceiver::handleUnknownMessage(const unsigned char* data,
size_t length,
const std::string& remoteAddr,
uint16_t remotePort)
{

View File

@ -61,7 +61,7 @@ private:
const Logger* _logger;
SharedHandle<DHTMessage>
handleUnknownMessage(const char* data, size_t length,
handleUnknownMessage(const unsigned char* data, size_t length,
const std::string& remoteAddr, uint16_t remotePort);
public:
DHTMessageReceiver(const SharedHandle<DHTMessageTracker>& tracker);

View File

@ -126,7 +126,8 @@ void DHTRoutingTableDeserializer::deserialize(std::istream& in)
in.read(buf, 42);
continue;
}
std::pair<std::string, uint16_t> peer = PeerMessageUtil::unpackcompact(buf);
std::pair<std::string, uint16_t> peer =
PeerMessageUtil::unpackcompact(reinterpret_cast<const unsigned char*>(buf));
if(peer.first.empty()) {
// skip this entry
in.read(buf, 42);

View File

@ -101,7 +101,7 @@ void DHTRoutingTableSerializer::serialize(std::ostream& o)
const SharedHandle<DHTNode>& node = *i;
// Currently, only IPv4 address and IPv4-mapped address are saved.
// 6bytes: write IP address + port in Compact IP-address/port info form.
char compactPeer[6];
unsigned char compactPeer[6];
if(!PeerMessageUtil::createcompact(compactPeer, node->getIPAddress(), node->getPort())) {
memset(compactPeer, 0, 6);
}
@ -110,7 +110,7 @@ void DHTRoutingTableSerializer::serialize(std::ostream& o)
// 7bytes reserved
o.write(zero, 7);
// 6 bytes compact peer
o.write(compactPeer, 6);
o.write(reinterpret_cast<const char*>(compactPeer), 6);
// 2bytes reserved
o.write(zero, 2);
// 16bytes reserved

View File

@ -48,7 +48,7 @@ DHTTokenTracker::DHTTokenTracker()
memcpy(_secret[1], _secret[0], SECRET_SIZE);
}
DHTTokenTracker::DHTTokenTracker(const char* initialSecret)
DHTTokenTracker::DHTTokenTracker(const unsigned char* initialSecret)
{
memcpy(_secret[0], initialSecret, SECRET_SIZE);
memcpy(_secret[1], initialSecret, SECRET_SIZE);
@ -58,9 +58,9 @@ DHTTokenTracker::~DHTTokenTracker() {}
std::string DHTTokenTracker::generateToken(const unsigned char* infoHash,
const std::string& ipaddr, uint16_t port,
const char* secret) const
const unsigned char* secret) const
{
char src[DHT_ID_LENGTH+6+SECRET_SIZE];
unsigned char src[DHT_ID_LENGTH+6+SECRET_SIZE];
if(!PeerMessageUtil::createcompact(src+DHT_ID_LENGTH, ipaddr, port)) {
throw new DlAbortEx("Token generation failed: ipaddr=%s, port=%u",
ipaddr.c_str(), port);

View File

@ -44,15 +44,15 @@ class DHTTokenTracker {
private:
static const size_t SECRET_SIZE = 4;
char _secret[2][SECRET_SIZE];
unsigned char _secret[2][SECRET_SIZE];
std::string generateToken(const unsigned char* infoHash,
const std::string& ipaddr, uint16_t port,
const char* secret) const;
const unsigned char* secret) const;
public:
DHTTokenTracker();
DHTTokenTracker(const char* initialSecret);
DHTTokenTracker(const unsigned char* initialSecret);
~DHTTokenTracker();

View File

@ -40,7 +40,7 @@
namespace aria2 {
DHTUnknownMessage::DHTUnknownMessage(const SharedHandle<DHTNode>& localNode,
const char* data, size_t length,
const unsigned char* data, size_t length,
const std::string& ipaddr, uint16_t port):
DHTMessage(localNode, 0),
_length(length),
@ -50,7 +50,7 @@ DHTUnknownMessage::DHTUnknownMessage(const SharedHandle<DHTNode>& localNode,
if(_length == 0) {
_data = 0;
} else {
_data = new char[length];
_data = new unsigned char[length];
memcpy(_data, data, length);
}
}

View File

@ -41,14 +41,14 @@ namespace aria2 {
class DHTUnknownMessage:public DHTMessage {
private:
char* _data;
unsigned char* _data;
size_t _length;
std::string _ipaddr;
uint16_t _port;
public:
// _remoteNode is always null
DHTUnknownMessage(const SharedHandle<DHTNode>& localNode,
const char* data, size_t length,
const unsigned char* data, size_t length,
const std::string& ipaddr, uint16_t port);
virtual ~DHTUnknownMessage();

View File

@ -48,7 +48,7 @@ namespace aria2 {
void DHTUtil::generateRandomKey(unsigned char* key)
{
char bytes[40];
unsigned char bytes[40];
generateRandomData(bytes, sizeof(bytes));
MessageDigestHelper::digest(key, 20, "sha1", bytes, sizeof(bytes));
}

View File

@ -46,12 +46,7 @@ public:
static void generateRandomKey(unsigned char* key);
static void generateRandomData(unsigned char* data, size_t length);
static void generateRandomData(char* data, size_t length)
{
return generateRandomData(reinterpret_cast<unsigned char*>(data), length);
}
static void flipBit(unsigned char* data, size_t length, size_t bitIndex);
};

View File

@ -38,23 +38,24 @@
namespace aria2 {
Data::Data(const char* data, int32_t len, bool number):number(number) {
Data::Data(const unsigned char* data, int32_t len, bool number):number(number)
{
init(data, len);
}
Data::Data(const unsigned char* data, int32_t len, bool number):number(number) {
init(reinterpret_cast<const char*>(data), len);
Data::Data(const char* data, int32_t len, bool number):number(number) {
init(reinterpret_cast<const unsigned char*>(data), len);
}
Data::Data(const std::string& data, bool number):number(number)
{
init(data.c_str(), data.size());
init(reinterpret_cast<const unsigned char*>(data.c_str()), data.size());
}
void Data::init(const char* src, int32_t slen)
void Data::init(const unsigned char* src, int32_t slen)
{
if(src) {
data = new char[slen];
data = new unsigned char[slen];
memcpy(data, src, slen);
len = slen;
} else {
@ -71,7 +72,7 @@ std::string Data::toString() const {
return std::string(&data[0], &data[len]);
}
const char* Data::getData() const {
const unsigned char* Data::getData() const {
if(this->len == 0) {
return NULL;
} else {

View File

@ -43,10 +43,10 @@ namespace aria2 {
class Data : public MetaEntry {
private:
int32_t len;
char* data;
unsigned char* data;
bool number;
void init(const char* data, int32_t len);
void init(const unsigned char* data, int32_t len);
public:
/**
* This class stores the copy of data. So caller must take care of freeing
@ -64,7 +64,7 @@ public:
int32_t toInt() const;
int64_t toLLInt() const;
const char* getData() const;
const unsigned char* getData() const;
int32_t getLen() const;
bool isNumber() const;

View File

@ -158,8 +158,7 @@ std::string DefaultBtAnnounce::getAnnounceUrl() {
url += std::string("&")+"event="+event;
}
if(!trackerId.empty()) {
url += std::string("&")+"trackerid="+Util::torrentUrlencode((const unsigned char*)trackerId.c_str(),
trackerId.size());
url += std::string("&")+"trackerid="+Util::torrentUrlencode(trackerId);
}
if(option->getAsBool(PREF_BT_REQUIRE_CRYPTO)) {
url += "&requirecrypto=1";
@ -193,7 +192,7 @@ void DefaultBtAnnounce::resetAnnounce() {
}
void
DefaultBtAnnounce::processAnnounceResponse(const char* trackerResponse,
DefaultBtAnnounce::processAnnounceResponse(const unsigned char* trackerResponse,
size_t trackerResponseLength)
{
SharedHandle<MetaEntry> entry(MetaFileUtil::bdecoding(trackerResponse,

View File

@ -107,7 +107,7 @@ public:
virtual void resetAnnounce();
virtual void processAnnounceResponse(const char* trackerResponse,
virtual void processAnnounceResponse(const unsigned char* trackerResponse,
size_t trackerResponseLength);
virtual bool noMoreAnnounce();

View File

@ -258,7 +258,9 @@ void DefaultBtContext::extractNodes(const List* nodes)
}
}
void DefaultBtContext::loadFromMemory(const char* content, int32_t length, const std::string& defaultName)
void DefaultBtContext::loadFromMemory(const unsigned char* content,
int32_t length,
const std::string& defaultName)
{
SharedHandle<MetaEntry> rootEntry = MetaFileUtil::bdecoding(content, length);
const Dictionary* rootDic = dynamic_cast<const Dictionary*>(rootEntry.get());
@ -310,8 +312,7 @@ void DefaultBtContext::processRootDictionary(const Dictionary* rootDic, const st
}
pieceLength = pieceLengthData->toInt();
// retrieve piece hashes
extractPieceHash((unsigned char*)pieceHashData->getData(),
pieceHashData->getLen(),
extractPieceHash(pieceHashData->getData(), pieceHashData->getLen(),
PIECE_HASH_LENGTH);
const Data* privateFlag = dynamic_cast<const Data*>(infoDic->get("private"));
if(privateFlag) {
@ -386,7 +387,7 @@ std::string DefaultBtContext::getActualBasePath() const
std::deque<int32_t> DefaultBtContext::computeFastSet(const std::string& ipaddr, int32_t fastSetSize)
{
std::deque<int32_t> fastSet;
char compact[6];
unsigned char compact[6];
if(!PeerMessageUtil::createcompact(compact, ipaddr, 0)) {
return fastSet;
}

View File

@ -119,7 +119,14 @@ private:
virtual void load(const std::string& torrentFile);
void loadFromMemory(const char* content, int32_t length, const std::string& defaultName);
void loadFromMemory(const unsigned char* content, int32_t length,
const std::string& defaultName);
void loadFromMemory(const std::string& context, const std::string& defaultName)
{
loadFromMemory(reinterpret_cast<const unsigned char*>(context.c_str()),
context.size(), defaultName);
}
virtual std::string getName() const;
@ -133,7 +140,7 @@ private:
if(peerId == "") {
peerId = generatePeerId();
}
return (const unsigned char*)peerId.c_str();
return reinterpret_cast<const unsigned char*>(peerId.c_str());
}
virtual std::deque<int32_t> computeFastSet(const std::string& ipaddr, int32_t fastSetSize);

View File

@ -187,7 +187,7 @@ DefaultBtMessageFactory::createBtMessage(const unsigned char* data, int32_t data
}
case BtExtendedMessage::ID: {
if(peer->isExtendedMessagingEnabled()) {
msg = BtExtendedMessage::create(btContext, peer, (const char*)data, dataLength);
msg = BtExtendedMessage::create(btContext, peer, data, dataLength);
} else {
throw new DlAbortEx("Received extended message from peer during a session with extended messaging disabled.");
}

View File

@ -58,7 +58,7 @@ DefaultExtensionMessageFactory::DefaultExtensionMessageFactory(const BtContextHa
DefaultExtensionMessageFactory::~DefaultExtensionMessageFactory() {}
ExtensionMessageHandle
DefaultExtensionMessageFactory::createMessage(const char* data, size_t length)
DefaultExtensionMessageFactory::createMessage(const unsigned char* data, size_t length)
{
uint8_t extensionMessageID = *data;
if(extensionMessageID == 0) {

View File

@ -60,7 +60,7 @@ public:
virtual ~DefaultExtensionMessageFactory();
virtual SharedHandle<ExtensionMessage>
createMessage(const char* data, size_t length);
createMessage(const unsigned char* data, size_t length);
void setBtContext(const SharedHandle<BtContext>& btContext);

View File

@ -110,7 +110,7 @@ bool DownloadCommand::executeInternal() {
SegmentHandle segment = _segments.front();
int32_t BUFSIZE = 16*1024;
char buf[BUFSIZE];
unsigned char buf[BUFSIZE];
int32_t bufSize;
if(segment->getLength() > 0 && segment->getLength()-segment->getWrittenLength() < BUFSIZE) {
bufSize = segment->getLength()-segment->getWrittenLength();
@ -120,8 +120,8 @@ bool DownloadCommand::executeInternal() {
socket->readData(buf, bufSize);
if(transferDecoder.isNull()) {
_requestGroup->getPieceStorage()->getDiskAdaptor()->writeData((const unsigned char*)buf, bufSize,
segment->getPositionToWrite());
_requestGroup->getPieceStorage()->getDiskAdaptor()->writeData(buf, bufSize,
segment->getPositionToWrite());
//logger->debug("bufSize = %d, posToWrite = %lld", bufSize, segment->getPositionToWrite());
segment->updateWrittenLength(bufSize);
//logger->debug("overflow length = %d, next posToWrite = %lld", segment->getOverflowLength(), segment->getPositionToWrite());
@ -131,10 +131,10 @@ bool DownloadCommand::executeInternal() {
peerStat->updateDownloadLength(bufSize);
} else {
int32_t infbufSize = 16*1024;
char infbuf[infbufSize];
unsigned char infbuf[infbufSize];
transferDecoder->inflate(infbuf, infbufSize, buf, bufSize);
_requestGroup->getPieceStorage()->getDiskAdaptor()->writeData((const unsigned char*)infbuf, infbufSize,
segment->getPositionToWrite());
_requestGroup->getPieceStorage()->getDiskAdaptor()->writeData(infbuf, infbufSize,
segment->getPositionToWrite());
segment->updateWrittenLength(infbufSize);
//segment->writtenLength += infbufSize;
peerStat->updateDownloadLength(infbufSize);

View File

@ -47,7 +47,7 @@ public:
virtual ~ExtensionMessageFactory() {}
virtual SharedHandle<ExtensionMessage>
createMessage(const char* data, size_t length) = 0;
createMessage(const unsigned char* data, size_t length) = 0;
};
typedef SharedHandle<ExtensionMessageFactory> ExtensionMessageFactoryHandle;

View File

@ -132,7 +132,7 @@ uint8_t HandshakeExtensionMessage::getExtensionMessageID(const std::string& name
}
HandshakeExtensionMessageHandle
HandshakeExtensionMessage::create(const char* data, size_t length)
HandshakeExtensionMessage::create(const unsigned char* data, size_t length)
{
if(length < 1) {
throw new DlAbortEx(MSG_TOO_SMALL_PAYLOAD_SIZE,
@ -140,7 +140,7 @@ HandshakeExtensionMessage::create(const char* data, size_t length)
}
HandshakeExtensionMessageHandle msg = new HandshakeExtensionMessage();
msg->_logger->debug("Creating HandshakeExtensionMessage from %s",
Util::urlencode((const unsigned char*)data, length).c_str());
Util::urlencode(data, length).c_str());
SharedHandle<MetaEntry> root = MetaFileUtil::bdecoding(data+1, length-1);
Dictionary* d = dynamic_cast<Dictionary*>(root.get());
if(d == 0) {

View File

@ -120,9 +120,8 @@ public:
void setBtContext(const SharedHandle<BtContext>& btContext);
static HandshakeExtensionMessageHandle create(const char* data,
static HandshakeExtensionMessageHandle create(const unsigned char* data,
size_t dataLength);
};
typedef SharedHandle<HandshakeExtensionMessage> HandshakeExtensionMessageHandle;

View File

@ -117,7 +117,7 @@ HttpResponseHandle HttpConnection::receiveResponse()
HttpRequestEntryHandle entry = outstandingHttpRequests.front();
HttpHeaderProcessorHandle proc = entry->getHttpHeaderProcessor();
char buf[512];
unsigned char buf[512];
int32_t size = sizeof(buf);
socket->peekData(buf, size);
if(size == 0) {

View File

@ -45,10 +45,10 @@ HttpHeaderProcessor::HttpHeaderProcessor():_limit(4096) {}
HttpHeaderProcessor::~HttpHeaderProcessor() {}
void HttpHeaderProcessor::update(const char* data, int32_t length)
void HttpHeaderProcessor::update(const unsigned char* data, int32_t length)
{
checkHeaderLimit(length);
strm.write(data, length);
strm.write(reinterpret_cast<const char*>(data), length);
}
void HttpHeaderProcessor::update(const std::string& data)

View File

@ -56,7 +56,7 @@ public:
~HttpHeaderProcessor();
void update(const char* data, int32_t length);
void update(const unsigned char* data, int32_t length);
void update(const std::string& data);

View File

@ -45,11 +45,11 @@ namespace aria2 {
MetaEntry* MetaFileUtil::parseMetaFile(const std::string& file) {
File f(file);
int32_t len = f.size();
char* buf = new char[len];
size_t len = f.size();
unsigned char* buf = new unsigned char[len];
FILE* fp = fopen(file.c_str(), "r+b");
try {
if(fp == NULL) {
if(!fp) {
throw new DlAbortEx("cannot open metainfo file");
}
if(fread(buf, len, 1, fp) != 1) {
@ -57,35 +57,29 @@ MetaEntry* MetaFileUtil::parseMetaFile(const std::string& file) {
throw new DlAbortEx("cannot read metainfo");
}
fclose(fp);
fp = NULL;
fp = 0;
MetaEntry* entry = bdecoding(buf, len);
delete [] buf;
return entry;
} catch(RecoverableException* ex) {
delete [] buf;
if(fp != NULL) {
if(fp) {
fclose(fp);
}
throw;
}
}
MetaEntry* MetaFileUtil::bdecoding(const char* buf, int32_t len) {
MetaEntry* entry = NULL;
try{
const char* p = buf;
const char* end = buf+len;
entry = bdecodingR(&p, end);
return entry;
} catch(RecoverableException* ex) {
if(entry != NULL) {
delete entry;
}
throw;
}
MetaEntry* MetaFileUtil::bdecoding(const unsigned char* buf, size_t len)
{
const unsigned char* p = buf;
const unsigned char* end = buf+len;
return bdecodingR(&p, end);
}
MetaEntry* MetaFileUtil::bdecodingR(const char** pp, const char* end) {
MetaEntry*
MetaFileUtil::bdecodingR(const unsigned char** pp, const unsigned char* end)
{
if(*pp >= end) {
throw new DlAbortEx("Malformed metainfo");
}
@ -109,7 +103,9 @@ MetaEntry* MetaFileUtil::bdecodingR(const char** pp, const char* end) {
return e;
}
Dictionary* MetaFileUtil::parseDictionaryTree(const char** pp, const char* end) {
Dictionary*
MetaFileUtil::parseDictionaryTree(const unsigned char** pp, const unsigned char* end)
{
if(*pp >= end) {
throw new DlAbortEx("Malformed metainfo");
}
@ -131,7 +127,9 @@ Dictionary* MetaFileUtil::parseDictionaryTree(const char** pp, const char* end)
}
}
List* MetaFileUtil::parseListTree(const char** pp, const char* end) {
List*
MetaFileUtil::parseListTree(const unsigned char** pp, const unsigned char* end)
{
if(*pp >= end) {
throw new DlAbortEx("Malformed metainfo");
}
@ -152,37 +150,42 @@ List* MetaFileUtil::parseListTree(const char** pp, const char* end) {
}
}
Data* MetaFileUtil::decodeInt(const char** pp, const char* end) {
Data*
MetaFileUtil::decodeInt(const unsigned char** pp, const unsigned char* end)
{
if(*pp >= end) {
throw new DlAbortEx(EX_MALFORMED_META_INFO);
}
char* endTerm = (char*)memchr(*pp, 'e', end-*pp);
unsigned char* endTerm = reinterpret_cast<unsigned char*>(memchr(*pp, 'e', end-*pp));
// TODO if endTerm is null
if(endTerm == NULL) {
if(!endTerm) {
throw new DlAbortEx(EX_MALFORMED_META_INFO);
}
int32_t numSize = endTerm-*pp;
size_t numSize = endTerm-*pp;
Data* data = new Data(*pp, numSize, true);
*pp += numSize+1;
return data;
}
Data* MetaFileUtil::decodeWord(const char** pp, const char* end) {
Data*
MetaFileUtil::decodeWord(const unsigned char** pp, const unsigned char* end)
{
if(*pp >= end) {
throw new DlAbortEx("Malformed metainfo");
}
char* delim = (char*)memchr(*pp, ':', end-*pp);
unsigned char* delim = reinterpret_cast<unsigned char*>(memchr(*pp, ':', end-*pp));
// TODO if delim is null
if(delim == *pp || delim == NULL) {
if(delim == *pp || !delim) {
throw new DlAbortEx(EX_MALFORMED_META_INFO);
}
int32_t numSize = delim-*pp;
char* temp = new char[numSize+1];
size_t numSize = delim-*pp;
unsigned char* temp = new unsigned char[numSize+1];
memcpy(temp, *pp, numSize);
temp[numSize] = '\0';
char* endptr;
int32_t size = strtol(temp, &endptr, 10);
unsigned long int size = strtoul(reinterpret_cast<const char*>(temp),
&endptr, 10);
if(*endptr != '\0') {
delete [] temp;
throw new DlAbortEx(EX_MALFORMED_META_INFO);
@ -198,7 +201,9 @@ Data* MetaFileUtil::decodeWord(const char** pp, const char* end) {
return data;
}
std::string MetaFileUtil::decodeWordAsString(const char** pp, const char* end) {
std::string
MetaFileUtil::decodeWordAsString(const unsigned char** pp, const unsigned char* end)
{
Data* data = decodeWord(pp, end);
std::string str = data->toString();
delete data;

View File

@ -49,16 +49,22 @@ class MetaFileUtil {
private:
MetaFileUtil() {}
static MetaEntry* bdecodingR(const char** pp, const char* end);
static Dictionary* parseDictionaryTree(const char** pp, const char* end);
static List* parseListTree(const char** pp, const char* end);
static Data* decodeWord(const char** pp, const char* end);
static Data* decodeInt(const char** pp, const char* end);
static std::string decodeWordAsString(const char** pp, const char* end);
static MetaEntry* bdecodingR(const unsigned char** pp, const unsigned char* end);
static Dictionary* parseDictionaryTree(const unsigned char** pp, const unsigned char* end);
static List* parseListTree(const unsigned char** pp, const unsigned char* end);
static Data* decodeWord(const unsigned char** pp, const unsigned char* end);
static Data* decodeInt(const unsigned char** pp, const unsigned char* end);
static std::string decodeWordAsString(const unsigned char** pp, const unsigned char* end);
public:
static MetaEntry* parseMetaFile(const std::string& file);
static MetaEntry* bdecoding(const char* buf, int32_t len);
static MetaEntry* bdecoding(const unsigned char* buf, size_t len);
static MetaEntry* bdecoding(const std::string& content)
{
return bdecoding(reinterpret_cast<const unsigned char*>(content.c_str()),
content.size());
}
};
} // namespace aria2

View File

@ -160,14 +160,13 @@ bool PeerConnection::receiveHandshake(unsigned char* data, int32_t& dataLength,
return retval;
}
void PeerConnection::readData(char* data, int32_t& length, bool encryption)
void PeerConnection::readData(unsigned char* data, int32_t& length, bool encryption)
{
if(encryption) {
unsigned char* cdata = reinterpret_cast<unsigned char*>(data);
unsigned char temp[MAX_PAYLOAD_LEN];
assert(MAX_PAYLOAD_LEN >= length);
socket->readData(temp, length);
_decryptor->decrypt(cdata, length, temp, length);
_decryptor->decrypt(data, length, temp, length);
} else {
socket->readData(data, length);
}

View File

@ -57,17 +57,17 @@ private:
const Option* option;
const Logger* logger;
char resbuf[MAX_PAYLOAD_LEN];
unsigned char resbuf[MAX_PAYLOAD_LEN];
int32_t resbufLength;
int32_t currentPayloadLength;
char lenbuf[4];
unsigned char lenbuf[4];
int32_t lenbufLength;
bool _encryptionEnabled;
SharedHandle<ARC4Encryptor> _encryptor;
SharedHandle<ARC4Decryptor> _decryptor;
void readData(char* data, int32_t& length, bool encryption);
void readData(unsigned char* data, int32_t& length, bool encryption);
void sendData(const unsigned char* data, size_t length, bool encryption);

View File

@ -125,7 +125,8 @@ void PeerMessageUtil::createPeerMessageString(unsigned char* msg,
msg[4] = messageId;
}
bool PeerMessageUtil::createcompact(char* compact, const std::string& addr, uint16_t port)
bool
PeerMessageUtil::createcompact(unsigned char* compact, const std::string& addr, uint16_t port)
{
struct addrinfo hints;
struct addrinfo* res;
@ -144,7 +145,8 @@ bool PeerMessageUtil::createcompact(char* compact, const std::string& addr, uint
return true;
}
std::pair<std::string, uint16_t> PeerMessageUtil::unpackcompact(const char* compact)
std::pair<std::string, uint16_t>
PeerMessageUtil::unpackcompact(const unsigned char* compact)
{
struct sockaddr_in in;
memset(&in, 0, sizeof(in));

View File

@ -78,9 +78,9 @@ public:
* The example of failure reason is that addr is not numbers-and-dots
* notation.
*/
static bool createcompact(char* compact, const std::string& addr, uint16_t port);
static bool createcompact(unsigned char* compact, const std::string& addr, uint16_t port);
static std::pair<std::string, uint16_t> unpackcompact(const char* compact);
static std::pair<std::string, uint16_t> unpackcompact(const unsigned char* compact);
};
} // namespace aria2

View File

@ -190,6 +190,12 @@ public:
void writeData(const char* data, size_t len, const std::string& host, uint16_t port);
void writeData(const unsigned char* data, size_t len, const std::string& host,
uint16_t port)
{
writeData(reinterpret_cast<const char*>(data), len, host, port);
}
/**
* Reads up to len bytes from this socket.
* data is a pointer pointing the first
@ -209,10 +215,17 @@ public:
readData(reinterpret_cast<char*>(data), len);
}
ssize_t readDataFrom(char*, size_t len,
ssize_t readDataFrom(char* data, size_t len,
std::pair<std::string /* numerichost */,
uint16_t /* port */>& sender);
ssize_t readDataFrom(unsigned char* data, size_t len,
std::pair<std::string /* numerichost */,
uint16_t /* port */>& sender)
{
return readDataFrom(reinterpret_cast<char*>(data), len, sender);
}
/**
* Reads up to len bytes from this socket, but bytes are not removed from
* this socket.

View File

@ -125,11 +125,11 @@ bool TrackerWatcherCommand::execute() {
std::string TrackerWatcherCommand::getTrackerResponse(const RequestGroupHandle& requestGroup)
{
std::stringstream strm;
char data[2048];
unsigned char data[2048];
requestGroup->getPieceStorage()->getDiskAdaptor()->openFile();
while(1) {
int32_t dataLength = requestGroup->getPieceStorage()->getDiskAdaptor()->readData((unsigned char*)data, sizeof(data), strm.tellp());
strm.write(data, dataLength);
int32_t dataLength = requestGroup->getPieceStorage()->getDiskAdaptor()->readData(data, sizeof(data), strm.tellp());
strm.write(reinterpret_cast<const char*>(data), dataLength);
if(dataLength == 0) {
break;
}
@ -140,7 +140,7 @@ std::string TrackerWatcherCommand::getTrackerResponse(const RequestGroupHandle&
// TODO we have to deal with the exception thrown By BtAnnounce
void TrackerWatcherCommand::processTrackerResponse(const std::string& trackerResponse)
{
btAnnounce->processAnnounceResponse(trackerResponse.c_str(),
btAnnounce->processAnnounceResponse(reinterpret_cast<const unsigned char*>(trackerResponse.c_str()),
trackerResponse.size());
while(!btRuntime->isHalt() && btRuntime->lessThanMinPeer()) {
PeerHandle peer = peerStorage->getUnusedPeer();

View File

@ -45,7 +45,8 @@ class TransferEncoding {
public:
virtual ~TransferEncoding() {}
virtual void init() = 0;
virtual void inflate(char* outbuf, int32_t& outlen, const char* inbuf, int32_t inlen) = 0;
virtual void inflate(unsigned char* outbuf, int32_t& outlen,
const unsigned char* inbuf, int32_t inlen) = 0;
virtual bool finished() = 0;
virtual void end() = 0;
};

View File

@ -76,7 +76,7 @@ std::pair<std::string, std::string> UTPexExtensionMessage::createCompactPeerList
std::string addrstring;
std::string flagstring;
for(Peers::const_iterator itr = peers.begin(); itr != peers.end(); ++itr) {
char compact[6];
unsigned char compact[6];
if(PeerMessageUtil::createcompact(compact, (*itr)->ipaddr, (*itr)->port)) {
addrstring.append(&compact[0], &compact[6]);
flagstring += (*itr)->isSeeder() ? "2" : "0";
@ -123,7 +123,7 @@ void UTPexExtensionMessage::setBtContext(const BtContextHandle& btContext)
UTPexExtensionMessageHandle
UTPexExtensionMessage::create(const BtContextHandle& btContext,
const char* data, size_t len)
const unsigned char* data, size_t len)
{
if(len < 1) {
throw new DlAbortEx(MSG_TOO_SMALL_PAYLOAD_SIZE,

View File

@ -93,7 +93,7 @@ public:
void setBtContext(const SharedHandle<BtContext>& btContext);
static UTPexExtensionMessageHandle create(const SharedHandle<BtContext>& btContext,
const char* data, size_t len);
const unsigned char* data, size_t len);
};
typedef SharedHandle<UTPexExtensionMessage> UTPexExtensionMessageHandle;

View File

@ -137,6 +137,12 @@ public:
static std::string torrentUrlencode(const unsigned char* target, int32_t len);
static std::string torrentUrlencode(const std::string& target)
{
return torrentUrlencode(reinterpret_cast<const unsigned char*>(target.c_str()),
target.size());
}
static std::string toHex(const unsigned char* src, int32_t len);
static std::string toHex(const std::string& src)

View File

@ -43,7 +43,7 @@ CPPUNIT_TEST_SUITE_REGISTRATION( AnnounceListTest );
void AnnounceListTest::testSingleElementList() {
std::string peersString = "ll8:tracker1el8:tracker2el8:tracker3ee";
Dictionary* announces = (Dictionary*)MetaFileUtil::bdecoding(peersString.c_str(), peersString.size());
Dictionary* announces = (Dictionary*)MetaFileUtil::bdecoding(peersString);
// ANNOUNCE_LIST
// [ [ tracker1 ], [ tracker2 ], [ tracker3 ] ]
@ -89,7 +89,7 @@ void AnnounceListTest::testSingleElementList() {
void AnnounceListTest::testMultiElementList() {
std::string peersString = "ll8:tracker18:tracker28:tracker3ee";
Dictionary* announces = (Dictionary*)MetaFileUtil::bdecoding(peersString.c_str(), peersString.size());
Dictionary* announces = (Dictionary*)MetaFileUtil::bdecoding(peersString);
// ANNOUNCE_LIST
// [ [ tracker1, tracker2, tracker3 ] ]
AnnounceList announceList(announces);
@ -121,7 +121,7 @@ void AnnounceListTest::testMultiElementList() {
void AnnounceListTest::testSingleAndMulti() {
std::string peersString = "ll8:tracker18:tracker2el8:tracker3ee";
Dictionary* announces = (Dictionary*)MetaFileUtil::bdecoding(peersString.c_str(), peersString.size());
Dictionary* announces = (Dictionary*)MetaFileUtil::bdecoding(peersString);
// ANNOUNCE_LIST
// [ [ tracker1, tracker2 ], [ tracker3 ] ]
@ -147,7 +147,7 @@ void AnnounceListTest::testSingleAndMulti() {
void AnnounceListTest::testNoGroup() {
std::string peersString = "llee";
Dictionary* announces = (Dictionary*)MetaFileUtil::bdecoding(peersString.c_str(), peersString.size());
Dictionary* announces = (Dictionary*)MetaFileUtil::bdecoding(peersString);
AnnounceList announceList(announces);
@ -156,7 +156,7 @@ void AnnounceListTest::testNoGroup() {
void AnnounceListTest::testNextEventIfAfterStarted() {
std::string peersString = "ll8:tracker1ee";
Dictionary* announces = (Dictionary*)MetaFileUtil::bdecoding(peersString.c_str(), peersString.size());
Dictionary* announces = (Dictionary*)MetaFileUtil::bdecoding(peersString);
// ANNOUNCE_LIST
// [ [ tracker1 ] ]
@ -176,7 +176,7 @@ void AnnounceListTest::testNextEventIfAfterStarted() {
void AnnounceListTest::testEvent() {
std::string peersString = "ll8:tracker1el8:tracker2el8:tracker3ee";
Dictionary* announces = (Dictionary*)MetaFileUtil::bdecoding(peersString.c_str(), peersString.size());
Dictionary* announces = (Dictionary*)MetaFileUtil::bdecoding(peersString);
// ANNOUNCE_LIST
// [ [ tracker1 ], [ tracker2 ], [ tracker3 ] ]
@ -200,7 +200,7 @@ void AnnounceListTest::testEvent() {
void AnnounceListTest::testCountStoppedAllowedTier() {
std::string peersString = "ll8:tracker1el8:tracker2el8:tracker3ee";
Dictionary* announces = (Dictionary*)MetaFileUtil::bdecoding(peersString.c_str(), peersString.size());
Dictionary* announces = (Dictionary*)MetaFileUtil::bdecoding(peersString);
// ANNOUNCE_LIST
// [ [ tracker1 ], [ tracker2 ], [ tracker3 ] ]
@ -227,7 +227,7 @@ void AnnounceListTest::testCountStoppedAllowedTier() {
void AnnounceListTest::testCountCompletedAllowedTier() {
std::string peersString = "ll8:tracker1el8:tracker2el8:tracker3ee";
Dictionary* announces = (Dictionary*)MetaFileUtil::bdecoding(peersString.c_str(), peersString.size());
Dictionary* announces = (Dictionary*)MetaFileUtil::bdecoding(peersString);
// ANNOUNCE_LIST
// [ [ tracker1 ], [ tracker2 ], [ tracker3 ] ]

View File

@ -66,7 +66,7 @@ void BtExtendedMessageTest::testCreate() {
// payload:{4:name3:foo}->11bytes
std::string payload = "4:name3:foo";
char msg[17];// 6+11bytes
unsigned char msg[17];// 6+11bytes
PeerMessageUtil::createPeerMessageString((unsigned char*)msg, sizeof(msg), 13, 20);
msg[5] = 1; // Set dummy extended message ID 1
memcpy(msg+6, payload.c_str(), payload.size());
@ -77,8 +77,8 @@ void BtExtendedMessageTest::testCreate() {
// case: payload size is wrong
try {
char msg[5];
PeerMessageUtil::createPeerMessageString((unsigned char*)msg, sizeof(msg), 1, 20);
unsigned char msg[5];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 1, 20);
BtExtendedMessage::create(ctx, peer, &msg[4], 1);
CPPUNIT_FAIL("exception must be thrown.");
} catch(Exception* e) {
@ -87,8 +87,8 @@ void BtExtendedMessageTest::testCreate() {
}
// case: id is wrong
try {
char msg[6];
PeerMessageUtil::createPeerMessageString((unsigned char*)msg, sizeof(msg), 2, 21);
unsigned char msg[6];
PeerMessageUtil::createPeerMessageString(msg, sizeof(msg), 2, 21);
BtExtendedMessage::create(ctx, peer, &msg[4], 2);
CPPUNIT_FAIL("exception must be thrown.");
} catch(Exception* e) {
@ -101,13 +101,11 @@ void BtExtendedMessageTest::testGetMessage() {
std::string payload = "4:name3:foo";
uint8_t extendedMessageID = 1;
SharedHandle<MockExtensionMessage> exmsg =
new MockExtensionMessage("charlie", extendedMessageID,
payload.c_str(),
payload.size());
new MockExtensionMessage("charlie", extendedMessageID, payload);
BtExtendedMessage msg(exmsg);
char data[17];
PeerMessageUtil::createPeerMessageString((unsigned char*)data, sizeof(data), 13, 20);
unsigned char data[17];
PeerMessageUtil::createPeerMessageString(data, sizeof(data), 13, 20);
*(data+5) = extendedMessageID;
memcpy(data+6, payload.c_str(), payload.size());
CPPUNIT_ASSERT(memcmp(msg.getMessage(), data, 17) == 0);
@ -115,7 +113,7 @@ void BtExtendedMessageTest::testGetMessage() {
void BtExtendedMessageTest::testDoReceivedAction() {
SharedHandle<MockExtensionMessage> exmsg =
new MockExtensionMessage("charlie", 1, "", 0);
new MockExtensionMessage("charlie", 1, "");
BtExtendedMessage msg(exmsg);
msg.doReceivedAction();
CPPUNIT_ASSERT(exmsg->_doReceivedActionCalled);
@ -125,9 +123,7 @@ void BtExtendedMessageTest::testToString() {
std::string payload = "4:name3:foo";
uint8_t extendedMessageID = 1;
SharedHandle<MockExtensionMessage> exmsg =
new MockExtensionMessage("charlie", extendedMessageID,
payload.c_str(),
payload.size());
new MockExtensionMessage("charlie", extendedMessageID, payload);
BtExtendedMessage msg(exmsg);
CPPUNIT_ASSERT_EQUAL(std::string("extended charlie"), msg.toString());
}

View File

@ -30,17 +30,17 @@ CPPUNIT_TEST_SUITE_REGISTRATION( ChunkedEncodingTest );
void ChunkedEncodingTest::testInflate1() {
std::string msg = "a\r\n1234567890\r\n";
char buf[100];
unsigned char buf[100];
int32_t len = sizeof(buf);
enc->inflate(buf, len, msg.c_str(), msg.size());
buf[len] = '\0';
CPPUNIT_ASSERT_EQUAL(std::string("1234567890"), std::string(buf));
enc->inflate(buf, len,
reinterpret_cast<const unsigned char*>(msg.c_str()), msg.size());
CPPUNIT_ASSERT_EQUAL(std::string("1234567890"), std::string(&buf[0], &buf[len]));
// second pass
len = sizeof(buf);
msg = "3;extensionIgnored\r\n123\r\n0\r\n";
enc->inflate(buf, len, msg.c_str(), msg.size());
buf[len] = '\0';
CPPUNIT_ASSERT_EQUAL(std::string("123"), std::string(buf));
enc->inflate(buf, len,
reinterpret_cast<const unsigned char*>(msg.c_str()), msg.size());
CPPUNIT_ASSERT_EQUAL(std::string("123"), std::string(&buf[0], &buf[len]));
// input is over
CPPUNIT_ASSERT(enc->finished());
}
@ -53,16 +53,16 @@ void ChunkedEncodingTest::testInflateLargeChunk() {
}
std::string body;
is >> body;
char buf[4097];
unsigned char buf[4097];
int32_t len = sizeof(buf);
for(int i = 0; i < 2; i++) {
std::string msg = "1000\r\n"+body+"\r\n";
len = sizeof(buf);
enc->inflate(buf, len, msg.c_str(), msg.size());
buf[len] = '\0';
CPPUNIT_ASSERT_EQUAL(body, std::string(buf));
enc->inflate(buf, len,
reinterpret_cast<const unsigned char*>(msg.c_str()), msg.size());
CPPUNIT_ASSERT_EQUAL(body, std::string(&buf[0], &buf[len]));
}
enc->inflate(buf, len, "0\r\n", 3);
enc->inflate(buf, len, reinterpret_cast<const unsigned char*>("0\r\n"), 3);
CPPUNIT_ASSERT_EQUAL((int32_t)0, len);
CPPUNIT_ASSERT(enc->finished());
}

View File

@ -46,7 +46,7 @@ void DHTAnnouncePeerMessageTest::testGetBencodedMessage()
SharedHandle<DHTNode> localNode = new DHTNode();
SharedHandle<DHTNode> remoteNode = new DHTNode();
char tid[DHT_TRANSACTION_ID_LENGTH];
unsigned char tid[DHT_TRANSACTION_ID_LENGTH];
DHTUtil::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);
@ -66,7 +66,7 @@ void DHTAnnouncePeerMessageTest::testGetBencodedMessage()
cm->put("q", new Data("announce_peer"));
Dictionary* a = new Dictionary();
cm->put("a", a);
a->put("id", new Data(reinterpret_cast<const char*>(localNode->getID()), DHT_ID_LENGTH));
a->put("id", new Data(localNode->getID(), DHT_ID_LENGTH));
a->put("info_hash", new Data(infoHash, DHT_ID_LENGTH));
a->put("port", new Data(Util::uitos(port), true));
a->put("token", new Data(token));
@ -85,7 +85,7 @@ void DHTAnnouncePeerMessageTest::testDoReceivedAction()
remoteNode->setIPAddress("192.168.0.1");
remoteNode->setPort(6881);
char tid[DHT_TRANSACTION_ID_LENGTH];
unsigned char tid[DHT_TRANSACTION_ID_LENGTH];
DHTUtil::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);

View File

@ -31,7 +31,7 @@ void DHTAnnouncePeerReplyMessageTest::testGetBencodedMessage()
SharedHandle<DHTNode> localNode = new DHTNode();
SharedHandle<DHTNode> remoteNode = new DHTNode();
char tid[DHT_TRANSACTION_ID_LENGTH];
unsigned char tid[DHT_TRANSACTION_ID_LENGTH];
DHTUtil::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);

View File

@ -30,16 +30,17 @@ void DHTConnectionImplTest::testWriteAndReadData()
uint16_t con2port = con2.bind(0);
std::string message1 = "hello world.";
con1.sendMessage(message1.c_str(), message1.size(), "127.0.0.1", con2port);
con1.sendMessage(reinterpret_cast<const unsigned char*>(message1.c_str()),
message1.size(), "127.0.0.1", con2port);
char readbuffer[100];
unsigned char readbuffer[100];
std::string remoteHost;
uint16_t remotePort;
{
ssize_t rlength = con2.receiveMessage(readbuffer, sizeof(readbuffer), remoteHost, remotePort);
CPPUNIT_ASSERT_EQUAL((ssize_t)message1.size(), rlength);
readbuffer[rlength] = '\0';
CPPUNIT_ASSERT_EQUAL(message1, std::string(readbuffer));
CPPUNIT_ASSERT_EQUAL(message1,
std::string(&readbuffer[0], &readbuffer[rlength]));
}
} catch(Exception* e) {
std::cerr << *e << std::endl;

View File

@ -51,7 +51,7 @@ void DHTFindNodeMessageTest::testGetBencodedMessage()
SharedHandle<DHTNode> localNode = new DHTNode();
SharedHandle<DHTNode> remoteNode = new DHTNode();
char tid[DHT_TRANSACTION_ID_LENGTH];
unsigned char tid[DHT_TRANSACTION_ID_LENGTH];
DHTUtil::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);
@ -67,8 +67,8 @@ void DHTFindNodeMessageTest::testGetBencodedMessage()
cm->put("q", new Data("find_node"));
Dictionary* a = new Dictionary();
cm->put("a", a);
a->put("id", new Data(reinterpret_cast<const char*>(localNode->getID()), DHT_ID_LENGTH));
a->put("target", new Data(reinterpret_cast<const char*>(targetNode->getID()), DHT_ID_LENGTH));
a->put("id", new Data(localNode->getID(), DHT_ID_LENGTH));
a->put("target", new Data(targetNode->getID(), DHT_ID_LENGTH));
BencodeVisitor v;
cm->accept(&v);
@ -81,7 +81,7 @@ void DHTFindNodeMessageTest::testDoReceivedAction()
SharedHandle<DHTNode> localNode = new DHTNode();
SharedHandle<DHTNode> remoteNode = new DHTNode();
char tid[DHT_TRANSACTION_ID_LENGTH];
unsigned char tid[DHT_TRANSACTION_ID_LENGTH];
DHTUtil::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);

View File

@ -33,7 +33,7 @@ void DHTFindNodeReplyMessageTest::testGetBencodedMessage()
SharedHandle<DHTNode> localNode = new DHTNode();
SharedHandle<DHTNode> remoteNode = new DHTNode();
char tid[DHT_TRANSACTION_ID_LENGTH];
unsigned char tid[DHT_TRANSACTION_ID_LENGTH];
DHTUtil::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);
@ -46,7 +46,7 @@ void DHTFindNodeReplyMessageTest::testGetBencodedMessage()
nodes[i]->setIPAddress("192.168.0."+Util::uitos(i+1));
nodes[i]->setPort(6881+i);
char buf[6];
unsigned char buf[6];
CPPUNIT_ASSERT(PeerMessageUtil::createcompact(buf, nodes[i]->getIPAddress(), nodes[i]->getPort()));
compactNodeInfo +=
std::string(&nodes[i]->getID()[0], &nodes[i]->getID()[DHT_ID_LENGTH])+
@ -61,7 +61,7 @@ void DHTFindNodeReplyMessageTest::testGetBencodedMessage()
cm->put("y", new Data("r"));
Dictionary* r = new Dictionary();
cm->put("r", r);
r->put("id", new Data(reinterpret_cast<const char*>(localNode->getID()), DHT_ID_LENGTH));
r->put("id", new Data(localNode->getID(), DHT_ID_LENGTH));
r->put("nodes", new Data(compactNodeInfo));
BencodeVisitor v;

View File

@ -69,7 +69,7 @@ void DHTGetPeersMessageTest::testGetBencodedMessage()
SharedHandle<DHTNode> localNode = new DHTNode();
SharedHandle<DHTNode> remoteNode = new DHTNode();
char tid[DHT_TRANSACTION_ID_LENGTH];
unsigned char tid[DHT_TRANSACTION_ID_LENGTH];
DHTUtil::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);
@ -86,7 +86,7 @@ void DHTGetPeersMessageTest::testGetBencodedMessage()
cm->put("q", new Data("get_peers"));
Dictionary* a = new Dictionary();
cm->put("a", a);
a->put("id", new Data(reinterpret_cast<const char*>(localNode->getID()), DHT_ID_LENGTH));
a->put("id", new Data(localNode->getID(), DHT_ID_LENGTH));
a->put("info_hash", new Data(infoHash, DHT_ID_LENGTH));
BencodeVisitor v;
@ -103,7 +103,7 @@ void DHTGetPeersMessageTest::testDoReceivedAction()
remoteNode->setIPAddress("192.168.0.1");
remoteNode->setPort(6881);
char tid[DHT_TRANSACTION_ID_LENGTH];
unsigned char tid[DHT_TRANSACTION_ID_LENGTH];
DHTUtil::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);

View File

@ -35,7 +35,7 @@ void DHTGetPeersReplyMessageTest::testGetBencodedMessage()
SharedHandle<DHTNode> localNode = new DHTNode();
SharedHandle<DHTNode> remoteNode = new DHTNode();
char tid[DHT_TRANSACTION_ID_LENGTH];
unsigned char tid[DHT_TRANSACTION_ID_LENGTH];
DHTUtil::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);
@ -48,7 +48,7 @@ void DHTGetPeersReplyMessageTest::testGetBencodedMessage()
cm->put("y", new Data("r"));
Dictionary* r = new Dictionary();
cm->put("r", r);
r->put("id", new Data(reinterpret_cast<const char*>(localNode->getID()), DHT_ID_LENGTH));
r->put("id", new Data(localNode->getID(), DHT_ID_LENGTH));
r->put("token", new Data(token));
{
@ -59,7 +59,7 @@ void DHTGetPeersReplyMessageTest::testGetBencodedMessage()
nodes[i]->setIPAddress("192.168.0."+Util::uitos(i+1));
nodes[i]->setPort(6881+i);
char buf[6];
unsigned char buf[6];
CPPUNIT_ASSERT(PeerMessageUtil::createcompact(buf, nodes[i]->getIPAddress(), nodes[i]->getPort()));
compactNodeInfo +=
std::string(&nodes[i]->getID()[0], &nodes[i]->getID()[DHT_ID_LENGTH])+
@ -84,7 +84,7 @@ void DHTGetPeersReplyMessageTest::testGetBencodedMessage()
r->put("values", values);
for(size_t i = 0; i < 4; ++i) {
SharedHandle<Peer> peer = new Peer("192.168.0."+Util::uitos(i+1), 6881+i);
char buffer[6];
unsigned char buffer[6];
CPPUNIT_ASSERT(PeerMessageUtil::createcompact(buffer, peer->ipaddr, peer->port));
values->add(new Data(buffer, sizeof(buffer)));
peers.push_back(peer);

View File

@ -163,7 +163,7 @@ void DHTMessageFactoryImplTest::testCreateFindNodeReplyMessage()
nodes[i]->setIPAddress("192.168.0."+Util::uitos(i+1));
nodes[i]->setPort(6881+i);
char buf[6];
unsigned char buf[6];
CPPUNIT_ASSERT(PeerMessageUtil::createcompact(buf, nodes[i]->getIPAddress(), nodes[i]->getPort()));
compactNodeInfo +=
std::string(&nodes[i]->getID()[0], &nodes[i]->getID()[DHT_ID_LENGTH])+
@ -232,7 +232,7 @@ void DHTMessageFactoryImplTest::testCreateGetPeersReplyMessage_nodes()
nodes[i]->setIPAddress("192.168.0."+Util::uitos(i+1));
nodes[i]->setPort(6881+i);
char buf[6];
unsigned char buf[6];
CPPUNIT_ASSERT(PeerMessageUtil::createcompact(buf, nodes[i]->getIPAddress(), nodes[i]->getPort()));
compactNodeInfo +=
std::string(&nodes[i]->getID()[0], &nodes[i]->getID()[DHT_ID_LENGTH])+
@ -276,7 +276,7 @@ void DHTMessageFactoryImplTest::testCreateGetPeersReplyMessage_values()
r->put("values", values);
for(size_t i = 0; i < 4; ++i) {
SharedHandle<Peer> peer = new Peer("192.168.0."+Util::uitos(i+1), 6881+i);
char buffer[6];
unsigned char buffer[6];
CPPUNIT_ASSERT(PeerMessageUtil::createcompact(buffer, peer->ipaddr, peer->port));
values->add(new Data(buffer, sizeof(buffer)));
peers.push_back(peer);

View File

@ -48,7 +48,7 @@ void DHTPingMessageTest::testGetBencodedMessage()
SharedHandle<DHTNode> localNode = new DHTNode();
SharedHandle<DHTNode> remoteNode = new DHTNode();
char tid[DHT_TRANSACTION_ID_LENGTH];
unsigned char tid[DHT_TRANSACTION_ID_LENGTH];
DHTUtil::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);
@ -62,7 +62,7 @@ void DHTPingMessageTest::testGetBencodedMessage()
cm->put("q", new Data("ping"));
Dictionary* a = new Dictionary();
cm->put("a", a);
a->put("id", new Data(reinterpret_cast<const char*>(localNode->getID()), DHT_ID_LENGTH));
a->put("id", new Data(localNode->getID(), DHT_ID_LENGTH));
BencodeVisitor v;
cm->accept(&v);
@ -75,7 +75,7 @@ void DHTPingMessageTest::testDoReceivedAction()
SharedHandle<DHTNode> localNode = new DHTNode();
SharedHandle<DHTNode> remoteNode = new DHTNode();
char tid[DHT_TRANSACTION_ID_LENGTH];
unsigned char tid[DHT_TRANSACTION_ID_LENGTH];
DHTUtil::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);

View File

@ -31,14 +31,14 @@ void DHTPingReplyMessageTest::testGetBencodedMessage()
SharedHandle<DHTNode> localNode = new DHTNode();
SharedHandle<DHTNode> remoteNode = new DHTNode();
char tid[DHT_TRANSACTION_ID_LENGTH];
unsigned char tid[DHT_TRANSACTION_ID_LENGTH];
DHTUtil::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
std::string transactionID(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);
char id[DHT_ID_LENGTH];
unsigned char id[DHT_ID_LENGTH];
DHTUtil::generateRandomData(id, DHT_ID_LENGTH);
DHTPingReplyMessage msg(localNode, remoteNode, (const unsigned char*)id, transactionID);
DHTPingReplyMessage msg(localNode, remoteNode, id, transactionID);
std::string msgbody = msg.getBencodedMessage();

View File

@ -108,7 +108,8 @@ void DHTRoutingTableSerializerTest::testSerialize()
// 6bytes compact peer info
ss.read(buf, 6);
{
std::pair<std::string, uint16_t> peer = PeerMessageUtil::unpackcompact(buf);
std::pair<std::string, uint16_t> peer =
PeerMessageUtil::unpackcompact(reinterpret_cast<const unsigned char*>(buf));
CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), peer.first);
CPPUNIT_ASSERT_EQUAL((uint16_t)6881, peer.second);
}
@ -166,7 +167,8 @@ void DHTRoutingTableSerializerTest::testSerialize()
// 6bytes compact peer info
ss.read(buf, 6);
{
std::pair<std::string, uint16_t> peer = PeerMessageUtil::unpackcompact(buf);
std::pair<std::string, uint16_t> peer =
PeerMessageUtil::unpackcompact(reinterpret_cast<const unsigned char*>(buf));
CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.3"), peer.first);
CPPUNIT_ASSERT_EQUAL((uint16_t)6883, peer.second);
}

View File

@ -26,7 +26,7 @@ CPPUNIT_TEST_SUITE_REGISTRATION(DHTTokenTrackerTest);
void DHTTokenTrackerTest::testGenerateToken()
{
unsigned char infohash[DHT_ID_LENGTH];
DHTUtil::generateRandomData(reinterpret_cast<char*>(infohash), DHT_ID_LENGTH);
DHTUtil::generateRandomData(infohash, DHT_ID_LENGTH);
std::string ipaddr = "192.168.0.1";
uint16_t port = 6881;

View File

@ -30,7 +30,9 @@ void DHTUnknownMessageTest::testToString()
{
// data.size() > 8
std::string data = "chocolate";
DHTUnknownMessage msg(localNode, data.c_str(), data.size(),
DHTUnknownMessage msg(localNode,
reinterpret_cast<const unsigned char*>(data.c_str()),
data.size(),
ipaddr, port);
CPPUNIT_ASSERT_EQUAL(std::string("dht unknown Remote:192.168.0.1:6881 length=9, first 8 bytes(hex)=63686f636f6c6174"), msg.toString());
@ -38,7 +40,9 @@ void DHTUnknownMessageTest::testToString()
{
// data.size() == 3
std::string data = "foo";
DHTUnknownMessage msg(localNode, data.c_str(), data.size(),
DHTUnknownMessage msg(localNode,
reinterpret_cast<const unsigned char*>(data.c_str()),
data.size(),
ipaddr, port);
CPPUNIT_ASSERT_EQUAL(std::string("dht unknown Remote:192.168.0.1:6881 length=3, first 8 bytes(hex)=666f6f"), msg.toString());

View File

@ -41,7 +41,7 @@ void DataTest::testGetData() {
CPPUNIT_ASSERT_EQUAL((int32_t)5, data.getLen());
Data null(reinterpret_cast<const char*>(0), 0);
CPPUNIT_ASSERT_EQUAL((const char*)NULL, null.getData());
CPPUNIT_ASSERT(null.getData() == 0);
CPPUNIT_ASSERT_EQUAL((int32_t)0, null.getLen());
}

View File

@ -57,7 +57,7 @@ public:
_btContext->setInfoHash(infoHash);
_btContext->setTotalLength(totalLength);
_btContext->setPieceLength(pieceLength);
_btContext->setPeerId((const unsigned char*)peerId.c_str());
_btContext->setPeerId(reinterpret_cast<const unsigned char*>(peerId.c_str()));
_pieceStorage = new MockPieceStorage();
_pieceStorage->setTotalLength(totalLength);
@ -288,7 +288,7 @@ void DefaultBtAnnounceTest::testProcessAnnounceResponse_malformed()
{
try {
std::string res = "i123e";
DefaultBtAnnounce(new MockBtContext(), _option).processAnnounceResponse(res.c_str(), res.size());
DefaultBtAnnounce(new MockBtContext(), _option).processAnnounceResponse(reinterpret_cast<const unsigned char*>(res.c_str()), res.size());
CPPUNIT_FAIL("exception must be thrown.");
} catch(Exception* e) {
std::cerr << *e << std::endl;
@ -300,7 +300,7 @@ void DefaultBtAnnounceTest::testProcessAnnounceResponse_failureReason()
{
try {
std::string res = "d14:failure reason11:hello worlde";
DefaultBtAnnounce(new MockBtContext(), _option).processAnnounceResponse(res.c_str(), res.size());
DefaultBtAnnounce(new MockBtContext(), _option).processAnnounceResponse(reinterpret_cast<const unsigned char*>(res.c_str()), res.size());
CPPUNIT_FAIL("exception must be thrown.");
} catch(Exception* e) {
std::cerr << *e << std::endl;
@ -320,7 +320,7 @@ void DefaultBtAnnounceTest::testProcessAnnounceResponse()
"e";
DefaultBtAnnounce an(new MockBtContext(), _option);
an.processAnnounceResponse(res.c_str(), res.size());
an.processAnnounceResponse(reinterpret_cast<const unsigned char*>(res.c_str()), res.size());
CPPUNIT_ASSERT_EQUAL(std::string("foo"), an.getTrackerID());
CPPUNIT_ASSERT_EQUAL(3000, an.getInterval());
CPPUNIT_ASSERT_EQUAL(1800, an.getMinInterval());

View File

@ -323,7 +323,7 @@ void DefaultBtContextTest::testLoadFromMemory()
std::string memory = "d8:announce36:http://aria.rednoah.com/announce.php13:announce-listll16:http://tracker1 el15:http://tracker2el15:http://tracker3ee7:comment17:REDNOAH.COM RULES13:creation datei1123456789e4:infod5:filesld6:lengthi284e4:pathl5:aria23:src6:aria2ceed6:lengthi100e4:pathl19:aria2-0.2.2.tar.bz2eee4:name10:aria2-test12:piece lengthi128e6:pieces60:AAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCee";
DefaultBtContext btContext;
btContext.loadFromMemory(memory.c_str(), memory.size(), "default");
btContext.loadFromMemory(memory, "default");
std::string correctHash = "248d0a1cd08284299de78d5c1ed359bb46717d8c";
@ -338,7 +338,7 @@ void DefaultBtContextTest::testLoadFromMemory_somethingMissing()
try {
std::string memory = "d8:announce36:http://aria.rednoah.com/announce.php4:infod4:name13:aria2.tar.bz26:lengthi262144eee";
DefaultBtContext btContext;
btContext.loadFromMemory(memory.c_str(), memory.size(), "default");
btContext.loadFromMemory(memory, "default");
CPPUNIT_FAIL("exception must be thrown.");
} catch(Exception* e) {
std::cerr << *e << std::endl;
@ -358,7 +358,7 @@ void DefaultBtContextTest::testGetNodes()
"6:pieces20:AAAAAAAAAAAAAAAAAAAA"
"ee";
DefaultBtContext btContext;
btContext.loadFromMemory(memory.c_str(), memory.size(), "default");
btContext.loadFromMemory(memory, "default");
const std::deque<std::pair<std::string, uint16_t> >& nodes =
btContext.getNodes();
@ -379,7 +379,7 @@ void DefaultBtContextTest::testGetNodes()
"6:pieces20:AAAAAAAAAAAAAAAAAAAA"
"ee";
DefaultBtContext btContext;
btContext.loadFromMemory(memory.c_str(), memory.size(), "default");
btContext.loadFromMemory(memory, "default");
const std::deque<std::pair<std::string, uint16_t> >& nodes =
btContext.getNodes();
@ -398,7 +398,7 @@ void DefaultBtContextTest::testGetNodes()
"6:pieces20:AAAAAAAAAAAAAAAAAAAA"
"ee";
DefaultBtContext btContext;
btContext.loadFromMemory(memory.c_str(), memory.size(), "default");
btContext.loadFromMemory(memory, "default");
const std::deque<std::pair<std::string, uint16_t> >& nodes =
btContext.getNodes();
@ -417,7 +417,7 @@ void DefaultBtContextTest::testGetNodes()
"6:pieces20:AAAAAAAAAAAAAAAAAAAA"
"ee";
DefaultBtContext btContext;
btContext.loadFromMemory(memory.c_str(), memory.size(), "default");
btContext.loadFromMemory(memory, "default");
const std::deque<std::pair<std::string, uint16_t> >& nodes =
btContext.getNodes();
@ -435,7 +435,7 @@ void DefaultBtContextTest::testGetNodes()
"6:pieces20:AAAAAAAAAAAAAAAAAAAA"
"ee";
DefaultBtContext btContext;
btContext.loadFromMemory(memory.c_str(), memory.size(), "default");
btContext.loadFromMemory(memory, "default");
const std::deque<std::pair<std::string, uint16_t> >& nodes =
btContext.getNodes();
@ -452,7 +452,7 @@ void DefaultBtContextTest::testGetNodes()
"6:pieces20:AAAAAAAAAAAAAAAAAAAA"
"ee";
DefaultBtContext btContext;
btContext.loadFromMemory(memory.c_str(), memory.size(), "default");
btContext.loadFromMemory(memory, "default");
const std::deque<std::pair<std::string, uint16_t> >& nodes =
btContext.getNodes();

View File

@ -69,7 +69,8 @@ void DefaultExtensionMessageFactoryTest::testCreateMessage_unknown()
std::string data = std::string(&id[0], &id[1]);
try {
// this test fails because localhost doesn't have extension id = 255.
factory.createMessage(data.c_str(), data.size());
factory.createMessage(reinterpret_cast<const unsigned char*>(data.c_str()),
data.size());
CPPUNIT_FAIL("exception must be thrown.");
} catch(Exception* e) {
std::cerr << *e << std::endl;
@ -87,7 +88,8 @@ void DefaultExtensionMessageFactoryTest::testCreateMessage_Handshake()
std::string data = std::string(&id[0], &id[1])+"d1:v5:aria2e";
SharedHandle<HandshakeExtensionMessage> m =
factory.createMessage(data.c_str(), data.size());
factory.createMessage(reinterpret_cast<const unsigned char*>(data.c_str()),
data.size());
CPPUNIT_ASSERT_EQUAL(std::string("aria2"), m->getClientVersion());
}
@ -97,10 +99,10 @@ void DefaultExtensionMessageFactoryTest::testCreateMessage_UTPex()
factory.setBtContext(_btContext);
factory.setPeer(_peer);
char c1[6];
char c2[6];
char c3[6];
char c4[6];
unsigned char c1[6];
unsigned char c2[6];
unsigned char c3[6];
unsigned char c4[6];
PeerMessageUtil::createcompact(c1, "192.168.0.1", 6881);
PeerMessageUtil::createcompact(c2, "10.1.1.2", 9999);
PeerMessageUtil::createcompact(c3, "192.168.0.2", 6882);
@ -115,7 +117,8 @@ void DefaultExtensionMessageFactoryTest::testCreateMessage_UTPex()
"e";
SharedHandle<UTPexExtensionMessage> m =
factory.createMessage(data.c_str(), data.size());
factory.createMessage(reinterpret_cast<const unsigned char*>(data.c_str()),
data.size());
CPPUNIT_ASSERT_EQUAL(factory.getExtensionMessageID("ut_pex"),
m->getExtensionMessageID());
}

View File

@ -30,7 +30,7 @@ void DefaultPeerListProcessorTest::testExtractPeer() {
DefaultPeerListProcessor proc(1024*1024, 10*1024*1024);
std::string peersString = "d5:peersld2:ip11:192.168.0.17:peer id20:aria2-000000000000004:porti2006eeee";
Dictionary* dic = (Dictionary*)MetaFileUtil::bdecoding(peersString.c_str(), peersString.size());
Dictionary* dic = (Dictionary*)MetaFileUtil::bdecoding(peersString);
CPPUNIT_ASSERT(proc.canHandle(dic->get("peers")));
@ -45,7 +45,7 @@ void DefaultPeerListProcessorTest::testExtract2Peers() {
DefaultPeerListProcessor proc(1024*1024, 10*1024*1024);
std::string peersString = "d5:peersld2:ip11:192.168.0.17:peer id20:aria2-000000000000004:porti2006eed2:ip11:192.168.0.27:peer id20:aria2-000000000000004:porti2007eeee";
Dictionary* dic = (Dictionary*)MetaFileUtil::bdecoding(peersString.c_str(), peersString.size());
Dictionary* dic = (Dictionary*)MetaFileUtil::bdecoding(peersString);
std::deque<SharedHandle<Peer> > peers = proc.extractPeer(dic->get("peers"));
CPPUNIT_ASSERT_EQUAL((size_t)2, peers.size());

View File

@ -95,7 +95,8 @@ void HandshakeExtensionMessageTest::testCreate()
{
std::string in = "0d1:pi6881e1:v5:aria21:md6:ut_pexi1eee";
SharedHandle<HandshakeExtensionMessage> m =
HandshakeExtensionMessage::create(in.c_str(), in.size());
HandshakeExtensionMessage::create(reinterpret_cast<const unsigned char*>(in.c_str()),
in.size());
CPPUNIT_ASSERT_EQUAL(std::string("aria2"), m->getClientVersion());
CPPUNIT_ASSERT_EQUAL((uint16_t)6881, m->getTCPPort());
CPPUNIT_ASSERT_EQUAL((uint8_t)1, m->getExtensionMessageID("ut_pex"));
@ -103,7 +104,8 @@ void HandshakeExtensionMessageTest::testCreate()
try {
// bad payload format
std::string in = "011:hello world";
HandshakeExtensionMessage::create(in.c_str(), in.size());
HandshakeExtensionMessage::create(reinterpret_cast<const unsigned char*>(in.c_str()),
in.size());
CPPUNIT_FAIL("exception must be thrown.");
} catch(Exception* e) {
std::cerr << *e << std::endl;
@ -112,7 +114,8 @@ void HandshakeExtensionMessageTest::testCreate()
try {
// malformed dencoded message
std::string in = "011:hello";
HandshakeExtensionMessage::create(in.c_str(), in.size());
HandshakeExtensionMessage::create(reinterpret_cast<const unsigned char*>(in.c_str()),
in.size());
CPPUNIT_FAIL("exception must be thrown.");
} catch(Exception* e) {
std::cerr << *e << std::endl;
@ -121,7 +124,8 @@ void HandshakeExtensionMessageTest::testCreate()
try {
// 0 length data
std::string in = "";
HandshakeExtensionMessage::create(in.c_str(), in.size());
HandshakeExtensionMessage::create(reinterpret_cast<const unsigned char*>(in.c_str()),
in.size());
CPPUNIT_FAIL("exception must be thrown.");
} catch(Exception* e) {
std::cerr << *e << std::endl;
@ -133,7 +137,8 @@ void HandshakeExtensionMessageTest::testCreate_stringnum()
{
std::string in = "0d1:p4:68811:v5:aria21:md6:ut_pex1:1ee";
SharedHandle<HandshakeExtensionMessage> m =
HandshakeExtensionMessage::create(in.c_str(), in.size());
HandshakeExtensionMessage::create(reinterpret_cast<const unsigned char*>(in.c_str()),
in.size());
CPPUNIT_ASSERT_EQUAL(std::string("aria2"), m->getClientVersion());
CPPUNIT_ASSERT_EQUAL((uint16_t)6881, m->getTCPPort());
CPPUNIT_ASSERT_EQUAL((uint8_t)1, m->getExtensionMessageID("ut_pex"));

View File

@ -78,12 +78,13 @@ void HttpHeaderProcessorTest::testGetPutBackDataLength()
void HttpHeaderProcessorTest::testGetPutBackDataLength_nullChar()
{
HttpHeaderProcessor proc;
proc.update("HTTP/1.1 200 OK\r\n"
"foo: foo\0bar\r\n"
"\r\nputbackme", 35+7);
const char* x = "HTTP/1.1 200 OK\r\n"
"foo: foo\0bar\r\n"
"\r\nputbackme";
std::string hd1(&x[0], &x[42]);
proc.update(hd1);
CPPUNIT_ASSERT(proc.eoh());
CPPUNIT_ASSERT_EQUAL((int32_t)9, proc.getPutBackDataLength());
}
void HttpHeaderProcessorTest::testGetHttpStatusHeader()

View File

@ -35,8 +35,8 @@ void MetaFileUtilTest::testParseMetaFile() {
void MetaFileUtilTest::testBdecoding() {
try {
const char* str = "5:abcd";
MetaFileUtil::bdecoding(str, strlen(str));
std::string str = "5:abcd";
MetaFileUtil::bdecoding(str);
CPPUNIT_FAIL("DlAbortEx exception must be thrown.");
} catch(DlAbortEx* ex) {
delete ex;
@ -45,8 +45,8 @@ void MetaFileUtilTest::testBdecoding() {
}
try {
const char* str = "i1234";
MetaFileUtil::bdecoding(str, strlen(str));
std::string str = "i1234";
MetaFileUtil::bdecoding(str);
CPPUNIT_FAIL("DlAbortEx exception must be thrown.");
} catch(DlAbortEx* ex) {
delete ex;
@ -55,8 +55,8 @@ void MetaFileUtilTest::testBdecoding() {
}
try {
const char* str = "5abcd";
MetaFileUtil::bdecoding(str, strlen(str));
const std::string str = "5abcd";
MetaFileUtil::bdecoding(str);
CPPUNIT_FAIL("DlAbortEx exception must be thrown.");
} catch(DlAbortEx* ex) {
delete ex;
@ -65,8 +65,8 @@ void MetaFileUtilTest::testBdecoding() {
}
try {
const char* str = "d";
MetaFileUtil::bdecoding(str, strlen(str));
const std::string str = "d";
MetaFileUtil::bdecoding(str);
CPPUNIT_FAIL("DlAbortEx exception must be thrown.");
} catch(DlAbortEx* ex) {
delete ex;

View File

@ -42,7 +42,7 @@ public:
virtual void resetAnnounce() {}
virtual void processAnnounceResponse(const char* trackerResponse,
virtual void processAnnounceResponse(const unsigned char* trackerResponse,
size_t trackerResponseLength) {}
virtual bool noMoreAnnounce() {

View File

@ -112,8 +112,8 @@ public:
}
virtual SharedHandle<DHTMessage>
createUnknownMessage(const char* data, size_t length, const std::string& ipaddr,
uint16_t port)
createUnknownMessage(const unsigned char* data, size_t length,
const std::string& ipaddr, uint16_t port)
{
return 0;
}

View File

@ -14,12 +14,20 @@ public:
public:
MockExtensionMessage(const std::string& extensionName,
uint8_t extensionMessageID,
const char* data,
const unsigned char* data,
size_t length):_extensionName(extensionName),
_extensionMessageID(extensionMessageID),
_data(&data[0], &data[length]),
_doReceivedActionCalled(false) {}
MockExtensionMessage(const std::string& extensionName,
uint8_t extensionMessageID,
const std::string& data):
_extensionName(extensionName),
_extensionMessageID(extensionMessageID),
_data(data),
_doReceivedActionCalled(false) {}
virtual ~MockExtensionMessage() {}
virtual std::string getBencodedData()

View File

@ -10,7 +10,7 @@ class MockExtensionMessageFactory:public ExtensionMessageFactory {
public:
virtual ~MockExtensionMessageFactory() {}
virtual SharedHandle<ExtensionMessage> createMessage(const char* data,
virtual SharedHandle<ExtensionMessage> createMessage(const unsigned char* data,
size_t length)
{
return new MockExtensionMessage("a2_mock", *data, data+1, length-1);

View File

@ -41,7 +41,7 @@ void createNLengthMessage(char* msg, int msgLen, int payloadLen, int id) {
void PeerMessageUtilTest::testCreateCompact()
{
char compact[6];
unsigned char compact[6];
CPPUNIT_ASSERT(PeerMessageUtil::createcompact(compact, "::ffff:127.0.0.1", 6881));
std::pair<std::string, uint16_t> p = PeerMessageUtil::unpackcompact(compact);

View File

@ -83,10 +83,10 @@ void UTPexExtensionMessageTest::testGetBencodedData()
SharedHandle<Peer> p4 = new Peer("10.1.1.3", 10000);
msg.addDroppedPeer(p4);
char c1[6];
char c2[6];
char c3[6];
char c4[6];
unsigned char c1[6];
unsigned char c2[6];
unsigned char c3[6];
unsigned char c4[6];
PeerMessageUtil::createcompact(c1, p1->ipaddr, p1->port);
PeerMessageUtil::createcompact(c2, p2->ipaddr, p2->port);
PeerMessageUtil::createcompact(c3, p3->ipaddr, p3->port);
@ -153,10 +153,10 @@ void UTPexExtensionMessageTest::testCreate()
_btContext->setPieceLength(256*1024);
_btContext->setTotalLength(1024*1024);
char c1[6];
char c2[6];
char c3[6];
char c4[6];
unsigned char c1[6];
unsigned char c2[6];
unsigned char c3[6];
unsigned char c4[6];
PeerMessageUtil::createcompact(c1, "192.168.0.1", 6881);
PeerMessageUtil::createcompact(c2, "10.1.1.2", 9999);
PeerMessageUtil::createcompact(c3, "192.168.0.2", 6882);
@ -171,7 +171,9 @@ void UTPexExtensionMessageTest::testCreate()
"e";
SharedHandle<UTPexExtensionMessage> msg =
UTPexExtensionMessage::create(_btContext, data.c_str(), data.size());
UTPexExtensionMessage::create(_btContext,
reinterpret_cast<const unsigned char*>(data.c_str()),
data.size());
CPPUNIT_ASSERT_EQUAL((uint8_t)1, msg->getExtensionMessageID());
CPPUNIT_ASSERT_EQUAL((size_t)2, msg->getFreshPeers().size());
CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), msg->getFreshPeers()[0]->ipaddr);
@ -186,7 +188,9 @@ void UTPexExtensionMessageTest::testCreate()
try {
// 0 length data
std::string in = "";
UTPexExtensionMessage::create(_btContext, in.c_str(), in.size());
UTPexExtensionMessage::create(_btContext,
reinterpret_cast<const unsigned char*>(in.c_str()),
in.size());
CPPUNIT_FAIL("exception must be thrown.");
} catch(Exception* e) {
std::cerr << *e << std::endl;