| 538 | } |
| 539 | |
| 540 | bool json_parse_input(jsmn_parser *parser, |
| 541 | jsmntok_t **toks, |
| 542 | const char *input, int len, |
| 543 | bool *complete) |
| 544 | { |
| 545 | int ret; |
| 546 | |
| 547 | again: |
| 548 | ret = jsmn_parse(parser, input, len, *toks, tal_count(*toks) - 1); |
| 549 | if (ret == JSMN_ERROR_INVAL) |
| 550 | return false; |
| 551 | |
| 552 | /* Check whether we read at least one full root element, i.e., root |
| 553 | * element has its end set. */ |
| 554 | if ((*toks)[0].type == JSMN_UNDEFINED || (*toks)[0].end == -1) { |
| 555 | /* If it ran out of tokens, provide more. */ |
| 556 | if (ret == JSMN_ERROR_NOMEM) { |
| 557 | tal_resize(toks, tal_count(*toks) * 2); |
| 558 | goto again; |
| 559 | } |
| 560 | /* Otherwise, must be incomplete */ |
| 561 | *complete = false; |
| 562 | return true; |
| 563 | } |
| 564 | |
| 565 | /* If we read a partial element at the end of the stream we'll get a |
| 566 | * errro, but due to the previous check we know we read at |
| 567 | * least one full element, so count tokens that are part of this root |
| 568 | * element. Bound the nesting depth here, before any recursive walk. */ |
| 569 | size_t datumlen; |
| 570 | if (!bounded_datum_len(*toks, &datumlen)) |
| 571 | return false; |
| 572 | ret = datumlen; |
| 573 | |
| 574 | if (!validate_jsmn_parse_output(input, *toks, *toks + ret)) |
| 575 | return false; |
| 576 | |
| 577 | /* Cut to length and return. */ |
| 578 | tal_resize(toks, ret + 1); |
| 579 | /* Make sure last one is always referenceable. */ |
| 580 | (*toks)[ret].type = -1; |
| 581 | (*toks)[ret].start = (*toks)[ret].end = (*toks)[ret].size = 0; |
| 582 | |
| 583 | *complete = true; |
| 584 | return true; |
| 585 | } |
| 586 | |
| 587 | jsmntok_t *json_parse_simple(const tal_t *ctx, const char *input, int len) |
| 588 | { |