blob: b98ce7788d79437015a6a78cd53969736a3dd5df [file] [log] [blame]
Radek Krejcie7b95092019-05-15 11:03:07 +02001/**
2 * @file tree_data_helpers.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Parsing and validation helper functions for data trees
5 *
Michal Vasko8cc3f662022-03-29 11:25:51 +02006 * Copyright (c) 2015 - 2022 CESNET, z.s.p.o.
Radek Krejcie7b95092019-05-15 11:03:07 +02007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Michal Vasko43297a02021-05-19 11:12:37 +020014
15#define _GNU_SOURCE /* asprintf, strdup */
Radek Krejcie7b95092019-05-15 11:03:07 +020016
17#include <assert.h>
Michal Vasko43297a02021-05-19 11:12:37 +020018#include <ctype.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020019#include <stdint.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020020#include <stdlib.h>
Radek Krejciad97c5f2020-06-30 09:19:28 +020021#include <string.h>
Michal Vasko43297a02021-05-19 11:12:37 +020022#include <time.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020023
Radek Krejci535ea9f2020-05-29 16:01:05 +020024#include "common.h"
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"
Radek Krejciad97c5f2020-06-30 09:19:28 +020030#include "lyb.h"
Radek Krejci7931b192020-06-25 17:05:03 +020031#include "parser_data.h"
Michal Vasko8cc3f662022-03-29 11:25:51 +020032#include "plugins_exts.h"
Michal Vaskoa820c312021-02-05 16:33:00 +010033#include "printer_data.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020034#include "set.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020035#include "tree.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020036#include "tree_data.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020037#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010038#include "tree_edit.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020039#include "tree_schema.h"
Radek Krejci0aa1f702021-04-01 16:16:19 +020040#include "tree_schema_internal.h"
Radek Krejci4f2e3e52021-03-30 14:20:28 +020041#include "validation.h"
Radek Krejci77114102021-03-10 15:21:57 +010042#include "xml.h"
aPiecekdf23eee2021-10-07 12:21:50 +020043#include "xpath.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020044
Michal Vaskod7c048c2021-05-18 16:12:55 +020045/**
46 * @brief Find an entry in duplicate instance cache for an instance. Create it if it does not exist.
47 *
48 * @param[in] first_inst Instance of the cache entry.
49 * @param[in,out] dup_inst_cache Duplicate instance cache.
50 * @return Instance cache entry.
51 */
52static struct lyd_dup_inst *
53lyd_dup_inst_get(const struct lyd_node *first_inst, struct lyd_dup_inst **dup_inst_cache)
54{
55 struct lyd_dup_inst *item;
56 LY_ARRAY_COUNT_TYPE u;
57
58 LY_ARRAY_FOR(*dup_inst_cache, u) {
59 if ((*dup_inst_cache)[u].inst_set->dnodes[0] == first_inst) {
60 return &(*dup_inst_cache)[u];
61 }
62 }
63
64 /* it was not added yet, add it now */
65 LY_ARRAY_NEW_RET(LYD_CTX(first_inst), *dup_inst_cache, item, NULL);
66
67 return item;
68}
69
70LY_ERR
71lyd_dup_inst_next(struct lyd_node **inst, const struct lyd_node *siblings, struct lyd_dup_inst **dup_inst_cache)
72{
73 struct lyd_dup_inst *dup_inst;
74
75 if (!*inst || !lysc_is_dup_inst_list((*inst)->schema)) {
76 /* no match or not dup-inst list, inst is unchanged */
77 return LY_SUCCESS;
78 }
79
80 /* there can be more exact same instances and we must make sure we do not match a single node more times */
81 dup_inst = lyd_dup_inst_get(*inst, dup_inst_cache);
82 LY_CHECK_ERR_RET(!dup_inst, LOGMEM(LYD_CTX(siblings)), LY_EMEM);
83
84 if (!dup_inst->used) {
85 /* we did not cache these instances yet, do so */
86 lyd_find_sibling_dup_inst_set(siblings, *inst, &dup_inst->inst_set);
87 assert(dup_inst->inst_set->count && (dup_inst->inst_set->dnodes[0] == *inst));
88 }
89
90 if (dup_inst->used == dup_inst->inst_set->count) {
91 /* we have used all the instances */
92 *inst = NULL;
93 } else {
94 assert(dup_inst->used < dup_inst->inst_set->count);
95
96 /* use another instance */
97 *inst = dup_inst->inst_set->dnodes[dup_inst->used];
98 ++dup_inst->used;
99 }
100
101 return LY_SUCCESS;
102}
103
104void
105lyd_dup_inst_free(struct lyd_dup_inst *dup_inst)
106{
107 LY_ARRAY_COUNT_TYPE u;
108
109 LY_ARRAY_FOR(dup_inst, u) {
110 ly_set_free(dup_inst[u].inst_set, NULL);
111 }
112 LY_ARRAY_FREE(dup_inst);
113}
114
Michal Vaskoa6669ba2020-08-06 16:14:26 +0200115struct lyd_node *
116lys_getnext_data(const struct lyd_node *last, const struct lyd_node *sibling, const struct lysc_node **slast,
Radek Krejci0f969882020-08-21 16:56:47 +0200117 const struct lysc_node *parent, const struct lysc_module *module)
Michal Vaskoa6669ba2020-08-06 16:14:26 +0200118{
119 const struct lysc_node *siter = NULL;
120 struct lyd_node *match = NULL;
121
122 assert(parent || module);
123 assert(!last || (slast && *slast));
124
125 if (slast) {
126 siter = *slast;
127 }
128
129 if (last && last->next && (last->next->schema == siter)) {
130 /* return next data instance */
131 return last->next;
132 }
133
134 /* find next schema node data instance */
135 while ((siter = lys_getnext(siter, parent, module, 0))) {
136 if (!lyd_find_sibling_val(sibling, siter, NULL, 0, &match)) {
137 break;
138 }
139 }
140
141 if (slast) {
142 *slast = siter;
143 }
144 return match;
145}
146
Radek Krejcie7b95092019-05-15 11:03:07 +0200147struct lyd_node **
Michal Vaskoe0665742021-02-11 11:08:44 +0100148lyd_node_child_p(struct lyd_node *node)
Radek Krejcie7b95092019-05-15 11:03:07 +0200149{
150 assert(node);
Michal Vasko52927e22020-03-16 17:26:14 +0100151
152 if (!node->schema) {
153 return &((struct lyd_node_opaq *)node)->child;
154 } else {
155 switch (node->schema->nodetype) {
156 case LYS_CONTAINER:
157 case LYS_LIST:
Michal Vasko1bf09392020-03-27 12:38:10 +0100158 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +0100159 case LYS_ACTION:
160 case LYS_NOTIF:
161 return &((struct lyd_node_inner *)node)->child;
162 default:
163 return NULL;
164 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200165 }
166}
167
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100168LIBYANG_API_DEF LY_ERR
aPiecekdf23eee2021-10-07 12:21:50 +0200169lyxp_vars_set(struct lyxp_var **vars, const char *name, const char *value)
170{
171 LY_ERR ret = LY_SUCCESS;
172 char *var_name = NULL, *var_value = NULL;
173 struct lyxp_var *item;
174
175 if (!vars || !name || !value) {
176 return LY_EINVAL;
177 }
178
179 /* If variable is already defined then change its value. */
180 if (*vars && !lyxp_vars_find(*vars, name, 0, &item)) {
181 var_value = strdup(value);
182 LY_CHECK_RET(!var_value, LY_EMEM);
183
184 /* Set new value. */
185 free(item->value);
186 item->value = var_value;
187 } else {
188 var_name = strdup(name);
189 var_value = strdup(value);
190 LY_CHECK_ERR_GOTO(!var_name || !var_value, ret = LY_EMEM, error);
191
192 /* Add new variable. */
193 LY_ARRAY_NEW_GOTO(NULL, *vars, item, ret, error);
194 item->name = var_name;
195 item->value = var_value;
196 }
197
198 return LY_SUCCESS;
199
200error:
201 free(var_name);
202 free(var_value);
203 return ret;
204}
205
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100206LIBYANG_API_DEF void
aPiecekdf23eee2021-10-07 12:21:50 +0200207lyxp_vars_free(struct lyxp_var *vars)
208{
209 LY_ARRAY_COUNT_TYPE u;
210
211 if (!vars) {
212 return;
213 }
214
215 LY_ARRAY_FOR(vars, u) {
216 free(vars[u].name);
217 free(vars[u].value);
218 }
219
220 LY_ARRAY_FREE(vars);
221}
222
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100223LIBYANG_API_DEF struct lyd_node *
Radek Krejcia1c1e542020-09-29 16:06:52 +0200224lyd_child_no_keys(const struct lyd_node *node)
225{
226 struct lyd_node **children;
227
228 if (!node) {
229 return NULL;
230 }
231
232 if (!node->schema) {
233 /* opaq node */
Michal Vasko9e685082021-01-29 14:49:09 +0100234 return ((struct lyd_node_opaq *)node)->child;
Radek Krejcia1c1e542020-09-29 16:06:52 +0200235 }
236
Michal Vaskoe0665742021-02-11 11:08:44 +0100237 children = lyd_node_child_p((struct lyd_node *)node);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200238 if (children) {
239 struct lyd_node *child = *children;
240 while (child && child->schema && (child->schema->flags & LYS_KEY)) {
241 child = child->next;
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200242 }
243 return child;
Radek Krejcie7b95092019-05-15 11:03:07 +0200244 } else {
245 return NULL;
246 }
247}
Michal Vasko9b368d32020-02-14 13:53:31 +0100248
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100249LIBYANG_API_DEF const struct lys_module *
Michal Vaskoc193ce92020-03-06 11:04:48 +0100250lyd_owner_module(const struct lyd_node *node)
Michal Vasko9b368d32020-02-14 13:53:31 +0100251{
Michal Vaskod5cfa6e2020-11-23 16:56:08 +0100252 const struct lyd_node_opaq *opaq;
Michal Vasko9b368d32020-02-14 13:53:31 +0100253
Michal Vaskod5cfa6e2020-11-23 16:56:08 +0100254 if (!node) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100255 return NULL;
256 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100257
Michal Vaskod5cfa6e2020-11-23 16:56:08 +0100258 if (!node->schema) {
259 opaq = (struct lyd_node_opaq *)node;
260 switch (opaq->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200261 case LY_VALUE_XML:
Michal Vaskod5cfa6e2020-11-23 16:56:08 +0100262 return ly_ctx_get_module_implemented_ns(LYD_CTX(node), opaq->name.module_ns);
Radek Krejci8df109d2021-04-23 12:19:08 +0200263 case LY_VALUE_JSON:
Michal Vaskod5cfa6e2020-11-23 16:56:08 +0100264 return ly_ctx_get_module_implemented(LYD_CTX(node), opaq->name.module_name);
265 default:
266 return NULL;
267 }
268 }
269
Michal Vaskoef53c812021-10-13 10:21:03 +0200270 return lysc_owner_module(node->schema);
Michal Vasko9b368d32020-02-14 13:53:31 +0100271}
Michal Vaskob1b5c262020-03-05 14:29:47 +0100272
Michal Vasko598063b2021-07-19 11:39:05 +0200273void
274lyd_first_module_sibling(struct lyd_node **node, const struct lys_module *mod)
275{
276 int cmp;
277 struct lyd_node *first;
278
279 assert(node && mod);
280
281 if (!*node) {
282 return;
283 }
284
285 first = *node;
286 cmp = strcmp(lyd_owner_module(first)->name, mod->name);
287 if (cmp > 0) {
288 /* there may be some preceding data */
289 while (first->prev->next) {
290 first = first->prev;
291 if (lyd_owner_module(first) == mod) {
292 cmp = 0;
293 break;
294 }
295 }
296 }
297
298 if (cmp == 0) {
299 /* there may be some preceding data belonging to this module */
300 while (first->prev->next) {
301 if (lyd_owner_module(first->prev) != mod) {
302 break;
303 }
304 first = first->prev;
305 }
306 }
307
308 if (cmp < 0) {
309 /* there may be some following data */
310 LY_LIST_FOR(first, first) {
311 if (lyd_owner_module(first) == mod) {
312 cmp = 0;
313 break;
314 }
315 }
316 }
317
318 if (cmp == 0) {
319 /* we have found the first module data node */
320 *node = first;
321 }
322}
323
Michal Vaskob1b5c262020-03-05 14:29:47 +0100324const struct lys_module *
Michal Vasko26e80012020-07-08 10:55:46 +0200325lyd_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 +0200326 struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +0100327{
328 struct lyd_node *iter;
329 const struct lys_module *mod;
330
331 /* get the next module */
Michal Vasko26e80012020-07-08 10:55:46 +0200332 if (module) {
333 if (*i) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100334 mod = NULL;
Michal Vasko26e80012020-07-08 10:55:46 +0200335 } else {
336 mod = module;
337 ++(*i);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100338 }
339 } else {
340 do {
341 mod = ly_ctx_get_module_iter(ctx, i);
342 } while (mod && !mod->implemented);
343 }
344
345 /* find its data */
346 *first = NULL;
347 if (mod) {
348 LY_LIST_FOR(tree, iter) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100349 if (lyd_owner_module(iter) == mod) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100350 *first = iter;
351 break;
352 }
353 }
354 }
355
356 return mod;
357}
358
359const struct lys_module *
360lyd_data_next_module(struct lyd_node **next, struct lyd_node **first)
361{
362 const struct lys_module *mod;
363
364 if (!*next) {
365 /* all data traversed */
366 *first = NULL;
367 return NULL;
368 }
369
370 *first = *next;
371
372 /* prepare next */
Michal Vaskoc193ce92020-03-06 11:04:48 +0100373 mod = lyd_owner_module(*next);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100374 LY_LIST_FOR(*next, *next) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100375 if (lyd_owner_module(*next) != mod) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100376 break;
377 }
378 }
379
380 return mod;
381}
Michal Vasko9f96a052020-03-10 09:41:45 +0100382
383LY_ERR
384lyd_parse_check_keys(struct lyd_node *node)
385{
386 const struct lysc_node *skey = NULL;
387 const struct lyd_node *key;
388
389 assert(node->schema->nodetype == LYS_LIST);
390
Radek Krejcia1c1e542020-09-29 16:06:52 +0200391 key = lyd_child(node);
Michal Vasko9f96a052020-03-10 09:41:45 +0100392 while ((skey = lys_getnext(skey, node->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
393 if (!key || (key->schema != skey)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100394 LOGVAL(LYD_CTX(node), LY_VCODE_NOKEY, skey->name);
Michal Vasko9f96a052020-03-10 09:41:45 +0100395 return LY_EVALID;
396 }
397
398 key = key->next;
399 }
400
401 return LY_SUCCESS;
402}
Michal Vasko60ea6352020-06-29 13:39:39 +0200403
Michal Vasko8cc3f662022-03-29 11:25:51 +0200404LY_ERR
405lyd_parse_set_data_flags(struct lyd_node *node, struct lyd_meta **meta, struct lyd_ctx *lydctx,
406 struct lysc_ext_instance *ext)
Michal Vasko60ea6352020-06-29 13:39:39 +0200407{
408 struct lyd_meta *meta2, *prev_meta = NULL;
Michal Vasko8cc3f662022-03-29 11:25:51 +0200409 struct lyd_ctx_ext_val *ext_val;
Michal Vasko60ea6352020-06-29 13:39:39 +0200410
Michal Vaskoa5705e52020-12-09 18:15:14 +0100411 if (lysc_has_when(node->schema)) {
Michal Vasko8cc3f662022-03-29 11:25:51 +0200412 if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200413 /* remember we need to evaluate this node's when */
Michal Vasko8cc3f662022-03-29 11:25:51 +0200414 LY_CHECK_RET(ly_set_add(&lydctx->node_when, node, 1, NULL));
Michal Vasko60ea6352020-06-29 13:39:39 +0200415 }
416 }
417
Michal Vasko60ea6352020-06-29 13:39:39 +0200418 LY_LIST_FOR(*meta, meta2) {
Michal Vasko69730152020-10-09 16:30:07 +0200419 if (!strcmp(meta2->name, "default") && !strcmp(meta2->annotation->module->name, "ietf-netconf-with-defaults") &&
420 meta2->value.boolean) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200421 /* node is default according to the metadata */
422 node->flags |= LYD_DEFAULT;
423
424 /* delete the metadata */
425 if (prev_meta) {
426 prev_meta->next = meta2->next;
427 } else {
428 *meta = (*meta)->next;
429 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200430 lyd_free_meta_single(meta2);
Michal Vasko60ea6352020-06-29 13:39:39 +0200431 break;
432 }
433
434 prev_meta = meta2;
435 }
Michal Vasko8cc3f662022-03-29 11:25:51 +0200436
437 if (ext) {
438 /* parsed for an extension */
439 node->flags |= LYD_EXT;
440
441 if (!(lydctx->parse_opts & LYD_PARSE_ONLY)) {
442 /* rememeber for validation */
443 ext_val = malloc(sizeof *ext_val);
444 LY_CHECK_ERR_RET(!ext_val, LOGMEM(LYD_CTX(node)), LY_EMEM);
445 ext_val->ext = ext;
446 ext_val->sibling = node;
447 LY_CHECK_RET(ly_set_add(&lydctx->ext_val, ext_val, 1, NULL));
448 }
449 }
450
451 return LY_SUCCESS;
Michal Vasko60ea6352020-06-29 13:39:39 +0200452}
453
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100454/**
455 * @brief Check list node parsed into an opaque node for the reason.
456 *
457 * @param[in] node Opaque node.
458 * @param[in] snode Schema node of @p opaq.
459 * @return LY_SUCCESS if the node is valid;
460 * @return LY_ERR on error.
461 */
462static LY_ERR
463lyd_parse_opaq_list_error(const struct lyd_node *node, const struct lysc_node *snode)
464{
465 LY_ERR ret = LY_SUCCESS;
466 struct ly_set key_set = {0};
467 const struct lysc_node *key = NULL;
468 const struct lyd_node *child;
469 const struct lyd_node_opaq *opaq_k;
470 uint32_t i;
471
472 assert(!node->schema);
473
474 /* get all keys into a set */
475 while ((key = lys_getnext(key, snode, NULL, 0)) && (snode->flags & LYS_KEY)) {
476 LY_CHECK_GOTO(ret = ly_set_add(&key_set, (void *)snode, 1, NULL), cleanup);
477 }
478
479 LY_LIST_FOR(lyd_child(node), child) {
480 if (child->schema) {
481 LOGERR(LYD_CTX(node), LY_EINVAL, "Unexpected node %s \"%s\".", lys_nodetype2str(child->schema->nodetype),
482 LYD_NAME(child));
483 ret = LY_EINVAL;
484 goto cleanup;
485 }
486
487 opaq_k = (struct lyd_node_opaq *)child;
488
489 /* find the key schema node */
490 for (i = 0; i < key_set.count; ++i) {
491 key = key_set.snodes[i];
492 if (!strcmp(key->name, opaq_k->name.name)) {
493 break;
494 }
495 }
496 if (i == key_set.count) {
497 /* some other node, skip */
498 continue;
499 }
500
501 /* key found */
502 ly_set_rm_index(&key_set, i, NULL);
503
504 /* check value */
505 ret = lys_value_validate(LYD_CTX(node), key, opaq_k->value, strlen(opaq_k->value), opaq_k->format,
506 opaq_k->val_prefix_data);
507 LY_CHECK_GOTO(ret, cleanup);
508 }
509
510 if (key_set.count) {
511 /* missing keys */
512 LOGVAL(LYD_CTX(node), LY_VCODE_NOKEY, key_set.snodes[0]->name);
513 ret = LY_EVALID;
514 goto cleanup;
515 }
516
517cleanup:
518 ly_set_erase(&key_set, NULL);
519 return ret;
520}
521
522LIBYANG_API_DEF LY_ERR
523lyd_parse_opaq_error(const struct lyd_node *node)
524{
525 const struct ly_ctx *ctx;
526 const struct lyd_node_opaq *opaq;
527 const struct lyd_node *parent;
528 const struct lys_module *mod;
529 const struct lysc_node *snode;
530
531 LY_CHECK_ARG_RET(LYD_CTX(node), node, !node->schema, !lyd_parent(node) || lyd_parent(node)->schema, LY_EINVAL);
532
533 ctx = LYD_CTX(node);
534 opaq = (struct lyd_node_opaq *)node;
535 parent = lyd_parent(node);
536
537 /* is always filled by parsers */
538 LY_CHECK_ARG_RET(ctx, opaq->name.module_ns, LY_EINVAL);
539
540 /* module */
541 switch (opaq->format) {
542 case LY_VALUE_XML:
543 if (!parent || strcmp(opaq->name.module_ns, parent->schema->module->ns)) {
544 mod = ly_ctx_get_module_implemented_ns(ctx, opaq->name.module_ns);
545 if (!mod) {
546 LOGVAL(ctx, LYVE_REFERENCE, "No (implemented) module with namespace \"%s\" in the context.",
547 opaq->name.module_ns);
548 return LY_EVALID;
549 }
550 } else {
551 /* inherit */
552 mod = parent->schema->module;
553 }
554 break;
555 case LY_VALUE_JSON:
556 case LY_VALUE_LYB:
557 if (!parent || strcmp(opaq->name.module_name, parent->schema->module->name)) {
558 mod = ly_ctx_get_module_implemented(ctx, opaq->name.module_name);
559 if (!mod) {
560 LOGVAL(ctx, LYVE_REFERENCE, "No (implemented) module named \"%s\" in the context.", opaq->name.module_name);
561 return LY_EVALID;
562 }
563 } else {
564 /* inherit */
565 mod = parent->schema->module;
566 }
567 break;
568 default:
569 LOGERR(ctx, LY_EINVAL, "Unsupported value format.");
570 return LY_EINVAL;
571 }
572
573 /* schema */
574 snode = lys_find_child(parent ? parent->schema : NULL, mod, opaq->name.name, 0, 0, 0);
Michal Vaskoac6f4be2022-05-02 10:16:50 +0200575 if (!snode && parent && parent->schema && (parent->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
576 /* maybe output node */
Michal Vasko89afc6e2022-05-02 10:24:26 +0200577 snode = lys_find_child(parent->schema, mod, opaq->name.name, 0, 0, LYS_GETNEXT_OUTPUT);
Michal Vaskoac6f4be2022-05-02 10:16:50 +0200578 }
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100579 if (!snode) {
580 if (parent) {
581 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%s\" not found as a child of \"%s\" node.", opaq->name.name,
582 LYD_NAME(parent));
583 } else {
584 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%s\" not found in the \"%s\" module.", opaq->name.name, mod->name);
585 }
586 return LY_EVALID;
587 }
588
589 if (snode->nodetype & LYD_NODE_TERM) {
590 /* leaf / leaf-list */
591 LY_CHECK_RET(lys_value_validate(ctx, snode, opaq->value, strlen(opaq->value), opaq->format, opaq->val_prefix_data));
592 } else if (snode->nodetype == LYS_LIST) {
593 /* list */
594 LY_CHECK_RET(lyd_parse_opaq_list_error(node, snode));
595 } else if (snode->nodetype & LYD_NODE_INNER) {
596 /* inner node */
597 if (opaq->value) {
598 LOGVAL(ctx, LYVE_DATA, "Invalid value \"%s\" for %s \"%s\".", opaq->value,
599 lys_nodetype2str(snode->nodetype), snode->name);
600 return LY_EVALID;
601 }
602 } else {
603 LOGERR(ctx, LY_EINVAL, "Unexpected opaque schema node %s \"%s\".", lys_nodetype2str(snode->nodetype), snode->name);
604 return LY_EINVAL;
605 }
606
607 LOGERR(ctx, LY_EINVAL, "Unexpected valid opaque node %s \"%s\".", lys_nodetype2str(snode->nodetype), snode->name);
608 return LY_EINVAL;
609}
610
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100611LIBYANG_API_DEF const char *
Christian Hopps46bd21b2021-04-27 09:43:58 -0400612lyd_value_get_canonical(const struct ly_ctx *ctx, const struct lyd_value *value)
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200613{
Michal Vaskoab40e7e2021-04-28 17:04:24 +0200614 LY_CHECK_ARG_RET(ctx, ctx, value, NULL);
615
Michal Vasko33876022021-04-27 16:42:24 +0200616 return value->_canonical ? value->_canonical :
617 (const char *)value->realtype->plugin->print(ctx, value, LY_VALUE_CANON, NULL, NULL, NULL);
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200618}
619
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100620LIBYANG_API_DEF LY_ERR
Michal Vaskoa820c312021-02-05 16:33:00 +0100621lyd_any_value_str(const struct lyd_node *any, char **value_str)
622{
623 const struct lyd_node_any *a;
624 struct lyd_node *tree = NULL;
625 const char *str = NULL;
626 ly_bool dynamic = 0;
627 LY_ERR ret = LY_SUCCESS;
628
629 LY_CHECK_ARG_RET(NULL, any, value_str, LY_EINVAL);
Radek Krejci71877df2021-04-06 17:24:06 +0200630 LY_CHECK_ARG_RET(NULL, any->schema, any->schema->nodetype & LYS_ANYDATA, LY_EINVAL);
Michal Vaskoa820c312021-02-05 16:33:00 +0100631
632 a = (struct lyd_node_any *)any;
633 *value_str = NULL;
634
635 if (!a->value.str) {
636 /* there is no value in the union */
637 return LY_SUCCESS;
638 }
639
640 switch (a->value_type) {
641 case LYD_ANYDATA_LYB:
642 /* parse into a data tree */
643 ret = lyd_parse_data_mem(LYD_CTX(any), a->value.mem, LYD_LYB, LYD_PARSE_ONLY, 0, &tree);
644 LY_CHECK_GOTO(ret, cleanup);
645 dynamic = 1;
646 break;
647 case LYD_ANYDATA_DATATREE:
648 tree = a->value.tree;
649 break;
650 case LYD_ANYDATA_STRING:
651 case LYD_ANYDATA_XML:
652 case LYD_ANYDATA_JSON:
653 /* simply use the string */
654 str = a->value.str;
655 break;
656 }
657
658 if (tree) {
659 /* print into a string */
660 ret = lyd_print_mem(value_str, tree, LYD_XML, LYD_PRINT_WITHSIBLINGS);
661 LY_CHECK_GOTO(ret, cleanup);
662 } else {
663 assert(str);
664 *value_str = strdup(str);
665 LY_CHECK_ERR_GOTO(!*value_str, LOGMEM(LYD_CTX(any)), cleanup);
666 }
667
668 /* success */
669
670cleanup:
671 if (dynamic) {
672 lyd_free_all(tree);
673 }
674 return ret;
675}
676
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100677LIBYANG_API_DEF LY_ERR
Michal Vasko61551fa2020-07-09 15:45:45 +0200678lyd_any_copy_value(struct lyd_node *trg, const union lyd_any_value *value, LYD_ANYDATA_VALUETYPE value_type)
679{
680 struct lyd_node_any *t;
Michal Vasko61551fa2020-07-09 15:45:45 +0200681
Michal Vaskoa820c312021-02-05 16:33:00 +0100682 LY_CHECK_ARG_RET(NULL, trg, LY_EINVAL);
Radek Krejci71877df2021-04-06 17:24:06 +0200683 LY_CHECK_ARG_RET(NULL, trg->schema, trg->schema->nodetype & LYS_ANYDATA, LY_EINVAL);
Michal Vasko61551fa2020-07-09 15:45:45 +0200684
685 t = (struct lyd_node_any *)trg;
686
687 /* free trg */
688 switch (t->value_type) {
689 case LYD_ANYDATA_DATATREE:
690 lyd_free_all(t->value.tree);
691 break;
692 case LYD_ANYDATA_STRING:
693 case LYD_ANYDATA_XML:
694 case LYD_ANYDATA_JSON:
Michal Vaskoe180ed02021-02-05 16:31:20 +0100695 lydict_remove(LYD_CTX(trg), t->value.str);
Michal Vasko61551fa2020-07-09 15:45:45 +0200696 break;
697 case LYD_ANYDATA_LYB:
698 free(t->value.mem);
699 break;
700 }
701 t->value.str = NULL;
702
703 if (!value) {
704 /* only free value in this case */
705 return LY_SUCCESS;
706 }
707
708 /* copy src */
709 t->value_type = value_type;
710 switch (value_type) {
711 case LYD_ANYDATA_DATATREE:
712 if (value->tree) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200713 LY_CHECK_RET(lyd_dup_siblings(value->tree, NULL, LYD_DUP_RECURSIVE, &t->value.tree));
Michal Vasko61551fa2020-07-09 15:45:45 +0200714 }
715 break;
716 case LYD_ANYDATA_STRING:
717 case LYD_ANYDATA_XML:
718 case LYD_ANYDATA_JSON:
719 if (value->str) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200720 LY_CHECK_RET(lydict_insert(LYD_CTX(trg), value->str, 0, &t->value.str));
Michal Vasko61551fa2020-07-09 15:45:45 +0200721 }
722 break;
723 case LYD_ANYDATA_LYB:
724 if (value->mem) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200725 int len = lyd_lyb_data_length(value->mem);
Radek Krejci82fa8d42020-07-11 22:00:59 +0200726 LY_CHECK_RET(len == -1, LY_EINVAL);
Michal Vasko61551fa2020-07-09 15:45:45 +0200727 t->value.mem = malloc(len);
Michal Vaskob7be7a82020-08-20 09:09:04 +0200728 LY_CHECK_ERR_RET(!t->value.mem, LOGMEM(LYD_CTX(trg)), LY_EMEM);
Michal Vasko61551fa2020-07-09 15:45:45 +0200729 memcpy(t->value.mem, value->mem, len);
730 }
731 break;
732 }
733
734 return LY_SUCCESS;
735}
736
Michal Vasko106f0862021-11-02 11:49:27 +0100737const struct lysc_node *
738lyd_node_schema(const struct lyd_node *node)
739{
740 const struct lysc_node *schema = NULL;
741 const struct lyd_node *prev_iter = NULL, *iter;
742 const struct lys_module *mod;
743
744 if (!node) {
745 return NULL;
746 } else if (node->schema) {
747 return node->schema;
748 }
749
750 /* get schema node of an opaque node */
751 do {
752 /* get next data node */
753 for (iter = node; lyd_parent(iter) != prev_iter; iter = lyd_parent(iter)) {}
754
755 /* get equivalent schema node */
756 if (iter->schema) {
757 schema = iter->schema;
758 } else {
759 /* get module */
760 mod = lyd_owner_module(iter);
Michal Vaskoa41826a2021-11-02 12:13:03 +0100761 if (!mod && !schema) {
762 /* top-level opaque node has unknown module */
763 break;
764 }
Michal Vasko106f0862021-11-02 11:49:27 +0100765
766 /* get schema node */
767 schema = lys_find_child(schema, mod ? mod : schema->module, LYD_NAME(iter), 0, 0, 0);
768 }
Michal Vaskod2f404f2021-11-04 15:37:11 +0100769
770 /* remember to move to the descendant */
771 prev_iter = iter;
Michal Vasko106f0862021-11-02 11:49:27 +0100772 } while (schema && (iter != node));
773
774 return schema;
775}
776
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100777void
Michal Vaskod3bb12f2020-12-04 14:33:09 +0100778lyd_del_move_root(struct lyd_node **root, const struct lyd_node *to_del, const struct lys_module *mod)
779{
780 if (*root && (lyd_owner_module(*root) != mod)) {
781 /* there are no data of mod so this is simply the first top-level sibling */
782 mod = NULL;
783 }
784
785 if ((*root != to_del) || (*root)->parent) {
786 return;
787 }
788
Michal Vasko598063b2021-07-19 11:39:05 +0200789 if (mod && (*root)->prev->next && (!(*root)->next || (lyd_owner_module(to_del) != lyd_owner_module((*root)->next)))) {
790 /* there are no more nodes from mod, simply get the first top-level sibling */
Michal Vaskod3bb12f2020-12-04 14:33:09 +0100791 *root = lyd_first_sibling(*root);
Michal Vasko598063b2021-07-19 11:39:05 +0200792 } else {
793 *root = (*root)->next;
Michal Vaskod3bb12f2020-12-04 14:33:09 +0100794 }
795}
796
Michal Vasko8cc3f662022-03-29 11:25:51 +0200797LY_ERR
798ly_nested_ext_schema(const struct lyd_node *parent, const struct lysc_node *sparent, const char *prefix,
799 size_t prefix_len, LY_VALUE_FORMAT format, void *prefix_data, const char *name, size_t name_len,
800 const struct lysc_node **snode, struct lysc_ext_instance **ext)
801{
802 LY_ERR r;
803 LY_ARRAY_COUNT_TYPE u;
804 struct lysc_ext_instance *nested_exts = NULL;
805 lyplg_ext_data_snode_clb ext_snode_cb;
806
807 /* check if there are any nested extension instances */
808 if (parent && parent->schema) {
809 nested_exts = parent->schema->exts;
810 } else if (sparent) {
811 nested_exts = sparent->exts;
812 }
813 LY_ARRAY_FOR(nested_exts, u) {
Michal Vasko305c6cb2022-04-27 10:33:04 +0200814 if (!nested_exts[u].def->plugin) {
815 /* no plugin */
816 continue;
817 }
818
Michal Vasko8cc3f662022-03-29 11:25:51 +0200819 ext_snode_cb = nested_exts[u].def->plugin->snode;
820 if (!ext_snode_cb) {
821 /* not an extension with nested data */
822 continue;
823 }
824
825 /* try to get the schema node */
826 r = ext_snode_cb(&nested_exts[u], parent, sparent, prefix, prefix_len, format, prefix_data, name, name_len, snode);
827 if (!r) {
828 /* data successfully created, remember the ext instance */
829 *ext = &nested_exts[u];
830 return LY_SUCCESS;
831 } else if (r != LY_ENOT) {
832 /* fatal error */
833 return r;
834 }
835 /* data was not from this module, continue */
836 }
837
838 /* no extensions or none matched */
839 return LY_ENOT;
840}
841
Michal Vaskod3bb12f2020-12-04 14:33:09 +0100842void
Radek Krejci8df109d2021-04-23 12:19:08 +0200843ly_free_prefix_data(LY_VALUE_FORMAT format, void *prefix_data)
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100844{
845 struct ly_set *ns_list;
846 struct lysc_prefix *prefixes;
847 uint32_t i;
848 LY_ARRAY_COUNT_TYPE u;
849
850 if (!prefix_data) {
851 return;
852 }
853
854 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200855 case LY_VALUE_XML:
Michal Vaskoddd76592022-01-17 13:34:48 +0100856 case LY_VALUE_STR_NS:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100857 ns_list = prefix_data;
858 for (i = 0; i < ns_list->count; ++i) {
859 free(((struct lyxml_ns *)ns_list->objs[i])->prefix);
860 free(((struct lyxml_ns *)ns_list->objs[i])->uri);
861 }
862 ly_set_free(ns_list, free);
863 break;
Radek Krejci8df109d2021-04-23 12:19:08 +0200864 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100865 prefixes = prefix_data;
866 LY_ARRAY_FOR(prefixes, u) {
867 free(prefixes[u].prefix);
868 }
869 LY_ARRAY_FREE(prefixes);
870 break;
Radek Krejci224d4b42021-04-23 13:54:59 +0200871 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +0200872 case LY_VALUE_SCHEMA:
873 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +0200874 case LY_VALUE_LYB:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100875 break;
876 }
877}
878
879LY_ERR
Radek Krejci8df109d2021-04-23 12:19:08 +0200880ly_dup_prefix_data(const struct ly_ctx *ctx, LY_VALUE_FORMAT format, const void *prefix_data,
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100881 void **prefix_data_p)
882{
883 LY_ERR ret = LY_SUCCESS;
884 struct lyxml_ns *ns;
885 struct lysc_prefix *prefixes = NULL, *orig_pref;
886 struct ly_set *ns_list, *orig_ns;
887 uint32_t i;
888 LY_ARRAY_COUNT_TYPE u;
889
890 assert(!*prefix_data_p);
891
892 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200893 case LY_VALUE_SCHEMA:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100894 *prefix_data_p = (void *)prefix_data;
895 break;
Radek Krejci8df109d2021-04-23 12:19:08 +0200896 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100897 /* copy all the value prefixes */
898 orig_pref = (struct lysc_prefix *)prefix_data;
899 LY_ARRAY_CREATE_GOTO(ctx, prefixes, LY_ARRAY_COUNT(orig_pref), ret, cleanup);
900 *prefix_data_p = prefixes;
901
902 LY_ARRAY_FOR(orig_pref, u) {
903 if (orig_pref[u].prefix) {
904 prefixes[u].prefix = strdup(orig_pref[u].prefix);
905 LY_CHECK_ERR_GOTO(!prefixes[u].prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup);
906 }
907 prefixes[u].mod = orig_pref[u].mod;
908 LY_ARRAY_INCREMENT(prefixes);
909 }
910 break;
Radek Krejci8df109d2021-04-23 12:19:08 +0200911 case LY_VALUE_XML:
Michal Vaskoddd76592022-01-17 13:34:48 +0100912 case LY_VALUE_STR_NS:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100913 /* copy all the namespaces */
914 LY_CHECK_GOTO(ret = ly_set_new(&ns_list), cleanup);
915 *prefix_data_p = ns_list;
916
917 orig_ns = (struct ly_set *)prefix_data;
918 for (i = 0; i < orig_ns->count; ++i) {
919 ns = calloc(1, sizeof *ns);
920 LY_CHECK_ERR_GOTO(!ns, LOGMEM(ctx); ret = LY_EMEM, cleanup);
921 LY_CHECK_GOTO(ret = ly_set_add(ns_list, ns, 1, NULL), cleanup);
922
923 if (((struct lyxml_ns *)orig_ns->objs[i])->prefix) {
924 ns->prefix = strdup(((struct lyxml_ns *)orig_ns->objs[i])->prefix);
925 LY_CHECK_ERR_GOTO(!ns->prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup);
926 }
927 ns->uri = strdup(((struct lyxml_ns *)orig_ns->objs[i])->uri);
928 LY_CHECK_ERR_GOTO(!ns->uri, LOGMEM(ctx); ret = LY_EMEM, cleanup);
929 }
930 break;
Radek Krejci224d4b42021-04-23 13:54:59 +0200931 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +0200932 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +0200933 case LY_VALUE_LYB:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100934 assert(!prefix_data);
935 *prefix_data_p = NULL;
936 break;
937 }
938
939cleanup:
940 if (ret) {
941 ly_free_prefix_data(format, *prefix_data_p);
942 *prefix_data_p = NULL;
943 }
944 return ret;
945}
946
947LY_ERR
Radek Krejcif9943642021-04-26 10:18:21 +0200948ly_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 +0200949 const void *prefix_data, LY_VALUE_FORMAT *format_p, void **prefix_data_p)
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100950{
951 LY_ERR ret = LY_SUCCESS;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100952 const struct lys_module *mod;
Michal Vaskofc2cd072021-02-24 13:17:17 +0100953 const struct lyxml_ns *ns;
954 struct lyxml_ns *new_ns;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100955 struct ly_set *ns_list;
956 struct lysc_prefix *prefixes = NULL, *val_pref;
aPiecek83436bc2021-03-30 12:20:45 +0200957 const char *value_iter, *value_next, *value_end;
958 uint32_t substr_len;
959 ly_bool is_prefix;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100960
961 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200962 case LY_VALUE_SCHEMA:
Michal Vaskofc2cd072021-02-24 13:17:17 +0100963 /* copy all referenced modules as prefix - module pairs */
964 if (!*prefix_data_p) {
965 /* new prefix data */
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100966 LY_ARRAY_CREATE_GOTO(ctx, prefixes, 0, ret, cleanup);
Radek Krejci8df109d2021-04-23 12:19:08 +0200967 *format_p = LY_VALUE_SCHEMA_RESOLVED;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100968 *prefix_data_p = prefixes;
Michal Vaskofc2cd072021-02-24 13:17:17 +0100969 } else {
970 /* reuse prefix data */
Radek Krejci8df109d2021-04-23 12:19:08 +0200971 assert(*format_p == LY_VALUE_SCHEMA_RESOLVED);
Michal Vaskofc2cd072021-02-24 13:17:17 +0100972 prefixes = *prefix_data_p;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100973 }
974
Michal Vaskoc0f9c4c2022-05-06 12:12:17 +0200975 /* add current module for unprefixed values */
976 LY_ARRAY_NEW_GOTO(ctx, prefixes, val_pref, ret, cleanup);
977 *prefix_data_p = prefixes;
978
979 val_pref->prefix = NULL;
980 val_pref->mod = ((const struct lysp_module *)prefix_data)->mod;
981
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100982 /* add all used prefixes */
Michal Vasko59e90fc2021-09-22 12:17:08 +0200983 value_end = (char *)value + value_len;
aPiecek83436bc2021-03-30 12:20:45 +0200984 for (value_iter = value; value_iter; value_iter = value_next) {
aPieceke3f828d2021-05-10 15:34:41 +0200985 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 +0200986 if (is_prefix) {
987 /* we have a possible prefix. Do we already have the prefix? */
988 mod = ly_resolve_prefix(ctx, value_iter, substr_len, *format_p, *prefix_data_p);
989 if (!mod) {
990 mod = ly_resolve_prefix(ctx, value_iter, substr_len, format, prefix_data);
991 if (mod) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200992 assert(*format_p == LY_VALUE_SCHEMA_RESOLVED);
aPiecek83436bc2021-03-30 12:20:45 +0200993 /* store a new prefix - module pair */
994 LY_ARRAY_NEW_GOTO(ctx, prefixes, val_pref, ret, cleanup);
995 *prefix_data_p = prefixes;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100996
aPiecek83436bc2021-03-30 12:20:45 +0200997 val_pref->prefix = strndup(value_iter, substr_len);
998 LY_CHECK_ERR_GOTO(!val_pref->prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup);
999 val_pref->mod = mod;
1000 } /* else it is not even defined */
1001 } /* else the prefix is already present */
Michal Vaskofc2cd072021-02-24 13:17:17 +01001002 }
1003 }
1004 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02001005 case LY_VALUE_XML:
Michal Vaskoddd76592022-01-17 13:34:48 +01001006 case LY_VALUE_STR_NS:
Michal Vaskofc2cd072021-02-24 13:17:17 +01001007 /* copy all referenced namespaces as prefix - namespace pairs */
1008 if (!*prefix_data_p) {
1009 /* new prefix data */
1010 LY_CHECK_GOTO(ret = ly_set_new(&ns_list), cleanup);
Michal Vaskoddd76592022-01-17 13:34:48 +01001011 *format_p = format;
Michal Vaskofc2cd072021-02-24 13:17:17 +01001012 *prefix_data_p = ns_list;
1013 } else {
1014 /* reuse prefix data */
Michal Vaskoddd76592022-01-17 13:34:48 +01001015 assert(*format_p == format);
Michal Vaskofc2cd072021-02-24 13:17:17 +01001016 ns_list = *prefix_data_p;
1017 }
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001018
Michal Vasko294e7f02022-02-28 13:59:00 +01001019 /* store default namespace */
1020 ns = lyxml_ns_get(prefix_data, NULL, 0);
1021 if (ns) {
1022 new_ns = calloc(1, sizeof *new_ns);
1023 LY_CHECK_ERR_GOTO(!new_ns, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1024 LY_CHECK_GOTO(ret = ly_set_add(ns_list, new_ns, 1, NULL), cleanup);
1025
1026 new_ns->prefix = NULL;
1027 new_ns->uri = strdup(ns->uri);
1028 LY_CHECK_ERR_GOTO(!new_ns->uri, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1029 }
1030
Michal Vaskofc2cd072021-02-24 13:17:17 +01001031 /* add all used prefixes */
Michal Vasko59e90fc2021-09-22 12:17:08 +02001032 value_end = (char *)value + value_len;
aPiecek83436bc2021-03-30 12:20:45 +02001033 for (value_iter = value; value_iter; value_iter = value_next) {
aPieceke3f828d2021-05-10 15:34:41 +02001034 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 +02001035 if (is_prefix) {
1036 /* we have a possible prefix. Do we already have the prefix? */
1037 ns = lyxml_ns_get(ns_list, value_iter, substr_len);
1038 if (!ns) {
1039 ns = lyxml_ns_get(prefix_data, value_iter, substr_len);
1040 if (ns) {
1041 /* store a new prefix - namespace pair */
1042 new_ns = calloc(1, sizeof *new_ns);
1043 LY_CHECK_ERR_GOTO(!new_ns, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1044 LY_CHECK_GOTO(ret = ly_set_add(ns_list, new_ns, 1, NULL), cleanup);
Michal Vaskofc2cd072021-02-24 13:17:17 +01001045
aPiecek83436bc2021-03-30 12:20:45 +02001046 new_ns->prefix = strndup(value_iter, substr_len);
1047 LY_CHECK_ERR_GOTO(!new_ns->prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1048 new_ns->uri = strdup(ns->uri);
1049 LY_CHECK_ERR_GOTO(!new_ns->uri, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1050 } /* else it is not even defined */
1051 } /* else the prefix is already present */
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001052 }
1053 }
1054 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02001055 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02001056 case LY_VALUE_SCHEMA_RESOLVED:
1057 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02001058 case LY_VALUE_LYB:
Michal Vaskofc2cd072021-02-24 13:17:17 +01001059 if (!*prefix_data_p) {
1060 /* new prefix data - simply copy all the prefix data */
1061 *format_p = format;
1062 LY_CHECK_GOTO(ret = ly_dup_prefix_data(ctx, format, prefix_data, prefix_data_p), cleanup);
1063 } /* else reuse prefix data - the prefix data are always the same, nothing to do */
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001064 break;
1065 }
1066
1067cleanup:
1068 if (ret) {
1069 ly_free_prefix_data(*format_p, *prefix_data_p);
1070 *prefix_data_p = NULL;
1071 }
1072 return ret;
1073}
Michal Vasko7ed1fcb2020-12-03 14:15:22 +01001074
1075const char *
Radek Krejci8df109d2021-04-23 12:19:08 +02001076ly_format2str(LY_VALUE_FORMAT format)
Michal Vasko7ed1fcb2020-12-03 14:15:22 +01001077{
1078 switch (format) {
Radek Krejci224d4b42021-04-23 13:54:59 +02001079 case LY_VALUE_CANON:
1080 return "canonical";
Radek Krejci8df109d2021-04-23 12:19:08 +02001081 case LY_VALUE_SCHEMA:
Michal Vasko7ed1fcb2020-12-03 14:15:22 +01001082 return "schema imports";
Radek Krejci8df109d2021-04-23 12:19:08 +02001083 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko7ed1fcb2020-12-03 14:15:22 +01001084 return "schema stored mapping";
Radek Krejci8df109d2021-04-23 12:19:08 +02001085 case LY_VALUE_XML:
Michal Vasko7ed1fcb2020-12-03 14:15:22 +01001086 return "XML prefixes";
Radek Krejci8df109d2021-04-23 12:19:08 +02001087 case LY_VALUE_JSON:
Michal Vasko7ed1fcb2020-12-03 14:15:22 +01001088 return "JSON module names";
Radek Krejcif9943642021-04-26 10:18:21 +02001089 case LY_VALUE_LYB:
1090 return "LYB prefixes";
Michal Vasko7ed1fcb2020-12-03 14:15:22 +01001091 default:
1092 break;
1093 }
1094
1095 return NULL;
1096}
Michal Vasko43297a02021-05-19 11:12:37 +02001097
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001098LIBYANG_API_DEF LY_ERR
Michal Vasko43297a02021-05-19 11:12:37 +02001099ly_time_str2time(const char *value, time_t *time, char **fractions_s)
1100{
1101 struct tm tm = {0};
1102 uint32_t i, frac_len;
1103 const char *frac;
1104 int64_t shift, shift_m;
1105 time_t t;
1106
1107 LY_CHECK_ARG_RET(NULL, value, time, LY_EINVAL);
1108
1109 tm.tm_year = atoi(&value[0]) - 1900;
1110 tm.tm_mon = atoi(&value[5]) - 1;
1111 tm.tm_mday = atoi(&value[8]);
1112 tm.tm_hour = atoi(&value[11]);
1113 tm.tm_min = atoi(&value[14]);
1114 tm.tm_sec = atoi(&value[17]);
1115
1116 t = timegm(&tm);
1117 i = 19;
1118
1119 /* fractions of a second */
1120 if (value[i] == '.') {
1121 ++i;
1122 frac = &value[i];
1123 for (frac_len = 0; isdigit(frac[frac_len]); ++frac_len) {}
1124
1125 i += frac_len;
Michal Vasko43297a02021-05-19 11:12:37 +02001126 } else {
1127 frac = NULL;
1128 }
1129
1130 /* apply offset */
1131 if ((value[i] == 'Z') || (value[i] == 'z')) {
1132 /* zero shift */
1133 shift = 0;
1134 } else {
1135 shift = strtol(&value[i], NULL, 10);
1136 shift = shift * 60 * 60; /* convert from hours to seconds */
1137 shift_m = strtol(&value[i + 4], NULL, 10) * 60; /* includes conversion from minutes to seconds */
1138 /* correct sign */
1139 if (shift < 0) {
1140 shift_m *= -1;
1141 }
1142 /* connect hours and minutes of the shift */
1143 shift = shift + shift_m;
1144 }
1145
1146 /* we have to shift to the opposite way to correct the time */
1147 t -= shift;
1148
1149 *time = t;
1150 if (fractions_s) {
1151 if (frac) {
1152 *fractions_s = strndup(frac, frac_len);
1153 LY_CHECK_RET(!*fractions_s, LY_EMEM);
1154 } else {
1155 *fractions_s = NULL;
1156 }
1157 }
1158 return LY_SUCCESS;
1159}
1160
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001161LIBYANG_API_DEF LY_ERR
Michal Vasko43297a02021-05-19 11:12:37 +02001162ly_time_time2str(time_t time, const char *fractions_s, char **str)
1163{
1164 struct tm tm;
Michal Vasko143ffa82021-05-20 11:11:39 +02001165 char zoneshift[8];
Michal Vasko43297a02021-05-19 11:12:37 +02001166 int32_t zonediff_h, zonediff_m;
1167
1168 LY_CHECK_ARG_RET(NULL, str, LY_EINVAL);
1169
1170 /* initialize the local timezone */
1171 tzset();
1172
Jan Kundrátb17efe92022-02-14 18:32:18 +01001173#ifdef HAVE_TM_GMTOFF
Michal Vasko43297a02021-05-19 11:12:37 +02001174 /* convert */
1175 if (!localtime_r(&time, &tm)) {
1176 return LY_ESYS;
1177 }
1178
1179 /* get timezone offset */
1180 if (tm.tm_gmtoff == 0) {
1181 /* time is Zulu (UTC) */
1182 zonediff_h = 0;
1183 zonediff_m = 0;
1184 } else {
1185 /* timezone offset */
1186 zonediff_h = tm.tm_gmtoff / 60 / 60;
1187 zonediff_m = tm.tm_gmtoff / 60 % 60;
1188 }
1189 sprintf(zoneshift, "%+03d:%02d", zonediff_h, zonediff_m);
Jan Kundráte182a272021-12-09 23:25:15 +01001190#else
Jan Kundrátb17efe92022-02-14 18:32:18 +01001191 /* convert */
1192 if (!gmtime_r(&time, &tm)) {
1193 return LY_ESYS;
1194 }
1195
Jan Kundráte182a272021-12-09 23:25:15 +01001196 (void)zonediff_h;
1197 (void)zonediff_m;
1198 sprintf(zoneshift, "-00:00");
1199#endif
Michal Vasko43297a02021-05-19 11:12:37 +02001200
1201 /* print */
1202 if (asprintf(str, "%04d-%02d-%02dT%02d:%02d:%02d%s%s%s",
1203 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
1204 fractions_s ? "." : "", fractions_s ? fractions_s : "", zoneshift) == -1) {
1205 return LY_EMEM;
1206 }
1207
1208 return LY_SUCCESS;
1209}
1210
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001211LIBYANG_API_DEF LY_ERR
Michal Vasko43297a02021-05-19 11:12:37 +02001212ly_time_str2ts(const char *value, struct timespec *ts)
1213{
1214 LY_ERR rc;
Michal Vasko72975062021-08-25 08:13:04 +02001215 char *fractions_s, frac_buf[10];
Michal Vasko43297a02021-05-19 11:12:37 +02001216 int frac_len;
1217
1218 LY_CHECK_ARG_RET(NULL, value, ts, LY_EINVAL);
1219
1220 rc = ly_time_str2time(value, &ts->tv_sec, &fractions_s);
1221 LY_CHECK_RET(rc);
1222
1223 /* convert fractions of a second to nanoseconds */
1224 if (fractions_s) {
Michal Vasko72975062021-08-25 08:13:04 +02001225 /* init frac_buf with zeroes */
1226 memset(frac_buf, '0', 9);
1227 frac_buf[9] = '\0';
1228
Michal Vasko43297a02021-05-19 11:12:37 +02001229 frac_len = strlen(fractions_s);
1230 memcpy(frac_buf, fractions_s, frac_len > 9 ? 9 : frac_len);
1231 ts->tv_nsec = atol(frac_buf);
1232 free(fractions_s);
1233 } else {
1234 ts->tv_nsec = 0;
1235 }
1236
1237 return LY_SUCCESS;
1238}
1239
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001240LIBYANG_API_DEF LY_ERR
Michal Vasko43297a02021-05-19 11:12:37 +02001241ly_time_ts2str(const struct timespec *ts, char **str)
1242{
1243 char frac_buf[10];
1244
Jan Kundrátbd157002021-08-30 14:02:22 +02001245 LY_CHECK_ARG_RET(NULL, ts, str, ((ts->tv_nsec <= 999999999) && (ts->tv_nsec >= 0)), LY_EINVAL);
Michal Vasko43297a02021-05-19 11:12:37 +02001246
1247 /* convert nanoseconds to fractions of a second */
1248 if (ts->tv_nsec) {
1249 sprintf(frac_buf, "%09ld", ts->tv_nsec);
1250 }
1251
1252 return ly_time_time2str(ts->tv_sec, ts->tv_nsec ? frac_buf : NULL, str);
1253}