blob: bf7c6ab71be39bd9d03d5afb44a5609661cd044b [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>
20
21#include "context.h"
22#include "dict.h"
23#include "log.h"
24#include "plugins_types.h"
25#include "set.h"
26#include "tree_data.h"
27#include "tree_data_internal.h"
28#include "tree_schema.h"
29#include "xml.h"
Radek Krejci38d85362019-09-05 16:26:38 +020030#include "plugins_exts_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020031
32/**
33 * @brief internal context for XML YANG data parser.
34 *
35 * The leading part is compatible with the struct lyxml_context
36 */
37struct lyd_xml_ctx {
38 struct ly_ctx *ctx; /**< libyang context */
39 uint64_t line; /**< number of the line being currently processed */
40 enum LYXML_PARSER_STATUS status; /**< status providing information about the next expected object in input data */
41 struct ly_set elements; /**< list of not-yet-closed elements */
42 struct ly_set ns; /**< handled with LY_SET_OPT_USEASLIST */
43
44 uint16_t options; /**< various @ref dataparseroptions. */
45 uint16_t path_len; /**< used bytes in the path buffer */
46#define LYD_PARSER_BUFSIZE 4078
47 char path[LYD_PARSER_BUFSIZE]; /**< buffer for the generated path */
Radek Krejcie553e6d2019-06-07 15:33:18 +020048 struct ly_set incomplete_type_validation; /**< set of nodes validated with LY_EINCOMPLETE result */
Radek Krejci38d85362019-09-05 16:26:38 +020049 struct ly_set incomplete_type_validation_attrs; /**< set of attributes validated with LY_EINCOMPLETE result */
Michal Vaskoecd62de2019-11-13 12:35:11 +010050 struct ly_set when_check; /**< set of nodes with "when" conditions */
Radek Krejcie7b95092019-05-15 11:03:07 +020051};
52
53/**
Radek Krejciaca74032019-06-04 08:53:06 +020054 * @brief XML-parser's implementation of ly_type_resolve_prefix() callback to provide mapping between prefixes used in the values to the schema
55 * via XML namespaces.
56 */
57static const struct lys_module *
58lydxml_resolve_prefix(struct ly_ctx *ctx, const char *prefix, size_t prefix_len, void *parser)
59{
60 const struct lyxml_ns *ns;
61 struct lyxml_context *xmlctx = (struct lyxml_context*)parser;
62
63 ns = lyxml_ns_get(xmlctx, prefix, prefix_len);
64 if (!ns) {
65 return NULL;
66 }
67
68 return ly_ctx_get_module_implemented_ns(ctx, ns->uri);
69}
70
Radek Krejci28681fa2019-09-06 13:08:45 +020071struct attr_data_s {
72 const char *prefix;
73 const char *name;
74 char *value;
75 size_t prefix_len;
76 size_t name_len;
77 size_t value_len;
78 int dynamic;
79};
80
Radek Krejciaca74032019-06-04 08:53:06 +020081/**
Radek Krejcie7b95092019-05-15 11:03:07 +020082 * @brief Parse XML attributes of the XML element of YANG data.
83 *
84 * @param[in] ctx XML YANG data parser context.
Radek Krejcie7b95092019-05-15 11:03:07 +020085 * @param[in,out] data Pointer to the XML string representation of the YANG data to parse.
86 * @param[out] attributes Resulting list of the parsed attributes. XML namespace definitions are not parsed
87 * as attributes, they are stored internally in the parser context.
88 * @reutn LY_ERR value.
89 */
90static LY_ERR
Radek Krejci28681fa2019-09-06 13:08:45 +020091lydxml_attributes_parse(struct lyd_xml_ctx *ctx, const char **data, struct ly_set *attrs_data)
Radek Krejcie7b95092019-05-15 11:03:07 +020092{
93 LY_ERR ret = LY_SUCCESS;
Radek Krejci28681fa2019-09-06 13:08:45 +020094 unsigned int u;
Radek Krejcie7b95092019-05-15 11:03:07 +020095 const char *prefix, *name;
96 size_t prefix_len, name_len;
Radek Krejci28681fa2019-09-06 13:08:45 +020097 struct attr_data_s *attr_data;
Radek Krejcie7b95092019-05-15 11:03:07 +020098
99 while(ctx->status == LYXML_ATTRIBUTE &&
100 lyxml_get_attribute((struct lyxml_context*)ctx, data, &prefix, &prefix_len, &name, &name_len) == LY_SUCCESS) {
Radek Krejci38d85362019-09-05 16:26:38 +0200101 char *buffer = NULL;
102 size_t buffer_size = 0;
Radek Krejcie7b95092019-05-15 11:03:07 +0200103
Radek Krejci17a78d82019-05-15 15:49:55 +0200104 if (!name) {
105 /* seems like all the attrributes were internally processed as namespace definitions */
106 continue;
Radek Krejcie7b95092019-05-15 11:03:07 +0200107 }
Radek Krejci17a78d82019-05-15 15:49:55 +0200108
Radek Krejci38d85362019-09-05 16:26:38 +0200109 /* auxiliary store the prefix information and value string, because we have to wait with resolving prefix
110 * to the time when all the namespaces, defined in this element, are parsed. With the prefix we can find the
111 * annotation definition for the attribute and correctly process the value */
112 attr_data = malloc(sizeof *attr_data);
113 attr_data->prefix = prefix;
Radek Krejci28681fa2019-09-06 13:08:45 +0200114 attr_data->name = name;
Radek Krejci38d85362019-09-05 16:26:38 +0200115 attr_data->prefix_len = prefix_len;
Radek Krejci28681fa2019-09-06 13:08:45 +0200116 attr_data->name_len = name_len;
Radek Krejci38d85362019-09-05 16:26:38 +0200117 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 +0200118 LY_CHECK_ERR_GOTO(ret, free(attr_data), error);
119 ly_set_add(attrs_data, attr_data, LY_SET_OPT_USEASLIST);
120 }
121
122 return LY_SUCCESS;
123
124error:
125 for (u = 0; u < attrs_data->count; ++u) {
126 if (((struct attr_data_s*)attrs_data->objs[u])->dynamic) {
127 free(((struct attr_data_s*)attrs_data->objs[u])->value);
128 }
129 }
130 ly_set_erase(attrs_data, free);
131 return ret;
132}
133
134static LY_ERR
135lydxml_attributes(struct lyd_xml_ctx *ctx, struct ly_set *attrs_data, struct lyd_node *parent)
136{
137 LY_ERR ret = LY_EVALID, rc;
138 struct lyd_attr *attr = NULL, *last = NULL;
139 const struct lyxml_ns *ns;
140 struct lys_module *mod;
141
142 for (unsigned int u = 0; u < attrs_data->count; ++u) {
143 unsigned int v;
144 struct lysc_ext_instance *ant = NULL;
145 struct attr_data_s *attr_data = (struct attr_data_s*)attrs_data->objs[u];
146
147 if (!attr_data->prefix_len) {
148 /* in XML, all attributes must be prefixed
149 * TODO exception for NETCONF filters which are supposed to map to the ietf-netconf without prefix */
150 if (ctx->options & LYD_OPT_STRICT) {
151 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE, "Missing mandatory prefix for XML attribute \"%.*s\".",
152 attr_data->name_len, attr_data->name);
153 }
154skip_attr:
155 if (attr_data->dynamic) {
156 free(attr_data->value);
157 attr_data->dynamic = 0;
158 }
159 continue;
160 }
161
162 /* get namespace of the attribute to find its annotation definition */
163 ns = lyxml_ns_get((struct lyxml_context *)ctx, attr_data->prefix, attr_data->prefix_len);
164 if (!ns) {
165 /* unknown namespace, ignore the attribute */
166 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", attr_data->prefix_len, attr_data->prefix);
167 goto cleanup;
168 }
169 mod = ly_ctx_get_module_implemented_ns(ctx->ctx, ns->uri);
170 if (!mod) {
171 /* module is not implemented or not present in the schema */
172 if (ctx->options & LYD_OPT_STRICT) {
173 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE,
174 "Unknown (or not implemented) YANG module with namespace \"%s\" for attribute \"%.*s%s%.*s\".",
175 ns, attr_data->prefix_len, attr_data->prefix, attr_data->prefix_len ? ":" : "", attr_data->name_len, attr_data->name);
176 }
177 goto skip_attr;
178 }
179
180 LY_ARRAY_FOR(mod->compiled->exts, v) {
181 if (mod->compiled->exts[v].def->plugin == lyext_plugins_internal[LYEXT_PLUGIN_INTERNAL_ANNOTATION].plugin &&
Radek Krejci7f9b6512019-09-18 13:11:09 +0200182 !ly_strncmp(mod->compiled->exts[v].argument, attr_data->name, attr_data->name_len)) {
Radek Krejci28681fa2019-09-06 13:08:45 +0200183 /* we have the annotation definition */
184 ant = &mod->compiled->exts[v];
185 break;
186 }
187 }
188 if (!ant) {
189 /* attribute is not defined as a metadata annotation (RFC 7952) */
190 if (ctx->options & LYD_OPT_STRICT) {
191 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE, "Annotation definition for attribute \"%s:%.*s\" not found.",
192 mod->name, attr_data->name_len, attr_data->name);
193 }
194 goto skip_attr;
195 }
Radek Krejci17a78d82019-05-15 15:49:55 +0200196
197 attr = calloc(1, sizeof *attr);
198 LY_CHECK_ERR_GOTO(!attr, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
Radek Krejci28681fa2019-09-06 13:08:45 +0200199 attr->parent = parent;
200 attr->annotation = ant;
201 rc = lyd_value_parse_attr(attr, attr_data->value, attr_data->value_len, attr_data->dynamic, 0, lydxml_resolve_prefix, ctx, LYD_XML, NULL);
202 if (rc == LY_EINCOMPLETE) {
203 ly_set_add(&ctx->incomplete_type_validation_attrs, attr, LY_SET_OPT_USEASLIST);
204 } else if (rc) {
205 ret = rc;
206 free(attr);
207 goto cleanup;
208 }
209 attr_data->dynamic = 0; /* value eaten by lyd_value_parse_attr() */
210 attr->name = lydict_insert(ctx->ctx, attr_data->name, attr_data->name_len);
Radek Krejci17a78d82019-05-15 15:49:55 +0200211
212 if (last) {
213 last->next = attr;
214 } else {
Radek Krejci28681fa2019-09-06 13:08:45 +0200215 parent->attr = attr;
Radek Krejci17a78d82019-05-15 15:49:55 +0200216 }
217 last = attr;
Radek Krejcie7b95092019-05-15 11:03:07 +0200218 }
Radek Krejci28681fa2019-09-06 13:08:45 +0200219 ret = LY_SUCCESS;
Radek Krejcie7b95092019-05-15 11:03:07 +0200220
221cleanup:
222
Radek Krejci28681fa2019-09-06 13:08:45 +0200223 for (unsigned int u = 0; u < attrs_data->count; ++u) {
224 if (((struct attr_data_s*)attrs_data->objs[u])->dynamic) {
225 free(((struct attr_data_s*)attrs_data->objs[u])->value);
Radek Krejci38d85362019-09-05 16:26:38 +0200226 }
227 }
Radek Krejci28681fa2019-09-06 13:08:45 +0200228 ly_set_erase(attrs_data, free);
229
Radek Krejcie7b95092019-05-15 11:03:07 +0200230 return ret;
231}
232
233/**
234 * @brief Parse XML elements as children YANG data node of the specified parent node.
235 *
236 * @param[in] ctx XML YANG data parser context.
237 * @param[in] parent Parent node where the children are inserted. NULL in case of parsing top-level elements.
238 * @param[in,out] data Pointer to the XML string representation of the YANG data to parse.
239 * @param[out] node Resulting list of the parsed nodes.
240 * @reutn LY_ERR value.
241 */
242static LY_ERR
243lydxml_nodes(struct lyd_xml_ctx *ctx, struct lyd_node_inner *parent, const char **data, struct lyd_node **node)
244{
245 LY_ERR ret = LY_SUCCESS;
246 const char *prefix, *name;
Radek Krejcie7b95092019-05-15 11:03:07 +0200247 size_t prefix_len, name_len;
Radek Krejci28681fa2019-09-06 13:08:45 +0200248 struct ly_set attrs_data = {0};
Radek Krejcie7b95092019-05-15 11:03:07 +0200249 const struct lyxml_ns *ns;
250 const struct lysc_node *snode;
251 struct lys_module *mod;
252 unsigned int parents_count = ctx->elements.count;
Radek Krejci710226d2019-07-24 17:24:59 +0200253 struct lyd_node *cur = NULL, *prev = NULL, *last = NULL;
Radek Krejcie7b95092019-05-15 11:03:07 +0200254
255 (*node) = NULL;
256
257 while(ctx->status == LYXML_ELEMENT) {
258 ret = lyxml_get_element((struct lyxml_context *)ctx, data, &prefix, &prefix_len, &name, &name_len);
259 LY_CHECK_GOTO(ret, cleanup);
260 if (!name) {
261 /* closing previous element */
Radek Krejcie7b95092019-05-15 11:03:07 +0200262 if (ctx->elements.count < parents_count) {
263 /* all siblings parsed */
264 break;
265 } else {
266 continue;
267 }
268 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200269 if (ctx->status == LYXML_ATTRIBUTE) {
Radek Krejci28681fa2019-09-06 13:08:45 +0200270 LY_CHECK_GOTO(lydxml_attributes_parse(ctx, data, &attrs_data), error);
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200271 }
272
Radek Krejcie7b95092019-05-15 11:03:07 +0200273 ns = lyxml_ns_get((struct lyxml_context *)ctx, prefix, prefix_len);
274 if (!ns) {
Radek Krejci28681fa2019-09-06 13:08:45 +0200275 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", prefix_len, prefix);
Radek Krejci11702142019-07-24 17:46:04 +0200276 goto error;
Radek Krejcie7b95092019-05-15 11:03:07 +0200277 }
278 mod = ly_ctx_get_module_implemented_ns(ctx->ctx, ns->uri);
279 if (!mod) {
280 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE, "No module with namespace \"%s\" in the context.", ns->uri);
Radek Krejci11702142019-07-24 17:46:04 +0200281 goto error;
Radek Krejcie7b95092019-05-15 11:03:07 +0200282 }
283 snode = lys_child(parent ? parent->schema : NULL, mod, name, name_len, 0, (ctx->options & LYD_OPT_RPCREPLY) ? LYS_GETNEXT_OUTPUT : 0);
284 if (!snode) {
285 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE, "Element \"%.*s\" not found in the \"%s\" module.", name_len, name, mod->name);
Radek Krejci11702142019-07-24 17:46:04 +0200286 goto error;
Radek Krejcie7b95092019-05-15 11:03:07 +0200287 }
288
289 /* allocate new node */
290 switch (snode->nodetype) {
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200291 case LYS_ACTION:
292 if ((ctx->options & LYD_OPT_TYPEMASK) != LYD_OPT_RPC) {
Michal Vaskoecd62de2019-11-13 12:35:11 +0100293 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_DATA, "Unexpected RPC/action element \"%.*s\" in %s data set.",
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200294 name_len, name, lyd_parse_options_type2str(ctx->options & LYD_OPT_TYPEMASK));
Radek Krejci11702142019-07-24 17:46:04 +0200295 goto error;
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200296 }
297 cur = calloc(1, sizeof(struct lyd_node_inner));
298 break;
299 case LYS_NOTIF:
300 if ((ctx->options & LYD_OPT_TYPEMASK) != LYD_OPT_RPC) {
Michal Vaskoecd62de2019-11-13 12:35:11 +0100301 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_DATA, "Unexpected Notification element \"%.*s\" in %s data set.",
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200302 name_len, name, lyd_parse_options_type2str(ctx->options));
Radek Krejci11702142019-07-24 17:46:04 +0200303 goto error;
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200304 }
305 cur = calloc(1, sizeof(struct lyd_node_inner));
306 break;
Radek Krejcie7b95092019-05-15 11:03:07 +0200307 case LYS_CONTAINER:
308 case LYS_LIST:
309 cur = calloc(1, sizeof(struct lyd_node_inner));
310 break;
311 case LYS_LEAF:
312 case LYS_LEAFLIST:
313 cur = calloc(1, sizeof(struct lyd_node_term));
314 break;
315 case LYS_ANYDATA:
316 case LYS_ANYXML:
317 cur = calloc(1, sizeof(struct lyd_node_any));
318 break;
Radek Krejcie7b95092019-05-15 11:03:07 +0200319 default:
320 LOGINT(ctx->ctx);
Radek Krejci11702142019-07-24 17:46:04 +0200321 ret = LY_EINT;
Radek Krejcie7b95092019-05-15 11:03:07 +0200322 goto cleanup;
323 }
324 if (!(*node)) {
325 (*node) = cur;
326 }
Radek Krejci710226d2019-07-24 17:24:59 +0200327 last = cur;
Radek Krejcie7b95092019-05-15 11:03:07 +0200328 cur->schema = snode;
Radek Krejci710226d2019-07-24 17:24:59 +0200329 cur->prev = cur;
Radek Krejcie7b95092019-05-15 11:03:07 +0200330 cur->parent = parent;
Radek Krejcie92210c2019-05-17 15:53:35 +0200331 if (parent) {
Radek Krejci710226d2019-07-24 17:24:59 +0200332 if (prev && cur->schema->nodetype == LYS_LEAF && (cur->schema->flags & LYS_KEY)) {
333 /* it is key and we need to insert it into a correct place */
Radek Krejci0fe9b512019-07-26 17:51:05 +0200334 struct lysc_node *key_s;
Radek Krejci710226d2019-07-24 17:24:59 +0200335 unsigned int cur_index, key_index;
336 struct lyd_node *key;
337
Radek Krejci0fe9b512019-07-26 17:51:05 +0200338 for (cur_index = 0, key_s = ((struct lysc_node_list*)parent->schema)->child;
339 key_s && key_s != cur->schema;
340 ++cur_index, key_s = key_s->next);
341 for (key = prev;
342 !(key->schema->flags & LYS_KEY) && key->prev != prev;
343 key = key->prev);
Radek Krejci710226d2019-07-24 17:24:59 +0200344 for (; key->schema->flags & LYS_KEY; key = key->prev) {
Radek Krejci0fe9b512019-07-26 17:51:05 +0200345 for (key_index = 0, key_s = ((struct lysc_node_list*)parent->schema)->child;
346 key_s && key_s != key->schema;
347 ++key_index, key_s = key_s->next);
Radek Krejci710226d2019-07-24 17:24:59 +0200348 if (key_index < cur_index) {
349 /* cur key is supposed to be placed after the key */
350 cur->next = key->next;
351 cur->prev = key;
352 key->next = cur;
353 if (cur->next) {
354 cur->next->prev = cur;
355 } else {
356 parent->child->prev = cur;
357 }
358 break;
359 }
360 if (key->prev == prev) {
361 /* current key is supposed to be the first child from the current children */
362 key = NULL;
363 break;
364 }
365 }
366 if (!key || !(key->schema->flags & LYS_KEY)) {
367 /* current key is supposed to be the first child from the current children */
368 cur->next = parent->child;
369 cur->prev = parent->child->prev;
370 parent->child->prev = cur;
371 parent->child = cur;
372 }
373 if (cur->next) {
374 last = prev;
375 if (ctx->options & LYD_OPT_STRICT) {
Michal Vaskoecd62de2019-11-13 12:35:11 +0100376 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_DATA, "Invalid position of the key \"%.*s\" in a list.",
Radek Krejci710226d2019-07-24 17:24:59 +0200377 name_len, name);
Radek Krejci11702142019-07-24 17:46:04 +0200378 goto error;
Radek Krejci710226d2019-07-24 17:24:59 +0200379 } else {
380 LOGWRN(ctx->ctx, "Invalid position of the key \"%.*s\" in a list.", name_len, name);
381 }
382 }
383 } else {
384 /* last child of the parent */
385 if (prev) {
386 parent->child->prev = cur;
387 prev->next = cur;
388 cur->prev = prev;
389 }
390 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200391 } else {
Radek Krejci710226d2019-07-24 17:24:59 +0200392 /* top level */
393 if (prev) {
394 /* last top level node */
395 struct lyd_node *iter;
396 for (iter = prev; iter->prev->next; iter = iter->prev);
397 iter->prev = cur;
398 prev->next = cur;
399 cur->prev = prev;
400 } /* first top level node - nothing more to do */
Radek Krejcie7b95092019-05-15 11:03:07 +0200401 }
Radek Krejci710226d2019-07-24 17:24:59 +0200402 prev = last;
Radek Krejci28681fa2019-09-06 13:08:45 +0200403 LY_CHECK_GOTO(ret = lydxml_attributes(ctx, &attrs_data, cur), cleanup);
Radek Krejcie7b95092019-05-15 11:03:07 +0200404
405 if (snode->nodetype & LYD_NODE_TERM) {
406 int dynamic = 0;
407 char *buffer = NULL, *value;
408 size_t buffer_size = 0, value_len;
409
410 if (ctx->status == LYXML_ELEM_CONTENT) {
411 /* get the value */
Radek Krejci11702142019-07-24 17:46:04 +0200412 ret = lyxml_get_string((struct lyxml_context *)ctx, data, &buffer, &buffer_size, &value, &value_len, &dynamic);
413 if (ret == LY_EINVAL) {
Radek Krejci339e2de2019-05-17 14:28:24 +0200414 /* just indentation of a child element found */
415 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX, "Child element inside terminal node \"%s\" found.", cur->schema->name);
416 goto cleanup;
417 }
Radek Krejci11702142019-07-24 17:46:04 +0200418 ret = LY_SUCCESS;
Radek Krejcie92210c2019-05-17 15:53:35 +0200419 } else {
420 /* no content - validate empty value */
421 value = "";
422 value_len = 0;
Radek Krejcie7b95092019-05-15 11:03:07 +0200423 }
Radek Krejci3c9758d2019-07-11 16:49:10 +0200424 ret = lyd_value_parse((struct lyd_node_term*)cur, value, value_len, dynamic, 0, lydxml_resolve_prefix, ctx, LYD_XML, NULL);
Radek Krejcie553e6d2019-06-07 15:33:18 +0200425 if (ret == LY_EINCOMPLETE) {
426 ly_set_add(&ctx->incomplete_type_validation, cur, LY_SET_OPT_USEASLIST);
427 } else if (ret) {
428 if (dynamic){
429 free(value);
430 }
431 goto cleanup;
432 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200433 } else if (snode->nodetype & LYD_NODE_INNER) {
434 int dynamic = 0;
435 char *buffer = NULL, *value;
436 size_t buffer_size = 0, value_len;
437
438 if (ctx->status == LYXML_ELEM_CONTENT) {
439 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 +0200440 if (r != LY_EINVAL && (r != LY_SUCCESS || value_len != 0)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200441 LOGINT(ctx->ctx);
Radek Krejci11702142019-07-24 17:46:04 +0200442 ret = LY_EINT;
Radek Krejcie7b95092019-05-15 11:03:07 +0200443 goto cleanup;
444 }
445 }
Radek Krejciee4cab22019-07-17 17:07:47 +0200446 /* process children */
Radek Krejcie553e6d2019-06-07 15:33:18 +0200447 if (ctx->status == LYXML_ELEMENT && parents_count != ctx->elements.count) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200448 ret = lydxml_nodes(ctx, (struct lyd_node_inner*)cur, data, lyd_node_children_p(cur));
Radek Krejcie92210c2019-05-17 15:53:35 +0200449 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcie7b95092019-05-15 11:03:07 +0200450 }
Radek Krejciee4cab22019-07-17 17:07:47 +0200451 } else if (snode->nodetype & LYD_NODE_ANY) {
452 unsigned int cur_element_index = ctx->elements.count;
453 const char *start = *data, *stop;
454 const char *p, *n;
455 size_t p_len, n_len;
456
457 /* skip children data and store them as a string */
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200458 while (ctx->status != LYXML_END && cur_element_index <= ctx->elements.count) {
Radek Krejciee4cab22019-07-17 17:07:47 +0200459 switch (ctx->status) {
460 case LYXML_ELEMENT:
461 ret = lyxml_get_element((struct lyxml_context *)ctx, data, &p, &p_len, &n, &n_len);
462 break;
463 case LYXML_ATTRIBUTE:
464 lyxml_get_attribute((struct lyxml_context*)ctx, data, &p, &p_len, &n, &n_len);
465 break;
466 case LYXML_ELEM_CONTENT:
467 case LYXML_ATTR_CONTENT:
468 ret = lyxml_get_string((struct lyxml_context *)ctx, data, NULL, NULL, NULL, NULL, NULL);
469 if (ret == LY_EINVAL) {
470 /* not an error, just incorrect XML parser status */
471 ret = LY_SUCCESS;
472 }
473 break;
474 case LYXML_END:
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200475 /* end of data */
Radek Krejciee4cab22019-07-17 17:07:47 +0200476 LOGINT(ctx->ctx);
Radek Krejci11702142019-07-24 17:46:04 +0200477 ret = LY_EINT;
Radek Krejciee4cab22019-07-17 17:07:47 +0200478 goto cleanup;
479 }
480 LY_CHECK_GOTO(ret, cleanup);
481 }
Radek Krejciee4cab22019-07-17 17:07:47 +0200482 ((struct lyd_node_any*)cur)->value_type = LYD_ANYDATA_XML;
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200483 if (start != *data) {
484 /* data now points after the anydata's closing element tag, we need just end of its content */
485 for (stop = *data - 1; *stop != '<'; --stop);
486 ((struct lyd_node_any*)cur)->value.xml = lydict_insert(ctx->ctx, start, stop - start);
487 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200488 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200489
490 /* calculate the hash and insert it into parent (list with keys is handled when its keys are inserted) */
491 lyd_hash(cur);
492 lyd_insert_hash(cur);
Radek Krejcib6f7ae52019-07-19 10:31:42 +0200493
494 /* if we have empty non-presence container, we keep it, but mark it as default */
495 if (cur->schema->nodetype == LYS_CONTAINER && !((struct lyd_node_inner*)cur)->child &&
496 !cur->attr && !(((struct lysc_node_container*)cur->schema)->flags & LYS_PRESENCE)) {
497 cur->flags |= LYD_DEFAULT;
498 }
499
500 /* TODO context validation */
Radek Krejcie7b95092019-05-15 11:03:07 +0200501 }
502
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200503 /* TODO add missing siblings default elements */
504
Radek Krejcie7b95092019-05-15 11:03:07 +0200505cleanup:
Radek Krejci28681fa2019-09-06 13:08:45 +0200506 for (unsigned int u = 0; u < attrs_data.count; ++u) {
507 if (((struct attr_data_s*)attrs_data.objs[u])->dynamic) {
508 free(((struct attr_data_s*)attrs_data.objs[u])->value);
509 }
510 }
511 ly_set_erase(&attrs_data, free);
Radek Krejcie7b95092019-05-15 11:03:07 +0200512 return ret;
Radek Krejci11702142019-07-24 17:46:04 +0200513
514error:
515 ret = LY_EVALID;
516 goto cleanup;
Radek Krejcie7b95092019-05-15 11:03:07 +0200517}
518
519LY_ERR
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200520lyd_parse_xml(struct ly_ctx *ctx, const char *data, int options, const struct lyd_node **trees, struct lyd_node **result)
Radek Krejcie7b95092019-05-15 11:03:07 +0200521{
Radek Krejci18a57d92019-07-25 14:01:42 +0200522 LY_ERR ret = LY_SUCCESS;
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200523 struct lyd_node_inner *parent = NULL;
Radek Krejcie7b95092019-05-15 11:03:07 +0200524 struct lyd_xml_ctx xmlctx = {0};
525
526 xmlctx.options = options;
527 xmlctx.ctx = ctx;
528 xmlctx.line = 1;
529
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200530 /* init */
531 *result = NULL;
532
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200533 if (options & LYD_OPT_RPCREPLY) {
Radek Krejci3c1046e2019-07-26 13:06:53 +0200534 /* prepare container for RPC reply, for which we need RPC
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200535 * - prepare *result as top-level node
536 * - prepare parent as the RPC/action node */
Radek Krejci3c1046e2019-07-26 13:06:53 +0200537 const struct lyd_node *action;
538 for (action = trees[0]; action && action->schema->nodetype != LYS_ACTION; action = lyd_node_children(action)) {
539 /* skip list's keys */
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200540 for ( ;action && action->schema->nodetype == LYS_LEAF; action = action->next);
541 if (action && action->schema->nodetype == LYS_ACTION) {
542 break;
543 }
Radek Krejci3c1046e2019-07-26 13:06:53 +0200544 }
545 if (!action) {
546 LOGERR(ctx, LY_EINVAL, "Data parser invalid argument trees - the first item in the array must be the RPC/action request when parsing %s.",
547 lyd_parse_options_type2str(options));
548 return LY_EINVAL;
549 }
550 parent = (struct lyd_node_inner*)lyd_dup(action, NULL, LYD_DUP_WITH_PARENTS);
551 LY_CHECK_ERR_RET(!parent, LOGERR(ctx, ly_errcode(ctx), "Unable to duplicate RPC/action container for RPC/action reply."), ly_errcode(ctx));
552 for (*result = (struct lyd_node*)parent; (*result)->parent; *result = (struct lyd_node*)(*result)->parent);
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200553 }
554
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200555 if (!data || !data[0]) {
556 goto no_data;
557 }
558
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200559 ret = lydxml_nodes(&xmlctx, parent, &data, *result ? &parent->child : result);
Radek Krejcie92210c2019-05-17 15:53:35 +0200560 if (ret) {
561 lyd_free_all(*result);
562 *result = NULL;
Radek Krejcie553e6d2019-06-07 15:33:18 +0200563 } else {
564 /* finish incompletely validated terminal values */
565 for (unsigned int u = 0; u < xmlctx.incomplete_type_validation.count; u++) {
566 struct lyd_node_term *node = (struct lyd_node_term*)xmlctx.incomplete_type_validation.objs[u];
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200567 const struct lyd_node **result_trees = NULL;
Radek Krejcie72c0432019-06-10 10:17:03 +0200568
569 /* prepare sized array for validator */
570 if (*result) {
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200571 result_trees = lyd_trees_new(1, *result);
Radek Krejcie553e6d2019-06-07 15:33:18 +0200572 }
Radek Krejcie72c0432019-06-10 10:17:03 +0200573 /* validate and store the value of the node */
Radek Krejci950f6a52019-09-12 17:15:32 +0200574 ret = lyd_value_parse(node, node->value.original, strlen(node->value.original), 0, 1,
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200575 lydxml_resolve_prefix, ctx, LYD_XML, result_trees);
576 lyd_trees_free(result_trees, 0);
Radek Krejcie553e6d2019-06-07 15:33:18 +0200577 if (ret) {
578 lyd_free_all(*result);
579 *result = NULL;
580 break;
581 }
582 }
Radek Krejci38d85362019-09-05 16:26:38 +0200583 /* ... and attribute values */
584 for (unsigned int u = 0; u < xmlctx.incomplete_type_validation_attrs.count; u++) {
585 struct lyd_attr *attr = (struct lyd_attr*)xmlctx.incomplete_type_validation_attrs.objs[u];
586 const struct lyd_node **result_trees = NULL;
587
588 /* prepare sized array for validator */
589 if (*result) {
590 result_trees = lyd_trees_new(1, *result);
591 }
592 /* validate and store the value of the node */
Radek Krejci950f6a52019-09-12 17:15:32 +0200593 ret = lyd_value_parse_attr(attr, attr->value.original, strlen(attr->value.original), 0, 1,
Radek Krejci38d85362019-09-05 16:26:38 +0200594 lydxml_resolve_prefix, ctx, LYD_XML, result_trees);
595 lyd_trees_free(result_trees, 0);
596 if (ret) {
597 lyd_free_all(*result);
598 *result = NULL;
599 break;
600 }
601 }
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200602
Radek Krejci3c1046e2019-07-26 13:06:53 +0200603 if (!(*result) || (parent && !parent->child)) {
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200604no_data:
605 /* no data */
606 if (options & (LYD_OPT_RPC | LYD_OPT_NOTIF)) {
Radek Krejci3c1046e2019-07-26 13:06:53 +0200607 /* error, missing top level node identify RPC and Notification */
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200608 LOGERR(ctx, LY_EINVAL, "Invalid input data of data parser - expected %s which cannot be empty.",
609 lyd_parse_options_type2str(options));
610 } else {
611 /* others - no work is needed, just check for missing mandatory nodes */
Radek Krejci3c1046e2019-07-26 13:06:53 +0200612 /* TODO lyd_validate(&result, options, ctx);
613 * - according to the data tree type */
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200614 }
615 }
Radek Krejcie92210c2019-05-17 15:53:35 +0200616 }
Radek Krejcie553e6d2019-06-07 15:33:18 +0200617
618 ly_set_erase(&xmlctx.incomplete_type_validation, NULL);
Radek Krejci38d85362019-09-05 16:26:38 +0200619 ly_set_erase(&xmlctx.incomplete_type_validation_attrs, NULL);
Radek Krejcie7b95092019-05-15 11:03:07 +0200620 lyxml_context_clear((struct lyxml_context*)&xmlctx);
621 return ret;
622}