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