is_json.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*-
  2. * Copyright (c) 2018 Christos Zoulas
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  15. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  16. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  17. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  18. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  19. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  20. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  21. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  22. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  23. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  24. * POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. /*
  27. * Parse JSON object serialization format (RFC-7159)
  28. */
  29. #ifndef TEST
  30. #include "file.h"
  31. #ifndef lint
  32. FILE_RCSID("@(#)$File: is_json.c,v 1.11 2018/10/15 16:29:16 christos Exp $")
  33. #endif
  34. #include <string.h>
  35. #include "magic.h"
  36. #endif
  37. #ifdef DEBUG
  38. #include <stdio.h>
  39. #define DPRINTF(a, b, c) \
  40. printf("%s [%.2x/%c] %.20s\n", (a), *(b), *(b), (const char *)(c))
  41. #else
  42. #define DPRINTF(a, b, c) (void)0
  43. #endif
  44. #define JSON_ARRAY 0
  45. #define JSON_CONSTANT 1
  46. #define JSON_NUMBER 2
  47. #define JSON_OBJECT 3
  48. #define JSON_STRING 4
  49. #define JSON_MAX 5
  50. /*
  51. * if JSON_COUNT != 0:
  52. * count all the objects, require that we have the whole data file
  53. * otherwise:
  54. * stop if we find an object or an array
  55. */
  56. #ifndef JSON_COUNT
  57. #define JSON_COUNT 0
  58. #endif
  59. static int json_parse(const unsigned char **, const unsigned char *, size_t *,
  60. size_t);
  61. static int
  62. json_isspace(const unsigned char uc)
  63. {
  64. switch (uc) {
  65. case ' ':
  66. case '\n':
  67. case '\r':
  68. case '\t':
  69. return 1;
  70. default:
  71. return 0;
  72. }
  73. }
  74. static int
  75. json_isdigit(unsigned char uc)
  76. {
  77. switch (uc) {
  78. case '0': case '1': case '2': case '3': case '4':
  79. case '5': case '6': case '7': case '8': case '9':
  80. return 1;
  81. default:
  82. return 0;
  83. }
  84. }
  85. static int
  86. json_isxdigit(unsigned char uc)
  87. {
  88. if (json_isdigit(uc))
  89. return 1;
  90. switch (uc) {
  91. case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
  92. case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
  93. return 1;
  94. default:
  95. return 0;
  96. }
  97. }
  98. static const unsigned char *
  99. json_skip_space(const unsigned char *uc, const unsigned char *ue)
  100. {
  101. while (uc < ue && json_isspace(*uc))
  102. uc++;
  103. return uc;
  104. }
  105. static int
  106. json_parse_string(const unsigned char **ucp, const unsigned char *ue)
  107. {
  108. const unsigned char *uc = *ucp;
  109. size_t i;
  110. DPRINTF("Parse string: ", uc, *ucp);
  111. while (uc < ue) {
  112. switch (*uc++) {
  113. case '\0':
  114. goto out;
  115. case '\\':
  116. if (uc == ue)
  117. goto out;
  118. switch (*uc++) {
  119. case '\0':
  120. goto out;
  121. case '"':
  122. case '\\':
  123. case '/':
  124. case 'b':
  125. case 'f':
  126. case 'n':
  127. case 'r':
  128. case 't':
  129. continue;
  130. case 'u':
  131. if (ue - uc < 4) {
  132. uc = ue;
  133. goto out;
  134. }
  135. for (i = 0; i < 4; i++)
  136. if (!json_isxdigit(*uc++))
  137. goto out;
  138. continue;
  139. default:
  140. goto out;
  141. }
  142. case '"':
  143. *ucp = uc;
  144. return 1;
  145. default:
  146. continue;
  147. }
  148. }
  149. out:
  150. DPRINTF("Bad string: ", uc, *ucp);
  151. *ucp = uc;
  152. return 0;
  153. }
  154. static int
  155. json_parse_array(const unsigned char **ucp, const unsigned char *ue,
  156. size_t *st, size_t lvl)
  157. {
  158. const unsigned char *uc = *ucp;
  159. DPRINTF("Parse array: ", uc, *ucp);
  160. while (uc < ue) {
  161. if (!json_parse(&uc, ue, st, lvl + 1))
  162. goto out;
  163. if (uc == ue)
  164. goto out;
  165. switch (*uc) {
  166. case ',':
  167. uc++;
  168. continue;
  169. case ']':
  170. *ucp = uc + 1;
  171. return 1;
  172. default:
  173. goto out;
  174. }
  175. }
  176. out:
  177. DPRINTF("Bad array: ", uc, *ucp);
  178. *ucp = uc;
  179. return 0;
  180. }
  181. static int
  182. json_parse_object(const unsigned char **ucp, const unsigned char *ue,
  183. size_t *st, size_t lvl)
  184. {
  185. const unsigned char *uc = *ucp;
  186. DPRINTF("Parse object: ", uc, *ucp);
  187. while (uc < ue) {
  188. uc = json_skip_space(uc, ue);
  189. if (uc == ue)
  190. goto out;
  191. if (*uc++ != '"') {
  192. DPRINTF("not string", uc, *ucp);
  193. goto out;
  194. }
  195. DPRINTF("next field", uc, *ucp);
  196. if (!json_parse_string(&uc, ue)) {
  197. DPRINTF("not string", uc, *ucp);
  198. goto out;
  199. }
  200. uc = json_skip_space(uc, ue);
  201. if (uc == ue)
  202. goto out;
  203. if (*uc++ != ':') {
  204. DPRINTF("not colon", uc, *ucp);
  205. goto out;
  206. }
  207. if (!json_parse(&uc, ue, st, lvl + 1)) {
  208. DPRINTF("not json", uc, *ucp);
  209. goto out;
  210. }
  211. if (uc == ue)
  212. goto out;
  213. switch (*uc++) {
  214. case ',':
  215. continue;
  216. case '}': /* { */
  217. *ucp = uc;
  218. DPRINTF("Good object: ", uc, *ucp);
  219. return 1;
  220. default:
  221. *ucp = uc - 1;
  222. DPRINTF("not more", uc, *ucp);
  223. goto out;
  224. }
  225. }
  226. out:
  227. DPRINTF("Bad object: ", uc, *ucp);
  228. *ucp = uc;
  229. return 0;
  230. }
  231. static int
  232. json_parse_number(const unsigned char **ucp, const unsigned char *ue)
  233. {
  234. const unsigned char *uc = *ucp;
  235. int got = 0;
  236. DPRINTF("Parse number: ", uc, *ucp);
  237. if (uc == ue)
  238. return 0;
  239. if (*uc == '-')
  240. uc++;
  241. for (; uc < ue; uc++) {
  242. if (!json_isdigit(*uc))
  243. break;
  244. got = 1;
  245. }
  246. if (uc == ue)
  247. goto out;
  248. if (*uc == '.')
  249. uc++;
  250. for (; uc < ue; uc++) {
  251. if (!json_isdigit(*uc))
  252. break;
  253. got = 1;
  254. }
  255. if (uc == ue)
  256. goto out;
  257. if (got && (*uc == 'e' || *uc == 'E')) {
  258. uc++;
  259. got = 0;
  260. if (uc == ue)
  261. goto out;
  262. if (*uc == '+' || *uc == '-')
  263. uc++;
  264. for (; uc < ue; uc++) {
  265. if (!json_isdigit(*uc))
  266. break;
  267. got = 1;
  268. }
  269. }
  270. out:
  271. if (!got)
  272. DPRINTF("Bad number: ", uc, *ucp);
  273. else
  274. DPRINTF("Good number: ", uc, *ucp);
  275. *ucp = uc;
  276. return got;
  277. }
  278. static int
  279. json_parse_const(const unsigned char **ucp, const unsigned char *ue,
  280. const char *str, size_t len)
  281. {
  282. const unsigned char *uc = *ucp;
  283. DPRINTF("Parse const: ", uc, *ucp);
  284. for (len--; uc < ue && --len;) {
  285. if (*uc++ == *++str)
  286. continue;
  287. }
  288. if (len)
  289. DPRINTF("Bad const: ", uc, *ucp);
  290. *ucp = uc;
  291. return len == 0;
  292. }
  293. static int
  294. json_parse(const unsigned char **ucp, const unsigned char *ue,
  295. size_t *st, size_t lvl)
  296. {
  297. const unsigned char *uc;
  298. int rv = 0;
  299. int t;
  300. uc = json_skip_space(*ucp, ue);
  301. if (uc == ue)
  302. goto out;
  303. // Avoid recursion
  304. if (lvl > 20)
  305. return 0;
  306. #if JSON_COUNT
  307. /* bail quickly if not counting */
  308. if (lvl > 1 && (st[JSON_OBJECT] || st[JSON_ARRAY]))
  309. return 1;
  310. #endif
  311. DPRINTF("Parse general: ", uc, *ucp);
  312. switch (*uc++) {
  313. case '"':
  314. rv = json_parse_string(&uc, ue);
  315. t = JSON_STRING;
  316. break;
  317. case '[':
  318. rv = json_parse_array(&uc, ue, st, lvl + 1);
  319. t = JSON_ARRAY;
  320. break;
  321. case '{': /* '}' */
  322. rv = json_parse_object(&uc, ue, st, lvl + 1);
  323. t = JSON_OBJECT;
  324. break;
  325. case 't':
  326. rv = json_parse_const(&uc, ue, "true", sizeof("true"));
  327. t = JSON_CONSTANT;
  328. break;
  329. case 'f':
  330. rv = json_parse_const(&uc, ue, "false", sizeof("false"));
  331. t = JSON_CONSTANT;
  332. break;
  333. case 'n':
  334. rv = json_parse_const(&uc, ue, "null", sizeof("null"));
  335. t = JSON_CONSTANT;
  336. break;
  337. default:
  338. --uc;
  339. rv = json_parse_number(&uc, ue);
  340. t = JSON_NUMBER;
  341. break;
  342. }
  343. if (rv)
  344. st[t]++;
  345. uc = json_skip_space(uc, ue);
  346. out:
  347. *ucp = uc;
  348. DPRINTF("End general: ", uc, *ucp);
  349. if (lvl == 0)
  350. return rv && (st[JSON_ARRAY] || st[JSON_OBJECT]);
  351. return rv;
  352. }
  353. #ifndef TEST
  354. int
  355. file_is_json(struct magic_set *ms, const struct buffer *b)
  356. {
  357. const unsigned char *uc = CAST(const unsigned char *, b->fbuf);
  358. const unsigned char *ue = uc + b->flen;
  359. size_t st[JSON_MAX];
  360. int mime = ms->flags & MAGIC_MIME;
  361. if ((ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION)) != 0)
  362. return 0;
  363. memset(st, 0, sizeof(st));
  364. if (!json_parse(&uc, ue, st, 0))
  365. return 0;
  366. if (mime == MAGIC_MIME_ENCODING)
  367. return 1;
  368. if (mime) {
  369. if (file_printf(ms, "application/json") == -1)
  370. return -1;
  371. return 1;
  372. }
  373. if (file_printf(ms, "JSON data") == -1)
  374. return -1;
  375. #if JSON_COUNT
  376. #define P(n) st[n], st[n] > 1 ? "s" : ""
  377. if (file_printf(ms, " (%" SIZE_T_FORMAT "u object%s, %" SIZE_T_FORMAT
  378. "u array%s, %" SIZE_T_FORMAT "u string%s, %" SIZE_T_FORMAT
  379. "u constant%s, %" SIZE_T_FORMAT "u number%s)", P(JSON_OBJECT),
  380. P(JSON_ARRAY), P(JSON_STRING), P(JSON_CONSTANT), P(JSON_NUMBER))
  381. == -1)
  382. return -1;
  383. #endif
  384. return 1;
  385. }
  386. #else
  387. #include <sys/types.h>
  388. #include <sys/stat.h>
  389. #include <stdio.h>
  390. #include <fcntl.h>
  391. #include <unistd.h>
  392. #include <stdlib.h>
  393. #include <stdint.h>
  394. #include <err.h>
  395. int
  396. main(int argc, char *argv[])
  397. {
  398. int fd, rv;
  399. struct stat st;
  400. unsigned char *p;
  401. size_t stats[JSON_MAX];
  402. if ((fd = open(argv[1], O_RDONLY)) == -1)
  403. err(EXIT_FAILURE, "Can't open `%s'", argv[1]);
  404. if (fstat(fd, &st) == -1)
  405. err(EXIT_FAILURE, "Can't stat `%s'", argv[1]);
  406. if ((p = malloc(st.st_size)) == NULL)
  407. err(EXIT_FAILURE, "Can't allocate %jd bytes",
  408. (intmax_t)st.st_size);
  409. if (read(fd, p, st.st_size) != st.st_size)
  410. err(EXIT_FAILURE, "Can't read %jd bytes",
  411. (intmax_t)st.st_size);
  412. memset(stats, 0, sizeof(stats));
  413. printf("is json %d\n", json_parse((const unsigned char **)&p,
  414. p + st.st_size, stats, 0));
  415. return 0;
  416. }
  417. #endif