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