libyang  6.4.7
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 "compat.h"
25 #include "dict.h"
26 #include "ly_common.h"
27 #include "plugins_internal.h"
28 
38 LIBYANG_API_DEF LY_ERR
39 lyplg_type_store_hex_string(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, uint64_t value_size_bits,
40  uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
41  const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
42  struct ly_err_item **err)
43 {
44  LY_ERR ret = LY_SUCCESS;
45  uint32_t value_size, i;
46 
47  /* init storage */
48  memset(storage, 0, sizeof *storage);
49  storage->realtype = type;
50 
51  /* check value length */
52  ret = lyplg_type_check_value_size("hex-string", format, value_size_bits, LYPLG_LYB_SIZE_VARIABLE_BYTES, 0,
53  &value_size, err);
54  LY_CHECK_GOTO(ret, cleanup);
55 
56  /* check hints */
57  ret = lyplg_type_check_hints(hints, value, value_size, type->basetype, NULL, err);
58  LY_CHECK_GOTO(ret, cleanup);
59 
60  /* make a copy, it is needed for canonization */
61  if ((format != LY_VALUE_CANON) && !(options & LYPLG_TYPE_STORE_DYNAMIC)) {
62  value = strndup(value, value_size);
63  LY_CHECK_ERR_GOTO(!value, ret = LY_EMEM, cleanup);
64  options |= LYPLG_TYPE_STORE_DYNAMIC;
65  }
66 
67  /* store canonical value */
68  if (format != LY_VALUE_CANON) {
69  /* make lowercase and store, the value must be dynamic */
70  for (i = 0; i < value_size; ++i) {
71  ((char *)value)[i] = tolower(((char *)value)[i]);
72  }
73 
74  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
75  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
76  LY_CHECK_GOTO(ret, cleanup);
77 
78  /* value may have been freed */
79  value = storage->_canonical;
80  } else {
81  /* store directly */
82  ret = lydict_insert(ctx, value_size ? value : "", value_size, &storage->_canonical);
83  LY_CHECK_GOTO(ret, cleanup);
84  }
85 
86  if (!(options & LYPLG_TYPE_STORE_ONLY)) {
87  /* validate value */
88  ret = lyplg_type_validate_value_string(ctx, type, storage, err);
89  LY_CHECK_GOTO(ret, cleanup);
90  }
91 
92 cleanup:
93  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
94  free((void *)value);
95  }
96 
97  if (ret) {
98  lyplg_type_free_simple(ctx, storage);
99  }
100  return ret;
101 }
102 
111  {
112  .module = "ietf-yang-types",
113  .revision = NULL,
114  .name = "phys-address",
115 
116  .plugin.id = "ly2 hex-string",
117  .plugin.lyb_size = lyplg_type_lyb_size_variable_bytes,
118  .plugin.store = lyplg_type_store_hex_string,
119  .plugin.validate_value = lyplg_type_validate_value_string,
120  .plugin.validate_tree = NULL,
121  .plugin.compare = lyplg_type_compare_simple,
122  .plugin.sort = lyplg_type_sort_simple,
123  .plugin.print = lyplg_type_print_simple,
124  .plugin.duplicate = lyplg_type_dup_simple,
125  .plugin.free = lyplg_type_free_simple,
126  },
127  {
128  .module = "ietf-yang-types",
129  .revision = NULL,
130  .name = "mac-address",
131 
132  .plugin.id = "ly2 hex-string",
133  .plugin.lyb_size = lyplg_type_lyb_size_variable_bytes,
134  .plugin.store = lyplg_type_store_hex_string,
135  .plugin.validate_value = lyplg_type_validate_value_string,
136  .plugin.validate_tree = NULL,
137  .plugin.compare = lyplg_type_compare_simple,
138  .plugin.sort = lyplg_type_sort_simple,
139  .plugin.print = lyplg_type_print_simple,
140  .plugin.duplicate = lyplg_type_dup_simple,
141  .plugin.free = lyplg_type_free_simple,
142  },
143  {
144  .module = "ietf-yang-types",
145  .revision = NULL,
146  .name = "hex-string",
147 
148  .plugin.id = "ly2 hex-string",
149  .plugin.lyb_size = lyplg_type_lyb_size_variable_bytes,
150  .plugin.store = lyplg_type_store_hex_string,
151  .plugin.validate_value = lyplg_type_validate_value_string,
152  .plugin.validate_tree = NULL,
153  .plugin.compare = lyplg_type_compare_simple,
154  .plugin.sort = lyplg_type_sort_simple,
155  .plugin.print = lyplg_type_print_simple,
156  .plugin.duplicate = lyplg_type_dup_simple,
157  .plugin.free = lyplg_type_free_simple,
158  },
159  {
160  .module = "ietf-yang-types",
161  .revision = NULL,
162  .name = "uuid",
163 
164  .plugin.id = "ly2 hex-string",
165  .plugin.lyb_size = lyplg_type_lyb_size_variable_bytes,
166  .plugin.store = lyplg_type_store_hex_string,
167  .plugin.validate_value = lyplg_type_validate_value_string,
168  .plugin.validate_tree = NULL,
169  .plugin.compare = lyplg_type_compare_simple,
170  .plugin.sort = lyplg_type_sort_simple,
171  .plugin.print = lyplg_type_print_simple,
172  .plugin.duplicate = lyplg_type_dup_simple,
173  .plugin.free = lyplg_type_free_simple,
174  },
175  {0}
176 };
struct lysc_type * realtype
Definition: tree_data.h:614
Compiled YANG data node.
Definition: tree_schema.h:1430
memset(value->fixed_mem, 0, LYD_VALUE_FIXED_MEM_SIZE)
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...
LIBYANG_API_DEF LY_ERR lyplg_type_store_hex_string(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, uint64_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), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres), struct ly_err_item **err)
Definition: hex_string.c:39
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:255
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 dictionary
#define LYPLG_TYPE_STORE_DYNAMIC
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, uint64_t *value_size_bits)
Implementation of lyplg_type_print_clb for a generic simple type.
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:257
YANG data representation.
Definition: tree_data.h:610
const char * _canonical
Definition: tree_data.h:611
Libyang full error structure.
Definition: log.h:301
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:116
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: utils.h:93
struct lyplg_type_record plugins_hex_string[]
Plugin information for string type implementation.
Definition: hex_string.c:110
LY_DATA_TYPE basetype
Definition: tree_schema.h:1296
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