Coverage Report

Created: 2025-01-28 07:24

/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
5.60k
                                   const size_t num_tokens) {
108
5.60k
  jsmntok_t *tok;
109
5.60k
  if (parser->toknext >= num_tokens) {
110
0
    return NULL;
111
0
  }
112
5.60k
  tok = &tokens[parser->toknext++];
113
5.60k
  tok->start = tok->end = -1;
114
5.60k
  tok->size = 0;
115
5.60k
#ifdef JSMN_PARENT_LINKS
116
5.60k
  tok->parent = -1;
117
5.60k
#endif
118
5.60k
  return tok;
119
5.60k
}
120
121
/**
122
 * Fills token type and boundaries.
123
 */
124
static void jsmn_fill_token(jsmntok_t *token, const jsmntype_t type,
125
3.79k
                            const int start, const int end) {
126
3.79k
  token->type = type;
127
3.79k
  token->start = start;
128
3.79k
  token->end = end;
129
3.79k
  token->size = 0;
130
3.79k
}
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
3.13k
                                const size_t num_tokens) {
138
3.13k
  jsmntok_t *token;
139
3.13k
  int start;
140
141
3.13k
  start = parser->pos;
142
143
18.4k
  for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
144
17.9k
    switch (js[parser->pos]) {
145
#ifndef JSMN_STRICT
146
    /* In strict mode primitive must be followed by "," or "}" or "]" */
147
    case ':':
148
#endif
149
124
    case '\t':
150
282
    case '\r':
151
1.03k
    case '\n':
152
1.38k
    case ' ':
153
2.32k
    case ',':
154
2.52k
    case ']':
155
2.54k
    case '}':
156
2.54k
      goto found;
157
15.3k
    default:
158
                   /* to quiet a warning from gcc*/
159
15.3k
      break;
160
17.9k
    }
161
15.3k
    if (js[parser->pos] < 32 || js[parser->pos] >= 127) {
162
65
      parser->pos = start;
163
65
      return JSMN_ERROR_INVAL;
164
65
    }
165
15.3k
  }
166
519
#ifdef JSMN_STRICT
167
  /* In strict mode primitive must be followed by a comma/object/array */
168
519
  parser->pos = start;
169
519
  return JSMN_ERROR_PART;
170
0
#endif
171
172
2.54k
found:
173
2.54k
  if (tokens == NULL) {
174
0
    parser->pos--;
175
0
    return 0;
176
0
  }
177
2.54k
  token = jsmn_alloc_token(parser, tokens, num_tokens);
178
2.54k
  if (token == NULL) {
179
0
    parser->pos = start;
180
0
    return JSMN_ERROR_NOMEM;
181
0
  }
182
2.54k
  jsmn_fill_token(token, JSMN_PRIMITIVE, start, parser->pos);
183
2.54k
#ifdef JSMN_PARENT_LINKS
184
2.54k
  token->parent = parser->toksuper;
185
2.54k
#endif
186
2.54k
  parser->pos--;
187
2.54k
  return 0;
