blob: 105c5862ad9aac9232b66abd36bca899cade1d5f [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/**
47 * @brief Find an entry in duplicate instance cache for an instance. Create it if it does not exist.
48 *
49 * @param[in] first_inst Instance of the cache entry.
50 * @param[in,out] dup_inst_cache Duplicate instance cache.
51 * @return Instance cache entry.
52 */
53static struct lyd_dup_inst *
54lyd_dup_inst_get(const struct lyd_node *first_inst, struct lyd_dup_inst **dup_inst_cache)
55{
56 struct lyd_dup_inst *item;
57 LY_ARRAY_COUNT_TYPE u;
58
59 LY_ARRAY_FOR(*dup_inst_cache, u) {
60 if ((*dup_inst_cache)[u].inst_set->dnodes[0] == first_inst) {
61 return &(*dup_inst_cache)[u];
62 }
63 }
64
65 /* it was not added yet, add it now */
66 LY_ARRAY_NEW_RET(LYD_CTX(first_inst), *dup_inst_cache, item, NULL);
67
68 return item;
69}
70
71LY_ERR
72lyd_dup_inst_next(struct lyd_node **inst, const struct lyd_node *siblings, struct lyd_dup_inst **dup_inst_cache)
73{
74 struct lyd_dup_inst *dup_inst;
75
Michal Vasko83ae7772022-06-08 10:01:55 +020076 if (!*inst) {
77 /* no match, inst is unchanged */
Michal Vaskod7c048c2021-05-18 16:12:55 +020078 return LY_SUCCESS;
79 }
80
Michal Vasko83ae7772022-06-08 10:01:55 +020081 /* there can be more exact same instances (even if not allowed in invalid data) and we must make sure we do not
82 * match a single node more times */
Michal Vaskod7c048c2021-05-18 16:12:55 +020083 dup_inst = lyd_dup_inst_get(*inst, dup_inst_cache);
84 LY_CHECK_ERR_RET(!dup_inst, LOGMEM(LYD_CTX(siblings)), LY_EMEM);
85
86 if (!dup_inst->used) {
87 /* we did not cache these instances yet, do so */
88 lyd_find_sibling_dup_inst_set(siblings, *inst, &dup_inst->inst_set);
89 assert(dup_inst->inst_set->count && (dup_inst->inst_set->dnodes[0] == *inst));
90 }
91
92 if (dup_inst->used == dup_inst->inst_set->count) {
Michal Vasko4525e1f2022-07-13 16:20:59 +020093 if (lysc_is_dup_inst_list((*inst)->schema)) {
94 /* we have used all the instances */
95 *inst = NULL;
96 } /* else just keep using the last (ideally only) instance */
Michal Vaskod7c048c2021-05-18 16:12:55 +020097 } else {
98 assert(dup_inst->used < dup_inst->inst_set->count);
99
100 /* use another instance */
101 *inst = dup_inst->inst_set->dnodes[dup_inst->used];
102 ++dup_inst->used;
103 }
104
105 return LY_SUCCESS;
106}
107
108void
109lyd_dup_inst_free(struct lyd_dup_inst *dup_inst)
110{
111 LY_ARRAY_COUNT_TYPE u;
112
113 LY_ARRAY_FOR(dup_inst, u) {
114 ly_set_free(dup_inst[u].inst_set, NULL);
115 }
116 LY_ARRAY_FREE(dup_inst);
117}
118
Michal Vaskoa6669ba2020-08-06 16:14:26 +0200119struct lyd_node *
120lys_getnext_data(const struct lyd_node *last, const struct lyd_node *sibling, const struct lysc_node **slast,
Radek Krejci0f969882020-08-21 16:56:47 +0200121 const struct lysc_node *parent, const struct lysc_module *module)
Michal Vaskoa6669ba2020-08-06 16:14:26 +0200122{
123 const struct lysc_node *siter = NULL;
124 struct lyd_node *match = NULL;
125
126 assert(parent || module);
127 assert(!last || (slast && *slast));
128
129 if (slast) {
130 siter = *slast;
131 }
132
133 if (last && last->next && (last->next->schema == siter)) {
134 /* return next data instance */
135 return last->next;
136 }
137
138 /* find next schema node data instance */
139 while ((siter = lys_getnext(siter, parent, module, 0))) {
140 if (!lyd_find_sibling_val(sibling, siter, NULL, 0, &match)) {
141 break;
142 }
143 }
144
145 if (slast) {
146 *slast = siter;
147 }
148 return match;
149}
150
Radek Krejcie7b95092019-05-15 11:03:07 +0200151struct lyd_node **
Michal Vaskoe0665742021-02-11 11:08:44 +0100152lyd_node_child_p(struct lyd_node *node)
Radek Krejcie7b95092019-05-15 11:03:07 +0200153{
154 assert(node);
Michal Vasko52927e22020-03-16 17:26:14 +0100155
156 if (!node->schema) {
157 return &((struct lyd_node_opaq *)node)->child;
158 } else {
159 switch (node->schema->nodetype) {
160 case LYS_CONTAINER:
161 case LYS_LIST:
Michal Vasko1bf09392020-03-27 12:38:10 +0100162 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +0100163 case LYS_ACTION:
164 case LYS_NOTIF:
165 return &((struct lyd_node_inner *)node)->child;
166 default:
167 return NULL;
168 }
Radek Krejcie7b95092019-05-15 11:03:07 +0200169 }
170}
171
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100172LIBYANG_API_DEF LY_ERR
aPiecekdf23eee2021-10-07 12:21:50 +0200173lyxp_vars_set(struct lyxp_var **vars, const char *name, const char *value)
174{
175 LY_ERR ret = LY_SUCCESS;
176 char *var_name = NULL, *var_value = NULL;
177 struct lyxp_var *item;
178
179 if (!vars || !name || !value) {
180 return LY_EINVAL;
181 }
182
183 /* If variable is already defined then change its value. */
184 if (*vars && !lyxp_vars_find(*vars, name, 0, &item)) {
185 var_value = strdup(value);
186 LY_CHECK_RET(!var_value, LY_EMEM);
187
188 /* Set new value. */
189 free(item->value);
190 item->value = var_value;
191 } else {
192 var_name = strdup(name);
193 var_value = strdup(value);
194 LY_CHECK_ERR_GOTO(!var_name || !var_value, ret = LY_EMEM, error);
195
196 /* Add new variable. */
197 LY_ARRAY_NEW_GOTO(NULL, *vars, item, ret, error);
198 item->name = var_name;
199 item->value = var_value;
200 }
201
202 return LY_SUCCESS;
203
204error:
205 free(var_name);
206 free(var_value);
207 return ret;
208}
209
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100210LIBYANG_API_DEF void
aPiecekdf23eee2021-10-07 12:21:50 +0200211lyxp_vars_free(struct lyxp_var *vars)
212{
213 LY_ARRAY_COUNT_TYPE u;
214
215 if (!vars) {
216 return;
217 }
218
219 LY_ARRAY_FOR(vars, u) {
220 free(vars[u].name);
221 free(vars[u].value);
222 }
223
224 LY_ARRAY_FREE(vars);
225}
226
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100227LIBYANG_API_DEF struct lyd_node *
Radek Krejcia1c1e542020-09-29 16:06:52 +0200228lyd_child_no_keys(const struct lyd_node *node)
229{
230 struct lyd_node **children;
231
232 if (!node) {
233 return NULL;
234 }
235
236 if (!node->schema) {
237 /* opaq node */
Michal Vasko9e685082021-01-29 14:49:09 +0100238 return ((struct lyd_node_opaq *)node)->child;
Radek Krejcia1c1e542020-09-29 16:06:52 +0200239 }
240
Michal Vaskoe0665742021-02-11 11:08:44 +0100241 children = lyd_node_child_p((struct lyd_node *)node);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200242 if (children) {
243 struct lyd_node *child = *children;
244 while (child && child->schema && (child->schema->flags & LYS_KEY)) {
245 child = child->next;
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200246 }
247 return child;
Radek Krejcie7b95092019-05-15 11:03:07 +0200248 } else {
249 return NULL;
250 }
251}
Michal Vasko9b368d32020-02-14 13:53:31 +0100252
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100253LIBYANG_API_DEF const struct lys_module *
Michal Vaskoc193ce92020-03-06 11:04:48 +0100254lyd_owner_module(const struct lyd_node *node)
Michal Vasko9b368d32020-02-14 13:53:31 +0100255{
Michal Vaskod5cfa6e2020-11-23 16:56:08 +0100256 const struct lyd_node_opaq *opaq;
Michal Vasko9b368d32020-02-14 13:53:31 +0100257
Michal Vaskod5cfa6e2020-11-23 16:56:08 +0100258 if (!node) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100259 return NULL;
260 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100261
Michal Vaskod5cfa6e2020-11-23 16:56:08 +0100262 if (!node->schema) {
263 opaq = (struct lyd_node_opaq *)node;
264 switch (opaq->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200265 case LY_VALUE_XML:
Michal Vaskod5cfa6e2020-11-23 16:56:08 +0100266 return ly_ctx_get_module_implemented_ns(LYD_CTX(node), opaq->name.module_ns);
Radek Krejci8df109d2021-04-23 12:19:08 +0200267 case LY_VALUE_JSON:
Michal Vaskod5cfa6e2020-11-23 16:56:08 +0100268 return ly_ctx_get_module_implemented(LYD_CTX(node), opaq->name.module_name);
269 default:
270 return NULL;
271 }
272 }
273
Michal Vaskoef53c812021-10-13 10:21:03 +0200274 return lysc_owner_module(node->schema);
Michal Vasko9b368d32020-02-14 13:53:31 +0100275}
Michal Vaskob1b5c262020-03-05 14:29:47 +0100276
Michal Vasko598063b2021-07-19 11:39:05 +0200277void
278lyd_first_module_sibling(struct lyd_node **node, const struct lys_module *mod)
279{
280 int cmp;
281 struct lyd_node *first;
Michal Vaskoec139eb2022-05-10 10:08:40 +0200282 const struct lys_module *own_mod;
Michal Vasko598063b2021-07-19 11:39:05 +0200283
284 assert(node && mod);
285
286 if (!*node) {
287 return;
288 }
289
290 first = *node;
Michal Vaskoec139eb2022-05-10 10:08:40 +0200291 own_mod = lyd_owner_module(first);
292 cmp = own_mod ? strcmp(own_mod->name, mod->name) : 1;
Michal Vasko598063b2021-07-19 11:39:05 +0200293 if (cmp > 0) {
294 /* there may be some preceding data */
295 while (first->prev->next) {
296 first = first->prev;
297 if (lyd_owner_module(first) == mod) {
298 cmp = 0;
299 break;
300 }
301 }
302 }
303
304 if (cmp == 0) {
305 /* there may be some preceding data belonging to this module */
306 while (first->prev->next) {
307 if (lyd_owner_module(first->prev) != mod) {
308 break;
309 }
310 first = first->prev;
311 }
312 }
313
314 if (cmp < 0) {
315 /* there may be some following data */
316 LY_LIST_FOR(first, first) {
317 if (lyd_owner_module(first) == mod) {
318 cmp = 0;
319 break;
320 }
321 }
322 }
323
324 if (cmp == 0) {
325 /* we have found the first module data node */
326 *node = first;
327 }
328}
329
Michal Vaskob1b5c262020-03-05 14:29:47 +0100330const struct lys_module *
Michal Vasko26e80012020-07-08 10:55:46 +0200331lyd_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 +0200332 struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +0100333{
334 struct lyd_node *iter;
335 const struct lys_module *mod;
336
337 /* get the next module */
Michal Vasko26e80012020-07-08 10:55:46 +0200338 if (module) {
339 if (*i) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100340 mod = NULL;
Michal Vasko26e80012020-07-08 10:55:46 +0200341 } else {
342 mod = module;
343 ++(*i);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100344 }
345 } else {
346 do {
347 mod = ly_ctx_get_module_iter(ctx, i);
348 } while (mod && !mod->implemented);
349 }
350
351 /* find its data */
352 *first = NULL;
353 if (mod) {
354 LY_LIST_FOR(tree, iter) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100355 if (lyd_owner_module(iter) == mod) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100356 *first = iter;
357 break;
358 }
359 }
360 }
361
362 return mod;
363}
364
365const struct lys_module *
366lyd_data_next_module(struct lyd_node **next, struct lyd_node **first)
367{
368 const struct lys_module *mod;
369
370 if (!*next) {
371 /* all data traversed */
372 *first = NULL;
373 return NULL;
374 }
375
376 *first = *next;
377
378 /* prepare next */
Michal Vaskoc193ce92020-03-06 11:04:48 +0100379 mod = lyd_owner_module(*next);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100380 LY_LIST_FOR(*next, *next) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100381 if (lyd_owner_module(*next) != mod) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100382 break;
383 }
384 }
385
386 return mod;
387}
Michal Vasko9f96a052020-03-10 09:41:45 +0100388
389LY_ERR
Michal Vasko59892dd2022-05-13 11:02:30 +0200390lyd_value_store(const struct ly_ctx *ctx, struct lyd_value *val, const struct lysc_type *type, const void *value,
391 size_t value_len, ly_bool *dynamic, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints,
392 const struct lysc_node *ctx_node, ly_bool *incomplete)
393{
394 LY_ERR ret;
395 struct ly_err_item *err = NULL;
396 uint32_t options = (dynamic && *dynamic ? LYPLG_TYPE_STORE_DYNAMIC : 0);
397
398 if (!value) {
399 value = "";
400 }
401 if (incomplete) {
402 *incomplete = 0;
403 }
404
405 ret = type->plugin->store(ctx, type, value, value_len, options, format, prefix_data, hints, ctx_node, val, NULL, &err);
406 if (dynamic) {
407 *dynamic = 0;
408 }
409
410 if (ret == LY_EINCOMPLETE) {
411 if (incomplete) {
412 *incomplete = 1;
413 }
414 } else if (ret) {
415 if (err) {
416 LOGVAL_ERRITEM(ctx, err);
417 ly_err_free(err);
418 } else {
419 LOGVAL(ctx, LYVE_OTHER, "Storing value failed.");
420 }
421 return ret;
422 }
423
424 return LY_SUCCESS;
425}
426
427LY_ERR
428lyd_value_validate_incomplete(const struct ly_ctx *ctx, const struct lysc_type *type, struct lyd_value *val,
429 const struct lyd_node *ctx_node, const struct lyd_node *tree)
430{
431 LY_ERR ret;
432 struct ly_err_item *err = NULL;
433
434 assert(type->plugin->validate);
435
436 ret = type->plugin->validate(ctx, type, ctx_node, tree, val, &err);
437 if (ret) {
438 if (err) {
439 LOGVAL_ERRITEM(ctx, err);
440 ly_err_free(err);
441 } else {
442 LOGVAL(ctx, LYVE_OTHER, "Resolving value \"%s\" failed.", type->plugin->print(ctx, val, LY_VALUE_CANON,
443 NULL, NULL, NULL));
444 }
445 return ret;
446 }
447
448 return LY_SUCCESS;
449}
450
451LY_ERR
452lys_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len,
453 LY_VALUE_FORMAT format, void *prefix_data)
454{
455 LY_ERR rc = LY_SUCCESS;
456 struct ly_err_item *err = NULL;
457 struct lyd_value storage;
458 struct lysc_type *type;
459
460 LY_CHECK_ARG_RET(ctx, node, value, LY_EINVAL);
461
462 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
463 LOGARG(ctx, node);
464 return LY_EINVAL;
465 }
466
467 type = ((struct lysc_node_leaf *)node)->type;
468 rc = type->plugin->store(ctx ? ctx : node->module->ctx, type, value, value_len, 0, format, prefix_data,
469 LYD_HINT_SCHEMA, node, &storage, NULL, &err);
470 if (rc == LY_EINCOMPLETE) {
471 /* actually success since we do not provide the context tree and call validation with
472 * LY_TYPE_OPTS_INCOMPLETE_DATA */
473 rc = LY_SUCCESS;
474 } else if (rc && err) {
475 if (ctx) {
476 /* log only in case the ctx was provided as input parameter */
Michal Vaskof8da2682022-06-16 07:52:37 +0200477 if (err->path) {
478 LOG_LOCSET(NULL, NULL, err->path, NULL);
479 } else {
480 /* use at least the schema path */
481 LOG_LOCSET(node, NULL, NULL, NULL);
482 }
Michal Vasko59892dd2022-05-13 11:02:30 +0200483 LOGVAL_ERRITEM(ctx, err);
Michal Vaskof8da2682022-06-16 07:52:37 +0200484 if (err->path) {
485 LOG_LOCBACK(0, 0, 1, 0);
486 } else {
487 LOG_LOCBACK(1, 0, 0, 0);
488 }
Michal Vasko59892dd2022-05-13 11:02:30 +0200489 }
490 ly_err_free(err);
491 }
492
493 if (!rc) {
494 type->plugin->free(ctx ? ctx : node->module->ctx, &storage);
495 }
496 return rc;
497}
498
499LIBYANG_API_DEF LY_ERR
500lyd_value_validate(const struct ly_ctx *ctx, const struct lysc_node *schema, const char *value, size_t value_len,
501 const struct lyd_node *ctx_node, const struct lysc_type **realtype, const char **canonical)
502{
503 LY_ERR rc;
504 struct ly_err_item *err = NULL;
505 struct lysc_type *type;
506 struct lyd_value val = {0};
507 ly_bool stored = 0, log = 1;
508
Michal Vasko3dd16da2022-06-15 07:58:41 +0200509 LY_CHECK_ARG_RET(ctx, schema, !value_len || value, LY_EINVAL);
Michal Vasko59892dd2022-05-13 11:02:30 +0200510
511 if (!ctx) {
512 ctx = schema->module->ctx;
513 log = 0;
514 }
Michal Vasko3dd16da2022-06-15 07:58:41 +0200515 if (!value_len) {
516 value = "";
517 }
Michal Vasko59892dd2022-05-13 11:02:30 +0200518 type = ((struct lysc_node_leaf *)schema)->type;
519
520 /* store */
521 rc = type->plugin->store(ctx, type, value, value_len, 0, LY_VALUE_JSON, NULL,
522 LYD_HINT_DATA, schema, &val, NULL, &err);
523 if (!rc || (rc == LY_EINCOMPLETE)) {
524 stored = 1;
525 }
526
527 if (ctx_node && (rc == LY_EINCOMPLETE)) {
528 /* resolve */
529 rc = type->plugin->validate(ctx, type, ctx_node, ctx_node, &val, &err);
530 }
531
532 if (rc && (rc != LY_EINCOMPLETE) && err) {
533 if (log) {
534 /* log error */
535 if (err->path) {
536 LOG_LOCSET(NULL, NULL, err->path, NULL);
537 } else if (ctx_node) {
538 LOG_LOCSET(NULL, ctx_node, NULL, NULL);
539 } else {
540 LOG_LOCSET(schema, NULL, NULL, NULL);
541 }
542 LOGVAL_ERRITEM(ctx, err);
543 if (err->path) {
544 LOG_LOCBACK(0, 0, 1, 0);
545 } else if (ctx_node) {
546 LOG_LOCBACK(0, 1, 0, 0);
547 } else {
548 LOG_LOCBACK(1, 0, 0, 0);
549 }
550 }
551 ly_err_free(err);
552 }
553
554 if (!rc || (rc == LY_EINCOMPLETE)) {
555 if (realtype) {
556 /* return realtype */
557 if (val.realtype->basetype == LY_TYPE_UNION) {
558 *realtype = val.subvalue->value.realtype;
559 } else {
560 *realtype = val.realtype;
561 }
562 }
563
564 if (canonical) {
565 /* return canonical value */
566 lydict_insert(ctx, val.realtype->plugin->print(ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL), 0, canonical);
567 }
568 }
569
570 if (stored) {
571 /* free value */
572 type->plugin->free(ctx ? ctx : schema->module->ctx, &val);
573 }
574 return rc;
575}
576
577LIBYANG_API_DEF LY_ERR
578lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len)
579{
580 LY_ERR ret = LY_SUCCESS;
581 struct ly_ctx *ctx;
582 struct lysc_type *type;
583 struct lyd_value val = {0};
584
585 LY_CHECK_ARG_RET(node ? node->schema->module->ctx : NULL, node, value, LY_EINVAL);
586
587 ctx = node->schema->module->ctx;
588 type = ((struct lysc_node_leaf *)node->schema)->type;
589
590 /* store the value */
591 LOG_LOCSET(node->schema, &node->node, NULL, NULL);
592 ret = lyd_value_store(ctx, &val, type, value, value_len, NULL, LY_VALUE_JSON, NULL, LYD_HINT_DATA, node->schema, NULL);
593 LOG_LOCBACK(1, 1, 0, 0);
594 LY_CHECK_RET(ret);
595
596 /* compare values */
597 ret = type->plugin->compare(&node->value, &val);
598
599 type->plugin->free(ctx, &val);
600 return ret;
601}
602
603LIBYANG_API_DEF ly_bool
604lyd_is_default(const struct lyd_node *node)
605{
606 const struct lysc_node_leaf *leaf;
607 const struct lysc_node_leaflist *llist;
608 const struct lyd_node_term *term;
609 LY_ARRAY_COUNT_TYPE u;
610
611 if (!(node->schema->nodetype & LYD_NODE_TERM)) {
612 return 0;
613 }
614
615 term = (const struct lyd_node_term *)node;
616
617 if (node->schema->nodetype == LYS_LEAF) {
618 leaf = (const struct lysc_node_leaf *)node->schema;
619 if (!leaf->dflt) {
620 return 0;
621 }
622
623 /* compare with the default value */
624 if (!leaf->type->plugin->compare(&term->value, leaf->dflt)) {
625 return 1;
626 }
627 } else {
628 llist = (const struct lysc_node_leaflist *)node->schema;
629 if (!llist->dflts) {
630 return 0;
631 }
632
633 LY_ARRAY_FOR(llist->dflts, u) {
634 /* compare with each possible default value */
635 if (!llist->type->plugin->compare(&term->value, llist->dflts[u])) {
636 return 1;
637 }
638 }
639 }
640
641 return 0;
642}
643
644LIBYANG_API_DEF uint32_t
645lyd_list_pos(const struct lyd_node *instance)
646{
647 const struct lyd_node *iter = NULL;
648 uint32_t pos = 0;
649
650 if (!instance || !(instance->schema->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
651 return 0;
652 }
653
654 /* data instances are ordered, so we can stop when we found instance of other schema node */
655 for (iter = instance; iter->schema == instance->schema; iter = iter->prev) {
656 if (pos && (iter->next == NULL)) {
657 /* overrun to the end of the siblings list */
658 break;
659 }
660 ++pos;
661 }
662
663 return pos;
664}
665
666LIBYANG_API_DEF struct lyd_node *
667lyd_first_sibling(const struct lyd_node *node)
668{
669 struct lyd_node *start;
670
671 if (!node) {
672 return NULL;
673 }
674
675 /* get the first sibling */
676 if (node->parent) {
677 start = node->parent->child;
678 } else {
679 for (start = (struct lyd_node *)node; start->prev->next; start = start->prev) {}
680 }
681
682 return start;
683}
684
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100685/**
686 * @brief Check list node parsed into an opaque node for the reason.
687 *
688 * @param[in] node Opaque node.
689 * @param[in] snode Schema node of @p opaq.
690 * @return LY_SUCCESS if the node is valid;
691 * @return LY_ERR on error.
692 */
693static LY_ERR
694lyd_parse_opaq_list_error(const struct lyd_node *node, const struct lysc_node *snode)
695{
696 LY_ERR ret = LY_SUCCESS;
697 struct ly_set key_set = {0};
698 const struct lysc_node *key = NULL;
699 const struct lyd_node *child;
700 const struct lyd_node_opaq *opaq_k;
701 uint32_t i;
702
703 assert(!node->schema);
704
705 /* get all keys into a set */
706 while ((key = lys_getnext(key, snode, NULL, 0)) && (snode->flags & LYS_KEY)) {
707 LY_CHECK_GOTO(ret = ly_set_add(&key_set, (void *)snode, 1, NULL), cleanup);
708 }
709
710 LY_LIST_FOR(lyd_child(node), child) {
711 if (child->schema) {
712 LOGERR(LYD_CTX(node), LY_EINVAL, "Unexpected node %s \"%s\".", lys_nodetype2str(child->schema->nodetype),
713 LYD_NAME(child));
714 ret = LY_EINVAL;
715 goto cleanup;
716 }
717
718 opaq_k = (struct lyd_node_opaq *)child;
719
720 /* find the key schema node */
721 for (i = 0; i < key_set.count; ++i) {
722 key = key_set.snodes[i];
723 if (!strcmp(key->name, opaq_k->name.name)) {
724 break;
725 }
726 }
727 if (i == key_set.count) {
728 /* some other node, skip */
729 continue;
730 }
731
732 /* key found */
733 ly_set_rm_index(&key_set, i, NULL);
734
735 /* check value */
736 ret = lys_value_validate(LYD_CTX(node), key, opaq_k->value, strlen(opaq_k->value), opaq_k->format,
737 opaq_k->val_prefix_data);
738 LY_CHECK_GOTO(ret, cleanup);
739 }
740
741 if (key_set.count) {
742 /* missing keys */
743 LOGVAL(LYD_CTX(node), LY_VCODE_NOKEY, key_set.snodes[0]->name);
744 ret = LY_EVALID;
745 goto cleanup;
746 }
747
748cleanup:
749 ly_set_erase(&key_set, NULL);
750 return ret;
751}
752
753LIBYANG_API_DEF LY_ERR
754lyd_parse_opaq_error(const struct lyd_node *node)
755{
756 const struct ly_ctx *ctx;
757 const struct lyd_node_opaq *opaq;
758 const struct lyd_node *parent;
759 const struct lys_module *mod;
760 const struct lysc_node *snode;
761
762 LY_CHECK_ARG_RET(LYD_CTX(node), node, !node->schema, !lyd_parent(node) || lyd_parent(node)->schema, LY_EINVAL);
763
764 ctx = LYD_CTX(node);
765 opaq = (struct lyd_node_opaq *)node;
766 parent = lyd_parent(node);
767
Michal Vaskof4e63922022-05-10 10:32:13 +0200768 if (!opaq->name.module_ns) {
769 LOGVAL(ctx, LYVE_REFERENCE, "Unknown module of node \"%s\".", opaq->name.name);
770 return LY_EVALID;
771 }
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100772
773 /* module */
774 switch (opaq->format) {
775 case LY_VALUE_XML:
776 if (!parent || strcmp(opaq->name.module_ns, parent->schema->module->ns)) {
777 mod = ly_ctx_get_module_implemented_ns(ctx, opaq->name.module_ns);
778 if (!mod) {
Michal Vasko959f8d82022-06-16 07:51:50 +0200779 LOGVAL(ctx, LYVE_REFERENCE, "No (implemented) module with namespace \"%s\" of node \"%s\" in the context.",
780 opaq->name.module_ns, opaq->name.name);
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100781 return LY_EVALID;
782 }
783 } else {
784 /* inherit */
785 mod = parent->schema->module;
786 }
787 break;
788 case LY_VALUE_JSON:
789 case LY_VALUE_LYB:
790 if (!parent || strcmp(opaq->name.module_name, parent->schema->module->name)) {
791 mod = ly_ctx_get_module_implemented(ctx, opaq->name.module_name);
792 if (!mod) {
Michal Vasko959f8d82022-06-16 07:51:50 +0200793 LOGVAL(ctx, LYVE_REFERENCE, "No (implemented) module named \"%s\" of node \"%s\" in the context.",
794 opaq->name.module_name, opaq->name.name);
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100795 return LY_EVALID;
796 }
797 } else {
798 /* inherit */
799 mod = parent->schema->module;
800 }
801 break;
802 default:
803 LOGERR(ctx, LY_EINVAL, "Unsupported value format.");
804 return LY_EINVAL;
805 }
806
807 /* schema */
808 snode = lys_find_child(parent ? parent->schema : NULL, mod, opaq->name.name, 0, 0, 0);
Michal Vaskoac6f4be2022-05-02 10:16:50 +0200809 if (!snode && parent && parent->schema && (parent->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
810 /* maybe output node */
Michal Vasko89afc6e2022-05-02 10:24:26 +0200811 snode = lys_find_child(parent->schema, mod, opaq->name.name, 0, 0, LYS_GETNEXT_OUTPUT);
Michal Vaskoac6f4be2022-05-02 10:16:50 +0200812 }
Michal Vaskobfff6ac2022-02-23 16:22:53 +0100813 if (!snode) {
814 if (parent) {
815 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%s\" not found as a child of \"%s\" node.", opaq->name.name,
816 LYD_NAME(parent));
817 } else {
818 LOGVAL(ctx, LYVE_REFERENCE, "Node \"%s\" not found in the \"%s\" module.", opaq->name.name, mod->name);
819 }
820 return LY_EVALID;
821 }
822
823 if (snode->nodetype & LYD_NODE_TERM) {
824 /* leaf / leaf-list */
825 LY_CHECK_RET(lys_value_validate(ctx, snode, opaq->value, strlen(opaq->value), opaq->format, opaq->val_prefix_data));
826 } else if (snode->nodetype == LYS_LIST) {
827 /* list */
828 LY_CHECK_RET(lyd_parse_opaq_list_error(node, snode));
829 } else if (snode->nodetype & LYD_NODE_INNER) {
830 /* inner node */
831 if (opaq->value) {
832 LOGVAL(ctx, LYVE_DATA, "Invalid value \"%s\" for %s \"%s\".", opaq->value,
833 lys_nodetype2str(snode->nodetype), snode->name);
834 return LY_EVALID;
835 }
836 } else {
837 LOGERR(ctx, LY_EINVAL, "Unexpected opaque schema node %s \"%s\".", lys_nodetype2str(snode->nodetype), snode->name);
838 return LY_EINVAL;
839 }
840
841 LOGERR(ctx, LY_EINVAL, "Unexpected valid opaque node %s \"%s\".", lys_nodetype2str(snode->nodetype), snode->name);
842 return LY_EINVAL;
843}
844
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100845LIBYANG_API_DEF const char *
Christian Hopps46bd21b2021-04-27 09:43:58 -0400846lyd_value_get_canonical(const struct ly_ctx *ctx, const struct lyd_value *value)
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200847{
Michal Vaskoab40e7e2021-04-28 17:04:24 +0200848 LY_CHECK_ARG_RET(ctx, ctx, value, NULL);
849
Michal Vasko33876022021-04-27 16:42:24 +0200850 return value->_canonical ? value->_canonical :
851 (const char *)value->realtype->plugin->print(ctx, value, LY_VALUE_CANON, NULL, NULL, NULL);
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200852}
853
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100854LIBYANG_API_DEF LY_ERR
Michal Vaskoa820c312021-02-05 16:33:00 +0100855lyd_any_value_str(const struct lyd_node *any, char **value_str)
856{
857 const struct lyd_node_any *a;
858 struct lyd_node *tree = NULL;
859 const char *str = NULL;
860 ly_bool dynamic = 0;
861 LY_ERR ret = LY_SUCCESS;
862
863 LY_CHECK_ARG_RET(NULL, any, value_str, LY_EINVAL);
Radek Krejci71877df2021-04-06 17:24:06 +0200864 LY_CHECK_ARG_RET(NULL, any->schema, any->schema->nodetype & LYS_ANYDATA, LY_EINVAL);
Michal Vaskoa820c312021-02-05 16:33:00 +0100865
866 a = (struct lyd_node_any *)any;
867 *value_str = NULL;
868
869 if (!a->value.str) {
870 /* there is no value in the union */
871 return LY_SUCCESS;
872 }
873
874 switch (a->value_type) {
875 case LYD_ANYDATA_LYB:
876 /* parse into a data tree */
877 ret = lyd_parse_data_mem(LYD_CTX(any), a->value.mem, LYD_LYB, LYD_PARSE_ONLY, 0, &tree);
878 LY_CHECK_GOTO(ret, cleanup);
879 dynamic = 1;
880 break;
881 case LYD_ANYDATA_DATATREE:
882 tree = a->value.tree;
883 break;
884 case LYD_ANYDATA_STRING:
885 case LYD_ANYDATA_XML:
886 case LYD_ANYDATA_JSON:
887 /* simply use the string */
888 str = a->value.str;
889 break;
890 }
891
892 if (tree) {
893 /* print into a string */
894 ret = lyd_print_mem(value_str, tree, LYD_XML, LYD_PRINT_WITHSIBLINGS);
895 LY_CHECK_GOTO(ret, cleanup);
896 } else {
897 assert(str);
898 *value_str = strdup(str);
899 LY_CHECK_ERR_GOTO(!*value_str, LOGMEM(LYD_CTX(any)), cleanup);
900 }
901
902 /* success */
903
904cleanup:
905 if (dynamic) {
906 lyd_free_all(tree);
907 }
908 return ret;
909}
910
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100911LIBYANG_API_DEF LY_ERR
Michal Vasko61551fa2020-07-09 15:45:45 +0200912lyd_any_copy_value(struct lyd_node *trg, const union lyd_any_value *value, LYD_ANYDATA_VALUETYPE value_type)
913{
914 struct lyd_node_any *t;
Michal Vasko61551fa2020-07-09 15:45:45 +0200915
Michal Vaskoa820c312021-02-05 16:33:00 +0100916 LY_CHECK_ARG_RET(NULL, trg, LY_EINVAL);
Radek Krejci71877df2021-04-06 17:24:06 +0200917 LY_CHECK_ARG_RET(NULL, trg->schema, trg->schema->nodetype & LYS_ANYDATA, LY_EINVAL);
Michal Vasko61551fa2020-07-09 15:45:45 +0200918
919 t = (struct lyd_node_any *)trg;
920
921 /* free trg */
922 switch (t->value_type) {
923 case LYD_ANYDATA_DATATREE:
924 lyd_free_all(t->value.tree);
925 break;
926 case LYD_ANYDATA_STRING:
927 case LYD_ANYDATA_XML:
928 case LYD_ANYDATA_JSON:
Michal Vaskoe180ed02021-02-05 16:31:20 +0100929 lydict_remove(LYD_CTX(trg), t->value.str);
Michal Vasko61551fa2020-07-09 15:45:45 +0200930 break;
931 case LYD_ANYDATA_LYB:
932 free(t->value.mem);
933 break;
934 }
935 t->value.str = NULL;
936
937 if (!value) {
938 /* only free value in this case */
939 return LY_SUCCESS;
940 }
941
942 /* copy src */
943 t->value_type = value_type;
944 switch (value_type) {
945 case LYD_ANYDATA_DATATREE:
946 if (value->tree) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200947 LY_CHECK_RET(lyd_dup_siblings(value->tree, NULL, LYD_DUP_RECURSIVE, &t->value.tree));
Michal Vasko61551fa2020-07-09 15:45:45 +0200948 }
949 break;
950 case LYD_ANYDATA_STRING:
951 case LYD_ANYDATA_XML:
952 case LYD_ANYDATA_JSON:
953 if (value->str) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200954 LY_CHECK_RET(lydict_insert(LYD_CTX(trg), value->str, 0, &t->value.str));
Michal Vasko61551fa2020-07-09 15:45:45 +0200955 }
956 break;
957 case LYD_ANYDATA_LYB:
958 if (value->mem) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200959 int len = lyd_lyb_data_length(value->mem);
Radek Krejci82fa8d42020-07-11 22:00:59 +0200960 LY_CHECK_RET(len == -1, LY_EINVAL);
Michal Vasko61551fa2020-07-09 15:45:45 +0200961 t->value.mem = malloc(len);
Michal Vaskob7be7a82020-08-20 09:09:04 +0200962 LY_CHECK_ERR_RET(!t->value.mem, LOGMEM(LYD_CTX(trg)), LY_EMEM);
Michal Vasko61551fa2020-07-09 15:45:45 +0200963 memcpy(t->value.mem, value->mem, len);
964 }
965 break;
966 }
967
968 return LY_SUCCESS;
969}
970
Michal Vasko106f0862021-11-02 11:49:27 +0100971const struct lysc_node *
972lyd_node_schema(const struct lyd_node *node)
973{
974 const struct lysc_node *schema = NULL;
975 const struct lyd_node *prev_iter = NULL, *iter;
976 const struct lys_module *mod;
977
978 if (!node) {
979 return NULL;
980 } else if (node->schema) {
981 return node->schema;
982 }
983
984 /* get schema node of an opaque node */
985 do {
986 /* get next data node */
987 for (iter = node; lyd_parent(iter) != prev_iter; iter = lyd_parent(iter)) {}
988
989 /* get equivalent schema node */
990 if (iter->schema) {
991 schema = iter->schema;
992 } else {
993 /* get module */
994 mod = lyd_owner_module(iter);
Michal Vaskoa41826a2021-11-02 12:13:03 +0100995 if (!mod && !schema) {
996 /* top-level opaque node has unknown module */
997 break;
998 }
Michal Vasko106f0862021-11-02 11:49:27 +0100999
1000 /* get schema node */
1001 schema = lys_find_child(schema, mod ? mod : schema->module, LYD_NAME(iter), 0, 0, 0);
1002 }
Michal Vaskod2f404f2021-11-04 15:37:11 +01001003
1004 /* remember to move to the descendant */
1005 prev_iter = iter;
Michal Vasko106f0862021-11-02 11:49:27 +01001006 } while (schema && (iter != node));
1007
1008 return schema;
1009}
1010
Michal Vasko59892dd2022-05-13 11:02:30 +02001011/**
1012 * @brief Comparison callback to match schema node with a schema of a data node.
1013 *
1014 * @param[in] val1_p Pointer to the schema node
1015 * @param[in] val2_p Pointer to the data node
1016 * Implementation of ::lyht_value_equal_cb.
1017 */
1018static ly_bool
1019lyd_hash_table_schema_val_equal(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
1020{
1021 struct lysc_node *val1;
1022 struct lyd_node *val2;
1023
1024 val1 = *((struct lysc_node **)val1_p);
1025 val2 = *((struct lyd_node **)val2_p);
1026
1027 if (val1 == val2->schema) {
1028 /* schema match is enough */
1029 return 1;
1030 } else {
1031 return 0;
1032 }
1033}
1034
1035LY_ERR
1036lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match)
1037{
1038 struct lyd_node **match_p;
1039 struct lyd_node_inner *parent;
1040 uint32_t hash;
1041 lyht_value_equal_cb ht_cb;
1042
1043 assert(siblings && schema);
1044
1045 parent = siblings->parent;
1046 if (parent && parent->schema && parent->children_ht) {
1047 /* calculate our hash */
1048 hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name));
1049 hash = dict_hash_multi(hash, schema->name, strlen(schema->name));
1050 hash = dict_hash_multi(hash, NULL, 0);
1051
1052 /* use special hash table function */
1053 ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal);
1054
1055 /* find by hash */
1056 if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) {
1057 siblings = *match_p;
1058 } else {
1059 /* not found */
1060 siblings = NULL;
1061 }
1062
1063 /* set the original hash table compare function back */
1064 lyht_set_cb(parent->children_ht, ht_cb);
1065 } else {
1066 /* find first sibling */
1067 if (siblings->parent) {
1068 siblings = siblings->parent->child;
1069 } else {
1070 while (siblings->prev->next) {
1071 siblings = siblings->prev;
1072 }
1073 }
1074
1075 /* search manually without hashes */
1076 for ( ; siblings; siblings = siblings->next) {
1077 if (siblings->schema == schema) {
1078 /* schema match is enough */
1079 break;
1080 }
1081 }
1082 }
1083
1084 if (!siblings) {
1085 if (match) {
1086 *match = NULL;
1087 }
1088 return LY_ENOTFOUND;
1089 }
1090
1091 if (match) {
1092 *match = (struct lyd_node *)siblings;
1093 }
1094 return LY_SUCCESS;
1095}
1096
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001097void
Michal Vaskod3bb12f2020-12-04 14:33:09 +01001098lyd_del_move_root(struct lyd_node **root, const struct lyd_node *to_del, const struct lys_module *mod)
1099{
1100 if (*root && (lyd_owner_module(*root) != mod)) {
1101 /* there are no data of mod so this is simply the first top-level sibling */
1102 mod = NULL;
1103 }
1104
1105 if ((*root != to_del) || (*root)->parent) {
1106 return;
1107 }
1108
Michal Vasko598063b2021-07-19 11:39:05 +02001109 if (mod && (*root)->prev->next && (!(*root)->next || (lyd_owner_module(to_del) != lyd_owner_module((*root)->next)))) {
1110 /* there are no more nodes from mod, simply get the first top-level sibling */
Michal Vaskod3bb12f2020-12-04 14:33:09 +01001111 *root = lyd_first_sibling(*root);
Michal Vasko598063b2021-07-19 11:39:05 +02001112 } else {
1113 *root = (*root)->next;
Michal Vaskod3bb12f2020-12-04 14:33:09 +01001114 }
1115}
1116
Michal Vasko8cc3f662022-03-29 11:25:51 +02001117LY_ERR
1118ly_nested_ext_schema(const struct lyd_node *parent, const struct lysc_node *sparent, const char *prefix,
1119 size_t prefix_len, LY_VALUE_FORMAT format, void *prefix_data, const char *name, size_t name_len,
1120 const struct lysc_node **snode, struct lysc_ext_instance **ext)
1121{
1122 LY_ERR r;
1123 LY_ARRAY_COUNT_TYPE u;
1124 struct lysc_ext_instance *nested_exts = NULL;
1125 lyplg_ext_data_snode_clb ext_snode_cb;
1126
1127 /* check if there are any nested extension instances */
1128 if (parent && parent->schema) {
1129 nested_exts = parent->schema->exts;
1130 } else if (sparent) {
1131 nested_exts = sparent->exts;
1132 }
1133 LY_ARRAY_FOR(nested_exts, u) {
Michal Vasko305c6cb2022-04-27 10:33:04 +02001134 if (!nested_exts[u].def->plugin) {
1135 /* no plugin */
1136 continue;
1137 }
1138
Michal Vasko8cc3f662022-03-29 11:25:51 +02001139 ext_snode_cb = nested_exts[u].def->plugin->snode;
1140 if (!ext_snode_cb) {
1141 /* not an extension with nested data */
1142 continue;
1143 }
1144
1145 /* try to get the schema node */
1146 r = ext_snode_cb(&nested_exts[u], parent, sparent, prefix, prefix_len, format, prefix_data, name, name_len, snode);
1147 if (!r) {
1148 /* data successfully created, remember the ext instance */
1149 *ext = &nested_exts[u];
1150 return LY_SUCCESS;
1151 } else if (r != LY_ENOT) {
1152 /* fatal error */
1153 return r;
1154 }
1155 /* data was not from this module, continue */
1156 }
1157
1158 /* no extensions or none matched */
1159 return LY_ENOT;
1160}
1161
Michal Vaskod3bb12f2020-12-04 14:33:09 +01001162void
Radek Krejci8df109d2021-04-23 12:19:08 +02001163ly_free_prefix_data(LY_VALUE_FORMAT format, void *prefix_data)
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001164{
1165 struct ly_set *ns_list;
1166 struct lysc_prefix *prefixes;
1167 uint32_t i;
1168 LY_ARRAY_COUNT_TYPE u;
1169
1170 if (!prefix_data) {
1171 return;
1172 }
1173
1174 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001175 case LY_VALUE_XML:
Michal Vaskoddd76592022-01-17 13:34:48 +01001176 case LY_VALUE_STR_NS:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001177 ns_list = prefix_data;
1178 for (i = 0; i < ns_list->count; ++i) {
1179 free(((struct lyxml_ns *)ns_list->objs[i])->prefix);
1180 free(((struct lyxml_ns *)ns_list->objs[i])->uri);
1181 }
1182 ly_set_free(ns_list, free);
1183 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02001184 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001185 prefixes = prefix_data;
1186 LY_ARRAY_FOR(prefixes, u) {
1187 free(prefixes[u].prefix);
1188 }
1189 LY_ARRAY_FREE(prefixes);
1190 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02001191 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02001192 case LY_VALUE_SCHEMA:
1193 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02001194 case LY_VALUE_LYB:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001195 break;
1196 }
1197}
1198
1199LY_ERR
Radek Krejci8df109d2021-04-23 12:19:08 +02001200ly_dup_prefix_data(const struct ly_ctx *ctx, LY_VALUE_FORMAT format, const void *prefix_data,
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001201 void **prefix_data_p)
1202{
1203 LY_ERR ret = LY_SUCCESS;
1204 struct lyxml_ns *ns;
1205 struct lysc_prefix *prefixes = NULL, *orig_pref;
1206 struct ly_set *ns_list, *orig_ns;
1207 uint32_t i;
1208 LY_ARRAY_COUNT_TYPE u;
1209
1210 assert(!*prefix_data_p);
1211
1212 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001213 case LY_VALUE_SCHEMA:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001214 *prefix_data_p = (void *)prefix_data;
1215 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02001216 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001217 /* copy all the value prefixes */
1218 orig_pref = (struct lysc_prefix *)prefix_data;
1219 LY_ARRAY_CREATE_GOTO(ctx, prefixes, LY_ARRAY_COUNT(orig_pref), ret, cleanup);
1220 *prefix_data_p = prefixes;
1221
1222 LY_ARRAY_FOR(orig_pref, u) {
1223 if (orig_pref[u].prefix) {
1224 prefixes[u].prefix = strdup(orig_pref[u].prefix);
1225 LY_CHECK_ERR_GOTO(!prefixes[u].prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1226 }
1227 prefixes[u].mod = orig_pref[u].mod;
1228 LY_ARRAY_INCREMENT(prefixes);
1229 }
1230 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02001231 case LY_VALUE_XML:
Michal Vaskoddd76592022-01-17 13:34:48 +01001232 case LY_VALUE_STR_NS:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001233 /* copy all the namespaces */
1234 LY_CHECK_GOTO(ret = ly_set_new(&ns_list), cleanup);
1235 *prefix_data_p = ns_list;
1236
1237 orig_ns = (struct ly_set *)prefix_data;
1238 for (i = 0; i < orig_ns->count; ++i) {
1239 ns = calloc(1, sizeof *ns);
1240 LY_CHECK_ERR_GOTO(!ns, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1241 LY_CHECK_GOTO(ret = ly_set_add(ns_list, ns, 1, NULL), cleanup);
1242
1243 if (((struct lyxml_ns *)orig_ns->objs[i])->prefix) {
1244 ns->prefix = strdup(((struct lyxml_ns *)orig_ns->objs[i])->prefix);
1245 LY_CHECK_ERR_GOTO(!ns->prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1246 }
1247 ns->uri = strdup(((struct lyxml_ns *)orig_ns->objs[i])->uri);
1248 LY_CHECK_ERR_GOTO(!ns->uri, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1249 }
1250 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02001251 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02001252 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02001253 case LY_VALUE_LYB:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001254 assert(!prefix_data);
1255 *prefix_data_p = NULL;
1256 break;
1257 }
1258
1259cleanup:
1260 if (ret) {
1261 ly_free_prefix_data(format, *prefix_data_p);
1262 *prefix_data_p = NULL;
1263 }
1264 return ret;
1265}
1266
1267LY_ERR
Radek Krejcif9943642021-04-26 10:18:21 +02001268ly_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 +02001269 const void *prefix_data, LY_VALUE_FORMAT *format_p, void **prefix_data_p)
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001270{
1271 LY_ERR ret = LY_SUCCESS;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001272 const struct lys_module *mod;
Michal Vaskofc2cd072021-02-24 13:17:17 +01001273 const struct lyxml_ns *ns;
1274 struct lyxml_ns *new_ns;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001275 struct ly_set *ns_list;
1276 struct lysc_prefix *prefixes = NULL, *val_pref;
aPiecek83436bc2021-03-30 12:20:45 +02001277 const char *value_iter, *value_next, *value_end;
1278 uint32_t substr_len;
1279 ly_bool is_prefix;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001280
1281 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001282 case LY_VALUE_SCHEMA:
Michal Vaskofc2cd072021-02-24 13:17:17 +01001283 /* copy all referenced modules as prefix - module pairs */
1284 if (!*prefix_data_p) {
1285 /* new prefix data */
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001286 LY_ARRAY_CREATE_GOTO(ctx, prefixes, 0, ret, cleanup);
Radek Krejci8df109d2021-04-23 12:19:08 +02001287 *format_p = LY_VALUE_SCHEMA_RESOLVED;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001288 *prefix_data_p = prefixes;
Michal Vaskofc2cd072021-02-24 13:17:17 +01001289 } else {
1290 /* reuse prefix data */
Radek Krejci8df109d2021-04-23 12:19:08 +02001291 assert(*format_p == LY_VALUE_SCHEMA_RESOLVED);
Michal Vaskofc2cd072021-02-24 13:17:17 +01001292 prefixes = *prefix_data_p;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001293 }
1294
Michal Vaskoc0f9c4c2022-05-06 12:12:17 +02001295 /* add current module for unprefixed values */
1296 LY_ARRAY_NEW_GOTO(ctx, prefixes, val_pref, ret, cleanup);
1297 *prefix_data_p = prefixes;
1298
1299 val_pref->prefix = NULL;
1300 val_pref->mod = ((const struct lysp_module *)prefix_data)->mod;
1301
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001302 /* add all used prefixes */
Michal Vasko59e90fc2021-09-22 12:17:08 +02001303 value_end = (char *)value + value_len;
aPiecek83436bc2021-03-30 12:20:45 +02001304 for (value_iter = value; value_iter; value_iter = value_next) {
aPieceke3f828d2021-05-10 15:34:41 +02001305 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 +02001306 if (is_prefix) {
1307 /* we have a possible prefix. Do we already have the prefix? */
1308 mod = ly_resolve_prefix(ctx, value_iter, substr_len, *format_p, *prefix_data_p);
1309 if (!mod) {
1310 mod = ly_resolve_prefix(ctx, value_iter, substr_len, format, prefix_data);
1311 if (mod) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001312 assert(*format_p == LY_VALUE_SCHEMA_RESOLVED);
aPiecek83436bc2021-03-30 12:20:45 +02001313 /* store a new prefix - module pair */
1314 LY_ARRAY_NEW_GOTO(ctx, prefixes, val_pref, ret, cleanup);
1315 *prefix_data_p = prefixes;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001316
aPiecek83436bc2021-03-30 12:20:45 +02001317 val_pref->prefix = strndup(value_iter, substr_len);
1318 LY_CHECK_ERR_GOTO(!val_pref->prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1319 val_pref->mod = mod;
1320 } /* else it is not even defined */
1321 } /* else the prefix is already present */
Michal Vaskofc2cd072021-02-24 13:17:17 +01001322 }
1323 }
1324 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02001325 case LY_VALUE_XML:
Michal Vaskoddd76592022-01-17 13:34:48 +01001326 case LY_VALUE_STR_NS:
Michal Vaskofc2cd072021-02-24 13:17:17 +01001327 /* copy all referenced namespaces as prefix - namespace pairs */
1328 if (!*prefix_data_p) {
1329 /* new prefix data */
1330 LY_CHECK_GOTO(ret = ly_set_new(&ns_list), cleanup);
Michal Vaskoddd76592022-01-17 13:34:48 +01001331 *format_p = format;
Michal Vaskofc2cd072021-02-24 13:17:17 +01001332 *prefix_data_p = ns_list;
1333 } else {
1334 /* reuse prefix data */
Michal Vaskoddd76592022-01-17 13:34:48 +01001335 assert(*format_p == format);
Michal Vaskofc2cd072021-02-24 13:17:17 +01001336 ns_list = *prefix_data_p;
1337 }
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001338
Michal Vasko294e7f02022-02-28 13:59:00 +01001339 /* store default namespace */
1340 ns = lyxml_ns_get(prefix_data, NULL, 0);
1341 if (ns) {
1342 new_ns = calloc(1, sizeof *new_ns);
1343 LY_CHECK_ERR_GOTO(!new_ns, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1344 LY_CHECK_GOTO(ret = ly_set_add(ns_list, new_ns, 1, NULL), cleanup);
1345
1346 new_ns->prefix = NULL;
1347 new_ns->uri = strdup(ns->uri);
1348 LY_CHECK_ERR_GOTO(!new_ns->uri, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1349 }
1350
Michal Vaskofc2cd072021-02-24 13:17:17 +01001351 /* add all used prefixes */
Michal Vasko59e90fc2021-09-22 12:17:08 +02001352 value_end = (char *)value + value_len;
aPiecek83436bc2021-03-30 12:20:45 +02001353 for (value_iter = value; value_iter; value_iter = value_next) {
aPieceke3f828d2021-05-10 15:34:41 +02001354 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 +02001355 if (is_prefix) {
1356 /* we have a possible prefix. Do we already have the prefix? */
1357 ns = lyxml_ns_get(ns_list, value_iter, substr_len);
1358 if (!ns) {
1359 ns = lyxml_ns_get(prefix_data, value_iter, substr_len);
1360 if (ns) {
1361 /* store a new prefix - namespace pair */
1362 new_ns = calloc(1, sizeof *new_ns);
1363 LY_CHECK_ERR_GOTO(!new_ns, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1364 LY_CHECK_GOTO(ret = ly_set_add(ns_list, new_ns, 1, NULL), cleanup);
Michal Vaskofc2cd072021-02-24 13:17:17 +01001365
aPiecek83436bc2021-03-30 12:20:45 +02001366 new_ns->prefix = strndup(value_iter, substr_len);
1367 LY_CHECK_ERR_GOTO(!new_ns->prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1368 new_ns->uri = strdup(ns->uri);
1369 LY_CHECK_ERR_GOTO(!new_ns->uri, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1370 } /* else it is not even defined */
1371 } /* else the prefix is already present */
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001372 }
1373 }
1374 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02001375 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02001376 case LY_VALUE_SCHEMA_RESOLVED:
1377 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02001378 case LY_VALUE_LYB:
Michal Vaskofc2cd072021-02-24 13:17:17 +01001379 if (!*prefix_data_p) {
1380 /* new prefix data - simply copy all the prefix data */
1381 *format_p = format;
1382 LY_CHECK_GOTO(ret = ly_dup_prefix_data(ctx, format, prefix_data, prefix_data_p), cleanup);
1383 } /* else reuse prefix data - the prefix data are always the same, nothing to do */
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001384 break;
1385 }
1386
1387cleanup:
1388 if (ret) {
1389 ly_free_prefix_data(*format_p, *prefix_data_p);
1390 *prefix_data_p = NULL;
1391 }
1392 return ret;
1393}
Michal Vasko7ed1fcb2020-12-03 14:15:22 +01001394
1395const char *
Radek Krejci8df109d2021-04-23 12:19:08 +02001396ly_format2str(LY_VALUE_FORMAT format)
Michal Vasko7ed1fcb2020-12-03 14:15:22 +01001397{
1398 switch (format) {
Radek Krejci224d4b42021-04-23 13:54:59 +02001399 case LY_VALUE_CANON:
1400 return "canonical";
Radek Krejci8df109d2021-04-23 12:19:08 +02001401 case LY_VALUE_SCHEMA:
Michal Vasko7ed1fcb2020-12-03 14:15:22 +01001402 return "schema imports";
Radek Krejci8df109d2021-04-23 12:19:08 +02001403 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko7ed1fcb2020-12-03 14:15:22 +01001404 return "schema stored mapping";
Radek Krejci8df109d2021-04-23 12:19:08 +02001405 case LY_VALUE_XML:
Michal Vasko7ed1fcb2020-12-03 14:15:22 +01001406 return "XML prefixes";
Radek Krejci8df109d2021-04-23 12:19:08 +02001407 case LY_VALUE_JSON:
Michal Vasko7ed1fcb2020-12-03 14:15:22 +01001408 return "JSON module names";
Radek Krejcif9943642021-04-26 10:18:21 +02001409 case LY_VALUE_LYB:
1410 return "LYB prefixes";
Michal Vasko7ed1fcb2020-12-03 14:15:22 +01001411 default:
1412 break;
1413 }
1414
1415 return NULL;
1416}
Michal Vasko43297a02021-05-19 11:12:37 +02001417
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001418LIBYANG_API_DEF LY_ERR
Michal Vasko43297a02021-05-19 11:12:37 +02001419ly_time_str2time(const char *value, time_t *time, char **fractions_s)
1420{
1421 struct tm tm = {0};
1422 uint32_t i, frac_len;
1423 const char *frac;
1424 int64_t shift, shift_m;
1425 time_t t;
1426
1427 LY_CHECK_ARG_RET(NULL, value, time, LY_EINVAL);
1428
1429 tm.tm_year = atoi(&value[0]) - 1900;
1430 tm.tm_mon = atoi(&value[5]) - 1;
1431 tm.tm_mday = atoi(&value[8]);
1432 tm.tm_hour = atoi(&value[11]);
1433 tm.tm_min = atoi(&value[14]);
1434 tm.tm_sec = atoi(&value[17]);
1435
1436 t = timegm(&tm);
1437 i = 19;
1438
1439 /* fractions of a second */
1440 if (value[i] == '.') {
1441 ++i;
1442 frac = &value[i];
1443 for (frac_len = 0; isdigit(frac[frac_len]); ++frac_len) {}
1444
1445 i += frac_len;
Michal Vasko43297a02021-05-19 11:12:37 +02001446 } else {
1447 frac = NULL;
1448 }
1449
1450 /* apply offset */
1451 if ((value[i] == 'Z') || (value[i] == 'z')) {
1452 /* zero shift */
1453 shift = 0;
1454 } else {
1455 shift = strtol(&value[i], NULL, 10);
1456 shift = shift * 60 * 60; /* convert from hours to seconds */
1457 shift_m = strtol(&value[i + 4], NULL, 10) * 60; /* includes conversion from minutes to seconds */
1458 /* correct sign */
1459 if (shift < 0) {
1460 shift_m *= -1;
1461 }
1462 /* connect hours and minutes of the shift */
1463 shift = shift + shift_m;
1464 }
1465
1466 /* we have to shift to the opposite way to correct the time */
1467 t -= shift;
1468
1469 *time = t;
1470 if (fractions_s) {
1471 if (frac) {
1472 *fractions_s = strndup(frac, frac_len);
1473 LY_CHECK_RET(!*fractions_s, LY_EMEM);
1474 } else {
1475 *fractions_s = NULL;
1476 }
1477 }
1478 return LY_SUCCESS;
1479}
1480
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001481LIBYANG_API_DEF LY_ERR
Michal Vasko43297a02021-05-19 11:12:37 +02001482ly_time_time2str(time_t time, const char *fractions_s, char **str)
1483{
1484 struct tm tm;
Michal Vasko143ffa82021-05-20 11:11:39 +02001485 char zoneshift[8];
Michal Vasko43297a02021-05-19 11:12:37 +02001486 int32_t zonediff_h, zonediff_m;
1487
1488 LY_CHECK_ARG_RET(NULL, str, LY_EINVAL);
1489
1490 /* initialize the local timezone */
1491 tzset();
1492
Jan Kundrátb17efe92022-02-14 18:32:18 +01001493#ifdef HAVE_TM_GMTOFF
Michal Vasko43297a02021-05-19 11:12:37 +02001494 /* convert */
1495 if (!localtime_r(&time, &tm)) {
1496 return LY_ESYS;
1497 }
1498
1499 /* get timezone offset */
1500 if (tm.tm_gmtoff == 0) {
1501 /* time is Zulu (UTC) */
1502 zonediff_h = 0;
1503 zonediff_m = 0;
1504 } else {
1505 /* timezone offset */
1506 zonediff_h = tm.tm_gmtoff / 60 / 60;
1507 zonediff_m = tm.tm_gmtoff / 60 % 60;
1508 }
1509 sprintf(zoneshift, "%+03d:%02d", zonediff_h, zonediff_m);
Jan Kundráte182a272021-12-09 23:25:15 +01001510#else
Jan Kundrátb17efe92022-02-14 18:32:18 +01001511 /* convert */
1512 if (!gmtime_r(&time, &tm)) {
1513 return LY_ESYS;
1514 }
1515
Jan Kundráte182a272021-12-09 23:25:15 +01001516 (void)zonediff_h;
1517 (void)zonediff_m;
1518 sprintf(zoneshift, "-00:00");
1519#endif
Michal Vasko43297a02021-05-19 11:12:37 +02001520
1521 /* print */
1522 if (asprintf(str, "%04d-%02d-%02dT%02d:%02d:%02d%s%s%s",
1523 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
1524 fractions_s ? "." : "", fractions_s ? fractions_s : "", zoneshift) == -1) {
1525 return LY_EMEM;
1526 }
1527
1528 return LY_SUCCESS;
1529}
1530
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001531LIBYANG_API_DEF LY_ERR
Michal Vasko43297a02021-05-19 11:12:37 +02001532ly_time_str2ts(const char *value, struct timespec *ts)
1533{
1534 LY_ERR rc;
Michal Vasko72975062021-08-25 08:13:04 +02001535 char *fractions_s, frac_buf[10];
Michal Vasko43297a02021-05-19 11:12:37 +02001536 int frac_len;
1537
1538 LY_CHECK_ARG_RET(NULL, value, ts, LY_EINVAL);
1539
1540 rc = ly_time_str2time(value, &ts->tv_sec, &fractions_s);
1541 LY_CHECK_RET(rc);
1542
1543 /* convert fractions of a second to nanoseconds */
1544 if (fractions_s) {
Michal Vasko72975062021-08-25 08:13:04 +02001545 /* init frac_buf with zeroes */
1546 memset(frac_buf, '0', 9);
1547 frac_buf[9] = '\0';
1548
Michal Vasko43297a02021-05-19 11:12:37 +02001549 frac_len = strlen(fractions_s);
1550 memcpy(frac_buf, fractions_s, frac_len > 9 ? 9 : frac_len);
1551 ts->tv_nsec = atol(frac_buf);
1552 free(fractions_s);
1553 } else {
1554 ts->tv_nsec = 0;
1555 }
1556
1557 return LY_SUCCESS;
1558}
1559
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001560LIBYANG_API_DEF LY_ERR
Michal Vasko43297a02021-05-19 11:12:37 +02001561ly_time_ts2str(const struct timespec *ts, char **str)
1562{
1563 char frac_buf[10];
1564
Jan Kundrátbd157002021-08-30 14:02:22 +02001565 LY_CHECK_ARG_RET(NULL, ts, str, ((ts->tv_nsec <= 999999999) && (ts->tv_nsec >= 0)), LY_EINVAL);
Michal Vasko43297a02021-05-19 11:12:37 +02001566
1567 /* convert nanoseconds to fractions of a second */
1568 if (ts->tv_nsec) {
1569 sprintf(frac_buf, "%09ld", ts->tv_nsec);
1570 }
1571
1572 return ly_time_time2str(ts->tv_sec, ts->tv_nsec ? frac_buf : NULL, str);
1573}