/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 | 41.5M | const size_t num_tokens) { |
108 | 41.5M | jsmntok_t *tok; |
109 | 41.5M | if (parser->toknext >= num_tokens) { |
110 | 145k | return NULL; |
111 | 145k | } |
112 | 41.4M | tok = &tokens[parser->toknext++]; |
113 | 41.4M | tok->start = tok->end = -1; |
114 | 41.4M | tok->size = 0; |
115 | 41.4M | #ifdef JSMN_PARENT_LINKS |
116 | 41.4M | tok->parent = -1; |
117 | 41.4M | #endif |
118 | 41.4M | return tok; |
119 | 41.5M | } |
120 | | |
121 | | /** |
122 | | * Fills token type and boundaries. |
123 | | */ |
124 | | static void jsmn_fill_token(jsmntok_t *token, const jsmntype_t type, |
125 | 31.1M | const int start, const int end) { |
126 | 31.1M | token->type = type; |
127 | 31.1M | token->start = start; |
128 | 31.1M | token->end = end; |
129 | 31.1M | token->size = 0; |
130 | 31.1M | } |
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 | 23.9M | const size_t num_tokens) { |
138 | 23.9M | jsmntok_t *token; |
139 | 23.9M | int start; |
140 | | |
141 | 23.9M | start = parser->pos; |
142 | | |
143 | 64.1M | for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) { |
144 | 64.1M | switch (js[parser->pos]) { |
145 | | #ifndef JSMN_STRICT |
146 | | /* In strict mode primitive must be followed by "," or "}" or "]" */ |
147 | | case ':': |
148 | | #endif |
149 | 706k | case '\t': |
150 | 10.9M | case '\r': |
151 | 15.3M | case '\n': |
152 | 15.9M | case ' ': |
153 | 22.1M | case ',': |
154 | 23.9M | case ']': |
155 | 23.9M | case '}': |
156 | 23.9M | goto found; |
157 | 40.1M | default: |
158 | | /* to quiet a warning from gcc*/ |
159 | 40.1M | break; |
160 | 64.1M | } |
161 | 40.1M | if (js[parser->pos] < 32 || js[parser->pos] >= 127) { |
162 | 7.99k | parser->pos = start; |
163 | 7.99k | return JSMN_ERROR_INVAL; |
164 | 7.99k | } |
165 | 40.1M | } |
166 | 6.39k | #ifdef JSMN_STRICT |
167 | | /* In strict mode primitive must be followed by a comma/object/array */ |
168 | 6.39k | parser->pos = start; |
169 | 6.39k | return JSMN_ERROR_PART; |
170 | 0 | #endif |
171 | | |
172 | 23.9M | found: |
173 | 23.9M | if (tokens == NULL) { |
174 | 0 | parser->pos--; |
175 | 0 | return 0; |
176 | 0 | } |
177 | 23.9M | token = jsmn_alloc_token(parser, tokens, num_tokens); |
178 | 23.9M | if (token == NULL) { |
179 | 86.3k | parser->pos = start; |
180 | 86.3k | return JSMN_ERROR_NOMEM; |
181 | 86.3k | } |
182 | 23.8M | jsmn_fill_token(token, JSMN_PRIMITIVE, start, parser->pos); |
183 | 23.8M | #ifdef JSMN_PARENT_LINKS |
184 | 23.8M | token->parent = parser->toksuper; |
185 | 23.8M | #endif |
186 | 23.8M | parser->pos--; |
187 | 23.8M | return 0; |
188 | 23.9M | } |
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 | 7.29M | const size_t num_tokens) { |
196 | 7.29M | jsmntok_t *token; |
197 | | |
198 | 7.29M | int start = parser->pos; |
199 | | |
200 | 7.29M | parser->pos++; |
201 | | |
202 | | /* Skip starting quote */ |
203 | 87.6M | for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) { |
204 | 87.6M | char c = js[parser->pos]; |
205 | | |
206 | | /* Quote: end of string */ |
207 | 87.6M | if (c == '\"') { |
208 | 7.29M | if (tokens == NULL) { |
209 | 0 | return 0; |
210 | 0 | } |
211 | 7.29M | token = jsmn_alloc_token(parser, tokens, num_tokens); |
212 | 7.29M | if (token == NULL) { |
213 | 22.9k | parser->pos = start; |
214 | 22.9k | return JSMN_ERROR_NOMEM; |
215 | 22.9k | } |
216 | 7.26M | jsmn_fill_token(token, JSMN_STRING, start + 1, parser->pos); |
217 | 7.26M | #ifdef JSMN_PARENT_LINKS |
218 | 7.26M | token->parent = parser->toksuper; |
219 | 7.26M | #endif |
220 | 7.26M | return 0; |
221 | 7.29M | } |
222 | | |
223 | | /* Backslash: Quoted symbol expected */ |
224 | 80.3M | if (c == '\\' && parser->pos + 1 < len) { |
225 | 499k | int i; |
226 | 499k | parser->pos++; |
227 | 499k | switch (js[parser->pos]) { |
228 | | /* Allowed escaped symbols */ |
229 | 187k | case '\"': |
230 | 189k | case '/': |
231 | 394k | case '\\': |
232 | 394k | case 'b': |
233 | 396k | case 'f': |
234 | 399k | case 'r': |
235 | 405k | case 'n': |
236 | 410k | case 't': |
237 | 410k | break; |
238 | | /* Allows escaped symbol \uXXXX */ |
239 | 86.5k | case 'u': |
240 | 86.5k | parser->pos++; |
241 | 428k | for (i = 0; i < 4 && parser->pos < len && js[parser->pos] != '\0'; |
242 | 342k | i++) { |
243 | | /* If it isn't a hex character we have an error */ |
244 | 342k | if (!((js[parser->pos] >= 48 && js[parser->pos] <= 57) || /* 0-9 */ |
245 | 342k | (js[parser->pos] >= 65 && js[parser->pos] <= 70) || /* A-F */ |
246 | 342k | (js[parser->pos] >= 97 && js[parser->pos] <= 102))) { /* a-f */ |
247 | 784 | parser->pos = start; |
248 | 784 | return JSMN_ERROR_INVAL; |
249 | 784 | } |
250 | 341k | parser->pos++; |
251 | 341k | } |
252 | 85.7k | parser->pos--; |
253 | 85.7k | break; |
254 | | /* Unexpected symbol */ |
255 | 1.83k | default: |
256 | 1.83k | parser->pos = start; |
257 | 1.83k | return JSMN_ERROR_INVAL; |
258 | 499k | } |
259 | 499k | } |
260 | 80.3M | } |
261 | 5.09k | parser->pos = start; |
262 | 5.09k | return JSMN_ERROR_PART; |
263 | 7.29M | } |
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 | 384k | jsmntok_t *tokens, const unsigned int num_tokens) { |
270 | 384k | int r; |
271 | 384k | int i; |
272 | 384k | jsmntok_t *token; |
273 | 384k | int count = parser->toknext; |
274 | | |
275 | 69.5M | for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) { |
276 | 69.4M | char c; |
277 | 69.4M | jsmntype_t type; |
278 | | |
279 | 69.4M | c = js[parser->pos]; |
280 | 69.4M | switch (c) { |
281 | 148k | case '{': |
282 | 10.2M | case '[': |
283 | 10.2M | count++; |
284 | 10.2M | if (tokens == NULL) { |
285 | 0 | break; |
286 | 0 | } |
287 | 10.2M | token = jsmn_alloc_token(parser, tokens, num_tokens); |
288 | 10.2M | if (token == NULL) { |
289 | 36.6k | return JSMN_ERROR_NOMEM; |
290 | 36.6k | } |
291 | 10.2M | if (parser->toksuper != -1) { |
292 | 10.1M | jsmntok_t *t = &tokens[parser->toksuper]; |
293 | 10.1M | #ifdef JSMN_STRICT |
294 | | /* In strict mode an object or array can't become a key */ |
295 | 10.1M | if (t->type == JSMN_OBJECT) { |
296 | 352 | return JSMN_ERROR_INVAL; |
297 | 352 | } |
298 | 10.1M | #endif |
299 | 10.1M | t->size++; |
300 | 10.1M | #ifdef JSMN_PARENT_LINKS |
301 | 10.1M | token->parent = parser->toksuper; |
302 | 10.1M | #endif |
303 | 10.1M | } |
304 | 10.2M | token->type = (c == '{' ? JSMN_OBJECT : JSMN_ARRAY); |
305 | 10.2M | token->start = parser->pos; |
306 | 10.2M | parser->toksuper = parser->toknext - 1; |
307 | 10.2M | break; |
308 | 158k | case '}': |
309 | 2.72M | case ']': |
310 | 2.72M | if (tokens == NULL) { |
311 | 0 | break; |
312 | 0 | } |
313 | 2.72M | type = (c == '}' ? JSMN_OBJECT : JSMN_ARRAY); |
314 | 2.72M | #ifdef JSMN_PARENT_LINKS |
315 | 2.72M | if (parser->toknext < 1) { |
316 | 924 | return JSMN_ERROR_INVAL; |
317 | 924 | } |
318 | 2.72M | token = &tokens[parser->toknext - 1]; |
319 | 116M | for (;;) { |
320 | 116M | if (token->start != -1 && token->end == -1) { |
321 | 2.58M | if (token->type != type) { |
322 | 548 | return JSMN_ERROR_INVAL; |
323 | 548 | } |
324 | 2.58M | token->end = parser->pos + 1; |
325 | 2.58M | parser->toksuper = token->parent; |
326 | 2.58M | break; |
327 | 2.58M | } |
328 | 113M | if (token->parent == -1) { |
329 | 135k | if (token->type != type || parser->toksuper == -1) { |
330 | 6.81k | return JSMN_ERROR_INVAL; |
331 | 6.81k | } |
332 | 128k | break; |
333 | 135k | } |
334 | 113M | token = &tokens[token->parent]; |
335 | 113M | } |
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 | 2.71M | break; |
361 | 7.29M | case '\"': |
362 | 7.29M | r = jsmn_parse_string(parser, js, len, tokens, num_tokens); |
363 | 7.29M | if (r < 0) { |
364 | 30.6k | return r; |
365 | 30.6k | } |
366 | 7.26M | count++; |
367 | 7.26M | if (parser->toksuper != -1 && tokens != NULL) { |
368 | 5.30M | tokens[parser->toksuper].size++; |
369 | 5.30M | } |
370 | 7.26M | break; |
371 | 779k | case '\t': |
372 | 11.2M | case '\r': |
373 | 15.7M | case '\n': |
374 | 16.3M | case ' ': |
375 | 16.3M | break; |
376 | 1.74M | case ':': |
377 | 1.74M | parser->toksuper = parser->toknext - 1; |
378 | 1.74M | break; |
379 | 6.93M | case ',': |
380 | 6.93M | if (tokens != NULL && parser->toksuper != -1 && |
381 | 6.93M | tokens[parser->toksuper].type != JSMN_ARRAY && |
382 | 6.93M | tokens[parser->toksuper].type != JSMN_OBJECT) { |
383 | 1.16M | #ifdef JSMN_PARENT_LINKS |
384 | 1.16M | 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 | 1.16M | } |
396 | 6.93M | break; |
397 | 0 | #ifdef JSMN_STRICT |
398 | | /* In strict mode primitives are: numbers and booleans */ |
399 | 1.28M | case '-': |
400 | 6.48M | case '0': |
401 | 11.9M | case '1': |
402 | 16.9M | case '2': |
403 | 17.0M | case '3': |
404 | 17.2M | case '4': |
405 | 17.4M | case '5': |
406 | 17.5M | case '6': |
407 | 17.7M | case '7': |
408 | 18.0M | case '8': |
409 | 21.9M | case '9': |
410 | 21.9M | case 't': |
411 | 23.8M | case 'f': |
412 | 23.9M | case 'n': |
413 | | /* And they must not be keys of the object */ |
414 | 23.9M | if (tokens != NULL && parser->toksuper != -1) { |
415 | 13.7M | const jsmntok_t *t = &tokens[parser->toksuper]; |
416 | 13.7M | if (t->type == JSMN_OBJECT || |
417 | 13.7M | (t->type == JSMN_STRING && t->size != 0)) { |
418 | 1.18k | return JSMN_ERROR_INVAL; |
419 | 1.18k | } |
420 | 13.7M | } |
421 | | #else |
422 | | /* In non-strict mode every unquoted value is a primitive */ |
423 | | default: |
424 | | #endif |
425 | 23.9M | r = jsmn_parse_primitive(parser, js, len, tokens, num_tokens); |
426 | 23.9M | if (r < 0) { |
427 | 100k | return r; |
428 | 100k | } |
429 | 23.8M | count++; |
430 | 23.8M | if (parser->toksuper != -1 && tokens != NULL) { |
431 | 13.6M | tokens[parser->toksuper].size++; |
432 | 13.6M | } |
433 | 23.8M | break; |
434 | | |
435 | 0 | #ifdef JSMN_STRICT |
436 | | /* Unexpected char in strict mode */ |
437 | 96.2k | default: |
438 | 96.2k | return JSMN_ERROR_INVAL; |
439 | 69.4M | #endif |
440 | 69.4M | } |
441 | 69.4M | } |
442 | | |
443 | 110k | if (tokens != NULL) { |
444 | 25.9M | for (i = parser->toknext - 1; i >= 0; i--) { |
445 | | /* Unmatched opened object or array */ |
446 | 25.8M | if (tokens[i].start != -1 && tokens[i].end == -1) { |
447 | 3.18k | return JSMN_ERROR_PART; |
448 | 3.18k | } |
449 | 25.8M | } |
450 | 110k | } |
451 | | |
452 | 107k | return count; |
453 | 110k | } |
454 | | |
455 | | /** |
456 | | * Creates a new parser based over a given buffer with an array of tokens |
457 | | * available. |
458 | | */ |
459 | 238k | JSMN_API void jsmn_init(jsmn_parser *parser) { |
460 | 238k | parser->pos = 0; |
461 | 238k | parser->toknext = 0; |
462 | 238k | parser->toksuper = -1; |
463 | 238k | } |
464 | | |
465 | | #endif /* JSMN_HEADER */ |
466 | | |
467 | | #ifdef __cplusplus |
468 | | } |
469 | | #endif |
470 | | |
471 | | #endif /* JSMN_H */ |