188
2.54k
}
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
1.48k
                             const size_t num_tokens) {
196
1.48k
  jsmntok_t *token;
197
198
1.48k
  int start = parser->pos;
199
200
1.48k
  parser->pos++;
201
202
  /* Skip starting quote */
203
8.76k
  for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
204
8.55k
    char c = js[parser->pos];
205
206
    /* Quote: end of string */
207
8.55k
    if (c == '\"') {
208
1.25k
      if (tokens == NULL) {
209
0
        return 0;
210
0
      }
211
1.25k
      token = jsmn_alloc_token(parser, tokens, num_tokens);
212
1.25k
      if (token == NULL) {
213
0
        parser->pos = start;
214
0
        return JSMN_ERROR_NOMEM;
215
0
      }
216
1.25k
      jsmn_fill_token(token, JSMN_STRING, start + 1, parser->pos);
217
1.25k
#ifdef JSMN_PARENT_LINKS
218
1.25k
      token->parent = parser->toksuper;
219
1.25k
#endif
220
1.25k
      return 0;
221
1.25k
    }
222
223
    /* Backslash: Quoted symbol expected */
224
7.30k
    if (c == '\\' && parser->pos + 1 < len) {
225
994
      int i;
226
994
      parser->pos++;
227
994
      switch (js[parser->pos]) {
228
      /* Allowed escaped symbols */
229
116
      case '\"':
230
210
      case '/':
231
345
      case '\\':
232
425
      case 'b':
233
528
      case 'f':
234
639
      case 'r':
235
741
      case 'n':
236
835
      case 't':
237
835
        break;
238
      /* Allows escaped symbol \uXXXX */
239
155
      case 'u':
240
155
        parser->pos++;
241
692
        for (i = 0; i < 4 && parser->pos < len && js[parser->pos] != '\0';
242
561
             i++) {
243
          /* If it isn't a hex character we have an error */
244
561
          if (!((js[parser->pos] >= 48 && js[parser->pos] <= 57) ||   /* 0-9 */
245
561
                (js[parser->pos] >= 65 && js[parser->pos] <= 70) ||   /* A-F */
246
561
                (js[parser->pos] >= 97 && js[parser->pos] <= 102))) { /* a-f */
247
24
            parser->pos = start;
248
24
            return JSMN_ERROR_INVAL;
249
24
          }
250
537
          parser->pos++;
251
537
        }
252
131
        parser->pos--;
253
131
        break;
254
      /* Unexpected symbol */
255
4
      default:
256
4
        parser->pos = start;
257
4
        return JSMN_ERROR_INVAL;
258
994
      }
259
994
    }
260
7.30k
  }
261
208
  parser->pos = start;
262
208
  return JSMN_ERROR_PART;
