Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1 | /** |
| 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 Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 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 |
Michal Vasko | 8de098c | 2016-02-26 10:00:25 +0100 | [diff] [blame] | 11 | * |
Radek Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 12 | * https://opensource.org/licenses/BSD-3-Clause |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 13 | */ |
| 14 | |
Radek Krejci | 9a5daea | 2016-03-02 16:49:40 +0100 | [diff] [blame] | 15 | #define _GNU_SOURCE |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 16 | #include <assert.h> |
| 17 | #include <ctype.h> |
| 18 | #include <limits.h> |
Michal Vasko | 1e676ca | 2016-06-24 15:23:54 +0200 | [diff] [blame] | 19 | #include <errno.h> |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 20 | #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 Krejci | 9a5daea | 2016-03-02 16:49:40 +0100 | [diff] [blame] | 30 | #include "xml_internal.h" |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 31 | |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 32 | static int |
| 33 | lyjson_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 | |
| 46 | static unsigned int |
| 47 | skip_ws(const char *data) |
| 48 | { |
| 49 | unsigned int len = 0; |
| 50 | |
| 51 | /* skip leading whitespaces */ |
| 52 | while (data[len] && lyjson_isspace(data[len])) { |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 53 | len++; |
| 54 | } |
| 55 | |
| 56 | return len; |
| 57 | } |
| 58 | |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 59 | static char * |
| 60 | lyjson_parse_text(const char *data, unsigned int *len) |
| 61 | { |
| 62 | #define BUFSIZE 1024 |
| 63 | |
| 64 | char buf[BUFSIZE]; |
| 65 | char *result = NULL, *aux; |
Radek Krejci | 967e4bf | 2015-11-28 10:06:40 +0100 | [diff] [blame] | 66 | int o, size = 0; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 67 | 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 Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 75 | aux = ly_realloc(result, size + 1); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 76 | LY_CHECK_ERR_RETURN(!aux, LOGMEM, NULL); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 77 | result = aux; |
| 78 | } else { |
| 79 | size = o; |
| 80 | result = malloc((size + 1) * sizeof *result); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 81 | LY_CHECK_ERR_RETURN(!result, LOGMEM, NULL); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 82 | } |
| 83 | memcpy(&result[size - o], buf, o); |
| 84 | |
| 85 | /* write again into the beginning of the buffer */ |
| 86 | o = 0; |
| 87 | } |
| 88 | |
| 89 | if (data[*len] == '\\') { |
| 90 | /* parse escape sequence */ |
| 91 | (*len)++; |
| 92 | i = 1; |
| 93 | switch (data[(*len)]) { |
| 94 | case '"': |
| 95 | /* quotation mark */ |
| 96 | value = 0x22; |
| 97 | break; |
| 98 | case '\\': |
| 99 | /* reverse solidus */ |
| 100 | value = 0x5c; |
| 101 | break; |
| 102 | case '/': |
| 103 | /* solidus */ |
| 104 | value = 0x2f; |
| 105 | break; |
| 106 | case 'b': |
| 107 | /* backspace */ |
| 108 | value = 0x08; |
| 109 | break; |
| 110 | case 'f': |
| 111 | /* form feed */ |
| 112 | value = 0x0c; |
| 113 | break; |
| 114 | case 'n': |
| 115 | /* line feed */ |
| 116 | value = 0x0a; |
| 117 | break; |
| 118 | case 'r': |
| 119 | /* carriage return */ |
| 120 | value = 0x0d; |
| 121 | break; |
| 122 | case 't': |
| 123 | /* tab */ |
| 124 | value = 0x09; |
| 125 | break; |
| 126 | case 'u': |
| 127 | /* Basic Multilingual Plane character \uXXXX */ |
| 128 | (*len)++; |
| 129 | for (value = i = 0; i < 4; i++) { |
| 130 | if (isdigit(data[(*len) + i])) { |
| 131 | r = (data[(*len) + i] - '0'); |
| 132 | } else if (data[(*len) + i] > 'F') { |
| 133 | r = 10 + (data[(*len) + i] - 'a'); |
| 134 | } else { |
| 135 | r = 10 + (data[(*len) + i] - 'A'); |
| 136 | } |
| 137 | value = (16 * value) + r; |
| 138 | } |
| 139 | break; |
| 140 | default: |
| 141 | /* invalid escape sequence */ |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 142 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "character escape sequence"); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 143 | goto error; |
| 144 | |
| 145 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 146 | r = pututf8(&buf[o], value); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 147 | if (!r) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 148 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "character UTF8 character"); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 149 | goto error; |
| 150 | } |
| 151 | o += r - 1; /* o is ++ in for loop */ |
| 152 | (*len) += i; /* number of read characters */ |
zyinter2008 | 7fa6573 | 2016-11-17 10:55:12 +0800 | [diff] [blame] | 153 | } else if ((data[*len] >= 0 && data[*len] < 0x20) || data[*len] == 0x5c) { |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 154 | /* control characters must be escaped */ |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 155 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "control character (unescaped)"); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 156 | goto error; |
| 157 | } else { |
| 158 | /* unescaped character */ |
Radek Krejci | deee60e | 2016-09-23 15:21:14 +0200 | [diff] [blame] | 159 | r = copyutf8(&buf[o], &data[*len]); |
| 160 | if (!r) { |
| 161 | goto error; |
| 162 | } |
| 163 | |
| 164 | o += r - 1; /* o is ++ in for loop */ |
| 165 | (*len) += r; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
| 169 | #undef BUFSIZE |
| 170 | |
| 171 | if (o) { |
| 172 | if (result) { |
| 173 | size = size + o; |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 174 | aux = ly_realloc(result, size + 1); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 175 | LY_CHECK_ERR_RETURN(!aux, LOGMEM, NULL); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 176 | result = aux; |
| 177 | } else { |
| 178 | size = o; |
| 179 | result = malloc((size + 1) * sizeof *result); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 180 | LY_CHECK_ERR_RETURN(!result, LOGMEM, NULL); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 181 | } |
| 182 | memcpy(&result[size - o], buf, o); |
| 183 | } |
| 184 | if (result) { |
| 185 | result[size] = '\0'; |
| 186 | } else { |
| 187 | size = 0; |
| 188 | result = strdup(""); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 189 | LY_CHECK_ERR_RETURN(!result, LOGMEM, NULL); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | return result; |
| 193 | |
| 194 | error: |
| 195 | free(result); |
| 196 | return NULL; |
| 197 | } |
| 198 | |
| 199 | static unsigned int |
| 200 | lyjson_parse_number(const char *data) |
| 201 | { |
Michal Vasko | 8f32c11 | 2016-05-18 13:22:59 +0200 | [diff] [blame] | 202 | unsigned int len = 0; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 203 | |
Michal Vasko | 8f32c11 | 2016-05-18 13:22:59 +0200 | [diff] [blame] | 204 | if (data[len] == '-') { |
| 205 | ++len; |
| 206 | } |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 207 | |
Michal Vasko | 8f32c11 | 2016-05-18 13:22:59 +0200 | [diff] [blame] | 208 | if (data[len] == '0') { |
| 209 | ++len; |
| 210 | } else if (isdigit(data[len])) { |
| 211 | ++len; |
| 212 | while (isdigit(data[len])) { |
| 213 | ++len; |
| 214 | } |
| 215 | } else { |
| 216 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid character in JSON Number value ('%c').", data[len]); |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | if (data[len] == '.') { |
| 221 | ++len; |
| 222 | if (!isdigit(data[len])) { |
| 223 | if (data[len]) { |
| 224 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid character in JSON Number value ('%c').", data[len]); |
| 225 | } else { |
| 226 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid character in JSON Number value (EOF)."); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 227 | } |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 228 | return 0; |
| 229 | } |
Michal Vasko | 8f32c11 | 2016-05-18 13:22:59 +0200 | [diff] [blame] | 230 | while (isdigit(data[len])) { |
| 231 | ++len; |
| 232 | } |
| 233 | } |
| 234 | |
Michal Vasko | 1e676ca | 2016-06-24 15:23:54 +0200 | [diff] [blame] | 235 | if ((data[len] == 'e') || (data[len] == 'E')) { |
| 236 | ++len; |
| 237 | if ((data[len] == '+') || (data[len] == '-')) { |
| 238 | ++len; |
| 239 | } |
| 240 | while (isdigit(data[len])) { |
| 241 | ++len; |
| 242 | } |
| 243 | } |
| 244 | |
Michal Vasko | 8f32c11 | 2016-05-18 13:22:59 +0200 | [diff] [blame] | 245 | if (data[len] && (data[len] != ',') && (data[len] != ']') && (data[len] != '}') && !lyjson_isspace(data[len])) { |
| 246 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Invalid character in JSON Number value ('%c').", data[len]); |
| 247 | return 0; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | return len; |
| 251 | } |
| 252 | |
Michal Vasko | 1e676ca | 2016-06-24 15:23:54 +0200 | [diff] [blame] | 253 | static char * |
| 254 | lyjson_convert_enumber(const char *number, unsigned int num_len, char *e_ptr) |
| 255 | { |
| 256 | char *ptr, *num; |
| 257 | const char *number_ptr; |
| 258 | long int e_val; |
| 259 | int dot_pos, chars_to_dot, minus; |
| 260 | unsigned int num_len_no_e; |
| 261 | |
| 262 | if (*number == '-') { |
| 263 | minus = 1; |
| 264 | ++number; |
| 265 | --num_len; |
| 266 | } else { |
| 267 | minus = 0; |
| 268 | } |
| 269 | |
| 270 | num_len_no_e = e_ptr - number; |
| 271 | |
| 272 | errno = 0; |
| 273 | ++e_ptr; |
| 274 | e_val = strtol(e_ptr, &ptr, 10); |
| 275 | if (errno) { |
| 276 | LOGVAL(LYE_SPEC, LY_VLOG_NONE, NULL, "Exponent out-of-bounds in a JSON Number value (%.*s).", |
| 277 | num_len - (e_ptr - number), e_ptr); |
| 278 | return NULL; |
| 279 | } else if (ptr != number + num_len) { |
| 280 | /* we checked this already */ |
| 281 | LOGINT; |
| 282 | return NULL; |
| 283 | } |
| 284 | |
| 285 | if ((ptr = strnchr(number, '.', num_len_no_e))) { |
| 286 | dot_pos = ptr - number; |
| 287 | } else { |
| 288 | dot_pos = num_len_no_e; |
| 289 | } |
| 290 | |
| 291 | dot_pos += e_val; |
| 292 | |
| 293 | /* allocate enough memory */ |
| 294 | if (dot_pos < 1) { |
| 295 | /* (.XXX)XXX[.]XXXX */ |
| 296 | num = malloc((minus ? 1 : 0) + -dot_pos + 2 + (num_len_no_e - (ptr ? 1 : 0)) + 1); |
| 297 | } else if (dot_pos < (signed)num_len_no_e) { |
| 298 | /* XXXX(.)XX.XXX */ |
| 299 | num = malloc((minus ? 1 : 0) + num_len_no_e + (ptr ? 0 : 1) + 1); |
| 300 | } else { |
| 301 | /* XXX[.]XXXX(XXX.) */ |
| 302 | num = malloc((minus ? 1 : 0) + (dot_pos - (ptr ? 2 : 1)) + 1); |
| 303 | } |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 304 | LY_CHECK_ERR_RETURN(!num, LOGMEM, NULL); |
Michal Vasko | 1e676ca | 2016-06-24 15:23:54 +0200 | [diff] [blame] | 305 | if (minus) { |
| 306 | strcpy(num, "-"); |
| 307 | } else { |
| 308 | num[0] = '\0'; |
| 309 | } |
| 310 | |
| 311 | if (dot_pos < 1) { |
| 312 | strcat(num, "0."); |
| 313 | } |
| 314 | if (dot_pos < 0) { |
| 315 | sprintf(num + strlen(num), "%0*d", -dot_pos, 0); |
| 316 | } |
| 317 | |
| 318 | chars_to_dot = dot_pos; |
Michal Vasko | 9d1593d | 2016-09-30 12:33:34 +0200 | [diff] [blame] | 319 | for (ptr = num + strlen(num), number_ptr = number; (unsigned)(number_ptr - number) < num_len_no_e; ) { |
Michal Vasko | 1e676ca | 2016-06-24 15:23:54 +0200 | [diff] [blame] | 320 | if (!chars_to_dot) { |
| 321 | *ptr = '.'; |
| 322 | ++ptr; |
| 323 | chars_to_dot = -1; |
| 324 | } else if (isdigit(*number_ptr)) { |
| 325 | *ptr = *number_ptr; |
| 326 | ++ptr; |
| 327 | ++number_ptr; |
| 328 | if (chars_to_dot > 0) { |
| 329 | --chars_to_dot; |
| 330 | } |
| 331 | } else if (*number_ptr == '.') { |
| 332 | ++number_ptr; |
| 333 | } else { |
| 334 | LOGINT; |
| 335 | free(num); |
| 336 | return NULL; |
| 337 | } |
| 338 | } |
| 339 | *ptr = '\0'; |
| 340 | |
| 341 | if (dot_pos > (signed)num_len_no_e) { |
| 342 | sprintf(num + strlen(num), "%0*d", dot_pos - num_len_no_e, 0); |
| 343 | } |
| 344 | |
| 345 | return num; |
| 346 | } |
| 347 | |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 348 | static unsigned int |
| 349 | lyjson_parse_boolean(const char *data) |
| 350 | { |
Radek Krejci | 6b47b50 | 2015-10-30 15:52:41 +0100 | [diff] [blame] | 351 | unsigned int len = 0; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 352 | |
| 353 | if (!strncmp(data, "false", 5)) { |
| 354 | len = 5; |
| 355 | } else if (!strncmp(data, "true", 4)) { |
| 356 | len = 4; |
| 357 | } |
| 358 | |
| 359 | if (data[len] && data[len] != ',' && data[len] != ']' && data[len] != '}' && !lyjson_isspace(data[len])) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 360 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON literal value (expected true or false)"); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | return len; |
| 365 | } |
| 366 | |
| 367 | static unsigned int |
Radek Krejci | 4ae8294 | 2016-09-19 16:41:06 +0200 | [diff] [blame] | 368 | json_get_anydata(struct lyd_node_anydata *any, const char *data) |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 369 | { |
Radek Krejci | a717767 | 2016-11-17 14:53:03 +0900 | [diff] [blame] | 370 | unsigned int len = 0, start, stop, c = 0; |
| 371 | char *str; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 372 | |
Radek Krejci | a717767 | 2016-11-17 14:53:03 +0900 | [diff] [blame] | 373 | /* anydata (as well as meaningful anyxml) is supposed to be encoded as object, |
| 374 | * anyxml can be a string value, other JSON types are not supported since it is |
| 375 | * not clear how they are supposed to be represented/converted into an internal representation */ |
| 376 | if (data[len] == '"' && any->schema->nodetype == LYS_ANYXML) { |
| 377 | len = 1; |
| 378 | str = lyjson_parse_text(&data[len], &c); |
| 379 | if (!str) { |
| 380 | return 0; |
| 381 | } |
| 382 | if (data[len + c] != '"') { |
Radek Krejci | 7d6d284 | 2016-11-28 11:21:38 +0100 | [diff] [blame] | 383 | free(str); |
Radek Krejci | a717767 | 2016-11-17 14:53:03 +0900 | [diff] [blame] | 384 | LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, any, |
| 385 | "JSON data (missing quotation-mark at the end of string)"); |
| 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | any->value.str = lydict_insert_zc(any->schema->module->ctx, str); |
| 390 | any->value_type = LYD_ANYDATA_CONSTSTRING; |
| 391 | return len + c + 1; |
| 392 | } else if (data[len] != '{') { |
| 393 | LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, any, "Unsupported Anydata/anyxml content (not an object nor string)"); |
Michal Vasko | a19f7d7 | 2016-05-18 13:24:08 +0200 | [diff] [blame] | 394 | return 0; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 395 | } |
| 396 | |
Radek Krejci | a717767 | 2016-11-17 14:53:03 +0900 | [diff] [blame] | 397 | /* count opening '{' and closing '}' brackets to get the end of the object without its parsing */ |
| 398 | c = len = 1; |
Radek Krejci | 4ae8294 | 2016-09-19 16:41:06 +0200 | [diff] [blame] | 399 | len += skip_ws(&data[len]); |
Radek Krejci | a717767 | 2016-11-17 14:53:03 +0900 | [diff] [blame] | 400 | start = len; |
| 401 | stop = start - 1; |
Radek Krejci | 4ae8294 | 2016-09-19 16:41:06 +0200 | [diff] [blame] | 402 | while (data[len] && c) { |
| 403 | switch (data[len]) { |
| 404 | case '{': |
| 405 | c++; |
| 406 | break; |
| 407 | case '}': |
| 408 | c--; |
| 409 | break; |
| 410 | default: |
| 411 | if (!isspace(data[len])) { |
| 412 | stop = len; |
| 413 | } |
| 414 | } |
| 415 | len++; |
| 416 | } |
| 417 | if (c) { |
| 418 | LOGVAL(LYE_EOF, LY_VLOG_LYD, any); |
| 419 | return 0; |
| 420 | } |
| 421 | any->value_type = LYD_ANYDATA_JSON; |
Radek Krejci | a717767 | 2016-11-17 14:53:03 +0900 | [diff] [blame] | 422 | if (stop >= start) { |
| 423 | any->value.str = lydict_insert(any->schema->module->ctx, &data[start], stop - start + 1); |
| 424 | } /* else no data */ |
Radek Krejci | 4ae8294 | 2016-09-19 16:41:06 +0200 | [diff] [blame] | 425 | |
Michal Vasko | a19f7d7 | 2016-05-18 13:24:08 +0200 | [diff] [blame] | 426 | return len; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | static unsigned int |
Radek Krejci | 45f41dd | 2017-02-09 14:11:09 +0100 | [diff] [blame] | 430 | json_get_value(struct lyd_node_leaf_list *leaf, struct lyd_node **first_sibling, const char *data, int options, |
| 431 | struct unres_data *unres) |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 432 | { |
Radek Krejci | bd93012 | 2016-08-10 13:28:26 +0200 | [diff] [blame] | 433 | struct lyd_node_leaf_list *new; |
Radek Krejci | 37b756f | 2016-01-18 10:15:03 +0100 | [diff] [blame] | 434 | struct lys_type *stype; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 435 | struct ly_ctx *ctx; |
| 436 | unsigned int len = 0, r; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 437 | char *str; |
| 438 | |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 439 | assert(leaf && data); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 440 | ctx = leaf->schema->module->ctx; |
| 441 | |
| 442 | stype = &((struct lys_node_leaf *)leaf->schema)->type; |
Radek Krejci | 2323892 | 2015-10-27 17:13:34 +0100 | [diff] [blame] | 443 | |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 444 | if (leaf->schema->nodetype == LYS_LEAFLIST) { |
| 445 | /* expecting begin-array */ |
| 446 | if (data[len++] != '[') { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 447 | LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, leaf, "JSON data (expected begin-array)"); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 448 | return 0; |
| 449 | } |
| 450 | |
| 451 | repeat: |
| 452 | len += skip_ws(&data[len]); |
| 453 | } |
| 454 | |
| 455 | /* will be changed in case of union */ |
| 456 | leaf->value_type = stype->base; |
| 457 | |
| 458 | if (data[len] == '"') { |
| 459 | /* string representations */ |
Michal Vasko | 6baed1c | 2016-05-18 13:24:44 +0200 | [diff] [blame] | 460 | ++len; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 461 | str = lyjson_parse_text(&data[len], &r); |
Radek Krejci | 2323892 | 2015-10-27 17:13:34 +0100 | [diff] [blame] | 462 | if (!str) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 463 | LOGPATH(LY_VLOG_LYD, leaf); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 464 | return 0; |
| 465 | } |
| 466 | leaf->value_str = lydict_insert_zc(ctx, str); |
| 467 | if (data[len + r] != '"') { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 468 | LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, leaf, |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 469 | "JSON data (missing quotation-mark at the end of string)"); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 470 | return 0; |
| 471 | } |
| 472 | len += r + 1; |
| 473 | } else if (data[len] == '-' || isdigit(data[len])) { |
| 474 | /* numeric type */ |
| 475 | r = lyjson_parse_number(&data[len]); |
| 476 | if (!r) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 477 | LOGPATH(LY_VLOG_LYD, leaf); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 478 | return 0; |
| 479 | } |
Michal Vasko | 1e676ca | 2016-06-24 15:23:54 +0200 | [diff] [blame] | 480 | /* if it's a number with 'e' or 'E', get rid of it first */ |
| 481 | if ((str = strnchr(&data[len], 'e', r)) || (str = strnchr(&data[len], 'E', r))) { |
| 482 | str = lyjson_convert_enumber(&data[len], r, str); |
| 483 | if (!str) { |
| 484 | return 0; |
| 485 | } |
| 486 | leaf->value_str = lydict_insert_zc(ctx, str); |
| 487 | } else { |
| 488 | leaf->value_str = lydict_insert(ctx, &data[len], r); |
| 489 | } |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 490 | len += r; |
| 491 | } else if (data[len] == 'f' || data[len] == 't') { |
| 492 | /* boolean */ |
| 493 | r = lyjson_parse_boolean(&data[len]); |
| 494 | if (!r) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 495 | LOGPATH(LY_VLOG_LYD, leaf); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 496 | return 0; |
| 497 | } |
| 498 | leaf->value_str = lydict_insert(ctx, &data[len], r); |
| 499 | len += r; |
| 500 | } else if (!strncmp(&data[len], "[null]", 6)) { |
| 501 | /* empty */ |
Michal Vasko | 4491384 | 2016-04-13 14:20:41 +0200 | [diff] [blame] | 502 | leaf->value_str = lydict_insert(ctx, "", 0); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 503 | len += 6; |
| 504 | } else { |
| 505 | /* error */ |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 506 | LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, leaf, "JSON data (unexpected value)"); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 507 | return 0; |
| 508 | } |
| 509 | |
Radek Krejci | 1899d6a | 2016-11-03 13:48:07 +0100 | [diff] [blame] | 510 | /* the value is here converted to a JSON format if needed in case of LY_TYPE_IDENT and LY_TYPE_INST or to a |
| 511 | * canonical form of the value */ |
Michal Vasko | 31a2d32 | 2018-01-12 13:36:12 +0100 | [diff] [blame] | 512 | if (!lyp_parse_value(&((struct lys_node_leaf *)leaf->schema)->type, &leaf->value_str, NULL, leaf, NULL, NULL, 1, 0)) { |
Radek Krejci | 2323892 | 2015-10-27 17:13:34 +0100 | [diff] [blame] | 513 | ly_errno = LY_EVALID; |
| 514 | return 0; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | if (leaf->schema->nodetype == LYS_LEAFLIST) { |
| 518 | /* repeat until end-array */ |
| 519 | len += skip_ws(&data[len]); |
| 520 | if (data[len] == ',') { |
Radek Krejci | 45f41dd | 2017-02-09 14:11:09 +0100 | [diff] [blame] | 521 | /* various validation checks */ |
| 522 | if (lyv_data_context((struct lyd_node*)leaf, options, unres)) { |
| 523 | return 0; |
| 524 | } |
| 525 | |
Radek Krejci | cf74825 | 2017-09-04 11:11:14 +0200 | [diff] [blame] | 526 | ly_err_clean(ly_parser_data.ctx, 1); |
Radek Krejci | 45f41dd | 2017-02-09 14:11:09 +0100 | [diff] [blame] | 527 | if (lyv_data_content((struct lyd_node*)leaf, options, unres) || |
| 528 | lyv_multicases((struct lyd_node*)leaf, NULL, first_sibling, 0, NULL)) { |
| 529 | if (ly_errno) { |
| 530 | return 0; |
| 531 | } |
| 532 | } |
| 533 | |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 534 | /* another instance of the leaf-list */ |
| 535 | new = calloc(1, sizeof(struct lyd_node_leaf_list)); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 536 | LY_CHECK_ERR_RETURN(!new, LOGMEM, 0); |
| 537 | |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 538 | new->parent = leaf->parent; |
| 539 | new->prev = (struct lyd_node *)leaf; |
| 540 | leaf->next = (struct lyd_node *)new; |
| 541 | |
Radek Krejci | e179488 | 2017-02-08 13:23:38 +0100 | [diff] [blame] | 542 | /* copy the validity and when flags */ |
| 543 | new->validity = leaf->validity; |
| 544 | new->when_status = leaf->when_status; |
| 545 | |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 546 | /* fix the "last" pointer */ |
Radek Krejci | 45f41dd | 2017-02-09 14:11:09 +0100 | [diff] [blame] | 547 | (*first_sibling)->prev = (struct lyd_node *)new; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 548 | |
| 549 | new->schema = leaf->schema; |
| 550 | |
| 551 | /* repeat value parsing */ |
| 552 | leaf = new; |
| 553 | len++; |
| 554 | goto repeat; |
| 555 | } else if (data[len] == ']') { |
| 556 | len++; |
| 557 | len += skip_ws(&data[len]); |
| 558 | } else { |
| 559 | /* something unexpected */ |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 560 | LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, leaf, "JSON data (expecting value-separator or end-array)"); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 561 | return 0; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | len += skip_ws(&data[len]); |
| 566 | return len; |
| 567 | } |
| 568 | |
| 569 | static unsigned int |
Radek Krejci | 532e5e9 | 2017-02-22 12:59:24 +0100 | [diff] [blame] | 570 | json_parse_attr(struct lys_module *parent_module, struct lyd_attr **attr, const char *data, int options) |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 571 | { |
| 572 | unsigned int len = 0, r; |
Radek Krejci | 532e5e9 | 2017-02-22 12:59:24 +0100 | [diff] [blame] | 573 | char *str = NULL, *name, *prefix = NULL, *value; |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 574 | struct lys_module *module = parent_module; |
| 575 | struct lyd_attr *attr_new, *attr_last = NULL; |
Michal Vasko | 7675c62 | 2017-03-02 10:50:07 +0100 | [diff] [blame] | 576 | int ret; |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 577 | |
Radek Krejci | de9d92c | 2015-10-30 15:59:59 +0100 | [diff] [blame] | 578 | *attr = NULL; |
| 579 | |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 580 | if (data[len] != '{') { |
| 581 | if (!strncmp(&data[len], "null", 4)) { |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 582 | len += 4; |
| 583 | len += skip_ws(&data[len]); |
| 584 | return len; |
| 585 | } |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 586 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing begin-object)"); |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 587 | goto error; |
| 588 | } |
| 589 | |
| 590 | repeat: |
Radek Krejci | 6a6326d | 2017-02-27 13:00:52 +0100 | [diff] [blame] | 591 | prefix = NULL; |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 592 | len++; |
| 593 | len += skip_ws(&data[len]); |
| 594 | |
| 595 | if (data[len] != '"') { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 596 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing quotation-mark at the begining of string)"); |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 597 | return 0; |
| 598 | } |
| 599 | len++; |
| 600 | str = lyjson_parse_text(&data[len], &r); |
| 601 | if (!r) { |
| 602 | goto error; |
| 603 | } else if (data[len + r] != '"') { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 604 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing quotation-mark at the end of string)"); |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 605 | goto error; |
| 606 | } |
| 607 | if ((name = strchr(str, ':'))) { |
| 608 | *name = '\0'; |
| 609 | name++; |
| 610 | prefix = str; |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 611 | module = (struct lys_module *)ly_ctx_get_module(parent_module->ctx, prefix, NULL, 1); |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 612 | if (!module) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 613 | LOGVAL(LYE_INELEM, LY_VLOG_NONE, NULL, name); |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 614 | goto error; |
| 615 | } |
| 616 | } else { |
| 617 | name = str; |
| 618 | } |
| 619 | |
| 620 | /* prepare data for parsing node content */ |
| 621 | len += r + 1; |
| 622 | len += skip_ws(&data[len]); |
| 623 | if (data[len] != ':') { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 624 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing name-separator)"); |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 625 | goto error; |
| 626 | } |
| 627 | len++; |
| 628 | len += skip_ws(&data[len]); |
| 629 | |
| 630 | if (data[len] != '"') { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 631 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing quotation-mark at the beginning of string)"); |
Radek Krejci | de9d92c | 2015-10-30 15:59:59 +0100 | [diff] [blame] | 632 | goto error; |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 633 | } |
| 634 | len++; |
| 635 | value = lyjson_parse_text(&data[len], &r); |
| 636 | if (!r) { |
| 637 | goto error; |
| 638 | } else if (data[len + r] != '"') { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 639 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing quotation-mark at the end of string)"); |
Radek Krejci | de9d92c | 2015-10-30 15:59:59 +0100 | [diff] [blame] | 640 | free(value); |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 641 | goto error; |
| 642 | } |
| 643 | len += r + 1; |
| 644 | len += skip_ws(&data[len]); |
| 645 | |
Michal Vasko | 7675c62 | 2017-03-02 10:50:07 +0100 | [diff] [blame] | 646 | ret = lyp_fill_attr(parent_module->ctx, NULL, NULL, prefix, name, value, NULL, &attr_new); |
| 647 | if (ret == -1) { |
Radek Krejci | 532e5e9 | 2017-02-22 12:59:24 +0100 | [diff] [blame] | 648 | free(value); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 649 | goto error; |
Michal Vasko | 7675c62 | 2017-03-02 10:50:07 +0100 | [diff] [blame] | 650 | } else if (ret == 1) { |
| 651 | if (options & LYD_OPT_STRICT) { |
| 652 | LOGVAL(LYE_INMETA, LY_VLOG_NONE, NULL, prefix, name, value); |
| 653 | free(value); |
| 654 | goto error; |
| 655 | } |
Radek Krejci | 532e5e9 | 2017-02-22 12:59:24 +0100 | [diff] [blame] | 656 | |
Michal Vasko | 7675c62 | 2017-03-02 10:50:07 +0100 | [diff] [blame] | 657 | LOGWRN("Unknown \"%s:%s\" metadata with value \"%s\", ignoring.", |
| 658 | (prefix ? prefix : "<none>"), name, value); |
| 659 | free(value); |
| 660 | goto next; |
Radek Krejci | 532e5e9 | 2017-02-22 12:59:24 +0100 | [diff] [blame] | 661 | } |
Michal Vasko | 7675c62 | 2017-03-02 10:50:07 +0100 | [diff] [blame] | 662 | free(value); |
Radek Krejci | a571d94 | 2017-02-24 09:26:49 +0100 | [diff] [blame] | 663 | |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 664 | if (!attr_last) { |
| 665 | *attr = attr_last = attr_new; |
| 666 | } else { |
| 667 | attr_last->next = attr_new; |
| 668 | attr_last = attr_new; |
| 669 | } |
| 670 | |
Radek Krejci | 532e5e9 | 2017-02-22 12:59:24 +0100 | [diff] [blame] | 671 | next: |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 672 | free(str); |
Radek Krejci | de9d92c | 2015-10-30 15:59:59 +0100 | [diff] [blame] | 673 | str = NULL; |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 674 | |
| 675 | if (data[len] == ',') { |
| 676 | goto repeat; |
| 677 | } else if (data[len] != '}') { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 678 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing end-object)"); |
Radek Krejci | de9d92c | 2015-10-30 15:59:59 +0100 | [diff] [blame] | 679 | goto error; |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 680 | } |
| 681 | len++; |
| 682 | len += skip_ws(&data[len]); |
| 683 | |
| 684 | return len; |
| 685 | |
| 686 | error: |
| 687 | free(str); |
Radek Krejci | de9d92c | 2015-10-30 15:59:59 +0100 | [diff] [blame] | 688 | if (*attr) { |
Radek Krejci | 532e5e9 | 2017-02-22 12:59:24 +0100 | [diff] [blame] | 689 | lyd_free_attr(module->ctx, NULL, *attr, 1); |
Radek Krejci | de9d92c | 2015-10-30 15:59:59 +0100 | [diff] [blame] | 690 | *attr = NULL; |
| 691 | } |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 692 | return 0; |
| 693 | } |
| 694 | |
| 695 | struct attr_cont { |
| 696 | struct attr_cont *next; |
| 697 | struct lyd_attr *attr; |
| 698 | struct lys_node *schema; |
| 699 | unsigned int index; /** non-zero only in case of leaf-list */ |
| 700 | }; |
| 701 | |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 702 | static int |
Michal Vasko | 7675c62 | 2017-03-02 10:50:07 +0100 | [diff] [blame] | 703 | store_attrs(struct ly_ctx *ctx, struct attr_cont *attrs, struct lyd_node *first, int options) |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 704 | { |
| 705 | struct lyd_node *diter; |
| 706 | struct attr_cont *iter; |
Radek Krejci | 532e5e9 | 2017-02-22 12:59:24 +0100 | [diff] [blame] | 707 | struct lyd_attr *aiter; |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 708 | unsigned int flag_leaflist = 0; |
| 709 | |
| 710 | while (attrs) { |
| 711 | iter = attrs; |
| 712 | attrs = attrs->next; |
| 713 | |
| 714 | if (iter->index) { |
| 715 | flag_leaflist = 1; |
| 716 | } |
| 717 | |
| 718 | LY_TREE_FOR(first, diter) { |
| 719 | if (iter->schema != diter->schema) { |
| 720 | continue; |
| 721 | } |
| 722 | |
| 723 | if (flag_leaflist && flag_leaflist != iter->index) { |
| 724 | flag_leaflist++; |
| 725 | continue; |
| 726 | } |
| 727 | |
| 728 | /* we have match */ |
| 729 | if (diter->attr) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 730 | LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, diter, |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 731 | "attribute (multiple attribute definitions belong to a single element)"); |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 732 | free(iter); |
| 733 | goto error; |
| 734 | } |
| 735 | |
| 736 | diter->attr = iter->attr; |
Radek Krejci | 532e5e9 | 2017-02-22 12:59:24 +0100 | [diff] [blame] | 737 | for (aiter = iter->attr; aiter; aiter = aiter->next) { |
| 738 | aiter->parent = diter; |
| 739 | } |
Michal Vasko | 7675c62 | 2017-03-02 10:50:07 +0100 | [diff] [blame] | 740 | |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 741 | break; |
| 742 | } |
| 743 | |
| 744 | if (!diter) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 745 | LOGVAL(LYE_XML_MISS, LY_VLOG_NONE, NULL, "element for the specified attribute", iter->attr->name); |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 746 | lyd_free_attr(iter->schema->module->ctx, NULL, iter->attr, 1); |
| 747 | free(iter); |
| 748 | goto error; |
| 749 | } |
| 750 | free(iter); |
Michal Vasko | 7675c62 | 2017-03-02 10:50:07 +0100 | [diff] [blame] | 751 | |
| 752 | /* check edit-config attribute correctness */ |
| 753 | if ((options & LYD_OPT_EDIT) && lyp_check_edit_attr(ctx, diter->attr, diter, NULL)) { |
| 754 | goto error; |
| 755 | } |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | return 0; |
| 759 | |
| 760 | error: |
| 761 | |
| 762 | while (attrs) { |
| 763 | iter = attrs; |
| 764 | attrs = attrs->next; |
| 765 | |
| 766 | lyd_free_attr(ctx, NULL, iter->attr, 1); |
| 767 | free(iter); |
| 768 | } |
| 769 | |
| 770 | return -1; |
| 771 | } |
| 772 | |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 773 | static unsigned int |
Michal Vasko | 36ef693 | 2015-12-01 14:30:17 +0100 | [diff] [blame] | 774 | json_parse_data(struct ly_ctx *ctx, const char *data, const struct lys_node *schema_parent, struct lyd_node **parent, |
Radek Krejci | bd93012 | 2016-08-10 13:28:26 +0200 | [diff] [blame] | 775 | struct lyd_node *first_sibling, struct lyd_node *prev, struct attr_cont **attrs, int options, |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 776 | struct unres_data *unres, struct lyd_node **act_notif) |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 777 | { |
| 778 | unsigned int len = 0; |
| 779 | unsigned int r; |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 780 | unsigned int flag_leaflist = 0; |
Radek Krejci | 65aca41 | 2018-01-24 11:23:06 +0100 | [diff] [blame] | 781 | int i; |
| 782 | uint8_t pos; |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 783 | char *name, *prefix = NULL, *str = NULL; |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 784 | const struct lys_module *module = NULL; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 785 | struct lys_node *schema = NULL; |
Radek Krejci | bd93012 | 2016-08-10 13:28:26 +0200 | [diff] [blame] | 786 | struct lyd_node *result = NULL, *new, *list, *diter = NULL; |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 787 | struct lyd_attr *attr; |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 788 | struct attr_cont *attrs_aux; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 789 | |
| 790 | /* each YANG data node representation starts with string (node identifier) */ |
| 791 | if (data[len] != '"') { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 792 | LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, (*parent), |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 793 | "JSON data (missing quotation-mark at the beginning of string)"); |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 794 | return 0; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 795 | } |
| 796 | len++; |
| 797 | |
Radek Krejci | 2323892 | 2015-10-27 17:13:34 +0100 | [diff] [blame] | 798 | str = lyjson_parse_text(&data[len], &r); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 799 | if (!r) { |
| 800 | goto error; |
| 801 | } else if (data[len + r] != '"') { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 802 | LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, (*parent), |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 803 | "JSON data (missing quotation-mark at the end of string)"); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 804 | goto error; |
| 805 | } |
Radek Krejci | 2323892 | 2015-10-27 17:13:34 +0100 | [diff] [blame] | 806 | if ((name = strchr(str, ':'))) { |
| 807 | *name = '\0'; |
| 808 | name++; |
| 809 | prefix = str; |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 810 | if (prefix[0] == '@') { |
| 811 | prefix++; |
| 812 | } |
Radek Krejci | 2323892 | 2015-10-27 17:13:34 +0100 | [diff] [blame] | 813 | } else { |
| 814 | name = str; |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 815 | if (name[0] == '@') { |
| 816 | name++; |
| 817 | } |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 818 | } |
| 819 | |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 820 | /* prepare data for parsing node content */ |
| 821 | len += r + 1; |
| 822 | len += skip_ws(&data[len]); |
| 823 | if (data[len] != ':') { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 824 | LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, (*parent), "JSON data (missing name-separator)"); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 825 | goto error; |
| 826 | } |
| 827 | len++; |
| 828 | len += skip_ws(&data[len]); |
| 829 | |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 830 | if (str[0] == '@' && !str[1]) { |
| 831 | /* process attribute of the parent object (container or list) */ |
Radek Krejci | 2a0efef | 2016-03-24 15:10:40 +0100 | [diff] [blame] | 832 | if (!(*parent)) { |
| 833 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "attribute with no corresponding element to belongs to"); |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 834 | goto error; |
| 835 | } |
| 836 | |
Radek Krejci | 532e5e9 | 2017-02-22 12:59:24 +0100 | [diff] [blame] | 837 | r = json_parse_attr((*parent)->schema->module, &attr, &data[len], options); |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 838 | if (!r) { |
Michal Vasko | 7675c62 | 2017-03-02 10:50:07 +0100 | [diff] [blame] | 839 | LOGPATH(LY_VLOG_LYD, *parent); |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 840 | goto error; |
| 841 | } |
| 842 | len += r; |
| 843 | |
| 844 | if ((*parent)->attr) { |
| 845 | lyd_free_attr(ctx, NULL, attr, 1); |
| 846 | } else { |
| 847 | (*parent)->attr = attr; |
Radek Krejci | 532e5e9 | 2017-02-22 12:59:24 +0100 | [diff] [blame] | 848 | for (; attr; attr = attr->next) { |
| 849 | attr->parent = *parent; |
| 850 | } |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 851 | } |
Michal Vasko | 7675c62 | 2017-03-02 10:50:07 +0100 | [diff] [blame] | 852 | |
| 853 | /* check edit-config attribute correctness */ |
| 854 | if ((options & LYD_OPT_EDIT) && lyp_check_edit_attr(ctx, (*parent)->attr, *parent, NULL)) { |
| 855 | goto error; |
| 856 | } |
| 857 | |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 858 | free(str); |
| 859 | return len; |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 860 | } |
| 861 | |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 862 | /* find schema node */ |
| 863 | if (!(*parent)) { |
| 864 | /* starting in root */ |
| 865 | /* get the proper schema */ |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 866 | module = ly_ctx_get_module(ctx, prefix, NULL, 1); |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 867 | if (ctx->data_clb) { |
| 868 | if (!module) { |
| 869 | module = ctx->data_clb(ctx, prefix, NULL, 0, ctx->data_clb_data); |
| 870 | } else if (!module->implemented) { |
| 871 | module = ctx->data_clb(ctx, module->name, module->ns, LY_MODCLB_NOT_IMPLEMENTED, ctx->data_clb_data); |
| 872 | } |
| 873 | } |
| 874 | if (module && module->implemented) { |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 875 | /* get the proper schema node */ |
Radek Krejci | 919a924 | 2016-07-27 08:17:13 +0200 | [diff] [blame] | 876 | while ((schema = (struct lys_node *)lys_getnext(schema, NULL, module, 0))) { |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 877 | if (!strcmp(schema->name, name)) { |
| 878 | break; |
| 879 | } |
| 880 | } |
| 881 | } |
| 882 | } else { |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 883 | if (prefix) { |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 884 | /* get the proper module to give the chance to load/implement it */ |
Radek Krejci | dfb00d6 | 2017-09-06 09:39:35 +0200 | [diff] [blame] | 885 | module = ly_ctx_get_module(ctx, prefix, NULL, 1); |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 886 | if (ctx->data_clb) { |
| 887 | if (!module) { |
Michal Vasko | ad43c18 | 2017-01-23 09:55:28 +0100 | [diff] [blame] | 888 | ctx->data_clb(ctx, prefix, NULL, 0, ctx->data_clb_data); |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 889 | } else if (!module->implemented) { |
Michal Vasko | ad43c18 | 2017-01-23 09:55:28 +0100 | [diff] [blame] | 890 | ctx->data_clb(ctx, module->name, module->ns, LY_MODCLB_NOT_IMPLEMENTED, ctx->data_clb_data); |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 891 | } |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 892 | } |
| 893 | } |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 894 | |
| 895 | /* go through RPC's input/output following the options' data type */ |
| 896 | if ((*parent)->schema->nodetype == LYS_RPC) { |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 897 | while ((schema = (struct lys_node *)lys_getnext(schema, (*parent)->schema, NULL, LYS_GETNEXT_WITHINOUT))) { |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 898 | if ((options & LYD_OPT_RPC) && (schema->nodetype == LYS_INPUT)) { |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 899 | break; |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 900 | } else if ((options & LYD_OPT_RPCREPLY) && (schema->nodetype == LYS_OUTPUT)) { |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 901 | break; |
| 902 | } |
| 903 | } |
Radek Krejci | 4a49bdf | 2016-01-12 17:17:01 +0100 | [diff] [blame] | 904 | schema_parent = schema; |
| 905 | schema = NULL; |
| 906 | } |
| 907 | |
Michal Vasko | 36ef693 | 2015-12-01 14:30:17 +0100 | [diff] [blame] | 908 | if (schema_parent) { |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 909 | while ((schema = (struct lys_node *)lys_getnext(schema, schema_parent, NULL, 0))) { |
| 910 | if (!strcmp(schema->name, name) |
| 911 | && ((prefix && !strcmp(lys_node_module(schema)->name, prefix)) |
| 912 | || (!prefix && (lys_node_module(schema) == lys_node_module(schema_parent))))) { |
Michal Vasko | 36ef693 | 2015-12-01 14:30:17 +0100 | [diff] [blame] | 913 | break; |
| 914 | } |
| 915 | } |
| 916 | } else { |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 917 | while ((schema = (struct lys_node *)lys_getnext(schema, (*parent)->schema, NULL, 0))) { |
| 918 | if (!strcmp(schema->name, name) |
| 919 | && ((prefix && !strcmp(lys_node_module(schema)->name, prefix)) |
| 920 | || (!prefix && (lys_node_module(schema) == lyd_node_module(*parent))))) { |
Michal Vasko | 36ef693 | 2015-12-01 14:30:17 +0100 | [diff] [blame] | 921 | break; |
| 922 | } |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 923 | } |
| 924 | } |
| 925 | } |
Michal Vasko | f53187d | 2017-01-13 13:23:14 +0100 | [diff] [blame] | 926 | |
| 927 | module = lys_node_module(schema); |
| 928 | if (!module || !module->implemented || module->disabled) { |
| 929 | LOGVAL(LYE_INELEM, (*parent ? LY_VLOG_LYD : LY_VLOG_NONE), (*parent), name); |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 930 | goto error; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 931 | } |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 932 | |
| 933 | if (str[0] == '@') { |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 934 | /* attribute for some sibling node */ |
| 935 | if (data[len] == '[') { |
| 936 | flag_leaflist = 1; |
| 937 | len++; |
| 938 | len += skip_ws(&data[len]); |
| 939 | } |
| 940 | |
| 941 | attr_repeat: |
Radek Krejci | 532e5e9 | 2017-02-22 12:59:24 +0100 | [diff] [blame] | 942 | r = json_parse_attr((struct lys_module *)module, &attr, &data[len], options); |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 943 | if (!r) { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 944 | LOGPATH(LY_VLOG_LYD, (*parent)); |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 945 | goto error; |
| 946 | } |
| 947 | len += r; |
| 948 | |
| 949 | if (attr) { |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 950 | attrs_aux = malloc(sizeof *attrs_aux); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 951 | LY_CHECK_ERR_GOTO(!attrs_aux, LOGMEM, error); |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 952 | attrs_aux->attr = attr; |
| 953 | attrs_aux->index = flag_leaflist; |
| 954 | attrs_aux->schema = schema; |
| 955 | attrs_aux->next = *attrs; |
| 956 | *attrs = attrs_aux; |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 957 | } |
| 958 | |
| 959 | if (flag_leaflist) { |
| 960 | if (data[len] == ',') { |
| 961 | len++; |
| 962 | len += skip_ws(&data[len]); |
| 963 | flag_leaflist++; |
| 964 | goto attr_repeat; |
| 965 | } else if (data[len] != ']') { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 966 | LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, (*parent), "JSON data (missing end-array)"); |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 967 | goto error; |
| 968 | } |
| 969 | len++; |
| 970 | len += skip_ws(&data[len]); |
| 971 | } |
| 972 | |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 973 | free(str); |
| 974 | return len; |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 975 | } |
| 976 | |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 977 | switch (schema->nodetype) { |
| 978 | case LYS_CONTAINER: |
| 979 | case LYS_LIST: |
| 980 | case LYS_NOTIF: |
| 981 | case LYS_RPC: |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 982 | case LYS_ACTION: |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 983 | result = calloc(1, sizeof *result); |
| 984 | break; |
| 985 | case LYS_LEAF: |
| 986 | case LYS_LEAFLIST: |
| 987 | result = calloc(1, sizeof(struct lyd_node_leaf_list)); |
| 988 | break; |
| 989 | case LYS_ANYXML: |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 990 | case LYS_ANYDATA: |
| 991 | result = calloc(1, sizeof(struct lyd_node_anydata)); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 992 | break; |
| 993 | default: |
| 994 | LOGINT; |
Radek Krejci | 595060f | 2015-10-30 16:29:58 +0100 | [diff] [blame] | 995 | goto error; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 996 | } |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 997 | LY_CHECK_ERR_GOTO(!result, LOGMEM, error); |
Michal Vasko | 253035f | 2015-12-17 16:58:13 +0100 | [diff] [blame] | 998 | |
Radek Krejci | 61767ca | 2016-09-19 14:21:55 +0200 | [diff] [blame] | 999 | result->prev = result; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1000 | result->schema = schema; |
Radek Krejci | 61767ca | 2016-09-19 14:21:55 +0200 | [diff] [blame] | 1001 | result->parent = *parent; |
| 1002 | diter = NULL; |
Radek Krejci | 65aca41 | 2018-01-24 11:23:06 +0100 | [diff] [blame] | 1003 | if (schema->nodetype == LYS_LEAF && lys_is_key((struct lys_node_leaf *)schema, &pos)) { |
Michal Vasko | 8038954 | 2018-02-08 14:35:32 +0100 | [diff] [blame^] | 1004 | /* it is key and we need to insert it into a correct place (we must have parent then, a key cannot be top-level) */ |
| 1005 | assert(*parent); |
Radek Krejci | 61767ca | 2016-09-19 14:21:55 +0200 | [diff] [blame] | 1006 | for (i = 0, diter = (*parent)->child; |
Radek Krejci | 65aca41 | 2018-01-24 11:23:06 +0100 | [diff] [blame] | 1007 | diter && i < pos && diter->schema->nodetype == LYS_LEAF && lys_is_key((struct lys_node_leaf *)diter->schema, NULL); |
Radek Krejci | 61767ca | 2016-09-19 14:21:55 +0200 | [diff] [blame] | 1008 | i++, diter = diter->next); |
| 1009 | if (diter) { |
| 1010 | /* out of order insertion - insert list's key to the correct position, before the diter */ |
| 1011 | if ((*parent)->child == diter) { |
| 1012 | (*parent)->child = result; |
| 1013 | /* update first_sibling */ |
| 1014 | first_sibling = result; |
| 1015 | } |
| 1016 | if (diter->prev->next) { |
| 1017 | diter->prev->next = result; |
| 1018 | } |
| 1019 | result->prev = diter->prev; |
| 1020 | diter->prev = result; |
| 1021 | result->next = diter; |
| 1022 | } |
| 1023 | } |
| 1024 | if (!diter) { |
| 1025 | /* simplified (faster) insert as the last node */ |
| 1026 | if (*parent && !(*parent)->child) { |
| 1027 | (*parent)->child = result; |
| 1028 | } |
| 1029 | if (prev) { |
| 1030 | result->prev = prev; |
| 1031 | prev->next = result; |
| 1032 | |
| 1033 | /* fix the "last" pointer */ |
| 1034 | first_sibling->prev = result; |
| 1035 | } else { |
| 1036 | result->prev = result; |
| 1037 | first_sibling = result; |
| 1038 | } |
| 1039 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 1040 | result->validity = ly_new_node_validity(result->schema); |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 1041 | if (resolve_applies_when(schema, 0, NULL)) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 1042 | result->when_status = LYD_WHEN; |
| 1043 | } |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1044 | |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1045 | /* type specific processing */ |
| 1046 | if (schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 1047 | /* type detection and assigning the value */ |
Radek Krejci | 45f41dd | 2017-02-09 14:11:09 +0100 | [diff] [blame] | 1048 | r = json_get_value((struct lyd_node_leaf_list *)result, &first_sibling, &data[len], options, unres); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1049 | if (!r) { |
| 1050 | goto error; |
| 1051 | } |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 1052 | while(result->next) { |
| 1053 | result = result->next; |
| 1054 | } |
| 1055 | |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1056 | len += r; |
| 1057 | len += skip_ws(&data[len]); |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 1058 | } else if (schema->nodetype & LYS_ANYDATA) { |
Radek Krejci | 4ae8294 | 2016-09-19 16:41:06 +0200 | [diff] [blame] | 1059 | r = json_get_anydata((struct lyd_node_anydata *)result, &data[len]); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1060 | if (!r) { |
| 1061 | goto error; |
| 1062 | } |
| 1063 | len += r; |
| 1064 | len += skip_ws(&data[len]); |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 1065 | } else if (schema->nodetype & (LYS_CONTAINER | LYS_RPC | LYS_ACTION | LYS_NOTIF)) { |
| 1066 | if (schema->nodetype & (LYS_RPC | LYS_ACTION)) { |
| 1067 | if (!(options & LYD_OPT_RPC) || *act_notif) { |
| 1068 | LOGVAL(LYE_INELEM, LY_VLOG_LYD, result, schema->name); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 1069 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Unexpected %s node \"%s\".", |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 1070 | (schema->nodetype == LYS_RPC ? "rpc" : "action"), schema->name); |
| 1071 | goto error; |
| 1072 | } |
| 1073 | *act_notif = result; |
| 1074 | } else if (schema->nodetype == LYS_NOTIF) { |
| 1075 | if (!(options & LYD_OPT_NOTIF) || *act_notif) { |
| 1076 | LOGVAL(LYE_INELEM, LY_VLOG_LYD, result, schema->name); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 1077 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Unexpected notification node \"%s\".", schema->name); |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 1078 | goto error; |
| 1079 | } |
| 1080 | *act_notif = result; |
| 1081 | } |
| 1082 | |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1083 | if (data[len] != '{') { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 1084 | LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, result, "JSON data (missing begin-object)"); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1085 | goto error; |
| 1086 | } |
| 1087 | len++; |
| 1088 | len += skip_ws(&data[len]); |
| 1089 | |
| 1090 | if (data[len] != '}') { |
| 1091 | /* non-empty container */ |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1092 | len--; |
| 1093 | diter = NULL; |
| 1094 | attrs_aux = NULL; |
| 1095 | do { |
| 1096 | len++; |
| 1097 | len += skip_ws(&data[len]); |
| 1098 | |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 1099 | r = json_parse_data(ctx, &data[len], NULL, &result, result->child, diter, &attrs_aux, options, unres, act_notif); |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1100 | if (!r) { |
| 1101 | goto error; |
| 1102 | } |
| 1103 | len += r; |
| 1104 | |
| 1105 | if (result->child) { |
| 1106 | diter = result->child->prev; |
| 1107 | } |
| 1108 | } while(data[len] == ','); |
| 1109 | |
| 1110 | /* store attributes */ |
Michal Vasko | 7675c62 | 2017-03-02 10:50:07 +0100 | [diff] [blame] | 1111 | if (store_attrs(ctx, attrs_aux, result->child, options)) { |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1112 | goto error; |
| 1113 | } |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1114 | } |
| 1115 | |
| 1116 | if (data[len] != '}') { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 1117 | LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, result, "JSON data (missing end-object)"); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1118 | goto error; |
| 1119 | } |
| 1120 | len++; |
| 1121 | len += skip_ws(&data[len]); |
| 1122 | |
Radek Krejci | fb7156e | 2016-10-27 13:39:56 +0200 | [diff] [blame] | 1123 | /* if we have empty non-presence container, mark it as default */ |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 1124 | if (schema->nodetype == LYS_CONTAINER && !result->child && |
Radek Krejci | d3e7372 | 2016-05-23 12:24:55 +0200 | [diff] [blame] | 1125 | !result->attr && !((struct lys_node_container *)schema)->presence) { |
Radek Krejci | fb7156e | 2016-10-27 13:39:56 +0200 | [diff] [blame] | 1126 | result->dflt = 1; |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 1127 | } |
| 1128 | |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1129 | } else if (schema->nodetype == LYS_LIST) { |
| 1130 | if (data[len] != '[') { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 1131 | LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, result, "JSON data (missing begin-array)"); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1132 | goto error; |
| 1133 | } |
| 1134 | |
| 1135 | list = result; |
| 1136 | do { |
| 1137 | len++; |
| 1138 | len += skip_ws(&data[len]); |
Radek Krejci | 2323892 | 2015-10-27 17:13:34 +0100 | [diff] [blame] | 1139 | |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1140 | if (data[len] != '{') { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 1141 | LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, result, |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 1142 | "JSON data (missing list instance's begin-object)"); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1143 | goto error; |
| 1144 | } |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1145 | diter = NULL; |
| 1146 | attrs_aux = NULL; |
| 1147 | do { |
| 1148 | len++; |
| 1149 | len += skip_ws(&data[len]); |
| 1150 | |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 1151 | r = json_parse_data(ctx, &data[len], NULL, &list, list->child, diter, &attrs_aux, options, unres, act_notif); |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1152 | if (!r) { |
| 1153 | goto error; |
| 1154 | } |
| 1155 | len += r; |
| 1156 | |
Radek Krejci | 5293469 | 2015-11-01 20:08:15 +0100 | [diff] [blame] | 1157 | if (list->child) { |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1158 | diter = list->child->prev; |
| 1159 | } |
| 1160 | } while(data[len] == ','); |
| 1161 | |
| 1162 | /* store attributes */ |
Michal Vasko | 7675c62 | 2017-03-02 10:50:07 +0100 | [diff] [blame] | 1163 | if (store_attrs(ctx, attrs_aux, list->child, options)) { |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1164 | goto error; |
| 1165 | } |
| 1166 | |
| 1167 | if (data[len] != '}') { |
| 1168 | /* expecting end-object */ |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 1169 | LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, result, |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 1170 | "JSON data (missing list instance's end-object)"); |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1171 | goto error; |
| 1172 | } |
| 1173 | len++; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1174 | len += skip_ws(&data[len]); |
| 1175 | |
| 1176 | if (data[len] == ',') { |
Radek Krejci | 93fab98 | 2016-02-03 15:58:19 +0100 | [diff] [blame] | 1177 | /* various validation checks */ |
Radek Krejci | 45f41dd | 2017-02-09 14:11:09 +0100 | [diff] [blame] | 1178 | if (lyv_data_context(list, options, unres)) { |
| 1179 | goto error; |
| 1180 | } |
| 1181 | |
Radek Krejci | cf74825 | 2017-09-04 11:11:14 +0200 | [diff] [blame] | 1182 | ly_err_clean(ly_parser_data.ctx, 1); |
Michal Vasko | ad2e44a | 2017-01-03 10:31:35 +0100 | [diff] [blame] | 1183 | if (lyv_data_content(list, options, unres) || |
| 1184 | lyv_multicases(list, NULL, prev ? &first_sibling : NULL, 0, NULL)) { |
Radek Krejci | 93fab98 | 2016-02-03 15:58:19 +0100 | [diff] [blame] | 1185 | if (ly_errno) { |
| 1186 | goto error; |
| 1187 | } |
| 1188 | } |
Radek Krejci | 93fab98 | 2016-02-03 15:58:19 +0100 | [diff] [blame] | 1189 | |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1190 | /* another instance of the list */ |
| 1191 | new = calloc(1, sizeof *new); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 1192 | LY_CHECK_ERR_GOTO(!new, LOGMEM, error); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1193 | new->parent = list->parent; |
| 1194 | new->prev = list; |
| 1195 | list->next = new; |
| 1196 | |
Radek Krejci | e179488 | 2017-02-08 13:23:38 +0100 | [diff] [blame] | 1197 | /* copy the validity and when flags */ |
| 1198 | new->validity = list->validity; |
| 1199 | new->when_status = list->when_status; |
| 1200 | |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1201 | /* fix the "last" pointer */ |
Radek Krejci | 2d5525d | 2016-04-04 15:43:30 +0200 | [diff] [blame] | 1202 | first_sibling->prev = new; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1203 | |
| 1204 | new->schema = list->schema; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1205 | list = new; |
| 1206 | } |
| 1207 | } while (data[len] == ','); |
Radek Krejci | b915fb9 | 2017-02-08 14:12:40 +0100 | [diff] [blame] | 1208 | result = list; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1209 | |
| 1210 | if (data[len] != ']') { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 1211 | LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, result, "JSON data (missing end-array)"); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1212 | goto error; |
| 1213 | } |
| 1214 | len++; |
| 1215 | len += skip_ws(&data[len]); |
| 1216 | } |
| 1217 | |
| 1218 | /* various validation checks */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 1219 | if (lyv_data_context(result, options, unres)) { |
Michal Vasko | 10e586f | 2016-05-18 13:25:30 +0200 | [diff] [blame] | 1220 | goto error; |
| 1221 | } |
| 1222 | |
Radek Krejci | cf74825 | 2017-09-04 11:11:14 +0200 | [diff] [blame] | 1223 | ly_err_clean(ly_parser_data.ctx, 1); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 1224 | if (lyv_data_content(result, options, unres) || |
| 1225 | lyv_multicases(result, NULL, prev ? &first_sibling : NULL, 0, NULL)) { |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1226 | if (ly_errno) { |
| 1227 | goto error; |
| 1228 | } |
| 1229 | } |
| 1230 | |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 1231 | /* validation successful */ |
Radek Krejci | 63b79c8 | 2016-08-10 10:09:33 +0200 | [diff] [blame] | 1232 | if (result->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 1233 | /* postpone checking of unique when there will be all list/leaflist instances */ |
| 1234 | result->validity |= LYD_VAL_UNIQUE; |
Radek Krejci | 63b79c8 | 2016-08-10 10:09:33 +0200 | [diff] [blame] | 1235 | } |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 1236 | |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 1237 | if (!(*parent)) { |
| 1238 | *parent = result; |
| 1239 | } |
| 1240 | |
Radek Krejci | de9d92c | 2015-10-30 15:59:59 +0100 | [diff] [blame] | 1241 | free(str); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1242 | return len; |
| 1243 | |
| 1244 | error: |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 1245 | len = 0; |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 1246 | /* cleanup */ |
| 1247 | for (i = unres->count - 1; i >= 0; i--) { |
| 1248 | /* remove unres items connected with the node being removed */ |
| 1249 | if (unres->node[i] == result) { |
| 1250 | unres_data_del(unres, i); |
| 1251 | } |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 1252 | } |
| 1253 | while (*attrs) { |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1254 | attrs_aux = *attrs; |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 1255 | *attrs = (*attrs)->next; |
| 1256 | |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1257 | lyd_free_attr(ctx, NULL, attrs_aux->attr, 1); |
| 1258 | free(attrs_aux); |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 1259 | } |
| 1260 | |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1261 | lyd_free(result); |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 1262 | free(str); |
| 1263 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 1264 | return len; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1265 | } |
| 1266 | |
| 1267 | struct lyd_node * |
Michal Vasko | 945b96b | 2016-10-18 11:49:12 +0200 | [diff] [blame] | 1268 | lyd_parse_json(struct ly_ctx *ctx, const char *data, int options, const struct lyd_node *rpc_act, |
| 1269 | const struct lyd_node *data_tree) |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1270 | { |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 1271 | struct lyd_node *result = NULL, *next, *iter, *reply_parent = NULL, *reply_top = NULL, *act_notif = NULL; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1272 | struct unres_data *unres = NULL; |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1273 | unsigned int len = 0, r; |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 1274 | int i, act_cont = 0; |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1275 | struct attr_cont *attrs = NULL; |
Radek Krejci | 63b79c8 | 2016-08-10 10:09:33 +0200 | [diff] [blame] | 1276 | struct ly_set *set; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1277 | |
Radek Krejci | cf74825 | 2017-09-04 11:11:14 +0200 | [diff] [blame] | 1278 | ly_err_clean(ly_parser_data.ctx, 1); |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 1279 | |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 1280 | if (!ctx || !data) { |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1281 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 1282 | return NULL; |
| 1283 | } |
| 1284 | |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1285 | /* skip leading whitespaces */ |
| 1286 | len += skip_ws(&data[len]); |
| 1287 | |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 1288 | /* no data (or whitespaces only) are fine */ |
| 1289 | if (!data[len]) { |
Michal Vasko | 85c63f4 | 2018-01-02 10:43:45 +0100 | [diff] [blame] | 1290 | empty: |
Radek Krejci | b1ed7e5 | 2017-08-09 12:42:35 +0200 | [diff] [blame] | 1291 | if (options & LYD_OPT_DATA_ADD_YANGLIB) { |
| 1292 | result = ly_ctx_info(ctx); |
| 1293 | } |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 1294 | lyd_validate(&result, options, ctx); |
| 1295 | return result; |
| 1296 | } |
| 1297 | |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1298 | /* expect top-level { */ |
| 1299 | if (data[len] != '{') { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 1300 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing top level begin-object)"); |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 1301 | return NULL; |
| 1302 | } |
| 1303 | |
Michal Vasko | 85c63f4 | 2018-01-02 10:43:45 +0100 | [diff] [blame] | 1304 | /* check for empty object */ |
| 1305 | r = len + 1; |
| 1306 | r += skip_ws(&data[r]); |
| 1307 | if (data[r] == '}') { |
| 1308 | goto empty; |
| 1309 | } |
| 1310 | |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 1311 | unres = calloc(1, sizeof *unres); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 1312 | LY_CHECK_ERR_RETURN(!unres, LOGMEM, NULL); |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1313 | |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 1314 | /* create RPC/action reply part that is not in the parsed data */ |
| 1315 | if (rpc_act) { |
| 1316 | assert(options & LYD_OPT_RPCREPLY); |
| 1317 | if (rpc_act->schema->nodetype == LYS_RPC) { |
| 1318 | /* RPC request */ |
| 1319 | reply_top = reply_parent = _lyd_new(NULL, rpc_act->schema, 0); |
| 1320 | } else { |
| 1321 | /* action request */ |
| 1322 | reply_top = lyd_dup(rpc_act, 1); |
| 1323 | LY_TREE_DFS_BEGIN(reply_top, iter, reply_parent) { |
| 1324 | if (reply_parent->schema->nodetype == LYS_ACTION) { |
| 1325 | break; |
| 1326 | } |
| 1327 | LY_TREE_DFS_END(reply_top, iter, reply_parent); |
| 1328 | } |
| 1329 | if (!reply_parent) { |
| 1330 | LOGERR(LY_EINVAL, "%s: invalid variable parameter (const struct lyd_node *rpc_act).", __func__); |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 1331 | goto error; |
| 1332 | } |
| 1333 | lyd_free_withsiblings(reply_parent->child); |
| 1334 | } |
| 1335 | } |
| 1336 | |
| 1337 | iter = NULL; |
| 1338 | next = reply_parent; |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1339 | do { |
| 1340 | len++; |
| 1341 | len += skip_ws(&data[len]); |
| 1342 | |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 1343 | if (!act_cont) { |
| 1344 | if (!strncmp(&data[len], "\"yang:action\"", 13)) { |
| 1345 | len += 13; |
| 1346 | len += skip_ws(&data[len]); |
| 1347 | if (data[len] != ':') { |
| 1348 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing top-level begin-object)"); |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 1349 | goto error; |
| 1350 | } |
| 1351 | ++len; |
| 1352 | len += skip_ws(&data[len]); |
| 1353 | if (data[len] != '{') { |
| 1354 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing top level yang:action object)"); |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 1355 | goto error; |
| 1356 | } |
| 1357 | ++len; |
| 1358 | len += skip_ws(&data[len]); |
| 1359 | |
| 1360 | act_cont = 1; |
| 1361 | } else { |
| 1362 | act_cont = -1; |
| 1363 | } |
| 1364 | } |
| 1365 | |
| 1366 | r = json_parse_data(ctx, &data[len], NULL, &next, result, iter, &attrs, options, unres, &act_notif); |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1367 | if (!r) { |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 1368 | goto error; |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1369 | } |
| 1370 | len += r; |
| 1371 | |
| 1372 | if (!result) { |
Radek Krejci | e4f9dcd | 2017-10-27 15:22:41 +0200 | [diff] [blame] | 1373 | if (reply_parent) { |
| 1374 | result = next->child; |
| 1375 | iter = next->child; |
| 1376 | } else { |
| 1377 | for (iter = next; iter && iter->prev->next; iter = iter->prev); |
| 1378 | result = iter; |
| 1379 | if (iter && (options & LYD_OPT_DATA_ADD_YANGLIB) && iter->schema->module == ctx->models.list[ctx->internal_module_count - 1]) { |
| 1380 | /* ietf-yang-library data present, so ignore the option to add them */ |
| 1381 | options &= ~LYD_OPT_DATA_ADD_YANGLIB; |
| 1382 | } |
| 1383 | iter = next; |
Radek Krejci | 06f8bb9 | 2017-08-02 15:36:25 +0200 | [diff] [blame] | 1384 | } |
Radek Krejci | e4f9dcd | 2017-10-27 15:22:41 +0200 | [diff] [blame] | 1385 | } else { |
| 1386 | iter = result->prev; |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1387 | } |
Radek Krejci | e4f9dcd | 2017-10-27 15:22:41 +0200 | [diff] [blame] | 1388 | if (!reply_parent) { |
| 1389 | next = NULL; |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1390 | } |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 1391 | } while (data[len] == ','); |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1392 | |
| 1393 | if (data[len] != '}') { |
| 1394 | /* expecting end-object */ |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 1395 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing top-level end-object)"); |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 1396 | goto error; |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1397 | } |
| 1398 | len++; |
| 1399 | len += skip_ws(&data[len]); |
| 1400 | |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 1401 | if (act_cont == 1) { |
| 1402 | if (data[len] != '}') { |
| 1403 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing top-level end-object)"); |
| 1404 | goto error; |
| 1405 | } |
| 1406 | len++; |
| 1407 | len += skip_ws(&data[len]); |
| 1408 | } |
| 1409 | |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1410 | /* store attributes */ |
Michal Vasko | 7675c62 | 2017-03-02 10:50:07 +0100 | [diff] [blame] | 1411 | if (store_attrs(ctx, attrs, result, options)) { |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 1412 | goto error; |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1413 | } |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1414 | |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 1415 | if (reply_top) { |
| 1416 | result = reply_top; |
| 1417 | } |
| 1418 | |
Michal Vasko | c1cf86f | 2015-11-04 09:54:51 +0100 | [diff] [blame] | 1419 | if (!result) { |
| 1420 | LOGERR(LY_EVALID, "Model for the data to be linked with not found."); |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 1421 | goto error; |
Michal Vasko | c1cf86f | 2015-11-04 09:54:51 +0100 | [diff] [blame] | 1422 | } |
| 1423 | |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 1424 | if ((options & LYD_OPT_RPCREPLY) && (rpc_act->schema->nodetype != LYS_RPC)) { |
| 1425 | /* action reply */ |
| 1426 | act_notif = reply_parent; |
| 1427 | } else if ((options & (LYD_OPT_RPC | LYD_OPT_NOTIF)) && !act_notif) { |
| 1428 | ly_vecode = LYVE_INELEM; |
| 1429 | LOGVAL(LYE_SPEC, LY_VLOG_LYD, result, "Missing %s node.", (options & LYD_OPT_RPC ? "action" : "notification")); |
| 1430 | goto error; |
| 1431 | } |
| 1432 | |
Radek Krejci | 06f8bb9 | 2017-08-02 15:36:25 +0200 | [diff] [blame] | 1433 | /* add missing ietf-yang-library if requested */ |
| 1434 | if (options & LYD_OPT_DATA_ADD_YANGLIB) { |
Radek Krejci | b1ed7e5 | 2017-08-09 12:42:35 +0200 | [diff] [blame] | 1435 | if (lyd_merge(result, ly_ctx_info(ctx), LYD_OPT_DESTRUCT | LYD_OPT_EXPLICIT)) { |
Radek Krejci | 06f8bb9 | 2017-08-02 15:36:25 +0200 | [diff] [blame] | 1436 | LOGERR(LY_EINT, "Adding ietf-yang-library data failed."); |
| 1437 | goto error; |
| 1438 | } |
| 1439 | } |
| 1440 | |
Radek Krejci | 63b79c8 | 2016-08-10 10:09:33 +0200 | [diff] [blame] | 1441 | /* check for uniquness of top-level lists/leaflists because |
| 1442 | * only the inner instances were tested in lyv_data_content() */ |
| 1443 | set = ly_set_new(); |
| 1444 | LY_TREE_FOR(result, iter) { |
| 1445 | if (!(iter->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(iter->validity & LYD_VAL_UNIQUE)) { |
| 1446 | continue; |
| 1447 | } |
| 1448 | |
| 1449 | /* check each list/leaflist only once */ |
| 1450 | i = set->number; |
| 1451 | if (ly_set_add(set, iter->schema, 0) != i) { |
| 1452 | /* already checked */ |
| 1453 | continue; |
| 1454 | } |
| 1455 | |
| 1456 | if (lyv_data_unique(iter, result)) { |
| 1457 | ly_set_free(set); |
| 1458 | goto error; |
| 1459 | } |
| 1460 | } |
| 1461 | ly_set_free(set); |
| 1462 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 1463 | /* add/validate default values, unres */ |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 1464 | if (lyd_defaults_add_unres(&result, options, ctx, data_tree, act_notif, unres)) { |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 1465 | goto error; |
Radek Krejci | 5c16245 | 2016-03-23 13:36:01 +0100 | [diff] [blame] | 1466 | } |
| 1467 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 1468 | /* check for missing top level mandatory nodes */ |
Michal Vasko | ad2e44a | 2017-01-03 10:31:35 +0100 | [diff] [blame] | 1469 | if (!(options & (LYD_OPT_TRUSTED | LYD_OPT_NOTIF_FILTER)) |
| 1470 | && lyd_check_mandatory_tree((act_notif ? act_notif : result), ctx, options)) { |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 1471 | goto error; |
Radek Krejci | 5c16245 | 2016-03-23 13:36:01 +0100 | [diff] [blame] | 1472 | } |
| 1473 | |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1474 | free(unres->node); |
| 1475 | free(unres->type); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1476 | free(unres); |
| 1477 | |
| 1478 | return result; |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 1479 | |
| 1480 | error: |
| 1481 | lyd_free_withsiblings(result); |
Radek Krejci | 1fe4179 | 2017-10-27 14:47:05 +0200 | [diff] [blame] | 1482 | if (reply_top && result != reply_top) { |
| 1483 | lyd_free_withsiblings(reply_top); |
| 1484 | } |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 1485 | free(unres->node); |
| 1486 | free(unres->type); |
| 1487 | free(unres); |
| 1488 | |
| 1489 | return NULL; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1490 | } |