| 448 | } |
| 449 | |
| 450 | bool json_parse_input(jsmn_parser *parser, |
| 451 | jsmntok_t **toks, |
| 452 | const char *input, int len, |
| 453 | bool *complete) |
| 454 | { |
| 455 | int ret; |
| 456 | |
| 457 | again: |
| 458 | ret = jsmn_parse(parser, input, len, *toks, tal_count(*toks) - 1); |
| 459 | |
| 460 | switch (ret) { |
| 461 | case JSMN_ERROR_INVAL: |
| 462 | return false; |
| 463 | case JSMN_ERROR_NOMEM: |
| 464 | tal_resize(toks, tal_count(*toks) * 2); |
| 465 | goto again; |
| 466 | } |
| 467 | |
| 468 | /* Check whether we read at least one full root element, i.e., root |
| 469 | * element has its end set. */ |
| 470 | if ((*toks)[0].type == JSMN_UNDEFINED || (*toks)[0].end == -1) { |
| 471 | *complete = false; |
| 472 | return true; |
| 473 | } |
| 474 | |
| 475 | /* If we read a partial element at the end of the stream we'll get a |
| 476 | * ret=JSMN_ERROR_PART, but due to the previous check we know we read at |
| 477 | * least one full element, so count tokens that are part of this root |
| 478 | * element. */ |
| 479 | ret = json_next(*toks) - *toks; |
| 480 | |
| 481 | if (!validate_jsmn_parse_output(input, *toks, *toks + ret)) |
| 482 | return false; |
| 483 | |
| 484 | /* Cut to length and return. */ |
| 485 | tal_resize(toks, ret + 1); |
| 486 | /* Make sure last one is always referenceable. */ |
| 487 | (*toks)[ret].type = -1; |
| 488 | (*toks)[ret].start = (*toks)[ret].end = (*toks)[ret].size = 0; |
| 489 | |
| 490 | *complete = true; |
| 491 | return true; |
| 492 | } |
| 493 | |
| 494 | jsmntok_t *json_parse_simple(const tal_t *ctx, const char *input, int len) |
| 495 | { |