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