libyang  6.4.13
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
decimal64.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 
35 static LY_ERR lyplg_type_validate_value_decimal64(const struct ly_ctx *ctx, const struct lysc_type *type,
36  struct lyd_value *storage, struct ly_err_item **err);
37 
46 static LY_ERR
47 decimal64_num2str(int64_t num, struct lysc_type_dec *type, char **str)
48 {
49  char *ret;
50 
51  /* allocate the value */
52  ret = calloc(1, LY_NUMBER_MAXLEN);
53  LY_CHECK_RET(!ret, LY_EMEM);
54 
55  if (num) {
56  int count = sprintf(ret, "%" PRId64 " ", num);
57 
58  if (((num > 0) && ((count - 1) <= type->fraction_digits)) || ((count - 2) <= type->fraction_digits)) {
59  /* we have 0. value, print the value with the leading zeros
60  * (one for 0. and also keep the correct with of num according
61  * to fraction-digits value)
62  * for (num < 0) - extra character for '-' sign */
63  count = sprintf(ret, "%0*" PRId64 " ", (num > 0) ? (type->fraction_digits + 1) : (type->fraction_digits + 2), num);
64  }
65  for (uint8_t i = type->fraction_digits, j = 1; i > 0; i--) {
66  if (j && (i > 1) && (ret[count - 2] == '0')) {
67  /* we have trailing zero to skip */
68  ret[count - 1] = '\0';
69  } else {
70  j = 0;
71  ret[count - 1] = ret[count - 2];
72  }
73  count--;
74  }
75  ret[count - 1] = '.';
76  } else {
77  /* zero */
78  sprintf(ret, "0.0");
79  }
80 
81  *str = ret;
82  return LY_SUCCESS;
83 }
84 
85 static void
86 lyplg_type_lyb_size_decimal64(const struct lysc_type *UNUSED(type), enum lyplg_lyb_size_type *size_type,
87  uint64_t *fixed_size_bits)
88 {
89  *size_type = LYPLG_LYB_SIZE_FIXED_BITS;
90  *fixed_size_bits = 64;
91 }
92 
93 static LY_ERR
94 lyplg_type_store_decimal64(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, uint64_t value_size_bits,
95  uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
96  const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
97  struct ly_err_item **err)
98 {
99  struct lysc_type_dec *type_dec = (struct lysc_type_dec *)type;
100  LY_ERR ret = LY_SUCCESS;
101  uint32_t value_size;
102  int64_t num = 0;
103  char *canon;
104 
105  /* init storage */
106  memset(storage, 0, sizeof *storage);
107  storage->realtype = type;
108 
109  /* check value length */
110  ret = lyplg_type_check_value_size("decimal64", format, value_size_bits, LYPLG_LYB_SIZE_FIXED_BITS, 64, &value_size, err);
111  LY_CHECK_GOTO(ret, cleanup);
112 
113  if (format == LY_VALUE_LYB) {
114  /* we have the decimal64 number, in host byte order */
115  memcpy(&num, value, value_size);
116  num = le64toh(num);
117  } else {
118  /* check hints */
119  ret = lyplg_type_check_hints(hints, value, value_size, type->basetype, NULL, err);
120  LY_CHECK_GOTO(ret, cleanup);
121 
122  /* parse decimal64 value */
123  ret = lyplg_type_parse_dec64(type_dec->fraction_digits, value, value_size, &num, err);
124  LY_CHECK_GOTO(ret, cleanup);
125  }
126 
127  /* store value */
128  storage->dec64 = num;
129 
130  /* we need canonical value for the range check */
131  if (format == LY_VALUE_CANON) {
132  /* store canonical value */
133  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
134  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
135  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
136  LY_CHECK_GOTO(ret, cleanup);
137  } else {
138  ret = lydict_insert(ctx, value, value_size, &storage->_canonical);
139  LY_CHECK_GOTO(ret, cleanup);
140  }
141  } else {
142  /* generate canonical value */
143  ret = decimal64_num2str(num, type_dec, &canon);
144  LY_CHECK_GOTO(ret, cleanup);
145 
146  /* store it */
147  ret = lydict_insert_zc(ctx, canon, (const char **)&storage->_canonical);
148  LY_CHECK_GOTO(ret, cleanup);
149  }
150 
151  if (!(options & LYPLG_TYPE_STORE_ONLY)) {
152  /* validate value */
153  ret = lyplg_type_validate_value_decimal64(ctx, type, storage, err);
154  LY_CHECK_GOTO(ret, cleanup);
155  }
156 
157 cleanup:
158  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
159  free((void *)value);
160  }
161 
162  if (ret) {
163  lyplg_type_free_simple(ctx, storage);
164  }
165  return ret;
166 }
167 
171 static LY_ERR
172 lyplg_type_validate_value_decimal64(const struct ly_ctx *UNUSED(ctx), const struct lysc_type *type,
173  struct lyd_value *storage, struct ly_err_item **err)
174 {
175  LY_ERR ret;
176  struct lysc_type_dec *type_dec = (struct lysc_type_dec *)type;
177  int64_t num;
178 
179  LY_CHECK_ARG_RET(NULL, type, storage, err, LY_EINVAL);
180  *err = NULL;
181  num = storage->dec64;
182 
183  if (type_dec->range) {
184  /* check range of the number */
185  ret = lyplg_type_validate_range(type->basetype, type_dec->range, num, storage->_canonical,
186  strlen(storage->_canonical), err);
187  LY_CHECK_RET(ret);
188  }
189 
190  return LY_SUCCESS;
191 }
192 
193 static LY_ERR
194 lyplg_type_compare_decimal64(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
195  const struct lyd_value *val2)
196 {
197  /* if type is the same, the fraction digits are, too */
198  if (val1->dec64 != val2->dec64) {
199  return LY_ENOT;
200  }
201  return LY_SUCCESS;
202 }
203 
204 static int
205 lyplg_type_sort_decimal64(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1, const struct lyd_value *val2)
206 {
207  if (val1->dec64 > val2->dec64) {
208  return 1;
209  } else if (val1->dec64 < val2->dec64) {
210  return -1;
211  } else {
212  return 0;
213  }
214 }
215 
216 static const void *
217 lyplg_type_print_decimal64(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *value, LY_VALUE_FORMAT format,
218  void *UNUSED(prefix_data), ly_bool *dynamic, uint64_t *value_size_bits)
219 {
220  int64_t num = 0;
221  void *buf;
222 
223  if (format == LY_VALUE_LYB) {
224  num = htole64(value->dec64);
225  if (num == value->dec64) {
226  /* values are equal, little-endian */
227  *dynamic = 0;
228  if (value_size_bits) {
229  *value_size_bits = sizeof value->dec64 * 8;
230  }
231  return &value->dec64;
232  } else {
233  /* values differ, big-endian */
234  buf = calloc(1, sizeof value->dec64);
235  LY_CHECK_RET(!buf, NULL);
236 
237  *dynamic = 1;
238  if (value_size_bits) {
239  *value_size_bits = sizeof value->dec64 * 8;
240  }
241  memcpy(buf, &num, sizeof value->dec64);
242  return buf;
243  }
244  }
245 
246  /* use the cached canonical value */
247  if (dynamic) {
248  *dynamic = 0;
249  }
250  if (value_size_bits) {
251  *value_size_bits = strlen(value->_canonical) * 8;
252  }
253  return value->_canonical;
254 }
255 
263 const struct lyplg_type_record plugins_decimal64[] = {
264  {
265  .module = "",
266  .revision = NULL,
267  .name = LY_TYPE_DEC64_STR,
268 
269  .plugin.id = "ly2 decimal64",
270  .plugin.lyb_size = lyplg_type_lyb_size_decimal64,
271  .plugin.store = lyplg_type_store_decimal64,
272  .plugin.validate_value = lyplg_type_validate_value_decimal64,
273  .plugin.validate_tree = NULL,
274  .plugin.compare = lyplg_type_compare_decimal64,
275  .plugin.sort = lyplg_type_sort_decimal64,
276  .plugin.print = lyplg_type_print_decimal64,
277  .plugin.duplicate = lyplg_type_dup_simple,
278  .plugin.free = lyplg_type_free_simple,
279  },
280  {0}
281 };
const struct lyplg_type_record plugins_decimal64[]
Plugin information for decimal64 type implementation.
Definition: decimal64.c:263
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
@ LY_EINVAL
Definition: log.h:256
@ LY_EMEM
Definition: log.h:254
@ LY_ENOT
Definition: log.h:266
@ LY_SUCCESS
Definition: log.h:253
Libyang full error structure.
Definition: log.h:298
const char * module
lyplg_lyb_size_type
Type of the LYB size of a value of a particular type.
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_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 lyplg_type_parse_dec64(uint8_t fraction_digits, const char *value, uint32_t value_len, int64_t *ret, struct ly_err_item **err)
Convert a string with a decimal64 value into libyang representation: ret = value * 10^fraction-digits...
@ LYPLG_LYB_SIZE_FIXED_BITS
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.
#define LYPLG_TYPE_STORE_DYNAMIC
#define LYPLG_TYPE_STORE_ONLY
struct lysc_range * range
Definition: tree_schema.h:1301
LY_DATA_TYPE basetype
Definition: tree_schema.h:1278
uint8_t fraction_digits
Definition: tree_schema.h:1300
Compiled YANG data node.
Definition: tree_schema.h:1412
uint8_t ly_bool
Type to indicate boolean value.
Definition: log.h:29
API for (user) types plugins.
const struct lysc_type * realtype
Definition: tree_data.h:614
const char * _canonical
Definition: tree_data.h:611
YANG data representation.
Definition: tree_data.h:610
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition: utils.h:93
@ LY_VALUE_CANON
Definition: utils.h:94
@ LY_VALUE_LYB
Definition: utils.h:100