jsmn.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * Copyright (c) 2010 Serge A. Zaitsev
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. *
  22. * Slightly modified by AK to not assume 0 terminated input.
  23. */
  24. #include <stdlib.h>
  25. #include "jsmn.h"
  26. /*
  27. * Allocates a fresh unused token from the token pool.
  28. */
  29. static jsmntok_t *jsmn_alloc_token(jsmn_parser *parser,
  30. jsmntok_t *tokens, size_t num_tokens)
  31. {
  32. jsmntok_t *tok;
  33. if ((unsigned)parser->toknext >= num_tokens)
  34. return NULL;
  35. tok = &tokens[parser->toknext++];
  36. tok->start = tok->end = -1;
  37. tok->size = 0;
  38. return tok;
  39. }
  40. /*
  41. * Fills token type and boundaries.
  42. */
  43. static void jsmn_fill_token(jsmntok_t *token, jsmntype_t type,
  44. int start, int end)
  45. {
  46. token->type = type;
  47. token->start = start;
  48. token->end = end;
  49. token->size = 0;
  50. }
  51. /*
  52. * Fills next available token with JSON primitive.
  53. */
  54. static jsmnerr_t jsmn_parse_primitive(jsmn_parser *parser, const char *js,
  55. size_t len,
  56. jsmntok_t *tokens, size_t num_tokens)
  57. {
  58. jsmntok_t *token;
  59. int start;
  60. start = parser->pos;
  61. for (; parser->pos < len; parser->pos++) {
  62. switch (js[parser->pos]) {
  63. #ifndef JSMN_STRICT
  64. /*
  65. * In strict mode primitive must be followed by ","
  66. * or "}" or "]"
  67. */
  68. case ':':
  69. #endif
  70. case '\t':
  71. case '\r':
  72. case '\n':
  73. case ' ':
  74. case ',':
  75. case ']':
  76. case '}':
  77. goto found;
  78. default:
  79. break;
  80. }
  81. if (js[parser->pos] < 32 || js[parser->pos] >= 127) {
  82. parser->pos = start;
  83. return JSMN_ERROR_INVAL;
  84. }
  85. }
  86. #ifdef JSMN_STRICT
  87. /*
  88. * In strict mode primitive must be followed by a
  89. * comma/object/array.
  90. */
  91. parser->pos = start;
  92. return JSMN_ERROR_PART;
  93. #endif
  94. found:
  95. token = jsmn_alloc_token(parser, tokens, num_tokens);
  96. if (token == NULL) {
  97. parser->pos = start;
  98. return JSMN_ERROR_NOMEM;
  99. }
  100. jsmn_fill_token(token, JSMN_PRIMITIVE, start, parser->pos);
  101. parser->pos--; /* parent sees closing brackets */
  102. return JSMN_SUCCESS;
  103. }
  104. /*
  105. * Fills next token with JSON string.
  106. */
  107. static jsmnerr_t jsmn_parse_string(jsmn_parser *parser, const char *js,
  108. size_t len,
  109. jsmntok_t *tokens, size_t num_tokens)
  110. {
  111. jsmntok_t *token;
  112. int start = parser->pos;
  113. /* Skip starting quote */
  114. parser->pos++;
  115. for (; parser->pos < len; parser->pos++) {
  116. char c = js[parser->pos];
  117. /* Quote: end of string */
  118. if (c == '\"') {
  119. token = jsmn_alloc_token(parser, tokens, num_tokens);
  120. if (token == NULL) {
  121. parser->pos = start;
  122. return JSMN_ERROR_NOMEM;
  123. }
  124. jsmn_fill_token(token, JSMN_STRING, start+1,
  125. parser->pos);
  126. return JSMN_SUCCESS;
  127. }
  128. /* Backslash: Quoted symbol expected */
  129. if (c == '\\') {
  130. parser->pos++;
  131. switch (js[parser->pos]) {
  132. /* Allowed escaped symbols */
  133. case '\"':
  134. case '/':
  135. case '\\':
  136. case 'b':
  137. case 'f':
  138. case 'r':
  139. case 'n':
  140. case 't':
  141. break;
  142. /* Allows escaped symbol \uXXXX */
  143. case 'u':
  144. /* TODO */
  145. break;
  146. /* Unexpected symbol */
  147. default:
  148. parser->pos = start;
  149. return JSMN_ERROR_INVAL;
  150. }
  151. }
  152. }
  153. parser->pos = start;
  154. return JSMN_ERROR_PART;
  155. }
  156. /*
  157. * Parse JSON string and fill tokens.
  158. */
  159. jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, size_t len,
  160. jsmntok_t *tokens, unsigned int num_tokens)
  161. {
  162. jsmnerr_t r;
  163. int i;
  164. jsmntok_t *token;
  165. for (; parser->pos < len; parser->pos++) {
  166. char c;
  167. jsmntype_t type;
  168. c = js[parser->pos];
  169. switch (c) {
  170. case '{':
  171. case '[':
  172. token = jsmn_alloc_token(parser, tokens, num_tokens);
  173. if (token == NULL)
  174. return JSMN_ERROR_NOMEM;
  175. if (parser->toksuper != -1)
  176. tokens[parser->toksuper].size++;
  177. token->type = (c == '{' ? JSMN_OBJECT : JSMN_ARRAY);
  178. token->start = parser->pos;
  179. parser->toksuper = parser->toknext - 1;
  180. break;
  181. case '}':
  182. case ']':
  183. type = (c == '}' ? JSMN_OBJECT : JSMN_ARRAY);
  184. for (i = parser->toknext - 1; i >= 0; i--) {
  185. token = &tokens[i];
  186. if (token->start != -1 && token->end == -1) {
  187. if (token->type != type)
  188. return JSMN_ERROR_INVAL;
  189. parser->toksuper = -1;
  190. token->end = parser->pos + 1;
  191. break;
  192. }
  193. }
  194. /* Error if unmatched closing bracket */
  195. if (i == -1)
  196. return JSMN_ERROR_INVAL;
  197. for (; i >= 0; i--) {
  198. token = &tokens[i];
  199. if (token->start != -1 && token->end == -1) {
  200. parser->toksuper = i;
  201. break;
  202. }
  203. }
  204. break;
  205. case '\"':
  206. r = jsmn_parse_string(parser, js, len, tokens,
  207. num_tokens);
  208. if (r < 0)
  209. return r;
  210. if (parser->toksuper != -1)
  211. tokens[parser->toksuper].size++;
  212. break;
  213. case '\t':
  214. case '\r':
  215. case '\n':
  216. case ':':
  217. case ',':
  218. case ' ':
  219. break;
  220. #ifdef JSMN_STRICT
  221. /*
  222. * In strict mode primitives are:
  223. * numbers and booleans.
  224. */
  225. case '-':
  226. case '0':
  227. case '1':
  228. case '2':
  229. case '3':
  230. case '4':
  231. case '5':
  232. case '6':
  233. case '7':
  234. case '8':
  235. case '9':
  236. case 't':
  237. case 'f':
  238. case 'n':
  239. #else
  240. /*
  241. * In non-strict mode every unquoted value
  242. * is a primitive.
  243. */
  244. /*FALL THROUGH */
  245. default:
  246. #endif
  247. r = jsmn_parse_primitive(parser, js, len, tokens,
  248. num_tokens);
  249. if (r < 0)
  250. return r;
  251. if (parser->toksuper != -1)
  252. tokens[parser->toksuper].size++;
  253. break;
  254. #ifdef JSMN_STRICT
  255. /* Unexpected char in strict mode */
  256. default:
  257. return JSMN_ERROR_INVAL;
  258. #endif
  259. }
  260. }
  261. for (i = parser->toknext - 1; i >= 0; i--) {
  262. /* Unmatched opened object or array */
  263. if (tokens[i].start != -1 && tokens[i].end == -1)
  264. return JSMN_ERROR_PART;
  265. }
  266. return JSMN_SUCCESS;
  267. }
  268. /*
  269. * Creates a new parser based over a given buffer with an array of tokens
  270. * available.
  271. */
  272. void jsmn_init(jsmn_parser *parser)
  273. {
  274. parser->pos = 0;
  275. parser->toknext = 0;
  276. parser->toksuper = -1;
  277. }
  278. const char *jsmn_strerror(jsmnerr_t err)
  279. {
  280. switch (err) {
  281. case JSMN_ERROR_NOMEM:
  282. return "No enough tokens";
  283. case JSMN_ERROR_INVAL:
  284. return "Invalid character inside JSON string";
  285. case JSMN_ERROR_PART:
  286. return "The string is not a full JSON packet, more bytes expected";
  287. case JSMN_SUCCESS:
  288. return "Success";
  289. default:
  290. return "Unknown json error";
  291. }
  292. }