ujson.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /* ujson: see ujson.h. */
  2. #include "ujson.h"
  3. struct pstate {
  4. struct ujson *j;
  5. const char *s;
  6. unsigned int len, pos;
  7. int depth;
  8. };
  9. static int fail(struct pstate *p, const char *msg)
  10. {
  11. if (!p->j->error) {
  12. p->j->error = msg;
  13. p->j->error_pos = p->pos;
  14. }
  15. return -1;
  16. }
  17. static void skip_ws(struct pstate *p)
  18. {
  19. while (p->pos < p->len) {
  20. char c = p->s[p->pos];
  21. if (c != ' ' && c != '\t' && c != '\n' && c != '\r')
  22. break;
  23. p->pos++;
  24. }
  25. }
  26. static int new_node(struct pstate *p, unsigned char type)
  27. {
  28. struct ujson *j = p->j;
  29. struct ujson_node *n;
  30. if (j->n >= j->cap)
  31. return fail(p, "too many JSON nodes");
  32. n = &j->nodes[j->n];
  33. n->type = type;
  34. n->key = 0;
  35. n->str = 0;
  36. n->len = 0;
  37. n->child = -1;
  38. n->next = -1;
  39. return (int)j->n++;
  40. }
  41. static int sbuf_put(struct pstate *p, char c)
  42. {
  43. struct ujson *j = p->j;
  44. if (j->sn >= j->scap)
  45. return fail(p, "JSON string pool full");
  46. j->sbuf[j->sn++] = c;
  47. return 0;
  48. }
  49. static int put_utf8(struct pstate *p, unsigned int cp)
  50. {
  51. if (cp < 0x80)
  52. return sbuf_put(p, (char)cp);
  53. if (cp < 0x800) {
  54. if (sbuf_put(p, (char)(0xC0 | (cp >> 6))) ||
  55. sbuf_put(p, (char)(0x80 | (cp & 0x3F))))
  56. return -1;
  57. return 0;
  58. }
  59. if (cp < 0x10000) {
  60. if (sbuf_put(p, (char)(0xE0 | (cp >> 12))) ||
  61. sbuf_put(p, (char)(0x80 | ((cp >> 6) & 0x3F))) ||
  62. sbuf_put(p, (char)(0x80 | (cp & 0x3F))))
  63. return -1;
  64. return 0;
  65. }
  66. if (sbuf_put(p, (char)(0xF0 | (cp >> 18))) ||
  67. sbuf_put(p, (char)(0x80 | ((cp >> 12) & 0x3F))) ||
  68. sbuf_put(p, (char)(0x80 | ((cp >> 6) & 0x3F))) ||
  69. sbuf_put(p, (char)(0x80 | (cp & 0x3F))))
  70. return -1;
  71. return 0;
  72. }
  73. static int hex4(struct pstate *p, unsigned int *out)
  74. {
  75. unsigned int v = 0, i;
  76. if (p->pos + 4 > p->len)
  77. return fail(p, "short \\u escape");
  78. for (i = 0; i < 4; i++) {
  79. char c = p->s[p->pos + i];
  80. unsigned int d;
  81. if (c >= '0' && c <= '9')
  82. d = (unsigned int)(c - '0');
  83. else if (c >= 'a' && c <= 'f')
  84. d = (unsigned int)(c - 'a' + 10);
  85. else if (c >= 'A' && c <= 'F')
  86. d = (unsigned int)(c - 'A' + 10);
  87. else
  88. return fail(p, "bad \\u escape");
  89. v = v * 16 + d;
  90. }
  91. p->pos += 4;
  92. *out = v;
  93. return 0;
  94. }
  95. /* Parse a string literal at p->pos (on the opening quote) into the pool. */
  96. static int parse_string(struct pstate *p, const char **out, unsigned int *olen)
  97. {
  98. unsigned int start;
  99. p->pos++; /* opening quote */
  100. start = p->j->sn;
  101. for (;;) {
  102. unsigned char c;
  103. if (p->pos >= p->len)
  104. return fail(p, "unterminated string");
  105. c = (unsigned char)p->s[p->pos++];
  106. if (c == '"')
  107. break;
  108. if (c < 0x20)
  109. return fail(p, "control character in string");
  110. if (c != '\\') {
  111. if (sbuf_put(p, (char)c))
  112. return -1;
  113. continue;
  114. }
  115. if (p->pos >= p->len)
  116. return fail(p, "unterminated escape");
  117. c = (unsigned char)p->s[p->pos++];
  118. switch (c) {
  119. case '"': c = '"'; break;
  120. case '\\': c = '\\'; break;
  121. case '/': c = '/'; break;
  122. case 'b': c = 8; break;
  123. case 'f': c = 12; break;
  124. case 'n': c = '\n'; break;
  125. case 'r': c = '\r'; break;
  126. case 't': c = '\t'; break;
  127. case 'u': {
  128. unsigned int cp, lo;
  129. if (hex4(p, &cp))
  130. return -1;
  131. if (cp >= 0xD800 && cp <= 0xDBFF) {
  132. /* High surrogate: a low one should follow. */
  133. if (p->pos + 6 <= p->len &&
  134. p->s[p->pos] == '\\' &&
  135. p->s[p->pos + 1] == 'u') {
  136. unsigned int save = p->pos;
  137. p->pos += 2;
  138. if (hex4(p, &lo))
  139. return -1;
  140. if (lo >= 0xDC00 && lo <= 0xDFFF) {
  141. cp = 0x10000 +
  142. ((cp - 0xD800) << 10) +
  143. (lo - 0xDC00);
  144. } else {
  145. p->pos = save;
  146. cp = 0xFFFD;
  147. }
  148. } else {
  149. cp = 0xFFFD;
  150. }
  151. } else if (cp >= 0xDC00 && cp <= 0xDFFF) {
  152. cp = 0xFFFD;
  153. }
  154. if (put_utf8(p, cp))
  155. return -1;
  156. continue;
  157. }
  158. default:
  159. return fail(p, "bad escape");
  160. }
  161. if (sbuf_put(p, (char)c))
  162. return -1;
  163. }
  164. *olen = p->j->sn - start;
  165. if (sbuf_put(p, '\0'))
  166. return -1;
  167. *out = p->j->sbuf + start;
  168. return 0;
  169. }
  170. static int match_word(struct pstate *p, const char *w)
  171. {
  172. unsigned int i;
  173. for (i = 0; w[i]; i++)
  174. if (p->pos + i >= p->len || p->s[p->pos + i] != w[i])
  175. return 0;
  176. p->pos += i;
  177. return 1;
  178. }
  179. static int parse_value(struct pstate *p);
  180. static int parse_number(struct pstate *p)
  181. {
  182. unsigned int start = p->pos, i;
  183. int idx, digits = 0;
  184. struct ujson_node *n;
  185. if (p->pos < p->len && p->s[p->pos] == '-')
  186. p->pos++;
  187. while (p->pos < p->len) {
  188. char c = p->s[p->pos];
  189. if (c >= '0' && c <= '9') {
  190. digits++;
  191. } else if (c != '.' && c != 'e' && c != 'E' &&
  192. c != '+' && c != '-') {
  193. break;
  194. }
  195. p->pos++;
  196. }
  197. if (!digits)
  198. return fail(p, "bad number");
  199. idx = new_node(p, UJSON_NUMBER);
  200. if (idx < 0)
  201. return -1;
  202. n = &p->j->nodes[idx];
  203. n->str = p->j->sbuf + p->j->sn;
  204. n->len = p->pos - start;
  205. for (i = start; i < p->pos; i++)
  206. if (sbuf_put(p, p->s[i]))
  207. return -1;
  208. if (sbuf_put(p, '\0'))
  209. return -1;
  210. return idx;
  211. }
  212. static int parse_container(struct pstate *p, int obj)
  213. {
  214. int idx, last = -1;
  215. char close = obj ? '}' : ']';
  216. idx = new_node(p, obj ? UJSON_OBJECT : UJSON_ARRAY);
  217. if (idx < 0)
  218. return -1;
  219. if (++p->depth > UJSON_MAX_DEPTH)
  220. return fail(p, "JSON nested too deeply");
  221. p->pos++; /* [ or { */
  222. skip_ws(p);
  223. if (p->pos < p->len && p->s[p->pos] == close) {
  224. p->pos++;
  225. p->depth--;
  226. return idx;
  227. }
  228. for (;;) {
  229. const char *key = 0;
  230. unsigned int klen;
  231. int child;
  232. skip_ws(p);
  233. if (obj) {
  234. if (p->pos >= p->len || p->s[p->pos] != '"')
  235. return fail(p, "expected member name");
  236. if (parse_string(p, &key, &klen))
  237. return -1;
  238. skip_ws(p);
  239. if (p->pos >= p->len || p->s[p->pos] != ':')
  240. return fail(p, "expected ':'");
  241. p->pos++;
  242. skip_ws(p);
  243. }
  244. child = parse_value(p);
  245. if (child < 0)
  246. return -1;
  247. p->j->nodes[child].key = key;
  248. if (last < 0)
  249. p->j->nodes[idx].child = child;
  250. else
  251. p->j->nodes[last].next = child;
  252. last = child;
  253. p->j->nodes[idx].len++;
  254. skip_ws(p);
  255. if (p->pos >= p->len)
  256. return fail(p, "unterminated container");
  257. if (p->s[p->pos] == ',') {
  258. p->pos++;
  259. continue;
  260. }
  261. if (p->s[p->pos] == close) {
  262. p->pos++;
  263. break;
  264. }
  265. return fail(p, obj ? "expected ',' or '}'" :
  266. "expected ',' or ']'");
  267. }
  268. p->depth--;
  269. return idx;
  270. }
  271. static int parse_value(struct pstate *p)
  272. {
  273. char c;
  274. int idx;
  275. skip_ws(p);
  276. if (p->pos >= p->len)
  277. return fail(p, "unexpected end of JSON");
  278. c = p->s[p->pos];
  279. if (c == '{')
  280. return parse_container(p, 1);
  281. if (c == '[')
  282. return parse_container(p, 0);
  283. if (c == '"') {
  284. const char *s;
  285. unsigned int n;
  286. idx = new_node(p, UJSON_STRING);
  287. if (idx < 0)
  288. return -1;
  289. if (parse_string(p, &s, &n))
  290. return -1;
  291. p->j->nodes[idx].str = s;
  292. p->j->nodes[idx].len = n;
  293. return idx;
  294. }
  295. if (c == '-' || (c >= '0' && c <= '9'))
  296. return parse_number(p);
  297. if (match_word(p, "true"))
  298. return new_node(p, UJSON_TRUE);
  299. if (match_word(p, "false"))
  300. return new_node(p, UJSON_FALSE);
  301. if (match_word(p, "null"))
  302. return new_node(p, UJSON_NULL);
  303. return fail(p, "unexpected character");
  304. }
  305. void ujson_init(struct ujson *j, struct ujson_node *nodes, unsigned int cap,
  306. char *sbuf, unsigned int scap)
  307. {
  308. j->nodes = nodes;
  309. j->cap = cap;
  310. j->n = 0;
  311. j->sbuf = sbuf;
  312. j->scap = scap;
  313. j->sn = 0;
  314. j->error = 0;
  315. j->error_pos = 0;
  316. }
  317. int ujson_parse(struct ujson *j, const char *src, unsigned int len)
  318. {
  319. struct pstate p;
  320. int root;
  321. j->n = 0;
  322. j->sn = 0;
  323. j->error = 0;
  324. j->error_pos = 0;
  325. p.j = j;
  326. p.s = src;
  327. p.len = len;
  328. p.pos = 0;
  329. p.depth = 0;
  330. /* Skip a UTF-8 byte order mark. */
  331. if (len >= 3 && (unsigned char)src[0] == 0xEF &&
  332. (unsigned char)src[1] == 0xBB && (unsigned char)src[2] == 0xBF)
  333. p.pos = 3;
  334. root = parse_value(&p);
  335. if (root < 0)
  336. return -1;
  337. skip_ws(&p);
  338. if (p.pos != p.len)
  339. return fail(&p, "trailing data after JSON");
  340. return root;
  341. }
  342. static int valid(const struct ujson *j, int idx)
  343. {
  344. return idx >= 0 && (unsigned int)idx < j->n;
  345. }
  346. int ujson_type(const struct ujson *j, int idx)
  347. {
  348. return valid(j, idx) ? j->nodes[idx].type : -1;
  349. }
  350. static int str_eq(const char *a, const char *b)
  351. {
  352. while (*a && *a == *b) {
  353. a++;
  354. b++;
  355. }
  356. return *a == *b;
  357. }
  358. int ujson_get(const struct ujson *j, int idx, const char *key)
  359. {
  360. int c;
  361. if (!valid(j, idx) || j->nodes[idx].type != UJSON_OBJECT)
  362. return -1;
  363. for (c = j->nodes[idx].child; c >= 0; c = j->nodes[c].next)
  364. if (j->nodes[c].key && str_eq(j->nodes[c].key, key))
  365. return c;
  366. return -1;
  367. }
  368. int ujson_first(const struct ujson *j, int idx)
  369. {
  370. if (!valid(j, idx) || (j->nodes[idx].type != UJSON_ARRAY &&
  371. j->nodes[idx].type != UJSON_OBJECT))
  372. return -1;
  373. return j->nodes[idx].child;
  374. }
  375. int ujson_next(const struct ujson *j, int idx)
  376. {
  377. return valid(j, idx) ? j->nodes[idx].next : -1;
  378. }
  379. unsigned int ujson_count(const struct ujson *j, int idx)
  380. {
  381. if (!valid(j, idx) || (j->nodes[idx].type != UJSON_ARRAY &&
  382. j->nodes[idx].type != UJSON_OBJECT))
  383. return 0;
  384. return j->nodes[idx].len;
  385. }
  386. const char *ujson_str(const struct ujson *j, int idx)
  387. {
  388. if (!valid(j, idx) || j->nodes[idx].type != UJSON_STRING)
  389. return "";
  390. return j->nodes[idx].str;
  391. }
  392. const char *ujson_get_str(const struct ujson *j, int obj, const char *key)
  393. {
  394. return ujson_str(j, ujson_get(j, obj, key));
  395. }
  396. long ujson_int(const struct ujson *j, int idx, long def)
  397. {
  398. const char *s;
  399. long v = 0;
  400. int neg = 0, digits = 0;
  401. if (!valid(j, idx))
  402. return def;
  403. if (j->nodes[idx].type != UJSON_NUMBER &&
  404. j->nodes[idx].type != UJSON_STRING)
  405. return def;
  406. s = j->nodes[idx].str;
  407. if (*s == '-') {
  408. neg = 1;
  409. s++;
  410. }
  411. while (*s >= '0' && *s <= '9') {
  412. v = v * 10 + (*s - '0');
  413. digits++;
  414. s++;
  415. }
  416. if (!digits)
  417. return def;
  418. return neg ? -v : v;
  419. }
  420. int ujson_truthy(const struct ujson *j, int idx)
  421. {
  422. if (!valid(j, idx))
  423. return 0;
  424. switch (j->nodes[idx].type) {
  425. case UJSON_TRUE:
  426. return 1;
  427. case UJSON_NUMBER:
  428. return ujson_int(j, idx, 0) != 0;
  429. case UJSON_STRING:
  430. return j->nodes[idx].len != 0;
  431. default:
  432. return 0;
  433. }
  434. }
  435. int ujson_escape(char *out, unsigned int cap, const char *s)
  436. {
  437. static const char hex[] = "0123456789abcdef";
  438. unsigned int n = 0;
  439. #define PUT(ch) do { if (n + 1 >= cap) return -1; out[n++] = (ch); } while (0)
  440. PUT('"');
  441. for (; *s; s++) {
  442. unsigned char c = (unsigned char)*s;
  443. if (c == '"' || c == '\\') {
  444. PUT('\\');
  445. PUT((char)c);
  446. } else if (c == '\n') {
  447. PUT('\\');
  448. PUT('n');
  449. } else if (c == '\r') {
  450. PUT('\\');
  451. PUT('r');
  452. } else if (c == '\t') {
  453. PUT('\\');
  454. PUT('t');
  455. } else if (c < 0x20) {
  456. PUT('\\');
  457. PUT('u');
  458. PUT('0');
  459. PUT('0');
  460. PUT(hex[c >> 4]);
  461. PUT(hex[c & 15]);
  462. } else {
  463. PUT((char)c);
  464. }
  465. }
  466. PUT('"');
  467. #undef PUT
  468. out[n] = '\0';
  469. return (int)n;
  470. }