/src/open62541/deps/cj5.c
Line | Count | Source |
1 | | // MIT License |
2 | | // |
3 | | // Copyright (c) 2020 Sepehr Taghdisian |
4 | | // Copyright (c) 2022, 2024 Julius Pfrommer |
5 | | // Copyright 2026 (c) o6 Automation GmbH (Author: Julius Pfrommer) |
6 | | // |
7 | | // Permission is hereby granted, free of charge, to any person obtaining a copy |
8 | | // of this software and associated documentation files (the "Software"), to deal |
9 | | // in the Software without restriction, including without limitation the rights |
10 | | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
11 | | // copies of the Software, and to permit persons to whom the Software is |
12 | | // furnished to do so, subject to the following conditions: |
13 | | // |
14 | | // The above copyright notice and this permission notice shall be included in all |
15 | | // copies or substantial portions of the Software. |
16 | | // |
17 | | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
18 | | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
19 | | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
20 | | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
21 | | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
22 | | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
23 | | // SOFTWARE. |
24 | | |
25 | | #include "cj5.h" |
26 | | #include "parse_num.h" |
27 | | #include "utf8.h" |
28 | | |
29 | | #include <math.h> |
30 | | #include <float.h> |
31 | | #include <string.h> |
32 | | |
33 | | #if defined(_MSC_VER) |
34 | | # define CJ5_INLINE __inline |
35 | | #else |
36 | | # define CJ5_INLINE inline |
37 | | #endif |
38 | | |
39 | | /* vs2008 does not have INFINITY and NAN defined */ |
40 | | #ifndef INFINITY |
41 | | # define INFINITY ((double)(DBL_MAX+DBL_MAX)) |
42 | | #endif |
43 | | #ifndef NAN |
44 | | # define NAN ((double)(INFINITY-INFINITY)) |
45 | | #endif |
46 | | |
47 | | #if defined(_MSC_VER) |
48 | | # pragma warning(disable: 4056) |
49 | | # pragma warning(disable: 4756) |
50 | | #endif |
51 | | |
52 | | /* Max nesting depth of objects and arrays */ |
53 | 0 | #define CJ5_MAX_NESTING 256 |
54 | | |
55 | | #define CJ5__FOURCC(_a, _b, _c, _d) \ |
56 | | (((uint32_t)(_a) | ((uint32_t)(_b) << 8) | \ |
57 | | ((uint32_t)(_c) << 16) | ((uint32_t)(_d) << 24))) |
58 | | |
59 | | static const uint32_t CJ5__NULL_FOURCC = CJ5__FOURCC('n', 'u', 'l', 'l'); |
60 | | static const uint32_t CJ5__TRUE_FOURCC = CJ5__FOURCC('t', 'r', 'u', 'e'); |
61 | | static const uint32_t CJ5__FALSE_FOURCC = CJ5__FOURCC('f', 'a', 'l', 's'); |
62 | | |
63 | | typedef struct { |
64 | | unsigned int pos; |
65 | | cj5_error_code error; |
66 | | |
67 | | const char *json5; |
68 | | unsigned int len; |
69 | | |
70 | | unsigned int curr_tok_idx; |
71 | | |
72 | | cj5_token *tokens; |
73 | | unsigned int token_count; |
74 | | unsigned int max_tokens; |
75 | | |
76 | | bool stop_early; |
77 | | } cj5__parser; |
78 | | |
79 | | static CJ5_INLINE bool |
80 | 0 | cj5__isrange(char ch, char from, char to) { |
81 | 0 | return (uint8_t)(ch - from) <= (uint8_t)(to - from); |
82 | 0 | } |
83 | | |
84 | 0 | #define cj5__isupperchar(ch) cj5__isrange(ch, 'A', 'Z') |
85 | 0 | #define cj5__islowerchar(ch) cj5__isrange(ch, 'a', 'z') |
86 | 0 | #define cj5__isnum(ch) cj5__isrange(ch, '0', '9') |
87 | | |
88 | | static cj5_token * |
89 | 0 | cj5__alloc_token(cj5__parser *parser) { |
90 | 0 | cj5_token* token = NULL; |
91 | 0 | if(parser->token_count < parser->max_tokens) { |
92 | 0 | token = &parser->tokens[parser->token_count]; |
93 | 0 | memset(token, 0x0, sizeof(cj5_token)); |
94 | 0 | } else { |
95 | 0 | parser->error = CJ5_ERROR_OVERFLOW; |
96 | 0 | } |
97 | | |
98 | | // Always increase the index. So we know eventually how many token would be |
99 | | // required (if there are not enough). |
100 | 0 | parser->token_count++; |
101 | 0 | return token; |
102 | 0 | } |
103 | | |
104 | | static void |
105 | 0 | cj5__parse_string(cj5__parser *parser) { |
106 | 0 | const char *json5 = parser->json5; |
107 | 0 | unsigned int len = parser->len; |
108 | 0 | unsigned int start = parser->pos; |
109 | 0 | char str_open = json5[start]; |
110 | |
|
111 | 0 | parser->pos++; |
112 | 0 | for(; parser->pos < len; parser->pos++) { |
113 | 0 | char c = json5[parser->pos]; |
114 | | |
115 | | // End of string |
116 | 0 | if(str_open == c) { |
117 | 0 | cj5_token *token = cj5__alloc_token(parser); |
118 | 0 | if(token) { |
119 | 0 | token->type = CJ5_TOKEN_STRING; |
120 | 0 | token->start = start + 1; |
121 | 0 | token->end = parser->pos - 1; |
122 | 0 | token->size = token->end - token->start + 1; |
123 | 0 | token->parent_id = parser->curr_tok_idx; |
124 | 0 | } |
125 | 0 | return; |
126 | 0 | } |
127 | | |
128 | | // Unescaped newlines are forbidden |
129 | 0 | if(c == '\n') { |
130 | 0 | parser->error = CJ5_ERROR_INVALID; |
131 | 0 | return; |
132 | 0 | } |
133 | | |
134 | | // Skip escape character |
135 | 0 | if(c == '\\') { |
136 | 0 | if(parser->pos + 1 >= len) { |
137 | 0 | parser->error = CJ5_ERROR_INCOMPLETE; |
138 | 0 | return; |
139 | 0 | } |
140 | 0 | parser->pos++; |
141 | 0 | } |
142 | 0 | } |
143 | | |
144 | | // The file has ended before the string terminates |
145 | 0 | parser->error = CJ5_ERROR_INCOMPLETE; |
146 | 0 | } |
147 | | |
148 | | // parser->pos is advanced a last time in the next iteration of the main |
149 | | // parse-loop. So we leave parse-primitive in a state where parse->pos points to |
150 | | // the last character of the primitive value (or the quote-character of the |
151 | | // string). |
152 | | static void |
153 | 0 | cj5__parse_primitive(cj5__parser* parser) { |
154 | 0 | const char* json5 = parser->json5; |
155 | 0 | unsigned int len = parser->len; |
156 | 0 | unsigned int start = parser->pos; |
157 | | |
158 | | // String value |
159 | 0 | if(json5[start] == '\"' || |
160 | 0 | json5[start] == '\'') { |
161 | 0 | cj5__parse_string(parser); |
162 | 0 | return; |
163 | 0 | } |
164 | | |
165 | | // Fast comparison of bool, and null. |
166 | | // Make the comparison case-insensitive. |
167 | 0 | uint32_t fourcc = 0; |
168 | 0 | if(start + 3 < len) { |
169 | 0 | fourcc += (unsigned char)json5[start] | 32U; |
170 | 0 | fourcc += ((unsigned char)json5[start+1] | 32U) << 8; |
171 | 0 | fourcc += ((unsigned char)json5[start+2] | 32U) << 16; |
172 | 0 | fourcc += ((unsigned char)json5[start+3] | 32U) << 24; |
173 | 0 | } |
174 | | |
175 | 0 | cj5_token_type type; |
176 | 0 | if(fourcc == CJ5__NULL_FOURCC) { |
177 | 0 | type = CJ5_TOKEN_NULL; |
178 | 0 | parser->pos += 3; |
179 | 0 | } else if(fourcc == CJ5__TRUE_FOURCC) { |
180 | 0 | type = CJ5_TOKEN_BOOL; |
181 | 0 | parser->pos += 3; |
182 | 0 | } else if(fourcc == CJ5__FALSE_FOURCC) { |
183 | | // "false" has five characters |
184 | 0 | type = CJ5_TOKEN_BOOL; |
185 | 0 | if(start + 4 >= len || (json5[start+4] | 32) != 'e') { |
186 | 0 | parser->error = CJ5_ERROR_INVALID; |
187 | 0 | return; |
188 | 0 | } |
189 | 0 | parser->pos += 4; |
190 | 0 | } else { |
191 | | // Numbers are checked for basic compatibility. |
192 | | // But they are fully parsed only in the cj5_get_XXX functions. |
193 | 0 | type = CJ5_TOKEN_NUMBER; |
194 | 0 | for(; parser->pos < len; parser->pos++) { |
195 | 0 | if(!cj5__isnum(json5[parser->pos]) && |
196 | 0 | !(json5[parser->pos] == '.') && |
197 | 0 | !cj5__islowerchar(json5[parser->pos]) && |
198 | 0 | !cj5__isupperchar(json5[parser->pos]) && |
199 | 0 | !(json5[parser->pos] == '+') && !(json5[parser->pos] == '-')) { |
200 | 0 | break; |
201 | 0 | } |
202 | 0 | } |
203 | 0 | parser->pos--; // Point to the last character that is still inside the |
204 | | // primitive value |
205 | 0 | } |
206 | | |
207 | 0 | cj5_token *token = cj5__alloc_token(parser); |
208 | 0 | if(token) { |
209 | 0 | token->type = type; |
210 | 0 | token->start = start; |
211 | 0 | token->end = parser->pos; |
212 | 0 | token->size = parser->pos - start + 1; |
213 | 0 | token->parent_id = parser->curr_tok_idx; |
214 | 0 | } |
215 | 0 | } |
216 | | |
217 | | static void |
218 | 0 | cj5__parse_key(cj5__parser* parser) { |
219 | 0 | const char* json5 = parser->json5; |
220 | 0 | unsigned int start = parser->pos; |
221 | 0 | cj5_token* token; |
222 | | |
223 | | // Key is a a normal string |
224 | 0 | if(json5[start] == '\"' || json5[start] == '\'') { |
225 | 0 | cj5__parse_string(parser); |
226 | 0 | return; |
227 | 0 | } |
228 | | |
229 | | // An unquoted key. Must start with a-ZA-Z_$. Can contain numbers later on. |
230 | 0 | unsigned int len = parser->len; |
231 | 0 | for(; parser->pos < len; parser->pos++) { |
232 | 0 | if(cj5__islowerchar(json5[parser->pos]) || |
233 | 0 | cj5__isupperchar(json5[parser->pos]) || |
234 | 0 | json5[parser->pos] == '_' || json5[parser->pos] == '$') |
235 | 0 | continue; |
236 | 0 | if(cj5__isnum(json5[parser->pos]) && parser->pos != start) |
237 | 0 | continue; |
238 | 0 | break; |
239 | 0 | } |
240 | | |
241 | | // An empty key is not allowed |
242 | 0 | if(parser->pos <= start) { |
243 | 0 | parser->error = CJ5_ERROR_INVALID; |
244 | 0 | return; |
245 | 0 | } |
246 | | |
247 | | // Move pos to the last character within the unquoted key |
248 | 0 | parser->pos--; |
249 | |
|
250 | 0 | token = cj5__alloc_token(parser); |
251 | 0 | if(token) { |
252 | 0 | token->type = CJ5_TOKEN_STRING; |
253 | 0 | token->start = start; |
254 | 0 | token->end = parser->pos; |
255 | 0 | token->size = parser->pos - start + 1; |
256 | 0 | token->parent_id = parser->curr_tok_idx; |
257 | 0 | } |
258 | 0 | } |
259 | | |
260 | | static void |
261 | 0 | cj5__skip_comment(cj5__parser* parser) { |
262 | 0 | const char* json5 = parser->json5; |
263 | | |
264 | | // Single-line comment |
265 | 0 | if(json5[parser->pos] == '#') { |
266 | 0 | skip_line: |
267 | 0 | while(parser->pos < parser->len) { |
268 | 0 | if(json5[parser->pos] == '\n') { |
269 | 0 | parser->pos--; // Reparse the newline in the main parse loop |
270 | 0 | return; |
271 | 0 | } |
272 | 0 | parser->pos++; |
273 | 0 | } |
274 | 0 | return; |
275 | 0 | } |
276 | | |
277 | | // Comment begins with '/' but not enough space for another character |
278 | 0 | if(parser->pos + 1 >= parser->len) { |
279 | 0 | parser->error = CJ5_ERROR_INVALID; |
280 | 0 | return; |
281 | 0 | } |
282 | 0 | parser->pos++; |
283 | | |
284 | | // Comment begins with '//' -> single-line comment |
285 | 0 | if(json5[parser->pos] == '/') |
286 | 0 | goto skip_line; |
287 | | |
288 | | // Multi-line comments begin with '/*' and end with '*/' |
289 | 0 | if(json5[parser->pos] == '*') { |
290 | 0 | parser->pos++; |
291 | 0 | for(; parser->pos + 1 < parser->len; parser->pos++) { |
292 | 0 | if(json5[parser->pos] == '*' && json5[parser->pos + 1] == '/') { |
293 | 0 | parser->pos++; |
294 | 0 | return; |
295 | 0 | } |
296 | 0 | } |
297 | 0 | } |
298 | | |
299 | | // Unknown comment type or the multi-line comment is not terminated |
300 | 0 | parser->error = CJ5_ERROR_INCOMPLETE; |
301 | 0 | } |
302 | | |
303 | | cj5_result |
304 | | cj5_parse(const char *json5, unsigned int len, |
305 | | cj5_token *tokens, unsigned int max_tokens, |
306 | 0 | cj5_options *options) { |
307 | 0 | cj5_result r; |
308 | 0 | cj5__parser parser; |
309 | 0 | memset(&parser, 0x0, sizeof(parser)); |
310 | 0 | parser.curr_tok_idx = 0; |
311 | 0 | parser.json5 = json5; |
312 | 0 | parser.len = len; |
313 | 0 | parser.tokens = tokens; |
314 | 0 | parser.max_tokens = max_tokens; |
315 | |
|
316 | 0 | if(options) |
317 | 0 | parser.stop_early = options->stop_early; |
318 | |
|
319 | 0 | unsigned short depth = 0; // Nesting depth zero means "outside the root object" |
320 | 0 | char nesting[CJ5_MAX_NESTING]; // Contains either '\0', '{' or '[' for the |
321 | | // type of nesting at each depth. '\0' |
322 | | // indicates we are out of the root object. |
323 | 0 | char next[CJ5_MAX_NESTING]; // Next content to parse: 'k' (key), ':', 'v' |
324 | | // (value) or ',' (comma). |
325 | 0 | next[0] = 'v'; // The root is a "value" (object, array or primitive). If we |
326 | | // detect a colon after the first value then everything is |
327 | | // wrapped into a "virtual root object" and the parsing is |
328 | | // restarted. |
329 | 0 | nesting[0] = 0; // Becomes '{' if there is a virtual root object |
330 | |
|
331 | 0 | cj5_token *token = NULL; // The current token |
332 | |
|
333 | 0 | start_parsing: |
334 | 0 | for(; parser.pos < len; parser.pos++) { |
335 | 0 | char c = json5[parser.pos]; |
336 | 0 | switch(c) { |
337 | 0 | case '\n': // Skip newline and whitespace |
338 | 0 | case '\r': |
339 | 0 | case '\t': |
340 | 0 | case ' ': |
341 | 0 | break; |
342 | | |
343 | 0 | case '#': // Skip comment |
344 | 0 | case '/': |
345 | 0 | cj5__skip_comment(&parser); |
346 | 0 | if(parser.error != CJ5_ERROR_NONE && |
347 | 0 | parser.error != CJ5_ERROR_OVERFLOW) |
348 | 0 | goto finish; |
349 | 0 | break; |
350 | | |
351 | 0 | case '{': // Open an object or array |
352 | 0 | case '[': |
353 | | // Check the nesting depth |
354 | 0 | if(depth + 1 >= CJ5_MAX_NESTING) { |
355 | 0 | parser.error = CJ5_ERROR_INVALID; |
356 | 0 | goto finish; |
357 | 0 | } |
358 | | |
359 | | // Correct next? |
360 | 0 | if(next[depth] != 'v') { |
361 | 0 | parser.error = CJ5_ERROR_INVALID; |
362 | 0 | goto finish; |
363 | 0 | } |
364 | | |
365 | 0 | depth++; // Increase the nesting depth |
366 | 0 | nesting[depth] = c; // Set the nesting type |
367 | 0 | next[depth] = (c == '{') ? 'k' : 'v'; // next is either a key or a value |
368 | | |
369 | | // Create a token for the object or array |
370 | 0 | token = cj5__alloc_token(&parser); |
371 | 0 | if(token) { |
372 | 0 | token->parent_id = parser.curr_tok_idx; |
373 | 0 | token->type = (c == '{') ? CJ5_TOKEN_OBJECT : CJ5_TOKEN_ARRAY; |
374 | 0 | token->start = parser.pos; |
375 | 0 | token->size = 0; |
376 | 0 | parser.curr_tok_idx = parser.token_count - 1; // The new curr_tok_idx |
377 | | // is for this token |
378 | 0 | } |
379 | 0 | break; |
380 | | |
381 | 0 | case '}': // Close an object or array |
382 | 0 | case ']': |
383 | | // Check the nesting depth. Note that a "virtual root object" at |
384 | | // depth zero must not be closed. |
385 | 0 | if(depth == 0) { |
386 | 0 | parser.error = CJ5_ERROR_INVALID; |
387 | 0 | goto finish; |
388 | 0 | } |
389 | | |
390 | | // Check and adjust the nesting. Note that ']' - '[' == 2 and '}' - |
391 | | // '{' == 2. Arrays can always be closed. Objects can only close |
392 | | // when a key or a comma is expected. |
393 | 0 | if(c - nesting[depth] != 2 || |
394 | 0 | (c == '}' && next[depth] != 'k' && next[depth] != ',')) { |
395 | 0 | parser.error = CJ5_ERROR_INVALID; |
396 | 0 | goto finish; |
397 | 0 | } |
398 | | |
399 | 0 | if(token) { |
400 | | // Finalize the current token |
401 | 0 | token->end = parser.pos; |
402 | | |
403 | | // Move to the parent and increase the parent size. Omit this |
404 | | // when we leave the root (parent the same as the current |
405 | | // token). |
406 | 0 | if(parser.curr_tok_idx != token->parent_id) { |
407 | 0 | parser.curr_tok_idx = token->parent_id; |
408 | 0 | token = &tokens[token->parent_id]; |
409 | 0 | token->size++; |
410 | 0 | } |
411 | 0 | } |
412 | | |
413 | | // Step one level up |
414 | 0 | depth--; |
415 | 0 | next[depth] = (depth == 0) ? 0 : ','; // zero if we step out the root |
416 | | // object. then we do not look for |
417 | | // another element. |
418 | | |
419 | | // The first element was successfully parsed. Stop early or try to |
420 | | // parse the full input string? |
421 | 0 | if(depth == 0 && parser.stop_early) |
422 | 0 | goto finish; |
423 | | |
424 | 0 | break; |
425 | | |
426 | 0 | case ':': // Colon (between key and value) |
427 | 0 | if(next[depth] != ':') { |
428 | 0 | parser.error = CJ5_ERROR_INVALID; |
429 | 0 | goto finish; |
430 | 0 | } |
431 | 0 | next[depth] = 'v'; |
432 | 0 | break; |
433 | | |
434 | 0 | case ',': // Comma |
435 | 0 | if(next[depth] != ',') { |
436 | 0 | parser.error = CJ5_ERROR_INVALID; |
437 | 0 | goto finish; |
438 | 0 | } |
439 | 0 | next[depth] = (nesting[depth] == '{') ? 'k' : 'v'; |
440 | 0 | break; |
441 | | |
442 | 0 | default: // Value or key |
443 | 0 | if(next[depth] == 'v') { |
444 | 0 | cj5__parse_primitive(&parser); // Parse primitive value |
445 | 0 | if(nesting[depth] != 0) { |
446 | | // Parent is object or array |
447 | 0 | if(token) |
448 | 0 | token->size++; |
449 | 0 | next[depth] = ','; |
450 | 0 | } else { |
451 | | // The current value was the root element. Don't look for |
452 | | // any next element. |
453 | 0 | next[depth] = 0; |
454 | | |
455 | | // The first element was successfully parsed. Stop early or try to |
456 | | // parse the full input string? |
457 | 0 | if(parser.stop_early) |
458 | 0 | goto finish; |
459 | 0 | } |
460 | 0 | } else if(next[depth] == 'k') { |
461 | 0 | cj5__parse_key(&parser); |
462 | 0 | if(token) |
463 | 0 | token->size++; // Keys count towards the length |
464 | 0 | next[depth] = ':'; |
465 | 0 | } else { |
466 | 0 | parser.error = CJ5_ERROR_INVALID; |
467 | 0 | } |
468 | | |
469 | 0 | if(parser.error && parser.error != CJ5_ERROR_OVERFLOW) |
470 | 0 | goto finish; |
471 | | |
472 | 0 | break; |
473 | 0 | } |
474 | 0 | } |
475 | | |
476 | | // Are we back to the initial nesting depth? |
477 | 0 | if(depth != 0) { |
478 | 0 | parser.error = CJ5_ERROR_INCOMPLETE; |
479 | 0 | goto finish; |
480 | 0 | } |
481 | | |
482 | | // Close the virtual root object if there is one |
483 | 0 | if(nesting[0] == '{' && parser.error != CJ5_ERROR_OVERFLOW) { |
484 | | // Check the we end after a complete key-value pair (or dangling comma) |
485 | 0 | if(next[0] != 'k' && next[0] != ',') |
486 | 0 | parser.error = CJ5_ERROR_INVALID; |
487 | 0 | tokens[0].end = parser.pos - 1; |
488 | 0 | } |
489 | |
|
490 | 0 | finish: |
491 | | // If parsing failed at the initial nesting depth, create a virtual root object |
492 | | // and restart parsing. |
493 | 0 | if(parser.error != CJ5_ERROR_NONE && |
494 | 0 | parser.error != CJ5_ERROR_OVERFLOW && |
495 | 0 | depth == 0 && nesting[0] != '{') { |
496 | 0 | parser.token_count = 0; |
497 | 0 | token = cj5__alloc_token(&parser); |
498 | 0 | if(token) { |
499 | 0 | token->parent_id = 0; |
500 | 0 | token->type = CJ5_TOKEN_OBJECT; |
501 | 0 | token->start = 0; |
502 | 0 | token->size = 0; |
503 | |
|
504 | 0 | nesting[0] = '{'; |
505 | 0 | next[0] = 'k'; |
506 | |
|
507 | 0 | parser.curr_tok_idx = 0; |
508 | 0 | parser.pos = 0; |
509 | 0 | parser.error = CJ5_ERROR_NONE; |
510 | 0 | goto start_parsing; |
511 | 0 | } |
512 | 0 | } |
513 | | |
514 | 0 | memset(&r, 0x0, sizeof(r)); |
515 | 0 | r.error = parser.error; |
516 | 0 | r.error_pos = parser.pos; |
517 | 0 | r.num_tokens = parser.token_count; // How many tokens (would) have been |
518 | | // consumed by the parser? |
519 | | |
520 | | // Not a single token was parsed -> return an error |
521 | 0 | if(r.num_tokens == 0) |
522 | 0 | r.error = CJ5_ERROR_INCOMPLETE; |
523 | | |
524 | | // Set the tokens and original string only if successfully parsed |
525 | 0 | if(r.error == CJ5_ERROR_NONE) { |
526 | 0 | r.tokens = tokens; |
527 | 0 | r.json5 = json5; |
528 | 0 | } |
529 | |
|
530 | 0 | return r; |
531 | 0 | } |
532 | | |
533 | | cj5_error_code |
534 | 0 | cj5_get_bool(const cj5_result *r, unsigned int tok_index, bool *out) { |
535 | 0 | const cj5_token *token = &r->tokens[tok_index]; |
536 | 0 | if(token->type != CJ5_TOKEN_BOOL) |
537 | 0 | return CJ5_ERROR_INVALID; |
538 | 0 | *out = (r->json5[token->start] == 't'); |
539 | 0 | return CJ5_ERROR_NONE; |
540 | 0 | } |
541 | | |
542 | | cj5_error_code |
543 | 0 | cj5_get_float(const cj5_result *r, unsigned int tok_index, double *out) { |
544 | 0 | const cj5_token *token = &r->tokens[tok_index]; |
545 | 0 | if(token->type != CJ5_TOKEN_NUMBER) |
546 | 0 | return CJ5_ERROR_INVALID; |
547 | | |
548 | 0 | const char *tokstr = &r->json5[token->start]; |
549 | 0 | size_t toksize = token->end - token->start + 1; |
550 | 0 | if(toksize == 0) |
551 | 0 | return CJ5_ERROR_INVALID; |
552 | | |
553 | | // Skip prefixed +/- |
554 | 0 | bool neg = false; |
555 | 0 | if(tokstr[0] == '+' || tokstr[0] == '-') { |
556 | 0 | neg = (tokstr[0] == '-'); |
557 | 0 | tokstr++; |
558 | 0 | toksize--; |
559 | 0 | } |
560 | | |
561 | | // Detect prefixed inf/nan |
562 | 0 | if(strncmp(tokstr, "Infinity", toksize) == 0) { |
563 | 0 | *out = neg ? -INFINITY : INFINITY; |
564 | 0 | return CJ5_ERROR_NONE; |
565 | 0 | } else if(strncmp(tokstr, "NaN", toksize) == 0) { |
566 | 0 | *out = NAN; |
567 | 0 | return CJ5_ERROR_NONE; |
568 | 0 | } |
569 | | |
570 | | // reset the +/- detection and parse |
571 | 0 | tokstr = &r->json5[token->start]; |
572 | 0 | toksize = token->end - token->start + 1; |
573 | 0 | size_t parsed = parseDouble(tokstr, toksize, out); |
574 | | |
575 | | // There must only be whitespace between the end of the parsed number and |
576 | | // the end of the token |
577 | 0 | for(size_t i = parsed; i < toksize; i++) { |
578 | 0 | if(tokstr[i] != ' ' && tokstr[i] -'\t' >= 5) |
579 | 0 | return CJ5_ERROR_INVALID; |
580 | 0 | } |
581 | | |
582 | 0 | return (parsed != 0) ? CJ5_ERROR_NONE : CJ5_ERROR_INVALID; |
583 | 0 | } |
584 | | |
585 | | cj5_error_code |
586 | | cj5_get_int(const cj5_result *r, unsigned int tok_index, |
587 | 0 | int64_t *out) { |
588 | 0 | const cj5_token *token = &r->tokens[tok_index]; |
589 | 0 | if(token->type != CJ5_TOKEN_NUMBER) |
590 | 0 | return CJ5_ERROR_INVALID; |
591 | 0 | size_t parsed = parseInt64(&r->json5[token->start], token->size, out); |
592 | 0 | return (parsed != 0) ? CJ5_ERROR_NONE : CJ5_ERROR_INVALID; |
593 | 0 | } |
594 | | |
595 | | cj5_error_code |
596 | | cj5_get_uint(const cj5_result *r, unsigned int tok_index, |
597 | 0 | uint64_t *out) { |
598 | 0 | const cj5_token *token = &r->tokens[tok_index]; |
599 | 0 | if(token->type != CJ5_TOKEN_NUMBER) |
600 | 0 | return CJ5_ERROR_INVALID; |
601 | 0 | size_t parsed = parseUInt64(&r->json5[token->start], token->size, out); |
602 | 0 | return (parsed != 0) ? CJ5_ERROR_NONE : CJ5_ERROR_INVALID; |
603 | 0 | } |
604 | | |
605 | | static const uint32_t SURROGATE_OFFSET = 0x10000u - (0xD800u << 10) - 0xDC00; |
606 | | |
607 | | static cj5_error_code |
608 | 0 | parse_codepoint(const char *pos, uint32_t *out_utf) { |
609 | 0 | uint32_t utf = 0; |
610 | 0 | for(unsigned int i = 0; i < 4; i++) { |
611 | 0 | char byte = pos[i]; |
612 | 0 | if(cj5__isnum(byte)) { |
613 | 0 | byte = (char)(byte - '0'); |
614 | 0 | } else if(cj5__isrange(byte, 'a', 'f')) { |
615 | 0 | byte = (char)(byte - ('a' - 10)); |
616 | 0 | } else if(cj5__isrange(byte, 'A', 'F')) { |
617 | 0 | byte = (char)(byte - ('A' - 10)); |
618 | 0 | } else { |
619 | 0 | return CJ5_ERROR_INVALID; |
620 | 0 | } |
621 | 0 | utf = (utf << 4) | ((uint8_t)byte & 0xF); |
622 | 0 | } |
623 | 0 | *out_utf = utf; |
624 | 0 | return CJ5_ERROR_NONE; |
625 | 0 | } |
626 | | |
627 | | cj5_error_code |
628 | | cj5_get_str(const cj5_result *r, unsigned int tok_index, |
629 | 0 | char *buf, unsigned int *buflen) { |
630 | 0 | const cj5_token *token = &r->tokens[tok_index]; |
631 | 0 | if(token->type != CJ5_TOKEN_STRING) { |
632 | 0 | buf[0] = 0; |
633 | 0 | if(buflen) |
634 | 0 | *buflen = 0; |
635 | 0 | return CJ5_ERROR_INVALID; |
636 | 0 | } |
637 | | |
638 | 0 | const char *pos = &r->json5[token->start]; |
639 | 0 | const char *end = &r->json5[token->end + 1]; |
640 | 0 | unsigned int outpos = 0; |
641 | 0 | cj5_error_code error = CJ5_ERROR_NONE; |
642 | 0 | for(; pos < end; pos++) { |
643 | 0 | uint8_t c = (uint8_t)*pos; |
644 | | // Unprintable ascii characters must be escaped |
645 | 0 | if(c < ' ' || c == 127) { |
646 | 0 | error = CJ5_ERROR_INVALID; |
647 | 0 | goto done; |
648 | 0 | } |
649 | | |
650 | | // Unescaped Ascii character or utf8 byte |
651 | 0 | if(c != '\\') { |
652 | 0 | buf[outpos++] = (char)c; |
653 | 0 | continue; |
654 | 0 | } |
655 | | |
656 | | // End of input before the escaped character |
657 | 0 | if(pos + 1 >= end) { |
658 | 0 | error = CJ5_ERROR_INCOMPLETE; |
659 | 0 | goto done; |
660 | 0 | } |
661 | | |
662 | | // Process escaped character |
663 | 0 | pos++; |
664 | 0 | c = (uint8_t)*pos; |
665 | 0 | switch(c) { |
666 | 0 | case 'b': buf[outpos++] = '\b'; break; |
667 | 0 | case 'f': buf[outpos++] = '\f'; break; |
668 | 0 | case 'r': buf[outpos++] = '\r'; break; |
669 | 0 | case 'n': buf[outpos++] = '\n'; break; |
670 | 0 | case 't': buf[outpos++] = '\t'; break; |
671 | 0 | default: buf[outpos++] = (char)c; break; |
672 | 0 | case 'u': { |
673 | | // Parse a unicode code point |
674 | 0 | if(pos + 4 >= end) { |
675 | 0 | error = CJ5_ERROR_INCOMPLETE; |
676 | 0 | goto done; |
677 | 0 | } |
678 | 0 | pos++; |
679 | 0 | uint32_t utf; |
680 | 0 | error = parse_codepoint(pos, &utf); |
681 | 0 | if(error != CJ5_ERROR_NONE) |
682 | 0 | goto done; |
683 | 0 | pos += 3; |
684 | | |
685 | | // Parse a surrogate pair |
686 | 0 | if(0xd800 <= utf && utf <= 0xdfff) { |
687 | 0 | if(pos + 6 >= end) { |
688 | 0 | error = CJ5_ERROR_INVALID; |
689 | 0 | goto done; |
690 | 0 | } |
691 | 0 | if(pos[1] != '\\' && pos[2] != 'u') { |
692 | 0 | error = CJ5_ERROR_INVALID; |
693 | 0 | goto done; |
694 | 0 | } |
695 | 0 | pos += 3; |
696 | 0 | uint32_t utf2; |
697 | 0 | error = parse_codepoint(pos, &utf2); |
698 | 0 | if(error != CJ5_ERROR_NONE) |
699 | 0 | goto done; |
700 | 0 | pos += 3; |
701 | | // High or low surrogate pair |
702 | 0 | utf = (utf <= 0xdbff) ? |
703 | 0 | (utf << 10) + utf2 + SURROGATE_OFFSET : |
704 | 0 | (utf2 << 10) + utf + SURROGATE_OFFSET; |
705 | 0 | } |
706 | | |
707 | | // Write the utf8 bytes of the code point |
708 | 0 | unsigned len = utf8_from_codepoint((unsigned char*)buf + outpos, utf); |
709 | 0 | if(len == 0) { |
710 | 0 | error = CJ5_ERROR_INVALID; // Not a utf8 string |
711 | 0 | goto done; |
712 | 0 | } |
713 | 0 | outpos += len; |
714 | 0 | break; |
715 | 0 | } |
716 | 0 | } |
717 | 0 | } |
718 | | |
719 | 0 | done: |
720 | | // Always leave buf as a valid, NUL-terminated string, even when decoding |
721 | | // fails midway. Callers still must check the returned error code. |
722 | 0 | buf[outpos] = 0; |
723 | | |
724 | | // Set the output length |
725 | 0 | if(buflen) |
726 | 0 | *buflen = outpos; |
727 | 0 | return error; |
728 | 0 | } |
729 | | |
730 | | void |
731 | 0 | cj5_skip(const cj5_result *r, unsigned int *tok_index) { |
732 | 0 | unsigned int idx = *tok_index; |
733 | 0 | unsigned int end = r->tokens[idx].end; |
734 | 0 | do { idx++; } while(idx < r->num_tokens && |
735 | 0 | r->tokens[idx].start < end); |
736 | 0 | *tok_index = idx; |
737 | 0 | } |
738 | | |
739 | | cj5_error_code |
740 | | cj5_find(const cj5_result *r, unsigned int *tok_index, |
741 | 0 | const char *key) { |
742 | | // It has to be an object |
743 | 0 | unsigned int idx = *tok_index; |
744 | 0 | if(r->tokens[idx].type != CJ5_TOKEN_OBJECT) |
745 | 0 | return CJ5_ERROR_INVALID; |
746 | 0 | unsigned int size = r->tokens[idx].size; |
747 | | |
748 | | // Skip to the first key |
749 | 0 | idx++; |
750 | | |
751 | | // Size is number of keys + number of values |
752 | 0 | for(unsigned int i = 0; i < size; i += 2) { |
753 | | // Key has to be a string |
754 | 0 | if(r->tokens[idx].type != CJ5_TOKEN_STRING) |
755 | 0 | return CJ5_ERROR_INVALID; |
756 | | |
757 | | // Return the index to the value if the key matches |
758 | 0 | const char *keystart = &r->json5[r->tokens[idx].start]; |
759 | 0 | size_t keysize = r->tokens[idx].end - r->tokens[idx].start + 1; |
760 | 0 | if(strncmp(key, keystart, keysize) == 0) { |
761 | 0 | *tok_index = idx + 1; |
762 | 0 | return CJ5_ERROR_NONE; |
763 | 0 | } |
764 | | |
765 | | // Skip over the value |
766 | 0 | idx++; |
767 | 0 | cj5_skip(r, &idx); |
768 | 0 | } |
769 | 0 | return CJ5_ERROR_NOTFOUND; |
770 | 0 | } |