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_uint16(const struct spdk_json_val *val, uint16_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 > UINT16_MAX) { 223 return -ERANGE; 224 } 225 *num = (uint16_t)split_num.significand; 226 return 0; 227 } 228 229 int 230 spdk_json_number_to_int32(const struct spdk_json_val *val, int32_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) { 241 return -ERANGE; 242 } 243 244 if (split_num.negative) { 245 if (split_num.significand > 2147483648) { /* abs(INT32_MIN) */ 246 return -ERANGE; 247 } 248 *num = (int32_t) - (int64_t)split_num.significand; 249 return 0; 250 } 251 252 /* positive */ 253 if (split_num.significand > INT32_MAX) { 254 return -ERANGE; 255 } 256 *num = (int32_t)split_num.significand; 257 return 0; 258 } 259 260 int 261 spdk_json_number_to_uint32(const struct spdk_json_val *val, uint32_t *num) 262 { 263 struct spdk_json_num split_num; 264 int rc; 265 266 rc = json_number_split(val, &split_num); 267 if (rc) { 268 return rc; 269 } 270 271 if (split_num.exponent || split_num.negative) { 272 return -ERANGE; 273 } 274 275 if (split_num.significand > UINT32_MAX) { 276 return -ERANGE; 277 } 278 *num = (uint32_t)split_num.significand; 279 return 0; 280 } 281 282 int 283 spdk_json_number_to_uint64(const struct spdk_json_val *val, uint64_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 *num = split_num.significand; 298 return 0; 299 } 300 301 static int 302 _json_decode_object(const struct spdk_json_val *values, 303 const struct spdk_json_object_decoder *decoders, size_t num_decoders, void *out, bool relaxed) 304 { 305 uint32_t i; 306 bool invalid = false; 307 size_t decidx; 308 bool *seen; 309 310 if (values == NULL || values->type != SPDK_JSON_VAL_OBJECT_BEGIN) { 311 return -1; 312 } 313 314 seen = calloc(sizeof(bool), num_decoders); 315 if (seen == NULL) { 316 return -1; 317 } 318 319 for (i = 0; i < values->len;) { 320 const struct spdk_json_val *name = &values[i + 1]; 321 const struct spdk_json_val *v = &values[i + 2]; 322 bool found = false; 323 324 for (decidx = 0; decidx < num_decoders; decidx++) { 325 const struct spdk_json_object_decoder *dec = &decoders[decidx]; 326 if (spdk_json_strequal(name, dec->name)) { 327 void *field = (void *)((uintptr_t)out + dec->offset); 328 329 found = true; 330 331 if (seen[decidx]) { 332 /* duplicate field name */ 333 invalid = true; 334 SPDK_JSON_DEBUG("Duplicate key '%s'\n", dec->name); 335 } else { 336 seen[decidx] = true; 337 if (dec->decode_func(v, field)) { 338 invalid = true; 339 SPDK_JSON_DEBUG("Decoder failed to decode key '%s'\n", dec->name); 340 /* keep going to fill out any other valid keys */ 341 } 342 } 343 break; 344 } 345 } 346 347 if (!relaxed && !found) { 348 invalid = true; 349 SPDK_JSON_DEBUG("Decoder not found for key '%.*s'\n", name->len, (char *)name->start); 350 } 351 352 i += 1 + spdk_json_val_len(v); 353 } 354 355 for (decidx = 0; decidx < num_decoders; decidx++) { 356 if (!decoders[decidx].optional && !seen[decidx]) { 357 /* required field is missing */ 358 invalid = true; 359 break; 360 } 361 } 362 363 free(seen); 364 return invalid ? -1 : 0; 365 } 366 367 void 368 spdk_json_free_object(const struct spdk_json_object_decoder *decoders, size_t num_decoders, 369 void *obj) 370 { 371 struct spdk_json_val invalid_val = { 372 .start = "", 373 .len = 0, 374 .type = SPDK_JSON_VAL_INVALID 375 }; 376 size_t decidx; 377 378 for (decidx = 0; decidx < num_decoders; decidx++) { 379 const struct spdk_json_object_decoder *dec = &decoders[decidx]; 380 void *field = (void *)((uintptr_t)obj + dec->offset); 381 382 /* decoding an invalid value will free the 383 * previous memory without allocating it again. 384 */ 385 dec->decode_func(&invalid_val, field); 386 } 387 } 388 389 390 int 391 spdk_json_decode_object(const struct spdk_json_val *values, 392 const struct spdk_json_object_decoder *decoders, size_t num_decoders, void *out) 393 { 394 return _json_decode_object(values, decoders, num_decoders, out, false); 395 } 396 397 int 398 spdk_json_decode_object_relaxed(const struct spdk_json_val *values, 399 const struct spdk_json_object_decoder *decoders, size_t num_decoders, void *out) 400 { 401 return _json_decode_object(values, decoders, num_decoders, out, true); 402 } 403 404 int 405 spdk_json_decode_array(const struct spdk_json_val *values, spdk_json_decode_fn decode_func, 406 void *out, size_t max_size, size_t *out_size, size_t stride) 407 { 408 uint32_t i; 409 char *field; 410 char *out_end; 411 412 if (values == NULL || values->type != SPDK_JSON_VAL_ARRAY_BEGIN) { 413 return -1; 414 } 415 416 *out_size = 0; 417 field = out; 418 out_end = field + max_size * stride; 419 for (i = 0; i < values->len;) { 420 const struct spdk_json_val *v = &values[i + 1]; 421 422 if (field == out_end) { 423 return -1; 424 } 425 426 if (decode_func(v, field)) { 427 return -1; 428 } 429 430 i += spdk_json_val_len(v); 431 field += stride; 432 (*out_size)++; 433 } 434 435 return 0; 436 } 437 438 int 439 spdk_json_decode_bool(const struct spdk_json_val *val, void *out) 440 { 441 bool *f = out; 442 443 if (val->type != SPDK_JSON_VAL_TRUE && val->type != SPDK_JSON_VAL_FALSE) { 444 return -1; 445 } 446 447 *f = val->type == SPDK_JSON_VAL_TRUE; 448 return 0; 449 } 450 451 int 452 spdk_json_decode_uint16(const struct spdk_json_val *val, void *out) 453 { 454 uint16_t *i = out; 455 456 return spdk_json_number_to_uint16(val, i); 457 } 458 459 int 460 spdk_json_decode_int32(const struct spdk_json_val *val, void *out) 461 { 462 int32_t *i = out; 463 464 return spdk_json_number_to_int32(val, i); 465 } 466 467 int 468 spdk_json_decode_uint32(const struct spdk_json_val *val, void *out) 469 { 470 uint32_t *i = out; 471 472 return spdk_json_number_to_uint32(val, i); 473 } 474 475 int 476 spdk_json_decode_uint64(const struct spdk_json_val *val, void *out) 477 { 478 uint64_t *i = out; 479 480 return spdk_json_number_to_uint64(val, i); 481 } 482 483 int 484 spdk_json_decode_string(const struct spdk_json_val *val, void *out) 485 { 486 char **s = out; 487 488 free(*s); 489 490 *s = spdk_json_strdup(val); 491 492 if (*s) { 493 return 0; 494 } else { 495 return -1; 496 } 497 } 498 499 static struct spdk_json_val * 500 json_first(struct spdk_json_val *object, enum spdk_json_val_type type) 501 { 502 /* 'object' must be JSON object or array. 'type' might be combination of these two. */ 503 assert((type & (SPDK_JSON_VAL_ARRAY_BEGIN | SPDK_JSON_VAL_OBJECT_BEGIN)) != 0); 504 505 assert(object != NULL); 506 507 if ((object->type & type) == 0) { 508 return NULL; 509 } 510 511 object++; 512 if (object->len == 0) { 513 return NULL; 514 } 515 516 return object; 517 } 518 519 static struct spdk_json_val * 520 json_value(struct spdk_json_val *key) 521 { 522 return key->type == SPDK_JSON_VAL_NAME ? key + 1 : NULL; 523 } 524 525 int 526 spdk_json_find(struct spdk_json_val *object, const char *key_name, struct spdk_json_val **key, 527 struct spdk_json_val **val, enum spdk_json_val_type type) 528 { 529 struct spdk_json_val *_key = NULL; 530 struct spdk_json_val *_val = NULL; 531 struct spdk_json_val *it; 532 533 assert(object != NULL); 534 535 for (it = json_first(object, SPDK_JSON_VAL_ARRAY_BEGIN | SPDK_JSON_VAL_OBJECT_BEGIN); 536 it != NULL; 537 it = spdk_json_next(it)) { 538 if (it->type != SPDK_JSON_VAL_NAME) { 539 continue; 540 } 541 542 if (spdk_json_strequal(it, key_name) != true) { 543 continue; 544 } 545 546 if (_key) { 547 SPDK_JSON_DEBUG("Duplicate key '%s'", key_name); 548 return -EINVAL; 549 } 550 551 _key = it; 552 _val = json_value(_key); 553 554 if (type != SPDK_JSON_VAL_INVALID && (_val->type & type) == 0) { 555 SPDK_JSON_DEBUG("key '%s' type is %#x but expected one of %#x\n", key_name, _val->type, type); 556 return -EDOM; 557 } 558 } 559 560 if (key) { 561 *key = _key; 562 } 563 564 if (val) { 565 *val = _val; 566 } 567 568 return _val ? 0 : -ENOENT; 569 } 570 571 int 572 spdk_json_find_string(struct spdk_json_val *object, const char *key_name, 573 struct spdk_json_val **key, struct spdk_json_val **val) 574 { 575 return spdk_json_find(object, key_name, key, val, SPDK_JSON_VAL_STRING); 576 } 577 578 int 579 spdk_json_find_array(struct spdk_json_val *object, const char *key_name, 580 struct spdk_json_val **key, struct spdk_json_val **val) 581 { 582 return spdk_json_find(object, key_name, key, val, SPDK_JSON_VAL_ARRAY_BEGIN); 583 } 584 585 struct spdk_json_val * 586 spdk_json_object_first(struct spdk_json_val *object) 587 { 588 struct spdk_json_val *first = json_first(object, SPDK_JSON_VAL_OBJECT_BEGIN); 589 590 /* Empty object? */ 591 return first && first->type != SPDK_JSON_VAL_OBJECT_END ? first : NULL; 592 } 593 594 struct spdk_json_val * 595 spdk_json_array_first(struct spdk_json_val *array_begin) 596 { 597 struct spdk_json_val *first = json_first(array_begin, SPDK_JSON_VAL_ARRAY_BEGIN); 598 599 /* Empty array? */ 600 return first && first->type != SPDK_JSON_VAL_ARRAY_END ? first : NULL; 601 } 602 603 static struct spdk_json_val * 604 json_skip_object_or_array(struct spdk_json_val *val) 605 { 606 unsigned lvl; 607 enum spdk_json_val_type end_type; 608 struct spdk_json_val *it; 609 610 if (val->type == SPDK_JSON_VAL_OBJECT_BEGIN) { 611 end_type = SPDK_JSON_VAL_OBJECT_END; 612 } else if (val->type == SPDK_JSON_VAL_ARRAY_BEGIN) { 613 end_type = SPDK_JSON_VAL_ARRAY_END; 614 } else { 615 SPDK_JSON_DEBUG("Expected JSON object (%#x) or array (%#x) but got %#x\n", 616 SPDK_JSON_VAL_OBJECT_BEGIN, SPDK_JSON_VAL_ARRAY_BEGIN, val->type); 617 return NULL; 618 } 619 620 lvl = 1; 621 for (it = val + 1; it->type != SPDK_JSON_VAL_INVALID && lvl != 0; it++) { 622 if (it->type == val->type) { 623 lvl++; 624 } else if (it->type == end_type) { 625 lvl--; 626 } 627 } 628 629 /* if lvl != 0 we have invalid JSON object */ 630 if (lvl != 0) { 631 SPDK_JSON_DEBUG("Can't find end of object (type: %#x): lvl (%u) != 0)\n", val->type, lvl); 632 it = NULL; 633 } 634 635 return it; 636 } 637 638 struct spdk_json_val * 639 spdk_json_next(struct spdk_json_val *it) 640 { 641 struct spdk_json_val *val, *next; 642 643 switch (it->type) { 644 case SPDK_JSON_VAL_NAME: 645 val = json_value(it); 646 next = spdk_json_next(val); 647 break; 648 649 /* We are in the middle of an array - get to next entry */ 650 case SPDK_JSON_VAL_NULL: 651 case SPDK_JSON_VAL_TRUE: 652 case SPDK_JSON_VAL_FALSE: 653 case SPDK_JSON_VAL_NUMBER: 654 case SPDK_JSON_VAL_STRING: 655 val = it + 1; 656 return val; 657 658 case SPDK_JSON_VAL_ARRAY_BEGIN: 659 case SPDK_JSON_VAL_OBJECT_BEGIN: 660 next = json_skip_object_or_array(it); 661 break; 662 663 /* Can't go to the next object if started from the end of array or object */ 664 case SPDK_JSON_VAL_ARRAY_END: 665 case SPDK_JSON_VAL_OBJECT_END: 666 case SPDK_JSON_VAL_INVALID: 667 return NULL; 668 default: 669 assert(false); 670 return NULL; 671 672 } 673 674 /* EOF ? */ 675 if (next == NULL) { 676 return NULL; 677 } 678 679 switch (next->type) { 680 case SPDK_JSON_VAL_ARRAY_END: 681 case SPDK_JSON_VAL_OBJECT_END: 682 case SPDK_JSON_VAL_INVALID: 683 return NULL; 684 default: 685 /* Next value */ 686 return next; 687 } 688 } 689 690 SPDK_LOG_REGISTER_COMPONENT(json_util) 691