blob: 31fac0ba56688e9e7fe8817c636f4f94d99058c7 [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"
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;
139 struct lyd_attr *attr = NULL, *last = NULL;
140 const struct lyxml_ns *ns;
141 struct lys_module *mod;
142
143 for (unsigned int u = 0; u < attrs_data->count; ++u) {
144 unsigned int v;
145 struct lysc_ext_instance *ant = NULL;
146 struct attr_data_s *attr_data = (struct attr_data_s*)attrs_data->objs[u];
147
148 if (!attr_data->prefix_len) {
149 /* in XML, all attributes must be prefixed
150 * TODO exception for NETCONF filters which are supposed to map to the ietf-netconf without prefix */
151 if (ctx->options & LYD_OPT_STRICT) {
152 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE, "Missing mandatory prefix for XML attribute \"%.*s\".",
153 attr_data->name_len, attr_data->name);
154 }
155skip_attr:
156 if (attr_data->dynamic) {
157 free(attr_data->value);
158 attr_data->dynamic = 0;
159 }
160 continue;
161 }
162
163 /* get namespace of the attribute to find its annotation definition */
164 ns = lyxml_ns_get((struct lyxml_context *)ctx, attr_data->prefix, attr_data->prefix_len);
165 if (!ns) {
166 /* unknown namespace, ignore the attribute */
167 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", attr_data->prefix_len, attr_data->prefix);
168 goto cleanup;
169 }
170 mod = ly_ctx_get_module_implemented_ns(ctx->ctx, ns->uri);
171 if (!mod) {
172 /* module is not implemented or not present in the schema */
173 if (ctx->options & LYD_OPT_STRICT) {
174 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE,
175 "Unknown (or not implemented) YANG module with namespace \"%s\" for attribute \"%.*s%s%.*s\".",
176 ns, attr_data->prefix_len, attr_data->prefix, attr_data->prefix_len ? ":" : "", attr_data->name_len, attr_data->name);
177 }
178 goto skip_attr;
179 }
180
181 LY_ARRAY_FOR(mod->compiled->exts, v) {
182 if (mod->compiled->exts[v].def->plugin == lyext_plugins_internal[LYEXT_PLUGIN_INTERNAL_ANNOTATION].plugin &&
Radek Krejci7f9b6512019-09-18 13:11:09 +0200183 !ly_strncmp(mod->compiled->exts[v].argument, attr_data->name, attr_data->name_len)) {
Radek Krejci28681fa2019-09-06 13:08:45 +0200184 /* we have the annotation definition */
185 ant = &mod->compiled->exts[v];
186 break;
187 }
188 }
189 if (!ant) {
190 /* attribute is not defined as a metadata annotation (RFC 7952) */
191 if (ctx->options & LYD_OPT_STRICT) {
192 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE, "Annotation definition for attribute \"%s:%.*s\" not found.",
193 mod->name, attr_data->name_len, attr_data->name);
194 }
195 goto skip_attr;
196 }
Radek Krejci17a78d82019-05-15 15:49:55 +0200197
198 attr = calloc(1, sizeof *attr);
199 LY_CHECK_ERR_GOTO(!attr, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup);
Radek Krejci28681fa2019-09-06 13:08:45 +0200200 attr->parent = parent;
201 attr->annotation = ant;
202 rc = lyd_value_parse_attr(attr, attr_data->value, attr_data->value_len, attr_data->dynamic, 0, lydxml_resolve_prefix, ctx, LYD_XML, NULL);
203 if (rc == LY_EINCOMPLETE) {
204 ly_set_add(&ctx->incomplete_type_validation_attrs, attr, LY_SET_OPT_USEASLIST);
205 } else if (rc) {
206 ret = rc;
207 free(attr);
208 goto cleanup;
209 }
210 attr_data->dynamic = 0; /* value eaten by lyd_value_parse_attr() */
211 attr->name = lydict_insert(ctx->ctx, attr_data->name, attr_data->name_len);
Radek Krejci17a78d82019-05-15 15:49:55 +0200212
213 if (last) {
214 last->next = attr;
215 } else {
Radek Krejci28681fa2019-09-06 13:08:45 +0200216 parent->attr = attr;
Radek Krejci17a78d82019-05-15 15:49:55 +0200217 }
218 last = attr;
Radek Krejcie7b95092019-05-15 11:03:07 +0200219 }
Radek Krejci28681fa2019-09-06 13:08:45 +0200220 ret = LY_SUCCESS;
Radek Krejcie7b95092019-05-15 11:03:07 +0200221
222cleanup:
223
Radek Krejci28681fa2019-09-06 13:08:45 +0200224 for (unsigned int u = 0; u < attrs_data->count; ++u) {
225 if (((struct attr_data_s*)attrs_data->objs[u])->dynamic) {
226 free(((struct attr_data_s*)attrs_data->objs[u])->value);
Radek Krejci38d85362019-09-05 16:26:38 +0200227 }
228 }
Radek Krejci28681fa2019-09-06 13:08:45 +0200229 ly_set_erase(attrs_data, free);
230
Radek Krejcie7b95092019-05-15 11:03:07 +0200231 return ret;
232}
233
234/**
235 * @brief Parse XML elements as children YANG data node of the specified parent node.
236 *
237 * @param[in] ctx XML YANG data parser context.
238 * @param[in] parent Parent node where the children are inserted. NULL in case of parsing top-level elements.
239 * @param[in,out] data Pointer to the XML string representation of the YANG data to parse.
240 * @param[out] node Resulting list of the parsed nodes.
241 * @reutn LY_ERR value.
242 */
243static LY_ERR
244lydxml_nodes(struct lyd_xml_ctx *ctx, struct lyd_node_inner *parent, const char **data, struct lyd_node **node)
245{
246 LY_ERR ret = LY_SUCCESS;
247 const char *prefix, *name;
Radek Krejcie7b95092019-05-15 11:03:07 +0200248 size_t prefix_len, name_len;
Radek Krejci28681fa2019-09-06 13:08:45 +0200249 struct ly_set attrs_data = {0};
Radek Krejcie7b95092019-05-15 11:03:07 +0200250 const struct lyxml_ns *ns;
251 const struct lysc_node *snode;
252 struct lys_module *mod;
253 unsigned int parents_count = ctx->elements.count;
Radek Krejci710226d2019-07-24 17:24:59 +0200254 struct lyd_node *cur = NULL, *prev = NULL, *last = NULL;
Radek Krejcie7b95092019-05-15 11:03:07 +0200255
256 (*node) = NULL;
257
258 while(ctx->status == LYXML_ELEMENT) {
259 ret = lyxml_get_element((struct lyxml_context *)ctx, data, &prefix, &prefix_len, &name, &name_len);
260 LY_CHECK_GOTO(ret, cleanup);
261 if (!name) {
262 /* closing previous element */
Radek Krejcie7b95092019-05-15 11:03:07 +0200263 if (ctx->elements.count < parents_count) {
264 /* all siblings parsed */
265 break;
266 } else {
267 continue;
268 }
269 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200270 if (ctx->status == LYXML_ATTRIBUTE) {
Radek Krejci28681fa2019-09-06 13:08:45 +0200271 LY_CHECK_GOTO(lydxml_attributes_parse(ctx, data, &attrs_data), error);
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200272 }
273
Radek Krejcie7b95092019-05-15 11:03:07 +0200274 ns = lyxml_ns_get((struct lyxml_context *)ctx, prefix, prefix_len);
275 if (!ns) {
Radek Krejci28681fa2019-09-06 13:08:45 +0200276 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", prefix_len, prefix);
Radek Krejci11702142019-07-24 17:46:04 +0200277 goto error;
Radek Krejcie7b95092019-05-15 11:03:07 +0200278 }
279 mod = ly_ctx_get_module_implemented_ns(ctx->ctx, ns->uri);
280 if (!mod) {
281 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 +0200282 goto error;
Radek Krejcie7b95092019-05-15 11:03:07 +0200283 }
284 snode = lys_child(parent ? parent->schema : NULL, mod, name, name_len, 0, (ctx->options & LYD_OPT_RPCREPLY) ? LYS_GETNEXT_OUTPUT : 0);
285 if (!snode) {
286 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 +0200287 goto error;
Radek Krejcie7b95092019-05-15 11:03:07 +0200288 }
289
290 /* allocate new node */
291 switch (snode->nodetype) {
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200292 case LYS_ACTION:
293 if ((ctx->options & LYD_OPT_TYPEMASK) != LYD_OPT_RPC) {
Michal Vaskoecd62de2019-11-13 12:35:11 +0100294 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 +0200295 name_len, name, lyd_parse_options_type2str(ctx->options & LYD_OPT_TYPEMASK));
Radek Krejci11702142019-07-24 17:46:04 +0200296 goto error;
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200297 }
298 cur = calloc(1, sizeof(struct lyd_node_inner));
299 break;
300 case LYS_NOTIF:
301 if ((ctx->options & LYD_OPT_TYPEMASK) != LYD_OPT_RPC) {
Michal Vaskoecd62de2019-11-13 12:35:11 +0100302 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 +0200303 name_len, name, lyd_parse_options_type2str(ctx->options));
Radek Krejci11702142019-07-24 17:46:04 +0200304 goto error;
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200305 }
306 cur = calloc(1, sizeof(struct lyd_node_inner));
307 break;
Radek Krejcie7b95092019-05-15 11:03:07 +0200308 case LYS_CONTAINER:
309 case LYS_LIST:
310 cur = calloc(1, sizeof(struct lyd_node_inner));
311 break;
312 case LYS_LEAF:
313 case LYS_LEAFLIST:
314 cur = calloc(1, sizeof(struct lyd_node_term));
315 break;
316 case LYS_ANYDATA:
317 case LYS_ANYXML:
318 cur = calloc(1, sizeof(struct lyd_node_any));
319 break;
Radek Krejcie7b95092019-05-15 11:03:07 +0200320 default:
321 LOGINT(ctx->ctx);
Radek Krejci11702142019-07-24 17:46:04 +0200322 ret = LY_EINT;
Radek Krejcie7b95092019-05-15 11:03:07 +0200323 goto cleanup;
324 }
325 if (!(*node)) {
326 (*node) = cur;
327 }
Radek Krejci710226d2019-07-24 17:24:59 +0200328 last = cur;
Radek Krejcie7b95092019-05-15 11:03:07 +0200329 cur->schema = snode;
Radek Krejci710226d2019-07-24 17:24:59 +0200330 cur->prev = cur;
Radek Krejcie7b95092019-05-15 11:03:07 +0200331 cur->parent = parent;
Radek Krejcie92210c2019-05-17 15:53:35 +0200332 if (parent) {
Radek Krejci710226d2019-07-24 17:24:59 +0200333 if (prev && cur->schema->nodetype == LYS_LEAF && (cur->schema->flags & LYS_KEY)) {
334 /* it is key and we need to insert it into a correct place */
Radek Krejci0fe9b512019-07-26 17:51:05 +0200335 struct lysc_node *key_s;
Radek Krejci710226d2019-07-24 17:24:59 +0200336 unsigned int cur_index, key_index;
337 struct lyd_node *key;
338
Radek Krejci0fe9b512019-07-26 17:51:05 +0200339 for (cur_index = 0, key_s = ((struct lysc_node_list*)parent->schema)->child;
340 key_s && key_s != cur->schema;
341 ++cur_index, key_s = key_s->next);
342 for (key = prev;
343 !(key->schema->flags & LYS_KEY) && key->prev != prev;
344 key = key->prev);
Radek Krejci710226d2019-07-24 17:24:59 +0200345 for (; key->schema->flags & LYS_KEY; key = key->prev) {
Radek Krejci0fe9b512019-07-26 17:51:05 +0200346 for (key_index = 0, key_s = ((struct lysc_node_list*)parent->schema)->child;
347 key_s && key_s != key->schema;
348 ++key_index, key_s = key_s->next);
Radek Krejci710226d2019-07-24 17:24:59 +0200349 if (key_index < cur_index) {
350 /* cur key is supposed to be placed after the key */
351 cur->next = key->next;
352 cur->prev = key;
353 key->next = cur;
354 if (cur->next) {
355 cur->next->prev = cur;
356 } else {
357 parent->child->prev = cur;
358 }
359 break;
360 }
361 if (key->prev == prev) {
362 /* current key is supposed to be the first child from the current children */
363 key = NULL;
364 break;
365 }
366 }
367 if (!key || !(key->schema->flags & LYS_KEY)) {
368 /* current key is supposed to be the first child from the current children */
369 cur->next = parent->child;
370 cur->prev = parent->child->prev;
371 parent->child->prev = cur;
372 parent->child = cur;
373 }
374 if (cur->next) {
375 last = prev;
376 if (ctx->options & LYD_OPT_STRICT) {
Michal Vaskoecd62de2019-11-13 12:35:11 +0100377 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 +0200378 name_len, name);
Radek Krejci11702142019-07-24 17:46:04 +0200379 goto error;
Radek Krejci710226d2019-07-24 17:24:59 +0200380 } else {
381 LOGWRN(ctx->ctx, "Invalid position of the key \"%.*s\" in a list.", name_len, name);
382 }
383 }
384 } else {
385 /* last child of the parent */
386 if (prev) {
387 parent->child->prev = cur;
388 prev->next = cur;
389 cur->prev = prev;
390 }
391 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200392 } else {
Radek Krejci710226d2019-07-24 17:24:59 +0200393 /* top level */
394 if (prev) {
395 /* last top level node */
396 struct lyd_node *iter;
397 for (iter = prev; iter->prev->next; iter = iter->prev);
398 iter->prev = cur;
399 prev->next = cur;
400 cur->prev = prev;
401 } /* first top level node - nothing more to do */
Radek Krejcie7b95092019-05-15 11:03:07 +0200402 }
Radek Krejci710226d2019-07-24 17:24:59 +0200403 prev = last;
Radek Krejci28681fa2019-09-06 13:08:45 +0200404 LY_CHECK_GOTO(ret = lydxml_attributes(ctx, &attrs_data, cur), cleanup);
Radek Krejcie7b95092019-05-15 11:03:07 +0200405
406 if (snode->nodetype & LYD_NODE_TERM) {
407 int dynamic = 0;
408 char *buffer = NULL, *value;
409 size_t buffer_size = 0, value_len;
410
411 if (ctx->status == LYXML_ELEM_CONTENT) {
412 /* get the value */
Radek Krejci11702142019-07-24 17:46:04 +0200413 ret = lyxml_get_string((struct lyxml_context *)ctx, data, &buffer, &buffer_size, &value, &value_len, &dynamic);
414 if (ret == LY_EINVAL) {
Radek Krejci339e2de2019-05-17 14:28:24 +0200415 /* just indentation of a child element found */
416 LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX, "Child element inside terminal node \"%s\" found.", cur->schema->name);
417 goto cleanup;
418 }
Radek Krejci11702142019-07-24 17:46:04 +0200419 ret = LY_SUCCESS;
Radek Krejcie92210c2019-05-17 15:53:35 +0200420 } else {
421 /* no content - validate empty value */
422 value = "";
423 value_len = 0;
Radek Krejcie7b95092019-05-15 11:03:07 +0200424 }
Radek Krejci3c9758d2019-07-11 16:49:10 +0200425 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 +0200426 if (ret == LY_EINCOMPLETE) {
427 ly_set_add(&ctx->incomplete_type_validation, cur, LY_SET_OPT_USEASLIST);
428 } else if (ret) {
429 if (dynamic){
430 free(value);
431 }
432 goto cleanup;
433 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200434 } else if (snode->nodetype & LYD_NODE_INNER) {
435 int dynamic = 0;
436 char *buffer = NULL, *value;
437 size_t buffer_size = 0, value_len;
438
439 if (ctx->status == LYXML_ELEM_CONTENT) {
440 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 +0200441 if (r != LY_EINVAL && (r != LY_SUCCESS || value_len != 0)) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200442 LOGINT(ctx->ctx);
Radek Krejci11702142019-07-24 17:46:04 +0200443 ret = LY_EINT;
Radek Krejcie7b95092019-05-15 11:03:07 +0200444 goto cleanup;
445 }
446 }
Radek Krejciee4cab22019-07-17 17:07:47 +0200447 /* process children */
Radek Krejcie553e6d2019-06-07 15:33:18 +0200448 if (ctx->status == LYXML_ELEMENT && parents_count != ctx->elements.count) {
Radek Krejcie7b95092019-05-15 11:03:07 +0200449 ret = lydxml_nodes(ctx, (struct lyd_node_inner*)cur, data, lyd_node_children_p(cur));
Radek Krejcie92210c2019-05-17 15:53:35 +0200450 LY_CHECK_GOTO(ret, cleanup);
Radek Krejcie7b95092019-05-15 11:03:07 +0200451 }
Radek Krejciee4cab22019-07-17 17:07:47 +0200452 } else if (snode->nodetype & LYD_NODE_ANY) {
453 unsigned int cur_element_index = ctx->elements.count;
454 const char *start = *data, *stop;
455 const char *p, *n;
456 size_t p_len, n_len;
457
458 /* skip children data and store them as a string */
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200459 while (ctx->status != LYXML_END && cur_element_index <= ctx->elements.count) {
Radek Krejciee4cab22019-07-17 17:07:47 +0200460 switch (ctx->status) {
461 case LYXML_ELEMENT:
462 ret = lyxml_get_element((struct lyxml_context *)ctx, data, &p, &p_len, &n, &n_len);
463 break;
464 case LYXML_ATTRIBUTE:
465 lyxml_get_attribute((struct lyxml_context*)ctx, data, &p, &p_len, &n, &n_len);
466 break;
467 case LYXML_ELEM_CONTENT:
468 case LYXML_ATTR_CONTENT:
469 ret = lyxml_get_string((struct lyxml_context *)ctx, data, NULL, NULL, NULL, NULL, NULL);
470 if (ret == LY_EINVAL) {
471 /* not an error, just incorrect XML parser status */
472 ret = LY_SUCCESS;
473 }
474 break;
475 case LYXML_END:
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200476 /* end of data */
Radek Krejciee4cab22019-07-17 17:07:47 +0200477 LOGINT(ctx->ctx);
Radek Krejci11702142019-07-24 17:46:04 +0200478 ret = LY_EINT;
Radek Krejciee4cab22019-07-17 17:07:47 +0200479 goto cleanup;
480 }
481 LY_CHECK_GOTO(ret, cleanup);
482 }
Radek Krejciee4cab22019-07-17 17:07:47 +0200483 ((struct lyd_node_any*)cur)->value_type = LYD_ANYDATA_XML;
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200484 if (start != *data) {
485 /* data now points after the anydata's closing element tag, we need just end of its content */
486 for (stop = *data - 1; *stop != '<'; --stop);
487 ((struct lyd_node_any*)cur)->value.xml = lydict_insert(ctx->ctx, start, stop - start);
488 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200489 }
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200490
Michal Vaskocde73ac2019-11-14 16:10:27 +0100491 /* remember we need to evaluate this node's when */
492 if (!(snode->nodetype & (LYS_ACTION | LYS_NOTIF)) && snode->when) {
493 ly_set_add(&ctx->when_check, cur, LY_SET_OPT_USEASLIST);
494 }
495
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200496 /* calculate the hash and insert it into parent (list with keys is handled when its keys are inserted) */
497 lyd_hash(cur);
498 lyd_insert_hash(cur);
Radek Krejcib6f7ae52019-07-19 10:31:42 +0200499
500 /* if we have empty non-presence container, we keep it, but mark it as default */
501 if (cur->schema->nodetype == LYS_CONTAINER && !((struct lyd_node_inner*)cur)->child &&
502 !cur->attr && !(((struct lysc_node_container*)cur->schema)->flags & LYS_PRESENCE)) {
503 cur->flags |= LYD_DEFAULT;
504 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200505 }
506
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200507 /* TODO add missing siblings default elements */
508
Radek Krejcie7b95092019-05-15 11:03:07 +0200509cleanup:
Radek Krejci28681fa2019-09-06 13:08:45 +0200510 for (unsigned int u = 0; u < attrs_data.count; ++u) {
511 if (((struct attr_data_s*)attrs_data.objs[u])->dynamic) {
512 free(((struct attr_data_s*)attrs_data.objs[u])->value);
513 }
514 }
515 ly_set_erase(&attrs_data, free);
Radek Krejcie7b95092019-05-15 11:03:07 +0200516 return ret;
Radek Krejci11702142019-07-24 17:46:04 +0200517
518error:
519 ret = LY_EVALID;
520 goto cleanup;
Radek Krejcie7b95092019-05-15 11:03:07 +0200521}
522
523LY_ERR
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200524lyd_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 +0200525{
Radek Krejci18a57d92019-07-25 14:01:42 +0200526 LY_ERR ret = LY_SUCCESS;
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200527 struct lyd_node_inner *parent = NULL;
Michal Vaskocde73ac2019-11-14 16:10:27 +0100528 const struct lyd_node **result_trees = NULL;
Radek Krejcie7b95092019-05-15 11:03:07 +0200529 struct lyd_xml_ctx xmlctx = {0};
530
531 xmlctx.options = options;
532 xmlctx.ctx = ctx;
533 xmlctx.line = 1;
534
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200535 /* init */
536 *result = NULL;
537
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200538 if (options & LYD_OPT_RPCREPLY) {
Radek Krejci3c1046e2019-07-26 13:06:53 +0200539 /* prepare container for RPC reply, for which we need RPC
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200540 * - prepare *result as top-level node
541 * - prepare parent as the RPC/action node */
Radek Krejci3c1046e2019-07-26 13:06:53 +0200542 const struct lyd_node *action;
543 for (action = trees[0]; action && action->schema->nodetype != LYS_ACTION; action = lyd_node_children(action)) {
544 /* skip list's keys */
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200545 for ( ;action && action->schema->nodetype == LYS_LEAF; action = action->next);
546 if (action && action->schema->nodetype == LYS_ACTION) {
547 break;
548 }
Radek Krejci3c1046e2019-07-26 13:06:53 +0200549 }
550 if (!action) {
551 LOGERR(ctx, LY_EINVAL, "Data parser invalid argument trees - the first item in the array must be the RPC/action request when parsing %s.",
552 lyd_parse_options_type2str(options));
553 return LY_EINVAL;
554 }
555 parent = (struct lyd_node_inner*)lyd_dup(action, NULL, LYD_DUP_WITH_PARENTS);
556 LY_CHECK_ERR_RET(!parent, LOGERR(ctx, ly_errcode(ctx), "Unable to duplicate RPC/action container for RPC/action reply."), ly_errcode(ctx));
557 for (*result = (struct lyd_node*)parent; (*result)->parent; *result = (struct lyd_node*)(*result)->parent);
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200558 }
559
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200560 if (!data || !data[0]) {
Michal Vaskocde73ac2019-11-14 16:10:27 +0100561 /* no data - just check for missing mandatory nodes */
562 goto validation;
Radek Krejci26a5dfb2019-07-26 14:51:06 +0200563 }
564
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200565 ret = lydxml_nodes(&xmlctx, parent, &data, *result ? &parent->child : result);
Michal Vaskocde73ac2019-11-14 16:10:27 +0100566 LY_CHECK_GOTO(ret, cleanup);
567
568 /* prepare sized array for validator */
569 if (*result) {
570 result_trees = lyd_trees_new(1, *result);
571 }
572
573 /* finish incompletely validated terminal values/attributes and when conditions */
574 ret = lyd_validate_unres(&xmlctx.incomplete_type_validation, &xmlctx.incomplete_type_validation_attrs,
575 &xmlctx.when_check, LYD_XML, lydxml_resolve_prefix, ctx, result_trees);
576 LY_CHECK_GOTO(ret, cleanup);
577
578validation:
579 if ((!(*result) || (parent && !parent->child)) && (options & (LYD_OPT_RPC | LYD_OPT_NOTIF))) {
580 /* error, missing top level node identify RPC and Notification */
581 LOGERR(ctx, LY_EINVAL, "Invalid input data of data parser - expected %s which cannot be empty.",
582 lyd_parse_options_type2str(options));
583 ret = LY_EINVAL;
584 goto cleanup;
585 }
586
587 /* context node and other validation tasks that depend on other data nodes */
588 ret = lyd_validate_modules(result_trees, NULL, 0, ctx, options);
589 LY_CHECK_GOTO(result, cleanup);
590
591cleanup:
592 ly_set_erase(&xmlctx.incomplete_type_validation, NULL);
593 ly_set_erase(&xmlctx.incomplete_type_validation_attrs, NULL);
594 ly_set_erase(&xmlctx.when_check, NULL);
595 lyxml_context_clear((struct lyxml_context*)&xmlctx);
596 lyd_trees_free(result_trees, 0);
Radek Krejcie92210c2019-05-17 15:53:35 +0200597 if (ret) {
598 lyd_free_all(*result);
599 *result = NULL;
600 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200601 return ret;
602}