blob: 04c189ff8d9eb064d0f5b8322ce4b2bd290e73d4 [file] [log] [blame]
Radek Krejci5449d472015-10-26 14:35:56 +01001/**
2 * @file parser_json.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief JSON data parser for libyang
5 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
Radek Krejci54f6fb32016-02-24 12:56:39 +01008 * 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
Michal Vasko8de098c2016-02-26 10:00:25 +010011 *
Radek Krejci54f6fb32016-02-24 12:56:39 +010012 * https://opensource.org/licenses/BSD-3-Clause
Radek Krejci5449d472015-10-26 14:35:56 +010013 */
14
Radek Krejci9a5daea2016-03-02 16:49:40 +010015#define _GNU_SOURCE
Radek Krejci5449d472015-10-26 14:35:56 +010016#include <assert.h>
17#include <ctype.h>
18#include <limits.h>
Michal Vasko1e676ca2016-06-24 15:23:54 +020019#include <errno.h>
Radek Krejci5449d472015-10-26 14:35:56 +010020#include <stdlib.h>
21#include <string.h>
22
23#include "libyang.h"
24#include "common.h"
25#include "context.h"
26#include "parser.h"
27#include "printer.h"
28#include "tree_internal.h"
29#include "validation.h"
Radek Krejci9a5daea2016-03-02 16:49:40 +010030#include "xml_internal.h"
Radek Krejci5449d472015-10-26 14:35:56 +010031
Radek Krejci5449d472015-10-26 14:35:56 +010032static int
33lyjson_isspace(int c)
34{
35 switch(c) {
36 case 0x20: /* space */
37 case 0x09: /* horizontal tab */
38 case 0x0a: /* line feed or new line */
39 case 0x0d: /* carriage return */
40 return 1;
41 default:
42 return 0;
43 }
44}
45
46static unsigned int
47skip_ws(const char *data)
48{
49 unsigned int len = 0;
50
51 /* skip leading whitespaces */
52 while (data[len] && lyjson_isspace(data[len])) {
Radek Krejci5449d472015-10-26 14:35:56 +010053 len++;
54 }
55
56 return len;
57}
58
Radek Krejci5449d472015-10-26 14:35:56 +010059static char *
60lyjson_parse_text(const char *data, unsigned int *len)
61{
62#define BUFSIZE 1024
63
64 char buf[BUFSIZE];
65 char *result = NULL, *aux;
Radek Krejci967e4bf2015-11-28 10:06:40 +010066 int o, size = 0;
Radek Krejci5449d472015-10-26 14:35:56 +010067 unsigned int r, i;
68 int32_t value;
69
70 for (*len = o = 0; data[*len] && data[*len] != '"'; o++) {
71 if (o > BUFSIZE - 3) {
72 /* add buffer into the result */
73 if (result) {
74 size = size + o;
Michal Vasko253035f2015-12-17 16:58:13 +010075 aux = ly_realloc(result, size + 1);
76 if (!aux) {
77 LOGMEM;
78 return NULL;
79 }
Radek Krejci5449d472015-10-26 14:35:56 +010080 result = aux;
81 } else {
82 size = o;
83 result = malloc((size + 1) * sizeof *result);
Michal Vasko253035f2015-12-17 16:58:13 +010084 if (!result) {
85 LOGMEM;
86 return NULL;
87 }
Radek Krejci5449d472015-10-26 14:35:56 +010088 }
89 memcpy(&result[size - o], buf, o);
90
91 /* write again into the beginning of the buffer */
92 o = 0;
93 }
94
95 if (data[*len] == '\\') {
96 /* parse escape sequence */
97 (*len)++;
98 i = 1;
99 switch (data[(*len)]) {
100 case '"':
101 /* quotation mark */
102 value = 0x22;
103 break;
104 case '\\':
105 /* reverse solidus */
106 value = 0x5c;
107 break;
108 case '/':
109 /* solidus */
110 value = 0x2f;
111 break;
112 case 'b':
113 /* backspace */
114 value = 0x08;
115 break;
116 case 'f':
117 /* form feed */
118 value = 0x0c;
119 break;
120 case 'n':
121 /* line feed */
122 value = 0x0a;
123 break;
124 case 'r':
125 /* carriage return */
126 value = 0x0d;
127 break;
128 case 't':
129 /* tab */
130 value = 0x09;
131 break;
132 case 'u':
133 /* Basic Multilingual Plane character \uXXXX */
134 (*len)++;
135 for (value = i = 0; i < 4; i++) {
136 if (isdigit(data[(*len) + i])) {
137 r = (data[(*len) + i] - '0');
138 } else if (data[(*len) + i] > 'F') {
139 r = 10 + (data[(*len) + i] - 'a');
140 } else {
141 r = 10 + (data[(*len) + i] - 'A');
142 }
143 value = (16 * value) + r;
144 }
145 break;
146 default:
147 /* invalid escape sequence */
Radek Krejci48464ed2016-03-17 15:44:09 +0100148 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "character escape sequence");
Radek Krejci5449d472015-10-26 14:35:56 +0100149 goto error;
150
151 }
Radek Krejci48464ed2016-03-17 15:44:09 +0100152 r = pututf8(&buf[o], value);
Radek Krejci5449d472015-10-26 14:35:56 +0100153 if (!r) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100154 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "character UTF8 character");
Radek Krejci5449d472015-10-26 14:35:56 +0100155 goto error;
156 }
157 o += r - 1; /* o is ++ in for loop */
158 (*len) += i; /* number of read characters */
zyinter20087fa65732016-11-17 10:55:12 +0800159 } else if ((data[*len] >= 0 && data[*len] < 0x20) || data[*len] == 0x5c) {
Radek Krejci5449d472015-10-26 14:35:56 +0100160 /* control characters must be escaped */
Radek Krejci48464ed2016-03-17 15:44:09 +0100161 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "control character (unescaped)");
Radek Krejci5449d472015-10-26 14:35:56 +0100162 goto error;
163 } else {
164 /* unescaped character */
Radek Krejcideee60e2016-09-23 15:21:14 +0200165 r = copyutf8(&buf[o], &data[*len]);
166 if (!r) {
167 goto error;
168 }
169
170 o += r - 1; /* o is ++ in for loop */
171 (*len) += r;
Radek Krejci5449d472015-10-26 14:35:56 +0100172 }
173 }
174
175#undef BUFSIZE
176
177 if (o) {
178 if (result) {
179 size = size + o;
Michal Vasko253035f2015-12-17 16:58:13 +0100180 aux = ly_realloc(result, size + 1);
181 if (!aux) {
182 LOGMEM;
183 return NULL;
184 }
Radek Krejci5449d472015-10-26 14:35:56 +0100185 result = aux;
186 } else {
187 size = o;
188 result = malloc((size + 1) * sizeof *result);
Michal Vasko253035f2015-12-17 16:58:13 +0100189 if (!result) {
190 LOGMEM;
191 return NULL;
192 }
Radek Krejci5449d472015-10-26 14:35:56 +0100193 }
194 memcpy(&result[size - o], buf, o);
195 }
196 if (result) {
197 result[size] = '\0';
198 } else {
199 size = 0;
200 result = strdup("");
201 }
202
203 return result;
204
205error:
206 free(result);
207 return NULL;
208}
209
210static unsigned int
211lyjson_parse_number(const char *data)
212{
Michal Vasko8f32c112016-05-18 13:22:59 +0200213 unsigned int len = 0;
Radek Krejci5449d472015-10-26 14:35:56 +0100214
Michal Vasko8f32c112016-05-18 13:22:59 +0200215 if (data[len] == '-') {
216 ++len;
217 }
Radek Krejci5449d472015-10-26 14:35:56 +0100218
Michal Vasko8f32c112016-05-18 13:22:59 +0200219 if (data[len] == '0') {
220 ++len;
221 } else if (isdigit(data[len])) {
222 ++len;
223 while (isdigit(data[len])) {
224 ++len;
225 }
226 } else {
227 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid character in JSON Number value ('%c').", data[len]);
228 return 0;
229 }
230
231 if (data[len] == '.') {
232 ++len;
233 if (!isdigit(data[len])) {
234 if (data[len]) {
235 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid character in JSON Number value ('%c').", data[len]);
236 } else {
237 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid character in JSON Number value (EOF).");
Radek Krejci5449d472015-10-26 14:35:56 +0100238 }
Radek Krejci5449d472015-10-26 14:35:56 +0100239 return 0;
240 }
Michal Vasko8f32c112016-05-18 13:22:59 +0200241 while (isdigit(data[len])) {
242 ++len;
243 }
244 }
245
Michal Vasko1e676ca2016-06-24 15:23:54 +0200246 if ((data[len] == 'e') || (data[len] == 'E')) {
247 ++len;
248 if ((data[len] == '+') || (data[len] == '-')) {
249 ++len;
250 }
251 while (isdigit(data[len])) {
252 ++len;
253 }
254 }
255
Michal Vasko8f32c112016-05-18 13:22:59 +0200256 if (data[len] && (data[len] != ',') && (data[len] != ']') && (data[len] != '}') && !lyjson_isspace(data[len])) {
257 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid character in JSON Number value ('%c').", data[len]);
258 return 0;
Radek Krejci5449d472015-10-26 14:35:56 +0100259 }
260
261 return len;
262}
263
Michal Vasko1e676ca2016-06-24 15:23:54 +0200264static char *
265lyjson_convert_enumber(const char *number, unsigned int num_len, char *e_ptr)
266{
267 char *ptr, *num;
268 const char *number_ptr;
269 long int e_val;
270 int dot_pos, chars_to_dot, minus;
271 unsigned int num_len_no_e;
272
273 if (*number == '-') {
274 minus = 1;
275 ++number;
276 --num_len;
277 } else {
278 minus = 0;
279 }
280
281 num_len_no_e = e_ptr - number;
282
283 errno = 0;
284 ++e_ptr;
285 e_val = strtol(e_ptr, &ptr, 10);
286 if (errno) {
287 LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Exponent out-of-bounds in a JSON Number value (%.*s).",
288 num_len - (e_ptr - number), e_ptr);
289 return NULL;
290 } else if (ptr != number + num_len) {
291 /* we checked this already */
292 LOGINT;
293 return NULL;
294 }
295
296 if ((ptr = strnchr(number, '.', num_len_no_e))) {
297 dot_pos = ptr - number;
298 } else {
299 dot_pos = num_len_no_e;
300 }
301
302 dot_pos += e_val;
303
304 /* allocate enough memory */
305 if (dot_pos < 1) {
306 /* (.XXX)XXX[.]XXXX */
307 num = malloc((minus ? 1 : 0) + -dot_pos + 2 + (num_len_no_e - (ptr ? 1 : 0)) + 1);
308 } else if (dot_pos < (signed)num_len_no_e) {
309 /* XXXX(.)XX.XXX */
310 num = malloc((minus ? 1 : 0) + num_len_no_e + (ptr ? 0 : 1) + 1);
311 } else {
312 /* XXX[.]XXXX(XXX.) */
313 num = malloc((minus ? 1 : 0) + (dot_pos - (ptr ? 2 : 1)) + 1);
314 }
315
316 if (!num) {
317 LOGMEM;
318 return NULL;
319 }
320 if (minus) {
321 strcpy(num, "-");
322 } else {
323 num[0] = '\0';
324 }
325
326 if (dot_pos < 1) {
327 strcat(num, "0.");
328 }
329 if (dot_pos < 0) {
330 sprintf(num + strlen(num), "%0*d", -dot_pos, 0);
331 }
332
333 chars_to_dot = dot_pos;
Michal Vasko9d1593d2016-09-30 12:33:34 +0200334 for (ptr = num + strlen(num), number_ptr = number; (unsigned)(number_ptr - number) < num_len_no_e; ) {
Michal Vasko1e676ca2016-06-24 15:23:54 +0200335 if (!chars_to_dot) {
336 *ptr = '.';
337 ++ptr;
338 chars_to_dot = -1;
339 } else if (isdigit(*number_ptr)) {
340 *ptr = *number_ptr;
341 ++ptr;
342 ++number_ptr;
343 if (chars_to_dot > 0) {
344 --chars_to_dot;
345 }
346 } else if (*number_ptr == '.') {
347 ++number_ptr;
348 } else {
349 LOGINT;
350 free(num);
351 return NULL;
352 }
353 }
354 *ptr = '\0';
355
356 if (dot_pos > (signed)num_len_no_e) {
357 sprintf(num + strlen(num), "%0*d", dot_pos - num_len_no_e, 0);
358 }
359
360 return num;
361}
362
Radek Krejci5449d472015-10-26 14:35:56 +0100363static unsigned int
364lyjson_parse_boolean(const char *data)
365{
Radek Krejci6b47b502015-10-30 15:52:41 +0100366 unsigned int len = 0;
Radek Krejci5449d472015-10-26 14:35:56 +0100367
368 if (!strncmp(data, "false", 5)) {
369 len = 5;
370 } else if (!strncmp(data, "true", 4)) {
371 len = 4;
372 }
373
374 if (data[len] && data[len] != ',' && data[len] != ']' && data[len] != '}' && !lyjson_isspace(data[len])) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100375 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON literal value (expected true or false)");
Radek Krejci5449d472015-10-26 14:35:56 +0100376 return 0;
377 }
378
379 return len;
380}
381
382static unsigned int
Radek Krejci4ae82942016-09-19 16:41:06 +0200383json_get_anydata(struct lyd_node_anydata *any, const char *data)
Radek Krejci5449d472015-10-26 14:35:56 +0100384{
Radek Krejcia7177672016-11-17 14:53:03 +0900385 unsigned int len = 0, start, stop, c = 0;
386 char *str;
Radek Krejci5449d472015-10-26 14:35:56 +0100387
Radek Krejcia7177672016-11-17 14:53:03 +0900388 /* anydata (as well as meaningful anyxml) is supposed to be encoded as object,
389 * anyxml can be a string value, other JSON types are not supported since it is
390 * not clear how they are supposed to be represented/converted into an internal representation */
391 if (data[len] == '"' && any->schema->nodetype == LYS_ANYXML) {
392 len = 1;
393 str = lyjson_parse_text(&data[len], &c);
394 if (!str) {
395 return 0;
396 }
397 if (data[len + c] != '"') {
Radek Krejci7d6d2842016-11-28 11:21:38 +0100398 free(str);
Radek Krejcia7177672016-11-17 14:53:03 +0900399 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, any,
400 "JSON data (missing quotation-mark at the end of string)");
401 return 0;
402 }
403
404 any->value.str = lydict_insert_zc(any->schema->module->ctx, str);
405 any->value_type = LYD_ANYDATA_CONSTSTRING;
406 return len + c + 1;
407 } else if (data[len] != '{') {
408 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, any, "Unsupported Anydata/anyxml content (not an object nor string)");
Michal Vaskoa19f7d72016-05-18 13:24:08 +0200409 return 0;
Radek Krejci5449d472015-10-26 14:35:56 +0100410 }
411
Radek Krejcia7177672016-11-17 14:53:03 +0900412 /* count opening '{' and closing '}' brackets to get the end of the object without its parsing */
413 c = len = 1;
Radek Krejci4ae82942016-09-19 16:41:06 +0200414 len += skip_ws(&data[len]);
Radek Krejcia7177672016-11-17 14:53:03 +0900415 start = len;
416 stop = start - 1;
Radek Krejci4ae82942016-09-19 16:41:06 +0200417 while (data[len] && c) {
418 switch (data[len]) {
419 case '{':
420 c++;
421 break;
422 case '}':
423 c--;
424 break;
425 default:
426 if (!isspace(data[len])) {
427 stop = len;
428 }
429 }
430 len++;
431 }
432 if (c) {
433 LOGVAL(LYE_EOF, LY_VLOG_LYD, any);
434 return 0;
435 }
436 any->value_type = LYD_ANYDATA_JSON;
Radek Krejcia7177672016-11-17 14:53:03 +0900437 if (stop >= start) {
438 any->value.str = lydict_insert(any->schema->module->ctx, &data[start], stop - start + 1);
439 } /* else no data */
Radek Krejci4ae82942016-09-19 16:41:06 +0200440
Michal Vaskoa19f7d72016-05-18 13:24:08 +0200441 return len;
Radek Krejci5449d472015-10-26 14:35:56 +0100442}
443
444static unsigned int
Radek Krejci45f41dd2017-02-09 14:11:09 +0100445json_get_value(struct lyd_node_leaf_list *leaf, struct lyd_node **first_sibling, const char *data, int options,
446 struct unres_data *unres)
Radek Krejci5449d472015-10-26 14:35:56 +0100447{
Radek Krejcibd930122016-08-10 13:28:26 +0200448 struct lyd_node_leaf_list *new;
Radek Krejci37b756f2016-01-18 10:15:03 +0100449 struct lys_type *stype;
Radek Krejci5449d472015-10-26 14:35:56 +0100450 struct ly_ctx *ctx;
451 unsigned int len = 0, r;
Radek Krejci5449d472015-10-26 14:35:56 +0100452 char *str;
453
Radek Krejci0b7704f2016-03-18 12:16:14 +0100454 assert(leaf && data);
Radek Krejci5449d472015-10-26 14:35:56 +0100455 ctx = leaf->schema->module->ctx;
456
457 stype = &((struct lys_node_leaf *)leaf->schema)->type;
Radek Krejci23238922015-10-27 17:13:34 +0100458
Radek Krejci5449d472015-10-26 14:35:56 +0100459 if (leaf->schema->nodetype == LYS_LEAFLIST) {
460 /* expecting begin-array */
461 if (data[len++] != '[') {
Radek Krejci48464ed2016-03-17 15:44:09 +0100462 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, leaf, "JSON data (expected begin-array)");
Radek Krejci5449d472015-10-26 14:35:56 +0100463 return 0;
464 }
465
466repeat:
467 len += skip_ws(&data[len]);
468 }
469
470 /* will be changed in case of union */
471 leaf->value_type = stype->base;
472
473 if (data[len] == '"') {
474 /* string representations */
Michal Vasko6baed1c2016-05-18 13:24:44 +0200475 ++len;
Radek Krejci5449d472015-10-26 14:35:56 +0100476 str = lyjson_parse_text(&data[len], &r);
Radek Krejci23238922015-10-27 17:13:34 +0100477 if (!str) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100478 LOGPATH(LY_VLOG_LYD, leaf);
Radek Krejci5449d472015-10-26 14:35:56 +0100479 return 0;
480 }
481 leaf->value_str = lydict_insert_zc(ctx, str);
482 if (data[len + r] != '"') {
Radek Krejci48464ed2016-03-17 15:44:09 +0100483 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, leaf,
Radek Krejciadb57612016-02-16 13:34:34 +0100484 "JSON data (missing quotation-mark at the end of string)");
Radek Krejci5449d472015-10-26 14:35:56 +0100485 return 0;
486 }
487 len += r + 1;
488 } else if (data[len] == '-' || isdigit(data[len])) {
489 /* numeric type */
490 r = lyjson_parse_number(&data[len]);
491 if (!r) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100492 LOGPATH(LY_VLOG_LYD, leaf);
Radek Krejci5449d472015-10-26 14:35:56 +0100493 return 0;
494 }
Michal Vasko1e676ca2016-06-24 15:23:54 +0200495 /* if it's a number with 'e' or 'E', get rid of it first */
496 if ((str = strnchr(&data[len], 'e', r)) || (str = strnchr(&data[len], 'E', r))) {
497 str = lyjson_convert_enumber(&data[len], r, str);
498 if (!str) {
499 return 0;
500 }
501 leaf->value_str = lydict_insert_zc(ctx, str);
502 } else {
503 leaf->value_str = lydict_insert(ctx, &data[len], r);
504 }
Radek Krejci5449d472015-10-26 14:35:56 +0100505 len += r;
506 } else if (data[len] == 'f' || data[len] == 't') {
507 /* boolean */
508 r = lyjson_parse_boolean(&data[len]);
509 if (!r) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100510 LOGPATH(LY_VLOG_LYD, leaf);
Radek Krejci5449d472015-10-26 14:35:56 +0100511 return 0;
512 }
513 leaf->value_str = lydict_insert(ctx, &data[len], r);
514 len += r;
515 } else if (!strncmp(&data[len], "[null]", 6)) {
516 /* empty */
Michal Vasko44913842016-04-13 14:20:41 +0200517 leaf->value_str = lydict_insert(ctx, "", 0);
Radek Krejci5449d472015-10-26 14:35:56 +0100518 len += 6;
519 } else {
520 /* error */
Radek Krejci48464ed2016-03-17 15:44:09 +0100521 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, leaf, "JSON data (unexpected value)");
Radek Krejci5449d472015-10-26 14:35:56 +0100522 return 0;
523 }
524
Radek Krejci1899d6a2016-11-03 13:48:07 +0100525 /* the value is here converted to a JSON format if needed in case of LY_TYPE_IDENT and LY_TYPE_INST or to a
526 * canonical form of the value */
Radek Krejcia571d942017-02-24 09:26:49 +0100527 if (!lyp_parse_value(&((struct lys_node_leaf *)leaf->schema)->type, &leaf->value_str, NULL, leaf, NULL, 1, 0)) {
Radek Krejci23238922015-10-27 17:13:34 +0100528 ly_errno = LY_EVALID;
529 return 0;
Radek Krejci5449d472015-10-26 14:35:56 +0100530 }
531
532 if (leaf->schema->nodetype == LYS_LEAFLIST) {
533 /* repeat until end-array */
534 len += skip_ws(&data[len]);
535 if (data[len] == ',') {
Radek Krejci45f41dd2017-02-09 14:11:09 +0100536 /* various validation checks */
537 if (lyv_data_context((struct lyd_node*)leaf, options, unres)) {
538 return 0;
539 }
540
541 ly_err_clean(1);
542 if (lyv_data_content((struct lyd_node*)leaf, options, unres) ||
543 lyv_multicases((struct lyd_node*)leaf, NULL, first_sibling, 0, NULL)) {
544 if (ly_errno) {
545 return 0;
546 }
547 }
548
Radek Krejci5449d472015-10-26 14:35:56 +0100549 /* another instance of the leaf-list */
550 new = calloc(1, sizeof(struct lyd_node_leaf_list));
Michal Vasko253035f2015-12-17 16:58:13 +0100551 if (!new) {
552 LOGMEM;
553 return 0;
554 }
Radek Krejci5449d472015-10-26 14:35:56 +0100555 new->parent = leaf->parent;
556 new->prev = (struct lyd_node *)leaf;
557 leaf->next = (struct lyd_node *)new;
558
Radek Krejcie1794882017-02-08 13:23:38 +0100559 /* copy the validity and when flags */
560 new->validity = leaf->validity;
561 new->when_status = leaf->when_status;
562
Radek Krejci5449d472015-10-26 14:35:56 +0100563 /* fix the "last" pointer */
Radek Krejci45f41dd2017-02-09 14:11:09 +0100564 (*first_sibling)->prev = (struct lyd_node *)new;
Radek Krejci5449d472015-10-26 14:35:56 +0100565
566 new->schema = leaf->schema;
567
568 /* repeat value parsing */
569 leaf = new;
570 len++;
571 goto repeat;
572 } else if (data[len] == ']') {
573 len++;
574 len += skip_ws(&data[len]);
575 } else {
576 /* something unexpected */
Radek Krejci48464ed2016-03-17 15:44:09 +0100577 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, leaf, "JSON data (expecting value-separator or end-array)");
Radek Krejci5449d472015-10-26 14:35:56 +0100578 return 0;
579 }
580 }
581
582 len += skip_ws(&data[len]);
583 return len;
584}
585
586static unsigned int
Radek Krejci532e5e92017-02-22 12:59:24 +0100587json_parse_attr(struct lys_module *parent_module, struct lyd_attr **attr, const char *data, int options)
Radek Krejci88f29302015-10-30 15:42:33 +0100588{
589 unsigned int len = 0, r;
Radek Krejci532e5e92017-02-22 12:59:24 +0100590 char *str = NULL, *name, *prefix = NULL, *value;
Radek Krejci88f29302015-10-30 15:42:33 +0100591 struct lys_module *module = parent_module;
592 struct lyd_attr *attr_new, *attr_last = NULL;
Michal Vasko7675c622017-03-02 10:50:07 +0100593 int ret;
Radek Krejci88f29302015-10-30 15:42:33 +0100594
Radek Krejcide9d92c2015-10-30 15:59:59 +0100595 *attr = NULL;
596
Radek Krejci88f29302015-10-30 15:42:33 +0100597 if (data[len] != '{') {
598 if (!strncmp(&data[len], "null", 4)) {
Radek Krejci88f29302015-10-30 15:42:33 +0100599 len += 4;
600 len += skip_ws(&data[len]);
601 return len;
602 }
Radek Krejci48464ed2016-03-17 15:44:09 +0100603 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing begin-object)");
Radek Krejci88f29302015-10-30 15:42:33 +0100604 goto error;
605 }
606
607repeat:
Radek Krejci6a6326d2017-02-27 13:00:52 +0100608 prefix = NULL;
Radek Krejci88f29302015-10-30 15:42:33 +0100609 len++;
610 len += skip_ws(&data[len]);
611
612 if (data[len] != '"') {
Radek Krejci48464ed2016-03-17 15:44:09 +0100613 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing quotation-mark at the begining of string)");
Radek Krejci88f29302015-10-30 15:42:33 +0100614 return 0;
615 }
616 len++;
617 str = lyjson_parse_text(&data[len], &r);
618 if (!r) {
619 goto error;
620 } else if (data[len + r] != '"') {
Radek Krejci48464ed2016-03-17 15:44:09 +0100621 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing quotation-mark at the end of string)");
Radek Krejci88f29302015-10-30 15:42:33 +0100622 goto error;
623 }
624 if ((name = strchr(str, ':'))) {
625 *name = '\0';
626 name++;
627 prefix = str;
Michal Vasko1e62a092015-12-01 12:27:20 +0100628 module = (struct lys_module *)ly_ctx_get_module(parent_module->ctx, prefix, NULL);
Radek Krejci88f29302015-10-30 15:42:33 +0100629 if (!module) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100630 LOGVAL(LYE_INELEM, LY_VLOG_NONE, NULL, name);
Radek Krejci88f29302015-10-30 15:42:33 +0100631 goto error;
632 }
633 } else {
634 name = str;
635 }
636
637 /* prepare data for parsing node content */
638 len += r + 1;
639 len += skip_ws(&data[len]);
640 if (data[len] != ':') {
Radek Krejci48464ed2016-03-17 15:44:09 +0100641 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing name-separator)");
Radek Krejci88f29302015-10-30 15:42:33 +0100642 goto error;
643 }
644 len++;
645 len += skip_ws(&data[len]);
646
647 if (data[len] != '"') {
Radek Krejci48464ed2016-03-17 15:44:09 +0100648 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing quotation-mark at the beginning of string)");
Radek Krejcide9d92c2015-10-30 15:59:59 +0100649 goto error;
Radek Krejci88f29302015-10-30 15:42:33 +0100650 }
651 len++;
652 value = lyjson_parse_text(&data[len], &r);
653 if (!r) {
654 goto error;
655 } else if (data[len + r] != '"') {
Radek Krejci48464ed2016-03-17 15:44:09 +0100656 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing quotation-mark at the end of string)");
Radek Krejcide9d92c2015-10-30 15:59:59 +0100657 free(value);
Radek Krejci88f29302015-10-30 15:42:33 +0100658 goto error;
659 }
660 len += r + 1;
661 len += skip_ws(&data[len]);
662
Michal Vasko7675c622017-03-02 10:50:07 +0100663 ret = lyp_fill_attr(parent_module->ctx, NULL, NULL, prefix, name, value, NULL, &attr_new);
664 if (ret == -1) {
Radek Krejci532e5e92017-02-22 12:59:24 +0100665 free(value);
Michal Vasko253035f2015-12-17 16:58:13 +0100666 goto error;
Michal Vasko7675c622017-03-02 10:50:07 +0100667 } else if (ret == 1) {
668 if (options & LYD_OPT_STRICT) {
669 LOGVAL(LYE_INMETA, LY_VLOG_NONE, NULL, prefix, name, value);
670 free(value);
671 goto error;
672 }
Radek Krejci532e5e92017-02-22 12:59:24 +0100673
Michal Vasko7675c622017-03-02 10:50:07 +0100674 LOGWRN("Unknown \"%s:%s\" metadata with value \"%s\", ignoring.",
675 (prefix ? prefix : "<none>"), name, value);
676 free(value);
677 goto next;
Radek Krejci532e5e92017-02-22 12:59:24 +0100678 }
Michal Vasko7675c622017-03-02 10:50:07 +0100679 free(value);
Radek Krejcia571d942017-02-24 09:26:49 +0100680
Radek Krejci88f29302015-10-30 15:42:33 +0100681 if (!attr_last) {
682 *attr = attr_last = attr_new;
683 } else {
684 attr_last->next = attr_new;
685 attr_last = attr_new;
686 }
687
Radek Krejci532e5e92017-02-22 12:59:24 +0100688next:
Radek Krejci88f29302015-10-30 15:42:33 +0100689 free(str);
Radek Krejcide9d92c2015-10-30 15:59:59 +0100690 str = NULL;
Radek Krejci88f29302015-10-30 15:42:33 +0100691
692 if (data[len] == ',') {
693 goto repeat;
694 } else if (data[len] != '}') {
Radek Krejci48464ed2016-03-17 15:44:09 +0100695 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing end-object)");
Radek Krejcide9d92c2015-10-30 15:59:59 +0100696 goto error;
Radek Krejci88f29302015-10-30 15:42:33 +0100697 }
698 len++;
699 len += skip_ws(&data[len]);
700
701 return len;
702
703error:
704 free(str);
Radek Krejcide9d92c2015-10-30 15:59:59 +0100705 if (*attr) {
Radek Krejci532e5e92017-02-22 12:59:24 +0100706 lyd_free_attr(module->ctx, NULL, *attr, 1);
Radek Krejcide9d92c2015-10-30 15:59:59 +0100707 *attr = NULL;
708 }
Radek Krejci88f29302015-10-30 15:42:33 +0100709 return 0;
710}
711
712struct attr_cont {
713 struct attr_cont *next;
714 struct lyd_attr *attr;
715 struct lys_node *schema;
716 unsigned int index; /** non-zero only in case of leaf-list */
717};
718
Radek Krejcic4831272015-11-01 19:26:34 +0100719static int
Michal Vasko7675c622017-03-02 10:50:07 +0100720store_attrs(struct ly_ctx *ctx, struct attr_cont *attrs, struct lyd_node *first, int options)
Radek Krejcic4831272015-11-01 19:26:34 +0100721{
722 struct lyd_node *diter;
723 struct attr_cont *iter;
Radek Krejci532e5e92017-02-22 12:59:24 +0100724 struct lyd_attr *aiter;
Radek Krejcic4831272015-11-01 19:26:34 +0100725 unsigned int flag_leaflist = 0;
726
727 while (attrs) {
728 iter = attrs;
729 attrs = attrs->next;
730
731 if (iter->index) {
732 flag_leaflist = 1;
733 }
734
735 LY_TREE_FOR(first, diter) {
736 if (iter->schema != diter->schema) {
737 continue;
738 }
739
740 if (flag_leaflist && flag_leaflist != iter->index) {
741 flag_leaflist++;
742 continue;
743 }
744
745 /* we have match */
746 if (diter->attr) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100747 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, diter,
Radek Krejciadb57612016-02-16 13:34:34 +0100748 "attribute (multiple attribute definitions belong to a single element)");
Radek Krejcic4831272015-11-01 19:26:34 +0100749 free(iter);
750 goto error;
751 }
752
753 diter->attr = iter->attr;
Radek Krejci532e5e92017-02-22 12:59:24 +0100754 for (aiter = iter->attr; aiter; aiter = aiter->next) {
755 aiter->parent = diter;
756 }
Michal Vasko7675c622017-03-02 10:50:07 +0100757
Radek Krejcic4831272015-11-01 19:26:34 +0100758 break;
759 }
760
761 if (!diter) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100762 LOGVAL(LYE_XML_MISS, LY_VLOG_NONE, NULL, "element for the specified attribute", iter->attr->name);
Radek Krejcic4831272015-11-01 19:26:34 +0100763 lyd_free_attr(iter->schema->module->ctx, NULL, iter->attr, 1);
764 free(iter);
765 goto error;
766 }
767 free(iter);
Michal Vasko7675c622017-03-02 10:50:07 +0100768
769 /* check edit-config attribute correctness */
770 if ((options & LYD_OPT_EDIT) && lyp_check_edit_attr(ctx, diter->attr, diter, NULL)) {
771 goto error;
772 }
Radek Krejcic4831272015-11-01 19:26:34 +0100773 }
774
775 return 0;
776
777error:
778
779 while (attrs) {
780 iter = attrs;
781 attrs = attrs->next;
782
783 lyd_free_attr(ctx, NULL, iter->attr, 1);
784 free(iter);
785 }
786
787 return -1;
788}
789
Radek Krejci88f29302015-10-30 15:42:33 +0100790static unsigned int
Michal Vasko36ef6932015-12-01 14:30:17 +0100791json_parse_data(struct ly_ctx *ctx, const char *data, const struct lys_node *schema_parent, struct lyd_node **parent,
Radek Krejcibd930122016-08-10 13:28:26 +0200792 struct lyd_node *first_sibling, struct lyd_node *prev, struct attr_cont **attrs, int options,
Michal Vaskoafa7a642016-10-18 15:11:38 +0200793 struct unres_data *unres, struct lyd_node **act_notif)
Radek Krejci5449d472015-10-26 14:35:56 +0100794{
795 unsigned int len = 0;
796 unsigned int r;
Radek Krejcic4831272015-11-01 19:26:34 +0100797 unsigned int flag_leaflist = 0;
Radek Krejci61767ca2016-09-19 14:21:55 +0200798 int i, pos;
Radek Krejci88f29302015-10-30 15:42:33 +0100799 char *name, *prefix = NULL, *str = NULL;
Michal Vasko1e62a092015-12-01 12:27:20 +0100800 const struct lys_module *module = NULL;
Radek Krejci5449d472015-10-26 14:35:56 +0100801 struct lys_node *schema = NULL;
Radek Krejcibd930122016-08-10 13:28:26 +0200802 struct lyd_node *result = NULL, *new, *list, *diter = NULL;
Radek Krejci88f29302015-10-30 15:42:33 +0100803 struct lyd_attr *attr;
Radek Krejcic4831272015-11-01 19:26:34 +0100804 struct attr_cont *attrs_aux;
Radek Krejci5449d472015-10-26 14:35:56 +0100805
806 /* each YANG data node representation starts with string (node identifier) */
807 if (data[len] != '"') {
Radek Krejci48464ed2016-03-17 15:44:09 +0100808 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, (*parent),
Radek Krejciadb57612016-02-16 13:34:34 +0100809 "JSON data (missing quotation-mark at the beginning of string)");
Radek Krejci88f29302015-10-30 15:42:33 +0100810 return 0;
Radek Krejci5449d472015-10-26 14:35:56 +0100811 }
812 len++;
813
Radek Krejci23238922015-10-27 17:13:34 +0100814 str = lyjson_parse_text(&data[len], &r);
Radek Krejci5449d472015-10-26 14:35:56 +0100815 if (!r) {
816 goto error;
817 } else if (data[len + r] != '"') {
Radek Krejci48464ed2016-03-17 15:44:09 +0100818 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, (*parent),
Radek Krejciadb57612016-02-16 13:34:34 +0100819 "JSON data (missing quotation-mark at the end of string)");
Radek Krejci5449d472015-10-26 14:35:56 +0100820 goto error;
821 }
Radek Krejci23238922015-10-27 17:13:34 +0100822 if ((name = strchr(str, ':'))) {
823 *name = '\0';
824 name++;
825 prefix = str;
Radek Krejci88f29302015-10-30 15:42:33 +0100826 if (prefix[0] == '@') {
827 prefix++;
828 }
Radek Krejci23238922015-10-27 17:13:34 +0100829 } else {
830 name = str;
Radek Krejci88f29302015-10-30 15:42:33 +0100831 if (name[0] == '@') {
832 name++;
833 }
Radek Krejci5449d472015-10-26 14:35:56 +0100834 }
835
Radek Krejci5449d472015-10-26 14:35:56 +0100836 /* prepare data for parsing node content */
837 len += r + 1;
838 len += skip_ws(&data[len]);
839 if (data[len] != ':') {
Radek Krejci48464ed2016-03-17 15:44:09 +0100840 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, (*parent), "JSON data (missing name-separator)");
Radek Krejci5449d472015-10-26 14:35:56 +0100841 goto error;
842 }
843 len++;
844 len += skip_ws(&data[len]);
845
Radek Krejci88f29302015-10-30 15:42:33 +0100846 if (str[0] == '@' && !str[1]) {
847 /* process attribute of the parent object (container or list) */
Radek Krejci2a0efef2016-03-24 15:10:40 +0100848 if (!(*parent)) {
849 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "attribute with no corresponding element to belongs to");
Radek Krejci88f29302015-10-30 15:42:33 +0100850 goto error;
851 }
852
Radek Krejci532e5e92017-02-22 12:59:24 +0100853 r = json_parse_attr((*parent)->schema->module, &attr, &data[len], options);
Radek Krejci88f29302015-10-30 15:42:33 +0100854 if (!r) {
Michal Vasko7675c622017-03-02 10:50:07 +0100855 LOGPATH(LY_VLOG_LYD, *parent);
Radek Krejci88f29302015-10-30 15:42:33 +0100856 goto error;
857 }
858 len += r;
859
860 if ((*parent)->attr) {
861 lyd_free_attr(ctx, NULL, attr, 1);
862 } else {
863 (*parent)->attr = attr;
Radek Krejci532e5e92017-02-22 12:59:24 +0100864 for (; attr; attr = attr->next) {
865 attr->parent = *parent;
866 }
Radek Krejci88f29302015-10-30 15:42:33 +0100867 }
Michal Vasko7675c622017-03-02 10:50:07 +0100868
869 /* check edit-config attribute correctness */
870 if ((options & LYD_OPT_EDIT) && lyp_check_edit_attr(ctx, (*parent)->attr, *parent, NULL)) {
871 goto error;
872 }
873
Radek Krejcic4831272015-11-01 19:26:34 +0100874 free(str);
875 return len;
Radek Krejci88f29302015-10-30 15:42:33 +0100876 }
877
Radek Krejci5449d472015-10-26 14:35:56 +0100878 /* find schema node */
879 if (!(*parent)) {
880 /* starting in root */
881 /* get the proper schema */
882 module = ly_ctx_get_module(ctx, prefix, NULL);
Michal Vaskof53187d2017-01-13 13:23:14 +0100883 if (ctx->data_clb) {
884 if (!module) {
885 module = ctx->data_clb(ctx, prefix, NULL, 0, ctx->data_clb_data);
886 } else if (!module->implemented) {
887 module = ctx->data_clb(ctx, module->name, module->ns, LY_MODCLB_NOT_IMPLEMENTED, ctx->data_clb_data);
888 }
889 }
890 if (module && module->implemented) {
Radek Krejci5449d472015-10-26 14:35:56 +0100891 /* get the proper schema node */
Radek Krejci919a9242016-07-27 08:17:13 +0200892 while ((schema = (struct lys_node *)lys_getnext(schema, NULL, module, 0))) {
Radek Krejci5449d472015-10-26 14:35:56 +0100893 if (!strcmp(schema->name, name)) {
894 break;
895 }
896 }
897 }
898 } else {
Radek Krejci5449d472015-10-26 14:35:56 +0100899 if (prefix) {
Michal Vaskof53187d2017-01-13 13:23:14 +0100900 /* get the proper module to give the chance to load/implement it */
Radek Krejci5449d472015-10-26 14:35:56 +0100901 module = ly_ctx_get_module(ctx, prefix, NULL);
Michal Vaskof53187d2017-01-13 13:23:14 +0100902 if (ctx->data_clb) {
903 if (!module) {
Michal Vaskoad43c182017-01-23 09:55:28 +0100904 ctx->data_clb(ctx, prefix, NULL, 0, ctx->data_clb_data);
Michal Vaskof53187d2017-01-13 13:23:14 +0100905 } else if (!module->implemented) {
Michal Vaskoad43c182017-01-23 09:55:28 +0100906 ctx->data_clb(ctx, module->name, module->ns, LY_MODCLB_NOT_IMPLEMENTED, ctx->data_clb_data);
Michal Vaskof53187d2017-01-13 13:23:14 +0100907 }
Radek Krejci5449d472015-10-26 14:35:56 +0100908 }
909 }
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100910
911 /* go through RPC's input/output following the options' data type */
912 if ((*parent)->schema->nodetype == LYS_RPC) {
Michal Vaskof53187d2017-01-13 13:23:14 +0100913 while ((schema = (struct lys_node *)lys_getnext(schema, (*parent)->schema, NULL, LYS_GETNEXT_WITHINOUT))) {
Michal Vaskoafa7a642016-10-18 15:11:38 +0200914 if ((options & LYD_OPT_RPC) && (schema->nodetype == LYS_INPUT)) {
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100915 break;
Michal Vaskoafa7a642016-10-18 15:11:38 +0200916 } else if ((options & LYD_OPT_RPCREPLY) && (schema->nodetype == LYS_OUTPUT)) {
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100917 break;
918 }
919 }
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100920 schema_parent = schema;
921 schema = NULL;
922 }
923
Michal Vasko36ef6932015-12-01 14:30:17 +0100924 if (schema_parent) {
Michal Vaskof53187d2017-01-13 13:23:14 +0100925 while ((schema = (struct lys_node *)lys_getnext(schema, schema_parent, NULL, 0))) {
926 if (!strcmp(schema->name, name)
927 && ((prefix && !strcmp(lys_node_module(schema)->name, prefix))
928 || (!prefix && (lys_node_module(schema) == lys_node_module(schema_parent))))) {
Michal Vasko36ef6932015-12-01 14:30:17 +0100929 break;
930 }
931 }
932 } else {
Michal Vaskof53187d2017-01-13 13:23:14 +0100933 while ((schema = (struct lys_node *)lys_getnext(schema, (*parent)->schema, NULL, 0))) {
934 if (!strcmp(schema->name, name)
935 && ((prefix && !strcmp(lys_node_module(schema)->name, prefix))
936 || (!prefix && (lys_node_module(schema) == lyd_node_module(*parent))))) {
Michal Vasko36ef6932015-12-01 14:30:17 +0100937 break;
938 }
Radek Krejci5449d472015-10-26 14:35:56 +0100939 }
940 }
941 }
Michal Vaskof53187d2017-01-13 13:23:14 +0100942
943 module = lys_node_module(schema);
944 if (!module || !module->implemented || module->disabled) {
945 LOGVAL(LYE_INELEM, (*parent ? LY_VLOG_LYD : LY_VLOG_NONE), (*parent), name);
Radek Krejci88f29302015-10-30 15:42:33 +0100946 goto error;
Radek Krejci5449d472015-10-26 14:35:56 +0100947 }
Radek Krejci88f29302015-10-30 15:42:33 +0100948
949 if (str[0] == '@') {
Radek Krejci88f29302015-10-30 15:42:33 +0100950 /* attribute for some sibling node */
951 if (data[len] == '[') {
952 flag_leaflist = 1;
953 len++;
954 len += skip_ws(&data[len]);
955 }
956
957attr_repeat:
Radek Krejci532e5e92017-02-22 12:59:24 +0100958 r = json_parse_attr((struct lys_module *)module, &attr, &data[len], options);
Radek Krejci88f29302015-10-30 15:42:33 +0100959 if (!r) {
Radek Krejci48464ed2016-03-17 15:44:09 +0100960 LOGPATH(LY_VLOG_LYD, (*parent));
Radek Krejci88f29302015-10-30 15:42:33 +0100961 goto error;
962 }
963 len += r;
964
965 if (attr) {
Radek Krejcic4831272015-11-01 19:26:34 +0100966 attrs_aux = malloc(sizeof *attrs_aux);
Michal Vasko253035f2015-12-17 16:58:13 +0100967 if (!attrs_aux) {
968 LOGMEM;
969 goto error;
970 }
Radek Krejcic4831272015-11-01 19:26:34 +0100971 attrs_aux->attr = attr;
972 attrs_aux->index = flag_leaflist;
973 attrs_aux->schema = schema;
974 attrs_aux->next = *attrs;
975 *attrs = attrs_aux;
Radek Krejci88f29302015-10-30 15:42:33 +0100976 }
977
978 if (flag_leaflist) {
979 if (data[len] == ',') {
980 len++;
981 len += skip_ws(&data[len]);
982 flag_leaflist++;
983 goto attr_repeat;
984 } else if (data[len] != ']') {
Radek Krejci48464ed2016-03-17 15:44:09 +0100985 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, (*parent), "JSON data (missing end-array)");
Radek Krejci88f29302015-10-30 15:42:33 +0100986 goto error;
987 }
988 len++;
989 len += skip_ws(&data[len]);
990 }
991
Radek Krejcic4831272015-11-01 19:26:34 +0100992 free(str);
993 return len;
Radek Krejci88f29302015-10-30 15:42:33 +0100994 }
995
Radek Krejci5449d472015-10-26 14:35:56 +0100996 switch (schema->nodetype) {
997 case LYS_CONTAINER:
998 case LYS_LIST:
999 case LYS_NOTIF:
1000 case LYS_RPC:
Michal Vaskoafa7a642016-10-18 15:11:38 +02001001 case LYS_ACTION:
Radek Krejci5449d472015-10-26 14:35:56 +01001002 result = calloc(1, sizeof *result);
1003 break;
1004 case LYS_LEAF:
1005 case LYS_LEAFLIST:
1006 result = calloc(1, sizeof(struct lyd_node_leaf_list));
1007 break;
1008 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02001009 case LYS_ANYDATA:
1010 result = calloc(1, sizeof(struct lyd_node_anydata));
Radek Krejci5449d472015-10-26 14:35:56 +01001011 break;
1012 default:
1013 LOGINT;
Radek Krejci595060f2015-10-30 16:29:58 +01001014 goto error;
Radek Krejci5449d472015-10-26 14:35:56 +01001015 }
Michal Vasko253035f2015-12-17 16:58:13 +01001016 if (!result) {
1017 LOGMEM;
1018 goto error;
1019 }
1020
Radek Krejci61767ca2016-09-19 14:21:55 +02001021 result->prev = result;
Radek Krejci5449d472015-10-26 14:35:56 +01001022 result->schema = schema;
Radek Krejci61767ca2016-09-19 14:21:55 +02001023 result->parent = *parent;
1024 diter = NULL;
1025 if (*parent && (*parent)->child && schema->nodetype == LYS_LEAF && (*parent)->schema->nodetype == LYS_LIST &&
1026 (pos = lys_is_key((struct lys_node_list *)(*parent)->schema, (struct lys_node_leaf *)schema))) {
1027 /* it is key and we need to insert it into a correct place */
1028 for (i = 0, diter = (*parent)->child;
1029 diter && i < (pos - 1) && diter->schema->nodetype == LYS_LEAF &&
1030 lys_is_key((struct lys_node_list *)(*parent)->schema, (struct lys_node_leaf *)diter->schema);
1031 i++, diter = diter->next);
1032 if (diter) {
1033 /* out of order insertion - insert list's key to the correct position, before the diter */
1034 if ((*parent)->child == diter) {
1035 (*parent)->child = result;
1036 /* update first_sibling */
1037 first_sibling = result;
1038 }
1039 if (diter->prev->next) {
1040 diter->prev->next = result;
1041 }
1042 result->prev = diter->prev;
1043 diter->prev = result;
1044 result->next = diter;
1045 }
1046 }
1047 if (!diter) {
1048 /* simplified (faster) insert as the last node */
1049 if (*parent && !(*parent)->child) {
1050 (*parent)->child = result;
1051 }
1052 if (prev) {
1053 result->prev = prev;
1054 prev->next = result;
1055
1056 /* fix the "last" pointer */
1057 first_sibling->prev = result;
1058 } else {
1059 result->prev = result;
1060 first_sibling = result;
1061 }
1062 }
Michal Vaskoe3886bb2017-01-02 11:33:28 +01001063 result->validity = ly_new_node_validity(result->schema);
Radek Krejci46165822016-08-26 14:06:27 +02001064 if (resolve_applies_when(schema, 0, NULL)) {
Radek Krejci0b7704f2016-03-18 12:16:14 +01001065 result->when_status = LYD_WHEN;
1066 }
Radek Krejci5449d472015-10-26 14:35:56 +01001067
Radek Krejci5449d472015-10-26 14:35:56 +01001068 /* type specific processing */
1069 if (schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
1070 /* type detection and assigning the value */
Radek Krejci45f41dd2017-02-09 14:11:09 +01001071 r = json_get_value((struct lyd_node_leaf_list *)result, &first_sibling, &data[len], options, unres);
Radek Krejci5449d472015-10-26 14:35:56 +01001072 if (!r) {
1073 goto error;
1074 }
Radek Krejci88f29302015-10-30 15:42:33 +01001075 while(result->next) {
1076 result = result->next;
1077 }
1078
Radek Krejci5449d472015-10-26 14:35:56 +01001079 len += r;
1080 len += skip_ws(&data[len]);
Radek Krejcibf2abff2016-08-23 15:51:52 +02001081 } else if (schema->nodetype & LYS_ANYDATA) {
Radek Krejci4ae82942016-09-19 16:41:06 +02001082 r = json_get_anydata((struct lyd_node_anydata *)result, &data[len]);
Radek Krejci5449d472015-10-26 14:35:56 +01001083 if (!r) {
1084 goto error;
1085 }
1086 len += r;
1087 len += skip_ws(&data[len]);
Michal Vaskoafa7a642016-10-18 15:11:38 +02001088 } else if (schema->nodetype & (LYS_CONTAINER | LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
1089 if (schema->nodetype & (LYS_RPC | LYS_ACTION)) {
1090 if (!(options & LYD_OPT_RPC) || *act_notif) {
1091 LOGVAL(LYE_INELEM, LY_VLOG_LYD, result, schema->name);
Michal Vasko51e5c582017-01-19 14:16:39 +01001092 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Unexpected %s node \"%s\".",
Michal Vaskoafa7a642016-10-18 15:11:38 +02001093 (schema->nodetype == LYS_RPC ? "rpc" : "action"), schema->name);
1094 goto error;
1095 }
1096 *act_notif = result;
1097 } else if (schema->nodetype == LYS_NOTIF) {
1098 if (!(options & LYD_OPT_NOTIF) || *act_notif) {
1099 LOGVAL(LYE_INELEM, LY_VLOG_LYD, result, schema->name);
Michal Vasko51e5c582017-01-19 14:16:39 +01001100 LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Unexpected notification node \"%s\".", schema->name);
Michal Vaskoafa7a642016-10-18 15:11:38 +02001101 goto error;
1102 }
1103 *act_notif = result;
1104 }
1105
Radek Krejci5449d472015-10-26 14:35:56 +01001106 if (data[len] != '{') {
Radek Krejci48464ed2016-03-17 15:44:09 +01001107 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, result, "JSON data (missing begin-object)");
Radek Krejci5449d472015-10-26 14:35:56 +01001108 goto error;
1109 }
1110 len++;
1111 len += skip_ws(&data[len]);
1112
1113 if (data[len] != '}') {
1114 /* non-empty container */
Radek Krejcic4831272015-11-01 19:26:34 +01001115 len--;
1116 diter = NULL;
1117 attrs_aux = NULL;
1118 do {
1119 len++;
1120 len += skip_ws(&data[len]);
1121
Michal Vaskoafa7a642016-10-18 15:11:38 +02001122 r = json_parse_data(ctx, &data[len], NULL, &result, result->child, diter, &attrs_aux, options, unres, act_notif);
Radek Krejcic4831272015-11-01 19:26:34 +01001123 if (!r) {
1124 goto error;
1125 }
1126 len += r;
1127
1128 if (result->child) {
1129 diter = result->child->prev;
1130 }
1131 } while(data[len] == ',');
1132
1133 /* store attributes */
Michal Vasko7675c622017-03-02 10:50:07 +01001134 if (store_attrs(ctx, attrs_aux, result->child, options)) {
Radek Krejci5449d472015-10-26 14:35:56 +01001135 goto error;
1136 }
Radek Krejci5449d472015-10-26 14:35:56 +01001137 }
1138
1139 if (data[len] != '}') {
Radek Krejci48464ed2016-03-17 15:44:09 +01001140 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, result, "JSON data (missing end-object)");
Radek Krejci5449d472015-10-26 14:35:56 +01001141 goto error;
1142 }
1143 len++;
1144 len += skip_ws(&data[len]);
1145
Radek Krejcifb7156e2016-10-27 13:39:56 +02001146 /* if we have empty non-presence container, mark it as default */
Radek Krejci2537fd32016-09-07 16:22:41 +02001147 if (schema->nodetype == LYS_CONTAINER && !result->child &&
Radek Krejcid3e73722016-05-23 12:24:55 +02001148 !result->attr && !((struct lys_node_container *)schema)->presence) {
Radek Krejcifb7156e2016-10-27 13:39:56 +02001149 result->dflt = 1;
Radek Krejci0c0086a2016-03-24 15:20:28 +01001150 }
1151
Radek Krejci5449d472015-10-26 14:35:56 +01001152 } else if (schema->nodetype == LYS_LIST) {
1153 if (data[len] != '[') {
Radek Krejci48464ed2016-03-17 15:44:09 +01001154 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, result, "JSON data (missing begin-array)");
Radek Krejci5449d472015-10-26 14:35:56 +01001155 goto error;
1156 }
1157
1158 list = result;
1159 do {
1160 len++;
1161 len += skip_ws(&data[len]);
Radek Krejci23238922015-10-27 17:13:34 +01001162
Radek Krejcic4831272015-11-01 19:26:34 +01001163 if (data[len] != '{') {
Radek Krejci48464ed2016-03-17 15:44:09 +01001164 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, result,
Radek Krejciadb57612016-02-16 13:34:34 +01001165 "JSON data (missing list instance's begin-object)");
Radek Krejci5449d472015-10-26 14:35:56 +01001166 goto error;
1167 }
Radek Krejcic4831272015-11-01 19:26:34 +01001168 diter = NULL;
1169 attrs_aux = NULL;
1170 do {
1171 len++;
1172 len += skip_ws(&data[len]);
1173
Michal Vaskoafa7a642016-10-18 15:11:38 +02001174 r = json_parse_data(ctx, &data[len], NULL, &list, list->child, diter, &attrs_aux, options, unres, act_notif);
Radek Krejcic4831272015-11-01 19:26:34 +01001175 if (!r) {
1176 goto error;
1177 }
1178 len += r;
1179
Radek Krejci52934692015-11-01 20:08:15 +01001180 if (list->child) {
Radek Krejcic4831272015-11-01 19:26:34 +01001181 diter = list->child->prev;
1182 }
1183 } while(data[len] == ',');
1184
1185 /* store attributes */
Michal Vasko7675c622017-03-02 10:50:07 +01001186 if (store_attrs(ctx, attrs_aux, list->child, options)) {
Radek Krejcic4831272015-11-01 19:26:34 +01001187 goto error;
1188 }
1189
1190 if (data[len] != '}') {
1191 /* expecting end-object */
Radek Krejci48464ed2016-03-17 15:44:09 +01001192 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, result,
Radek Krejciadb57612016-02-16 13:34:34 +01001193 "JSON data (missing list instance's end-object)");
Radek Krejcic4831272015-11-01 19:26:34 +01001194 goto error;
1195 }
1196 len++;
Radek Krejci5449d472015-10-26 14:35:56 +01001197 len += skip_ws(&data[len]);
1198
1199 if (data[len] == ',') {
Radek Krejci93fab982016-02-03 15:58:19 +01001200 /* various validation checks */
Radek Krejci45f41dd2017-02-09 14:11:09 +01001201 if (lyv_data_context(list, options, unres)) {
1202 goto error;
1203 }
1204
Radek Krejci00a0e712016-10-26 10:24:46 +02001205 ly_err_clean(1);
Michal Vaskoad2e44a2017-01-03 10:31:35 +01001206 if (lyv_data_content(list, options, unres) ||
1207 lyv_multicases(list, NULL, prev ? &first_sibling : NULL, 0, NULL)) {
Radek Krejci93fab982016-02-03 15:58:19 +01001208 if (ly_errno) {
1209 goto error;
1210 }
1211 }
Radek Krejci93fab982016-02-03 15:58:19 +01001212
Radek Krejci5449d472015-10-26 14:35:56 +01001213 /* another instance of the list */
1214 new = calloc(1, sizeof *new);
Michal Vasko253035f2015-12-17 16:58:13 +01001215 if (!new) {
1216 goto error;
1217 }
Radek Krejci5449d472015-10-26 14:35:56 +01001218 new->parent = list->parent;
1219 new->prev = list;
1220 list->next = new;
1221
Radek Krejcie1794882017-02-08 13:23:38 +01001222 /* copy the validity and when flags */
1223 new->validity = list->validity;
1224 new->when_status = list->when_status;
1225
Radek Krejci5449d472015-10-26 14:35:56 +01001226 /* fix the "last" pointer */
Radek Krejci2d5525d2016-04-04 15:43:30 +02001227 first_sibling->prev = new;
Radek Krejci5449d472015-10-26 14:35:56 +01001228
1229 new->schema = list->schema;
Radek Krejci5449d472015-10-26 14:35:56 +01001230 list = new;
1231 }
1232 } while (data[len] == ',');
Radek Krejcib915fb92017-02-08 14:12:40 +01001233 result = list;
Radek Krejci5449d472015-10-26 14:35:56 +01001234
1235 if (data[len] != ']') {
Radek Krejci48464ed2016-03-17 15:44:09 +01001236 LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, result, "JSON data (missing end-array)");
Radek Krejci5449d472015-10-26 14:35:56 +01001237 goto error;
1238 }
1239 len++;
1240 len += skip_ws(&data[len]);
1241 }
1242
1243 /* various validation checks */
Michal Vaskoe3886bb2017-01-02 11:33:28 +01001244 if (lyv_data_context(result, options, unres)) {
Michal Vasko10e586f2016-05-18 13:25:30 +02001245 goto error;
1246 }
1247
Radek Krejci00a0e712016-10-26 10:24:46 +02001248 ly_err_clean(1);
Michal Vaskoe3886bb2017-01-02 11:33:28 +01001249 if (lyv_data_content(result, options, unres) ||
1250 lyv_multicases(result, NULL, prev ? &first_sibling : NULL, 0, NULL)) {
Radek Krejci5449d472015-10-26 14:35:56 +01001251 if (ly_errno) {
1252 goto error;
1253 }
1254 }
1255
Radek Krejcica7efb72016-01-18 13:06:01 +01001256 /* validation successful */
Radek Krejci63b79c82016-08-10 10:09:33 +02001257 if (result->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vaskoe3886bb2017-01-02 11:33:28 +01001258 /* postpone checking of unique when there will be all list/leaflist instances */
1259 result->validity |= LYD_VAL_UNIQUE;
Radek Krejci63b79c82016-08-10 10:09:33 +02001260 }
Radek Krejcica7efb72016-01-18 13:06:01 +01001261
Radek Krejci88f29302015-10-30 15:42:33 +01001262 if (!(*parent)) {
1263 *parent = result;
1264 }
1265
Radek Krejcide9d92c2015-10-30 15:59:59 +01001266 free(str);
Radek Krejci5449d472015-10-26 14:35:56 +01001267 return len;
1268
1269error:
Radek Krejci0c0086a2016-03-24 15:20:28 +01001270 len = 0;
Radek Krejci0c0086a2016-03-24 15:20:28 +01001271 /* cleanup */
1272 for (i = unres->count - 1; i >= 0; i--) {
1273 /* remove unres items connected with the node being removed */
1274 if (unres->node[i] == result) {
1275 unres_data_del(unres, i);
1276 }
Radek Krejci88f29302015-10-30 15:42:33 +01001277 }
1278 while (*attrs) {
Radek Krejcic4831272015-11-01 19:26:34 +01001279 attrs_aux = *attrs;
Radek Krejci88f29302015-10-30 15:42:33 +01001280 *attrs = (*attrs)->next;
1281
Radek Krejcic4831272015-11-01 19:26:34 +01001282 lyd_free_attr(ctx, NULL, attrs_aux->attr, 1);
1283 free(attrs_aux);
Radek Krejci88f29302015-10-30 15:42:33 +01001284 }
1285
Radek Krejci5449d472015-10-26 14:35:56 +01001286 lyd_free(result);
Radek Krejci88f29302015-10-30 15:42:33 +01001287 free(str);
1288
Radek Krejci0c0086a2016-03-24 15:20:28 +01001289 return len;
Radek Krejci5449d472015-10-26 14:35:56 +01001290}
1291
1292struct lyd_node *
Michal Vasko945b96b2016-10-18 11:49:12 +02001293lyd_parse_json(struct ly_ctx *ctx, const char *data, int options, const struct lyd_node *rpc_act,
1294 const struct lyd_node *data_tree)
Radek Krejci5449d472015-10-26 14:35:56 +01001295{
Michal Vaskoafa7a642016-10-18 15:11:38 +02001296 struct lyd_node *result = NULL, *next, *iter, *reply_parent = NULL, *reply_top = NULL, *act_notif = NULL;
Radek Krejci5449d472015-10-26 14:35:56 +01001297 struct unres_data *unres = NULL;
Radek Krejcic4831272015-11-01 19:26:34 +01001298 unsigned int len = 0, r;
Michal Vaskoafa7a642016-10-18 15:11:38 +02001299 int i, act_cont = 0;
Radek Krejcic4831272015-11-01 19:26:34 +01001300 struct attr_cont *attrs = NULL;
Radek Krejci63b79c82016-08-10 10:09:33 +02001301 struct ly_set *set;
Radek Krejci5449d472015-10-26 14:35:56 +01001302
Radek Krejci00a0e712016-10-26 10:24:46 +02001303 ly_err_clean(1);
Radek Krejci2342cf62016-01-29 16:48:23 +01001304
Michal Vaskoafa7a642016-10-18 15:11:38 +02001305 if (!ctx || !data) {
Radek Krejci5449d472015-10-26 14:35:56 +01001306 LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__);
1307 return NULL;
1308 }
1309
Radek Krejcic4831272015-11-01 19:26:34 +01001310 /* skip leading whitespaces */
1311 len += skip_ws(&data[len]);
1312
Michal Vasko24d982f2016-04-18 15:13:58 +02001313 /* no data (or whitespaces only) are fine */
1314 if (!data[len]) {
1315 lyd_validate(&result, options, ctx);
1316 return result;
1317 }
1318
Radek Krejcic4831272015-11-01 19:26:34 +01001319 /* expect top-level { */
1320 if (data[len] != '{') {
Radek Krejci48464ed2016-03-17 15:44:09 +01001321 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing top level begin-object)");
Michal Vasko24d982f2016-04-18 15:13:58 +02001322 return NULL;
1323 }
1324
1325 unres = calloc(1, sizeof *unres);
1326 if (!unres) {
1327 LOGMEM;
1328 return NULL;
Radek Krejcic4831272015-11-01 19:26:34 +01001329 }
1330
Michal Vaskoafa7a642016-10-18 15:11:38 +02001331 /* create RPC/action reply part that is not in the parsed data */
1332 if (rpc_act) {
1333 assert(options & LYD_OPT_RPCREPLY);
1334 if (rpc_act->schema->nodetype == LYS_RPC) {
1335 /* RPC request */
1336 reply_top = reply_parent = _lyd_new(NULL, rpc_act->schema, 0);
1337 } else {
1338 /* action request */
1339 reply_top = lyd_dup(rpc_act, 1);
1340 LY_TREE_DFS_BEGIN(reply_top, iter, reply_parent) {
1341 if (reply_parent->schema->nodetype == LYS_ACTION) {
1342 break;
1343 }
1344 LY_TREE_DFS_END(reply_top, iter, reply_parent);
1345 }
1346 if (!reply_parent) {
1347 LOGERR(LY_EINVAL, "%s: invalid variable parameter (const struct lyd_node *rpc_act).", __func__);
1348 lyd_free_withsiblings(reply_top);
1349 goto error;
1350 }
1351 lyd_free_withsiblings(reply_parent->child);
1352 }
1353 }
1354
1355 iter = NULL;
1356 next = reply_parent;
Radek Krejcic4831272015-11-01 19:26:34 +01001357 do {
1358 len++;
1359 len += skip_ws(&data[len]);
1360
Michal Vaskoafa7a642016-10-18 15:11:38 +02001361 if (!act_cont) {
1362 if (!strncmp(&data[len], "\"yang:action\"", 13)) {
1363 len += 13;
1364 len += skip_ws(&data[len]);
1365 if (data[len] != ':') {
1366 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing top-level begin-object)");
1367 lyd_free_withsiblings(reply_top);
1368 goto error;
1369 }
1370 ++len;
1371 len += skip_ws(&data[len]);
1372 if (data[len] != '{') {
1373 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing top level yang:action object)");
1374 lyd_free_withsiblings(reply_top);
1375 goto error;
1376 }
1377 ++len;
1378 len += skip_ws(&data[len]);
1379
1380 act_cont = 1;
1381 } else {
1382 act_cont = -1;
1383 }
1384 }
1385
1386 r = json_parse_data(ctx, &data[len], NULL, &next, result, iter, &attrs, options, unres, &act_notif);
Radek Krejcic4831272015-11-01 19:26:34 +01001387 if (!r) {
Michal Vaskoafa7a642016-10-18 15:11:38 +02001388 lyd_free_withsiblings(reply_top);
Michal Vasko24d982f2016-04-18 15:13:58 +02001389 goto error;
Radek Krejcic4831272015-11-01 19:26:34 +01001390 }
1391 len += r;
1392
1393 if (!result) {
Radek Krejciba024522017-02-20 10:59:58 +01001394 for (iter = next; iter && iter->prev->next; iter = iter->prev);
1395 result = iter;
Radek Krejcic4831272015-11-01 19:26:34 +01001396 }
1397 if (next) {
1398 iter = next;
1399 }
1400 next = NULL;
Michal Vaskoafa7a642016-10-18 15:11:38 +02001401 } while (data[len] == ',');
Radek Krejcic4831272015-11-01 19:26:34 +01001402
1403 if (data[len] != '}') {
1404 /* expecting end-object */
Radek Krejci48464ed2016-03-17 15:44:09 +01001405 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing top-level end-object)");
Michal Vasko24d982f2016-04-18 15:13:58 +02001406 goto error;
Radek Krejcic4831272015-11-01 19:26:34 +01001407 }
1408 len++;
1409 len += skip_ws(&data[len]);
1410
Michal Vaskoafa7a642016-10-18 15:11:38 +02001411 if (act_cont == 1) {
1412 if (data[len] != '}') {
1413 LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing top-level end-object)");
1414 goto error;
1415 }
1416 len++;
1417 len += skip_ws(&data[len]);
1418 }
1419
Radek Krejcic4831272015-11-01 19:26:34 +01001420 /* store attributes */
Michal Vasko7675c622017-03-02 10:50:07 +01001421 if (store_attrs(ctx, attrs, result, options)) {
Michal Vasko24d982f2016-04-18 15:13:58 +02001422 goto error;
Radek Krejcic4831272015-11-01 19:26:34 +01001423 }
Radek Krejci5449d472015-10-26 14:35:56 +01001424
Michal Vaskoafa7a642016-10-18 15:11:38 +02001425 if (reply_top) {
1426 result = reply_top;
1427 }
1428
Michal Vaskoc1cf86f2015-11-04 09:54:51 +01001429 if (!result) {
1430 LOGERR(LY_EVALID, "Model for the data to be linked with not found.");
Michal Vasko24d982f2016-04-18 15:13:58 +02001431 goto error;
Michal Vaskoc1cf86f2015-11-04 09:54:51 +01001432 }
1433
Michal Vaskoafa7a642016-10-18 15:11:38 +02001434 if ((options & LYD_OPT_RPCREPLY) && (rpc_act->schema->nodetype != LYS_RPC)) {
1435 /* action reply */
1436 act_notif = reply_parent;
1437 } else if ((options & (LYD_OPT_RPC | LYD_OPT_NOTIF)) && !act_notif) {
1438 ly_vecode = LYVE_INELEM;
1439 LOGVAL(LYE_SPEC, LY_VLOG_LYD, result, "Missing %s node.", (options & LYD_OPT_RPC ? "action" : "notification"));
1440 goto error;
1441 }
1442
Radek Krejci63b79c82016-08-10 10:09:33 +02001443 /* check for uniquness of top-level lists/leaflists because
1444 * only the inner instances were tested in lyv_data_content() */
1445 set = ly_set_new();
1446 LY_TREE_FOR(result, iter) {
1447 if (!(iter->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(iter->validity & LYD_VAL_UNIQUE)) {
1448 continue;
1449 }
1450
1451 /* check each list/leaflist only once */
1452 i = set->number;
1453 if (ly_set_add(set, iter->schema, 0) != i) {
1454 /* already checked */
1455 continue;
1456 }
1457
1458 if (lyv_data_unique(iter, result)) {
1459 ly_set_free(set);
1460 goto error;
1461 }
1462 }
1463 ly_set_free(set);
1464
Radek Krejci46165822016-08-26 14:06:27 +02001465 /* add/validate default values, unres */
Michal Vaskoafa7a642016-10-18 15:11:38 +02001466 if (lyd_defaults_add_unres(&result, options, ctx, data_tree, act_notif, unres)) {
Michal Vasko24d982f2016-04-18 15:13:58 +02001467 goto error;
Radek Krejci5c162452016-03-23 13:36:01 +01001468 }
1469
Radek Krejci46165822016-08-26 14:06:27 +02001470 /* check for missing top level mandatory nodes */
Michal Vaskoad2e44a2017-01-03 10:31:35 +01001471 if (!(options & (LYD_OPT_TRUSTED | LYD_OPT_NOTIF_FILTER))
1472 && lyd_check_mandatory_tree((act_notif ? act_notif : result), ctx, options)) {
Michal Vasko24d982f2016-04-18 15:13:58 +02001473 goto error;
Radek Krejci5c162452016-03-23 13:36:01 +01001474 }
1475
Radek Krejci5449d472015-10-26 14:35:56 +01001476 free(unres->node);
1477 free(unres->type);
Radek Krejci5449d472015-10-26 14:35:56 +01001478 free(unres);
1479
1480 return result;
Michal Vasko24d982f2016-04-18 15:13:58 +02001481
1482error:
1483 lyd_free_withsiblings(result);
1484 free(unres->node);
1485 free(unres->type);
1486 free(unres);
1487
1488 return NULL;
Radek Krejci5449d472015-10-26 14:35:56 +01001489}