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