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