diff --git a/include/libtorrent/escape_string.hpp b/include/libtorrent/escape_string.hpp index b663d094d..660dd4d08 100755 --- a/include/libtorrent/escape_string.hpp +++ b/include/libtorrent/escape_string.hpp @@ -51,6 +51,8 @@ namespace libtorrent TORRENT_EXPORT boost::optional url_has_argument( std::string const& url, std::string argument); + + TORRENT_EXPORT std::string to_hex(std::string const& s); } #endif // TORRENT_ESCAPE_STRING_HPP_INCLUDED diff --git a/src/entry.cpp b/src/entry.cpp index 0dd9a43fc..59e5b061d 100755 --- a/src/entry.cpp +++ b/src/entry.cpp @@ -35,8 +35,10 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include #include "libtorrent/entry.hpp" #include "libtorrent/config.hpp" +#include "libtorrent/escape_string.hpp" #if defined(_MSC_VER) namespace std @@ -371,21 +373,8 @@ namespace libtorrent break; } } - if (binary_string) - { - os.unsetf(std::ios_base::dec); - os.setf(std::ios_base::hex); - for (std::string::const_iterator i = string().begin(); i != string().end(); ++i) - os << std::setfill('0') << std::setw(2) - << static_cast((unsigned char)*i); - os.unsetf(std::ios_base::hex); - os.setf(std::ios_base::dec); - os << "\n"; - } - else - { - os << string() << "\n"; - } + if (binary_string) os << to_hex(string()) << "\n"; + else os << string() << "\n"; } break; case list_t: { @@ -400,8 +389,21 @@ namespace libtorrent os << "dictionary\n"; for (dictionary_type::const_iterator i = dict().begin(); i != dict().end(); ++i) { + bool binary_string = false; + for (std::string::const_iterator k = i->first.begin(); k != i->first.end(); ++k) + { + if (!std::isprint(static_cast(*k))) + { + binary_string = true; + break; + } + } for (int j = 0; j < indent+1; ++j) os << " "; - os << "[" << i->first << "]"; + os << "["; + if (binary_string) os << to_hex(i->first); + else os << i->first; + os << "]"; + if (i->second.type() != entry::string_t && i->second.type() != entry::int_t) os << "\n"; diff --git a/src/escape_string.cpp b/src/escape_string.cpp index 186b34f88..616570014 100755 --- a/src/escape_string.cpp +++ b/src/escape_string.cpp @@ -352,5 +352,17 @@ namespace libtorrent return url.substr(pos, url.find('&', pos) - pos); } + TORRENT_EXPORT std::string to_hex(std::string const& s) + { + std::string ret; + char* digits = "0123456789abcdef"; + for (std::string::const_iterator i = s.begin(); i != s.end(); ++i) + { + ret += digits[((unsigned char)*i) >> 4]; + ret += digits[((unsigned char)*i) & 0xf]; + } + return ret; + } + } diff --git a/src/kademlia/dht_tracker.cpp b/src/kademlia/dht_tracker.cpp index bd0cfde86..461ea9da6 100644 --- a/src/kademlia/dht_tracker.cpp +++ b/src/kademlia/dht_tracker.cpp @@ -51,6 +51,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/bencode.hpp" #include "libtorrent/io.hpp" #include "libtorrent/version.hpp" +#include "libtorrent/escape_string.hpp" using boost::ref; using boost::lexical_cast; @@ -137,19 +138,6 @@ namespace libtorrent { namespace dht #ifdef TORRENT_DHT_VERBOSE_LOGGING TORRENT_DEFINE_LOG(dht_tracker) - - std::string to_hex(std::string const& s) - { - std::string ret; - char* digits = "0123456789abcdef"; - for (std::string::const_iterator i = s.begin(); i != s.end(); ++i) - { - ret += digits[((unsigned char)*i) >> 4]; - ret += digits[((unsigned char)*i) & 0xf]; - } - return ret; - } - #endif // class that puts the networking and the kademlia node in a single