29 #include "plugins_internal.h"
59 union_subvalue_assignment(
const void *value,
size_t value_len,
void **original,
size_t *orig_len, uint32_t *options)
65 *original = (
void *)value;
66 *options &= ~LYPLG_TYPE_STORE_DYNAMIC;
67 }
else if (value_len) {
69 *original = calloc(1, value_len);
70 LY_CHECK_ERR_RET(!*original, ret =
LY_EMEM, ret);
71 memcpy(*original, value, value_len);
74 *original = strdup(
"");
75 LY_CHECK_ERR_RET(!*original, ret =
LY_EMEM, ret);
77 *orig_len = value_len;
95 uint64_t type_idx = 0;
105 memcpy(&type_idx, lyb_data,
IDX_SIZE);
106 type_idx = le64toh(type_idx);
128 lyb_parse_union(
const void *lyb_data,
size_t lyb_data_len, uint32_t *type_idx,
const void **lyb_value,
size_t *lyb_value_len)
132 assert(lyb_data && !(lyb_value && !lyb_value_len));
141 if (lyb_value && lyb_value_len && lyb_data_len) {
147 *lyb_value_len = lyb_data_len -
IDX_SIZE;
148 *lyb_value = (
char *)lyb_data + IDX_SIZE;
172 const void *value = NULL;
173 size_t value_len = 0;
176 lyb_parse_union(subvalue->
original, subvalue->
orig_len, NULL, &value, &value_len);
186 memset(&subvalue->
value, 0,
sizeof subvalue->
value);
219 struct lys_glob_unres *unres,
struct ly_err_item **err)
223 uint32_t temp_lo = 0;
236 ret = union_store_type(ctx, types[u], subvalue, resolve, ctx_node, tree, unres, err);
248 }
else if (type_idx) {
272 lyb_fill_subvalue(
const struct ly_ctx *ctx,
struct lysc_type_union *type_u,
const void *lyb_data,
size_t lyb_data_len,
273 void *prefix_data,
struct lyd_value_union *subvalue, uint32_t *options,
struct lys_glob_unres *unres,
278 const void *lyb_value = NULL;
279 size_t lyb_value_len = 0;
281 ret = lyb_union_validate(lyb_data, lyb_data_len, type_u, err);
285 lyb_parse_union(lyb_data, lyb_data_len, &type_idx, &lyb_value, &lyb_value_len);
289 ret = union_subvalue_assignment(lyb_data, lyb_data_len, &subvalue->
original, &subvalue->
orig_len, options);
304 ret = union_store_type(ctx, type_u->
types[type_idx], subvalue, 0, NULL, NULL, unres, err);
321 memset(storage, 0,
sizeof *storage);
323 LY_CHECK_ERR_GOTO(!subvalue, ret =
LY_EMEM, cleanup);
329 ret = lyb_fill_subvalue(ctx, type_u, value, value_len, prefix_data, subvalue, &options, unres, err);
333 ret = union_subvalue_assignment(value, value_len, &subvalue->
original, &subvalue->
orig_len, &options);
334 LY_CHECK_GOTO(ret, cleanup);
339 LY_CHECK_GOTO(ret, cleanup);
342 ret = union_find_type(ctx, type_u->
types, subvalue, 0, NULL, NULL, NULL, unres, err);
348 LY_CHECK_ERR_GOTO(r, ret = r, cleanup);
379 lyb_parse_union(subvalue->
original, 0, &type_idx, NULL, NULL);
380 ret = union_store_type(ctx, type_u->
types[type_idx], subvalue, 1, ctx_node, tree, NULL, err);
384 ret = union_find_type(ctx, type_u->
types, subvalue, 1, ctx_node, tree, NULL, NULL, err);
404 return val1->subvalue->value.
realtype->
plugin->
compare(&val1->subvalue->value, &val2->subvalue->value);
421 void *prefix_data,
size_t *value_len)
427 uint32_t type_idx = 0;
441 r = union_find_type(ctx, type_u->
types, subvalue, 0, NULL, NULL, &type_idx, NULL, &err);
447 LY_CHECK_RET(!pval, NULL);
451 ret = malloc(*value_len);
452 LY_CHECK_RET(!ret, NULL);
457 memcpy((
char *)ret +
IDX_SIZE, pval, pval_len);
466 LIBYANG_API_DEF
const void *
468 void *prefix_data,
ly_bool *dynamic,
size_t *value_len)
473 size_t lyb_data_len = 0;
485 ret = lyb_union_print(ctx, type_u, subvalue, prefix_data, &lyb_data_len);
487 *value_len = lyb_data_len;
509 memset(dup, 0,
sizeof *dup);
513 LY_CHECK_GOTO(ret, cleanup);
515 dup_val = calloc(1,
sizeof *dup_val);
516 LY_CHECK_ERR_GOTO(!dup_val,
LOGMEM(ctx); ret =
LY_EMEM, cleanup);
517 dup->subvalue = dup_val;
520 LY_CHECK_GOTO(ret, cleanup);
523 dup_val->original = calloc(1, orig_val->
orig_len);
524 LY_CHECK_ERR_GOTO(!dup_val->original,
LOGMEM(ctx); ret =
LY_EMEM, cleanup);
527 dup_val->original = strdup(
"");
528 LY_CHECK_ERR_GOTO(!dup_val->original,
LOGMEM(ctx); ret =
LY_EMEM, cleanup);
530 dup_val->orig_len = orig_val->
orig_len;
532 dup_val->format = orig_val->
format;
533 dup_val->ctx_node = orig_val->
ctx_node;
534 dup_val->hints = orig_val->
hints;
536 LY_CHECK_GOTO(ret, cleanup);
575 .name = LY_TYPE_UNION_STR,
577 .plugin.id =
"libyang 2 - union,version 1",
585 .plugin.lyb_data_len = -1,
struct lysc_type * realtype
#define LY_PRI_ARRAY_COUNT_TYPE
Printing format specifier macro for LY_ARRAY_SIZE_TYPE values.
struct lyplg_type * plugin
Generic structure for a data node.
LIBYANG_API_DECL LY_ERR ly_err_new(struct ly_err_item **err, LY_ERR ecode, LY_VECODE vecode, char *path, char *apptag, const char *err_format,...) _FORMAT_PRINTF(6
Create and fill error structure.
uint8_t ly_bool
Type to indicate boolean value.
LIBYANG_API_DECL const void * lyplg_type_print_union(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format, void *prefix_data, ly_bool *dynamic, size_t *value_len)
Implementation of lyplg_type_print_clb for the built-in union type.
#define LYPLG_TYPE_STORE_DYNAMIC
LIBYANG_API_DECL void ly_temp_log_options(uint32_t *opts)
Set temporary thread-safe logger options overwriting those set by ly_log_options().
LIBYANG_API_DECL LY_ERR lyplg_type_compare_union(const struct lyd_value *val1, const struct lyd_value *val2)
Implementation of lyplg_type_compare_clb for the built-in union type.
LIBYANG_API_DECL void lyplg_type_free_union(const struct ly_ctx *ctx, struct lyd_value *value)
Implementation of lyplg_type_free_clb for the built-in union type.
struct lysc_node * ctx_node
struct lysc_type ** types
The main libyang public header.
YANG data representation.
lyplg_type_store_clb store
Libyang full error structure.
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
LIBYANG_API_DECL LY_ERR LIBYANG_API_DECL void ly_err_free(void *ptr)
Destructor for the error records created with ly_err_new().
struct lyplg_type_record plugins_union[]
Plugin information for union type implementation.
LIBYANG_API_DECL LY_ERR lyplg_type_prefix_data_new(const struct ly_ctx *ctx, const void *value, size_t value_len, 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 lyplg_type_store_union(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, size_t value_len, uint32_t options, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, const struct lysc_node *ctx_node, struct lyd_value *storage, struct lys_glob_unres *unres, struct ly_err_item **err)
Implementation of lyplg_type_store_clb for the built-in union type.
#define IDX_SIZE
Size in bytes of the index in the LYB Binary Format.
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 ...
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...
lyplg_type_dup_clb duplicate
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 LY_ARRAY_COUNT(ARRAY)
Get the number of records in the ARRAY.
LIBYANG_API_DECL void lyplg_type_prefix_data_free(LY_VALUE_FORMAT format, void *prefix_data)
Free internal prefix data.
#define LY_ARRAY_COUNT_TYPE
Type (i.e. size) of the sized array's size counter.
struct lys_module * module
#define LYPLG_TYPE_VAL_INLINE_DESTROY(type_val)
Destroy a prepared value.
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
lyplg_type_print_clb print
lyplg_type_validate_clb validate
#define LYPLG_TYPE_VAL_INLINE_PREPARE(storage, type_val)
Prepare value memory for storing a specific type value, may be allocated dynamically.
LY_ERR
libyang's error codes returned by the libyang functions.
LIBYANG_API_DECL LY_ERR lyplg_type_validate_union(const struct ly_ctx *ctx, const struct lysc_type *type, const struct lyd_node *ctx_node, const struct lyd_node *tree, struct lyd_value *storage, struct ly_err_item **err)
Implementation of lyplg_type_validate_clb for the built-in union type.
lyplg_type_compare_clb compare
Special lyd_value structure for built-in union values.
API for (user) types plugins.
LIBYANG_API_DECL LY_ERR lyplg_type_dup_union(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
Implementation of lyplg_type_dup_clb for the built-in union type.