nxjson.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * Copyright (c) 2013 Yaroslav Stavnichiy <yarosla@gmail.com>
  3. *
  4. * This file is part of NXJSON.
  5. *
  6. * NXJSON is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License
  8. * as published by the Free Software Foundation, either version 3
  9. * of the License, or (at your option) any later version.
  10. *
  11. * NXJSON is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with NXJSON. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. // this file can be #included in your code
  20. #ifndef NXJSON_C
  21. #define NXJSON_C
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <assert.h>
  29. #include <errno.h>
  30. #include <furi.h>
  31. #include "nxjson.h"
  32. // redefine NX_JSON_CALLOC & NX_JSON_FREE to use custom allocator
  33. #ifndef NX_JSON_CALLOC
  34. #define NX_JSON_CALLOC() calloc(1, sizeof(nx_json))
  35. #define NX_JSON_FREE(json) free((void*)(json))
  36. #endif
  37. // redefine NX_JSON_REPORT_ERROR to use custom error reporting
  38. #ifndef NX_JSON_REPORT_ERROR
  39. #define NX_JSON_REPORT_ERROR(msg, p) FURI_LOG_E("nxjson", "PARSE ERROR (%d): at %s", __LINE__, p)
  40. #endif
  41. #define IS_WHITESPACE(c) ((unsigned char)(c) <= (unsigned char)' ')
  42. static nx_json* create_json(nx_json_type type, const char* key, nx_json* parent) {
  43. nx_json* js = NX_JSON_CALLOC();
  44. assert(js);
  45. js->type = type;
  46. js->key = key;
  47. if(!parent->children.last) {
  48. parent->children.first = parent->children.last = js;
  49. } else {
  50. parent->children.last->next = js;
  51. parent->children.last = js;
  52. }
  53. parent->children.length++;
  54. return js;
  55. }
  56. void nx_json_free(const nx_json* js) {
  57. if(!js) {
  58. return;
  59. }
  60. if(js->type == NX_JSON_OBJECT || js->type == NX_JSON_ARRAY) {
  61. nx_json* p = js->children.first;
  62. nx_json* p1;
  63. while(p) {
  64. p1 = p->next;
  65. nx_json_free(p);
  66. p = p1;
  67. }
  68. }
  69. NX_JSON_FREE(js);
  70. }
  71. static int unicode_to_utf8(unsigned int codepoint, char* p, char** endp) {
  72. // code from http://stackoverflow.com/a/4609989/697313
  73. if(codepoint < 0x80)
  74. *p++ = codepoint;
  75. else if(codepoint < 0x800)
  76. *p++ = 192 + codepoint / 64, *p++ = 128 + codepoint % 64;
  77. else if(codepoint - 0xd800u < 0x800)
  78. return 0; // surrogate must have been treated earlier
  79. else if(codepoint < 0x10000)
  80. *p++ = 224 + codepoint / 4096, *p++ = 128 + codepoint / 64 % 64,
  81. *p++ = 128 + codepoint % 64;
  82. else if(codepoint < 0x110000)
  83. *p++ = 240 + codepoint / 262144, *p++ = 128 + codepoint / 4096 % 64,
  84. *p++ = 128 + codepoint / 64 % 64, *p++ = 128 + codepoint % 64;
  85. else
  86. return 0; // error
  87. *endp = p;
  88. return 1;
  89. }
  90. nx_json_unicode_encoder nx_json_unicode_to_utf8 = unicode_to_utf8;
  91. static inline int hex_val(char c) {
  92. if(c >= '0' && c <= '9') return c - '0';
  93. if(c >= 'a' && c <= 'f') return c - 'a' + 10;
  94. if(c >= 'A' && c <= 'F') return c - 'A' + 10;
  95. return -1;
  96. }
  97. static char* unescape_string(char* s, char** end, nx_json_unicode_encoder encoder) {
  98. char* p = s;
  99. char* d = s;
  100. char c;
  101. while((c = *p++)) {
  102. if(c == '"') {
  103. *d = '\0';
  104. *end = p;
  105. return s;
  106. } else if(c == '\\') {
  107. switch(*p) {
  108. case '\\':
  109. case '/':
  110. case '"':
  111. *d++ = *p++;
  112. break;
  113. case 'b':
  114. *d++ = '\b';
  115. p++;
  116. break;
  117. case 'f':
  118. *d++ = '\f';
  119. p++;
  120. break;
  121. case 'n':
  122. *d++ = '\n';
  123. p++;
  124. break;
  125. case 'r':
  126. *d++ = '\r';
  127. p++;
  128. break;
  129. case 't':
  130. *d++ = '\t';
  131. p++;
  132. break;
  133. case 'u': // unicode
  134. if(!encoder) {
  135. // leave untouched
  136. *d++ = c;
  137. break;
  138. }
  139. char* ps = p - 1;
  140. int h1, h2, h3, h4;
  141. if((h1 = hex_val(p[1])) < 0 || (h2 = hex_val(p[2])) < 0 ||
  142. (h3 = hex_val(p[3])) < 0 || (h4 = hex_val(p[4])) < 0) {
  143. NX_JSON_REPORT_ERROR("invalid unicode escape", p - 1);
  144. return 0;
  145. }
  146. unsigned int codepoint = h1 << 12 | h2 << 8 | h3 << 4 | h4;
  147. if((codepoint & 0xfc00) ==
  148. 0xd800) { // high surrogate; need one more unicode to succeed
  149. p += 6;
  150. if(p[-1] != '\\' || *p != 'u' || (h1 = hex_val(p[1])) < 0 ||
  151. (h2 = hex_val(p[2])) < 0 || (h3 = hex_val(p[3])) < 0 ||
  152. (h4 = hex_val(p[4])) < 0) {
  153. NX_JSON_REPORT_ERROR("invalid unicode surrogate", ps);
  154. return 0;
  155. }
  156. unsigned int codepoint2 = h1 << 12 | h2 << 8 | h3 << 4 | h4;
  157. if((codepoint2 & 0xfc00) != 0xdc00) {
  158. NX_JSON_REPORT_ERROR("invalid unicode surrogate", ps);
  159. return 0;
  160. }
  161. codepoint = 0x10000 + ((codepoint - 0xd800) << 10) + (codepoint2 - 0xdc00);
  162. }
  163. if(!encoder(codepoint, d, &d)) {
  164. NX_JSON_REPORT_ERROR("invalid codepoint", ps);
  165. return 0;
  166. }
  167. p += 5;
  168. break;
  169. default:
  170. // leave untouched
  171. *d++ = c;
  172. break;
  173. }
  174. } else {
  175. *d++ = c;
  176. }
  177. }
  178. NX_JSON_REPORT_ERROR("no closing quote for string", s);
  179. return 0;
  180. }
  181. static char* skip_block_comment(char* p) {
  182. // assume p[-2]=='/' && p[-1]=='*'
  183. char* ps = p - 2;
  184. if(!*p) {
  185. NX_JSON_REPORT_ERROR("endless comment", ps);
  186. return 0;
  187. }
  188. REPEAT:
  189. p = strchr(p + 1, '/');
  190. if(!p) {
  191. NX_JSON_REPORT_ERROR("endless comment", ps);
  192. return 0;
  193. }
  194. if(p[-1] != '*') goto REPEAT;
  195. return p + 1;
  196. }
  197. static char* parse_key(const char** key, char* p, nx_json_unicode_encoder encoder) {
  198. // on '}' return with *p=='}'
  199. char c;
  200. while((c = *p++)) {
  201. if(c == '"') {
  202. *key = unescape_string(p, &p, encoder);
  203. if(!*key) return 0; // propagate error
  204. while(*p && IS_WHITESPACE(*p))
  205. p++;
  206. if(*p == ':') return p + 1;
  207. NX_JSON_REPORT_ERROR("unexpected chars", p);
  208. return 0;
  209. } else if(IS_WHITESPACE(c) || c == ',') {
  210. // continue
  211. } else if(c == '}') {
  212. return p - 1;
  213. } else if(c == '/') {
  214. if(*p == '/') { // line comment
  215. char* ps = p - 1;
  216. p = strchr(p + 1, '\n');
  217. if(!p) {
  218. NX_JSON_REPORT_ERROR("endless comment", ps);
  219. return 0; // error
  220. }
  221. p++;
  222. } else if(*p == '*') { // block comment
  223. p = skip_block_comment(p + 1);
  224. if(!p) return 0;
  225. } else {
  226. NX_JSON_REPORT_ERROR("unexpected chars", p - 1);
  227. return 0; // error
  228. }
  229. } else {
  230. NX_JSON_REPORT_ERROR("unexpected chars", p - 1);
  231. return 0; // error
  232. }
  233. }
  234. NX_JSON_REPORT_ERROR("unexpected chars", p - 1);
  235. return 0; // error
  236. }
  237. static char*
  238. parse_value(nx_json* parent, const char* key, char* p, nx_json_unicode_encoder encoder) {
  239. nx_json* js;
  240. while(1) {
  241. switch(*p) {
  242. case '\0':
  243. NX_JSON_REPORT_ERROR("unexpected end of text", p);
  244. return 0; // error
  245. case ' ':
  246. case '\t':
  247. case '\n':
  248. case '\r':
  249. case ',':
  250. // skip
  251. p++;
  252. break;
  253. case '{':
  254. js = create_json(NX_JSON_OBJECT, key, parent);
  255. p++;
  256. while(1) {
  257. const char* new_key = NULL;
  258. p = parse_key(&new_key, p, encoder);
  259. if(!p) return 0; // error
  260. if(*p == '}') return p + 1; // end of object
  261. p = parse_value(js, new_key, p, encoder);
  262. if(!p) return 0; // error
  263. }
  264. case '[':
  265. js = create_json(NX_JSON_ARRAY, key, parent);
  266. p++;
  267. while(1) {
  268. p = parse_value(js, 0, p, encoder);
  269. if(!p) return 0; // error
  270. if(*p == ']') return p + 1; // end of array
  271. }
  272. case ']':
  273. return p;
  274. case '"':
  275. p++;
  276. js = create_json(NX_JSON_STRING, key, parent);
  277. js->text_value = unescape_string(p, &p, encoder);
  278. if(!js->text_value) return 0; // propagate error
  279. return p;
  280. case '-':
  281. case '0':
  282. case '1':
  283. case '2':
  284. case '3':
  285. case '4':
  286. case '5':
  287. case '6':
  288. case '7':
  289. case '8':
  290. case '9': {
  291. js = create_json(NX_JSON_INTEGER, key, parent);
  292. char* pe;
  293. if(*p == '-') {
  294. js->num.s_value = (nxjson_s64)strtol(p, &pe, 0); // was strtoll
  295. } else {
  296. js->num.u_value = (nxjson_u64)strtoul(p, &pe, 0); // was stroull
  297. }
  298. if(pe == p || errno == ERANGE) {
  299. NX_JSON_REPORT_ERROR("invalid number", p);
  300. return 0; // error
  301. }
  302. if(*pe == '.' || *pe == 'e' || *pe == 'E') { // float value
  303. js->type = NX_JSON_float;
  304. js->num.dbl_value = strtod(p, &pe);
  305. if(pe == p || errno == ERANGE) {
  306. NX_JSON_REPORT_ERROR("invalid number", p);
  307. return 0; // error
  308. }
  309. } else {
  310. if(*p == '-') {
  311. js->num.dbl_value = js->num.s_value;
  312. } else {
  313. js->num.dbl_value = js->num.u_value;
  314. }
  315. }
  316. return pe;
  317. }
  318. case 't':
  319. if(!strncmp(p, "true", 4)) {
  320. js = create_json(NX_JSON_BOOL, key, parent);
  321. js->num.u_value = 1;
  322. return p + 4;
  323. }
  324. NX_JSON_REPORT_ERROR("unexpected chars", p);
  325. return 0; // error
  326. case 'f':
  327. if(!strncmp(p, "false", 5)) {
  328. js = create_json(NX_JSON_BOOL, key, parent);
  329. js->num.u_value = 0;
  330. return p + 5;
  331. }
  332. NX_JSON_REPORT_ERROR("unexpected chars", p);
  333. return 0; // error
  334. case 'n':
  335. if(!strncmp(p, "null", 4)) {
  336. create_json(NX_JSON_NULL, key, parent);
  337. return p + 4;
  338. }
  339. NX_JSON_REPORT_ERROR("unexpected chars", p);
  340. return 0; // error
  341. case '/': // comment
  342. if(p[1] == '/') { // line comment
  343. char* ps = p;
  344. p = strchr(p + 2, '\n');
  345. if(!p) {
  346. NX_JSON_REPORT_ERROR("endless comment", ps);
  347. return 0; // error
  348. }
  349. p++;
  350. } else if(p[1] == '*') { // block comment
  351. p = skip_block_comment(p + 2);
  352. if(!p) return 0;
  353. } else {
  354. NX_JSON_REPORT_ERROR("unexpected chars", p);
  355. return 0; // error
  356. }
  357. break;
  358. default:
  359. NX_JSON_REPORT_ERROR("unexpected chars", p);
  360. return 0; // error
  361. }
  362. }
  363. }
  364. const nx_json* nx_json_parse_utf8(char* text) {
  365. return nx_json_parse(text, unicode_to_utf8);
  366. }
  367. const nx_json* nx_json_parse(char* text, nx_json_unicode_encoder encoder) {
  368. nx_json js = {0};
  369. if(!parse_value(&js, 0, text, encoder)) {
  370. if(js.children.first) nx_json_free(js.children.first);
  371. return 0;
  372. }
  373. return js.children.first;
  374. }
  375. const nx_json* nx_json_get(const nx_json* json, const char* key) {
  376. nx_json* js;
  377. for(js = json->children.first; js; js = js->next) {
  378. if(js->key && !strcmp(js->key, key)) return js;
  379. }
  380. return NULL;
  381. }
  382. const nx_json* nx_json_item(const nx_json* json, int idx) {
  383. nx_json* js;
  384. for(js = json->children.first; js; js = js->next) {
  385. if(!idx--) return js;
  386. }
  387. return NULL;
  388. }
  389. #ifdef __cplusplus
  390. }
  391. #endif
  392. #endif /* NXJSON_C */