libyang  4.0.0
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
hex_string.c
Go to the documentation of this file.
1 
15 #define _GNU_SOURCE
16 
17 #include "plugins_types.h"
18 
19 #include <ctype.h>
20 #include <stdint.h>
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #include "libyang.h"
25 
26 /* additional internal headers for some useful simple macros */
27 #include "compat.h"
28 #include "ly_common.h"
29 #include "plugins_internal.h" /* LY_TYPE_*_STR */
30 
40 LIBYANG_API_DEF LY_ERR
41 lyplg_type_store_hex_string(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, uint32_t value_size_bits,
42  uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
43  const struct lysc_node *UNUSED(ctx_node), const struct lysc_ext_instance *UNUSED(top_ext),
44  struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres), struct ly_err_item **err)
45 {
46  LY_ERR ret = LY_SUCCESS;
47  uint32_t value_size, i;
48 
49  /* init storage */
50  memset(storage, 0, sizeof *storage);
51  storage->realtype = type;
52 
53  /* check value length */
54  ret = lyplg_type_check_value_size("hex-string", format, value_size_bits, LYPLG_LYB_SIZE_VARIABLE_BYTES, 0,
55  &value_size, err);
56  LY_CHECK_GOTO(ret, cleanup);
57 
58  /* check hints */
59  ret = lyplg_type_check_hints(hints, value, value_size, type->basetype, NULL, err);
60  LY_CHECK_GOTO(ret, cleanup);
61 
62  /* make a copy, it is needed for canonization */
63  if ((format != LY_VALUE_CANON) && !(options & LYPLG_TYPE_STORE_DYNAMIC)) {
64  value = strndup(value, value_size);
65  LY_CHECK_ERR_GOTO(!value, ret = LY_EMEM, cleanup);
66  options |= LYPLG_TYPE_STORE_DYNAMIC;
67  }
68 
69  /* store canonical value */
70  if (format != LY_VALUE_CANON) {
71  /* make lowercase and store, the value must be dynamic */
72  for (i = 0; i < value_size; ++i) {
73  ((char *)value)[i] = tolower(((char *)value)[i]);
74  }
75 
76  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
77  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
78  LY_CHECK_GOTO(ret, cleanup);
79 
80  /* value may have been freed */
81  value = storage->_canonical;
82  } else {
83  /* store directly */
84  ret = lydict_insert(ctx, value_size ? value : "", value_size, &storage->_canonical);
85  LY_CHECK_GOTO(ret, cleanup);
86  }
87 
88  if (!(options & LYPLG_TYPE_STORE_ONLY)) {
89  /* validate value */
90  ret = lyplg_type_validate_value_string(ctx, type, storage, err);
91  LY_CHECK_GOTO(ret, cleanup);
92  }
93 
94 cleanup:
95  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
96  free((void *)value);
97  }
98 
99  if (ret) {
100  lyplg_type_free_simple(ctx, storage);
101  }
102  return ret;
103 }
104 
113  {
114  .module = "ietf-yang-types",
115  .revision = "2013-07-15",
116  .name = "phys-address",
117 
118  .plugin.id = "ly2 hex-string",
119  .plugin.lyb_size = lyplg_type_lyb_size_variable_bytes,
120  .plugin.store = lyplg_type_store_hex_string,
121  .plugin.validate_value = lyplg_type_validate_value_string,
122  .plugin.validate_tree = NULL,
123  .plugin.compare = lyplg_type_compare_simple,
124  .plugin.sort = lyplg_type_sort_simple,
125  .plugin.print = lyplg_type_print_simple,
126  .plugin.duplicate = lyplg_type_dup_simple,
127  .plugin.free = lyplg_type_free_simple,
128  },
129  {
130  .module = "ietf-yang-types",
131  .revision = "2013-07-15",
132  .name = "mac-address",
133 
134  .plugin.id = "ly2 hex-string",
135  .plugin.lyb_size = lyplg_type_lyb_size_variable_bytes,
136  .plugin.store = lyplg_type_store_hex_string,
137  .plugin.validate_value = lyplg_type_validate_value_string,
138  .plugin.validate_tree = NULL,
139  .plugin.compare = lyplg_type_compare_simple,
140  .plugin.sort = lyplg_type_sort_simple,
141  .plugin.print = lyplg_type_print_simple,
142  .plugin.duplicate = lyplg_type_dup_simple,
143  .plugin.free = lyplg_type_free_simple,
144  },
145  {
146  .module = "ietf-yang-types",
147  .revision = "2013-07-15",
148  .name = "hex-string",
149 
150  .plugin.id = "ly2 hex-string",
151  .plugin.lyb_size = lyplg_type_lyb_size_variable_bytes,
152  .plugin.store = lyplg_type_store_hex_string,
153  .plugin.validate_value = lyplg_type_validate_value_string,
154  .plugin.validate_tree = NULL,
155  .plugin.compare = lyplg_type_compare_simple,
156  .plugin.sort = lyplg_type_sort_simple,
157  .plugin.print = lyplg_type_print_simple,
158  .plugin.duplicate = lyplg_type_dup_simple,
159  .plugin.free = lyplg_type_free_simple,
160  },
161  {
162  .module = "ietf-yang-types",
163  .revision = "2013-07-15",
164  .name = "uuid",
165 
166  .plugin.id = "ly2 hex-string",
167  .plugin.lyb_size = lyplg_type_lyb_size_variable_bytes,
168  .plugin.store = lyplg_type_store_hex_string,
169  .plugin.validate_value = lyplg_type_validate_value_string,
170  .plugin.validate_tree = NULL,
171  .plugin.compare = lyplg_type_compare_simple,
172  .plugin.sort = lyplg_type_sort_simple,
173  .plugin.print = lyplg_type_print_simple,
174  .plugin.duplicate = lyplg_type_dup_simple,
175  .plugin.free = lyplg_type_free_simple,
176  },
177  {0}
178 };
struct lysc_type * realtype
Definition: tree_data.h:567
Compiled YANG data node.
Definition: tree_schema.h:1434
memset(value->fixed_mem, 0, LYD_VALUE_FIXED_MEM_SIZE)
LIBYANG_API_DECL LY_ERR lyplg_type_check_value_size(const char *type_name, LY_VALUE_FORMAT format, uint32_t value_size_bits, enum lyplg_lyb_size_type lyb_size_type, uint32_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.
YANG extension compiled instance.
Definition: plugins_exts.h:436
LIBYANG_API_DECL const void * lyplg_type_print_simple(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format, void *prefix_data, ly_bool *dynamic, uint32_t *value_size_bits)
Implementation of lyplg_type_print_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.
LY_ERR
libyang&#39;s error codes returned by the libyang functions.
Definition: log.h:240
#define LYPLG_TYPE_STORE_DYNAMIC
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.
Definition: log.h:242
The main libyang public header.
LIBYANG_API_DECL void lyplg_type_lyb_size_variable_bytes(const struct lysc_type *type, enum lyplg_lyb_size_type *size_type, uint32_t *fixed_size_bits)
Implementation of lyplg_type_lyb_size_clb for a type with variable length rounded to bytes...
YANG data representation.
Definition: tree_data.h:563
const char * _canonical
Definition: tree_data.h:564
Libyang full error structure.
Definition: log.h:285
LIBYANG_API_DECL LY_ERR lyplg_type_validate_value_string(const struct ly_ctx *ctx, const struct lysc_type *type, struct lyd_value *storage, struct ly_err_item **err)
Implementation of lyplg_type_validate_value_clb for the string type.
Definition: string.c:118
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...
const char * module
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.
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition: tree.h:234
struct lyplg_type_record plugins_hex_string[]
Plugin information for string type implementation.
Definition: hex_string.c:112
LY_DATA_TYPE basetype
Definition: tree_schema.h:1300
LIBYANG_API_DEF LY_ERR lyplg_type_store_hex_string(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, uint32_t value_size_bits, uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints, const struct lysc_node *UNUSED(ctx_node), const struct lysc_ext_instance *UNUSED(top_ext), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres), struct ly_err_item **err)
Definition: hex_string.c:41
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_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&#39;s hints (if any) in the specified format.
API for (user) types plugins.
libyang context handler.
#define LYPLG_TYPE_STORE_ONLY