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