From 0c162dfbfb4a875fac9374c65cf93382ff9ce4ed Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sat, 5 Nov 2011 18:05:23 +0900 Subject: [PATCH] Made util::fromHex and base32::decode function template --- src/LpdMessageReceiver.cc | 5 ++- src/MetalinkParserController.cc | 6 +-- src/RpcMethodImpl.cc | 4 +- src/base32.cc | 37 ---------------- src/base32.h | 41 +++++++++++++++++- src/bittorrent_helper.cc | 7 ++-- src/util.cc | 21 ---------- src/util.h | 28 +++++++++++-- test/Base32Test.cc | 22 ++++++---- test/BittorrentHelperTest.cc | 12 +++--- test/DefaultBtAnnounceTest.cc | 5 ++- test/IteratableChecksumValidatorTest.cc | 5 ++- test/IteratableChunkChecksumValidatorTest.cc | 13 +++--- test/LpdMessageDispatcherTest.cc | 3 +- test/LpdMessageReceiverTest.cc | 5 ++- test/TestUtil.cc | 5 +++ test/TestUtil.h | 2 + test/UTPexExtensionMessageTest.cc | 9 ++-- test/UtilTest.cc | 44 ++++++++++---------- 19 files changed, 147 insertions(+), 127 deletions(-) diff --git a/src/LpdMessageReceiver.cc b/src/LpdMessageReceiver.cc index f5fd290c..e46dc915 100644 --- a/src/LpdMessageReceiver.cc +++ b/src/LpdMessageReceiver.cc @@ -94,7 +94,7 @@ SharedHandle LpdMessageReceiver::receiveMessage() return msg; } SharedHandle 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 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()); diff --git a/src/MetalinkParserController.cc b/src/MetalinkParserController.cc index 0d886a49..51146930 100644 --- a/src/MetalinkParserController.cc +++ b/src/MetalinkParserController.cc @@ -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(); } diff --git a/src/RpcMethodImpl.cc b/src/RpcMethodImpl.cc index ac709670..5c402d31 100644 --- a/src/RpcMethodImpl.cc +++ b/src/RpcMethodImpl.cc @@ -1130,10 +1130,8 @@ void changeOption std::pair 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 sgl; diff --git a/src/base32.cc b/src/base32.cc index 112964fe..fbfcc189 100644 --- a/src/base32.cc +++ b/src/base32.cc @@ -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(&buf); - ret.append(&p[(64-bits)/8], &p[8]); - } - return ret; -} - } // namespace base32 } // namespace aria2 diff --git a/src/base32.h b/src/base32.h index 0065b62c..a93f9180 100644 --- a/src/base32.h +++ b/src/base32.h @@ -39,13 +39,52 @@ #include +#include "util.h" + namespace aria2 { namespace base32 { std::string encode(const std::string& src); -std::string decode(const std::string& src); +template +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(&buf); + ret.append(&p[(64-bits)/8], &p[8]); + } + return ret; +} } // namespace base32 diff --git a/src/bittorrent_helper.cc b/src/bittorrent_helper.cc index 27f82443..2c1101ca 100644 --- a/src/bittorrent_helper.cc +++ b/src/bittorrent_helper.cc @@ -881,15 +881,14 @@ SharedHandle parseMagnet(const std::string& magnet) const String* xt = downcast(*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); } diff --git a/src/util.cc b/src/util.cc index cf255939..a9eec6ac 100644 --- a/src/util.cc +++ b/src/util.cc @@ -486,10 +486,8 @@ std::string toHex(const std::string& src) return toHex(reinterpret_cast(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()); diff --git a/src/util.h b/src/util.h index 1af2c86a..6f577ec0 100644 --- a/src/util.h +++ b/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 +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); diff --git a/test/Base32Test.cc b/test/Base32Test.cc index 8b86c4d2..03d52e8d 100644 --- a/test/Base32Test.cc +++ b/test/Base32Test.cc @@ -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 diff --git a/test/BittorrentHelperTest.cc b/test/BittorrentHelperTest.cc index 0e4c941d..64428600 100644 --- a/test/BittorrentHelperTest.cc +++ b/test/BittorrentHelperTest.cc @@ -417,7 +417,7 @@ void BittorrentHelperTest::testLoadFromMemory_multiFileNonUtf8Path() { SharedHandle path = List::g(); path->append("path"); - path->append(util::fromHex("90a28a")+"E"); + path->append(fromHex("90a28a")+"E"); SharedHandle 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 dctx(new DownloadContext()); @@ -446,7 +446,7 @@ void BittorrentHelperTest::testLoadFromMemory_singleFileNonUtf8Path() SharedHandle 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 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 str = bencode2::decode(peersstr.begin(), peersstr.end()); std::deque > 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)); diff --git a/test/DefaultBtAnnounceTest.cc b/test/DefaultBtAnnounceTest.cc index 2ab3c586..c628837a 100644 --- a/test/DefaultBtAnnounceTest.cc +++ b/test/DefaultBtAnnounceTest.cc @@ -4,6 +4,7 @@ #include +#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_); diff --git a/test/IteratableChecksumValidatorTest.cc b/test/IteratableChecksumValidatorTest.cc index 8bc7f1e1..9e224045 100644 --- a/test/IteratableChecksumValidatorTest.cc +++ b/test/IteratableChecksumValidatorTest.cc @@ -2,6 +2,7 @@ #include +#include "TestUtil.h" #include "DownloadContext.h" #include "DefaultPieceStorage.h" #include "Option.h" @@ -35,7 +36,7 @@ void IteratableChecksumValidatorTest::testValidate() { SharedHandle dctx (new DownloadContext(100, 250, A2_TEST_DIR"/chunkChecksumTestFile250.txt")); dctx->setDigest("sha-1", - util::fromHex("898a81b8e0181280ae2ee1b81e269196d91e869a")); + fromHex("898a81b8e0181280ae2ee1b81e269196d91e869a")); SharedHandle ps(new DefaultPieceStorage(dctx, &option)); ps->initStorage(); ps->getDiskAdaptor()->enableReadOnly(); @@ -55,7 +56,7 @@ void IteratableChecksumValidatorTest::testValidate_fail() { SharedHandle 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 ps(new DefaultPieceStorage(dctx, &option)); ps->initStorage(); ps->getDiskAdaptor()->enableReadOnly(); diff --git a/test/IteratableChunkChecksumValidatorTest.cc b/test/IteratableChunkChecksumValidatorTest.cc index 921e9338..3e1cac84 100644 --- a/test/IteratableChunkChecksumValidatorTest.cc +++ b/test/IteratableChunkChecksumValidatorTest.cc @@ -2,6 +2,7 @@ #include +#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 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 dctx (new DownloadContext(100, 500, A2_TEST_DIR"/chunkChecksumTestFile250.txt")); std::deque 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 ps(new DefaultPieceStorage(dctx, &option)); ps->initStorage(); diff --git a/test/LpdMessageDispatcherTest.cc b/test/LpdMessageDispatcherTest.cc index 3324bf84..d328e16d 100644 --- a/test/LpdMessageDispatcherTest.cc +++ b/test/LpdMessageDispatcherTest.cc @@ -5,6 +5,7 @@ #include +#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() diff --git a/test/LpdMessageReceiverTest.cc b/test/LpdMessageReceiverTest.cc index 3d2aa0a0..ed42bfb2 100644 --- a/test/LpdMessageReceiverTest.cc +++ b/test/LpdMessageReceiverTest.cc @@ -4,6 +4,7 @@ #include +#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); diff --git a/test/TestUtil.cc b/test/TestUtil.cc index fee39055..6dd5d811 100644 --- a/test/TestUtil.cc +++ b/test/TestUtil.cc @@ -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& ctx, const std::string& filename) diff --git a/test/TestUtil.h b/test/TestUtil.h index 1dd010ee..9dc66b87 100644 --- a/test/TestUtil.h +++ b/test/TestUtil.h @@ -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 diff --git a/test/UTPexExtensionMessageTest.cc b/test/UTPexExtensionMessageTest.cc index eb0c3178..00913de8 100644 --- a/test/UTPexExtensionMessageTest.cc +++ b/test/UTPexExtensionMessageTest.cc @@ -4,6 +4,7 @@ #include +#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"; diff --git a/test/UtilTest.cc b/test/UtilTest.cc index e6e330c8..ae50c489 100644 --- a/test/UtilTest.cc +++ b/test/UtilTest.cc @@ -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()