Coverage Report

Created: 2026-11-13 06:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/barebox/include/jsmn.h
Line
Count
Source
1
// SPDX-License-Identifier: MIT
2
/*
3
 * Copyright (c) 2010 Serge Zaitsev
4
 */
5
#ifndef __JSMN_H_
6
#define __JSMN_H_
7
8
#define JSMN_STRICT
9
#define JSMN_PARENT_LINKS
10
11
#include <stddef.h>
12
#include <errno.h>
13
14
#ifdef __cplusplus
15
extern "C" {
16
#endif
17
18
#ifdef JSMN_STATIC
19
#define JSMN_API static
20
#else
21
#define JSMN_API extern
22
#endif
23
24
/**
25
 * JSON type identifier. Basic types are:
26
 *  o Object
27
 *  o Array
28
 *  o String
29
 *  o Other primitive: number, boolean (true/false) or null
30
 */
31
typedef enum {
32
  JSMN_UNDEFINED = 0,
33
  JSMN_OBJECT = 1 << 0,
34
  JSMN_ARRAY = 1 << 1,
35
  JSMN_STRING = 1 << 2,
36
  JSMN_PRIMITIVE = 1 << 3
37
} jsmntype_t;
38
39
enum jsmnerr {
40
  /* Not enough tokens were provided */
41
  JSMN_ERROR_NOMEM = -ENOMEM,
42
  /* Invalid character inside JSON string */
43
  JSMN_ERROR_INVAL = -EINVAL,
44
  /* The string is not a full JSON packet, more bytes expected */
45
  JSMN_ERROR_PART = -EMSGSIZE
46
};
47
48
/**
49
 * JSON token description.
50
 * type   type (object, array, string etc.)
51
 * start  start position in JSON data string
52
 * end    end position in JSON data string
53
 */
54
typedef struct jsmntok {
55
  jsmntype_t type;
56
  int start;
57
  int end;
58
  int size;
59
#ifdef JSMN_PARENT_LINKS
60
  int parent;
61
#endif
62
} jsmntok_t;
63
64
/**
65
 * JSON parser. Contains an array of token blocks available. Also stores
66
 * the string being parsed now and current position in that string.
67
 */
68
typedef struct jsmn_parser {
69
  unsigned int pos;     /* offset in the JSON string */
70
  unsigned int toknext; /* next token to allocate */
71
  int toksuper;         /* superior token node, e.g. parent object or array */
72
} jsmn_parser;
73
74
/**
75
 * Create JSON parser over an array of tokens
76
 */
77
JSMN_API void jsmn_init(jsmn_parser *parser);
78
79
/**
80
 * Run JSON parser. It parses a JSON data string into and array of tokens, each
81
 * describing
82
 * a single JSON object.
83
 */
84
JSMN_API int jsmn_parse(jsmn_parser *parser, const char *js, const size_t len,
85
      jsmntok_t *tokens, const unsigned int num_tokens);
86
87
/**
88
 * Like jsmn_parse, but allocates tokens dynamically.
89
 */
90
JSMN_API jsmntok_t *jsmn_parse_alloc(const char *js, const size_t len,
91
             unsigned int *num_tokens);
92
93
static inline int jsmn_token_size(const jsmntok_t *token)
94
1.27M
{
95
1.27M
  return token->end - token->start;
96
1.27M
}
jsmn.c:jsmn_token_size
Line
Count
Source
94
1.27M
{
95
1.27M
  return token->end - token->start;
96
1.27M
}
Unexecuted instantiation: jwt.c:jsmn_token_size
97
98
/** Returns `true` if `token` is a string and equal to `str`. */
99
JSMN_API bool jsmn_str_eq(const char *str, const char *json, const jsmntok_t *token);
100
101
/** Returns `true` if `token` is to `str`. */
102
JSMN_API bool jsmn_eq(const char *val, const char *json, const jsmntok_t *token);
103
104
/** Returns `true` if `token` is equal to `str`, ignoring case. */
105
JSMN_API bool jsmn_strcase_eq(const char *str, const char *json, const jsmntok_t *token);
106
107
/** Returns the token after the value at `tokens[0]`. */
108
JSMN_API const jsmntok_t *jsmn_skip_value(const jsmntok_t *tokens);
109
110
/**
111
 * Returns a pointer to the value associated with `key` inside `json` starting
112
 * at `tokens[0]`, which is expected to be an object, or returns `NULL` if `key`
113
 * cannot be found.
114
 */
115
JSMN_API const jsmntok_t *jsmn_find_value(const char *key, const char *json,
116
            const jsmntok_t *tokens);
117
118
/**
119
 * Locate the token at `path` inside `json` in a manner similar to JSONPath.
120
 *
121
 * Example:
122
 * \code
123
 * {
124
 *   "date": "...",
125
 *   "product": {
126
 *   "serial": "1234",
127
 *   }
128
 * }
129
 * \endcode
130
 *
131
 * To locate the serial number in the JSON object above, you would use the
132
 * JSONPath expression "$.product.serial". The same thing can be accomplished
133
 * with this call:
134
 *
135
 * \code
136
 * const char *path[] = {"product", "serial", NULL};
137
 * const jsmntok_t *token = jsmn_locate(path, json, tokens);
138
 * \endcode
139
 *
140
 * \remark This function cannot search inside arrays.
141
 *
142
 * @param path    Null-terminated list of path elements.
143
 * @param json    JSON string to search in.
144
 * @param tokens  Tokens for `json`.
145
 *
146
 * @return Pointer to the value token or `NULL` if the token could not be found.
147
 */
148
JSMN_API const jsmntok_t *jsmn_locate(const char *path[], const char *json,
149
              const jsmntok_t *tokens);
150
151
/**
152
 * Similar to `jsmn_locate` but returns a copy of the value or `NULL` if the
153
 * value does not exist or is not a string. The caller takes ownership of the
154
 * pointer returned.
155
 */
156
JSMN_API char *jsmn_strdup(const char *path[], const char *json, const jsmntok_t *tokens);
157
158
#ifdef __cplusplus
159
}
160
#endif
161
162
#endif /* JSMN_H */