summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2011-11-05 18:05:23 +0900
committerTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2011-11-05 18:05:23 +0900
commit0c162dfbfb4a875fac9374c65cf93382ff9ce4ed (patch)
tree6fbc9cc204305017438d9511db631b9b2e7fc538 /src
parent6ea1b68db186036e9f05c06915f8c4bd757c3a25 (diff)
Made util::fromHex and base32::decode function template
Diffstat (limited to 'src')
-rw-r--r--src/LpdMessageReceiver.cc5
-rw-r--r--src/MetalinkParserController.cc6
-rw-r--r--src/RpcMethodImpl.cc4
-rw-r--r--src/base32.cc37
-rw-r--r--src/base32.h41
-rw-r--r--src/bittorrent_helper.cc7
-rw-r--r--src/util.cc21
-rw-r--r--src/util.h28
8 files changed, 74 insertions, 75 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<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());
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<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;
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<char*>(&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 <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
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<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);
}
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<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());
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<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);