blob: aeaa763e7b273f5ca77f6d12bd0a4a658b15b80e [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 Vaskoebc3f402022-12-06 08:45:15 +0100307 return opaq->name.module_ns ? ly_ctx_get_module_implemented_ns(LYD_CTX(node), opaq->name.module_ns) : NULL;
Radek Krejci8df109d2021-04-23 12:19:08 +0200308 case LY_VALUE_JSON:
Michal Vaskoebc3f402022-12-06 08:45:15 +0100309 return opaq->name.module_name ? ly_ctx_get_module_implemented(LYD_CTX(node), opaq->name.module_name) : NULL;
Michal Vaskod5cfa6e2020-11-23 16:56:08 +0100310 default:
311 return NULL;
312 }
313 }
314
Michal Vaskoef53c812021-10-13 10:21:03 +0200315 return lysc_owner_module(node->schema);
Michal Vasko9b368d32020-02-14 13:53:31 +0100316}
Michal Vaskob1b5c262020-03-05 14:29:47 +0100317
Michal Vasko598063b2021-07-19 11:39:05 +0200318void
319lyd_first_module_sibling(struct lyd_node **node, const struct lys_module *mod)
320{
321 int cmp;
322 struct lyd_node *first;
Michal Vaskoec139eb2022-05-10 10:08:40 +0200323 const struct lys_module *own_mod;
Michal Vasko598063b2021-07-19 11:39:05 +0200324
325 assert(node && mod);
326
327 if (!*node) {
328 return;
329 }
330
331 first = *node;
Michal Vaskoec139eb2022-05-10 10:08:40 +0200332 own_mod = lyd_owner_module(first);
333 cmp = own_mod ? strcmp(own_mod->name, mod->name) : 1;
Michal Vasko598063b2021-07-19 11:39:05 +0200334 if (cmp > 0) {
335 /* there may be some preceding data */
336 while (first->prev->next) {
337 first = first->prev;
338 if (lyd_owner_module(first) == mod) {
339 cmp = 0;
340 break;
341 }
342 }
343 }
344
345 if (cmp == 0) {
346 /* there may be some preceding data belonging to this module */
347 while (first->prev->next) {
348 if (lyd_owner_module(first->prev) != mod) {
349 break;
350 }
351 first = first->prev;
352 }
353 }
354
355 if (cmp < 0) {
356 /* there may be some following data */
357 LY_LIST_FOR(first, first) {
358 if (lyd_owner_module(first) == mod) {
359 cmp = 0;
360 break;
361 }
362 }
363 }
364
365 if (cmp == 0) {
366 /* we have found the first module data node */
367 *node = first;
368 }
369}
370
Michal Vaskob1b5c262020-03-05 14:29:47 +0100371const struct lys_module *
Michal Vasko26e80012020-07-08 10:55:46 +0200372lyd_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 +0200373 struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +0100374{
375 struct lyd_node *iter;
376 const struct lys_module *mod;
377
378 /* get the next module */
Michal Vasko26e80012020-07-08 10:55:46 +0200379 if (module) {
380 if (*i) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100381 mod = NULL;
Michal Vasko26e80012020-07-08 10:55:46 +0200382 } else {
383 mod = module;
384 ++(*i);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100385 }
386 } else {
387 do {
388 mod = ly_ctx_get_module_iter(ctx, i);
389 } while (mod && !mod->implemented);
390 }
391
392 /* find its data */
393 *first = NULL;
394 if (mod) {
395 LY_LIST_FOR(tree, iter) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100396 if (lyd_owner_module(iter) == mod) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100397 *first = iter;
398 break;
399 }
400 }
401 }
402
403 return mod;
404}
405
406const struct lys_module *
407lyd_data_next_module(struct lyd_node **next, struct lyd_node **first)
408{
409 const struct lys_module *mod;
410
411 if (!*next) {
412 /* all data traversed */
413 *first = NULL;
414 return NULL;
415 }
416
417 *first = *next;
418
419 /* prepare next */
Michal Vaskoc193ce92020-03-06 11:04:48 +0100420 mod = lyd_owner_module(*next);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100421 LY_LIST_FOR(*next, *next) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100422 if (lyd_owner_module(*next) != mod) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100423 break;
424 }
425 }
426
427 return mod;
428}
Michal Vasko9f96a052020-03-10 09:41:45 +0100429
430LY_ERR
Michal Vasko59892dd2022-05-13 11:02:30 +0200431lyd_value_store(const struct ly_ctx *ctx, struct lyd_value *val, const struct lysc_type *type, const void *value,
432 size_t value_len, ly_bool *dynamic, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints,
433 const struct lysc_node *ctx_node, ly_bool *incomplete)
434{
435 LY_ERR ret;
436 struct ly_err_item *err = NULL;
437 uint32_t options = (dynamic && *dynamic ? LYPLG_TYPE_STORE_DYNAMIC : 0);
438
439 if (!value) {
440 value = "";
441 }
442 if (incomplete) {
443 *incomplete = 0;
444 }
445
446 ret = type->plugin->store(ctx, type, value, value_len, options, format, prefix_data, hints, ctx_node, val, NULL, &err);
447 if (dynamic) {
448 *dynamic = 0;
449 }
450
451 if (ret == LY_EINCOMPLETE) {
452 if (incomplete) {
453 *incomplete = 1;
454 }
455 } else if (ret) {
456 if (err) {
457 LOGVAL_ERRITEM(ctx, err);
458 ly_err_free(err);
459 } else {
460 LOGVAL(ctx, LYVE_OTHER, "Storing value failed.");
461 }
462 return ret;
463 }
464
465 return LY_SUCCESS;
466}
467
468LY_ERR
469lyd_value_validate_incomplete(const struct ly_ctx *ctx, const struct lysc_type *type, struct lyd_value *val,
470 const struct lyd_node *ctx_node, const struct lyd_node *tree)
471{
472 LY_ERR ret;
473 struct ly_err_item *err = NULL;
474
475 assert(type->plugin->validate);
476
477 ret = type->plugin->validate(ctx, type, ctx_node, tree, val, &err);
478 if (ret) {
479 if (err) {
480 LOGVAL_ERRITEM(ctx, err);
481 ly_err_free(err);
482 } else {
483 LOGVAL(ctx, LYVE_OTHER, "Resolving value \"%s\" failed.", type->plugin->print(ctx, val, LY_VALUE_CANON,
484 NULL, NULL, NULL));
485 }
486 return ret;
487 }
488
489 return LY_SUCCESS;
490}
491
492LY_ERR
493lys_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len,
494 LY_VALUE_FORMAT format, void *prefix_data)
495{
496 LY_ERR rc = LY_SUCCESS;
497 struct ly_err_item *err = NULL;
498 struct lyd_value storage;
499 struct lysc_type *type;
500
Michal Vasko0c361552023-04-04 10:04:03 +0200501 LY_CHECK_ARG_RET(ctx, node, LY_EINVAL);
Michal Vasko59892dd2022-05-13 11:02:30 +0200502
503 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
504 LOGARG(ctx, node);
505 return LY_EINVAL;
506 }
507
508 type = ((struct lysc_node_leaf *)node)->type;
509 rc = type->plugin->store(ctx ? ctx : node->module->ctx, type, value, value_len, 0, format, prefix_data,
510 LYD_HINT_SCHEMA, node, &storage, NULL, &err);
511 if (rc == LY_EINCOMPLETE) {
512 /* actually success since we do not provide the context tree and call validation with
513 * LY_TYPE_OPTS_INCOMPLETE_DATA */
514 rc = LY_SUCCESS;
515 } else if (rc && err) {
516 if (ctx) {
517 /* log only in case the ctx was provided as input parameter */
Michal Vaskof8da2682022-06-16 07:52:37 +0200518 if (err->path) {
519 LOG_LOCSET(NULL, NULL, err->path, NULL);
Michal Vaskof8da2682022-06-16 07:52:37 +0200520 }
Michal Vasko59892dd2022-05-13 11:02:30 +0200521 LOGVAL_ERRITEM(ctx, err);
Michal Vaskof8da2682022-06-16 07:52:37 +0200522 if (err->path) {
523 LOG_LOCBACK(0, 0, 1, 0);
Michal Vaskof8da2682022-06-16 07:52:37 +0200524 }
Michal Vasko59892dd2022-05-13 11:02:30 +0200525 }
526 ly_err_free(err);
527 }
528
529 if (!rc) {
530 type->plugin->free(ctx ? ctx : node->module->ctx, &storage);
531 }
532 return rc;
533}
534
535LIBYANG_API_DEF LY_ERR
536lyd_value_validate(const struct ly_ctx *ctx, const struct lysc_node *schema, const char *value, size_t value_len,
537 const struct lyd_node *ctx_node, const struct lysc_type **realtype, const char **canonical)
538{
539 LY_ERR rc;
540 struct ly_err_item *err = NULL;
541 struct lysc_type *type;
542 struct lyd_value val = {0};
543 ly_bool stored = 0, log = 1;
544
Michal Vasko3dd16da2022-06-15 07:58:41 +0200545 LY_CHECK_ARG_RET(ctx, schema, !value_len || value, LY_EINVAL);
Michal Vasko59892dd2022-05-13 11:02:30 +0200546
547 if (!ctx) {
548 ctx = schema->module->ctx;
549 log = 0;
550 }
Michal Vasko3dd16da2022-06-15 07:58:41 +0200551 if (!value_len) {
552 value = "";
553 }
Michal Vasko59892dd2022-05-13 11:02:30 +0200554 type = ((struct lysc_node_leaf *)schema)->type;
555
556 /* store */
557 rc = type->plugin->store(ctx, type, value, value_len, 0, LY_VALUE_JSON, NULL,
558 LYD_HINT_DATA, schema, &val, NULL, &err);
559 if (!rc || (rc == LY_EINCOMPLETE)) {
560 stored = 1;
561 }
562
563 if (ctx_node && (rc == LY_EINCOMPLETE)) {
564 /* resolve */
565 rc = type->plugin->validate(ctx, type, ctx_node, ctx_node, &val, &err);
566 }
567
568 if (rc && (rc != LY_EINCOMPLETE) && err) {
569 if (log) {
570 /* log error */
571 if (err->path) {
572 LOG_LOCSET(NULL, NULL, err->path, NULL);
573 } else if (ctx_node) {
574 LOG_LOCSET(NULL, ctx_node, NULL, NULL);
575 } else {
576 LOG_LOCSET(schema, NULL, NULL, NULL);
577 }
578 LOGVAL_ERRITEM(ctx, err);
579 if (err->path) {
580 LOG_LOCBACK(0, 0, 1, 0);
581 } else if (ctx_node) {
582 LOG_LOCBACK(0, 1, 0, 0);
583 } else {
584 LOG_LOCBACK(1, 0, 0, 0);
585 }
586 }
587 ly_err_free(err);
588 }
589
590 if (!rc || (rc == LY_EINCOMPLETE)) {
591 if (realtype) {
592 /* return realtype */
593 if (val.realtype->basetype == LY_TYPE_UNION) {
594 *realtype = val.subvalue->value.realtype;
595 } else {
596 *realtype = val.realtype;
597 }
598 }
599
600 if (canonical) {
601 /* return canonical value */
602 lydict_insert(ctx, val.realtype->plugin->print(ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL), 0, canonical);
603 }
604 }
605
606 if (stored) {
607 /* free value */
608 type->plugin->free(ctx ? ctx : schema->module->ctx, &val);
609 }
610 return rc;
611}
612
613LIBYANG_API_DEF LY_ERR
614lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len)
615{
616 LY_ERR ret = LY_SUCCESS;
617 struct ly_ctx *ctx;
618 struct lysc_type *type;
619 struct lyd_value val = {0};
620
621 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, value, LY_EINVAL);
622
623 ctx = node->schema->module->ctx;
624 type = ((struct lysc_node_leaf *)node->schema)->type;
625
626 /* store the value */
627 LOG_LOCSET(node->schema, &node->node, NULL, NULL);
628 ret = lyd_value_store(ctx, &val, type, value, value_len, NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, node->schema, NULL);
629 LOG_LOCBACK(1, 1, 0, 0);
630 LY_CHECK_RET(ret);
631
632 /* compare values */
633 ret = type->plugin->compare(&node->value, &val);
634
635 type->plugin->free(ctx, &val);
636 return ret;
637}
638
639LIBYANG_API_DEF ly_bool
640lyd_is_default(const struct lyd_node *node)
641{
642 const struct lysc_node_leaf *leaf;
643 const struct lysc_node_leaflist *llist;
644 const struct lyd_node_term *term;
645 LY_ARRAY_COUNT_TYPE u;
646
647 if (!(node->schema->nodetype & LYD_NODE_TERM)) {
648 return 0;
649 }
650
651 term = (const struct lyd_node_term *)node;
652
653 if (node->schema->nodetype == LYS_LEAF) {
654 leaf = (const struct lysc_node_leaf *)node->schema;
655 if (!leaf->dflt) {
656 return 0;
657 }
658
659 /* compare with the default value */
660 if (!leaf->type->plugin->compare(&term->value, leaf->dflt)) {
661 return 1;
662 }
663 } else {
664 llist = (const struct lysc_node_leaflist *)node->schema;
665 if (!llist->dflts) {
666 return 0;
667 }
668
669 LY_ARRAY_FOR(llist->dflts, u) {
670 /* compare with each possible default value */
671 if (!llist->type->plugin->compare(&term->value, llist->dflts[u])) {
672 return 1;
673 }
674 }
675 }
676
677 return 0;
678}
679
680LIBYANG_API_DEF uint32_t
681lyd_list_pos(const struct lyd_node *instance)
682{
683 const struct lyd_node *iter = NULL;
684 uint32_t pos = 0;
685
686 if (!instance || !(instance->schema->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
687 return 0;
688 }
689
690 /* data instances are ordered, so we can stop when we found instance of other schema node */
691 for (iter = instance; iter->schema == instance->schema; iter = iter->prev) {
692 if (pos && (iter->next == NULL)) {
693 /* overrun to the end of the siblings list */
694 break;
695 }
696 ++pos;
697 }
698
699 return pos;
700}
701
702LIBYANG_API_DEF struct lyd_node *
703lyd_first_sibling(const struct lyd_node *node)
704{
705 struct lyd_node *start;
706
707 if (!node) {
708 return NULL;
709 }
710
711 /* get the first sibling */
712 if (node->parent) {
713 start = node->parent->child;
714 } else {
715 for (start = (struct lyd_node *)node; start->prev->next; start = start->prev) {}
716 }
717
718 return start;
719}
720
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100721/**
722 * @brief Check list node parsed into an opaque node for the reason.
723 *
724 * @param[in] node Opaque node.
725 * @param[in] snode Schema node of @p opaq.
726 * @return LY_SUCCESS if the node is valid;
727 * @return LY_ERR on error.
728 */
729static LY_ERR
730lyd_parse_opaq_list_error(const struct lyd_node *node, const struct lysc_node *snode)
731{
732 LY_ERR ret = LY_SUCCESS;
733 struct ly_set key_set = {0};
734 const struct lysc_node *key = NULL;
735 const struct lyd_node *child;
736 const struct lyd_node_opaq *opaq_k;
737 uint32_t i;
738
739 assert(!node->schema);
740
741 /* get all keys into a set */
742 while ((key = lys_getnext(key, snode, NULL, 0)) && (snode->flags & LYS_KEY)) {
743 LY_CHECK_GOTO(ret = ly_set_add(&key_set, (void *)snode, 1, NULL), cleanup);
744 }
745
746 LY_LIST_FOR(lyd_child(node), child) {
747 if (child->schema) {
748 LOGERR(LYD_CTX(node), LY_EINVAL, "Unexpected node %s \"%s\".", lys_nodetype2str(child->schema->nodetype),
749 LYD_NAME(child));
750 ret = LY_EINVAL;
751 goto cleanup;
752 }
753
754 opaq_k = (struct lyd_node_opaq *)child;
755
756 /* find the key schema node */
757 for (i = 0; i < key_set.count; ++i) {
758 key = key_set.snodes[i];
759 if (!strcmp(key->name, opaq_k->name.name)) {
760 break;
761 }
762 }
763 if (i == key_set.count) {
764 /* some other node, skip */
765 continue;
766 }
767
768 /* key found */
769 ly_set_rm_index(&key_set, i, NULL);
770
771 /* check value */
772 ret = lys_value_validate(LYD_CTX(node), key, opaq_k->value, strlen(opaq_k->value), opaq_k->format,
773 opaq_k->val_prefix_data);
774 LY_CHECK_GOTO(ret, cleanup);
775 }
776
777 if (key_set.count) {
778 /* missing keys */
779 LOGVAL(LYD_CTX(node), LY_VCODE_NOKEY, key_set.snodes[0]->name);
780 ret = LY_EVALID;
781 goto cleanup;
782 }
783
784cleanup:
785 ly_set_erase(&key_set, NULL);
786 return ret;
787}
788
789LIBYANG_API_DEF LY_ERR
790lyd_parse_opaq_error(const struct lyd_node *node)
791{
Michal Vasko6727c682023-02-17 10:40:26 +0100792 LY_ERR rc = LY_SUCCESS;
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100793 const struct ly_ctx *ctx;
794 const struct lyd_node_opaq *opaq;
795 const struct lyd_node *parent;
796 const struct lys_module *mod;
797 const struct lysc_node *snode;
Michal Vasko6727c682023-02-17 10:40:26 +0100798 uint32_t loc_node = 0, loc_path = 0;
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100799
800 LY_CHECK_ARG_RET(LYD_CTX(node), node, !node->schema, !lyd_parent(node) || lyd_parent(node)->schema, LY_EINVAL);
801
802 ctx = LYD_CTX(node);
803 opaq = (struct lyd_node_opaq *)node;
804 parent = lyd_parent(node);
805
Michal Vasko6727c682023-02-17 10:40:26 +0100806 if (lyd_parent(node)) {
807 LOG_LOCSET(NULL, lyd_parent(node), NULL, NULL);
808 ++loc_node;
809 } else {
810 LOG_LOCSET(NULL, NULL, "/", NULL);
811 ++loc_path;
812 }
813
Michal Vaskof4e63922022-05-10 10:32:13 +0200814 if (!opaq->name.module_ns) {
815 LOGVAL(ctx, LYVE_REFERENCE, "Unknown module of node \"%s\".", opaq->name.name);
Michal Vasko6727c682023-02-17 10:40:26 +0100816 rc = LY_EVALID;
817 goto cleanup;
Michal Vaskof4e63922022-05-10 10:32:13 +0200818 }
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100819
820 /* module */
821 switch (opaq->format) {
822 case LY_VALUE_XML:
823 if (!parent || strcmp(opaq->name.module_ns, parent->schema->module->ns)) {
824 mod = ly_ctx_get_module_implemented_ns(ctx, opaq->name.module_ns);
825 if (!mod) {
Michal Vasko959f8d82022-06-16 07:51:50 +0200826 LOGVAL(ctx, LYVE_REFERENCE, "No (implemented) module with namespace \"%s\" of node \"%s\" in the context.",
827 opaq->name.module_ns, opaq->name.name);
Michal Vasko6727c682023-02-17 10:40:26 +0100828 rc = LY_EVALID;
829 goto cleanup;
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100830 }
831 } else {
832 /* inherit */
833 mod = parent->schema->module;
834 }
835 break;
836 case LY_VALUE_JSON:
837 case LY_VALUE_LYB:
838 if (!parent || strcmp(opaq->name.module_name, parent->schema->module->name)) {
839 mod = ly_ctx_get_module_implemented(ctx, opaq->name.module_name);
840 if (!mod) {
Michal Vasko959f8d82022-06-16 07:51:50 +0200841 LOGVAL(ctx, LYVE_REFERENCE, "No (implemented) module named \"%s\" of node \"%s\" in the context.",
842 opaq->name.module_name, opaq->name.name);
Michal Vasko6727c682023-02-17 10:40:26 +0100843 rc = LY_EVALID;
844 goto cleanup;
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100845 }
846 } else {
847 /* inherit */
848 mod = parent->schema->module;
849 }
850 break;
851 default:
852 LOGERR(ctx, LY_EINVAL, "Unsupported value format.");
Michal Vasko6727c682023-02-17 10:40:26 +0100853 rc = LY_EINVAL;
854 goto cleanup;
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100855 }
856
857 /* schema */
858 snode = lys_find_child(parent ? parent->schema : NULL, mod, opaq->name.name, 0, 0, 0);
Michal Vaskoac6f4be2022-05-02 10:16:50 +0200859 if (!snode && parent && parent->schema && (parent->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
860 /* maybe output node */
Michal Vasko89afc6e2022-05-02 10:24:26 +0200861 snode = lys_find_child(parent->schema, mod, opaq->name.name, 0, 0, LYS_GETNEXT_OUTPUT);
Michal Vaskoac6f4be2022-05-02 10:16:50 +0200862 }
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100863 if (!snode) {
864 if (parent) {
865 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%s\" not found as a child of \"%s\" node.", opaq->name.name,
866 LYD_NAME(parent));
867 } else {
868 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%s\" not found in the \"%s\" module.", opaq->name.name, mod->name);
869 }
Michal Vasko6727c682023-02-17 10:40:26 +0100870 rc = LY_EVALID;
871 goto cleanup;
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100872 }
873
Michal Vasko6727c682023-02-17 10:40:26 +0100874 /* schema node exists */
875 LOG_LOCBACK(0, loc_node, loc_path, 0);
876 loc_node = 0;
877 loc_path = 0;
878 LOG_LOCSET(NULL, node, NULL, NULL);
879 ++loc_node;
880
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100881 if (snode->nodetype & LYD_NODE_TERM) {
882 /* leaf / leaf-list */
Michal Vasko6727c682023-02-17 10:40:26 +0100883 rc = lys_value_validate(ctx, snode, opaq->value, strlen(opaq->value), opaq->format, opaq->val_prefix_data);
884 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100885 } else if (snode->nodetype == LYS_LIST) {
886 /* list */
Michal Vasko6727c682023-02-17 10:40:26 +0100887 rc = lyd_parse_opaq_list_error(node, snode);
888 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100889 } else if (snode->nodetype & LYD_NODE_INNER) {
890 /* inner node */
891 if (opaq->value) {
892 LOGVAL(ctx, LYVE_DATA, "Invalid value \"%s\" for %s \"%s\".", opaq->value,
893 lys_nodetype2str(snode->nodetype), snode->name);
Michal Vasko6727c682023-02-17 10:40:26 +0100894 rc = LY_EVALID;
895 goto cleanup;
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100896 }
897 } else {
898 LOGERR(ctx, LY_EINVAL, "Unexpected opaque schema node %s \"%s\".", lys_nodetype2str(snode->nodetype), snode->name);
Michal Vasko6727c682023-02-17 10:40:26 +0100899 rc = LY_EINVAL;
900 goto cleanup;
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100901 }
902
903 LOGERR(ctx, LY_EINVAL, "Unexpected valid opaque node %s \"%s\".", lys_nodetype2str(snode->nodetype), snode->name);
Michal Vasko6727c682023-02-17 10:40:26 +0100904 rc = LY_EINVAL;
905
906cleanup:
907 LOG_LOCBACK(0, loc_node, loc_path, 0);
908 return rc;
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100909}
910
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100911LIBYANG_API_DEF const char *
Christian Hopps46bd21b2021-04-27 09:43:58 -0400912lyd_value_get_canonical(const struct ly_ctx *ctx, const struct lyd_value *value)
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200913{
Michal Vaskoab40e7e2021-04-28 17:04:24 +0200914 LY_CHECK_ARG_RET(ctx, ctx, value, NULL);
915
Michal Vasko33876022021-04-27 16:42:24 +0200916 return value->_canonical ? value->_canonical :
917 (const char *)value->realtype->plugin->print(ctx, value, LY_VALUE_CANON, NULL, NULL, NULL);
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200918}
919
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100920LIBYANG_API_DEF LY_ERR
Michal Vaskoa820c312021-02-05 16:33:00 +0100921lyd_any_value_str(const struct lyd_node *any, char **value_str)
922{
923 const struct lyd_node_any *a;
924 struct lyd_node *tree = NULL;
925 const char *str = NULL;
926 ly_bool dynamic = 0;
927 LY_ERR ret = LY_SUCCESS;
928
929 LY_CHECK_ARG_RET(NULL, any, value_str, LY_EINVAL);
Radek Krejci71877df2021-04-06 17:24:06 +0200930 LY_CHECK_ARG_RET(NULL, any->schema, any->schema->nodetype & LYS_ANYDATA, LY_EINVAL);
Michal Vaskoa820c312021-02-05 16:33:00 +0100931
932 a = (struct lyd_node_any *)any;
933 *value_str = NULL;
934
935 if (!a->value.str) {
936 /* there is no value in the union */
937 return LY_SUCCESS;
938 }
939
940 switch (a->value_type) {
941 case LYD_ANYDATA_LYB:
942 /* parse into a data tree */
943 ret = lyd_parse_data_mem(LYD_CTX(any), a->value.mem, LYD_LYB, LYD_PARSE_ONLY, 0, &tree);
944 LY_CHECK_GOTO(ret, cleanup);
945 dynamic = 1;
946 break;
947 case LYD_ANYDATA_DATATREE:
948 tree = a->value.tree;
949 break;
950 case LYD_ANYDATA_STRING:
951 case LYD_ANYDATA_XML:
952 case LYD_ANYDATA_JSON:
953 /* simply use the string */
954 str = a->value.str;
955 break;
956 }
957
958 if (tree) {
959 /* print into a string */
960 ret = lyd_print_mem(value_str, tree, LYD_XML, LYD_PRINT_WITHSIBLINGS);
961 LY_CHECK_GOTO(ret, cleanup);
962 } else {
963 assert(str);
964 *value_str = strdup(str);
965 LY_CHECK_ERR_GOTO(!*value_str, LOGMEM(LYD_CTX(any)), cleanup);
966 }
967
968 /* success */
969
970cleanup:
971 if (dynamic) {
972 lyd_free_all(tree);
973 }
974 return ret;
975}
976
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100977LIBYANG_API_DEF LY_ERR
Michal Vasko61551fa2020-07-09 15:45:45 +0200978lyd_any_copy_value(struct lyd_node *trg, const union lyd_any_value *value, LYD_ANYDATA_VALUETYPE value_type)
979{
980 struct lyd_node_any *t;
Michal Vasko61551fa2020-07-09 15:45:45 +0200981
Michal Vaskoa820c312021-02-05 16:33:00 +0100982 LY_CHECK_ARG_RET(NULL, trg, LY_EINVAL);
Radek Krejci71877df2021-04-06 17:24:06 +0200983 LY_CHECK_ARG_RET(NULL, trg->schema, trg->schema->nodetype & LYS_ANYDATA, LY_EINVAL);
Michal Vasko61551fa2020-07-09 15:45:45 +0200984
985 t = (struct lyd_node_any *)trg;
986
987 /* free trg */
988 switch (t->value_type) {
989 case LYD_ANYDATA_DATATREE:
990 lyd_free_all(t->value.tree);
991 break;
992 case LYD_ANYDATA_STRING:
993 case LYD_ANYDATA_XML:
994 case LYD_ANYDATA_JSON:
Michal Vaskoe180ed02021-02-05 16:31:20 +0100995 lydict_remove(LYD_CTX(trg), t->value.str);
Michal Vasko61551fa2020-07-09 15:45:45 +0200996 break;
997 case LYD_ANYDATA_LYB:
998 free(t->value.mem);
999 break;
1000 }
1001 t->value.str = NULL;
1002
1003 if (!value) {
1004 /* only free value in this case */
1005 return LY_SUCCESS;
1006 }
1007
1008 /* copy src */
1009 t->value_type = value_type;
1010 switch (value_type) {
1011 case LYD_ANYDATA_DATATREE:
1012 if (value->tree) {
Michal Vasko3a41dff2020-07-15 14:30:28 +02001013 LY_CHECK_RET(lyd_dup_siblings(value->tree, NULL, LYD_DUP_RECURSIVE, &t->value.tree));
Michal Vasko61551fa2020-07-09 15:45:45 +02001014 }
1015 break;
1016 case LYD_ANYDATA_STRING:
1017 case LYD_ANYDATA_XML:
1018 case LYD_ANYDATA_JSON:
1019 if (value->str) {
Radek Krejci011e4aa2020-09-04 15:22:31 +02001020 LY_CHECK_RET(lydict_insert(LYD_CTX(trg), value->str, 0, &t->value.str));
Michal Vasko61551fa2020-07-09 15:45:45 +02001021 }
1022 break;
1023 case LYD_ANYDATA_LYB:
1024 if (value->mem) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02001025 int len = lyd_lyb_data_length(value->mem);
Michal Vasko26bbb272022-08-02 14:54:33 +02001026
Radek Krejci82fa8d42020-07-11 22:00:59 +02001027 LY_CHECK_RET(len == -1, LY_EINVAL);
Michal Vasko61551fa2020-07-09 15:45:45 +02001028 t->value.mem = malloc(len);
Michal Vaskob7be7a82020-08-20 09:09:04 +02001029 LY_CHECK_ERR_RET(!t->value.mem, LOGMEM(LYD_CTX(trg)), LY_EMEM);
Michal Vasko61551fa2020-07-09 15:45:45 +02001030 memcpy(t->value.mem, value->mem, len);
1031 }
1032 break;
1033 }
1034
1035 return LY_SUCCESS;
1036}
1037
Michal Vasko106f0862021-11-02 11:49:27 +01001038const struct lysc_node *
1039lyd_node_schema(const struct lyd_node *node)
1040{
1041 const struct lysc_node *schema = NULL;
1042 const struct lyd_node *prev_iter = NULL, *iter;
1043 const struct lys_module *mod;
1044
1045 if (!node) {
1046 return NULL;
1047 } else if (node->schema) {
1048 return node->schema;
1049 }
1050
1051 /* get schema node of an opaque node */
1052 do {
1053 /* get next data node */
1054 for (iter = node; lyd_parent(iter) != prev_iter; iter = lyd_parent(iter)) {}
1055
1056 /* get equivalent schema node */
1057 if (iter->schema) {
1058 schema = iter->schema;
1059 } else {
1060 /* get module */
1061 mod = lyd_owner_module(iter);
Michal Vaskoa41826a2021-11-02 12:13:03 +01001062 if (!mod && !schema) {
1063 /* top-level opaque node has unknown module */
1064 break;
1065 }
Michal Vasko106f0862021-11-02 11:49:27 +01001066
1067 /* get schema node */
1068 schema = lys_find_child(schema, mod ? mod : schema->module, LYD_NAME(iter), 0, 0, 0);
1069 }
Michal Vaskod2f404f2021-11-04 15:37:11 +01001070
1071 /* remember to move to the descendant */
1072 prev_iter = iter;
Michal Vasko106f0862021-11-02 11:49:27 +01001073 } while (schema && (iter != node));
1074
1075 return schema;
1076}
1077
Michal Vasko4754d4a2022-12-01 10:11:21 +01001078void
1079lyd_cont_set_dflt(struct lyd_node *node)
1080{
1081 const struct lyd_node *child;
1082
1083 while (node) {
1084 if (!node->schema || (node->flags & LYD_DEFAULT) || !lysc_is_np_cont(node->schema)) {
1085 /* not a non-dflt NP container */
1086 break;
1087 }
1088
1089 LY_LIST_FOR(lyd_child(node), child) {
1090 if (!(child->flags & LYD_DEFAULT)) {
1091 break;
1092 }
1093 }
1094 if (child) {
1095 /* explicit child, no dflt change */
1096 break;
1097 }
1098
1099 /* set the dflt flag */
1100 node->flags |= LYD_DEFAULT;
1101
1102 /* check all parent containers */
1103 node = lyd_parent(node);
1104 }
1105}
1106
Michal Vasko59892dd2022-05-13 11:02:30 +02001107/**
1108 * @brief Comparison callback to match schema node with a schema of a data node.
1109 *
1110 * @param[in] val1_p Pointer to the schema node
1111 * @param[in] val2_p Pointer to the data node
1112 * Implementation of ::lyht_value_equal_cb.
1113 */
1114static ly_bool
1115lyd_hash_table_schema_val_equal(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
1116{
1117 struct lysc_node *val1;
1118 struct lyd_node *val2;
1119
1120 val1 = *((struct lysc_node **)val1_p);
1121 val2 = *((struct lyd_node **)val2_p);
1122
1123 if (val1 == val2->schema) {
1124 /* schema match is enough */
1125 return 1;
1126 } else {
1127 return 0;
1128 }
1129}
1130
1131LY_ERR
1132lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match)
1133{
1134 struct lyd_node **match_p;
1135 struct lyd_node_inner *parent;
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001136 const struct lysc_node *cur_schema;
Michal Vasko59892dd2022-05-13 11:02:30 +02001137 uint32_t hash;
1138 lyht_value_equal_cb ht_cb;
1139
Michal Vasko21beaeb2022-08-02 10:42:48 +02001140 assert(schema);
1141 if (!siblings) {
1142 /* no data */
1143 if (match) {
1144 *match = NULL;
1145 }
1146 return LY_ENOTFOUND;
1147 }
Michal Vasko59892dd2022-05-13 11:02:30 +02001148
1149 parent = siblings->parent;
1150 if (parent && parent->schema && parent->children_ht) {
1151 /* calculate our hash */
1152 hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name));
1153 hash = dict_hash_multi(hash, schema->name, strlen(schema->name));
1154 hash = dict_hash_multi(hash, NULL, 0);
1155
1156 /* use special hash table function */
1157 ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal);
1158
1159 /* find by hash */
1160 if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) {
1161 siblings = *match_p;
1162 } else {
1163 /* not found */
1164 siblings = NULL;
1165 }
1166
1167 /* set the original hash table compare function back */
1168 lyht_set_cb(parent->children_ht, ht_cb);
1169 } else {
1170 /* find first sibling */
1171 if (siblings->parent) {
1172 siblings = siblings->parent->child;
1173 } else {
1174 while (siblings->prev->next) {
1175 siblings = siblings->prev;
1176 }
1177 }
1178
1179 /* search manually without hashes */
1180 for ( ; siblings; siblings = siblings->next) {
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001181 cur_schema = lyd_node_schema(siblings);
1182 if (!cur_schema) {
1183 /* some unknown opaque node */
1184 continue;
Michal Vasko31bac382022-07-20 08:07:59 +02001185 }
Michal Vaskodf8ebf62022-11-10 10:33:28 +01001186
1187 /* schema match is enough */
1188 if (cur_schema->module->ctx == schema->module->ctx) {
1189 if (cur_schema == schema) {
1190 break;
1191 }
1192 } else {
1193 if (!strcmp(cur_schema->name, schema->name) && !strcmp(cur_schema->module->name, schema->module->name)) {
1194 break;
1195 }
Michal Vasko59892dd2022-05-13 11:02:30 +02001196 }
1197 }
1198 }
1199
1200 if (!siblings) {
1201 if (match) {
1202 *match = NULL;
1203 }
1204 return LY_ENOTFOUND;
1205 }
1206
1207 if (match) {
1208 *match = (struct lyd_node *)siblings;
1209 }
1210 return LY_SUCCESS;
1211}
1212
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001213void
Michal Vaskod3bb12f2020-12-04 14:33:09 +01001214lyd_del_move_root(struct lyd_node **root, const struct lyd_node *to_del, const struct lys_module *mod)
1215{
1216 if (*root && (lyd_owner_module(*root) != mod)) {
1217 /* there are no data of mod so this is simply the first top-level sibling */
1218 mod = NULL;
1219 }
1220
1221 if ((*root != to_del) || (*root)->parent) {
1222 return;
1223 }
1224
Michal Vasko598063b2021-07-19 11:39:05 +02001225 if (mod && (*root)->prev->next && (!(*root)->next || (lyd_owner_module(to_del) != lyd_owner_module((*root)->next)))) {
1226 /* there are no more nodes from mod, simply get the first top-level sibling */
Michal Vaskod3bb12f2020-12-04 14:33:09 +01001227 *root = lyd_first_sibling(*root);
Michal Vasko598063b2021-07-19 11:39:05 +02001228 } else {
1229 *root = (*root)->next;
Michal Vaskod3bb12f2020-12-04 14:33:09 +01001230 }
1231}
1232
Michal Vasko8cc3f662022-03-29 11:25:51 +02001233LY_ERR
1234ly_nested_ext_schema(const struct lyd_node *parent, const struct lysc_node *sparent, const char *prefix,
1235 size_t prefix_len, LY_VALUE_FORMAT format, void *prefix_data, const char *name, size_t name_len,
1236 const struct lysc_node **snode, struct lysc_ext_instance **ext)
1237{
1238 LY_ERR r;
1239 LY_ARRAY_COUNT_TYPE u;
1240 struct lysc_ext_instance *nested_exts = NULL;
1241 lyplg_ext_data_snode_clb ext_snode_cb;
1242
1243 /* check if there are any nested extension instances */
1244 if (parent && parent->schema) {
1245 nested_exts = parent->schema->exts;
1246 } else if (sparent) {
1247 nested_exts = sparent->exts;
1248 }
1249 LY_ARRAY_FOR(nested_exts, u) {
Michal Vasko305c6cb2022-04-27 10:33:04 +02001250 if (!nested_exts[u].def->plugin) {
1251 /* no plugin */
1252 continue;
1253 }
1254
Michal Vasko8cc3f662022-03-29 11:25:51 +02001255 ext_snode_cb = nested_exts[u].def->plugin->snode;
1256 if (!ext_snode_cb) {
1257 /* not an extension with nested data */
1258 continue;
1259 }
1260
1261 /* try to get the schema node */
1262 r = ext_snode_cb(&nested_exts[u], parent, sparent, prefix, prefix_len, format, prefix_data, name, name_len, snode);
1263 if (!r) {
Michal Vasko66330fc2022-11-21 15:52:24 +01001264 if (ext) {
1265 /* data successfully created, remember the ext instance */
1266 *ext = &nested_exts[u];
1267 }
Michal Vasko8cc3f662022-03-29 11:25:51 +02001268 return LY_SUCCESS;
1269 } else if (r != LY_ENOT) {
1270 /* fatal error */
1271 return r;
1272 }
1273 /* data was not from this module, continue */
1274 }
1275
1276 /* no extensions or none matched */
1277 return LY_ENOT;
1278}
1279
Michal Vaskod3bb12f2020-12-04 14:33:09 +01001280void
Radek Krejci8df109d2021-04-23 12:19:08 +02001281ly_free_prefix_data(LY_VALUE_FORMAT format, void *prefix_data)
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001282{
1283 struct ly_set *ns_list;
1284 struct lysc_prefix *prefixes;
1285 uint32_t i;
1286 LY_ARRAY_COUNT_TYPE u;
1287
1288 if (!prefix_data) {
1289 return;
1290 }
1291
1292 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001293 case LY_VALUE_XML:
Michal Vaskoddd76592022-01-17 13:34:48 +01001294 case LY_VALUE_STR_NS:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001295 ns_list = prefix_data;
1296 for (i = 0; i < ns_list->count; ++i) {
1297 free(((struct lyxml_ns *)ns_list->objs[i])->prefix);
1298 free(((struct lyxml_ns *)ns_list->objs[i])->uri);
1299 }
1300 ly_set_free(ns_list, free);
1301 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02001302 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001303 prefixes = prefix_data;
1304 LY_ARRAY_FOR(prefixes, u) {
1305 free(prefixes[u].prefix);
1306 }
1307 LY_ARRAY_FREE(prefixes);
1308 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02001309 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02001310 case LY_VALUE_SCHEMA:
1311 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02001312 case LY_VALUE_LYB:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001313 break;
1314 }
1315}
1316
1317LY_ERR
Radek Krejci8df109d2021-04-23 12:19:08 +02001318ly_dup_prefix_data(const struct ly_ctx *ctx, LY_VALUE_FORMAT format, const void *prefix_data,
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001319 void **prefix_data_p)
1320{
1321 LY_ERR ret = LY_SUCCESS;
1322 struct lyxml_ns *ns;
1323 struct lysc_prefix *prefixes = NULL, *orig_pref;
1324 struct ly_set *ns_list, *orig_ns;
1325 uint32_t i;
1326 LY_ARRAY_COUNT_TYPE u;
1327
1328 assert(!*prefix_data_p);
1329
1330 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001331 case LY_VALUE_SCHEMA:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001332 *prefix_data_p = (void *)prefix_data;
1333 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02001334 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001335 /* copy all the value prefixes */
1336 orig_pref = (struct lysc_prefix *)prefix_data;
1337 LY_ARRAY_CREATE_GOTO(ctx, prefixes, LY_ARRAY_COUNT(orig_pref), ret, cleanup);
1338 *prefix_data_p = prefixes;
1339
1340 LY_ARRAY_FOR(orig_pref, u) {
1341 if (orig_pref[u].prefix) {
1342 prefixes[u].prefix = strdup(orig_pref[u].prefix);
1343 LY_CHECK_ERR_GOTO(!prefixes[u].prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1344 }
1345 prefixes[u].mod = orig_pref[u].mod;
1346 LY_ARRAY_INCREMENT(prefixes);
1347 }
1348 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02001349 case LY_VALUE_XML:
Michal Vaskoddd76592022-01-17 13:34:48 +01001350 case LY_VALUE_STR_NS:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001351 /* copy all the namespaces */
1352 LY_CHECK_GOTO(ret = ly_set_new(&ns_list), cleanup);
1353 *prefix_data_p = ns_list;
1354
1355 orig_ns = (struct ly_set *)prefix_data;
1356 for (i = 0; i < orig_ns->count; ++i) {
1357 ns = calloc(1, sizeof *ns);
1358 LY_CHECK_ERR_GOTO(!ns, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1359 LY_CHECK_GOTO(ret = ly_set_add(ns_list, ns, 1, NULL), cleanup);
1360
1361 if (((struct lyxml_ns *)orig_ns->objs[i])->prefix) {
1362 ns->prefix = strdup(((struct lyxml_ns *)orig_ns->objs[i])->prefix);
1363 LY_CHECK_ERR_GOTO(!ns->prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1364 }
1365 ns->uri = strdup(((struct lyxml_ns *)orig_ns->objs[i])->uri);
1366 LY_CHECK_ERR_GOTO(!ns->uri, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1367 }
1368 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02001369 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02001370 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02001371 case LY_VALUE_LYB:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001372 assert(!prefix_data);
1373 *prefix_data_p = NULL;
1374 break;
1375 }
1376
1377cleanup:
1378 if (ret) {
1379 ly_free_prefix_data(format, *prefix_data_p);
1380 *prefix_data_p = NULL;
1381 }
1382 return ret;
1383}
1384
1385LY_ERR
Radek Krejcif9943642021-04-26 10:18:21 +02001386ly_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 +02001387 const void *prefix_data, LY_VALUE_FORMAT *format_p, void **prefix_data_p)
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001388{
1389 LY_ERR ret = LY_SUCCESS;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001390 const struct lys_module *mod;
Michal Vaskofc2cd072021-02-24 13:17:17 +01001391 const struct lyxml_ns *ns;
1392 struct lyxml_ns *new_ns;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001393 struct ly_set *ns_list;
1394 struct lysc_prefix *prefixes = NULL, *val_pref;
aPiecek83436bc2021-03-30 12:20:45 +02001395 const char *value_iter, *value_next, *value_end;
1396 uint32_t substr_len;
1397 ly_bool is_prefix;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001398
1399 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001400 case LY_VALUE_SCHEMA:
Michal Vaskofc2cd072021-02-24 13:17:17 +01001401 /* copy all referenced modules as prefix - module pairs */
1402 if (!*prefix_data_p) {
1403 /* new prefix data */
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001404 LY_ARRAY_CREATE_GOTO(ctx, prefixes, 0, ret, cleanup);
Radek Krejci8df109d2021-04-23 12:19:08 +02001405 *format_p = LY_VALUE_SCHEMA_RESOLVED;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001406 *prefix_data_p = prefixes;
Michal Vaskofc2cd072021-02-24 13:17:17 +01001407 } else {
1408 /* reuse prefix data */
Radek Krejci8df109d2021-04-23 12:19:08 +02001409 assert(*format_p == LY_VALUE_SCHEMA_RESOLVED);
Michal Vaskofc2cd072021-02-24 13:17:17 +01001410 prefixes = *prefix_data_p;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001411 }
1412
Michal Vaskoc0f9c4c2022-05-06 12:12:17 +02001413 /* add current module for unprefixed values */
1414 LY_ARRAY_NEW_GOTO(ctx, prefixes, val_pref, ret, cleanup);
1415 *prefix_data_p = prefixes;
1416
1417 val_pref->prefix = NULL;
1418 val_pref->mod = ((const struct lysp_module *)prefix_data)->mod;
1419
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001420 /* add all used prefixes */
Michal Vasko59e90fc2021-09-22 12:17:08 +02001421 value_end = (char *)value + value_len;
aPiecek83436bc2021-03-30 12:20:45 +02001422 for (value_iter = value; value_iter; value_iter = value_next) {
aPieceke3f828d2021-05-10 15:34:41 +02001423 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 +02001424 if (is_prefix) {
1425 /* we have a possible prefix. Do we already have the prefix? */
1426 mod = ly_resolve_prefix(ctx, value_iter, substr_len, *format_p, *prefix_data_p);
1427 if (!mod) {
1428 mod = ly_resolve_prefix(ctx, value_iter, substr_len, format, prefix_data);
1429 if (mod) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001430 assert(*format_p == LY_VALUE_SCHEMA_RESOLVED);
aPiecek83436bc2021-03-30 12:20:45 +02001431 /* store a new prefix - module pair */
1432 LY_ARRAY_NEW_GOTO(ctx, prefixes, val_pref, ret, cleanup);
1433 *prefix_data_p = prefixes;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001434
aPiecek83436bc2021-03-30 12:20:45 +02001435 val_pref->prefix = strndup(value_iter, substr_len);
1436 LY_CHECK_ERR_GOTO(!val_pref->prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1437 val_pref->mod = mod;
1438 } /* else it is not even defined */
1439 } /* else the prefix is already present */
Michal Vaskofc2cd072021-02-24 13:17:17 +01001440 }
1441 }
1442 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02001443 case LY_VALUE_XML:
Michal Vaskoddd76592022-01-17 13:34:48 +01001444 case LY_VALUE_STR_NS:
Michal Vaskofc2cd072021-02-24 13:17:17 +01001445 /* copy all referenced namespaces as prefix - namespace pairs */
1446 if (!*prefix_data_p) {
1447 /* new prefix data */
1448 LY_CHECK_GOTO(ret = ly_set_new(&ns_list), cleanup);
Michal Vaskoddd76592022-01-17 13:34:48 +01001449 *format_p = format;
Michal Vaskofc2cd072021-02-24 13:17:17 +01001450 *prefix_data_p = ns_list;
1451 } else {
1452 /* reuse prefix data */
Michal Vaskoddd76592022-01-17 13:34:48 +01001453 assert(*format_p == format);
Michal Vaskofc2cd072021-02-24 13:17:17 +01001454 ns_list = *prefix_data_p;
1455 }
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001456
Michal Vasko294e7f02022-02-28 13:59:00 +01001457 /* store default namespace */
1458 ns = lyxml_ns_get(prefix_data, NULL, 0);
1459 if (ns) {
1460 new_ns = calloc(1, sizeof *new_ns);
1461 LY_CHECK_ERR_GOTO(!new_ns, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1462 LY_CHECK_GOTO(ret = ly_set_add(ns_list, new_ns, 1, NULL), cleanup);
1463
1464 new_ns->prefix = NULL;
1465 new_ns->uri = strdup(ns->uri);
1466 LY_CHECK_ERR_GOTO(!new_ns->uri, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1467 }
1468
Michal Vaskofc2cd072021-02-24 13:17:17 +01001469 /* add all used prefixes */
Michal Vasko59e90fc2021-09-22 12:17:08 +02001470 value_end = (char *)value + value_len;
aPiecek83436bc2021-03-30 12:20:45 +02001471 for (value_iter = value; value_iter; value_iter = value_next) {
aPieceke3f828d2021-05-10 15:34:41 +02001472 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 +02001473 if (is_prefix) {
1474 /* we have a possible prefix. Do we already have the prefix? */
1475 ns = lyxml_ns_get(ns_list, value_iter, substr_len);
1476 if (!ns) {
1477 ns = lyxml_ns_get(prefix_data, value_iter, substr_len);
1478 if (ns) {
1479 /* store a new prefix - namespace pair */
1480 new_ns = calloc(1, sizeof *new_ns);
1481 LY_CHECK_ERR_GOTO(!new_ns, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1482 LY_CHECK_GOTO(ret = ly_set_add(ns_list, new_ns, 1, NULL), cleanup);
Michal Vaskofc2cd072021-02-24 13:17:17 +01001483
aPiecek83436bc2021-03-30 12:20:45 +02001484 new_ns->prefix = strndup(value_iter, substr_len);
1485 LY_CHECK_ERR_GOTO(!new_ns->prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1486 new_ns->uri = strdup(ns->uri);
1487 LY_CHECK_ERR_GOTO(!new_ns->uri, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1488 } /* else it is not even defined */
1489 } /* else the prefix is already present */
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001490 }
1491 }
1492 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02001493 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02001494 case LY_VALUE_SCHEMA_RESOLVED:
1495 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02001496 case LY_VALUE_LYB:
Michal Vaskofc2cd072021-02-24 13:17:17 +01001497 if (!*prefix_data_p) {
1498 /* new prefix data - simply copy all the prefix data */
1499 *format_p = format;
1500 LY_CHECK_GOTO(ret = ly_dup_prefix_data(ctx, format, prefix_data, prefix_data_p), cleanup);
1501 } /* else reuse prefix data - the prefix data are always the same, nothing to do */
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001502 break;
1503 }
1504
1505cleanup:
1506 if (ret) {
1507 ly_free_prefix_data(*format_p, *prefix_data_p);
1508 *prefix_data_p = NULL;
1509 }
1510 return ret;
1511}
Michal Vasko7ed1fcb2020-12-03 14:15:22 +01001512
1513const char *
Radek Krejci8df109d2021-04-23 12:19:08 +02001514ly_format2str(LY_VALUE_FORMAT format)
Michal Vasko7ed1fcb2020-12-03 14:15:22 +01001515{
1516 switch (format) {
Radek Krejci224d4b42021-04-23 13:54:59 +02001517 case LY_VALUE_CANON:
1518 return "canonical";
Radek Krejci8df109d2021-04-23 12:19:08 +02001519 case LY_VALUE_SCHEMA:
Michal Vasko7ed1fcb2020-12-03 14:15:22 +01001520 return "schema imports";
Radek Krejci8df109d2021-04-23 12:19:08 +02001521 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko7ed1fcb2020-12-03 14:15:22 +01001522 return "schema stored mapping";
Radek Krejci8df109d2021-04-23 12:19:08 +02001523 case LY_VALUE_XML:
Michal Vasko7ed1fcb2020-12-03 14:15:22 +01001524 return "XML prefixes";
Radek Krejci8df109d2021-04-23 12:19:08 +02001525 case LY_VALUE_JSON:
Michal Vasko7ed1fcb2020-12-03 14:15:22 +01001526 return "JSON module names";
Radek Krejcif9943642021-04-26 10:18:21 +02001527 case LY_VALUE_LYB:
1528 return "LYB prefixes";
Michal Vasko7ed1fcb2020-12-03 14:15:22 +01001529 default:
1530 break;
1531 }
1532
1533 return NULL;
1534}
Michal Vasko43297a02021-05-19 11:12:37 +02001535
Michal Vaskof17bb2a2023-02-10 11:34:55 +01001536LIBYANG_API_DEF int
1537ly_time_tz_offset(void)
1538{
Michal Vaskoe50b98d2023-03-28 11:39:01 +02001539 time_t now;
Michal Vaskof17bb2a2023-02-10 11:34:55 +01001540 struct tm tm_local, tm_utc;
1541 int result = 0;
1542
1543 /* init timezone */
1544 tzset();
1545
Michal Vaskoe50b98d2023-03-28 11:39:01 +02001546 /* get current timestamp (need to use it to account for DST) */
1547 now = time(NULL);
1548
Michal Vaskof17bb2a2023-02-10 11:34:55 +01001549 /* get local and UTC time */
Michal Vaskoe50b98d2023-03-28 11:39:01 +02001550 localtime_r(&now, &tm_local);
1551 gmtime_r(&now, &tm_utc);
1552
1553 /* account for year/month/day change by adding/subtracting from the hours */
1554 if (tm_local.tm_mday != tm_utc.tm_mday) {
1555 tm_local.tm_hour += (tm_local.tm_mday - tm_utc.tm_mday) * 24;
1556 }
Michal Vaskof17bb2a2023-02-10 11:34:55 +01001557
1558 /* hours shift in seconds */
1559 result += (tm_local.tm_hour - tm_utc.tm_hour) * 3600;
1560
1561 /* minutes shift in seconds */
1562 result += (tm_local.tm_min - tm_utc.tm_min) * 60;
1563
1564 /* seconds shift */
1565 result += tm_local.tm_sec - tm_utc.tm_sec;
1566
1567 return result;
1568}
1569
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001570LIBYANG_API_DEF LY_ERR
Michal Vasko43297a02021-05-19 11:12:37 +02001571ly_time_str2time(const char *value, time_t *time, char **fractions_s)
1572{
1573 struct tm tm = {0};
1574 uint32_t i, frac_len;
1575 const char *frac;
1576 int64_t shift, shift_m;
1577 time_t t;
1578
1579 LY_CHECK_ARG_RET(NULL, value, time, LY_EINVAL);
1580
1581 tm.tm_year = atoi(&value[0]) - 1900;
1582 tm.tm_mon = atoi(&value[5]) - 1;
1583 tm.tm_mday = atoi(&value[8]);
1584 tm.tm_hour = atoi(&value[11]);
1585 tm.tm_min = atoi(&value[14]);
1586 tm.tm_sec = atoi(&value[17]);
1587
1588 t = timegm(&tm);
1589 i = 19;
1590
1591 /* fractions of a second */
1592 if (value[i] == '.') {
1593 ++i;
1594 frac = &value[i];
1595 for (frac_len = 0; isdigit(frac[frac_len]); ++frac_len) {}
1596
1597 i += frac_len;
Michal Vasko43297a02021-05-19 11:12:37 +02001598 } else {
1599 frac = NULL;
1600 }
1601
1602 /* apply offset */
1603 if ((value[i] == 'Z') || (value[i] == 'z')) {
1604 /* zero shift */
1605 shift = 0;
1606 } else {
1607 shift = strtol(&value[i], NULL, 10);
1608 shift = shift * 60 * 60; /* convert from hours to seconds */
1609 shift_m = strtol(&value[i + 4], NULL, 10) * 60; /* includes conversion from minutes to seconds */
1610 /* correct sign */
1611 if (shift < 0) {
1612 shift_m *= -1;
1613 }
1614 /* connect hours and minutes of the shift */
1615 shift = shift + shift_m;
1616 }
1617
1618 /* we have to shift to the opposite way to correct the time */
1619 t -= shift;
1620
1621 *time = t;
1622 if (fractions_s) {
1623 if (frac) {
1624 *fractions_s = strndup(frac, frac_len);
1625 LY_CHECK_RET(!*fractions_s, LY_EMEM);
1626 } else {
1627 *fractions_s = NULL;
1628 }
1629 }
1630 return LY_SUCCESS;
1631}
1632
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001633LIBYANG_API_DEF LY_ERR
Michal Vasko43297a02021-05-19 11:12:37 +02001634ly_time_time2str(time_t time, const char *fractions_s, char **str)
1635{
1636 struct tm tm;
Michal Vaskof17bb2a2023-02-10 11:34:55 +01001637 char zoneshift[12];
1638 int zonediff_s, zonediff_h, zonediff_m;
Michal Vasko43297a02021-05-19 11:12:37 +02001639
1640 LY_CHECK_ARG_RET(NULL, str, LY_EINVAL);
1641
Michal Vaskof17bb2a2023-02-10 11:34:55 +01001642 /* init timezone */
Michal Vasko43297a02021-05-19 11:12:37 +02001643 tzset();
1644
1645 /* convert */
1646 if (!localtime_r(&time, &tm)) {
1647 return LY_ESYS;
1648 }
1649
Michal Vaskoe50b98d2023-03-28 11:39:01 +02001650 /* get timezone offset (do not use tm_gmtoff to avoid portability problems) */
Michal Vaskof17bb2a2023-02-10 11:34:55 +01001651 zonediff_s = ly_time_tz_offset();
1652 zonediff_h = zonediff_s / 60 / 60;
1653 zonediff_m = zonediff_s / 60 % 60;
Michal Vasko43297a02021-05-19 11:12:37 +02001654 sprintf(zoneshift, "%+03d:%02d", zonediff_h, zonediff_m);
1655
1656 /* print */
1657 if (asprintf(str, "%04d-%02d-%02dT%02d:%02d:%02d%s%s%s",
1658 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
1659 fractions_s ? "." : "", fractions_s ? fractions_s : "", zoneshift) == -1) {
1660 return LY_EMEM;
1661 }
1662
1663 return LY_SUCCESS;
1664}
1665
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001666LIBYANG_API_DEF LY_ERR
Michal Vasko43297a02021-05-19 11:12:37 +02001667ly_time_str2ts(const char *value, struct timespec *ts)
1668{
1669 LY_ERR rc;
Michal Vasko72975062021-08-25 08:13:04 +02001670 char *fractions_s, frac_buf[10];
Michal Vasko43297a02021-05-19 11:12:37 +02001671 int frac_len;
1672
1673 LY_CHECK_ARG_RET(NULL, value, ts, LY_EINVAL);
1674
1675 rc = ly_time_str2time(value, &ts->tv_sec, &fractions_s);
1676 LY_CHECK_RET(rc);
1677
1678 /* convert fractions of a second to nanoseconds */
1679 if (fractions_s) {
Michal Vasko72975062021-08-25 08:13:04 +02001680 /* init frac_buf with zeroes */
1681 memset(frac_buf, '0', 9);
1682 frac_buf[9] = '\0';
1683
Michal Vasko43297a02021-05-19 11:12:37 +02001684 frac_len = strlen(fractions_s);
1685 memcpy(frac_buf, fractions_s, frac_len > 9 ? 9 : frac_len);
1686 ts->tv_nsec = atol(frac_buf);
1687 free(fractions_s);
1688 } else {
1689 ts->tv_nsec = 0;
1690 }
1691
1692 return LY_SUCCESS;
1693}
1694
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001695LIBYANG_API_DEF LY_ERR
Michal Vasko43297a02021-05-19 11:12:37 +02001696ly_time_ts2str(const struct timespec *ts, char **str)
1697{
1698 char frac_buf[10];
1699
Jan Kundrátbd157002021-08-30 14:02:22 +02001700 LY_CHECK_ARG_RET(NULL, ts, str, ((ts->tv_nsec <= 999999999) && (ts->tv_nsec >= 0)), LY_EINVAL);
Michal Vasko43297a02021-05-19 11:12:37 +02001701
1702 /* convert nanoseconds to fractions of a second */
1703 if (ts->tv_nsec) {
1704 sprintf(frac_buf, "%09ld", ts->tv_nsec);
1705 }
1706
1707 return ly_time_time2str(ts->tv_sec, ts->tv_nsec ? frac_buf : NULL, str);
1708}