From f8272b90ae0f95687e7319ce5fdbe03bcf822bc3 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Mon, 29 Sep 2014 07:06:18 +0000 Subject: [PATCH] merged changes from RC_1_0 --- ChangeLog | 1 + include/libtorrent/lazy_entry.hpp | 4 ++++ src/http_tracker_connection.cpp | 4 +--- src/lazy_bdecode.cpp | 20 ++++++++++++++++++++ test/test_bencoding.cpp | 17 +++++++++++++++++ test/test_tracker.cpp | 6 +++--- 6 files changed, 46 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index 5788154a2..23db60c3d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -114,6 +114,7 @@ 0.16.18 release + * fix bug in HTTP scrape response parsing * enable TCP keepalive for socks5 connection for UDP associate * fix python3 support * fix bug in lt_donthave extension diff --git a/include/libtorrent/lazy_entry.hpp b/include/libtorrent/lazy_entry.hpp index ef46df2bd..00b5fb48e 100644 --- a/include/libtorrent/lazy_entry.hpp +++ b/include/libtorrent/lazy_entry.hpp @@ -226,6 +226,9 @@ namespace libtorrent lazy_entry* dict_find(char const* name); lazy_entry const* dict_find(char const* name) const { return const_cast(this)->dict_find(name); } + lazy_entry* dict_find(std::string const& name); + lazy_entry const* dict_find(std::string const& name) const + { return const_cast(this)->dict_find(name); } lazy_entry const* dict_find_string(char const* name) const; // if this is a dictionary, look for a key ``name`` whose value @@ -248,6 +251,7 @@ namespace libtorrent // if no key with the corresponding value of the right type is // found, NULL is returned. lazy_entry const* dict_find_dict(char const* name) const; + lazy_entry const* dict_find_dict(std::string const& name) const; lazy_entry const* dict_find_list(char const* name) const; // if this is a dictionary, return the key value pair at diff --git a/src/http_tracker_connection.cpp b/src/http_tracker_connection.cpp index 535136922..5adab1e53 100644 --- a/src/http_tracker_connection.cpp +++ b/src/http_tracker_connection.cpp @@ -476,10 +476,8 @@ namespace libtorrent return resp; } - // TODO: 4 this is a bug. if the info-hash contains a 0, this will - // fail! lazy_entry const* scrape_data = files->dict_find_dict( - scrape_ih.to_string().c_str()); + scrape_ih.to_string()); if (scrape_data == 0) { diff --git a/src/lazy_bdecode.cpp b/src/lazy_bdecode.cpp index bd59dd72d..12956ddde 100644 --- a/src/lazy_bdecode.cpp +++ b/src/lazy_bdecode.cpp @@ -395,6 +395,13 @@ namespace libtorrent return e; } + lazy_entry const* lazy_entry::dict_find_dict(std::string const& name) const + { + lazy_entry const* e = dict_find(name); + if (e == 0 || e->type() != lazy_entry::dict_t) return 0; + return e; + } + lazy_entry const* lazy_entry::dict_find_list(char const* name) const { lazy_entry const* e = dict_find(name); @@ -414,6 +421,19 @@ namespace libtorrent return 0; } + lazy_entry* lazy_entry::dict_find(std::string const& name) + { + TORRENT_ASSERT(m_type == dict_t); + for (int i = 0; i < int(m_size); ++i) + { + lazy_dict_entry& e = m_data.dict[i+1]; + if (name.size() != e.val.m_begin - e.name) continue; + if (std::equal(name.begin(), name.end(), e.name)) + return &e.val; + } + return 0; + } + lazy_entry* lazy_entry::list_append() { TORRENT_ASSERT(m_type == list_t); diff --git a/test/test_bencoding.cpp b/test/test_bencoding.cpp index 268456b12..7fec7d736 100644 --- a/test/test_bencoding.cpp +++ b/test/test_bencoding.cpp @@ -178,6 +178,23 @@ int test_main() TEST_CHECK(e.dict_find_string_value("X") == "0123456789"); } + // dictionary key with \0 + { + char b[] = "d3:a\0bi1ee"; + lazy_entry e; + error_code ec; + int ret = lazy_bdecode(b, b + sizeof(b)-1, e, ec); + TEST_CHECK(ret == 0); +#if TORRENT_USE_IOSTREAM + std::cout << e << std::endl; +#endif + TEST_CHECK(e.dict_size() == 1); + lazy_entre* d = e.dict_find(std::string("a\0b")); + TEST_CHECK(d); + TEST_EQUAL(d->type, lazy_entry::integer_t); + TEST_EQUAL(d->integer(), 1); + } + // test strings with negative length-prefix { char b[] = "-10:foobar"; diff --git a/test/test_tracker.cpp b/test/test_tracker.cpp index 6d0df0a3e..d0fd9767d 100644 --- a/test/test_tracker.cpp +++ b/test/test_tracker.cpp @@ -149,9 +149,9 @@ void test_parse_scrape_response_with_zero() , ec, true, sha1_hash("aaa\0aaaaaaaaaaaaaaaa")); TEST_EQUAL(ec, error_code()); - TEST_EQUAL(resp.complete, 1); - TEST_EQUAL(resp.incomplete, 2); - TEST_EQUAL(resp.downloaded, 3); + TEST_EQUAL(resp.complete, 4); + TEST_EQUAL(resp.incomplete, 5); + TEST_EQUAL(resp.downloaded, 6); TEST_EQUAL(resp.downloaders, -1); }