From 22c047f8a04c65bbc06312ecfeff702acf27263d Mon Sep 17 00:00:00 2001 From: arvidn Date: Tue, 22 Aug 2017 22:20:18 +0200 Subject: [PATCH] factor out and simplify get_syncoffset --- include/libtorrent/bt_peer_connection.hpp | 6 ----- include/libtorrent/string_util.hpp | 5 ++++ src/bt_peer_connection.cpp | 30 +++-------------------- src/string_util.cpp | 15 ++++++++++++ 4 files changed, 23 insertions(+), 33 deletions(-) diff --git a/include/libtorrent/bt_peer_connection.hpp b/include/libtorrent/bt_peer_connection.hpp index cb98557ff..0aa61ee75 100644 --- a/include/libtorrent/bt_peer_connection.hpp +++ b/include/libtorrent/bt_peer_connection.hpp @@ -321,12 +321,6 @@ namespace libtorrent { void write_pe_vc_cryptofield(span write_buf , int crypto_field, std::size_t pad_size); - // Returns offset at which bytestream (src, src + src_size) - // matches bytestream(target, target + target_size). - // If no sync found, return -1 - int get_syncoffset(char const* src, int src_size - , char const* target, int target_size) const; - // helper to cut down on boilerplate void rc4_decrypt(span buf); #endif diff --git a/include/libtorrent/string_util.hpp b/include/libtorrent/string_util.hpp index 7f44eb81d..930cdaa34 100644 --- a/include/libtorrent/string_util.hpp +++ b/include/libtorrent/string_util.hpp @@ -35,6 +35,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/config.hpp" #include "libtorrent/string_view.hpp" +#include "libtorrent/span.hpp" #include #include @@ -71,6 +72,10 @@ namespace libtorrent { TORRENT_EXTRA_EXPORT bool string_ends_with(string_view s1, string_view s2); + // Returns offset at which src matches target. + // If no sync found, return -1 + TORRENT_EXTRA_EXPORT int search(span src, span target); + struct listen_interface_t { std::string device; diff --git a/src/bt_peer_connection.cpp b/src/bt_peer_connection.cpp index f38d0176d..77b3dba4f 100644 --- a/src/bt_peer_connection.cpp +++ b/src/bt_peer_connection.cpp @@ -68,6 +68,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/socket_type.hpp" #include "libtorrent/performance_counters.hpp" // for counters #include "libtorrent/alert_manager.hpp" // for alert_manager +#include "libtorrent/string_util.hpp" // for search #if !defined(TORRENT_DISABLE_ENCRYPTION) && !defined(TORRENT_DISABLE_EXTENSIONS) #include "libtorrent/pe_crypto.hpp" @@ -668,29 +669,6 @@ namespace { aux::write_uint16(handshake_len, write_buf); // len(IA) } - // TODO: 3 use span instead of (pointer,len) pairs - int bt_peer_connection::get_syncoffset(char const* src, int const src_size - , char const* target, int const target_size) const - { - TORRENT_ASSERT(target_size >= src_size); - TORRENT_ASSERT(src_size > 0); - TORRENT_ASSERT(src); - TORRENT_ASSERT(target); - - int const traverse_limit = target_size - src_size; - - // TODO: this could be optimized using knuth morris pratt - for (int i = 0; i < traverse_limit; ++i) - { - char const* target_ptr = target + i; - if (std::equal(src, src+src_size, target_ptr)) - return i; - } - - // no complete sync - return -1; - } - void bt_peer_connection::rc4_decrypt(span buf) { m_rc4->decrypt(buf); @@ -2579,8 +2557,7 @@ namespace { #endif } - int const syncoffset = get_syncoffset(m_sync_hash->data(), 20 - , recv_buffer.begin(), int(recv_buffer.size())); + int const syncoffset = search(*m_sync_hash, recv_buffer); // No sync if (syncoffset == -1) @@ -2721,8 +2698,7 @@ namespace { } TORRENT_ASSERT(m_sync_vc.get()); - int const syncoffset = get_syncoffset(m_sync_vc.get(), 8 - , recv_buffer.begin(), int(recv_buffer.size())); + int const syncoffset = search({m_sync_vc.get(), 8}, recv_buffer); // No sync if (syncoffset == -1) diff --git a/src/string_util.cpp b/src/string_util.cpp index 41edc027b..8f46d50e4 100644 --- a/src/string_util.cpp +++ b/src/string_util.cpp @@ -40,6 +40,7 @@ POSSIBILITY OF SUCH DAMAGE. #include // for malloc #include // for memmov/strcpy/strlen +#include // for search namespace libtorrent { @@ -149,6 +150,20 @@ namespace libtorrent { return s1.size() >= s2.size() && std::equal(s2.rbegin(), s2.rend(), s1.rbegin()); } + int search(span src, span target) + { + TORRENT_ASSERT(!src.empty()); + TORRENT_ASSERT(!target.empty()); + TORRENT_ASSERT(target.size() >= src.size()); + TORRENT_ASSERT(target.size() < std::size_t(std::numeric_limits::max())); + + auto it = std::search(target.begin(), target.end(), src.begin(), src.end()); + + // no complete sync + if (it == target.end()) return -1; + return static_cast(it - target.begin()); + } + char* allocate_string_copy(char const* str) { if (str == nullptr) return nullptr;