libyang  3.13.6
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
enumeration.c
Go to the documentation of this file.
1 
15 #define _GNU_SOURCE /* strdup */
16 
17 #include "plugins_types.h"
18 
19 #include <stdint.h>
20 #include <stdlib.h>
21 #include <string.h>
22 
23 #include "libyang.h"
24 
25 /* additional internal headers for some useful simple macros */
26 #include "compat.h"
27 #include "ly_common.h"
28 #include "plugins_internal.h" /* LY_TYPE_*_STR */
29 
39 LIBYANG_API_DEF LY_ERR
40 lyplg_type_store_enum(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, size_t value_len,
41  uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
42  const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
43  struct ly_err_item **err)
44 {
45  struct lysc_type_enum *type_enum = (struct lysc_type_enum *)type;
46  LY_ERR ret = LY_SUCCESS;
48  ly_bool found = 0;
49  int64_t num = 0;
50  int32_t num_val;
51 
52  /* init storage */
53  memset(storage, 0, sizeof *storage);
54  storage->realtype = type;
55 
56  if (format == LY_VALUE_LYB) {
57  /* validation */
58  if (value_len != 4) {
59  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB enumeration value size %zu (expected 4).",
60  value_len);
61  goto cleanup;
62  }
63 
64  /* convert the value to host byte order */
65  memcpy(&num, value, value_len);
66  num = le64toh(num);
67  num_val = num;
68 
69  /* find the matching enumeration value item */
70  LY_ARRAY_FOR(type_enum->enums, u) {
71  if (type_enum->enums[u].value == num_val) {
72  found = 1;
73  break;
74  }
75  }
76 
77  if (!found) {
78  /* value not found */
79  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid enumeration value % " PRIi32 ".", num_val);
80  goto cleanup;
81  }
82 
83  /* store value */
84  storage->enum_item = &type_enum->enums[u];
85 
86  /* canonical settings via dictionary due to free callback */
87  ret = lydict_insert(ctx, type_enum->enums[u].name, 0, &storage->_canonical);
88  LY_CHECK_GOTO(ret, cleanup);
89 
90  /* success */
91  goto cleanup;
92  }
93 
94  /* check hints */
95  ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
96  LY_CHECK_GOTO(ret, cleanup);
97 
98  /* find the matching enumeration value item */
99  LY_ARRAY_FOR(type_enum->enums, u) {
100  if (!ly_strncmp(type_enum->enums[u].name, value, value_len)) {
101  found = 1;
102  break;
103  }
104  }
105 
106  if (!found) {
107  /* enum not found */
108  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid enumeration value \"%.*s\".", (int)value_len,
109  (char *)value);
110  goto cleanup;
111  }
112 
113  /* store value */
114  storage->enum_item = &type_enum->enums[u];
115 
116  /* store canonical value, it always is */
117  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
118  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
119  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
120  LY_CHECK_GOTO(ret, cleanup);
121  } else {
122  ret = lydict_insert(ctx, value, value_len, &storage->_canonical);
123  LY_CHECK_GOTO(ret, cleanup);
124  }
125 
126 cleanup:
127  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
128  free((void *)value);
129  }
130 
131  if (ret) {
132  lyplg_type_free_simple(ctx, storage);
133  }
134  return ret;
135 }
136 
137 LIBYANG_API_DEF int
138 lyplg_type_sort_enum(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
139  const struct lyd_value *val2)
140 {
141  if (val1->enum_item->value > val2->enum_item->value) {
142  return -1;
143  } else if (val1->enum_item->value < val2->enum_item->value) {
144  return 1;
145  } else {
146  return 0;
147  }
148 }
149 
150 LIBYANG_API_DEF const void *
151 lyplg_type_print_enum(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *value, LY_VALUE_FORMAT format,
152  void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
153 {
154  int64_t prev_num = 0, num = 0;
155  void *buf;
156 
157  if (format == LY_VALUE_LYB) {
158  prev_num = num = value->enum_item->value;
159  num = htole64(num);
160  if (num == prev_num) {
161  /* values are equal, little-endian */
162  *dynamic = 0;
163  if (value_len) {
164  *value_len = 4;
165  }
166  return &value->enum_item->value;
167  } else {
168  /* values differ, big-endian */
169  buf = calloc(1, 4);
170  LY_CHECK_RET(!buf, NULL);
171 
172  *dynamic = 1;
173  if (value_len) {
174  *value_len = 4;
175  }
176  memcpy(buf, &num, 4);
177  return buf;
178  }
179  }
180 
181  /* use the cached canonical value */
182  if (dynamic) {
183  *dynamic = 0;
184  }
185  if (value_len) {
186  *value_len = strlen(value->_canonical);
187  }
188  return value->_canonical;
189 }
190 
198 const struct lyplg_type_record plugins_enumeration[] = {
199  {
200  .module = "",
201  .revision = NULL,
202  .name = LY_TYPE_ENUM_STR,
203 
204  .plugin.id = "libyang 2 - enumeration, version 1",
205  .plugin.store = lyplg_type_store_enum,
206  .plugin.validate = NULL,
207  .plugin.compare = lyplg_type_compare_simple,
208  .plugin.sort = lyplg_type_sort_enum,
209  .plugin.print = lyplg_type_print_enum,
210  .plugin.duplicate = lyplg_type_dup_simple,
211  .plugin.free = lyplg_type_free_simple,
212  .plugin.lyb_data_len = 4,
213  },
214  {0}
215 };
LIBYANG_API_DEF int lyplg_type_sort_enum(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1, const struct lyd_value *val2)
Definition: enumeration.c:138
LIBYANG_API_DEF LY_ERR lyplg_type_store_enum(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 *UNUSED(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)
Definition: enumeration.c:40
const struct lyplg_type_record plugins_enumeration[]
Plugin information for enumeration type implementation.
Definition: enumeration.c:198
LIBYANG_API_DEF const void * lyplg_type_print_enum(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *value, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
Definition: enumeration.c:151
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
libyang's error codes returned by the libyang functions.
Definition: log.h:237
@ LYVE_DATA
Definition: log.h:274
@ LY_EVALID
Definition: log.h:245
@ LY_SUCCESS
Definition: log.h:238
Libyang full error structure.
Definition: log.h:282
const char * module
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.
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.
LIBYANG_API_DECL LY_ERR lyplg_type_dup_simple(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
Implementation of lyplg_type_dup_clb for a generic simple type.
LIBYANG_API_DECL void lyplg_type_free_simple(const struct ly_ctx *ctx, struct lyd_value *value)
Implementation of lyplg_type_free_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.
#define LYPLG_TYPE_STORE_DYNAMIC
LY_DATA_TYPE basetype
Definition: tree_schema.h:1287
struct lysc_type_bitenum_item * enums
Definition: tree_schema.h:1345
Compiled YANG data node.
Definition: tree_schema.h:1421
#define LY_ARRAY_FOR(ARRAY,...)
Sized-array iterator (for-loop).
Definition: tree.h:167
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition: tree.h:234
#define LY_ARRAY_COUNT_TYPE
Type (i.e. size) of the sized array's size counter.
Definition: tree.h:104
@ LY_VALUE_LYB
Definition: tree.h:240
The main libyang public header.
uint8_t ly_bool
Type to indicate boolean value.
Definition: log.h:28
API for (user) types plugins.
const struct lysc_type * realtype
Definition: tree_data.h:571
const char * _canonical
Definition: tree_data.h:568
YANG data representation.
Definition: tree_data.h:567