libyang 3.1.0
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
Loading...
Searching...
No Matches
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
37static LY_ERR lyplg_type_validate_string(const struct ly_ctx *UNUSED(ctx), const struct lysc_type *type, const struct lyd_node *UNUSED(ctx_node), const struct lyd_node *UNUSED(tree), struct lyd_value *storage, struct ly_err_item **err);
38
47static LY_ERR
48string_check_chars(const char *value, size_t value_len, struct ly_err_item **err)
49{
50 size_t len, parsed = 0;
51
52 while (value_len - parsed) {
53 if (ly_checkutf8(value + parsed, value_len - parsed, &len)) {
54 return ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid character 0x%hhx.", value[parsed]);
55 }
56 parsed += len;
57 }
58
59 return LY_SUCCESS;
60}
61
62LIBYANG_API_DEF LY_ERR
63lyplg_type_store_string(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, size_t value_len,
64 uint32_t options, LY_VALUE_FORMAT UNUSED(format), void *UNUSED(prefix_data), uint32_t hints,
65 const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
66 struct ly_err_item **err)
67{
68 LY_ERR ret = LY_SUCCESS;
69
70 /* init storage */
71 memset(storage, 0, sizeof *storage);
72 storage->realtype = type;
73
74 if (!(options & LYPLG_TYPE_STORE_IS_UTF8)) {
75 /* check the UTF-8 encoding */
76 ret = string_check_chars(value, value_len, err);
77 LY_CHECK_GOTO(ret, cleanup);
78 }
79
80 /* check hints */
81 ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
82 LY_CHECK_GOTO(ret, cleanup);
83
84 /* store canonical value */
85 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
86 ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
87 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
88 LY_CHECK_GOTO(ret, cleanup);
89 } else {
90 ret = lydict_insert(ctx, value_len ? value : "", value_len, &storage->_canonical);
91 LY_CHECK_GOTO(ret, cleanup);
92 }
93
94 if (!(options & LYPLG_TYPE_STORE_ONLY)) {
95 /* validate value */
96 ret = lyplg_type_validate_string(ctx, type, NULL, NULL, storage, err);
97 LY_CHECK_GOTO(ret, cleanup);
98 }
99
100cleanup:
101 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
102 free((void *)value);
103 }
104
105 if (ret) {
106 lyplg_type_free_simple(ctx, storage);
107 }
108 return ret;
109}
110
114static LY_ERR
115lyplg_type_validate_string(const struct ly_ctx *ctx, const struct lysc_type *type, const struct lyd_node *UNUSED(ctx_node),
116 const struct lyd_node *UNUSED(tree), struct lyd_value *storage, struct ly_err_item **err)
117{
118 LY_ERR ret;
119 struct lysc_type_str *type_str = (struct lysc_type_str *)type;
120 const void *value;
121 size_t value_len;
122
123 LY_CHECK_ARG_RET(NULL, type, storage, err, LY_EINVAL);
124 value = lyd_value_get_canonical(ctx, storage);
125 value_len = strlen(value);
126 *err = NULL;
127
128 /* length restriction of the string */
129 if (type_str->length) {
130 /* value_len is in bytes, but we need number of characters here */
131 ret = lyplg_type_validate_range(LY_TYPE_STRING, type_str->length, ly_utf8len(value, value_len), value, value_len, err);
132 LY_CHECK_RET(ret);
133 }
134
135 /* pattern restrictions */
136 ret = lyplg_type_validate_patterns(type_str->patterns, value, value_len, err);
137 LY_CHECK_RET(ret);
138
139 return LY_SUCCESS;
140}
141
150 {
151 .module = "",
152 .revision = NULL,
153 .name = LY_TYPE_STRING_STR,
154
155 .plugin.id = "libyang 2 - string, version 1",
156 .plugin.store = lyplg_type_store_string,
157 .plugin.validate = lyplg_type_validate_string,
158 .plugin.compare = lyplg_type_compare_simple,
159 .plugin.sort = lyplg_type_sort_simple,
160 .plugin.print = lyplg_type_print_simple,
161 .plugin.duplicate = lyplg_type_dup_simple,
162 .plugin.free = lyplg_type_free_simple,
163 .plugin.lyb_data_len = -1,
164 },
165 {0}
166};
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:237
@ LYVE_DATA
Definition log.h:274
@ LY_EINVAL
Definition log.h:241
@ LY_EVALID
Definition log.h:245
@ LY_SUCCESS
Definition log.h:238
Libyang full error structure.
Definition log.h:282
const char *const char * revision
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_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.
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's hints (if any) in the specified format.
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.
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.
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.
#define LYPLG_TYPE_STORE_DYNAMIC
#define LYPLG_TYPE_STORE_IS_UTF8
#define LYPLG_TYPE_STORE_ONLY
LY_DATA_TYPE basetype
struct lysc_pattern ** patterns
struct lysc_range * length
Compiled YANG data node.
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition tree.h:234
@ LY_TYPE_STRING
Definition tree.h:209
The main libyang public header.
API for (user) types plugins.
const struct lyplg_type_record plugins_string[]
Plugin information for string type implementation.
Definition string.c:149
LIBYANG_API_DEF 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 UNUSED(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:63
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 struct lysc_type * realtype
Definition tree_data.h:575
const char * _canonical
Definition tree_data.h:572
Generic structure for a data node.
Definition tree_data.h:799
YANG data representation.
Definition tree_data.h:571