|
@ -148,6 +148,16 @@ const jsmntok_t *json_next(const jsmntok_t *tok) |
|
|
return t; |
|
|
return t; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const jsmntok_t *json_last_toks(const jsmntok_t *tok) |
|
|
|
|
|
{ |
|
|
|
|
|
const jsmntok_t *t = tok; |
|
|
|
|
|
for (size_t i=0; i<tok->size; i++) { |
|
|
|
|
|
t++; |
|
|
|
|
|
t = json_last_toks(t); |
|
|
|
|
|
} |
|
|
|
|
|
return t; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
const jsmntok_t *json_get_member(const char *buffer, const jsmntok_t tok[], |
|
|
const jsmntok_t *json_get_member(const char *buffer, const jsmntok_t tok[], |
|
|
const char *label) |
|
|
const char *label) |
|
|
{ |
|
|
{ |
|
@ -187,13 +197,11 @@ jsmntok_t *json_parse_input(const char *input, int len, bool *valid) |
|
|
jsmntok_t *toks; |
|
|
jsmntok_t *toks; |
|
|
int ret; |
|
|
int ret; |
|
|
|
|
|
|
|
|
/* Zero out so we can count elements correctly even on incomplete reads
|
|
|
toks = tal_arr(input, jsmntok_t, 10); |
|
|
* (when jsmn_parse returns -1). This results in all toks being of type |
|
|
toks[0].type = JSMN_UNDEFINED; |
|
|
* JSMN_UNDEFINED which we can recognize. */ |
|
|
|
|
|
toks = tal_arrz(input, jsmntok_t, 10); |
|
|
|
|
|
|
|
|
|
|
|
again: |
|
|
|
|
|
jsmn_init(&parser); |
|
|
jsmn_init(&parser); |
|
|
|
|
|
again: |
|
|
ret = jsmn_parse(&parser, input, len, toks, tal_count(toks) - 1); |
|
|
ret = jsmn_parse(&parser, input, len, toks, tal_count(toks) - 1); |
|
|
|
|
|
|
|
|
switch (ret) { |
|
|
switch (ret) { |
|
@ -201,7 +209,7 @@ again: |
|
|
*valid = false; |
|
|
*valid = false; |
|
|
return tal_free(toks); |
|
|
return tal_free(toks); |
|
|
case JSMN_ERROR_NOMEM: |
|
|
case JSMN_ERROR_NOMEM: |
|
|
tal_resizez(&toks, tal_count(toks) * 2); |
|
|
tal_resize(&toks, tal_count(toks) * 2); |
|
|
goto again; |
|
|
goto again; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -216,10 +224,7 @@ again: |
|
|
* ret=JSMN_ERROR_PART, but due to the previous check we know we read at |
|
|
* 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 |
|
|
* least one full element, so count tokens that are part of this root |
|
|
* element. */ |
|
|
* element. */ |
|
|
for (ret=0; ret < tal_count(toks)-1; ret++) { |
|
|
ret = json_last_toks(toks) - toks + 1; |
|
|
if (toks[ret].type == JSMN_UNDEFINED || toks[ret].start >= toks[0].end) |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* Cut to length and return. */ |
|
|
/* Cut to length and return. */ |
|
|
*valid = true; |
|
|
*valid = true; |
|
|