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