2003-10-23 01:00:57 +02:00
|
|
|
/*
|
|
|
|
|
2018-04-09 09:04:33 +02:00
|
|
|
Copyright (c) 2003-2018, Arvid Norberg
|
2003-10-23 01:00:57 +02:00
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TORRENT_BENCODE_HPP_INCLUDED
|
|
|
|
#define TORRENT_BENCODE_HPP_INCLUDED
|
|
|
|
|
2013-08-17 04:00:41 +02:00
|
|
|
// OVERVIEW
|
2016-10-22 17:47:24 +02:00
|
|
|
//
|
2019-02-24 03:16:27 +01:00
|
|
|
// Bencoding is a common representation in bittorrent used for dictionary,
|
2015-03-14 22:55:26 +01:00
|
|
|
// list, int and string hierarchies. It's used to encode .torrent files and
|
|
|
|
// some messages in the network protocol. libtorrent also uses it to store
|
2018-11-18 12:59:52 +01:00
|
|
|
// settings, resume data and other session state.
|
2013-08-17 04:00:41 +02:00
|
|
|
//
|
2018-11-18 12:59:52 +01:00
|
|
|
// Strings in bencoded structures do not necessarily represent text.
|
2015-03-14 22:55:26 +01:00
|
|
|
// Strings are raw byte buffers of a certain length. If a string is meant to be
|
|
|
|
// interpreted as text, it is required to be UTF-8 encoded. See `BEP 3`_.
|
2013-08-17 04:00:41 +02:00
|
|
|
//
|
2018-11-18 12:59:52 +01:00
|
|
|
// The function for decoding bencoded data bdecode(), returning a bdecode_node.
|
|
|
|
// This function builds a tree that points back into the original buffer. The
|
|
|
|
// returned bdecode_node will not be valid once the buffer it was parsed out of
|
|
|
|
// is discarded.
|
2013-08-17 04:00:41 +02:00
|
|
|
//
|
2018-11-18 12:59:52 +01:00
|
|
|
// It's possible to construct an entry from a bdecode_node, if a structure needs
|
|
|
|
// to be altered and re-encoded.
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2012-08-16 02:48:13 +02:00
|
|
|
#include <string>
|
|
|
|
#include <iterator> // for distance
|
2004-01-25 19:18:36 +01:00
|
|
|
|
2005-11-01 19:30:39 +01:00
|
|
|
#include "libtorrent/config.hpp"
|
2016-05-23 14:15:39 +02:00
|
|
|
#include "libtorrent/entry.hpp"
|
2007-09-10 08:12:41 +02:00
|
|
|
#include "libtorrent/assert.hpp"
|
2014-05-04 08:46:47 +02:00
|
|
|
#include "libtorrent/io.hpp" // for write_string
|
2016-08-18 23:08:40 +02:00
|
|
|
#include "libtorrent/string_util.hpp" // for is_digit
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2017-04-12 19:00:57 +02:00
|
|
|
namespace libtorrent {
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2018-04-26 09:01:14 +02:00
|
|
|
#if TORRENT_ABI_VERSION == 1
|
2016-05-15 06:33:06 +02:00
|
|
|
using invalid_encoding = system_error;
|
2014-11-29 10:12:44 +01:00
|
|
|
#endif
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2017-04-12 19:00:57 +02:00
|
|
|
namespace detail {
|
|
|
|
|
2017-01-09 07:43:57 +01:00
|
|
|
template <class OutIt, class In, typename Cond
|
|
|
|
= typename std::enable_if<std::is_integral<In>::value>::type>
|
|
|
|
int write_integer(OutIt& out, In data)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2017-01-09 15:32:52 +01:00
|
|
|
entry::integer_type const val = entry::integer_type(data);
|
2017-01-09 07:43:57 +01:00
|
|
|
TORRENT_ASSERT(data == In(val));
|
2005-08-12 14:40:58 +02:00
|
|
|
// the stack allocated buffer for keeping the
|
|
|
|
// decimal representation of the number can
|
|
|
|
// not hold number bigger than this:
|
2016-05-23 14:15:39 +02:00
|
|
|
static_assert(sizeof(entry::integer_type) <= 8, "64 bit integers required");
|
2017-01-09 07:43:57 +01:00
|
|
|
static_assert(sizeof(data) <= sizeof(entry::integer_type), "input data too big, see entry::integer_type");
|
2005-08-12 14:40:58 +02:00
|
|
|
char buf[21];
|
2018-11-16 02:39:31 +01:00
|
|
|
auto const str = integer_to_str(buf, val);
|
|
|
|
for (char const c : str)
|
2005-08-12 14:40:58 +02:00
|
|
|
{
|
2018-11-16 02:39:31 +01:00
|
|
|
*out = c;
|
2005-08-12 14:40:58 +02:00
|
|
|
++out;
|
|
|
|
}
|
2018-11-16 02:39:31 +01:00
|
|
|
return static_cast<int>(str.size());
|
2015-08-08 08:33:54 +02:00
|
|
|
}
|
|
|
|
|
2005-05-25 12:01:01 +02:00
|
|
|
template <class OutIt>
|
|
|
|
void write_char(OutIt& out, char c)
|
|
|
|
{
|
|
|
|
*out = c;
|
|
|
|
++out;
|
|
|
|
}
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
template <class InIt>
|
2007-11-02 02:08:10 +01:00
|
|
|
std::string read_until(InIt& in, InIt end, char end_token, bool& err)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
|
|
|
std::string ret;
|
2007-11-02 02:08:10 +01:00
|
|
|
if (in == end)
|
|
|
|
{
|
|
|
|
err = true;
|
|
|
|
return ret;
|
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
while (*in != end_token)
|
|
|
|
{
|
|
|
|
ret += *in;
|
|
|
|
++in;
|
2007-11-02 02:08:10 +01:00
|
|
|
if (in == end)
|
|
|
|
{
|
|
|
|
err = true;
|
|
|
|
return ret;
|
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class InIt>
|
2007-11-02 02:08:10 +01:00
|
|
|
void read_string(InIt& in, InIt end, int len, std::string& str, bool& err)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(len >= 0);
|
2003-10-23 01:00:57 +02:00
|
|
|
for (int i = 0; i < len; ++i)
|
|
|
|
{
|
2007-11-02 02:08:10 +01:00
|
|
|
if (in == end)
|
|
|
|
{
|
|
|
|
err = true;
|
|
|
|
return;
|
|
|
|
}
|
2006-04-30 02:39:18 +02:00
|
|
|
str += *in;
|
2003-10-23 01:00:57 +02:00
|
|
|
++in;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class OutIt>
|
2007-12-22 09:15:05 +01:00
|
|
|
int bencode_recursive(OutIt& out, const entry& e)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2007-12-22 09:15:05 +01:00
|
|
|
int ret = 0;
|
2003-10-23 01:00:57 +02:00
|
|
|
switch(e.type())
|
|
|
|
{
|
|
|
|
case entry::int_t:
|
2005-05-25 12:01:01 +02:00
|
|
|
write_char(out, 'i');
|
2007-12-22 09:15:05 +01:00
|
|
|
ret += write_integer(out, e.integer());
|
2005-05-25 12:01:01 +02:00
|
|
|
write_char(out, 'e');
|
2007-12-22 09:15:05 +01:00
|
|
|
ret += 2;
|
2003-10-23 01:00:57 +02:00
|
|
|
break;
|
|
|
|
case entry::string_t:
|
2007-12-22 09:15:05 +01:00
|
|
|
ret += write_integer(out, e.string().length());
|
2005-05-25 12:01:01 +02:00
|
|
|
write_char(out, ':');
|
2014-05-04 08:46:47 +02:00
|
|
|
ret += write_string(e.string(), out);
|
2007-12-22 09:15:05 +01:00
|
|
|
ret += 1;
|
2003-10-23 01:00:57 +02:00
|
|
|
break;
|
|
|
|
case entry::list_t:
|
2005-05-25 12:01:01 +02:00
|
|
|
write_char(out, 'l');
|
2018-03-15 22:36:15 +01:00
|
|
|
for (auto const& i : e.list())
|
|
|
|
ret += bencode_recursive(out, i);
|
2005-05-25 12:01:01 +02:00
|
|
|
write_char(out, 'e');
|
2007-12-22 09:15:05 +01:00
|
|
|
ret += 2;
|
2003-10-23 01:00:57 +02:00
|
|
|
break;
|
|
|
|
case entry::dictionary_t:
|
2005-05-25 12:01:01 +02:00
|
|
|
write_char(out, 'd');
|
2018-03-15 22:36:15 +01:00
|
|
|
for (auto const& i : e.dict())
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
|
|
|
// write key
|
2018-03-15 22:36:15 +01:00
|
|
|
ret += write_integer(out, i.first.length());
|
2005-05-25 12:01:01 +02:00
|
|
|
write_char(out, ':');
|
2018-03-15 22:36:15 +01:00
|
|
|
ret += write_string(i.first, out);
|
2003-10-23 01:00:57 +02:00
|
|
|
// write value
|
2018-03-15 22:36:15 +01:00
|
|
|
ret += bencode_recursive(out, i.second);
|
2007-12-22 09:15:05 +01:00
|
|
|
ret += 1;
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
2005-05-25 12:01:01 +02:00
|
|
|
write_char(out, 'e');
|
2007-12-22 09:15:05 +01:00
|
|
|
ret += 2;
|
2003-10-23 01:00:57 +02:00
|
|
|
break;
|
2016-05-06 03:38:57 +02:00
|
|
|
case entry::preformatted_t:
|
|
|
|
std::copy(e.preformatted().begin(), e.preformatted().end(), out);
|
2018-04-23 07:54:22 +02:00
|
|
|
ret += static_cast<int>(e.preformatted().size());
|
2016-05-06 03:38:57 +02:00
|
|
|
break;
|
2015-08-02 01:55:28 +02:00
|
|
|
case entry::undefined_t:
|
2016-05-26 09:33:05 +02:00
|
|
|
|
|
|
|
// empty string
|
|
|
|
write_char(out, '0');
|
|
|
|
write_char(out, ':');
|
|
|
|
|
|
|
|
ret += 2;
|
2005-10-16 23:14:08 +02:00
|
|
|
break;
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
2007-12-22 09:15:05 +01:00
|
|
|
return ret;
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
2018-11-18 12:59:52 +01:00
|
|
|
#if TORRENT_ABI_VERSION == 1
|
2003-10-23 01:00:57 +02:00
|
|
|
template<class InIt>
|
2008-01-28 03:58:17 +01:00
|
|
|
void bdecode_recursive(InIt& in, InIt end, entry& ret, bool& err, int depth)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2008-01-28 03:58:17 +01:00
|
|
|
if (depth >= 100)
|
|
|
|
{
|
|
|
|
err = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-11-02 02:08:10 +01:00
|
|
|
if (in == end)
|
|
|
|
{
|
|
|
|
err = true;
|
2016-06-28 04:28:51 +02:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2007-12-28 20:07:28 +01:00
|
|
|
ret.m_type_queried = false;
|
|
|
|
#endif
|
2007-11-02 02:08:10 +01:00
|
|
|
return;
|
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
switch (*in)
|
|
|
|
{
|
|
|
|
|
|
|
|
// ----------------------------------------------
|
|
|
|
// integer
|
|
|
|
case 'i':
|
|
|
|
{
|
2015-06-03 07:12:54 +02:00
|
|
|
++in; // 'i'
|
2018-09-04 13:46:09 +02:00
|
|
|
std::string const val = read_until(in, end, 'e', err);
|
2007-11-02 02:08:10 +01:00
|
|
|
if (err) return;
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(*in == 'e');
|
2015-06-03 07:12:54 +02:00
|
|
|
++in; // 'e'
|
2003-10-23 01:00:57 +02:00
|
|
|
ret = entry(entry::int_t);
|
2009-04-01 05:37:54 +02:00
|
|
|
char* end_pointer;
|
2017-04-04 22:42:37 +02:00
|
|
|
ret.integer() = std::strtoll(val.c_str(), &end_pointer, 10);
|
2016-06-28 04:28:51 +02:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2007-12-28 20:07:28 +01:00
|
|
|
ret.m_type_queried = false;
|
|
|
|
#endif
|
2009-04-01 05:37:54 +02:00
|
|
|
if (end_pointer == val.c_str())
|
|
|
|
{
|
|
|
|
err = true;
|
|
|
|
return;
|
|
|
|
}
|
2018-09-04 13:46:09 +02:00
|
|
|
}
|
|
|
|
break;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
|
|
|
// ----------------------------------------------
|
|
|
|
// list
|
|
|
|
case 'l':
|
|
|
|
ret = entry(entry::list_t);
|
|
|
|
++in; // 'l'
|
|
|
|
while (*in != 'e')
|
|
|
|
{
|
2018-03-15 22:36:15 +01:00
|
|
|
ret.list().emplace_back();
|
2004-03-05 13:04:47 +01:00
|
|
|
entry& e = ret.list().back();
|
2008-01-28 03:58:17 +01:00
|
|
|
bdecode_recursive(in, end, e, err, depth + 1);
|
2007-12-28 20:07:28 +01:00
|
|
|
if (err)
|
|
|
|
{
|
2016-06-28 04:28:51 +02:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2007-12-28 20:07:28 +01:00
|
|
|
ret.m_type_queried = false;
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
2007-11-02 02:08:10 +01:00
|
|
|
if (in == end)
|
|
|
|
{
|
|
|
|
err = true;
|
2016-06-28 04:28:51 +02:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2007-12-28 20:07:28 +01:00
|
|
|
ret.m_type_queried = false;
|
|
|
|
#endif
|
2007-11-02 02:08:10 +01:00
|
|
|
return;
|
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
2016-06-28 04:28:51 +02:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2007-12-28 20:07:28 +01:00
|
|
|
ret.m_type_queried = false;
|
|
|
|
#endif
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(*in == 'e');
|
2003-10-23 01:00:57 +02:00
|
|
|
++in; // 'e'
|
2018-09-04 13:46:09 +02:00
|
|
|
break;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
|
|
|
// ----------------------------------------------
|
|
|
|
// dictionary
|
|
|
|
case 'd':
|
|
|
|
ret = entry(entry::dictionary_t);
|
|
|
|
++in; // 'd'
|
|
|
|
while (*in != 'e')
|
|
|
|
{
|
2004-03-05 13:04:47 +01:00
|
|
|
entry key;
|
2008-01-28 03:58:17 +01:00
|
|
|
bdecode_recursive(in, end, key, err, depth + 1);
|
2007-12-28 20:07:28 +01:00
|
|
|
if (err || key.type() != entry::string_t)
|
2015-06-03 07:12:54 +02:00
|
|
|
{
|
2016-06-28 04:28:51 +02:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2007-12-28 20:07:28 +01:00
|
|
|
ret.m_type_queried = false;
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
2004-03-05 13:04:47 +01:00
|
|
|
entry& e = ret[key.string()];
|
2008-01-28 03:58:17 +01:00
|
|
|
bdecode_recursive(in, end, e, err, depth + 1);
|
2007-12-28 20:07:28 +01:00
|
|
|
if (err)
|
|
|
|
{
|
2016-06-28 04:28:51 +02:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2007-12-28 20:07:28 +01:00
|
|
|
ret.m_type_queried = false;
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
2007-11-02 02:08:10 +01:00
|
|
|
if (in == end)
|
|
|
|
{
|
|
|
|
err = true;
|
2016-06-28 04:28:51 +02:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2007-12-28 20:07:28 +01:00
|
|
|
ret.m_type_queried = false;
|
|
|
|
#endif
|
2007-11-02 02:08:10 +01:00
|
|
|
return;
|
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
2016-06-28 04:28:51 +02:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2007-12-28 20:07:28 +01:00
|
|
|
ret.m_type_queried = false;
|
|
|
|
#endif
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(*in == 'e');
|
2003-10-23 01:00:57 +02:00
|
|
|
++in; // 'e'
|
2018-09-04 13:46:09 +02:00
|
|
|
break;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
|
|
|
// ----------------------------------------------
|
|
|
|
// string
|
|
|
|
default:
|
2017-01-09 07:43:57 +01:00
|
|
|
static_assert(sizeof(*in) == 1, "Input iterator to 8 bit data required");
|
|
|
|
if (is_digit(char(*in)))
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2007-11-02 02:08:10 +01:00
|
|
|
std::string len_s = read_until(in, end, ':', err);
|
2007-12-28 20:07:28 +01:00
|
|
|
if (err)
|
|
|
|
{
|
2016-06-28 04:28:51 +02:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2007-12-28 20:07:28 +01:00
|
|
|
ret.m_type_queried = false;
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(*in == ':');
|
2003-10-23 01:00:57 +02:00
|
|
|
++in; // ':'
|
2012-08-16 02:48:13 +02:00
|
|
|
int len = atoi(len_s.c_str());
|
2003-10-23 01:00:57 +02:00
|
|
|
ret = entry(entry::string_t);
|
2007-11-02 02:08:10 +01:00
|
|
|
read_string(in, end, len, ret.string(), err);
|
2007-12-28 20:07:28 +01:00
|
|
|
if (err)
|
|
|
|
{
|
2016-06-28 04:28:51 +02:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2007-12-28 20:07:28 +01:00
|
|
|
ret.m_type_queried = false;
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-11-02 02:08:10 +01:00
|
|
|
err = true;
|
2016-06-28 04:28:51 +02:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2007-12-28 20:07:28 +01:00
|
|
|
ret.m_type_queried = false;
|
|
|
|
#endif
|
2007-11-02 02:08:10 +01:00
|
|
|
return;
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
2016-06-28 04:28:51 +02:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2007-12-28 20:07:28 +01:00
|
|
|
ret.m_type_queried = false;
|
|
|
|
#endif
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
}
|
2018-11-18 12:59:52 +01:00
|
|
|
#endif // TORRENT_ABI_VERSION
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
2015-06-03 07:12:54 +02:00
|
|
|
|
2018-11-18 12:59:52 +01:00
|
|
|
// This function will encode data to bencoded form.
|
2016-10-22 17:47:24 +02:00
|
|
|
//
|
2013-08-08 03:03:54 +02:00
|
|
|
// The entry_ class is the internal representation of the bencoded data
|
|
|
|
// and it can be used to retrieve information, an entry_ can also be build by
|
|
|
|
// the program and given to ``bencode()`` to encode it into the ``OutIt``
|
|
|
|
// iterator.
|
2016-10-22 17:47:24 +02:00
|
|
|
//
|
2018-11-18 12:59:52 +01:00
|
|
|
// ``OutIt`` is an OutputIterator_. It's a template and usually
|
|
|
|
// instantiated as ostream_iterator_ or back_insert_iterator_. This
|
|
|
|
// function assumes the value_type of the iterator is a ``char``.
|
|
|
|
// In order to encode entry ``e`` into a buffer, do::
|
2016-10-22 17:47:24 +02:00
|
|
|
//
|
2013-08-08 03:03:54 +02:00
|
|
|
// std::vector<char> buffer;
|
|
|
|
// bencode(std::back_inserter(buf), e);
|
2016-10-22 17:47:24 +02:00
|
|
|
//
|
2018-11-18 12:59:52 +01:00
|
|
|
// .. _OutputIterator: https://en.cppreference.com/w/cpp/named_req/OutputIterator
|
|
|
|
// .. _ostream_iterator: https://en.cppreference.com/w/cpp/iterator/ostream_iterator
|
|
|
|
// .. _back_insert_iterator: https://en.cppreference.com/w/cpp/iterator/back_insert_iterator
|
2013-08-12 18:24:37 +02:00
|
|
|
template<class OutIt> int bencode(OutIt out, const entry& e)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2007-12-22 09:15:05 +01:00
|
|
|
return detail::bencode_recursive(out, e);
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
2018-11-18 12:59:52 +01:00
|
|
|
|
|
|
|
#if TORRENT_ABI_VERSION == 1
|
|
|
|
template<class InIt>
|
|
|
|
TORRENT_DEPRECATED
|
|
|
|
entry bdecode(InIt start, InIt end)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2007-11-02 02:08:10 +01:00
|
|
|
entry e;
|
|
|
|
bool err = false;
|
2008-01-28 03:58:17 +01:00
|
|
|
detail::bdecode_recursive(start, end, e, err, 0);
|
2007-12-28 20:07:28 +01:00
|
|
|
TORRENT_ASSERT(e.m_type_queried == false);
|
2008-06-24 00:00:27 +02:00
|
|
|
if (err) return entry();
|
2007-11-02 02:08:10 +01:00
|
|
|
return e;
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
2018-11-18 12:59:52 +01:00
|
|
|
template<class InIt>
|
|
|
|
TORRENT_DEPRECATED
|
|
|
|
entry bdecode(InIt start, InIt end
|
2016-12-05 02:15:49 +01:00
|
|
|
, typename std::iterator_traits<InIt>::difference_type& len)
|
2007-12-22 09:15:05 +01:00
|
|
|
{
|
|
|
|
entry e;
|
|
|
|
bool err = false;
|
|
|
|
InIt s = start;
|
2008-01-28 03:58:17 +01:00
|
|
|
detail::bdecode_recursive(start, end, e, err, 0);
|
2007-12-22 09:15:05 +01:00
|
|
|
len = std::distance(s, start);
|
|
|
|
TORRENT_ASSERT(len >= 0);
|
2008-06-24 00:00:27 +02:00
|
|
|
if (err) return entry();
|
2007-12-22 09:15:05 +01:00
|
|
|
return e;
|
|
|
|
}
|
2018-11-18 12:59:52 +01:00
|
|
|
#endif
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // TORRENT_BENCODE_HPP_INCLUDED
|