2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2008-2022 Mnemosyne LLC.
|
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0), GPLv3 (SPDX: GPL-3.0),
|
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <array>
|
2021-10-17 20:17:18 +00:00
|
|
|
#include <cctype> /* isdigit() */
|
2021-09-29 00:09:01 +00:00
|
|
|
#include <deque>
|
2021-10-17 20:17:18 +00:00
|
|
|
#include <cerrno>
|
2021-10-15 13:28:47 +00:00
|
|
|
#include <string_view>
|
2021-10-15 18:13:33 +00:00
|
|
|
#include <optional>
|
2012-12-14 04:34:42 +00:00
|
|
|
|
|
|
|
#include <event2/buffer.h>
|
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
#define LIBTRANSMISSION_VARIANT_MODULE
|
2017-11-14 20:21:28 +00:00
|
|
|
|
2012-12-14 04:34:42 +00:00
|
|
|
#include "transmission.h"
|
2021-11-18 05:37:35 +00:00
|
|
|
|
2022-01-24 06:30:00 +00:00
|
|
|
#include "benc.h"
|
2021-11-18 05:37:35 +00:00
|
|
|
#include "tr-assert.h"
|
2022-01-13 02:13:58 +00:00
|
|
|
#include "quark.h"
|
2012-12-24 22:38:41 +00:00
|
|
|
#include "utils.h" /* tr_snprintf() */
|
2012-12-14 04:34:42 +00:00
|
|
|
#include "variant-common.h"
|
2021-11-18 05:37:35 +00:00
|
|
|
#include "variant.h"
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2021-11-17 03:29:30 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2021-12-29 08:28:12 +00:00
|
|
|
auto constexpr MaxBencStrLength = size_t{ 128 * 1024 * 1024 }; // arbitrary
|
2017-05-16 18:37:00 +00:00
|
|
|
|
2012-12-14 04:34:42 +00:00
|
|
|
/***
|
2022-01-24 06:30:00 +00:00
|
|
|
****
|
|
|
|
****
|
2012-12-14 04:34:42 +00:00
|
|
|
***/
|
|
|
|
|
2022-01-24 06:30:00 +00:00
|
|
|
namespace transmission::benc::impl
|
|
|
|
{
|
|
|
|
|
2012-12-14 04:34:42 +00:00
|
|
|
/**
|
|
|
|
* The initial i and trailing e are beginning and ending delimiters.
|
|
|
|
* You can have negative numbers such as i-3e. You cannot prefix the
|
|
|
|
* number with a zero such as i04e. However, i0e is valid.
|
|
|
|
* Example: i3e represents the integer "3"
|
2021-11-17 03:29:30 +00:00
|
|
|
*
|
|
|
|
* The maximum number of bit of this integer is unspecified,
|
2012-12-14 04:34:42 +00:00
|
|
|
* but to handle it as a signed 64bit integer is mandatory to handle
|
|
|
|
* "large files" aka .torrent for more that 4Gbyte
|
|
|
|
*/
|
2022-01-24 06:30:00 +00:00
|
|
|
std::optional<int64_t> ParseInt(std::string_view* benc)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2021-12-29 08:28:12 +00:00
|
|
|
auto constexpr Prefix = "i"sv;
|
|
|
|
auto constexpr Suffix = "e"sv;
|
|
|
|
|
2021-11-17 03:29:30 +00:00
|
|
|
// find the beginning delimiter
|
|
|
|
auto walk = *benc;
|
2021-12-29 08:28:12 +00:00
|
|
|
if (std::size(walk) < 3 || !tr_strvStartsWith(walk, Prefix))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-17 03:29:30 +00:00
|
|
|
return {};
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-14 16:04:44 +00:00
|
|
|
|
2021-11-17 03:29:30 +00:00
|
|
|
// find the ending delimiter
|
2021-12-29 08:28:12 +00:00
|
|
|
walk.remove_prefix(std::size(Prefix));
|
|
|
|
auto const pos = walk.find(Suffix);
|
2021-12-17 05:47:51 +00:00
|
|
|
if (pos == std::string_view::npos)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-17 03:29:30 +00:00
|
|
|
return {};
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-14 16:04:44 +00:00
|
|
|
|
2021-11-17 03:29:30 +00:00
|
|
|
// leading zeroes are not allowed
|
|
|
|
if ((walk[0] == '0' && isdigit(walk[1])) || (walk[0] == '-' && walk[1] == '0' && isdigit(walk[2])))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-17 03:29:30 +00:00
|
|
|
return {};
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-12-29 08:28:12 +00:00
|
|
|
// parse the string and make sure the next char is `Suffix`
|
|
|
|
auto const value = tr_parseNum<int64_t>(walk);
|
|
|
|
if (!value || !tr_strvStartsWith(walk, Suffix))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-17 03:29:30 +00:00
|
|
|
return {};
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-12-29 08:28:12 +00:00
|
|
|
walk.remove_prefix(std::size(Suffix));
|
2021-11-17 03:29:30 +00:00
|
|
|
*benc = walk;
|
2021-12-29 08:28:12 +00:00
|
|
|
return *value;
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Byte strings are encoded as follows:
|
|
|
|
* <string length encoded in base ten ASCII>:<string data>
|
|
|
|
* Note that there is no constant beginning delimiter, and no ending delimiter.
|
|
|
|
* Example: 4:spam represents the string "spam"
|
|
|
|
*/
|
2022-01-24 06:30:00 +00:00
|
|
|
std::optional<std::string_view> ParseString(std::string_view* benc)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2021-11-17 03:29:30 +00:00
|
|
|
// find the ':' delimiter
|
|
|
|
auto const colon_pos = benc->find(':');
|
2021-12-17 05:47:51 +00:00
|
|
|
if (colon_pos == std::string_view::npos)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-17 03:29:30 +00:00
|
|
|
return {};
|
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2021-11-17 03:29:30 +00:00
|
|
|
// get the string length
|
2021-12-29 08:28:12 +00:00
|
|
|
auto svtmp = benc->substr(0, colon_pos);
|
|
|
|
auto const len = tr_parseNum<size_t>(svtmp);
|
|
|
|
if (!len || *len >= MaxBencStrLength)
|
2021-11-17 03:29:30 +00:00
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2021-11-17 03:29:30 +00:00
|
|
|
// do we have `len` bytes of string data?
|
2021-12-29 08:28:12 +00:00
|
|
|
svtmp = benc->substr(colon_pos + 1);
|
|
|
|
if (std::size(svtmp) < len)
|
2021-11-17 03:29:30 +00:00
|
|
|
{
|
|
|
|
return {};
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2021-12-29 08:28:12 +00:00
|
|
|
auto const string = svtmp.substr(0, *len);
|
|
|
|
*benc = svtmp.substr(*len);
|
2021-11-17 03:29:30 +00:00
|
|
|
return string;
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 06:30:00 +00:00
|
|
|
} // namespace transmission::benc::impl
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2022-01-24 06:30:00 +00:00
|
|
|
/***
|
|
|
|
**** tr_variantParse()
|
|
|
|
**** tr_variantLoad()
|
|
|
|
***/
|
|
|
|
|
|
|
|
struct MyHandler : public transmission::benc::Handler
|
|
|
|
{
|
|
|
|
tr_variant* const top_;
|
|
|
|
int const parse_opts_;
|
|
|
|
std::deque<tr_variant*> stack_;
|
|
|
|
std::optional<tr_quark> key_;
|
|
|
|
|
|
|
|
MyHandler(tr_variant* top, int parse_opts)
|
|
|
|
: top_{ top }
|
|
|
|
, parse_opts_{ parse_opts }
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-01-25 01:05:24 +00:00
|
|
|
~MyHandler() override = default;
|
2022-01-24 19:07:55 +00:00
|
|
|
|
2022-01-24 06:30:00 +00:00
|
|
|
bool Int64(int64_t value) final
|
|
|
|
{
|
|
|
|
if (tr_variant* variant = get_node(); variant != nullptr)
|
2012-12-24 22:38:41 +00:00
|
|
|
{
|
2022-01-24 06:30:00 +00:00
|
|
|
tr_variantInitInt(variant, value);
|
2012-12-24 22:38:41 +00:00
|
|
|
}
|
2022-01-24 06:30:00 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool String(std::string_view sv) final
|
|
|
|
{
|
|
|
|
if (tr_variant* variant = get_node(); variant != nullptr)
|
2012-12-24 22:38:41 +00:00
|
|
|
{
|
2022-01-24 06:30:00 +00:00
|
|
|
if ((parse_opts_ & TR_VARIANT_PARSE_INPLACE) != 0)
|
|
|
|
{
|
|
|
|
tr_variantInitStrView(variant, sv);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_variantInitStr(variant, sv);
|
|
|
|
}
|
2012-12-24 22:38:41 +00:00
|
|
|
}
|
2022-01-24 06:30:00 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StartDict() final
|
|
|
|
{
|
|
|
|
if (tr_variant* variant = get_node(); variant != nullptr)
|
2012-12-24 22:38:41 +00:00
|
|
|
{
|
2022-01-24 06:30:00 +00:00
|
|
|
tr_variantInitDict(variant, 0);
|
|
|
|
stack_.push_back(variant);
|
2012-12-24 22:38:41 +00:00
|
|
|
}
|
2022-01-24 06:30:00 +00:00
|
|
|
|
|
|
|
return true;
|
2012-12-24 22:38:41 +00:00
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2022-01-24 06:30:00 +00:00
|
|
|
bool Key(std::string_view sv) final
|
|
|
|
{
|
|
|
|
key_ = tr_quark_new(sv);
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2022-01-24 06:30:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
2021-11-18 05:37:35 +00:00
|
|
|
|
2022-01-24 06:30:00 +00:00
|
|
|
bool EndDict() final
|
|
|
|
{
|
|
|
|
stack_.pop_back();
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2022-01-24 06:30:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2022-01-24 06:30:00 +00:00
|
|
|
bool StartArray() final
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2022-01-24 06:30:00 +00:00
|
|
|
if (tr_variant* variant = get_node(); variant != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-01-24 06:30:00 +00:00
|
|
|
tr_variantInitList(variant, 0);
|
|
|
|
stack_.push_back(variant);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-24 22:38:41 +00:00
|
|
|
|
2022-01-24 06:30:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2022-01-24 06:30:00 +00:00
|
|
|
bool EndArray() final
|
|
|
|
{
|
|
|
|
stack_.pop_back();
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2022-01-24 06:30:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
2021-11-28 01:58:35 +00:00
|
|
|
|
2022-01-24 06:30:00 +00:00
|
|
|
private:
|
|
|
|
tr_variant* get_node()
|
|
|
|
{
|
|
|
|
tr_variant* node = nullptr;
|
2021-11-17 03:29:30 +00:00
|
|
|
|
2022-01-24 06:30:00 +00:00
|
|
|
if (std::empty(stack_))
|
|
|
|
{
|
|
|
|
node = top_;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto* parent = stack_.back();
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2022-01-24 06:30:00 +00:00
|
|
|
if (tr_variantIsList(parent))
|
2021-11-28 01:58:35 +00:00
|
|
|
{
|
2022-01-24 06:30:00 +00:00
|
|
|
node = tr_variantListAdd(parent);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2022-01-24 06:30:00 +00:00
|
|
|
else if (key_ && tr_variantIsDict(parent))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-01-24 06:30:00 +00:00
|
|
|
node = tr_variantDictAdd(parent, *key_);
|
|
|
|
key_.reset();
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2022-01-24 06:30:00 +00:00
|
|
|
return node;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2022-01-24 06:30:00 +00:00
|
|
|
};
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2022-01-24 06:30:00 +00:00
|
|
|
bool tr_variantParseBenc(tr_variant& top, int parse_opts, std::string_view benc, char const** setme_end, tr_error** error)
|
|
|
|
{
|
|
|
|
using Stack = transmission::benc::ParserStack<512>;
|
|
|
|
auto stack = Stack{};
|
|
|
|
auto handler = MyHandler{ &top, parse_opts };
|
|
|
|
bool const ok = transmission::benc::parse(benc, stack, handler, setme_end, error);
|
|
|
|
if (!ok)
|
2017-01-17 20:55:34 +00:00
|
|
|
{
|
2021-11-17 03:29:30 +00:00
|
|
|
tr_variantFree(&top);
|
2017-01-17 20:55:34 +00:00
|
|
|
}
|
2022-01-24 06:30:00 +00:00
|
|
|
return ok;
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
static void saveIntFunc(tr_variant const* val, void* vevbuf)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* evbuf = static_cast<struct evbuffer*>(vevbuf);
|
2017-04-19 12:04:45 +00:00
|
|
|
evbuffer_add_printf(evbuf, "i%" PRId64 "e", val->val.i);
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
static void saveBoolFunc(tr_variant const* val, void* vevbuf)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* evbuf = static_cast<struct evbuffer*>(vevbuf);
|
2017-04-19 12:04:45 +00:00
|
|
|
if (val->val.b)
|
|
|
|
{
|
|
|
|
evbuffer_add(evbuf, "i1e", 3);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
evbuffer_add(evbuf, "i0e", 3);
|
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
static void saveRealFunc(tr_variant const* val, void* vevbuf)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2021-11-17 03:29:30 +00:00
|
|
|
auto buf = std::array<char, 64>{};
|
|
|
|
int const len = tr_snprintf(std::data(buf), std::size(buf), "%f", val->val.d);
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2021-11-17 03:29:30 +00:00
|
|
|
auto* evbuf = static_cast<evbuffer*>(vevbuf);
|
2017-04-19 12:04:45 +00:00
|
|
|
evbuffer_add_printf(evbuf, "%d:", len);
|
2021-11-17 03:29:30 +00:00
|
|
|
evbuffer_add(evbuf, std::data(buf), len);
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
static void saveStringFunc(tr_variant const* v, void* vevbuf)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2021-11-05 06:29:19 +00:00
|
|
|
auto sv = std::string_view{};
|
|
|
|
(void)!tr_variantGetStrView(v, &sv);
|
2019-02-18 22:38:24 +00:00
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* evbuf = static_cast<struct evbuffer*>(vevbuf);
|
2021-11-05 06:29:19 +00:00
|
|
|
evbuffer_add_printf(evbuf, "%zu:", std::size(sv));
|
|
|
|
evbuffer_add(evbuf, std::data(sv), std::size(sv));
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 16:41:54 +00:00
|
|
|
static void saveDictBeginFunc(tr_variant const* /*val*/, void* vevbuf)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* evbuf = static_cast<struct evbuffer*>(vevbuf);
|
2017-04-19 12:04:45 +00:00
|
|
|
evbuffer_add(evbuf, "d", 1);
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 16:41:54 +00:00
|
|
|
static void saveListBeginFunc(tr_variant const* /*val*/, void* vevbuf)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* evbuf = static_cast<struct evbuffer*>(vevbuf);
|
2017-04-19 12:04:45 +00:00
|
|
|
evbuffer_add(evbuf, "l", 1);
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 16:41:54 +00:00
|
|
|
static void saveContainerEndFunc(tr_variant const* /*val*/, void* vevbuf)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* evbuf = static_cast<struct evbuffer*>(vevbuf);
|
2017-04-19 12:04:45 +00:00
|
|
|
evbuffer_add(evbuf, "e", 1);
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
static struct VariantWalkFuncs const walk_funcs = {
|
|
|
|
saveIntFunc, //
|
|
|
|
saveBoolFunc, //
|
|
|
|
saveRealFunc, //
|
|
|
|
saveStringFunc, //
|
|
|
|
saveDictBeginFunc, //
|
|
|
|
saveListBeginFunc, //
|
|
|
|
saveContainerEndFunc, //
|
2017-04-19 12:04:45 +00:00
|
|
|
};
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_variantToBufBenc(tr_variant const* top, struct evbuffer* buf)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_variantWalk(top, &walk_funcs, buf, true);
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|