Devuan fork of gpsd
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  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 (c) 2010-2018 by the GPSD project
  51. SPDX-License-Identifier: BSD-2-clause
  52. ***************************************************************************/
  53. #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 "os_compat.h"
  62. #ifdef SOCKET_EXPORT_ENABLE
  63. #include "json.h"
  64. #include "gps.h" /* for safe_atof() prototype */
  65. #include "strfuncs.h"
  66. #include "timespec.h"
  67. #ifdef CLIENTDEBUG_ENABLE
  68. static int debuglevel = 0;
  69. static FILE *debugfp;
  70. void json_enable_debug(int level, FILE * fp)
  71. /* control the level and destination of debug trace messages */
  72. {
  73. debuglevel = level;
  74. debugfp = fp;
  75. }
  76. static void json_trace(int errlevel, const char *fmt, ...)
  77. /* assemble command in printf(3) style */
  78. {
  79. if (errlevel <= debuglevel && NULL != debugfp) {
  80. char buf[BUFSIZ] = {0};
  81. va_list ap;
  82. (void)strlcpy(buf, "json: ", sizeof(buf));
  83. va_start(ap, fmt);
  84. str_vappendf(buf, sizeof(buf), fmt, ap);
  85. va_end(ap);
  86. (void)fputs(buf, debugfp);
  87. }
  88. }
  89. # define json_debug_trace(args) (void) json_trace args
  90. #else
  91. # define json_debug_trace(args) do { } while (0)
  92. #endif /* CLIENTDEBUG_ENABLE */
  93. static char *json_target_address(const struct json_attr_t *cursor,
  94. const struct json_array_t
  95. *parent, int offset)
  96. {
  97. char *targetaddr = NULL;
  98. if (parent == NULL || parent->element_type != t_structobject) {
  99. /* ordinary case - use the address in the cursor structure */
  100. switch (cursor->type) {
  101. case t_byte:
  102. targetaddr = (char *)&cursor->addr.byte[offset];
  103. break;
  104. case t_ubyte:
  105. targetaddr = (char *)&cursor->addr.ubyte[offset];
  106. break;
  107. case t_ignore:
  108. targetaddr = NULL;
  109. break;
  110. case t_integer:
  111. targetaddr = (char *)&cursor->addr.integer[offset];
  112. break;
  113. case t_uinteger:
  114. targetaddr = (char *)&cursor->addr.uinteger[offset];
  115. break;
  116. case t_short:
  117. targetaddr = (char *)&cursor->addr.shortint[offset];
  118. break;
  119. case t_ushort:
  120. targetaddr = (char *)&cursor->addr.ushortint[offset];
  121. break;
  122. case t_time:
  123. targetaddr = (char *)&cursor->addr.ts[offset];
  124. break;
  125. case t_real:
  126. targetaddr = (char *)&cursor->addr.real[offset];
  127. break;
  128. case t_string:
  129. targetaddr = cursor->addr.string;
  130. break;
  131. case t_boolean:
  132. targetaddr = (char *)&cursor->addr.boolean[offset];
  133. break;
  134. case t_character:
  135. targetaddr = (char *)&cursor->addr.character[offset];
  136. break;
  137. default:
  138. targetaddr = NULL;
  139. break;
  140. }
  141. } else
  142. /* tricky case - hacking a member in an array of structures */
  143. targetaddr =
  144. parent->arr.objects.base + (offset * parent->arr.objects.stride) +
  145. cursor->addr.offset;
  146. json_debug_trace((1, "Target address for %s (offset %d) is %p\n",
  147. cursor->attribute, offset, targetaddr));
  148. return targetaddr;
  149. }
  150. static int json_internal_read_object(const char *cp,
  151. const struct json_attr_t *attrs,
  152. const struct json_array_t *parent,
  153. int offset,
  154. const char **end)
  155. {
  156. enum
  157. { init, await_attr, in_attr, await_value, in_val_string,
  158. in_escape, in_val_token, post_val, post_element
  159. } state = 0;
  160. #ifdef CLIENTDEBUG_ENABLE
  161. char *statenames[] = {
  162. "init", "await_attr", "in_attr", "await_value", "in_val_string",
  163. "in_escape", "in_val_token", "post_val", "post_element",
  164. };
  165. #endif /* CLIENTDEBUG_ENABLE */
  166. char attrbuf[JSON_ATTR_MAX + 1], *pattr = NULL;
  167. char valbuf[JSON_VAL_MAX + 1], *pval = NULL;
  168. bool value_quoted = false;
  169. char uescape[5]; /* enough space for 4 hex digits and a NUL */
  170. const struct json_attr_t *cursor;
  171. int substatus, maxlen = 0;
  172. unsigned int u;
  173. const struct json_enum_t *mp;
  174. char *lptr;
  175. if (end != NULL)
  176. *end = NULL; /* give it a well-defined value on parse failure */
  177. /* stuff fields with defaults in case they're omitted in the JSON input */
  178. for (cursor = attrs; cursor->attribute != NULL; cursor++)
  179. if (!cursor->nodefault) {
  180. lptr = json_target_address(cursor, parent, offset);
  181. if (lptr != NULL)
  182. switch (cursor->type) {
  183. case t_byte:
  184. lptr[0] = cursor->dflt.byte;
  185. break;
  186. case t_ubyte:
  187. lptr[0] = cursor->dflt.ubyte;
  188. break;
  189. case t_integer:
  190. memcpy(lptr, &cursor->dflt.integer, sizeof(int));
  191. break;
  192. case t_uinteger:
  193. memcpy(lptr, &cursor->dflt.uinteger, sizeof(unsigned int));
  194. break;
  195. case t_short:
  196. memcpy(lptr, &cursor->dflt.shortint, sizeof(short));
  197. break;
  198. case t_ushort:
  199. memcpy(lptr, &cursor->dflt.ushortint,
  200. sizeof(unsigned short));
  201. break;
  202. case t_time:
  203. memcpy(lptr, &cursor->dflt.ts, sizeof(timespec_t));
  204. break;
  205. case t_real:
  206. memcpy(lptr, &cursor->dflt.real, sizeof(double));
  207. break;
  208. case t_string:
  209. if (parent != NULL
  210. && parent->element_type != t_structobject
  211. && offset > 0)
  212. return JSON_ERR_NOPARSTR;
  213. lptr[0] = '\0';
  214. break;
  215. case t_boolean:
  216. memcpy(lptr, &cursor->dflt.boolean, sizeof(bool));
  217. break;
  218. case t_character:
  219. lptr[0] = cursor->dflt.character;
  220. break;
  221. case t_object: /* silences a compiler warning */
  222. case t_structobject:
  223. case t_array:
  224. case t_check:
  225. case t_ignore:
  226. break;
  227. }
  228. }
  229. json_debug_trace((1, "JSON parse of '%s' begins.\n", cp));
  230. /* parse input JSON */
  231. for (; *cp != '\0'; cp++) {
  232. json_debug_trace((2, "State %-14s, looking at '%c' (%p)\n",
  233. statenames[state], *cp, cp));
  234. switch (state) {
  235. case init:
  236. if (isspace((unsigned char) *cp))
  237. continue;
  238. else if (*cp == '{')
  239. state = await_attr;
  240. else {
  241. json_debug_trace((1,
  242. "Non-WS when expecting object start.\n"));
  243. if (end != NULL)
  244. *end = cp;
  245. return JSON_ERR_OBSTART;
  246. }
  247. break;
  248. case await_attr:
  249. if (isspace((unsigned char) *cp))
  250. continue;
  251. else if (*cp == '"') {
  252. state = in_attr;
  253. pattr = attrbuf;
  254. if (end != NULL)
  255. *end = cp;
  256. } else if (*cp == '}')
  257. break;
  258. else {
  259. json_debug_trace((1, "Non-WS when expecting attribute.\n"));
  260. if (end != NULL)
  261. *end = cp;
  262. return JSON_ERR_ATTRSTART;
  263. }
  264. break;
  265. case in_attr:
  266. if (pattr == NULL)
  267. /* don't update end here, leave at attribute start */
  268. return JSON_ERR_NULLPTR;
  269. if (*cp == '"') {
  270. *pattr++ = '\0';
  271. json_debug_trace((1, "Collected attribute name %s\n",
  272. attrbuf));
  273. for (cursor = attrs; cursor->attribute != NULL; cursor++) {
  274. json_debug_trace((2, "Checking against %s\n",
  275. cursor->attribute));
  276. if (strcmp(cursor->attribute, attrbuf) == 0)
  277. break;
  278. if (cursor->type == t_ignore && strncmp(cursor->attribute, "", 1) == 0) {
  279. break;
  280. }
  281. }
  282. if (cursor->attribute == NULL) {
  283. json_debug_trace((1,
  284. "Unknown attribute name '%s'"
  285. " (attributes begin with '%s').\n",
  286. attrbuf, attrs->attribute));
  287. /* don't update end here, leave at attribute start */
  288. return JSON_ERR_BADATTR;
  289. }
  290. state = await_value;
  291. if (cursor->type == t_string)
  292. maxlen = (int)cursor->len - 1;
  293. else if (cursor->type == t_check)
  294. maxlen = (int)strlen(cursor->dflt.check);
  295. else if (cursor->type == t_time || cursor->type == t_ignore)
  296. maxlen = JSON_VAL_MAX;
  297. else if (cursor->map != NULL)
  298. maxlen = (int)sizeof(valbuf) - 1;
  299. pval = valbuf;
  300. } else if (pattr >= attrbuf + JSON_ATTR_MAX - 1) {
  301. json_debug_trace((1, "Attribute name too long.\n"));
  302. /* don't update end here, leave at attribute start */
  303. return JSON_ERR_ATTRLEN;
  304. } else
  305. *pattr++ = *cp;
  306. break;
  307. case await_value:
  308. if (isspace((unsigned char) *cp) || *cp == ':')
  309. continue;
  310. else if (*cp == '[') {
  311. if (cursor->type != t_array) {
  312. json_debug_trace((1,
  313. "Saw [ when not expecting array.\n"));
  314. if (end != NULL)
  315. *end = cp;
  316. return JSON_ERR_NOARRAY;
  317. }
  318. substatus = json_read_array(cp, &cursor->addr.array, &cp);
  319. if (substatus != 0)
  320. return substatus;
  321. state = post_element;
  322. } else if (cursor->type == t_array) {
  323. json_debug_trace((1,
  324. "Array element was specified, but no [.\n"));
  325. if (end != NULL)
  326. *end = cp;
  327. return JSON_ERR_NOBRAK;
  328. } else if (*cp == '"') {
  329. value_quoted = true;
  330. state = in_val_string;
  331. pval = valbuf;
  332. } else {
  333. value_quoted = false;
  334. state = in_val_token;
  335. pval = valbuf;
  336. *pval++ = *cp;
  337. }
  338. break;
  339. case in_val_string:
  340. if (pval == NULL)
  341. /* don't update end here, leave at value start */
  342. return JSON_ERR_NULLPTR;
  343. if (*cp == '\\')
  344. state = in_escape;
  345. else if (*cp == '"') {
  346. *pval++ = '\0';
  347. json_debug_trace((1, "Collected string value %s\n", valbuf));
  348. state = post_val;
  349. } else if (pval > valbuf + JSON_VAL_MAX - 1
  350. || pval > valbuf + maxlen) {
  351. json_debug_trace((1, "String value too long.\n"));
  352. /* don't update end here, leave at value start */
  353. return JSON_ERR_STRLONG; /* */
  354. } else
  355. *pval++ = *cp;
  356. break;
  357. case in_escape:
  358. if (pval == NULL)
  359. /* don't update end here, leave at value start */
  360. return JSON_ERR_NULLPTR;
  361. else if (pval > valbuf + JSON_VAL_MAX - 1
  362. || pval > valbuf + maxlen) {
  363. json_debug_trace((1, "String value too long.\n"));
  364. /* don't update end here, leave at value start */
  365. return JSON_ERR_STRLONG; /* */
  366. }
  367. switch (*cp) {
  368. case 'b':
  369. *pval++ = '\b';
  370. break;
  371. case 'f':
  372. *pval++ = '\f';
  373. break;
  374. case 'n':
  375. *pval++ = '\n';
  376. break;
  377. case 'r':
  378. *pval++ = '\r';
  379. break;
  380. case 't':
  381. *pval++ = '\t';
  382. break;
  383. case 'u':
  384. {
  385. unsigned n;
  386. cp++; /* skip the 'u' */
  387. /* NetBSD 6 wants the cast */
  388. for (n = 0; n < 4 && isxdigit((int)*cp); n++)
  389. uescape[n] = *cp++;
  390. uescape[n] = '\0'; /* terminate */
  391. --cp;
  392. /* ECMA-404 says JSON \u must have 4 hex digits */
  393. if ((4 != n) || (1 != sscanf(uescape, "%4x", &u))) {
  394. return JSON_ERR_BADSTRING;
  395. }
  396. /* truncate values above 0xff */
  397. *pval++ = (unsigned char)u;
  398. }
  399. break;
  400. default: /* handles double quote and solidus */
  401. *pval++ = *cp;
  402. break;
  403. }
  404. state = in_val_string;
  405. break;
  406. case in_val_token:
  407. if (pval == NULL)
  408. /* don't update end here, leave at value start */
  409. return JSON_ERR_NULLPTR;
  410. if (isspace((unsigned char) *cp) || *cp == ',' || *cp == '}') {
  411. *pval = '\0';
  412. json_debug_trace((1, "Collected token value %s.\n", valbuf));
  413. state = post_val;
  414. if (*cp == '}' || *cp == ',')
  415. --cp;
  416. } else if (pval > valbuf + JSON_VAL_MAX - 1) {
  417. json_debug_trace((1, "Token value too long.\n"));
  418. /* don't update end here, leave at value start */
  419. return JSON_ERR_TOKLONG;
  420. } else
  421. *pval++ = *cp;
  422. break;
  423. /* coverity[unterminated_case] */
  424. case post_val:
  425. /*
  426. * We know that cursor points at the first spec matching
  427. * the current attribute. We don't know that it's *the*
  428. * correct spec; our dialect allows there to be any number
  429. * of adjacent ones with the same attrname but different
  430. * types. Here's where we try to seek forward for a
  431. * matching type/attr pair if we're not looking at one.
  432. */
  433. for (;;) {
  434. int seeking = cursor->type;
  435. if (value_quoted && (cursor->type == t_string
  436. || cursor->type == t_time))
  437. break;
  438. if ((strcmp(valbuf, "true")==0 || strcmp(valbuf, "false")==0)
  439. && seeking == t_boolean)
  440. break;
  441. if (isdigit((unsigned char) valbuf[0])) {
  442. bool decimal = strchr(valbuf, '.') != NULL;
  443. if (decimal && seeking == t_real)
  444. break;
  445. if (!decimal && (seeking == t_integer
  446. || seeking == t_uinteger))
  447. break;
  448. }
  449. if (cursor[1].attribute==NULL) /* out of possiblities */
  450. break;
  451. if (strcmp(cursor[1].attribute, attrbuf)!=0)
  452. break;
  453. ++cursor;
  454. }
  455. if (value_quoted
  456. && (cursor->type != t_string && cursor->type != t_character
  457. && cursor->type != t_check && cursor->type != t_time
  458. && cursor->type != t_ignore && cursor->map == 0)) {
  459. json_debug_trace((1, "Saw quoted value when expecting"
  460. " non-string.\n"));
  461. return JSON_ERR_QNONSTRING;
  462. }
  463. if (!value_quoted
  464. && (cursor->type == t_string || cursor->type == t_check
  465. || cursor->type == t_time || cursor->map != 0)) {
  466. json_debug_trace((1, "Didn't see quoted value when expecting"
  467. " string.\n"));
  468. return JSON_ERR_NONQSTRING;
  469. }
  470. if (cursor->map != 0) {
  471. for (mp = cursor->map; mp->name != NULL; mp++)
  472. if (strcmp(mp->name, valbuf) == 0) {
  473. goto foundit;
  474. }
  475. json_debug_trace((1, "Invalid enumerated value string %s.\n",
  476. valbuf));
  477. return JSON_ERR_BADENUM;
  478. foundit:
  479. (void)snprintf(valbuf, sizeof(valbuf), "%d", mp->value);
  480. }
  481. lptr = json_target_address(cursor, parent, offset);
  482. if (lptr != NULL)
  483. switch (cursor->type) {
  484. case t_byte:
  485. {
  486. int tmp = atoi(valbuf);
  487. lptr[0] = (char)tmp;
  488. }
  489. break;
  490. case t_ubyte:
  491. {
  492. int tmp = atoi(valbuf);
  493. lptr[0] = (unsigned char)tmp;
  494. }
  495. break;
  496. case t_integer:
  497. {
  498. int tmp = atoi(valbuf);
  499. memcpy(lptr, &tmp, sizeof(int));
  500. }
  501. break;
  502. case t_uinteger:
  503. {
  504. unsigned int tmp = (unsigned int)atoi(valbuf);
  505. memcpy(lptr, &tmp, sizeof(unsigned int));
  506. }
  507. break;
  508. case t_short:
  509. {
  510. short tmp = atoi(valbuf);
  511. memcpy(lptr, &tmp, sizeof(short));
  512. }
  513. break;
  514. case t_ushort:
  515. {
  516. unsigned short tmp = (unsigned int)atoi(valbuf);
  517. memcpy(lptr, &tmp, sizeof(unsigned short));
  518. }
  519. break;
  520. case t_time:
  521. {
  522. timespec_t ts_tmp = iso8601_to_timespec(valbuf);
  523. memcpy(lptr, &ts_tmp, sizeof(timespec_t));
  524. }
  525. break;
  526. case t_real:
  527. {
  528. double tmp = safe_atof(valbuf);
  529. memcpy(lptr, &tmp, sizeof(double));
  530. }
  531. break;
  532. case t_string:
  533. if (parent != NULL
  534. && parent->element_type != t_structobject
  535. && offset > 0)
  536. return JSON_ERR_NOPARSTR;
  537. (void)strlcpy(lptr, valbuf, cursor->len);
  538. break;
  539. case t_boolean:
  540. {
  541. bool tmp = (strcmp(valbuf, "true") == 0);
  542. memcpy(lptr, &tmp, sizeof(bool));
  543. }
  544. break;
  545. case t_character:
  546. if (strlen(valbuf) > 1)
  547. /* don't update end here, leave at value start */
  548. return JSON_ERR_STRLONG;
  549. else
  550. lptr[0] = valbuf[0];
  551. break;
  552. case t_ignore: /* silences a compiler warning */
  553. case t_object: /* silences a compiler warning */
  554. case t_structobject:
  555. case t_array:
  556. break;
  557. case t_check:
  558. if (strcmp(cursor->dflt.check, valbuf) != 0) {
  559. json_debug_trace((1, "Required attribute value %s"
  560. " not present.\n",
  561. cursor->dflt.check));
  562. /* don't update end here, leave at start of attribute */
  563. return JSON_ERR_CHECKFAIL;
  564. }
  565. break;
  566. }
  567. /* FALLTHROUGH */
  568. case post_element:
  569. if (isspace((unsigned char) *cp))
  570. continue;
  571. else if (*cp == ',')
  572. state = await_attr;
  573. else if (*cp == '}') {
  574. ++cp;
  575. goto good_parse;
  576. } else {
  577. json_debug_trace((1, "Garbage while expecting comma or }\n"));
  578. if (end != NULL)
  579. *end = cp;
  580. return JSON_ERR_BADTRAIL;
  581. }
  582. break;
  583. }
  584. }
  585. good_parse:
  586. /* in case there's another object following, consume trailing WS */
  587. while (isspace((unsigned char) *cp))
  588. ++cp;
  589. if (end != NULL)
  590. *end = cp;
  591. json_debug_trace((1, "JSON parse ends.\n"));
  592. return 0;
  593. }
  594. int json_read_array(const char *cp, const struct json_array_t *arr,
  595. const char **end)
  596. {
  597. int substatus, offset, arrcount;
  598. char *tp;
  599. if (end != NULL)
  600. *end = NULL; /* give it a well-defined value on parse failure */
  601. json_debug_trace((1, "Entered json_read_array()\n"));
  602. while (isspace((unsigned char) *cp))
  603. cp++;
  604. if (*cp != '[') {
  605. json_debug_trace((1, "Didn't find expected array start\n"));
  606. return JSON_ERR_ARRAYSTART;
  607. } else
  608. cp++;
  609. tp = arr->arr.strings.store;
  610. arrcount = 0;
  611. /* Check for empty array */
  612. while (isspace((unsigned char) *cp))
  613. cp++;
  614. if (*cp == ']')
  615. goto breakout;
  616. for (offset = 0; offset < arr->maxlen; offset++) {
  617. char *ep = NULL;
  618. json_debug_trace((1, "Looking at %s\n", cp));
  619. switch (arr->element_type) {
  620. case t_string:
  621. if (isspace((unsigned char) *cp))
  622. cp++;
  623. if (*cp != '"')
  624. return JSON_ERR_BADSTRING;
  625. else
  626. ++cp;
  627. arr->arr.strings.ptrs[offset] = tp;
  628. for (; tp - arr->arr.strings.store < arr->arr.strings.storelen;
  629. tp++)
  630. if (*cp == '"') {
  631. ++cp;
  632. *tp++ = '\0';
  633. goto stringend;
  634. } else if (*cp == '\0') {
  635. json_debug_trace((1,
  636. "Bad string syntax in string list.\n"));
  637. return JSON_ERR_BADSTRING;
  638. } else {
  639. *tp = *cp++;
  640. }
  641. json_debug_trace((1, "Bad string syntax in string list.\n"));
  642. return JSON_ERR_BADSTRING;
  643. stringend:
  644. break;
  645. case t_object:
  646. case t_structobject:
  647. substatus =
  648. json_internal_read_object(cp, arr->arr.objects.subtype, arr,
  649. offset, &cp);
  650. if (substatus != 0) {
  651. if (end != NULL)
  652. end = &cp;
  653. return substatus;
  654. }
  655. break;
  656. case t_integer:
  657. arr->arr.integers.store[offset] = (int)strtol(cp, &ep, 0);
  658. if (ep == cp)
  659. return JSON_ERR_BADNUM;
  660. else
  661. cp = ep;
  662. break;
  663. case t_uinteger:
  664. arr->arr.uintegers.store[offset] = (unsigned int)strtoul(cp,
  665. &ep, 0);
  666. if (ep == cp)
  667. return JSON_ERR_BADNUM;
  668. else
  669. cp = ep;
  670. break;
  671. case t_byte:
  672. arr->arr.bytes.store[offset] = (char)strtol(cp, &ep, 0);
  673. if (ep == cp)
  674. return JSON_ERR_BADNUM;
  675. else
  676. cp = ep;
  677. break;
  678. case t_ubyte:
  679. arr->arr.ubytes.store[offset] = (unsigned char)strtoul(cp,
  680. &ep, 0);
  681. if (ep == cp)
  682. return JSON_ERR_BADNUM;
  683. else
  684. cp = ep;
  685. break;
  686. case t_short:
  687. arr->arr.shorts.store[offset] = (short)strtol(cp, &ep, 0);
  688. if (ep == cp)
  689. return JSON_ERR_BADNUM;
  690. else
  691. cp = ep;
  692. break;
  693. case t_ushort:
  694. arr->arr.ushorts.store[offset] = (unsigned short)strtoul(cp,
  695. &ep, 0);
  696. if (ep == cp)
  697. return JSON_ERR_BADNUM;
  698. else
  699. cp = ep;
  700. break;
  701. case t_time:
  702. {
  703. timespec_t ts_tmp;
  704. if (*cp != '"')
  705. return JSON_ERR_BADSTRING;
  706. else
  707. ++cp;
  708. ts_tmp = iso8601_to_timespec((char *)cp);
  709. arr->arr.timespecs.store[offset] = ts_tmp;
  710. while (*cp && *cp != '"')
  711. cp++;
  712. if (*cp != '"')
  713. return JSON_ERR_BADSTRING;
  714. else
  715. ++cp;
  716. }
  717. break;
  718. case t_real:
  719. arr->arr.reals.store[offset] = strtod(cp, &ep);
  720. if (ep == cp)
  721. return JSON_ERR_BADNUM;
  722. else
  723. cp = ep;
  724. break;
  725. case t_boolean:
  726. if (str_starts_with(cp, "true")) {
  727. arr->arr.booleans.store[offset] = true;
  728. cp += 4;
  729. }
  730. else if (str_starts_with(cp, "false")) {
  731. arr->arr.booleans.store[offset] = false;
  732. cp += 5;
  733. }
  734. break;
  735. case t_character:
  736. case t_array:
  737. case t_check:
  738. case t_ignore:
  739. json_debug_trace((1, "Invalid array subtype.\n"));
  740. return JSON_ERR_SUBTYPE;
  741. }
  742. arrcount++;
  743. if (isspace((unsigned char) *cp))
  744. cp++;
  745. if (*cp == ']') {
  746. json_debug_trace((1, "End of array found.\n"));
  747. goto breakout;
  748. } else if (*cp == ',')
  749. cp++;
  750. else {
  751. json_debug_trace((1, "Bad trailing syntax on array.\n"));
  752. return JSON_ERR_BADSUBTRAIL;
  753. }
  754. }
  755. json_debug_trace((1, "Too many elements in array.\n"));
  756. if (end != NULL)
  757. *end = cp;
  758. return JSON_ERR_SUBTOOLONG;
  759. breakout:
  760. if (arr->count != NULL)
  761. *(arr->count) = arrcount;
  762. if (end != NULL)
  763. *end = cp;
  764. json_debug_trace((1, "leaving json_read_array() with %d elements\n",
  765. arrcount));
  766. return 0;
  767. }
  768. int json_read_object(const char *cp, const struct json_attr_t *attrs,
  769. const char **end)
  770. {
  771. int st;
  772. json_debug_trace((1, "json_read_object() sees '%s'\n", cp));
  773. st = json_internal_read_object(cp, attrs, NULL, 0, end);
  774. return st;
  775. }
  776. const char *json_error_string(int err)
  777. {
  778. const char *errors[] = {
  779. "unknown error while parsing JSON",
  780. "non-whitespace when expecting object start",
  781. "non-whitespace when expecting attribute start",
  782. "unknown attribute name",
  783. "attribute name too long",
  784. "saw [ when not expecting array",
  785. "array element specified, but no [",
  786. "string value too long",
  787. "token value too long",
  788. "garbage while expecting comma or } or ]",
  789. "didn't find expected array start",
  790. "error while parsing object array",
  791. "too many array elements",
  792. "garbage while expecting array comma",
  793. "unsupported array element type",
  794. "error while string parsing",
  795. "check attribute not matched",
  796. "can't support strings in parallel arrays",
  797. "invalid enumerated value",
  798. "saw quoted value when expecting nonstring",
  799. "didn't see quoted value when expecting string",
  800. "other data conversion error",
  801. "unexpected null value or attribute pointer",
  802. "object element specified, but no {",
  803. };
  804. if (err <= 0 || err >= (int)(sizeof(errors) / sizeof(errors[0])))
  805. return errors[0];
  806. else
  807. return errors[err];
  808. }
  809. #endif /* SOCKET_EXPORT_ENABLE */
  810. /* end */