Coverage Report

Created: 2026-10-05 07:34

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libredwg/jsmn/jsmn.h
Line
Count
Source
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 {
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 {
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
71.2k
                                   const size_t num_tokens) {
108
71.2k
  jsmntok_t *tok;
109
71.2k
  if (parser->toknext >= num_tokens) {
110
0
    return NULL;
111
0
  }
112
71.2k
  tok = &tokens[parser->toknext++];
113
71.2k
  tok->start = tok->end = -1;
114
71.2k
  tok->size = 0;
115
#ifdef JSMN_PARENT_LINKS
116
  tok->parent = -1;
117
#endif
118
71.2k
  return tok;
119
71.2k
}
120
121
/**
122
 * Fills token type and boundaries.
123
 */
124
static void jsmn_fill_token(jsmntok_t *token, const jsmntype_t type,
125
37.4k
                            const int start, const int end) {
126
37.4k
  token->type = type;
127
37.4k
  token->start = start;
128
37.4k
  token->end = end;
129
37.4k
  token->size = 0;
130
37.4k
}
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
19.7k
                                const size_t num_tokens) {
138
19.7k
  jsmntok_t *token;
139
19.7k
  int start;
140
141
19.7k
  start = parser->pos;
142
143
191k
  for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
144
191k
    switch (js[parser->pos]) {
145
0
#ifndef JSMN_STRICT
146
    /* In strict mode primitive must be followed by "," or "}" or "]" */
147
47
    case ':':
148
47
#endif
149
47
    case '\t':
150
119
    case '\r':
151
1.09k
    case '\n':
152
7.86k
    case ' ':
153
15.6k
    case ',':
154
17.0k
    case ']':
155
19.7k
    case '}':
156
19.7k
      goto found;
157
171k
    default:
158
                   /* to quiet a warning from gcc*/
159
171k
      break;
160
191k
    }
161
171k
    if (js[parser->pos] < 32 || js[parser->pos] >= 127) {
162
0
      parser->pos = start;
163
0
      return JSMN_ERROR_INVAL;
164
0
    }
165
171k
  }
166
#ifdef JSMN_STRICT
167
  /* In strict mode primitive must be followed by a comma/object/array */
168
  parser->pos = start;
169
  return JSMN_ERROR_PART;
170
#endif
171
172
19.7k
found:
173
19.7k
  if (tokens == NULL) {
174
9.97k
    parser->pos--;
175
9.97k
    return 0;
176
9.97k
  }
177
9.77k
  token = jsmn_alloc_token(parser, tokens, num_tokens);
178
9.77k
  if (token == NULL) {
179
0
    parser->pos = start;
180
0
    return JSMN_ERROR_NOMEM;
181
0
  }
182
9.77k
  jsmn_fill_token(token, JSMN_PRIMITIVE, start, parser->pos);
183
#ifdef JSMN_PARENT_LINKS
184
  token->parent = parser->toksuper;
185
#endif
186
9.77k
  parser->pos--;
187
9.77k
  return 0;
188
9.77k
}
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
55.9k
                             const size_t num_tokens) {
196
55.9k
  jsmntok_t *token;
197
198
55.9k
  int start = parser->pos;
199
200
55.9k
  parser->pos++;
201
202
  /* Skip starting quote */
203
2.31M
  for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
204
2.31M
    char c = js[parser->pos];
205
206
    /* Quote: end of string */
207
2.31M
    if (c == '\"') {
208
55.9k
      if (tokens == NULL) {
209
28.3k
        return 0;
210
28.3k
      }
211
27.6k
      token = jsmn_alloc_token(parser, tokens, num_tokens);
212
27.6k
      if (token == NULL) {
213
0
        parser->pos = start;
214
0
        return JSMN_ERROR_NOMEM;
215
0
      }
216
27.6k
      jsmn_fill_token(token, JSMN_STRING, start + 1, parser->pos);
217
#ifdef JSMN_PARENT_LINKS
218
      token->parent = parser->toksuper;
219
#endif
220
27.6k
      return 0;
221
27.6k
    }
222
223
    /* Backslash: Quoted symbol expected */
224
2.26M
    if (c == '\\' && parser->pos + 1 < len) {
225
5.38k
      int i;
226
5.38k
      parser->pos++;
227
5.38k
      switch (js[parser->pos]) {
228
      /* Allowed escaped symbols */
229
2.25k
      case '\"':
230
2.25k
      case '/':
231
2.25k
      case '\\':
232
2.25k
      case 'b':
233
2.27k
      case 'f':
234
2.27k
      case 'r':
235
2.56k
      case 'n':
236
2.74k
      case 't':
237
2.74k
        break;
238
      /* Allows escaped symbol \uXXXX */
239
2.63k
      case 'u':
240
2.63k
        parser->pos++;
241
13.1k
        for (i = 0; i < 4 && parser->pos < len && js[parser->pos] != '\0';
242
10.5k
             i++) {
243
          /* If it isn't a hex character we have an error */
244
10.5k
          if (!((js[parser->pos] >= 48 && js[parser->pos] <= 57) ||   /* 0-9 */
245
2.04k
                (js[parser->pos] >= 65 && js[parser->pos] <= 70) ||   /* A-F */
246
2.04k
                (js[parser->pos] >= 97 && js[parser->pos] <= 102))) { /* a-f */
247
0
            parser->pos = start;
248
0
            return JSMN_ERROR_INVAL;
249
0
          }
250
10.5k
          parser->pos++;
251
10.5k
        }
252
2.63k
        parser->pos--;
253
2.63k
        break;
254
      /* Unexpected symbol */
255
0
      default:
256
0
        parser->pos = start;
257
0
        return JSMN_ERROR_INVAL;
258
5.38k
      }
259
5.38k
    }
260
2.26M
  }
