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
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <math.h> /* fabs() */
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h> /* EILSEQ, EINVAL */
|
|
|
|
|
|
|
|
#include <event2/buffer.h> /* evbuffer_add() */
|
2017-04-21 07:40:57 +00:00
|
|
|
#include <event2/util.h> /* evutil_strtoll() */
|
2012-12-14 04:34:42 +00:00
|
|
|
|
|
|
|
#define JSONSL_STATE_USER_FIELDS /* no fields */
|
|
|
|
#include "jsonsl.h"
|
|
|
|
#include "jsonsl.c"
|
|
|
|
|
2015-05-04 19:58:34 +00:00
|
|
|
#define __LIBTRANSMISSION_VARIANT_MODULE__
|
2012-12-14 04:34:42 +00:00
|
|
|
#include "transmission.h"
|
|
|
|
#include "ConvertUTF.h"
|
|
|
|
#include "list.h"
|
2013-01-25 23:34:20 +00:00
|
|
|
#include "log.h"
|
2012-12-14 04:34:42 +00:00
|
|
|
#include "ptrarray.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.h"
|
|
|
|
#include "variant-common.h"
|
|
|
|
|
|
|
|
/* 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
|
|
|
int error;
|
|
|
|
bool has_content;
|
|
|
|
tr_variant* top;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* key;
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t keylen;
|
|
|
|
struct evbuffer* keybuf;
|
|
|
|
struct evbuffer* strbuf;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* source;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_ptrArray stack;
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* parent;
|
|
|
|
tr_variant* node = NULL;
|
|
|
|
struct json_wrapper_data* data = jsn->data;
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
parent = tr_ptrArrayEmpty(&data->stack) ? NULL : tr_ptrArrayBack(&data->stack);
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (parent == NULL)
|
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
|
|
|
}
|
2017-04-30 16:25:26 +00:00
|
|
|
else if (tr_variantIsDict(parent) && data->key != NULL)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
node = tr_variantDictAdd(parent, tr_quark_new(data->key, data->keylen));
|
2012-12-15 00:01:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
data->key = NULL;
|
|
|
|
data->keylen = 0;
|
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
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void error_handler(jsonsl_t jsn, jsonsl_error_t error, struct jsonsl_state_st* state UNUSED, jsonsl_char_t const* buf)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
struct json_wrapper_data* data = jsn->data;
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (data->source != NULL)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_logAddError("JSON parse failed in %s at pos %zu: %s -- remaining text \"%.16s\"", data->source, jsn->pos,
|
|
|
|
jsonsl_strerror(error), buf);
|
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
|
|
|
{
|
2017-04-19 12:04:45 +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
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void action_callback_PUSH(jsonsl_t jsn, jsonsl_action_t action UNUSED, struct jsonsl_state_st* state,
|
2017-04-20 16:02:19 +00:00
|
|
|
jsonsl_char_t const* buf UNUSED)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* node;
|
|
|
|
struct json_wrapper_data* data = jsn->data;
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (state->type)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case JSONSL_T_LIST:
|
2012-12-14 04:34:42 +00:00
|
|
|
data->has_content = true;
|
2017-04-19 12:04:45 +00:00
|
|
|
node = get_node(jsn);
|
|
|
|
tr_variantInitList(node, 0);
|
|
|
|
tr_ptrArrayAppend(&data->stack, node);
|
2012-12-14 04:34:42 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case JSONSL_T_OBJECT:
|
2012-12-14 04:34:42 +00:00
|
|
|
data->has_content = true;
|
2017-04-19 12:04:45 +00:00
|
|
|
node = get_node(jsn);
|
|
|
|
tr_variantInitDict(node, 0);
|
|
|
|
tr_ptrArrayAppend(&data->stack, node);
|
2012-12-14 04:34:42 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
default:
|
2012-12-14 04:34:42 +00:00
|
|
|
/* nothing else interesting on push */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2017-06-13 02:24:09 +00:00
|
|
|
TR_ASSERT(in != NULL);
|
|
|
|
|
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
|
|
|
{
|
2017-04-30 16:30:03 +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
|
|
|
{
|
2017-04-30 16:30:03 +00:00
|
|
|
val += *in - 'A' + 10u;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (++in != end);
|
|
|
|
|
|
|
|
*setme = val;
|
|
|
|
return true;
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char* extract_escaped_string(char const* in, size_t in_len, size_t* 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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
UTF32 str32_buf[2] = { val, 0 };
|
2017-04-20 16:02:19 +00:00
|
|
|
UTF32 const* str32_walk = str32_buf;
|
|
|
|
UTF32 const* str32_end = str32_buf + 1;
|
2017-04-19 12:04:45 +00:00
|
|
|
UTF8 str8_buf[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
|
|
|
|
UTF8* str8_walk = str8_buf;
|
|
|
|
UTF8* str8_end = str8_buf + 8;
|
|
|
|
|
|
|
|
if (ConvertUTF32toUTF8(&str32_walk, str32_end, &str8_walk, str8_end, 0) == 0)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
size_t const len = str8_walk - str8_buf;
|
2017-04-19 12:04:45 +00:00
|
|
|
evbuffer_add(buf, str8_buf, len);
|
|
|
|
unescaped = true;
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
2015-01-02 11:15:31 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
*len = evbuffer_get_length(buf);
|
|
|
|
return (char*)evbuffer_pullup(buf, -1);
|
2012-12-15 00:01:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char const* extract_string(jsonsl_t jsn, struct jsonsl_state_st* state, size_t* len, struct evbuffer* buf)
|
2012-12-15 00:01:59 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* ret;
|
|
|
|
char const* in_begin;
|
|
|
|
char const* in_end;
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t in_len;
|
2012-12-15 00:01:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* figure out where the string is */
|
|
|
|
in_begin = jsn->base + state->pos_begin;
|
2012-12-15 00:01:59 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
in_end = jsn->base + state->pos_cur;
|
|
|
|
in_len = in_end - in_begin;
|
|
|
|
|
|
|
|
if (memchr(in_begin, '\\', in_len) == NULL)
|
2012-12-15 00:01:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
/* it's not escaped */
|
|
|
|
ret = in_begin;
|
|
|
|
*len = in_len;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = extract_escaped_string(in_begin, in_len, len, buf);
|
2012-12-15 00:01:59 +00:00
|
|
|
}
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2012-12-14 04:34:42 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void action_callback_POP(jsonsl_t jsn, jsonsl_action_t action UNUSED, struct jsonsl_state_st* state,
|
2017-04-20 16:02:19 +00:00
|
|
|
jsonsl_char_t const* buf UNUSED)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
struct json_wrapper_data* 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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t len;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* str = extract_string(jsn, state, &len, data->strbuf);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantInitStr(get_node(jsn), str, len);
|
|
|
|
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;
|
|
|
|
data->key = extract_string(jsn, state, &data->keylen, data->keybuf);
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_ptrArrayPop(&data->stack);
|
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;
|
|
|
|
tr_variantInitReal(get_node(jsn), strtod(begin, NULL));
|
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;
|
|
|
|
tr_variantInitInt(get_node(jsn), evutil_strtoll(begin, NULL, 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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
int tr_jsonParse(char const* source, void const* vbuf, size_t len, tr_variant* setme_variant, char const** setme_end)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int error;
|
|
|
|
jsonsl_t jsn;
|
|
|
|
struct json_wrapper_data data;
|
|
|
|
|
|
|
|
jsn = jsonsl_new(MAX_DEPTH);
|
|
|
|
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;
|
|
|
|
data.key = NULL;
|
|
|
|
data.top = setme_variant;
|
|
|
|
data.stack = TR_PTR_ARRAY_INIT;
|
|
|
|
data.source = source;
|
|
|
|
data.keybuf = evbuffer_new();
|
|
|
|
data.strbuf = evbuffer_new();
|
|
|
|
|
|
|
|
/* parse it */
|
|
|
|
jsonsl_feed(jsn, vbuf, len);
|
|
|
|
|
|
|
|
/* 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 */
|
2017-04-30 16:25:26 +00:00
|
|
|
if (setme_end != NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
*setme_end = ((char const*)vbuf) + jsn->pos;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
error = data.error;
|
|
|
|
evbuffer_free(data.keybuf);
|
|
|
|
evbuffer_free(data.strbuf);
|
|
|
|
tr_ptrArrayDestruct(&data.stack, NULL);
|
|
|
|
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;
|
|
|
|
tr_list* parents;
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
evbuffer_add(data->out, buf, tr_list_size(data->parents) * 4 + 1);
|
|
|
|
}
|
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
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
if (data->parents != NULL && data->parents->data != NULL)
|
2012-12-14 04:34:42 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
struct ParentState* pstate = data->parents->data;
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +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
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
int const i = 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
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
bool const isLast = pstate->childIndex == pstate->childCount;
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!isLast)
|
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
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
bool const isLast = ++pstate->childIndex == pstate->childCount;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (!isLast)
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
struct ParentState* pstate = tr_new(struct ParentState, 1);
|
2012-12-14 04:34:42 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
pstate->variantType = v->type;
|
|
|
|
pstate->childIndex = 0;
|
|
|
|
pstate->childCount = v->val.l.count;
|
2012-12-24 22:38:41 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantIsDict(v))
|
|
|
|
{
|
|
|
|
pstate->childCount *= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_list_prepend(&data->parents, pstate);
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(tr_list_pop_front(&data->parents));
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
struct jsonWalk* data = vdata;
|
|
|
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
struct jsonWalk* data = 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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
struct jsonWalk* data = 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
|
|
|
char* out;
|
|
|
|
char* outwalk;
|
|
|
|
char* outend;
|
|
|
|
struct evbuffer_iovec vec[1];
|
|
|
|
struct jsonWalk* data = vdata;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* str;
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t len;
|
2017-04-20 16:02:19 +00:00
|
|
|
unsigned char const* it;
|
|
|
|
unsigned char const* end;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
tr_variantGetStr(val, &str, &len);
|
2017-04-20 16:02:19 +00:00
|
|
|
it = (unsigned char const*)str;
|
2017-04-19 12:04:45 +00:00
|
|
|
end = it + len;
|
|
|
|
|
|
|
|
evbuffer_reserve_space(data->out, len * 4, vec, 1);
|
|
|
|
out = vec[0].iov_base;
|
|
|
|
outend = out + vec[0].iov_len;
|
|
|
|
|
|
|
|
outwalk = out;
|
|
|
|
*outwalk++ = '"';
|
|
|
|
|
|
|
|
for (; it != end; ++it)
|
|
|
|
{
|
|
|
|
switch (*it)
|
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:
|
|
|
|
if (isprint(*it))
|
|
|
|
{
|
2012-12-14 04:34:42 +00:00
|
|
|
*outwalk++ = *it;
|
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
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
UTF8 const* tmp = it;
|
2012-12-14 04:34:42 +00:00
|
|
|
UTF32 buf[1] = { 0 };
|
2017-04-19 12:04:45 +00:00
|
|
|
UTF32* u32 = buf;
|
|
|
|
ConversionResult result = ConvertUTF8toUTF32(&tmp, end, &u32, buf + 1, 0);
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if ((result == conversionOK || result == targetExhausted) && tmp != it)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
outwalk += tr_snprintf(outwalk, outend - outwalk, "\\u%04x", (unsigned int)buf[0]);
|
2012-12-14 04:34:42 +00:00
|
|
|
it = tmp - 1;
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
struct jsonWalk* data = vdata;
|
|
|
|
|
|
|
|
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);
|
2017-04-19 12:04:45 +00:00
|
|
|
struct jsonWalk* data = vdata;
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
struct jsonWalk* data = vdata;
|
|
|
|
bool emptyContainer = false;
|
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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!emptyContainer)
|
|
|
|
{
|
|
|
|
jsonIndent(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static struct VariantWalkFuncs const walk_funcs =
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
jsonIntFunc,
|
|
|
|
jsonBoolFunc,
|
|
|
|
jsonRealFunc,
|
|
|
|
jsonStringFunc,
|
|
|
|
jsonDictBeginFunc,
|
|
|
|
jsonListBeginFunc,
|
|
|
|
jsonContainerEndFunc
|
|
|
|
};
|
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;
|
|
|
|
data.parents = NULL;
|
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
|
|
|
}
|