1 /*- 2 * BSD LICENSE 3 * 4 * Copyright (c) Intel Corporation. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * * Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * * Neither the name of Intel Corporation nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include "spdk/json.h" 35 36 #include "spdk_internal/utf.h" 37 #include "spdk/log.h" 38 39 #define SPDK_JSON_DEBUG(...) SPDK_DEBUGLOG(json_util, __VA_ARGS__) 40 41 size_t 42 spdk_json_val_len(const struct spdk_json_val *val) 43 { 44 if (val == NULL) { 45 return 0; 46 } 47 48 if (val->type == SPDK_JSON_VAL_ARRAY_BEGIN || val->type == SPDK_JSON_VAL_OBJECT_BEGIN) { 49 return val->len + 2; 50 } 51 52 return 1; 53 } 54 55 bool 56 spdk_json_strequal(const struct spdk_json_val *val, const char *str) 57 { 58 size_t len; 59 60 if (val->type != SPDK_JSON_VAL_STRING && val->type != SPDK_JSON_VAL_NAME) { 61 return false; 62 } 63 64 len = strlen(str); 65 if (val->len != len) { 66 return false; 67 } 68 69 return memcmp(val->start, str, len) == 0; 70 } 71 72 char * 73 spdk_json_strdup(const struct spdk_json_val *val) 74 { 75 size_t len; 76 char *s; 77 78 if (val->type != SPDK_JSON_VAL_STRING && val->type != SPDK_JSON_VAL_NAME) { 79 return NULL; 80 } 81 82 len = val->len; 83 84 if (memchr(val->start, '\0', len)) { 85 /* String contains embedded NUL, so it is not a valid C string. */ 86 return NULL; 87 } 88 89 s = malloc(len + 1); 90 if (s == NULL) { 91 return s; 92 } 93 94 memcpy(s, val->start, len); 95 s[len] = '\0'; 96 97 return s; 98 } 99 100 struct spdk_json_num { 101 bool negative; 102 uint64_t significand; 103 int64_t exponent; 104 }; 105 106 static int 107 json_number_split(const struct spdk_json_val *val, struct spdk_json_num *num) 108 { 109 const char *iter; 110 size_t remaining; 111 uint64_t *pval; 112 uint64_t frac_digits = 0; 113 uint64_t exponent_u64 = 0; 114 bool exponent_negative = false; 115 enum { 116 NUM_STATE_INT, 117 NUM_STATE_FRAC, 118 NUM_STATE_EXP, 119 } state; 120 121 memset(num, 0, sizeof(*num)); 122 123 if (val->type != SPDK_JSON_VAL_NUMBER) { 124 return -EINVAL; 125 } 126 127 remaining = val->len; 128 if (remaining == 0) { 129 return -EINVAL; 130 } 131 132 iter = val->start; 133 if (*iter == '-') { 134 num->negative = true; 135 iter++; 136 remaining--; 137 } 138 139 state = NUM_STATE_INT; 140 pval = &num->significand; 141 while (remaining--) { 142 char c = *iter++; 143 144 if (c == '.') { 145 state = NUM_STATE_FRAC; 146 } else if (c == 'e' || c == 'E') { 147 state = NUM_STATE_EXP; 148 pval = &exponent_u64; 149 } else if (c == '-') { 150 assert(state == NUM_STATE_EXP); 151 exponent_negative = true; 152 } else if (c == '+') { 153 assert(state == NUM_STATE_EXP); 154 /* exp_negative = false; */ /* already false by default */ 155 } else { 156 uint64_t new_val; 157 158 assert(c >= '0' && c <= '9'); 159 new_val = *pval * 10 + c - '0'; 160 if (new_val < *pval) { 161 return -ERANGE; 162 } 163 164 if (state == NUM_STATE_FRAC) { 165 frac_digits++; 166 } 167 168 *pval = new_val; 169 } 170 } 171 172 if (exponent_negative) { 173 if (exponent_u64 > 9223372036854775808ULL) { /* abs(INT64_MIN) */ 174 return -ERANGE; 175 } 176 num->exponent = (int64_t) - exponent_u64; 177 } else { 178 if (exponent_u64 > INT64_MAX) { 179 return -ERANGE; 180 } 181 num->exponent = exponent_u64; 182 } 183 num->exponent -= frac_digits; 184 185 /* Apply as much of the exponent as possible without overflow or truncation */ 186 if (num->exponent < 0) { 187 while (num->exponent && num->significand >= 10 && num->significand % 10 == 0) { 188 num->significand /= 10; 189 num->exponent++; 190 } 191 } else { /* positive exponent */ 192 while (num->exponent) { 193 uint64_t new_val = num->significand * 10; 194 195 if (new_val < num->significand) { 196 break; 197 } 198 199 num->significand = new_val; 200 num->exponent--; 201 } 202 } 203 204 return 0; 205 } 206 207 int 208 spdk_json_number_to_uint8(const struct spdk_json_val *val, uint8_t *num) 209 { 210 struct spdk_json_num split_num; 211 int rc; 212 213 rc = json_number_split(val, &split_num); 214 if (rc) { 215 return rc; 216 } 217 218 if (split_num.exponent || split_num.negative) { 219 return -ERANGE; 220 } 221 222 if (split_num.significand > UINT8_MAX) { 223 return -ERANGE; 224 } 225 *num = (uint8_t)split_num.significand; 226 return 0; 227 } 228 229 int 230 spdk_json_number_to_uint16(const struct spdk_json_val *val, uint16_t *num) 231 { 232 struct spdk_json_num split_num; 233 int rc; 234 235 rc = json_number_split(val, &split_num); 236 if (rc) { 237 return rc; 238 } 239 240 if (split_num.exponent || split_num.negative) { 241 return -ERANGE; 242 } 243 244 if (split_num.significand > UINT16_MAX) { 245 return -ERANGE; 246 } 247 *num = (uint16_t)split_num.significand; 248 return 0; 249 } 250 251 int 252 spdk_json_number_to_int32(const struct spdk_json_val *val, int32_t *num) 253 { 254 struct spdk_json_num split_num; 255 int rc; 256 257 rc = json_number_split(val, &split_num); 258 if (rc) { 259 return rc; 260 } 261 262 if (split_num.exponent) { 263 return -ERANGE; 264 } 265 266 if (split_num.negative) { 267 if (split_num.significand > 2147483648) { /* abs(INT32_MIN) */ 268 return -ERANGE; 269 } 270 *num = (int32_t) - (int64_t)split_num.significand; 271 return 0; 272 } 273 274 /* positive */ 275 if (split_num.significand > INT32_MAX) { 276 return -ERANGE; 277 } 278 *num = (int32_t)split_num.significand; 279 return 0; 280 } 281 282 int 283 spdk_json_number_to_uint32(const struct spdk_json_val *val, uint32_t *num) 284 { 285 struct spdk_json_num split_num; 286 int rc; 287 288 rc = json_number_split(val, &split_num); 289 if (rc) { 290 return rc; 291 } 292 293 if (split_num.exponent || split_num.negative) { 294 return -ERANGE; 295 } 296 297 if (split_num.significand > UINT32_MAX) { 298 return -ERANGE; 299 } 300 *num = (uint32_t)split_num.significand; 301 return 0; 302 } 303 304 int 305 spdk_json_number_to_uint64(const struct spdk_json_val *val, uint64_t *num) 306 { 307 struct spdk_json_num split_num; 308 int rc; 309 310 rc = json_number_split(val, &split_num); 311 if (rc) { 312 return rc; 313 } 314 315 if (split_num.exponent || split_num.negative) { 316 return -ERANGE; 317 } 318 319 *num = split_num.significand; 320 return 0; 321 } 322 323 static int 324 _json_decode_object(const struct spdk_json_val *values, 325 const struct spdk_json_object_decoder *decoders, size_t num_decoders, void *out, bool relaxed) 326 { 327 uint32_t i; 328 bool invalid = false; 329 size_t decidx; 330 bool *seen; 331 332 if (values == NULL || values->type != SPDK_JSON_VAL_OBJECT_BEGIN) { 333 return -1; 334 } 335 336 seen = calloc(sizeof(bool), num_decoders); 337 if (seen == NULL) { 338 return -1; 339 } 340 341 for (i = 0; i < values->len;) { 342 const struct spdk_json_val *name = &values[i + 1]; 343 const struct spdk_json_val *v = &values[i + 2]; 344 bool found = false; 345 346 for (decidx = 0; decidx < num_decoders; decidx++) { 347 const struct spdk_json_object_decoder *dec = &decoders[decidx]; 348 if (spdk_json_strequal(name, dec->name)) { 349 void *field = (void *)((uintptr_t)out + dec->offset); 350 351 found = true; 352 353 if (seen[decidx]) { 354 /* duplicate field name */ 355 invalid = true; 356 SPDK_JSON_DEBUG("Duplicate key '%s'\n", dec->name); 357 } else { 358 seen[decidx] = true; 359 if (dec->decode_func(v, field)) { 360 invalid = true; 361 SPDK_JSON_DEBUG("Decoder failed to decode key '%s'\n", dec->name); 362 /* keep going to fill out any other valid keys */ 363 } 364 } 365 break; 366 } 367 } 368 369 if (!relaxed && !found) { 370 invalid = true; 371 SPDK_JSON_DEBUG("Decoder not found for key '%.*s'\n", name->len, (char *)name->start); 372 } 373 374 i += 1 + spdk_json_val_len(v); 375 } 376 377 for (decidx = 0; decidx < num_decoders; decidx++) { 378 if (!decoders[decidx].optional && !seen[decidx]) { 379 /* required field is missing */ 380 invalid = true; 381 break; 382 } 383 } 384 385 free(seen); 386 return invalid ? -1 : 0; 387 } 388 389 void 390 spdk_json_free_object(const struct spdk_json_object_decoder *decoders, size_t num_decoders, 391 void *obj) 392 { 393 struct spdk_json_val invalid_val = { 394 .start = "", 395 .len = 0, 396 .type = SPDK_JSON_VAL_INVALID 397 }; 398 size_t decidx; 399 400 for (decidx = 0; decidx < num_decoders; decidx++) { 401 const struct spdk_json_object_decoder *dec = &decoders[decidx]; 402 void *field = (void *)((uintptr_t)obj + dec->offset); 403 404 /* decoding an invalid value will free the 405 * previous memory without allocating it again. 406 */ 407 dec->decode_func(&invalid_val, field); 408 } 409 } 410 411 412 int 413 spdk_json_decode_object(const struct spdk_json_val *values, 414 const struct spdk_json_object_decoder *decoders, size_t num_decoders, void *out) 415 { 416 return _json_decode_object(values, decoders, num_decoders, out, false); 417 } 418 419 int 420 spdk_json_decode_object_relaxed(const struct spdk_json_val *values, 421 const struct spdk_json_object_decoder *decoders, size_t num_decoders, void *out) 422 { 423 return _json_decode_object(values, decoders, num_decoders, out, true); 424 } 425 426 int 427 spdk_json_decode_array(const struct spdk_json_val *values, spdk_json_decode_fn decode_func, 428 void *out, size_t max_size, size_t *out_size, size_t stride) 429 { 430 uint32_t i; 431 char *field; 432 char *out_end; 433 434 if (values == NULL || values->type != SPDK_JSON_VAL_ARRAY_BEGIN) { 435 return -1; 436 } 437 438 *out_size = 0; 439 field = out; 440 out_end = field + max_size * stride; 441 for (i = 0; i < values->len;) { 442 const struct spdk_json_val *v = &values[i + 1]; 443 444 if (field == out_end) { 445 return -1; 446 } 447 448 if (decode_func(v, field)) { 449 return -1; 450 } 451 452 i += spdk_json_val_len(v); 453 field += stride; 454 (*out_size)++; 455 } 456 457 return 0; 458 } 459 460 int 461 spdk_json_decode_bool(const struct spdk_json_val *val, void *out) 462 { 463 bool *f = out; 464 465 if (val->type != SPDK_JSON_VAL_TRUE && val->type != SPDK_JSON_VAL_FALSE) { 466 return -1; 467 } 468 469 *f = val->type == SPDK_JSON_VAL_TRUE; 470 return 0; 471 } 472 473 int 474 spdk_json_decode_uint8(const struct spdk_json_val *val, void *out) 475 { 476 uint8_t *i = out; 477 478 return spdk_json_number_to_uint8(val, i); 479 } 480 481 int 482 spdk_json_decode_uint16(const struct spdk_json_val *val, void *out) 483 { 484 uint16_t *i = out; 485 486 return spdk_json_number_to_uint16(val, i); 487 } 488 489 int 490 spdk_json_decode_int32(const struct spdk_json_val *val, void *out) 491 { 492 int32_t *i = out; 493 494 return spdk_json_number_to_int32(val, i); 495 } 496 497 int 498 spdk_json_decode_uint32(const struct spdk_json_val *val, void *out) 499 { 500 uint32_t *i = out; 501 502 return spdk_json_number_to_uint32(val, i); 503 } 504 505 int 506 spdk_json_decode_uint64(const struct spdk_json_val *val, void *out) 507 { 508 uint64_t *i = out; 509 510 return spdk_json_number_to_uint64(val, i); 511 } 512 513 int 514 spdk_json_decode_string(const struct spdk_json_val *val, void *out) 515 { 516 char **s = out; 517 518 free(*s); 519 520 *s = spdk_json_strdup(val); 521 522 if (*s) { 523 return 0; 524 } else { 525 return -1; 526 } 527 } 528 529 static struct spdk_json_val * 530 json_first(struct spdk_json_val *object, enum spdk_json_val_type type) 531 { 532 /* 'object' must be JSON object or array. 'type' might be combination of these two. */ 533 assert((type & (SPDK_JSON_VAL_ARRAY_BEGIN | SPDK_JSON_VAL_OBJECT_BEGIN)) != 0); 534 535 assert(object != NULL); 536 537 if ((object->type & type) == 0) { 538 return NULL; 539 } 540 541 object++; 542 if (object->len == 0) { 543 return NULL; 544 } 545 546 return object; 547 } 548 549 static struct spdk_json_val * 550 json_value(struct spdk_json_val *key) 551 { 552 return key->type == SPDK_JSON_VAL_NAME ? key + 1 : NULL; 553 } 554 555 int 556 spdk_json_find(struct spdk_json_val *object, const char *key_name, struct spdk_json_val **key, 557 struct spdk_json_val **val, enum spdk_json_val_type type) 558 { 559 struct spdk_json_val *_key = NULL; 560 struct spdk_json_val *_val = NULL; 561 struct spdk_json_val *it; 562 563 assert(object != NULL); 564 565 for (it = json_first(object, SPDK_JSON_VAL_ARRAY_BEGIN | SPDK_JSON_VAL_OBJECT_BEGIN); 566 it != NULL; 567 it = spdk_json_next(it)) { 568 if (it->type != SPDK_JSON_VAL_NAME) { 569 continue; 570 } 571 572 if (spdk_json_strequal(it, key_name) != true) { 573 continue; 574 } 575 576 if (_key) { 577 SPDK_JSON_DEBUG("Duplicate key '%s'", key_name); 578 return -EINVAL; 579 } 580 581 _key = it; 582 _val = json_value(_key); 583 584 if (type != SPDK_JSON_VAL_INVALID && (_val->type & type) == 0) { 585 SPDK_JSON_DEBUG("key '%s' type is %#x but expected one of %#x\n", key_name, _val->type, type); 586 return -EDOM; 587 } 588 } 589 590 if (key) { 591 *key = _key; 592 } 593 594 if (val) { 595 *val = _val; 596 } 597 598 return _val ? 0 : -ENOENT; 599 } 600 601 int 602 spdk_json_find_string(struct spdk_json_val *object, const char *key_name, 603 struct spdk_json_val **key, struct spdk_json_val **val) 604 { 605 return spdk_json_find(object, key_name, key, val, SPDK_JSON_VAL_STRING); 606 } 607 608 int 609 spdk_json_find_array(struct spdk_json_val *object, const char *key_name, 610 struct spdk_json_val **key, struct spdk_json_val **val) 611 { 612 return spdk_json_find(object, key_name, key, val, SPDK_JSON_VAL_ARRAY_BEGIN); 613 } 614 615 struct spdk_json_val * 616 spdk_json_object_first(struct spdk_json_val *object) 617 { 618 struct spdk_json_val *first = json_first(object, SPDK_JSON_VAL_OBJECT_BEGIN); 619 620 /* Empty object? */ 621 return first && first->type != SPDK_JSON_VAL_OBJECT_END ? first : NULL; 622 } 623 624 struct spdk_json_val * 625 spdk_json_array_first(struct spdk_json_val *array_begin) 626 { 627 struct spdk_json_val *first = json_first(array_begin, SPDK_JSON_VAL_ARRAY_BEGIN); 628 629 /* Empty array? */ 630 return first && first->type != SPDK_JSON_VAL_ARRAY_END ? first : NULL; 631 } 632 633 static struct spdk_json_val * 634 json_skip_object_or_array(struct spdk_json_val *val) 635 { 636 unsigned lvl; 637 enum spdk_json_val_type end_type; 638 struct spdk_json_val *it; 639 640 if (val->type == SPDK_JSON_VAL_OBJECT_BEGIN) { 641 end_type = SPDK_JSON_VAL_OBJECT_END; 642 } else if (val->type == SPDK_JSON_VAL_ARRAY_BEGIN) { 643 end_type = SPDK_JSON_VAL_ARRAY_END; 644 } else { 645 SPDK_JSON_DEBUG("Expected JSON object (%#x) or array (%#x) but got %#x\n", 646 SPDK_JSON_VAL_OBJECT_BEGIN, SPDK_JSON_VAL_ARRAY_BEGIN, val->type); 647 return NULL; 648 } 649 650 lvl = 1; 651 for (it = val + 1; it->type != SPDK_JSON_VAL_INVALID && lvl != 0; it++) { 652 if (it->type == val->type) { 653 lvl++; 654 } else if (it->type == end_type) { 655 lvl--; 656 } 657 } 658 659 /* if lvl != 0 we have invalid JSON object */ 660 if (lvl != 0) { 661 SPDK_JSON_DEBUG("Can't find end of object (type: %#x): lvl (%u) != 0)\n", val->type, lvl); 662 it = NULL; 663 } 664 665 return it; 666 } 667 668 struct spdk_json_val * 669 spdk_json_next(struct spdk_json_val *it) 670 { 671 struct spdk_json_val *val, *next; 672 673 switch (it->type) { 674 case SPDK_JSON_VAL_NAME: 675 val = json_value(it); 676 next = spdk_json_next(val); 677 break; 678 679 /* We are in the middle of an array - get to next entry */ 680 case SPDK_JSON_VAL_NULL: 681 case SPDK_JSON_VAL_TRUE: 682 case SPDK_JSON_VAL_FALSE: 683 case SPDK_JSON_VAL_NUMBER: 684 case SPDK_JSON_VAL_STRING: 685 val = it + 1; 686 return val; 687 688 case SPDK_JSON_VAL_ARRAY_BEGIN: 689 case SPDK_JSON_VAL_OBJECT_BEGIN: 690 next = json_skip_object_or_array(it); 691 break; 692 693 /* Can't go to the next object if started from the end of array or object */ 694 case SPDK_JSON_VAL_ARRAY_END: 695 case SPDK_JSON_VAL_OBJECT_END: 696 case SPDK_JSON_VAL_INVALID: 697 return NULL; 698 default: 699 assert(false); 700 return NULL; 701 702 } 703 704 /* EOF ? */ 705 if (next == NULL) { 706 return NULL; 707 } 708 709 switch (next->type) { 710 case SPDK_JSON_VAL_ARRAY_END: 711 case SPDK_JSON_VAL_OBJECT_END: 712 case SPDK_JSON_VAL_INVALID: 713 return NULL; 714 default: 715 /* Next value */ 716 return next; 717 } 718 } 719 720 SPDK_LOG_REGISTER_COMPONENT(json_util) 721