Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1 | /** |
| 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 Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 30 | #include "plugins_exts_internal.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * @brief internal context for XML YANG data parser. |
| 34 | * |
| 35 | * The leading part is compatible with the struct lyxml_context |
| 36 | */ |
| 37 | struct lyd_xml_ctx { |
| 38 | struct ly_ctx *ctx; /**< libyang context */ |
| 39 | uint64_t line; /**< number of the line being currently processed */ |
| 40 | enum LYXML_PARSER_STATUS status; /**< status providing information about the next expected object in input data */ |
| 41 | struct ly_set elements; /**< list of not-yet-closed elements */ |
| 42 | struct ly_set ns; /**< handled with LY_SET_OPT_USEASLIST */ |
| 43 | |
| 44 | uint16_t options; /**< various @ref dataparseroptions. */ |
| 45 | uint16_t path_len; /**< used bytes in the path buffer */ |
| 46 | #define LYD_PARSER_BUFSIZE 4078 |
| 47 | char path[LYD_PARSER_BUFSIZE]; /**< buffer for the generated path */ |
Radek Krejci | e553e6d | 2019-06-07 15:33:18 +0200 | [diff] [blame] | 48 | struct ly_set incomplete_type_validation; /**< set of nodes validated with LY_EINCOMPLETE result */ |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 49 | struct ly_set incomplete_type_validation_attrs; /**< set of attributes validated with LY_EINCOMPLETE result */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | /** |
Radek Krejci | aca7403 | 2019-06-04 08:53:06 +0200 | [diff] [blame] | 53 | * @brief XML-parser's implementation of ly_type_resolve_prefix() callback to provide mapping between prefixes used in the values to the schema |
| 54 | * via XML namespaces. |
| 55 | */ |
| 56 | static const struct lys_module * |
| 57 | lydxml_resolve_prefix(struct ly_ctx *ctx, const char *prefix, size_t prefix_len, void *parser) |
| 58 | { |
| 59 | const struct lyxml_ns *ns; |
| 60 | struct lyxml_context *xmlctx = (struct lyxml_context*)parser; |
| 61 | |
| 62 | ns = lyxml_ns_get(xmlctx, prefix, prefix_len); |
| 63 | if (!ns) { |
| 64 | return NULL; |
| 65 | } |
| 66 | |
| 67 | return ly_ctx_get_module_implemented_ns(ctx, ns->uri); |
| 68 | } |
| 69 | |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame^] | 70 | struct attr_data_s { |
| 71 | const char *prefix; |
| 72 | const char *name; |
| 73 | char *value; |
| 74 | size_t prefix_len; |
| 75 | size_t name_len; |
| 76 | size_t value_len; |
| 77 | int dynamic; |
| 78 | }; |
| 79 | |
Radek Krejci | aca7403 | 2019-06-04 08:53:06 +0200 | [diff] [blame] | 80 | /** |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 81 | * @brief Parse XML attributes of the XML element of YANG data. |
| 82 | * |
| 83 | * @param[in] ctx XML YANG data parser context. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 84 | * @param[in,out] data Pointer to the XML string representation of the YANG data to parse. |
| 85 | * @param[out] attributes Resulting list of the parsed attributes. XML namespace definitions are not parsed |
| 86 | * as attributes, they are stored internally in the parser context. |
| 87 | * @reutn LY_ERR value. |
| 88 | */ |
| 89 | static LY_ERR |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame^] | 90 | lydxml_attributes_parse(struct lyd_xml_ctx *ctx, const char **data, struct ly_set *attrs_data) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 91 | { |
| 92 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame^] | 93 | unsigned int u; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 94 | const char *prefix, *name; |
| 95 | size_t prefix_len, name_len; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame^] | 96 | struct attr_data_s *attr_data; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 97 | |
| 98 | while(ctx->status == LYXML_ATTRIBUTE && |
| 99 | lyxml_get_attribute((struct lyxml_context*)ctx, data, &prefix, &prefix_len, &name, &name_len) == LY_SUCCESS) { |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 100 | char *buffer = NULL; |
| 101 | size_t buffer_size = 0; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 102 | |
Radek Krejci | 17a78d8 | 2019-05-15 15:49:55 +0200 | [diff] [blame] | 103 | if (!name) { |
| 104 | /* seems like all the attrributes were internally processed as namespace definitions */ |
| 105 | continue; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 106 | } |
Radek Krejci | 17a78d8 | 2019-05-15 15:49:55 +0200 | [diff] [blame] | 107 | |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 108 | /* auxiliary store the prefix information and value string, because we have to wait with resolving prefix |
| 109 | * to the time when all the namespaces, defined in this element, are parsed. With the prefix we can find the |
| 110 | * annotation definition for the attribute and correctly process the value */ |
| 111 | attr_data = malloc(sizeof *attr_data); |
| 112 | attr_data->prefix = prefix; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame^] | 113 | attr_data->name = name; |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 114 | attr_data->prefix_len = prefix_len; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame^] | 115 | attr_data->name_len = name_len; |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 116 | ret = lyxml_get_string((struct lyxml_context *)ctx, data, &buffer, &buffer_size, &attr_data->value, &attr_data->value_len, &attr_data->dynamic); |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame^] | 117 | LY_CHECK_ERR_GOTO(ret, free(attr_data), error); |
| 118 | ly_set_add(attrs_data, attr_data, LY_SET_OPT_USEASLIST); |
| 119 | } |
| 120 | |
| 121 | return LY_SUCCESS; |
| 122 | |
| 123 | error: |
| 124 | for (u = 0; u < attrs_data->count; ++u) { |
| 125 | if (((struct attr_data_s*)attrs_data->objs[u])->dynamic) { |
| 126 | free(((struct attr_data_s*)attrs_data->objs[u])->value); |
| 127 | } |
| 128 | } |
| 129 | ly_set_erase(attrs_data, free); |
| 130 | return ret; |
| 131 | } |
| 132 | |
| 133 | static LY_ERR |
| 134 | lydxml_attributes(struct lyd_xml_ctx *ctx, struct ly_set *attrs_data, struct lyd_node *parent) |
| 135 | { |
| 136 | LY_ERR ret = LY_EVALID, rc; |
| 137 | struct lyd_attr *attr = NULL, *last = NULL; |
| 138 | const struct lyxml_ns *ns; |
| 139 | struct lys_module *mod; |
| 140 | |
| 141 | for (unsigned int u = 0; u < attrs_data->count; ++u) { |
| 142 | unsigned int v; |
| 143 | struct lysc_ext_instance *ant = NULL; |
| 144 | struct attr_data_s *attr_data = (struct attr_data_s*)attrs_data->objs[u]; |
| 145 | |
| 146 | if (!attr_data->prefix_len) { |
| 147 | /* in XML, all attributes must be prefixed |
| 148 | * TODO exception for NETCONF filters which are supposed to map to the ietf-netconf without prefix */ |
| 149 | if (ctx->options & LYD_OPT_STRICT) { |
| 150 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE, "Missing mandatory prefix for XML attribute \"%.*s\".", |
| 151 | attr_data->name_len, attr_data->name); |
| 152 | } |
| 153 | skip_attr: |
| 154 | if (attr_data->dynamic) { |
| 155 | free(attr_data->value); |
| 156 | attr_data->dynamic = 0; |
| 157 | } |
| 158 | continue; |
| 159 | } |
| 160 | |
| 161 | /* get namespace of the attribute to find its annotation definition */ |
| 162 | ns = lyxml_ns_get((struct lyxml_context *)ctx, attr_data->prefix, attr_data->prefix_len); |
| 163 | if (!ns) { |
| 164 | /* unknown namespace, ignore the attribute */ |
| 165 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", attr_data->prefix_len, attr_data->prefix); |
| 166 | goto cleanup; |
| 167 | } |
| 168 | mod = ly_ctx_get_module_implemented_ns(ctx->ctx, ns->uri); |
| 169 | if (!mod) { |
| 170 | /* module is not implemented or not present in the schema */ |
| 171 | if (ctx->options & LYD_OPT_STRICT) { |
| 172 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE, |
| 173 | "Unknown (or not implemented) YANG module with namespace \"%s\" for attribute \"%.*s%s%.*s\".", |
| 174 | ns, attr_data->prefix_len, attr_data->prefix, attr_data->prefix_len ? ":" : "", attr_data->name_len, attr_data->name); |
| 175 | } |
| 176 | goto skip_attr; |
| 177 | } |
| 178 | |
| 179 | LY_ARRAY_FOR(mod->compiled->exts, v) { |
| 180 | if (mod->compiled->exts[v].def->plugin == lyext_plugins_internal[LYEXT_PLUGIN_INTERNAL_ANNOTATION].plugin && |
| 181 | !strncmp(mod->compiled->exts[v].argument, attr_data->name, attr_data->name_len) && !mod->compiled->exts[v].argument[attr_data->name_len]) { |
| 182 | /* we have the annotation definition */ |
| 183 | ant = &mod->compiled->exts[v]; |
| 184 | break; |
| 185 | } |
| 186 | } |
| 187 | if (!ant) { |
| 188 | /* attribute is not defined as a metadata annotation (RFC 7952) */ |
| 189 | if (ctx->options & LYD_OPT_STRICT) { |
| 190 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE, "Annotation definition for attribute \"%s:%.*s\" not found.", |
| 191 | mod->name, attr_data->name_len, attr_data->name); |
| 192 | } |
| 193 | goto skip_attr; |
| 194 | } |
Radek Krejci | 17a78d8 | 2019-05-15 15:49:55 +0200 | [diff] [blame] | 195 | |
| 196 | attr = calloc(1, sizeof *attr); |
| 197 | LY_CHECK_ERR_GOTO(!attr, LOGMEM(ctx->ctx); ret = LY_EMEM, cleanup); |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame^] | 198 | attr->parent = parent; |
| 199 | attr->annotation = ant; |
| 200 | rc = lyd_value_parse_attr(attr, attr_data->value, attr_data->value_len, attr_data->dynamic, 0, lydxml_resolve_prefix, ctx, LYD_XML, NULL); |
| 201 | if (rc == LY_EINCOMPLETE) { |
| 202 | ly_set_add(&ctx->incomplete_type_validation_attrs, attr, LY_SET_OPT_USEASLIST); |
| 203 | } else if (rc) { |
| 204 | ret = rc; |
| 205 | free(attr); |
| 206 | goto cleanup; |
| 207 | } |
| 208 | attr_data->dynamic = 0; /* value eaten by lyd_value_parse_attr() */ |
| 209 | attr->name = lydict_insert(ctx->ctx, attr_data->name, attr_data->name_len); |
Radek Krejci | 17a78d8 | 2019-05-15 15:49:55 +0200 | [diff] [blame] | 210 | |
| 211 | if (last) { |
| 212 | last->next = attr; |
| 213 | } else { |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame^] | 214 | parent->attr = attr; |
Radek Krejci | 17a78d8 | 2019-05-15 15:49:55 +0200 | [diff] [blame] | 215 | } |
| 216 | last = attr; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 217 | } |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame^] | 218 | ret = LY_SUCCESS; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 219 | |
| 220 | cleanup: |
| 221 | |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame^] | 222 | for (unsigned int u = 0; u < attrs_data->count; ++u) { |
| 223 | if (((struct attr_data_s*)attrs_data->objs[u])->dynamic) { |
| 224 | free(((struct attr_data_s*)attrs_data->objs[u])->value); |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 225 | } |
| 226 | } |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame^] | 227 | ly_set_erase(attrs_data, free); |
| 228 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 229 | return ret; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * @brief Parse XML elements as children YANG data node of the specified parent node. |
| 234 | * |
| 235 | * @param[in] ctx XML YANG data parser context. |
| 236 | * @param[in] parent Parent node where the children are inserted. NULL in case of parsing top-level elements. |
| 237 | * @param[in,out] data Pointer to the XML string representation of the YANG data to parse. |
| 238 | * @param[out] node Resulting list of the parsed nodes. |
| 239 | * @reutn LY_ERR value. |
| 240 | */ |
| 241 | static LY_ERR |
| 242 | lydxml_nodes(struct lyd_xml_ctx *ctx, struct lyd_node_inner *parent, const char **data, struct lyd_node **node) |
| 243 | { |
| 244 | LY_ERR ret = LY_SUCCESS; |
| 245 | const char *prefix, *name; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 246 | size_t prefix_len, name_len; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame^] | 247 | struct ly_set attrs_data = {0}; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 248 | const struct lyxml_ns *ns; |
| 249 | const struct lysc_node *snode; |
| 250 | struct lys_module *mod; |
| 251 | unsigned int parents_count = ctx->elements.count; |
Radek Krejci | 710226d | 2019-07-24 17:24:59 +0200 | [diff] [blame] | 252 | struct lyd_node *cur = NULL, *prev = NULL, *last = NULL; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 253 | |
| 254 | (*node) = NULL; |
| 255 | |
| 256 | while(ctx->status == LYXML_ELEMENT) { |
| 257 | ret = lyxml_get_element((struct lyxml_context *)ctx, data, &prefix, &prefix_len, &name, &name_len); |
| 258 | LY_CHECK_GOTO(ret, cleanup); |
| 259 | if (!name) { |
| 260 | /* closing previous element */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 261 | if (ctx->elements.count < parents_count) { |
| 262 | /* all siblings parsed */ |
| 263 | break; |
| 264 | } else { |
| 265 | continue; |
| 266 | } |
| 267 | } |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 268 | if (ctx->status == LYXML_ATTRIBUTE) { |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame^] | 269 | LY_CHECK_GOTO(lydxml_attributes_parse(ctx, data, &attrs_data), error); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 270 | } |
| 271 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 272 | ns = lyxml_ns_get((struct lyxml_context *)ctx, prefix, prefix_len); |
| 273 | if (!ns) { |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame^] | 274 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE, "Unknown XML prefix \"%.*s\".", prefix_len, prefix); |
Radek Krejci | 1170214 | 2019-07-24 17:46:04 +0200 | [diff] [blame] | 275 | goto error; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 276 | } |
| 277 | mod = ly_ctx_get_module_implemented_ns(ctx->ctx, ns->uri); |
| 278 | if (!mod) { |
| 279 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE, "No module with namespace \"%s\" in the context.", ns->uri); |
Radek Krejci | 1170214 | 2019-07-24 17:46:04 +0200 | [diff] [blame] | 280 | goto error; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 281 | } |
| 282 | snode = lys_child(parent ? parent->schema : NULL, mod, name, name_len, 0, (ctx->options & LYD_OPT_RPCREPLY) ? LYS_GETNEXT_OUTPUT : 0); |
| 283 | if (!snode) { |
| 284 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_REFERENCE, "Element \"%.*s\" not found in the \"%s\" module.", name_len, name, mod->name); |
Radek Krejci | 1170214 | 2019-07-24 17:46:04 +0200 | [diff] [blame] | 285 | goto error; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | /* allocate new node */ |
| 289 | switch (snode->nodetype) { |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 290 | case LYS_ACTION: |
| 291 | if ((ctx->options & LYD_OPT_TYPEMASK) != LYD_OPT_RPC) { |
| 292 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_RESTRICTION, "Unexpected RPC/action element \"%.*s\" in %s data set.", |
| 293 | name_len, name, lyd_parse_options_type2str(ctx->options & LYD_OPT_TYPEMASK)); |
Radek Krejci | 1170214 | 2019-07-24 17:46:04 +0200 | [diff] [blame] | 294 | goto error; |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 295 | } |
| 296 | cur = calloc(1, sizeof(struct lyd_node_inner)); |
| 297 | break; |
| 298 | case LYS_NOTIF: |
| 299 | if ((ctx->options & LYD_OPT_TYPEMASK) != LYD_OPT_RPC) { |
| 300 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_RESTRICTION, "Unexpected Notification element \"%.*s\" in %s data set.", |
| 301 | name_len, name, lyd_parse_options_type2str(ctx->options)); |
Radek Krejci | 1170214 | 2019-07-24 17:46:04 +0200 | [diff] [blame] | 302 | goto error; |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 303 | } |
| 304 | cur = calloc(1, sizeof(struct lyd_node_inner)); |
| 305 | break; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 306 | case LYS_CONTAINER: |
| 307 | case LYS_LIST: |
| 308 | cur = calloc(1, sizeof(struct lyd_node_inner)); |
| 309 | break; |
| 310 | case LYS_LEAF: |
| 311 | case LYS_LEAFLIST: |
| 312 | cur = calloc(1, sizeof(struct lyd_node_term)); |
| 313 | break; |
| 314 | case LYS_ANYDATA: |
| 315 | case LYS_ANYXML: |
| 316 | cur = calloc(1, sizeof(struct lyd_node_any)); |
| 317 | break; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 318 | default: |
| 319 | LOGINT(ctx->ctx); |
Radek Krejci | 1170214 | 2019-07-24 17:46:04 +0200 | [diff] [blame] | 320 | ret = LY_EINT; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 321 | goto cleanup; |
| 322 | } |
| 323 | if (!(*node)) { |
| 324 | (*node) = cur; |
| 325 | } |
Radek Krejci | 710226d | 2019-07-24 17:24:59 +0200 | [diff] [blame] | 326 | last = cur; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 327 | cur->schema = snode; |
Radek Krejci | 710226d | 2019-07-24 17:24:59 +0200 | [diff] [blame] | 328 | cur->prev = cur; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 329 | cur->parent = parent; |
Radek Krejci | e92210c | 2019-05-17 15:53:35 +0200 | [diff] [blame] | 330 | if (parent) { |
Radek Krejci | 710226d | 2019-07-24 17:24:59 +0200 | [diff] [blame] | 331 | if (prev && cur->schema->nodetype == LYS_LEAF && (cur->schema->flags & LYS_KEY)) { |
| 332 | /* it is key and we need to insert it into a correct place */ |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 333 | struct lysc_node *key_s; |
Radek Krejci | 710226d | 2019-07-24 17:24:59 +0200 | [diff] [blame] | 334 | unsigned int cur_index, key_index; |
| 335 | struct lyd_node *key; |
| 336 | |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 337 | for (cur_index = 0, key_s = ((struct lysc_node_list*)parent->schema)->child; |
| 338 | key_s && key_s != cur->schema; |
| 339 | ++cur_index, key_s = key_s->next); |
| 340 | for (key = prev; |
| 341 | !(key->schema->flags & LYS_KEY) && key->prev != prev; |
| 342 | key = key->prev); |
Radek Krejci | 710226d | 2019-07-24 17:24:59 +0200 | [diff] [blame] | 343 | for (; key->schema->flags & LYS_KEY; key = key->prev) { |
Radek Krejci | 0fe9b51 | 2019-07-26 17:51:05 +0200 | [diff] [blame] | 344 | for (key_index = 0, key_s = ((struct lysc_node_list*)parent->schema)->child; |
| 345 | key_s && key_s != key->schema; |
| 346 | ++key_index, key_s = key_s->next); |
Radek Krejci | 710226d | 2019-07-24 17:24:59 +0200 | [diff] [blame] | 347 | if (key_index < cur_index) { |
| 348 | /* cur key is supposed to be placed after the key */ |
| 349 | cur->next = key->next; |
| 350 | cur->prev = key; |
| 351 | key->next = cur; |
| 352 | if (cur->next) { |
| 353 | cur->next->prev = cur; |
| 354 | } else { |
| 355 | parent->child->prev = cur; |
| 356 | } |
| 357 | break; |
| 358 | } |
| 359 | if (key->prev == prev) { |
| 360 | /* current key is supposed to be the first child from the current children */ |
| 361 | key = NULL; |
| 362 | break; |
| 363 | } |
| 364 | } |
| 365 | if (!key || !(key->schema->flags & LYS_KEY)) { |
| 366 | /* current key is supposed to be the first child from the current children */ |
| 367 | cur->next = parent->child; |
| 368 | cur->prev = parent->child->prev; |
| 369 | parent->child->prev = cur; |
| 370 | parent->child = cur; |
| 371 | } |
| 372 | if (cur->next) { |
| 373 | last = prev; |
| 374 | if (ctx->options & LYD_OPT_STRICT) { |
| 375 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_RESTRICTION, "Invalid position of the key \"%.*s\" in a list.", |
| 376 | name_len, name); |
Radek Krejci | 1170214 | 2019-07-24 17:46:04 +0200 | [diff] [blame] | 377 | goto error; |
Radek Krejci | 710226d | 2019-07-24 17:24:59 +0200 | [diff] [blame] | 378 | } else { |
| 379 | LOGWRN(ctx->ctx, "Invalid position of the key \"%.*s\" in a list.", name_len, name); |
| 380 | } |
| 381 | } |
| 382 | } else { |
| 383 | /* last child of the parent */ |
| 384 | if (prev) { |
| 385 | parent->child->prev = cur; |
| 386 | prev->next = cur; |
| 387 | cur->prev = prev; |
| 388 | } |
| 389 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 390 | } else { |
Radek Krejci | 710226d | 2019-07-24 17:24:59 +0200 | [diff] [blame] | 391 | /* top level */ |
| 392 | if (prev) { |
| 393 | /* last top level node */ |
| 394 | struct lyd_node *iter; |
| 395 | for (iter = prev; iter->prev->next; iter = iter->prev); |
| 396 | iter->prev = cur; |
| 397 | prev->next = cur; |
| 398 | cur->prev = prev; |
| 399 | } /* first top level node - nothing more to do */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 400 | } |
Radek Krejci | 710226d | 2019-07-24 17:24:59 +0200 | [diff] [blame] | 401 | prev = last; |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame^] | 402 | LY_CHECK_GOTO(ret = lydxml_attributes(ctx, &attrs_data, cur), cleanup); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 403 | |
| 404 | if (snode->nodetype & LYD_NODE_TERM) { |
| 405 | int dynamic = 0; |
| 406 | char *buffer = NULL, *value; |
| 407 | size_t buffer_size = 0, value_len; |
| 408 | |
| 409 | if (ctx->status == LYXML_ELEM_CONTENT) { |
| 410 | /* get the value */ |
Radek Krejci | 1170214 | 2019-07-24 17:46:04 +0200 | [diff] [blame] | 411 | ret = lyxml_get_string((struct lyxml_context *)ctx, data, &buffer, &buffer_size, &value, &value_len, &dynamic); |
| 412 | if (ret == LY_EINVAL) { |
Radek Krejci | 339e2de | 2019-05-17 14:28:24 +0200 | [diff] [blame] | 413 | /* just indentation of a child element found */ |
| 414 | LOGVAL(ctx->ctx, LY_VLOG_LINE, &ctx->line, LYVE_SYNTAX, "Child element inside terminal node \"%s\" found.", cur->schema->name); |
| 415 | goto cleanup; |
| 416 | } |
Radek Krejci | 1170214 | 2019-07-24 17:46:04 +0200 | [diff] [blame] | 417 | ret = LY_SUCCESS; |
Radek Krejci | e92210c | 2019-05-17 15:53:35 +0200 | [diff] [blame] | 418 | } else { |
| 419 | /* no content - validate empty value */ |
| 420 | value = ""; |
| 421 | value_len = 0; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 422 | } |
Radek Krejci | 3c9758d | 2019-07-11 16:49:10 +0200 | [diff] [blame] | 423 | ret = lyd_value_parse((struct lyd_node_term*)cur, value, value_len, dynamic, 0, lydxml_resolve_prefix, ctx, LYD_XML, NULL); |
Radek Krejci | e553e6d | 2019-06-07 15:33:18 +0200 | [diff] [blame] | 424 | if (ret == LY_EINCOMPLETE) { |
| 425 | ly_set_add(&ctx->incomplete_type_validation, cur, LY_SET_OPT_USEASLIST); |
| 426 | } else if (ret) { |
| 427 | if (dynamic){ |
| 428 | free(value); |
| 429 | } |
| 430 | goto cleanup; |
| 431 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 432 | } else if (snode->nodetype & LYD_NODE_INNER) { |
| 433 | int dynamic = 0; |
| 434 | char *buffer = NULL, *value; |
| 435 | size_t buffer_size = 0, value_len; |
| 436 | |
| 437 | if (ctx->status == LYXML_ELEM_CONTENT) { |
| 438 | LY_ERR r = lyxml_get_string((struct lyxml_context *)ctx, data, &buffer, &buffer_size, &value, &value_len, &dynamic); |
Radek Krejci | e553e6d | 2019-06-07 15:33:18 +0200 | [diff] [blame] | 439 | if (r != LY_EINVAL && (r != LY_SUCCESS || value_len != 0)) { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 440 | LOGINT(ctx->ctx); |
Radek Krejci | 1170214 | 2019-07-24 17:46:04 +0200 | [diff] [blame] | 441 | ret = LY_EINT; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 442 | goto cleanup; |
| 443 | } |
| 444 | } |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 445 | /* process children */ |
Radek Krejci | e553e6d | 2019-06-07 15:33:18 +0200 | [diff] [blame] | 446 | if (ctx->status == LYXML_ELEMENT && parents_count != ctx->elements.count) { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 447 | ret = lydxml_nodes(ctx, (struct lyd_node_inner*)cur, data, lyd_node_children_p(cur)); |
Radek Krejci | e92210c | 2019-05-17 15:53:35 +0200 | [diff] [blame] | 448 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 449 | } |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 450 | } else if (snode->nodetype & LYD_NODE_ANY) { |
| 451 | unsigned int cur_element_index = ctx->elements.count; |
| 452 | const char *start = *data, *stop; |
| 453 | const char *p, *n; |
| 454 | size_t p_len, n_len; |
| 455 | |
| 456 | /* skip children data and store them as a string */ |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 457 | while (ctx->status != LYXML_END && cur_element_index <= ctx->elements.count) { |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 458 | switch (ctx->status) { |
| 459 | case LYXML_ELEMENT: |
| 460 | ret = lyxml_get_element((struct lyxml_context *)ctx, data, &p, &p_len, &n, &n_len); |
| 461 | break; |
| 462 | case LYXML_ATTRIBUTE: |
| 463 | lyxml_get_attribute((struct lyxml_context*)ctx, data, &p, &p_len, &n, &n_len); |
| 464 | break; |
| 465 | case LYXML_ELEM_CONTENT: |
| 466 | case LYXML_ATTR_CONTENT: |
| 467 | ret = lyxml_get_string((struct lyxml_context *)ctx, data, NULL, NULL, NULL, NULL, NULL); |
| 468 | if (ret == LY_EINVAL) { |
| 469 | /* not an error, just incorrect XML parser status */ |
| 470 | ret = LY_SUCCESS; |
| 471 | } |
| 472 | break; |
| 473 | case LYXML_END: |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 474 | /* end of data */ |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 475 | LOGINT(ctx->ctx); |
Radek Krejci | 1170214 | 2019-07-24 17:46:04 +0200 | [diff] [blame] | 476 | ret = LY_EINT; |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 477 | goto cleanup; |
| 478 | } |
| 479 | LY_CHECK_GOTO(ret, cleanup); |
| 480 | } |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 481 | ((struct lyd_node_any*)cur)->value_type = LYD_ANYDATA_XML; |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 482 | if (start != *data) { |
| 483 | /* data now points after the anydata's closing element tag, we need just end of its content */ |
| 484 | for (stop = *data - 1; *stop != '<'; --stop); |
| 485 | ((struct lyd_node_any*)cur)->value.xml = lydict_insert(ctx->ctx, start, stop - start); |
| 486 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 487 | } |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 488 | |
| 489 | /* calculate the hash and insert it into parent (list with keys is handled when its keys are inserted) */ |
| 490 | lyd_hash(cur); |
| 491 | lyd_insert_hash(cur); |
Radek Krejci | b6f7ae5 | 2019-07-19 10:31:42 +0200 | [diff] [blame] | 492 | |
| 493 | /* if we have empty non-presence container, we keep it, but mark it as default */ |
| 494 | if (cur->schema->nodetype == LYS_CONTAINER && !((struct lyd_node_inner*)cur)->child && |
| 495 | !cur->attr && !(((struct lysc_node_container*)cur->schema)->flags & LYS_PRESENCE)) { |
| 496 | cur->flags |= LYD_DEFAULT; |
| 497 | } |
| 498 | |
| 499 | /* TODO context validation */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 500 | } |
| 501 | |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 502 | /* TODO add missing siblings default elements */ |
| 503 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 504 | cleanup: |
Radek Krejci | 28681fa | 2019-09-06 13:08:45 +0200 | [diff] [blame^] | 505 | for (unsigned int u = 0; u < attrs_data.count; ++u) { |
| 506 | if (((struct attr_data_s*)attrs_data.objs[u])->dynamic) { |
| 507 | free(((struct attr_data_s*)attrs_data.objs[u])->value); |
| 508 | } |
| 509 | } |
| 510 | ly_set_erase(&attrs_data, free); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 511 | return ret; |
Radek Krejci | 1170214 | 2019-07-24 17:46:04 +0200 | [diff] [blame] | 512 | |
| 513 | error: |
| 514 | ret = LY_EVALID; |
| 515 | goto cleanup; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | LY_ERR |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 519 | lyd_parse_xml(struct ly_ctx *ctx, const char *data, int options, const struct lyd_node **trees, struct lyd_node **result) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 520 | { |
Radek Krejci | 18a57d9 | 2019-07-25 14:01:42 +0200 | [diff] [blame] | 521 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 522 | struct lyd_node_inner *parent = NULL; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 523 | struct lyd_xml_ctx xmlctx = {0}; |
| 524 | |
| 525 | xmlctx.options = options; |
| 526 | xmlctx.ctx = ctx; |
| 527 | xmlctx.line = 1; |
| 528 | |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 529 | /* init */ |
| 530 | *result = NULL; |
| 531 | |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 532 | if (options & LYD_OPT_RPCREPLY) { |
Radek Krejci | 3c1046e | 2019-07-26 13:06:53 +0200 | [diff] [blame] | 533 | /* prepare container for RPC reply, for which we need RPC |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 534 | * - prepare *result as top-level node |
| 535 | * - prepare parent as the RPC/action node */ |
Radek Krejci | 3c1046e | 2019-07-26 13:06:53 +0200 | [diff] [blame] | 536 | const struct lyd_node *action; |
| 537 | for (action = trees[0]; action && action->schema->nodetype != LYS_ACTION; action = lyd_node_children(action)) { |
| 538 | /* skip list's keys */ |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 539 | for ( ;action && action->schema->nodetype == LYS_LEAF; action = action->next); |
| 540 | if (action && action->schema->nodetype == LYS_ACTION) { |
| 541 | break; |
| 542 | } |
Radek Krejci | 3c1046e | 2019-07-26 13:06:53 +0200 | [diff] [blame] | 543 | } |
| 544 | if (!action) { |
| 545 | LOGERR(ctx, LY_EINVAL, "Data parser invalid argument trees - the first item in the array must be the RPC/action request when parsing %s.", |
| 546 | lyd_parse_options_type2str(options)); |
| 547 | return LY_EINVAL; |
| 548 | } |
| 549 | parent = (struct lyd_node_inner*)lyd_dup(action, NULL, LYD_DUP_WITH_PARENTS); |
| 550 | LY_CHECK_ERR_RET(!parent, LOGERR(ctx, ly_errcode(ctx), "Unable to duplicate RPC/action container for RPC/action reply."), ly_errcode(ctx)); |
| 551 | for (*result = (struct lyd_node*)parent; (*result)->parent; *result = (struct lyd_node*)(*result)->parent); |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 552 | } |
| 553 | |
Radek Krejci | 26a5dfb | 2019-07-26 14:51:06 +0200 | [diff] [blame] | 554 | if (!data || !data[0]) { |
| 555 | goto no_data; |
| 556 | } |
| 557 | |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 558 | ret = lydxml_nodes(&xmlctx, parent, &data, *result ? &parent->child : result); |
Radek Krejci | e92210c | 2019-05-17 15:53:35 +0200 | [diff] [blame] | 559 | if (ret) { |
| 560 | lyd_free_all(*result); |
| 561 | *result = NULL; |
Radek Krejci | e553e6d | 2019-06-07 15:33:18 +0200 | [diff] [blame] | 562 | } else { |
| 563 | /* finish incompletely validated terminal values */ |
| 564 | for (unsigned int u = 0; u < xmlctx.incomplete_type_validation.count; u++) { |
| 565 | struct lyd_node_term *node = (struct lyd_node_term*)xmlctx.incomplete_type_validation.objs[u]; |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 566 | const struct lyd_node **result_trees = NULL; |
Radek Krejci | e72c043 | 2019-06-10 10:17:03 +0200 | [diff] [blame] | 567 | |
| 568 | /* prepare sized array for validator */ |
| 569 | if (*result) { |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 570 | result_trees = lyd_trees_new(1, *result); |
Radek Krejci | e553e6d | 2019-06-07 15:33:18 +0200 | [diff] [blame] | 571 | } |
Radek Krejci | e72c043 | 2019-06-10 10:17:03 +0200 | [diff] [blame] | 572 | /* validate and store the value of the node */ |
Radek Krejci | 3c9758d | 2019-07-11 16:49:10 +0200 | [diff] [blame] | 573 | ret = lyd_value_parse(node, node->value.canonized, node->value.canonized ? strlen(node->value.canonized) : 0, 0, 1, |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 574 | lydxml_resolve_prefix, ctx, LYD_XML, result_trees); |
| 575 | lyd_trees_free(result_trees, 0); |
Radek Krejci | e553e6d | 2019-06-07 15:33:18 +0200 | [diff] [blame] | 576 | if (ret) { |
| 577 | lyd_free_all(*result); |
| 578 | *result = NULL; |
| 579 | break; |
| 580 | } |
| 581 | } |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 582 | /* ... and attribute values */ |
| 583 | for (unsigned int u = 0; u < xmlctx.incomplete_type_validation_attrs.count; u++) { |
| 584 | struct lyd_attr *attr = (struct lyd_attr*)xmlctx.incomplete_type_validation_attrs.objs[u]; |
| 585 | const struct lyd_node **result_trees = NULL; |
| 586 | |
| 587 | /* prepare sized array for validator */ |
| 588 | if (*result) { |
| 589 | result_trees = lyd_trees_new(1, *result); |
| 590 | } |
| 591 | /* validate and store the value of the node */ |
| 592 | ret = lyd_value_parse_attr(attr, attr->value.canonized, attr->value.canonized ? strlen(attr->value.canonized) : 0, 0, 1, |
| 593 | lydxml_resolve_prefix, ctx, LYD_XML, result_trees); |
| 594 | lyd_trees_free(result_trees, 0); |
| 595 | if (ret) { |
| 596 | lyd_free_all(*result); |
| 597 | *result = NULL; |
| 598 | break; |
| 599 | } |
| 600 | } |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 601 | |
Radek Krejci | 3c1046e | 2019-07-26 13:06:53 +0200 | [diff] [blame] | 602 | if (!(*result) || (parent && !parent->child)) { |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 603 | no_data: |
| 604 | /* no data */ |
| 605 | if (options & (LYD_OPT_RPC | LYD_OPT_NOTIF)) { |
Radek Krejci | 3c1046e | 2019-07-26 13:06:53 +0200 | [diff] [blame] | 606 | /* error, missing top level node identify RPC and Notification */ |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 607 | LOGERR(ctx, LY_EINVAL, "Invalid input data of data parser - expected %s which cannot be empty.", |
| 608 | lyd_parse_options_type2str(options)); |
| 609 | } else { |
| 610 | /* others - no work is needed, just check for missing mandatory nodes */ |
Radek Krejci | 3c1046e | 2019-07-26 13:06:53 +0200 | [diff] [blame] | 611 | /* TODO lyd_validate(&result, options, ctx); |
| 612 | * - according to the data tree type */ |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 613 | } |
| 614 | } |
Radek Krejci | e92210c | 2019-05-17 15:53:35 +0200 | [diff] [blame] | 615 | } |
Radek Krejci | e553e6d | 2019-06-07 15:33:18 +0200 | [diff] [blame] | 616 | |
| 617 | ly_set_erase(&xmlctx.incomplete_type_validation, NULL); |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 618 | ly_set_erase(&xmlctx.incomplete_type_validation_attrs, NULL); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 619 | lyxml_context_clear((struct lyxml_context*)&xmlctx); |
| 620 | return ret; |
| 621 | } |