blob: f2a37a2e47a7279f290bfd4d1d26c07412ec64cb [file] [log] [blame]
Radek Krejcie7b95092019-05-15 11:03:07 +02001/**
Michal Vasko59892dd2022-05-13 11:02:30 +02002 * @file tree_data_common.c
Radek Krejcie7b95092019-05-15 11:03:07 +02003 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko59892dd2022-05-13 11:02:30 +02004 * @author Michal Vasko <mvasko@cesnet.cz>
5 * @brief Parsing and validation common functions for data trees
Radek Krejcie7b95092019-05-15 11:03:07 +02006 *
Michal Vasko8cc3f662022-03-29 11:25:51 +02007 * Copyright (c) 2015 - 2022 CESNET, z.s.p.o.
Radek Krejcie7b95092019-05-15 11:03:07 +02008 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
Michal Vasko43297a02021-05-19 11:12:37 +020015
16#define _GNU_SOURCE /* asprintf, strdup */
Radek Krejcie7b95092019-05-15 11:03:07 +020017
18#include <assert.h>
Michal Vasko43297a02021-05-19 11:12:37 +020019#include <ctype.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020020#include <stdint.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020021#include <stdlib.h>
Radek Krejciad97c5f2020-06-30 09:19:28 +020022#include <string.h>
Michal Vasko43297a02021-05-19 11:12:37 +020023#include <time.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020024
Radek Krejci535ea9f2020-05-29 16:01:05 +020025#include "common.h"
Michal Vasko6b5cb2a2020-11-11 19:11:21 +010026#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020027#include "context.h"
Radek Krejci47fab892020-11-05 17:02:41 +010028#include "dict.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020029#include "hash_table.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020030#include "log.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020031#include "lyb.h"
Radek Krejci7931b192020-06-25 17:05:03 +020032#include "parser_data.h"
Michal Vasko8cc3f662022-03-29 11:25:51 +020033#include "plugins_exts.h"
Michal Vaskoa820c312021-02-05 16:33:00 +010034#include "printer_data.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020035#include "set.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020036#include "tree.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020037#include "tree_data.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020038#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010039#include "tree_edit.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020040#include "tree_schema.h"
Radek Krejci0aa1f702021-04-01 16:16:19 +020041#include "tree_schema_internal.h"
Radek Krejci4f2e3e52021-03-30 14:20:28 +020042#include "validation.h"
Radek Krejci77114102021-03-10 15:21:57 +010043#include "xml.h"
aPiecekdf23eee2021-10-07 12:21:50 +020044#include "xpath.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020045
Michal Vaskod7c048c2021-05-18 16:12:55 +020046/**
Michal Vasko271d2e32023-01-31 15:43:19 +010047 * @brief Callback for checking first instance hash table values equivalence.
48 *
49 * @param[in] val1_p If not @p mod, pointer to the first instance.
50 * @param[in] val2_p If not @p mod, pointer to the found dup inst item.
51 */
52static ly_bool
53lyht_dup_inst_ht_equal_cb(void *val1_p, void *val2_p, ly_bool mod, void *UNUSED(cb_data))
54{
55 if (mod) {
56 struct lyd_dup_inst **item1 = val1_p, **item2 = val2_p;
57
58 /* equal on 2 dup inst items */
59 return *item1 == *item2 ? 1 : 0;
60 } else {
61 struct lyd_node **first_inst = val1_p;
62 struct lyd_dup_inst **item = val2_p;
63
64 /* equal on dup inst item and a first instance */
65 return (*item)->set->dnodes[0] == *first_inst ? 1 : 0;
66 }
67}
68
69/**
Michal Vaskod7c048c2021-05-18 16:12:55 +020070 * @brief Find an entry in duplicate instance cache for an instance. Create it if it does not exist.
71 *
Michal Vasko271d2e32023-01-31 15:43:19 +010072 * @param[in] first_inst First instance of the cache entry.
73 * @param[in] dup_inst_ht Duplicate instance cache hash table.
Michal Vaskod7c048c2021-05-18 16:12:55 +020074 * @return Instance cache entry.
75 */
76static struct lyd_dup_inst *
Michal Vasko8efac242023-03-30 08:24:56 +020077lyd_dup_inst_get(const struct lyd_node *first_inst, struct ly_ht **dup_inst_ht)
Michal Vaskod7c048c2021-05-18 16:12:55 +020078{
Michal Vasko271d2e32023-01-31 15:43:19 +010079 struct lyd_dup_inst **item_p, *item;
Michal Vaskod7c048c2021-05-18 16:12:55 +020080
Michal Vasko271d2e32023-01-31 15:43:19 +010081 if (*dup_inst_ht) {
82 /* find the item of the first instance */
83 if (!lyht_find(*dup_inst_ht, &first_inst, first_inst->hash, (void **)&item_p)) {
84 return *item_p;
Michal Vaskod7c048c2021-05-18 16:12:55 +020085 }
Michal Vasko271d2e32023-01-31 15:43:19 +010086 } else {
87 /* create the hash table */
88 *dup_inst_ht = lyht_new(2, sizeof item, lyht_dup_inst_ht_equal_cb, NULL, 1);
89 LY_CHECK_RET(!*dup_inst_ht, NULL);
Michal Vaskod7c048c2021-05-18 16:12:55 +020090 }
91
Michal Vasko271d2e32023-01-31 15:43:19 +010092 /* first instance has no dup inst item, create it */
93 item = calloc(1, sizeof *item);
94 LY_CHECK_RET(!item, NULL);
95
96 /* add into the hash table */
97 if (lyht_insert(*dup_inst_ht, &item, first_inst->hash, NULL)) {
98 return NULL;
99 }
Michal Vaskod7c048c2021-05-18 16:12:55 +0200100
101 return item;
102}
103
104LY_ERR
Michal Vasko8efac242023-03-30 08:24:56 +0200105lyd_dup_inst_next(struct lyd_node **inst, const struct lyd_node *siblings, struct ly_ht **dup_inst_ht)
Michal Vaskod7c048c2021-05-18 16:12:55 +0200106{
107 struct lyd_dup_inst *dup_inst;
108
Michal Vasko83ae7772022-06-08 10:01:55 +0200109 if (!*inst) {
110 /* no match, inst is unchanged */
Michal Vaskod7c048c2021-05-18 16:12:55 +0200111 return LY_SUCCESS;
112 }
113
Michal Vasko83ae7772022-06-08 10:01:55 +0200114 /* there can be more exact same instances (even if not allowed in invalid data) and we must make sure we do not
115 * match a single node more times */
Michal Vasko271d2e32023-01-31 15:43:19 +0100116 dup_inst = lyd_dup_inst_get(*inst, dup_inst_ht);
Michal Vaskod7c048c2021-05-18 16:12:55 +0200117 LY_CHECK_ERR_RET(!dup_inst, LOGMEM(LYD_CTX(siblings)), LY_EMEM);
118
119 if (!dup_inst->used) {
120 /* we did not cache these instances yet, do so */
Michal Vasko271d2e32023-01-31 15:43:19 +0100121 lyd_find_sibling_dup_inst_set(siblings, *inst, &dup_inst->set);
122 assert(dup_inst->set->count && (dup_inst->set->dnodes[0] == *inst));
Michal Vaskod7c048c2021-05-18 16:12:55 +0200123 }
124
Michal Vasko271d2e32023-01-31 15:43:19 +0100125 if (dup_inst->used == dup_inst->set->count) {
Michal Vasko4525e1f2022-07-13 16:20:59 +0200126 if (lysc_is_dup_inst_list((*inst)->schema)) {
127 /* we have used all the instances */
128 *inst = NULL;
129 } /* else just keep using the last (ideally only) instance */
Michal Vaskod7c048c2021-05-18 16:12:55 +0200130 } else {
Michal Vasko271d2e32023-01-31 15:43:19 +0100131 assert(dup_inst->used < dup_inst->set->count);
Michal Vaskod7c048c2021-05-18 16:12:55 +0200132
133 /* use another instance */
Michal Vasko271d2e32023-01-31 15:43:19 +0100134 *inst = dup_inst->set->dnodes[dup_inst->used];
Michal Vaskod7c048c2021-05-18 16:12:55 +0200135 ++dup_inst->used;
136 }
137
138 return LY_SUCCESS;
139}
140
Michal Vasko271d2e32023-01-31 15:43:19 +0100141/**
142 * @brief Callback for freeing first instance hash table values.
143 */
144static void
145lyht_dup_inst_ht_free_cb(void *val_p)
Michal Vaskod7c048c2021-05-18 16:12:55 +0200146{
Michal Vasko271d2e32023-01-31 15:43:19 +0100147 struct lyd_dup_inst **item = val_p;
Michal Vaskod7c048c2021-05-18 16:12:55 +0200148
Michal Vasko271d2e32023-01-31 15:43:19 +0100149 ly_set_free((*item)->set, NULL);
150 free(*item);
151}
152
153void
Michal Vasko8efac242023-03-30 08:24:56 +0200154lyd_dup_inst_free(struct ly_ht *dup_inst_ht)
Michal Vasko271d2e32023-01-31 15:43:19 +0100155{
156 lyht_free(dup_inst_ht, lyht_dup_inst_ht_free_cb);
Michal Vaskod7c048c2021-05-18 16:12:55 +0200157}
158
Michal Vaskoa6669ba2020-08-06 16:14:26 +0200159struct lyd_node *
160lys_getnext_data(const struct lyd_node *last, const struct lyd_node *sibling, const struct lysc_node **slast,
Radek Krejci0f969882020-08-21 16:56:47 +0200161 const struct lysc_node *parent, const struct lysc_module *module)
Michal Vaskoa6669ba2020-08-06 16:14:26 +0200162{
163 const struct lysc_node *siter = NULL;
164 struct lyd_node *match = NULL;
165
166 assert(parent || module);
167 assert(!last || (slast && *slast));
168
169 if (slast) {
170 siter = *slast;
171 }
172
173 if (last && last->next && (last->next->schema == siter)) {
174 /* return next data instance */
175 return last->next;
176 }
177
178 /* find next schema node data instance */
179 while ((siter = lys_getnext(siter, parent, module, 0))) {
180 if (!lyd_find_sibling_val(sibling, siter, NULL, 0, &match)) {
181 break;
182 }
183 }
184
185 if (slast) {
186 *slast = siter;
187 }
188 return match;
189}
190
Radek Krejcie7b95092019-05-15 11:03:07 +0200191struct lyd_node **
Michal Vaskoe0665742021-02-11 11:08:44 +0100192lyd_node_child_p(struct lyd_node *node)
Radek Krejcie7b95092019-05-15 11:03:07 +0200193{
194 assert(node);
Michal Vasko52927e22020-03-16 17:26:14 +0100195
196 if (!node->schema) {
197 return &((struct lyd_node_opaq *)node)->child;
198 } else {
199 switch (node->schema->nodetype) {
200 case LYS_CONTAINER:
201 case LYS_LIST:
Michal Vasko1bf09392020-03-27 12:38:10 +0100202 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +0100203 case LYS_ACTION:
204 case LYS_NOTIF:
205 return &((struct lyd_node_inner *)node)->child;
206 default:
207 return NULL;
208 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200209 }
210}
211
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100212LIBYANG_API_DEF LY_ERR
aPiecekdf23eee2021-10-07 12:21:50 +0200213lyxp_vars_set(struct lyxp_var **vars, const char *name, const char *value)
214{
215 LY_ERR ret = LY_SUCCESS;
216 char *var_name = NULL, *var_value = NULL;
217 struct lyxp_var *item;
218
219 if (!vars || !name || !value) {
220 return LY_EINVAL;
221 }
222
Michal Vasko90189962023-02-28 12:10:34 +0100223 /* if variable is already defined then change its value */
224 if (*vars && !lyxp_vars_find(NULL, *vars, name, 0, &item)) {
aPiecekdf23eee2021-10-07 12:21:50 +0200225 var_value = strdup(value);
226 LY_CHECK_RET(!var_value, LY_EMEM);
227
Michal Vasko90189962023-02-28 12:10:34 +0100228 /* update value */
aPiecekdf23eee2021-10-07 12:21:50 +0200229 free(item->value);
230 item->value = var_value;
231 } else {
232 var_name = strdup(name);
233 var_value = strdup(value);
234 LY_CHECK_ERR_GOTO(!var_name || !var_value, ret = LY_EMEM, error);
235
Michal Vasko90189962023-02-28 12:10:34 +0100236 /* add new variable */
aPiecekdf23eee2021-10-07 12:21:50 +0200237 LY_ARRAY_NEW_GOTO(NULL, *vars, item, ret, error);
238 item->name = var_name;
239 item->value = var_value;
240 }
241
242 return LY_SUCCESS;
243
244error:
245 free(var_name);
246 free(var_value);
247 return ret;
248}
249
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100250LIBYANG_API_DEF void
aPiecekdf23eee2021-10-07 12:21:50 +0200251lyxp_vars_free(struct lyxp_var *vars)
252{
253 LY_ARRAY_COUNT_TYPE u;
254
255 if (!vars) {
256 return;
257 }
258
259 LY_ARRAY_FOR(vars, u) {
260 free(vars[u].name);
261 free(vars[u].value);
262 }
263
264 LY_ARRAY_FREE(vars);
265}
266
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100267LIBYANG_API_DEF struct lyd_node *
Radek Krejcia1c1e542020-09-29 16:06:52 +0200268lyd_child_no_keys(const struct lyd_node *node)
269{
270 struct lyd_node **children;
271
272 if (!node) {
273 return NULL;
274 }
275
276 if (!node->schema) {
277 /* opaq node */
Michal Vasko9e685082021-01-29 14:49:09 +0100278 return ((struct lyd_node_opaq *)node)->child;
Radek Krejcia1c1e542020-09-29 16:06:52 +0200279 }
280
Michal Vaskoe0665742021-02-11 11:08:44 +0100281 children = lyd_node_child_p((struct lyd_node *)node);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200282 if (children) {
283 struct lyd_node *child = *children;
Michal Vasko26bbb272022-08-02 14:54:33 +0200284
Radek Krejcia1c1e542020-09-29 16:06:52 +0200285 while (child && child->schema && (child->schema->flags & LYS_KEY)) {
286 child = child->next;
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200287 }
288 return child;
Radek Krejcie7b95092019-05-15 11:03:07 +0200289 } else {
290 return NULL;
291 }
292}
Michal Vasko9b368d32020-02-14 13:53:31 +0100293
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100294LIBYANG_API_DEF const struct lys_module *
Michal Vaskoc193ce92020-03-06 11:04:48 +0100295lyd_owner_module(const struct lyd_node *node)
Michal Vasko9b368d32020-02-14 13:53:31 +0100296{
Michal Vaskod5cfa6e2020-11-23 16:56:08 +0100297 const struct lyd_node_opaq *opaq;
Michal Vasko9b368d32020-02-14 13:53:31 +0100298
Michal Vaskod5cfa6e2020-11-23 16:56:08 +0100299 if (!node) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100300 return NULL;
301 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100302
Michal Vaskod5cfa6e2020-11-23 16:56:08 +0100303 if (!node->schema) {
304 opaq = (struct lyd_node_opaq *)node;
305 switch (opaq->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200306 case LY_VALUE_XML:
Michal Vasko09fbb812023-04-13 14:11:06 +0200307 if (opaq->name.module_ns) {
308 return ly_ctx_get_module_implemented_ns(LYD_CTX(node), opaq->name.module_ns);
309 }
310 break;
Radek Krejci8df109d2021-04-23 12:19:08 +0200311 case LY_VALUE_JSON:
Michal Vasko09fbb812023-04-13 14:11:06 +0200312 if (opaq->name.module_name) {
313 return ly_ctx_get_module_implemented(LYD_CTX(node), opaq->name.module_name);
314 }
315 break;
Michal Vaskod5cfa6e2020-11-23 16:56:08 +0100316 default:
317 return NULL;
318 }
Michal Vasko09fbb812023-04-13 14:11:06 +0200319
320 /* try a parent */
321 return lyd_owner_module(lyd_parent(node));
Michal Vaskod5cfa6e2020-11-23 16:56:08 +0100322 }
323
Michal Vaskoef53c812021-10-13 10:21:03 +0200324 return lysc_owner_module(node->schema);
Michal Vasko9b368d32020-02-14 13:53:31 +0100325}
Michal Vaskob1b5c262020-03-05 14:29:47 +0100326
Michal Vasko598063b2021-07-19 11:39:05 +0200327void
328lyd_first_module_sibling(struct lyd_node **node, const struct lys_module *mod)
329{
330 int cmp;
331 struct lyd_node *first;
Michal Vaskoec139eb2022-05-10 10:08:40 +0200332 const struct lys_module *own_mod;
Michal Vasko598063b2021-07-19 11:39:05 +0200333
334 assert(node && mod);
335
336 if (!*node) {
337 return;
338 }
339
340 first = *node;
Michal Vaskoec139eb2022-05-10 10:08:40 +0200341 own_mod = lyd_owner_module(first);
342 cmp = own_mod ? strcmp(own_mod->name, mod->name) : 1;
Michal Vasko598063b2021-07-19 11:39:05 +0200343 if (cmp > 0) {
344 /* there may be some preceding data */
345 while (first->prev->next) {
346 first = first->prev;
347 if (lyd_owner_module(first) == mod) {
348 cmp = 0;
349 break;
350 }
351 }
352 }
353
354 if (cmp == 0) {
355 /* there may be some preceding data belonging to this module */
356 while (first->prev->next) {
357 if (lyd_owner_module(first->prev) != mod) {
358 break;
359 }
360 first = first->prev;
361 }
362 }
363
364 if (cmp < 0) {
365 /* there may be some following data */
366 LY_LIST_FOR(first, first) {
367 if (lyd_owner_module(first) == mod) {
368 cmp = 0;
369 break;
370 }
371 }
372 }
373
374 if (cmp == 0) {
375 /* we have found the first module data node */
376 *node = first;
377 }
378}
379
Michal Vaskob1b5c262020-03-05 14:29:47 +0100380const struct lys_module *
Michal Vasko26e80012020-07-08 10:55:46 +0200381lyd_mod_next_module(struct lyd_node *tree, const struct lys_module *module, const struct ly_ctx *ctx, uint32_t *i,
Radek Krejci0f969882020-08-21 16:56:47 +0200382 struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +0100383{
384 struct lyd_node *iter;
385 const struct lys_module *mod;
386
387 /* get the next module */
Michal Vasko26e80012020-07-08 10:55:46 +0200388 if (module) {
389 if (*i) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100390 mod = NULL;
Michal Vasko26e80012020-07-08 10:55:46 +0200391 } else {
392 mod = module;
393 ++(*i);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100394 }
395 } else {
396 do {
397 mod = ly_ctx_get_module_iter(ctx, i);
398 } while (mod && !mod->implemented);
399 }
400
401 /* find its data */
402 *first = NULL;
403 if (mod) {
404 LY_LIST_FOR(tree, iter) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100405 if (lyd_owner_module(iter) == mod) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100406 *first = iter;
407 break;
408 }
409 }
410 }
411
412 return mod;
413}
414
415const struct lys_module *
416lyd_data_next_module(struct lyd_node **next, struct lyd_node **first)
417{
418 const struct lys_module *mod;
419
420 if (!*next) {
421 /* all data traversed */
422 *first = NULL;
423 return NULL;
424 }
425
426 *first = *next;
427
428 /* prepare next */
Michal Vaskoc193ce92020-03-06 11:04:48 +0100429 mod = lyd_owner_module(*next);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100430 LY_LIST_FOR(*next, *next) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100431 if (lyd_owner_module(*next) != mod) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100432 break;
433 }
434 }
435
436 return mod;
437}
Michal Vasko9f96a052020-03-10 09:41:45 +0100438
439LY_ERR
Michal Vasko59892dd2022-05-13 11:02:30 +0200440lyd_value_store(const struct ly_ctx *ctx, struct lyd_value *val, const struct lysc_type *type, const void *value,
441 size_t value_len, ly_bool *dynamic, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints,
442 const struct lysc_node *ctx_node, ly_bool *incomplete)
443{
444 LY_ERR ret;
445 struct ly_err_item *err = NULL;
446 uint32_t options = (dynamic && *dynamic ? LYPLG_TYPE_STORE_DYNAMIC : 0);
447
448 if (!value) {
449 value = "";
450 }
451 if (incomplete) {
452 *incomplete = 0;
453 }
454
455 ret = type->plugin->store(ctx, type, value, value_len, options, format, prefix_data, hints, ctx_node, val, NULL, &err);
456 if (dynamic) {
457 *dynamic = 0;
458 }
459
460 if (ret == LY_EINCOMPLETE) {
461 if (incomplete) {
462 *incomplete = 1;
463 }
464 } else if (ret) {
465 if (err) {
466 LOGVAL_ERRITEM(ctx, err);
467 ly_err_free(err);
468 } else {
469 LOGVAL(ctx, LYVE_OTHER, "Storing value failed.");
470 }
471 return ret;
472 }
473
474 return LY_SUCCESS;
475}
476
477LY_ERR
478lyd_value_validate_incomplete(const struct ly_ctx *ctx, const struct lysc_type *type, struct lyd_value *val,
479 const struct lyd_node *ctx_node, const struct lyd_node *tree)
480{
481 LY_ERR ret;
482 struct ly_err_item *err = NULL;
483
484 assert(type->plugin->validate);
485
486 ret = type->plugin->validate(ctx, type, ctx_node, tree, val, &err);
487 if (ret) {
488 if (err) {
489 LOGVAL_ERRITEM(ctx, err);
490 ly_err_free(err);
491 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +0200492 LOGVAL(ctx, LYVE_OTHER, "Resolving value \"%s\" failed.",
493 (char *)type->plugin->print(ctx, val, LY_VALUE_CANON, NULL, NULL, NULL));
Michal Vasko59892dd2022-05-13 11:02:30 +0200494 }
495 return ret;
496 }
497
498 return LY_SUCCESS;
499}
500
501LY_ERR
Michal Vasko583b4642023-05-25 10:39:34 +0200502ly_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len,
503 LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints)
Michal Vasko59892dd2022-05-13 11:02:30 +0200504{
505 LY_ERR rc = LY_SUCCESS;
506 struct ly_err_item *err = NULL;
507 struct lyd_value storage;
508 struct lysc_type *type;
509
Michal Vasko0c361552023-04-04 10:04:03 +0200510 LY_CHECK_ARG_RET(ctx, node, LY_EINVAL);
Michal Vasko59892dd2022-05-13 11:02:30 +0200511
512 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
513 LOGARG(ctx, node);
514 return LY_EINVAL;
515 }
516
517 type = ((struct lysc_node_leaf *)node)->type;
Michal Vasko583b4642023-05-25 10:39:34 +0200518 rc = type->plugin->store(ctx ? ctx : node->module->ctx, type, value, value_len, 0, format, prefix_data, hints, node,
519 &storage, NULL, &err);
Michal Vasko59892dd2022-05-13 11:02:30 +0200520 if (rc == LY_EINCOMPLETE) {
521 /* actually success since we do not provide the context tree and call validation with
522 * LY_TYPE_OPTS_INCOMPLETE_DATA */
523 rc = LY_SUCCESS;
524 } else if (rc && err) {
525 if (ctx) {
526 /* log only in case the ctx was provided as input parameter */
Michal Vaskof8da2682022-06-16 07:52:37 +0200527 if (err->path) {
528 LOG_LOCSET(NULL, NULL, err->path, NULL);
Michal Vaskoccd1fa62023-08-17 09:15:46 +0200529 } else {
530 LOG_LOCSET(node, NULL, NULL, NULL);
Michal Vaskof8da2682022-06-16 07:52:37 +0200531 }
Michal Vasko59892dd2022-05-13 11:02:30 +0200532 LOGVAL_ERRITEM(ctx, err);
Michal Vaskof8da2682022-06-16 07:52:37 +0200533 if (err->path) {
534 LOG_LOCBACK(0, 0, 1, 0);
Michal Vaskoccd1fa62023-08-17 09:15:46 +0200535 } else {
536 LOG_LOCBACK(1, 0, 1, 0);
Michal Vaskof8da2682022-06-16 07:52:37 +0200537 }
Michal Vasko59892dd2022-05-13 11:02:30 +0200538 }
539 ly_err_free(err);
540 }
541
542 if (!rc) {
543 type->plugin->free(ctx ? ctx : node->module->ctx, &storage);
544 }
545 return rc;
546}
547
548LIBYANG_API_DEF LY_ERR
549lyd_value_validate(const struct ly_ctx *ctx, const struct lysc_node *schema, const char *value, size_t value_len,
550 const struct lyd_node *ctx_node, const struct lysc_type **realtype, const char **canonical)
551{
552 LY_ERR rc;
553 struct ly_err_item *err = NULL;
554 struct lysc_type *type;
555 struct lyd_value val = {0};
556 ly_bool stored = 0, log = 1;
557
Michal Vasko3dd16da2022-06-15 07:58:41 +0200558 LY_CHECK_ARG_RET(ctx, schema, !value_len || value, LY_EINVAL);
Michal Vasko59892dd2022-05-13 11:02:30 +0200559
560 if (!ctx) {
561 ctx = schema->module->ctx;
562 log = 0;
563 }
Michal Vasko3dd16da2022-06-15 07:58:41 +0200564 if (!value_len) {
565 value = "";
566 }
Michal Vasko59892dd2022-05-13 11:02:30 +0200567 type = ((struct lysc_node_leaf *)schema)->type;
568
569 /* store */
570 rc = type->plugin->store(ctx, type, value, value_len, 0, LY_VALUE_JSON, NULL,
571 LYD_HINT_DATA, schema, &val, NULL, &err);
572 if (!rc || (rc == LY_EINCOMPLETE)) {
573 stored = 1;
574 }
575
576 if (ctx_node && (rc == LY_EINCOMPLETE)) {
577 /* resolve */
578 rc = type->plugin->validate(ctx, type, ctx_node, ctx_node, &val, &err);
579 }
580
581 if (rc && (rc != LY_EINCOMPLETE) && err) {
582 if (log) {
583 /* log error */
584 if (err->path) {
585 LOG_LOCSET(NULL, NULL, err->path, NULL);
586 } else if (ctx_node) {
587 LOG_LOCSET(NULL, ctx_node, NULL, NULL);
588 } else {
589 LOG_LOCSET(schema, NULL, NULL, NULL);
590 }
591 LOGVAL_ERRITEM(ctx, err);
592 if (err->path) {
593 LOG_LOCBACK(0, 0, 1, 0);
594 } else if (ctx_node) {
595 LOG_LOCBACK(0, 1, 0, 0);
596 } else {
597 LOG_LOCBACK(1, 0, 0, 0);
598 }
599 }
600 ly_err_free(err);
601 }
602
603 if (!rc || (rc == LY_EINCOMPLETE)) {
604 if (realtype) {
605 /* return realtype */
606 if (val.realtype->basetype == LY_TYPE_UNION) {
607 *realtype = val.subvalue->value.realtype;
608 } else {
609 *realtype = val.realtype;
610 }
611 }
612
613 if (canonical) {
614 /* return canonical value */
615 lydict_insert(ctx, val.realtype->plugin->print(ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL), 0, canonical);
616 }
617 }
618
619 if (stored) {
620 /* free value */
621 type->plugin->free(ctx ? ctx : schema->module->ctx, &val);
622 }
623 return rc;
624}
625
626LIBYANG_API_DEF LY_ERR
627lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len)
628{
629 LY_ERR ret = LY_SUCCESS;
630 struct ly_ctx *ctx;
631 struct lysc_type *type;
632 struct lyd_value val = {0};
633
634 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, value, LY_EINVAL);
635
636 ctx = node->schema->module->ctx;
637 type = ((struct lysc_node_leaf *)node->schema)->type;
638
639 /* store the value */
640 LOG_LOCSET(node->schema, &node->node, NULL, NULL);
641 ret = lyd_value_store(ctx, &val, type, value, value_len, NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, node->schema, NULL);
642 LOG_LOCBACK(1, 1, 0, 0);
643 LY_CHECK_RET(ret);
644
645 /* compare values */
646 ret = type->plugin->compare(&node->value, &val);
647
648 type->plugin->free(ctx, &val);
649 return ret;
650}
651
652LIBYANG_API_DEF ly_bool
653lyd_is_default(const struct lyd_node *node)
654{
655 const struct lysc_node_leaf *leaf;
656 const struct lysc_node_leaflist *llist;
657 const struct lyd_node_term *term;
658 LY_ARRAY_COUNT_TYPE u;
659
660 if (!(node->schema->nodetype & LYD_NODE_TERM)) {
661 return 0;
662 }
663
664 term = (const struct lyd_node_term *)node;
665
666 if (node->schema->nodetype == LYS_LEAF) {
667 leaf = (const struct lysc_node_leaf *)node->schema;
668 if (!leaf->dflt) {
669 return 0;
670 }
671
672 /* compare with the default value */
673 if (!leaf->type->plugin->compare(&term->value, leaf->dflt)) {
674 return 1;
675 }
676 } else {
677 llist = (const struct lysc_node_leaflist *)node->schema;
678 if (!llist->dflts) {
679 return 0;
680 }
681
682 LY_ARRAY_FOR(llist->dflts, u) {
683 /* compare with each possible default value */
684 if (!llist->type->plugin->compare(&term->value, llist->dflts[u])) {
685 return 1;
686 }
687 }
688 }
689
690 return 0;
691}
692
693LIBYANG_API_DEF uint32_t
694lyd_list_pos(const struct lyd_node *instance)
695{
696 const struct lyd_node *iter = NULL;
697 uint32_t pos = 0;
698
699 if (!instance || !(instance->schema->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
700 return 0;
701 }
702
703 /* data instances are ordered, so we can stop when we found instance of other schema node */
704 for (iter = instance; iter->schema == instance->schema; iter = iter->prev) {
705 if (pos && (iter->next == NULL)) {
706 /* overrun to the end of the siblings list */
707 break;
708 }
709 ++pos;
710 }
711
712 return pos;
713}
714
715LIBYANG_API_DEF struct lyd_node *
716lyd_first_sibling(const struct lyd_node *node)
717{
718 struct lyd_node *start;
719
720 if (!node) {
721 return NULL;
722 }
723
724 /* get the first sibling */
725 if (node->parent) {
726 start = node->parent->child;
727 } else {
728 for (start = (struct lyd_node *)node; start->prev->next; start = start->prev) {}
729 }
730
731 return start;
732}
733
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100734/**
735 * @brief Check list node parsed into an opaque node for the reason.
736 *
737 * @param[in] node Opaque node.
738 * @param[in] snode Schema node of @p opaq.
739 * @return LY_SUCCESS if the node is valid;
740 * @return LY_ERR on error.
741 */
742static LY_ERR
743lyd_parse_opaq_list_error(const struct lyd_node *node, const struct lysc_node *snode)
744{
745 LY_ERR ret = LY_SUCCESS;
746 struct ly_set key_set = {0};
747 const struct lysc_node *key = NULL;
748 const struct lyd_node *child;
749 const struct lyd_node_opaq *opaq_k;
750 uint32_t i;
751
752 assert(!node->schema);
753
754 /* get all keys into a set */
Michal Vaskof30bcf12023-07-14 12:12:03 +0200755 while ((key = lys_getnext(key, snode, NULL, 0)) && (key->flags & LYS_KEY)) {
756 LY_CHECK_GOTO(ret = ly_set_add(&key_set, (void *)key, 1, NULL), cleanup);
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100757 }
758
759 LY_LIST_FOR(lyd_child(node), child) {
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100760 /* find the key schema node */
761 for (i = 0; i < key_set.count; ++i) {
762 key = key_set.snodes[i];
Michal Vaskoa878a892023-08-18 12:22:07 +0200763 if (!strcmp(key->name, LYD_NAME(child))) {
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100764 break;
765 }
766 }
767 if (i == key_set.count) {
768 /* some other node, skip */
769 continue;
770 }
771
772 /* key found */
773 ly_set_rm_index(&key_set, i, NULL);
774
Michal Vaskoa878a892023-08-18 12:22:07 +0200775 if (child->schema) {
776 /* valid key */
777 continue;
778 }
779
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100780 /* check value */
Michal Vaskoa878a892023-08-18 12:22:07 +0200781 opaq_k = (struct lyd_node_opaq *)child;
Michal Vasko583b4642023-05-25 10:39:34 +0200782 ret = ly_value_validate(LYD_CTX(node), key, opaq_k->value, strlen(opaq_k->value), opaq_k->format,
783 opaq_k->val_prefix_data, opaq_k->hints);
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100784 LY_CHECK_GOTO(ret, cleanup);
785 }
786
787 if (key_set.count) {
788 /* missing keys */
789 LOGVAL(LYD_CTX(node), LY_VCODE_NOKEY, key_set.snodes[0]->name);
790 ret = LY_EVALID;
791 goto cleanup;
792 }
793
794cleanup:
795 ly_set_erase(&key_set, NULL);
796 return ret;
797}
798
799LIBYANG_API_DEF LY_ERR
800lyd_parse_opaq_error(const struct lyd_node *node)
801{
Michal Vasko6727c682023-02-17 10:40:26 +0100802 LY_ERR rc = LY_SUCCESS;
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100803 const struct ly_ctx *ctx;
804 const struct lyd_node_opaq *opaq;
805 const struct lyd_node *parent;
806 const struct lys_module *mod;
Michal Vasko9d9b88d2023-08-21 11:55:43 +0200807 const struct lysc_node *sparent, *snode;
Michal Vasko6727c682023-02-17 10:40:26 +0100808 uint32_t loc_node = 0, loc_path = 0;
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100809
Michal Vasko9d9b88d2023-08-21 11:55:43 +0200810 LY_CHECK_ARG_RET(LYD_CTX(node), node, !node->schema, LY_EINVAL);
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100811
812 ctx = LYD_CTX(node);
813 opaq = (struct lyd_node_opaq *)node;
814 parent = lyd_parent(node);
Michal Vasko9d9b88d2023-08-21 11:55:43 +0200815 sparent = lyd_node_schema(parent);
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100816
Michal Vaskoe50a0b52023-08-17 14:58:30 +0200817 if (parent) {
818 LOG_LOCSET(NULL, parent, NULL, NULL);
Michal Vasko6727c682023-02-17 10:40:26 +0100819 ++loc_node;
820 } else {
821 LOG_LOCSET(NULL, NULL, "/", NULL);
822 ++loc_path;
823 }
824
Michal Vaskof4e63922022-05-10 10:32:13 +0200825 if (!opaq->name.module_ns) {
826 LOGVAL(ctx, LYVE_REFERENCE, "Unknown module of node \"%s\".", opaq->name.name);
Michal Vasko6727c682023-02-17 10:40:26 +0100827 rc = LY_EVALID;
828 goto cleanup;
Michal Vaskof4e63922022-05-10 10:32:13 +0200829 }
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100830
831 /* module */
832 switch (opaq->format) {
833 case LY_VALUE_XML:
Michal Vasko9d9b88d2023-08-21 11:55:43 +0200834 if (!sparent || strcmp(opaq->name.module_ns, sparent->module->ns)) {
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100835 mod = ly_ctx_get_module_implemented_ns(ctx, opaq->name.module_ns);
836 if (!mod) {
Michal Vasko959f8d82022-06-16 07:51:50 +0200837 LOGVAL(ctx, LYVE_REFERENCE, "No (implemented) module with namespace \"%s\" of node \"%s\" in the context.",
838 opaq->name.module_ns, opaq->name.name);
Michal Vasko6727c682023-02-17 10:40:26 +0100839 rc = LY_EVALID;
840 goto cleanup;
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100841 }
842 } else {
843 /* inherit */
Michal Vasko9d9b88d2023-08-21 11:55:43 +0200844 mod = sparent->module;
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100845 }
846 break;
847 case LY_VALUE_JSON:
848 case LY_VALUE_LYB:
Michal Vasko9d9b88d2023-08-21 11:55:43 +0200849 if (!sparent || strcmp(opaq->name.module_name, sparent->module->name)) {
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100850 mod = ly_ctx_get_module_implemented(ctx, opaq->name.module_name);
851 if (!mod) {
Michal Vasko959f8d82022-06-16 07:51:50 +0200852 LOGVAL(ctx, LYVE_REFERENCE, "No (implemented) module named \"%s\" of node \"%s\" in the context.",
853 opaq->name.module_name, opaq->name.name);
Michal Vasko6727c682023-02-17 10:40:26 +0100854 rc = LY_EVALID;
855 goto cleanup;
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100856 }
857 } else {
858 /* inherit */
Michal Vasko9d9b88d2023-08-21 11:55:43 +0200859 mod = sparent->module;
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100860 }
861 break;
862 default:
863 LOGERR(ctx, LY_EINVAL, "Unsupported value format.");
Michal Vasko6727c682023-02-17 10:40:26 +0100864 rc = LY_EINVAL;
865 goto cleanup;
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100866 }
867
868 /* schema */
Michal Vasko9d9b88d2023-08-21 11:55:43 +0200869 snode = lys_find_child(sparent, mod, opaq->name.name, 0, 0, 0);
870 if (!snode && sparent && (sparent->nodetype & (LYS_RPC | LYS_ACTION))) {
Michal Vaskoac6f4be2022-05-02 10:16:50 +0200871 /* maybe output node */
Michal Vasko9d9b88d2023-08-21 11:55:43 +0200872 snode = lys_find_child(sparent, mod, opaq->name.name, 0, 0, LYS_GETNEXT_OUTPUT);
Michal Vaskoac6f4be2022-05-02 10:16:50 +0200873 }
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100874 if (!snode) {
Michal Vasko9d9b88d2023-08-21 11:55:43 +0200875 if (sparent) {
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100876 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%s\" not found as a child of \"%s\" node.", opaq->name.name,
Michal Vasko9d9b88d2023-08-21 11:55:43 +0200877 sparent->name);
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100878 } else {
879 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%s\" not found in the \"%s\" module.", opaq->name.name, mod->name);
880 }
Michal Vasko6727c682023-02-17 10:40:26 +0100881 rc = LY_EVALID;
882 goto cleanup;
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100883 }
884
Michal Vasko6727c682023-02-17 10:40:26 +0100885 /* schema node exists */
886 LOG_LOCBACK(0, loc_node, loc_path, 0);
887 loc_node = 0;
888 loc_path = 0;
889 LOG_LOCSET(NULL, node, NULL, NULL);
890 ++loc_node;
891
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100892 if (snode->nodetype & LYD_NODE_TERM) {
893 /* leaf / leaf-list */
Michal Vasko583b4642023-05-25 10:39:34 +0200894 rc = ly_value_validate(ctx, snode, opaq->value, strlen(opaq->value), opaq->format, opaq->val_prefix_data, opaq->hints);
Michal Vasko6727c682023-02-17 10:40:26 +0100895 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100896 } else if (snode->nodetype == LYS_LIST) {
897 /* list */
Michal Vasko6727c682023-02-17 10:40:26 +0100898 rc = lyd_parse_opaq_list_error(node, snode);
899 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100900 } else if (snode->nodetype & LYD_NODE_INNER) {
901 /* inner node */
902 if (opaq->value) {
903 LOGVAL(ctx, LYVE_DATA, "Invalid value \"%s\" for %s \"%s\".", opaq->value,
904 lys_nodetype2str(snode->nodetype), snode->name);
Michal Vasko6727c682023-02-17 10:40:26 +0100905 rc = LY_EVALID;
906 goto cleanup;
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100907 }
908 } else {
909 LOGERR(ctx, LY_EINVAL, "Unexpected opaque schema node %s \"%s\".", lys_nodetype2str(snode->nodetype), snode->name);
Michal Vasko6727c682023-02-17 10:40:26 +0100910 rc = LY_EINVAL;
911 goto cleanup;
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100912 }
913
914 LOGERR(ctx, LY_EINVAL, "Unexpected valid opaque node %s \"%s\".", lys_nodetype2str(snode->nodetype), snode->name);
Michal Vasko6727c682023-02-17 10:40:26 +0100915 rc = LY_EINVAL;
916
917cleanup:
918 LOG_LOCBACK(0, loc_node, loc_path, 0);
919 return rc;
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100920}
921
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100922LIBYANG_API_DEF const char *
Christian Hopps46bd21b2021-04-27 09:43:58 -0400923lyd_value_get_canonical(const struct ly_ctx *ctx, const struct lyd_value *value)
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200924{
Michal Vaskoab40e7e2021-04-28 17:04:24 +0200925 LY_CHECK_ARG_RET(ctx, ctx, value, NULL);
926
Michal Vasko33876022021-04-27 16:42:24 +0200927 return value->_canonical ? value->_canonical :
928 (const char *)value->realtype->plugin->print(ctx, value, LY_VALUE_CANON, NULL, NULL, NULL);
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200929}
930
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100931LIBYANG_API_DEF LY_ERR
Michal Vaskoa820c312021-02-05 16:33:00 +0100932lyd_any_value_str(const struct lyd_node *any, char **value_str)
933{
934 const struct lyd_node_any *a;
935 struct lyd_node *tree = NULL;
936 const char *str = NULL;
937 ly_bool dynamic = 0;
938 LY_ERR ret = LY_SUCCESS;
939
940 LY_CHECK_ARG_RET(NULL, any, value_str, LY_EINVAL);
Radek Krejci71877df2021-04-06 17:24:06 +0200941 LY_CHECK_ARG_RET(NULL, any->schema, any->schema->nodetype & LYS_ANYDATA, LY_EINVAL);
Michal Vaskoa820c312021-02-05 16:33:00 +0100942
943 a = (struct lyd_node_any *)any;
944 *value_str = NULL;
945
946 if (!a->value.str) {
947 /* there is no value in the union */
948 return LY_SUCCESS;
949 }
950
951 switch (a->value_type) {
952 case LYD_ANYDATA_LYB:
953 /* parse into a data tree */
954 ret = lyd_parse_data_mem(LYD_CTX(any), a->value.mem, LYD_LYB, LYD_PARSE_ONLY, 0, &tree);
955 LY_CHECK_GOTO(ret, cleanup);
956 dynamic = 1;
957 break;
958 case LYD_ANYDATA_DATATREE:
959 tree = a->value.tree;
960 break;
961 case LYD_ANYDATA_STRING:
962 case LYD_ANYDATA_XML:
963 case LYD_ANYDATA_JSON:
964 /* simply use the string */
965 str = a->value.str;
966 break;
967 }
968
969 if (tree) {
970 /* print into a string */
971 ret = lyd_print_mem(value_str, tree, LYD_XML, LYD_PRINT_WITHSIBLINGS);
972 LY_CHECK_GOTO(ret, cleanup);
973 } else {
974 assert(str);
975 *value_str = strdup(str);
976 LY_CHECK_ERR_GOTO(!*value_str, LOGMEM(LYD_CTX(any)), cleanup);
977 }
978
979 /* success */
980
981cleanup:
982 if (dynamic) {
983 lyd_free_all(tree);
984 }
985 return ret;
986}
987
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100988LIBYANG_API_DEF LY_ERR
Michal Vasko61551fa2020-07-09 15:45:45 +0200989lyd_any_copy_value(struct lyd_node *trg, const union lyd_any_value *value, LYD_ANYDATA_VALUETYPE value_type)
990{
991 struct lyd_node_any *t;
Michal Vasko61551fa2020-07-09 15:45:45 +0200992
Michal Vaskoa820c312021-02-05 16:33:00 +0100993 LY_CHECK_ARG_RET(NULL, trg, LY_EINVAL);
Radek Krejci71877df2021-04-06 17:24:06 +0200994 LY_CHECK_ARG_RET(NULL, trg->schema, trg->schema->nodetype & LYS_ANYDATA, LY_EINVAL);
Michal Vasko61551fa2020-07-09 15:45:45 +0200995
996 t = (struct lyd_node_any *)trg;
997
998 /* free trg */
999 switch (t->value_type) {
1000 case LYD_ANYDATA_DATATREE:
1001 lyd_free_all(t->value.tree);
1002 break;
1003 case LYD_ANYDATA_STRING:
1004 case LYD_ANYDATA_XML:
1005 case LYD_ANYDATA_JSON:
Michal Vaskoe180ed02021-02-05 16:31:20 +01001006 lydict_remove(LYD_CTX(trg), t->value.str);
Michal Vasko61551fa2020-07-09 15:45:45 +02001007 break;
1008 case LYD_ANYDATA_LYB:
1009 free(t->value.mem);
1010 break;
1011 }
1012 t->value.str = NULL;
1013
1014 if (!value) {
1015 /* only free value in this case */
1016 return LY_SUCCESS;
1017 }
1018
1019 /* copy src */
1020 t->value_type = value_type;
1021 switch (value_type) {
1022 case LYD_ANYDATA_DATATREE:
1023 if (value->tree) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02001024 LY_CHECK_RET(lyd_dup_siblings(value->tree, NULL, LYD_DUP_RECURSIVE, &t->value.tree));
Michal Vasko61551fa2020-07-09 15:45:45 +02001025 }
1026 break;
1027 case LYD_ANYDATA_STRING:
1028 case LYD_ANYDATA_XML:
1029 case LYD_ANYDATA_JSON:
1030 if (value->str) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001031 LY_CHECK_RET(lydict_insert(LYD_CTX(trg), value->str, 0, &t->value.str));
Michal Vasko61551fa2020-07-09 15:45:45 +02001032 }
1033 break;
1034 case LYD_ANYDATA_LYB:
1035 if (value->mem) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02001036 int len = lyd_lyb_data_length(value->mem);
Michal Vasko26bbb272022-08-02 14:54:33 +02001037
Radek Krejci82fa8d42020-07-11 22:00:59 +02001038 LY_CHECK_RET(len == -1, LY_EINVAL);
Michal Vasko61551fa2020-07-09 15:45:45 +02001039 t->value.mem = malloc(len);
Michal Vaskob7be7a82020-08-20 09:09:04 +02001040 LY_CHECK_ERR_RET(!t->value.mem, LOGMEM(LYD_CTX(trg)), LY_EMEM);
Michal Vasko61551fa2020-07-09 15:45:45 +02001041 memcpy(t->value.mem, value->mem, len);
1042 }
1043 break;
1044 }
1045
1046 return LY_SUCCESS;
1047}
1048
Michal Vasko106f0862021-11-02 11:49:27 +01001049const struct lysc_node *
1050lyd_node_schema(const struct lyd_node *node)
1051{
1052 const struct lysc_node *schema = NULL;
1053 const struct lyd_node *prev_iter = NULL, *iter;
1054 const struct lys_module *mod;
1055
1056 if (!node) {
1057 return NULL;
1058 } else if (node->schema) {
1059 return node->schema;
1060 }
1061
Michal Vaskoa878a892023-08-18 12:22:07 +02001062 /* find the first schema node in the parents */
1063 for (iter = lyd_parent(node); iter && !iter->schema; iter = lyd_parent(iter)) {}
1064 if (iter) {
1065 prev_iter = iter;
1066 schema = prev_iter->schema;
1067 }
1068
Michal Vasko106f0862021-11-02 11:49:27 +01001069 /* get schema node of an opaque node */
1070 do {
1071 /* get next data node */
1072 for (iter = node; lyd_parent(iter) != prev_iter; iter = lyd_parent(iter)) {}
1073
Michal Vaskoa878a892023-08-18 12:22:07 +02001074 /* get module */
1075 mod = lyd_owner_module(iter);
1076 if (!mod) {
1077 /* unknown module, no schema node */
1078 schema = NULL;
1079 break;
Michal Vasko106f0862021-11-02 11:49:27 +01001080 }
Michal Vaskod2f404f2021-11-04 15:37:11 +01001081
Michal Vaskoa878a892023-08-18 12:22:07 +02001082 /* get schema node */
1083 schema = lys_find_child(schema, mod, LYD_NAME(iter), 0, 0, 0);
1084
1085 /* move to the descendant */
Michal Vaskod2f404f2021-11-04 15:37:11 +01001086 prev_iter = iter;
Michal Vasko106f0862021-11-02 11:49:27 +01001087 } while (schema && (iter != node));
1088
1089 return schema;
1090}
1091
Michal Vasko4754d4a2022-12-01 10:11:21 +01001092void
1093lyd_cont_set_dflt(struct lyd_node *node)
1094{
1095 const struct lyd_node *child;
1096
1097 while (node) {
1098 if (!node->schema || (node->flags & LYD_DEFAULT) || !lysc_is_np_cont(node->schema)) {
1099 /* not a non-dflt NP container */
1100 break;
1101 }
1102
1103 LY_LIST_FOR(lyd_child(node), child) {
1104 if (!(child->flags & LYD_DEFAULT)) {
1105 break;
1106 }
1107 }
1108 if (child) {
1109 /* explicit child, no dflt change */
1110 break;
1111 }
1112
1113 /* set the dflt flag */
1114 node->flags |= LYD_DEFAULT;
1115
1116 /* check all parent containers */
1117 node = lyd_parent(node);
1118 }
1119}
1120
Michal Vasko59892dd2022-05-13 11:02:30 +02001121/**
1122 * @brief Comparison callback to match schema node with a schema of a data node.
1123 *
1124 * @param[in] val1_p Pointer to the schema node
1125 * @param[in] val2_p Pointer to the data node
1126 * Implementation of ::lyht_value_equal_cb.
1127 */
1128static ly_bool
1129lyd_hash_table_schema_val_equal(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
1130{
1131 struct lysc_node *val1;
1132 struct lyd_node *val2;
1133
1134 val1 = *((struct lysc_node **)val1_p);
1135 val2 = *((struct lyd_node **)val2_p);
1136
1137 if (val1 == val2->schema) {
1138 /* schema match is enough */
1139 return 1;
1140 } else {
1141 return 0;
1142 }
1143}
1144
1145LY_ERR
1146lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match)
1147{
1148 struct lyd_node **match_p;
1149 struct lyd_node_inner *parent;
1150 uint32_t hash;
1151 lyht_value_equal_cb ht_cb;
1152
Michal Vasko21beaeb2022-08-02 10:42:48 +02001153 assert(schema);
1154 if (!siblings) {
1155 /* no data */
1156 if (match) {
1157 *match = NULL;
1158 }
1159 return LY_ENOTFOUND;
1160 }
Michal Vasko59892dd2022-05-13 11:02:30 +02001161
1162 parent = siblings->parent;
1163 if (parent && parent->schema && parent->children_ht) {
1164 /* calculate our hash */
Michal Vaskoae130f52023-04-20 14:25:16 +02001165 hash = lyht_hash_multi(0, schema->module->name, strlen(schema->module->name));
1166 hash = lyht_hash_multi(hash, schema->name, strlen(schema->name));
1167 hash = lyht_hash_multi(hash, NULL, 0);
Michal Vasko59892dd2022-05-13 11:02:30 +02001168
1169 /* use special hash table function */
1170 ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal);
1171
1172 /* find by hash */
1173 if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) {
1174 siblings = *match_p;
1175 } else {
1176 /* not found */
1177 siblings = NULL;
1178 }
1179
1180 /* set the original hash table compare function back */
1181 lyht_set_cb(parent->children_ht, ht_cb);
1182 } else {
1183 /* find first sibling */
1184 if (siblings->parent) {
1185 siblings = siblings->parent->child;
1186 } else {
1187 while (siblings->prev->next) {
1188 siblings = siblings->prev;
1189 }
1190 }
1191
Michal Vaskod8a52012023-08-15 11:38:10 +02001192 /* search manually without hashes and ignore opaque nodes (cannot be found by hashes) */
1193 for ( ; siblings && siblings->schema; siblings = siblings->next) {
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001194 /* schema match is enough */
Michal Vaskod8a52012023-08-15 11:38:10 +02001195 if (LYD_CTX(siblings) == schema->module->ctx) {
1196 if (siblings->schema == schema) {
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001197 break;
1198 }
1199 } else {
Michal Vaskod8a52012023-08-15 11:38:10 +02001200 if (!strcmp(LYD_NAME(siblings), schema->name) && !strcmp(siblings->schema->module->name, schema->module->name)) {
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001201 break;
1202 }
Michal Vasko59892dd2022-05-13 11:02:30 +02001203 }
1204 }
Michal Vaskod8a52012023-08-15 11:38:10 +02001205 if (siblings && !siblings->schema) {
1206 siblings = NULL;
1207 }
Michal Vasko59892dd2022-05-13 11:02:30 +02001208 }
1209
1210 if (!siblings) {
1211 if (match) {
1212 *match = NULL;
1213 }
1214 return LY_ENOTFOUND;
1215 }
1216
1217 if (match) {
1218 *match = (struct lyd_node *)siblings;
1219 }
1220 return LY_SUCCESS;
1221}
1222
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001223void
Michal Vaskod3bb12f2020-12-04 14:33:09 +01001224lyd_del_move_root(struct lyd_node **root, const struct lyd_node *to_del, const struct lys_module *mod)
1225{
1226 if (*root && (lyd_owner_module(*root) != mod)) {
1227 /* there are no data of mod so this is simply the first top-level sibling */
1228 mod = NULL;
1229 }
1230
1231 if ((*root != to_del) || (*root)->parent) {
1232 return;
1233 }
1234
Michal Vasko598063b2021-07-19 11:39:05 +02001235 if (mod && (*root)->prev->next && (!(*root)->next || (lyd_owner_module(to_del) != lyd_owner_module((*root)->next)))) {
1236 /* there are no more nodes from mod, simply get the first top-level sibling */
Michal Vaskod3bb12f2020-12-04 14:33:09 +01001237 *root = lyd_first_sibling(*root);
Michal Vasko598063b2021-07-19 11:39:05 +02001238 } else {
1239 *root = (*root)->next;
Michal Vaskod3bb12f2020-12-04 14:33:09 +01001240 }
1241}
1242
Michal Vasko8cc3f662022-03-29 11:25:51 +02001243LY_ERR
1244ly_nested_ext_schema(const struct lyd_node *parent, const struct lysc_node *sparent, const char *prefix,
1245 size_t prefix_len, LY_VALUE_FORMAT format, void *prefix_data, const char *name, size_t name_len,
1246 const struct lysc_node **snode, struct lysc_ext_instance **ext)
1247{
1248 LY_ERR r;
1249 LY_ARRAY_COUNT_TYPE u;
1250 struct lysc_ext_instance *nested_exts = NULL;
1251 lyplg_ext_data_snode_clb ext_snode_cb;
1252
1253 /* check if there are any nested extension instances */
1254 if (parent && parent->schema) {
1255 nested_exts = parent->schema->exts;
1256 } else if (sparent) {
1257 nested_exts = sparent->exts;
1258 }
1259 LY_ARRAY_FOR(nested_exts, u) {
Michal Vasko305c6cb2022-04-27 10:33:04 +02001260 if (!nested_exts[u].def->plugin) {
1261 /* no plugin */
1262 continue;
1263 }
1264
Michal Vasko8cc3f662022-03-29 11:25:51 +02001265 ext_snode_cb = nested_exts[u].def->plugin->snode;
1266 if (!ext_snode_cb) {
1267 /* not an extension with nested data */
1268 continue;
1269 }
1270
1271 /* try to get the schema node */
1272 r = ext_snode_cb(&nested_exts[u], parent, sparent, prefix, prefix_len, format, prefix_data, name, name_len, snode);
1273 if (!r) {
Michal Vasko66330fc2022-11-21 15:52:24 +01001274 if (ext) {
1275 /* data successfully created, remember the ext instance */
1276 *ext = &nested_exts[u];
1277 }
Michal Vasko8cc3f662022-03-29 11:25:51 +02001278 return LY_SUCCESS;
1279 } else if (r != LY_ENOT) {
1280 /* fatal error */
1281 return r;
1282 }
1283 /* data was not from this module, continue */
1284 }
1285
1286 /* no extensions or none matched */
1287 return LY_ENOT;
1288}
1289
Michal Vaskod3bb12f2020-12-04 14:33:09 +01001290void
Radek Krejci8df109d2021-04-23 12:19:08 +02001291ly_free_prefix_data(LY_VALUE_FORMAT format, void *prefix_data)
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001292{
1293 struct ly_set *ns_list;
1294 struct lysc_prefix *prefixes;
1295 uint32_t i;
1296 LY_ARRAY_COUNT_TYPE u;
1297
1298 if (!prefix_data) {
1299 return;
1300 }
1301
1302 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001303 case LY_VALUE_XML:
Michal Vaskoddd76592022-01-17 13:34:48 +01001304 case LY_VALUE_STR_NS:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001305 ns_list = prefix_data;
1306 for (i = 0; i < ns_list->count; ++i) {
1307 free(((struct lyxml_ns *)ns_list->objs[i])->prefix);
1308 free(((struct lyxml_ns *)ns_list->objs[i])->uri);
1309 }
1310 ly_set_free(ns_list, free);
1311 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02001312 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001313 prefixes = prefix_data;
1314 LY_ARRAY_FOR(prefixes, u) {
1315 free(prefixes[u].prefix);
1316 }
1317 LY_ARRAY_FREE(prefixes);
1318 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02001319 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02001320 case LY_VALUE_SCHEMA:
1321 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02001322 case LY_VALUE_LYB:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001323 break;
1324 }
1325}
1326
1327LY_ERR
Radek Krejci8df109d2021-04-23 12:19:08 +02001328ly_dup_prefix_data(const struct ly_ctx *ctx, LY_VALUE_FORMAT format, const void *prefix_data,
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001329 void **prefix_data_p)
1330{
1331 LY_ERR ret = LY_SUCCESS;
1332 struct lyxml_ns *ns;
1333 struct lysc_prefix *prefixes = NULL, *orig_pref;
1334 struct ly_set *ns_list, *orig_ns;
1335 uint32_t i;
1336 LY_ARRAY_COUNT_TYPE u;
1337
1338 assert(!*prefix_data_p);
1339
1340 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001341 case LY_VALUE_SCHEMA:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001342 *prefix_data_p = (void *)prefix_data;
1343 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02001344 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001345 /* copy all the value prefixes */
1346 orig_pref = (struct lysc_prefix *)prefix_data;
1347 LY_ARRAY_CREATE_GOTO(ctx, prefixes, LY_ARRAY_COUNT(orig_pref), ret, cleanup);
1348 *prefix_data_p = prefixes;
1349
1350 LY_ARRAY_FOR(orig_pref, u) {
1351 if (orig_pref[u].prefix) {
1352 prefixes[u].prefix = strdup(orig_pref[u].prefix);
1353 LY_CHECK_ERR_GOTO(!prefixes[u].prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1354 }
1355 prefixes[u].mod = orig_pref[u].mod;
1356 LY_ARRAY_INCREMENT(prefixes);
1357 }
1358 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02001359 case LY_VALUE_XML:
Michal Vaskoddd76592022-01-17 13:34:48 +01001360 case LY_VALUE_STR_NS:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001361 /* copy all the namespaces */
1362 LY_CHECK_GOTO(ret = ly_set_new(&ns_list), cleanup);
1363 *prefix_data_p = ns_list;
1364
1365 orig_ns = (struct ly_set *)prefix_data;
1366 for (i = 0; i < orig_ns->count; ++i) {
1367 ns = calloc(1, sizeof *ns);
1368 LY_CHECK_ERR_GOTO(!ns, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1369 LY_CHECK_GOTO(ret = ly_set_add(ns_list, ns, 1, NULL), cleanup);
1370
1371 if (((struct lyxml_ns *)orig_ns->objs[i])->prefix) {
1372 ns->prefix = strdup(((struct lyxml_ns *)orig_ns->objs[i])->prefix);
1373 LY_CHECK_ERR_GOTO(!ns->prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1374 }
1375 ns->uri = strdup(((struct lyxml_ns *)orig_ns->objs[i])->uri);
1376 LY_CHECK_ERR_GOTO(!ns->uri, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1377 }
1378 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02001379 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02001380 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02001381 case LY_VALUE_LYB:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001382 assert(!prefix_data);
1383 *prefix_data_p = NULL;
1384 break;
1385 }
1386
1387cleanup:
1388 if (ret) {
1389 ly_free_prefix_data(format, *prefix_data_p);
1390 *prefix_data_p = NULL;
1391 }
1392 return ret;
1393}
1394
1395LY_ERR
Radek Krejcif9943642021-04-26 10:18:21 +02001396ly_store_prefix_data(const struct ly_ctx *ctx, const void *value, size_t value_len, LY_VALUE_FORMAT format,
Radek Krejci8df109d2021-04-23 12:19:08 +02001397 const void *prefix_data, LY_VALUE_FORMAT *format_p, void **prefix_data_p)
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001398{
1399 LY_ERR ret = LY_SUCCESS;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001400 const struct lys_module *mod;
Michal Vaskofc2cd072021-02-24 13:17:17 +01001401 const struct lyxml_ns *ns;
1402 struct lyxml_ns *new_ns;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001403 struct ly_set *ns_list;
1404 struct lysc_prefix *prefixes = NULL, *val_pref;
aPiecek83436bc2021-03-30 12:20:45 +02001405 const char *value_iter, *value_next, *value_end;
1406 uint32_t substr_len;
1407 ly_bool is_prefix;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001408
1409 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001410 case LY_VALUE_SCHEMA:
Michal Vaskofc2cd072021-02-24 13:17:17 +01001411 /* copy all referenced modules as prefix - module pairs */
1412 if (!*prefix_data_p) {
1413 /* new prefix data */
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001414 LY_ARRAY_CREATE_GOTO(ctx, prefixes, 0, ret, cleanup);
Radek Krejci8df109d2021-04-23 12:19:08 +02001415 *format_p = LY_VALUE_SCHEMA_RESOLVED;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001416 *prefix_data_p = prefixes;
Michal Vaskofc2cd072021-02-24 13:17:17 +01001417 } else {
1418 /* reuse prefix data */
Radek Krejci8df109d2021-04-23 12:19:08 +02001419 assert(*format_p == LY_VALUE_SCHEMA_RESOLVED);
Michal Vaskofc2cd072021-02-24 13:17:17 +01001420 prefixes = *prefix_data_p;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001421 }
1422
Michal Vaskoc0f9c4c2022-05-06 12:12:17 +02001423 /* add current module for unprefixed values */
1424 LY_ARRAY_NEW_GOTO(ctx, prefixes, val_pref, ret, cleanup);
1425 *prefix_data_p = prefixes;
1426
1427 val_pref->prefix = NULL;
1428 val_pref->mod = ((const struct lysp_module *)prefix_data)->mod;
1429
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001430 /* add all used prefixes */
Michal Vasko59e90fc2021-09-22 12:17:08 +02001431 value_end = (char *)value + value_len;
aPiecek83436bc2021-03-30 12:20:45 +02001432 for (value_iter = value; value_iter; value_iter = value_next) {
aPieceke3f828d2021-05-10 15:34:41 +02001433 LY_CHECK_GOTO(ret = ly_value_prefix_next(value_iter, value_end, &substr_len, &is_prefix, &value_next), cleanup);
aPiecek83436bc2021-03-30 12:20:45 +02001434 if (is_prefix) {
1435 /* we have a possible prefix. Do we already have the prefix? */
1436 mod = ly_resolve_prefix(ctx, value_iter, substr_len, *format_p, *prefix_data_p);
1437 if (!mod) {
1438 mod = ly_resolve_prefix(ctx, value_iter, substr_len, format, prefix_data);
1439 if (mod) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001440 assert(*format_p == LY_VALUE_SCHEMA_RESOLVED);
aPiecek83436bc2021-03-30 12:20:45 +02001441 /* store a new prefix - module pair */
1442 LY_ARRAY_NEW_GOTO(ctx, prefixes, val_pref, ret, cleanup);
1443 *prefix_data_p = prefixes;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001444
aPiecek83436bc2021-03-30 12:20:45 +02001445 val_pref->prefix = strndup(value_iter, substr_len);
1446 LY_CHECK_ERR_GOTO(!val_pref->prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1447 val_pref->mod = mod;
1448 } /* else it is not even defined */
1449 } /* else the prefix is already present */
Michal Vaskofc2cd072021-02-24 13:17:17 +01001450 }
1451 }
1452 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02001453 case LY_VALUE_XML:
Michal Vaskoddd76592022-01-17 13:34:48 +01001454 case LY_VALUE_STR_NS:
Michal Vaskofc2cd072021-02-24 13:17:17 +01001455 /* copy all referenced namespaces as prefix - namespace pairs */
1456 if (!*prefix_data_p) {
1457 /* new prefix data */
1458 LY_CHECK_GOTO(ret = ly_set_new(&ns_list), cleanup);
Michal Vaskoddd76592022-01-17 13:34:48 +01001459 *format_p = format;
Michal Vaskofc2cd072021-02-24 13:17:17 +01001460 *prefix_data_p = ns_list;
1461 } else {
1462 /* reuse prefix data */
Michal Vaskoddd76592022-01-17 13:34:48 +01001463 assert(*format_p == format);
Michal Vaskofc2cd072021-02-24 13:17:17 +01001464 ns_list = *prefix_data_p;
1465 }
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001466
Michal Vasko294e7f02022-02-28 13:59:00 +01001467 /* store default namespace */
1468 ns = lyxml_ns_get(prefix_data, NULL, 0);
1469 if (ns) {
1470 new_ns = calloc(1, sizeof *new_ns);
1471 LY_CHECK_ERR_GOTO(!new_ns, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1472 LY_CHECK_GOTO(ret = ly_set_add(ns_list, new_ns, 1, NULL), cleanup);
1473
1474 new_ns->prefix = NULL;
1475 new_ns->uri = strdup(ns->uri);
1476 LY_CHECK_ERR_GOTO(!new_ns->uri, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1477 }
1478
Michal Vaskofc2cd072021-02-24 13:17:17 +01001479 /* add all used prefixes */
Michal Vasko59e90fc2021-09-22 12:17:08 +02001480 value_end = (char *)value + value_len;
aPiecek83436bc2021-03-30 12:20:45 +02001481 for (value_iter = value; value_iter; value_iter = value_next) {
aPieceke3f828d2021-05-10 15:34:41 +02001482 LY_CHECK_GOTO(ret = ly_value_prefix_next(value_iter, value_end, &substr_len, &is_prefix, &value_next), cleanup);
aPiecek83436bc2021-03-30 12:20:45 +02001483 if (is_prefix) {
1484 /* we have a possible prefix. Do we already have the prefix? */
1485 ns = lyxml_ns_get(ns_list, value_iter, substr_len);
1486 if (!ns) {
1487 ns = lyxml_ns_get(prefix_data, value_iter, substr_len);
1488 if (ns) {
1489 /* store a new prefix - namespace pair */
1490 new_ns = calloc(1, sizeof *new_ns);
1491 LY_CHECK_ERR_GOTO(!new_ns, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1492 LY_CHECK_GOTO(ret = ly_set_add(ns_list, new_ns, 1, NULL), cleanup);
Michal Vaskofc2cd072021-02-24 13:17:17 +01001493
aPiecek83436bc2021-03-30 12:20:45 +02001494 new_ns->prefix = strndup(value_iter, substr_len);
1495 LY_CHECK_ERR_GOTO(!new_ns->prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1496 new_ns->uri = strdup(ns->uri);
1497 LY_CHECK_ERR_GOTO(!new_ns->uri, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1498 } /* else it is not even defined */
1499 } /* else the prefix is already present */
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001500 }
1501 }
1502 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02001503 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02001504 case LY_VALUE_SCHEMA_RESOLVED:
1505 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02001506 case LY_VALUE_LYB:
Michal Vaskofc2cd072021-02-24 13:17:17 +01001507 if (!*prefix_data_p) {
1508 /* new prefix data - simply copy all the prefix data */
1509 *format_p = format;
1510 LY_CHECK_GOTO(ret = ly_dup_prefix_data(ctx, format, prefix_data, prefix_data_p), cleanup);
1511 } /* else reuse prefix data - the prefix data are always the same, nothing to do */
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001512 break;
1513 }
1514
1515cleanup:
1516 if (ret) {
1517 ly_free_prefix_data(*format_p, *prefix_data_p);
1518 *prefix_data_p = NULL;
1519 }
1520 return ret;
1521}
Michal Vasko7ed1fcb2020-12-03 14:15:22 +01001522
1523const char *
Radek Krejci8df109d2021-04-23 12:19:08 +02001524ly_format2str(LY_VALUE_FORMAT format)
Michal Vasko7ed1fcb2020-12-03 14:15:22 +01001525{
1526 switch (format) {
Radek Krejci224d4b42021-04-23 13:54:59 +02001527 case LY_VALUE_CANON:
1528 return "canonical";
Radek Krejci8df109d2021-04-23 12:19:08 +02001529 case LY_VALUE_SCHEMA:
Michal Vasko7ed1fcb2020-12-03 14:15:22 +01001530 return "schema imports";
Radek Krejci8df109d2021-04-23 12:19:08 +02001531 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko7ed1fcb2020-12-03 14:15:22 +01001532 return "schema stored mapping";
Radek Krejci8df109d2021-04-23 12:19:08 +02001533 case LY_VALUE_XML:
Michal Vasko7ed1fcb2020-12-03 14:15:22 +01001534 return "XML prefixes";
Radek Krejci8df109d2021-04-23 12:19:08 +02001535 case LY_VALUE_JSON:
Michal Vasko7ed1fcb2020-12-03 14:15:22 +01001536 return "JSON module names";
Radek Krejcif9943642021-04-26 10:18:21 +02001537 case LY_VALUE_LYB:
1538 return "LYB prefixes";
Michal Vasko7ed1fcb2020-12-03 14:15:22 +01001539 default:
1540 break;
1541 }
1542
1543 return NULL;
1544}
Michal Vasko43297a02021-05-19 11:12:37 +02001545
Michal Vaskof17bb2a2023-02-10 11:34:55 +01001546LIBYANG_API_DEF int
1547ly_time_tz_offset(void)
1548{
Michal Vasko9b560332023-04-24 15:43:56 +02001549 return ly_time_tz_offset_at(time(NULL));
1550}
1551
1552LIBYANG_API_DEF int
1553ly_time_tz_offset_at(time_t time)
1554{
Michal Vaskof17bb2a2023-02-10 11:34:55 +01001555 struct tm tm_local, tm_utc;
1556 int result = 0;
1557
1558 /* init timezone */
1559 tzset();
1560
1561 /* get local and UTC time */
Michal Vasko9b560332023-04-24 15:43:56 +02001562 localtime_r(&time, &tm_local);
1563 gmtime_r(&time, &tm_utc);
Michal Vaskoe50b98d2023-03-28 11:39:01 +02001564
Michal Vasko9b560332023-04-24 15:43:56 +02001565 /* account for year/month/day change by adding/subtracting from the hours, the change cannot be more than 1 day */
1566 if (tm_local.tm_year < tm_utc.tm_year) {
1567 tm_utc.tm_hour += 24;
1568 } else if (tm_local.tm_year > tm_utc.tm_year) {
1569 tm_local.tm_hour += 24;
1570 } else if (tm_local.tm_mon < tm_utc.tm_mon) {
1571 tm_utc.tm_hour += 24;
1572 } else if (tm_local.tm_mon > tm_utc.tm_mon) {
1573 tm_local.tm_hour += 24;
1574 } else if (tm_local.tm_mday < tm_utc.tm_mday) {
1575 tm_utc.tm_hour += 24;
1576 } else if (tm_local.tm_mday > tm_utc.tm_mday) {
1577 tm_local.tm_hour += 24;
Michal Vaskoe50b98d2023-03-28 11:39:01 +02001578 }
Michal Vaskof17bb2a2023-02-10 11:34:55 +01001579
1580 /* hours shift in seconds */
1581 result += (tm_local.tm_hour - tm_utc.tm_hour) * 3600;
1582
1583 /* minutes shift in seconds */
1584 result += (tm_local.tm_min - tm_utc.tm_min) * 60;
1585
1586 /* seconds shift */
1587 result += tm_local.tm_sec - tm_utc.tm_sec;
1588
1589 return result;
1590}
1591
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001592LIBYANG_API_DEF LY_ERR
Michal Vasko43297a02021-05-19 11:12:37 +02001593ly_time_str2time(const char *value, time_t *time, char **fractions_s)
1594{
1595 struct tm tm = {0};
1596 uint32_t i, frac_len;
1597 const char *frac;
1598 int64_t shift, shift_m;
1599 time_t t;
1600
1601 LY_CHECK_ARG_RET(NULL, value, time, LY_EINVAL);
1602
1603 tm.tm_year = atoi(&value[0]) - 1900;
1604 tm.tm_mon = atoi(&value[5]) - 1;
1605 tm.tm_mday = atoi(&value[8]);
1606 tm.tm_hour = atoi(&value[11]);
1607 tm.tm_min = atoi(&value[14]);
1608 tm.tm_sec = atoi(&value[17]);
1609
Michal Vasko2b7e1612023-08-08 13:37:34 +02001610 /* explicit checks for some gross errors */
1611 if (tm.tm_mon > 11) {
1612 LOGERR(NULL, LY_EINVAL, "Invalid date-and-time month \"%d\".", tm.tm_mon);
1613 return LY_EINVAL;
1614 }
1615 if ((tm.tm_mday < 1) || (tm.tm_mday > 31)) {
1616 LOGERR(NULL, LY_EINVAL, "Invalid date-and-time day of month \"%d\".", tm.tm_mday);
1617 return LY_EINVAL;
1618 }
1619 if (tm.tm_hour > 23) {
1620 LOGERR(NULL, LY_EINVAL, "Invalid date-and-time hours \"%d\".", tm.tm_hour);
1621 return LY_EINVAL;
1622 }
1623 if (tm.tm_min > 59) {
1624 LOGERR(NULL, LY_EINVAL, "Invalid date-and-time minutes \"%d\".", tm.tm_min);
1625 return LY_EINVAL;
1626 }
1627 if (tm.tm_sec > 60) {
1628 LOGERR(NULL, LY_EINVAL, "Invalid date-and-time seconds \"%d\".", tm.tm_sec);
1629 return LY_EINVAL;
1630 }
1631
Michal Vasko43297a02021-05-19 11:12:37 +02001632 t = timegm(&tm);
1633 i = 19;
1634
1635 /* fractions of a second */
1636 if (value[i] == '.') {
1637 ++i;
1638 frac = &value[i];
1639 for (frac_len = 0; isdigit(frac[frac_len]); ++frac_len) {}
1640
1641 i += frac_len;
Michal Vasko43297a02021-05-19 11:12:37 +02001642 } else {
1643 frac = NULL;
1644 }
1645
1646 /* apply offset */
1647 if ((value[i] == 'Z') || (value[i] == 'z')) {
1648 /* zero shift */
1649 shift = 0;
1650 } else {
1651 shift = strtol(&value[i], NULL, 10);
Michal Vasko2b7e1612023-08-08 13:37:34 +02001652 if (shift > 23) {
1653 LOGERR(NULL, LY_EINVAL, "Invalid date-and-time timezone hour \"%" PRIi64 "\".", shift);
1654 return LY_EINVAL;
1655 }
Michal Vasko43297a02021-05-19 11:12:37 +02001656 shift = shift * 60 * 60; /* convert from hours to seconds */
Michal Vasko2b7e1612023-08-08 13:37:34 +02001657
1658 shift_m = strtol(&value[i + 4], NULL, 10);
1659 if (shift_m > 59) {
1660 LOGERR(NULL, LY_EINVAL, "Invalid date-and-time timezone minutes \"%" PRIi64 "\".", shift_m);
1661 return LY_EINVAL;
1662 }
1663 shift_m *= 60; /* convert from minutes to seconds */
1664
Michal Vasko43297a02021-05-19 11:12:37 +02001665 /* correct sign */
1666 if (shift < 0) {
1667 shift_m *= -1;
1668 }
Michal Vasko2b7e1612023-08-08 13:37:34 +02001669
Michal Vasko43297a02021-05-19 11:12:37 +02001670 /* connect hours and minutes of the shift */
1671 shift = shift + shift_m;
1672 }
1673
1674 /* we have to shift to the opposite way to correct the time */
1675 t -= shift;
1676
1677 *time = t;
1678 if (fractions_s) {
1679 if (frac) {
1680 *fractions_s = strndup(frac, frac_len);
1681 LY_CHECK_RET(!*fractions_s, LY_EMEM);
1682 } else {
1683 *fractions_s = NULL;
1684 }
1685 }
1686 return LY_SUCCESS;
1687}
1688
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001689LIBYANG_API_DEF LY_ERR
Michal Vasko43297a02021-05-19 11:12:37 +02001690ly_time_time2str(time_t time, const char *fractions_s, char **str)
1691{
1692 struct tm tm;
Michal Vaskof17bb2a2023-02-10 11:34:55 +01001693 char zoneshift[12];
1694 int zonediff_s, zonediff_h, zonediff_m;
Michal Vasko43297a02021-05-19 11:12:37 +02001695
1696 LY_CHECK_ARG_RET(NULL, str, LY_EINVAL);
1697
Michal Vaskof17bb2a2023-02-10 11:34:55 +01001698 /* init timezone */
Michal Vasko43297a02021-05-19 11:12:37 +02001699 tzset();
1700
1701 /* convert */
1702 if (!localtime_r(&time, &tm)) {
1703 return LY_ESYS;
1704 }
1705
Michal Vaskoe50b98d2023-03-28 11:39:01 +02001706 /* get timezone offset (do not use tm_gmtoff to avoid portability problems) */
Michal Vasko9b560332023-04-24 15:43:56 +02001707 zonediff_s = ly_time_tz_offset_at(time);
Michal Vaskof17bb2a2023-02-10 11:34:55 +01001708 zonediff_h = zonediff_s / 60 / 60;
1709 zonediff_m = zonediff_s / 60 % 60;
Michal Vasko43297a02021-05-19 11:12:37 +02001710 sprintf(zoneshift, "%+03d:%02d", zonediff_h, zonediff_m);
1711
1712 /* print */
1713 if (asprintf(str, "%04d-%02d-%02dT%02d:%02d:%02d%s%s%s",
1714 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
1715 fractions_s ? "." : "", fractions_s ? fractions_s : "", zoneshift) == -1) {
1716 return LY_EMEM;
1717 }
1718
1719 return LY_SUCCESS;
1720}
1721
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001722LIBYANG_API_DEF LY_ERR
Michal Vasko43297a02021-05-19 11:12:37 +02001723ly_time_str2ts(const char *value, struct timespec *ts)
1724{
1725 LY_ERR rc;
Michal Vasko72975062021-08-25 08:13:04 +02001726 char *fractions_s, frac_buf[10];
Michal Vasko43297a02021-05-19 11:12:37 +02001727 int frac_len;
1728
1729 LY_CHECK_ARG_RET(NULL, value, ts, LY_EINVAL);
1730
1731 rc = ly_time_str2time(value, &ts->tv_sec, &fractions_s);
1732 LY_CHECK_RET(rc);
1733
1734 /* convert fractions of a second to nanoseconds */
1735 if (fractions_s) {
Michal Vasko72975062021-08-25 08:13:04 +02001736 /* init frac_buf with zeroes */
1737 memset(frac_buf, '0', 9);
1738 frac_buf[9] = '\0';
1739
Michal Vasko43297a02021-05-19 11:12:37 +02001740 frac_len = strlen(fractions_s);
1741 memcpy(frac_buf, fractions_s, frac_len > 9 ? 9 : frac_len);
1742 ts->tv_nsec = atol(frac_buf);
1743 free(fractions_s);
1744 } else {
1745 ts->tv_nsec = 0;
1746 }
1747
1748 return LY_SUCCESS;
1749}
1750
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001751LIBYANG_API_DEF LY_ERR
Michal Vasko43297a02021-05-19 11:12:37 +02001752ly_time_ts2str(const struct timespec *ts, char **str)
1753{
1754 char frac_buf[10];
1755
Jan Kundrátbd157002021-08-30 14:02:22 +02001756 LY_CHECK_ARG_RET(NULL, ts, str, ((ts->tv_nsec <= 999999999) && (ts->tv_nsec >= 0)), LY_EINVAL);
Michal Vasko43297a02021-05-19 11:12:37 +02001757
1758 /* convert nanoseconds to fractions of a second */
1759 if (ts->tv_nsec) {
1760 sprintf(frac_buf, "%09ld", ts->tv_nsec);
1761 }
1762
1763 return ly_time_time2str(ts->tv_sec, ts->tv_nsec ? frac_buf : NULL, str);
1764}