diff --git a/include/libtorrent/entry.hpp b/include/libtorrent/entry.hpp index 3f3b6e0a6..2b6bcbe2c 100644 --- a/include/libtorrent/entry.hpp +++ b/include/libtorrent/entry.hpp @@ -59,7 +59,6 @@ POSSIBILITY OF SUCH DAMAGE. */ -#include #include #include #include @@ -70,6 +69,10 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/assert.hpp" #include "libtorrent/error_code.hpp" +#if TORRENT_USE_IOSTREAM +#include +#endif + namespace libtorrent { @@ -171,7 +174,9 @@ namespace libtorrent entry* find_key(std::string const& key); entry const* find_key(std::string const& key) const; +#if TORRENT_USE_IOSTREAM void print(std::ostream& os, int indent = 0) const; +#endif protected: @@ -219,11 +224,13 @@ namespace libtorrent #endif }; +#if TORRENT_USE_IOSTREAM inline std::ostream& operator<<(std::ostream& os, const entry& e) { e.print(os, 0); return os; } +#endif #ifndef BOOST_NO_EXCEPTIONS inline void throw_type_error() diff --git a/include/libtorrent/peer_id.hpp b/include/libtorrent/peer_id.hpp index 50274eebe..da04a52f7 100644 --- a/include/libtorrent/peer_id.hpp +++ b/include/libtorrent/peer_id.hpp @@ -175,7 +175,7 @@ namespace libtorrent { char hex[40]; is.read(hex, 40); - if (from_hex(hex, 40, (char*)&peer[0]) == -1) + if (!from_hex(hex, 40, (char*)&peer[0])) is.setstate(std::ios_base::failbit); return is; } diff --git a/src/bt_peer_connection.cpp b/src/bt_peer_connection.cpp index f99caf0f7..782ac6958 100644 --- a/src/bt_peer_connection.cpp +++ b/src/bt_peer_connection.cpp @@ -1841,7 +1841,7 @@ namespace libtorrent i.begin += msg.size(); TORRENT_ASSERT(i.begin == i.end); -#ifdef TORRENT_VERBOSE_LOGGING +#if defined TORRENT_VERBOSE_LOGGING && TORRENT_USE_IOSTREAM std::stringstream ext; handshake.print(ext); (*m_logger) << "==> EXTENDED HANDSHAKE: \n" << ext.str(); diff --git a/src/escape_string.cpp b/src/escape_string.cpp index cb5c104fd..de7f723f6 100644 --- a/src/escape_string.cpp +++ b/src/escape_string.cpp @@ -425,6 +425,7 @@ namespace libtorrent int t = hex_to_int(*in); if (t == -1) return false; *out = t << 4; + ++in; t = hex_to_int(*in); if (t == -1) return false; *out |= t & 15; diff --git a/test/setup_transfer.cpp b/test/setup_transfer.cpp index f61895db2..9bf5a7174 100644 --- a/test/setup_transfer.cpp +++ b/test/setup_transfer.cpp @@ -279,7 +279,9 @@ setup_transfer(session* ses1, session* ses2, session* ses3 remove_all("./tmp2" + suffix + "/temporary"); remove_all("./tmp3" + suffix + "/temporary"); } - std::cerr << "generated torrent: " << t->info_hash() << std::endl; + char ih_hex[41]; + to_hex((char const*)&t->info_hash()[0], 20, ih_hex); + std::cerr << "generated torrent: " << ih_hex << std::endl; } else { diff --git a/test/test_bencoding.cpp b/test/test_bencoding.cpp index b1cdd5900..027d30940 100644 --- a/test/test_bencoding.cpp +++ b/test/test_bencoding.cpp @@ -109,7 +109,9 @@ int test_main() lazy_entry e; int ret = lazy_bdecode(b, b + sizeof(b)-1, e); TORRENT_ASSERT(ret == 0); +#if TORRENT_USE_IOSTREAM std::cout << e << std::endl; +#endif std::pair section = e.data_section(); TORRENT_ASSERT(std::memcmp(b, section.first, section.second) == 0); TORRENT_ASSERT(section.second == sizeof(b) - 1); @@ -122,7 +124,9 @@ int test_main() lazy_entry e; int ret = lazy_bdecode(b, b + sizeof(b)-1, e); TORRENT_ASSERT(ret == 0); +#if TORRENT_USE_IOSTREAM std::cout << e << std::endl; +#endif std::pair section = e.data_section(); TORRENT_ASSERT(std::memcmp(b, section.first, section.second) == 0); TORRENT_ASSERT(section.second == sizeof(b) - 1); @@ -136,7 +140,9 @@ int test_main() lazy_entry e; int ret = lazy_bdecode(b, b + sizeof(b)-1, e); TORRENT_ASSERT(ret == 0); +#if TORRENT_USE_IOSTREAM std::cout << e << std::endl; +#endif std::pair section = e.data_section(); TORRENT_ASSERT(std::memcmp(b, section.first, section.second) == 0); TORRENT_ASSERT(section.second == sizeof(b) - 1); @@ -157,7 +163,9 @@ int test_main() lazy_entry e; int ret = lazy_bdecode(b, b + sizeof(b)-1, e); TORRENT_ASSERT(ret == 0); +#if TORRENT_USE_IOSTREAM std::cout << e << std::endl; +#endif std::pair section = e.data_section(); TORRENT_ASSERT(std::memcmp(b, section.first, section.second) == 0); TORRENT_ASSERT(section.second == sizeof(b) - 1); diff --git a/test/test_hasher.cpp b/test/test_hasher.cpp index b2453a903..55cc4259f 100644 --- a/test/test_hasher.cpp +++ b/test/test_hasher.cpp @@ -69,7 +69,8 @@ int test_main() for (int i = 0; i < repeat_count[test]; ++i) h.update(test_array[test], std::strlen(test_array[test])); - sha1_hash result = boost::lexical_cast(result_array[test]); + sha1_hash result; + from_hex(result_array[test], 40, (char*)&result[0]); TEST_CHECK(result == h.final()); } diff --git a/test/test_primitives.cpp b/test/test_primitives.cpp index 8d75df0da..2607019f7 100644 --- a/test/test_primitives.cpp +++ b/test/test_primitives.cpp @@ -54,6 +54,13 @@ using namespace libtorrent; using namespace boost::tuples; using boost::bind; +sha1_hash to_hash(char const* s) +{ + sha1_hash ret; + from_hex(s, 40, (char*)&ret[0]); + return ret; +} + tuple feed_bytes(http_parser& parser, char const* str) { tuple ret(0, 0, false); @@ -347,6 +354,15 @@ int test_main() { using namespace libtorrent; + // test to/from hex conversion + + char const* str = "0123456789012345678901234567890123456789"; + char bin[20]; + TEST_CHECK(from_hex(str, 40, bin)); + char hex[41]; + to_hex(bin, 20, hex); + TEST_CHECK(strcmp(hex, str) == 0); + // test itoa TEST_CHECK(to_string(345).elems == std::string("345")); @@ -500,7 +516,7 @@ int test_main() TEST_CHECK(atoi(parser.header("port").c_str()) == 6881); TEST_CHECK(parser.header("infohash") == "12345678901234567890"); - TEST_CHECK(!parser.finished()); + TEST_CHECK(parser.finished()); parser.reset(); TEST_CHECK(!parser.finished()); @@ -653,12 +669,12 @@ int test_main() // test kademlia routing table dht_settings s; - node_id id = boost::lexical_cast("6123456789abcdef01232456789abcdef0123456"); + node_id id = to_hash("6123456789abcdef01232456789abcdef0123456"); dht::routing_table table(id, 10, s); table.node_seen(id, udp::endpoint(address_v4::any(), rand())); node_id tmp; - node_id diff = boost::lexical_cast("00001f7459456a9453f8719b09547c11d5f34064"); + node_id diff = to_hash("00001f7459456a9453f8719b09547c11d5f34064"); std::vector nodes; for (int i = 0; i < 10000; ++i) { @@ -736,8 +752,8 @@ int test_main() TEST_CHECK(!(h1 < h2)); TEST_CHECK(h1.is_all_zeros()); - h1 = boost::lexical_cast("0123456789012345678901234567890123456789"); - h2 = boost::lexical_cast("0113456789012345678901234567890123456789"); + h1 = to_hash("0123456789012345678901234567890123456789"); + h2 = to_hash("0113456789012345678901234567890123456789"); TEST_CHECK(h2 < h1); TEST_CHECK(h2 == h2); @@ -745,34 +761,36 @@ int test_main() h2.clear(); TEST_CHECK(h2.is_all_zeros()); - h2 = boost::lexical_cast("ffffffffff0000000000ffffffffff0000000000"); - h1 = boost::lexical_cast("fffff00000fffff00000fffff00000fffff00000"); + h2 = to_hash("ffffffffff0000000000ffffffffff0000000000"); + h1 = to_hash("fffff00000fffff00000fffff00000fffff00000"); h1 &= h2; - TEST_CHECK(h1 == boost::lexical_cast("fffff000000000000000fffff000000000000000")); + TEST_CHECK(h1 == to_hash("fffff000000000000000fffff000000000000000")); - h2 = boost::lexical_cast("ffffffffff0000000000ffffffffff0000000000"); - h1 = boost::lexical_cast("fffff00000fffff00000fffff00000fffff00000"); + h2 = to_hash("ffffffffff0000000000ffffffffff0000000000"); + h1 = to_hash("fffff00000fffff00000fffff00000fffff00000"); h1 |= h2; - TEST_CHECK(h1 == boost::lexical_cast("fffffffffffffff00000fffffffffffffff00000")); + TEST_CHECK(h1 == to_hash("fffffffffffffff00000fffffffffffffff00000")); - h2 = boost::lexical_cast("0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f"); + h2 = to_hash("0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f"); h1 ^= h2; +#if TORRENT_USE_IOSTREAM std::cerr << h1 << std::endl; - TEST_CHECK(h1 == boost::lexical_cast("f0f0f0f0f0f0f0ff0f0ff0f0f0f0f0f0f0ff0f0f")); +#endif + TEST_CHECK(h1 == to_hash("f0f0f0f0f0f0f0ff0f0ff0f0f0f0f0f0f0ff0f0f")); TEST_CHECK(h1 != h2); h2 = sha1_hash(" "); - TEST_CHECK(h2 == boost::lexical_cast("2020202020202020202020202020202020202020")); + TEST_CHECK(h2 == to_hash("2020202020202020202020202020202020202020")); // CIDR distance test - h1 = boost::lexical_cast("0123456789abcdef01232456789abcdef0123456"); - h2 = boost::lexical_cast("0123456789abcdef01232456789abcdef0123456"); + h1 = to_hash("0123456789abcdef01232456789abcdef0123456"); + h2 = to_hash("0123456789abcdef01232456789abcdef0123456"); TEST_CHECK(common_bits(&h1[0], &h2[0], 20) == 160); - h2 = boost::lexical_cast("0120456789abcdef01232456789abcdef0123456"); + h2 = to_hash("0120456789abcdef01232456789abcdef0123456"); TEST_CHECK(common_bits(&h1[0], &h2[0], 20) == 14); - h2 = boost::lexical_cast("012f456789abcdef01232456789abcdef0123456"); + h2 = to_hash("012f456789abcdef01232456789abcdef0123456"); TEST_CHECK(common_bits(&h1[0], &h2[0], 20) == 12); - h2 = boost::lexical_cast("0123456789abcdef11232456789abcdef0123456"); + h2 = to_hash("0123456789abcdef11232456789abcdef0123456"); TEST_CHECK(common_bits(&h1[0], &h2[0], 20) == 16 * 4 + 3); diff --git a/test/test_storage.cpp b/test/test_storage.cpp index d1ee095df..ecfe88ea3 100644 --- a/test/test_storage.cpp +++ b/test/test_storage.cpp @@ -512,7 +512,9 @@ void test_fastresume(path const& test_path) ses.remove_torrent(h, session::delete_files); } TEST_CHECK(!exists(test_path / "tmp1/temporary")); +#if TORRENT_USE_IOSTREAM resume.print(std::cout); +#endif // make sure the fast resume check fails! since we removed the file { @@ -583,7 +585,9 @@ void test_rename_file_in_fastresume(path const& test_path) TEST_CHECK(!exists(test_path / "tmp2/temporary")); TEST_CHECK(exists(test_path / "tmp2/testing_renamed_files")); TEST_CHECK(resume.dict().find("mapped_files") != resume.dict().end()); +#if TORRENT_USE_IOSTREAM resume.print(std::cout); +#endif // make sure the fast resume check succeeds, even though we renamed the file { @@ -604,7 +608,9 @@ void test_rename_file_in_fastresume(path const& test_path) ses.remove_torrent(h); } TEST_CHECK(resume.dict().find("mapped_files") != resume.dict().end()); +#if TORRENT_USE_IOSTREAM resume.print(std::cout); +#endif remove_all(test_path / "tmp2"); }