Loading...
Searching...
No Matches
instanceid_keys.c
Go to the documentation of this file.
#define _GNU_SOURCE /* strdup */
#include "plugins_types.h"
#include <stdint.h>
#include <stdlib.h>
#include "libyang.h"
/* additional internal headers for some useful simple macros */
#include "compat.h"
#include "ly_common.h"
#include "path.h"
#include "plugins_internal.h" /* LY_TYPE_*_STR */
#include "xpath.h"
struct lyd_value_instance_identifier_keys {
struct lyxp_expr *keys;
const struct ly_ctx *ctx;
void *prefix_data;
LY_VALUE_FORMAT format;
};
static LY_ERR
instanceid_keys_print_value(const struct lyd_value_instance_identifier_keys *val, LY_VALUE_FORMAT format, void *prefix_data,
char **str_value, struct ly_err_item **err)
{
LY_ERR ret = LY_SUCCESS;
uint32_t cur_idx = 0, str_len = 0;
enum lyxp_token cur_tok;
char *str_tok;
void *mem;
const char *cur_exp_ptr;
ly_bool is_nt;
const struct lys_module *context_mod = NULL, *local_mod = NULL;
struct ly_set *mods;
*str_value = NULL;
if (format == LY_VALUE_XML) {
/* null the local module so that all the prefixes are printed */
mods = prefix_data;
local_mod = mods->objs[0];
mods->objs[0] = NULL;
}
while (cur_idx < val->keys->used) {
cur_tok = val->keys->tokens[cur_idx];
cur_exp_ptr = val->keys->expr + val->keys->tok_pos[cur_idx];
if ((cur_tok == LYXP_TOKEN_NAMETEST) || (cur_tok == LYXP_TOKEN_LITERAL)) {
/* tokens that may include prefixes, get them in the target format */
is_nt = (cur_tok == LYXP_TOKEN_NAMETEST) ? 1 : 0;
LY_CHECK_GOTO(ret = lyplg_type_xpath10_print_token(cur_exp_ptr, val->keys->tok_len[cur_idx], is_nt, &context_mod,
val->ctx, val->format, val->prefix_data, format, prefix_data, &str_tok, err), cleanup);
/* append the converted token */
mem = realloc(*str_value, str_len + strlen(str_tok) + 1);
if (!mem) {
free(str_tok);
ret = ly_err_new(err, LY_EMEM, LYVE_DATA, NULL, NULL, "No memory.");
goto cleanup;
}
*str_value = mem;
str_len += sprintf(*str_value + str_len, "%s", str_tok);
free(str_tok);
/* token processed */
++cur_idx;
} else {
/* just copy the token */
mem = realloc(*str_value, str_len + val->keys->tok_len[cur_idx] + 1);
if (!mem) {
ret = ly_err_new(err, LY_EMEM, LYVE_DATA, NULL, NULL, "No memory.");
goto cleanup;
}
*str_value = mem;
str_len += sprintf(*str_value + str_len, "%.*s", (int)val->keys->tok_len[cur_idx], cur_exp_ptr);
/* token processed */
++cur_idx;
}
}
cleanup:
if (local_mod) {
mods->objs[0] = (void *)local_mod;
}
if (ret) {
free(*str_value);
*str_value = NULL;
}
return ret;
}
static LY_ERR
lyplg_type_store_instanceid_keys(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 *UNUSED(ctx_node),
struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres), struct ly_err_item **err)
{
LY_ERR ret = LY_SUCCESS;
struct lysc_type_str *type_str = (struct lysc_type_str *)type;
struct lyd_value_instance_identifier_keys *val;
uint32_t *prev_lo, temp_lo = LY_LOSTORE;
const struct ly_err_item *eitem;
char *canon;
/* init storage */
memset(storage, 0, sizeof *storage);
LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
storage->realtype = type;
/* check hints */
ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
LY_CHECK_GOTO(ret, cleanup);
/* length restriction of the string */
if (type_str->length) {
/* value_len is in bytes, but we need number of characters here */
ret = lyplg_type_validate_range(LY_TYPE_STRING, type_str->length, ly_utf8len(value, value_len), value, value_len, err);
LY_CHECK_GOTO(ret, cleanup);
}
/* pattern restrictions */
ret = lyplg_type_validate_patterns(type_str->patterns, value, value_len, err);
LY_CHECK_GOTO(ret, cleanup);
/* parse instance-identifier keys, with optional prefix even though it should be mandatory */
if (value_len && (((char *)value)[0] != '[')) {
ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid first character '%c', list key predicates expected.",
((char *)value)[0]);
goto cleanup;
}
/* do not log */
prev_lo = ly_temp_log_options(&temp_lo);
ret = ly_path_parse_predicate(ctx, NULL, value_len ? value : "", value_len, LY_PATH_PREFIX_OPTIONAL,
LY_PATH_PRED_KEYS, &val->keys);
ly_temp_log_options(prev_lo);
if (ret) {
eitem = ly_err_last(ctx);
ret = ly_err_new(err, ret, LYVE_DATA, eitem->data_path, NULL, "%s", eitem->msg);
ly_err_clean((struct ly_ctx *)ctx, NULL);
goto cleanup;
}
val->ctx = ctx;
/* store format-specific data and context for later prefix resolution */
ret = lyplg_type_prefix_data_new(ctx, value, value_len, format, prefix_data, &val->format, &val->prefix_data);
LY_CHECK_GOTO(ret, cleanup);
switch (format) {
case LY_VALUE_CANON:
case LY_VALUE_JSON:
case LY_VALUE_LYB:
case LY_VALUE_STR_NS:
/* store canonical value */
if (options & LYPLG_TYPE_STORE_DYNAMIC) {
ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
options &= ~LYPLG_TYPE_STORE_DYNAMIC;
LY_CHECK_GOTO(ret, cleanup);
} else {
ret = lydict_insert(ctx, value_len ? value : "", value_len, &storage->_canonical);
LY_CHECK_GOTO(ret, cleanup);
}
break;
case LY_VALUE_SCHEMA:
case LY_VALUE_SCHEMA_RESOLVED:
case LY_VALUE_XML:
/* JSON format with prefix is the canonical one */
ret = instanceid_keys_print_value(val, LY_VALUE_JSON, NULL, &canon, err);
LY_CHECK_GOTO(ret, cleanup);
ret = lydict_insert_zc(ctx, canon, &storage->_canonical);
LY_CHECK_GOTO(ret, cleanup);
break;
}
cleanup:
if (options & LYPLG_TYPE_STORE_DYNAMIC) {
free((void *)value);
}
if (ret) {
lyplg_type_free_xpath10(ctx, storage);
}
return ret;
}
const struct lyplg_type_record plugins_instanceid_keys[] = {
{
.module = "yang",
.revision = NULL,
.name = "instance-identifier-keys",
.plugin.id = "libyang 2 - instance-identifier-keys, version 1",
.plugin.store = lyplg_type_store_instanceid_keys,
.plugin.validate = NULL,
.plugin.compare = lyplg_type_compare_simple,
.plugin.sort = lyplg_type_sort_simple,
.plugin.print = lyplg_type_print_xpath10,
.plugin.duplicate = lyplg_type_dup_xpath10,
.plugin.free = lyplg_type_free_xpath10,
.plugin.lyb_data_len = -1,
},
{0}
};
ly_ctx
libyang context handler.
lydict_insert
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...
lydict_insert_zc
LIBYANG_API_DECL LY_ERR lydict_insert_zc(const struct ly_ctx *ctx, char *value, const char **str_p)
Insert string into dictionary - zerocopy version. If the string is already present,...
ly_err_item::err
LY_ERR err
Definition log.h:284
ly_err_item::data_path
char * data_path
Definition log.h:287
ly_err_item::msg
char * msg
Definition log.h:286
ly_err_last
LIBYANG_API_DECL const struct ly_err_item * ly_err_last(const struct ly_ctx *ctx)
Get the latest (thread, context-specific) generated error structure.
LY_ERR
LY_ERR
libyang's error codes returned by the libyang functions.
Definition log.h:237
ly_err_clean
LIBYANG_API_DECL void ly_err_clean(struct ly_ctx *ctx, struct ly_err_item *eitem)
Free error structures from a context.
LYVE_DATA
@ LYVE_DATA
Definition log.h:274
LY_EMEM
@ LY_EMEM
Definition log.h:239
LY_EVALID
@ LY_EVALID
Definition log.h:245
LY_SUCCESS
@ LY_SUCCESS
Definition log.h:238
ly_err_item
Libyang full error structure.
Definition log.h:282
ly_temp_log_options
LIBYANG_API_DECL uint32_t * ly_temp_log_options(uint32_t *opts)
Set temporary thread-safe logger options overwriting those set by ly_log_options().
LY_LOSTORE
#define LY_LOSTORE
Definition log.h:118
ly_set
Structure to hold a set of (not necessary somehow connected) objects. Usually used for lyd_node,...
Definition set.h:47
LYPLG_TYPE_VAL_INLINE_PREPARE
#define LYPLG_TYPE_VAL_INLINE_PREPARE(storage, type_val)
Prepare value memory for storing a specific type value, may be allocated dynamically.
Definition plugins_types.h:194
lyplg_type_validate_patterns
LIBYANG_API_DECL LY_ERR lyplg_type_validate_patterns(struct lysc_pattern **patterns, const char *str, size_t str_len, struct ly_err_item **err)
Data type validator for pattern-restricted string values.
lyplg_type_xpath10_print_token
LIBYANG_API_DEF LY_ERR lyplg_type_xpath10_print_token(const char *token, uint16_t tok_len, ly_bool is_nametest, const struct lys_module **context_mod, const struct ly_ctx *resolve_ctx, LY_VALUE_FORMAT resolve_format, const void *resolve_prefix_data, LY_VALUE_FORMAT get_format, void *get_prefix_data, char **token_p, struct ly_err_item **err)
Print xpath1.0 token in the specific format.
Definition xpath1.0.c:41
lyplg_type_validate_range
LIBYANG_API_DECL LY_ERR lyplg_type_validate_range(LY_DATA_TYPE basetype, struct lysc_range *range, int64_t value, const char *strval, size_t strval_len, struct ly_err_item **err)
Data type validator for a range/length-restricted values.
lyplg_type_check_hints
LIBYANG_API_DECL LY_ERR lyplg_type_check_hints(uint32_t hints, const char *value, size_t value_len, LY_DATA_TYPE type, int *base, struct ly_err_item **err)
Check that the type is suitable for the parser's hints (if any) in the specified format.
ly_err_new
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 error structure.
lyplg_type_prefix_data_new
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.
lyplg_type_record
Definition plugins_types.h:635
lyplg_type_sort_simple
LIBYANG_API_DEF int lyplg_type_sort_simple(const struct ly_ctx *ctx, const struct lyd_value *val1, const struct lyd_value *val2)
Implementation of lyplg_type_sort_clb for a generic simple type.
lyplg_type_compare_simple
LIBYANG_API_DECL LY_ERR lyplg_type_compare_simple(const struct ly_ctx *ctx, const struct lyd_value *val1, const struct lyd_value *val2)
Implementation of lyplg_type_compare_clb for a generic simple type.
lyplg_type_dup_xpath10
LIBYANG_API_DECL LY_ERR lyplg_type_dup_xpath10(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
Implementation of lyplg_type_dup_clb for the ietf-yang-types xpath1.0 type.
Definition xpath1.0.c:479
lyplg_type_free_xpath10
LIBYANG_API_DECL void lyplg_type_free_xpath10(const struct ly_ctx *ctx, struct lyd_value *value)
Implementation of lyplg_type_free_clb for the ietf-yang-types xpath1.0 type.
Definition xpath1.0.c:511
lyplg_type_print_xpath10
LIBYANG_API_DECL const void * lyplg_type_print_xpath10(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 ietf-yang-types xpath1.0 type.
Definition xpath1.0.c:440
LYPLG_TYPE_STORE_DYNAMIC
#define LYPLG_TYPE_STORE_DYNAMIC
Definition plugins_types.h:471
lysc_type::basetype
LY_DATA_TYPE basetype
Definition tree_schema.h:1287
lysc_type_str::patterns
struct lysc_pattern ** patterns
Definition tree_schema.h:1321
lysc_type_str::length
struct lysc_range * length
Definition tree_schema.h:1320
lys_module
Available YANG schema tree structures representing YANG module.
Definition tree_schema.h:2137
lysc_node
Compiled YANG data node.
Definition tree_schema.h:1421
lysc_type
Definition tree_schema.h:1283
lysc_type_str
Definition tree_schema.h:1313
LY_VALUE_FORMAT
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition tree.h:234
LY_TYPE_STRING
@ LY_TYPE_STRING
Definition tree.h:209
LY_VALUE_JSON
@ LY_VALUE_JSON
Definition tree.h:239
LY_VALUE_SCHEMA
@ LY_VALUE_SCHEMA
Definition tree.h:236
LY_VALUE_CANON
@ LY_VALUE_CANON
Definition tree.h:235
LY_VALUE_XML
@ LY_VALUE_XML
Definition tree.h:238
LY_VALUE_STR_NS
@ LY_VALUE_STR_NS
Definition tree.h:241
LY_VALUE_SCHEMA_RESOLVED
@ LY_VALUE_SCHEMA_RESOLVED
Definition tree.h:237
LY_VALUE_LYB
@ LY_VALUE_LYB
Definition tree.h:240
lyd_value_instance_identifier_keys::format
LY_VALUE_FORMAT format
Definition instanceid_keys.c:46
lyd_value_instance_identifier_keys::prefix_data
void * prefix_data
Definition instanceid_keys.c:45
plugins_instanceid_keys
const struct lyplg_type_record plugins_instanceid_keys[]
Plugin information for instance-identifier type implementation.
Definition instanceid_keys.c:236
lyd_value_instance_identifier_keys::ctx
const struct ly_ctx * ctx
Definition instanceid_keys.c:44
lyd_value_instance_identifier_keys::keys
struct lyxp_expr * keys
Definition instanceid_keys.c:43
lyd_value_instance_identifier_keys
Special lyd_value structure for yang instance-identifier-keys values.
Definition instanceid_keys.c:42
libyang.h
The main libyang public header.
ly_bool
uint8_t ly_bool
Type to indicate boolean value.
Definition log.h:35
plugins_types.h
API for (user) types plugins.
lyd_value::realtype
const struct lysc_type * realtype
Definition tree_data.h:571
lyd_value::_canonical
const char * _canonical
Definition tree_data.h:568
lyd_value
YANG data representation.
Definition tree_data.h:567