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)) { |
Radek Krejci | 61767ca | 2016-09-19 14:21:55 +0200 | [diff] [blame] | 1004 | /* it is key and we need to insert it into a correct place */ |
| 1005 | for (i = 0, diter = (*parent)->child; |
Radek Krejci | 65aca41 | 2018-01-24 11:23:06 +0100 | [diff] [blame^] | 1006 | 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] | 1007 | i++, diter = diter->next); |
| 1008 | if (diter) { |
| 1009 | /* out of order insertion - insert list's key to the correct position, before the diter */ |
| 1010 | if ((*parent)->child == diter) { |
| 1011 | (*parent)->child = result; |
| 1012 | /* update first_sibling */ |
| 1013 | first_sibling = result; |
| 1014 | } |
| 1015 | if (diter->prev->next) { |
| 1016 | diter->prev->next = result; |
| 1017 | } |
| 1018 | result->prev = diter->prev; |
| 1019 | diter->prev = result; |
| 1020 | result->next = diter; |
| 1021 | } |
| 1022 | } |
| 1023 | if (!diter) { |
| 1024 | /* simplified (faster) insert as the last node */ |
| 1025 | if (*parent && !(*parent)->child) { |
| 1026 | (*parent)->child = result; |
| 1027 | } |
| 1028 | if (prev) { |
| 1029 | result->prev = prev; |
| 1030 | prev->next = result; |
| 1031 | |
| 1032 | /* fix the "last" pointer */ |
| 1033 | first_sibling->prev = result; |
| 1034 | } else { |
| 1035 | result->prev = result; |
| 1036 | first_sibling = result; |
| 1037 | } |
| 1038 | } |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 1039 | result->validity = ly_new_node_validity(result->schema); |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 1040 | if (resolve_applies_when(schema, 0, NULL)) { |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 1041 | result->when_status = LYD_WHEN; |
| 1042 | } |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1043 | |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1044 | /* type specific processing */ |
| 1045 | if (schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 1046 | /* type detection and assigning the value */ |
Radek Krejci | 45f41dd | 2017-02-09 14:11:09 +0100 | [diff] [blame] | 1047 | 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] | 1048 | if (!r) { |
| 1049 | goto error; |
| 1050 | } |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 1051 | while(result->next) { |
| 1052 | result = result->next; |
| 1053 | } |
| 1054 | |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1055 | len += r; |
| 1056 | len += skip_ws(&data[len]); |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 1057 | } else if (schema->nodetype & LYS_ANYDATA) { |
Radek Krejci | 4ae8294 | 2016-09-19 16:41:06 +0200 | [diff] [blame] | 1058 | r = json_get_anydata((struct lyd_node_anydata *)result, &data[len]); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1059 | if (!r) { |
| 1060 | goto error; |
| 1061 | } |
| 1062 | len += r; |
| 1063 | len += skip_ws(&data[len]); |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 1064 | } else if (schema->nodetype & (LYS_CONTAINER | LYS_RPC | LYS_ACTION | LYS_NOTIF)) { |
| 1065 | if (schema->nodetype & (LYS_RPC | LYS_ACTION)) { |
| 1066 | if (!(options & LYD_OPT_RPC) || *act_notif) { |
| 1067 | LOGVAL(LYE_INELEM, LY_VLOG_LYD, result, schema->name); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 1068 | LOGVAL(LYE_SPEC, LY_VLOG_PREV, NULL, "Unexpected %s node \"%s\".", |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 1069 | (schema->nodetype == LYS_RPC ? "rpc" : "action"), schema->name); |
| 1070 | goto error; |
| 1071 | } |
| 1072 | *act_notif = result; |
| 1073 | } else if (schema->nodetype == LYS_NOTIF) { |
| 1074 | if (!(options & LYD_OPT_NOTIF) || *act_notif) { |
| 1075 | LOGVAL(LYE_INELEM, LY_VLOG_LYD, result, schema->name); |
Michal Vasko | 51e5c58 | 2017-01-19 14:16:39 +0100 | [diff] [blame] | 1076 | 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] | 1077 | goto error; |
| 1078 | } |
| 1079 | *act_notif = result; |
| 1080 | } |
| 1081 | |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1082 | if (data[len] != '{') { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 1083 | 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] | 1084 | goto error; |
| 1085 | } |
| 1086 | len++; |
| 1087 | len += skip_ws(&data[len]); |
| 1088 | |
| 1089 | if (data[len] != '}') { |
| 1090 | /* non-empty container */ |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1091 | len--; |
| 1092 | diter = NULL; |
| 1093 | attrs_aux = NULL; |
| 1094 | do { |
| 1095 | len++; |
| 1096 | len += skip_ws(&data[len]); |
| 1097 | |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 1098 | 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] | 1099 | if (!r) { |
| 1100 | goto error; |
| 1101 | } |
| 1102 | len += r; |
| 1103 | |
| 1104 | if (result->child) { |
| 1105 | diter = result->child->prev; |
| 1106 | } |
| 1107 | } while(data[len] == ','); |
| 1108 | |
| 1109 | /* store attributes */ |
Michal Vasko | 7675c62 | 2017-03-02 10:50:07 +0100 | [diff] [blame] | 1110 | if (store_attrs(ctx, attrs_aux, result->child, options)) { |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1111 | goto error; |
| 1112 | } |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | if (data[len] != '}') { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 1116 | 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] | 1117 | goto error; |
| 1118 | } |
| 1119 | len++; |
| 1120 | len += skip_ws(&data[len]); |
| 1121 | |
Radek Krejci | fb7156e | 2016-10-27 13:39:56 +0200 | [diff] [blame] | 1122 | /* if we have empty non-presence container, mark it as default */ |
Radek Krejci | 2537fd3 | 2016-09-07 16:22:41 +0200 | [diff] [blame] | 1123 | if (schema->nodetype == LYS_CONTAINER && !result->child && |
Radek Krejci | d3e7372 | 2016-05-23 12:24:55 +0200 | [diff] [blame] | 1124 | !result->attr && !((struct lys_node_container *)schema)->presence) { |
Radek Krejci | fb7156e | 2016-10-27 13:39:56 +0200 | [diff] [blame] | 1125 | result->dflt = 1; |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 1126 | } |
| 1127 | |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1128 | } else if (schema->nodetype == LYS_LIST) { |
| 1129 | if (data[len] != '[') { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 1130 | 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] | 1131 | goto error; |
| 1132 | } |
| 1133 | |
| 1134 | list = result; |
| 1135 | do { |
| 1136 | len++; |
| 1137 | len += skip_ws(&data[len]); |
Radek Krejci | 2323892 | 2015-10-27 17:13:34 +0100 | [diff] [blame] | 1138 | |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1139 | if (data[len] != '{') { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 1140 | LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, result, |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 1141 | "JSON data (missing list instance's begin-object)"); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1142 | goto error; |
| 1143 | } |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1144 | diter = NULL; |
| 1145 | attrs_aux = NULL; |
| 1146 | do { |
| 1147 | len++; |
| 1148 | len += skip_ws(&data[len]); |
| 1149 | |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 1150 | 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] | 1151 | if (!r) { |
| 1152 | goto error; |
| 1153 | } |
| 1154 | len += r; |
| 1155 | |
Radek Krejci | 5293469 | 2015-11-01 20:08:15 +0100 | [diff] [blame] | 1156 | if (list->child) { |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1157 | diter = list->child->prev; |
| 1158 | } |
| 1159 | } while(data[len] == ','); |
| 1160 | |
| 1161 | /* store attributes */ |
Michal Vasko | 7675c62 | 2017-03-02 10:50:07 +0100 | [diff] [blame] | 1162 | if (store_attrs(ctx, attrs_aux, list->child, options)) { |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1163 | goto error; |
| 1164 | } |
| 1165 | |
| 1166 | if (data[len] != '}') { |
| 1167 | /* expecting end-object */ |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 1168 | LOGVAL(LYE_XML_INVAL, LY_VLOG_LYD, result, |
Radek Krejci | adb5761 | 2016-02-16 13:34:34 +0100 | [diff] [blame] | 1169 | "JSON data (missing list instance's end-object)"); |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1170 | goto error; |
| 1171 | } |
| 1172 | len++; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1173 | len += skip_ws(&data[len]); |
| 1174 | |
| 1175 | if (data[len] == ',') { |
Radek Krejci | 93fab98 | 2016-02-03 15:58:19 +0100 | [diff] [blame] | 1176 | /* various validation checks */ |
Radek Krejci | 45f41dd | 2017-02-09 14:11:09 +0100 | [diff] [blame] | 1177 | if (lyv_data_context(list, options, unres)) { |
| 1178 | goto error; |
| 1179 | } |
| 1180 | |
Radek Krejci | cf74825 | 2017-09-04 11:11:14 +0200 | [diff] [blame] | 1181 | ly_err_clean(ly_parser_data.ctx, 1); |
Michal Vasko | ad2e44a | 2017-01-03 10:31:35 +0100 | [diff] [blame] | 1182 | if (lyv_data_content(list, options, unres) || |
| 1183 | lyv_multicases(list, NULL, prev ? &first_sibling : NULL, 0, NULL)) { |
Radek Krejci | 93fab98 | 2016-02-03 15:58:19 +0100 | [diff] [blame] | 1184 | if (ly_errno) { |
| 1185 | goto error; |
| 1186 | } |
| 1187 | } |
Radek Krejci | 93fab98 | 2016-02-03 15:58:19 +0100 | [diff] [blame] | 1188 | |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1189 | /* another instance of the list */ |
| 1190 | new = calloc(1, sizeof *new); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 1191 | LY_CHECK_ERR_GOTO(!new, LOGMEM, error); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1192 | new->parent = list->parent; |
| 1193 | new->prev = list; |
| 1194 | list->next = new; |
| 1195 | |
Radek Krejci | e179488 | 2017-02-08 13:23:38 +0100 | [diff] [blame] | 1196 | /* copy the validity and when flags */ |
| 1197 | new->validity = list->validity; |
| 1198 | new->when_status = list->when_status; |
| 1199 | |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1200 | /* fix the "last" pointer */ |
Radek Krejci | 2d5525d | 2016-04-04 15:43:30 +0200 | [diff] [blame] | 1201 | first_sibling->prev = new; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1202 | |
| 1203 | new->schema = list->schema; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1204 | list = new; |
| 1205 | } |
| 1206 | } while (data[len] == ','); |
Radek Krejci | b915fb9 | 2017-02-08 14:12:40 +0100 | [diff] [blame] | 1207 | result = list; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1208 | |
| 1209 | if (data[len] != ']') { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 1210 | 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] | 1211 | goto error; |
| 1212 | } |
| 1213 | len++; |
| 1214 | len += skip_ws(&data[len]); |
| 1215 | } |
| 1216 | |
| 1217 | /* various validation checks */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 1218 | if (lyv_data_context(result, options, unres)) { |
Michal Vasko | 10e586f | 2016-05-18 13:25:30 +0200 | [diff] [blame] | 1219 | goto error; |
| 1220 | } |
| 1221 | |
Radek Krejci | cf74825 | 2017-09-04 11:11:14 +0200 | [diff] [blame] | 1222 | ly_err_clean(ly_parser_data.ctx, 1); |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 1223 | if (lyv_data_content(result, options, unres) || |
| 1224 | lyv_multicases(result, NULL, prev ? &first_sibling : NULL, 0, NULL)) { |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1225 | if (ly_errno) { |
| 1226 | goto error; |
| 1227 | } |
| 1228 | } |
| 1229 | |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 1230 | /* validation successful */ |
Radek Krejci | 63b79c8 | 2016-08-10 10:09:33 +0200 | [diff] [blame] | 1231 | if (result->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 1232 | /* postpone checking of unique when there will be all list/leaflist instances */ |
| 1233 | result->validity |= LYD_VAL_UNIQUE; |
Radek Krejci | 63b79c8 | 2016-08-10 10:09:33 +0200 | [diff] [blame] | 1234 | } |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 1235 | |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 1236 | if (!(*parent)) { |
| 1237 | *parent = result; |
| 1238 | } |
| 1239 | |
Radek Krejci | de9d92c | 2015-10-30 15:59:59 +0100 | [diff] [blame] | 1240 | free(str); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1241 | return len; |
| 1242 | |
| 1243 | error: |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 1244 | len = 0; |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 1245 | /* cleanup */ |
| 1246 | for (i = unres->count - 1; i >= 0; i--) { |
| 1247 | /* remove unres items connected with the node being removed */ |
| 1248 | if (unres->node[i] == result) { |
| 1249 | unres_data_del(unres, i); |
| 1250 | } |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 1251 | } |
| 1252 | while (*attrs) { |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1253 | attrs_aux = *attrs; |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 1254 | *attrs = (*attrs)->next; |
| 1255 | |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1256 | lyd_free_attr(ctx, NULL, attrs_aux->attr, 1); |
| 1257 | free(attrs_aux); |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 1258 | } |
| 1259 | |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1260 | lyd_free(result); |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 1261 | free(str); |
| 1262 | |
Radek Krejci | 0c0086a | 2016-03-24 15:20:28 +0100 | [diff] [blame] | 1263 | return len; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1264 | } |
| 1265 | |
| 1266 | struct lyd_node * |
Michal Vasko | 945b96b | 2016-10-18 11:49:12 +0200 | [diff] [blame] | 1267 | lyd_parse_json(struct ly_ctx *ctx, const char *data, int options, const struct lyd_node *rpc_act, |
| 1268 | const struct lyd_node *data_tree) |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1269 | { |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 1270 | 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] | 1271 | struct unres_data *unres = NULL; |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1272 | unsigned int len = 0, r; |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 1273 | int i, act_cont = 0; |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1274 | struct attr_cont *attrs = NULL; |
Radek Krejci | 63b79c8 | 2016-08-10 10:09:33 +0200 | [diff] [blame] | 1275 | struct ly_set *set; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1276 | |
Radek Krejci | cf74825 | 2017-09-04 11:11:14 +0200 | [diff] [blame] | 1277 | ly_err_clean(ly_parser_data.ctx, 1); |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 1278 | |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 1279 | if (!ctx || !data) { |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1280 | LOGERR(LY_EINVAL, "%s: Invalid parameter.", __func__); |
| 1281 | return NULL; |
| 1282 | } |
| 1283 | |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1284 | /* skip leading whitespaces */ |
| 1285 | len += skip_ws(&data[len]); |
| 1286 | |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 1287 | /* no data (or whitespaces only) are fine */ |
| 1288 | if (!data[len]) { |
Michal Vasko | 85c63f4 | 2018-01-02 10:43:45 +0100 | [diff] [blame] | 1289 | empty: |
Radek Krejci | b1ed7e5 | 2017-08-09 12:42:35 +0200 | [diff] [blame] | 1290 | if (options & LYD_OPT_DATA_ADD_YANGLIB) { |
| 1291 | result = ly_ctx_info(ctx); |
| 1292 | } |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 1293 | lyd_validate(&result, options, ctx); |
| 1294 | return result; |
| 1295 | } |
| 1296 | |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1297 | /* expect top-level { */ |
| 1298 | if (data[len] != '{') { |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 1299 | 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] | 1300 | return NULL; |
| 1301 | } |
| 1302 | |
Michal Vasko | 85c63f4 | 2018-01-02 10:43:45 +0100 | [diff] [blame] | 1303 | /* check for empty object */ |
| 1304 | r = len + 1; |
| 1305 | r += skip_ws(&data[r]); |
| 1306 | if (data[r] == '}') { |
| 1307 | goto empty; |
| 1308 | } |
| 1309 | |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 1310 | unres = calloc(1, sizeof *unres); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 1311 | LY_CHECK_ERR_RETURN(!unres, LOGMEM, NULL); |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1312 | |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 1313 | /* create RPC/action reply part that is not in the parsed data */ |
| 1314 | if (rpc_act) { |
| 1315 | assert(options & LYD_OPT_RPCREPLY); |
| 1316 | if (rpc_act->schema->nodetype == LYS_RPC) { |
| 1317 | /* RPC request */ |
| 1318 | reply_top = reply_parent = _lyd_new(NULL, rpc_act->schema, 0); |
| 1319 | } else { |
| 1320 | /* action request */ |
| 1321 | reply_top = lyd_dup(rpc_act, 1); |
| 1322 | LY_TREE_DFS_BEGIN(reply_top, iter, reply_parent) { |
| 1323 | if (reply_parent->schema->nodetype == LYS_ACTION) { |
| 1324 | break; |
| 1325 | } |
| 1326 | LY_TREE_DFS_END(reply_top, iter, reply_parent); |
| 1327 | } |
| 1328 | if (!reply_parent) { |
| 1329 | 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] | 1330 | goto error; |
| 1331 | } |
| 1332 | lyd_free_withsiblings(reply_parent->child); |
| 1333 | } |
| 1334 | } |
| 1335 | |
| 1336 | iter = NULL; |
| 1337 | next = reply_parent; |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1338 | do { |
| 1339 | len++; |
| 1340 | len += skip_ws(&data[len]); |
| 1341 | |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 1342 | if (!act_cont) { |
| 1343 | if (!strncmp(&data[len], "\"yang:action\"", 13)) { |
| 1344 | len += 13; |
| 1345 | len += skip_ws(&data[len]); |
| 1346 | if (data[len] != ':') { |
| 1347 | 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] | 1348 | goto error; |
| 1349 | } |
| 1350 | ++len; |
| 1351 | len += skip_ws(&data[len]); |
| 1352 | if (data[len] != '{') { |
| 1353 | 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] | 1354 | goto error; |
| 1355 | } |
| 1356 | ++len; |
| 1357 | len += skip_ws(&data[len]); |
| 1358 | |
| 1359 | act_cont = 1; |
| 1360 | } else { |
| 1361 | act_cont = -1; |
| 1362 | } |
| 1363 | } |
| 1364 | |
| 1365 | 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] | 1366 | if (!r) { |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 1367 | goto error; |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1368 | } |
| 1369 | len += r; |
| 1370 | |
| 1371 | if (!result) { |
Radek Krejci | e4f9dcd | 2017-10-27 15:22:41 +0200 | [diff] [blame] | 1372 | if (reply_parent) { |
| 1373 | result = next->child; |
| 1374 | iter = next->child; |
| 1375 | } else { |
| 1376 | for (iter = next; iter && iter->prev->next; iter = iter->prev); |
| 1377 | result = iter; |
| 1378 | if (iter && (options & LYD_OPT_DATA_ADD_YANGLIB) && iter->schema->module == ctx->models.list[ctx->internal_module_count - 1]) { |
| 1379 | /* ietf-yang-library data present, so ignore the option to add them */ |
| 1380 | options &= ~LYD_OPT_DATA_ADD_YANGLIB; |
| 1381 | } |
| 1382 | iter = next; |
Radek Krejci | 06f8bb9 | 2017-08-02 15:36:25 +0200 | [diff] [blame] | 1383 | } |
Radek Krejci | e4f9dcd | 2017-10-27 15:22:41 +0200 | [diff] [blame] | 1384 | } else { |
| 1385 | iter = result->prev; |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1386 | } |
Radek Krejci | e4f9dcd | 2017-10-27 15:22:41 +0200 | [diff] [blame] | 1387 | if (!reply_parent) { |
| 1388 | next = NULL; |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1389 | } |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 1390 | } while (data[len] == ','); |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1391 | |
| 1392 | if (data[len] != '}') { |
| 1393 | /* expecting end-object */ |
Radek Krejci | 48464ed | 2016-03-17 15:44:09 +0100 | [diff] [blame] | 1394 | 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] | 1395 | goto error; |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1396 | } |
| 1397 | len++; |
| 1398 | len += skip_ws(&data[len]); |
| 1399 | |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 1400 | if (act_cont == 1) { |
| 1401 | if (data[len] != '}') { |
| 1402 | LOGVAL(LYE_XML_INVAL, LY_VLOG_NONE, NULL, "JSON data (missing top-level end-object)"); |
| 1403 | goto error; |
| 1404 | } |
| 1405 | len++; |
| 1406 | len += skip_ws(&data[len]); |
| 1407 | } |
| 1408 | |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1409 | /* store attributes */ |
Michal Vasko | 7675c62 | 2017-03-02 10:50:07 +0100 | [diff] [blame] | 1410 | if (store_attrs(ctx, attrs, result, options)) { |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 1411 | goto error; |
Radek Krejci | c483127 | 2015-11-01 19:26:34 +0100 | [diff] [blame] | 1412 | } |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1413 | |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 1414 | if (reply_top) { |
| 1415 | result = reply_top; |
| 1416 | } |
| 1417 | |
Michal Vasko | c1cf86f | 2015-11-04 09:54:51 +0100 | [diff] [blame] | 1418 | if (!result) { |
| 1419 | 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] | 1420 | goto error; |
Michal Vasko | c1cf86f | 2015-11-04 09:54:51 +0100 | [diff] [blame] | 1421 | } |
| 1422 | |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 1423 | if ((options & LYD_OPT_RPCREPLY) && (rpc_act->schema->nodetype != LYS_RPC)) { |
| 1424 | /* action reply */ |
| 1425 | act_notif = reply_parent; |
| 1426 | } else if ((options & (LYD_OPT_RPC | LYD_OPT_NOTIF)) && !act_notif) { |
| 1427 | ly_vecode = LYVE_INELEM; |
| 1428 | LOGVAL(LYE_SPEC, LY_VLOG_LYD, result, "Missing %s node.", (options & LYD_OPT_RPC ? "action" : "notification")); |
| 1429 | goto error; |
| 1430 | } |
| 1431 | |
Radek Krejci | 06f8bb9 | 2017-08-02 15:36:25 +0200 | [diff] [blame] | 1432 | /* add missing ietf-yang-library if requested */ |
| 1433 | if (options & LYD_OPT_DATA_ADD_YANGLIB) { |
Radek Krejci | b1ed7e5 | 2017-08-09 12:42:35 +0200 | [diff] [blame] | 1434 | 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] | 1435 | LOGERR(LY_EINT, "Adding ietf-yang-library data failed."); |
| 1436 | goto error; |
| 1437 | } |
| 1438 | } |
| 1439 | |
Radek Krejci | 63b79c8 | 2016-08-10 10:09:33 +0200 | [diff] [blame] | 1440 | /* check for uniquness of top-level lists/leaflists because |
| 1441 | * only the inner instances were tested in lyv_data_content() */ |
| 1442 | set = ly_set_new(); |
| 1443 | LY_TREE_FOR(result, iter) { |
| 1444 | if (!(iter->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) || !(iter->validity & LYD_VAL_UNIQUE)) { |
| 1445 | continue; |
| 1446 | } |
| 1447 | |
| 1448 | /* check each list/leaflist only once */ |
| 1449 | i = set->number; |
| 1450 | if (ly_set_add(set, iter->schema, 0) != i) { |
| 1451 | /* already checked */ |
| 1452 | continue; |
| 1453 | } |
| 1454 | |
| 1455 | if (lyv_data_unique(iter, result)) { |
| 1456 | ly_set_free(set); |
| 1457 | goto error; |
| 1458 | } |
| 1459 | } |
| 1460 | ly_set_free(set); |
| 1461 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 1462 | /* add/validate default values, unres */ |
Michal Vasko | afa7a64 | 2016-10-18 15:11:38 +0200 | [diff] [blame] | 1463 | 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] | 1464 | goto error; |
Radek Krejci | 5c16245 | 2016-03-23 13:36:01 +0100 | [diff] [blame] | 1465 | } |
| 1466 | |
Radek Krejci | 4616582 | 2016-08-26 14:06:27 +0200 | [diff] [blame] | 1467 | /* check for missing top level mandatory nodes */ |
Michal Vasko | ad2e44a | 2017-01-03 10:31:35 +0100 | [diff] [blame] | 1468 | if (!(options & (LYD_OPT_TRUSTED | LYD_OPT_NOTIF_FILTER)) |
| 1469 | && lyd_check_mandatory_tree((act_notif ? act_notif : result), ctx, options)) { |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 1470 | goto error; |
Radek Krejci | 5c16245 | 2016-03-23 13:36:01 +0100 | [diff] [blame] | 1471 | } |
| 1472 | |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1473 | free(unres->node); |
| 1474 | free(unres->type); |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1475 | free(unres); |
| 1476 | |
| 1477 | return result; |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 1478 | |
| 1479 | error: |
| 1480 | lyd_free_withsiblings(result); |
Radek Krejci | 1fe4179 | 2017-10-27 14:47:05 +0200 | [diff] [blame] | 1481 | if (reply_top && result != reply_top) { |
| 1482 | lyd_free_withsiblings(reply_top); |
| 1483 | } |
Michal Vasko | 24d982f | 2016-04-18 15:13:58 +0200 | [diff] [blame] | 1484 | free(unres->node); |
| 1485 | free(unres->type); |
| 1486 | free(unres); |
| 1487 | |
| 1488 | return NULL; |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 1489 | } |