libyang  2.2.8
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, size_t value_len,
42  uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
43  const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
44  struct ly_err_item **err)
45 {
46  LY_ERR ret = LY_SUCCESS;
47  struct lysc_type_str *type_str = (struct lysc_type_str *)type;
48  uint32_t i;
49 
50  /* init storage */
51  memset(storage, 0, sizeof *storage);
52  storage->realtype = type;
53 
54  /* check hints */
55  ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
56  LY_CHECK_GOTO(ret, cleanup);
57 
58  /* length restriction of the string */
59  if (type_str->length) {
60  /* value_len is in bytes, but we need number of characters here */
61  ret = lyplg_type_validate_range(LY_TYPE_STRING, type_str->length, ly_utf8len(value, value_len), value, value_len, err);
62  LY_CHECK_GOTO(ret, cleanup);
63  }
64 
65  /* pattern restrictions */
66  ret = lyplg_type_validate_patterns(type_str->patterns, value, value_len, err);
67  LY_CHECK_GOTO(ret, cleanup);
68 
69  /* make a copy, it is needed for canonization */
70  if ((format != LY_VALUE_CANON) && !(options & LYPLG_TYPE_STORE_DYNAMIC)) {
71  value = strndup(value, value_len);
72  LY_CHECK_ERR_GOTO(!value, ret = LY_EMEM, cleanup);
73  options |= LYPLG_TYPE_STORE_DYNAMIC;
74  }
75 
76  /* store canonical value */
77  if (format != LY_VALUE_CANON) {
78  /* make lowercase and store, the value must be dynamic */
79  for (i = 0; i < value_len; ++i) {
80  ((char *)value)[i] = tolower(((char *)value)[i]);
81  }
82 
83  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
84  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
85  LY_CHECK_GOTO(ret, cleanup);
86  } else {
87  /* store directly */
88  ret = lydict_insert(ctx, value_len ? value : "", value_len, &storage->_canonical);
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 = "2013-07-15",
114  .name = "phys-address",
115 
116  .plugin.id = "libyang 2 - hex-string, version 1",
117  .plugin.store = lyplg_type_store_hex_string,
118  .plugin.validate = NULL,
119  .plugin.compare = lyplg_type_compare_simple,
120  .plugin.sort = lyplg_type_sort_simple,
121  .plugin.print = lyplg_type_print_simple,
122  .plugin.duplicate = lyplg_type_dup_simple,
123  .plugin.free = lyplg_type_free_simple,
124  .plugin.lyb_data_len = -1,
125  },
126  {
127  .module = "ietf-yang-types",
128  .revision = "2013-07-15",
129  .name = "mac-address",
130 
131  .plugin.id = "libyang 2 - hex-string, version 1",
132  .plugin.store = lyplg_type_store_hex_string,
133  .plugin.validate = NULL,
134  .plugin.compare = lyplg_type_compare_simple,
135  .plugin.sort = lyplg_type_sort_simple,
136  .plugin.print = lyplg_type_print_simple,
137  .plugin.duplicate = lyplg_type_dup_simple,
138  .plugin.free = lyplg_type_free_simple,
139  .plugin.lyb_data_len = -1,
140  },
141  {
142  .module = "ietf-yang-types",
143  .revision = "2013-07-15",
144  .name = "hex-string",
145 
146  .plugin.id = "libyang 2 - hex-string, version 1",
147  .plugin.store = lyplg_type_store_hex_string,
148  .plugin.validate = NULL,
149  .plugin.compare = lyplg_type_compare_simple,
150  .plugin.sort = lyplg_type_sort_simple,
151  .plugin.print = lyplg_type_print_simple,
152  .plugin.duplicate = lyplg_type_dup_simple,
153  .plugin.free = lyplg_type_free_simple,
154  .plugin.lyb_data_len = -1,
155  },
156  {
157  .module = "ietf-yang-types",
158  .revision = "2013-07-15",
159  .name = "uuid",
160 
161  .plugin.id = "libyang 2 - hex-string, version 1",
162  .plugin.store = lyplg_type_store_hex_string,
163  .plugin.validate = NULL,
164  .plugin.compare = lyplg_type_compare_simple,
165  .plugin.sort = lyplg_type_sort_simple,
166  .plugin.print = lyplg_type_print_simple,
167  .plugin.duplicate = lyplg_type_dup_simple,
168  .plugin.free = lyplg_type_free_simple,
169  .plugin.lyb_data_len = -1,
170  },
171  {0}
172 };
struct lysc_type * realtype
Definition: tree_data.h:575
Compiled YANG data node.
Definition: tree_schema.h:1439
LIBYANG_API_DEF LY_ERR lyplg_type_store_hex_string(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: hex_string.c:41
memset(value->fixed_mem, 0, LYD_VALUE_FIXED_MEM_SIZE)
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.
Definition: log.h:242
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, size_t *value_len)
Implementation of lyplg_type_print_clb for a generic simple type.
#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.
The main libyang public header.
struct lysc_pattern ** patterns
Definition: tree_schema.h:1339
struct lysc_range * length
Definition: tree_schema.h:1338
YANG data representation.
Definition: tree_data.h:571
const char * _canonical
Definition: tree_data.h:572
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:285
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 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.
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:110
LY_DATA_TYPE basetype
Definition: tree_schema.h:1305
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.
API for (user) types plugins.
LY_ERR
libyang&#39;s error codes returned by the libyang functions.
Definition: log.h:240
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.