mirror of https://github.com/aria2/aria2
Made util::fromHex and base32::decode function template
parent
6ea1b68db1
commit
0c162dfbfb
|
@ -94,7 +94,7 @@ SharedHandle<LpdMessage> LpdMessageReceiver::receiveMessage()
|
|||
return msg;
|
||||
}
|
||||
SharedHandle<HttpHeader> header = proc.getHttpRequestHeader();
|
||||
std::string infoHashString = header->getFirst("Infohash");
|
||||
const std::string& infoHashString = header->getFirst("Infohash");
|
||||
uint16_t port = header->getFirstAsUInt("Port");
|
||||
A2_LOG_INFO(fmt("LPD message received infohash=%s, port=%u from %s",
|
||||
infoHashString.c_str(),
|
||||
|
@ -102,7 +102,8 @@ SharedHandle<LpdMessage> LpdMessageReceiver::receiveMessage()
|
|||
peerAddr.first.c_str()));
|
||||
std::string infoHash;
|
||||
if(infoHashString.size() != 40 ||
|
||||
(infoHash = util::fromHex(infoHashString)).empty() ||
|
||||
(infoHash = util::fromHex(infoHashString.begin(),
|
||||
infoHashString.end())).empty() ||
|
||||
port == 0) {
|
||||
A2_LOG_INFO(fmt("LPD bad request. infohash=%s", infoHashString.c_str()));
|
||||
msg.reset(new LpdMessage());
|
||||
|
|
|
@ -284,7 +284,7 @@ void MetalinkParserController::setHashOfChecksum(const std::string& md)
|
|||
return;
|
||||
}
|
||||
if(MessageDigest::isValidHash(tChecksum_->getHashType(), md)) {
|
||||
tChecksum_->setDigest(util::fromHex(md));
|
||||
tChecksum_->setDigest(util::fromHex(md.begin(), md.end()));
|
||||
} else {
|
||||
cancelChecksumTransaction();
|
||||
}
|
||||
|
@ -360,7 +360,7 @@ void MetalinkParserController::addHashOfChunkChecksumV4(const std::string& md)
|
|||
return;
|
||||
}
|
||||
if(MessageDigest::isValidHash(tChunkChecksumV4_->getHashType(), md)) {
|
||||
tempChunkChecksumsV4_.push_back(util::fromHex(md));
|
||||
tempChunkChecksumsV4_.push_back(util::fromHex(md.begin(), md.end()));
|
||||
} else {
|
||||
cancelChunkChecksumTransactionV4();
|
||||
}
|
||||
|
@ -463,7 +463,7 @@ void MetalinkParserController::setMessageDigestOfChunkChecksum(const std::string
|
|||
return;
|
||||
}
|
||||
if(MessageDigest::isValidHash(tChunkChecksum_->getHashType(), md)) {
|
||||
tempHashPair_.second = util::fromHex(md);
|
||||
tempHashPair_.second = util::fromHex(md.begin(), md.end());
|
||||
} else {
|
||||
cancelChunkChecksumTransaction();
|
||||
}
|
||||
|
|
|
@ -1130,10 +1130,8 @@ void changeOption
|
|||
std::pair<Scip, Scip> p;
|
||||
util::divide(p, checksum.begin(), checksum.end(), '=');
|
||||
std::string hashType(p.first.first, p.first.second);
|
||||
std::string hexDigest(p.second.first, p.second.second);
|
||||
util::lowercase(hashType);
|
||||
util::lowercase(hexDigest);
|
||||
dctx->setDigest(hashType, util::fromHex(hexDigest));
|
||||
dctx->setDigest(hashType, util::fromHex(p.second.first, p.second.second));
|
||||
}
|
||||
if(option.defined(PREF_SELECT_FILE)) {
|
||||
SegList<int> sgl;
|
||||
|
|
|
@ -94,43 +94,6 @@ std::string encode(const std::string& src)
|
|||
return ret;
|
||||
}
|
||||
|
||||
std::string decode(const std::string& src)
|
||||
{
|
||||
std::string ret;
|
||||
if(src.size()%8) {
|
||||
return ret;
|
||||
}
|
||||
bool done = false;
|
||||
for(size_t i = 0; i < src.size() && !done; i += 8) {
|
||||
uint64_t buf = 0;
|
||||
size_t bits = 0;
|
||||
for(size_t j = 0; j < 8; ++j) {
|
||||
char ch = src[i+j];
|
||||
unsigned char value;
|
||||
if('A' <= ch && ch <= 'Z') {
|
||||
value = ch-'A';
|
||||
} else if('2' <= ch && ch <= '7') {
|
||||
value = ch-'2'+26;
|
||||
} else if(ch == '=') {
|
||||
done = true;
|
||||
break;
|
||||
} else {
|
||||
ret.clear();
|
||||
return ret;
|
||||
}
|
||||
buf <<= 5;
|
||||
buf += value;
|
||||
bits += 5;
|
||||
}
|
||||
buf >>= (bits%8);
|
||||
bits = bits/8*8;
|
||||
buf = hton64(buf);
|
||||
char* p = reinterpret_cast<char*>(&buf);
|
||||
ret.append(&p[(64-bits)/8], &p[8]);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
} // namespace base32
|
||||
|
||||
} // namespace aria2
|
||||
|
|
41
src/base32.h
41
src/base32.h
|
@ -39,13 +39,52 @@
|
|||
|
||||
#include <string>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
namespace aria2 {
|
||||
|
||||
namespace base32 {
|
||||
|
||||
std::string encode(const std::string& src);
|
||||
|
||||
std::string decode(const std::string& src);
|
||||
template<typename InputIterator>
|
||||
std::string decode(InputIterator first, InputIterator last)
|
||||
{
|
||||
std::string ret;
|
||||
size_t len = last-first;
|
||||
if(len%8) {
|
||||
return ret;
|
||||
}
|
||||
bool done = false;
|
||||
for(; first != last && !done; first += 8) {
|
||||
uint64_t buf = 0;
|
||||
size_t bits = 0;
|
||||
for(size_t i = 0; i < 8; ++i) {
|
||||
char ch = *(first+i);
|
||||
unsigned char value;
|
||||
if('A' <= ch && ch <= 'Z') {
|
||||
value = ch-'A';
|
||||
} else if('2' <= ch && ch <= '7') {
|
||||
value = ch-'2'+26;
|
||||
} else if(ch == '=') {
|
||||
done = true;
|
||||
break;
|
||||
} else {
|
||||
ret.clear();
|
||||
return ret;
|
||||
}
|
||||
buf <<= 5;
|
||||
buf += value;
|
||||
bits += 5;
|
||||
}
|
||||
buf >>= (bits%8);
|
||||
bits = bits/8*8;
|
||||
buf = hton64(buf);
|
||||
char* p = reinterpret_cast<char*>(&buf);
|
||||
ret.append(&p[(64-bits)/8], &p[8]);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
} // namespace base32
|
||||
|
||||
|
|
|
@ -881,15 +881,14 @@ SharedHandle<TorrentAttribute> parseMagnet(const std::string& magnet)
|
|||
const String* xt = downcast<String>(*xtiter);
|
||||
if(util::startsWith(xt->s().begin(), xt->s().end(),
|
||||
A2_URN_BTIH, vend(A2_URN_BTIH)-1)) {
|
||||
std::string xtarg(xt->s().begin()+9, xt->s().end());
|
||||
size_t size = xtarg.size();
|
||||
size_t size = xt->s().end()-xt->s().begin()-9;
|
||||
if(size == 32) {
|
||||
std::string rawhash = base32::decode(xtarg);
|
||||
std::string rawhash = base32::decode(xt->s().begin()+9, xt->s().end());
|
||||
if(rawhash.size() == 20) {
|
||||
infoHash.swap(rawhash);
|
||||
}
|
||||
} else if(size == 40) {
|
||||
std::string rawhash = util::fromHex(xtarg);
|
||||
std::string rawhash = util::fromHex(xt->s().begin()+9, xt->s().end());
|
||||
if(!rawhash.empty()) {
|
||||
infoHash.swap(rawhash);
|
||||
}
|
||||
|
|
21
src/util.cc
21
src/util.cc
|
@ -486,10 +486,8 @@ std::string toHex(const std::string& src)
|
|||
return toHex(reinterpret_cast<const unsigned char*>(src.c_str()), src.size());
|
||||
}
|
||||
|
||||
namespace {
|
||||
unsigned int hexCharToUInt(unsigned char ch)
|
||||
{
|
||||
|
||||
if('a' <= ch && ch <= 'f') {
|
||||
ch -= 'a';
|
||||
ch += 10;
|
||||
|
@ -503,25 +501,6 @@ unsigned int hexCharToUInt(unsigned char ch)
|
|||
}
|
||||
return ch;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::string fromHex(const std::string& src)
|
||||
{
|
||||
std::string dest;
|
||||
if(src.size()%2) {
|
||||
return dest;
|
||||
}
|
||||
for(size_t i = 0, eoi = src.size(); i < eoi; i += 2) {
|
||||
unsigned char high = hexCharToUInt(src[i]);
|
||||
unsigned char low = hexCharToUInt(src[i+1]);
|
||||
if(high == 255 || low == 255) {
|
||||
dest.clear();
|
||||
return dest;
|
||||
}
|
||||
dest += (high*16+low);
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
|
||||
FILE* openFile(const std::string& filename, const std::string& mode) {
|
||||
FILE* file = fopen(filename.c_str(), mode.c_str());
|
||||
|
|
28
src/util.h
28
src/util.h
|
@ -233,10 +233,30 @@ std::string toHex(const char* src, size_t len);
|
|||
|
||||
std::string toHex(const std::string& src);
|
||||
|
||||
// Converts hexadecimal ascii string 'src' into packed binary form and
|
||||
// return the result. If src is not well formed, then empty string is
|
||||
// returned.
|
||||
std::string fromHex(const std::string& src);
|
||||
unsigned int hexCharToUInt(unsigned char ch);
|
||||
|
||||
// Converts hexadecimal ascii characters in [first, last) into packed
|
||||
// binary form and return the result. If characters in given range is
|
||||
// not well formed, then empty string is returned.
|
||||
template<typename InputIterator>
|
||||
std::string fromHex(InputIterator first, InputIterator last)
|
||||
{
|
||||
std::string dest;
|
||||
size_t len = last-first;
|
||||
if(len%2) {
|
||||
return dest;
|
||||
}
|
||||
for(; first != last; first += 2) {
|
||||
unsigned char high = hexCharToUInt(*first);
|
||||
unsigned char low = hexCharToUInt(*(first+1));
|
||||
if(high == 255 || low == 255) {
|
||||
dest.clear();
|
||||
return dest;
|
||||
}
|
||||
dest += (high*16+low);
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
|
||||
FILE* openFile(const std::string& filename, const std::string& mode);
|
||||
|
||||
|
|
|
@ -28,18 +28,26 @@ void Base32Test::testEncode()
|
|||
CPPUNIT_ASSERT_EQUAL(std::string("GEZDG==="), base32::encode("123"));
|
||||
CPPUNIT_ASSERT_EQUAL(std::string("GEZDGNA="), base32::encode("1234"));
|
||||
CPPUNIT_ASSERT_EQUAL(std::string("GEZDGNBV"), base32::encode("12345"));
|
||||
std::string s = "248d0a1cd08284";
|
||||
CPPUNIT_ASSERT_EQUAL(std::string("ESGQUHGQQKCA===="),
|
||||
base32::encode(util::fromHex("248d0a1cd08284")));
|
||||
base32::encode(util::fromHex(s.begin(), s.end())));
|
||||
}
|
||||
|
||||
void Base32Test::testDecode()
|
||||
{
|
||||
CPPUNIT_ASSERT_EQUAL(std::string(), base32::decode(""));
|
||||
CPPUNIT_ASSERT_EQUAL(std::string("1"), base32::decode("GE======"));
|
||||
CPPUNIT_ASSERT_EQUAL(std::string("12"), base32::decode("GEZA===="));
|
||||
CPPUNIT_ASSERT_EQUAL(std::string("123"), base32::decode("GEZDG==="));
|
||||
CPPUNIT_ASSERT_EQUAL(std::string("1234"), base32::decode("GEZDGNA="));
|
||||
CPPUNIT_ASSERT_EQUAL(std::string("12345"), base32::decode("GEZDGNBV"));
|
||||
std::string s = "";
|
||||
CPPUNIT_ASSERT_EQUAL(std::string(), base32::decode(s.begin(), s.end()));
|
||||
s = "GE======";
|
||||
CPPUNIT_ASSERT_EQUAL(std::string("1"), base32::decode(s.begin(), s.end()));
|
||||
s = "GEZA====";
|
||||
CPPUNIT_ASSERT_EQUAL(std::string("12"), base32::decode(s.begin(), s.end()));
|
||||
s = "GEZDG===";
|
||||
CPPUNIT_ASSERT_EQUAL(std::string("123"), base32::decode(s.begin(), s.end()));
|
||||
s = "GEZDGNA=";
|
||||
CPPUNIT_ASSERT_EQUAL(std::string("1234"), base32::decode(s.begin(), s.end()));
|
||||
s = "GEZDGNBV";
|
||||
CPPUNIT_ASSERT_EQUAL(std::string("12345"),
|
||||
base32::decode(s.begin(), s.end()));
|
||||
}
|
||||
|
||||
} // namespace aria2
|
||||
|
|
|
@ -417,7 +417,7 @@ void BittorrentHelperTest::testLoadFromMemory_multiFileNonUtf8Path()
|
|||
{
|
||||
SharedHandle<List> path = List::g();
|
||||
path->append("path");
|
||||
path->append(util::fromHex("90a28a")+"E");
|
||||
path->append(fromHex("90a28a")+"E");
|
||||
SharedHandle<Dict> file = Dict::g();
|
||||
file->put("length", Integer::g(1024));
|
||||
file->put("path", path);
|
||||
|
@ -427,7 +427,7 @@ void BittorrentHelperTest::testLoadFromMemory_multiFileNonUtf8Path()
|
|||
info->put("files", files);
|
||||
info->put("piece length", Integer::g(1024));
|
||||
info->put("pieces", "01234567890123456789");
|
||||
info->put("name", util::fromHex("1b")+"$B%O%m!<"+util::fromHex("1b")+"(B");
|
||||
info->put("name", fromHex("1b")+"$B%O%m!<"+fromHex("1b")+"(B");
|
||||
Dict dict;
|
||||
dict.put("info", info);
|
||||
SharedHandle<DownloadContext> dctx(new DownloadContext());
|
||||
|
@ -446,7 +446,7 @@ void BittorrentHelperTest::testLoadFromMemory_singleFileNonUtf8Path()
|
|||
SharedHandle<Dict> info = Dict::g();
|
||||
info->put("piece length", Integer::g(1024));
|
||||
info->put("pieces", "01234567890123456789");
|
||||
info->put("name", util::fromHex("90a28a")+"E");
|
||||
info->put("name", fromHex("90a28a")+"E");
|
||||
info->put("length", Integer::g(1024));
|
||||
Dict dict;
|
||||
dict.put("info", info);
|
||||
|
@ -787,7 +787,7 @@ void BittorrentHelperTest::testParseMagnet()
|
|||
void BittorrentHelperTest::testParseMagnet_base32()
|
||||
{
|
||||
std::string infoHash = "248d0a1cd08284299de78d5c1ed359bb46717d8c";
|
||||
std::string base32InfoHash = base32::encode(util::fromHex(infoHash));
|
||||
std::string base32InfoHash = base32::encode(fromHex(infoHash));
|
||||
std::string magnet = "magnet:?xt=urn:btih:"+base32InfoHash+"&dn=aria2";
|
||||
SharedHandle<TorrentAttribute> attrs = bittorrent::parseMagnet(magnet);
|
||||
CPPUNIT_ASSERT_EQUAL
|
||||
|
@ -830,7 +830,7 @@ void BittorrentHelperTest::testExtractPeerFromString()
|
|||
{
|
||||
std::string hextext = "100210354527354678541237324732171ae1";
|
||||
hextext += "20010db8bd0501d2288a1fc0000110ee1ae2";
|
||||
std::string peersstr = "36:"+util::fromHex(hextext);
|
||||
std::string peersstr = "36:"+fromHex(hextext);
|
||||
SharedHandle<ValueBase> str =
|
||||
bencode2::decode(peersstr.begin(), peersstr.end());
|
||||
std::deque<SharedHandle<Peer> > peers;
|
||||
|
@ -845,7 +845,7 @@ void BittorrentHelperTest::testExtractPeerFromString()
|
|||
|
||||
hextext = "c0a800011ae1";
|
||||
hextext += "c0a800021ae2";
|
||||
peersstr = "12:"+util::fromHex(hextext);
|
||||
peersstr = "12:"+fromHex(hextext);
|
||||
str = bencode2::decode(peersstr.begin(), peersstr.end());
|
||||
peers.clear();
|
||||
extractPeer(str, AF_INET, std::back_inserter(peers));
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
#include <cppunit/extensions/HelperMacros.h>
|
||||
|
||||
#include "TestUtil.h"
|
||||
#include "Option.h"
|
||||
#include "util.h"
|
||||
#include "Exception.h"
|
||||
|
@ -391,9 +392,9 @@ void DefaultBtAnnounceTest::testProcessAnnounceResponse()
|
|||
"8:completei100e"
|
||||
"10:incompletei200e";
|
||||
res += "5:peers6:";
|
||||
res += util::fromHex("c0a800011ae1");
|
||||
res += fromHex("c0a800011ae1");
|
||||
res += "6:peers618:";
|
||||
res += util::fromHex("100210354527354678541237324732171ae1");
|
||||
res += fromHex("100210354527354678541237324732171ae1");
|
||||
res += "e";
|
||||
|
||||
DefaultBtAnnounce an(dctx_, option_);
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include <cppunit/extensions/HelperMacros.h>
|
||||
|
||||
#include "TestUtil.h"
|
||||
#include "DownloadContext.h"
|
||||
#include "DefaultPieceStorage.h"
|
||||
#include "Option.h"
|
||||
|
@ -35,7 +36,7 @@ void IteratableChecksumValidatorTest::testValidate() {
|
|||
SharedHandle<DownloadContext> dctx
|
||||
(new DownloadContext(100, 250, A2_TEST_DIR"/chunkChecksumTestFile250.txt"));
|
||||
dctx->setDigest("sha-1",
|
||||
util::fromHex("898a81b8e0181280ae2ee1b81e269196d91e869a"));
|
||||
fromHex("898a81b8e0181280ae2ee1b81e269196d91e869a"));
|
||||
SharedHandle<DefaultPieceStorage> ps(new DefaultPieceStorage(dctx, &option));
|
||||
ps->initStorage();
|
||||
ps->getDiskAdaptor()->enableReadOnly();
|
||||
|
@ -55,7 +56,7 @@ void IteratableChecksumValidatorTest::testValidate_fail() {
|
|||
SharedHandle<DownloadContext> dctx
|
||||
(new DownloadContext(100, 250, A2_TEST_DIR"/chunkChecksumTestFile250.txt"));
|
||||
dctx->setDigest("sha-1",
|
||||
util::fromHex(std::string(40, '0'))); // set wrong checksum
|
||||
fromHex(std::string(40, '0'))); // set wrong checksum
|
||||
SharedHandle<DefaultPieceStorage> ps(new DefaultPieceStorage(dctx, &option));
|
||||
ps->initStorage();
|
||||
ps->getDiskAdaptor()->enableReadOnly();
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include <cppunit/extensions/HelperMacros.h>
|
||||
|
||||
#include "TestUtil.h"
|
||||
#include "DownloadContext.h"
|
||||
#include "DefaultPieceStorage.h"
|
||||
#include "Option.h"
|
||||
|
@ -32,9 +33,9 @@ public:
|
|||
CPPUNIT_TEST_SUITE_REGISTRATION( IteratableChunkChecksumValidatorTest );
|
||||
|
||||
const std::string IteratableChunkChecksumValidatorTest::csArray[] =
|
||||
{ util::fromHex("29b0e7878271645fffb7eec7db4a7473a1c00bc1"),
|
||||
util::fromHex("4df75a661cb7eb2733d9cdaa7f772eae3a4e2976"),
|
||||
util::fromHex("0a4ea2f7dd7c52ddf2099a444ab2184b4d341bdb") };
|
||||
{ fromHex("29b0e7878271645fffb7eec7db4a7473a1c00bc1"),
|
||||
fromHex("4df75a661cb7eb2733d9cdaa7f772eae3a4e2976"),
|
||||
fromHex("0a4ea2f7dd7c52ddf2099a444ab2184b4d341bdb") };
|
||||
|
||||
void IteratableChunkChecksumValidatorTest::testValidate() {
|
||||
Option option;
|
||||
|
@ -60,7 +61,7 @@ void IteratableChunkChecksumValidatorTest::testValidate() {
|
|||
|
||||
// make the test fail
|
||||
std::deque<std::string> badHashes(&csArray[0], &csArray[3]);
|
||||
badHashes[1] = util::fromHex("ffffffffffffffffffffffffffffffffffffffff");
|
||||
badHashes[1] = fromHex("ffffffffffffffffffffffffffffffffffffffff");
|
||||
dctx->setPieceHashes("sha-1", badHashes.begin(), badHashes.end());
|
||||
|
||||
validator.init();
|
||||
|
@ -78,8 +79,8 @@ void IteratableChunkChecksumValidatorTest::testValidate_readError() {
|
|||
SharedHandle<DownloadContext> dctx
|
||||
(new DownloadContext(100, 500, A2_TEST_DIR"/chunkChecksumTestFile250.txt"));
|
||||
std::deque<std::string> hashes(&csArray[0], &csArray[3]);
|
||||
hashes.push_back(util::fromHex("ffffffffffffffffffffffffffffffffffffffff"));
|
||||
hashes.push_back(util::fromHex("ffffffffffffffffffffffffffffffffffffffff"));
|
||||
hashes.push_back(fromHex("ffffffffffffffffffffffffffffffffffffffff"));
|
||||
hashes.push_back(fromHex("ffffffffffffffffffffffffffffffffffffffff"));
|
||||
dctx->setPieceHashes("sha-1", hashes.begin(), hashes.end());
|
||||
SharedHandle<DefaultPieceStorage> ps(new DefaultPieceStorage(dctx, &option));
|
||||
ps->initStorage();
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
|
||||
#include <cppunit/extensions/HelperMacros.h>
|
||||
|
||||
#include "TestUtil.h"
|
||||
#include "Exception.h"
|
||||
#include "util.h"
|
||||
#include "LpdMessageDispatcher.h"
|
||||
|
@ -37,7 +38,7 @@ void LpdMessageDispatcherTest::testCreateLpdRequest()
|
|||
"Infohash: cd41c7fdddfd034a15a04d7ff881216e01c4ceaf\r\n"
|
||||
"\r\n\r\n"),
|
||||
bittorrent::createLpdRequest("239.192.152.143", 6771,
|
||||
util::fromHex(infoHashString), 6000));
|
||||
fromHex(infoHashString), 6000));
|
||||
}
|
||||
|
||||
void LpdMessageDispatcherTest::testSendMessage()
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
#include <cppunit/extensions/HelperMacros.h>
|
||||
|
||||
#include "TestUtil.h"
|
||||
#include "Exception.h"
|
||||
#include "util.h"
|
||||
#include "LpdMessageReceiver.h"
|
||||
|
@ -39,7 +40,7 @@ void LpdMessageReceiverTest::testReceiveMessage()
|
|||
sendsock->setMulticastTtl(0);
|
||||
|
||||
std::string infoHashString = "cd41c7fdddfd034a15a04d7ff881216e01c4ceaf";
|
||||
std::string infoHash = util::fromHex(infoHashString);
|
||||
std::string infoHash = fromHex(infoHashString);
|
||||
std::string request =
|
||||
bittorrent::createLpdRequest(LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT,
|
||||
infoHash,
|
||||
|
@ -59,7 +60,7 @@ void LpdMessageReceiverTest::testReceiveMessage()
|
|||
std::string badInfoHashString = "cd41c7fdddfd034a15a04d7ff881216e01c4ce";
|
||||
request =
|
||||
bittorrent::createLpdRequest(LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT,
|
||||
util::fromHex(badInfoHashString),
|
||||
fromHex(badInfoHashString),
|
||||
6000);
|
||||
sendsock->writeData(request.c_str(), request.size(),
|
||||
LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT);
|
||||
|
|
|
@ -76,6 +76,11 @@ Cookie createCookie
|
|||
(name, value, expiryTime, true, domain, hostOnly, path, secure, false, 0);
|
||||
}
|
||||
|
||||
std::string fromHex(const std::string& s)
|
||||
{
|
||||
return util::fromHex(s.begin(), s.end());
|
||||
}
|
||||
|
||||
#ifdef ENABLE_MESSAGE_DIGEST
|
||||
std::string fileHexDigest
|
||||
(const SharedHandle<MessageDigest>& ctx, const std::string& filename)
|
||||
|
|
|
@ -42,6 +42,8 @@ Cookie createCookie
|
|||
const std::string& path,
|
||||
bool secure);
|
||||
|
||||
std::string fromHex(const std::string& s);
|
||||
|
||||
#ifdef ENABLE_MESSAGE_DIGEST
|
||||
// Returns hex digest of contents of file denoted by filename.
|
||||
std::string fileHexDigest
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
#include <cppunit/extensions/HelperMacros.h>
|
||||
|
||||
#include "TestUtil.h"
|
||||
#include "Peer.h"
|
||||
#include "a2netcompat.h"
|
||||
#include "util.h"
|
||||
|
@ -103,9 +104,9 @@ void UTPexExtensionMessageTest::testGetBencodedData()
|
|||
|
||||
std::string expected = "d5:added12:"+
|
||||
std::string(&c1[0], &c1[6])+std::string(&c2[0], &c2[6])+
|
||||
"7:added.f2:"+util::fromHex("0200")+
|
||||
"7:added.f2:"+fromHex("0200")+
|
||||
"6:added618:"+std::string(&c5[0], &c5[COMPACT_LEN_IPV6])+
|
||||
"8:added6.f1:"+util::fromHex("00")+
|
||||
"8:added6.f1:"+fromHex("00")+
|
||||
"7:dropped12:"+std::string(&c3[0], &c3[6])+std::string(&c4[0], &c4[6])+
|
||||
"8:dropped618:"+std::string(&c6[0], &c6[COMPACT_LEN_IPV6])+
|
||||
"e";
|
||||
|
@ -194,9 +195,9 @@ void UTPexExtensionMessageTest::testCreate()
|
|||
|
||||
std::string data = std::string(&id[0], &id[1])+"d5:added12:"+
|
||||
std::string(&c1[0], &c1[6])+std::string(&c2[0], &c2[6])+
|
||||
"7:added.f2:"+util::fromHex("0200")+
|
||||
"7:added.f2:"+fromHex("0200")+
|
||||
"6:added618:"+std::string(&c5[0], &c5[COMPACT_LEN_IPV6])+
|
||||
"8:added6.f1:"+util::fromHex("00")+
|
||||
"8:added6.f1:"+fromHex("00")+
|
||||
"7:dropped12:"+std::string(&c3[0], &c3[6])+std::string(&c4[0], &c4[6])+
|
||||
"8:dropped618:"+std::string(&c6[0], &c6[COMPACT_LEN_IPV6])+
|
||||
"e";
|
||||
|
|
|
@ -1553,17 +1553,17 @@ void UtilTest::testFromHex()
|
|||
std::string dest;
|
||||
|
||||
src = "0011fF";
|
||||
dest = util::fromHex(src);
|
||||
dest = util::fromHex(src.begin(), src.end());
|
||||
CPPUNIT_ASSERT_EQUAL((size_t)3, dest.size());
|
||||
CPPUNIT_ASSERT_EQUAL((char)0x00, dest[0]);
|
||||
CPPUNIT_ASSERT_EQUAL((char)0x11, dest[1]);
|
||||
CPPUNIT_ASSERT_EQUAL((char)0xff, dest[2]);
|
||||
|
||||
src = "0011f";
|
||||
CPPUNIT_ASSERT(util::fromHex(src).empty());
|
||||
CPPUNIT_ASSERT(util::fromHex(src.begin(), src.end()).empty());
|
||||
|
||||
src = "001g";
|
||||
CPPUNIT_ASSERT(util::fromHex(src).empty());
|
||||
CPPUNIT_ASSERT(util::fromHex(src.begin(), src.end()).empty());
|
||||
}
|
||||
|
||||
void UtilTest::testParsePrioritizePieceRange()
|
||||
|
@ -1720,31 +1720,31 @@ void UtilTest::testIsUtf8String()
|
|||
CPPUNIT_ASSERT(util::isUtf8("ascii"));
|
||||
// "Hello World" in Japanese UTF-8
|
||||
CPPUNIT_ASSERT(util::isUtf8
|
||||
(util::fromHex("e38193e38293e381abe381a1e381afe4b896e7958c")));
|
||||
(fromHex("e38193e38293e381abe381a1e381afe4b896e7958c")));
|
||||
// "World" in Shift_JIS
|
||||
CPPUNIT_ASSERT(!util::isUtf8(util::fromHex("90a28a")+"E"));
|
||||
CPPUNIT_ASSERT(!util::isUtf8(fromHex("90a28a")+"E"));
|
||||
// UTF8-2
|
||||
CPPUNIT_ASSERT(util::isUtf8(util::fromHex("c280")));
|
||||
CPPUNIT_ASSERT(util::isUtf8(util::fromHex("dfbf")));
|
||||
CPPUNIT_ASSERT(util::isUtf8(fromHex("c280")));
|
||||
CPPUNIT_ASSERT(util::isUtf8(fromHex("dfbf")));
|
||||
// UTF8-3
|
||||
CPPUNIT_ASSERT(util::isUtf8(util::fromHex("e0a080")));
|
||||
CPPUNIT_ASSERT(util::isUtf8(util::fromHex("e0bf80")));
|
||||
CPPUNIT_ASSERT(util::isUtf8(util::fromHex("e18080")));
|
||||
CPPUNIT_ASSERT(util::isUtf8(util::fromHex("ec8080")));
|
||||
CPPUNIT_ASSERT(util::isUtf8(util::fromHex("ed8080")));
|
||||
CPPUNIT_ASSERT(util::isUtf8(util::fromHex("ed9f80")));
|
||||
CPPUNIT_ASSERT(util::isUtf8(util::fromHex("ee8080")));
|
||||
CPPUNIT_ASSERT(util::isUtf8(util::fromHex("ef8080")));
|
||||
CPPUNIT_ASSERT(util::isUtf8(fromHex("e0a080")));
|
||||
CPPUNIT_ASSERT(util::isUtf8(fromHex("e0bf80")));
|
||||
CPPUNIT_ASSERT(util::isUtf8(fromHex("e18080")));
|
||||
CPPUNIT_ASSERT(util::isUtf8(fromHex("ec8080")));
|
||||
CPPUNIT_ASSERT(util::isUtf8(fromHex("ed8080")));
|
||||
CPPUNIT_ASSERT(util::isUtf8(fromHex("ed9f80")));
|
||||
CPPUNIT_ASSERT(util::isUtf8(fromHex("ee8080")));
|
||||
CPPUNIT_ASSERT(util::isUtf8(fromHex("ef8080")));
|
||||
// UTF8-4
|
||||
CPPUNIT_ASSERT(util::isUtf8(util::fromHex("f0908080")));
|
||||
CPPUNIT_ASSERT(util::isUtf8(util::fromHex("f0bf8080")));
|
||||
CPPUNIT_ASSERT(util::isUtf8(util::fromHex("f1808080")));
|
||||
CPPUNIT_ASSERT(util::isUtf8(util::fromHex("f3808080")));
|
||||
CPPUNIT_ASSERT(util::isUtf8(util::fromHex("f4808080")));
|
||||
CPPUNIT_ASSERT(util::isUtf8(util::fromHex("f48f8080")));
|
||||
CPPUNIT_ASSERT(util::isUtf8(fromHex("f0908080")));
|
||||
CPPUNIT_ASSERT(util::isUtf8(fromHex("f0bf8080")));
|
||||
CPPUNIT_ASSERT(util::isUtf8(fromHex("f1808080")));
|
||||
CPPUNIT_ASSERT(util::isUtf8(fromHex("f3808080")));
|
||||
CPPUNIT_ASSERT(util::isUtf8(fromHex("f4808080")));
|
||||
CPPUNIT_ASSERT(util::isUtf8(fromHex("f48f8080")));
|
||||
|
||||
CPPUNIT_ASSERT(util::isUtf8(""));
|
||||
CPPUNIT_ASSERT(!util::isUtf8(util::fromHex("00")));
|
||||
CPPUNIT_ASSERT(!util::isUtf8(fromHex("00")));
|
||||
}
|
||||
|
||||
void UtilTest::testNextParam()
|
||||
|
|
Loading…
Reference in New Issue