1 /* vi:set ts=8 sts=4 sw=4 noet: 2 * 3 * VIM - Vi IMproved by Bram Moolenaar 4 * 5 * Do ":help uganda" in Vim to read copying and usage conditions. 6 * Do ":help credits" in Vim to see a list of people who contributed. 7 * See README.txt for an overview of the Vim source code. 8 */ 9 10 /* 11 * json.c: Encoding and decoding JSON. 12 * 13 * Follows this standard: https://tools.ietf.org/html/rfc7159.html 14 */ 15 #define USING_FLOAT_STUFF 16 17 #include "vim.h" 18 19 #if defined(FEAT_EVAL) || defined(PROTO) 20 21 static int json_encode_item(garray_T *gap, typval_T *val, int copyID, int options); 22 static int json_decode_item(js_read_T *reader, typval_T *res, int options); 23 24 /* 25 * Encode "val" into a JSON format string. 26 * The result is added to "gap" 27 * Returns FAIL on failure and makes gap->ga_data empty. 28 */ 29 static int 30 json_encode_gap(garray_T *gap, typval_T *val, int options) 31 { 32 if (json_encode_item(gap, val, get_copyID(), options) == FAIL) 33 { 34 ga_clear(gap); 35 gap->ga_data = vim_strsave((char_u *)""); 36 return FAIL; 37 } 38 return OK; 39 } 40 41 /* 42 * Encode "val" into a JSON format string. 43 * The result is in allocated memory. 44 * The result is empty when encoding fails. 45 * "options" can contain JSON_JS, JSON_NO_NONE and JSON_NL. 46 */ 47 char_u * 48 json_encode(typval_T *val, int options) 49 { 50 garray_T ga; 51 52 /* Store bytes in the growarray. */ 53 ga_init2(&ga, 1, 4000); 54 json_encode_gap(&ga, val, options); 55 return ga.ga_data; 56 } 57 58 /* 59 * Encode ["nr", "val"] into a JSON format string in allocated memory. 60 * "options" can contain JSON_JS, JSON_NO_NONE and JSON_NL. 61 * Returns NULL when out of memory. 62 */ 63 char_u * 64 json_encode_nr_expr(int nr, typval_T *val, int options) 65 { 66 typval_T listtv; 67 typval_T nrtv; 68 garray_T ga; 69 70 nrtv.v_type = VAR_NUMBER; 71 nrtv.vval.v_number = nr; 72 if (rettv_list_alloc(&listtv) == FAIL) 73 return NULL; 74 if (list_append_tv(listtv.vval.v_list, &nrtv) == FAIL 75 || list_append_tv(listtv.vval.v_list, val) == FAIL) 76 { 77 list_unref(listtv.vval.v_list); 78 return NULL; 79 } 80 81 ga_init2(&ga, 1, 4000); 82 if (json_encode_gap(&ga, &listtv, options) == OK && (options & JSON_NL)) 83 ga_append(&ga, '\n'); 84 list_unref(listtv.vval.v_list); 85 return ga.ga_data; 86 } 87 88 static void 89 write_string(garray_T *gap, char_u *str) 90 { 91 char_u *res = str; 92 char_u numbuf[NUMBUFLEN]; 93 94 if (res == NULL) 95 ga_concat(gap, (char_u *)"\"\""); 96 else 97 { 98 #if defined(FEAT_MBYTE) && defined(USE_ICONV) 99 vimconv_T conv; 100 char_u *converted = NULL; 101 102 if (!enc_utf8) 103 { 104 /* Convert the text from 'encoding' to utf-8, the JSON string is 105 * always utf-8. */ 106 conv.vc_type = CONV_NONE; 107 convert_setup(&conv, p_enc, (char_u*)"utf-8"); 108 if (conv.vc_type != CONV_NONE) 109 converted = res = string_convert(&conv, res, NULL); 110 convert_setup(&conv, NULL, NULL); 111 } 112 #endif 113 ga_append(gap, '"'); 114 while (*res != NUL) 115 { 116 int c; 117 #ifdef FEAT_MBYTE 118 /* always use utf-8 encoding, ignore 'encoding' */ 119 c = utf_ptr2char(res); 120 #else 121 c = *res; 122 #endif 123 124 switch (c) 125 { 126 case 0x08: 127 ga_append(gap, '\\'); ga_append(gap, 'b'); break; 128 case 0x09: 129 ga_append(gap, '\\'); ga_append(gap, 't'); break; 130 case 0x0a: 131 ga_append(gap, '\\'); ga_append(gap, 'n'); break; 132 case 0x0c: 133 ga_append(gap, '\\'); ga_append(gap, 'f'); break; 134 case 0x0d: 135 ga_append(gap, '\\'); ga_append(gap, 'r'); break; 136 case 0x22: /* " */ 137 case 0x5c: /* \ */ 138 ga_append(gap, '\\'); 139 ga_append(gap, c); 140 break; 141 default: 142 if (c >= 0x20) 143 { 144 #ifdef FEAT_MBYTE 145 numbuf[utf_char2bytes(c, numbuf)] = NUL; 146 #else 147 numbuf[0] = c; 148 numbuf[1] = NUL; 149 #endif 150 ga_concat(gap, numbuf); 151 } 152 else 153 { 154 vim_snprintf((char *)numbuf, NUMBUFLEN, 155 "\\u%04lx", (long)c); 156 ga_concat(gap, numbuf); 157 } 158 } 159 #ifdef FEAT_MBYTE 160 res += utf_ptr2len(res); 161 #else 162 ++res; 163 #endif 164 } 165 ga_append(gap, '"'); 166 #if defined(FEAT_MBYTE) && defined(USE_ICONV) 167 vim_free(converted); 168 #endif 169 } 170 } 171 172 /* 173 * Return TRUE if "key" can be used without quotes. 174 * That is when it starts with a letter and only contains letters, digits and 175 * underscore. 176 */ 177 static int 178 is_simple_key(char_u *key) 179 { 180 char_u *p; 181 182 if (!ASCII_ISALPHA(*key)) 183 return FALSE; 184 for (p = key + 1; *p != NUL; ++p) 185 if (!ASCII_ISALPHA(*p) && *p != '_' && !vim_isdigit(*p)) 186 return FALSE; 187 return TRUE; 188 } 189 190 /* 191 * Encode "val" into "gap". 192 * Return FAIL or OK. 193 */ 194 static int 195 json_encode_item(garray_T *gap, typval_T *val, int copyID, int options) 196 { 197 char_u numbuf[NUMBUFLEN]; 198 char_u *res; 199 list_T *l; 200 dict_T *d; 201 202 switch (val->v_type) 203 { 204 case VAR_SPECIAL: 205 switch (val->vval.v_number) 206 { 207 case VVAL_FALSE: ga_concat(gap, (char_u *)"false"); break; 208 case VVAL_TRUE: ga_concat(gap, (char_u *)"true"); break; 209 case VVAL_NONE: if ((options & JSON_JS) != 0 210 && (options & JSON_NO_NONE) == 0) 211 /* empty item */ 212 break; 213 /* FALLTHROUGH */ 214 case VVAL_NULL: ga_concat(gap, (char_u *)"null"); break; 215 } 216 break; 217 218 case VAR_NUMBER: 219 vim_snprintf((char *)numbuf, NUMBUFLEN, "%lld", 220 val->vval.v_number); 221 ga_concat(gap, numbuf); 222 break; 223 224 case VAR_STRING: 225 res = val->vval.v_string; 226 write_string(gap, res); 227 break; 228 229 case VAR_FUNC: 230 case VAR_PARTIAL: 231 case VAR_JOB: 232 case VAR_CHANNEL: 233 /* no JSON equivalent TODO: better error */ 234 EMSG(_(e_invarg)); 235 return FAIL; 236 237 case VAR_LIST: 238 l = val->vval.v_list; 239 if (l == NULL) 240 ga_concat(gap, (char_u *)"[]"); 241 else 242 { 243 if (l->lv_copyID == copyID) 244 ga_concat(gap, (char_u *)"[]"); 245 else 246 { 247 listitem_T *li; 248 249 l->lv_copyID = copyID; 250 ga_append(gap, '['); 251 for (li = l->lv_first; li != NULL && !got_int; ) 252 { 253 if (json_encode_item(gap, &li->li_tv, copyID, 254 options & JSON_JS) == FAIL) 255 return FAIL; 256 if ((options & JSON_JS) 257 && li->li_next == NULL 258 && li->li_tv.v_type == VAR_SPECIAL 259 && li->li_tv.vval.v_number == VVAL_NONE) 260 /* add an extra comma if the last item is v:none */ 261 ga_append(gap, ','); 262 li = li->li_next; 263 if (li != NULL) 264 ga_append(gap, ','); 265 } 266 ga_append(gap, ']'); 267 l->lv_copyID = 0; 268 } 269 } 270 break; 271 272 case VAR_DICT: 273 d = val->vval.v_dict; 274 if (d == NULL) 275 ga_concat(gap, (char_u *)"{}"); 276 else 277 { 278 if (d->dv_copyID == copyID) 279 ga_concat(gap, (char_u *)"{}"); 280 else 281 { 282 int first = TRUE; 283 int todo = (int)d->dv_hashtab.ht_used; 284 hashitem_T *hi; 285 286 d->dv_copyID = copyID; 287 ga_append(gap, '{'); 288 289 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; 290 ++hi) 291 if (!HASHITEM_EMPTY(hi)) 292 { 293 --todo; 294 if (first) 295 first = FALSE; 296 else 297 ga_append(gap, ','); 298 if ((options & JSON_JS) 299 && is_simple_key(hi->hi_key)) 300 ga_concat(gap, hi->hi_key); 301 else 302 write_string(gap, hi->hi_key); 303 ga_append(gap, ':'); 304 if (json_encode_item(gap, &dict_lookup(hi)->di_tv, 305 copyID, options | JSON_NO_NONE) == FAIL) 306 return FAIL; 307 } 308 ga_append(gap, '}'); 309 d->dv_copyID = 0; 310 } 311 } 312 break; 313 314 case VAR_FLOAT: 315 #ifdef FEAT_FLOAT 316 # if defined(HAVE_MATH_H) 317 if (isnan(val->vval.v_float)) 318 ga_concat(gap, (char_u *)"NaN"); 319 else if (isinf(val->vval.v_float)) 320 ga_concat(gap, (char_u *)"Infinity"); 321 else 322 # endif 323 { 324 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", 325 val->vval.v_float); 326 ga_concat(gap, numbuf); 327 } 328 break; 329 #endif 330 case VAR_UNKNOWN: 331 internal_error("json_encode_item()"); 332 return FAIL; 333 } 334 return OK; 335 } 336 337 /* 338 * When "reader" has less than NUMBUFLEN bytes available, call the fill 339 * callback to get more. 340 */ 341 static void 342 fill_numbuflen(js_read_T *reader) 343 { 344 if (reader->js_fill != NULL && (int)(reader->js_end - reader->js_buf) 345 - reader->js_used < NUMBUFLEN) 346 { 347 if (reader->js_fill(reader)) 348 reader->js_end = reader->js_buf + STRLEN(reader->js_buf); 349 } 350 } 351 352 /* 353 * Skip white space in "reader". All characters <= space are considered white 354 * space. 355 * Also tops up readahead when needed. 356 */ 357 static void 358 json_skip_white(js_read_T *reader) 359 { 360 int c; 361 362 for (;;) 363 { 364 c = reader->js_buf[reader->js_used]; 365 if (reader->js_fill != NULL && c == NUL) 366 { 367 if (reader->js_fill(reader)) 368 { 369 reader->js_end = reader->js_buf + STRLEN(reader->js_buf); 370 continue; 371 } 372 } 373 if (c == NUL || c > ' ') 374 break; 375 ++reader->js_used; 376 } 377 fill_numbuflen(reader); 378 } 379 380 static int 381 json_decode_string(js_read_T *reader, typval_T *res, int quote) 382 { 383 garray_T ga; 384 int len; 385 char_u *p; 386 int c; 387 varnumber_T nr; 388 389 if (res != NULL) 390 ga_init2(&ga, 1, 200); 391 392 p = reader->js_buf + reader->js_used + 1; /* skip over " or ' */ 393 while (*p != quote) 394 { 395 /* The JSON is always expected to be utf-8, thus use utf functions 396 * here. The string is converted below if needed. */ 397 if (*p == NUL || p[1] == NUL 398 #ifdef FEAT_MBYTE 399 || utf_ptr2len(p) < utf_byte2len(*p) 400 #endif 401 ) 402 { 403 /* Not enough bytes to make a character or end of the string. Get 404 * more if possible. */ 405 if (reader->js_fill == NULL) 406 break; 407 len = (int)(reader->js_end - p); 408 reader->js_used = (int)(p - reader->js_buf); 409 if (!reader->js_fill(reader)) 410 break; /* didn't get more */ 411 p = reader->js_buf + reader->js_used; 412 reader->js_end = reader->js_buf + STRLEN(reader->js_buf); 413 continue; 414 } 415 416 if (*p == '\\') 417 { 418 c = -1; 419 switch (p[1]) 420 { 421 case '\\': c = '\\'; break; 422 case '"': c = '"'; break; 423 case 'b': c = BS; break; 424 case 't': c = TAB; break; 425 case 'n': c = NL; break; 426 case 'f': c = FF; break; 427 case 'r': c = CAR; break; 428 case 'u': 429 if (reader->js_fill != NULL 430 && (int)(reader->js_end - p) < NUMBUFLEN) 431 { 432 reader->js_used = (int)(p - reader->js_buf); 433 if (reader->js_fill(reader)) 434 { 435 p = reader->js_buf + reader->js_used; 436 reader->js_end = reader->js_buf 437 + STRLEN(reader->js_buf); 438 } 439 } 440 nr = 0; 441 len = 0; 442 vim_str2nr(p + 2, NULL, &len, 443 STR2NR_HEX + STR2NR_FORCE, &nr, NULL, 4); 444 p += len + 2; 445 if (0xd800 <= nr && nr <= 0xdfff 446 && (int)(reader->js_end - p) >= 6 447 && *p == '\\' && *(p+1) == 'u') 448 { 449 varnumber_T nr2 = 0; 450 451 /* decode surrogate pair: \ud812\u3456 */ 452 len = 0; 453 vim_str2nr(p + 2, NULL, &len, 454 STR2NR_HEX + STR2NR_FORCE, &nr2, NULL, 4); 455 if (0xdc00 <= nr2 && nr2 <= 0xdfff) 456 { 457 p += len + 2; 458 nr = (((nr - 0xd800) << 10) | 459 ((nr2 - 0xdc00) & 0x3ff)) + 0x10000; 460 } 461 } 462 if (res != NULL) 463 { 464 #ifdef FEAT_MBYTE 465 char_u buf[NUMBUFLEN]; 466 buf[utf_char2bytes((int)nr, buf)] = NUL; 467 ga_concat(&ga, buf); 468 #else 469 ga_append(&ga, (int)nr); 470 #endif 471 } 472 break; 473 default: 474 /* not a special char, skip over \ */ 475 ++p; 476 continue; 477 } 478 if (c > 0) 479 { 480 p += 2; 481 if (res != NULL) 482 ga_append(&ga, c); 483 } 484 } 485 else 486 { 487 #ifdef FEAT_MBYTE 488 len = utf_ptr2len(p); 489 #else 490 len = 1; 491 #endif 492 if (res != NULL) 493 { 494 if (ga_grow(&ga, len) == FAIL) 495 { 496 ga_clear(&ga); 497 return FAIL; 498 } 499 mch_memmove((char *)ga.ga_data + ga.ga_len, p, (size_t)len); 500 ga.ga_len += len; 501 } 502 p += len; 503 } 504 } 505 506 reader->js_used = (int)(p - reader->js_buf); 507 if (*p == quote) 508 { 509 ++reader->js_used; 510 if (res != NULL) 511 { 512 ga_append(&ga, NUL); 513 res->v_type = VAR_STRING; 514 #if defined(FEAT_MBYTE) && defined(USE_ICONV) 515 if (!enc_utf8) 516 { 517 vimconv_T conv; 518 519 /* Convert the utf-8 string to 'encoding'. */ 520 conv.vc_type = CONV_NONE; 521 convert_setup(&conv, (char_u*)"utf-8", p_enc); 522 if (conv.vc_type != CONV_NONE) 523 { 524 res->vval.v_string = 525 string_convert(&conv, ga.ga_data, NULL); 526 vim_free(ga.ga_data); 527 } 528 convert_setup(&conv, NULL, NULL); 529 } 530 else 531 #endif 532 res->vval.v_string = ga.ga_data; 533 } 534 return OK; 535 } 536 if (res != NULL) 537 { 538 res->v_type = VAR_SPECIAL; 539 res->vval.v_number = VVAL_NONE; 540 ga_clear(&ga); 541 } 542 return MAYBE; 543 } 544 545 typedef enum { 546 JSON_ARRAY, /* parsing items in an array */ 547 JSON_OBJECT_KEY, /* parsing key of an object */ 548 JSON_OBJECT /* parsing item in an object, after the key */ 549 } json_decode_T; 550 551 typedef struct { 552 json_decode_T jd_type; 553 typval_T jd_tv; /* the list or dict */ 554 typval_T jd_key_tv; 555 char_u *jd_key; 556 } json_dec_item_T; 557 558 /* 559 * Decode one item and put it in "res". If "res" is NULL only advance. 560 * Must already have skipped white space. 561 * 562 * Return FAIL for a decoding error (and give an error). 563 * Return MAYBE for an incomplete message. 564 */ 565 static int 566 json_decode_item(js_read_T *reader, typval_T *res, int options) 567 { 568 char_u *p; 569 int len; 570 int retval; 571 garray_T stack; 572 typval_T item; 573 typval_T *cur_item; 574 json_dec_item_T *top_item; 575 char_u key_buf[NUMBUFLEN]; 576 577 ga_init2(&stack, sizeof(json_dec_item_T), 100); 578 cur_item = res; 579 init_tv(&item); 580 if (res != NULL) 581 init_tv(res); 582 583 fill_numbuflen(reader); 584 p = reader->js_buf + reader->js_used; 585 for (;;) 586 { 587 top_item = NULL; 588 if (stack.ga_len > 0) 589 { 590 top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len - 1; 591 json_skip_white(reader); 592 p = reader->js_buf + reader->js_used; 593 if (*p == NUL) 594 { 595 retval = MAYBE; 596 if (top_item->jd_type == JSON_OBJECT) 597 /* did get the key, clear it */ 598 clear_tv(&top_item->jd_key_tv); 599 goto theend; 600 } 601 if (top_item->jd_type == JSON_OBJECT_KEY 602 || top_item->jd_type == JSON_ARRAY) 603 { 604 /* Check for end of object or array. */ 605 if (*p == (top_item->jd_type == JSON_ARRAY ? ']' : '}')) 606 { 607 ++reader->js_used; /* consume the ']' or '}' */ 608 --stack.ga_len; 609 if (stack.ga_len == 0) 610 { 611 retval = OK; 612 goto theend; 613 } 614 if (cur_item != NULL) 615 cur_item = &top_item->jd_tv; 616 goto item_end; 617 } 618 } 619 } 620 621 if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY 622 && (options & JSON_JS) 623 && reader->js_buf[reader->js_used] != '"' 624 && reader->js_buf[reader->js_used] != '\'') 625 { 626 char_u *key; 627 628 /* accept an object key that is not in quotes */ 629 key = p = reader->js_buf + reader->js_used; 630 while (*p != NUL && *p != ':' && *p > ' ') 631 ++p; 632 if (cur_item != NULL) 633 { 634 cur_item->v_type = VAR_STRING; 635 cur_item->vval.v_string = vim_strnsave(key, (int)(p - key)); 636 top_item->jd_key = cur_item->vval.v_string; 637 } 638 reader->js_used += (int)(p - key); 639 } 640 else 641 { 642 switch (*p) 643 { 644 case '[': /* start of array */ 645 if (ga_grow(&stack, 1) == FAIL) 646 { 647 retval = FAIL; 648 break; 649 } 650 if (cur_item != NULL && rettv_list_alloc(cur_item) == FAIL) 651 { 652 cur_item->v_type = VAR_SPECIAL; 653 cur_item->vval.v_number = VVAL_NONE; 654 retval = FAIL; 655 break; 656 } 657 658 ++reader->js_used; /* consume the '[' */ 659 top_item = ((json_dec_item_T *)stack.ga_data) 660 + stack.ga_len; 661 top_item->jd_type = JSON_ARRAY; 662 ++stack.ga_len; 663 if (cur_item != NULL) 664 { 665 top_item->jd_tv = *cur_item; 666 cur_item = &item; 667 } 668 continue; 669 670 case '{': /* start of object */ 671 if (ga_grow(&stack, 1) == FAIL) 672 { 673 retval = FAIL; 674 break; 675 } 676 if (cur_item != NULL && rettv_dict_alloc(cur_item) == FAIL) 677 { 678 cur_item->v_type = VAR_SPECIAL; 679 cur_item->vval.v_number = VVAL_NONE; 680 retval = FAIL; 681 break; 682 } 683 684 ++reader->js_used; /* consume the '{' */ 685 top_item = ((json_dec_item_T *)stack.ga_data) 686 + stack.ga_len; 687 top_item->jd_type = JSON_OBJECT_KEY; 688 ++stack.ga_len; 689 if (cur_item != NULL) 690 { 691 top_item->jd_tv = *cur_item; 692 cur_item = &top_item->jd_key_tv; 693 } 694 continue; 695 696 case '"': /* string */ 697 retval = json_decode_string(reader, cur_item, *p); 698 break; 699 700 case '\'': 701 if (options & JSON_JS) 702 retval = json_decode_string(reader, cur_item, *p); 703 else 704 { 705 EMSG(_(e_invarg)); 706 retval = FAIL; 707 } 708 break; 709 710 case ',': /* comma: empty item */ 711 if ((options & JSON_JS) == 0) 712 { 713 EMSG(_(e_invarg)); 714 retval = FAIL; 715 break; 716 } 717 /* FALLTHROUGH */ 718 case NUL: /* empty */ 719 if (cur_item != NULL) 720 { 721 cur_item->v_type = VAR_SPECIAL; 722 cur_item->vval.v_number = VVAL_NONE; 723 } 724 retval = OK; 725 break; 726 727 default: 728 if (VIM_ISDIGIT(*p) || *p == '-') 729 { 730 #ifdef FEAT_FLOAT 731 char_u *sp = p; 732 733 if (*sp == '-') 734 { 735 ++sp; 736 if (*sp == NUL) 737 { 738 retval = MAYBE; 739 break; 740 } 741 if (!VIM_ISDIGIT(*sp)) 742 { 743 EMSG(_(e_invarg)); 744 retval = FAIL; 745 break; 746 } 747 } 748 sp = skipdigits(sp); 749 if (*sp == '.' || *sp == 'e' || *sp == 'E') 750 { 751 if (cur_item == NULL) 752 { 753 float_T f; 754 755 len = string2float(p, &f); 756 } 757 else 758 { 759 cur_item->v_type = VAR_FLOAT; 760 len = string2float(p, &cur_item->vval.v_float); 761 } 762 } 763 else 764 #endif 765 { 766 varnumber_T nr; 767 768 vim_str2nr(reader->js_buf + reader->js_used, 769 NULL, &len, 0, /* what */ 770 &nr, NULL, 0); 771 if (cur_item != NULL) 772 { 773 cur_item->v_type = VAR_NUMBER; 774 cur_item->vval.v_number = nr; 775 } 776 } 777 reader->js_used += len; 778 retval = OK; 779 break; 780 } 781 if (STRNICMP((char *)p, "false", 5) == 0) 782 { 783 reader->js_used += 5; 784 if (cur_item != NULL) 785 { 786 cur_item->v_type = VAR_SPECIAL; 787 cur_item->vval.v_number = VVAL_FALSE; 788 } 789 retval = OK; 790 break; 791 } 792 if (STRNICMP((char *)p, "true", 4) == 0) 793 { 794 reader->js_used += 4; 795 if (cur_item != NULL) 796 { 797 cur_item->v_type = VAR_SPECIAL; 798 cur_item->vval.v_number = VVAL_TRUE; 799 } 800 retval = OK; 801 break; 802 } 803 if (STRNICMP((char *)p, "null", 4) == 0) 804 { 805 reader->js_used += 4; 806 if (cur_item != NULL) 807 { 808 cur_item->v_type = VAR_SPECIAL; 809 cur_item->vval.v_number = VVAL_NULL; 810 } 811 retval = OK; 812 break; 813 } 814 #ifdef FEAT_FLOAT 815 if (STRNICMP((char *)p, "NaN", 3) == 0) 816 { 817 reader->js_used += 3; 818 if (cur_item != NULL) 819 { 820 cur_item->v_type = VAR_FLOAT; 821 cur_item->vval.v_float = NAN; 822 } 823 retval = OK; 824 break; 825 } 826 if (STRNICMP((char *)p, "Infinity", 8) == 0) 827 { 828 reader->js_used += 8; 829 if (cur_item != NULL) 830 { 831 cur_item->v_type = VAR_FLOAT; 832 cur_item->vval.v_float = INFINITY; 833 } 834 retval = OK; 835 break; 836 } 837 #endif 838 /* check for truncated name */ 839 len = (int)(reader->js_end - (reader->js_buf + reader->js_used)); 840 if ( 841 (len < 5 && STRNICMP((char *)p, "false", len) == 0) 842 #ifdef FEAT_FLOAT 843 || (len < 8 && STRNICMP((char *)p, "Infinity", len) == 0) 844 || (len < 3 && STRNICMP((char *)p, "NaN", len) == 0) 845 #endif 846 || (len < 4 && (STRNICMP((char *)p, "true", len) == 0 847 || STRNICMP((char *)p, "null", len) == 0))) 848 849 retval = MAYBE; 850 else 851 retval = FAIL; 852 break; 853 } 854 855 /* We are finished when retval is FAIL or MAYBE and when at the 856 * toplevel. */ 857 if (retval == FAIL) 858 break; 859 if (retval == MAYBE || stack.ga_len == 0) 860 goto theend; 861 862 if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY 863 && cur_item != NULL) 864 { 865 top_item->jd_key = get_tv_string_buf_chk(cur_item, key_buf); 866 if (top_item->jd_key == NULL) 867 { 868 clear_tv(cur_item); 869 EMSG(_(e_invarg)); 870 retval = FAIL; 871 goto theend; 872 } 873 } 874 } 875 876 item_end: 877 top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len - 1; 878 switch (top_item->jd_type) 879 { 880 case JSON_ARRAY: 881 if (res != NULL) 882 { 883 listitem_T *li = listitem_alloc(); 884 885 if (li == NULL) 886 { 887 clear_tv(cur_item); 888 retval = FAIL; 889 goto theend; 890 } 891 li->li_tv = *cur_item; 892 list_append(top_item->jd_tv.vval.v_list, li); 893 } 894 if (cur_item != NULL) 895 cur_item = &item; 896 897 json_skip_white(reader); 898 p = reader->js_buf + reader->js_used; 899 if (*p == ',') 900 ++reader->js_used; 901 else if (*p != ']') 902 { 903 if (*p == NUL) 904 retval = MAYBE; 905 else 906 { 907 EMSG(_(e_invarg)); 908 retval = FAIL; 909 } 910 goto theend; 911 } 912 break; 913 914 case JSON_OBJECT_KEY: 915 json_skip_white(reader); 916 p = reader->js_buf + reader->js_used; 917 if (*p != ':') 918 { 919 if (cur_item != NULL) 920 clear_tv(cur_item); 921 if (*p == NUL) 922 retval = MAYBE; 923 else 924 { 925 EMSG(_(e_invarg)); 926 retval = FAIL; 927 } 928 goto theend; 929 } 930 ++reader->js_used; 931 json_skip_white(reader); 932 top_item->jd_type = JSON_OBJECT; 933 if (cur_item != NULL) 934 cur_item = &item; 935 break; 936 937 case JSON_OBJECT: 938 if (cur_item != NULL 939 && dict_find(top_item->jd_tv.vval.v_dict, 940 top_item->jd_key, -1) != NULL) 941 { 942 EMSG2(_("E938: Duplicate key in JSON: \"%s\""), 943 top_item->jd_key); 944 clear_tv(&top_item->jd_key_tv); 945 clear_tv(cur_item); 946 retval = FAIL; 947 goto theend; 948 } 949 950 if (cur_item != NULL) 951 { 952 dictitem_T *di = dictitem_alloc(top_item->jd_key); 953 954 clear_tv(&top_item->jd_key_tv); 955 if (di == NULL) 956 { 957 clear_tv(cur_item); 958 retval = FAIL; 959 goto theend; 960 } 961 di->di_tv = *cur_item; 962 di->di_tv.v_lock = 0; 963 if (dict_add(top_item->jd_tv.vval.v_dict, di) == FAIL) 964 { 965 dictitem_free(di); 966 retval = FAIL; 967 goto theend; 968 } 969 } 970 971 json_skip_white(reader); 972 p = reader->js_buf + reader->js_used; 973 if (*p == ',') 974 ++reader->js_used; 975 else if (*p != '}') 976 { 977 if (*p == NUL) 978 retval = MAYBE; 979 else 980 { 981 EMSG(_(e_invarg)); 982 retval = FAIL; 983 } 984 goto theend; 985 } 986 top_item->jd_type = JSON_OBJECT_KEY; 987 if (cur_item != NULL) 988 cur_item = &top_item->jd_key_tv; 989 break; 990 } 991 } 992 993 /* Get here when parsing failed. */ 994 if (res != NULL) 995 { 996 clear_tv(res); 997 res->v_type = VAR_SPECIAL; 998 res->vval.v_number = VVAL_NONE; 999 } 1000 EMSG(_(e_invarg)); 1001 1002 theend: 1003 ga_clear(&stack); 1004 return retval; 1005 } 1006 1007 /* 1008 * Decode the JSON from "reader" and store the result in "res". 1009 * "options" can be JSON_JS or zero; 1010 * Return FAIL if not the whole message was consumed. 1011 */ 1012 int 1013 json_decode_all(js_read_T *reader, typval_T *res, int options) 1014 { 1015 int ret; 1016 1017 /* We find the end once, to avoid calling strlen() many times. */ 1018 reader->js_end = reader->js_buf + STRLEN(reader->js_buf); 1019 json_skip_white(reader); 1020 ret = json_decode_item(reader, res, options); 1021 if (ret != OK) 1022 { 1023 if (ret == MAYBE) 1024 EMSG(_(e_invarg)); 1025 return FAIL; 1026 } 1027 json_skip_white(reader); 1028 if (reader->js_buf[reader->js_used] != NUL) 1029 { 1030 EMSG(_(e_trailing)); 1031 return FAIL; 1032 } 1033 return OK; 1034 } 1035 1036 /* 1037 * Decode the JSON from "reader" and store the result in "res". 1038 * "options" can be JSON_JS or zero; 1039 * Return FAIL for a decoding error. 1040 * Return MAYBE for an incomplete message. 1041 * Consumes the message anyway. 1042 */ 1043 int 1044 json_decode(js_read_T *reader, typval_T *res, int options) 1045 { 1046 int ret; 1047 1048 /* We find the end once, to avoid calling strlen() many times. */ 1049 reader->js_end = reader->js_buf + STRLEN(reader->js_buf); 1050 json_skip_white(reader); 1051 ret = json_decode_item(reader, res, options); 1052 json_skip_white(reader); 1053 1054 return ret; 1055 } 1056 1057 /* 1058 * Decode the JSON from "reader" to find the end of the message. 1059 * "options" can be JSON_JS or zero. 1060 * This is only used for testing. 1061 * Return FAIL if the message has a decoding error. 1062 * Return MAYBE if the message is truncated, need to read more. 1063 * This only works reliable if the message contains an object, array or 1064 * string. A number might be trucated without knowing. 1065 * Does not advance the reader. 1066 */ 1067 int 1068 json_find_end(js_read_T *reader, int options) 1069 { 1070 int used_save = reader->js_used; 1071 int ret; 1072 1073 /* We find the end once, to avoid calling strlen() many times. */ 1074 reader->js_end = reader->js_buf + STRLEN(reader->js_buf); 1075 json_skip_white(reader); 1076 ret = json_decode_item(reader, NULL, options); 1077 reader->js_used = used_save; 1078 return ret; 1079 } 1080 #endif 1081