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