use span for read_resume_data() (#1867)

use span for read_resume_data()
This commit is contained in:
Arvid Norberg 2017-03-29 16:10:32 -04:00 committed by GitHub
parent 8ebbad19d4
commit b8e77444d5
12 changed files with 50 additions and 30 deletions

View File

@ -559,7 +559,7 @@ namespace
add_torrent_params read_resume_data_wrapper(bytes const& b)
{
error_code ec;
add_torrent_params p = read_resume_data(&b.arr[0], int(b.arr.size()), ec);
add_torrent_params p = read_resume_data(b.arr, ec);
#ifndef BOOST_NO_EXCEPTIONS
if (ec) throw system_error(ec);
#endif

View File

@ -87,7 +87,7 @@ int main(int argc, char const* argv[])
, std::istream_iterator<char>()};
lt::error_code ec;
lt::add_torrent_params atp = lt::read_resume_data(&buf[0], int(buf.size()), ec);
lt::add_torrent_params atp = lt::read_resume_data(buf, ec);
if (ec) {
std::cerr << "failed to read resume data: " << ec.message() << std::endl;
return 1;

View File

@ -555,7 +555,7 @@ void add_magnet(lt::session& ses, lt::string_view uri)
load_file(resume_file(p.info_hash), resume_data, ec);
if (!ec)
{
p = lt::read_resume_data(&resume_data[0], int(resume_data.size()), ec);
p = lt::read_resume_data(resume_data, ec);
if (ec) std::printf(" failed to load resume data: %s\n", ec.message().c_str());
parse_magnet_uri(uri.to_string(), p, ec);
}
@ -599,7 +599,7 @@ bool add_torrent(libtorrent::session& ses, std::string torrent)
load_file(resume_file(ti->info_hash()), resume_data, ec);
if (!ec)
{
p = lt::read_resume_data(&resume_data[0], int(resume_data.size()), ec);
p = lt::read_resume_data(resume_data, ec);
if (ec) std::printf(" failed to load resume data: %s\n", ec.message().c_str());
}
ec.clear();
@ -1286,7 +1286,7 @@ MAGNETURL is a magnet link
, file.c_str(), ec.message().c_str());
continue;
}
add_torrent_params p = lt::read_resume_data(&resume_data[0], int(resume_data.size()), ec);
add_torrent_params p = lt::read_resume_data(resume_data, ec);
if (ec)
{
std::printf(" failed to parse resume data \"%s\": %s\n"

View File

@ -247,12 +247,14 @@ struct bdecode_token
// There are 5 different types of nodes, see type_t.
struct TORRENT_EXPORT bdecode_node
{
// TODO: 3 make this take span<char const> for buffer, and make it return a
// bdecode_node
// TODO: 3 deprecate this overload
TORRENT_EXPORT friend int bdecode(char const* start, char const* end, bdecode_node& ret
, error_code& ec, int* error_pos, int depth_limit
, int token_limit);
TORRENT_EXPORT friend bdecode_node bdecode(span<char const> buffer
, error_code& ec, int* error_pos, int depth_limit, int token_limit);
// creates a default constructed node, it will have the type ``none_t``.
bdecode_node();
@ -260,6 +262,8 @@ struct TORRENT_EXPORT bdecode_node
// underlying buffer remains the same.
bdecode_node(bdecode_node const&);
bdecode_node& operator=(bdecode_node const&);
bdecode_node(bdecode_node&&);
bdecode_node& operator=(bdecode_node&&);
// the types of bdecoded nodes
enum type_t
@ -421,6 +425,9 @@ TORRENT_EXPORT std::string print_entry(bdecode_node const& e
TORRENT_EXPORT int bdecode(char const* start, char const* end, bdecode_node& ret
, error_code& ec, int* error_pos = nullptr, int depth_limit = 100
, int token_limit = 1000000);
TORRENT_EXPORT bdecode_node bdecode(span<char const> buffer
, error_code& ec, int* error_pos = nullptr, int depth_limit = 100
, int token_limit = 1000000);
}

View File

@ -35,6 +35,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/error_code.hpp"
#include "libtorrent/export.hpp"
#include "libtorrent/span.hpp"
namespace libtorrent
{
@ -51,8 +52,8 @@ namespace libtorrent
// data but before adding the torrent.
TORRENT_EXPORT add_torrent_params read_resume_data(bdecode_node const& rd
, error_code& ec);
TORRENT_EXPORT add_torrent_params read_resume_data(char const* buffer
, int size, error_code& ec);
TORRENT_EXPORT add_torrent_params read_resume_data(span<char const> buffer
, error_code& ec);
}
#endif

View File

@ -243,6 +243,9 @@ namespace libtorrent
return *this;
}
bdecode_node::bdecode_node(bdecode_node&&) = default;
bdecode_node& bdecode_node::operator=(bdecode_node&&) = default;
bdecode_node::bdecode_node(bdecode_token const* tokens, char const* buf
, int len, int idx)
: m_root_tokens(tokens)
@ -647,14 +650,21 @@ namespace libtorrent
int bdecode(char const* start, char const* end, bdecode_node& ret
, error_code& ec, int* error_pos, int const depth_limit, int token_limit)
{
ec.clear();
ret.clear();
ret = bdecode({start, static_cast<size_t>(end - start)}, ec, error_pos, depth_limit, token_limit);
return ec ? -1 : 0;
}
if (end - start > bdecode_token::max_offset)
bdecode_node bdecode(span<char const> buffer
, error_code& ec, int* error_pos, int depth_limit, int token_limit)
{
bdecode_node ret;
ec.clear();
if (buffer.size() > bdecode_token::max_offset)
{
if (error_pos) *error_pos = 0;
ec = bdecode_errors::limit_exceeded;
return -1;
return ret;
}
// this is the stack of bdecode_token indices, into m_tokens.
@ -662,6 +672,9 @@ namespace libtorrent
int sp = 0;
TORRENT_ALLOCA(stack, stack_frame, depth_limit);
// TODO: 2 attempt to simplify this implementation by embracing the span
char const* start = buffer.data();
char const* end = start + buffer.size();
char const* const orig_start = start;
if (start == end)
@ -853,7 +866,7 @@ done:
ret.m_buffer_size = int(start - orig_start);
ret.m_root_tokens = ret.m_tokens.data();
return ec ? -1 : 0;
return ret;
}
namespace {

View File

@ -332,10 +332,9 @@ namespace libtorrent
return ret;
}
add_torrent_params read_resume_data(char const* buffer, int size, error_code& ec)
add_torrent_params read_resume_data(span<char const> buffer, error_code& ec)
{
bdecode_node rd;
bdecode(buffer, buffer + size, rd, ec);
bdecode_node rd = bdecode(buffer, ec);
if (ec) return add_torrent_params();
return read_resume_data(rd, ec);

View File

@ -200,7 +200,7 @@ namespace libtorrent
error_code ec;
add_torrent_params resume_data
= read_resume_data(&atp.resume_data[0], int(atp.resume_data.size()), ec);
= read_resume_data(atp.resume_data, ec);
resume_data.internal_resume_data_error = ec;
if (ec) return;

View File

@ -295,7 +295,7 @@ done:
#endif
{
error_code ec;
p = read_resume_data(&resume_data[0], int(resume_data.size()), ec);
p = read_resume_data(resume_data, ec);
TEST_CHECK(!ec);
}
p.flags &= ~add_torrent_params::flag_paused;

View File

@ -77,7 +77,7 @@ TORRENT_TEST(read_resume)
bencode(std::back_inserter(resume_data), rd);
error_code ec;
add_torrent_params atp = read_resume_data(&resume_data[0], int(resume_data.size()), ec);
add_torrent_params atp = read_resume_data(resume_data, ec);
TEST_CHECK(!ec);
@ -123,7 +123,7 @@ TORRENT_TEST(read_resume_missing_info_hash)
bencode(std::back_inserter(resume_data), rd);
error_code ec;
add_torrent_params atp = read_resume_data(&resume_data[0], int(resume_data.size()), ec);
add_torrent_params atp = read_resume_data(resume_data, ec);
TEST_EQUAL(ec, error_code(errors::missing_info_hash));
}
@ -139,7 +139,7 @@ TORRENT_TEST(read_resume_missing_file_format)
bencode(std::back_inserter(resume_data), rd);
error_code ec;
add_torrent_params atp = read_resume_data(&resume_data[0], int(resume_data.size()), ec);
add_torrent_params atp = read_resume_data(resume_data, ec);
TEST_EQUAL(ec, error_code(errors::invalid_file_tag));
}
@ -161,7 +161,7 @@ TORRENT_TEST(read_resume_mismatching_torrent)
// the info-hash field does not match the torrent in the "info" field, so it
// will be ignored
error_code ec;
add_torrent_params atp = read_resume_data(&resume_data[0], int(resume_data.size()), ec);
add_torrent_params atp = read_resume_data(resume_data, ec);
TEST_CHECK(!ec);
TEST_CHECK(!atp.ti);
}
@ -207,7 +207,7 @@ TORRENT_TEST(read_resume_torrent)
// the info-hash field does not match the torrent in the "info" field, so it
// will be ignored
error_code ec;
add_torrent_params atp = read_resume_data(&resume_data[0], int(resume_data.size()), ec);
add_torrent_params atp = read_resume_data(resume_data, ec);
TEST_CHECK(!ec);
TEST_CHECK(atp.ti);

