2008-04-11 05:41:09 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2003, 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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TORRENT_LAZY_ENTRY_HPP_INCLUDED
|
|
|
|
#define TORRENT_LAZY_ENTRY_HPP_INCLUDED
|
|
|
|
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2008-11-30 05:50:52 +01:00
|
|
|
#include <string>
|
2009-11-27 08:17:10 +01:00
|
|
|
#include <cstring>
|
2009-01-06 23:44:32 +01:00
|
|
|
#include "libtorrent/config.hpp"
|
|
|
|
#include "libtorrent/assert.hpp"
|
|
|
|
#include "libtorrent/size_type.hpp"
|
2010-10-28 06:01:59 +02:00
|
|
|
#include "libtorrent/error_code.hpp"
|
2008-04-11 05:41:09 +02:00
|
|
|
|
2009-04-04 11:52:25 +02:00
|
|
|
#if TORRENT_USE_IOSTREAM
|
|
|
|
#include <iosfwd>
|
|
|
|
#endif
|
|
|
|
|
2008-04-11 05:41:09 +02:00
|
|
|
namespace libtorrent
|
|
|
|
{
|
|
|
|
struct lazy_entry;
|
|
|
|
|
2009-01-06 23:44:32 +01:00
|
|
|
TORRENT_EXPORT char const* parse_int(char const* start, char const* end
|
|
|
|
, char delimiter, boost::int64_t& val);
|
2010-11-25 03:49:50 +01:00
|
|
|
|
2008-04-11 05:41:09 +02:00
|
|
|
// return 0 = success
|
2010-10-28 06:01:59 +02:00
|
|
|
TORRENT_EXPORT int lazy_bdecode(char const* start, char const* end
|
|
|
|
, lazy_entry& ret, error_code& ec, int* error_pos = 0
|
2010-11-25 03:49:50 +01:00
|
|
|
, int depth_limit = 1000, int item_limit = 1000000);
|
2008-04-11 05:41:09 +02:00
|
|
|
|
2010-11-29 03:48:40 +01:00
|
|
|
#ifndef TORRENT_NO_DEPRECATE
|
|
|
|
// for backwards compatibility, does not report error code
|
|
|
|
// deprecated in 0.16
|
|
|
|
TORRENT_DEPRECATED_PREFIX
|
|
|
|
TORRENT_EXPORT int lazy_bdecode(char const* start, char const* end
|
|
|
|
, lazy_entry& ret, int depth_limit = 1000, int item_limit = 1000000) TORRENT_DEPRECATED;
|
|
|
|
#endif
|
|
|
|
|
2009-11-26 01:09:49 +01:00
|
|
|
struct pascal_string
|
|
|
|
{
|
|
|
|
pascal_string(char const* p, int l): len(l), ptr(p) {}
|
|
|
|
int len;
|
|
|
|
char const* ptr;
|
|
|
|
bool operator<(pascal_string const& rhs) const
|
|
|
|
{
|
2009-11-27 08:17:10 +01:00
|
|
|
return std::memcmp(ptr, rhs.ptr, (std::min)(len, rhs.len)) < 0
|
2009-11-26 01:09:49 +01:00
|
|
|
|| len < rhs.len;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-11-27 08:08:47 +01:00
|
|
|
struct lazy_dict_entry;
|
|
|
|
|
2009-01-06 23:44:32 +01:00
|
|
|
struct TORRENT_EXPORT lazy_entry
|
2008-04-11 05:41:09 +02:00
|
|
|
{
|
|
|
|
enum entry_type_t
|
|
|
|
{
|
|
|
|
none_t, dict_t, list_t, string_t, int_t
|
|
|
|
};
|
|
|
|
|
2011-02-21 06:24:41 +01:00
|
|
|
lazy_entry() : m_begin(0), m_len(0), m_size(0), m_capacity(0), m_type(none_t)
|
2008-04-14 00:34:04 +02:00
|
|
|
{ m_data.start = 0; }
|
2008-04-11 05:41:09 +02:00
|
|
|
|
2010-02-18 17:31:51 +01:00
|
|
|
entry_type_t type() const { return (entry_type_t)m_type; }
|
2008-04-11 05:41:09 +02:00
|
|
|
|
2008-04-14 00:34:04 +02:00
|
|
|
// start points to the first decimal digit
|
|
|
|
// length is the number of digits
|
|
|
|
void construct_int(char const* start, int length)
|
2008-04-11 05:41:09 +02:00
|
|
|
{
|
|
|
|
TORRENT_ASSERT(m_type == none_t);
|
|
|
|
m_type = int_t;
|
|
|
|
m_data.start = start;
|
2008-04-14 00:34:04 +02:00
|
|
|
m_size = length;
|
|
|
|
m_begin = start - 1; // include 'i'
|
2010-11-25 00:49:22 +01:00
|
|
|
m_len = length + 2; // include 'e'
|
2008-04-11 05:41:09 +02:00
|
|
|
}
|
|
|
|
|
2008-04-19 05:00:07 +02:00
|
|
|
size_type int_value() const;
|
2008-04-11 05:41:09 +02:00
|
|
|
|
|
|
|
// string functions
|
|
|
|
// ================
|
|
|
|
|
2008-04-14 00:34:04 +02:00
|
|
|
void construct_string(char const* start, int length);
|
|
|
|
|
|
|
|
// the string is not null-terminated!
|
|
|
|
char const* string_ptr() const
|
2008-04-11 05:41:09 +02:00
|
|
|
{
|
2008-04-14 00:34:04 +02:00
|
|
|
TORRENT_ASSERT(m_type == string_t);
|
|
|
|
return m_data.start;
|
2008-04-11 05:41:09 +02:00
|
|
|
}
|
|
|
|
|
2008-04-14 00:34:04 +02:00
|
|
|
// this will return a null terminated string
|
|
|
|
// it will write to the source buffer!
|
|
|
|
char const* string_cstr() const
|
2008-04-11 05:41:09 +02:00
|
|
|
{
|
|
|
|
TORRENT_ASSERT(m_type == string_t);
|
2008-04-14 00:34:04 +02:00
|
|
|
const_cast<char*>(m_data.start)[m_size] = 0;
|
2008-04-11 05:41:09 +02:00
|
|
|
return m_data.start;
|
|
|
|
}
|
|
|
|
|
2009-11-26 01:09:49 +01:00
|
|
|
pascal_string string_pstr() const
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(m_type == string_t);
|
|
|
|
return pascal_string(m_data.start, m_size);
|
|
|
|
}
|
|
|
|
|
2008-04-14 00:34:04 +02:00
|
|
|
std::string string_value() const
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(m_type == string_t);
|
|
|
|
return std::string(m_data.start, m_size);
|
|
|
|
}
|
|
|
|
|
2008-04-12 03:58:55 +02:00
|
|
|
int string_length() const
|
|
|
|
{ return m_size; }
|
|
|
|
|
2008-04-11 05:41:09 +02:00
|
|
|
// dictionary functions
|
|
|
|
// ====================
|
|
|
|
|
2008-04-14 00:34:04 +02:00
|
|
|
void construct_dict(char const* begin)
|
2008-04-11 05:41:09 +02:00
|
|
|
{
|
|
|
|
TORRENT_ASSERT(m_type == none_t);
|
|
|
|
m_type = dict_t;
|
|
|
|
m_size = 0;
|
|
|
|
m_capacity = 0;
|
2008-04-14 00:34:04 +02:00
|
|
|
m_begin = begin;
|
2008-04-11 05:41:09 +02:00
|
|
|
}
|
|
|
|
|
2008-04-14 00:34:04 +02:00
|
|
|
lazy_entry* dict_append(char const* name);
|
2012-02-10 05:19:11 +01:00
|
|
|
void pop();
|
2008-04-11 05:41:09 +02:00
|
|
|
lazy_entry* dict_find(char const* name);
|
|
|
|
lazy_entry const* dict_find(char const* name) const
|
|
|
|
{ return const_cast<lazy_entry*>(this)->dict_find(name); }
|
2008-04-19 05:00:07 +02:00
|
|
|
|
|
|
|
std::string dict_find_string_value(char const* name) const;
|
2009-11-26 07:54:52 +01:00
|
|
|
pascal_string dict_find_pstr(char const* name) const;
|
2008-04-19 05:00:07 +02:00
|
|
|
size_type dict_find_int_value(char const* name, size_type default_val = 0) const;
|
|
|
|
lazy_entry const* dict_find_dict(char const* name) const;
|
|
|
|
lazy_entry const* dict_find_list(char const* name) const;
|
2008-07-18 15:48:09 +02:00
|
|
|
lazy_entry const* dict_find_string(char const* name) const;
|
2009-12-02 05:05:24 +01:00
|
|
|
lazy_entry const* dict_find_int(char const* name) const;
|
2008-04-19 05:00:07 +02:00
|
|
|
|
2009-11-27 08:08:47 +01:00
|
|
|
std::pair<std::string, lazy_entry const*> dict_at(int i) const;
|
2008-04-11 05:41:09 +02:00
|
|
|
|
|
|
|
int dict_size() const
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(m_type == dict_t);
|
|
|
|
return m_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
// list functions
|
|
|
|
// ==============
|
|
|
|
|
2008-04-14 00:34:04 +02:00
|
|
|
void construct_list(char const* begin)
|
2008-04-11 05:41:09 +02:00
|
|
|
{
|
|
|
|
TORRENT_ASSERT(m_type == none_t);
|
|
|
|
m_type = list_t;
|
|
|
|
m_size = 0;
|
|
|
|
m_capacity = 0;
|
2008-04-14 00:34:04 +02:00
|
|
|
m_begin = begin;
|
2008-04-11 05:41:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
lazy_entry* list_append();
|
|
|
|
lazy_entry* list_at(int i)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(m_type == list_t);
|
2010-02-14 02:39:55 +01:00
|
|
|
TORRENT_ASSERT(i < int(m_size));
|
2008-04-11 05:41:09 +02:00
|
|
|
return &m_data.list[i];
|
|
|
|
}
|
2008-04-13 07:19:10 +02:00
|
|
|
lazy_entry const* list_at(int i) const
|
|
|
|
{ return const_cast<lazy_entry*>(this)->list_at(i); }
|
2008-04-11 05:41:09 +02:00
|
|
|
|
2008-04-19 05:00:07 +02:00
|
|
|
std::string list_string_value_at(int i) const;
|
2009-11-26 07:54:52 +01:00
|
|
|
pascal_string list_pstr_at(int i) const;
|
2008-04-19 05:00:07 +02:00
|
|
|
size_type list_int_value_at(int i, size_type default_val = 0) const;
|
|
|
|
|
2008-04-11 05:41:09 +02:00
|
|
|
int list_size() const
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(m_type == list_t);
|
2010-02-14 02:39:55 +01:00
|
|
|
return int(m_size);
|
2008-04-11 05:41:09 +02:00
|
|
|
}
|
|
|
|
|
2008-07-01 01:14:31 +02:00
|
|
|
// end points one byte passed last byte
|
2008-04-14 00:34:04 +02:00
|
|
|
void set_end(char const* end)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(end > m_begin);
|
2010-11-25 00:49:22 +01:00
|
|
|
m_len = end - m_begin;
|
2008-04-14 00:34:04 +02:00
|
|
|
}
|
|
|
|
|
2008-04-11 05:41:09 +02:00
|
|
|
void clear();
|
|
|
|
|
2008-05-15 10:23:32 +02:00
|
|
|
// releases ownership of any memory allocated
|
|
|
|
void release()
|
|
|
|
{
|
|
|
|
m_data.start = 0;
|
|
|
|
m_size = 0;
|
|
|
|
m_capacity = 0;
|
|
|
|
m_type = none_t;
|
|
|
|
}
|
|
|
|
|
2008-04-11 05:41:09 +02:00
|
|
|
~lazy_entry()
|
|
|
|
{ clear(); }
|
|
|
|
|
2008-04-14 00:34:04 +02:00
|
|
|
// returns pointers into the source buffer where
|
|
|
|
// this entry has its bencoded data
|
2008-04-20 05:18:49 +02:00
|
|
|
std::pair<char const*, int> data_section() const;
|
2008-04-14 00:34:04 +02:00
|
|
|
|
2008-05-14 07:16:40 +02:00
|
|
|
void swap(lazy_entry& e)
|
|
|
|
{
|
|
|
|
using std::swap;
|
2010-02-18 17:31:51 +01:00
|
|
|
boost::uint32_t tmp = e.m_type;
|
2010-02-14 02:39:55 +01:00
|
|
|
e.m_type = m_type;
|
2010-02-18 17:31:51 +01:00
|
|
|
m_type = tmp;
|
|
|
|
tmp = e.m_capacity;
|
2010-02-14 02:39:55 +01:00
|
|
|
e.m_capacity = m_capacity;
|
2010-02-18 17:31:51 +01:00
|
|
|
m_capacity = tmp;
|
2008-05-14 07:16:40 +02:00
|
|
|
swap(m_data.start, e.m_data.start);
|
|
|
|
swap(m_size, e.m_size);
|
|
|
|
swap(m_begin, e.m_begin);
|
2010-11-25 00:49:22 +01:00
|
|
|
swap(m_len, e.m_len);
|
2008-05-14 07:16:40 +02:00
|
|
|
}
|
|
|
|
|
2008-04-11 05:41:09 +02:00
|
|
|
private:
|
|
|
|
|
|
|
|
union data_t
|
|
|
|
{
|
2009-11-27 08:08:47 +01:00
|
|
|
lazy_dict_entry* dict;
|
2008-04-11 05:41:09 +02:00
|
|
|
lazy_entry* list;
|
2008-04-14 00:34:04 +02:00
|
|
|
char const* start;
|
2008-04-11 05:41:09 +02:00
|
|
|
} m_data;
|
2009-11-27 08:08:47 +01:00
|
|
|
|
2008-04-14 00:34:04 +02:00
|
|
|
// used for dictionaries and lists to record the range
|
|
|
|
// in the original buffer they are based on
|
|
|
|
char const* m_begin;
|
2010-11-25 00:49:22 +01:00
|
|
|
// the number of bytes this entry extends in the
|
|
|
|
// bencoded byffer
|
|
|
|
boost::uint32_t m_len;
|
|
|
|
|
|
|
|
// if list or dictionary, the number of items
|
|
|
|
boost::uint32_t m_size;
|
|
|
|
// if list or dictionary, allocated number of items
|
|
|
|
boost::uint32_t m_capacity:29;
|
|
|
|
// element type (dict, list, int, string)
|
|
|
|
boost::uint32_t m_type:3;
|
2010-02-14 02:39:55 +01:00
|
|
|
|
2009-11-26 01:09:49 +01:00
|
|
|
// non-copyable
|
|
|
|
lazy_entry(lazy_entry const&);
|
|
|
|
lazy_entry const& operator=(lazy_entry const&);
|
2008-04-11 05:41:09 +02:00
|
|
|
};
|
|
|
|
|
2009-11-27 08:08:47 +01:00
|
|
|
struct lazy_dict_entry
|
|
|
|
{
|
|
|
|
char const* name;
|
|
|
|
lazy_entry val;
|
|
|
|
};
|
|
|
|
|
2009-12-03 06:10:30 +01:00
|
|
|
TORRENT_EXPORT std::string print_entry(lazy_entry const& e
|
|
|
|
, bool single_line = false, int indent = 0);
|
2009-04-04 11:52:25 +02:00
|
|
|
#if TORRENT_USE_IOSTREAM
|
2009-01-06 23:44:32 +01:00
|
|
|
TORRENT_EXPORT std::ostream& operator<<(std::ostream& os, lazy_entry const& e);
|
2009-04-04 11:52:25 +02:00
|
|
|
#endif
|
2008-04-13 07:19:10 +02:00
|
|
|
|
2008-10-26 23:12:12 +01:00
|
|
|
}
|
2008-04-11 05:41:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|