Amazon Kinesis Webrtc C SDK
jsmn.h
Go to the documentation of this file.
1 /*
2  * MIT License
3  *
4  * Copyright (c) 2010 Serge Zaitsev
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #ifndef JSMN_H
25 #define JSMN_H
26 
27 #include <stddef.h>
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 #ifdef JSMN_STATIC
34 #define JSMN_API static
35 #else
36 #define JSMN_API extern
37 #endif
38 
46 typedef enum { JSMN_UNDEFINED = 0, JSMN_OBJECT = 1, JSMN_ARRAY = 2, JSMN_STRING = 3, JSMN_PRIMITIVE = 4 } jsmntype_t;
47 
48 enum jsmnerr {
49  /* Not enough tokens were provided */
51  /* Invalid character inside JSON string */
53  /* The string is not a full JSON packet, more bytes expected */
54  JSMN_ERROR_PART = -3
55 };
56 
63 typedef struct {
65  int start;
66  int end;
67  int size;
68 #ifdef JSMN_PARENT_LINKS
69  int parent;
70 #endif
71 } jsmntok_t;
72 
77 typedef struct {
78  unsigned int pos; /* offset in the JSON string */
79  unsigned int toknext; /* next token to allocate */
80  int toksuper; /* superior token node, e.g. parent object or array */
81 } jsmn_parser;
82 
86 JSMN_API void jsmn_init(jsmn_parser* parser);
87 
93 JSMN_API int jsmn_parse(jsmn_parser* parser, const char* js, const size_t len, jsmntok_t* tokens, const unsigned int num_tokens);
94 
95 #ifndef JSMN_HEADER
99 static jsmntok_t* jsmn_alloc_token(jsmn_parser* parser, jsmntok_t* tokens, const size_t num_tokens)
100 {
101  jsmntok_t* tok;
102  if (parser->toknext >= num_tokens) {
103  return NULL;
104  }
105  tok = &tokens[parser->toknext++];
106  tok->start = tok->end = -1;
107  tok->size = 0;
108 #ifdef JSMN_PARENT_LINKS
109  tok->parent = -1;
110 #endif
111  return tok;
112 }
113 
117 static void jsmn_fill_token(jsmntok_t* token, const jsmntype_t type, const int start, const int end)
118 {
119  token->type = type;
120  token->start = start;
121  token->end = end;
122  token->size = 0;
123 }
124 
128 static int jsmn_parse_primitive(jsmn_parser* parser, const char* js, const size_t len, jsmntok_t* tokens, const size_t num_tokens)
129 {
130  jsmntok_t* token;
131  int start;
132 
133  start = parser->pos;
134 
135  for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
136  switch (js[parser->pos]) {
137 #ifndef JSMN_STRICT
138  /* In strict mode primitive must be followed by "," or "}" or "]" */
139  case ':':
140 #endif
141  case '\t':
142  case '\r':
143  case '\n':
144  case ' ':
145  case ',':
146  case ']':
147  case '}':
148  goto found;
149  }
150  if (js[parser->pos] < 32 || js[parser->pos] >= 127) {
151  parser->pos = start;
152  return JSMN_ERROR_INVAL;
153  }
154  }
155 #ifdef JSMN_STRICT
156  /* In strict mode primitive must be followed by a comma/object/array */
157  parser->pos = start;
158  return JSMN_ERROR_PART;
159 #endif
160 
161 found:
162  if (tokens == NULL) {
163  parser->pos--;
164  return 0;
165  }
166  token = jsmn_alloc_token(parser, tokens, num_tokens);
167  if (token == NULL) {
168  parser->pos = start;
169  return JSMN_ERROR_NOMEM;
170  }
171  jsmn_fill_token(token, JSMN_PRIMITIVE, start, parser->pos);
172 #ifdef JSMN_PARENT_LINKS
173  token->parent = parser->toksuper;
174 #endif
175  parser->pos--;
176  return 0;
177 }
178 
182 static int jsmn_parse_string(jsmn_parser* parser, const char* js, const size_t len, jsmntok_t* tokens, const size_t num_tokens)
183 {
184  jsmntok_t* token;
185 
186  int start = parser->pos;
187 
188  parser->pos++;
189 
190  /* Skip starting quote */
191  for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
192  char c = js[parser->pos];
193 
194  /* Quote: end of string */
195  if (c == '\"') {
196  if (tokens == NULL) {
197  return 0;
198  }
199  token = jsmn_alloc_token(parser, tokens, num_tokens);
200  if (token == NULL) {
201  parser->pos = start;
202  return JSMN_ERROR_NOMEM;
203  }
204  jsmn_fill_token(token, JSMN_STRING, start + 1, parser->pos);
205 #ifdef JSMN_PARENT_LINKS
206  token->parent = parser->toksuper;
207 #endif
208  return 0;
209  }
210 
211  /* Backslash: Quoted symbol expected */
212  if (c == '\\' && parser->pos + 1 < len) {
213  int i;
214  parser->pos++;
215  switch (js[parser->pos]) {
216  /* Allowed escaped symbols */
217  case '\"':
218  case '/':
219  case '\\':
220  case 'b':
221  case 'f':
222  case 'r':
223  case 'n':
224  case 't':
225  break;
226  /* Allows escaped symbol \uXXXX */
227  case 'u':
228  parser->pos++;
229  for (i = 0; i < 4 && parser->pos < len && js[parser->pos] != '\0'; i++) {
230  /* If it isn't a hex character we have an error */
231  if (!((js[parser->pos] >= 48 && js[parser->pos] <= 57) || /* 0-9 */
232  (js[parser->pos] >= 65 && js[parser->pos] <= 70) || /* A-F */
233  (js[parser->pos] >= 97 && js[parser->pos] <= 102))) { /* a-f */
234  parser->pos = start;
235  return JSMN_ERROR_INVAL;
236  }
237  parser->pos++;
238  }
239  parser->pos--;
240  break;
241  /* Unexpected symbol */
242  default:
243  parser->pos = start;
244  return JSMN_ERROR_INVAL;
245  }
246  }
247  }
248  parser->pos = start;
249  return JSMN_ERROR_PART;
250 }
251 
255 JSMN_API int jsmn_parse(jsmn_parser* parser, const char* js, const size_t len, jsmntok_t* tokens, const unsigned int num_tokens)
256 {
257  int r;
258  int i;
259  jsmntok_t* token;
260  int count = parser->toknext;
261 
262  for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
263  char c;
264  jsmntype_t type;
265 
266  c = js[parser->pos];
267  switch (c) {
268  case '{':
269  case '[':
270  count++;
271  if (tokens == NULL) {
272  break;
273  }
274  token = jsmn_alloc_token(parser, tokens, num_tokens);
275  if (token == NULL) {
276  return JSMN_ERROR_NOMEM;
277  }
278  if (parser->toksuper != -1) {
279  jsmntok_t* t = &tokens[parser->toksuper];
280 #ifdef JSMN_STRICT
281  /* In strict mode an object or array can't become a key */
282  if (t->type == JSMN_OBJECT) {
283  return JSMN_ERROR_INVAL;
284  }
285 #endif
286  t->size++;
287 #ifdef JSMN_PARENT_LINKS
288  token->parent = parser->toksuper;
289 #endif
290  }
291  token->type = (c == '{' ? JSMN_OBJECT : JSMN_ARRAY);
292  token->start = parser->pos;
293  parser->toksuper = parser->toknext - 1;
294  break;
295  case '}':
296  case ']':
297  if (tokens == NULL) {
298  break;
299  }
300  type = (c == '}' ? JSMN_OBJECT : JSMN_ARRAY);
301 #ifdef JSMN_PARENT_LINKS
302  if (parser->toknext < 1) {
303  return JSMN_ERROR_INVAL;
304  }
305  token = &tokens[parser->toknext - 1];
306  for (;;) {
307  if (token->start != -1 && token->end == -1) {
308  if (token->type != type) {
309  return JSMN_ERROR_INVAL;
310  }
311  token->end = parser->pos + 1;
312  parser->toksuper = token->parent;
313  break;
314  }
315  if (token->parent == -1) {
316  if (token->type != type || parser->toksuper == -1) {
317  return JSMN_ERROR_INVAL;
318  }
319  break;
320  }
321  token = &tokens[token->parent];
322  }
323 #else
324  for (i = parser->toknext - 1; i >= 0; i--) {
325  token = &tokens[i];
326  if (token->start != -1 && token->end == -1) {
327  if (token->type != type) {
328  return JSMN_ERROR_INVAL;
329  }
330  parser->toksuper = -1;
331  token->end = parser->pos + 1;
332  break;
333  }
334  }
335  /* Error if unmatched closing bracket */
336  if (i == -1) {
337  return JSMN_ERROR_INVAL;
338  }
339  for (; i >= 0; i--) {
340  token = &tokens[i];
341  if (token->start != -1 && token->end == -1) {
342  parser->toksuper = i;
343  break;
344  }
345  }
346 #endif
347  break;
348  case '\"':
349  r = jsmn_parse_string(parser, js, len, tokens, num_tokens);
350  if (r < 0) {
351  return r;
352  }
353  count++;
354  if (parser->toksuper != -1 && tokens != NULL) {
355  tokens[parser->toksuper].size++;
356  }
357  break;
358  case '\t':
359  case '\r':
360  case '\n':
361  case ' ':
362  break;
363  case ':':
364  parser->toksuper = parser->toknext - 1;
365  break;
366  case ',':
367  if (tokens != NULL && parser->toksuper != -1 && tokens[parser->toksuper].type != JSMN_ARRAY &&
368  tokens[parser->toksuper].type != JSMN_OBJECT) {
369 #ifdef JSMN_PARENT_LINKS
370  parser->toksuper = tokens[parser->toksuper].parent;
371 #else
372  for (i = parser->toknext - 1; i >= 0; i--) {
373  if (tokens[i].type == JSMN_ARRAY || tokens[i].type == JSMN_OBJECT) {
374  if (tokens[i].start != -1 && tokens[i].end == -1) {
375  parser->toksuper = i;
376  break;
377  }
378  }
379  }
380 #endif
381  }
382  break;
383 #ifdef JSMN_STRICT
384  /* In strict mode primitives are: numbers and booleans */
385  case '-':
386  case '0':
387  case '1':
388  case '2':
389  case '3':
390  case '4':
391  case '5':
392  case '6':
393  case '7':
394  case '8':
395  case '9':
396  case 't':
397  case 'f':
398  case 'n':
399  /* And they must not be keys of the object */
400  if (tokens != NULL && parser->toksuper != -1) {
401  const jsmntok_t* t = &tokens[parser->toksuper];
402  if (t->type == JSMN_OBJECT || (t->type == JSMN_STRING && t->size != 0)) {
403  return JSMN_ERROR_INVAL;
404  }
405  }
406 #else
407  /* In non-strict mode every unquoted value is a primitive */
408  default:
409 #endif
410  r = jsmn_parse_primitive(parser, js, len, tokens, num_tokens);
411  if (r < 0) {
412  return r;
413  }
414  count++;
415  if (parser->toksuper != -1 && tokens != NULL) {
416  tokens[parser->toksuper].size++;
417  }
418  break;
419 
420 #ifdef JSMN_STRICT
421  /* Unexpected char in strict mode */
422  default:
423  return JSMN_ERROR_INVAL;
424 #endif
425  }
426  }
427 
428  if (tokens != NULL) {
429  for (i = parser->toknext - 1; i >= 0; i--) {
430  /* Unmatched opened object or array */
431  if (tokens[i].start != -1 && tokens[i].end == -1) {
432  return JSMN_ERROR_PART;
433  }
434  }
435  }
436 
437  return count;
438 }
439 
445 {
446  parser->pos = 0;
447  parser->toknext = 0;
448  parser->toksuper = -1;
449 }
450 
451 #endif /* JSMN_HEADER */
452 
453 #ifdef __cplusplus
454 }
455 #endif
456 
457 #endif /* JSMN_H */
jsmntype_t
Definition: jsmn.h:46
@ JSMN_PRIMITIVE
Definition: jsmn.h:46
@ JSMN_OBJECT
Definition: jsmn.h:46
@ JSMN_UNDEFINED
Definition: jsmn.h:46
@ JSMN_ARRAY
Definition: jsmn.h:46
@ JSMN_STRING
Definition: jsmn.h:46
void jsmn_init(jsmn_parser *parser)
Definition: jsmn.h:444
#define JSMN_API
Definition: jsmn.h:36
int jsmn_parse(jsmn_parser *parser, const char *js, const size_t len, jsmntok_t *tokens, const unsigned int num_tokens)
Definition: jsmn.h:255
jsmnerr
Definition: jsmn.h:48
@ JSMN_ERROR_INVAL
Definition: jsmn.h:52
@ JSMN_ERROR_PART
Definition: jsmn.h:54
@ JSMN_ERROR_NOMEM
Definition: jsmn.h:50
Definition: jsmn.h:77
unsigned int pos
Definition: jsmn.h:78
int toksuper
Definition: jsmn.h:80
unsigned int toknext
Definition: jsmn.h:79
Definition: jsmn.h:63
int start
Definition: jsmn.h:65
int size
Definition: jsmn.h:67
int end
Definition: jsmn.h:66
jsmntype_t type
Definition: jsmn.h:64