Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1 | /** |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 2 | * @file tree_data_common.c |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 4 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 5 | * @brief Parsing and validation common functions for data trees |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 6 | * |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 7 | * Copyright (c) 2015 - 2022 CESNET, z.s.p.o. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 8 | * |
| 9 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 10 | * You may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * https://opensource.org/licenses/BSD-3-Clause |
| 14 | */ |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 15 | |
| 16 | #define _GNU_SOURCE /* asprintf, strdup */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 17 | |
| 18 | #include <assert.h> |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 19 | #include <ctype.h> |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 20 | #include <stdint.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 21 | #include <stdlib.h> |
Radek Krejci | ad97c5f | 2020-06-30 09:19:28 +0200 | [diff] [blame] | 22 | #include <string.h> |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 23 | #include <time.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 24 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 25 | #include "common.h" |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 26 | #include "compat.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 27 | #include "context.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 28 | #include "dict.h" |
Radek Krejci | ad97c5f | 2020-06-30 09:19:28 +0200 | [diff] [blame] | 29 | #include "hash_table.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 30 | #include "log.h" |
Radek Krejci | ad97c5f | 2020-06-30 09:19:28 +0200 | [diff] [blame] | 31 | #include "lyb.h" |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 32 | #include "parser_data.h" |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 33 | #include "plugins_exts.h" |
Michal Vasko | a820c31 | 2021-02-05 16:33:00 +0100 | [diff] [blame] | 34 | #include "printer_data.h" |
Radek Krejci | ad97c5f | 2020-06-30 09:19:28 +0200 | [diff] [blame] | 35 | #include "set.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 36 | #include "tree.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 37 | #include "tree_data.h" |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 38 | #include "tree_data_internal.h" |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 39 | #include "tree_edit.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 40 | #include "tree_schema.h" |
Radek Krejci | 0aa1f70 | 2021-04-01 16:16:19 +0200 | [diff] [blame] | 41 | #include "tree_schema_internal.h" |
Radek Krejci | 4f2e3e5 | 2021-03-30 14:20:28 +0200 | [diff] [blame] | 42 | #include "validation.h" |
Radek Krejci | 7711410 | 2021-03-10 15:21:57 +0100 | [diff] [blame] | 43 | #include "xml.h" |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 44 | #include "xpath.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 45 | |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 46 | /** |
Michal Vasko | 271d2e3 | 2023-01-31 15:43:19 +0100 | [diff] [blame] | 47 | * @brief Callback for checking first instance hash table values equivalence. |
| 48 | * |
| 49 | * @param[in] val1_p If not @p mod, pointer to the first instance. |
| 50 | * @param[in] val2_p If not @p mod, pointer to the found dup inst item. |
| 51 | */ |
| 52 | static ly_bool |
| 53 | lyht_dup_inst_ht_equal_cb(void *val1_p, void *val2_p, ly_bool mod, void *UNUSED(cb_data)) |
| 54 | { |
| 55 | if (mod) { |
| 56 | struct lyd_dup_inst **item1 = val1_p, **item2 = val2_p; |
| 57 | |
| 58 | /* equal on 2 dup inst items */ |
| 59 | return *item1 == *item2 ? 1 : 0; |
| 60 | } else { |
| 61 | struct lyd_node **first_inst = val1_p; |
| 62 | struct lyd_dup_inst **item = val2_p; |
| 63 | |
| 64 | /* equal on dup inst item and a first instance */ |
| 65 | return (*item)->set->dnodes[0] == *first_inst ? 1 : 0; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 70 | * @brief Find an entry in duplicate instance cache for an instance. Create it if it does not exist. |
| 71 | * |
Michal Vasko | 271d2e3 | 2023-01-31 15:43:19 +0100 | [diff] [blame] | 72 | * @param[in] first_inst First instance of the cache entry. |
| 73 | * @param[in] dup_inst_ht Duplicate instance cache hash table. |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 74 | * @return Instance cache entry. |
| 75 | */ |
| 76 | static struct lyd_dup_inst * |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 77 | lyd_dup_inst_get(const struct lyd_node *first_inst, struct ly_ht **dup_inst_ht) |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 78 | { |
Michal Vasko | 271d2e3 | 2023-01-31 15:43:19 +0100 | [diff] [blame] | 79 | struct lyd_dup_inst **item_p, *item; |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 80 | |
Michal Vasko | 271d2e3 | 2023-01-31 15:43:19 +0100 | [diff] [blame] | 81 | if (*dup_inst_ht) { |
| 82 | /* find the item of the first instance */ |
| 83 | if (!lyht_find(*dup_inst_ht, &first_inst, first_inst->hash, (void **)&item_p)) { |
| 84 | return *item_p; |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 85 | } |
Michal Vasko | 271d2e3 | 2023-01-31 15:43:19 +0100 | [diff] [blame] | 86 | } else { |
| 87 | /* create the hash table */ |
| 88 | *dup_inst_ht = lyht_new(2, sizeof item, lyht_dup_inst_ht_equal_cb, NULL, 1); |
| 89 | LY_CHECK_RET(!*dup_inst_ht, NULL); |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 90 | } |
| 91 | |
Michal Vasko | 271d2e3 | 2023-01-31 15:43:19 +0100 | [diff] [blame] | 92 | /* first instance has no dup inst item, create it */ |
| 93 | item = calloc(1, sizeof *item); |
| 94 | LY_CHECK_RET(!item, NULL); |
| 95 | |
| 96 | /* add into the hash table */ |
| 97 | if (lyht_insert(*dup_inst_ht, &item, first_inst->hash, NULL)) { |
| 98 | return NULL; |
| 99 | } |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 100 | |
| 101 | return item; |
| 102 | } |
| 103 | |
| 104 | LY_ERR |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 105 | lyd_dup_inst_next(struct lyd_node **inst, const struct lyd_node *siblings, struct ly_ht **dup_inst_ht) |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 106 | { |
| 107 | struct lyd_dup_inst *dup_inst; |
| 108 | |
Michal Vasko | 83ae777 | 2022-06-08 10:01:55 +0200 | [diff] [blame] | 109 | if (!*inst) { |
| 110 | /* no match, inst is unchanged */ |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 111 | return LY_SUCCESS; |
| 112 | } |
| 113 | |
Michal Vasko | 83ae777 | 2022-06-08 10:01:55 +0200 | [diff] [blame] | 114 | /* there can be more exact same instances (even if not allowed in invalid data) and we must make sure we do not |
| 115 | * match a single node more times */ |
Michal Vasko | 271d2e3 | 2023-01-31 15:43:19 +0100 | [diff] [blame] | 116 | dup_inst = lyd_dup_inst_get(*inst, dup_inst_ht); |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 117 | LY_CHECK_ERR_RET(!dup_inst, LOGMEM(LYD_CTX(siblings)), LY_EMEM); |
| 118 | |
| 119 | if (!dup_inst->used) { |
| 120 | /* we did not cache these instances yet, do so */ |
Michal Vasko | 271d2e3 | 2023-01-31 15:43:19 +0100 | [diff] [blame] | 121 | lyd_find_sibling_dup_inst_set(siblings, *inst, &dup_inst->set); |
| 122 | assert(dup_inst->set->count && (dup_inst->set->dnodes[0] == *inst)); |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 123 | } |
| 124 | |
Michal Vasko | 271d2e3 | 2023-01-31 15:43:19 +0100 | [diff] [blame] | 125 | if (dup_inst->used == dup_inst->set->count) { |
Michal Vasko | 4525e1f | 2022-07-13 16:20:59 +0200 | [diff] [blame] | 126 | if (lysc_is_dup_inst_list((*inst)->schema)) { |
| 127 | /* we have used all the instances */ |
| 128 | *inst = NULL; |
| 129 | } /* else just keep using the last (ideally only) instance */ |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 130 | } else { |
Michal Vasko | 271d2e3 | 2023-01-31 15:43:19 +0100 | [diff] [blame] | 131 | assert(dup_inst->used < dup_inst->set->count); |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 132 | |
| 133 | /* use another instance */ |
Michal Vasko | 271d2e3 | 2023-01-31 15:43:19 +0100 | [diff] [blame] | 134 | *inst = dup_inst->set->dnodes[dup_inst->used]; |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 135 | ++dup_inst->used; |
| 136 | } |
| 137 | |
| 138 | return LY_SUCCESS; |
| 139 | } |
| 140 | |
Michal Vasko | 271d2e3 | 2023-01-31 15:43:19 +0100 | [diff] [blame] | 141 | /** |
| 142 | * @brief Callback for freeing first instance hash table values. |
| 143 | */ |
| 144 | static void |
| 145 | lyht_dup_inst_ht_free_cb(void *val_p) |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 146 | { |
Michal Vasko | 271d2e3 | 2023-01-31 15:43:19 +0100 | [diff] [blame] | 147 | struct lyd_dup_inst **item = val_p; |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 148 | |
Michal Vasko | 271d2e3 | 2023-01-31 15:43:19 +0100 | [diff] [blame] | 149 | ly_set_free((*item)->set, NULL); |
| 150 | free(*item); |
| 151 | } |
| 152 | |
| 153 | void |
Michal Vasko | 8efac24 | 2023-03-30 08:24:56 +0200 | [diff] [blame] | 154 | lyd_dup_inst_free(struct ly_ht *dup_inst_ht) |
Michal Vasko | 271d2e3 | 2023-01-31 15:43:19 +0100 | [diff] [blame] | 155 | { |
| 156 | lyht_free(dup_inst_ht, lyht_dup_inst_ht_free_cb); |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 157 | } |
| 158 | |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 159 | struct lyd_node * |
| 160 | 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] | 161 | const struct lysc_node *parent, const struct lysc_module *module) |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 162 | { |
| 163 | const struct lysc_node *siter = NULL; |
| 164 | struct lyd_node *match = NULL; |
| 165 | |
| 166 | assert(parent || module); |
| 167 | assert(!last || (slast && *slast)); |
| 168 | |
| 169 | if (slast) { |
| 170 | siter = *slast; |
| 171 | } |
| 172 | |
| 173 | if (last && last->next && (last->next->schema == siter)) { |
| 174 | /* return next data instance */ |
| 175 | return last->next; |
| 176 | } |
| 177 | |
| 178 | /* find next schema node data instance */ |
| 179 | while ((siter = lys_getnext(siter, parent, module, 0))) { |
| 180 | if (!lyd_find_sibling_val(sibling, siter, NULL, 0, &match)) { |
| 181 | break; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | if (slast) { |
| 186 | *slast = siter; |
| 187 | } |
| 188 | return match; |
| 189 | } |
| 190 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 191 | struct lyd_node ** |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 192 | lyd_node_child_p(struct lyd_node *node) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 193 | { |
| 194 | assert(node); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 195 | |
| 196 | if (!node->schema) { |
| 197 | return &((struct lyd_node_opaq *)node)->child; |
| 198 | } else { |
| 199 | switch (node->schema->nodetype) { |
| 200 | case LYS_CONTAINER: |
| 201 | case LYS_LIST: |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 202 | case LYS_RPC: |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 203 | case LYS_ACTION: |
| 204 | case LYS_NOTIF: |
| 205 | return &((struct lyd_node_inner *)node)->child; |
| 206 | default: |
| 207 | return NULL; |
| 208 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 212 | LIBYANG_API_DEF LY_ERR |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 213 | lyxp_vars_set(struct lyxp_var **vars, const char *name, const char *value) |
| 214 | { |
| 215 | LY_ERR ret = LY_SUCCESS; |
| 216 | char *var_name = NULL, *var_value = NULL; |
| 217 | struct lyxp_var *item; |
| 218 | |
| 219 | if (!vars || !name || !value) { |
| 220 | return LY_EINVAL; |
| 221 | } |
| 222 | |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 223 | /* if variable is already defined then change its value */ |
| 224 | if (*vars && !lyxp_vars_find(NULL, *vars, name, 0, &item)) { |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 225 | var_value = strdup(value); |
| 226 | LY_CHECK_RET(!var_value, LY_EMEM); |
| 227 | |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 228 | /* update value */ |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 229 | free(item->value); |
| 230 | item->value = var_value; |
| 231 | } else { |
| 232 | var_name = strdup(name); |
| 233 | var_value = strdup(value); |
| 234 | LY_CHECK_ERR_GOTO(!var_name || !var_value, ret = LY_EMEM, error); |
| 235 | |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 236 | /* add new variable */ |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 237 | LY_ARRAY_NEW_GOTO(NULL, *vars, item, ret, error); |
| 238 | item->name = var_name; |
| 239 | item->value = var_value; |
| 240 | } |
| 241 | |
| 242 | return LY_SUCCESS; |
| 243 | |
| 244 | error: |
| 245 | free(var_name); |
| 246 | free(var_value); |
| 247 | return ret; |
| 248 | } |
| 249 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 250 | LIBYANG_API_DEF void |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 251 | lyxp_vars_free(struct lyxp_var *vars) |
| 252 | { |
| 253 | LY_ARRAY_COUNT_TYPE u; |
| 254 | |
| 255 | if (!vars) { |
| 256 | return; |
| 257 | } |
| 258 | |
| 259 | LY_ARRAY_FOR(vars, u) { |
| 260 | free(vars[u].name); |
| 261 | free(vars[u].value); |
| 262 | } |
| 263 | |
| 264 | LY_ARRAY_FREE(vars); |
| 265 | } |
| 266 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 267 | LIBYANG_API_DEF struct lyd_node * |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 268 | lyd_child_no_keys(const struct lyd_node *node) |
| 269 | { |
| 270 | struct lyd_node **children; |
| 271 | |
| 272 | if (!node) { |
| 273 | return NULL; |
| 274 | } |
| 275 | |
| 276 | if (!node->schema) { |
| 277 | /* opaq node */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 278 | return ((struct lyd_node_opaq *)node)->child; |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 279 | } |
| 280 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 281 | children = lyd_node_child_p((struct lyd_node *)node); |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 282 | if (children) { |
| 283 | struct lyd_node *child = *children; |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 284 | |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 285 | while (child && child->schema && (child->schema->flags & LYS_KEY)) { |
| 286 | child = child->next; |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 287 | } |
| 288 | return child; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 289 | } else { |
| 290 | return NULL; |
| 291 | } |
| 292 | } |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 293 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 294 | LIBYANG_API_DEF const struct lys_module * |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 295 | lyd_owner_module(const struct lyd_node *node) |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 296 | { |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 297 | const struct lyd_node_opaq *opaq; |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 298 | |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 299 | if (!node) { |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 300 | return NULL; |
| 301 | } |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 302 | |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 303 | if (!node->schema) { |
| 304 | opaq = (struct lyd_node_opaq *)node; |
| 305 | switch (opaq->format) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 306 | case LY_VALUE_XML: |
Michal Vasko | 09fbb81 | 2023-04-13 14:11:06 +0200 | [diff] [blame] | 307 | if (opaq->name.module_ns) { |
| 308 | return ly_ctx_get_module_implemented_ns(LYD_CTX(node), opaq->name.module_ns); |
| 309 | } |
| 310 | break; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 311 | case LY_VALUE_JSON: |
Michal Vasko | 09fbb81 | 2023-04-13 14:11:06 +0200 | [diff] [blame] | 312 | if (opaq->name.module_name) { |
| 313 | return ly_ctx_get_module_implemented(LYD_CTX(node), opaq->name.module_name); |
| 314 | } |
| 315 | break; |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 316 | default: |
| 317 | return NULL; |
| 318 | } |
Michal Vasko | 09fbb81 | 2023-04-13 14:11:06 +0200 | [diff] [blame] | 319 | |
| 320 | /* try a parent */ |
| 321 | return lyd_owner_module(lyd_parent(node)); |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 322 | } |
| 323 | |
Michal Vasko | ef53c81 | 2021-10-13 10:21:03 +0200 | [diff] [blame] | 324 | return lysc_owner_module(node->schema); |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 325 | } |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 326 | |
Michal Vasko | 598063b | 2021-07-19 11:39:05 +0200 | [diff] [blame] | 327 | void |
| 328 | lyd_first_module_sibling(struct lyd_node **node, const struct lys_module *mod) |
| 329 | { |
| 330 | int cmp; |
| 331 | struct lyd_node *first; |
Michal Vasko | ec139eb | 2022-05-10 10:08:40 +0200 | [diff] [blame] | 332 | const struct lys_module *own_mod; |
Michal Vasko | 598063b | 2021-07-19 11:39:05 +0200 | [diff] [blame] | 333 | |
| 334 | assert(node && mod); |
| 335 | |
| 336 | if (!*node) { |
| 337 | return; |
| 338 | } |
| 339 | |
| 340 | first = *node; |
Michal Vasko | ec139eb | 2022-05-10 10:08:40 +0200 | [diff] [blame] | 341 | own_mod = lyd_owner_module(first); |
| 342 | cmp = own_mod ? strcmp(own_mod->name, mod->name) : 1; |
Michal Vasko | 598063b | 2021-07-19 11:39:05 +0200 | [diff] [blame] | 343 | if (cmp > 0) { |
| 344 | /* there may be some preceding data */ |
| 345 | while (first->prev->next) { |
| 346 | first = first->prev; |
| 347 | if (lyd_owner_module(first) == mod) { |
| 348 | cmp = 0; |
| 349 | break; |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | if (cmp == 0) { |
| 355 | /* there may be some preceding data belonging to this module */ |
| 356 | while (first->prev->next) { |
| 357 | if (lyd_owner_module(first->prev) != mod) { |
| 358 | break; |
| 359 | } |
| 360 | first = first->prev; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | if (cmp < 0) { |
| 365 | /* there may be some following data */ |
| 366 | LY_LIST_FOR(first, first) { |
| 367 | if (lyd_owner_module(first) == mod) { |
| 368 | cmp = 0; |
| 369 | break; |
| 370 | } |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | if (cmp == 0) { |
| 375 | /* we have found the first module data node */ |
| 376 | *node = first; |
| 377 | } |
| 378 | } |
| 379 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 380 | const struct lys_module * |
Michal Vasko | 26e8001 | 2020-07-08 10:55:46 +0200 | [diff] [blame] | 381 | 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] | 382 | struct lyd_node **first) |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 383 | { |
| 384 | struct lyd_node *iter; |
| 385 | const struct lys_module *mod; |
| 386 | |
| 387 | /* get the next module */ |
Michal Vasko | 26e8001 | 2020-07-08 10:55:46 +0200 | [diff] [blame] | 388 | if (module) { |
| 389 | if (*i) { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 390 | mod = NULL; |
Michal Vasko | 26e8001 | 2020-07-08 10:55:46 +0200 | [diff] [blame] | 391 | } else { |
| 392 | mod = module; |
| 393 | ++(*i); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 394 | } |
| 395 | } else { |
| 396 | do { |
| 397 | mod = ly_ctx_get_module_iter(ctx, i); |
| 398 | } while (mod && !mod->implemented); |
| 399 | } |
| 400 | |
| 401 | /* find its data */ |
| 402 | *first = NULL; |
| 403 | if (mod) { |
| 404 | LY_LIST_FOR(tree, iter) { |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 405 | if (lyd_owner_module(iter) == mod) { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 406 | *first = iter; |
| 407 | break; |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | return mod; |
| 413 | } |
| 414 | |
| 415 | const struct lys_module * |
| 416 | lyd_data_next_module(struct lyd_node **next, struct lyd_node **first) |
| 417 | { |
| 418 | const struct lys_module *mod; |
| 419 | |
| 420 | if (!*next) { |
| 421 | /* all data traversed */ |
| 422 | *first = NULL; |
| 423 | return NULL; |
| 424 | } |
| 425 | |
| 426 | *first = *next; |
| 427 | |
| 428 | /* prepare next */ |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 429 | mod = lyd_owner_module(*next); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 430 | LY_LIST_FOR(*next, *next) { |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 431 | if (lyd_owner_module(*next) != mod) { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 432 | break; |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | return mod; |
| 437 | } |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 438 | |
| 439 | LY_ERR |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 440 | lyd_value_store(const struct ly_ctx *ctx, struct lyd_value *val, const struct lysc_type *type, const void *value, |
| 441 | size_t value_len, ly_bool *dynamic, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, |
| 442 | const struct lysc_node *ctx_node, ly_bool *incomplete) |
| 443 | { |
| 444 | LY_ERR ret; |
| 445 | struct ly_err_item *err = NULL; |
| 446 | uint32_t options = (dynamic && *dynamic ? LYPLG_TYPE_STORE_DYNAMIC : 0); |
| 447 | |
| 448 | if (!value) { |
| 449 | value = ""; |
| 450 | } |
| 451 | if (incomplete) { |
| 452 | *incomplete = 0; |
| 453 | } |
| 454 | |
| 455 | ret = type->plugin->store(ctx, type, value, value_len, options, format, prefix_data, hints, ctx_node, val, NULL, &err); |
| 456 | if (dynamic) { |
| 457 | *dynamic = 0; |
| 458 | } |
| 459 | |
| 460 | if (ret == LY_EINCOMPLETE) { |
| 461 | if (incomplete) { |
| 462 | *incomplete = 1; |
| 463 | } |
| 464 | } else if (ret) { |
| 465 | if (err) { |
| 466 | LOGVAL_ERRITEM(ctx, err); |
| 467 | ly_err_free(err); |
| 468 | } else { |
| 469 | LOGVAL(ctx, LYVE_OTHER, "Storing value failed."); |
| 470 | } |
| 471 | return ret; |
| 472 | } |
| 473 | |
| 474 | return LY_SUCCESS; |
| 475 | } |
| 476 | |
| 477 | LY_ERR |
| 478 | lyd_value_validate_incomplete(const struct ly_ctx *ctx, const struct lysc_type *type, struct lyd_value *val, |
| 479 | const struct lyd_node *ctx_node, const struct lyd_node *tree) |
| 480 | { |
| 481 | LY_ERR ret; |
| 482 | struct ly_err_item *err = NULL; |
| 483 | |
| 484 | assert(type->plugin->validate); |
| 485 | |
| 486 | ret = type->plugin->validate(ctx, type, ctx_node, tree, val, &err); |
| 487 | if (ret) { |
| 488 | if (err) { |
| 489 | LOGVAL_ERRITEM(ctx, err); |
| 490 | ly_err_free(err); |
| 491 | } else { |
| 492 | LOGVAL(ctx, LYVE_OTHER, "Resolving value \"%s\" failed.", type->plugin->print(ctx, val, LY_VALUE_CANON, |
| 493 | NULL, NULL, NULL)); |
| 494 | } |
| 495 | return ret; |
| 496 | } |
| 497 | |
| 498 | return LY_SUCCESS; |
| 499 | } |
| 500 | |
| 501 | LY_ERR |
| 502 | lys_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len, |
| 503 | LY_VALUE_FORMAT format, void *prefix_data) |
| 504 | { |
| 505 | LY_ERR rc = LY_SUCCESS; |
| 506 | struct ly_err_item *err = NULL; |
| 507 | struct lyd_value storage; |
| 508 | struct lysc_type *type; |
| 509 | |
Michal Vasko | 0c36155 | 2023-04-04 10:04:03 +0200 | [diff] [blame] | 510 | LY_CHECK_ARG_RET(ctx, node, LY_EINVAL); |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 511 | |
| 512 | if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 513 | LOGARG(ctx, node); |
| 514 | return LY_EINVAL; |
| 515 | } |
| 516 | |
| 517 | type = ((struct lysc_node_leaf *)node)->type; |
| 518 | rc = type->plugin->store(ctx ? ctx : node->module->ctx, type, value, value_len, 0, format, prefix_data, |
| 519 | LYD_HINT_SCHEMA, node, &storage, NULL, &err); |
| 520 | if (rc == LY_EINCOMPLETE) { |
| 521 | /* actually success since we do not provide the context tree and call validation with |
| 522 | * LY_TYPE_OPTS_INCOMPLETE_DATA */ |
| 523 | rc = LY_SUCCESS; |
| 524 | } else if (rc && err) { |
| 525 | if (ctx) { |
| 526 | /* log only in case the ctx was provided as input parameter */ |
Michal Vasko | f8da268 | 2022-06-16 07:52:37 +0200 | [diff] [blame] | 527 | if (err->path) { |
| 528 | LOG_LOCSET(NULL, NULL, err->path, NULL); |
Michal Vasko | f8da268 | 2022-06-16 07:52:37 +0200 | [diff] [blame] | 529 | } |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 530 | LOGVAL_ERRITEM(ctx, err); |
Michal Vasko | f8da268 | 2022-06-16 07:52:37 +0200 | [diff] [blame] | 531 | if (err->path) { |
| 532 | LOG_LOCBACK(0, 0, 1, 0); |
Michal Vasko | f8da268 | 2022-06-16 07:52:37 +0200 | [diff] [blame] | 533 | } |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 534 | } |
| 535 | ly_err_free(err); |
| 536 | } |
| 537 | |
| 538 | if (!rc) { |
| 539 | type->plugin->free(ctx ? ctx : node->module->ctx, &storage); |
| 540 | } |
| 541 | return rc; |
| 542 | } |
| 543 | |
| 544 | LIBYANG_API_DEF LY_ERR |
| 545 | lyd_value_validate(const struct ly_ctx *ctx, const struct lysc_node *schema, const char *value, size_t value_len, |
| 546 | const struct lyd_node *ctx_node, const struct lysc_type **realtype, const char **canonical) |
| 547 | { |
| 548 | LY_ERR rc; |
| 549 | struct ly_err_item *err = NULL; |
| 550 | struct lysc_type *type; |
| 551 | struct lyd_value val = {0}; |
| 552 | ly_bool stored = 0, log = 1; |
| 553 | |
Michal Vasko | 3dd16da | 2022-06-15 07:58:41 +0200 | [diff] [blame] | 554 | LY_CHECK_ARG_RET(ctx, schema, !value_len || value, LY_EINVAL); |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 555 | |
| 556 | if (!ctx) { |
| 557 | ctx = schema->module->ctx; |
| 558 | log = 0; |
| 559 | } |
Michal Vasko | 3dd16da | 2022-06-15 07:58:41 +0200 | [diff] [blame] | 560 | if (!value_len) { |
| 561 | value = ""; |
| 562 | } |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 563 | type = ((struct lysc_node_leaf *)schema)->type; |
| 564 | |
| 565 | /* store */ |
| 566 | rc = type->plugin->store(ctx, type, value, value_len, 0, LY_VALUE_JSON, NULL, |
| 567 | LYD_HINT_DATA, schema, &val, NULL, &err); |
| 568 | if (!rc || (rc == LY_EINCOMPLETE)) { |
| 569 | stored = 1; |
| 570 | } |
| 571 | |
| 572 | if (ctx_node && (rc == LY_EINCOMPLETE)) { |
| 573 | /* resolve */ |
| 574 | rc = type->plugin->validate(ctx, type, ctx_node, ctx_node, &val, &err); |
| 575 | } |
| 576 | |
| 577 | if (rc && (rc != LY_EINCOMPLETE) && err) { |
| 578 | if (log) { |
| 579 | /* log error */ |
| 580 | if (err->path) { |
| 581 | LOG_LOCSET(NULL, NULL, err->path, NULL); |
| 582 | } else if (ctx_node) { |
| 583 | LOG_LOCSET(NULL, ctx_node, NULL, NULL); |
| 584 | } else { |
| 585 | LOG_LOCSET(schema, NULL, NULL, NULL); |
| 586 | } |
| 587 | LOGVAL_ERRITEM(ctx, err); |
| 588 | if (err->path) { |
| 589 | LOG_LOCBACK(0, 0, 1, 0); |
| 590 | } else if (ctx_node) { |
| 591 | LOG_LOCBACK(0, 1, 0, 0); |
| 592 | } else { |
| 593 | LOG_LOCBACK(1, 0, 0, 0); |
| 594 | } |
| 595 | } |
| 596 | ly_err_free(err); |
| 597 | } |
| 598 | |
| 599 | if (!rc || (rc == LY_EINCOMPLETE)) { |
| 600 | if (realtype) { |
| 601 | /* return realtype */ |
| 602 | if (val.realtype->basetype == LY_TYPE_UNION) { |
| 603 | *realtype = val.subvalue->value.realtype; |
| 604 | } else { |
| 605 | *realtype = val.realtype; |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | if (canonical) { |
| 610 | /* return canonical value */ |
| 611 | lydict_insert(ctx, val.realtype->plugin->print(ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL), 0, canonical); |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | if (stored) { |
| 616 | /* free value */ |
| 617 | type->plugin->free(ctx ? ctx : schema->module->ctx, &val); |
| 618 | } |
| 619 | return rc; |
| 620 | } |
| 621 | |
| 622 | LIBYANG_API_DEF LY_ERR |
| 623 | lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len) |
| 624 | { |
| 625 | LY_ERR ret = LY_SUCCESS; |
| 626 | struct ly_ctx *ctx; |
| 627 | struct lysc_type *type; |
| 628 | struct lyd_value val = {0}; |
| 629 | |
| 630 | LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, value, LY_EINVAL); |
| 631 | |
| 632 | ctx = node->schema->module->ctx; |
| 633 | type = ((struct lysc_node_leaf *)node->schema)->type; |
| 634 | |
| 635 | /* store the value */ |
| 636 | LOG_LOCSET(node->schema, &node->node, NULL, NULL); |
| 637 | ret = lyd_value_store(ctx, &val, type, value, value_len, NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, node->schema, NULL); |
| 638 | LOG_LOCBACK(1, 1, 0, 0); |
| 639 | LY_CHECK_RET(ret); |
| 640 | |
| 641 | /* compare values */ |
| 642 | ret = type->plugin->compare(&node->value, &val); |
| 643 | |
| 644 | type->plugin->free(ctx, &val); |
| 645 | return ret; |
| 646 | } |
| 647 | |
| 648 | LIBYANG_API_DEF ly_bool |
| 649 | lyd_is_default(const struct lyd_node *node) |
| 650 | { |
| 651 | const struct lysc_node_leaf *leaf; |
| 652 | const struct lysc_node_leaflist *llist; |
| 653 | const struct lyd_node_term *term; |
| 654 | LY_ARRAY_COUNT_TYPE u; |
| 655 | |
| 656 | if (!(node->schema->nodetype & LYD_NODE_TERM)) { |
| 657 | return 0; |
| 658 | } |
| 659 | |
| 660 | term = (const struct lyd_node_term *)node; |
| 661 | |
| 662 | if (node->schema->nodetype == LYS_LEAF) { |
| 663 | leaf = (const struct lysc_node_leaf *)node->schema; |
| 664 | if (!leaf->dflt) { |
| 665 | return 0; |
| 666 | } |
| 667 | |
| 668 | /* compare with the default value */ |
| 669 | if (!leaf->type->plugin->compare(&term->value, leaf->dflt)) { |
| 670 | return 1; |
| 671 | } |
| 672 | } else { |
| 673 | llist = (const struct lysc_node_leaflist *)node->schema; |
| 674 | if (!llist->dflts) { |
| 675 | return 0; |
| 676 | } |
| 677 | |
| 678 | LY_ARRAY_FOR(llist->dflts, u) { |
| 679 | /* compare with each possible default value */ |
| 680 | if (!llist->type->plugin->compare(&term->value, llist->dflts[u])) { |
| 681 | return 1; |
| 682 | } |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | return 0; |
| 687 | } |
| 688 | |
| 689 | LIBYANG_API_DEF uint32_t |
| 690 | lyd_list_pos(const struct lyd_node *instance) |
| 691 | { |
| 692 | const struct lyd_node *iter = NULL; |
| 693 | uint32_t pos = 0; |
| 694 | |
| 695 | if (!instance || !(instance->schema->nodetype & (LYS_LIST | LYS_LEAFLIST))) { |
| 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | /* data instances are ordered, so we can stop when we found instance of other schema node */ |
| 700 | for (iter = instance; iter->schema == instance->schema; iter = iter->prev) { |
| 701 | if (pos && (iter->next == NULL)) { |
| 702 | /* overrun to the end of the siblings list */ |
| 703 | break; |
| 704 | } |
| 705 | ++pos; |
| 706 | } |
| 707 | |
| 708 | return pos; |
| 709 | } |
| 710 | |
| 711 | LIBYANG_API_DEF struct lyd_node * |
| 712 | lyd_first_sibling(const struct lyd_node *node) |
| 713 | { |
| 714 | struct lyd_node *start; |
| 715 | |
| 716 | if (!node) { |
| 717 | return NULL; |
| 718 | } |
| 719 | |
| 720 | /* get the first sibling */ |
| 721 | if (node->parent) { |
| 722 | start = node->parent->child; |
| 723 | } else { |
| 724 | for (start = (struct lyd_node *)node; start->prev->next; start = start->prev) {} |
| 725 | } |
| 726 | |
| 727 | return start; |
| 728 | } |
| 729 | |
Michal Vasko | bfff6ac | 2022-02-23 16:22:53 +0100 | [diff] [blame] | 730 | /** |
| 731 | * @brief Check list node parsed into an opaque node for the reason. |
| 732 | * |
| 733 | * @param[in] node Opaque node. |
| 734 | * @param[in] snode Schema node of @p opaq. |
| 735 | * @return LY_SUCCESS if the node is valid; |
| 736 | * @return LY_ERR on error. |
| 737 | */ |
| 738 | static LY_ERR |
| 739 | lyd_parse_opaq_list_error(const struct lyd_node *node, const struct lysc_node *snode) |
| 740 | { |
| 741 | LY_ERR ret = LY_SUCCESS; |
| 742 | struct ly_set key_set = {0}; |
| 743 | const struct lysc_node *key = NULL; |
| 744 | const struct lyd_node *child; |
| 745 | const struct lyd_node_opaq *opaq_k; |
| 746 | uint32_t i; |
| 747 | |
| 748 | assert(!node->schema); |
| 749 | |
| 750 | /* get all keys into a set */ |
| 751 | while ((key = lys_getnext(key, snode, NULL, 0)) && (snode->flags & LYS_KEY)) { |
| 752 | LY_CHECK_GOTO(ret = ly_set_add(&key_set, (void *)snode, 1, NULL), cleanup); |
| 753 | } |
| 754 | |
| 755 | LY_LIST_FOR(lyd_child(node), child) { |
| 756 | if (child->schema) { |
| 757 | LOGERR(LYD_CTX(node), LY_EINVAL, "Unexpected node %s \"%s\".", lys_nodetype2str(child->schema->nodetype), |
| 758 | LYD_NAME(child)); |
| 759 | ret = LY_EINVAL; |
| 760 | goto cleanup; |
| 761 | } |
| 762 | |
| 763 | opaq_k = (struct lyd_node_opaq *)child; |
| 764 | |
| 765 | /* find the key schema node */ |
| 766 | for (i = 0; i < key_set.count; ++i) { |
| 767 | key = key_set.snodes[i]; |
| 768 | if (!strcmp(key->name, opaq_k->name.name)) { |
| 769 | break; |
| 770 | } |
| 771 | } |
| 772 | if (i == key_set.count) { |
| 773 | /* some other node, skip */ |
| 774 | continue; |
| 775 | } |
| 776 | |
| 777 | /* key found */ |
| 778 | ly_set_rm_index(&key_set, i, NULL); |
| 779 | |
| 780 | /* check value */ |
| 781 | ret = lys_value_validate(LYD_CTX(node), key, opaq_k->value, strlen(opaq_k->value), opaq_k->format, |
| 782 | opaq_k->val_prefix_data); |
| 783 | LY_CHECK_GOTO(ret, cleanup); |
| 784 | } |
| 785 | |
| 786 | if (key_set.count) { |
| 787 | /* missing keys */ |
| 788 | LOGVAL(LYD_CTX(node), LY_VCODE_NOKEY, key_set.snodes[0]->name); |
| 789 | ret = LY_EVALID; |
| 790 | goto cleanup; |
| 791 | } |
| 792 | |
| 793 | cleanup: |
| 794 | ly_set_erase(&key_set, NULL); |
| 795 | return ret; |
| 796 | } |
| 797 | |
| 798 | LIBYANG_API_DEF LY_ERR |
| 799 | lyd_parse_opaq_error(const struct lyd_node *node) |
| 800 | { |
Michal Vasko | 6727c68 | 2023-02-17 10:40:26 +0100 | [diff] [blame] | 801 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | bfff6ac | 2022-02-23 16:22:53 +0100 | [diff] [blame] | 802 | const struct ly_ctx *ctx; |
| 803 | const struct lyd_node_opaq *opaq; |
| 804 | const struct lyd_node *parent; |
| 805 | const struct lys_module *mod; |
| 806 | const struct lysc_node *snode; |
Michal Vasko | 6727c68 | 2023-02-17 10:40:26 +0100 | [diff] [blame] | 807 | uint32_t loc_node = 0, loc_path = 0; |
Michal Vasko | bfff6ac | 2022-02-23 16:22:53 +0100 | [diff] [blame] | 808 | |
| 809 | LY_CHECK_ARG_RET(LYD_CTX(node), node, !node->schema, !lyd_parent(node) || lyd_parent(node)->schema, LY_EINVAL); |
| 810 | |
| 811 | ctx = LYD_CTX(node); |
| 812 | opaq = (struct lyd_node_opaq *)node; |
| 813 | parent = lyd_parent(node); |
| 814 | |
Michal Vasko | 6727c68 | 2023-02-17 10:40:26 +0100 | [diff] [blame] | 815 | if (lyd_parent(node)) { |
| 816 | LOG_LOCSET(NULL, lyd_parent(node), NULL, NULL); |
| 817 | ++loc_node; |
| 818 | } else { |
| 819 | LOG_LOCSET(NULL, NULL, "/", NULL); |
| 820 | ++loc_path; |
| 821 | } |
| 822 | |
Michal Vasko | f4e6392 | 2022-05-10 10:32:13 +0200 | [diff] [blame] | 823 | if (!opaq->name.module_ns) { |
| 824 | LOGVAL(ctx, LYVE_REFERENCE, "Unknown module of node \"%s\".", opaq->name.name); |
Michal Vasko | 6727c68 | 2023-02-17 10:40:26 +0100 | [diff] [blame] | 825 | rc = LY_EVALID; |
| 826 | goto cleanup; |
Michal Vasko | f4e6392 | 2022-05-10 10:32:13 +0200 | [diff] [blame] | 827 | } |
Michal Vasko | bfff6ac | 2022-02-23 16:22:53 +0100 | [diff] [blame] | 828 | |
| 829 | /* module */ |
| 830 | switch (opaq->format) { |
| 831 | case LY_VALUE_XML: |
| 832 | if (!parent || strcmp(opaq->name.module_ns, parent->schema->module->ns)) { |
| 833 | mod = ly_ctx_get_module_implemented_ns(ctx, opaq->name.module_ns); |
| 834 | if (!mod) { |
Michal Vasko | 959f8d8 | 2022-06-16 07:51:50 +0200 | [diff] [blame] | 835 | LOGVAL(ctx, LYVE_REFERENCE, "No (implemented) module with namespace \"%s\" of node \"%s\" in the context.", |
| 836 | opaq->name.module_ns, opaq->name.name); |
Michal Vasko | 6727c68 | 2023-02-17 10:40:26 +0100 | [diff] [blame] | 837 | rc = LY_EVALID; |
| 838 | goto cleanup; |
Michal Vasko | bfff6ac | 2022-02-23 16:22:53 +0100 | [diff] [blame] | 839 | } |
| 840 | } else { |
| 841 | /* inherit */ |
| 842 | mod = parent->schema->module; |
| 843 | } |
| 844 | break; |
| 845 | case LY_VALUE_JSON: |
| 846 | case LY_VALUE_LYB: |
| 847 | if (!parent || strcmp(opaq->name.module_name, parent->schema->module->name)) { |
| 848 | mod = ly_ctx_get_module_implemented(ctx, opaq->name.module_name); |
| 849 | if (!mod) { |
Michal Vasko | 959f8d8 | 2022-06-16 07:51:50 +0200 | [diff] [blame] | 850 | LOGVAL(ctx, LYVE_REFERENCE, "No (implemented) module named \"%s\" of node \"%s\" in the context.", |
| 851 | opaq->name.module_name, opaq->name.name); |
Michal Vasko | 6727c68 | 2023-02-17 10:40:26 +0100 | [diff] [blame] | 852 | rc = LY_EVALID; |
| 853 | goto cleanup; |
Michal Vasko | bfff6ac | 2022-02-23 16:22:53 +0100 | [diff] [blame] | 854 | } |
| 855 | } else { |
| 856 | /* inherit */ |
| 857 | mod = parent->schema->module; |
| 858 | } |
| 859 | break; |
| 860 | default: |
| 861 | LOGERR(ctx, LY_EINVAL, "Unsupported value format."); |
Michal Vasko | 6727c68 | 2023-02-17 10:40:26 +0100 | [diff] [blame] | 862 | rc = LY_EINVAL; |
| 863 | goto cleanup; |
Michal Vasko | bfff6ac | 2022-02-23 16:22:53 +0100 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | /* schema */ |
| 867 | snode = lys_find_child(parent ? parent->schema : NULL, mod, opaq->name.name, 0, 0, 0); |
Michal Vasko | ac6f4be | 2022-05-02 10:16:50 +0200 | [diff] [blame] | 868 | if (!snode && parent && parent->schema && (parent->schema->nodetype & (LYS_RPC | LYS_ACTION))) { |
| 869 | /* maybe output node */ |
Michal Vasko | 89afc6e | 2022-05-02 10:24:26 +0200 | [diff] [blame] | 870 | snode = lys_find_child(parent->schema, mod, opaq->name.name, 0, 0, LYS_GETNEXT_OUTPUT); |
Michal Vasko | ac6f4be | 2022-05-02 10:16:50 +0200 | [diff] [blame] | 871 | } |
Michal Vasko | bfff6ac | 2022-02-23 16:22:53 +0100 | [diff] [blame] | 872 | if (!snode) { |
| 873 | if (parent) { |
| 874 | LOGVAL(ctx, LYVE_REFERENCE, "Node \"%s\" not found as a child of \"%s\" node.", opaq->name.name, |
| 875 | LYD_NAME(parent)); |
| 876 | } else { |
| 877 | LOGVAL(ctx, LYVE_REFERENCE, "Node \"%s\" not found in the \"%s\" module.", opaq->name.name, mod->name); |
| 878 | } |
Michal Vasko | 6727c68 | 2023-02-17 10:40:26 +0100 | [diff] [blame] | 879 | rc = LY_EVALID; |
| 880 | goto cleanup; |
Michal Vasko | bfff6ac | 2022-02-23 16:22:53 +0100 | [diff] [blame] | 881 | } |
| 882 | |
Michal Vasko | 6727c68 | 2023-02-17 10:40:26 +0100 | [diff] [blame] | 883 | /* schema node exists */ |
| 884 | LOG_LOCBACK(0, loc_node, loc_path, 0); |
| 885 | loc_node = 0; |
| 886 | loc_path = 0; |
| 887 | LOG_LOCSET(NULL, node, NULL, NULL); |
| 888 | ++loc_node; |
| 889 | |
Michal Vasko | bfff6ac | 2022-02-23 16:22:53 +0100 | [diff] [blame] | 890 | if (snode->nodetype & LYD_NODE_TERM) { |
| 891 | /* leaf / leaf-list */ |
Michal Vasko | 6727c68 | 2023-02-17 10:40:26 +0100 | [diff] [blame] | 892 | rc = lys_value_validate(ctx, snode, opaq->value, strlen(opaq->value), opaq->format, opaq->val_prefix_data); |
| 893 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | bfff6ac | 2022-02-23 16:22:53 +0100 | [diff] [blame] | 894 | } else if (snode->nodetype == LYS_LIST) { |
| 895 | /* list */ |
Michal Vasko | 6727c68 | 2023-02-17 10:40:26 +0100 | [diff] [blame] | 896 | rc = lyd_parse_opaq_list_error(node, snode); |
| 897 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | bfff6ac | 2022-02-23 16:22:53 +0100 | [diff] [blame] | 898 | } else if (snode->nodetype & LYD_NODE_INNER) { |
| 899 | /* inner node */ |
| 900 | if (opaq->value) { |
| 901 | LOGVAL(ctx, LYVE_DATA, "Invalid value \"%s\" for %s \"%s\".", opaq->value, |
| 902 | lys_nodetype2str(snode->nodetype), snode->name); |
Michal Vasko | 6727c68 | 2023-02-17 10:40:26 +0100 | [diff] [blame] | 903 | rc = LY_EVALID; |
| 904 | goto cleanup; |
Michal Vasko | bfff6ac | 2022-02-23 16:22:53 +0100 | [diff] [blame] | 905 | } |
| 906 | } else { |
| 907 | LOGERR(ctx, LY_EINVAL, "Unexpected opaque schema node %s \"%s\".", lys_nodetype2str(snode->nodetype), snode->name); |
Michal Vasko | 6727c68 | 2023-02-17 10:40:26 +0100 | [diff] [blame] | 908 | rc = LY_EINVAL; |
| 909 | goto cleanup; |
Michal Vasko | bfff6ac | 2022-02-23 16:22:53 +0100 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | LOGERR(ctx, LY_EINVAL, "Unexpected valid opaque node %s \"%s\".", lys_nodetype2str(snode->nodetype), snode->name); |
Michal Vasko | 6727c68 | 2023-02-17 10:40:26 +0100 | [diff] [blame] | 913 | rc = LY_EINVAL; |
| 914 | |
| 915 | cleanup: |
| 916 | LOG_LOCBACK(0, loc_node, loc_path, 0); |
| 917 | return rc; |
Michal Vasko | bfff6ac | 2022-02-23 16:22:53 +0100 | [diff] [blame] | 918 | } |
| 919 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 920 | LIBYANG_API_DEF const char * |
Christian Hopps | 46bd21b | 2021-04-27 09:43:58 -0400 | [diff] [blame] | 921 | lyd_value_get_canonical(const struct ly_ctx *ctx, const struct lyd_value *value) |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 922 | { |
Michal Vasko | ab40e7e | 2021-04-28 17:04:24 +0200 | [diff] [blame] | 923 | LY_CHECK_ARG_RET(ctx, ctx, value, NULL); |
| 924 | |
Michal Vasko | 3387602 | 2021-04-27 16:42:24 +0200 | [diff] [blame] | 925 | return value->_canonical ? value->_canonical : |
| 926 | (const char *)value->realtype->plugin->print(ctx, value, LY_VALUE_CANON, NULL, NULL, NULL); |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 927 | } |
| 928 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 929 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | a820c31 | 2021-02-05 16:33:00 +0100 | [diff] [blame] | 930 | lyd_any_value_str(const struct lyd_node *any, char **value_str) |
| 931 | { |
| 932 | const struct lyd_node_any *a; |
| 933 | struct lyd_node *tree = NULL; |
| 934 | const char *str = NULL; |
| 935 | ly_bool dynamic = 0; |
| 936 | LY_ERR ret = LY_SUCCESS; |
| 937 | |
| 938 | LY_CHECK_ARG_RET(NULL, any, value_str, LY_EINVAL); |
Radek Krejci | 71877df | 2021-04-06 17:24:06 +0200 | [diff] [blame] | 939 | LY_CHECK_ARG_RET(NULL, any->schema, any->schema->nodetype & LYS_ANYDATA, LY_EINVAL); |
Michal Vasko | a820c31 | 2021-02-05 16:33:00 +0100 | [diff] [blame] | 940 | |
| 941 | a = (struct lyd_node_any *)any; |
| 942 | *value_str = NULL; |
| 943 | |
| 944 | if (!a->value.str) { |
| 945 | /* there is no value in the union */ |
| 946 | return LY_SUCCESS; |
| 947 | } |
| 948 | |
| 949 | switch (a->value_type) { |
| 950 | case LYD_ANYDATA_LYB: |
| 951 | /* parse into a data tree */ |
| 952 | ret = lyd_parse_data_mem(LYD_CTX(any), a->value.mem, LYD_LYB, LYD_PARSE_ONLY, 0, &tree); |
| 953 | LY_CHECK_GOTO(ret, cleanup); |
| 954 | dynamic = 1; |
| 955 | break; |
| 956 | case LYD_ANYDATA_DATATREE: |
| 957 | tree = a->value.tree; |
| 958 | break; |
| 959 | case LYD_ANYDATA_STRING: |
| 960 | case LYD_ANYDATA_XML: |
| 961 | case LYD_ANYDATA_JSON: |
| 962 | /* simply use the string */ |
| 963 | str = a->value.str; |
| 964 | break; |
| 965 | } |
| 966 | |
| 967 | if (tree) { |
| 968 | /* print into a string */ |
| 969 | ret = lyd_print_mem(value_str, tree, LYD_XML, LYD_PRINT_WITHSIBLINGS); |
| 970 | LY_CHECK_GOTO(ret, cleanup); |
| 971 | } else { |
| 972 | assert(str); |
| 973 | *value_str = strdup(str); |
| 974 | LY_CHECK_ERR_GOTO(!*value_str, LOGMEM(LYD_CTX(any)), cleanup); |
| 975 | } |
| 976 | |
| 977 | /* success */ |
| 978 | |
| 979 | cleanup: |
| 980 | if (dynamic) { |
| 981 | lyd_free_all(tree); |
| 982 | } |
| 983 | return ret; |
| 984 | } |
| 985 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 986 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 987 | lyd_any_copy_value(struct lyd_node *trg, const union lyd_any_value *value, LYD_ANYDATA_VALUETYPE value_type) |
| 988 | { |
| 989 | struct lyd_node_any *t; |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 990 | |
Michal Vasko | a820c31 | 2021-02-05 16:33:00 +0100 | [diff] [blame] | 991 | LY_CHECK_ARG_RET(NULL, trg, LY_EINVAL); |
Radek Krejci | 71877df | 2021-04-06 17:24:06 +0200 | [diff] [blame] | 992 | LY_CHECK_ARG_RET(NULL, trg->schema, trg->schema->nodetype & LYS_ANYDATA, LY_EINVAL); |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 993 | |
| 994 | t = (struct lyd_node_any *)trg; |
| 995 | |
| 996 | /* free trg */ |
| 997 | switch (t->value_type) { |
| 998 | case LYD_ANYDATA_DATATREE: |
| 999 | lyd_free_all(t->value.tree); |
| 1000 | break; |
| 1001 | case LYD_ANYDATA_STRING: |
| 1002 | case LYD_ANYDATA_XML: |
| 1003 | case LYD_ANYDATA_JSON: |
Michal Vasko | e180ed0 | 2021-02-05 16:31:20 +0100 | [diff] [blame] | 1004 | lydict_remove(LYD_CTX(trg), t->value.str); |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 1005 | break; |
| 1006 | case LYD_ANYDATA_LYB: |
| 1007 | free(t->value.mem); |
| 1008 | break; |
| 1009 | } |
| 1010 | t->value.str = NULL; |
| 1011 | |
| 1012 | if (!value) { |
| 1013 | /* only free value in this case */ |
| 1014 | return LY_SUCCESS; |
| 1015 | } |
| 1016 | |
| 1017 | /* copy src */ |
| 1018 | t->value_type = value_type; |
| 1019 | switch (value_type) { |
| 1020 | case LYD_ANYDATA_DATATREE: |
| 1021 | if (value->tree) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1022 | 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] | 1023 | } |
| 1024 | break; |
| 1025 | case LYD_ANYDATA_STRING: |
| 1026 | case LYD_ANYDATA_XML: |
| 1027 | case LYD_ANYDATA_JSON: |
| 1028 | if (value->str) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 1029 | 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] | 1030 | } |
| 1031 | break; |
| 1032 | case LYD_ANYDATA_LYB: |
| 1033 | if (value->mem) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1034 | int len = lyd_lyb_data_length(value->mem); |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 1035 | |
Radek Krejci | 82fa8d4 | 2020-07-11 22:00:59 +0200 | [diff] [blame] | 1036 | LY_CHECK_RET(len == -1, LY_EINVAL); |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 1037 | t->value.mem = malloc(len); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1038 | 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] | 1039 | memcpy(t->value.mem, value->mem, len); |
| 1040 | } |
| 1041 | break; |
| 1042 | } |
| 1043 | |
| 1044 | return LY_SUCCESS; |
| 1045 | } |
| 1046 | |
Michal Vasko | 106f086 | 2021-11-02 11:49:27 +0100 | [diff] [blame] | 1047 | const struct lysc_node * |
| 1048 | lyd_node_schema(const struct lyd_node *node) |
| 1049 | { |
| 1050 | const struct lysc_node *schema = NULL; |
| 1051 | const struct lyd_node *prev_iter = NULL, *iter; |
| 1052 | const struct lys_module *mod; |
| 1053 | |
| 1054 | if (!node) { |
| 1055 | return NULL; |
| 1056 | } else if (node->schema) { |
| 1057 | return node->schema; |
| 1058 | } |
| 1059 | |
| 1060 | /* get schema node of an opaque node */ |
| 1061 | do { |
| 1062 | /* get next data node */ |
| 1063 | for (iter = node; lyd_parent(iter) != prev_iter; iter = lyd_parent(iter)) {} |
| 1064 | |
| 1065 | /* get equivalent schema node */ |
| 1066 | if (iter->schema) { |
| 1067 | schema = iter->schema; |
| 1068 | } else { |
| 1069 | /* get module */ |
| 1070 | mod = lyd_owner_module(iter); |
Michal Vasko | a41826a | 2021-11-02 12:13:03 +0100 | [diff] [blame] | 1071 | if (!mod && !schema) { |
| 1072 | /* top-level opaque node has unknown module */ |
| 1073 | break; |
| 1074 | } |
Michal Vasko | 106f086 | 2021-11-02 11:49:27 +0100 | [diff] [blame] | 1075 | |
| 1076 | /* get schema node */ |
| 1077 | schema = lys_find_child(schema, mod ? mod : schema->module, LYD_NAME(iter), 0, 0, 0); |
| 1078 | } |
Michal Vasko | d2f404f | 2021-11-04 15:37:11 +0100 | [diff] [blame] | 1079 | |
| 1080 | /* remember to move to the descendant */ |
| 1081 | prev_iter = iter; |
Michal Vasko | 106f086 | 2021-11-02 11:49:27 +0100 | [diff] [blame] | 1082 | } while (schema && (iter != node)); |
| 1083 | |
| 1084 | return schema; |
| 1085 | } |
| 1086 | |
Michal Vasko | 4754d4a | 2022-12-01 10:11:21 +0100 | [diff] [blame] | 1087 | void |
| 1088 | lyd_cont_set_dflt(struct lyd_node *node) |
| 1089 | { |
| 1090 | const struct lyd_node *child; |
| 1091 | |
| 1092 | while (node) { |
| 1093 | if (!node->schema || (node->flags & LYD_DEFAULT) || !lysc_is_np_cont(node->schema)) { |
| 1094 | /* not a non-dflt NP container */ |
| 1095 | break; |
| 1096 | } |
| 1097 | |
| 1098 | LY_LIST_FOR(lyd_child(node), child) { |
| 1099 | if (!(child->flags & LYD_DEFAULT)) { |
| 1100 | break; |
| 1101 | } |
| 1102 | } |
| 1103 | if (child) { |
| 1104 | /* explicit child, no dflt change */ |
| 1105 | break; |
| 1106 | } |
| 1107 | |
| 1108 | /* set the dflt flag */ |
| 1109 | node->flags |= LYD_DEFAULT; |
| 1110 | |
| 1111 | /* check all parent containers */ |
| 1112 | node = lyd_parent(node); |
| 1113 | } |
| 1114 | } |
| 1115 | |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1116 | /** |
| 1117 | * @brief Comparison callback to match schema node with a schema of a data node. |
| 1118 | * |
| 1119 | * @param[in] val1_p Pointer to the schema node |
| 1120 | * @param[in] val2_p Pointer to the data node |
| 1121 | * Implementation of ::lyht_value_equal_cb. |
| 1122 | */ |
| 1123 | static ly_bool |
| 1124 | lyd_hash_table_schema_val_equal(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data)) |
| 1125 | { |
| 1126 | struct lysc_node *val1; |
| 1127 | struct lyd_node *val2; |
| 1128 | |
| 1129 | val1 = *((struct lysc_node **)val1_p); |
| 1130 | val2 = *((struct lyd_node **)val2_p); |
| 1131 | |
| 1132 | if (val1 == val2->schema) { |
| 1133 | /* schema match is enough */ |
| 1134 | return 1; |
| 1135 | } else { |
| 1136 | return 0; |
| 1137 | } |
| 1138 | } |
| 1139 | |
| 1140 | LY_ERR |
| 1141 | lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match) |
| 1142 | { |
| 1143 | struct lyd_node **match_p; |
| 1144 | struct lyd_node_inner *parent; |
Michal Vasko | df8ebf6 | 2022-11-10 10:33:28 +0100 | [diff] [blame] | 1145 | const struct lysc_node *cur_schema; |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1146 | uint32_t hash; |
| 1147 | lyht_value_equal_cb ht_cb; |
| 1148 | |
Michal Vasko | 21beaeb | 2022-08-02 10:42:48 +0200 | [diff] [blame] | 1149 | assert(schema); |
| 1150 | if (!siblings) { |
| 1151 | /* no data */ |
| 1152 | if (match) { |
| 1153 | *match = NULL; |
| 1154 | } |
| 1155 | return LY_ENOTFOUND; |
| 1156 | } |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1157 | |
| 1158 | parent = siblings->parent; |
| 1159 | if (parent && parent->schema && parent->children_ht) { |
| 1160 | /* calculate our hash */ |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 1161 | hash = lyht_hash_multi(0, schema->module->name, strlen(schema->module->name)); |
| 1162 | hash = lyht_hash_multi(hash, schema->name, strlen(schema->name)); |
| 1163 | hash = lyht_hash_multi(hash, NULL, 0); |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1164 | |
| 1165 | /* use special hash table function */ |
| 1166 | ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal); |
| 1167 | |
| 1168 | /* find by hash */ |
| 1169 | if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) { |
| 1170 | siblings = *match_p; |
| 1171 | } else { |
| 1172 | /* not found */ |
| 1173 | siblings = NULL; |
| 1174 | } |
| 1175 | |
| 1176 | /* set the original hash table compare function back */ |
| 1177 | lyht_set_cb(parent->children_ht, ht_cb); |
| 1178 | } else { |
| 1179 | /* find first sibling */ |
| 1180 | if (siblings->parent) { |
| 1181 | siblings = siblings->parent->child; |
| 1182 | } else { |
| 1183 | while (siblings->prev->next) { |
| 1184 | siblings = siblings->prev; |
| 1185 | } |
| 1186 | } |
| 1187 | |
| 1188 | /* search manually without hashes */ |
| 1189 | for ( ; siblings; siblings = siblings->next) { |
Michal Vasko | df8ebf6 | 2022-11-10 10:33:28 +0100 | [diff] [blame] | 1190 | cur_schema = lyd_node_schema(siblings); |
| 1191 | if (!cur_schema) { |
| 1192 | /* some unknown opaque node */ |
| 1193 | continue; |
Michal Vasko | 31bac38 | 2022-07-20 08:07:59 +0200 | [diff] [blame] | 1194 | } |
Michal Vasko | df8ebf6 | 2022-11-10 10:33:28 +0100 | [diff] [blame] | 1195 | |
| 1196 | /* schema match is enough */ |
| 1197 | if (cur_schema->module->ctx == schema->module->ctx) { |
| 1198 | if (cur_schema == schema) { |
| 1199 | break; |
| 1200 | } |
| 1201 | } else { |
| 1202 | if (!strcmp(cur_schema->name, schema->name) && !strcmp(cur_schema->module->name, schema->module->name)) { |
| 1203 | break; |
| 1204 | } |
Michal Vasko | 59892dd | 2022-05-13 11:02:30 +0200 | [diff] [blame] | 1205 | } |
| 1206 | } |
| 1207 | } |
| 1208 | |
| 1209 | if (!siblings) { |
| 1210 | if (match) { |
| 1211 | *match = NULL; |
| 1212 | } |
| 1213 | return LY_ENOTFOUND; |
| 1214 | } |
| 1215 | |
| 1216 | if (match) { |
| 1217 | *match = (struct lyd_node *)siblings; |
| 1218 | } |
| 1219 | return LY_SUCCESS; |
| 1220 | } |
| 1221 | |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 1222 | void |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 1223 | lyd_del_move_root(struct lyd_node **root, const struct lyd_node *to_del, const struct lys_module *mod) |
| 1224 | { |
| 1225 | if (*root && (lyd_owner_module(*root) != mod)) { |
| 1226 | /* there are no data of mod so this is simply the first top-level sibling */ |
| 1227 | mod = NULL; |
| 1228 | } |
| 1229 | |
| 1230 | if ((*root != to_del) || (*root)->parent) { |
| 1231 | return; |
| 1232 | } |
| 1233 | |
Michal Vasko | 598063b | 2021-07-19 11:39:05 +0200 | [diff] [blame] | 1234 | if (mod && (*root)->prev->next && (!(*root)->next || (lyd_owner_module(to_del) != lyd_owner_module((*root)->next)))) { |
| 1235 | /* there are no more nodes from mod, simply get the first top-level sibling */ |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 1236 | *root = lyd_first_sibling(*root); |
Michal Vasko | 598063b | 2021-07-19 11:39:05 +0200 | [diff] [blame] | 1237 | } else { |
| 1238 | *root = (*root)->next; |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 1239 | } |
| 1240 | } |
| 1241 | |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1242 | LY_ERR |
| 1243 | ly_nested_ext_schema(const struct lyd_node *parent, const struct lysc_node *sparent, const char *prefix, |
| 1244 | size_t prefix_len, LY_VALUE_FORMAT format, void *prefix_data, const char *name, size_t name_len, |
| 1245 | const struct lysc_node **snode, struct lysc_ext_instance **ext) |
| 1246 | { |
| 1247 | LY_ERR r; |
| 1248 | LY_ARRAY_COUNT_TYPE u; |
| 1249 | struct lysc_ext_instance *nested_exts = NULL; |
| 1250 | lyplg_ext_data_snode_clb ext_snode_cb; |
| 1251 | |
| 1252 | /* check if there are any nested extension instances */ |
| 1253 | if (parent && parent->schema) { |
| 1254 | nested_exts = parent->schema->exts; |
| 1255 | } else if (sparent) { |
| 1256 | nested_exts = sparent->exts; |
| 1257 | } |
| 1258 | LY_ARRAY_FOR(nested_exts, u) { |
Michal Vasko | 305c6cb | 2022-04-27 10:33:04 +0200 | [diff] [blame] | 1259 | if (!nested_exts[u].def->plugin) { |
| 1260 | /* no plugin */ |
| 1261 | continue; |
| 1262 | } |
| 1263 | |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1264 | ext_snode_cb = nested_exts[u].def->plugin->snode; |
| 1265 | if (!ext_snode_cb) { |
| 1266 | /* not an extension with nested data */ |
| 1267 | continue; |
| 1268 | } |
| 1269 | |
| 1270 | /* try to get the schema node */ |
| 1271 | r = ext_snode_cb(&nested_exts[u], parent, sparent, prefix, prefix_len, format, prefix_data, name, name_len, snode); |
| 1272 | if (!r) { |
Michal Vasko | 66330fc | 2022-11-21 15:52:24 +0100 | [diff] [blame] | 1273 | if (ext) { |
| 1274 | /* data successfully created, remember the ext instance */ |
| 1275 | *ext = &nested_exts[u]; |
| 1276 | } |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 1277 | return LY_SUCCESS; |
| 1278 | } else if (r != LY_ENOT) { |
| 1279 | /* fatal error */ |
| 1280 | return r; |
| 1281 | } |
| 1282 | /* data was not from this module, continue */ |
| 1283 | } |
| 1284 | |
| 1285 | /* no extensions or none matched */ |
| 1286 | return LY_ENOT; |
| 1287 | } |
| 1288 | |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 1289 | void |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1290 | ly_free_prefix_data(LY_VALUE_FORMAT format, void *prefix_data) |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 1291 | { |
| 1292 | struct ly_set *ns_list; |
| 1293 | struct lysc_prefix *prefixes; |
| 1294 | uint32_t i; |
| 1295 | LY_ARRAY_COUNT_TYPE u; |
| 1296 | |
| 1297 | if (!prefix_data) { |
| 1298 | return; |
| 1299 | } |
| 1300 | |
| 1301 | switch (format) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1302 | case LY_VALUE_XML: |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1303 | case LY_VALUE_STR_NS: |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 1304 | ns_list = prefix_data; |
| 1305 | for (i = 0; i < ns_list->count; ++i) { |
| 1306 | free(((struct lyxml_ns *)ns_list->objs[i])->prefix); |
| 1307 | free(((struct lyxml_ns *)ns_list->objs[i])->uri); |
| 1308 | } |
| 1309 | ly_set_free(ns_list, free); |
| 1310 | break; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1311 | case LY_VALUE_SCHEMA_RESOLVED: |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 1312 | prefixes = prefix_data; |
| 1313 | LY_ARRAY_FOR(prefixes, u) { |
| 1314 | free(prefixes[u].prefix); |
| 1315 | } |
| 1316 | LY_ARRAY_FREE(prefixes); |
| 1317 | break; |
Radek Krejci | 224d4b4 | 2021-04-23 13:54:59 +0200 | [diff] [blame] | 1318 | case LY_VALUE_CANON: |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1319 | case LY_VALUE_SCHEMA: |
| 1320 | case LY_VALUE_JSON: |
Radek Krejci | f994364 | 2021-04-26 10:18:21 +0200 | [diff] [blame] | 1321 | case LY_VALUE_LYB: |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 1322 | break; |
| 1323 | } |
| 1324 | } |
| 1325 | |
| 1326 | LY_ERR |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1327 | ly_dup_prefix_data(const struct ly_ctx *ctx, LY_VALUE_FORMAT format, const void *prefix_data, |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 1328 | void **prefix_data_p) |
| 1329 | { |
| 1330 | LY_ERR ret = LY_SUCCESS; |
| 1331 | struct lyxml_ns *ns; |
| 1332 | struct lysc_prefix *prefixes = NULL, *orig_pref; |
| 1333 | struct ly_set *ns_list, *orig_ns; |
| 1334 | uint32_t i; |
| 1335 | LY_ARRAY_COUNT_TYPE u; |
| 1336 | |
| 1337 | assert(!*prefix_data_p); |
| 1338 | |
| 1339 | switch (format) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1340 | case LY_VALUE_SCHEMA: |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 1341 | *prefix_data_p = (void *)prefix_data; |
| 1342 | break; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1343 | case LY_VALUE_SCHEMA_RESOLVED: |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 1344 | /* copy all the value prefixes */ |
| 1345 | orig_pref = (struct lysc_prefix *)prefix_data; |
| 1346 | LY_ARRAY_CREATE_GOTO(ctx, prefixes, LY_ARRAY_COUNT(orig_pref), ret, cleanup); |
| 1347 | *prefix_data_p = prefixes; |
| 1348 | |
| 1349 | LY_ARRAY_FOR(orig_pref, u) { |
| 1350 | if (orig_pref[u].prefix) { |
| 1351 | prefixes[u].prefix = strdup(orig_pref[u].prefix); |
| 1352 | LY_CHECK_ERR_GOTO(!prefixes[u].prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 1353 | } |
| 1354 | prefixes[u].mod = orig_pref[u].mod; |
| 1355 | LY_ARRAY_INCREMENT(prefixes); |
| 1356 | } |
| 1357 | break; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1358 | case LY_VALUE_XML: |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1359 | case LY_VALUE_STR_NS: |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 1360 | /* copy all the namespaces */ |
| 1361 | LY_CHECK_GOTO(ret = ly_set_new(&ns_list), cleanup); |
| 1362 | *prefix_data_p = ns_list; |
| 1363 | |
| 1364 | orig_ns = (struct ly_set *)prefix_data; |
| 1365 | for (i = 0; i < orig_ns->count; ++i) { |
| 1366 | ns = calloc(1, sizeof *ns); |
| 1367 | LY_CHECK_ERR_GOTO(!ns, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 1368 | LY_CHECK_GOTO(ret = ly_set_add(ns_list, ns, 1, NULL), cleanup); |
| 1369 | |
| 1370 | if (((struct lyxml_ns *)orig_ns->objs[i])->prefix) { |
| 1371 | ns->prefix = strdup(((struct lyxml_ns *)orig_ns->objs[i])->prefix); |
| 1372 | LY_CHECK_ERR_GOTO(!ns->prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 1373 | } |
| 1374 | ns->uri = strdup(((struct lyxml_ns *)orig_ns->objs[i])->uri); |
| 1375 | LY_CHECK_ERR_GOTO(!ns->uri, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 1376 | } |
| 1377 | break; |
Radek Krejci | 224d4b4 | 2021-04-23 13:54:59 +0200 | [diff] [blame] | 1378 | case LY_VALUE_CANON: |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1379 | case LY_VALUE_JSON: |
Radek Krejci | f994364 | 2021-04-26 10:18:21 +0200 | [diff] [blame] | 1380 | case LY_VALUE_LYB: |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 1381 | assert(!prefix_data); |
| 1382 | *prefix_data_p = NULL; |
| 1383 | break; |
| 1384 | } |
| 1385 | |
| 1386 | cleanup: |
| 1387 | if (ret) { |
| 1388 | ly_free_prefix_data(format, *prefix_data_p); |
| 1389 | *prefix_data_p = NULL; |
| 1390 | } |
| 1391 | return ret; |
| 1392 | } |
| 1393 | |
| 1394 | LY_ERR |
Radek Krejci | f994364 | 2021-04-26 10:18:21 +0200 | [diff] [blame] | 1395 | ly_store_prefix_data(const struct ly_ctx *ctx, const void *value, size_t value_len, LY_VALUE_FORMAT format, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1396 | const void *prefix_data, LY_VALUE_FORMAT *format_p, void **prefix_data_p) |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 1397 | { |
| 1398 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 1399 | const struct lys_module *mod; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1400 | const struct lyxml_ns *ns; |
| 1401 | struct lyxml_ns *new_ns; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 1402 | struct ly_set *ns_list; |
| 1403 | struct lysc_prefix *prefixes = NULL, *val_pref; |
aPiecek | 83436bc | 2021-03-30 12:20:45 +0200 | [diff] [blame] | 1404 | const char *value_iter, *value_next, *value_end; |
| 1405 | uint32_t substr_len; |
| 1406 | ly_bool is_prefix; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 1407 | |
| 1408 | switch (format) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1409 | case LY_VALUE_SCHEMA: |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1410 | /* copy all referenced modules as prefix - module pairs */ |
| 1411 | if (!*prefix_data_p) { |
| 1412 | /* new prefix data */ |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 1413 | LY_ARRAY_CREATE_GOTO(ctx, prefixes, 0, ret, cleanup); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1414 | *format_p = LY_VALUE_SCHEMA_RESOLVED; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 1415 | *prefix_data_p = prefixes; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1416 | } else { |
| 1417 | /* reuse prefix data */ |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1418 | assert(*format_p == LY_VALUE_SCHEMA_RESOLVED); |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1419 | prefixes = *prefix_data_p; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 1420 | } |
| 1421 | |
Michal Vasko | c0f9c4c | 2022-05-06 12:12:17 +0200 | [diff] [blame] | 1422 | /* add current module for unprefixed values */ |
| 1423 | LY_ARRAY_NEW_GOTO(ctx, prefixes, val_pref, ret, cleanup); |
| 1424 | *prefix_data_p = prefixes; |
| 1425 | |
| 1426 | val_pref->prefix = NULL; |
| 1427 | val_pref->mod = ((const struct lysp_module *)prefix_data)->mod; |
| 1428 | |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 1429 | /* add all used prefixes */ |
Michal Vasko | 59e90fc | 2021-09-22 12:17:08 +0200 | [diff] [blame] | 1430 | value_end = (char *)value + value_len; |
aPiecek | 83436bc | 2021-03-30 12:20:45 +0200 | [diff] [blame] | 1431 | for (value_iter = value; value_iter; value_iter = value_next) { |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 1432 | LY_CHECK_GOTO(ret = ly_value_prefix_next(value_iter, value_end, &substr_len, &is_prefix, &value_next), cleanup); |
aPiecek | 83436bc | 2021-03-30 12:20:45 +0200 | [diff] [blame] | 1433 | if (is_prefix) { |
| 1434 | /* we have a possible prefix. Do we already have the prefix? */ |
| 1435 | mod = ly_resolve_prefix(ctx, value_iter, substr_len, *format_p, *prefix_data_p); |
| 1436 | if (!mod) { |
| 1437 | mod = ly_resolve_prefix(ctx, value_iter, substr_len, format, prefix_data); |
| 1438 | if (mod) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1439 | assert(*format_p == LY_VALUE_SCHEMA_RESOLVED); |
aPiecek | 83436bc | 2021-03-30 12:20:45 +0200 | [diff] [blame] | 1440 | /* store a new prefix - module pair */ |
| 1441 | LY_ARRAY_NEW_GOTO(ctx, prefixes, val_pref, ret, cleanup); |
| 1442 | *prefix_data_p = prefixes; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 1443 | |
aPiecek | 83436bc | 2021-03-30 12:20:45 +0200 | [diff] [blame] | 1444 | val_pref->prefix = strndup(value_iter, substr_len); |
| 1445 | LY_CHECK_ERR_GOTO(!val_pref->prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 1446 | val_pref->mod = mod; |
| 1447 | } /* else it is not even defined */ |
| 1448 | } /* else the prefix is already present */ |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1449 | } |
| 1450 | } |
| 1451 | break; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1452 | case LY_VALUE_XML: |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1453 | case LY_VALUE_STR_NS: |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1454 | /* copy all referenced namespaces as prefix - namespace pairs */ |
| 1455 | if (!*prefix_data_p) { |
| 1456 | /* new prefix data */ |
| 1457 | LY_CHECK_GOTO(ret = ly_set_new(&ns_list), cleanup); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1458 | *format_p = format; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1459 | *prefix_data_p = ns_list; |
| 1460 | } else { |
| 1461 | /* reuse prefix data */ |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1462 | assert(*format_p == format); |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1463 | ns_list = *prefix_data_p; |
| 1464 | } |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 1465 | |
Michal Vasko | 294e7f0 | 2022-02-28 13:59:00 +0100 | [diff] [blame] | 1466 | /* store default namespace */ |
| 1467 | ns = lyxml_ns_get(prefix_data, NULL, 0); |
| 1468 | if (ns) { |
| 1469 | new_ns = calloc(1, sizeof *new_ns); |
| 1470 | LY_CHECK_ERR_GOTO(!new_ns, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 1471 | LY_CHECK_GOTO(ret = ly_set_add(ns_list, new_ns, 1, NULL), cleanup); |
| 1472 | |
| 1473 | new_ns->prefix = NULL; |
| 1474 | new_ns->uri = strdup(ns->uri); |
| 1475 | LY_CHECK_ERR_GOTO(!new_ns->uri, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 1476 | } |
| 1477 | |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1478 | /* add all used prefixes */ |
Michal Vasko | 59e90fc | 2021-09-22 12:17:08 +0200 | [diff] [blame] | 1479 | value_end = (char *)value + value_len; |
aPiecek | 83436bc | 2021-03-30 12:20:45 +0200 | [diff] [blame] | 1480 | for (value_iter = value; value_iter; value_iter = value_next) { |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 1481 | LY_CHECK_GOTO(ret = ly_value_prefix_next(value_iter, value_end, &substr_len, &is_prefix, &value_next), cleanup); |
aPiecek | 83436bc | 2021-03-30 12:20:45 +0200 | [diff] [blame] | 1482 | if (is_prefix) { |
| 1483 | /* we have a possible prefix. Do we already have the prefix? */ |
| 1484 | ns = lyxml_ns_get(ns_list, value_iter, substr_len); |
| 1485 | if (!ns) { |
| 1486 | ns = lyxml_ns_get(prefix_data, value_iter, substr_len); |
| 1487 | if (ns) { |
| 1488 | /* store a new prefix - namespace pair */ |
| 1489 | new_ns = calloc(1, sizeof *new_ns); |
| 1490 | LY_CHECK_ERR_GOTO(!new_ns, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 1491 | 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] | 1492 | |
aPiecek | 83436bc | 2021-03-30 12:20:45 +0200 | [diff] [blame] | 1493 | new_ns->prefix = strndup(value_iter, substr_len); |
| 1494 | LY_CHECK_ERR_GOTO(!new_ns->prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 1495 | new_ns->uri = strdup(ns->uri); |
| 1496 | LY_CHECK_ERR_GOTO(!new_ns->uri, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 1497 | } /* else it is not even defined */ |
| 1498 | } /* else the prefix is already present */ |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 1499 | } |
| 1500 | } |
| 1501 | break; |
Radek Krejci | 224d4b4 | 2021-04-23 13:54:59 +0200 | [diff] [blame] | 1502 | case LY_VALUE_CANON: |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1503 | case LY_VALUE_SCHEMA_RESOLVED: |
| 1504 | case LY_VALUE_JSON: |
Radek Krejci | f994364 | 2021-04-26 10:18:21 +0200 | [diff] [blame] | 1505 | case LY_VALUE_LYB: |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1506 | if (!*prefix_data_p) { |
| 1507 | /* new prefix data - simply copy all the prefix data */ |
| 1508 | *format_p = format; |
| 1509 | LY_CHECK_GOTO(ret = ly_dup_prefix_data(ctx, format, prefix_data, prefix_data_p), cleanup); |
| 1510 | } /* 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] | 1511 | break; |
| 1512 | } |
| 1513 | |
| 1514 | cleanup: |
| 1515 | if (ret) { |
| 1516 | ly_free_prefix_data(*format_p, *prefix_data_p); |
| 1517 | *prefix_data_p = NULL; |
| 1518 | } |
| 1519 | return ret; |
| 1520 | } |
Michal Vasko | 7ed1fcb | 2020-12-03 14:15:22 +0100 | [diff] [blame] | 1521 | |
| 1522 | const char * |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1523 | ly_format2str(LY_VALUE_FORMAT format) |
Michal Vasko | 7ed1fcb | 2020-12-03 14:15:22 +0100 | [diff] [blame] | 1524 | { |
| 1525 | switch (format) { |
Radek Krejci | 224d4b4 | 2021-04-23 13:54:59 +0200 | [diff] [blame] | 1526 | case LY_VALUE_CANON: |
| 1527 | return "canonical"; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1528 | case LY_VALUE_SCHEMA: |
Michal Vasko | 7ed1fcb | 2020-12-03 14:15:22 +0100 | [diff] [blame] | 1529 | return "schema imports"; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1530 | case LY_VALUE_SCHEMA_RESOLVED: |
Michal Vasko | 7ed1fcb | 2020-12-03 14:15:22 +0100 | [diff] [blame] | 1531 | return "schema stored mapping"; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1532 | case LY_VALUE_XML: |
Michal Vasko | 7ed1fcb | 2020-12-03 14:15:22 +0100 | [diff] [blame] | 1533 | return "XML prefixes"; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1534 | case LY_VALUE_JSON: |
Michal Vasko | 7ed1fcb | 2020-12-03 14:15:22 +0100 | [diff] [blame] | 1535 | return "JSON module names"; |
Radek Krejci | f994364 | 2021-04-26 10:18:21 +0200 | [diff] [blame] | 1536 | case LY_VALUE_LYB: |
| 1537 | return "LYB prefixes"; |
Michal Vasko | 7ed1fcb | 2020-12-03 14:15:22 +0100 | [diff] [blame] | 1538 | default: |
| 1539 | break; |
| 1540 | } |
| 1541 | |
| 1542 | return NULL; |
| 1543 | } |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 1544 | |
Michal Vasko | f17bb2a | 2023-02-10 11:34:55 +0100 | [diff] [blame] | 1545 | LIBYANG_API_DEF int |
| 1546 | ly_time_tz_offset(void) |
| 1547 | { |
Michal Vasko | 9b56033 | 2023-04-24 15:43:56 +0200 | [diff] [blame^] | 1548 | return ly_time_tz_offset_at(time(NULL)); |
| 1549 | } |
| 1550 | |
| 1551 | LIBYANG_API_DEF int |
| 1552 | ly_time_tz_offset_at(time_t time) |
| 1553 | { |
Michal Vasko | f17bb2a | 2023-02-10 11:34:55 +0100 | [diff] [blame] | 1554 | struct tm tm_local, tm_utc; |
| 1555 | int result = 0; |
| 1556 | |
| 1557 | /* init timezone */ |
| 1558 | tzset(); |
| 1559 | |
| 1560 | /* get local and UTC time */ |
Michal Vasko | 9b56033 | 2023-04-24 15:43:56 +0200 | [diff] [blame^] | 1561 | localtime_r(&time, &tm_local); |
| 1562 | gmtime_r(&time, &tm_utc); |
Michal Vasko | e50b98d | 2023-03-28 11:39:01 +0200 | [diff] [blame] | 1563 | |
Michal Vasko | 9b56033 | 2023-04-24 15:43:56 +0200 | [diff] [blame^] | 1564 | /* account for year/month/day change by adding/subtracting from the hours, the change cannot be more than 1 day */ |
| 1565 | if (tm_local.tm_year < tm_utc.tm_year) { |
| 1566 | tm_utc.tm_hour += 24; |
| 1567 | } else if (tm_local.tm_year > tm_utc.tm_year) { |
| 1568 | tm_local.tm_hour += 24; |
| 1569 | } else if (tm_local.tm_mon < tm_utc.tm_mon) { |
| 1570 | tm_utc.tm_hour += 24; |
| 1571 | } else if (tm_local.tm_mon > tm_utc.tm_mon) { |
| 1572 | tm_local.tm_hour += 24; |
| 1573 | } else if (tm_local.tm_mday < tm_utc.tm_mday) { |
| 1574 | tm_utc.tm_hour += 24; |
| 1575 | } else if (tm_local.tm_mday > tm_utc.tm_mday) { |
| 1576 | tm_local.tm_hour += 24; |
Michal Vasko | e50b98d | 2023-03-28 11:39:01 +0200 | [diff] [blame] | 1577 | } |
Michal Vasko | f17bb2a | 2023-02-10 11:34:55 +0100 | [diff] [blame] | 1578 | |
| 1579 | /* hours shift in seconds */ |
| 1580 | result += (tm_local.tm_hour - tm_utc.tm_hour) * 3600; |
| 1581 | |
| 1582 | /* minutes shift in seconds */ |
| 1583 | result += (tm_local.tm_min - tm_utc.tm_min) * 60; |
| 1584 | |
| 1585 | /* seconds shift */ |
| 1586 | result += tm_local.tm_sec - tm_utc.tm_sec; |
| 1587 | |
| 1588 | return result; |
| 1589 | } |
| 1590 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1591 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 1592 | ly_time_str2time(const char *value, time_t *time, char **fractions_s) |
| 1593 | { |
| 1594 | struct tm tm = {0}; |
| 1595 | uint32_t i, frac_len; |
| 1596 | const char *frac; |
| 1597 | int64_t shift, shift_m; |
| 1598 | time_t t; |
| 1599 | |
| 1600 | LY_CHECK_ARG_RET(NULL, value, time, LY_EINVAL); |
| 1601 | |
| 1602 | tm.tm_year = atoi(&value[0]) - 1900; |
| 1603 | tm.tm_mon = atoi(&value[5]) - 1; |
| 1604 | tm.tm_mday = atoi(&value[8]); |
| 1605 | tm.tm_hour = atoi(&value[11]); |
| 1606 | tm.tm_min = atoi(&value[14]); |
| 1607 | tm.tm_sec = atoi(&value[17]); |
| 1608 | |
| 1609 | t = timegm(&tm); |
| 1610 | i = 19; |
| 1611 | |
| 1612 | /* fractions of a second */ |
| 1613 | if (value[i] == '.') { |
| 1614 | ++i; |
| 1615 | frac = &value[i]; |
| 1616 | for (frac_len = 0; isdigit(frac[frac_len]); ++frac_len) {} |
| 1617 | |
| 1618 | i += frac_len; |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 1619 | } else { |
| 1620 | frac = NULL; |
| 1621 | } |
| 1622 | |
| 1623 | /* apply offset */ |
| 1624 | if ((value[i] == 'Z') || (value[i] == 'z')) { |
| 1625 | /* zero shift */ |
| 1626 | shift = 0; |
| 1627 | } else { |
| 1628 | shift = strtol(&value[i], NULL, 10); |
| 1629 | shift = shift * 60 * 60; /* convert from hours to seconds */ |
| 1630 | shift_m = strtol(&value[i + 4], NULL, 10) * 60; /* includes conversion from minutes to seconds */ |
| 1631 | /* correct sign */ |
| 1632 | if (shift < 0) { |
| 1633 | shift_m *= -1; |
| 1634 | } |
| 1635 | /* connect hours and minutes of the shift */ |
| 1636 | shift = shift + shift_m; |
| 1637 | } |
| 1638 | |
| 1639 | /* we have to shift to the opposite way to correct the time */ |
| 1640 | t -= shift; |
| 1641 | |
| 1642 | *time = t; |
| 1643 | if (fractions_s) { |
| 1644 | if (frac) { |
| 1645 | *fractions_s = strndup(frac, frac_len); |
| 1646 | LY_CHECK_RET(!*fractions_s, LY_EMEM); |
| 1647 | } else { |
| 1648 | *fractions_s = NULL; |
| 1649 | } |
| 1650 | } |
| 1651 | return LY_SUCCESS; |
| 1652 | } |
| 1653 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1654 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 1655 | ly_time_time2str(time_t time, const char *fractions_s, char **str) |
| 1656 | { |
| 1657 | struct tm tm; |
Michal Vasko | f17bb2a | 2023-02-10 11:34:55 +0100 | [diff] [blame] | 1658 | char zoneshift[12]; |
| 1659 | int zonediff_s, zonediff_h, zonediff_m; |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 1660 | |
| 1661 | LY_CHECK_ARG_RET(NULL, str, LY_EINVAL); |
| 1662 | |
Michal Vasko | f17bb2a | 2023-02-10 11:34:55 +0100 | [diff] [blame] | 1663 | /* init timezone */ |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 1664 | tzset(); |
| 1665 | |
| 1666 | /* convert */ |
| 1667 | if (!localtime_r(&time, &tm)) { |
| 1668 | return LY_ESYS; |
| 1669 | } |
| 1670 | |
Michal Vasko | e50b98d | 2023-03-28 11:39:01 +0200 | [diff] [blame] | 1671 | /* get timezone offset (do not use tm_gmtoff to avoid portability problems) */ |
Michal Vasko | 9b56033 | 2023-04-24 15:43:56 +0200 | [diff] [blame^] | 1672 | zonediff_s = ly_time_tz_offset_at(time); |
Michal Vasko | f17bb2a | 2023-02-10 11:34:55 +0100 | [diff] [blame] | 1673 | zonediff_h = zonediff_s / 60 / 60; |
| 1674 | zonediff_m = zonediff_s / 60 % 60; |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 1675 | sprintf(zoneshift, "%+03d:%02d", zonediff_h, zonediff_m); |
| 1676 | |
| 1677 | /* print */ |
| 1678 | if (asprintf(str, "%04d-%02d-%02dT%02d:%02d:%02d%s%s%s", |
| 1679 | tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, |
| 1680 | fractions_s ? "." : "", fractions_s ? fractions_s : "", zoneshift) == -1) { |
| 1681 | return LY_EMEM; |
| 1682 | } |
| 1683 | |
| 1684 | return LY_SUCCESS; |
| 1685 | } |
| 1686 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1687 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 1688 | ly_time_str2ts(const char *value, struct timespec *ts) |
| 1689 | { |
| 1690 | LY_ERR rc; |
Michal Vasko | 7297506 | 2021-08-25 08:13:04 +0200 | [diff] [blame] | 1691 | char *fractions_s, frac_buf[10]; |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 1692 | int frac_len; |
| 1693 | |
| 1694 | LY_CHECK_ARG_RET(NULL, value, ts, LY_EINVAL); |
| 1695 | |
| 1696 | rc = ly_time_str2time(value, &ts->tv_sec, &fractions_s); |
| 1697 | LY_CHECK_RET(rc); |
| 1698 | |
| 1699 | /* convert fractions of a second to nanoseconds */ |
| 1700 | if (fractions_s) { |
Michal Vasko | 7297506 | 2021-08-25 08:13:04 +0200 | [diff] [blame] | 1701 | /* init frac_buf with zeroes */ |
| 1702 | memset(frac_buf, '0', 9); |
| 1703 | frac_buf[9] = '\0'; |
| 1704 | |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 1705 | frac_len = strlen(fractions_s); |
| 1706 | memcpy(frac_buf, fractions_s, frac_len > 9 ? 9 : frac_len); |
| 1707 | ts->tv_nsec = atol(frac_buf); |
| 1708 | free(fractions_s); |
| 1709 | } else { |
| 1710 | ts->tv_nsec = 0; |
| 1711 | } |
| 1712 | |
| 1713 | return LY_SUCCESS; |
| 1714 | } |
| 1715 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1716 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 1717 | ly_time_ts2str(const struct timespec *ts, char **str) |
| 1718 | { |
| 1719 | char frac_buf[10]; |
| 1720 | |
Jan Kundrát | bd15700 | 2021-08-30 14:02:22 +0200 | [diff] [blame] | 1721 | LY_CHECK_ARG_RET(NULL, ts, str, ((ts->tv_nsec <= 999999999) && (ts->tv_nsec >= 0)), LY_EINVAL); |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 1722 | |
| 1723 | /* convert nanoseconds to fractions of a second */ |
| 1724 | if (ts->tv_nsec) { |
| 1725 | sprintf(frac_buf, "%09ld", ts->tv_nsec); |
| 1726 | } |
| 1727 | |
| 1728 | return ly_time_time2str(ts->tv_sec, ts->tv_nsec ? frac_buf : NULL, str); |
| 1729 | } |