263
1.48k
}
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
1.98k
                        jsmntok_t *tokens, const unsigned int num_tokens) {
270
1.98k
  int r;
271
1.98k
  int i;
272
1.98k
  jsmntok_t *token;
273
1.98k
  int count = parser->toknext;
274
275
12.9k
  for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
276
12.5k
    char c;
277
12.5k
    jsmntype_t type;
278
279
12.5k
    c = js[parser->pos];
280
12.5k
    switch (c) {
281
641
    case '{':
282
1.81k
    case '[':
283
1.81k
      count++;
284
1.81k
      if (tokens == NULL) {
285
0
        break;
286
0
      }
287
1.81k
      token = jsmn_alloc_token(parser, tokens, num_tokens);
288
1.81k
      if (token == NULL) {
289
0
        return JSMN_ERROR_NOMEM;
290
0
      }
291
1.81k
      if (parser->toksuper != -1) {
292
878
        jsmntok_t *t = &tokens[parser->toksuper];
293
878
#ifdef JSMN_STRICT
294
        /* In strict mode an object or array can't become a key */
295
878
        if (t->type == JSMN_OBJECT) {
296
10
          return JSMN_ERROR_INVAL;
297
10
        }
298
868
#endif
299
868
        t->size++;
300
868
#ifdef JSMN_PARENT_LINKS
301
868
        token->parent = parser->toksuper;
302
868
#endif
303
868
      }
304
1.80k
      token->type = (c == '{' ? JSMN_OBJECT : JSMN_ARRAY);
305
1.80k
      token->start = parser->pos;
306
1.80k
      parser->toksuper = parser->toknext - 1;
307
1.80k
      break;
308
543
    case '}':
309
1.26k
    case ']':
310
1.26k
      if (tokens == NULL) {
311
0
        break;
312
0
      }
313
1.26k
      type = (c == '}' ? JSMN_OBJECT : JSMN_ARRAY);
314
1.26k
#ifdef JSMN_PARENT_LINKS
315
1.26k
      if (parser->toknext < 1) {
316
6
        return JSMN_ERROR_INVAL;
317
6
      }
318
1.25k
      token = &tokens[parser->toknext - 1];
319
2.96k
      for (;;) {
320
2.96k
        if (token->start != -1 && token->end == -1) {
321
1.19k
          if (token->type != type) {
322
3
            return JSMN_ERROR_INVAL;
323
3
          }
324
1.19k
          token->end = parser->pos + 1;
325
1.19k
          parser->toksuper = token->parent;
326
1.19k
          break;
327
1.19k
        }
328
1.77k
        if (token->parent == -1) {
329
61
          if (token->type != type || parser->toksuper == -1) {
330
15
            return JSMN_ERROR_INVAL;
331
15
          }
332
46
          break;
333
61
        }
334
1.70k
        token = &tokens[token->parent];
335
1.70k
      }
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
1.23k
      break;
361
1.48k
    case '\"':
362
1.48k
      r = jsmn_parse_string(parser, js, len, tokens, num_tokens);
363
1.48k
      if (r < 0) {
364
236
        return r;
365
236
      }
366
1.25k
      count++;
367
1.25k
      if (parser->toksuper != -1 && tokens != NULL) {
368
895
        tokens[parser->toksuper].size++;
369
895
      }
370
1.25k
      break;
371
296
    case '\t':
372
695
    case '\r':
373
1.88k
    case '\n':
374
2.65k
    case ' ':
375
2.65k
      break;
376
264
    case ':':
377
264
      parser->toksuper = parser->toknext - 1;
378
264
      break;
379
1.26k
    case ',':
380
1.26k
      if (tokens != NULL && parser->toksuper != -1 &&
381
1.26k
          tokens[parser->toksuper].type != JSMN_ARRAY &&
382
1.26k
          tokens[parser->toksuper].type != JSMN_OBJECT) {
383
55
#ifdef JSMN_PARENT_LINKS
384
55
        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
55
      }
396
1.26k
      break;
397
0
#ifdef JSMN_STRICT
398
    /* In strict mode primitives are: numbers and booleans */
399
749
    case '-':
400
985
    case '0':
401
1.42k
    case '1':
402
1.68k
    case '2':
403
1.82k
    case '3':
404
2.02k
    case '4':
405
2.15k
    case '5':
406
2.31k
    case '6':
407
2.43k
    case '7':
408
2.55k
    case '8':
409
2.75k
    case '9':
410
2.94k
    case 't':
411
3.02k
    case 'f':
412
3.13k
    case 'n':
413
      /* And they must not be keys of the object */
414
3.13k
      if (tokens != NULL && parser->toksuper != -1) {
415
1.23k
        const jsmntok_t *t = &tokens[parser->toksuper];
416
1.23k
        if (t->type == JSMN_OBJECT ||
417
1.23k
            (t->type == JSMN_STRING && t->size != 0)) {
418
8
          return JSMN_ERROR_INVAL;
419
8
        }
420
1.23k
      }
421
#else
422
    /* In non-strict mode every unquoted value is a primitive */
423
    default:
424
#endif
425
3.13k
      r = jsmn_parse_primitive(parser, js, len, tokens, num_tokens);
426
3.13k
      if (r < 0) {
427
584
        return r;
428
584
      }
429
2.54k
      count++;
430
2.54k
      if (parser->toksuper != -1 && tokens != NULL) {
431
1.11k
        tokens[parser->toksuper].size++;
432
1.11k
      }
433
2.54k
      break;
434
435
0
#ifdef JSMN_STRICT
436
    /* Unexpected char in strict mode */
437
654
    default:
438
654
      return JSMN_ERROR_INVAL;
439
12.5k
#endif
440
12.5k
    }
441
12.5k
  }
442
443
464
  if (tokens != NULL) {
444
2.38k
    for (i = parser->toknext - 1; i >= 0; i--) {
445
      /* Unmatched opened object or array */
446
2.00k
      if (tokens[i].start != -1 && tokens[i].end == -1) {
447
78
        return JSMN_ERROR_PART;
448
78
      }
449
2.00k
    }
450
464
  }
451
452
386
  return count;
453
464
}
454
455
/**
456
 * Creates a new parser based over a given buffer with an array of tokens
457
 * available.
458
 */
459
2.64k
JSMN_API void jsmn_init(jsmn_parser *parser) {
460
2.64k
  parser->pos = 0;
461
2.64k
  parser->toknext = 0;
462
2.64k
  parser->toksuper = -1;
463
2.64k
}
464
465
#endif /* JSMN_HEADER */
466
467
#ifdef __cplusplus
468
}
469
#endif
470
471
#endif /* JSMN_H */