blob: 09b6303abf9f927ca5321fcf0e76b2c36364f51c [file] [log] [blame]
Radek Krejci50f0c6b2020-06-18 16:31:48 +02001/**
2 * @file json.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Generic JSON format parser for libyang
5 *
6 * Copyright (c) 2020 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#include <assert.h>
16#include <ctype.h>
17#include <errno.h>
Radek Krejci50f0c6b2020-06-18 16:31:48 +020018#include <stdlib.h>
Radek Krejci47fab892020-11-05 17:02:41 +010019#include <string.h>
Radek Krejci50f0c6b2020-06-18 16:31:48 +020020#include <sys/types.h>
21
22#include "common.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020023#include "in_internal.h"
Radek Krejci47fab892020-11-05 17:02:41 +010024#include "json.h"
Radek Krejci50f0c6b2020-06-18 16:31:48 +020025
26#define JSON_PUSH_STATUS_RET(CTX, STATUS) \
Radek Krejci3d92e442020-10-12 12:48:13 +020027 LY_CHECK_RET(ly_set_add(&CTX->status, (void*)STATUS, 1, NULL))
Radek Krejci50f0c6b2020-06-18 16:31:48 +020028
29#define JSON_POP_STATUS_RET(CTX) \
30 assert(CTX->status.count); CTX->status.count--;
31
Michal Vasko22df3f02020-08-24 13:29:22 +020032const char *
Radek Krejci50f0c6b2020-06-18 16:31:48 +020033lyjson_token2str(enum LYJSON_PARSER_STATUS status)
34{
35 switch (status) {
36 case LYJSON_ERROR:
37 return "error";
38 case LYJSON_ROOT:
39 return "document root";
40 case LYJSON_FALSE:
41 return "false";
42 case LYJSON_TRUE:
43 return "true";
44 case LYJSON_NULL:
45 return "null";
46 case LYJSON_OBJECT:
47 return "object";
48 case LYJSON_OBJECT_CLOSED:
49 return "object closed";
50 case LYJSON_OBJECT_EMPTY:
51 return "empty object";
52 case LYJSON_ARRAY:
53 return "array";
54 case LYJSON_ARRAY_CLOSED:
55 return "array closed";
56 case LYJSON_ARRAY_EMPTY:
57 return "empty array";
58 case LYJSON_NUMBER:
59 return "number";
60 case LYJSON_STRING:
61 return "string";
62 case LYJSON_END:
63 return "end of input";
64 }
65
66 return "";
67}
68
69static LY_ERR
70skip_ws(struct lyjson_ctx *jsonctx)
71{
72 /* skip leading whitespaces */
73 while (*jsonctx->in->current != '\0' && is_jsonws(*jsonctx->in->current)) {
Radek Krejcidd713ce2021-01-04 23:12:12 +010074 if (*jsonctx->in->current == '\n') {
75 LY_IN_NEW_LINE(jsonctx->in);
76 }
Radek Krejci50f0c6b2020-06-18 16:31:48 +020077 ly_in_skip(jsonctx->in, 1);
78 }
79 if (*jsonctx->in->current == '\0') {
80 JSON_PUSH_STATUS_RET(jsonctx, LYJSON_END);
81 }
82
83 return LY_SUCCESS;
84}
85
86/*
87 * @brief Set value corresponding to the current context's status
88 */
89static void
Radek Krejci857189e2020-09-01 13:26:36 +020090lyjson_ctx_set_value(struct lyjson_ctx *jsonctx, const char *value, size_t value_len, ly_bool dynamic)
Radek Krejci50f0c6b2020-06-18 16:31:48 +020091{
92 assert(jsonctx);
93
Juraj Vijtiukec285cd2021-01-14 11:41:20 +010094 if (jsonctx->dynamic) {
Michal Vasko22df3f02020-08-24 13:29:22 +020095 free((char *)jsonctx->value);
Radek Krejci50f0c6b2020-06-18 16:31:48 +020096 }
97 jsonctx->value = value;
98 jsonctx->value_len = value_len;
99 jsonctx->dynamic = dynamic;
100}
101
102static LY_ERR
103lyjson_check_next(struct lyjson_ctx *jsonctx)
104{
105 if (jsonctx->status.count == 1) {
106 /* top level value (JSON-text), ws expected */
Michal Vasko69730152020-10-09 16:30:07 +0200107 if ((*jsonctx->in->current == '\0') || is_jsonws(*jsonctx->in->current)) {
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200108 return LY_SUCCESS;
109 }
110 } else if (lyjson_ctx_status(jsonctx, 1) == LYJSON_OBJECT) {
111 LY_CHECK_RET(skip_ws(jsonctx));
Michal Vasko69730152020-10-09 16:30:07 +0200112 if ((*jsonctx->in->current == ',') || (*jsonctx->in->current == '}')) {
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200113 return LY_SUCCESS;
114 }
115 } else if (lyjson_ctx_status(jsonctx, 1) == LYJSON_ARRAY) {
116 LY_CHECK_RET(skip_ws(jsonctx));
Michal Vasko69730152020-10-09 16:30:07 +0200117 if ((*jsonctx->in->current == ',') || (*jsonctx->in->current == ']')) {
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200118 return LY_SUCCESS;
119 }
120 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100121 LOGVAL(jsonctx->ctx, LYVE_SYNTAX, "Unexpected character \"%c\" after JSON %s.",
122 *jsonctx->in->current, lyjson_token2str(lyjson_ctx_status(jsonctx, 0)));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200123 }
124
125 return LY_EVALID;
126}
127
128/**
129 * Input is expected to start after the opening quotation-mark.
130 * When succeeds, input is moved after the closing quotation-mark.
131 */
132static LY_ERR
133lyjson_string_(struct lyjson_ctx *jsonctx)
134{
135#define BUFSIZE 24
136#define BUFSIZE_STEP 128
137
138 const char *in = jsonctx->in->current, *start;
139 char *buf = NULL;
140 size_t offset; /* read offset in input buffer */
141 size_t len; /* length of the output string (write offset in output buffer) */
142 size_t size = 0; /* size of the output buffer */
143 size_t u;
144 uint64_t start_line;
145
146 assert(jsonctx);
147
148 /* init */
149 start = in;
Radek Krejcid54412f2020-12-17 20:25:35 +0100150 start_line = jsonctx->in->line;
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200151 offset = len = 0;
152
153 /* parse */
154 while (in[offset]) {
155 if (in[offset] == '\\') {
156 /* escape sequence */
157 size_t slash = offset;
158 uint32_t value;
159 uint8_t i = 1;
160
161 if (!buf) {
162 /* prepare output buffer */
163 buf = malloc(BUFSIZE);
164 LY_CHECK_ERR_RET(!buf, LOGMEM(jsonctx->ctx), LY_EMEM);
165 size = BUFSIZE;
166 }
167
168 /* allocate enough for the offset and next character,
169 * we will need 4 bytes at most since we support only the predefined
170 * (one-char) entities and character references */
171 if (len + offset + 4 >= size) {
Juraj Vijtiukd746a352021-01-15 11:33:33 +0100172 size_t increment;
Radek Krejcidf549132021-01-21 10:32:32 +0100173 for (increment = BUFSIZE_STEP; len + offset + 4 >= size + increment; increment += BUFSIZE_STEP) {}
Juraj Vijtiukd746a352021-01-15 11:33:33 +0100174 buf = ly_realloc(buf, size + increment);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200175 LY_CHECK_ERR_RET(!buf, LOGMEM(jsonctx->ctx), LY_EMEM);
176 size += BUFSIZE_STEP;
177 }
178
179 if (offset) {
180 /* store what we have so far */
181 memcpy(&buf[len], in, offset);
182 len += offset;
183 in += offset;
184 offset = 0;
185 }
186
187 switch (in[++offset]) {
188 case '"':
189 /* quotation mark */
190 value = 0x22;
191 break;
192 case '\\':
193 /* reverse solidus */
194 value = 0x5c;
195 break;
196 case '/':
197 /* solidus */
198 value = 0x2f;
199 break;
200 case 'b':
201 /* backspace */
202 value = 0x08;
203 break;
204 case 'f':
205 /* form feed */
206 value = 0x0c;
207 break;
208 case 'n':
209 /* line feed */
210 value = 0x0a;
211 break;
212 case 'r':
213 /* carriage return */
214 value = 0x0d;
215 break;
216 case 't':
217 /* tab */
218 value = 0x09;
219 break;
220 case 'u':
221 /* Basic Multilingual Plane character \uXXXX */
222 offset++;
223 for (value = i = 0; i < 4; i++) {
Juraj Vijtiuk2b94e4b2020-11-16 23:52:07 +0100224 if (!in[offset + i]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100225 LOGVAL(jsonctx->ctx, LYVE_SYNTAX, "Invalid basic multilingual plane character \"%s\".", &in[slash]);
Juraj Vijtiuk2b94e4b2020-11-16 23:52:07 +0100226 goto error;
227 } else if (isdigit(in[offset + i])) {
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200228 u = (in[offset + i] - '0');
229 } else if (in[offset + i] > 'F') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100230 u = LY_BASE_DEC + (in[offset + i] - 'a');
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200231 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100232 u = LY_BASE_DEC + (in[offset + i] - 'A');
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200233 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100234 value = (LY_BASE_HEX * value) + u;
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200235 }
236 break;
237 default:
238 /* invalid escape sequence */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100239 LOGVAL(jsonctx->ctx, LYVE_SYNTAX, "Invalid character escape sequence \\%c.", in[offset]);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200240 goto error;
241
242 }
243
244 offset += i; /* add read escaped characters */
245 LY_CHECK_ERR_GOTO(ly_pututf8(&buf[len], value, &u),
Radek Krejci2efc45b2020-12-22 16:25:44 +0100246 LOGVAL(jsonctx->ctx, LYVE_SYNTAX, "Invalid character reference \"%.*s\" (0x%08x).",
247 offset - slash, &in[slash], value),
Michal Vasko69730152020-10-09 16:30:07 +0200248 error);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200249 len += u; /* update number of bytes in buffer */
250 in += offset; /* move the input by the processed bytes stored in the buffer ... */
251 offset = 0; /* ... and reset the offset index for future moving data into buffer */
252
253 } else if (in[offset] == '"') {
254 /* end of string */
255 if (buf) {
256 /* realloc exact size string */
257 buf = ly_realloc(buf, len + offset + 1);
258 LY_CHECK_ERR_RET(!buf, LOGMEM(jsonctx->ctx), LY_EMEM);
259 size = len + offset + 1;
260 memcpy(&buf[len], in, offset);
261
262 /* set terminating NULL byte */
263 buf[len + offset] = '\0';
264 }
265 len += offset;
266 ++offset;
267 in += offset;
268 goto success;
269 } else {
270 /* get it as UTF-8 character for check */
271 const char *c = &in[offset];
272 uint32_t code = 0;
273 size_t code_len = 0;
274
275 LY_CHECK_ERR_GOTO(ly_getutf8(&c, &code, &code_len),
Radek Krejci2efc45b2020-12-22 16:25:44 +0100276 LOGVAL(jsonctx->ctx, LY_VCODE_INCHAR, in[offset]), error);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200277
278 LY_CHECK_ERR_GOTO(!is_jsonstrchar(code),
Radek Krejci2efc45b2020-12-22 16:25:44 +0100279 LOGVAL(jsonctx->ctx, LYVE_SYNTAX, "Invalid character in JSON string \"%.*s\" (0x%08x).",
280 &in[offset] - start + code_len, start, code),
Michal Vasko69730152020-10-09 16:30:07 +0200281 error);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200282
283 /* character is ok, continue */
284 offset += code_len;
285 }
286 }
287
288 /* EOF reached before endchar */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100289 LOGVAL(jsonctx->ctx, LY_VCODE_EOF);
290 LOGVAL_LINE(jsonctx->ctx, start_line, LYVE_SYNTAX, "Missing quotation-mark at the end of a JSON string.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200291
292error:
293 free(buf);
294 return LY_EVALID;
295
296success:
Radek Krejcid54412f2020-12-17 20:25:35 +0100297 jsonctx->in->current = in;
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200298 if (buf) {
299 lyjson_ctx_set_value(jsonctx, buf, len, 1);
300 } else {
301 lyjson_ctx_set_value(jsonctx, start, len, 0);
302 }
303
304 return LY_SUCCESS;
305
306#undef BUFSIZE
307#undef BUFSIZE_STEP
308}
309
310/*
311 *
312 * Wrapper around lyjson_string_() adding LYJSON_STRING status into context to allow using lyjson_string_() for parsing object's name.
313 */
314static LY_ERR
315lyjson_string(struct lyjson_ctx *jsonctx)
316{
317 LY_CHECK_RET(lyjson_string_(jsonctx));
318
319 JSON_PUSH_STATUS_RET(jsonctx, LYJSON_STRING);
320 LY_CHECK_RET(lyjson_check_next(jsonctx));
321
322 return LY_SUCCESS;
323}
324
325static LY_ERR
326lyjson_number(struct lyjson_ctx *jsonctx)
327{
328 size_t offset = 0, exponent = 0;
329 const char *in = jsonctx->in->current;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200330 uint8_t minus = 0;
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200331
332 if (in[offset] == '-') {
333 ++offset;
334 minus = 1;
335 }
336
337 if (in[offset] == '0') {
338 ++offset;
339 } else if (isdigit(in[offset])) {
340 ++offset;
341 while (isdigit(in[offset])) {
342 ++offset;
343 }
344 } else {
345invalid_character:
346 if (in[offset]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100347 LOGVAL(jsonctx->ctx, LYVE_SYNTAX, "Invalid character in JSON Number value (\"%c\").", in[offset]);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200348 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100349 LOGVAL(jsonctx->ctx, LY_VCODE_EOF);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200350 }
351 return LY_EVALID;
352 }
353
354 if (in[offset] == '.') {
355 ++offset;
356 if (!isdigit(in[offset])) {
357 goto invalid_character;
358 }
359 while (isdigit(in[offset])) {
360 ++offset;
361 }
362 }
363
364 if ((in[offset] == 'e') || (in[offset] == 'E')) {
365 exponent = offset++;
366 if ((in[offset] == '+') || (in[offset] == '-')) {
367 ++offset;
368 }
369 if (!isdigit(in[offset])) {
370 goto invalid_character;
371 }
372 while (isdigit(in[offset])) {
373 ++offset;
374 }
375 }
376
377 if (exponent) {
378 /* convert JSON number with exponent into the representation used by YANG */
379 long int e_val;
380 char *ptr, *dec_point, *num;
381 const char *e_ptr = &in[exponent + 1];
382 size_t num_len, i;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200383 int64_t dp_position; /* final position of the deciaml point */
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200384
385 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100386 e_val = strtol(e_ptr, &ptr, LY_BASE_DEC);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200387 if (errno) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100388 LOGVAL(jsonctx->ctx, LYVE_SEMANTICS, "Exponent out-of-bounds in a JSON Number value (%.*s).",
389 offset - minus - (e_ptr - in), e_ptr);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200390 return LY_EVALID;
391 }
392
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200393 dec_point = ly_strnchr(in, '.', exponent);
394 if (!dec_point) {
395 /* value is integer, we are just ... */
396 if (e_val >= 0) {
397 /* adding zeros at the end */
398 num_len = exponent + e_val;
399 dp_position = num_len; /* decimal point is behind the actual value */
400 } else if ((size_t)labs(e_val) < exponent) {
401 /* adding decimal point between the integer's digits */
402 num_len = exponent + 1;
403 dp_position = exponent + e_val;
404 } else {
405 /* adding decimal point before the integer with adding leading zero(s) */
406 num_len = labs(e_val) + 2;
407 dp_position = exponent + e_val;
408 }
409 dp_position -= minus;
410 } else {
411 /* value is decimal, we are moving the decimal point */
412 dp_position = dec_point - in + e_val - minus;
413 if (dp_position > (ssize_t)exponent) {
414 /* moving decimal point after the decimal value make the integer result */
415 num_len = dp_position;
416 } else if (dp_position < 0) {
417 /* moving decimal point before the decimal value requires additional zero(s)
418 * (decimal point is already count in exponent value) */
419 num_len = exponent + labs(dp_position) + 1;
420 } else {
421 /* moving decimal point just inside the decimal value does not make any change in length */
422 num_len = exponent;
423 }
424 }
425
426 /* allocate buffer for the result (add terminating NULL-byte */
427 num = malloc(num_len + 1);
428 LY_CHECK_ERR_RET(!num, LOGMEM(jsonctx->ctx), LY_EMEM);
429
430 /* compose the resulting vlaue */
431 i = 0;
432 if (minus) {
433 num[i++] = '-';
434 }
435 /* add leading zeros */
436 if (dp_position <= 0) {
437 num[i++] = '0';
438 num[i++] = '.';
Michal Vaskod989ba02020-08-24 10:59:24 +0200439 for ( ; dp_position; dp_position++) {
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200440 num[i++] = '0';
441 }
442 }
443 /* copy the value */
Radek Krejci857189e2020-09-01 13:26:36 +0200444 ly_bool dp_placed;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200445 size_t j;
446 for (dp_placed = dp_position ? 0 : 1, j = minus; j < exponent; j++) {
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200447 if (in[j] == '.') {
448 continue;
449 }
450 if (!dp_placed) {
451 if (!dp_position) {
452 num[i++] = '.';
453 dp_placed = 1;
454 } else {
455 dp_position--;
456 if (in[j] == '0') {
457 num_len--;
458 continue;
459 }
460 }
461 }
462
463 num[i++] = in[j];
464 }
465 /* trailing zeros */
466 while (dp_position--) {
467 num[i++] = '0';
468 }
469 /* terminating NULL byte */
470 num[i] = '\0';
471
472 /* store the modified number */
473 lyjson_ctx_set_value(jsonctx, num, num_len, 1);
474 } else {
475 /* store the number */
476 lyjson_ctx_set_value(jsonctx, jsonctx->in->current, offset, 0);
477 }
478 ly_in_skip(jsonctx->in, offset);
479
480 JSON_PUSH_STATUS_RET(jsonctx, LYJSON_NUMBER);
481 LY_CHECK_RET(lyjson_check_next(jsonctx));
482
483 return LY_SUCCESS;
484}
485
486static LY_ERR
487lyjson_object_name(struct lyjson_ctx *jsonctx)
488{
489 if (*jsonctx->in->current != '"') {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100490 LOGVAL(jsonctx->ctx, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(jsonctx->in->current),
Michal Vasko69730152020-10-09 16:30:07 +0200491 jsonctx->in->current, "a JSON object's member");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200492 return LY_EVALID;
493 }
494 ly_in_skip(jsonctx->in, 1);
495
496 LY_CHECK_RET(lyjson_string_(jsonctx));
497 LY_CHECK_RET(skip_ws(jsonctx));
Michal Vasko08dc70b2020-10-07 13:58:47 +0200498 if (*jsonctx->in->current != ':') {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100499 LOGVAL(jsonctx->ctx, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(jsonctx->in->current), jsonctx->in->current,
500 "a JSON object's name-separator ':'");
Michal Vasko08dc70b2020-10-07 13:58:47 +0200501 return LY_EVALID;
502 }
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200503 ly_in_skip(jsonctx->in, 1);
504 LY_CHECK_RET(skip_ws(jsonctx));
505
506 return LY_SUCCESS;
507}
508
509static LY_ERR
510lyjson_object(struct lyjson_ctx *jsonctx)
511{
512 LY_CHECK_RET(skip_ws(jsonctx));
513
514 if (*jsonctx->in->current == '}') {
515 /* empty object */
516 ly_in_skip(jsonctx->in, 1);
517 lyjson_ctx_set_value(jsonctx, NULL, 0, 0);
518 JSON_PUSH_STATUS_RET(jsonctx, LYJSON_OBJECT_EMPTY);
519 return LY_SUCCESS;
520 }
521
522 LY_CHECK_RET(lyjson_object_name(jsonctx));
523
524 /* output data are set by lyjson_string_() */
525 JSON_PUSH_STATUS_RET(jsonctx, LYJSON_OBJECT);
526
527 return LY_SUCCESS;
528}
529
530/*
531 * @brief Process JSON array envelope
532 *
533 *
534 *
535 * @param[in] jsonctx JSON parser context
536 * @return LY_SUCCESS or LY_EMEM
537 */
538static LY_ERR
539lyjson_array(struct lyjson_ctx *jsonctx)
540{
541 LY_CHECK_RET(skip_ws(jsonctx));
542
543 if (*jsonctx->in->current == ']') {
544 /* empty array */
545 ly_in_skip(jsonctx->in, 1);
546 JSON_PUSH_STATUS_RET(jsonctx, LYJSON_ARRAY_EMPTY);
547 } else {
548 JSON_PUSH_STATUS_RET(jsonctx, LYJSON_ARRAY);
549 }
550
551 /* erase previous values, array has no value on its own */
552 lyjson_ctx_set_value(jsonctx, NULL, 0, 0);
553
554 return LY_SUCCESS;
555}
556
557static LY_ERR
558lyjson_value(struct lyjson_ctx *jsonctx)
559{
Michal Vasko69730152020-10-09 16:30:07 +0200560 if (jsonctx->status.count && (lyjson_ctx_status(jsonctx, 0) == LYJSON_END)) {
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200561 return LY_SUCCESS;
562 }
563
Radek Krejcif13b87b2020-12-01 22:02:17 +0100564 if ((*jsonctx->in->current == 'f') && !strncmp(jsonctx->in->current, "false", ly_strlen_const("false"))) {
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200565 /* false */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100566 lyjson_ctx_set_value(jsonctx, jsonctx->in->current, ly_strlen_const("false"), 0);
567 ly_in_skip(jsonctx->in, ly_strlen_const("false"));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200568 JSON_PUSH_STATUS_RET(jsonctx, LYJSON_FALSE);
569 LY_CHECK_RET(lyjson_check_next(jsonctx));
570
Radek Krejcif13b87b2020-12-01 22:02:17 +0100571 } else if ((*jsonctx->in->current == 't') && !strncmp(jsonctx->in->current, "true", ly_strlen_const("true"))) {
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200572 /* true */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100573 lyjson_ctx_set_value(jsonctx, jsonctx->in->current, ly_strlen_const("true"), 0);
574 ly_in_skip(jsonctx->in, ly_strlen_const("true"));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200575 JSON_PUSH_STATUS_RET(jsonctx, LYJSON_TRUE);
576 LY_CHECK_RET(lyjson_check_next(jsonctx));
577
Radek Krejcif13b87b2020-12-01 22:02:17 +0100578 } else if ((*jsonctx->in->current == 'n') && !strncmp(jsonctx->in->current, "null", ly_strlen_const("null"))) {
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200579 /* none */
Radek Krejci201963a2020-12-03 11:43:40 +0100580 lyjson_ctx_set_value(jsonctx, "", 0, 0);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100581 ly_in_skip(jsonctx->in, ly_strlen_const("null"));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200582 JSON_PUSH_STATUS_RET(jsonctx, LYJSON_NULL);
583 LY_CHECK_RET(lyjson_check_next(jsonctx));
584
585 } else if (*jsonctx->in->current == '"') {
586 /* string */
587 ly_in_skip(jsonctx->in, 1);
588 LY_CHECK_RET(lyjson_string(jsonctx));
589
590 } else if (*jsonctx->in->current == '[') {
591 /* array */
592 ly_in_skip(jsonctx->in, 1);
593 LY_CHECK_RET(lyjson_array(jsonctx));
594
595 } else if (*jsonctx->in->current == '{') {
596 /* object */
597 ly_in_skip(jsonctx->in, 1);
598 LY_CHECK_RET(lyjson_object(jsonctx));
599
Michal Vasko69730152020-10-09 16:30:07 +0200600 } else if ((*jsonctx->in->current == '-') || ((*jsonctx->in->current >= '0') && (*jsonctx->in->current <= '9'))) {
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200601 /* number */
602 LY_CHECK_RET(lyjson_number(jsonctx));
603
604 } else {
605 /* unexpected value */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100606 LOGVAL(jsonctx->ctx, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(jsonctx->in->current),
Michal Vasko69730152020-10-09 16:30:07 +0200607 jsonctx->in->current, "a JSON value");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200608 return LY_EVALID;
609 }
610
611 return LY_SUCCESS;
612}
613
614LY_ERR
615lyjson_ctx_new(const struct ly_ctx *ctx, struct ly_in *in, struct lyjson_ctx **jsonctx_p)
616{
617 LY_ERR ret = LY_SUCCESS;
618 struct lyjson_ctx *jsonctx;
619
620 assert(ctx);
621 assert(in);
622 assert(jsonctx_p);
623
624 /* new context */
625 jsonctx = calloc(1, sizeof *jsonctx);
626 LY_CHECK_ERR_RET(!jsonctx, LOGMEM(ctx), LY_EMEM);
627 jsonctx->ctx = ctx;
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200628 jsonctx->in = in;
629
Radek Krejciddace2c2021-01-08 11:30:56 +0100630 LOG_LOCINIT(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100631
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200632 /* parse JSON value, if any */
633 LY_CHECK_GOTO(ret = skip_ws(jsonctx), cleanup);
634 if (lyjson_ctx_status(jsonctx, 0) == LYJSON_END) {
635 /* empty data input */
636 goto cleanup;
637 }
638
639 ret = lyjson_value(jsonctx);
640
Michal Vasko69730152020-10-09 16:30:07 +0200641 if ((jsonctx->status.count > 1) && (lyjson_ctx_status(jsonctx, 0) == LYJSON_END)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100642 LOGVAL(jsonctx->ctx, LY_VCODE_EOF);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200643 ret = LY_EVALID;
644 }
645
646cleanup:
647 if (ret) {
648 lyjson_ctx_free(jsonctx);
649 } else {
650 *jsonctx_p = jsonctx;
651 }
652 return ret;
653}
654
655void
656lyjson_ctx_backup(struct lyjson_ctx *jsonctx)
657{
658 if (jsonctx->backup.dynamic) {
659 free((char *)jsonctx->backup.value);
660 }
661 jsonctx->backup.status = lyjson_ctx_status(jsonctx, 0);
662 jsonctx->backup.status_count = jsonctx->status.count;
663 jsonctx->backup.value = jsonctx->value;
664 jsonctx->backup.value_len = jsonctx->value_len;
665 jsonctx->backup.input = jsonctx->in->current;
666 jsonctx->backup.dynamic = jsonctx->dynamic;
667 jsonctx->dynamic = 0;
668}
669
670void
671lyjson_ctx_restore(struct lyjson_ctx *jsonctx)
672{
673 if (jsonctx->dynamic) {
674 free((char *)jsonctx->value);
675 }
676 jsonctx->status.count = jsonctx->backup.status_count;
Michal Vasko22df3f02020-08-24 13:29:22 +0200677 jsonctx->status.objs[jsonctx->backup.status_count - 1] = (void *)jsonctx->backup.status;
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200678 jsonctx->value = jsonctx->backup.value;
679 jsonctx->value_len = jsonctx->backup.value_len;
680 jsonctx->in->current = jsonctx->backup.input;
681 jsonctx->dynamic = jsonctx->backup.dynamic;
682 jsonctx->backup.dynamic = 0;
683}
684
685LY_ERR
686lyjson_ctx_next(struct lyjson_ctx *jsonctx, enum LYJSON_PARSER_STATUS *status)
687{
688 LY_ERR ret = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +0200689 ly_bool toplevel = 0;
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200690 enum LYJSON_PARSER_STATUS prev;
691
692 assert(jsonctx);
693
694 prev = lyjson_ctx_status(jsonctx, 0);
695
Michal Vasko69730152020-10-09 16:30:07 +0200696 if ((prev == LYJSON_OBJECT) || (prev == LYJSON_ARRAY)) {
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200697 /* get value for the object's member OR the first value in the array */
698 ret = lyjson_value(jsonctx);
699 goto result;
700 } else {
701 /* the previous token is closed and should be completely processed */
702 JSON_POP_STATUS_RET(jsonctx);
703 prev = lyjson_ctx_status(jsonctx, 0);
704 }
705
706 if (!jsonctx->status.count) {
707 /* we are done with the top level value */
708 toplevel = 1;
709 }
710 LY_CHECK_RET(skip_ws(jsonctx));
711 if (toplevel && !jsonctx->status.count) {
712 /* EOF expected, but there are some data after the top level token */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100713 LOGVAL(jsonctx->ctx, LYVE_SYNTAX, "Expecting end-of-input, but some data follows the top level JSON value.");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200714 return LY_EVALID;
715 }
716
717 if (toplevel) {
718 /* we are done */
719 return LY_SUCCESS;
720 }
721
722 /* continue with the next token */
723 assert(prev == LYJSON_OBJECT || prev == LYJSON_ARRAY);
724
725 if (*jsonctx->in->current == ',') {
726 /* sibling item in the ... */
727 ly_in_skip(jsonctx->in, 1);
728 LY_CHECK_RET(skip_ws(jsonctx));
729
730 if (prev == LYJSON_OBJECT) {
731 /* ... object - get another object's member */
732 ret = lyjson_object_name(jsonctx);
733 } else { /* LYJSON_ARRAY */
734 /* ... array - get another complete value */
735 ret = lyjson_value(jsonctx);
736 }
Michal Vasko69730152020-10-09 16:30:07 +0200737 } else if (((prev == LYJSON_OBJECT) && (*jsonctx->in->current == '}')) || ((prev == LYJSON_ARRAY) && (*jsonctx->in->current == ']'))) {
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200738 ly_in_skip(jsonctx->in, 1);
739 JSON_POP_STATUS_RET(jsonctx);
740 JSON_PUSH_STATUS_RET(jsonctx, prev + 1);
741 } else {
742 /* unexpected value */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100743 LOGVAL(jsonctx->ctx, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(jsonctx->in->current), jsonctx->in->current,
744 prev == LYJSON_ARRAY ? "another JSON value in array" : "another JSON object's member");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200745 return LY_EVALID;
746 }
747
748result:
Michal Vasko69730152020-10-09 16:30:07 +0200749 if ((ret == LY_SUCCESS) && (jsonctx->status.count > 1) && (lyjson_ctx_status(jsonctx, 0) == LYJSON_END)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100750 LOGVAL(jsonctx->ctx, LY_VCODE_EOF);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200751 ret = LY_EVALID;
752 }
753
Michal Vasko69730152020-10-09 16:30:07 +0200754 if ((ret == LY_SUCCESS) && status) {
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200755 *status = lyjson_ctx_status(jsonctx, 0);
756 }
757
758 return ret;
759}
760
761enum LYJSON_PARSER_STATUS
762lyjson_ctx_status(struct lyjson_ctx *jsonctx, uint32_t index)
763{
764 assert(jsonctx);
765
766 if (jsonctx->status.count < index) {
767 return LYJSON_ERROR;
768 } else if (jsonctx->status.count == index) {
769 return LYJSON_ROOT;
770 } else {
Michal Vasko27915722020-08-31 14:54:42 +0200771 return (enum LYJSON_PARSER_STATUS)(uintptr_t)jsonctx->status.objs[jsonctx->status.count - (index + 1)];
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200772 }
773}
774
775void
776lyjson_ctx_free(struct lyjson_ctx *jsonctx)
777{
778 if (!jsonctx) {
779 return;
780 }
781
Radek Krejciddace2c2021-01-08 11:30:56 +0100782 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100783
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200784 if (jsonctx->dynamic) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200785 free((char *)jsonctx->value);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200786 }
787 if (jsonctx->backup.dynamic) {
788 free((char *)jsonctx->backup.value);
789 }
790
791 ly_set_erase(&jsonctx->status, NULL);
792
793 free(jsonctx);
794}