json.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  1. /****************************************************************************
  2. NAME
  3. json.c - parse JSON into fixed-extent data structures
  4. DESCRIPTION
  5. This module parses a large subset of JSON (JavaScript Object
  6. Notation). Unlike more general JSON parsers, it doesn't use malloc(3)
  7. and doesn't support polymorphism; you need to give it a set of
  8. template structures describing the expected shape of the incoming
  9. JSON, and it will error out if that shape is not matched. When the
  10. parse succeeds, attribute values will be extracted into static
  11. locations specified in the template structures.
  12. The "shape" of a JSON object in the type signature of its
  13. attributes (and attribute values, and so on recursively down through
  14. all nestings of objects and arrays). This parser is indifferent to
  15. the order of attributes at any level, but you have to tell it in
  16. advance what the type of each attribute value will be and where the
  17. parsed value will be stored. The template structures may supply
  18. default values to be used when an expected attribute is omitted.
  19. The preceding paragraph told one fib. A single attribute may
  20. actually have a span of multiple specifications with different
  21. syntactically distinguishable types (e.g. string vs. real vs. integer
  22. vs. boolean, but not signed integer vs. unsigned integer). The parser
  23. will match the right spec against the actual data.
  24. The dialect this parses has some limitations. First, it cannot
  25. recognize the JSON "null" value. Secondly, arrays may not have
  26. character values as elements (this limitation could be easily removed
  27. if required). Third, all elements of an array must be of the same
  28. type. Fourth, it can not handle NaN's in doubles (Issue 53150).
  29. There are separate entry points for beginning a parse of either
  30. JSON object or a JSON array. JSON "float" quantities are actually
  31. stored as doubles.
  32. This parser processes object arrays in one of two different ways,
  33. defending on whether the array subtype is declared as object or
  34. structobject.
  35. Object arrays take one base address per object subfield, and are
  36. mapped into parallel C arrays (one per subfield). Strings are not
  37. supported in this kind of array, as they don't have a "natural" size
  38. to use as an offset multiplier.
  39. Structobjects arrays are a way to parse a list of objects to a set
  40. of modifications to a corresponding array of C structs. The trick is
  41. that the array object initialization has to specify both the C struct
  42. array's base address and the stride length (the size of the C struct).
  43. If you initialize the offset fields with the correct offsetof calls,
  44. everything will work. Strings are supported but all string storage
  45. has to be inline in the struct.
  46. NOTE
  47. This code has been spun out, packaged, and documented as a
  48. reusable module; search for "microjson".
  49. PERMISSIONS
  50. This file is Copyright 2010 by the GPSD project
  51. SPDX-License-Identifier: BSD-2-clause
  52. ***************************************************************************/
  53. #include "../include/gpsd_config.h" /* must be before all includes */
  54. #include <ctype.h>
  55. #include <math.h> /* for HUGE_VAL */
  56. #include <stdarg.h>
  57. #include <stdbool.h>
  58. #include <stdio.h>
  59. #include <stdlib.h>
  60. #include <string.h>
  61. #include "../include/compiler.h" // for FALLTHROUGH
  62. #include "../include/os_compat.h"
  63. #include "../include/json.h"
  64. #include "../include/gps.h" /* for safe_atof() prototype */
  65. #include "../include/strfuncs.h"
  66. #include "../include/timespec.h"
  67. static int debuglevel = 0;
  68. static FILE *debugfp;
  69. void json_enable_debug(int level, FILE * fp)
  70. /* control the level and destination of debug trace messages */
  71. {
  72. debuglevel = level;
  73. debugfp = fp;
  74. }
  75. static void json_trace(int errlevel, const char *fmt, ...)
  76. /* assemble command in printf(3) style */
  77. {
  78. if (errlevel <= debuglevel && NULL != debugfp) {
  79. char buf[BUFSIZ] = {0};
  80. va_list ap;
  81. (void)strlcpy(buf, "json: ", sizeof(buf));
  82. va_start(ap, fmt);
  83. str_vappendf(buf, sizeof(buf), fmt, ap);
  84. va_end(ap);
  85. (void)fputs(buf, debugfp);
  86. }
  87. }
  88. #define json_debug_trace(args) (void) json_trace args
  89. static char *json_target_address(const struct json_attr_t *cursor,
  90. const struct json_array_t
  91. *parent, int offset)
  92. {
  93. char *targetaddr = NULL;
  94. if (parent == NULL || parent->element_type != t_structobject) {
  95. /* ordinary case - use the address in the cursor structure */
  96. switch (cursor->type) {
  97. case t_byte:
  98. targetaddr = (char *)&cursor->addr.byte[offset];
  99. break;
  100. case t_ubyte:
  101. targetaddr = (char *)&cursor->addr.ubyte[offset];
  102. break;
  103. case t_ignore:
  104. targetaddr = NULL;
  105. break;
  106. case t_integer:
  107. targetaddr = (char *)&cursor->addr.integer[offset];
  108. break;
  109. case t_uinteger:
  110. targetaddr = (char *)&cursor->addr.uinteger[offset];
  111. break;
  112. case t_longint:
  113. targetaddr = (char *)&cursor->addr.longint[offset];
  114. break;
  115. case t_ulongint:
  116. targetaddr = (char *)&cursor->addr.ulongint[offset];
  117. break;
  118. case t_short:
  119. targetaddr = (char *)&cursor->addr.shortint[offset];
  120. break;
  121. case t_ushort:
  122. targetaddr = (char *)&cursor->addr.ushortint[offset];
  123. break;
  124. case t_time:
  125. targetaddr = (char *)&cursor->addr.ts[offset];
  126. break;
  127. case t_timespec:
  128. targetaddr = (char *)&cursor->addr.ts[offset];
  129. break;
  130. case t_real:
  131. targetaddr = (char *)&cursor->addr.real[offset];
  132. break;
  133. case t_string:
  134. targetaddr = cursor->addr.string;
  135. break;
  136. case t_boolean:
  137. targetaddr = (char *)&cursor->addr.boolean[offset];
  138. break;
  139. case t_character:
  140. targetaddr = (char *)&cursor->addr.character[offset];
  141. break;
  142. default:
  143. targetaddr = NULL;
  144. break;
  145. }
  146. } else
  147. /* tricky case - hacking a member in an array of structures */
  148. targetaddr =
  149. parent->arr.objects.base + (offset * parent->arr.objects.stride) +
  150. cursor->addr.offset;
  151. json_debug_trace((1, "Target address for %s (offset %d) is %p\n",
  152. cursor->attribute, offset, targetaddr));
  153. return targetaddr;
  154. }
  155. static int json_internal_read_object(const char *cp,
  156. const struct json_attr_t *attrs,
  157. const struct json_array_t *parent,
  158. int offset,
  159. const char **end)
  160. {
  161. enum
  162. { init, await_attr, in_attr, await_value, in_val_string,
  163. in_escape, in_val_token, post_val, post_element
  164. } state = 0;
  165. char *statenames[] = {
  166. "init", "await_attr", "in_attr", "await_value", "in_val_string",
  167. "in_escape", "in_val_token", "post_val", "post_element",
  168. };
  169. char attrbuf[JSON_ATTR_MAX + 1], *pattr = NULL;
  170. char valbuf[JSON_VAL_MAX + 1], *pval = NULL;
  171. bool value_quoted = false;
  172. char uescape[5]; /* enough space for 4 hex digits and a NUL */
  173. const struct json_attr_t *cursor;
  174. int substatus, maxlen = 0;
  175. unsigned int u;
  176. const struct json_enum_t *mp;
  177. char *lptr;
  178. if (end != NULL)
  179. *end = NULL; /* give it a well-defined value on parse failure */
  180. /* stuff fields with defaults in case they're omitted in the JSON input */
  181. for (cursor = attrs; cursor->attribute != NULL; cursor++)
  182. if (!cursor->nodefault) {
  183. lptr = json_target_address(cursor, parent, offset);
  184. if (lptr != NULL)
  185. switch (cursor->type) {
  186. case t_byte:
  187. lptr[0] = cursor->dflt.byte;
  188. break;
  189. case t_ubyte:
  190. lptr[0] = cursor->dflt.ubyte;
  191. break;
  192. case t_integer:
  193. memcpy(lptr, &cursor->dflt.integer, sizeof(int));
  194. break;
  195. case t_uinteger:
  196. memcpy(lptr, &cursor->dflt.uinteger, sizeof(unsigned int));
  197. break;
  198. case t_longint:
  199. memcpy(lptr, &cursor->dflt.longint, sizeof(long));
  200. break;
  201. case t_ulongint:
  202. memcpy(lptr, &cursor->dflt.ulongint,
  203. sizeof(unsigned long));
  204. break;
  205. case t_short:
  206. memcpy(lptr, &cursor->dflt.shortint, sizeof(short));
  207. break;
  208. case t_ushort:
  209. memcpy(lptr, &cursor->dflt.ushortint,
  210. sizeof(unsigned short));
  211. break;
  212. case t_time:
  213. memcpy(lptr, &cursor->dflt.ts, sizeof(timespec_t));
  214. break;
  215. case t_timespec:
  216. memcpy(lptr, &cursor->dflt.ts, sizeof(timespec_t));
  217. break;
  218. case t_real:
  219. memcpy(lptr, &cursor->dflt.real, sizeof(double));
  220. break;
  221. case t_string:
  222. if (parent != NULL
  223. && parent->element_type != t_structobject
  224. && offset > 0)
  225. return JSON_ERR_NOPARSTR;
  226. lptr[0] = '\0';
  227. break;
  228. case t_boolean:
  229. memcpy(lptr, &cursor->dflt.boolean, sizeof(bool));
  230. break;
  231. case t_character:
  232. lptr[0] = cursor->dflt.character;
  233. break;
  234. case t_object: /* silences a compiler warning */
  235. case t_structobject:
  236. case t_array:
  237. case t_check:
  238. case t_ignore:
  239. break;
  240. }
  241. }
  242. json_debug_trace((1, "JSON parse of '%s' begins.\n", cp));
  243. /* parse input JSON */
  244. for (; *cp != '\0'; cp++) {
  245. json_debug_trace((2, "State %-14s, looking at '%c' (%p)\n",
  246. statenames[state], *cp, cp));
  247. switch (state) {
  248. case init:
  249. if (isspace((unsigned char) *cp))
  250. continue;
  251. else if (*cp == '{')
  252. state = await_attr;
  253. else {
  254. json_debug_trace((1,
  255. "Non-WS when expecting object start.\n"));
  256. if (end != NULL)
  257. *end = cp;
  258. return JSON_ERR_OBSTART;
  259. }
  260. break;
  261. case await_attr:
  262. if (isspace((unsigned char) *cp))
  263. continue;
  264. else if (*cp == '"') {
  265. state = in_attr;
  266. pattr = attrbuf;
  267. if (end != NULL)
  268. *end = cp;
  269. } else if (*cp == '}')
  270. break;
  271. else {
  272. json_debug_trace((1, "Non-WS when expecting attribute.\n"));
  273. if (end != NULL)
  274. *end = cp;
  275. return JSON_ERR_ATTRSTART;
  276. }
  277. break;
  278. case in_attr:
  279. if (pattr == NULL)
  280. /* don't update end here, leave at attribute start */
  281. return JSON_ERR_NULLPTR;
  282. if (*cp == '"') {
  283. *pattr++ = '\0';
  284. json_debug_trace((1, "Collected attribute name %s\n",
  285. attrbuf));
  286. for (cursor = attrs; cursor->attribute != NULL; cursor++) {
  287. json_debug_trace((2, "Checking against %s\n",
  288. cursor->attribute));
  289. if (strcmp(cursor->attribute, attrbuf) == 0)
  290. break;
  291. if (cursor->type == t_ignore &&
  292. strncmp(cursor->attribute, "", 1) == 0) {
  293. break;
  294. }
  295. }
  296. if (cursor->attribute == NULL) {
  297. json_debug_trace((1,
  298. "Unknown attribute name '%s'"
  299. " (attributes begin with '%s').\n",
  300. attrbuf, attrs->attribute));
  301. /* don't update end here, leave at attribute start */
  302. return JSON_ERR_BADATTR;
  303. }
  304. state = await_value;
  305. if (cursor->type == t_string)
  306. maxlen = (int)cursor->len - 1;
  307. else if (cursor->type == t_check)
  308. maxlen = (int)strlen(cursor->dflt.check);
  309. else if (cursor->type == t_time || cursor->type == t_ignore)
  310. maxlen = JSON_VAL_MAX;
  311. else if (cursor->map != NULL)
  312. maxlen = (int)sizeof(valbuf) - 1;
  313. pval = valbuf;
  314. } else if (pattr >= attrbuf + JSON_ATTR_MAX - 1) {
  315. json_debug_trace((1, "Attribute name too long.\n"));
  316. /* don't update end here, leave at attribute start */
  317. return JSON_ERR_ATTRLEN;
  318. } else
  319. *pattr++ = *cp;
  320. break;
  321. case await_value:
  322. if (isspace((unsigned char) *cp) || *cp == ':')
  323. continue;
  324. else if (*cp == '[') {
  325. if (cursor->type != t_array) {
  326. json_debug_trace((1,
  327. "Saw [ when not expecting array.\n"));
  328. if (end != NULL)
  329. *end = cp;
  330. return JSON_ERR_NOARRAY;
  331. }
  332. substatus = json_read_array(cp, &cursor->addr.array, &cp);
  333. if (substatus != 0)
  334. return substatus;
  335. state = post_element;
  336. } else if (cursor->type == t_array) {
  337. json_debug_trace((1,
  338. "Array element was specified, but no [.\n"));
  339. if (end != NULL)
  340. *end = cp;
  341. return JSON_ERR_NOBRAK;
  342. } else if (*cp == '"') {
  343. value_quoted = true;
  344. state = in_val_string;
  345. pval = valbuf;
  346. } else {
  347. value_quoted = false;
  348. state = in_val_token;
  349. pval = valbuf;
  350. *pval++ = *cp;
  351. }
  352. break;
  353. case in_val_string:
  354. if (pval == NULL)
  355. /* don't update end here, leave at value start */
  356. return JSON_ERR_NULLPTR;
  357. if (*cp == '\\')
  358. state = in_escape;
  359. else if (*cp == '"') {
  360. *pval++ = '\0';
  361. json_debug_trace((1, "Collected string value %s\n", valbuf));
  362. state = post_val;
  363. } else if (pval > valbuf + JSON_VAL_MAX - 1
  364. || pval > valbuf + maxlen - 1) {
  365. json_debug_trace((1, "String value too long.\n"));
  366. /* don't update end here, leave at value start */
  367. return JSON_ERR_STRLONG; /* */
  368. } else
  369. *pval++ = *cp;
  370. break;
  371. case in_escape:
  372. if (pval == NULL)
  373. /* don't update end here, leave at value start */
  374. return JSON_ERR_NULLPTR;
  375. else if (pval > valbuf + JSON_VAL_MAX - 1
  376. || pval > valbuf + maxlen) {
  377. json_debug_trace((1, "String value too long.\n"));
  378. /* don't update end here, leave at value start */
  379. return JSON_ERR_STRLONG; /* */
  380. }
  381. switch (*cp) {
  382. case 'b':
  383. *pval++ = '\b';
  384. break;
  385. case 'f':
  386. *pval++ = '\f';
  387. break;
  388. case 'n':
  389. *pval++ = '\n';
  390. break;
  391. case 'r':
  392. *pval++ = '\r';
  393. break;
  394. case 't':
  395. *pval++ = '\t';
  396. break;
  397. case 'u':
  398. {
  399. unsigned n;
  400. cp++; /* skip the 'u' */
  401. /* NetBSD 6 wants the cast */
  402. for (n = 0; n < 4 && isxdigit((int)*cp); n++)
  403. uescape[n] = *cp++;
  404. uescape[n] = '\0'; /* terminate */
  405. --cp;
  406. /* ECMA-404 says JSON \u must have 4 hex digits */
  407. if ((4 != n) || (1 != sscanf(uescape, "%4x", &u))) {
  408. return JSON_ERR_BADSTRING;
  409. }
  410. /* truncate values above 0xff */
  411. *pval++ = (unsigned char)u;
  412. }
  413. break;
  414. default: /* handles double quote and solidus */
  415. *pval++ = *cp;
  416. break;
  417. }
  418. state = in_val_string;
  419. break;
  420. case in_val_token:
  421. if (pval == NULL)
  422. /* don't update end here, leave at value start */
  423. return JSON_ERR_NULLPTR;
  424. if (isspace((unsigned char) *cp) || *cp == ',' || *cp == '}') {
  425. *pval = '\0';
  426. json_debug_trace((1, "Collected token value %s.\n", valbuf));
  427. state = post_val;
  428. if (*cp == '}' || *cp == ',')
  429. --cp;
  430. } else if (pval > valbuf + JSON_VAL_MAX - 1) {
  431. json_debug_trace((1, "Token value too long.\n"));
  432. /* don't update end here, leave at value start */
  433. return JSON_ERR_TOKLONG;
  434. } else
  435. *pval++ = *cp;
  436. break;
  437. /* coverity[unterminated_case] */
  438. case post_val:
  439. // Ignore whitespace after either string or token values.
  440. if (isspace(*cp)) {
  441. while (*cp != '\0' && isspace((unsigned char) *cp)) {
  442. ++cp;
  443. }
  444. json_debug_trace((1, "Skipped trailing whitespace: value \"%s\"\n", valbuf));
  445. }
  446. /*
  447. * We know that cursor points at the first spec matching
  448. * the current attribute. We don't know that it's *the*
  449. * correct spec; our dialect allows there to be any number
  450. * of adjacent ones with the same attrname but different
  451. * types. Here's where we try to seek forward for a
  452. * matching type/attr pair if we're not looking at one.
  453. */
  454. for (;;) {
  455. int seeking = cursor->type;
  456. if (value_quoted && (cursor->type == t_string
  457. || cursor->type == t_time))
  458. break;
  459. if ((strcmp(valbuf, "true")==0 || strcmp(valbuf, "false")==0)
  460. && seeking == t_boolean)
  461. break;
  462. if (isdigit((unsigned char) valbuf[0])) {
  463. bool decimal = strchr(valbuf, '.') != NULL;
  464. if (decimal && seeking == t_real)
  465. break;
  466. if (!decimal && (seeking == t_byte ||
  467. seeking == t_ubyte ||
  468. seeking == t_integer ||
  469. seeking == t_uinteger ||
  470. seeking == t_longint ||
  471. seeking == t_ulongint ||
  472. seeking == t_short ||
  473. seeking == t_ushort))
  474. break;
  475. }
  476. if (NULL == cursor[1].attribute) /* out of possibilities */
  477. break;
  478. if (0 != strcmp(cursor[1].attribute, attrbuf))
  479. break;
  480. ++cursor;
  481. }
  482. if (value_quoted &&
  483. (cursor->type != t_string &&
  484. cursor->type != t_character &&
  485. cursor->type != t_check &&
  486. cursor->type != t_time &&
  487. cursor->type != t_ignore &&
  488. cursor->map == 0)) {
  489. json_debug_trace((1, "Saw quoted value when expecting"
  490. " non-string.\n"));
  491. return JSON_ERR_QNONSTRING;
  492. }
  493. if (!value_quoted
  494. && (cursor->type == t_string || cursor->type == t_check
  495. || cursor->type == t_time || cursor->map != 0)) {
  496. json_debug_trace((1, "Didn't see quoted value when expecting"
  497. " string.\n"));
  498. return JSON_ERR_NONQSTRING;
  499. }
  500. if (cursor->map != 0) {
  501. for (mp = cursor->map; mp->name != NULL; mp++)
  502. if (strcmp(mp->name, valbuf) == 0) {
  503. goto foundit;
  504. }
  505. json_debug_trace((1, "Invalid enumerated value string %s.\n",
  506. valbuf));
  507. return JSON_ERR_BADENUM;
  508. foundit:
  509. (void)snprintf(valbuf, sizeof(valbuf), "%d", mp->value);
  510. }
  511. if (cursor->type == t_check) {
  512. lptr = cursor->dflt.check;
  513. } else {
  514. lptr = json_target_address(cursor, parent, offset);
  515. }
  516. if (lptr != NULL) {
  517. switch (cursor->type) {
  518. case t_byte:
  519. {
  520. int tmp = atoi(valbuf);
  521. lptr[0] = (char)tmp;
  522. }
  523. break;
  524. case t_ubyte:
  525. {
  526. int tmp = atoi(valbuf);
  527. lptr[0] = (unsigned char)tmp;
  528. }
  529. break;
  530. case t_integer:
  531. {
  532. int tmp = atoi(valbuf);
  533. memcpy(lptr, &tmp, sizeof(int));
  534. }
  535. break;
  536. case t_uinteger:
  537. {
  538. unsigned int tmp = (unsigned int)atol(valbuf);
  539. memcpy(lptr, &tmp, sizeof(unsigned int));
  540. }
  541. break;
  542. case t_longint:
  543. {
  544. long tmp = atol(valbuf);
  545. memcpy(lptr, &tmp, sizeof(long));
  546. }
  547. break;
  548. case t_ulongint:
  549. {
  550. unsigned long tmp = (unsigned long)atoll(valbuf);
  551. memcpy(lptr, &tmp, sizeof(unsigned long));
  552. }
  553. break;
  554. case t_short:
  555. {
  556. short tmp = atoi(valbuf);
  557. memcpy(lptr, &tmp, sizeof(short));
  558. }
  559. break;
  560. case t_ushort:
  561. {
  562. unsigned short tmp = (unsigned int)atoi(valbuf);
  563. memcpy(lptr, &tmp, sizeof(unsigned short));
  564. }
  565. break;
  566. case t_time:
  567. {
  568. timespec_t ts_tmp = iso8601_to_timespec(valbuf);
  569. memcpy(lptr, &ts_tmp, sizeof(timespec_t));
  570. }
  571. break;
  572. case t_timespec:
  573. {
  574. double sec_tmp = safe_atof(valbuf);
  575. timespec_t ts_tmp;
  576. if (0 != isfinite(sec_tmp)) {
  577. DTOTS(&ts_tmp, sec_tmp);
  578. memcpy(lptr, &ts_tmp, sizeof(timespec_t));
  579. } // else leave at .dflt
  580. }
  581. break;
  582. case t_real:
  583. {
  584. double tmp = safe_atof(valbuf);
  585. if (0 != isfinite(tmp)) {
  586. memcpy(lptr, &tmp, sizeof(double));
  587. } // else leave at .dflt
  588. }
  589. break;
  590. case t_string:
  591. if (parent != NULL
  592. && parent->element_type != t_structobject
  593. && offset > 0)
  594. return JSON_ERR_NOPARSTR;
  595. (void)strlcpy(lptr, valbuf, cursor->len);
  596. break;
  597. case t_boolean:
  598. {
  599. bool tmp = (strcmp(valbuf, "true") == 0);
  600. memcpy(lptr, &tmp, sizeof(bool));
  601. }
  602. break;
  603. case t_character:
  604. if (strlen(valbuf) > 1)
  605. /* don't update end here, leave at value start */
  606. return JSON_ERR_STRLONG;
  607. else
  608. lptr[0] = valbuf[0];
  609. break;
  610. case t_ignore: /* silences a compiler warning */
  611. case t_object: /* silences a compiler warning */
  612. case t_structobject:
  613. case t_array:
  614. break;
  615. case t_check:
  616. if (strcmp(cursor->dflt.check, valbuf) != 0) {
  617. json_debug_trace((1, "Required attribute value %s"
  618. " not present.\n",
  619. cursor->dflt.check));
  620. /* don't update end here, leave at start of attribute */
  621. return JSON_ERR_CHECKFAIL;
  622. }
  623. break;
  624. }
  625. }
  626. FALLTHROUGH
  627. case post_element:
  628. if (isspace((unsigned char) *cp))
  629. continue;
  630. else if (*cp == ',')
  631. state = await_attr;
  632. else if (*cp == '}') {
  633. ++cp;
  634. goto good_parse;
  635. } else {
  636. json_debug_trace((1, "Garbage while expecting comma or }\n"));
  637. if (end != NULL)
  638. *end = cp;
  639. return JSON_ERR_BADTRAIL;
  640. }
  641. break;
  642. }
  643. }
  644. if (state == init) {
  645. json_debug_trace((1, "Input was empty or white-space only\n"));
  646. return JSON_ERR_EMPTY;
  647. }
  648. good_parse:
  649. /* in case there's another object following, consume trailing WS */
  650. while (isspace((unsigned char) *cp))
  651. ++cp;
  652. if (end != NULL)
  653. *end = cp;
  654. json_debug_trace((1, "JSON parse ends.\n"));
  655. return 0;
  656. }
  657. int json_read_array(const char *cp, const struct json_array_t *arr,
  658. const char **end)
  659. {
  660. int substatus, offset, arrcount;
  661. char *tp;
  662. if (end != NULL)
  663. *end = NULL; /* give it a well-defined value on parse failure */
  664. json_debug_trace((1, "Entered json_read_array()\n"));
  665. while (isspace((unsigned char) *cp))
  666. cp++;
  667. if (*cp != '[') {
  668. json_debug_trace((1, "Didn't find expected array start\n"));
  669. return JSON_ERR_ARRAYSTART;
  670. } else
  671. cp++;
  672. tp = arr->arr.strings.store;
  673. arrcount = 0;
  674. /* Check for empty array */
  675. while (isspace((unsigned char) *cp))
  676. cp++;
  677. if (*cp == ']')
  678. goto breakout;
  679. for (offset = 0; offset < arr->maxlen; offset++) {
  680. char *ep = NULL;
  681. json_debug_trace((1, "Looking at %s\n", cp));
  682. switch (arr->element_type) {
  683. case t_string:
  684. if (isspace((unsigned char) *cp))
  685. cp++;
  686. if (*cp != '"')
  687. return JSON_ERR_BADSTRING;
  688. else
  689. ++cp;
  690. arr->arr.strings.ptrs[offset] = tp;
  691. for (; tp - arr->arr.strings.store < arr->arr.strings.storelen;
  692. tp++)
  693. if (*cp == '"') {
  694. ++cp;
  695. *tp++ = '\0';
  696. goto stringend;
  697. } else if (*cp == '\0') {
  698. json_debug_trace((1,
  699. "Bad string syntax in string list.\n"));
  700. return JSON_ERR_BADSTRING;
  701. } else {
  702. *tp = *cp++;
  703. }
  704. json_debug_trace((1, "Bad string syntax in string list.\n"));
  705. return JSON_ERR_BADSTRING;
  706. stringend:
  707. break;
  708. case t_object:
  709. case t_structobject:
  710. substatus =
  711. json_internal_read_object(cp, arr->arr.objects.subtype, arr,
  712. offset, &cp);
  713. if (substatus != 0) {
  714. if (end != NULL)
  715. *end = cp;
  716. return substatus;
  717. }
  718. break;
  719. case t_integer:
  720. arr->arr.integers.store[offset] = (int)strtol(cp, &ep, 0);
  721. if (ep == cp)
  722. return JSON_ERR_BADNUM;
  723. else
  724. cp = ep;
  725. break;
  726. case t_uinteger:
  727. arr->arr.uintegers.store[offset] = (unsigned int)strtoul(cp,
  728. &ep, 0);
  729. if (ep == cp)
  730. return JSON_ERR_BADNUM;
  731. else
  732. cp = ep;
  733. break;
  734. case t_longint:
  735. arr->arr.longint.store[offset] = strtol(cp, &ep, 0);
  736. if (ep == cp)
  737. return JSON_ERR_BADNUM;
  738. else
  739. cp = ep;
  740. break;
  741. case t_ulongint:
  742. arr->arr.ulongint.store[offset] = strtoul(cp, &ep, 0);
  743. if (ep == cp)
  744. return JSON_ERR_BADNUM;
  745. else
  746. cp = ep;
  747. break;
  748. case t_byte:
  749. arr->arr.bytes.store[offset] = (char)strtol(cp, &ep, 0);
  750. if (ep == cp)
  751. return JSON_ERR_BADNUM;
  752. else
  753. cp = ep;
  754. break;
  755. case t_ubyte:
  756. arr->arr.ubytes.store[offset] = (unsigned char)strtoul(cp,
  757. &ep, 0);
  758. if (ep == cp)
  759. return JSON_ERR_BADNUM;
  760. else
  761. cp = ep;
  762. break;
  763. case t_short:
  764. arr->arr.shorts.store[offset] = (short)strtol(cp, &ep, 0);
  765. if (ep == cp)
  766. return JSON_ERR_BADNUM;
  767. else
  768. cp = ep;
  769. break;
  770. case t_ushort:
  771. arr->arr.ushorts.store[offset] = (unsigned short)strtoul(cp,
  772. &ep, 0);
  773. if (ep == cp)
  774. return JSON_ERR_BADNUM;
  775. else
  776. cp = ep;
  777. break;
  778. case t_time:
  779. {
  780. timespec_t ts_tmp;
  781. if (*cp != '"')
  782. return JSON_ERR_BADSTRING;
  783. else
  784. ++cp;
  785. ts_tmp = iso8601_to_timespec((char *)cp);
  786. arr->arr.timespecs.store[offset] = ts_tmp;
  787. while (*cp && *cp != '"')
  788. cp++;
  789. if (*cp != '"')
  790. return JSON_ERR_BADSTRING;
  791. else
  792. ++cp;
  793. }
  794. break;
  795. case t_timespec:
  796. // TODO not sure how to implement this
  797. return JSON_ERR_BADNUM;
  798. break;
  799. case t_real:
  800. arr->arr.reals.store[offset] = strtod(cp, &ep);
  801. if (ep == cp)
  802. return JSON_ERR_BADNUM;
  803. else
  804. cp = ep;
  805. break;
  806. case t_boolean:
  807. if (str_starts_with(cp, "true")) {
  808. arr->arr.booleans.store[offset] = true;
  809. cp += 4;
  810. }
  811. else if (str_starts_with(cp, "false")) {
  812. arr->arr.booleans.store[offset] = false;
  813. cp += 5;
  814. }
  815. break;
  816. case t_character:
  817. case t_array:
  818. case t_check:
  819. case t_ignore:
  820. json_debug_trace((1, "Invalid array subtype.\n"));
  821. return JSON_ERR_SUBTYPE;
  822. }
  823. arrcount++;
  824. if (isspace((unsigned char) *cp))
  825. cp++;
  826. if (*cp == ']') {
  827. json_debug_trace((1, "End of array found.\n"));
  828. goto breakout;
  829. } else if (*cp == ',')
  830. cp++;
  831. else {
  832. json_debug_trace((1, "Bad trailing syntax on array.\n"));
  833. return JSON_ERR_BADSUBTRAIL;
  834. }
  835. }
  836. json_debug_trace((1, "Too many elements in array.\n"));
  837. if (end != NULL)
  838. *end = cp;
  839. return JSON_ERR_SUBTOOLONG;
  840. breakout:
  841. if (arr->count != NULL)
  842. *(arr->count) = arrcount;
  843. if (end != NULL)
  844. *end = cp;
  845. json_debug_trace((1, "leaving json_read_array() with %d elements\n",
  846. arrcount));
  847. return 0;
  848. }
  849. int json_read_object(const char *cp, const struct json_attr_t *attrs,
  850. const char **end)
  851. {
  852. int st;
  853. json_debug_trace((1, "json_read_object() sees '%s'\n", cp));
  854. st = json_internal_read_object(cp, attrs, NULL, 0, end);
  855. return st;
  856. }
  857. const char *json_error_string(int err)
  858. {
  859. const char *errors[] = {
  860. "unknown error while parsing JSON",
  861. "non-whitespace when expecting object start",
  862. "non-whitespace when expecting attribute start",
  863. "unknown attribute name",
  864. "attribute name too long",
  865. "saw [ when not expecting array",
  866. "array element specified, but no [",
  867. "string value too long",
  868. "token value too long",
  869. "garbage while expecting comma or } or ]",
  870. "didn't find expected array start",
  871. "error while parsing object array",
  872. "too many array elements",
  873. "garbage while expecting array comma",
  874. "unsupported array element type",
  875. "error while string parsing",
  876. "check attribute not matched",
  877. "can't support strings in parallel arrays",
  878. "invalid enumerated value",
  879. "saw quoted value when expecting nonstring",
  880. "didn't see quoted value when expecting string",
  881. "other data conversion error",
  882. "unexpected null value or attribute pointer",
  883. "object element specified, but no {",
  884. "input was empty or white-space only",
  885. };
  886. if (err <= 0 || err >= (int)(sizeof(errors) / sizeof(errors[0])))
  887. return errors[0];
  888. else
  889. return errors[err];
  890. }
  891. /* quote a JSON string so it can be used as a simple JSON string.
  892. * Used to output the JSON as a literal JSON string
  893. * escape control chars, escape double quote.
  894. * stop at NUL, in_len or bad unicode char
  895. */
  896. char *json_quote(const char *in_buffer, char *out_buffer, size_t in_len,
  897. size_t out_len)
  898. {
  899. const char *escape_match = "'\"/\\\b\f\n\r\t";
  900. const char *escaped_bit = "'\"/\\bfnrt";
  901. unsigned out_index = 0;
  902. const char *escape_ptr;
  903. unsigned in_index = 0;
  904. unsigned to_copy = 0;
  905. out_buffer[0] = '\0';
  906. // check in string, stop at NUL, done in_len, or out_buffer full
  907. for (in_index = 0; in_buffer[in_index] != '\0'; in_index++) {
  908. if (in_index >= in_len) {
  909. // got all from input buffer
  910. break;
  911. }
  912. if (out_index > (out_len - 8) ) {
  913. /* output out_buffer full. Not enough space for a 4-byte UTF + NUL,
  914. * or \uxxxx + NUL. Safer to check once, at the top,
  915. * than a lot of specific size checks later in the loop.
  916. */
  917. break;
  918. }
  919. if (in_buffer[in_index] & 0x80) {
  920. // highbit set. assume unicode
  921. to_copy = 0; // always reset before use, to shut up coverity
  922. // check in_len so we don't overrun in_buffer
  923. if ((in_len > (in_index + 1)) &&
  924. (0xC0 == (0xE0 & (uint8_t)in_buffer[in_index])) &&
  925. (0x80 == (0xC0 & (uint8_t)in_buffer[in_index + 1]))) {
  926. // utf-8 ish 16bit rune - deg, plusm, mplus etc.
  927. to_copy = 2;
  928. } else if ((in_len > (in_index + 2)) &&
  929. (0xE0 == (0xF0 & (uint8_t)in_buffer[in_index])) &&
  930. (0x80 == (0xC0 & (uint8_t)in_buffer[in_index + 1])) &&
  931. (0x80 == (0xC0 & (uint8_t)in_buffer[in_index + 2]))) {
  932. // utf-8 ish 24 bit rune - (double) prime etc.
  933. to_copy = 3;
  934. } else if ((in_len > (in_index + 3)) &&
  935. (0xF0 == (0xF8 & (uint8_t)in_buffer[in_index])) &&
  936. (0x80 == (0xC0 & (uint8_t)in_buffer[in_index + 1])) &&
  937. (0x80 == (0xC0 & (uint8_t)in_buffer[in_index + 2])) &&
  938. (0x80 == (0xC0 & (uint8_t)in_buffer[in_index + 3]))) {
  939. // utf-8 ish 32 bit rune - musical symbol g clef etc.
  940. to_copy = 4;
  941. } else {
  942. // WTF?? Short UTF? Bad UTF?
  943. str_appendf(out_buffer, out_len,
  944. "\\u%04x", in_buffer[in_index] & 0x0ff);
  945. out_index += 6;
  946. continue;
  947. }
  948. memcpy((void*)&out_buffer[out_index],
  949. (void*)&in_buffer[in_index], to_copy);
  950. out_index += to_copy;
  951. // minus one as the for loop does in_index++
  952. in_index += to_copy - 1;
  953. out_buffer[out_index] = '\0';
  954. continue;
  955. }
  956. /* Try to find current byte from in buffer in string escape
  957. * match if it is there append '\', the corresponding byte
  958. * from escaped bit, and a null byte to end of out buffer.
  959. */
  960. escape_ptr = strchr(escape_match, in_buffer[in_index]);
  961. if (escape_ptr >= escape_match) {
  962. out_buffer[out_index++] = '\\';
  963. out_buffer[out_index++] = escaped_bit[escape_ptr-escape_match];
  964. out_buffer[out_index] = 0;
  965. continue;
  966. }
  967. // Escape 0-31 and 127 if not previously handled (0-x01f,x7f)
  968. if ('\x1f' >= in_buffer[in_index] || '\x7f' == in_buffer[in_index]) {
  969. str_appendf(out_buffer, out_len, "\\u%04x",
  970. in_buffer[in_index] & 0x0ff);
  971. out_index += 6;
  972. continue;
  973. }
  974. // pass through everything not escaped.
  975. out_buffer[out_index++] = in_buffer[in_index];
  976. out_buffer[out_index] = '\0';
  977. }
  978. return out_buffer;
  979. }
  980. /* end */
  981. // vim: set expandtab shiftwidth=4