blob: b62146f84021f6e2d81846a12a486f7d939a4b3b [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"
Radek Krejciad97c5f2020-06-30 09:19:28 +020029#include "set.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020030#include "tree.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020031#include "tree_data.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020032#include "tree_data_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020033#include "tree_schema.h"
34
Michal Vaskoa6669ba2020-08-06 16:14:26 +020035struct lyd_node *
36lys_getnext_data(const struct lyd_node *last, const struct lyd_node *sibling, const struct lysc_node **slast,
Radek Krejci0f969882020-08-21 16:56:47 +020037 const struct lysc_node *parent, const struct lysc_module *module)
Michal Vaskoa6669ba2020-08-06 16:14:26 +020038{
39 const struct lysc_node *siter = NULL;
40 struct lyd_node *match = NULL;
41
42 assert(parent || module);
43 assert(!last || (slast && *slast));
44
45 if (slast) {
46 siter = *slast;
47 }
48
49 if (last && last->next && (last->next->schema == siter)) {
50 /* return next data instance */
51 return last->next;
52 }
53
54 /* find next schema node data instance */
55 while ((siter = lys_getnext(siter, parent, module, 0))) {
56 if (!lyd_find_sibling_val(sibling, siter, NULL, 0, &match)) {
57 break;
58 }
59 }
60
61 if (slast) {
62 *slast = siter;
63 }
64 return match;
65}
66
Radek Krejcie7b95092019-05-15 11:03:07 +020067struct lyd_node **
68lyd_node_children_p(struct lyd_node *node)
69{
70 assert(node);
Michal Vasko52927e22020-03-16 17:26:14 +010071
72 if (!node->schema) {
73 return &((struct lyd_node_opaq *)node)->child;
74 } else {
75 switch (node->schema->nodetype) {
76 case LYS_CONTAINER:
77 case LYS_LIST:
Michal Vasko1bf09392020-03-27 12:38:10 +010078 case LYS_RPC:
Michal Vasko52927e22020-03-16 17:26:14 +010079 case LYS_ACTION:
80 case LYS_NOTIF:
81 return &((struct lyd_node_inner *)node)->child;
82 default:
83 return NULL;
84 }
Radek Krejcie7b95092019-05-15 11:03:07 +020085 }
86}
87
Radek Krejcidae0ee82020-05-06 16:53:24 +020088API struct lyd_node *
Radek Krejcia1c1e542020-09-29 16:06:52 +020089lyd_child_no_keys(const struct lyd_node *node)
90{
91 struct lyd_node **children;
92
93 if (!node) {
94 return NULL;
95 }
96
97 if (!node->schema) {
98 /* opaq node */
Michal Vasko9e685082021-01-29 14:49:09 +010099 return ((struct lyd_node_opaq *)node)->child;
Radek Krejcia1c1e542020-09-29 16:06:52 +0200100 }
101
102 children = lyd_node_children_p((struct lyd_node *)node);
103 if (children) {
104 struct lyd_node *child = *children;
105 while (child && child->schema && (child->schema->flags & LYS_KEY)) {
106 child = child->next;
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200107 }
108 return child;
Radek Krejcie7b95092019-05-15 11:03:07 +0200109 } else {
110 return NULL;
111 }
112}
Michal Vasko9b368d32020-02-14 13:53:31 +0100113
Michal Vaskoc193ce92020-03-06 11:04:48 +0100114API const struct lys_module *
115lyd_owner_module(const struct lyd_node *node)
Michal Vasko9b368d32020-02-14 13:53:31 +0100116{
117 const struct lysc_node *schema;
Michal Vaskod5cfa6e2020-11-23 16:56:08 +0100118 const struct lyd_node_opaq *opaq;
Michal Vasko9b368d32020-02-14 13:53:31 +0100119
Michal Vaskod5cfa6e2020-11-23 16:56:08 +0100120 if (!node) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100121 return NULL;
122 }
Michal Vasko9b368d32020-02-14 13:53:31 +0100123
Michal Vaskod5cfa6e2020-11-23 16:56:08 +0100124 if (!node->schema) {
125 opaq = (struct lyd_node_opaq *)node;
126 switch (opaq->format) {
127 case LY_PREF_XML:
128 return ly_ctx_get_module_implemented_ns(LYD_CTX(node), opaq->name.module_ns);
129 case LY_PREF_JSON:
130 return ly_ctx_get_module_implemented(LYD_CTX(node), opaq->name.module_name);
131 default:
132 return NULL;
133 }
134 }
135
Radek Krejci1e008d22020-08-17 11:37:37 +0200136 for (schema = node->schema; schema->parent; schema = schema->parent) {}
Michal Vasko9b368d32020-02-14 13:53:31 +0100137 return schema->module;
138}
Michal Vaskob1b5c262020-03-05 14:29:47 +0100139
140const struct lys_module *
Michal Vasko26e80012020-07-08 10:55:46 +0200141lyd_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 +0200142 struct lyd_node **first)
Michal Vaskob1b5c262020-03-05 14:29:47 +0100143{
144 struct lyd_node *iter;
145 const struct lys_module *mod;
146
147 /* get the next module */
Michal Vasko26e80012020-07-08 10:55:46 +0200148 if (module) {
149 if (*i) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100150 mod = NULL;
Michal Vasko26e80012020-07-08 10:55:46 +0200151 } else {
152 mod = module;
153 ++(*i);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100154 }
155 } else {
156 do {
157 mod = ly_ctx_get_module_iter(ctx, i);
158 } while (mod && !mod->implemented);
159 }
160
161 /* find its data */
162 *first = NULL;
163 if (mod) {
164 LY_LIST_FOR(tree, iter) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100165 if (lyd_owner_module(iter) == mod) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100166 *first = iter;
167 break;
168 }
169 }
170 }
171
172 return mod;
173}
174
175const struct lys_module *
176lyd_data_next_module(struct lyd_node **next, struct lyd_node **first)
177{
178 const struct lys_module *mod;
179
180 if (!*next) {
181 /* all data traversed */
182 *first = NULL;
183 return NULL;
184 }
185
186 *first = *next;
187
188 /* prepare next */
Michal Vaskoc193ce92020-03-06 11:04:48 +0100189 mod = lyd_owner_module(*next);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100190 LY_LIST_FOR(*next, *next) {
Michal Vaskoc193ce92020-03-06 11:04:48 +0100191 if (lyd_owner_module(*next) != mod) {
Michal Vaskob1b5c262020-03-05 14:29:47 +0100192 break;
193 }
194 }
195
196 return mod;
197}
Michal Vasko9f96a052020-03-10 09:41:45 +0100198
199LY_ERR
200lyd_parse_check_keys(struct lyd_node *node)
201{
202 const struct lysc_node *skey = NULL;
203 const struct lyd_node *key;
204
205 assert(node->schema->nodetype == LYS_LIST);
206
Radek Krejcia1c1e542020-09-29 16:06:52 +0200207 key = lyd_child(node);
Michal Vasko9f96a052020-03-10 09:41:45 +0100208 while ((skey = lys_getnext(skey, node->schema, NULL, 0)) && (skey->flags & LYS_KEY)) {
209 if (!key || (key->schema != skey)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100210 LOGVAL(LYD_CTX(node), LY_VCODE_NOKEY, skey->name);
Michal Vasko9f96a052020-03-10 09:41:45 +0100211 return LY_EVALID;
212 }
213
214 key = key->next;
215 }
216
217 return LY_SUCCESS;
218}
Michal Vasko60ea6352020-06-29 13:39:39 +0200219
220void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200221lyd_parse_set_data_flags(struct lyd_node *node, struct ly_set *when_check, struct lyd_meta **meta, uint32_t options)
Michal Vasko60ea6352020-06-29 13:39:39 +0200222{
223 struct lyd_meta *meta2, *prev_meta = NULL;
224
Michal Vaskoa5705e52020-12-09 18:15:14 +0100225 if (lysc_has_when(node->schema)) {
Michal Vasko0f3377d2020-11-09 20:56:11 +0100226 if (!(options & LYD_PARSE_ONLY)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200227 /* remember we need to evaluate this node's when */
Radek Krejci3d92e442020-10-12 12:48:13 +0200228 LY_CHECK_RET(ly_set_add(when_check, node, 1, NULL), );
Michal Vasko60ea6352020-06-29 13:39:39 +0200229 }
230 }
231
Michal Vasko60ea6352020-06-29 13:39:39 +0200232 LY_LIST_FOR(*meta, meta2) {
Michal Vasko69730152020-10-09 16:30:07 +0200233 if (!strcmp(meta2->name, "default") && !strcmp(meta2->annotation->module->name, "ietf-netconf-with-defaults") &&
234 meta2->value.boolean) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200235 /* node is default according to the metadata */
236 node->flags |= LYD_DEFAULT;
237
238 /* delete the metadata */
239 if (prev_meta) {
240 prev_meta->next = meta2->next;
241 } else {
242 *meta = (*meta)->next;
243 }
Michal Vasko3a41dff2020-07-15 14:30:28 +0200244 lyd_free_meta_single(meta2);
Michal Vasko60ea6352020-06-29 13:39:39 +0200245 break;
246 }
247
248 prev_meta = meta2;
249 }
250}
251
Michal Vaskoc0004272020-08-06 08:32:34 +0200252API LY_ERR
Michal Vasko61551fa2020-07-09 15:45:45 +0200253lyd_any_copy_value(struct lyd_node *trg, const union lyd_any_value *value, LYD_ANYDATA_VALUETYPE value_type)
254{
255 struct lyd_node_any *t;
Michal Vasko61551fa2020-07-09 15:45:45 +0200256
257 assert(trg->schema->nodetype & LYS_ANYDATA);
258
259 t = (struct lyd_node_any *)trg;
260
261 /* free trg */
262 switch (t->value_type) {
263 case LYD_ANYDATA_DATATREE:
264 lyd_free_all(t->value.tree);
265 break;
266 case LYD_ANYDATA_STRING:
267 case LYD_ANYDATA_XML:
268 case LYD_ANYDATA_JSON:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200269 FREE_STRING(LYD_CTX(trg), t->value.str);
Michal Vasko61551fa2020-07-09 15:45:45 +0200270 break;
271 case LYD_ANYDATA_LYB:
272 free(t->value.mem);
273 break;
274 }
275 t->value.str = NULL;
276
277 if (!value) {
278 /* only free value in this case */
279 return LY_SUCCESS;
280 }
281
282 /* copy src */
283 t->value_type = value_type;
284 switch (value_type) {
285 case LYD_ANYDATA_DATATREE:
286 if (value->tree) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200287 LY_CHECK_RET(lyd_dup_siblings(value->tree, NULL, LYD_DUP_RECURSIVE, &t->value.tree));
Michal Vasko61551fa2020-07-09 15:45:45 +0200288 }
289 break;
290 case LYD_ANYDATA_STRING:
291 case LYD_ANYDATA_XML:
292 case LYD_ANYDATA_JSON:
293 if (value->str) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200294 LY_CHECK_RET(lydict_insert(LYD_CTX(trg), value->str, 0, &t->value.str));
Michal Vasko61551fa2020-07-09 15:45:45 +0200295 }
296 break;
297 case LYD_ANYDATA_LYB:
298 if (value->mem) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200299 int len = lyd_lyb_data_length(value->mem);
Radek Krejci82fa8d42020-07-11 22:00:59 +0200300 LY_CHECK_RET(len == -1, LY_EINVAL);
Michal Vasko61551fa2020-07-09 15:45:45 +0200301 t->value.mem = malloc(len);
Michal Vaskob7be7a82020-08-20 09:09:04 +0200302 LY_CHECK_ERR_RET(!t->value.mem, LOGMEM(LYD_CTX(trg)), LY_EMEM);
Michal Vasko61551fa2020-07-09 15:45:45 +0200303 memcpy(t->value.mem, value->mem, len);
304 }
305 break;
306 }
307
308 return LY_SUCCESS;
309}
310
Michal Vasko60ea6352020-06-29 13:39:39 +0200311LYB_HASH
312lyb_hash(struct lysc_node *sibling, uint8_t collision_id)
313{
314 const struct lys_module *mod;
Michal Vasko60ea6352020-06-29 13:39:39 +0200315 uint32_t full_hash;
316 LYB_HASH hash;
317
318 if ((collision_id < LYS_NODE_HASH_COUNT) && sibling->hash[collision_id]) {
319 return sibling->hash[collision_id];
320 }
321
322 mod = sibling->module;
323
324 full_hash = dict_hash_multi(0, mod->name, strlen(mod->name));
325 full_hash = dict_hash_multi(full_hash, sibling->name, strlen(sibling->name));
326 if (collision_id) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200327 size_t ext_len;
328
Michal Vasko60ea6352020-06-29 13:39:39 +0200329 if (collision_id > strlen(mod->name)) {
330 /* fine, we will not hash more bytes, just use more bits from the hash than previously */
331 ext_len = strlen(mod->name);
332 } else {
333 /* use one more byte from the module name than before */
334 ext_len = collision_id;
335 }
336 full_hash = dict_hash_multi(full_hash, mod->name, ext_len);
337 }
338 full_hash = dict_hash_multi(full_hash, NULL, 0);
339
340 /* use the shortened hash */
341 hash = full_hash & (LYB_HASH_MASK >> collision_id);
342 /* add colision identificator */
343 hash |= LYB_HASH_COLLISION_ID >> collision_id;
344
345 /* save this hash */
346 if (collision_id < LYS_NODE_HASH_COUNT) {
347 sibling->hash[collision_id] = hash;
348 }
349
350 return hash;
351}
352
Radek Krejci857189e2020-09-01 13:26:36 +0200353ly_bool
Michal Vasko60ea6352020-06-29 13:39:39 +0200354lyb_has_schema_model(const struct lysc_node *sibling, const struct lys_module **models)
355{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200356 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200357
358 LY_ARRAY_FOR(models, u) {
359 if (sibling->module == models[u]) {
360 return 1;
361 }
362 }
363
364 return 0;
365}
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100366
367void
Michal Vaskod3bb12f2020-12-04 14:33:09 +0100368lyd_del_move_root(struct lyd_node **root, const struct lyd_node *to_del, const struct lys_module *mod)
369{
370 if (*root && (lyd_owner_module(*root) != mod)) {
371 /* there are no data of mod so this is simply the first top-level sibling */
372 mod = NULL;
373 }
374
375 if ((*root != to_del) || (*root)->parent) {
376 return;
377 }
378
379 *root = (*root)->next;
380 if (mod && *root && (lyd_owner_module(to_del) != lyd_owner_module(*root))) {
381 /* there are no more nodes from mod */
382 *root = lyd_first_sibling(*root);
383 }
384}
385
386void
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100387ly_free_prefix_data(LY_PREFIX_FORMAT format, void *prefix_data)
388{
389 struct ly_set *ns_list;
390 struct lysc_prefix *prefixes;
391 uint32_t i;
392 LY_ARRAY_COUNT_TYPE u;
393
394 if (!prefix_data) {
395 return;
396 }
397
398 switch (format) {
399 case LY_PREF_XML:
400 ns_list = prefix_data;
401 for (i = 0; i < ns_list->count; ++i) {
402 free(((struct lyxml_ns *)ns_list->objs[i])->prefix);
403 free(((struct lyxml_ns *)ns_list->objs[i])->uri);
404 }
405 ly_set_free(ns_list, free);
406 break;
407 case LY_PREF_SCHEMA_RESOLVED:
408 prefixes = prefix_data;
409 LY_ARRAY_FOR(prefixes, u) {
410 free(prefixes[u].prefix);
411 }
412 LY_ARRAY_FREE(prefixes);
413 break;
414 case LY_PREF_SCHEMA:
415 case LY_PREF_JSON:
416 break;
417 }
418}
419
420LY_ERR
421ly_dup_prefix_data(const struct ly_ctx *ctx, LY_PREFIX_FORMAT format, const void *prefix_data,
422 void **prefix_data_p)
423{
424 LY_ERR ret = LY_SUCCESS;
425 struct lyxml_ns *ns;
426 struct lysc_prefix *prefixes = NULL, *orig_pref;
427 struct ly_set *ns_list, *orig_ns;
428 uint32_t i;
429 LY_ARRAY_COUNT_TYPE u;
430
431 assert(!*prefix_data_p);
432
433 switch (format) {
434 case LY_PREF_SCHEMA:
435 *prefix_data_p = (void *)prefix_data;
436 break;
437 case LY_PREF_SCHEMA_RESOLVED:
438 /* copy all the value prefixes */
439 orig_pref = (struct lysc_prefix *)prefix_data;
440 LY_ARRAY_CREATE_GOTO(ctx, prefixes, LY_ARRAY_COUNT(orig_pref), ret, cleanup);
441 *prefix_data_p = prefixes;
442
443 LY_ARRAY_FOR(orig_pref, u) {
444 if (orig_pref[u].prefix) {
445 prefixes[u].prefix = strdup(orig_pref[u].prefix);
446 LY_CHECK_ERR_GOTO(!prefixes[u].prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup);
447 }
448 prefixes[u].mod = orig_pref[u].mod;
449 LY_ARRAY_INCREMENT(prefixes);
450 }
451 break;
452 case LY_PREF_XML:
453 /* copy all the namespaces */
454 LY_CHECK_GOTO(ret = ly_set_new(&ns_list), cleanup);
455 *prefix_data_p = ns_list;
456
457 orig_ns = (struct ly_set *)prefix_data;
458 for (i = 0; i < orig_ns->count; ++i) {
459 ns = calloc(1, sizeof *ns);
460 LY_CHECK_ERR_GOTO(!ns, LOGMEM(ctx); ret = LY_EMEM, cleanup);
461 LY_CHECK_GOTO(ret = ly_set_add(ns_list, ns, 1, NULL), cleanup);
462
463 if (((struct lyxml_ns *)orig_ns->objs[i])->prefix) {
464 ns->prefix = strdup(((struct lyxml_ns *)orig_ns->objs[i])->prefix);
465 LY_CHECK_ERR_GOTO(!ns->prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup);
466 }
467 ns->uri = strdup(((struct lyxml_ns *)orig_ns->objs[i])->uri);
468 LY_CHECK_ERR_GOTO(!ns->uri, LOGMEM(ctx); ret = LY_EMEM, cleanup);
469 }
470 break;
471 case LY_PREF_JSON:
472 assert(!prefix_data);
473 *prefix_data_p = NULL;
474 break;
475 }
476
477cleanup:
478 if (ret) {
479 ly_free_prefix_data(format, *prefix_data_p);
480 *prefix_data_p = NULL;
481 }
482 return ret;
483}
484
485LY_ERR
486ly_store_prefix_data(const struct ly_ctx *ctx, const char *value, size_t value_len, LY_PREFIX_FORMAT format,
487 void *prefix_data, LY_PREFIX_FORMAT *format_p, void **prefix_data_p)
488{
489 LY_ERR ret = LY_SUCCESS;
490 const char *start, *stop;
491 const struct lys_module *mod;
492 struct lyxml_ns *ns;
493 struct ly_set *ns_list;
494 struct lysc_prefix *prefixes = NULL, *val_pref;
495
496 switch (format) {
497 case LY_PREF_SCHEMA:
498 case LY_PREF_XML:
499 /* copy all referenced modules... */
500 if (format == LY_PREF_XML) {
501 /* ...as prefix - namespace pairs */
502 LY_CHECK_GOTO(ret = ly_set_new(&ns_list), cleanup);
503 *format_p = LY_PREF_XML;
504 *prefix_data_p = ns_list;
505 } else {
506 /* ...as prefix - module pairs */
507 assert(format == LY_PREF_SCHEMA);
508 LY_ARRAY_CREATE_GOTO(ctx, prefixes, 0, ret, cleanup);
509 *format_p = LY_PREF_SCHEMA_RESOLVED;
510 *prefix_data_p = prefixes;
511 }
512
513 /* add all used prefixes */
514 for (stop = start = value; (size_t)(stop - value) < value_len; start = stop) {
515 size_t bytes;
516 uint32_t c;
517
518 ly_getutf8(&stop, &c, &bytes);
519 if (is_xmlqnamestartchar(c)) {
520 for (ly_getutf8(&stop, &c, &bytes);
521 is_xmlqnamechar(c) && (size_t)(stop - value) < value_len;
522 ly_getutf8(&stop, &c, &bytes)) {}
523 stop = stop - bytes;
524 if (*stop == ':') {
525 /* we have a possible prefix */
526 size_t len = stop - start;
527
528 /* do we already have the prefix? */
529 mod = ly_type_store_resolve_prefix(ctx, start, len, *format_p, *prefix_data_p);
530 if (!mod) {
531 mod = ly_type_store_resolve_prefix(ctx, start, len, format, prefix_data);
532 if (mod) {
533 if (*format_p == LY_PREF_XML) {
534 /* store a new prefix - namespace pair */
535 ns = calloc(1, sizeof *ns);
536 LY_CHECK_ERR_GOTO(!ns, LOGMEM(ctx); ret = LY_EMEM, cleanup);
537 LY_CHECK_GOTO(ret = ly_set_add(ns_list, ns, 1, NULL), cleanup);
538
539 ns->prefix = strndup(start, len);
540 LY_CHECK_ERR_GOTO(!ns->prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup);
541 ns->uri = strdup(mod->ns);
542 LY_CHECK_ERR_GOTO(!ns->uri, LOGMEM(ctx); ret = LY_EMEM, cleanup);
543 } else {
544 assert(*format_p == LY_PREF_SCHEMA_RESOLVED);
545 /* store a new prefix - module pair */
546 LY_ARRAY_NEW_GOTO(ctx, prefixes, val_pref, ret, cleanup);
547 *prefix_data_p = prefixes;
548
549 val_pref->prefix = strndup(start, len);
550 LY_CHECK_ERR_GOTO(!val_pref->prefix, LOGMEM(ctx); ret = LY_EMEM, cleanup);
551 val_pref->mod = mod;
552 }
553 } /* else it is not even defined */
554 } /* else the prefix is already present */
555 }
556 stop = stop + bytes;
557 }
558 }
559 break;
560 case LY_PREF_SCHEMA_RESOLVED:
561 case LY_PREF_JSON:
562 /* simply copy all the prefix data */
563 *format_p = format;
564 LY_CHECK_GOTO(ret = ly_dup_prefix_data(ctx, format, prefix_data, prefix_data_p), cleanup);
565 break;
566 }
567
568cleanup:
569 if (ret) {
570 ly_free_prefix_data(*format_p, *prefix_data_p);
571 *prefix_data_p = NULL;
572 }
573 return ret;
574}
Michal Vasko7ed1fcb2020-12-03 14:15:22 +0100575
576const char *
577ly_format2str(LY_PREFIX_FORMAT format)
578{
579 switch (format) {
580 case LY_PREF_SCHEMA:
581 return "schema imports";
582 case LY_PREF_SCHEMA_RESOLVED:
583 return "schema stored mapping";
584 case LY_PREF_XML:
585 return "XML prefixes";
586 case LY_PREF_JSON:
587 return "JSON module names";
588 default:
589 break;
590 }
591
592 return NULL;
593}