/src/fluent-bit/lib/jsmn/jsmn.h
Line | Count | Source (jump to first uncovered line) |
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 | | |
39 | | /** |
40 | | * JSON type identifier. Basic types are: |
41 | | * o Object |
42 | | * o Array |
43 | | * o String |
44 | | * o Other primitive: number, boolean (true/false) or null |
45 | | */ |
46 | | typedef enum { |
47 | | JSMN_UNDEFINED = 0, |
48 | | JSMN_OBJECT = 1, |
49 | | JSMN_ARRAY = 2, |
50 | | JSMN_STRING = 3, |
51 | | JSMN_PRIMITIVE = 4 |
52 | | } jsmntype_t; |
53 | | |
54 | | enum jsmnerr { |
55 | | /* Not enough tokens were provided */ |
56 | | JSMN_ERROR_NOMEM = -1, |
57 | | /* Invalid character inside JSON string */ |
58 | | JSMN_ERROR_INVAL = -2, |
59 | | /* The string is not a full JSON packet, more bytes expected */ |
60 | | JSMN_ERROR_PART = -3 |
61 | | }; |
62 | | |
63 | | /** |
64 | | * JSON token description. |
65 | | * type type (object, array, string etc.) |
66 | | * start start position in JSON data string |
67 | | * end end position in JSON data string |
68 | | */ |
69 | | typedef struct jsmntok { |
70 | | jsmntype_t type; |
71 | | int start; |
72 | | int end; |
73 | | int size; |
74 | | #ifdef JSMN_PARENT_LINKS |
75 | | int parent; |
76 | | #endif |
77 | | } jsmntok_t; |
78 | | |
79 | | /** |
80 | | * JSON parser. Contains an array of token blocks available. Also stores |
81 | | * the string being parsed now and current position in that string. |
82 | | */ |
83 | | typedef struct jsmn_parser { |
84 | | unsigned int pos; /* offset in the JSON string */ |
85 | | unsigned int toknext; /* next token to allocate */ |
86 | | int toksuper; /* superior token node, e.g. parent object or array */ |
87 | | } jsmn_parser; |
88 | | |
89 | | /** |
90 | | * Create JSON parser over an array of tokens |
91 | | */ |
92 | | JSMN_API void jsmn_init(jsmn_parser *parser); |
93 | | |
94 | | /** |
95 | | * Run JSON parser. It parses a JSON data string into and array of tokens, each |
96 | | * describing |
97 | | * a single JSON object. |
98 | | */ |
99 | | JSMN_API int jsmn_parse(jsmn_parser *parser, const char *js, const size_t len, |
100 | | jsmntok_t *tokens, const unsigned int num_tokens); |
101 | | |
102 | | #ifndef JSMN_HEADER |
103 | | /** |
104 | | * Allocates a fresh unused token from the token pool. |
105 | | */ |
106 | | static jsmntok_t *jsmn_alloc_token(jsmn_parser *parser, jsmntok_t *tokens, |
107 | 2.06M | const size_t num_tokens) { |
108 | 2.06M | jsmntok_t *tok; |
109 | 2.06M | if (parser->toknext >= num_tokens) { |
110 | 7.12k | return NULL; |
111 | 7.12k | } |
112 | 2.05M | tok = &tokens[parser->toknext++]; |
113 | 2.05M | tok->start = tok->end = -1; |
114 | 2.05M | tok->size = 0; |
115 | 2.05M | #ifdef JSMN_PARENT_LINKS |
116 | 2.05M | tok->parent = -1; |
117 | 2.05M | #endif |
118 | 2.05M | return tok; |
119 | 2.06M | } |
120 | | |
121 | | /** |
122 | | * Fills token type and boundaries. |
123 | | */ |
124 | | static void jsmn_fill_token(jsmntok_t *token, const jsmntype_t type, |
125 | 1.62M | const int start, const int end) { |
126 | 1.62M | token->type = type; |
127 | 1.62M | token->start = start; |
128 | 1.62M | token->end = end; |
129 | 1.62M | token->size = 0; |
130 | 1.62M | } |
131 | | |
132 | | /** |
133 | | * Fills next available token with JSON primitive. |
134 | | */ |
135 | | static int jsmn_parse_primitive(jsmn_parser *parser, const char *js, |
136 | | const size_t len, jsmntok_t *tokens, |
137 | 1.40M | const size_t num_tokens) { |
138 | 1.40M | jsmntok_t *token; |
139 | 1.40M | int start; |
140 | | |
141 | 1.40M | start = parser->pos; |
142 | | |
143 | 4.08M | for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) { |
144 | 4.08M | switch (js[parser->pos]) { |
145 | | #ifndef JSMN_STRICT |
146 | | /* In strict mode primitive must be followed by "," or "}" or "]" */ |
147 | | case ':': |
148 | | #endif |
149 | 36.2k | case '\t': |
150 | 65.1k | case '\r': |
151 | 1.14M | case '\n': |
152 | 1.25M | case ' ': |
153 | 1.35M | case ',': |
154 | 1.39M | case ']': |
155 | 1.40M | case '}': |
156 | 1.40M | goto found; |
157 | 2.68M | default: |
158 | | /* to quiet a warning from gcc*/ |
159 | 2.68M | break; |
160 | 4.08M | } |
161 | 2.68M | if (js[parser->pos] < 32 || js[parser->pos] >= 127) { |
162 | 453 | parser->pos = start; |
163 | 453 | return JSMN_ERROR_INVAL; |
164 | 453 | } |
165 | 2.68M | } |
166 | 282 | #ifdef JSMN_STRICT |
167 | | /* In strict mode primitive must be followed by a comma/object/array */ |
168 | 282 | parser->pos = start; |
169 | 282 | return JSMN_ERROR_PART; |
170 | 0 | #endif |
171 | | |
172 | 1.40M | found: |
173 | 1.40M | if (tokens == NULL) { |
174 | 0 | parser->pos--; |
175 | 0 | return 0; |
176 | 0 | } |
177 | 1.40M | token = jsmn_alloc_token(parser, tokens, num_tokens); |
178 | 1.40M | if (token == NULL) { |
179 | 5.19k | parser->pos = start; |
180 | 5.19k | return JSMN_ERROR_NOMEM; |
181 | 5.19k | } |
182 | 1.39M | jsmn_fill_token(token, JSMN_PRIMITIVE, start, parser->pos); |
183 | 1.39M | #ifdef JSMN_PARENT_LINKS |
184 | 1.39M | token->parent = parser->toksuper; |
185 | 1.39M | #endif |
186 | 1.39M | parser->pos--; |
187 | 1.39M | return 0; |
188 | 1.40M | } |
189 | | |
190 | | /** |
191 | | * Fills next token with JSON string. |
192 | | */ |
193 | | static int jsmn_parse_string(jsmn_parser *parser, const char *js, |
194 | | const size_t len, jsmntok_t *tokens, |
195 | 231k | const size_t num_tokens) { |
196 | 231k | jsmntok_t *token; |
197 | | |
198 | 231k | int start = parser->pos; |
199 | | |
200 | 231k | parser->pos++; |
201 | | |
202 | | /* Skip starting quote */ |
203 | 1.43M | for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) { |
204 | 1.43M | char c = js[parser->pos]; |
205 | | |
206 | | /* Quote: end of string */ |
207 | 1.43M | if (c == '\"') { |
208 | 226k | if (tokens == NULL) { |
209 | 0 | return 0; |
210 | 0 | } |
211 | 226k | token = jsmn_alloc_token(parser, tokens, num_tokens); |
212 | 226k | if (token == NULL) { |
213 | 947 | parser->pos = start; |
214 | 947 | return JSMN_ERROR_NOMEM; |
215 | 947 | } |
216 | 225k | jsmn_fill_token(token, JSMN_STRING, start + 1, parser->pos); |
217 | 225k | #ifdef JSMN_PARENT_LINKS |
218 | 225k | token->parent = parser->toksuper; |
219 | 225k | #endif |
220 | 225k | return 0; |
221 | 226k | } |
222 | | |
223 | | /* Backslash: Quoted symbol expected */ |
224 | 1.20M | if (c == '\\' && parser->pos + 1 < len) { |
225 | 11.6k | int i; |
226 | 11.6k | parser->pos++; |
227 | 11.6k | switch (js[parser->pos]) { |
228 | | /* Allowed escaped symbols */ |
229 | 605 | case '\"': |
230 | 826 | case '/': |
231 | 1.88k | case '\\': |
232 | 2.37k | case 'b': |
233 | 2.61k | case 'f': |
234 | 5.17k | case 'r': |
235 | 5.56k | case 'n': |
236 | 5.81k | case 't': |
237 | 5.81k | break; |
238 | | /* Allows escaped symbol \uXXXX */ |
239 | 5.58k | case 'u': |
240 | 5.58k | parser->pos++; |
241 | 8.91k | for (i = 0; i < 4 && parser->pos < len && js[parser->pos] != '\0'; |
242 | 5.58k | i++) { |
243 | | /* If it isn't a hex character we have an error */ |
244 | 3.77k | if (!((js[parser->pos] >= 48 && js[parser->pos] <= 57) || /* 0-9 */ |
245 | 3.77k | (js[parser->pos] >= 65 && js[parser->pos] <= 70) || /* A-F */ |
246 | 3.77k | (js[parser->pos] >= 97 && js[parser->pos] <= 102))) { /* a-f */ |
247 | 444 | parser->pos = start; |
248 | 444 | return JSMN_ERROR_INVAL; |
249 | 444 | } |
250 | 3.33k | parser->pos++; |
251 | 3.33k | } |
252 | 5.13k | parser->pos--; |
253 | 5.13k | break; |
254 | | /* Unexpected symbol */ |
255 | 255 | default: |
256 | 255 | parser->pos = start; |
257 | 255 | return JSMN_ERROR_INVAL; |
258 | 11.6k | } |
259 | 11.6k | } |
260 | 1.20M | } |
261 | 5.06k | parser->pos = start; |
262 | 5.06k | return JSMN_ERROR_PART; |
263 | 231k | } |
264 | | |
265 | | /** |
266 | | * Parse JSON string and fill tokens. |
267 | | */ |
268 | | JSMN_API int jsmn_parse(jsmn_parser *parser, const char *js, const size_t len, |
269 | 51.1k | jsmntok_t *tokens, const unsigned int num_tokens) { |
270 | 51.1k | int r; |
271 | 51.1k | int i; |
272 | 51.1k | jsmntok_t *token; |
273 | 51.1k | int count = parser->toknext; |
274 | | |
275 | 3.61M | for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) { |
276 | 3.57M | char c; |
277 | 3.57M | jsmntype_t type; |
278 | | |
279 | 3.57M | c = js[parser->pos]; |
280 | 3.57M | switch (c) { |
281 | 42.0k | case '{': |
282 | 435k | case '[': |
283 | 435k | count++; |
284 | 435k | if (tokens == NULL) { |
285 | 0 | break; |
286 | 0 | } |
287 | 435k | token = jsmn_alloc_token(parser, tokens, num_tokens); |
288 | 435k | if (token == NULL) { |
289 | 992 | return JSMN_ERROR_NOMEM; |
290 | 992 | } |
291 | 434k | if (parser->toksuper != -1) { |
292 | 388k | jsmntok_t *t = &tokens[parser->toksuper]; |
293 | 388k | #ifdef JSMN_STRICT |
294 | | /* In strict mode an object or array can't become a key */ |
295 | 388k | if (t->type == JSMN_OBJECT) { |
296 | 1.15k | return JSMN_ERROR_INVAL; |
297 | 1.15k | } |
298 | 387k | #endif |
299 | 387k | t->size++; |
300 | 387k | #ifdef JSMN_PARENT_LINKS |
301 | 387k | token->parent = parser->toksuper; |
302 | 387k | #endif |
303 | 387k | } |
304 | 432k | token->type = (c == '{' ? JSMN_OBJECT : JSMN_ARRAY); |
305 | 432k | token->start = parser->pos; |
306 | 432k | parser->toksuper = parser->toknext - 1; |
307 | 432k | break; |
308 | 27.7k | case '}': |
309 | 73.4k | case ']': |
310 | 73.4k | if (tokens == NULL) { |
311 | 0 | break; |
312 | 0 | } |
313 | 73.4k | type = (c == '}' ? JSMN_OBJECT : JSMN_ARRAY); |
314 | 73.4k | #ifdef JSMN_PARENT_LINKS |
315 | 73.4k | if (parser->toknext < 1) { |
316 | 1 | return JSMN_ERROR_INVAL; |
317 | 1 | } |
318 | 73.4k | token = &tokens[parser->toknext - 1]; |
319 | 343k | for (;;) { |
320 | 343k | if (token->start != -1 && token->end == -1) { |
321 | 62.5k | if (token->type != type) { |
322 | 312 | return JSMN_ERROR_INVAL; |
323 | 312 | } |
324 | 62.2k | token->end = parser->pos + 1; |
325 | 62.2k | parser->toksuper = token->parent; |
326 | 62.2k | break; |
327 | 62.5k | } |
328 | 281k | if (token->parent == -1) { |
329 | 10.9k | if (token->type != type || parser->toksuper == -1) { |
330 | 405 | return JSMN_ERROR_INVAL; |
331 | 405 | } |
332 | 10.5k | break; |
333 | 10.9k | } |
334 | 270k | token = &tokens[token->parent]; |
335 | 270k | } |
336 | | #else |
337 | | for (i = parser->toknext - 1; i >= 0; i--) { |
338 | | token = &tokens[i]; |
339 | | if (token->start != -1 && token->end == -1) { |
340 | | if (token->type != type) { |
341 | | return JSMN_ERROR_INVAL; |
342 | | } |
343 | | parser->toksuper = -1; |
344 | | token->end = parser->pos + 1; |
345 | | break; |
346 | | } |
347 | | } |
348 | | /* Error if unmatched closing bracket */ |
349 | | if (i == -1) { |
350 | | return JSMN_ERROR_INVAL; |
351 | | } |
352 | | for (; i >= 0; i--) { |
353 | | token = &tokens[i]; |
354 | | if (token->start != -1 && token->end == -1) { |
355 | | parser->toksuper = i; |
356 | | break; |
357 | | } |
358 | | } |
359 | | #endif |
360 | 72.7k | break; |
361 | 231k | case '\"': |
362 | 231k | r = jsmn_parse_string(parser, js, len, tokens, num_tokens); |
363 | 231k | if (r < 0) { |
364 | 6.70k | return r; |
365 | 6.70k | } |
366 | 225k | count++; |
367 | 225k | if (parser->toksuper != -1 && tokens != NULL) { |
368 | 224k | tokens[parser->toksuper].size++; |
369 | 224k | } |
370 | 225k | break; |
371 | 38.3k | case '\t': |
372 | 69.2k | case '\r': |
373 | 1.15M | case '\n': |
374 | 1.27M | case ' ': |
375 | 1.27M | break; |
376 | 51.3k | case ':': |
377 | 51.3k | parser->toksuper = parser->toknext - 1; |
378 | 51.3k | break; |
379 | 112k | case ',': |
380 | 112k | if (tokens != NULL && parser->toksuper != -1 && |
381 | 112k | tokens[parser->toksuper].type != JSMN_ARRAY && |
382 | 112k | tokens[parser->toksuper].type != JSMN_OBJECT) { |
383 | 42.7k | #ifdef JSMN_PARENT_LINKS |
384 | 42.7k | parser->toksuper = tokens[parser->toksuper].parent; |
385 | | #else |
386 | | for (i = parser->toknext - 1; i >= 0; i--) { |
387 | | if (tokens[i].type == JSMN_ARRAY || tokens[i].type == JSMN_OBJECT) { |
388 | | if (tokens[i].start != -1 && tokens[i].end == -1) { |
389 | | parser->toksuper = i; |
390 | | break; |
391 | | } |
392 | | } |
393 | | } |
394 | | #endif |
395 | 42.7k | } |
396 | 112k | break; |
397 | 0 | #ifdef JSMN_STRICT |
398 | | /* In strict mode primitives are: numbers and booleans */ |
399 | 81.9k | case '-': |
400 | 105k | case '0': |
401 | 140k | case '1': |
402 | 1.18M | case '2': |
403 | 1.18M | case '3': |
404 | 1.19M | case '4': |
405 | 1.20M | case '5': |
406 | 1.20M | case '6': |
407 | 1.22M | case '7': |
408 | 1.29M | case '8': |
409 | 1.29M | case '9': |
410 | 1.32M | case 't': |
411 | 1.37M | case 'f': |
412 | 1.40M | case 'n': |
413 | | /* And they must not be keys of the object */ |
414 | 1.40M | if (tokens != NULL && parser->toksuper != -1) { |
415 | 1.22M | const jsmntok_t *t = &tokens[parser->toksuper]; |
416 | 1.22M | if (t->type == JSMN_OBJECT || |
417 | 1.22M | (t->type == JSMN_STRING && t->size != 0)) { |
418 | 498 | return JSMN_ERROR_INVAL; |
419 | 498 | } |
420 | 1.22M | } |
421 | | #else |
422 | | /* In non-strict mode every unquoted value is a primitive */ |
423 | | default: |
424 | | #endif |
425 | 1.40M | r = jsmn_parse_primitive(parser, js, len, tokens, num_tokens); |
426 | 1.40M | if (r < 0) { |
427 | 5.92k | return r; |
428 | 5.92k | } |
429 | 1.39M | count++; |
430 | 1.39M | if (parser->toksuper != -1 && tokens != NULL) { |
431 | 1.22M | tokens[parser->toksuper].size++; |
432 | 1.22M | } |
433 | 1.39M | break; |
434 | | |
435 | 0 | #ifdef JSMN_STRICT |
436 | | /* Unexpected char in strict mode */ |
437 | 968 | default: |
438 | 968 | return JSMN_ERROR_INVAL; |
439 | 3.57M | #endif |
440 | 3.57M | } |
441 | 3.57M | } |
442 | | |
443 | 34.1k | if (tokens != NULL) { |
444 | 1.68M | for (i = parser->toknext - 1; i >= 0; i--) { |
445 | | /* Unmatched opened object or array */ |
446 | 1.67M | if (tokens[i].start != -1 && tokens[i].end == -1) { |
447 | 23.0k | return JSMN_ERROR_PART; |
448 | 23.0k | } |
449 | 1.67M | } |
450 | 34.1k | } |
451 | | |
452 | 11.1k | return count; |
453 | 34.1k | } |
454 | | |
455 | | /** |
456 | | * Creates a new parser based over a given buffer with an array of tokens |
457 | | * available. |
458 | | */ |
459 | 44.0k | JSMN_API void jsmn_init(jsmn_parser *parser) { |
460 | 44.0k | parser->pos = 0; |
461 | 44.0k | parser->toknext = 0; |
462 | 44.0k | parser->toksuper = -1; |
463 | 44.0k | } |
464 | | |
465 | | #endif /* JSMN_HEADER */ |
466 | | |
467 | | #ifdef __cplusplus |
468 | | } |
469 | | #endif |
470 | | |
471 | | #endif /* JSMN_H */ |