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_ENTRY_HPP_INCLUDED
|
|
|
|
#define TORRENT_ENTRY_HPP_INCLUDED
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* This file declares the entry class. It is a
|
|
|
|
* variant-type that can be an integer, list,
|
|
|
|
* dictionary (map) or a string. This type is
|
|
|
|
* used to hold bdecoded data (which is the
|
|
|
|
* encoding BitTorrent messages uses).
|
|
|
|
*
|
|
|
|
* it has 4 accessors to access the actual
|
|
|
|
* type of the object. They are:
|
|
|
|
* integer()
|
|
|
|
* string()
|
|
|
|
* list()
|
|
|
|
* dict()
|
|
|
|
* The actual type has to match the type you
|
|
|
|
* are asking for, otherwise you will get an
|
|
|
|
* assertion failure.
|
|
|
|
* When you default construct an entry, it is
|
|
|
|
* uninitialized. You can initialize it through the
|
|
|
|
* assignment operator, copy-constructor or
|
|
|
|
* the constructor that takes a data_type enum.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2015-04-19 08:28:21 +02:00
|
|
|
#include "libtorrent/config.hpp"
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
#include <map>
|
2004-03-27 23:02:31 +01:00
|
|
|
#include <list>
|
2003-10-23 01:00:57 +02:00
|
|
|
#include <string>
|
2017-03-27 20:40:19 +02:00
|
|
|
#include <vector>
|
2003-10-23 01:00:57 +02:00
|
|
|
#include <stdexcept>
|
2016-06-18 20:01:38 +02:00
|
|
|
#include <cstdint>
|
2015-04-19 08:28:21 +02:00
|
|
|
#if TORRENT_USE_IOSTREAM
|
|
|
|
#include <iosfwd>
|
|
|
|
#endif
|
|
|
|
|
2007-09-01 06:08:39 +02:00
|
|
|
#include "libtorrent/assert.hpp"
|
2009-02-23 02:21:19 +01:00
|
|
|
#include "libtorrent/error_code.hpp"
|
2016-07-22 18:31:42 +02:00
|
|
|
#include "libtorrent/span.hpp"
|
2016-09-24 15:10:54 +02:00
|
|
|
#include "libtorrent/string_view.hpp"
|
2017-01-21 00:27:20 +01:00
|
|
|
#include "libtorrent/aux_/aligned_union.hpp"
|
2004-01-18 02:58:33 +01:00
|
|
|
|
2017-04-12 19:00:57 +02:00
|
|
|
namespace libtorrent {
|
|
|
|
|
2018-04-26 09:01:14 +02:00
|
|
|
#if TORRENT_ABI_VERSION == 1
|
2010-03-04 17:42:39 +01:00
|
|
|
struct lazy_entry;
|
2016-05-15 06:33:06 +02:00
|
|
|
// backwards compatibility
|
|
|
|
using type_error = system_error;
|
2015-03-12 06:20:12 +01:00
|
|
|
#endif
|
|
|
|
struct bdecode_node;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2017-04-12 19:00:57 +02:00
|
|
|
namespace aux {
|
|
|
|
|
2017-01-20 00:08:39 +01:00
|
|
|
#if (__cplusplus > 201103) || (defined _MSC_VER && _MSC_VER >= 1900)
|
2016-09-24 15:10:54 +02:00
|
|
|
// this enables us to compare a string_view against the std::string that's
|
|
|
|
// held by the std::map
|
2019-01-17 13:14:33 +01:00
|
|
|
// is_transparent was introduced in C++14
|
2016-09-24 15:10:54 +02:00
|
|
|
struct strview_less
|
|
|
|
{
|
|
|
|
using is_transparent = std::true_type;
|
|
|
|
template <typename T1, typename T2>
|
|
|
|
bool operator()(T1 const& rhs, T2 const& lhs) const
|
|
|
|
{ return rhs < lhs; }
|
|
|
|
};
|
2017-01-20 00:08:39 +01:00
|
|
|
|
|
|
|
template<class T> using map_string = std::map<std::string, T, aux::strview_less>;
|
|
|
|
#else
|
|
|
|
template<class T>
|
|
|
|
struct map_string : std::map<std::string, T>
|
|
|
|
{
|
|
|
|
using base = std::map<std::string, T>;
|
|
|
|
|
|
|
|
typename base::iterator find(const string_view& key)
|
|
|
|
{
|
|
|
|
return this->base::find(key.to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
typename base::const_iterator find(const string_view& key) const
|
|
|
|
{
|
|
|
|
return this->base::find(key.to_string());
|
|
|
|
}
|
|
|
|
};
|
2016-09-24 15:10:54 +02:00
|
|
|
#endif
|
2017-01-20 00:08:39 +01:00
|
|
|
}
|
2016-09-24 15:10:54 +02:00
|
|
|
|
2013-07-26 08:11:10 +02:00
|
|
|
// The ``entry`` class represents one node in a bencoded hierarchy. It works as a
|
|
|
|
// variant type, it can be either a list, a dictionary (``std::map``), an integer
|
|
|
|
// or a string.
|
2005-11-01 19:30:39 +01:00
|
|
|
class TORRENT_EXPORT entry
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2005-08-11 13:06:52 +02:00
|
|
|
// the key is always a string. If a generic entry would be allowed
|
|
|
|
// as a key, sorting would become a problem (e.g. to compare a string
|
|
|
|
// to a list). The definition doesn't mention such a limit though.
|
2017-01-20 00:08:39 +01:00
|
|
|
using dictionary_type = aux::map_string<entry>;
|
2016-10-08 20:07:11 +02:00
|
|
|
using string_type = std::string;
|
|
|
|
using list_type = std::vector<entry>;
|
|
|
|
using integer_type = std::int64_t;
|
|
|
|
using preformatted_type = std::vector<char>;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2013-08-17 09:01:03 +02:00
|
|
|
// the types an entry can have
|
2003-10-23 01:00:57 +02:00
|
|
|
enum data_type
|
|
|
|
{
|
|
|
|
int_t,
|
|
|
|
string_t,
|
|
|
|
list_t,
|
|
|
|
dictionary_t,
|
2016-05-06 03:38:57 +02:00
|
|
|
undefined_t,
|
|
|
|
preformatted_t
|
2003-10-23 01:00:57 +02:00
|
|
|
};
|
|
|
|
|
2013-08-17 09:01:03 +02:00
|
|
|
// returns the concrete type of the entry
|
2004-03-28 19:45:37 +02:00
|
|
|
data_type type() const;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2013-08-17 09:01:03 +02:00
|
|
|
// constructors directly from a specific type.
|
|
|
|
// The content of the argument is copied into the
|
|
|
|
// newly constructed entry
|
2016-10-08 20:07:11 +02:00
|
|
|
entry(dictionary_type); // NOLINT
|
|
|
|
entry(span<char const>); // NOLINT
|
2016-07-27 00:30:19 +02:00
|
|
|
template <typename U, typename Cond = typename std::enable_if<
|
2016-07-22 16:29:39 +02:00
|
|
|
std::is_same<U, entry::string_type>::value
|
2016-09-24 15:10:54 +02:00
|
|
|
|| std::is_same<U, string_view>::value
|
2016-07-25 23:58:00 +02:00
|
|
|
|| std::is_same<U, char const*>::value>::type>
|
2016-10-08 20:07:11 +02:00
|
|
|
entry(U v) // NOLINT
|
2016-07-22 16:29:39 +02:00
|
|
|
: m_type(undefined_t)
|
|
|
|
{
|
|
|
|
#if TORRENT_USE_ASSERTS
|
|
|
|
m_type_queried = true;
|
|
|
|
#endif
|
|
|
|
new(&data) string_type(std::move(v));
|
|
|
|
m_type = string_t;
|
|
|
|
}
|
2016-10-08 20:07:11 +02:00
|
|
|
entry(list_type); // NOLINT
|
|
|
|
entry(integer_type); // NOLINT
|
|
|
|
entry(preformatted_type); // NOLINT
|
2004-01-07 01:48:02 +01:00
|
|
|
|
2013-08-17 09:01:03 +02:00
|
|
|
// construct an empty entry of the specified type.
|
|
|
|
// see data_type enum.
|
2016-10-08 20:07:11 +02:00
|
|
|
entry(data_type t); // NOLINT
|
2013-08-17 09:01:03 +02:00
|
|
|
|
|
|
|
// hidden
|
2006-11-14 01:08:16 +01:00
|
|
|
entry(entry const& e);
|
2017-05-28 13:14:58 +02:00
|
|
|
entry(entry&& e) noexcept;
|
2013-11-27 21:57:19 +01:00
|
|
|
|
2018-11-18 12:59:52 +01:00
|
|
|
// construct from bdecode_node parsed form (see bdecode())
|
|
|
|
entry(bdecode_node const& n); // NOLINT
|
|
|
|
|
2013-11-27 21:57:19 +01:00
|
|
|
// hidden
|
2013-08-17 09:01:03 +02:00
|
|
|
entry();
|
2013-11-27 21:57:19 +01:00
|
|
|
|
|
|
|
// hidden
|
2004-03-28 19:45:37 +02:00
|
|
|
~entry();
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2013-11-27 21:57:19 +01:00
|
|
|
// copies the structure of the right hand side into this
|
|
|
|
// entry.
|
2018-04-26 09:01:14 +02:00
|
|
|
#if TORRENT_ABI_VERSION == 1
|
2018-06-30 18:31:45 +02:00
|
|
|
entry& operator=(lazy_entry const&) &;
|
2015-03-12 06:20:12 +01:00
|
|
|
#endif
|
2018-06-30 18:31:45 +02:00
|
|
|
entry& operator=(bdecode_node const&) &;
|
|
|
|
entry& operator=(entry const&) &;
|
|
|
|
entry& operator=(entry&&) & noexcept;
|
|
|
|
entry& operator=(dictionary_type) &;
|
|
|
|
entry& operator=(span<char const>) &;
|
2016-07-27 00:30:19 +02:00
|
|
|
template <typename U, typename Cond = typename std::enable_if<
|
2016-07-22 16:29:39 +02:00
|
|
|
std::is_same<U, entry::string_type>::value
|
2016-07-25 23:58:00 +02:00
|
|
|
|| std::is_same<U, char const*>::value>::type>
|
2018-06-30 18:31:45 +02:00
|
|
|
entry& operator=(U v) &
|
2016-07-22 16:29:39 +02:00
|
|
|
{
|
|
|
|
destruct();
|
|
|
|
new(&data) string_type(std::move(v));
|
|
|
|
m_type = string_t;
|
|
|
|
#if TORRENT_USE_ASSERTS
|
|
|
|
m_type_queried = true;
|
|
|
|
#endif
|
|
|
|
return *this;
|
|
|
|
}
|
2018-06-30 18:31:45 +02:00
|
|
|
entry& operator=(list_type) &;
|
|
|
|
entry& operator=(integer_type) &;
|
|
|
|
entry& operator=(preformatted_type) &;
|
2004-01-07 01:48:02 +01:00
|
|
|
|
2013-07-26 08:11:10 +02:00
|
|
|
// The ``integer()``, ``string()``, ``list()`` and ``dict()`` functions
|
2014-02-28 05:02:48 +01:00
|
|
|
// are accessors that return the respective type. If the ``entry`` object
|
|
|
|
// isn't of the type you request, the accessor will throw
|
2016-05-15 06:33:06 +02:00
|
|
|
// system_error. You can ask an ``entry`` for its type through the
|
|
|
|
// ``type()`` function.
|
2016-10-22 17:47:24 +02:00
|
|
|
//
|
2014-02-28 05:02:48 +01:00
|
|
|
// If you want to create an ``entry`` you give it the type you want it to
|
|
|
|
// have in its constructor, and then use one of the non-const accessors
|
|
|
|
// to get a reference which you then can assign the value you want it to
|
|
|
|
// have.
|
2016-10-22 17:47:24 +02:00
|
|
|
//
|
2014-02-28 05:02:48 +01:00
|
|
|
// The typical code to get info from a torrent file will then look like
|
2014-09-03 08:52:45 +02:00
|
|
|
// this:
|
|
|
|
//
|
|
|
|
// .. code:: c++
|
2016-10-22 17:47:24 +02:00
|
|
|
//
|
2013-07-26 08:11:10 +02:00
|
|
|
// entry torrent_file;
|
|
|
|
// // ...
|
2016-10-22 17:47:24 +02:00
|
|
|
//
|
2013-07-26 08:11:10 +02:00
|
|
|
// // throws if this is not a dictionary
|
|
|
|
// entry::dictionary_type const& dict = torrent_file.dict();
|
|
|
|
// entry::dictionary_type::const_iterator i;
|
|
|
|
// i = dict.find("announce");
|
|
|
|
// if (i != dict.end())
|
|
|
|
// {
|
|
|
|
// std::string tracker_url = i->second.string();
|
|
|
|
// std::cout << tracker_url << "\n";
|
|
|
|
// }
|
2016-10-22 17:47:24 +02:00
|
|
|
//
|
|
|
|
//
|
2014-09-03 08:52:45 +02:00
|
|
|
// The following code is equivalent, but a little bit shorter:
|
|
|
|
//
|
|
|
|
// .. code:: c++
|
2016-10-22 17:47:24 +02:00
|
|
|
//
|
2013-07-26 08:11:10 +02:00
|
|
|
// entry torrent_file;
|
|
|
|
// // ...
|
2016-10-22 17:47:24 +02:00
|
|
|
//
|
2013-07-26 08:11:10 +02:00
|
|
|
// // throws if this is not a dictionary
|
|
|
|
// if (entry* i = torrent_file.find_key("announce"))
|
|
|
|
// {
|
|
|
|
// std::string tracker_url = i->string();
|
|
|
|
// std::cout << tracker_url << "\n";
|
|
|
|
// }
|
2016-10-22 17:47:24 +02:00
|
|
|
//
|
|
|
|
//
|
2014-02-28 05:02:48 +01:00
|
|
|
// To make it easier to extract information from a torrent file, the
|
|
|
|
// class torrent_info exists.
|
2004-03-28 19:45:37 +02:00
|
|
|
integer_type& integer();
|
|
|
|
const integer_type& integer() const;
|
|
|
|
string_type& string();
|
|
|
|
const string_type& string() const;
|
|
|
|
list_type& list();
|
|
|
|
const list_type& list() const;
|
|
|
|
dictionary_type& dict();
|
|
|
|
const dictionary_type& dict() const;
|
2016-05-06 03:38:57 +02:00
|
|
|
preformatted_type& preformatted();
|
|
|
|
const preformatted_type& preformatted() const;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2013-08-17 09:01:03 +02:00
|
|
|
// swaps the content of *this* with ``e``.
|
2018-01-04 21:02:34 +01:00
|
|
|
void swap(entry& e);
|
2007-06-10 22:46:09 +02:00
|
|
|
|
2014-02-28 05:02:48 +01:00
|
|
|
// All of these functions requires the entry to be a dictionary, if it
|
2016-05-15 06:33:06 +02:00
|
|
|
// isn't they will throw ``system_error``.
|
2013-07-26 08:11:10 +02:00
|
|
|
//
|
2014-02-28 05:02:48 +01:00
|
|
|
// The non-const versions of the ``operator[]`` will return a reference
|
|
|
|
// to either the existing element at the given key or, if there is no
|
|
|
|
// element with the given key, a reference to a newly inserted element at
|
|
|
|
// that key.
|
2013-07-26 08:11:10 +02:00
|
|
|
//
|
|
|
|
// The const version of ``operator[]`` will only return a reference to an
|
2014-02-28 05:02:48 +01:00
|
|
|
// existing element at the given key. If the key is not found, it will
|
2016-05-15 06:33:06 +02:00
|
|
|
// throw ``system_error``.
|
2016-09-24 15:10:54 +02:00
|
|
|
entry& operator[](string_view key);
|
|
|
|
const entry& operator[](string_view key) const;
|
2013-07-26 08:11:10 +02:00
|
|
|
|
2014-02-28 05:02:48 +01:00
|
|
|
// These functions requires the entry to be a dictionary, if it isn't
|
2016-05-15 06:33:06 +02:00
|
|
|
// they will throw ``system_error``.
|
2013-07-26 08:11:10 +02:00
|
|
|
//
|
2014-02-28 05:02:48 +01:00
|
|
|
// They will look for an element at the given key in the dictionary, if
|
2016-09-24 15:10:54 +02:00
|
|
|
// the element cannot be found, they will return nullptr. If an element
|
|
|
|
// with the given key is found, the return a pointer to it.
|
|
|
|
entry* find_key(string_view key);
|
|
|
|
entry const* find_key(string_view key) const;
|
2013-07-26 08:11:10 +02:00
|
|
|
|
2014-02-02 04:05:55 +01:00
|
|
|
// returns a pretty-printed string representation
|
|
|
|
// of the bencoded structure, with JSON-style syntax
|
2018-09-04 14:31:05 +02:00
|
|
|
std::string to_string(bool single_line = false) const;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2008-04-07 05:03:34 +02:00
|
|
|
protected:
|
2003-10-23 01:00:57 +02:00
|
|
|
|
|
|
|
void construct(data_type t);
|
|
|
|
void copy(const entry& e);
|
|
|
|
void destruct();
|
|
|
|
|
2008-04-07 05:03:34 +02:00
|
|
|
private:
|
|
|
|
|
2017-01-21 00:27:20 +01:00
|
|
|
aux::aligned_union<1
|
2016-02-21 05:40:45 +01:00
|
|
|
#if TORRENT_COMPLETE_TYPES_REQUIRED
|
2016-05-22 03:04:47 +02:00
|
|
|
// for implementations that require complete types, use char and hope
|
|
|
|
// for the best
|
|
|
|
, std::list<char>
|
|
|
|
, std::map<std::string, char>
|
2003-10-23 01:00:57 +02:00
|
|
|
#else
|
2016-05-22 03:04:47 +02:00
|
|
|
, list_type
|
|
|
|
, dictionary_type
|
2008-01-03 04:10:25 +01:00
|
|
|
#endif
|
2016-05-22 03:04:47 +02:00
|
|
|
, preformatted_type
|
|
|
|
, string_type
|
|
|
|
, integer_type
|
|
|
|
>::type data;
|
2013-11-17 21:37:55 +01:00
|
|
|
|
2014-02-28 05:02:48 +01:00
|
|
|
// the bitfield is used so that the m_type_queried field still fits, so
|
|
|
|
// that the ABI is the same for debug builds and release builds. It
|
|
|
|
// appears to be very hard to match debug builds with debug versions of
|
|
|
|
// libtorrent
|
2016-06-18 20:01:38 +02:00
|
|
|
std::uint8_t m_type:7;
|
2013-11-17 21:37:55 +01:00
|
|
|
|
|
|
|
public:
|
2014-02-28 05:02:48 +01:00
|
|
|
// in debug mode this is set to false by bdecode to indicate that the
|
2016-02-19 16:50:17 +01:00
|
|
|
// program has not yet queried the type of this entry, and should not
|
2014-02-28 05:02:48 +01:00
|
|
|
// assume that it has a certain type. This is asserted in the accessor
|
|
|
|
// functions. This does not apply if exceptions are used.
|
2016-06-18 20:01:38 +02:00
|
|
|
mutable std::uint8_t m_type_queried:1;
|
2003-10-23 01:00:57 +02:00
|
|
|
};
|
|
|
|
|
2018-11-18 12:59:52 +01:00
|
|
|
TORRENT_EXPORT bool operator==(entry const& lhs, entry const& rhs);
|
|
|
|
inline bool operator!=(entry const& lhs, entry const& rhs) { return !(lhs == rhs); }
|
|
|
|
|
2017-04-12 19:00:57 +02:00
|
|
|
namespace detail {
|
|
|
|
|
2018-11-18 12:59:52 +01:00
|
|
|
// internal
|
|
|
|
TORRENT_EXPORT string_view integer_to_str(span<char> buf
|
2018-11-16 02:39:31 +01:00
|
|
|
, entry::integer_type val);
|
|
|
|
}
|
2015-04-20 02:01:27 +02:00
|
|
|
|
2013-12-19 09:30:17 +01:00
|
|
|
#if TORRENT_USE_IOSTREAM
|
2014-02-02 04:05:55 +01:00
|
|
|
// prints the bencoded structure to the ostream as a JSON-style structure.
|
2003-10-23 01:00:57 +02:00
|
|
|
inline std::ostream& operator<<(std::ostream& os, const entry& e)
|
|
|
|
{
|
2013-12-19 09:30:17 +01:00
|
|
|
os << e.to_string();
|
2003-10-23 01:00:57 +02:00
|
|
|
return os;
|
|
|
|
}
|
2009-04-04 23:44:04 +02:00
|
|
|
#endif
|
2003-10-23 01:00:57 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // TORRENT_ENTRY_HPP_INCLUDED
|