29 #include "ly_common.h"
30 #include "plugins_internal.h"
31 #include "tree_schema_internal.h"
48 lyplg_type_store_int(
const struct ly_ctx *ctx,
const struct lysc_type *type,
const void *value, uint32_t value_size_bits,
49 uint32_t options,
LY_VALUE_FORMAT format,
void *UNUSED(prefix_data), uint32_t hints,
60 memset(storage, 0,
sizeof *storage);
66 LY_CHECK_GOTO(ret, cleanup);
70 memcpy(&num, value, value_size);
75 LY_CHECK_GOTO(ret, cleanup);
80 ret =
lyplg_type_parse_int(
"int8", base, INT64_C(-128), INT64_C(127), value, value_size, &num, err);
83 ret =
lyplg_type_parse_int(
"int16", base, INT64_C(-32768), INT64_C(32767), value, value_size, &num, err);
86 ret =
lyplg_type_parse_int(
"int32", base, INT64_C(-2147483648), INT64_C(2147483647), value, value_size, &num, err);
90 INT64_C(9223372036854775807), value, value_size, &num, err);
96 LY_CHECK_GOTO(ret, cleanup);
106 storage->int16 = num;
107 num = storage->int16;
110 storage->int32 = num;
111 num = storage->int32;
114 storage->int64 = num;
115 num = storage->int64;
125 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
126 LY_CHECK_GOTO(ret, cleanup);
129 LY_CHECK_GOTO(ret, cleanup);
135 LY_CHECK_ERR_GOTO(asprintf(&canon,
"%" PRId8, storage->int8) == -1, ret =
LY_EMEM, cleanup);
138 LY_CHECK_ERR_GOTO(asprintf(&canon,
"%" PRId16, storage->int16) == -1, ret =
LY_EMEM, cleanup);
141 LY_CHECK_ERR_GOTO(asprintf(&canon,
"%" PRId32, storage->int32) == -1, ret =
LY_EMEM, cleanup);
144 LY_CHECK_ERR_GOTO(asprintf(&canon,
"%" PRId64, storage->int64) == -1, ret =
LY_EMEM, cleanup);
152 LY_CHECK_GOTO(ret, cleanup);
157 ret = lyplg_type_validate_value_int(ctx, type, storage, err);
158 LY_CHECK_GOTO(ret, cleanup);
162 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
176 lyplg_type_validate_value_int(
const struct ly_ctx *UNUSED(ctx),
const struct lysc_type *type,
struct lyd_value *storage,
183 LY_CHECK_ARG_RET(NULL, type, storage, err,
LY_EINVAL);
192 num = storage->int16;
195 num = storage->int32;
198 num = storage->int64;
205 if (type_num->
range) {
215 lyplg_type_compare_int(
const struct ly_ctx *UNUSED(ctx),
const struct lyd_value *val1,
const struct lyd_value *val2)
223 if (val1->int8 != val2->int8) {
228 if (val1->int16 != val2->int16) {
233 if (val1->int32 != val2->int32) {
238 if (val1->int64 != val2->int64) {
253 if (val1->int8 < val2->int8) {
255 }
else if (val1->int8 > val2->int8) {
262 if (val1->int16 < val2->int16) {
264 }
else if (val1->int16 > val2->int16) {
271 if (val1->int32 < val2->int32) {
273 }
else if (val1->int32 > val2->int32) {
280 if (val1->int64 < val2->int64) {
282 }
else if (val1->int64 > val2->int64) {
296 void *UNUSED(prefix_data),
ly_bool *dynamic, uint32_t *value_size_bits)
298 int64_t prev_num = 0, num = 0;
304 prev_num = num = value->int8;
307 prev_num = num = value->int16;
310 prev_num = num = value->int32;
313 prev_num = num = value->int64;
320 if (num == prev_num) {
323 if (value_size_bits) {
327 return &value->int64;
331 LY_CHECK_RET(!buf, NULL);
334 if (value_size_bits) {
346 if (value_size_bits) {
347 *value_size_bits = strlen(value->
_canonical) * 8;
353 lyplg_type_store_uint(
const struct ly_ctx *ctx,
const struct lysc_type *type,
const void *value, uint32_t value_size_bits,
354 uint32_t options,
LY_VALUE_FORMAT format,
void *UNUSED(prefix_data), uint32_t hints,
365 memset(storage, 0,
sizeof *storage);
371 LY_CHECK_GOTO(ret, cleanup);
375 memcpy(&num, value, value_size);
380 LY_CHECK_GOTO(ret, cleanup);
394 ret =
lyplg_type_parse_uint(
"uint64", base, UINT64_C(18446744073709551615), value, value_size, &num, err);
400 LY_CHECK_GOTO(ret, cleanup);
406 storage->uint8 = num;
409 storage->uint16 = num;
412 storage->uint32 = num;
415 storage->uint64 = num;
423 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
425 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
426 LY_CHECK_GOTO(ret, cleanup);
429 LY_CHECK_GOTO(ret, cleanup);
433 LY_CHECK_ERR_GOTO(asprintf(&canon,
"%" PRIu64, num) == -1, ret =
LY_EMEM, cleanup);
437 LY_CHECK_GOTO(ret, cleanup);
440 if (!(options & LYPLG_TYPE_STORE_ONLY)) {
442 ret = lyplg_type_validate_value_uint(ctx, type, storage, err);
443 LY_CHECK_GOTO(ret, cleanup);
447 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
461 lyplg_type_validate_value_uint(
const struct ly_ctx *UNUSED(ctx),
const struct lysc_type *type,
struct lyd_value *storage,
468 LY_CHECK_ARG_RET(NULL, type, storage, err,
LY_EINVAL);
474 num = storage->uint8;
477 num = storage->uint16;
480 num = storage->uint32;
483 num = storage->uint64;
490 if (type_num->
range) {
500 lyplg_type_compare_uint(
const struct ly_ctx *UNUSED(ctx),
const struct lyd_value *val1,
const struct lyd_value *val2)
504 if (val1->uint8 != val2->uint8) {
509 if (val1->uint16 != val2->uint16) {
514 if (val1->uint32 != val2->uint32) {
519 if (val1->uint64 != val2->uint64) {
534 if (val1->uint8 < val2->uint8) {
536 }
else if (val1->uint8 > val2->uint8) {
543 if (val1->uint16 < val2->uint16) {
545 }
else if (val1->uint16 > val2->uint16) {
552 if (val1->uint32 < val2->uint32) {
554 }
else if (val1->uint32 > val2->uint32) {
561 if (val1->uint64 < val2->uint64) {
563 }
else if (val1->uint64 > val2->uint64) {
577 void *UNUSED(prefix_data),
ly_bool *dynamic, uint32_t *value_size_bits)
600 if (num == value->uint64) {
603 if (value_size_bits) {
607 return &value->uint64;
611 LY_CHECK_RET(!buf, NULL);
614 if (value_size_bits) {
626 if (value_size_bits) {
627 *value_size_bits = strlen(value->
_canonical) * 8;
643 .name = LY_TYPE_UINT8_STR,
645 .plugin.id =
"ly2 integers",
647 .plugin.store = lyplg_type_store_uint,
648 .plugin.validate_value = lyplg_type_validate_value_uint,
649 .plugin.validate_tree = NULL,
650 .plugin.compare = lyplg_type_compare_uint,
651 .plugin.sort = lyplg_type_sort_uint,
652 .plugin.print = lyplg_type_print_uint,
658 .name = LY_TYPE_UINT16_STR,
660 .plugin.id =
"ly2 integers",
662 .plugin.store = lyplg_type_store_uint,
663 .plugin.validate_value = lyplg_type_validate_value_uint,
664 .plugin.validate_tree = NULL,
665 .plugin.compare = lyplg_type_compare_uint,
666 .plugin.sort = lyplg_type_sort_uint,
667 .plugin.print = lyplg_type_print_uint,
673 .name = LY_TYPE_UINT32_STR,
675 .plugin.id =
"ly2 integers",
677 .plugin.store = lyplg_type_store_uint,
678 .plugin.validate_value = lyplg_type_validate_value_uint,
679 .plugin.validate_tree = NULL,
680 .plugin.compare = lyplg_type_compare_uint,
681 .plugin.sort = lyplg_type_sort_uint,
682 .plugin.print = lyplg_type_print_uint,
688 .name = LY_TYPE_UINT64_STR,
690 .plugin.id =
"ly2 integers",
692 .plugin.store = lyplg_type_store_uint,
693 .plugin.validate_value = lyplg_type_validate_value_uint,
694 .plugin.validate_tree = NULL,
695 .plugin.compare = lyplg_type_compare_uint,
696 .plugin.sort = lyplg_type_sort_uint,
697 .plugin.print = lyplg_type_print_uint,
703 .name = LY_TYPE_INT8_STR,
705 .plugin.id =
"ly2 integers",
707 .plugin.store = lyplg_type_store_int,
708 .plugin.validate_value = lyplg_type_validate_value_int,
709 .plugin.validate_tree = NULL,
710 .plugin.compare = lyplg_type_compare_int,
711 .plugin.sort = lyplg_type_sort_int,
712 .plugin.print = lyplg_type_print_int,
718 .name = LY_TYPE_INT16_STR,
720 .plugin.id =
"ly2 integers",
722 .plugin.store = lyplg_type_store_int,
723 .plugin.validate_value = lyplg_type_validate_value_int,
724 .plugin.validate_tree = NULL,
725 .plugin.compare = lyplg_type_compare_int,
726 .plugin.sort = lyplg_type_sort_int,
727 .plugin.print = lyplg_type_print_int,
733 .name = LY_TYPE_INT32_STR,
735 .plugin.id =
"ly2 integers",
737 .plugin.store = lyplg_type_store_int,
738 .plugin.validate_value = lyplg_type_validate_value_int,
739 .plugin.validate_tree = NULL,
740 .plugin.compare = lyplg_type_compare_int,
741 .plugin.sort = lyplg_type_sort_int,
742 .plugin.print = lyplg_type_print_int,
748 .name = LY_TYPE_INT64_STR,
750 .plugin.id =
"ly2 integers",
752 .plugin.store = lyplg_type_store_int,
753 .plugin.validate_value = lyplg_type_validate_value_int,
754 .plugin.validate_tree = NULL,
755 .plugin.compare = lyplg_type_compare_int,
756 .plugin.sort = lyplg_type_sort_int,
757 .plugin.print = lyplg_type_print_int,
LIBYANG_API_DECL void lyplg_type_lyb_size_variable_bits(const struct lysc_type *type, enum lyplg_lyb_size_type *size_type, uint32_t *fixed_size_bits)
Implementation of lyplg_type_lyb_size_clb for a type with variable length in bits.
struct lysc_type * realtype
memset(value->fixed_mem, 0, LYD_VALUE_FIXED_MEM_SIZE)
LIBYANG_API_DECL LY_ERR lyplg_type_check_value_size(const char *type_name, LY_VALUE_FORMAT format, uint32_t value_size_bits, enum lyplg_lyb_size_type lyb_size_type, uint32_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.
YANG extension compiled instance.
struct lyplg_type_record plugins_integer[]
Plugin information for integer types implementation.
LY_ERR
libyang's error codes returned by the libyang functions.
uint8_t ly_bool
Type to indicate boolean value.
#define LYPLG_TYPE_STORE_DYNAMIC
LIBYANG_API_DECL LY_ERR lyplg_type_parse_int(const char *datatype, int base, int64_t min, int64_t max, const char *value, uint32_t value_len, int64_t *ret, struct ly_err_item **err)
Unsigned integer value parser and validator.
The main libyang public header.
YANG data representation.
Libyang full error structure.
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...
struct lysc_range * range
LIBYANG_API_DECL LY_ERR lyplg_type_parse_uint(const char *datatype, int base, uint64_t max, const char *value, uint32_t value_len, uint64_t *ret, struct ly_err_item **err)
Unsigned integer value parser and validator.
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.
LIBYANG_API_DECL uint64_t lyplg_type_get_highest_set_bit_pos(uint64_t num)
Learn the position of the highest set bit in a number. Represents also the least amount of bits requi...
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
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_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.
API for (user) types plugins.
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.
#define LYPLG_BITS2BYTES(bits)
Convert bits to bytes.
#define LYPLG_TYPE_STORE_ONLY