Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file tree_data_helpers.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief Parsing and validation helper functions for data trees |
| 5 | * |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 6 | * Copyright (c) 2015 - 2022 CESNET, z.s.p.o. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 14 | |
| 15 | #define _GNU_SOURCE /* asprintf, strdup */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 16 | |
| 17 | #include <assert.h> |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 18 | #include <ctype.h> |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 19 | #include <stdint.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 20 | #include <stdlib.h> |
Radek Krejci | ad97c5f | 2020-06-30 09:19:28 +0200 | [diff] [blame] | 21 | #include <string.h> |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 22 | #include <time.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 23 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 24 | #include "common.h" |
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" |
Radek Krejci | ad97c5f | 2020-06-30 09:19:28 +0200 | [diff] [blame] | 30 | #include "lyb.h" |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 31 | #include "parser_data.h" |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 32 | #include "plugins_exts.h" |
Michal Vasko | a820c31 | 2021-02-05 16:33:00 +0100 | [diff] [blame] | 33 | #include "printer_data.h" |
Radek Krejci | ad97c5f | 2020-06-30 09:19:28 +0200 | [diff] [blame] | 34 | #include "set.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 35 | #include "tree.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 36 | #include "tree_data.h" |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 37 | #include "tree_data_internal.h" |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 38 | #include "tree_edit.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 39 | #include "tree_schema.h" |
Radek Krejci | 0aa1f70 | 2021-04-01 16:16:19 +0200 | [diff] [blame] | 40 | #include "tree_schema_internal.h" |
Radek Krejci | 4f2e3e5 | 2021-03-30 14:20:28 +0200 | [diff] [blame] | 41 | #include "validation.h" |
Radek Krejci | 7711410 | 2021-03-10 15:21:57 +0100 | [diff] [blame] | 42 | #include "xml.h" |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 43 | #include "xpath.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 44 | |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 45 | /** |
| 46 | * @brief Find an entry in duplicate instance cache for an instance. Create it if it does not exist. |
| 47 | * |
| 48 | * @param[in] first_inst Instance of the cache entry. |
| 49 | * @param[in,out] dup_inst_cache Duplicate instance cache. |
| 50 | * @return Instance cache entry. |
| 51 | */ |
| 52 | static struct lyd_dup_inst * |
| 53 | lyd_dup_inst_get(const struct lyd_node *first_inst, struct lyd_dup_inst **dup_inst_cache) |
| 54 | { |
| 55 | struct lyd_dup_inst *item; |
| 56 | LY_ARRAY_COUNT_TYPE u; |
| 57 | |
| 58 | LY_ARRAY_FOR(*dup_inst_cache, u) { |
| 59 | if ((*dup_inst_cache)[u].inst_set->dnodes[0] == first_inst) { |
| 60 | return &(*dup_inst_cache)[u]; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /* it was not added yet, add it now */ |
| 65 | LY_ARRAY_NEW_RET(LYD_CTX(first_inst), *dup_inst_cache, item, NULL); |
| 66 | |
| 67 | return item; |
| 68 | } |
| 69 | |
| 70 | LY_ERR |
| 71 | lyd_dup_inst_next(struct lyd_node **inst, const struct lyd_node *siblings, struct lyd_dup_inst **dup_inst_cache) |
| 72 | { |
| 73 | struct lyd_dup_inst *dup_inst; |
| 74 | |
| 75 | if (!*inst || !lysc_is_dup_inst_list((*inst)->schema)) { |
| 76 | /* no match or not dup-inst list, inst is unchanged */ |
| 77 | return LY_SUCCESS; |
| 78 | } |
| 79 | |
| 80 | /* there can be more exact same instances and we must make sure we do not match a single node more times */ |
| 81 | dup_inst = lyd_dup_inst_get(*inst, dup_inst_cache); |
| 82 | LY_CHECK_ERR_RET(!dup_inst, LOGMEM(LYD_CTX(siblings)), LY_EMEM); |
| 83 | |
| 84 | if (!dup_inst->used) { |
| 85 | /* we did not cache these instances yet, do so */ |
| 86 | lyd_find_sibling_dup_inst_set(siblings, *inst, &dup_inst->inst_set); |
| 87 | assert(dup_inst->inst_set->count && (dup_inst->inst_set->dnodes[0] == *inst)); |
| 88 | } |
| 89 | |
| 90 | if (dup_inst->used == dup_inst->inst_set->count) { |
| 91 | /* we have used all the instances */ |
| 92 | *inst = NULL; |
| 93 | } else { |
| 94 | assert(dup_inst->used < dup_inst->inst_set->count); |
| 95 | |
| 96 | /* use another instance */ |
| 97 | *inst = dup_inst->inst_set->dnodes[dup_inst->used]; |
| 98 | ++dup_inst->used; |
| 99 | } |
| 100 | |
| 101 | return LY_SUCCESS; |
| 102 | } |
| 103 | |
| 104 | void |
| 105 | lyd_dup_inst_free(struct lyd_dup_inst *dup_inst) |
| 106 | { |
| 107 | LY_ARRAY_COUNT_TYPE u; |
| 108 | |
| 109 | LY_ARRAY_FOR(dup_inst, u) { |
| 110 | ly_set_free(dup_inst[u].inst_set, NULL); |
| 111 | } |
| 112 | LY_ARRAY_FREE(dup_inst); |
| 113 | } |
| 114 | |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 115 | struct lyd_node * |
| 116 | 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] | 117 | const struct lysc_node *parent, const struct lysc_module *module) |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 118 | { |
| 119 | const struct lysc_node *siter = NULL; |
| 120 | struct lyd_node *match = NULL; |
| 121 | |
| 122 | assert(parent || module); |
| 123 | assert(!last || (slast && *slast)); |
| 124 | |
| 125 | if (slast) { |
| 126 | siter = *slast; |
| 127 | } |
| 128 | |
| 129 | if (last && last->next && (last->next->schema == siter)) { |
| 130 | /* return next data instance */ |
| 131 | return last->next; |
| 132 | } |
| 133 | |
| 134 | /* find next schema node data instance */ |
| 135 | while ((siter = lys_getnext(siter, parent, module, 0))) { |
| 136 | if (!lyd_find_sibling_val(sibling, siter, NULL, 0, &match)) { |
| 137 | break; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | if (slast) { |
| 142 | *slast = siter; |
| 143 | } |
| 144 | return match; |
| 145 | } |
| 146 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 147 | struct lyd_node ** |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 148 | lyd_node_child_p(struct lyd_node *node) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 149 | { |
| 150 | assert(node); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 151 | |
| 152 | if (!node->schema) { |
| 153 | return &((struct lyd_node_opaq *)node)->child; |
| 154 | } else { |
| 155 | switch (node->schema->nodetype) { |
| 156 | case LYS_CONTAINER: |
| 157 | case LYS_LIST: |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 158 | case LYS_RPC: |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 159 | case LYS_ACTION: |
| 160 | case LYS_NOTIF: |
| 161 | return &((struct lyd_node_inner *)node)->child; |
| 162 | default: |
| 163 | return NULL; |
| 164 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 168 | LIBYANG_API_DEF LY_ERR |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 169 | lyxp_vars_set(struct lyxp_var **vars, const char *name, const char *value) |
| 170 | { |
| 171 | LY_ERR ret = LY_SUCCESS; |
| 172 | char *var_name = NULL, *var_value = NULL; |
| 173 | struct lyxp_var *item; |
| 174 | |
| 175 | if (!vars || !name || !value) { |
| 176 | return LY_EINVAL; |
| 177 | } |
| 178 | |
| 179 | /* If variable is already defined then change its value. */ |
| 180 | if (*vars && !lyxp_vars_find(*vars, name, 0, &item)) { |
| 181 | var_value = strdup(value); |
| 182 | LY_CHECK_RET(!var_value, LY_EMEM); |
| 183 | |
| 184 | /* Set new value. */ |
| 185 | free(item->value); |
| 186 | item->value = var_value; |
| 187 | } else { |
| 188 | var_name = strdup(name); |
| 189 | var_value = strdup(value); |
| 190 | LY_CHECK_ERR_GOTO(!var_name || !var_value, ret = LY_EMEM, error); |
| 191 | |
| 192 | /* Add new variable. */ |
| 193 | LY_ARRAY_NEW_GOTO(NULL, *vars, item, ret, error); |
| 194 | item->name = var_name; |
| 195 | item->value = var_value; |
| 196 | } |
| 197 | |
| 198 | return LY_SUCCESS; |
| 199 | |
| 200 | error: |
| 201 | free(var_name); |
| 202 | free(var_value); |
| 203 | return ret; |
| 204 | } |
| 205 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 206 | LIBYANG_API_DEF void |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 207 | lyxp_vars_free(struct lyxp_var *vars) |
| 208 | { |
| 209 | LY_ARRAY_COUNT_TYPE u; |
| 210 | |
| 211 | if (!vars) { |
| 212 | return; |
| 213 | } |
| 214 | |
| 215 | LY_ARRAY_FOR(vars, u) { |
| 216 | free(vars[u].name); |
| 217 | free(vars[u].value); |
| 218 | } |
| 219 | |
| 220 | LY_ARRAY_FREE(vars); |
| 221 | } |
| 222 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 223 | LIBYANG_API_DEF struct lyd_node * |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 224 | lyd_child_no_keys(const struct lyd_node *node) |
| 225 | { |
| 226 | struct lyd_node **children; |
| 227 | |
| 228 | if (!node) { |
| 229 | return NULL; |
| 230 | } |
| 231 | |
| 232 | if (!node->schema) { |
| 233 | /* opaq node */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 234 | return ((struct lyd_node_opaq *)node)->child; |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 235 | } |
| 236 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 237 | children = lyd_node_child_p((struct lyd_node *)node); |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 238 | if (children) { |
| 239 | struct lyd_node *child = *children; |
| 240 | while (child && child->schema && (child->schema->flags & LYS_KEY)) { |
| 241 | child = child->next; |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 242 | } |
| 243 | return child; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 244 | } else { |
| 245 | return NULL; |
| 246 | } |
| 247 | } |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 248 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 249 | LIBYANG_API_DEF const struct lys_module * |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 250 | lyd_owner_module(const struct lyd_node *node) |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 251 | { |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 252 | const struct lyd_node_opaq *opaq; |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 253 | |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 254 | if (!node) { |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 255 | return NULL; |
| 256 | } |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 257 | |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 258 | if (!node->schema) { |
| 259 | opaq = (struct lyd_node_opaq *)node; |
| 260 | switch (opaq->format) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 261 | case LY_VALUE_XML: |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 262 | return ly_ctx_get_module_implemented_ns(LYD_CTX(node), opaq->name.module_ns); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 263 | case LY_VALUE_JSON: |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 264 | return ly_ctx_get_module_implemented(LYD_CTX(node), opaq->name.module_name); |
| 265 | default: |
| 266 | return NULL; |
| 267 | } |
| 268 | } |
| 269 | |
Michal Vasko | ef53c81 | 2021-10-13 10:21:03 +0200 | [diff] [blame] | 270 | return lysc_owner_module(node->schema); |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 271 | } |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 272 | |
Michal Vasko | 598063b | 2021-07-19 11:39:05 +0200 | [diff] [blame] | 273 | void |
| 274 | lyd_first_module_sibling(struct lyd_node **node, const struct lys_module *mod) |
| 275 | { |
| 276 | int cmp; |
| 277 | struct lyd_node *first; |
| 278 | |
| 279 | assert(node && mod); |
| 280 | |
| 281 | if (!*node) { |
| 282 | return; |
| 283 | } |
| 284 | |
| 285 | first = *node; |
| 286 | cmp = strcmp(lyd_owner_module(first)->name, mod->name); |
| 287 | if (cmp > 0) { |
| 288 | /* there may be some preceding data */ |
| 289 | while (first->prev->next) { |
| 290 | first = first->prev; |
| 291 | if (lyd_owner_module(first) == mod) { |
| 292 | cmp = 0; |
| 293 | break; |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | if (cmp == 0) { |
| 299 | /* there may be some preceding data belonging to this module */ |
| 300 | while (first->prev->next) { |
| 301 | if (lyd_owner_module(first->prev) != mod) { |
| 302 | break; |
| 303 | } |
| 304 | first = first->prev; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | if (cmp < 0) { |
| 309 | /* there may be some following data */ |
| 310 | LY_LIST_FOR(first, first) { |
| 311 | if (lyd_owner_module(first) == mod) { |
| 312 | cmp = 0; |
| 313 | break; |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | if (cmp == 0) { |
| 319 | /* we have found the first module data node */ |
| 320 | *node = first; |
| 321 | } |
| 322 | } |
| 323 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 324 | const struct lys_module * |
Michal Vasko | 26e8001 | 2020-07-08 10:55:46 +0200 | [diff] [blame] | 325 | 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] | 326 | struct lyd_node **first) |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 327 | { |
| 328 | struct lyd_node *iter; |
| 329 | const struct lys_module *mod; |
| 330 | |
| 331 | /* get the next module */ |
Michal Vasko | 26e8001 | 2020-07-08 10:55:46 +0200 | [diff] [blame] | 332 | if (module) { |
| 333 | if (*i) { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 334 | mod = NULL; |
Michal Vasko | 26e8001 | 2020-07-08 10:55:46 +0200 | [diff] [blame] | 335 | } else { |
| 336 | mod = module; |
| 337 | ++(*i); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 338 | } |
| 339 | } else { |
| 340 | do { |
| 341 | mod = ly_ctx_get_module_iter(ctx, i); |
| 342 | } while (mod && !mod->implemented); |
| 343 | } |
| 344 | |
| 345 | /* find its data */ |
| 346 | *first = NULL; |
| 347 | if (mod) { |
| 348 | LY_LIST_FOR(tree, iter) { |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 349 | if (lyd_owner_module(iter) == mod) { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 350 | *first = iter; |
| 351 | break; |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | return mod; |
| 357 | } |
| 358 | |
| 359 | const struct lys_module * |
| 360 | lyd_data_next_module(struct lyd_node **next, struct lyd_node **first) |
| 361 | { |
| 362 | const struct lys_module *mod; |
| 363 | |
| 364 | if (!*next) { |
| 365 | /* all data traversed */ |
| 366 | *first = NULL; |
| 367 | return NULL; |
| 368 | } |
| 369 | |
| 370 | *first = *next; |
| 371 | |
| 372 | /* prepare next */ |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 373 | mod = lyd_owner_module(*next); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 374 | LY_LIST_FOR(*next, *next) { |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 375 | if (lyd_owner_module(*next) != mod) { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 376 | break; |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | return mod; |
| 381 | } |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 382 | |
| 383 | LY_ERR |
| 384 | lyd_parse_check_keys(struct lyd_node *node) |
| 385 | { |
| 386 | const struct lysc_node *skey = NULL; |
| 387 | const struct lyd_node *key; |
| 388 | |
| 389 | assert(node->schema->nodetype == LYS_LIST); |
| 390 | |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 391 | key = lyd_child(node); |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 392 | while ((skey = lys_getnext(skey, node->schema, NULL, 0)) && (skey->flags & LYS_KEY)) { |
| 393 | if (!key || (key->schema != skey)) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 394 | LOGVAL(LYD_CTX(node), LY_VCODE_NOKEY, skey->name); |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 395 | return LY_EVALID; |
| 396 | } |
| 397 | |
| 398 | key = key->next; |
| 399 | } |
| 400 | |
| 401 | return LY_SUCCESS; |
| 402 | } |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 403 | |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 404 | LY_ERR |
| 405 | lyd_parse_set_data_flags(struct lyd_node *node, struct lyd_meta **meta, struct lyd_ctx *lydctx, |
| 406 | struct lysc_ext_instance *ext) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 407 | { |
| 408 | struct lyd_meta *meta2, *prev_meta = NULL; |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 409 | struct lyd_ctx_ext_val *ext_val; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 410 | |
Michal Vasko | a5705e5 | 2020-12-09 18:15:14 +0100 | [diff] [blame] | 411 | if (lysc_has_when(node->schema)) { |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 412 | if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 413 | /* remember we need to evaluate this node's when */ |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 414 | LY_CHECK_RET(ly_set_add(&lydctx->node_when, node, 1, NULL)); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 418 | LY_LIST_FOR(*meta, meta2) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 419 | if (!strcmp(meta2->name, "default") && !strcmp(meta2->annotation->module->name, "ietf-netconf-with-defaults") && |
| 420 | meta2->value.boolean) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 421 | /* node is default according to the metadata */ |
| 422 | node->flags |= LYD_DEFAULT; |
| 423 | |
| 424 | /* delete the metadata */ |
| 425 | if (prev_meta) { |
| 426 | prev_meta->next = meta2->next; |
| 427 | } else { |
| 428 | *meta = (*meta)->next; |
| 429 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 430 | lyd_free_meta_single(meta2); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 431 | break; |
| 432 | } |
| 433 | |
| 434 | prev_meta = meta2; |
| 435 | } |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 436 | |
| 437 | if (ext) { |
| 438 | /* parsed for an extension */ |
| 439 | node->flags |= LYD_EXT; |
| 440 | |
| 441 | if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) { |
| 442 | /* rememeber for validation */ |
| 443 | ext_val = malloc(sizeof *ext_val); |
| 444 | LY_CHECK_ERR_RET(!ext_val, LOGMEM(LYD_CTX(node)), LY_EMEM); |
| 445 | ext_val->ext = ext; |
| 446 | ext_val->sibling = node; |
| 447 | LY_CHECK_RET(ly_set_add(&lydctx->ext_val, ext_val, 1, NULL)); |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | return LY_SUCCESS; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 452 | } |
| 453 | |
Michal Vasko | bfff6ac | 2022-02-23 16:22:53 +0100 | [diff] [blame] | 454 | /** |
| 455 | * @brief Check list node parsed into an opaque node for the reason. |
| 456 | * |
| 457 | * @param[in] node Opaque node. |
| 458 | * @param[in] snode Schema node of @p opaq. |
| 459 | * @return LY_SUCCESS if the node is valid; |
| 460 | * @return LY_ERR on error. |
| 461 | */ |
| 462 | static LY_ERR |
| 463 | lyd_parse_opaq_list_error(const struct lyd_node *node, const struct lysc_node *snode) |
| 464 | { |
| 465 | LY_ERR ret = LY_SUCCESS; |
| 466 | struct ly_set key_set = {0}; |
| 467 | const struct lysc_node *key = NULL; |
| 468 | const struct lyd_node *child; |
| 469 | const struct lyd_node_opaq *opaq_k; |
| 470 | uint32_t i; |
| 471 | |
| 472 | assert(!node->schema); |
| 473 | |
| 474 | /* get all keys into a set */ |
| 475 | while ((key = lys_getnext(key, snode, NULL, 0)) && (snode->flags & LYS_KEY)) { |
| 476 | LY_CHECK_GOTO(ret = ly_set_add(&key_set, (void *)snode, 1, NULL), cleanup); |
| 477 | } |
| 478 | |
| 479 | LY_LIST_FOR(lyd_child(node), child) { |
| 480 | if (child->schema) { |
| 481 | LOGERR(LYD_CTX(node), LY_EINVAL, "Unexpected node %s \"%s\".", lys_nodetype2str(child->schema->nodetype), |
| 482 | LYD_NAME(child)); |
| 483 | ret = LY_EINVAL; |
| 484 | goto cleanup; |
| 485 | } |
| 486 | |
| 487 | opaq_k = (struct lyd_node_opaq *)child; |
| 488 | |
| 489 | /* find the key schema node */ |
| 490 | for (i = 0; i < key_set.count; ++i) { |
| 491 | key = key_set.snodes[i]; |
| 492 | if (!strcmp(key->name, opaq_k->name.name)) { |
| 493 | break; |
| 494 | } |
| 495 | } |
| 496 | if (i == key_set.count) { |
| 497 | /* some other node, skip */ |
| 498 | continue; |
| 499 | } |
| 500 | |
| 501 | /* key found */ |
| 502 | ly_set_rm_index(&key_set, i, NULL); |
| 503 | |
| 504 | /* check value */ |
| 505 | ret = lys_value_validate(LYD_CTX(node), key, opaq_k->value, strlen(opaq_k->value), opaq_k->format, |
| 506 | opaq_k->val_prefix_data); |
| 507 | LY_CHECK_GOTO(ret, cleanup); |
| 508 | } |
| 509 | |
| 510 | if (key_set.count) { |
| 511 | /* missing keys */ |
| 512 | LOGVAL(LYD_CTX(node), LY_VCODE_NOKEY, key_set.snodes[0]->name); |
| 513 | ret = LY_EVALID; |
| 514 | goto cleanup; |
| 515 | } |
| 516 | |
| 517 | cleanup: |
| 518 | ly_set_erase(&key_set, NULL); |
| 519 | return ret; |
| 520 | } |
| 521 | |
| 522 | LIBYANG_API_DEF LY_ERR |
| 523 | lyd_parse_opaq_error(const struct lyd_node *node) |
| 524 | { |
| 525 | const struct ly_ctx *ctx; |
| 526 | const struct lyd_node_opaq *opaq; |
| 527 | const struct lyd_node *parent; |
| 528 | const struct lys_module *mod; |
| 529 | const struct lysc_node *snode; |
| 530 | |
| 531 | LY_CHECK_ARG_RET(LYD_CTX(node), node, !node->schema, !lyd_parent(node) || lyd_parent(node)->schema, LY_EINVAL); |
| 532 | |
| 533 | ctx = LYD_CTX(node); |
| 534 | opaq = (struct lyd_node_opaq *)node; |
| 535 | parent = lyd_parent(node); |
| 536 | |
| 537 | /* is always filled by parsers */ |
| 538 | LY_CHECK_ARG_RET(ctx, opaq->name.module_ns, LY_EINVAL); |
| 539 | |
| 540 | /* module */ |
| 541 | switch (opaq->format) { |
| 542 | case LY_VALUE_XML: |
| 543 | if (!parent || strcmp(opaq->name.module_ns, parent->schema->module->ns)) { |
| 544 | mod = ly_ctx_get_module_implemented_ns(ctx, opaq->name.module_ns); |
| 545 | if (!mod) { |
| 546 | LOGVAL(ctx, LYVE_REFERENCE, "No (implemented) module with namespace \"%s\" in the context.", |
| 547 | opaq->name.module_ns); |
| 548 | return LY_EVALID; |
| 549 | } |
| 550 | } else { |
| 551 | /* inherit */ |
| 552 | mod = parent->schema->module; |
| 553 | } |
| 554 | break; |
| 555 | case LY_VALUE_JSON: |
| 556 | case LY_VALUE_LYB: |
| 557 | if (!parent || strcmp(opaq->name.module_name, parent->schema->module->name)) { |
| 558 | mod = ly_ctx_get_module_implemented(ctx, opaq->name.module_name); |
| 559 | if (!mod) { |
| 560 | LOGVAL(ctx, LYVE_REFERENCE, "No (implemented) module named \"%s\" in the context.", opaq->name.module_name); |
| 561 | return LY_EVALID; |
| 562 | } |
| 563 | } else { |
| 564 | /* inherit */ |
| 565 | mod = parent->schema->module; |
| 566 | } |
| 567 | break; |
| 568 | default: |
| 569 | LOGERR(ctx, LY_EINVAL, "Unsupported value format."); |
| 570 | return LY_EINVAL; |
| 571 | } |
| 572 | |
| 573 | /* schema */ |
| 574 | snode = lys_find_child(parent ? parent->schema : NULL, mod, opaq->name.name, 0, 0, 0); |
| 575 | if (!snode) { |
| 576 | if (parent) { |
| 577 | LOGVAL(ctx, LYVE_REFERENCE, "Node \"%s\" not found as a child of \"%s\" node.", opaq->name.name, |
| 578 | LYD_NAME(parent)); |
| 579 | } else { |
| 580 | LOGVAL(ctx, LYVE_REFERENCE, "Node \"%s\" not found in the \"%s\" module.", opaq->name.name, mod->name); |
| 581 | } |
| 582 | return LY_EVALID; |
| 583 | } |
| 584 | |
| 585 | if (snode->nodetype & LYD_NODE_TERM) { |
| 586 | /* leaf / leaf-list */ |
| 587 | LY_CHECK_RET(lys_value_validate(ctx, snode, opaq->value, strlen(opaq->value), opaq->format, opaq->val_prefix_data)); |
| 588 | } else if (snode->nodetype == LYS_LIST) { |
| 589 | /* list */ |
| 590 | LY_CHECK_RET(lyd_parse_opaq_list_error(node, snode)); |
| 591 | } else if (snode->nodetype & LYD_NODE_INNER) { |
| 592 | /* inner node */ |
| 593 | if (opaq->value) { |
| 594 | LOGVAL(ctx, LYVE_DATA, "Invalid value \"%s\" for %s \"%s\".", opaq->value, |
| 595 | lys_nodetype2str(snode->nodetype), snode->name); |
| 596 | return LY_EVALID; |
| 597 | } |
| 598 | } else { |
| 599 | LOGERR(ctx, LY_EINVAL, "Unexpected opaque schema node %s \"%s\".", lys_nodetype2str(snode->nodetype), snode->name); |
| 600 | return LY_EINVAL; |
| 601 | } |
| 602 | |
| 603 | LOGERR(ctx, LY_EINVAL, "Unexpected valid opaque node %s \"%s\".", lys_nodetype2str(snode->nodetype), snode->name); |
| 604 | return LY_EINVAL; |
| 605 | } |
| 606 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 607 | LIBYANG_API_DEF const char * |
Christian Hopps | 46bd21b | 2021-04-27 09:43:58 -0400 | [diff] [blame] | 608 | 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] | 609 | { |
Michal Vasko | ab40e7e | 2021-04-28 17:04:24 +0200 | [diff] [blame] | 610 | LY_CHECK_ARG_RET(ctx, ctx, value, NULL); |
| 611 | |
Michal Vasko | 3387602 | 2021-04-27 16:42:24 +0200 | [diff] [blame] | 612 | return value->_canonical ? value->_canonical : |
| 613 | (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] | 614 | } |
| 615 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 616 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | a820c31 | 2021-02-05 16:33:00 +0100 | [diff] [blame] | 617 | lyd_any_value_str(const struct lyd_node *any, char **value_str) |
| 618 | { |
| 619 | const struct lyd_node_any *a; |
| 620 | struct lyd_node *tree = NULL; |
| 621 | const char *str = NULL; |
| 622 | ly_bool dynamic = 0; |
| 623 | LY_ERR ret = LY_SUCCESS; |
| 624 | |
| 625 | LY_CHECK_ARG_RET(NULL, any, value_str, LY_EINVAL); |
Radek Krejci | 71877df | 2021-04-06 17:24:06 +0200 | [diff] [blame] | 626 | 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] | 627 | |
| 628 | a = (struct lyd_node_any *)any; |
| 629 | *value_str = NULL; |
| 630 | |
| 631 | if (!a->value.str) { |
| 632 | /* there is no value in the union */ |
| 633 | return LY_SUCCESS; |
| 634 | } |
| 635 | |
| 636 | switch (a->value_type) { |
| 637 | case LYD_ANYDATA_LYB: |
| 638 | /* parse into a data tree */ |
| 639 | ret = lyd_parse_data_mem(LYD_CTX(any), a->value.mem, LYD_LYB, LYD_PARSE_ONLY, 0, &tree); |
| 640 | LY_CHECK_GOTO(ret, cleanup); |
| 641 | dynamic = 1; |
| 642 | break; |
| 643 | case LYD_ANYDATA_DATATREE: |
| 644 | tree = a->value.tree; |
| 645 | break; |
| 646 | case LYD_ANYDATA_STRING: |
| 647 | case LYD_ANYDATA_XML: |
| 648 | case LYD_ANYDATA_JSON: |
| 649 | /* simply use the string */ |
| 650 | str = a->value.str; |
| 651 | break; |
| 652 | } |
| 653 | |
| 654 | if (tree) { |
| 655 | /* print into a string */ |
| 656 | ret = lyd_print_mem(value_str, tree, LYD_XML, LYD_PRINT_WITHSIBLINGS); |
| 657 | LY_CHECK_GOTO(ret, cleanup); |
| 658 | } else { |
| 659 | assert(str); |
| 660 | *value_str = strdup(str); |
| 661 | LY_CHECK_ERR_GOTO(!*value_str, LOGMEM(LYD_CTX(any)), cleanup); |
| 662 | } |
| 663 | |
| 664 | /* success */ |
| 665 | |
| 666 | cleanup: |
| 667 | if (dynamic) { |
| 668 | lyd_free_all(tree); |
| 669 | } |
| 670 | return ret; |
| 671 | } |
| 672 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 673 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 674 | lyd_any_copy_value(struct lyd_node *trg, const union lyd_any_value *value, LYD_ANYDATA_VALUETYPE value_type) |
| 675 | { |
| 676 | struct lyd_node_any *t; |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 677 | |
Michal Vasko | a820c31 | 2021-02-05 16:33:00 +0100 | [diff] [blame] | 678 | LY_CHECK_ARG_RET(NULL, trg, LY_EINVAL); |
Radek Krejci | 71877df | 2021-04-06 17:24:06 +0200 | [diff] [blame] | 679 | 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] | 680 | |
| 681 | t = (struct lyd_node_any *)trg; |
| 682 | |
| 683 | /* free trg */ |
| 684 | switch (t->value_type) { |
| 685 | case LYD_ANYDATA_DATATREE: |
| 686 | lyd_free_all(t->value.tree); |
| 687 | break; |
| 688 | case LYD_ANYDATA_STRING: |
| 689 | case LYD_ANYDATA_XML: |
| 690 | case LYD_ANYDATA_JSON: |
Michal Vasko | e180ed0 | 2021-02-05 16:31:20 +0100 | [diff] [blame] | 691 | lydict_remove(LYD_CTX(trg), t->value.str); |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 692 | break; |
| 693 | case LYD_ANYDATA_LYB: |
| 694 | free(t->value.mem); |
| 695 | break; |
| 696 | } |
| 697 | t->value.str = NULL; |
| 698 | |
| 699 | if (!value) { |
| 700 | /* only free value in this case */ |
| 701 | return LY_SUCCESS; |
| 702 | } |
| 703 | |
| 704 | /* copy src */ |
| 705 | t->value_type = value_type; |
| 706 | switch (value_type) { |
| 707 | case LYD_ANYDATA_DATATREE: |
| 708 | if (value->tree) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 709 | 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] | 710 | } |
| 711 | break; |
| 712 | case LYD_ANYDATA_STRING: |
| 713 | case LYD_ANYDATA_XML: |
| 714 | case LYD_ANYDATA_JSON: |
| 715 | if (value->str) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 716 | 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] | 717 | } |
| 718 | break; |
| 719 | case LYD_ANYDATA_LYB: |
| 720 | if (value->mem) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 721 | int len = lyd_lyb_data_length(value->mem); |
Radek Krejci | 82fa8d4 | 2020-07-11 22:00:59 +0200 | [diff] [blame] | 722 | LY_CHECK_RET(len == -1, LY_EINVAL); |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 723 | t->value.mem = malloc(len); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 724 | 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] | 725 | memcpy(t->value.mem, value->mem, len); |
| 726 | } |
| 727 | break; |
| 728 | } |
| 729 | |
| 730 | return LY_SUCCESS; |
| 731 | } |
| 732 | |
Michal Vasko | 106f086 | 2021-11-02 11:49:27 +0100 | [diff] [blame] | 733 | const struct lysc_node * |
| 734 | lyd_node_schema(const struct lyd_node *node) |
| 735 | { |
| 736 | const struct lysc_node *schema = NULL; |
| 737 | const struct lyd_node *prev_iter = NULL, *iter; |
| 738 | const struct lys_module *mod; |
| 739 | |
| 740 | if (!node) { |
| 741 | return NULL; |
| 742 | } else if (node->schema) { |
| 743 | return node->schema; |
| 744 | } |
| 745 | |
| 746 | /* get schema node of an opaque node */ |
| 747 | do { |
| 748 | /* get next data node */ |
| 749 | for (iter = node; lyd_parent(iter) != prev_iter; iter = lyd_parent(iter)) {} |
| 750 | |
| 751 | /* get equivalent schema node */ |
| 752 | if (iter->schema) { |
| 753 | schema = iter->schema; |
| 754 | } else { |
| 755 | /* get module */ |
| 756 | mod = lyd_owner_module(iter); |
Michal Vasko | a41826a | 2021-11-02 12:13:03 +0100 | [diff] [blame] | 757 | if (!mod && !schema) { |
| 758 | /* top-level opaque node has unknown module */ |
| 759 | break; |
| 760 | } |
Michal Vasko | 106f086 | 2021-11-02 11:49:27 +0100 | [diff] [blame] | 761 | |
| 762 | /* get schema node */ |
| 763 | schema = lys_find_child(schema, mod ? mod : schema->module, LYD_NAME(iter), 0, 0, 0); |
| 764 | } |
Michal Vasko | d2f404f | 2021-11-04 15:37:11 +0100 | [diff] [blame] | 765 | |
| 766 | /* remember to move to the descendant */ |
| 767 | prev_iter = iter; |
Michal Vasko | 106f086 | 2021-11-02 11:49:27 +0100 | [diff] [blame] | 768 | } while (schema && (iter != node)); |
| 769 | |
| 770 | return schema; |
| 771 | } |
| 772 | |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 773 | void |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 774 | lyd_del_move_root(struct lyd_node **root, const struct lyd_node *to_del, const struct lys_module *mod) |
| 775 | { |
| 776 | if (*root && (lyd_owner_module(*root) != mod)) { |
| 777 | /* there are no data of mod so this is simply the first top-level sibling */ |
| 778 | mod = NULL; |
| 779 | } |
| 780 | |
| 781 | if ((*root != to_del) || (*root)->parent) { |
| 782 | return; |
| 783 | } |
| 784 | |
Michal Vasko | 598063b | 2021-07-19 11:39:05 +0200 | [diff] [blame] | 785 | if (mod && (*root)->prev->next && (!(*root)->next || (lyd_owner_module(to_del) != lyd_owner_module((*root)->next)))) { |
| 786 | /* 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] | 787 | *root = lyd_first_sibling(*root); |
Michal Vasko | 598063b | 2021-07-19 11:39:05 +0200 | [diff] [blame] | 788 | } else { |
| 789 | *root = (*root)->next; |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 790 | } |
| 791 | } |
| 792 | |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 793 | LY_ERR |
| 794 | ly_nested_ext_schema(const struct lyd_node *parent, const struct lysc_node *sparent, const char *prefix, |
| 795 | size_t prefix_len, LY_VALUE_FORMAT format, void *prefix_data, const char *name, size_t name_len, |
| 796 | const struct lysc_node **snode, struct lysc_ext_instance **ext) |
| 797 | { |
| 798 | LY_ERR r; |
| 799 | LY_ARRAY_COUNT_TYPE u; |
| 800 | struct lysc_ext_instance *nested_exts = NULL; |
| 801 | lyplg_ext_data_snode_clb ext_snode_cb; |
| 802 | |
| 803 | /* check if there are any nested extension instances */ |
| 804 | if (parent && parent->schema) { |
| 805 | nested_exts = parent->schema->exts; |
| 806 | } else if (sparent) { |
| 807 | nested_exts = sparent->exts; |
| 808 | } |
| 809 | LY_ARRAY_FOR(nested_exts, u) { |
Michal Vasko | 305c6cb | 2022-04-27 10:33:04 +0200 | [diff] [blame^] | 810 | if (!nested_exts[u].def->plugin) { |
| 811 | /* no plugin */ |
| 812 | continue; |
| 813 | } |
| 814 | |
Michal Vasko | 8cc3f66 | 2022-03-29 11:25:51 +0200 | [diff] [blame] | 815 | ext_snode_cb = nested_exts[u].def->plugin->snode; |
| 816 | if (!ext_snode_cb) { |
| 817 | /* not an extension with nested data */ |
| 818 | continue; |
| 819 | } |
| 820 | |
| 821 | /* try to get the schema node */ |
| 822 | r = ext_snode_cb(&nested_exts[u], parent, sparent, prefix, prefix_len, format, prefix_data, name, name_len, snode); |
| 823 | if (!r) { |
| 824 | /* data successfully created, remember the ext instance */ |
| 825 | *ext = &nested_exts[u]; |
| 826 | return LY_SUCCESS; |
| 827 | } else if (r != LY_ENOT) { |
| 828 | /* fatal error */ |
| 829 | return r; |
| 830 | } |
| 831 | /* data was not from this module, continue */ |
| 832 | } |
| 833 | |
| 834 | /* no extensions or none matched */ |
| 835 | return LY_ENOT; |
| 836 | } |
| 837 | |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 838 | void |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 839 | ly_free_prefix_data(LY_VALUE_FORMAT format, void *prefix_data) |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 840 | { |
| 841 | struct ly_set *ns_list; |
| 842 | struct lysc_prefix *prefixes; |
| 843 | uint32_t i; |
| 844 | LY_ARRAY_COUNT_TYPE u; |
| 845 | |
| 846 | if (!prefix_data) { |
| 847 | return; |
| 848 | } |
| 849 | |
| 850 | switch (format) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 851 | case LY_VALUE_XML: |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 852 | case LY_VALUE_STR_NS: |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 853 | ns_list = prefix_data; |
| 854 | for (i = 0; i < ns_list->count; ++i) { |
| 855 | free(((struct lyxml_ns *)ns_list->objs[i])->prefix); |
| 856 | free(((struct lyxml_ns *)ns_list->objs[i])->uri); |
| 857 | } |
| 858 | ly_set_free(ns_list, free); |
| 859 | break; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 860 | case LY_VALUE_SCHEMA_RESOLVED: |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 861 | prefixes = prefix_data; |
| 862 | LY_ARRAY_FOR(prefixes, u) { |
| 863 | free(prefixes[u].prefix); |
| 864 | } |
| 865 | LY_ARRAY_FREE(prefixes); |
| 866 | break; |
Radek Krejci | 224d4b4 | 2021-04-23 13:54:59 +0200 | [diff] [blame] | 867 | case LY_VALUE_CANON: |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 868 | case LY_VALUE_SCHEMA: |
| 869 | case LY_VALUE_JSON: |
Radek Krejci | f994364 | 2021-04-26 10:18:21 +0200 | [diff] [blame] | 870 | case LY_VALUE_LYB: |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 871 | break; |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | LY_ERR |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 876 | 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] | 877 | void **prefix_data_p) |
| 878 | { |
| 879 | LY_ERR ret = LY_SUCCESS; |
| 880 | struct lyxml_ns *ns; |
| 881 | struct lysc_prefix *prefixes = NULL, *orig_pref; |
| 882 | struct ly_set *ns_list, *orig_ns; |
| 883 | uint32_t i; |
| 884 | LY_ARRAY_COUNT_TYPE u; |
| 885 | |
| 886 | assert(!*prefix_data_p); |
| 887 | |
| 888 | switch (format) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 889 | case LY_VALUE_SCHEMA: |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 890 | *prefix_data_p = (void *)prefix_data; |
| 891 | break; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 892 | case LY_VALUE_SCHEMA_RESOLVED: |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 893 | /* copy all the value prefixes */ |
| 894 | orig_pref = (struct lysc_prefix *)prefix_data; |
| 895 | LY_ARRAY_CREATE_GOTO(ctx, prefixes, LY_ARRAY_COUNT(orig_pref), ret, cleanup); |
| 896 | *prefix_data_p = prefixes; |
| 897 | |
| 898 | LY_ARRAY_FOR(orig_pref, u) { |
| 899 | if (orig_pref[u].prefix) { |
| 900 | prefixes[u].prefix = strdup(orig_pref[u].prefix); |
| 901 | LY_CHECK_ERR_GOTO(!prefixes[u].prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 902 | } |
| 903 | prefixes[u].mod = orig_pref[u].mod; |
| 904 | LY_ARRAY_INCREMENT(prefixes); |
| 905 | } |
| 906 | break; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 907 | case LY_VALUE_XML: |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 908 | case LY_VALUE_STR_NS: |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 909 | /* copy all the namespaces */ |
| 910 | LY_CHECK_GOTO(ret = ly_set_new(&ns_list), cleanup); |
| 911 | *prefix_data_p = ns_list; |
| 912 | |
| 913 | orig_ns = (struct ly_set *)prefix_data; |
| 914 | for (i = 0; i < orig_ns->count; ++i) { |
| 915 | ns = calloc(1, sizeof *ns); |
| 916 | LY_CHECK_ERR_GOTO(!ns, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 917 | LY_CHECK_GOTO(ret = ly_set_add(ns_list, ns, 1, NULL), cleanup); |
| 918 | |
| 919 | if (((struct lyxml_ns *)orig_ns->objs[i])->prefix) { |
| 920 | ns->prefix = strdup(((struct lyxml_ns *)orig_ns->objs[i])->prefix); |
| 921 | LY_CHECK_ERR_GOTO(!ns->prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 922 | } |
| 923 | ns->uri = strdup(((struct lyxml_ns *)orig_ns->objs[i])->uri); |
| 924 | LY_CHECK_ERR_GOTO(!ns->uri, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 925 | } |
| 926 | break; |
Radek Krejci | 224d4b4 | 2021-04-23 13:54:59 +0200 | [diff] [blame] | 927 | case LY_VALUE_CANON: |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 928 | case LY_VALUE_JSON: |
Radek Krejci | f994364 | 2021-04-26 10:18:21 +0200 | [diff] [blame] | 929 | case LY_VALUE_LYB: |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 930 | assert(!prefix_data); |
| 931 | *prefix_data_p = NULL; |
| 932 | break; |
| 933 | } |
| 934 | |
| 935 | cleanup: |
| 936 | if (ret) { |
| 937 | ly_free_prefix_data(format, *prefix_data_p); |
| 938 | *prefix_data_p = NULL; |
| 939 | } |
| 940 | return ret; |
| 941 | } |
| 942 | |
| 943 | LY_ERR |
Radek Krejci | f994364 | 2021-04-26 10:18:21 +0200 | [diff] [blame] | 944 | 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] | 945 | 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] | 946 | { |
| 947 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 948 | const struct lys_module *mod; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 949 | const struct lyxml_ns *ns; |
| 950 | struct lyxml_ns *new_ns; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 951 | struct ly_set *ns_list; |
| 952 | struct lysc_prefix *prefixes = NULL, *val_pref; |
aPiecek | 83436bc | 2021-03-30 12:20:45 +0200 | [diff] [blame] | 953 | const char *value_iter, *value_next, *value_end; |
| 954 | uint32_t substr_len; |
| 955 | ly_bool is_prefix; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 956 | |
| 957 | switch (format) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 958 | case LY_VALUE_SCHEMA: |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 959 | /* copy all referenced modules as prefix - module pairs */ |
| 960 | if (!*prefix_data_p) { |
| 961 | /* new prefix data */ |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 962 | LY_ARRAY_CREATE_GOTO(ctx, prefixes, 0, ret, cleanup); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 963 | *format_p = LY_VALUE_SCHEMA_RESOLVED; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 964 | *prefix_data_p = prefixes; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 965 | } else { |
| 966 | /* reuse prefix data */ |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 967 | assert(*format_p == LY_VALUE_SCHEMA_RESOLVED); |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 968 | prefixes = *prefix_data_p; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | /* add all used prefixes */ |
Michal Vasko | 59e90fc | 2021-09-22 12:17:08 +0200 | [diff] [blame] | 972 | value_end = (char *)value + value_len; |
aPiecek | 83436bc | 2021-03-30 12:20:45 +0200 | [diff] [blame] | 973 | for (value_iter = value; value_iter; value_iter = value_next) { |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 974 | 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] | 975 | if (is_prefix) { |
| 976 | /* we have a possible prefix. Do we already have the prefix? */ |
| 977 | mod = ly_resolve_prefix(ctx, value_iter, substr_len, *format_p, *prefix_data_p); |
| 978 | if (!mod) { |
| 979 | mod = ly_resolve_prefix(ctx, value_iter, substr_len, format, prefix_data); |
| 980 | if (mod) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 981 | assert(*format_p == LY_VALUE_SCHEMA_RESOLVED); |
aPiecek | 83436bc | 2021-03-30 12:20:45 +0200 | [diff] [blame] | 982 | /* store a new prefix - module pair */ |
| 983 | LY_ARRAY_NEW_GOTO(ctx, prefixes, val_pref, ret, cleanup); |
| 984 | *prefix_data_p = prefixes; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 985 | |
aPiecek | 83436bc | 2021-03-30 12:20:45 +0200 | [diff] [blame] | 986 | val_pref->prefix = strndup(value_iter, substr_len); |
| 987 | LY_CHECK_ERR_GOTO(!val_pref->prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 988 | val_pref->mod = mod; |
| 989 | } /* else it is not even defined */ |
| 990 | } /* else the prefix is already present */ |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 991 | } |
| 992 | } |
| 993 | break; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 994 | case LY_VALUE_XML: |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 995 | case LY_VALUE_STR_NS: |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 996 | /* copy all referenced namespaces as prefix - namespace pairs */ |
| 997 | if (!*prefix_data_p) { |
| 998 | /* new prefix data */ |
| 999 | LY_CHECK_GOTO(ret = ly_set_new(&ns_list), cleanup); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1000 | *format_p = format; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1001 | *prefix_data_p = ns_list; |
| 1002 | } else { |
| 1003 | /* reuse prefix data */ |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1004 | assert(*format_p == format); |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1005 | ns_list = *prefix_data_p; |
| 1006 | } |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 1007 | |
Michal Vasko | 294e7f0 | 2022-02-28 13:59:00 +0100 | [diff] [blame] | 1008 | /* store default namespace */ |
| 1009 | ns = lyxml_ns_get(prefix_data, NULL, 0); |
| 1010 | if (ns) { |
| 1011 | new_ns = calloc(1, sizeof *new_ns); |
| 1012 | LY_CHECK_ERR_GOTO(!new_ns, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 1013 | LY_CHECK_GOTO(ret = ly_set_add(ns_list, new_ns, 1, NULL), cleanup); |
| 1014 | |
| 1015 | new_ns->prefix = NULL; |
| 1016 | new_ns->uri = strdup(ns->uri); |
| 1017 | LY_CHECK_ERR_GOTO(!new_ns->uri, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 1018 | } |
| 1019 | |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1020 | /* add all used prefixes */ |
Michal Vasko | 59e90fc | 2021-09-22 12:17:08 +0200 | [diff] [blame] | 1021 | value_end = (char *)value + value_len; |
aPiecek | 83436bc | 2021-03-30 12:20:45 +0200 | [diff] [blame] | 1022 | for (value_iter = value; value_iter; value_iter = value_next) { |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 1023 | 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] | 1024 | if (is_prefix) { |
| 1025 | /* we have a possible prefix. Do we already have the prefix? */ |
| 1026 | ns = lyxml_ns_get(ns_list, value_iter, substr_len); |
| 1027 | if (!ns) { |
| 1028 | ns = lyxml_ns_get(prefix_data, value_iter, substr_len); |
| 1029 | if (ns) { |
| 1030 | /* store a new prefix - namespace pair */ |
| 1031 | new_ns = calloc(1, sizeof *new_ns); |
| 1032 | LY_CHECK_ERR_GOTO(!new_ns, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 1033 | 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] | 1034 | |
aPiecek | 83436bc | 2021-03-30 12:20:45 +0200 | [diff] [blame] | 1035 | new_ns->prefix = strndup(value_iter, substr_len); |
| 1036 | LY_CHECK_ERR_GOTO(!new_ns->prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 1037 | new_ns->uri = strdup(ns->uri); |
| 1038 | LY_CHECK_ERR_GOTO(!new_ns->uri, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 1039 | } /* else it is not even defined */ |
| 1040 | } /* else the prefix is already present */ |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 1041 | } |
| 1042 | } |
| 1043 | break; |
Radek Krejci | 224d4b4 | 2021-04-23 13:54:59 +0200 | [diff] [blame] | 1044 | case LY_VALUE_CANON: |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1045 | case LY_VALUE_SCHEMA_RESOLVED: |
| 1046 | case LY_VALUE_JSON: |
Radek Krejci | f994364 | 2021-04-26 10:18:21 +0200 | [diff] [blame] | 1047 | case LY_VALUE_LYB: |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 1048 | if (!*prefix_data_p) { |
| 1049 | /* new prefix data - simply copy all the prefix data */ |
| 1050 | *format_p = format; |
| 1051 | LY_CHECK_GOTO(ret = ly_dup_prefix_data(ctx, format, prefix_data, prefix_data_p), cleanup); |
| 1052 | } /* 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] | 1053 | break; |
| 1054 | } |
| 1055 | |
| 1056 | cleanup: |
| 1057 | if (ret) { |
| 1058 | ly_free_prefix_data(*format_p, *prefix_data_p); |
| 1059 | *prefix_data_p = NULL; |
| 1060 | } |
| 1061 | return ret; |
| 1062 | } |
Michal Vasko | 7ed1fcb | 2020-12-03 14:15:22 +0100 | [diff] [blame] | 1063 | |
| 1064 | const char * |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1065 | ly_format2str(LY_VALUE_FORMAT format) |
Michal Vasko | 7ed1fcb | 2020-12-03 14:15:22 +0100 | [diff] [blame] | 1066 | { |
| 1067 | switch (format) { |
Radek Krejci | 224d4b4 | 2021-04-23 13:54:59 +0200 | [diff] [blame] | 1068 | case LY_VALUE_CANON: |
| 1069 | return "canonical"; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1070 | case LY_VALUE_SCHEMA: |
Michal Vasko | 7ed1fcb | 2020-12-03 14:15:22 +0100 | [diff] [blame] | 1071 | return "schema imports"; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1072 | case LY_VALUE_SCHEMA_RESOLVED: |
Michal Vasko | 7ed1fcb | 2020-12-03 14:15:22 +0100 | [diff] [blame] | 1073 | return "schema stored mapping"; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1074 | case LY_VALUE_XML: |
Michal Vasko | 7ed1fcb | 2020-12-03 14:15:22 +0100 | [diff] [blame] | 1075 | return "XML prefixes"; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 1076 | case LY_VALUE_JSON: |
Michal Vasko | 7ed1fcb | 2020-12-03 14:15:22 +0100 | [diff] [blame] | 1077 | return "JSON module names"; |
Radek Krejci | f994364 | 2021-04-26 10:18:21 +0200 | [diff] [blame] | 1078 | case LY_VALUE_LYB: |
| 1079 | return "LYB prefixes"; |
Michal Vasko | 7ed1fcb | 2020-12-03 14:15:22 +0100 | [diff] [blame] | 1080 | default: |
| 1081 | break; |
| 1082 | } |
| 1083 | |
| 1084 | return NULL; |
| 1085 | } |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 1086 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1087 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 1088 | ly_time_str2time(const char *value, time_t *time, char **fractions_s) |
| 1089 | { |
| 1090 | struct tm tm = {0}; |
| 1091 | uint32_t i, frac_len; |
| 1092 | const char *frac; |
| 1093 | int64_t shift, shift_m; |
| 1094 | time_t t; |
| 1095 | |
| 1096 | LY_CHECK_ARG_RET(NULL, value, time, LY_EINVAL); |
| 1097 | |
| 1098 | tm.tm_year = atoi(&value[0]) - 1900; |
| 1099 | tm.tm_mon = atoi(&value[5]) - 1; |
| 1100 | tm.tm_mday = atoi(&value[8]); |
| 1101 | tm.tm_hour = atoi(&value[11]); |
| 1102 | tm.tm_min = atoi(&value[14]); |
| 1103 | tm.tm_sec = atoi(&value[17]); |
| 1104 | |
| 1105 | t = timegm(&tm); |
| 1106 | i = 19; |
| 1107 | |
| 1108 | /* fractions of a second */ |
| 1109 | if (value[i] == '.') { |
| 1110 | ++i; |
| 1111 | frac = &value[i]; |
| 1112 | for (frac_len = 0; isdigit(frac[frac_len]); ++frac_len) {} |
| 1113 | |
| 1114 | i += frac_len; |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 1115 | } else { |
| 1116 | frac = NULL; |
| 1117 | } |
| 1118 | |
| 1119 | /* apply offset */ |
| 1120 | if ((value[i] == 'Z') || (value[i] == 'z')) { |
| 1121 | /* zero shift */ |
| 1122 | shift = 0; |
| 1123 | } else { |
| 1124 | shift = strtol(&value[i], NULL, 10); |
| 1125 | shift = shift * 60 * 60; /* convert from hours to seconds */ |
| 1126 | shift_m = strtol(&value[i + 4], NULL, 10) * 60; /* includes conversion from minutes to seconds */ |
| 1127 | /* correct sign */ |
| 1128 | if (shift < 0) { |
| 1129 | shift_m *= -1; |
| 1130 | } |
| 1131 | /* connect hours and minutes of the shift */ |
| 1132 | shift = shift + shift_m; |
| 1133 | } |
| 1134 | |
| 1135 | /* we have to shift to the opposite way to correct the time */ |
| 1136 | t -= shift; |
| 1137 | |
| 1138 | *time = t; |
| 1139 | if (fractions_s) { |
| 1140 | if (frac) { |
| 1141 | *fractions_s = strndup(frac, frac_len); |
| 1142 | LY_CHECK_RET(!*fractions_s, LY_EMEM); |
| 1143 | } else { |
| 1144 | *fractions_s = NULL; |
| 1145 | } |
| 1146 | } |
| 1147 | return LY_SUCCESS; |
| 1148 | } |
| 1149 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1150 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 1151 | ly_time_time2str(time_t time, const char *fractions_s, char **str) |
| 1152 | { |
| 1153 | struct tm tm; |
Michal Vasko | 143ffa8 | 2021-05-20 11:11:39 +0200 | [diff] [blame] | 1154 | char zoneshift[8]; |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 1155 | int32_t zonediff_h, zonediff_m; |
| 1156 | |
| 1157 | LY_CHECK_ARG_RET(NULL, str, LY_EINVAL); |
| 1158 | |
| 1159 | /* initialize the local timezone */ |
| 1160 | tzset(); |
| 1161 | |
Jan Kundrát | b17efe9 | 2022-02-14 18:32:18 +0100 | [diff] [blame] | 1162 | #ifdef HAVE_TM_GMTOFF |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 1163 | /* convert */ |
| 1164 | if (!localtime_r(&time, &tm)) { |
| 1165 | return LY_ESYS; |
| 1166 | } |
| 1167 | |
| 1168 | /* get timezone offset */ |
| 1169 | if (tm.tm_gmtoff == 0) { |
| 1170 | /* time is Zulu (UTC) */ |
| 1171 | zonediff_h = 0; |
| 1172 | zonediff_m = 0; |
| 1173 | } else { |
| 1174 | /* timezone offset */ |
| 1175 | zonediff_h = tm.tm_gmtoff / 60 / 60; |
| 1176 | zonediff_m = tm.tm_gmtoff / 60 % 60; |
| 1177 | } |
| 1178 | sprintf(zoneshift, "%+03d:%02d", zonediff_h, zonediff_m); |
Jan Kundrát | e182a27 | 2021-12-09 23:25:15 +0100 | [diff] [blame] | 1179 | #else |
Jan Kundrát | b17efe9 | 2022-02-14 18:32:18 +0100 | [diff] [blame] | 1180 | /* convert */ |
| 1181 | if (!gmtime_r(&time, &tm)) { |
| 1182 | return LY_ESYS; |
| 1183 | } |
| 1184 | |
Jan Kundrát | e182a27 | 2021-12-09 23:25:15 +0100 | [diff] [blame] | 1185 | (void)zonediff_h; |
| 1186 | (void)zonediff_m; |
| 1187 | sprintf(zoneshift, "-00:00"); |
| 1188 | #endif |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 1189 | |
| 1190 | /* print */ |
| 1191 | if (asprintf(str, "%04d-%02d-%02dT%02d:%02d:%02d%s%s%s", |
| 1192 | tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, |
| 1193 | fractions_s ? "." : "", fractions_s ? fractions_s : "", zoneshift) == -1) { |
| 1194 | return LY_EMEM; |
| 1195 | } |
| 1196 | |
| 1197 | return LY_SUCCESS; |
| 1198 | } |
| 1199 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1200 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 1201 | ly_time_str2ts(const char *value, struct timespec *ts) |
| 1202 | { |
| 1203 | LY_ERR rc; |
Michal Vasko | 7297506 | 2021-08-25 08:13:04 +0200 | [diff] [blame] | 1204 | char *fractions_s, frac_buf[10]; |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 1205 | int frac_len; |
| 1206 | |
| 1207 | LY_CHECK_ARG_RET(NULL, value, ts, LY_EINVAL); |
| 1208 | |
| 1209 | rc = ly_time_str2time(value, &ts->tv_sec, &fractions_s); |
| 1210 | LY_CHECK_RET(rc); |
| 1211 | |
| 1212 | /* convert fractions of a second to nanoseconds */ |
| 1213 | if (fractions_s) { |
Michal Vasko | 7297506 | 2021-08-25 08:13:04 +0200 | [diff] [blame] | 1214 | /* init frac_buf with zeroes */ |
| 1215 | memset(frac_buf, '0', 9); |
| 1216 | frac_buf[9] = '\0'; |
| 1217 | |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 1218 | frac_len = strlen(fractions_s); |
| 1219 | memcpy(frac_buf, fractions_s, frac_len > 9 ? 9 : frac_len); |
| 1220 | ts->tv_nsec = atol(frac_buf); |
| 1221 | free(fractions_s); |
| 1222 | } else { |
| 1223 | ts->tv_nsec = 0; |
| 1224 | } |
| 1225 | |
| 1226 | return LY_SUCCESS; |
| 1227 | } |
| 1228 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1229 | LIBYANG_API_DEF LY_ERR |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 1230 | ly_time_ts2str(const struct timespec *ts, char **str) |
| 1231 | { |
| 1232 | char frac_buf[10]; |
| 1233 | |
Jan Kundrát | bd15700 | 2021-08-30 14:02:22 +0200 | [diff] [blame] | 1234 | 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] | 1235 | |
| 1236 | /* convert nanoseconds to fractions of a second */ |
| 1237 | if (ts->tv_nsec) { |
| 1238 | sprintf(frac_buf, "%09ld", ts->tv_nsec); |
| 1239 | } |
| 1240 | |
| 1241 | return ly_time_time2str(ts->tv_sec, ts->tv_nsec ? frac_buf : NULL, str); |
| 1242 | } |