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
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2012-12-24 22:38:41 +00:00
|
|
|
#include <ctype.h> /* isdigit() */
|
2012-12-14 04:34:42 +00:00
|
|
|
#include <errno.h>
|
2013-02-04 21:07:24 +00:00
|
|
|
#include <stdlib.h> /* strtoul() */
|
2012-12-24 22:38:41 +00:00
|
|
|
#include <string.h> /* strlen(), memchr() */
|
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"
|
|
|
|
#include "ptrarray.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;
|
2017-04-19 12:04:45 +00:00
|
|
|
char* endptr;
|
2017-04-20 16:02:19 +00:00
|
|
|
void const* begin;
|
|
|
|
void const* end;
|
2017-04-19 12:04:45 +00:00
|
|
|
int64_t val;
|
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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
begin = buf + 1;
|
|
|
|
end = memchr(begin, 'e', (bufend - buf) - 1);
|
|
|
|
|
|
|
|
if (end == NULL)
|
|
|
|
{
|
|
|
|
return EILSEQ;
|
|
|
|
}
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
val = evutil_strtoll(begin, &endptr, 10);
|
|
|
|
|
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"
|
|
|
|
*/
|
2020-08-11 18:11:55 +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;
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void const* end;
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t len;
|
|
|
|
char* ulend;
|
2017-04-20 16:02:19 +00:00
|
|
|
uint8_t const* strbegin;
|
|
|
|
uint8_t const* strend;
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (buf >= bufend)
|
|
|
|
{
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isdigit(*buf))
|
|
|
|
{
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
end = memchr(buf, ':', bufend - buf);
|
|
|
|
|
|
|
|
if (end == NULL)
|
|
|
|
{
|
|
|
|
goto err;
|
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
errno = 0;
|
2017-04-20 16:02:19 +00:00
|
|
|
len = strtoul((char const*)buf, &ulend, 10);
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-05-16 18:37:00 +00:00
|
|
|
if (errno != 0 || ulend != end || len > MAX_BENC_STR_LENGTH)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
goto err;
|
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
strbegin = (uint8_t const*)end + 1;
|
2017-04-19 12:04:45 +00:00
|
|
|
strend = strbegin + len;
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (strend < strbegin || strend > bufend)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
goto err;
|
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
*setme_end = (uint8_t const*)end + 1 + len;
|
|
|
|
*setme_str = (uint8_t const*)end + 1;
|
2017-04-19 12:04:45 +00:00
|
|
|
*setme_strlen = len;
|
|
|
|
return 0;
|
2014-06-29 01:44:22 +00:00
|
|
|
|
|
|
|
err:
|
2017-04-19 12:04:45 +00:00
|
|
|
*setme_end = NULL;
|
|
|
|
*setme_str = NULL;
|
|
|
|
*setme_strlen = 0;
|
|
|
|
return EILSEQ;
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static tr_variant* get_node(tr_ptrArray* stack, tr_quark* key, tr_variant* top, int* err)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* node = NULL;
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_ptrArrayEmpty(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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* parent = tr_ptrArrayBack(stack);
|
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
|
|
|
}
|
2017-04-30 16:25:26 +00:00
|
|
|
else if (*key != 0 && tr_variantIsDict(parent))
|
2012-12-24 22:38:41 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
node = tr_variantDictAdd(parent, *key);
|
|
|
|
*key = 0;
|
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;
|
2017-04-20 16:02:19 +00:00
|
|
|
uint8_t const* buf = buf_in;
|
|
|
|
uint8_t const* bufend = bufend_in;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_ptrArray stack = TR_PTR_ARRAY_INIT;
|
|
|
|
tr_quark key = 0;
|
2012-12-14 04:34:42 +00:00
|
|
|
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int64_t val;
|
2017-04-20 16:02:19 +00:00
|
|
|
uint8_t const* end;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* v;
|
2012-12-14 04:34:42 +00:00
|
|
|
|
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
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((v = get_node(&stack, &key, top, &err)) != NULL)
|
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
|
|
|
tr_variant* v;
|
2012-12-14 16:04:44 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
++buf;
|
2012-12-24 22:38:41 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((v = get_node(&stack, &key, top, &err)) != NULL)
|
2012-12-24 22:38:41 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantInitList(v, 0);
|
|
|
|
tr_ptrArrayAppend(&stack, 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
|
|
|
tr_variant* v;
|
2012-12-14 16:04:44 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
++buf;
|
2012-12-24 22:38:41 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((v = get_node(&stack, &key, top, &err)) != NULL)
|
2012-12-24 22:38:41 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantInitDict(v, 0);
|
|
|
|
tr_ptrArrayAppend(&stack, 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
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (tr_ptrArrayEmpty(&stack) || key != 0)
|
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
|
|
|
else
|
2012-12-24 22:38:41 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_ptrArrayPop(&stack);
|
|
|
|
|
|
|
|
if (tr_ptrArrayEmpty(&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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* v;
|
2017-04-20 16:02:19 +00:00
|
|
|
uint8_t const* end;
|
|
|
|
uint8_t const* str;
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t str_len;
|
2012-12-14 04:34:42 +00:00
|
|
|
|
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;
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (key == 0 && !tr_ptrArrayEmpty(&stack) && tr_variantIsDict(tr_ptrArrayBack(&stack)))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
key = tr_quark_new(str, str_len);
|
|
|
|
}
|
2017-04-30 16:25:26 +00:00
|
|
|
else if ((v = get_node(&stack, &key, top, &err)) != NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_variantInitStr(v, str, str_len);
|
|
|
|
}
|
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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_ptrArrayEmpty(&stack))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (err == 0 && (top->type == 0 || !tr_ptrArrayEmpty(&stack)))
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (setme_end != NULL)
|
|
|
|
{
|
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
|
|
|
tr_ptrArrayDestruct(&stack, NULL);
|
|
|
|
return err;
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void saveIntFunc(tr_variant const* val, void* evbuf)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void saveBoolFunc(tr_variant const* val, void* evbuf)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void saveRealFunc(tr_variant const* val, void* evbuf)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int len;
|
|
|
|
char buf[128];
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
len = tr_snprintf(buf, sizeof(buf), "%f", val->val.d);
|
|
|
|
evbuffer_add_printf(evbuf, "%d:", len);
|
|
|
|
evbuffer_add(evbuf, buf, len);
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void saveStringFunc(tr_variant const* v, void* evbuf)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t len;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* str;
|
2019-02-18 22:38:24 +00:00
|
|
|
if (!tr_variantGetStr(v, &str, &len))
|
|
|
|
{
|
|
|
|
len = 0;
|
|
|
|
str = NULL;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-08-18 10:36:10 +00:00
|
|
|
static void saveDictBeginFunc(tr_variant const* val, void* evbuf)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2020-08-18 10:36:10 +00:00
|
|
|
TR_UNUSED(val);
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
evbuffer_add(evbuf, "d", 1);
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 10:36:10 +00:00
|
|
|
static void saveListBeginFunc(tr_variant const* val, void* evbuf)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2020-08-18 10:36:10 +00:00
|
|
|
TR_UNUSED(val);
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
evbuffer_add(evbuf, "l", 1);
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 10:36:10 +00:00
|
|
|
static void saveContainerEndFunc(tr_variant const* val, void* evbuf)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2020-08-18 10:36:10 +00:00
|
|
|
TR_UNUSED(val);
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
evbuffer_add(evbuf, "e", 1);
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static struct VariantWalkFuncs const walk_funcs =
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
saveIntFunc,
|
|
|
|
saveBoolFunc,
|
|
|
|
saveRealFunc,
|
|
|
|
saveStringFunc,
|
|
|
|
saveDictBeginFunc,
|
|
|
|
saveListBeginFunc,
|
|
|
|
saveContainerEndFunc
|
|
|
|
};
|
|
|
|
|
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
|
|
|
}
|