261
5
  parser->pos = start;
262
5
  return JSMN_ERROR_PART;
263
55.9k
}
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
103
                        jsmntok_t *tokens, const unsigned int num_tokens) {
270
103
  int r;
271
103
  int i;
272
103
  jsmntok_t *token;
273
103
  int count = parser->toknext;
274
275
233k
  for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
276
233k
    char c;
277
233k
    jsmntype_t type;
278
279
233k
    c = js[parser->pos];
280
233k
    switch (c) {
281
65.6k
    case '{':
282
67.9k
    case '[':
283
67.9k
      count++;
284
67.9k
      if (tokens == NULL) {
285
34.1k
        break;
286
34.1k
      }
287
33.8k
      token = jsmn_alloc_token(parser, tokens, num_tokens);
288
33.8k
      if (token == NULL) {
289
0
        return JSMN_ERROR_NOMEM;
290
0
      }
291
33.8k
      if (parser->toksuper != -1) {
292
32.1k
        jsmntok_t *t = &tokens[parser->toksuper];
293
#ifdef JSMN_STRICT
294
        /* In strict mode an object or array can't become a key */
295
        if (t->type == JSMN_OBJECT) {
296
          return JSMN_ERROR_INVAL;
297
        }
298
#endif
299
32.1k
        t->size++;
300
#ifdef JSMN_PARENT_LINKS
301
        token->parent = parser->toksuper;
302
#endif
303
32.1k
      }
304
33.8k
      token->type = (c == '{' ? JSMN_OBJECT : JSMN_ARRAY);
305
33.8k
      token->start = parser->pos;
306
33.8k
      parser->toksuper = parser->toknext - 1;
307
33.8k
      break;
308
49.6k
    case '}':
309
51.9k
    case ']':
310
51.9k
      if (tokens == NULL) {
311
26.1k
        break;
312
26.1k
      }
313
25.8k
      type = (c == '}' ? JSMN_OBJECT : JSMN_ARRAY);
314
#ifdef JSMN_PARENT_LINKS
315
      if (parser->toknext < 1) {
316
        return JSMN_ERROR_INVAL;
317
      }
318
      token = &tokens[parser->toknext - 1];
319
      for (;;) {
320
        if (token->start != -1 && token->end == -1) {
321
          if (token->type != type) {
322
            return JSMN_ERROR_INVAL;
323
          }
324
          token->end = parser->pos + 1;
325
          parser->toksuper = token->parent;
326
          break;
327
        }
328
        if (token->parent == -1) {
329
          if (token->type != type || parser->toksuper == -1) {
330
            return JSMN_ERROR_INVAL;
331
          }
332
          break;
333
        }
334
        token = &tokens[token->parent];
335
      }
336
#else
337
6.62M
      for (i = parser->toknext - 1; i >= 0; i--) {
338
6.62M
        token = &tokens[i];
339
6.62M
        if (token->start != -1 && token->end == -1) {
340
25.8k
          if (token->type != type) {
341
2
            return JSMN_ERROR_INVAL;
342
2
          }
343
25.8k
          parser->toksuper = -1;
344
25.8k
          token->end = parser->pos + 1;
345
25.8k
          break;
346
25.8k
        }
347
6.62M
      }
348
      /* Error if unmatched closing bracket */
349
25.8k
      if (i == -1) {
350
0
        return JSMN_ERROR_INVAL;
351
0
      }
352
3.07M
      for (; i >= 0; i--) {
353
3.07M
        token = &tokens[i];
354
3.07M
        if (token->start != -1 && token->end == -1) {
355
24.1k
          parser->toksuper = i;
356
24.1k
          break;
357
24.1k
        }
358
3.07M
      }
359
25.8k
#endif
360
25.8k
      break;
361
55.9k
    case '\"':
362
55.9k
      r = jsmn_parse_string(parser, js, len, tokens, num_tokens);
363
55.9k
      if (r < 0) {
364
5
        return r;
365
5
      }
366
55.9k
      count++;
367
55.9k
      if (parser->toksuper != -1 && tokens != NULL) {
368
27.4k
        tokens[parser->toksuper].size++;
369
27.4k
      }
370
55.9k
      break;
371
0
    case '\t':
372
196
    case '\r':
373
2.81k
    case '\n':
374
10.3k
    case ' ':
375
10.3k
      break;
376
13.5k
    case ':':
377
13.5k
      parser->toksuper = parser->toknext - 1;
378
13.5k
      break;
379
13.6k
    case ',':
380
13.6k
      if (tokens != NULL && parser->toksuper != -1 &&
381
6.71k
          tokens[parser->toksuper].type != JSMN_ARRAY &&
382
5.38k
          tokens[parser->toksuper].type != JSMN_OBJECT) {
383
#ifdef JSMN_PARENT_LINKS
384
        parser->toksuper = tokens[parser->toksuper].parent;
385
#else
386
105k
        for (i = parser->toknext - 1; i >= 0; i--) {
387
105k
          if (tokens[i].type == JSMN_ARRAY || tokens[i].type == JSMN_OBJECT) {
388
19.4k
            if (tokens[i].start != -1 && tokens[i].end == -1) {
389
3.13k
              parser->toksuper = i;
390
3.13k
              break;
391
3.13k
            }
392
19.4k
          }
393
105k
        }
394
3.13k
#endif
395
3.13k
      }
396
13.6k
      break;
397
#ifdef JSMN_STRICT
398
    /* In strict mode primitives are: numbers and booleans */
399
    case '-':
400
    case '0':
401
    case '1':
402
    case '2':
403
    case '3':
404
    case '4':
405
    case '5':
406
    case '6':
407
    case '7':
408
    case '8':
409
    case '9':
410
    case 't':
411
    case 'f':
412
    case 'n':
413
      /* And they must not be keys of the object */
414
      if (tokens != NULL && parser->toksuper != -1) {
415
        const jsmntok_t *t = &tokens[parser->toksuper];
416
        if (t->type == JSMN_OBJECT ||
417
            (t->type == JSMN_STRING && t->size != 0)) {
418
          return JSMN_ERROR_INVAL;
419
        }
420
      }
421
#else
422
    /* In non-strict mode every unquoted value is a primitive */
423
19.7k
    default:
424
19.7k
#endif
425
19.7k
      r = jsmn_parse_primitive(parser, js, len, tokens, num_tokens);
426
19.7k
      if (r < 0) {
427
0
        return r;
428
0
      }
429
19.7k
      count++;
430
19.7k
      if (parser->toksuper != -1 && tokens != NULL) {
431
9.44k
        tokens[parser->toksuper].size++;
432
9.44k
      }
433
19.7k
      break;
434
435
#ifdef JSMN_STRICT
436
    /* Unexpected char in strict mode */
437
    default:
438
      return JSMN_ERROR_INVAL;
439
#endif
440
233k
    }
441
233k
  }
442
443
96
  if (tokens != NULL) {
444
29.0k
    for (i = parser->toknext - 1; i >= 0; i--) {
445
      /* Unmatched opened object or array */
446
29.0k
      if (tokens[i].start != -1 && tokens[i].end == -1) {
447
10
        return JSMN_ERROR_PART;
448
10
      }
449
29.0k
    }
450
47
  }
451
452
86
  return count;
453
96
}
454
455
/**
456
 * Creates a new parser based over a given buffer with an array of tokens
457
 * available.
458
 */
459
103
JSMN_API void jsmn_init(jsmn_parser *parser) {
460
103
  parser->pos = 0;
461
103
  parser->toknext = 0;
462
103
  parser->toksuper = -1;
463
103
}
464
465
#endif /* JSMN_HEADER */
466
467
#ifdef __cplusplus
468
}
469
#endif
470
471
#endif /* JSMN_H */