1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
|
// Copyright (c) 2018-2020 Cesanta Software Limited
// All rights reserved
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include <float.h>
#include <math.h>
#include <import/mjson.h>
#if defined(_MSC_VER)
#define alloca(x) _alloca(x)
#endif
#if defined(_MSC_VER) && _MSC_VER < 1700
#define va_copy(x, y) (x) = (y)
#define isinf(x) !_finite(x)
#define isnan(x) _isnan(x)
#endif
static double mystrtod(const char *str, char **end);
static int mjson_esc(int c, int esc) {
const char *p, *esc1 = "\b\f\n\r\t\\\"", *esc2 = "bfnrt\\\"";
for (p = esc ? esc1 : esc2; *p != '\0'; p++) {
if (*p == c) return esc ? esc2[p - esc1] : esc1[p - esc2];
}
return 0;
}
static int mjson_escape(int c) {
return mjson_esc(c, 1);
}
static int mjson_pass_string(const char *s, int len) {
int i;
for (i = 0; i < len; i++) {
if (s[i] == '\\' && i + 1 < len && mjson_escape(s[i + 1])) {
i++;
} else if (s[i] == '\0') {
return MJSON_ERROR_INVALID_INPUT;
} else if (s[i] == '"') {
return i;
}
}
return MJSON_ERROR_INVALID_INPUT;
}
int mjson(const char *s, int len, mjson_cb_t cb, void *ud) {
enum { S_VALUE, S_KEY, S_COLON, S_COMMA_OR_EOO } expecting = S_VALUE;
unsigned char nesting[MJSON_MAX_DEPTH];
int i, depth = 0;
#define MJSONCALL(ev) \
if (cb != NULL && cb(ev, s, start, i - start + 1, ud)) return i + 1;
// In the ascii table, the distance between `[` and `]` is 2.
// Ditto for `{` and `}`. Hence +2 in the code below.
#define MJSONEOO() \
do { \
if (c != nesting[depth - 1] + 2) return MJSON_ERROR_INVALID_INPUT; \
depth--; \
if (depth == 0) { \
MJSONCALL(tok); \
return i + 1; \
} \
} while (0)
for (i = 0; i < len; i++) {
int start = i;
unsigned char c = ((unsigned char *) s)[i];
int tok = c;
if (c == ' ' || c == '\t' || c == '\n' || c == '\r') continue;
// printf("- %c [%.*s] %d %d\n", c, i, s, depth, expecting);
switch (expecting) {
case S_VALUE:
if (c == '{') {
if (depth >= (int) sizeof(nesting)) return MJSON_ERROR_TOO_DEEP;
nesting[depth++] = c;
expecting = S_KEY;
break;
} else if (c == '[') {
if (depth >= (int) sizeof(nesting)) return MJSON_ERROR_TOO_DEEP;
nesting[depth++] = c;
break;
} else if (c == ']' && depth > 0) { // Empty array
MJSONEOO();
} else if (c == 't' && i + 3 < len && memcmp(&s[i], "true", 4) == 0) {
i += 3;
tok = MJSON_TOK_TRUE;
} else if (c == 'n' && i + 3 < len && memcmp(&s[i], "null", 4) == 0) {
i += 3;
tok = MJSON_TOK_NULL;
} else if (c == 'f' && i + 4 < len && memcmp(&s[i], "false", 5) == 0) {
i += 4;
tok = MJSON_TOK_FALSE;
} else if (c == '-' || ((c >= '0' && c <= '9'))) {
char *end = NULL;
mystrtod(&s[i], &end);
if (end != NULL) i += (int) (end - &s[i] - 1);
tok = MJSON_TOK_NUMBER;
} else if (c == '"') {
int n = mjson_pass_string(&s[i + 1], len - i - 1);
if (n < 0) return n;
i += n + 1;
tok = MJSON_TOK_STRING;
} else {
return MJSON_ERROR_INVALID_INPUT;
}
if (depth == 0) {
MJSONCALL(tok);
return i + 1;
}
expecting = S_COMMA_OR_EOO;
break;
case S_KEY:
if (c == '"') {
int n = mjson_pass_string(&s[i + 1], len - i - 1);
if (n < 0) return n;
i += n + 1;
tok = MJSON_TOK_KEY;
expecting = S_COLON;
} else if (c == '}') { // Empty object
MJSONEOO();
expecting = S_COMMA_OR_EOO;
} else {
return MJSON_ERROR_INVALID_INPUT;
}
break;
case S_COLON:
if (c == ':') {
expecting = S_VALUE;
} else {
return MJSON_ERROR_INVALID_INPUT;
}
break;
case S_COMMA_OR_EOO:
if (depth <= 0) return MJSON_ERROR_INVALID_INPUT;
if (c == ',') {
expecting = (nesting[depth - 1] == '{') ? S_KEY : S_VALUE;
} else if (c == ']' || c == '}') {
MJSONEOO();
} else {
return MJSON_ERROR_INVALID_INPUT;
}
break;
}
MJSONCALL(tok);
}
return MJSON_ERROR_INVALID_INPUT;
}
struct msjon_get_data {
const char *path; // Lookup json path
int pos; // Current path index
int d1; // Current depth of traversal
int d2; // Expected depth of traversal
int i1; // Index in an array
int i2; // Expected index in an array
int obj; // If the value is array/object, offset where it starts
const char **tokptr; // Destination
int *toklen; // Destination length
int tok; // Returned token
};
#include <stdio.h>
static int plen1(const char *s) {
int i = 0, n = 0;
while (s[i] != '\0' && s[i] != '.' && s[i] != '[')
n++, i += s[i] == '\\' ? 2 : 1;
// printf("PLEN: s: [%s], [%.*s] => %d\n", s, i, s, n);
return n;
}
static int plen2(const char *s) {
int i = 0, __attribute__((unused)) n = 0;
while (s[i] != '\0' && s[i] != '.' && s[i] != '[')
n++, i += s[i] == '\\' ? 2 : 1;
// printf("PLEN: s: [%s], [%.*s] => %d\n", s, i, s, n);
return i;
}
static int kcmp(const char *a, const char *b, int n) {
int i = 0, j = 0, r = 0;
for (i = 0, j = 0; j < n; i++, j++) {
if (b[i] == '\\') i++;
if ((r = a[j] - b[i]) != 0) return r;
}
// printf("KCMP: a: [%.*s], b:[%.*s] ==> %d\n", n, a, i, b, r);
return r;
}
static int mjson_get_cb(int tok, const char *s, int off, int len, void *ud) {
struct msjon_get_data *data = (struct msjon_get_data *) ud;
// printf("--> %2x %2d %2d %2d %2d\t'%s'\t'%.*s'\t\t'%.*s'\n", tok, data->d1,
// data->d2, data->i1, data->i2, data->path + data->pos, off, s, len,
// s + off);
if (data->tok != MJSON_TOK_INVALID) return 1; // Found
if (tok == '{') {
if (!data->path[data->pos] && data->d1 == data->d2) data->obj = off;
data->d1++;
} else if (tok == '[') {
if (data->d1 == data->d2 && data->path[data->pos] == '[') {
data->i1 = 0;
data->i2 = (int) mystrtod(&data->path[data->pos + 1], NULL);
if (data->i1 == data->i2) {
data->d2++;
data->pos += 3;
}
}
if (!data->path[data->pos] && data->d1 == data->d2) data->obj = off;
data->d1++;
} else if (tok == ',') {
if (data->d1 == data->d2 + 1) {
data->i1++;
if (data->i1 == data->i2) {
while (data->path[data->pos] != ']') data->pos++;
data->pos++;
data->d2++;
}
}
} else if (tok == MJSON_TOK_KEY && data->d1 == data->d2 + 1 &&
data->path[data->pos] == '.' && s[off] == '"' &&
s[off + len - 1] == '"' &&
plen1(&data->path[data->pos + 1]) == len - 2 &&
kcmp(s + off + 1, &data->path[data->pos + 1], len - 2) == 0) {
data->d2++;
data->pos += plen2(&data->path[data->pos + 1]) + 1;
} else if (tok == MJSON_TOK_KEY && data->d1 == data->d2) {
return 1; // Exhausted path, not found
} else if (tok == '}' || tok == ']') {
data->d1--;
// data->d2--;
if (!data->path[data->pos] && data->d1 == data->d2 && data->obj != -1) {
data->tok = tok - 2;
if (data->tokptr) *data->tokptr = s + data->obj;
if (data->toklen) *data->toklen = off - data->obj + 1;
return 1;
}
} else if (MJSON_TOK_IS_VALUE(tok)) {
// printf("TOK --> %d\n", tok);
if (data->d1 == data->d2 && !data->path[data->pos]) {
data->tok = tok;
if (data->tokptr) *data->tokptr = s + off;
if (data->toklen) *data->toklen = len;
return 1;
}
}
return 0;
}
enum mjson_tok mjson_find(const char *s, int len, const char *jp,
const char **tokptr, int *toklen) {
struct msjon_get_data data = {jp, 1, 0, 0, 0,
0, -1, tokptr, toklen, MJSON_TOK_INVALID};
if (jp[0] != '$') return MJSON_TOK_INVALID;
if (mjson(s, len, mjson_get_cb, &data) < 0) return MJSON_TOK_INVALID;
return (enum mjson_tok) data.tok;
}
int mjson_get_number(const char *s, int len, const char *path, double *v) {
const char *p;
int tok, n;
if ((tok = mjson_find(s, len, path, &p, &n)) == MJSON_TOK_NUMBER) {
if (v != NULL) *v = mystrtod(p, NULL);
}
return tok == MJSON_TOK_NUMBER ? 1 : 0;
}
int mjson_get_bool(const char *s, int len, const char *path, int *v) {
int tok = mjson_find(s, len, path, NULL, NULL);
if (tok == MJSON_TOK_TRUE && v != NULL) *v = 1;
if (tok == MJSON_TOK_FALSE && v != NULL) *v = 0;
return tok == MJSON_TOK_TRUE || tok == MJSON_TOK_FALSE ? 1 : 0;
}
static unsigned char mjson_unhex_nimble(const char *s) {
unsigned char i, v = 0;
for (i = 0; i < 2; i++) {
int c = s[i];
if (i > 0) v <<= 4;
v |= (c >= '0' && c <= '9') ? c - '0'
: (c >= 'A' && c <= 'F') ? c - '7' : c - 'W';
}
return v;
}
static int mjson_unescape(const char *s, int len, char *to, int n) {
int i, j;
for (i = 0, j = 0; i < len && j < n; i++, j++) {
if (s[i] == '\\' && i + 5 < len && s[i + 1] == 'u') {
// \uXXXX escape. We could process a simple one-byte chars
// \u00xx from the ASCII range. More complex chars would require
// dragging in a UTF8 library, which is too much for us
if (s[i + 2] != '0' || s[i + 3] != '0') return -1; // Too much, give up
to[j] = mjson_unhex_nimble(s + i + 4);
i += 5;
} else if (s[i] == '\\' && i + 1 < len) {
int c = mjson_esc(s[i + 1], 0);
if (c == 0) return -1;
to[j] = c;
i++;
} else {
to[j] = s[i];
}
}
if (j >= n) return -1;
if (n > 0) to[j] = '\0';
return j;
}
int mjson_get_string(const char *s, int len, const char *path, char *to,
int n) {
const char *p;
int sz;
if (mjson_find(s, len, path, &p, &sz) != MJSON_TOK_STRING) return -1;
return mjson_unescape(p + 1, sz - 2, to, n);
}
int mjson_get_hex(const char *s, int len, const char *x, char *to, int n) {
const char *p;
int i, j, sz;
if (mjson_find(s, len, x, &p, &sz) != MJSON_TOK_STRING) return -1;
for (i = j = 0; i < sz - 3 && j < n; i += 2, j++) {
((unsigned char *) to)[j] = mjson_unhex_nimble(p + i + 1);
}
if (j < n) to[j] = '\0';
return j;
}
#if MJSON_ENABLE_BASE64
static int mjson_base64rev(int c) {
if (c >= 'A' && c <= 'Z') {
return c - 'A';
} else if (c >= 'a' && c <= 'z') {
return c + 26 - 'a';
} else if (c >= '0' && c <= '9') {
return c + 52 - '0';
} else if (c == '+') {
return 62;
} else if (c == '/') {
return 63;
} else {
return 64;
}
}
int mjson_base64_dec(const char *src, int n, char *dst, int dlen) {
const char *end = src + n;
int len = 0;
while (src + 3 < end && len < dlen) {
int a = mjson_base64rev(src[0]), b = mjson_base64rev(src[1]),
c = mjson_base64rev(src[2]), d = mjson_base64rev(src[3]);
dst[len++] = (a << 2) | (b >> 4);
if (src[2] != '=' && len < dlen) {
dst[len++] = (b << 4) | (c >> 2);
if (src[3] != '=' && len < dlen) {
dst[len++] = (c << 6) | d;
}
}
src += 4;
}
if (len < dlen) dst[len] = '\0';
return len;
}
int mjson_get_base64(const char *s, int len, const char *path, char *to,
int n) {
const char *p;
int sz;
if (mjson_find(s, len, path, &p, &sz) != MJSON_TOK_STRING) return 0;
return mjson_base64_dec(p + 1, sz - 2, to, n);
}
#endif // MJSON_ENABLE_BASE64
#if MJSON_ENABLE_NEXT
struct nextdata {
int off, len, depth, t, vo, arrayindex;
int *koff, *klen, *voff, *vlen, *vtype;
};
static int next_cb(int tok, const char *s, int off, int len, void *ud) {
struct nextdata *d = (struct nextdata *) ud;
// int i;
switch (tok) {
case '{':
case '[':
if (d->depth == 0 && tok == '[') d->arrayindex = 0;
if (d->depth == 1 && off > d->off) {
d->vo = off;
d->t = tok == '{' ? MJSON_TOK_OBJECT : MJSON_TOK_ARRAY;
if (d->voff) *d->voff = off;
if (d->vtype) *d->vtype = d->t;
}
d->depth++;
break;
case '}':
case ']':
d->depth--;
if (d->depth == 1 && d->vo) {
d->len = off + len;
if (d->vlen) *d->vlen = d->len - d->vo;
if (d->arrayindex >= 0) {
if (d->koff) *d->koff = d->arrayindex; // koff holds array index
if (d->klen) *d->klen = 0; // klen holds 0
}
return 1;
}
if (d->depth == 1 && d->arrayindex >= 0) d->arrayindex++;
break;
case ',':
case ':':
break;
case MJSON_TOK_KEY:
if (d->depth == 1 && d->off < off) {
if (d->koff) *d->koff = off; // And report back to the user
if (d->klen) *d->klen = len; // If we have to
}
break;
default:
if (d->depth != 1) break;
// If we're iterating over the array
if (off > d->off) {
d->len = off + len;
if (d->vlen) *d->vlen = len; // value length
if (d->voff) *d->voff = off; // value offset
if (d->vtype) *d->vtype = tok; // value type
if (d->arrayindex >= 0) {
if (d->koff) *d->koff = d->arrayindex; // koff holds array index
if (d->klen) *d->klen = 0; // klen holds 0
}
return 1;
}
if (d->arrayindex >= 0) d->arrayindex++;
break;
}
(void) s;
return 0;
}
int mjson_next(const char *s, int n, int off, int *koff, int *klen, int *voff,
int *vlen, int *vtype) {
struct nextdata d = {off, 0, 0, 0, 0, -1, koff, klen, voff, vlen, vtype};
mjson(s, n, next_cb, &d);
return d.len;
}
#endif
#if MJSON_ENABLE_PRINT
int mjson_print_fixed_buf(const char *ptr, int len, void *fndata) {
struct mjson_fixedbuf *fb = (struct mjson_fixedbuf *) fndata;
int i, left = fb->size - 1 - fb->len;
if (left < len) len = left;
for (i = 0; i < len; i++) fb->ptr[fb->len + i] = ptr[i];
fb->len += len;
fb->ptr[fb->len] = '\0';
return len;
}
// This function allocates memory in chunks of size MJSON_DYNBUF_CHUNK
// to decrease memory fragmentation, when many calls are executed to
// print e.g. a base64 string or a hex string.
int mjson_print_dynamic_buf(const char *ptr, int len, void *fndata) {
char *s, *buf = *(char **) fndata;
size_t curlen = buf == NULL ? 0 : strlen(buf);
size_t new_size = curlen + len + 1 + MJSON_DYNBUF_CHUNK;
new_size -= new_size % MJSON_DYNBUF_CHUNK;
if ((s = (char *) realloc(buf, new_size)) == NULL) {
return 0;
} else {
memcpy(s + curlen, ptr, len);
s[curlen + len] = '\0';
*(char **) fndata = s;
return len;
}
}
int mjson_print_null(const char *ptr, int len, void *userdata) {
(void) ptr;
(void) userdata;
return len;
}
int mjson_print_buf(mjson_print_fn_t fn, void *fnd, const char *buf, int len) {
return fn(buf, len, fnd);
}
int mjson_print_long(mjson_print_fn_t fn, void *fnd, long val, int is_signed) {
unsigned long v = val, s = 0, n, i;
char buf[20], t;
if (is_signed && val < 0) {
buf[s++] = '-', v = -val;
}
// This loop prints a number in reverse order. I guess this is because we
// write numbers from right to left: least significant digit comes last.
// Maybe because we use Arabic numbers, and Arabs write RTL?
for (n = 0; v > 0; v /= 10) buf[s + n++] = "0123456789"[v % 10];
// Reverse a string
for (i = 0; i < n / 2; i++)
t = buf[s + i], buf[s + i] = buf[s + n - i - 1], buf[s + n - i - 1] = t;
if (val == 0) buf[n++] = '0'; // Handle special case
return fn(buf, s + n, fnd);
}
int mjson_print_int(mjson_print_fn_t fn, void *fnd, int v, int s) {
return mjson_print_long(fn, fnd, s ? (long) v : (unsigned) v, s);
}
static int addexp(char *buf, int e, int sign) {
int n = 0;
buf[n++] = 'e';
buf[n++] = sign;
if (e > 400) return 0;
if (e < 10) buf[n++] = '0';
if (e >= 100) buf[n++] = (e / 100) + '0', e -= 100 * (e / 100);
if (e >= 10) buf[n++] = (e / 10) + '0', e -= 10 * (e / 10);
buf[n++] = e + '0';
return n;
}
int mjson_print_dbl(mjson_print_fn_t fn, void *fnd, double d, int width) {
char buf[40];
int i, s = 0, n = 0, e = 0;
double t, mul, saved;
if (d == 0.0) return fn("0", 1, fnd);
if (isinf(d)) return fn(d > 0 ? "inf" : "-inf", d > 0 ? 3 : 4, fnd);
if (isnan(d)) return fn("nan", 3, fnd);
if (d < 0.0) d = -d, buf[s++] = '-';
// Round
saved = d;
mul = 1.0;
while (d >= 10.0 && d / mul >= 10.0) mul *= 10.0;
while (d <= 1.0 && d / mul <= 1.0) mul /= 10.0;
for (i = 0, t = mul * 5; i < width; i++) t /= 10.0;
d += t;
// Calculate exponent, and 'mul' for scientific representation
mul = 1.0;
while (d >= 10.0 && d / mul >= 10.0) mul *= 10.0, e++;
while (d < 1.0 && d / mul < 1.0) mul /= 10.0, e--;
// printf(" --> %g %d %g %g\n", saved, e, t, mul);
if (e >= width) {
struct mjson_fixedbuf fb = {buf + s, (int) sizeof(buf) - s, 0};
n = mjson_print_dbl(mjson_print_fixed_buf, &fb, saved / mul, width);
// printf(" --> %.*g %d [%.*s]\n", 10, d / t, e, fb.len, fb.ptr);
n += addexp(buf + s + n, e, '+');
return fn(buf, s + n, fnd);
} else if (e <= -width) {
struct mjson_fixedbuf fb = {buf + s, (int) sizeof(buf) - s, 0};
n = mjson_print_dbl(mjson_print_fixed_buf, &fb, saved / mul, width);
// printf(" --> %.*g %d [%.*s]\n", 10, d / mul, e, fb.len, fb.ptr);
n += addexp(buf + s + n, -e, '-');
return fn(buf, s + n, fnd);
} else {
for (i = 0, t = mul; d >= 1.0 && s + n < (int) sizeof(buf); i++) {
int ch = (int) (d / t);
if (n > 0 || ch > 0) buf[s + n++] = ch + '0';
d -= ch * t;
t /= 10.0;
}
// printf(" --> [%g] -> %g %g (%d) [%.*s]\n", saved, d, t, n, s + n, buf);
if (n == 0) buf[s++] = '0';
while (t >= 1.0 && n + s < (int) sizeof(buf)) buf[n++] = '0', t /= 10.0;
if (s + n < (int) sizeof(buf)) buf[n + s++] = '.';
// printf(" 1--> [%g] -> [%.*s]\n", saved, s + n, buf);
for (i = 0, t = 0.1; s + n < (int) sizeof(buf) && n < width; i++) {
int ch = (int) (d / t);
buf[s + n++] = ch + '0';
d -= ch * t;
t /= 10.0;
}
}
while (n > 0 && buf[s + n - 1] == '0') n--; // Trim trailing zeros
if (n > 0 && buf[s + n - 1] == '.') n--; // Trim trailing dot
return fn(buf, s + n, fnd);
}
int mjson_print_str(mjson_print_fn_t fn, void *fnd, const char *s, int len) {
int i, n = fn("\"", 1, fnd);
for (i = 0; i < len; i++) {
char c = mjson_escape(s[i]);
if (c) {
n += fn("\\", 1, fnd);
n += fn(&c, 1, fnd);
} else {
n += fn(&s[i], 1, fnd);
}
}
return n + fn("\"", 1, fnd);
}
#if MJSON_ENABLE_BASE64
int mjson_print_b64(mjson_print_fn_t fn, void *fnd, const unsigned char *s,
int n) {
const char *t =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int i, len = fn("\"", 1, fnd);
for (i = 0; i < n; i += 3) {
int a = s[i], b = i + 1 < n ? s[i + 1] : 0, c = i + 2 < n ? s[i + 2] : 0;
char buf[4] = {t[a >> 2], t[(a & 3) << 4 | (b >> 4)], '=', '='};
if (i + 1 < n) buf[2] = t[(b & 15) << 2 | (c >> 6)];
if (i + 2 < n) buf[3] = t[c & 63];
len += fn(buf, sizeof(buf), fnd);
}
return len + fn("\"", 1, fnd);
}
#endif /* MJSON_ENABLE_BASE64 */
int mjson_vprintf(mjson_print_fn_t fn, void *fnd, const char *fmt,
va_list xap) {
int i = 0, n = 0;
va_list ap;
va_copy(ap, xap);
while (fmt[i] != '\0') {
if (fmt[i] == '%') {
char fc = fmt[++i];
int is_long = 0;
if (fc == 'l') {
is_long = 1;
fc = fmt[i + 1];
}
if (fc == 'Q') {
char *buf = va_arg(ap, char *);
n += mjson_print_str(fn, fnd, buf ? buf : "",
buf ? (int) strlen(buf) : 0);
} else if (strncmp(&fmt[i], ".*Q", 3) == 0) {
int len = va_arg(ap, int);
char *buf = va_arg(ap, char *);
n += mjson_print_str(fn, fnd, buf, len);
i += 2;
} else if (fc == 'd' || fc == 'u') {
int is_signed = (fc == 'd');
if (is_long) {
long val = va_arg(ap, long);
n += mjson_print_long(fn, fnd, val, is_signed);
i++;
} else {
int val = va_arg(ap, int);
n += mjson_print_int(fn, fnd, val, is_signed);
}
} else if (fc == 'B') {
const char *s = va_arg(ap, int) ? "true" : "false";
n += mjson_print_buf(fn, fnd, s, (int) strlen(s));
} else if (fc == 's') {
char *buf = va_arg(ap, char *);
n += mjson_print_buf(fn, fnd, buf, (int) strlen(buf));
} else if (strncmp(&fmt[i], ".*s", 3) == 0) {
int len = va_arg(ap, int);
char *buf = va_arg(ap, char *);
n += mjson_print_buf(fn, fnd, buf, len);
i += 2;
} else if (fc == 'g') {
n += mjson_print_dbl(fn, fnd, va_arg(ap, double), 6);
} else if (strncmp(&fmt[i], ".*g", 3) == 0) {
int width = va_arg(ap, int);
n += mjson_print_dbl(fn, fnd, va_arg(ap, double), width);
i += 2;
#if MJSON_ENABLE_BASE64
} else if (fc == 'V') {
int len = va_arg(ap, int);
const char *buf = va_arg(ap, const char *);
n += mjson_print_b64(fn, fnd, (unsigned char *) buf, len);
#endif
} else if (fc == 'H') {
const char *hex = "0123456789abcdef";
int i, len = va_arg(ap, int);
const unsigned char *p = va_arg(ap, const unsigned char *);
n += fn("\"", 1, fnd);
for (i = 0; i < len; i++) {
n += fn(&hex[(p[i] >> 4) & 15], 1, fnd);
n += fn(&hex[p[i] & 15], 1, fnd);
}
n += fn("\"", 1, fnd);
} else if (fc == 'M') {
mjson_vprint_fn_t vfn = va_arg(ap, mjson_vprint_fn_t);
n += vfn(fn, fnd, &ap);
}
i++;
} else {
n += mjson_print_buf(fn, fnd, &fmt[i++], 1);
}
}
va_end(xap);
va_end(ap);
return n;
}
int mjson_printf(mjson_print_fn_t fn, void *fnd, const char *fmt, ...) {
va_list ap;
int len;
va_start(ap, fmt);
len = mjson_vprintf(fn, fnd, fmt, ap);
va_end(ap);
return len;
}
#endif /* MJSON_ENABLE_PRINT */
static int is_digit(int c) {
return c >= '0' && c <= '9';
}
/* NOTE: strtod() implementation by Yasuhiro Matsumoto. */
static double mystrtod(const char *str, char **end) {
double d = 0.0;
int sign = 1, __attribute__((unused)) n = 0;
const char *p = str, *a = str;
/* decimal part */
if (*p == '-') {
sign = -1;
++p;
} else if (*p == '+') {
++p;
}
if (is_digit(*p)) {
d = (double) (*p++ - '0');
while (*p && is_digit(*p)) {
d = d * 10.0 + (double) (*p - '0');
++p;
++n;
}
a = p;
} else if (*p != '.') {
goto done;
}
d *= sign;
/* fraction part */
if (*p == '.') {
double f = 0.0;
double base = 0.1;
++p;
if (is_digit(*p)) {
while (*p && is_digit(*p)) {
f += base * (*p - '0');
base /= 10.0;
++p;
++n;
}
}
d += f * sign;
a = p;
}
/* exponential part */
if ((*p == 'E') || (*p == 'e')) {
double exp, f;
int i, e = 0, neg = 0;
p++;
if (*p == '-') p++, neg++;
if (*p == '+') p++;
while (is_digit(*p)) e = e * 10 + *p++ - '0';
i = e;
if (neg) e = -e;
#if 0
if (d == 2.2250738585072011 && e == -308) {
d = 0.0;
a = p;
goto done;
}
if (d == 2.2250738585072012 && e <= -308) {
d *= 1.0e-308;
a = p;
goto done;
}
#endif
/* calculate f = 10^i */
exp = 10;
f = 1;
while (i > 0) {
if (i & 1) f *= exp;
exp *= exp;
i >>= 1;
}
if (e > 0) d *= f;
else if (e < 0) d /= f;
a = p;
} else if (p > str && !is_digit(*(p - 1))) {
a = str;
goto done;
}
done:
if (end) *end = (char *) a;
return d;
}
#if MJSON_ENABLE_MERGE
int mjson_merge(const char *s, int n, const char *s2, int n2,
mjson_print_fn_t fn, void *userdata) {
int koff, klen, voff, vlen, t, t2, k, off = 0, len = 0, comma = 0;
if (n < 2) return len;
len += fn("{", 1, userdata);
while ((off = mjson_next(s, n, off, &koff, &klen, &voff, &vlen, &t)) != 0) {
char *path = (char *) alloca(klen + 1);
const char *val;
memcpy(path, "$.", 2);
memcpy(path + 2, s + koff + 1, klen - 2);
path[klen] = '\0';
if ((t2 = mjson_find(s2, n2, path, &val, &k)) != MJSON_TOK_INVALID) {
if (t2 == MJSON_TOK_NULL) continue; // null deletes the key
} else {
val = s + voff; // Key is not found in the update. Copy the old value.
}
if (comma) len += fn(",", 1, userdata);
len += fn(s + koff, klen, userdata);
len += fn(":", 1, userdata);
if (t == MJSON_TOK_OBJECT && t2 == MJSON_TOK_OBJECT) {
len += mjson_merge(s + voff, vlen, val, k, fn, userdata);
} else {
if (t2 != MJSON_TOK_INVALID) vlen = k;
len += fn(val, vlen, userdata);
}
comma = 1;
}
// Add missing keys
off = 0;
while ((off = mjson_next(s2, n2, off, &koff, &klen, &voff, &vlen, &t)) != 0) {
char *path = (char *) alloca(klen + 1);
const char *val;
if (t == MJSON_TOK_NULL) continue;
memcpy(path, "$.", 2);
memcpy(path + 2, s2 + koff + 1, klen - 2);
path[klen] = '\0';
if (mjson_find(s, n, path, &val, &vlen) != MJSON_TOK_INVALID) continue;
if (comma) len += fn(",", 1, userdata);
len += fn(s2 + koff, klen, userdata);
len += fn(":", 1, userdata);
len += fn(s2 + voff, vlen, userdata);
comma = 1;
}
len += fn("}", 1, userdata);
return len;
}
#endif // MJSON_ENABLE_MERGE
#if MJSON_ENABLE_PRETTY
struct prettydata {
int level;
int len;
int prev;
const char *pad;
int padlen;
mjson_print_fn_t fn;
void *userdata;
};
static int pretty_cb(int ev, const char *s, int off, int len, void *ud) {
struct prettydata *d = (struct prettydata *) ud;
int i;
switch (ev) {
case '{':
case '[':
d->level++;
d->len += d->fn(s + off, len, d->userdata);
break;
case '}':
case ']':
d->level--;
if (d->prev != '[' && d->prev != '{' && d->padlen > 0) {
d->len += d->fn("\n", 1, d->userdata);
for (i = 0; i < d->level; i++)
d->len += d->fn(d->pad, d->padlen, d->userdata);
}
d->len += d->fn(s + off, len, d->userdata);
break;
case ',':
d->len += d->fn(s + off, len, d->userdata);
if (d->padlen > 0) {
d->len += d->fn("\n", 1, d->userdata);
for (i = 0; i < d->level; i++)
d->len += d->fn(d->pad, d->padlen, d->userdata);
}
break;
case ':':
d->len += d->fn(s + off, len, d->userdata);
if (d->padlen > 0) d->len += d->fn(" ", 1, d->userdata);
break;
case MJSON_TOK_KEY:
if (d->prev == '{' && d->padlen > 0) {
d->len += d->fn("\n", 1, d->userdata);
for (i = 0; i < d->level; i++)
d->len += d->fn(d->pad, d->padlen, d->userdata);
}
d->len += d->fn(s + off, len, d->userdata);
break;
default:
if (d->prev == '[' && d->padlen > 0) {
d->len += d->fn("\n", 1, d->userdata);
for (i = 0; i < d->level; i++)
d->len += d->fn(d->pad, d->padlen, d->userdata);
}
d->len += d->fn(s + off, len, d->userdata);
break;
}
d->prev = ev;
return 0;
}
int mjson_pretty(const char *s, int n, const char *pad, mjson_print_fn_t fn,
void *userdata) {
struct prettydata d = {0, 0, 0, pad, (int) strlen(pad), fn, userdata};
if (mjson(s, n, pretty_cb, &d) < 0) return -1;
return d.len;
}
#endif // MJSON_ENABLE_PRETTY
#if MJSON_ENABLE_RPC
struct jsonrpc_ctx jsonrpc_default_context;
int mjson_globmatch(const char *s1, int n1, const char *s2, int n2) {
int i = 0, j = 0, ni = 0, nj = 0;
while (i < n1 || j < n2) {
if (i < n1 && j < n2 && (s1[i] == '?' || s2[j] == s1[i])) {
i++, j++;
} else if (i < n1 && (s1[i] == '*' || s1[i] == '#')) {
ni = i, nj = j + 1, i++;
} else if (nj > 0 && nj <= n2 && (s1[i - 1] == '#' || s2[j] != '/')) {
i = ni, j = nj;
} else {
return 0;
}
}
return 1;
}
void jsonrpc_return_errorv(struct jsonrpc_request *r, int code,
const char *message, const char *data_fmt,
va_list ap) {
if (r->id_len == 0) return;
mjson_printf(r->fn, r->fndata,
"{\"id\":%.*s,\"error\":{\"code\":%d,\"message\":%Q", r->id_len,
r->id, code, message == NULL ? "" : message);
if (data_fmt != NULL) {
mjson_printf(r->fn, r->fndata, ",\"data\":");
mjson_vprintf(r->fn, r->fndata, data_fmt, ap);
}
mjson_printf(r->fn, r->fndata, "}}\n");
}
void jsonrpc_return_error(struct jsonrpc_request *r, int code,
const char *message, const char *data_fmt, ...) {
va_list ap;
va_start(ap, data_fmt);
jsonrpc_return_errorv(r, code, message, data_fmt, ap);
va_end(ap);
}
void jsonrpc_return_successv(struct jsonrpc_request *r, const char *result_fmt,
va_list ap) {
if (r->id_len == 0) return;
mjson_printf(r->fn, r->fndata, "{\"id\":%.*s,\"result\":", r->id_len, r->id);
if (result_fmt != NULL) {
mjson_vprintf(r->fn, r->fndata, result_fmt, ap);
} else {
mjson_printf(r->fn, r->fndata, "%s", "null");
}
mjson_printf(r->fn, r->fndata, "}\n");
}
void jsonrpc_return_success(struct jsonrpc_request *r, const char *result_fmt,
...) {
va_list ap;
va_start(ap, result_fmt);
jsonrpc_return_successv(r, result_fmt, ap);
va_end(ap);
}
void jsonrpc_ctx_process(struct jsonrpc_ctx *ctx, const char *buf, int len,
mjson_print_fn_t fn, void *fndata, void *ud) {
const char *result = NULL, *error = NULL;
int result_sz = 0, error_sz = 0;
struct jsonrpc_method *m = NULL;
struct jsonrpc_request r = {ctx, buf, len, 0, 0, 0, 0, 0, 0, fn, fndata, ud};
// Is is a response frame?
mjson_find(buf, len, "$.result", &result, &result_sz);
if (result == NULL) mjson_find(buf, len, "$.error", &error, &error_sz);
if (result_sz > 0 || error_sz > 0) {
if (ctx->response_cb) ctx->response_cb(buf, len, ctx->response_cb_data);
return;
}
// Method must exist and must be a string
if (mjson_find(buf, len, "$.method", &r.method, &r.method_len) !=
MJSON_TOK_STRING) {
mjson_printf(fn, fndata, "{\"error\":{\"code\":-32700,\"message\":%.*Q}}\n",
len, buf);
return;
}
// id and params are optional
mjson_find(buf, len, "$.id", &r.id, &r.id_len);
mjson_find(buf, len, "$.params", &r.params, &r.params_len);
for (m = ctx->methods; m != NULL; m = m->next) {
if (mjson_globmatch(m->method, m->method_sz, r.method + 1,
r.method_len - 2) > 0) {
if (r.params == NULL) r.params = "";
m->cb(&r);
break;
}
}
if (m == NULL) {
jsonrpc_return_error(&r, JSONRPC_ERROR_NOT_FOUND, "method not found", NULL);
}
}
static int jsonrpc_print_methods(mjson_print_fn_t fn, void *fndata,
va_list *ap) {
struct jsonrpc_ctx *ctx = va_arg(*ap, struct jsonrpc_ctx *);
struct jsonrpc_method *m;
int len = 0;
for (m = ctx->methods; m != NULL; m = m->next) {
if (m != ctx->methods) len += mjson_print_buf(fn, fndata, ",", 1);
len += mjson_print_str(fn, fndata, m->method, (int) strlen(m->method));
}
return len;
}
static void rpclist(struct jsonrpc_request *r) {
jsonrpc_return_success(r, "[%M]", jsonrpc_print_methods, r->ctx);
}
void jsonrpc_ctx_init(struct jsonrpc_ctx *ctx, mjson_print_fn_t response_cb,
void *response_cb_data) {
ctx->response_cb = response_cb;
ctx->response_cb_data = response_cb_data;
jsonrpc_ctx_export(ctx, MJSON_RPC_LIST_NAME, rpclist);
}
void jsonrpc_init(mjson_print_fn_t response_cb, void *userdata) {
jsonrpc_ctx_init(&jsonrpc_default_context, response_cb, userdata);
}
#endif // MJSON_ENABLE_RPC
|