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-14 15:36:49 +00:00
|
|
|
#include <array>
|
2021-10-17 20:17:18 +00:00
|
|
|
#include <cctype>
|
2021-09-27 12:46:38 +00:00
|
|
|
#include <deque>
|
2021-10-17 20:17:18 +00:00
|
|
|
#include <cerrno> /* EILSEQ, EINVAL */
|
|
|
|
#include <cmath> /* fabs() */
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2021-11-30 21:13:56 +00:00
|
|
|
#include <utf8.h>
|
2012-12-14 04:34:42 +00:00
|
|
|
#include <event2/buffer.h> /* evbuffer_add() */
|
|
|
|
|
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-30 21:13:56 +00:00
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
#include "jsonsl.h"
|
2013-01-25 23:34:20 +00:00
|
|
|
#include "log.h"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.h"
|
2012-12-14 04:34:42 +00:00
|
|
|
#include "utils.h"
|
|
|
|
#include "variant-common.h"
|
2021-11-30 21:13:56 +00:00
|
|
|
#include "variant.h"
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2021-11-18 05:37:35 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2012-12-14 04:34:42 +00:00
|
|
|
/* arbitrary value... this is much deeper than our code goes */
|
|
|
|
#define MAX_DEPTH 64
|
|
|
|
|
|
|
|
struct json_wrapper_data
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
bool has_content;
|
2021-11-18 05:37:35 +00:00
|
|
|
std::string_view key;
|
|
|
|
evbuffer* keybuf;
|
|
|
|
evbuffer* strbuf;
|
|
|
|
int error;
|
2021-09-28 13:56:46 +00:00
|
|
|
std::deque<tr_variant*> stack;
|
2021-11-18 05:37:35 +00:00
|
|
|
tr_variant* top;
|
|
|
|
int parse_opts;
|
2019-11-12 01:37:05 +00:00
|
|
|
|
|
|
|
/* A very common pattern is for a container's children to be similar,
|
|
|
|
* e.g. they may all be objects with the same set of keys. So when
|
|
|
|
* a container is popped off the stack, remember its size to use as
|
|
|
|
* a preallocation heuristic for the next container at that depth. */
|
2021-10-14 15:36:49 +00:00
|
|
|
std::array<size_t, MAX_DEPTH> preallocGuess;
|
2012-12-14 04:34:42 +00:00
|
|
|
};
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static tr_variant* get_node(struct jsonsl_st* jsn)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* data = static_cast<struct json_wrapper_data*>(jsn->data);
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2021-09-28 13:56:46 +00:00
|
|
|
auto* parent = std::empty(data->stack) ? nullptr : data->stack.back();
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
tr_variant* node = nullptr;
|
|
|
|
if (parent == nullptr)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
node = data->top;
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else if (tr_variantIsList(parent))
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
node = tr_variantListAdd(parent);
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
2021-11-18 05:37:35 +00:00
|
|
|
else if (tr_variantIsDict(parent) && !std::empty(data->key))
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2021-11-18 05:37:35 +00:00
|
|
|
node = tr_variantDictAdd(parent, tr_quark_new(data->key));
|
|
|
|
data->key = ""sv;
|
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
|
|
|
}
|
|
|
|
|
2021-10-24 16:41:54 +00:00
|
|
|
static void error_handler(jsonsl_t jsn, jsonsl_error_t error, jsonsl_state_st* /*state*/, jsonsl_char_t const* buf)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* data = static_cast<struct json_wrapper_data*>(jsn->data);
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2021-11-17 03:29:30 +00:00
|
|
|
tr_logAddError("JSON parse failed at pos %zu: %s -- remaining text \"%.16s\"", jsn->pos, jsonsl_strerror(error), buf);
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
data->error = EILSEQ;
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static int error_callback(jsonsl_t jsn, jsonsl_error_t error, struct jsonsl_state_st* state, jsonsl_char_t* at)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
error_handler(jsn, error, state, at);
|
|
|
|
return 0; /* bail */
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2021-10-10 16:52:26 +00:00
|
|
|
static void action_callback_PUSH(
|
|
|
|
jsonsl_t jsn,
|
2021-10-24 16:41:54 +00:00
|
|
|
jsonsl_action_t /*action*/,
|
2021-10-10 16:52:26 +00:00
|
|
|
struct jsonsl_state_st* state,
|
2021-10-24 16:41:54 +00:00
|
|
|
jsonsl_char_t const* /*buf*/)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* data = static_cast<struct json_wrapper_data*>(jsn->data);
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2019-11-12 01:37:05 +00:00
|
|
|
if ((state->type == JSONSL_T_LIST) || (state->type == JSONSL_T_OBJECT))
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
|
|
|
data->has_content = true;
|
2021-09-28 13:56:46 +00:00
|
|
|
tr_variant* node = get_node(jsn);
|
|
|
|
data->stack.push_back(node);
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2021-09-28 13:56:46 +00:00
|
|
|
int const depth = std::size(data->stack);
|
2019-11-12 01:37:05 +00:00
|
|
|
size_t const n = depth < MAX_DEPTH ? data->preallocGuess[depth] : 0;
|
|
|
|
if (state->type == JSONSL_T_LIST)
|
|
|
|
{
|
|
|
|
tr_variantInitList(node, n);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_variantInitDict(node, n);
|
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-14 16:04:44 +00:00
|
|
|
/* like sscanf(in+2, "%4x", &val) but less slow */
|
2017-04-20 16:02:19 +00:00
|
|
|
static bool decode_hex_string(char const* in, unsigned int* setme)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(in != nullptr);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
unsigned int val = 0;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* const end = in + 6;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(in[0] == '\\');
|
|
|
|
TR_ASSERT(in[1] == 'u');
|
2017-04-19 12:04:45 +00:00
|
|
|
in += 2;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
val <<= 4;
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ('0' <= *in && *in <= '9')
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-04-30 16:30:03 +00:00
|
|
|
val += *in - '0';
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2017-04-30 16:25:26 +00:00
|
|
|
else if ('a' <= *in && *in <= 'f')
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2019-03-17 04:07:48 +00:00
|
|
|
val += *in - 'a' + 10U;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2017-04-30 16:25:26 +00:00
|
|
|
else if ('A' <= *in && *in <= 'F')
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2019-03-17 04:07:48 +00:00
|
|
|
val += *in - 'A' + 10U;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2021-08-15 09:41:48 +00:00
|
|
|
} while (++in != end);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
*setme = val;
|
|
|
|
return true;
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2021-11-18 05:37:35 +00:00
|
|
|
static std::string_view extract_escaped_string(char const* in, size_t in_len, struct evbuffer* buf)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* const in_end = in + in_len;
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
evbuffer_drain(buf, evbuffer_get_length(buf));
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
while (in < in_end)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
bool unescaped = false;
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (*in == '\\' && in_end - in >= 2)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (in[1])
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'b':
|
|
|
|
evbuffer_add(buf, "\b", 1);
|
|
|
|
in += 2;
|
|
|
|
unescaped = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f':
|
|
|
|
evbuffer_add(buf, "\f", 1);
|
|
|
|
in += 2;
|
|
|
|
unescaped = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'n':
|
|
|
|
evbuffer_add(buf, "\n", 1);
|
|
|
|
in += 2;
|
|
|
|
unescaped = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'r':
|
|
|
|
evbuffer_add(buf, "\r", 1);
|
|
|
|
in += 2;
|
|
|
|
unescaped = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 't':
|
|
|
|
evbuffer_add(buf, "\t", 1);
|
|
|
|
in += 2;
|
|
|
|
unescaped = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '/':
|
|
|
|
evbuffer_add(buf, "/", 1);
|
|
|
|
in += 2;
|
|
|
|
unescaped = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '"':
|
|
|
|
evbuffer_add(buf, "\"", 1);
|
|
|
|
in += 2;
|
|
|
|
unescaped = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '\\':
|
|
|
|
evbuffer_add(buf, "\\", 1);
|
|
|
|
in += 2;
|
|
|
|
unescaped = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'u':
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (in_end - in >= 6)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
unsigned int val = 0;
|
2012-12-15 00:01:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (decode_hex_string(in, &val))
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2021-11-30 21:13:56 +00:00
|
|
|
try
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2021-11-30 21:13:56 +00:00
|
|
|
auto buf8 = std::array<char, 8>{};
|
|
|
|
auto const it = utf8::append(val, std::data(buf8));
|
|
|
|
evbuffer_add(buf, std::data(buf8), it - std::data(buf8));
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
2021-12-01 23:11:57 +00:00
|
|
|
catch (utf8::exception const&)
|
2021-11-30 21:13:56 +00:00
|
|
|
{ // invalid codepoint
|
|
|
|
evbuffer_add(buf, "?", 1);
|
|
|
|
}
|
|
|
|
unescaped = true;
|
2017-04-19 12:04:45 +00:00
|
|
|
in += 6;
|
|
|
|
break;
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!unescaped)
|
2012-12-15 00:01:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
evbuffer_add(buf, in, 1);
|
|
|
|
++in;
|
2012-12-15 00:01:59 +00:00
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2021-11-18 05:37:35 +00:00
|
|
|
return { (char const*)evbuffer_pullup(buf, -1), evbuffer_get_length(buf) };
|
2012-12-15 00:01:59 +00:00
|
|
|
}
|
|
|
|
|
2021-11-18 05:37:35 +00:00
|
|
|
static std::pair<std::string_view, bool> extract_string(jsonsl_t jsn, struct jsonsl_state_st* state, struct evbuffer* buf)
|
2012-12-15 00:01:59 +00:00
|
|
|
{
|
2021-11-18 05:37:35 +00:00
|
|
|
// figure out where the string is
|
2021-10-15 17:27:12 +00:00
|
|
|
char const* in_begin = jsn->base + state->pos_begin;
|
2017-04-19 12:04:45 +00:00
|
|
|
if (*in_begin == '"')
|
2012-12-15 00:01:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
in_begin++;
|
2012-12-15 00:01:59 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-10-15 17:27:12 +00:00
|
|
|
char const* const in_end = jsn->base + state->pos_cur;
|
|
|
|
size_t const in_len = in_end - in_begin;
|
2021-09-15 00:18:09 +00:00
|
|
|
if (memchr(in_begin, '\\', in_len) == nullptr)
|
2012-12-15 00:01:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
/* it's not escaped */
|
2021-11-18 05:37:35 +00:00
|
|
|
return std::make_pair(std::string_view{ in_begin, in_len }, true);
|
2012-12-15 00:01:59 +00:00
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2021-11-18 05:37:35 +00:00
|
|
|
return std::make_pair(extract_escaped_string(in_begin, in_len, buf), false);
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2021-10-10 16:52:26 +00:00
|
|
|
static void action_callback_POP(
|
|
|
|
jsonsl_t jsn,
|
2021-10-24 16:41:54 +00:00
|
|
|
jsonsl_action_t /*action*/,
|
2021-10-10 16:52:26 +00:00
|
|
|
struct jsonsl_state_st* state,
|
2021-10-24 16:41:54 +00:00
|
|
|
jsonsl_char_t const* /*buf*/)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* data = static_cast<struct json_wrapper_data*>(jsn->data);
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (state->type == JSONSL_T_STRING)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2021-11-18 05:37:35 +00:00
|
|
|
auto const [str, inplace] = extract_string(jsn, state, data->strbuf);
|
|
|
|
if (inplace && ((data->parse_opts & TR_VARIANT_PARSE_INPLACE) != 0))
|
|
|
|
{
|
|
|
|
tr_variantInitStrView(get_node(jsn), str);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_variantInitStr(get_node(jsn), str);
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
data->has_content = true;
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else if (state->type == JSONSL_T_HKEY)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
data->has_content = true;
|
2021-11-18 05:37:35 +00:00
|
|
|
auto const [key, inplace] = extract_string(jsn, state, data->keybuf);
|
|
|
|
data->key = key;
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
2017-04-30 16:25:26 +00:00
|
|
|
else if (state->type == JSONSL_T_LIST || state->type == JSONSL_T_OBJECT)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2021-09-28 13:56:46 +00:00
|
|
|
int const depth = std::size(data->stack);
|
|
|
|
auto* v = data->stack.back();
|
|
|
|
data->stack.pop_back();
|
2019-11-12 01:37:05 +00:00
|
|
|
if (depth < MAX_DEPTH)
|
|
|
|
{
|
|
|
|
data->preallocGuess[depth] = v->val.l.count;
|
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else if (state->type == JSONSL_T_SPECIAL)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((state->special_flags & JSONSL_SPECIALf_NUMNOINT) != 0)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* begin = jsn->base + state->pos_begin;
|
2017-04-19 12:04:45 +00:00
|
|
|
data->has_content = true;
|
2021-09-15 00:18:09 +00:00
|
|
|
tr_variantInitReal(get_node(jsn), strtod(begin, nullptr));
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
2017-04-30 16:25:26 +00:00
|
|
|
else if ((state->special_flags & JSONSL_SPECIALf_NUMERIC) != 0)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* begin = jsn->base + state->pos_begin;
|
2017-04-19 12:04:45 +00:00
|
|
|
data->has_content = true;
|
2021-11-17 03:29:30 +00:00
|
|
|
tr_variantInitInt(get_node(jsn), std::strtoll(begin, nullptr, 10));
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
2017-04-30 16:25:26 +00:00
|
|
|
else if ((state->special_flags & JSONSL_SPECIALf_BOOLEAN) != 0)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
bool const b = (state->special_flags & JSONSL_SPECIALf_TRUE) != 0;
|
2017-04-19 12:04:45 +00:00
|
|
|
data->has_content = true;
|
|
|
|
tr_variantInitBool(get_node(jsn), b);
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
2017-04-30 16:25:26 +00:00
|
|
|
else if ((state->special_flags & JSONSL_SPECIALf_NULL) != 0)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
data->has_content = true;
|
|
|
|
tr_variantInitQuark(get_node(jsn), TR_KEY_NONE);
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-18 05:37:35 +00:00
|
|
|
int tr_variantParseJson(tr_variant& setme, int parse_opts, std::string_view benc, char const** setme_end)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2021-11-18 05:37:35 +00:00
|
|
|
TR_ASSERT((parse_opts & TR_VARIANT_PARSE_JSON) != 0);
|
|
|
|
|
2021-10-15 17:27:12 +00:00
|
|
|
auto data = json_wrapper_data{};
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-10-15 17:27:12 +00:00
|
|
|
jsonsl_t jsn = jsonsl_new(MAX_DEPTH);
|
2017-04-19 12:04:45 +00:00
|
|
|
jsn->action_callback_PUSH = action_callback_PUSH;
|
|
|
|
jsn->action_callback_POP = action_callback_POP;
|
|
|
|
jsn->error_callback = error_callback;
|
|
|
|
jsn->data = &data;
|
|
|
|
jsonsl_enable_all_callbacks(jsn);
|
|
|
|
|
|
|
|
data.error = 0;
|
|
|
|
data.has_content = false;
|
2021-11-18 05:37:35 +00:00
|
|
|
data.key = ""sv;
|
2017-04-19 12:04:45 +00:00
|
|
|
data.keybuf = evbuffer_new();
|
2021-11-18 05:37:35 +00:00
|
|
|
data.parse_opts = parse_opts;
|
2021-10-14 15:36:49 +00:00
|
|
|
data.preallocGuess = {};
|
2021-11-18 05:37:35 +00:00
|
|
|
data.stack = {};
|
|
|
|
data.strbuf = evbuffer_new();
|
|
|
|
data.top = &setme;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* parse it */
|
2021-11-17 03:29:30 +00:00
|
|
|
jsonsl_feed(jsn, static_cast<jsonsl_char_t const*>(std::data(benc)), std::size(benc));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* EINVAL if there was no content */
|
2017-04-30 16:25:26 +00:00
|
|
|
if (data.error == 0 && !data.has_content)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
data.error = EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* maybe set the end ptr */
|
2021-09-15 00:18:09 +00:00
|
|
|
if (setme_end != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-17 03:29:30 +00:00
|
|
|
*setme_end = std::data(benc) + jsn->pos;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* cleanup */
|
2021-10-15 17:27:12 +00:00
|
|
|
int const error = data.error;
|
2017-04-19 12:04:45 +00:00
|
|
|
evbuffer_free(data.keybuf);
|
|
|
|
evbuffer_free(data.strbuf);
|
|
|
|
jsonsl_destroy(jsn);
|
|
|
|
return error;
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
|
|
|
struct ParentState
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int variantType;
|
|
|
|
int childIndex;
|
|
|
|
int childCount;
|
2012-12-14 04:34:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct jsonWalk
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
bool doIndent;
|
2021-09-27 12:46:38 +00:00
|
|
|
std::deque<ParentState> parents;
|
2017-04-19 12:04:45 +00:00
|
|
|
struct evbuffer* out;
|
2012-12-14 04:34:42 +00:00
|
|
|
};
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void jsonIndent(struct jsonWalk* data)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
static char buf[1024] = { '\0' };
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (*buf == '\0')
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
memset(buf, ' ', sizeof(buf));
|
|
|
|
buf[0] = '\n';
|
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 (data->doIndent)
|
|
|
|
{
|
2021-09-27 12:46:38 +00:00
|
|
|
evbuffer_add(data->out, buf, std::size(data->parents) * 4 + 1);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void jsonChildFunc(struct jsonWalk* data)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2021-09-27 12:46:38 +00:00
|
|
|
if (!std::empty(data->parents))
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2021-09-27 12:46:38 +00:00
|
|
|
auto& pstate = data->parents.back();
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2021-09-27 12:46:38 +00:00
|
|
|
switch (pstate.variantType)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_VARIANT_TYPE_DICT:
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2021-09-27 12:46:38 +00:00
|
|
|
int const i = pstate.childIndex;
|
|
|
|
++pstate.childIndex;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (i % 2 == 0)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
evbuffer_add(data->out, ": ", data->doIndent ? 2 : 1);
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2021-09-27 12:46:38 +00:00
|
|
|
bool const is_last = pstate.childIndex == pstate.childCount;
|
|
|
|
if (!is_last)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
evbuffer_add(data->out, ",", 1);
|
|
|
|
jsonIndent(data);
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
case TR_VARIANT_TYPE_LIST:
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2021-09-27 12:46:38 +00:00
|
|
|
++pstate.childIndex;
|
|
|
|
bool const is_last = pstate.childIndex == pstate.childCount;
|
|
|
|
if (!is_last)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
evbuffer_add(data->out, ",", 1);
|
|
|
|
jsonIndent(data);
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
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
|
|
|
default:
|
2012-12-14 04:34:42 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void jsonPushParent(struct jsonWalk* data, tr_variant const* v)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2021-09-27 12:46:38 +00:00
|
|
|
int const n_children = tr_variantIsDict(v) ? v->val.l.count * 2 : v->val.l.count;
|
|
|
|
data->parents.push_back({ v->type, 0, n_children });
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void jsonPopParent(struct jsonWalk* data)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2021-09-27 12:46:38 +00:00
|
|
|
data->parents.pop_back();
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void jsonIntFunc(tr_variant const* val, void* vdata)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* data = static_cast<struct jsonWalk*>(vdata);
|
2017-04-19 12:04:45 +00:00
|
|
|
evbuffer_add_printf(data->out, "%" PRId64, val->val.i);
|
|
|
|
jsonChildFunc(data);
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void jsonBoolFunc(tr_variant const* val, void* vdata)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* data = static_cast<struct jsonWalk*>(vdata);
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (val->val.b)
|
|
|
|
{
|
|
|
|
evbuffer_add(data->out, "true", 4);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
evbuffer_add(data->out, "false", 5);
|
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
jsonChildFunc(data);
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void jsonRealFunc(tr_variant const* val, void* vdata)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* data = static_cast<struct jsonWalk*>(vdata);
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (fabs(val->val.d - (int)val->val.d) < 0.00001)
|
|
|
|
{
|
|
|
|
evbuffer_add_printf(data->out, "%d", (int)val->val.d);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
evbuffer_add_printf(data->out, "%.4f", tr_truncd(val->val.d, 4));
|
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
jsonChildFunc(data);
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void jsonStringFunc(tr_variant const* val, void* vdata)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
struct evbuffer_iovec vec[1];
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* data = static_cast<struct jsonWalk*>(vdata);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-11-05 06:29:19 +00:00
|
|
|
auto sv = std::string_view{};
|
|
|
|
(void)!tr_variantGetStrView(val, &sv);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-11-05 06:29:19 +00:00
|
|
|
evbuffer_reserve_space(data->out, std::size(sv) * 4, vec, 1);
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* out = static_cast<char*>(vec[0].iov_base);
|
2020-11-09 03:31:02 +00:00
|
|
|
char const* const outend = out + vec[0].iov_len;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
char* outwalk = out;
|
2017-04-19 12:04:45 +00:00
|
|
|
*outwalk++ = '"';
|
|
|
|
|
2021-11-26 00:14:25 +00:00
|
|
|
for (; !std::empty(sv); sv.remove_prefix(1))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-26 00:14:25 +00:00
|
|
|
switch (sv.front())
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case '\b':
|
|
|
|
*outwalk++ = '\\';
|
|
|
|
*outwalk++ = 'b';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '\f':
|
|
|
|
*outwalk++ = '\\';
|
|
|
|
*outwalk++ = 'f';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '\n':
|
|
|
|
*outwalk++ = '\\';
|
|
|
|
*outwalk++ = 'n';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '\r':
|
|
|
|
*outwalk++ = '\\';
|
|
|
|
*outwalk++ = 'r';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '\t':
|
|
|
|
*outwalk++ = '\\';
|
|
|
|
*outwalk++ = 't';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '"':
|
|
|
|
*outwalk++ = '\\';
|
|
|
|
*outwalk++ = '"';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '\\':
|
|
|
|
*outwalk++ = '\\';
|
|
|
|
*outwalk++ = '\\';
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2021-11-26 00:14:25 +00:00
|
|
|
if (isprint(sv.front()))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-26 00:14:25 +00:00
|
|
|
*outwalk++ = sv.front();
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-30 21:13:56 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
auto* begin8 = std::data(sv);
|
|
|
|
auto* end8 = begin8 + std::size(sv);
|
|
|
|
auto* walk8 = begin8;
|
|
|
|
auto const uch32 = utf8::next(walk8, end8);
|
|
|
|
outwalk += tr_snprintf(outwalk, outend - outwalk, "\\u%04x", uch32);
|
|
|
|
sv.remove_prefix(walk8 - begin8 - 1);
|
|
|
|
}
|
2021-12-01 23:11:57 +00:00
|
|
|
catch (utf8::exception const&)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-30 21:13:56 +00:00
|
|
|
*outwalk++ = '?';
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
*outwalk++ = '"';
|
|
|
|
vec[0].iov_len = outwalk - out;
|
|
|
|
evbuffer_commit_space(data->out, vec, 1);
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
jsonChildFunc(data);
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void jsonDictBeginFunc(tr_variant const* val, void* vdata)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* data = static_cast<struct jsonWalk*>(vdata);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
jsonPushParent(data, val);
|
|
|
|
evbuffer_add(data->out, "{", 1);
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (val->val.l.count != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
jsonIndent(data);
|
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void jsonListBeginFunc(tr_variant const* val, void* vdata)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
size_t const nChildren = tr_variantListSize(val);
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* data = static_cast<struct jsonWalk*>(vdata);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
jsonPushParent(data, val);
|
|
|
|
evbuffer_add(data->out, "[", 1);
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (nChildren != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
jsonIndent(data);
|
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void jsonContainerEndFunc(tr_variant const* val, void* vdata)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* data = static_cast<struct jsonWalk*>(vdata);
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
jsonPopParent(data);
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2021-11-17 03:29:30 +00:00
|
|
|
jsonIndent(data);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (tr_variantIsDict(val))
|
|
|
|
{
|
|
|
|
evbuffer_add(data->out, "}", 1);
|
|
|
|
}
|
|
|
|
else /* list */
|
|
|
|
{
|
|
|
|
evbuffer_add(data->out, "]", 1);
|
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
jsonChildFunc(data);
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
static struct VariantWalkFuncs const walk_funcs = {
|
|
|
|
jsonIntFunc, //
|
|
|
|
jsonBoolFunc, //
|
|
|
|
jsonRealFunc, //
|
|
|
|
jsonStringFunc, //
|
|
|
|
jsonDictBeginFunc, //
|
|
|
|
jsonListBeginFunc, //
|
|
|
|
jsonContainerEndFunc, //
|
2017-04-19 12:04:45 +00:00
|
|
|
};
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_variantToBufJson(tr_variant const* top, struct evbuffer* buf, bool lean)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
struct jsonWalk data;
|
2012-12-17 02:43:17 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
data.doIndent = !lean;
|
|
|
|
data.out = buf;
|
2012-12-17 02:43:17 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantWalk(top, &walk_funcs, &data, true);
|
2012-12-17 02:43:17 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (evbuffer_get_length(buf) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
evbuffer_add_printf(buf, "\n");
|
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|