2003-10-23 01:00:57 +02:00
|
|
|
/*
|
|
|
|
|
2016-01-18 00:57:46 +01:00
|
|
|
Copyright (c) 2003-2016, 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
|
|
|
|
//
|
2015-03-14 22:55:26 +01:00
|
|
|
// Bencoding is a common representation in bittorrent used for for dictionary,
|
|
|
|
// 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
|
|
|
|
// settings, resume data and other state between sessions.
|
2013-08-17 04:00:41 +02:00
|
|
|
//
|
2015-03-14 22:55:26 +01:00
|
|
|
// Strings in bencoded structures are not necessarily representing text.
|
|
|
|
// 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
|
|
|
//
|
2016-08-02 06:15:26 +02:00
|
|
|
// There are two mechanisms to *decode* bencoded buffers in libtorrent.
|
2013-08-17 04:00:41 +02:00
|
|
|
//
|
2015-03-12 06:20:12 +01:00
|
|
|
// The most flexible one is `bdecode() bencode()`_, which returns a structure
|
2016-08-02 06:15:26 +02:00
|
|
|
// represented by entry. Once a buffer has been decoded with this function, it
|
2015-03-14 22:55:26 +01:00
|
|
|
// can be discarded. The entry does not contain any references back to it. This
|
|
|
|
// means that bdecode() copies all the data out of the buffer and into its own
|
|
|
|
// hierarchy. This makes this function expensive, which might matter if you're
|
|
|
|
// parsing large amounts of data.
|
2013-08-17 04:00:41 +02:00
|
|
|
//
|
2015-03-12 06:20:12 +01:00
|
|
|
// Another consideration is that `bdecode() bencode()`_ is a recursive parser.
|
2015-03-14 22:55:26 +01:00
|
|
|
// For this reason, in order to avoid DoS attacks by triggering a stack
|
|
|
|
// overflow, there is a recursion limit. This limit is a sanity check to make
|
|
|
|
// sure it doesn't run the risk of busting the stack.
|
2013-08-17 04:00:41 +02:00
|
|
|
//
|
2015-03-14 22:55:26 +01:00
|
|
|
// The second mechanism is the decode function for 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
|
|
|
//
|
2015-03-14 22:55:26 +01:00
|
|
|
// Not only is this function more efficient because of less memory allocation
|
|
|
|
// and data copy, the parser is also not recursive, which means it probably
|
|
|
|
// performs a little bit better and can have a higher recursion limit on the
|
|
|
|
// structures it's parsing.
|
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
|
|
|
|
|
|
|
namespace libtorrent
|
|
|
|
{
|
|
|
|
|
2014-11-29 10:12:44 +01:00
|
|
|
#ifndef TORRENT_NO_DEPRECATE
|
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
|
|
|
|
|
|
|
namespace detail
|
|
|
|
{
|
|
|
|
template <class OutIt>
|
2007-12-22 09:15:05 +01:00
|
|
|
int write_integer(OutIt& out, entry::integer_type val)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
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");
|
2005-08-12 14:40:58 +02:00
|
|
|
char buf[21];
|
2007-12-22 09:15:05 +01:00
|
|
|
int ret = 0;
|
2005-08-12 14:40:58 +02:00
|
|
|
for (char const* str = integer_to_str(buf, 21, val);
|
|
|
|
*str != 0; ++str)
|
|
|
|
{
|
|
|
|
*out = *str;
|
|
|
|
++out;
|
2007-12-22 09:15:05 +01:00
|
|
|
++ret;
|
2005-08-12 14:40:58 +02:00
|
|
|
}
|
2007-12-22 09:15:05 +01:00
|
|
|
return ret;
|
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');
|
2003-10-23 01:00:57 +02:00
|
|
|
for (entry::list_type::const_iterator i = e.list().begin(); i != e.list().end(); ++i)
|
2007-12-22 09:15:05 +01:00
|
|
|
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');
|
2004-01-26 11:29:00 +01:00
|
|
|
for (entry::dictionary_type::const_iterator i = e.dict().begin();
|
2005-05-25 12:01:01 +02:00
|
|
|
i != e.dict().end(); ++i)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
|
|
|
// write key
|
2007-12-22 09:15:05 +01:00
|
|
|
ret += write_integer(out, i->first.length());
|
2005-05-25 12:01:01 +02:00
|
|
|
write_char(out, ':');
|
2014-05-04 08:46:47 +02:00
|
|
|
ret += write_string(i->first, out);
|
2003-10-23 01:00:57 +02:00
|
|
|
// write value
|
2007-12-22 09:15:05 +01:00
|
|
|
ret += bencode_recursive(out, i->second);
|
|
|
|
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);
|
2016-05-07 07:34:54 +02:00
|
|
|
ret += 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
|
|
|
}
|
|
|
|
|
|
|
|
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'
|
2007-11-02 02:08:10 +01:00
|
|
|
std::string val = read_until(in, end, 'e', err);
|
|
|
|
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;
|
|
|
|
ret.integer() = 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;
|
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
} break;
|
|
|
|
|
|
|
|
// ----------------------------------------------
|
|
|
|
// list
|
|
|
|
case 'l':
|
|
|
|
{
|
|
|
|
ret = entry(entry::list_t);
|
|
|
|
++in; // 'l'
|
|
|
|
while (*in != 'e')
|
|
|
|
{
|
2004-03-05 13:04:47 +01:00
|
|
|
ret.list().push_back(entry());
|
|
|
|
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'
|
|
|
|
} break;
|
|
|
|
|
|
|
|
// ----------------------------------------------
|
|
|
|
// 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'
|
|
|
|
} break;
|
|
|
|
|
|
|
|
// ----------------------------------------------
|
|
|
|
// string
|
|
|
|
default:
|
2016-06-18 20:01:38 +02:00
|
|
|
if (is_digit(std::uint8_t(*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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-03 07:12:54 +02:00
|
|
|
|
2015-03-12 06:20:12 +01:00
|
|
|
// These functions will encode data to bencoded or decode bencoded data.
|
2013-08-08 03:03:54 +02:00
|
|
|
//
|
2015-03-13 06:42:18 +01:00
|
|
|
// If possible, ``bdecode()`` producing a bdecode_node should be preferred
|
|
|
|
// over this function.
|
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.
|
|
|
|
//
|
|
|
|
// The ``OutIt`` and ``InIt`` are iterators
|
|
|
|
// (InputIterator_ and OutputIterator_ respectively). They
|
|
|
|
// are templates and are usually instantiated as ostream_iterator_,
|
|
|
|
// back_insert_iterator_ or istream_iterator_. These
|
|
|
|
// functions will assume that the iterator refers to a character
|
|
|
|
// (``char``). So, if you want to encode entry ``e`` into a buffer
|
|
|
|
// in memory, you can do it like this::
|
|
|
|
//
|
|
|
|
// std::vector<char> buffer;
|
|
|
|
// bencode(std::back_inserter(buf), e);
|
|
|
|
//
|
|
|
|
// .. _InputIterator: http://www.sgi.com/tech/stl/InputIterator.html
|
|
|
|
// .. _OutputIterator: http://www.sgi.com/tech/stl/OutputIterator.html
|
|
|
|
// .. _ostream_iterator: http://www.sgi.com/tech/stl/ostream_iterator.html
|
|
|
|
// .. _back_insert_iterator: http://www.sgi.com/tech/stl/back_insert_iterator.html
|
|
|
|
// .. _istream_iterator: http://www.sgi.com/tech/stl/istream_iterator.html
|
|
|
|
//
|
|
|
|
// If you want to decode a torrent file from a buffer in memory, you can do it like this::
|
|
|
|
//
|
|
|
|
// std::vector<char> buffer;
|
|
|
|
// // ...
|
|
|
|
// entry e = bdecode(buf.begin(), buf.end());
|
|
|
|
//
|
|
|
|
// Or, if you have a raw char buffer::
|
|
|
|
//
|
|
|
|
// const char* buf;
|
2013-08-11 18:57:33 +02:00
|
|
|
// // ...
|
2013-08-08 03:03:54 +02:00
|
|
|
// entry e = bdecode(buf, buf + data_size);
|
|
|
|
//
|
|
|
|
// Now we just need to know how to retrieve information from the entry.
|
|
|
|
//
|
|
|
|
// If ``bdecode()`` encounters invalid encoded data in the range given to it
|
2014-11-29 10:12:44 +01:00
|
|
|
// it will return a default constructed ``entry`` object.
|
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
|
|
|
}
|
2013-08-12 18:24:37 +02:00
|
|
|
template<class InIt> 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
|
|
|
}
|
2013-08-12 18:24:37 +02:00
|
|
|
template<class InIt> entry bdecode(InIt start, InIt end, int& 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;
|
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // TORRENT_BENCODE_HPP_INCLUDED
|