blob: 10e74db28bed26f2036007e0b968691f56a488f9 [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 }
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200120 }
121
Radek Krejcie7010dc2021-03-04 15:54:24 +0100122 LOGVAL(jsonctx->ctx, LYVE_SYNTAX, "Unexpected character \"%c\" after JSON %s.",
123 *jsonctx->in->current, lyjson_token2str(lyjson_ctx_status(jsonctx, 0)));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200124 return LY_EVALID;
125}
126
127/**
128 * Input is expected to start after the opening quotation-mark.
129 * When succeeds, input is moved after the closing quotation-mark.
130 */
131static LY_ERR
132lyjson_string_(struct lyjson_ctx *jsonctx)
133{
134#define BUFSIZE 24
135#define BUFSIZE_STEP 128
136
137 const char *in = jsonctx->in->current, *start;
138 char *buf = NULL;
139 size_t offset; /* read offset in input buffer */
140 size_t len; /* length of the output string (write offset in output buffer) */
141 size_t size = 0; /* size of the output buffer */
142 size_t u;
143 uint64_t start_line;
144
145 assert(jsonctx);
146
147 /* init */
148 start = in;
Radek Krejcid54412f2020-12-17 20:25:35 +0100149 start_line = jsonctx->in->line;
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200150 offset = len = 0;
151
152 /* parse */
153 while (in[offset]) {
154 if (in[offset] == '\\') {
155 /* escape sequence */
156 size_t slash = offset;
157 uint32_t value;
158 uint8_t i = 1;
159
160 if (!buf) {
161 /* prepare output buffer */
162 buf = malloc(BUFSIZE);
163 LY_CHECK_ERR_RET(!buf, LOGMEM(jsonctx->ctx), LY_EMEM);
164 size = BUFSIZE;
165 }
166
167 /* allocate enough for the offset and next character,
168 * we will need 4 bytes at most since we support only the predefined
169 * (one-char) entities and character references */
170 if (len + offset + 4 >= size) {
Juraj Vijtiukd746a352021-01-15 11:33:33 +0100171 size_t increment;
Radek Krejcidf549132021-01-21 10:32:32 +0100172 for (increment = BUFSIZE_STEP; len + offset + 4 >= size + increment; increment += BUFSIZE_STEP) {}
Juraj Vijtiukd746a352021-01-15 11:33:33 +0100173 buf = ly_realloc(buf, size + increment);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200174 LY_CHECK_ERR_RET(!buf, LOGMEM(jsonctx->ctx), LY_EMEM);
175 size += BUFSIZE_STEP;
176 }
177
178 if (offset) {
179 /* store what we have so far */
180 memcpy(&buf[len], in, offset);
181 len += offset;
182 in += offset;
183 offset = 0;
184 }
185
186 switch (in[++offset]) {
187 case '"':
188 /* quotation mark */
189 value = 0x22;
190 break;
191 case '\\':
192 /* reverse solidus */
193 value = 0x5c;
194 break;
195 case '/':
196 /* solidus */
197 value = 0x2f;
198 break;
199 case 'b':
200 /* backspace */
201 value = 0x08;
202 break;
203 case 'f':
204 /* form feed */
205 value = 0x0c;
206 break;
207 case 'n':
208 /* line feed */
209 value = 0x0a;
210 break;
211 case 'r':
212 /* carriage return */
213 value = 0x0d;
214 break;
215 case 't':
216 /* tab */
217 value = 0x09;
218 break;
219 case 'u':
220 /* Basic Multilingual Plane character \uXXXX */
221 offset++;
222 for (value = i = 0; i < 4; i++) {
Juraj Vijtiuk2b94e4b2020-11-16 23:52:07 +0100223 if (!in[offset + i]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100224 LOGVAL(jsonctx->ctx, LYVE_SYNTAX, "Invalid basic multilingual plane character \"%s\".", &in[slash]);
Juraj Vijtiuk2b94e4b2020-11-16 23:52:07 +0100225 goto error;
226 } else if (isdigit(in[offset + i])) {
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200227 u = (in[offset + i] - '0');
228 } else if (in[offset + i] > 'F') {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100229 u = LY_BASE_DEC + (in[offset + i] - 'a');
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200230 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100231 u = LY_BASE_DEC + (in[offset + i] - 'A');
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200232 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100233 value = (LY_BASE_HEX * value) + u;
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200234 }
235 break;
236 default:
237 /* invalid escape sequence */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100238 LOGVAL(jsonctx->ctx, LYVE_SYNTAX, "Invalid character escape sequence \\%c.", in[offset]);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200239 goto error;
240
241 }
242
243 offset += i; /* add read escaped characters */
244 LY_CHECK_ERR_GOTO(ly_pututf8(&buf[len], value, &u),
Radek Krejci2efc45b2020-12-22 16:25:44 +0100245 LOGVAL(jsonctx->ctx, LYVE_SYNTAX, "Invalid character reference \"%.*s\" (0x%08x).",
246 offset - slash, &in[slash], value),
Michal Vasko69730152020-10-09 16:30:07 +0200247 error);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200248 len += u; /* update number of bytes in buffer */
249 in += offset; /* move the input by the processed bytes stored in the buffer ... */
250 offset = 0; /* ... and reset the offset index for future moving data into buffer */
251
252 } else if (in[offset] == '"') {
253 /* end of string */
254 if (buf) {
255 /* realloc exact size string */
256 buf = ly_realloc(buf, len + offset + 1);
257 LY_CHECK_ERR_RET(!buf, LOGMEM(jsonctx->ctx), LY_EMEM);
258 size = len + offset + 1;
259 memcpy(&buf[len], in, offset);
260
261 /* set terminating NULL byte */
262 buf[len + offset] = '\0';
263 }
264 len += offset;
265 ++offset;
266 in += offset;
267 goto success;
268 } else {
269 /* get it as UTF-8 character for check */
270 const char *c = &in[offset];
271 uint32_t code = 0;
272 size_t code_len = 0;
273
274 LY_CHECK_ERR_GOTO(ly_getutf8(&c, &code, &code_len),
Radek Krejci2efc45b2020-12-22 16:25:44 +0100275 LOGVAL(jsonctx->ctx, LY_VCODE_INCHAR, in[offset]), error);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200276
277 LY_CHECK_ERR_GOTO(!is_jsonstrchar(code),
Radek Krejci2efc45b2020-12-22 16:25:44 +0100278 LOGVAL(jsonctx->ctx, LYVE_SYNTAX, "Invalid character in JSON string \"%.*s\" (0x%08x).",
279 &in[offset] - start + code_len, start, code),
Michal Vasko69730152020-10-09 16:30:07 +0200280 error);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200281
282 /* character is ok, continue */
283 offset += code_len;
284 }
285 }
286
287 /* EOF reached before endchar */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100288 LOGVAL(jsonctx->ctx, LY_VCODE_EOF);
289 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 +0200290
291error:
292 free(buf);
293 return LY_EVALID;
294
295success:
Radek Krejcid54412f2020-12-17 20:25:35 +0100296 jsonctx->in->current = in;
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200297 if (buf) {
298 lyjson_ctx_set_value(jsonctx, buf, len, 1);
299 } else {
300 lyjson_ctx_set_value(jsonctx, start, len, 0);
301 }
302
303 return LY_SUCCESS;
304
305#undef BUFSIZE
306#undef BUFSIZE_STEP
307}
308
309/*
310 *
311 * Wrapper around lyjson_string_() adding LYJSON_STRING status into context to allow using lyjson_string_() for parsing object's name.
312 */
313static LY_ERR
314lyjson_string(struct lyjson_ctx *jsonctx)
315{
316 LY_CHECK_RET(lyjson_string_(jsonctx));
317
318 JSON_PUSH_STATUS_RET(jsonctx, LYJSON_STRING);
319 LY_CHECK_RET(lyjson_check_next(jsonctx));
320
321 return LY_SUCCESS;
322}
323
324static LY_ERR
325lyjson_number(struct lyjson_ctx *jsonctx)
326{
327 size_t offset = 0, exponent = 0;
328 const char *in = jsonctx->in->current;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200329 uint8_t minus = 0;
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200330
331 if (in[offset] == '-') {
332 ++offset;
333 minus = 1;
334 }
335
336 if (in[offset] == '0') {
337 ++offset;
338 } else if (isdigit(in[offset])) {
339 ++offset;
340 while (isdigit(in[offset])) {
341 ++offset;
342 }
343 } else {
344invalid_character:
345 if (in[offset]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100346 LOGVAL(jsonctx->ctx, LYVE_SYNTAX, "Invalid character in JSON Number value (\"%c\").", in[offset]);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200347 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100348 LOGVAL(jsonctx->ctx, LY_VCODE_EOF);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200349 }
350 return LY_EVALID;
351 }
352
353 if (in[offset] == '.') {
354 ++offset;
355 if (!isdigit(in[offset])) {
356 goto invalid_character;
357 }
358 while (isdigit(in[offset])) {
359 ++offset;
360 }
361 }
362
363 if ((in[offset] == 'e') || (in[offset] == 'E')) {
364 exponent = offset++;
365 if ((in[offset] == '+') || (in[offset] == '-')) {
366 ++offset;
367 }
368 if (!isdigit(in[offset])) {
369 goto invalid_character;
370 }
371 while (isdigit(in[offset])) {
372 ++offset;
373 }
374 }
375
376 if (exponent) {
377 /* convert JSON number with exponent into the representation used by YANG */
378 long int e_val;
379 char *ptr, *dec_point, *num;
380 const char *e_ptr = &in[exponent + 1];
381 size_t num_len, i;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200382 int64_t dp_position; /* final position of the deciaml point */
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200383
384 errno = 0;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100385 e_val = strtol(e_ptr, &ptr, LY_BASE_DEC);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200386 if (errno) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100387 LOGVAL(jsonctx->ctx, LYVE_SEMANTICS, "Exponent out-of-bounds in a JSON Number value (%.*s).",
388 offset - minus - (e_ptr - in), e_ptr);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200389 return LY_EVALID;
390 }
391
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200392 dec_point = ly_strnchr(in, '.', exponent);
393 if (!dec_point) {
394 /* value is integer, we are just ... */
395 if (e_val >= 0) {
396 /* adding zeros at the end */
397 num_len = exponent + e_val;
398 dp_position = num_len; /* decimal point is behind the actual value */
399 } else if ((size_t)labs(e_val) < exponent) {
400 /* adding decimal point between the integer's digits */
401 num_len = exponent + 1;
402 dp_position = exponent + e_val;
403 } else {
404 /* adding decimal point before the integer with adding leading zero(s) */
Juraj Vijtiukc6166a02021-03-01 11:38:40 +0100405 num_len = labs(e_val) + 2 + minus;
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200406 dp_position = exponent + e_val;
407 }
408 dp_position -= minus;
409 } else {
410 /* value is decimal, we are moving the decimal point */
411 dp_position = dec_point - in + e_val - minus;
412 if (dp_position > (ssize_t)exponent) {
413 /* moving decimal point after the decimal value make the integer result */
414 num_len = dp_position;
415 } else if (dp_position < 0) {
416 /* moving decimal point before the decimal value requires additional zero(s)
417 * (decimal point is already count in exponent value) */
418 num_len = exponent + labs(dp_position) + 1;
419 } else {
420 /* moving decimal point just inside the decimal value does not make any change in length */
421 num_len = exponent;
422 }
423 }
424
425 /* allocate buffer for the result (add terminating NULL-byte */
426 num = malloc(num_len + 1);
427 LY_CHECK_ERR_RET(!num, LOGMEM(jsonctx->ctx), LY_EMEM);
428
429 /* compose the resulting vlaue */
430 i = 0;
431 if (minus) {
432 num[i++] = '-';
433 }
434 /* add leading zeros */
435 if (dp_position <= 0) {
436 num[i++] = '0';
437 num[i++] = '.';
Michal Vaskod989ba02020-08-24 10:59:24 +0200438 for ( ; dp_position; dp_position++) {
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200439 num[i++] = '0';
440 }
441 }
442 /* copy the value */
Radek Krejci857189e2020-09-01 13:26:36 +0200443 ly_bool dp_placed;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200444 size_t j;
445 for (dp_placed = dp_position ? 0 : 1, j = minus; j < exponent; j++) {
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200446 if (in[j] == '.') {
447 continue;
448 }
449 if (!dp_placed) {
450 if (!dp_position) {
451 num[i++] = '.';
452 dp_placed = 1;
453 } else {
454 dp_position--;
455 if (in[j] == '0') {
456 num_len--;
457 continue;
458 }
459 }
460 }
461
462 num[i++] = in[j];
463 }
464 /* trailing zeros */
465 while (dp_position--) {
466 num[i++] = '0';
467 }
468 /* terminating NULL byte */
469 num[i] = '\0';
470
471 /* store the modified number */
472 lyjson_ctx_set_value(jsonctx, num, num_len, 1);
473 } else {
474 /* store the number */
475 lyjson_ctx_set_value(jsonctx, jsonctx->in->current, offset, 0);
476 }
477 ly_in_skip(jsonctx->in, offset);
478
479 JSON_PUSH_STATUS_RET(jsonctx, LYJSON_NUMBER);
480 LY_CHECK_RET(lyjson_check_next(jsonctx));
481
482 return LY_SUCCESS;
483}
484
485static LY_ERR
486lyjson_object_name(struct lyjson_ctx *jsonctx)
487{
488 if (*jsonctx->in->current != '"') {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100489 LOGVAL(jsonctx->ctx, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(jsonctx->in->current),
Michal Vasko69730152020-10-09 16:30:07 +0200490 jsonctx->in->current, "a JSON object's member");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200491 return LY_EVALID;
492 }
493 ly_in_skip(jsonctx->in, 1);
494
495 LY_CHECK_RET(lyjson_string_(jsonctx));
496 LY_CHECK_RET(skip_ws(jsonctx));
Michal Vasko08dc70b2020-10-07 13:58:47 +0200497 if (*jsonctx->in->current != ':') {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100498 LOGVAL(jsonctx->ctx, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(jsonctx->in->current), jsonctx->in->current,
499 "a JSON object's name-separator ':'");
Michal Vasko08dc70b2020-10-07 13:58:47 +0200500 return LY_EVALID;
501 }
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200502 ly_in_skip(jsonctx->in, 1);
503 LY_CHECK_RET(skip_ws(jsonctx));
504
505 return LY_SUCCESS;
506}
507
508static LY_ERR
509lyjson_object(struct lyjson_ctx *jsonctx)
510{
511 LY_CHECK_RET(skip_ws(jsonctx));
512
513 if (*jsonctx->in->current == '}') {
514 /* empty object */
515 ly_in_skip(jsonctx->in, 1);
516 lyjson_ctx_set_value(jsonctx, NULL, 0, 0);
517 JSON_PUSH_STATUS_RET(jsonctx, LYJSON_OBJECT_EMPTY);
518 return LY_SUCCESS;
519 }
520
521 LY_CHECK_RET(lyjson_object_name(jsonctx));
522
523 /* output data are set by lyjson_string_() */
524 JSON_PUSH_STATUS_RET(jsonctx, LYJSON_OBJECT);
525
526 return LY_SUCCESS;
527}
528
529/*
530 * @brief Process JSON array envelope
531 *
532 *
533 *
534 * @param[in] jsonctx JSON parser context
535 * @return LY_SUCCESS or LY_EMEM
536 */
537static LY_ERR
538lyjson_array(struct lyjson_ctx *jsonctx)
539{
540 LY_CHECK_RET(skip_ws(jsonctx));
541
542 if (*jsonctx->in->current == ']') {
543 /* empty array */
544 ly_in_skip(jsonctx->in, 1);
545 JSON_PUSH_STATUS_RET(jsonctx, LYJSON_ARRAY_EMPTY);
546 } else {
547 JSON_PUSH_STATUS_RET(jsonctx, LYJSON_ARRAY);
548 }
549
550 /* erase previous values, array has no value on its own */
551 lyjson_ctx_set_value(jsonctx, NULL, 0, 0);
552
553 return LY_SUCCESS;
554}
555
556static LY_ERR
557lyjson_value(struct lyjson_ctx *jsonctx)
558{
Michal Vasko69730152020-10-09 16:30:07 +0200559 if (jsonctx->status.count && (lyjson_ctx_status(jsonctx, 0) == LYJSON_END)) {
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200560 return LY_SUCCESS;
561 }
562
Radek Krejcif13b87b2020-12-01 22:02:17 +0100563 if ((*jsonctx->in->current == 'f') && !strncmp(jsonctx->in->current, "false", ly_strlen_const("false"))) {
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200564 /* false */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100565 lyjson_ctx_set_value(jsonctx, jsonctx->in->current, ly_strlen_const("false"), 0);
566 ly_in_skip(jsonctx->in, ly_strlen_const("false"));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200567 JSON_PUSH_STATUS_RET(jsonctx, LYJSON_FALSE);
568 LY_CHECK_RET(lyjson_check_next(jsonctx));
569
Radek Krejcif13b87b2020-12-01 22:02:17 +0100570 } else if ((*jsonctx->in->current == 't') && !strncmp(jsonctx->in->current, "true", ly_strlen_const("true"))) {
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200571 /* true */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100572 lyjson_ctx_set_value(jsonctx, jsonctx->in->current, ly_strlen_const("true"), 0);
573 ly_in_skip(jsonctx->in, ly_strlen_const("true"));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200574 JSON_PUSH_STATUS_RET(jsonctx, LYJSON_TRUE);
575 LY_CHECK_RET(lyjson_check_next(jsonctx));
576
Radek Krejcif13b87b2020-12-01 22:02:17 +0100577 } else if ((*jsonctx->in->current == 'n') && !strncmp(jsonctx->in->current, "null", ly_strlen_const("null"))) {
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200578 /* none */
Radek Krejci201963a2020-12-03 11:43:40 +0100579 lyjson_ctx_set_value(jsonctx, "", 0, 0);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100580 ly_in_skip(jsonctx->in, ly_strlen_const("null"));
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200581 JSON_PUSH_STATUS_RET(jsonctx, LYJSON_NULL);
582 LY_CHECK_RET(lyjson_check_next(jsonctx));
583
584 } else if (*jsonctx->in->current == '"') {
585 /* string */
586 ly_in_skip(jsonctx->in, 1);
587 LY_CHECK_RET(lyjson_string(jsonctx));
588
589 } else if (*jsonctx->in->current == '[') {
590 /* array */
591 ly_in_skip(jsonctx->in, 1);
592 LY_CHECK_RET(lyjson_array(jsonctx));
593
594 } else if (*jsonctx->in->current == '{') {
595 /* object */
596 ly_in_skip(jsonctx->in, 1);
597 LY_CHECK_RET(lyjson_object(jsonctx));
598
Michal Vasko69730152020-10-09 16:30:07 +0200599 } else if ((*jsonctx->in->current == '-') || ((*jsonctx->in->current >= '0') && (*jsonctx->in->current <= '9'))) {
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200600 /* number */
601 LY_CHECK_RET(lyjson_number(jsonctx));
602
603 } else {
604 /* unexpected value */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100605 LOGVAL(jsonctx->ctx, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(jsonctx->in->current),
Michal Vasko69730152020-10-09 16:30:07 +0200606 jsonctx->in->current, "a JSON value");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200607 return LY_EVALID;
608 }
609
610 return LY_SUCCESS;
611}
612
613LY_ERR
614lyjson_ctx_new(const struct ly_ctx *ctx, struct ly_in *in, struct lyjson_ctx **jsonctx_p)
615{
616 LY_ERR ret = LY_SUCCESS;
617 struct lyjson_ctx *jsonctx;
618
619 assert(ctx);
620 assert(in);
621 assert(jsonctx_p);
622
623 /* new context */
624 jsonctx = calloc(1, sizeof *jsonctx);
625 LY_CHECK_ERR_RET(!jsonctx, LOGMEM(ctx), LY_EMEM);
626 jsonctx->ctx = ctx;
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200627 jsonctx->in = in;
628
Radek Krejciddace2c2021-01-08 11:30:56 +0100629 LOG_LOCINIT(NULL, NULL, NULL, in);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100630
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200631 /* parse JSON value, if any */
632 LY_CHECK_GOTO(ret = skip_ws(jsonctx), cleanup);
633 if (lyjson_ctx_status(jsonctx, 0) == LYJSON_END) {
634 /* empty data input */
635 goto cleanup;
636 }
637
638 ret = lyjson_value(jsonctx);
639
Michal Vasko69730152020-10-09 16:30:07 +0200640 if ((jsonctx->status.count > 1) && (lyjson_ctx_status(jsonctx, 0) == LYJSON_END)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100641 LOGVAL(jsonctx->ctx, LY_VCODE_EOF);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200642 ret = LY_EVALID;
643 }
644
645cleanup:
646 if (ret) {
647 lyjson_ctx_free(jsonctx);
648 } else {
649 *jsonctx_p = jsonctx;
650 }
651 return ret;
652}
653
654void
655lyjson_ctx_backup(struct lyjson_ctx *jsonctx)
656{
657 if (jsonctx->backup.dynamic) {
658 free((char *)jsonctx->backup.value);
659 }
660 jsonctx->backup.status = lyjson_ctx_status(jsonctx, 0);
661 jsonctx->backup.status_count = jsonctx->status.count;
662 jsonctx->backup.value = jsonctx->value;
663 jsonctx->backup.value_len = jsonctx->value_len;
664 jsonctx->backup.input = jsonctx->in->current;
665 jsonctx->backup.dynamic = jsonctx->dynamic;
666 jsonctx->dynamic = 0;
667}
668
669void
670lyjson_ctx_restore(struct lyjson_ctx *jsonctx)
671{
672 if (jsonctx->dynamic) {
673 free((char *)jsonctx->value);
674 }
675 jsonctx->status.count = jsonctx->backup.status_count;
Michal Vasko22df3f02020-08-24 13:29:22 +0200676 jsonctx->status.objs[jsonctx->backup.status_count - 1] = (void *)jsonctx->backup.status;
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200677 jsonctx->value = jsonctx->backup.value;
678 jsonctx->value_len = jsonctx->backup.value_len;
679 jsonctx->in->current = jsonctx->backup.input;
680 jsonctx->dynamic = jsonctx->backup.dynamic;
681 jsonctx->backup.dynamic = 0;
682}
683
684LY_ERR
685lyjson_ctx_next(struct lyjson_ctx *jsonctx, enum LYJSON_PARSER_STATUS *status)
686{
687 LY_ERR ret = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +0200688 ly_bool toplevel = 0;
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200689 enum LYJSON_PARSER_STATUS prev;
690
691 assert(jsonctx);
692
693 prev = lyjson_ctx_status(jsonctx, 0);
694
Michal Vasko69730152020-10-09 16:30:07 +0200695 if ((prev == LYJSON_OBJECT) || (prev == LYJSON_ARRAY)) {
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200696 /* get value for the object's member OR the first value in the array */
697 ret = lyjson_value(jsonctx);
698 goto result;
699 } else {
700 /* the previous token is closed and should be completely processed */
701 JSON_POP_STATUS_RET(jsonctx);
702 prev = lyjson_ctx_status(jsonctx, 0);
703 }
704
705 if (!jsonctx->status.count) {
706 /* we are done with the top level value */
707 toplevel = 1;
708 }
709 LY_CHECK_RET(skip_ws(jsonctx));
710 if (toplevel && !jsonctx->status.count) {
711 /* EOF expected, but there are some data after the top level token */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100712 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 +0200713 return LY_EVALID;
714 }
715
716 if (toplevel) {
717 /* we are done */
718 return LY_SUCCESS;
719 }
720
721 /* continue with the next token */
722 assert(prev == LYJSON_OBJECT || prev == LYJSON_ARRAY);
723
724 if (*jsonctx->in->current == ',') {
725 /* sibling item in the ... */
726 ly_in_skip(jsonctx->in, 1);
727 LY_CHECK_RET(skip_ws(jsonctx));
728
729 if (prev == LYJSON_OBJECT) {
730 /* ... object - get another object's member */
731 ret = lyjson_object_name(jsonctx);
732 } else { /* LYJSON_ARRAY */
733 /* ... array - get another complete value */
734 ret = lyjson_value(jsonctx);
735 }
Michal Vasko69730152020-10-09 16:30:07 +0200736 } else if (((prev == LYJSON_OBJECT) && (*jsonctx->in->current == '}')) || ((prev == LYJSON_ARRAY) && (*jsonctx->in->current == ']'))) {
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200737 ly_in_skip(jsonctx->in, 1);
738 JSON_POP_STATUS_RET(jsonctx);
739 JSON_PUSH_STATUS_RET(jsonctx, prev + 1);
740 } else {
741 /* unexpected value */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100742 LOGVAL(jsonctx->ctx, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(jsonctx->in->current), jsonctx->in->current,
743 prev == LYJSON_ARRAY ? "another JSON value in array" : "another JSON object's member");
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200744 return LY_EVALID;
745 }
746
747result:
Michal Vasko69730152020-10-09 16:30:07 +0200748 if ((ret == LY_SUCCESS) && (jsonctx->status.count > 1) && (lyjson_ctx_status(jsonctx, 0) == LYJSON_END)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100749 LOGVAL(jsonctx->ctx, LY_VCODE_EOF);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200750 ret = LY_EVALID;
751 }
752
Michal Vasko69730152020-10-09 16:30:07 +0200753 if ((ret == LY_SUCCESS) && status) {
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200754 *status = lyjson_ctx_status(jsonctx, 0);
755 }
756
757 return ret;
758}
759
760enum LYJSON_PARSER_STATUS
761lyjson_ctx_status(struct lyjson_ctx *jsonctx, uint32_t index)
762{
763 assert(jsonctx);
764
765 if (jsonctx->status.count < index) {
766 return LYJSON_ERROR;
767 } else if (jsonctx->status.count == index) {
768 return LYJSON_ROOT;
769 } else {
Michal Vasko27915722020-08-31 14:54:42 +0200770 return (enum LYJSON_PARSER_STATUS)(uintptr_t)jsonctx->status.objs[jsonctx->status.count - (index + 1)];
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200771 }
772}
773
774void
775lyjson_ctx_free(struct lyjson_ctx *jsonctx)
776{
777 if (!jsonctx) {
778 return;
779 }
780
Radek Krejciddace2c2021-01-08 11:30:56 +0100781 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100782
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200783 if (jsonctx->dynamic) {
Michal Vasko22df3f02020-08-24 13:29:22 +0200784 free((char *)jsonctx->value);
Radek Krejci50f0c6b2020-06-18 16:31:48 +0200785 }
786 if (jsonctx->backup.dynamic) {
787 free((char *)jsonctx->backup.value);
788 }
789
790 ly_set_erase(&jsonctx->status, NULL);
791
792 free(jsonctx);
793}