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