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