diff options
| author | 2025-02-13 08:36:34 +0100 | |
|---|---|---|
| committer | 2025-02-13 08:36:34 +0100 | |
| commit | af8a95bbf0b3709d605175da92b93ad1d0aa0bd3 (patch) | |
| tree | e42906a03f0d8cd45cad46803512b50bafc4956a /xs_json.h | |
| parent | Don't show the bookmark emoji to strangers. (diff) | |
| download | penes-snac2-af8a95bbf0b3709d605175da92b93ad1d0aa0bd3.tar.gz penes-snac2-af8a95bbf0b3709d605175da92b93ad1d0aa0bd3.tar.xz penes-snac2-af8a95bbf0b3709d605175da92b93ad1d0aa0bd3.zip | |
Limit JSON depth
Diffstat (limited to '')
| -rw-r--r-- | xs_json.h | 36 |
1 files changed, 19 insertions, 17 deletions
| @@ -7,14 +7,16 @@ | |||
| 7 | int xs_json_dump(const xs_val *data, int indent, FILE *f); | 7 | int xs_json_dump(const xs_val *data, int indent, FILE *f); |
| 8 | xs_str *xs_json_dumps(const xs_val *data, int indent); | 8 | xs_str *xs_json_dumps(const xs_val *data, int indent); |
| 9 | 9 | ||
| 10 | xs_val *xs_json_load(FILE *f); | 10 | xs_val *xs_json_load_full(FILE *f, int maxdepth); |
| 11 | xs_val *xs_json_loads(const xs_str *json); | 11 | xs_val *xs_json_loads_full(const xs_str *json, int maxdepth); |
| 12 | #define xs_json_load(f) xs_json_load_full(f, MAX_JSON_DEPTH) | ||
| 13 | #define xs_json_loads(s) xs_json_loads_full(s, MAX_JSON_DEPTH) | ||
| 12 | 14 | ||
| 13 | xstype xs_json_load_type(FILE *f); | 15 | xstype xs_json_load_type(FILE *f); |
| 14 | int xs_json_load_array_iter(FILE *f, xs_val **value, xstype *pt, int *c); | 16 | int xs_json_load_array_iter(FILE *f, xs_val **value, xstype *pt, int *c); |
| 15 | int xs_json_load_object_iter(FILE *f, xs_str **key, xs_val **value, xstype *pt, int *c); | 17 | int xs_json_load_object_iter(FILE *f, xs_str **key, xs_val **value, xstype *pt, int *c); |
| 16 | xs_list *xs_json_load_array(FILE *f); | 18 | xs_list *xs_json_load_array(FILE *f, int maxdepth); |
| 17 | xs_dict *xs_json_load_object(FILE *f); | 19 | xs_dict *xs_json_load_object(FILE *f, int maxdepth); |
| 18 | 20 | ||
| 19 | 21 | ||
| 20 | #ifdef XS_IMPLEMENTATION | 22 | #ifdef XS_IMPLEMENTATION |
| @@ -371,7 +373,7 @@ int xs_json_load_array_iter(FILE *f, xs_val **value, xstype *pt, int *c) | |||
| 371 | } | 373 | } |
| 372 | 374 | ||
| 373 | 375 | ||
| 374 | xs_list *xs_json_load_array(FILE *f) | 376 | xs_list *xs_json_load_array(FILE *f, int maxdepth) |
| 375 | /* loads a full JSON array (after the initial OBRACK) */ | 377 | /* loads a full JSON array (after the initial OBRACK) */ |
| 376 | { | 378 | { |
| 377 | xstype t; | 379 | xstype t; |
| @@ -387,12 +389,12 @@ xs_list *xs_json_load_array(FILE *f) | |||
| 387 | 389 | ||
| 388 | if (r == 1) { | 390 | if (r == 1) { |
| 389 | /* partial load? */ | 391 | /* partial load? */ |
| 390 | if (v == NULL) { | 392 | if (v == NULL && maxdepth != 0) { |
| 391 | if (t == XSTYPE_LIST) | 393 | if (t == XSTYPE_LIST) |
| 392 | v = xs_json_load_array(f); | 394 | v = xs_json_load_array(f, maxdepth - 1); |
| 393 | else | 395 | else |
| 394 | if (t == XSTYPE_DICT) | 396 | if (t == XSTYPE_DICT) |
| 395 | v = xs_json_load_object(f); | 397 | v = xs_json_load_object(f, maxdepth - 1); |
| 396 | } | 398 | } |
| 397 | 399 | ||
| 398 | /* still null? fail */ | 400 | /* still null? fail */ |
| @@ -459,7 +461,7 @@ int xs_json_load_object_iter(FILE *f, xs_str **key, xs_val **value, xstype *pt, | |||
| 459 | } | 461 | } |
| 460 | 462 | ||
| 461 | 463 | ||
| 462 | xs_dict *xs_json_load_object(FILE *f) | 464 | xs_dict *xs_json_load_object(FILE *f, int maxdepth) |
| 463 | /* loads a full JSON object (after the initial OCURLY) */ | 465 | /* loads a full JSON object (after the initial OCURLY) */ |
| 464 | { | 466 | { |
| 465 | xstype t; | 467 | xstype t; |
| @@ -476,12 +478,12 @@ xs_dict *xs_json_load_object(FILE *f) | |||
| 476 | 478 | ||
| 477 | if (r == 1) { | 479 | if (r == 1) { |
| 478 | /* partial load? */ | 480 | /* partial load? */ |
| 479 | if (v == NULL) { | 481 | if (v == NULL && maxdepth != 0) { |
| 480 | if (t == XSTYPE_LIST) | 482 | if (t == XSTYPE_LIST) |
| 481 | v = xs_json_load_array(f); | 483 | v = xs_json_load_array(f, maxdepth - 1); |
| 482 | else | 484 | else |
| 483 | if (t == XSTYPE_DICT) | 485 | if (t == XSTYPE_DICT) |
| 484 | v = xs_json_load_object(f); | 486 | v = xs_json_load_object(f, maxdepth - 1); |
| 485 | } | 487 | } |
| 486 | 488 | ||
| 487 | /* still null? fail */ | 489 | /* still null? fail */ |
| @@ -500,14 +502,14 @@ xs_dict *xs_json_load_object(FILE *f) | |||
| 500 | } | 502 | } |
| 501 | 503 | ||
| 502 | 504 | ||
| 503 | xs_val *xs_json_loads(const xs_str *json) | 505 | xs_val *xs_json_loads_full(const xs_str *json, int maxdepth) |
| 504 | /* loads a string in JSON format and converts to a multiple data */ | 506 | /* loads a string in JSON format and converts to a multiple data */ |
| 505 | { | 507 | { |
| 506 | FILE *f; | 508 | FILE *f; |
| 507 | xs_val *v = NULL; | 509 | xs_val *v = NULL; |
| 508 | 510 | ||
| 509 | if ((f = fmemopen((char *)json, strlen(json), "r")) != NULL) { | 511 | if ((f = fmemopen((char *)json, strlen(json), "r")) != NULL) { |
| 510 | v = xs_json_load(f); | 512 | v = xs_json_load_full(f, maxdepth); |
| 511 | fclose(f); | 513 | fclose(f); |
| 512 | } | 514 | } |
| 513 | 515 | ||
| @@ -533,17 +535,17 @@ xstype xs_json_load_type(FILE *f) | |||
| 533 | } | 535 | } |
| 534 | 536 | ||
| 535 | 537 | ||
| 536 | xs_val *xs_json_load(FILE *f) | 538 | xs_val *xs_json_load_full(FILE *f, int maxdepth) |
| 537 | /* loads a JSON file */ | 539 | /* loads a JSON file */ |
| 538 | { | 540 | { |
| 539 | xs_val *v = NULL; | 541 | xs_val *v = NULL; |
| 540 | xstype t = xs_json_load_type(f); | 542 | xstype t = xs_json_load_type(f); |
| 541 | 543 | ||
| 542 | if (t == XSTYPE_LIST) | 544 | if (t == XSTYPE_LIST) |
| 543 | v = xs_json_load_array(f); | 545 | v = xs_json_load_array(f, maxdepth); |
| 544 | else | 546 | else |
| 545 | if (t == XSTYPE_DICT) | 547 | if (t == XSTYPE_DICT) |
| 546 | v = xs_json_load_object(f); | 548 | v = xs_json_load_object(f, maxdepth); |
| 547 | 549 | ||
| 548 | return v; | 550 | return v; |
| 549 | } | 551 | } |