aux::to_hex(sha1) -> sha1.to_hex() (#1418)

generate hex strings into std::string instead of stack-allocated char arrays
This commit is contained in:
Pavel Pimenov 2016-12-22 18:44:36 +03:00 committed by Arvid Norberg
parent 3f1084d63d
commit 6afb1f3e83
12 changed files with 55 additions and 103 deletions

View File

@ -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<char const> s);
TORRENT_DEPRECATED_EXPORT void to_hex(span<char const> 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

View File

@ -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;
}

View File

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

View File

@ -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<char const> s)
std::string to_hex(span<char const> 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<char const> 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

View File

@ -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::function<vo
#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

View File

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

View File

@ -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<void*>(this), hex_target, m_node.m_table.bucket_size());
, static_cast<void*>(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<void*>(this), hex_id, print_address(o->target_addr()).c_str(), name());
, static_cast<void*>(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<void*>(this), hex_id, print_endpoint(addr).c_str()
, static_cast<void*>(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<void*>(this), hex_id, distance_exp(m_target, o->id())
, static_cast<void*>(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<void*>(this), hex_id, distance_exp(m_target, o->id())
, static_cast<void*>(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<void*>(this), hex_id, closest_target
, static_cast<void*>(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<void*>(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

View File

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

View File

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

View File

@ -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<request_callback> 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

View File

@ -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
{

View File

@ -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);
}
}