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