Line | Count | Source |
1 | | /* |
2 | | * jsmn.c |
3 | | * Simple JSON parser |
4 | | * |
5 | | * Copyright (c) 2010 Serge A. Zaitsev |
6 | | * Updated to use size_t for token offsets and harden against overflows. |
7 | | * (Nikias Bassen, January 2026) |
8 | | * |
9 | | * Permission is hereby granted, free of charge, to any person obtaining a copy |
10 | | * of this software and associated documentation files (the "Software"), to deal |
11 | | * in the Software without restriction, including without limitation the rights |
12 | | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
13 | | * copies of the Software, and to permit persons to whom the Software is |
14 | | * furnished to do so, subject to the following conditions: |
15 | | * |
16 | | * The above copyright notice and this permission notice shall be included in |
17 | | * all copies or substantial portions of the Software. |
18 | | * |
19 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20 | | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
22 | | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24 | | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
25 | | * THE SOFTWARE. |
26 | | */ |
27 | | |
28 | | #include <stdlib.h> |
29 | | #include <stdint.h> |
30 | | #include <limits.h> |
31 | | #include <assert.h> |
32 | | |
33 | | #include "jsmn.h" |
34 | | |
35 | 0 | #define JSMN_POS_INVALID ((size_t)SIZE_MAX) |
36 | | |
37 | | /** |
38 | | * Allocates a fresh unused token from the token pull. |
39 | | */ |
40 | | static jsmntok_t *jsmn_alloc_token(jsmn_parser *parser, |
41 | 0 | jsmntok_t *tokens, unsigned int num_tokens) { |
42 | 0 | jsmntok_t *tok; |
43 | 0 | if ((unsigned int)parser->toknext >= num_tokens) { |
44 | 0 | return NULL; |
45 | 0 | } |
46 | 0 | tok = &tokens[parser->toknext++]; |
47 | 0 | tok->start = tok->end = JSMN_POS_INVALID; |
48 | 0 | tok->size = 0; |
49 | | #ifdef JSMN_PARENT_LINKS |
50 | | tok->parent = -1; |
51 | | #endif |
52 | 0 | return tok; |
53 | 0 | } |
54 | | |
55 | | /** |
56 | | * Fills token type and boundaries. |
57 | | */ |
58 | | static void jsmn_fill_token(jsmntok_t *token, jsmntype_t type, |
59 | 0 | size_t start, size_t end) { |
60 | 0 | token->type = type; |
61 | 0 | token->start = start; |
62 | 0 | token->end = end; |
63 | 0 | token->size = 0; |
64 | 0 | } |
65 | | |
66 | | /** |
67 | | * Fills next available token with JSON primitive. |
68 | | */ |
69 | | static jsmnerr_t jsmn_parse_primitive(jsmn_parser *parser, const char *js, |
70 | 0 | jsmntok_t *tokens, unsigned int num_tokens) { |
71 | 0 | jsmntok_t *token; |
72 | 0 | size_t start; |
73 | |
|
74 | 0 | start = parser->pos; |
75 | |
|
76 | 0 | for (; (parser->end > 0 && parser->pos < parser->end) && js[parser->pos] != '\0'; parser->pos++) { |
77 | 0 | switch (js[parser->pos]) { |
78 | 0 | #ifndef JSMN_STRICT |
79 | | /* In strict mode primitive must be followed by "," or "}" or "]" */ |
80 | 0 | case ':': |
81 | 0 | #endif |
82 | 0 | case '\t' : case '\r' : case '\n' : case ' ' : |
83 | 0 | case ',' : case ']' : case '}' : |
84 | 0 | goto found; |
85 | 0 | default: |
86 | 0 | break; |
87 | 0 | } |
88 | 0 | if (js[parser->pos] < 32 || js[parser->pos] >= 127) { |
89 | 0 | parser->pos = start; |
90 | 0 | return JSMN_ERROR_INVAL; |
91 | 0 | } |
92 | 0 | } |
93 | | #ifdef JSMN_STRICT |
94 | | /* In strict mode primitive must be followed by a comma/object/array */ |
95 | | parser->pos = start; |
96 | | return JSMN_ERROR_PART; |
97 | | #endif |
98 | | |
99 | 0 | found: |
100 | 0 | token = jsmn_alloc_token(parser, tokens, num_tokens); |
101 | 0 | if (token == NULL) { |
102 | 0 | parser->pos = start; |
103 | 0 | return JSMN_ERROR_NOMEM; |
104 | 0 | } |
105 | 0 | jsmn_fill_token(token, JSMN_PRIMITIVE, start, parser->pos); |
106 | | #ifdef JSMN_PARENT_LINKS |
107 | | token->parent = parser->toksuper; |
108 | | #endif |
109 | 0 | parser->pos--; |
110 | 0 | return JSMN_SUCCESS; |
111 | 0 | } |
112 | | |
113 | | /** |
114 | | * Fills next token with JSON string. |
115 | | */ |
116 | | static jsmnerr_t jsmn_parse_string(jsmn_parser *parser, const char *js, |
117 | 0 | jsmntok_t *tokens, unsigned int num_tokens) { |
118 | 0 | jsmntok_t *token; |
119 | |
|
120 | 0 | size_t start = parser->pos; |
121 | |
|
122 | 0 | parser->pos++; |
123 | | |
124 | | /* Skip starting quote */ |
125 | 0 | for (; (parser->end > 0 && parser->pos < parser->end) && js[parser->pos] != '\0'; parser->pos++) { |
126 | 0 | char c = js[parser->pos]; |
127 | | |
128 | | /* Quote: end of string */ |
129 | 0 | if (c == '\"') { |
130 | 0 | token = jsmn_alloc_token(parser, tokens, num_tokens); |
131 | 0 | if (token == NULL) { |
132 | 0 | parser->pos = start; |
133 | 0 | return JSMN_ERROR_NOMEM; |
134 | 0 | } |
135 | 0 | jsmn_fill_token(token, JSMN_STRING, start+1, parser->pos); |
136 | | #ifdef JSMN_PARENT_LINKS |
137 | | token->parent = parser->toksuper; |
138 | | #endif |
139 | 0 | return JSMN_SUCCESS; |
140 | 0 | } |
141 | | |
142 | | /* Backslash: Quoted symbol expected */ |
143 | 0 | if (c == '\\') { |
144 | 0 | parser->pos++; |
145 | 0 | if (parser->end > 0 && parser->pos >= parser->end) { |
146 | 0 | parser->pos = start; |
147 | 0 | return JSMN_ERROR_INVAL; |
148 | 0 | } |
149 | 0 | switch (js[parser->pos]) { |
150 | | /* Allowed escaped symbols */ |
151 | 0 | case '\"': case '/' : case '\\' : case 'b' : |
152 | 0 | case 'f' : case 'r' : case 'n' : case 't' : |
153 | 0 | break; |
154 | | /* Allows escaped symbol \uXXXX */ |
155 | 0 | case 'u': |
156 | | /* TODO */ |
157 | 0 | break; |
158 | | /* Unexpected symbol */ |
159 | 0 | default: |
160 | 0 | parser->pos = start; |
161 | 0 | return JSMN_ERROR_INVAL; |
162 | 0 | } |
163 | 0 | } |
164 | 0 | } |
165 | 0 | parser->pos = start; |
166 | 0 | return JSMN_ERROR_PART; |
167 | 0 | } |
168 | | |
169 | | /** |
170 | | * Parse JSON string and fill tokens. |
171 | | */ |
172 | | jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, size_t length, jsmntok_t *tokens, |
173 | 0 | unsigned int num_tokens) { |
174 | 0 | jsmnerr_t r; |
175 | 0 | int i; |
176 | 0 | jsmntok_t *token; |
177 | |
|
178 | 0 | parser->end = length; |
179 | |
|
180 | 0 | if (num_tokens >= INT_MAX) { |
181 | 0 | return JSMN_ERROR_LIMIT; |
182 | 0 | } |
183 | 0 | if (length > SIZE_MAX / 2) { |
184 | 0 | return JSMN_ERROR_LIMIT; |
185 | 0 | } |
186 | | |
187 | 0 | for (; (parser->end > 0 && parser->pos < parser->end) && js[parser->pos] != '\0'; parser->pos++) { |
188 | 0 | char c; |
189 | 0 | jsmntype_t type; |
190 | |
|
191 | 0 | c = js[parser->pos]; |
192 | 0 | switch (c) { |
193 | 0 | case '{': case '[': |
194 | 0 | token = jsmn_alloc_token(parser, tokens, num_tokens); |
195 | 0 | if (token == NULL) |
196 | 0 | return JSMN_ERROR_NOMEM; |
197 | 0 | if (parser->toksuper != -1) { |
198 | 0 | tokens[parser->toksuper].size++; |
199 | | #ifdef JSMN_PARENT_LINKS |
200 | | token->parent = parser->toksuper; |
201 | | #endif |
202 | 0 | } |
203 | 0 | token->type = (c == '{' ? JSMN_OBJECT : JSMN_ARRAY); |
204 | 0 | token->start = parser->pos; |
205 | 0 | parser->toksuper = parser->toknext - 1; |
206 | 0 | break; |
207 | 0 | case '}': case ']': |
208 | 0 | type = (c == '}' ? JSMN_OBJECT : JSMN_ARRAY); |
209 | | #ifdef JSMN_PARENT_LINKS |
210 | | if (parser->toknext < 1) { |
211 | | return JSMN_ERROR_INVAL; |
212 | | } |
213 | | token = &tokens[parser->toknext - 1]; |
214 | | for (;;) { |
215 | | if (token->start != JSMN_POS_INVALID && token->end == JSMN_POS_INVALID) { |
216 | | if (token->type != type) { |
217 | | return JSMN_ERROR_INVAL; |
218 | | } |
219 | | if (parser->pos == SIZE_MAX) { |
220 | | return JSMN_ERROR_INVAL; |
221 | | } |
222 | | token->end = parser->pos + 1; |
223 | | parser->toksuper = token->parent; |
224 | | break; |
225 | | } |
226 | | if (token->parent == -1) { |
227 | | break; |
228 | | } |
229 | | token = &tokens[token->parent]; |
230 | | } |
231 | | #else |
232 | 0 | for (i = parser->toknext - 1; i >= 0; i--) { |
233 | 0 | token = &tokens[i]; |
234 | 0 | if (token->start != JSMN_POS_INVALID && token->end == JSMN_POS_INVALID) { |
235 | 0 | if (token->type != type) { |
236 | 0 | return JSMN_ERROR_INVAL; |
237 | 0 | } |
238 | 0 | parser->toksuper = -1; |
239 | 0 | token->end = parser->pos + 1; |
240 | 0 | break; |
241 | 0 | } |
242 | 0 | } |
243 | | /* Error if unmatched closing bracket */ |
244 | 0 | if (i == -1) return JSMN_ERROR_INVAL; |
245 | 0 | for (; i >= 0; i--) { |
246 | 0 | token = &tokens[i]; |
247 | 0 | if (token->start != JSMN_POS_INVALID && token->end == JSMN_POS_INVALID) { |
248 | 0 | parser->toksuper = i; |
249 | 0 | break; |
250 | 0 | } |
251 | 0 | } |
252 | 0 | #endif |
253 | 0 | break; |
254 | 0 | case '\"': |
255 | 0 | r = jsmn_parse_string(parser, js, tokens, num_tokens); |
256 | 0 | if (r < 0) return r; |
257 | 0 | if (parser->toksuper != -1) |
258 | 0 | tokens[parser->toksuper].size++; |
259 | 0 | break; |
260 | 0 | case '\t' : case '\r' : case '\n' : case ':' : case ',': case ' ': |
261 | 0 | break; |
262 | | #ifdef JSMN_STRICT |
263 | | /* In strict mode primitives are: numbers and booleans */ |
264 | | case '-': case '0': case '1' : case '2': case '3' : case '4': |
265 | | case '5': case '6': case '7' : case '8': case '9': |
266 | | case 't': case 'f': case 'n' : |
267 | | #else |
268 | | /* In non-strict mode every unquoted value is a primitive */ |
269 | 0 | default: |
270 | 0 | #endif |
271 | 0 | r = jsmn_parse_primitive(parser, js, tokens, num_tokens); |
272 | 0 | if (r < 0) return r; |
273 | 0 | if (parser->toksuper != -1) |
274 | 0 | tokens[parser->toksuper].size++; |
275 | 0 | break; |
276 | |
|
277 | | #ifdef JSMN_STRICT |
278 | | /* Unexpected char in strict mode */ |
279 | | default: |
280 | | return JSMN_ERROR_INVAL; |
281 | | #endif |
282 | |
|
283 | 0 | } |
284 | 0 | } |
285 | | |
286 | 0 | for (i = parser->toknext - 1; i >= 0; i--) { |
287 | | /* Unmatched opened object or array */ |
288 | 0 | if (tokens[i].start != JSMN_POS_INVALID && tokens[i].end == JSMN_POS_INVALID) { |
289 | 0 | return JSMN_ERROR_PART; |
290 | 0 | } |
291 | 0 | } |
292 | | |
293 | 0 | return JSMN_SUCCESS; |
294 | 0 | } |
295 | | |
296 | | /** |
297 | | * Creates a new parser based over a given buffer with an array of tokens |
298 | | * available. |
299 | | */ |
300 | 0 | void jsmn_init(jsmn_parser *parser) { |
301 | 0 | parser->pos = 0; |
302 | 0 | parser->end = 0; |
303 | 0 | parser->toknext = 0; |
304 | 0 | parser->toksuper = -1; |
305 | 0 | } |
306 | | |