View File

@ -157,7 +157,7 @@ torrent_handle test_resume_flags(lt::session& ses, int flags
#endif
{
error_code ec;
p = read_resume_data(&rd[0], int(rd.size()), ec);
p = read_resume_data(rd, ec);
TEST_CHECK(!ec);
}
@ -241,7 +241,7 @@ void test_piece_priorities(bool test_deprecated = false)
#endif
{
error_code ec;
p = read_resume_data(&resume_data[0], int(resume_data.size()), ec);
p = read_resume_data(resume_data, ec);
TEST_CHECK(!ec);
p.ti = ti;
p.save_path = ".";
@ -735,7 +735,7 @@ void test_zero_file_prio(bool test_deprecated = false)
#endif
{
error_code ec;
p = read_resume_data(&resume_data[0], int(resume_data.size()), ec);
p = read_resume_data(resume_data, ec);
TEST_CHECK(!ec);
p.ti = ti;
p.save_path = ".";
@ -820,7 +820,7 @@ void test_seed_mode(bool const file_prio, bool const pieces_have, bool const pie
#endif
{
error_code ec;
p = read_resume_data(&resume_data[0], int(resume_data.size()), ec);
p = read_resume_data(resume_data, ec);
TEST_CHECK(!ec);
p.ti = ti;
p.save_path = ".";

View File

@ -710,7 +710,7 @@ void test_fastresume(bool const test_deprecated)
else
#endif
{
p = read_resume_data(&resume_data[0], int(resume_data.size()), ec);
p = read_resume_data(resume_data, ec);
TEST_CHECK(!ec);
}
@ -880,7 +880,7 @@ void test_rename_file_fastresume(bool test_deprecated)
else
#endif
{
p = read_resume_data(&resume_data[0], int(resume_data.size()), ec);
p = read_resume_data(resume_data, ec);
TEST_CHECK(!ec);
}
p.ti = std::make_shared<torrent_info>(std::cref(*t));