From 6afb1f3e834bdc965efab89ad4847e9ad3c0bf76 Mon Sep 17 00:00:00 2001 From: Pavel Pimenov Date: Thu, 22 Dec 2016 18:44:36 +0300 Subject: [PATCH] aux::to_hex(sha1) -> sha1.to_hex() (#1418) generate hex strings into std::string instead of stack-allocated char arrays --- include/libtorrent/hex.hpp | 1 + src/alert.cpp | 16 ++++----------- src/bt_peer_connection.cpp | 5 +---- src/hex.cpp | 27 ++++++++++++++----------- src/kademlia/node.cpp | 16 ++++----------- src/kademlia/routing_table.cpp | 30 +++++++--------------------- src/kademlia/traversal_algorithm.cpp | 28 +++++++------------------- src/lsd.cpp | 8 +++----- src/sha1_hash.cpp | 4 +--- src/udp_tracker_connection.cpp | 8 ++------ test/setup_transfer.cpp | 4 +--- test/test_hasher.cpp | 11 ++++++++-- 12 files changed, 55 insertions(+), 103 deletions(-) diff --git a/include/libtorrent/hex.hpp b/include/libtorrent/hex.hpp index 0377b895b..fe50a12cb 100644 --- a/include/libtorrent/hex.hpp +++ b/include/libtorrent/hex.hpp @@ -54,6 +54,7 @@ namespace libtorrent // by ``out`` is large enough, i.e. has at least len * 2 bytes of space. TORRENT_DEPRECATED_EXPORT std::string to_hex(span s); TORRENT_DEPRECATED_EXPORT void to_hex(span in, char* out); + TORRENT_DEPRECATED_EXPORT void to_hex(char const* in, size_t const len, char* out); // converts the buffer [``in``, ``in`` + len) from hexadecimal to // binary. The binary output is written to the buffer pointed to diff --git a/src/alert.cpp b/src/alert.cpp index a972a0eb0..f757fc002 100644 --- a/src/alert.cpp +++ b/src/alert.cpp @@ -68,9 +68,7 @@ namespace libtorrent { } else { - char msg[41]; - aux::to_hex(t->info_hash(), msg); - m_name_idx = alloc.copy_string(msg); + m_name_idx = alloc.copy_string(aux::to_hex(t->info_hash())); } } else @@ -1120,11 +1118,9 @@ namespace libtorrent { std::string dht_announce_alert::message() const { error_code ec; - char ih_hex[41]; - aux::to_hex(info_hash, ih_hex); char msg[200]; std::snprintf(msg, sizeof(msg), "incoming dht announce: %s:%u (%s)" - , ip.to_string(ec).c_str(), port, ih_hex); + , ip.to_string(ec).c_str(), port, aux::to_hex(info_hash).c_str()); return msg; } @@ -1135,10 +1131,8 @@ namespace libtorrent { std::string dht_get_peers_alert::message() const { - char ih_hex[41]; - aux::to_hex(info_hash, ih_hex); char msg[200]; - std::snprintf(msg, sizeof(msg), "incoming dht get_peers: %s", ih_hex); + std::snprintf(msg, sizeof(msg), "incoming dht get_peers: %s", aux::to_hex(info_hash).c_str()); return msg; } @@ -1988,10 +1982,8 @@ namespace libtorrent { std::string dht_get_peers_reply_alert::message() const { - char ih_hex[41]; - aux::to_hex(info_hash, ih_hex); char msg[200]; - std::snprintf(msg, sizeof(msg), "incoming dht get_peers reply: %s, peers %d", ih_hex, m_num_peers); + std::snprintf(msg, sizeof(msg), "incoming dht get_peers reply: %s, peers %d", aux::to_hex(info_hash).c_str(), m_num_peers); return msg; } diff --git a/src/bt_peer_connection.cpp b/src/bt_peer_connection.cpp index be4ecc6c5..a3d2608cc 100644 --- a/src/bt_peer_connection.cpp +++ b/src/bt_peer_connection.cpp @@ -845,12 +845,9 @@ namespace libtorrent #ifndef TORRENT_DISABLE_LOGGING if (should_log(peer_log_alert::outgoing)) { - char hex_pid[41]; - aux::to_hex(m_our_peer_id, hex_pid); - hex_pid[40] = 0; peer_log(peer_log_alert::outgoing, "HANDSHAKE" , "sent peer_id: %s client: %s" - , hex_pid, identify_client(m_our_peer_id).c_str()); + , aux::to_hex(m_our_peer_id).c_str(), identify_client(m_our_peer_id).c_str()); } if (should_log(peer_log_alert::outgoing_message)) { diff --git a/src/hex.cpp b/src/hex.cpp index e0f3f2ade..a39fbe07c 100644 --- a/src/hex.cpp +++ b/src/hex.cpp @@ -73,28 +73,31 @@ namespace libtorrent extern char const hex_chars[]; char const hex_chars[] = "0123456789abcdef"; + void to_hex(char const* in, size_t const len, char* out) + { + int idx = 0; + for (size_t i=0; i < len; ++i) + { + out[idx++] = hex_chars[std::uint8_t(in[i]) >> 4]; + out[idx++] = hex_chars[std::uint8_t(in[i]) & 0xf]; + } + } - std::string to_hex(span s) + std::string to_hex(span in) { std::string ret; - ret.resize(s.size() * 2); - int idx = 0; - for (char const i : s) + if (!in.empty()) { - ret[idx++] = hex_chars[std::uint8_t(i) >> 4]; - ret[idx++] = hex_chars[std::uint8_t(i) & 0xf]; + ret.resize(in.size() * 2); + to_hex(in.data(), in.size(), &ret[0]); } return ret; } void to_hex(span in, char* out) { - for (char const i : in) - { - *out++ = hex_chars[std::uint8_t(i) >> 4]; - *out++ = hex_chars[std::uint8_t(i) & 0xf]; - } - *out = '\0'; + to_hex(in.data(), in.size(), out); + out[in.size() * 2] = '\0'; } } // aux namespace diff --git a/src/kademlia/node.cpp b/src/kademlia/node.cpp index 9936da3a5..33253c848 100644 --- a/src/kademlia/node.cpp +++ b/src/kademlia/node.cpp @@ -362,10 +362,8 @@ namespace auto logger = node.observer(); if (logger != nullptr && logger->should_log(dht_logger::node)) { - char hex_ih[41]; - aux::to_hex(ih, hex_ih); logger->log(dht_logger::node, "sending announce_peer [ ih: %s " - " p: %d nodes: %d ]", hex_ih, listen_port, int(v.size())); + " p: %d nodes: %d ]", aux::to_hex(ih).c_str(), listen_port, int(v.size())); } #endif @@ -444,10 +442,8 @@ void node::announce(sha1_hash const& info_hash, int const listen_port, int const #ifndef TORRENT_DISABLE_LOGGING if (m_observer != nullptr && m_observer->should_log(dht_logger::node)) { - char hex_ih[41]; - aux::to_hex(info_hash, hex_ih); m_observer->log(dht_logger::node, "announcing [ ih: %s p: %d ]" - , hex_ih, listen_port); + , aux::to_hex(info_hash).c_str(), listen_port); } #endif @@ -476,10 +472,8 @@ void node::get_item(sha1_hash const& target #ifndef TORRENT_DISABLE_LOGGING if (m_observer != nullptr && m_observer->should_log(dht_logger::node)) { - char hex_target[41]; - aux::to_hex(target, hex_target); m_observer->log(dht_logger::node, "starting get for [ hash: %s ]" - , hex_target); + , aux::to_hex(target).c_str()); } #endif @@ -533,10 +527,8 @@ void node::put_item(sha1_hash const& target, entry const& data, std::functionshould_log(dht_logger::node)) { - char hex_target[41]; - aux::to_hex(target, hex_target); m_observer->log(dht_logger::node, "starting get for [ hash: %s ]" - , hex_target); + , aux::to_hex(target).c_str()); } #endif diff --git a/src/kademlia/routing_table.cpp b/src/kademlia/routing_table.cpp index 5d172cef6..b041bfeba 100644 --- a/src/kademlia/routing_table.cpp +++ b/src/kademlia/routing_table.cpp @@ -488,10 +488,8 @@ routing_table::add_node_status_t routing_table::add_node_impl(node_entry e) #ifndef TORRENT_DISABLE_LOGGING if (m_log != nullptr && m_log->should_log(dht_logger::routing_table)) { - char hex_id[41]; - aux::to_hex(e.id, hex_id); m_log->log(dht_logger::routing_table, "ignoring node (duplicate IP): %s %s" - , hex_id, print_address(e.addr()).c_str()); + , aux::to_hex(e.id).c_str(), print_address(e.addr()).c_str()); } #endif return failed_to_add; @@ -531,12 +529,8 @@ routing_table::add_node_status_t routing_table::add_node_impl(node_entry e) #ifndef TORRENT_DISABLE_LOGGING if (m_log != nullptr && m_log->should_log(dht_logger::routing_table)) { - char hex_id_new[41]; - char hex_id_old[41]; - aux::to_hex(e.id, hex_id_new); - aux::to_hex(existing->id, hex_id_old); m_log->log(dht_logger::routing_table, "evicting node (changed ID): old: %s new: %s %s" - , hex_id_old, hex_id_new, print_address(e.addr()).c_str()); + , aux::to_hex(existing->id).c_str(), aux::to_hex(e.id).c_str(), print_address(e.addr()).c_str()); } #endif @@ -628,13 +622,9 @@ routing_table::add_node_status_t routing_table::add_node_impl(node_entry e) #ifndef TORRENT_DISABLE_LOGGING if (m_log != nullptr && m_log->should_log(dht_logger::routing_table)) { - char hex_id1[41]; - aux::to_hex(e.id, hex_id1); - char hex_id2[41]; - aux::to_hex(j->id, hex_id2); m_log->log(dht_logger::routing_table, "ignoring node: %s %s existing node: %s %s" - , hex_id1, print_address(e.addr()).c_str() - , hex_id2, print_address(j->addr()).c_str()); + , aux::to_hex(e.id).c_str(), print_address(e.addr()).c_str() + , aux::to_hex(j->id).c_str(), print_address(j->addr()).c_str()); } #endif return failed_to_add; @@ -822,10 +812,8 @@ ip_ok: #ifndef TORRENT_DISABLE_LOGGING if (m_log != nullptr && m_log->should_log(dht_logger::routing_table)) { - char hex_id[41]; - aux::to_hex(e.id, hex_id); m_log->log(dht_logger::routing_table, "replacing node with higher RTT: %s %s" - , hex_id, print_address(e.addr()).c_str()); + , aux::to_hex(e.id).c_str(), print_address(e.addr()).c_str()); } #endif return node_added; @@ -1019,10 +1007,8 @@ void routing_table::node_failed(node_id const& nid, udp::endpoint const& ep) #ifndef TORRENT_DISABLE_LOGGING if (m_log != nullptr && m_log->should_log(dht_logger::routing_table)) { - char hex_id[41]; - aux::to_hex(nid, hex_id); m_log->log(dht_logger::routing_table, "NODE FAILED id: %s ip: %s fails: %d pinged: %d up-time: %d" - , hex_id, print_endpoint(j->ep()).c_str() + , aux::to_hex(nid).c_str(), print_endpoint(j->ep()).c_str() , j->fail_count() , int(j->pinged()) , int(total_seconds(aux::time_now() - j->first_seen))); @@ -1043,10 +1029,8 @@ void routing_table::node_failed(node_id const& nid, udp::endpoint const& ep) #ifndef TORRENT_DISABLE_LOGGING if (m_log != nullptr && m_log->should_log(dht_logger::routing_table)) { - char hex_id[41]; - aux::to_hex(nid, hex_id); m_log->log(dht_logger::routing_table, "NODE FAILED id: %s ip: %s fails: %d pinged: %d up-time: %d" - , hex_id, print_endpoint(j->ep()).c_str() + , aux::to_hex(nid).c_str(), print_endpoint(j->ep()).c_str() , j->fail_count() , int(j->pinged()) , int(total_seconds(aux::time_now() - j->first_seen))); diff --git a/src/kademlia/traversal_algorithm.cpp b/src/kademlia/traversal_algorithm.cpp index e84be1000..1b8406b41 100644 --- a/src/kademlia/traversal_algorithm.cpp +++ b/src/kademlia/traversal_algorithm.cpp @@ -87,10 +87,8 @@ traversal_algorithm::traversal_algorithm( dht_observer* logger = get_node().observer(); if (logger != nullptr && logger->should_log(dht_logger::traversal)) { - char hex_target[41]; - aux::to_hex(target, hex_target); logger->log(dht_logger::traversal, "[%p] NEW target: %s k: %d" - , static_cast(this), hex_target, m_node.m_table.bucket_size()); + , static_cast(this), aux::to_hex(target).c_str(), m_node.m_table.bucket_size()); } #endif } @@ -168,11 +166,9 @@ void traversal_algorithm::add_entry(node_id const& id dht_observer* logger = get_node().observer(); if (logger != nullptr && logger->should_log(dht_logger::traversal)) { - char hex_id[41]; - aux::to_hex(o->id(), hex_id); logger->log(dht_logger::traversal , "[%p] traversal DUPLICATE node. id: %s addr: %s type: %s" - , static_cast(this), hex_id, print_address(o->target_addr()).c_str(), name()); + , static_cast(this), aux::to_hex(o->id()).c_str(), print_address(o->target_addr()).c_str(), name()); } #endif return; @@ -188,11 +184,9 @@ void traversal_algorithm::add_entry(node_id const& id dht_observer* logger = get_node().observer(); if (logger != nullptr && logger->should_log(dht_logger::traversal)) { - char hex_id[41]; - aux::to_hex(id, hex_id); logger->log(dht_logger::traversal , "[%p] ADD id: %s addr: %s distance: %d invoke-count: %d type: %s" - , static_cast(this), hex_id, print_endpoint(addr).c_str() + , static_cast(this), aux::to_hex(id).c_str(), print_endpoint(addr).c_str() , distance_exp(m_target, id), m_invoke_count, name()); } #endif @@ -317,12 +311,10 @@ void traversal_algorithm::failed(observer_ptr o, int const flags) dht_observer* logger = get_node().observer(); if (logger != nullptr && logger->should_log(dht_logger::traversal)) { - char hex_id[41]; - aux::to_hex(o->id(), hex_id); logger->log(dht_logger::traversal , "[%p] 1ST_TIMEOUT id: %s distance: %d addr: %s branch-factor: %d " "invoke-count: %d type: %s" - , static_cast(this), hex_id, distance_exp(m_target, o->id()) + , static_cast(this), aux::to_hex(o->id()).c_str(), distance_exp(m_target, o->id()) , print_address(o->target_addr()).c_str(), m_branch_factor , m_invoke_count, name()); } @@ -339,12 +331,10 @@ void traversal_algorithm::failed(observer_ptr o, int const flags) dht_observer* logger = get_node().observer(); if (logger != nullptr && logger->should_log(dht_logger::traversal)) { - char hex_id[41]; - aux::to_hex(o->id(), hex_id); logger->log(dht_logger::traversal , "[%p] TIMEOUT id: %s distance: %d addr: %s branch-factor: %d " "invoke-count: %d type: %s" - , static_cast(this), hex_id, distance_exp(m_target, o->id()) + , static_cast(this), aux::to_hex(o->id()).c_str(), distance_exp(m_target, o->id()) , print_address(o->target_addr()).c_str(), m_branch_factor , m_invoke_count, name()); } @@ -393,11 +383,9 @@ void traversal_algorithm::done() && logger != nullptr && logger->should_log(dht_logger::traversal)) { TORRENT_ASSERT(o->flags & observer::flag_queried); - char hex_id[41]; - aux::to_hex(o->id(), hex_id); logger->log(dht_logger::traversal , "[%p] id: %s distance: %d addr: %s" - , static_cast(this), hex_id, closest_target + , static_cast(this), aux::to_hex(o->id()).c_str(), closest_target , print_endpoint(o->target_ep()).c_str()); --results_target; @@ -474,14 +462,12 @@ bool traversal_algorithm::add_requests() dht_observer* logger = get_node().observer(); if (logger != nullptr && logger->should_log(dht_logger::traversal)) { - char hex_id[41]; - aux::to_hex(o->id(), hex_id); logger->log(dht_logger::traversal , "[%p] INVOKE nodes-left: %d top-invoke-count: %d " "invoke-count: %d branch-factor: %d " "distance: %d id: %s addr: %s type: %s" , static_cast(this), int(m_results.end() - i), outstanding, int(m_invoke_count) - , int(m_branch_factor), distance_exp(m_target, o->id()), hex_id + , int(m_branch_factor), distance_exp(m_target, o->id()), aux::to_hex(o->id()).c_str() , print_address(o->target_addr()).c_str(), name()); } #endif diff --git a/src/lsd.cpp b/src/lsd.cpp index f9d12ee48..d255f8d07 100644 --- a/src/lsd.cpp +++ b/src/lsd.cpp @@ -128,18 +128,16 @@ void lsd::announce_impl(sha1_hash const& ih, int const listen_port if (m_disabled) return; #endif - char ih_hex[41]; - aux::to_hex(ih, ih_hex); char msg[200]; #ifndef TORRENT_DISABLE_LOGGING - debug_log("==> LSD: ih: %s port: %u\n", ih_hex, listen_port); + debug_log("==> LSD: ih: %s port: %u\n", aux::to_hex(ih).c_str(), listen_port); #endif error_code ec; if (!m_disabled) { - int const msg_len = render_lsd_packet(msg, sizeof(msg), listen_port, ih_hex + int const msg_len = render_lsd_packet(msg, sizeof(msg), listen_port, aux::to_hex(ih).c_str() , m_cookie, "239.192.152.143"); m_socket.send(msg, msg_len, ec, broadcast ? broadcast_socket::flag_broadcast : 0); if (ec) @@ -158,7 +156,7 @@ void lsd::announce_impl(sha1_hash const& ih, int const listen_port #if TORRENT_USE_IPV6 if (!m_disabled6) { - int const msg_len = render_lsd_packet(msg, sizeof(msg), listen_port, ih_hex + int const msg_len = render_lsd_packet(msg, sizeof(msg), listen_port, aux::to_hex(ih).c_str() , m_cookie, "[ff15::efc0:988f]"); m_socket6.send(msg, msg_len, ec, broadcast ? broadcast_socket::flag_broadcast : 0); if (ec) diff --git a/src/sha1_hash.cpp b/src/sha1_hash.cpp index be8a85d93..673f1c503 100644 --- a/src/sha1_hash.cpp +++ b/src/sha1_hash.cpp @@ -45,9 +45,7 @@ namespace libtorrent // print a sha1_hash object to an ostream as 40 hexadecimal digits std::ostream& operator<<(std::ostream& os, sha1_hash const& peer) { - char out[sha1_hash::size() * 2 + 1]; - aux::to_hex(peer, out); - return os << out; + return os << aux::to_hex(peer); } // read 40 hexadecimal digits from an istream into a sha1_hash diff --git a/src/udp_tracker_connection.cpp b/src/udp_tracker_connection.cpp index 80d5017e0..bd34d9515 100644 --- a/src/udp_tracker_connection.cpp +++ b/src/udp_tracker_connection.cpp @@ -529,13 +529,11 @@ namespace libtorrent #ifndef TORRENT_DISABLE_LOGGING if (cb && cb->should_log()) { - char hex_ih[41]; - aux::to_hex(tracker_req().info_hash, hex_ih); cb->debug_log("==> UDP_TRACKER_CONNECT [ to: %s ih: %s]" , m_hostname.empty() ? print_endpoint(m_target).c_str() : (m_hostname + ":" + to_string(m_target.port()).data()).c_str() - , hex_ih); + , aux::to_hex(tracker_req().info_hash).c_str()); } #endif @@ -754,9 +752,7 @@ namespace libtorrent std::shared_ptr cb = requester(); if (cb && cb->should_log()) { - char hex_ih[41]; - aux::to_hex(req.info_hash, hex_ih); - cb->debug_log("==> UDP_TRACKER_ANNOUNCE [%s]", hex_ih); + cb->debug_log("==> UDP_TRACKER_ANNOUNCE [%s]", aux::to_hex(req.info_hash).c_str()); } #endif diff --git a/test/setup_transfer.cpp b/test/setup_transfer.cpp index 5cc4589bd..f41ad062f 100644 --- a/test/setup_transfer.cpp +++ b/test/setup_transfer.cpp @@ -841,9 +841,7 @@ setup_transfer(lt::session* ses1, lt::session* ses2, lt::session* ses3 remove_all(combine_path("tmp2" + suffix, "temporary"), ec); remove_all(combine_path("tmp3" + suffix, "temporary"), ec); } - char ih_hex[41]; - aux::to_hex(t->info_hash(), ih_hex); - std::printf("generated torrent: %s tmp1%s/temporary\n", ih_hex, suffix.c_str()); + std::printf("generated torrent: %s tmp1%s/temporary\n", aux::to_hex(t->info_hash()).c_str(), suffix.c_str()); } else { diff --git a/test/test_hasher.cpp b/test/test_hasher.cpp index 30ace89b8..744043b76 100644 --- a/test/test_hasher.cpp +++ b/test/test_hasher.cpp @@ -65,8 +65,15 @@ void test_vector(std::string s, std::string output, int const n = 1) hasher h; for (int i = 0; i < n; i++) h.update(s); - std::string digest = h.final().to_string(); - TEST_EQUAL(aux::to_hex(digest), output); + std::string const digest = h.final().to_string(); + std::string const digest_hex = aux::to_hex(digest); + + TEST_EQUAL(digest_hex, output); + + std::string output_hex = digest_hex; + aux::to_hex(digest.c_str(), digest.size(), &output_hex[0]); + + TEST_EQUAL(output_hex, digest_hex); } }