28#include "plugins_internal.h"
29#include "tree_schema_internal.h"
46lyplg_type_store_int(
const struct ly_ctx *ctx,
const struct lysc_type *type,
const void *value, uint64_t value_size_bits,
47 uint32_t options,
LY_VALUE_FORMAT format,
void *UNUSED(prefix_data), uint32_t hints,
48 const struct lysc_node *UNUSED(ctx_node),
struct lyd_value *storage,
struct lys_glob_unres *UNUSED(unres),
58 memset(storage, 0,
sizeof *storage);
64 LY_CHECK_GOTO(ret, cleanup);
68 if (value_size >
sizeof num) {
70 lys_datatype2str(type->
basetype), value_size,
sizeof num);
71 LY_CHECK_GOTO(ret, cleanup);
73 memcpy(&num, value, value_size);
78 LY_CHECK_GOTO(ret, cleanup);
83 ret =
lyplg_type_parse_int(
"int8", base, INT64_C(-128), INT64_C(127), value, value_size, &num, err);
86 ret =
lyplg_type_parse_int(
"int16", base, INT64_C(-32768), INT64_C(32767), value, value_size, &num, err);
89 ret =
lyplg_type_parse_int(
"int32", base, INT64_C(-2147483648), INT64_C(2147483647), value, value_size, &num, err);
93 INT64_C(9223372036854775807), value, value_size, &num, err);
99 LY_CHECK_GOTO(ret, cleanup);
109 storage->int16 = num;
110 num = storage->int16;
113 storage->int32 = num;
114 num = storage->int32;
117 storage->int64 = num;
118 num = storage->int64;
128 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
129 LY_CHECK_GOTO(ret, cleanup);
132 LY_CHECK_GOTO(ret, cleanup);
138 LY_CHECK_ERR_GOTO(asprintf(&canon,
"%" PRId8, storage->int8) == -1, ret =
LY_EMEM, cleanup);
141 LY_CHECK_ERR_GOTO(asprintf(&canon,
"%" PRId16, storage->int16) == -1, ret =
LY_EMEM, cleanup);
144 LY_CHECK_ERR_GOTO(asprintf(&canon,
"%" PRId32, storage->int32) == -1, ret =
LY_EMEM, cleanup);
147 LY_CHECK_ERR_GOTO(asprintf(&canon,
"%" PRId64, storage->int64) == -1, ret =
LY_EMEM, cleanup);
155 LY_CHECK_GOTO(ret, cleanup);
160 ret = lyplg_type_validate_value_int(ctx, type, storage, err);
161 LY_CHECK_GOTO(ret, cleanup);
179lyplg_type_validate_value_int(
const struct ly_ctx *UNUSED(ctx),
const struct lysc_type *type,
struct lyd_value *storage,
186 LY_CHECK_ARG_RET(NULL, type, storage, err,
LY_EINVAL);
195 num = storage->int16;
198 num = storage->int32;
201 num = storage->int64;
208 if (type_num->
range) {
226 if (val1->int8 != val2->int8) {
231 if (val1->int16 != val2->int16) {
236 if (val1->int32 != val2->int32) {
241 if (val1->int64 != val2->int64) {
256 if (val1->int8 < val2->int8) {
258 }
else if (val1->int8 > val2->int8) {
265 if (val1->int16 < val2->int16) {
267 }
else if (val1->int16 > val2->int16) {
274 if (val1->int32 < val2->int32) {
276 }
else if (val1->int32 > val2->int32) {
283 if (val1->int64 < val2->int64) {
285 }
else if (val1->int64 > val2->int64) {
298lyplg_type_store_uint(
const struct ly_ctx *ctx,
const struct lysc_type *type,
const void *value, uint64_t value_size_bits,
299 uint32_t options,
LY_VALUE_FORMAT format,
void *UNUSED(prefix_data), uint32_t hints,
300 const struct lysc_node *UNUSED(ctx_node),
struct lyd_value *storage,
struct lys_glob_unres *UNUSED(unres),
310 memset(storage, 0,
sizeof *storage);
316 LY_CHECK_GOTO(ret, cleanup);
320 if (value_size >
sizeof num) {
322 lys_datatype2str(type->
basetype), value_size,
sizeof num);
323 LY_CHECK_GOTO(ret, cleanup);
325 memcpy(&num, value, value_size);
330 LY_CHECK_GOTO(ret, cleanup);
344 ret =
lyplg_type_parse_uint(
"uint64", base, UINT64_C(18446744073709551615), value, value_size, &num, err);
350 LY_CHECK_GOTO(ret, cleanup);
356 storage->uint8 = num;
359 storage->uint16 = num;
362 storage->uint32 = num;
365 storage->uint64 = num;
375 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
376 LY_CHECK_GOTO(ret, cleanup);
379 LY_CHECK_GOTO(ret, cleanup);
383 LY_CHECK_ERR_GOTO(asprintf(&canon,
"%" PRIu64, num) == -1, ret =
LY_EMEM, cleanup);
387 LY_CHECK_GOTO(ret, cleanup);
392 ret = lyplg_type_validate_value_uint(ctx, type, storage, err);
393 LY_CHECK_GOTO(ret, cleanup);
411lyplg_type_validate_value_uint(
const struct ly_ctx *UNUSED(ctx),
const struct lysc_type *type,
struct lyd_value *storage,
418 LY_CHECK_ARG_RET(NULL, type, storage, err,
LY_EINVAL);
424 num = storage->uint8;
427 num = storage->uint16;
430 num = storage->uint32;
433 num = storage->uint64;
440 if (type_num->
range) {
450lyplg_type_compare_uint(
const struct ly_ctx *UNUSED(ctx),
const struct lyd_value *val1,
const struct lyd_value *val2)
454 if (val1->uint8 != val2->uint8) {
459 if (val1->uint16 != val2->uint16) {
464 if (val1->uint32 != val2->uint32) {
469 if (val1->uint64 != val2->uint64) {
484 if (val1->uint8 < val2->uint8) {
486 }
else if (val1->uint8 > val2->uint8) {
493 if (val1->uint16 < val2->uint16) {
495 }
else if (val1->uint16 > val2->uint16) {
502 if (val1->uint32 < val2->uint32) {
504 }
else if (val1->uint32 > val2->uint32) {
511 if (val1->uint64 < val2->uint64) {
513 }
else if (val1->uint64 > val2->uint64) {
527 void *UNUSED(prefix_data),
ly_bool *dynamic, uint64_t *value_size_bits)
556 if (htole64(num) == value->uint64) {
559 if (value_size_bits) {
563 return &value->uint64;
569 buf = calloc(1, bytes_used);
570 LY_CHECK_RET(!buf, NULL);
572 memcpy(buf, &num, bytes_used);
575 if (value_size_bits) {
576 *value_size_bits = bits_used;
586 if (value_size_bits) {
587 *value_size_bits = strlen(value->
_canonical) * 8;
603 .name = LY_TYPE_UINT8_STR,
605 .plugin.id =
"ly2 integers",
607 .plugin.store = lyplg_type_store_uint,
608 .plugin.validate_value = lyplg_type_validate_value_uint,
609 .plugin.validate_tree = NULL,
610 .plugin.compare = lyplg_type_compare_uint,
611 .plugin.sort = lyplg_type_sort_uint,
612 .plugin.print = lyplg_type_print_u_int,
618 .name = LY_TYPE_UINT16_STR,
620 .plugin.id =
"ly2 integers",
622 .plugin.store = lyplg_type_store_uint,
623 .plugin.validate_value = lyplg_type_validate_value_uint,
624 .plugin.validate_tree = NULL,
625 .plugin.compare = lyplg_type_compare_uint,
626 .plugin.sort = lyplg_type_sort_uint,
627 .plugin.print = lyplg_type_print_u_int,
633 .name = LY_TYPE_UINT32_STR,
635 .plugin.id =
"ly2 integers",
637 .plugin.store = lyplg_type_store_uint,
638 .plugin.validate_value = lyplg_type_validate_value_uint,
639 .plugin.validate_tree = NULL,
640 .plugin.compare = lyplg_type_compare_uint,
641 .plugin.sort = lyplg_type_sort_uint,
642 .plugin.print = lyplg_type_print_u_int,
648 .name = LY_TYPE_UINT64_STR,
650 .plugin.id =
"ly2 integers",
652 .plugin.store = lyplg_type_store_uint,
653 .plugin.validate_value = lyplg_type_validate_value_uint,
654 .plugin.validate_tree = NULL,
655 .plugin.compare = lyplg_type_compare_uint,
656 .plugin.sort = lyplg_type_sort_uint,
657 .plugin.print = lyplg_type_print_u_int,
663 .name = LY_TYPE_INT8_STR,
665 .plugin.id =
"ly2 integers",
667 .plugin.store = lyplg_type_store_int,
668 .plugin.validate_value = lyplg_type_validate_value_int,
669 .plugin.validate_tree = NULL,
670 .plugin.compare = lyplg_type_compare_int,
671 .plugin.sort = lyplg_type_sort_int,
672 .plugin.print = lyplg_type_print_u_int,
678 .name = LY_TYPE_INT16_STR,
680 .plugin.id =
"ly2 integers",
682 .plugin.store = lyplg_type_store_int,
683 .plugin.validate_value = lyplg_type_validate_value_int,
684 .plugin.validate_tree = NULL,
685 .plugin.compare = lyplg_type_compare_int,
686 .plugin.sort = lyplg_type_sort_int,
687 .plugin.print = lyplg_type_print_u_int,
693 .name = LY_TYPE_INT32_STR,
695 .plugin.id =
"ly2 integers",
697 .plugin.store = lyplg_type_store_int,
698 .plugin.validate_value = lyplg_type_validate_value_int,
699 .plugin.validate_tree = NULL,
700 .plugin.compare = lyplg_type_compare_int,
701 .plugin.sort = lyplg_type_sort_int,
702 .plugin.print = lyplg_type_print_u_int,
708 .name = LY_TYPE_INT64_STR,
710 .plugin.id =
"ly2 integers",
712 .plugin.store = lyplg_type_store_int,
713 .plugin.validate_value = lyplg_type_validate_value_int,
714 .plugin.validate_tree = NULL,
715 .plugin.compare = lyplg_type_compare_int,
716 .plugin.sort = lyplg_type_sort_int,
717 .plugin.print = lyplg_type_print_u_int,
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.
Libyang full error structure.
const char *const char * revision
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_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 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.
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...
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 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_BITS2BYTES(bits)
Convert bits to bytes.
@ LYPLG_LYB_SIZE_VARIABLE_BITS
LIBYANG_API_DECL void lyplg_type_lyb_size_variable_bits(const struct lysc_type *type, enum lyplg_lyb_size_type *size_type, uint64_t *fixed_size_bits)
Implementation of lyplg_type_lyb_size_clb for a type with variable length in 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
const struct lyplg_type_record plugins_integer[]
Plugin information for integer types implementation.
uint8_t ly_bool
Type to indicate boolean value.
API for (user) types plugins.
const struct lysc_type * realtype
YANG data representation.
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.