blob: eb1443313e4fe40c278602a1525a50d048116029 [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
Michal Vasko6b5cb2a2020-11-11 19:11:21 +010025#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020026#include "context.h"
Radek Krejci47fab892020-11-05 17:02:41 +010027#include "dict.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020028#include "hash_table.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020029#include "log.h"
Michal Vasko8f702ee2024-02-20 15:44:24 +010030#include "ly_common.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 Vasko420cc252023-08-24 08:14:24 +0200303 while (!node->schema && node->parent) {
304 node = lyd_parent(node);
305 }
306
Michal Vaskod5cfa6e2020-11-23 16:56:08 +0100307 if (!node->schema) {
Michal Vasko420cc252023-08-24 08:14:24 +0200308 /* top-level opaque node */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +0100309 opaq = (struct lyd_node_opaq *)node;
310 switch (opaq->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200311 case LY_VALUE_XML:
Michal Vasko09fbb812023-04-13 14:11:06 +0200312 if (opaq->name.module_ns) {
313 return ly_ctx_get_module_implemented_ns(LYD_CTX(node), opaq->name.module_ns);
314 }
315 break;
Radek Krejci8df109d2021-04-23 12:19:08 +0200316 case LY_VALUE_JSON:
Michal Vasko09fbb812023-04-13 14:11:06 +0200317 if (opaq->name.module_name) {
318 return ly_ctx_get_module_implemented(LYD_CTX(node), opaq->name.module_name);
319 }
320 break;
Michal Vaskod5cfa6e2020-11-23 16:56:08 +0100321 default:
322 return NULL;
323 }
Michal Vasko09fbb812023-04-13 14:11:06 +0200324
Michal Vasko420cc252023-08-24 08:14:24 +0200325 return NULL;
Michal Vaskod5cfa6e2020-11-23 16:56:08 +0100326 }
327
Michal Vaskoef53c812021-10-13 10:21:03 +0200328 return lysc_owner_module(node->schema);
Michal Vasko9b368d32020-02-14 13:53:31 +0100329}
Michal Vaskob1b5c262020-03-05 14:29:47 +0100330
Michal Vasko420cc252023-08-24 08:14:24 +0200331LIBYANG_API_DEF const struct lys_module *
332lyd_node_module(const struct lyd_node *node)
333{
334 const struct lyd_node_opaq *opaq;
335
336 while (node) {
337 /* data node */
338 if (node->schema) {
339 return node->schema->module;
340 }
341
342 /* opaque node */
343 opaq = (struct lyd_node_opaq *)node;
344 switch (opaq->format) {
345 case LY_VALUE_XML:
346 if (opaq->name.module_ns) {
347 return ly_ctx_get_module_implemented_ns(LYD_CTX(node), opaq->name.module_ns);
348 }
349 break;
350 case LY_VALUE_JSON:
351 if (opaq->name.module_name) {
352 return ly_ctx_get_module_implemented(LYD_CTX(node), opaq->name.module_name);
353 }
354 break;
355 default:
356 break;
357 }
358
359 node = lyd_parent(node);
360 }
361
362 return NULL;
363}
364
Michal Vasko598063b2021-07-19 11:39:05 +0200365void
366lyd_first_module_sibling(struct lyd_node **node, const struct lys_module *mod)
367{
368 int cmp;
369 struct lyd_node *first;
Michal Vaskoec139eb2022-05-10 10:08:40 +0200370 const struct lys_module *own_mod;
Michal Vasko598063b2021-07-19 11:39:05 +0200371
372 assert(node && mod);
373
374 if (!*node) {
375 return;
376 }
377
378 first = *node;
Michal Vaskoec139eb2022-05-10 10:08:40 +0200379 own_mod = lyd_owner_module(first);
380 cmp = own_mod ? strcmp(own_mod->name, mod->name) : 1;
Michal Vasko598063b2021-07-19 11:39:05 +0200381 if (cmp > 0) {
382 /* there may be some preceding data */
383 while (first->prev->next) {
384 first = first->prev;
385 if (lyd_owner_module(first) == mod) {
386 cmp = 0;
387 break;
388 }
389 }
390 }
391
392 if (cmp == 0) {
393 /* there may be some preceding data belonging to this module */
394 while (first->prev->next) {
395 if (lyd_owner_module(first->prev) != mod) {
396 break;
397 }
398 first = first->prev;
399 }
400 }
401
402 if (cmp < 0) {
403 /* there may be some following data */
404 LY_LIST_FOR(first, first) {
405 if (lyd_owner_module(first) == mod) {
406 cmp = 0;
407 break;
408 }
409 }
410 }
411
412 if (cmp == 0) {
413 /* we have found the first module data node */
414 *node = first;
415 }
416}
417
Michal Vaskob1b5c262020-03-05 14:29:47 +0100418const struct lys_module *
Michal Vasko26e80012020-07-08 10:55:46 +0200419lyd_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 +0200420 struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +0100421{
422 struct lyd_node *iter;
423 const struct lys_module *mod;
424
425 /* get the next module */
Michal Vasko26e80012020-07-08 10:55:46 +0200426 if (module) {
427 if (*i) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100428 mod = NULL;
Michal Vasko26e80012020-07-08 10:55:46 +0200429 } else {
430 mod = module;
431 ++(*i);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100432 }
433 } else {
434 do {
435 mod = ly_ctx_get_module_iter(ctx, i);
436 } while (mod && !mod->implemented);
437 }
438
439 /* find its data */
440 *first = NULL;
441 if (mod) {
442 LY_LIST_FOR(tree, iter) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100443 if (lyd_owner_module(iter) == mod) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100444 *first = iter;
445 break;
446 }
447 }
448 }
449
450 return mod;
451}
452
453const struct lys_module *
454lyd_data_next_module(struct lyd_node **next, struct lyd_node **first)
455{
456 const struct lys_module *mod;
457
458 if (!*next) {
459 /* all data traversed */
460 *first = NULL;
461 return NULL;
462 }
463
464 *first = *next;
465
466 /* prepare next */
Michal Vaskoc193ce92020-03-06 11:04:48 +0100467 mod = lyd_owner_module(*next);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100468 LY_LIST_FOR(*next, *next) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100469 if (lyd_owner_module(*next) != mod) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100470 break;
471 }
472 }
473
474 return mod;
475}
Michal Vasko9f96a052020-03-10 09:41:45 +0100476
Michal Vasko7a266772024-01-23 11:02:38 +0100477/**
478 * @brief Log generated error item and use log location information if not in the error item.
479 *
480 * @param[in] ctx Context to use.
481 * @param[in] node Optional data node to log.
482 * @param[in] scnode Optional schema node to log.
483 * @param[in] eitem Error item to log.
484 */
485static void
486ly_err_print_build_path(const struct ly_ctx *ctx, const struct lyd_node *node, const struct lysc_node *scnode,
487 struct ly_err_item *eitem)
488{
489 if (eitem->data_path || eitem->schema_path || eitem->line) {
490 ly_err_print(ctx, eitem);
491 } else {
492 if (node) {
493 LOG_LOCSET(NULL, node);
494 } else if (scnode) {
495 LOG_LOCSET(scnode, NULL);
496 }
497 ly_vlog(ctx, eitem->apptag, eitem->err == LY_EVALID ? eitem->vecode : LYVE_DATA, "%s", eitem->msg);
498 if (node) {
499 LOG_LOCBACK(0, 1);
500 } else if (scnode) {
501 LOG_LOCBACK(1, 0);
502 }
503 }
504}
505
Michal Vasko9f96a052020-03-10 09:41:45 +0100506LY_ERR
Michal Vasko59892dd2022-05-13 11:02:30 +0200507lyd_value_store(const struct ly_ctx *ctx, struct lyd_value *val, const struct lysc_type *type, const void *value,
Michal Vasko989cdb42023-10-06 15:32:37 +0200508 size_t value_len, ly_bool is_utf8, ly_bool *dynamic, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints,
Michal Vasko59892dd2022-05-13 11:02:30 +0200509 const struct lysc_node *ctx_node, ly_bool *incomplete)
510{
511 LY_ERR ret;
512 struct ly_err_item *err = NULL;
Michal Vasko989cdb42023-10-06 15:32:37 +0200513 uint32_t options = 0;
Michal Vasko59892dd2022-05-13 11:02:30 +0200514
515 if (!value) {
516 value = "";
517 }
518 if (incomplete) {
519 *incomplete = 0;
520 }
521
Michal Vasko989cdb42023-10-06 15:32:37 +0200522 if (dynamic && *dynamic) {
523 options |= LYPLG_TYPE_STORE_DYNAMIC;
524 }
525 if (is_utf8) {
526 options |= LYPLG_TYPE_STORE_IS_UTF8;
527 }
528
Michal Vasko59892dd2022-05-13 11:02:30 +0200529 ret = type->plugin->store(ctx, type, value, value_len, options, format, prefix_data, hints, ctx_node, val, NULL, &err);
530 if (dynamic) {
531 *dynamic = 0;
532 }
533
534 if (ret == LY_EINCOMPLETE) {
535 if (incomplete) {
536 *incomplete = 1;
537 }
538 } else if (ret) {
539 if (err) {
Michal Vasko7a266772024-01-23 11:02:38 +0100540 ly_err_print_build_path(ctx, NULL, ctx_node, err);
Michal Vasko59892dd2022-05-13 11:02:30 +0200541 ly_err_free(err);
542 } else {
543 LOGVAL(ctx, LYVE_OTHER, "Storing value failed.");
544 }
545 return ret;
546 }
547
548 return LY_SUCCESS;
549}
550
551LY_ERR
552lyd_value_validate_incomplete(const struct ly_ctx *ctx, const struct lysc_type *type, struct lyd_value *val,
553 const struct lyd_node *ctx_node, const struct lyd_node *tree)
554{
555 LY_ERR ret;
556 struct ly_err_item *err = NULL;
557
558 assert(type->plugin->validate);
559
560 ret = type->plugin->validate(ctx, type, ctx_node, tree, val, &err);
561 if (ret) {
562 if (err) {
Michal Vasko7a266772024-01-23 11:02:38 +0100563 ly_err_print_build_path(ctx, ctx_node, NULL, err);
Michal Vasko59892dd2022-05-13 11:02:30 +0200564 ly_err_free(err);
565 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +0200566 LOGVAL(ctx, LYVE_OTHER, "Resolving value \"%s\" failed.",
567 (char *)type->plugin->print(ctx, val, LY_VALUE_CANON, NULL, NULL, NULL));
Michal Vasko59892dd2022-05-13 11:02:30 +0200568 }
569 return ret;
570 }
571
572 return LY_SUCCESS;
573}
574
575LY_ERR
Michal Vasko583b4642023-05-25 10:39:34 +0200576ly_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len,
577 LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints)
Michal Vasko59892dd2022-05-13 11:02:30 +0200578{
579 LY_ERR rc = LY_SUCCESS;
580 struct ly_err_item *err = NULL;
581 struct lyd_value storage;
582 struct lysc_type *type;
583
Michal Vasko0c361552023-04-04 10:04:03 +0200584 LY_CHECK_ARG_RET(ctx, node, LY_EINVAL);
Michal Vasko59892dd2022-05-13 11:02:30 +0200585
586 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
587 LOGARG(ctx, node);
588 return LY_EINVAL;
589 }
590
591 type = ((struct lysc_node_leaf *)node)->type;
Michal Vasko583b4642023-05-25 10:39:34 +0200592 rc = type->plugin->store(ctx ? ctx : node->module->ctx, type, value, value_len, 0, format, prefix_data, hints, node,
593 &storage, NULL, &err);
Michal Vasko59892dd2022-05-13 11:02:30 +0200594 if (rc == LY_EINCOMPLETE) {
595 /* actually success since we do not provide the context tree and call validation with
596 * LY_TYPE_OPTS_INCOMPLETE_DATA */
597 rc = LY_SUCCESS;
598 } else if (rc && err) {
599 if (ctx) {
600 /* log only in case the ctx was provided as input parameter */
Michal Vasko7a266772024-01-23 11:02:38 +0100601 ly_err_print_build_path(ctx, NULL, node, err);
Michal Vasko59892dd2022-05-13 11:02:30 +0200602 }
603 ly_err_free(err);
604 }
605
606 if (!rc) {
607 type->plugin->free(ctx ? ctx : node->module->ctx, &storage);
608 }
609 return rc;
610}
611
612LIBYANG_API_DEF LY_ERR
613lyd_value_validate(const struct ly_ctx *ctx, const struct lysc_node *schema, const char *value, size_t value_len,
614 const struct lyd_node *ctx_node, const struct lysc_type **realtype, const char **canonical)
615{
616 LY_ERR rc;
617 struct ly_err_item *err = NULL;
618 struct lysc_type *type;
619 struct lyd_value val = {0};
620 ly_bool stored = 0, log = 1;
621
Michal Vasko3dd16da2022-06-15 07:58:41 +0200622 LY_CHECK_ARG_RET(ctx, schema, !value_len || value, LY_EINVAL);
Michal Vasko59892dd2022-05-13 11:02:30 +0200623
624 if (!ctx) {
625 ctx = schema->module->ctx;
626 log = 0;
627 }
Michal Vasko3dd16da2022-06-15 07:58:41 +0200628 if (!value_len) {
629 value = "";
630 }
Michal Vasko59892dd2022-05-13 11:02:30 +0200631 type = ((struct lysc_node_leaf *)schema)->type;
632
633 /* store */
634 rc = type->plugin->store(ctx, type, value, value_len, 0, LY_VALUE_JSON, NULL,
635 LYD_HINT_DATA, schema, &val, NULL, &err);
636 if (!rc || (rc == LY_EINCOMPLETE)) {
637 stored = 1;
638 }
639
640 if (ctx_node && (rc == LY_EINCOMPLETE)) {
641 /* resolve */
642 rc = type->plugin->validate(ctx, type, ctx_node, ctx_node, &val, &err);
643 }
644
645 if (rc && (rc != LY_EINCOMPLETE) && err) {
646 if (log) {
647 /* log error */
Michal Vasko7a266772024-01-23 11:02:38 +0100648 ly_err_print_build_path(ctx, ctx_node, schema, err);
Michal Vasko59892dd2022-05-13 11:02:30 +0200649 }
650 ly_err_free(err);
651 }
652
653 if (!rc || (rc == LY_EINCOMPLETE)) {
654 if (realtype) {
655 /* return realtype */
656 if (val.realtype->basetype == LY_TYPE_UNION) {
657 *realtype = val.subvalue->value.realtype;
658 } else {
659 *realtype = val.realtype;
660 }
661 }
662
663 if (canonical) {
664 /* return canonical value */
665 lydict_insert(ctx, val.realtype->plugin->print(ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL), 0, canonical);
666 }
667 }
668
669 if (stored) {
670 /* free value */
671 type->plugin->free(ctx ? ctx : schema->module->ctx, &val);
672 }
673 return rc;
674}
675
676LIBYANG_API_DEF LY_ERR
677lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len)
678{
679 LY_ERR ret = LY_SUCCESS;
680 struct ly_ctx *ctx;
681 struct lysc_type *type;
682 struct lyd_value val = {0};
683
684 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, value, LY_EINVAL);
685
686 ctx = node->schema->module->ctx;
687 type = ((struct lysc_node_leaf *)node->schema)->type;
688
689 /* store the value */
Michal Vasko7a266772024-01-23 11:02:38 +0100690 LOG_LOCSET(NULL, &node->node);
Michal Vasko989cdb42023-10-06 15:32:37 +0200691 ret = lyd_value_store(ctx, &val, type, value, value_len, 0, NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, node->schema, NULL);
Michal Vasko7a266772024-01-23 11:02:38 +0100692 LOG_LOCBACK(0, 1);
Michal Vasko59892dd2022-05-13 11:02:30 +0200693 LY_CHECK_RET(ret);
694
695 /* compare values */
aPiecek0a6705b2023-11-14 14:20:58 +0100696 ret = type->plugin->compare(ctx, &node->value, &val);
Michal Vasko59892dd2022-05-13 11:02:30 +0200697
698 type->plugin->free(ctx, &val);
699 return ret;
700}
701
702LIBYANG_API_DEF ly_bool
703lyd_is_default(const struct lyd_node *node)
704{
705 const struct lysc_node_leaf *leaf;
706 const struct lysc_node_leaflist *llist;
707 const struct lyd_node_term *term;
708 LY_ARRAY_COUNT_TYPE u;
709
710 if (!(node->schema->nodetype & LYD_NODE_TERM)) {
711 return 0;
712 }
713
714 term = (const struct lyd_node_term *)node;
715
716 if (node->schema->nodetype == LYS_LEAF) {
717 leaf = (const struct lysc_node_leaf *)node->schema;
718 if (!leaf->dflt) {
719 return 0;
720 }
721
722 /* compare with the default value */
aPiecek0a6705b2023-11-14 14:20:58 +0100723 if (!leaf->type->plugin->compare(LYD_CTX(node), &term->value, leaf->dflt)) {
Michal Vasko59892dd2022-05-13 11:02:30 +0200724 return 1;
725 }
726 } else {
727 llist = (const struct lysc_node_leaflist *)node->schema;
728 if (!llist->dflts) {
729 return 0;
730 }
731
732 LY_ARRAY_FOR(llist->dflts, u) {
733 /* compare with each possible default value */
aPiecek0a6705b2023-11-14 14:20:58 +0100734 if (!llist->type->plugin->compare(LYD_CTX(node), &term->value, llist->dflts[u])) {
Michal Vasko59892dd2022-05-13 11:02:30 +0200735 return 1;
736 }
737 }
738 }
739
740 return 0;
741}
742
743LIBYANG_API_DEF uint32_t
744lyd_list_pos(const struct lyd_node *instance)
745{
746 const struct lyd_node *iter = NULL;
747 uint32_t pos = 0;
748
749 if (!instance || !(instance->schema->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
750 return 0;
751 }
752
753 /* data instances are ordered, so we can stop when we found instance of other schema node */
754 for (iter = instance; iter->schema == instance->schema; iter = iter->prev) {
755 if (pos && (iter->next == NULL)) {
756 /* overrun to the end of the siblings list */
757 break;
758 }
759 ++pos;
760 }
761
762 return pos;
763}
764
765LIBYANG_API_DEF struct lyd_node *
766lyd_first_sibling(const struct lyd_node *node)
767{
768 struct lyd_node *start;
769
770 if (!node) {
771 return NULL;
772 }
773
774 /* get the first sibling */
775 if (node->parent) {
aPiecek743184b2024-02-01 13:25:56 +0100776 return node->parent->child;
777 } else if (!node->prev->next) {
778 return (struct lyd_node *)node;
779 }
780
781 for (start = (struct lyd_node *)node->prev; start->prev->next; start = start->prev) {
782 assert(start != node);
Michal Vasko59892dd2022-05-13 11:02:30 +0200783 }
784
785 return start;
786}
787
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100788/**
789 * @brief Check list node parsed into an opaque node for the reason.
790 *
791 * @param[in] node Opaque node.
792 * @param[in] snode Schema node of @p opaq.
793 * @return LY_SUCCESS if the node is valid;
794 * @return LY_ERR on error.
795 */
796static LY_ERR
797lyd_parse_opaq_list_error(const struct lyd_node *node, const struct lysc_node *snode)
798{
799 LY_ERR ret = LY_SUCCESS;
800 struct ly_set key_set = {0};
801 const struct lysc_node *key = NULL;
802 const struct lyd_node *child;
803 const struct lyd_node_opaq *opaq_k;
804 uint32_t i;
805
806 assert(!node->schema);
807
808 /* get all keys into a set */
Michal Vaskof30bcf12023-07-14 12:12:03 +0200809 while ((key = lys_getnext(key, snode, NULL, 0)) && (key->flags & LYS_KEY)) {
810 LY_CHECK_GOTO(ret = ly_set_add(&key_set, (void *)key, 1, NULL), cleanup);
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100811 }
812
813 LY_LIST_FOR(lyd_child(node), child) {
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100814 /* find the key schema node */
815 for (i = 0; i < key_set.count; ++i) {
816 key = key_set.snodes[i];
Michal Vaskoa878a892023-08-18 12:22:07 +0200817 if (!strcmp(key->name, LYD_NAME(child))) {
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100818 break;
819 }
820 }
821 if (i == key_set.count) {
822 /* some other node, skip */
823 continue;
824 }
825
826 /* key found */
827 ly_set_rm_index(&key_set, i, NULL);
828
Michal Vaskoa878a892023-08-18 12:22:07 +0200829 if (child->schema) {
830 /* valid key */
831 continue;
832 }
833
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100834 /* check value */
Michal Vaskoa878a892023-08-18 12:22:07 +0200835 opaq_k = (struct lyd_node_opaq *)child;
Michal Vasko583b4642023-05-25 10:39:34 +0200836 ret = ly_value_validate(LYD_CTX(node), key, opaq_k->value, strlen(opaq_k->value), opaq_k->format,
837 opaq_k->val_prefix_data, opaq_k->hints);
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100838 LY_CHECK_GOTO(ret, cleanup);
839 }
840
841 if (key_set.count) {
842 /* missing keys */
843 LOGVAL(LYD_CTX(node), LY_VCODE_NOKEY, key_set.snodes[0]->name);
844 ret = LY_EVALID;
845 goto cleanup;
846 }
847
848cleanup:
849 ly_set_erase(&key_set, NULL);
850 return ret;
851}
852
853LIBYANG_API_DEF LY_ERR
854lyd_parse_opaq_error(const struct lyd_node *node)
855{
Michal Vasko6727c682023-02-17 10:40:26 +0100856 LY_ERR rc = LY_SUCCESS;
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100857 const struct ly_ctx *ctx;
858 const struct lyd_node_opaq *opaq;
859 const struct lyd_node *parent;
860 const struct lys_module *mod;
Michal Vasko9d9b88d2023-08-21 11:55:43 +0200861 const struct lysc_node *sparent, *snode;
Michal Vasko7a266772024-01-23 11:02:38 +0100862 uint32_t loc_scnode = 0, loc_dnode = 0;
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100863
Michal Vasko9d9b88d2023-08-21 11:55:43 +0200864 LY_CHECK_ARG_RET(LYD_CTX(node), node, !node->schema, LY_EINVAL);
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100865
866 ctx = LYD_CTX(node);
867 opaq = (struct lyd_node_opaq *)node;
868 parent = lyd_parent(node);
Michal Vasko9d9b88d2023-08-21 11:55:43 +0200869 sparent = lyd_node_schema(parent);
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100870
Michal Vasko7a266772024-01-23 11:02:38 +0100871 /* if parent is NULL, it is still added as root */
872 LOG_LOCSET(NULL, parent);
873 loc_dnode = 1;
Michal Vasko6727c682023-02-17 10:40:26 +0100874
Michal Vaskof4e63922022-05-10 10:32:13 +0200875 if (!opaq->name.module_ns) {
876 LOGVAL(ctx, LYVE_REFERENCE, "Unknown module of node \"%s\".", opaq->name.name);
Michal Vasko6727c682023-02-17 10:40:26 +0100877 rc = LY_EVALID;
878 goto cleanup;
Michal Vaskof4e63922022-05-10 10:32:13 +0200879 }
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100880
881 /* module */
882 switch (opaq->format) {
883 case LY_VALUE_XML:
Michal Vasko9d9b88d2023-08-21 11:55:43 +0200884 if (!sparent || strcmp(opaq->name.module_ns, sparent->module->ns)) {
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100885 mod = ly_ctx_get_module_implemented_ns(ctx, opaq->name.module_ns);
886 if (!mod) {
Michal Vasko959f8d82022-06-16 07:51:50 +0200887 LOGVAL(ctx, LYVE_REFERENCE, "No (implemented) module with namespace \"%s\" of node \"%s\" in the context.",
888 opaq->name.module_ns, opaq->name.name);
Michal Vasko6727c682023-02-17 10:40:26 +0100889 rc = LY_EVALID;
890 goto cleanup;
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100891 }
892 } else {
893 /* inherit */
Michal Vasko9d9b88d2023-08-21 11:55:43 +0200894 mod = sparent->module;
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100895 }
896 break;
897 case LY_VALUE_JSON:
898 case LY_VALUE_LYB:
Michal Vasko9d9b88d2023-08-21 11:55:43 +0200899 if (!sparent || strcmp(opaq->name.module_name, sparent->module->name)) {
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100900 mod = ly_ctx_get_module_implemented(ctx, opaq->name.module_name);
901 if (!mod) {
Michal Vasko959f8d82022-06-16 07:51:50 +0200902 LOGVAL(ctx, LYVE_REFERENCE, "No (implemented) module named \"%s\" of node \"%s\" in the context.",
903 opaq->name.module_name, opaq->name.name);
Michal Vasko6727c682023-02-17 10:40:26 +0100904 rc = LY_EVALID;
905 goto cleanup;
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100906 }
907 } else {
908 /* inherit */
Michal Vasko9d9b88d2023-08-21 11:55:43 +0200909 mod = sparent->module;
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100910 }
911 break;
912 default:
913 LOGERR(ctx, LY_EINVAL, "Unsupported value format.");
Michal Vasko6727c682023-02-17 10:40:26 +0100914 rc = LY_EINVAL;
915 goto cleanup;
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100916 }
917
918 /* schema */
Michal Vasko9d9b88d2023-08-21 11:55:43 +0200919 snode = lys_find_child(sparent, mod, opaq->name.name, 0, 0, 0);
920 if (!snode && sparent && (sparent->nodetype & (LYS_RPC | LYS_ACTION))) {
Michal Vaskoac6f4be2022-05-02 10:16:50 +0200921 /* maybe output node */
Michal Vasko9d9b88d2023-08-21 11:55:43 +0200922 snode = lys_find_child(sparent, mod, opaq->name.name, 0, 0, LYS_GETNEXT_OUTPUT);
Michal Vaskoac6f4be2022-05-02 10:16:50 +0200923 }
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100924 if (!snode) {
Michal Vasko9d9b88d2023-08-21 11:55:43 +0200925 if (sparent) {
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100926 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%s\" not found as a child of \"%s\" node.", opaq->name.name,
Michal Vasko9d9b88d2023-08-21 11:55:43 +0200927 sparent->name);
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100928 } else {
929 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%s\" not found in the \"%s\" module.", opaq->name.name, mod->name);
930 }
Michal Vasko6727c682023-02-17 10:40:26 +0100931 rc = LY_EVALID;
932 goto cleanup;
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100933 }
934
Michal Vasko6727c682023-02-17 10:40:26 +0100935 /* schema node exists */
Michal Vasko7a266772024-01-23 11:02:38 +0100936 LOG_LOCBACK(0, 1);
937 loc_dnode = 0;
938 LOG_LOCSET(snode, NULL);
939 loc_scnode = 1;
Michal Vasko6727c682023-02-17 10:40:26 +0100940
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100941 if (snode->nodetype & LYD_NODE_TERM) {
942 /* leaf / leaf-list */
Michal Vasko583b4642023-05-25 10:39:34 +0200943 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 +0100944 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100945 } else if (snode->nodetype == LYS_LIST) {
946 /* list */
Michal Vasko6727c682023-02-17 10:40:26 +0100947 rc = lyd_parse_opaq_list_error(node, snode);
948 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100949 } else if (snode->nodetype & LYD_NODE_INNER) {
950 /* inner node */
951 if (opaq->value) {
952 LOGVAL(ctx, LYVE_DATA, "Invalid value \"%s\" for %s \"%s\".", opaq->value,
953 lys_nodetype2str(snode->nodetype), snode->name);
Michal Vasko6727c682023-02-17 10:40:26 +0100954 rc = LY_EVALID;
955 goto cleanup;
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100956 }
957 } else {
958 LOGERR(ctx, LY_EINVAL, "Unexpected opaque schema node %s \"%s\".", lys_nodetype2str(snode->nodetype), snode->name);
Michal Vasko6727c682023-02-17 10:40:26 +0100959 rc = LY_EINVAL;
960 goto cleanup;
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100961 }
962
963 LOGERR(ctx, LY_EINVAL, "Unexpected valid opaque node %s \"%s\".", lys_nodetype2str(snode->nodetype), snode->name);
Michal Vasko6727c682023-02-17 10:40:26 +0100964 rc = LY_EINVAL;
965
966cleanup:
Michal Vasko7a266772024-01-23 11:02:38 +0100967 LOG_LOCBACK(loc_scnode, loc_dnode);
Michal Vasko6727c682023-02-17 10:40:26 +0100968 return rc;
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100969}
970
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100971LIBYANG_API_DEF const char *
Christian Hopps46bd21b2021-04-27 09:43:58 -0400972lyd_value_get_canonical(const struct ly_ctx *ctx, const struct lyd_value *value)
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200973{
Michal Vaskoab40e7e2021-04-28 17:04:24 +0200974 LY_CHECK_ARG_RET(ctx, ctx, value, NULL);
975
Michal Vasko33876022021-04-27 16:42:24 +0200976 return value->_canonical ? value->_canonical :
977 (const char *)value->realtype->plugin->print(ctx, value, LY_VALUE_CANON, NULL, NULL, NULL);
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200978}
979
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100980LIBYANG_API_DEF LY_ERR
Michal Vaskoa820c312021-02-05 16:33:00 +0100981lyd_any_value_str(const struct lyd_node *any, char **value_str)
982{
983 const struct lyd_node_any *a;
984 struct lyd_node *tree = NULL;
985 const char *str = NULL;
986 ly_bool dynamic = 0;
987 LY_ERR ret = LY_SUCCESS;
988
989 LY_CHECK_ARG_RET(NULL, any, value_str, LY_EINVAL);
Radek Krejci71877df2021-04-06 17:24:06 +0200990 LY_CHECK_ARG_RET(NULL, any->schema, any->schema->nodetype & LYS_ANYDATA, LY_EINVAL);
Michal Vaskoa820c312021-02-05 16:33:00 +0100991
992 a = (struct lyd_node_any *)any;
993 *value_str = NULL;
994
995 if (!a->value.str) {
996 /* there is no value in the union */
997 return LY_SUCCESS;
998 }
999
1000 switch (a->value_type) {
1001 case LYD_ANYDATA_LYB:
1002 /* parse into a data tree */
1003 ret = lyd_parse_data_mem(LYD_CTX(any), a->value.mem, LYD_LYB, LYD_PARSE_ONLY, 0, &tree);
1004 LY_CHECK_GOTO(ret, cleanup);
1005 dynamic = 1;
1006 break;
1007 case LYD_ANYDATA_DATATREE:
1008 tree = a->value.tree;
1009 break;
1010 case LYD_ANYDATA_STRING:
1011 case LYD_ANYDATA_XML:
1012 case LYD_ANYDATA_JSON:
1013 /* simply use the string */
1014 str = a->value.str;
1015 break;
1016 }
1017
1018 if (tree) {
1019 /* print into a string */
1020 ret = lyd_print_mem(value_str, tree, LYD_XML, LYD_PRINT_WITHSIBLINGS);
1021 LY_CHECK_GOTO(ret, cleanup);
1022 } else {
1023 assert(str);
1024 *value_str = strdup(str);
1025 LY_CHECK_ERR_GOTO(!*value_str, LOGMEM(LYD_CTX(any)), cleanup);
1026 }
1027
1028 /* success */
1029
1030cleanup:
1031 if (dynamic) {
1032 lyd_free_all(tree);
1033 }
1034 return ret;
1035}
1036
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001037LIBYANG_API_DEF LY_ERR
Michal Vasko61551fa2020-07-09 15:45:45 +02001038lyd_any_copy_value(struct lyd_node *trg, const union lyd_any_value *value, LYD_ANYDATA_VALUETYPE value_type)
1039{
1040 struct lyd_node_any *t;
Michal Vasko61551fa2020-07-09 15:45:45 +02001041
Michal Vaskoa820c312021-02-05 16:33:00 +01001042 LY_CHECK_ARG_RET(NULL, trg, LY_EINVAL);
Radek Krejci71877df2021-04-06 17:24:06 +02001043 LY_CHECK_ARG_RET(NULL, trg->schema, trg->schema->nodetype & LYS_ANYDATA, LY_EINVAL);
Michal Vasko61551fa2020-07-09 15:45:45 +02001044
1045 t = (struct lyd_node_any *)trg;
1046
1047 /* free trg */
1048 switch (t->value_type) {
1049 case LYD_ANYDATA_DATATREE:
1050 lyd_free_all(t->value.tree);
1051 break;
1052 case LYD_ANYDATA_STRING:
1053 case LYD_ANYDATA_XML:
1054 case LYD_ANYDATA_JSON:
Michal Vaskoe180ed02021-02-05 16:31:20 +01001055 lydict_remove(LYD_CTX(trg), t->value.str);
Michal Vasko61551fa2020-07-09 15:45:45 +02001056 break;
1057 case LYD_ANYDATA_LYB:
1058 free(t->value.mem);
1059 break;
1060 }
1061 t->value.str = NULL;
1062
1063 if (!value) {
1064 /* only free value in this case */
1065 return LY_SUCCESS;
1066 }
1067
1068 /* copy src */
1069 t->value_type = value_type;
1070 switch (value_type) {
1071 case LYD_ANYDATA_DATATREE:
1072 if (value->tree) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02001073 LY_CHECK_RET(lyd_dup_siblings(value->tree, NULL, LYD_DUP_RECURSIVE, &t->value.tree));
Michal Vasko61551fa2020-07-09 15:45:45 +02001074 }
1075 break;
1076 case LYD_ANYDATA_STRING:
1077 case LYD_ANYDATA_XML:
1078 case LYD_ANYDATA_JSON:
1079 if (value->str) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001080 LY_CHECK_RET(lydict_insert(LYD_CTX(trg), value->str, 0, &t->value.str));
Michal Vasko61551fa2020-07-09 15:45:45 +02001081 }
1082 break;
1083 case LYD_ANYDATA_LYB:
1084 if (value->mem) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02001085 int len = lyd_lyb_data_length(value->mem);
Michal Vasko26bbb272022-08-02 14:54:33 +02001086
Radek Krejci82fa8d42020-07-11 22:00:59 +02001087 LY_CHECK_RET(len == -1, LY_EINVAL);
Michal Vasko61551fa2020-07-09 15:45:45 +02001088 t->value.mem = malloc(len);
Michal Vaskob7be7a82020-08-20 09:09:04 +02001089 LY_CHECK_ERR_RET(!t->value.mem, LOGMEM(LYD_CTX(trg)), LY_EMEM);
Michal Vasko61551fa2020-07-09 15:45:45 +02001090 memcpy(t->value.mem, value->mem, len);
1091 }
1092 break;
1093 }
1094
1095 return LY_SUCCESS;
1096}
1097
Michal Vasko21c11c22023-10-09 16:06:58 +02001098LIBYANG_API_DEF const struct lysc_node *
Michal Vasko106f0862021-11-02 11:49:27 +01001099lyd_node_schema(const struct lyd_node *node)
1100{
1101 const struct lysc_node *schema = NULL;
1102 const struct lyd_node *prev_iter = NULL, *iter;
1103 const struct lys_module *mod;
1104
1105 if (!node) {
1106 return NULL;
1107 } else if (node->schema) {
1108 return node->schema;
1109 }
1110
Michal Vaskoa878a892023-08-18 12:22:07 +02001111 /* find the first schema node in the parents */
1112 for (iter = lyd_parent(node); iter && !iter->schema; iter = lyd_parent(iter)) {}
1113 if (iter) {
1114 prev_iter = iter;
1115 schema = prev_iter->schema;
1116 }
1117
Michal Vasko106f0862021-11-02 11:49:27 +01001118 /* get schema node of an opaque node */
1119 do {
1120 /* get next data node */
1121 for (iter = node; lyd_parent(iter) != prev_iter; iter = lyd_parent(iter)) {}
1122
Michal Vaskoa878a892023-08-18 12:22:07 +02001123 /* get module */
Michal Vasko420cc252023-08-24 08:14:24 +02001124 mod = lyd_node_module(iter);
Michal Vaskoa878a892023-08-18 12:22:07 +02001125 if (!mod) {
1126 /* unknown module, no schema node */
1127 schema = NULL;
1128 break;
Michal Vasko106f0862021-11-02 11:49:27 +01001129 }
Michal Vaskod2f404f2021-11-04 15:37:11 +01001130
Michal Vaskoa878a892023-08-18 12:22:07 +02001131 /* get schema node */
1132 schema = lys_find_child(schema, mod, LYD_NAME(iter), 0, 0, 0);
1133
1134 /* move to the descendant */
Michal Vaskod2f404f2021-11-04 15:37:11 +01001135 prev_iter = iter;
Michal Vasko106f0862021-11-02 11:49:27 +01001136 } while (schema && (iter != node));
1137
1138 return schema;
1139}
1140
Michal Vasko4754d4a2022-12-01 10:11:21 +01001141void
1142lyd_cont_set_dflt(struct lyd_node *node)
1143{
1144 const struct lyd_node *child;
1145
1146 while (node) {
1147 if (!node->schema || (node->flags & LYD_DEFAULT) || !lysc_is_np_cont(node->schema)) {
1148 /* not a non-dflt NP container */
1149 break;
1150 }
1151
1152 LY_LIST_FOR(lyd_child(node), child) {
1153 if (!(child->flags & LYD_DEFAULT)) {
1154 break;
1155 }
1156 }
1157 if (child) {
1158 /* explicit child, no dflt change */
1159 break;
1160 }
1161
1162 /* set the dflt flag */
1163 node->flags |= LYD_DEFAULT;
1164
1165 /* check all parent containers */
1166 node = lyd_parent(node);
1167 }
1168}
1169
Michal Vasko59892dd2022-05-13 11:02:30 +02001170/**
1171 * @brief Comparison callback to match schema node with a schema of a data node.
1172 *
1173 * @param[in] val1_p Pointer to the schema node
1174 * @param[in] val2_p Pointer to the data node
1175 * Implementation of ::lyht_value_equal_cb.
1176 */
1177static ly_bool
1178lyd_hash_table_schema_val_equal(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
1179{
1180 struct lysc_node *val1;
1181 struct lyd_node *val2;
1182
1183 val1 = *((struct lysc_node **)val1_p);
1184 val2 = *((struct lyd_node **)val2_p);
1185
1186 if (val1 == val2->schema) {
1187 /* schema match is enough */
1188 return 1;
1189 } else {
1190 return 0;
1191 }
1192}
1193
1194LY_ERR
1195lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match)
1196{
1197 struct lyd_node **match_p;
1198 struct lyd_node_inner *parent;
1199 uint32_t hash;
Michal Vasko59892dd2022-05-13 11:02:30 +02001200
Michal Vasko21beaeb2022-08-02 10:42:48 +02001201 assert(schema);
1202 if (!siblings) {
1203 /* no data */
1204 if (match) {
1205 *match = NULL;
1206 }
1207 return LY_ENOTFOUND;
1208 }
Michal Vasko59892dd2022-05-13 11:02:30 +02001209
1210 parent = siblings->parent;
1211 if (parent && parent->schema && parent->children_ht) {
1212 /* calculate our hash */
Michal Vaskoae130f52023-04-20 14:25:16 +02001213 hash = lyht_hash_multi(0, schema->module->name, strlen(schema->module->name));
1214 hash = lyht_hash_multi(hash, schema->name, strlen(schema->name));
1215 hash = lyht_hash_multi(hash, NULL, 0);
Michal Vasko59892dd2022-05-13 11:02:30 +02001216
Michal Vasko87d6bf62023-10-23 10:05:43 +02001217 /* find by hash but use special hash table function (and stay thread-safe) */
1218 if (!lyht_find_with_val_cb(parent->children_ht, &schema, hash, lyd_hash_table_schema_val_equal, (void **)&match_p)) {
Michal Vasko59892dd2022-05-13 11:02:30 +02001219 siblings = *match_p;
1220 } else {
1221 /* not found */
1222 siblings = NULL;
1223 }
Michal Vasko59892dd2022-05-13 11:02:30 +02001224 } else {
1225 /* find first sibling */
1226 if (siblings->parent) {
1227 siblings = siblings->parent->child;
1228 } else {
1229 while (siblings->prev->next) {
1230 siblings = siblings->prev;
1231 }
1232 }
1233
Michal Vaskod8a52012023-08-15 11:38:10 +02001234 /* search manually without hashes and ignore opaque nodes (cannot be found by hashes) */
1235 for ( ; siblings && siblings->schema; siblings = siblings->next) {
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001236 /* schema match is enough */
Michal Vaskod8a52012023-08-15 11:38:10 +02001237 if (LYD_CTX(siblings) == schema->module->ctx) {
1238 if (siblings->schema == schema) {
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001239 break;
1240 }
1241 } else {
Michal Vaskod8a52012023-08-15 11:38:10 +02001242 if (!strcmp(LYD_NAME(siblings), schema->name) && !strcmp(siblings->schema->module->name, schema->module->name)) {
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001243 break;
1244 }
Michal Vasko59892dd2022-05-13 11:02:30 +02001245 }
1246 }
Michal Vaskod8a52012023-08-15 11:38:10 +02001247 if (siblings && !siblings->schema) {
1248 siblings = NULL;
1249 }
Michal Vasko59892dd2022-05-13 11:02:30 +02001250 }
1251
1252 if (!siblings) {
1253 if (match) {
1254 *match = NULL;
1255 }
1256 return LY_ENOTFOUND;
1257 }
1258
1259 if (match) {
1260 *match = (struct lyd_node *)siblings;
1261 }
1262 return LY_SUCCESS;
1263}
1264
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001265void
Michal Vaskod3bb12f2020-12-04 14:33:09 +01001266lyd_del_move_root(struct lyd_node **root, const struct lyd_node *to_del, const struct lys_module *mod)
1267{
1268 if (*root && (lyd_owner_module(*root) != mod)) {
1269 /* there are no data of mod so this is simply the first top-level sibling */
1270 mod = NULL;
1271 }
1272
1273 if ((*root != to_del) || (*root)->parent) {
1274 return;
1275 }
1276
Michal Vasko598063b2021-07-19 11:39:05 +02001277 if (mod && (*root)->prev->next && (!(*root)->next || (lyd_owner_module(to_del) != lyd_owner_module((*root)->next)))) {
1278 /* there are no more nodes from mod, simply get the first top-level sibling */
Michal Vaskod3bb12f2020-12-04 14:33:09 +01001279 *root = lyd_first_sibling(*root);
Michal Vasko598063b2021-07-19 11:39:05 +02001280 } else {
1281 *root = (*root)->next;
Michal Vaskod3bb12f2020-12-04 14:33:09 +01001282 }
1283}
1284
Michal Vasko8cc3f662022-03-29 11:25:51 +02001285LY_ERR
1286ly_nested_ext_schema(const struct lyd_node *parent, const struct lysc_node *sparent, const char *prefix,
1287 size_t prefix_len, LY_VALUE_FORMAT format, void *prefix_data, const char *name, size_t name_len,
1288 const struct lysc_node **snode, struct lysc_ext_instance **ext)
1289{
1290 LY_ERR r;
1291 LY_ARRAY_COUNT_TYPE u;
1292 struct lysc_ext_instance *nested_exts = NULL;
1293 lyplg_ext_data_snode_clb ext_snode_cb;
1294
1295 /* check if there are any nested extension instances */
1296 if (parent && parent->schema) {
1297 nested_exts = parent->schema->exts;
1298 } else if (sparent) {
1299 nested_exts = sparent->exts;
1300 }
1301 LY_ARRAY_FOR(nested_exts, u) {
Michal Vasko305c6cb2022-04-27 10:33:04 +02001302 if (!nested_exts[u].def->plugin) {
1303 /* no plugin */
1304 continue;
1305 }
1306
Michal Vasko8cc3f662022-03-29 11:25:51 +02001307 ext_snode_cb = nested_exts[u].def->plugin->snode;
1308 if (!ext_snode_cb) {
1309 /* not an extension with nested data */
1310 continue;
1311 }
1312
1313 /* try to get the schema node */
1314 r = ext_snode_cb(&nested_exts[u], parent, sparent, prefix, prefix_len, format, prefix_data, name, name_len, snode);
1315 if (!r) {
Michal Vasko66330fc2022-11-21 15:52:24 +01001316 if (ext) {
1317 /* data successfully created, remember the ext instance */
1318 *ext = &nested_exts[u];
1319 }
Michal Vasko8cc3f662022-03-29 11:25:51 +02001320 return LY_SUCCESS;
1321 } else if (r != LY_ENOT) {
1322 /* fatal error */
1323 return r;
1324 }
1325 /* data was not from this module, continue */
1326 }
1327
1328 /* no extensions or none matched */
1329 return LY_ENOT;
1330}
1331
Michal Vaskod3bb12f2020-12-04 14:33:09 +01001332void
Radek Krejci8df109d2021-04-23 12:19:08 +02001333ly_free_prefix_data(LY_VALUE_FORMAT format, void *prefix_data)
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001334{
1335 struct ly_set *ns_list;
1336 struct lysc_prefix *prefixes;
1337 uint32_t i;
1338 LY_ARRAY_COUNT_TYPE u;
1339
1340 if (!prefix_data) {
1341 return;
1342 }
1343
1344 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001345 case LY_VALUE_XML:
Michal Vaskoddd76592022-01-17 13:34:48 +01001346 case LY_VALUE_STR_NS:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001347 ns_list = prefix_data;
1348 for (i = 0; i < ns_list->count; ++i) {
1349 free(((struct lyxml_ns *)ns_list->objs[i])->prefix);
1350 free(((struct lyxml_ns *)ns_list->objs[i])->uri);
1351 }
1352 ly_set_free(ns_list, free);
1353 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02001354 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001355 prefixes = prefix_data;
1356 LY_ARRAY_FOR(prefixes, u) {
1357 free(prefixes[u].prefix);
1358 }
1359 LY_ARRAY_FREE(prefixes);
1360 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02001361 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02001362 case LY_VALUE_SCHEMA:
1363 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02001364 case LY_VALUE_LYB:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001365 break;
1366 }
1367}
1368
1369LY_ERR
Radek Krejci8df109d2021-04-23 12:19:08 +02001370ly_dup_prefix_data(const struct ly_ctx *ctx, LY_VALUE_FORMAT format, const void *prefix_data,
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001371 void **prefix_data_p)
1372{
1373 LY_ERR ret = LY_SUCCESS;
1374 struct lyxml_ns *ns;
1375 struct lysc_prefix *prefixes = NULL, *orig_pref;
1376 struct ly_set *ns_list, *orig_ns;
1377 uint32_t i;
1378 LY_ARRAY_COUNT_TYPE u;
1379
1380 assert(!*prefix_data_p);
1381
1382 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001383 case LY_VALUE_SCHEMA:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001384 *prefix_data_p = (void *)prefix_data;
1385 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02001386 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001387 /* copy all the value prefixes */
1388 orig_pref = (struct lysc_prefix *)prefix_data;
1389 LY_ARRAY_CREATE_GOTO(ctx, prefixes, LY_ARRAY_COUNT(orig_pref), ret, cleanup);
1390 *prefix_data_p = prefixes;
1391
1392 LY_ARRAY_FOR(orig_pref, u) {
1393 if (orig_pref[u].prefix) {
1394 prefixes[u].prefix = strdup(orig_pref[u].prefix);
1395 LY_CHECK_ERR_GOTO(!prefixes[u].prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1396 }
1397 prefixes[u].mod = orig_pref[u].mod;
1398 LY_ARRAY_INCREMENT(prefixes);
1399 }
1400 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02001401 case LY_VALUE_XML:
Michal Vaskoddd76592022-01-17 13:34:48 +01001402 case LY_VALUE_STR_NS:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001403 /* copy all the namespaces */
1404 LY_CHECK_GOTO(ret = ly_set_new(&ns_list), cleanup);
1405 *prefix_data_p = ns_list;
1406
1407 orig_ns = (struct ly_set *)prefix_data;
1408 for (i = 0; i < orig_ns->count; ++i) {
1409 ns = calloc(1, sizeof *ns);
1410 LY_CHECK_ERR_GOTO(!ns, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1411 LY_CHECK_GOTO(ret = ly_set_add(ns_list, ns, 1, NULL), cleanup);
1412
1413 if (((struct lyxml_ns *)orig_ns->objs[i])->prefix) {
1414 ns->prefix = strdup(((struct lyxml_ns *)orig_ns->objs[i])->prefix);
1415 LY_CHECK_ERR_GOTO(!ns->prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1416 }
1417 ns->uri = strdup(((struct lyxml_ns *)orig_ns->objs[i])->uri);
1418 LY_CHECK_ERR_GOTO(!ns->uri, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1419 }
1420 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02001421 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02001422 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02001423 case LY_VALUE_LYB:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001424 assert(!prefix_data);
1425 *prefix_data_p = NULL;
1426 break;
1427 }
1428
1429cleanup:
1430 if (ret) {
1431 ly_free_prefix_data(format, *prefix_data_p);
1432 *prefix_data_p = NULL;
1433 }
1434 return ret;
1435}
1436
1437LY_ERR
Radek Krejcif9943642021-04-26 10:18:21 +02001438ly_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 +02001439 const void *prefix_data, LY_VALUE_FORMAT *format_p, void **prefix_data_p)
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001440{
1441 LY_ERR ret = LY_SUCCESS;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001442 const struct lys_module *mod;
Michal Vaskofc2cd072021-02-24 13:17:17 +01001443 const struct lyxml_ns *ns;
1444 struct lyxml_ns *new_ns;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001445 struct ly_set *ns_list;
1446 struct lysc_prefix *prefixes = NULL, *val_pref;
aPiecek83436bc2021-03-30 12:20:45 +02001447 const char *value_iter, *value_next, *value_end;
1448 uint32_t substr_len;
1449 ly_bool is_prefix;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001450
1451 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001452 case LY_VALUE_SCHEMA:
Michal Vaskofc2cd072021-02-24 13:17:17 +01001453 /* copy all referenced modules as prefix - module pairs */
1454 if (!*prefix_data_p) {
1455 /* new prefix data */
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001456 LY_ARRAY_CREATE_GOTO(ctx, prefixes, 0, ret, cleanup);
Radek Krejci8df109d2021-04-23 12:19:08 +02001457 *format_p = LY_VALUE_SCHEMA_RESOLVED;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001458 *prefix_data_p = prefixes;
Michal Vaskofc2cd072021-02-24 13:17:17 +01001459 } else {
1460 /* reuse prefix data */
Radek Krejci8df109d2021-04-23 12:19:08 +02001461 assert(*format_p == LY_VALUE_SCHEMA_RESOLVED);
Michal Vaskofc2cd072021-02-24 13:17:17 +01001462 prefixes = *prefix_data_p;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001463 }
1464
Michal Vaskoc0f9c4c2022-05-06 12:12:17 +02001465 /* add current module for unprefixed values */
1466 LY_ARRAY_NEW_GOTO(ctx, prefixes, val_pref, ret, cleanup);
1467 *prefix_data_p = prefixes;
1468
1469 val_pref->prefix = NULL;
1470 val_pref->mod = ((const struct lysp_module *)prefix_data)->mod;
1471
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001472 /* add all used prefixes */
Michal Vasko59e90fc2021-09-22 12:17:08 +02001473 value_end = (char *)value + value_len;
aPiecek83436bc2021-03-30 12:20:45 +02001474 for (value_iter = value; value_iter; value_iter = value_next) {
aPieceke3f828d2021-05-10 15:34:41 +02001475 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 +02001476 if (is_prefix) {
1477 /* we have a possible prefix. Do we already have the prefix? */
1478 mod = ly_resolve_prefix(ctx, value_iter, substr_len, *format_p, *prefix_data_p);
1479 if (!mod) {
1480 mod = ly_resolve_prefix(ctx, value_iter, substr_len, format, prefix_data);
1481 if (mod) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001482 assert(*format_p == LY_VALUE_SCHEMA_RESOLVED);
aPiecek83436bc2021-03-30 12:20:45 +02001483 /* store a new prefix - module pair */
1484 LY_ARRAY_NEW_GOTO(ctx, prefixes, val_pref, ret, cleanup);
1485 *prefix_data_p = prefixes;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001486
aPiecek83436bc2021-03-30 12:20:45 +02001487 val_pref->prefix = strndup(value_iter, substr_len);
1488 LY_CHECK_ERR_GOTO(!val_pref->prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1489 val_pref->mod = mod;
1490 } /* else it is not even defined */
1491 } /* else the prefix is already present */
Michal Vaskofc2cd072021-02-24 13:17:17 +01001492 }
1493 }
1494 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02001495 case LY_VALUE_XML:
Michal Vaskoddd76592022-01-17 13:34:48 +01001496 case LY_VALUE_STR_NS:
Michal Vaskofc2cd072021-02-24 13:17:17 +01001497 /* copy all referenced namespaces as prefix - namespace pairs */
1498 if (!*prefix_data_p) {
1499 /* new prefix data */
1500 LY_CHECK_GOTO(ret = ly_set_new(&ns_list), cleanup);
Michal Vaskoddd76592022-01-17 13:34:48 +01001501 *format_p = format;
Michal Vaskofc2cd072021-02-24 13:17:17 +01001502 *prefix_data_p = ns_list;
1503 } else {
1504 /* reuse prefix data */
Michal Vaskoddd76592022-01-17 13:34:48 +01001505 assert(*format_p == format);
Michal Vaskofc2cd072021-02-24 13:17:17 +01001506 ns_list = *prefix_data_p;
1507 }
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001508
Michal Vasko294e7f02022-02-28 13:59:00 +01001509 /* store default namespace */
1510 ns = lyxml_ns_get(prefix_data, NULL, 0);
1511 if (ns) {
1512 new_ns = calloc(1, sizeof *new_ns);
1513 LY_CHECK_ERR_GOTO(!new_ns, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1514 LY_CHECK_GOTO(ret = ly_set_add(ns_list, new_ns, 1, NULL), cleanup);
1515
1516 new_ns->prefix = NULL;
1517 new_ns->uri = strdup(ns->uri);
1518 LY_CHECK_ERR_GOTO(!new_ns->uri, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1519 }
1520
Michal Vaskofc2cd072021-02-24 13:17:17 +01001521 /* add all used prefixes */
Michal Vasko59e90fc2021-09-22 12:17:08 +02001522 value_end = (char *)value + value_len;
aPiecek83436bc2021-03-30 12:20:45 +02001523 for (value_iter = value; value_iter; value_iter = value_next) {
aPieceke3f828d2021-05-10 15:34:41 +02001524 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 +02001525 if (is_prefix) {
1526 /* we have a possible prefix. Do we already have the prefix? */
1527 ns = lyxml_ns_get(ns_list, value_iter, substr_len);
1528 if (!ns) {
1529 ns = lyxml_ns_get(prefix_data, value_iter, substr_len);
1530 if (ns) {
1531 /* store a new prefix - namespace pair */
1532 new_ns = calloc(1, sizeof *new_ns);
1533 LY_CHECK_ERR_GOTO(!new_ns, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1534 LY_CHECK_GOTO(ret = ly_set_add(ns_list, new_ns, 1, NULL), cleanup);
Michal Vaskofc2cd072021-02-24 13:17:17 +01001535
aPiecek83436bc2021-03-30 12:20:45 +02001536 new_ns->prefix = strndup(value_iter, substr_len);
1537 LY_CHECK_ERR_GOTO(!new_ns->prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1538 new_ns->uri = strdup(ns->uri);
1539 LY_CHECK_ERR_GOTO(!new_ns->uri, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1540 } /* else it is not even defined */
1541 } /* else the prefix is already present */
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001542 }
1543 }
1544 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02001545 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02001546 case LY_VALUE_SCHEMA_RESOLVED:
1547 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02001548 case LY_VALUE_LYB:
Michal Vaskofc2cd072021-02-24 13:17:17 +01001549 if (!*prefix_data_p) {
1550 /* new prefix data - simply copy all the prefix data */
1551 *format_p = format;
1552 LY_CHECK_GOTO(ret = ly_dup_prefix_data(ctx, format, prefix_data, prefix_data_p), cleanup);
1553 } /* else reuse prefix data - the prefix data are always the same, nothing to do */
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001554 break;
1555 }
1556
1557cleanup:
1558 if (ret) {
1559 ly_free_prefix_data(*format_p, *prefix_data_p);
1560 *prefix_data_p = NULL;
1561 }
1562 return ret;
1563}
Michal Vasko7ed1fcb2020-12-03 14:15:22 +01001564
1565const char *
Radek Krejci8df109d2021-04-23 12:19:08 +02001566ly_format2str(LY_VALUE_FORMAT format)
Michal Vasko7ed1fcb2020-12-03 14:15:22 +01001567{
1568 switch (format) {
Radek Krejci224d4b42021-04-23 13:54:59 +02001569 case LY_VALUE_CANON:
1570 return "canonical";
Radek Krejci8df109d2021-04-23 12:19:08 +02001571 case LY_VALUE_SCHEMA:
Michal Vasko7ed1fcb2020-12-03 14:15:22 +01001572 return "schema imports";
Radek Krejci8df109d2021-04-23 12:19:08 +02001573 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko7ed1fcb2020-12-03 14:15:22 +01001574 return "schema stored mapping";
Radek Krejci8df109d2021-04-23 12:19:08 +02001575 case LY_VALUE_XML:
Michal Vasko7ed1fcb2020-12-03 14:15:22 +01001576 return "XML prefixes";
Radek Krejci8df109d2021-04-23 12:19:08 +02001577 case LY_VALUE_JSON:
Michal Vasko7ed1fcb2020-12-03 14:15:22 +01001578 return "JSON module names";
Radek Krejcif9943642021-04-26 10:18:21 +02001579 case LY_VALUE_LYB:
1580 return "LYB prefixes";
Michal Vasko7ed1fcb2020-12-03 14:15:22 +01001581 default:
1582 break;
1583 }
1584
1585 return NULL;
1586}
Michal Vasko43297a02021-05-19 11:12:37 +02001587
Michal Vaskof17bb2a2023-02-10 11:34:55 +01001588LIBYANG_API_DEF int
1589ly_time_tz_offset(void)
1590{
Michal Vasko9b560332023-04-24 15:43:56 +02001591 return ly_time_tz_offset_at(time(NULL));
1592}
1593
1594LIBYANG_API_DEF int
1595ly_time_tz_offset_at(time_t time)
1596{
Michal Vaskof17bb2a2023-02-10 11:34:55 +01001597 struct tm tm_local, tm_utc;
1598 int result = 0;
1599
1600 /* init timezone */
1601 tzset();
1602
1603 /* get local and UTC time */
Michal Vasko9b560332023-04-24 15:43:56 +02001604 localtime_r(&time, &tm_local);
1605 gmtime_r(&time, &tm_utc);
Michal Vaskoe50b98d2023-03-28 11:39:01 +02001606
Michal Vasko9b560332023-04-24 15:43:56 +02001607 /* account for year/month/day change by adding/subtracting from the hours, the change cannot be more than 1 day */
1608 if (tm_local.tm_year < tm_utc.tm_year) {
1609 tm_utc.tm_hour += 24;
1610 } else if (tm_local.tm_year > tm_utc.tm_year) {
1611 tm_local.tm_hour += 24;
1612 } else if (tm_local.tm_mon < tm_utc.tm_mon) {
1613 tm_utc.tm_hour += 24;
1614 } else if (tm_local.tm_mon > tm_utc.tm_mon) {
1615 tm_local.tm_hour += 24;
1616 } else if (tm_local.tm_mday < tm_utc.tm_mday) {
1617 tm_utc.tm_hour += 24;
1618 } else if (tm_local.tm_mday > tm_utc.tm_mday) {
1619 tm_local.tm_hour += 24;
Michal Vaskoe50b98d2023-03-28 11:39:01 +02001620 }
Michal Vaskof17bb2a2023-02-10 11:34:55 +01001621
1622 /* hours shift in seconds */
1623 result += (tm_local.tm_hour - tm_utc.tm_hour) * 3600;
1624
1625 /* minutes shift in seconds */
1626 result += (tm_local.tm_min - tm_utc.tm_min) * 60;
1627
1628 /* seconds shift */
1629 result += tm_local.tm_sec - tm_utc.tm_sec;
1630
1631 return result;
1632}
1633
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001634LIBYANG_API_DEF LY_ERR
Michal Vasko43297a02021-05-19 11:12:37 +02001635ly_time_str2time(const char *value, time_t *time, char **fractions_s)
1636{
1637 struct tm tm = {0};
1638 uint32_t i, frac_len;
1639 const char *frac;
1640 int64_t shift, shift_m;
1641 time_t t;
1642
1643 LY_CHECK_ARG_RET(NULL, value, time, LY_EINVAL);
1644
1645 tm.tm_year = atoi(&value[0]) - 1900;
1646 tm.tm_mon = atoi(&value[5]) - 1;
1647 tm.tm_mday = atoi(&value[8]);
1648 tm.tm_hour = atoi(&value[11]);
1649 tm.tm_min = atoi(&value[14]);
1650 tm.tm_sec = atoi(&value[17]);
1651
Michal Vasko2b7e1612023-08-08 13:37:34 +02001652 /* explicit checks for some gross errors */
1653 if (tm.tm_mon > 11) {
1654 LOGERR(NULL, LY_EINVAL, "Invalid date-and-time month \"%d\".", tm.tm_mon);
1655 return LY_EINVAL;
1656 }
1657 if ((tm.tm_mday < 1) || (tm.tm_mday > 31)) {
1658 LOGERR(NULL, LY_EINVAL, "Invalid date-and-time day of month \"%d\".", tm.tm_mday);
1659 return LY_EINVAL;
1660 }
1661 if (tm.tm_hour > 23) {
1662 LOGERR(NULL, LY_EINVAL, "Invalid date-and-time hours \"%d\".", tm.tm_hour);
1663 return LY_EINVAL;
1664 }
1665 if (tm.tm_min > 59) {
1666 LOGERR(NULL, LY_EINVAL, "Invalid date-and-time minutes \"%d\".", tm.tm_min);
1667 return LY_EINVAL;
1668 }
1669 if (tm.tm_sec > 60) {
1670 LOGERR(NULL, LY_EINVAL, "Invalid date-and-time seconds \"%d\".", tm.tm_sec);
1671 return LY_EINVAL;
1672 }
1673
Michal Vasko43297a02021-05-19 11:12:37 +02001674 t = timegm(&tm);
1675 i = 19;
1676
1677 /* fractions of a second */
1678 if (value[i] == '.') {
1679 ++i;
1680 frac = &value[i];
1681 for (frac_len = 0; isdigit(frac[frac_len]); ++frac_len) {}
1682
1683 i += frac_len;
Michal Vasko43297a02021-05-19 11:12:37 +02001684 } else {
1685 frac = NULL;
1686 }
1687
1688 /* apply offset */
1689 if ((value[i] == 'Z') || (value[i] == 'z')) {
1690 /* zero shift */
1691 shift = 0;
1692 } else {
1693 shift = strtol(&value[i], NULL, 10);
Michal Vasko2b7e1612023-08-08 13:37:34 +02001694 if (shift > 23) {
1695 LOGERR(NULL, LY_EINVAL, "Invalid date-and-time timezone hour \"%" PRIi64 "\".", shift);
1696 return LY_EINVAL;
1697 }
Michal Vasko43297a02021-05-19 11:12:37 +02001698 shift = shift * 60 * 60; /* convert from hours to seconds */
Michal Vasko2b7e1612023-08-08 13:37:34 +02001699
1700 shift_m = strtol(&value[i + 4], NULL, 10);
1701 if (shift_m > 59) {
1702 LOGERR(NULL, LY_EINVAL, "Invalid date-and-time timezone minutes \"%" PRIi64 "\".", shift_m);
1703 return LY_EINVAL;
1704 }
1705 shift_m *= 60; /* convert from minutes to seconds */
1706
Michal Vasko43297a02021-05-19 11:12:37 +02001707 /* correct sign */
1708 if (shift < 0) {
1709 shift_m *= -1;
1710 }
Michal Vasko2b7e1612023-08-08 13:37:34 +02001711
Michal Vasko43297a02021-05-19 11:12:37 +02001712 /* connect hours and minutes of the shift */
1713 shift = shift + shift_m;
1714 }
1715
1716 /* we have to shift to the opposite way to correct the time */
1717 t -= shift;
1718
1719 *time = t;
1720 if (fractions_s) {
1721 if (frac) {
1722 *fractions_s = strndup(frac, frac_len);
1723 LY_CHECK_RET(!*fractions_s, LY_EMEM);
1724 } else {
1725 *fractions_s = NULL;
1726 }
1727 }
1728 return LY_SUCCESS;
1729}
1730
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001731LIBYANG_API_DEF LY_ERR
Michal Vasko43297a02021-05-19 11:12:37 +02001732ly_time_time2str(time_t time, const char *fractions_s, char **str)
1733{
1734 struct tm tm;
Michal Vaskof17bb2a2023-02-10 11:34:55 +01001735 char zoneshift[12];
1736 int zonediff_s, zonediff_h, zonediff_m;
Michal Vasko43297a02021-05-19 11:12:37 +02001737
1738 LY_CHECK_ARG_RET(NULL, str, LY_EINVAL);
1739
Michal Vaskof17bb2a2023-02-10 11:34:55 +01001740 /* init timezone */
Michal Vasko43297a02021-05-19 11:12:37 +02001741 tzset();
1742
1743 /* convert */
1744 if (!localtime_r(&time, &tm)) {
1745 return LY_ESYS;
1746 }
1747
Michal Vaskoe50b98d2023-03-28 11:39:01 +02001748 /* get timezone offset (do not use tm_gmtoff to avoid portability problems) */
Michal Vasko9b560332023-04-24 15:43:56 +02001749 zonediff_s = ly_time_tz_offset_at(time);
Michal Vaskof17bb2a2023-02-10 11:34:55 +01001750 zonediff_h = zonediff_s / 60 / 60;
1751 zonediff_m = zonediff_s / 60 % 60;
Michal Vasko43297a02021-05-19 11:12:37 +02001752 sprintf(zoneshift, "%+03d:%02d", zonediff_h, zonediff_m);
1753
1754 /* print */
1755 if (asprintf(str, "%04d-%02d-%02dT%02d:%02d:%02d%s%s%s",
1756 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
1757 fractions_s ? "." : "", fractions_s ? fractions_s : "", zoneshift) == -1) {
1758 return LY_EMEM;
1759 }
1760
1761 return LY_SUCCESS;
1762}
1763
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001764LIBYANG_API_DEF LY_ERR
Michal Vasko43297a02021-05-19 11:12:37 +02001765ly_time_str2ts(const char *value, struct timespec *ts)
1766{
1767 LY_ERR rc;
Michal Vasko72975062021-08-25 08:13:04 +02001768 char *fractions_s, frac_buf[10];
Michal Vasko43297a02021-05-19 11:12:37 +02001769 int frac_len;
1770
1771 LY_CHECK_ARG_RET(NULL, value, ts, LY_EINVAL);
1772
1773 rc = ly_time_str2time(value, &ts->tv_sec, &fractions_s);
1774 LY_CHECK_RET(rc);
1775
1776 /* convert fractions of a second to nanoseconds */
1777 if (fractions_s) {
Michal Vasko72975062021-08-25 08:13:04 +02001778 /* init frac_buf with zeroes */
1779 memset(frac_buf, '0', 9);
1780 frac_buf[9] = '\0';
1781
Michal Vasko43297a02021-05-19 11:12:37 +02001782 frac_len = strlen(fractions_s);
1783 memcpy(frac_buf, fractions_s, frac_len > 9 ? 9 : frac_len);
1784 ts->tv_nsec = atol(frac_buf);
1785 free(fractions_s);
1786 } else {
1787 ts->tv_nsec = 0;
1788 }
1789
1790 return LY_SUCCESS;
1791}
1792
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001793LIBYANG_API_DEF LY_ERR
Michal Vasko43297a02021-05-19 11:12:37 +02001794ly_time_ts2str(const struct timespec *ts, char **str)
1795{
1796 char frac_buf[10];
1797
Jan Kundrátbd157002021-08-30 14:02:22 +02001798 LY_CHECK_ARG_RET(NULL, ts, str, ((ts->tv_nsec <= 999999999) && (ts->tv_nsec >= 0)), LY_EINVAL);
Michal Vasko43297a02021-05-19 11:12:37 +02001799
1800 /* convert nanoseconds to fractions of a second */
1801 if (ts->tv_nsec) {
1802 sprintf(frac_buf, "%09ld", ts->tv_nsec);
1803 }
1804
1805 return ly_time_time2str(ts->tv_sec, ts->tv_nsec ? frac_buf : NULL, str);
1806}