libyang  2.1.80
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
instanceid_keys.c
Go to the documentation of this file.
1 
14 #define _GNU_SOURCE /* strdup */
15 
16 #include "plugins_types.h"
17 
18 #include <stdint.h>
19 #include <stdlib.h>
20 
21 #include "libyang.h"
22 
23 /* additional internal headers for some useful simple macros */
24 #include "common.h"
25 #include "compat.h"
26 #include "path.h"
27 #include "plugins_internal.h" /* LY_TYPE_*_STR */
28 #include "xpath.h"
29 
43  struct lyxp_expr *keys;
44  const struct ly_ctx *ctx;
45  void *prefix_data;
47 };
48 
59 static LY_ERR
60 instanceid_keys_print_value(const struct lyd_value_instance_identifier_keys *val, LY_VALUE_FORMAT format, void *prefix_data,
61  char **str_value, struct ly_err_item **err)
62 {
63  LY_ERR ret = LY_SUCCESS;
64  uint32_t cur_idx = 0, str_len = 0;
65  enum lyxp_token cur_tok;
66  char *str_tok;
67  void *mem;
68  const char *cur_exp_ptr;
69  ly_bool is_nt;
70  const struct lys_module *context_mod = NULL;
71 
72  *str_value = NULL;
73 
74  while (cur_idx < val->keys->used) {
75  cur_tok = val->keys->tokens[cur_idx];
76  cur_exp_ptr = val->keys->expr + val->keys->tok_pos[cur_idx];
77 
78  if ((cur_tok == LYXP_TOKEN_NAMETEST) || (cur_tok == LYXP_TOKEN_LITERAL)) {
79  /* tokens that may include prefixes, get them in the target format */
80  is_nt = (cur_tok == LYXP_TOKEN_NAMETEST) ? 1 : 0;
81  LY_CHECK_GOTO(ret = lyplg_type_xpath10_print_token(cur_exp_ptr, val->keys->tok_len[cur_idx], is_nt, &context_mod,
82  val->ctx, val->format, val->prefix_data, format, prefix_data, &str_tok, err), error);
83 
84  /* append the converted token */
85  mem = realloc(*str_value, str_len + strlen(str_tok) + 1);
86  LY_CHECK_ERR_GOTO(!mem, free(str_tok), error_mem);
87  *str_value = mem;
88  str_len += sprintf(*str_value + str_len, "%s", str_tok);
89  free(str_tok);
90 
91  /* token processed */
92  ++cur_idx;
93  } else {
94  /* just copy the token */
95  mem = realloc(*str_value, str_len + val->keys->tok_len[cur_idx] + 1);
96  LY_CHECK_GOTO(!mem, error_mem);
97  *str_value = mem;
98  str_len += sprintf(*str_value + str_len, "%.*s", (int)val->keys->tok_len[cur_idx], cur_exp_ptr);
99 
100  /* token processed */
101  ++cur_idx;
102  }
103  }
104 
105  return LY_SUCCESS;
106 
107 error_mem:
108  ret = ly_err_new(err, LY_EMEM, LYVE_DATA, NULL, NULL, "No memory.");
109 
110 error:
111  free(*str_value);
112  return ret;
113 }
114 
118 static LY_ERR
119 lyplg_type_store_instanceid_keys(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, size_t value_len,
120  uint32_t options, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, const struct lysc_node *UNUSED(ctx_node),
121  struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres), struct ly_err_item **err)
122 {
123  LY_ERR ret = LY_SUCCESS;
124  struct lysc_type_str *type_str = (struct lysc_type_str *)type;
126  uint32_t log_opts = LY_LOSTORE;
127  char *canon;
128 
129  /* init storage */
130  memset(storage, 0, sizeof *storage);
131  LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
132  LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
133  storage->realtype = type;
134 
135  /* check hints */
136  ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
137  LY_CHECK_GOTO(ret, cleanup);
138 
139  /* length restriction of the string */
140  if (type_str->length) {
141  /* value_len is in bytes, but we need number of characters here */
142  ret = lyplg_type_validate_range(LY_TYPE_STRING, type_str->length, ly_utf8len(value, value_len), value, value_len, err);
143  LY_CHECK_GOTO(ret, cleanup);
144  }
145 
146  /* pattern restrictions */
147  ret = lyplg_type_validate_patterns(type_str->patterns, value, value_len, err);
148  LY_CHECK_GOTO(ret, cleanup);
149 
150  /* parse instance-identifier keys, with optional prefix even though it should be mandatory */
151  if (value_len && (((char *)value)[0] != '[')) {
152  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid first character '%c', list key predicates expected.",
153  ((char *)value)[0]);
154  goto cleanup;
155  }
156 
157  /* do not log */
158  ly_temp_log_options(&log_opts);
159  ret = ly_path_parse_predicate(ctx, NULL, value_len ? value : "", value_len, LY_PATH_PREFIX_OPTIONAL,
160  LY_PATH_PRED_KEYS, &val->keys);
161  ly_temp_log_options(NULL);
162  if (ret) {
163  ret = ly_err_new(err, ret, LYVE_DATA, NULL, NULL, "%s", ly_errmsg(ctx));
164  ly_err_clean((struct ly_ctx *)ctx, NULL);
165  goto cleanup;
166  }
167  val->ctx = ctx;
168 
169  /* store format-specific data and context for later prefix resolution */
170  ret = lyplg_type_prefix_data_new(ctx, value, value_len, format, prefix_data, &val->format, &val->prefix_data);
171  LY_CHECK_GOTO(ret, cleanup);
172 
173  switch (format) {
174  case LY_VALUE_CANON:
175  case LY_VALUE_JSON:
176  case LY_VALUE_LYB:
177  case LY_VALUE_STR_NS:
178  /* store canonical value */
179  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
180  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
181  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
182  LY_CHECK_GOTO(ret, cleanup);
183  } else {
184  ret = lydict_insert(ctx, value_len ? value : "", value_len, &storage->_canonical);
185  LY_CHECK_GOTO(ret, cleanup);
186  }
187  break;
188  case LY_VALUE_SCHEMA:
190  case LY_VALUE_XML:
191  /* JSON format with prefix is the canonical one */
192  ret = instanceid_keys_print_value(val, LY_VALUE_JSON, NULL, &canon, err);
193  LY_CHECK_GOTO(ret, cleanup);
194 
195  ret = lydict_insert_zc(ctx, canon, &storage->_canonical);
196  LY_CHECK_GOTO(ret, cleanup);
197  break;
198  }
199 
200 cleanup:
201  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
202  free((void *)value);
203  }
204 
205  if (ret) {
206  lyplg_type_free_xpath10(ctx, storage);
207  }
208  return ret;
209 }
210 
219  {
220  .module = "yang",
221  .revision = NULL,
222  .name = "instance-identifier-keys",
223 
224  .plugin.id = "libyang 2 - instance-identifier-keys, version 1",
225  .plugin.store = lyplg_type_store_instanceid_keys,
226  .plugin.validate = NULL,
227  .plugin.compare = lyplg_type_compare_simple,
228  .plugin.sort = NULL,
229  .plugin.print = lyplg_type_print_xpath10,
230  .plugin.duplicate = lyplg_type_dup_xpath10,
231  .plugin.free = lyplg_type_free_xpath10,
232  .plugin.lyb_data_len = -1,
233  },
234  {0}
235 };
struct lysc_type * realtype
Definition: tree_data.h:564
LIBYANG_API_DECL LY_ERR lyplg_type_compare_simple(const struct lyd_value *val1, const struct lyd_value *val2)
Implementation of lyplg_type_compare_clb for a generic simple type.
Compiled YANG data node.
Definition: tree_schema.h:1414
struct ly_ctx * ctx
Definition: tree_schema.h:2101
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:43
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.
Definition: log.h:256
uint8_t ly_bool
Type to indicate boolean value.
Definition: log.h:28
LIBYANG_API_DECL const char * ly_errmsg(const struct ly_ctx *ctx)
Get the last (thread, context-specific) error message. If the coresponding module defined a specific ...
#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().
The main libyang public header.
struct lysc_pattern ** patterns
Definition: tree_schema.h:1327
struct lysc_range * length
Definition: tree_schema.h:1326
YANG data representation.
Definition: tree_data.h:560
const char * _canonical
Definition: tree_data.h:561
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.
Libyang full error structure.
Definition: log.h:299
Definition: log.h:291
Definition: log.h:262
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_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:472
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...
Available YANG schema tree structures representing YANG module.
Definition: tree_schema.h:2100
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&#39;s hints (if any) in the specified format.
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:433
const char * module
LIBYANG_API_DECL void ly_err_clean(struct ly_ctx *ctx, struct ly_err_item *eitem)
Free error structures from a context.
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, only a reference counter is incremented and no memory allocation is performed. This insert function variant avoids duplication of specified value - it is inserted into the dictionary directly.
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:504
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition: tree.h:234
#define LYPLG_TYPE_VAL_INLINE_PREPARE(storage, type_val)
Prepare value memory for storing a specific type value, may be allocated dynamically.
LY_DATA_TYPE basetype
Definition: tree_schema.h:1299
LY_ERR
libyang&#39;s error codes returned by the libyang functions.
Definition: log.h:254
struct lyplg_type_record plugins_instanceid_keys[]
Plugin information for instance-identifier type implementation.
API for (user) types plugins.
#define LY_LOSTORE
Definition: log.h:119
Special lyd_value structure for yang instance-identifier-keys values.
libyang context handler.
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.