2023-11-01 21:11:11 +00:00
|
|
|
// This file Copyright © Mnemosyne LLC.
|
2022-08-08 18:05:39 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-24 06:30:00 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <cerrno>
|
2022-01-31 23:05:35 +00:00
|
|
|
#include <cstddef> // size_t
|
|
|
|
#include <cstdint> // int64_t
|
2022-01-24 06:30:00 +00:00
|
|
|
#include <optional>
|
2022-05-24 04:05:16 +00:00
|
|
|
#include <string>
|
2022-01-24 06:30:00 +00:00
|
|
|
#include <string_view>
|
2022-01-31 23:05:35 +00:00
|
|
|
#include <utility> // make_pair
|
2022-01-24 06:30:00 +00:00
|
|
|
|
2023-11-03 17:03:26 +00:00
|
|
|
#include "libtransmission/error.h"
|
2022-01-24 06:30:00 +00:00
|
|
|
|
|
|
|
namespace transmission::benc
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace impl
|
|
|
|
{
|
|
|
|
|
|
|
|
std::optional<int64_t> ParseInt(std::string_view* benc);
|
|
|
|
|
|
|
|
std::optional<std::string_view> ParseString(std::string_view* benc);
|
|
|
|
|
|
|
|
} // namespace impl
|
|
|
|
|
|
|
|
struct Handler
|
|
|
|
{
|
2022-01-28 22:46:14 +00:00
|
|
|
class Context
|
|
|
|
{
|
|
|
|
public:
|
2023-11-04 16:39:41 +00:00
|
|
|
constexpr Context(char const* stream_begin_in, tr_error& error_in)
|
2022-01-28 22:46:14 +00:00
|
|
|
: error{ error_in }
|
|
|
|
, stream_begin_{ stream_begin_in }
|
|
|
|
{
|
|
|
|
}
|
2023-07-16 00:02:39 +00:00
|
|
|
|
|
|
|
[[nodiscard]] constexpr std::pair<long, long> tokenSpan() const
|
2022-01-28 22:46:14 +00:00
|
|
|
{
|
2023-07-16 00:02:39 +00:00
|
|
|
return { token_begin_ - stream_begin_, token_end_ - stream_begin_ };
|
2022-01-28 22:46:14 +00:00
|
|
|
}
|
2023-07-16 00:02:39 +00:00
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto raw() const
|
2022-01-28 22:46:14 +00:00
|
|
|
{
|
|
|
|
return std::string_view{ token_begin_, size_t(token_end_ - token_begin_) };
|
|
|
|
}
|
2023-07-16 00:02:39 +00:00
|
|
|
|
|
|
|
constexpr void setTokenSpan(char const* a, size_t len)
|
2022-01-28 22:46:14 +00:00
|
|
|
{
|
|
|
|
token_begin_ = a;
|
|
|
|
token_end_ = token_begin_ + len;
|
|
|
|
}
|
2023-07-16 00:02:39 +00:00
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
tr_error& error;
|
2022-01-28 22:46:14 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
char const* token_begin_ = nullptr;
|
|
|
|
char const* token_end_ = nullptr;
|
|
|
|
char const* const stream_begin_;
|
|
|
|
};
|
|
|
|
|
2022-01-25 01:05:24 +00:00
|
|
|
virtual ~Handler() = default;
|
|
|
|
|
2022-01-28 22:46:14 +00:00
|
|
|
virtual bool Int64(int64_t, Context const& context) = 0;
|
|
|
|
virtual bool String(std::string_view, Context const& context) = 0;
|
2022-01-24 06:30:00 +00:00
|
|
|
|
2022-01-28 22:46:14 +00:00
|
|
|
virtual bool StartDict(Context const& context) = 0;
|
|
|
|
virtual bool Key(std::string_view, Context const& context) = 0;
|
|
|
|
virtual bool EndDict(Context const& context) = 0;
|
2022-01-24 06:30:00 +00:00
|
|
|
|
2022-01-28 22:46:14 +00:00
|
|
|
virtual bool StartArray(Context const& context) = 0;
|
|
|
|
virtual bool EndArray(Context const& context) = 0;
|
2022-01-24 06:30:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<std::size_t MaxDepth>
|
|
|
|
struct BasicHandler : public Handler
|
|
|
|
{
|
2022-01-28 22:46:14 +00:00
|
|
|
bool Int64(int64_t /*value*/, Context const& /*context*/) override
|
2022-01-24 06:30:00 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-01-28 22:46:14 +00:00
|
|
|
bool String(std::string_view /*value*/, Context const& /*context*/) override
|
2022-01-24 06:30:00 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-01-28 22:46:14 +00:00
|
|
|
bool StartDict(Context const& /*context*/) override
|
2022-01-24 06:30:00 +00:00
|
|
|
{
|
2022-01-28 18:39:45 +00:00
|
|
|
push();
|
2022-01-24 06:30:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-01-28 22:46:14 +00:00
|
|
|
bool Key(std::string_view key, Context const& /*context*/) override
|
2022-01-24 06:30:00 +00:00
|
|
|
{
|
2022-01-28 18:39:45 +00:00
|
|
|
keys_[depth_] = key;
|
2022-01-24 06:30:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-01-28 22:46:14 +00:00
|
|
|
bool EndDict(Context const& /*context*/) override
|
2022-01-24 06:30:00 +00:00
|
|
|
{
|
2022-01-28 18:39:45 +00:00
|
|
|
pop();
|
2022-01-24 06:30:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-01-28 22:46:14 +00:00
|
|
|
bool StartArray(Context const& /*context*/) override
|
2022-01-24 06:30:00 +00:00
|
|
|
{
|
2022-01-28 18:39:45 +00:00
|
|
|
push();
|
2022-01-24 06:30:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-01-28 22:46:14 +00:00
|
|
|
bool EndArray(Context const& /*context*/) override
|
2022-01-24 06:30:00 +00:00
|
|
|
{
|
2022-01-28 18:39:45 +00:00
|
|
|
pop();
|
2022-01-24 06:30:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-08-31 04:17:23 +00:00
|
|
|
constexpr auto key(size_t i) const
|
2022-01-28 18:39:45 +00:00
|
|
|
{
|
|
|
|
return keys_[i];
|
|
|
|
}
|
|
|
|
|
2022-08-31 04:17:23 +00:00
|
|
|
constexpr auto depth() const
|
2022-01-28 18:39:45 +00:00
|
|
|
{
|
|
|
|
return depth_;
|
|
|
|
}
|
|
|
|
|
2022-08-31 04:17:23 +00:00
|
|
|
constexpr auto currentKey() const
|
2022-01-28 18:39:45 +00:00
|
|
|
{
|
|
|
|
return key(depth());
|
|
|
|
}
|
|
|
|
|
2022-05-24 04:05:16 +00:00
|
|
|
protected:
|
|
|
|
[[nodiscard]] std::string path() const
|
|
|
|
{
|
|
|
|
auto ret = std::string{};
|
|
|
|
for (size_t i = 0; i <= depth(); ++i)
|
|
|
|
{
|
|
|
|
ret += '[';
|
|
|
|
ret += key(i);
|
|
|
|
ret += ']';
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-01-28 18:39:45 +00:00
|
|
|
private:
|
2022-04-02 00:48:09 +00:00
|
|
|
constexpr void push() noexcept
|
2022-01-28 18:39:45 +00:00
|
|
|
{
|
|
|
|
++depth_;
|
|
|
|
keys_[depth_] = {};
|
|
|
|
}
|
|
|
|
|
2022-04-02 00:48:09 +00:00
|
|
|
constexpr void pop() noexcept
|
2022-01-28 18:39:45 +00:00
|
|
|
{
|
|
|
|
--depth_;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t depth_ = 0;
|
|
|
|
std::array<std::string_view, MaxDepth> keys_;
|
2022-01-24 06:30:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<std::size_t MaxDepth>
|
|
|
|
struct ParserStack
|
|
|
|
{
|
|
|
|
enum class ParentType
|
|
|
|
{
|
|
|
|
Array,
|
|
|
|
Dict
|
|
|
|
};
|
|
|
|
struct Node
|
|
|
|
{
|
|
|
|
ParentType parent_type;
|
|
|
|
size_t n_children_walked;
|
|
|
|
};
|
|
|
|
std::array<Node, MaxDepth> stack;
|
|
|
|
std::size_t depth = 0;
|
|
|
|
|
2022-04-02 00:48:09 +00:00
|
|
|
constexpr void clear() noexcept
|
2022-01-24 06:30:00 +00:00
|
|
|
{
|
|
|
|
depth = 0;
|
|
|
|
}
|
|
|
|
|
2022-04-06 21:39:39 +00:00
|
|
|
[[nodiscard]] constexpr auto empty() const noexcept
|
|
|
|
{
|
|
|
|
return depth == 0;
|
|
|
|
}
|
|
|
|
|
2022-04-02 00:48:09 +00:00
|
|
|
constexpr void tokenWalked()
|
2022-01-24 06:30:00 +00:00
|
|
|
{
|
|
|
|
++stack[depth].n_children_walked;
|
|
|
|
}
|
|
|
|
|
2022-04-02 00:48:09 +00:00
|
|
|
[[nodiscard]] constexpr Node& current()
|
2022-01-24 06:30:00 +00:00
|
|
|
{
|
|
|
|
return stack[depth];
|
|
|
|
}
|
2022-04-02 00:48:09 +00:00
|
|
|
|
|
|
|
[[nodiscard]] constexpr Node& current() const
|
2022-01-24 06:30:00 +00:00
|
|
|
{
|
|
|
|
return stack[depth];
|
|
|
|
}
|
|
|
|
|
2022-04-02 00:48:09 +00:00
|
|
|
[[nodiscard]] constexpr bool expectingDictKey() const
|
2022-01-24 06:30:00 +00:00
|
|
|
{
|
|
|
|
return depth > 0 && stack[depth].parent_type == ParentType::Dict && (stack[depth].n_children_walked % 2) == 0;
|
|
|
|
}
|
|
|
|
|
2022-08-31 04:17:23 +00:00
|
|
|
constexpr std::optional<ParentType> parentType() const
|
2022-01-24 06:30:00 +00:00
|
|
|
{
|
|
|
|
if (depth == 0)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return stack[depth].parent_type;
|
|
|
|
}
|
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
std::optional<ParentType> pop(tr_error& error)
|
2022-01-24 06:30:00 +00:00
|
|
|
{
|
|
|
|
if (depth == 0)
|
|
|
|
{
|
2023-11-04 16:39:41 +00:00
|
|
|
error.set(EILSEQ, "Cannot pop empty stack");
|
2022-01-24 06:30:00 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stack[depth].parent_type == ParentType::Dict && ((stack[depth].n_children_walked % 2) != 0))
|
|
|
|
{
|
2023-11-04 16:39:41 +00:00
|
|
|
error.set(EILSEQ, "Premature end-of-dict found. Malformed benc?");
|
2022-01-24 06:30:00 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto const ret = stack[depth].parent_type;
|
|
|
|
--depth;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
bool push(ParentType parent_type, tr_error& error)
|
2022-01-24 06:30:00 +00:00
|
|
|
{
|
|
|
|
if (depth + 1 >= std::size(stack))
|
|
|
|
{
|
2023-11-04 16:39:41 +00:00
|
|
|
error.set(E2BIG, "Max stack depth reached; unable to continue parsing");
|
2022-01-24 06:30:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
++depth;
|
|
|
|
current() = { parent_type, 0 };
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<size_t MaxDepth>
|
|
|
|
bool parse(
|
|
|
|
std::string_view benc,
|
|
|
|
ParserStack<MaxDepth>& stack,
|
|
|
|
Handler& handler,
|
|
|
|
char const** setme_end = nullptr,
|
2023-11-04 16:39:41 +00:00
|
|
|
tr_error* error = nullptr)
|
2022-01-24 06:30:00 +00:00
|
|
|
{
|
2023-11-04 16:39:41 +00:00
|
|
|
// ensure `error` isn't a nullptr
|
|
|
|
auto local_error = tr_error{};
|
|
|
|
if (error == nullptr)
|
|
|
|
{
|
|
|
|
error = &local_error;
|
|
|
|
}
|
|
|
|
|
2022-01-24 06:30:00 +00:00
|
|
|
stack.clear();
|
2022-01-31 03:12:57 +00:00
|
|
|
auto const* const stream_begin = std::data(benc);
|
2023-11-04 16:39:41 +00:00
|
|
|
auto context = Handler::Context{ stream_begin, *error };
|
2022-01-24 06:30:00 +00:00
|
|
|
|
|
|
|
int err = 0;
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
if (std::empty(benc))
|
|
|
|
{
|
|
|
|
err = EILSEQ;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err != 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-01-28 22:46:14 +00:00
|
|
|
auto const* const front = std::data(benc);
|
2022-01-24 06:30:00 +00:00
|
|
|
switch (benc.front())
|
|
|
|
{
|
|
|
|
case 'i': // int
|
2022-01-25 01:05:24 +00:00
|
|
|
if (auto const value = impl::ParseInt(&benc); !value)
|
|
|
|
{
|
2022-01-31 03:12:57 +00:00
|
|
|
err = EILSEQ;
|
2023-11-04 16:39:41 +00:00
|
|
|
error->set(err, "Malformed benc? Unable to parse integer");
|
2022-01-25 01:05:24 +00:00
|
|
|
}
|
|
|
|
else
|
2022-01-24 06:30:00 +00:00
|
|
|
{
|
2022-01-28 22:46:14 +00:00
|
|
|
context.setTokenSpan(front, std::data(benc) - front);
|
|
|
|
|
|
|
|
if (!handler.Int64(*value, context))
|
|
|
|
{
|
|
|
|
err = ECANCELED;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stack.tokenWalked();
|
|
|
|
}
|
2022-01-24 06:30:00 +00:00
|
|
|
}
|
2022-01-25 01:05:24 +00:00
|
|
|
break;
|
|
|
|
|
2022-01-24 06:30:00 +00:00
|
|
|
case 'l': // list
|
|
|
|
case 'd': // dict
|
|
|
|
{
|
2023-11-04 16:39:41 +00:00
|
|
|
bool ok = benc.front() == 'l' ? stack.push(ParserStack<MaxDepth>::ParentType::Array, *error) :
|
|
|
|
stack.push(ParserStack<MaxDepth>::ParentType::Dict, *error);
|
2022-01-24 06:30:00 +00:00
|
|
|
if (!ok)
|
|
|
|
{
|
|
|
|
err = EILSEQ;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-01-28 22:46:14 +00:00
|
|
|
context.setTokenSpan(front, 1);
|
|
|
|
ok = benc.front() == 'l' ? handler.StartArray(context) : handler.StartDict(context);
|
2022-01-24 06:30:00 +00:00
|
|
|
if (!ok)
|
|
|
|
{
|
|
|
|
err = ECANCELED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
benc.remove_prefix(1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'e': // end of list or dict
|
2022-01-25 01:05:24 +00:00
|
|
|
benc.remove_prefix(1);
|
2022-01-24 06:30:00 +00:00
|
|
|
|
2023-11-04 16:39:41 +00:00
|
|
|
if (auto const parent_type = stack.pop(*error); !parent_type)
|
2022-01-25 01:05:24 +00:00
|
|
|
{
|
|
|
|
err = EILSEQ;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-01-24 06:30:00 +00:00
|
|
|
stack.tokenWalked();
|
2022-01-28 22:46:14 +00:00
|
|
|
context.setTokenSpan(front, 1);
|
2022-01-24 06:30:00 +00:00
|
|
|
|
2022-01-28 22:46:14 +00:00
|
|
|
if (auto const ok = *parent_type == ParserStack<MaxDepth>::ParentType::Array ? handler.EndArray(context) :
|
|
|
|
handler.EndDict(context);
|
2022-01-25 01:05:24 +00:00
|
|
|
!ok)
|
2022-01-24 06:30:00 +00:00
|
|
|
{
|
|
|
|
err = ECANCELED;
|
|
|
|
}
|
|
|
|
}
|
2022-01-25 01:05:24 +00:00
|
|
|
break;
|
2022-01-24 06:30:00 +00:00
|
|
|
|
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
case '8':
|
|
|
|
case '9': // string
|
2022-01-25 01:05:24 +00:00
|
|
|
if (auto const sv = impl::ParseString(&benc); !sv)
|
|
|
|
{
|
|
|
|
err = EILSEQ;
|
2023-11-04 16:39:41 +00:00
|
|
|
error->set(err, "Malformed benc? Unable to parse string");
|
2022-01-25 01:05:24 +00:00
|
|
|
}
|
|
|
|
else
|
2022-01-24 06:30:00 +00:00
|
|
|
{
|
2022-01-28 22:46:14 +00:00
|
|
|
context.setTokenSpan(front, std::data(benc) - front);
|
|
|
|
if (bool const ok = stack.expectingDictKey() ? handler.Key(*sv, context) : handler.String(*sv, context); !ok)
|
|
|
|
{
|
|
|
|
err = ECANCELED;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stack.tokenWalked();
|
|
|
|
}
|
2022-01-24 06:30:00 +00:00
|
|
|
}
|
2022-01-25 01:05:24 +00:00
|
|
|
break;
|
2022-01-24 06:30:00 +00:00
|
|
|
|
|
|
|
default: // invalid bencoded text... march past it
|
|
|
|
benc.remove_prefix(1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stack.depth == 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-17 16:02:45 +00:00
|
|
|
if (setme_end != nullptr)
|
|
|
|
{
|
|
|
|
*setme_end = std::data(benc);
|
|
|
|
}
|
|
|
|
|
2022-01-24 06:30:00 +00:00
|
|
|
if (err != 0)
|
|
|
|
{
|
|
|
|
errno = err;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stack.depth != 0)
|
|
|
|
{
|
|
|
|
err = EILSEQ;
|
2023-11-04 16:39:41 +00:00
|
|
|
error->set(err, "premature end-of-data reached");
|
2022-01-24 06:30:00 +00:00
|
|
|
errno = err;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stack.stack[0].n_children_walked == 0)
|
|
|
|
{
|
|
|
|
err = EILSEQ;
|
2023-11-04 16:39:41 +00:00
|
|
|
error->set(err, "no bencoded data to parse");
|
2022-01-24 06:30:00 +00:00
|
|
|
errno = err;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace transmission::benc
|