libyang  6.4.13
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
string.c
Go to the documentation of this file.
1 
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 "plugins_internal.h"
25 
43 static LY_ERR
44 string_check_chars(const char *value, uint32_t value_size, struct ly_err_item **err)
45 {
46  uint32_t len, parsed = 0;
47 
48  while (value_size - parsed) {
49  if (ly_checkutf8(value + parsed, value_size - parsed, &len)) {
50  return ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid character 0x%hhx.", value[parsed]);
51  }
52  parsed += len;
53  }
54 
55  return LY_SUCCESS;
56 }
57 
58 LIBYANG_API_DEF LY_ERR
59 lyplg_type_store_string(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, uint64_t value_size_bits,
60  uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
61  const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
62  struct ly_err_item **err)
63 {
64  LY_ERR ret = LY_SUCCESS;
65  uint32_t value_size;
66 
67  /* init storage */
68  memset(storage, 0, sizeof *storage);
69  storage->realtype = type;
70 
71  /* check value length */
72  ret = lyplg_type_check_value_size("string", format, value_size_bits, LYPLG_LYB_SIZE_VARIABLE_BYTES, 0, &value_size, err);
73  LY_CHECK_GOTO(ret, cleanup);
74 
75  if (!(options & LYPLG_TYPE_STORE_IS_UTF8)) {
76  /* check the UTF-8 encoding */
77  ret = string_check_chars(value, value_size, err);
78  LY_CHECK_GOTO(ret, cleanup);
79  }
80 
81  /* check hints */
82  ret = lyplg_type_check_hints(hints, value, value_size, type->basetype, NULL, err);
83  LY_CHECK_GOTO(ret, cleanup);
84 
85  /* store canonical value */
86  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
87  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
88  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
89  LY_CHECK_GOTO(ret, cleanup);
90 
91  /* value may have been freed */
92  value = storage->_canonical;
93  } else {
94  ret = lydict_insert(ctx, value_size ? value : "", value_size, &storage->_canonical);
95  LY_CHECK_GOTO(ret, cleanup);
96  }
97 
98  if (!(options & LYPLG_TYPE_STORE_ONLY)) {
99  /* validate value */
100  ret = lyplg_type_validate_value_string(ctx, type, storage, err);
101  LY_CHECK_GOTO(ret, cleanup);
102  }
103 
104 cleanup:
105  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
106  free((void *)value);
107  }
108 
109  if (ret) {
110  lyplg_type_free_simple(ctx, storage);
111  }
112  return ret;
113 }
114 
115 LIBYANG_API_DEF LY_ERR
116 lyplg_type_validate_value_string(const struct ly_ctx *ctx, const struct lysc_type *type, struct lyd_value *storage,
117  struct ly_err_item **err)
118 {
119  LY_ERR ret;
120  struct lysc_type_str *type_str = (struct lysc_type_str *)type;
121  const void *value;
122  size_t value_len;
123 
124  LY_CHECK_ARG_RET(NULL, type, storage, err, LY_EINVAL);
125  value = lyd_value_get_canonical(ctx, storage);
126  value_len = strlen(value);
127  *err = NULL;
128 
129  /* length restriction of the string */
130  if (type_str->length) {
131  /* value_len is in bytes, but we need number of characters here */
132  ret = lyplg_type_validate_range(LY_TYPE_STRING, type_str->length, ly_utf8len(value, value_len), value, value_len, err);
133  LY_CHECK_RET(ret);
134  }
135 
136  /* pattern restrictions */
137  ret = lyplg_type_validate_patterns(ctx, type_str->patterns, value, value_len, err);
138  LY_CHECK_RET(ret);
139 
140  return LY_SUCCESS;
141 }
142 
150 const struct lyplg_type_record plugins_string[] = {
151  {
152  .module = "",
153  .revision = NULL,
154  .name = LY_TYPE_STRING_STR,
155 
156  .plugin.id = "ly2 string",
157  .plugin.lyb_size = lyplg_type_lyb_size_variable_bytes,
158  .plugin.store = lyplg_type_store_string,
159  .plugin.validate_value = lyplg_type_validate_value_string,
160  .plugin.validate_tree = NULL,
161  .plugin.compare = lyplg_type_compare_simple,
162  .plugin.sort = lyplg_type_sort_simple,
163  .plugin.print = lyplg_type_print_simple,
164  .plugin.duplicate = lyplg_type_dup_simple,
165  .plugin.free = lyplg_type_free_simple,
166  },
167  {0}
168 };
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
libyang's error codes returned by the libyang functions.
Definition: log.h:252
@ LYVE_DATA
Definition: log.h:290
@ LY_EINVAL
Definition: log.h:256
@ LY_EVALID
Definition: log.h:260
@ LY_SUCCESS
Definition: log.h:253
Libyang full error structure.
Definition: log.h:298
const char * module
LIBYANG_API_DEF 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 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_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 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.
@ LYPLG_LYB_SIZE_VARIABLE_BYTES
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.
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.
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
#define LYPLG_TYPE_STORE_IS_UTF8
#define LYPLG_TYPE_STORE_ONLY
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
Compiled YANG data node.
Definition: tree_schema.h:1412
API for (user) types plugins.
LIBYANG_API_DEF LY_ERR lyplg_type_store_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: string.c:59
const struct lyplg_type_record plugins_string[]
Plugin information for string type implementation.
Definition: string.c:150
const struct lysc_type * realtype
Definition: tree_data.h:614
LIBYANG_API_DECL const char * lyd_value_get_canonical(const struct ly_ctx *ctx, const struct lyd_value *value)
Get the (canonical) value of a lyd_value.
const char * _canonical
Definition: tree_data.h:611
YANG data representation.
Definition: tree_data.h:610
@ 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