Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file tree_data_helpers.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief Parsing and validation helper functions for data trees |
| 5 | * |
| 6 | * Copyright (c) 2015 - 2018 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 | */ |
Radek Krejci | f8dc59a | 2020-11-25 13:47:44 +0100 | [diff] [blame] | 14 | #define _POSIX_C_SOURCE 200809L /* strdup, strndup */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 15 | |
| 16 | #include <assert.h> |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 17 | #include <stdint.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 18 | #include <stdlib.h> |
Radek Krejci | ad97c5f | 2020-06-30 09:19:28 +0200 | [diff] [blame] | 19 | #include <string.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 20 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 21 | #include "common.h" |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 22 | #include "compat.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 23 | #include "context.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 24 | #include "dict.h" |
Radek Krejci | ad97c5f | 2020-06-30 09:19:28 +0200 | [diff] [blame] | 25 | #include "hash_table.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 26 | #include "log.h" |
Radek Krejci | ad97c5f | 2020-06-30 09:19:28 +0200 | [diff] [blame] | 27 | #include "lyb.h" |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 28 | #include "parser_data.h" |
Radek Krejci | ad97c5f | 2020-06-30 09:19:28 +0200 | [diff] [blame] | 29 | #include "set.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 30 | #include "tree.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 31 | #include "tree_data.h" |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 32 | #include "tree_data_internal.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 33 | #include "tree_schema.h" |
| 34 | |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 35 | struct lyd_node * |
| 36 | lys_getnext_data(const struct lyd_node *last, const struct lyd_node *sibling, const struct lysc_node **slast, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 37 | const struct lysc_node *parent, const struct lysc_module *module) |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 38 | { |
| 39 | const struct lysc_node *siter = NULL; |
| 40 | struct lyd_node *match = NULL; |
| 41 | |
| 42 | assert(parent || module); |
| 43 | assert(!last || (slast && *slast)); |
| 44 | |
| 45 | if (slast) { |
| 46 | siter = *slast; |
| 47 | } |
| 48 | |
| 49 | if (last && last->next && (last->next->schema == siter)) { |
| 50 | /* return next data instance */ |
| 51 | return last->next; |
| 52 | } |
| 53 | |
| 54 | /* find next schema node data instance */ |
| 55 | while ((siter = lys_getnext(siter, parent, module, 0))) { |
| 56 | if (!lyd_find_sibling_val(sibling, siter, NULL, 0, &match)) { |
| 57 | break; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | if (slast) { |
| 62 | *slast = siter; |
| 63 | } |
| 64 | return match; |
| 65 | } |
| 66 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 67 | struct lyd_node ** |
| 68 | lyd_node_children_p(struct lyd_node *node) |
| 69 | { |
| 70 | assert(node); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 71 | |
| 72 | if (!node->schema) { |
| 73 | return &((struct lyd_node_opaq *)node)->child; |
| 74 | } else { |
| 75 | switch (node->schema->nodetype) { |
| 76 | case LYS_CONTAINER: |
| 77 | case LYS_LIST: |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 78 | case LYS_RPC: |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 79 | case LYS_ACTION: |
| 80 | case LYS_NOTIF: |
| 81 | return &((struct lyd_node_inner *)node)->child; |
| 82 | default: |
| 83 | return NULL; |
| 84 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
Radek Krejci | dae0ee8 | 2020-05-06 16:53:24 +0200 | [diff] [blame] | 88 | API struct lyd_node * |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 89 | lyd_child_no_keys(const struct lyd_node *node) |
| 90 | { |
| 91 | struct lyd_node **children; |
| 92 | |
| 93 | if (!node) { |
| 94 | return NULL; |
| 95 | } |
| 96 | |
| 97 | if (!node->schema) { |
| 98 | /* opaq node */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 99 | return ((struct lyd_node_opaq *)node)->child; |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | children = lyd_node_children_p((struct lyd_node *)node); |
| 103 | if (children) { |
| 104 | struct lyd_node *child = *children; |
| 105 | while (child && child->schema && (child->schema->flags & LYS_KEY)) { |
| 106 | child = child->next; |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 107 | } |
| 108 | return child; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 109 | } else { |
| 110 | return NULL; |
| 111 | } |
| 112 | } |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 113 | |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 114 | API const struct lys_module * |
| 115 | lyd_owner_module(const struct lyd_node *node) |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 116 | { |
| 117 | const struct lysc_node *schema; |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 118 | const struct lyd_node_opaq *opaq; |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 119 | |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 120 | if (!node) { |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 121 | return NULL; |
| 122 | } |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 123 | |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 124 | if (!node->schema) { |
| 125 | opaq = (struct lyd_node_opaq *)node; |
| 126 | switch (opaq->format) { |
| 127 | case LY_PREF_XML: |
| 128 | return ly_ctx_get_module_implemented_ns(LYD_CTX(node), opaq->name.module_ns); |
| 129 | case LY_PREF_JSON: |
| 130 | return ly_ctx_get_module_implemented(LYD_CTX(node), opaq->name.module_name); |
| 131 | default: |
| 132 | return NULL; |
| 133 | } |
| 134 | } |
| 135 | |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 136 | for (schema = node->schema; schema->parent; schema = schema->parent) {} |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 137 | return schema->module; |
| 138 | } |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 139 | |
| 140 | const struct lys_module * |
Michal Vasko | 26e8001 | 2020-07-08 10:55:46 +0200 | [diff] [blame] | 141 | lyd_mod_next_module(struct lyd_node *tree, const struct lys_module *module, const struct ly_ctx *ctx, uint32_t *i, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 142 | struct lyd_node **first) |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 143 | { |
| 144 | struct lyd_node *iter; |
| 145 | const struct lys_module *mod; |
| 146 | |
| 147 | /* get the next module */ |
Michal Vasko | 26e8001 | 2020-07-08 10:55:46 +0200 | [diff] [blame] | 148 | if (module) { |
| 149 | if (*i) { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 150 | mod = NULL; |
Michal Vasko | 26e8001 | 2020-07-08 10:55:46 +0200 | [diff] [blame] | 151 | } else { |
| 152 | mod = module; |
| 153 | ++(*i); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 154 | } |
| 155 | } else { |
| 156 | do { |
| 157 | mod = ly_ctx_get_module_iter(ctx, i); |
| 158 | } while (mod && !mod->implemented); |
| 159 | } |
| 160 | |
| 161 | /* find its data */ |
| 162 | *first = NULL; |
| 163 | if (mod) { |
| 164 | LY_LIST_FOR(tree, iter) { |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 165 | if (lyd_owner_module(iter) == mod) { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 166 | *first = iter; |
| 167 | break; |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | return mod; |
| 173 | } |
| 174 | |
| 175 | const struct lys_module * |
| 176 | lyd_data_next_module(struct lyd_node **next, struct lyd_node **first) |
| 177 | { |
| 178 | const struct lys_module *mod; |
| 179 | |
| 180 | if (!*next) { |
| 181 | /* all data traversed */ |
| 182 | *first = NULL; |
| 183 | return NULL; |
| 184 | } |
| 185 | |
| 186 | *first = *next; |
| 187 | |
| 188 | /* prepare next */ |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 189 | mod = lyd_owner_module(*next); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 190 | LY_LIST_FOR(*next, *next) { |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 191 | if (lyd_owner_module(*next) != mod) { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 192 | break; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | return mod; |
| 197 | } |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 198 | |
| 199 | LY_ERR |
| 200 | lyd_parse_check_keys(struct lyd_node *node) |
| 201 | { |
| 202 | const struct lysc_node *skey = NULL; |
| 203 | const struct lyd_node *key; |
| 204 | |
| 205 | assert(node->schema->nodetype == LYS_LIST); |
| 206 | |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 207 | key = lyd_child(node); |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 208 | while ((skey = lys_getnext(skey, node->schema, NULL, 0)) && (skey->flags & LYS_KEY)) { |
| 209 | if (!key || (key->schema != skey)) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 210 | LOGVAL(LYD_CTX(node), LY_VCODE_NOKEY, skey->name); |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 211 | return LY_EVALID; |
| 212 | } |
| 213 | |
| 214 | key = key->next; |
| 215 | } |
| 216 | |
| 217 | return LY_SUCCESS; |
| 218 | } |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 219 | |
| 220 | void |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 221 | lyd_parse_set_data_flags(struct lyd_node *node, struct ly_set *when_check, struct lyd_meta **meta, uint32_t options) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 222 | { |
| 223 | struct lyd_meta *meta2, *prev_meta = NULL; |
| 224 | |
Michal Vasko | a5705e5 | 2020-12-09 18:15:14 +0100 | [diff] [blame] | 225 | if (lysc_has_when(node->schema)) { |
Michal Vasko | 0f3377d | 2020-11-09 20:56:11 +0100 | [diff] [blame] | 226 | if (!(options & LYD_PARSE_ONLY)) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 227 | /* remember we need to evaluate this node's when */ |
Radek Krejci | 3d92e44 | 2020-10-12 12:48:13 +0200 | [diff] [blame] | 228 | LY_CHECK_RET(ly_set_add(when_check, node, 1, NULL), ); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 232 | LY_LIST_FOR(*meta, meta2) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 233 | if (!strcmp(meta2->name, "default") && !strcmp(meta2->annotation->module->name, "ietf-netconf-with-defaults") && |
| 234 | meta2->value.boolean) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 235 | /* node is default according to the metadata */ |
| 236 | node->flags |= LYD_DEFAULT; |
| 237 | |
| 238 | /* delete the metadata */ |
| 239 | if (prev_meta) { |
| 240 | prev_meta->next = meta2->next; |
| 241 | } else { |
| 242 | *meta = (*meta)->next; |
| 243 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 244 | lyd_free_meta_single(meta2); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 245 | break; |
| 246 | } |
| 247 | |
| 248 | prev_meta = meta2; |
| 249 | } |
| 250 | } |
| 251 | |
Michal Vasko | c000427 | 2020-08-06 08:32:34 +0200 | [diff] [blame] | 252 | API LY_ERR |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 253 | lyd_any_copy_value(struct lyd_node *trg, const union lyd_any_value *value, LYD_ANYDATA_VALUETYPE value_type) |
| 254 | { |
| 255 | struct lyd_node_any *t; |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 256 | |
| 257 | assert(trg->schema->nodetype & LYS_ANYDATA); |
| 258 | |
| 259 | t = (struct lyd_node_any *)trg; |
| 260 | |
| 261 | /* free trg */ |
| 262 | switch (t->value_type) { |
| 263 | case LYD_ANYDATA_DATATREE: |
| 264 | lyd_free_all(t->value.tree); |
| 265 | break; |
| 266 | case LYD_ANYDATA_STRING: |
| 267 | case LYD_ANYDATA_XML: |
| 268 | case LYD_ANYDATA_JSON: |
Michal Vasko | e180ed0 | 2021-02-05 16:31:20 +0100 | [diff] [blame^] | 269 | lydict_remove(LYD_CTX(trg), t->value.str); |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 270 | break; |
| 271 | case LYD_ANYDATA_LYB: |
| 272 | free(t->value.mem); |
| 273 | break; |
| 274 | } |
| 275 | t->value.str = NULL; |
| 276 | |
| 277 | if (!value) { |
| 278 | /* only free value in this case */ |
| 279 | return LY_SUCCESS; |
| 280 | } |
| 281 | |
| 282 | /* copy src */ |
| 283 | t->value_type = value_type; |
| 284 | switch (value_type) { |
| 285 | case LYD_ANYDATA_DATATREE: |
| 286 | if (value->tree) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 287 | LY_CHECK_RET(lyd_dup_siblings(value->tree, NULL, LYD_DUP_RECURSIVE, &t->value.tree)); |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 288 | } |
| 289 | break; |
| 290 | case LYD_ANYDATA_STRING: |
| 291 | case LYD_ANYDATA_XML: |
| 292 | case LYD_ANYDATA_JSON: |
| 293 | if (value->str) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 294 | LY_CHECK_RET(lydict_insert(LYD_CTX(trg), value->str, 0, &t->value.str)); |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 295 | } |
| 296 | break; |
| 297 | case LYD_ANYDATA_LYB: |
| 298 | if (value->mem) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 299 | int len = lyd_lyb_data_length(value->mem); |
Radek Krejci | 82fa8d4 | 2020-07-11 22:00:59 +0200 | [diff] [blame] | 300 | LY_CHECK_RET(len == -1, LY_EINVAL); |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 301 | t->value.mem = malloc(len); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 302 | LY_CHECK_ERR_RET(!t->value.mem, LOGMEM(LYD_CTX(trg)), LY_EMEM); |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 303 | memcpy(t->value.mem, value->mem, len); |
| 304 | } |
| 305 | break; |
| 306 | } |
| 307 | |
| 308 | return LY_SUCCESS; |
| 309 | } |
| 310 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 311 | LYB_HASH |
| 312 | lyb_hash(struct lysc_node *sibling, uint8_t collision_id) |
| 313 | { |
| 314 | const struct lys_module *mod; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 315 | uint32_t full_hash; |
| 316 | LYB_HASH hash; |
| 317 | |
| 318 | if ((collision_id < LYS_NODE_HASH_COUNT) && sibling->hash[collision_id]) { |
| 319 | return sibling->hash[collision_id]; |
| 320 | } |
| 321 | |
| 322 | mod = sibling->module; |
| 323 | |
| 324 | full_hash = dict_hash_multi(0, mod->name, strlen(mod->name)); |
| 325 | full_hash = dict_hash_multi(full_hash, sibling->name, strlen(sibling->name)); |
| 326 | if (collision_id) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 327 | size_t ext_len; |
| 328 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 329 | if (collision_id > strlen(mod->name)) { |
| 330 | /* fine, we will not hash more bytes, just use more bits from the hash than previously */ |
| 331 | ext_len = strlen(mod->name); |
| 332 | } else { |
| 333 | /* use one more byte from the module name than before */ |
| 334 | ext_len = collision_id; |
| 335 | } |
| 336 | full_hash = dict_hash_multi(full_hash, mod->name, ext_len); |
| 337 | } |
| 338 | full_hash = dict_hash_multi(full_hash, NULL, 0); |
| 339 | |
| 340 | /* use the shortened hash */ |
| 341 | hash = full_hash & (LYB_HASH_MASK >> collision_id); |
| 342 | /* add colision identificator */ |
| 343 | hash |= LYB_HASH_COLLISION_ID >> collision_id; |
| 344 | |
| 345 | /* save this hash */ |
| 346 | if (collision_id < LYS_NODE_HASH_COUNT) { |
| 347 | sibling->hash[collision_id] = hash; |
| 348 | } |
| 349 | |
| 350 | return hash; |
| 351 | } |
| 352 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 353 | ly_bool |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 354 | lyb_has_schema_model(const struct lysc_node *sibling, const struct lys_module **models) |
| 355 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 356 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 357 | |
| 358 | LY_ARRAY_FOR(models, u) { |
| 359 | if (sibling->module == models[u]) { |
| 360 | return 1; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | return 0; |
| 365 | } |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 366 | |
| 367 | void |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 368 | lyd_del_move_root(struct lyd_node **root, const struct lyd_node *to_del, const struct lys_module *mod) |
| 369 | { |
| 370 | if (*root && (lyd_owner_module(*root) != mod)) { |
| 371 | /* there are no data of mod so this is simply the first top-level sibling */ |
| 372 | mod = NULL; |
| 373 | } |
| 374 | |
| 375 | if ((*root != to_del) || (*root)->parent) { |
| 376 | return; |
| 377 | } |
| 378 | |
| 379 | *root = (*root)->next; |
| 380 | if (mod && *root && (lyd_owner_module(to_del) != lyd_owner_module(*root))) { |
| 381 | /* there are no more nodes from mod */ |
| 382 | *root = lyd_first_sibling(*root); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | void |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 387 | ly_free_prefix_data(LY_PREFIX_FORMAT format, void *prefix_data) |
| 388 | { |
| 389 | struct ly_set *ns_list; |
| 390 | struct lysc_prefix *prefixes; |
| 391 | uint32_t i; |
| 392 | LY_ARRAY_COUNT_TYPE u; |
| 393 | |
| 394 | if (!prefix_data) { |
| 395 | return; |
| 396 | } |
| 397 | |
| 398 | switch (format) { |
| 399 | case LY_PREF_XML: |
| 400 | ns_list = prefix_data; |
| 401 | for (i = 0; i < ns_list->count; ++i) { |
| 402 | free(((struct lyxml_ns *)ns_list->objs[i])->prefix); |
| 403 | free(((struct lyxml_ns *)ns_list->objs[i])->uri); |
| 404 | } |
| 405 | ly_set_free(ns_list, free); |
| 406 | break; |
| 407 | case LY_PREF_SCHEMA_RESOLVED: |
| 408 | prefixes = prefix_data; |
| 409 | LY_ARRAY_FOR(prefixes, u) { |
| 410 | free(prefixes[u].prefix); |
| 411 | } |
| 412 | LY_ARRAY_FREE(prefixes); |
| 413 | break; |
| 414 | case LY_PREF_SCHEMA: |
| 415 | case LY_PREF_JSON: |
| 416 | break; |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | LY_ERR |
| 421 | ly_dup_prefix_data(const struct ly_ctx *ctx, LY_PREFIX_FORMAT format, const void *prefix_data, |
| 422 | void **prefix_data_p) |
| 423 | { |
| 424 | LY_ERR ret = LY_SUCCESS; |
| 425 | struct lyxml_ns *ns; |
| 426 | struct lysc_prefix *prefixes = NULL, *orig_pref; |
| 427 | struct ly_set *ns_list, *orig_ns; |
| 428 | uint32_t i; |
| 429 | LY_ARRAY_COUNT_TYPE u; |
| 430 | |
| 431 | assert(!*prefix_data_p); |
| 432 | |
| 433 | switch (format) { |
| 434 | case LY_PREF_SCHEMA: |
| 435 | *prefix_data_p = (void *)prefix_data; |
| 436 | break; |
| 437 | case LY_PREF_SCHEMA_RESOLVED: |
| 438 | /* copy all the value prefixes */ |
| 439 | orig_pref = (struct lysc_prefix *)prefix_data; |
| 440 | LY_ARRAY_CREATE_GOTO(ctx, prefixes, LY_ARRAY_COUNT(orig_pref), ret, cleanup); |
| 441 | *prefix_data_p = prefixes; |
| 442 | |
| 443 | LY_ARRAY_FOR(orig_pref, u) { |
| 444 | if (orig_pref[u].prefix) { |
| 445 | prefixes[u].prefix = strdup(orig_pref[u].prefix); |
| 446 | LY_CHECK_ERR_GOTO(!prefixes[u].prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 447 | } |
| 448 | prefixes[u].mod = orig_pref[u].mod; |
| 449 | LY_ARRAY_INCREMENT(prefixes); |
| 450 | } |
| 451 | break; |
| 452 | case LY_PREF_XML: |
| 453 | /* copy all the namespaces */ |
| 454 | LY_CHECK_GOTO(ret = ly_set_new(&ns_list), cleanup); |
| 455 | *prefix_data_p = ns_list; |
| 456 | |
| 457 | orig_ns = (struct ly_set *)prefix_data; |
| 458 | for (i = 0; i < orig_ns->count; ++i) { |
| 459 | ns = calloc(1, sizeof *ns); |
| 460 | LY_CHECK_ERR_GOTO(!ns, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 461 | LY_CHECK_GOTO(ret = ly_set_add(ns_list, ns, 1, NULL), cleanup); |
| 462 | |
| 463 | if (((struct lyxml_ns *)orig_ns->objs[i])->prefix) { |
| 464 | ns->prefix = strdup(((struct lyxml_ns *)orig_ns->objs[i])->prefix); |
| 465 | LY_CHECK_ERR_GOTO(!ns->prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 466 | } |
| 467 | ns->uri = strdup(((struct lyxml_ns *)orig_ns->objs[i])->uri); |
| 468 | LY_CHECK_ERR_GOTO(!ns->uri, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 469 | } |
| 470 | break; |
| 471 | case LY_PREF_JSON: |
| 472 | assert(!prefix_data); |
| 473 | *prefix_data_p = NULL; |
| 474 | break; |
| 475 | } |
| 476 | |
| 477 | cleanup: |
| 478 | if (ret) { |
| 479 | ly_free_prefix_data(format, *prefix_data_p); |
| 480 | *prefix_data_p = NULL; |
| 481 | } |
| 482 | return ret; |
| 483 | } |
| 484 | |
| 485 | LY_ERR |
| 486 | ly_store_prefix_data(const struct ly_ctx *ctx, const char *value, size_t value_len, LY_PREFIX_FORMAT format, |
| 487 | void *prefix_data, LY_PREFIX_FORMAT *format_p, void **prefix_data_p) |
| 488 | { |
| 489 | LY_ERR ret = LY_SUCCESS; |
| 490 | const char *start, *stop; |
| 491 | const struct lys_module *mod; |
| 492 | struct lyxml_ns *ns; |
| 493 | struct ly_set *ns_list; |
| 494 | struct lysc_prefix *prefixes = NULL, *val_pref; |
| 495 | |
| 496 | switch (format) { |
| 497 | case LY_PREF_SCHEMA: |
| 498 | case LY_PREF_XML: |
| 499 | /* copy all referenced modules... */ |
| 500 | if (format == LY_PREF_XML) { |
| 501 | /* ...as prefix - namespace pairs */ |
| 502 | LY_CHECK_GOTO(ret = ly_set_new(&ns_list), cleanup); |
| 503 | *format_p = LY_PREF_XML; |
| 504 | *prefix_data_p = ns_list; |
| 505 | } else { |
| 506 | /* ...as prefix - module pairs */ |
| 507 | assert(format == LY_PREF_SCHEMA); |
| 508 | LY_ARRAY_CREATE_GOTO(ctx, prefixes, 0, ret, cleanup); |
| 509 | *format_p = LY_PREF_SCHEMA_RESOLVED; |
| 510 | *prefix_data_p = prefixes; |
| 511 | } |
| 512 | |
| 513 | /* add all used prefixes */ |
| 514 | for (stop = start = value; (size_t)(stop - value) < value_len; start = stop) { |
| 515 | size_t bytes; |
| 516 | uint32_t c; |
| 517 | |
| 518 | ly_getutf8(&stop, &c, &bytes); |
| 519 | if (is_xmlqnamestartchar(c)) { |
| 520 | for (ly_getutf8(&stop, &c, &bytes); |
| 521 | is_xmlqnamechar(c) && (size_t)(stop - value) < value_len; |
| 522 | ly_getutf8(&stop, &c, &bytes)) {} |
| 523 | stop = stop - bytes; |
| 524 | if (*stop == ':') { |
| 525 | /* we have a possible prefix */ |
| 526 | size_t len = stop - start; |
| 527 | |
| 528 | /* do we already have the prefix? */ |
| 529 | mod = ly_type_store_resolve_prefix(ctx, start, len, *format_p, *prefix_data_p); |
| 530 | if (!mod) { |
| 531 | mod = ly_type_store_resolve_prefix(ctx, start, len, format, prefix_data); |
| 532 | if (mod) { |
| 533 | if (*format_p == LY_PREF_XML) { |
| 534 | /* store a new prefix - namespace pair */ |
| 535 | ns = calloc(1, sizeof *ns); |
| 536 | LY_CHECK_ERR_GOTO(!ns, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 537 | LY_CHECK_GOTO(ret = ly_set_add(ns_list, ns, 1, NULL), cleanup); |
| 538 | |
| 539 | ns->prefix = strndup(start, len); |
| 540 | LY_CHECK_ERR_GOTO(!ns->prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 541 | ns->uri = strdup(mod->ns); |
| 542 | LY_CHECK_ERR_GOTO(!ns->uri, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 543 | } else { |
| 544 | assert(*format_p == LY_PREF_SCHEMA_RESOLVED); |
| 545 | /* store a new prefix - module pair */ |
| 546 | LY_ARRAY_NEW_GOTO(ctx, prefixes, val_pref, ret, cleanup); |
| 547 | *prefix_data_p = prefixes; |
| 548 | |
| 549 | val_pref->prefix = strndup(start, len); |
| 550 | LY_CHECK_ERR_GOTO(!val_pref->prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 551 | val_pref->mod = mod; |
| 552 | } |
| 553 | } /* else it is not even defined */ |
| 554 | } /* else the prefix is already present */ |
| 555 | } |
| 556 | stop = stop + bytes; |
| 557 | } |
| 558 | } |
| 559 | break; |
| 560 | case LY_PREF_SCHEMA_RESOLVED: |
| 561 | case LY_PREF_JSON: |
| 562 | /* simply copy all the prefix data */ |
| 563 | *format_p = format; |
| 564 | LY_CHECK_GOTO(ret = ly_dup_prefix_data(ctx, format, prefix_data, prefix_data_p), cleanup); |
| 565 | break; |
| 566 | } |
| 567 | |
| 568 | cleanup: |
| 569 | if (ret) { |
| 570 | ly_free_prefix_data(*format_p, *prefix_data_p); |
| 571 | *prefix_data_p = NULL; |
| 572 | } |
| 573 | return ret; |
| 574 | } |
Michal Vasko | 7ed1fcb | 2020-12-03 14:15:22 +0100 | [diff] [blame] | 575 | |
| 576 | const char * |
| 577 | ly_format2str(LY_PREFIX_FORMAT format) |
| 578 | { |
| 579 | switch (format) { |
| 580 | case LY_PREF_SCHEMA: |
| 581 | return "schema imports"; |
| 582 | case LY_PREF_SCHEMA_RESOLVED: |
| 583 | return "schema stored mapping"; |
| 584 | case LY_PREF_XML: |
| 585 | return "XML prefixes"; |
| 586 | case LY_PREF_JSON: |
| 587 | return "JSON module names"; |
| 588 | default: |
| 589 | break; |
| 590 | } |
| 591 | |
| 592 | return NULL; |
| 593 | } |