2016-01-21 21:11:48 +01:00
|
|
|
/* JSON core and helpers */
|
|
|
|
#include "json.h"
|
|
|
|
#include <assert.h>
|
2019-01-15 04:54:27 +01:00
|
|
|
#include <bitcoin/pubkey.h>
|
2016-01-21 21:11:48 +01:00
|
|
|
#include <ccan/build_assert/build_assert.h>
|
2018-12-16 05:47:06 +01:00
|
|
|
#include <ccan/mem/mem.h>
|
2016-01-21 21:11:48 +01:00
|
|
|
#include <ccan/str/hex/hex.h>
|
|
|
|
#include <ccan/tal/str/str.h>
|
2018-12-16 05:44:06 +01:00
|
|
|
#include <common/utils.h>
|
2016-01-21 21:11:48 +01:00
|
|
|
#include <errno.h>
|
2016-01-21 21:11:48 +01:00
|
|
|
#include <inttypes.h>
|
2016-01-21 21:11:48 +01:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2018-12-16 05:48:06 +01:00
|
|
|
const char *json_tok_full(const char *buffer, const jsmntok_t *t)
|
2016-01-21 21:11:48 +01:00
|
|
|
{
|
|
|
|
if (t->type == JSMN_STRING)
|
|
|
|
return buffer + t->start - 1;
|
|
|
|
return buffer + t->start;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Include " if it's a string. */
|
2018-12-16 05:48:06 +01:00
|
|
|
int json_tok_full_len(const jsmntok_t *t)
|
2016-01-21 21:11:48 +01:00
|
|
|
{
|
|
|
|
if (t->type == JSMN_STRING)
|
|
|
|
return t->end - t->start + 2;
|
|
|
|
return t->end - t->start;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool json_tok_streq(const char *buffer, const jsmntok_t *tok, const char *str)
|
|
|
|
{
|
|
|
|
if (tok->type != JSMN_STRING)
|
|
|
|
return false;
|
|
|
|
if (tok->end - tok->start != strlen(str))
|
|
|
|
return false;
|
|
|
|
return strncmp(buffer + tok->start, str, tok->end - tok->start) == 0;
|
|
|
|
}
|
|
|
|
|
2018-12-16 05:45:06 +01:00
|
|
|
char *json_strdup(const tal_t *ctx, const char *buffer, const jsmntok_t *tok)
|
|
|
|
{
|
|
|
|
return tal_strndup(ctx, buffer + tok->start, tok->end - tok->start);
|
|
|
|
}
|
|
|
|
|
2018-08-13 18:16:41 +02:00
|
|
|
bool json_to_u64(const char *buffer, const jsmntok_t *tok,
|
2016-01-21 21:11:48 +01:00
|
|
|
uint64_t *num)
|
2016-01-21 21:11:48 +01:00
|
|
|
{
|
|
|
|
char *end;
|
2016-11-16 04:04:56 +01:00
|
|
|
unsigned long long l;
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2016-11-16 04:04:56 +01:00
|
|
|
l = strtoull(buffer + tok->start, &end, 0);
|
2016-01-21 21:11:48 +01:00
|
|
|
if (end != buffer + tok->end)
|
|
|
|
return false;
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
BUILD_ASSERT(sizeof(l) >= sizeof(*num));
|
2016-01-21 21:11:48 +01:00
|
|
|
*num = l;
|
|
|
|
|
|
|
|
/* Check for overflow */
|
2016-11-16 04:04:56 +01:00
|
|
|
if (l == ULLONG_MAX && errno == ERANGE)
|
2016-01-21 21:11:48 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (*num != l)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
|
2018-08-13 18:42:11 +02:00
|
|
|
bool json_to_double(const char *buffer, const jsmntok_t *tok, double *num)
|
2016-09-06 09:17:48 +02:00
|
|
|
{
|
|
|
|
char *end;
|
|
|
|
|
|
|
|
*num = strtod(buffer + tok->start, &end);
|
|
|
|
if (end != buffer + tok->end)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-08-10 22:16:22 +02:00
|
|
|
bool json_to_number(const char *buffer, const jsmntok_t *tok,
|
|
|
|
unsigned int *num)
|
2016-01-21 21:11:48 +01:00
|
|
|
{
|
|
|
|
uint64_t u64;
|
|
|
|
|
2018-08-13 18:16:41 +02:00
|
|
|
if (!json_to_u64(buffer, tok, &u64))
|
2016-01-21 21:11:48 +01:00
|
|
|
return false;
|
|
|
|
*num = u64;
|
|
|
|
|
|
|
|
/* Just in case it doesn't fit. */
|
|
|
|
if (*num != u64)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2018-12-16 05:46:06 +01:00
|
|
|
bool json_to_int(const char *buffer, const jsmntok_t *tok, int *num)
|
|
|
|
{
|
|
|
|
char *end;
|
|
|
|
long l;
|
|
|
|
|
|
|
|
l = strtol(buffer + tok->start, &end, 0);
|
|
|
|
if (end != buffer + tok->end)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
BUILD_ASSERT(sizeof(l) >= sizeof(*num));
|
|
|
|
*num = l;
|
|
|
|
|
|
|
|
/* Check for overflow/underflow */
|
|
|
|
if ((l == LONG_MAX || l == LONG_MIN) && errno == ERANGE)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Check for truncation */
|
|
|
|
if (*num != l)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-16 05:47:06 +01:00
|
|
|
bool json_to_bool(const char *buffer, const jsmntok_t *tok, bool *b)
|
|
|
|
{
|
|
|
|
if (tok->type != JSMN_PRIMITIVE)
|
|
|
|
return false;
|
|
|
|
if (memeqstr(buffer + tok->start, tok->end - tok->start, "true")) {
|
|
|
|
*b = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (memeqstr(buffer + tok->start, tok->end - tok->start, "false")) {
|
|
|
|
*b = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-09 16:06:34 +01:00
|
|
|
u8 *json_tok_bin_from_hex(const tal_t *ctx, const char *buffer, const jsmntok_t *tok)
|
|
|
|
{
|
|
|
|
u8 *result;
|
|
|
|
size_t hexlen, rawlen;
|
|
|
|
hexlen = tok->end - tok->start;
|
|
|
|
rawlen = hex_data_size(hexlen);
|
|
|
|
|
|
|
|
result = tal_arr(ctx, u8, rawlen);
|
|
|
|
if (!hex_decode(buffer + tok->start, hexlen, result, rawlen))
|
|
|
|
return tal_free(result);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool json_to_preimage(const char *buffer, const jsmntok_t *tok, struct preimage *preimage)
|
|
|
|
{
|
|
|
|
size_t hexlen = tok->end - tok->start;
|
|
|
|
return hex_decode(buffer + tok->start, hexlen, preimage->r, sizeof(preimage->r));
|
|
|
|
}
|
|
|
|
|
2018-08-23 23:04:38 +02:00
|
|
|
bool json_tok_is_num(const char *buffer, const jsmntok_t *tok)
|
|
|
|
{
|
|
|
|
if (tok->type != JSMN_PRIMITIVE)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (int i = tok->start; i < tok->end; i++)
|
|
|
|
if (!cisdigit(buffer[i]))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
bool json_tok_is_null(const char *buffer, const jsmntok_t *tok)
|
|
|
|
{
|
|
|
|
if (tok->type != JSMN_PRIMITIVE)
|
|
|
|
return false;
|
|
|
|
return buffer[tok->start] == 'n';
|
|
|
|
}
|
|
|
|
|
|
|
|
const jsmntok_t *json_next(const jsmntok_t *tok)
|
|
|
|
{
|
|
|
|
const jsmntok_t *t;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (t = tok + 1, i = 0; i < tok->size; i++)
|
|
|
|
t = json_next(t);
|
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
const jsmntok_t *json_get_member(const char *buffer, const jsmntok_t tok[],
|
|
|
|
const char *label)
|
|
|
|
{
|
2019-01-17 03:12:13 +01:00
|
|
|
const jsmntok_t *t;
|
|
|
|
size_t i;
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2017-10-24 04:23:43 +02:00
|
|
|
if (tok->type != JSMN_OBJECT)
|
|
|
|
return NULL;
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2019-01-17 03:12:13 +01:00
|
|
|
json_for_each_obj(i, t, tok)
|
2016-01-21 21:11:48 +01:00
|
|
|
if (json_tok_streq(buffer, t, label))
|
|
|
|
return t + 1;
|
2016-11-11 00:02:04 +01:00
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-08-31 09:49:40 +02:00
|
|
|
const jsmntok_t *json_get_arr(const jsmntok_t tok[], size_t index)
|
2016-01-21 21:11:48 +01:00
|
|
|
{
|
2019-01-17 03:12:13 +01:00
|
|
|
const jsmntok_t *t;
|
|
|
|
size_t i;
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2017-10-24 04:23:43 +02:00
|
|
|
if (tok->type != JSMN_ARRAY)
|
|
|
|
return NULL;
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2019-01-17 03:12:13 +01:00
|
|
|
json_for_each_arr(i, t, tok) {
|
2016-01-21 21:11:48 +01:00
|
|
|
if (index == 0)
|
|
|
|
return t;
|
|
|
|
index--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-12-08 01:37:56 +01:00
|
|
|
jsmntok_t *json_parse_input(const tal_t *ctx,
|
|
|
|
const char *input, int len, bool *valid)
|
2016-01-21 21:11:48 +01:00
|
|
|
{
|
|
|
|
jsmn_parser parser;
|
|
|
|
jsmntok_t *toks;
|
2018-02-05 04:23:19 +01:00
|
|
|
int ret;
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2018-12-08 01:37:56 +01:00
|
|
|
toks = tal_arr(ctx, jsmntok_t, 10);
|
2018-09-25 07:47:23 +02:00
|
|
|
toks[0].type = JSMN_UNDEFINED;
|
2016-01-21 21:11:48 +01:00
|
|
|
|
|
|
|
jsmn_init(&parser);
|
2018-09-25 07:47:23 +02:00
|
|
|
again:
|
2016-01-21 21:11:48 +01:00
|
|
|
ret = jsmn_parse(&parser, input, len, toks, tal_count(toks) - 1);
|
|
|
|
|
|
|
|
switch (ret) {
|
|
|
|
case JSMN_ERROR_INVAL:
|
|
|
|
*valid = false;
|
|
|
|
return tal_free(toks);
|
|
|
|
case JSMN_ERROR_NOMEM:
|
|
|
|
tal_resize(&toks, tal_count(toks) * 2);
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
2018-09-25 07:47:23 +02:00
|
|
|
/* Check whether we read at least one full root element, i.e., root
|
|
|
|
* element has its end set. */
|
|
|
|
if (toks[0].type == JSMN_UNDEFINED || toks[0].end == -1) {
|
|
|
|
*valid = true;
|
|
|
|
return tal_free(toks);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we read a partial element at the end of the stream we'll get a
|
|
|
|
* ret=JSMN_ERROR_PART, but due to the previous check we know we read at
|
|
|
|
* least one full element, so count tokens that are part of this root
|
|
|
|
* element. */
|
|
|
|
ret = json_next(toks) - toks;
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
/* Cut to length and return. */
|
|
|
|
*valid = true;
|
|
|
|
tal_resize(&toks, ret + 1);
|
2017-12-07 23:59:39 +01:00
|
|
|
/* Make sure last one is always referenceable. */
|
2016-01-21 21:11:48 +01:00
|
|
|
toks[ret].type = -1;
|
|
|
|
toks[ret].start = toks[ret].end = toks[ret].size = 0;
|
2016-11-11 00:02:04 +01:00
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
return toks;
|
|
|
|
}
|
2018-11-21 18:53:00 +01:00
|
|
|
|
|
|
|
const char *jsmntype_to_string(jsmntype_t t)
|
|
|
|
{
|
|
|
|
switch (t) {
|
|
|
|
case JSMN_UNDEFINED :
|
|
|
|
return "UNDEFINED";
|
|
|
|
case JSMN_OBJECT :
|
|
|
|
return "OBJECT";
|
|
|
|
case JSMN_ARRAY :
|
|
|
|
return "ARRAY";
|
|
|
|
case JSMN_STRING :
|
|
|
|
return "STRING";
|
|
|
|
case JSMN_PRIMITIVE :
|
|
|
|
return "PRIMITIVE";
|
|
|
|
}
|
|
|
|
return "INVALID";
|
|
|
|
}
|
|
|
|
|
|
|
|
void json_tok_print(const char *buffer, const jsmntok_t *tok)
|
|
|
|
{
|
|
|
|
const jsmntok_t *first = tok;
|
|
|
|
const jsmntok_t *last = json_next(tok);
|
|
|
|
printf("size: %d, count: %td\n", tok->size, last - first);
|
|
|
|
while (first != last) {
|
|
|
|
printf("%td. %.*s, %s\n", first - tok,
|
|
|
|
first->end - first->start, buffer + first->start,
|
|
|
|
jsmntype_to_string(first->type));
|
|
|
|
first++;
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
jsmntok_t *json_tok_copy(const tal_t *ctx, const jsmntok_t *tok)
|
|
|
|
{
|
2018-12-05 03:02:20 +01:00
|
|
|
return tal_dup_arr(ctx, jsmntok_t, tok, json_next(tok) - tok, 0);
|
2018-11-21 18:53:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void json_tok_remove(jsmntok_t **tokens, jsmntok_t *tok, size_t num)
|
|
|
|
{
|
|
|
|
assert(*tokens);
|
|
|
|
assert((*tokens)->type == JSMN_ARRAY || (*tokens)->type == JSMN_OBJECT);
|
|
|
|
const jsmntok_t *src = tok;
|
|
|
|
const jsmntok_t *end = json_next(*tokens);
|
|
|
|
jsmntok_t *dest = tok;
|
|
|
|
int remove_count;
|
|
|
|
|
|
|
|
for (int i = 0; i < num; i++)
|
|
|
|
src = json_next(src);
|
|
|
|
|
|
|
|
remove_count = src - tok;
|
|
|
|
|
|
|
|
memmove(dest, src, sizeof(jsmntok_t) * (end - src));
|
|
|
|
|
|
|
|
tal_resize(tokens, tal_count(*tokens) - remove_count);
|
|
|
|
(*tokens)->size -= num;
|
|
|
|
}
|
2018-12-16 05:44:06 +01:00
|
|
|
|
|
|
|
const jsmntok_t *json_delve(const char *buffer,
|
|
|
|
const jsmntok_t *tok,
|
|
|
|
const char *guide)
|
|
|
|
{
|
|
|
|
while (*guide) {
|
|
|
|
const char *key;
|
|
|
|
size_t len = strcspn(guide+1, ".[]");
|
|
|
|
|
|
|
|
key = tal_strndup(tmpctx, guide+1, len);
|
|
|
|
switch (guide[0]) {
|
|
|
|
case '.':
|
|
|
|
if (tok->type != JSMN_OBJECT)
|
|
|
|
return NULL;
|
|
|
|
tok = json_get_member(buffer, tok, key);
|
|
|
|
if (!tok)
|
|
|
|
return NULL;
|
|
|
|
break;
|
|
|
|
case '[':
|
|
|
|
if (tok->type != JSMN_ARRAY)
|
|
|
|
return NULL;
|
|
|
|
tok = json_get_arr(tok, atol(key));
|
|
|
|
if (!tok)
|
|
|
|
return NULL;
|
|
|
|
/* Must be terminated */
|
|
|
|
assert(guide[1+strlen(key)] == ']');
|
|
|
|
len++;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
guide += len + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return tok;
|
|
|
|
}
|