From 4fd230dbe5388904291f717f372fb202d9daf846 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Mon, 20 Apr 2015 00:01:27 +0000 Subject: [PATCH] fix build and some warnings --- include/libtorrent/bencode.hpp | 4 --- include/libtorrent/config.hpp | 2 ++ include/libtorrent/entry.hpp | 6 ++++ src/bt_peer_connection.cpp | 20 ++++++++---- src/crc32c.cpp | 2 +- src/disk_buffer_pool.cpp | 4 +++ src/entry.cpp | 14 ++++++-- src/escape_string.cpp | 2 ++ src/file_storage.cpp | 23 ++++++------- src/gzip.cpp | 3 ++ src/lazy_bdecode.cpp | 17 ++++++---- src/peer_connection.cpp | 9 +++++ src/receive_buffer.cpp | 10 +++--- src/session_impl.cpp | 60 ++++++++++++++++++++-------------- src/socket_type.cpp | 18 ++++++++-- src/time.cpp | 1 + src/torrent_info.cpp | 57 ++++++++++++++++++++------------ src/torrent_peer.cpp | 15 +++++---- 18 files changed, 176 insertions(+), 91 deletions(-) diff --git a/include/libtorrent/bencode.hpp b/include/libtorrent/bencode.hpp index e04b1ea1b..6264e2fef 100644 --- a/include/libtorrent/bencode.hpp +++ b/include/libtorrent/bencode.hpp @@ -103,10 +103,6 @@ namespace libtorrent namespace detail { - // this is used in the template, so it must be available to the client - TORRENT_EXPORT char const* integer_to_str(char* buf, int size - , entry::integer_type val); - template int write_integer(OutIt& out, entry::integer_type val) { diff --git a/include/libtorrent/config.hpp b/include/libtorrent/config.hpp index 8e82790d2..b2142b3c7 100644 --- a/include/libtorrent/config.hpp +++ b/include/libtorrent/config.hpp @@ -363,6 +363,8 @@ POSSIBILITY OF SUCH DAMAGE. #endif +#define TORRENT_UNUSED(x) (void)(x) + #if defined TORRENT_WINDOWS && !defined TORRENT_MINGW #include diff --git a/include/libtorrent/entry.hpp b/include/libtorrent/entry.hpp index b1f899c93..d1f4392dc 100644 --- a/include/libtorrent/entry.hpp +++ b/include/libtorrent/entry.hpp @@ -296,6 +296,12 @@ namespace libtorrent mutable boost::uint8_t m_type_queried:1; }; + namespace detail + { + TORRENT_EXPORT char const* integer_to_str(char* buf, int size + , entry::integer_type val); + } + #if TORRENT_USE_IOSTREAM // prints the bencoded structure to the ostream as a JSON-style structure. inline std::ostream& operator<<(std::ostream& os, const entry& e) diff --git a/src/bt_peer_connection.cpp b/src/bt_peer_connection.cpp index d793ab6a9..d720fa7a0 100644 --- a/src/bt_peer_connection.cpp +++ b/src/bt_peer_connection.cpp @@ -392,8 +392,10 @@ namespace libtorrent stats_counters().inc_stats_counter(counters::num_outgoing_suggest); } - char random_byte() - { return random() & 0xff; } + namespace { + char random_byte() + { return random() & 0xff; } + } void bt_peer_connection::get_specific_peer_info(peer_info& p) const { @@ -715,10 +717,12 @@ namespace libtorrent } #endif // #if !defined(TORRENT_DISABLE_ENCRYPTION) && !defined(TORRENT_DISABLE_EXTENSIONS) - void regular_c_free(char* buf, void* /* userdata */ - , block_cache_reference /* ref */) - { - ::free(buf); + namespace { + void regular_c_free(char* buf, void* /* userdata */ + , block_cache_reference /* ref */) + { + ::free(buf); + } } void bt_peer_connection::append_const_send_buffer(char const* buffer, int size @@ -2426,6 +2430,8 @@ namespace libtorrent #endif } + namespace { + void buffer_reclaim_block(char* /* buffer */, void* userdata , block_cache_reference ref) { @@ -2440,6 +2446,8 @@ namespace libtorrent buf->free_disk_buffer(buffer); } + } // anonymous namespace + void bt_peer_connection::write_piece(peer_request const& r, disk_buffer_holder& buffer) { INVARIANT_CHECK; diff --git a/src/crc32c.cpp b/src/crc32c.cpp index e1ba2e1e7..a9233d146 100644 --- a/src/crc32c.cpp +++ b/src/crc32c.cpp @@ -54,7 +54,7 @@ namespace libtorrent #endif } - static bool sse42_support = supports_sse42(); + bool sse42_support = supports_sse42(); } // anonymous namespace diff --git a/src/disk_buffer_pool.cpp b/src/disk_buffer_pool.cpp index 8415e1b12..babfb67ca 100644 --- a/src/disk_buffer_pool.cpp +++ b/src/disk_buffer_pool.cpp @@ -436,6 +436,8 @@ namespace libtorrent check_buffer_level(l); } + namespace { + boost::uint64_t physical_ram() { boost::uint64_t ret = 0; @@ -483,6 +485,8 @@ namespace libtorrent return ret; } + } // anonymous namespace + void disk_buffer_pool::set_settings(aux::session_settings const& sett , error_code& ec) { diff --git a/src/entry.cpp b/src/entry.cpp index 70c2239d3..69130fc58 100644 --- a/src/entry.cpp +++ b/src/entry.cpp @@ -30,17 +30,24 @@ POSSIBILITY OF SUCH DAMAGE. */ -#include +#include "libtorrent/config.hpp" + +#include "aux_/disable_warnings_push.hpp" + #if TORRENT_USE_IOSTREAM #include #endif +#include #include + +#include "aux_/disable_warnings_pop.hpp" + #include "libtorrent/entry.hpp" -#include "libtorrent/config.hpp" #ifndef TORRENT_NO_DEPRECATE #include "libtorrent/lazy_entry.hpp" #endif #include "libtorrent/bdecode.hpp" +#include "libtorrent/entry.hpp" #include "libtorrent/hex.hpp" #if defined(_MSC_VER) @@ -61,7 +68,8 @@ namespace libtorrent { namespace detail { - TORRENT_EXPORT char const* integer_to_str(char* buf, int size, entry::integer_type val) + TORRENT_EXPORT char const* integer_to_str(char* buf, int size + , entry::integer_type val) { int sign = 0; if (val < 0) diff --git a/src/escape_string.cpp b/src/escape_string.cpp index 88265daf3..32eea1f4f 100644 --- a/src/escape_string.cpp +++ b/src/escape_string.cpp @@ -475,6 +475,8 @@ namespace libtorrent *out = '\0'; } + // TODO: 3 move the hex coding to its own .cpp file corresponding + // to the functions exported by hex.hpp TORRENT_EXTRA_EXPORT int hex_to_int(char in) { if (in >= '0' && in <= '9') return int(in) - '0'; diff --git a/src/file_storage.cpp b/src/file_storage.cpp index a2839b120..5c2714910 100644 --- a/src/file_storage.cpp +++ b/src/file_storage.cpp @@ -123,6 +123,18 @@ namespace libtorrent if (str2.size() != len) return false; return memcmp(str2.c_str(), str, len) == 0; } + + bool compare_file_entry_size(internal_file_entry const& fe1 + , internal_file_entry const& fe2) + { + return fe1.size < fe2.size; + } + + bool compare_file_offset(internal_file_entry const& lhs + , internal_file_entry const& rhs) + { + return lhs.offset < rhs.offset; + } } // path is not supposed to include the name of the torrent itself. @@ -352,14 +364,6 @@ namespace libtorrent update_path_index(m_files[index], new_filename); } - namespace - { - bool compare_file_offset(internal_file_entry const& lhs, internal_file_entry const& rhs) - { - return lhs.offset < rhs.offset; - } - } - #ifndef TORRENT_NO_DEPRECATE file_storage::iterator file_storage::file_at_offset_deprecated(boost::int64_t offset) const { @@ -889,9 +893,6 @@ namespace libtorrent { return at(i - m_files.begin()); } #endif // TORRENT_NO_DEPRECATE - bool compare_file_entry_size(internal_file_entry const& fe1, internal_file_entry const& fe2) - { return fe1.size < fe2.size; } - void file_storage::reorder_file(int index, int dst) { TORRENT_ASSERT(index < int(m_files.size())); diff --git a/src/gzip.cpp b/src/gzip.cpp index 9af00606e..ec0eb5b1b 100644 --- a/src/gzip.cpp +++ b/src/gzip.cpp @@ -109,6 +109,8 @@ namespace libtorrent } } + namespace + { // returns -1 if gzip header is invalid or the header size in bytes int gzip_header(const char* buf, int size) { @@ -182,6 +184,7 @@ namespace libtorrent return total_size - size; } + } // anonymous namespace TORRENT_EXTRA_EXPORT void inflate_gzip( char const* in diff --git a/src/lazy_bdecode.cpp b/src/lazy_bdecode.cpp index 9961966bf..960ea248e 100644 --- a/src/lazy_bdecode.cpp +++ b/src/lazy_bdecode.cpp @@ -67,11 +67,10 @@ namespace libtorrent if (error_pos) *error_pos = start - orig_start; return -1; } - } #define TORRENT_FAIL_BDECODE(code) do { ec = make_error_code(code); return fail(error_pos, stack, start, orig_start); } while (false) - namespace { bool numeric(char c) { return c >= '0' && c <= '9'; } } + bool numeric(char c) { return c >= '0' && c <= '9'; } char const* find_char(char const* start, char const* end, char delimiter) { @@ -79,6 +78,8 @@ namespace libtorrent return start; } + } // anonymous namespace + #ifndef TORRENT_NO_DEPRECATE int lazy_bdecode(char const* start, char const* end , lazy_entry& ret, int depth_limit, int item_limit) @@ -170,10 +171,10 @@ namespace libtorrent { case 'd': top->construct_dict(start - 1); - continue; + break; case 'l': top->construct_list(start - 1); - continue; + break; case 'i': { char const* int_start = start; @@ -183,7 +184,7 @@ namespace libtorrent TORRENT_ASSERT(*start == 'e'); ++start; stack.pop_back(); - continue; + break; } default: { @@ -204,10 +205,9 @@ namespace libtorrent top->construct_string(start, int(len)); stack.pop_back(); start += len; - continue; + break; } } - return 0; } return 0; } @@ -477,6 +477,8 @@ namespace libtorrent return return_t(m_begin, m_len); } + namespace { + int line_longer_than(lazy_entry const& e, int limit) { int line_len = 0; @@ -580,6 +582,7 @@ namespace libtorrent } ret += "'"; } + } // anonymous namespace std::string print_entry(lazy_entry const& e, bool single_line, int indent) { diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index 54bf6d316..711942830 100644 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -97,11 +97,15 @@ namespace libtorrent min_request_queue = 2, }; + namespace { + bool pending_block_in_buffer(pending_block const& pb) { return pb.send_buffer_offset != pending_block::not_in_buffer; } + } + #if defined TORRENT_REQUEST_LOGGING void write_request_log(FILE* f, sha1_hash const& ih , peer_connection* p, peer_request const& r) @@ -1398,6 +1402,8 @@ namespace libtorrent } } + namespace { + bool match_request(peer_request const& r, piece_block const& b, int block_size) { if (int(b.piece_index) != r.piece) return false; @@ -1405,6 +1411,7 @@ namespace libtorrent if (r.start % block_size != 0) return false; return true; } + } // ----------------------------- // -------- REJECT PIECE ------- @@ -5673,11 +5680,13 @@ namespace libtorrent , userdata, ref); } + namespace { void session_free_buffer(char* buffer, void* userdata, block_cache_reference) { aux::session_interface* ses = (aux::session_interface*)userdata; ses->free_buffer(buffer); } + } void peer_connection::send_buffer(char const* buf, int size, int flags) { diff --git a/src/receive_buffer.cpp b/src/receive_buffer.cpp index 4d286d2e0..fd1df162b 100644 --- a/src/receive_buffer.cpp +++ b/src/receive_buffer.cpp @@ -34,10 +34,12 @@ POSSIBILITY OF SUCH DAMAGE. namespace libtorrent { -int round_up8(int v) -{ - return ((v & 7) == 0) ? v : v + (8 - (v & 7)); -} + namespace { + int round_up8(int v) + { + return ((v & 7) == 0) ? v : v + (8 - (v & 7)); + } + } int receive_buffer::max_receive() { diff --git a/src/session_impl.cpp b/src/session_impl.cpp index b97b52927..78cd545ef 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -3219,6 +3219,7 @@ retry: // m_peer_pool.release_memory(); } + namespace { // returns the index of the first set bit. int log2(boost::uint32_t v) { @@ -3238,6 +3239,8 @@ retry: return MultiplyDeBruijnBitPosition[boost::uint32_t(v * 0x07C4ACDDU) >> 27]; } + } // anonymous namespace + void session_impl::received_buffer(int s) { int index = (std::min)(log2(s >> 3), 17); @@ -5376,10 +5379,13 @@ retry: void session_impl::start_dht() { start_dht(m_dht_state); } - void on_bootstrap(alert_manager& alerts) - { - if (alerts.should_post()) - alerts.emplace_alert(); + namespace { + + void on_bootstrap(alert_manager& alerts) + { + if (alerts.should_post()) + alerts.emplace_alert(); + } } void session_impl::start_dht(entry const& startup_state) @@ -5496,11 +5502,31 @@ retry: , this, _1), salt); } - void on_dht_put(alert_manager& alerts, sha1_hash target) - { - if (alerts.should_post()) - alerts.emplace_alert(target); - } + namespace { + + void on_dht_put(alert_manager& alerts, sha1_hash target) + { + if (alerts.should_post()) + alerts.emplace_alert(target); + } + + void put_mutable_callback(alert_manager& alerts, dht::item& i + , boost::function& + , boost::uint64_t&, std::string const&)> cb) + { + entry value = i.value(); + boost::array sig = i.sig(); + boost::array pk = i.pk(); + boost::uint64_t seq = i.seq(); + std::string salt = i.salt(); + cb(value, sig, seq, salt); + i.assign(value, salt, seq, pk.data(), sig.data()); + + if (alerts.should_post()) + alerts.emplace_alert(pk, sig, salt, seq); + } + + } // anonymous namespace void session_impl::dht_put_item(entry data, sha1_hash target) { @@ -5509,22 +5535,6 @@ retry: , target)); } - void put_mutable_callback(alert_manager& alerts, dht::item& i - , boost::function& - , boost::uint64_t&, std::string const&)> cb) - { - entry value = i.value(); - boost::array sig = i.sig(); - boost::array pk = i.pk(); - boost::uint64_t seq = i.seq(); - std::string salt = i.salt(); - cb(value, sig, seq, salt); - i.assign(value, salt, seq, pk.data(), sig.data()); - - if (alerts.should_post()) - alerts.emplace_alert(pk, sig, salt, seq); - } - void session_impl::dht_put_mutable_item(boost::array key , boost::function& , boost::uint64_t&, std::string const&)> cb diff --git a/src/socket_type.cpp b/src/socket_type.cpp index a399007ce..7880ed7c5 100644 --- a/src/socket_type.cpp +++ b/src/socket_type.cpp @@ -64,6 +64,7 @@ namespace libtorrent }; #undef CASE #else + TORRENT_UNUSED(s); return false; #endif } @@ -94,7 +95,8 @@ namespace libtorrent // for SSL connections, make sure to authenticate the hostname // of the certificate #define CASE(t) case socket_type_int_impl >::value: \ - s.get >()->set_verify_callback(asio::ssl::rfc2818_verification(hostname), ec); \ + s.get >()->set_verify_callback( \ + asio::ssl::rfc2818_verification(hostname), ec); \ ctx = SSL_get_SSL_CTX(s.get >()->native_handle()); \ break; @@ -116,11 +118,16 @@ namespace libtorrent SSL_CTX_set_tlsext_servername_arg(ctx, 0); } #endif // OPENSSL_VERSION_NUMBER - +#else + TORRENT_UNUSED(ec); + TORRENT_UNUSED(hostname); + TORRENT_UNUSED(s); #endif } - void on_close_socket(socket_type* s, boost::shared_ptr holder) + namespace { + + void on_close_socket(socket_type* s, boost::shared_ptr) { #if defined TORRENT_ASIO_DEBUGGING complete_async("on_close_socket"); @@ -129,6 +136,8 @@ namespace libtorrent s->close(ec); } + } // anonymous namespace + // the second argument is a shared pointer to an object that // will keep the socket (s) alive for the duration of the async operation void async_shutdown(socket_type& s, boost::shared_ptr holder) @@ -158,6 +167,7 @@ namespace libtorrent } #undef CASE #else + TORRENT_UNUSED(holder); s.close(e); #endif // TORRENT_USE_OPENSSL } @@ -247,6 +257,8 @@ namespace libtorrent new ((ssl_stream*)m_data) ssl_stream(m_io_service , *((boost::asio::ssl::context*)userdata)); break; +#else + TORRENT_UNUSED(userdata); #endif default: TORRENT_ASSERT(false); } diff --git a/src/time.cpp b/src/time.cpp index 8192520fb..845e83d33 100644 --- a/src/time.cpp +++ b/src/time.cpp @@ -37,6 +37,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include "libtorrent/config.hpp" #include "libtorrent/time.hpp" +#include "libtorrent/aux_/time.hpp" #ifndef _WIN32 #include diff --git a/src/torrent_info.cpp b/src/torrent_info.cpp index 825610223..5d5117248 100644 --- a/src/torrent_info.cpp +++ b/src/torrent_info.cpp @@ -80,6 +80,8 @@ POSSIBILITY OF SUCH DAMAGE. namespace libtorrent { + namespace { + bool valid_path_character(char c) { #ifdef TORRENT_WINDOWS @@ -91,6 +93,8 @@ namespace libtorrent return std::strchr(invalid_chars, c) == 0; } + } // anonymous namespace + // fixes invalid UTF-8 sequences and // replaces characters that are invalid // in paths @@ -360,6 +364,8 @@ namespace libtorrent if (path.empty()) path = "_"; } + namespace { + // 'top_level' is extracting the file for a single-file torrent. The // distinction is that the filename is found in "name" rather than // "path" @@ -572,6 +578,32 @@ namespace libtorrent return true; } + int load_file(std::string const& filename, std::vector& v + , error_code& ec, int limit = 8000000) + { + ec.clear(); + file f; + if (!f.open(filename, file::read_only, ec)) return -1; + boost::int64_t s = f.get_size(ec); + if (ec) return -1; + if (s > limit) + { + ec = errors::metadata_too_large; + return -2; + } + v.resize((unsigned int)s); + if (s == 0) return 0; + file::iovec_t b = {&v[0], size_t(s) }; + boost::int64_t read = f.readv(0, &b, 1, ec); + if (read != s) return -3; + if (ec) return -3; + return 0; + } + + } // anonymous namespace + + // TODO: 3 move the merkle functions out into its own file + // and header file int merkle_get_parent(int tree_node) { // node 0 doesn't have a parent @@ -603,27 +635,6 @@ namespace libtorrent return ret; } - int load_file(std::string const& filename, std::vector& v, error_code& ec, int limit = 8000000) - { - ec.clear(); - file f; - if (!f.open(filename, file::read_only, ec)) return -1; - boost::int64_t s = f.get_size(ec); - if (ec) return -1; - if (s > limit) - { - ec = errors::metadata_too_large; - return -2; - } - v.resize((unsigned int)s); - if (s == 0) return 0; - file::iovec_t b = {&v[0], size_t(s) }; - boost::int64_t read = f.readv(0, &b, 1, ec); - if (read != s) return -3; - if (ec) return -3; - return 0; - } - announce_entry::announce_entry(std::string const& u) : url(u) , next_announce(min_time()) @@ -1470,6 +1481,9 @@ namespace libtorrent } #if TORRENT_USE_I2P + + // TODO: 3 this function is used in other translation units. Make sure + // it's declared in an appropriate header. bool is_i2p_url(std::string const& url) { using boost::tuples::ignore; @@ -1480,6 +1494,7 @@ namespace libtorrent char const* top_domain = strrchr(hostname.c_str(), '.'); return top_domain && strcmp(top_domain, ".i2p") == 0; } + #endif bool torrent_info::parse_torrent_file(bdecode_node const& torrent_file diff --git a/src/torrent_peer.cpp b/src/torrent_peer.cpp index 8d9a9a046..f844a72df 100644 --- a/src/torrent_peer.cpp +++ b/src/torrent_peer.cpp @@ -39,13 +39,16 @@ POSSIBILITY OF SUCH DAMAGE. namespace libtorrent { - void apply_mask(boost::uint8_t* b, boost::uint8_t const* mask, int size) - { - for (int i = 0; i < size; ++i) + namespace { + + void apply_mask(boost::uint8_t* b, boost::uint8_t const* mask, int size) { - *b &= *mask; - ++b; - ++mask; + for (int i = 0; i < size; ++i) + { + *b &= *mask; + ++b; + ++mask; + } } }