| 491 | } |
| 492 | |
| 493 | bool json_parse_input(jsmn_parser *parser, |
| 494 | jsmntok_t **toks, |
| 495 | const char *input, int len, |
| 496 | bool *complete) |
| 497 | { |
| 498 | int ret; |
| 499 | |
| 500 | again: |
| 501 | ret = jsmn_parse(parser, input, len, *toks, tal_count(*toks) - 1); |
| 502 | if (ret == JSMN_ERROR_INVAL) |
| 503 | return false; |
| 504 | |
| 505 | /* Check whether we read at least one full root element, i.e., root |
| 506 | * element has its end set. */ |
| 507 | if ((*toks)[0].type == JSMN_UNDEFINED || (*toks)[0].end == -1) { |
| 508 | /* If it ran out of tokens, provide more. */ |
| 509 | if (ret == JSMN_ERROR_NOMEM) { |
| 510 | tal_resize(toks, tal_count(*toks) * 2); |
| 511 | goto again; |
| 512 | } |
| 513 | /* Otherwise, must be incomplete */ |
| 514 | *complete = false; |
| 515 | return true; |
| 516 | } |
| 517 | |
| 518 | /* If we read a partial element at the end of the stream we'll get a |
| 519 | * errro, but due to the previous check we know we read at |
| 520 | * least one full element, so count tokens that are part of this root |
| 521 | * element. */ |
| 522 | ret = json_next(*toks) - *toks; |
| 523 | |
| 524 | if (!validate_jsmn_parse_output(input, *toks, *toks + ret)) |
| 525 | return false; |
| 526 | |
| 527 | /* Cut to length and return. */ |
| 528 | tal_resize(toks, ret + 1); |
| 529 | /* Make sure last one is always referenceable. */ |
| 530 | (*toks)[ret].type = -1; |
| 531 | (*toks)[ret].start = (*toks)[ret].end = (*toks)[ret].size = 0; |
| 532 | |
| 533 | *complete = true; |
| 534 | return true; |
| 535 | } |
| 536 | |
| 537 | jsmntok_t *json_parse_simple(const tal_t *ctx, const char *input, int len) |
| 538 | { |