blob: e1eadf8e3c43d6fc4c11effd896d16f4f2d7592e [file] [log] [blame]
Radek Krejcie7b95092019-05-15 11:03:07 +02001/**
2 * @file tree_data_helpers.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Parsing and validation helper functions for data trees
5 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Radek Krejcif8dc59a2020-11-25 13:47:44 +010014#define _POSIX_C_SOURCE 200809L /* strdup, strndup */
Radek Krejcie7b95092019-05-15 11:03:07 +020015
16#include <assert.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020017#include <stdint.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020018#include <stdlib.h>
Radek Krejciad97c5f2020-06-30 09:19:28 +020019#include <string.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020020
Radek Krejci535ea9f2020-05-29 16:01:05 +020021#include "common.h"
Michal Vasko6b5cb2a2020-11-11 19:11:21 +010022#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020023#include "context.h"
Radek Krejci47fab892020-11-05 17:02:41 +010024#include "dict.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020025#include "hash_table.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020026#include "log.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020027#include "lyb.h"
Radek Krejci7931b192020-06-25 17:05:03 +020028#include "parser_data.h"
Michal Vaskoa820c312021-02-05 16:33:00 +010029#include "printer_data.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020030#include "set.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020031#include "tree.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020032#include "tree_data.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020033#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010034#include "tree_edit.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020035#include "tree_schema.h"
Radek Krejci0aa1f702021-04-01 16:16:19 +020036#include "tree_schema_internal.h"
Radek Krejci4f2e3e52021-03-30 14:20:28 +020037#include "validation.h"
Radek Krejci77114102021-03-10 15:21:57 +010038#include "xml.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020039
Michal Vaskoa6669ba2020-08-06 16:14:26 +020040struct lyd_node *
41lys_getnext_data(const struct lyd_node *last, const struct lyd_node *sibling, const struct lysc_node **slast,
Radek Krejci0f969882020-08-21 16:56:47 +020042 const struct lysc_node *parent, const struct lysc_module *module)
Michal Vaskoa6669ba2020-08-06 16:14:26 +020043{
44 const struct lysc_node *siter = NULL;
45 struct lyd_node *match = NULL;
46
47 assert(parent || module);
48 assert(!last || (slast && *slast));
49
50 if (slast) {
51 siter = *slast;
52 }
53
54 if (last && last->next && (last->next->schema == siter)) {
55 /* return next data instance */
56 return last->next;
57 }
58
59 /* find next schema node data instance */
60 while ((siter = lys_getnext(siter, parent, module, 0))) {
61 if (!lyd_find_sibling_val(sibling, siter, NULL, 0, &match)) {
62 break;
63 }
64 }
65
66 if (slast) {
67 *slast = siter;
68 }
69 return match;
70}
71
Radek Krejcie7b95092019-05-15 11:03:07 +020072struct lyd_node **
Michal Vaskoe0665742021-02-11 11:08:44 +010073lyd_node_child_p(struct lyd_node *node)
Radek Krejcie7b95092019-05-15 11:03:07 +020074{
75 assert(node);
Michal Vasko52927e22020-03-16 17:26:14 +010076
77 if (!node->schema) {
78 return &((struct lyd_node_opaq *)node)->child;
79 } else {
80 switch (node->schema->nodetype) {
81 case LYS_CONTAINER:
82 case LYS_LIST:
Michal Vasko1bf09392020-03-27 12:38:10 +010083 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +010084 case LYS_ACTION:
85 case LYS_NOTIF:
86 return &((struct lyd_node_inner *)node)->child;
87 default:
88 return NULL;
89 }
Radek Krejcie7b95092019-05-15 11:03:07 +020090 }
91}
92
Radek Krejcidae0ee82020-05-06 16:53:24 +020093API struct lyd_node *
Radek Krejcia1c1e542020-09-29 16:06:52 +020094lyd_child_no_keys(const struct lyd_node *node)
95{
96 struct lyd_node **children;
97
98 if (!node) {
99 return NULL;
100 }
101
102 if (!node->schema) {
103 /* opaq node */
Michal Vasko9e685082021-01-29 14:49:09 +0100104 return ((struct lyd_node_opaq *)node)->child;
Radek Krejcia1c1e542020-09-29 16:06:52 +0200105 }
106
Michal Vaskoe0665742021-02-11 11:08:44 +0100107 children = lyd_node_child_p((struct lyd_node *)node);
Radek Krejcia1c1e542020-09-29 16:06:52 +0200108 if (children) {
109 struct lyd_node *child = *children;
110 while (child && child->schema && (child->schema->flags & LYS_KEY)) {
111 child = child->next;
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200112 }
113 return child;
Radek Krejcie7b95092019-05-15 11:03:07 +0200114 } else {
115 return NULL;
116 }
117}
Michal Vasko9b368d32020-02-14 13:53:31 +0100118
Michal Vaskoc193ce92020-03-06 11:04:48 +0100119API const struct lys_module *
120lyd_owner_module(const struct lyd_node *node)
Michal Vasko9b368d32020-02-14 13:53:31 +0100121{
122 const struct lysc_node *schema;
Michal Vaskod5cfa6e2020-11-23 16:56:08 +0100123 const struct lyd_node_opaq *opaq;
Michal Vasko9b368d32020-02-14 13:53:31 +0100124
Michal Vaskod5cfa6e2020-11-23 16:56:08 +0100125 if (!node) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100126 return NULL;
127 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100128
Michal Vaskod5cfa6e2020-11-23 16:56:08 +0100129 if (!node->schema) {
130 opaq = (struct lyd_node_opaq *)node;
131 switch (opaq->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200132 case LY_VALUE_XML:
Michal Vaskod5cfa6e2020-11-23 16:56:08 +0100133 return ly_ctx_get_module_implemented_ns(LYD_CTX(node), opaq->name.module_ns);
Radek Krejci8df109d2021-04-23 12:19:08 +0200134 case LY_VALUE_JSON:
Michal Vaskod5cfa6e2020-11-23 16:56:08 +0100135 return ly_ctx_get_module_implemented(LYD_CTX(node), opaq->name.module_name);
136 default:
137 return NULL;
138 }
139 }
140
Radek Krejci1e008d22020-08-17 11:37:37 +0200141 for (schema = node->schema; schema->parent; schema = schema->parent) {}
Michal Vasko9b368d32020-02-14 13:53:31 +0100142 return schema->module;
143}
Michal Vaskob1b5c262020-03-05 14:29:47 +0100144
145const struct lys_module *
Michal Vasko26e80012020-07-08 10:55:46 +0200146lyd_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 +0200147 struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +0100148{
149 struct lyd_node *iter;
150 const struct lys_module *mod;
151
152 /* get the next module */
Michal Vasko26e80012020-07-08 10:55:46 +0200153 if (module) {
154 if (*i) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100155 mod = NULL;
Michal Vasko26e80012020-07-08 10:55:46 +0200156 } else {
157 mod = module;
158 ++(*i);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100159 }
160 } else {
161 do {
162 mod = ly_ctx_get_module_iter(ctx, i);
163 } while (mod && !mod->implemented);
164 }
165
166 /* find its data */
167 *first = NULL;
168 if (mod) {
169 LY_LIST_FOR(tree, iter) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100170 if (lyd_owner_module(iter) == mod) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100171 *first = iter;
172 break;
173 }
174 }
175 }
176
177 return mod;
178}
179
180const struct lys_module *
181lyd_data_next_module(struct lyd_node **next, struct lyd_node **first)
182{
183 const struct lys_module *mod;
184
185 if (!*next) {
186 /* all data traversed */
187 *first = NULL;
188 return NULL;
189 }
190
191 *first = *next;
192
193 /* prepare next */
Michal Vaskoc193ce92020-03-06 11:04:48 +0100194 mod = lyd_owner_module(*next);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100195 LY_LIST_FOR(*next, *next) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100196 if (lyd_owner_module(*next) != mod) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100197 break;
198 }
199 }
200
201 return mod;
202}
Michal Vasko9f96a052020-03-10 09:41:45 +0100203
204LY_ERR
205lyd_parse_check_keys(struct lyd_node *node)
206{
207 const struct lysc_node *skey = NULL;
208 const struct lyd_node *key;
209
210 assert(node->schema->nodetype == LYS_LIST);
211
Radek Krejcia1c1e542020-09-29 16:06:52 +0200212 key = lyd_child(node);
Michal Vasko9f96a052020-03-10 09:41:45 +0100213 while ((skey = lys_getnext(skey, node->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
214 if (!key || (key->schema != skey)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100215 LOGVAL(LYD_CTX(node), LY_VCODE_NOKEY, skey->name);
Michal Vasko9f96a052020-03-10 09:41:45 +0100216 return LY_EVALID;
217 }
218
219 key = key->next;
220 }
221
222 return LY_SUCCESS;
223}
Michal Vasko60ea6352020-06-29 13:39:39 +0200224
225void
Radek Krejci4f2e3e52021-03-30 14:20:28 +0200226lyd_parse_set_data_flags(struct lyd_node *node, struct ly_set *when_check, struct ly_set *exts_check, struct lyd_meta **meta,
227 uint32_t options)
Michal Vasko60ea6352020-06-29 13:39:39 +0200228{
229 struct lyd_meta *meta2, *prev_meta = NULL;
230
Michal Vaskoa5705e52020-12-09 18:15:14 +0100231 if (lysc_has_when(node->schema)) {
Michal Vasko0f3377d2020-11-09 20:56:11 +0100232 if (!(options & LYD_PARSE_ONLY)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200233 /* remember we need to evaluate this node's when */
Radek Krejci3d92e442020-10-12 12:48:13 +0200234 LY_CHECK_RET(ly_set_add(when_check, node, 1, NULL), );
Michal Vasko60ea6352020-06-29 13:39:39 +0200235 }
236 }
Radek Krejci4f2e3e52021-03-30 14:20:28 +0200237 LY_CHECK_RET(lysc_node_ext_tovalidate(exts_check, node), );
Michal Vasko60ea6352020-06-29 13:39:39 +0200238
Michal Vasko60ea6352020-06-29 13:39:39 +0200239 LY_LIST_FOR(*meta, meta2) {
Michal Vasko69730152020-10-09 16:30:07 +0200240 if (!strcmp(meta2->name, "default") && !strcmp(meta2->annotation->module->name, "ietf-netconf-with-defaults") &&
241 meta2->value.boolean) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200242 /* node is default according to the metadata */
243 node->flags |= LYD_DEFAULT;
244
245 /* delete the metadata */
246 if (prev_meta) {
247 prev_meta->next = meta2->next;
248 } else {
249 *meta = (*meta)->next;
250 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200251 lyd_free_meta_single(meta2);
Michal Vasko60ea6352020-06-29 13:39:39 +0200252 break;
253 }
254
255 prev_meta = meta2;
256 }
257}
258
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200259API const char *
Christian Hopps46bd21b2021-04-27 09:43:58 -0400260lyd_value_get_canonical(const struct ly_ctx *ctx, const struct lyd_value *value)
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200261{
Michal Vasko33876022021-04-27 16:42:24 +0200262 return value->_canonical ? value->_canonical :
263 (const char *)value->realtype->plugin->print(ctx, value, LY_VALUE_CANON, NULL, NULL, NULL);
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200264}
265
Michal Vaskoc0004272020-08-06 08:32:34 +0200266API LY_ERR
Michal Vaskoa820c312021-02-05 16:33:00 +0100267lyd_any_value_str(const struct lyd_node *any, char **value_str)
268{
269 const struct lyd_node_any *a;
270 struct lyd_node *tree = NULL;
271 const char *str = NULL;
272 ly_bool dynamic = 0;
273 LY_ERR ret = LY_SUCCESS;
274
275 LY_CHECK_ARG_RET(NULL, any, value_str, LY_EINVAL);
Radek Krejci71877df2021-04-06 17:24:06 +0200276 LY_CHECK_ARG_RET(NULL, any->schema, any->schema->nodetype & LYS_ANYDATA, LY_EINVAL);
Michal Vaskoa820c312021-02-05 16:33:00 +0100277
278 a = (struct lyd_node_any *)any;
279 *value_str = NULL;
280
281 if (!a->value.str) {
282 /* there is no value in the union */
283 return LY_SUCCESS;
284 }
285
286 switch (a->value_type) {
287 case LYD_ANYDATA_LYB:
288 /* parse into a data tree */
289 ret = lyd_parse_data_mem(LYD_CTX(any), a->value.mem, LYD_LYB, LYD_PARSE_ONLY, 0, &tree);
290 LY_CHECK_GOTO(ret, cleanup);
291 dynamic = 1;
292 break;
293 case LYD_ANYDATA_DATATREE:
294 tree = a->value.tree;
295 break;
296 case LYD_ANYDATA_STRING:
297 case LYD_ANYDATA_XML:
298 case LYD_ANYDATA_JSON:
299 /* simply use the string */
300 str = a->value.str;
301 break;
302 }
303
304 if (tree) {
305 /* print into a string */
306 ret = lyd_print_mem(value_str, tree, LYD_XML, LYD_PRINT_WITHSIBLINGS);
307 LY_CHECK_GOTO(ret, cleanup);
308 } else {
309 assert(str);
310 *value_str = strdup(str);
311 LY_CHECK_ERR_GOTO(!*value_str, LOGMEM(LYD_CTX(any)), cleanup);
312 }
313
314 /* success */
315
316cleanup:
317 if (dynamic) {
318 lyd_free_all(tree);
319 }
320 return ret;
321}
322
323API LY_ERR
Michal Vasko61551fa2020-07-09 15:45:45 +0200324lyd_any_copy_value(struct lyd_node *trg, const union lyd_any_value *value, LYD_ANYDATA_VALUETYPE value_type)
325{
326 struct lyd_node_any *t;
Michal Vasko61551fa2020-07-09 15:45:45 +0200327
Michal Vaskoa820c312021-02-05 16:33:00 +0100328 LY_CHECK_ARG_RET(NULL, trg, LY_EINVAL);
Radek Krejci71877df2021-04-06 17:24:06 +0200329 LY_CHECK_ARG_RET(NULL, trg->schema, trg->schema->nodetype & LYS_ANYDATA, LY_EINVAL);
Michal Vasko61551fa2020-07-09 15:45:45 +0200330
331 t = (struct lyd_node_any *)trg;
332
333 /* free trg */
334 switch (t->value_type) {
335 case LYD_ANYDATA_DATATREE:
336 lyd_free_all(t->value.tree);
337 break;
338 case LYD_ANYDATA_STRING:
339 case LYD_ANYDATA_XML:
340 case LYD_ANYDATA_JSON:
Michal Vaskoe180ed02021-02-05 16:31:20 +0100341 lydict_remove(LYD_CTX(trg), t->value.str);
Michal Vasko61551fa2020-07-09 15:45:45 +0200342 break;
343 case LYD_ANYDATA_LYB:
344 free(t->value.mem);
345 break;
346 }
347 t->value.str = NULL;
348
349 if (!value) {
350 /* only free value in this case */
351 return LY_SUCCESS;
352 }
353
354 /* copy src */
355 t->value_type = value_type;
356 switch (value_type) {
357 case LYD_ANYDATA_DATATREE:
358 if (value->tree) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200359 LY_CHECK_RET(lyd_dup_siblings(value->tree, NULL, LYD_DUP_RECURSIVE, &t->value.tree));
Michal Vasko61551fa2020-07-09 15:45:45 +0200360 }
361 break;
362 case LYD_ANYDATA_STRING:
363 case LYD_ANYDATA_XML:
364 case LYD_ANYDATA_JSON:
365 if (value->str) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200366 LY_CHECK_RET(lydict_insert(LYD_CTX(trg), value->str, 0, &t->value.str));
Michal Vasko61551fa2020-07-09 15:45:45 +0200367 }
368 break;
369 case LYD_ANYDATA_LYB:
370 if (value->mem) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200371 int len = lyd_lyb_data_length(value->mem);
Radek Krejci82fa8d42020-07-11 22:00:59 +0200372 LY_CHECK_RET(len == -1, LY_EINVAL);
Michal Vasko61551fa2020-07-09 15:45:45 +0200373 t->value.mem = malloc(len);
Michal Vaskob7be7a82020-08-20 09:09:04 +0200374 LY_CHECK_ERR_RET(!t->value.mem, LOGMEM(LYD_CTX(trg)), LY_EMEM);
Michal Vasko61551fa2020-07-09 15:45:45 +0200375 memcpy(t->value.mem, value->mem, len);
376 }
377 break;
378 }
379
380 return LY_SUCCESS;
381}
382
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100383void
Michal Vaskod3bb12f2020-12-04 14:33:09 +0100384lyd_del_move_root(struct lyd_node **root, const struct lyd_node *to_del, const struct lys_module *mod)
385{
386 if (*root && (lyd_owner_module(*root) != mod)) {
387 /* there are no data of mod so this is simply the first top-level sibling */
388 mod = NULL;
389 }
390
391 if ((*root != to_del) || (*root)->parent) {
392 return;
393 }
394
395 *root = (*root)->next;
396 if (mod && *root && (lyd_owner_module(to_del) != lyd_owner_module(*root))) {
397 /* there are no more nodes from mod */
398 *root = lyd_first_sibling(*root);
399 }
400}
401
402void
Radek Krejci8df109d2021-04-23 12:19:08 +0200403ly_free_prefix_data(LY_VALUE_FORMAT format, void *prefix_data)
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100404{
405 struct ly_set *ns_list;
406 struct lysc_prefix *prefixes;
407 uint32_t i;
408 LY_ARRAY_COUNT_TYPE u;
409
410 if (!prefix_data) {
411 return;
412 }
413
414 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200415 case LY_VALUE_XML:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100416 ns_list = prefix_data;
417 for (i = 0; i < ns_list->count; ++i) {
418 free(((struct lyxml_ns *)ns_list->objs[i])->prefix);
419 free(((struct lyxml_ns *)ns_list->objs[i])->uri);
420 }
421 ly_set_free(ns_list, free);
422 break;
Radek Krejci8df109d2021-04-23 12:19:08 +0200423 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100424 prefixes = prefix_data;
425 LY_ARRAY_FOR(prefixes, u) {
426 free(prefixes[u].prefix);
427 }
428 LY_ARRAY_FREE(prefixes);
429 break;
Radek Krejci224d4b42021-04-23 13:54:59 +0200430 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +0200431 case LY_VALUE_SCHEMA:
432 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +0200433 case LY_VALUE_LYB:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100434 break;
435 }
436}
437
438LY_ERR
Radek Krejci8df109d2021-04-23 12:19:08 +0200439ly_dup_prefix_data(const struct ly_ctx *ctx, LY_VALUE_FORMAT format, const void *prefix_data,
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100440 void **prefix_data_p)
441{
442 LY_ERR ret = LY_SUCCESS;
443 struct lyxml_ns *ns;
444 struct lysc_prefix *prefixes = NULL, *orig_pref;
445 struct ly_set *ns_list, *orig_ns;
446 uint32_t i;
447 LY_ARRAY_COUNT_TYPE u;
448
449 assert(!*prefix_data_p);
450
451 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200452 case LY_VALUE_SCHEMA:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100453 *prefix_data_p = (void *)prefix_data;
454 break;
Radek Krejci8df109d2021-04-23 12:19:08 +0200455 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100456 /* copy all the value prefixes */
457 orig_pref = (struct lysc_prefix *)prefix_data;
458 LY_ARRAY_CREATE_GOTO(ctx, prefixes, LY_ARRAY_COUNT(orig_pref), ret, cleanup);
459 *prefix_data_p = prefixes;
460
461 LY_ARRAY_FOR(orig_pref, u) {
462 if (orig_pref[u].prefix) {
463 prefixes[u].prefix = strdup(orig_pref[u].prefix);
464 LY_CHECK_ERR_GOTO(!prefixes[u].prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup);
465 }
466 prefixes[u].mod = orig_pref[u].mod;
467 LY_ARRAY_INCREMENT(prefixes);
468 }
469 break;
Radek Krejci8df109d2021-04-23 12:19:08 +0200470 case LY_VALUE_XML:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100471 /* copy all the namespaces */
472 LY_CHECK_GOTO(ret = ly_set_new(&ns_list), cleanup);
473 *prefix_data_p = ns_list;
474
475 orig_ns = (struct ly_set *)prefix_data;
476 for (i = 0; i < orig_ns->count; ++i) {
477 ns = calloc(1, sizeof *ns);
478 LY_CHECK_ERR_GOTO(!ns, LOGMEM(ctx); ret = LY_EMEM, cleanup);
479 LY_CHECK_GOTO(ret = ly_set_add(ns_list, ns, 1, NULL), cleanup);
480
481 if (((struct lyxml_ns *)orig_ns->objs[i])->prefix) {
482 ns->prefix = strdup(((struct lyxml_ns *)orig_ns->objs[i])->prefix);
483 LY_CHECK_ERR_GOTO(!ns->prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup);
484 }
485 ns->uri = strdup(((struct lyxml_ns *)orig_ns->objs[i])->uri);
486 LY_CHECK_ERR_GOTO(!ns->uri, LOGMEM(ctx); ret = LY_EMEM, cleanup);
487 }
488 break;
Radek Krejci224d4b42021-04-23 13:54:59 +0200489 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +0200490 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +0200491 case LY_VALUE_LYB:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100492 assert(!prefix_data);
493 *prefix_data_p = NULL;
494 break;
495 }
496
497cleanup:
498 if (ret) {
499 ly_free_prefix_data(format, *prefix_data_p);
500 *prefix_data_p = NULL;
501 }
502 return ret;
503}
504
505LY_ERR
Radek Krejcif9943642021-04-26 10:18:21 +0200506ly_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 +0200507 const void *prefix_data, LY_VALUE_FORMAT *format_p, void **prefix_data_p)
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100508{
509 LY_ERR ret = LY_SUCCESS;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100510 const struct lys_module *mod;
Michal Vaskofc2cd072021-02-24 13:17:17 +0100511 const struct lyxml_ns *ns;
512 struct lyxml_ns *new_ns;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100513 struct ly_set *ns_list;
514 struct lysc_prefix *prefixes = NULL, *val_pref;
aPiecek83436bc2021-03-30 12:20:45 +0200515 const char *value_iter, *value_next, *value_end;
516 uint32_t substr_len;
517 ly_bool is_prefix;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100518
519 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200520 case LY_VALUE_SCHEMA:
Michal Vaskofc2cd072021-02-24 13:17:17 +0100521 /* copy all referenced modules as prefix - module pairs */
522 if (!*prefix_data_p) {
523 /* new prefix data */
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100524 LY_ARRAY_CREATE_GOTO(ctx, prefixes, 0, ret, cleanup);
Radek Krejci8df109d2021-04-23 12:19:08 +0200525 *format_p = LY_VALUE_SCHEMA_RESOLVED;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100526 *prefix_data_p = prefixes;
Michal Vaskofc2cd072021-02-24 13:17:17 +0100527 } else {
528 /* reuse prefix data */
Radek Krejci8df109d2021-04-23 12:19:08 +0200529 assert(*format_p == LY_VALUE_SCHEMA_RESOLVED);
Michal Vaskofc2cd072021-02-24 13:17:17 +0100530 prefixes = *prefix_data_p;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100531 }
532
533 /* add all used prefixes */
aPiecek83436bc2021-03-30 12:20:45 +0200534 value_end = value + value_len;
535 for (value_iter = value; value_iter; value_iter = value_next) {
536 substr_len = ly_value_prefix_next(value_iter, value_end, &is_prefix, &value_next);
537 if (is_prefix) {
538 /* we have a possible prefix. Do we already have the prefix? */
539 mod = ly_resolve_prefix(ctx, value_iter, substr_len, *format_p, *prefix_data_p);
540 if (!mod) {
541 mod = ly_resolve_prefix(ctx, value_iter, substr_len, format, prefix_data);
542 if (mod) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200543 assert(*format_p == LY_VALUE_SCHEMA_RESOLVED);
aPiecek83436bc2021-03-30 12:20:45 +0200544 /* store a new prefix - module pair */
545 LY_ARRAY_NEW_GOTO(ctx, prefixes, val_pref, ret, cleanup);
546 *prefix_data_p = prefixes;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100547
aPiecek83436bc2021-03-30 12:20:45 +0200548 val_pref->prefix = strndup(value_iter, substr_len);
549 LY_CHECK_ERR_GOTO(!val_pref->prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup);
550 val_pref->mod = mod;
551 } /* else it is not even defined */
552 } /* else the prefix is already present */
Michal Vaskofc2cd072021-02-24 13:17:17 +0100553 }
554 }
555 break;
Radek Krejci8df109d2021-04-23 12:19:08 +0200556 case LY_VALUE_XML:
Michal Vaskofc2cd072021-02-24 13:17:17 +0100557 /* copy all referenced namespaces as prefix - namespace pairs */
558 if (!*prefix_data_p) {
559 /* new prefix data */
560 LY_CHECK_GOTO(ret = ly_set_new(&ns_list), cleanup);
Radek Krejci8df109d2021-04-23 12:19:08 +0200561 *format_p = LY_VALUE_XML;
Michal Vaskofc2cd072021-02-24 13:17:17 +0100562 *prefix_data_p = ns_list;
563 } else {
564 /* reuse prefix data */
Radek Krejci8df109d2021-04-23 12:19:08 +0200565 assert(*format_p == LY_VALUE_XML);
Michal Vaskofc2cd072021-02-24 13:17:17 +0100566 ns_list = *prefix_data_p;
567 }
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100568
Michal Vaskofc2cd072021-02-24 13:17:17 +0100569 /* add all used prefixes */
aPiecek83436bc2021-03-30 12:20:45 +0200570 value_end = value + value_len;
571 for (value_iter = value; value_iter; value_iter = value_next) {
572 substr_len = ly_value_prefix_next(value_iter, value_end, &is_prefix, &value_next);
573 if (is_prefix) {
574 /* we have a possible prefix. Do we already have the prefix? */
575 ns = lyxml_ns_get(ns_list, value_iter, substr_len);
576 if (!ns) {
577 ns = lyxml_ns_get(prefix_data, value_iter, substr_len);
578 if (ns) {
579 /* store a new prefix - namespace pair */
580 new_ns = calloc(1, sizeof *new_ns);
581 LY_CHECK_ERR_GOTO(!new_ns, LOGMEM(ctx); ret = LY_EMEM, cleanup);
582 LY_CHECK_GOTO(ret = ly_set_add(ns_list, new_ns, 1, NULL), cleanup);
Michal Vaskofc2cd072021-02-24 13:17:17 +0100583
aPiecek83436bc2021-03-30 12:20:45 +0200584 new_ns->prefix = strndup(value_iter, substr_len);
585 LY_CHECK_ERR_GOTO(!new_ns->prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup);
586 new_ns->uri = strdup(ns->uri);
587 LY_CHECK_ERR_GOTO(!new_ns->uri, LOGMEM(ctx); ret = LY_EMEM, cleanup);
588 } /* else it is not even defined */
589 } /* else the prefix is already present */
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100590 }
591 }
592 break;
Radek Krejci224d4b42021-04-23 13:54:59 +0200593 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +0200594 case LY_VALUE_SCHEMA_RESOLVED:
595 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +0200596 case LY_VALUE_LYB:
Michal Vaskofc2cd072021-02-24 13:17:17 +0100597 if (!*prefix_data_p) {
598 /* new prefix data - simply copy all the prefix data */
599 *format_p = format;
600 LY_CHECK_GOTO(ret = ly_dup_prefix_data(ctx, format, prefix_data, prefix_data_p), cleanup);
601 } /* else reuse prefix data - the prefix data are always the same, nothing to do */
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100602 break;
603 }
604
605cleanup:
606 if (ret) {
607 ly_free_prefix_data(*format_p, *prefix_data_p);
608 *prefix_data_p = NULL;
609 }
610 return ret;
611}
Michal Vasko7ed1fcb2020-12-03 14:15:22 +0100612
613const char *
Radek Krejci8df109d2021-04-23 12:19:08 +0200614ly_format2str(LY_VALUE_FORMAT format)
Michal Vasko7ed1fcb2020-12-03 14:15:22 +0100615{
616 switch (format) {
Radek Krejci224d4b42021-04-23 13:54:59 +0200617 case LY_VALUE_CANON:
618 return "canonical";
Radek Krejci8df109d2021-04-23 12:19:08 +0200619 case LY_VALUE_SCHEMA:
Michal Vasko7ed1fcb2020-12-03 14:15:22 +0100620 return "schema imports";
Radek Krejci8df109d2021-04-23 12:19:08 +0200621 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko7ed1fcb2020-12-03 14:15:22 +0100622 return "schema stored mapping";
Radek Krejci8df109d2021-04-23 12:19:08 +0200623 case LY_VALUE_XML:
Michal Vasko7ed1fcb2020-12-03 14:15:22 +0100624 return "XML prefixes";
Radek Krejci8df109d2021-04-23 12:19:08 +0200625 case LY_VALUE_JSON:
Michal Vasko7ed1fcb2020-12-03 14:15:22 +0100626 return "JSON module names";
Radek Krejcif9943642021-04-26 10:18:21 +0200627 case LY_VALUE_LYB:
628 return "LYB prefixes";
Michal Vasko7ed1fcb2020-12-03 14:15:22 +0100629 default:
630 break;
631 }
632
633 return NULL;
634}