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" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 42 | |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 43 | /** |
| 44 | * @brief Find an entry in duplicate instance cache for an instance. Create it if it does not exist. |
| 45 | * |
| 46 | * @param[in] first_inst Instance of the cache entry. |
| 47 | * @param[in,out] dup_inst_cache Duplicate instance cache. |
| 48 | * @return Instance cache entry. |
| 49 | */ |
| 50 | static struct lyd_dup_inst * |
| 51 | lyd_dup_inst_get(const struct lyd_node *first_inst, struct lyd_dup_inst **dup_inst_cache) |
| 52 | { |
| 53 | struct lyd_dup_inst *item; |
| 54 | LY_ARRAY_COUNT_TYPE u; |
| 55 | |
| 56 | LY_ARRAY_FOR(*dup_inst_cache, u) { |
| 57 | if ((*dup_inst_cache)[u].inst_set->dnodes[0] == first_inst) { |
| 58 | return &(*dup_inst_cache)[u]; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /* it was not added yet, add it now */ |
| 63 | LY_ARRAY_NEW_RET(LYD_CTX(first_inst), *dup_inst_cache, item, NULL); |
| 64 | |
| 65 | return item; |
| 66 | } |
| 67 | |
| 68 | LY_ERR |
| 69 | lyd_dup_inst_next(struct lyd_node **inst, const struct lyd_node *siblings, struct lyd_dup_inst **dup_inst_cache) |
| 70 | { |
| 71 | struct lyd_dup_inst *dup_inst; |
| 72 | |
| 73 | if (!*inst || !lysc_is_dup_inst_list((*inst)->schema)) { |
| 74 | /* no match or not dup-inst list, inst is unchanged */ |
| 75 | return LY_SUCCESS; |
| 76 | } |
| 77 | |
| 78 | /* there can be more exact same instances and we must make sure we do not match a single node more times */ |
| 79 | dup_inst = lyd_dup_inst_get(*inst, dup_inst_cache); |
| 80 | LY_CHECK_ERR_RET(!dup_inst, LOGMEM(LYD_CTX(siblings)), LY_EMEM); |
| 81 | |
| 82 | if (!dup_inst->used) { |
| 83 | /* we did not cache these instances yet, do so */ |
| 84 | lyd_find_sibling_dup_inst_set(siblings, *inst, &dup_inst->inst_set); |
| 85 | assert(dup_inst->inst_set->count && (dup_inst->inst_set->dnodes[0] == *inst)); |
| 86 | } |
| 87 | |
| 88 | if (dup_inst->used == dup_inst->inst_set->count) { |
| 89 | /* we have used all the instances */ |
| 90 | *inst = NULL; |
| 91 | } else { |
| 92 | assert(dup_inst->used < dup_inst->inst_set->count); |
| 93 | |
| 94 | /* use another instance */ |
| 95 | *inst = dup_inst->inst_set->dnodes[dup_inst->used]; |
| 96 | ++dup_inst->used; |
| 97 | } |
| 98 | |
| 99 | return LY_SUCCESS; |
| 100 | } |
| 101 | |
| 102 | void |
| 103 | lyd_dup_inst_free(struct lyd_dup_inst *dup_inst) |
| 104 | { |
| 105 | LY_ARRAY_COUNT_TYPE u; |
| 106 | |
| 107 | LY_ARRAY_FOR(dup_inst, u) { |
| 108 | ly_set_free(dup_inst[u].inst_set, NULL); |
| 109 | } |
| 110 | LY_ARRAY_FREE(dup_inst); |
| 111 | } |
| 112 | |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 113 | struct lyd_node * |
| 114 | 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] | 115 | const struct lysc_node *parent, const struct lysc_module *module) |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 116 | { |
| 117 | const struct lysc_node *siter = NULL; |
| 118 | struct lyd_node *match = NULL; |
| 119 | |
| 120 | assert(parent || module); |
| 121 | assert(!last || (slast && *slast)); |
| 122 | |
| 123 | if (slast) { |
| 124 | siter = *slast; |
| 125 | } |
| 126 | |
| 127 | if (last && last->next && (last->next->schema == siter)) { |
| 128 | /* return next data instance */ |
| 129 | return last->next; |
| 130 | } |
| 131 | |
| 132 | /* find next schema node data instance */ |
| 133 | while ((siter = lys_getnext(siter, parent, module, 0))) { |
| 134 | if (!lyd_find_sibling_val(sibling, siter, NULL, 0, &match)) { |
| 135 | break; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | if (slast) { |
| 140 | *slast = siter; |
| 141 | } |
| 142 | return match; |
| 143 | } |
| 144 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 145 | struct lyd_node ** |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 146 | lyd_node_child_p(struct lyd_node *node) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 147 | { |
| 148 | assert(node); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 149 | |
| 150 | if (!node->schema) { |
| 151 | return &((struct lyd_node_opaq *)node)->child; |
| 152 | } else { |
| 153 | switch (node->schema->nodetype) { |
| 154 | case LYS_CONTAINER: |
| 155 | case LYS_LIST: |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 156 | case LYS_RPC: |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 157 | case LYS_ACTION: |
| 158 | case LYS_NOTIF: |
| 159 | return &((struct lyd_node_inner *)node)->child; |
| 160 | default: |
| 161 | return NULL; |
| 162 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
Radek Krejci | dae0ee8 | 2020-05-06 16:53:24 +0200 | [diff] [blame] | 166 | API struct lyd_node * |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 167 | lyd_child_no_keys(const struct lyd_node *node) |
| 168 | { |
| 169 | struct lyd_node **children; |
| 170 | |
| 171 | if (!node) { |
| 172 | return NULL; |
| 173 | } |
| 174 | |
| 175 | if (!node->schema) { |
| 176 | /* opaq node */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 177 | return ((struct lyd_node_opaq *)node)->child; |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 178 | } |
| 179 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 180 | children = lyd_node_child_p((struct lyd_node *)node); |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 181 | if (children) { |
| 182 | struct lyd_node *child = *children; |
| 183 | while (child && child->schema && (child->schema->flags & LYS_KEY)) { |
| 184 | child = child->next; |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 185 | } |
| 186 | return child; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 187 | } else { |
| 188 | return NULL; |
| 189 | } |
| 190 | } |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 191 | |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 192 | API const struct lys_module * |
| 193 | lyd_owner_module(const struct lyd_node *node) |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 194 | { |
| 195 | const struct lysc_node *schema; |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 196 | const struct lyd_node_opaq *opaq; |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 197 | |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 198 | if (!node) { |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 199 | return NULL; |
| 200 | } |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 201 | |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 202 | if (!node->schema) { |
| 203 | opaq = (struct lyd_node_opaq *)node; |
| 204 | switch (opaq->format) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 205 | case LY_VALUE_XML: |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 206 | 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] | 207 | case LY_VALUE_JSON: |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 208 | return ly_ctx_get_module_implemented(LYD_CTX(node), opaq->name.module_name); |
| 209 | default: |
| 210 | return NULL; |
| 211 | } |
| 212 | } |
| 213 | |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 214 | for (schema = node->schema; schema->parent; schema = schema->parent) {} |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 215 | return schema->module; |
| 216 | } |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 217 | |
Michal Vasko | 598063b | 2021-07-19 11:39:05 +0200 | [diff] [blame] | 218 | void |
| 219 | lyd_first_module_sibling(struct lyd_node **node, const struct lys_module *mod) |
| 220 | { |
| 221 | int cmp; |
| 222 | struct lyd_node *first; |
| 223 | |
| 224 | assert(node && mod); |
| 225 | |
| 226 | if (!*node) { |
| 227 | return; |
| 228 | } |
| 229 | |
| 230 | first = *node; |
| 231 | cmp = strcmp(lyd_owner_module(first)->name, mod->name); |
| 232 | if (cmp > 0) { |
| 233 | /* there may be some preceding data */ |
| 234 | while (first->prev->next) { |
| 235 | first = first->prev; |
| 236 | if (lyd_owner_module(first) == mod) { |
| 237 | cmp = 0; |
| 238 | break; |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | if (cmp == 0) { |
| 244 | /* there may be some preceding data belonging to this module */ |
| 245 | while (first->prev->next) { |
| 246 | if (lyd_owner_module(first->prev) != mod) { |
| 247 | break; |
| 248 | } |
| 249 | first = first->prev; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | if (cmp < 0) { |
| 254 | /* there may be some following data */ |
| 255 | LY_LIST_FOR(first, first) { |
| 256 | if (lyd_owner_module(first) == mod) { |
| 257 | cmp = 0; |
| 258 | break; |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | if (cmp == 0) { |
| 264 | /* we have found the first module data node */ |
| 265 | *node = first; |
| 266 | } |
| 267 | } |
| 268 | |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 269 | const struct lys_module * |
Michal Vasko | 26e8001 | 2020-07-08 10:55:46 +0200 | [diff] [blame] | 270 | 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] | 271 | struct lyd_node **first) |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 272 | { |
| 273 | struct lyd_node *iter; |
| 274 | const struct lys_module *mod; |
| 275 | |
| 276 | /* get the next module */ |
Michal Vasko | 26e8001 | 2020-07-08 10:55:46 +0200 | [diff] [blame] | 277 | if (module) { |
| 278 | if (*i) { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 279 | mod = NULL; |
Michal Vasko | 26e8001 | 2020-07-08 10:55:46 +0200 | [diff] [blame] | 280 | } else { |
| 281 | mod = module; |
| 282 | ++(*i); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 283 | } |
| 284 | } else { |
| 285 | do { |
| 286 | mod = ly_ctx_get_module_iter(ctx, i); |
| 287 | } while (mod && !mod->implemented); |
| 288 | } |
| 289 | |
| 290 | /* find its data */ |
| 291 | *first = NULL; |
| 292 | if (mod) { |
| 293 | LY_LIST_FOR(tree, iter) { |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 294 | if (lyd_owner_module(iter) == mod) { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 295 | *first = iter; |
| 296 | break; |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | return mod; |
| 302 | } |
| 303 | |
| 304 | const struct lys_module * |
| 305 | lyd_data_next_module(struct lyd_node **next, struct lyd_node **first) |
| 306 | { |
| 307 | const struct lys_module *mod; |
| 308 | |
| 309 | if (!*next) { |
| 310 | /* all data traversed */ |
| 311 | *first = NULL; |
| 312 | return NULL; |
| 313 | } |
| 314 | |
| 315 | *first = *next; |
| 316 | |
| 317 | /* prepare next */ |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 318 | mod = lyd_owner_module(*next); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 319 | LY_LIST_FOR(*next, *next) { |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 320 | if (lyd_owner_module(*next) != mod) { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 321 | break; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | return mod; |
| 326 | } |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 327 | |
| 328 | LY_ERR |
| 329 | lyd_parse_check_keys(struct lyd_node *node) |
| 330 | { |
| 331 | const struct lysc_node *skey = NULL; |
| 332 | const struct lyd_node *key; |
| 333 | |
| 334 | assert(node->schema->nodetype == LYS_LIST); |
| 335 | |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 336 | key = lyd_child(node); |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 337 | while ((skey = lys_getnext(skey, node->schema, NULL, 0)) && (skey->flags & LYS_KEY)) { |
| 338 | if (!key || (key->schema != skey)) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 339 | LOGVAL(LYD_CTX(node), LY_VCODE_NOKEY, skey->name); |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 340 | return LY_EVALID; |
| 341 | } |
| 342 | |
| 343 | key = key->next; |
| 344 | } |
| 345 | |
| 346 | return LY_SUCCESS; |
| 347 | } |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 348 | |
| 349 | void |
Michal Vasko | f9b6834 | 2021-07-23 13:50:59 +0200 | [diff] [blame] | 350 | lyd_parse_set_data_flags(struct lyd_node *node, struct ly_set *node_when, struct ly_set *node_exts, struct lyd_meta **meta, |
| 351 | uint32_t parse_opts) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 352 | { |
| 353 | struct lyd_meta *meta2, *prev_meta = NULL; |
| 354 | |
Michal Vasko | a5705e5 | 2020-12-09 18:15:14 +0100 | [diff] [blame] | 355 | if (lysc_has_when(node->schema)) { |
Michal Vasko | f9b6834 | 2021-07-23 13:50:59 +0200 | [diff] [blame] | 356 | if (!(parse_opts & LYD_PARSE_ONLY)) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 357 | /* remember we need to evaluate this node's when */ |
Michal Vasko | f9b6834 | 2021-07-23 13:50:59 +0200 | [diff] [blame] | 358 | LY_CHECK_RET(ly_set_add(node_when, node, 1, NULL), ); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 359 | } |
| 360 | } |
Michal Vasko | f9b6834 | 2021-07-23 13:50:59 +0200 | [diff] [blame] | 361 | LY_CHECK_RET(lysc_node_ext_tovalidate(node_exts, node), ); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 362 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 363 | LY_LIST_FOR(*meta, meta2) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 364 | if (!strcmp(meta2->name, "default") && !strcmp(meta2->annotation->module->name, "ietf-netconf-with-defaults") && |
| 365 | meta2->value.boolean) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 366 | /* node is default according to the metadata */ |
| 367 | node->flags |= LYD_DEFAULT; |
| 368 | |
| 369 | /* delete the metadata */ |
| 370 | if (prev_meta) { |
| 371 | prev_meta->next = meta2->next; |
| 372 | } else { |
| 373 | *meta = (*meta)->next; |
| 374 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 375 | lyd_free_meta_single(meta2); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 376 | break; |
| 377 | } |
| 378 | |
| 379 | prev_meta = meta2; |
| 380 | } |
| 381 | } |
| 382 | |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 383 | API const char * |
Christian Hopps | 46bd21b | 2021-04-27 09:43:58 -0400 | [diff] [blame] | 384 | 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] | 385 | { |
Michal Vasko | ab40e7e | 2021-04-28 17:04:24 +0200 | [diff] [blame] | 386 | LY_CHECK_ARG_RET(ctx, ctx, value, NULL); |
| 387 | |
Michal Vasko | 3387602 | 2021-04-27 16:42:24 +0200 | [diff] [blame] | 388 | return value->_canonical ? value->_canonical : |
| 389 | (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] | 390 | } |
| 391 | |
Michal Vasko | c000427 | 2020-08-06 08:32:34 +0200 | [diff] [blame] | 392 | API LY_ERR |
Michal Vasko | a820c31 | 2021-02-05 16:33:00 +0100 | [diff] [blame] | 393 | lyd_any_value_str(const struct lyd_node *any, char **value_str) |
| 394 | { |
| 395 | const struct lyd_node_any *a; |
| 396 | struct lyd_node *tree = NULL; |
| 397 | const char *str = NULL; |
| 398 | ly_bool dynamic = 0; |
| 399 | LY_ERR ret = LY_SUCCESS; |
| 400 | |
| 401 | LY_CHECK_ARG_RET(NULL, any, value_str, LY_EINVAL); |
Radek Krejci | 71877df | 2021-04-06 17:24:06 +0200 | [diff] [blame] | 402 | 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] | 403 | |
| 404 | a = (struct lyd_node_any *)any; |
| 405 | *value_str = NULL; |
| 406 | |
| 407 | if (!a->value.str) { |
| 408 | /* there is no value in the union */ |
| 409 | return LY_SUCCESS; |
| 410 | } |
| 411 | |
| 412 | switch (a->value_type) { |
| 413 | case LYD_ANYDATA_LYB: |
| 414 | /* parse into a data tree */ |
| 415 | ret = lyd_parse_data_mem(LYD_CTX(any), a->value.mem, LYD_LYB, LYD_PARSE_ONLY, 0, &tree); |
| 416 | LY_CHECK_GOTO(ret, cleanup); |
| 417 | dynamic = 1; |
| 418 | break; |
| 419 | case LYD_ANYDATA_DATATREE: |
| 420 | tree = a->value.tree; |
| 421 | break; |
| 422 | case LYD_ANYDATA_STRING: |
| 423 | case LYD_ANYDATA_XML: |
| 424 | case LYD_ANYDATA_JSON: |
| 425 | /* simply use the string */ |
| 426 | str = a->value.str; |
| 427 | break; |
| 428 | } |
| 429 | |
| 430 | if (tree) { |
| 431 | /* print into a string */ |
| 432 | ret = lyd_print_mem(value_str, tree, LYD_XML, LYD_PRINT_WITHSIBLINGS); |
| 433 | LY_CHECK_GOTO(ret, cleanup); |
| 434 | } else { |
| 435 | assert(str); |
| 436 | *value_str = strdup(str); |
| 437 | LY_CHECK_ERR_GOTO(!*value_str, LOGMEM(LYD_CTX(any)), cleanup); |
| 438 | } |
| 439 | |
| 440 | /* success */ |
| 441 | |
| 442 | cleanup: |
| 443 | if (dynamic) { |
| 444 | lyd_free_all(tree); |
| 445 | } |
| 446 | return ret; |
| 447 | } |
| 448 | |
| 449 | API LY_ERR |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 450 | lyd_any_copy_value(struct lyd_node *trg, const union lyd_any_value *value, LYD_ANYDATA_VALUETYPE value_type) |
| 451 | { |
| 452 | struct lyd_node_any *t; |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 453 | |
Michal Vasko | a820c31 | 2021-02-05 16:33:00 +0100 | [diff] [blame] | 454 | LY_CHECK_ARG_RET(NULL, trg, LY_EINVAL); |
Radek Krejci | 71877df | 2021-04-06 17:24:06 +0200 | [diff] [blame] | 455 | 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] | 456 | |
| 457 | t = (struct lyd_node_any *)trg; |
| 458 | |
| 459 | /* free trg */ |
| 460 | switch (t->value_type) { |
| 461 | case LYD_ANYDATA_DATATREE: |
| 462 | lyd_free_all(t->value.tree); |
| 463 | break; |
| 464 | case LYD_ANYDATA_STRING: |
| 465 | case LYD_ANYDATA_XML: |
| 466 | case LYD_ANYDATA_JSON: |
Michal Vasko | e180ed0 | 2021-02-05 16:31:20 +0100 | [diff] [blame] | 467 | lydict_remove(LYD_CTX(trg), t->value.str); |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 468 | break; |
| 469 | case LYD_ANYDATA_LYB: |
| 470 | free(t->value.mem); |
| 471 | break; |
| 472 | } |
| 473 | t->value.str = NULL; |
| 474 | |
| 475 | if (!value) { |
| 476 | /* only free value in this case */ |
| 477 | return LY_SUCCESS; |
| 478 | } |
| 479 | |
| 480 | /* copy src */ |
| 481 | t->value_type = value_type; |
| 482 | switch (value_type) { |
| 483 | case LYD_ANYDATA_DATATREE: |
| 484 | if (value->tree) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 485 | 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] | 486 | } |
| 487 | break; |
| 488 | case LYD_ANYDATA_STRING: |
| 489 | case LYD_ANYDATA_XML: |
| 490 | case LYD_ANYDATA_JSON: |
| 491 | if (value->str) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 492 | 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] | 493 | } |
| 494 | break; |
| 495 | case LYD_ANYDATA_LYB: |
| 496 | if (value->mem) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 497 | int len = lyd_lyb_data_length(value->mem); |
Radek Krejci | 82fa8d4 | 2020-07-11 22:00:59 +0200 | [diff] [blame] | 498 | LY_CHECK_RET(len == -1, LY_EINVAL); |
Michal Vasko | 61551fa | 2020-07-09 15:45:45 +0200 | [diff] [blame] | 499 | t->value.mem = malloc(len); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 500 | 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] | 501 | memcpy(t->value.mem, value->mem, len); |
| 502 | } |
| 503 | break; |
| 504 | } |
| 505 | |
| 506 | return LY_SUCCESS; |
| 507 | } |
| 508 | |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 509 | void |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 510 | lyd_del_move_root(struct lyd_node **root, const struct lyd_node *to_del, const struct lys_module *mod) |
| 511 | { |
| 512 | if (*root && (lyd_owner_module(*root) != mod)) { |
| 513 | /* there are no data of mod so this is simply the first top-level sibling */ |
| 514 | mod = NULL; |
| 515 | } |
| 516 | |
| 517 | if ((*root != to_del) || (*root)->parent) { |
| 518 | return; |
| 519 | } |
| 520 | |
Michal Vasko | 598063b | 2021-07-19 11:39:05 +0200 | [diff] [blame] | 521 | if (mod && (*root)->prev->next && (!(*root)->next || (lyd_owner_module(to_del) != lyd_owner_module((*root)->next)))) { |
| 522 | /* 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] | 523 | *root = lyd_first_sibling(*root); |
Michal Vasko | 598063b | 2021-07-19 11:39:05 +0200 | [diff] [blame] | 524 | } else { |
| 525 | *root = (*root)->next; |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | |
| 529 | void |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 530 | ly_free_prefix_data(LY_VALUE_FORMAT format, void *prefix_data) |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 531 | { |
| 532 | struct ly_set *ns_list; |
| 533 | struct lysc_prefix *prefixes; |
| 534 | uint32_t i; |
| 535 | LY_ARRAY_COUNT_TYPE u; |
| 536 | |
| 537 | if (!prefix_data) { |
| 538 | return; |
| 539 | } |
| 540 | |
| 541 | switch (format) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 542 | case LY_VALUE_XML: |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 543 | ns_list = prefix_data; |
| 544 | for (i = 0; i < ns_list->count; ++i) { |
| 545 | free(((struct lyxml_ns *)ns_list->objs[i])->prefix); |
| 546 | free(((struct lyxml_ns *)ns_list->objs[i])->uri); |
| 547 | } |
| 548 | ly_set_free(ns_list, free); |
| 549 | break; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 550 | case LY_VALUE_SCHEMA_RESOLVED: |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 551 | prefixes = prefix_data; |
| 552 | LY_ARRAY_FOR(prefixes, u) { |
| 553 | free(prefixes[u].prefix); |
| 554 | } |
| 555 | LY_ARRAY_FREE(prefixes); |
| 556 | break; |
Radek Krejci | 224d4b4 | 2021-04-23 13:54:59 +0200 | [diff] [blame] | 557 | case LY_VALUE_CANON: |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 558 | case LY_VALUE_SCHEMA: |
| 559 | case LY_VALUE_JSON: |
Radek Krejci | f994364 | 2021-04-26 10:18:21 +0200 | [diff] [blame] | 560 | case LY_VALUE_LYB: |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 561 | break; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | LY_ERR |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 566 | 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] | 567 | void **prefix_data_p) |
| 568 | { |
| 569 | LY_ERR ret = LY_SUCCESS; |
| 570 | struct lyxml_ns *ns; |
| 571 | struct lysc_prefix *prefixes = NULL, *orig_pref; |
| 572 | struct ly_set *ns_list, *orig_ns; |
| 573 | uint32_t i; |
| 574 | LY_ARRAY_COUNT_TYPE u; |
| 575 | |
| 576 | assert(!*prefix_data_p); |
| 577 | |
| 578 | switch (format) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 579 | case LY_VALUE_SCHEMA: |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 580 | *prefix_data_p = (void *)prefix_data; |
| 581 | break; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 582 | case LY_VALUE_SCHEMA_RESOLVED: |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 583 | /* copy all the value prefixes */ |
| 584 | orig_pref = (struct lysc_prefix *)prefix_data; |
| 585 | LY_ARRAY_CREATE_GOTO(ctx, prefixes, LY_ARRAY_COUNT(orig_pref), ret, cleanup); |
| 586 | *prefix_data_p = prefixes; |
| 587 | |
| 588 | LY_ARRAY_FOR(orig_pref, u) { |
| 589 | if (orig_pref[u].prefix) { |
| 590 | prefixes[u].prefix = strdup(orig_pref[u].prefix); |
| 591 | LY_CHECK_ERR_GOTO(!prefixes[u].prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 592 | } |
| 593 | prefixes[u].mod = orig_pref[u].mod; |
| 594 | LY_ARRAY_INCREMENT(prefixes); |
| 595 | } |
| 596 | break; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 597 | case LY_VALUE_XML: |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 598 | /* copy all the namespaces */ |
| 599 | LY_CHECK_GOTO(ret = ly_set_new(&ns_list), cleanup); |
| 600 | *prefix_data_p = ns_list; |
| 601 | |
| 602 | orig_ns = (struct ly_set *)prefix_data; |
| 603 | for (i = 0; i < orig_ns->count; ++i) { |
| 604 | ns = calloc(1, sizeof *ns); |
| 605 | LY_CHECK_ERR_GOTO(!ns, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 606 | LY_CHECK_GOTO(ret = ly_set_add(ns_list, ns, 1, NULL), cleanup); |
| 607 | |
| 608 | if (((struct lyxml_ns *)orig_ns->objs[i])->prefix) { |
| 609 | ns->prefix = strdup(((struct lyxml_ns *)orig_ns->objs[i])->prefix); |
| 610 | LY_CHECK_ERR_GOTO(!ns->prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 611 | } |
| 612 | ns->uri = strdup(((struct lyxml_ns *)orig_ns->objs[i])->uri); |
| 613 | LY_CHECK_ERR_GOTO(!ns->uri, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 614 | } |
| 615 | break; |
Radek Krejci | 224d4b4 | 2021-04-23 13:54:59 +0200 | [diff] [blame] | 616 | case LY_VALUE_CANON: |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 617 | case LY_VALUE_JSON: |
Radek Krejci | f994364 | 2021-04-26 10:18:21 +0200 | [diff] [blame] | 618 | case LY_VALUE_LYB: |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 619 | assert(!prefix_data); |
| 620 | *prefix_data_p = NULL; |
| 621 | break; |
| 622 | } |
| 623 | |
| 624 | cleanup: |
| 625 | if (ret) { |
| 626 | ly_free_prefix_data(format, *prefix_data_p); |
| 627 | *prefix_data_p = NULL; |
| 628 | } |
| 629 | return ret; |
| 630 | } |
| 631 | |
| 632 | LY_ERR |
Radek Krejci | f994364 | 2021-04-26 10:18:21 +0200 | [diff] [blame] | 633 | 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] | 634 | 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] | 635 | { |
| 636 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 637 | const struct lys_module *mod; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 638 | const struct lyxml_ns *ns; |
| 639 | struct lyxml_ns *new_ns; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 640 | struct ly_set *ns_list; |
| 641 | struct lysc_prefix *prefixes = NULL, *val_pref; |
aPiecek | 83436bc | 2021-03-30 12:20:45 +0200 | [diff] [blame] | 642 | const char *value_iter, *value_next, *value_end; |
| 643 | uint32_t substr_len; |
| 644 | ly_bool is_prefix; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 645 | |
| 646 | switch (format) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 647 | case LY_VALUE_SCHEMA: |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 648 | /* copy all referenced modules as prefix - module pairs */ |
| 649 | if (!*prefix_data_p) { |
| 650 | /* new prefix data */ |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 651 | LY_ARRAY_CREATE_GOTO(ctx, prefixes, 0, ret, cleanup); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 652 | *format_p = LY_VALUE_SCHEMA_RESOLVED; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 653 | *prefix_data_p = prefixes; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 654 | } else { |
| 655 | /* reuse prefix data */ |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 656 | assert(*format_p == LY_VALUE_SCHEMA_RESOLVED); |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 657 | prefixes = *prefix_data_p; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | /* add all used prefixes */ |
Michal Vasko | 59e90fc | 2021-09-22 12:17:08 +0200 | [diff] [blame] | 661 | value_end = (char *)value + value_len; |
aPiecek | 83436bc | 2021-03-30 12:20:45 +0200 | [diff] [blame] | 662 | for (value_iter = value; value_iter; value_iter = value_next) { |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 663 | 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] | 664 | if (is_prefix) { |
| 665 | /* we have a possible prefix. Do we already have the prefix? */ |
| 666 | mod = ly_resolve_prefix(ctx, value_iter, substr_len, *format_p, *prefix_data_p); |
| 667 | if (!mod) { |
| 668 | mod = ly_resolve_prefix(ctx, value_iter, substr_len, format, prefix_data); |
| 669 | if (mod) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 670 | assert(*format_p == LY_VALUE_SCHEMA_RESOLVED); |
aPiecek | 83436bc | 2021-03-30 12:20:45 +0200 | [diff] [blame] | 671 | /* store a new prefix - module pair */ |
| 672 | LY_ARRAY_NEW_GOTO(ctx, prefixes, val_pref, ret, cleanup); |
| 673 | *prefix_data_p = prefixes; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 674 | |
aPiecek | 83436bc | 2021-03-30 12:20:45 +0200 | [diff] [blame] | 675 | val_pref->prefix = strndup(value_iter, substr_len); |
| 676 | LY_CHECK_ERR_GOTO(!val_pref->prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 677 | val_pref->mod = mod; |
| 678 | } /* else it is not even defined */ |
| 679 | } /* else the prefix is already present */ |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 680 | } |
| 681 | } |
| 682 | break; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 683 | case LY_VALUE_XML: |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 684 | /* copy all referenced namespaces as prefix - namespace pairs */ |
| 685 | if (!*prefix_data_p) { |
| 686 | /* new prefix data */ |
| 687 | LY_CHECK_GOTO(ret = ly_set_new(&ns_list), cleanup); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 688 | *format_p = LY_VALUE_XML; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 689 | *prefix_data_p = ns_list; |
| 690 | } else { |
| 691 | /* reuse prefix data */ |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 692 | assert(*format_p == LY_VALUE_XML); |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 693 | ns_list = *prefix_data_p; |
| 694 | } |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 695 | |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 696 | /* add all used prefixes */ |
Michal Vasko | 59e90fc | 2021-09-22 12:17:08 +0200 | [diff] [blame] | 697 | value_end = (char *)value + value_len; |
aPiecek | 83436bc | 2021-03-30 12:20:45 +0200 | [diff] [blame] | 698 | for (value_iter = value; value_iter; value_iter = value_next) { |
aPiecek | e3f828d | 2021-05-10 15:34:41 +0200 | [diff] [blame] | 699 | 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] | 700 | if (is_prefix) { |
| 701 | /* we have a possible prefix. Do we already have the prefix? */ |
| 702 | ns = lyxml_ns_get(ns_list, value_iter, substr_len); |
| 703 | if (!ns) { |
| 704 | ns = lyxml_ns_get(prefix_data, value_iter, substr_len); |
| 705 | if (ns) { |
| 706 | /* store a new prefix - namespace pair */ |
| 707 | new_ns = calloc(1, sizeof *new_ns); |
| 708 | LY_CHECK_ERR_GOTO(!new_ns, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 709 | 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] | 710 | |
aPiecek | 83436bc | 2021-03-30 12:20:45 +0200 | [diff] [blame] | 711 | new_ns->prefix = strndup(value_iter, substr_len); |
| 712 | LY_CHECK_ERR_GOTO(!new_ns->prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 713 | new_ns->uri = strdup(ns->uri); |
| 714 | LY_CHECK_ERR_GOTO(!new_ns->uri, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 715 | } /* else it is not even defined */ |
| 716 | } /* else the prefix is already present */ |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 717 | } |
| 718 | } |
| 719 | break; |
Radek Krejci | 224d4b4 | 2021-04-23 13:54:59 +0200 | [diff] [blame] | 720 | case LY_VALUE_CANON: |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 721 | case LY_VALUE_SCHEMA_RESOLVED: |
| 722 | case LY_VALUE_JSON: |
Radek Krejci | f994364 | 2021-04-26 10:18:21 +0200 | [diff] [blame] | 723 | case LY_VALUE_LYB: |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 724 | if (!*prefix_data_p) { |
| 725 | /* new prefix data - simply copy all the prefix data */ |
| 726 | *format_p = format; |
| 727 | LY_CHECK_GOTO(ret = ly_dup_prefix_data(ctx, format, prefix_data, prefix_data_p), cleanup); |
| 728 | } /* 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] | 729 | break; |
| 730 | } |
| 731 | |
| 732 | cleanup: |
| 733 | if (ret) { |
| 734 | ly_free_prefix_data(*format_p, *prefix_data_p); |
| 735 | *prefix_data_p = NULL; |
| 736 | } |
| 737 | return ret; |
| 738 | } |
Michal Vasko | 7ed1fcb | 2020-12-03 14:15:22 +0100 | [diff] [blame] | 739 | |
| 740 | const char * |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 741 | ly_format2str(LY_VALUE_FORMAT format) |
Michal Vasko | 7ed1fcb | 2020-12-03 14:15:22 +0100 | [diff] [blame] | 742 | { |
| 743 | switch (format) { |
Radek Krejci | 224d4b4 | 2021-04-23 13:54:59 +0200 | [diff] [blame] | 744 | case LY_VALUE_CANON: |
| 745 | return "canonical"; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 746 | case LY_VALUE_SCHEMA: |
Michal Vasko | 7ed1fcb | 2020-12-03 14:15:22 +0100 | [diff] [blame] | 747 | return "schema imports"; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 748 | case LY_VALUE_SCHEMA_RESOLVED: |
Michal Vasko | 7ed1fcb | 2020-12-03 14:15:22 +0100 | [diff] [blame] | 749 | return "schema stored mapping"; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 750 | case LY_VALUE_XML: |
Michal Vasko | 7ed1fcb | 2020-12-03 14:15:22 +0100 | [diff] [blame] | 751 | return "XML prefixes"; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 752 | case LY_VALUE_JSON: |
Michal Vasko | 7ed1fcb | 2020-12-03 14:15:22 +0100 | [diff] [blame] | 753 | return "JSON module names"; |
Radek Krejci | f994364 | 2021-04-26 10:18:21 +0200 | [diff] [blame] | 754 | case LY_VALUE_LYB: |
| 755 | return "LYB prefixes"; |
Michal Vasko | 7ed1fcb | 2020-12-03 14:15:22 +0100 | [diff] [blame] | 756 | default: |
| 757 | break; |
| 758 | } |
| 759 | |
| 760 | return NULL; |
| 761 | } |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 762 | |
| 763 | API LY_ERR |
| 764 | ly_time_str2time(const char *value, time_t *time, char **fractions_s) |
| 765 | { |
| 766 | struct tm tm = {0}; |
| 767 | uint32_t i, frac_len; |
| 768 | const char *frac; |
| 769 | int64_t shift, shift_m; |
| 770 | time_t t; |
| 771 | |
| 772 | LY_CHECK_ARG_RET(NULL, value, time, LY_EINVAL); |
| 773 | |
| 774 | tm.tm_year = atoi(&value[0]) - 1900; |
| 775 | tm.tm_mon = atoi(&value[5]) - 1; |
| 776 | tm.tm_mday = atoi(&value[8]); |
| 777 | tm.tm_hour = atoi(&value[11]); |
| 778 | tm.tm_min = atoi(&value[14]); |
| 779 | tm.tm_sec = atoi(&value[17]); |
| 780 | |
| 781 | t = timegm(&tm); |
| 782 | i = 19; |
| 783 | |
| 784 | /* fractions of a second */ |
| 785 | if (value[i] == '.') { |
| 786 | ++i; |
| 787 | frac = &value[i]; |
| 788 | for (frac_len = 0; isdigit(frac[frac_len]); ++frac_len) {} |
| 789 | |
| 790 | i += frac_len; |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 791 | } else { |
| 792 | frac = NULL; |
| 793 | } |
| 794 | |
| 795 | /* apply offset */ |
| 796 | if ((value[i] == 'Z') || (value[i] == 'z')) { |
| 797 | /* zero shift */ |
| 798 | shift = 0; |
| 799 | } else { |
| 800 | shift = strtol(&value[i], NULL, 10); |
| 801 | shift = shift * 60 * 60; /* convert from hours to seconds */ |
| 802 | shift_m = strtol(&value[i + 4], NULL, 10) * 60; /* includes conversion from minutes to seconds */ |
| 803 | /* correct sign */ |
| 804 | if (shift < 0) { |
| 805 | shift_m *= -1; |
| 806 | } |
| 807 | /* connect hours and minutes of the shift */ |
| 808 | shift = shift + shift_m; |
| 809 | } |
| 810 | |
| 811 | /* we have to shift to the opposite way to correct the time */ |
| 812 | t -= shift; |
| 813 | |
| 814 | *time = t; |
| 815 | if (fractions_s) { |
| 816 | if (frac) { |
| 817 | *fractions_s = strndup(frac, frac_len); |
| 818 | LY_CHECK_RET(!*fractions_s, LY_EMEM); |
| 819 | } else { |
| 820 | *fractions_s = NULL; |
| 821 | } |
| 822 | } |
| 823 | return LY_SUCCESS; |
| 824 | } |
| 825 | |
| 826 | API LY_ERR |
| 827 | ly_time_time2str(time_t time, const char *fractions_s, char **str) |
| 828 | { |
| 829 | struct tm tm; |
Michal Vasko | 143ffa8 | 2021-05-20 11:11:39 +0200 | [diff] [blame] | 830 | char zoneshift[8]; |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 831 | int32_t zonediff_h, zonediff_m; |
| 832 | |
| 833 | LY_CHECK_ARG_RET(NULL, str, LY_EINVAL); |
| 834 | |
| 835 | /* initialize the local timezone */ |
| 836 | tzset(); |
| 837 | |
| 838 | /* convert */ |
| 839 | if (!localtime_r(&time, &tm)) { |
| 840 | return LY_ESYS; |
| 841 | } |
| 842 | |
| 843 | /* get timezone offset */ |
| 844 | if (tm.tm_gmtoff == 0) { |
| 845 | /* time is Zulu (UTC) */ |
| 846 | zonediff_h = 0; |
| 847 | zonediff_m = 0; |
| 848 | } else { |
| 849 | /* timezone offset */ |
| 850 | zonediff_h = tm.tm_gmtoff / 60 / 60; |
| 851 | zonediff_m = tm.tm_gmtoff / 60 % 60; |
| 852 | } |
| 853 | sprintf(zoneshift, "%+03d:%02d", zonediff_h, zonediff_m); |
| 854 | |
| 855 | /* print */ |
| 856 | if (asprintf(str, "%04d-%02d-%02dT%02d:%02d:%02d%s%s%s", |
| 857 | tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, |
| 858 | fractions_s ? "." : "", fractions_s ? fractions_s : "", zoneshift) == -1) { |
| 859 | return LY_EMEM; |
| 860 | } |
| 861 | |
| 862 | return LY_SUCCESS; |
| 863 | } |
| 864 | |
| 865 | API LY_ERR |
| 866 | ly_time_str2ts(const char *value, struct timespec *ts) |
| 867 | { |
| 868 | LY_ERR rc; |
Michal Vasko | 7297506 | 2021-08-25 08:13:04 +0200 | [diff] [blame] | 869 | char *fractions_s, frac_buf[10]; |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 870 | int frac_len; |
| 871 | |
| 872 | LY_CHECK_ARG_RET(NULL, value, ts, LY_EINVAL); |
| 873 | |
| 874 | rc = ly_time_str2time(value, &ts->tv_sec, &fractions_s); |
| 875 | LY_CHECK_RET(rc); |
| 876 | |
| 877 | /* convert fractions of a second to nanoseconds */ |
| 878 | if (fractions_s) { |
Michal Vasko | 7297506 | 2021-08-25 08:13:04 +0200 | [diff] [blame] | 879 | /* init frac_buf with zeroes */ |
| 880 | memset(frac_buf, '0', 9); |
| 881 | frac_buf[9] = '\0'; |
| 882 | |
Michal Vasko | 43297a0 | 2021-05-19 11:12:37 +0200 | [diff] [blame] | 883 | frac_len = strlen(fractions_s); |
| 884 | memcpy(frac_buf, fractions_s, frac_len > 9 ? 9 : frac_len); |
| 885 | ts->tv_nsec = atol(frac_buf); |
| 886 | free(fractions_s); |
| 887 | } else { |
| 888 | ts->tv_nsec = 0; |
| 889 | } |
| 890 | |
| 891 | return LY_SUCCESS; |
| 892 | } |
| 893 | |
| 894 | API LY_ERR |
| 895 | ly_time_ts2str(const struct timespec *ts, char **str) |
| 896 | { |
| 897 | char frac_buf[10]; |
| 898 | |
Jan Kundrát | bd15700 | 2021-08-30 14:02:22 +0200 | [diff] [blame] | 899 | 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] | 900 | |
| 901 | /* convert nanoseconds to fractions of a second */ |
| 902 | if (ts->tv_nsec) { |
| 903 | sprintf(frac_buf, "%09ld", ts->tv_nsec); |
| 904 | } |
| 905 | |
| 906 | return ly_time_time2str(ts->tv_sec, ts->tv_nsec ? frac_buf : NULL, str); |
| 907 | } |