blob: 34d6fdc4c77a775edac0e0795c1b8630b21352c3 [file] [log] [blame]
Radek Krejcie7b95092019-05-15 11:03:07 +02001/**
2 * @file parser_xml.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko8cc3f662022-03-29 11:25:51 +02004 * @author Michal Vasko <mvasko@cesnet.cz>
Radek Krejcie7b95092019-05-15 11:03:07 +02005 * @brief XML data parser for libyang
6 *
Michal Vasko8cc3f662022-03-29 11:25:51 +02007 * Copyright (c) 2019 - 2022 CESNET, z.s.p.o.
Radek Krejcie7b95092019-05-15 11:03:07 +02008 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
15
Michal Vasko742a5b12022-02-24 16:07:27 +010016#define _GNU_SOURCE
17
Michal Vasko69730152020-10-09 16:30:07 +020018#include <assert.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020019#include <stdint.h>
20#include <stdlib.h>
21#include <string.h>
22
Michal Vasko742a5b12022-02-24 16:07:27 +010023#include "compat.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020024#include "context.h"
Radek Krejci77114102021-03-10 15:21:57 +010025#include "dict.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020026#include "in_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020027#include "log.h"
Michal Vasko8f702ee2024-02-20 15:44:24 +010028#include "ly_common.h"
Radek Krejci7931b192020-06-25 17:05:03 +020029#include "parser_data.h"
30#include "parser_internal.h"
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +010031#include "plugins_exts.h"
Michal Vasko51de7b72022-04-29 09:50:22 +020032#include "plugins_internal.h"
33#include "schema_compile_node.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020034#include "set.h"
Radek Krejci77114102021-03-10 15:21:57 +010035#include "tree.h"
Radek Krejci47fab892020-11-05 17:02:41 +010036#include "tree_data.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020037#include "tree_data_internal.h"
38#include "tree_schema.h"
Radek Krejci77114102021-03-10 15:21:57 +010039#include "tree_schema_internal.h"
Michal Vaskocde73ac2019-11-14 16:10:27 +010040#include "validation.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020041#include "xml.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020042
Michal Vaskod027f382023-02-10 09:13:25 +010043static LY_ERR lydxml_subtree_r(struct lyd_xml_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p,
44 struct ly_set *parsed);
45
Radek Krejci1798aae2020-07-14 13:26:06 +020046void
47lyd_xml_ctx_free(struct lyd_ctx *lydctx)
48{
49 struct lyd_xml_ctx *ctx = (struct lyd_xml_ctx *)lydctx;
50
51 lyd_ctx_free(lydctx);
52 lyxml_ctx_free(ctx->xmlctx);
53 free(ctx);
54}
55
Michal Vasko45791ad2021-06-17 08:45:03 +020056/**
aPiecek77a4f502024-07-03 08:34:48 +020057 * @brief Log namespace error.
58 *
59 * @param[in] xmlctx XML context
60 * @param[in] prefix XML prefix.
61 * @param[in] prefix_len XML prefix length.
62 * @param[in] attr_name Current XML attribute name.
63 * @param[in] attr_len Current XML attribute name length.
64 */
65static void
66lydxml_log_namespace_err(struct lyxml_ctx *xmlctx, const char *prefix, size_t prefix_len,
67 const char *attr_name, size_t attr_len)
68{
69 if (prefix_len && attr_len) {
70 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\" at attribute \"%.*s\".",
71 (int)prefix_len, prefix, (int)attr_len, attr_name);
72 } else if (prefix_len) {
73 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".",
74 (int)prefix_len, prefix);
75 } else {
76 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Missing XML namespace.");
77 }
78}
79
80/**
Michal Vasko45791ad2021-06-17 08:45:03 +020081 * @brief Parse and create XML metadata.
82 *
83 * @param[in] lydctx XML data parser context.
Michal Vaskoddd76592022-01-17 13:34:48 +010084 * @param[in] sparent Schema node of the parent.
Michal Vasko45791ad2021-06-17 08:45:03 +020085 * @param[out] meta List of created metadata instances.
86 * @return LY_ERR value.
87 */
Radek Krejcie7b95092019-05-15 11:03:07 +020088static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +010089lydxml_metadata(struct lyd_xml_ctx *lydctx, const struct lysc_node *sparent, struct lyd_meta **meta)
Radek Krejcie7b95092019-05-15 11:03:07 +020090{
aPiecek1c4da362021-04-29 14:26:34 +020091 LY_ERR ret = LY_SUCCESS;
Radek Krejci28681fa2019-09-06 13:08:45 +020092 const struct lyxml_ns *ns;
93 struct lys_module *mod;
Michal Vaskob36053d2020-03-26 15:49:30 +010094 const char *name;
95 size_t name_len;
Michal Vasko45791ad2021-06-17 08:45:03 +020096 LY_ARRAY_COUNT_TYPE u;
97 ly_bool filter_attrs = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +020098 struct lyxml_ctx *xmlctx = lydctx->xmlctx;
Radek Krejci28681fa2019-09-06 13:08:45 +020099
Michal Vaskob36053d2020-03-26 15:49:30 +0100100 *meta = NULL;
Radek Krejci28681fa2019-09-06 13:08:45 +0200101
Michal Vasko7a266772024-01-23 11:02:38 +0100102 LOG_LOCSET(sparent, NULL);
Michal Vasko85be65e2023-06-13 09:44:17 +0200103
Michal Vasko45791ad2021-06-17 08:45:03 +0200104 /* check for NETCONF filter unqualified attributes */
Michal Vasko1b2a3f42022-12-20 09:38:28 +0100105 if (!strcmp(sparent->module->name, "notifications")) {
106 /* ancient module that does not even use the extension */
107 filter_attrs = 1;
108 } else {
109 LY_ARRAY_FOR(sparent->exts, u) {
110 if (!strcmp(sparent->exts[u].def->name, "get-filter-element-attributes") &&
111 !strcmp(sparent->exts[u].def->module->name, "ietf-netconf")) {
112 filter_attrs = 1;
113 break;
114 }
Michal Vasko45791ad2021-06-17 08:45:03 +0200115 }
116 }
117
Michal Vaskob36053d2020-03-26 15:49:30 +0100118 while (xmlctx->status == LYXML_ATTRIBUTE) {
119 if (!xmlctx->prefix_len) {
Michal Vasko45791ad2021-06-17 08:45:03 +0200120 /* in XML all attributes must be prefixed except NETCONF filter ones marked by an extension */
121 if (filter_attrs && (!ly_strncmp("type", xmlctx->name, xmlctx->name_len) ||
122 !ly_strncmp("select", xmlctx->name, xmlctx->name_len))) {
123 mod = ly_ctx_get_module_implemented(xmlctx->ctx, "ietf-netconf");
124 if (!mod) {
125 LOGVAL(xmlctx->ctx, LYVE_REFERENCE,
126 "Missing (or not implemented) YANG module \"ietf-netconf\" for special filter attributes.");
127 ret = LY_ENOTFOUND;
128 goto cleanup;
129 }
130 goto create_meta;
131 }
132
Michal Vaskoe0665742021-02-11 11:08:44 +0100133 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100134 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Missing mandatory prefix for XML metadata \"%.*s\".",
Radek Krejci422afb12021-03-04 16:38:16 +0100135 (int)xmlctx->name_len, xmlctx->name);
aPiecekf410c2e2024-06-20 13:09:07 +0200136 /* If LYD_VALIDATE_MULTI_ERROR is set, then continue parsing, because otherwise the parser context
137 * will remain in a bad state, which will cause termination on some assert or undefined behavior.
138 */
139 LY_DPARSER_ERR_GOTO(LY_EVALID, ret = LY_EVALID, lydctx, cleanup);
Radek Krejci28681fa2019-09-06 13:08:45 +0200140 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100141
Michal Vasko45791ad2021-06-17 08:45:03 +0200142 /* skip attr */
Michal Vaskob36053d2020-03-26 15:49:30 +0100143 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
144 assert(xmlctx->status == LYXML_ATTR_CONTENT);
145 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Radek Krejci28681fa2019-09-06 13:08:45 +0200146 continue;
147 }
148
149 /* get namespace of the attribute to find its annotation definition */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200150 ns = lyxml_ns_get(&xmlctx->ns, xmlctx->prefix, xmlctx->prefix_len);
Radek Krejci28681fa2019-09-06 13:08:45 +0200151 if (!ns) {
aPiecek77a4f502024-07-03 08:34:48 +0200152 lydxml_log_namespace_err(xmlctx, xmlctx->prefix, xmlctx->prefix_len, xmlctx->name, xmlctx->name_len);
Michal Vasko45791ad2021-06-17 08:45:03 +0200153 ret = LY_ENOTFOUND;
Radek Krejci28681fa2019-09-06 13:08:45 +0200154 goto cleanup;
155 }
Michal Vasko45791ad2021-06-17 08:45:03 +0200156
157 /* get the module with metadata definition */
Michal Vasko52927e22020-03-16 17:26:14 +0100158 mod = ly_ctx_get_module_implemented_ns(xmlctx->ctx, ns->uri);
Radek Krejci28681fa2019-09-06 13:08:45 +0200159 if (!mod) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100160 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100161 LOGVAL(xmlctx->ctx, LYVE_REFERENCE,
Michal Vasko69730152020-10-09 16:30:07 +0200162 "Unknown (or not implemented) YANG module with namespace \"%s\" for metadata \"%.*s%s%.*s\".",
Radek Krejci422afb12021-03-04 16:38:16 +0100163 ns->uri, (int)xmlctx->prefix_len, xmlctx->prefix, xmlctx->prefix_len ? ":" : "",
164 (int)xmlctx->name_len, xmlctx->name);
Michal Vasko45791ad2021-06-17 08:45:03 +0200165 ret = LY_ENOTFOUND;
Michal Vaskob36053d2020-03-26 15:49:30 +0100166 goto cleanup;
Radek Krejci28681fa2019-09-06 13:08:45 +0200167 }
Michal Vasko45791ad2021-06-17 08:45:03 +0200168
169 /* skip attr */
170 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
171 assert(xmlctx->status == LYXML_ATTR_CONTENT);
172 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
173 continue;
Radek Krejci28681fa2019-09-06 13:08:45 +0200174 }
175
Michal Vasko45791ad2021-06-17 08:45:03 +0200176create_meta:
Michal Vasko60ea6352020-06-29 13:39:39 +0200177 /* remember meta name and get its content */
Michal Vaskob36053d2020-03-26 15:49:30 +0100178 name = xmlctx->name;
179 name_len = xmlctx->name_len;
180 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
181 assert(xmlctx->status == LYXML_ATTR_CONTENT);
182
183 /* create metadata */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200184 ret = lyd_parser_create_meta((struct lyd_ctx *)lydctx, NULL, meta, mod, name, name_len, xmlctx->value,
Michal Vaskoddd76592022-01-17 13:34:48 +0100185 xmlctx->value_len, &xmlctx->dynamic, LY_VALUE_XML, &xmlctx->ns, LYD_HINT_DATA, sparent);
Radek Krejci1798aae2020-07-14 13:26:06 +0200186 LY_CHECK_GOTO(ret, cleanup);
Michal Vaskob36053d2020-03-26 15:49:30 +0100187
188 /* next attribute */
189 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Radek Krejcie7b95092019-05-15 11:03:07 +0200190 }
Michal Vasko52927e22020-03-16 17:26:14 +0100191
Radek Krejcie7b95092019-05-15 11:03:07 +0200192cleanup:
Michal Vasko7a266772024-01-23 11:02:38 +0100193 LOG_LOCBACK(1, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +0100194 if (ret) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200195 lyd_free_meta_siblings(*meta);
Michal Vaskob36053d2020-03-26 15:49:30 +0100196 *meta = NULL;
Radek Krejci38d85362019-09-05 16:26:38 +0200197 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200198 return ret;
199}
200
Michal Vasko52927e22020-03-16 17:26:14 +0100201static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200202lydxml_attrs(struct lyxml_ctx *xmlctx, struct lyd_attr **attr)
Michal Vasko52927e22020-03-16 17:26:14 +0100203{
204 LY_ERR ret = LY_SUCCESS;
205 const struct lyxml_ns *ns;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100206 void *val_prefix_data;
Radek Krejci8df109d2021-04-23 12:19:08 +0200207 LY_VALUE_FORMAT format;
Radek Krejci1798aae2020-07-14 13:26:06 +0200208 struct lyd_attr *attr2;
Michal Vaskob36053d2020-03-26 15:49:30 +0100209 const char *name, *prefix;
210 size_t name_len, prefix_len;
Michal Vasko52927e22020-03-16 17:26:14 +0100211
212 assert(attr);
Michal Vaskob36053d2020-03-26 15:49:30 +0100213 *attr = NULL;
Michal Vasko52927e22020-03-16 17:26:14 +0100214
Michal Vaskob36053d2020-03-26 15:49:30 +0100215 while (xmlctx->status == LYXML_ATTRIBUTE) {
Michal Vasko52927e22020-03-16 17:26:14 +0100216 if (*attr) {
217 attr2 = *attr;
218 } else {
219 attr2 = NULL;
220 }
221
Michal Vaskob36053d2020-03-26 15:49:30 +0100222 /* remember attr prefix, name, and get its content */
223 prefix = xmlctx->prefix;
224 prefix_len = xmlctx->prefix_len;
225 name = xmlctx->name;
226 name_len = xmlctx->name_len;
227 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
228 assert(xmlctx->status == LYXML_ATTR_CONTENT);
229
Michal Vaskoe137fc42021-07-22 11:53:13 +0200230 /* handle special "xml" attribute prefix */
231 if ((prefix_len == 3) && !strncmp(prefix, "xml", 3)) {
232 name = prefix;
233 name_len += 1 + prefix_len;
234 prefix = NULL;
235 prefix_len = 0;
236 }
237
238 /* find namespace of the attribute, if any */
239 ns = NULL;
240 if (prefix_len) {
241 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
242 if (!ns) {
aPiecek77a4f502024-07-03 08:34:48 +0200243 lydxml_log_namespace_err(xmlctx, prefix, prefix_len, name, name_len);
Michal Vaskoe137fc42021-07-22 11:53:13 +0200244 ret = LY_EVALID;
245 goto cleanup;
246 }
247 }
248
Michal Vasko52927e22020-03-16 17:26:14 +0100249 /* get value prefixes */
Michal Vaskofc2cd072021-02-24 13:17:17 +0100250 val_prefix_data = NULL;
Radek Krejci8df109d2021-04-23 12:19:08 +0200251 LY_CHECK_GOTO(ret = ly_store_prefix_data(xmlctx->ctx, xmlctx->value, xmlctx->value_len, LY_VALUE_XML,
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100252 &xmlctx->ns, &format, &val_prefix_data), cleanup);
Michal Vasko52927e22020-03-16 17:26:14 +0100253
254 /* attr2 is always changed to the created attribute */
Michal Vasko501af032020-11-11 20:27:44 +0100255 ret = lyd_create_attr(NULL, &attr2, xmlctx->ctx, name, name_len, prefix, prefix_len, ns ? ns->uri : NULL,
Michal Vaskoe5e49e92022-02-01 13:15:08 +0100256 ns ? strlen(ns->uri) : 0, xmlctx->value, xmlctx->value_len, &xmlctx->dynamic, format, val_prefix_data,
257 LYD_HINT_DATA);
Michal Vasko52927e22020-03-16 17:26:14 +0100258 LY_CHECK_GOTO(ret, cleanup);
259
260 if (!*attr) {
261 *attr = attr2;
262 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100263
264 /* next attribute */
265 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Michal Vasko52927e22020-03-16 17:26:14 +0100266 }
267
268cleanup:
Michal Vaskob36053d2020-03-26 15:49:30 +0100269 if (ret) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200270 lyd_free_attr_siblings(xmlctx->ctx, *attr);
Michal Vaskob36053d2020-03-26 15:49:30 +0100271 *attr = NULL;
Michal Vasko52927e22020-03-16 17:26:14 +0100272 }
Michal Vasko52927e22020-03-16 17:26:14 +0100273 return ret;
274}
275
Michal Vasko44685da2020-03-17 15:38:06 +0100276static LY_ERR
Michal Vaskob36053d2020-03-26 15:49:30 +0100277lydxml_check_list(struct lyxml_ctx *xmlctx, const struct lysc_node *list)
Michal Vasko44685da2020-03-17 15:38:06 +0100278{
Michal Vaskob36053d2020-03-26 15:49:30 +0100279 LY_ERR ret = LY_SUCCESS, r;
280 enum LYXML_PARSER_STATUS next;
Michal Vasko44685da2020-03-17 15:38:06 +0100281 struct ly_set key_set = {0};
282 const struct lysc_node *snode;
Michal Vaskob36053d2020-03-26 15:49:30 +0100283 uint32_t i, parents_count;
Michal Vasko44685da2020-03-17 15:38:06 +0100284
285 assert(list && (list->nodetype == LYS_LIST));
286
287 /* get all keys into a set (keys do not have if-features or anything) */
288 snode = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100289 while ((snode = lys_getnext(snode, list, NULL, 0)) && (snode->flags & LYS_KEY)) {
Radek Krejci3d92e442020-10-12 12:48:13 +0200290 ret = ly_set_add(&key_set, (void *)snode, 1, NULL);
Radek Krejciba03a5a2020-08-27 14:40:41 +0200291 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko44685da2020-03-17 15:38:06 +0100292 }
293
Michal Vasko12d809c2021-03-03 16:34:32 +0100294 /* remember parent count */
295 parents_count = xmlctx->elements.count;
296
Michal Vaskob36053d2020-03-26 15:49:30 +0100297 while (xmlctx->status == LYXML_ELEMENT) {
Michal Vasko44685da2020-03-17 15:38:06 +0100298 /* find key definition */
299 for (i = 0; i < key_set.count; ++i) {
300 snode = (const struct lysc_node *)key_set.objs[i];
Michal Vaskob36053d2020-03-26 15:49:30 +0100301 if (!ly_strncmp(snode->name, xmlctx->name, xmlctx->name_len)) {
Michal Vasko44685da2020-03-17 15:38:06 +0100302 break;
303 }
304 }
Michal Vaskob36053d2020-03-26 15:49:30 +0100305 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Michal Vasko44685da2020-03-17 15:38:06 +0100306
307 /* skip attributes */
308 while (xmlctx->status == LYXML_ATTRIBUTE) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100309 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
310 assert(xmlctx->status == LYXML_ATTR_CONTENT);
311 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Michal Vasko44685da2020-03-17 15:38:06 +0100312 }
313
Michal Vaskob36053d2020-03-26 15:49:30 +0100314 assert(xmlctx->status == LYXML_ELEM_CONTENT);
315 if (i < key_set.count) {
316 /* validate the value */
Michal Vasko583b4642023-05-25 10:39:34 +0200317 r = ly_value_validate(NULL, snode, xmlctx->value, xmlctx->value_len, LY_VALUE_XML, &xmlctx->ns, LYD_HINT_DATA);
Michal Vaskob36053d2020-03-26 15:49:30 +0100318 if (!r) {
319 /* key with a valid value, remove from the set */
320 ly_set_rm_index(&key_set, i, NULL);
Michal Vasko44685da2020-03-17 15:38:06 +0100321 }
322 }
323
Michal Vaskob36053d2020-03-26 15:49:30 +0100324 /* parser next */
325 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
Michal Vasko44685da2020-03-17 15:38:06 +0100326
Michal Vaskob36053d2020-03-26 15:49:30 +0100327 /* skip any children, resursively */
Michal Vasko12d809c2021-03-03 16:34:32 +0100328 while (xmlctx->status == LYXML_ELEMENT) {
329 while (parents_count < xmlctx->elements.count) {
330 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
331 }
332 assert(xmlctx->status == LYXML_ELEM_CLOSE);
Michal Vaskob36053d2020-03-26 15:49:30 +0100333 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
334 }
335
336 /* parser next, but do not parse closing element of the list because it would remove its namespaces */
337 assert(xmlctx->status == LYXML_ELEM_CLOSE);
338 LY_CHECK_GOTO(ret = lyxml_ctx_peek(xmlctx, &next), cleanup);
339 if (next != LYXML_ELEM_CLOSE) {
340 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), cleanup);
341 }
Michal Vasko44685da2020-03-17 15:38:06 +0100342 }
343
344 if (key_set.count) {
Michal Vaskob36053d2020-03-26 15:49:30 +0100345 /* some keys are missing/did not validate */
Michal Vasko44685da2020-03-17 15:38:06 +0100346 ret = LY_ENOT;
Michal Vasko44685da2020-03-17 15:38:06 +0100347 }
348
349cleanup:
350 ly_set_erase(&key_set, NULL);
351 return ret;
352}
353
Michal Vasko5c24ed12021-06-09 09:27:32 +0200354/**
355 * @brief Skip an element with all its descendants.
356 *
357 * @param[in] xmlctx XML parser context.
358 * @return LY_ERR value.
359 */
Michal Vasko1bf09392020-03-27 12:38:10 +0100360static LY_ERR
361lydxml_data_skip(struct lyxml_ctx *xmlctx)
362{
363 uint32_t parents_count;
364
365 /* remember current number of parents */
366 parents_count = xmlctx->elements.count;
aPiecek9cdb9e62021-05-18 09:46:20 +0200367 assert(parents_count);
Michal Vasko1bf09392020-03-27 12:38:10 +0100368
369 /* skip after the content */
370 while (xmlctx->status != LYXML_ELEM_CONTENT) {
371 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
372 }
373 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
374
375 /* skip all children elements, recursively, if any */
aPiecek9cdb9e62021-05-18 09:46:20 +0200376 while (parents_count <= xmlctx->elements.count) {
Michal Vasko1bf09392020-03-27 12:38:10 +0100377 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
378 }
379
380 /* close element */
381 assert(xmlctx->status == LYXML_ELEM_CLOSE);
382 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
383
384 return LY_SUCCESS;
385}
386
Michal Vasko5c24ed12021-06-09 09:27:32 +0200387/**
388 * @brief Check that the current element can be parsed as a data node.
389 *
390 * @param[in] lydctx XML data parser context.
391 * @param[in,out] snode Found schema node, set to NULL if data node cannot be created.
392 * @return LY_ERR value.
393 */
Michal Vasko1bf09392020-03-27 12:38:10 +0100394static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200395lydxml_data_check_opaq(struct lyd_xml_ctx *lydctx, const struct lysc_node **snode)
Michal Vasko1bf09392020-03-27 12:38:10 +0100396{
397 LY_ERR ret = LY_SUCCESS;
Michal Vaskoda8fbbf2021-06-16 11:44:44 +0200398 struct lyxml_ctx *xmlctx = lydctx->xmlctx, pxmlctx;
Michal Vasko1bf09392020-03-27 12:38:10 +0100399
Radek IÅ¡a3930fd72021-03-08 10:48:40 +0100400 if (!(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
401 /* only checks specific to opaque nodes */
402 return LY_SUCCESS;
403 }
404
Michal Vasko13854662021-06-09 09:27:50 +0200405 if (!((*snode)->nodetype & (LYD_NODE_TERM | LYD_NODE_INNER))) {
406 /* nothing to check */
407 return LY_SUCCESS;
408 }
Michal Vasko1bf09392020-03-27 12:38:10 +0100409
Michal Vaskoda8fbbf2021-06-16 11:44:44 +0200410 assert(xmlctx->elements.count);
411
Michal Vasko13854662021-06-09 09:27:50 +0200412 /* backup parser */
Michal Vaskoda8fbbf2021-06-16 11:44:44 +0200413 LY_CHECK_RET(lyxml_ctx_backup(xmlctx, &pxmlctx));
Michal Vasko1bf09392020-03-27 12:38:10 +0100414
Michal Vasko13854662021-06-09 09:27:50 +0200415 /* skip attributes */
416 while (xmlctx->status == LYXML_ATTRIBUTE) {
417 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore);
418 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore);
419 }
Michal Vasko1bf09392020-03-27 12:38:10 +0100420
Michal Vasko13854662021-06-09 09:27:50 +0200421 if ((*snode)->nodetype & LYD_NODE_TERM) {
422 /* value may not be valid in which case we parse it as an opaque node */
Michal Vasko583b4642023-05-25 10:39:34 +0200423 if (ly_value_validate(NULL, *snode, xmlctx->value, xmlctx->value_len, LY_VALUE_XML, &xmlctx->ns, LYD_HINT_DATA)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +0200424 LOGVRB("Parsing opaque term node \"%s\" with invalid value \"%.*s\".", (*snode)->name, (int)xmlctx->value_len,
Michal Vaskod0237d42021-07-12 14:49:46 +0200425 xmlctx->value);
Michal Vasko13854662021-06-09 09:27:50 +0200426 *snode = NULL;
Michal Vasko1bf09392020-03-27 12:38:10 +0100427 }
Michal Vasko13854662021-06-09 09:27:50 +0200428 } else if ((*snode)->nodetype == LYS_LIST) {
429 /* skip content */
430 LY_CHECK_GOTO(ret = lyxml_ctx_next(xmlctx), restore);
Michal Vasko1bf09392020-03-27 12:38:10 +0100431
Michal Vasko13854662021-06-09 09:27:50 +0200432 if (lydxml_check_list(xmlctx, *snode)) {
433 /* invalid list, parse as opaque if it missing/has invalid some keys */
Michal Vaskod0237d42021-07-12 14:49:46 +0200434 LOGVRB("Parsing opaque list node \"%s\" with missing/invalid keys.", (*snode)->name);
Michal Vasko13854662021-06-09 09:27:50 +0200435 *snode = NULL;
Michal Vasko1bf09392020-03-27 12:38:10 +0100436 }
Michal Vasko13854662021-06-09 09:27:50 +0200437 } else {
Radek IÅ¡a3930fd72021-03-08 10:48:40 +0100438 /* if there is a non-WS value, it cannot be parsed as an inner node */
439 assert(xmlctx->status == LYXML_ELEM_CONTENT);
440 if (!xmlctx->ws_only) {
441 *snode = NULL;
442 }
Michal Vasko1bf09392020-03-27 12:38:10 +0100443 }
444
Michal Vasko13854662021-06-09 09:27:50 +0200445restore:
446 /* restore parser */
Michal Vaskoda8fbbf2021-06-16 11:44:44 +0200447 lyxml_ctx_restore(xmlctx, &pxmlctx);
Michal Vasko1bf09392020-03-27 12:38:10 +0100448 return ret;
449}
450
Radek Krejcie7b95092019-05-15 11:03:07 +0200451/**
Michal Vaskocea58712022-04-01 14:37:08 +0200452 * @brief Get sensible data hints for an opaque node.
453 *
454 * @param[in] name Node name.
455 * @param[in] name_len Length of @p name.
456 * @param[in] value Node value.
457 * @param[in] value_len Length of @p value.
458 * @param[in] first Node first sibling.
Michal Vaskoce2e07c2022-12-01 11:08:52 +0100459 * @param[in] ns Node module namespace, NULL for no namespace.
Michal Vaskocea58712022-04-01 14:37:08 +0200460 * @param[out] hints Data hints to use.
461 * @param[out] anchor Anchor to insert after in case of a list.
462 */
463static void
Michal Vaskod027f382023-02-10 09:13:25 +0100464lydxml_get_hints_opaq(const char *name, size_t name_len, const char *value, size_t value_len, const struct lyd_node *first,
Michal Vaskocea58712022-04-01 14:37:08 +0200465 const char *ns, uint32_t *hints, struct lyd_node **anchor)
466{
467 struct lyd_node_opaq *opaq;
468 char *ptr;
Jan Kundrát863b18b2024-07-06 20:55:23 +0200469 /* this needs to be at least 64bit, and it "should not" be an explicit int64_t
470 * because the code calls strtoll later on, which "might" return a bigger type */
471 long long num;
Michal Vaskocea58712022-04-01 14:37:08 +0200472
473 *hints = 0;
474 *anchor = NULL;
475
476 if (!value_len) {
Michal Vasko6235e152023-08-07 13:39:13 +0200477 /* no value but it may also be zero-length string */
478 *hints |= LYD_VALHINT_EMPTY | LYD_VALHINT_STRING;
Michal Vaskocea58712022-04-01 14:37:08 +0200479 } else if (!strncmp(value, "true", value_len) || !strncmp(value, "false", value_len)) {
480 /* boolean value */
481 *hints |= LYD_VALHINT_BOOLEAN;
482 } else {
Jan Kundrát863b18b2024-07-06 20:55:23 +0200483 num = strtoll(value, &ptr, 10);
Michal Vaskocea58712022-04-01 14:37:08 +0200484 if ((unsigned)(ptr - value) == value_len) {
485 /* number value */
486 *hints |= LYD_VALHINT_DECNUM;
Jan Kundrát863b18b2024-07-06 20:55:23 +0200487 if ((num < INT32_MIN) || (num > UINT32_MAX)) {
Michal Vaskocea58712022-04-01 14:37:08 +0200488 /* large number */
489 *hints |= LYD_VALHINT_NUM64;
490 }
491 } else {
492 /* string value */
493 *hints |= LYD_VALHINT_STRING;
494 }
495 }
496
497 if (!first) {
498 return;
499 }
500
501 /* search backwards to find the last instance */
502 do {
503 first = first->prev;
504 if (first->schema) {
505 continue;
506 }
507
508 opaq = (struct lyd_node_opaq *)first;
509 assert(opaq->format == LY_VALUE_XML);
Michal Vaskoce2e07c2022-12-01 11:08:52 +0100510 if (!ly_strncmp(opaq->name.name, name, name_len) &&
zhangtaogb348a8b2024-02-21 15:43:30 +0800511 ((ns && opaq->name.module_ns && !strcmp(opaq->name.module_ns, ns)) || (!ns && !opaq->name.module_ns))) {
Michal Vaskocea58712022-04-01 14:37:08 +0200512 if (opaq->value && opaq->value[0]) {
513 /* leaf-list nodes */
514 opaq->hints |= LYD_NODEHINT_LEAFLIST;
515 *hints |= LYD_NODEHINT_LEAFLIST;
516 } else {
517 /* list nodes */
518 opaq->hints |= LYD_NODEHINT_LIST;
519 *hints |= LYD_NODEHINT_LIST;
520 }
Michal Vaskod027f382023-02-10 09:13:25 +0100521 *anchor = (struct lyd_node *)first;
Michal Vaskocea58712022-04-01 14:37:08 +0200522 break;
523 }
524 } while (first->prev->next);
525}
526
527/**
Michal Vasko8cc3f662022-03-29 11:25:51 +0200528 * @brief Get schema node for the current element.
Michal Vaskoddd76592022-01-17 13:34:48 +0100529 *
530 * @param[in] lydctx XML data parser context.
Michal Vasko8cc3f662022-03-29 11:25:51 +0200531 * @param[in] parent Parsed parent data node, if any.
532 * @param[in] prefix Element prefix, if any.
533 * @param[in] prefix_len Length of @p prefix.
534 * @param[in] name Element name.
535 * @param[in] name_len Length of @p name.
536 * @param[out] snode Found schema node, NULL if no suitable was found.
537 * @param[out] ext Extension instance that provided @p snode, if any.
Michal Vaskoddd76592022-01-17 13:34:48 +0100538 * @return LY_SUCCESS on success;
Michal Vaskoddd76592022-01-17 13:34:48 +0100539 * @return LY_ERR on error.
540 */
541static LY_ERR
Michal Vaskod027f382023-02-10 09:13:25 +0100542lydxml_subtree_get_snode(struct lyd_xml_ctx *lydctx, const struct lyd_node *parent, const char *prefix, size_t prefix_len,
Michal Vasko8cc3f662022-03-29 11:25:51 +0200543 const char *name, size_t name_len, const struct lysc_node **snode, struct lysc_ext_instance **ext)
Michal Vaskoddd76592022-01-17 13:34:48 +0100544{
545 LY_ERR r;
Michal Vaskob36053d2020-03-26 15:49:30 +0100546 struct lyxml_ctx *xmlctx;
547 const struct ly_ctx *ctx;
Radek Krejcie7b95092019-05-15 11:03:07 +0200548 const struct lyxml_ns *ns;
Radek Krejcie7b95092019-05-15 11:03:07 +0200549 struct lys_module *mod;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200550 uint32_t getnext_opts;
Michal Vaskoe0665742021-02-11 11:08:44 +0100551
Michal Vaskob36053d2020-03-26 15:49:30 +0100552 xmlctx = lydctx->xmlctx;
553 ctx = xmlctx->ctx;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100554 getnext_opts = lydctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0;
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100555
Michal Vasko8cc3f662022-03-29 11:25:51 +0200556 *snode = NULL;
557 *ext = NULL;
Michal Vaskoddd76592022-01-17 13:34:48 +0100558
Michal Vasko8cc3f662022-03-29 11:25:51 +0200559 /* get current namespace */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200560 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200561 if (!ns) {
Michal Vaskoce2e07c2022-12-01 11:08:52 +0100562 if (lydctx->int_opts & LYD_INTOPT_ANY) {
563 goto unknown_module;
564 }
aPiecek77a4f502024-07-03 08:34:48 +0200565 lydxml_log_namespace_err(xmlctx, prefix, prefix_len, NULL, 0);
Michal Vasko8cc3f662022-03-29 11:25:51 +0200566 return LY_EVALID;
Michal Vaskoa5da3292020-08-12 13:10:50 +0200567 }
Michal Vasko8cc3f662022-03-29 11:25:51 +0200568
569 /* get the element module, use parent context if possible because of extensions */
570 mod = ly_ctx_get_module_implemented_ns(parent ? LYD_CTX(parent) : ctx, ns->uri);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200571 if (!mod) {
Michal Vaskoddd76592022-01-17 13:34:48 +0100572 /* check for extension data */
Michal Vasko8cc3f662022-03-29 11:25:51 +0200573 r = ly_nested_ext_schema(parent, NULL, prefix, prefix_len, LY_VALUE_XML, &lydctx->xmlctx->ns, name, name_len,
574 snode, ext);
575 if (r != LY_ENOT) {
576 /* success or error */
Michal Vaskoddd76592022-01-17 13:34:48 +0100577 return r;
Michal Vaskoddd76592022-01-17 13:34:48 +0100578 }
579
Michal Vaskoce2e07c2022-12-01 11:08:52 +0100580unknown_module:
Michal Vaskoe0665742021-02-11 11:08:44 +0100581 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +0200582 if (ns) {
583 LOGVAL(ctx, LYVE_REFERENCE, "No module with namespace \"%s\" in the context.", ns->uri);
584 } else if (prefix_len) {
585 LOGVAL(ctx, LYVE_REFERENCE, "No module with namespace \"%.*s\" in the context.", (int)prefix_len, prefix);
586 } else {
587 LOGVAL(ctx, LYVE_REFERENCE, "No default namespace in the context.");
588 }
Michal Vasko8cc3f662022-03-29 11:25:51 +0200589 return LY_EVALID;
Radek Krejcie7b95092019-05-15 11:03:07 +0200590 }
Michal Vasko8cc3f662022-03-29 11:25:51 +0200591 return LY_SUCCESS;
Radek Krejcie7b95092019-05-15 11:03:07 +0200592 }
593
Michal Vaskoa5da3292020-08-12 13:10:50 +0200594 /* get the schema node */
Michal Vaskoa878a892023-08-18 12:22:07 +0200595 if (!parent && lydctx->ext) {
596 *snode = lysc_ext_find_node(lydctx->ext, mod, name, name_len, 0, getnext_opts);
597 } else {
598 /* try to find parent schema node even if it is an opaque node (not connected to the parent) */
599 *snode = lys_find_child(lyd_parser_node_schema(parent), mod, name, name_len, 0, getnext_opts);
600 }
601 if (!*snode) {
602 /* check for extension data */
603 r = ly_nested_ext_schema(parent, NULL, prefix, prefix_len, LY_VALUE_XML, &lydctx->xmlctx->ns, name,
604 name_len, snode, ext);
605 if (r != LY_ENOT) {
606 /* success or error */
607 return r;
Radek Krejcif16e2542021-02-17 15:39:23 +0100608 }
Michal Vaskoddd76592022-01-17 13:34:48 +0100609
Michal Vaskoa878a892023-08-18 12:22:07 +0200610 /* unknown data node */
611 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
612 if (parent) {
613 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found as a child of \"%s\" node.",
614 (int)name_len, name, LYD_NAME(parent));
615 } else if (lydctx->ext) {
616 if (lydctx->ext->argument) {
617 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" %s extension instance.",
618 (int)name_len, name, lydctx->ext->argument, lydctx->ext->def->name);
Michal Vaskoe0665742021-02-11 11:08:44 +0100619 } else {
Michal Vaskoa878a892023-08-18 12:22:07 +0200620 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the %s extension instance.",
621 (int)name_len, name, lydctx->ext->def->name);
Michal Vaskoe0665742021-02-11 11:08:44 +0100622 }
Michal Vaskoa878a892023-08-18 12:22:07 +0200623 } else {
624 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%.*s\" not found in the \"%s\" module.",
625 (int)name_len, name, mod->name);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200626 }
Michal Vaskoa878a892023-08-18 12:22:07 +0200627 return LY_EVALID;
Michal Vaskoa5da3292020-08-12 13:10:50 +0200628 }
Michal Vaskoa878a892023-08-18 12:22:07 +0200629 return LY_SUCCESS;
630 } else {
631 /* check that schema node is valid and can be used */
632 LY_CHECK_RET(lyd_parser_check_schema((struct lyd_ctx *)lydctx, *snode));
633 LY_CHECK_RET(lydxml_data_check_opaq(lydctx, snode));
Michal Vaskoa5da3292020-08-12 13:10:50 +0200634 }
635
Michal Vasko8cc3f662022-03-29 11:25:51 +0200636 return LY_SUCCESS;
637}
638
639/**
Michal Vaskod027f382023-02-10 09:13:25 +0100640 * @brief Parse an XML opque node.
641 *
642 * @param[in] lydctx XML YANG data parser context.
643 * @param[in] sibling Existing sibling node, if any.
644 * @param[in] prefix Parsed node prefix.
645 * @param[in] prefix_len Length of @p prefix.
646 * @param[in] name Parsed node name.
647 * @param[in] name_len Length of @p name.
648 * @param[out] insert_anchor Optional anchor node for inserting this node.
649 * @param[out] node Created node.
650 * @return LY_ERR value.
651 */
652static LY_ERR
653lydxml_subtree_opaq(struct lyd_xml_ctx *lydctx, const struct lyd_node *sibling, const char *prefix, uint32_t prefix_len,
654 const char *name, uint32_t name_len, struct lyd_node **insert_anchor, struct lyd_node **node)
655{
656 LY_ERR rc = LY_SUCCESS;
657 struct lyxml_ctx *xmlctx = lydctx->xmlctx;
Michal Vaskoa16dbb42023-08-15 15:20:25 +0200658 struct lyd_node_opaq *opaq;
659 const char *ns_uri, *value = NULL;
660 size_t value_len;
661 ly_bool ws_only, dynamic = 0;
Michal Vaskod027f382023-02-10 09:13:25 +0100662 const struct lyxml_ns *ns;
663 uint32_t hints;
664 void *val_prefix_data = NULL;
665 LY_VALUE_FORMAT format;
666
667 assert(lydctx->parse_opts & LYD_PARSE_OPAQ);
668
669 *node = NULL;
670
Michal Vaskoa16dbb42023-08-15 15:20:25 +0200671 /* remember the value */
672 value = xmlctx->value;
673 value_len = xmlctx->value_len;
674 ws_only = xmlctx->ws_only;
675 dynamic = xmlctx->dynamic;
676 if (dynamic) {
Michal Vaskod027f382023-02-10 09:13:25 +0100677 xmlctx->dynamic = 0;
Michal Vaskod027f382023-02-10 09:13:25 +0100678 }
679
Michal Vasko535d21c2023-08-09 10:41:44 +0200680 /* get value prefixes, if any */
Michal Vaskoa16dbb42023-08-15 15:20:25 +0200681 rc = ly_store_prefix_data(xmlctx->ctx, value, value_len, LY_VALUE_XML, &xmlctx->ns, &format, &val_prefix_data);
Michal Vasko535d21c2023-08-09 10:41:44 +0200682 LY_CHECK_GOTO(rc, cleanup);
683
Michal Vaskod027f382023-02-10 09:13:25 +0100684 /* get NS again, it may have been backed up and restored */
685 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
686 ns_uri = ns ? ns->uri : NULL;
687
688 /* get best-effort node hints */
689 lydxml_get_hints_opaq(name, name_len, xmlctx->value, xmlctx->value_len, sibling, ns_uri, &hints, insert_anchor);
690
Michal Vaskoa16dbb42023-08-15 15:20:25 +0200691 /* create the node without value */
692 rc = lyd_create_opaq(xmlctx->ctx, name, name_len, prefix, prefix_len, ns_uri, ns_uri ? strlen(ns_uri) : 0, NULL, 0,
693 NULL, format, NULL, hints, node);
Michal Vaskod027f382023-02-10 09:13:25 +0100694 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskod027f382023-02-10 09:13:25 +0100695
Michal Vaskoa878a892023-08-18 12:22:07 +0200696 assert(*node);
Michal Vasko7a266772024-01-23 11:02:38 +0100697 LOG_LOCSET(NULL, *node);
Michal Vaskoa878a892023-08-18 12:22:07 +0200698
Michal Vaskod027f382023-02-10 09:13:25 +0100699 /* parser next */
700 rc = lyxml_ctx_next(xmlctx);
701 LY_CHECK_GOTO(rc, cleanup);
702
703 /* process children */
704 while (xmlctx->status == LYXML_ELEMENT) {
705 rc = lydxml_subtree_r(lydctx, *node, lyd_node_child_p(*node), NULL);
706 LY_CHECK_GOTO(rc, cleanup);
707 }
708
Michal Vaskoa16dbb42023-08-15 15:20:25 +0200709 /* update the value */
710 opaq = (struct lyd_node_opaq *)*node;
711 if (opaq->child) {
712 if (!ws_only) {
713 LOGVAL(xmlctx->ctx, LYVE_SYNTAX_XML, "Mixed XML content node \"%s\" found, not supported.", LYD_NAME(opaq));
714 rc = LY_EVALID;
715 goto cleanup;
716 }
717 } else if (value_len) {
718 lydict_remove(xmlctx->ctx, opaq->value);
719 if (dynamic) {
720 LY_CHECK_GOTO(rc = lydict_insert_zc(xmlctx->ctx, (char *)value, &opaq->value), cleanup);
721 dynamic = 0;
722 } else {
723 LY_CHECK_GOTO(rc = lydict_insert(xmlctx->ctx, value, value_len, &opaq->value), cleanup);
724 }
725 }
726
727 /* always store val_prefix_data because the format requires them */
728 assert(!opaq->val_prefix_data);
729 opaq->val_prefix_data = val_prefix_data;
730 val_prefix_data = NULL;
731
Michal Vaskod027f382023-02-10 09:13:25 +0100732cleanup:
Michal Vaskoa878a892023-08-18 12:22:07 +0200733 if (*node) {
Michal Vasko7a266772024-01-23 11:02:38 +0100734 LOG_LOCBACK(0, 1);
Michal Vaskoa878a892023-08-18 12:22:07 +0200735 }
Michal Vaskod027f382023-02-10 09:13:25 +0100736 ly_free_prefix_data(format, val_prefix_data);
Michal Vaskoa16dbb42023-08-15 15:20:25 +0200737 if (dynamic) {
738 free((char *)value);
739 }
Michal Vaskod027f382023-02-10 09:13:25 +0100740 if (rc) {
741 lyd_free_tree(*node);
742 *node = NULL;
743 }
744 return rc;
745}
746
747/**
748 * @brief Parse an XML leaf/leaf-list node.
749 *
750 * @param[in] lydctx XML YANG data parser context.
751 * @param[in] parent Parent node, if any.
752 * @param[in] snode Schema node of the new node.
753 * @param[out] node Created node.
754 * @return LY_ERR value.
755 */
756static LY_ERR
757lydxml_subtree_term(struct lyd_xml_ctx *lydctx, struct lyd_node *parent, const struct lysc_node *snode,
758 struct lyd_node **node)
759{
760 LY_ERR r, rc = LY_SUCCESS;
761 struct lyxml_ctx *xmlctx = lydctx->xmlctx;
762 struct lyd_node *anchor;
763
764 *node = NULL;
765
766 /* create node */
767 r = lyd_parser_create_term((struct lyd_ctx *)lydctx, snode, xmlctx->value, xmlctx->value_len, &xmlctx->dynamic,
768 LY_VALUE_XML, &xmlctx->ns, LYD_HINT_DATA, node);
769 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
770
771 if (*node) {
Michal Vasko7a266772024-01-23 11:02:38 +0100772 LOG_LOCSET(NULL, *node);
Michal Vaskod027f382023-02-10 09:13:25 +0100773 }
774
Michal Vasko58a1a702023-03-01 13:45:38 +0100775 if (*node && parent && (snode->flags & LYS_KEY)) {
Michal Vaskod027f382023-02-10 09:13:25 +0100776 /* check the key order, the anchor must never be a key */
777 anchor = lyd_insert_get_next_anchor(lyd_child(parent), *node);
778 if (anchor && anchor->schema && (anchor->schema->flags & LYS_KEY)) {
779 if (lydctx->parse_opts & LYD_PARSE_STRICT) {
780 LOGVAL(xmlctx->ctx, LYVE_DATA, "Invalid position of the key \"%s\" in a list.", snode->name);
781 r = LY_EVALID;
782 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
783 } else {
784 LOGWRN(xmlctx->ctx, "Invalid position of the key \"%s\" in a list.", snode->name);
785 }
786 }
787 }
788
789 /* parser next */
790 r = lyxml_ctx_next(xmlctx);
791 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
792
793 /* no children expected */
794 if (xmlctx->status == LYXML_ELEMENT) {
795 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Child element \"%.*s\" inside a terminal node \"%s\" found.",
796 (int)xmlctx->name_len, xmlctx->name, snode->name);
797 r = LY_EVALID;
798 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
799 }
800
801cleanup:
802 if (*node) {
Michal Vasko7a266772024-01-23 11:02:38 +0100803 LOG_LOCBACK(0, 1);
Michal Vaskod027f382023-02-10 09:13:25 +0100804 }
805 if (rc && (!(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) {
806 lyd_free_tree(*node);
807 *node = NULL;
808 }
809 return rc;
810}
811
812/**
813 * @brief Parse an XML inner node.
814 *
815 * @param[in] lydctx XML YANG data parser context.
816 * @param[in] snode Schema node of the new node.
817 * @param[in] ext Extension instance of @p snode, if any.
818 * @param[out] node Created node.
819 * @return LY_ERR value.
820 */
821static LY_ERR
822lydxml_subtree_inner(struct lyd_xml_ctx *lydctx, const struct lysc_node *snode, const struct lysc_ext_instance *ext,
823 struct lyd_node **node)
824{
825 LY_ERR r, rc = LY_SUCCESS;
826 struct lyxml_ctx *xmlctx = lydctx->xmlctx;
827 uint32_t prev_parse_opts = lydctx->parse_opts;
828
829 *node = NULL;
830
831 if (!xmlctx->ws_only) {
832 /* value in inner node */
833 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Text value \"%.*s\" inside an inner node \"%s\" found.",
834 (int)xmlctx->value_len, xmlctx->value, snode->name);
835 r = LY_EVALID;
836 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
837 }
838
839 /* create node */
840 rc = lyd_create_inner(snode, node);
841 LY_CHECK_GOTO(rc, cleanup);
842
Michal Vasko58a1a702023-03-01 13:45:38 +0100843 assert(*node);
Michal Vasko7a266772024-01-23 11:02:38 +0100844 LOG_LOCSET(NULL, *node);
Michal Vaskod027f382023-02-10 09:13:25 +0100845
846 /* parser next */
847 rc = lyxml_ctx_next(xmlctx);
848 LY_CHECK_GOTO(rc, cleanup);
849
850 if (ext) {
851 /* only parse these extension data and validate afterwards */
852 lydctx->parse_opts |= LYD_PARSE_ONLY;
853 }
854
855 /* process children */
856 while (xmlctx->status == LYXML_ELEMENT) {
857 r = lydxml_subtree_r(lydctx, *node, lyd_node_child_p(*node), NULL);
858 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
859 }
860
861 /* restore options */
862 lydctx->parse_opts = prev_parse_opts;
863
864 if (snode->nodetype == LYS_LIST) {
865 /* check all keys exist */
866 r = lyd_parse_check_keys(*node);
Michal Vasko202d8162023-03-01 14:42:19 +0100867 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskod027f382023-02-10 09:13:25 +0100868 }
869
Michal Vasko40015732023-11-20 13:48:15 +0100870 if (!(lydctx->parse_opts & LYD_PARSE_ONLY) && !rc) {
871 /* new node validation, autodelete CANNOT occur (it can if multi-error), all nodes are new */
Michal Vaskod027f382023-02-10 09:13:25 +0100872 r = lyd_validate_new(lyd_node_child_p(*node), snode, NULL, lydctx->val_opts, NULL);
873 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
874
875 /* add any missing default children */
876 r = lyd_new_implicit_r(*node, lyd_node_child_p(*node), NULL, NULL, &lydctx->node_when, &lydctx->node_types,
877 &lydctx->ext_node, (lydctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL);
878 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
879 }
880
881 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
882 /* rememeber the RPC/action/notification */
883 lydctx->op_node = *node;
884 }
885
886cleanup:
887 if (*node) {
Michal Vasko7a266772024-01-23 11:02:38 +0100888 LOG_LOCBACK(0, 1);
Michal Vaskod027f382023-02-10 09:13:25 +0100889 }
890 lydctx->parse_opts = prev_parse_opts;
Michal Vasko202d8162023-03-01 14:42:19 +0100891 if (rc && ((*node && !(*node)->hash) || !(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) {
892 /* list without keys is unusable or an error */
Michal Vaskod027f382023-02-10 09:13:25 +0100893 lyd_free_tree(*node);
894 *node = NULL;
895 }
896 return rc;
897}
898
899/**
900 * @brief Parse an XML anyxml/anydata node.
901 *
902 * @param[in] lydctx XML YANG data parser context.
903 * @param[in] snode Schema node of the new node.
904 * @param[in] ext Extension instance of @p snode, if any.
905 * @param[out] node Created node.
906 * @return LY_ERR value.
907 */
908static LY_ERR
909lydxml_subtree_any(struct lyd_xml_ctx *lydctx, const struct lysc_node *snode, const struct lysc_ext_instance *ext,
910 struct lyd_node **node)
911{
912 LY_ERR r, rc = LY_SUCCESS;
913 struct lyxml_ctx *xmlctx = lydctx->xmlctx;
914 uint32_t prev_parse_opts = lydctx->parse_opts, prev_int_opts = lydctx->int_opts;
915 struct lyd_node *child = NULL;
916 char *val = NULL;
Michal Vasko85be65e2023-06-13 09:44:17 +0200917 ly_bool log_node = 0;
Michal Vaskod027f382023-02-10 09:13:25 +0100918
919 *node = NULL;
920
921 if ((snode->nodetype == LYS_ANYDATA) && !xmlctx->ws_only) {
922 /* value in anydata node, we expect a tree */
923 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Text value \"%.*s\" inside an anydata node \"%s\" found.",
Michal Vasko7b3a00e2023-08-09 11:58:03 +0200924 xmlctx->value_len < 20 ? (int)xmlctx->value_len : 20, xmlctx->value, snode->name);
Michal Vaskod027f382023-02-10 09:13:25 +0100925 r = LY_EVALID;
926 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
927 }
928
929 if (!xmlctx->ws_only) {
930 /* use an arbitrary text value for anyxml */
931 val = strndup(xmlctx->value, xmlctx->value_len);
932 LY_CHECK_ERR_GOTO(!val, LOGMEM(xmlctx->ctx); rc = LY_EMEM, cleanup);
933
934 /* parser next */
935 r = lyxml_ctx_next(xmlctx);
936 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
937
938 /* create node */
939 r = lyd_create_any(snode, val, LYD_ANYDATA_STRING, 1, node);
940 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
941 val = NULL;
942 } else {
Michal Vasko85be65e2023-06-13 09:44:17 +0200943 /* create node */
944 r = lyd_create_any(snode, NULL, LYD_ANYDATA_DATATREE, 1, node);
945 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
946
947 assert(*node);
Michal Vasko7a266772024-01-23 11:02:38 +0100948 LOG_LOCSET(NULL, *node);
Michal Vasko85be65e2023-06-13 09:44:17 +0200949 log_node = 1;
950
Michal Vaskod027f382023-02-10 09:13:25 +0100951 /* parser next */
952 r = lyxml_ctx_next(xmlctx);
953 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
954
955 /* update options so that generic data can be parsed */
956 lydctx->parse_opts &= ~LYD_PARSE_STRICT;
957 lydctx->parse_opts |= LYD_PARSE_OPAQ | (ext ? LYD_PARSE_ONLY : 0);
958 lydctx->int_opts |= LYD_INTOPT_ANY | LYD_INTOPT_WITH_SIBLINGS;
959
960 /* parse any data tree */
961 while (xmlctx->status == LYXML_ELEMENT) {
962 r = lydxml_subtree_r(lydctx, NULL, &child, NULL);
963 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
964 }
965
Michal Vasko85be65e2023-06-13 09:44:17 +0200966 /* assign the data tree */
967 ((struct lyd_node_any *)*node)->value.tree = child;
Michal Vaskod027f382023-02-10 09:13:25 +0100968 child = NULL;
969 }
970
971cleanup:
Michal Vasko85be65e2023-06-13 09:44:17 +0200972 if (log_node) {
Michal Vasko7a266772024-01-23 11:02:38 +0100973 LOG_LOCBACK(0, 1);
Michal Vasko85be65e2023-06-13 09:44:17 +0200974 }
Michal Vaskod027f382023-02-10 09:13:25 +0100975 lydctx->parse_opts = prev_parse_opts;
976 lydctx->int_opts = prev_int_opts;
977 free(val);
978 lyd_free_tree(child);
979 if (rc && (!(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) {
980 lyd_free_tree(*node);
981 *node = NULL;
982 }
983 return rc;
984}
985
986/**
987 * @brief Parse an XML subtree, recursively.
Michal Vasko8cc3f662022-03-29 11:25:51 +0200988 *
989 * @param[in] lydctx XML YANG data parser context.
990 * @param[in,out] parent Parent node where the children are inserted. NULL in case of parsing top-level elements.
Michal Vaskod027f382023-02-10 09:13:25 +0100991 * @param[in,out] first_p Pointer to the first (@p parent or top-level) child.
Michal Vasko8cc3f662022-03-29 11:25:51 +0200992 * @param[in,out] parsed Optional set to add all the parsed siblings into.
993 * @return LY_ERR value.
994 */
995static LY_ERR
996lydxml_subtree_r(struct lyd_xml_ctx *lydctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_set *parsed)
997{
Michal Vaskod027f382023-02-10 09:13:25 +0100998 LY_ERR r, rc = LY_SUCCESS;
999 const char *prefix, *name;
Michal Vasko8cc3f662022-03-29 11:25:51 +02001000 size_t prefix_len, name_len;
1001 struct lyxml_ctx *xmlctx;
1002 const struct ly_ctx *ctx;
Michal Vasko8cc3f662022-03-29 11:25:51 +02001003 struct lyd_meta *meta = NULL;
1004 struct lyd_attr *attr = NULL;
Michal Vasko4a1e3e82023-09-05 08:45:01 +02001005 const struct lysc_node *snode = NULL;
1006 struct lysc_ext_instance *ext = NULL;
Michal Vaskod027f382023-02-10 09:13:25 +01001007 uint32_t orig_parse_opts;
1008 struct lyd_node *node = NULL, *insert_anchor = NULL;
Michal Vasko8cc3f662022-03-29 11:25:51 +02001009 ly_bool parse_subtree;
Michal Vasko8cc3f662022-03-29 11:25:51 +02001010
1011 assert(parent || first_p);
1012
1013 xmlctx = lydctx->xmlctx;
1014 ctx = xmlctx->ctx;
1015
1016 parse_subtree = lydctx->parse_opts & LYD_PARSE_SUBTREE ? 1 : 0;
1017 /* all descendants should be parsed */
1018 lydctx->parse_opts &= ~LYD_PARSE_SUBTREE;
1019 orig_parse_opts = lydctx->parse_opts;
1020
1021 assert(xmlctx->status == LYXML_ELEMENT);
1022
1023 /* remember element prefix and name */
1024 prefix = xmlctx->prefix;
1025 prefix_len = xmlctx->prefix_len;
1026 name = xmlctx->name;
1027 name_len = xmlctx->name_len;
1028
1029 /* parser next */
Michal Vaskod027f382023-02-10 09:13:25 +01001030 rc = lyxml_ctx_next(xmlctx);
1031 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko8cc3f662022-03-29 11:25:51 +02001032
Michal Vasko4a1e3e82023-09-05 08:45:01 +02001033 if ((lydctx->int_opts & LYD_INTOPT_EVENTTIME) && !parent && name_len && !prefix_len &&
1034 !ly_strncmp("eventTime", name, name_len)) {
1035 /* parse eventTime, create node */
1036 assert(xmlctx->status == LYXML_ELEM_CONTENT);
1037 rc = lyd_create_opaq(xmlctx->ctx, name, name_len, prefix, prefix_len,
1038 "urn:ietf:params:xml:ns:netconf:notification:1.0", 47, xmlctx->value,
1039 xmlctx->ws_only ? 0 : xmlctx->value_len, NULL, LY_VALUE_XML, NULL, LYD_HINT_DATA, &node);
1040 LY_CHECK_GOTO(rc, cleanup);
1041
1042 /* validate the value */
1043 r = lyd_parser_notif_eventtime_validate(node);
1044 LY_CHECK_ERR_GOTO(r, rc = r; lyd_free_tree(node), cleanup);
1045
1046 /* parser next */
1047 r = lyxml_ctx_next(xmlctx);
1048 LY_CHECK_ERR_GOTO(r, rc = r; lyd_free_tree(node), cleanup);
1049 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1050 LOGVAL(ctx, LYVE_DATA, "Unexpected notification \"eventTime\" node children.");
1051 rc = LY_EVALID;
1052 lyd_free_tree(node);
1053 goto cleanup;
1054 }
1055
1056 goto node_parsed;
1057 }
1058
Michal Vasko8cc3f662022-03-29 11:25:51 +02001059 /* get the schema node */
Michal Vasko202d8162023-03-01 14:42:19 +01001060 r = lydxml_subtree_get_snode(lydctx, parent, prefix, prefix_len, name, name_len, &snode, &ext);
1061 if (r) {
1062 rc = r;
1063 if ((r == LY_EVALID) && (lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR)) {
1064 /* skip the invalid data */
1065 if ((r = lydxml_data_skip(xmlctx))) {
1066 rc = r;
1067 }
1068 }
1069 goto cleanup;
1070 } else if (!snode && !(lydctx->parse_opts & LYD_PARSE_OPAQ)) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02001071 LOGVRB("Skipping parsing of unknown node \"%.*s\".", (int)name_len, name);
Michal Vasko8cc3f662022-03-29 11:25:51 +02001072
1073 /* skip element with children */
Michal Vaskod027f382023-02-10 09:13:25 +01001074 rc = lydxml_data_skip(xmlctx);
1075 goto cleanup;
Michal Vasko8cc3f662022-03-29 11:25:51 +02001076 }
1077
Michal Vaskoa5da3292020-08-12 13:10:50 +02001078 /* create metadata/attributes */
1079 if (xmlctx->status == LYXML_ATTRIBUTE) {
1080 if (snode) {
aPiecekf410c2e2024-06-20 13:09:07 +02001081 r = lydxml_metadata(lydctx, snode, &meta);
Michal Vaskoa5da3292020-08-12 13:10:50 +02001082 } else {
Michal Vaskoe0665742021-02-11 11:08:44 +01001083 assert(lydctx->parse_opts & LYD_PARSE_OPAQ);
aPiecekf410c2e2024-06-20 13:09:07 +02001084 r = lydxml_attrs(xmlctx, &attr);
Michal Vaskoa5da3292020-08-12 13:10:50 +02001085 }
1086 }
aPiecekf410c2e2024-06-20 13:09:07 +02001087 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vaskoa5da3292020-08-12 13:10:50 +02001088
1089 assert(xmlctx->status == LYXML_ELEM_CONTENT);
1090 if (!snode) {
Michal Vaskod027f382023-02-10 09:13:25 +01001091 /* opaque */
1092 r = lydxml_subtree_opaq(lydctx, parent ? lyd_child(parent) : *first_p, prefix, prefix_len, name, name_len,
1093 &insert_anchor, &node);
Michal Vaskoa5da3292020-08-12 13:10:50 +02001094 } else if (snode->nodetype & LYD_NODE_TERM) {
Michal Vaskod027f382023-02-10 09:13:25 +01001095 /* term */
1096 r = lydxml_subtree_term(lydctx, parent, snode, &node);
Michal Vaskoa5da3292020-08-12 13:10:50 +02001097 } else if (snode->nodetype & LYD_NODE_INNER) {
Michal Vaskod027f382023-02-10 09:13:25 +01001098 /* inner */
1099 r = lydxml_subtree_inner(lydctx, snode, ext, &node);
Michal Vasko7c6f33f2023-02-10 09:40:11 +01001100 } else {
Michal Vaskod027f382023-02-10 09:13:25 +01001101 /* any */
Michal Vasko7c6f33f2023-02-10 09:40:11 +01001102 assert(snode->nodetype & LYD_NODE_ANY);
Michal Vaskod027f382023-02-10 09:13:25 +01001103 r = lydxml_subtree_any(lydctx, snode, ext, &node);
Michal Vaskoa5da3292020-08-12 13:10:50 +02001104 }
Michal Vaskod027f382023-02-10 09:13:25 +01001105 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vasko1524f1a2023-03-01 09:36:34 +01001106
Michal Vasko4a1e3e82023-09-05 08:45:01 +02001107node_parsed:
Michal Vasko58a1a702023-03-01 13:45:38 +01001108 if (node && snode) {
Michal Vasko135719f2022-08-25 12:18:17 +02001109 /* add/correct flags */
Michal Vaskod027f382023-02-10 09:13:25 +01001110 r = lyd_parse_set_data_flags(node, &meta, (struct lyd_ctx *)lydctx, ext);
Michal Vasko567d7032023-02-13 08:53:31 +01001111 LY_CHECK_ERR_GOTO(r, rc = r; lyd_free_tree(node), cleanup);
Michal Vasko135719f2022-08-25 12:18:17 +02001112
Michal Vaskoeba23112022-08-26 08:35:41 +02001113 if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) {
1114 /* store for ext instance node validation, if needed */
Michal Vaskod027f382023-02-10 09:13:25 +01001115 r = lyd_validate_node_ext(node, &lydctx->ext_node);
1116 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vaskoeba23112022-08-26 08:35:41 +02001117 }
Michal Vaskoa5da3292020-08-12 13:10:50 +02001118 }
1119
1120 /* parser next */
1121 assert(xmlctx->status == LYXML_ELEM_CLOSE);
Michal Vaskoddd76592022-01-17 13:34:48 +01001122 if (!parse_subtree) {
Michal Vaskod027f382023-02-10 09:13:25 +01001123 r = lyxml_ctx_next(xmlctx);
Michal Vasko567d7032023-02-13 08:53:31 +01001124 LY_CHECK_ERR_GOTO(r, rc = r; lyd_free_tree(node), cleanup);
Michal Vaskoddd76592022-01-17 13:34:48 +01001125 }
Michal Vaskoa5da3292020-08-12 13:10:50 +02001126
Michal Vasko58a1a702023-03-01 13:45:38 +01001127 LY_CHECK_GOTO(!node, cleanup);
1128
Michal Vaskoa5da3292020-08-12 13:10:50 +02001129 /* add metadata/attributes */
1130 if (snode) {
Michal Vasko871a0252020-11-11 18:35:24 +01001131 lyd_insert_meta(node, meta, 0);
Michal Vaskod027f382023-02-10 09:13:25 +01001132 meta = NULL;
Michal Vaskoa5da3292020-08-12 13:10:50 +02001133 } else {
1134 lyd_insert_attr(node, attr);
Michal Vaskod027f382023-02-10 09:13:25 +01001135 attr = NULL;
Michal Vaskoa5da3292020-08-12 13:10:50 +02001136 }
1137
1138 /* insert, keep first pointer correct */
Michal Vaskocea58712022-04-01 14:37:08 +02001139 if (insert_anchor) {
1140 lyd_insert_after(insert_anchor, node);
1141 } else if (ext) {
Michal Vaskod027f382023-02-10 09:13:25 +01001142 r = lyplg_ext_insert(parent, node);
Michal Vasko567d7032023-02-13 08:53:31 +01001143 LY_CHECK_ERR_GOTO(r, rc = r; lyd_free_tree(node), cleanup);
Michal Vasko8cc3f662022-03-29 11:25:51 +02001144 } else {
aPiecek1462ab12024-02-07 09:13:29 +01001145 lyd_insert_node(parent, first_p, node,
1146 lydctx->parse_opts & LYD_PARSE_ORDERED ? LYD_INSERT_NODE_LAST : LYD_INSERT_NODE_DEFAULT);
Michal Vasko8cc3f662022-03-29 11:25:51 +02001147 }
Michal Vaskoa5da3292020-08-12 13:10:50 +02001148 while (!parent && (*first_p)->prev->next) {
1149 *first_p = (*first_p)->prev;
1150 }
1151
Michal Vaskoe0665742021-02-11 11:08:44 +01001152 /* rememeber a successfully parsed node */
1153 if (parsed) {
1154 ly_set_add(parsed, node, 1, NULL);
1155 }
1156
Michal Vaskod027f382023-02-10 09:13:25 +01001157cleanup:
Michal Vasko8cc3f662022-03-29 11:25:51 +02001158 lydctx->parse_opts = orig_parse_opts;
Michal Vasko3a41dff2020-07-15 14:30:28 +02001159 lyd_free_meta_siblings(meta);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001160 lyd_free_attr_siblings(ctx, attr);
Michal Vaskod027f382023-02-10 09:13:25 +01001161 return rc;
Radek Krejcie7b95092019-05-15 11:03:07 +02001162}
1163
Michal Vaskoe0665742021-02-11 11:08:44 +01001164/**
1165 * @brief Parse a specific XML element into an opaque node.
1166 *
1167 * @param[in] xmlctx XML parser context.
1168 * @param[in] name Name of the element.
1169 * @param[in] uri URI of the element.
1170 * @param[in] value Whether a value is expected in the element.
1171 * @param[out] evnp Parsed envelope (opaque node).
1172 * @return LY_SUCCESS on success.
1173 * @return LY_ENOT if the specified element did not match.
1174 * @return LY_ERR value on error.
1175 */
Michal Vasko1bf09392020-03-27 12:38:10 +01001176static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +01001177lydxml_envelope(struct lyxml_ctx *xmlctx, const char *name, const char *uri, ly_bool value, struct lyd_node **envp)
Michal Vasko1bf09392020-03-27 12:38:10 +01001178{
Michal Vaskoe0665742021-02-11 11:08:44 +01001179 LY_ERR rc = LY_SUCCESS;
1180 const struct lyxml_ns *ns;
Radek Krejci1798aae2020-07-14 13:26:06 +02001181 struct lyd_attr *attr = NULL;
Michal Vasko1bf09392020-03-27 12:38:10 +01001182 const char *prefix;
1183 size_t prefix_len;
1184
Michal Vaskof048ba52023-06-13 10:40:43 +02001185 if (xmlctx->status != LYXML_ELEMENT) {
1186 /* nothing to parse */
1187 return LY_ENOT;
1188 }
1189
Michal Vasko1bf09392020-03-27 12:38:10 +01001190 if (ly_strncmp(name, xmlctx->name, xmlctx->name_len)) {
1191 /* not the expected element */
Michal Vaskoe0665742021-02-11 11:08:44 +01001192 return LY_ENOT;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001193 }
1194
1195 prefix = xmlctx->prefix;
1196 prefix_len = xmlctx->prefix_len;
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001197 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
Michal Vaskoa8edff02020-03-27 14:47:01 +01001198 if (!ns) {
aPiecek77a4f502024-07-03 08:34:48 +02001199 lydxml_log_namespace_err(xmlctx, prefix, prefix_len, NULL, 0);
Michal Vaskoe0665742021-02-11 11:08:44 +01001200 return LY_EVALID;
1201 } else if (strcmp(ns->uri, uri)) {
1202 /* different namespace */
1203 return LY_ENOT;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001204 }
1205
1206 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
1207
1208 /* create attributes */
1209 if (xmlctx->status == LYXML_ATTRIBUTE) {
1210 LY_CHECK_RET(lydxml_attrs(xmlctx, &attr));
1211 }
1212
Michal Vaskoe0665742021-02-11 11:08:44 +01001213 assert(xmlctx->status == LYXML_ELEM_CONTENT);
1214 if (!value && !xmlctx->ws_only) {
1215 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected value \"%.*s\" in the \"%s\" element.",
Radek Krejci422afb12021-03-04 16:38:16 +01001216 (int)xmlctx->value_len, xmlctx->value, name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001217 rc = LY_EVALID;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001218 goto cleanup;
Michal Vaskoe0665742021-02-11 11:08:44 +01001219 }
Michal Vaskoa8edff02020-03-27 14:47:01 +01001220
1221 /* create node */
Michal Vaskoe0665742021-02-11 11:08:44 +01001222 rc = lyd_create_opaq(xmlctx->ctx, name, strlen(name), prefix, prefix_len, uri, strlen(uri), xmlctx->value,
Radek Krejci8df109d2021-04-23 12:19:08 +02001223 xmlctx->ws_only ? 0 : xmlctx->value_len, NULL, LY_VALUE_XML, NULL, 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +01001224 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoa8edff02020-03-27 14:47:01 +01001225
1226 /* assign atributes */
Michal Vaskoe0665742021-02-11 11:08:44 +01001227 ((struct lyd_node_opaq *)(*envp))->attr = attr;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001228 attr = NULL;
1229
Michal Vaskoe0665742021-02-11 11:08:44 +01001230 /* parser next element */
1231 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
Michal Vaskoa8edff02020-03-27 14:47:01 +01001232
Michal Vaskoe0665742021-02-11 11:08:44 +01001233cleanup:
1234 lyd_free_attr_siblings(xmlctx->ctx, attr);
1235 if (rc) {
1236 lyd_free_tree(*envp);
1237 *envp = NULL;
1238 }
1239 return rc;
1240}
1241
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001242LY_ERR
1243lyd_parse_xml(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
1244 struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, uint32_t int_opts,
1245 struct ly_set *parsed, ly_bool *subtree_sibling, struct lyd_ctx **lydctx_p)
1246{
Michal Vaskod027f382023-02-10 09:13:25 +01001247 LY_ERR r, rc = LY_SUCCESS;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001248 struct lyd_xml_ctx *lydctx;
Michal Vasko64592692023-06-12 13:50:11 +02001249 ly_bool parsed_data_nodes = 0, close_elem = 0;
1250 struct lyd_node *act = NULL;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001251 enum LYXML_PARSER_STATUS status;
1252
1253 assert(ctx && in && lydctx_p);
1254 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
1255 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
1256
1257 /* init context */
1258 lydctx = calloc(1, sizeof *lydctx);
1259 LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
1260 LY_CHECK_GOTO(rc = lyxml_ctx_new(ctx, in, &lydctx->xmlctx), cleanup);
1261 lydctx->parse_opts = parse_opts;
1262 lydctx->val_opts = val_opts;
1263 lydctx->int_opts = int_opts;
1264 lydctx->free = lyd_xml_ctx_free;
1265 lydctx->ext = ext;
1266
1267 /* find the operation node if it exists already */
1268 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup);
1269
Michal Vasko64592692023-06-12 13:50:11 +02001270 if ((int_opts & LYD_INTOPT_RPC) && (int_opts & LYD_INTOPT_ACTION)) {
1271 /* can be either, try to parse "action" */
1272 if (!lydxml_envelope(lydctx->xmlctx, "action", "urn:ietf:params:xml:ns:yang:1", 0, &act)) {
1273 close_elem = 1;
1274 int_opts &= ~LYD_INTOPT_RPC;
1275 }
1276 }
1277
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001278 /* parse XML data */
1279 while (lydctx->xmlctx->status == LYXML_ELEMENT) {
Michal Vaskod027f382023-02-10 09:13:25 +01001280 r = lydxml_subtree_r(lydctx, parent, first_p, parsed);
1281 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
1282
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001283 parsed_data_nodes = 1;
1284
1285 if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
1286 break;
1287 }
1288 }
1289
Michal Vasko64592692023-06-12 13:50:11 +02001290 /* close an opened element */
1291 if (close_elem) {
1292 if (lydctx->xmlctx->status != LYXML_ELEM_CLOSE) {
1293 assert(lydctx->xmlctx->status == LYXML_ELEMENT);
1294 LOGVAL(lydctx->xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\".",
1295 (int)lydctx->xmlctx->name_len, lydctx->xmlctx->name);
1296 rc = LY_EVALID;
1297 goto cleanup;
1298 }
1299
1300 LY_CHECK_GOTO(rc = lyxml_ctx_next(lydctx->xmlctx), cleanup);
1301 }
1302
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001303 /* check final state */
1304 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && (lydctx->xmlctx->status == LYXML_ELEMENT)) {
1305 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
Michal Vaskod027f382023-02-10 09:13:25 +01001306 r = LY_EVALID;
1307 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001308 }
1309 if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) {
1310 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
Michal Vaskod027f382023-02-10 09:13:25 +01001311 r = LY_EVALID;
1312 LY_DPARSER_ERR_GOTO(r, rc = r, lydctx, cleanup);
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001313 }
1314
1315 if (!parsed_data_nodes) {
1316 /* no data nodes were parsed */
1317 lydctx->op_node = NULL;
1318 }
1319
1320 if (parse_opts & LYD_PARSE_SUBTREE) {
1321 /* check for a sibling element */
1322 assert(subtree_sibling);
1323 if (!lyxml_ctx_peek(lydctx->xmlctx, &status) && (status == LYXML_ELEMENT)) {
1324 *subtree_sibling = 1;
1325 } else {
1326 *subtree_sibling = 0;
1327 }
1328 }
1329
1330cleanup:
1331 /* there should be no unres stored if validation should be skipped */
1332 assert(!(parse_opts & LYD_PARSE_ONLY) || (!lydctx->node_types.count && !lydctx->meta_types.count &&
1333 !lydctx->node_when.count));
1334
Michal Vasko64592692023-06-12 13:50:11 +02001335 lyd_free_tree(act);
Michal Vaskod027f382023-02-10 09:13:25 +01001336 if (rc && (!(lydctx->val_opts & LYD_VALIDATE_MULTI_ERROR) || (rc != LY_EVALID))) {
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001337 lyd_xml_ctx_free((struct lyd_ctx *)lydctx);
1338 } else {
1339 *lydctx_p = (struct lyd_ctx *)lydctx;
1340
1341 /* the XML context is no more needed, freeing it also stops logging line numbers which would be confusing now */
1342 lyxml_ctx_free(lydctx->xmlctx);
1343 lydctx->xmlctx = NULL;
1344 }
1345 return rc;
1346}
1347
Michal Vaskoe0665742021-02-11 11:08:44 +01001348/**
1349 * @brief Parse all expected non-data XML elements of a NETCONF rpc message.
1350 *
1351 * @param[in] xmlctx XML parser context.
1352 * @param[out] evnp Parsed envelope(s) (opaque node).
1353 * @param[out] int_opts Internal options for parsing the rest of YANG data.
1354 * @param[out] close_elem Number of parsed opened elements that need to be closed.
1355 * @return LY_SUCCESS on success.
1356 * @return LY_ERR value on error.
1357 */
1358static LY_ERR
1359lydxml_env_netconf_rpc(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem)
1360{
1361 LY_ERR rc = LY_SUCCESS, r;
1362 struct lyd_node *child;
1363
1364 assert(envp && !*envp);
1365
1366 /* parse "rpc" */
1367 r = lydxml_envelope(xmlctx, "rpc", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +01001368 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1369
1370 /* parse "action", if any */
1371 r = lydxml_envelope(xmlctx, "action", "urn:ietf:params:xml:ns:yang:1", 0, &child);
1372 if (r == LY_SUCCESS) {
1373 /* insert */
aPiecek1462ab12024-02-07 09:13:29 +01001374 lyd_insert_node(*envp, NULL, child, LYD_INSERT_NODE_DEFAULT);
Michal Vaskoe0665742021-02-11 11:08:44 +01001375
1376 /* NETCONF action */
1377 *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_ACTION;
1378 *close_elem = 2;
1379 } else if (r == LY_ENOT) {
1380 /* NETCONF RPC */
1381 *int_opts = LYD_INTOPT_NO_SIBLINGS | LYD_INTOPT_RPC;
1382 *close_elem = 1;
1383 } else {
1384 rc = r;
1385 goto cleanup;
1386 }
1387
1388cleanup:
1389 if (rc) {
1390 lyd_free_tree(*envp);
1391 *envp = NULL;
1392 }
1393 return rc;
1394}
1395
1396/**
Michal Vaskoe0665742021-02-11 11:08:44 +01001397 * @brief Parse an XML element as an opaque node subtree.
1398 *
1399 * @param[in] xmlctx XML parser context.
1400 * @param[in] parent Parent to append nodes to.
1401 * @return LY_ERR value.
1402 */
1403static LY_ERR
1404lydxml_opaq_r(struct lyxml_ctx *xmlctx, struct lyd_node *parent)
Michal Vaskoa8edff02020-03-27 14:47:01 +01001405{
Michal Vaskoe0665742021-02-11 11:08:44 +01001406 LY_ERR rc = LY_SUCCESS;
1407 const struct lyxml_ns *ns;
1408 struct lyd_attr *attr = NULL;
Michal Vaskoa16dbb42023-08-15 15:20:25 +02001409 struct lyd_node *node = NULL;
1410 struct lyd_node_opaq *opaq;
1411 const char *name, *prefix, *value = NULL;
1412 size_t name_len, prefix_len, value_len;
1413 ly_bool ws_only, dynamic = 0;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001414
Michal Vaskoe0665742021-02-11 11:08:44 +01001415 assert(xmlctx->status == LYXML_ELEMENT);
Michal Vaskoa8edff02020-03-27 14:47:01 +01001416
Michal Vaskoe0665742021-02-11 11:08:44 +01001417 name = xmlctx->name;
1418 name_len = xmlctx->name_len;
1419 prefix = xmlctx->prefix;
1420 prefix_len = xmlctx->prefix_len;
1421 ns = lyxml_ns_get(&xmlctx->ns, prefix, prefix_len);
1422 if (!ns) {
aPiecek77a4f502024-07-03 08:34:48 +02001423 lydxml_log_namespace_err(xmlctx, prefix, prefix_len, NULL, 0);
Michal Vaskoe0665742021-02-11 11:08:44 +01001424 return LY_EVALID;
1425 }
Michal Vaskoa8edff02020-03-27 14:47:01 +01001426
Michal Vaskoe0665742021-02-11 11:08:44 +01001427 LY_CHECK_RET(lyxml_ctx_next(xmlctx));
Michal Vaskoa8edff02020-03-27 14:47:01 +01001428
Michal Vaskoe0665742021-02-11 11:08:44 +01001429 /* create attributes */
1430 if (xmlctx->status == LYXML_ATTRIBUTE) {
1431 LY_CHECK_RET(lydxml_attrs(xmlctx, &attr));
1432 }
1433
Michal Vaskoa16dbb42023-08-15 15:20:25 +02001434 /* remember the value */
Michal Vaskoe0665742021-02-11 11:08:44 +01001435 assert(xmlctx->status == LYXML_ELEM_CONTENT);
Michal Vaskoa16dbb42023-08-15 15:20:25 +02001436 value = xmlctx->value;
1437 value_len = xmlctx->value_len;
1438 ws_only = xmlctx->ws_only;
1439 dynamic = xmlctx->dynamic;
1440 if (dynamic) {
1441 xmlctx->dynamic = 0;
1442 }
1443
1444 /* create the node without value */
1445 rc = lyd_create_opaq(xmlctx->ctx, name, name_len, prefix, prefix_len, ns->uri, strlen(ns->uri), NULL, 0, NULL,
1446 LY_VALUE_XML, NULL, 0, &node);
Michal Vaskoe0665742021-02-11 11:08:44 +01001447 LY_CHECK_GOTO(rc, cleanup);
1448
1449 /* assign atributes */
Michal Vaskoa16dbb42023-08-15 15:20:25 +02001450 ((struct lyd_node_opaq *)node)->attr = attr;
Michal Vaskoe0665742021-02-11 11:08:44 +01001451 attr = NULL;
1452
1453 /* parser next element */
1454 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
1455
1456 /* parse all the descendants */
1457 while (xmlctx->status == LYXML_ELEMENT) {
Michal Vaskoa16dbb42023-08-15 15:20:25 +02001458 rc = lydxml_opaq_r(xmlctx, node);
Michal Vaskoe0665742021-02-11 11:08:44 +01001459 LY_CHECK_GOTO(rc, cleanup);
1460 }
1461
1462 /* insert */
aPiecek1462ab12024-02-07 09:13:29 +01001463 lyd_insert_node(parent, NULL, node, LYD_INSERT_NODE_LAST);
Michal Vaskoa16dbb42023-08-15 15:20:25 +02001464
1465 /* update the value */
1466 opaq = (struct lyd_node_opaq *)node;
1467 if (opaq->child) {
1468 if (!ws_only) {
1469 LOGVAL(xmlctx->ctx, LYVE_SYNTAX_XML, "Mixed XML content node \"%s\" found, not supported.", LYD_NAME(node));
1470 rc = LY_EVALID;
1471 goto cleanup;
1472 }
1473 } else if (value_len) {
1474 lydict_remove(xmlctx->ctx, opaq->value);
1475 if (dynamic) {
1476 LY_CHECK_GOTO(rc = lydict_insert_zc(xmlctx->ctx, (char *)value, &opaq->value), cleanup);
1477 dynamic = 0;
1478 } else {
1479 LY_CHECK_GOTO(rc = lydict_insert(xmlctx->ctx, value, value_len, &opaq->value), cleanup);
1480 }
1481 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001482
1483cleanup:
1484 lyd_free_attr_siblings(xmlctx->ctx, attr);
Michal Vaskoa16dbb42023-08-15 15:20:25 +02001485 if (dynamic) {
1486 free((char *)value);
1487 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001488 if (rc) {
Michal Vaskoa16dbb42023-08-15 15:20:25 +02001489 lyd_free_tree(node);
Michal Vaskoe0665742021-02-11 11:08:44 +01001490 }
1491 return rc;
1492}
1493
1494/**
1495 * @brief Parse all expected non-data XML elements of the error-info element in NETCONF rpc-reply message.
1496 *
1497 * @param[in] xmlctx XML parser context.
1498 * @param[in] parent Parent to append nodes to.
1499 * @return LY_ERR value.
1500 */
1501static LY_ERR
1502lydxml_env_netconf_rpc_reply_error_info(struct lyxml_ctx *xmlctx, struct lyd_node *parent)
1503{
1504 LY_ERR r;
1505 struct lyd_node *child, *iter;
Michal Vaskoe0665742021-02-11 11:08:44 +01001506 ly_bool no_dup;
1507
1508 /* there must be some child */
1509 if (xmlctx->status == LYXML_ELEM_CLOSE) {
1510 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"error-info\".");
1511 return LY_EVALID;
1512 }
1513
1514 while (xmlctx->status == LYXML_ELEMENT) {
1515 child = NULL;
1516
1517 /*
1518 * session-id
1519 */
1520 r = lydxml_envelope(xmlctx, "session-id", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1521 if (r == LY_SUCCESS) {
1522 no_dup = 1;
1523 goto check_child;
1524 } else if (r != LY_ENOT) {
1525 goto error;
1526 }
1527
1528 /*
1529 * bad-attribute
1530 */
1531 r = lydxml_envelope(xmlctx, "bad-attribute", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1532 if (r == LY_SUCCESS) {
1533 no_dup = 1;
1534 goto check_child;
1535 } else if (r != LY_ENOT) {
1536 goto error;
1537 }
1538
1539 /*
1540 * bad-element
1541 */
1542 r = lydxml_envelope(xmlctx, "bad-element", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1543 if (r == LY_SUCCESS) {
1544 no_dup = 1;
1545 goto check_child;
1546 } else if (r != LY_ENOT) {
1547 goto error;
1548 }
1549
1550 /*
1551 * bad-namespace
1552 */
1553 r = lydxml_envelope(xmlctx, "bad-namespace", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1554 if (r == LY_SUCCESS) {
1555 no_dup = 1;
1556 goto check_child;
1557 } else if (r != LY_ENOT) {
1558 goto error;
1559 }
1560
1561 if (r == LY_ENOT) {
1562 assert(xmlctx->status == LYXML_ELEMENT);
1563
Michal Vasko845ba392022-06-16 07:52:13 +02001564 /* custom elements, parse all the siblings */
1565 while (xmlctx->status == LYXML_ELEMENT) {
1566 LY_CHECK_GOTO(r = lydxml_opaq_r(xmlctx, parent), error);
1567 LY_CHECK_GOTO(r = lyxml_ctx_next(xmlctx), error);
1568 }
Michal Vasko6398eaf2022-01-10 10:12:30 +01001569 continue;
Michal Vaskoe0665742021-02-11 11:08:44 +01001570 }
1571
1572check_child:
1573 /* check for duplicates */
1574 if (no_dup) {
1575 LY_LIST_FOR(lyd_child(parent), iter) {
1576 if ((((struct lyd_node_opaq *)iter)->name.name == ((struct lyd_node_opaq *)child)->name.name) &&
1577 (((struct lyd_node_opaq *)iter)->name.module_ns == ((struct lyd_node_opaq *)child)->name.module_ns)) {
1578 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Duplicate element \"%s\" in \"error-info\".",
1579 ((struct lyd_node_opaq *)child)->name.name);
1580 r = LY_EVALID;
1581 goto error;
1582 }
1583 }
1584 }
1585
1586 /* finish child parsing */
1587 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1588 assert(xmlctx->status == LYXML_ELEMENT);
1589 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"error-info\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001590 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001591 r = LY_EVALID;
1592 goto error;
1593 }
1594 LY_CHECK_GOTO(r = lyxml_ctx_next(xmlctx), error);
1595
1596 /* insert */
aPiecek1462ab12024-02-07 09:13:29 +01001597 lyd_insert_node(parent, NULL, child, LYD_INSERT_NODE_LAST);
Michal Vaskoe0665742021-02-11 11:08:44 +01001598 }
1599
1600 return LY_SUCCESS;
1601
1602error:
1603 lyd_free_tree(child);
1604 return r;
1605}
1606
1607/**
1608 * @brief Parse all expected non-data XML elements of the rpc-error element in NETCONF rpc-reply message.
1609 *
1610 * @param[in] xmlctx XML parser context.
1611 * @param[in] parent Parent to append nodes to.
1612 * @return LY_ERR value.
1613 */
1614static LY_ERR
1615lydxml_env_netconf_rpc_reply_error(struct lyxml_ctx *xmlctx, struct lyd_node *parent)
1616{
1617 LY_ERR r;
1618 struct lyd_node *child, *iter;
1619 const char *val;
1620 ly_bool no_dup;
1621
1622 /* there must be some child */
1623 if (xmlctx->status == LYXML_ELEM_CLOSE) {
1624 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"rpc-error\".");
1625 return LY_EVALID;
1626 }
1627
1628 while (xmlctx->status == LYXML_ELEMENT) {
1629 child = NULL;
1630
1631 /*
1632 * error-type
1633 */
1634 r = lydxml_envelope(xmlctx, "error-type", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1635 if (r == LY_SUCCESS) {
1636 val = ((struct lyd_node_opaq *)child)->value;
1637 if (strcmp(val, "transport") && strcmp(val, "rpc") && strcmp(val, "protocol") && strcmp(val, "application")) {
1638 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val,
1639 ((struct lyd_node_opaq *)child)->name.name);
1640 r = LY_EVALID;
1641 goto error;
1642 }
1643
1644 no_dup = 1;
1645 goto check_child;
1646 } else if (r != LY_ENOT) {
1647 goto error;
1648 }
1649
1650 /*
1651 * error-tag
1652 */
1653 r = lydxml_envelope(xmlctx, "error-tag", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1654 if (r == LY_SUCCESS) {
1655 val = ((struct lyd_node_opaq *)child)->value;
1656 if (strcmp(val, "in-use") && strcmp(val, "invalid-value") && strcmp(val, "too-big") &&
1657 strcmp(val, "missing-attribute") && strcmp(val, "bad-attribute") &&
1658 strcmp(val, "unknown-attribute") && strcmp(val, "missing-element") && strcmp(val, "bad-element") &&
1659 strcmp(val, "unknown-element") && strcmp(val, "unknown-namespace") && strcmp(val, "access-denied") &&
1660 strcmp(val, "lock-denied") && strcmp(val, "resource-denied") && strcmp(val, "rollback-failed") &&
1661 strcmp(val, "data-exists") && strcmp(val, "data-missing") && strcmp(val, "operation-not-supported") &&
1662 strcmp(val, "operation-failed") && strcmp(val, "malformed-message")) {
1663 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val,
1664 ((struct lyd_node_opaq *)child)->name.name);
1665 r = LY_EVALID;
1666 goto error;
1667 }
1668
1669 no_dup = 1;
1670 goto check_child;
1671 } else if (r != LY_ENOT) {
1672 goto error;
1673 }
1674
1675 /*
1676 * error-severity
1677 */
1678 r = lydxml_envelope(xmlctx, "error-severity", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1679 if (r == LY_SUCCESS) {
1680 val = ((struct lyd_node_opaq *)child)->value;
1681 if (strcmp(val, "error") && strcmp(val, "warning")) {
1682 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Invalid value \"%s\" of element \"%s\".", val,
1683 ((struct lyd_node_opaq *)child)->name.name);
1684 r = LY_EVALID;
1685 goto error;
1686 }
1687
1688 no_dup = 1;
1689 goto check_child;
1690 } else if (r != LY_ENOT) {
1691 goto error;
1692 }
1693
1694 /*
1695 * error-app-tag
1696 */
1697 r = lydxml_envelope(xmlctx, "error-app-tag", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1698 if (r == LY_SUCCESS) {
1699 no_dup = 1;
1700 goto check_child;
1701 } else if (r != LY_ENOT) {
1702 goto error;
1703 }
1704
1705 /*
1706 * error-path
1707 */
1708 r = lydxml_envelope(xmlctx, "error-path", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1709 if (r == LY_SUCCESS) {
1710 no_dup = 1;
1711 goto check_child;
1712 } else if (r != LY_ENOT) {
1713 goto error;
1714 }
1715
1716 /*
1717 * error-message
1718 */
1719 r = lydxml_envelope(xmlctx, "error-message", "urn:ietf:params:xml:ns:netconf:base:1.0", 1, &child);
1720 if (r == LY_SUCCESS) {
1721 no_dup = 1;
1722 goto check_child;
1723 } else if (r != LY_ENOT) {
1724 goto error;
1725 }
1726
1727 /* error-info */
1728 r = lydxml_envelope(xmlctx, "error-info", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child);
1729 if (r == LY_SUCCESS) {
1730 /* parse all the descendants */
1731 LY_CHECK_GOTO(r = lydxml_env_netconf_rpc_reply_error_info(xmlctx, child), error);
1732
1733 no_dup = 0;
1734 goto check_child;
1735 } else if (r != LY_ENOT) {
1736 goto error;
1737 }
1738
1739 if (r == LY_ENOT) {
1740 assert(xmlctx->status == LYXML_ELEMENT);
1741 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"rpc-error\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001742 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001743 r = LY_EVALID;
1744 goto error;
1745 }
1746
1747check_child:
1748 /* check for duplicates */
1749 if (no_dup) {
1750 LY_LIST_FOR(lyd_child(parent), iter) {
1751 if ((((struct lyd_node_opaq *)iter)->name.name == ((struct lyd_node_opaq *)child)->name.name) &&
1752 (((struct lyd_node_opaq *)iter)->name.module_ns == ((struct lyd_node_opaq *)child)->name.module_ns)) {
1753 LOGVAL(xmlctx->ctx, LYVE_REFERENCE, "Duplicate element \"%s\" in \"rpc-error\".",
1754 ((struct lyd_node_opaq *)child)->name.name);
1755 r = LY_EVALID;
1756 goto error;
1757 }
1758 }
1759 }
1760
1761 /* finish child parsing */
1762 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1763 assert(xmlctx->status == LYXML_ELEMENT);
1764 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"rpc-error\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001765 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001766 r = LY_EVALID;
1767 goto error;
1768 }
1769 LY_CHECK_GOTO(r = lyxml_ctx_next(xmlctx), error);
1770
1771 /* insert */
aPiecek1462ab12024-02-07 09:13:29 +01001772 lyd_insert_node(parent, NULL, child, LYD_INSERT_NODE_LAST);
Michal Vaskoe0665742021-02-11 11:08:44 +01001773 }
1774
1775 return LY_SUCCESS;
1776
1777error:
1778 lyd_free_tree(child);
1779 return r;
1780}
1781
1782/**
1783 * @brief Parse all expected non-data XML elements of a NETCONF rpc-reply message.
1784 *
1785 * @param[in] xmlctx XML parser context.
1786 * @param[out] evnp Parsed envelope(s) (opaque node).
1787 * @param[out] int_opts Internal options for parsing the rest of YANG data.
1788 * @param[out] close_elem Number of parsed opened elements that need to be closed.
1789 * @return LY_SUCCESS on success.
1790 * @return LY_ERR value on error.
1791 */
1792static LY_ERR
1793lydxml_env_netconf_reply(struct lyxml_ctx *xmlctx, struct lyd_node **envp, uint32_t *int_opts, uint32_t *close_elem)
1794{
1795 LY_ERR rc = LY_SUCCESS, r;
1796 struct lyd_node *child = NULL;
1797 const char *parsed_elem = NULL;
1798
1799 assert(envp && !*envp);
1800
1801 /* parse "rpc-reply" */
1802 r = lydxml_envelope(xmlctx, "rpc-reply", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, envp);
Michal Vaskoe0665742021-02-11 11:08:44 +01001803 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
1804
1805 /* there must be some child */
1806 if (xmlctx->status == LYXML_ELEM_CLOSE) {
1807 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Missing child elements of \"rpc-reply\".");
1808 rc = LY_EVALID;
Michal Vaskocf770e22020-08-12 13:21:43 +02001809 goto cleanup;
Michal Vaskoa8edff02020-03-27 14:47:01 +01001810 }
1811
Michal Vaskoe0665742021-02-11 11:08:44 +01001812 /* try to parse "ok" */
1813 r = lydxml_envelope(xmlctx, "ok", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child);
1814 if (r == LY_SUCCESS) {
1815 /* insert */
aPiecek1462ab12024-02-07 09:13:29 +01001816 lyd_insert_node(*envp, NULL, child, LYD_INSERT_NODE_LAST);
Michal Vaskoe0665742021-02-11 11:08:44 +01001817
1818 /* finish child parsing */
1819 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1820 assert(xmlctx->status == LYXML_ELEMENT);
1821 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\" of \"ok\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001822 (int)xmlctx->name_len, xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001823 rc = LY_EVALID;
1824 goto cleanup;
1825 }
1826 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
1827
1828 /* success */
1829 parsed_elem = "ok";
1830 goto finish;
1831 } else if (r != LY_ENOT) {
1832 rc = r;
1833 goto cleanup;
Michal Vasko2552ea32020-12-08 15:32:34 +01001834 }
1835
Michal Vaskoe0665742021-02-11 11:08:44 +01001836 /* try to parse all "rpc-error" elements */
1837 while (xmlctx->status == LYXML_ELEMENT) {
1838 r = lydxml_envelope(xmlctx, "rpc-error", "urn:ietf:params:xml:ns:netconf:base:1.0", 0, &child);
1839 if (r == LY_ENOT) {
1840 break;
1841 } else if (r) {
1842 rc = r;
1843 goto cleanup;
1844 }
1845
1846 /* insert */
aPiecek1462ab12024-02-07 09:13:29 +01001847 lyd_insert_node(*envp, NULL, child, LYD_INSERT_NODE_LAST);
Michal Vaskoe0665742021-02-11 11:08:44 +01001848
1849 /* parse all children of "rpc-error" */
1850 LY_CHECK_GOTO(rc = lydxml_env_netconf_rpc_reply_error(xmlctx, child), cleanup);
1851
1852 /* finish child parsing */
1853 assert(xmlctx->status == LYXML_ELEM_CLOSE);
1854 LY_CHECK_GOTO(rc = lyxml_ctx_next(xmlctx), cleanup);
1855
1856 parsed_elem = "rpc-error";
Michal Vasko2552ea32020-12-08 15:32:34 +01001857 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001858
1859finish:
1860 if (parsed_elem) {
1861 /* NETCONF rpc-reply with no data */
1862 if (xmlctx->status != LYXML_ELEM_CLOSE) {
1863 assert(xmlctx->status == LYXML_ELEMENT);
1864 LOGVAL(xmlctx->ctx, LYVE_SYNTAX, "Unexpected sibling element \"%.*s\" of \"%s\".",
Radek Krejci422afb12021-03-04 16:38:16 +01001865 (int)xmlctx->name_len, xmlctx->name, parsed_elem);
Michal Vaskoe0665742021-02-11 11:08:44 +01001866 rc = LY_EVALID;
1867 goto cleanup;
1868 }
1869 }
1870
1871 /* NETCONF rpc-reply */
Michal Vasko1d991fd2021-07-09 13:14:40 +02001872 *int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_REPLY;
Michal Vaskoe0665742021-02-11 11:08:44 +01001873 *close_elem = 1;
1874
1875cleanup:
1876 if (rc) {
1877 lyd_free_tree(*envp);
1878 *envp = NULL;
1879 }
1880 return rc;
1881}
1882
Michal Vasko2552ea32020-12-08 15:32:34 +01001883LY_ERR
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001884lyd_parse_xml_netconf(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
Radek Krejcif16e2542021-02-17 15:39:23 +01001885 struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, enum lyd_type data_type,
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001886 struct lyd_node **envp, struct ly_set *parsed, struct lyd_ctx **lydctx_p)
Michal Vasko2552ea32020-12-08 15:32:34 +01001887{
Michal Vaskoe0665742021-02-11 11:08:44 +01001888 LY_ERR rc = LY_SUCCESS;
1889 struct lyd_xml_ctx *lydctx;
Michal Vasko4a1e3e82023-09-05 08:45:01 +02001890 struct lyd_node *node;
Michal Vasko2ca9f9e2021-07-02 09:21:36 +02001891 uint32_t i, int_opts = 0, close_elem = 0;
Michal Vaskoe0665742021-02-11 11:08:44 +01001892 ly_bool parsed_data_nodes = 0;
Michal Vasko2552ea32020-12-08 15:32:34 +01001893
Michal Vaskoe0665742021-02-11 11:08:44 +01001894 assert(ctx && in && lydctx_p);
1895 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
1896 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001897 assert(!(parse_opts & LYD_PARSE_SUBTREE));
1898
Michal Vaskoe0665742021-02-11 11:08:44 +01001899 /* init context */
1900 lydctx = calloc(1, sizeof *lydctx);
1901 LY_CHECK_ERR_RET(!lydctx, LOGMEM(ctx), LY_EMEM);
1902 LY_CHECK_GOTO(rc = lyxml_ctx_new(ctx, in, &lydctx->xmlctx), cleanup);
1903 lydctx->parse_opts = parse_opts;
1904 lydctx->val_opts = val_opts;
1905 lydctx->free = lyd_xml_ctx_free;
Radek Krejcif16e2542021-02-17 15:39:23 +01001906 lydctx->ext = ext;
Michal Vasko2552ea32020-12-08 15:32:34 +01001907
Michal Vaskoe0665742021-02-11 11:08:44 +01001908 switch (data_type) {
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001909 case LYD_TYPE_RPC_NETCONF:
Michal Vaskoe0665742021-02-11 11:08:44 +01001910 assert(!parent);
Michal Vaskod71c2d42022-04-29 09:50:44 +02001911 rc = lydxml_env_netconf_rpc(lydctx->xmlctx, envp, &int_opts, &close_elem);
1912 if (rc == LY_ENOT) {
1913 LOGVAL(ctx, LYVE_DATA, "Missing NETCONF <rpc> envelope or in incorrect namespace.");
1914 }
1915 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +01001916 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001917 case LYD_TYPE_NOTIF_NETCONF:
1918 assert(!parent);
Michal Vasko4a1e3e82023-09-05 08:45:01 +02001919
1920 /* parse "notification" */
1921 rc = lydxml_envelope(lydctx->xmlctx, "notification", "urn:ietf:params:xml:ns:netconf:notification:1.0", 0, envp);
Michal Vaskod71c2d42022-04-29 09:50:44 +02001922 if (rc == LY_ENOT) {
1923 LOGVAL(ctx, LYVE_DATA, "Missing NETCONF <notification> envelope or in incorrect namespace.");
1924 }
1925 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko4a1e3e82023-09-05 08:45:01 +02001926
1927 /* NETCONF notification */
1928 int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_NOTIF | LYD_INTOPT_EVENTTIME;
1929 close_elem = 1;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001930 break;
1931 case LYD_TYPE_REPLY_NETCONF:
Michal Vaskoe0665742021-02-11 11:08:44 +01001932 assert(parent);
Michal Vaskod71c2d42022-04-29 09:50:44 +02001933 rc = lydxml_env_netconf_reply(lydctx->xmlctx, envp, &int_opts, &close_elem);
1934 if (rc == LY_ENOT) {
1935 LOGVAL(ctx, LYVE_DATA, "Missing NETCONF <rpc-reply> envelope or in incorrect namespace.");
1936 }
1937 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +01001938 break;
Michal Vasko820efe82023-05-12 15:47:43 +02001939 case LYD_TYPE_RPC_RESTCONF:
1940 assert(parent);
1941
1942 /* parse "input" */
Michal Vasko420cc252023-08-24 08:14:24 +02001943 rc = lydxml_envelope(lydctx->xmlctx, "input", lyd_node_module(parent)->ns, 0, envp);
Michal Vasko820efe82023-05-12 15:47:43 +02001944 if (rc == LY_ENOT) {
1945 LOGVAL(ctx, LYVE_DATA, "Missing RESTCONF \"input\" object or in incorrect namespace.");
1946 }
1947 LY_CHECK_GOTO(rc, cleanup);
1948
1949 int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_RPC | LYD_INTOPT_ACTION;
1950 close_elem = 1;
1951 break;
1952 case LYD_TYPE_REPLY_RESTCONF:
1953 assert(parent);
1954
1955 /* parse "output" */
Michal Vasko420cc252023-08-24 08:14:24 +02001956 rc = lydxml_envelope(lydctx->xmlctx, "output", lyd_node_module(parent)->ns, 0, envp);
Michal Vasko820efe82023-05-12 15:47:43 +02001957 if (rc == LY_ENOT) {
1958 LOGVAL(ctx, LYVE_DATA, "Missing RESTCONF \"output\" object or in incorrect namespace.");
1959 }
1960 LY_CHECK_GOTO(rc, cleanup);
1961
1962 int_opts = LYD_INTOPT_WITH_SIBLINGS | LYD_INTOPT_REPLY;
1963 close_elem = 1;
1964 break;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001965 default:
1966 LOGINT(ctx);
1967 rc = LY_EINT;
1968 goto cleanup;
Michal Vaskoe0665742021-02-11 11:08:44 +01001969 }
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001970
Michal Vaskoe0665742021-02-11 11:08:44 +01001971 lydctx->int_opts = int_opts;
1972
1973 /* find the operation node if it exists already */
1974 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lydctx->op_node), cleanup);
1975
1976 /* parse XML data */
1977 while (lydctx->xmlctx->status == LYXML_ELEMENT) {
1978 LY_CHECK_GOTO(rc = lydxml_subtree_r(lydctx, parent, first_p, parsed), cleanup);
1979 parsed_data_nodes = 1;
1980
1981 if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
1982 break;
1983 }
Michal Vasko2552ea32020-12-08 15:32:34 +01001984 }
1985
Michal Vaskoe0665742021-02-11 11:08:44 +01001986 /* close all opened elements */
1987 for (i = 0; i < close_elem; ++i) {
1988 if (lydctx->xmlctx->status != LYXML_ELEM_CLOSE) {
1989 assert(lydctx->xmlctx->status == LYXML_ELEMENT);
Radek Krejci422afb12021-03-04 16:38:16 +01001990 LOGVAL(lydctx->xmlctx->ctx, LYVE_SYNTAX, "Unexpected child element \"%.*s\".",
1991 (int)lydctx->xmlctx->name_len, lydctx->xmlctx->name);
Michal Vaskoe0665742021-02-11 11:08:44 +01001992 rc = LY_EVALID;
1993 goto cleanup;
1994 }
1995
1996 LY_CHECK_GOTO(rc = lyxml_ctx_next(lydctx->xmlctx), cleanup);
Michal Vasko4189c0f2020-08-13 09:05:22 +02001997 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001998
1999 /* check final state */
2000 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && (lydctx->xmlctx->status == LYXML_ELEMENT)) {
2001 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
2002 rc = LY_EVALID;
2003 goto cleanup;
2004 }
Michal Vaskoc939fdd2022-01-03 11:35:13 +01002005 if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lydctx->op_node) {
Michal Vaskoe0665742021-02-11 11:08:44 +01002006 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
2007 rc = LY_EVALID;
2008 goto cleanup;
2009 }
Michal Vasko4a1e3e82023-09-05 08:45:01 +02002010 if (int_opts & LYD_INTOPT_EVENTTIME) {
2011 /* parse as a child of the envelope */
2012 node = (*first_p)->prev;
2013 if (node->schema) {
2014 LOGVAL(ctx, LYVE_DATA, "Missing notification \"eventTime\" node.");
2015 rc = LY_EVALID;
2016 goto cleanup;
2017 } else {
2018 /* can be the only opaque node and an operation had to be parsed */
2019 assert(!strcmp(LYD_NAME(node), "eventTime") && (*first_p)->next);
Michal Vasko2e784f82024-01-11 09:51:22 +01002020 lyd_unlink(node);
Michal Vasko4a1e3e82023-09-05 08:45:01 +02002021 assert(*envp);
2022 lyd_insert_child(*envp, node);
2023 }
2024 }
Michal Vaskoe0665742021-02-11 11:08:44 +01002025
2026 if (!parsed_data_nodes) {
2027 /* no data nodes were parsed */
2028 lydctx->op_node = NULL;
Michal Vaskoa8edff02020-03-27 14:47:01 +01002029 }
2030
2031cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +01002032 /* there should be no unres stored if validation should be skipped */
2033 assert(!(parse_opts & LYD_PARSE_ONLY) || (!lydctx->node_types.count && !lydctx->meta_types.count &&
Michal Vaskoddd76592022-01-17 13:34:48 +01002034 !lydctx->node_when.count));
Michal Vaskoe0665742021-02-11 11:08:44 +01002035
2036 if (rc) {
2037 lyd_xml_ctx_free((struct lyd_ctx *)lydctx);
2038 } else {
2039 *lydctx_p = (struct lyd_ctx *)lydctx;
2040
2041 /* the XML context is no more needed, freeing it also stops logging line numbers which would be confusing now */
2042 lyxml_ctx_free(lydctx->xmlctx);
2043 lydctx->xmlctx = NULL;
Michal Vasko1ce933a2020-03-30 12:38:22 +02002044 }
Michal Vaskoe0665742021-02-11 11:08:44 +01002045 return rc;
Michal Vasko1ce933a2020-03-30 12:38:22 +02002046}