libyang  6.4.41
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
union.c
Go to the documentation of this file.
1 
16 #define _GNU_SOURCE /* strdup */
17 
18 #include "plugins_types.h"
19 
20 #include <assert.h>
21 #include <inttypes.h>
22 #include <stdlib.h>
23 #include <string.h>
24 
25 #include "compat.h"
26 #include "dict.h"
27 #include "ly_array.h"
28 #include "ly_common.h"
29 #include "lyb.h"
30 #include "plugins_internal.h"
31 
44 static void lyplg_type_free_union(const struct ly_ctx *ctx, struct lyd_value *value);
45 
49 #define LYPLG_UNION_TYPE_IDX_SIZE 1
50 
60 static LY_ERR
61 union_validate_value_lyb(const void *lyb_data, uint64_t lyb_data_size_bits, const struct lysc_type_union *type_u,
62  struct ly_err_item **err)
63 {
64  uint32_t type_idx = 0;
65 
66  /* basic validation */
67  if (lyb_data_size_bits < LYPLG_UNION_TYPE_IDX_SIZE * 8) {
68  return ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB union value size %" PRIu64
69  " b (expected at least %" PRIu8 " b).", lyb_data_size_bits, LYPLG_UNION_TYPE_IDX_SIZE * 8);
70  }
71 
72  /* get index in correct byte order */
73  memcpy(&type_idx, lyb_data, LYPLG_UNION_TYPE_IDX_SIZE);
74  type_idx = le32toh(type_idx);
75  if (type_idx >= LYA_COUNT(type_u->types)) {
76  return ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL,
77  "Invalid LYB union type index %" PRIu32 " (type count %" LYA_PRI_COUNT_T ").",
78  type_idx, LYA_COUNT(type_u->types));
79  }
80 
81  return LY_SUCCESS;
82 }
83 
94 static void
95 union_parse_value_lyb(const void *lyb_data, uint64_t lyb_data_size_bits, uint32_t *type_idx, const void **lyb_value,
96  uint64_t *lyb_value_size_bits)
97 {
98  uint32_t num = 0;
99 
100  assert(lyb_data);
101 
102  if (type_idx) {
103  memcpy(&num, lyb_data, LYPLG_UNION_TYPE_IDX_SIZE);
104  num = le32toh(num);
105 
106  *type_idx = num;
107  }
108 
109  if (lyb_value && lyb_value_size_bits && lyb_data_size_bits) {
110  /* get lyb_value and its size */
111  if (lyb_data_size_bits == LYPLG_UNION_TYPE_IDX_SIZE * 8) {
112  *lyb_value_size_bits = 0;
113  *lyb_value = "";
114  } else {
115  *lyb_value_size_bits = lyb_data_size_bits - LYPLG_UNION_TYPE_IDX_SIZE * 8;
116  *lyb_value = (char *)lyb_data + LYPLG_UNION_TYPE_IDX_SIZE;
117  }
118  }
119 }
120 
133 static LY_ERR
134 union_create_value_lyb(const struct ly_ctx *ctx, uint32_t type_idx, const struct lyd_value *value, void **lyb_value,
135  uint64_t *lyb_size_bits, struct ly_err_item **err)
136 {
137  LY_ERR rc = LY_SUCCESS;
138  void *lyb = NULL;
139  const void *pval = NULL;
140  uint64_t pval_size_bits, lyb_bits = 0;
141  uint32_t num;
142  ly_bool dynamic = 0;
143 
144  *err = NULL;
145 
146  /* print the value in LYB format */
147  pval = LYSC_GET_TYPE_PLG(value->realtype->plugin_ref)->print(ctx, value, LY_VALUE_LYB, NULL, &dynamic, &pval_size_bits);
148  if (!pval) {
149  rc = ly_err_new_no_mem(err);
150  goto cleanup;
151  }
152 
153  /* create the binary data of the pinned member type index followed by its binary value
154  * (the size may not be an exact number of bytes, e.g. for integers) */
155  lyb_bits = LYPLG_UNION_TYPE_IDX_SIZE * 8 + pval_size_bits;
156  lyb = malloc(LYPLG_BITS2BYTES(lyb_bits));
157  if (!lyb) {
158  lyb_bits = 0;
159  rc = ly_err_new_no_mem(err);
160  goto cleanup;
161  }
162 
163  num = htole32(type_idx);
164  memcpy(lyb, &num, LYPLG_UNION_TYPE_IDX_SIZE);
165  memcpy((char *)lyb + LYPLG_UNION_TYPE_IDX_SIZE, pval, LYPLG_BITS2BYTES(pval_size_bits));
166 
167 cleanup:
168  if (dynamic) {
169  free((void *)pval);
170  }
171  *lyb_value = lyb;
172  if (lyb_size_bits) {
173  *lyb_size_bits = lyb_bits;
174  }
175  return rc;
176 }
177 
189 static LY_ERR
190 union_update_lref_err(struct ly_err_item *err, const struct lysc_type *type, const void *value, uint64_t value_size_bits)
191 {
192  const struct lysc_type_leafref *lref;
193  char *valstr = NULL;
194  int r;
195 
196  if (!err || (type->basetype != LY_TYPE_LEAFREF)) {
197  /* nothing to do */
198  return LY_SUCCESS;
199  }
200 
201  lref = (const struct lysc_type_leafref *)type;
202 
203  /* update error-app-tag */
204  free(err->apptag);
205  err->apptag = strdup("instance-required");
206  LY_CHECK_ERR_RET(!err->apptag, LOGMEM(NULL), LY_EMEM);
207 
208  valstr = strndup((const char *)value, value_size_bits / 8);
209  LY_CHECK_ERR_RET(!valstr, LOGMEM(NULL), LY_EMEM);
210 
211  /* update error-message */
212  free(err->msg);
213  r = asprintf(&err->msg, LY_ERRMSG_NOLREF_VAL, valstr, lyxp_get_expr(lref->path));
214  free(valstr);
215  LY_CHECK_ERR_RET(r == -1, LOGMEM(NULL), LY_EMEM);
216 
217  return LY_SUCCESS;
218 }
219 
237 static LY_ERR
238 union_subvalue_get_input_value(const struct ly_ctx *ctx, const struct lysc_type_union *type_u, uint32_t type_idx,
239  const struct lyd_value_union *subvalue, const void **value, uint64_t *value_size_bits, ly_bool *dynamic,
240  LY_VALUE_FORMAT *format, void **prefix_data, struct ly_err_item **err)
241 {
242  LY_ERR rc = LY_SUCCESS;
243  struct lyd_value storage = {0};
244  uint32_t ti;
245  struct lyplg_type *type_plg;
246 
247  *value = NULL;
248  *value_size_bits = 0;
249  *dynamic = 0;
250  *format = 0;
251  *prefix_data = NULL;
252  *err = NULL;
253 
254  if (subvalue->format == LY_VALUE_LYB) {
255  union_parse_value_lyb(subvalue->original, subvalue->orig_size_bits, &ti, value, value_size_bits);
256  if (ti != type_idx) {
257  /* value of another type, store the value */
258  type_plg = LYSC_GET_TYPE_PLG(type_u->types[ti]->plugin_ref);
259  rc = type_plg->store(ctx, type_u->types[ti], *value, *value_size_bits, LYPLG_TYPE_STORE_ONLY,
260  subvalue->format, subvalue->prefix_data, subvalue->hints, subvalue->ctx_node, &storage, NULL, err);
261  if (rc && (rc != LY_EINCOMPLETE)) {
262  /* if this is a leafref, lets make sure we propagate the appropriate error, and not a type validation failure */
263  union_update_lref_err(*err, type_u->types[ti], *value, *value_size_bits);
264  goto cleanup;
265  }
266 
267  /* print the value in canonical format */
268  assert(storage.realtype);
269  *value = LYSC_GET_TYPE_PLG(storage.realtype->plugin_ref)->print(ctx, &storage, LY_VALUE_CANON, NULL, dynamic,
270  value_size_bits);
271  assert(!(*value_size_bits % 8));
272 
273  /* free storage, but we need the value, which may be stored there */
274  if (!*dynamic) {
275  *value = strndup(*value, *value_size_bits / 8);
276  *dynamic = 1;
277  }
278  type_plg->free(ctx, &storage);
279 
280  *format = LY_VALUE_CANON;
281  *prefix_data = NULL;
282  } else {
283  *format = subvalue->format;
284  *prefix_data = subvalue->prefix_data;
285  }
286  } else {
287  *value = subvalue->original;
288  *value_size_bits = subvalue->orig_size_bits;
289  *format = subvalue->format;
290  *prefix_data = subvalue->prefix_data;
291  }
292 
293 cleanup:
294  return rc;
295 }
296 
316 static LY_ERR
317 union_store_type(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, uint64_t value_size_bits,
318  uint32_t options, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, const struct lysc_node *ctx_snode,
319  struct lyd_value *storage, struct lys_glob_unres *unres, struct ly_err_item **err)
320 {
321  LY_ERR rc = LY_SUCCESS;
322  struct lyplg_type *type_plg;
323  ly_bool dynamic = 0;
324 
325  *err = NULL;
326 
327  type_plg = LYSC_GET_TYPE_PLG(type->plugin_ref);
328 
329  /* do not free the value (yet) */
330  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
331  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
332  dynamic = 1;
333  }
334 
335  rc = type_plg->store(ctx, type, value, value_size_bits, options, format, prefix_data, hints, ctx_snode, storage,
336  unres, err);
337 
338  if (rc && (rc != LY_EINCOMPLETE)) {
339  /* if this is a leafref, lets make sure we propagate the appropriate error, and not a type validation failure */
340  union_update_lref_err(*err, type, value, value_size_bits);
341 
342  /* clear any leftover/freed garbage */
343  memset(storage, 0, sizeof *storage);
344  }
345  if (dynamic) {
346  free((void *)value);
347  }
348  return rc;
349 }
350 
369 static LY_ERR
370 union_read_store_type(const struct ly_ctx *ctx, const struct lysc_type_union *type_u, uint32_t type_idx,
371  struct lyd_value_union *subvalue, uint32_t options, const struct lyd_node *ctx_node,
372  const struct lyd_node *tree, const struct lyd_val_unres *val_unres, struct lys_glob_unres *unres,
373  struct ly_err_item **err)
374 {
375  LY_ERR rc = LY_SUCCESS, r;
376  const void *value;
377  uint64_t value_size_bits;
378  ly_bool dynamic;
379  LY_VALUE_FORMAT format;
380  void *prefix_data;
381  struct lyplg_type *type_plg;
382 
383  /* get the input value in a suitable format from the original value */
384  r = union_subvalue_get_input_value(ctx, type_u, type_idx, subvalue, &value, &value_size_bits, &dynamic, &format,
385  &prefix_data, err);
386  if (r && (r != LY_EINCOMPLETE)) {
387  rc = r;
388  goto cleanup;
389  }
390 
391  if (dynamic) {
392  options |= LYPLG_TYPE_STORE_DYNAMIC;
393  } else {
394  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
395  }
396 
397  /* store the value as the specific type */
398  rc = union_store_type(ctx, type_u->types[type_idx], value, value_size_bits, options, format, prefix_data,
399  subvalue->hints, subvalue->ctx_node, &subvalue->value, unres, err);
400  if (rc && (rc != LY_EINCOMPLETE)) {
401  goto cleanup;
402  }
403 
404  /* validate the value */
405  type_plg = LYSC_GET_TYPE_PLG(type_u->types[type_idx]->plugin_ref);
406  if (tree && type_plg->validate_tree) {
407  rc = type_plg->validate_tree(ctx, type_u->types[type_idx], ctx_node, tree, val_unres, &subvalue->value, err);
408  if (rc) {
409  /* validate failed, we need to free the stored value */
410  type_plg->free(ctx, &subvalue->value);
411  memset(&subvalue->value, 0, sizeof subvalue->value);
412  goto cleanup;
413  }
414  }
415 
416 cleanup:
417  return rc;
418 }
419 
435 static LY_ERR
436 union_find_type(const struct ly_ctx *ctx, struct lysc_type_union *type_u, struct lyd_value_union *subvalue,
437  uint32_t options, const struct lyd_node *ctx_node, const struct lyd_node *tree, const struct lyd_val_unres *val_unres,
438  uint32_t *type_idx, struct lys_glob_unres *unres, struct ly_err_item **err)
439 {
440  LY_ERR rc = LY_SUCCESS, r;
441  LYA_COUNT_T u;
442  struct ly_err_item **errs = NULL, *e;
443  uint32_t *prev_lo, temp_lo = 0;
444  char *msg = NULL, *err_app_tag = NULL;
445  uint32_t msg_size = 0, msg_len = 0;
446  ly_bool use_err_app_tag = 0;
447  struct lyplg_type *type_plg;
448 
449  *err = NULL;
450 
451  /* alloc errors */
452  errs = calloc(LYA_COUNT(type_u->types), sizeof *errs);
453  LY_CHECK_RET(!errs, LY_EMEM);
454 
455  /* turn logging temporarily off */
456  prev_lo = ly_temp_log_options(&temp_lo);
457 
458  /* use the first usable subtype to store the value */
459  LYA_FOR(type_u->types, u) {
460  rc = union_read_store_type(ctx, type_u, u, subvalue, options, ctx_node, tree, val_unres, unres, &e);
461  if (!rc || (rc == LY_EINCOMPLETE)) {
462  /* success */
463  break;
464  }
465 
466  errs[u] = e;
467  }
468 
469  if (u == LYA_COUNT(type_u->types)) {
470  /* create the full error */
471  if (subvalue->format == LY_VALUE_LYB) {
472  r = ly_append_str(&msg, &msg_size, &msg_len, "Invalid LYB union value - no matching subtype found:\n");
473  } else {
474  r = ly_append_str(&msg, &msg_size, &msg_len, "Invalid union value \"%.*s\" - no matching subtype found:\n",
475  (int)subvalue->orig_size_bits / 8, (char *)subvalue->original);
476  }
477  LY_CHECK_ERR_GOTO(r, rc = LY_EMEM, cleanup);
478  LYA_FOR(type_u->types, u) {
479  if (!errs[u]) {
480  /* no error for some reason */
481  continue;
482  }
483 
484  /* use an app-tag if all the types set it or set none */
485  if (errs[u]->apptag) {
486  if (!err_app_tag) {
487  err_app_tag = strdup(errs[u]->apptag);
488  LY_CHECK_ERR_GOTO(!err_app_tag, rc = LY_EMEM, cleanup);
489  use_err_app_tag = 1;
490  } else if (strcmp(errs[u]->apptag, err_app_tag)) {
491  use_err_app_tag = 0;
492  }
493  }
494 
495  type_plg = LYSC_GET_TYPE_PLG(type_u->types[u]->plugin_ref);
496 
497  r = ly_append_str(&msg, &msg_size, &msg_len, " %s: %s\n", type_plg->id, errs[u]->msg);
498  LY_CHECK_ERR_GOTO(r, rc = LY_EMEM, cleanup);
499  }
500 
501  if (!use_err_app_tag) {
502  free(err_app_tag);
503  err_app_tag = NULL;
504  }
505  rc = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, err_app_tag, "%s", msg);
506  err_app_tag = NULL;
507  } else if (type_idx) {
508  *type_idx = u;
509  }
510 
511 cleanup:
512  LYA_FOR(type_u->types, u) {
513  ly_err_free(errs[u]);
514  }
515  free(errs);
516  free(msg);
517  free(err_app_tag);
518  ly_temp_log_options(prev_lo);
519  return rc;
520 }
521 
540 static LY_ERR
541 union_prepare_subvalue(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, uint64_t value_size_bits,
542  uint32_t *options, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, const struct lysc_node *ctx_node,
543  struct lyd_value *storage, struct lyd_value_union **subvalue, struct ly_err_item **err)
544 {
545  *err = NULL;
546 
547  /* init storage */
548  memset(storage, 0, sizeof *storage);
549  LYPLG_TYPE_VAL_INLINE_PREPARE(storage, *subvalue);
550  LY_CHECK_RET(!*subvalue, ly_err_new_no_mem(err));
551 
552  storage->realtype = type;
553  (*subvalue)->hints = hints;
554  (*subvalue)->ctx_node = ctx_node;
555 
556  /* store the caller value as the subvalue original, it is needed for the member type store */
557  if (*options & LYPLG_TYPE_STORE_DYNAMIC) {
558  (*subvalue)->original = (void *)value;
559  *options &= ~LYPLG_TYPE_STORE_DYNAMIC;
560  } else if (value_size_bits) {
561  (*subvalue)->original = calloc(1, LYPLG_BITS2BYTES(value_size_bits));
562  LY_CHECK_RET(!(*subvalue)->original, ly_err_new_no_mem(err));
563  memcpy((*subvalue)->original, value, LYPLG_BITS2BYTES(value_size_bits));
564  } else {
565  (*subvalue)->original = strdup("");
566  LY_CHECK_RET(!(*subvalue)->original, ly_err_new_no_mem(err));
567  }
568  (*subvalue)->orig_size_bits = value_size_bits;
569 
570  /* derive format-specific data of the value, needed for the member type store;
571  * the values in the LYB format must have no prefix data stored (see ly_dup_prefix_data()) */
572  return lyplg_type_prefix_data_new(ctx, value, LYPLG_BITS2BYTES(value_size_bits), format,
573  (format == LY_VALUE_LYB) ? NULL : prefix_data, &(*subvalue)->format, &(*subvalue)->prefix_data);
574 }
575 
576 static LY_ERR
577 lyplg_type_store_union(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, uint64_t value_size_bits,
578  uint32_t options, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, const struct lysc_node *ctx_node,
579  struct lyd_value *storage, struct lys_glob_unres *unres, struct ly_err_item **err)
580 {
581  LY_ERR rc = LY_SUCCESS, r;
582  struct lysc_type_union *type_u;
583  struct lyd_value_union *subvalue;
584  uint32_t type_idx = 0;
585 
586  *err = NULL;
587 
588  type_u = (struct lysc_type_union *)type;
589 
590  /* initialize the storage and assign the caller value to the subvalue */
591  rc = union_prepare_subvalue(ctx, type, value, value_size_bits, &options, format, prefix_data, hints,
592  ctx_node, storage, &subvalue, err);
593  LY_CHECK_GOTO(rc, cleanup);
594 
595  if (format == LY_VALUE_LYB) {
596  /* the binary value pins the member type to use, validate it and learn its index */
597  rc = union_validate_value_lyb(value, value_size_bits, type_u, err);
598  LY_CHECK_GOTO(rc, cleanup);
599  union_parse_value_lyb(value, 0, &type_idx, NULL, NULL);
600 
601  /* use the pinned member type to store the value, its acceptance errors are propagated */
602  rc = union_read_store_type(ctx, type_u, type_idx, subvalue, options, NULL, NULL, NULL, unres, err);
603  LY_CHECK_GOTO(rc && (rc != LY_EINCOMPLETE), cleanup);
604  } else {
605  /* use the first usable and valid subtype to store the value */
606  rc = union_find_type(ctx, type_u, subvalue, options & ~LYPLG_TYPE_STORE_ONLY, NULL, NULL, NULL, NULL, unres, err);
607  if (rc && (rc != LY_EINCOMPLETE) && (options & LYPLG_TYPE_STORE_ONLY)) {
608  /* we tried to find the actual type by validating the value but no type matched, so try to find any type */
609  ly_err_free(*err);
610  rc = union_find_type(ctx, type_u, subvalue, options, NULL, NULL, NULL, NULL, unres, err);
611  }
612  LY_CHECK_GOTO(rc && (rc != LY_EINCOMPLETE), cleanup);
613  }
614 
615  /* store canonical value, if any (use the specific type value) */
616  r = lydict_dup(ctx, subvalue->value._canonical, &storage->_canonical);
617  LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
618 
619 cleanup:
620  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
621  free((void *)value);
622  }
623 
624  if (rc && (rc != LY_EINCOMPLETE)) {
625  lyplg_type_free_union(ctx, storage);
626  }
627  return rc;
628 }
629 
630 LIBYANG_API_DEF LY_ERR
631 lyplg_type_store_union_idx(const struct ly_ctx *ctx, const struct lysc_type *type, uint32_t type_idx,
632  const void *value, uint64_t value_size_bits, uint32_t options, LY_VALUE_FORMAT format, void *prefix_data,
633  uint32_t hints, const struct lysc_node *ctx_node, struct lyd_value *storage, struct ly_err_item **err)
634 {
635  LY_ERR rc = LY_SUCCESS, r;
636  struct lysc_type_union *type_u = (struct lysc_type_union *)type;
637  struct lyd_value_union *subvalue;
638  void *lyb;
639  uint64_t lyb_size_bits;
640 
641  LY_CHECK_ARG_RET(ctx, ctx, type, type->basetype == LY_TYPE_UNION, type_idx < LYA_COUNT(type_u->types),
642  !(options & LYPLG_TYPE_STORE_IMPLEMENT), storage, err, LY_EINVAL);
643 
644  memset(storage, 0, sizeof *storage);
645  *err = NULL;
646 
647  LYPLG_TYPE_VAL_INLINE_PREPARE(storage, subvalue);
648  LY_CHECK_ERR_GOTO(!subvalue, rc = LY_EMEM, cleanup);
649 
650  /* store the value of the specific type */
651  rc = union_store_type(ctx, type_u->types[type_idx], value, value_size_bits, options & ~LYPLG_TYPE_STORE_DYNAMIC,
652  format, prefix_data, hints, ctx_node, &subvalue->value, NULL, err);
653  LY_CHECK_GOTO(rc && (rc != LY_EINCOMPLETE), cleanup);
654 
655  /* create the union LYB value with the resolved type index */
656  r = union_create_value_lyb(ctx, type_idx, &subvalue->value, &lyb, &lyb_size_bits, err);
657  LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
658 
659  /* store it as the original value (hints, while not relevant for the LYB format, may be used by other formats) */
660  subvalue->original = lyb;
661  subvalue->orig_size_bits = lyb_size_bits;
662  subvalue->hints = hints;
663  subvalue->format = LY_VALUE_LYB;
664  subvalue->ctx_node = ctx_node;
665 
666  /* store canonical value (use the specific type value) and realtype */
667  r = lydict_dup(ctx, subvalue->value._canonical, &storage->_canonical);
668  LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
669  storage->realtype = type;
670 
671 cleanup:
672  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
673  free((void *)value);
674  }
675 
676  if (rc && (rc != LY_EINCOMPLETE)) {
677  lyplg_type_free_union(ctx, storage);
678  memset(storage, 0, sizeof *storage);
679  }
680  return rc;
681 }
682 
683 static LY_ERR
684 lyplg_type_validate_tree_union(const struct ly_ctx *ctx, const struct lysc_type *type, const struct lyd_node *ctx_node,
685  const struct lyd_node *tree, const struct lyd_val_unres *val_unres, struct lyd_value *storage, struct ly_err_item **err)
686 {
687  LY_ERR rc = LY_SUCCESS;
688  struct lysc_type_union *type_u = (struct lysc_type_union *)type;
689  struct lyd_value_union *subvalue = storage->subvalue;
690  struct lyd_value orig = {0};
691  uint32_t type_idx;
692  ly_bool validated = 0;
693  struct lyplg_type *realtype_plg;
694 
695  *err = NULL;
696 
697  /* because of types that do not store their own type as realtype (leafref), we are not able to call their
698  * validate callback (there is no way to get the type) but even if possible, the value may be invalid
699  * for the type, so we may have to perform union value storing again from scratch, but keep a value backup */
700  realtype_plg = LYSC_GET_TYPE_PLG(subvalue->value.realtype->plugin_ref);
701  LY_CHECK_RET(realtype_plg->duplicate(ctx, &subvalue->value, &orig));
702  realtype_plg->free(ctx, &subvalue->value);
703 
704  if (subvalue->format == LY_VALUE_LYB) {
705  /* use the specific type to store and validate the value */
706  union_parse_value_lyb(subvalue->original, 0, &type_idx, NULL, NULL);
707 
708  if (union_read_store_type(ctx, type_u, type_idx, subvalue, 0, ctx_node, tree, val_unres, NULL, err)) {
709  /* validation failed, we need to try storing the value again */
710  ly_err_free(*err);
711  *err = NULL;
712  } else {
713  validated = 1;
714  }
715  }
716 
717  if (!validated) {
718  /* use the first usable subtype to store and validate the value */
719  rc = union_find_type(ctx, type_u, subvalue, 0, ctx_node, tree, val_unres, NULL, NULL, err);
720  LY_CHECK_GOTO(rc, cleanup);
721  }
722 
723  /* update the canonical value, if any generated */
724  lydict_remove(ctx, storage->_canonical);
725  storage->_canonical = NULL;
726  LY_CHECK_GOTO(rc = lydict_dup(ctx, subvalue->value._canonical, &storage->_canonical), cleanup);
727 
728 cleanup:
729  if (rc) {
730  /* restore the previous value */
731  subvalue->value = orig;
732  } else {
733  /* free the backup value */
734  LYSC_GET_TYPE_PLG(orig.realtype->plugin_ref)->free(ctx, &orig);
735  }
736  return rc;
737 }
738 
739 static LY_ERR
740 lyplg_type_compare_union(const struct ly_ctx *ctx, const struct lyd_value *val1, const struct lyd_value *val2)
741 {
742  if (val1->subvalue->value.realtype != val2->subvalue->value.realtype) {
743  return LY_ENOT;
744  }
745  return LYSC_GET_TYPE_PLG(val1->subvalue->value.realtype->plugin_ref)->compare(ctx,
746  &val1->subvalue->value, &val2->subvalue->value);
747 }
748 
749 static int
750 lyplg_type_sort_union(const struct ly_ctx *ctx, const struct lyd_value *val1, const struct lyd_value *val2)
751 {
752  int rc;
753  LYA_COUNT_T u;
754  struct lysc_type **types, *type;
755 
756  if (val1->subvalue->value.realtype == val2->subvalue->value.realtype) {
757  return LYSC_GET_TYPE_PLG(val1->subvalue->value.realtype->plugin_ref)->sort(ctx,
758  &val1->subvalue->value, &val2->subvalue->value);
759  }
760 
761  /* compare according to the order of types */
762  rc = 0;
763  types = ((struct lysc_type_union *)val1->realtype)->types;
764  LYA_FOR(types, u) {
765  if (types[u]->basetype == LY_TYPE_LEAFREF) {
766  type = ((struct lysc_type_leafref *)types[u])->realtype;
767  } else {
768  type = types[u];
769  }
770 
771  if (type == val1->subvalue->value.realtype) {
772  rc = 1;
773  break;
774  } else if (type == val2->subvalue->value.realtype) {
775  rc = -1;
776  break;
777  }
778  }
779  assert(rc);
780 
781  return rc;
782 }
783 
794 static const void *
795 union_print_value_lyb(const struct ly_ctx *ctx, struct lysc_type_union *type_u, struct lyd_value_union *subvalue,
796  uint64_t *value_size_bits)
797 {
798  void *ret = NULL;
799  LY_ERR r;
800  struct ly_err_item *err;
801  uint32_t type_idx = 0;
802  struct lyd_value orig = {0};
803  struct lyplg_type *realtype_plg;
804 
805  if (!ctx) {
806  assert(subvalue->ctx_node);
807  ctx = subvalue->ctx_node->module->ctx;
808  }
809 
810  /* because of types that do not store their own type as realtype (leafref), we are not able to call their
811  * validate callback (there is no way to get the type) but even if possible, the value may be invalid
812  * for the type, so we may have to perform union value storing again from scratch, but keep a value backup */
813  realtype_plg = LYSC_GET_TYPE_PLG(subvalue->value.realtype->plugin_ref);
814  LY_CHECK_RET(realtype_plg->duplicate(ctx, &subvalue->value, &orig), NULL);
815  realtype_plg->free(ctx, &subvalue->value);
816 
817  /* learn the type index, may fail if LYPLG_TYPE_STORE_ONLY has been used before */
818  r = union_find_type(ctx, type_u, subvalue, 0, NULL, NULL, NULL, &type_idx, NULL, &err);
819  ly_err_free(err);
820  if (r && (r != LY_EINCOMPLETE)) {
821  /* use LYPLG_TYPE_STORE_ONLY to manage to print the value */
822  r = union_find_type(ctx, type_u, subvalue, LYPLG_TYPE_STORE_ONLY, NULL, NULL, NULL, &type_idx, NULL, &err);
823  ly_err_free(err);
824  }
825  LY_CHECK_ERR_RET(r && (r != LY_EINCOMPLETE), subvalue->value = orig, NULL);
826 
827  /* create the LYB data of the resolved member type value */
828  r = union_create_value_lyb(NULL, type_idx, &subvalue->value, &ret, value_size_bits, &err);
829  ly_err_free(err);
830  LY_CHECK_ERR_RET(r, subvalue->value = orig, NULL);
831 
832  /* restore backup (original) value */
833  realtype_plg = LYSC_GET_TYPE_PLG(subvalue->value.realtype->plugin_ref);
834  realtype_plg->free(ctx, &subvalue->value);
835  subvalue->value = orig;
836  return ret;
837 }
838 
839 static const void *
840 lyplg_type_print_union(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
841  void *prefix_data, ly_bool *dynamic, uint64_t *value_size_bits)
842 {
843  const void *ret;
844  struct lyd_value_union *subvalue = value->subvalue;
845  struct lysc_type_union *type_u = (struct lysc_type_union *)value->realtype;
846  uint64_t lyb_data_size_bits = 0;
847 
848  if ((format == LY_VALUE_LYB) && (subvalue->format == LY_VALUE_LYB)) {
849  /* return value is ready */
850  *dynamic = 0;
851  if (value_size_bits) {
852  *value_size_bits = subvalue->orig_size_bits;
853  }
854  return subvalue->original;
855  } else if ((format == LY_VALUE_LYB) && (subvalue->format != LY_VALUE_LYB)) {
856  /* return LYB data value must be created */
857  *dynamic = 1;
858  ret = union_print_value_lyb(ctx, type_u, subvalue, &lyb_data_size_bits);
859  if (value_size_bits) {
860  *value_size_bits = lyb_data_size_bits;
861  }
862  return ret;
863  }
864 
865  assert(format != LY_VALUE_LYB);
866  ret = (void *)LYSC_GET_TYPE_PLG(subvalue->value.realtype->plugin_ref)->print(ctx, &subvalue->value,
867  format, prefix_data, dynamic, value_size_bits);
868  if (!value->_canonical && (format == LY_VALUE_CANON)) {
869  /* the canonical value is supposed to be stored now */
870  lydict_dup(ctx, subvalue->value._canonical, (const char **)&value->_canonical);
871  }
872 
873  return ret;
874 }
875 
876 static LY_ERR
877 lyplg_type_dup_union(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
878 {
879  LY_ERR ret = LY_SUCCESS;
880  struct lyd_value_union *orig_val = original->subvalue, *dup_val;
881 
882  /* init dup value */
883  memset(dup, 0, sizeof *dup);
884  dup->realtype = original->realtype;
885 
886  ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
887  LY_CHECK_GOTO(ret, cleanup);
888 
889  dup_val = calloc(1, sizeof *dup_val);
890  LY_CHECK_ERR_GOTO(!dup_val, LOGMEM(ctx); ret = LY_EMEM, cleanup);
891  dup->subvalue = dup_val;
892 
893  ret = LYSC_GET_TYPE_PLG(orig_val->value.realtype->plugin_ref)->duplicate(ctx, &orig_val->value, &dup_val->value);
894  LY_CHECK_GOTO(ret, cleanup);
895 
896  if (orig_val->orig_size_bits) {
897  dup_val->original = calloc(1, LYPLG_BITS2BYTES(orig_val->orig_size_bits));
898  LY_CHECK_ERR_GOTO(!dup_val->original, LOGMEM(ctx); ret = LY_EMEM, cleanup);
899  memcpy(dup_val->original, orig_val->original, LYPLG_BITS2BYTES(orig_val->orig_size_bits));
900  } else {
901  dup_val->original = strdup("");
902  LY_CHECK_ERR_GOTO(!dup_val->original, LOGMEM(ctx); ret = LY_EMEM, cleanup);
903  }
904  dup_val->orig_size_bits = orig_val->orig_size_bits;
905 
906  dup_val->format = orig_val->format;
907  dup_val->ctx_node = orig_val->ctx_node;
908  dup_val->hints = orig_val->hints;
909  ret = lyplg_type_prefix_data_dup(ctx, orig_val->format, orig_val->prefix_data, &dup_val->prefix_data);
910  LY_CHECK_GOTO(ret, cleanup);
911 
912 cleanup:
913  if (ret) {
914  lyplg_type_free_union(ctx, dup);
915  }
916  return ret;
917 }
918 
919 static void
920 lyplg_type_free_union(const struct ly_ctx *ctx, struct lyd_value *value)
921 {
922  struct lyd_value_union *val;
923 
925  value->_canonical = NULL;
926  LYD_VALUE_GET(value, val);
927  if (val) {
928  if (val->value.realtype) {
929  LYSC_GET_TYPE_PLG(val->value.realtype->plugin_ref)->free(ctx, &val->value);
930  }
932  free(val->original);
933 
935  }
936 }
937 
945 const struct lyplg_type_record plugins_union[] = {
946  {
947  .module = "",
948  .revision = NULL,
949  .name = LY_TYPE_UNION_STR,
950 
951  .plugin.id = "ly2 union",
952  .plugin.lyb_size = lyplg_type_lyb_size_variable_bits,
953  .plugin.store = lyplg_type_store_union,
954  .plugin.validate_value = NULL,
955  .plugin.validate_tree = lyplg_type_validate_tree_union,
956  .plugin.compare = lyplg_type_compare_union,
957  .plugin.sort = lyplg_type_sort_union,
958  .plugin.print = lyplg_type_print_union,
959  .plugin.duplicate = lyplg_type_dup_union,
960  .plugin.free = lyplg_type_free_union,
961  },
962  {0}
963 };
libyang dictionary
#define LYA_PRI_COUNT_T
Printing format specifier macro for LYA_COUNT_T values.
Definition: ly_array.h:43
#define LYA_COUNT(ARRAY)
Get the number of records in the ARRAY.
Definition: ly_array.h:78
#define LYA_FOR(ARRAY, INDEX)
Helper macro to go through sized-arrays with a numeric iterator.
Definition: ly_array.h:55
#define LYA_COUNT_T
Type (i.e. size) of the sized array's size counter.
Definition: ly_array.h:38
libyang context handler.
LIBYANG_API_DECL LY_ERR lydict_dup(const struct ly_ctx *ctx, const char *value, const char **str_p)
Duplicate string in dictionary. Only a reference counter is incremented.
LIBYANG_API_DECL LY_ERR lydict_insert(const struct ly_ctx *ctx, const char *value, size_t len, const char **str_p)
Insert string into dictionary. If the string is already present, only a reference counter is incremen...
LIBYANG_API_DECL LY_ERR lydict_remove(const struct ly_ctx *ctx, const char *value)
Remove specified string from the dictionary. It decrement reference counter for the string and if it ...
LY_ERR err
Definition: log.h:300
char * apptag
Definition: log.h:306
char * msg
Definition: log.h:302
LY_ERR
libyang's error codes returned by the libyang functions.
Definition: log.h:252
@ LYVE_DATA
Definition: log.h:290
@ LY_EINVAL
Definition: log.h:256
@ LY_EMEM
Definition: log.h:254
@ LY_ENOT
Definition: log.h:266
@ LY_EVALID
Definition: log.h:260
@ LY_SUCCESS
Definition: log.h:253
@ LY_EINCOMPLETE
Definition: log.h:262
Libyang full error structure.
Definition: log.h:298
LIBYANG_API_DECL uint32_t * ly_temp_log_options(uint32_t *opts)
Set temporary thread-safe (thread-specific) logger options overwriting those set by ly_log_options().
const char * module
lyplg_type_store_clb store
const char * id
lyplg_type_dup_clb duplicate
lyplg_type_validate_tree_clb validate_tree
lyplg_type_free_clb free
#define LYPLG_TYPE_VAL_INLINE_PREPARE(storage, type_val)
Prepare value memory for storing a specific type value, may be allocated dynamically.
LIBYANG_API_DECL void ly_err_free(void *ptr)
Destructor for the error records created with ly_err_new().
LIBYANG_API_DEF LY_ERR lyplg_type_store_union_idx(const struct ly_ctx *ctx, const struct lysc_type *type, uint32_t type_idx, const void *value, uint64_t value_size_bits, uint32_t options, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, const struct lysc_node *ctx_node, struct lyd_value *storage, struct ly_err_item **err)
Store a union value with a specific member type index, keeping that member resolved (pinned),...
Definition: union.c:631
#define LYPLG_TYPE_VAL_INLINE_DESTROY(type_val)
Destroy a prepared value.
LIBYANG_API_DECL LY_ERR lyplg_type_prefix_data_new(const struct ly_ctx *ctx, const void *value, uint32_t value_size, LY_VALUE_FORMAT format, const void *prefix_data, LY_VALUE_FORMAT *format_p, void **prefix_data_p)
Store used prefixes in a string into an internal libyang structure used in lyd_value.
LIBYANG_API_DECL LY_ERR ly_err_new(struct ly_err_item **err, LY_ERR ecode, LY_VECODE vecode, char *data_path, char *apptag, const char *err_format,...) _FORMAT_PRINTF(6
Create and fill an error structure.
LIBYANG_API_DECL LY_ERR lyplg_type_prefix_data_dup(const struct ly_ctx *ctx, LY_VALUE_FORMAT format, const void *orig, void **dup)
Duplicate prefix data.
#define LYPLG_BITS2BYTES(bits)
Convert bits to bytes.
LIBYANG_API_DECL LY_ERR LIBYANG_API_DECL LY_ERR ly_err_new_no_mem(struct ly_err_item **err)
Create and fill an error structure with a no-memory error.
LIBYANG_API_DECL void lyplg_type_prefix_data_free(LY_VALUE_FORMAT format, void *prefix_data)
Free internal prefix data.
Hold type-specific functions for various operations with the data values.
LIBYANG_API_DECL void lyplg_type_lyb_size_variable_bits(const struct lysc_type *type, enum lyplg_lyb_size_type *size_type, uint64_t *fixed_size_bits)
Implementation of lyplg_type_lyb_size_clb for a type with variable length in bits.
#define LYPLG_TYPE_STORE_DYNAMIC
#define LYPLG_TYPE_STORE_IMPLEMENT
#define LYPLG_TYPE_STORE_ONLY
LY_DATA_TYPE basetype
Definition: tree_schema.h:1278
struct lyxp_expr * path
Definition: tree_schema.h:1357
struct lys_module * module
Definition: tree_schema.h:1416
uintptr_t plugin_ref
Definition: tree_schema.h:1277
struct lysc_type ** types
Definition: tree_schema.h:1391
struct ly_ctx * ctx
Definition: tree_schema.h:2243
LY_DATA_TYPE basetype
Definition: tree_schema.h:1388
LIBYANG_API_DECL const char * lyxp_get_expr(const struct lyxp_expr *path)
Getter for original XPath expression from a parsed expression.
Compiled YANG data node.
Definition: tree_schema.h:1412
uint8_t ly_bool
Type to indicate boolean value.
Definition: log.h:29
libyang sized array API.
API for (user) types plugins.
uint64_t orig_size_bits
Definition: tree_data.h:669
const struct lysc_type * realtype
Definition: tree_data.h:614
void * original
Definition: tree_data.h:668
void * prefix_data
Definition: tree_data.h:674
uint32_t hints
Definition: tree_data.h:670
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
Definition: tree_data.h:653
struct lyd_value value
Definition: tree_data.h:666
const char * _canonical
Definition: tree_data.h:611
LY_VALUE_FORMAT format
Definition: tree_data.h:671
const struct lysc_node * ctx_node
Definition: tree_data.h:675
Generic structure for a data node.
Definition: tree_data.h:875
YANG data representation.
Definition: tree_data.h:610
Special lyd_value structure for built-in union values.
Definition: tree_data.h:665
#define LYPLG_UNION_TYPE_IDX_SIZE
Size in bytes of the used type index in the LYB Binary Format.
Definition: union.c:49
const struct lyplg_type_record plugins_union[]
Plugin information for union type implementation.
Definition: union.c:945
@ LY_TYPE_LEAFREF
Definition: utils.h:76
@ LY_TYPE_UNION
Definition: utils.h:77
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition: utils.h:93
@ LY_VALUE_CANON
Definition: utils.h:94
@ LY_VALUE_LYB
Definition: utils.h:100