libyang 2.1.148
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
Loading...
Searching...
No Matches
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 "common.h"
28#include "compat.h"
29
39LIBYANG_API_DEF LY_ERR
40lyplg_type_store_hex_string(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, size_t value_len,
41 uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
42 const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
43 struct ly_err_item **err)
44{
45 LY_ERR ret = LY_SUCCESS;
46 struct lysc_type_str *type_str = (struct lysc_type_str *)type;
47 uint32_t i;
48
49 /* init storage */
50 memset(storage, 0, sizeof *storage);
51 storage->realtype = type;
52
53 /* check hints */
54 ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
55 LY_CHECK_GOTO(ret, cleanup);
56
57 /* length restriction of the string */
58 if (type_str->length) {
59 /* value_len is in bytes, but we need number of characters here */
60 ret = lyplg_type_validate_range(LY_TYPE_STRING, type_str->length, ly_utf8len(value, value_len), value, value_len, err);
61 LY_CHECK_GOTO(ret, cleanup);
62 }
63
64 /* pattern restrictions */
65 ret = lyplg_type_validate_patterns(type_str->patterns, value, value_len, err);
66 LY_CHECK_GOTO(ret, cleanup);
67
68 /* make a copy, it is needed for canonization */
69 if ((format != LY_VALUE_CANON) && !(options & LYPLG_TYPE_STORE_DYNAMIC)) {
70 value = strndup(value, value_len);
71 LY_CHECK_ERR_GOTO(!value, ret = LY_EMEM, cleanup);
72 options |= LYPLG_TYPE_STORE_DYNAMIC;
73 }
74
75 /* store canonical value */
76 if (format != LY_VALUE_CANON) {
77 /* make lowercase and store, the value must be dynamic */
78 for (i = 0; i < value_len; ++i) {
79 ((char *)value)[i] = tolower(((char *)value)[i]);
80 }
81
82 ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
83 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
84 LY_CHECK_GOTO(ret, cleanup);
85 } else {
86 /* store directly */
87 ret = lydict_insert(ctx, value_len ? value : "", value_len, &storage->_canonical);
88 LY_CHECK_GOTO(ret, cleanup);
89 }
90
91cleanup:
92 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
93 free((void *)value);
94 }
95
96 if (ret) {
97 lyplg_type_free_simple(ctx, storage);
98 }
99 return ret;
100}
101
110 {
111 .module = "ietf-yang-types",
112 .revision = "2013-07-15",
113 .name = "phys-address",
114
115 .plugin.id = "libyang 2 - hex-string, version 1",
116 .plugin.store = lyplg_type_store_hex_string,
117 .plugin.validate = NULL,
118 .plugin.compare = lyplg_type_compare_simple,
119 .plugin.sort = NULL,
120 .plugin.print = lyplg_type_print_simple,
121 .plugin.duplicate = lyplg_type_dup_simple,
122 .plugin.free = lyplg_type_free_simple,
123 .plugin.lyb_data_len = -1,
124 },
125 {
126 .module = "ietf-yang-types",
127 .revision = "2013-07-15",
128 .name = "mac-address",
129
130 .plugin.id = "libyang 2 - hex-string, version 1",
131 .plugin.store = lyplg_type_store_hex_string,
132 .plugin.validate = NULL,
133 .plugin.compare = lyplg_type_compare_simple,
134 .plugin.sort = NULL,
135 .plugin.print = lyplg_type_print_simple,
136 .plugin.duplicate = lyplg_type_dup_simple,
137 .plugin.free = lyplg_type_free_simple,
138 .plugin.lyb_data_len = -1,
139 },
140 {
141 .module = "ietf-yang-types",
142 .revision = "2013-07-15",
143 .name = "hex-string",
144
145 .plugin.id = "libyang 2 - hex-string, version 1",
146 .plugin.store = lyplg_type_store_hex_string,
147 .plugin.validate = NULL,
148 .plugin.compare = lyplg_type_compare_simple,
149 .plugin.sort = NULL,
150 .plugin.print = lyplg_type_print_simple,
151 .plugin.duplicate = lyplg_type_dup_simple,
152 .plugin.free = lyplg_type_free_simple,
153 .plugin.lyb_data_len = -1,
154 },
155 {
156 .module = "ietf-yang-types",
157 .revision = "2013-07-15",
158 .name = "uuid",
159
160 .plugin.id = "libyang 2 - hex-string, version 1",
161 .plugin.store = lyplg_type_store_hex_string,
162 .plugin.validate = NULL,
163 .plugin.compare = lyplg_type_compare_simple,
164 .plugin.sort = NULL,
165 .plugin.print = lyplg_type_print_simple,
166 .plugin.duplicate = lyplg_type_dup_simple,
167 .plugin.free = lyplg_type_free_simple,
168 .plugin.lyb_data_len = -1,
169 },
170 {0}
171};
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:248
@ LY_EMEM
Definition log.h:250
@ LY_SUCCESS
Definition log.h:249
Libyang full error structure.
Definition log.h:293
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 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_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 lyd_value *val1, const struct lyd_value *val2)
Implementation of lyplg_type_compare_clb for a generic simple type.
#define LYPLG_TYPE_STORE_DYNAMIC
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
@ LY_VALUE_CANON
Definition tree.h:235
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:40
const struct lyplg_type_record plugins_hex_string[]
Plugin information for string type implementation.
Definition hex_string.c:109
The main libyang public header.
API for (user) types plugins.
const struct lysc_type * realtype
Definition tree_data.h:564
const char * _canonical
Definition tree_data.h:561
YANG data representation.
Definition tree_data.h:560