premiere-libtorrent/src/lazy_bdecode.cpp

505 lines
13 KiB
C++
Raw Normal View History

2008-04-11 05:41:09 +02:00
/*
Copyright (c) 2008, 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.
*/
#include "libtorrent/lazy_entry.hpp"
#include "libtorrent/escape_string.hpp"
2009-04-04 11:52:25 +02:00
#include <cstring>
#if TORRENT_USE_IOSTREAM
#include <iostream>
2009-04-04 11:52:25 +02:00
#endif
2008-04-11 05:41:09 +02:00
namespace
{
2008-11-10 05:15:29 +01:00
const float lazy_entry_grow_factor = 1.5f;
const int lazy_entry_dict_init = 5;
const int lazy_entry_list_init = 5;
}
2008-04-11 05:41:09 +02:00
namespace libtorrent
{
int fail_bdecode(lazy_entry& ret, int return_value = -1)
{
ret.clear();
return return_value;
}
2008-04-11 05:41:09 +02:00
// fills in 'val' with what the string between start and the
// first occurance of the delimiter is interpreted as an int.
// return the pointer to the delimiter, or 0 if there is a
// parse error. val should be initialized to zero
2008-04-14 00:34:04 +02:00
char const* parse_int(char const* start, char const* end, char delimiter, boost::int64_t& val)
2008-04-11 05:41:09 +02:00
{
while (start < end && *start != delimiter)
{
if (!is_digit(*start)) { return 0; }
2008-04-11 05:41:09 +02:00
val *= 10;
val += *start - '0';
++start;
}
return start;
}
2008-04-14 00:34:04 +02:00
char const* find_char(char const* start, char const* end, char delimiter)
2008-04-11 05:41:09 +02:00
{
while (start < end && *start != delimiter) ++start;
return start;
}
// return 0 = success
2008-04-14 00:34:04 +02:00
int lazy_bdecode(char const* start, char const* end, lazy_entry& ret, int depth_limit)
2008-04-11 05:41:09 +02:00
{
ret.clear();
if (start == end) return 0;
std::vector<lazy_entry*> stack;
stack.push_back(&ret);
while (start < end)
{
if (stack.empty()) break; // done!
lazy_entry* top = stack.back();
if (int(stack.size()) > depth_limit) return fail_bdecode(ret);
if (start >= end) return fail_bdecode(ret);
2008-04-11 05:41:09 +02:00
char t = *start;
2008-04-14 00:34:04 +02:00
++start;
if (start >= end && t != 'e') return fail_bdecode(ret);
2008-04-11 05:41:09 +02:00
switch (top->type())
{
case lazy_entry::dict_t:
{
if (t == 'e')
{
2008-04-14 00:34:04 +02:00
top->set_end(start);
2008-04-11 05:41:09 +02:00
stack.pop_back();
continue;
}
boost::int64_t len = t - '0';
start = parse_int(start, end, ':', len);
if (start == 0 || start + len + 3 > end || *start != ':') return fail_bdecode(ret);
2008-04-11 05:41:09 +02:00
++start;
if (start == end) return fail_bdecode(ret);
2008-04-11 05:41:09 +02:00
lazy_entry* ent = top->dict_append(start);
if (ent == 0) return fail_bdecode(ret, -2);
2008-04-11 05:41:09 +02:00
start += len;
if (start >= end) return fail_bdecode(ret);
2008-04-11 05:41:09 +02:00
stack.push_back(ent);
t = *start;
2008-04-14 00:34:04 +02:00
++start;
2008-04-11 05:41:09 +02:00
break;
}
case lazy_entry::list_t:
{
if (t == 'e')
{
2008-04-14 00:34:04 +02:00
top->set_end(start);
2008-04-11 05:41:09 +02:00
stack.pop_back();
continue;
}
lazy_entry* ent = top->list_append();
if (ent == 0) return fail_bdecode(ret, -2);
2008-04-11 05:41:09 +02:00
stack.push_back(ent);
break;
}
default: break;
}
top = stack.back();
switch (t)
{
case 'd':
2008-04-14 00:34:04 +02:00
top->construct_dict(start - 1);
2008-04-11 05:41:09 +02:00
continue;
case 'l':
2008-04-14 00:34:04 +02:00
top->construct_list(start - 1);
2008-04-11 05:41:09 +02:00
continue;
case 'i':
2008-04-14 00:34:04 +02:00
{
char const* int_start = start;
2008-04-11 05:41:09 +02:00
start = find_char(start, end, 'e');
2008-04-14 00:34:04 +02:00
top->construct_int(int_start, start - int_start);
if (start == end) return fail_bdecode(ret);
2008-04-11 05:41:09 +02:00
TORRENT_ASSERT(*start == 'e');
2008-04-14 00:34:04 +02:00
++start;
2008-04-11 05:41:09 +02:00
stack.pop_back();
continue;
2008-04-14 00:34:04 +02:00
}
2008-04-11 05:41:09 +02:00
default:
{
if (!is_digit(t)) return fail_bdecode(ret);
2008-04-11 05:41:09 +02:00
boost::int64_t len = t - '0';
start = parse_int(start, end, ':', len);
if (start == 0 || start + len + 1 > end || *start != ':') return fail_bdecode(ret);
2008-04-11 05:41:09 +02:00
++start;
top->construct_string(start, int(len));
2008-04-11 05:41:09 +02:00
stack.pop_back();
start += len;
continue;
}
}
return 0;
}
return 0;
}
2008-04-19 05:00:07 +02:00
size_type lazy_entry::int_value() const
2008-04-11 05:41:09 +02:00
{
TORRENT_ASSERT(m_type == int_t);
boost::int64_t val = 0;
bool negative = false;
if (*m_data.start == '-') negative = true;
2008-04-14 00:34:04 +02:00
parse_int(negative?m_data.start+1:m_data.start, m_data.start + m_size, 'e', val);
2008-04-11 05:41:09 +02:00
if (negative) val = -val;
return val;
}
2008-04-14 00:34:04 +02:00
lazy_entry* lazy_entry::dict_append(char const* name)
2008-04-11 05:41:09 +02:00
{
TORRENT_ASSERT(m_type == dict_t);
TORRENT_ASSERT(m_size <= m_capacity);
if (m_capacity == 0)
{
int capacity = lazy_entry_dict_init;
2009-11-27 08:08:47 +01:00
m_data.dict = new (std::nothrow) lazy_dict_entry[capacity];
2008-04-11 05:41:09 +02:00
if (m_data.dict == 0) return 0;
m_capacity = capacity;
}
else if (m_size == m_capacity)
{
int capacity = m_capacity * lazy_entry_grow_factor;
2009-11-27 08:08:47 +01:00
lazy_dict_entry* tmp = new (std::nothrow) lazy_dict_entry[capacity];
2008-04-11 05:41:09 +02:00
if (tmp == 0) return 0;
2009-11-27 08:08:47 +01:00
std::memcpy(tmp, m_data.dict, sizeof(lazy_dict_entry) * m_size);
for (int i = 0; i < m_size; ++i) m_data.dict[i].val.release();
2008-04-11 05:41:09 +02:00
delete[] m_data.dict;
m_data.dict = tmp;
m_capacity = capacity;
}
TORRENT_ASSERT(m_size < m_capacity);
2009-11-27 08:08:47 +01:00
lazy_dict_entry& ret = m_data.dict[m_size++];
ret.name = name;
return &ret.val;
2008-04-11 05:41:09 +02:00
}
2008-04-14 00:34:04 +02:00
namespace
{
// the number of decimal digits needed
// to represent the given value
int num_digits(int val)
{
int ret = 1;
2008-05-24 06:36:59 +02:00
while (val >= 10)
2008-04-14 00:34:04 +02:00
{
++ret;
val /= 10;
}
return ret;
}
}
void lazy_entry::construct_string(char const* start, int length)
{
TORRENT_ASSERT(m_type == none_t);
m_type = string_t;
m_data.start = start;
m_size = length;
m_begin = start - 1 - num_digits(length);
m_end = start + length;
}
namespace
{
// str1 is null-terminated
// str2 is not, str2 is len2 chars
bool string_equal(char const* str1, char const* str2, int len2)
{
while (len2 > 0)
{
if (*str1 != *str2) return false;
if (*str1 == 0) return false;
++str1;
++str2;
--len2;
}
2008-04-20 05:18:49 +02:00
return *str1 == 0;
2008-04-14 00:34:04 +02:00
}
}
2009-11-27 08:08:47 +01:00
std::pair<std::string, lazy_entry const*> lazy_entry::dict_at(int i) const
{
TORRENT_ASSERT(m_type == dict_t);
TORRENT_ASSERT(i < m_size);
lazy_dict_entry const& e = m_data.dict[i];
return std::make_pair(std::string(e.name, e.val.m_begin - e.name), &e.val);
}
2008-04-19 05:00:07 +02:00
std::string lazy_entry::dict_find_string_value(char const* name) const
{
lazy_entry const* e = dict_find(name);
if (e == 0 || e->type() != lazy_entry::string_t) return std::string();
return e->string_value();
}
pascal_string lazy_entry::dict_find_pstr(char const* name) const
{
lazy_entry const* e = dict_find(name);
if (e == 0 || e->type() != lazy_entry::string_t) return pascal_string(0, 0);
return e->string_pstr();
}
2008-07-18 15:48:09 +02:00
lazy_entry const* lazy_entry::dict_find_string(char const* name) const
{
lazy_entry const* e = dict_find(name);
if (e == 0 || e->type() != lazy_entry::string_t) return 0;
return e;
}
2009-12-02 05:05:24 +01:00
lazy_entry const* lazy_entry::dict_find_int(char const* name) const
{
lazy_entry const* e = dict_find(name);
if (e == 0 || e->type() != lazy_entry::int_t) return 0;
return e;
}
2008-04-19 05:00:07 +02:00
size_type lazy_entry::dict_find_int_value(char const* name, size_type default_val) const
{
lazy_entry const* e = dict_find(name);
if (e == 0 || e->type() != lazy_entry::int_t) return default_val;
return e->int_value();
}
lazy_entry const* lazy_entry::dict_find_dict(char const* name) const
{
lazy_entry const* e = dict_find(name);
if (e == 0 || e->type() != lazy_entry::dict_t) return 0;
return e;
}
lazy_entry const* lazy_entry::dict_find_list(char const* name) const
{
lazy_entry const* e = dict_find(name);
if (e == 0 || e->type() != lazy_entry::list_t) return 0;
return e;
}
2008-04-11 05:41:09 +02:00
lazy_entry* lazy_entry::dict_find(char const* name)
{
TORRENT_ASSERT(m_type == dict_t);
for (int i = 0; i < m_size; ++i)
{
2009-11-27 08:08:47 +01:00
lazy_dict_entry& e = m_data.dict[i];
if (string_equal(name, e.name, e.val.m_begin - e.name))
return &e.val;
2008-04-11 05:41:09 +02:00
}
return 0;
}
lazy_entry* lazy_entry::list_append()
{
TORRENT_ASSERT(m_type == list_t);
TORRENT_ASSERT(m_size <= m_capacity);
if (m_capacity == 0)
{
int capacity = lazy_entry_list_init;
2008-04-11 05:41:09 +02:00
m_data.list = new (std::nothrow) lazy_entry[capacity];
if (m_data.list == 0) return 0;
m_capacity = capacity;
}
else if (m_size == m_capacity)
{
int capacity = m_capacity * lazy_entry_grow_factor;
2008-04-11 05:41:09 +02:00
lazy_entry* tmp = new (std::nothrow) lazy_entry[capacity];
if (tmp == 0) return 0;
std::memcpy(tmp, m_data.list, sizeof(lazy_entry) * m_size);
2008-05-15 10:23:32 +02:00
for (int i = 0; i < m_size; ++i) m_data.list[i].release();
2008-04-11 05:41:09 +02:00
delete[] m_data.list;
m_data.list = tmp;
m_capacity = capacity;
}
TORRENT_ASSERT(m_size < m_capacity);
return m_data.list + (m_size++);
}
2008-04-19 05:00:07 +02:00
std::string lazy_entry::list_string_value_at(int i) const
{
lazy_entry const* e = list_at(i);
if (e == 0 || e->type() != lazy_entry::string_t) return std::string();
return e->string_value();
}
pascal_string lazy_entry::list_pstr_at(int i) const
{
lazy_entry const* e = list_at(i);
if (e == 0 || e->type() != lazy_entry::string_t) return pascal_string(0, 0);
return e->string_pstr();
}
2008-04-19 05:00:07 +02:00
size_type lazy_entry::list_int_value_at(int i, size_type default_val) const
{
lazy_entry const* e = list_at(i);
if (e == 0 || e->type() != lazy_entry::int_t) return default_val;
return e->int_value();
}
2008-04-11 05:41:09 +02:00
void lazy_entry::clear()
{
switch (m_type)
{
case list_t: delete[] m_data.list; break;
case dict_t: delete[] m_data.dict; break;
default: break;
}
2008-05-15 09:04:39 +02:00
m_data.start = 0;
2008-04-11 05:41:09 +02:00
m_size = 0;
m_capacity = 0;
m_type = none_t;
}
2008-04-20 05:18:49 +02:00
std::pair<char const*, int> lazy_entry::data_section() const
2008-04-14 00:34:04 +02:00
{
typedef std::pair<char const*, int> return_t;
return return_t(m_begin, m_end - m_begin);
}
2009-04-04 11:52:25 +02:00
#if TORRENT_USE_IOSTREAM
std::ostream& operator<<(std::ostream& os, lazy_entry const& e)
{
return os << print_entry(e);
}
#endif // TORRENT_USE_IOSTREAM
std::string print_entry(lazy_entry const& e, bool single_line, int indent)
{
char indent_str[200];
memset(indent_str, ' ', 200);
indent_str[0] = ',';
indent_str[1] = '\n';
indent_str[199] = 0;
if (indent < 197 && indent >= 0) indent_str[indent+2] = 0;
std::string ret;
switch (e.type())
{
case lazy_entry::none_t: return "none";
case lazy_entry::int_t:
{
char str[100];
snprintf(str, sizeof(str), "%"PRId64, e.int_value());
return str;
}
case lazy_entry::string_t:
{
bool printable = true;
2008-04-14 00:34:04 +02:00
char const* str = e.string_ptr();
for (int i = 0; i < e.string_length(); ++i)
{
using namespace std;
if (is_print((unsigned char)str[i])) continue;
printable = false;
break;
}
ret += "'";
if (printable)
{
ret += e.string_value();
ret += "'";
return ret;
}
for (int i = 0; i < e.string_length(); ++i)
{
2009-05-20 00:22:22 +02:00
char tmp[5];
snprintf(tmp, sizeof(tmp), "%02x", (unsigned char)str[i]);
ret += tmp;
}
ret += "'";
return ret;
}
case lazy_entry::list_t:
{
ret += '[';
2008-04-13 07:50:13 +02:00
bool one_liner = (e.list_size() == 0
|| (e.list_at(0)->type() == lazy_entry::int_t
&& e.list_size() < 20)
|| (e.list_at(0)->type() == lazy_entry::string_t
2008-05-14 20:58:24 +02:00
&& (e.list_at(0)->string_length() < 10
2009-03-22 06:31:28 +01:00
|| e.list_size() < 2)
&& e.list_size() < 5))
|| single_line;
if (!one_liner) ret += indent_str + 1;
for (int i = 0; i < e.list_size(); ++i)
{
if (i == 0 && one_liner) ret += " ";
ret += print_entry(*e.list_at(i), single_line, indent + 2);
if (i < e.list_size() - 1) ret += (one_liner?", ":indent_str);
else ret += (one_liner?" ":indent_str+1);
}
ret += "]";
return ret;
}
case lazy_entry::dict_t:
{
ret += "{";
bool one_liner = ((e.dict_size() == 0
|| e.dict_at(0).second->type() == lazy_entry::int_t
|| (e.dict_at(0).second->type() == lazy_entry::string_t
2008-05-14 20:58:24 +02:00
&& e.dict_at(0).second->string_length() < 30)
2008-04-20 05:18:49 +02:00
|| e.dict_at(0).first.size() < 10)
&& e.dict_size() < 5)
|| single_line;
2008-04-13 07:50:13 +02:00
if (!one_liner) ret += indent_str+1;
for (int i = 0; i < e.dict_size(); ++i)
{
if (i == 0 && one_liner) ret += " ";
2008-04-20 05:18:49 +02:00
std::pair<std::string, lazy_entry const*> ent = e.dict_at(i);
ret += "'";
ret += ent.first;
ret += "': ";
ret += print_entry(*ent.second, single_line, indent + 2);
if (i < e.dict_size() - 1) ret += (one_liner?", ":indent_str);
else ret += (one_liner?" ":indent_str+1);
}
ret += "}";
return ret;
}
}
return ret;
}
2008-04-11 05:41:09 +02:00
};