premiere-libtorrent/src/entry.cpp

740 lines
16 KiB
C++
Raw Normal View History

/*
Copyright (c) 2003-2016, Arvid Norberg
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.
*/
2015-04-20 02:01:27 +02:00
#include "libtorrent/config.hpp"
2013-12-19 09:30:17 +01:00
#if TORRENT_USE_IOSTREAM
2009-04-04 11:52:25 +02:00
#include <iostream>
#endif
2015-04-20 02:01:27 +02:00
#ifndef TORRENT_NO_DEPRECATE
#include "libtorrent/lazy_entry.hpp"
#endif
#include "libtorrent/bdecode.hpp"
2015-04-20 02:01:27 +02:00
#include "libtorrent/entry.hpp"
#include "libtorrent/hex.hpp"
#include "libtorrent/string_util.hpp"
2004-01-07 01:48:02 +01:00
namespace libtorrent
{
2005-08-12 14:40:58 +02:00
namespace detail
{
char const* integer_to_str(char* buf, int size
2015-04-20 02:01:27 +02:00
, entry::integer_type val)
2005-08-12 14:40:58 +02:00
{
int sign = 0;
if (val < 0)
{
sign = 1;
val = -val;
}
buf[--size] = '\0';
if (val == 0) buf[--size] = '0';
for (; size > sign && val != 0;)
{
buf[--size] = '0' + char(val % 10);
val /= 10;
}
if (sign) buf[--size] = '-';
return buf + size;
}
}
namespace
{
TORRENT_NO_RETURN inline void throw_error()
{
#ifndef BOOST_NO_EXCEPTIONS
throw system_error(error_code(errors::invalid_entry_type
, get_libtorrent_category()));
#else
std::terminate();
#endif
}
template <class T>
void call_destructor(T* o)
{
TORRENT_ASSERT(o);
o->~T();
}
}
entry& entry::operator[](string_view key)
2004-03-17 13:14:44 +01:00
{
#if __cplusplus <= 201103
dictionary_type::iterator i = dict().find(key.to_string());
#else
2008-05-19 09:15:44 +02:00
dictionary_type::iterator i = dict().find(key);
#endif
2008-05-19 09:15:44 +02:00
if (i != dict().end()) return i->second;
dictionary_type::iterator ret = dict().insert(
std::make_pair(key.to_string(), entry())).first;
2008-05-19 09:15:44 +02:00
return ret->second;
2004-03-17 13:14:44 +01:00
}
const entry& entry::operator[](string_view key) const
2004-03-17 13:14:44 +01:00
{
#if __cplusplus <= 201103
dictionary_type::const_iterator i = dict().find(key.to_string());
#else
dictionary_type::const_iterator i = dict().find(key);
#endif
if (i == dict().end()) throw_error();
return i->second;
2004-03-17 13:14:44 +01:00
}
2015-06-03 07:12:54 +02:00
entry* entry::find_key(string_view key)
2008-05-19 09:15:44 +02:00
{
#if __cplusplus <= 201103
dictionary_type::iterator i = dict().find(key.to_string());
#else
2008-05-19 09:15:44 +02:00
dictionary_type::iterator i = dict().find(key);
#endif
2016-07-09 22:26:26 +02:00
if (i == dict().end()) return nullptr;
2008-05-19 09:15:44 +02:00
return &i->second;
}
entry const* entry::find_key(string_view key) const
2008-05-19 09:15:44 +02:00
{
#if __cplusplus <= 201103
dictionary_type::const_iterator i = dict().find(key.to_string());
#else
2008-05-19 09:15:44 +02:00
dictionary_type::const_iterator i = dict().find(key);
#endif
2016-07-09 22:26:26 +02:00
if (i == dict().end()) return nullptr;
2008-05-19 09:15:44 +02:00
return &i->second;
}
2015-06-03 07:12:54 +02:00
entry::data_type entry::type() const
{
#if TORRENT_USE_ASSERTS
m_type_queried = true;
#endif
2015-08-02 05:57:11 +02:00
return entry::data_type(m_type);
}
entry::~entry() { destruct(); }
entry& entry::operator=(const entry& e)
{
2016-05-06 07:08:05 +02:00
if (&e == this) return *this;
destruct();
copy(e);
return *this;
}
2016-05-01 05:44:48 +02:00
entry& entry::operator=(entry&& e)
{
swap(e);
return *this;
}
entry::integer_type& entry::integer()
{
if (m_type == undefined_t) construct(int_t);
#ifdef BOOST_NO_EXCEPTIONS
TORRENT_ASSERT(m_type_queried);
#endif
if (m_type != int_t) throw_error();
TORRENT_ASSERT(m_type == int_t);
return *reinterpret_cast<integer_type*>(&data);
}
entry::integer_type const& entry::integer() const
{
if (m_type != int_t) throw_error();
#ifdef BOOST_NO_EXCEPTIONS
TORRENT_ASSERT(m_type_queried);
#endif
TORRENT_ASSERT(m_type == int_t);
return *reinterpret_cast<const integer_type*>(&data);
}
entry::string_type& entry::string()
{
if (m_type == undefined_t) construct(string_t);
#ifdef BOOST_NO_EXCEPTIONS
TORRENT_ASSERT(m_type_queried);
#endif
if (m_type != string_t) throw_error();
TORRENT_ASSERT(m_type == string_t);
return *reinterpret_cast<string_type*>(&data);
}
entry::string_type const& entry::string() const
{
if (m_type != string_t) throw_error();
#ifdef BOOST_NO_EXCEPTIONS
TORRENT_ASSERT(m_type_queried);
#endif
TORRENT_ASSERT(m_type == string_t);
return *reinterpret_cast<const string_type*>(&data);
}
entry::list_type& entry::list()
{
if (m_type == undefined_t) construct(list_t);
#ifdef BOOST_NO_EXCEPTIONS
TORRENT_ASSERT(m_type_queried);
#endif
if (m_type != list_t) throw_error();
TORRENT_ASSERT(m_type == list_t);
return *reinterpret_cast<list_type*>(&data);
}
entry::list_type const& entry::list() const
{
if (m_type != list_t) throw_error();
#ifdef BOOST_NO_EXCEPTIONS
TORRENT_ASSERT(m_type_queried);
#endif
TORRENT_ASSERT(m_type == list_t);
return *reinterpret_cast<const list_type*>(&data);
}
entry::dictionary_type& entry::dict()
{
if (m_type == undefined_t) construct(dictionary_t);
#ifdef BOOST_NO_EXCEPTIONS
TORRENT_ASSERT(m_type_queried);
#endif
if (m_type != dictionary_t) throw_error();
TORRENT_ASSERT(m_type == dictionary_t);
return *reinterpret_cast<dictionary_type*>(&data);
}
entry::dictionary_type const& entry::dict() const
{
if (m_type != dictionary_t) throw_error();
#ifdef BOOST_NO_EXCEPTIONS
TORRENT_ASSERT(m_type_queried);
#endif
TORRENT_ASSERT(m_type == dictionary_t);
return *reinterpret_cast<const dictionary_type*>(&data);
}
entry::preformatted_type& entry::preformatted()
{
if (m_type == undefined_t) construct(preformatted_t);
#ifdef BOOST_NO_EXCEPTIONS
TORRENT_ASSERT(m_type_queried);
#endif
if (m_type != preformatted_t) throw_error();
TORRENT_ASSERT(m_type == preformatted_t);
return *reinterpret_cast<preformatted_type*>(&data);
}
entry::preformatted_type const& entry::preformatted() const
{
if (m_type != preformatted_t) throw_error();
#ifdef BOOST_NO_EXCEPTIONS
TORRENT_ASSERT(m_type_queried);
#endif
TORRENT_ASSERT(m_type == preformatted_t);
return *reinterpret_cast<const preformatted_type*>(&data);
}
2008-02-22 05:54:43 +01:00
entry::entry()
: m_type(undefined_t)
{
#if TORRENT_USE_ASSERTS
m_type_queried = true;
#endif
}
2008-02-22 05:54:43 +01:00
entry::entry(data_type t)
: m_type(undefined_t)
{
construct(t);
#if TORRENT_USE_ASSERTS
m_type_queried = true;
#endif
}
entry::entry(const entry& e)
2008-02-22 05:54:43 +01:00
: m_type(undefined_t)
{
copy(e);
#if TORRENT_USE_ASSERTS
m_type_queried = e.m_type_queried;
#endif
}
2016-05-01 05:44:48 +02:00
entry::entry(entry&& e)
: m_type(undefined_t)
{
#if TORRENT_USE_ASSERTS
2016-05-01 05:44:48 +02:00
uint8_t type_queried = e.m_type_queried;
#endif
swap(e);
#if TORRENT_USE_ASSERTS
2016-05-01 05:44:48 +02:00
m_type_queried = type_queried;
#endif
}
entry::entry(dictionary_type v)
2008-02-22 05:54:43 +01:00
: m_type(undefined_t)
{
#if TORRENT_USE_ASSERTS
m_type_queried = true;
#endif
new(&data) dictionary_type(std::move(v));
2004-01-07 01:48:02 +01:00
m_type = dictionary_t;
}
entry::entry(span<char const> v)
2008-02-22 05:54:43 +01:00
: m_type(undefined_t)
{
#if TORRENT_USE_ASSERTS
m_type_queried = true;
#endif
new(&data) string_type(v.data(), v.size());
2004-01-07 01:48:02 +01:00
m_type = string_t;
}
entry::entry(list_type v)
2008-02-22 05:54:43 +01:00
: m_type(undefined_t)
{
#if TORRENT_USE_ASSERTS
m_type_queried = true;
#endif
new(&data) list_type(std::move(v));
2004-01-07 01:48:02 +01:00
m_type = list_t;
}
entry::entry(integer_type v)
2008-02-22 05:54:43 +01:00
: m_type(undefined_t)
2004-01-07 01:48:02 +01:00
{
#if TORRENT_USE_ASSERTS
m_type_queried = true;
#endif
new(&data) integer_type(std::move(v));
2004-01-07 01:48:02 +01:00
m_type = int_t;
}
entry::entry(preformatted_type v)
: m_type(undefined_t)
{
#if TORRENT_USE_ASSERTS
m_type_queried = true;
#endif
new(&data) preformatted_type(std::move(v));
m_type = preformatted_t;
}
// convert a bdecode_node into an old skool entry
entry& entry::operator=(bdecode_node const& e)
{
switch (e.type())
{
case bdecode_node::string_t:
this->string() = e.string_value().to_string();
break;
case bdecode_node::int_t:
this->integer() = e.int_value();
break;
case bdecode_node::dict_t:
{
dictionary_type& d = this->dict();
for (int i = 0; i < e.dict_size(); ++i)
{
std::pair<string_view, bdecode_node> elem = e.dict_at(i);
d[elem.first.to_string()] = elem.second;
}
break;
}
case bdecode_node::list_t:
{
list_type& l = this->list();
for (int i = 0; i < e.list_size(); ++i)
{
l.push_back(entry());
l.back() = e.list_at(i);
}
break;
}
case bdecode_node::none_t:
destruct();
break;
}
return *this;
}
#ifndef TORRENT_NO_DEPRECATE
// convert a lazy_entry into an old skool entry
entry& entry::operator=(lazy_entry const& e)
{
switch (e.type())
{
case lazy_entry::string_t:
this->string() = e.string_value();
break;
case lazy_entry::int_t:
this->integer() = e.int_value();
break;
case lazy_entry::dict_t:
{
dictionary_type& d = this->dict();
for (int i = 0; i < e.dict_size(); ++i)
{
std::pair<std::string, lazy_entry const*> elem = e.dict_at(i);
d[elem.first] = *elem.second;
}
break;
}
case lazy_entry::list_t:
{
list_type& l = this->list();
for (int i = 0; i < e.list_size(); ++i)
{
l.push_back(entry());
l.back() = *e.list_at(i);
}
break;
}
2011-02-14 02:59:01 +01:00
case lazy_entry::none_t:
destruct();
break;
}
return *this;
}
#endif
entry& entry::operator=(preformatted_type v)
{
destruct();
new(&data) preformatted_type(std::move(v));
m_type = preformatted_t;
#if TORRENT_USE_ASSERTS
m_type_queried = true;
#endif
2016-05-06 07:08:05 +02:00
return *this;
}
entry& entry::operator=(dictionary_type v)
2004-01-07 01:48:02 +01:00
{
destruct();
new(&data) dictionary_type(std::move(v));
2004-01-07 01:48:02 +01:00
m_type = dictionary_t;
#if TORRENT_USE_ASSERTS
m_type_queried = true;
#endif
return *this;
2004-01-07 01:48:02 +01:00
}
entry& entry::operator=(span<char const> v)
2004-01-07 01:48:02 +01:00
{
destruct();
new(&data) string_type(v.data(), v.size());
2004-01-07 01:48:02 +01:00
m_type = string_t;
#if TORRENT_USE_ASSERTS
m_type_queried = true;
#endif
return *this;
2004-01-07 01:48:02 +01:00
}
entry& entry::operator=(list_type v)
2004-01-07 01:48:02 +01:00
{
destruct();
new(&data) list_type(std::move(v));
2004-01-07 01:48:02 +01:00
m_type = list_t;
#if TORRENT_USE_ASSERTS
m_type_queried = true;
#endif
return *this;
2004-01-07 01:48:02 +01:00
}
entry& entry::operator=(integer_type v)
2004-01-07 01:48:02 +01:00
{
destruct();
new(&data) integer_type(std::move(v));
2004-01-07 01:48:02 +01:00
m_type = int_t;
#if TORRENT_USE_ASSERTS
m_type_queried = true;
#endif
return *this;
2004-01-07 01:48:02 +01:00
}
2005-08-11 13:06:52 +02:00
bool entry::operator==(entry const& e) const
{
if (type() != e.type()) return false;
2005-08-11 13:06:52 +02:00
2015-08-11 02:03:24 +02:00
switch (m_type)
2005-08-11 13:06:52 +02:00
{
case int_t:
return integer() == e.integer();
case string_t:
return string() == e.string();
case list_t:
return list() == e.list();
case dictionary_t:
return dict() == e.dict();
case preformatted_t:
return preformatted() == e.preformatted();
2005-08-11 13:06:52 +02:00
default:
TORRENT_ASSERT(m_type == undefined_t);
2005-08-11 13:06:52 +02:00
return true;
}
}
2004-01-07 01:48:02 +01:00
void entry::construct(data_type t)
{
2015-08-11 02:03:24 +02:00
switch (t)
2004-01-07 01:48:02 +01:00
{
case int_t:
new (&data) integer_type;
2004-01-07 01:48:02 +01:00
break;
case string_t:
new (&data) string_type;
2004-01-07 01:48:02 +01:00
break;
case list_t:
new (&data) list_type;
2004-01-07 01:48:02 +01:00
break;
case dictionary_t:
new (&data) dictionary_type;
2004-01-07 01:48:02 +01:00
break;
2015-08-11 02:03:24 +02:00
case undefined_t:
break;
case preformatted_t:
new (&data) preformatted_type;
break;
2004-01-07 01:48:02 +01:00
}
2008-02-22 05:54:43 +01:00
m_type = t;
#if TORRENT_USE_ASSERTS
m_type_queried = true;
#endif
2004-01-07 01:48:02 +01:00
}
void entry::copy(entry const& e)
{
2008-02-22 05:54:43 +01:00
switch (e.type())
{
2004-01-07 01:48:02 +01:00
case int_t:
new (&data) integer_type(e.integer());
2004-01-07 01:48:02 +01:00
break;
case string_t:
new (&data) string_type(e.string());
2004-01-07 01:48:02 +01:00
break;
case list_t:
new (&data) list_type(e.list());
2004-01-07 01:48:02 +01:00
break;
case dictionary_t:
new (&data) dictionary_type(e.dict());
2004-01-07 01:48:02 +01:00
break;
2015-08-11 02:03:24 +02:00
case undefined_t:
2008-02-22 05:54:43 +01:00
TORRENT_ASSERT(e.type() == undefined_t);
break;
case preformatted_t:
new (&data) preformatted_type(e.preformatted());
break;
2004-01-07 01:48:02 +01:00
}
2008-02-22 05:54:43 +01:00
m_type = e.type();
#if TORRENT_USE_ASSERTS
m_type_queried = true;
#endif
2004-01-07 01:48:02 +01:00
}
void entry::destruct()
{
switch(m_type)
{
case int_t:
call_destructor(reinterpret_cast<integer_type*>(&data));
2004-01-07 01:48:02 +01:00
break;
case string_t:
call_destructor(reinterpret_cast<string_type*>(&data));
2004-01-07 01:48:02 +01:00
break;
case list_t:
call_destructor(reinterpret_cast<list_type*>(&data));
2004-01-07 01:48:02 +01:00
break;
case dictionary_t:
call_destructor(reinterpret_cast<dictionary_type*>(&data));
2004-01-07 01:48:02 +01:00
break;
case preformatted_t:
call_destructor(reinterpret_cast<preformatted_type*>(&data));
break;
2004-01-07 01:48:02 +01:00
default:
TORRENT_ASSERT(m_type == undefined_t);
2004-01-07 01:48:02 +01:00
break;
}
2008-02-22 05:54:43 +01:00
m_type = undefined_t;
#if TORRENT_USE_ASSERTS
m_type_queried = false;
#endif
2004-01-07 01:48:02 +01:00
}
2007-06-10 22:46:09 +02:00
void entry::swap(entry& e)
{
bool clear_this = false;
bool clear_that = false;
if (m_type == undefined_t && e.m_type == undefined_t)
return;
if (m_type == undefined_t)
{
2015-08-02 05:57:11 +02:00
construct(data_type(e.m_type));
clear_that = true;
}
if (e.m_type == undefined_t)
{
2015-08-02 05:57:11 +02:00
e.construct(data_type(m_type));
clear_this = true;
}
if (m_type == e.m_type)
{
switch (m_type)
{
case int_t:
std::swap(*reinterpret_cast<integer_type*>(&data)
, *reinterpret_cast<integer_type*>(&e.data));
break;
case string_t:
std::swap(*reinterpret_cast<string_type*>(&data)
, *reinterpret_cast<string_type*>(&e.data));
break;
case list_t:
std::swap(*reinterpret_cast<list_type*>(&data)
, *reinterpret_cast<list_type*>(&e.data));
break;
case dictionary_t:
std::swap(*reinterpret_cast<dictionary_type*>(&data)
, *reinterpret_cast<dictionary_type*>(&e.data));
break;
case preformatted_t:
std::swap(*reinterpret_cast<preformatted_type*>(&data)
, *reinterpret_cast<preformatted_type*>(&e.data));
break;
default:
break;
}
if (clear_this)
destruct();
if (clear_that)
e.destruct();
}
else
{
// currently, only swapping entries of the same type or where one
// of the entries is uninitialized is supported.
TORRENT_ASSERT_FAIL();
}
2007-06-10 22:46:09 +02:00
}
2013-12-19 09:30:17 +01:00
std::string entry::to_string() const
{
std::string ret;
to_string_impl(ret, 0);
return ret;
}
void entry::to_string_impl(std::string& out, int indent) const
2004-01-07 01:48:02 +01:00
{
TORRENT_ASSERT(indent >= 0);
2013-12-19 09:30:17 +01:00
for (int i = 0; i < indent; ++i) out += " ";
switch (type())
2004-01-07 01:48:02 +01:00
{
case int_t:
2016-05-01 05:10:47 +02:00
out += libtorrent::to_string(integer()).data();
2015-08-08 08:33:54 +02:00
out += "\n";
2004-01-07 01:48:02 +01:00
break;
case string_t:
{
2004-01-07 01:48:02 +01:00
bool binary_string = false;
for (std::string::const_iterator i = string().begin(); i != string().end(); ++i)
{
if (!is_print(static_cast<unsigned char>(*i)))
2004-01-07 01:48:02 +01:00
{
binary_string = true;
break;
}
}
2013-12-19 09:30:17 +01:00
if (binary_string)
{
out += aux::to_hex(string());
2013-12-19 09:30:17 +01:00
out += "\n";
}
else
{
out += string();
out += "\n";
}
2004-01-07 01:48:02 +01:00
} break;
case list_t:
{
2013-12-19 09:30:17 +01:00
out += "list\n";
2004-01-07 01:48:02 +01:00
for (list_type::const_iterator i = list().begin(); i != list().end(); ++i)
{
2013-12-19 09:30:17 +01:00
i->to_string_impl(out, indent+1);
2004-01-07 01:48:02 +01:00
}
} break;
case dictionary_t:
{
2013-12-19 09:30:17 +01:00
out += "dictionary\n";
2004-01-07 01:48:02 +01:00
for (dictionary_type::const_iterator i = dict().begin(); i != dict().end(); ++i)
{
bool binary_string = false;
for (std::string::const_iterator k = i->first.begin(); k != i->first.end(); ++k)
{
if (!is_print(static_cast<unsigned char>(*k)))
{
binary_string = true;
break;
}
}
2013-12-19 09:30:17 +01:00
for (int j = 0; j < indent+1; ++j) out += " ";
out += "[";
if (binary_string) out += aux::to_hex(i->first);
2013-12-19 09:30:17 +01:00
else out += i->first;
out += "]";
if (i->second.type() != entry::string_t
&& i->second.type() != entry::int_t)
2013-12-19 09:30:17 +01:00
out += "\n";
else out += " ";
i->second.to_string_impl(out, indent+2);
2004-01-07 01:48:02 +01:00
}
} break;
case preformatted_t:
out += "<preformatted>\n";
break;
2015-08-11 02:03:24 +02:00
case undefined_t:
2013-12-19 09:30:17 +01:00
out += "<uninitialized>\n";
2004-01-07 01:48:02 +01:00
}
}
}