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_internal/log.h" 38 39 #define SPDK_JSON_DEBUG(...) SPDK_DEBUGLOG(SPDK_LOG_JSON, __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 int 368 spdk_json_decode_object(const struct spdk_json_val *values, 369 const struct spdk_json_object_decoder *decoders, size_t num_decoders, void *out) 370 { 371 return _json_decode_object(values, decoders, num_decoders, out, false); 372 } 373 374 int 375 spdk_json_decode_object_relaxed(const struct spdk_json_val *values, 376 const struct spdk_json_object_decoder *decoders, size_t num_decoders, void *out) 377 { 378 return _json_decode_object(values, decoders, num_decoders, out, true); 379 } 380 381 int 382 spdk_json_decode_array(const struct spdk_json_val *values, spdk_json_decode_fn decode_func, 383 void *out, size_t max_size, size_t *out_size, size_t stride) 384 { 385 uint32_t i; 386 char *field; 387 char *out_end; 388 389 if (values == NULL || values->type != SPDK_JSON_VAL_ARRAY_BEGIN) { 390 return -1; 391 } 392 393 *out_size = 0; 394 field = out; 395 out_end = field + max_size * stride; 396 for (i = 0; i < values->len;) { 397 const struct spdk_json_val *v = &values[i + 1]; 398 399 if (field == out_end) { 400 return -1; 401 } 402 403 if (decode_func(v, field)) { 404 return -1; 405 } 406 407 i += spdk_json_val_len(v); 408 field += stride; 409 (*out_size)++; 410 } 411 412 return 0; 413 } 414 415 int 416 spdk_json_decode_bool(const struct spdk_json_val *val, void *out) 417 { 418 bool *f = out; 419 420 if (val->type != SPDK_JSON_VAL_TRUE && val->type != SPDK_JSON_VAL_FALSE) { 421 return -1; 422 } 423 424 *f = val->type == SPDK_JSON_VAL_TRUE; 425 return 0; 426 } 427 428 int 429 spdk_json_decode_uint16(const struct spdk_json_val *val, void *out) 430 { 431 uint16_t *i = out; 432 433 return spdk_json_number_to_uint16(val, i); 434 } 435 436 int 437 spdk_json_decode_int32(const struct spdk_json_val *val, void *out) 438 { 439 int32_t *i = out; 440 441 return spdk_json_number_to_int32(val, i); 442 } 443 444 int 445 spdk_json_decode_uint32(const struct spdk_json_val *val, void *out) 446 { 447 uint32_t *i = out; 448 449 return spdk_json_number_to_uint32(val, i); 450 } 451 452 int 453 spdk_json_decode_uint64(const struct spdk_json_val *val, void *out) 454 { 455 uint64_t *i = out; 456 457 return spdk_json_number_to_uint64(val, i); 458 } 459 460 int 461 spdk_json_decode_string(const struct spdk_json_val *val, void *out) 462 { 463 char **s = out; 464 465 free(*s); 466 467 *s = spdk_json_strdup(val); 468 469 if (*s) { 470 return 0; 471 } else { 472 return -1; 473 } 474 } 475 476 static struct spdk_json_val * 477 json_first(struct spdk_json_val *object, enum spdk_json_val_type type) 478 { 479 /* 'object' must be JSON object or array. 'type' might be combination of these two. */ 480 assert((type & (SPDK_JSON_VAL_ARRAY_BEGIN | SPDK_JSON_VAL_OBJECT_BEGIN)) != 0); 481 482 assert(object != NULL); 483 484 if ((object->type & type) == 0) { 485 return NULL; 486 } 487 488 object++; 489 if (object->len == 0) { 490 return NULL; 491 } 492 493 return object; 494 } 495 496 static struct spdk_json_val * 497 json_value(struct spdk_json_val *key) 498 { 499 return key->type == SPDK_JSON_VAL_NAME ? key + 1 : NULL; 500 } 501 502 int 503 spdk_json_find(struct spdk_json_val *object, const char *key_name, struct spdk_json_val **key, 504 struct spdk_json_val **val, enum spdk_json_val_type type) 505 { 506 struct spdk_json_val *_key = NULL; 507 struct spdk_json_val *_val = NULL; 508 struct spdk_json_val *it; 509 510 assert(object != NULL); 511 512 for (it = json_first(object, SPDK_JSON_VAL_ARRAY_BEGIN | SPDK_JSON_VAL_OBJECT_BEGIN); 513 it != NULL; 514 it = spdk_json_next(it)) { 515 if (it->type != SPDK_JSON_VAL_NAME) { 516 continue; 517 } 518 519 if (spdk_json_strequal(it, key_name) != true) { 520 continue; 521 } 522 523 if (_key) { 524 SPDK_JSON_DEBUG("Duplicate key '%s'", key_name); 525 return -EINVAL; 526 } 527 528 _key = it; 529 _val = json_value(_key); 530 531 if (type != SPDK_JSON_VAL_INVALID && (_val->type & type) == 0) { 532 SPDK_JSON_DEBUG("key '%s' type is %#x but expected one of %#x\n", key_name, _val->type, type); 533 return -EDOM; 534 } 535 } 536 537 if (key) { 538 *key = _key; 539 } 540 541 if (val) { 542 *val = _val; 543 } 544 545 return _val ? 0 : -ENOENT; 546 } 547 548 int 549 spdk_json_find_string(struct spdk_json_val *object, const char *key_name, 550 struct spdk_json_val **key, struct spdk_json_val **val) 551 { 552 return spdk_json_find(object, key_name, key, val, SPDK_JSON_VAL_STRING); 553 } 554 555 int 556 spdk_json_find_array(struct spdk_json_val *object, const char *key_name, 557 struct spdk_json_val **key, struct spdk_json_val **val) 558 { 559 return spdk_json_find(object, key_name, key, val, SPDK_JSON_VAL_ARRAY_BEGIN); 560 } 561 562 struct spdk_json_val * 563 spdk_json_object_first(struct spdk_json_val *object) 564 { 565 struct spdk_json_val *first = json_first(object, SPDK_JSON_VAL_OBJECT_BEGIN); 566 567 /* Empty object? */ 568 return first && first->type != SPDK_JSON_VAL_OBJECT_END ? first : NULL; 569 } 570 571 struct spdk_json_val * 572 spdk_json_array_first(struct spdk_json_val *array_begin) 573 { 574 struct spdk_json_val *first = json_first(array_begin, SPDK_JSON_VAL_ARRAY_BEGIN); 575 576 /* Empty array? */ 577 return first && first->type != SPDK_JSON_VAL_ARRAY_END ? first : NULL; 578 } 579 580 static struct spdk_json_val * 581 json_skip_object_or_array(struct spdk_json_val *val) 582 { 583 unsigned lvl; 584 enum spdk_json_val_type end_type; 585 struct spdk_json_val *it; 586 587 if (val->type == SPDK_JSON_VAL_OBJECT_BEGIN) { 588 end_type = SPDK_JSON_VAL_OBJECT_END; 589 } else if (val->type == SPDK_JSON_VAL_ARRAY_BEGIN) { 590 end_type = SPDK_JSON_VAL_ARRAY_END; 591 } else { 592 SPDK_JSON_DEBUG("Expected JSON object (%#x) or array (%#x) but got %#x\n", 593 SPDK_JSON_VAL_OBJECT_BEGIN, SPDK_JSON_VAL_ARRAY_BEGIN, val->type); 594 return NULL; 595 } 596 597 lvl = 1; 598 for (it = val + 1; it->type != SPDK_JSON_VAL_INVALID && lvl != 0; it++) { 599 if (it->type == val->type) { 600 lvl++; 601 } else if (it->type == end_type) { 602 lvl--; 603 } 604 } 605 606 /* if lvl != 0 we have invalid JSON object */ 607 if (lvl != 0) { 608 SPDK_JSON_DEBUG("Can't find end of object (type: %#x): lvl (%u) != 0)\n", val->type, lvl); 609 it = NULL; 610 } 611 612 return it; 613 } 614 615 struct spdk_json_val * 616 spdk_json_next(struct spdk_json_val *it) 617 { 618 struct spdk_json_val *val, *next; 619 620 switch (it->type) { 621 case SPDK_JSON_VAL_NAME: 622 val = json_value(it); 623 next = spdk_json_next(val); 624 break; 625 626 /* We are in the middle of an array - get to next entry */ 627 case SPDK_JSON_VAL_NULL: 628 case SPDK_JSON_VAL_TRUE: 629 case SPDK_JSON_VAL_FALSE: 630 case SPDK_JSON_VAL_NUMBER: 631 case SPDK_JSON_VAL_STRING: 632 val = it + 1; 633 return val; 634 635 case SPDK_JSON_VAL_ARRAY_BEGIN: 636 case SPDK_JSON_VAL_OBJECT_BEGIN: 637 next = json_skip_object_or_array(it); 638 break; 639 640 /* Can't go to the next object if started from the end of array or object */ 641 case SPDK_JSON_VAL_ARRAY_END: 642 case SPDK_JSON_VAL_OBJECT_END: 643 case SPDK_JSON_VAL_INVALID: 644 return NULL; 645 default: 646 assert(false); 647 return NULL; 648 649 } 650 651 /* EOF ? */ 652 if (next == NULL) { 653 return NULL; 654 } 655 656 switch (next->type) { 657 case SPDK_JSON_VAL_ARRAY_END: 658 case SPDK_JSON_VAL_OBJECT_END: 659 case SPDK_JSON_VAL_INVALID: 660 return NULL; 661 default: 662 /* Next value */ 663 return next; 664 } 665 } 666 667 SPDK_LOG_REGISTER_COMPONENT("json_util", SPDK_LOG_JSON) 668