libyang  6.4.33
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
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 "compat.h"
22 #include "dict.h"
23 #include "ly_common.h"
24 #include "path.h"
25 #include "plugins_internal.h"
26 #include "utils.h"
27 #include "xpath.h"
28 
42  struct lyxp_expr *keys;
43  const struct ly_ctx *ctx;
44  void *prefix_data;
46 };
47 
58 static LY_ERR
59 instanceid_keys_print_value(const struct lyd_value_instance_identifier_keys *val, LY_VALUE_FORMAT format, void *prefix_data,
60  char **str_value, struct ly_err_item **err)
61 {
62  LY_ERR ret = LY_SUCCESS;
63  uint32_t cur_idx = 0, str_size = 0, str_len = 0;
64  enum lyxp_token cur_tok;
65  char *str_tok;
66  const char *cur_exp_ptr;
67  ly_bool is_nt;
68  const struct lys_module *local_mod = NULL, *ctx_mod = NULL;
69  const struct lysc_node *ctx_node = NULL;
70  struct ly_set *mods;
71 
72  *str_value = NULL;
73 
74  if (format == LY_VALUE_XML) {
75  /* null the local module so that all the prefixes are printed */
76  mods = prefix_data;
77  local_mod = mods->objs[0];
78  mods->objs[0] = NULL;
79  }
80 
81  while (cur_idx < val->keys->used) {
82  cur_tok = val->keys->tokens[cur_idx];
83  cur_exp_ptr = val->keys->expr + val->keys->tok_pos[cur_idx];
84 
85  if ((cur_tok == LYXP_TOKEN_NAMETEST) || (cur_tok == LYXP_TOKEN_LITERAL)) {
86  /* tokens that may include prefixes, get them in the target format */
87  is_nt = (cur_tok == LYXP_TOKEN_NAMETEST) ? 1 : 0;
88  LY_CHECK_GOTO(ret = lyplg_type_xpath10_print_token(cur_exp_ptr, val->keys->tok_len[cur_idx], is_nt, val->ctx,
89  val->format, val->prefix_data, format, prefix_data, &ctx_mod, &ctx_node, &str_tok, err), cleanup);
90 
91  /* append the converted token */
92  ret = ly_append_str(str_value, &str_size, &str_len, "%s", str_tok);
93  free(str_tok);
94  LY_CHECK_ERR_GOTO(ret, ret = ly_err_new_no_mem(err), cleanup);
95 
96  /* token processed */
97  ++cur_idx;
98  } else {
99  /* just copy the token */
100  ret = ly_append_str(str_value, &str_size, &str_len, "%.*s", (int)val->keys->tok_len[cur_idx], cur_exp_ptr);
101  LY_CHECK_ERR_GOTO(ret, ret = ly_err_new_no_mem(err), cleanup);
102 
103  /* token processed */
104  ++cur_idx;
105  }
106  }
107 
108 cleanup:
109  if (local_mod) {
110  mods->objs[0] = (void *)local_mod;
111  }
112  if (ret) {
113  free(*str_value);
114  *str_value = NULL;
115  }
116  return ret;
117 }
118 
122 static LY_ERR
123 lyplg_type_store_instanceid_keys(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, uint64_t value_size_bits,
124  uint32_t options, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, const struct lysc_node *UNUSED(ctx_node),
125  struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres), struct ly_err_item **err)
126 {
127  LY_ERR ret = LY_SUCCESS;
128  struct lysc_type_str *type_str = (struct lysc_type_str *)type;
130  uint32_t value_size, *prev_lo, temp_lo = LY_LOSTORE;
131  const struct ly_err_item *eitem;
132  char *canon;
133 
134  /* init storage */
135  memset(storage, 0, sizeof *storage);
136  LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
137  LY_CHECK_ERR_GOTO(!val, ret = ly_err_new_no_mem(err), cleanup);
138  storage->realtype = type;
139 
140  /* check value length */
141  ret = lyplg_type_check_value_size("instance-identifier-keys", format, value_size_bits,
142  LYPLG_LYB_SIZE_VARIABLE_BYTES, 0, &value_size, err);
143  LY_CHECK_GOTO(ret, cleanup);
144 
145  /* check hints */
146  ret = lyplg_type_check_hints(hints, value, value_size, type->basetype, NULL, err);
147  LY_CHECK_GOTO(ret, cleanup);
148 
149  /* length restriction of the string */
150  if (type_str->length) {
151  /* value_size is in bytes, but we need number of characters here */
152  ret = lyplg_type_validate_range(LY_TYPE_STRING, type_str->length, ly_utf8len(value, value_size), value, value_size, err);
153  LY_CHECK_GOTO(ret, cleanup);
154  }
155 
156  /* pattern restrictions */
157  ret = lyplg_type_validate_patterns(ctx, type_str->patterns, value, value_size, err);
158  LY_CHECK_GOTO(ret, cleanup);
159 
160  /* parse instance-identifier keys, with optional prefix even though it should be mandatory */
161  if (value_size && (((char *)value)[0] != '[')) {
162  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid first character '%c', list key predicates expected.",
163  ((char *)value)[0]);
164  goto cleanup;
165  }
166 
167  /* do not log */
168  prev_lo = ly_temp_log_options(&temp_lo);
169  ret = ly_path_parse_predicate(ctx, NULL, value_size ? value : "", value_size, LY_PATH_PREFIX_OPTIONAL,
170  LY_PATH_PRED_KEYS, &val->keys);
171  ly_temp_log_options(prev_lo);
172  if (ret) {
173  eitem = ly_err_last(ctx);
174  ret = ly_err_new(err, ret, LYVE_DATA, eitem->data_path, NULL, "%s", eitem->msg);
175  ly_err_clean((struct ly_ctx *)ctx, NULL);
176  goto cleanup;
177  }
178  val->ctx = ctx;
179 
180  /* store format-specific data and context for later prefix resolution */
181  ret = lyplg_type_prefix_data_new(ctx, value, value_size, format, prefix_data, &val->format, &val->prefix_data);
182  LY_CHECK_GOTO(ret, cleanup);
183 
184  switch (format) {
185  case LY_VALUE_CANON:
186  case LY_VALUE_CBOR:
187  case LY_VALUE_JSON:
188  case LY_VALUE_LYB:
189  case LY_VALUE_STR_NS:
190  /* store canonical value */
191  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
192  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
193  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
194  LY_CHECK_GOTO(ret, cleanup);
195  } else {
196  ret = lydict_insert(ctx, value_size ? value : "", value_size, &storage->_canonical);
197  LY_CHECK_GOTO(ret, cleanup);
198  }
199  break;
200  case LY_VALUE_SCHEMA:
202  case LY_VALUE_XML:
203  /* JSON format with prefix is the canonical one */
204  ret = instanceid_keys_print_value(val, LY_VALUE_JSON, NULL, &canon, err);
205  LY_CHECK_GOTO(ret, cleanup);
206 
207  ret = lydict_insert_zc(ctx, canon, &storage->_canonical);
208  LY_CHECK_GOTO(ret, cleanup);
209  break;
210  }
211 
212 cleanup:
213  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
214  free((void *)value);
215  }
216 
217  if (ret) {
218  lyplg_type_free_xpath10(ctx, storage);
219  }
220  return ret;
221 }
222 
231  {
232  .module = "yang",
233  .revision = NULL,
234  .name = "instance-identifier-keys",
235 
236  .plugin.id = "ly2 instance-identifier-keys",
237  .plugin.lyb_size = lyplg_type_lyb_size_variable_bytes,
238  .plugin.store = lyplg_type_store_instanceid_keys,
239  .plugin.validate_value = NULL,
240  .plugin.validate_tree = NULL,
241  .plugin.compare = lyplg_type_compare_simple,
242  .plugin.sort = lyplg_type_sort_simple,
243  .plugin.print = lyplg_type_print_xpath10,
244  .plugin.duplicate = lyplg_type_dup_xpath10,
245  .plugin.free = lyplg_type_free_xpath10,
246  },
247  {0}
248 };
libyang dictionary
libyang context handler.
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_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 err
Definition: log.h:300
char * data_path
Definition: log.h:303
char * msg
Definition: log.h:302
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
libyang's error codes returned by the libyang functions.
Definition: log.h:252
LIBYANG_API_DECL void ly_err_clean(const struct ly_ctx *ctx, struct ly_err_item *eitem)
Free error structures from a context.
@ LYVE_DATA
Definition: log.h:290
@ LY_EVALID
Definition: log.h:260
@ LY_SUCCESS
Definition: log.h:253
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().
#define LY_LOSTORE
Definition: log.h:121
Structure to hold a set of (not necessary somehow connected) objects. Usually used for lyd_node,...
Definition: set.h:47
const char * module
#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 LY_ERR lyplg_type_check_hints(uint32_t hints, const char *value, uint32_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.
LIBYANG_API_DECL LY_ERR lyplg_type_validate_patterns(const struct ly_ctx *ctx, struct lysc_pattern **patterns, const char *str, uint32_t str_len, struct ly_err_item **err)
Data type validator for pattern-restricted string values.
LIBYANG_API_DECL LY_ERR lyplg_type_validate_range(LY_DATA_TYPE basetype, struct lysc_range *range, int64_t value, const char *strval, uint32_t strval_len, struct ly_err_item **err)
Data type validator for a range/length-restricted values.
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:533
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 lyplg_type_check_value_size(const char *type_name, LY_VALUE_FORMAT format, uint64_t value_size_bits, enum lyplg_lyb_size_type lyb_size_type, uint64_t lyb_fixed_size_bits, uint32_t *value_size, struct ly_err_item **err)
Check a value type in bits is correct and as expected.
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, uint64_t *value_size_bits)
Implementation of lyplg_type_print_clb for the ietf-yang-types xpath1.0 type.
Definition: xpath1.0.c:494
LIBYANG_API_DECL LY_ERR lyplg_type_xpath10_print_token(const char *token, uint16_t tok_len, ly_bool is_nametest, 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, const struct lys_module **ctx_mod, const struct lysc_node **ctx_node, char **token_p, struct ly_err_item **err)
Print xpath1.0 token in the specific format.
Definition: xpath1.0.c:41
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 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:565
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.
@ LYPLG_LYB_SIZE_VARIABLE_BYTES
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.
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.
LIBYANG_API_DECL void lyplg_type_lyb_size_variable_bytes(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 rounded to bytes.
#define LYPLG_TYPE_STORE_DYNAMIC
LY_DATA_TYPE basetype
Definition: tree_schema.h:1278
struct lysc_pattern ** patterns
Definition: tree_schema.h:1312
struct lysc_range * length
Definition: tree_schema.h:1311
Available YANG schema tree structures representing YANG module.
Definition: tree_schema.h:2242
Compiled YANG data node.
Definition: tree_schema.h:1412
const struct lyplg_type_record plugins_instanceid_keys[]
Plugin information for instance-identifier type implementation.
Special lyd_value structure for yang instance-identifier-keys values.
uint8_t ly_bool
Type to indicate boolean value.
Definition: log.h:29
API for (user) types plugins.
const struct lysc_type * realtype
Definition: tree_data.h:614
const char * _canonical
Definition: tree_data.h:611
YANG data representation.
Definition: tree_data.h:610
Utilities and generic types of libyang.
@ LY_TYPE_STRING
Definition: utils.h:68
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition: utils.h:93
@ LY_VALUE_JSON
Definition: utils.h:98
@ LY_VALUE_SCHEMA
Definition: utils.h:95
@ LY_VALUE_CBOR
Definition: utils.h:99
@ LY_VALUE_CANON
Definition: utils.h:94
@ LY_VALUE_XML
Definition: utils.h:97
@ LY_VALUE_STR_NS
Definition: utils.h:101
@ LY_VALUE_SCHEMA_RESOLVED
Definition: utils.h:96
@ LY_VALUE_LYB
Definition: utils.h:100