libyang  3.6.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
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 "libyang.h"
22 
23 /* additional internal headers for some useful simple macros */
24 #include "compat.h"
25 #include "ly_common.h"
26 #include "plugins_internal.h" /* LY_TYPE_*_STR */
27 
45 static LY_ERR
46 string_check_chars(const char *value, size_t value_len, struct ly_err_item **err)
47 {
48  size_t len, parsed = 0;
49 
50  while (value_len - parsed) {
51  if (ly_checkutf8(value + parsed, value_len - parsed, &len)) {
52  return ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid character 0x%hhx.", value[parsed]);
53  }
54  parsed += len;
55  }
56 
57  return LY_SUCCESS;
58 }
59 
60 LIBYANG_API_DEF LY_ERR
61 lyplg_type_store_string(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, size_t value_len,
62  uint32_t options, LY_VALUE_FORMAT UNUSED(format), void *UNUSED(prefix_data), uint32_t hints,
63  const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
64  struct ly_err_item **err)
65 {
66  struct lysc_type_str *type_str = (struct lysc_type_str *)type;
67  LY_ERR ret = LY_SUCCESS;
68 
69  /* init storage */
70  memset(storage, 0, sizeof *storage);
71  storage->realtype = type;
72 
73  if (!(options & LYPLG_TYPE_STORE_IS_UTF8)) {
74  /* check the UTF-8 encoding */
75  ret = string_check_chars(value, value_len, err);
76  LY_CHECK_GOTO(ret, cleanup);
77  }
78 
79  /* check hints */
80  ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
81  LY_CHECK_GOTO(ret, cleanup);
82 
83  /* store canonical value */
84  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
85  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
86  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
87  LY_CHECK_GOTO(ret, cleanup);
88 
89  /* value may have been freed */
90  value = storage->_canonical;
91  } else {
92  ret = lydict_insert(ctx, value_len ? value : "", value_len, &storage->_canonical);
93  LY_CHECK_GOTO(ret, cleanup);
94  }
95 
96  if (!(options & LYPLG_TYPE_STORE_ONLY)) {
97  /* length restriction of the string */
98  if (type_str->length) {
99  /* value_len is in bytes, but we need number of characters here */
100  ret = lyplg_type_validate_range(LY_TYPE_STRING, type_str->length, ly_utf8len(value, value_len), value, value_len, err);
101  LY_CHECK_GOTO(ret, cleanup);
102  }
103 
104  /* pattern restrictions */
105  ret = lyplg_type_validate_patterns(type_str->patterns, value, value_len, err);
106  LY_CHECK_GOTO(ret, cleanup);
107  }
108 
109 cleanup:
110  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
111  free((void *)value);
112  }
113 
114  if (ret) {
115  lyplg_type_free_simple(ctx, storage);
116  }
117  return ret;
118 }
119 
128  {
129  .module = "",
130  .revision = NULL,
131  .name = LY_TYPE_STRING_STR,
132 
133  .plugin.id = "libyang 2 - string, version 1",
134  .plugin.store = lyplg_type_store_string,
135  .plugin.validate = NULL,
136  .plugin.compare = lyplg_type_compare_simple,
137  .plugin.sort = lyplg_type_sort_simple,
138  .plugin.print = lyplg_type_print_simple,
139  .plugin.duplicate = lyplg_type_dup_simple,
140  .plugin.free = lyplg_type_free_simple,
141  .plugin.lyb_data_len = -1,
142  },
143  {0}
144 };
struct lysc_type * realtype
Definition: tree_data.h:579
Compiled YANG data node.
Definition: tree_schema.h:1439
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.
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:575
const char * _canonical
Definition: tree_data.h:576
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
Definition: log.h:277
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.
#define LYPLG_TYPE_STORE_IS_UTF8
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 lyplg_type_store_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 *prefix_data, uint32_t hints, const struct lysc_node *ctx_node, struct lyd_value *storage, struct lys_glob_unres *unres, struct ly_err_item **err)
Implementation of lyplg_type_store_clb for the built-in string type.
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
Definition: log.h:248
LY_DATA_TYPE basetype
Definition: tree_schema.h:1305
struct lyplg_type_record plugins_string[]
Plugin information for string type implementation.
Definition: string.c:127
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.
#define LYPLG_TYPE_STORE_ONLY
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.