Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file xml.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief XML data parser for libyang |
| 5 | * |
| 6 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in |
| 15 | * the documentation and/or other materials provided with the |
| 16 | * distribution. |
| 17 | * 3. Neither the name of the Company nor the names of its contributors |
| 18 | * may be used to endorse or promote products derived from this |
| 19 | * software without specific prior written permission. |
| 20 | */ |
| 21 | |
| 22 | #include <assert.h> |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 23 | #include <ctype.h> |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 24 | #include <errno.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
Michal Vasko | be190a6 | 2015-07-13 16:14:20 +0200 | [diff] [blame] | 27 | #include <sys/types.h> |
| 28 | #include <regex.h> |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 29 | |
Radek Krejci | 998a0b8 | 2015-08-17 13:14:36 +0200 | [diff] [blame] | 30 | #include "libyang.h" |
| 31 | #include "common.h" |
| 32 | #include "context.h" |
| 33 | #include "resolve.h" |
| 34 | #include "xml.h" |
| 35 | #include "tree_internal.h" |
| 36 | #include "validation.h" |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 37 | |
| 38 | #define LY_NSNC "urn:ietf:params:xml:ns:netconf:base:1.0" |
| 39 | |
Michal Vasko | 9286afd | 2015-07-14 15:27:59 +0200 | [diff] [blame] | 40 | /* kind == 0 - unsigned (unum used), 1 - signed (snum used), 2 - floating point (fnum used) */ |
| 41 | static int |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 42 | validate_length_range(uint8_t kind, uint64_t unum, int64_t snum, long double fnum, struct lys_type *type, |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 43 | struct lyxml_elem *xml, const char *str_val, int log) |
Michal Vasko | 9286afd | 2015-07-14 15:27:59 +0200 | [diff] [blame] | 44 | { |
Michal Vasko | 020404f | 2015-07-15 15:45:42 +0200 | [diff] [blame] | 45 | struct len_ran_intv *intv = NULL, *tmp_intv; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 46 | int ret = EXIT_FAILURE; |
Michal Vasko | 9286afd | 2015-07-14 15:27:59 +0200 | [diff] [blame] | 47 | |
Michal Vasko | 9c1bc64 | 2015-08-05 16:25:53 +0200 | [diff] [blame] | 48 | if (resolve_len_ran_interval(NULL, type, 0, &intv)) { |
| 49 | return EXIT_FAILURE; |
| 50 | } |
Michal Vasko | 020404f | 2015-07-15 15:45:42 +0200 | [diff] [blame] | 51 | if (!intv) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 52 | return EXIT_SUCCESS; |
Michal Vasko | 020404f | 2015-07-15 15:45:42 +0200 | [diff] [blame] | 53 | } |
| 54 | |
Michal Vasko | 9286afd | 2015-07-14 15:27:59 +0200 | [diff] [blame] | 55 | for (tmp_intv = intv; tmp_intv; tmp_intv = tmp_intv->next) { |
| 56 | if (((kind == 0) && (unum < tmp_intv->value.uval.min)) |
| 57 | || ((kind == 1) && (snum < tmp_intv->value.sval.min)) |
| 58 | || ((kind == 2) && (fnum < tmp_intv->value.fval.min))) { |
| 59 | break; |
| 60 | } |
| 61 | |
| 62 | if (((kind == 0) && (unum >= tmp_intv->value.uval.min) && (unum <= tmp_intv->value.uval.max)) |
| 63 | || ((kind == 1) && (snum >= tmp_intv->value.sval.min) && (snum <= tmp_intv->value.sval.max)) |
| 64 | || ((kind == 2) && (fnum >= tmp_intv->value.fval.min) && (fnum <= tmp_intv->value.fval.max))) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 65 | ret = EXIT_SUCCESS; |
Michal Vasko | 9286afd | 2015-07-14 15:27:59 +0200 | [diff] [blame] | 66 | break; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | while (intv) { |
| 71 | tmp_intv = intv->next; |
| 72 | free(intv); |
| 73 | intv = tmp_intv; |
| 74 | } |
| 75 | |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 76 | if (ret && log) { |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 77 | /* so that it's used even in RELEASE target */ |
| 78 | (void)xml; |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 79 | LOGVAL(LYE_OORVAL, LOGLINE(xml), str_val); |
Michal Vasko | 0e60c83 | 2015-07-15 15:48:35 +0200 | [diff] [blame] | 80 | } |
Michal Vasko | 9286afd | 2015-07-14 15:27:59 +0200 | [diff] [blame] | 81 | return ret; |
| 82 | } |
| 83 | |
Michal Vasko | fbf7e48 | 2015-07-14 14:49:03 +0200 | [diff] [blame] | 84 | static int |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 85 | validate_pattern(const char *str, struct lys_type *type, struct lyxml_elem *xml, const char *str_val, int log) |
Michal Vasko | fbf7e48 | 2015-07-14 14:49:03 +0200 | [diff] [blame] | 86 | { |
| 87 | int i; |
| 88 | regex_t preq; |
| 89 | char *posix_regex; |
| 90 | |
| 91 | assert(type->base == LY_TYPE_STRING); |
| 92 | |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 93 | if (type->der && validate_pattern(str, &type->der->type, xml, str_val, log)) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 94 | return EXIT_FAILURE; |
Michal Vasko | fbf7e48 | 2015-07-14 14:49:03 +0200 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | for (i = 0; i < type->info.str.pat_count; ++i) { |
| 98 | /* |
| 99 | * adjust the expression to a POSIX.2 equivalent |
| 100 | * |
| 101 | * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs |
| 102 | */ |
| 103 | posix_regex = malloc((strlen(type->info.str.patterns[i].expr)+3) * sizeof(char)); |
| 104 | posix_regex[0] = '\0'; |
| 105 | |
| 106 | if (strncmp(type->info.str.patterns[i].expr, ".*", 2)) { |
| 107 | strcat(posix_regex, "^"); |
| 108 | } |
| 109 | strcat(posix_regex, type->info.str.patterns[i].expr); |
| 110 | if (strncmp(type->info.str.patterns[i].expr |
| 111 | + strlen(type->info.str.patterns[i].expr) - 2, ".*", 2)) { |
| 112 | strcat(posix_regex, "$"); |
| 113 | } |
| 114 | |
| 115 | /* must return 0, already checked during parsing */ |
Michal Vasko | 88876dc | 2015-08-05 09:46:40 +0200 | [diff] [blame] | 116 | if (regcomp(&preq, posix_regex, REG_EXTENDED | REG_NOSUB)) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 117 | LOGINT; |
Radek Krejci | 61ff1a0 | 2015-08-13 09:11:34 +0200 | [diff] [blame] | 118 | free(posix_regex); |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 119 | return EXIT_FAILURE; |
Michal Vasko | 88876dc | 2015-08-05 09:46:40 +0200 | [diff] [blame] | 120 | } |
Michal Vasko | fbf7e48 | 2015-07-14 14:49:03 +0200 | [diff] [blame] | 121 | free(posix_regex); |
| 122 | |
| 123 | if (regexec(&preq, str, 0, 0, 0)) { |
| 124 | regfree(&preq); |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 125 | if (log) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 126 | LOGVAL(LYE_INVAL, LOGLINE(xml), str_val, xml->name); |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 127 | } |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 128 | return EXIT_FAILURE; |
Michal Vasko | fbf7e48 | 2015-07-14 14:49:03 +0200 | [diff] [blame] | 129 | } |
| 130 | regfree(&preq); |
| 131 | } |
| 132 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 133 | return EXIT_SUCCESS; |
Michal Vasko | fbf7e48 | 2015-07-14 14:49:03 +0200 | [diff] [blame] | 134 | } |
| 135 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 136 | static struct lys_node * |
| 137 | xml_data_search_schemanode(struct lyxml_elem *xml, struct lys_node *start) |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 138 | { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 139 | struct lys_node *result, *aux; |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 140 | |
| 141 | LY_TREE_FOR(start, result) { |
| 142 | /* skip groupings */ |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 143 | if (result->nodetype == LYS_GROUPING) { |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 144 | continue; |
| 145 | } |
| 146 | |
| 147 | /* go into cases, choices, uses */ |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 148 | if (result->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES)) { |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 149 | aux = xml_data_search_schemanode(xml, result->child); |
| 150 | if (aux) { |
| 151 | /* we have matching result */ |
| 152 | return aux; |
| 153 | } |
| 154 | /* else, continue with next schema node */ |
| 155 | continue; |
| 156 | } |
| 157 | |
| 158 | /* match data nodes */ |
| 159 | if (result->name == xml->name) { |
| 160 | /* names matches, what about namespaces? */ |
| 161 | if (result->module->ns == xml->ns->value) { |
| 162 | /* we have matching result */ |
| 163 | return result; |
| 164 | } |
| 165 | /* else, continue with next schema node */ |
| 166 | continue; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | /* no match */ |
| 171 | return NULL; |
| 172 | } |
| 173 | |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 174 | static int |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 175 | parse_int(const char *str_val, struct lyxml_elem *xml, int64_t min, int64_t max, int base, int64_t *ret, int log) |
Michal Vasko | bdee69c | 2015-07-15 15:49:39 +0200 | [diff] [blame] | 176 | { |
| 177 | char *strptr; |
| 178 | |
| 179 | /* convert to 64-bit integer, all the redundant characters are handled */ |
| 180 | errno = 0; |
| 181 | strptr = NULL; |
| 182 | *ret = strtoll(str_val, &strptr, base); |
| 183 | if (errno || (*ret < min) || (*ret > max)) { |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 184 | if (log) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 185 | LOGVAL(LYE_OORVAL, LOGLINE(xml), str_val, xml->name); |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 186 | } |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 187 | return EXIT_FAILURE; |
Michal Vasko | bdee69c | 2015-07-15 15:49:39 +0200 | [diff] [blame] | 188 | } else if (strptr && *strptr) { |
| 189 | while (isspace(*strptr)) { |
| 190 | ++strptr; |
| 191 | } |
| 192 | if (*strptr) { |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 193 | if (log) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 194 | LOGVAL(LYE_INVAL, LOGLINE(xml), str_val, xml->name); |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 195 | } |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 196 | return EXIT_FAILURE; |
Michal Vasko | bdee69c | 2015-07-15 15:49:39 +0200 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 200 | return EXIT_SUCCESS; |
Michal Vasko | bdee69c | 2015-07-15 15:49:39 +0200 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | static int |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 204 | parse_uint(const char *str_val, struct lyxml_elem *xml, uint64_t max, int base, uint64_t *ret, int log) |
Michal Vasko | bdee69c | 2015-07-15 15:49:39 +0200 | [diff] [blame] | 205 | { |
| 206 | char *strptr; |
| 207 | |
| 208 | errno = 0; |
| 209 | strptr = NULL; |
| 210 | *ret = strtoull(str_val, &strptr, base); |
| 211 | if (errno || (*ret > max)) { |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 212 | if (log) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 213 | LOGVAL(LYE_OORVAL, LOGLINE(xml), str_val, xml->name); |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 214 | } |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 215 | return EXIT_FAILURE; |
Michal Vasko | bdee69c | 2015-07-15 15:49:39 +0200 | [diff] [blame] | 216 | } else if (strptr && *strptr) { |
| 217 | while (isspace(*strptr)) { |
| 218 | ++strptr; |
| 219 | } |
| 220 | if (*strptr) { |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 221 | if (log) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 222 | LOGVAL(LYE_INVAL, LOGLINE(xml), str_val, xml->name); |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 223 | } |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 224 | return EXIT_FAILURE; |
Michal Vasko | bdee69c | 2015-07-15 15:49:39 +0200 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 228 | return EXIT_SUCCESS; |
Michal Vasko | bdee69c | 2015-07-15 15:49:39 +0200 | [diff] [blame] | 229 | } |
| 230 | |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 231 | static struct lys_type * |
| 232 | get_next_union_type(struct lys_type *type, struct lys_type *prev_type, int *found) |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 233 | { |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 234 | int i; |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 235 | struct lys_type *ret = NULL; |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 236 | |
Michal Vasko | 6da1a37 | 2015-07-27 11:19:10 +0200 | [diff] [blame] | 237 | for (i = 0; i < type->info.uni.count; ++i) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 238 | if (type->info.uni.types[i].base == LY_TYPE_UNION) { |
| 239 | ret = get_next_union_type(&type->info.uni.types[i], prev_type, found); |
Michal Vasko | 6da1a37 | 2015-07-27 11:19:10 +0200 | [diff] [blame] | 240 | if (ret) { |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 241 | break;; |
| 242 | } |
| 243 | continue; |
| 244 | } |
| 245 | |
| 246 | if (!prev_type || *found) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 247 | ret = &type->info.uni.types[i]; |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 248 | break; |
| 249 | } |
| 250 | |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 251 | if (&type->info.uni.types[i] == prev_type) { |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 252 | *found = 1; |
| 253 | } |
| 254 | } |
| 255 | |
Michal Vasko | 6da1a37 | 2015-07-27 11:19:10 +0200 | [diff] [blame] | 256 | if (!ret && type->der) { |
| 257 | ret = get_next_union_type(&type->der->type, prev_type, found); |
| 258 | } |
| 259 | |
| 260 | return ret; |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 261 | } |
| 262 | |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 263 | static const char * |
| 264 | instid_xml2json(struct ly_ctx *ctx, struct lyxml_elem *xml) |
| 265 | { |
| 266 | const char *in = xml->content; |
| 267 | char *out, *aux, *prefix; |
| 268 | size_t out_size, len, size, i = 0, o = 0; |
| 269 | int start = 1, interior = 1; |
| 270 | struct lys_module *mod, *mod_prev = NULL; |
| 271 | struct lyxml_ns *ns; |
| 272 | |
| 273 | out_size = strlen(in); |
| 274 | out = malloc((out_size + 1) * sizeof *out); |
| 275 | |
| 276 | while (in[i]) { |
| 277 | |
| 278 | /* skip whitespaces */ |
| 279 | while (isspace(in[i])) { |
| 280 | i++; |
| 281 | } |
| 282 | |
| 283 | if (start) { |
| 284 | /* leading '/' character */ |
| 285 | if (start == 1 && in[i] != '/') { |
| 286 | LOGVAL(LYE_INCHAR, LOGLINE(xml), in[i], &in[i]); |
Radek Krejci | 61ff1a0 | 2015-08-13 09:11:34 +0200 | [diff] [blame] | 287 | free(out); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 288 | return NULL; |
| 289 | } |
| 290 | |
| 291 | /* check the output buffer size */ |
| 292 | if (out_size == o) { |
| 293 | out_size += 16; /* just add some size */ |
| 294 | aux = realloc(out, out_size + 1); |
| 295 | if (!aux) { |
| 296 | free(out); |
| 297 | LOGMEM; |
| 298 | return NULL; |
| 299 | } |
| 300 | out = aux; |
| 301 | } |
| 302 | |
| 303 | out[o++] = in[i++]; |
| 304 | start = 0; |
| 305 | continue; |
| 306 | } else { |
| 307 | /* translate the node identifier */ |
| 308 | /* prefix */ |
| 309 | aux = strchr(&in[i], ':'); |
| 310 | if (aux) { |
| 311 | /* interior segment */ |
| 312 | len = aux - &in[i]; |
| 313 | prefix = strndup(&in[i], len); |
| 314 | i += len + 1; /* move after ':' */ |
| 315 | } else { |
| 316 | /* missing prefix -> invalid instance-identifier */ |
| 317 | LOGVAL(LYE_INVAL, LOGLINE(xml), xml->content, xml->name); |
| 318 | free(out); |
| 319 | return NULL; |
| 320 | } |
| 321 | ns = lyxml_get_ns(xml, prefix); |
| 322 | free(prefix); |
| 323 | mod = ly_ctx_get_module_by_ns(ctx, ns->value, NULL); |
| 324 | |
| 325 | /* node name */ |
| 326 | aux = strpbrk(&in[i], "/[="); |
| 327 | if (aux) { |
| 328 | /* interior segment */ |
| 329 | len = aux - &in[i]; |
| 330 | } else { |
| 331 | /* end segment */ |
| 332 | interior = 0; |
| 333 | len = strlen(&in[i]); |
| 334 | } |
| 335 | |
| 336 | /* check the output buffer size */ |
| 337 | if (!mod_prev || (mod != mod_prev)) { |
| 338 | /* prefix + ':' + name to print + '/' */ |
| 339 | size = o + len + 1 + strlen(mod->name) + interior; |
| 340 | } else { |
| 341 | /* name to print + '/' */ |
| 342 | size = o + len + interior; |
| 343 | } |
| 344 | if (out_size <= size) { |
| 345 | /* extend to fit the needed size */ |
| 346 | out_size = size; |
| 347 | aux = realloc(out, out_size + 1); |
| 348 | if (!aux) { |
| 349 | free(out); |
| 350 | LOGMEM; |
| 351 | return NULL; |
| 352 | } |
| 353 | out = aux; |
| 354 | } |
| 355 | |
| 356 | if (!mod_prev || (mod != mod_prev)) { |
| 357 | mod_prev = mod; |
| 358 | size = strlen(mod->name); |
| 359 | memcpy(&out[o], mod->name, size); |
| 360 | o += size; |
| 361 | out[o++] = ':'; |
| 362 | } |
| 363 | memcpy(&out[o], &in[i], len); |
| 364 | o += len; |
| 365 | i += len; |
| 366 | |
| 367 | if (in[i] == '=') { |
| 368 | /* we are in the predicate on the value, so just copy data */ |
| 369 | aux = strchr(&in[i], ']'); |
| 370 | if (aux) { |
| 371 | len = aux - &in[i] + 1; /* include ] */ |
| 372 | |
| 373 | /* check the output buffer size */ |
| 374 | size = o + len + 1; |
| 375 | if (out_size <= size) { |
| 376 | out_size = size; /* just add some size */ |
| 377 | aux = realloc(out, out_size + 1); |
| 378 | if (!aux) { |
| 379 | free(out); |
| 380 | LOGMEM; |
| 381 | return NULL; |
| 382 | } |
| 383 | out = aux; |
| 384 | } |
| 385 | |
| 386 | memcpy(&out[o], &in[i], len); |
| 387 | o += len; |
| 388 | i += len; |
| 389 | } else { |
| 390 | /* missing closing ] of predicate -> invalid instance-identifier */ |
| 391 | LOGVAL(LYE_INVAL, LOGLINE(xml), xml->content, xml->name); |
| 392 | free(out); |
| 393 | return NULL; |
| 394 | } |
| 395 | } |
| 396 | start = 2; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | /* terminating NULL byte */ |
| 401 | /* check the output buffer size */ |
| 402 | if (out_size < o) { |
| 403 | out_size += 1; /* just add some size */ |
| 404 | aux = realloc(out, out_size + 1); |
| 405 | if (!aux) { |
| 406 | free(out); |
| 407 | LOGMEM; |
| 408 | return NULL; |
| 409 | } |
| 410 | out = aux; |
| 411 | } |
| 412 | out[o] = '\0'; |
| 413 | |
| 414 | return lydict_insert_zc(ctx, out); |
| 415 | } |
| 416 | |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 417 | static int |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 418 | _xml_get_value(struct lyd_node *node, struct lys_type *node_type, struct lyxml_elem *xml, |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 419 | int options, struct unres_data **unres, int log) |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 420 | { |
| 421 | #define DECSIZE 21 |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 422 | struct lyd_node_leaf *leaf = (struct lyd_node_leaf *)node; |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 423 | struct lys_type *type; |
Radek Krejci | ac8aac6 | 2015-07-10 15:36:35 +0200 | [diff] [blame] | 424 | struct lyxml_ns *ns; |
Radek Krejci | 7511f40 | 2015-07-10 09:56:30 +0200 | [diff] [blame] | 425 | char dec[DECSIZE]; |
Michal Vasko | fbf7e48 | 2015-07-14 14:49:03 +0200 | [diff] [blame] | 426 | char *strptr; |
Radek Krejci | ac8aac6 | 2015-07-10 15:36:35 +0200 | [diff] [blame] | 427 | const char *name; |
Michal Vasko | bdee69c | 2015-07-15 15:49:39 +0200 | [diff] [blame] | 428 | int64_t num; |
| 429 | uint64_t unum; |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 430 | int len; |
Radek Krejci | 7511f40 | 2015-07-10 09:56:30 +0200 | [diff] [blame] | 431 | int c, i, j, d; |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 432 | int found; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 433 | struct unres_data *new_unres; |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 434 | |
| 435 | leaf->value_str = xml->content; |
| 436 | xml->content = NULL; |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 437 | |
Radek Krejci | e3c3314 | 2015-08-10 15:04:36 +0200 | [diff] [blame] | 438 | /* will be change in case of union */ |
| 439 | leaf->value_type = node_type->base; |
| 440 | |
Radek Krejci | e2cf7c1 | 2015-08-12 10:24:05 +0200 | [diff] [blame] | 441 | if ((options & LYD_OPT_FILTER) && !leaf->value_str) { |
| 442 | /* no value in filter (selection) node -> nothing more is needed */ |
| 443 | return EXIT_SUCCESS; |
| 444 | } |
| 445 | |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 446 | switch (node_type->base) { |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 447 | case LY_TYPE_BINARY: |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 448 | leaf->value.binary = leaf->value_str; |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 449 | |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 450 | if (node_type->info.binary.length |
| 451 | && validate_length_range(0, strlen(leaf->value.binary), 0, 0, node_type, xml, leaf->value_str, log)) { |
Michal Vasko | 0e60c83 | 2015-07-15 15:48:35 +0200 | [diff] [blame] | 452 | return EXIT_FAILURE; |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 453 | } |
| 454 | break; |
| 455 | |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 456 | case LY_TYPE_BITS: |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 457 | /* locate bits structure with the bits definitions */ |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 458 | for (type = node_type; type->der->type.der; type = &type->der->type); |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 459 | |
| 460 | /* allocate the array of pointers to bits definition */ |
| 461 | leaf->value.bit = calloc(type->info.bits.count, sizeof *leaf->value.bit); |
| 462 | |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 463 | if (!leaf->value_str) { |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 464 | /* no bits set */ |
| 465 | break; |
| 466 | } |
| 467 | |
| 468 | c = 0; |
| 469 | i = 0; |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 470 | while (leaf->value_str[c]) { |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 471 | /* skip leading whitespaces */ |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 472 | while(isspace(leaf->value_str[c])) { |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 473 | c++; |
| 474 | } |
| 475 | |
| 476 | /* get the length of the bit identifier */ |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 477 | for (len = 0; leaf->value_str[c] && !isspace(leaf->value_str[c]); c++, len++); |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 478 | |
| 479 | /* go back to the beginning of the identifier */ |
| 480 | c = c - len; |
| 481 | |
| 482 | /* find bit definition, identifiers appear ordered by their posititon */ |
| 483 | for (found = 0; i < type->info.bits.count; i++) { |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 484 | if (!strncmp(type->info.bits.bit[i].name, &leaf->value_str[c], len) |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 485 | && !type->info.bits.bit[i].name[len]) { |
| 486 | /* we have match, store the pointer */ |
| 487 | leaf->value.bit[i] = &type->info.bits.bit[i]; |
| 488 | |
| 489 | /* stop searching */ |
| 490 | i++; |
| 491 | found = 1; |
| 492 | break; |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | if (!found) { |
| 497 | /* referenced bit value does not exists */ |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 498 | if (log) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 499 | LOGVAL(LYE_INVAL, LOGLINE(xml), leaf->value_str, xml->name); |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 500 | } |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 501 | return EXIT_FAILURE; |
| 502 | } |
| 503 | |
| 504 | c = c + len; |
| 505 | } |
| 506 | |
| 507 | break; |
| 508 | |
Radek Krejci | b738464 | 2015-07-09 16:08:45 +0200 | [diff] [blame] | 509 | case LY_TYPE_BOOL: |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 510 | if (!strcmp(leaf->value_str, "true")) { |
Radek Krejci | b738464 | 2015-07-09 16:08:45 +0200 | [diff] [blame] | 511 | leaf->value.bool = 1; |
| 512 | } /* else false, so keep it zero */ |
| 513 | break; |
| 514 | |
Radek Krejci | 7511f40 | 2015-07-10 09:56:30 +0200 | [diff] [blame] | 515 | case LY_TYPE_DEC64: |
| 516 | /* locate dec64 structure with the fraction-digits value */ |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 517 | for (type = node_type; type->der->type.der; type = &type->der->type); |
Radek Krejci | 7511f40 | 2015-07-10 09:56:30 +0200 | [diff] [blame] | 518 | |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 519 | for (c = 0; isspace(leaf->value_str[c]); c++); |
| 520 | for (len = 0; leaf->value_str[c] && !isspace(leaf->value_str[c]); c++, len++); |
Radek Krejci | 7511f40 | 2015-07-10 09:56:30 +0200 | [diff] [blame] | 521 | c = c - len; |
| 522 | if (len > DECSIZE) { |
| 523 | /* too long */ |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 524 | if (log) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 525 | LOGVAL(LYE_INVAL, LOGLINE(xml), leaf->value_str, xml->name); |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 526 | } |
Radek Krejci | 7511f40 | 2015-07-10 09:56:30 +0200 | [diff] [blame] | 527 | return EXIT_FAILURE; |
| 528 | } |
| 529 | |
| 530 | /* normalize the number */ |
| 531 | dec[0] = '\0'; |
| 532 | for (i = j = d = found = 0; i < DECSIZE; i++) { |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 533 | if (leaf->value_str[c + i] == '.') { |
Radek Krejci | 7511f40 | 2015-07-10 09:56:30 +0200 | [diff] [blame] | 534 | found = 1; |
| 535 | j = type->info.dec64.dig; |
| 536 | i--; |
| 537 | c++; |
| 538 | continue; |
| 539 | } |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 540 | if (leaf->value_str[c + i] == '\0') { |
Radek Krejci | 7511f40 | 2015-07-10 09:56:30 +0200 | [diff] [blame] | 541 | c--; |
| 542 | if (!found) { |
| 543 | j = type->info.dec64.dig; |
| 544 | found = 1; |
| 545 | } |
| 546 | if (!j) { |
| 547 | dec[i] = '\0'; |
| 548 | break; |
| 549 | } |
| 550 | d++; |
| 551 | if (d > DECSIZE - 2) { |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 552 | if (log) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 553 | LOGVAL(LYE_OORVAL, LOGLINE(xml), leaf->value_str, xml->name); |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 554 | } |
Radek Krejci | 7511f40 | 2015-07-10 09:56:30 +0200 | [diff] [blame] | 555 | return EXIT_FAILURE; |
| 556 | } |
| 557 | dec[i] = '0'; |
| 558 | } else { |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 559 | if (!isdigit(leaf->value_str[c + i])) { |
| 560 | if (i || leaf->value_str[c] != '-') { |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 561 | if (log) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 562 | LOGVAL(LYE_INVAL, LOGLINE(xml), leaf->value_str, xml->name); |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 563 | } |
Radek Krejci | 7511f40 | 2015-07-10 09:56:30 +0200 | [diff] [blame] | 564 | return EXIT_FAILURE; |
| 565 | } |
| 566 | } else { |
| 567 | d++; |
| 568 | } |
| 569 | if (d > DECSIZE - 2 || (found && !j)) { |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 570 | if (log) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 571 | LOGVAL(LYE_OORVAL, LOGLINE(xml), leaf->value_str, xml->name); |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 572 | } |
Radek Krejci | 7511f40 | 2015-07-10 09:56:30 +0200 | [diff] [blame] | 573 | return EXIT_FAILURE; |
| 574 | } |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 575 | dec[i] = leaf->value_str[c + i]; |
Radek Krejci | 7511f40 | 2015-07-10 09:56:30 +0200 | [diff] [blame] | 576 | } |
| 577 | if (j) { |
| 578 | j--; |
| 579 | } |
| 580 | } |
| 581 | |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 582 | if (parse_int(dec, xml, -9223372036854775807L - 1L, 9223372036854775807L, 10, &num, log) |
| 583 | || validate_length_range(2, 0, 0, ((long double)num)/(1 << type->info.dec64.dig), node_type, xml, leaf->value_str, log)) { |
Radek Krejci | 7511f40 | 2015-07-10 09:56:30 +0200 | [diff] [blame] | 584 | return EXIT_FAILURE; |
| 585 | } |
Michal Vasko | bdee69c | 2015-07-15 15:49:39 +0200 | [diff] [blame] | 586 | leaf->value.dec64 = num; |
Radek Krejci | 7511f40 | 2015-07-10 09:56:30 +0200 | [diff] [blame] | 587 | break; |
| 588 | |
Radek Krejci | bce7374 | 2015-07-10 12:46:06 +0200 | [diff] [blame] | 589 | case LY_TYPE_EMPTY: |
| 590 | /* just check that it is empty */ |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 591 | if (leaf->value_str && leaf->value_str[0]) { |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 592 | if (log) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 593 | LOGVAL(LYE_INVAL, LOGLINE(xml), leaf->value_str, xml->name); |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 594 | } |
Radek Krejci | bce7374 | 2015-07-10 12:46:06 +0200 | [diff] [blame] | 595 | return EXIT_FAILURE; |
| 596 | } |
| 597 | break; |
| 598 | |
Radek Krejci | 5b315a9 | 2015-07-10 13:18:45 +0200 | [diff] [blame] | 599 | case LY_TYPE_ENUM: |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 600 | if (!leaf->value_str) { |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 601 | if (log) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 602 | LOGVAL(LYE_INVAL, LOGLINE(xml), "", xml->name); |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 603 | } |
Radek Krejci | 5b315a9 | 2015-07-10 13:18:45 +0200 | [diff] [blame] | 604 | return EXIT_FAILURE; |
| 605 | } |
| 606 | |
| 607 | /* locate enums structure with the enumeration definitions */ |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 608 | for (type = node_type; type->der->type.der; type = &type->der->type); |
Radek Krejci | 5b315a9 | 2015-07-10 13:18:45 +0200 | [diff] [blame] | 609 | |
| 610 | /* find matching enumeration value */ |
| 611 | for (i = 0; i < type->info.enums.count; i++) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 612 | if (!strcmp(leaf->value_str, type->info.enums.enm[i].name)) { |
Radek Krejci | 5b315a9 | 2015-07-10 13:18:45 +0200 | [diff] [blame] | 613 | /* we have match, store pointer to the definition */ |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 614 | leaf->value.enm = &type->info.enums.enm[i]; |
Radek Krejci | 5b315a9 | 2015-07-10 13:18:45 +0200 | [diff] [blame] | 615 | break; |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | if (!leaf->value.enm) { |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 620 | if (log) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 621 | LOGVAL(LYE_INVAL, LOGLINE(xml), leaf->value_str, xml->name); |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 622 | } |
Radek Krejci | 5b315a9 | 2015-07-10 13:18:45 +0200 | [diff] [blame] | 623 | return EXIT_FAILURE; |
| 624 | } |
| 625 | |
| 626 | break; |
| 627 | |
Radek Krejci | ac8aac6 | 2015-07-10 15:36:35 +0200 | [diff] [blame] | 628 | case LY_TYPE_IDENT: |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 629 | if ((strptr = strchr(leaf->value_str, ':'))) { |
| 630 | len = strptr - leaf->value_str; |
Radek Krejci | ac8aac6 | 2015-07-10 15:36:35 +0200 | [diff] [blame] | 631 | if (!len) { |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 632 | if (log) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 633 | LOGVAL(LYE_INVAL, LOGLINE(xml), leaf->value_str, xml->name); |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 634 | } |
Radek Krejci | ac8aac6 | 2015-07-10 15:36:35 +0200 | [diff] [blame] | 635 | return EXIT_FAILURE; |
| 636 | } |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 637 | strptr = strndup(leaf->value_str, len); |
Radek Krejci | ac8aac6 | 2015-07-10 15:36:35 +0200 | [diff] [blame] | 638 | } |
| 639 | ns = lyxml_get_ns(xml, strptr); |
| 640 | if (!ns) { |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 641 | if (log) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 642 | LOGVAL(LYE_INVAL, LOGLINE(xml), leaf->value_str, xml->name); |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 643 | } |
Radek Krejci | ac8aac6 | 2015-07-10 15:36:35 +0200 | [diff] [blame] | 644 | return EXIT_FAILURE; |
| 645 | } |
| 646 | if (strptr) { |
| 647 | free(strptr); |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 648 | name = leaf->value_str + len + 1; |
Radek Krejci | ac8aac6 | 2015-07-10 15:36:35 +0200 | [diff] [blame] | 649 | } else { |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 650 | name = leaf->value_str; |
Radek Krejci | ac8aac6 | 2015-07-10 15:36:35 +0200 | [diff] [blame] | 651 | } |
| 652 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 653 | leaf->value.ident = resolve_identityref(node_type->info.ident.ref, name, ns->value); |
Radek Krejci | ac8aac6 | 2015-07-10 15:36:35 +0200 | [diff] [blame] | 654 | if (!leaf->value.ident) { |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 655 | if (log) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 656 | LOGVAL(LYE_INVAL, LOGLINE(xml), leaf->value_str, xml->name); |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 657 | } |
Radek Krejci | ac8aac6 | 2015-07-10 15:36:35 +0200 | [diff] [blame] | 658 | return EXIT_FAILURE; |
| 659 | } |
| 660 | break; |
| 661 | |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 662 | case LY_TYPE_INST: |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 663 | if (!leaf->value_str) { |
| 664 | if (log) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 665 | LOGVAL(LYE_INVAL, LOGLINE(xml), "", xml->name); |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 666 | } |
| 667 | return EXIT_FAILURE; |
| 668 | } |
| 669 | |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 670 | /* convert the path from the XML form using XML namespaces into the JSON format |
| 671 | * using module names as namespaces |
| 672 | */ |
| 673 | xml->content = leaf->value_str; |
| 674 | leaf->value_str = instid_xml2json(node->schema->module->ctx, xml); |
| 675 | lydict_remove(node->schema->module->ctx, xml->content); |
| 676 | xml->content = NULL; |
| 677 | if (!leaf->value_str) { |
| 678 | return EXIT_FAILURE; |
| 679 | } |
| 680 | |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 681 | if (options & (LYD_OPT_EDIT | LYD_OPT_FILTER)) { |
| 682 | leaf->value_type |= LY_TYPE_INST_UNRES; |
| 683 | } else { |
| 684 | /* validity checking is performed later, right now the data tree |
| 685 | * is not complete, so many instanceids cannot be resolved |
| 686 | */ |
| 687 | /* remember the leaf for later checking */ |
| 688 | new_unres = malloc(sizeof *new_unres); |
| 689 | new_unres->is_leafref = 0; |
| 690 | new_unres->dnode = node; |
| 691 | new_unres->next = *unres; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 692 | #ifndef NDEBUG |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 693 | new_unres->line = LOGLINE(xml); |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 694 | #endif |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 695 | *unres = new_unres; |
| 696 | } |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 697 | break; |
| 698 | |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 699 | case LY_TYPE_LEAFREF: |
| 700 | if (!leaf->value_str) { |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 701 | if (log) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 702 | LOGVAL(LYE_INVAL, LOGLINE(xml), "", xml->name); |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 703 | } |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 704 | return EXIT_FAILURE; |
| 705 | } |
| 706 | |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 707 | if (options & (LYD_OPT_EDIT | LYD_OPT_FILTER)) { |
| 708 | do { |
| 709 | type = &((struct lys_node_leaf *)leaf->schema)->type.info.lref.target->type; |
| 710 | } while (type->base == LY_TYPE_LEAFREF); |
| 711 | leaf->value_type = type->base | LY_TYPE_LEAFREF_UNRES; |
| 712 | } else { |
| 713 | /* validity checking is performed later, right now the data tree |
| 714 | * is not complete, so many leafrefs cannot be resolved |
| 715 | */ |
| 716 | /* remember the leaf for later checking */ |
| 717 | new_unres = malloc(sizeof *new_unres); |
| 718 | new_unres->is_leafref = 1; |
| 719 | new_unres->dnode = node; |
| 720 | new_unres->next = *unres; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 721 | #ifndef NDEBUG |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 722 | new_unres->line = LOGLINE(xml); |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 723 | #endif |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 724 | *unres = new_unres; |
| 725 | } |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 726 | break; |
| 727 | |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 728 | case LY_TYPE_STRING: |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 729 | leaf->value.string = leaf->value_str; |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 730 | |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 731 | if (node_type->info.str.length |
| 732 | && validate_length_range(0, strlen(leaf->value.string), 0, 0, node_type, xml, leaf->value_str, log)) { |
Michal Vasko | 0e60c83 | 2015-07-15 15:48:35 +0200 | [diff] [blame] | 733 | return EXIT_FAILURE; |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 734 | } |
| 735 | |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 736 | if (node_type->info.str.patterns |
| 737 | && validate_pattern(leaf->value.string, node_type, xml, leaf->value_str, log)) { |
Michal Vasko | 0e60c83 | 2015-07-15 15:48:35 +0200 | [diff] [blame] | 738 | return EXIT_FAILURE; |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 739 | } |
| 740 | break; |
| 741 | |
Michal Vasko | 5811016 | 2015-07-15 15:50:16 +0200 | [diff] [blame] | 742 | case LY_TYPE_UNION: |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 743 | found = 0; |
Michal Vasko | 6da1a37 | 2015-07-27 11:19:10 +0200 | [diff] [blame] | 744 | type = get_next_union_type(node_type, NULL, &found); |
| 745 | for (; type; found = 0, type = get_next_union_type(node_type, type, &found)) { |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 746 | xml->content = leaf->value_str; |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 747 | if (!_xml_get_value(node, type, xml, options, unres, 0)) { |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 748 | leaf->value_type = type->base; |
| 749 | break; |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | if (!type) { |
| 754 | if (log) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 755 | LOGVAL(LYE_INVAL, LOGLINE(xml), leaf->value_str, xml->name); |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 756 | } |
| 757 | return EXIT_FAILURE; |
| 758 | } |
Michal Vasko | 5811016 | 2015-07-15 15:50:16 +0200 | [diff] [blame] | 759 | break; |
| 760 | |
| 761 | case LY_TYPE_INT8: |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 762 | if (parse_int(leaf->value_str, xml, -128, 127, 0, &num, log) |
| 763 | || validate_length_range(1, 0, num, 0, node_type, xml, leaf->value_str, log)) { |
Michal Vasko | 5811016 | 2015-07-15 15:50:16 +0200 | [diff] [blame] | 764 | return EXIT_FAILURE; |
| 765 | } |
| 766 | leaf->value.int8 = num; |
| 767 | break; |
| 768 | |
| 769 | case LY_TYPE_INT16: |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 770 | if (parse_int(leaf->value_str, xml, -32768, 32767, 0, &num, log) |
| 771 | || validate_length_range(1, 0, num, 0, node_type, xml, leaf->value_str, log)) { |
Michal Vasko | 5811016 | 2015-07-15 15:50:16 +0200 | [diff] [blame] | 772 | return EXIT_FAILURE; |
| 773 | } |
| 774 | leaf->value.int16 = num; |
| 775 | break; |
| 776 | |
| 777 | case LY_TYPE_INT32: |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 778 | if (parse_int(leaf->value_str, xml, -2147483648, 2147483647, 0, &num, log) |
| 779 | || validate_length_range(1, 0, num, 0, node_type, xml, leaf->value_str, log)) { |
Michal Vasko | 5811016 | 2015-07-15 15:50:16 +0200 | [diff] [blame] | 780 | return EXIT_FAILURE; |
| 781 | } |
| 782 | leaf->value.int32 = num; |
| 783 | break; |
| 784 | |
| 785 | case LY_TYPE_INT64: |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 786 | if (parse_int(leaf->value_str, xml, -9223372036854775807L - 1L, 9223372036854775807L, 0, &num, log) |
| 787 | || validate_length_range(1, 0, num, 0, node_type, xml, leaf->value_str, log)) { |
Michal Vasko | 5811016 | 2015-07-15 15:50:16 +0200 | [diff] [blame] | 788 | return EXIT_FAILURE; |
| 789 | } |
| 790 | leaf->value.int64 = num; |
| 791 | break; |
| 792 | |
| 793 | case LY_TYPE_UINT8: |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 794 | if (parse_uint(leaf->value_str, xml, 255, 0, &unum, log) |
| 795 | || validate_length_range(0, unum, 0, 0, node_type, xml, leaf->value_str, log)) { |
Michal Vasko | 5811016 | 2015-07-15 15:50:16 +0200 | [diff] [blame] | 796 | return EXIT_FAILURE; |
| 797 | } |
| 798 | leaf->value.uint8 = unum; |
| 799 | break; |
| 800 | |
| 801 | case LY_TYPE_UINT16: |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 802 | if (parse_uint(leaf->value_str, xml, 65535, 0, &unum, log) |
| 803 | || validate_length_range(0, unum, 0, 0, node_type, xml, leaf->value_str, log)) { |
Michal Vasko | 5811016 | 2015-07-15 15:50:16 +0200 | [diff] [blame] | 804 | return EXIT_FAILURE; |
| 805 | } |
| 806 | leaf->value.uint16 = unum; |
| 807 | break; |
| 808 | |
| 809 | case LY_TYPE_UINT32: |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 810 | if (parse_uint(leaf->value_str, xml, 4294967295, 0, &unum, log) |
| 811 | || validate_length_range(0, unum, 0, 0, node_type, xml, leaf->value_str, log)) { |
Michal Vasko | 5811016 | 2015-07-15 15:50:16 +0200 | [diff] [blame] | 812 | return EXIT_FAILURE; |
| 813 | } |
| 814 | leaf->value.uint32 = unum; |
| 815 | break; |
| 816 | |
| 817 | case LY_TYPE_UINT64: |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 818 | if (parse_uint(leaf->value_str, xml, 18446744073709551615UL, 0, &unum, log) |
| 819 | || validate_length_range(0, unum, 0, 0, node_type, xml, leaf->value_str, log)) { |
Michal Vasko | 5811016 | 2015-07-15 15:50:16 +0200 | [diff] [blame] | 820 | return EXIT_FAILURE; |
| 821 | } |
| 822 | leaf->value.uint64 = unum; |
| 823 | break; |
| 824 | |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 825 | default: |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 826 | return EXIT_FAILURE; |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | return EXIT_SUCCESS; |
| 830 | } |
| 831 | |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 832 | static int |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 833 | xml_get_value(struct lyd_node *node, struct lyxml_elem *xml, int options, struct unres_data **unres) |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 834 | { |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 835 | return _xml_get_value(node, &((struct lys_node_leaf *)node->schema)->type, xml, options, unres, 1); |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 836 | } |
| 837 | |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 838 | struct lyd_node * |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 839 | xml_parse_data(struct ly_ctx *ctx, struct lyxml_elem *xml, struct lyd_node *parent, struct lyd_node *prev, |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 840 | int options, struct unres_data **unres) |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 841 | { |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 842 | struct lyd_node *result = NULL, *diter; |
| 843 | struct lys_node *schema = NULL, *siter; |
Radek Krejci | da37434 | 2015-08-19 13:33:22 +0200 | [diff] [blame] | 844 | struct lys_node *cs, *ch; |
Michal Vasko | ab8e440 | 2015-07-17 12:54:28 +0200 | [diff] [blame] | 845 | int i, havechildren; |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 846 | |
| 847 | if (!xml) { |
| 848 | return NULL; |
| 849 | } |
| 850 | if (!xml->ns || !xml->ns->value) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 851 | LOGVAL(LYE_XML_MISS, LOGLINE(xml), "element's", "namespace"); |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 852 | return NULL; |
| 853 | } |
| 854 | |
| 855 | /* find schema node */ |
| 856 | if (!parent) { |
| 857 | /* starting in root */ |
| 858 | for (i = 0; i < ctx->models.used; i++) { |
| 859 | /* match data model based on namespace */ |
| 860 | if (ctx->models.list[i]->ns == xml->ns->value) { |
| 861 | /* get the proper schema node */ |
| 862 | LY_TREE_FOR(ctx->models.list[i]->data, schema) { |
| 863 | if (schema->name == xml->name) { |
| 864 | break; |
| 865 | } |
| 866 | } |
| 867 | break; |
| 868 | } |
| 869 | } |
| 870 | } else { |
| 871 | /* parsing some internal node, we start with parent's schema pointer */ |
| 872 | schema = xml_data_search_schemanode(xml, parent->schema->child); |
| 873 | } |
| 874 | if (!schema) { |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 875 | if ((options & LYD_OPT_STRICT) || ly_ctx_get_module_by_ns(ctx, xml->ns->value, NULL)) { |
| 876 | LOGVAL(LYE_INELEM, LOGLINE(xml), xml->name); |
| 877 | return NULL; |
| 878 | } else { |
| 879 | goto siblings; |
| 880 | } |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 881 | } |
| 882 | |
Radek Krejci | 074bf85 | 2015-08-19 14:22:16 +0200 | [diff] [blame^] | 883 | /* check for (non-)presence of status data in edit-config data */ |
| 884 | if ((options & LYD_OPT_EDIT) && (schema->flags & LYS_CONFIG_R)) { |
| 885 | LOGVAL(LYE_INELEM, LOGLINE(xml), schema->name); |
| 886 | return NULL; |
| 887 | } |
| 888 | /* check if the node instance is enabled by if-feature */ |
| 889 | if (lys_is_disabled(schema, 2)) { |
| 890 | LOGVAL(LYE_INELEM, LOGLINE(xml), schema->name); |
| 891 | return NULL; |
| 892 | } |
| 893 | |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 894 | switch (schema->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 895 | case LYS_CONTAINER: |
Michal Vasko | ab8e440 | 2015-07-17 12:54:28 +0200 | [diff] [blame] | 896 | result = calloc(1, sizeof *result); |
| 897 | havechildren = 1; |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 898 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 899 | case LYS_LEAF: |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 900 | result = calloc(1, sizeof(struct lyd_node_leaf)); |
| 901 | havechildren = 0; |
| 902 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 903 | case LYS_LEAFLIST: |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 904 | result = calloc(1, sizeof(struct lyd_node_leaflist)); |
| 905 | havechildren = 0; |
| 906 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 907 | case LYS_LIST: |
Michal Vasko | ab8e440 | 2015-07-17 12:54:28 +0200 | [diff] [blame] | 908 | result = calloc(1, sizeof(struct lyd_node_list)); |
| 909 | havechildren = 1; |
| 910 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 911 | case LYS_ANYXML: |
Michal Vasko | ab8e440 | 2015-07-17 12:54:28 +0200 | [diff] [blame] | 912 | result = calloc(1, sizeof(struct lyd_node_anyxml)); |
| 913 | havechildren = 0; |
| 914 | break; |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 915 | default: |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 916 | LOGINT; |
Michal Vasko | ab8e440 | 2015-07-17 12:54:28 +0200 | [diff] [blame] | 917 | return NULL; |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 918 | } |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 919 | result->parent = parent; |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 920 | result->prev = prev; |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 921 | result->schema = schema; |
| 922 | |
Radek Krejci | 88daf82 | 2015-08-10 13:58:16 +0200 | [diff] [blame] | 923 | /* check number of instances for non-list nodes */ |
Radek Krejci | b677214 | 2015-08-12 10:16:44 +0200 | [diff] [blame] | 924 | if (!(options & LYD_OPT_FILTER) && (schema->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_ANYXML))) { |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 925 | for (diter = result->prev; diter; diter = diter->prev) { |
| 926 | if (diter->schema == schema) { |
Radek Krejci | 88daf82 | 2015-08-10 13:58:16 +0200 | [diff] [blame] | 927 | LOGVAL(LYE_TOOMANY, LOGLINE(xml), xml->name, xml->parent ? xml->parent->name : "data tree"); |
| 928 | goto error; |
| 929 | } |
| 930 | } |
| 931 | } |
| 932 | |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 933 | /* type specific processing */ |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 934 | if (schema->nodetype == LYS_LIST) { |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 935 | /* pointers to next and previous instances of the same list */ |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 936 | for (diter = result->prev; diter; diter = diter->prev) { |
| 937 | if (diter->schema == result->schema) { |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 938 | /* instances of the same list */ |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 939 | ((struct lyd_node_list *)diter)->lnext = (struct lyd_node_list *)result; |
| 940 | ((struct lyd_node_list *)result)->lprev = (struct lyd_node_list *)diter; |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 941 | break; |
| 942 | } |
| 943 | } |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 944 | } else if (schema->nodetype == LYS_LEAF) { |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 945 | /* type detection and assigning the value */ |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 946 | if (xml_get_value(result, xml, options, unres)) { |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 947 | goto error; |
| 948 | } |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 949 | } else if (schema->nodetype == LYS_LEAFLIST) { |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 950 | /* type detection and assigning the value */ |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 951 | if (xml_get_value(result, xml, options, unres)) { |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 952 | goto error; |
| 953 | } |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 954 | |
| 955 | /* pointers to next and previous instances of the same leaflist */ |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 956 | for (diter = result->prev; diter; diter = diter->prev) { |
| 957 | if (diter->schema == result->schema) { |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 958 | /* instances of the same list */ |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 959 | ((struct lyd_node_leaflist *)diter)->lnext = (struct lyd_node_leaflist *)result; |
| 960 | ((struct lyd_node_leaflist *)result)->lprev = (struct lyd_node_leaflist *)diter; |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 961 | break; |
| 962 | } |
| 963 | } |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 964 | } else if (schema->nodetype == LYS_ANYXML) { |
Michal Vasko | ab8e440 | 2015-07-17 12:54:28 +0200 | [diff] [blame] | 965 | ((struct lyd_node_anyxml *)result)->value = xml; |
| 966 | lyxml_unlink_elem(xml); |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 967 | } |
| 968 | |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 969 | /* process children */ |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 970 | if (havechildren && xml->child) { |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 971 | result->child = xml_parse_data(ctx, xml->child, result, NULL, options, unres); |
| 972 | if (ly_errno) { |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 973 | goto error; |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 974 | } |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 975 | } |
| 976 | |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 977 | /* various validation checks */ |
Radek Krejci | 1073cc0 | 2015-08-12 20:37:39 +0200 | [diff] [blame] | 978 | |
| 979 | /* check presence of all keys in case of list */ |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 980 | if (schema->nodetype == LYS_LIST && !(options & LYD_OPT_FILTER)) { |
Radek Krejci | 1073cc0 | 2015-08-12 20:37:39 +0200 | [diff] [blame] | 981 | siter = (struct lys_node *)lyv_keys_present((struct lyd_node_list *)result); |
| 982 | if (siter) { |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 983 | /* key not found in the data */ |
Radek Krejci | 1073cc0 | 2015-08-12 20:37:39 +0200 | [diff] [blame] | 984 | LOGVAL(LYE_MISSELEM, LOGLINE(xml), siter->name, schema->name); |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 985 | goto error; |
| 986 | } |
| 987 | } |
| 988 | |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 989 | /* mandatory children */ |
| 990 | if (havechildren && !(options & (LYD_OPT_FILTER | LYD_OPT_EDIT))) { |
| 991 | siter = ly_check_mandatory(result); |
| 992 | if (siter) { |
Radek Krejci | 14a11a6 | 2015-08-17 17:27:38 +0200 | [diff] [blame] | 993 | if (siter->nodetype & (LYS_LIST | LYS_LEAFLIST)) { |
| 994 | LOGVAL(LYE_SPEC, LOGLINE(xml), "Number of \"%s\" instances in \"%s\" does not follow min/max constraints.", |
| 995 | siter->name, siter->parent->name); |
| 996 | } else { |
| 997 | LOGVAL(LYE_MISSELEM, LOGLINE(xml), siter->name, siter->parent->name); |
| 998 | } |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 999 | goto error; |
| 1000 | } |
| 1001 | } |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 1002 | |
Radek Krejci | 78ce861 | 2015-08-18 14:31:05 +0200 | [diff] [blame] | 1003 | /* uniqueness of (leaf-)list instances */ |
| 1004 | if (schema->nodetype == LYS_LEAFLIST) { |
| 1005 | /* check uniqueness of the leaf-list instances (compare values) */ |
| 1006 | for (diter = (struct lyd_node *)((struct lyd_node_leaflist *)result)->lprev; |
| 1007 | diter; |
| 1008 | diter = (struct lyd_node *)((struct lyd_node_leaflist *)diter)->lprev) { |
| 1009 | if (!lyd_compare(diter, result, 0)) { |
Radek Krejci | b3d6d55 | 2015-08-19 11:56:13 +0200 | [diff] [blame] | 1010 | if (options & LYD_OPT_FILTER) { |
| 1011 | /* optimize filter and do not duplicate the same selection node, |
| 1012 | * so this is not actually error, but the data are silently removed */ |
| 1013 | ((struct lyd_node_leaflist *)result)->lprev->lnext = NULL; |
| 1014 | result->next = NULL; |
| 1015 | result->parent = NULL; |
| 1016 | result->prev = result; |
| 1017 | lyd_free(result); |
| 1018 | result = NULL; |
| 1019 | break; |
| 1020 | } else { |
| 1021 | LOGVAL(LYE_DUPLEAFLIST, LOGLINE(xml), schema->name, ((struct lyd_node_leaflist *)result)->value_str); |
| 1022 | goto error; |
| 1023 | } |
Radek Krejci | 78ce861 | 2015-08-18 14:31:05 +0200 | [diff] [blame] | 1024 | } |
| 1025 | } |
| 1026 | } else if (schema->nodetype == LYS_LIST) { |
Radek Krejci | b3d6d55 | 2015-08-19 11:56:13 +0200 | [diff] [blame] | 1027 | /* check uniqueness of the list instances */ |
Radek Krejci | 78ce861 | 2015-08-18 14:31:05 +0200 | [diff] [blame] | 1028 | for (diter = (struct lyd_node *)((struct lyd_node_list *)result)->lprev; |
| 1029 | diter; |
| 1030 | diter = (struct lyd_node *)((struct lyd_node_list *)diter)->lprev) { |
Radek Krejci | b3d6d55 | 2015-08-19 11:56:13 +0200 | [diff] [blame] | 1031 | if (options & LYD_OPT_FILTER) { |
| 1032 | /* compare content match nodes */ |
| 1033 | if (!lyd_filter_compare(diter, result)) { |
| 1034 | /* merge both nodes */ |
| 1035 | /* add selection and containment nodes from result into the diter, |
| 1036 | * but only in case the diter already contains some selection nodes, |
| 1037 | * otherwise it already will return all the data */ |
| 1038 | lyd_filter_merge(diter, result); |
| 1039 | |
| 1040 | /* not the error, just return no data */ |
| 1041 | ((struct lyd_node_list *)result)->lprev->lnext = NULL; |
| 1042 | result->next = NULL; |
| 1043 | result->parent = NULL; |
| 1044 | result->prev = result; |
| 1045 | lyd_free(result); |
| 1046 | result = NULL; |
| 1047 | break; |
| 1048 | } |
| 1049 | } else { |
| 1050 | /* compare keys and unique combinations */ |
| 1051 | if (!lyd_compare(diter, result, 1)) { |
| 1052 | LOGVAL(LYE_DUPLIST, LOGLINE(xml), schema->name); |
| 1053 | goto error; |
| 1054 | } |
Radek Krejci | 78ce861 | 2015-08-18 14:31:05 +0200 | [diff] [blame] | 1055 | } |
| 1056 | } |
Radek Krejci | da37434 | 2015-08-19 13:33:22 +0200 | [diff] [blame] | 1057 | } else if (!(options & LYD_OPT_FILTER) && schema->parent && (schema->parent->nodetype & (LYS_CASE | LYS_CHOICE))) { |
| 1058 | /* check that there are no data from different choice case */ |
| 1059 | if (schema->parent->nodetype == LYS_CHOICE) { |
| 1060 | cs = NULL; |
| 1061 | ch = schema->parent; |
| 1062 | } else { /* schema->parent->nodetype == LYS_CASE */ |
| 1063 | cs = schema->parent; |
| 1064 | ch = schema->parent->parent; |
| 1065 | } |
| 1066 | if (ch->parent && ch->parent->nodetype == LYS_CASE) { |
| 1067 | /* TODO check schemas with a choice inside a case */ |
| 1068 | LOGWRN("Not checking parent branches of nested choice"); |
| 1069 | } |
| 1070 | for (diter = result->prev; diter; diter = diter->prev) { |
| 1071 | if ((diter->schema->parent->nodetype == LYS_CHOICE && diter->schema->parent == ch) || |
| 1072 | (diter->schema->parent->nodetype == LYS_CASE && !cs) || |
| 1073 | (diter->schema->parent->nodetype == LYS_CASE && cs && diter->schema->parent != cs && diter->schema->parent->parent == ch)) { |
| 1074 | LOGVAL(LYE_MCASEDATA, LOGLINE(xml), ch->name); |
| 1075 | goto error; |
| 1076 | } |
| 1077 | } |
Radek Krejci | 78ce861 | 2015-08-18 14:31:05 +0200 | [diff] [blame] | 1078 | } |
| 1079 | |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 1080 | siblings: |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 1081 | /* process siblings */ |
| 1082 | if (xml->next) { |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 1083 | if (result) { |
| 1084 | result->next = xml_parse_data(ctx, xml->next, parent, result, options, unres); |
| 1085 | } else { |
| 1086 | result = xml_parse_data(ctx, xml->next, parent, prev, options, unres); |
| 1087 | } |
| 1088 | if (ly_errno) { |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 1089 | goto error; |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 1090 | } |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 1091 | } |
| 1092 | |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 1093 | /* fix the "last" pointer */ |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 1094 | if (result && !result->prev) { |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 1095 | for (diter = result; diter->next; diter = diter->next); |
| 1096 | result->prev = diter; |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 1097 | } |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 1098 | return result; |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 1099 | |
| 1100 | error: |
| 1101 | |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 1102 | if (result) { |
| 1103 | result->next = NULL; |
| 1104 | result->parent = NULL; |
| 1105 | result->prev = result; |
| 1106 | lyd_free(result); |
Radek Krejci | 7511f40 | 2015-07-10 09:56:30 +0200 | [diff] [blame] | 1107 | } |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 1108 | |
| 1109 | return NULL; |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 1110 | } |
| 1111 | |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 1112 | static int |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 1113 | check_unres(struct unres_data **list) |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 1114 | { |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 1115 | struct lyd_node_leaf *leaf; |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 1116 | struct lys_node_leaf *sleaf; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 1117 | struct unres_data *item, *refset = NULL, *ref; |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 1118 | |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 1119 | while (*list) { |
Michal Vasko | 8ed7530 | 2015-07-17 11:05:14 +0200 | [diff] [blame] | 1120 | leaf = (struct lyd_node_leaf *)(*list)->dnode; |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 1121 | sleaf = (struct lys_node_leaf *)(*list)->dnode->schema; |
Michal Vasko | 8ed7530 | 2015-07-17 11:05:14 +0200 | [diff] [blame] | 1122 | |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 1123 | /* resolve path and create a set of possible leafrefs (we need their values) */ |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 1124 | if ((*list)->is_leafref) { |
Michal Vasko | 1f76a28 | 2015-08-04 16:16:53 +0200 | [diff] [blame] | 1125 | if (resolve_path_arg_data(*list, sleaf->type.info.lref.path, &refset)) { |
Michal Vasko | 955abbb | 2015-07-17 11:31:21 +0200 | [diff] [blame] | 1126 | LOGERR(LY_EVALID, "Leafref \"%s\" could not be resolved.", sleaf->type.info.lref.path); |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 1127 | goto error; |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 1128 | } |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 1129 | |
| 1130 | while (refset) { |
| 1131 | if (leaf->value_str == ((struct lyd_node_leaf *)refset->dnode)->value_str) { |
| 1132 | leaf->value.leafref = refset->dnode; |
| 1133 | } |
| 1134 | ref = refset->next; |
| 1135 | free(refset); |
| 1136 | refset = ref; |
| 1137 | } |
| 1138 | |
| 1139 | if (!leaf->value.leafref) { |
| 1140 | /* reference not found */ |
Michal Vasko | 955abbb | 2015-07-17 11:31:21 +0200 | [diff] [blame] | 1141 | LOGERR(LY_EVALID, "Leafref \"%s\" value \"%s\" did not match any node value.", sleaf->type.info.lref.path, leaf->value_str); |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 1142 | goto error; |
| 1143 | } |
| 1144 | |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 1145 | /* instance-identifier */ |
| 1146 | } else { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 1147 | ly_errno = 0; |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 1148 | if (!resolve_instid((*list)->dnode, leaf->value_str, LOGLINE(*list))) { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 1149 | if (ly_errno) { |
| 1150 | goto error; |
| 1151 | } else if (sleaf->type.info.inst.req > -1) { |
| 1152 | LOGERR(LY_EVALID, "Instance for the \"%s\" does not exist.", leaf->value_str); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1153 | goto error; |
| 1154 | } else { |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 1155 | LOGVRB("Instance for the \"%s\" does not exist.", leaf->value_str); |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 1156 | } |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 1157 | } |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 1158 | } |
Michal Vasko | 8ed7530 | 2015-07-17 11:05:14 +0200 | [diff] [blame] | 1159 | |
| 1160 | item = (*list)->next; |
| 1161 | free(*list); |
| 1162 | *list = item; |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 1163 | } |
| 1164 | |
| 1165 | return EXIT_SUCCESS; |
| 1166 | |
| 1167 | error: |
| 1168 | |
| 1169 | while (*list) { |
| 1170 | item = (*list)->next; |
| 1171 | free(*list); |
| 1172 | *list = item; |
| 1173 | } |
| 1174 | |
| 1175 | return EXIT_FAILURE; |
| 1176 | } |
| 1177 | |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 1178 | struct lyd_node * |
| 1179 | xml_read_data(struct ly_ctx *ctx, const char *data, int options) |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 1180 | { |
| 1181 | struct lyxml_elem *xml; |
Radek Krejci | 0e1d1a6 | 2015-07-31 11:17:01 +0200 | [diff] [blame] | 1182 | struct lyd_node *result, *next, *iter; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 1183 | struct unres_data *unres = NULL; |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 1184 | |
| 1185 | xml = lyxml_read(ctx, data, 0); |
| 1186 | if (!xml) { |
| 1187 | return NULL; |
| 1188 | } |
| 1189 | |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 1190 | ly_errno = 0; |
| 1191 | result = xml_parse_data(ctx, xml->child, NULL, NULL, options, &unres); |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 1192 | /* check leafrefs and/or instids if any */ |
| 1193 | if (check_unres(&unres)) { |
| 1194 | /* leafref & instid checking failed */ |
Radek Krejci | 0e1d1a6 | 2015-07-31 11:17:01 +0200 | [diff] [blame] | 1195 | LY_TREE_FOR_SAFE(result, next, iter) { |
| 1196 | lyd_free(iter); |
| 1197 | } |
| 1198 | result = NULL; |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 1199 | } |
| 1200 | |
| 1201 | /* free source XML tree */ |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 1202 | lyxml_free_elem(ctx, xml); |
| 1203 | |
| 1204 | return result; |
| 1205 | } |