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