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 | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame^] | 27 | #include <limits.h> |
Michal Vasko | be190a6 | 2015-07-13 16:14:20 +0200 | [diff] [blame] | 28 | #include <sys/types.h> |
| 29 | #include <regex.h> |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 30 | |
Radek Krejci | 998a0b8 | 2015-08-17 13:14:36 +0200 | [diff] [blame] | 31 | #include "libyang.h" |
| 32 | #include "common.h" |
| 33 | #include "context.h" |
| 34 | #include "resolve.h" |
| 35 | #include "xml.h" |
| 36 | #include "tree_internal.h" |
| 37 | #include "validation.h" |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 38 | |
| 39 | #define LY_NSNC "urn:ietf:params:xml:ns:netconf:base:1.0" |
| 40 | |
Michal Vasko | 249e6b5 | 2015-08-19 11:08:52 +0200 | [diff] [blame] | 41 | /** |
| 42 | * @brief Transform instance-identifier from XML data format to |
| 43 | * JSON data format (replace prefixes with module names). |
| 44 | * Logs directly. |
| 45 | * |
| 46 | * @param[in] ctx Main context with the dictionary. |
| 47 | * @param[in] xml Instance-identifier XML element. |
| 48 | * |
| 49 | * @return Transformed instance-identifier or NULL on error. |
| 50 | */ |
| 51 | static const char * |
| 52 | transform_instid_xml2json(struct ly_ctx *ctx, struct lyxml_elem *xml) |
| 53 | { |
| 54 | const char *in, *id; |
| 55 | char *out, *col, *prefix; |
| 56 | size_t out_size, out_used, id_len, rc; |
| 57 | struct lys_module *mod; |
| 58 | struct lyxml_ns *ns; |
| 59 | |
| 60 | in = xml->content; |
| 61 | out_size = strlen(in)+1; |
| 62 | out = malloc(out_size); |
| 63 | out_used = 0; |
| 64 | |
| 65 | while (1) { |
| 66 | col = strchr(in, ':'); |
| 67 | /* we're finished, copy the remaining part */ |
| 68 | if (!col) { |
| 69 | strcpy(&out[out_used], in); |
| 70 | out_used += strlen(in)+1; |
| 71 | assert(out_size == out_used); |
| 72 | return lydict_insert_zc(ctx, out); |
| 73 | } |
| 74 | id = strpbrk_backwards(col-1, "/ [", (col-in)-1); |
| 75 | ++id; |
| 76 | id_len = col-id; |
| 77 | rc = parse_identifier(id); |
| 78 | if (rc < id_len) { |
| 79 | LOGVAL(LYE_INCHAR, LOGLINE(xml), id[rc], &id[rc]); |
| 80 | free(out); |
| 81 | return NULL; |
| 82 | } |
| 83 | |
| 84 | /* get the module */ |
| 85 | prefix = strndup(id, id_len); |
| 86 | ns = lyxml_get_ns(xml, prefix); |
| 87 | free(prefix); |
| 88 | if (!ns) { |
| 89 | LOGVAL(LYE_SPEC, LOGLINE(xml), "XML namespace with prefix \"%.*s\" not defined.", id_len, id); |
| 90 | free(out); |
| 91 | return NULL; |
| 92 | } |
| 93 | mod = ly_ctx_get_module_by_ns(ctx, ns->value, NULL); |
| 94 | if (!mod) { |
| 95 | LOGVAL(LYE_SPEC, LOGLINE(xml), "Module with the namespace \"%s\" could not be found.", ns->value); |
| 96 | free(out); |
| 97 | return NULL; |
| 98 | } |
| 99 | |
| 100 | /* adjust out size (it can even decrease in some strange cases) */ |
| 101 | out_size += strlen(mod->name)-id_len; |
| 102 | out = realloc(out, out_size); |
| 103 | |
| 104 | /* copy the data before prefix */ |
| 105 | strncpy(&out[out_used], in, id-in); |
| 106 | out_used += id-in; |
| 107 | |
| 108 | /* copy the model name */ |
| 109 | strcpy(&out[out_used], mod->name); |
| 110 | out_used += strlen(mod->name); |
| 111 | |
| 112 | /* copy ':' */ |
| 113 | out[out_used] = ':'; |
| 114 | ++out_used; |
| 115 | |
| 116 | /* finally adjust in pointer for next round */ |
| 117 | in = col+1; |
| 118 | } |
| 119 | |
| 120 | /* unreachable */ |
| 121 | assert(0); |
| 122 | return NULL; |
| 123 | } |
| 124 | |
Michal Vasko | 9286afd | 2015-07-14 15:27:59 +0200 | [diff] [blame] | 125 | /* kind == 0 - unsigned (unum used), 1 - signed (snum used), 2 - floating point (fnum used) */ |
| 126 | static int |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 127 | 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] | 128 | struct lyxml_elem *xml, const char *str_val, int log) |
Michal Vasko | 9286afd | 2015-07-14 15:27:59 +0200 | [diff] [blame] | 129 | { |
Michal Vasko | 020404f | 2015-07-15 15:45:42 +0200 | [diff] [blame] | 130 | struct len_ran_intv *intv = NULL, *tmp_intv; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 131 | int ret = EXIT_FAILURE; |
Michal Vasko | 9286afd | 2015-07-14 15:27:59 +0200 | [diff] [blame] | 132 | |
Michal Vasko | 9c1bc64 | 2015-08-05 16:25:53 +0200 | [diff] [blame] | 133 | if (resolve_len_ran_interval(NULL, type, 0, &intv)) { |
| 134 | return EXIT_FAILURE; |
| 135 | } |
Michal Vasko | 020404f | 2015-07-15 15:45:42 +0200 | [diff] [blame] | 136 | if (!intv) { |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 137 | return EXIT_SUCCESS; |
Michal Vasko | 020404f | 2015-07-15 15:45:42 +0200 | [diff] [blame] | 138 | } |
| 139 | |
Michal Vasko | 9286afd | 2015-07-14 15:27:59 +0200 | [diff] [blame] | 140 | for (tmp_intv = intv; tmp_intv; tmp_intv = tmp_intv->next) { |
| 141 | if (((kind == 0) && (unum < tmp_intv->value.uval.min)) |
| 142 | || ((kind == 1) && (snum < tmp_intv->value.sval.min)) |
| 143 | || ((kind == 2) && (fnum < tmp_intv->value.fval.min))) { |
| 144 | break; |
| 145 | } |
| 146 | |
| 147 | if (((kind == 0) && (unum >= tmp_intv->value.uval.min) && (unum <= tmp_intv->value.uval.max)) |
| 148 | || ((kind == 1) && (snum >= tmp_intv->value.sval.min) && (snum <= tmp_intv->value.sval.max)) |
| 149 | || ((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] | 150 | ret = EXIT_SUCCESS; |
Michal Vasko | 9286afd | 2015-07-14 15:27:59 +0200 | [diff] [blame] | 151 | break; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | while (intv) { |
| 156 | tmp_intv = intv->next; |
| 157 | free(intv); |
| 158 | intv = tmp_intv; |
| 159 | } |
| 160 | |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 161 | if (ret && log) { |
Michal Vasko | c07187d | 2015-08-13 15:20:57 +0200 | [diff] [blame] | 162 | /* so that it's used even in RELEASE target */ |
| 163 | (void)xml; |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 164 | LOGVAL(LYE_OORVAL, LOGLINE(xml), str_val); |
Michal Vasko | 0e60c83 | 2015-07-15 15:48:35 +0200 | [diff] [blame] | 165 | } |
Michal Vasko | 9286afd | 2015-07-14 15:27:59 +0200 | [diff] [blame] | 166 | return ret; |
| 167 | } |
| 168 | |
Michal Vasko | fbf7e48 | 2015-07-14 14:49:03 +0200 | [diff] [blame] | 169 | static int |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 170 | 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] | 171 | { |
| 172 | int i; |
| 173 | regex_t preq; |
| 174 | char *posix_regex; |
| 175 | |
| 176 | assert(type->base == LY_TYPE_STRING); |
| 177 | |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 178 | 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] | 179 | return EXIT_FAILURE; |
Michal Vasko | fbf7e48 | 2015-07-14 14:49:03 +0200 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | for (i = 0; i < type->info.str.pat_count; ++i) { |
| 183 | /* |
| 184 | * adjust the expression to a POSIX.2 equivalent |
| 185 | * |
| 186 | * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs |
| 187 | */ |
| 188 | posix_regex = malloc((strlen(type->info.str.patterns[i].expr)+3) * sizeof(char)); |
| 189 | posix_regex[0] = '\0'; |
| 190 | |
| 191 | if (strncmp(type->info.str.patterns[i].expr, ".*", 2)) { |
| 192 | strcat(posix_regex, "^"); |
| 193 | } |
| 194 | strcat(posix_regex, type->info.str.patterns[i].expr); |
| 195 | if (strncmp(type->info.str.patterns[i].expr |
| 196 | + strlen(type->info.str.patterns[i].expr) - 2, ".*", 2)) { |
| 197 | strcat(posix_regex, "$"); |
| 198 | } |
| 199 | |
| 200 | /* must return 0, already checked during parsing */ |
Michal Vasko | 88876dc | 2015-08-05 09:46:40 +0200 | [diff] [blame] | 201 | if (regcomp(&preq, posix_regex, REG_EXTENDED | REG_NOSUB)) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 202 | LOGINT; |
Radek Krejci | 61ff1a0 | 2015-08-13 09:11:34 +0200 | [diff] [blame] | 203 | free(posix_regex); |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 204 | return EXIT_FAILURE; |
Michal Vasko | 88876dc | 2015-08-05 09:46:40 +0200 | [diff] [blame] | 205 | } |
Michal Vasko | fbf7e48 | 2015-07-14 14:49:03 +0200 | [diff] [blame] | 206 | free(posix_regex); |
| 207 | |
| 208 | if (regexec(&preq, str, 0, 0, 0)) { |
| 209 | regfree(&preq); |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 210 | if (log) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 211 | LOGVAL(LYE_INVAL, LOGLINE(xml), str_val, xml->name); |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 212 | } |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 213 | return EXIT_FAILURE; |
Michal Vasko | fbf7e48 | 2015-07-14 14:49:03 +0200 | [diff] [blame] | 214 | } |
| 215 | regfree(&preq); |
| 216 | } |
| 217 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 218 | return EXIT_SUCCESS; |
Michal Vasko | fbf7e48 | 2015-07-14 14:49:03 +0200 | [diff] [blame] | 219 | } |
| 220 | |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 221 | static struct lys_node * |
| 222 | xml_data_search_schemanode(struct lyxml_elem *xml, struct lys_node *start) |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 223 | { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 224 | struct lys_node *result, *aux; |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 225 | |
| 226 | LY_TREE_FOR(start, result) { |
| 227 | /* skip groupings */ |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 228 | if (result->nodetype == LYS_GROUPING) { |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 229 | continue; |
| 230 | } |
| 231 | |
| 232 | /* go into cases, choices, uses */ |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 233 | if (result->nodetype & (LYS_CHOICE | LYS_CASE | LYS_USES)) { |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 234 | aux = xml_data_search_schemanode(xml, result->child); |
| 235 | if (aux) { |
| 236 | /* we have matching result */ |
| 237 | return aux; |
| 238 | } |
| 239 | /* else, continue with next schema node */ |
| 240 | continue; |
| 241 | } |
| 242 | |
| 243 | /* match data nodes */ |
| 244 | if (result->name == xml->name) { |
| 245 | /* names matches, what about namespaces? */ |
| 246 | if (result->module->ns == xml->ns->value) { |
| 247 | /* we have matching result */ |
| 248 | return result; |
| 249 | } |
| 250 | /* else, continue with next schema node */ |
| 251 | continue; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | /* no match */ |
| 256 | return NULL; |
| 257 | } |
| 258 | |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 259 | static int |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 260 | 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] | 261 | { |
| 262 | char *strptr; |
| 263 | |
| 264 | /* convert to 64-bit integer, all the redundant characters are handled */ |
| 265 | errno = 0; |
| 266 | strptr = NULL; |
| 267 | *ret = strtoll(str_val, &strptr, base); |
| 268 | if (errno || (*ret < min) || (*ret > max)) { |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 269 | if (log) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 270 | LOGVAL(LYE_OORVAL, LOGLINE(xml), str_val, xml->name); |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 271 | } |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 272 | return EXIT_FAILURE; |
Michal Vasko | bdee69c | 2015-07-15 15:49:39 +0200 | [diff] [blame] | 273 | } else if (strptr && *strptr) { |
| 274 | while (isspace(*strptr)) { |
| 275 | ++strptr; |
| 276 | } |
| 277 | if (*strptr) { |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 278 | if (log) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 279 | LOGVAL(LYE_INVAL, LOGLINE(xml), str_val, xml->name); |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 280 | } |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 281 | return EXIT_FAILURE; |
Michal Vasko | bdee69c | 2015-07-15 15:49:39 +0200 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 285 | return EXIT_SUCCESS; |
Michal Vasko | bdee69c | 2015-07-15 15:49:39 +0200 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | static int |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 289 | 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] | 290 | { |
| 291 | char *strptr; |
| 292 | |
| 293 | errno = 0; |
| 294 | strptr = NULL; |
| 295 | *ret = strtoull(str_val, &strptr, base); |
| 296 | if (errno || (*ret > max)) { |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 297 | if (log) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 298 | LOGVAL(LYE_OORVAL, LOGLINE(xml), str_val, xml->name); |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 299 | } |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 300 | return EXIT_FAILURE; |
Michal Vasko | bdee69c | 2015-07-15 15:49:39 +0200 | [diff] [blame] | 301 | } else if (strptr && *strptr) { |
| 302 | while (isspace(*strptr)) { |
| 303 | ++strptr; |
| 304 | } |
| 305 | if (*strptr) { |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 306 | if (log) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 307 | LOGVAL(LYE_INVAL, LOGLINE(xml), str_val, xml->name); |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 308 | } |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 309 | return EXIT_FAILURE; |
Michal Vasko | bdee69c | 2015-07-15 15:49:39 +0200 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 313 | return EXIT_SUCCESS; |
Michal Vasko | bdee69c | 2015-07-15 15:49:39 +0200 | [diff] [blame] | 314 | } |
| 315 | |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 316 | static struct lys_type * |
| 317 | 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] | 318 | { |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 319 | int i; |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 320 | struct lys_type *ret = NULL; |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 321 | |
Michal Vasko | 6da1a37 | 2015-07-27 11:19:10 +0200 | [diff] [blame] | 322 | for (i = 0; i < type->info.uni.count; ++i) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 323 | if (type->info.uni.types[i].base == LY_TYPE_UNION) { |
| 324 | 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] | 325 | if (ret) { |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 326 | break;; |
| 327 | } |
| 328 | continue; |
| 329 | } |
| 330 | |
| 331 | if (!prev_type || *found) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 332 | ret = &type->info.uni.types[i]; |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 333 | break; |
| 334 | } |
| 335 | |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 336 | if (&type->info.uni.types[i] == prev_type) { |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 337 | *found = 1; |
| 338 | } |
| 339 | } |
| 340 | |
Michal Vasko | 6da1a37 | 2015-07-27 11:19:10 +0200 | [diff] [blame] | 341 | if (!ret && type->der) { |
| 342 | ret = get_next_union_type(&type->der->type, prev_type, found); |
| 343 | } |
| 344 | |
| 345 | return ret; |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | static int |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 349 | _xml_get_value(struct lyd_node *node, struct lys_type *node_type, struct lyxml_elem *xml, |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 350 | int options, struct unres_data *unres, int log) |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 351 | { |
| 352 | #define DECSIZE 21 |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 353 | struct lyd_node_leaf *leaf = (struct lyd_node_leaf *)node; |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 354 | struct lys_type *type; |
Radek Krejci | ac8aac6 | 2015-07-10 15:36:35 +0200 | [diff] [blame] | 355 | struct lyxml_ns *ns; |
Radek Krejci | 7511f40 | 2015-07-10 09:56:30 +0200 | [diff] [blame] | 356 | char dec[DECSIZE]; |
Michal Vasko | fbf7e48 | 2015-07-14 14:49:03 +0200 | [diff] [blame] | 357 | char *strptr; |
Radek Krejci | ac8aac6 | 2015-07-10 15:36:35 +0200 | [diff] [blame] | 358 | const char *name; |
Michal Vasko | bdee69c | 2015-07-15 15:49:39 +0200 | [diff] [blame] | 359 | int64_t num; |
| 360 | uint64_t unum; |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 361 | int len; |
Radek Krejci | 7511f40 | 2015-07-10 09:56:30 +0200 | [diff] [blame] | 362 | int c, i, j, d; |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 363 | int found; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 364 | |
| 365 | assert(node && node_type && xml && unres); |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 366 | |
| 367 | leaf->value_str = xml->content; |
| 368 | xml->content = NULL; |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 369 | |
Radek Krejci | e3c3314 | 2015-08-10 15:04:36 +0200 | [diff] [blame] | 370 | /* will be change in case of union */ |
| 371 | leaf->value_type = node_type->base; |
| 372 | |
Radek Krejci | e2cf7c1 | 2015-08-12 10:24:05 +0200 | [diff] [blame] | 373 | if ((options & LYD_OPT_FILTER) && !leaf->value_str) { |
| 374 | /* no value in filter (selection) node -> nothing more is needed */ |
| 375 | return EXIT_SUCCESS; |
| 376 | } |
| 377 | |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 378 | switch (node_type->base) { |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 379 | case LY_TYPE_BINARY: |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 380 | leaf->value.binary = leaf->value_str; |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 381 | |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 382 | if (node_type->info.binary.length |
| 383 | && 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] | 384 | return EXIT_FAILURE; |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 385 | } |
| 386 | break; |
| 387 | |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 388 | case LY_TYPE_BITS: |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 389 | /* locate bits structure with the bits definitions */ |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 390 | for (type = node_type; type->der->type.der; type = &type->der->type); |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 391 | |
| 392 | /* allocate the array of pointers to bits definition */ |
| 393 | leaf->value.bit = calloc(type->info.bits.count, sizeof *leaf->value.bit); |
| 394 | |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 395 | if (!leaf->value_str) { |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 396 | /* no bits set */ |
| 397 | break; |
| 398 | } |
| 399 | |
| 400 | c = 0; |
| 401 | i = 0; |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 402 | while (leaf->value_str[c]) { |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 403 | /* skip leading whitespaces */ |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 404 | while(isspace(leaf->value_str[c])) { |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 405 | c++; |
| 406 | } |
| 407 | |
| 408 | /* get the length of the bit identifier */ |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 409 | 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] | 410 | |
| 411 | /* go back to the beginning of the identifier */ |
| 412 | c = c - len; |
| 413 | |
| 414 | /* find bit definition, identifiers appear ordered by their posititon */ |
| 415 | for (found = 0; i < type->info.bits.count; i++) { |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 416 | 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] | 417 | && !type->info.bits.bit[i].name[len]) { |
| 418 | /* we have match, store the pointer */ |
| 419 | leaf->value.bit[i] = &type->info.bits.bit[i]; |
| 420 | |
| 421 | /* stop searching */ |
| 422 | i++; |
| 423 | found = 1; |
| 424 | break; |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | if (!found) { |
| 429 | /* referenced bit value does not exists */ |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 430 | if (log) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 431 | LOGVAL(LYE_INVAL, LOGLINE(xml), leaf->value_str, xml->name); |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 432 | } |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 433 | return EXIT_FAILURE; |
| 434 | } |
| 435 | |
| 436 | c = c + len; |
| 437 | } |
| 438 | |
| 439 | break; |
| 440 | |
Radek Krejci | b738464 | 2015-07-09 16:08:45 +0200 | [diff] [blame] | 441 | case LY_TYPE_BOOL: |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 442 | if (!strcmp(leaf->value_str, "true")) { |
Radek Krejci | b738464 | 2015-07-09 16:08:45 +0200 | [diff] [blame] | 443 | leaf->value.bool = 1; |
| 444 | } /* else false, so keep it zero */ |
| 445 | break; |
| 446 | |
Radek Krejci | 7511f40 | 2015-07-10 09:56:30 +0200 | [diff] [blame] | 447 | case LY_TYPE_DEC64: |
| 448 | /* locate dec64 structure with the fraction-digits value */ |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 449 | for (type = node_type; type->der->type.der; type = &type->der->type); |
Radek Krejci | 7511f40 | 2015-07-10 09:56:30 +0200 | [diff] [blame] | 450 | |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 451 | for (c = 0; isspace(leaf->value_str[c]); c++); |
| 452 | 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] | 453 | c = c - len; |
| 454 | if (len > DECSIZE) { |
| 455 | /* too long */ |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 456 | if (log) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 457 | LOGVAL(LYE_INVAL, LOGLINE(xml), leaf->value_str, xml->name); |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 458 | } |
Radek Krejci | 7511f40 | 2015-07-10 09:56:30 +0200 | [diff] [blame] | 459 | return EXIT_FAILURE; |
| 460 | } |
| 461 | |
| 462 | /* normalize the number */ |
| 463 | dec[0] = '\0'; |
| 464 | for (i = j = d = found = 0; i < DECSIZE; i++) { |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 465 | if (leaf->value_str[c + i] == '.') { |
Radek Krejci | 7511f40 | 2015-07-10 09:56:30 +0200 | [diff] [blame] | 466 | found = 1; |
| 467 | j = type->info.dec64.dig; |
| 468 | i--; |
| 469 | c++; |
| 470 | continue; |
| 471 | } |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 472 | if (leaf->value_str[c + i] == '\0') { |
Radek Krejci | 7511f40 | 2015-07-10 09:56:30 +0200 | [diff] [blame] | 473 | c--; |
| 474 | if (!found) { |
| 475 | j = type->info.dec64.dig; |
| 476 | found = 1; |
| 477 | } |
| 478 | if (!j) { |
| 479 | dec[i] = '\0'; |
| 480 | break; |
| 481 | } |
| 482 | d++; |
| 483 | if (d > DECSIZE - 2) { |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 484 | if (log) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 485 | LOGVAL(LYE_OORVAL, LOGLINE(xml), leaf->value_str, xml->name); |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 486 | } |
Radek Krejci | 7511f40 | 2015-07-10 09:56:30 +0200 | [diff] [blame] | 487 | return EXIT_FAILURE; |
| 488 | } |
| 489 | dec[i] = '0'; |
| 490 | } else { |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 491 | if (!isdigit(leaf->value_str[c + i])) { |
| 492 | if (i || leaf->value_str[c] != '-') { |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 493 | if (log) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 494 | LOGVAL(LYE_INVAL, LOGLINE(xml), leaf->value_str, xml->name); |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 495 | } |
Radek Krejci | 7511f40 | 2015-07-10 09:56:30 +0200 | [diff] [blame] | 496 | return EXIT_FAILURE; |
| 497 | } |
| 498 | } else { |
| 499 | d++; |
| 500 | } |
| 501 | if (d > DECSIZE - 2 || (found && !j)) { |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 502 | if (log) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 503 | LOGVAL(LYE_OORVAL, LOGLINE(xml), leaf->value_str, xml->name); |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 504 | } |
Radek Krejci | 7511f40 | 2015-07-10 09:56:30 +0200 | [diff] [blame] | 505 | return EXIT_FAILURE; |
| 506 | } |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 507 | dec[i] = leaf->value_str[c + i]; |
Radek Krejci | 7511f40 | 2015-07-10 09:56:30 +0200 | [diff] [blame] | 508 | } |
| 509 | if (j) { |
| 510 | j--; |
| 511 | } |
| 512 | } |
| 513 | |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 514 | if (parse_int(dec, xml, -9223372036854775807L - 1L, 9223372036854775807L, 10, &num, log) |
| 515 | || 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] | 516 | return EXIT_FAILURE; |
| 517 | } |
Michal Vasko | bdee69c | 2015-07-15 15:49:39 +0200 | [diff] [blame] | 518 | leaf->value.dec64 = num; |
Radek Krejci | 7511f40 | 2015-07-10 09:56:30 +0200 | [diff] [blame] | 519 | break; |
| 520 | |
Radek Krejci | bce7374 | 2015-07-10 12:46:06 +0200 | [diff] [blame] | 521 | case LY_TYPE_EMPTY: |
| 522 | /* just check that it is empty */ |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 523 | if (leaf->value_str && leaf->value_str[0]) { |
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 | bce7374 | 2015-07-10 12:46:06 +0200 | [diff] [blame] | 527 | return EXIT_FAILURE; |
| 528 | } |
| 529 | break; |
| 530 | |
Radek Krejci | 5b315a9 | 2015-07-10 13:18:45 +0200 | [diff] [blame] | 531 | case LY_TYPE_ENUM: |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 532 | if (!leaf->value_str) { |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 533 | if (log) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 534 | LOGVAL(LYE_INVAL, LOGLINE(xml), "", xml->name); |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 535 | } |
Radek Krejci | 5b315a9 | 2015-07-10 13:18:45 +0200 | [diff] [blame] | 536 | return EXIT_FAILURE; |
| 537 | } |
| 538 | |
| 539 | /* locate enums structure with the enumeration definitions */ |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 540 | for (type = node_type; type->der->type.der; type = &type->der->type); |
Radek Krejci | 5b315a9 | 2015-07-10 13:18:45 +0200 | [diff] [blame] | 541 | |
| 542 | /* find matching enumeration value */ |
| 543 | for (i = 0; i < type->info.enums.count; i++) { |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 544 | if (!strcmp(leaf->value_str, type->info.enums.enm[i].name)) { |
Radek Krejci | 5b315a9 | 2015-07-10 13:18:45 +0200 | [diff] [blame] | 545 | /* we have match, store pointer to the definition */ |
Radek Krejci | 1574a8d | 2015-08-03 14:16:52 +0200 | [diff] [blame] | 546 | leaf->value.enm = &type->info.enums.enm[i]; |
Radek Krejci | 5b315a9 | 2015-07-10 13:18:45 +0200 | [diff] [blame] | 547 | break; |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | if (!leaf->value.enm) { |
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_INVAL, LOGLINE(xml), leaf->value_str, xml->name); |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 554 | } |
Radek Krejci | 5b315a9 | 2015-07-10 13:18:45 +0200 | [diff] [blame] | 555 | return EXIT_FAILURE; |
| 556 | } |
| 557 | |
| 558 | break; |
| 559 | |
Radek Krejci | ac8aac6 | 2015-07-10 15:36:35 +0200 | [diff] [blame] | 560 | case LY_TYPE_IDENT: |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 561 | if ((strptr = strchr(leaf->value_str, ':'))) { |
| 562 | len = strptr - leaf->value_str; |
Radek Krejci | ac8aac6 | 2015-07-10 15:36:35 +0200 | [diff] [blame] | 563 | if (!len) { |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 564 | if (log) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 565 | LOGVAL(LYE_INVAL, LOGLINE(xml), leaf->value_str, xml->name); |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 566 | } |
Radek Krejci | ac8aac6 | 2015-07-10 15:36:35 +0200 | [diff] [blame] | 567 | return EXIT_FAILURE; |
| 568 | } |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 569 | strptr = strndup(leaf->value_str, len); |
Radek Krejci | ac8aac6 | 2015-07-10 15:36:35 +0200 | [diff] [blame] | 570 | } |
| 571 | ns = lyxml_get_ns(xml, strptr); |
| 572 | if (!ns) { |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 573 | if (log) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 574 | LOGVAL(LYE_INVAL, LOGLINE(xml), leaf->value_str, xml->name); |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 575 | } |
Radek Krejci | ac8aac6 | 2015-07-10 15:36:35 +0200 | [diff] [blame] | 576 | return EXIT_FAILURE; |
| 577 | } |
| 578 | if (strptr) { |
| 579 | free(strptr); |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 580 | name = leaf->value_str + len + 1; |
Radek Krejci | ac8aac6 | 2015-07-10 15:36:35 +0200 | [diff] [blame] | 581 | } else { |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 582 | name = leaf->value_str; |
Radek Krejci | ac8aac6 | 2015-07-10 15:36:35 +0200 | [diff] [blame] | 583 | } |
| 584 | |
Michal Vasko | c3d9f8c | 2015-07-31 14:37:24 +0200 | [diff] [blame] | 585 | 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] | 586 | if (!leaf->value.ident) { |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 587 | if (log) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 588 | LOGVAL(LYE_INVAL, LOGLINE(xml), leaf->value_str, xml->name); |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 589 | } |
Radek Krejci | ac8aac6 | 2015-07-10 15:36:35 +0200 | [diff] [blame] | 590 | return EXIT_FAILURE; |
| 591 | } |
| 592 | break; |
| 593 | |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 594 | case LY_TYPE_INST: |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 595 | if (!leaf->value_str) { |
| 596 | if (log) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 597 | LOGVAL(LYE_INVAL, LOGLINE(xml), "", xml->name); |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 598 | } |
| 599 | return EXIT_FAILURE; |
| 600 | } |
| 601 | |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 602 | /* convert the path from the XML form using XML namespaces into the JSON format |
| 603 | * using module names as namespaces |
| 604 | */ |
| 605 | xml->content = leaf->value_str; |
Michal Vasko | 083a433 | 2015-08-19 11:11:43 +0200 | [diff] [blame] | 606 | leaf->value_str = transform_instid_xml2json(node->schema->module->ctx, xml); |
Radek Krejci | c5090c3 | 2015-08-12 09:46:19 +0200 | [diff] [blame] | 607 | lydict_remove(node->schema->module->ctx, xml->content); |
| 608 | xml->content = NULL; |
| 609 | if (!leaf->value_str) { |
| 610 | return EXIT_FAILURE; |
| 611 | } |
| 612 | |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 613 | if (options & (LYD_OPT_EDIT | LYD_OPT_FILTER)) { |
| 614 | leaf->value_type |= LY_TYPE_INST_UNRES; |
| 615 | } else { |
| 616 | /* validity checking is performed later, right now the data tree |
| 617 | * is not complete, so many instanceids cannot be resolved |
| 618 | */ |
| 619 | /* remember the leaf for later checking */ |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame^] | 620 | if (unres_data_add(unres, node, (log ? LOGLINE(xml) : UINT_MAX))) { |
| 621 | return EXIT_FAILURE; |
| 622 | } |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 623 | } |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 624 | break; |
| 625 | |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 626 | case LY_TYPE_LEAFREF: |
| 627 | if (!leaf->value_str) { |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 628 | if (log) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 629 | LOGVAL(LYE_INVAL, LOGLINE(xml), "", xml->name); |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 630 | } |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 631 | return EXIT_FAILURE; |
| 632 | } |
| 633 | |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 634 | if (options & (LYD_OPT_EDIT | LYD_OPT_FILTER)) { |
| 635 | do { |
| 636 | type = &((struct lys_node_leaf *)leaf->schema)->type.info.lref.target->type; |
| 637 | } while (type->base == LY_TYPE_LEAFREF); |
| 638 | leaf->value_type = type->base | LY_TYPE_LEAFREF_UNRES; |
| 639 | } else { |
| 640 | /* validity checking is performed later, right now the data tree |
| 641 | * is not complete, so many leafrefs cannot be resolved |
| 642 | */ |
| 643 | /* remember the leaf for later checking */ |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame^] | 644 | if (unres_data_add(unres, node, (log ? LOGLINE(xml) : UINT_MAX))) { |
| 645 | return EXIT_FAILURE; |
| 646 | } |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 647 | } |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 648 | break; |
| 649 | |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 650 | case LY_TYPE_STRING: |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 651 | leaf->value.string = leaf->value_str; |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 652 | |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 653 | if (node_type->info.str.length |
| 654 | && 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] | 655 | return EXIT_FAILURE; |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 656 | } |
| 657 | |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 658 | if (node_type->info.str.patterns |
| 659 | && validate_pattern(leaf->value.string, node_type, xml, leaf->value_str, log)) { |
Michal Vasko | 0e60c83 | 2015-07-15 15:48:35 +0200 | [diff] [blame] | 660 | return EXIT_FAILURE; |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 661 | } |
| 662 | break; |
| 663 | |
Michal Vasko | 5811016 | 2015-07-15 15:50:16 +0200 | [diff] [blame] | 664 | case LY_TYPE_UNION: |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 665 | found = 0; |
Michal Vasko | 6da1a37 | 2015-07-27 11:19:10 +0200 | [diff] [blame] | 666 | type = get_next_union_type(node_type, NULL, &found); |
| 667 | 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] | 668 | xml->content = leaf->value_str; |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 669 | if (!_xml_get_value(node, type, xml, options, unres, 0)) { |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 670 | leaf->value_type = type->base; |
| 671 | break; |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | if (!type) { |
| 676 | if (log) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 677 | LOGVAL(LYE_INVAL, LOGLINE(xml), leaf->value_str, xml->name); |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 678 | } |
| 679 | return EXIT_FAILURE; |
| 680 | } |
Michal Vasko | 5811016 | 2015-07-15 15:50:16 +0200 | [diff] [blame] | 681 | break; |
| 682 | |
| 683 | case LY_TYPE_INT8: |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 684 | if (parse_int(leaf->value_str, xml, -128, 127, 0, &num, log) |
| 685 | || 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] | 686 | return EXIT_FAILURE; |
| 687 | } |
| 688 | leaf->value.int8 = num; |
| 689 | break; |
| 690 | |
| 691 | case LY_TYPE_INT16: |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 692 | if (parse_int(leaf->value_str, xml, -32768, 32767, 0, &num, log) |
| 693 | || 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] | 694 | return EXIT_FAILURE; |
| 695 | } |
| 696 | leaf->value.int16 = num; |
| 697 | break; |
| 698 | |
| 699 | case LY_TYPE_INT32: |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 700 | if (parse_int(leaf->value_str, xml, -2147483648, 2147483647, 0, &num, log) |
| 701 | || 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] | 702 | return EXIT_FAILURE; |
| 703 | } |
| 704 | leaf->value.int32 = num; |
| 705 | break; |
| 706 | |
| 707 | case LY_TYPE_INT64: |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 708 | if (parse_int(leaf->value_str, xml, -9223372036854775807L - 1L, 9223372036854775807L, 0, &num, log) |
| 709 | || 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] | 710 | return EXIT_FAILURE; |
| 711 | } |
| 712 | leaf->value.int64 = num; |
| 713 | break; |
| 714 | |
| 715 | case LY_TYPE_UINT8: |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 716 | if (parse_uint(leaf->value_str, xml, 255, 0, &unum, log) |
| 717 | || 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] | 718 | return EXIT_FAILURE; |
| 719 | } |
| 720 | leaf->value.uint8 = unum; |
| 721 | break; |
| 722 | |
| 723 | case LY_TYPE_UINT16: |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 724 | if (parse_uint(leaf->value_str, xml, 65535, 0, &unum, log) |
| 725 | || 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] | 726 | return EXIT_FAILURE; |
| 727 | } |
| 728 | leaf->value.uint16 = unum; |
| 729 | break; |
| 730 | |
| 731 | case LY_TYPE_UINT32: |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 732 | if (parse_uint(leaf->value_str, xml, 4294967295, 0, &unum, log) |
| 733 | || 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] | 734 | return EXIT_FAILURE; |
| 735 | } |
| 736 | leaf->value.uint32 = unum; |
| 737 | break; |
| 738 | |
| 739 | case LY_TYPE_UINT64: |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 740 | if (parse_uint(leaf->value_str, xml, 18446744073709551615UL, 0, &unum, log) |
| 741 | || 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] | 742 | return EXIT_FAILURE; |
| 743 | } |
| 744 | leaf->value.uint64 = unum; |
| 745 | break; |
| 746 | |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 747 | default: |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 748 | return EXIT_FAILURE; |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | return EXIT_SUCCESS; |
| 752 | } |
| 753 | |
Michal Vasko | 07471a5 | 2015-07-16 11:18:48 +0200 | [diff] [blame] | 754 | static int |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 755 | 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] | 756 | { |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 757 | 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] | 758 | } |
| 759 | |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 760 | struct lyd_node * |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 761 | xml_parse_data(struct ly_ctx *ctx, struct lyxml_elem *xml, struct lyd_node *parent, struct lyd_node *prev, |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 762 | int options, struct unres_data *unres) |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 763 | { |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 764 | struct lyd_node *result = NULL, *diter; |
| 765 | struct lys_node *schema = NULL, *siter; |
Michal Vasko | ab8e440 | 2015-07-17 12:54:28 +0200 | [diff] [blame] | 766 | int i, havechildren; |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 767 | |
| 768 | if (!xml) { |
| 769 | return NULL; |
| 770 | } |
| 771 | if (!xml->ns || !xml->ns->value) { |
Michal Vasko | e7fc19c | 2015-08-05 16:24:39 +0200 | [diff] [blame] | 772 | LOGVAL(LYE_XML_MISS, LOGLINE(xml), "element's", "namespace"); |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 773 | return NULL; |
| 774 | } |
| 775 | |
| 776 | /* find schema node */ |
| 777 | if (!parent) { |
| 778 | /* starting in root */ |
| 779 | for (i = 0; i < ctx->models.used; i++) { |
| 780 | /* match data model based on namespace */ |
| 781 | if (ctx->models.list[i]->ns == xml->ns->value) { |
| 782 | /* get the proper schema node */ |
| 783 | LY_TREE_FOR(ctx->models.list[i]->data, schema) { |
| 784 | if (schema->name == xml->name) { |
| 785 | break; |
| 786 | } |
| 787 | } |
| 788 | break; |
| 789 | } |
| 790 | } |
| 791 | } else { |
| 792 | /* parsing some internal node, we start with parent's schema pointer */ |
| 793 | schema = xml_data_search_schemanode(xml, parent->schema->child); |
| 794 | } |
| 795 | if (!schema) { |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 796 | if ((options & LYD_OPT_STRICT) || ly_ctx_get_module_by_ns(ctx, xml->ns->value, NULL)) { |
| 797 | LOGVAL(LYE_INELEM, LOGLINE(xml), xml->name); |
| 798 | return NULL; |
| 799 | } else { |
| 800 | goto siblings; |
| 801 | } |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 802 | } |
| 803 | |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 804 | switch (schema->nodetype) { |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 805 | case LYS_CONTAINER: |
Michal Vasko | ab8e440 | 2015-07-17 12:54:28 +0200 | [diff] [blame] | 806 | result = calloc(1, sizeof *result); |
| 807 | havechildren = 1; |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 808 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 809 | case LYS_LEAF: |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 810 | result = calloc(1, sizeof(struct lyd_node_leaf)); |
| 811 | havechildren = 0; |
| 812 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 813 | case LYS_LEAFLIST: |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 814 | result = calloc(1, sizeof(struct lyd_node_leaflist)); |
| 815 | havechildren = 0; |
| 816 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 817 | case LYS_LIST: |
Michal Vasko | ab8e440 | 2015-07-17 12:54:28 +0200 | [diff] [blame] | 818 | result = calloc(1, sizeof(struct lyd_node_list)); |
| 819 | havechildren = 1; |
| 820 | break; |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 821 | case LYS_ANYXML: |
Michal Vasko | ab8e440 | 2015-07-17 12:54:28 +0200 | [diff] [blame] | 822 | result = calloc(1, sizeof(struct lyd_node_anyxml)); |
| 823 | havechildren = 0; |
| 824 | break; |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 825 | default: |
Michal Vasko | 0c888fd | 2015-08-11 15:54:08 +0200 | [diff] [blame] | 826 | LOGINT; |
Michal Vasko | ab8e440 | 2015-07-17 12:54:28 +0200 | [diff] [blame] | 827 | return NULL; |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 828 | } |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 829 | result->parent = parent; |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 830 | result->prev = prev; |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 831 | result->schema = schema; |
| 832 | |
Radek Krejci | 88daf82 | 2015-08-10 13:58:16 +0200 | [diff] [blame] | 833 | /* check number of instances for non-list nodes */ |
Radek Krejci | b677214 | 2015-08-12 10:16:44 +0200 | [diff] [blame] | 834 | 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] | 835 | for (diter = result->prev; diter; diter = diter->prev) { |
| 836 | if (diter->schema == schema) { |
Radek Krejci | 88daf82 | 2015-08-10 13:58:16 +0200 | [diff] [blame] | 837 | LOGVAL(LYE_TOOMANY, LOGLINE(xml), xml->name, xml->parent ? xml->parent->name : "data tree"); |
| 838 | goto error; |
| 839 | } |
| 840 | } |
| 841 | } |
| 842 | |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 843 | /* type specific processing */ |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 844 | if (schema->nodetype == LYS_LIST) { |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 845 | /* pointers to next and previous instances of the same list */ |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 846 | for (diter = result->prev; diter; diter = diter->prev) { |
| 847 | if (diter->schema == result->schema) { |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 848 | /* instances of the same list */ |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 849 | ((struct lyd_node_list *)diter)->lnext = (struct lyd_node_list *)result; |
| 850 | ((struct lyd_node_list *)result)->lprev = (struct lyd_node_list *)diter; |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 851 | break; |
| 852 | } |
| 853 | } |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 854 | } else if (schema->nodetype == LYS_LEAF) { |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 855 | /* type detection and assigning the value */ |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 856 | if (xml_get_value(result, xml, options, unres)) { |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 857 | goto error; |
| 858 | } |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 859 | } else if (schema->nodetype == LYS_LEAFLIST) { |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 860 | /* type detection and assigning the value */ |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 861 | if (xml_get_value(result, xml, options, unres)) { |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 862 | goto error; |
| 863 | } |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 864 | |
| 865 | /* pointers to next and previous instances of the same leaflist */ |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 866 | for (diter = result->prev; diter; diter = diter->prev) { |
| 867 | if (diter->schema == result->schema) { |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 868 | /* instances of the same list */ |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 869 | ((struct lyd_node_leaflist *)diter)->lnext = (struct lyd_node_leaflist *)result; |
| 870 | ((struct lyd_node_leaflist *)result)->lprev = (struct lyd_node_leaflist *)diter; |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 871 | break; |
| 872 | } |
| 873 | } |
Radek Krejci | 7651257 | 2015-08-04 09:47:08 +0200 | [diff] [blame] | 874 | } else if (schema->nodetype == LYS_ANYXML) { |
Michal Vasko | ab8e440 | 2015-07-17 12:54:28 +0200 | [diff] [blame] | 875 | ((struct lyd_node_anyxml *)result)->value = xml; |
| 876 | lyxml_unlink_elem(xml); |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 877 | } |
| 878 | |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 879 | /* process children */ |
Radek Krejci | e474847 | 2015-07-08 18:00:22 +0200 | [diff] [blame] | 880 | if (havechildren && xml->child) { |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 881 | result->child = xml_parse_data(ctx, xml->child, result, NULL, options, unres); |
| 882 | if (ly_errno) { |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 883 | goto error; |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 884 | } |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 885 | } |
| 886 | |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 887 | /* various validation checks */ |
Radek Krejci | 1073cc0 | 2015-08-12 20:37:39 +0200 | [diff] [blame] | 888 | |
| 889 | /* check presence of all keys in case of list */ |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 890 | if (schema->nodetype == LYS_LIST && !(options & LYD_OPT_FILTER)) { |
Radek Krejci | 1073cc0 | 2015-08-12 20:37:39 +0200 | [diff] [blame] | 891 | siter = (struct lys_node *)lyv_keys_present((struct lyd_node_list *)result); |
| 892 | if (siter) { |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 893 | /* key not found in the data */ |
Radek Krejci | 1073cc0 | 2015-08-12 20:37:39 +0200 | [diff] [blame] | 894 | LOGVAL(LYE_MISSELEM, LOGLINE(xml), siter->name, schema->name); |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 895 | goto error; |
| 896 | } |
| 897 | } |
| 898 | |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 899 | /* mandatory children */ |
| 900 | if (havechildren && !(options & (LYD_OPT_FILTER | LYD_OPT_EDIT))) { |
| 901 | siter = ly_check_mandatory(result); |
| 902 | if (siter) { |
| 903 | LOGVAL(LYE_MISSELEM, LOGLINE(xml), siter->name, siter->parent->name); |
| 904 | goto error; |
| 905 | } |
| 906 | } |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 907 | |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 908 | siblings: |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 909 | /* process siblings */ |
| 910 | if (xml->next) { |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 911 | if (result) { |
| 912 | result->next = xml_parse_data(ctx, xml->next, parent, result, options, unres); |
| 913 | } else { |
| 914 | result = xml_parse_data(ctx, xml->next, parent, prev, options, unres); |
| 915 | } |
| 916 | if (ly_errno) { |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 917 | goto error; |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 918 | } |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 919 | } |
| 920 | |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 921 | /* fix the "last" pointer */ |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 922 | if (result && !result->prev) { |
Radek Krejci | 7f40ce3 | 2015-08-12 20:38:46 +0200 | [diff] [blame] | 923 | for (diter = result; diter->next; diter = diter->next); |
| 924 | result->prev = diter; |
Radek Krejci | b993025 | 2015-07-08 15:47:45 +0200 | [diff] [blame] | 925 | } |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 926 | return result; |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 927 | |
| 928 | error: |
| 929 | |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 930 | if (result) { |
| 931 | result->next = NULL; |
| 932 | result->parent = NULL; |
| 933 | result->prev = result; |
| 934 | lyd_free(result); |
Radek Krejci | 7511f40 | 2015-07-10 09:56:30 +0200 | [diff] [blame] | 935 | } |
Radek Krejci | 3e3affe | 2015-07-09 15:38:40 +0200 | [diff] [blame] | 936 | |
| 937 | return NULL; |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 938 | } |
| 939 | |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 940 | struct lyd_node * |
| 941 | xml_read_data(struct ly_ctx *ctx, const char *data, int options) |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 942 | { |
| 943 | struct lyxml_elem *xml; |
Radek Krejci | 0e1d1a6 | 2015-07-31 11:17:01 +0200 | [diff] [blame] | 944 | struct lyd_node *result, *next, *iter; |
Michal Vasko | f02e374 | 2015-08-05 16:27:02 +0200 | [diff] [blame] | 945 | struct unres_data *unres = NULL; |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 946 | |
| 947 | xml = lyxml_read(ctx, data, 0); |
| 948 | if (!xml) { |
| 949 | return NULL; |
| 950 | } |
| 951 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 952 | unres = calloc(1, sizeof *unres); |
| 953 | |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 954 | ly_errno = 0; |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 955 | result = xml_parse_data(ctx, xml->child, NULL, NULL, options, unres); |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 956 | /* check leafrefs and/or instids if any */ |
Michal Vasko | 8bcdf29 | 2015-08-19 14:04:43 +0200 | [diff] [blame^] | 957 | if (resolve_unres_data(unres)) { |
Michal Vasko | 493bea7 | 2015-07-16 16:08:12 +0200 | [diff] [blame] | 958 | /* leafref & instid checking failed */ |
Radek Krejci | 0e1d1a6 | 2015-07-31 11:17:01 +0200 | [diff] [blame] | 959 | LY_TREE_FOR_SAFE(result, next, iter) { |
| 960 | lyd_free(iter); |
| 961 | } |
| 962 | result = NULL; |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 963 | } |
| 964 | |
Michal Vasko | 23b61ec | 2015-08-19 11:19:50 +0200 | [diff] [blame] | 965 | free(unres->dnode); |
| 966 | #ifndef NDEBUG |
| 967 | free(unres->line); |
| 968 | #endif |
| 969 | free(unres); |
| 970 | |
Radek Krejci | 5a98815 | 2015-07-15 11:16:26 +0200 | [diff] [blame] | 971 | /* free source XML tree */ |
Radek Krejci | 1721c01 | 2015-07-08 12:52:33 +0200 | [diff] [blame] | 972 | lyxml_free_elem(ctx, xml); |
| 973 | |
| 974 | return result; |
| 975 | } |