blob: bc385983459da16a5182c2e1adab6d8527bed1a4 [file] [log] [blame]
Radek Krejcie7b95092019-05-15 11:03:07 +02001/**
2 * @file parser_xml.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief XML data parser for libyang
5 *
6 * Copyright (c) 2019 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#include "common.h"
16
17#include <stdint.h>
18#include <stdlib.h>
19#include <string.h>
Michal Vasko9b368d32020-02-14 13:53:31 +010020#include <assert.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020021
22#include "context.h"
23#include "dict.h"
24#include "log.h"
25#include "plugins_types.h"
26#include "set.h"
27#include "tree_data.h"
28#include "tree_data_internal.h"
29#include "tree_schema.h"
30#include "xml.h"
Michal Vaskocde73ac2019-11-14 16:10:27 +010031#include "validation.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020032
33/**
34 * @brief internal context for XML YANG data parser.
35 *
36 * The leading part is compatible with the struct lyxml_context
37 */
38struct lyd_xml_ctx {
39 struct ly_ctx *ctx; /**< libyang context */
40 uint64_t line; /**< number of the line being currently processed */
41 enum LYXML_PARSER_STATUS status; /**< status providing information about the next expected object in input data */
42 struct ly_set elements; /**< list of not-yet-closed elements */
43 struct ly_set ns; /**< handled with LY_SET_OPT_USEASLIST */
44
45 uint16_t options; /**< various @ref dataparseroptions. */
46 uint16_t path_len; /**< used bytes in the path buffer */
47#define LYD_PARSER_BUFSIZE 4078
48 char path[LYD_PARSER_BUFSIZE]; /**< buffer for the generated path */
Radek Krejcie553e6d2019-06-07 15:33:18 +020049 struct ly_set incomplete_type_validation; /**< set of nodes validated with LY_EINCOMPLETE result */
Radek Krejci38d85362019-09-05 16:26:38 +020050 struct ly_set incomplete_type_validation_attrs; /**< set of attributes validated with LY_EINCOMPLETE result */
Michal Vaskoecd62de2019-11-13 12:35:11 +010051 struct ly_set when_check; /**< set of nodes with "when" conditions */
Radek Krejcie7b95092019-05-15 11:03:07 +020052};
53
54/**
Radek Krejciaca74032019-06-04 08:53:06 +020055 * @brief XML-parser's implementation of ly_type_resolve_prefix() callback to provide mapping between prefixes used in the values to the schema
56 * via XML namespaces.
57 */
58static const struct lys_module *
59lydxml_resolve_prefix(struct ly_ctx *ctx, const char *prefix, size_t prefix_len, void *parser)
60{
61 const struct lyxml_ns *ns;
62 struct lyxml_context *xmlctx = (struct lyxml_context*)parser;
63
64 ns = lyxml_ns_get(xmlctx, prefix, prefix_len);
65 if (!ns) {
66 return NULL;
67 }
68
69 return ly_ctx_get_module_implemented_ns(ctx, ns->uri);
70}
71
Radek Krejci28681fa2019-09-06 13:08:45 +020072struct attr_data_s {
73 const char *prefix;
74 const char *name;
75 char *value;
76 size_t prefix_len;
77 size_t name_len;
78 size_t value_len;
79 int dynamic;
80};
81
Radek Krejciaca74032019-06-04 08:53:06 +020082/**
Radek Krejcie7b95092019-05-15 11:03:07 +020083 * @brief Parse XML attributes of the XML element of YANG data.
84 *
85 * @param[in] ctx XML YANG data parser context.
Radek Krejcie7b95092019-05-15 11:03:07 +020086 * @param[in,out] data Pointer to the XML string representation of the YANG data to parse.
87 * @param[out] attributes Resulting list of the parsed attributes. XML namespace definitions are not parsed
88 * as attributes, they are stored internally in the parser context.
89 * @reutn LY_ERR value.
90 */
91static LY_ERR
Radek Krejci28681fa2019-09-06 13:08:45 +020092lydxml_attributes_parse(struct lyd_xml_ctx *ctx, const char **data, struct ly_set *attrs_data)
Radek Krejcie7b95092019-05-15 11:03:07 +020093{
94 LY_ERR ret = LY_SUCCESS;
Radek Krejci28681fa2019-09-06 13:08:45 +020095 unsigned int u;
Radek Krejcie7b95092019-05-15 11:03:07 +020096 const char *prefix, *name;
97 size_t prefix_len, name_len;
Radek Krejci28681fa2019-09-06 13:08:45 +020098 struct attr_data_s *attr_data;
Radek Krejcie7b95092019-05-15 11:03:07 +020099
100 while(ctx->status == LYXML_ATTRIBUTE &&
101 lyxml_get_attribute((struct lyxml_context*)ctx, data, &prefix, &prefix_len, &name, &name_len) == LY_SUCCESS) {
Radek Krejci38d85362019-09-05 16:26:38 +0200102 char *buffer = NULL;
103 size_t buffer_size = 0;
Radek Krejcie7b95092019-05-15 11:03:07 +0200104
Radek Krejci17a78d82019-05-15 15:49:55 +0200105 if (!name) {
106 /* seems like all the attrributes were internally processed as namespace definitions */
107 continue;
Radek Krejcie7b95092019-05-15 11:03:07 +0200108 }
Radek Krejci17a78d82019-05-15 15:49:55 +0200109
Radek Krejci38d85362019-09-05 16:26:38 +0200110 /* auxiliary store the prefix information and value string, because we have to wait with resolving prefix
111 * to the time when all the namespaces, defined in this element, are parsed. With the prefix we can find the
112 * annotation definition for the attribute and correctly process the value */
113 attr_data = malloc(sizeof *attr_data);
114 attr_data->prefix = prefix;
Radek Krejci28681fa2019-09-06 13:08:45 +0200115 attr_data->name = name;
Radek Krejci38d85362019-09-05 16:26:38 +0200116 attr_data->prefix_len = prefix_len;
Radek Krejci28681fa2019-09-06 13:08:45 +0200117 attr_data->name_len = name_len;
Radek Krejci38d85362019-09-05 16:26:38 +0200118 ret = lyxml_get_string((struct lyxml_context *)ctx, data, &buffer, &buffer_size, &attr_data->value, &attr_data->value_len, &attr_data->dynamic);
Radek Krejci28681fa2019-09-06 13:08:45 +0200119 LY_CHECK_ERR_GOTO(ret, free(attr_data), error);
120 ly_set_add(attrs_data, attr_data, LY_SET_OPT_USEASLIST);
121 }
122
123 return LY_SUCCESS;
124
125error:
126 for (u = 0; u < attrs_data->count; ++u) {
127 if (((struct attr_data_s*)attrs_data->objs[u])->dynamic) {
128 free(((struct attr_data_s*)attrs_data->objs[u])->value);
129 }
130 }
131 ly_set_erase(attrs_data, free);
132 return ret;
133}
134
135static LY_ERR
136lydxml_attributes(struct lyd_xml_ctx *ctx, struct ly_set *attrs_data, struct lyd_node *parent)
137{
138 LY_ERR ret = LY_EVALID, rc;
Michal Vasko90932a92020-02-12 14:33:03 +0100139 struct lyd_attr *attr = NULL;
Radek Krejci28681fa2019-09-06 13:08:45 +0200140 const struct lyxml_ns *ns;
141 struct lys_module *mod;
142
143 for (unsigned int u = 0; u < attrs_data->count; ++u) {
Radek Krejci28681fa2019-09-06 13:08:45 +0200144 struct attr_data_s *attr_data = (struct attr_data_s*)attrs_data->objs[u];
145
146 if (!attr_data->prefix_len) {
147 /* in XML, all attributes must be prefixed
148 * TODO exception for NETCONF filters which are supposed to map to the ietf-netconf without prefix */
149 if (ctx->options & LYD_OPT_STRICT) {
150 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE, "Missing mandatory prefix for XML attribute \"%.*s\".",
151 attr_data->name_len, attr_data->name);
152 }
153skip_attr:
154 if (attr_data->dynamic) {
155 free(attr_data->value);
156 attr_data->dynamic = 0;
157 }
158 continue;
159 }
160
161 /* get namespace of the attribute to find its annotation definition */
162 ns = lyxml_ns_get((struct lyxml_context *)ctx, attr_data->prefix, attr_data->prefix_len);
163 if (!ns) {
164 /* unknown namespace, ignore the attribute */
165 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", attr_data->prefix_len, attr_data->prefix);
166 goto cleanup;
167 }
168 mod = ly_ctx_get_module_implemented_ns(ctx->ctx, ns->uri);
169 if (!mod) {
170 /* module is not implemented or not present in the schema */
171 if (ctx->options & LYD_OPT_STRICT) {
172 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE,
173 "Unknown (or not implemented) YANG module with namespace \"%s\" for attribute \"%.*s%s%.*s\".",
Michal Vasko90932a92020-02-12 14:33:03 +0100174 ns, attr_data->prefix_len, attr_data->prefix, attr_data->prefix_len ? ":" : "", attr_data->name_len,
175 attr_data->name);
Radek Krejci28681fa2019-09-06 13:08:45 +0200176 }
177 goto skip_attr;
178 }
179
Michal Vasko90932a92020-02-12 14:33:03 +0100180 rc = lyd_create_attr(parent, mod, attr_data->name, attr_data->name_len, attr_data->value, attr_data->value_len,
181 &attr_data->dynamic, lydxml_resolve_prefix, ctx, LYD_XML, &attr);
Radek Krejci28681fa2019-09-06 13:08:45 +0200182 if (rc == LY_EINCOMPLETE) {
183 ly_set_add(&ctx->incomplete_type_validation_attrs, attr, LY_SET_OPT_USEASLIST);
184 } else if (rc) {
185 ret = rc;
Radek Krejci28681fa2019-09-06 13:08:45 +0200186 goto cleanup;
187 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200188 }
Radek Krejci28681fa2019-09-06 13:08:45 +0200189 ret = LY_SUCCESS;
Radek Krejcie7b95092019-05-15 11:03:07 +0200190
191cleanup:
192
Radek Krejci28681fa2019-09-06 13:08:45 +0200193 for (unsigned int u = 0; u < attrs_data->count; ++u) {
194 if (((struct attr_data_s*)attrs_data->objs[u])->dynamic) {
195 free(((struct attr_data_s*)attrs_data->objs[u])->value);
Radek Krejci38d85362019-09-05 16:26:38 +0200196 }
197 }
Radek Krejci28681fa2019-09-06 13:08:45 +0200198 ly_set_erase(attrs_data, free);
199
Radek Krejcie7b95092019-05-15 11:03:07 +0200200 return ret;
201}
202
203/**
204 * @brief Parse XML elements as children YANG data node of the specified parent node.
205 *
206 * @param[in] ctx XML YANG data parser context.
207 * @param[in] parent Parent node where the children are inserted. NULL in case of parsing top-level elements.
208 * @param[in,out] data Pointer to the XML string representation of the YANG data to parse.
209 * @param[out] node Resulting list of the parsed nodes.
Michal Vasko9b368d32020-02-14 13:53:31 +0100210 * @return LY_ERR value.
Radek Krejcie7b95092019-05-15 11:03:07 +0200211 */
212static LY_ERR
213lydxml_nodes(struct lyd_xml_ctx *ctx, struct lyd_node_inner *parent, const char **data, struct lyd_node **node)
214{
215 LY_ERR ret = LY_SUCCESS;
216 const char *prefix, *name;
Radek Krejcie7b95092019-05-15 11:03:07 +0200217 size_t prefix_len, name_len;
Radek Krejci28681fa2019-09-06 13:08:45 +0200218 struct ly_set attrs_data = {0};
Radek Krejcie7b95092019-05-15 11:03:07 +0200219 const struct lyxml_ns *ns;
220 const struct lysc_node *snode;
221 struct lys_module *mod;
222 unsigned int parents_count = ctx->elements.count;
Michal Vasko90932a92020-02-12 14:33:03 +0100223 struct lyd_node *cur = NULL, *key_anchor;
224 int dynamic = 0;
225 char *buffer = NULL, *value;
226 size_t buffer_size = 0, value_len;
Radek Krejcie7b95092019-05-15 11:03:07 +0200227
Michal Vasko14654712020-02-06 08:35:21 +0100228 while (ctx->status == LYXML_ELEMENT) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200229 ret = lyxml_get_element((struct lyxml_context *)ctx, data, &prefix, &prefix_len, &name, &name_len);
230 LY_CHECK_GOTO(ret, cleanup);
231 if (!name) {
232 /* closing previous element */
Radek Krejcie7b95092019-05-15 11:03:07 +0200233 if (ctx->elements.count < parents_count) {
234 /* all siblings parsed */
235 break;
236 } else {
237 continue;
238 }
239 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200240 if (ctx->status == LYXML_ATTRIBUTE) {
Michal Vasko90932a92020-02-12 14:33:03 +0100241 if (lydxml_attributes_parse(ctx, data, &attrs_data) != LY_SUCCESS) {
242 ret = LY_EVALID;
243 goto cleanup;
244 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200245 }
246
Radek Krejcie7b95092019-05-15 11:03:07 +0200247 ns = lyxml_ns_get((struct lyxml_context *)ctx, prefix, prefix_len);
248 if (!ns) {
Radek Krejci28681fa2019-09-06 13:08:45 +0200249 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", prefix_len, prefix);
Michal Vasko90932a92020-02-12 14:33:03 +0100250 ret = LY_EVALID;
251 goto cleanup;
Radek Krejcie7b95092019-05-15 11:03:07 +0200252 }
253 mod = ly_ctx_get_module_implemented_ns(ctx->ctx, ns->uri);
254 if (!mod) {
255 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE, "No module with namespace \"%s\" in the context.", ns->uri);
Michal Vasko90932a92020-02-12 14:33:03 +0100256 ret = LY_EVALID;
257 goto cleanup;
Radek Krejcie7b95092019-05-15 11:03:07 +0200258 }
Michal Vaskoe444f752020-02-10 12:20:06 +0100259 snode = lys_find_child(parent ? parent->schema : NULL, mod, name, name_len, 0, 0);
Radek Krejcie7b95092019-05-15 11:03:07 +0200260 if (!snode) {
Michal Vasko90932a92020-02-12 14:33:03 +0100261 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE, "Element \"%.*s\" not found in the \"%s\" module.",
262 name_len, name, mod->name);
263 ret = LY_EVALID;
Radek Krejcie7b95092019-05-15 11:03:07 +0200264 goto cleanup;
265 }
Radek Krejci710226d2019-07-24 17:24:59 +0200266
Michal Vasko90932a92020-02-12 14:33:03 +0100267 if (snode->nodetype & (LYS_ACTION | LYS_NOTIF)) {
268 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_DATA, "Unexpected %s element \"%.*s\".",
269 snode->nodetype == LYS_ACTION ? "RPC/action" : "notification", name_len, name);
270 ret = LY_EVALID;
271 goto cleanup;
Radek Krejcie7b95092019-05-15 11:03:07 +0200272 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200273
274 if (snode->nodetype & LYD_NODE_TERM) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200275 if (ctx->status == LYXML_ELEM_CONTENT) {
276 /* get the value */
Radek Krejci11702142019-07-24 17:46:04 +0200277 ret = lyxml_get_string((struct lyxml_context *)ctx, data, &buffer, &buffer_size, &value, &value_len, &dynamic);
Michal Vasko14654712020-02-06 08:35:21 +0100278 if (ret != LY_SUCCESS) {
279 if (ret == LY_EINVAL) {
280 /* just indentation of a child element found */
Michal Vasko90932a92020-02-12 14:33:03 +0100281 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX, "Child element inside terminal node \"%s\" found.",
282 snode->name);
Michal Vasko14654712020-02-06 08:35:21 +0100283 }
Radek Krejci339e2de2019-05-17 14:28:24 +0200284 goto cleanup;
285 }
Radek Krejcie92210c2019-05-17 15:53:35 +0200286 } else {
287 /* no content - validate empty value */
288 value = "";
289 value_len = 0;
Michal Vasko90932a92020-02-12 14:33:03 +0100290 dynamic = 0;
Radek Krejcie7b95092019-05-15 11:03:07 +0200291 }
Michal Vasko90932a92020-02-12 14:33:03 +0100292
293 /* create node */
294 ret = lyd_create_term(snode, value, value_len, &dynamic, lydxml_resolve_prefix, ctx, LYD_XML, &cur);
295 /* buffer spent */
296 buffer = NULL;
Radek Krejcie553e6d2019-06-07 15:33:18 +0200297 if (ret == LY_EINCOMPLETE) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100298 if (!(ctx->options & LYD_OPT_PARSE_ONLY)) {
299 ly_set_add(&ctx->incomplete_type_validation, cur, LY_SET_OPT_USEASLIST);
300 }
Radek Krejcie553e6d2019-06-07 15:33:18 +0200301 } else if (ret) {
Radek Krejcie553e6d2019-06-07 15:33:18 +0200302 goto cleanup;
303 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100304
305 if (parent && (cur->schema->flags & LYS_KEY)) {
306 /* check the key order, the anchor must always be the last child */
307 key_anchor = lyd_get_prev_key_anchor(parent->child, cur->schema);
308 if ((!key_anchor && parent->child) || (key_anchor && key_anchor->next)) {
309 if (ctx->options & LYD_OPT_STRICT) {
310 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_DATA, "Invalid position of the key \"%s\" in a list.",
311 cur->schema->name);
312 ret = LY_EVALID;
313 goto cleanup;
314 } else {
315 LOGWRN(ctx->ctx, "Invalid position of the key \"%s\" in a list.", cur->schema->name);
316 }
317 }
318 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200319 } else if (snode->nodetype & LYD_NODE_INNER) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200320 if (ctx->status == LYXML_ELEM_CONTENT) {
321 LY_ERR r = lyxml_get_string((struct lyxml_context *)ctx, data, &buffer, &buffer_size, &value, &value_len, &dynamic);
Radek Krejcie553e6d2019-06-07 15:33:18 +0200322 if (r != LY_EINVAL && (r != LY_SUCCESS || value_len != 0)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200323 LOGINT(ctx->ctx);
Radek Krejci11702142019-07-24 17:46:04 +0200324 ret = LY_EINT;
Radek Krejcie7b95092019-05-15 11:03:07 +0200325 goto cleanup;
326 }
327 }
Michal Vasko90932a92020-02-12 14:33:03 +0100328
329 /* create node */
330 ret = lyd_create_inner(snode, &cur);
331 LY_CHECK_GOTO(ret, cleanup);
332
Radek Krejciee4cab22019-07-17 17:07:47 +0200333 /* process children */
Radek Krejcie553e6d2019-06-07 15:33:18 +0200334 if (ctx->status == LYXML_ELEMENT && parents_count != ctx->elements.count) {
Michal Vasko14654712020-02-06 08:35:21 +0100335 ret = lydxml_nodes(ctx, (struct lyd_node_inner *)cur, data, lyd_node_children_p(cur));
Radek Krejcie92210c2019-05-17 15:53:35 +0200336 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcie7b95092019-05-15 11:03:07 +0200337 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100338
339 if (!(ctx->options & LYD_OPT_PARSE_ONLY)) {
340 /* add any missing default children */
341 ret = lyd_validate_defaults_r((struct lyd_node_inner *)cur, lyd_node_children_p(cur), cur->schema, NULL,
342 &ctx->incomplete_type_validation, &ctx->when_check);
343 LY_CHECK_GOTO(ret, cleanup);
344 }
345
346 /* hash now that all keys should be parsed, rehash for key-less list */
347 if (snode->nodetype == LYS_LIST) {
348 lyd_hash(cur);
349 }
Radek Krejciee4cab22019-07-17 17:07:47 +0200350 } else if (snode->nodetype & LYD_NODE_ANY) {
351 unsigned int cur_element_index = ctx->elements.count;
352 const char *start = *data, *stop;
353 const char *p, *n;
354 size_t p_len, n_len;
355
356 /* skip children data and store them as a string */
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200357 while (ctx->status != LYXML_END && cur_element_index <= ctx->elements.count) {
Radek Krejciee4cab22019-07-17 17:07:47 +0200358 switch (ctx->status) {
359 case LYXML_ELEMENT:
360 ret = lyxml_get_element((struct lyxml_context *)ctx, data, &p, &p_len, &n, &n_len);
Michal Vasko90932a92020-02-12 14:33:03 +0100361 LY_CHECK_GOTO(ret, cleanup);
Radek Krejciee4cab22019-07-17 17:07:47 +0200362 break;
363 case LYXML_ATTRIBUTE:
Michal Vasko14654712020-02-06 08:35:21 +0100364 lyxml_get_attribute((struct lyxml_context *)ctx, data, &p, &p_len, &n, &n_len);
Radek Krejciee4cab22019-07-17 17:07:47 +0200365 break;
366 case LYXML_ELEM_CONTENT:
367 case LYXML_ATTR_CONTENT:
368 ret = lyxml_get_string((struct lyxml_context *)ctx, data, NULL, NULL, NULL, NULL, NULL);
Michal Vasko90932a92020-02-12 14:33:03 +0100369 /* not an error, just incorrect XML parser status */
370 if (ret && (ret != LY_EINVAL)) {
371 goto cleanup;
Radek Krejciee4cab22019-07-17 17:07:47 +0200372 }
373 break;
374 case LYXML_END:
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200375 /* end of data */
Radek Krejciee4cab22019-07-17 17:07:47 +0200376 LOGINT(ctx->ctx);
Radek Krejci11702142019-07-24 17:46:04 +0200377 ret = LY_EINT;
Radek Krejciee4cab22019-07-17 17:07:47 +0200378 goto cleanup;
379 }
Radek Krejciee4cab22019-07-17 17:07:47 +0200380 }
Michal Vasko90932a92020-02-12 14:33:03 +0100381
382 /* get value */
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200383 if (start != *data) {
384 /* data now points after the anydata's closing element tag, we need just end of its content */
385 for (stop = *data - 1; *stop != '<'; --stop);
Michal Vasko90932a92020-02-12 14:33:03 +0100386 start = lydict_insert(ctx->ctx, start, stop - start);
387 } else {
388 start = NULL;
389 }
390
391 /* create node */
392 ret = lyd_create_any(snode, start, LYD_ANYDATA_XML, &cur);
393 if (ret) {
394 lydict_remove(ctx->ctx, start);
395 goto cleanup;
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200396 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200397 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200398
Michal Vasko90932a92020-02-12 14:33:03 +0100399 /* add attributes */
400 LY_CHECK_GOTO(ret = lydxml_attributes(ctx, &attrs_data, cur), cleanup);
401
Michal Vasko9b368d32020-02-14 13:53:31 +0100402 /* correct flags */
Michal Vaskocde73ac2019-11-14 16:10:27 +0100403 if (!(snode->nodetype & (LYS_ACTION | LYS_NOTIF)) && snode->when) {
Michal Vasko9b368d32020-02-14 13:53:31 +0100404 if (ctx->options & LYD_OPT_TRUSTED) {
405 /* just set it to true */
406 cur->flags |= LYD_WHEN_TRUE;
407 } else {
408 /* remember we need to evaluate this node's when */
409 ly_set_add(&ctx->when_check, cur, LY_SET_OPT_USEASLIST);
410 }
Michal Vaskocde73ac2019-11-14 16:10:27 +0100411 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100412 if (ctx->options & LYD_OPT_TRUSTED) {
413 /* node is valid */
414 cur->flags &= ~LYD_NEW;
415 }
Radek Krejcib6f7ae52019-07-19 10:31:42 +0200416
Michal Vasko90932a92020-02-12 14:33:03 +0100417 /* insert */
Michal Vasko9b368d32020-02-14 13:53:31 +0100418 lyd_insert_node((struct lyd_node *)parent, node, cur);
Michal Vasko90932a92020-02-12 14:33:03 +0100419
420 cur = NULL;
Radek Krejcie7b95092019-05-15 11:03:07 +0200421 }
422
Michal Vasko90932a92020-02-12 14:33:03 +0100423 /* success */
424 ret = LY_SUCCESS;
425
Radek Krejcie7b95092019-05-15 11:03:07 +0200426cleanup:
Michal Vasko90932a92020-02-12 14:33:03 +0100427 free(buffer);
428 lyd_free_tree(cur);
Radek Krejci28681fa2019-09-06 13:08:45 +0200429 for (unsigned int u = 0; u < attrs_data.count; ++u) {
430 if (((struct attr_data_s*)attrs_data.objs[u])->dynamic) {
431 free(((struct attr_data_s*)attrs_data.objs[u])->value);
432 }
433 }
434 ly_set_erase(&attrs_data, free);
Michal Vasko90932a92020-02-12 14:33:03 +0100435 if (ret && *node) {
436 lyd_free_withsiblings(*node);
437 *node = NULL;
438 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200439 return ret;
440}
441
442LY_ERR
Michal Vaskoa3881362020-01-21 15:57:35 +0100443lyd_parse_xml(struct ly_ctx *ctx, const char *data, int options, struct lyd_node **result)
Radek Krejcie7b95092019-05-15 11:03:07 +0200444{
Radek Krejci18a57d92019-07-25 14:01:42 +0200445 LY_ERR ret = LY_SUCCESS;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100446 const struct lyd_node **result_trees = NULL;
Radek Krejcie7b95092019-05-15 11:03:07 +0200447 struct lyd_xml_ctx xmlctx = {0};
448
449 xmlctx.options = options;
450 xmlctx.ctx = ctx;
451 xmlctx.line = 1;
452
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200453 /* init */
454 *result = NULL;
455
Michal Vasko9b368d32020-02-14 13:53:31 +0100456 ret = lydxml_nodes(&xmlctx, NULL, &data, result);
Michal Vaskocde73ac2019-11-14 16:10:27 +0100457 LY_CHECK_GOTO(ret, cleanup);
458
Michal Vasko9b368d32020-02-14 13:53:31 +0100459 if (!(options & LYD_OPT_PARSE_ONLY)) {
460 /* add top-level default nodes */
461 ret = lyd_validate_defaults_top(result, NULL, 0, ctx, &xmlctx.incomplete_type_validation, &xmlctx.when_check, options);
462 LY_CHECK_GOTO(ret, cleanup);
463
464 /* prepare sized array for validator */
465 if (*result) {
466 result_trees = lyd_trees_new(1, *result);
467 }
468
469 /* finish incompletely validated terminal values/attributes and when conditions */
470 ret = lyd_validate_unres(&xmlctx.incomplete_type_validation, &xmlctx.incomplete_type_validation_attrs,
471 &xmlctx.when_check, LYD_XML, lydxml_resolve_prefix, ctx, result_trees);
472 LY_CHECK_GOTO(ret, cleanup);
473
474 /* context node and other validation tasks that depend on other data nodes */
475 ret = lyd_validate_data(result_trees, NULL, 0, ctx, options);
476 LY_CHECK_GOTO(result, cleanup);
Michal Vaskocde73ac2019-11-14 16:10:27 +0100477 }
478
Michal Vaskocde73ac2019-11-14 16:10:27 +0100479cleanup:
Michal Vasko9b368d32020-02-14 13:53:31 +0100480 /* there should be no unresolved types stored */
481 assert(!(options & LYD_OPT_PARSE_ONLY) || (!xmlctx.incomplete_type_validation.count
482 && !xmlctx.incomplete_type_validation_attrs.count && !xmlctx.when_check.count));
483
Michal Vaskocde73ac2019-11-14 16:10:27 +0100484 ly_set_erase(&xmlctx.incomplete_type_validation, NULL);
485 ly_set_erase(&xmlctx.incomplete_type_validation_attrs, NULL);
486 ly_set_erase(&xmlctx.when_check, NULL);
Michal Vasko9b368d32020-02-14 13:53:31 +0100487 lyxml_context_clear((struct lyxml_context *)&xmlctx);
Michal Vaskocde73ac2019-11-14 16:10:27 +0100488 lyd_trees_free(result_trees, 0);
Radek Krejcie92210c2019-05-17 15:53:35 +0200489 if (ret) {
490 lyd_free_all(*result);
491 *result = NULL;
492 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200493 return ret;
494}