2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2008-2022 Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// 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-10-14 15:36:49 +00:00
|
|
|
#include <array>
|
2021-10-17 20:17:18 +00:00
|
|
|
#include <cctype>
|
|
|
|
#include <cerrno> /* EILSEQ, EINVAL */
|
|
|
|
#include <cmath> /* fabs() */
|
2022-07-27 21:53:39 +00:00
|
|
|
#include <cstdlib>
|
2021-10-17 20:17:18 +00:00
|
|
|
#include <cstring>
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <deque>
|
|
|
|
#include <string_view>
|
2022-07-26 02:45:54 +00:00
|
|
|
#include <utility>
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2022-02-14 17:54:32 +00:00
|
|
|
#define UTF_CPP_CPLUSPLUS 201703L
|
2021-11-30 21:13:56 +00:00
|
|
|
#include <utf8.h>
|
2022-02-14 17:54:32 +00:00
|
|
|
|
|
|
|
#include <event2/buffer.h>
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2022-04-03 18:23:00 +00:00
|
|
|
#include <fmt/compile.h>
|
|
|
|
#include <fmt/format.h>
|
2022-03-14 04:43:35 +00:00
|
|
|
|
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
|
|
|
|
2022-04-06 21:39:39 +00:00
|
|
|
#include "error.h"
|
2020-08-11 18:11:55 +00:00
|
|
|
#include "jsonsl.h"
|
2013-01-25 23:34:20 +00:00
|
|
|
#include "log.h"
|
2022-01-13 02:13:58 +00:00
|
|
|
#include "quark.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 */
|
2022-01-17 18:39:50 +00:00
|
|
|
static auto constexpr MaxDepth = int{ 64 };
|
2012-12-14 04:34:42 +00:00
|
|
|
|
|
|
|
struct json_wrapper_data
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
bool has_content;
|
2022-04-06 21:39:39 +00:00
|
|
|
size_t size;
|
2021-11-18 05:37:35 +00:00
|
|
|
std::string_view key;
|
|
|
|
evbuffer* keybuf;
|
|
|
|
evbuffer* strbuf;
|
2022-04-06 21:39:39 +00:00
|
|
|
tr_error* 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. */
|
2022-01-17 18:39:50 +00:00
|
|
|
std::array<size_t, MaxDepth> 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
|
|
|
|
2022-04-06 21:39:39 +00:00
|
|
|
tr_error_set(
|
|
|
|
&data->error,
|
|
|
|
EILSEQ,
|
|
|
|
fmt::format(
|
|
|
|
_("Couldn't parse JSON at position {position} '{text}': {error} ({error_code})"),
|
|
|
|
fmt::arg("position", jsn->pos),
|
|
|
|
fmt::arg("text", std::string_view{ buf, std::min(size_t{ 16U }, data->size - jsn->pos) }),
|
|
|
|
fmt::arg("error", jsonsl_strerror(error)),
|
|
|
|
fmt::arg("error_code", error)));
|
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
|
|
|
{
|
2022-04-06 21:39:39 +00:00
|
|
|
auto* const data = static_cast<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
|
|
|
|
2022-06-02 00:51:09 +00:00
|
|
|
size_t const depth = std::size(data->stack);
|
2022-01-17 18:39:50 +00:00
|
|
|
size_t const n = depth < MaxDepth ? data->preallocGuess[depth] : 0;
|
2019-11-12 01:37:05 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-04-03 18:23:00 +00:00
|
|
|
static std::string_view extract_escaped_string(char const* in, size_t in_len, 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':
|
2022-04-02 22:42:51 +00:00
|
|
|
if (in_end - in >= 6)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2022-04-02 22:42:51 +00:00
|
|
|
unsigned int val = 0;
|
2012-12-15 00:01:59 +00:00
|
|
|
|
2022-04-02 22:42:51 +00:00
|
|
|
if (decode_hex_string(in, &val))
|
|
|
|
{
|
|
|
|
try
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2022-04-02 22:42:51 +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));
|
|
|
|
}
|
|
|
|
catch (utf8::exception const&)
|
|
|
|
{ // invalid codepoint
|
|
|
|
evbuffer_add(buf, "?", 1);
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
2022-04-02 22:42:51 +00:00
|
|
|
unescaped = true;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-04-03 18:23:00 +00:00
|
|
|
static std::pair<std::string_view, bool> extract_string(jsonsl_t jsn, struct jsonsl_state_st* state, 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);
|
2022-04-02 22:42:51 +00:00
|
|
|
auto const* const v = data->stack.back();
|
2021-09-28 13:56:46 +00:00
|
|
|
data->stack.pop_back();
|
2022-01-17 18:39:50 +00:00
|
|
|
if (depth < MaxDepth)
|
2019-11-12 01:37:05 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2022-05-16 05:06:17 +00:00
|
|
|
auto sv = std::string_view{ jsn->base + state->pos_begin, jsn->pos - state->pos_begin };
|
|
|
|
auto const val = tr_parseNum<double>(sv);
|
|
|
|
tr_variantInitReal(get_node(jsn), val ? *val : double{});
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
bool tr_variantParseJson(tr_variant& setme, int parse_opts, std::string_view json, char const** setme_end, tr_error** error)
|
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
|
|
|
|
2022-01-17 18:39:50 +00:00
|
|
|
jsonsl_t jsn = jsonsl_new(MaxDepth);
|
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);
|
|
|
|
|
2022-04-06 21:39:39 +00:00
|
|
|
data.error = nullptr;
|
2022-08-03 06:15:37 +00:00
|
|
|
data.size = std::size(json);
|
2017-04-19 12:04:45 +00:00
|
|
|
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 */
|
2022-08-03 06:15:37 +00:00
|
|
|
jsonsl_feed(jsn, static_cast<jsonsl_char_t const*>(std::data(json)), std::size(json));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* EINVAL if there was no content */
|
2022-04-06 21:39:39 +00:00
|
|
|
if (data.error == nullptr && !data.has_content)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-04-06 21:39:39 +00:00
|
|
|
tr_error_set(&data.error, EINVAL, "No content");
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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
|
|
|
{
|
2022-08-03 06:15:37 +00:00
|
|
|
*setme_end = std::data(json) + jsn->pos;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* cleanup */
|
2022-04-06 21:39:39 +00:00
|
|
|
auto const success = data.error == nullptr;
|
|
|
|
if (data.error != nullptr)
|
|
|
|
{
|
|
|
|
tr_error_propagate(error, &data.error);
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
evbuffer_free(data.keybuf);
|
|
|
|
evbuffer_free(data.strbuf);
|
|
|
|
jsonsl_destroy(jsn);
|
2022-04-06 21:39:39 +00:00
|
|
|
return success;
|
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;
|
2022-04-03 18:23:00 +00:00
|
|
|
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
|
|
|
{
|
2022-08-25 01:19:21 +00:00
|
|
|
static auto buf = std::array<char, 1024>{};
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-08-25 01:19:21 +00:00
|
|
|
if (buf.front() == '\0')
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2022-08-25 01:19:21 +00:00
|
|
|
memset(std::data(buf), ' ', std::size(buf));
|
2017-04-19 12:04:45 +00:00
|
|
|
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)
|
|
|
|
{
|
2022-08-25 01:19:21 +00:00
|
|
|
evbuffer_add(data->out, std::data(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:
|
2022-04-02 22:42:51 +00:00
|
|
|
++pstate.childIndex;
|
|
|
|
if (bool const is_last = pstate.childIndex == pstate.childCount; !is_last)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2022-04-02 22:42:51 +00:00
|
|
|
evbuffer_add(data->out, ",", 1);
|
|
|
|
jsonIndent(data);
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2022-04-02 22:42:51 +00:00
|
|
|
break;
|
|
|
|
|
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
|
|
|
{
|
2022-04-03 18:23:00 +00:00
|
|
|
auto buf = std::array<char, 64>{};
|
|
|
|
auto const out = fmt::format_to(std::data(buf), FMT_COMPILE("{:d}"), val->val.i);
|
|
|
|
auto* const data = static_cast<jsonWalk*>(vdata);
|
|
|
|
evbuffer_add(data->out, std::data(buf), static_cast<size_t>(out - std::data(buf)));
|
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 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)
|
|
|
|
{
|
2022-04-03 18:23:00 +00:00
|
|
|
auto buf = std::array<char, 64>{};
|
|
|
|
auto const out = fmt::format_to(std::data(buf), FMT_COMPILE("{:.0f}"), val->val.d);
|
|
|
|
evbuffer_add(data->out, std::data(buf), static_cast<size_t>(out - std::data(buf)));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-04-03 18:23:00 +00:00
|
|
|
auto buf = std::array<char, 64>{};
|
|
|
|
auto const out = fmt::format_to(std::data(buf), FMT_COMPILE("{:.4f}"), val->val.d);
|
|
|
|
evbuffer_add(data->out, std::data(buf), static_cast<size_t>(out - std::data(buf)));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
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
|
|
|
{
|
2022-04-03 18:23:00 +00:00
|
|
|
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
|
|
|
|
2022-04-22 06:28:02 +00:00
|
|
|
evbuffer_reserve_space(data->out, std::size(sv) * 6 + 2, 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:
|
2022-02-07 04:28:36 +00:00
|
|
|
if (isprint((unsigned char)sv.front()) != 0)
|
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
|
|
|
|
{
|
2022-01-24 19:07:55 +00:00
|
|
|
auto const* const begin8 = std::data(sv);
|
|
|
|
auto const* const end8 = begin8 + std::size(sv);
|
|
|
|
auto const* walk8 = begin8;
|
2021-11-30 21:13:56 +00:00
|
|
|
auto const uch32 = utf8::next(walk8, end8);
|
2022-04-03 18:23:00 +00:00
|
|
|
outwalk = fmt::format_to_n(outwalk, outend - outwalk - 1, FMT_COMPILE("\\u{:04x}"), uch32).out;
|
2021-11-30 21:13:56 +00:00
|
|
|
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
|
|
|
{
|
2022-09-07 16:04:28 +00:00
|
|
|
size_t const n_children = 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
|
|
|
|
2022-09-07 16:04:28 +00:00
|
|
|
if (n_children != 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
|
|
|
|
2022-04-03 18:23:00 +00:00
|
|
|
void tr_variantToBufJson(tr_variant const* top, 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
|
|
|
}
|