From f4127ae12b6a5845d74364ce9499cdf5f60a8a38 Mon Sep 17 00:00:00 2001 From: Alden Torres Date: Tue, 16 Aug 2016 10:21:17 -0400 Subject: [PATCH] refactor in crypto_plugin::decrypt to use tuple --- include/libtorrent/extensions.hpp | 6 ++++-- include/libtorrent/pe_crypto.hpp | 5 +---- src/bt_peer_connection.cpp | 5 +---- src/pe_crypto.cpp | 24 +++++++----------------- test/test_pe_crypto.cpp | 8 ++++---- 5 files changed, 17 insertions(+), 31 deletions(-) diff --git a/include/libtorrent/extensions.hpp b/include/libtorrent/extensions.hpp index 86755eca6..4d1baec2d 100644 --- a/include/libtorrent/extensions.hpp +++ b/include/libtorrent/extensions.hpp @@ -498,6 +498,9 @@ namespace libtorrent encrypt(span> /*send_vec*/) = 0; // decrypt the provided buffers. + // returns is a tuple representing the values + // (consume, produce, packet_size) + // // consume is set to the number of bytes which should be trimmed from the // head of the buffers, default is 0 // @@ -506,8 +509,7 @@ namespace libtorrent // // packet_size is set to the minimum number of bytes which must be read to // advance the next step of decryption. default is 0 - virtual void decrypt(span> /*receive_vec*/ - , int& /* consume */, int& /*produce*/, int& /*packet_size*/) = 0; + virtual std::tuple decrypt(span> /*receive_vec*/) = 0; }; } diff --git a/include/libtorrent/pe_crypto.hpp b/include/libtorrent/pe_crypto.hpp index b20d3306c..32be40624 100644 --- a/include/libtorrent/pe_crypto.hpp +++ b/include/libtorrent/pe_crypto.hpp @@ -145,10 +145,7 @@ namespace libtorrent std::tuple>> encrypt(span> buf) override; - void decrypt(span> buf - , int& consume - , int& produce - , int& packet_size) override; + std::tuple decrypt(span> buf) override; private: rc4 m_rc4_incoming; diff --git a/src/bt_peer_connection.cpp b/src/bt_peer_connection.cpp index d4dfe1dab..2e185de53 100644 --- a/src/bt_peer_connection.cpp +++ b/src/bt_peer_connection.cpp @@ -727,10 +727,7 @@ namespace libtorrent void bt_peer_connection::rc4_decrypt(span buf) { - int consume = 0; - int produce = int(buf.size()); - int packet_size = 0; - m_rc4->decrypt(buf, consume, produce, packet_size); + m_rc4->decrypt(buf); } namespace { diff --git a/src/pe_crypto.cpp b/src/pe_crypto.cpp index 8bb16e87b..5090d5b6e 100644 --- a/src/pe_crypto.cpp +++ b/src/pe_crypto.cpp @@ -219,9 +219,9 @@ namespace libtorrent if (recv_buffer.crypto_packet_finished()) { span wr_buf = recv_buffer.mutable_buffer(bytes_transferred); + int produce = 0; int packet_size = 0; - int produce = int(bytes_transferred); - m_dec_handler->decrypt(wr_buf, consume, produce, packet_size); + std::tie(consume, produce, packet_size) = m_dec_handler->decrypt(wr_buf); TORRENT_ASSERT(packet_size || produce); TORRENT_ASSERT(packet_size >= 0); bytes_transferred = produce; @@ -265,7 +265,7 @@ namespace libtorrent int consume = 0; int produce = 0; std::vector> wr_buf; - crypto->decrypt(wr_buf, consume, produce, packet_size); + std::tie(consume, produce, packet_size) = crypto->decrypt(wr_buf); TORRENT_ASSERT(wr_buf.empty()); TORRENT_ASSERT(consume == 0); TORRENT_ASSERT(produce == 0); @@ -289,12 +289,9 @@ namespace libtorrent rc4_init(reinterpret_cast(key.data()) , key.size(), &m_rc4_incoming); // Discard first 1024 bytes - int consume = 0; - int produce = 0; - int packet_size = 0; char buf[1024]; span vec(buf, sizeof(buf)); - decrypt(vec, consume, produce, packet_size); + decrypt(vec); } void rc4_handler::set_outgoing_key(span key) @@ -330,16 +327,9 @@ namespace libtorrent return std::make_tuple(bytes_processed, empty); } - void rc4_handler::decrypt(span> bufs - , int& consume - , int& produce - , int& packet_size) + std::tuple rc4_handler::decrypt(span> bufs) { - // these are out-parameters that are not set - TORRENT_UNUSED(consume); - TORRENT_UNUSED(packet_size); - - if (!m_decrypt) return; + if (!m_decrypt) std::make_tuple(0, 0, 0); int bytes_processed = 0; for (auto& buf : bufs) @@ -353,7 +343,7 @@ namespace libtorrent bytes_processed += len; rc4_encrypt(pos, len, &m_rc4_incoming); } - produce = bytes_processed; + return std::make_tuple(0, bytes_processed, 0); } // All this code is based on libTomCrypt (http://www.libtomcrypt.com/) diff --git a/test/test_pe_crypto.cpp b/test/test_pe_crypto.cpp index 10abebe70..def3de0dd 100644 --- a/test/test_pe_crypto.cpp +++ b/test/test_pe_crypto.cpp @@ -73,10 +73,10 @@ void test_enc_handler(libtorrent::crypto_plugin& a, libtorrent::crypto_plugin& b { int consume = 0; - int produce = buf_len; + int produce = 0; int packet_size = 0; lt::span iovec(&buf[0], buf_len); - b.decrypt(iovec, consume, produce, packet_size); + std::tie(consume, produce, packet_size) = b.decrypt(iovec); TEST_CHECK(buf == cmp_buf); TEST_EQUAL(consume, 0); TEST_EQUAL(produce, buf_len); @@ -93,10 +93,10 @@ void test_enc_handler(libtorrent::crypto_plugin& a, libtorrent::crypto_plugin& b TEST_EQUAL(next_barrier, buf_len); int consume = 0; - int produce = buf_len; + int produce = 0; int packet_size = 0; lt::span iovec2(&buf[0], buf_len); - a.decrypt(iovec2, consume, produce, packet_size); + std::tie(consume, produce, packet_size) = a.decrypt(iovec2); TEST_CHECK(buf == cmp_buf); TEST_EQUAL(consume, 0); TEST_EQUAL(produce, buf_len);