2012-12-14 04:34:42 +00:00
|
|
|
/*
|
2014-01-19 01:09:44 +00:00
|
|
|
* This file Copyright (C) 2008-2014 Mnemosyne LLC
|
2012-12-14 04:34:42 +00:00
|
|
|
*
|
2014-01-21 03:10:30 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
2012-12-14 04:34:42 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
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>
|
|
|
|
#include <cstdlib> /* strtoul() */
|
|
|
|
#include <cstring> /* strlen(), memchr() */
|
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"
|
2012-12-24 22:38:41 +00:00
|
|
|
#include "utils.h" /* tr_snprintf() */
|
2012-12-14 04:34:42 +00:00
|
|
|
#include "variant.h"
|
|
|
|
#include "variant-common.h"
|
|
|
|
|
2017-05-16 18:37:00 +00:00
|
|
|
#define MAX_BENC_STR_LENGTH (128 * 1024 * 1024) /* arbitrary */
|
|
|
|
|
2012-12-14 04:34:42 +00:00
|
|
|
/***
|
2012-12-24 22:38:41 +00:00
|
|
|
**** tr_variantParse()
|
|
|
|
**** tr_variantLoad()
|
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"
|
|
|
|
* NOTE: The maximum number of bit of this integer is unspecified,
|
|
|
|
* but to handle it as a signed 64bit integer is mandatory to handle
|
|
|
|
* "large files" aka .torrent for more that 4Gbyte
|
|
|
|
*/
|
2020-08-11 18:11:55 +00:00
|
|
|
int tr_bencParseInt(void const* vbuf, void const* vbufend, uint8_t const** setme_end, int64_t* setme_val)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2020-08-11 18:11:55 +00:00
|
|
|
uint8_t const* const buf = (uint8_t const*)vbuf;
|
|
|
|
uint8_t const* const bufend = (uint8_t const*)vbufend;
|
2012-12-14 16:04:44 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (buf >= bufend)
|
|
|
|
{
|
|
|
|
return EILSEQ;
|
|
|
|
}
|
2012-12-14 16:04:44 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (*buf != 'i')
|
|
|
|
{
|
|
|
|
return EILSEQ;
|
|
|
|
}
|
2012-12-14 16:04:44 +00:00
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
void const* begin = buf + 1;
|
|
|
|
void const* end = memchr(begin, 'e', (bufend - buf) - 1);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (end == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return EILSEQ;
|
|
|
|
}
|
|
|
|
|
|
|
|
errno = 0;
|
2021-10-15 17:27:12 +00:00
|
|
|
char* endptr = nullptr;
|
2021-09-12 17:41:49 +00:00
|
|
|
int64_t val = evutil_strtoll(static_cast<char const*>(begin), &endptr, 10);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (errno != 0 || endptr != end) /* incomplete parse */
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return EILSEQ;
|
|
|
|
}
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (val != 0 && *(char const*)begin == '0') /* no leading zeroes! */
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return EILSEQ;
|
|
|
|
}
|
2012-12-14 16:04:44 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
*setme_end = (uint8_t const*)end + 1;
|
2017-04-19 12:04:45 +00:00
|
|
|
*setme_val = val;
|
|
|
|
return 0;
|
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"
|
|
|
|
*/
|
2021-08-15 09:41:48 +00:00
|
|
|
int tr_bencParseStr(
|
|
|
|
void const* vbuf,
|
|
|
|
void const* vbufend,
|
|
|
|
uint8_t const** setme_end,
|
|
|
|
uint8_t const** setme_str,
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t* setme_strlen)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2020-08-11 18:11:55 +00:00
|
|
|
uint8_t const* const buf = (uint8_t const*)vbuf;
|
|
|
|
uint8_t const* const bufend = (uint8_t const*)vbufend;
|
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
if ((buf < bufend) && isdigit(*buf))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
void const* end = memchr(buf, ':', bufend - buf);
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (end != nullptr)
|
2021-09-12 17:41:49 +00:00
|
|
|
{
|
|
|
|
errno = 0;
|
2021-10-15 17:27:12 +00:00
|
|
|
char* ulend = nullptr;
|
2021-09-12 17:41:49 +00:00
|
|
|
size_t len = strtoul((char const*)buf, &ulend, 10);
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
if (errno == 0 && ulend == end && len <= MAX_BENC_STR_LENGTH)
|
|
|
|
{
|
|
|
|
uint8_t const* strbegin = (uint8_t const*)end + 1;
|
|
|
|
uint8_t const* strend = strbegin + len;
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
if (strbegin <= strend && strend <= bufend)
|
|
|
|
{
|
|
|
|
*setme_end = (uint8_t const*)end + 1 + len;
|
|
|
|
*setme_str = (uint8_t const*)end + 1;
|
|
|
|
*setme_strlen = len;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
*setme_end = nullptr;
|
|
|
|
*setme_str = nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
*setme_strlen = 0;
|
|
|
|
return EILSEQ;
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2021-10-15 18:13:33 +00:00
|
|
|
static tr_variant* get_node(std::deque<tr_variant*>& stack, std::optional<tr_quark>& dict_key, tr_variant* top, int* err)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
tr_variant* node = nullptr;
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2021-09-29 00:09:01 +00:00
|
|
|
if (std::empty(stack))
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
node = top;
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2012-12-24 22:38:41 +00:00
|
|
|
{
|
2021-09-29 00:09:01 +00:00
|
|
|
auto* parent = stack.back();
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantIsList(parent))
|
2012-12-24 22:38:41 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
node = tr_variantListAdd(parent);
|
2012-12-24 22:38:41 +00:00
|
|
|
}
|
2021-10-15 18:13:33 +00:00
|
|
|
else if (dict_key && tr_variantIsDict(parent))
|
2012-12-24 22:38:41 +00:00
|
|
|
{
|
2021-10-15 18:13:33 +00:00
|
|
|
node = tr_variantDictAdd(parent, *dict_key);
|
|
|
|
dict_key.reset();
|
2012-12-24 22:38:41 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2012-12-24 22:38:41 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
*err = EILSEQ;
|
2012-12-24 22:38:41 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return node;
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function's previous recursive implementation was
|
|
|
|
* easier to read, but was vulnerable to a smash-stacking
|
|
|
|
* attack via maliciously-crafted bencoded data. (#667)
|
|
|
|
*/
|
2017-04-20 16:02:19 +00:00
|
|
|
int tr_variantParseBenc(void const* buf_in, void const* bufend_in, tr_variant* top, char const** setme_end)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int err = 0;
|
2021-09-12 17:41:49 +00:00
|
|
|
auto const* buf = static_cast<uint8_t const*>(buf_in);
|
|
|
|
auto const* const bufend = static_cast<uint8_t const*>(bufend_in);
|
2021-09-29 00:09:01 +00:00
|
|
|
auto stack = std::deque<tr_variant*>{};
|
2021-10-15 18:13:33 +00:00
|
|
|
auto key = std::optional<tr_quark>{};
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if ((buf_in == nullptr) || (bufend_in == nullptr) || (top == nullptr))
|
2020-11-04 03:23:53 +00:00
|
|
|
{
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantInit(top, 0);
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
while (buf != bufend)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (buf > bufend) /* no more text to parse... */
|
|
|
|
{
|
|
|
|
err = EILSEQ;
|
|
|
|
}
|
2012-12-24 22:38:41 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (err != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (*buf == 'i') /* int */
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2021-10-15 17:27:12 +00:00
|
|
|
uint8_t const* end = nullptr;
|
|
|
|
auto val = int64_t{};
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((err = tr_bencParseInt(buf, bufend, &end, &val)) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = end;
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2021-10-15 18:13:33 +00:00
|
|
|
tr_variant* const v = get_node(stack, key, top, &err);
|
2021-10-15 17:27:12 +00:00
|
|
|
if (v != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_variantInitInt(v, val);
|
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else if (*buf == 'l') /* list */
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
++buf;
|
2012-12-24 22:38:41 +00:00
|
|
|
|
2021-10-15 18:13:33 +00:00
|
|
|
tr_variant* const v = get_node(stack, key, top, &err);
|
2021-10-15 17:27:12 +00:00
|
|
|
if (v != nullptr)
|
2012-12-24 22:38:41 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantInitList(v, 0);
|
2021-09-29 00:09:01 +00:00
|
|
|
stack.push_back(v);
|
2012-12-24 22:38:41 +00:00
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else if (*buf == 'd') /* dict */
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
++buf;
|
2012-12-24 22:38:41 +00:00
|
|
|
|
2021-10-15 18:13:33 +00:00
|
|
|
tr_variant* const v = get_node(stack, key, top, &err);
|
2021-10-15 17:27:12 +00:00
|
|
|
if (v != nullptr)
|
2012-12-24 22:38:41 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantInitDict(v, 0);
|
2021-09-29 00:09:01 +00:00
|
|
|
stack.push_back(v);
|
2012-12-24 22:38:41 +00:00
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else if (*buf == 'e') /* end of list or dict */
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
++buf;
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2021-10-15 18:13:33 +00:00
|
|
|
if (std::empty(stack) || key)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
err = EILSEQ;
|
|
|
|
break;
|
2012-12-24 22:38:41 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-10-14 19:26:38 +00:00
|
|
|
stack.pop_back();
|
|
|
|
if (std::empty(stack))
|
|
|
|
{
|
|
|
|
break;
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else if (isdigit(*buf)) /* string? */
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2021-10-15 17:27:12 +00:00
|
|
|
uint8_t const* end = nullptr;
|
|
|
|
uint8_t const* str = nullptr;
|
|
|
|
auto str_len = size_t{};
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((err = tr_bencParseStr(buf, bufend, &end, &str, &str_len)) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
buf = end;
|
2021-10-20 02:30:50 +00:00
|
|
|
auto const sv = std::string_view{ reinterpret_cast<char const*>(str), str_len };
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-10-15 18:13:33 +00:00
|
|
|
if (!key && !std::empty(stack) && tr_variantIsDict(stack.back()))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-15 18:13:33 +00:00
|
|
|
key = tr_quark_new(sv);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2021-10-15 17:27:12 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-15 18:13:33 +00:00
|
|
|
tr_variant* const v = get_node(stack, key, top, &err);
|
2021-10-15 17:27:12 +00:00
|
|
|
if (v != nullptr)
|
|
|
|
{
|
2021-10-20 02:30:50 +00:00
|
|
|
tr_variantInitStr(v, sv);
|
2021-10-15 17:27:12 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else /* invalid bencoded text... march past it */
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
++buf;
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
2012-12-24 22:38:41 +00:00
|
|
|
|
2021-09-29 00:09:01 +00:00
|
|
|
if (std::empty(stack))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 00:09:01 +00:00
|
|
|
if (err == 0 && (top->type == 0 || !std::empty(stack)))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
err = EILSEQ;
|
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (err == 0)
|
2017-01-17 20:55:34 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (setme_end != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
*setme_end = (char const*)buf;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2017-01-17 20:55:34 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else if (top->type != 0)
|
2017-01-17 20:55:34 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantFree(top);
|
|
|
|
tr_variantInit(top, 0);
|
2017-01-17 20:55:34 +00:00
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return err;
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char buf[128];
|
2021-09-12 17:41:49 +00:00
|
|
|
int const len = tr_snprintf(buf, sizeof(buf), "%f", val->val.d);
|
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, "%d:", len);
|
|
|
|
evbuffer_add(evbuf, 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-10-15 17:27:12 +00:00
|
|
|
auto len = size_t{};
|
|
|
|
char const* str = nullptr;
|
2019-02-18 22:38:24 +00:00
|
|
|
if (!tr_variantGetStr(v, &str, &len))
|
|
|
|
{
|
|
|
|
len = 0;
|
2021-09-15 00:18:09 +00:00
|
|
|
str = nullptr;
|
2019-02-18 22:38:24 +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, "%zu:", len);
|
|
|
|
evbuffer_add(evbuf, str, len);
|
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
|
|
|
}
|