blob: e1b39c584d5e12fe672193b2d9048f0dff97841f [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
Michal Vasko21beaeb2022-08-02 10:42:48 +02001043 assert(schema);
1044 if (!siblings) {
1045 /* no data */
1046 if (match) {
1047 *match = NULL;
1048 }
1049 return LY_ENOTFOUND;
1050 }
Michal Vasko59892dd2022-05-13 11:02:30 +02001051
1052 parent = siblings->parent;
1053 if (parent && parent->schema && parent->children_ht) {
1054 /* calculate our hash */
1055 hash = dict_hash_multi(0, schema->module->name, strlen(schema->module->name));
1056 hash = dict_hash_multi(hash, schema->name, strlen(schema->name));
1057 hash = dict_hash_multi(hash, NULL, 0);
1058
1059 /* use special hash table function */
1060 ht_cb = lyht_set_cb(parent->children_ht, lyd_hash_table_schema_val_equal);
1061
1062 /* find by hash */
1063 if (!lyht_find(parent->children_ht, &schema, hash, (void **)&match_p)) {
1064 siblings = *match_p;
1065 } else {
1066 /* not found */
1067 siblings = NULL;
1068 }
1069
1070 /* set the original hash table compare function back */
1071 lyht_set_cb(parent->children_ht, ht_cb);
1072 } else {
1073 /* find first sibling */
1074 if (siblings->parent) {
1075 siblings = siblings->parent->child;
1076 } else {
1077 while (siblings->prev->next) {
1078 siblings = siblings->prev;
1079 }
1080 }
1081
1082 /* search manually without hashes */
1083 for ( ; siblings; siblings = siblings->next) {
Michal Vasko31bac382022-07-20 08:07:59 +02001084 /* schema match is enough */
Michal Vasko59892dd2022-05-13 11:02:30 +02001085 if (siblings->schema == schema) {
Michal Vasko31bac382022-07-20 08:07:59 +02001086 break;
1087 }
1088 if ((LYD_CTX(siblings) != schema->module->ctx) && !strcmp(siblings->schema->name, schema->name) &&
1089 !strcmp(siblings->schema->module->name, schema->module->name)) {
Michal Vasko59892dd2022-05-13 11:02:30 +02001090 break;
1091 }
1092 }
1093 }
1094
1095 if (!siblings) {
1096 if (match) {
1097 *match = NULL;
1098 }
1099 return LY_ENOTFOUND;
1100 }
1101
1102 if (match) {
1103 *match = (struct lyd_node *)siblings;
1104 }
1105 return LY_SUCCESS;
1106}
1107
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001108void
Michal Vaskod3bb12f2020-12-04 14:33:09 +01001109lyd_del_move_root(struct lyd_node **root, const struct lyd_node *to_del, const struct lys_module *mod)
1110{
1111 if (*root && (lyd_owner_module(*root) != mod)) {
1112 /* there are no data of mod so this is simply the first top-level sibling */
1113 mod = NULL;
1114 }
1115
1116 if ((*root != to_del) || (*root)->parent) {
1117 return;
1118 }
1119
Michal Vasko598063b2021-07-19 11:39:05 +02001120 if (mod && (*root)->prev->next && (!(*root)->next || (lyd_owner_module(to_del) != lyd_owner_module((*root)->next)))) {
1121 /* there are no more nodes from mod, simply get the first top-level sibling */
Michal Vaskod3bb12f2020-12-04 14:33:09 +01001122 *root = lyd_first_sibling(*root);
Michal Vasko598063b2021-07-19 11:39:05 +02001123 } else {
1124 *root = (*root)->next;
Michal Vaskod3bb12f2020-12-04 14:33:09 +01001125 }
1126}
1127
Michal Vasko8cc3f662022-03-29 11:25:51 +02001128LY_ERR
1129ly_nested_ext_schema(const struct lyd_node *parent, const struct lysc_node *sparent, const char *prefix,
1130 size_t prefix_len, LY_VALUE_FORMAT format, void *prefix_data, const char *name, size_t name_len,
1131 const struct lysc_node **snode, struct lysc_ext_instance **ext)
1132{
1133 LY_ERR r;
1134 LY_ARRAY_COUNT_TYPE u;
1135 struct lysc_ext_instance *nested_exts = NULL;
1136 lyplg_ext_data_snode_clb ext_snode_cb;
1137
1138 /* check if there are any nested extension instances */
1139 if (parent && parent->schema) {
1140 nested_exts = parent->schema->exts;
1141 } else if (sparent) {
1142 nested_exts = sparent->exts;
1143 }
1144 LY_ARRAY_FOR(nested_exts, u) {
Michal Vasko305c6cb2022-04-27 10:33:04 +02001145 if (!nested_exts[u].def->plugin) {
1146 /* no plugin */
1147 continue;
1148 }
1149
Michal Vasko8cc3f662022-03-29 11:25:51 +02001150 ext_snode_cb = nested_exts[u].def->plugin->snode;
1151 if (!ext_snode_cb) {
1152 /* not an extension with nested data */
1153 continue;
1154 }
1155
1156 /* try to get the schema node */
1157 r = ext_snode_cb(&nested_exts[u], parent, sparent, prefix, prefix_len, format, prefix_data, name, name_len, snode);
1158 if (!r) {
1159 /* data successfully created, remember the ext instance */
1160 *ext = &nested_exts[u];
1161 return LY_SUCCESS;
1162 } else if (r != LY_ENOT) {
1163 /* fatal error */
1164 return r;
1165 }
1166 /* data was not from this module, continue */
1167 }
1168
1169 /* no extensions or none matched */
1170 return LY_ENOT;
1171}
1172
Michal Vaskod3bb12f2020-12-04 14:33:09 +01001173void
Radek Krejci8df109d2021-04-23 12:19:08 +02001174ly_free_prefix_data(LY_VALUE_FORMAT format, void *prefix_data)
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001175{
1176 struct ly_set *ns_list;
1177 struct lysc_prefix *prefixes;
1178 uint32_t i;
1179 LY_ARRAY_COUNT_TYPE u;
1180
1181 if (!prefix_data) {
1182 return;
1183 }
1184
1185 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001186 case LY_VALUE_XML:
Michal Vaskoddd76592022-01-17 13:34:48 +01001187 case LY_VALUE_STR_NS:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001188 ns_list = prefix_data;
1189 for (i = 0; i < ns_list->count; ++i) {
1190 free(((struct lyxml_ns *)ns_list->objs[i])->prefix);
1191 free(((struct lyxml_ns *)ns_list->objs[i])->uri);
1192 }
1193 ly_set_free(ns_list, free);
1194 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02001195 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001196 prefixes = prefix_data;
1197 LY_ARRAY_FOR(prefixes, u) {
1198 free(prefixes[u].prefix);
1199 }
1200 LY_ARRAY_FREE(prefixes);
1201 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02001202 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02001203 case LY_VALUE_SCHEMA:
1204 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02001205 case LY_VALUE_LYB:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001206 break;
1207 }
1208}
1209
1210LY_ERR
Radek Krejci8df109d2021-04-23 12:19:08 +02001211ly_dup_prefix_data(const struct ly_ctx *ctx, LY_VALUE_FORMAT format, const void *prefix_data,
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001212 void **prefix_data_p)
1213{
1214 LY_ERR ret = LY_SUCCESS;
1215 struct lyxml_ns *ns;
1216 struct lysc_prefix *prefixes = NULL, *orig_pref;
1217 struct ly_set *ns_list, *orig_ns;
1218 uint32_t i;
1219 LY_ARRAY_COUNT_TYPE u;
1220
1221 assert(!*prefix_data_p);
1222
1223 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001224 case LY_VALUE_SCHEMA:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001225 *prefix_data_p = (void *)prefix_data;
1226 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02001227 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001228 /* copy all the value prefixes */
1229 orig_pref = (struct lysc_prefix *)prefix_data;
1230 LY_ARRAY_CREATE_GOTO(ctx, prefixes, LY_ARRAY_COUNT(orig_pref), ret, cleanup);
1231 *prefix_data_p = prefixes;
1232
1233 LY_ARRAY_FOR(orig_pref, u) {
1234 if (orig_pref[u].prefix) {
1235 prefixes[u].prefix = strdup(orig_pref[u].prefix);
1236 LY_CHECK_ERR_GOTO(!prefixes[u].prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1237 }
1238 prefixes[u].mod = orig_pref[u].mod;
1239 LY_ARRAY_INCREMENT(prefixes);
1240 }
1241 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02001242 case LY_VALUE_XML:
Michal Vaskoddd76592022-01-17 13:34:48 +01001243 case LY_VALUE_STR_NS:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001244 /* copy all the namespaces */
1245 LY_CHECK_GOTO(ret = ly_set_new(&ns_list), cleanup);
1246 *prefix_data_p = ns_list;
1247
1248 orig_ns = (struct ly_set *)prefix_data;
1249 for (i = 0; i < orig_ns->count; ++i) {
1250 ns = calloc(1, sizeof *ns);
1251 LY_CHECK_ERR_GOTO(!ns, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1252 LY_CHECK_GOTO(ret = ly_set_add(ns_list, ns, 1, NULL), cleanup);
1253
1254 if (((struct lyxml_ns *)orig_ns->objs[i])->prefix) {
1255 ns->prefix = strdup(((struct lyxml_ns *)orig_ns->objs[i])->prefix);
1256 LY_CHECK_ERR_GOTO(!ns->prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1257 }
1258 ns->uri = strdup(((struct lyxml_ns *)orig_ns->objs[i])->uri);
1259 LY_CHECK_ERR_GOTO(!ns->uri, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1260 }
1261 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02001262 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02001263 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02001264 case LY_VALUE_LYB:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001265 assert(!prefix_data);
1266 *prefix_data_p = NULL;
1267 break;
1268 }
1269
1270cleanup:
1271 if (ret) {
1272 ly_free_prefix_data(format, *prefix_data_p);
1273 *prefix_data_p = NULL;
1274 }
1275 return ret;
1276}
1277
1278LY_ERR
Radek Krejcif9943642021-04-26 10:18:21 +02001279ly_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 +02001280 const void *prefix_data, LY_VALUE_FORMAT *format_p, void **prefix_data_p)
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001281{
1282 LY_ERR ret = LY_SUCCESS;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001283 const struct lys_module *mod;
Michal Vaskofc2cd072021-02-24 13:17:17 +01001284 const struct lyxml_ns *ns;
1285 struct lyxml_ns *new_ns;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001286 struct ly_set *ns_list;
1287 struct lysc_prefix *prefixes = NULL, *val_pref;
aPiecek83436bc2021-03-30 12:20:45 +02001288 const char *value_iter, *value_next, *value_end;
1289 uint32_t substr_len;
1290 ly_bool is_prefix;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001291
1292 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001293 case LY_VALUE_SCHEMA:
Michal Vaskofc2cd072021-02-24 13:17:17 +01001294 /* copy all referenced modules as prefix - module pairs */
1295 if (!*prefix_data_p) {
1296 /* new prefix data */
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001297 LY_ARRAY_CREATE_GOTO(ctx, prefixes, 0, ret, cleanup);
Radek Krejci8df109d2021-04-23 12:19:08 +02001298 *format_p = LY_VALUE_SCHEMA_RESOLVED;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001299 *prefix_data_p = prefixes;
Michal Vaskofc2cd072021-02-24 13:17:17 +01001300 } else {
1301 /* reuse prefix data */
Radek Krejci8df109d2021-04-23 12:19:08 +02001302 assert(*format_p == LY_VALUE_SCHEMA_RESOLVED);
Michal Vaskofc2cd072021-02-24 13:17:17 +01001303 prefixes = *prefix_data_p;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001304 }
1305
Michal Vaskoc0f9c4c2022-05-06 12:12:17 +02001306 /* add current module for unprefixed values */
1307 LY_ARRAY_NEW_GOTO(ctx, prefixes, val_pref, ret, cleanup);
1308 *prefix_data_p = prefixes;
1309
1310 val_pref->prefix = NULL;
1311 val_pref->mod = ((const struct lysp_module *)prefix_data)->mod;
1312
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001313 /* add all used prefixes */
Michal Vasko59e90fc2021-09-22 12:17:08 +02001314 value_end = (char *)value + value_len;
aPiecek83436bc2021-03-30 12:20:45 +02001315 for (value_iter = value; value_iter; value_iter = value_next) {
aPieceke3f828d2021-05-10 15:34:41 +02001316 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 +02001317 if (is_prefix) {
1318 /* we have a possible prefix. Do we already have the prefix? */
1319 mod = ly_resolve_prefix(ctx, value_iter, substr_len, *format_p, *prefix_data_p);
1320 if (!mod) {
1321 mod = ly_resolve_prefix(ctx, value_iter, substr_len, format, prefix_data);
1322 if (mod) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001323 assert(*format_p == LY_VALUE_SCHEMA_RESOLVED);
aPiecek83436bc2021-03-30 12:20:45 +02001324 /* store a new prefix - module pair */
1325 LY_ARRAY_NEW_GOTO(ctx, prefixes, val_pref, ret, cleanup);
1326 *prefix_data_p = prefixes;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001327
aPiecek83436bc2021-03-30 12:20:45 +02001328 val_pref->prefix = strndup(value_iter, substr_len);
1329 LY_CHECK_ERR_GOTO(!val_pref->prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1330 val_pref->mod = mod;
1331 } /* else it is not even defined */
1332 } /* else the prefix is already present */
Michal Vaskofc2cd072021-02-24 13:17:17 +01001333 }
1334 }
1335 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02001336 case LY_VALUE_XML:
Michal Vaskoddd76592022-01-17 13:34:48 +01001337 case LY_VALUE_STR_NS:
Michal Vaskofc2cd072021-02-24 13:17:17 +01001338 /* copy all referenced namespaces as prefix - namespace pairs */
1339 if (!*prefix_data_p) {
1340 /* new prefix data */
1341 LY_CHECK_GOTO(ret = ly_set_new(&ns_list), cleanup);
Michal Vaskoddd76592022-01-17 13:34:48 +01001342 *format_p = format;
Michal Vaskofc2cd072021-02-24 13:17:17 +01001343 *prefix_data_p = ns_list;
1344 } else {
1345 /* reuse prefix data */
Michal Vaskoddd76592022-01-17 13:34:48 +01001346 assert(*format_p == format);
Michal Vaskofc2cd072021-02-24 13:17:17 +01001347 ns_list = *prefix_data_p;
1348 }
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001349
Michal Vasko294e7f02022-02-28 13:59:00 +01001350 /* store default namespace */
1351 ns = lyxml_ns_get(prefix_data, NULL, 0);
1352 if (ns) {
1353 new_ns = calloc(1, sizeof *new_ns);
1354 LY_CHECK_ERR_GOTO(!new_ns, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1355 LY_CHECK_GOTO(ret = ly_set_add(ns_list, new_ns, 1, NULL), cleanup);
1356
1357 new_ns->prefix = NULL;
1358 new_ns->uri = strdup(ns->uri);
1359 LY_CHECK_ERR_GOTO(!new_ns->uri, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1360 }
1361
Michal Vaskofc2cd072021-02-24 13:17:17 +01001362 /* add all used prefixes */
Michal Vasko59e90fc2021-09-22 12:17:08 +02001363 value_end = (char *)value + value_len;
aPiecek83436bc2021-03-30 12:20:45 +02001364 for (value_iter = value; value_iter; value_iter = value_next) {
aPieceke3f828d2021-05-10 15:34:41 +02001365 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 +02001366 if (is_prefix) {
1367 /* we have a possible prefix. Do we already have the prefix? */
1368 ns = lyxml_ns_get(ns_list, value_iter, substr_len);
1369 if (!ns) {
1370 ns = lyxml_ns_get(prefix_data, value_iter, substr_len);
1371 if (ns) {
1372 /* store a new prefix - namespace pair */
1373 new_ns = calloc(1, sizeof *new_ns);
1374 LY_CHECK_ERR_GOTO(!new_ns, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1375 LY_CHECK_GOTO(ret = ly_set_add(ns_list, new_ns, 1, NULL), cleanup);
Michal Vaskofc2cd072021-02-24 13:17:17 +01001376
aPiecek83436bc2021-03-30 12:20:45 +02001377 new_ns->prefix = strndup(value_iter, substr_len);
1378 LY_CHECK_ERR_GOTO(!new_ns->prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1379 new_ns->uri = strdup(ns->uri);
1380 LY_CHECK_ERR_GOTO(!new_ns->uri, LOGMEM(ctx); ret = LY_EMEM, cleanup);
1381 } /* else it is not even defined */
1382 } /* else the prefix is already present */
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001383 }
1384 }
1385 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02001386 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02001387 case LY_VALUE_SCHEMA_RESOLVED:
1388 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02001389 case LY_VALUE_LYB:
Michal Vaskofc2cd072021-02-24 13:17:17 +01001390 if (!*prefix_data_p) {
1391 /* new prefix data - simply copy all the prefix data */
1392 *format_p = format;
1393 LY_CHECK_GOTO(ret = ly_dup_prefix_data(ctx, format, prefix_data, prefix_data_p), cleanup);
1394 } /* else reuse prefix data - the prefix data are always the same, nothing to do */
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001395 break;
1396 }
1397
1398cleanup:
1399 if (ret) {
1400 ly_free_prefix_data(*format_p, *prefix_data_p);
1401 *prefix_data_p = NULL;
1402 }
1403 return ret;
1404}
Michal Vasko7ed1fcb2020-12-03 14:15:22 +01001405
1406const char *
Radek Krejci8df109d2021-04-23 12:19:08 +02001407ly_format2str(LY_VALUE_FORMAT format)
Michal Vasko7ed1fcb2020-12-03 14:15:22 +01001408{
1409 switch (format) {
Radek Krejci224d4b42021-04-23 13:54:59 +02001410 case LY_VALUE_CANON:
1411 return "canonical";
Radek Krejci8df109d2021-04-23 12:19:08 +02001412 case LY_VALUE_SCHEMA:
Michal Vasko7ed1fcb2020-12-03 14:15:22 +01001413 return "schema imports";
Radek Krejci8df109d2021-04-23 12:19:08 +02001414 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko7ed1fcb2020-12-03 14:15:22 +01001415 return "schema stored mapping";
Radek Krejci8df109d2021-04-23 12:19:08 +02001416 case LY_VALUE_XML:
Michal Vasko7ed1fcb2020-12-03 14:15:22 +01001417 return "XML prefixes";
Radek Krejci8df109d2021-04-23 12:19:08 +02001418 case LY_VALUE_JSON:
Michal Vasko7ed1fcb2020-12-03 14:15:22 +01001419 return "JSON module names";
Radek Krejcif9943642021-04-26 10:18:21 +02001420 case LY_VALUE_LYB:
1421 return "LYB prefixes";
Michal Vasko7ed1fcb2020-12-03 14:15:22 +01001422 default:
1423 break;
1424 }
1425
1426 return NULL;
1427}
Michal Vasko43297a02021-05-19 11:12:37 +02001428
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001429LIBYANG_API_DEF LY_ERR
Michal Vasko43297a02021-05-19 11:12:37 +02001430ly_time_str2time(const char *value, time_t *time, char **fractions_s)
1431{
1432 struct tm tm = {0};
1433 uint32_t i, frac_len;
1434 const char *frac;
1435 int64_t shift, shift_m;
1436 time_t t;
1437
1438 LY_CHECK_ARG_RET(NULL, value, time, LY_EINVAL);
1439
1440 tm.tm_year = atoi(&value[0]) - 1900;
1441 tm.tm_mon = atoi(&value[5]) - 1;
1442 tm.tm_mday = atoi(&value[8]);
1443 tm.tm_hour = atoi(&value[11]);
1444 tm.tm_min = atoi(&value[14]);
1445 tm.tm_sec = atoi(&value[17]);
1446
1447 t = timegm(&tm);
1448 i = 19;
1449
1450 /* fractions of a second */
1451 if (value[i] == '.') {
1452 ++i;
1453 frac = &value[i];
1454 for (frac_len = 0; isdigit(frac[frac_len]); ++frac_len) {}
1455
1456 i += frac_len;
Michal Vasko43297a02021-05-19 11:12:37 +02001457 } else {
1458 frac = NULL;
1459 }
1460
1461 /* apply offset */
1462 if ((value[i] == 'Z') || (value[i] == 'z')) {
1463 /* zero shift */
1464 shift = 0;
1465 } else {
1466 shift = strtol(&value[i], NULL, 10);
1467 shift = shift * 60 * 60; /* convert from hours to seconds */
1468 shift_m = strtol(&value[i + 4], NULL, 10) * 60; /* includes conversion from minutes to seconds */
1469 /* correct sign */
1470 if (shift < 0) {
1471 shift_m *= -1;
1472 }
1473 /* connect hours and minutes of the shift */
1474 shift = shift + shift_m;
1475 }
1476
1477 /* we have to shift to the opposite way to correct the time */
1478 t -= shift;
1479
1480 *time = t;
1481 if (fractions_s) {
1482 if (frac) {
1483 *fractions_s = strndup(frac, frac_len);
1484 LY_CHECK_RET(!*fractions_s, LY_EMEM);
1485 } else {
1486 *fractions_s = NULL;
1487 }
1488 }
1489 return LY_SUCCESS;
1490}
1491
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001492LIBYANG_API_DEF LY_ERR
Michal Vasko43297a02021-05-19 11:12:37 +02001493ly_time_time2str(time_t time, const char *fractions_s, char **str)
1494{
1495 struct tm tm;
Michal Vasko143ffa82021-05-20 11:11:39 +02001496 char zoneshift[8];
Michal Vasko43297a02021-05-19 11:12:37 +02001497 int32_t zonediff_h, zonediff_m;
1498
1499 LY_CHECK_ARG_RET(NULL, str, LY_EINVAL);
1500
1501 /* initialize the local timezone */
1502 tzset();
1503
Jan Kundrátb17efe92022-02-14 18:32:18 +01001504#ifdef HAVE_TM_GMTOFF
Michal Vasko43297a02021-05-19 11:12:37 +02001505 /* convert */
1506 if (!localtime_r(&time, &tm)) {
1507 return LY_ESYS;
1508 }
1509
1510 /* get timezone offset */
1511 if (tm.tm_gmtoff == 0) {
1512 /* time is Zulu (UTC) */
1513 zonediff_h = 0;
1514 zonediff_m = 0;
1515 } else {
1516 /* timezone offset */
1517 zonediff_h = tm.tm_gmtoff / 60 / 60;
1518 zonediff_m = tm.tm_gmtoff / 60 % 60;
1519 }
1520 sprintf(zoneshift, "%+03d:%02d", zonediff_h, zonediff_m);
Jan Kundráte182a272021-12-09 23:25:15 +01001521#else
Jan Kundrátb17efe92022-02-14 18:32:18 +01001522 /* convert */
1523 if (!gmtime_r(&time, &tm)) {
1524 return LY_ESYS;
1525 }
1526
Jan Kundráte182a272021-12-09 23:25:15 +01001527 (void)zonediff_h;
1528 (void)zonediff_m;
1529 sprintf(zoneshift, "-00:00");
1530#endif
Michal Vasko43297a02021-05-19 11:12:37 +02001531
1532 /* print */
1533 if (asprintf(str, "%04d-%02d-%02dT%02d:%02d:%02d%s%s%s",
1534 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
1535 fractions_s ? "." : "", fractions_s ? fractions_s : "", zoneshift) == -1) {
1536 return LY_EMEM;
1537 }
1538
1539 return LY_SUCCESS;
1540}
1541
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001542LIBYANG_API_DEF LY_ERR
Michal Vasko43297a02021-05-19 11:12:37 +02001543ly_time_str2ts(const char *value, struct timespec *ts)
1544{
1545 LY_ERR rc;
Michal Vasko72975062021-08-25 08:13:04 +02001546 char *fractions_s, frac_buf[10];
Michal Vasko43297a02021-05-19 11:12:37 +02001547 int frac_len;
1548
1549 LY_CHECK_ARG_RET(NULL, value, ts, LY_EINVAL);
1550
1551 rc = ly_time_str2time(value, &ts->tv_sec, &fractions_s);
1552 LY_CHECK_RET(rc);
1553
1554 /* convert fractions of a second to nanoseconds */
1555 if (fractions_s) {
Michal Vasko72975062021-08-25 08:13:04 +02001556 /* init frac_buf with zeroes */
1557 memset(frac_buf, '0', 9);
1558 frac_buf[9] = '\0';
1559
Michal Vasko43297a02021-05-19 11:12:37 +02001560 frac_len = strlen(fractions_s);
1561 memcpy(frac_buf, fractions_s, frac_len > 9 ? 9 : frac_len);
1562 ts->tv_nsec = atol(frac_buf);
1563 free(fractions_s);
1564 } else {
1565 ts->tv_nsec = 0;
1566 }
1567
1568 return LY_SUCCESS;
1569}
1570
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001571LIBYANG_API_DEF LY_ERR
Michal Vasko43297a02021-05-19 11:12:37 +02001572ly_time_ts2str(const struct timespec *ts, char **str)
1573{
1574 char frac_buf[10];
1575
Jan Kundrátbd157002021-08-30 14:02:22 +02001576 LY_CHECK_ARG_RET(NULL, ts, str, ((ts->tv_nsec <= 999999999) && (ts->tv_nsec >= 0)), LY_EINVAL);
Michal Vasko43297a02021-05-19 11:12:37 +02001577
1578 /* convert nanoseconds to fractions of a second */
1579 if (ts->tv_nsec) {
1580 sprintf(frac_buf, "%09ld", ts->tv_nsec);
1581 }
1582
1583 return ly_time_time2str(ts->tv_sec, ts->tv_nsec ? frac_buf : NULL, str);
1584}