factor out and simplify get_syncoffset

This commit is contained in:
arvidn 2017-08-22 22:20:18 +02:00 committed by Arvid Norberg
parent f311bf354e
commit 22c047f8a0
4 changed files with 23 additions and 33 deletions

View File

@ -321,12 +321,6 @@ namespace libtorrent {
void write_pe_vc_cryptofield(span<char> 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<char> buf);
#endif

View File

@ -35,6 +35,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/config.hpp"
#include "libtorrent/string_view.hpp"
#include "libtorrent/span.hpp"
#include <vector>
#include <string>
@ -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<char const> src, span<char const> target);
struct listen_interface_t
{
std::string device;

View File

@ -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<char> 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)

View File

@ -40,6 +40,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include <cstdlib> // for malloc
#include <cstring> // for memmov/strcpy/strlen
#include <algorithm> // 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<char const> src, span<char const> 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<int>::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<int>(it - target.begin());
}
char* allocate_string_copy(char const* str)
{
if (str == nullptr) return nullptr;